Commit | Line | Data |
---|---|---|
252b5132 | 1 | /* ELF executable support for BFD. |
340b6d91 | 2 | |
82704155 | 3 | Copyright (C) 1993-2019 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 *); |
217aa764 | 54 | static bfd_boolean prep_headers (bfd *); |
ef10c3ac | 55 | static bfd_boolean swap_out_syms (bfd *, struct elf_strtab_hash **, int) ; |
276da9b3 L |
56 | static bfd_boolean elf_read_notes (bfd *, file_ptr, bfd_size_type, |
57 | size_t align) ; | |
718175fa | 58 | static bfd_boolean elf_parse_notes (bfd *abfd, char *buf, size_t size, |
276da9b3 | 59 | file_ptr offset, size_t align); |
50b2bdb7 | 60 | |
252b5132 RH |
61 | /* Swap version information in and out. The version information is |
62 | currently size independent. If that ever changes, this code will | |
63 | need to move into elfcode.h. */ | |
64 | ||
65 | /* Swap in a Verdef structure. */ | |
66 | ||
67 | void | |
217aa764 AM |
68 | _bfd_elf_swap_verdef_in (bfd *abfd, |
69 | const Elf_External_Verdef *src, | |
70 | Elf_Internal_Verdef *dst) | |
252b5132 | 71 | { |
dc810e39 AM |
72 | dst->vd_version = H_GET_16 (abfd, src->vd_version); |
73 | dst->vd_flags = H_GET_16 (abfd, src->vd_flags); | |
74 | dst->vd_ndx = H_GET_16 (abfd, src->vd_ndx); | |
75 | dst->vd_cnt = H_GET_16 (abfd, src->vd_cnt); | |
76 | dst->vd_hash = H_GET_32 (abfd, src->vd_hash); | |
77 | dst->vd_aux = H_GET_32 (abfd, src->vd_aux); | |
78 | dst->vd_next = H_GET_32 (abfd, src->vd_next); | |
252b5132 RH |
79 | } |
80 | ||
81 | /* Swap out a Verdef structure. */ | |
82 | ||
83 | void | |
217aa764 AM |
84 | _bfd_elf_swap_verdef_out (bfd *abfd, |
85 | const Elf_Internal_Verdef *src, | |
86 | Elf_External_Verdef *dst) | |
252b5132 | 87 | { |
dc810e39 AM |
88 | H_PUT_16 (abfd, src->vd_version, dst->vd_version); |
89 | H_PUT_16 (abfd, src->vd_flags, dst->vd_flags); | |
90 | H_PUT_16 (abfd, src->vd_ndx, dst->vd_ndx); | |
91 | H_PUT_16 (abfd, src->vd_cnt, dst->vd_cnt); | |
92 | H_PUT_32 (abfd, src->vd_hash, dst->vd_hash); | |
93 | H_PUT_32 (abfd, src->vd_aux, dst->vd_aux); | |
94 | H_PUT_32 (abfd, src->vd_next, dst->vd_next); | |
252b5132 RH |
95 | } |
96 | ||
97 | /* Swap in a Verdaux structure. */ | |
98 | ||
99 | void | |
217aa764 AM |
100 | _bfd_elf_swap_verdaux_in (bfd *abfd, |
101 | const Elf_External_Verdaux *src, | |
102 | Elf_Internal_Verdaux *dst) | |
252b5132 | 103 | { |
dc810e39 AM |
104 | dst->vda_name = H_GET_32 (abfd, src->vda_name); |
105 | dst->vda_next = H_GET_32 (abfd, src->vda_next); | |
252b5132 RH |
106 | } |
107 | ||
108 | /* Swap out a Verdaux structure. */ | |
109 | ||
110 | void | |
217aa764 AM |
111 | _bfd_elf_swap_verdaux_out (bfd *abfd, |
112 | const Elf_Internal_Verdaux *src, | |
113 | Elf_External_Verdaux *dst) | |
252b5132 | 114 | { |
dc810e39 AM |
115 | H_PUT_32 (abfd, src->vda_name, dst->vda_name); |
116 | H_PUT_32 (abfd, src->vda_next, dst->vda_next); | |
252b5132 RH |
117 | } |
118 | ||
119 | /* Swap in a Verneed structure. */ | |
120 | ||
121 | void | |
217aa764 AM |
122 | _bfd_elf_swap_verneed_in (bfd *abfd, |
123 | const Elf_External_Verneed *src, | |
124 | Elf_Internal_Verneed *dst) | |
252b5132 | 125 | { |
dc810e39 AM |
126 | dst->vn_version = H_GET_16 (abfd, src->vn_version); |
127 | dst->vn_cnt = H_GET_16 (abfd, src->vn_cnt); | |
128 | dst->vn_file = H_GET_32 (abfd, src->vn_file); | |
129 | dst->vn_aux = H_GET_32 (abfd, src->vn_aux); | |
130 | dst->vn_next = H_GET_32 (abfd, src->vn_next); | |
252b5132 RH |
131 | } |
132 | ||
133 | /* Swap out a Verneed structure. */ | |
134 | ||
135 | void | |
217aa764 AM |
136 | _bfd_elf_swap_verneed_out (bfd *abfd, |
137 | const Elf_Internal_Verneed *src, | |
138 | Elf_External_Verneed *dst) | |
252b5132 | 139 | { |
dc810e39 AM |
140 | H_PUT_16 (abfd, src->vn_version, dst->vn_version); |
141 | H_PUT_16 (abfd, src->vn_cnt, dst->vn_cnt); | |
142 | H_PUT_32 (abfd, src->vn_file, dst->vn_file); | |
143 | H_PUT_32 (abfd, src->vn_aux, dst->vn_aux); | |
144 | H_PUT_32 (abfd, src->vn_next, dst->vn_next); | |
252b5132 RH |
145 | } |
146 | ||
147 | /* Swap in a Vernaux structure. */ | |
148 | ||
149 | void | |
217aa764 AM |
150 | _bfd_elf_swap_vernaux_in (bfd *abfd, |
151 | const Elf_External_Vernaux *src, | |
152 | Elf_Internal_Vernaux *dst) | |
252b5132 | 153 | { |
dc810e39 AM |
154 | dst->vna_hash = H_GET_32 (abfd, src->vna_hash); |
155 | dst->vna_flags = H_GET_16 (abfd, src->vna_flags); | |
156 | dst->vna_other = H_GET_16 (abfd, src->vna_other); | |
157 | dst->vna_name = H_GET_32 (abfd, src->vna_name); | |
158 | dst->vna_next = H_GET_32 (abfd, src->vna_next); | |
252b5132 RH |
159 | } |
160 | ||
161 | /* Swap out a Vernaux structure. */ | |
162 | ||
163 | void | |
217aa764 AM |
164 | _bfd_elf_swap_vernaux_out (bfd *abfd, |
165 | const Elf_Internal_Vernaux *src, | |
166 | Elf_External_Vernaux *dst) | |
252b5132 | 167 | { |
dc810e39 AM |
168 | H_PUT_32 (abfd, src->vna_hash, dst->vna_hash); |
169 | H_PUT_16 (abfd, src->vna_flags, dst->vna_flags); | |
170 | H_PUT_16 (abfd, src->vna_other, dst->vna_other); | |
171 | H_PUT_32 (abfd, src->vna_name, dst->vna_name); | |
172 | H_PUT_32 (abfd, src->vna_next, dst->vna_next); | |
252b5132 RH |
173 | } |
174 | ||
175 | /* Swap in a Versym structure. */ | |
176 | ||
177 | void | |
217aa764 AM |
178 | _bfd_elf_swap_versym_in (bfd *abfd, |
179 | const Elf_External_Versym *src, | |
180 | Elf_Internal_Versym *dst) | |
252b5132 | 181 | { |
dc810e39 | 182 | dst->vs_vers = H_GET_16 (abfd, src->vs_vers); |
252b5132 RH |
183 | } |
184 | ||
185 | /* Swap out a Versym structure. */ | |
186 | ||
187 | void | |
217aa764 AM |
188 | _bfd_elf_swap_versym_out (bfd *abfd, |
189 | const Elf_Internal_Versym *src, | |
190 | Elf_External_Versym *dst) | |
252b5132 | 191 | { |
dc810e39 | 192 | H_PUT_16 (abfd, src->vs_vers, dst->vs_vers); |
252b5132 RH |
193 | } |
194 | ||
195 | /* Standard ELF hash function. Do not change this function; you will | |
196 | cause invalid hash tables to be generated. */ | |
3a99b017 | 197 | |
252b5132 | 198 | unsigned long |
217aa764 | 199 | bfd_elf_hash (const char *namearg) |
252b5132 | 200 | { |
3a99b017 | 201 | const unsigned char *name = (const unsigned char *) namearg; |
252b5132 RH |
202 | unsigned long h = 0; |
203 | unsigned long g; | |
204 | int ch; | |
205 | ||
206 | while ((ch = *name++) != '\0') | |
207 | { | |
208 | h = (h << 4) + ch; | |
209 | if ((g = (h & 0xf0000000)) != 0) | |
210 | { | |
211 | h ^= g >> 24; | |
212 | /* The ELF ABI says `h &= ~g', but this is equivalent in | |
213 | this case and on some machines one insn instead of two. */ | |
214 | h ^= g; | |
215 | } | |
216 | } | |
32dfa85d | 217 | return h & 0xffffffff; |
252b5132 RH |
218 | } |
219 | ||
fdc90cb4 JJ |
220 | /* DT_GNU_HASH hash function. Do not change this function; you will |
221 | cause invalid hash tables to be generated. */ | |
222 | ||
223 | unsigned long | |
224 | bfd_elf_gnu_hash (const char *namearg) | |
225 | { | |
226 | const unsigned char *name = (const unsigned char *) namearg; | |
227 | unsigned long h = 5381; | |
228 | unsigned char ch; | |
229 | ||
230 | while ((ch = *name++) != '\0') | |
231 | h = (h << 5) + h + ch; | |
232 | return h & 0xffffffff; | |
233 | } | |
234 | ||
0c8d6e5c AM |
235 | /* Create a tdata field OBJECT_SIZE bytes in length, zeroed out and with |
236 | the object_id field of an elf_obj_tdata field set to OBJECT_ID. */ | |
b34976b6 | 237 | bfd_boolean |
0c8d6e5c | 238 | bfd_elf_allocate_object (bfd *abfd, |
0ffa91dd | 239 | size_t object_size, |
4dfe6ac6 | 240 | enum elf_target_id object_id) |
252b5132 | 241 | { |
0ffa91dd NC |
242 | BFD_ASSERT (object_size >= sizeof (struct elf_obj_tdata)); |
243 | abfd->tdata.any = bfd_zalloc (abfd, object_size); | |
244 | if (abfd->tdata.any == NULL) | |
245 | return FALSE; | |
252b5132 | 246 | |
0ffa91dd | 247 | elf_object_id (abfd) = object_id; |
c0355132 AM |
248 | if (abfd->direction != read_direction) |
249 | { | |
250 | struct output_elf_obj_tdata *o = bfd_zalloc (abfd, sizeof *o); | |
251 | if (o == NULL) | |
252 | return FALSE; | |
253 | elf_tdata (abfd)->o = o; | |
254 | elf_program_header_size (abfd) = (bfd_size_type) -1; | |
255 | } | |
b34976b6 | 256 | return TRUE; |
252b5132 RH |
257 | } |
258 | ||
0ffa91dd NC |
259 | |
260 | bfd_boolean | |
ae95ffa6 | 261 | bfd_elf_make_object (bfd *abfd) |
0ffa91dd | 262 | { |
ae95ffa6 | 263 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
0ffa91dd | 264 | return bfd_elf_allocate_object (abfd, sizeof (struct elf_obj_tdata), |
ae95ffa6 | 265 | bed->target_id); |
0ffa91dd NC |
266 | } |
267 | ||
b34976b6 | 268 | bfd_boolean |
217aa764 | 269 | bfd_elf_mkcorefile (bfd *abfd) |
252b5132 | 270 | { |
c044fabd | 271 | /* I think this can be done just like an object file. */ |
228e534f AM |
272 | if (!abfd->xvec->_bfd_set_format[(int) bfd_object] (abfd)) |
273 | return FALSE; | |
274 | elf_tdata (abfd)->core = bfd_zalloc (abfd, sizeof (*elf_tdata (abfd)->core)); | |
275 | return elf_tdata (abfd)->core != NULL; | |
252b5132 RH |
276 | } |
277 | ||
72a80a16 | 278 | static char * |
217aa764 | 279 | bfd_elf_get_str_section (bfd *abfd, unsigned int shindex) |
252b5132 RH |
280 | { |
281 | Elf_Internal_Shdr **i_shdrp; | |
f075ee0c | 282 | bfd_byte *shstrtab = NULL; |
dc810e39 AM |
283 | file_ptr offset; |
284 | bfd_size_type shstrtabsize; | |
252b5132 RH |
285 | |
286 | i_shdrp = elf_elfsections (abfd); | |
74f2e02b AM |
287 | if (i_shdrp == 0 |
288 | || shindex >= elf_numsections (abfd) | |
289 | || i_shdrp[shindex] == 0) | |
f075ee0c | 290 | return NULL; |
252b5132 | 291 | |
f075ee0c | 292 | shstrtab = i_shdrp[shindex]->contents; |
252b5132 RH |
293 | if (shstrtab == NULL) |
294 | { | |
c044fabd | 295 | /* No cached one, attempt to read, and cache what we read. */ |
252b5132 RH |
296 | offset = i_shdrp[shindex]->sh_offset; |
297 | shstrtabsize = i_shdrp[shindex]->sh_size; | |
c6c60d09 JJ |
298 | |
299 | /* Allocate and clear an extra byte at the end, to prevent crashes | |
300 | in case the string table is not terminated. */ | |
3471d59d | 301 | if (shstrtabsize + 1 <= 1 |
95a6d235 | 302 | || shstrtabsize > bfd_get_file_size (abfd) |
06614111 NC |
303 | || bfd_seek (abfd, offset, SEEK_SET) != 0 |
304 | || (shstrtab = (bfd_byte *) bfd_alloc (abfd, shstrtabsize + 1)) == NULL) | |
c6c60d09 JJ |
305 | shstrtab = NULL; |
306 | else if (bfd_bread (shstrtab, shstrtabsize, abfd) != shstrtabsize) | |
307 | { | |
308 | if (bfd_get_error () != bfd_error_system_call) | |
309 | bfd_set_error (bfd_error_file_truncated); | |
06614111 | 310 | bfd_release (abfd, shstrtab); |
c6c60d09 | 311 | shstrtab = NULL; |
3471d59d CC |
312 | /* Once we've failed to read it, make sure we don't keep |
313 | trying. Otherwise, we'll keep allocating space for | |
314 | the string table over and over. */ | |
315 | i_shdrp[shindex]->sh_size = 0; | |
c6c60d09 JJ |
316 | } |
317 | else | |
318 | shstrtab[shstrtabsize] = '\0'; | |
217aa764 | 319 | i_shdrp[shindex]->contents = shstrtab; |
252b5132 | 320 | } |
f075ee0c | 321 | return (char *) shstrtab; |
252b5132 RH |
322 | } |
323 | ||
324 | char * | |
217aa764 AM |
325 | bfd_elf_string_from_elf_section (bfd *abfd, |
326 | unsigned int shindex, | |
327 | unsigned int strindex) | |
252b5132 RH |
328 | { |
329 | Elf_Internal_Shdr *hdr; | |
330 | ||
331 | if (strindex == 0) | |
332 | return ""; | |
333 | ||
74f2e02b AM |
334 | if (elf_elfsections (abfd) == NULL || shindex >= elf_numsections (abfd)) |
335 | return NULL; | |
336 | ||
252b5132 RH |
337 | hdr = elf_elfsections (abfd)[shindex]; |
338 | ||
06614111 NC |
339 | if (hdr->contents == NULL) |
340 | { | |
341 | if (hdr->sh_type != SHT_STRTAB && hdr->sh_type < SHT_LOOS) | |
342 | { | |
343 | /* PR 17512: file: f057ec89. */ | |
695344c0 | 344 | /* xgettext:c-format */ |
871b3ab2 | 345 | _bfd_error_handler (_("%pB: attempt to load strings from" |
63a5468a | 346 | " a non-string section (number %d)"), |
06614111 NC |
347 | abfd, shindex); |
348 | return NULL; | |
349 | } | |
b1fa9dd6 | 350 | |
06614111 NC |
351 | if (bfd_elf_get_str_section (abfd, shindex) == NULL) |
352 | return NULL; | |
353 | } | |
eed5def8 NC |
354 | else |
355 | { | |
356 | /* PR 24273: The string section's contents may have already | |
357 | been loaded elsewhere, eg because a corrupt file has the | |
358 | string section index in the ELF header pointing at a group | |
359 | section. So be paranoid, and test that the last byte of | |
360 | the section is zero. */ | |
361 | if (hdr->sh_size == 0 || hdr->contents[hdr->sh_size - 1] != 0) | |
362 | return NULL; | |
363 | } | |
252b5132 RH |
364 | |
365 | if (strindex >= hdr->sh_size) | |
366 | { | |
1b3a8575 | 367 | unsigned int shstrndx = elf_elfheader(abfd)->e_shstrndx; |
4eca0228 | 368 | _bfd_error_handler |
695344c0 | 369 | /* xgettext:c-format */ |
2dcf00ce AM |
370 | (_("%pB: invalid string offset %u >= %" PRIu64 " for section `%s'"), |
371 | abfd, strindex, (uint64_t) hdr->sh_size, | |
1b3a8575 | 372 | (shindex == shstrndx && strindex == hdr->sh_name |
252b5132 | 373 | ? ".shstrtab" |
1b3a8575 | 374 | : bfd_elf_string_from_elf_section (abfd, shstrndx, hdr->sh_name))); |
45b222d6 | 375 | return NULL; |
252b5132 RH |
376 | } |
377 | ||
378 | return ((char *) hdr->contents) + strindex; | |
379 | } | |
380 | ||
6cdc0ccc AM |
381 | /* Read and convert symbols to internal format. |
382 | SYMCOUNT specifies the number of symbols to read, starting from | |
383 | symbol SYMOFFSET. If any of INTSYM_BUF, EXTSYM_BUF or EXTSHNDX_BUF | |
384 | are non-NULL, they are used to store the internal symbols, external | |
b7c368d0 NC |
385 | symbols, and symbol section index extensions, respectively. |
386 | Returns a pointer to the internal symbol buffer (malloced if necessary) | |
387 | or NULL if there were no symbols or some kind of problem. */ | |
6cdc0ccc AM |
388 | |
389 | Elf_Internal_Sym * | |
217aa764 AM |
390 | bfd_elf_get_elf_syms (bfd *ibfd, |
391 | Elf_Internal_Shdr *symtab_hdr, | |
392 | size_t symcount, | |
393 | size_t symoffset, | |
394 | Elf_Internal_Sym *intsym_buf, | |
395 | void *extsym_buf, | |
396 | Elf_External_Sym_Shndx *extshndx_buf) | |
6cdc0ccc AM |
397 | { |
398 | Elf_Internal_Shdr *shndx_hdr; | |
217aa764 | 399 | void *alloc_ext; |
df622259 | 400 | const bfd_byte *esym; |
6cdc0ccc AM |
401 | Elf_External_Sym_Shndx *alloc_extshndx; |
402 | Elf_External_Sym_Shndx *shndx; | |
4dd07732 | 403 | Elf_Internal_Sym *alloc_intsym; |
6cdc0ccc AM |
404 | Elf_Internal_Sym *isym; |
405 | Elf_Internal_Sym *isymend; | |
9c5bfbb7 | 406 | const struct elf_backend_data *bed; |
6cdc0ccc AM |
407 | size_t extsym_size; |
408 | bfd_size_type amt; | |
409 | file_ptr pos; | |
410 | ||
e44a2c9c AM |
411 | if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour) |
412 | abort (); | |
413 | ||
6cdc0ccc AM |
414 | if (symcount == 0) |
415 | return intsym_buf; | |
416 | ||
417 | /* Normal syms might have section extension entries. */ | |
418 | shndx_hdr = NULL; | |
6a40cf0c NC |
419 | if (elf_symtab_shndx_list (ibfd) != NULL) |
420 | { | |
421 | elf_section_list * entry; | |
422 | Elf_Internal_Shdr **sections = elf_elfsections (ibfd); | |
423 | ||
424 | /* Find an index section that is linked to this symtab section. */ | |
425 | for (entry = elf_symtab_shndx_list (ibfd); entry != NULL; entry = entry->next) | |
315350be NC |
426 | { |
427 | /* PR 20063. */ | |
428 | if (entry->hdr.sh_link >= elf_numsections (ibfd)) | |
429 | continue; | |
430 | ||
431 | if (sections[entry->hdr.sh_link] == symtab_hdr) | |
432 | { | |
433 | shndx_hdr = & entry->hdr; | |
434 | break; | |
435 | }; | |
436 | } | |
6a40cf0c NC |
437 | |
438 | if (shndx_hdr == NULL) | |
439 | { | |
440 | if (symtab_hdr == & elf_symtab_hdr (ibfd)) | |
441 | /* Not really accurate, but this was how the old code used to work. */ | |
442 | shndx_hdr = & elf_symtab_shndx_list (ibfd)->hdr; | |
443 | /* Otherwise we do nothing. The assumption is that | |
444 | the index table will not be needed. */ | |
445 | } | |
446 | } | |
6cdc0ccc AM |
447 | |
448 | /* Read the symbols. */ | |
449 | alloc_ext = NULL; | |
450 | alloc_extshndx = NULL; | |
4dd07732 | 451 | alloc_intsym = NULL; |
6cdc0ccc AM |
452 | bed = get_elf_backend_data (ibfd); |
453 | extsym_size = bed->s->sizeof_sym; | |
ef53be89 | 454 | amt = (bfd_size_type) symcount * extsym_size; |
6cdc0ccc AM |
455 | pos = symtab_hdr->sh_offset + symoffset * extsym_size; |
456 | if (extsym_buf == NULL) | |
457 | { | |
d0fb9a8d | 458 | alloc_ext = bfd_malloc2 (symcount, extsym_size); |
6cdc0ccc AM |
459 | extsym_buf = alloc_ext; |
460 | } | |
461 | if (extsym_buf == NULL | |
462 | || bfd_seek (ibfd, pos, SEEK_SET) != 0 | |
463 | || bfd_bread (extsym_buf, amt, ibfd) != amt) | |
464 | { | |
465 | intsym_buf = NULL; | |
466 | goto out; | |
467 | } | |
468 | ||
469 | if (shndx_hdr == NULL || shndx_hdr->sh_size == 0) | |
470 | extshndx_buf = NULL; | |
471 | else | |
472 | { | |
ef53be89 | 473 | amt = (bfd_size_type) symcount * sizeof (Elf_External_Sym_Shndx); |
6cdc0ccc AM |
474 | pos = shndx_hdr->sh_offset + symoffset * sizeof (Elf_External_Sym_Shndx); |
475 | if (extshndx_buf == NULL) | |
476 | { | |
a50b1753 | 477 | alloc_extshndx = (Elf_External_Sym_Shndx *) |
07d6d2b8 | 478 | bfd_malloc2 (symcount, sizeof (Elf_External_Sym_Shndx)); |
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 | { | |
a50b1753 | 492 | alloc_intsym = (Elf_Internal_Sym *) |
07d6d2b8 | 493 | bfd_malloc2 (symcount, sizeof (Elf_Internal_Sym)); |
4dd07732 | 494 | intsym_buf = alloc_intsym; |
6cdc0ccc AM |
495 | if (intsym_buf == NULL) |
496 | goto out; | |
497 | } | |
498 | ||
499 | /* Convert the symbols to internal form. */ | |
500 | isymend = intsym_buf + symcount; | |
a50b1753 | 501 | for (esym = (const bfd_byte *) extsym_buf, isym = intsym_buf, |
07d6d2b8 | 502 | shndx = extshndx_buf; |
6cdc0ccc AM |
503 | isym < isymend; |
504 | esym += extsym_size, isym++, shndx = shndx != NULL ? shndx + 1 : NULL) | |
8384fb8f AM |
505 | if (!(*bed->s->swap_symbol_in) (ibfd, esym, shndx, isym)) |
506 | { | |
507 | symoffset += (esym - (bfd_byte *) extsym_buf) / extsym_size; | |
695344c0 | 508 | /* xgettext:c-format */ |
871b3ab2 | 509 | _bfd_error_handler (_("%pB symbol number %lu references" |
63a5468a | 510 | " nonexistent SHT_SYMTAB_SHNDX section"), |
4eca0228 | 511 | ibfd, (unsigned long) symoffset); |
4dd07732 AM |
512 | if (alloc_intsym != NULL) |
513 | free (alloc_intsym); | |
8384fb8f AM |
514 | intsym_buf = NULL; |
515 | goto out; | |
516 | } | |
6cdc0ccc AM |
517 | |
518 | out: | |
519 | if (alloc_ext != NULL) | |
520 | free (alloc_ext); | |
521 | if (alloc_extshndx != NULL) | |
522 | free (alloc_extshndx); | |
523 | ||
524 | return intsym_buf; | |
525 | } | |
526 | ||
5cab59f6 AM |
527 | /* Look up a symbol name. */ |
528 | const char * | |
be8dd2ca AM |
529 | bfd_elf_sym_name (bfd *abfd, |
530 | Elf_Internal_Shdr *symtab_hdr, | |
26c61ae5 L |
531 | Elf_Internal_Sym *isym, |
532 | asection *sym_sec) | |
5cab59f6 | 533 | { |
26c61ae5 | 534 | const char *name; |
5cab59f6 | 535 | unsigned int iname = isym->st_name; |
be8dd2ca | 536 | unsigned int shindex = symtab_hdr->sh_link; |
26c61ae5 | 537 | |
138f35cc JJ |
538 | if (iname == 0 && ELF_ST_TYPE (isym->st_info) == STT_SECTION |
539 | /* Check for a bogus st_shndx to avoid crashing. */ | |
4fbb74a6 | 540 | && isym->st_shndx < elf_numsections (abfd)) |
5cab59f6 AM |
541 | { |
542 | iname = elf_elfsections (abfd)[isym->st_shndx]->sh_name; | |
543 | shindex = elf_elfheader (abfd)->e_shstrndx; | |
544 | } | |
545 | ||
26c61ae5 L |
546 | name = bfd_elf_string_from_elf_section (abfd, shindex, iname); |
547 | if (name == NULL) | |
548 | name = "(null)"; | |
549 | else if (sym_sec && *name == '\0') | |
fd361982 | 550 | name = bfd_section_name (sym_sec); |
26c61ae5 L |
551 | |
552 | return name; | |
5cab59f6 AM |
553 | } |
554 | ||
dbb410c3 AM |
555 | /* Elf_Internal_Shdr->contents is an array of these for SHT_GROUP |
556 | sections. The first element is the flags, the rest are section | |
557 | pointers. */ | |
558 | ||
559 | typedef union elf_internal_group { | |
560 | Elf_Internal_Shdr *shdr; | |
561 | unsigned int flags; | |
562 | } Elf_Internal_Group; | |
563 | ||
b885599b AM |
564 | /* Return the name of the group signature symbol. Why isn't the |
565 | signature just a string? */ | |
566 | ||
567 | static const char * | |
217aa764 | 568 | group_signature (bfd *abfd, Elf_Internal_Shdr *ghdr) |
b885599b | 569 | { |
9dce4196 | 570 | Elf_Internal_Shdr *hdr; |
9dce4196 AM |
571 | unsigned char esym[sizeof (Elf64_External_Sym)]; |
572 | Elf_External_Sym_Shndx eshndx; | |
573 | Elf_Internal_Sym isym; | |
b885599b | 574 | |
13792e9d L |
575 | /* First we need to ensure the symbol table is available. Make sure |
576 | that it is a symbol table section. */ | |
4fbb74a6 AM |
577 | if (ghdr->sh_link >= elf_numsections (abfd)) |
578 | return NULL; | |
13792e9d L |
579 | hdr = elf_elfsections (abfd) [ghdr->sh_link]; |
580 | if (hdr->sh_type != SHT_SYMTAB | |
581 | || ! bfd_section_from_shdr (abfd, ghdr->sh_link)) | |
b885599b AM |
582 | return NULL; |
583 | ||
9dce4196 AM |
584 | /* Go read the symbol. */ |
585 | hdr = &elf_tdata (abfd)->symtab_hdr; | |
6cdc0ccc AM |
586 | if (bfd_elf_get_elf_syms (abfd, hdr, 1, ghdr->sh_info, |
587 | &isym, esym, &eshndx) == NULL) | |
b885599b | 588 | return NULL; |
9dce4196 | 589 | |
26c61ae5 | 590 | return bfd_elf_sym_name (abfd, hdr, &isym, NULL); |
b885599b AM |
591 | } |
592 | ||
dbb410c3 AM |
593 | /* Set next_in_group list pointer, and group name for NEWSECT. */ |
594 | ||
b34976b6 | 595 | static bfd_boolean |
217aa764 | 596 | setup_group (bfd *abfd, Elf_Internal_Shdr *hdr, asection *newsect) |
dbb410c3 AM |
597 | { |
598 | unsigned int num_group = elf_tdata (abfd)->num_group; | |
599 | ||
600 | /* If num_group is zero, read in all SHT_GROUP sections. The count | |
601 | is set to -1 if there are no SHT_GROUP sections. */ | |
602 | if (num_group == 0) | |
603 | { | |
604 | unsigned int i, shnum; | |
605 | ||
606 | /* First count the number of groups. If we have a SHT_GROUP | |
607 | section with just a flag word (ie. sh_size is 4), ignore it. */ | |
9ad5cbcf | 608 | shnum = elf_numsections (abfd); |
dbb410c3 | 609 | num_group = 0; |
08a40648 | 610 | |
44534af3 | 611 | #define IS_VALID_GROUP_SECTION_HEADER(shdr, minsize) \ |
1783205a | 612 | ( (shdr)->sh_type == SHT_GROUP \ |
44534af3 | 613 | && (shdr)->sh_size >= minsize \ |
1783205a NC |
614 | && (shdr)->sh_entsize == GRP_ENTRY_SIZE \ |
615 | && ((shdr)->sh_size % GRP_ENTRY_SIZE) == 0) | |
08a40648 | 616 | |
dbb410c3 AM |
617 | for (i = 0; i < shnum; i++) |
618 | { | |
619 | Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i]; | |
1783205a | 620 | |
44534af3 | 621 | if (IS_VALID_GROUP_SECTION_HEADER (shdr, 2 * GRP_ENTRY_SIZE)) |
dbb410c3 AM |
622 | num_group += 1; |
623 | } | |
624 | ||
625 | if (num_group == 0) | |
20dbb49d L |
626 | { |
627 | num_group = (unsigned) -1; | |
628 | elf_tdata (abfd)->num_group = num_group; | |
ce497010 | 629 | elf_tdata (abfd)->group_sect_ptr = NULL; |
20dbb49d L |
630 | } |
631 | else | |
dbb410c3 AM |
632 | { |
633 | /* We keep a list of elf section headers for group sections, | |
634 | so we can find them quickly. */ | |
20dbb49d | 635 | bfd_size_type amt; |
d0fb9a8d | 636 | |
20dbb49d | 637 | elf_tdata (abfd)->num_group = num_group; |
a50b1753 | 638 | elf_tdata (abfd)->group_sect_ptr = (Elf_Internal_Shdr **) |
07d6d2b8 | 639 | bfd_alloc2 (abfd, num_group, sizeof (Elf_Internal_Shdr *)); |
dbb410c3 | 640 | if (elf_tdata (abfd)->group_sect_ptr == NULL) |
b34976b6 | 641 | return FALSE; |
4bba0fb1 AM |
642 | memset (elf_tdata (abfd)->group_sect_ptr, 0, |
643 | num_group * sizeof (Elf_Internal_Shdr *)); | |
dbb410c3 | 644 | num_group = 0; |
ce497010 | 645 | |
dbb410c3 AM |
646 | for (i = 0; i < shnum; i++) |
647 | { | |
648 | Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i]; | |
1783205a | 649 | |
44534af3 | 650 | if (IS_VALID_GROUP_SECTION_HEADER (shdr, 2 * GRP_ENTRY_SIZE)) |
dbb410c3 | 651 | { |
973ffd63 | 652 | unsigned char *src; |
dbb410c3 AM |
653 | Elf_Internal_Group *dest; |
654 | ||
07d6d2b8 AM |
655 | /* Make sure the group section has a BFD section |
656 | attached to it. */ | |
657 | if (!bfd_section_from_shdr (abfd, i)) | |
658 | return FALSE; | |
659 | ||
dbb410c3 AM |
660 | /* Add to list of sections. */ |
661 | elf_tdata (abfd)->group_sect_ptr[num_group] = shdr; | |
662 | num_group += 1; | |
663 | ||
664 | /* Read the raw contents. */ | |
665 | BFD_ASSERT (sizeof (*dest) >= 4); | |
666 | amt = shdr->sh_size * sizeof (*dest) / 4; | |
a50b1753 | 667 | shdr->contents = (unsigned char *) |
eed5def8 | 668 | bfd_alloc2 (abfd, shdr->sh_size, sizeof (*dest) / 4); |
1783205a NC |
669 | /* PR binutils/4110: Handle corrupt group headers. */ |
670 | if (shdr->contents == NULL) | |
671 | { | |
672 | _bfd_error_handler | |
695344c0 | 673 | /* xgettext:c-format */ |
871b3ab2 | 674 | (_("%pB: corrupt size field in group section" |
2dcf00ce AM |
675 | " header: %#" PRIx64), |
676 | abfd, (uint64_t) shdr->sh_size); | |
1783205a | 677 | bfd_set_error (bfd_error_bad_value); |
493a3386 NC |
678 | -- num_group; |
679 | continue; | |
1783205a NC |
680 | } |
681 | ||
682 | memset (shdr->contents, 0, amt); | |
683 | ||
684 | if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0 | |
dbb410c3 AM |
685 | || (bfd_bread (shdr->contents, shdr->sh_size, abfd) |
686 | != shdr->sh_size)) | |
493a3386 NC |
687 | { |
688 | _bfd_error_handler | |
695344c0 | 689 | /* xgettext:c-format */ |
871b3ab2 | 690 | (_("%pB: invalid size field in group section" |
2dcf00ce AM |
691 | " header: %#" PRIx64 ""), |
692 | abfd, (uint64_t) shdr->sh_size); | |
493a3386 NC |
693 | bfd_set_error (bfd_error_bad_value); |
694 | -- num_group; | |
63a5468a AM |
695 | /* PR 17510: If the group contents are even |
696 | partially corrupt, do not allow any of the | |
697 | contents to be used. */ | |
493a3386 NC |
698 | memset (shdr->contents, 0, amt); |
699 | continue; | |
700 | } | |
708d7d0d | 701 | |
dbb410c3 AM |
702 | /* Translate raw contents, a flag word followed by an |
703 | array of elf section indices all in target byte order, | |
704 | to the flag word followed by an array of elf section | |
705 | pointers. */ | |
706 | src = shdr->contents + shdr->sh_size; | |
707 | dest = (Elf_Internal_Group *) (shdr->contents + amt); | |
06614111 | 708 | |
dbb410c3 AM |
709 | while (1) |
710 | { | |
711 | unsigned int idx; | |
712 | ||
713 | src -= 4; | |
714 | --dest; | |
715 | idx = H_GET_32 (abfd, src); | |
716 | if (src == shdr->contents) | |
717 | { | |
718 | dest->flags = idx; | |
b885599b AM |
719 | if (shdr->bfd_section != NULL && (idx & GRP_COMDAT)) |
720 | shdr->bfd_section->flags | |
721 | |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD; | |
dbb410c3 AM |
722 | break; |
723 | } | |
4bba0fb1 | 724 | if (idx < shnum) |
bae363f1 L |
725 | { |
726 | dest->shdr = elf_elfsections (abfd)[idx]; | |
727 | /* PR binutils/23199: All sections in a | |
728 | section group should be marked with | |
729 | SHF_GROUP. But some tools generate | |
730 | broken objects without SHF_GROUP. Fix | |
731 | them up here. */ | |
732 | dest->shdr->sh_flags |= SHF_GROUP; | |
733 | } | |
4bba0fb1 AM |
734 | if (idx >= shnum |
735 | || dest->shdr->sh_type == SHT_GROUP) | |
dbb410c3 | 736 | { |
4eca0228 | 737 | _bfd_error_handler |
4bba0fb1 AM |
738 | (_("%pB: invalid entry in SHT_GROUP section [%u]"), |
739 | abfd, i); | |
740 | dest->shdr = NULL; | |
dbb410c3 | 741 | } |
dbb410c3 AM |
742 | } |
743 | } | |
744 | } | |
493a3386 NC |
745 | |
746 | /* PR 17510: Corrupt binaries might contain invalid groups. */ | |
747 | if (num_group != (unsigned) elf_tdata (abfd)->num_group) | |
748 | { | |
749 | elf_tdata (abfd)->num_group = num_group; | |
750 | ||
751 | /* If all groups are invalid then fail. */ | |
752 | if (num_group == 0) | |
753 | { | |
754 | elf_tdata (abfd)->group_sect_ptr = NULL; | |
755 | elf_tdata (abfd)->num_group = num_group = -1; | |
4eca0228 | 756 | _bfd_error_handler |
871b3ab2 | 757 | (_("%pB: no valid group sections found"), abfd); |
493a3386 NC |
758 | bfd_set_error (bfd_error_bad_value); |
759 | } | |
760 | } | |
dbb410c3 AM |
761 | } |
762 | } | |
763 | ||
764 | if (num_group != (unsigned) -1) | |
765 | { | |
564e11c9 JW |
766 | unsigned int search_offset = elf_tdata (abfd)->group_search_offset; |
767 | unsigned int j; | |
dbb410c3 | 768 | |
564e11c9 | 769 | for (j = 0; j < num_group; j++) |
dbb410c3 | 770 | { |
564e11c9 JW |
771 | /* Begin search from previous found group. */ |
772 | unsigned i = (j + search_offset) % num_group; | |
773 | ||
dbb410c3 | 774 | Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i]; |
ce497010 | 775 | Elf_Internal_Group *idx; |
0c54f692 | 776 | bfd_size_type n_elt; |
ce497010 NC |
777 | |
778 | if (shdr == NULL) | |
779 | continue; | |
780 | ||
781 | idx = (Elf_Internal_Group *) shdr->contents; | |
0c54f692 NC |
782 | if (idx == NULL || shdr->sh_size < 4) |
783 | { | |
784 | /* See PR 21957 for a reproducer. */ | |
785 | /* xgettext:c-format */ | |
871b3ab2 | 786 | _bfd_error_handler (_("%pB: group section '%pA' has no contents"), |
0c54f692 NC |
787 | abfd, shdr->bfd_section); |
788 | elf_tdata (abfd)->group_sect_ptr[i] = NULL; | |
789 | bfd_set_error (bfd_error_bad_value); | |
790 | return FALSE; | |
791 | } | |
ce497010 | 792 | n_elt = shdr->sh_size / 4; |
dbb410c3 AM |
793 | |
794 | /* Look through this group's sections to see if current | |
795 | section is a member. */ | |
796 | while (--n_elt != 0) | |
797 | if ((++idx)->shdr == hdr) | |
798 | { | |
e0e8c97f | 799 | asection *s = NULL; |
dbb410c3 AM |
800 | |
801 | /* We are a member of this group. Go looking through | |
802 | other members to see if any others are linked via | |
803 | next_in_group. */ | |
804 | idx = (Elf_Internal_Group *) shdr->contents; | |
805 | n_elt = shdr->sh_size / 4; | |
806 | while (--n_elt != 0) | |
4bba0fb1 AM |
807 | if ((++idx)->shdr != NULL |
808 | && (s = idx->shdr->bfd_section) != NULL | |
945906ff | 809 | && elf_next_in_group (s) != NULL) |
dbb410c3 AM |
810 | break; |
811 | if (n_elt != 0) | |
812 | { | |
dbb410c3 AM |
813 | /* Snarf the group name from other member, and |
814 | insert current section in circular list. */ | |
945906ff AM |
815 | elf_group_name (newsect) = elf_group_name (s); |
816 | elf_next_in_group (newsect) = elf_next_in_group (s); | |
817 | elf_next_in_group (s) = newsect; | |
dbb410c3 AM |
818 | } |
819 | else | |
820 | { | |
dbb410c3 AM |
821 | const char *gname; |
822 | ||
b885599b AM |
823 | gname = group_signature (abfd, shdr); |
824 | if (gname == NULL) | |
b34976b6 | 825 | return FALSE; |
945906ff | 826 | elf_group_name (newsect) = gname; |
dbb410c3 AM |
827 | |
828 | /* Start a circular list with one element. */ | |
945906ff | 829 | elf_next_in_group (newsect) = newsect; |
dbb410c3 | 830 | } |
b885599b | 831 | |
9dce4196 AM |
832 | /* If the group section has been created, point to the |
833 | new member. */ | |
dbb410c3 | 834 | if (shdr->bfd_section != NULL) |
945906ff | 835 | elf_next_in_group (shdr->bfd_section) = newsect; |
b885599b | 836 | |
564e11c9 JW |
837 | elf_tdata (abfd)->group_search_offset = i; |
838 | j = num_group - 1; | |
dbb410c3 AM |
839 | break; |
840 | } | |
841 | } | |
842 | } | |
843 | ||
945906ff | 844 | if (elf_group_name (newsect) == NULL) |
dbb410c3 | 845 | { |
695344c0 | 846 | /* xgettext:c-format */ |
871b3ab2 | 847 | _bfd_error_handler (_("%pB: no group info for section '%pA'"), |
4eca0228 | 848 | abfd, newsect); |
493a3386 | 849 | return FALSE; |
dbb410c3 | 850 | } |
b34976b6 | 851 | return TRUE; |
dbb410c3 AM |
852 | } |
853 | ||
3d7f7666 | 854 | bfd_boolean |
dd863624 | 855 | _bfd_elf_setup_sections (bfd *abfd) |
3d7f7666 L |
856 | { |
857 | unsigned int i; | |
858 | unsigned int num_group = elf_tdata (abfd)->num_group; | |
859 | bfd_boolean result = TRUE; | |
dd863624 L |
860 | asection *s; |
861 | ||
862 | /* Process SHF_LINK_ORDER. */ | |
863 | for (s = abfd->sections; s != NULL; s = s->next) | |
864 | { | |
865 | Elf_Internal_Shdr *this_hdr = &elf_section_data (s)->this_hdr; | |
866 | if ((this_hdr->sh_flags & SHF_LINK_ORDER) != 0) | |
867 | { | |
868 | unsigned int elfsec = this_hdr->sh_link; | |
869 | /* FIXME: The old Intel compiler and old strip/objcopy may | |
870 | not set the sh_link or sh_info fields. Hence we could | |
871 | get the situation where elfsec is 0. */ | |
872 | if (elfsec == 0) | |
873 | { | |
4fbb74a6 | 874 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
dd863624 L |
875 | if (bed->link_order_error_handler) |
876 | bed->link_order_error_handler | |
695344c0 | 877 | /* xgettext:c-format */ |
871b3ab2 | 878 | (_("%pB: warning: sh_link not set for section `%pA'"), |
dd863624 L |
879 | abfd, s); |
880 | } | |
881 | else | |
882 | { | |
91d6fa6a | 883 | asection *linksec = NULL; |
25bbc984 | 884 | |
4fbb74a6 AM |
885 | if (elfsec < elf_numsections (abfd)) |
886 | { | |
887 | this_hdr = elf_elfsections (abfd)[elfsec]; | |
91d6fa6a | 888 | linksec = this_hdr->bfd_section; |
4fbb74a6 | 889 | } |
25bbc984 L |
890 | |
891 | /* PR 1991, 2008: | |
892 | Some strip/objcopy may leave an incorrect value in | |
893 | sh_link. We don't want to proceed. */ | |
91d6fa6a | 894 | if (linksec == NULL) |
25bbc984 | 895 | { |
4eca0228 | 896 | _bfd_error_handler |
695344c0 | 897 | /* xgettext:c-format */ |
871b3ab2 | 898 | (_("%pB: sh_link [%d] in section `%pA' is incorrect"), |
c08bb8dd | 899 | s->owner, elfsec, s); |
25bbc984 L |
900 | result = FALSE; |
901 | } | |
902 | ||
91d6fa6a | 903 | elf_linked_to_section (s) = linksec; |
dd863624 L |
904 | } |
905 | } | |
53720c49 AM |
906 | else if (this_hdr->sh_type == SHT_GROUP |
907 | && elf_next_in_group (s) == NULL) | |
908 | { | |
4eca0228 | 909 | _bfd_error_handler |
695344c0 | 910 | /* xgettext:c-format */ |
871b3ab2 | 911 | (_("%pB: SHT_GROUP section [index %d] has no SHF_GROUP sections"), |
53720c49 AM |
912 | abfd, elf_section_data (s)->this_idx); |
913 | result = FALSE; | |
914 | } | |
dd863624 | 915 | } |
3d7f7666 | 916 | |
dd863624 | 917 | /* Process section groups. */ |
3d7f7666 L |
918 | if (num_group == (unsigned) -1) |
919 | return result; | |
920 | ||
921 | for (i = 0; i < num_group; i++) | |
922 | { | |
923 | Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i]; | |
4b0e8a5f NC |
924 | Elf_Internal_Group *idx; |
925 | unsigned int n_elt; | |
3d7f7666 | 926 | |
4b0e8a5f NC |
927 | /* PR binutils/18758: Beware of corrupt binaries with invalid group data. */ |
928 | if (shdr == NULL || shdr->bfd_section == NULL || shdr->contents == NULL) | |
929 | { | |
4eca0228 | 930 | _bfd_error_handler |
695344c0 | 931 | /* xgettext:c-format */ |
871b3ab2 | 932 | (_("%pB: section group entry number %u is corrupt"), |
4b0e8a5f NC |
933 | abfd, i); |
934 | result = FALSE; | |
935 | continue; | |
936 | } | |
937 | ||
938 | idx = (Elf_Internal_Group *) shdr->contents; | |
939 | n_elt = shdr->sh_size / 4; | |
1b786873 | 940 | |
3d7f7666 | 941 | while (--n_elt != 0) |
24d3e51b NC |
942 | { |
943 | ++ idx; | |
944 | ||
945 | if (idx->shdr == NULL) | |
946 | continue; | |
947 | else if (idx->shdr->bfd_section) | |
948 | elf_sec_group (idx->shdr->bfd_section) = shdr->bfd_section; | |
db4677b8 AM |
949 | else if (idx->shdr->sh_type != SHT_RELA |
950 | && idx->shdr->sh_type != SHT_REL) | |
24d3e51b NC |
951 | { |
952 | /* There are some unknown sections in the group. */ | |
953 | _bfd_error_handler | |
954 | /* xgettext:c-format */ | |
871b3ab2 | 955 | (_("%pB: unknown type [%#x] section `%s' in group [%pA]"), |
24d3e51b NC |
956 | abfd, |
957 | idx->shdr->sh_type, | |
958 | bfd_elf_string_from_elf_section (abfd, | |
959 | (elf_elfheader (abfd) | |
960 | ->e_shstrndx), | |
961 | idx->shdr->sh_name), | |
962 | shdr->bfd_section); | |
963 | result = FALSE; | |
964 | } | |
965 | } | |
3d7f7666 | 966 | } |
24d3e51b | 967 | |
3d7f7666 L |
968 | return result; |
969 | } | |
970 | ||
72adc230 AM |
971 | bfd_boolean |
972 | bfd_elf_is_group_section (bfd *abfd ATTRIBUTE_UNUSED, const asection *sec) | |
973 | { | |
974 | return elf_next_in_group (sec) != NULL; | |
975 | } | |
976 | ||
cb7f4b29 AM |
977 | const char * |
978 | bfd_elf_group_name (bfd *abfd ATTRIBUTE_UNUSED, const asection *sec) | |
979 | { | |
980 | if (elf_sec_group (sec) != NULL) | |
981 | return elf_group_name (sec); | |
982 | return NULL; | |
983 | } | |
984 | ||
f6fe1ccd L |
985 | static char * |
986 | convert_debug_to_zdebug (bfd *abfd, const char *name) | |
987 | { | |
988 | unsigned int len = strlen (name); | |
989 | char *new_name = bfd_alloc (abfd, len + 2); | |
990 | if (new_name == NULL) | |
991 | return NULL; | |
992 | new_name[0] = '.'; | |
993 | new_name[1] = 'z'; | |
994 | memcpy (new_name + 2, name + 1, len); | |
995 | return new_name; | |
996 | } | |
997 | ||
998 | static char * | |
999 | convert_zdebug_to_debug (bfd *abfd, const char *name) | |
1000 | { | |
1001 | unsigned int len = strlen (name); | |
1002 | char *new_name = bfd_alloc (abfd, len); | |
1003 | if (new_name == NULL) | |
1004 | return NULL; | |
1005 | new_name[0] = '.'; | |
1006 | memcpy (new_name + 1, name + 2, len - 1); | |
1007 | return new_name; | |
1008 | } | |
1009 | ||
cc5277b1 ML |
1010 | /* This a copy of lto_section defined in GCC (lto-streamer.h). */ |
1011 | ||
1012 | struct lto_section | |
1013 | { | |
1014 | int16_t major_version; | |
1015 | int16_t minor_version; | |
1016 | unsigned char slim_object; | |
1017 | ||
1018 | /* Flags is a private field that is not defined publicly. */ | |
1019 | uint16_t flags; | |
1020 | }; | |
1021 | ||
252b5132 RH |
1022 | /* Make a BFD section from an ELF section. We store a pointer to the |
1023 | BFD section in the bfd_section field of the header. */ | |
1024 | ||
b34976b6 | 1025 | bfd_boolean |
217aa764 AM |
1026 | _bfd_elf_make_section_from_shdr (bfd *abfd, |
1027 | Elf_Internal_Shdr *hdr, | |
6dc132d9 L |
1028 | const char *name, |
1029 | int shindex) | |
252b5132 RH |
1030 | { |
1031 | asection *newsect; | |
1032 | flagword flags; | |
9c5bfbb7 | 1033 | const struct elf_backend_data *bed; |
252b5132 RH |
1034 | |
1035 | if (hdr->bfd_section != NULL) | |
4e011fb5 | 1036 | return TRUE; |
252b5132 RH |
1037 | |
1038 | newsect = bfd_make_section_anyway (abfd, name); | |
1039 | if (newsect == NULL) | |
b34976b6 | 1040 | return FALSE; |
252b5132 | 1041 | |
1829f4b2 AM |
1042 | hdr->bfd_section = newsect; |
1043 | elf_section_data (newsect)->this_hdr = *hdr; | |
6dc132d9 | 1044 | elf_section_data (newsect)->this_idx = shindex; |
1829f4b2 | 1045 | |
2f89ff8d L |
1046 | /* Always use the real type/flags. */ |
1047 | elf_section_type (newsect) = hdr->sh_type; | |
1048 | elf_section_flags (newsect) = hdr->sh_flags; | |
1049 | ||
252b5132 RH |
1050 | newsect->filepos = hdr->sh_offset; |
1051 | ||
fd361982 AM |
1052 | if (!bfd_set_section_vma (newsect, hdr->sh_addr) |
1053 | || !bfd_set_section_size (newsect, hdr->sh_size) | |
1054 | || !bfd_set_section_alignment (newsect, bfd_log2 (hdr->sh_addralign))) | |
b34976b6 | 1055 | return FALSE; |
252b5132 RH |
1056 | |
1057 | flags = SEC_NO_FLAGS; | |
1058 | if (hdr->sh_type != SHT_NOBITS) | |
1059 | flags |= SEC_HAS_CONTENTS; | |
dbb410c3 | 1060 | if (hdr->sh_type == SHT_GROUP) |
7bdf4127 | 1061 | flags |= SEC_GROUP; |
252b5132 RH |
1062 | if ((hdr->sh_flags & SHF_ALLOC) != 0) |
1063 | { | |
1064 | flags |= SEC_ALLOC; | |
1065 | if (hdr->sh_type != SHT_NOBITS) | |
1066 | flags |= SEC_LOAD; | |
1067 | } | |
1068 | if ((hdr->sh_flags & SHF_WRITE) == 0) | |
1069 | flags |= SEC_READONLY; | |
1070 | if ((hdr->sh_flags & SHF_EXECINSTR) != 0) | |
1071 | flags |= SEC_CODE; | |
1072 | else if ((flags & SEC_LOAD) != 0) | |
1073 | flags |= SEC_DATA; | |
f5fa8ca2 JJ |
1074 | if ((hdr->sh_flags & SHF_MERGE) != 0) |
1075 | { | |
1076 | flags |= SEC_MERGE; | |
1077 | newsect->entsize = hdr->sh_entsize; | |
f5fa8ca2 | 1078 | } |
84865015 NC |
1079 | if ((hdr->sh_flags & SHF_STRINGS) != 0) |
1080 | flags |= SEC_STRINGS; | |
dbb410c3 AM |
1081 | if (hdr->sh_flags & SHF_GROUP) |
1082 | if (!setup_group (abfd, hdr, newsect)) | |
b34976b6 | 1083 | return FALSE; |
13ae64f3 JJ |
1084 | if ((hdr->sh_flags & SHF_TLS) != 0) |
1085 | flags |= SEC_THREAD_LOCAL; | |
18ae9cc1 L |
1086 | if ((hdr->sh_flags & SHF_EXCLUDE) != 0) |
1087 | flags |= SEC_EXCLUDE; | |
252b5132 | 1088 | |
df3a023b AM |
1089 | switch (elf_elfheader (abfd)->e_ident[EI_OSABI]) |
1090 | { | |
1091 | /* FIXME: We should not recognize SHF_GNU_MBIND for ELFOSABI_NONE, | |
1092 | but binutils as of 2019-07-23 did not set the EI_OSABI header | |
1093 | byte. */ | |
1094 | case ELFOSABI_NONE: | |
1095 | case ELFOSABI_GNU: | |
1096 | case ELFOSABI_FREEBSD: | |
1097 | if ((hdr->sh_flags & SHF_GNU_MBIND) != 0) | |
1098 | elf_tdata (abfd)->has_gnu_osabi |= elf_gnu_osabi_mbind; | |
1099 | break; | |
1100 | } | |
1101 | ||
3d2b39cf | 1102 | if ((flags & SEC_ALLOC) == 0) |
7a6cc5fb | 1103 | { |
3d2b39cf L |
1104 | /* The debugging sections appear to be recognized only by name, |
1105 | not any sort of flag. Their SEC_ALLOC bits are cleared. */ | |
3d2b39cf L |
1106 | if (name [0] == '.') |
1107 | { | |
f073ced3 AM |
1108 | const char *p; |
1109 | int n; | |
1110 | if (name[1] == 'd') | |
1111 | p = ".debug", n = 6; | |
1112 | else if (name[1] == 'g' && name[2] == 'n') | |
1113 | p = ".gnu.linkonce.wi.", n = 17; | |
1114 | else if (name[1] == 'g' && name[2] == 'd') | |
1115 | p = ".gdb_index", n = 11; /* yes we really do mean 11. */ | |
1116 | else if (name[1] == 'l') | |
1117 | p = ".line", n = 5; | |
1118 | else if (name[1] == 's') | |
1119 | p = ".stab", n = 5; | |
1120 | else if (name[1] == 'z') | |
1121 | p = ".zdebug", n = 7; | |
1122 | else | |
1123 | p = NULL, n = 0; | |
1124 | if (p != NULL && strncmp (name, p, n) == 0) | |
3d2b39cf L |
1125 | flags |= SEC_DEBUGGING; |
1126 | } | |
1127 | } | |
252b5132 RH |
1128 | |
1129 | /* As a GNU extension, if the name begins with .gnu.linkonce, we | |
1130 | only link a single copy of the section. This is used to support | |
1131 | g++. g++ will emit each template expansion in its own section. | |
1132 | The symbols will be defined as weak, so that multiple definitions | |
1133 | are permitted. The GNU linker extension is to actually discard | |
1134 | all but one of the sections. */ | |
0112cd26 | 1135 | if (CONST_STRNEQ (name, ".gnu.linkonce") |
b885599b | 1136 | && elf_next_in_group (newsect) == NULL) |
252b5132 RH |
1137 | flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD; |
1138 | ||
fa152c49 JW |
1139 | bed = get_elf_backend_data (abfd); |
1140 | if (bed->elf_backend_section_flags) | |
1141 | if (! bed->elf_backend_section_flags (&flags, hdr)) | |
b34976b6 | 1142 | return FALSE; |
fa152c49 | 1143 | |
fd361982 | 1144 | if (!bfd_set_section_flags (newsect, flags)) |
b34976b6 | 1145 | return FALSE; |
252b5132 | 1146 | |
718175fa JK |
1147 | /* We do not parse the PT_NOTE segments as we are interested even in the |
1148 | separate debug info files which may have the segments offsets corrupted. | |
1149 | PT_NOTEs from the core files are currently not parsed using BFD. */ | |
1150 | if (hdr->sh_type == SHT_NOTE) | |
1151 | { | |
baea7ef1 | 1152 | bfd_byte *contents; |
718175fa | 1153 | |
baea7ef1 | 1154 | if (!bfd_malloc_and_get_section (abfd, newsect, &contents)) |
718175fa JK |
1155 | return FALSE; |
1156 | ||
276da9b3 L |
1157 | elf_parse_notes (abfd, (char *) contents, hdr->sh_size, |
1158 | hdr->sh_offset, hdr->sh_addralign); | |
718175fa JK |
1159 | free (contents); |
1160 | } | |
1161 | ||
252b5132 RH |
1162 | if ((flags & SEC_ALLOC) != 0) |
1163 | { | |
1164 | Elf_Internal_Phdr *phdr; | |
6ffd7900 AM |
1165 | unsigned int i, nload; |
1166 | ||
1167 | /* Some ELF linkers produce binaries with all the program header | |
1168 | p_paddr fields zero. If we have such a binary with more than | |
1169 | one PT_LOAD header, then leave the section lma equal to vma | |
1170 | so that we don't create sections with overlapping lma. */ | |
1171 | phdr = elf_tdata (abfd)->phdr; | |
1172 | for (nload = 0, i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++) | |
1173 | if (phdr->p_paddr != 0) | |
1174 | break; | |
1175 | else if (phdr->p_type == PT_LOAD && phdr->p_memsz != 0) | |
1176 | ++nload; | |
1177 | if (i >= elf_elfheader (abfd)->e_phnum && nload > 1) | |
1178 | return TRUE; | |
252b5132 | 1179 | |
252b5132 RH |
1180 | phdr = elf_tdata (abfd)->phdr; |
1181 | for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++) | |
1182 | { | |
86b2281f AM |
1183 | if (((phdr->p_type == PT_LOAD |
1184 | && (hdr->sh_flags & SHF_TLS) == 0) | |
1185 | || phdr->p_type == PT_TLS) | |
9a83a553 | 1186 | && ELF_SECTION_IN_SEGMENT (hdr, phdr)) |
252b5132 | 1187 | { |
88967714 AM |
1188 | if ((flags & SEC_LOAD) == 0) |
1189 | newsect->lma = (phdr->p_paddr | |
1190 | + hdr->sh_addr - phdr->p_vaddr); | |
1191 | else | |
1192 | /* We used to use the same adjustment for SEC_LOAD | |
1193 | sections, but that doesn't work if the segment | |
1194 | is packed with code from multiple VMAs. | |
1195 | Instead we calculate the section LMA based on | |
1196 | the segment LMA. It is assumed that the | |
1197 | segment will contain sections with contiguous | |
1198 | LMAs, even if the VMAs are not. */ | |
1199 | newsect->lma = (phdr->p_paddr | |
1200 | + hdr->sh_offset - phdr->p_offset); | |
1201 | ||
1202 | /* With contiguous segments, we can't tell from file | |
1203 | offsets whether a section with zero size should | |
1204 | be placed at the end of one segment or the | |
1205 | beginning of the next. Decide based on vaddr. */ | |
1206 | if (hdr->sh_addr >= phdr->p_vaddr | |
1207 | && (hdr->sh_addr + hdr->sh_size | |
1208 | <= phdr->p_vaddr + phdr->p_memsz)) | |
1209 | break; | |
252b5132 RH |
1210 | } |
1211 | } | |
1212 | } | |
1213 | ||
4a114e3e L |
1214 | /* Compress/decompress DWARF debug sections with names: .debug_* and |
1215 | .zdebug_*, after the section flags is set. */ | |
1216 | if ((flags & SEC_DEBUGGING) | |
1217 | && ((name[1] == 'd' && name[6] == '_') | |
1218 | || (name[1] == 'z' && name[7] == '_'))) | |
1219 | { | |
1220 | enum { nothing, compress, decompress } action = nothing; | |
151411f8 | 1221 | int compression_header_size; |
dab394de | 1222 | bfd_size_type uncompressed_size; |
4207142d | 1223 | unsigned int uncompressed_align_power; |
151411f8 L |
1224 | bfd_boolean compressed |
1225 | = bfd_is_section_compressed_with_header (abfd, newsect, | |
dab394de | 1226 | &compression_header_size, |
4207142d MW |
1227 | &uncompressed_size, |
1228 | &uncompressed_align_power); | |
151411f8 | 1229 | if (compressed) |
4a114e3e L |
1230 | { |
1231 | /* Compressed section. Check if we should decompress. */ | |
1232 | if ((abfd->flags & BFD_DECOMPRESS)) | |
1233 | action = decompress; | |
1234 | } | |
151411f8 L |
1235 | |
1236 | /* Compress the uncompressed section or convert from/to .zdebug* | |
1237 | section. Check if we should compress. */ | |
1238 | if (action == nothing) | |
4a114e3e | 1239 | { |
151411f8 L |
1240 | if (newsect->size != 0 |
1241 | && (abfd->flags & BFD_COMPRESS) | |
1242 | && compression_header_size >= 0 | |
dab394de | 1243 | && uncompressed_size > 0 |
151411f8 L |
1244 | && (!compressed |
1245 | || ((compression_header_size > 0) | |
1246 | != ((abfd->flags & BFD_COMPRESS_GABI) != 0)))) | |
4a114e3e | 1247 | action = compress; |
151411f8 L |
1248 | else |
1249 | return TRUE; | |
4a114e3e L |
1250 | } |
1251 | ||
151411f8 | 1252 | if (action == compress) |
4a114e3e | 1253 | { |
4a114e3e L |
1254 | if (!bfd_init_section_compress_status (abfd, newsect)) |
1255 | { | |
4eca0228 | 1256 | _bfd_error_handler |
695344c0 | 1257 | /* xgettext:c-format */ |
871b3ab2 | 1258 | (_("%pB: unable to initialize compress status for section %s"), |
4a114e3e L |
1259 | abfd, name); |
1260 | return FALSE; | |
1261 | } | |
151411f8 L |
1262 | } |
1263 | else | |
1264 | { | |
4a114e3e L |
1265 | if (!bfd_init_section_decompress_status (abfd, newsect)) |
1266 | { | |
4eca0228 | 1267 | _bfd_error_handler |
695344c0 | 1268 | /* xgettext:c-format */ |
871b3ab2 | 1269 | (_("%pB: unable to initialize decompress status for section %s"), |
4a114e3e L |
1270 | abfd, name); |
1271 | return FALSE; | |
1272 | } | |
151411f8 L |
1273 | } |
1274 | ||
f6fe1ccd | 1275 | if (abfd->is_linker_input) |
151411f8 | 1276 | { |
f6fe1ccd L |
1277 | if (name[1] == 'z' |
1278 | && (action == decompress | |
1279 | || (action == compress | |
1280 | && (abfd->flags & BFD_COMPRESS_GABI) != 0))) | |
4e011fb5 | 1281 | { |
f6fe1ccd L |
1282 | /* Convert section name from .zdebug_* to .debug_* so |
1283 | that linker will consider this section as a debug | |
1284 | section. */ | |
1285 | char *new_name = convert_zdebug_to_debug (abfd, name); | |
151411f8 L |
1286 | if (new_name == NULL) |
1287 | return FALSE; | |
fd361982 | 1288 | bfd_rename_section (newsect, new_name); |
151411f8 | 1289 | } |
4a114e3e | 1290 | } |
f6fe1ccd L |
1291 | else |
1292 | /* For objdump, don't rename the section. For objcopy, delay | |
1293 | section rename to elf_fake_sections. */ | |
1294 | newsect->flags |= SEC_ELF_RENAME; | |
4a114e3e L |
1295 | } |
1296 | ||
cc5277b1 ML |
1297 | /* GCC uses .gnu.lto_.lto.<some_hash> as a LTO bytecode information |
1298 | section. */ | |
1299 | const char *lto_section_name = ".gnu.lto_.lto."; | |
1300 | if (strncmp (name, lto_section_name, strlen (lto_section_name)) == 0) | |
1301 | { | |
1302 | struct lto_section lsection; | |
1303 | if (bfd_get_section_contents (abfd, newsect, &lsection, 0, | |
1304 | sizeof (struct lto_section))) | |
1305 | abfd->lto_slim_object = lsection.slim_object; | |
1306 | } | |
1307 | ||
b34976b6 | 1308 | return TRUE; |
252b5132 RH |
1309 | } |
1310 | ||
84865015 NC |
1311 | const char *const bfd_elf_section_type_names[] = |
1312 | { | |
252b5132 RH |
1313 | "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB", |
1314 | "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE", | |
1315 | "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM", | |
1316 | }; | |
1317 | ||
1049f94e | 1318 | /* ELF relocs are against symbols. If we are producing relocatable |
252b5132 RH |
1319 | output, and the reloc is against an external symbol, and nothing |
1320 | has given us any additional addend, the resulting reloc will also | |
1321 | be against the same symbol. In such a case, we don't want to | |
1322 | change anything about the way the reloc is handled, since it will | |
1323 | all be done at final link time. Rather than put special case code | |
1324 | into bfd_perform_relocation, all the reloc types use this howto | |
1325 | function. It just short circuits the reloc if producing | |
1049f94e | 1326 | relocatable output against an external symbol. */ |
252b5132 | 1327 | |
252b5132 | 1328 | bfd_reloc_status_type |
217aa764 AM |
1329 | bfd_elf_generic_reloc (bfd *abfd ATTRIBUTE_UNUSED, |
1330 | arelent *reloc_entry, | |
1331 | asymbol *symbol, | |
1332 | void *data ATTRIBUTE_UNUSED, | |
1333 | asection *input_section, | |
1334 | bfd *output_bfd, | |
1335 | char **error_message ATTRIBUTE_UNUSED) | |
1336 | { | |
1337 | if (output_bfd != NULL | |
252b5132 RH |
1338 | && (symbol->flags & BSF_SECTION_SYM) == 0 |
1339 | && (! reloc_entry->howto->partial_inplace | |
1340 | || reloc_entry->addend == 0)) | |
1341 | { | |
1342 | reloc_entry->address += input_section->output_offset; | |
1343 | return bfd_reloc_ok; | |
1344 | } | |
1345 | ||
1346 | return bfd_reloc_continue; | |
1347 | } | |
1348 | \f | |
84865015 NC |
1349 | /* Returns TRUE if section A matches section B. |
1350 | Names, addresses and links may be different, but everything else | |
1351 | should be the same. */ | |
1352 | ||
1353 | static bfd_boolean | |
5522f910 NC |
1354 | section_match (const Elf_Internal_Shdr * a, |
1355 | const Elf_Internal_Shdr * b) | |
84865015 | 1356 | { |
ac85e67c AM |
1357 | if (a->sh_type != b->sh_type |
1358 | || ((a->sh_flags ^ b->sh_flags) & ~SHF_INFO_LINK) != 0 | |
1359 | || a->sh_addralign != b->sh_addralign | |
1360 | || a->sh_entsize != b->sh_entsize) | |
1361 | return FALSE; | |
1362 | if (a->sh_type == SHT_SYMTAB | |
1363 | || a->sh_type == SHT_STRTAB) | |
1364 | return TRUE; | |
1365 | return a->sh_size == b->sh_size; | |
84865015 NC |
1366 | } |
1367 | ||
1368 | /* Find a section in OBFD that has the same characteristics | |
1369 | as IHEADER. Return the index of this section or SHN_UNDEF if | |
1370 | none can be found. Check's section HINT first, as this is likely | |
1371 | to be the correct section. */ | |
1372 | ||
1373 | static unsigned int | |
5cc4ca83 ST |
1374 | find_link (const bfd *obfd, const Elf_Internal_Shdr *iheader, |
1375 | const unsigned int hint) | |
84865015 NC |
1376 | { |
1377 | Elf_Internal_Shdr ** oheaders = elf_elfsections (obfd); | |
1378 | unsigned int i; | |
1379 | ||
a55c9876 NC |
1380 | BFD_ASSERT (iheader != NULL); |
1381 | ||
1382 | /* See PR 20922 for a reproducer of the NULL test. */ | |
5cc4ca83 ST |
1383 | if (hint < elf_numsections (obfd) |
1384 | && oheaders[hint] != NULL | |
a55c9876 | 1385 | && section_match (oheaders[hint], iheader)) |
84865015 NC |
1386 | return hint; |
1387 | ||
1388 | for (i = 1; i < elf_numsections (obfd); i++) | |
1389 | { | |
1390 | Elf_Internal_Shdr * oheader = oheaders[i]; | |
1391 | ||
a55c9876 NC |
1392 | if (oheader == NULL) |
1393 | continue; | |
84865015 NC |
1394 | if (section_match (oheader, iheader)) |
1395 | /* FIXME: Do we care if there is a potential for | |
1396 | multiple matches ? */ | |
1397 | return i; | |
1398 | } | |
1399 | ||
1400 | return SHN_UNDEF; | |
1401 | } | |
1402 | ||
5522f910 NC |
1403 | /* PR 19938: Attempt to set the ELF section header fields of an OS or |
1404 | Processor specific section, based upon a matching input section. | |
1405 | Returns TRUE upon success, FALSE otherwise. */ | |
07d6d2b8 | 1406 | |
5522f910 NC |
1407 | static bfd_boolean |
1408 | copy_special_section_fields (const bfd *ibfd, | |
1409 | bfd *obfd, | |
1410 | const Elf_Internal_Shdr *iheader, | |
1411 | Elf_Internal_Shdr *oheader, | |
1412 | const unsigned int secnum) | |
1413 | { | |
1414 | const struct elf_backend_data *bed = get_elf_backend_data (obfd); | |
1415 | const Elf_Internal_Shdr **iheaders = (const Elf_Internal_Shdr **) elf_elfsections (ibfd); | |
1416 | bfd_boolean changed = FALSE; | |
1417 | unsigned int sh_link; | |
1418 | ||
1419 | if (oheader->sh_type == SHT_NOBITS) | |
1420 | { | |
1421 | /* This is a feature for objcopy --only-keep-debug: | |
1422 | When a section's type is changed to NOBITS, we preserve | |
1423 | the sh_link and sh_info fields so that they can be | |
1424 | matched up with the original. | |
1425 | ||
1426 | Note: Strictly speaking these assignments are wrong. | |
1427 | The sh_link and sh_info fields should point to the | |
1428 | relevent sections in the output BFD, which may not be in | |
1429 | the same location as they were in the input BFD. But | |
1430 | the whole point of this action is to preserve the | |
1431 | original values of the sh_link and sh_info fields, so | |
1432 | that they can be matched up with the section headers in | |
1433 | the original file. So strictly speaking we may be | |
1434 | creating an invalid ELF file, but it is only for a file | |
1435 | that just contains debug info and only for sections | |
1436 | without any contents. */ | |
1437 | if (oheader->sh_link == 0) | |
1438 | oheader->sh_link = iheader->sh_link; | |
1439 | if (oheader->sh_info == 0) | |
1440 | oheader->sh_info = iheader->sh_info; | |
1441 | return TRUE; | |
1442 | } | |
1443 | ||
1444 | /* Allow the target a chance to decide how these fields should be set. */ | |
1445 | if (bed->elf_backend_copy_special_section_fields != NULL | |
1446 | && bed->elf_backend_copy_special_section_fields | |
1447 | (ibfd, obfd, iheader, oheader)) | |
1448 | return TRUE; | |
1449 | ||
1450 | /* We have an iheader which might match oheader, and which has non-zero | |
1451 | sh_info and/or sh_link fields. Attempt to follow those links and find | |
1452 | the section in the output bfd which corresponds to the linked section | |
1453 | in the input bfd. */ | |
1454 | if (iheader->sh_link != SHN_UNDEF) | |
1455 | { | |
4f3ca05b NC |
1456 | /* See PR 20931 for a reproducer. */ |
1457 | if (iheader->sh_link >= elf_numsections (ibfd)) | |
1458 | { | |
76cfced5 | 1459 | _bfd_error_handler |
4f3ca05b | 1460 | /* xgettext:c-format */ |
9793eb77 | 1461 | (_("%pB: invalid sh_link field (%d) in section number %d"), |
4f3ca05b NC |
1462 | ibfd, iheader->sh_link, secnum); |
1463 | return FALSE; | |
1464 | } | |
1465 | ||
5522f910 NC |
1466 | sh_link = find_link (obfd, iheaders[iheader->sh_link], iheader->sh_link); |
1467 | if (sh_link != SHN_UNDEF) | |
1468 | { | |
1469 | oheader->sh_link = sh_link; | |
1470 | changed = TRUE; | |
1471 | } | |
1472 | else | |
1473 | /* FIXME: Should we install iheader->sh_link | |
1474 | if we could not find a match ? */ | |
76cfced5 | 1475 | _bfd_error_handler |
695344c0 | 1476 | /* xgettext:c-format */ |
9793eb77 | 1477 | (_("%pB: failed to find link section for section %d"), obfd, secnum); |
5522f910 NC |
1478 | } |
1479 | ||
1480 | if (iheader->sh_info) | |
1481 | { | |
1482 | /* The sh_info field can hold arbitrary information, but if the | |
1483 | SHF_LINK_INFO flag is set then it should be interpreted as a | |
1484 | section index. */ | |
1485 | if (iheader->sh_flags & SHF_INFO_LINK) | |
1486 | { | |
1487 | sh_link = find_link (obfd, iheaders[iheader->sh_info], | |
1488 | iheader->sh_info); | |
1489 | if (sh_link != SHN_UNDEF) | |
1490 | oheader->sh_flags |= SHF_INFO_LINK; | |
1491 | } | |
1492 | else | |
1493 | /* No idea what it means - just copy it. */ | |
1494 | sh_link = iheader->sh_info; | |
1495 | ||
1496 | if (sh_link != SHN_UNDEF) | |
1497 | { | |
1498 | oheader->sh_info = sh_link; | |
1499 | changed = TRUE; | |
1500 | } | |
1501 | else | |
76cfced5 | 1502 | _bfd_error_handler |
695344c0 | 1503 | /* xgettext:c-format */ |
9793eb77 | 1504 | (_("%pB: failed to find info section for section %d"), obfd, secnum); |
5522f910 NC |
1505 | } |
1506 | ||
1507 | return changed; | |
1508 | } | |
07d6d2b8 | 1509 | |
0ac4564e L |
1510 | /* Copy the program header and other data from one object module to |
1511 | another. */ | |
252b5132 | 1512 | |
b34976b6 | 1513 | bfd_boolean |
217aa764 | 1514 | _bfd_elf_copy_private_bfd_data (bfd *ibfd, bfd *obfd) |
2d502050 | 1515 | { |
5522f910 NC |
1516 | const Elf_Internal_Shdr **iheaders = (const Elf_Internal_Shdr **) elf_elfsections (ibfd); |
1517 | Elf_Internal_Shdr **oheaders = elf_elfsections (obfd); | |
1518 | const struct elf_backend_data *bed; | |
84865015 NC |
1519 | unsigned int i; |
1520 | ||
2d502050 | 1521 | if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour |
84865015 | 1522 | || bfd_get_flavour (obfd) != bfd_target_elf_flavour) |
b34976b6 | 1523 | return TRUE; |
2d502050 | 1524 | |
57b828ef L |
1525 | if (!elf_flags_init (obfd)) |
1526 | { | |
1527 | elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags; | |
1528 | elf_flags_init (obfd) = TRUE; | |
1529 | } | |
2d502050 | 1530 | |
0ac4564e | 1531 | elf_gp (obfd) = elf_gp (ibfd); |
57b828ef L |
1532 | |
1533 | /* Also copy the EI_OSABI field. */ | |
1534 | elf_elfheader (obfd)->e_ident[EI_OSABI] = | |
1535 | elf_elfheader (ibfd)->e_ident[EI_OSABI]; | |
104d59d1 | 1536 | |
5522f910 NC |
1537 | /* If set, copy the EI_ABIVERSION field. */ |
1538 | if (elf_elfheader (ibfd)->e_ident[EI_ABIVERSION]) | |
1539 | elf_elfheader (obfd)->e_ident[EI_ABIVERSION] | |
1540 | = elf_elfheader (ibfd)->e_ident[EI_ABIVERSION]; | |
07d6d2b8 | 1541 | |
104d59d1 JM |
1542 | /* Copy object attributes. */ |
1543 | _bfd_elf_copy_obj_attributes (ibfd, obfd); | |
63b9bbb7 | 1544 | |
84865015 NC |
1545 | if (iheaders == NULL || oheaders == NULL) |
1546 | return TRUE; | |
63b9bbb7 | 1547 | |
5522f910 NC |
1548 | bed = get_elf_backend_data (obfd); |
1549 | ||
1550 | /* Possibly copy other fields in the section header. */ | |
84865015 | 1551 | for (i = 1; i < elf_numsections (obfd); i++) |
63b9bbb7 | 1552 | { |
84865015 NC |
1553 | unsigned int j; |
1554 | Elf_Internal_Shdr * oheader = oheaders[i]; | |
63b9bbb7 | 1555 | |
5522f910 NC |
1556 | /* Ignore ordinary sections. SHT_NOBITS sections are considered however |
1557 | because of a special case need for generating separate debug info | |
1558 | files. See below for more details. */ | |
84865015 NC |
1559 | if (oheader == NULL |
1560 | || (oheader->sh_type != SHT_NOBITS | |
5522f910 NC |
1561 | && oheader->sh_type < SHT_LOOS)) |
1562 | continue; | |
1563 | ||
1564 | /* Ignore empty sections, and sections whose | |
1565 | fields have already been initialised. */ | |
1566 | if (oheader->sh_size == 0 | |
84865015 NC |
1567 | || (oheader->sh_info != 0 && oheader->sh_link != 0)) |
1568 | continue; | |
63b9bbb7 | 1569 | |
84865015 | 1570 | /* Scan for the matching section in the input bfd. |
5522f910 NC |
1571 | First we try for a direct mapping between the input and output sections. */ |
1572 | for (j = 1; j < elf_numsections (ibfd); j++) | |
1573 | { | |
1574 | const Elf_Internal_Shdr * iheader = iheaders[j]; | |
1575 | ||
1576 | if (iheader == NULL) | |
1577 | continue; | |
1578 | ||
1579 | if (oheader->bfd_section != NULL | |
1580 | && iheader->bfd_section != NULL | |
1581 | && iheader->bfd_section->output_section != NULL | |
1582 | && iheader->bfd_section->output_section == oheader->bfd_section) | |
1583 | { | |
1584 | /* We have found a connection from the input section to the | |
1585 | output section. Attempt to copy the header fields. If | |
1586 | this fails then do not try any further sections - there | |
1587 | should only be a one-to-one mapping between input and output. */ | |
1588 | if (! copy_special_section_fields (ibfd, obfd, iheader, oheader, i)) | |
1589 | j = elf_numsections (ibfd); | |
1590 | break; | |
1591 | } | |
1592 | } | |
1593 | ||
1594 | if (j < elf_numsections (ibfd)) | |
1595 | continue; | |
1596 | ||
1597 | /* That failed. So try to deduce the corresponding input section. | |
84865015 NC |
1598 | Unfortunately we cannot compare names as the output string table |
1599 | is empty, so instead we check size, address and type. */ | |
1600 | for (j = 1; j < elf_numsections (ibfd); j++) | |
1601 | { | |
5522f910 | 1602 | const Elf_Internal_Shdr * iheader = iheaders[j]; |
84865015 | 1603 | |
5522f910 NC |
1604 | if (iheader == NULL) |
1605 | continue; | |
1606 | ||
1607 | /* Try matching fields in the input section's header. | |
1608 | Since --only-keep-debug turns all non-debug sections into | |
84865015 NC |
1609 | SHT_NOBITS sections, the output SHT_NOBITS type matches any |
1610 | input type. */ | |
1611 | if ((oheader->sh_type == SHT_NOBITS | |
1612 | || iheader->sh_type == oheader->sh_type) | |
5522f910 NC |
1613 | && (iheader->sh_flags & ~ SHF_INFO_LINK) |
1614 | == (oheader->sh_flags & ~ SHF_INFO_LINK) | |
84865015 NC |
1615 | && iheader->sh_addralign == oheader->sh_addralign |
1616 | && iheader->sh_entsize == oheader->sh_entsize | |
1617 | && iheader->sh_size == oheader->sh_size | |
1618 | && iheader->sh_addr == oheader->sh_addr | |
1619 | && (iheader->sh_info != oheader->sh_info | |
1620 | || iheader->sh_link != oheader->sh_link)) | |
63b9bbb7 | 1621 | { |
5522f910 NC |
1622 | if (copy_special_section_fields (ibfd, obfd, iheader, oheader, i)) |
1623 | break; | |
63b9bbb7 NC |
1624 | } |
1625 | } | |
5522f910 NC |
1626 | |
1627 | if (j == elf_numsections (ibfd) && oheader->sh_type >= SHT_LOOS) | |
1628 | { | |
1629 | /* Final attempt. Call the backend copy function | |
1630 | with a NULL input section. */ | |
1631 | if (bed->elf_backend_copy_special_section_fields != NULL) | |
1632 | bed->elf_backend_copy_special_section_fields (ibfd, obfd, NULL, oheader); | |
1633 | } | |
63b9bbb7 NC |
1634 | } |
1635 | ||
b34976b6 | 1636 | return TRUE; |
2d502050 L |
1637 | } |
1638 | ||
cedc298e L |
1639 | static const char * |
1640 | get_segment_type (unsigned int p_type) | |
1641 | { | |
1642 | const char *pt; | |
1643 | switch (p_type) | |
1644 | { | |
1645 | case PT_NULL: pt = "NULL"; break; | |
1646 | case PT_LOAD: pt = "LOAD"; break; | |
1647 | case PT_DYNAMIC: pt = "DYNAMIC"; break; | |
1648 | case PT_INTERP: pt = "INTERP"; break; | |
1649 | case PT_NOTE: pt = "NOTE"; break; | |
1650 | case PT_SHLIB: pt = "SHLIB"; break; | |
1651 | case PT_PHDR: pt = "PHDR"; break; | |
1652 | case PT_TLS: pt = "TLS"; break; | |
1653 | case PT_GNU_EH_FRAME: pt = "EH_FRAME"; break; | |
2b05f1b7 | 1654 | case PT_GNU_STACK: pt = "STACK"; break; |
cedc298e L |
1655 | case PT_GNU_RELRO: pt = "RELRO"; break; |
1656 | default: pt = NULL; break; | |
1657 | } | |
1658 | return pt; | |
1659 | } | |
1660 | ||
f0b79d91 L |
1661 | /* Print out the program headers. */ |
1662 | ||
b34976b6 | 1663 | bfd_boolean |
217aa764 | 1664 | _bfd_elf_print_private_bfd_data (bfd *abfd, void *farg) |
252b5132 | 1665 | { |
a50b1753 | 1666 | FILE *f = (FILE *) farg; |
252b5132 RH |
1667 | Elf_Internal_Phdr *p; |
1668 | asection *s; | |
1669 | bfd_byte *dynbuf = NULL; | |
1670 | ||
1671 | p = elf_tdata (abfd)->phdr; | |
1672 | if (p != NULL) | |
1673 | { | |
1674 | unsigned int i, c; | |
1675 | ||
1676 | fprintf (f, _("\nProgram Header:\n")); | |
1677 | c = elf_elfheader (abfd)->e_phnum; | |
1678 | for (i = 0; i < c; i++, p++) | |
1679 | { | |
cedc298e | 1680 | const char *pt = get_segment_type (p->p_type); |
252b5132 RH |
1681 | char buf[20]; |
1682 | ||
cedc298e | 1683 | if (pt == NULL) |
252b5132 | 1684 | { |
cedc298e L |
1685 | sprintf (buf, "0x%lx", p->p_type); |
1686 | pt = buf; | |
252b5132 | 1687 | } |
dc810e39 | 1688 | fprintf (f, "%8s off 0x", pt); |
60b89a18 | 1689 | bfd_fprintf_vma (abfd, f, p->p_offset); |
252b5132 | 1690 | fprintf (f, " vaddr 0x"); |
60b89a18 | 1691 | bfd_fprintf_vma (abfd, f, p->p_vaddr); |
252b5132 | 1692 | fprintf (f, " paddr 0x"); |
60b89a18 | 1693 | bfd_fprintf_vma (abfd, f, p->p_paddr); |
252b5132 RH |
1694 | fprintf (f, " align 2**%u\n", bfd_log2 (p->p_align)); |
1695 | fprintf (f, " filesz 0x"); | |
60b89a18 | 1696 | bfd_fprintf_vma (abfd, f, p->p_filesz); |
252b5132 | 1697 | fprintf (f, " memsz 0x"); |
60b89a18 | 1698 | bfd_fprintf_vma (abfd, f, p->p_memsz); |
252b5132 RH |
1699 | fprintf (f, " flags %c%c%c", |
1700 | (p->p_flags & PF_R) != 0 ? 'r' : '-', | |
1701 | (p->p_flags & PF_W) != 0 ? 'w' : '-', | |
1702 | (p->p_flags & PF_X) != 0 ? 'x' : '-'); | |
dc810e39 AM |
1703 | if ((p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X)) != 0) |
1704 | fprintf (f, " %lx", p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X)); | |
252b5132 RH |
1705 | fprintf (f, "\n"); |
1706 | } | |
1707 | } | |
1708 | ||
1709 | s = bfd_get_section_by_name (abfd, ".dynamic"); | |
1710 | if (s != NULL) | |
1711 | { | |
cb33740c | 1712 | unsigned int elfsec; |
dc810e39 | 1713 | unsigned long shlink; |
252b5132 RH |
1714 | bfd_byte *extdyn, *extdynend; |
1715 | size_t extdynsize; | |
217aa764 | 1716 | void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *); |
252b5132 RH |
1717 | |
1718 | fprintf (f, _("\nDynamic Section:\n")); | |
1719 | ||
eea6121a | 1720 | if (!bfd_malloc_and_get_section (abfd, s, &dynbuf)) |
252b5132 RH |
1721 | goto error_return; |
1722 | ||
1723 | elfsec = _bfd_elf_section_from_bfd_section (abfd, s); | |
cb33740c | 1724 | if (elfsec == SHN_BAD) |
252b5132 | 1725 | goto error_return; |
dc810e39 | 1726 | shlink = elf_elfsections (abfd)[elfsec]->sh_link; |
252b5132 RH |
1727 | |
1728 | extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn; | |
1729 | swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in; | |
1730 | ||
1731 | extdyn = dynbuf; | |
06614111 NC |
1732 | /* PR 17512: file: 6f427532. */ |
1733 | if (s->size < extdynsize) | |
1734 | goto error_return; | |
eea6121a | 1735 | extdynend = extdyn + s->size; |
1036838a | 1736 | /* PR 17512: file: id:000006,sig:06,src:000000,op:flip4,pos:5664. |
07d6d2b8 | 1737 | Fix range check. */ |
1036838a | 1738 | for (; extdyn <= (extdynend - extdynsize); extdyn += extdynsize) |
252b5132 RH |
1739 | { |
1740 | Elf_Internal_Dyn dyn; | |
ad9563d6 | 1741 | const char *name = ""; |
252b5132 | 1742 | char ab[20]; |
b34976b6 | 1743 | bfd_boolean stringp; |
ad9563d6 | 1744 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
252b5132 | 1745 | |
217aa764 | 1746 | (*swap_dyn_in) (abfd, extdyn, &dyn); |
252b5132 RH |
1747 | |
1748 | if (dyn.d_tag == DT_NULL) | |
1749 | break; | |
1750 | ||
b34976b6 | 1751 | stringp = FALSE; |
252b5132 RH |
1752 | switch (dyn.d_tag) |
1753 | { | |
1754 | default: | |
ad9563d6 CM |
1755 | if (bed->elf_backend_get_target_dtag) |
1756 | name = (*bed->elf_backend_get_target_dtag) (dyn.d_tag); | |
1757 | ||
1758 | if (!strcmp (name, "")) | |
1759 | { | |
cd9af601 | 1760 | sprintf (ab, "%#" BFD_VMA_FMT "x", dyn.d_tag); |
ad9563d6 CM |
1761 | name = ab; |
1762 | } | |
252b5132 RH |
1763 | break; |
1764 | ||
b34976b6 | 1765 | case DT_NEEDED: name = "NEEDED"; stringp = TRUE; break; |
252b5132 RH |
1766 | case DT_PLTRELSZ: name = "PLTRELSZ"; break; |
1767 | case DT_PLTGOT: name = "PLTGOT"; break; | |
1768 | case DT_HASH: name = "HASH"; break; | |
1769 | case DT_STRTAB: name = "STRTAB"; break; | |
1770 | case DT_SYMTAB: name = "SYMTAB"; break; | |
1771 | case DT_RELA: name = "RELA"; break; | |
1772 | case DT_RELASZ: name = "RELASZ"; break; | |
1773 | case DT_RELAENT: name = "RELAENT"; break; | |
1774 | case DT_STRSZ: name = "STRSZ"; break; | |
1775 | case DT_SYMENT: name = "SYMENT"; break; | |
1776 | case DT_INIT: name = "INIT"; break; | |
1777 | case DT_FINI: name = "FINI"; break; | |
b34976b6 AM |
1778 | case DT_SONAME: name = "SONAME"; stringp = TRUE; break; |
1779 | case DT_RPATH: name = "RPATH"; stringp = TRUE; break; | |
252b5132 RH |
1780 | case DT_SYMBOLIC: name = "SYMBOLIC"; break; |
1781 | case DT_REL: name = "REL"; break; | |
1782 | case DT_RELSZ: name = "RELSZ"; break; | |
1783 | case DT_RELENT: name = "RELENT"; break; | |
1784 | case DT_PLTREL: name = "PLTREL"; break; | |
1785 | case DT_DEBUG: name = "DEBUG"; break; | |
1786 | case DT_TEXTREL: name = "TEXTREL"; break; | |
1787 | case DT_JMPREL: name = "JMPREL"; break; | |
94558834 L |
1788 | case DT_BIND_NOW: name = "BIND_NOW"; break; |
1789 | case DT_INIT_ARRAY: name = "INIT_ARRAY"; break; | |
1790 | case DT_FINI_ARRAY: name = "FINI_ARRAY"; break; | |
1791 | case DT_INIT_ARRAYSZ: name = "INIT_ARRAYSZ"; break; | |
1792 | case DT_FINI_ARRAYSZ: name = "FINI_ARRAYSZ"; break; | |
b34976b6 | 1793 | case DT_RUNPATH: name = "RUNPATH"; stringp = TRUE; break; |
94558834 L |
1794 | case DT_FLAGS: name = "FLAGS"; break; |
1795 | case DT_PREINIT_ARRAY: name = "PREINIT_ARRAY"; break; | |
1796 | case DT_PREINIT_ARRAYSZ: name = "PREINIT_ARRAYSZ"; break; | |
d48188b9 | 1797 | case DT_CHECKSUM: name = "CHECKSUM"; break; |
94558834 L |
1798 | case DT_PLTPADSZ: name = "PLTPADSZ"; break; |
1799 | case DT_MOVEENT: name = "MOVEENT"; break; | |
1800 | case DT_MOVESZ: name = "MOVESZ"; break; | |
1801 | case DT_FEATURE: name = "FEATURE"; break; | |
1802 | case DT_POSFLAG_1: name = "POSFLAG_1"; break; | |
1803 | case DT_SYMINSZ: name = "SYMINSZ"; break; | |
1804 | case DT_SYMINENT: name = "SYMINENT"; break; | |
b34976b6 AM |
1805 | case DT_CONFIG: name = "CONFIG"; stringp = TRUE; break; |
1806 | case DT_DEPAUDIT: name = "DEPAUDIT"; stringp = TRUE; break; | |
1807 | case DT_AUDIT: name = "AUDIT"; stringp = TRUE; break; | |
94558834 L |
1808 | case DT_PLTPAD: name = "PLTPAD"; break; |
1809 | case DT_MOVETAB: name = "MOVETAB"; break; | |
1810 | case DT_SYMINFO: name = "SYMINFO"; break; | |
1811 | case DT_RELACOUNT: name = "RELACOUNT"; break; | |
1812 | case DT_RELCOUNT: name = "RELCOUNT"; break; | |
1813 | case DT_FLAGS_1: name = "FLAGS_1"; break; | |
252b5132 RH |
1814 | case DT_VERSYM: name = "VERSYM"; break; |
1815 | case DT_VERDEF: name = "VERDEF"; break; | |
1816 | case DT_VERDEFNUM: name = "VERDEFNUM"; break; | |
1817 | case DT_VERNEED: name = "VERNEED"; break; | |
1818 | case DT_VERNEEDNUM: name = "VERNEEDNUM"; break; | |
b34976b6 | 1819 | case DT_AUXILIARY: name = "AUXILIARY"; stringp = TRUE; break; |
94558834 | 1820 | case DT_USED: name = "USED"; break; |
b34976b6 | 1821 | case DT_FILTER: name = "FILTER"; stringp = TRUE; break; |
fdc90cb4 | 1822 | case DT_GNU_HASH: name = "GNU_HASH"; break; |
252b5132 RH |
1823 | } |
1824 | ||
ad9563d6 | 1825 | fprintf (f, " %-20s ", name); |
252b5132 | 1826 | if (! stringp) |
a1f3c56e AN |
1827 | { |
1828 | fprintf (f, "0x"); | |
1829 | bfd_fprintf_vma (abfd, f, dyn.d_un.d_val); | |
1830 | } | |
252b5132 RH |
1831 | else |
1832 | { | |
1833 | const char *string; | |
dc810e39 | 1834 | unsigned int tagv = dyn.d_un.d_val; |
252b5132 | 1835 | |
dc810e39 | 1836 | string = bfd_elf_string_from_elf_section (abfd, shlink, tagv); |
252b5132 RH |
1837 | if (string == NULL) |
1838 | goto error_return; | |
1839 | fprintf (f, "%s", string); | |
1840 | } | |
1841 | fprintf (f, "\n"); | |
1842 | } | |
1843 | ||
1844 | free (dynbuf); | |
1845 | dynbuf = NULL; | |
1846 | } | |
1847 | ||
1848 | if ((elf_dynverdef (abfd) != 0 && elf_tdata (abfd)->verdef == NULL) | |
1849 | || (elf_dynverref (abfd) != 0 && elf_tdata (abfd)->verref == NULL)) | |
1850 | { | |
fc0e6df6 | 1851 | if (! _bfd_elf_slurp_version_tables (abfd, FALSE)) |
b34976b6 | 1852 | return FALSE; |
252b5132 RH |
1853 | } |
1854 | ||
1855 | if (elf_dynverdef (abfd) != 0) | |
1856 | { | |
1857 | Elf_Internal_Verdef *t; | |
1858 | ||
1859 | fprintf (f, _("\nVersion definitions:\n")); | |
1860 | for (t = elf_tdata (abfd)->verdef; t != NULL; t = t->vd_nextdef) | |
1861 | { | |
1862 | fprintf (f, "%d 0x%2.2x 0x%8.8lx %s\n", t->vd_ndx, | |
d0fb9a8d JJ |
1863 | t->vd_flags, t->vd_hash, |
1864 | t->vd_nodename ? t->vd_nodename : "<corrupt>"); | |
1865 | if (t->vd_auxptr != NULL && t->vd_auxptr->vda_nextptr != NULL) | |
252b5132 RH |
1866 | { |
1867 | Elf_Internal_Verdaux *a; | |
1868 | ||
1869 | fprintf (f, "\t"); | |
1870 | for (a = t->vd_auxptr->vda_nextptr; | |
1871 | a != NULL; | |
1872 | a = a->vda_nextptr) | |
d0fb9a8d JJ |
1873 | fprintf (f, "%s ", |
1874 | a->vda_nodename ? a->vda_nodename : "<corrupt>"); | |
252b5132 RH |
1875 | fprintf (f, "\n"); |
1876 | } | |
1877 | } | |
1878 | } | |
1879 | ||
1880 | if (elf_dynverref (abfd) != 0) | |
1881 | { | |
1882 | Elf_Internal_Verneed *t; | |
1883 | ||
1884 | fprintf (f, _("\nVersion References:\n")); | |
1885 | for (t = elf_tdata (abfd)->verref; t != NULL; t = t->vn_nextref) | |
1886 | { | |
1887 | Elf_Internal_Vernaux *a; | |
1888 | ||
d0fb9a8d JJ |
1889 | fprintf (f, _(" required from %s:\n"), |
1890 | t->vn_filename ? t->vn_filename : "<corrupt>"); | |
252b5132 RH |
1891 | for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) |
1892 | fprintf (f, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a->vna_hash, | |
d0fb9a8d JJ |
1893 | a->vna_flags, a->vna_other, |
1894 | a->vna_nodename ? a->vna_nodename : "<corrupt>"); | |
252b5132 RH |
1895 | } |
1896 | } | |
1897 | ||
b34976b6 | 1898 | return TRUE; |
252b5132 RH |
1899 | |
1900 | error_return: | |
1901 | if (dynbuf != NULL) | |
1902 | free (dynbuf); | |
b34976b6 | 1903 | return FALSE; |
252b5132 RH |
1904 | } |
1905 | ||
bb4d2ac2 L |
1906 | /* Get version string. */ |
1907 | ||
1908 | const char * | |
60bb06bc L |
1909 | _bfd_elf_get_symbol_version_string (bfd *abfd, asymbol *symbol, |
1910 | bfd_boolean *hidden) | |
bb4d2ac2 L |
1911 | { |
1912 | const char *version_string = NULL; | |
1913 | if (elf_dynversym (abfd) != 0 | |
1914 | && (elf_dynverdef (abfd) != 0 || elf_dynverref (abfd) != 0)) | |
1915 | { | |
1916 | unsigned int vernum = ((elf_symbol_type *) symbol)->version; | |
1917 | ||
1918 | *hidden = (vernum & VERSYM_HIDDEN) != 0; | |
1919 | vernum &= VERSYM_VERSION; | |
1920 | ||
1921 | if (vernum == 0) | |
1922 | version_string = ""; | |
1f6f5dba L |
1923 | else if (vernum == 1 |
1924 | && (vernum > elf_tdata (abfd)->cverdefs | |
1925 | || (elf_tdata (abfd)->verdef[0].vd_flags | |
1926 | == VER_FLG_BASE))) | |
bb4d2ac2 L |
1927 | version_string = "Base"; |
1928 | else if (vernum <= elf_tdata (abfd)->cverdefs) | |
1929 | version_string = | |
1930 | elf_tdata (abfd)->verdef[vernum - 1].vd_nodename; | |
1931 | else | |
1932 | { | |
1933 | Elf_Internal_Verneed *t; | |
1934 | ||
7a815dd5 | 1935 | version_string = _("<corrupt>"); |
bb4d2ac2 L |
1936 | for (t = elf_tdata (abfd)->verref; |
1937 | t != NULL; | |
1938 | t = t->vn_nextref) | |
1939 | { | |
1940 | Elf_Internal_Vernaux *a; | |
1941 | ||
1942 | for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) | |
1943 | { | |
1944 | if (a->vna_other == vernum) | |
1945 | { | |
1946 | version_string = a->vna_nodename; | |
1947 | break; | |
1948 | } | |
1949 | } | |
1950 | } | |
1951 | } | |
1952 | } | |
1953 | return version_string; | |
1954 | } | |
1955 | ||
252b5132 RH |
1956 | /* Display ELF-specific fields of a symbol. */ |
1957 | ||
1958 | void | |
217aa764 AM |
1959 | bfd_elf_print_symbol (bfd *abfd, |
1960 | void *filep, | |
1961 | asymbol *symbol, | |
1962 | bfd_print_symbol_type how) | |
252b5132 | 1963 | { |
a50b1753 | 1964 | FILE *file = (FILE *) filep; |
252b5132 RH |
1965 | switch (how) |
1966 | { | |
1967 | case bfd_print_symbol_name: | |
1968 | fprintf (file, "%s", symbol->name); | |
1969 | break; | |
1970 | case bfd_print_symbol_more: | |
1971 | fprintf (file, "elf "); | |
60b89a18 | 1972 | bfd_fprintf_vma (abfd, file, symbol->value); |
cd9af601 | 1973 | fprintf (file, " %x", symbol->flags); |
252b5132 RH |
1974 | break; |
1975 | case bfd_print_symbol_all: | |
1976 | { | |
4e8a9624 AM |
1977 | const char *section_name; |
1978 | const char *name = NULL; | |
9c5bfbb7 | 1979 | const struct elf_backend_data *bed; |
7a13edea | 1980 | unsigned char st_other; |
dbb410c3 | 1981 | bfd_vma val; |
bb4d2ac2 L |
1982 | const char *version_string; |
1983 | bfd_boolean hidden; | |
c044fabd | 1984 | |
252b5132 | 1985 | section_name = symbol->section ? symbol->section->name : "(*none*)"; |
587ff49e RH |
1986 | |
1987 | bed = get_elf_backend_data (abfd); | |
1988 | if (bed->elf_backend_print_symbol_all) | |
c044fabd | 1989 | name = (*bed->elf_backend_print_symbol_all) (abfd, filep, symbol); |
587ff49e RH |
1990 | |
1991 | if (name == NULL) | |
1992 | { | |
7ee38065 | 1993 | name = symbol->name; |
217aa764 | 1994 | bfd_print_symbol_vandf (abfd, file, symbol); |
587ff49e RH |
1995 | } |
1996 | ||
252b5132 RH |
1997 | fprintf (file, " %s\t", section_name); |
1998 | /* Print the "other" value for a symbol. For common symbols, | |
1999 | we've already printed the size; now print the alignment. | |
2000 | For other symbols, we have no specified alignment, and | |
2001 | we've printed the address; now print the size. */ | |
dcf6c779 | 2002 | if (symbol->section && bfd_is_com_section (symbol->section)) |
dbb410c3 AM |
2003 | val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_value; |
2004 | else | |
2005 | val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_size; | |
2006 | bfd_fprintf_vma (abfd, file, val); | |
252b5132 RH |
2007 | |
2008 | /* If we have version information, print it. */ | |
60bb06bc L |
2009 | version_string = _bfd_elf_get_symbol_version_string (abfd, |
2010 | symbol, | |
2011 | &hidden); | |
bb4d2ac2 | 2012 | if (version_string) |
252b5132 | 2013 | { |
bb4d2ac2 | 2014 | if (!hidden) |
252b5132 RH |
2015 | fprintf (file, " %-11s", version_string); |
2016 | else | |
2017 | { | |
2018 | int i; | |
2019 | ||
2020 | fprintf (file, " (%s)", version_string); | |
2021 | for (i = 10 - strlen (version_string); i > 0; --i) | |
2022 | putc (' ', file); | |
2023 | } | |
2024 | } | |
2025 | ||
2026 | /* If the st_other field is not zero, print it. */ | |
7a13edea | 2027 | st_other = ((elf_symbol_type *) symbol)->internal_elf_sym.st_other; |
c044fabd | 2028 | |
7a13edea NC |
2029 | switch (st_other) |
2030 | { | |
2031 | case 0: break; | |
2032 | case STV_INTERNAL: fprintf (file, " .internal"); break; | |
2033 | case STV_HIDDEN: fprintf (file, " .hidden"); break; | |
2034 | case STV_PROTECTED: fprintf (file, " .protected"); break; | |
2035 | default: | |
2036 | /* Some other non-defined flags are also present, so print | |
2037 | everything hex. */ | |
2038 | fprintf (file, " 0x%02x", (unsigned int) st_other); | |
2039 | } | |
252b5132 | 2040 | |
587ff49e | 2041 | fprintf (file, " %s", name); |
252b5132 RH |
2042 | } |
2043 | break; | |
2044 | } | |
2045 | } | |
252b5132 RH |
2046 | \f |
2047 | /* ELF .o/exec file reading */ | |
2048 | ||
c044fabd | 2049 | /* Create a new bfd section from an ELF section header. */ |
252b5132 | 2050 | |
b34976b6 | 2051 | bfd_boolean |
217aa764 | 2052 | bfd_section_from_shdr (bfd *abfd, unsigned int shindex) |
252b5132 | 2053 | { |
4fbb74a6 AM |
2054 | Elf_Internal_Shdr *hdr; |
2055 | Elf_Internal_Ehdr *ehdr; | |
2056 | const struct elf_backend_data *bed; | |
90937f86 | 2057 | const char *name; |
bf67003b NC |
2058 | bfd_boolean ret = TRUE; |
2059 | static bfd_boolean * sections_being_created = NULL; | |
5a4b0ccc | 2060 | static bfd * sections_being_created_abfd = NULL; |
bf67003b | 2061 | static unsigned int nesting = 0; |
252b5132 | 2062 | |
4fbb74a6 AM |
2063 | if (shindex >= elf_numsections (abfd)) |
2064 | return FALSE; | |
2065 | ||
bf67003b NC |
2066 | if (++ nesting > 3) |
2067 | { | |
2068 | /* PR17512: A corrupt ELF binary might contain a recursive group of | |
67ce483b | 2069 | sections, with each the string indices pointing to the next in the |
bf67003b NC |
2070 | loop. Detect this here, by refusing to load a section that we are |
2071 | already in the process of loading. We only trigger this test if | |
2072 | we have nested at least three sections deep as normal ELF binaries | |
5a4b0ccc NC |
2073 | can expect to recurse at least once. |
2074 | ||
2075 | FIXME: It would be better if this array was attached to the bfd, | |
2076 | rather than being held in a static pointer. */ | |
2077 | ||
2078 | if (sections_being_created_abfd != abfd) | |
2079 | sections_being_created = NULL; | |
bf67003b NC |
2080 | if (sections_being_created == NULL) |
2081 | { | |
bf67003b | 2082 | sections_being_created = (bfd_boolean *) |
7a6e0d89 | 2083 | bfd_zalloc2 (abfd, elf_numsections (abfd), sizeof (bfd_boolean)); |
5a4b0ccc | 2084 | sections_being_created_abfd = abfd; |
bf67003b NC |
2085 | } |
2086 | if (sections_being_created [shindex]) | |
2087 | { | |
4eca0228 | 2088 | _bfd_error_handler |
871b3ab2 | 2089 | (_("%pB: warning: loop in section dependencies detected"), abfd); |
bf67003b NC |
2090 | return FALSE; |
2091 | } | |
2092 | sections_being_created [shindex] = TRUE; | |
2093 | } | |
2094 | ||
4fbb74a6 AM |
2095 | hdr = elf_elfsections (abfd)[shindex]; |
2096 | ehdr = elf_elfheader (abfd); | |
2097 | name = bfd_elf_string_from_elf_section (abfd, ehdr->e_shstrndx, | |
1b3a8575 | 2098 | hdr->sh_name); |
933d961a | 2099 | if (name == NULL) |
bf67003b | 2100 | goto fail; |
252b5132 | 2101 | |
4fbb74a6 | 2102 | bed = get_elf_backend_data (abfd); |
252b5132 RH |
2103 | switch (hdr->sh_type) |
2104 | { | |
2105 | case SHT_NULL: | |
2106 | /* Inactive section. Throw it away. */ | |
bf67003b | 2107 | goto success; |
252b5132 | 2108 | |
bf67003b NC |
2109 | case SHT_PROGBITS: /* Normal section with contents. */ |
2110 | case SHT_NOBITS: /* .bss section. */ | |
2111 | case SHT_HASH: /* .hash section. */ | |
2112 | case SHT_NOTE: /* .note section. */ | |
25e27870 L |
2113 | case SHT_INIT_ARRAY: /* .init_array section. */ |
2114 | case SHT_FINI_ARRAY: /* .fini_array section. */ | |
2115 | case SHT_PREINIT_ARRAY: /* .preinit_array section. */ | |
7f1204bb | 2116 | case SHT_GNU_LIBLIST: /* .gnu.liblist section. */ |
fdc90cb4 | 2117 | case SHT_GNU_HASH: /* .gnu.hash section. */ |
bf67003b NC |
2118 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); |
2119 | goto success; | |
252b5132 | 2120 | |
797fc050 | 2121 | case SHT_DYNAMIC: /* Dynamic linking information. */ |
6dc132d9 | 2122 | if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex)) |
bf67003b NC |
2123 | goto fail; |
2124 | ||
cfcac11d NC |
2125 | if (hdr->sh_link > elf_numsections (abfd)) |
2126 | { | |
caa83f8b | 2127 | /* PR 10478: Accept Solaris binaries with a sh_link |
cfcac11d NC |
2128 | field set to SHN_BEFORE or SHN_AFTER. */ |
2129 | switch (bfd_get_arch (abfd)) | |
2130 | { | |
caa83f8b | 2131 | case bfd_arch_i386: |
cfcac11d NC |
2132 | case bfd_arch_sparc: |
2133 | if (hdr->sh_link == (SHN_LORESERVE & 0xffff) /* SHN_BEFORE */ | |
2134 | || hdr->sh_link == ((SHN_LORESERVE + 1) & 0xffff) /* SHN_AFTER */) | |
2135 | break; | |
2136 | /* Otherwise fall through. */ | |
2137 | default: | |
bf67003b | 2138 | goto fail; |
cfcac11d NC |
2139 | } |
2140 | } | |
2141 | else if (elf_elfsections (abfd)[hdr->sh_link] == NULL) | |
bf67003b | 2142 | goto fail; |
cfcac11d | 2143 | else if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_STRTAB) |
797fc050 AM |
2144 | { |
2145 | Elf_Internal_Shdr *dynsymhdr; | |
2146 | ||
2147 | /* The shared libraries distributed with hpux11 have a bogus | |
2148 | sh_link field for the ".dynamic" section. Find the | |
2149 | string table for the ".dynsym" section instead. */ | |
2150 | if (elf_dynsymtab (abfd) != 0) | |
2151 | { | |
2152 | dynsymhdr = elf_elfsections (abfd)[elf_dynsymtab (abfd)]; | |
2153 | hdr->sh_link = dynsymhdr->sh_link; | |
2154 | } | |
2155 | else | |
2156 | { | |
2157 | unsigned int i, num_sec; | |
2158 | ||
2159 | num_sec = elf_numsections (abfd); | |
2160 | for (i = 1; i < num_sec; i++) | |
2161 | { | |
2162 | dynsymhdr = elf_elfsections (abfd)[i]; | |
2163 | if (dynsymhdr->sh_type == SHT_DYNSYM) | |
2164 | { | |
2165 | hdr->sh_link = dynsymhdr->sh_link; | |
2166 | break; | |
2167 | } | |
2168 | } | |
2169 | } | |
2170 | } | |
bf67003b | 2171 | goto success; |
797fc050 | 2172 | |
bf67003b | 2173 | case SHT_SYMTAB: /* A symbol table. */ |
252b5132 | 2174 | if (elf_onesymtab (abfd) == shindex) |
bf67003b | 2175 | goto success; |
252b5132 | 2176 | |
a50b2160 | 2177 | if (hdr->sh_entsize != bed->s->sizeof_sym) |
bf67003b NC |
2178 | goto fail; |
2179 | ||
3337c1e5 | 2180 | if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size) |
eee3b786 AM |
2181 | { |
2182 | if (hdr->sh_size != 0) | |
bf67003b | 2183 | goto fail; |
eee3b786 AM |
2184 | /* Some assemblers erroneously set sh_info to one with a |
2185 | zero sh_size. ld sees this as a global symbol count | |
2186 | of (unsigned) -1. Fix it here. */ | |
2187 | hdr->sh_info = 0; | |
bf67003b | 2188 | goto success; |
eee3b786 | 2189 | } |
bf67003b | 2190 | |
16ad13ec NC |
2191 | /* PR 18854: A binary might contain more than one symbol table. |
2192 | Unusual, but possible. Warn, but continue. */ | |
2193 | if (elf_onesymtab (abfd) != 0) | |
2194 | { | |
4eca0228 | 2195 | _bfd_error_handler |
695344c0 | 2196 | /* xgettext:c-format */ |
871b3ab2 | 2197 | (_("%pB: warning: multiple symbol tables detected" |
63a5468a | 2198 | " - ignoring the table in section %u"), |
16ad13ec NC |
2199 | abfd, shindex); |
2200 | goto success; | |
2201 | } | |
252b5132 | 2202 | elf_onesymtab (abfd) = shindex; |
6a40cf0c NC |
2203 | elf_symtab_hdr (abfd) = *hdr; |
2204 | elf_elfsections (abfd)[shindex] = hdr = & elf_symtab_hdr (abfd); | |
252b5132 RH |
2205 | abfd->flags |= HAS_SYMS; |
2206 | ||
2207 | /* Sometimes a shared object will map in the symbol table. If | |
08a40648 AM |
2208 | SHF_ALLOC is set, and this is a shared object, then we also |
2209 | treat this section as a BFD section. We can not base the | |
2210 | decision purely on SHF_ALLOC, because that flag is sometimes | |
2211 | set in a relocatable object file, which would confuse the | |
2212 | linker. */ | |
252b5132 RH |
2213 | if ((hdr->sh_flags & SHF_ALLOC) != 0 |
2214 | && (abfd->flags & DYNAMIC) != 0 | |
6dc132d9 L |
2215 | && ! _bfd_elf_make_section_from_shdr (abfd, hdr, name, |
2216 | shindex)) | |
bf67003b | 2217 | goto fail; |
252b5132 | 2218 | |
1b3a8575 AM |
2219 | /* Go looking for SHT_SYMTAB_SHNDX too, since if there is one we |
2220 | can't read symbols without that section loaded as well. It | |
2221 | is most likely specified by the next section header. */ | |
6a40cf0c NC |
2222 | { |
2223 | elf_section_list * entry; | |
2224 | unsigned int i, num_sec; | |
1b3a8575 | 2225 | |
6a40cf0c NC |
2226 | for (entry = elf_symtab_shndx_list (abfd); entry != NULL; entry = entry->next) |
2227 | if (entry->hdr.sh_link == shindex) | |
2228 | goto success; | |
2229 | ||
2230 | num_sec = elf_numsections (abfd); | |
2231 | for (i = shindex + 1; i < num_sec; i++) | |
2232 | { | |
2233 | Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i]; | |
2234 | ||
2235 | if (hdr2->sh_type == SHT_SYMTAB_SHNDX | |
2236 | && hdr2->sh_link == shindex) | |
2237 | break; | |
2238 | } | |
2239 | ||
2240 | if (i == num_sec) | |
2241 | for (i = 1; i < shindex; i++) | |
1b3a8575 AM |
2242 | { |
2243 | Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i]; | |
6a40cf0c | 2244 | |
1b3a8575 AM |
2245 | if (hdr2->sh_type == SHT_SYMTAB_SHNDX |
2246 | && hdr2->sh_link == shindex) | |
2247 | break; | |
2248 | } | |
6a40cf0c NC |
2249 | |
2250 | if (i != shindex) | |
2251 | ret = bfd_section_from_shdr (abfd, i); | |
2252 | /* else FIXME: we have failed to find the symbol table - should we issue an error ? */ | |
2253 | goto success; | |
2254 | } | |
252b5132 | 2255 | |
bf67003b | 2256 | case SHT_DYNSYM: /* A dynamic symbol table. */ |
252b5132 | 2257 | if (elf_dynsymtab (abfd) == shindex) |
bf67003b | 2258 | goto success; |
252b5132 | 2259 | |
a50b2160 | 2260 | if (hdr->sh_entsize != bed->s->sizeof_sym) |
bf67003b NC |
2261 | goto fail; |
2262 | ||
eee3b786 AM |
2263 | if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size) |
2264 | { | |
2265 | if (hdr->sh_size != 0) | |
bf67003b NC |
2266 | goto fail; |
2267 | ||
eee3b786 AM |
2268 | /* Some linkers erroneously set sh_info to one with a |
2269 | zero sh_size. ld sees this as a global symbol count | |
2270 | of (unsigned) -1. Fix it here. */ | |
2271 | hdr->sh_info = 0; | |
bf67003b | 2272 | goto success; |
eee3b786 | 2273 | } |
bf67003b | 2274 | |
16ad13ec NC |
2275 | /* PR 18854: A binary might contain more than one dynamic symbol table. |
2276 | Unusual, but possible. Warn, but continue. */ | |
2277 | if (elf_dynsymtab (abfd) != 0) | |
2278 | { | |
4eca0228 | 2279 | _bfd_error_handler |
695344c0 | 2280 | /* xgettext:c-format */ |
871b3ab2 | 2281 | (_("%pB: warning: multiple dynamic symbol tables detected" |
63a5468a | 2282 | " - ignoring the table in section %u"), |
16ad13ec NC |
2283 | abfd, shindex); |
2284 | goto success; | |
2285 | } | |
252b5132 RH |
2286 | elf_dynsymtab (abfd) = shindex; |
2287 | elf_tdata (abfd)->dynsymtab_hdr = *hdr; | |
2288 | elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->dynsymtab_hdr; | |
2289 | abfd->flags |= HAS_SYMS; | |
2290 | ||
2291 | /* Besides being a symbol table, we also treat this as a regular | |
2292 | section, so that objcopy can handle it. */ | |
bf67003b NC |
2293 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); |
2294 | goto success; | |
252b5132 | 2295 | |
bf67003b | 2296 | case SHT_SYMTAB_SHNDX: /* Symbol section indices when >64k sections. */ |
6a40cf0c NC |
2297 | { |
2298 | elf_section_list * entry; | |
9ad5cbcf | 2299 | |
6a40cf0c NC |
2300 | for (entry = elf_symtab_shndx_list (abfd); entry != NULL; entry = entry->next) |
2301 | if (entry->ndx == shindex) | |
2302 | goto success; | |
07d6d2b8 | 2303 | |
7a6e0d89 | 2304 | entry = bfd_alloc (abfd, sizeof (*entry)); |
6a40cf0c NC |
2305 | if (entry == NULL) |
2306 | goto fail; | |
2307 | entry->ndx = shindex; | |
2308 | entry->hdr = * hdr; | |
2309 | entry->next = elf_symtab_shndx_list (abfd); | |
2310 | elf_symtab_shndx_list (abfd) = entry; | |
2311 | elf_elfsections (abfd)[shindex] = & entry->hdr; | |
2312 | goto success; | |
2313 | } | |
9ad5cbcf | 2314 | |
bf67003b | 2315 | case SHT_STRTAB: /* A string table. */ |
252b5132 | 2316 | if (hdr->bfd_section != NULL) |
bf67003b NC |
2317 | goto success; |
2318 | ||
252b5132 RH |
2319 | if (ehdr->e_shstrndx == shindex) |
2320 | { | |
2321 | elf_tdata (abfd)->shstrtab_hdr = *hdr; | |
2322 | elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->shstrtab_hdr; | |
bf67003b | 2323 | goto success; |
252b5132 | 2324 | } |
bf67003b | 2325 | |
1b3a8575 AM |
2326 | if (elf_elfsections (abfd)[elf_onesymtab (abfd)]->sh_link == shindex) |
2327 | { | |
2328 | symtab_strtab: | |
2329 | elf_tdata (abfd)->strtab_hdr = *hdr; | |
2330 | elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->strtab_hdr; | |
bf67003b | 2331 | goto success; |
1b3a8575 | 2332 | } |
bf67003b | 2333 | |
1b3a8575 AM |
2334 | if (elf_elfsections (abfd)[elf_dynsymtab (abfd)]->sh_link == shindex) |
2335 | { | |
2336 | dynsymtab_strtab: | |
2337 | elf_tdata (abfd)->dynstrtab_hdr = *hdr; | |
2338 | hdr = &elf_tdata (abfd)->dynstrtab_hdr; | |
2339 | elf_elfsections (abfd)[shindex] = hdr; | |
2340 | /* We also treat this as a regular section, so that objcopy | |
2341 | can handle it. */ | |
bf67003b NC |
2342 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, |
2343 | shindex); | |
2344 | goto success; | |
1b3a8575 | 2345 | } |
252b5132 | 2346 | |
1b3a8575 AM |
2347 | /* If the string table isn't one of the above, then treat it as a |
2348 | regular section. We need to scan all the headers to be sure, | |
2349 | just in case this strtab section appeared before the above. */ | |
2350 | if (elf_onesymtab (abfd) == 0 || elf_dynsymtab (abfd) == 0) | |
2351 | { | |
2352 | unsigned int i, num_sec; | |
252b5132 | 2353 | |
1b3a8575 AM |
2354 | num_sec = elf_numsections (abfd); |
2355 | for (i = 1; i < num_sec; i++) | |
2356 | { | |
2357 | Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i]; | |
2358 | if (hdr2->sh_link == shindex) | |
2359 | { | |
933d961a JJ |
2360 | /* Prevent endless recursion on broken objects. */ |
2361 | if (i == shindex) | |
bf67003b | 2362 | goto fail; |
1b3a8575 | 2363 | if (! bfd_section_from_shdr (abfd, i)) |
bf67003b | 2364 | goto fail; |
1b3a8575 AM |
2365 | if (elf_onesymtab (abfd) == i) |
2366 | goto symtab_strtab; | |
2367 | if (elf_dynsymtab (abfd) == i) | |
2368 | goto dynsymtab_strtab; | |
2369 | } | |
2370 | } | |
2371 | } | |
bf67003b NC |
2372 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); |
2373 | goto success; | |
252b5132 RH |
2374 | |
2375 | case SHT_REL: | |
2376 | case SHT_RELA: | |
2377 | /* *These* do a lot of work -- but build no sections! */ | |
2378 | { | |
2379 | asection *target_sect; | |
d4730f92 | 2380 | Elf_Internal_Shdr *hdr2, **p_hdr; |
9ad5cbcf | 2381 | unsigned int num_sec = elf_numsections (abfd); |
d4730f92 | 2382 | struct bfd_elf_section_data *esdt; |
252b5132 | 2383 | |
aa2ca951 JJ |
2384 | if (hdr->sh_entsize |
2385 | != (bfd_size_type) (hdr->sh_type == SHT_REL | |
a50b2160 | 2386 | ? bed->s->sizeof_rel : bed->s->sizeof_rela)) |
bf67003b | 2387 | goto fail; |
a50b2160 | 2388 | |
03ae5f59 | 2389 | /* Check for a bogus link to avoid crashing. */ |
4fbb74a6 | 2390 | if (hdr->sh_link >= num_sec) |
03ae5f59 | 2391 | { |
4eca0228 | 2392 | _bfd_error_handler |
695344c0 | 2393 | /* xgettext:c-format */ |
871b3ab2 | 2394 | (_("%pB: invalid link %u for reloc section %s (index %u)"), |
4eca0228 | 2395 | abfd, hdr->sh_link, name, shindex); |
bf67003b NC |
2396 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, |
2397 | shindex); | |
2398 | goto success; | |
03ae5f59 ILT |
2399 | } |
2400 | ||
252b5132 RH |
2401 | /* For some incomprehensible reason Oracle distributes |
2402 | libraries for Solaris in which some of the objects have | |
2403 | bogus sh_link fields. It would be nice if we could just | |
2404 | reject them, but, unfortunately, some people need to use | |
2405 | them. We scan through the section headers; if we find only | |
2406 | one suitable symbol table, we clobber the sh_link to point | |
83b89087 L |
2407 | to it. I hope this doesn't break anything. |
2408 | ||
2409 | Don't do it on executable nor shared library. */ | |
2410 | if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0 | |
2411 | && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_SYMTAB | |
252b5132 RH |
2412 | && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_DYNSYM) |
2413 | { | |
9ad5cbcf | 2414 | unsigned int scan; |
252b5132 RH |
2415 | int found; |
2416 | ||
2417 | found = 0; | |
9ad5cbcf | 2418 | for (scan = 1; scan < num_sec; scan++) |
252b5132 RH |
2419 | { |
2420 | if (elf_elfsections (abfd)[scan]->sh_type == SHT_SYMTAB | |
2421 | || elf_elfsections (abfd)[scan]->sh_type == SHT_DYNSYM) | |
2422 | { | |
2423 | if (found != 0) | |
2424 | { | |
2425 | found = 0; | |
2426 | break; | |
2427 | } | |
2428 | found = scan; | |
2429 | } | |
2430 | } | |
2431 | if (found != 0) | |
2432 | hdr->sh_link = found; | |
2433 | } | |
2434 | ||
2435 | /* Get the symbol table. */ | |
1b3a8575 AM |
2436 | if ((elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_SYMTAB |
2437 | || elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_DYNSYM) | |
252b5132 | 2438 | && ! bfd_section_from_shdr (abfd, hdr->sh_link)) |
bf67003b | 2439 | goto fail; |
252b5132 | 2440 | |
a4bcd733 AM |
2441 | /* If this is an alloc section in an executable or shared |
2442 | library, or the reloc section does not use the main symbol | |
2443 | table we don't treat it as a reloc section. BFD can't | |
2444 | adequately represent such a section, so at least for now, | |
2445 | we don't try. We just present it as a normal section. We | |
2446 | also can't use it as a reloc section if it points to the | |
2447 | null section, an invalid section, another reloc section, or | |
2448 | its sh_link points to the null section. */ | |
2449 | if (((abfd->flags & (DYNAMIC | EXEC_P)) != 0 | |
2450 | && (hdr->sh_flags & SHF_ALLOC) != 0) | |
83b89087 | 2451 | || hdr->sh_link == SHN_UNDEF |
a4bcd733 | 2452 | || hdr->sh_link != elf_onesymtab (abfd) |
185ef66d | 2453 | || hdr->sh_info == SHN_UNDEF |
185ef66d AM |
2454 | || hdr->sh_info >= num_sec |
2455 | || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_REL | |
2456 | || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_RELA) | |
bf67003b NC |
2457 | { |
2458 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, | |
2459 | shindex); | |
2460 | goto success; | |
2461 | } | |
252b5132 RH |
2462 | |
2463 | if (! bfd_section_from_shdr (abfd, hdr->sh_info)) | |
bf67003b NC |
2464 | goto fail; |
2465 | ||
252b5132 RH |
2466 | target_sect = bfd_section_from_elf_index (abfd, hdr->sh_info); |
2467 | if (target_sect == NULL) | |
bf67003b | 2468 | goto fail; |
252b5132 | 2469 | |
d4730f92 BS |
2470 | esdt = elf_section_data (target_sect); |
2471 | if (hdr->sh_type == SHT_RELA) | |
2472 | p_hdr = &esdt->rela.hdr; | |
252b5132 | 2473 | else |
d4730f92 BS |
2474 | p_hdr = &esdt->rel.hdr; |
2475 | ||
a7ba3896 NC |
2476 | /* PR 17512: file: 0b4f81b7. |
2477 | Also see PR 24456, for a file which deliberately has two reloc | |
2478 | sections. */ | |
06614111 | 2479 | if (*p_hdr != NULL) |
a7ba3896 NC |
2480 | { |
2481 | _bfd_error_handler | |
2482 | /* xgettext:c-format */ | |
2483 | (_("%pB: warning: multiple relocation sections for section %pA \ | |
2484 | found - ignoring all but the first"), | |
2485 | abfd, target_sect); | |
2486 | goto success; | |
2487 | } | |
ef53be89 | 2488 | hdr2 = (Elf_Internal_Shdr *) bfd_alloc (abfd, sizeof (*hdr2)); |
d4730f92 | 2489 | if (hdr2 == NULL) |
bf67003b | 2490 | goto fail; |
252b5132 | 2491 | *hdr2 = *hdr; |
d4730f92 | 2492 | *p_hdr = hdr2; |
252b5132 | 2493 | elf_elfsections (abfd)[shindex] = hdr2; |
056bafd4 MR |
2494 | target_sect->reloc_count += (NUM_SHDR_ENTRIES (hdr) |
2495 | * bed->s->int_rels_per_ext_rel); | |
252b5132 RH |
2496 | target_sect->flags |= SEC_RELOC; |
2497 | target_sect->relocation = NULL; | |
2498 | target_sect->rel_filepos = hdr->sh_offset; | |
bf572ba0 MM |
2499 | /* In the section to which the relocations apply, mark whether |
2500 | its relocations are of the REL or RELA variety. */ | |
72730e0c | 2501 | if (hdr->sh_size != 0) |
d4730f92 BS |
2502 | { |
2503 | if (hdr->sh_type == SHT_RELA) | |
2504 | target_sect->use_rela_p = 1; | |
2505 | } | |
252b5132 | 2506 | abfd->flags |= HAS_RELOC; |
bf67003b | 2507 | goto success; |
252b5132 | 2508 | } |
252b5132 RH |
2509 | |
2510 | case SHT_GNU_verdef: | |
2511 | elf_dynverdef (abfd) = shindex; | |
2512 | elf_tdata (abfd)->dynverdef_hdr = *hdr; | |
bf67003b NC |
2513 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); |
2514 | goto success; | |
252b5132 RH |
2515 | |
2516 | case SHT_GNU_versym: | |
a50b2160 | 2517 | if (hdr->sh_entsize != sizeof (Elf_External_Versym)) |
bf67003b NC |
2518 | goto fail; |
2519 | ||
252b5132 RH |
2520 | elf_dynversym (abfd) = shindex; |
2521 | elf_tdata (abfd)->dynversym_hdr = *hdr; | |
bf67003b NC |
2522 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); |
2523 | goto success; | |
252b5132 RH |
2524 | |
2525 | case SHT_GNU_verneed: | |
2526 | elf_dynverref (abfd) = shindex; | |
2527 | elf_tdata (abfd)->dynverref_hdr = *hdr; | |
bf67003b NC |
2528 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); |
2529 | goto success; | |
252b5132 RH |
2530 | |
2531 | case SHT_SHLIB: | |
bf67003b | 2532 | goto success; |
252b5132 | 2533 | |
dbb410c3 | 2534 | case SHT_GROUP: |
44534af3 | 2535 | if (! IS_VALID_GROUP_SECTION_HEADER (hdr, GRP_ENTRY_SIZE)) |
bf67003b NC |
2536 | goto fail; |
2537 | ||
6dc132d9 | 2538 | if (!_bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex)) |
bf67003b NC |
2539 | goto fail; |
2540 | ||
bf67003b | 2541 | goto success; |
dbb410c3 | 2542 | |
252b5132 | 2543 | default: |
104d59d1 JM |
2544 | /* Possibly an attributes section. */ |
2545 | if (hdr->sh_type == SHT_GNU_ATTRIBUTES | |
2546 | || hdr->sh_type == bed->obj_attrs_section_type) | |
2547 | { | |
2548 | if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex)) | |
bf67003b | 2549 | goto fail; |
104d59d1 | 2550 | _bfd_elf_parse_attributes (abfd, hdr); |
bf67003b | 2551 | goto success; |
104d59d1 JM |
2552 | } |
2553 | ||
252b5132 | 2554 | /* Check for any processor-specific section types. */ |
3eb70a79 | 2555 | if (bed->elf_backend_section_from_shdr (abfd, hdr, name, shindex)) |
bf67003b | 2556 | goto success; |
3eb70a79 L |
2557 | |
2558 | if (hdr->sh_type >= SHT_LOUSER && hdr->sh_type <= SHT_HIUSER) | |
2559 | { | |
2560 | if ((hdr->sh_flags & SHF_ALLOC) != 0) | |
2561 | /* FIXME: How to properly handle allocated section reserved | |
2562 | for applications? */ | |
4eca0228 | 2563 | _bfd_error_handler |
695344c0 | 2564 | /* xgettext:c-format */ |
871b3ab2 | 2565 | (_("%pB: unknown type [%#x] section `%s'"), |
76cfced5 | 2566 | abfd, hdr->sh_type, name); |
3eb70a79 | 2567 | else |
bf67003b NC |
2568 | { |
2569 | /* Allow sections reserved for applications. */ | |
2570 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, | |
2571 | shindex); | |
2572 | goto success; | |
2573 | } | |
3eb70a79 L |
2574 | } |
2575 | else if (hdr->sh_type >= SHT_LOPROC | |
2576 | && hdr->sh_type <= SHT_HIPROC) | |
2577 | /* FIXME: We should handle this section. */ | |
4eca0228 | 2578 | _bfd_error_handler |
695344c0 | 2579 | /* xgettext:c-format */ |
871b3ab2 | 2580 | (_("%pB: unknown type [%#x] section `%s'"), |
76cfced5 | 2581 | abfd, hdr->sh_type, name); |
3eb70a79 | 2582 | else if (hdr->sh_type >= SHT_LOOS && hdr->sh_type <= SHT_HIOS) |
ff15b240 NC |
2583 | { |
2584 | /* Unrecognised OS-specific sections. */ | |
2585 | if ((hdr->sh_flags & SHF_OS_NONCONFORMING) != 0) | |
2586 | /* SHF_OS_NONCONFORMING indicates that special knowledge is | |
08a40648 | 2587 | required to correctly process the section and the file should |
ff15b240 | 2588 | be rejected with an error message. */ |
4eca0228 | 2589 | _bfd_error_handler |
695344c0 | 2590 | /* xgettext:c-format */ |
871b3ab2 | 2591 | (_("%pB: unknown type [%#x] section `%s'"), |
76cfced5 | 2592 | abfd, hdr->sh_type, name); |
ff15b240 | 2593 | else |
bf67003b NC |
2594 | { |
2595 | /* Otherwise it should be processed. */ | |
2596 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); | |
2597 | goto success; | |
2598 | } | |
ff15b240 | 2599 | } |
3eb70a79 L |
2600 | else |
2601 | /* FIXME: We should handle this section. */ | |
4eca0228 | 2602 | _bfd_error_handler |
695344c0 | 2603 | /* xgettext:c-format */ |
871b3ab2 | 2604 | (_("%pB: unknown type [%#x] section `%s'"), |
76cfced5 | 2605 | abfd, hdr->sh_type, name); |
3eb70a79 | 2606 | |
bf67003b | 2607 | goto fail; |
252b5132 RH |
2608 | } |
2609 | ||
bf67003b NC |
2610 | fail: |
2611 | ret = FALSE; | |
2612 | success: | |
e5b470e2 | 2613 | if (sections_being_created && sections_being_created_abfd == abfd) |
bf67003b NC |
2614 | sections_being_created [shindex] = FALSE; |
2615 | if (-- nesting == 0) | |
5a4b0ccc NC |
2616 | { |
2617 | sections_being_created = NULL; | |
2618 | sections_being_created_abfd = abfd; | |
2619 | } | |
bf67003b | 2620 | return ret; |
252b5132 RH |
2621 | } |
2622 | ||
87d72d41 | 2623 | /* Return the local symbol specified by ABFD, R_SYMNDX. */ |
ec338859 | 2624 | |
87d72d41 AM |
2625 | Elf_Internal_Sym * |
2626 | bfd_sym_from_r_symndx (struct sym_cache *cache, | |
2627 | bfd *abfd, | |
2628 | unsigned long r_symndx) | |
ec338859 | 2629 | { |
ec338859 AM |
2630 | unsigned int ent = r_symndx % LOCAL_SYM_CACHE_SIZE; |
2631 | ||
a5d1b3b5 AM |
2632 | if (cache->abfd != abfd || cache->indx[ent] != r_symndx) |
2633 | { | |
2634 | Elf_Internal_Shdr *symtab_hdr; | |
2635 | unsigned char esym[sizeof (Elf64_External_Sym)]; | |
2636 | Elf_External_Sym_Shndx eshndx; | |
ec338859 | 2637 | |
a5d1b3b5 AM |
2638 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; |
2639 | if (bfd_elf_get_elf_syms (abfd, symtab_hdr, 1, r_symndx, | |
87d72d41 | 2640 | &cache->sym[ent], esym, &eshndx) == NULL) |
a5d1b3b5 | 2641 | return NULL; |
9ad5cbcf | 2642 | |
a5d1b3b5 AM |
2643 | if (cache->abfd != abfd) |
2644 | { | |
2645 | memset (cache->indx, -1, sizeof (cache->indx)); | |
2646 | cache->abfd = abfd; | |
2647 | } | |
2648 | cache->indx[ent] = r_symndx; | |
ec338859 | 2649 | } |
a5d1b3b5 | 2650 | |
87d72d41 | 2651 | return &cache->sym[ent]; |
ec338859 AM |
2652 | } |
2653 | ||
252b5132 RH |
2654 | /* Given an ELF section number, retrieve the corresponding BFD |
2655 | section. */ | |
2656 | ||
2657 | asection * | |
91d6fa6a | 2658 | bfd_section_from_elf_index (bfd *abfd, unsigned int sec_index) |
252b5132 | 2659 | { |
91d6fa6a | 2660 | if (sec_index >= elf_numsections (abfd)) |
252b5132 | 2661 | return NULL; |
91d6fa6a | 2662 | return elf_elfsections (abfd)[sec_index]->bfd_section; |
252b5132 RH |
2663 | } |
2664 | ||
b35d266b | 2665 | static const struct bfd_elf_special_section special_sections_b[] = |
2f89ff8d | 2666 | { |
0112cd26 | 2667 | { STRING_COMMA_LEN (".bss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE }, |
07d6d2b8 | 2668 | { NULL, 0, 0, 0, 0 } |
7f4d3958 L |
2669 | }; |
2670 | ||
b35d266b | 2671 | static const struct bfd_elf_special_section special_sections_c[] = |
7f4d3958 | 2672 | { |
0112cd26 | 2673 | { STRING_COMMA_LEN (".comment"), 0, SHT_PROGBITS, 0 }, |
07d6d2b8 | 2674 | { NULL, 0, 0, 0, 0 } |
7f4d3958 L |
2675 | }; |
2676 | ||
b35d266b | 2677 | static const struct bfd_elf_special_section special_sections_d[] = |
7f4d3958 | 2678 | { |
07d6d2b8 AM |
2679 | { STRING_COMMA_LEN (".data"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE }, |
2680 | { STRING_COMMA_LEN (".data1"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE }, | |
a9a72a65 DE |
2681 | /* There are more DWARF sections than these, but they needn't be added here |
2682 | unless you have to cope with broken compilers that don't emit section | |
2683 | attributes or you want to help the user writing assembler. */ | |
07d6d2b8 AM |
2684 | { STRING_COMMA_LEN (".debug"), 0, SHT_PROGBITS, 0 }, |
2685 | { STRING_COMMA_LEN (".debug_line"), 0, SHT_PROGBITS, 0 }, | |
2686 | { STRING_COMMA_LEN (".debug_info"), 0, SHT_PROGBITS, 0 }, | |
2687 | { STRING_COMMA_LEN (".debug_abbrev"), 0, SHT_PROGBITS, 0 }, | |
0112cd26 | 2688 | { STRING_COMMA_LEN (".debug_aranges"), 0, SHT_PROGBITS, 0 }, |
07d6d2b8 AM |
2689 | { STRING_COMMA_LEN (".dynamic"), 0, SHT_DYNAMIC, SHF_ALLOC }, |
2690 | { STRING_COMMA_LEN (".dynstr"), 0, SHT_STRTAB, SHF_ALLOC }, | |
2691 | { STRING_COMMA_LEN (".dynsym"), 0, SHT_DYNSYM, SHF_ALLOC }, | |
2692 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2693 | }; |
2694 | ||
b35d266b | 2695 | static const struct bfd_elf_special_section special_sections_f[] = |
7f4d3958 | 2696 | { |
07d6d2b8 | 2697 | { STRING_COMMA_LEN (".fini"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR }, |
6f9dbcd4 | 2698 | { STRING_COMMA_LEN (".fini_array"), -2, SHT_FINI_ARRAY, SHF_ALLOC + SHF_WRITE }, |
07d6d2b8 | 2699 | { NULL, 0 , 0, 0, 0 } |
7f4d3958 L |
2700 | }; |
2701 | ||
b35d266b | 2702 | static const struct bfd_elf_special_section special_sections_g[] = |
7f4d3958 | 2703 | { |
0112cd26 | 2704 | { STRING_COMMA_LEN (".gnu.linkonce.b"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE }, |
07d6d2b8 AM |
2705 | { STRING_COMMA_LEN (".gnu.lto_"), -1, SHT_PROGBITS, SHF_EXCLUDE }, |
2706 | { STRING_COMMA_LEN (".got"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE }, | |
2707 | { STRING_COMMA_LEN (".gnu.version"), 0, SHT_GNU_versym, 0 }, | |
0112cd26 NC |
2708 | { STRING_COMMA_LEN (".gnu.version_d"), 0, SHT_GNU_verdef, 0 }, |
2709 | { STRING_COMMA_LEN (".gnu.version_r"), 0, SHT_GNU_verneed, 0 }, | |
07d6d2b8 AM |
2710 | { STRING_COMMA_LEN (".gnu.liblist"), 0, SHT_GNU_LIBLIST, SHF_ALLOC }, |
2711 | { STRING_COMMA_LEN (".gnu.conflict"), 0, SHT_RELA, SHF_ALLOC }, | |
2712 | { STRING_COMMA_LEN (".gnu.hash"), 0, SHT_GNU_HASH, SHF_ALLOC }, | |
2713 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2714 | }; |
2715 | ||
b35d266b | 2716 | static const struct bfd_elf_special_section special_sections_h[] = |
7f4d3958 | 2717 | { |
07d6d2b8 AM |
2718 | { STRING_COMMA_LEN (".hash"), 0, SHT_HASH, SHF_ALLOC }, |
2719 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2720 | }; |
2721 | ||
b35d266b | 2722 | static const struct bfd_elf_special_section special_sections_i[] = |
7f4d3958 | 2723 | { |
07d6d2b8 | 2724 | { STRING_COMMA_LEN (".init"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR }, |
6f9dbcd4 | 2725 | { STRING_COMMA_LEN (".init_array"), -2, SHT_INIT_ARRAY, SHF_ALLOC + SHF_WRITE }, |
07d6d2b8 AM |
2726 | { STRING_COMMA_LEN (".interp"), 0, SHT_PROGBITS, 0 }, |
2727 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2728 | }; |
2729 | ||
b35d266b | 2730 | static const struct bfd_elf_special_section special_sections_l[] = |
7f4d3958 | 2731 | { |
0112cd26 | 2732 | { STRING_COMMA_LEN (".line"), 0, SHT_PROGBITS, 0 }, |
07d6d2b8 | 2733 | { NULL, 0, 0, 0, 0 } |
7f4d3958 L |
2734 | }; |
2735 | ||
b35d266b | 2736 | static const struct bfd_elf_special_section special_sections_n[] = |
7f4d3958 | 2737 | { |
0112cd26 | 2738 | { STRING_COMMA_LEN (".note.GNU-stack"), 0, SHT_PROGBITS, 0 }, |
07d6d2b8 AM |
2739 | { STRING_COMMA_LEN (".note"), -1, SHT_NOTE, 0 }, |
2740 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2741 | }; |
2742 | ||
b35d266b | 2743 | static const struct bfd_elf_special_section special_sections_p[] = |
7f4d3958 | 2744 | { |
6f9dbcd4 | 2745 | { STRING_COMMA_LEN (".preinit_array"), -2, SHT_PREINIT_ARRAY, SHF_ALLOC + SHF_WRITE }, |
07d6d2b8 AM |
2746 | { STRING_COMMA_LEN (".plt"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR }, |
2747 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2748 | }; |
2749 | ||
b35d266b | 2750 | static const struct bfd_elf_special_section special_sections_r[] = |
7f4d3958 | 2751 | { |
0112cd26 NC |
2752 | { STRING_COMMA_LEN (".rodata"), -2, SHT_PROGBITS, SHF_ALLOC }, |
2753 | { STRING_COMMA_LEN (".rodata1"), 0, SHT_PROGBITS, SHF_ALLOC }, | |
07d6d2b8 AM |
2754 | { STRING_COMMA_LEN (".rela"), -1, SHT_RELA, 0 }, |
2755 | { STRING_COMMA_LEN (".rel"), -1, SHT_REL, 0 }, | |
2756 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2757 | }; |
2758 | ||
b35d266b | 2759 | static const struct bfd_elf_special_section special_sections_s[] = |
7f4d3958 | 2760 | { |
0112cd26 NC |
2761 | { STRING_COMMA_LEN (".shstrtab"), 0, SHT_STRTAB, 0 }, |
2762 | { STRING_COMMA_LEN (".strtab"), 0, SHT_STRTAB, 0 }, | |
2763 | { STRING_COMMA_LEN (".symtab"), 0, SHT_SYMTAB, 0 }, | |
60ff4dc4 HPN |
2764 | /* See struct bfd_elf_special_section declaration for the semantics of |
2765 | this special case where .prefix_length != strlen (.prefix). */ | |
2766 | { ".stabstr", 5, 3, SHT_STRTAB, 0 }, | |
07d6d2b8 | 2767 | { NULL, 0, 0, 0, 0 } |
2f89ff8d L |
2768 | }; |
2769 | ||
b35d266b | 2770 | static const struct bfd_elf_special_section special_sections_t[] = |
7f4d3958 | 2771 | { |
07d6d2b8 AM |
2772 | { STRING_COMMA_LEN (".text"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR }, |
2773 | { STRING_COMMA_LEN (".tbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS }, | |
0112cd26 | 2774 | { STRING_COMMA_LEN (".tdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS }, |
07d6d2b8 | 2775 | { NULL, 0, 0, 0, 0 } |
7f4d3958 L |
2776 | }; |
2777 | ||
1b315056 CS |
2778 | static const struct bfd_elf_special_section special_sections_z[] = |
2779 | { | |
07d6d2b8 AM |
2780 | { STRING_COMMA_LEN (".zdebug_line"), 0, SHT_PROGBITS, 0 }, |
2781 | { STRING_COMMA_LEN (".zdebug_info"), 0, SHT_PROGBITS, 0 }, | |
1b315056 CS |
2782 | { STRING_COMMA_LEN (".zdebug_abbrev"), 0, SHT_PROGBITS, 0 }, |
2783 | { STRING_COMMA_LEN (".zdebug_aranges"), 0, SHT_PROGBITS, 0 }, | |
07d6d2b8 | 2784 | { NULL, 0, 0, 0, 0 } |
1b315056 CS |
2785 | }; |
2786 | ||
e4c93b56 | 2787 | static const struct bfd_elf_special_section * const special_sections[] = |
7f4d3958 | 2788 | { |
7f4d3958 | 2789 | special_sections_b, /* 'b' */ |
98ece1b3 | 2790 | special_sections_c, /* 'c' */ |
7f4d3958 L |
2791 | special_sections_d, /* 'd' */ |
2792 | NULL, /* 'e' */ | |
2793 | special_sections_f, /* 'f' */ | |
2794 | special_sections_g, /* 'g' */ | |
2795 | special_sections_h, /* 'h' */ | |
2796 | special_sections_i, /* 'i' */ | |
2797 | NULL, /* 'j' */ | |
2798 | NULL, /* 'k' */ | |
2799 | special_sections_l, /* 'l' */ | |
2800 | NULL, /* 'm' */ | |
2801 | special_sections_n, /* 'n' */ | |
2802 | NULL, /* 'o' */ | |
2803 | special_sections_p, /* 'p' */ | |
2804 | NULL, /* 'q' */ | |
2805 | special_sections_r, /* 'r' */ | |
2806 | special_sections_s, /* 's' */ | |
2807 | special_sections_t, /* 't' */ | |
1b315056 CS |
2808 | NULL, /* 'u' */ |
2809 | NULL, /* 'v' */ | |
2810 | NULL, /* 'w' */ | |
2811 | NULL, /* 'x' */ | |
2812 | NULL, /* 'y' */ | |
2813 | special_sections_z /* 'z' */ | |
7f4d3958 L |
2814 | }; |
2815 | ||
551b43fd AM |
2816 | const struct bfd_elf_special_section * |
2817 | _bfd_elf_get_special_section (const char *name, | |
2818 | const struct bfd_elf_special_section *spec, | |
2819 | unsigned int rela) | |
2f89ff8d L |
2820 | { |
2821 | int i; | |
7f4d3958 | 2822 | int len; |
7f4d3958 | 2823 | |
551b43fd | 2824 | len = strlen (name); |
7f4d3958 | 2825 | |
551b43fd | 2826 | for (i = 0; spec[i].prefix != NULL; i++) |
7dcb9820 AM |
2827 | { |
2828 | int suffix_len; | |
551b43fd | 2829 | int prefix_len = spec[i].prefix_length; |
7dcb9820 AM |
2830 | |
2831 | if (len < prefix_len) | |
2832 | continue; | |
551b43fd | 2833 | if (memcmp (name, spec[i].prefix, prefix_len) != 0) |
7dcb9820 AM |
2834 | continue; |
2835 | ||
551b43fd | 2836 | suffix_len = spec[i].suffix_length; |
7dcb9820 AM |
2837 | if (suffix_len <= 0) |
2838 | { | |
2839 | if (name[prefix_len] != 0) | |
2840 | { | |
2841 | if (suffix_len == 0) | |
2842 | continue; | |
2843 | if (name[prefix_len] != '.' | |
2844 | && (suffix_len == -2 | |
551b43fd | 2845 | || (rela && spec[i].type == SHT_REL))) |
7dcb9820 AM |
2846 | continue; |
2847 | } | |
2848 | } | |
2849 | else | |
2850 | { | |
2851 | if (len < prefix_len + suffix_len) | |
2852 | continue; | |
2853 | if (memcmp (name + len - suffix_len, | |
551b43fd | 2854 | spec[i].prefix + prefix_len, |
7dcb9820 AM |
2855 | suffix_len) != 0) |
2856 | continue; | |
2857 | } | |
551b43fd | 2858 | return &spec[i]; |
7dcb9820 | 2859 | } |
2f89ff8d L |
2860 | |
2861 | return NULL; | |
2862 | } | |
2863 | ||
7dcb9820 | 2864 | const struct bfd_elf_special_section * |
29ef7005 | 2865 | _bfd_elf_get_sec_type_attr (bfd *abfd, asection *sec) |
2f89ff8d | 2866 | { |
551b43fd AM |
2867 | int i; |
2868 | const struct bfd_elf_special_section *spec; | |
29ef7005 | 2869 | const struct elf_backend_data *bed; |
2f89ff8d L |
2870 | |
2871 | /* See if this is one of the special sections. */ | |
551b43fd AM |
2872 | if (sec->name == NULL) |
2873 | return NULL; | |
2f89ff8d | 2874 | |
29ef7005 L |
2875 | bed = get_elf_backend_data (abfd); |
2876 | spec = bed->special_sections; | |
2877 | if (spec) | |
2878 | { | |
2879 | spec = _bfd_elf_get_special_section (sec->name, | |
2880 | bed->special_sections, | |
2881 | sec->use_rela_p); | |
2882 | if (spec != NULL) | |
2883 | return spec; | |
2884 | } | |
2885 | ||
551b43fd AM |
2886 | if (sec->name[0] != '.') |
2887 | return NULL; | |
2f89ff8d | 2888 | |
551b43fd | 2889 | i = sec->name[1] - 'b'; |
1b315056 | 2890 | if (i < 0 || i > 'z' - 'b') |
551b43fd AM |
2891 | return NULL; |
2892 | ||
2893 | spec = special_sections[i]; | |
2f89ff8d | 2894 | |
551b43fd AM |
2895 | if (spec == NULL) |
2896 | return NULL; | |
2897 | ||
2898 | return _bfd_elf_get_special_section (sec->name, spec, sec->use_rela_p); | |
2f89ff8d L |
2899 | } |
2900 | ||
b34976b6 | 2901 | bfd_boolean |
217aa764 | 2902 | _bfd_elf_new_section_hook (bfd *abfd, asection *sec) |
252b5132 RH |
2903 | { |
2904 | struct bfd_elf_section_data *sdata; | |
551b43fd | 2905 | const struct elf_backend_data *bed; |
7dcb9820 | 2906 | const struct bfd_elf_special_section *ssect; |
252b5132 | 2907 | |
f0abc2a1 AM |
2908 | sdata = (struct bfd_elf_section_data *) sec->used_by_bfd; |
2909 | if (sdata == NULL) | |
2910 | { | |
a50b1753 | 2911 | sdata = (struct bfd_elf_section_data *) bfd_zalloc (abfd, |
07d6d2b8 | 2912 | sizeof (*sdata)); |
f0abc2a1 AM |
2913 | if (sdata == NULL) |
2914 | return FALSE; | |
217aa764 | 2915 | sec->used_by_bfd = sdata; |
f0abc2a1 | 2916 | } |
bf572ba0 | 2917 | |
551b43fd AM |
2918 | /* Indicate whether or not this section should use RELA relocations. */ |
2919 | bed = get_elf_backend_data (abfd); | |
2920 | sec->use_rela_p = bed->default_use_rela_p; | |
2921 | ||
e843e0f8 L |
2922 | /* When we read a file, we don't need to set ELF section type and |
2923 | flags. They will be overridden in _bfd_elf_make_section_from_shdr | |
2924 | anyway. We will set ELF section type and flags for all linker | |
2925 | created sections. If user specifies BFD section flags, we will | |
2926 | set ELF section type and flags based on BFD section flags in | |
02ecc8e9 L |
2927 | elf_fake_sections. Special handling for .init_array/.fini_array |
2928 | output sections since they may contain .ctors/.dtors input | |
2929 | sections. We don't want _bfd_elf_init_private_section_data to | |
2930 | copy ELF section type from .ctors/.dtors input sections. */ | |
2931 | if (abfd->direction != read_direction | |
3496cb2a | 2932 | || (sec->flags & SEC_LINKER_CREATED) != 0) |
2f89ff8d | 2933 | { |
551b43fd | 2934 | ssect = (*bed->get_sec_type_attr) (abfd, sec); |
02ecc8e9 L |
2935 | if (ssect != NULL |
2936 | && (!sec->flags | |
2937 | || (sec->flags & SEC_LINKER_CREATED) != 0 | |
2938 | || ssect->type == SHT_INIT_ARRAY | |
2939 | || ssect->type == SHT_FINI_ARRAY)) | |
a31501e9 L |
2940 | { |
2941 | elf_section_type (sec) = ssect->type; | |
2942 | elf_section_flags (sec) = ssect->attr; | |
2943 | } | |
2f89ff8d L |
2944 | } |
2945 | ||
f592407e | 2946 | return _bfd_generic_new_section_hook (abfd, sec); |
252b5132 RH |
2947 | } |
2948 | ||
2949 | /* Create a new bfd section from an ELF program header. | |
2950 | ||
2951 | Since program segments have no names, we generate a synthetic name | |
2952 | of the form segment<NUM>, where NUM is generally the index in the | |
2953 | program header table. For segments that are split (see below) we | |
2954 | generate the names segment<NUM>a and segment<NUM>b. | |
2955 | ||
2956 | Note that some program segments may have a file size that is different than | |
2957 | (less than) the memory size. All this means is that at execution the | |
2958 | system must allocate the amount of memory specified by the memory size, | |
2959 | but only initialize it with the first "file size" bytes read from the | |
2960 | file. This would occur for example, with program segments consisting | |
2961 | of combined data+bss. | |
2962 | ||
2963 | To handle the above situation, this routine generates TWO bfd sections | |
2964 | for the single program segment. The first has the length specified by | |
2965 | the file size of the segment, and the second has the length specified | |
2966 | by the difference between the two sizes. In effect, the segment is split | |
d5191d0c | 2967 | into its initialized and uninitialized parts. |
252b5132 RH |
2968 | |
2969 | */ | |
2970 | ||
b34976b6 | 2971 | bfd_boolean |
217aa764 AM |
2972 | _bfd_elf_make_section_from_phdr (bfd *abfd, |
2973 | Elf_Internal_Phdr *hdr, | |
91d6fa6a | 2974 | int hdr_index, |
a50b1753 | 2975 | const char *type_name) |
252b5132 RH |
2976 | { |
2977 | asection *newsect; | |
2978 | char *name; | |
2979 | char namebuf[64]; | |
d4c88bbb | 2980 | size_t len; |
252b5132 RH |
2981 | int split; |
2982 | ||
2983 | split = ((hdr->p_memsz > 0) | |
2984 | && (hdr->p_filesz > 0) | |
2985 | && (hdr->p_memsz > hdr->p_filesz)); | |
d5191d0c AM |
2986 | |
2987 | if (hdr->p_filesz > 0) | |
252b5132 | 2988 | { |
91d6fa6a | 2989 | sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "a" : ""); |
d5191d0c | 2990 | len = strlen (namebuf) + 1; |
a50b1753 | 2991 | name = (char *) bfd_alloc (abfd, len); |
d5191d0c AM |
2992 | if (!name) |
2993 | return FALSE; | |
2994 | memcpy (name, namebuf, len); | |
2995 | newsect = bfd_make_section (abfd, name); | |
2996 | if (newsect == NULL) | |
2997 | return FALSE; | |
2998 | newsect->vma = hdr->p_vaddr; | |
2999 | newsect->lma = hdr->p_paddr; | |
3000 | newsect->size = hdr->p_filesz; | |
3001 | newsect->filepos = hdr->p_offset; | |
3002 | newsect->flags |= SEC_HAS_CONTENTS; | |
3003 | newsect->alignment_power = bfd_log2 (hdr->p_align); | |
3004 | if (hdr->p_type == PT_LOAD) | |
252b5132 | 3005 | { |
d5191d0c AM |
3006 | newsect->flags |= SEC_ALLOC; |
3007 | newsect->flags |= SEC_LOAD; | |
3008 | if (hdr->p_flags & PF_X) | |
3009 | { | |
3010 | /* FIXME: all we known is that it has execute PERMISSION, | |
3011 | may be data. */ | |
3012 | newsect->flags |= SEC_CODE; | |
3013 | } | |
3014 | } | |
3015 | if (!(hdr->p_flags & PF_W)) | |
3016 | { | |
3017 | newsect->flags |= SEC_READONLY; | |
252b5132 | 3018 | } |
252b5132 RH |
3019 | } |
3020 | ||
d5191d0c | 3021 | if (hdr->p_memsz > hdr->p_filesz) |
252b5132 | 3022 | { |
d5191d0c AM |
3023 | bfd_vma align; |
3024 | ||
91d6fa6a | 3025 | sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "b" : ""); |
d4c88bbb | 3026 | len = strlen (namebuf) + 1; |
a50b1753 | 3027 | name = (char *) bfd_alloc (abfd, len); |
252b5132 | 3028 | if (!name) |
b34976b6 | 3029 | return FALSE; |
d4c88bbb | 3030 | memcpy (name, namebuf, len); |
252b5132 RH |
3031 | newsect = bfd_make_section (abfd, name); |
3032 | if (newsect == NULL) | |
b34976b6 | 3033 | return FALSE; |
252b5132 RH |
3034 | newsect->vma = hdr->p_vaddr + hdr->p_filesz; |
3035 | newsect->lma = hdr->p_paddr + hdr->p_filesz; | |
eea6121a | 3036 | newsect->size = hdr->p_memsz - hdr->p_filesz; |
d5191d0c AM |
3037 | newsect->filepos = hdr->p_offset + hdr->p_filesz; |
3038 | align = newsect->vma & -newsect->vma; | |
3039 | if (align == 0 || align > hdr->p_align) | |
3040 | align = hdr->p_align; | |
3041 | newsect->alignment_power = bfd_log2 (align); | |
252b5132 RH |
3042 | if (hdr->p_type == PT_LOAD) |
3043 | { | |
d5191d0c AM |
3044 | /* Hack for gdb. Segments that have not been modified do |
3045 | not have their contents written to a core file, on the | |
3046 | assumption that a debugger can find the contents in the | |
3047 | executable. We flag this case by setting the fake | |
3048 | section size to zero. Note that "real" bss sections will | |
3049 | always have their contents dumped to the core file. */ | |
3050 | if (bfd_get_format (abfd) == bfd_core) | |
3051 | newsect->size = 0; | |
252b5132 RH |
3052 | newsect->flags |= SEC_ALLOC; |
3053 | if (hdr->p_flags & PF_X) | |
3054 | newsect->flags |= SEC_CODE; | |
3055 | } | |
3056 | if (!(hdr->p_flags & PF_W)) | |
3057 | newsect->flags |= SEC_READONLY; | |
3058 | } | |
3059 | ||
b34976b6 | 3060 | return TRUE; |
252b5132 RH |
3061 | } |
3062 | ||
b34976b6 | 3063 | bfd_boolean |
91d6fa6a | 3064 | bfd_section_from_phdr (bfd *abfd, Elf_Internal_Phdr *hdr, int hdr_index) |
20cfcaae | 3065 | { |
9c5bfbb7 | 3066 | const struct elf_backend_data *bed; |
20cfcaae NC |
3067 | |
3068 | switch (hdr->p_type) | |
3069 | { | |
3070 | case PT_NULL: | |
91d6fa6a | 3071 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "null"); |
20cfcaae NC |
3072 | |
3073 | case PT_LOAD: | |
91d6fa6a | 3074 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "load"); |
20cfcaae NC |
3075 | |
3076 | case PT_DYNAMIC: | |
91d6fa6a | 3077 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "dynamic"); |
20cfcaae NC |
3078 | |
3079 | case PT_INTERP: | |
91d6fa6a | 3080 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "interp"); |
20cfcaae NC |
3081 | |
3082 | case PT_NOTE: | |
91d6fa6a | 3083 | if (! _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "note")) |
b34976b6 | 3084 | return FALSE; |
276da9b3 L |
3085 | if (! elf_read_notes (abfd, hdr->p_offset, hdr->p_filesz, |
3086 | hdr->p_align)) | |
b34976b6 AM |
3087 | return FALSE; |
3088 | return TRUE; | |
20cfcaae NC |
3089 | |
3090 | case PT_SHLIB: | |
91d6fa6a | 3091 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "shlib"); |
20cfcaae NC |
3092 | |
3093 | case PT_PHDR: | |
91d6fa6a | 3094 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "phdr"); |
20cfcaae | 3095 | |
811072d8 | 3096 | case PT_GNU_EH_FRAME: |
91d6fa6a | 3097 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, |
811072d8 RM |
3098 | "eh_frame_hdr"); |
3099 | ||
2b05f1b7 | 3100 | case PT_GNU_STACK: |
91d6fa6a | 3101 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "stack"); |
9ee5e499 | 3102 | |
8c37241b | 3103 | case PT_GNU_RELRO: |
91d6fa6a | 3104 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "relro"); |
8c37241b | 3105 | |
20cfcaae | 3106 | default: |
8c1acd09 | 3107 | /* Check for any processor-specific program segment types. */ |
20cfcaae | 3108 | bed = get_elf_backend_data (abfd); |
91d6fa6a | 3109 | return bed->elf_backend_section_from_phdr (abfd, hdr, hdr_index, "proc"); |
20cfcaae NC |
3110 | } |
3111 | } | |
3112 | ||
d4730f92 BS |
3113 | /* Return the REL_HDR for SEC, assuming there is only a single one, either |
3114 | REL or RELA. */ | |
3115 | ||
3116 | Elf_Internal_Shdr * | |
3117 | _bfd_elf_single_rel_hdr (asection *sec) | |
3118 | { | |
3119 | if (elf_section_data (sec)->rel.hdr) | |
3120 | { | |
3121 | BFD_ASSERT (elf_section_data (sec)->rela.hdr == NULL); | |
3122 | return elf_section_data (sec)->rel.hdr; | |
3123 | } | |
3124 | else | |
3125 | return elf_section_data (sec)->rela.hdr; | |
3126 | } | |
3127 | ||
3e19fb8f L |
3128 | static bfd_boolean |
3129 | _bfd_elf_set_reloc_sh_name (bfd *abfd, | |
3130 | Elf_Internal_Shdr *rel_hdr, | |
3131 | const char *sec_name, | |
3132 | bfd_boolean use_rela_p) | |
3133 | { | |
3134 | char *name = (char *) bfd_alloc (abfd, | |
3135 | sizeof ".rela" + strlen (sec_name)); | |
3136 | if (name == NULL) | |
3137 | return FALSE; | |
3138 | ||
3139 | sprintf (name, "%s%s", use_rela_p ? ".rela" : ".rel", sec_name); | |
3140 | rel_hdr->sh_name = | |
3141 | (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), name, | |
3142 | FALSE); | |
3143 | if (rel_hdr->sh_name == (unsigned int) -1) | |
3144 | return FALSE; | |
3145 | ||
3146 | return TRUE; | |
3147 | } | |
3148 | ||
d4730f92 BS |
3149 | /* Allocate and initialize a section-header for a new reloc section, |
3150 | containing relocations against ASECT. It is stored in RELDATA. If | |
3151 | USE_RELA_P is TRUE, we use RELA relocations; otherwise, we use REL | |
3152 | relocations. */ | |
23bc299b | 3153 | |
5d13b3b3 | 3154 | static bfd_boolean |
217aa764 | 3155 | _bfd_elf_init_reloc_shdr (bfd *abfd, |
d4730f92 | 3156 | struct bfd_elf_section_reloc_data *reldata, |
f6fe1ccd | 3157 | const char *sec_name, |
3e19fb8f L |
3158 | bfd_boolean use_rela_p, |
3159 | bfd_boolean delay_st_name_p) | |
23bc299b | 3160 | { |
d4730f92 | 3161 | Elf_Internal_Shdr *rel_hdr; |
9c5bfbb7 | 3162 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
d4730f92 | 3163 | |
d4730f92 | 3164 | BFD_ASSERT (reldata->hdr == NULL); |
ef53be89 | 3165 | rel_hdr = bfd_zalloc (abfd, sizeof (*rel_hdr)); |
d4730f92 | 3166 | reldata->hdr = rel_hdr; |
23bc299b | 3167 | |
3e19fb8f L |
3168 | if (delay_st_name_p) |
3169 | rel_hdr->sh_name = (unsigned int) -1; | |
3170 | else if (!_bfd_elf_set_reloc_sh_name (abfd, rel_hdr, sec_name, | |
3171 | use_rela_p)) | |
b34976b6 | 3172 | return FALSE; |
23bc299b MM |
3173 | rel_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL; |
3174 | rel_hdr->sh_entsize = (use_rela_p | |
3175 | ? bed->s->sizeof_rela | |
3176 | : bed->s->sizeof_rel); | |
72de5009 | 3177 | rel_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align; |
28e07a05 | 3178 | rel_hdr->sh_flags = 0; |
23bc299b MM |
3179 | rel_hdr->sh_addr = 0; |
3180 | rel_hdr->sh_size = 0; | |
3181 | rel_hdr->sh_offset = 0; | |
3182 | ||
b34976b6 | 3183 | return TRUE; |
23bc299b MM |
3184 | } |
3185 | ||
94be91de JB |
3186 | /* Return the default section type based on the passed in section flags. */ |
3187 | ||
3188 | int | |
3189 | bfd_elf_get_default_section_type (flagword flags) | |
3190 | { | |
0e41bebb | 3191 | if ((flags & (SEC_ALLOC | SEC_IS_COMMON)) != 0 |
2e76e85a | 3192 | && (flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0) |
94be91de JB |
3193 | return SHT_NOBITS; |
3194 | return SHT_PROGBITS; | |
3195 | } | |
3196 | ||
d4730f92 BS |
3197 | struct fake_section_arg |
3198 | { | |
3199 | struct bfd_link_info *link_info; | |
3200 | bfd_boolean failed; | |
3201 | }; | |
3202 | ||
252b5132 RH |
3203 | /* Set up an ELF internal section header for a section. */ |
3204 | ||
252b5132 | 3205 | static void |
d4730f92 | 3206 | elf_fake_sections (bfd *abfd, asection *asect, void *fsarg) |
252b5132 | 3207 | { |
d4730f92 | 3208 | struct fake_section_arg *arg = (struct fake_section_arg *)fsarg; |
9c5bfbb7 | 3209 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
d4730f92 | 3210 | struct bfd_elf_section_data *esd = elf_section_data (asect); |
252b5132 | 3211 | Elf_Internal_Shdr *this_hdr; |
0414f35b | 3212 | unsigned int sh_type; |
0ce398f1 | 3213 | const char *name = asect->name; |
3e19fb8f | 3214 | bfd_boolean delay_st_name_p = FALSE; |
252b5132 | 3215 | |
d4730f92 | 3216 | if (arg->failed) |
252b5132 RH |
3217 | { |
3218 | /* We already failed; just get out of the bfd_map_over_sections | |
08a40648 | 3219 | loop. */ |
252b5132 RH |
3220 | return; |
3221 | } | |
3222 | ||
d4730f92 | 3223 | this_hdr = &esd->this_hdr; |
252b5132 | 3224 | |
f6fe1ccd | 3225 | if (arg->link_info) |
0ce398f1 | 3226 | { |
f6fe1ccd L |
3227 | /* ld: compress DWARF debug sections with names: .debug_*. */ |
3228 | if ((arg->link_info->compress_debug & COMPRESS_DEBUG) | |
3229 | && (asect->flags & SEC_DEBUGGING) | |
3230 | && name[1] == 'd' | |
3231 | && name[6] == '_') | |
3232 | { | |
3233 | /* Set SEC_ELF_COMPRESS to indicate this section should be | |
3234 | compressed. */ | |
3235 | asect->flags |= SEC_ELF_COMPRESS; | |
0ce398f1 | 3236 | |
dd905818 | 3237 | /* If this section will be compressed, delay adding section |
3e19fb8f L |
3238 | name to section name section after it is compressed in |
3239 | _bfd_elf_assign_file_positions_for_non_load. */ | |
3240 | delay_st_name_p = TRUE; | |
f6fe1ccd L |
3241 | } |
3242 | } | |
3243 | else if ((asect->flags & SEC_ELF_RENAME)) | |
3244 | { | |
3245 | /* objcopy: rename output DWARF debug section. */ | |
3246 | if ((abfd->flags & (BFD_DECOMPRESS | BFD_COMPRESS_GABI))) | |
3247 | { | |
3248 | /* When we decompress or compress with SHF_COMPRESSED, | |
3249 | convert section name from .zdebug_* to .debug_* if | |
3250 | needed. */ | |
3251 | if (name[1] == 'z') | |
3252 | { | |
3253 | char *new_name = convert_zdebug_to_debug (abfd, name); | |
3254 | if (new_name == NULL) | |
3255 | { | |
3256 | arg->failed = TRUE; | |
3257 | return; | |
3258 | } | |
3259 | name = new_name; | |
3260 | } | |
3261 | } | |
3262 | else if (asect->compress_status == COMPRESS_SECTION_DONE) | |
0ce398f1 | 3263 | { |
f6fe1ccd L |
3264 | /* PR binutils/18087: Compression does not always make a |
3265 | section smaller. So only rename the section when | |
3266 | compression has actually taken place. If input section | |
3267 | name is .zdebug_*, we should never compress it again. */ | |
3268 | char *new_name = convert_debug_to_zdebug (abfd, name); | |
0ce398f1 L |
3269 | if (new_name == NULL) |
3270 | { | |
3271 | arg->failed = TRUE; | |
3272 | return; | |
3273 | } | |
f6fe1ccd L |
3274 | BFD_ASSERT (name[1] != 'z'); |
3275 | name = new_name; | |
0ce398f1 L |
3276 | } |
3277 | } | |
3278 | ||
3e19fb8f L |
3279 | if (delay_st_name_p) |
3280 | this_hdr->sh_name = (unsigned int) -1; | |
3281 | else | |
252b5132 | 3282 | { |
3e19fb8f L |
3283 | this_hdr->sh_name |
3284 | = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), | |
3285 | name, FALSE); | |
3286 | if (this_hdr->sh_name == (unsigned int) -1) | |
3287 | { | |
3288 | arg->failed = TRUE; | |
3289 | return; | |
3290 | } | |
252b5132 RH |
3291 | } |
3292 | ||
a4d8e49b | 3293 | /* Don't clear sh_flags. Assembler may set additional bits. */ |
252b5132 RH |
3294 | |
3295 | if ((asect->flags & SEC_ALLOC) != 0 | |
3296 | || asect->user_set_vma) | |
3297 | this_hdr->sh_addr = asect->vma; | |
3298 | else | |
3299 | this_hdr->sh_addr = 0; | |
3300 | ||
3301 | this_hdr->sh_offset = 0; | |
eea6121a | 3302 | this_hdr->sh_size = asect->size; |
252b5132 | 3303 | this_hdr->sh_link = 0; |
c86934ce NC |
3304 | /* PR 17512: file: 0eb809fe, 8b0535ee. */ |
3305 | if (asect->alignment_power >= (sizeof (bfd_vma) * 8) - 1) | |
3306 | { | |
4eca0228 | 3307 | _bfd_error_handler |
695344c0 | 3308 | /* xgettext:c-format */ |
9793eb77 | 3309 | (_("%pB: error: alignment power %d of section `%pA' is too big"), |
c08bb8dd | 3310 | abfd, asect->alignment_power, asect); |
c86934ce NC |
3311 | arg->failed = TRUE; |
3312 | return; | |
3313 | } | |
72de5009 | 3314 | this_hdr->sh_addralign = (bfd_vma) 1 << asect->alignment_power; |
252b5132 RH |
3315 | /* The sh_entsize and sh_info fields may have been set already by |
3316 | copy_private_section_data. */ | |
3317 | ||
3318 | this_hdr->bfd_section = asect; | |
3319 | this_hdr->contents = NULL; | |
3320 | ||
3cddba1e L |
3321 | /* If the section type is unspecified, we set it based on |
3322 | asect->flags. */ | |
98ece1b3 AM |
3323 | if ((asect->flags & SEC_GROUP) != 0) |
3324 | sh_type = SHT_GROUP; | |
98ece1b3 | 3325 | else |
94be91de | 3326 | sh_type = bfd_elf_get_default_section_type (asect->flags); |
98ece1b3 | 3327 | |
3cddba1e | 3328 | if (this_hdr->sh_type == SHT_NULL) |
98ece1b3 AM |
3329 | this_hdr->sh_type = sh_type; |
3330 | else if (this_hdr->sh_type == SHT_NOBITS | |
3331 | && sh_type == SHT_PROGBITS | |
3332 | && (asect->flags & SEC_ALLOC) != 0) | |
3cddba1e | 3333 | { |
98ece1b3 AM |
3334 | /* Warn if we are changing a NOBITS section to PROGBITS, but |
3335 | allow the link to proceed. This can happen when users link | |
3336 | non-bss input sections to bss output sections, or emit data | |
3337 | to a bss output section via a linker script. */ | |
4eca0228 | 3338 | _bfd_error_handler |
871b3ab2 | 3339 | (_("warning: section `%pA' type changed to PROGBITS"), asect); |
98ece1b3 | 3340 | this_hdr->sh_type = sh_type; |
3cddba1e L |
3341 | } |
3342 | ||
2f89ff8d | 3343 | switch (this_hdr->sh_type) |
252b5132 | 3344 | { |
2f89ff8d | 3345 | default: |
2f89ff8d L |
3346 | break; |
3347 | ||
3348 | case SHT_STRTAB: | |
2f89ff8d L |
3349 | case SHT_NOTE: |
3350 | case SHT_NOBITS: | |
3351 | case SHT_PROGBITS: | |
3352 | break; | |
606851fb AM |
3353 | |
3354 | case SHT_INIT_ARRAY: | |
3355 | case SHT_FINI_ARRAY: | |
3356 | case SHT_PREINIT_ARRAY: | |
3357 | this_hdr->sh_entsize = bed->s->arch_size / 8; | |
3358 | break; | |
2f89ff8d L |
3359 | |
3360 | case SHT_HASH: | |
c7ac6ff8 | 3361 | this_hdr->sh_entsize = bed->s->sizeof_hash_entry; |
2f89ff8d | 3362 | break; |
5de3bf90 | 3363 | |
2f89ff8d | 3364 | case SHT_DYNSYM: |
252b5132 | 3365 | this_hdr->sh_entsize = bed->s->sizeof_sym; |
2f89ff8d L |
3366 | break; |
3367 | ||
3368 | case SHT_DYNAMIC: | |
252b5132 | 3369 | this_hdr->sh_entsize = bed->s->sizeof_dyn; |
2f89ff8d L |
3370 | break; |
3371 | ||
3372 | case SHT_RELA: | |
3373 | if (get_elf_backend_data (abfd)->may_use_rela_p) | |
3374 | this_hdr->sh_entsize = bed->s->sizeof_rela; | |
3375 | break; | |
3376 | ||
3377 | case SHT_REL: | |
3378 | if (get_elf_backend_data (abfd)->may_use_rel_p) | |
3379 | this_hdr->sh_entsize = bed->s->sizeof_rel; | |
3380 | break; | |
3381 | ||
3382 | case SHT_GNU_versym: | |
252b5132 | 3383 | this_hdr->sh_entsize = sizeof (Elf_External_Versym); |
2f89ff8d L |
3384 | break; |
3385 | ||
3386 | case SHT_GNU_verdef: | |
252b5132 RH |
3387 | this_hdr->sh_entsize = 0; |
3388 | /* objcopy or strip will copy over sh_info, but may not set | |
08a40648 AM |
3389 | cverdefs. The linker will set cverdefs, but sh_info will be |
3390 | zero. */ | |
252b5132 RH |
3391 | if (this_hdr->sh_info == 0) |
3392 | this_hdr->sh_info = elf_tdata (abfd)->cverdefs; | |
3393 | else | |
3394 | BFD_ASSERT (elf_tdata (abfd)->cverdefs == 0 | |
3395 | || this_hdr->sh_info == elf_tdata (abfd)->cverdefs); | |
2f89ff8d L |
3396 | break; |
3397 | ||
3398 | case SHT_GNU_verneed: | |
252b5132 RH |
3399 | this_hdr->sh_entsize = 0; |
3400 | /* objcopy or strip will copy over sh_info, but may not set | |
08a40648 AM |
3401 | cverrefs. The linker will set cverrefs, but sh_info will be |
3402 | zero. */ | |
252b5132 RH |
3403 | if (this_hdr->sh_info == 0) |
3404 | this_hdr->sh_info = elf_tdata (abfd)->cverrefs; | |
3405 | else | |
3406 | BFD_ASSERT (elf_tdata (abfd)->cverrefs == 0 | |
3407 | || this_hdr->sh_info == elf_tdata (abfd)->cverrefs); | |
2f89ff8d L |
3408 | break; |
3409 | ||
3410 | case SHT_GROUP: | |
1783205a | 3411 | this_hdr->sh_entsize = GRP_ENTRY_SIZE; |
2f89ff8d | 3412 | break; |
fdc90cb4 JJ |
3413 | |
3414 | case SHT_GNU_HASH: | |
3415 | this_hdr->sh_entsize = bed->s->arch_size == 64 ? 0 : 4; | |
3416 | break; | |
dbb410c3 | 3417 | } |
252b5132 RH |
3418 | |
3419 | if ((asect->flags & SEC_ALLOC) != 0) | |
3420 | this_hdr->sh_flags |= SHF_ALLOC; | |
3421 | if ((asect->flags & SEC_READONLY) == 0) | |
3422 | this_hdr->sh_flags |= SHF_WRITE; | |
3423 | if ((asect->flags & SEC_CODE) != 0) | |
3424 | this_hdr->sh_flags |= SHF_EXECINSTR; | |
f5fa8ca2 JJ |
3425 | if ((asect->flags & SEC_MERGE) != 0) |
3426 | { | |
3427 | this_hdr->sh_flags |= SHF_MERGE; | |
3428 | this_hdr->sh_entsize = asect->entsize; | |
f5fa8ca2 | 3429 | } |
84865015 NC |
3430 | if ((asect->flags & SEC_STRINGS) != 0) |
3431 | this_hdr->sh_flags |= SHF_STRINGS; | |
1126897b | 3432 | if ((asect->flags & SEC_GROUP) == 0 && elf_group_name (asect) != NULL) |
dbb410c3 | 3433 | this_hdr->sh_flags |= SHF_GROUP; |
13ae64f3 | 3434 | if ((asect->flags & SEC_THREAD_LOCAL) != 0) |
704afa60 JJ |
3435 | { |
3436 | this_hdr->sh_flags |= SHF_TLS; | |
3a800eb9 AM |
3437 | if (asect->size == 0 |
3438 | && (asect->flags & SEC_HAS_CONTENTS) == 0) | |
704afa60 | 3439 | { |
3a800eb9 | 3440 | struct bfd_link_order *o = asect->map_tail.link_order; |
b34976b6 | 3441 | |
704afa60 | 3442 | this_hdr->sh_size = 0; |
3a800eb9 AM |
3443 | if (o != NULL) |
3444 | { | |
704afa60 | 3445 | this_hdr->sh_size = o->offset + o->size; |
3a800eb9 AM |
3446 | if (this_hdr->sh_size != 0) |
3447 | this_hdr->sh_type = SHT_NOBITS; | |
3448 | } | |
704afa60 JJ |
3449 | } |
3450 | } | |
18ae9cc1 L |
3451 | if ((asect->flags & (SEC_GROUP | SEC_EXCLUDE)) == SEC_EXCLUDE) |
3452 | this_hdr->sh_flags |= SHF_EXCLUDE; | |
252b5132 | 3453 | |
d4730f92 BS |
3454 | /* If the section has relocs, set up a section header for the |
3455 | SHT_REL[A] section. If two relocation sections are required for | |
3456 | this section, it is up to the processor-specific back-end to | |
3457 | create the other. */ | |
3458 | if ((asect->flags & SEC_RELOC) != 0) | |
3459 | { | |
3460 | /* When doing a relocatable link, create both REL and RELA sections if | |
3461 | needed. */ | |
3462 | if (arg->link_info | |
3463 | /* Do the normal setup if we wouldn't create any sections here. */ | |
3464 | && esd->rel.count + esd->rela.count > 0 | |
0e1862bb L |
3465 | && (bfd_link_relocatable (arg->link_info) |
3466 | || arg->link_info->emitrelocations)) | |
d4730f92 BS |
3467 | { |
3468 | if (esd->rel.count && esd->rel.hdr == NULL | |
28e07a05 | 3469 | && !_bfd_elf_init_reloc_shdr (abfd, &esd->rel, name, |
db4677b8 | 3470 | FALSE, delay_st_name_p)) |
d4730f92 BS |
3471 | { |
3472 | arg->failed = TRUE; | |
3473 | return; | |
3474 | } | |
3475 | if (esd->rela.count && esd->rela.hdr == NULL | |
28e07a05 | 3476 | && !_bfd_elf_init_reloc_shdr (abfd, &esd->rela, name, |
db4677b8 | 3477 | TRUE, delay_st_name_p)) |
d4730f92 BS |
3478 | { |
3479 | arg->failed = TRUE; | |
3480 | return; | |
3481 | } | |
3482 | } | |
3483 | else if (!_bfd_elf_init_reloc_shdr (abfd, | |
3484 | (asect->use_rela_p | |
3485 | ? &esd->rela : &esd->rel), | |
f6fe1ccd | 3486 | name, |
3e19fb8f L |
3487 | asect->use_rela_p, |
3488 | delay_st_name_p)) | |
db4677b8 | 3489 | { |
d4730f92 | 3490 | arg->failed = TRUE; |
db4677b8 AM |
3491 | return; |
3492 | } | |
d4730f92 BS |
3493 | } |
3494 | ||
252b5132 | 3495 | /* Check for processor-specific section types. */ |
0414f35b | 3496 | sh_type = this_hdr->sh_type; |
e1fddb6b AO |
3497 | if (bed->elf_backend_fake_sections |
3498 | && !(*bed->elf_backend_fake_sections) (abfd, this_hdr, asect)) | |
db4677b8 AM |
3499 | { |
3500 | arg->failed = TRUE; | |
3501 | return; | |
3502 | } | |
252b5132 | 3503 | |
42bb2e33 | 3504 | if (sh_type == SHT_NOBITS && asect->size != 0) |
0414f35b AM |
3505 | { |
3506 | /* Don't change the header type from NOBITS if we are being | |
42bb2e33 | 3507 | called for objcopy --only-keep-debug. */ |
0414f35b AM |
3508 | this_hdr->sh_type = sh_type; |
3509 | } | |
252b5132 RH |
3510 | } |
3511 | ||
bcacc0f5 AM |
3512 | /* Fill in the contents of a SHT_GROUP section. Called from |
3513 | _bfd_elf_compute_section_file_positions for gas, objcopy, and | |
3514 | when ELF targets use the generic linker, ld. Called for ld -r | |
3515 | from bfd_elf_final_link. */ | |
dbb410c3 | 3516 | |
1126897b | 3517 | void |
217aa764 | 3518 | bfd_elf_set_group_contents (bfd *abfd, asection *sec, void *failedptrarg) |
dbb410c3 | 3519 | { |
a50b1753 | 3520 | bfd_boolean *failedptr = (bfd_boolean *) failedptrarg; |
9dce4196 | 3521 | asection *elt, *first; |
dbb410c3 | 3522 | unsigned char *loc; |
b34976b6 | 3523 | bfd_boolean gas; |
dbb410c3 | 3524 | |
7e4111ad L |
3525 | /* Ignore linker created group section. See elfNN_ia64_object_p in |
3526 | elfxx-ia64.c. */ | |
ce5aecf8 AM |
3527 | if ((sec->flags & (SEC_GROUP | SEC_LINKER_CREATED)) != SEC_GROUP |
3528 | || sec->size == 0 | |
dbb410c3 AM |
3529 | || *failedptr) |
3530 | return; | |
3531 | ||
bcacc0f5 AM |
3532 | if (elf_section_data (sec)->this_hdr.sh_info == 0) |
3533 | { | |
3534 | unsigned long symindx = 0; | |
3535 | ||
3536 | /* elf_group_id will have been set up by objcopy and the | |
3537 | generic linker. */ | |
3538 | if (elf_group_id (sec) != NULL) | |
3539 | symindx = elf_group_id (sec)->udata.i; | |
1126897b | 3540 | |
bcacc0f5 AM |
3541 | if (symindx == 0) |
3542 | { | |
3543 | /* If called from the assembler, swap_out_syms will have set up | |
3544 | elf_section_syms. */ | |
3545 | BFD_ASSERT (elf_section_syms (abfd) != NULL); | |
3546 | symindx = elf_section_syms (abfd)[sec->index]->udata.i; | |
3547 | } | |
3548 | elf_section_data (sec)->this_hdr.sh_info = symindx; | |
3549 | } | |
3550 | else if (elf_section_data (sec)->this_hdr.sh_info == (unsigned int) -2) | |
1126897b | 3551 | { |
bcacc0f5 AM |
3552 | /* The ELF backend linker sets sh_info to -2 when the group |
3553 | signature symbol is global, and thus the index can't be | |
3554 | set until all local symbols are output. */ | |
53720c49 AM |
3555 | asection *igroup; |
3556 | struct bfd_elf_section_data *sec_data; | |
3557 | unsigned long symndx; | |
3558 | unsigned long extsymoff; | |
bcacc0f5 AM |
3559 | struct elf_link_hash_entry *h; |
3560 | ||
53720c49 AM |
3561 | /* The point of this little dance to the first SHF_GROUP section |
3562 | then back to the SHT_GROUP section is that this gets us to | |
3563 | the SHT_GROUP in the input object. */ | |
3564 | igroup = elf_sec_group (elf_next_in_group (sec)); | |
3565 | sec_data = elf_section_data (igroup); | |
3566 | symndx = sec_data->this_hdr.sh_info; | |
3567 | extsymoff = 0; | |
bcacc0f5 AM |
3568 | if (!elf_bad_symtab (igroup->owner)) |
3569 | { | |
3570 | Elf_Internal_Shdr *symtab_hdr; | |
3571 | ||
3572 | symtab_hdr = &elf_tdata (igroup->owner)->symtab_hdr; | |
3573 | extsymoff = symtab_hdr->sh_info; | |
3574 | } | |
3575 | h = elf_sym_hashes (igroup->owner)[symndx - extsymoff]; | |
3576 | while (h->root.type == bfd_link_hash_indirect | |
3577 | || h->root.type == bfd_link_hash_warning) | |
3578 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
3579 | ||
3580 | elf_section_data (sec)->this_hdr.sh_info = h->indx; | |
1126897b | 3581 | } |
dbb410c3 | 3582 | |
1126897b | 3583 | /* The contents won't be allocated for "ld -r" or objcopy. */ |
b34976b6 | 3584 | gas = TRUE; |
dbb410c3 AM |
3585 | if (sec->contents == NULL) |
3586 | { | |
b34976b6 | 3587 | gas = FALSE; |
a50b1753 | 3588 | sec->contents = (unsigned char *) bfd_alloc (abfd, sec->size); |
9dce4196 AM |
3589 | |
3590 | /* Arrange for the section to be written out. */ | |
3591 | elf_section_data (sec)->this_hdr.contents = sec->contents; | |
dbb410c3 AM |
3592 | if (sec->contents == NULL) |
3593 | { | |
b34976b6 | 3594 | *failedptr = TRUE; |
dbb410c3 AM |
3595 | return; |
3596 | } | |
3597 | } | |
3598 | ||
eea6121a | 3599 | loc = sec->contents + sec->size; |
dbb410c3 | 3600 | |
9dce4196 AM |
3601 | /* Get the pointer to the first section in the group that gas |
3602 | squirreled away here. objcopy arranges for this to be set to the | |
3603 | start of the input section group. */ | |
3604 | first = elt = elf_next_in_group (sec); | |
dbb410c3 AM |
3605 | |
3606 | /* First element is a flag word. Rest of section is elf section | |
3607 | indices for all the sections of the group. Write them backwards | |
3608 | just to keep the group in the same order as given in .section | |
3609 | directives, not that it matters. */ | |
3610 | while (elt != NULL) | |
3611 | { | |
9dce4196 | 3612 | asection *s; |
9dce4196 | 3613 | |
9dce4196 | 3614 | s = elt; |
415f38a6 AM |
3615 | if (!gas) |
3616 | s = s->output_section; | |
3617 | if (s != NULL | |
3618 | && !bfd_is_abs_section (s)) | |
01e1a5bc | 3619 | { |
db4677b8 | 3620 | struct bfd_elf_section_data *elf_sec = elf_section_data (s); |
28e07a05 AM |
3621 | struct bfd_elf_section_data *input_elf_sec = elf_section_data (elt); |
3622 | ||
3623 | if (elf_sec->rel.hdr != NULL | |
3624 | && (gas | |
3625 | || (input_elf_sec->rel.hdr != NULL | |
3626 | && input_elf_sec->rel.hdr->sh_flags & SHF_GROUP) != 0)) | |
db4677b8 | 3627 | { |
28e07a05 | 3628 | elf_sec->rel.hdr->sh_flags |= SHF_GROUP; |
db4677b8 AM |
3629 | loc -= 4; |
3630 | H_PUT_32 (abfd, elf_sec->rel.idx, loc); | |
3631 | } | |
28e07a05 AM |
3632 | if (elf_sec->rela.hdr != NULL |
3633 | && (gas | |
3634 | || (input_elf_sec->rela.hdr != NULL | |
3635 | && input_elf_sec->rela.hdr->sh_flags & SHF_GROUP) != 0)) | |
db4677b8 | 3636 | { |
28e07a05 | 3637 | elf_sec->rela.hdr->sh_flags |= SHF_GROUP; |
db4677b8 AM |
3638 | loc -= 4; |
3639 | H_PUT_32 (abfd, elf_sec->rela.idx, loc); | |
3640 | } | |
01e1a5bc | 3641 | loc -= 4; |
db4677b8 | 3642 | H_PUT_32 (abfd, elf_sec->this_idx, loc); |
01e1a5bc | 3643 | } |
945906ff | 3644 | elt = elf_next_in_group (elt); |
9dce4196 AM |
3645 | if (elt == first) |
3646 | break; | |
dbb410c3 AM |
3647 | } |
3648 | ||
7bdf4127 AB |
3649 | loc -= 4; |
3650 | BFD_ASSERT (loc == sec->contents); | |
dbb410c3 | 3651 | |
9dce4196 | 3652 | H_PUT_32 (abfd, sec->flags & SEC_LINK_ONCE ? GRP_COMDAT : 0, loc); |
dbb410c3 AM |
3653 | } |
3654 | ||
bce964aa AM |
3655 | /* Given NAME, the name of a relocation section stripped of its |
3656 | .rel/.rela prefix, return the section in ABFD to which the | |
3657 | relocations apply. */ | |
bd53a53a L |
3658 | |
3659 | asection * | |
bce964aa AM |
3660 | _bfd_elf_plt_get_reloc_section (bfd *abfd, const char *name) |
3661 | { | |
3662 | /* If a target needs .got.plt section, relocations in rela.plt/rel.plt | |
3663 | section likely apply to .got.plt or .got section. */ | |
3664 | if (get_elf_backend_data (abfd)->want_got_plt | |
3665 | && strcmp (name, ".plt") == 0) | |
3666 | { | |
3667 | asection *sec; | |
3668 | ||
3669 | name = ".got.plt"; | |
3670 | sec = bfd_get_section_by_name (abfd, name); | |
3671 | if (sec != NULL) | |
3672 | return sec; | |
3673 | name = ".got"; | |
3674 | } | |
3675 | ||
3676 | return bfd_get_section_by_name (abfd, name); | |
3677 | } | |
3678 | ||
3679 | /* Return the section to which RELOC_SEC applies. */ | |
3680 | ||
3681 | static asection * | |
3682 | elf_get_reloc_section (asection *reloc_sec) | |
bd53a53a L |
3683 | { |
3684 | const char *name; | |
3685 | unsigned int type; | |
3686 | bfd *abfd; | |
bce964aa | 3687 | const struct elf_backend_data *bed; |
bd53a53a L |
3688 | |
3689 | type = elf_section_data (reloc_sec)->this_hdr.sh_type; | |
3690 | if (type != SHT_REL && type != SHT_RELA) | |
3691 | return NULL; | |
3692 | ||
3693 | /* We look up the section the relocs apply to by name. */ | |
3694 | name = reloc_sec->name; | |
bce964aa AM |
3695 | if (strncmp (name, ".rel", 4) != 0) |
3696 | return NULL; | |
3697 | name += 4; | |
3698 | if (type == SHT_RELA && *name++ != 'a') | |
3699 | return NULL; | |
bd53a53a | 3700 | |
bd53a53a | 3701 | abfd = reloc_sec->owner; |
bce964aa AM |
3702 | bed = get_elf_backend_data (abfd); |
3703 | return bed->get_reloc_section (abfd, name); | |
bd53a53a L |
3704 | } |
3705 | ||
252b5132 RH |
3706 | /* Assign all ELF section numbers. The dummy first section is handled here |
3707 | too. The link/info pointers for the standard section types are filled | |
3708 | in here too, while we're at it. */ | |
3709 | ||
b34976b6 | 3710 | static bfd_boolean |
da9f89d4 | 3711 | assign_section_numbers (bfd *abfd, struct bfd_link_info *link_info) |
252b5132 RH |
3712 | { |
3713 | struct elf_obj_tdata *t = elf_tdata (abfd); | |
3714 | asection *sec; | |
3e19fb8f | 3715 | unsigned int section_number; |
252b5132 | 3716 | Elf_Internal_Shdr **i_shdrp; |
47cc2cf5 | 3717 | struct bfd_elf_section_data *d; |
3516e984 | 3718 | bfd_boolean need_symtab; |
252b5132 RH |
3719 | |
3720 | section_number = 1; | |
3721 | ||
2b0f7ef9 JJ |
3722 | _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd)); |
3723 | ||
da9f89d4 | 3724 | /* SHT_GROUP sections are in relocatable files only. */ |
7bdf4127 | 3725 | if (link_info == NULL || !link_info->resolve_section_groups) |
252b5132 | 3726 | { |
ef53be89 | 3727 | size_t reloc_count = 0; |
14f2c699 | 3728 | |
da9f89d4 | 3729 | /* Put SHT_GROUP sections first. */ |
04dd1667 | 3730 | for (sec = abfd->sections; sec != NULL; sec = sec->next) |
47cc2cf5 | 3731 | { |
5daa8fe7 | 3732 | d = elf_section_data (sec); |
da9f89d4 L |
3733 | |
3734 | if (d->this_hdr.sh_type == SHT_GROUP) | |
08a40648 | 3735 | { |
5daa8fe7 | 3736 | if (sec->flags & SEC_LINKER_CREATED) |
da9f89d4 L |
3737 | { |
3738 | /* Remove the linker created SHT_GROUP sections. */ | |
5daa8fe7 | 3739 | bfd_section_list_remove (abfd, sec); |
da9f89d4 | 3740 | abfd->section_count--; |
da9f89d4 | 3741 | } |
08a40648 | 3742 | else |
4fbb74a6 | 3743 | d->this_idx = section_number++; |
da9f89d4 | 3744 | } |
14f2c699 L |
3745 | |
3746 | /* Count relocations. */ | |
3747 | reloc_count += sec->reloc_count; | |
47cc2cf5 | 3748 | } |
14f2c699 L |
3749 | |
3750 | /* Clear HAS_RELOC if there are no relocations. */ | |
3751 | if (reloc_count == 0) | |
3752 | abfd->flags &= ~HAS_RELOC; | |
47cc2cf5 PB |
3753 | } |
3754 | ||
3755 | for (sec = abfd->sections; sec; sec = sec->next) | |
3756 | { | |
3757 | d = elf_section_data (sec); | |
3758 | ||
3759 | if (d->this_hdr.sh_type != SHT_GROUP) | |
4fbb74a6 | 3760 | d->this_idx = section_number++; |
3e19fb8f L |
3761 | if (d->this_hdr.sh_name != (unsigned int) -1) |
3762 | _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->this_hdr.sh_name); | |
d4730f92 | 3763 | if (d->rel.hdr) |
2b0f7ef9 | 3764 | { |
d4730f92 | 3765 | d->rel.idx = section_number++; |
3e19fb8f L |
3766 | if (d->rel.hdr->sh_name != (unsigned int) -1) |
3767 | _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel.hdr->sh_name); | |
2b0f7ef9 | 3768 | } |
d4730f92 BS |
3769 | else |
3770 | d->rel.idx = 0; | |
23bc299b | 3771 | |
d4730f92 | 3772 | if (d->rela.hdr) |
2b0f7ef9 | 3773 | { |
d4730f92 | 3774 | d->rela.idx = section_number++; |
3e19fb8f L |
3775 | if (d->rela.hdr->sh_name != (unsigned int) -1) |
3776 | _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rela.hdr->sh_name); | |
2b0f7ef9 | 3777 | } |
23bc299b | 3778 | else |
d4730f92 | 3779 | d->rela.idx = 0; |
252b5132 RH |
3780 | } |
3781 | ||
3516e984 L |
3782 | need_symtab = (bfd_get_symcount (abfd) > 0 |
3783 | || (link_info == NULL | |
3784 | && ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC)) | |
3785 | == HAS_RELOC))); | |
3786 | if (need_symtab) | |
252b5132 | 3787 | { |
12bd6957 | 3788 | elf_onesymtab (abfd) = section_number++; |
2b0f7ef9 | 3789 | _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->symtab_hdr.sh_name); |
4fbb74a6 | 3790 | if (section_number > ((SHN_LORESERVE - 2) & 0xFFFF)) |
9ad5cbcf | 3791 | { |
7a6e0d89 | 3792 | elf_section_list *entry; |
6a40cf0c NC |
3793 | |
3794 | BFD_ASSERT (elf_symtab_shndx_list (abfd) == NULL); | |
3795 | ||
7a6e0d89 | 3796 | entry = bfd_zalloc (abfd, sizeof (*entry)); |
6a40cf0c NC |
3797 | entry->ndx = section_number++; |
3798 | elf_symtab_shndx_list (abfd) = entry; | |
3799 | entry->hdr.sh_name | |
9ad5cbcf | 3800 | = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), |
b34976b6 | 3801 | ".symtab_shndx", FALSE); |
6a40cf0c | 3802 | if (entry->hdr.sh_name == (unsigned int) -1) |
b34976b6 | 3803 | return FALSE; |
9ad5cbcf | 3804 | } |
12bd6957 | 3805 | elf_strtab_sec (abfd) = section_number++; |
2b0f7ef9 | 3806 | _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->strtab_hdr.sh_name); |
252b5132 RH |
3807 | } |
3808 | ||
dd905818 NC |
3809 | elf_shstrtab_sec (abfd) = section_number++; |
3810 | _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->shstrtab_hdr.sh_name); | |
3811 | elf_elfheader (abfd)->e_shstrndx = elf_shstrtab_sec (abfd); | |
3812 | ||
1c52a645 L |
3813 | if (section_number >= SHN_LORESERVE) |
3814 | { | |
695344c0 | 3815 | /* xgettext:c-format */ |
871b3ab2 | 3816 | _bfd_error_handler (_("%pB: too many sections: %u"), |
1c52a645 L |
3817 | abfd, section_number); |
3818 | return FALSE; | |
3819 | } | |
3820 | ||
9ad5cbcf | 3821 | elf_numsections (abfd) = section_number; |
252b5132 RH |
3822 | elf_elfheader (abfd)->e_shnum = section_number; |
3823 | ||
3824 | /* Set up the list of section header pointers, in agreement with the | |
3825 | indices. */ | |
a50b1753 | 3826 | i_shdrp = (Elf_Internal_Shdr **) bfd_zalloc2 (abfd, section_number, |
07d6d2b8 | 3827 | sizeof (Elf_Internal_Shdr *)); |
252b5132 | 3828 | if (i_shdrp == NULL) |
b34976b6 | 3829 | return FALSE; |
252b5132 | 3830 | |
a50b1753 | 3831 | i_shdrp[0] = (Elf_Internal_Shdr *) bfd_zalloc (abfd, |
07d6d2b8 | 3832 | sizeof (Elf_Internal_Shdr)); |
252b5132 RH |
3833 | if (i_shdrp[0] == NULL) |
3834 | { | |
3835 | bfd_release (abfd, i_shdrp); | |
b34976b6 | 3836 | return FALSE; |
252b5132 | 3837 | } |
252b5132 RH |
3838 | |
3839 | elf_elfsections (abfd) = i_shdrp; | |
3840 | ||
12bd6957 | 3841 | i_shdrp[elf_shstrtab_sec (abfd)] = &t->shstrtab_hdr; |
3516e984 | 3842 | if (need_symtab) |
252b5132 | 3843 | { |
12bd6957 | 3844 | i_shdrp[elf_onesymtab (abfd)] = &t->symtab_hdr; |
4fbb74a6 | 3845 | if (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF)) |
9ad5cbcf | 3846 | { |
6a40cf0c NC |
3847 | elf_section_list * entry = elf_symtab_shndx_list (abfd); |
3848 | BFD_ASSERT (entry != NULL); | |
3849 | i_shdrp[entry->ndx] = & entry->hdr; | |
3850 | entry->hdr.sh_link = elf_onesymtab (abfd); | |
9ad5cbcf | 3851 | } |
12bd6957 AM |
3852 | i_shdrp[elf_strtab_sec (abfd)] = &t->strtab_hdr; |
3853 | t->symtab_hdr.sh_link = elf_strtab_sec (abfd); | |
252b5132 | 3854 | } |
38ce5b11 | 3855 | |
252b5132 RH |
3856 | for (sec = abfd->sections; sec; sec = sec->next) |
3857 | { | |
252b5132 | 3858 | asection *s; |
252b5132 | 3859 | |
91d6fa6a NC |
3860 | d = elf_section_data (sec); |
3861 | ||
252b5132 | 3862 | i_shdrp[d->this_idx] = &d->this_hdr; |
d4730f92 BS |
3863 | if (d->rel.idx != 0) |
3864 | i_shdrp[d->rel.idx] = d->rel.hdr; | |
3865 | if (d->rela.idx != 0) | |
3866 | i_shdrp[d->rela.idx] = d->rela.hdr; | |
252b5132 RH |
3867 | |
3868 | /* Fill in the sh_link and sh_info fields while we're at it. */ | |
3869 | ||
3870 | /* sh_link of a reloc section is the section index of the symbol | |
3871 | table. sh_info is the section index of the section to which | |
3872 | the relocation entries apply. */ | |
d4730f92 | 3873 | if (d->rel.idx != 0) |
252b5132 | 3874 | { |
12bd6957 | 3875 | d->rel.hdr->sh_link = elf_onesymtab (abfd); |
d4730f92 | 3876 | d->rel.hdr->sh_info = d->this_idx; |
9ef5d938 | 3877 | d->rel.hdr->sh_flags |= SHF_INFO_LINK; |
252b5132 | 3878 | } |
d4730f92 | 3879 | if (d->rela.idx != 0) |
23bc299b | 3880 | { |
12bd6957 | 3881 | d->rela.hdr->sh_link = elf_onesymtab (abfd); |
d4730f92 | 3882 | d->rela.hdr->sh_info = d->this_idx; |
9ef5d938 | 3883 | d->rela.hdr->sh_flags |= SHF_INFO_LINK; |
23bc299b | 3884 | } |
252b5132 | 3885 | |
38ce5b11 L |
3886 | /* We need to set up sh_link for SHF_LINK_ORDER. */ |
3887 | if ((d->this_hdr.sh_flags & SHF_LINK_ORDER) != 0) | |
3888 | { | |
3889 | s = elf_linked_to_section (sec); | |
3890 | if (s) | |
38ce5b11 | 3891 | { |
f2876037 | 3892 | /* elf_linked_to_section points to the input section. */ |
ccd2ec6a | 3893 | if (link_info != NULL) |
38ce5b11 | 3894 | { |
f2876037 | 3895 | /* Check discarded linkonce section. */ |
dbaa2011 | 3896 | if (discarded_section (s)) |
38ce5b11 | 3897 | { |
ccd2ec6a | 3898 | asection *kept; |
4eca0228 | 3899 | _bfd_error_handler |
695344c0 | 3900 | /* xgettext:c-format */ |
871b3ab2 AM |
3901 | (_("%pB: sh_link of section `%pA' points to" |
3902 | " discarded section `%pA' of `%pB'"), | |
ccd2ec6a L |
3903 | abfd, d->this_hdr.bfd_section, |
3904 | s, s->owner); | |
3905 | /* Point to the kept section if it has the same | |
3906 | size as the discarded one. */ | |
c0f00686 | 3907 | kept = _bfd_elf_check_kept_section (s, link_info); |
ccd2ec6a | 3908 | if (kept == NULL) |
185d09ad | 3909 | { |
ccd2ec6a L |
3910 | bfd_set_error (bfd_error_bad_value); |
3911 | return FALSE; | |
185d09ad | 3912 | } |
ccd2ec6a | 3913 | s = kept; |
38ce5b11 | 3914 | } |
e424ecc8 | 3915 | |
ccd2ec6a L |
3916 | s = s->output_section; |
3917 | BFD_ASSERT (s != NULL); | |
38ce5b11 | 3918 | } |
f2876037 L |
3919 | else |
3920 | { | |
3921 | /* Handle objcopy. */ | |
3922 | if (s->output_section == NULL) | |
3923 | { | |
4eca0228 | 3924 | _bfd_error_handler |
695344c0 | 3925 | /* xgettext:c-format */ |
871b3ab2 AM |
3926 | (_("%pB: sh_link of section `%pA' points to" |
3927 | " removed section `%pA' of `%pB'"), | |
f2876037 L |
3928 | abfd, d->this_hdr.bfd_section, s, s->owner); |
3929 | bfd_set_error (bfd_error_bad_value); | |
3930 | return FALSE; | |
3931 | } | |
3932 | s = s->output_section; | |
3933 | } | |
ccd2ec6a L |
3934 | d->this_hdr.sh_link = elf_section_data (s)->this_idx; |
3935 | } | |
3936 | else | |
3937 | { | |
3938 | /* PR 290: | |
3939 | The Intel C compiler generates SHT_IA_64_UNWIND with | |
3940 | SHF_LINK_ORDER. But it doesn't set the sh_link or | |
3941 | sh_info fields. Hence we could get the situation | |
08a40648 | 3942 | where s is NULL. */ |
ccd2ec6a L |
3943 | const struct elf_backend_data *bed |
3944 | = get_elf_backend_data (abfd); | |
3945 | if (bed->link_order_error_handler) | |
3946 | bed->link_order_error_handler | |
695344c0 | 3947 | /* xgettext:c-format */ |
871b3ab2 | 3948 | (_("%pB: warning: sh_link not set for section `%pA'"), |
ccd2ec6a | 3949 | abfd, sec); |
38ce5b11 L |
3950 | } |
3951 | } | |
3952 | ||
252b5132 RH |
3953 | switch (d->this_hdr.sh_type) |
3954 | { | |
3955 | case SHT_REL: | |
3956 | case SHT_RELA: | |
3957 | /* A reloc section which we are treating as a normal BFD | |
3958 | section. sh_link is the section index of the symbol | |
3959 | table. sh_info is the section index of the section to | |
3960 | which the relocation entries apply. We assume that an | |
3961 | allocated reloc section uses the dynamic symbol table. | |
3962 | FIXME: How can we be sure? */ | |
3963 | s = bfd_get_section_by_name (abfd, ".dynsym"); | |
3964 | if (s != NULL) | |
3965 | d->this_hdr.sh_link = elf_section_data (s)->this_idx; | |
3966 | ||
bce964aa | 3967 | s = elf_get_reloc_section (sec); |
252b5132 | 3968 | if (s != NULL) |
9ef5d938 L |
3969 | { |
3970 | d->this_hdr.sh_info = elf_section_data (s)->this_idx; | |
3971 | d->this_hdr.sh_flags |= SHF_INFO_LINK; | |
3972 | } | |
252b5132 RH |
3973 | break; |
3974 | ||
3975 | case SHT_STRTAB: | |
3976 | /* We assume that a section named .stab*str is a stabs | |
3977 | string section. We look for a section with the same name | |
3978 | but without the trailing ``str'', and set its sh_link | |
3979 | field to point to this section. */ | |
0112cd26 | 3980 | if (CONST_STRNEQ (sec->name, ".stab") |
252b5132 RH |
3981 | && strcmp (sec->name + strlen (sec->name) - 3, "str") == 0) |
3982 | { | |
3983 | size_t len; | |
3984 | char *alc; | |
3985 | ||
3986 | len = strlen (sec->name); | |
a50b1753 | 3987 | alc = (char *) bfd_malloc (len - 2); |
252b5132 | 3988 | if (alc == NULL) |
b34976b6 | 3989 | return FALSE; |
d4c88bbb | 3990 | memcpy (alc, sec->name, len - 3); |
252b5132 RH |
3991 | alc[len - 3] = '\0'; |
3992 | s = bfd_get_section_by_name (abfd, alc); | |
3993 | free (alc); | |
3994 | if (s != NULL) | |
3995 | { | |
3996 | elf_section_data (s)->this_hdr.sh_link = d->this_idx; | |
3997 | ||
3998 | /* This is a .stab section. */ | |
0594c12d AM |
3999 | if (elf_section_data (s)->this_hdr.sh_entsize == 0) |
4000 | elf_section_data (s)->this_hdr.sh_entsize | |
4001 | = 4 + 2 * bfd_get_arch_size (abfd) / 8; | |
252b5132 RH |
4002 | } |
4003 | } | |
4004 | break; | |
4005 | ||
4006 | case SHT_DYNAMIC: | |
4007 | case SHT_DYNSYM: | |
4008 | case SHT_GNU_verneed: | |
4009 | case SHT_GNU_verdef: | |
4010 | /* sh_link is the section header index of the string table | |
4011 | used for the dynamic entries, or the symbol table, or the | |
4012 | version strings. */ | |
4013 | s = bfd_get_section_by_name (abfd, ".dynstr"); | |
4014 | if (s != NULL) | |
4015 | d->this_hdr.sh_link = elf_section_data (s)->this_idx; | |
4016 | break; | |
4017 | ||
7f1204bb JJ |
4018 | case SHT_GNU_LIBLIST: |
4019 | /* sh_link is the section header index of the prelink library | |
08a40648 AM |
4020 | list used for the dynamic entries, or the symbol table, or |
4021 | the version strings. */ | |
7f1204bb JJ |
4022 | s = bfd_get_section_by_name (abfd, (sec->flags & SEC_ALLOC) |
4023 | ? ".dynstr" : ".gnu.libstr"); | |
4024 | if (s != NULL) | |
4025 | d->this_hdr.sh_link = elf_section_data (s)->this_idx; | |
4026 | break; | |
4027 | ||
252b5132 | 4028 | case SHT_HASH: |
fdc90cb4 | 4029 | case SHT_GNU_HASH: |
252b5132 RH |
4030 | case SHT_GNU_versym: |
4031 | /* sh_link is the section header index of the symbol table | |
4032 | this hash table or version table is for. */ | |
4033 | s = bfd_get_section_by_name (abfd, ".dynsym"); | |
4034 | if (s != NULL) | |
4035 | d->this_hdr.sh_link = elf_section_data (s)->this_idx; | |
4036 | break; | |
dbb410c3 AM |
4037 | |
4038 | case SHT_GROUP: | |
12bd6957 | 4039 | d->this_hdr.sh_link = elf_onesymtab (abfd); |
252b5132 RH |
4040 | } |
4041 | } | |
4042 | ||
3e19fb8f L |
4043 | /* Delay setting sh_name to _bfd_elf_write_object_contents so that |
4044 | _bfd_elf_assign_file_positions_for_non_load can convert DWARF | |
4045 | debug section name from .debug_* to .zdebug_* if needed. */ | |
4046 | ||
b34976b6 | 4047 | return TRUE; |
252b5132 RH |
4048 | } |
4049 | ||
5372391b | 4050 | static bfd_boolean |
217aa764 | 4051 | sym_is_global (bfd *abfd, asymbol *sym) |
252b5132 RH |
4052 | { |
4053 | /* If the backend has a special mapping, use it. */ | |
9c5bfbb7 | 4054 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
217aa764 AM |
4055 | if (bed->elf_backend_sym_is_global) |
4056 | return (*bed->elf_backend_sym_is_global) (abfd, sym); | |
252b5132 | 4057 | |
e47bf690 | 4058 | return ((sym->flags & (BSF_GLOBAL | BSF_WEAK | BSF_GNU_UNIQUE)) != 0 |
e6f7f6d1 AM |
4059 | || bfd_is_und_section (bfd_asymbol_section (sym)) |
4060 | || bfd_is_com_section (bfd_asymbol_section (sym))); | |
252b5132 RH |
4061 | } |
4062 | ||
76359541 TP |
4063 | /* Filter global symbols of ABFD to include in the import library. All |
4064 | SYMCOUNT symbols of ABFD can be examined from their pointers in | |
4065 | SYMS. Pointers of symbols to keep should be stored contiguously at | |
4066 | the beginning of that array. | |
4067 | ||
4068 | Returns the number of symbols to keep. */ | |
4069 | ||
4070 | unsigned int | |
4071 | _bfd_elf_filter_global_symbols (bfd *abfd, struct bfd_link_info *info, | |
4072 | asymbol **syms, long symcount) | |
4073 | { | |
4074 | long src_count, dst_count = 0; | |
4075 | ||
4076 | for (src_count = 0; src_count < symcount; src_count++) | |
4077 | { | |
4078 | asymbol *sym = syms[src_count]; | |
4079 | char *name = (char *) bfd_asymbol_name (sym); | |
4080 | struct bfd_link_hash_entry *h; | |
4081 | ||
4082 | if (!sym_is_global (abfd, sym)) | |
4083 | continue; | |
4084 | ||
4085 | h = bfd_link_hash_lookup (info->hash, name, FALSE, FALSE, FALSE); | |
5df1bc57 AM |
4086 | if (h == NULL) |
4087 | continue; | |
76359541 TP |
4088 | if (h->type != bfd_link_hash_defined && h->type != bfd_link_hash_defweak) |
4089 | continue; | |
76359541 TP |
4090 | if (h->linker_def || h->ldscript_def) |
4091 | continue; | |
4092 | ||
4093 | syms[dst_count++] = sym; | |
4094 | } | |
4095 | ||
4096 | syms[dst_count] = NULL; | |
4097 | ||
4098 | return dst_count; | |
4099 | } | |
4100 | ||
5372391b | 4101 | /* Don't output section symbols for sections that are not going to be |
c6d8cab4 | 4102 | output, that are duplicates or there is no BFD section. */ |
5372391b AM |
4103 | |
4104 | static bfd_boolean | |
4105 | ignore_section_sym (bfd *abfd, asymbol *sym) | |
4106 | { | |
c6d8cab4 L |
4107 | elf_symbol_type *type_ptr; |
4108 | ||
db0c309f NC |
4109 | if (sym == NULL) |
4110 | return FALSE; | |
4111 | ||
c6d8cab4 L |
4112 | if ((sym->flags & BSF_SECTION_SYM) == 0) |
4113 | return FALSE; | |
4114 | ||
db0c309f NC |
4115 | if (sym->section == NULL) |
4116 | return TRUE; | |
4117 | ||
c6d8cab4 L |
4118 | type_ptr = elf_symbol_from (abfd, sym); |
4119 | return ((type_ptr != NULL | |
4120 | && type_ptr->internal_elf_sym.st_shndx != 0 | |
4121 | && bfd_is_abs_section (sym->section)) | |
4122 | || !(sym->section->owner == abfd | |
db0c309f NC |
4123 | || (sym->section->output_section != NULL |
4124 | && sym->section->output_section->owner == abfd | |
2633a79c AM |
4125 | && sym->section->output_offset == 0) |
4126 | || bfd_is_abs_section (sym->section))); | |
5372391b AM |
4127 | } |
4128 | ||
2633a79c AM |
4129 | /* Map symbol from it's internal number to the external number, moving |
4130 | all local symbols to be at the head of the list. */ | |
4131 | ||
b34976b6 | 4132 | static bfd_boolean |
12bd6957 | 4133 | elf_map_symbols (bfd *abfd, unsigned int *pnum_locals) |
252b5132 | 4134 | { |
dc810e39 | 4135 | unsigned int symcount = bfd_get_symcount (abfd); |
252b5132 RH |
4136 | asymbol **syms = bfd_get_outsymbols (abfd); |
4137 | asymbol **sect_syms; | |
dc810e39 AM |
4138 | unsigned int num_locals = 0; |
4139 | unsigned int num_globals = 0; | |
4140 | unsigned int num_locals2 = 0; | |
4141 | unsigned int num_globals2 = 0; | |
7292b3ac | 4142 | unsigned int max_index = 0; |
dc810e39 | 4143 | unsigned int idx; |
252b5132 RH |
4144 | asection *asect; |
4145 | asymbol **new_syms; | |
252b5132 RH |
4146 | |
4147 | #ifdef DEBUG | |
4148 | fprintf (stderr, "elf_map_symbols\n"); | |
4149 | fflush (stderr); | |
4150 | #endif | |
4151 | ||
252b5132 RH |
4152 | for (asect = abfd->sections; asect; asect = asect->next) |
4153 | { | |
4154 | if (max_index < asect->index) | |
4155 | max_index = asect->index; | |
4156 | } | |
4157 | ||
4158 | max_index++; | |
a50b1753 | 4159 | sect_syms = (asymbol **) bfd_zalloc2 (abfd, max_index, sizeof (asymbol *)); |
252b5132 | 4160 | if (sect_syms == NULL) |
b34976b6 | 4161 | return FALSE; |
252b5132 | 4162 | elf_section_syms (abfd) = sect_syms; |
4e89ac30 | 4163 | elf_num_section_syms (abfd) = max_index; |
252b5132 | 4164 | |
079e9a2f AM |
4165 | /* Init sect_syms entries for any section symbols we have already |
4166 | decided to output. */ | |
252b5132 RH |
4167 | for (idx = 0; idx < symcount; idx++) |
4168 | { | |
dc810e39 | 4169 | asymbol *sym = syms[idx]; |
c044fabd | 4170 | |
252b5132 | 4171 | if ((sym->flags & BSF_SECTION_SYM) != 0 |
0f0a5e58 | 4172 | && sym->value == 0 |
2633a79c AM |
4173 | && !ignore_section_sym (abfd, sym) |
4174 | && !bfd_is_abs_section (sym->section)) | |
252b5132 | 4175 | { |
5372391b | 4176 | asection *sec = sym->section; |
252b5132 | 4177 | |
5372391b AM |
4178 | if (sec->owner != abfd) |
4179 | sec = sec->output_section; | |
252b5132 | 4180 | |
5372391b | 4181 | sect_syms[sec->index] = syms[idx]; |
252b5132 RH |
4182 | } |
4183 | } | |
4184 | ||
252b5132 RH |
4185 | /* Classify all of the symbols. */ |
4186 | for (idx = 0; idx < symcount; idx++) | |
4187 | { | |
2633a79c | 4188 | if (sym_is_global (abfd, syms[idx])) |
252b5132 | 4189 | num_globals++; |
2633a79c AM |
4190 | else if (!ignore_section_sym (abfd, syms[idx])) |
4191 | num_locals++; | |
252b5132 | 4192 | } |
079e9a2f | 4193 | |
5372391b | 4194 | /* We will be adding a section symbol for each normal BFD section. Most |
079e9a2f AM |
4195 | sections will already have a section symbol in outsymbols, but |
4196 | eg. SHT_GROUP sections will not, and we need the section symbol mapped | |
4197 | at least in that case. */ | |
252b5132 RH |
4198 | for (asect = abfd->sections; asect; asect = asect->next) |
4199 | { | |
079e9a2f | 4200 | if (sect_syms[asect->index] == NULL) |
252b5132 | 4201 | { |
079e9a2f | 4202 | if (!sym_is_global (abfd, asect->symbol)) |
252b5132 RH |
4203 | num_locals++; |
4204 | else | |
4205 | num_globals++; | |
252b5132 RH |
4206 | } |
4207 | } | |
4208 | ||
4209 | /* Now sort the symbols so the local symbols are first. */ | |
a50b1753 | 4210 | new_syms = (asymbol **) bfd_alloc2 (abfd, num_locals + num_globals, |
07d6d2b8 | 4211 | sizeof (asymbol *)); |
dc810e39 | 4212 | |
252b5132 | 4213 | if (new_syms == NULL) |
b34976b6 | 4214 | return FALSE; |
252b5132 RH |
4215 | |
4216 | for (idx = 0; idx < symcount; idx++) | |
4217 | { | |
4218 | asymbol *sym = syms[idx]; | |
dc810e39 | 4219 | unsigned int i; |
252b5132 | 4220 | |
2633a79c AM |
4221 | if (sym_is_global (abfd, sym)) |
4222 | i = num_locals + num_globals2++; | |
4223 | else if (!ignore_section_sym (abfd, sym)) | |
252b5132 RH |
4224 | i = num_locals2++; |
4225 | else | |
2633a79c | 4226 | continue; |
252b5132 RH |
4227 | new_syms[i] = sym; |
4228 | sym->udata.i = i + 1; | |
4229 | } | |
4230 | for (asect = abfd->sections; asect; asect = asect->next) | |
4231 | { | |
079e9a2f | 4232 | if (sect_syms[asect->index] == NULL) |
252b5132 | 4233 | { |
079e9a2f | 4234 | asymbol *sym = asect->symbol; |
dc810e39 | 4235 | unsigned int i; |
252b5132 | 4236 | |
079e9a2f | 4237 | sect_syms[asect->index] = sym; |
252b5132 RH |
4238 | if (!sym_is_global (abfd, sym)) |
4239 | i = num_locals2++; | |
4240 | else | |
4241 | i = num_locals + num_globals2++; | |
4242 | new_syms[i] = sym; | |
4243 | sym->udata.i = i + 1; | |
4244 | } | |
4245 | } | |
4246 | ||
4247 | bfd_set_symtab (abfd, new_syms, num_locals + num_globals); | |
4248 | ||
12bd6957 | 4249 | *pnum_locals = num_locals; |
b34976b6 | 4250 | return TRUE; |
252b5132 RH |
4251 | } |
4252 | ||
4253 | /* Align to the maximum file alignment that could be required for any | |
4254 | ELF data structure. */ | |
4255 | ||
268b6b39 | 4256 | static inline file_ptr |
217aa764 | 4257 | align_file_position (file_ptr off, int align) |
252b5132 RH |
4258 | { |
4259 | return (off + align - 1) & ~(align - 1); | |
4260 | } | |
4261 | ||
4262 | /* Assign a file position to a section, optionally aligning to the | |
4263 | required section alignment. */ | |
4264 | ||
217aa764 AM |
4265 | file_ptr |
4266 | _bfd_elf_assign_file_position_for_section (Elf_Internal_Shdr *i_shdrp, | |
4267 | file_ptr offset, | |
4268 | bfd_boolean align) | |
252b5132 | 4269 | { |
72de5009 AM |
4270 | if (align && i_shdrp->sh_addralign > 1) |
4271 | offset = BFD_ALIGN (offset, i_shdrp->sh_addralign); | |
252b5132 RH |
4272 | i_shdrp->sh_offset = offset; |
4273 | if (i_shdrp->bfd_section != NULL) | |
4274 | i_shdrp->bfd_section->filepos = offset; | |
4275 | if (i_shdrp->sh_type != SHT_NOBITS) | |
4276 | offset += i_shdrp->sh_size; | |
4277 | return offset; | |
4278 | } | |
4279 | ||
4280 | /* Compute the file positions we are going to put the sections at, and | |
4281 | otherwise prepare to begin writing out the ELF file. If LINK_INFO | |
4282 | is not NULL, this is being called by the ELF backend linker. */ | |
4283 | ||
b34976b6 | 4284 | bfd_boolean |
217aa764 AM |
4285 | _bfd_elf_compute_section_file_positions (bfd *abfd, |
4286 | struct bfd_link_info *link_info) | |
252b5132 | 4287 | { |
9c5bfbb7 | 4288 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
d4730f92 | 4289 | struct fake_section_arg fsargs; |
b34976b6 | 4290 | bfd_boolean failed; |
ef10c3ac | 4291 | struct elf_strtab_hash *strtab = NULL; |
252b5132 | 4292 | Elf_Internal_Shdr *shstrtab_hdr; |
3516e984 | 4293 | bfd_boolean need_symtab; |
252b5132 RH |
4294 | |
4295 | if (abfd->output_has_begun) | |
b34976b6 | 4296 | return TRUE; |
252b5132 RH |
4297 | |
4298 | /* Do any elf backend specific processing first. */ | |
4299 | if (bed->elf_backend_begin_write_processing) | |
4300 | (*bed->elf_backend_begin_write_processing) (abfd, link_info); | |
4301 | ||
4302 | if (! prep_headers (abfd)) | |
b34976b6 | 4303 | return FALSE; |
252b5132 | 4304 | |
e6c51ed4 | 4305 | /* Post process the headers if necessary. */ |
78245035 | 4306 | (*bed->elf_backend_post_process_headers) (abfd, link_info); |
e6c51ed4 | 4307 | |
d4730f92 BS |
4308 | fsargs.failed = FALSE; |
4309 | fsargs.link_info = link_info; | |
4310 | bfd_map_over_sections (abfd, elf_fake_sections, &fsargs); | |
4311 | if (fsargs.failed) | |
b34976b6 | 4312 | return FALSE; |
252b5132 | 4313 | |
da9f89d4 | 4314 | if (!assign_section_numbers (abfd, link_info)) |
b34976b6 | 4315 | return FALSE; |
252b5132 RH |
4316 | |
4317 | /* The backend linker builds symbol table information itself. */ | |
3516e984 L |
4318 | need_symtab = (link_info == NULL |
4319 | && (bfd_get_symcount (abfd) > 0 | |
4320 | || ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC)) | |
4321 | == HAS_RELOC))); | |
4322 | if (need_symtab) | |
252b5132 RH |
4323 | { |
4324 | /* Non-zero if doing a relocatable link. */ | |
4325 | int relocatable_p = ! (abfd->flags & (EXEC_P | DYNAMIC)); | |
4326 | ||
4327 | if (! swap_out_syms (abfd, &strtab, relocatable_p)) | |
b34976b6 | 4328 | return FALSE; |
252b5132 RH |
4329 | } |
4330 | ||
d4730f92 | 4331 | failed = FALSE; |
1126897b | 4332 | if (link_info == NULL) |
dbb410c3 | 4333 | { |
1126897b | 4334 | bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed); |
dbb410c3 | 4335 | if (failed) |
b34976b6 | 4336 | return FALSE; |
dbb410c3 AM |
4337 | } |
4338 | ||
252b5132 RH |
4339 | shstrtab_hdr = &elf_tdata (abfd)->shstrtab_hdr; |
4340 | /* sh_name was set in prep_headers. */ | |
4341 | shstrtab_hdr->sh_type = SHT_STRTAB; | |
84865015 | 4342 | shstrtab_hdr->sh_flags = bed->elf_strtab_flags; |
252b5132 | 4343 | shstrtab_hdr->sh_addr = 0; |
946748d5 | 4344 | /* sh_size is set in _bfd_elf_assign_file_positions_for_non_load. */ |
252b5132 RH |
4345 | shstrtab_hdr->sh_entsize = 0; |
4346 | shstrtab_hdr->sh_link = 0; | |
4347 | shstrtab_hdr->sh_info = 0; | |
3e19fb8f | 4348 | /* sh_offset is set in _bfd_elf_assign_file_positions_for_non_load. */ |
252b5132 RH |
4349 | shstrtab_hdr->sh_addralign = 1; |
4350 | ||
c84fca4d | 4351 | if (!assign_file_positions_except_relocs (abfd, link_info)) |
b34976b6 | 4352 | return FALSE; |
252b5132 | 4353 | |
3516e984 | 4354 | if (need_symtab) |
252b5132 RH |
4355 | { |
4356 | file_ptr off; | |
4357 | Elf_Internal_Shdr *hdr; | |
4358 | ||
12bd6957 | 4359 | off = elf_next_file_pos (abfd); |
252b5132 | 4360 | |
6a40cf0c | 4361 | hdr = & elf_symtab_hdr (abfd); |
b34976b6 | 4362 | off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE); |
252b5132 | 4363 | |
6a40cf0c NC |
4364 | if (elf_symtab_shndx_list (abfd) != NULL) |
4365 | { | |
4366 | hdr = & elf_symtab_shndx_list (abfd)->hdr; | |
4367 | if (hdr->sh_size != 0) | |
4368 | off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE); | |
4369 | /* FIXME: What about other symtab_shndx sections in the list ? */ | |
4370 | } | |
9ad5cbcf | 4371 | |
252b5132 | 4372 | hdr = &elf_tdata (abfd)->strtab_hdr; |
b34976b6 | 4373 | off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE); |
252b5132 | 4374 | |
12bd6957 | 4375 | elf_next_file_pos (abfd) = off; |
252b5132 RH |
4376 | |
4377 | /* Now that we know where the .strtab section goes, write it | |
08a40648 | 4378 | out. */ |
252b5132 | 4379 | if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0 |
ef10c3ac | 4380 | || ! _bfd_elf_strtab_emit (abfd, strtab)) |
b34976b6 | 4381 | return FALSE; |
ef10c3ac | 4382 | _bfd_elf_strtab_free (strtab); |
252b5132 RH |
4383 | } |
4384 | ||
b34976b6 | 4385 | abfd->output_has_begun = TRUE; |
252b5132 | 4386 | |
b34976b6 | 4387 | return TRUE; |
252b5132 RH |
4388 | } |
4389 | ||
8ded5a0f AM |
4390 | /* Make an initial estimate of the size of the program header. If we |
4391 | get the number wrong here, we'll redo section placement. */ | |
4392 | ||
4393 | static bfd_size_type | |
4394 | get_program_header_size (bfd *abfd, struct bfd_link_info *info) | |
4395 | { | |
4396 | size_t segs; | |
4397 | asection *s; | |
2b05f1b7 | 4398 | const struct elf_backend_data *bed; |
8ded5a0f AM |
4399 | |
4400 | /* Assume we will need exactly two PT_LOAD segments: one for text | |
4401 | and one for data. */ | |
4402 | segs = 2; | |
4403 | ||
4404 | s = bfd_get_section_by_name (abfd, ".interp"); | |
1b9e270b | 4405 | if (s != NULL && (s->flags & SEC_LOAD) != 0 && s->size != 0) |
8ded5a0f AM |
4406 | { |
4407 | /* If we have a loadable interpreter section, we need a | |
4408 | PT_INTERP segment. In this case, assume we also need a | |
4409 | PT_PHDR segment, although that may not be true for all | |
4410 | targets. */ | |
e9a38e0f | 4411 | segs += 2; |
8ded5a0f AM |
4412 | } |
4413 | ||
4414 | if (bfd_get_section_by_name (abfd, ".dynamic") != NULL) | |
4415 | { | |
4416 | /* We need a PT_DYNAMIC segment. */ | |
4417 | ++segs; | |
f210dcff | 4418 | } |
08a40648 | 4419 | |
ceae84aa | 4420 | if (info != NULL && info->relro) |
f210dcff L |
4421 | { |
4422 | /* We need a PT_GNU_RELRO segment. */ | |
4423 | ++segs; | |
8ded5a0f AM |
4424 | } |
4425 | ||
12bd6957 | 4426 | if (elf_eh_frame_hdr (abfd)) |
8ded5a0f AM |
4427 | { |
4428 | /* We need a PT_GNU_EH_FRAME segment. */ | |
4429 | ++segs; | |
4430 | } | |
4431 | ||
12bd6957 | 4432 | if (elf_stack_flags (abfd)) |
8ded5a0f | 4433 | { |
2b05f1b7 L |
4434 | /* We need a PT_GNU_STACK segment. */ |
4435 | ++segs; | |
4436 | } | |
94b11780 | 4437 | |
0a59decb L |
4438 | s = bfd_get_section_by_name (abfd, |
4439 | NOTE_GNU_PROPERTY_SECTION_NAME); | |
4440 | if (s != NULL && s->size != 0) | |
4441 | { | |
4442 | /* We need a PT_GNU_PROPERTY segment. */ | |
4443 | ++segs; | |
4444 | } | |
4445 | ||
2b05f1b7 L |
4446 | for (s = abfd->sections; s != NULL; s = s->next) |
4447 | { | |
8ded5a0f | 4448 | if ((s->flags & SEC_LOAD) != 0 |
23e463ed | 4449 | && elf_section_type (s) == SHT_NOTE) |
8ded5a0f | 4450 | { |
23e463ed | 4451 | unsigned int alignment_power; |
8ded5a0f AM |
4452 | /* We need a PT_NOTE segment. */ |
4453 | ++segs; | |
23e463ed L |
4454 | /* Try to create just one PT_NOTE segment for all adjacent |
4455 | loadable SHT_NOTE sections. gABI requires that within a | |
4456 | PT_NOTE segment (and also inside of each SHT_NOTE section) | |
4457 | each note should have the same alignment. So we check | |
4458 | whether the sections are correctly aligned. */ | |
4459 | alignment_power = s->alignment_power; | |
4460 | while (s->next != NULL | |
4461 | && s->next->alignment_power == alignment_power | |
4462 | && (s->next->flags & SEC_LOAD) != 0 | |
4463 | && elf_section_type (s->next) == SHT_NOTE) | |
4464 | s = s->next; | |
8ded5a0f AM |
4465 | } |
4466 | } | |
4467 | ||
4468 | for (s = abfd->sections; s != NULL; s = s->next) | |
4469 | { | |
4470 | if (s->flags & SEC_THREAD_LOCAL) | |
4471 | { | |
4472 | /* We need a PT_TLS segment. */ | |
4473 | ++segs; | |
4474 | break; | |
4475 | } | |
4476 | } | |
4477 | ||
2b05f1b7 | 4478 | bed = get_elf_backend_data (abfd); |
a91e1603 | 4479 | |
df3a023b AM |
4480 | if ((abfd->flags & D_PAGED) != 0 |
4481 | && (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_mbind) != 0) | |
4482 | { | |
4483 | /* Add a PT_GNU_MBIND segment for each mbind section. */ | |
4484 | unsigned int page_align_power = bfd_log2 (bed->commonpagesize); | |
4485 | for (s = abfd->sections; s != NULL; s = s->next) | |
4486 | if (elf_section_flags (s) & SHF_GNU_MBIND) | |
4487 | { | |
4488 | if (elf_section_data (s)->this_hdr.sh_info > PT_GNU_MBIND_NUM) | |
4489 | { | |
4490 | _bfd_error_handler | |
4491 | /* xgettext:c-format */ | |
4492 | (_("%pB: GNU_MBIND section `%pA' has invalid " | |
4493 | "sh_info field: %d"), | |
4494 | abfd, s, elf_section_data (s)->this_hdr.sh_info); | |
4495 | continue; | |
4496 | } | |
4497 | /* Align mbind section to page size. */ | |
4498 | if (s->alignment_power < page_align_power) | |
4499 | s->alignment_power = page_align_power; | |
4500 | segs ++; | |
4501 | } | |
4502 | } | |
4503 | ||
4504 | /* Let the backend count up any program headers it might need. */ | |
4505 | if (bed->elf_backend_additional_program_headers) | |
8ded5a0f AM |
4506 | { |
4507 | int a; | |
4508 | ||
4509 | a = (*bed->elf_backend_additional_program_headers) (abfd, info); | |
4510 | if (a == -1) | |
4511 | abort (); | |
4512 | segs += a; | |
4513 | } | |
4514 | ||
4515 | return segs * bed->s->sizeof_phdr; | |
4516 | } | |
4517 | ||
2ea37f1c NC |
4518 | /* Find the segment that contains the output_section of section. */ |
4519 | ||
4520 | Elf_Internal_Phdr * | |
4521 | _bfd_elf_find_segment_containing_section (bfd * abfd, asection * section) | |
4522 | { | |
4523 | struct elf_segment_map *m; | |
4524 | Elf_Internal_Phdr *p; | |
4525 | ||
12bd6957 | 4526 | for (m = elf_seg_map (abfd), p = elf_tdata (abfd)->phdr; |
2ea37f1c NC |
4527 | m != NULL; |
4528 | m = m->next, p++) | |
4529 | { | |
4530 | int i; | |
4531 | ||
4532 | for (i = m->count - 1; i >= 0; i--) | |
4533 | if (m->sections[i] == section) | |
4534 | return p; | |
4535 | } | |
4536 | ||
4537 | return NULL; | |
4538 | } | |
4539 | ||
252b5132 RH |
4540 | /* Create a mapping from a set of sections to a program segment. */ |
4541 | ||
217aa764 AM |
4542 | static struct elf_segment_map * |
4543 | make_mapping (bfd *abfd, | |
4544 | asection **sections, | |
4545 | unsigned int from, | |
4546 | unsigned int to, | |
4547 | bfd_boolean phdr) | |
252b5132 RH |
4548 | { |
4549 | struct elf_segment_map *m; | |
4550 | unsigned int i; | |
4551 | asection **hdrpp; | |
dc810e39 | 4552 | bfd_size_type amt; |
252b5132 | 4553 | |
00bee008 AM |
4554 | amt = sizeof (struct elf_segment_map) - sizeof (asection *); |
4555 | amt += (to - from) * sizeof (asection *); | |
a50b1753 | 4556 | m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); |
252b5132 RH |
4557 | if (m == NULL) |
4558 | return NULL; | |
4559 | m->next = NULL; | |
4560 | m->p_type = PT_LOAD; | |
4561 | for (i = from, hdrpp = sections + from; i < to; i++, hdrpp++) | |
4562 | m->sections[i - from] = *hdrpp; | |
4563 | m->count = to - from; | |
4564 | ||
4565 | if (from == 0 && phdr) | |
4566 | { | |
4567 | /* Include the headers in the first PT_LOAD segment. */ | |
4568 | m->includes_filehdr = 1; | |
4569 | m->includes_phdrs = 1; | |
4570 | } | |
4571 | ||
4572 | return m; | |
4573 | } | |
4574 | ||
229fcec5 MM |
4575 | /* Create the PT_DYNAMIC segment, which includes DYNSEC. Returns NULL |
4576 | on failure. */ | |
4577 | ||
4578 | struct elf_segment_map * | |
4579 | _bfd_elf_make_dynamic_segment (bfd *abfd, asection *dynsec) | |
4580 | { | |
4581 | struct elf_segment_map *m; | |
4582 | ||
a50b1753 | 4583 | m = (struct elf_segment_map *) bfd_zalloc (abfd, |
07d6d2b8 | 4584 | sizeof (struct elf_segment_map)); |
229fcec5 MM |
4585 | if (m == NULL) |
4586 | return NULL; | |
4587 | m->next = NULL; | |
4588 | m->p_type = PT_DYNAMIC; | |
4589 | m->count = 1; | |
4590 | m->sections[0] = dynsec; | |
08a40648 | 4591 | |
229fcec5 MM |
4592 | return m; |
4593 | } | |
4594 | ||
8ded5a0f | 4595 | /* Possibly add or remove segments from the segment map. */ |
252b5132 | 4596 | |
b34976b6 | 4597 | static bfd_boolean |
3dea8fca AM |
4598 | elf_modify_segment_map (bfd *abfd, |
4599 | struct bfd_link_info *info, | |
4600 | bfd_boolean remove_empty_load) | |
252b5132 | 4601 | { |
252e386e | 4602 | struct elf_segment_map **m; |
8ded5a0f | 4603 | const struct elf_backend_data *bed; |
252b5132 | 4604 | |
8ded5a0f AM |
4605 | /* The placement algorithm assumes that non allocated sections are |
4606 | not in PT_LOAD segments. We ensure this here by removing such | |
4607 | sections from the segment map. We also remove excluded | |
252e386e AM |
4608 | sections. Finally, any PT_LOAD segment without sections is |
4609 | removed. */ | |
12bd6957 | 4610 | m = &elf_seg_map (abfd); |
252e386e | 4611 | while (*m) |
8ded5a0f AM |
4612 | { |
4613 | unsigned int i, new_count; | |
252b5132 | 4614 | |
252e386e | 4615 | for (new_count = 0, i = 0; i < (*m)->count; i++) |
8ded5a0f | 4616 | { |
252e386e AM |
4617 | if (((*m)->sections[i]->flags & SEC_EXCLUDE) == 0 |
4618 | && (((*m)->sections[i]->flags & SEC_ALLOC) != 0 | |
4619 | || (*m)->p_type != PT_LOAD)) | |
8ded5a0f | 4620 | { |
252e386e AM |
4621 | (*m)->sections[new_count] = (*m)->sections[i]; |
4622 | new_count++; | |
8ded5a0f AM |
4623 | } |
4624 | } | |
252e386e | 4625 | (*m)->count = new_count; |
252b5132 | 4626 | |
1a9ccd70 NC |
4627 | if (remove_empty_load |
4628 | && (*m)->p_type == PT_LOAD | |
4629 | && (*m)->count == 0 | |
4630 | && !(*m)->includes_phdrs) | |
252e386e AM |
4631 | *m = (*m)->next; |
4632 | else | |
4633 | m = &(*m)->next; | |
8ded5a0f | 4634 | } |
252b5132 | 4635 | |
8ded5a0f AM |
4636 | bed = get_elf_backend_data (abfd); |
4637 | if (bed->elf_backend_modify_segment_map != NULL) | |
252b5132 | 4638 | { |
252e386e | 4639 | if (!(*bed->elf_backend_modify_segment_map) (abfd, info)) |
8ded5a0f | 4640 | return FALSE; |
252b5132 | 4641 | } |
252b5132 | 4642 | |
8ded5a0f AM |
4643 | return TRUE; |
4644 | } | |
252b5132 | 4645 | |
dbc88fc1 AM |
4646 | #define IS_TBSS(s) \ |
4647 | ((s->flags & (SEC_THREAD_LOCAL | SEC_LOAD)) == SEC_THREAD_LOCAL) | |
4648 | ||
8ded5a0f | 4649 | /* Set up a mapping from BFD sections to program segments. */ |
252b5132 | 4650 | |
8ded5a0f AM |
4651 | bfd_boolean |
4652 | _bfd_elf_map_sections_to_segments (bfd *abfd, struct bfd_link_info *info) | |
4653 | { | |
4654 | unsigned int count; | |
4655 | struct elf_segment_map *m; | |
4656 | asection **sections = NULL; | |
4657 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
3dea8fca | 4658 | bfd_boolean no_user_phdrs; |
252b5132 | 4659 | |
12bd6957 | 4660 | no_user_phdrs = elf_seg_map (abfd) == NULL; |
d324f6d6 RM |
4661 | |
4662 | if (info != NULL) | |
4663 | info->user_phdrs = !no_user_phdrs; | |
4664 | ||
3dea8fca | 4665 | if (no_user_phdrs && bfd_count_sections (abfd) != 0) |
252b5132 | 4666 | { |
8ded5a0f AM |
4667 | asection *s; |
4668 | unsigned int i; | |
4669 | struct elf_segment_map *mfirst; | |
4670 | struct elf_segment_map **pm; | |
4671 | asection *last_hdr; | |
4672 | bfd_vma last_size; | |
00bee008 | 4673 | unsigned int hdr_index; |
8ded5a0f AM |
4674 | bfd_vma maxpagesize; |
4675 | asection **hdrpp; | |
64029e93 | 4676 | bfd_boolean phdr_in_segment; |
8ded5a0f | 4677 | bfd_boolean writable; |
2888249f | 4678 | bfd_boolean executable; |
8ded5a0f AM |
4679 | int tls_count = 0; |
4680 | asection *first_tls = NULL; | |
a91e1603 | 4681 | asection *first_mbind = NULL; |
8ded5a0f AM |
4682 | asection *dynsec, *eh_frame_hdr; |
4683 | bfd_size_type amt; | |
8d06853e | 4684 | bfd_vma addr_mask, wrap_to = 0; |
64029e93 | 4685 | bfd_size_type phdr_size; |
252b5132 | 4686 | |
8ded5a0f | 4687 | /* Select the allocated sections, and sort them. */ |
252b5132 | 4688 | |
a50b1753 | 4689 | sections = (asection **) bfd_malloc2 (bfd_count_sections (abfd), |
07d6d2b8 | 4690 | sizeof (asection *)); |
8ded5a0f | 4691 | if (sections == NULL) |
252b5132 | 4692 | goto error_return; |
252b5132 | 4693 | |
8d06853e AM |
4694 | /* Calculate top address, avoiding undefined behaviour of shift |
4695 | left operator when shift count is equal to size of type | |
4696 | being shifted. */ | |
4697 | addr_mask = ((bfd_vma) 1 << (bfd_arch_bits_per_address (abfd) - 1)) - 1; | |
4698 | addr_mask = (addr_mask << 1) + 1; | |
4699 | ||
8ded5a0f AM |
4700 | i = 0; |
4701 | for (s = abfd->sections; s != NULL; s = s->next) | |
4702 | { | |
4703 | if ((s->flags & SEC_ALLOC) != 0) | |
4704 | { | |
4705 | sections[i] = s; | |
4706 | ++i; | |
8d06853e AM |
4707 | /* A wrapping section potentially clashes with header. */ |
4708 | if (((s->lma + s->size) & addr_mask) < (s->lma & addr_mask)) | |
4709 | wrap_to = (s->lma + s->size) & addr_mask; | |
8ded5a0f AM |
4710 | } |
4711 | } | |
4712 | BFD_ASSERT (i <= bfd_count_sections (abfd)); | |
4713 | count = i; | |
252b5132 | 4714 | |
8ded5a0f | 4715 | qsort (sections, (size_t) count, sizeof (asection *), elf_sort_sections); |
252b5132 | 4716 | |
64029e93 AM |
4717 | phdr_size = elf_program_header_size (abfd); |
4718 | if (phdr_size == (bfd_size_type) -1) | |
4719 | phdr_size = get_program_header_size (abfd, info); | |
4720 | phdr_size += bed->s->sizeof_ehdr; | |
4721 | maxpagesize = bed->maxpagesize; | |
4722 | if (maxpagesize == 0) | |
4723 | maxpagesize = 1; | |
4724 | phdr_in_segment = info != NULL && info->load_phdrs; | |
4725 | if (count != 0 | |
4726 | && (((sections[0]->lma & addr_mask) & (maxpagesize - 1)) | |
4727 | >= (phdr_size & (maxpagesize - 1)))) | |
4728 | /* For compatibility with old scripts that may not be using | |
4729 | SIZEOF_HEADERS, add headers when it looks like space has | |
4730 | been left for them. */ | |
4731 | phdr_in_segment = TRUE; | |
252b5132 | 4732 | |
64029e93 | 4733 | /* Build the mapping. */ |
8ded5a0f AM |
4734 | mfirst = NULL; |
4735 | pm = &mfirst; | |
252b5132 | 4736 | |
8ded5a0f AM |
4737 | /* If we have a .interp section, then create a PT_PHDR segment for |
4738 | the program headers and a PT_INTERP segment for the .interp | |
4739 | section. */ | |
4740 | s = bfd_get_section_by_name (abfd, ".interp"); | |
1b9e270b | 4741 | if (s != NULL && (s->flags & SEC_LOAD) != 0 && s->size != 0) |
8ded5a0f AM |
4742 | { |
4743 | amt = sizeof (struct elf_segment_map); | |
a50b1753 | 4744 | m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); |
8ded5a0f AM |
4745 | if (m == NULL) |
4746 | goto error_return; | |
4747 | m->next = NULL; | |
4748 | m->p_type = PT_PHDR; | |
f882209d | 4749 | m->p_flags = PF_R; |
8ded5a0f AM |
4750 | m->p_flags_valid = 1; |
4751 | m->includes_phdrs = 1; | |
64029e93 | 4752 | phdr_in_segment = TRUE; |
8ded5a0f AM |
4753 | *pm = m; |
4754 | pm = &m->next; | |
252b5132 | 4755 | |
8ded5a0f | 4756 | amt = sizeof (struct elf_segment_map); |
a50b1753 | 4757 | m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); |
8ded5a0f AM |
4758 | if (m == NULL) |
4759 | goto error_return; | |
4760 | m->next = NULL; | |
4761 | m->p_type = PT_INTERP; | |
4762 | m->count = 1; | |
4763 | m->sections[0] = s; | |
4764 | ||
4765 | *pm = m; | |
4766 | pm = &m->next; | |
252b5132 | 4767 | } |
8ded5a0f AM |
4768 | |
4769 | /* Look through the sections. We put sections in the same program | |
4770 | segment when the start of the second section can be placed within | |
4771 | a few bytes of the end of the first section. */ | |
4772 | last_hdr = NULL; | |
4773 | last_size = 0; | |
00bee008 | 4774 | hdr_index = 0; |
8ded5a0f | 4775 | writable = FALSE; |
2888249f | 4776 | executable = FALSE; |
8ded5a0f AM |
4777 | dynsec = bfd_get_section_by_name (abfd, ".dynamic"); |
4778 | if (dynsec != NULL | |
4779 | && (dynsec->flags & SEC_LOAD) == 0) | |
4780 | dynsec = NULL; | |
4781 | ||
64029e93 AM |
4782 | if ((abfd->flags & D_PAGED) == 0) |
4783 | phdr_in_segment = FALSE; | |
4784 | ||
8ded5a0f AM |
4785 | /* Deal with -Ttext or something similar such that the first section |
4786 | is not adjacent to the program headers. This is an | |
4787 | approximation, since at this point we don't know exactly how many | |
4788 | program headers we will need. */ | |
64029e93 | 4789 | if (phdr_in_segment && count > 0) |
252b5132 | 4790 | { |
64029e93 AM |
4791 | bfd_vma phdr_lma; |
4792 | bfd_boolean separate_phdr = FALSE; | |
4793 | ||
4794 | phdr_lma = (sections[0]->lma - phdr_size) & addr_mask & -maxpagesize; | |
4795 | if (info != NULL | |
4796 | && info->separate_code | |
4797 | && (sections[0]->flags & SEC_CODE) != 0) | |
1a9ccd70 | 4798 | { |
64029e93 AM |
4799 | /* If data sections should be separate from code and |
4800 | thus not executable, and the first section is | |
4801 | executable then put the file and program headers in | |
4802 | their own PT_LOAD. */ | |
4803 | separate_phdr = TRUE; | |
4804 | if ((((phdr_lma + phdr_size - 1) & addr_mask & -maxpagesize) | |
4805 | == (sections[0]->lma & addr_mask & -maxpagesize))) | |
4806 | { | |
4807 | /* The file and program headers are currently on the | |
4808 | same page as the first section. Put them on the | |
4809 | previous page if we can. */ | |
4810 | if (phdr_lma >= maxpagesize) | |
4811 | phdr_lma -= maxpagesize; | |
4812 | else | |
4813 | separate_phdr = FALSE; | |
4814 | } | |
4815 | } | |
4816 | if ((sections[0]->lma & addr_mask) < phdr_lma | |
4817 | || (sections[0]->lma & addr_mask) < phdr_size) | |
4818 | /* If file and program headers would be placed at the end | |
4819 | of memory then it's probably better to omit them. */ | |
4820 | phdr_in_segment = FALSE; | |
4821 | else if (phdr_lma < wrap_to) | |
4822 | /* If a section wraps around to where we'll be placing | |
4823 | file and program headers, then the headers will be | |
4824 | overwritten. */ | |
4825 | phdr_in_segment = FALSE; | |
4826 | else if (separate_phdr) | |
4827 | { | |
4828 | m = make_mapping (abfd, sections, 0, 0, phdr_in_segment); | |
4829 | if (m == NULL) | |
4830 | goto error_return; | |
4831 | m->p_paddr = phdr_lma; | |
4832 | m->p_vaddr_offset | |
4833 | = (sections[0]->vma - phdr_size) & addr_mask & -maxpagesize; | |
4834 | m->p_paddr_valid = 1; | |
4835 | *pm = m; | |
4836 | pm = &m->next; | |
4837 | phdr_in_segment = FALSE; | |
1a9ccd70 | 4838 | } |
252b5132 RH |
4839 | } |
4840 | ||
8ded5a0f | 4841 | for (i = 0, hdrpp = sections; i < count; i++, hdrpp++) |
252b5132 | 4842 | { |
8ded5a0f AM |
4843 | asection *hdr; |
4844 | bfd_boolean new_segment; | |
4845 | ||
4846 | hdr = *hdrpp; | |
4847 | ||
4848 | /* See if this section and the last one will fit in the same | |
4849 | segment. */ | |
4850 | ||
4851 | if (last_hdr == NULL) | |
4852 | { | |
4853 | /* If we don't have a segment yet, then we don't need a new | |
4854 | one (we build the last one after this loop). */ | |
4855 | new_segment = FALSE; | |
4856 | } | |
4857 | else if (last_hdr->lma - last_hdr->vma != hdr->lma - hdr->vma) | |
4858 | { | |
4859 | /* If this section has a different relation between the | |
4860 | virtual address and the load address, then we need a new | |
4861 | segment. */ | |
4862 | new_segment = TRUE; | |
4863 | } | |
b5599592 AM |
4864 | else if (hdr->lma < last_hdr->lma + last_size |
4865 | || last_hdr->lma + last_size < last_hdr->lma) | |
4866 | { | |
4867 | /* If this section has a load address that makes it overlap | |
4868 | the previous section, then we need a new segment. */ | |
4869 | new_segment = TRUE; | |
4870 | } | |
76cb3a89 AM |
4871 | else if ((abfd->flags & D_PAGED) != 0 |
4872 | && (((last_hdr->lma + last_size - 1) & -maxpagesize) | |
4873 | == (hdr->lma & -maxpagesize))) | |
4874 | { | |
4875 | /* If we are demand paged then we can't map two disk | |
4876 | pages onto the same memory page. */ | |
4877 | new_segment = FALSE; | |
4878 | } | |
39948a60 NC |
4879 | /* In the next test we have to be careful when last_hdr->lma is close |
4880 | to the end of the address space. If the aligned address wraps | |
4881 | around to the start of the address space, then there are no more | |
4882 | pages left in memory and it is OK to assume that the current | |
4883 | section can be included in the current segment. */ | |
76cb3a89 AM |
4884 | else if ((BFD_ALIGN (last_hdr->lma + last_size, maxpagesize) |
4885 | + maxpagesize > last_hdr->lma) | |
4886 | && (BFD_ALIGN (last_hdr->lma + last_size, maxpagesize) | |
4887 | + maxpagesize <= hdr->lma)) | |
8ded5a0f AM |
4888 | { |
4889 | /* If putting this section in this segment would force us to | |
4890 | skip a page in the segment, then we need a new segment. */ | |
4891 | new_segment = TRUE; | |
4892 | } | |
4893 | else if ((last_hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0 | |
76cb3a89 | 4894 | && (hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) != 0) |
8ded5a0f | 4895 | { |
e5654c0f AM |
4896 | /* We don't want to put a loaded section after a |
4897 | nonloaded (ie. bss style) section in the same segment | |
4898 | as that will force the non-loaded section to be loaded. | |
76cb3a89 | 4899 | Consider .tbss sections as loaded for this purpose. */ |
8ded5a0f AM |
4900 | new_segment = TRUE; |
4901 | } | |
4902 | else if ((abfd->flags & D_PAGED) == 0) | |
4903 | { | |
4904 | /* If the file is not demand paged, which means that we | |
4905 | don't require the sections to be correctly aligned in the | |
4906 | file, then there is no other reason for a new segment. */ | |
4907 | new_segment = FALSE; | |
4908 | } | |
2888249f L |
4909 | else if (info != NULL |
4910 | && info->separate_code | |
4911 | && executable != ((hdr->flags & SEC_CODE) != 0)) | |
4912 | { | |
4913 | new_segment = TRUE; | |
4914 | } | |
8ded5a0f | 4915 | else if (! writable |
76cb3a89 | 4916 | && (hdr->flags & SEC_READONLY) == 0) |
8ded5a0f AM |
4917 | { |
4918 | /* We don't want to put a writable section in a read only | |
76cb3a89 | 4919 | segment. */ |
8ded5a0f AM |
4920 | new_segment = TRUE; |
4921 | } | |
4922 | else | |
4923 | { | |
4924 | /* Otherwise, we can use the same segment. */ | |
4925 | new_segment = FALSE; | |
4926 | } | |
4927 | ||
2889e75b | 4928 | /* Allow interested parties a chance to override our decision. */ |
ceae84aa AM |
4929 | if (last_hdr != NULL |
4930 | && info != NULL | |
4931 | && info->callbacks->override_segment_assignment != NULL) | |
4932 | new_segment | |
4933 | = info->callbacks->override_segment_assignment (info, abfd, hdr, | |
4934 | last_hdr, | |
4935 | new_segment); | |
2889e75b | 4936 | |
8ded5a0f AM |
4937 | if (! new_segment) |
4938 | { | |
4939 | if ((hdr->flags & SEC_READONLY) == 0) | |
4940 | writable = TRUE; | |
2888249f L |
4941 | if ((hdr->flags & SEC_CODE) != 0) |
4942 | executable = TRUE; | |
8ded5a0f AM |
4943 | last_hdr = hdr; |
4944 | /* .tbss sections effectively have zero size. */ | |
dbc88fc1 | 4945 | last_size = !IS_TBSS (hdr) ? hdr->size : 0; |
8ded5a0f AM |
4946 | continue; |
4947 | } | |
4948 | ||
4949 | /* We need a new program segment. We must create a new program | |
00bee008 | 4950 | header holding all the sections from hdr_index until hdr. */ |
8ded5a0f | 4951 | |
00bee008 | 4952 | m = make_mapping (abfd, sections, hdr_index, i, phdr_in_segment); |
8ded5a0f AM |
4953 | if (m == NULL) |
4954 | goto error_return; | |
4955 | ||
4956 | *pm = m; | |
4957 | pm = &m->next; | |
4958 | ||
252b5132 | 4959 | if ((hdr->flags & SEC_READONLY) == 0) |
b34976b6 | 4960 | writable = TRUE; |
8ded5a0f AM |
4961 | else |
4962 | writable = FALSE; | |
4963 | ||
2888249f L |
4964 | if ((hdr->flags & SEC_CODE) == 0) |
4965 | executable = FALSE; | |
4966 | else | |
4967 | executable = TRUE; | |
4968 | ||
baaff79e JJ |
4969 | last_hdr = hdr; |
4970 | /* .tbss sections effectively have zero size. */ | |
dbc88fc1 | 4971 | last_size = !IS_TBSS (hdr) ? hdr->size : 0; |
00bee008 | 4972 | hdr_index = i; |
8ded5a0f | 4973 | phdr_in_segment = FALSE; |
252b5132 RH |
4974 | } |
4975 | ||
86b2281f AM |
4976 | /* Create a final PT_LOAD program segment, but not if it's just |
4977 | for .tbss. */ | |
4978 | if (last_hdr != NULL | |
00bee008 | 4979 | && (i - hdr_index != 1 |
dbc88fc1 | 4980 | || !IS_TBSS (last_hdr))) |
8ded5a0f | 4981 | { |
00bee008 | 4982 | m = make_mapping (abfd, sections, hdr_index, i, phdr_in_segment); |
8ded5a0f AM |
4983 | if (m == NULL) |
4984 | goto error_return; | |
252b5132 | 4985 | |
8ded5a0f AM |
4986 | *pm = m; |
4987 | pm = &m->next; | |
4988 | } | |
252b5132 | 4989 | |
8ded5a0f AM |
4990 | /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */ |
4991 | if (dynsec != NULL) | |
4992 | { | |
4993 | m = _bfd_elf_make_dynamic_segment (abfd, dynsec); | |
4994 | if (m == NULL) | |
4995 | goto error_return; | |
4996 | *pm = m; | |
4997 | pm = &m->next; | |
4998 | } | |
252b5132 | 4999 | |
23e463ed | 5000 | /* For each batch of consecutive loadable SHT_NOTE sections, |
1c5265b5 JJ |
5001 | add a PT_NOTE segment. We don't use bfd_get_section_by_name, |
5002 | because if we link together nonloadable .note sections and | |
5003 | loadable .note sections, we will generate two .note sections | |
23e463ed | 5004 | in the output file. */ |
8ded5a0f AM |
5005 | for (s = abfd->sections; s != NULL; s = s->next) |
5006 | { | |
5007 | if ((s->flags & SEC_LOAD) != 0 | |
23e463ed | 5008 | && elf_section_type (s) == SHT_NOTE) |
8ded5a0f | 5009 | { |
1c5265b5 | 5010 | asection *s2; |
23e463ed | 5011 | unsigned int alignment_power = s->alignment_power; |
91d6fa6a NC |
5012 | |
5013 | count = 1; | |
23e463ed L |
5014 | for (s2 = s; s2->next != NULL; s2 = s2->next) |
5015 | { | |
5016 | if (s2->next->alignment_power == alignment_power | |
5017 | && (s2->next->flags & SEC_LOAD) != 0 | |
5018 | && elf_section_type (s2->next) == SHT_NOTE | |
5019 | && align_power (s2->lma + s2->size, | |
5020 | alignment_power) | |
5021 | == s2->next->lma) | |
5022 | count++; | |
5023 | else | |
5024 | break; | |
5025 | } | |
00bee008 AM |
5026 | amt = sizeof (struct elf_segment_map) - sizeof (asection *); |
5027 | amt += count * sizeof (asection *); | |
a50b1753 | 5028 | m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); |
8ded5a0f AM |
5029 | if (m == NULL) |
5030 | goto error_return; | |
5031 | m->next = NULL; | |
5032 | m->p_type = PT_NOTE; | |
1c5265b5 JJ |
5033 | m->count = count; |
5034 | while (count > 1) | |
5035 | { | |
5036 | m->sections[m->count - count--] = s; | |
5037 | BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0); | |
5038 | s = s->next; | |
5039 | } | |
5040 | m->sections[m->count - 1] = s; | |
5041 | BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0); | |
8ded5a0f AM |
5042 | *pm = m; |
5043 | pm = &m->next; | |
5044 | } | |
5045 | if (s->flags & SEC_THREAD_LOCAL) | |
5046 | { | |
5047 | if (! tls_count) | |
5048 | first_tls = s; | |
5049 | tls_count++; | |
5050 | } | |
a91e1603 L |
5051 | if (first_mbind == NULL |
5052 | && (elf_section_flags (s) & SHF_GNU_MBIND) != 0) | |
5053 | first_mbind = s; | |
8ded5a0f | 5054 | } |
252b5132 | 5055 | |
8ded5a0f AM |
5056 | /* If there are any SHF_TLS output sections, add PT_TLS segment. */ |
5057 | if (tls_count > 0) | |
5058 | { | |
00bee008 AM |
5059 | amt = sizeof (struct elf_segment_map) - sizeof (asection *); |
5060 | amt += tls_count * sizeof (asection *); | |
a50b1753 | 5061 | m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); |
8ded5a0f AM |
5062 | if (m == NULL) |
5063 | goto error_return; | |
5064 | m->next = NULL; | |
5065 | m->p_type = PT_TLS; | |
5066 | m->count = tls_count; | |
5067 | /* Mandated PF_R. */ | |
5068 | m->p_flags = PF_R; | |
5069 | m->p_flags_valid = 1; | |
d923cae0 | 5070 | s = first_tls; |
91d6fa6a | 5071 | for (i = 0; i < (unsigned int) tls_count; ++i) |
8ded5a0f | 5072 | { |
d923cae0 L |
5073 | if ((s->flags & SEC_THREAD_LOCAL) == 0) |
5074 | { | |
5075 | _bfd_error_handler | |
871b3ab2 | 5076 | (_("%pB: TLS sections are not adjacent:"), abfd); |
d923cae0 L |
5077 | s = first_tls; |
5078 | i = 0; | |
5079 | while (i < (unsigned int) tls_count) | |
5080 | { | |
5081 | if ((s->flags & SEC_THREAD_LOCAL) != 0) | |
5082 | { | |
871b3ab2 | 5083 | _bfd_error_handler (_(" TLS: %pA"), s); |
d923cae0 L |
5084 | i++; |
5085 | } | |
5086 | else | |
871b3ab2 | 5087 | _bfd_error_handler (_(" non-TLS: %pA"), s); |
d923cae0 L |
5088 | s = s->next; |
5089 | } | |
5090 | bfd_set_error (bfd_error_bad_value); | |
5091 | goto error_return; | |
5092 | } | |
5093 | m->sections[i] = s; | |
5094 | s = s->next; | |
8ded5a0f | 5095 | } |
252b5132 | 5096 | |
8ded5a0f AM |
5097 | *pm = m; |
5098 | pm = &m->next; | |
5099 | } | |
252b5132 | 5100 | |
df3a023b AM |
5101 | if (first_mbind |
5102 | && (abfd->flags & D_PAGED) != 0 | |
5103 | && (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_mbind) != 0) | |
a91e1603 L |
5104 | for (s = first_mbind; s != NULL; s = s->next) |
5105 | if ((elf_section_flags (s) & SHF_GNU_MBIND) != 0 | |
df3a023b | 5106 | && elf_section_data (s)->this_hdr.sh_info <= PT_GNU_MBIND_NUM) |
a91e1603 L |
5107 | { |
5108 | /* Mandated PF_R. */ | |
5109 | unsigned long p_flags = PF_R; | |
5110 | if ((s->flags & SEC_READONLY) == 0) | |
5111 | p_flags |= PF_W; | |
5112 | if ((s->flags & SEC_CODE) != 0) | |
5113 | p_flags |= PF_X; | |
5114 | ||
5115 | amt = sizeof (struct elf_segment_map) + sizeof (asection *); | |
5116 | m = bfd_zalloc (abfd, amt); | |
5117 | if (m == NULL) | |
5118 | goto error_return; | |
5119 | m->next = NULL; | |
5120 | m->p_type = (PT_GNU_MBIND_LO | |
5121 | + elf_section_data (s)->this_hdr.sh_info); | |
5122 | m->count = 1; | |
5123 | m->p_flags_valid = 1; | |
5124 | m->sections[0] = s; | |
5125 | m->p_flags = p_flags; | |
5126 | ||
5127 | *pm = m; | |
5128 | pm = &m->next; | |
5129 | } | |
5130 | ||
0a59decb L |
5131 | s = bfd_get_section_by_name (abfd, |
5132 | NOTE_GNU_PROPERTY_SECTION_NAME); | |
5133 | if (s != NULL && s->size != 0) | |
5134 | { | |
5135 | amt = sizeof (struct elf_segment_map) + sizeof (asection *); | |
5136 | m = bfd_zalloc (abfd, amt); | |
5137 | if (m == NULL) | |
5138 | goto error_return; | |
5139 | m->next = NULL; | |
5140 | m->p_type = PT_GNU_PROPERTY; | |
5141 | m->count = 1; | |
5142 | m->p_flags_valid = 1; | |
5143 | m->sections[0] = s; | |
5144 | m->p_flags = PF_R; | |
5145 | *pm = m; | |
5146 | pm = &m->next; | |
5147 | } | |
5148 | ||
8ded5a0f AM |
5149 | /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME |
5150 | segment. */ | |
12bd6957 | 5151 | eh_frame_hdr = elf_eh_frame_hdr (abfd); |
8ded5a0f AM |
5152 | if (eh_frame_hdr != NULL |
5153 | && (eh_frame_hdr->output_section->flags & SEC_LOAD) != 0) | |
252b5132 | 5154 | { |
dc810e39 | 5155 | amt = sizeof (struct elf_segment_map); |
a50b1753 | 5156 | m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); |
252b5132 RH |
5157 | if (m == NULL) |
5158 | goto error_return; | |
5159 | m->next = NULL; | |
8ded5a0f | 5160 | m->p_type = PT_GNU_EH_FRAME; |
252b5132 | 5161 | m->count = 1; |
8ded5a0f | 5162 | m->sections[0] = eh_frame_hdr->output_section; |
252b5132 RH |
5163 | |
5164 | *pm = m; | |
5165 | pm = &m->next; | |
5166 | } | |
13ae64f3 | 5167 | |
12bd6957 | 5168 | if (elf_stack_flags (abfd)) |
13ae64f3 | 5169 | { |
8ded5a0f | 5170 | amt = sizeof (struct elf_segment_map); |
a50b1753 | 5171 | m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); |
8ded5a0f AM |
5172 | if (m == NULL) |
5173 | goto error_return; | |
5174 | m->next = NULL; | |
2b05f1b7 | 5175 | m->p_type = PT_GNU_STACK; |
12bd6957 | 5176 | m->p_flags = elf_stack_flags (abfd); |
04c3a755 | 5177 | m->p_align = bed->stack_align; |
8ded5a0f | 5178 | m->p_flags_valid = 1; |
04c3a755 NS |
5179 | m->p_align_valid = m->p_align != 0; |
5180 | if (info->stacksize > 0) | |
5181 | { | |
5182 | m->p_size = info->stacksize; | |
5183 | m->p_size_valid = 1; | |
5184 | } | |
252b5132 | 5185 | |
8ded5a0f AM |
5186 | *pm = m; |
5187 | pm = &m->next; | |
5188 | } | |
65765700 | 5189 | |
ceae84aa | 5190 | if (info != NULL && info->relro) |
8ded5a0f | 5191 | { |
f210dcff L |
5192 | for (m = mfirst; m != NULL; m = m->next) |
5193 | { | |
3832a4d8 AM |
5194 | if (m->p_type == PT_LOAD |
5195 | && m->count != 0 | |
5196 | && m->sections[0]->vma >= info->relro_start | |
5197 | && m->sections[0]->vma < info->relro_end) | |
f210dcff | 5198 | { |
3832a4d8 AM |
5199 | i = m->count; |
5200 | while (--i != (unsigned) -1) | |
5201 | if ((m->sections[i]->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) | |
5202 | == (SEC_LOAD | SEC_HAS_CONTENTS)) | |
5203 | break; | |
5204 | ||
43a8475c | 5205 | if (i != (unsigned) -1) |
f210dcff L |
5206 | break; |
5207 | } | |
be01b344 | 5208 | } |
f210dcff L |
5209 | |
5210 | /* Make a PT_GNU_RELRO segment only when it isn't empty. */ | |
5211 | if (m != NULL) | |
5212 | { | |
5213 | amt = sizeof (struct elf_segment_map); | |
a50b1753 | 5214 | m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); |
f210dcff L |
5215 | if (m == NULL) |
5216 | goto error_return; | |
5217 | m->next = NULL; | |
5218 | m->p_type = PT_GNU_RELRO; | |
f210dcff L |
5219 | *pm = m; |
5220 | pm = &m->next; | |
5221 | } | |
8ded5a0f | 5222 | } |
9ee5e499 | 5223 | |
8ded5a0f | 5224 | free (sections); |
12bd6957 | 5225 | elf_seg_map (abfd) = mfirst; |
9ee5e499 JJ |
5226 | } |
5227 | ||
3dea8fca | 5228 | if (!elf_modify_segment_map (abfd, info, no_user_phdrs)) |
8ded5a0f | 5229 | return FALSE; |
8c37241b | 5230 | |
12bd6957 | 5231 | for (count = 0, m = elf_seg_map (abfd); m != NULL; m = m->next) |
8ded5a0f | 5232 | ++count; |
12bd6957 | 5233 | elf_program_header_size (abfd) = count * bed->s->sizeof_phdr; |
252b5132 | 5234 | |
b34976b6 | 5235 | return TRUE; |
252b5132 RH |
5236 | |
5237 | error_return: | |
5238 | if (sections != NULL) | |
5239 | free (sections); | |
b34976b6 | 5240 | return FALSE; |
252b5132 RH |
5241 | } |
5242 | ||
5243 | /* Sort sections by address. */ | |
5244 | ||
5245 | static int | |
217aa764 | 5246 | elf_sort_sections (const void *arg1, const void *arg2) |
252b5132 RH |
5247 | { |
5248 | const asection *sec1 = *(const asection **) arg1; | |
5249 | const asection *sec2 = *(const asection **) arg2; | |
eecdbe52 | 5250 | bfd_size_type size1, size2; |
252b5132 RH |
5251 | |
5252 | /* Sort by LMA first, since this is the address used to | |
5253 | place the section into a segment. */ | |
5254 | if (sec1->lma < sec2->lma) | |
5255 | return -1; | |
5256 | else if (sec1->lma > sec2->lma) | |
5257 | return 1; | |
5258 | ||
5259 | /* Then sort by VMA. Normally the LMA and the VMA will be | |
5260 | the same, and this will do nothing. */ | |
5261 | if (sec1->vma < sec2->vma) | |
5262 | return -1; | |
5263 | else if (sec1->vma > sec2->vma) | |
5264 | return 1; | |
5265 | ||
5266 | /* Put !SEC_LOAD sections after SEC_LOAD ones. */ | |
5267 | ||
07c6e936 | 5268 | #define TOEND(x) (((x)->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0) |
252b5132 RH |
5269 | |
5270 | if (TOEND (sec1)) | |
5271 | { | |
5272 | if (TOEND (sec2)) | |
00a7cdc5 | 5273 | { |
67ce483b | 5274 | /* If the indices are the same, do not return 0 |
00a7cdc5 NC |
5275 | here, but continue to try the next comparison. */ |
5276 | if (sec1->target_index - sec2->target_index != 0) | |
5277 | return sec1->target_index - sec2->target_index; | |
5278 | } | |
252b5132 RH |
5279 | else |
5280 | return 1; | |
5281 | } | |
00a7cdc5 | 5282 | else if (TOEND (sec2)) |
252b5132 RH |
5283 | return -1; |
5284 | ||
5285 | #undef TOEND | |
5286 | ||
00a7cdc5 NC |
5287 | /* Sort by size, to put zero sized sections |
5288 | before others at the same address. */ | |
252b5132 | 5289 | |
eea6121a AM |
5290 | size1 = (sec1->flags & SEC_LOAD) ? sec1->size : 0; |
5291 | size2 = (sec2->flags & SEC_LOAD) ? sec2->size : 0; | |
eecdbe52 JJ |
5292 | |
5293 | if (size1 < size2) | |
252b5132 | 5294 | return -1; |
eecdbe52 | 5295 | if (size1 > size2) |
252b5132 RH |
5296 | return 1; |
5297 | ||
5298 | return sec1->target_index - sec2->target_index; | |
5299 | } | |
5300 | ||
340b6d91 AC |
5301 | /* Ian Lance Taylor writes: |
5302 | ||
5303 | We shouldn't be using % with a negative signed number. That's just | |
5304 | not good. We have to make sure either that the number is not | |
5305 | negative, or that the number has an unsigned type. When the types | |
5306 | are all the same size they wind up as unsigned. When file_ptr is a | |
5307 | larger signed type, the arithmetic winds up as signed long long, | |
5308 | which is wrong. | |
5309 | ||
5310 | What we're trying to say here is something like ``increase OFF by | |
5311 | the least amount that will cause it to be equal to the VMA modulo | |
5312 | the page size.'' */ | |
5313 | /* In other words, something like: | |
5314 | ||
5315 | vma_offset = m->sections[0]->vma % bed->maxpagesize; | |
5316 | off_offset = off % bed->maxpagesize; | |
5317 | if (vma_offset < off_offset) | |
5318 | adjustment = vma_offset + bed->maxpagesize - off_offset; | |
5319 | else | |
5320 | adjustment = vma_offset - off_offset; | |
08a40648 | 5321 | |
de194d85 | 5322 | which can be collapsed into the expression below. */ |
340b6d91 AC |
5323 | |
5324 | static file_ptr | |
5325 | vma_page_aligned_bias (bfd_vma vma, ufile_ptr off, bfd_vma maxpagesize) | |
5326 | { | |
dc9155b2 NC |
5327 | /* PR binutils/16199: Handle an alignment of zero. */ |
5328 | if (maxpagesize == 0) | |
5329 | maxpagesize = 1; | |
340b6d91 AC |
5330 | return ((vma - off) % maxpagesize); |
5331 | } | |
5332 | ||
6d33f217 L |
5333 | static void |
5334 | print_segment_map (const struct elf_segment_map *m) | |
5335 | { | |
5336 | unsigned int j; | |
5337 | const char *pt = get_segment_type (m->p_type); | |
5338 | char buf[32]; | |
5339 | ||
5340 | if (pt == NULL) | |
5341 | { | |
5342 | if (m->p_type >= PT_LOPROC && m->p_type <= PT_HIPROC) | |
5343 | sprintf (buf, "LOPROC+%7.7x", | |
5344 | (unsigned int) (m->p_type - PT_LOPROC)); | |
5345 | else if (m->p_type >= PT_LOOS && m->p_type <= PT_HIOS) | |
5346 | sprintf (buf, "LOOS+%7.7x", | |
5347 | (unsigned int) (m->p_type - PT_LOOS)); | |
5348 | else | |
5349 | snprintf (buf, sizeof (buf), "%8.8x", | |
5350 | (unsigned int) m->p_type); | |
5351 | pt = buf; | |
5352 | } | |
4a97a0e5 | 5353 | fflush (stdout); |
6d33f217 L |
5354 | fprintf (stderr, "%s:", pt); |
5355 | for (j = 0; j < m->count; j++) | |
5356 | fprintf (stderr, " %s", m->sections [j]->name); | |
5357 | putc ('\n',stderr); | |
4a97a0e5 | 5358 | fflush (stderr); |
6d33f217 L |
5359 | } |
5360 | ||
32812159 AM |
5361 | static bfd_boolean |
5362 | write_zeros (bfd *abfd, file_ptr pos, bfd_size_type len) | |
5363 | { | |
5364 | void *buf; | |
5365 | bfd_boolean ret; | |
5366 | ||
5367 | if (bfd_seek (abfd, pos, SEEK_SET) != 0) | |
5368 | return FALSE; | |
5369 | buf = bfd_zmalloc (len); | |
5370 | if (buf == NULL) | |
5371 | return FALSE; | |
5372 | ret = bfd_bwrite (buf, len, abfd) == len; | |
5373 | free (buf); | |
5374 | return ret; | |
5375 | } | |
5376 | ||
252b5132 RH |
5377 | /* Assign file positions to the sections based on the mapping from |
5378 | sections to segments. This function also sets up some fields in | |
f3520d2f | 5379 | the file header. */ |
252b5132 | 5380 | |
b34976b6 | 5381 | static bfd_boolean |
f3520d2f AM |
5382 | assign_file_positions_for_load_sections (bfd *abfd, |
5383 | struct bfd_link_info *link_info) | |
252b5132 RH |
5384 | { |
5385 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
252b5132 | 5386 | struct elf_segment_map *m; |
252b5132 | 5387 | Elf_Internal_Phdr *phdrs; |
252b5132 | 5388 | Elf_Internal_Phdr *p; |
02bf8d82 | 5389 | file_ptr off; |
3f570048 | 5390 | bfd_size_type maxpagesize; |
a8c75b76 | 5391 | unsigned int pt_load_count = 0; |
f3520d2f | 5392 | unsigned int alloc; |
0920dee7 | 5393 | unsigned int i, j; |
2b0bc088 | 5394 | bfd_vma header_pad = 0; |
252b5132 | 5395 | |
e36284ab | 5396 | if (link_info == NULL |
ceae84aa | 5397 | && !_bfd_elf_map_sections_to_segments (abfd, link_info)) |
8ded5a0f | 5398 | return FALSE; |
252b5132 | 5399 | |
8ded5a0f | 5400 | alloc = 0; |
12bd6957 | 5401 | for (m = elf_seg_map (abfd); m != NULL; m = m->next) |
2b0bc088 NC |
5402 | { |
5403 | ++alloc; | |
5404 | if (m->header_size) | |
5405 | header_pad = m->header_size; | |
5406 | } | |
252b5132 | 5407 | |
82f2dbf7 NC |
5408 | if (alloc) |
5409 | { | |
5410 | elf_elfheader (abfd)->e_phoff = bed->s->sizeof_ehdr; | |
5411 | elf_elfheader (abfd)->e_phentsize = bed->s->sizeof_phdr; | |
5412 | } | |
5413 | else | |
5414 | { | |
5415 | /* PR binutils/12467. */ | |
5416 | elf_elfheader (abfd)->e_phoff = 0; | |
5417 | elf_elfheader (abfd)->e_phentsize = 0; | |
5418 | } | |
d324f6d6 | 5419 | |
8ded5a0f | 5420 | elf_elfheader (abfd)->e_phnum = alloc; |
252b5132 | 5421 | |
12bd6957 AM |
5422 | if (elf_program_header_size (abfd) == (bfd_size_type) -1) |
5423 | elf_program_header_size (abfd) = alloc * bed->s->sizeof_phdr; | |
8ded5a0f | 5424 | else |
12bd6957 | 5425 | BFD_ASSERT (elf_program_header_size (abfd) |
59e0647f | 5426 | >= alloc * bed->s->sizeof_phdr); |
252b5132 RH |
5427 | |
5428 | if (alloc == 0) | |
f3520d2f | 5429 | { |
12bd6957 | 5430 | elf_next_file_pos (abfd) = bed->s->sizeof_ehdr; |
8ded5a0f | 5431 | return TRUE; |
f3520d2f | 5432 | } |
252b5132 | 5433 | |
12bd6957 | 5434 | /* We're writing the size in elf_program_header_size (abfd), |
57268894 HPN |
5435 | see assign_file_positions_except_relocs, so make sure we have |
5436 | that amount allocated, with trailing space cleared. | |
12bd6957 AM |
5437 | The variable alloc contains the computed need, while |
5438 | elf_program_header_size (abfd) contains the size used for the | |
57268894 HPN |
5439 | layout. |
5440 | See ld/emultempl/elf-generic.em:gld${EMULATION_NAME}_map_segments | |
5441 | where the layout is forced to according to a larger size in the | |
5442 | last iterations for the testcase ld-elf/header. */ | |
12bd6957 | 5443 | BFD_ASSERT (elf_program_header_size (abfd) % bed->s->sizeof_phdr |
57268894 | 5444 | == 0); |
a50b1753 NC |
5445 | phdrs = (Elf_Internal_Phdr *) |
5446 | bfd_zalloc2 (abfd, | |
07d6d2b8 AM |
5447 | (elf_program_header_size (abfd) / bed->s->sizeof_phdr), |
5448 | sizeof (Elf_Internal_Phdr)); | |
f3520d2f | 5449 | elf_tdata (abfd)->phdr = phdrs; |
252b5132 | 5450 | if (phdrs == NULL) |
b34976b6 | 5451 | return FALSE; |
252b5132 | 5452 | |
3f570048 AM |
5453 | maxpagesize = 1; |
5454 | if ((abfd->flags & D_PAGED) != 0) | |
5455 | maxpagesize = bed->maxpagesize; | |
5456 | ||
252b5132 RH |
5457 | off = bed->s->sizeof_ehdr; |
5458 | off += alloc * bed->s->sizeof_phdr; | |
2b0bc088 NC |
5459 | if (header_pad < (bfd_vma) off) |
5460 | header_pad = 0; | |
5461 | else | |
5462 | header_pad -= off; | |
5463 | off += header_pad; | |
252b5132 | 5464 | |
12bd6957 | 5465 | for (m = elf_seg_map (abfd), p = phdrs, j = 0; |
252b5132 | 5466 | m != NULL; |
0920dee7 | 5467 | m = m->next, p++, j++) |
252b5132 | 5468 | { |
252b5132 | 5469 | asection **secpp; |
bf988460 AM |
5470 | bfd_vma off_adjust; |
5471 | bfd_boolean no_contents; | |
252b5132 RH |
5472 | |
5473 | /* If elf_segment_map is not from map_sections_to_segments, the | |
08a40648 | 5474 | sections may not be correctly ordered. NOTE: sorting should |
52e9b619 MS |
5475 | not be done to the PT_NOTE section of a corefile, which may |
5476 | contain several pseudo-sections artificially created by bfd. | |
5477 | Sorting these pseudo-sections breaks things badly. */ | |
47d9a591 AM |
5478 | if (m->count > 1 |
5479 | && !(elf_elfheader (abfd)->e_type == ET_CORE | |
52e9b619 | 5480 | && m->p_type == PT_NOTE)) |
252b5132 RH |
5481 | qsort (m->sections, (size_t) m->count, sizeof (asection *), |
5482 | elf_sort_sections); | |
5483 | ||
b301b248 AM |
5484 | /* An ELF segment (described by Elf_Internal_Phdr) may contain a |
5485 | number of sections with contents contributing to both p_filesz | |
5486 | and p_memsz, followed by a number of sections with no contents | |
5487 | that just contribute to p_memsz. In this loop, OFF tracks next | |
02bf8d82 | 5488 | available file offset for PT_LOAD and PT_NOTE segments. */ |
252b5132 | 5489 | p->p_type = m->p_type; |
28a7f3e7 | 5490 | p->p_flags = m->p_flags; |
252b5132 | 5491 | |
3f570048 | 5492 | if (m->count == 0) |
5d695627 | 5493 | p->p_vaddr = m->p_vaddr_offset; |
3f570048 | 5494 | else |
5d695627 | 5495 | p->p_vaddr = m->sections[0]->vma + m->p_vaddr_offset; |
3f570048 AM |
5496 | |
5497 | if (m->p_paddr_valid) | |
5498 | p->p_paddr = m->p_paddr; | |
5499 | else if (m->count == 0) | |
5500 | p->p_paddr = 0; | |
5501 | else | |
5d695627 | 5502 | p->p_paddr = m->sections[0]->lma + m->p_vaddr_offset; |
3f570048 AM |
5503 | |
5504 | if (p->p_type == PT_LOAD | |
5505 | && (abfd->flags & D_PAGED) != 0) | |
5506 | { | |
5507 | /* p_align in demand paged PT_LOAD segments effectively stores | |
5508 | the maximum page size. When copying an executable with | |
5509 | objcopy, we set m->p_align from the input file. Use this | |
5510 | value for maxpagesize rather than bed->maxpagesize, which | |
5511 | may be different. Note that we use maxpagesize for PT_TLS | |
5512 | segment alignment later in this function, so we are relying | |
5513 | on at least one PT_LOAD segment appearing before a PT_TLS | |
5514 | segment. */ | |
5515 | if (m->p_align_valid) | |
5516 | maxpagesize = m->p_align; | |
5517 | ||
5518 | p->p_align = maxpagesize; | |
a8c75b76 | 5519 | pt_load_count += 1; |
3f570048 | 5520 | } |
3271a814 NS |
5521 | else if (m->p_align_valid) |
5522 | p->p_align = m->p_align; | |
e970b90a DJ |
5523 | else if (m->count == 0) |
5524 | p->p_align = 1 << bed->s->log_file_align; | |
3f570048 AM |
5525 | else |
5526 | p->p_align = 0; | |
5527 | ||
bf988460 AM |
5528 | no_contents = FALSE; |
5529 | off_adjust = 0; | |
252b5132 | 5530 | if (p->p_type == PT_LOAD |
b301b248 | 5531 | && m->count > 0) |
252b5132 | 5532 | { |
b301b248 | 5533 | bfd_size_type align; |
a49e53ed | 5534 | unsigned int align_power = 0; |
b301b248 | 5535 | |
3271a814 NS |
5536 | if (m->p_align_valid) |
5537 | align = p->p_align; | |
5538 | else | |
252b5132 | 5539 | { |
3271a814 NS |
5540 | for (i = 0, secpp = m->sections; i < m->count; i++, secpp++) |
5541 | { | |
5542 | unsigned int secalign; | |
08a40648 | 5543 | |
fd361982 | 5544 | secalign = bfd_section_alignment (*secpp); |
3271a814 NS |
5545 | if (secalign > align_power) |
5546 | align_power = secalign; | |
5547 | } | |
5548 | align = (bfd_size_type) 1 << align_power; | |
5549 | if (align < maxpagesize) | |
5550 | align = maxpagesize; | |
b301b248 | 5551 | } |
252b5132 | 5552 | |
02bf8d82 AM |
5553 | for (i = 0; i < m->count; i++) |
5554 | if ((m->sections[i]->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0) | |
5555 | /* If we aren't making room for this section, then | |
5556 | it must be SHT_NOBITS regardless of what we've | |
5557 | set via struct bfd_elf_special_section. */ | |
5558 | elf_section_type (m->sections[i]) = SHT_NOBITS; | |
5559 | ||
bf988460 | 5560 | /* Find out whether this segment contains any loadable |
aea274d3 AM |
5561 | sections. */ |
5562 | no_contents = TRUE; | |
5563 | for (i = 0; i < m->count; i++) | |
5564 | if (elf_section_type (m->sections[i]) != SHT_NOBITS) | |
5565 | { | |
5566 | no_contents = FALSE; | |
5567 | break; | |
5568 | } | |
bf988460 | 5569 | |
85cfcbfb | 5570 | off_adjust = vma_page_aligned_bias (p->p_vaddr, off, align); |
a8c75b76 AM |
5571 | |
5572 | /* Broken hardware and/or kernel require that files do not | |
5573 | map the same page with different permissions on some hppa | |
5574 | processors. */ | |
5575 | if (pt_load_count > 1 | |
5576 | && bed->no_page_alias | |
5577 | && (off & (maxpagesize - 1)) != 0 | |
5578 | && (off & -maxpagesize) == ((off + off_adjust) & -maxpagesize)) | |
5579 | off_adjust += maxpagesize; | |
bf988460 AM |
5580 | off += off_adjust; |
5581 | if (no_contents) | |
5582 | { | |
5583 | /* We shouldn't need to align the segment on disk since | |
5584 | the segment doesn't need file space, but the gABI | |
5585 | arguably requires the alignment and glibc ld.so | |
5586 | checks it. So to comply with the alignment | |
5587 | requirement but not waste file space, we adjust | |
5588 | p_offset for just this segment. (OFF_ADJUST is | |
5589 | subtracted from OFF later.) This may put p_offset | |
5590 | past the end of file, but that shouldn't matter. */ | |
5591 | } | |
5592 | else | |
5593 | off_adjust = 0; | |
252b5132 | 5594 | } |
b1a6d0b1 NC |
5595 | /* Make sure the .dynamic section is the first section in the |
5596 | PT_DYNAMIC segment. */ | |
5597 | else if (p->p_type == PT_DYNAMIC | |
5598 | && m->count > 1 | |
5599 | && strcmp (m->sections[0]->name, ".dynamic") != 0) | |
5600 | { | |
5601 | _bfd_error_handler | |
871b3ab2 | 5602 | (_("%pB: The first section in the PT_DYNAMIC segment" |
63a5468a | 5603 | " is not the .dynamic section"), |
b301b248 | 5604 | abfd); |
b1a6d0b1 NC |
5605 | bfd_set_error (bfd_error_bad_value); |
5606 | return FALSE; | |
5607 | } | |
3f001e84 JK |
5608 | /* Set the note section type to SHT_NOTE. */ |
5609 | else if (p->p_type == PT_NOTE) | |
5610 | for (i = 0; i < m->count; i++) | |
5611 | elf_section_type (m->sections[i]) = SHT_NOTE; | |
252b5132 | 5612 | |
252b5132 RH |
5613 | p->p_offset = 0; |
5614 | p->p_filesz = 0; | |
5615 | p->p_memsz = 0; | |
5616 | ||
5617 | if (m->includes_filehdr) | |
5618 | { | |
bf988460 | 5619 | if (!m->p_flags_valid) |
252b5132 | 5620 | p->p_flags |= PF_R; |
252b5132 RH |
5621 | p->p_filesz = bed->s->sizeof_ehdr; |
5622 | p->p_memsz = bed->s->sizeof_ehdr; | |
5623 | if (m->count > 0) | |
5624 | { | |
9ab82472 L |
5625 | if (p->p_vaddr < (bfd_vma) off |
5626 | || (!m->p_paddr_valid | |
5627 | && p->p_paddr < (bfd_vma) off)) | |
252b5132 | 5628 | { |
4eca0228 | 5629 | _bfd_error_handler |
9793eb77 | 5630 | (_("%pB: not enough room for program headers," |
63a5468a | 5631 | " try linking with -N"), |
b301b248 | 5632 | abfd); |
252b5132 | 5633 | bfd_set_error (bfd_error_bad_value); |
b34976b6 | 5634 | return FALSE; |
252b5132 RH |
5635 | } |
5636 | ||
5637 | p->p_vaddr -= off; | |
bf988460 | 5638 | if (!m->p_paddr_valid) |
252b5132 RH |
5639 | p->p_paddr -= off; |
5640 | } | |
252b5132 RH |
5641 | } |
5642 | ||
5643 | if (m->includes_phdrs) | |
5644 | { | |
bf988460 | 5645 | if (!m->p_flags_valid) |
252b5132 RH |
5646 | p->p_flags |= PF_R; |
5647 | ||
f3520d2f | 5648 | if (!m->includes_filehdr) |
252b5132 RH |
5649 | { |
5650 | p->p_offset = bed->s->sizeof_ehdr; | |
5651 | ||
5652 | if (m->count > 0) | |
5653 | { | |
252b5132 | 5654 | p->p_vaddr -= off - p->p_offset; |
bf988460 | 5655 | if (!m->p_paddr_valid) |
252b5132 RH |
5656 | p->p_paddr -= off - p->p_offset; |
5657 | } | |
252b5132 RH |
5658 | } |
5659 | ||
5660 | p->p_filesz += alloc * bed->s->sizeof_phdr; | |
5661 | p->p_memsz += alloc * bed->s->sizeof_phdr; | |
2b0bc088 NC |
5662 | if (m->count) |
5663 | { | |
5664 | p->p_filesz += header_pad; | |
5665 | p->p_memsz += header_pad; | |
5666 | } | |
252b5132 RH |
5667 | } |
5668 | ||
5669 | if (p->p_type == PT_LOAD | |
5670 | || (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core)) | |
5671 | { | |
bf988460 | 5672 | if (!m->includes_filehdr && !m->includes_phdrs) |
02bf8d82 | 5673 | p->p_offset = off; |
252b5132 RH |
5674 | else |
5675 | { | |
5676 | file_ptr adjust; | |
5677 | ||
5678 | adjust = off - (p->p_offset + p->p_filesz); | |
bf988460 AM |
5679 | if (!no_contents) |
5680 | p->p_filesz += adjust; | |
252b5132 RH |
5681 | p->p_memsz += adjust; |
5682 | } | |
5683 | } | |
5684 | ||
1ea63fd2 AM |
5685 | /* Set up p_filesz, p_memsz, p_align and p_flags from the section |
5686 | maps. Set filepos for sections in PT_LOAD segments, and in | |
5687 | core files, for sections in PT_NOTE segments. | |
5688 | assign_file_positions_for_non_load_sections will set filepos | |
5689 | for other sections and update p_filesz for other segments. */ | |
252b5132 RH |
5690 | for (i = 0, secpp = m->sections; i < m->count; i++, secpp++) |
5691 | { | |
5692 | asection *sec; | |
252b5132 | 5693 | bfd_size_type align; |
627b32bc | 5694 | Elf_Internal_Shdr *this_hdr; |
252b5132 RH |
5695 | |
5696 | sec = *secpp; | |
02bf8d82 | 5697 | this_hdr = &elf_section_data (sec)->this_hdr; |
fd361982 | 5698 | align = (bfd_size_type) 1 << bfd_section_alignment (sec); |
252b5132 | 5699 | |
88967714 AM |
5700 | if ((p->p_type == PT_LOAD |
5701 | || p->p_type == PT_TLS) | |
5702 | && (this_hdr->sh_type != SHT_NOBITS | |
5703 | || ((this_hdr->sh_flags & SHF_ALLOC) != 0 | |
5704 | && ((this_hdr->sh_flags & SHF_TLS) == 0 | |
5705 | || p->p_type == PT_TLS)))) | |
252b5132 | 5706 | { |
b5599592 AM |
5707 | bfd_vma p_start = p->p_paddr; |
5708 | bfd_vma p_end = p_start + p->p_memsz; | |
5709 | bfd_vma s_start = sec->lma; | |
5710 | bfd_vma adjust = s_start - p_end; | |
252b5132 | 5711 | |
a2d1e028 L |
5712 | if (adjust != 0 |
5713 | && (s_start < p_end | |
5714 | || p_end < p_start)) | |
252b5132 | 5715 | { |
4eca0228 | 5716 | _bfd_error_handler |
695344c0 | 5717 | /* xgettext:c-format */ |
2dcf00ce AM |
5718 | (_("%pB: section %pA lma %#" PRIx64 " adjusted to %#" PRIx64), |
5719 | abfd, sec, (uint64_t) s_start, (uint64_t) p_end); | |
88967714 | 5720 | adjust = 0; |
b5599592 | 5721 | sec->lma = p_end; |
1cfb7d1e | 5722 | } |
3ac9b6c9 | 5723 | p->p_memsz += adjust; |
1cfb7d1e | 5724 | |
88967714 AM |
5725 | if (this_hdr->sh_type != SHT_NOBITS) |
5726 | { | |
32812159 AM |
5727 | if (p->p_filesz + adjust < p->p_memsz) |
5728 | { | |
5729 | /* We have a PROGBITS section following NOBITS ones. | |
07d6d2b8 | 5730 | Allocate file space for the NOBITS section(s) and |
32812159 AM |
5731 | zero it. */ |
5732 | adjust = p->p_memsz - p->p_filesz; | |
5733 | if (!write_zeros (abfd, off, adjust)) | |
5734 | return FALSE; | |
5735 | } | |
88967714 AM |
5736 | off += adjust; |
5737 | p->p_filesz += adjust; | |
252b5132 | 5738 | } |
252b5132 RH |
5739 | } |
5740 | ||
5741 | if (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core) | |
5742 | { | |
b301b248 AM |
5743 | /* The section at i == 0 is the one that actually contains |
5744 | everything. */ | |
4a938328 MS |
5745 | if (i == 0) |
5746 | { | |
627b32bc | 5747 | this_hdr->sh_offset = sec->filepos = off; |
6a3cd2b4 AM |
5748 | off += this_hdr->sh_size; |
5749 | p->p_filesz = this_hdr->sh_size; | |
b301b248 AM |
5750 | p->p_memsz = 0; |
5751 | p->p_align = 1; | |
252b5132 | 5752 | } |
4a938328 | 5753 | else |
252b5132 | 5754 | { |
b301b248 | 5755 | /* The rest are fake sections that shouldn't be written. */ |
252b5132 | 5756 | sec->filepos = 0; |
eea6121a | 5757 | sec->size = 0; |
b301b248 AM |
5758 | sec->flags = 0; |
5759 | continue; | |
252b5132 | 5760 | } |
252b5132 RH |
5761 | } |
5762 | else | |
5763 | { | |
1e951488 | 5764 | if (p->p_type == PT_LOAD) |
b301b248 | 5765 | { |
1e951488 AM |
5766 | this_hdr->sh_offset = sec->filepos = off; |
5767 | if (this_hdr->sh_type != SHT_NOBITS) | |
5768 | off += this_hdr->sh_size; | |
5769 | } | |
5770 | else if (this_hdr->sh_type == SHT_NOBITS | |
5771 | && (this_hdr->sh_flags & SHF_TLS) != 0 | |
5772 | && this_hdr->sh_offset == 0) | |
5773 | { | |
5774 | /* This is a .tbss section that didn't get a PT_LOAD. | |
5775 | (See _bfd_elf_map_sections_to_segments "Create a | |
5776 | final PT_LOAD".) Set sh_offset to the value it | |
5777 | would have if we had created a zero p_filesz and | |
5778 | p_memsz PT_LOAD header for the section. This | |
5779 | also makes the PT_TLS header have the same | |
5780 | p_offset value. */ | |
5781 | bfd_vma adjust = vma_page_aligned_bias (this_hdr->sh_addr, | |
5782 | off, align); | |
5783 | this_hdr->sh_offset = sec->filepos = off + adjust; | |
b301b248 | 5784 | } |
252b5132 | 5785 | |
02bf8d82 | 5786 | if (this_hdr->sh_type != SHT_NOBITS) |
b301b248 | 5787 | { |
6a3cd2b4 | 5788 | p->p_filesz += this_hdr->sh_size; |
02bf8d82 AM |
5789 | /* A load section without SHF_ALLOC is something like |
5790 | a note section in a PT_NOTE segment. These take | |
5791 | file space but are not loaded into memory. */ | |
5792 | if ((this_hdr->sh_flags & SHF_ALLOC) != 0) | |
6a3cd2b4 | 5793 | p->p_memsz += this_hdr->sh_size; |
b301b248 | 5794 | } |
6a3cd2b4 | 5795 | else if ((this_hdr->sh_flags & SHF_ALLOC) != 0) |
13ae64f3 | 5796 | { |
6a3cd2b4 AM |
5797 | if (p->p_type == PT_TLS) |
5798 | p->p_memsz += this_hdr->sh_size; | |
5799 | ||
5800 | /* .tbss is special. It doesn't contribute to p_memsz of | |
5801 | normal segments. */ | |
5802 | else if ((this_hdr->sh_flags & SHF_TLS) == 0) | |
5803 | p->p_memsz += this_hdr->sh_size; | |
13ae64f3 JJ |
5804 | } |
5805 | ||
b10a8ae0 L |
5806 | if (align > p->p_align |
5807 | && !m->p_align_valid | |
5808 | && (p->p_type != PT_LOAD | |
5809 | || (abfd->flags & D_PAGED) == 0)) | |
252b5132 RH |
5810 | p->p_align = align; |
5811 | } | |
5812 | ||
bf988460 | 5813 | if (!m->p_flags_valid) |
252b5132 RH |
5814 | { |
5815 | p->p_flags |= PF_R; | |
02bf8d82 | 5816 | if ((this_hdr->sh_flags & SHF_EXECINSTR) != 0) |
252b5132 | 5817 | p->p_flags |= PF_X; |
02bf8d82 | 5818 | if ((this_hdr->sh_flags & SHF_WRITE) != 0) |
252b5132 RH |
5819 | p->p_flags |= PF_W; |
5820 | } | |
5821 | } | |
43a8475c | 5822 | |
bf988460 | 5823 | off -= off_adjust; |
0920dee7 | 5824 | |
7c928300 AM |
5825 | /* Check that all sections are in a PT_LOAD segment. |
5826 | Don't check funky gdb generated core files. */ | |
5827 | if (p->p_type == PT_LOAD && bfd_get_format (abfd) != bfd_core) | |
9a83a553 AM |
5828 | { |
5829 | bfd_boolean check_vma = TRUE; | |
5830 | ||
5831 | for (i = 1; i < m->count; i++) | |
5832 | if (m->sections[i]->vma == m->sections[i - 1]->vma | |
5833 | && ELF_SECTION_SIZE (&(elf_section_data (m->sections[i]) | |
5834 | ->this_hdr), p) != 0 | |
5835 | && ELF_SECTION_SIZE (&(elf_section_data (m->sections[i - 1]) | |
5836 | ->this_hdr), p) != 0) | |
0920dee7 | 5837 | { |
9a83a553 AM |
5838 | /* Looks like we have overlays packed into the segment. */ |
5839 | check_vma = FALSE; | |
5840 | break; | |
0920dee7 | 5841 | } |
9a83a553 AM |
5842 | |
5843 | for (i = 0; i < m->count; i++) | |
5844 | { | |
5845 | Elf_Internal_Shdr *this_hdr; | |
5846 | asection *sec; | |
5847 | ||
5848 | sec = m->sections[i]; | |
5849 | this_hdr = &(elf_section_data(sec)->this_hdr); | |
86b2281f AM |
5850 | if (!ELF_SECTION_IN_SEGMENT_1 (this_hdr, p, check_vma, 0) |
5851 | && !ELF_TBSS_SPECIAL (this_hdr, p)) | |
9a83a553 | 5852 | { |
4eca0228 | 5853 | _bfd_error_handler |
695344c0 | 5854 | /* xgettext:c-format */ |
871b3ab2 | 5855 | (_("%pB: section `%pA' can't be allocated in segment %d"), |
9a83a553 AM |
5856 | abfd, sec, j); |
5857 | print_segment_map (m); | |
5858 | } | |
5859 | } | |
5860 | } | |
252b5132 RH |
5861 | } |
5862 | ||
12bd6957 | 5863 | elf_next_file_pos (abfd) = off; |
f3520d2f AM |
5864 | return TRUE; |
5865 | } | |
5866 | ||
1faa385f NC |
5867 | /* Determine if a bfd is a debuginfo file. Unfortunately there |
5868 | is no defined method for detecting such files, so we have to | |
5869 | use heuristics instead. */ | |
5870 | ||
5871 | bfd_boolean | |
5872 | is_debuginfo_file (bfd *abfd) | |
5873 | { | |
5874 | if (abfd == NULL || bfd_get_flavour (abfd) != bfd_target_elf_flavour) | |
5875 | return FALSE; | |
5876 | ||
5877 | Elf_Internal_Shdr **start_headers = elf_elfsections (abfd); | |
5878 | Elf_Internal_Shdr **end_headers = start_headers + elf_numsections (abfd); | |
5879 | Elf_Internal_Shdr **headerp; | |
5880 | ||
5881 | for (headerp = start_headers; headerp < end_headers; headerp ++) | |
5882 | { | |
5883 | Elf_Internal_Shdr *header = * headerp; | |
5884 | ||
5885 | /* Debuginfo files do not have any allocated SHT_PROGBITS sections. | |
5886 | The only allocated sections are SHT_NOBITS or SHT_NOTES. */ | |
5887 | if ((header->sh_flags & SHF_ALLOC) == SHF_ALLOC | |
5888 | && header->sh_type != SHT_NOBITS | |
5889 | && header->sh_type != SHT_NOTE) | |
5890 | return FALSE; | |
5891 | } | |
5892 | ||
5893 | return TRUE; | |
5894 | } | |
5895 | ||
f3520d2f AM |
5896 | /* Assign file positions for the other sections. */ |
5897 | ||
5898 | static bfd_boolean | |
5899 | assign_file_positions_for_non_load_sections (bfd *abfd, | |
5900 | struct bfd_link_info *link_info) | |
5901 | { | |
5902 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
5903 | Elf_Internal_Shdr **i_shdrpp; | |
e06efbf1 | 5904 | Elf_Internal_Shdr **hdrpp, **end_hdrpp; |
f3520d2f AM |
5905 | Elf_Internal_Phdr *phdrs; |
5906 | Elf_Internal_Phdr *p; | |
5907 | struct elf_segment_map *m; | |
62655c7b | 5908 | struct elf_segment_map *hdrs_segment; |
f3520d2f AM |
5909 | bfd_vma filehdr_vaddr, filehdr_paddr; |
5910 | bfd_vma phdrs_vaddr, phdrs_paddr; | |
5911 | file_ptr off; | |
f3520d2f AM |
5912 | unsigned int count; |
5913 | ||
5c182d5f | 5914 | i_shdrpp = elf_elfsections (abfd); |
e06efbf1 | 5915 | end_hdrpp = i_shdrpp + elf_numsections (abfd); |
12bd6957 | 5916 | off = elf_next_file_pos (abfd); |
e06efbf1 | 5917 | for (hdrpp = i_shdrpp + 1; hdrpp < end_hdrpp; hdrpp++) |
5c182d5f | 5918 | { |
5c182d5f AM |
5919 | Elf_Internal_Shdr *hdr; |
5920 | ||
5921 | hdr = *hdrpp; | |
5922 | if (hdr->bfd_section != NULL | |
252e386e AM |
5923 | && (hdr->bfd_section->filepos != 0 |
5924 | || (hdr->sh_type == SHT_NOBITS | |
5925 | && hdr->contents == NULL))) | |
627b32bc | 5926 | BFD_ASSERT (hdr->sh_offset == hdr->bfd_section->filepos); |
5c182d5f AM |
5927 | else if ((hdr->sh_flags & SHF_ALLOC) != 0) |
5928 | { | |
1faa385f NC |
5929 | if (hdr->sh_size != 0 |
5930 | /* PR 24717 - debuginfo files are known to be not strictly | |
5931 | compliant with the ELF standard. In particular they often | |
5932 | have .note.gnu.property sections that are outside of any | |
5933 | loadable segment. This is not a problem for such files, | |
5934 | so do not warn about them. */ | |
5935 | && ! is_debuginfo_file (abfd)) | |
4eca0228 | 5936 | _bfd_error_handler |
695344c0 | 5937 | /* xgettext:c-format */ |
871b3ab2 | 5938 | (_("%pB: warning: allocated section `%s' not in segment"), |
e8d2ba53 AM |
5939 | abfd, |
5940 | (hdr->bfd_section == NULL | |
5941 | ? "*unknown*" | |
5942 | : hdr->bfd_section->name)); | |
3ba71138 L |
5943 | /* We don't need to page align empty sections. */ |
5944 | if ((abfd->flags & D_PAGED) != 0 && hdr->sh_size != 0) | |
5c182d5f AM |
5945 | off += vma_page_aligned_bias (hdr->sh_addr, off, |
5946 | bed->maxpagesize); | |
5947 | else | |
5948 | off += vma_page_aligned_bias (hdr->sh_addr, off, | |
5949 | hdr->sh_addralign); | |
5950 | off = _bfd_elf_assign_file_position_for_section (hdr, off, | |
5951 | FALSE); | |
5952 | } | |
5953 | else if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA) | |
5954 | && hdr->bfd_section == NULL) | |
0ce398f1 L |
5955 | || (hdr->bfd_section != NULL |
5956 | && (hdr->bfd_section->flags & SEC_ELF_COMPRESS)) | |
5957 | /* Compress DWARF debug sections. */ | |
12bd6957 | 5958 | || hdr == i_shdrpp[elf_onesymtab (abfd)] |
6a40cf0c NC |
5959 | || (elf_symtab_shndx_list (abfd) != NULL |
5960 | && hdr == i_shdrpp[elf_symtab_shndx_list (abfd)->ndx]) | |
3e19fb8f L |
5961 | || hdr == i_shdrpp[elf_strtab_sec (abfd)] |
5962 | || hdr == i_shdrpp[elf_shstrtab_sec (abfd)]) | |
5c182d5f AM |
5963 | hdr->sh_offset = -1; |
5964 | else | |
5965 | off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE); | |
5c182d5f AM |
5966 | } |
5967 | ||
252b5132 RH |
5968 | /* Now that we have set the section file positions, we can set up |
5969 | the file positions for the non PT_LOAD segments. */ | |
f3520d2f AM |
5970 | count = 0; |
5971 | filehdr_vaddr = 0; | |
5972 | filehdr_paddr = 0; | |
5973 | phdrs_vaddr = bed->maxpagesize + bed->s->sizeof_ehdr; | |
5974 | phdrs_paddr = 0; | |
62655c7b | 5975 | hdrs_segment = NULL; |
f3520d2f | 5976 | phdrs = elf_tdata (abfd)->phdr; |
12bd6957 | 5977 | for (m = elf_seg_map (abfd), p = phdrs; m != NULL; m = m->next, p++) |
f3520d2f AM |
5978 | { |
5979 | ++count; | |
5980 | if (p->p_type != PT_LOAD) | |
5981 | continue; | |
5982 | ||
5983 | if (m->includes_filehdr) | |
5984 | { | |
5985 | filehdr_vaddr = p->p_vaddr; | |
5986 | filehdr_paddr = p->p_paddr; | |
5987 | } | |
5988 | if (m->includes_phdrs) | |
5989 | { | |
5990 | phdrs_vaddr = p->p_vaddr; | |
5991 | phdrs_paddr = p->p_paddr; | |
5992 | if (m->includes_filehdr) | |
5993 | { | |
62655c7b | 5994 | hdrs_segment = m; |
f3520d2f AM |
5995 | phdrs_vaddr += bed->s->sizeof_ehdr; |
5996 | phdrs_paddr += bed->s->sizeof_ehdr; | |
5997 | } | |
5998 | } | |
5999 | } | |
6000 | ||
62655c7b RM |
6001 | if (hdrs_segment != NULL && link_info != NULL) |
6002 | { | |
6003 | /* There is a segment that contains both the file headers and the | |
6004 | program headers, so provide a symbol __ehdr_start pointing there. | |
6005 | A program can use this to examine itself robustly. */ | |
6006 | ||
6007 | struct elf_link_hash_entry *hash | |
6008 | = elf_link_hash_lookup (elf_hash_table (link_info), "__ehdr_start", | |
6009 | FALSE, FALSE, TRUE); | |
6010 | /* If the symbol was referenced and not defined, define it. */ | |
6011 | if (hash != NULL | |
6012 | && (hash->root.type == bfd_link_hash_new | |
6013 | || hash->root.type == bfd_link_hash_undefined | |
6014 | || hash->root.type == bfd_link_hash_undefweak | |
6015 | || hash->root.type == bfd_link_hash_common)) | |
6016 | { | |
6017 | asection *s = NULL; | |
6018 | if (hdrs_segment->count != 0) | |
6019 | /* The segment contains sections, so use the first one. */ | |
6020 | s = hdrs_segment->sections[0]; | |
6021 | else | |
6022 | /* Use the first (i.e. lowest-addressed) section in any segment. */ | |
12bd6957 | 6023 | for (m = elf_seg_map (abfd); m != NULL; m = m->next) |
62655c7b RM |
6024 | if (m->count != 0) |
6025 | { | |
6026 | s = m->sections[0]; | |
6027 | break; | |
6028 | } | |
6029 | ||
6030 | if (s != NULL) | |
6031 | { | |
6032 | hash->root.u.def.value = filehdr_vaddr - s->vma; | |
6033 | hash->root.u.def.section = s; | |
6034 | } | |
6035 | else | |
6036 | { | |
6037 | hash->root.u.def.value = filehdr_vaddr; | |
6038 | hash->root.u.def.section = bfd_abs_section_ptr; | |
6039 | } | |
6040 | ||
6041 | hash->root.type = bfd_link_hash_defined; | |
6042 | hash->def_regular = 1; | |
6043 | hash->non_elf = 0; | |
6044 | } | |
6045 | } | |
6046 | ||
12bd6957 | 6047 | for (m = elf_seg_map (abfd), p = phdrs; m != NULL; m = m->next, p++) |
252b5132 | 6048 | { |
129af99f | 6049 | if (p->p_type == PT_GNU_RELRO) |
252b5132 | 6050 | { |
f2731e0c | 6051 | bfd_vma start, end; |
01f7e10c | 6052 | bfd_boolean ok; |
1ea63fd2 | 6053 | |
129af99f | 6054 | if (link_info != NULL) |
8c37241b | 6055 | { |
129af99f | 6056 | /* During linking the range of the RELRO segment is passed |
f2731e0c AM |
6057 | in link_info. Note that there may be padding between |
6058 | relro_start and the first RELRO section. */ | |
6059 | start = link_info->relro_start; | |
6060 | end = link_info->relro_end; | |
6061 | } | |
6062 | else if (m->count != 0) | |
6063 | { | |
6064 | if (!m->p_size_valid) | |
6065 | abort (); | |
6066 | start = m->sections[0]->vma; | |
6067 | end = start + m->p_size; | |
6068 | } | |
6069 | else | |
6070 | { | |
6071 | start = 0; | |
6072 | end = 0; | |
6073 | } | |
6074 | ||
01f7e10c | 6075 | ok = FALSE; |
f2731e0c AM |
6076 | if (start < end) |
6077 | { | |
6078 | struct elf_segment_map *lm; | |
6079 | const Elf_Internal_Phdr *lp; | |
6080 | unsigned int i; | |
6081 | ||
6082 | /* Find a LOAD segment containing a section in the RELRO | |
6083 | segment. */ | |
12bd6957 | 6084 | for (lm = elf_seg_map (abfd), lp = phdrs; |
3146fac4 AM |
6085 | lm != NULL; |
6086 | lm = lm->next, lp++) | |
8c37241b JJ |
6087 | { |
6088 | if (lp->p_type == PT_LOAD | |
3146fac4 | 6089 | && lm->count != 0 |
dbc88fc1 AM |
6090 | && (lm->sections[lm->count - 1]->vma |
6091 | + (!IS_TBSS (lm->sections[lm->count - 1]) | |
6092 | ? lm->sections[lm->count - 1]->size | |
6093 | : 0)) > start | |
f2731e0c | 6094 | && lm->sections[0]->vma < end) |
8c37241b JJ |
6095 | break; |
6096 | } | |
f2731e0c | 6097 | |
01f7e10c | 6098 | if (lm != NULL) |
129af99f | 6099 | { |
01f7e10c AM |
6100 | /* Find the section starting the RELRO segment. */ |
6101 | for (i = 0; i < lm->count; i++) | |
6102 | { | |
6103 | asection *s = lm->sections[i]; | |
6104 | if (s->vma >= start | |
6105 | && s->vma < end | |
6106 | && s->size != 0) | |
6107 | break; | |
6108 | } | |
6109 | ||
6110 | if (i < lm->count) | |
6111 | { | |
6112 | p->p_vaddr = lm->sections[i]->vma; | |
6113 | p->p_paddr = lm->sections[i]->lma; | |
6114 | p->p_offset = lm->sections[i]->filepos; | |
6115 | p->p_memsz = end - p->p_vaddr; | |
6116 | p->p_filesz = p->p_memsz; | |
6117 | ||
6118 | /* The RELRO segment typically ends a few bytes | |
6119 | into .got.plt but other layouts are possible. | |
6120 | In cases where the end does not match any | |
6121 | loaded section (for instance is in file | |
6122 | padding), trim p_filesz back to correspond to | |
6123 | the end of loaded section contents. */ | |
6124 | if (p->p_filesz > lp->p_vaddr + lp->p_filesz - p->p_vaddr) | |
6125 | p->p_filesz = lp->p_vaddr + lp->p_filesz - p->p_vaddr; | |
6126 | ||
6127 | /* Preserve the alignment and flags if they are | |
6128 | valid. The gold linker generates RW/4 for | |
6129 | the PT_GNU_RELRO section. It is better for | |
6130 | objcopy/strip to honor these attributes | |
6131 | otherwise gdb will choke when using separate | |
6132 | debug files. */ | |
6133 | if (!m->p_align_valid) | |
6134 | p->p_align = 1; | |
6135 | if (!m->p_flags_valid) | |
6136 | p->p_flags = PF_R; | |
6137 | ok = TRUE; | |
6138 | } | |
129af99f | 6139 | } |
b84a33b5 | 6140 | } |
01f7e10c AM |
6141 | if (link_info != NULL) |
6142 | BFD_ASSERT (ok); | |
6143 | if (!ok) | |
6144 | memset (p, 0, sizeof *p); | |
129af99f | 6145 | } |
04c3a755 NS |
6146 | else if (p->p_type == PT_GNU_STACK) |
6147 | { | |
6148 | if (m->p_size_valid) | |
6149 | p->p_memsz = m->p_size; | |
6150 | } | |
129af99f AS |
6151 | else if (m->count != 0) |
6152 | { | |
e06efbf1 | 6153 | unsigned int i; |
1a9ccd70 | 6154 | |
129af99f AS |
6155 | if (p->p_type != PT_LOAD |
6156 | && (p->p_type != PT_NOTE | |
6157 | || bfd_get_format (abfd) != bfd_core)) | |
6158 | { | |
1a9ccd70 NC |
6159 | /* A user specified segment layout may include a PHDR |
6160 | segment that overlaps with a LOAD segment... */ | |
6161 | if (p->p_type == PT_PHDR) | |
6162 | { | |
6163 | m->count = 0; | |
6164 | continue; | |
6165 | } | |
6166 | ||
c86934ce NC |
6167 | if (m->includes_filehdr || m->includes_phdrs) |
6168 | { | |
b1fa9dd6 | 6169 | /* PR 17512: file: 2195325e. */ |
4eca0228 | 6170 | _bfd_error_handler |
871b3ab2 | 6171 | (_("%pB: error: non-load segment %d includes file header " |
76cfced5 AM |
6172 | "and/or program header"), |
6173 | abfd, (int) (p - phdrs)); | |
c86934ce NC |
6174 | return FALSE; |
6175 | } | |
129af99f | 6176 | |
86b2281f | 6177 | p->p_filesz = 0; |
129af99f | 6178 | p->p_offset = m->sections[0]->filepos; |
86b2281f AM |
6179 | for (i = m->count; i-- != 0;) |
6180 | { | |
6181 | asection *sect = m->sections[i]; | |
6182 | Elf_Internal_Shdr *hdr = &elf_section_data (sect)->this_hdr; | |
6183 | if (hdr->sh_type != SHT_NOBITS) | |
6184 | { | |
6185 | p->p_filesz = (sect->filepos - m->sections[0]->filepos | |
6186 | + hdr->sh_size); | |
6187 | break; | |
6188 | } | |
6189 | } | |
129af99f AS |
6190 | } |
6191 | } | |
6192 | else if (m->includes_filehdr) | |
6193 | { | |
6194 | p->p_vaddr = filehdr_vaddr; | |
6195 | if (! m->p_paddr_valid) | |
6196 | p->p_paddr = filehdr_paddr; | |
6197 | } | |
6198 | else if (m->includes_phdrs) | |
6199 | { | |
6200 | p->p_vaddr = phdrs_vaddr; | |
6201 | if (! m->p_paddr_valid) | |
6202 | p->p_paddr = phdrs_paddr; | |
252b5132 RH |
6203 | } |
6204 | } | |
6205 | ||
12bd6957 | 6206 | elf_next_file_pos (abfd) = off; |
252b5132 | 6207 | |
b34976b6 | 6208 | return TRUE; |
252b5132 RH |
6209 | } |
6210 | ||
6a40cf0c NC |
6211 | static elf_section_list * |
6212 | find_section_in_list (unsigned int i, elf_section_list * list) | |
6213 | { | |
6214 | for (;list != NULL; list = list->next) | |
6215 | if (list->ndx == i) | |
6216 | break; | |
6217 | return list; | |
6218 | } | |
6219 | ||
252b5132 RH |
6220 | /* Work out the file positions of all the sections. This is called by |
6221 | _bfd_elf_compute_section_file_positions. All the section sizes and | |
6222 | VMAs must be known before this is called. | |
6223 | ||
e0638f70 AM |
6224 | Reloc sections come in two flavours: Those processed specially as |
6225 | "side-channel" data attached to a section to which they apply, and | |
6226 | those that bfd doesn't process as relocations. The latter sort are | |
6227 | stored in a normal bfd section by bfd_section_from_shdr. We don't | |
6228 | consider the former sort here, unless they form part of the loadable | |
6229 | image. Reloc sections not assigned here will be handled later by | |
6230 | assign_file_positions_for_relocs. | |
252b5132 RH |
6231 | |
6232 | We also don't set the positions of the .symtab and .strtab here. */ | |
6233 | ||
b34976b6 | 6234 | static bfd_boolean |
c84fca4d AO |
6235 | assign_file_positions_except_relocs (bfd *abfd, |
6236 | struct bfd_link_info *link_info) | |
252b5132 | 6237 | { |
5c182d5f AM |
6238 | struct elf_obj_tdata *tdata = elf_tdata (abfd); |
6239 | Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd); | |
9c5bfbb7 | 6240 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
252b5132 RH |
6241 | |
6242 | if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0 | |
6243 | && bfd_get_format (abfd) != bfd_core) | |
6244 | { | |
5c182d5f AM |
6245 | Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd); |
6246 | unsigned int num_sec = elf_numsections (abfd); | |
252b5132 RH |
6247 | Elf_Internal_Shdr **hdrpp; |
6248 | unsigned int i; | |
a485e98e | 6249 | file_ptr off; |
252b5132 RH |
6250 | |
6251 | /* Start after the ELF header. */ | |
6252 | off = i_ehdrp->e_ehsize; | |
6253 | ||
6254 | /* We are not creating an executable, which means that we are | |
6255 | not creating a program header, and that the actual order of | |
6256 | the sections in the file is unimportant. */ | |
9ad5cbcf | 6257 | for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++) |
252b5132 RH |
6258 | { |
6259 | Elf_Internal_Shdr *hdr; | |
6260 | ||
6261 | hdr = *hdrpp; | |
e0638f70 AM |
6262 | if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA) |
6263 | && hdr->bfd_section == NULL) | |
0ce398f1 L |
6264 | || (hdr->bfd_section != NULL |
6265 | && (hdr->bfd_section->flags & SEC_ELF_COMPRESS)) | |
6266 | /* Compress DWARF debug sections. */ | |
12bd6957 | 6267 | || i == elf_onesymtab (abfd) |
6a40cf0c NC |
6268 | || (elf_symtab_shndx_list (abfd) != NULL |
6269 | && hdr == i_shdrpp[elf_symtab_shndx_list (abfd)->ndx]) | |
3e19fb8f L |
6270 | || i == elf_strtab_sec (abfd) |
6271 | || i == elf_shstrtab_sec (abfd)) | |
252b5132 RH |
6272 | { |
6273 | hdr->sh_offset = -1; | |
252b5132 | 6274 | } |
9ad5cbcf | 6275 | else |
b34976b6 | 6276 | off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE); |
252b5132 | 6277 | } |
a485e98e AM |
6278 | |
6279 | elf_next_file_pos (abfd) = off; | |
252b5132 RH |
6280 | } |
6281 | else | |
6282 | { | |
f3520d2f AM |
6283 | unsigned int alloc; |
6284 | ||
252b5132 | 6285 | /* Assign file positions for the loaded sections based on the |
08a40648 | 6286 | assignment of sections to segments. */ |
f3520d2f AM |
6287 | if (!assign_file_positions_for_load_sections (abfd, link_info)) |
6288 | return FALSE; | |
6289 | ||
6290 | /* And for non-load sections. */ | |
6291 | if (!assign_file_positions_for_non_load_sections (abfd, link_info)) | |
6292 | return FALSE; | |
6293 | ||
e36284ab AM |
6294 | if (bed->elf_backend_modify_program_headers != NULL) |
6295 | { | |
6296 | if (!(*bed->elf_backend_modify_program_headers) (abfd, link_info)) | |
6297 | return FALSE; | |
6298 | } | |
6299 | ||
58e7ebac | 6300 | /* Set e_type in ELF header to ET_EXEC for -pie -Ttext-segment=. */ |
0e1862bb | 6301 | if (link_info != NULL && bfd_link_pie (link_info)) |
58e7ebac L |
6302 | { |
6303 | unsigned int num_segments = elf_elfheader (abfd)->e_phnum; | |
6304 | Elf_Internal_Phdr *segment = elf_tdata (abfd)->phdr; | |
6305 | Elf_Internal_Phdr *end_segment = &segment[num_segments]; | |
6306 | ||
6307 | /* Find the lowest p_vaddr in PT_LOAD segments. */ | |
6308 | bfd_vma p_vaddr = (bfd_vma) -1; | |
6309 | for (; segment < end_segment; segment++) | |
6310 | if (segment->p_type == PT_LOAD && p_vaddr > segment->p_vaddr) | |
6311 | p_vaddr = segment->p_vaddr; | |
6312 | ||
6313 | /* Set e_type to ET_EXEC if the lowest p_vaddr in PT_LOAD | |
6314 | segments is non-zero. */ | |
6315 | if (p_vaddr) | |
6316 | i_ehdrp->e_type = ET_EXEC; | |
6317 | } | |
6318 | ||
f3520d2f | 6319 | /* Write out the program headers. */ |
6838f2be | 6320 | alloc = elf_elfheader (abfd)->e_phnum; |
1a9ccd70 NC |
6321 | if (alloc == 0) |
6322 | return TRUE; | |
6323 | ||
1a9ccd70 NC |
6324 | /* PR ld/20815 - Check that the program header segment, if present, will |
6325 | be loaded into memory. FIXME: The check below is not sufficient as | |
6326 | really all PT_LOAD segments should be checked before issuing an error | |
6327 | message. Plus the PHDR segment does not have to be the first segment | |
6328 | in the program header table. But this version of the check should | |
cd584857 NC |
6329 | catch all real world use cases. |
6330 | ||
6331 | FIXME: We used to have code here to sort the PT_LOAD segments into | |
6332 | ascending order, as per the ELF spec. But this breaks some programs, | |
6333 | including the Linux kernel. But really either the spec should be | |
07d6d2b8 | 6334 | changed or the programs updated. */ |
1a9ccd70 | 6335 | if (alloc > 1 |
cd584857 | 6336 | && tdata->phdr[0].p_type == PT_PHDR |
d00dd7dc AM |
6337 | && (bed->elf_backend_allow_non_load_phdr == NULL |
6338 | || !bed->elf_backend_allow_non_load_phdr (abfd, tdata->phdr, | |
6339 | alloc)) | |
cd584857 NC |
6340 | && tdata->phdr[1].p_type == PT_LOAD |
6341 | && (tdata->phdr[1].p_vaddr > tdata->phdr[0].p_vaddr | |
6838f2be AM |
6342 | || (tdata->phdr[1].p_vaddr + tdata->phdr[1].p_memsz |
6343 | < tdata->phdr[0].p_vaddr + tdata->phdr[0].p_memsz))) | |
1a9ccd70 NC |
6344 | { |
6345 | /* The fix for this error is usually to edit the linker script being | |
6346 | used and set up the program headers manually. Either that or | |
6347 | leave room for the headers at the start of the SECTIONS. */ | |
6838f2be AM |
6348 | _bfd_error_handler (_("%pB: error: PHDR segment not covered" |
6349 | " by LOAD segment"), | |
1a9ccd70 | 6350 | abfd); |
1a9ccd70 NC |
6351 | return FALSE; |
6352 | } | |
6353 | ||
f3520d2f | 6354 | if (bfd_seek (abfd, (bfd_signed_vma) bed->s->sizeof_ehdr, SEEK_SET) != 0 |
cd584857 NC |
6355 | || bed->s->write_out_phdrs (abfd, tdata->phdr, alloc) != 0) |
6356 | return FALSE; | |
252b5132 RH |
6357 | } |
6358 | ||
b34976b6 | 6359 | return TRUE; |
252b5132 RH |
6360 | } |
6361 | ||
b34976b6 | 6362 | static bfd_boolean |
217aa764 | 6363 | prep_headers (bfd *abfd) |
252b5132 | 6364 | { |
3d540e93 | 6365 | Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form. */ |
2b0f7ef9 | 6366 | struct elf_strtab_hash *shstrtab; |
9c5bfbb7 | 6367 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
252b5132 RH |
6368 | |
6369 | i_ehdrp = elf_elfheader (abfd); | |
252b5132 | 6370 | |
2b0f7ef9 | 6371 | shstrtab = _bfd_elf_strtab_init (); |
252b5132 | 6372 | if (shstrtab == NULL) |
b34976b6 | 6373 | return FALSE; |
252b5132 RH |
6374 | |
6375 | elf_shstrtab (abfd) = shstrtab; | |
6376 | ||
6377 | i_ehdrp->e_ident[EI_MAG0] = ELFMAG0; | |
6378 | i_ehdrp->e_ident[EI_MAG1] = ELFMAG1; | |
6379 | i_ehdrp->e_ident[EI_MAG2] = ELFMAG2; | |
6380 | i_ehdrp->e_ident[EI_MAG3] = ELFMAG3; | |
6381 | ||
6382 | i_ehdrp->e_ident[EI_CLASS] = bed->s->elfclass; | |
6383 | i_ehdrp->e_ident[EI_DATA] = | |
6384 | bfd_big_endian (abfd) ? ELFDATA2MSB : ELFDATA2LSB; | |
6385 | i_ehdrp->e_ident[EI_VERSION] = bed->s->ev_current; | |
6386 | ||
252b5132 RH |
6387 | if ((abfd->flags & DYNAMIC) != 0) |
6388 | i_ehdrp->e_type = ET_DYN; | |
6389 | else if ((abfd->flags & EXEC_P) != 0) | |
6390 | i_ehdrp->e_type = ET_EXEC; | |
6391 | else if (bfd_get_format (abfd) == bfd_core) | |
6392 | i_ehdrp->e_type = ET_CORE; | |
6393 | else | |
6394 | i_ehdrp->e_type = ET_REL; | |
6395 | ||
6396 | switch (bfd_get_arch (abfd)) | |
6397 | { | |
6398 | case bfd_arch_unknown: | |
6399 | i_ehdrp->e_machine = EM_NONE; | |
6400 | break; | |
aa4f99bb AO |
6401 | |
6402 | /* There used to be a long list of cases here, each one setting | |
6403 | e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE | |
6404 | in the corresponding bfd definition. To avoid duplication, | |
6405 | the switch was removed. Machines that need special handling | |
6406 | can generally do it in elf_backend_final_write_processing(), | |
6407 | unless they need the information earlier than the final write. | |
6408 | Such need can generally be supplied by replacing the tests for | |
6409 | e_machine with the conditions used to determine it. */ | |
252b5132 | 6410 | default: |
9c5bfbb7 AM |
6411 | i_ehdrp->e_machine = bed->elf_machine_code; |
6412 | } | |
aa4f99bb | 6413 | |
252b5132 RH |
6414 | i_ehdrp->e_version = bed->s->ev_current; |
6415 | i_ehdrp->e_ehsize = bed->s->sizeof_ehdr; | |
6416 | ||
c044fabd | 6417 | /* No program header, for now. */ |
252b5132 RH |
6418 | i_ehdrp->e_phoff = 0; |
6419 | i_ehdrp->e_phentsize = 0; | |
6420 | i_ehdrp->e_phnum = 0; | |
6421 | ||
c044fabd | 6422 | /* Each bfd section is section header entry. */ |
252b5132 RH |
6423 | i_ehdrp->e_entry = bfd_get_start_address (abfd); |
6424 | i_ehdrp->e_shentsize = bed->s->sizeof_shdr; | |
6425 | ||
c044fabd | 6426 | /* If we're building an executable, we'll need a program header table. */ |
252b5132 | 6427 | if (abfd->flags & EXEC_P) |
0e71e495 BE |
6428 | /* It all happens later. */ |
6429 | ; | |
252b5132 RH |
6430 | else |
6431 | { | |
6432 | i_ehdrp->e_phentsize = 0; | |
252b5132 RH |
6433 | i_ehdrp->e_phoff = 0; |
6434 | } | |
6435 | ||
6436 | elf_tdata (abfd)->symtab_hdr.sh_name = | |
b34976b6 | 6437 | (unsigned int) _bfd_elf_strtab_add (shstrtab, ".symtab", FALSE); |
252b5132 | 6438 | elf_tdata (abfd)->strtab_hdr.sh_name = |
b34976b6 | 6439 | (unsigned int) _bfd_elf_strtab_add (shstrtab, ".strtab", FALSE); |
252b5132 | 6440 | elf_tdata (abfd)->shstrtab_hdr.sh_name = |
b34976b6 | 6441 | (unsigned int) _bfd_elf_strtab_add (shstrtab, ".shstrtab", FALSE); |
252b5132 | 6442 | if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1 |
17ca87fc | 6443 | || elf_tdata (abfd)->strtab_hdr.sh_name == (unsigned int) -1 |
252b5132 | 6444 | || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1) |
b34976b6 | 6445 | return FALSE; |
252b5132 | 6446 | |
b34976b6 | 6447 | return TRUE; |
252b5132 RH |
6448 | } |
6449 | ||
6450 | /* Assign file positions for all the reloc sections which are not part | |
a485e98e | 6451 | of the loadable file image, and the file position of section headers. */ |
252b5132 | 6452 | |
0ce398f1 L |
6453 | static bfd_boolean |
6454 | _bfd_elf_assign_file_positions_for_non_load (bfd *abfd) | |
252b5132 RH |
6455 | { |
6456 | file_ptr off; | |
e06efbf1 | 6457 | Elf_Internal_Shdr **shdrpp, **end_shdrpp; |
3e19fb8f | 6458 | Elf_Internal_Shdr *shdrp; |
a485e98e AM |
6459 | Elf_Internal_Ehdr *i_ehdrp; |
6460 | const struct elf_backend_data *bed; | |
252b5132 | 6461 | |
12bd6957 | 6462 | off = elf_next_file_pos (abfd); |
252b5132 | 6463 | |
e06efbf1 L |
6464 | shdrpp = elf_elfsections (abfd); |
6465 | end_shdrpp = shdrpp + elf_numsections (abfd); | |
6466 | for (shdrpp++; shdrpp < end_shdrpp; shdrpp++) | |
252b5132 | 6467 | { |
252b5132 | 6468 | shdrp = *shdrpp; |
0ce398f1 L |
6469 | if (shdrp->sh_offset == -1) |
6470 | { | |
3e19fb8f | 6471 | asection *sec = shdrp->bfd_section; |
0ce398f1 L |
6472 | bfd_boolean is_rel = (shdrp->sh_type == SHT_REL |
6473 | || shdrp->sh_type == SHT_RELA); | |
6474 | if (is_rel | |
3e19fb8f | 6475 | || (sec != NULL && (sec->flags & SEC_ELF_COMPRESS))) |
0ce398f1 L |
6476 | { |
6477 | if (!is_rel) | |
6478 | { | |
3e19fb8f L |
6479 | const char *name = sec->name; |
6480 | struct bfd_elf_section_data *d; | |
6481 | ||
0ce398f1 | 6482 | /* Compress DWARF debug sections. */ |
3e19fb8f | 6483 | if (!bfd_compress_section (abfd, sec, |
0ce398f1 L |
6484 | shdrp->contents)) |
6485 | return FALSE; | |
3e19fb8f L |
6486 | |
6487 | if (sec->compress_status == COMPRESS_SECTION_DONE | |
6488 | && (abfd->flags & BFD_COMPRESS_GABI) == 0) | |
6489 | { | |
6490 | /* If section is compressed with zlib-gnu, convert | |
6491 | section name from .debug_* to .zdebug_*. */ | |
6492 | char *new_name | |
6493 | = convert_debug_to_zdebug (abfd, name); | |
6494 | if (new_name == NULL) | |
6495 | return FALSE; | |
6496 | name = new_name; | |
6497 | } | |
dd905818 | 6498 | /* Add section name to section name section. */ |
3e19fb8f L |
6499 | if (shdrp->sh_name != (unsigned int) -1) |
6500 | abort (); | |
6501 | shdrp->sh_name | |
6502 | = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), | |
6503 | name, FALSE); | |
6504 | d = elf_section_data (sec); | |
6505 | ||
dd905818 | 6506 | /* Add reloc section name to section name section. */ |
3e19fb8f L |
6507 | if (d->rel.hdr |
6508 | && !_bfd_elf_set_reloc_sh_name (abfd, | |
6509 | d->rel.hdr, | |
6510 | name, FALSE)) | |
6511 | return FALSE; | |
6512 | if (d->rela.hdr | |
6513 | && !_bfd_elf_set_reloc_sh_name (abfd, | |
6514 | d->rela.hdr, | |
91cb26da | 6515 | name, TRUE)) |
3e19fb8f L |
6516 | return FALSE; |
6517 | ||
0ce398f1 | 6518 | /* Update section size and contents. */ |
3e19fb8f L |
6519 | shdrp->sh_size = sec->size; |
6520 | shdrp->contents = sec->contents; | |
0ce398f1 L |
6521 | shdrp->bfd_section->contents = NULL; |
6522 | } | |
6523 | off = _bfd_elf_assign_file_position_for_section (shdrp, | |
6524 | off, | |
6525 | TRUE); | |
6526 | } | |
6527 | } | |
252b5132 RH |
6528 | } |
6529 | ||
3e19fb8f L |
6530 | /* Place section name section after DWARF debug sections have been |
6531 | compressed. */ | |
6532 | _bfd_elf_strtab_finalize (elf_shstrtab (abfd)); | |
6533 | shdrp = &elf_tdata (abfd)->shstrtab_hdr; | |
6534 | shdrp->sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd)); | |
6535 | off = _bfd_elf_assign_file_position_for_section (shdrp, off, TRUE); | |
6536 | ||
6537 | /* Place the section headers. */ | |
a485e98e AM |
6538 | i_ehdrp = elf_elfheader (abfd); |
6539 | bed = get_elf_backend_data (abfd); | |
6540 | off = align_file_position (off, 1 << bed->s->log_file_align); | |
6541 | i_ehdrp->e_shoff = off; | |
6542 | off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize; | |
12bd6957 | 6543 | elf_next_file_pos (abfd) = off; |
0ce398f1 L |
6544 | |
6545 | return TRUE; | |
252b5132 RH |
6546 | } |
6547 | ||
b34976b6 | 6548 | bfd_boolean |
217aa764 | 6549 | _bfd_elf_write_object_contents (bfd *abfd) |
252b5132 | 6550 | { |
9c5bfbb7 | 6551 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
252b5132 | 6552 | Elf_Internal_Shdr **i_shdrp; |
b34976b6 | 6553 | bfd_boolean failed; |
9ad5cbcf | 6554 | unsigned int count, num_sec; |
30e8ee25 | 6555 | struct elf_obj_tdata *t; |
252b5132 RH |
6556 | |
6557 | if (! abfd->output_has_begun | |
217aa764 | 6558 | && ! _bfd_elf_compute_section_file_positions (abfd, NULL)) |
b34976b6 | 6559 | return FALSE; |
db727370 JL |
6560 | /* Do not rewrite ELF data when the BFD has been opened for update. |
6561 | abfd->output_has_begun was set to TRUE on opening, so creation of new | |
6562 | sections, and modification of existing section sizes was restricted. | |
6563 | This means the ELF header, program headers and section headers can't have | |
6564 | changed. | |
6565 | If the contents of any sections has been modified, then those changes have | |
6566 | already been written to the BFD. */ | |
6567 | else if (abfd->direction == both_direction) | |
6568 | { | |
6569 | BFD_ASSERT (abfd->output_has_begun); | |
6570 | return TRUE; | |
6571 | } | |
252b5132 RH |
6572 | |
6573 | i_shdrp = elf_elfsections (abfd); | |
252b5132 | 6574 | |
b34976b6 | 6575 | failed = FALSE; |
252b5132 RH |
6576 | bfd_map_over_sections (abfd, bed->s->write_relocs, &failed); |
6577 | if (failed) | |
b34976b6 | 6578 | return FALSE; |
252b5132 | 6579 | |
0ce398f1 L |
6580 | if (!_bfd_elf_assign_file_positions_for_non_load (abfd)) |
6581 | return FALSE; | |
252b5132 | 6582 | |
c044fabd | 6583 | /* After writing the headers, we need to write the sections too... */ |
9ad5cbcf AM |
6584 | num_sec = elf_numsections (abfd); |
6585 | for (count = 1; count < num_sec; count++) | |
252b5132 | 6586 | { |
3e19fb8f L |
6587 | i_shdrp[count]->sh_name |
6588 | = _bfd_elf_strtab_offset (elf_shstrtab (abfd), | |
6589 | i_shdrp[count]->sh_name); | |
252b5132 | 6590 | if (bed->elf_backend_section_processing) |
75506100 MR |
6591 | if (!(*bed->elf_backend_section_processing) (abfd, i_shdrp[count])) |
6592 | return FALSE; | |
252b5132 RH |
6593 | if (i_shdrp[count]->contents) |
6594 | { | |
dc810e39 AM |
6595 | bfd_size_type amt = i_shdrp[count]->sh_size; |
6596 | ||
252b5132 | 6597 | if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0 |
dc810e39 | 6598 | || bfd_bwrite (i_shdrp[count]->contents, amt, abfd) != amt) |
b34976b6 | 6599 | return FALSE; |
252b5132 RH |
6600 | } |
6601 | } | |
6602 | ||
6603 | /* Write out the section header names. */ | |
30e8ee25 | 6604 | t = elf_tdata (abfd); |
26ae6d5e | 6605 | if (elf_shstrtab (abfd) != NULL |
30e8ee25 | 6606 | && (bfd_seek (abfd, t->shstrtab_hdr.sh_offset, SEEK_SET) != 0 |
08a40648 | 6607 | || !_bfd_elf_strtab_emit (abfd, elf_shstrtab (abfd)))) |
b34976b6 | 6608 | return FALSE; |
252b5132 | 6609 | |
cc364be6 AM |
6610 | if (!(*bed->elf_backend_final_write_processing) (abfd)) |
6611 | return FALSE; | |
252b5132 | 6612 | |
ff59fc36 RM |
6613 | if (!bed->s->write_shdrs_and_ehdr (abfd)) |
6614 | return FALSE; | |
6615 | ||
6616 | /* This is last since write_shdrs_and_ehdr can touch i_shdrp[0]. */ | |
c0355132 AM |
6617 | if (t->o->build_id.after_write_object_contents != NULL) |
6618 | return (*t->o->build_id.after_write_object_contents) (abfd); | |
ff59fc36 RM |
6619 | |
6620 | return TRUE; | |
252b5132 RH |
6621 | } |
6622 | ||
b34976b6 | 6623 | bfd_boolean |
217aa764 | 6624 | _bfd_elf_write_corefile_contents (bfd *abfd) |
252b5132 | 6625 | { |
c044fabd | 6626 | /* Hopefully this can be done just like an object file. */ |
252b5132 RH |
6627 | return _bfd_elf_write_object_contents (abfd); |
6628 | } | |
c044fabd KH |
6629 | |
6630 | /* Given a section, search the header to find them. */ | |
6631 | ||
cb33740c | 6632 | unsigned int |
198beae2 | 6633 | _bfd_elf_section_from_bfd_section (bfd *abfd, struct bfd_section *asect) |
252b5132 | 6634 | { |
9c5bfbb7 | 6635 | const struct elf_backend_data *bed; |
91d6fa6a | 6636 | unsigned int sec_index; |
252b5132 | 6637 | |
9ad5cbcf AM |
6638 | if (elf_section_data (asect) != NULL |
6639 | && elf_section_data (asect)->this_idx != 0) | |
6640 | return elf_section_data (asect)->this_idx; | |
6641 | ||
6642 | if (bfd_is_abs_section (asect)) | |
91d6fa6a | 6643 | sec_index = SHN_ABS; |
af746e92 | 6644 | else if (bfd_is_com_section (asect)) |
91d6fa6a | 6645 | sec_index = SHN_COMMON; |
af746e92 | 6646 | else if (bfd_is_und_section (asect)) |
91d6fa6a | 6647 | sec_index = SHN_UNDEF; |
af746e92 | 6648 | else |
91d6fa6a | 6649 | sec_index = SHN_BAD; |
252b5132 | 6650 | |
af746e92 | 6651 | bed = get_elf_backend_data (abfd); |
252b5132 RH |
6652 | if (bed->elf_backend_section_from_bfd_section) |
6653 | { | |
91d6fa6a | 6654 | int retval = sec_index; |
9ad5cbcf | 6655 | |
af746e92 AM |
6656 | if ((*bed->elf_backend_section_from_bfd_section) (abfd, asect, &retval)) |
6657 | return retval; | |
252b5132 RH |
6658 | } |
6659 | ||
91d6fa6a | 6660 | if (sec_index == SHN_BAD) |
af746e92 | 6661 | bfd_set_error (bfd_error_nonrepresentable_section); |
252b5132 | 6662 | |
91d6fa6a | 6663 | return sec_index; |
252b5132 RH |
6664 | } |
6665 | ||
6666 | /* Given a BFD symbol, return the index in the ELF symbol table, or -1 | |
6667 | on error. */ | |
6668 | ||
6669 | int | |
217aa764 | 6670 | _bfd_elf_symbol_from_bfd_symbol (bfd *abfd, asymbol **asym_ptr_ptr) |
252b5132 RH |
6671 | { |
6672 | asymbol *asym_ptr = *asym_ptr_ptr; | |
6673 | int idx; | |
6674 | flagword flags = asym_ptr->flags; | |
6675 | ||
6676 | /* When gas creates relocations against local labels, it creates its | |
6677 | own symbol for the section, but does put the symbol into the | |
6678 | symbol chain, so udata is 0. When the linker is generating | |
6679 | relocatable output, this section symbol may be for one of the | |
6680 | input sections rather than the output section. */ | |
6681 | if (asym_ptr->udata.i == 0 | |
6682 | && (flags & BSF_SECTION_SYM) | |
6683 | && asym_ptr->section) | |
6684 | { | |
5372391b | 6685 | asection *sec; |
252b5132 RH |
6686 | int indx; |
6687 | ||
5372391b AM |
6688 | sec = asym_ptr->section; |
6689 | if (sec->owner != abfd && sec->output_section != NULL) | |
6690 | sec = sec->output_section; | |
6691 | if (sec->owner == abfd | |
6692 | && (indx = sec->index) < elf_num_section_syms (abfd) | |
4e89ac30 | 6693 | && elf_section_syms (abfd)[indx] != NULL) |
252b5132 RH |
6694 | asym_ptr->udata.i = elf_section_syms (abfd)[indx]->udata.i; |
6695 | } | |
6696 | ||
6697 | idx = asym_ptr->udata.i; | |
6698 | ||
6699 | if (idx == 0) | |
6700 | { | |
6701 | /* This case can occur when using --strip-symbol on a symbol | |
08a40648 | 6702 | which is used in a relocation entry. */ |
4eca0228 | 6703 | _bfd_error_handler |
695344c0 | 6704 | /* xgettext:c-format */ |
871b3ab2 | 6705 | (_("%pB: symbol `%s' required but not present"), |
d003868e | 6706 | abfd, bfd_asymbol_name (asym_ptr)); |
252b5132 RH |
6707 | bfd_set_error (bfd_error_no_symbols); |
6708 | return -1; | |
6709 | } | |
6710 | ||
6711 | #if DEBUG & 4 | |
6712 | { | |
6713 | fprintf (stderr, | |
cd9af601 AM |
6714 | "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8x\n", |
6715 | (long) asym_ptr, asym_ptr->name, idx, flags); | |
252b5132 RH |
6716 | fflush (stderr); |
6717 | } | |
6718 | #endif | |
6719 | ||
6720 | return idx; | |
6721 | } | |
6722 | ||
84d1d650 | 6723 | /* Rewrite program header information. */ |
252b5132 | 6724 | |
b34976b6 | 6725 | static bfd_boolean |
84d1d650 | 6726 | rewrite_elf_program_header (bfd *ibfd, bfd *obfd) |
252b5132 | 6727 | { |
b34976b6 AM |
6728 | Elf_Internal_Ehdr *iehdr; |
6729 | struct elf_segment_map *map; | |
6730 | struct elf_segment_map *map_first; | |
6731 | struct elf_segment_map **pointer_to_map; | |
6732 | Elf_Internal_Phdr *segment; | |
6733 | asection *section; | |
6734 | unsigned int i; | |
6735 | unsigned int num_segments; | |
6736 | bfd_boolean phdr_included = FALSE; | |
5c44b38e | 6737 | bfd_boolean p_paddr_valid; |
b34976b6 AM |
6738 | bfd_vma maxpagesize; |
6739 | struct elf_segment_map *phdr_adjust_seg = NULL; | |
6740 | unsigned int phdr_adjust_num = 0; | |
9c5bfbb7 | 6741 | const struct elf_backend_data *bed; |
bc67d8a6 | 6742 | |
caf47ea6 | 6743 | bed = get_elf_backend_data (ibfd); |
252b5132 RH |
6744 | iehdr = elf_elfheader (ibfd); |
6745 | ||
bc67d8a6 | 6746 | map_first = NULL; |
c044fabd | 6747 | pointer_to_map = &map_first; |
252b5132 RH |
6748 | |
6749 | num_segments = elf_elfheader (ibfd)->e_phnum; | |
bc67d8a6 NC |
6750 | maxpagesize = get_elf_backend_data (obfd)->maxpagesize; |
6751 | ||
6752 | /* Returns the end address of the segment + 1. */ | |
aecc8f8a AM |
6753 | #define SEGMENT_END(segment, start) \ |
6754 | (start + (segment->p_memsz > segment->p_filesz \ | |
6755 | ? segment->p_memsz : segment->p_filesz)) | |
bc67d8a6 | 6756 | |
eecdbe52 JJ |
6757 | #define SECTION_SIZE(section, segment) \ |
6758 | (((section->flags & (SEC_HAS_CONTENTS | SEC_THREAD_LOCAL)) \ | |
6759 | != SEC_THREAD_LOCAL || segment->p_type == PT_TLS) \ | |
eea6121a | 6760 | ? section->size : 0) |
eecdbe52 | 6761 | |
b34976b6 | 6762 | /* Returns TRUE if the given section is contained within |
bc67d8a6 | 6763 | the given segment. VMA addresses are compared. */ |
aecc8f8a AM |
6764 | #define IS_CONTAINED_BY_VMA(section, segment) \ |
6765 | (section->vma >= segment->p_vaddr \ | |
eecdbe52 | 6766 | && (section->vma + SECTION_SIZE (section, segment) \ |
aecc8f8a | 6767 | <= (SEGMENT_END (segment, segment->p_vaddr)))) |
c044fabd | 6768 | |
b34976b6 | 6769 | /* Returns TRUE if the given section is contained within |
bc67d8a6 | 6770 | the given segment. LMA addresses are compared. */ |
aecc8f8a AM |
6771 | #define IS_CONTAINED_BY_LMA(section, segment, base) \ |
6772 | (section->lma >= base \ | |
beab4532 | 6773 | && (section->lma + SECTION_SIZE (section, segment) >= section->lma) \ |
eecdbe52 | 6774 | && (section->lma + SECTION_SIZE (section, segment) \ |
aecc8f8a | 6775 | <= SEGMENT_END (segment, base))) |
252b5132 | 6776 | |
0efc80c8 L |
6777 | /* Handle PT_NOTE segment. */ |
6778 | #define IS_NOTE(p, s) \ | |
aecc8f8a | 6779 | (p->p_type == PT_NOTE \ |
0efc80c8 | 6780 | && elf_section_type (s) == SHT_NOTE \ |
aecc8f8a | 6781 | && (bfd_vma) s->filepos >= p->p_offset \ |
cb3ff1e5 | 6782 | && ((bfd_vma) s->filepos + s->size \ |
aecc8f8a | 6783 | <= p->p_offset + p->p_filesz)) |
252b5132 | 6784 | |
0efc80c8 L |
6785 | /* Special case: corefile "NOTE" section containing regs, prpsinfo |
6786 | etc. */ | |
6787 | #define IS_COREFILE_NOTE(p, s) \ | |
6788 | (IS_NOTE (p, s) \ | |
6789 | && bfd_get_format (ibfd) == bfd_core \ | |
6790 | && s->vma == 0 \ | |
6791 | && s->lma == 0) | |
6792 | ||
252b5132 RH |
6793 | /* The complicated case when p_vaddr is 0 is to handle the Solaris |
6794 | linker, which generates a PT_INTERP section with p_vaddr and | |
6795 | p_memsz set to 0. */ | |
aecc8f8a AM |
6796 | #define IS_SOLARIS_PT_INTERP(p, s) \ |
6797 | (p->p_vaddr == 0 \ | |
6798 | && p->p_paddr == 0 \ | |
6799 | && p->p_memsz == 0 \ | |
6800 | && p->p_filesz > 0 \ | |
6801 | && (s->flags & SEC_HAS_CONTENTS) != 0 \ | |
eea6121a | 6802 | && s->size > 0 \ |
aecc8f8a | 6803 | && (bfd_vma) s->filepos >= p->p_offset \ |
cb3ff1e5 | 6804 | && ((bfd_vma) s->filepos + s->size \ |
aecc8f8a | 6805 | <= p->p_offset + p->p_filesz)) |
5c440b1e | 6806 | |
bc67d8a6 NC |
6807 | /* Decide if the given section should be included in the given segment. |
6808 | A section will be included if: | |
f5ffc919 | 6809 | 1. It is within the address space of the segment -- we use the LMA |
08a40648 | 6810 | if that is set for the segment and the VMA otherwise, |
0efc80c8 | 6811 | 2. It is an allocated section or a NOTE section in a PT_NOTE |
d324f6d6 | 6812 | segment. |
bc67d8a6 | 6813 | 3. There is an output section associated with it, |
eecdbe52 | 6814 | 4. The section has not already been allocated to a previous segment. |
2b05f1b7 | 6815 | 5. PT_GNU_STACK segments do not include any sections. |
03394ac9 | 6816 | 6. PT_TLS segment includes only SHF_TLS sections. |
6f79b219 JJ |
6817 | 7. SHF_TLS sections are only in PT_TLS or PT_LOAD segments. |
6818 | 8. PT_DYNAMIC should not contain empty sections at the beginning | |
08a40648 | 6819 | (with the possible exception of .dynamic). */ |
9f17e2a6 | 6820 | #define IS_SECTION_IN_INPUT_SEGMENT(section, segment, bed) \ |
2b05f1b7 L |
6821 | ((((segment->p_paddr \ |
6822 | ? IS_CONTAINED_BY_LMA (section, segment, segment->p_paddr) \ | |
6823 | : IS_CONTAINED_BY_VMA (section, segment)) \ | |
6824 | && (section->flags & SEC_ALLOC) != 0) \ | |
0efc80c8 | 6825 | || IS_NOTE (segment, section)) \ |
2b05f1b7 L |
6826 | && segment->p_type != PT_GNU_STACK \ |
6827 | && (segment->p_type != PT_TLS \ | |
6828 | || (section->flags & SEC_THREAD_LOCAL)) \ | |
6829 | && (segment->p_type == PT_LOAD \ | |
6830 | || segment->p_type == PT_TLS \ | |
6831 | || (section->flags & SEC_THREAD_LOCAL) == 0) \ | |
6832 | && (segment->p_type != PT_DYNAMIC \ | |
6833 | || SECTION_SIZE (section, segment) > 0 \ | |
6834 | || (segment->p_paddr \ | |
6835 | ? segment->p_paddr != section->lma \ | |
6836 | : segment->p_vaddr != section->vma) \ | |
fd361982 | 6837 | || (strcmp (bfd_section_name (section), ".dynamic") == 0)) \ |
9933dc52 | 6838 | && (segment->p_type != PT_LOAD || !section->segment_mark)) |
bc67d8a6 | 6839 | |
9f17e2a6 L |
6840 | /* If the output section of a section in the input segment is NULL, |
6841 | it is removed from the corresponding output segment. */ | |
6842 | #define INCLUDE_SECTION_IN_SEGMENT(section, segment, bed) \ | |
6843 | (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed) \ | |
6844 | && section->output_section != NULL) | |
6845 | ||
b34976b6 | 6846 | /* Returns TRUE iff seg1 starts after the end of seg2. */ |
b5f852ea NC |
6847 | #define SEGMENT_AFTER_SEGMENT(seg1, seg2, field) \ |
6848 | (seg1->field >= SEGMENT_END (seg2, seg2->field)) | |
6849 | ||
6850 | /* Returns TRUE iff seg1 and seg2 overlap. Segments overlap iff both | |
6851 | their VMA address ranges and their LMA address ranges overlap. | |
6852 | It is possible to have overlapping VMA ranges without overlapping LMA | |
6853 | ranges. RedBoot images for example can have both .data and .bss mapped | |
6854 | to the same VMA range, but with the .data section mapped to a different | |
6855 | LMA. */ | |
aecc8f8a | 6856 | #define SEGMENT_OVERLAPS(seg1, seg2) \ |
b5f852ea | 6857 | ( !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_vaddr) \ |
08a40648 | 6858 | || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_vaddr)) \ |
b5f852ea | 6859 | && !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_paddr) \ |
08a40648 | 6860 | || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_paddr))) |
bc67d8a6 NC |
6861 | |
6862 | /* Initialise the segment mark field. */ | |
6863 | for (section = ibfd->sections; section != NULL; section = section->next) | |
b34976b6 | 6864 | section->segment_mark = FALSE; |
bc67d8a6 | 6865 | |
5c44b38e AM |
6866 | /* The Solaris linker creates program headers in which all the |
6867 | p_paddr fields are zero. When we try to objcopy or strip such a | |
6868 | file, we get confused. Check for this case, and if we find it | |
6869 | don't set the p_paddr_valid fields. */ | |
6870 | p_paddr_valid = FALSE; | |
6871 | for (i = 0, segment = elf_tdata (ibfd)->phdr; | |
6872 | i < num_segments; | |
6873 | i++, segment++) | |
6874 | if (segment->p_paddr != 0) | |
6875 | { | |
6876 | p_paddr_valid = TRUE; | |
6877 | break; | |
6878 | } | |
6879 | ||
252b5132 | 6880 | /* Scan through the segments specified in the program header |
bc67d8a6 | 6881 | of the input BFD. For this first scan we look for overlaps |
9ad5cbcf | 6882 | in the loadable segments. These can be created by weird |
aecc8f8a | 6883 | parameters to objcopy. Also, fix some solaris weirdness. */ |
bc67d8a6 NC |
6884 | for (i = 0, segment = elf_tdata (ibfd)->phdr; |
6885 | i < num_segments; | |
c044fabd | 6886 | i++, segment++) |
252b5132 | 6887 | { |
252b5132 | 6888 | unsigned int j; |
c044fabd | 6889 | Elf_Internal_Phdr *segment2; |
252b5132 | 6890 | |
aecc8f8a AM |
6891 | if (segment->p_type == PT_INTERP) |
6892 | for (section = ibfd->sections; section; section = section->next) | |
6893 | if (IS_SOLARIS_PT_INTERP (segment, section)) | |
6894 | { | |
6895 | /* Mininal change so that the normal section to segment | |
4cc11e76 | 6896 | assignment code will work. */ |
aecc8f8a AM |
6897 | segment->p_vaddr = section->vma; |
6898 | break; | |
6899 | } | |
6900 | ||
bc67d8a6 | 6901 | if (segment->p_type != PT_LOAD) |
b10a8ae0 L |
6902 | { |
6903 | /* Remove PT_GNU_RELRO segment. */ | |
6904 | if (segment->p_type == PT_GNU_RELRO) | |
6905 | segment->p_type = PT_NULL; | |
6906 | continue; | |
6907 | } | |
c044fabd | 6908 | |
bc67d8a6 | 6909 | /* Determine if this segment overlaps any previous segments. */ |
0067a569 | 6910 | for (j = 0, segment2 = elf_tdata (ibfd)->phdr; j < i; j++, segment2++) |
bc67d8a6 NC |
6911 | { |
6912 | bfd_signed_vma extra_length; | |
c044fabd | 6913 | |
bc67d8a6 | 6914 | if (segment2->p_type != PT_LOAD |
0067a569 | 6915 | || !SEGMENT_OVERLAPS (segment, segment2)) |
bc67d8a6 | 6916 | continue; |
c044fabd | 6917 | |
bc67d8a6 NC |
6918 | /* Merge the two segments together. */ |
6919 | if (segment2->p_vaddr < segment->p_vaddr) | |
6920 | { | |
c044fabd | 6921 | /* Extend SEGMENT2 to include SEGMENT and then delete |
08a40648 | 6922 | SEGMENT. */ |
0067a569 AM |
6923 | extra_length = (SEGMENT_END (segment, segment->p_vaddr) |
6924 | - SEGMENT_END (segment2, segment2->p_vaddr)); | |
c044fabd | 6925 | |
bc67d8a6 NC |
6926 | if (extra_length > 0) |
6927 | { | |
0067a569 | 6928 | segment2->p_memsz += extra_length; |
bc67d8a6 NC |
6929 | segment2->p_filesz += extra_length; |
6930 | } | |
c044fabd | 6931 | |
bc67d8a6 | 6932 | segment->p_type = PT_NULL; |
c044fabd | 6933 | |
bc67d8a6 NC |
6934 | /* Since we have deleted P we must restart the outer loop. */ |
6935 | i = 0; | |
6936 | segment = elf_tdata (ibfd)->phdr; | |
6937 | break; | |
6938 | } | |
6939 | else | |
6940 | { | |
c044fabd | 6941 | /* Extend SEGMENT to include SEGMENT2 and then delete |
08a40648 | 6942 | SEGMENT2. */ |
0067a569 AM |
6943 | extra_length = (SEGMENT_END (segment2, segment2->p_vaddr) |
6944 | - SEGMENT_END (segment, segment->p_vaddr)); | |
c044fabd | 6945 | |
bc67d8a6 NC |
6946 | if (extra_length > 0) |
6947 | { | |
0067a569 | 6948 | segment->p_memsz += extra_length; |
bc67d8a6 NC |
6949 | segment->p_filesz += extra_length; |
6950 | } | |
c044fabd | 6951 | |
bc67d8a6 NC |
6952 | segment2->p_type = PT_NULL; |
6953 | } | |
6954 | } | |
6955 | } | |
c044fabd | 6956 | |
bc67d8a6 NC |
6957 | /* The second scan attempts to assign sections to segments. */ |
6958 | for (i = 0, segment = elf_tdata (ibfd)->phdr; | |
6959 | i < num_segments; | |
0067a569 | 6960 | i++, segment++) |
bc67d8a6 | 6961 | { |
0067a569 AM |
6962 | unsigned int section_count; |
6963 | asection **sections; | |
6964 | asection *output_section; | |
6965 | unsigned int isec; | |
9933dc52 AM |
6966 | asection *matching_lma; |
6967 | asection *suggested_lma; | |
0067a569 | 6968 | unsigned int j; |
dc810e39 | 6969 | bfd_size_type amt; |
0067a569 | 6970 | asection *first_section; |
bc67d8a6 NC |
6971 | |
6972 | if (segment->p_type == PT_NULL) | |
6973 | continue; | |
c044fabd | 6974 | |
9f17e2a6 | 6975 | first_section = NULL; |
bc67d8a6 | 6976 | /* Compute how many sections might be placed into this segment. */ |
b5f852ea NC |
6977 | for (section = ibfd->sections, section_count = 0; |
6978 | section != NULL; | |
6979 | section = section->next) | |
9f17e2a6 L |
6980 | { |
6981 | /* Find the first section in the input segment, which may be | |
6982 | removed from the corresponding output segment. */ | |
6983 | if (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed)) | |
6984 | { | |
6985 | if (first_section == NULL) | |
6986 | first_section = section; | |
6987 | if (section->output_section != NULL) | |
6988 | ++section_count; | |
6989 | } | |
6990 | } | |
811072d8 | 6991 | |
b5f852ea NC |
6992 | /* Allocate a segment map big enough to contain |
6993 | all of the sections we have selected. */ | |
00bee008 AM |
6994 | amt = sizeof (struct elf_segment_map) - sizeof (asection *); |
6995 | amt += (bfd_size_type) section_count * sizeof (asection *); | |
a50b1753 | 6996 | map = (struct elf_segment_map *) bfd_zalloc (obfd, amt); |
bc67d8a6 | 6997 | if (map == NULL) |
b34976b6 | 6998 | return FALSE; |
252b5132 RH |
6999 | |
7000 | /* Initialise the fields of the segment map. Default to | |
7001 | using the physical address of the segment in the input BFD. */ | |
0067a569 AM |
7002 | map->next = NULL; |
7003 | map->p_type = segment->p_type; | |
7004 | map->p_flags = segment->p_flags; | |
bc67d8a6 | 7005 | map->p_flags_valid = 1; |
55d55ac7 | 7006 | |
9f17e2a6 L |
7007 | /* If the first section in the input segment is removed, there is |
7008 | no need to preserve segment physical address in the corresponding | |
7009 | output segment. */ | |
945c025a | 7010 | if (!first_section || first_section->output_section != NULL) |
9f17e2a6 L |
7011 | { |
7012 | map->p_paddr = segment->p_paddr; | |
5c44b38e | 7013 | map->p_paddr_valid = p_paddr_valid; |
9f17e2a6 | 7014 | } |
252b5132 RH |
7015 | |
7016 | /* Determine if this segment contains the ELF file header | |
7017 | and if it contains the program headers themselves. */ | |
bc67d8a6 NC |
7018 | map->includes_filehdr = (segment->p_offset == 0 |
7019 | && segment->p_filesz >= iehdr->e_ehsize); | |
bc67d8a6 | 7020 | map->includes_phdrs = 0; |
252b5132 | 7021 | |
0067a569 | 7022 | if (!phdr_included || segment->p_type != PT_LOAD) |
252b5132 | 7023 | { |
bc67d8a6 NC |
7024 | map->includes_phdrs = |
7025 | (segment->p_offset <= (bfd_vma) iehdr->e_phoff | |
7026 | && (segment->p_offset + segment->p_filesz | |
252b5132 RH |
7027 | >= ((bfd_vma) iehdr->e_phoff |
7028 | + iehdr->e_phnum * iehdr->e_phentsize))); | |
c044fabd | 7029 | |
bc67d8a6 | 7030 | if (segment->p_type == PT_LOAD && map->includes_phdrs) |
b34976b6 | 7031 | phdr_included = TRUE; |
252b5132 RH |
7032 | } |
7033 | ||
bc67d8a6 | 7034 | if (section_count == 0) |
252b5132 RH |
7035 | { |
7036 | /* Special segments, such as the PT_PHDR segment, may contain | |
7037 | no sections, but ordinary, loadable segments should contain | |
1ed89aa9 | 7038 | something. They are allowed by the ELF spec however, so only |
07d6d2b8 | 7039 | a warning is produced. |
f98450c6 NC |
7040 | There is however the valid use case of embedded systems which |
7041 | have segments with p_filesz of 0 and a p_memsz > 0 to initialize | |
7042 | flash memory with zeros. No warning is shown for that case. */ | |
7043 | if (segment->p_type == PT_LOAD | |
7044 | && (segment->p_filesz > 0 || segment->p_memsz == 0)) | |
7045 | /* xgettext:c-format */ | |
9793eb77 AM |
7046 | _bfd_error_handler |
7047 | (_("%pB: warning: empty loadable segment detected" | |
7048 | " at vaddr=%#" PRIx64 ", is this intentional?"), | |
7049 | ibfd, (uint64_t) segment->p_vaddr); | |
252b5132 | 7050 | |
5d695627 | 7051 | map->p_vaddr_offset = segment->p_vaddr; |
bc67d8a6 | 7052 | map->count = 0; |
c044fabd KH |
7053 | *pointer_to_map = map; |
7054 | pointer_to_map = &map->next; | |
252b5132 RH |
7055 | |
7056 | continue; | |
7057 | } | |
7058 | ||
7059 | /* Now scan the sections in the input BFD again and attempt | |
7060 | to add their corresponding output sections to the segment map. | |
7061 | The problem here is how to handle an output section which has | |
7062 | been moved (ie had its LMA changed). There are four possibilities: | |
7063 | ||
7064 | 1. None of the sections have been moved. | |
7065 | In this case we can continue to use the segment LMA from the | |
7066 | input BFD. | |
7067 | ||
7068 | 2. All of the sections have been moved by the same amount. | |
7069 | In this case we can change the segment's LMA to match the LMA | |
7070 | of the first section. | |
7071 | ||
7072 | 3. Some of the sections have been moved, others have not. | |
7073 | In this case those sections which have not been moved can be | |
7074 | placed in the current segment which will have to have its size, | |
7075 | and possibly its LMA changed, and a new segment or segments will | |
7076 | have to be created to contain the other sections. | |
7077 | ||
b5f852ea | 7078 | 4. The sections have been moved, but not by the same amount. |
252b5132 RH |
7079 | In this case we can change the segment's LMA to match the LMA |
7080 | of the first section and we will have to create a new segment | |
7081 | or segments to contain the other sections. | |
7082 | ||
7083 | In order to save time, we allocate an array to hold the section | |
7084 | pointers that we are interested in. As these sections get assigned | |
7085 | to a segment, they are removed from this array. */ | |
7086 | ||
a50b1753 | 7087 | sections = (asection **) bfd_malloc2 (section_count, sizeof (asection *)); |
252b5132 | 7088 | if (sections == NULL) |
b34976b6 | 7089 | return FALSE; |
252b5132 RH |
7090 | |
7091 | /* Step One: Scan for segment vs section LMA conflicts. | |
7092 | Also add the sections to the section array allocated above. | |
7093 | Also add the sections to the current segment. In the common | |
7094 | case, where the sections have not been moved, this means that | |
7095 | we have completely filled the segment, and there is nothing | |
7096 | more to do. */ | |
252b5132 | 7097 | isec = 0; |
9933dc52 AM |
7098 | matching_lma = NULL; |
7099 | suggested_lma = NULL; | |
252b5132 | 7100 | |
461c4b2e | 7101 | for (section = first_section, j = 0; |
bc67d8a6 NC |
7102 | section != NULL; |
7103 | section = section->next) | |
252b5132 | 7104 | { |
caf47ea6 | 7105 | if (INCLUDE_SECTION_IN_SEGMENT (section, segment, bed)) |
c0f7859b | 7106 | { |
bc67d8a6 NC |
7107 | output_section = section->output_section; |
7108 | ||
0067a569 | 7109 | sections[j++] = section; |
252b5132 RH |
7110 | |
7111 | /* The Solaris native linker always sets p_paddr to 0. | |
7112 | We try to catch that case here, and set it to the | |
5e8d7549 NC |
7113 | correct value. Note - some backends require that |
7114 | p_paddr be left as zero. */ | |
5c44b38e | 7115 | if (!p_paddr_valid |
4455705d | 7116 | && segment->p_vaddr != 0 |
0067a569 | 7117 | && !bed->want_p_paddr_set_to_zero |
252b5132 | 7118 | && isec == 0 |
bc67d8a6 | 7119 | && output_section->lma != 0 |
9933dc52 AM |
7120 | && (align_power (segment->p_vaddr |
7121 | + (map->includes_filehdr | |
7122 | ? iehdr->e_ehsize : 0) | |
7123 | + (map->includes_phdrs | |
7124 | ? iehdr->e_phnum * iehdr->e_phentsize | |
7125 | : 0), | |
7126 | output_section->alignment_power) | |
7127 | == output_section->vma)) | |
bc67d8a6 | 7128 | map->p_paddr = segment->p_vaddr; |
252b5132 RH |
7129 | |
7130 | /* Match up the physical address of the segment with the | |
7131 | LMA address of the output section. */ | |
bc67d8a6 | 7132 | if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr) |
5e8d7549 | 7133 | || IS_COREFILE_NOTE (segment, section) |
0067a569 AM |
7134 | || (bed->want_p_paddr_set_to_zero |
7135 | && IS_CONTAINED_BY_VMA (output_section, segment))) | |
252b5132 | 7136 | { |
9933dc52 AM |
7137 | if (matching_lma == NULL |
7138 | || output_section->lma < matching_lma->lma) | |
7139 | matching_lma = output_section; | |
252b5132 RH |
7140 | |
7141 | /* We assume that if the section fits within the segment | |
bc67d8a6 | 7142 | then it does not overlap any other section within that |
252b5132 | 7143 | segment. */ |
0067a569 AM |
7144 | map->sections[isec++] = output_section; |
7145 | } | |
9933dc52 AM |
7146 | else if (suggested_lma == NULL) |
7147 | suggested_lma = output_section; | |
147d51c2 L |
7148 | |
7149 | if (j == section_count) | |
7150 | break; | |
252b5132 RH |
7151 | } |
7152 | } | |
7153 | ||
bc67d8a6 | 7154 | BFD_ASSERT (j == section_count); |
252b5132 RH |
7155 | |
7156 | /* Step Two: Adjust the physical address of the current segment, | |
7157 | if necessary. */ | |
bc67d8a6 | 7158 | if (isec == section_count) |
252b5132 RH |
7159 | { |
7160 | /* All of the sections fitted within the segment as currently | |
7161 | specified. This is the default case. Add the segment to | |
7162 | the list of built segments and carry on to process the next | |
7163 | program header in the input BFD. */ | |
bc67d8a6 | 7164 | map->count = section_count; |
c044fabd KH |
7165 | *pointer_to_map = map; |
7166 | pointer_to_map = &map->next; | |
08a40648 | 7167 | |
5c44b38e AM |
7168 | if (p_paddr_valid |
7169 | && !bed->want_p_paddr_set_to_zero | |
9933dc52 | 7170 | && matching_lma->lma != map->p_paddr |
5c44b38e AM |
7171 | && !map->includes_filehdr |
7172 | && !map->includes_phdrs) | |
3271a814 NS |
7173 | /* There is some padding before the first section in the |
7174 | segment. So, we must account for that in the output | |
7175 | segment's vma. */ | |
5d695627 | 7176 | map->p_vaddr_offset = map->p_paddr - matching_lma->lma; |
08a40648 | 7177 | |
252b5132 RH |
7178 | free (sections); |
7179 | continue; | |
7180 | } | |
252b5132 RH |
7181 | else |
7182 | { | |
9933dc52 AM |
7183 | /* Change the current segment's physical address to match |
7184 | the LMA of the first section that fitted, or if no | |
7185 | section fitted, the first section. */ | |
7186 | if (matching_lma == NULL) | |
7187 | matching_lma = suggested_lma; | |
7188 | ||
7189 | map->p_paddr = matching_lma->lma; | |
72730e0c | 7190 | |
bc67d8a6 NC |
7191 | /* Offset the segment physical address from the lma |
7192 | to allow for space taken up by elf headers. */ | |
9933dc52 | 7193 | if (map->includes_phdrs) |
010c8431 | 7194 | { |
9933dc52 AM |
7195 | map->p_paddr -= iehdr->e_phnum * iehdr->e_phentsize; |
7196 | ||
7197 | /* iehdr->e_phnum is just an estimate of the number | |
7198 | of program headers that we will need. Make a note | |
7199 | here of the number we used and the segment we chose | |
7200 | to hold these headers, so that we can adjust the | |
7201 | offset when we know the correct value. */ | |
7202 | phdr_adjust_num = iehdr->e_phnum; | |
7203 | phdr_adjust_seg = map; | |
010c8431 | 7204 | } |
252b5132 | 7205 | |
9933dc52 | 7206 | if (map->includes_filehdr) |
bc67d8a6 | 7207 | { |
9933dc52 AM |
7208 | bfd_vma align = (bfd_vma) 1 << matching_lma->alignment_power; |
7209 | map->p_paddr -= iehdr->e_ehsize; | |
7210 | /* We've subtracted off the size of headers from the | |
7211 | first section lma, but there may have been some | |
7212 | alignment padding before that section too. Try to | |
7213 | account for that by adjusting the segment lma down to | |
7214 | the same alignment. */ | |
7215 | if (segment->p_align != 0 && segment->p_align < align) | |
7216 | align = segment->p_align; | |
7217 | map->p_paddr &= -align; | |
bc67d8a6 | 7218 | } |
252b5132 RH |
7219 | } |
7220 | ||
7221 | /* Step Three: Loop over the sections again, this time assigning | |
caf47ea6 | 7222 | those that fit to the current segment and removing them from the |
252b5132 RH |
7223 | sections array; but making sure not to leave large gaps. Once all |
7224 | possible sections have been assigned to the current segment it is | |
7225 | added to the list of built segments and if sections still remain | |
7226 | to be assigned, a new segment is constructed before repeating | |
7227 | the loop. */ | |
7228 | isec = 0; | |
7229 | do | |
7230 | { | |
bc67d8a6 | 7231 | map->count = 0; |
9933dc52 | 7232 | suggested_lma = NULL; |
252b5132 RH |
7233 | |
7234 | /* Fill the current segment with sections that fit. */ | |
bc67d8a6 | 7235 | for (j = 0; j < section_count; j++) |
252b5132 | 7236 | { |
bc67d8a6 | 7237 | section = sections[j]; |
252b5132 | 7238 | |
bc67d8a6 | 7239 | if (section == NULL) |
252b5132 RH |
7240 | continue; |
7241 | ||
bc67d8a6 | 7242 | output_section = section->output_section; |
252b5132 | 7243 | |
bc67d8a6 | 7244 | BFD_ASSERT (output_section != NULL); |
c044fabd | 7245 | |
bc67d8a6 NC |
7246 | if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr) |
7247 | || IS_COREFILE_NOTE (segment, section)) | |
252b5132 | 7248 | { |
bc67d8a6 | 7249 | if (map->count == 0) |
252b5132 RH |
7250 | { |
7251 | /* If the first section in a segment does not start at | |
bc67d8a6 NC |
7252 | the beginning of the segment, then something is |
7253 | wrong. */ | |
9933dc52 AM |
7254 | if (align_power (map->p_paddr |
7255 | + (map->includes_filehdr | |
7256 | ? iehdr->e_ehsize : 0) | |
7257 | + (map->includes_phdrs | |
7258 | ? iehdr->e_phnum * iehdr->e_phentsize | |
7259 | : 0), | |
7260 | output_section->alignment_power) | |
7261 | != output_section->lma) | |
252b5132 RH |
7262 | abort (); |
7263 | } | |
7264 | else | |
7265 | { | |
0067a569 | 7266 | asection *prev_sec; |
252b5132 | 7267 | |
bc67d8a6 | 7268 | prev_sec = map->sections[map->count - 1]; |
252b5132 RH |
7269 | |
7270 | /* If the gap between the end of the previous section | |
bc67d8a6 NC |
7271 | and the start of this section is more than |
7272 | maxpagesize then we need to start a new segment. */ | |
eea6121a | 7273 | if ((BFD_ALIGN (prev_sec->lma + prev_sec->size, |
079e9a2f | 7274 | maxpagesize) |
caf47ea6 | 7275 | < BFD_ALIGN (output_section->lma, maxpagesize)) |
0067a569 | 7276 | || (prev_sec->lma + prev_sec->size |
079e9a2f | 7277 | > output_section->lma)) |
252b5132 | 7278 | { |
9933dc52 AM |
7279 | if (suggested_lma == NULL) |
7280 | suggested_lma = output_section; | |
252b5132 RH |
7281 | |
7282 | continue; | |
7283 | } | |
7284 | } | |
7285 | ||
bc67d8a6 | 7286 | map->sections[map->count++] = output_section; |
252b5132 RH |
7287 | ++isec; |
7288 | sections[j] = NULL; | |
9933dc52 AM |
7289 | if (segment->p_type == PT_LOAD) |
7290 | section->segment_mark = TRUE; | |
0067a569 | 7291 | } |
9933dc52 AM |
7292 | else if (suggested_lma == NULL) |
7293 | suggested_lma = output_section; | |
252b5132 RH |
7294 | } |
7295 | ||
beab4532 NC |
7296 | /* PR 23932. A corrupt input file may contain sections that cannot |
7297 | be assigned to any segment - because for example they have a | |
7298 | negative size - or segments that do not contain any sections. */ | |
7299 | if (map->count == 0) | |
7300 | { | |
7301 | bfd_set_error (bfd_error_bad_value); | |
7302 | free (sections); | |
7303 | return FALSE; | |
7304 | } | |
252b5132 RH |
7305 | |
7306 | /* Add the current segment to the list of built segments. */ | |
c044fabd KH |
7307 | *pointer_to_map = map; |
7308 | pointer_to_map = &map->next; | |
252b5132 | 7309 | |
bc67d8a6 | 7310 | if (isec < section_count) |
252b5132 RH |
7311 | { |
7312 | /* We still have not allocated all of the sections to | |
7313 | segments. Create a new segment here, initialise it | |
7314 | and carry on looping. */ | |
00bee008 AM |
7315 | amt = sizeof (struct elf_segment_map) - sizeof (asection *); |
7316 | amt += (bfd_size_type) section_count * sizeof (asection *); | |
5964fc3a | 7317 | map = (struct elf_segment_map *) bfd_zalloc (obfd, amt); |
bc67d8a6 | 7318 | if (map == NULL) |
5ed6aba4 NC |
7319 | { |
7320 | free (sections); | |
7321 | return FALSE; | |
7322 | } | |
252b5132 RH |
7323 | |
7324 | /* Initialise the fields of the segment map. Set the physical | |
7325 | physical address to the LMA of the first section that has | |
7326 | not yet been assigned. */ | |
0067a569 AM |
7327 | map->next = NULL; |
7328 | map->p_type = segment->p_type; | |
7329 | map->p_flags = segment->p_flags; | |
7330 | map->p_flags_valid = 1; | |
9933dc52 | 7331 | map->p_paddr = suggested_lma->lma; |
5c44b38e | 7332 | map->p_paddr_valid = p_paddr_valid; |
bc67d8a6 | 7333 | map->includes_filehdr = 0; |
0067a569 | 7334 | map->includes_phdrs = 0; |
252b5132 RH |
7335 | } |
7336 | } | |
bc67d8a6 | 7337 | while (isec < section_count); |
252b5132 RH |
7338 | |
7339 | free (sections); | |
7340 | } | |
7341 | ||
12bd6957 | 7342 | elf_seg_map (obfd) = map_first; |
bc67d8a6 NC |
7343 | |
7344 | /* If we had to estimate the number of program headers that were | |
9ad5cbcf | 7345 | going to be needed, then check our estimate now and adjust |
bc67d8a6 NC |
7346 | the offset if necessary. */ |
7347 | if (phdr_adjust_seg != NULL) | |
7348 | { | |
7349 | unsigned int count; | |
c044fabd | 7350 | |
bc67d8a6 | 7351 | for (count = 0, map = map_first; map != NULL; map = map->next) |
c044fabd | 7352 | count++; |
252b5132 | 7353 | |
bc67d8a6 NC |
7354 | if (count > phdr_adjust_num) |
7355 | phdr_adjust_seg->p_paddr | |
7356 | -= (count - phdr_adjust_num) * iehdr->e_phentsize; | |
9933dc52 AM |
7357 | |
7358 | for (map = map_first; map != NULL; map = map->next) | |
7359 | if (map->p_type == PT_PHDR) | |
7360 | { | |
7361 | bfd_vma adjust | |
7362 | = phdr_adjust_seg->includes_filehdr ? iehdr->e_ehsize : 0; | |
7363 | map->p_paddr = phdr_adjust_seg->p_paddr + adjust; | |
7364 | break; | |
7365 | } | |
bc67d8a6 | 7366 | } |
c044fabd | 7367 | |
bc67d8a6 | 7368 | #undef SEGMENT_END |
eecdbe52 | 7369 | #undef SECTION_SIZE |
bc67d8a6 NC |
7370 | #undef IS_CONTAINED_BY_VMA |
7371 | #undef IS_CONTAINED_BY_LMA | |
0efc80c8 | 7372 | #undef IS_NOTE |
252b5132 | 7373 | #undef IS_COREFILE_NOTE |
bc67d8a6 | 7374 | #undef IS_SOLARIS_PT_INTERP |
9f17e2a6 | 7375 | #undef IS_SECTION_IN_INPUT_SEGMENT |
bc67d8a6 NC |
7376 | #undef INCLUDE_SECTION_IN_SEGMENT |
7377 | #undef SEGMENT_AFTER_SEGMENT | |
7378 | #undef SEGMENT_OVERLAPS | |
b34976b6 | 7379 | return TRUE; |
252b5132 RH |
7380 | } |
7381 | ||
84d1d650 L |
7382 | /* Copy ELF program header information. */ |
7383 | ||
7384 | static bfd_boolean | |
7385 | copy_elf_program_header (bfd *ibfd, bfd *obfd) | |
7386 | { | |
7387 | Elf_Internal_Ehdr *iehdr; | |
7388 | struct elf_segment_map *map; | |
7389 | struct elf_segment_map *map_first; | |
7390 | struct elf_segment_map **pointer_to_map; | |
7391 | Elf_Internal_Phdr *segment; | |
7392 | unsigned int i; | |
7393 | unsigned int num_segments; | |
7394 | bfd_boolean phdr_included = FALSE; | |
88967714 | 7395 | bfd_boolean p_paddr_valid; |
84d1d650 L |
7396 | |
7397 | iehdr = elf_elfheader (ibfd); | |
7398 | ||
7399 | map_first = NULL; | |
7400 | pointer_to_map = &map_first; | |
7401 | ||
88967714 AM |
7402 | /* If all the segment p_paddr fields are zero, don't set |
7403 | map->p_paddr_valid. */ | |
7404 | p_paddr_valid = FALSE; | |
84d1d650 | 7405 | num_segments = elf_elfheader (ibfd)->e_phnum; |
88967714 AM |
7406 | for (i = 0, segment = elf_tdata (ibfd)->phdr; |
7407 | i < num_segments; | |
7408 | i++, segment++) | |
7409 | if (segment->p_paddr != 0) | |
7410 | { | |
7411 | p_paddr_valid = TRUE; | |
7412 | break; | |
7413 | } | |
7414 | ||
84d1d650 L |
7415 | for (i = 0, segment = elf_tdata (ibfd)->phdr; |
7416 | i < num_segments; | |
7417 | i++, segment++) | |
7418 | { | |
7419 | asection *section; | |
7420 | unsigned int section_count; | |
7421 | bfd_size_type amt; | |
7422 | Elf_Internal_Shdr *this_hdr; | |
53020534 | 7423 | asection *first_section = NULL; |
a76e6f2f | 7424 | asection *lowest_section; |
2542e49e | 7425 | bfd_boolean no_contents = TRUE; |
84d1d650 | 7426 | |
84d1d650 L |
7427 | /* Compute how many sections are in this segment. */ |
7428 | for (section = ibfd->sections, section_count = 0; | |
7429 | section != NULL; | |
7430 | section = section->next) | |
7431 | { | |
7432 | this_hdr = &(elf_section_data(section)->this_hdr); | |
f4638467 | 7433 | if (ELF_SECTION_IN_SEGMENT (this_hdr, segment)) |
3271a814 | 7434 | { |
a76e6f2f AM |
7435 | if (first_section == NULL) |
7436 | first_section = section; | |
2542e49e JL |
7437 | if (elf_section_type (section) != SHT_NOBITS) |
7438 | no_contents = FALSE; | |
3271a814 NS |
7439 | section_count++; |
7440 | } | |
84d1d650 L |
7441 | } |
7442 | ||
7443 | /* Allocate a segment map big enough to contain | |
7444 | all of the sections we have selected. */ | |
00bee008 AM |
7445 | amt = sizeof (struct elf_segment_map) - sizeof (asection *); |
7446 | amt += (bfd_size_type) section_count * sizeof (asection *); | |
a50b1753 | 7447 | map = (struct elf_segment_map *) bfd_zalloc (obfd, amt); |
84d1d650 L |
7448 | if (map == NULL) |
7449 | return FALSE; | |
7450 | ||
7451 | /* Initialize the fields of the output segment map with the | |
7452 | input segment. */ | |
7453 | map->next = NULL; | |
7454 | map->p_type = segment->p_type; | |
7455 | map->p_flags = segment->p_flags; | |
7456 | map->p_flags_valid = 1; | |
7457 | map->p_paddr = segment->p_paddr; | |
88967714 | 7458 | map->p_paddr_valid = p_paddr_valid; |
3f570048 AM |
7459 | map->p_align = segment->p_align; |
7460 | map->p_align_valid = 1; | |
3271a814 | 7461 | map->p_vaddr_offset = 0; |
84d1d650 | 7462 | |
04c3a755 NS |
7463 | if (map->p_type == PT_GNU_RELRO |
7464 | || map->p_type == PT_GNU_STACK) | |
b10a8ae0 L |
7465 | { |
7466 | /* The PT_GNU_RELRO segment may contain the first a few | |
7467 | bytes in the .got.plt section even if the whole .got.plt | |
7468 | section isn't in the PT_GNU_RELRO segment. We won't | |
04c3a755 NS |
7469 | change the size of the PT_GNU_RELRO segment. |
7470 | Similarly, PT_GNU_STACK size is significant on uclinux | |
7471 | systems. */ | |
9433b9b1 | 7472 | map->p_size = segment->p_memsz; |
b10a8ae0 L |
7473 | map->p_size_valid = 1; |
7474 | } | |
7475 | ||
84d1d650 L |
7476 | /* Determine if this segment contains the ELF file header |
7477 | and if it contains the program headers themselves. */ | |
7478 | map->includes_filehdr = (segment->p_offset == 0 | |
7479 | && segment->p_filesz >= iehdr->e_ehsize); | |
7480 | ||
7481 | map->includes_phdrs = 0; | |
7482 | if (! phdr_included || segment->p_type != PT_LOAD) | |
7483 | { | |
7484 | map->includes_phdrs = | |
7485 | (segment->p_offset <= (bfd_vma) iehdr->e_phoff | |
7486 | && (segment->p_offset + segment->p_filesz | |
7487 | >= ((bfd_vma) iehdr->e_phoff | |
7488 | + iehdr->e_phnum * iehdr->e_phentsize))); | |
7489 | ||
7490 | if (segment->p_type == PT_LOAD && map->includes_phdrs) | |
7491 | phdr_included = TRUE; | |
7492 | } | |
7493 | ||
bbefd0a9 | 7494 | lowest_section = NULL; |
84d1d650 L |
7495 | if (section_count != 0) |
7496 | { | |
7497 | unsigned int isec = 0; | |
7498 | ||
53020534 | 7499 | for (section = first_section; |
84d1d650 L |
7500 | section != NULL; |
7501 | section = section->next) | |
7502 | { | |
7503 | this_hdr = &(elf_section_data(section)->this_hdr); | |
f4638467 | 7504 | if (ELF_SECTION_IN_SEGMENT (this_hdr, segment)) |
53020534 L |
7505 | { |
7506 | map->sections[isec++] = section->output_section; | |
a76e6f2f AM |
7507 | if ((section->flags & SEC_ALLOC) != 0) |
7508 | { | |
7509 | bfd_vma seg_off; | |
7510 | ||
bbefd0a9 AM |
7511 | if (lowest_section == NULL |
7512 | || section->lma < lowest_section->lma) | |
fb8a5684 AM |
7513 | lowest_section = section; |
7514 | ||
a76e6f2f AM |
7515 | /* Section lmas are set up from PT_LOAD header |
7516 | p_paddr in _bfd_elf_make_section_from_shdr. | |
7517 | If this header has a p_paddr that disagrees | |
7518 | with the section lma, flag the p_paddr as | |
7519 | invalid. */ | |
7520 | if ((section->flags & SEC_LOAD) != 0) | |
7521 | seg_off = this_hdr->sh_offset - segment->p_offset; | |
7522 | else | |
7523 | seg_off = this_hdr->sh_addr - segment->p_vaddr; | |
7524 | if (section->lma - segment->p_paddr != seg_off) | |
7525 | map->p_paddr_valid = FALSE; | |
7526 | } | |
53020534 L |
7527 | if (isec == section_count) |
7528 | break; | |
7529 | } | |
84d1d650 L |
7530 | } |
7531 | } | |
7532 | ||
a76e6f2f | 7533 | if (map->includes_filehdr && lowest_section != NULL) |
2542e49e JL |
7534 | { |
7535 | /* Try to keep the space used by the headers plus any | |
7536 | padding fixed. If there are sections with file contents | |
7537 | in this segment then the lowest sh_offset is the best | |
7538 | guess. Otherwise the segment only has file contents for | |
7539 | the headers, and p_filesz is the best guess. */ | |
7540 | if (no_contents) | |
7541 | map->header_size = segment->p_filesz; | |
7542 | else | |
7543 | map->header_size = lowest_section->filepos; | |
7544 | } | |
d324f6d6 | 7545 | |
5d695627 AM |
7546 | if (section_count == 0) |
7547 | map->p_vaddr_offset = segment->p_vaddr; | |
7548 | else if (!map->includes_phdrs | |
7549 | && !map->includes_filehdr | |
7550 | && map->p_paddr_valid) | |
7551 | /* Account for padding before the first section. */ | |
7552 | map->p_vaddr_offset = (segment->p_paddr | |
7553 | - (lowest_section ? lowest_section->lma : 0)); | |
a76e6f2f | 7554 | |
84d1d650 L |
7555 | map->count = section_count; |
7556 | *pointer_to_map = map; | |
7557 | pointer_to_map = &map->next; | |
7558 | } | |
7559 | ||
12bd6957 | 7560 | elf_seg_map (obfd) = map_first; |
84d1d650 L |
7561 | return TRUE; |
7562 | } | |
7563 | ||
7564 | /* Copy private BFD data. This copies or rewrites ELF program header | |
7565 | information. */ | |
7566 | ||
7567 | static bfd_boolean | |
7568 | copy_private_bfd_data (bfd *ibfd, bfd *obfd) | |
7569 | { | |
84d1d650 L |
7570 | if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour |
7571 | || bfd_get_flavour (obfd) != bfd_target_elf_flavour) | |
7572 | return TRUE; | |
7573 | ||
7574 | if (elf_tdata (ibfd)->phdr == NULL) | |
7575 | return TRUE; | |
7576 | ||
7577 | if (ibfd->xvec == obfd->xvec) | |
7578 | { | |
cb3ff1e5 NC |
7579 | /* Check to see if any sections in the input BFD |
7580 | covered by ELF program header have changed. */ | |
d55ce4e2 | 7581 | Elf_Internal_Phdr *segment; |
84d1d650 L |
7582 | asection *section, *osec; |
7583 | unsigned int i, num_segments; | |
7584 | Elf_Internal_Shdr *this_hdr; | |
147d51c2 L |
7585 | const struct elf_backend_data *bed; |
7586 | ||
7587 | bed = get_elf_backend_data (ibfd); | |
7588 | ||
7589 | /* Regenerate the segment map if p_paddr is set to 0. */ | |
7590 | if (bed->want_p_paddr_set_to_zero) | |
7591 | goto rewrite; | |
84d1d650 L |
7592 | |
7593 | /* Initialize the segment mark field. */ | |
7594 | for (section = obfd->sections; section != NULL; | |
7595 | section = section->next) | |
7596 | section->segment_mark = FALSE; | |
7597 | ||
7598 | num_segments = elf_elfheader (ibfd)->e_phnum; | |
7599 | for (i = 0, segment = elf_tdata (ibfd)->phdr; | |
7600 | i < num_segments; | |
7601 | i++, segment++) | |
7602 | { | |
5f6999aa NC |
7603 | /* PR binutils/3535. The Solaris linker always sets the p_paddr |
7604 | and p_memsz fields of special segments (DYNAMIC, INTERP) to 0 | |
7605 | which severly confuses things, so always regenerate the segment | |
7606 | map in this case. */ | |
7607 | if (segment->p_paddr == 0 | |
7608 | && segment->p_memsz == 0 | |
7609 | && (segment->p_type == PT_INTERP || segment->p_type == PT_DYNAMIC)) | |
cb3ff1e5 | 7610 | goto rewrite; |
5f6999aa | 7611 | |
84d1d650 L |
7612 | for (section = ibfd->sections; |
7613 | section != NULL; section = section->next) | |
7614 | { | |
7615 | /* We mark the output section so that we know it comes | |
7616 | from the input BFD. */ | |
7617 | osec = section->output_section; | |
7618 | if (osec) | |
7619 | osec->segment_mark = TRUE; | |
7620 | ||
7621 | /* Check if this section is covered by the segment. */ | |
7622 | this_hdr = &(elf_section_data(section)->this_hdr); | |
f4638467 | 7623 | if (ELF_SECTION_IN_SEGMENT (this_hdr, segment)) |
84d1d650 L |
7624 | { |
7625 | /* FIXME: Check if its output section is changed or | |
7626 | removed. What else do we need to check? */ | |
7627 | if (osec == NULL | |
7628 | || section->flags != osec->flags | |
7629 | || section->lma != osec->lma | |
7630 | || section->vma != osec->vma | |
7631 | || section->size != osec->size | |
7632 | || section->rawsize != osec->rawsize | |
7633 | || section->alignment_power != osec->alignment_power) | |
7634 | goto rewrite; | |
7635 | } | |
7636 | } | |
7637 | } | |
7638 | ||
cb3ff1e5 | 7639 | /* Check to see if any output section do not come from the |
84d1d650 L |
7640 | input BFD. */ |
7641 | for (section = obfd->sections; section != NULL; | |
7642 | section = section->next) | |
7643 | { | |
535b785f | 7644 | if (!section->segment_mark) |
84d1d650 L |
7645 | goto rewrite; |
7646 | else | |
7647 | section->segment_mark = FALSE; | |
7648 | } | |
7649 | ||
7650 | return copy_elf_program_header (ibfd, obfd); | |
7651 | } | |
7652 | ||
7653 | rewrite: | |
f1d85785 L |
7654 | if (ibfd->xvec == obfd->xvec) |
7655 | { | |
7656 | /* When rewriting program header, set the output maxpagesize to | |
7657 | the maximum alignment of input PT_LOAD segments. */ | |
7658 | Elf_Internal_Phdr *segment; | |
7659 | unsigned int i; | |
7660 | unsigned int num_segments = elf_elfheader (ibfd)->e_phnum; | |
7661 | bfd_vma maxpagesize = 0; | |
7662 | ||
7663 | for (i = 0, segment = elf_tdata (ibfd)->phdr; | |
7664 | i < num_segments; | |
7665 | i++, segment++) | |
7666 | if (segment->p_type == PT_LOAD | |
7667 | && maxpagesize < segment->p_align) | |
c86934ce NC |
7668 | { |
7669 | /* PR 17512: file: f17299af. */ | |
7670 | if (segment->p_align > (bfd_vma) 1 << ((sizeof (bfd_vma) * 8) - 2)) | |
695344c0 | 7671 | /* xgettext:c-format */ |
2dcf00ce AM |
7672 | _bfd_error_handler (_("%pB: warning: segment alignment of %#" |
7673 | PRIx64 " is too large"), | |
7674 | ibfd, (uint64_t) segment->p_align); | |
c86934ce NC |
7675 | else |
7676 | maxpagesize = segment->p_align; | |
7677 | } | |
f1d85785 L |
7678 | |
7679 | if (maxpagesize != get_elf_backend_data (obfd)->maxpagesize) | |
7680 | bfd_emul_set_maxpagesize (bfd_get_target (obfd), maxpagesize); | |
7681 | } | |
7682 | ||
84d1d650 L |
7683 | return rewrite_elf_program_header (ibfd, obfd); |
7684 | } | |
7685 | ||
ccd2ec6a L |
7686 | /* Initialize private output section information from input section. */ |
7687 | ||
7688 | bfd_boolean | |
7689 | _bfd_elf_init_private_section_data (bfd *ibfd, | |
7690 | asection *isec, | |
7691 | bfd *obfd, | |
7692 | asection *osec, | |
7693 | struct bfd_link_info *link_info) | |
7694 | ||
7695 | { | |
7696 | Elf_Internal_Shdr *ihdr, *ohdr; | |
0e1862bb L |
7697 | bfd_boolean final_link = (link_info != NULL |
7698 | && !bfd_link_relocatable (link_info)); | |
ccd2ec6a L |
7699 | |
7700 | if (ibfd->xvec->flavour != bfd_target_elf_flavour | |
7701 | || obfd->xvec->flavour != bfd_target_elf_flavour) | |
7702 | return TRUE; | |
7703 | ||
ba85c43e NC |
7704 | BFD_ASSERT (elf_section_data (osec) != NULL); |
7705 | ||
dfa7b0b8 AM |
7706 | /* For objcopy and relocatable link, don't copy the output ELF |
7707 | section type from input if the output BFD section flags have been | |
7708 | set to something different. For a final link allow some flags | |
7709 | that the linker clears to differ. */ | |
42bb2e33 | 7710 | if (elf_section_type (osec) == SHT_NULL |
dfa7b0b8 AM |
7711 | && (osec->flags == isec->flags |
7712 | || (final_link | |
7713 | && ((osec->flags ^ isec->flags) | |
0814be7d | 7714 | & ~(SEC_LINK_ONCE | SEC_LINK_DUPLICATES | SEC_RELOC)) == 0))) |
42bb2e33 | 7715 | elf_section_type (osec) = elf_section_type (isec); |
d270463e L |
7716 | |
7717 | /* FIXME: Is this correct for all OS/PROC specific flags? */ | |
7718 | elf_section_flags (osec) |= (elf_section_flags (isec) | |
7719 | & (SHF_MASKOS | SHF_MASKPROC)); | |
ccd2ec6a | 7720 | |
a91e1603 | 7721 | /* Copy sh_info from input for mbind section. */ |
df3a023b AM |
7722 | if ((elf_tdata (ibfd)->has_gnu_osabi & elf_gnu_osabi_mbind) != 0 |
7723 | && elf_section_flags (isec) & SHF_GNU_MBIND) | |
a91e1603 L |
7724 | elf_section_data (osec)->this_hdr.sh_info |
7725 | = elf_section_data (isec)->this_hdr.sh_info; | |
7726 | ||
ccd2ec6a L |
7727 | /* Set things up for objcopy and relocatable link. The output |
7728 | SHT_GROUP section will have its elf_next_in_group pointing back | |
7729 | to the input group members. Ignore linker created group section. | |
7730 | See elfNN_ia64_object_p in elfxx-ia64.c. */ | |
7bdf4127 AB |
7731 | if ((link_info == NULL |
7732 | || !link_info->resolve_section_groups) | |
7733 | && (elf_sec_group (isec) == NULL | |
7734 | || (elf_sec_group (isec)->flags & SEC_LINKER_CREATED) == 0)) | |
ccd2ec6a | 7735 | { |
7bdf4127 AB |
7736 | if (elf_section_flags (isec) & SHF_GROUP) |
7737 | elf_section_flags (osec) |= SHF_GROUP; | |
7738 | elf_next_in_group (osec) = elf_next_in_group (isec); | |
7739 | elf_section_data (osec)->group = elf_section_data (isec)->group; | |
ccd2ec6a L |
7740 | } |
7741 | ||
7bdf4127 AB |
7742 | /* If not decompress, preserve SHF_COMPRESSED. */ |
7743 | if (!final_link && (ibfd->flags & BFD_DECOMPRESS) == 0) | |
7744 | elf_section_flags (osec) |= (elf_section_flags (isec) | |
7745 | & SHF_COMPRESSED); | |
7746 | ||
ccd2ec6a L |
7747 | ihdr = &elf_section_data (isec)->this_hdr; |
7748 | ||
7749 | /* We need to handle elf_linked_to_section for SHF_LINK_ORDER. We | |
7750 | don't use the output section of the linked-to section since it | |
7751 | may be NULL at this point. */ | |
7752 | if ((ihdr->sh_flags & SHF_LINK_ORDER) != 0) | |
7753 | { | |
7754 | ohdr = &elf_section_data (osec)->this_hdr; | |
7755 | ohdr->sh_flags |= SHF_LINK_ORDER; | |
7756 | elf_linked_to_section (osec) = elf_linked_to_section (isec); | |
7757 | } | |
7758 | ||
7759 | osec->use_rela_p = isec->use_rela_p; | |
7760 | ||
7761 | return TRUE; | |
7762 | } | |
7763 | ||
252b5132 RH |
7764 | /* Copy private section information. This copies over the entsize |
7765 | field, and sometimes the info field. */ | |
7766 | ||
b34976b6 | 7767 | bfd_boolean |
217aa764 AM |
7768 | _bfd_elf_copy_private_section_data (bfd *ibfd, |
7769 | asection *isec, | |
7770 | bfd *obfd, | |
7771 | asection *osec) | |
252b5132 RH |
7772 | { |
7773 | Elf_Internal_Shdr *ihdr, *ohdr; | |
7774 | ||
7775 | if (ibfd->xvec->flavour != bfd_target_elf_flavour | |
7776 | || obfd->xvec->flavour != bfd_target_elf_flavour) | |
b34976b6 | 7777 | return TRUE; |
252b5132 | 7778 | |
252b5132 RH |
7779 | ihdr = &elf_section_data (isec)->this_hdr; |
7780 | ohdr = &elf_section_data (osec)->this_hdr; | |
7781 | ||
7782 | ohdr->sh_entsize = ihdr->sh_entsize; | |
7783 | ||
7784 | if (ihdr->sh_type == SHT_SYMTAB | |
7785 | || ihdr->sh_type == SHT_DYNSYM | |
7786 | || ihdr->sh_type == SHT_GNU_verneed | |
7787 | || ihdr->sh_type == SHT_GNU_verdef) | |
7788 | ohdr->sh_info = ihdr->sh_info; | |
7789 | ||
ccd2ec6a L |
7790 | return _bfd_elf_init_private_section_data (ibfd, isec, obfd, osec, |
7791 | NULL); | |
252b5132 RH |
7792 | } |
7793 | ||
d0bf826b AM |
7794 | /* Look at all the SHT_GROUP sections in IBFD, making any adjustments |
7795 | necessary if we are removing either the SHT_GROUP section or any of | |
7796 | the group member sections. DISCARDED is the value that a section's | |
7797 | output_section has if the section will be discarded, NULL when this | |
7798 | function is called from objcopy, bfd_abs_section_ptr when called | |
7799 | from the linker. */ | |
80fccad2 BW |
7800 | |
7801 | bfd_boolean | |
d0bf826b | 7802 | _bfd_elf_fixup_group_sections (bfd *ibfd, asection *discarded) |
80fccad2 | 7803 | { |
30288845 AM |
7804 | asection *isec; |
7805 | ||
30288845 | 7806 | for (isec = ibfd->sections; isec != NULL; isec = isec->next) |
415f38a6 | 7807 | if (elf_section_type (isec) == SHT_GROUP) |
30288845 AM |
7808 | { |
7809 | asection *first = elf_next_in_group (isec); | |
7810 | asection *s = first; | |
d0bf826b AM |
7811 | bfd_size_type removed = 0; |
7812 | ||
30288845 AM |
7813 | while (s != NULL) |
7814 | { | |
415f38a6 AM |
7815 | /* If this member section is being output but the |
7816 | SHT_GROUP section is not, then clear the group info | |
7817 | set up by _bfd_elf_copy_private_section_data. */ | |
d0bf826b AM |
7818 | if (s->output_section != discarded |
7819 | && isec->output_section == discarded) | |
30288845 AM |
7820 | { |
7821 | elf_section_flags (s->output_section) &= ~SHF_GROUP; | |
7822 | elf_group_name (s->output_section) = NULL; | |
7823 | } | |
415f38a6 AM |
7824 | /* Conversely, if the member section is not being output |
7825 | but the SHT_GROUP section is, then adjust its size. */ | |
d0bf826b AM |
7826 | else if (s->output_section == discarded |
7827 | && isec->output_section != discarded) | |
6e5e9d58 AM |
7828 | { |
7829 | struct bfd_elf_section_data *elf_sec = elf_section_data (s); | |
7830 | removed += 4; | |
7831 | if (elf_sec->rel.hdr != NULL | |
7832 | && (elf_sec->rel.hdr->sh_flags & SHF_GROUP) != 0) | |
7833 | removed += 4; | |
7834 | if (elf_sec->rela.hdr != NULL | |
7835 | && (elf_sec->rela.hdr->sh_flags & SHF_GROUP) != 0) | |
7836 | removed += 4; | |
7837 | } | |
30288845 AM |
7838 | s = elf_next_in_group (s); |
7839 | if (s == first) | |
7840 | break; | |
7841 | } | |
d0bf826b AM |
7842 | if (removed != 0) |
7843 | { | |
7844 | if (discarded != NULL) | |
7845 | { | |
7846 | /* If we've been called for ld -r, then we need to | |
6e5e9d58 | 7847 | adjust the input section size. */ |
d0bf826b AM |
7848 | if (isec->rawsize == 0) |
7849 | isec->rawsize = isec->size; | |
7850 | isec->size = isec->rawsize - removed; | |
6e5e9d58 AM |
7851 | if (isec->size <= 4) |
7852 | { | |
7853 | isec->size = 0; | |
7854 | isec->flags |= SEC_EXCLUDE; | |
7855 | } | |
d0bf826b AM |
7856 | } |
7857 | else | |
7858 | { | |
7859 | /* Adjust the output section size when called from | |
7860 | objcopy. */ | |
7861 | isec->output_section->size -= removed; | |
6e5e9d58 AM |
7862 | if (isec->output_section->size <= 4) |
7863 | { | |
7864 | isec->output_section->size = 0; | |
7865 | isec->output_section->flags |= SEC_EXCLUDE; | |
7866 | } | |
d0bf826b AM |
7867 | } |
7868 | } | |
30288845 AM |
7869 | } |
7870 | ||
80fccad2 BW |
7871 | return TRUE; |
7872 | } | |
7873 | ||
d0bf826b AM |
7874 | /* Copy private header information. */ |
7875 | ||
7876 | bfd_boolean | |
7877 | _bfd_elf_copy_private_header_data (bfd *ibfd, bfd *obfd) | |
7878 | { | |
7879 | if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour | |
7880 | || bfd_get_flavour (obfd) != bfd_target_elf_flavour) | |
7881 | return TRUE; | |
7882 | ||
7883 | /* Copy over private BFD data if it has not already been copied. | |
7884 | This must be done here, rather than in the copy_private_bfd_data | |
7885 | entry point, because the latter is called after the section | |
7886 | contents have been set, which means that the program headers have | |
7887 | already been worked out. */ | |
12bd6957 | 7888 | if (elf_seg_map (obfd) == NULL && elf_tdata (ibfd)->phdr != NULL) |
d0bf826b AM |
7889 | { |
7890 | if (! copy_private_bfd_data (ibfd, obfd)) | |
7891 | return FALSE; | |
7892 | } | |
7893 | ||
7894 | return _bfd_elf_fixup_group_sections (ibfd, NULL); | |
7895 | } | |
7896 | ||
252b5132 RH |
7897 | /* Copy private symbol information. If this symbol is in a section |
7898 | which we did not map into a BFD section, try to map the section | |
7899 | index correctly. We use special macro definitions for the mapped | |
7900 | section indices; these definitions are interpreted by the | |
7901 | swap_out_syms function. */ | |
7902 | ||
9ad5cbcf AM |
7903 | #define MAP_ONESYMTAB (SHN_HIOS + 1) |
7904 | #define MAP_DYNSYMTAB (SHN_HIOS + 2) | |
7905 | #define MAP_STRTAB (SHN_HIOS + 3) | |
7906 | #define MAP_SHSTRTAB (SHN_HIOS + 4) | |
7907 | #define MAP_SYM_SHNDX (SHN_HIOS + 5) | |
252b5132 | 7908 | |
b34976b6 | 7909 | bfd_boolean |
217aa764 AM |
7910 | _bfd_elf_copy_private_symbol_data (bfd *ibfd, |
7911 | asymbol *isymarg, | |
7912 | bfd *obfd, | |
7913 | asymbol *osymarg) | |
252b5132 RH |
7914 | { |
7915 | elf_symbol_type *isym, *osym; | |
7916 | ||
7917 | if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour | |
7918 | || bfd_get_flavour (obfd) != bfd_target_elf_flavour) | |
b34976b6 | 7919 | return TRUE; |
252b5132 RH |
7920 | |
7921 | isym = elf_symbol_from (ibfd, isymarg); | |
7922 | osym = elf_symbol_from (obfd, osymarg); | |
7923 | ||
7924 | if (isym != NULL | |
8424d8f5 | 7925 | && isym->internal_elf_sym.st_shndx != 0 |
252b5132 RH |
7926 | && osym != NULL |
7927 | && bfd_is_abs_section (isym->symbol.section)) | |
7928 | { | |
7929 | unsigned int shndx; | |
7930 | ||
7931 | shndx = isym->internal_elf_sym.st_shndx; | |
7932 | if (shndx == elf_onesymtab (ibfd)) | |
7933 | shndx = MAP_ONESYMTAB; | |
7934 | else if (shndx == elf_dynsymtab (ibfd)) | |
7935 | shndx = MAP_DYNSYMTAB; | |
12bd6957 | 7936 | else if (shndx == elf_strtab_sec (ibfd)) |
252b5132 | 7937 | shndx = MAP_STRTAB; |
12bd6957 | 7938 | else if (shndx == elf_shstrtab_sec (ibfd)) |
252b5132 | 7939 | shndx = MAP_SHSTRTAB; |
6a40cf0c | 7940 | else if (find_section_in_list (shndx, elf_symtab_shndx_list (ibfd))) |
9ad5cbcf | 7941 | shndx = MAP_SYM_SHNDX; |
252b5132 RH |
7942 | osym->internal_elf_sym.st_shndx = shndx; |
7943 | } | |
7944 | ||
b34976b6 | 7945 | return TRUE; |
252b5132 RH |
7946 | } |
7947 | ||
7948 | /* Swap out the symbols. */ | |
7949 | ||
b34976b6 | 7950 | static bfd_boolean |
217aa764 | 7951 | swap_out_syms (bfd *abfd, |
ef10c3ac | 7952 | struct elf_strtab_hash **sttp, |
217aa764 | 7953 | int relocatable_p) |
252b5132 | 7954 | { |
9c5bfbb7 | 7955 | const struct elf_backend_data *bed; |
079e9a2f AM |
7956 | int symcount; |
7957 | asymbol **syms; | |
ef10c3ac | 7958 | struct elf_strtab_hash *stt; |
079e9a2f | 7959 | Elf_Internal_Shdr *symtab_hdr; |
9ad5cbcf | 7960 | Elf_Internal_Shdr *symtab_shndx_hdr; |
079e9a2f | 7961 | Elf_Internal_Shdr *symstrtab_hdr; |
ef10c3ac | 7962 | struct elf_sym_strtab *symstrtab; |
f075ee0c AM |
7963 | bfd_byte *outbound_syms; |
7964 | bfd_byte *outbound_shndx; | |
ef10c3ac L |
7965 | unsigned long outbound_syms_index; |
7966 | unsigned long outbound_shndx_index; | |
079e9a2f | 7967 | int idx; |
12bd6957 | 7968 | unsigned int num_locals; |
079e9a2f | 7969 | bfd_size_type amt; |
174fd7f9 | 7970 | bfd_boolean name_local_sections; |
252b5132 | 7971 | |
12bd6957 | 7972 | if (!elf_map_symbols (abfd, &num_locals)) |
b34976b6 | 7973 | return FALSE; |
252b5132 | 7974 | |
c044fabd | 7975 | /* Dump out the symtabs. */ |
ef10c3ac | 7976 | stt = _bfd_elf_strtab_init (); |
079e9a2f | 7977 | if (stt == NULL) |
b34976b6 | 7978 | return FALSE; |
252b5132 | 7979 | |
079e9a2f AM |
7980 | bed = get_elf_backend_data (abfd); |
7981 | symcount = bfd_get_symcount (abfd); | |
7982 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; | |
7983 | symtab_hdr->sh_type = SHT_SYMTAB; | |
7984 | symtab_hdr->sh_entsize = bed->s->sizeof_sym; | |
7985 | symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1); | |
12bd6957 | 7986 | symtab_hdr->sh_info = num_locals + 1; |
72de5009 | 7987 | symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align; |
079e9a2f AM |
7988 | |
7989 | symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr; | |
7990 | symstrtab_hdr->sh_type = SHT_STRTAB; | |
7991 | ||
ef10c3ac | 7992 | /* Allocate buffer to swap out the .strtab section. */ |
7a6e0d89 AM |
7993 | symstrtab = (struct elf_sym_strtab *) bfd_malloc2 (symcount + 1, |
7994 | sizeof (*symstrtab)); | |
ef10c3ac L |
7995 | if (symstrtab == NULL) |
7996 | { | |
7997 | _bfd_elf_strtab_free (stt); | |
7998 | return FALSE; | |
7999 | } | |
8000 | ||
a50b1753 | 8001 | outbound_syms = (bfd_byte *) bfd_alloc2 (abfd, 1 + symcount, |
07d6d2b8 | 8002 | bed->s->sizeof_sym); |
079e9a2f | 8003 | if (outbound_syms == NULL) |
5ed6aba4 | 8004 | { |
ef10c3ac L |
8005 | error_return: |
8006 | _bfd_elf_strtab_free (stt); | |
8007 | free (symstrtab); | |
5ed6aba4 NC |
8008 | return FALSE; |
8009 | } | |
217aa764 | 8010 | symtab_hdr->contents = outbound_syms; |
ef10c3ac | 8011 | outbound_syms_index = 0; |
252b5132 | 8012 | |
9ad5cbcf | 8013 | outbound_shndx = NULL; |
ef10c3ac | 8014 | outbound_shndx_index = 0; |
6a40cf0c NC |
8015 | |
8016 | if (elf_symtab_shndx_list (abfd)) | |
9ad5cbcf | 8017 | { |
6a40cf0c NC |
8018 | symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr; |
8019 | if (symtab_shndx_hdr->sh_name != 0) | |
8020 | { | |
8021 | amt = (bfd_size_type) (1 + symcount) * sizeof (Elf_External_Sym_Shndx); | |
8022 | outbound_shndx = (bfd_byte *) | |
8023 | bfd_zalloc2 (abfd, 1 + symcount, sizeof (Elf_External_Sym_Shndx)); | |
8024 | if (outbound_shndx == NULL) | |
8025 | goto error_return; | |
5ed6aba4 | 8026 | |
6a40cf0c NC |
8027 | symtab_shndx_hdr->contents = outbound_shndx; |
8028 | symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX; | |
8029 | symtab_shndx_hdr->sh_size = amt; | |
8030 | symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx); | |
8031 | symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx); | |
8032 | } | |
8033 | /* FIXME: What about any other headers in the list ? */ | |
9ad5cbcf AM |
8034 | } |
8035 | ||
589e6347 | 8036 | /* Now generate the data (for "contents"). */ |
079e9a2f AM |
8037 | { |
8038 | /* Fill in zeroth symbol and swap it out. */ | |
8039 | Elf_Internal_Sym sym; | |
8040 | sym.st_name = 0; | |
8041 | sym.st_value = 0; | |
8042 | sym.st_size = 0; | |
8043 | sym.st_info = 0; | |
8044 | sym.st_other = 0; | |
8045 | sym.st_shndx = SHN_UNDEF; | |
35fc36a8 | 8046 | sym.st_target_internal = 0; |
ef10c3ac L |
8047 | symstrtab[0].sym = sym; |
8048 | symstrtab[0].dest_index = outbound_syms_index; | |
8049 | symstrtab[0].destshndx_index = outbound_shndx_index; | |
8050 | outbound_syms_index++; | |
9ad5cbcf | 8051 | if (outbound_shndx != NULL) |
ef10c3ac | 8052 | outbound_shndx_index++; |
079e9a2f | 8053 | } |
252b5132 | 8054 | |
174fd7f9 RS |
8055 | name_local_sections |
8056 | = (bed->elf_backend_name_local_section_symbols | |
8057 | && bed->elf_backend_name_local_section_symbols (abfd)); | |
8058 | ||
079e9a2f | 8059 | syms = bfd_get_outsymbols (abfd); |
ef10c3ac | 8060 | for (idx = 0; idx < symcount;) |
252b5132 | 8061 | { |
252b5132 | 8062 | Elf_Internal_Sym sym; |
079e9a2f AM |
8063 | bfd_vma value = syms[idx]->value; |
8064 | elf_symbol_type *type_ptr; | |
8065 | flagword flags = syms[idx]->flags; | |
8066 | int type; | |
252b5132 | 8067 | |
174fd7f9 RS |
8068 | if (!name_local_sections |
8069 | && (flags & (BSF_SECTION_SYM | BSF_GLOBAL)) == BSF_SECTION_SYM) | |
079e9a2f AM |
8070 | { |
8071 | /* Local section symbols have no name. */ | |
ef10c3ac | 8072 | sym.st_name = (unsigned long) -1; |
079e9a2f AM |
8073 | } |
8074 | else | |
8075 | { | |
ef10c3ac L |
8076 | /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize |
8077 | to get the final offset for st_name. */ | |
8078 | sym.st_name | |
8079 | = (unsigned long) _bfd_elf_strtab_add (stt, syms[idx]->name, | |
8080 | FALSE); | |
079e9a2f | 8081 | if (sym.st_name == (unsigned long) -1) |
ef10c3ac | 8082 | goto error_return; |
079e9a2f | 8083 | } |
252b5132 | 8084 | |
079e9a2f | 8085 | type_ptr = elf_symbol_from (abfd, syms[idx]); |
252b5132 | 8086 | |
079e9a2f AM |
8087 | if ((flags & BSF_SECTION_SYM) == 0 |
8088 | && bfd_is_com_section (syms[idx]->section)) | |
8089 | { | |
8090 | /* ELF common symbols put the alignment into the `value' field, | |
8091 | and the size into the `size' field. This is backwards from | |
8092 | how BFD handles it, so reverse it here. */ | |
8093 | sym.st_size = value; | |
8094 | if (type_ptr == NULL | |
8095 | || type_ptr->internal_elf_sym.st_value == 0) | |
8096 | sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value)); | |
8097 | else | |
8098 | sym.st_value = type_ptr->internal_elf_sym.st_value; | |
8099 | sym.st_shndx = _bfd_elf_section_from_bfd_section | |
8100 | (abfd, syms[idx]->section); | |
8101 | } | |
8102 | else | |
8103 | { | |
8104 | asection *sec = syms[idx]->section; | |
cb33740c | 8105 | unsigned int shndx; |
252b5132 | 8106 | |
079e9a2f AM |
8107 | if (sec->output_section) |
8108 | { | |
8109 | value += sec->output_offset; | |
8110 | sec = sec->output_section; | |
8111 | } | |
589e6347 | 8112 | |
079e9a2f AM |
8113 | /* Don't add in the section vma for relocatable output. */ |
8114 | if (! relocatable_p) | |
8115 | value += sec->vma; | |
8116 | sym.st_value = value; | |
8117 | sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0; | |
8118 | ||
8119 | if (bfd_is_abs_section (sec) | |
8120 | && type_ptr != NULL | |
8121 | && type_ptr->internal_elf_sym.st_shndx != 0) | |
8122 | { | |
8123 | /* This symbol is in a real ELF section which we did | |
8124 | not create as a BFD section. Undo the mapping done | |
8125 | by copy_private_symbol_data. */ | |
8126 | shndx = type_ptr->internal_elf_sym.st_shndx; | |
8127 | switch (shndx) | |
8128 | { | |
8129 | case MAP_ONESYMTAB: | |
8130 | shndx = elf_onesymtab (abfd); | |
8131 | break; | |
8132 | case MAP_DYNSYMTAB: | |
8133 | shndx = elf_dynsymtab (abfd); | |
8134 | break; | |
8135 | case MAP_STRTAB: | |
12bd6957 | 8136 | shndx = elf_strtab_sec (abfd); |
079e9a2f AM |
8137 | break; |
8138 | case MAP_SHSTRTAB: | |
12bd6957 | 8139 | shndx = elf_shstrtab_sec (abfd); |
079e9a2f | 8140 | break; |
9ad5cbcf | 8141 | case MAP_SYM_SHNDX: |
6a40cf0c NC |
8142 | if (elf_symtab_shndx_list (abfd)) |
8143 | shndx = elf_symtab_shndx_list (abfd)->ndx; | |
9ad5cbcf | 8144 | break; |
079e9a2f | 8145 | default: |
15bc576a | 8146 | shndx = SHN_ABS; |
079e9a2f AM |
8147 | break; |
8148 | } | |
8149 | } | |
8150 | else | |
8151 | { | |
8152 | shndx = _bfd_elf_section_from_bfd_section (abfd, sec); | |
252b5132 | 8153 | |
cb33740c | 8154 | if (shndx == SHN_BAD) |
079e9a2f AM |
8155 | { |
8156 | asection *sec2; | |
8157 | ||
8158 | /* Writing this would be a hell of a lot easier if | |
8159 | we had some decent documentation on bfd, and | |
8160 | knew what to expect of the library, and what to | |
8161 | demand of applications. For example, it | |
8162 | appears that `objcopy' might not set the | |
8163 | section of a symbol to be a section that is | |
8164 | actually in the output file. */ | |
8165 | sec2 = bfd_get_section_by_name (abfd, sec->name); | |
5df1bc57 AM |
8166 | if (sec2 != NULL) |
8167 | shndx = _bfd_elf_section_from_bfd_section (abfd, sec2); | |
8168 | if (shndx == SHN_BAD) | |
589e6347 | 8169 | { |
695344c0 | 8170 | /* xgettext:c-format */ |
9793eb77 AM |
8171 | _bfd_error_handler |
8172 | (_("unable to find equivalent output section" | |
8173 | " for symbol '%s' from section '%s'"), | |
8174 | syms[idx]->name ? syms[idx]->name : "<Local sym>", | |
8175 | sec->name); | |
811072d8 | 8176 | bfd_set_error (bfd_error_invalid_operation); |
ef10c3ac | 8177 | goto error_return; |
589e6347 | 8178 | } |
079e9a2f AM |
8179 | } |
8180 | } | |
252b5132 | 8181 | |
079e9a2f AM |
8182 | sym.st_shndx = shndx; |
8183 | } | |
252b5132 | 8184 | |
13ae64f3 JJ |
8185 | if ((flags & BSF_THREAD_LOCAL) != 0) |
8186 | type = STT_TLS; | |
d8045f23 NC |
8187 | else if ((flags & BSF_GNU_INDIRECT_FUNCTION) != 0) |
8188 | type = STT_GNU_IFUNC; | |
13ae64f3 | 8189 | else if ((flags & BSF_FUNCTION) != 0) |
079e9a2f AM |
8190 | type = STT_FUNC; |
8191 | else if ((flags & BSF_OBJECT) != 0) | |
8192 | type = STT_OBJECT; | |
d9352518 DB |
8193 | else if ((flags & BSF_RELC) != 0) |
8194 | type = STT_RELC; | |
8195 | else if ((flags & BSF_SRELC) != 0) | |
8196 | type = STT_SRELC; | |
079e9a2f AM |
8197 | else |
8198 | type = STT_NOTYPE; | |
252b5132 | 8199 | |
13ae64f3 JJ |
8200 | if (syms[idx]->section->flags & SEC_THREAD_LOCAL) |
8201 | type = STT_TLS; | |
8202 | ||
589e6347 | 8203 | /* Processor-specific types. */ |
079e9a2f AM |
8204 | if (type_ptr != NULL |
8205 | && bed->elf_backend_get_symbol_type) | |
8206 | type = ((*bed->elf_backend_get_symbol_type) | |
8207 | (&type_ptr->internal_elf_sym, type)); | |
252b5132 | 8208 | |
079e9a2f AM |
8209 | if (flags & BSF_SECTION_SYM) |
8210 | { | |
8211 | if (flags & BSF_GLOBAL) | |
8212 | sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION); | |
8213 | else | |
8214 | sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION); | |
8215 | } | |
8216 | else if (bfd_is_com_section (syms[idx]->section)) | |
0a40daed | 8217 | { |
b8871f35 L |
8218 | if (type != STT_TLS) |
8219 | { | |
8220 | if ((abfd->flags & BFD_CONVERT_ELF_COMMON)) | |
8221 | type = ((abfd->flags & BFD_USE_ELF_STT_COMMON) | |
8222 | ? STT_COMMON : STT_OBJECT); | |
8223 | else | |
8224 | type = ((flags & BSF_ELF_COMMON) != 0 | |
8225 | ? STT_COMMON : STT_OBJECT); | |
8226 | } | |
8227 | sym.st_info = ELF_ST_INFO (STB_GLOBAL, type); | |
0a40daed | 8228 | } |
079e9a2f AM |
8229 | else if (bfd_is_und_section (syms[idx]->section)) |
8230 | sym.st_info = ELF_ST_INFO (((flags & BSF_WEAK) | |
8231 | ? STB_WEAK | |
8232 | : STB_GLOBAL), | |
8233 | type); | |
8234 | else if (flags & BSF_FILE) | |
8235 | sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE); | |
8236 | else | |
8237 | { | |
8238 | int bind = STB_LOCAL; | |
252b5132 | 8239 | |
079e9a2f AM |
8240 | if (flags & BSF_LOCAL) |
8241 | bind = STB_LOCAL; | |
3e7a7d11 NC |
8242 | else if (flags & BSF_GNU_UNIQUE) |
8243 | bind = STB_GNU_UNIQUE; | |
079e9a2f AM |
8244 | else if (flags & BSF_WEAK) |
8245 | bind = STB_WEAK; | |
8246 | else if (flags & BSF_GLOBAL) | |
8247 | bind = STB_GLOBAL; | |
252b5132 | 8248 | |
079e9a2f AM |
8249 | sym.st_info = ELF_ST_INFO (bind, type); |
8250 | } | |
252b5132 | 8251 | |
079e9a2f | 8252 | if (type_ptr != NULL) |
35fc36a8 RS |
8253 | { |
8254 | sym.st_other = type_ptr->internal_elf_sym.st_other; | |
8255 | sym.st_target_internal | |
8256 | = type_ptr->internal_elf_sym.st_target_internal; | |
8257 | } | |
079e9a2f | 8258 | else |
35fc36a8 RS |
8259 | { |
8260 | sym.st_other = 0; | |
8261 | sym.st_target_internal = 0; | |
8262 | } | |
252b5132 | 8263 | |
ef10c3ac L |
8264 | idx++; |
8265 | symstrtab[idx].sym = sym; | |
8266 | symstrtab[idx].dest_index = outbound_syms_index; | |
8267 | symstrtab[idx].destshndx_index = outbound_shndx_index; | |
8268 | ||
8269 | outbound_syms_index++; | |
9ad5cbcf | 8270 | if (outbound_shndx != NULL) |
ef10c3ac L |
8271 | outbound_shndx_index++; |
8272 | } | |
8273 | ||
8274 | /* Finalize the .strtab section. */ | |
8275 | _bfd_elf_strtab_finalize (stt); | |
8276 | ||
8277 | /* Swap out the .strtab section. */ | |
8278 | for (idx = 0; idx <= symcount; idx++) | |
8279 | { | |
8280 | struct elf_sym_strtab *elfsym = &symstrtab[idx]; | |
8281 | if (elfsym->sym.st_name == (unsigned long) -1) | |
8282 | elfsym->sym.st_name = 0; | |
8283 | else | |
8284 | elfsym->sym.st_name = _bfd_elf_strtab_offset (stt, | |
8285 | elfsym->sym.st_name); | |
8286 | bed->s->swap_symbol_out (abfd, &elfsym->sym, | |
8287 | (outbound_syms | |
8288 | + (elfsym->dest_index | |
8289 | * bed->s->sizeof_sym)), | |
8290 | (outbound_shndx | |
8291 | + (elfsym->destshndx_index | |
8292 | * sizeof (Elf_External_Sym_Shndx)))); | |
079e9a2f | 8293 | } |
ef10c3ac | 8294 | free (symstrtab); |
252b5132 | 8295 | |
079e9a2f | 8296 | *sttp = stt; |
ef10c3ac | 8297 | symstrtab_hdr->sh_size = _bfd_elf_strtab_size (stt); |
079e9a2f | 8298 | symstrtab_hdr->sh_type = SHT_STRTAB; |
84865015 | 8299 | symstrtab_hdr->sh_flags = bed->elf_strtab_flags; |
079e9a2f AM |
8300 | symstrtab_hdr->sh_addr = 0; |
8301 | symstrtab_hdr->sh_entsize = 0; | |
8302 | symstrtab_hdr->sh_link = 0; | |
8303 | symstrtab_hdr->sh_info = 0; | |
8304 | symstrtab_hdr->sh_addralign = 1; | |
252b5132 | 8305 | |
b34976b6 | 8306 | return TRUE; |
252b5132 RH |
8307 | } |
8308 | ||
8309 | /* Return the number of bytes required to hold the symtab vector. | |
8310 | ||
8311 | Note that we base it on the count plus 1, since we will null terminate | |
8312 | the vector allocated based on this size. However, the ELF symbol table | |
8313 | always has a dummy entry as symbol #0, so it ends up even. */ | |
8314 | ||
8315 | long | |
217aa764 | 8316 | _bfd_elf_get_symtab_upper_bound (bfd *abfd) |
252b5132 | 8317 | { |
3a551c7a | 8318 | bfd_size_type symcount; |
252b5132 RH |
8319 | long symtab_size; |
8320 | Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr; | |
8321 | ||
8322 | symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym; | |
3a551c7a AM |
8323 | if (symcount >= LONG_MAX / sizeof (asymbol *)) |
8324 | { | |
8325 | bfd_set_error (bfd_error_file_too_big); | |
8326 | return -1; | |
8327 | } | |
b99d1833 AM |
8328 | symtab_size = (symcount + 1) * (sizeof (asymbol *)); |
8329 | if (symcount > 0) | |
8330 | symtab_size -= sizeof (asymbol *); | |
252b5132 RH |
8331 | |
8332 | return symtab_size; | |
8333 | } | |
8334 | ||
8335 | long | |
217aa764 | 8336 | _bfd_elf_get_dynamic_symtab_upper_bound (bfd *abfd) |
252b5132 | 8337 | { |
3a551c7a | 8338 | bfd_size_type symcount; |
252b5132 RH |
8339 | long symtab_size; |
8340 | Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr; | |
8341 | ||
8342 | if (elf_dynsymtab (abfd) == 0) | |
8343 | { | |
8344 | bfd_set_error (bfd_error_invalid_operation); | |
8345 | return -1; | |
8346 | } | |
8347 | ||
8348 | symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym; | |
3a551c7a AM |
8349 | if (symcount >= LONG_MAX / sizeof (asymbol *)) |
8350 | { | |
8351 | bfd_set_error (bfd_error_file_too_big); | |
8352 | return -1; | |
8353 | } | |
b99d1833 AM |
8354 | symtab_size = (symcount + 1) * (sizeof (asymbol *)); |
8355 | if (symcount > 0) | |
8356 | symtab_size -= sizeof (asymbol *); | |
252b5132 RH |
8357 | |
8358 | return symtab_size; | |
8359 | } | |
8360 | ||
8361 | long | |
217aa764 AM |
8362 | _bfd_elf_get_reloc_upper_bound (bfd *abfd ATTRIBUTE_UNUSED, |
8363 | sec_ptr asect) | |
252b5132 | 8364 | { |
242a1159 | 8365 | #if SIZEOF_LONG == SIZEOF_INT |
7a6e0d89 AM |
8366 | if (asect->reloc_count >= LONG_MAX / sizeof (arelent *)) |
8367 | { | |
8368 | bfd_set_error (bfd_error_file_too_big); | |
8369 | return -1; | |
8370 | } | |
242a1159 | 8371 | #endif |
252b5132 RH |
8372 | return (asect->reloc_count + 1) * sizeof (arelent *); |
8373 | } | |
8374 | ||
8375 | /* Canonicalize the relocs. */ | |
8376 | ||
8377 | long | |
217aa764 AM |
8378 | _bfd_elf_canonicalize_reloc (bfd *abfd, |
8379 | sec_ptr section, | |
8380 | arelent **relptr, | |
8381 | asymbol **symbols) | |
252b5132 RH |
8382 | { |
8383 | arelent *tblptr; | |
8384 | unsigned int i; | |
9c5bfbb7 | 8385 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
252b5132 | 8386 | |
b34976b6 | 8387 | if (! bed->s->slurp_reloc_table (abfd, section, symbols, FALSE)) |
252b5132 RH |
8388 | return -1; |
8389 | ||
8390 | tblptr = section->relocation; | |
8391 | for (i = 0; i < section->reloc_count; i++) | |
8392 | *relptr++ = tblptr++; | |
8393 | ||
8394 | *relptr = NULL; | |
8395 | ||
8396 | return section->reloc_count; | |
8397 | } | |
8398 | ||
8399 | long | |
6cee3f79 | 8400 | _bfd_elf_canonicalize_symtab (bfd *abfd, asymbol **allocation) |
252b5132 | 8401 | { |
9c5bfbb7 | 8402 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
217aa764 | 8403 | long symcount = bed->s->slurp_symbol_table (abfd, allocation, FALSE); |
252b5132 RH |
8404 | |
8405 | if (symcount >= 0) | |
ed48ec2e | 8406 | abfd->symcount = symcount; |
252b5132 RH |
8407 | return symcount; |
8408 | } | |
8409 | ||
8410 | long | |
217aa764 AM |
8411 | _bfd_elf_canonicalize_dynamic_symtab (bfd *abfd, |
8412 | asymbol **allocation) | |
252b5132 | 8413 | { |
9c5bfbb7 | 8414 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
217aa764 | 8415 | long symcount = bed->s->slurp_symbol_table (abfd, allocation, TRUE); |
1f70368c DJ |
8416 | |
8417 | if (symcount >= 0) | |
ed48ec2e | 8418 | abfd->dynsymcount = symcount; |
1f70368c | 8419 | return symcount; |
252b5132 RH |
8420 | } |
8421 | ||
8615f3f2 AM |
8422 | /* Return the size required for the dynamic reloc entries. Any loadable |
8423 | section that was actually installed in the BFD, and has type SHT_REL | |
8424 | or SHT_RELA, and uses the dynamic symbol table, is considered to be a | |
8425 | dynamic reloc section. */ | |
252b5132 RH |
8426 | |
8427 | long | |
217aa764 | 8428 | _bfd_elf_get_dynamic_reloc_upper_bound (bfd *abfd) |
252b5132 | 8429 | { |
3a551c7a | 8430 | bfd_size_type count; |
252b5132 RH |
8431 | asection *s; |
8432 | ||
8433 | if (elf_dynsymtab (abfd) == 0) | |
8434 | { | |
8435 | bfd_set_error (bfd_error_invalid_operation); | |
8436 | return -1; | |
8437 | } | |
8438 | ||
3a551c7a | 8439 | count = 1; |
252b5132 | 8440 | for (s = abfd->sections; s != NULL; s = s->next) |
266b05cf | 8441 | if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd) |
252b5132 RH |
8442 | && (elf_section_data (s)->this_hdr.sh_type == SHT_REL |
8443 | || elf_section_data (s)->this_hdr.sh_type == SHT_RELA)) | |
3a551c7a AM |
8444 | { |
8445 | count += s->size / elf_section_data (s)->this_hdr.sh_entsize; | |
8446 | if (count > LONG_MAX / sizeof (arelent *)) | |
8447 | { | |
8448 | bfd_set_error (bfd_error_file_too_big); | |
8449 | return -1; | |
8450 | } | |
8451 | } | |
8452 | return count * sizeof (arelent *); | |
252b5132 RH |
8453 | } |
8454 | ||
8615f3f2 AM |
8455 | /* Canonicalize the dynamic relocation entries. Note that we return the |
8456 | dynamic relocations as a single block, although they are actually | |
8457 | associated with particular sections; the interface, which was | |
8458 | designed for SunOS style shared libraries, expects that there is only | |
8459 | one set of dynamic relocs. Any loadable section that was actually | |
8460 | installed in the BFD, and has type SHT_REL or SHT_RELA, and uses the | |
8461 | dynamic symbol table, is considered to be a dynamic reloc section. */ | |
252b5132 RH |
8462 | |
8463 | long | |
217aa764 AM |
8464 | _bfd_elf_canonicalize_dynamic_reloc (bfd *abfd, |
8465 | arelent **storage, | |
8466 | asymbol **syms) | |
252b5132 | 8467 | { |
217aa764 | 8468 | bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean); |
252b5132 RH |
8469 | asection *s; |
8470 | long ret; | |
8471 | ||
8472 | if (elf_dynsymtab (abfd) == 0) | |
8473 | { | |
8474 | bfd_set_error (bfd_error_invalid_operation); | |
8475 | return -1; | |
8476 | } | |
8477 | ||
8478 | slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table; | |
8479 | ret = 0; | |
8480 | for (s = abfd->sections; s != NULL; s = s->next) | |
8481 | { | |
266b05cf | 8482 | if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd) |
252b5132 RH |
8483 | && (elf_section_data (s)->this_hdr.sh_type == SHT_REL |
8484 | || elf_section_data (s)->this_hdr.sh_type == SHT_RELA)) | |
8485 | { | |
8486 | arelent *p; | |
8487 | long count, i; | |
8488 | ||
b34976b6 | 8489 | if (! (*slurp_relocs) (abfd, s, syms, TRUE)) |
252b5132 | 8490 | return -1; |
eea6121a | 8491 | count = s->size / elf_section_data (s)->this_hdr.sh_entsize; |
252b5132 RH |
8492 | p = s->relocation; |
8493 | for (i = 0; i < count; i++) | |
8494 | *storage++ = p++; | |
8495 | ret += count; | |
8496 | } | |
8497 | } | |
8498 | ||
8499 | *storage = NULL; | |
8500 | ||
8501 | return ret; | |
8502 | } | |
8503 | \f | |
8504 | /* Read in the version information. */ | |
8505 | ||
b34976b6 | 8506 | bfd_boolean |
fc0e6df6 | 8507 | _bfd_elf_slurp_version_tables (bfd *abfd, bfd_boolean default_imported_symver) |
252b5132 RH |
8508 | { |
8509 | bfd_byte *contents = NULL; | |
fc0e6df6 PB |
8510 | unsigned int freeidx = 0; |
8511 | ||
8512 | if (elf_dynverref (abfd) != 0) | |
8513 | { | |
8514 | Elf_Internal_Shdr *hdr; | |
8515 | Elf_External_Verneed *everneed; | |
8516 | Elf_Internal_Verneed *iverneed; | |
8517 | unsigned int i; | |
d0fb9a8d | 8518 | bfd_byte *contents_end; |
fc0e6df6 PB |
8519 | |
8520 | hdr = &elf_tdata (abfd)->dynverref_hdr; | |
8521 | ||
bd61e135 AM |
8522 | if (hdr->sh_info == 0 |
8523 | || hdr->sh_info > hdr->sh_size / sizeof (Elf_External_Verneed)) | |
d0fb9a8d | 8524 | { |
601a03ba | 8525 | error_return_bad_verref: |
4eca0228 | 8526 | _bfd_error_handler |
871b3ab2 | 8527 | (_("%pB: .gnu.version_r invalid entry"), abfd); |
601a03ba | 8528 | bfd_set_error (bfd_error_bad_value); |
d0fb9a8d JJ |
8529 | error_return_verref: |
8530 | elf_tdata (abfd)->verref = NULL; | |
8531 | elf_tdata (abfd)->cverrefs = 0; | |
8532 | goto error_return; | |
8533 | } | |
601a03ba | 8534 | |
7e56c51c NC |
8535 | ufile_ptr filesize = bfd_get_file_size (abfd); |
8536 | if (filesize > 0 && filesize < hdr->sh_size) | |
8537 | { | |
8538 | /* PR 24708: Avoid attempts to allocate a ridiculous amount | |
8539 | of memory. */ | |
8540 | bfd_set_error (bfd_error_no_memory); | |
8541 | _bfd_error_handler | |
8542 | /* xgettext:c-format */ | |
8543 | (_("error: %pB version reference section is too large (%#" PRIx64 " bytes)"), | |
8544 | abfd, (uint64_t) hdr->sh_size); | |
8545 | goto error_return_verref; | |
8546 | } | |
601a03ba AM |
8547 | contents = (bfd_byte *) bfd_malloc (hdr->sh_size); |
8548 | if (contents == NULL) | |
8549 | goto error_return_verref; | |
8550 | ||
fc0e6df6 PB |
8551 | if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0 |
8552 | || bfd_bread (contents, hdr->sh_size, abfd) != hdr->sh_size) | |
d0fb9a8d | 8553 | goto error_return_verref; |
fc0e6df6 | 8554 | |
601a03ba | 8555 | elf_tdata (abfd)->verref = (Elf_Internal_Verneed *) |
bd61e135 | 8556 | bfd_alloc2 (abfd, hdr->sh_info, sizeof (Elf_Internal_Verneed)); |
601a03ba AM |
8557 | |
8558 | if (elf_tdata (abfd)->verref == NULL) | |
d0fb9a8d JJ |
8559 | goto error_return_verref; |
8560 | ||
8561 | BFD_ASSERT (sizeof (Elf_External_Verneed) | |
8562 | == sizeof (Elf_External_Vernaux)); | |
8563 | contents_end = contents + hdr->sh_size - sizeof (Elf_External_Verneed); | |
fc0e6df6 PB |
8564 | everneed = (Elf_External_Verneed *) contents; |
8565 | iverneed = elf_tdata (abfd)->verref; | |
8566 | for (i = 0; i < hdr->sh_info; i++, iverneed++) | |
8567 | { | |
8568 | Elf_External_Vernaux *evernaux; | |
8569 | Elf_Internal_Vernaux *ivernaux; | |
8570 | unsigned int j; | |
8571 | ||
8572 | _bfd_elf_swap_verneed_in (abfd, everneed, iverneed); | |
8573 | ||
8574 | iverneed->vn_bfd = abfd; | |
8575 | ||
8576 | iverneed->vn_filename = | |
8577 | bfd_elf_string_from_elf_section (abfd, hdr->sh_link, | |
8578 | iverneed->vn_file); | |
8579 | if (iverneed->vn_filename == NULL) | |
601a03ba | 8580 | goto error_return_bad_verref; |
fc0e6df6 | 8581 | |
d0fb9a8d JJ |
8582 | if (iverneed->vn_cnt == 0) |
8583 | iverneed->vn_auxptr = NULL; | |
8584 | else | |
8585 | { | |
a50b1753 | 8586 | iverneed->vn_auxptr = (struct elf_internal_vernaux *) |
07d6d2b8 AM |
8587 | bfd_alloc2 (abfd, iverneed->vn_cnt, |
8588 | sizeof (Elf_Internal_Vernaux)); | |
d0fb9a8d JJ |
8589 | if (iverneed->vn_auxptr == NULL) |
8590 | goto error_return_verref; | |
8591 | } | |
8592 | ||
8593 | if (iverneed->vn_aux | |
8594 | > (size_t) (contents_end - (bfd_byte *) everneed)) | |
601a03ba | 8595 | goto error_return_bad_verref; |
fc0e6df6 PB |
8596 | |
8597 | evernaux = ((Elf_External_Vernaux *) | |
8598 | ((bfd_byte *) everneed + iverneed->vn_aux)); | |
8599 | ivernaux = iverneed->vn_auxptr; | |
8600 | for (j = 0; j < iverneed->vn_cnt; j++, ivernaux++) | |
8601 | { | |
8602 | _bfd_elf_swap_vernaux_in (abfd, evernaux, ivernaux); | |
8603 | ||
8604 | ivernaux->vna_nodename = | |
8605 | bfd_elf_string_from_elf_section (abfd, hdr->sh_link, | |
8606 | ivernaux->vna_name); | |
8607 | if (ivernaux->vna_nodename == NULL) | |
601a03ba | 8608 | goto error_return_bad_verref; |
fc0e6df6 | 8609 | |
25ff461f AM |
8610 | if (ivernaux->vna_other > freeidx) |
8611 | freeidx = ivernaux->vna_other; | |
8612 | ||
8613 | ivernaux->vna_nextptr = NULL; | |
8614 | if (ivernaux->vna_next == 0) | |
8615 | { | |
8616 | iverneed->vn_cnt = j + 1; | |
8617 | break; | |
8618 | } | |
fc0e6df6 PB |
8619 | if (j + 1 < iverneed->vn_cnt) |
8620 | ivernaux->vna_nextptr = ivernaux + 1; | |
fc0e6df6 | 8621 | |
d0fb9a8d JJ |
8622 | if (ivernaux->vna_next |
8623 | > (size_t) (contents_end - (bfd_byte *) evernaux)) | |
601a03ba | 8624 | goto error_return_bad_verref; |
d0fb9a8d | 8625 | |
fc0e6df6 PB |
8626 | evernaux = ((Elf_External_Vernaux *) |
8627 | ((bfd_byte *) evernaux + ivernaux->vna_next)); | |
fc0e6df6 PB |
8628 | } |
8629 | ||
25ff461f AM |
8630 | iverneed->vn_nextref = NULL; |
8631 | if (iverneed->vn_next == 0) | |
8632 | break; | |
fc0e6df6 PB |
8633 | if (i + 1 < hdr->sh_info) |
8634 | iverneed->vn_nextref = iverneed + 1; | |
fc0e6df6 | 8635 | |
d0fb9a8d JJ |
8636 | if (iverneed->vn_next |
8637 | > (size_t) (contents_end - (bfd_byte *) everneed)) | |
601a03ba | 8638 | goto error_return_bad_verref; |
d0fb9a8d | 8639 | |
fc0e6df6 PB |
8640 | everneed = ((Elf_External_Verneed *) |
8641 | ((bfd_byte *) everneed + iverneed->vn_next)); | |
8642 | } | |
25ff461f | 8643 | elf_tdata (abfd)->cverrefs = i; |
fc0e6df6 PB |
8644 | |
8645 | free (contents); | |
8646 | contents = NULL; | |
8647 | } | |
252b5132 RH |
8648 | |
8649 | if (elf_dynverdef (abfd) != 0) | |
8650 | { | |
8651 | Elf_Internal_Shdr *hdr; | |
8652 | Elf_External_Verdef *everdef; | |
8653 | Elf_Internal_Verdef *iverdef; | |
f631889e UD |
8654 | Elf_Internal_Verdef *iverdefarr; |
8655 | Elf_Internal_Verdef iverdefmem; | |
252b5132 | 8656 | unsigned int i; |
062e2358 | 8657 | unsigned int maxidx; |
d0fb9a8d | 8658 | bfd_byte *contents_end_def, *contents_end_aux; |
252b5132 RH |
8659 | |
8660 | hdr = &elf_tdata (abfd)->dynverdef_hdr; | |
8661 | ||
601a03ba AM |
8662 | if (hdr->sh_info == 0 || hdr->sh_size < sizeof (Elf_External_Verdef)) |
8663 | { | |
8664 | error_return_bad_verdef: | |
4eca0228 | 8665 | _bfd_error_handler |
871b3ab2 | 8666 | (_("%pB: .gnu.version_d invalid entry"), abfd); |
601a03ba AM |
8667 | bfd_set_error (bfd_error_bad_value); |
8668 | error_return_verdef: | |
8669 | elf_tdata (abfd)->verdef = NULL; | |
8670 | elf_tdata (abfd)->cverdefs = 0; | |
8671 | goto error_return; | |
8672 | } | |
8673 | ||
a50b1753 | 8674 | contents = (bfd_byte *) bfd_malloc (hdr->sh_size); |
252b5132 | 8675 | if (contents == NULL) |
601a03ba | 8676 | goto error_return_verdef; |
252b5132 | 8677 | if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0 |
217aa764 | 8678 | || bfd_bread (contents, hdr->sh_size, abfd) != hdr->sh_size) |
601a03ba | 8679 | goto error_return_verdef; |
d0fb9a8d JJ |
8680 | |
8681 | BFD_ASSERT (sizeof (Elf_External_Verdef) | |
8682 | >= sizeof (Elf_External_Verdaux)); | |
8683 | contents_end_def = contents + hdr->sh_size | |
8684 | - sizeof (Elf_External_Verdef); | |
8685 | contents_end_aux = contents + hdr->sh_size | |
8686 | - sizeof (Elf_External_Verdaux); | |
8687 | ||
f631889e UD |
8688 | /* We know the number of entries in the section but not the maximum |
8689 | index. Therefore we have to run through all entries and find | |
8690 | the maximum. */ | |
252b5132 | 8691 | everdef = (Elf_External_Verdef *) contents; |
f631889e UD |
8692 | maxidx = 0; |
8693 | for (i = 0; i < hdr->sh_info; ++i) | |
8694 | { | |
8695 | _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem); | |
8696 | ||
601a03ba AM |
8697 | if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) == 0) |
8698 | goto error_return_bad_verdef; | |
062e2358 AM |
8699 | if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) > maxidx) |
8700 | maxidx = iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION); | |
f631889e | 8701 | |
25ff461f AM |
8702 | if (iverdefmem.vd_next == 0) |
8703 | break; | |
8704 | ||
d0fb9a8d JJ |
8705 | if (iverdefmem.vd_next |
8706 | > (size_t) (contents_end_def - (bfd_byte *) everdef)) | |
601a03ba | 8707 | goto error_return_bad_verdef; |
d0fb9a8d | 8708 | |
f631889e UD |
8709 | everdef = ((Elf_External_Verdef *) |
8710 | ((bfd_byte *) everdef + iverdefmem.vd_next)); | |
8711 | } | |
8712 | ||
fc0e6df6 PB |
8713 | if (default_imported_symver) |
8714 | { | |
8715 | if (freeidx > maxidx) | |
8716 | maxidx = ++freeidx; | |
8717 | else | |
8718 | freeidx = ++maxidx; | |
8719 | } | |
201159ec | 8720 | |
601a03ba AM |
8721 | elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *) |
8722 | bfd_zalloc2 (abfd, maxidx, sizeof (Elf_Internal_Verdef)); | |
f631889e | 8723 | if (elf_tdata (abfd)->verdef == NULL) |
601a03ba | 8724 | goto error_return_verdef; |
f631889e UD |
8725 | |
8726 | elf_tdata (abfd)->cverdefs = maxidx; | |
8727 | ||
8728 | everdef = (Elf_External_Verdef *) contents; | |
8729 | iverdefarr = elf_tdata (abfd)->verdef; | |
8730 | for (i = 0; i < hdr->sh_info; i++) | |
252b5132 RH |
8731 | { |
8732 | Elf_External_Verdaux *everdaux; | |
8733 | Elf_Internal_Verdaux *iverdaux; | |
8734 | unsigned int j; | |
8735 | ||
f631889e UD |
8736 | _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem); |
8737 | ||
d0fb9a8d | 8738 | if ((iverdefmem.vd_ndx & VERSYM_VERSION) == 0) |
601a03ba | 8739 | goto error_return_bad_verdef; |
d0fb9a8d | 8740 | |
f631889e | 8741 | iverdef = &iverdefarr[(iverdefmem.vd_ndx & VERSYM_VERSION) - 1]; |
595bce75 | 8742 | memcpy (iverdef, &iverdefmem, offsetof (Elf_Internal_Verdef, vd_bfd)); |
252b5132 RH |
8743 | |
8744 | iverdef->vd_bfd = abfd; | |
8745 | ||
d0fb9a8d JJ |
8746 | if (iverdef->vd_cnt == 0) |
8747 | iverdef->vd_auxptr = NULL; | |
8748 | else | |
8749 | { | |
a50b1753 | 8750 | iverdef->vd_auxptr = (struct elf_internal_verdaux *) |
07d6d2b8 AM |
8751 | bfd_alloc2 (abfd, iverdef->vd_cnt, |
8752 | sizeof (Elf_Internal_Verdaux)); | |
d0fb9a8d JJ |
8753 | if (iverdef->vd_auxptr == NULL) |
8754 | goto error_return_verdef; | |
8755 | } | |
8756 | ||
8757 | if (iverdef->vd_aux | |
8758 | > (size_t) (contents_end_aux - (bfd_byte *) everdef)) | |
601a03ba | 8759 | goto error_return_bad_verdef; |
252b5132 RH |
8760 | |
8761 | everdaux = ((Elf_External_Verdaux *) | |
8762 | ((bfd_byte *) everdef + iverdef->vd_aux)); | |
8763 | iverdaux = iverdef->vd_auxptr; | |
8764 | for (j = 0; j < iverdef->vd_cnt; j++, iverdaux++) | |
8765 | { | |
8766 | _bfd_elf_swap_verdaux_in (abfd, everdaux, iverdaux); | |
8767 | ||
8768 | iverdaux->vda_nodename = | |
8769 | bfd_elf_string_from_elf_section (abfd, hdr->sh_link, | |
8770 | iverdaux->vda_name); | |
8771 | if (iverdaux->vda_nodename == NULL) | |
601a03ba | 8772 | goto error_return_bad_verdef; |
252b5132 | 8773 | |
25ff461f AM |
8774 | iverdaux->vda_nextptr = NULL; |
8775 | if (iverdaux->vda_next == 0) | |
8776 | { | |
8777 | iverdef->vd_cnt = j + 1; | |
8778 | break; | |
8779 | } | |
252b5132 RH |
8780 | if (j + 1 < iverdef->vd_cnt) |
8781 | iverdaux->vda_nextptr = iverdaux + 1; | |
252b5132 | 8782 | |
d0fb9a8d JJ |
8783 | if (iverdaux->vda_next |
8784 | > (size_t) (contents_end_aux - (bfd_byte *) everdaux)) | |
601a03ba | 8785 | goto error_return_bad_verdef; |
d0fb9a8d | 8786 | |
252b5132 RH |
8787 | everdaux = ((Elf_External_Verdaux *) |
8788 | ((bfd_byte *) everdaux + iverdaux->vda_next)); | |
8789 | } | |
8790 | ||
595bce75 | 8791 | iverdef->vd_nodename = NULL; |
d0fb9a8d JJ |
8792 | if (iverdef->vd_cnt) |
8793 | iverdef->vd_nodename = iverdef->vd_auxptr->vda_nodename; | |
252b5132 | 8794 | |
25ff461f AM |
8795 | iverdef->vd_nextdef = NULL; |
8796 | if (iverdef->vd_next == 0) | |
8797 | break; | |
d0fb9a8d | 8798 | if ((size_t) (iverdef - iverdefarr) + 1 < maxidx) |
252b5132 | 8799 | iverdef->vd_nextdef = iverdef + 1; |
252b5132 RH |
8800 | |
8801 | everdef = ((Elf_External_Verdef *) | |
8802 | ((bfd_byte *) everdef + iverdef->vd_next)); | |
8803 | } | |
8804 | ||
8805 | free (contents); | |
8806 | contents = NULL; | |
8807 | } | |
fc0e6df6 | 8808 | else if (default_imported_symver) |
252b5132 | 8809 | { |
fc0e6df6 PB |
8810 | if (freeidx < 3) |
8811 | freeidx = 3; | |
8812 | else | |
8813 | freeidx++; | |
252b5132 | 8814 | |
a50b1753 | 8815 | elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *) |
07d6d2b8 | 8816 | bfd_zalloc2 (abfd, freeidx, sizeof (Elf_Internal_Verdef)); |
fc0e6df6 | 8817 | if (elf_tdata (abfd)->verdef == NULL) |
252b5132 RH |
8818 | goto error_return; |
8819 | ||
fc0e6df6 PB |
8820 | elf_tdata (abfd)->cverdefs = freeidx; |
8821 | } | |
252b5132 | 8822 | |
fc0e6df6 PB |
8823 | /* Create a default version based on the soname. */ |
8824 | if (default_imported_symver) | |
8825 | { | |
8826 | Elf_Internal_Verdef *iverdef; | |
8827 | Elf_Internal_Verdaux *iverdaux; | |
252b5132 | 8828 | |
5bb3703f | 8829 | iverdef = &elf_tdata (abfd)->verdef[freeidx - 1]; |
252b5132 | 8830 | |
fc0e6df6 PB |
8831 | iverdef->vd_version = VER_DEF_CURRENT; |
8832 | iverdef->vd_flags = 0; | |
8833 | iverdef->vd_ndx = freeidx; | |
8834 | iverdef->vd_cnt = 1; | |
252b5132 | 8835 | |
fc0e6df6 | 8836 | iverdef->vd_bfd = abfd; |
252b5132 | 8837 | |
fc0e6df6 PB |
8838 | iverdef->vd_nodename = bfd_elf_get_dt_soname (abfd); |
8839 | if (iverdef->vd_nodename == NULL) | |
d0fb9a8d | 8840 | goto error_return_verdef; |
fc0e6df6 | 8841 | iverdef->vd_nextdef = NULL; |
601a03ba AM |
8842 | iverdef->vd_auxptr = ((struct elf_internal_verdaux *) |
8843 | bfd_zalloc (abfd, sizeof (Elf_Internal_Verdaux))); | |
d0fb9a8d JJ |
8844 | if (iverdef->vd_auxptr == NULL) |
8845 | goto error_return_verdef; | |
252b5132 | 8846 | |
fc0e6df6 PB |
8847 | iverdaux = iverdef->vd_auxptr; |
8848 | iverdaux->vda_nodename = iverdef->vd_nodename; | |
252b5132 RH |
8849 | } |
8850 | ||
b34976b6 | 8851 | return TRUE; |
252b5132 RH |
8852 | |
8853 | error_return: | |
5ed6aba4 | 8854 | if (contents != NULL) |
252b5132 | 8855 | free (contents); |
b34976b6 | 8856 | return FALSE; |
252b5132 RH |
8857 | } |
8858 | \f | |
8859 | asymbol * | |
217aa764 | 8860 | _bfd_elf_make_empty_symbol (bfd *abfd) |
252b5132 RH |
8861 | { |
8862 | elf_symbol_type *newsym; | |
8863 | ||
7a6e0d89 | 8864 | newsym = (elf_symbol_type *) bfd_zalloc (abfd, sizeof (*newsym)); |
252b5132 RH |
8865 | if (!newsym) |
8866 | return NULL; | |
201159ec NC |
8867 | newsym->symbol.the_bfd = abfd; |
8868 | return &newsym->symbol; | |
252b5132 RH |
8869 | } |
8870 | ||
8871 | void | |
217aa764 AM |
8872 | _bfd_elf_get_symbol_info (bfd *abfd ATTRIBUTE_UNUSED, |
8873 | asymbol *symbol, | |
8874 | symbol_info *ret) | |
252b5132 RH |
8875 | { |
8876 | bfd_symbol_info (symbol, ret); | |
8877 | } | |
8878 | ||
8879 | /* Return whether a symbol name implies a local symbol. Most targets | |
8880 | use this function for the is_local_label_name entry point, but some | |
8881 | override it. */ | |
8882 | ||
b34976b6 | 8883 | bfd_boolean |
217aa764 AM |
8884 | _bfd_elf_is_local_label_name (bfd *abfd ATTRIBUTE_UNUSED, |
8885 | const char *name) | |
252b5132 RH |
8886 | { |
8887 | /* Normal local symbols start with ``.L''. */ | |
8888 | if (name[0] == '.' && name[1] == 'L') | |
b34976b6 | 8889 | return TRUE; |
252b5132 RH |
8890 | |
8891 | /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate | |
8892 | DWARF debugging symbols starting with ``..''. */ | |
8893 | if (name[0] == '.' && name[1] == '.') | |
b34976b6 | 8894 | return TRUE; |
252b5132 RH |
8895 | |
8896 | /* gcc will sometimes generate symbols beginning with ``_.L_'' when | |
8897 | emitting DWARF debugging output. I suspect this is actually a | |
8898 | small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call | |
8899 | ASM_GENERATE_INTERNAL_LABEL, and this causes the leading | |
8900 | underscore to be emitted on some ELF targets). For ease of use, | |
8901 | we treat such symbols as local. */ | |
8902 | if (name[0] == '_' && name[1] == '.' && name[2] == 'L' && name[3] == '_') | |
b34976b6 | 8903 | return TRUE; |
252b5132 | 8904 | |
b1fa9dd6 NC |
8905 | /* Treat assembler generated fake symbols, dollar local labels and |
8906 | forward-backward labels (aka local labels) as locals. | |
8907 | These labels have the form: | |
8908 | ||
07d6d2b8 | 8909 | L0^A.* (fake symbols) |
b1fa9dd6 NC |
8910 | |
8911 | [.]?L[0123456789]+{^A|^B}[0123456789]* (local labels) | |
8912 | ||
8913 | Versions which start with .L will have already been matched above, | |
8914 | so we only need to match the rest. */ | |
8915 | if (name[0] == 'L' && ISDIGIT (name[1])) | |
8916 | { | |
8917 | bfd_boolean ret = FALSE; | |
8918 | const char * p; | |
8919 | char c; | |
8920 | ||
8921 | for (p = name + 2; (c = *p); p++) | |
8922 | { | |
8923 | if (c == 1 || c == 2) | |
8924 | { | |
8925 | if (c == 1 && p == name + 2) | |
8926 | /* A fake symbol. */ | |
8927 | return TRUE; | |
8928 | ||
8929 | /* FIXME: We are being paranoid here and treating symbols like | |
8930 | L0^Bfoo as if there were non-local, on the grounds that the | |
8931 | assembler will never generate them. But can any symbol | |
8932 | containing an ASCII value in the range 1-31 ever be anything | |
8933 | other than some kind of local ? */ | |
8934 | ret = TRUE; | |
8935 | } | |
8936 | ||
8937 | if (! ISDIGIT (c)) | |
8938 | { | |
8939 | ret = FALSE; | |
8940 | break; | |
8941 | } | |
8942 | } | |
8943 | return ret; | |
8944 | } | |
ffa54770 | 8945 | |
b34976b6 | 8946 | return FALSE; |
252b5132 RH |
8947 | } |
8948 | ||
8949 | alent * | |
217aa764 AM |
8950 | _bfd_elf_get_lineno (bfd *abfd ATTRIBUTE_UNUSED, |
8951 | asymbol *symbol ATTRIBUTE_UNUSED) | |
252b5132 RH |
8952 | { |
8953 | abort (); | |
8954 | return NULL; | |
8955 | } | |
8956 | ||
b34976b6 | 8957 | bfd_boolean |
217aa764 AM |
8958 | _bfd_elf_set_arch_mach (bfd *abfd, |
8959 | enum bfd_architecture arch, | |
8960 | unsigned long machine) | |
252b5132 RH |
8961 | { |
8962 | /* If this isn't the right architecture for this backend, and this | |
8963 | isn't the generic backend, fail. */ | |
8964 | if (arch != get_elf_backend_data (abfd)->arch | |
8965 | && arch != bfd_arch_unknown | |
8966 | && get_elf_backend_data (abfd)->arch != bfd_arch_unknown) | |
b34976b6 | 8967 | return FALSE; |
252b5132 RH |
8968 | |
8969 | return bfd_default_set_arch_mach (abfd, arch, machine); | |
8970 | } | |
8971 | ||
d1fad7c6 NC |
8972 | /* Find the nearest line to a particular section and offset, |
8973 | for error reporting. */ | |
8974 | ||
b34976b6 | 8975 | bfd_boolean |
217aa764 | 8976 | _bfd_elf_find_nearest_line (bfd *abfd, |
217aa764 | 8977 | asymbol **symbols, |
fb167eb2 | 8978 | asection *section, |
217aa764 AM |
8979 | bfd_vma offset, |
8980 | const char **filename_ptr, | |
8981 | const char **functionname_ptr, | |
fb167eb2 AM |
8982 | unsigned int *line_ptr, |
8983 | unsigned int *discriminator_ptr) | |
d1fad7c6 | 8984 | { |
b34976b6 | 8985 | bfd_boolean found; |
d1fad7c6 | 8986 | |
fb167eb2 | 8987 | if (_bfd_dwarf2_find_nearest_line (abfd, symbols, NULL, section, offset, |
4e8a9624 | 8988 | filename_ptr, functionname_ptr, |
fb167eb2 | 8989 | line_ptr, discriminator_ptr, |
9defd221 | 8990 | dwarf_debug_sections, |
e00e8198 AM |
8991 | &elf_tdata (abfd)->dwarf2_find_line_info) |
8992 | || _bfd_dwarf1_find_nearest_line (abfd, symbols, section, offset, | |
8993 | filename_ptr, functionname_ptr, | |
8994 | line_ptr)) | |
d1fad7c6 NC |
8995 | { |
8996 | if (!*functionname_ptr) | |
e00e8198 AM |
8997 | _bfd_elf_find_function (abfd, symbols, section, offset, |
8998 | *filename_ptr ? NULL : filename_ptr, | |
8999 | functionname_ptr); | |
b34976b6 | 9000 | return TRUE; |
d1fad7c6 NC |
9001 | } |
9002 | ||
9003 | if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset, | |
4e8a9624 AM |
9004 | &found, filename_ptr, |
9005 | functionname_ptr, line_ptr, | |
9006 | &elf_tdata (abfd)->line_info)) | |
b34976b6 | 9007 | return FALSE; |
dc43ada5 | 9008 | if (found && (*functionname_ptr || *line_ptr)) |
b34976b6 | 9009 | return TRUE; |
d1fad7c6 NC |
9010 | |
9011 | if (symbols == NULL) | |
b34976b6 | 9012 | return FALSE; |
d1fad7c6 | 9013 | |
e00e8198 AM |
9014 | if (! _bfd_elf_find_function (abfd, symbols, section, offset, |
9015 | filename_ptr, functionname_ptr)) | |
b34976b6 | 9016 | return FALSE; |
d1fad7c6 | 9017 | |
252b5132 | 9018 | *line_ptr = 0; |
b34976b6 | 9019 | return TRUE; |
252b5132 RH |
9020 | } |
9021 | ||
5420f73d L |
9022 | /* Find the line for a symbol. */ |
9023 | ||
9024 | bfd_boolean | |
9025 | _bfd_elf_find_line (bfd *abfd, asymbol **symbols, asymbol *symbol, | |
9026 | const char **filename_ptr, unsigned int *line_ptr) | |
9b8d1a36 | 9027 | { |
fb167eb2 AM |
9028 | return _bfd_dwarf2_find_nearest_line (abfd, symbols, symbol, NULL, 0, |
9029 | filename_ptr, NULL, line_ptr, NULL, | |
9defd221 | 9030 | dwarf_debug_sections, |
fb167eb2 | 9031 | &elf_tdata (abfd)->dwarf2_find_line_info); |
5420f73d L |
9032 | } |
9033 | ||
4ab527b0 FF |
9034 | /* After a call to bfd_find_nearest_line, successive calls to |
9035 | bfd_find_inliner_info can be used to get source information about | |
9036 | each level of function inlining that terminated at the address | |
9037 | passed to bfd_find_nearest_line. Currently this is only supported | |
9038 | for DWARF2 with appropriate DWARF3 extensions. */ | |
9039 | ||
9040 | bfd_boolean | |
9041 | _bfd_elf_find_inliner_info (bfd *abfd, | |
9042 | const char **filename_ptr, | |
9043 | const char **functionname_ptr, | |
9044 | unsigned int *line_ptr) | |
9045 | { | |
9046 | bfd_boolean found; | |
9047 | found = _bfd_dwarf2_find_inliner_info (abfd, filename_ptr, | |
9048 | functionname_ptr, line_ptr, | |
9049 | & elf_tdata (abfd)->dwarf2_find_line_info); | |
9050 | return found; | |
9051 | } | |
9052 | ||
252b5132 | 9053 | int |
a6b96beb | 9054 | _bfd_elf_sizeof_headers (bfd *abfd, struct bfd_link_info *info) |
252b5132 | 9055 | { |
8ded5a0f AM |
9056 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
9057 | int ret = bed->s->sizeof_ehdr; | |
252b5132 | 9058 | |
0e1862bb | 9059 | if (!bfd_link_relocatable (info)) |
8ded5a0f | 9060 | { |
12bd6957 | 9061 | bfd_size_type phdr_size = elf_program_header_size (abfd); |
8ded5a0f | 9062 | |
62d7a5f6 AM |
9063 | if (phdr_size == (bfd_size_type) -1) |
9064 | { | |
9065 | struct elf_segment_map *m; | |
9066 | ||
9067 | phdr_size = 0; | |
12bd6957 | 9068 | for (m = elf_seg_map (abfd); m != NULL; m = m->next) |
62d7a5f6 | 9069 | phdr_size += bed->s->sizeof_phdr; |
8ded5a0f | 9070 | |
62d7a5f6 AM |
9071 | if (phdr_size == 0) |
9072 | phdr_size = get_program_header_size (abfd, info); | |
9073 | } | |
8ded5a0f | 9074 | |
12bd6957 | 9075 | elf_program_header_size (abfd) = phdr_size; |
8ded5a0f AM |
9076 | ret += phdr_size; |
9077 | } | |
9078 | ||
252b5132 RH |
9079 | return ret; |
9080 | } | |
9081 | ||
b34976b6 | 9082 | bfd_boolean |
217aa764 AM |
9083 | _bfd_elf_set_section_contents (bfd *abfd, |
9084 | sec_ptr section, | |
0f867abe | 9085 | const void *location, |
217aa764 AM |
9086 | file_ptr offset, |
9087 | bfd_size_type count) | |
252b5132 RH |
9088 | { |
9089 | Elf_Internal_Shdr *hdr; | |
1b6aeedb | 9090 | file_ptr pos; |
252b5132 RH |
9091 | |
9092 | if (! abfd->output_has_begun | |
217aa764 | 9093 | && ! _bfd_elf_compute_section_file_positions (abfd, NULL)) |
b34976b6 | 9094 | return FALSE; |
252b5132 | 9095 | |
0ce398f1 L |
9096 | if (!count) |
9097 | return TRUE; | |
9098 | ||
252b5132 | 9099 | hdr = &elf_section_data (section)->this_hdr; |
0ce398f1 L |
9100 | if (hdr->sh_offset == (file_ptr) -1) |
9101 | { | |
9102 | /* We must compress this section. Write output to the buffer. */ | |
9103 | unsigned char *contents = hdr->contents; | |
9104 | if ((offset + count) > hdr->sh_size | |
9105 | || (section->flags & SEC_ELF_COMPRESS) == 0 | |
9106 | || contents == NULL) | |
9107 | abort (); | |
9108 | memcpy (contents + offset, location, count); | |
9109 | return TRUE; | |
9110 | } | |
dc810e39 AM |
9111 | pos = hdr->sh_offset + offset; |
9112 | if (bfd_seek (abfd, pos, SEEK_SET) != 0 | |
9113 | || bfd_bwrite (location, count, abfd) != count) | |
b34976b6 | 9114 | return FALSE; |
252b5132 | 9115 | |
b34976b6 | 9116 | return TRUE; |
252b5132 RH |
9117 | } |
9118 | ||
f3185997 | 9119 | bfd_boolean |
217aa764 AM |
9120 | _bfd_elf_no_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED, |
9121 | arelent *cache_ptr ATTRIBUTE_UNUSED, | |
9122 | Elf_Internal_Rela *dst ATTRIBUTE_UNUSED) | |
252b5132 RH |
9123 | { |
9124 | abort (); | |
f3185997 | 9125 | return FALSE; |
252b5132 RH |
9126 | } |
9127 | ||
252b5132 RH |
9128 | /* Try to convert a non-ELF reloc into an ELF one. */ |
9129 | ||
b34976b6 | 9130 | bfd_boolean |
217aa764 | 9131 | _bfd_elf_validate_reloc (bfd *abfd, arelent *areloc) |
252b5132 | 9132 | { |
c044fabd | 9133 | /* Check whether we really have an ELF howto. */ |
252b5132 RH |
9134 | |
9135 | if ((*areloc->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec) | |
9136 | { | |
9137 | bfd_reloc_code_real_type code; | |
9138 | reloc_howto_type *howto; | |
9139 | ||
9140 | /* Alien reloc: Try to determine its type to replace it with an | |
c044fabd | 9141 | equivalent ELF reloc. */ |
252b5132 RH |
9142 | |
9143 | if (areloc->howto->pc_relative) | |
9144 | { | |
9145 | switch (areloc->howto->bitsize) | |
9146 | { | |
9147 | case 8: | |
9148 | code = BFD_RELOC_8_PCREL; | |
9149 | break; | |
9150 | case 12: | |
9151 | code = BFD_RELOC_12_PCREL; | |
9152 | break; | |
9153 | case 16: | |
9154 | code = BFD_RELOC_16_PCREL; | |
9155 | break; | |
9156 | case 24: | |
9157 | code = BFD_RELOC_24_PCREL; | |
9158 | break; | |
9159 | case 32: | |
9160 | code = BFD_RELOC_32_PCREL; | |
9161 | break; | |
9162 | case 64: | |
9163 | code = BFD_RELOC_64_PCREL; | |
9164 | break; | |
9165 | default: | |
9166 | goto fail; | |
9167 | } | |
9168 | ||
9169 | howto = bfd_reloc_type_lookup (abfd, code); | |
9170 | ||
9171 | if (areloc->howto->pcrel_offset != howto->pcrel_offset) | |
9172 | { | |
9173 | if (howto->pcrel_offset) | |
9174 | areloc->addend += areloc->address; | |
9175 | else | |
9176 | areloc->addend -= areloc->address; /* addend is unsigned!! */ | |
9177 | } | |
9178 | } | |
9179 | else | |
9180 | { | |
9181 | switch (areloc->howto->bitsize) | |
9182 | { | |
9183 | case 8: | |
9184 | code = BFD_RELOC_8; | |
9185 | break; | |
9186 | case 14: | |
9187 | code = BFD_RELOC_14; | |
9188 | break; | |
9189 | case 16: | |
9190 | code = BFD_RELOC_16; | |
9191 | break; | |
9192 | case 26: | |
9193 | code = BFD_RELOC_26; | |
9194 | break; | |
9195 | case 32: | |
9196 | code = BFD_RELOC_32; | |
9197 | break; | |
9198 | case 64: | |
9199 | code = BFD_RELOC_64; | |
9200 | break; | |
9201 | default: | |
9202 | goto fail; | |
9203 | } | |
9204 | ||
9205 | howto = bfd_reloc_type_lookup (abfd, code); | |
9206 | } | |
9207 | ||
9208 | if (howto) | |
9209 | areloc->howto = howto; | |
9210 | else | |
9211 | goto fail; | |
9212 | } | |
9213 | ||
b34976b6 | 9214 | return TRUE; |
252b5132 RH |
9215 | |
9216 | fail: | |
0aa13fee AM |
9217 | /* xgettext:c-format */ |
9218 | _bfd_error_handler (_("%pB: %s unsupported"), | |
9219 | abfd, areloc->howto->name); | |
252b5132 | 9220 | bfd_set_error (bfd_error_bad_value); |
b34976b6 | 9221 | return FALSE; |
252b5132 RH |
9222 | } |
9223 | ||
b34976b6 | 9224 | bfd_boolean |
217aa764 | 9225 | _bfd_elf_close_and_cleanup (bfd *abfd) |
252b5132 | 9226 | { |
d9071b0c TG |
9227 | struct elf_obj_tdata *tdata = elf_tdata (abfd); |
9228 | if (bfd_get_format (abfd) == bfd_object && tdata != NULL) | |
252b5132 | 9229 | { |
c0355132 | 9230 | if (elf_tdata (abfd)->o != NULL && elf_shstrtab (abfd) != NULL) |
2b0f7ef9 | 9231 | _bfd_elf_strtab_free (elf_shstrtab (abfd)); |
d9071b0c | 9232 | _bfd_dwarf2_cleanup_debug_info (abfd, &tdata->dwarf2_find_line_info); |
252b5132 RH |
9233 | } |
9234 | ||
9235 | return _bfd_generic_close_and_cleanup (abfd); | |
9236 | } | |
9237 | ||
9238 | /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY | |
9239 | in the relocation's offset. Thus we cannot allow any sort of sanity | |
9240 | range-checking to interfere. There is nothing else to do in processing | |
9241 | this reloc. */ | |
9242 | ||
9243 | bfd_reloc_status_type | |
217aa764 AM |
9244 | _bfd_elf_rel_vtable_reloc_fn |
9245 | (bfd *abfd ATTRIBUTE_UNUSED, arelent *re ATTRIBUTE_UNUSED, | |
fc0a2244 | 9246 | struct bfd_symbol *symbol ATTRIBUTE_UNUSED, |
217aa764 AM |
9247 | void *data ATTRIBUTE_UNUSED, asection *is ATTRIBUTE_UNUSED, |
9248 | bfd *obfd ATTRIBUTE_UNUSED, char **errmsg ATTRIBUTE_UNUSED) | |
252b5132 RH |
9249 | { |
9250 | return bfd_reloc_ok; | |
9251 | } | |
252b5132 RH |
9252 | \f |
9253 | /* Elf core file support. Much of this only works on native | |
9254 | toolchains, since we rely on knowing the | |
9255 | machine-dependent procfs structure in order to pick | |
c044fabd | 9256 | out details about the corefile. */ |
252b5132 RH |
9257 | |
9258 | #ifdef HAVE_SYS_PROCFS_H | |
16231b7b DG |
9259 | /* Needed for new procfs interface on sparc-solaris. */ |
9260 | # define _STRUCTURED_PROC 1 | |
252b5132 RH |
9261 | # include <sys/procfs.h> |
9262 | #endif | |
9263 | ||
261b8d08 PA |
9264 | /* Return a PID that identifies a "thread" for threaded cores, or the |
9265 | PID of the main process for non-threaded cores. */ | |
252b5132 RH |
9266 | |
9267 | static int | |
217aa764 | 9268 | elfcore_make_pid (bfd *abfd) |
252b5132 | 9269 | { |
261b8d08 PA |
9270 | int pid; |
9271 | ||
228e534f | 9272 | pid = elf_tdata (abfd)->core->lwpid; |
261b8d08 | 9273 | if (pid == 0) |
228e534f | 9274 | pid = elf_tdata (abfd)->core->pid; |
261b8d08 PA |
9275 | |
9276 | return pid; | |
252b5132 RH |
9277 | } |
9278 | ||
252b5132 RH |
9279 | /* If there isn't a section called NAME, make one, using |
9280 | data from SECT. Note, this function will generate a | |
9281 | reference to NAME, so you shouldn't deallocate or | |
c044fabd | 9282 | overwrite it. */ |
252b5132 | 9283 | |
b34976b6 | 9284 | static bfd_boolean |
217aa764 | 9285 | elfcore_maybe_make_sect (bfd *abfd, char *name, asection *sect) |
252b5132 | 9286 | { |
c044fabd | 9287 | asection *sect2; |
252b5132 RH |
9288 | |
9289 | if (bfd_get_section_by_name (abfd, name) != NULL) | |
b34976b6 | 9290 | return TRUE; |
252b5132 | 9291 | |
117ed4f8 | 9292 | sect2 = bfd_make_section_with_flags (abfd, name, sect->flags); |
252b5132 | 9293 | if (sect2 == NULL) |
b34976b6 | 9294 | return FALSE; |
252b5132 | 9295 | |
eea6121a | 9296 | sect2->size = sect->size; |
252b5132 | 9297 | sect2->filepos = sect->filepos; |
252b5132 | 9298 | sect2->alignment_power = sect->alignment_power; |
b34976b6 | 9299 | return TRUE; |
252b5132 RH |
9300 | } |
9301 | ||
bb0082d6 AM |
9302 | /* Create a pseudosection containing SIZE bytes at FILEPOS. This |
9303 | actually creates up to two pseudosections: | |
9304 | - For the single-threaded case, a section named NAME, unless | |
9305 | such a section already exists. | |
9306 | - For the multi-threaded case, a section named "NAME/PID", where | |
9307 | PID is elfcore_make_pid (abfd). | |
24d3e51b | 9308 | Both pseudosections have identical contents. */ |
b34976b6 | 9309 | bfd_boolean |
217aa764 AM |
9310 | _bfd_elfcore_make_pseudosection (bfd *abfd, |
9311 | char *name, | |
9312 | size_t size, | |
9313 | ufile_ptr filepos) | |
bb0082d6 AM |
9314 | { |
9315 | char buf[100]; | |
9316 | char *threaded_name; | |
d4c88bbb | 9317 | size_t len; |
bb0082d6 AM |
9318 | asection *sect; |
9319 | ||
9320 | /* Build the section name. */ | |
9321 | ||
9322 | sprintf (buf, "%s/%d", name, elfcore_make_pid (abfd)); | |
d4c88bbb | 9323 | len = strlen (buf) + 1; |
a50b1753 | 9324 | threaded_name = (char *) bfd_alloc (abfd, len); |
bb0082d6 | 9325 | if (threaded_name == NULL) |
b34976b6 | 9326 | return FALSE; |
d4c88bbb | 9327 | memcpy (threaded_name, buf, len); |
bb0082d6 | 9328 | |
117ed4f8 AM |
9329 | sect = bfd_make_section_anyway_with_flags (abfd, threaded_name, |
9330 | SEC_HAS_CONTENTS); | |
bb0082d6 | 9331 | if (sect == NULL) |
b34976b6 | 9332 | return FALSE; |
eea6121a | 9333 | sect->size = size; |
bb0082d6 | 9334 | sect->filepos = filepos; |
bb0082d6 AM |
9335 | sect->alignment_power = 2; |
9336 | ||
936e320b | 9337 | return elfcore_maybe_make_sect (abfd, name, sect); |
bb0082d6 AM |
9338 | } |
9339 | ||
58e07198 CZ |
9340 | static bfd_boolean |
9341 | elfcore_make_auxv_note_section (bfd *abfd, Elf_Internal_Note *note, | |
9342 | size_t offs) | |
9343 | { | |
9344 | asection *sect = bfd_make_section_anyway_with_flags (abfd, ".auxv", | |
9345 | SEC_HAS_CONTENTS); | |
9346 | ||
9347 | if (sect == NULL) | |
9348 | return FALSE; | |
9349 | ||
9350 | sect->size = note->descsz - offs; | |
9351 | sect->filepos = note->descpos + offs; | |
9352 | sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32; | |
9353 | ||
9354 | return TRUE; | |
9355 | } | |
9356 | ||
252b5132 | 9357 | /* prstatus_t exists on: |
4a938328 | 9358 | solaris 2.5+ |
252b5132 RH |
9359 | linux 2.[01] + glibc |
9360 | unixware 4.2 | |
9361 | */ | |
9362 | ||
9363 | #if defined (HAVE_PRSTATUS_T) | |
a7b97311 | 9364 | |
b34976b6 | 9365 | static bfd_boolean |
217aa764 | 9366 | elfcore_grok_prstatus (bfd *abfd, Elf_Internal_Note *note) |
252b5132 | 9367 | { |
eea6121a | 9368 | size_t size; |
7ee38065 | 9369 | int offset; |
252b5132 | 9370 | |
4a938328 MS |
9371 | if (note->descsz == sizeof (prstatus_t)) |
9372 | { | |
9373 | prstatus_t prstat; | |
252b5132 | 9374 | |
eea6121a | 9375 | size = sizeof (prstat.pr_reg); |
7ee38065 | 9376 | offset = offsetof (prstatus_t, pr_reg); |
4a938328 | 9377 | memcpy (&prstat, note->descdata, sizeof (prstat)); |
252b5132 | 9378 | |
fa49d224 NC |
9379 | /* Do not overwrite the core signal if it |
9380 | has already been set by another thread. */ | |
228e534f AM |
9381 | if (elf_tdata (abfd)->core->signal == 0) |
9382 | elf_tdata (abfd)->core->signal = prstat.pr_cursig; | |
9383 | if (elf_tdata (abfd)->core->pid == 0) | |
9384 | elf_tdata (abfd)->core->pid = prstat.pr_pid; | |
252b5132 | 9385 | |
4a938328 MS |
9386 | /* pr_who exists on: |
9387 | solaris 2.5+ | |
9388 | unixware 4.2 | |
9389 | pr_who doesn't exist on: | |
9390 | linux 2.[01] | |
9391 | */ | |
252b5132 | 9392 | #if defined (HAVE_PRSTATUS_T_PR_WHO) |
228e534f | 9393 | elf_tdata (abfd)->core->lwpid = prstat.pr_who; |
261b8d08 | 9394 | #else |
228e534f | 9395 | elf_tdata (abfd)->core->lwpid = prstat.pr_pid; |
252b5132 | 9396 | #endif |
4a938328 | 9397 | } |
7ee38065 | 9398 | #if defined (HAVE_PRSTATUS32_T) |
4a938328 MS |
9399 | else if (note->descsz == sizeof (prstatus32_t)) |
9400 | { | |
9401 | /* 64-bit host, 32-bit corefile */ | |
9402 | prstatus32_t prstat; | |
9403 | ||
eea6121a | 9404 | size = sizeof (prstat.pr_reg); |
7ee38065 | 9405 | offset = offsetof (prstatus32_t, pr_reg); |
4a938328 MS |
9406 | memcpy (&prstat, note->descdata, sizeof (prstat)); |
9407 | ||
fa49d224 NC |
9408 | /* Do not overwrite the core signal if it |
9409 | has already been set by another thread. */ | |
228e534f AM |
9410 | if (elf_tdata (abfd)->core->signal == 0) |
9411 | elf_tdata (abfd)->core->signal = prstat.pr_cursig; | |
9412 | if (elf_tdata (abfd)->core->pid == 0) | |
9413 | elf_tdata (abfd)->core->pid = prstat.pr_pid; | |
4a938328 MS |
9414 | |
9415 | /* pr_who exists on: | |
9416 | solaris 2.5+ | |
9417 | unixware 4.2 | |
9418 | pr_who doesn't exist on: | |
9419 | linux 2.[01] | |
9420 | */ | |
7ee38065 | 9421 | #if defined (HAVE_PRSTATUS32_T_PR_WHO) |
228e534f | 9422 | elf_tdata (abfd)->core->lwpid = prstat.pr_who; |
261b8d08 | 9423 | #else |
228e534f | 9424 | elf_tdata (abfd)->core->lwpid = prstat.pr_pid; |
4a938328 MS |
9425 | #endif |
9426 | } | |
7ee38065 | 9427 | #endif /* HAVE_PRSTATUS32_T */ |
4a938328 MS |
9428 | else |
9429 | { | |
9430 | /* Fail - we don't know how to handle any other | |
9431 | note size (ie. data object type). */ | |
b34976b6 | 9432 | return TRUE; |
4a938328 | 9433 | } |
252b5132 | 9434 | |
bb0082d6 | 9435 | /* Make a ".reg/999" section and a ".reg" section. */ |
936e320b | 9436 | return _bfd_elfcore_make_pseudosection (abfd, ".reg", |
eea6121a | 9437 | size, note->descpos + offset); |
252b5132 RH |
9438 | } |
9439 | #endif /* defined (HAVE_PRSTATUS_T) */ | |
9440 | ||
bb0082d6 | 9441 | /* Create a pseudosection containing the exact contents of NOTE. */ |
b34976b6 | 9442 | static bfd_boolean |
217aa764 AM |
9443 | elfcore_make_note_pseudosection (bfd *abfd, |
9444 | char *name, | |
9445 | Elf_Internal_Note *note) | |
252b5132 | 9446 | { |
936e320b AM |
9447 | return _bfd_elfcore_make_pseudosection (abfd, name, |
9448 | note->descsz, note->descpos); | |
252b5132 RH |
9449 | } |
9450 | ||
ff08c6bb JB |
9451 | /* There isn't a consistent prfpregset_t across platforms, |
9452 | but it doesn't matter, because we don't have to pick this | |
c044fabd KH |
9453 | data structure apart. */ |
9454 | ||
b34976b6 | 9455 | static bfd_boolean |
217aa764 | 9456 | elfcore_grok_prfpreg (bfd *abfd, Elf_Internal_Note *note) |
ff08c6bb JB |
9457 | { |
9458 | return elfcore_make_note_pseudosection (abfd, ".reg2", note); | |
9459 | } | |
9460 | ||
ff08c6bb | 9461 | /* Linux dumps the Intel SSE regs in a note named "LINUX" with a note |
971d4640 | 9462 | type of NT_PRXFPREG. Just include the whole note's contents |
ff08c6bb | 9463 | literally. */ |
c044fabd | 9464 | |
b34976b6 | 9465 | static bfd_boolean |
217aa764 | 9466 | elfcore_grok_prxfpreg (bfd *abfd, Elf_Internal_Note *note) |
ff08c6bb JB |
9467 | { |
9468 | return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note); | |
9469 | } | |
9470 | ||
4339cae0 L |
9471 | /* Linux dumps the Intel XSAVE extended state in a note named "LINUX" |
9472 | with a note type of NT_X86_XSTATE. Just include the whole note's | |
9473 | contents literally. */ | |
9474 | ||
9475 | static bfd_boolean | |
9476 | elfcore_grok_xstatereg (bfd *abfd, Elf_Internal_Note *note) | |
9477 | { | |
9478 | return elfcore_make_note_pseudosection (abfd, ".reg-xstate", note); | |
9479 | } | |
9480 | ||
97753bd5 AM |
9481 | static bfd_boolean |
9482 | elfcore_grok_ppc_vmx (bfd *abfd, Elf_Internal_Note *note) | |
9483 | { | |
9484 | return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vmx", note); | |
9485 | } | |
9486 | ||
89eeb0bc LM |
9487 | static bfd_boolean |
9488 | elfcore_grok_ppc_vsx (bfd *abfd, Elf_Internal_Note *note) | |
9489 | { | |
9490 | return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vsx", note); | |
9491 | } | |
97753bd5 | 9492 | |
cb2366c1 EBM |
9493 | static bfd_boolean |
9494 | elfcore_grok_ppc_tar (bfd *abfd, Elf_Internal_Note *note) | |
9495 | { | |
9496 | return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tar", note); | |
9497 | } | |
9498 | ||
9499 | static bfd_boolean | |
9500 | elfcore_grok_ppc_ppr (bfd *abfd, Elf_Internal_Note *note) | |
9501 | { | |
9502 | return elfcore_make_note_pseudosection (abfd, ".reg-ppc-ppr", note); | |
9503 | } | |
9504 | ||
9505 | static bfd_boolean | |
9506 | elfcore_grok_ppc_dscr (bfd *abfd, Elf_Internal_Note *note) | |
9507 | { | |
9508 | return elfcore_make_note_pseudosection (abfd, ".reg-ppc-dscr", note); | |
9509 | } | |
9510 | ||
9511 | static bfd_boolean | |
9512 | elfcore_grok_ppc_ebb (bfd *abfd, Elf_Internal_Note *note) | |
9513 | { | |
9514 | return elfcore_make_note_pseudosection (abfd, ".reg-ppc-ebb", note); | |
9515 | } | |
9516 | ||
9517 | static bfd_boolean | |
9518 | elfcore_grok_ppc_pmu (bfd *abfd, Elf_Internal_Note *note) | |
9519 | { | |
9520 | return elfcore_make_note_pseudosection (abfd, ".reg-ppc-pmu", note); | |
9521 | } | |
9522 | ||
9523 | static bfd_boolean | |
9524 | elfcore_grok_ppc_tm_cgpr (bfd *abfd, Elf_Internal_Note *note) | |
9525 | { | |
9526 | return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cgpr", note); | |
9527 | } | |
9528 | ||
9529 | static bfd_boolean | |
9530 | elfcore_grok_ppc_tm_cfpr (bfd *abfd, Elf_Internal_Note *note) | |
9531 | { | |
9532 | return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cfpr", note); | |
9533 | } | |
9534 | ||
9535 | static bfd_boolean | |
9536 | elfcore_grok_ppc_tm_cvmx (bfd *abfd, Elf_Internal_Note *note) | |
9537 | { | |
9538 | return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cvmx", note); | |
9539 | } | |
9540 | ||
9541 | static bfd_boolean | |
9542 | elfcore_grok_ppc_tm_cvsx (bfd *abfd, Elf_Internal_Note *note) | |
9543 | { | |
9544 | return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cvsx", note); | |
9545 | } | |
9546 | ||
9547 | static bfd_boolean | |
9548 | elfcore_grok_ppc_tm_spr (bfd *abfd, Elf_Internal_Note *note) | |
9549 | { | |
9550 | return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-spr", note); | |
9551 | } | |
9552 | ||
9553 | static bfd_boolean | |
9554 | elfcore_grok_ppc_tm_ctar (bfd *abfd, Elf_Internal_Note *note) | |
9555 | { | |
9556 | return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-ctar", note); | |
9557 | } | |
9558 | ||
9559 | static bfd_boolean | |
9560 | elfcore_grok_ppc_tm_cppr (bfd *abfd, Elf_Internal_Note *note) | |
9561 | { | |
9562 | return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cppr", note); | |
9563 | } | |
9564 | ||
9565 | static bfd_boolean | |
9566 | elfcore_grok_ppc_tm_cdscr (bfd *abfd, Elf_Internal_Note *note) | |
9567 | { | |
9568 | return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cdscr", note); | |
9569 | } | |
9570 | ||
0675e188 UW |
9571 | static bfd_boolean |
9572 | elfcore_grok_s390_high_gprs (bfd *abfd, Elf_Internal_Note *note) | |
9573 | { | |
9574 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-high-gprs", note); | |
9575 | } | |
9576 | ||
d7eeb400 MS |
9577 | static bfd_boolean |
9578 | elfcore_grok_s390_timer (bfd *abfd, Elf_Internal_Note *note) | |
9579 | { | |
9580 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-timer", note); | |
9581 | } | |
9582 | ||
9583 | static bfd_boolean | |
9584 | elfcore_grok_s390_todcmp (bfd *abfd, Elf_Internal_Note *note) | |
9585 | { | |
9586 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-todcmp", note); | |
9587 | } | |
9588 | ||
9589 | static bfd_boolean | |
9590 | elfcore_grok_s390_todpreg (bfd *abfd, Elf_Internal_Note *note) | |
9591 | { | |
9592 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-todpreg", note); | |
9593 | } | |
9594 | ||
9595 | static bfd_boolean | |
9596 | elfcore_grok_s390_ctrs (bfd *abfd, Elf_Internal_Note *note) | |
9597 | { | |
9598 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-ctrs", note); | |
9599 | } | |
9600 | ||
9601 | static bfd_boolean | |
9602 | elfcore_grok_s390_prefix (bfd *abfd, Elf_Internal_Note *note) | |
9603 | { | |
9604 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-prefix", note); | |
9605 | } | |
9606 | ||
355b81d9 UW |
9607 | static bfd_boolean |
9608 | elfcore_grok_s390_last_break (bfd *abfd, Elf_Internal_Note *note) | |
9609 | { | |
9610 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-last-break", note); | |
9611 | } | |
9612 | ||
9613 | static bfd_boolean | |
9614 | elfcore_grok_s390_system_call (bfd *abfd, Elf_Internal_Note *note) | |
9615 | { | |
9616 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-system-call", note); | |
9617 | } | |
9618 | ||
abb3f6cc NC |
9619 | static bfd_boolean |
9620 | elfcore_grok_s390_tdb (bfd *abfd, Elf_Internal_Note *note) | |
9621 | { | |
9622 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-tdb", note); | |
9623 | } | |
9624 | ||
4ef9f41a AA |
9625 | static bfd_boolean |
9626 | elfcore_grok_s390_vxrs_low (bfd *abfd, Elf_Internal_Note *note) | |
9627 | { | |
9628 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-vxrs-low", note); | |
9629 | } | |
9630 | ||
9631 | static bfd_boolean | |
9632 | elfcore_grok_s390_vxrs_high (bfd *abfd, Elf_Internal_Note *note) | |
9633 | { | |
9634 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-vxrs-high", note); | |
9635 | } | |
9636 | ||
88ab90e8 AA |
9637 | static bfd_boolean |
9638 | elfcore_grok_s390_gs_cb (bfd *abfd, Elf_Internal_Note *note) | |
9639 | { | |
9640 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-gs-cb", note); | |
9641 | } | |
9642 | ||
9643 | static bfd_boolean | |
9644 | elfcore_grok_s390_gs_bc (bfd *abfd, Elf_Internal_Note *note) | |
9645 | { | |
9646 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-gs-bc", note); | |
9647 | } | |
9648 | ||
faa9a424 UW |
9649 | static bfd_boolean |
9650 | elfcore_grok_arm_vfp (bfd *abfd, Elf_Internal_Note *note) | |
9651 | { | |
9652 | return elfcore_make_note_pseudosection (abfd, ".reg-arm-vfp", note); | |
9653 | } | |
9654 | ||
652451f8 YZ |
9655 | static bfd_boolean |
9656 | elfcore_grok_aarch_tls (bfd *abfd, Elf_Internal_Note *note) | |
9657 | { | |
9658 | return elfcore_make_note_pseudosection (abfd, ".reg-aarch-tls", note); | |
9659 | } | |
9660 | ||
9661 | static bfd_boolean | |
9662 | elfcore_grok_aarch_hw_break (bfd *abfd, Elf_Internal_Note *note) | |
9663 | { | |
9664 | return elfcore_make_note_pseudosection (abfd, ".reg-aarch-hw-break", note); | |
9665 | } | |
9666 | ||
9667 | static bfd_boolean | |
9668 | elfcore_grok_aarch_hw_watch (bfd *abfd, Elf_Internal_Note *note) | |
9669 | { | |
9670 | return elfcore_make_note_pseudosection (abfd, ".reg-aarch-hw-watch", note); | |
9671 | } | |
9672 | ||
ad1cc4e4 AH |
9673 | static bfd_boolean |
9674 | elfcore_grok_aarch_sve (bfd *abfd, Elf_Internal_Note *note) | |
9675 | { | |
9676 | return elfcore_make_note_pseudosection (abfd, ".reg-aarch-sve", note); | |
9677 | } | |
9678 | ||
e6c3b5bf AH |
9679 | static bfd_boolean |
9680 | elfcore_grok_aarch_pauth (bfd *abfd, Elf_Internal_Note *note) | |
9681 | { | |
9682 | return elfcore_make_note_pseudosection (abfd, ".reg-aarch-pauth", note); | |
9683 | } | |
9684 | ||
252b5132 | 9685 | #if defined (HAVE_PRPSINFO_T) |
4a938328 | 9686 | typedef prpsinfo_t elfcore_psinfo_t; |
7ee38065 | 9687 | #if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */ |
4a938328 MS |
9688 | typedef prpsinfo32_t elfcore_psinfo32_t; |
9689 | #endif | |
252b5132 RH |
9690 | #endif |
9691 | ||
9692 | #if defined (HAVE_PSINFO_T) | |
4a938328 | 9693 | typedef psinfo_t elfcore_psinfo_t; |
7ee38065 | 9694 | #if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */ |
4a938328 MS |
9695 | typedef psinfo32_t elfcore_psinfo32_t; |
9696 | #endif | |
252b5132 RH |
9697 | #endif |
9698 | ||
252b5132 RH |
9699 | /* return a malloc'ed copy of a string at START which is at |
9700 | most MAX bytes long, possibly without a terminating '\0'. | |
c044fabd | 9701 | the copy will always have a terminating '\0'. */ |
252b5132 | 9702 | |
936e320b | 9703 | char * |
217aa764 | 9704 | _bfd_elfcore_strndup (bfd *abfd, char *start, size_t max) |
252b5132 | 9705 | { |
dc810e39 | 9706 | char *dups; |
a50b1753 | 9707 | char *end = (char *) memchr (start, '\0', max); |
dc810e39 | 9708 | size_t len; |
252b5132 RH |
9709 | |
9710 | if (end == NULL) | |
9711 | len = max; | |
9712 | else | |
9713 | len = end - start; | |
9714 | ||
a50b1753 | 9715 | dups = (char *) bfd_alloc (abfd, len + 1); |
dc810e39 | 9716 | if (dups == NULL) |
252b5132 RH |
9717 | return NULL; |
9718 | ||
dc810e39 AM |
9719 | memcpy (dups, start, len); |
9720 | dups[len] = '\0'; | |
252b5132 | 9721 | |
dc810e39 | 9722 | return dups; |
252b5132 RH |
9723 | } |
9724 | ||
bb0082d6 | 9725 | #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) |
b34976b6 | 9726 | static bfd_boolean |
217aa764 | 9727 | elfcore_grok_psinfo (bfd *abfd, Elf_Internal_Note *note) |
252b5132 | 9728 | { |
4a938328 MS |
9729 | if (note->descsz == sizeof (elfcore_psinfo_t)) |
9730 | { | |
9731 | elfcore_psinfo_t psinfo; | |
252b5132 | 9732 | |
7ee38065 | 9733 | memcpy (&psinfo, note->descdata, sizeof (psinfo)); |
252b5132 | 9734 | |
335e41d4 | 9735 | #if defined (HAVE_PSINFO_T_PR_PID) || defined (HAVE_PRPSINFO_T_PR_PID) |
228e534f | 9736 | elf_tdata (abfd)->core->pid = psinfo.pr_pid; |
335e41d4 | 9737 | #endif |
228e534f | 9738 | elf_tdata (abfd)->core->program |
936e320b AM |
9739 | = _bfd_elfcore_strndup (abfd, psinfo.pr_fname, |
9740 | sizeof (psinfo.pr_fname)); | |
252b5132 | 9741 | |
228e534f | 9742 | elf_tdata (abfd)->core->command |
936e320b AM |
9743 | = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs, |
9744 | sizeof (psinfo.pr_psargs)); | |
4a938328 | 9745 | } |
7ee38065 | 9746 | #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T) |
4a938328 MS |
9747 | else if (note->descsz == sizeof (elfcore_psinfo32_t)) |
9748 | { | |
9749 | /* 64-bit host, 32-bit corefile */ | |
9750 | elfcore_psinfo32_t psinfo; | |
9751 | ||
7ee38065 | 9752 | memcpy (&psinfo, note->descdata, sizeof (psinfo)); |
252b5132 | 9753 | |
335e41d4 | 9754 | #if defined (HAVE_PSINFO32_T_PR_PID) || defined (HAVE_PRPSINFO32_T_PR_PID) |
228e534f | 9755 | elf_tdata (abfd)->core->pid = psinfo.pr_pid; |
335e41d4 | 9756 | #endif |
228e534f | 9757 | elf_tdata (abfd)->core->program |
936e320b AM |
9758 | = _bfd_elfcore_strndup (abfd, psinfo.pr_fname, |
9759 | sizeof (psinfo.pr_fname)); | |
4a938328 | 9760 | |
228e534f | 9761 | elf_tdata (abfd)->core->command |
936e320b AM |
9762 | = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs, |
9763 | sizeof (psinfo.pr_psargs)); | |
4a938328 MS |
9764 | } |
9765 | #endif | |
9766 | ||
9767 | else | |
9768 | { | |
9769 | /* Fail - we don't know how to handle any other | |
9770 | note size (ie. data object type). */ | |
b34976b6 | 9771 | return TRUE; |
4a938328 | 9772 | } |
252b5132 RH |
9773 | |
9774 | /* Note that for some reason, a spurious space is tacked | |
9775 | onto the end of the args in some (at least one anyway) | |
c044fabd | 9776 | implementations, so strip it off if it exists. */ |
252b5132 RH |
9777 | |
9778 | { | |
228e534f | 9779 | char *command = elf_tdata (abfd)->core->command; |
252b5132 RH |
9780 | int n = strlen (command); |
9781 | ||
9782 | if (0 < n && command[n - 1] == ' ') | |
9783 | command[n - 1] = '\0'; | |
9784 | } | |
9785 | ||
b34976b6 | 9786 | return TRUE; |
252b5132 RH |
9787 | } |
9788 | #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */ | |
9789 | ||
252b5132 | 9790 | #if defined (HAVE_PSTATUS_T) |
b34976b6 | 9791 | static bfd_boolean |
217aa764 | 9792 | elfcore_grok_pstatus (bfd *abfd, Elf_Internal_Note *note) |
252b5132 | 9793 | { |
f572a39d AM |
9794 | if (note->descsz == sizeof (pstatus_t) |
9795 | #if defined (HAVE_PXSTATUS_T) | |
9796 | || note->descsz == sizeof (pxstatus_t) | |
9797 | #endif | |
9798 | ) | |
4a938328 MS |
9799 | { |
9800 | pstatus_t pstat; | |
252b5132 | 9801 | |
4a938328 | 9802 | memcpy (&pstat, note->descdata, sizeof (pstat)); |
252b5132 | 9803 | |
228e534f | 9804 | elf_tdata (abfd)->core->pid = pstat.pr_pid; |
4a938328 | 9805 | } |
7ee38065 | 9806 | #if defined (HAVE_PSTATUS32_T) |
4a938328 MS |
9807 | else if (note->descsz == sizeof (pstatus32_t)) |
9808 | { | |
9809 | /* 64-bit host, 32-bit corefile */ | |
9810 | pstatus32_t pstat; | |
252b5132 | 9811 | |
4a938328 | 9812 | memcpy (&pstat, note->descdata, sizeof (pstat)); |
252b5132 | 9813 | |
228e534f | 9814 | elf_tdata (abfd)->core->pid = pstat.pr_pid; |
4a938328 MS |
9815 | } |
9816 | #endif | |
252b5132 RH |
9817 | /* Could grab some more details from the "representative" |
9818 | lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an | |
c044fabd | 9819 | NT_LWPSTATUS note, presumably. */ |
252b5132 | 9820 | |
b34976b6 | 9821 | return TRUE; |
252b5132 RH |
9822 | } |
9823 | #endif /* defined (HAVE_PSTATUS_T) */ | |
9824 | ||
252b5132 | 9825 | #if defined (HAVE_LWPSTATUS_T) |
b34976b6 | 9826 | static bfd_boolean |
217aa764 | 9827 | elfcore_grok_lwpstatus (bfd *abfd, Elf_Internal_Note *note) |
252b5132 RH |
9828 | { |
9829 | lwpstatus_t lwpstat; | |
9830 | char buf[100]; | |
c044fabd | 9831 | char *name; |
d4c88bbb | 9832 | size_t len; |
c044fabd | 9833 | asection *sect; |
252b5132 | 9834 | |
f572a39d AM |
9835 | if (note->descsz != sizeof (lwpstat) |
9836 | #if defined (HAVE_LWPXSTATUS_T) | |
9837 | && note->descsz != sizeof (lwpxstatus_t) | |
9838 | #endif | |
9839 | ) | |
b34976b6 | 9840 | return TRUE; |
252b5132 RH |
9841 | |
9842 | memcpy (&lwpstat, note->descdata, sizeof (lwpstat)); | |
9843 | ||
228e534f | 9844 | elf_tdata (abfd)->core->lwpid = lwpstat.pr_lwpid; |
a1504221 JB |
9845 | /* Do not overwrite the core signal if it has already been set by |
9846 | another thread. */ | |
228e534f AM |
9847 | if (elf_tdata (abfd)->core->signal == 0) |
9848 | elf_tdata (abfd)->core->signal = lwpstat.pr_cursig; | |
252b5132 | 9849 | |
c044fabd | 9850 | /* Make a ".reg/999" section. */ |
252b5132 RH |
9851 | |
9852 | sprintf (buf, ".reg/%d", elfcore_make_pid (abfd)); | |
d4c88bbb | 9853 | len = strlen (buf) + 1; |
217aa764 | 9854 | name = bfd_alloc (abfd, len); |
252b5132 | 9855 | if (name == NULL) |
b34976b6 | 9856 | return FALSE; |
d4c88bbb | 9857 | memcpy (name, buf, len); |
252b5132 | 9858 | |
117ed4f8 | 9859 | sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); |
252b5132 | 9860 | if (sect == NULL) |
b34976b6 | 9861 | return FALSE; |
252b5132 RH |
9862 | |
9863 | #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT) | |
eea6121a | 9864 | sect->size = sizeof (lwpstat.pr_context.uc_mcontext.gregs); |
252b5132 RH |
9865 | sect->filepos = note->descpos |
9866 | + offsetof (lwpstatus_t, pr_context.uc_mcontext.gregs); | |
9867 | #endif | |
9868 | ||
9869 | #if defined (HAVE_LWPSTATUS_T_PR_REG) | |
eea6121a | 9870 | sect->size = sizeof (lwpstat.pr_reg); |
252b5132 RH |
9871 | sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_reg); |
9872 | #endif | |
9873 | ||
252b5132 RH |
9874 | sect->alignment_power = 2; |
9875 | ||
9876 | if (!elfcore_maybe_make_sect (abfd, ".reg", sect)) | |
b34976b6 | 9877 | return FALSE; |
252b5132 RH |
9878 | |
9879 | /* Make a ".reg2/999" section */ | |
9880 | ||
9881 | sprintf (buf, ".reg2/%d", elfcore_make_pid (abfd)); | |
d4c88bbb | 9882 | len = strlen (buf) + 1; |
217aa764 | 9883 | name = bfd_alloc (abfd, len); |
252b5132 | 9884 | if (name == NULL) |
b34976b6 | 9885 | return FALSE; |
d4c88bbb | 9886 | memcpy (name, buf, len); |
252b5132 | 9887 | |
117ed4f8 | 9888 | sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); |
252b5132 | 9889 | if (sect == NULL) |
b34976b6 | 9890 | return FALSE; |
252b5132 RH |
9891 | |
9892 | #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT) | |
eea6121a | 9893 | sect->size = sizeof (lwpstat.pr_context.uc_mcontext.fpregs); |
252b5132 RH |
9894 | sect->filepos = note->descpos |
9895 | + offsetof (lwpstatus_t, pr_context.uc_mcontext.fpregs); | |
9896 | #endif | |
9897 | ||
9898 | #if defined (HAVE_LWPSTATUS_T_PR_FPREG) | |
eea6121a | 9899 | sect->size = sizeof (lwpstat.pr_fpreg); |
252b5132 RH |
9900 | sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_fpreg); |
9901 | #endif | |
9902 | ||
252b5132 RH |
9903 | sect->alignment_power = 2; |
9904 | ||
936e320b | 9905 | return elfcore_maybe_make_sect (abfd, ".reg2", sect); |
252b5132 RH |
9906 | } |
9907 | #endif /* defined (HAVE_LWPSTATUS_T) */ | |
9908 | ||
b34976b6 | 9909 | static bfd_boolean |
217aa764 | 9910 | elfcore_grok_win32pstatus (bfd *abfd, Elf_Internal_Note *note) |
16e9c715 NC |
9911 | { |
9912 | char buf[30]; | |
c044fabd | 9913 | char *name; |
d4c88bbb | 9914 | size_t len; |
c044fabd | 9915 | asection *sect; |
4a6636fb PA |
9916 | int type; |
9917 | int is_active_thread; | |
9918 | bfd_vma base_addr; | |
16e9c715 | 9919 | |
4a6636fb | 9920 | if (note->descsz < 728) |
b34976b6 | 9921 | return TRUE; |
16e9c715 | 9922 | |
4a6636fb PA |
9923 | if (! CONST_STRNEQ (note->namedata, "win32")) |
9924 | return TRUE; | |
9925 | ||
9926 | type = bfd_get_32 (abfd, note->descdata); | |
c044fabd | 9927 | |
4a6636fb | 9928 | switch (type) |
16e9c715 | 9929 | { |
4a6636fb | 9930 | case 1 /* NOTE_INFO_PROCESS */: |
228e534f | 9931 | /* FIXME: need to add ->core->command. */ |
4a6636fb | 9932 | /* process_info.pid */ |
228e534f | 9933 | elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, note->descdata + 8); |
4a6636fb | 9934 | /* process_info.signal */ |
228e534f | 9935 | elf_tdata (abfd)->core->signal = bfd_get_32 (abfd, note->descdata + 12); |
c044fabd | 9936 | break; |
16e9c715 | 9937 | |
4a6636fb | 9938 | case 2 /* NOTE_INFO_THREAD */: |
16e9c715 | 9939 | /* Make a ".reg/999" section. */ |
4a6636fb PA |
9940 | /* thread_info.tid */ |
9941 | sprintf (buf, ".reg/%ld", (long) bfd_get_32 (abfd, note->descdata + 8)); | |
c044fabd | 9942 | |
d4c88bbb | 9943 | len = strlen (buf) + 1; |
a50b1753 | 9944 | name = (char *) bfd_alloc (abfd, len); |
16e9c715 | 9945 | if (name == NULL) |
b34976b6 | 9946 | return FALSE; |
c044fabd | 9947 | |
d4c88bbb | 9948 | memcpy (name, buf, len); |
16e9c715 | 9949 | |
117ed4f8 | 9950 | sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); |
16e9c715 | 9951 | if (sect == NULL) |
b34976b6 | 9952 | return FALSE; |
c044fabd | 9953 | |
4a6636fb PA |
9954 | /* sizeof (thread_info.thread_context) */ |
9955 | sect->size = 716; | |
9956 | /* offsetof (thread_info.thread_context) */ | |
9957 | sect->filepos = note->descpos + 12; | |
16e9c715 NC |
9958 | sect->alignment_power = 2; |
9959 | ||
4a6636fb PA |
9960 | /* thread_info.is_active_thread */ |
9961 | is_active_thread = bfd_get_32 (abfd, note->descdata + 8); | |
9962 | ||
9963 | if (is_active_thread) | |
16e9c715 | 9964 | if (! elfcore_maybe_make_sect (abfd, ".reg", sect)) |
b34976b6 | 9965 | return FALSE; |
16e9c715 NC |
9966 | break; |
9967 | ||
4a6636fb | 9968 | case 3 /* NOTE_INFO_MODULE */: |
16e9c715 | 9969 | /* Make a ".module/xxxxxxxx" section. */ |
4a6636fb PA |
9970 | /* module_info.base_address */ |
9971 | base_addr = bfd_get_32 (abfd, note->descdata + 4); | |
0af1713e | 9972 | sprintf (buf, ".module/%08lx", (unsigned long) base_addr); |
c044fabd | 9973 | |
d4c88bbb | 9974 | len = strlen (buf) + 1; |
a50b1753 | 9975 | name = (char *) bfd_alloc (abfd, len); |
16e9c715 | 9976 | if (name == NULL) |
b34976b6 | 9977 | return FALSE; |
c044fabd | 9978 | |
d4c88bbb | 9979 | memcpy (name, buf, len); |
252b5132 | 9980 | |
117ed4f8 | 9981 | sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); |
c044fabd | 9982 | |
16e9c715 | 9983 | if (sect == NULL) |
b34976b6 | 9984 | return FALSE; |
c044fabd | 9985 | |
eea6121a | 9986 | sect->size = note->descsz; |
16e9c715 | 9987 | sect->filepos = note->descpos; |
16e9c715 NC |
9988 | sect->alignment_power = 2; |
9989 | break; | |
9990 | ||
9991 | default: | |
b34976b6 | 9992 | return TRUE; |
16e9c715 NC |
9993 | } |
9994 | ||
b34976b6 | 9995 | return TRUE; |
16e9c715 | 9996 | } |
252b5132 | 9997 | |
b34976b6 | 9998 | static bfd_boolean |
217aa764 | 9999 | elfcore_grok_note (bfd *abfd, Elf_Internal_Note *note) |
252b5132 | 10000 | { |
9c5bfbb7 | 10001 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
bb0082d6 | 10002 | |
252b5132 RH |
10003 | switch (note->type) |
10004 | { | |
10005 | default: | |
b34976b6 | 10006 | return TRUE; |
252b5132 | 10007 | |
252b5132 | 10008 | case NT_PRSTATUS: |
bb0082d6 AM |
10009 | if (bed->elf_backend_grok_prstatus) |
10010 | if ((*bed->elf_backend_grok_prstatus) (abfd, note)) | |
b34976b6 | 10011 | return TRUE; |
bb0082d6 | 10012 | #if defined (HAVE_PRSTATUS_T) |
252b5132 | 10013 | return elfcore_grok_prstatus (abfd, note); |
bb0082d6 | 10014 | #else |
b34976b6 | 10015 | return TRUE; |
252b5132 RH |
10016 | #endif |
10017 | ||
10018 | #if defined (HAVE_PSTATUS_T) | |
10019 | case NT_PSTATUS: | |
10020 | return elfcore_grok_pstatus (abfd, note); | |
10021 | #endif | |
10022 | ||
10023 | #if defined (HAVE_LWPSTATUS_T) | |
10024 | case NT_LWPSTATUS: | |
10025 | return elfcore_grok_lwpstatus (abfd, note); | |
10026 | #endif | |
10027 | ||
10028 | case NT_FPREGSET: /* FIXME: rename to NT_PRFPREG */ | |
10029 | return elfcore_grok_prfpreg (abfd, note); | |
10030 | ||
c044fabd | 10031 | case NT_WIN32PSTATUS: |
16e9c715 | 10032 | return elfcore_grok_win32pstatus (abfd, note); |
16e9c715 | 10033 | |
c044fabd | 10034 | case NT_PRXFPREG: /* Linux SSE extension */ |
e377ab71 MK |
10035 | if (note->namesz == 6 |
10036 | && strcmp (note->namedata, "LINUX") == 0) | |
ff08c6bb JB |
10037 | return elfcore_grok_prxfpreg (abfd, note); |
10038 | else | |
b34976b6 | 10039 | return TRUE; |
ff08c6bb | 10040 | |
4339cae0 L |
10041 | case NT_X86_XSTATE: /* Linux XSAVE extension */ |
10042 | if (note->namesz == 6 | |
10043 | && strcmp (note->namedata, "LINUX") == 0) | |
10044 | return elfcore_grok_xstatereg (abfd, note); | |
10045 | else | |
10046 | return TRUE; | |
10047 | ||
97753bd5 AM |
10048 | case NT_PPC_VMX: |
10049 | if (note->namesz == 6 | |
10050 | && strcmp (note->namedata, "LINUX") == 0) | |
10051 | return elfcore_grok_ppc_vmx (abfd, note); | |
10052 | else | |
10053 | return TRUE; | |
10054 | ||
89eeb0bc LM |
10055 | case NT_PPC_VSX: |
10056 | if (note->namesz == 6 | |
07d6d2b8 AM |
10057 | && strcmp (note->namedata, "LINUX") == 0) |
10058 | return elfcore_grok_ppc_vsx (abfd, note); | |
89eeb0bc | 10059 | else |
07d6d2b8 | 10060 | return TRUE; |
89eeb0bc | 10061 | |
cb2366c1 EBM |
10062 | case NT_PPC_TAR: |
10063 | if (note->namesz == 6 | |
4b24dd1a AM |
10064 | && strcmp (note->namedata, "LINUX") == 0) |
10065 | return elfcore_grok_ppc_tar (abfd, note); | |
cb2366c1 | 10066 | else |
4b24dd1a | 10067 | return TRUE; |
cb2366c1 EBM |
10068 | |
10069 | case NT_PPC_PPR: | |
10070 | if (note->namesz == 6 | |
4b24dd1a AM |
10071 | && strcmp (note->namedata, "LINUX") == 0) |
10072 | return elfcore_grok_ppc_ppr (abfd, note); | |
cb2366c1 | 10073 | else |
4b24dd1a | 10074 | return TRUE; |
cb2366c1 EBM |
10075 | |
10076 | case NT_PPC_DSCR: | |
10077 | if (note->namesz == 6 | |
4b24dd1a AM |
10078 | && strcmp (note->namedata, "LINUX") == 0) |
10079 | return elfcore_grok_ppc_dscr (abfd, note); | |
cb2366c1 | 10080 | else |
4b24dd1a | 10081 | return TRUE; |
cb2366c1 EBM |
10082 | |
10083 | case NT_PPC_EBB: | |
10084 | if (note->namesz == 6 | |
4b24dd1a AM |
10085 | && strcmp (note->namedata, "LINUX") == 0) |
10086 | return elfcore_grok_ppc_ebb (abfd, note); | |
cb2366c1 | 10087 | else |
4b24dd1a | 10088 | return TRUE; |
cb2366c1 EBM |
10089 | |
10090 | case NT_PPC_PMU: | |
10091 | if (note->namesz == 6 | |
4b24dd1a AM |
10092 | && strcmp (note->namedata, "LINUX") == 0) |
10093 | return elfcore_grok_ppc_pmu (abfd, note); | |
cb2366c1 | 10094 | else |
4b24dd1a | 10095 | return TRUE; |
cb2366c1 EBM |
10096 | |
10097 | case NT_PPC_TM_CGPR: | |
10098 | if (note->namesz == 6 | |
4b24dd1a AM |
10099 | && strcmp (note->namedata, "LINUX") == 0) |
10100 | return elfcore_grok_ppc_tm_cgpr (abfd, note); | |
cb2366c1 | 10101 | else |
4b24dd1a | 10102 | return TRUE; |
cb2366c1 EBM |
10103 | |
10104 | case NT_PPC_TM_CFPR: | |
10105 | if (note->namesz == 6 | |
4b24dd1a AM |
10106 | && strcmp (note->namedata, "LINUX") == 0) |
10107 | return elfcore_grok_ppc_tm_cfpr (abfd, note); | |
cb2366c1 | 10108 | else |
4b24dd1a | 10109 | return TRUE; |
cb2366c1 EBM |
10110 | |
10111 | case NT_PPC_TM_CVMX: | |
10112 | if (note->namesz == 6 | |
4b24dd1a AM |
10113 | && strcmp (note->namedata, "LINUX") == 0) |
10114 | return elfcore_grok_ppc_tm_cvmx (abfd, note); | |
cb2366c1 | 10115 | else |
4b24dd1a | 10116 | return TRUE; |
cb2366c1 EBM |
10117 | |
10118 | case NT_PPC_TM_CVSX: | |
10119 | if (note->namesz == 6 | |
4b24dd1a AM |
10120 | && strcmp (note->namedata, "LINUX") == 0) |
10121 | return elfcore_grok_ppc_tm_cvsx (abfd, note); | |
cb2366c1 | 10122 | else |
4b24dd1a | 10123 | return TRUE; |
cb2366c1 EBM |
10124 | |
10125 | case NT_PPC_TM_SPR: | |
10126 | if (note->namesz == 6 | |
4b24dd1a AM |
10127 | && strcmp (note->namedata, "LINUX") == 0) |
10128 | return elfcore_grok_ppc_tm_spr (abfd, note); | |
cb2366c1 | 10129 | else |
4b24dd1a | 10130 | return TRUE; |
cb2366c1 EBM |
10131 | |
10132 | case NT_PPC_TM_CTAR: | |
10133 | if (note->namesz == 6 | |
4b24dd1a AM |
10134 | && strcmp (note->namedata, "LINUX") == 0) |
10135 | return elfcore_grok_ppc_tm_ctar (abfd, note); | |
cb2366c1 | 10136 | else |
4b24dd1a | 10137 | return TRUE; |
cb2366c1 EBM |
10138 | |
10139 | case NT_PPC_TM_CPPR: | |
10140 | if (note->namesz == 6 | |
4b24dd1a AM |
10141 | && strcmp (note->namedata, "LINUX") == 0) |
10142 | return elfcore_grok_ppc_tm_cppr (abfd, note); | |
cb2366c1 | 10143 | else |
4b24dd1a | 10144 | return TRUE; |
cb2366c1 EBM |
10145 | |
10146 | case NT_PPC_TM_CDSCR: | |
10147 | if (note->namesz == 6 | |
4b24dd1a AM |
10148 | && strcmp (note->namedata, "LINUX") == 0) |
10149 | return elfcore_grok_ppc_tm_cdscr (abfd, note); | |
cb2366c1 | 10150 | else |
4b24dd1a | 10151 | return TRUE; |
cb2366c1 | 10152 | |
0675e188 UW |
10153 | case NT_S390_HIGH_GPRS: |
10154 | if (note->namesz == 6 | |
07d6d2b8 AM |
10155 | && strcmp (note->namedata, "LINUX") == 0) |
10156 | return elfcore_grok_s390_high_gprs (abfd, note); | |
0675e188 | 10157 | else |
07d6d2b8 | 10158 | return TRUE; |
0675e188 | 10159 | |
d7eeb400 MS |
10160 | case NT_S390_TIMER: |
10161 | if (note->namesz == 6 | |
07d6d2b8 AM |
10162 | && strcmp (note->namedata, "LINUX") == 0) |
10163 | return elfcore_grok_s390_timer (abfd, note); | |
d7eeb400 | 10164 | else |
07d6d2b8 | 10165 | return TRUE; |
d7eeb400 MS |
10166 | |
10167 | case NT_S390_TODCMP: | |
10168 | if (note->namesz == 6 | |
07d6d2b8 AM |
10169 | && strcmp (note->namedata, "LINUX") == 0) |
10170 | return elfcore_grok_s390_todcmp (abfd, note); | |
d7eeb400 | 10171 | else |
07d6d2b8 | 10172 | return TRUE; |
d7eeb400 MS |
10173 | |
10174 | case NT_S390_TODPREG: | |
10175 | if (note->namesz == 6 | |
07d6d2b8 AM |
10176 | && strcmp (note->namedata, "LINUX") == 0) |
10177 | return elfcore_grok_s390_todpreg (abfd, note); | |
d7eeb400 | 10178 | else |
07d6d2b8 | 10179 | return TRUE; |
d7eeb400 MS |
10180 | |
10181 | case NT_S390_CTRS: | |
10182 | if (note->namesz == 6 | |
07d6d2b8 AM |
10183 | && strcmp (note->namedata, "LINUX") == 0) |
10184 | return elfcore_grok_s390_ctrs (abfd, note); | |
d7eeb400 | 10185 | else |
07d6d2b8 | 10186 | return TRUE; |
d7eeb400 MS |
10187 | |
10188 | case NT_S390_PREFIX: | |
10189 | if (note->namesz == 6 | |
07d6d2b8 AM |
10190 | && strcmp (note->namedata, "LINUX") == 0) |
10191 | return elfcore_grok_s390_prefix (abfd, note); | |
d7eeb400 | 10192 | else |
07d6d2b8 | 10193 | return TRUE; |
d7eeb400 | 10194 | |
355b81d9 UW |
10195 | case NT_S390_LAST_BREAK: |
10196 | if (note->namesz == 6 | |
07d6d2b8 AM |
10197 | && strcmp (note->namedata, "LINUX") == 0) |
10198 | return elfcore_grok_s390_last_break (abfd, note); | |
355b81d9 | 10199 | else |
07d6d2b8 | 10200 | return TRUE; |
355b81d9 UW |
10201 | |
10202 | case NT_S390_SYSTEM_CALL: | |
10203 | if (note->namesz == 6 | |
07d6d2b8 AM |
10204 | && strcmp (note->namedata, "LINUX") == 0) |
10205 | return elfcore_grok_s390_system_call (abfd, note); | |
355b81d9 | 10206 | else |
07d6d2b8 | 10207 | return TRUE; |
355b81d9 | 10208 | |
abb3f6cc NC |
10209 | case NT_S390_TDB: |
10210 | if (note->namesz == 6 | |
07d6d2b8 AM |
10211 | && strcmp (note->namedata, "LINUX") == 0) |
10212 | return elfcore_grok_s390_tdb (abfd, note); | |
abb3f6cc | 10213 | else |
07d6d2b8 | 10214 | return TRUE; |
abb3f6cc | 10215 | |
4ef9f41a AA |
10216 | case NT_S390_VXRS_LOW: |
10217 | if (note->namesz == 6 | |
10218 | && strcmp (note->namedata, "LINUX") == 0) | |
10219 | return elfcore_grok_s390_vxrs_low (abfd, note); | |
10220 | else | |
10221 | return TRUE; | |
10222 | ||
10223 | case NT_S390_VXRS_HIGH: | |
10224 | if (note->namesz == 6 | |
10225 | && strcmp (note->namedata, "LINUX") == 0) | |
10226 | return elfcore_grok_s390_vxrs_high (abfd, note); | |
10227 | else | |
10228 | return TRUE; | |
10229 | ||
88ab90e8 AA |
10230 | case NT_S390_GS_CB: |
10231 | if (note->namesz == 6 | |
10232 | && strcmp (note->namedata, "LINUX") == 0) | |
8fe09d74 | 10233 | return elfcore_grok_s390_gs_cb (abfd, note); |
88ab90e8 AA |
10234 | else |
10235 | return TRUE; | |
10236 | ||
10237 | case NT_S390_GS_BC: | |
10238 | if (note->namesz == 6 | |
10239 | && strcmp (note->namedata, "LINUX") == 0) | |
8fe09d74 | 10240 | return elfcore_grok_s390_gs_bc (abfd, note); |
88ab90e8 AA |
10241 | else |
10242 | return TRUE; | |
10243 | ||
faa9a424 UW |
10244 | case NT_ARM_VFP: |
10245 | if (note->namesz == 6 | |
10246 | && strcmp (note->namedata, "LINUX") == 0) | |
10247 | return elfcore_grok_arm_vfp (abfd, note); | |
10248 | else | |
10249 | return TRUE; | |
10250 | ||
652451f8 YZ |
10251 | case NT_ARM_TLS: |
10252 | if (note->namesz == 6 | |
10253 | && strcmp (note->namedata, "LINUX") == 0) | |
10254 | return elfcore_grok_aarch_tls (abfd, note); | |
10255 | else | |
10256 | return TRUE; | |
10257 | ||
10258 | case NT_ARM_HW_BREAK: | |
10259 | if (note->namesz == 6 | |
10260 | && strcmp (note->namedata, "LINUX") == 0) | |
10261 | return elfcore_grok_aarch_hw_break (abfd, note); | |
10262 | else | |
10263 | return TRUE; | |
10264 | ||
10265 | case NT_ARM_HW_WATCH: | |
10266 | if (note->namesz == 6 | |
10267 | && strcmp (note->namedata, "LINUX") == 0) | |
10268 | return elfcore_grok_aarch_hw_watch (abfd, note); | |
10269 | else | |
10270 | return TRUE; | |
10271 | ||
ad1cc4e4 AH |
10272 | case NT_ARM_SVE: |
10273 | if (note->namesz == 6 | |
10274 | && strcmp (note->namedata, "LINUX") == 0) | |
10275 | return elfcore_grok_aarch_sve (abfd, note); | |
10276 | else | |
10277 | return TRUE; | |
10278 | ||
e6c3b5bf AH |
10279 | case NT_ARM_PAC_MASK: |
10280 | if (note->namesz == 6 | |
10281 | && strcmp (note->namedata, "LINUX") == 0) | |
10282 | return elfcore_grok_aarch_pauth (abfd, note); | |
10283 | else | |
10284 | return TRUE; | |
10285 | ||
252b5132 RH |
10286 | case NT_PRPSINFO: |
10287 | case NT_PSINFO: | |
bb0082d6 AM |
10288 | if (bed->elf_backend_grok_psinfo) |
10289 | if ((*bed->elf_backend_grok_psinfo) (abfd, note)) | |
b34976b6 | 10290 | return TRUE; |
bb0082d6 | 10291 | #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) |
252b5132 | 10292 | return elfcore_grok_psinfo (abfd, note); |
bb0082d6 | 10293 | #else |
b34976b6 | 10294 | return TRUE; |
252b5132 | 10295 | #endif |
3333a7c3 RM |
10296 | |
10297 | case NT_AUXV: | |
58e07198 | 10298 | return elfcore_make_auxv_note_section (abfd, note, 0); |
9015683b | 10299 | |
451b7c33 TT |
10300 | case NT_FILE: |
10301 | return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.file", | |
10302 | note); | |
10303 | ||
9015683b TT |
10304 | case NT_SIGINFO: |
10305 | return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.siginfo", | |
10306 | note); | |
5b2c414d | 10307 | |
252b5132 RH |
10308 | } |
10309 | } | |
10310 | ||
718175fa JK |
10311 | static bfd_boolean |
10312 | elfobj_grok_gnu_build_id (bfd *abfd, Elf_Internal_Note *note) | |
10313 | { | |
c74f7d1c | 10314 | struct bfd_build_id* build_id; |
30e8ee25 AM |
10315 | |
10316 | if (note->descsz == 0) | |
10317 | return FALSE; | |
10318 | ||
c74f7d1c JT |
10319 | build_id = bfd_alloc (abfd, sizeof (struct bfd_build_id) - 1 + note->descsz); |
10320 | if (build_id == NULL) | |
718175fa JK |
10321 | return FALSE; |
10322 | ||
c74f7d1c JT |
10323 | build_id->size = note->descsz; |
10324 | memcpy (build_id->data, note->descdata, note->descsz); | |
10325 | abfd->build_id = build_id; | |
718175fa JK |
10326 | |
10327 | return TRUE; | |
10328 | } | |
10329 | ||
10330 | static bfd_boolean | |
10331 | elfobj_grok_gnu_note (bfd *abfd, Elf_Internal_Note *note) | |
10332 | { | |
10333 | switch (note->type) | |
10334 | { | |
10335 | default: | |
10336 | return TRUE; | |
10337 | ||
46bed679 L |
10338 | case NT_GNU_PROPERTY_TYPE_0: |
10339 | return _bfd_elf_parse_gnu_properties (abfd, note); | |
10340 | ||
718175fa JK |
10341 | case NT_GNU_BUILD_ID: |
10342 | return elfobj_grok_gnu_build_id (abfd, note); | |
10343 | } | |
10344 | } | |
10345 | ||
e21e5835 NC |
10346 | static bfd_boolean |
10347 | elfobj_grok_stapsdt_note_1 (bfd *abfd, Elf_Internal_Note *note) | |
10348 | { | |
10349 | struct sdt_note *cur = | |
7a6e0d89 AM |
10350 | (struct sdt_note *) bfd_alloc (abfd, |
10351 | sizeof (struct sdt_note) + note->descsz); | |
e21e5835 NC |
10352 | |
10353 | cur->next = (struct sdt_note *) (elf_tdata (abfd))->sdt_note_head; | |
10354 | cur->size = (bfd_size_type) note->descsz; | |
10355 | memcpy (cur->data, note->descdata, note->descsz); | |
10356 | ||
10357 | elf_tdata (abfd)->sdt_note_head = cur; | |
10358 | ||
10359 | return TRUE; | |
10360 | } | |
10361 | ||
10362 | static bfd_boolean | |
10363 | elfobj_grok_stapsdt_note (bfd *abfd, Elf_Internal_Note *note) | |
10364 | { | |
10365 | switch (note->type) | |
10366 | { | |
10367 | case NT_STAPSDT: | |
10368 | return elfobj_grok_stapsdt_note_1 (abfd, note); | |
10369 | ||
10370 | default: | |
10371 | return TRUE; | |
10372 | } | |
10373 | } | |
10374 | ||
aa1ed4a9 JB |
10375 | static bfd_boolean |
10376 | elfcore_grok_freebsd_psinfo (bfd *abfd, Elf_Internal_Note *note) | |
10377 | { | |
10378 | size_t offset; | |
10379 | ||
b5430a3c | 10380 | switch (elf_elfheader (abfd)->e_ident[EI_CLASS]) |
aa1ed4a9 | 10381 | { |
b5430a3c | 10382 | case ELFCLASS32: |
0064d223 JB |
10383 | if (note->descsz < 108) |
10384 | return FALSE; | |
aa1ed4a9 JB |
10385 | break; |
10386 | ||
b5430a3c | 10387 | case ELFCLASS64: |
0064d223 JB |
10388 | if (note->descsz < 120) |
10389 | return FALSE; | |
aa1ed4a9 JB |
10390 | break; |
10391 | ||
10392 | default: | |
10393 | return FALSE; | |
10394 | } | |
10395 | ||
0064d223 JB |
10396 | /* Check for version 1 in pr_version. */ |
10397 | if (bfd_h_get_32 (abfd, (bfd_byte *) note->descdata) != 1) | |
10398 | return FALSE; | |
80a04378 | 10399 | |
0064d223 JB |
10400 | offset = 4; |
10401 | ||
10402 | /* Skip over pr_psinfosz. */ | |
b5430a3c | 10403 | if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS32) |
0064d223 JB |
10404 | offset += 4; |
10405 | else | |
10406 | { | |
10407 | offset += 4; /* Padding before pr_psinfosz. */ | |
10408 | offset += 8; | |
10409 | } | |
10410 | ||
aa1ed4a9 JB |
10411 | /* pr_fname is PRFNAMESZ (16) + 1 bytes in size. */ |
10412 | elf_tdata (abfd)->core->program | |
10413 | = _bfd_elfcore_strndup (abfd, note->descdata + offset, 17); | |
10414 | offset += 17; | |
10415 | ||
10416 | /* pr_psargs is PRARGSZ (80) + 1 bytes in size. */ | |
10417 | elf_tdata (abfd)->core->command | |
10418 | = _bfd_elfcore_strndup (abfd, note->descdata + offset, 81); | |
0064d223 JB |
10419 | offset += 81; |
10420 | ||
10421 | /* Padding before pr_pid. */ | |
10422 | offset += 2; | |
10423 | ||
10424 | /* The pr_pid field was added in version "1a". */ | |
10425 | if (note->descsz < offset + 4) | |
10426 | return TRUE; | |
10427 | ||
10428 | elf_tdata (abfd)->core->pid | |
10429 | = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset); | |
aa1ed4a9 JB |
10430 | |
10431 | return TRUE; | |
10432 | } | |
10433 | ||
10434 | static bfd_boolean | |
10435 | elfcore_grok_freebsd_prstatus (bfd *abfd, Elf_Internal_Note *note) | |
10436 | { | |
10437 | size_t offset; | |
10438 | size_t size; | |
24d3e51b | 10439 | size_t min_size; |
aa1ed4a9 | 10440 | |
24d3e51b NC |
10441 | /* Compute offset of pr_getregsz, skipping over pr_statussz. |
10442 | Also compute minimum size of this note. */ | |
b5430a3c | 10443 | switch (elf_elfheader (abfd)->e_ident[EI_CLASS]) |
aa1ed4a9 | 10444 | { |
b5430a3c | 10445 | case ELFCLASS32: |
24d3e51b NC |
10446 | offset = 4 + 4; |
10447 | min_size = offset + (4 * 2) + 4 + 4 + 4; | |
aa1ed4a9 JB |
10448 | break; |
10449 | ||
b5430a3c | 10450 | case ELFCLASS64: |
24d3e51b NC |
10451 | offset = 4 + 4 + 8; /* Includes padding before pr_statussz. */ |
10452 | min_size = offset + (8 * 2) + 4 + 4 + 4 + 4; | |
aa1ed4a9 JB |
10453 | break; |
10454 | ||
10455 | default: | |
10456 | return FALSE; | |
10457 | } | |
10458 | ||
24d3e51b NC |
10459 | if (note->descsz < min_size) |
10460 | return FALSE; | |
10461 | ||
10462 | /* Check for version 1 in pr_version. */ | |
10463 | if (bfd_h_get_32 (abfd, (bfd_byte *) note->descdata) != 1) | |
10464 | return FALSE; | |
aa1ed4a9 | 10465 | |
24d3e51b NC |
10466 | /* Extract size of pr_reg from pr_gregsetsz. */ |
10467 | /* Skip over pr_gregsetsz and pr_fpregsetsz. */ | |
b5430a3c | 10468 | if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS32) |
24d3e51b NC |
10469 | { |
10470 | size = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset); | |
10471 | offset += 4 * 2; | |
10472 | } | |
b5430a3c | 10473 | else |
24d3e51b NC |
10474 | { |
10475 | size = bfd_h_get_64 (abfd, (bfd_byte *) note->descdata + offset); | |
10476 | offset += 8 * 2; | |
10477 | } | |
aa1ed4a9 | 10478 | |
24d3e51b | 10479 | /* Skip over pr_osreldate. */ |
aa1ed4a9 JB |
10480 | offset += 4; |
10481 | ||
24d3e51b | 10482 | /* Read signal from pr_cursig. */ |
aa1ed4a9 JB |
10483 | if (elf_tdata (abfd)->core->signal == 0) |
10484 | elf_tdata (abfd)->core->signal | |
10485 | = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset); | |
10486 | offset += 4; | |
10487 | ||
24d3e51b | 10488 | /* Read TID from pr_pid. */ |
aa1ed4a9 JB |
10489 | elf_tdata (abfd)->core->lwpid |
10490 | = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset); | |
10491 | offset += 4; | |
10492 | ||
24d3e51b | 10493 | /* Padding before pr_reg. */ |
b5430a3c | 10494 | if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS64) |
aa1ed4a9 JB |
10495 | offset += 4; |
10496 | ||
24d3e51b NC |
10497 | /* Make sure that there is enough data remaining in the note. */ |
10498 | if ((note->descsz - offset) < size) | |
10499 | return FALSE; | |
10500 | ||
aa1ed4a9 JB |
10501 | /* Make a ".reg/999" section and a ".reg" section. */ |
10502 | return _bfd_elfcore_make_pseudosection (abfd, ".reg", | |
10503 | size, note->descpos + offset); | |
10504 | } | |
10505 | ||
10506 | static bfd_boolean | |
10507 | elfcore_grok_freebsd_note (bfd *abfd, Elf_Internal_Note *note) | |
10508 | { | |
544c67cd JB |
10509 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
10510 | ||
aa1ed4a9 JB |
10511 | switch (note->type) |
10512 | { | |
10513 | case NT_PRSTATUS: | |
544c67cd JB |
10514 | if (bed->elf_backend_grok_freebsd_prstatus) |
10515 | if ((*bed->elf_backend_grok_freebsd_prstatus) (abfd, note)) | |
10516 | return TRUE; | |
aa1ed4a9 JB |
10517 | return elfcore_grok_freebsd_prstatus (abfd, note); |
10518 | ||
10519 | case NT_FPREGSET: | |
10520 | return elfcore_grok_prfpreg (abfd, note); | |
10521 | ||
10522 | case NT_PRPSINFO: | |
10523 | return elfcore_grok_freebsd_psinfo (abfd, note); | |
10524 | ||
10525 | case NT_FREEBSD_THRMISC: | |
10526 | if (note->namesz == 8) | |
10527 | return elfcore_make_note_pseudosection (abfd, ".thrmisc", note); | |
10528 | else | |
10529 | return TRUE; | |
10530 | ||
ddb2bbcf JB |
10531 | case NT_FREEBSD_PROCSTAT_PROC: |
10532 | return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.proc", | |
10533 | note); | |
10534 | ||
10535 | case NT_FREEBSD_PROCSTAT_FILES: | |
10536 | return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.files", | |
10537 | note); | |
10538 | ||
10539 | case NT_FREEBSD_PROCSTAT_VMMAP: | |
10540 | return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.vmmap", | |
10541 | note); | |
10542 | ||
3350c5f5 | 10543 | case NT_FREEBSD_PROCSTAT_AUXV: |
58e07198 | 10544 | return elfcore_make_auxv_note_section (abfd, note, 4); |
3350c5f5 | 10545 | |
aa1ed4a9 JB |
10546 | case NT_X86_XSTATE: |
10547 | if (note->namesz == 8) | |
10548 | return elfcore_grok_xstatereg (abfd, note); | |
10549 | else | |
10550 | return TRUE; | |
10551 | ||
e6f3b9c3 JB |
10552 | case NT_FREEBSD_PTLWPINFO: |
10553 | return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.lwpinfo", | |
10554 | note); | |
10555 | ||
6d5be5d6 JB |
10556 | case NT_ARM_VFP: |
10557 | return elfcore_grok_arm_vfp (abfd, note); | |
10558 | ||
aa1ed4a9 JB |
10559 | default: |
10560 | return TRUE; | |
10561 | } | |
10562 | } | |
10563 | ||
b34976b6 | 10564 | static bfd_boolean |
217aa764 | 10565 | elfcore_netbsd_get_lwpid (Elf_Internal_Note *note, int *lwpidp) |
50b2bdb7 AM |
10566 | { |
10567 | char *cp; | |
10568 | ||
10569 | cp = strchr (note->namedata, '@'); | |
10570 | if (cp != NULL) | |
10571 | { | |
d2b64500 | 10572 | *lwpidp = atoi(cp + 1); |
b34976b6 | 10573 | return TRUE; |
50b2bdb7 | 10574 | } |
b34976b6 | 10575 | return FALSE; |
50b2bdb7 AM |
10576 | } |
10577 | ||
b34976b6 | 10578 | static bfd_boolean |
217aa764 | 10579 | elfcore_grok_netbsd_procinfo (bfd *abfd, Elf_Internal_Note *note) |
50b2bdb7 | 10580 | { |
80a04378 NC |
10581 | if (note->descsz <= 0x7c + 31) |
10582 | return FALSE; | |
10583 | ||
50b2bdb7 | 10584 | /* Signal number at offset 0x08. */ |
228e534f | 10585 | elf_tdata (abfd)->core->signal |
50b2bdb7 AM |
10586 | = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08); |
10587 | ||
10588 | /* Process ID at offset 0x50. */ | |
228e534f | 10589 | elf_tdata (abfd)->core->pid |
50b2bdb7 AM |
10590 | = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x50); |
10591 | ||
10592 | /* Command name at 0x7c (max 32 bytes, including nul). */ | |
228e534f | 10593 | elf_tdata (abfd)->core->command |
50b2bdb7 AM |
10594 | = _bfd_elfcore_strndup (abfd, note->descdata + 0x7c, 31); |
10595 | ||
7720ba9f MK |
10596 | return elfcore_make_note_pseudosection (abfd, ".note.netbsdcore.procinfo", |
10597 | note); | |
50b2bdb7 AM |
10598 | } |
10599 | ||
b34976b6 | 10600 | static bfd_boolean |
217aa764 | 10601 | elfcore_grok_netbsd_note (bfd *abfd, Elf_Internal_Note *note) |
50b2bdb7 AM |
10602 | { |
10603 | int lwp; | |
10604 | ||
10605 | if (elfcore_netbsd_get_lwpid (note, &lwp)) | |
228e534f | 10606 | elf_tdata (abfd)->core->lwpid = lwp; |
50b2bdb7 | 10607 | |
58e07198 | 10608 | switch (note->type) |
50b2bdb7 | 10609 | { |
58e07198 | 10610 | case NT_NETBSDCORE_PROCINFO: |
50b2bdb7 | 10611 | /* NetBSD-specific core "procinfo". Note that we expect to |
08a40648 AM |
10612 | find this note before any of the others, which is fine, |
10613 | since the kernel writes this note out first when it | |
10614 | creates a core file. */ | |
50b2bdb7 | 10615 | return elfcore_grok_netbsd_procinfo (abfd, note); |
58e07198 CZ |
10616 | #ifdef NT_NETBSDCORE_AUXV |
10617 | case NT_NETBSDCORE_AUXV: | |
10618 | /* NetBSD-specific Elf Auxiliary Vector data. */ | |
10619 | return elfcore_make_auxv_note_section (abfd, note, 4); | |
10620 | #endif | |
10621 | default: | |
10622 | break; | |
50b2bdb7 AM |
10623 | } |
10624 | ||
58e07198 | 10625 | /* As of March 2017 there are no other machine-independent notes |
b4db1224 JT |
10626 | defined for NetBSD core files. If the note type is less |
10627 | than the start of the machine-dependent note types, we don't | |
10628 | understand it. */ | |
47d9a591 | 10629 | |
b4db1224 | 10630 | if (note->type < NT_NETBSDCORE_FIRSTMACH) |
b34976b6 | 10631 | return TRUE; |
50b2bdb7 AM |
10632 | |
10633 | ||
10634 | switch (bfd_get_arch (abfd)) | |
10635 | { | |
08a40648 AM |
10636 | /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and |
10637 | PT_GETFPREGS == mach+2. */ | |
50b2bdb7 AM |
10638 | |
10639 | case bfd_arch_alpha: | |
10640 | case bfd_arch_sparc: | |
10641 | switch (note->type) | |
08a40648 AM |
10642 | { |
10643 | case NT_NETBSDCORE_FIRSTMACH+0: | |
10644 | return elfcore_make_note_pseudosection (abfd, ".reg", note); | |
50b2bdb7 | 10645 | |
08a40648 AM |
10646 | case NT_NETBSDCORE_FIRSTMACH+2: |
10647 | return elfcore_make_note_pseudosection (abfd, ".reg2", note); | |
50b2bdb7 | 10648 | |
08a40648 AM |
10649 | default: |
10650 | return TRUE; | |
10651 | } | |
50b2bdb7 | 10652 | |
58e07198 CZ |
10653 | /* On SuperH, PT_GETREGS == mach+3 and PT_GETFPREGS == mach+5. |
10654 | There's also old PT___GETREGS40 == mach + 1 for old reg | |
10655 | structure which lacks GBR. */ | |
10656 | ||
10657 | case bfd_arch_sh: | |
10658 | switch (note->type) | |
10659 | { | |
10660 | case NT_NETBSDCORE_FIRSTMACH+3: | |
10661 | return elfcore_make_note_pseudosection (abfd, ".reg", note); | |
10662 | ||
10663 | case NT_NETBSDCORE_FIRSTMACH+5: | |
10664 | return elfcore_make_note_pseudosection (abfd, ".reg2", note); | |
10665 | ||
10666 | default: | |
10667 | return TRUE; | |
10668 | } | |
10669 | ||
08a40648 AM |
10670 | /* On all other arch's, PT_GETREGS == mach+1 and |
10671 | PT_GETFPREGS == mach+3. */ | |
50b2bdb7 AM |
10672 | |
10673 | default: | |
10674 | switch (note->type) | |
08a40648 AM |
10675 | { |
10676 | case NT_NETBSDCORE_FIRSTMACH+1: | |
10677 | return elfcore_make_note_pseudosection (abfd, ".reg", note); | |
50b2bdb7 | 10678 | |
08a40648 AM |
10679 | case NT_NETBSDCORE_FIRSTMACH+3: |
10680 | return elfcore_make_note_pseudosection (abfd, ".reg2", note); | |
50b2bdb7 | 10681 | |
08a40648 AM |
10682 | default: |
10683 | return TRUE; | |
10684 | } | |
50b2bdb7 AM |
10685 | } |
10686 | /* NOTREACHED */ | |
10687 | } | |
10688 | ||
67cc5033 MK |
10689 | static bfd_boolean |
10690 | elfcore_grok_openbsd_procinfo (bfd *abfd, Elf_Internal_Note *note) | |
10691 | { | |
80a04378 NC |
10692 | if (note->descsz <= 0x48 + 31) |
10693 | return FALSE; | |
10694 | ||
67cc5033 | 10695 | /* Signal number at offset 0x08. */ |
228e534f | 10696 | elf_tdata (abfd)->core->signal |
67cc5033 MK |
10697 | = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08); |
10698 | ||
10699 | /* Process ID at offset 0x20. */ | |
228e534f | 10700 | elf_tdata (abfd)->core->pid |
67cc5033 MK |
10701 | = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x20); |
10702 | ||
10703 | /* Command name at 0x48 (max 32 bytes, including nul). */ | |
228e534f | 10704 | elf_tdata (abfd)->core->command |
67cc5033 MK |
10705 | = _bfd_elfcore_strndup (abfd, note->descdata + 0x48, 31); |
10706 | ||
10707 | return TRUE; | |
10708 | } | |
10709 | ||
10710 | static bfd_boolean | |
10711 | elfcore_grok_openbsd_note (bfd *abfd, Elf_Internal_Note *note) | |
10712 | { | |
10713 | if (note->type == NT_OPENBSD_PROCINFO) | |
10714 | return elfcore_grok_openbsd_procinfo (abfd, note); | |
10715 | ||
10716 | if (note->type == NT_OPENBSD_REGS) | |
10717 | return elfcore_make_note_pseudosection (abfd, ".reg", note); | |
10718 | ||
10719 | if (note->type == NT_OPENBSD_FPREGS) | |
10720 | return elfcore_make_note_pseudosection (abfd, ".reg2", note); | |
10721 | ||
10722 | if (note->type == NT_OPENBSD_XFPREGS) | |
10723 | return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note); | |
10724 | ||
10725 | if (note->type == NT_OPENBSD_AUXV) | |
58e07198 | 10726 | return elfcore_make_auxv_note_section (abfd, note, 0); |
67cc5033 MK |
10727 | |
10728 | if (note->type == NT_OPENBSD_WCOOKIE) | |
10729 | { | |
10730 | asection *sect = bfd_make_section_anyway_with_flags (abfd, ".wcookie", | |
10731 | SEC_HAS_CONTENTS); | |
10732 | ||
10733 | if (sect == NULL) | |
10734 | return FALSE; | |
10735 | sect->size = note->descsz; | |
10736 | sect->filepos = note->descpos; | |
10737 | sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32; | |
10738 | ||
10739 | return TRUE; | |
10740 | } | |
10741 | ||
10742 | return TRUE; | |
10743 | } | |
10744 | ||
07c6e936 | 10745 | static bfd_boolean |
d3fd4074 | 10746 | elfcore_grok_nto_status (bfd *abfd, Elf_Internal_Note *note, long *tid) |
07c6e936 NC |
10747 | { |
10748 | void *ddata = note->descdata; | |
10749 | char buf[100]; | |
10750 | char *name; | |
10751 | asection *sect; | |
f8843e87 AM |
10752 | short sig; |
10753 | unsigned flags; | |
07c6e936 | 10754 | |
80a04378 NC |
10755 | if (note->descsz < 16) |
10756 | return FALSE; | |
10757 | ||
07c6e936 | 10758 | /* nto_procfs_status 'pid' field is at offset 0. */ |
228e534f | 10759 | elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, (bfd_byte *) ddata); |
07c6e936 | 10760 | |
f8843e87 AM |
10761 | /* nto_procfs_status 'tid' field is at offset 4. Pass it back. */ |
10762 | *tid = bfd_get_32 (abfd, (bfd_byte *) ddata + 4); | |
10763 | ||
10764 | /* nto_procfs_status 'flags' field is at offset 8. */ | |
10765 | flags = bfd_get_32 (abfd, (bfd_byte *) ddata + 8); | |
07c6e936 NC |
10766 | |
10767 | /* nto_procfs_status 'what' field is at offset 14. */ | |
f8843e87 AM |
10768 | if ((sig = bfd_get_16 (abfd, (bfd_byte *) ddata + 14)) > 0) |
10769 | { | |
228e534f AM |
10770 | elf_tdata (abfd)->core->signal = sig; |
10771 | elf_tdata (abfd)->core->lwpid = *tid; | |
f8843e87 | 10772 | } |
07c6e936 | 10773 | |
f8843e87 AM |
10774 | /* _DEBUG_FLAG_CURTID (current thread) is 0x80. Some cores |
10775 | do not come from signals so we make sure we set the current | |
10776 | thread just in case. */ | |
10777 | if (flags & 0x00000080) | |
228e534f | 10778 | elf_tdata (abfd)->core->lwpid = *tid; |
07c6e936 NC |
10779 | |
10780 | /* Make a ".qnx_core_status/%d" section. */ | |
d3fd4074 | 10781 | sprintf (buf, ".qnx_core_status/%ld", *tid); |
07c6e936 | 10782 | |
a50b1753 | 10783 | name = (char *) bfd_alloc (abfd, strlen (buf) + 1); |
07c6e936 NC |
10784 | if (name == NULL) |
10785 | return FALSE; | |
10786 | strcpy (name, buf); | |
10787 | ||
117ed4f8 | 10788 | sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); |
07c6e936 NC |
10789 | if (sect == NULL) |
10790 | return FALSE; | |
10791 | ||
07d6d2b8 AM |
10792 | sect->size = note->descsz; |
10793 | sect->filepos = note->descpos; | |
07c6e936 NC |
10794 | sect->alignment_power = 2; |
10795 | ||
10796 | return (elfcore_maybe_make_sect (abfd, ".qnx_core_status", sect)); | |
10797 | } | |
10798 | ||
10799 | static bfd_boolean | |
d69f560c KW |
10800 | elfcore_grok_nto_regs (bfd *abfd, |
10801 | Elf_Internal_Note *note, | |
d3fd4074 | 10802 | long tid, |
d69f560c | 10803 | char *base) |
07c6e936 NC |
10804 | { |
10805 | char buf[100]; | |
10806 | char *name; | |
10807 | asection *sect; | |
10808 | ||
d69f560c | 10809 | /* Make a "(base)/%d" section. */ |
d3fd4074 | 10810 | sprintf (buf, "%s/%ld", base, tid); |
07c6e936 | 10811 | |
a50b1753 | 10812 | name = (char *) bfd_alloc (abfd, strlen (buf) + 1); |
07c6e936 NC |
10813 | if (name == NULL) |
10814 | return FALSE; | |
10815 | strcpy (name, buf); | |
10816 | ||
117ed4f8 | 10817 | sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); |
07c6e936 NC |
10818 | if (sect == NULL) |
10819 | return FALSE; | |
10820 | ||
07d6d2b8 AM |
10821 | sect->size = note->descsz; |
10822 | sect->filepos = note->descpos; | |
07c6e936 NC |
10823 | sect->alignment_power = 2; |
10824 | ||
f8843e87 | 10825 | /* This is the current thread. */ |
228e534f | 10826 | if (elf_tdata (abfd)->core->lwpid == tid) |
d69f560c | 10827 | return elfcore_maybe_make_sect (abfd, base, sect); |
f8843e87 AM |
10828 | |
10829 | return TRUE; | |
07c6e936 NC |
10830 | } |
10831 | ||
10832 | #define BFD_QNT_CORE_INFO 7 | |
10833 | #define BFD_QNT_CORE_STATUS 8 | |
10834 | #define BFD_QNT_CORE_GREG 9 | |
10835 | #define BFD_QNT_CORE_FPREG 10 | |
10836 | ||
10837 | static bfd_boolean | |
217aa764 | 10838 | elfcore_grok_nto_note (bfd *abfd, Elf_Internal_Note *note) |
07c6e936 NC |
10839 | { |
10840 | /* Every GREG section has a STATUS section before it. Store the | |
811072d8 | 10841 | tid from the previous call to pass down to the next gregs |
07c6e936 | 10842 | function. */ |
d3fd4074 | 10843 | static long tid = 1; |
07c6e936 NC |
10844 | |
10845 | switch (note->type) | |
10846 | { | |
d69f560c KW |
10847 | case BFD_QNT_CORE_INFO: |
10848 | return elfcore_make_note_pseudosection (abfd, ".qnx_core_info", note); | |
10849 | case BFD_QNT_CORE_STATUS: | |
10850 | return elfcore_grok_nto_status (abfd, note, &tid); | |
10851 | case BFD_QNT_CORE_GREG: | |
10852 | return elfcore_grok_nto_regs (abfd, note, tid, ".reg"); | |
10853 | case BFD_QNT_CORE_FPREG: | |
10854 | return elfcore_grok_nto_regs (abfd, note, tid, ".reg2"); | |
10855 | default: | |
10856 | return TRUE; | |
07c6e936 NC |
10857 | } |
10858 | } | |
10859 | ||
b15fa79e AM |
10860 | static bfd_boolean |
10861 | elfcore_grok_spu_note (bfd *abfd, Elf_Internal_Note *note) | |
10862 | { | |
10863 | char *name; | |
10864 | asection *sect; | |
10865 | size_t len; | |
10866 | ||
10867 | /* Use note name as section name. */ | |
10868 | len = note->namesz; | |
a50b1753 | 10869 | name = (char *) bfd_alloc (abfd, len); |
b15fa79e AM |
10870 | if (name == NULL) |
10871 | return FALSE; | |
10872 | memcpy (name, note->namedata, len); | |
10873 | name[len - 1] = '\0'; | |
10874 | ||
10875 | sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); | |
10876 | if (sect == NULL) | |
10877 | return FALSE; | |
10878 | ||
07d6d2b8 AM |
10879 | sect->size = note->descsz; |
10880 | sect->filepos = note->descpos; | |
b15fa79e AM |
10881 | sect->alignment_power = 1; |
10882 | ||
10883 | return TRUE; | |
10884 | } | |
10885 | ||
7c76fa91 MS |
10886 | /* Function: elfcore_write_note |
10887 | ||
47d9a591 | 10888 | Inputs: |
a39f3346 | 10889 | buffer to hold note, and current size of buffer |
7c76fa91 MS |
10890 | name of note |
10891 | type of note | |
10892 | data for note | |
10893 | size of data for note | |
10894 | ||
a39f3346 AM |
10895 | Writes note to end of buffer. ELF64 notes are written exactly as |
10896 | for ELF32, despite the current (as of 2006) ELF gabi specifying | |
10897 | that they ought to have 8-byte namesz and descsz field, and have | |
10898 | 8-byte alignment. Other writers, eg. Linux kernel, do the same. | |
10899 | ||
7c76fa91 | 10900 | Return: |
a39f3346 | 10901 | Pointer to realloc'd buffer, *BUFSIZ updated. */ |
7c76fa91 MS |
10902 | |
10903 | char * | |
a39f3346 | 10904 | elfcore_write_note (bfd *abfd, |
217aa764 | 10905 | char *buf, |
a39f3346 | 10906 | int *bufsiz, |
217aa764 | 10907 | const char *name, |
a39f3346 | 10908 | int type, |
217aa764 | 10909 | const void *input, |
a39f3346 | 10910 | int size) |
7c76fa91 MS |
10911 | { |
10912 | Elf_External_Note *xnp; | |
d4c88bbb | 10913 | size_t namesz; |
d4c88bbb | 10914 | size_t newspace; |
a39f3346 | 10915 | char *dest; |
7c76fa91 | 10916 | |
d4c88bbb | 10917 | namesz = 0; |
d4c88bbb | 10918 | if (name != NULL) |
a39f3346 | 10919 | namesz = strlen (name) + 1; |
d4c88bbb | 10920 | |
a39f3346 | 10921 | newspace = 12 + ((namesz + 3) & -4) + ((size + 3) & -4); |
d4c88bbb | 10922 | |
a50b1753 | 10923 | buf = (char *) realloc (buf, *bufsiz + newspace); |
14b1c01e AM |
10924 | if (buf == NULL) |
10925 | return buf; | |
a39f3346 | 10926 | dest = buf + *bufsiz; |
7c76fa91 MS |
10927 | *bufsiz += newspace; |
10928 | xnp = (Elf_External_Note *) dest; | |
10929 | H_PUT_32 (abfd, namesz, xnp->namesz); | |
10930 | H_PUT_32 (abfd, size, xnp->descsz); | |
10931 | H_PUT_32 (abfd, type, xnp->type); | |
d4c88bbb AM |
10932 | dest = xnp->name; |
10933 | if (name != NULL) | |
10934 | { | |
10935 | memcpy (dest, name, namesz); | |
10936 | dest += namesz; | |
a39f3346 | 10937 | while (namesz & 3) |
d4c88bbb AM |
10938 | { |
10939 | *dest++ = '\0'; | |
a39f3346 | 10940 | ++namesz; |
d4c88bbb AM |
10941 | } |
10942 | } | |
10943 | memcpy (dest, input, size); | |
a39f3346 AM |
10944 | dest += size; |
10945 | while (size & 3) | |
10946 | { | |
10947 | *dest++ = '\0'; | |
10948 | ++size; | |
10949 | } | |
10950 | return buf; | |
7c76fa91 MS |
10951 | } |
10952 | ||
602f1657 AM |
10953 | /* gcc-8 warns (*) on all the strncpy calls in this function about |
10954 | possible string truncation. The "truncation" is not a bug. We | |
10955 | have an external representation of structs with fields that are not | |
10956 | necessarily NULL terminated and corresponding internal | |
10957 | representation fields that are one larger so that they can always | |
10958 | be NULL terminated. | |
10959 | gcc versions between 4.2 and 4.6 do not allow pragma control of | |
10960 | diagnostics inside functions, giving a hard error if you try to use | |
10961 | the finer control available with later versions. | |
10962 | gcc prior to 4.2 warns about diagnostic push and pop. | |
10963 | gcc-5, gcc-6 and gcc-7 warn that -Wstringop-truncation is unknown, | |
10964 | unless you also add #pragma GCC diagnostic ignored "-Wpragma". | |
10965 | (*) Depending on your system header files! */ | |
d99b4b92 | 10966 | #if GCC_VERSION >= 8000 |
602f1657 AM |
10967 | # pragma GCC diagnostic push |
10968 | # pragma GCC diagnostic ignored "-Wstringop-truncation" | |
d99b4b92 | 10969 | #endif |
7c76fa91 | 10970 | char * |
217aa764 AM |
10971 | elfcore_write_prpsinfo (bfd *abfd, |
10972 | char *buf, | |
10973 | int *bufsiz, | |
10974 | const char *fname, | |
10975 | const char *psargs) | |
7c76fa91 | 10976 | { |
183e98be AM |
10977 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
10978 | ||
10979 | if (bed->elf_backend_write_core_note != NULL) | |
10980 | { | |
10981 | char *ret; | |
10982 | ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz, | |
10983 | NT_PRPSINFO, fname, psargs); | |
10984 | if (ret != NULL) | |
10985 | return ret; | |
10986 | } | |
7c76fa91 | 10987 | |
1f20dca5 | 10988 | #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) |
602f1657 | 10989 | # if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T) |
183e98be AM |
10990 | if (bed->s->elfclass == ELFCLASS32) |
10991 | { | |
602f1657 | 10992 | # if defined (HAVE_PSINFO32_T) |
183e98be AM |
10993 | psinfo32_t data; |
10994 | int note_type = NT_PSINFO; | |
602f1657 | 10995 | # else |
183e98be AM |
10996 | prpsinfo32_t data; |
10997 | int note_type = NT_PRPSINFO; | |
602f1657 | 10998 | # endif |
183e98be AM |
10999 | |
11000 | memset (&data, 0, sizeof (data)); | |
11001 | strncpy (data.pr_fname, fname, sizeof (data.pr_fname)); | |
11002 | strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs)); | |
11003 | return elfcore_write_note (abfd, buf, bufsiz, | |
1f20dca5 | 11004 | "CORE", note_type, &data, sizeof (data)); |
183e98be AM |
11005 | } |
11006 | else | |
602f1657 | 11007 | # endif |
183e98be | 11008 | { |
602f1657 | 11009 | # if defined (HAVE_PSINFO_T) |
183e98be AM |
11010 | psinfo_t data; |
11011 | int note_type = NT_PSINFO; | |
602f1657 | 11012 | # else |
183e98be AM |
11013 | prpsinfo_t data; |
11014 | int note_type = NT_PRPSINFO; | |
602f1657 | 11015 | # endif |
7c76fa91 | 11016 | |
183e98be AM |
11017 | memset (&data, 0, sizeof (data)); |
11018 | strncpy (data.pr_fname, fname, sizeof (data.pr_fname)); | |
11019 | strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs)); | |
11020 | return elfcore_write_note (abfd, buf, bufsiz, | |
1f20dca5 | 11021 | "CORE", note_type, &data, sizeof (data)); |
183e98be | 11022 | } |
7c76fa91 MS |
11023 | #endif /* PSINFO_T or PRPSINFO_T */ |
11024 | ||
1f20dca5 UW |
11025 | free (buf); |
11026 | return NULL; | |
11027 | } | |
d99b4b92 | 11028 | #if GCC_VERSION >= 8000 |
602f1657 | 11029 | # pragma GCC diagnostic pop |
d99b4b92 | 11030 | #endif |
1f20dca5 | 11031 | |
70a38d42 SDJ |
11032 | char * |
11033 | elfcore_write_linux_prpsinfo32 | |
11034 | (bfd *abfd, char *buf, int *bufsiz, | |
11035 | const struct elf_internal_linux_prpsinfo *prpsinfo) | |
11036 | { | |
a2f63b2e MR |
11037 | if (get_elf_backend_data (abfd)->linux_prpsinfo32_ugid16) |
11038 | { | |
11039 | struct elf_external_linux_prpsinfo32_ugid16 data; | |
11040 | ||
11041 | swap_linux_prpsinfo32_ugid16_out (abfd, prpsinfo, &data); | |
11042 | return elfcore_write_note (abfd, buf, bufsiz, "CORE", NT_PRPSINFO, | |
11043 | &data, sizeof (data)); | |
11044 | } | |
11045 | else | |
11046 | { | |
11047 | struct elf_external_linux_prpsinfo32_ugid32 data; | |
70a38d42 | 11048 | |
a2f63b2e MR |
11049 | swap_linux_prpsinfo32_ugid32_out (abfd, prpsinfo, &data); |
11050 | return elfcore_write_note (abfd, buf, bufsiz, "CORE", NT_PRPSINFO, | |
11051 | &data, sizeof (data)); | |
11052 | } | |
70a38d42 SDJ |
11053 | } |
11054 | ||
11055 | char * | |
11056 | elfcore_write_linux_prpsinfo64 | |
11057 | (bfd *abfd, char *buf, int *bufsiz, | |
11058 | const struct elf_internal_linux_prpsinfo *prpsinfo) | |
11059 | { | |
3c9a7b0d MR |
11060 | if (get_elf_backend_data (abfd)->linux_prpsinfo64_ugid16) |
11061 | { | |
11062 | struct elf_external_linux_prpsinfo64_ugid16 data; | |
11063 | ||
11064 | swap_linux_prpsinfo64_ugid16_out (abfd, prpsinfo, &data); | |
11065 | return elfcore_write_note (abfd, buf, bufsiz, | |
11066 | "CORE", NT_PRPSINFO, &data, sizeof (data)); | |
11067 | } | |
11068 | else | |
11069 | { | |
11070 | struct elf_external_linux_prpsinfo64_ugid32 data; | |
70a38d42 | 11071 | |
3c9a7b0d MR |
11072 | swap_linux_prpsinfo64_ugid32_out (abfd, prpsinfo, &data); |
11073 | return elfcore_write_note (abfd, buf, bufsiz, | |
11074 | "CORE", NT_PRPSINFO, &data, sizeof (data)); | |
11075 | } | |
70a38d42 SDJ |
11076 | } |
11077 | ||
7c76fa91 | 11078 | char * |
217aa764 AM |
11079 | elfcore_write_prstatus (bfd *abfd, |
11080 | char *buf, | |
11081 | int *bufsiz, | |
11082 | long pid, | |
11083 | int cursig, | |
11084 | const void *gregs) | |
7c76fa91 | 11085 | { |
183e98be | 11086 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
7c76fa91 | 11087 | |
183e98be AM |
11088 | if (bed->elf_backend_write_core_note != NULL) |
11089 | { | |
11090 | char *ret; | |
11091 | ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz, | |
11092 | NT_PRSTATUS, | |
11093 | pid, cursig, gregs); | |
11094 | if (ret != NULL) | |
11095 | return ret; | |
11096 | } | |
11097 | ||
1f20dca5 | 11098 | #if defined (HAVE_PRSTATUS_T) |
183e98be AM |
11099 | #if defined (HAVE_PRSTATUS32_T) |
11100 | if (bed->s->elfclass == ELFCLASS32) | |
11101 | { | |
11102 | prstatus32_t prstat; | |
11103 | ||
11104 | memset (&prstat, 0, sizeof (prstat)); | |
11105 | prstat.pr_pid = pid; | |
11106 | prstat.pr_cursig = cursig; | |
11107 | memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg)); | |
1f20dca5 | 11108 | return elfcore_write_note (abfd, buf, bufsiz, "CORE", |
183e98be AM |
11109 | NT_PRSTATUS, &prstat, sizeof (prstat)); |
11110 | } | |
11111 | else | |
11112 | #endif | |
11113 | { | |
11114 | prstatus_t prstat; | |
11115 | ||
11116 | memset (&prstat, 0, sizeof (prstat)); | |
11117 | prstat.pr_pid = pid; | |
11118 | prstat.pr_cursig = cursig; | |
11119 | memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg)); | |
1f20dca5 | 11120 | return elfcore_write_note (abfd, buf, bufsiz, "CORE", |
183e98be AM |
11121 | NT_PRSTATUS, &prstat, sizeof (prstat)); |
11122 | } | |
7c76fa91 MS |
11123 | #endif /* HAVE_PRSTATUS_T */ |
11124 | ||
1f20dca5 UW |
11125 | free (buf); |
11126 | return NULL; | |
11127 | } | |
11128 | ||
51316059 MS |
11129 | #if defined (HAVE_LWPSTATUS_T) |
11130 | char * | |
217aa764 AM |
11131 | elfcore_write_lwpstatus (bfd *abfd, |
11132 | char *buf, | |
11133 | int *bufsiz, | |
11134 | long pid, | |
11135 | int cursig, | |
11136 | const void *gregs) | |
51316059 MS |
11137 | { |
11138 | lwpstatus_t lwpstat; | |
183e98be | 11139 | const char *note_name = "CORE"; |
51316059 MS |
11140 | |
11141 | memset (&lwpstat, 0, sizeof (lwpstat)); | |
11142 | lwpstat.pr_lwpid = pid >> 16; | |
11143 | lwpstat.pr_cursig = cursig; | |
11144 | #if defined (HAVE_LWPSTATUS_T_PR_REG) | |
d1e8523e | 11145 | memcpy (&lwpstat.pr_reg, gregs, sizeof (lwpstat.pr_reg)); |
51316059 MS |
11146 | #elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT) |
11147 | #if !defined(gregs) | |
11148 | memcpy (lwpstat.pr_context.uc_mcontext.gregs, | |
11149 | gregs, sizeof (lwpstat.pr_context.uc_mcontext.gregs)); | |
11150 | #else | |
11151 | memcpy (lwpstat.pr_context.uc_mcontext.__gregs, | |
11152 | gregs, sizeof (lwpstat.pr_context.uc_mcontext.__gregs)); | |
11153 | #endif | |
11154 | #endif | |
47d9a591 | 11155 | return elfcore_write_note (abfd, buf, bufsiz, note_name, |
51316059 MS |
11156 | NT_LWPSTATUS, &lwpstat, sizeof (lwpstat)); |
11157 | } | |
11158 | #endif /* HAVE_LWPSTATUS_T */ | |
11159 | ||
7c76fa91 MS |
11160 | #if defined (HAVE_PSTATUS_T) |
11161 | char * | |
217aa764 AM |
11162 | elfcore_write_pstatus (bfd *abfd, |
11163 | char *buf, | |
11164 | int *bufsiz, | |
11165 | long pid, | |
6c10990d NC |
11166 | int cursig ATTRIBUTE_UNUSED, |
11167 | const void *gregs ATTRIBUTE_UNUSED) | |
7c76fa91 | 11168 | { |
183e98be AM |
11169 | const char *note_name = "CORE"; |
11170 | #if defined (HAVE_PSTATUS32_T) | |
11171 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
7c76fa91 | 11172 | |
183e98be AM |
11173 | if (bed->s->elfclass == ELFCLASS32) |
11174 | { | |
11175 | pstatus32_t pstat; | |
11176 | ||
11177 | memset (&pstat, 0, sizeof (pstat)); | |
11178 | pstat.pr_pid = pid & 0xffff; | |
11179 | buf = elfcore_write_note (abfd, buf, bufsiz, note_name, | |
11180 | NT_PSTATUS, &pstat, sizeof (pstat)); | |
11181 | return buf; | |
11182 | } | |
11183 | else | |
11184 | #endif | |
11185 | { | |
11186 | pstatus_t pstat; | |
11187 | ||
11188 | memset (&pstat, 0, sizeof (pstat)); | |
11189 | pstat.pr_pid = pid & 0xffff; | |
11190 | buf = elfcore_write_note (abfd, buf, bufsiz, note_name, | |
11191 | NT_PSTATUS, &pstat, sizeof (pstat)); | |
11192 | return buf; | |
11193 | } | |
7c76fa91 MS |
11194 | } |
11195 | #endif /* HAVE_PSTATUS_T */ | |
11196 | ||
11197 | char * | |
217aa764 AM |
11198 | elfcore_write_prfpreg (bfd *abfd, |
11199 | char *buf, | |
11200 | int *bufsiz, | |
11201 | const void *fpregs, | |
11202 | int size) | |
7c76fa91 | 11203 | { |
183e98be | 11204 | const char *note_name = "CORE"; |
47d9a591 | 11205 | return elfcore_write_note (abfd, buf, bufsiz, |
7c76fa91 MS |
11206 | note_name, NT_FPREGSET, fpregs, size); |
11207 | } | |
11208 | ||
11209 | char * | |
217aa764 AM |
11210 | elfcore_write_prxfpreg (bfd *abfd, |
11211 | char *buf, | |
11212 | int *bufsiz, | |
11213 | const void *xfpregs, | |
11214 | int size) | |
7c76fa91 MS |
11215 | { |
11216 | char *note_name = "LINUX"; | |
47d9a591 | 11217 | return elfcore_write_note (abfd, buf, bufsiz, |
7c76fa91 MS |
11218 | note_name, NT_PRXFPREG, xfpregs, size); |
11219 | } | |
11220 | ||
4339cae0 L |
11221 | char * |
11222 | elfcore_write_xstatereg (bfd *abfd, char *buf, int *bufsiz, | |
11223 | const void *xfpregs, int size) | |
11224 | { | |
97de3545 JB |
11225 | char *note_name; |
11226 | if (get_elf_backend_data (abfd)->elf_osabi == ELFOSABI_FREEBSD) | |
11227 | note_name = "FreeBSD"; | |
11228 | else | |
11229 | note_name = "LINUX"; | |
4339cae0 L |
11230 | return elfcore_write_note (abfd, buf, bufsiz, |
11231 | note_name, NT_X86_XSTATE, xfpregs, size); | |
11232 | } | |
11233 | ||
97753bd5 AM |
11234 | char * |
11235 | elfcore_write_ppc_vmx (bfd *abfd, | |
11236 | char *buf, | |
11237 | int *bufsiz, | |
11238 | const void *ppc_vmx, | |
11239 | int size) | |
11240 | { | |
11241 | char *note_name = "LINUX"; | |
11242 | return elfcore_write_note (abfd, buf, bufsiz, | |
11243 | note_name, NT_PPC_VMX, ppc_vmx, size); | |
11244 | } | |
11245 | ||
89eeb0bc LM |
11246 | char * |
11247 | elfcore_write_ppc_vsx (bfd *abfd, | |
07d6d2b8 AM |
11248 | char *buf, |
11249 | int *bufsiz, | |
11250 | const void *ppc_vsx, | |
11251 | int size) | |
89eeb0bc LM |
11252 | { |
11253 | char *note_name = "LINUX"; | |
11254 | return elfcore_write_note (abfd, buf, bufsiz, | |
07d6d2b8 | 11255 | note_name, NT_PPC_VSX, ppc_vsx, size); |
89eeb0bc LM |
11256 | } |
11257 | ||
cb2366c1 EBM |
11258 | char * |
11259 | elfcore_write_ppc_tar (bfd *abfd, | |
4b24dd1a AM |
11260 | char *buf, |
11261 | int *bufsiz, | |
11262 | const void *ppc_tar, | |
11263 | int size) | |
cb2366c1 EBM |
11264 | { |
11265 | char *note_name = "LINUX"; | |
11266 | return elfcore_write_note (abfd, buf, bufsiz, | |
4b24dd1a | 11267 | note_name, NT_PPC_TAR, ppc_tar, size); |
cb2366c1 EBM |
11268 | } |
11269 | ||
11270 | char * | |
11271 | elfcore_write_ppc_ppr (bfd *abfd, | |
4b24dd1a AM |
11272 | char *buf, |
11273 | int *bufsiz, | |
11274 | const void *ppc_ppr, | |
11275 | int size) | |
cb2366c1 EBM |
11276 | { |
11277 | char *note_name = "LINUX"; | |
11278 | return elfcore_write_note (abfd, buf, bufsiz, | |
4b24dd1a | 11279 | note_name, NT_PPC_PPR, ppc_ppr, size); |
cb2366c1 EBM |
11280 | } |
11281 | ||
11282 | char * | |
11283 | elfcore_write_ppc_dscr (bfd *abfd, | |
4b24dd1a AM |
11284 | char *buf, |
11285 | int *bufsiz, | |
11286 | const void *ppc_dscr, | |
11287 | int size) | |
cb2366c1 EBM |
11288 | { |
11289 | char *note_name = "LINUX"; | |
11290 | return elfcore_write_note (abfd, buf, bufsiz, | |
4b24dd1a | 11291 | note_name, NT_PPC_DSCR, ppc_dscr, size); |
cb2366c1 EBM |
11292 | } |
11293 | ||
11294 | char * | |
11295 | elfcore_write_ppc_ebb (bfd *abfd, | |
4b24dd1a AM |
11296 | char *buf, |
11297 | int *bufsiz, | |
11298 | const void *ppc_ebb, | |
11299 | int size) | |
cb2366c1 EBM |
11300 | { |
11301 | char *note_name = "LINUX"; | |
11302 | return elfcore_write_note (abfd, buf, bufsiz, | |
4b24dd1a | 11303 | note_name, NT_PPC_EBB, ppc_ebb, size); |
cb2366c1 EBM |
11304 | } |
11305 | ||
11306 | char * | |
11307 | elfcore_write_ppc_pmu (bfd *abfd, | |
4b24dd1a AM |
11308 | char *buf, |
11309 | int *bufsiz, | |
11310 | const void *ppc_pmu, | |
11311 | int size) | |
cb2366c1 EBM |
11312 | { |
11313 | char *note_name = "LINUX"; | |
11314 | return elfcore_write_note (abfd, buf, bufsiz, | |
4b24dd1a | 11315 | note_name, NT_PPC_PMU, ppc_pmu, size); |
cb2366c1 EBM |
11316 | } |
11317 | ||
11318 | char * | |
11319 | elfcore_write_ppc_tm_cgpr (bfd *abfd, | |
4b24dd1a AM |
11320 | char *buf, |
11321 | int *bufsiz, | |
11322 | const void *ppc_tm_cgpr, | |
11323 | int size) | |
cb2366c1 EBM |
11324 | { |
11325 | char *note_name = "LINUX"; | |
11326 | return elfcore_write_note (abfd, buf, bufsiz, | |
4b24dd1a | 11327 | note_name, NT_PPC_TM_CGPR, ppc_tm_cgpr, size); |
cb2366c1 EBM |
11328 | } |
11329 | ||
11330 | char * | |
11331 | elfcore_write_ppc_tm_cfpr (bfd *abfd, | |
4b24dd1a AM |
11332 | char *buf, |
11333 | int *bufsiz, | |
11334 | const void *ppc_tm_cfpr, | |
11335 | int size) | |
cb2366c1 EBM |
11336 | { |
11337 | char *note_name = "LINUX"; | |
11338 | return elfcore_write_note (abfd, buf, bufsiz, | |
4b24dd1a | 11339 | note_name, NT_PPC_TM_CFPR, ppc_tm_cfpr, size); |
cb2366c1 EBM |
11340 | } |
11341 | ||
11342 | char * | |
11343 | elfcore_write_ppc_tm_cvmx (bfd *abfd, | |
4b24dd1a AM |
11344 | char *buf, |
11345 | int *bufsiz, | |
11346 | const void *ppc_tm_cvmx, | |
11347 | int size) | |
cb2366c1 EBM |
11348 | { |
11349 | char *note_name = "LINUX"; | |
11350 | return elfcore_write_note (abfd, buf, bufsiz, | |
4b24dd1a | 11351 | note_name, NT_PPC_TM_CVMX, ppc_tm_cvmx, size); |
cb2366c1 EBM |
11352 | } |
11353 | ||
11354 | char * | |
11355 | elfcore_write_ppc_tm_cvsx (bfd *abfd, | |
4b24dd1a AM |
11356 | char *buf, |
11357 | int *bufsiz, | |
11358 | const void *ppc_tm_cvsx, | |
11359 | int size) | |
cb2366c1 EBM |
11360 | { |
11361 | char *note_name = "LINUX"; | |
11362 | return elfcore_write_note (abfd, buf, bufsiz, | |
4b24dd1a | 11363 | note_name, NT_PPC_TM_CVSX, ppc_tm_cvsx, size); |
cb2366c1 EBM |
11364 | } |
11365 | ||
11366 | char * | |
11367 | elfcore_write_ppc_tm_spr (bfd *abfd, | |
4b24dd1a AM |
11368 | char *buf, |
11369 | int *bufsiz, | |
11370 | const void *ppc_tm_spr, | |
11371 | int size) | |
cb2366c1 EBM |
11372 | { |
11373 | char *note_name = "LINUX"; | |
11374 | return elfcore_write_note (abfd, buf, bufsiz, | |
4b24dd1a | 11375 | note_name, NT_PPC_TM_SPR, ppc_tm_spr, size); |
cb2366c1 EBM |
11376 | } |
11377 | ||
11378 | char * | |
11379 | elfcore_write_ppc_tm_ctar (bfd *abfd, | |
4b24dd1a AM |
11380 | char *buf, |
11381 | int *bufsiz, | |
11382 | const void *ppc_tm_ctar, | |
11383 | int size) | |
cb2366c1 EBM |
11384 | { |
11385 | char *note_name = "LINUX"; | |
11386 | return elfcore_write_note (abfd, buf, bufsiz, | |
4b24dd1a | 11387 | note_name, NT_PPC_TM_CTAR, ppc_tm_ctar, size); |
cb2366c1 EBM |
11388 | } |
11389 | ||
11390 | char * | |
11391 | elfcore_write_ppc_tm_cppr (bfd *abfd, | |
4b24dd1a AM |
11392 | char *buf, |
11393 | int *bufsiz, | |
11394 | const void *ppc_tm_cppr, | |
11395 | int size) | |
cb2366c1 EBM |
11396 | { |
11397 | char *note_name = "LINUX"; | |
11398 | return elfcore_write_note (abfd, buf, bufsiz, | |
4b24dd1a | 11399 | note_name, NT_PPC_TM_CPPR, ppc_tm_cppr, size); |
cb2366c1 EBM |
11400 | } |
11401 | ||
11402 | char * | |
11403 | elfcore_write_ppc_tm_cdscr (bfd *abfd, | |
4b24dd1a AM |
11404 | char *buf, |
11405 | int *bufsiz, | |
11406 | const void *ppc_tm_cdscr, | |
11407 | int size) | |
cb2366c1 EBM |
11408 | { |
11409 | char *note_name = "LINUX"; | |
11410 | return elfcore_write_note (abfd, buf, bufsiz, | |
4b24dd1a | 11411 | note_name, NT_PPC_TM_CDSCR, ppc_tm_cdscr, size); |
cb2366c1 EBM |
11412 | } |
11413 | ||
0675e188 UW |
11414 | static char * |
11415 | elfcore_write_s390_high_gprs (bfd *abfd, | |
11416 | char *buf, | |
11417 | int *bufsiz, | |
11418 | const void *s390_high_gprs, | |
11419 | int size) | |
11420 | { | |
11421 | char *note_name = "LINUX"; | |
11422 | return elfcore_write_note (abfd, buf, bufsiz, | |
07d6d2b8 | 11423 | note_name, NT_S390_HIGH_GPRS, |
0675e188 UW |
11424 | s390_high_gprs, size); |
11425 | } | |
11426 | ||
d7eeb400 MS |
11427 | char * |
11428 | elfcore_write_s390_timer (bfd *abfd, | |
07d6d2b8 AM |
11429 | char *buf, |
11430 | int *bufsiz, | |
11431 | const void *s390_timer, | |
11432 | int size) | |
d7eeb400 MS |
11433 | { |
11434 | char *note_name = "LINUX"; | |
11435 | return elfcore_write_note (abfd, buf, bufsiz, | |
07d6d2b8 | 11436 | note_name, NT_S390_TIMER, s390_timer, size); |
d7eeb400 MS |
11437 | } |
11438 | ||
11439 | char * | |
11440 | elfcore_write_s390_todcmp (bfd *abfd, | |
07d6d2b8 AM |
11441 | char *buf, |
11442 | int *bufsiz, | |
11443 | const void *s390_todcmp, | |
11444 | int size) | |
d7eeb400 MS |
11445 | { |
11446 | char *note_name = "LINUX"; | |
11447 | return elfcore_write_note (abfd, buf, bufsiz, | |
07d6d2b8 | 11448 | note_name, NT_S390_TODCMP, s390_todcmp, size); |
d7eeb400 MS |
11449 | } |
11450 | ||
11451 | char * | |
11452 | elfcore_write_s390_todpreg (bfd *abfd, | |
07d6d2b8 AM |
11453 | char *buf, |
11454 | int *bufsiz, | |
11455 | const void *s390_todpreg, | |
11456 | int size) | |
d7eeb400 MS |
11457 | { |
11458 | char *note_name = "LINUX"; | |
11459 | return elfcore_write_note (abfd, buf, bufsiz, | |
07d6d2b8 | 11460 | note_name, NT_S390_TODPREG, s390_todpreg, size); |
d7eeb400 MS |
11461 | } |
11462 | ||
11463 | char * | |
11464 | elfcore_write_s390_ctrs (bfd *abfd, | |
07d6d2b8 AM |
11465 | char *buf, |
11466 | int *bufsiz, | |
11467 | const void *s390_ctrs, | |
11468 | int size) | |
d7eeb400 MS |
11469 | { |
11470 | char *note_name = "LINUX"; | |
11471 | return elfcore_write_note (abfd, buf, bufsiz, | |
07d6d2b8 | 11472 | note_name, NT_S390_CTRS, s390_ctrs, size); |
d7eeb400 MS |
11473 | } |
11474 | ||
11475 | char * | |
11476 | elfcore_write_s390_prefix (bfd *abfd, | |
07d6d2b8 AM |
11477 | char *buf, |
11478 | int *bufsiz, | |
11479 | const void *s390_prefix, | |
11480 | int size) | |
d7eeb400 MS |
11481 | { |
11482 | char *note_name = "LINUX"; | |
11483 | return elfcore_write_note (abfd, buf, bufsiz, | |
07d6d2b8 | 11484 | note_name, NT_S390_PREFIX, s390_prefix, size); |
d7eeb400 MS |
11485 | } |
11486 | ||
355b81d9 UW |
11487 | char * |
11488 | elfcore_write_s390_last_break (bfd *abfd, | |
11489 | char *buf, | |
11490 | int *bufsiz, | |
11491 | const void *s390_last_break, | |
11492 | int size) | |
11493 | { | |
11494 | char *note_name = "LINUX"; | |
11495 | return elfcore_write_note (abfd, buf, bufsiz, | |
07d6d2b8 | 11496 | note_name, NT_S390_LAST_BREAK, |
355b81d9 UW |
11497 | s390_last_break, size); |
11498 | } | |
11499 | ||
11500 | char * | |
11501 | elfcore_write_s390_system_call (bfd *abfd, | |
11502 | char *buf, | |
11503 | int *bufsiz, | |
11504 | const void *s390_system_call, | |
11505 | int size) | |
11506 | { | |
11507 | char *note_name = "LINUX"; | |
11508 | return elfcore_write_note (abfd, buf, bufsiz, | |
07d6d2b8 | 11509 | note_name, NT_S390_SYSTEM_CALL, |
355b81d9 UW |
11510 | s390_system_call, size); |
11511 | } | |
11512 | ||
abb3f6cc NC |
11513 | char * |
11514 | elfcore_write_s390_tdb (bfd *abfd, | |
11515 | char *buf, | |
11516 | int *bufsiz, | |
11517 | const void *s390_tdb, | |
11518 | int size) | |
11519 | { | |
11520 | char *note_name = "LINUX"; | |
11521 | return elfcore_write_note (abfd, buf, bufsiz, | |
07d6d2b8 | 11522 | note_name, NT_S390_TDB, s390_tdb, size); |
abb3f6cc NC |
11523 | } |
11524 | ||
4ef9f41a AA |
11525 | char * |
11526 | elfcore_write_s390_vxrs_low (bfd *abfd, | |
11527 | char *buf, | |
11528 | int *bufsiz, | |
11529 | const void *s390_vxrs_low, | |
11530 | int size) | |
11531 | { | |
11532 | char *note_name = "LINUX"; | |
11533 | return elfcore_write_note (abfd, buf, bufsiz, | |
11534 | note_name, NT_S390_VXRS_LOW, s390_vxrs_low, size); | |
11535 | } | |
11536 | ||
11537 | char * | |
11538 | elfcore_write_s390_vxrs_high (bfd *abfd, | |
11539 | char *buf, | |
11540 | int *bufsiz, | |
11541 | const void *s390_vxrs_high, | |
11542 | int size) | |
11543 | { | |
11544 | char *note_name = "LINUX"; | |
11545 | return elfcore_write_note (abfd, buf, bufsiz, | |
11546 | note_name, NT_S390_VXRS_HIGH, | |
11547 | s390_vxrs_high, size); | |
11548 | } | |
11549 | ||
88ab90e8 AA |
11550 | char * |
11551 | elfcore_write_s390_gs_cb (bfd *abfd, | |
11552 | char *buf, | |
11553 | int *bufsiz, | |
11554 | const void *s390_gs_cb, | |
11555 | int size) | |
11556 | { | |
11557 | char *note_name = "LINUX"; | |
11558 | return elfcore_write_note (abfd, buf, bufsiz, | |
11559 | note_name, NT_S390_GS_CB, | |
11560 | s390_gs_cb, size); | |
11561 | } | |
11562 | ||
11563 | char * | |
11564 | elfcore_write_s390_gs_bc (bfd *abfd, | |
11565 | char *buf, | |
11566 | int *bufsiz, | |
11567 | const void *s390_gs_bc, | |
11568 | int size) | |
11569 | { | |
11570 | char *note_name = "LINUX"; | |
11571 | return elfcore_write_note (abfd, buf, bufsiz, | |
11572 | note_name, NT_S390_GS_BC, | |
11573 | s390_gs_bc, size); | |
11574 | } | |
11575 | ||
faa9a424 UW |
11576 | char * |
11577 | elfcore_write_arm_vfp (bfd *abfd, | |
11578 | char *buf, | |
11579 | int *bufsiz, | |
11580 | const void *arm_vfp, | |
11581 | int size) | |
11582 | { | |
11583 | char *note_name = "LINUX"; | |
11584 | return elfcore_write_note (abfd, buf, bufsiz, | |
11585 | note_name, NT_ARM_VFP, arm_vfp, size); | |
11586 | } | |
11587 | ||
652451f8 YZ |
11588 | char * |
11589 | elfcore_write_aarch_tls (bfd *abfd, | |
11590 | char *buf, | |
11591 | int *bufsiz, | |
11592 | const void *aarch_tls, | |
11593 | int size) | |
11594 | { | |
11595 | char *note_name = "LINUX"; | |
11596 | return elfcore_write_note (abfd, buf, bufsiz, | |
11597 | note_name, NT_ARM_TLS, aarch_tls, size); | |
11598 | } | |
11599 | ||
11600 | char * | |
11601 | elfcore_write_aarch_hw_break (bfd *abfd, | |
11602 | char *buf, | |
11603 | int *bufsiz, | |
11604 | const void *aarch_hw_break, | |
11605 | int size) | |
11606 | { | |
11607 | char *note_name = "LINUX"; | |
11608 | return elfcore_write_note (abfd, buf, bufsiz, | |
11609 | note_name, NT_ARM_HW_BREAK, aarch_hw_break, size); | |
11610 | } | |
11611 | ||
11612 | char * | |
11613 | elfcore_write_aarch_hw_watch (bfd *abfd, | |
11614 | char *buf, | |
11615 | int *bufsiz, | |
11616 | const void *aarch_hw_watch, | |
11617 | int size) | |
11618 | { | |
11619 | char *note_name = "LINUX"; | |
11620 | return elfcore_write_note (abfd, buf, bufsiz, | |
11621 | note_name, NT_ARM_HW_WATCH, aarch_hw_watch, size); | |
11622 | } | |
11623 | ||
ad1cc4e4 AH |
11624 | char * |
11625 | elfcore_write_aarch_sve (bfd *abfd, | |
11626 | char *buf, | |
11627 | int *bufsiz, | |
11628 | const void *aarch_sve, | |
11629 | int size) | |
11630 | { | |
11631 | char *note_name = "LINUX"; | |
11632 | return elfcore_write_note (abfd, buf, bufsiz, | |
11633 | note_name, NT_ARM_SVE, aarch_sve, size); | |
11634 | } | |
11635 | ||
e6c3b5bf AH |
11636 | char * |
11637 | elfcore_write_aarch_pauth (bfd *abfd, | |
11638 | char *buf, | |
11639 | int *bufsiz, | |
11640 | const void *aarch_pauth, | |
11641 | int size) | |
11642 | { | |
11643 | char *note_name = "LINUX"; | |
11644 | return elfcore_write_note (abfd, buf, bufsiz, | |
11645 | note_name, NT_ARM_PAC_MASK, aarch_pauth, size); | |
11646 | } | |
11647 | ||
bb864ac1 CES |
11648 | char * |
11649 | elfcore_write_register_note (bfd *abfd, | |
11650 | char *buf, | |
11651 | int *bufsiz, | |
11652 | const char *section, | |
11653 | const void *data, | |
11654 | int size) | |
11655 | { | |
11656 | if (strcmp (section, ".reg2") == 0) | |
11657 | return elfcore_write_prfpreg (abfd, buf, bufsiz, data, size); | |
11658 | if (strcmp (section, ".reg-xfp") == 0) | |
11659 | return elfcore_write_prxfpreg (abfd, buf, bufsiz, data, size); | |
4339cae0 L |
11660 | if (strcmp (section, ".reg-xstate") == 0) |
11661 | return elfcore_write_xstatereg (abfd, buf, bufsiz, data, size); | |
bb864ac1 CES |
11662 | if (strcmp (section, ".reg-ppc-vmx") == 0) |
11663 | return elfcore_write_ppc_vmx (abfd, buf, bufsiz, data, size); | |
89eeb0bc LM |
11664 | if (strcmp (section, ".reg-ppc-vsx") == 0) |
11665 | return elfcore_write_ppc_vsx (abfd, buf, bufsiz, data, size); | |
cb2366c1 EBM |
11666 | if (strcmp (section, ".reg-ppc-tar") == 0) |
11667 | return elfcore_write_ppc_tar (abfd, buf, bufsiz, data, size); | |
11668 | if (strcmp (section, ".reg-ppc-ppr") == 0) | |
11669 | return elfcore_write_ppc_ppr (abfd, buf, bufsiz, data, size); | |
11670 | if (strcmp (section, ".reg-ppc-dscr") == 0) | |
11671 | return elfcore_write_ppc_dscr (abfd, buf, bufsiz, data, size); | |
11672 | if (strcmp (section, ".reg-ppc-ebb") == 0) | |
11673 | return elfcore_write_ppc_ebb (abfd, buf, bufsiz, data, size); | |
11674 | if (strcmp (section, ".reg-ppc-pmu") == 0) | |
11675 | return elfcore_write_ppc_pmu (abfd, buf, bufsiz, data, size); | |
11676 | if (strcmp (section, ".reg-ppc-tm-cgpr") == 0) | |
11677 | return elfcore_write_ppc_tm_cgpr (abfd, buf, bufsiz, data, size); | |
11678 | if (strcmp (section, ".reg-ppc-tm-cfpr") == 0) | |
11679 | return elfcore_write_ppc_tm_cfpr (abfd, buf, bufsiz, data, size); | |
11680 | if (strcmp (section, ".reg-ppc-tm-cvmx") == 0) | |
11681 | return elfcore_write_ppc_tm_cvmx (abfd, buf, bufsiz, data, size); | |
11682 | if (strcmp (section, ".reg-ppc-tm-cvsx") == 0) | |
11683 | return elfcore_write_ppc_tm_cvsx (abfd, buf, bufsiz, data, size); | |
11684 | if (strcmp (section, ".reg-ppc-tm-spr") == 0) | |
11685 | return elfcore_write_ppc_tm_spr (abfd, buf, bufsiz, data, size); | |
11686 | if (strcmp (section, ".reg-ppc-tm-ctar") == 0) | |
11687 | return elfcore_write_ppc_tm_ctar (abfd, buf, bufsiz, data, size); | |
11688 | if (strcmp (section, ".reg-ppc-tm-cppr") == 0) | |
11689 | return elfcore_write_ppc_tm_cppr (abfd, buf, bufsiz, data, size); | |
11690 | if (strcmp (section, ".reg-ppc-tm-cdscr") == 0) | |
11691 | return elfcore_write_ppc_tm_cdscr (abfd, buf, bufsiz, data, size); | |
0675e188 UW |
11692 | if (strcmp (section, ".reg-s390-high-gprs") == 0) |
11693 | return elfcore_write_s390_high_gprs (abfd, buf, bufsiz, data, size); | |
d7eeb400 MS |
11694 | if (strcmp (section, ".reg-s390-timer") == 0) |
11695 | return elfcore_write_s390_timer (abfd, buf, bufsiz, data, size); | |
11696 | if (strcmp (section, ".reg-s390-todcmp") == 0) | |
11697 | return elfcore_write_s390_todcmp (abfd, buf, bufsiz, data, size); | |
11698 | if (strcmp (section, ".reg-s390-todpreg") == 0) | |
11699 | return elfcore_write_s390_todpreg (abfd, buf, bufsiz, data, size); | |
11700 | if (strcmp (section, ".reg-s390-ctrs") == 0) | |
11701 | return elfcore_write_s390_ctrs (abfd, buf, bufsiz, data, size); | |
11702 | if (strcmp (section, ".reg-s390-prefix") == 0) | |
11703 | return elfcore_write_s390_prefix (abfd, buf, bufsiz, data, size); | |
355b81d9 UW |
11704 | if (strcmp (section, ".reg-s390-last-break") == 0) |
11705 | return elfcore_write_s390_last_break (abfd, buf, bufsiz, data, size); | |
11706 | if (strcmp (section, ".reg-s390-system-call") == 0) | |
11707 | return elfcore_write_s390_system_call (abfd, buf, bufsiz, data, size); | |
abb3f6cc NC |
11708 | if (strcmp (section, ".reg-s390-tdb") == 0) |
11709 | return elfcore_write_s390_tdb (abfd, buf, bufsiz, data, size); | |
4ef9f41a AA |
11710 | if (strcmp (section, ".reg-s390-vxrs-low") == 0) |
11711 | return elfcore_write_s390_vxrs_low (abfd, buf, bufsiz, data, size); | |
11712 | if (strcmp (section, ".reg-s390-vxrs-high") == 0) | |
11713 | return elfcore_write_s390_vxrs_high (abfd, buf, bufsiz, data, size); | |
88ab90e8 AA |
11714 | if (strcmp (section, ".reg-s390-gs-cb") == 0) |
11715 | return elfcore_write_s390_gs_cb (abfd, buf, bufsiz, data, size); | |
11716 | if (strcmp (section, ".reg-s390-gs-bc") == 0) | |
11717 | return elfcore_write_s390_gs_bc (abfd, buf, bufsiz, data, size); | |
faa9a424 UW |
11718 | if (strcmp (section, ".reg-arm-vfp") == 0) |
11719 | return elfcore_write_arm_vfp (abfd, buf, bufsiz, data, size); | |
652451f8 YZ |
11720 | if (strcmp (section, ".reg-aarch-tls") == 0) |
11721 | return elfcore_write_aarch_tls (abfd, buf, bufsiz, data, size); | |
11722 | if (strcmp (section, ".reg-aarch-hw-break") == 0) | |
11723 | return elfcore_write_aarch_hw_break (abfd, buf, bufsiz, data, size); | |
11724 | if (strcmp (section, ".reg-aarch-hw-watch") == 0) | |
11725 | return elfcore_write_aarch_hw_watch (abfd, buf, bufsiz, data, size); | |
ad1cc4e4 AH |
11726 | if (strcmp (section, ".reg-aarch-sve") == 0) |
11727 | return elfcore_write_aarch_sve (abfd, buf, bufsiz, data, size); | |
e6c3b5bf AH |
11728 | if (strcmp (section, ".reg-aarch-pauth") == 0) |
11729 | return elfcore_write_aarch_pauth (abfd, buf, bufsiz, data, size); | |
bb864ac1 CES |
11730 | return NULL; |
11731 | } | |
11732 | ||
b34976b6 | 11733 | static bfd_boolean |
276da9b3 L |
11734 | elf_parse_notes (bfd *abfd, char *buf, size_t size, file_ptr offset, |
11735 | size_t align) | |
252b5132 | 11736 | { |
c044fabd | 11737 | char *p; |
252b5132 | 11738 | |
276da9b3 L |
11739 | /* NB: CORE PT_NOTE segments may have p_align values of 0 or 1. |
11740 | gABI specifies that PT_NOTE alignment should be aligned to 4 | |
11741 | bytes for 32-bit objects and to 8 bytes for 64-bit objects. If | |
11742 | align is less than 4, we use 4 byte alignment. */ | |
11743 | if (align < 4) | |
11744 | align = 4; | |
ef135d43 NC |
11745 | if (align != 4 && align != 8) |
11746 | return FALSE; | |
276da9b3 | 11747 | |
252b5132 RH |
11748 | p = buf; |
11749 | while (p < buf + size) | |
11750 | { | |
c044fabd | 11751 | Elf_External_Note *xnp = (Elf_External_Note *) p; |
252b5132 RH |
11752 | Elf_Internal_Note in; |
11753 | ||
baea7ef1 AM |
11754 | if (offsetof (Elf_External_Note, name) > buf - p + size) |
11755 | return FALSE; | |
11756 | ||
dc810e39 | 11757 | in.type = H_GET_32 (abfd, xnp->type); |
252b5132 | 11758 | |
dc810e39 | 11759 | in.namesz = H_GET_32 (abfd, xnp->namesz); |
252b5132 | 11760 | in.namedata = xnp->name; |
baea7ef1 AM |
11761 | if (in.namesz > buf - in.namedata + size) |
11762 | return FALSE; | |
252b5132 | 11763 | |
dc810e39 | 11764 | in.descsz = H_GET_32 (abfd, xnp->descsz); |
276da9b3 | 11765 | in.descdata = p + ELF_NOTE_DESC_OFFSET (in.namesz, align); |
252b5132 | 11766 | in.descpos = offset + (in.descdata - buf); |
baea7ef1 AM |
11767 | if (in.descsz != 0 |
11768 | && (in.descdata >= buf + size | |
11769 | || in.descsz > buf - in.descdata + size)) | |
11770 | return FALSE; | |
252b5132 | 11771 | |
718175fa | 11772 | switch (bfd_get_format (abfd)) |
07d6d2b8 | 11773 | { |
718175fa JK |
11774 | default: |
11775 | return TRUE; | |
11776 | ||
11777 | case bfd_core: | |
f64e188b | 11778 | { |
8acbedd6 | 11779 | #define GROKER_ELEMENT(S,F) {S, sizeof (S) - 1, F} |
f64e188b | 11780 | struct |
718175fa | 11781 | { |
f64e188b | 11782 | const char * string; |
8acbedd6 | 11783 | size_t len; |
f64e188b | 11784 | bfd_boolean (* func)(bfd *, Elf_Internal_Note *); |
718175fa | 11785 | } |
f64e188b | 11786 | grokers[] = |
b15fa79e | 11787 | { |
8acbedd6 | 11788 | GROKER_ELEMENT ("", elfcore_grok_note), |
aa1ed4a9 | 11789 | GROKER_ELEMENT ("FreeBSD", elfcore_grok_freebsd_note), |
8acbedd6 KS |
11790 | GROKER_ELEMENT ("NetBSD-CORE", elfcore_grok_netbsd_note), |
11791 | GROKER_ELEMENT ( "OpenBSD", elfcore_grok_openbsd_note), | |
11792 | GROKER_ELEMENT ("QNX", elfcore_grok_nto_note), | |
11793 | GROKER_ELEMENT ("SPU/", elfcore_grok_spu_note) | |
f64e188b | 11794 | }; |
8acbedd6 | 11795 | #undef GROKER_ELEMENT |
f64e188b NC |
11796 | int i; |
11797 | ||
11798 | for (i = ARRAY_SIZE (grokers); i--;) | |
8acbedd6 KS |
11799 | { |
11800 | if (in.namesz >= grokers[i].len | |
11801 | && strncmp (in.namedata, grokers[i].string, | |
11802 | grokers[i].len) == 0) | |
11803 | { | |
11804 | if (! grokers[i].func (abfd, & in)) | |
11805 | return FALSE; | |
11806 | break; | |
11807 | } | |
11808 | } | |
f64e188b NC |
11809 | break; |
11810 | } | |
718175fa JK |
11811 | |
11812 | case bfd_object: | |
11813 | if (in.namesz == sizeof "GNU" && strcmp (in.namedata, "GNU") == 0) | |
11814 | { | |
11815 | if (! elfobj_grok_gnu_note (abfd, &in)) | |
11816 | return FALSE; | |
11817 | } | |
e21e5835 NC |
11818 | else if (in.namesz == sizeof "stapsdt" |
11819 | && strcmp (in.namedata, "stapsdt") == 0) | |
11820 | { | |
11821 | if (! elfobj_grok_stapsdt_note (abfd, &in)) | |
11822 | return FALSE; | |
11823 | } | |
718175fa | 11824 | break; |
08a40648 | 11825 | } |
252b5132 | 11826 | |
276da9b3 | 11827 | p += ELF_NOTE_NEXT_OFFSET (in.namesz, in.descsz, align); |
252b5132 RH |
11828 | } |
11829 | ||
718175fa JK |
11830 | return TRUE; |
11831 | } | |
11832 | ||
11833 | static bfd_boolean | |
276da9b3 L |
11834 | elf_read_notes (bfd *abfd, file_ptr offset, bfd_size_type size, |
11835 | size_t align) | |
718175fa JK |
11836 | { |
11837 | char *buf; | |
11838 | ||
957e1fc1 | 11839 | if (size == 0 || (size + 1) == 0) |
718175fa JK |
11840 | return TRUE; |
11841 | ||
11842 | if (bfd_seek (abfd, offset, SEEK_SET) != 0) | |
11843 | return FALSE; | |
11844 | ||
f64e188b | 11845 | buf = (char *) bfd_malloc (size + 1); |
718175fa JK |
11846 | if (buf == NULL) |
11847 | return FALSE; | |
11848 | ||
f64e188b NC |
11849 | /* PR 17512: file: ec08f814 |
11850 | 0-termintate the buffer so that string searches will not overflow. */ | |
11851 | buf[size] = 0; | |
11852 | ||
718175fa | 11853 | if (bfd_bread (buf, size, abfd) != size |
276da9b3 | 11854 | || !elf_parse_notes (abfd, buf, size, offset, align)) |
718175fa JK |
11855 | { |
11856 | free (buf); | |
11857 | return FALSE; | |
11858 | } | |
11859 | ||
252b5132 | 11860 | free (buf); |
b34976b6 | 11861 | return TRUE; |
252b5132 | 11862 | } |
98d8431c JB |
11863 | \f |
11864 | /* Providing external access to the ELF program header table. */ | |
11865 | ||
11866 | /* Return an upper bound on the number of bytes required to store a | |
11867 | copy of ABFD's program header table entries. Return -1 if an error | |
11868 | occurs; bfd_get_error will return an appropriate code. */ | |
c044fabd | 11869 | |
98d8431c | 11870 | long |
217aa764 | 11871 | bfd_get_elf_phdr_upper_bound (bfd *abfd) |
98d8431c JB |
11872 | { |
11873 | if (abfd->xvec->flavour != bfd_target_elf_flavour) | |
11874 | { | |
11875 | bfd_set_error (bfd_error_wrong_format); | |
11876 | return -1; | |
11877 | } | |
11878 | ||
936e320b | 11879 | return elf_elfheader (abfd)->e_phnum * sizeof (Elf_Internal_Phdr); |
98d8431c JB |
11880 | } |
11881 | ||
98d8431c JB |
11882 | /* Copy ABFD's program header table entries to *PHDRS. The entries |
11883 | will be stored as an array of Elf_Internal_Phdr structures, as | |
11884 | defined in include/elf/internal.h. To find out how large the | |
11885 | buffer needs to be, call bfd_get_elf_phdr_upper_bound. | |
11886 | ||
11887 | Return the number of program header table entries read, or -1 if an | |
11888 | error occurs; bfd_get_error will return an appropriate code. */ | |
c044fabd | 11889 | |
98d8431c | 11890 | int |
217aa764 | 11891 | bfd_get_elf_phdrs (bfd *abfd, void *phdrs) |
98d8431c JB |
11892 | { |
11893 | int num_phdrs; | |
11894 | ||
11895 | if (abfd->xvec->flavour != bfd_target_elf_flavour) | |
11896 | { | |
11897 | bfd_set_error (bfd_error_wrong_format); | |
11898 | return -1; | |
11899 | } | |
11900 | ||
11901 | num_phdrs = elf_elfheader (abfd)->e_phnum; | |
01bcaf63 TT |
11902 | if (num_phdrs != 0) |
11903 | memcpy (phdrs, elf_tdata (abfd)->phdr, | |
11904 | num_phdrs * sizeof (Elf_Internal_Phdr)); | |
98d8431c JB |
11905 | |
11906 | return num_phdrs; | |
11907 | } | |
ae4221d7 | 11908 | |
db6751f2 | 11909 | enum elf_reloc_type_class |
7e612e98 AM |
11910 | _bfd_elf_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED, |
11911 | const asection *rel_sec ATTRIBUTE_UNUSED, | |
11912 | const Elf_Internal_Rela *rela ATTRIBUTE_UNUSED) | |
db6751f2 JJ |
11913 | { |
11914 | return reloc_class_normal; | |
11915 | } | |
f8df10f4 | 11916 | |
47d9a591 | 11917 | /* For RELA architectures, return the relocation value for a |
f8df10f4 JJ |
11918 | relocation against a local symbol. */ |
11919 | ||
11920 | bfd_vma | |
217aa764 AM |
11921 | _bfd_elf_rela_local_sym (bfd *abfd, |
11922 | Elf_Internal_Sym *sym, | |
8517fae7 | 11923 | asection **psec, |
217aa764 | 11924 | Elf_Internal_Rela *rel) |
f8df10f4 | 11925 | { |
8517fae7 | 11926 | asection *sec = *psec; |
f8df10f4 JJ |
11927 | bfd_vma relocation; |
11928 | ||
6835821b AM |
11929 | relocation = (sec->output_section->vma |
11930 | + sec->output_offset | |
11931 | + sym->st_value); | |
f8df10f4 | 11932 | if ((sec->flags & SEC_MERGE) |
c629eae0 | 11933 | && ELF_ST_TYPE (sym->st_info) == STT_SECTION |
dbaa2011 | 11934 | && sec->sec_info_type == SEC_INFO_TYPE_MERGE) |
f8df10f4 | 11935 | { |
f8df10f4 | 11936 | rel->r_addend = |
8517fae7 | 11937 | _bfd_merged_section_offset (abfd, psec, |
65765700 | 11938 | elf_section_data (sec)->sec_info, |
753731ee AM |
11939 | sym->st_value + rel->r_addend); |
11940 | if (sec != *psec) | |
11941 | { | |
11942 | /* If we have changed the section, and our original section is | |
11943 | marked with SEC_EXCLUDE, it means that the original | |
11944 | SEC_MERGE section has been completely subsumed in some | |
11945 | other SEC_MERGE section. In this case, we need to leave | |
11946 | some info around for --emit-relocs. */ | |
11947 | if ((sec->flags & SEC_EXCLUDE) != 0) | |
11948 | sec->kept_section = *psec; | |
11949 | sec = *psec; | |
11950 | } | |
8517fae7 AM |
11951 | rel->r_addend -= relocation; |
11952 | rel->r_addend += sec->output_section->vma + sec->output_offset; | |
f8df10f4 JJ |
11953 | } |
11954 | return relocation; | |
11955 | } | |
c629eae0 JJ |
11956 | |
11957 | bfd_vma | |
217aa764 AM |
11958 | _bfd_elf_rel_local_sym (bfd *abfd, |
11959 | Elf_Internal_Sym *sym, | |
11960 | asection **psec, | |
11961 | bfd_vma addend) | |
47d9a591 | 11962 | { |
c629eae0 JJ |
11963 | asection *sec = *psec; |
11964 | ||
6835821b | 11965 | if (sec->sec_info_type != SEC_INFO_TYPE_MERGE) |
c629eae0 JJ |
11966 | return sym->st_value + addend; |
11967 | ||
11968 | return _bfd_merged_section_offset (abfd, psec, | |
65765700 | 11969 | elf_section_data (sec)->sec_info, |
753731ee | 11970 | sym->st_value + addend); |
c629eae0 JJ |
11971 | } |
11972 | ||
37b01f6a DG |
11973 | /* Adjust an address within a section. Given OFFSET within SEC, return |
11974 | the new offset within the section, based upon changes made to the | |
11975 | section. Returns -1 if the offset is now invalid. | |
11976 | The offset (in abnd out) is in target sized bytes, however big a | |
11977 | byte may be. */ | |
11978 | ||
c629eae0 | 11979 | bfd_vma |
217aa764 | 11980 | _bfd_elf_section_offset (bfd *abfd, |
92e4ec35 | 11981 | struct bfd_link_info *info, |
217aa764 AM |
11982 | asection *sec, |
11983 | bfd_vma offset) | |
c629eae0 | 11984 | { |
68bfbfcc | 11985 | switch (sec->sec_info_type) |
65765700 | 11986 | { |
dbaa2011 | 11987 | case SEC_INFO_TYPE_STABS: |
eea6121a AM |
11988 | return _bfd_stab_section_offset (sec, elf_section_data (sec)->sec_info, |
11989 | offset); | |
dbaa2011 | 11990 | case SEC_INFO_TYPE_EH_FRAME: |
92e4ec35 | 11991 | return _bfd_elf_eh_frame_section_offset (abfd, info, sec, offset); |
37b01f6a | 11992 | |
65765700 | 11993 | default: |
310fd250 L |
11994 | if ((sec->flags & SEC_ELF_REVERSE_COPY) != 0) |
11995 | { | |
37b01f6a | 11996 | /* Reverse the offset. */ |
310fd250 L |
11997 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
11998 | bfd_size_type address_size = bed->s->arch_size / 8; | |
37b01f6a DG |
11999 | |
12000 | /* address_size and sec->size are in octets. Convert | |
12001 | to bytes before subtracting the original offset. */ | |
12002 | offset = (sec->size - address_size) / bfd_octets_per_byte (abfd) - offset; | |
310fd250 | 12003 | } |
65765700 JJ |
12004 | return offset; |
12005 | } | |
c629eae0 | 12006 | } |
3333a7c3 RM |
12007 | \f |
12008 | /* Create a new BFD as if by bfd_openr. Rather than opening a file, | |
12009 | reconstruct an ELF file by reading the segments out of remote memory | |
12010 | based on the ELF file header at EHDR_VMA and the ELF program headers it | |
12011 | points to. If not null, *LOADBASEP is filled in with the difference | |
12012 | between the VMAs from which the segments were read, and the VMAs the | |
12013 | file headers (and hence BFD's idea of each section's VMA) put them at. | |
12014 | ||
12015 | The function TARGET_READ_MEMORY is called to copy LEN bytes from the | |
12016 | remote memory at target address VMA into the local buffer at MYADDR; it | |
12017 | should return zero on success or an `errno' code on failure. TEMPL must | |
12018 | be a BFD for an ELF target with the word size and byte order found in | |
12019 | the remote memory. */ | |
12020 | ||
12021 | bfd * | |
217aa764 AM |
12022 | bfd_elf_bfd_from_remote_memory |
12023 | (bfd *templ, | |
12024 | bfd_vma ehdr_vma, | |
f0a5d95a | 12025 | bfd_size_type size, |
217aa764 | 12026 | bfd_vma *loadbasep, |
fe78531d | 12027 | int (*target_read_memory) (bfd_vma, bfd_byte *, bfd_size_type)) |
3333a7c3 RM |
12028 | { |
12029 | return (*get_elf_backend_data (templ)->elf_backend_bfd_from_remote_memory) | |
5979d6b6 | 12030 | (templ, ehdr_vma, size, loadbasep, target_read_memory); |
3333a7c3 | 12031 | } |
4c45e5c9 JJ |
12032 | \f |
12033 | long | |
c9727e01 AM |
12034 | _bfd_elf_get_synthetic_symtab (bfd *abfd, |
12035 | long symcount ATTRIBUTE_UNUSED, | |
12036 | asymbol **syms ATTRIBUTE_UNUSED, | |
8615f3f2 | 12037 | long dynsymcount, |
c9727e01 AM |
12038 | asymbol **dynsyms, |
12039 | asymbol **ret) | |
4c45e5c9 JJ |
12040 | { |
12041 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
12042 | asection *relplt; | |
12043 | asymbol *s; | |
12044 | const char *relplt_name; | |
12045 | bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean); | |
12046 | arelent *p; | |
12047 | long count, i, n; | |
12048 | size_t size; | |
12049 | Elf_Internal_Shdr *hdr; | |
12050 | char *names; | |
12051 | asection *plt; | |
12052 | ||
8615f3f2 AM |
12053 | *ret = NULL; |
12054 | ||
90e3cdf2 JJ |
12055 | if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0) |
12056 | return 0; | |
12057 | ||
8615f3f2 AM |
12058 | if (dynsymcount <= 0) |
12059 | return 0; | |
12060 | ||
4c45e5c9 JJ |
12061 | if (!bed->plt_sym_val) |
12062 | return 0; | |
12063 | ||
12064 | relplt_name = bed->relplt_name; | |
12065 | if (relplt_name == NULL) | |
d35fd659 | 12066 | relplt_name = bed->rela_plts_and_copies_p ? ".rela.plt" : ".rel.plt"; |
4c45e5c9 JJ |
12067 | relplt = bfd_get_section_by_name (abfd, relplt_name); |
12068 | if (relplt == NULL) | |
12069 | return 0; | |
12070 | ||
12071 | hdr = &elf_section_data (relplt)->this_hdr; | |
12072 | if (hdr->sh_link != elf_dynsymtab (abfd) | |
12073 | || (hdr->sh_type != SHT_REL && hdr->sh_type != SHT_RELA)) | |
12074 | return 0; | |
12075 | ||
12076 | plt = bfd_get_section_by_name (abfd, ".plt"); | |
12077 | if (plt == NULL) | |
12078 | return 0; | |
12079 | ||
12080 | slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table; | |
c9727e01 | 12081 | if (! (*slurp_relocs) (abfd, relplt, dynsyms, TRUE)) |
4c45e5c9 JJ |
12082 | return -1; |
12083 | ||
eea6121a | 12084 | count = relplt->size / hdr->sh_entsize; |
4c45e5c9 JJ |
12085 | size = count * sizeof (asymbol); |
12086 | p = relplt->relocation; | |
cb53bf42 | 12087 | for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel) |
041de40d AM |
12088 | { |
12089 | size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt"); | |
12090 | if (p->addend != 0) | |
12091 | { | |
12092 | #ifdef BFD64 | |
12093 | size += sizeof ("+0x") - 1 + 8 + 8 * (bed->s->elfclass == ELFCLASS64); | |
12094 | #else | |
12095 | size += sizeof ("+0x") - 1 + 8; | |
12096 | #endif | |
12097 | } | |
12098 | } | |
4c45e5c9 | 12099 | |
a50b1753 | 12100 | s = *ret = (asymbol *) bfd_malloc (size); |
4c45e5c9 JJ |
12101 | if (s == NULL) |
12102 | return -1; | |
12103 | ||
12104 | names = (char *) (s + count); | |
12105 | p = relplt->relocation; | |
12106 | n = 0; | |
cb53bf42 | 12107 | for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel) |
4c45e5c9 JJ |
12108 | { |
12109 | size_t len; | |
12110 | bfd_vma addr; | |
12111 | ||
12112 | addr = bed->plt_sym_val (i, plt, p); | |
12113 | if (addr == (bfd_vma) -1) | |
12114 | continue; | |
12115 | ||
12116 | *s = **p->sym_ptr_ptr; | |
65a7a66f AM |
12117 | /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since |
12118 | we are defining a symbol, ensure one of them is set. */ | |
12119 | if ((s->flags & BSF_LOCAL) == 0) | |
12120 | s->flags |= BSF_GLOBAL; | |
6ba2a415 | 12121 | s->flags |= BSF_SYNTHETIC; |
4c45e5c9 JJ |
12122 | s->section = plt; |
12123 | s->value = addr - plt->vma; | |
12124 | s->name = names; | |
8f39ba8e | 12125 | s->udata.p = NULL; |
4c45e5c9 JJ |
12126 | len = strlen ((*p->sym_ptr_ptr)->name); |
12127 | memcpy (names, (*p->sym_ptr_ptr)->name, len); | |
12128 | names += len; | |
041de40d AM |
12129 | if (p->addend != 0) |
12130 | { | |
1d770845 | 12131 | char buf[30], *a; |
d324f6d6 | 12132 | |
041de40d AM |
12133 | memcpy (names, "+0x", sizeof ("+0x") - 1); |
12134 | names += sizeof ("+0x") - 1; | |
1d770845 L |
12135 | bfd_sprintf_vma (abfd, buf, p->addend); |
12136 | for (a = buf; *a == '0'; ++a) | |
12137 | ; | |
12138 | len = strlen (a); | |
12139 | memcpy (names, a, len); | |
12140 | names += len; | |
041de40d | 12141 | } |
4c45e5c9 JJ |
12142 | memcpy (names, "@plt", sizeof ("@plt")); |
12143 | names += sizeof ("@plt"); | |
8f39ba8e | 12144 | ++s, ++n; |
4c45e5c9 JJ |
12145 | } |
12146 | ||
12147 | return n; | |
12148 | } | |
3d7f7666 | 12149 | |
821e6ff6 AM |
12150 | /* It is only used by x86-64 so far. |
12151 | ??? This repeats *COM* id of zero. sec->id is supposed to be unique, | |
7eacd66b AM |
12152 | but current usage would allow all of _bfd_std_section to be zero. */ |
12153 | static const asymbol lcomm_sym | |
12154 | = GLOBAL_SYM_INIT ("LARGE_COMMON", &_bfd_elf_large_com_section); | |
3b22753a | 12155 | asection _bfd_elf_large_com_section |
7eacd66b | 12156 | = BFD_FAKE_SECTION (_bfd_elf_large_com_section, &lcomm_sym, |
821e6ff6 | 12157 | "LARGE_COMMON", 0, SEC_IS_COMMON); |
ecca9871 | 12158 | |
d1036acb | 12159 | void |
06f44071 AM |
12160 | _bfd_elf_post_process_headers (bfd *abfd ATTRIBUTE_UNUSED, |
12161 | struct bfd_link_info *info ATTRIBUTE_UNUSED) | |
d1036acb | 12162 | { |
06f44071 AM |
12163 | } |
12164 | ||
cc364be6 AM |
12165 | bfd_boolean |
12166 | _bfd_elf_final_write_processing (bfd *abfd) | |
06f44071 AM |
12167 | { |
12168 | Elf_Internal_Ehdr *i_ehdrp; /* ELF file header, internal form. */ | |
d1036acb L |
12169 | |
12170 | i_ehdrp = elf_elfheader (abfd); | |
12171 | ||
06f44071 AM |
12172 | if (i_ehdrp->e_ident[EI_OSABI] == ELFOSABI_NONE) |
12173 | i_ehdrp->e_ident[EI_OSABI] = get_elf_backend_data (abfd)->elf_osabi; | |
d8045f23 | 12174 | |
df3a023b AM |
12175 | /* Set the osabi field to ELFOSABI_GNU if the binary contains |
12176 | SHF_GNU_MBIND sections or symbols of STT_GNU_IFUNC type or | |
12177 | STB_GNU_UNIQUE binding. */ | |
cc364be6 AM |
12178 | if (elf_tdata (abfd)->has_gnu_osabi != 0) |
12179 | { | |
12180 | if (i_ehdrp->e_ident[EI_OSABI] == ELFOSABI_NONE) | |
12181 | i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_GNU; | |
12182 | else if (i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_GNU | |
12183 | && i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_FREEBSD) | |
12184 | { | |
12185 | if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_mbind) | |
12186 | _bfd_error_handler (_("GNU_MBIND section is unsupported")); | |
12187 | if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_ifunc) | |
12188 | _bfd_error_handler (_("symbol type STT_GNU_IFUNC is unsupported")); | |
12189 | if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_unique) | |
12190 | _bfd_error_handler (_("symbol binding STB_GNU_UNIQUE is unsupported")); | |
12191 | bfd_set_error (bfd_error_bad_value); | |
12192 | return FALSE; | |
12193 | } | |
12194 | } | |
12195 | return TRUE; | |
d1036acb | 12196 | } |
fcb93ecf PB |
12197 | |
12198 | ||
12199 | /* Return TRUE for ELF symbol types that represent functions. | |
12200 | This is the default version of this function, which is sufficient for | |
d8045f23 | 12201 | most targets. It returns true if TYPE is STT_FUNC or STT_GNU_IFUNC. */ |
fcb93ecf PB |
12202 | |
12203 | bfd_boolean | |
12204 | _bfd_elf_is_function_type (unsigned int type) | |
12205 | { | |
d8045f23 NC |
12206 | return (type == STT_FUNC |
12207 | || type == STT_GNU_IFUNC); | |
fcb93ecf | 12208 | } |
9f296da3 | 12209 | |
aef36ac1 AM |
12210 | /* If the ELF symbol SYM might be a function in SEC, return the |
12211 | function size and set *CODE_OFF to the function's entry point, | |
12212 | otherwise return zero. */ | |
9f296da3 | 12213 | |
aef36ac1 AM |
12214 | bfd_size_type |
12215 | _bfd_elf_maybe_function_sym (const asymbol *sym, asection *sec, | |
12216 | bfd_vma *code_off) | |
9f296da3 | 12217 | { |
aef36ac1 AM |
12218 | bfd_size_type size; |
12219 | ||
ff9e0f5b | 12220 | if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT |
aef36ac1 AM |
12221 | | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0 |
12222 | || sym->section != sec) | |
12223 | return 0; | |
ff9e0f5b | 12224 | |
ff9e0f5b | 12225 | *code_off = sym->value; |
aef36ac1 AM |
12226 | size = 0; |
12227 | if (!(sym->flags & BSF_SYNTHETIC)) | |
12228 | size = ((elf_symbol_type *) sym)->internal_elf_sym.st_size; | |
12229 | if (size == 0) | |
12230 | size = 1; | |
12231 | return size; | |
9f296da3 | 12232 | } |