Commit | Line | Data |
---|---|---|
252b5132 | 1 | /* ELF executable support for BFD. |
340b6d91 | 2 | |
219d1afa | 3 | Copyright (C) 1993-2018 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" |
3db64b00 | 38 | #include "bfd.h" |
252b5132 RH |
39 | #include "bfdlink.h" |
40 | #include "libbfd.h" | |
41 | #define ARCH_SIZE 0 | |
42 | #include "elf-bfd.h" | |
e0e8c97f | 43 | #include "libiberty.h" |
ff59fc36 | 44 | #include "safe-ctype.h" |
de64ce13 | 45 | #include "elf-linux-core.h" |
252b5132 | 46 | |
8bc7f138 L |
47 | #ifdef CORE_HEADER |
48 | #include CORE_HEADER | |
49 | #endif | |
50 | ||
217aa764 | 51 | static int elf_sort_sections (const void *, const void *); |
c84fca4d | 52 | static bfd_boolean assign_file_positions_except_relocs (bfd *, struct bfd_link_info *); |
217aa764 | 53 | static bfd_boolean prep_headers (bfd *); |
ef10c3ac | 54 | static bfd_boolean swap_out_syms (bfd *, struct elf_strtab_hash **, int) ; |
276da9b3 L |
55 | static bfd_boolean elf_read_notes (bfd *, file_ptr, bfd_size_type, |
56 | size_t align) ; | |
718175fa | 57 | static bfd_boolean elf_parse_notes (bfd *abfd, char *buf, size_t size, |
276da9b3 | 58 | file_ptr offset, size_t align); |
50b2bdb7 | 59 | |
252b5132 RH |
60 | /* Swap version information in and out. The version information is |
61 | currently size independent. If that ever changes, this code will | |
62 | need to move into elfcode.h. */ | |
63 | ||
64 | /* Swap in a Verdef structure. */ | |
65 | ||
66 | void | |
217aa764 AM |
67 | _bfd_elf_swap_verdef_in (bfd *abfd, |
68 | const Elf_External_Verdef *src, | |
69 | Elf_Internal_Verdef *dst) | |
252b5132 | 70 | { |
dc810e39 AM |
71 | dst->vd_version = H_GET_16 (abfd, src->vd_version); |
72 | dst->vd_flags = H_GET_16 (abfd, src->vd_flags); | |
73 | dst->vd_ndx = H_GET_16 (abfd, src->vd_ndx); | |
74 | dst->vd_cnt = H_GET_16 (abfd, src->vd_cnt); | |
75 | dst->vd_hash = H_GET_32 (abfd, src->vd_hash); | |
76 | dst->vd_aux = H_GET_32 (abfd, src->vd_aux); | |
77 | dst->vd_next = H_GET_32 (abfd, src->vd_next); | |
252b5132 RH |
78 | } |
79 | ||
80 | /* Swap out a Verdef structure. */ | |
81 | ||
82 | void | |
217aa764 AM |
83 | _bfd_elf_swap_verdef_out (bfd *abfd, |
84 | const Elf_Internal_Verdef *src, | |
85 | Elf_External_Verdef *dst) | |
252b5132 | 86 | { |
dc810e39 AM |
87 | H_PUT_16 (abfd, src->vd_version, dst->vd_version); |
88 | H_PUT_16 (abfd, src->vd_flags, dst->vd_flags); | |
89 | H_PUT_16 (abfd, src->vd_ndx, dst->vd_ndx); | |
90 | H_PUT_16 (abfd, src->vd_cnt, dst->vd_cnt); | |
91 | H_PUT_32 (abfd, src->vd_hash, dst->vd_hash); | |
92 | H_PUT_32 (abfd, src->vd_aux, dst->vd_aux); | |
93 | H_PUT_32 (abfd, src->vd_next, dst->vd_next); | |
252b5132 RH |
94 | } |
95 | ||
96 | /* Swap in a Verdaux structure. */ | |
97 | ||
98 | void | |
217aa764 AM |
99 | _bfd_elf_swap_verdaux_in (bfd *abfd, |
100 | const Elf_External_Verdaux *src, | |
101 | Elf_Internal_Verdaux *dst) | |
252b5132 | 102 | { |
dc810e39 AM |
103 | dst->vda_name = H_GET_32 (abfd, src->vda_name); |
104 | dst->vda_next = H_GET_32 (abfd, src->vda_next); | |
252b5132 RH |
105 | } |
106 | ||
107 | /* Swap out a Verdaux structure. */ | |
108 | ||
109 | void | |
217aa764 AM |
110 | _bfd_elf_swap_verdaux_out (bfd *abfd, |
111 | const Elf_Internal_Verdaux *src, | |
112 | Elf_External_Verdaux *dst) | |
252b5132 | 113 | { |
dc810e39 AM |
114 | H_PUT_32 (abfd, src->vda_name, dst->vda_name); |
115 | H_PUT_32 (abfd, src->vda_next, dst->vda_next); | |
252b5132 RH |
116 | } |
117 | ||
118 | /* Swap in a Verneed structure. */ | |
119 | ||
120 | void | |
217aa764 AM |
121 | _bfd_elf_swap_verneed_in (bfd *abfd, |
122 | const Elf_External_Verneed *src, | |
123 | Elf_Internal_Verneed *dst) | |
252b5132 | 124 | { |
dc810e39 AM |
125 | dst->vn_version = H_GET_16 (abfd, src->vn_version); |
126 | dst->vn_cnt = H_GET_16 (abfd, src->vn_cnt); | |
127 | dst->vn_file = H_GET_32 (abfd, src->vn_file); | |
128 | dst->vn_aux = H_GET_32 (abfd, src->vn_aux); | |
129 | dst->vn_next = H_GET_32 (abfd, src->vn_next); | |
252b5132 RH |
130 | } |
131 | ||
132 | /* Swap out a Verneed structure. */ | |
133 | ||
134 | void | |
217aa764 AM |
135 | _bfd_elf_swap_verneed_out (bfd *abfd, |
136 | const Elf_Internal_Verneed *src, | |
137 | Elf_External_Verneed *dst) | |
252b5132 | 138 | { |
dc810e39 AM |
139 | H_PUT_16 (abfd, src->vn_version, dst->vn_version); |
140 | H_PUT_16 (abfd, src->vn_cnt, dst->vn_cnt); | |
141 | H_PUT_32 (abfd, src->vn_file, dst->vn_file); | |
142 | H_PUT_32 (abfd, src->vn_aux, dst->vn_aux); | |
143 | H_PUT_32 (abfd, src->vn_next, dst->vn_next); | |
252b5132 RH |
144 | } |
145 | ||
146 | /* Swap in a Vernaux structure. */ | |
147 | ||
148 | void | |
217aa764 AM |
149 | _bfd_elf_swap_vernaux_in (bfd *abfd, |
150 | const Elf_External_Vernaux *src, | |
151 | Elf_Internal_Vernaux *dst) | |
252b5132 | 152 | { |
dc810e39 AM |
153 | dst->vna_hash = H_GET_32 (abfd, src->vna_hash); |
154 | dst->vna_flags = H_GET_16 (abfd, src->vna_flags); | |
155 | dst->vna_other = H_GET_16 (abfd, src->vna_other); | |
156 | dst->vna_name = H_GET_32 (abfd, src->vna_name); | |
157 | dst->vna_next = H_GET_32 (abfd, src->vna_next); | |
252b5132 RH |
158 | } |
159 | ||
160 | /* Swap out a Vernaux structure. */ | |
161 | ||
162 | void | |
217aa764 AM |
163 | _bfd_elf_swap_vernaux_out (bfd *abfd, |
164 | const Elf_Internal_Vernaux *src, | |
165 | Elf_External_Vernaux *dst) | |
252b5132 | 166 | { |
dc810e39 AM |
167 | H_PUT_32 (abfd, src->vna_hash, dst->vna_hash); |
168 | H_PUT_16 (abfd, src->vna_flags, dst->vna_flags); | |
169 | H_PUT_16 (abfd, src->vna_other, dst->vna_other); | |
170 | H_PUT_32 (abfd, src->vna_name, dst->vna_name); | |
171 | H_PUT_32 (abfd, src->vna_next, dst->vna_next); | |
252b5132 RH |
172 | } |
173 | ||
174 | /* Swap in a Versym structure. */ | |
175 | ||
176 | void | |
217aa764 AM |
177 | _bfd_elf_swap_versym_in (bfd *abfd, |
178 | const Elf_External_Versym *src, | |
179 | Elf_Internal_Versym *dst) | |
252b5132 | 180 | { |
dc810e39 | 181 | dst->vs_vers = H_GET_16 (abfd, src->vs_vers); |
252b5132 RH |
182 | } |
183 | ||
184 | /* Swap out a Versym structure. */ | |
185 | ||
186 | void | |
217aa764 AM |
187 | _bfd_elf_swap_versym_out (bfd *abfd, |
188 | const Elf_Internal_Versym *src, | |
189 | Elf_External_Versym *dst) | |
252b5132 | 190 | { |
dc810e39 | 191 | H_PUT_16 (abfd, src->vs_vers, dst->vs_vers); |
252b5132 RH |
192 | } |
193 | ||
194 | /* Standard ELF hash function. Do not change this function; you will | |
195 | cause invalid hash tables to be generated. */ | |
3a99b017 | 196 | |
252b5132 | 197 | unsigned long |
217aa764 | 198 | bfd_elf_hash (const char *namearg) |
252b5132 | 199 | { |
3a99b017 | 200 | const unsigned char *name = (const unsigned char *) namearg; |
252b5132 RH |
201 | unsigned long h = 0; |
202 | unsigned long g; | |
203 | int ch; | |
204 | ||
205 | while ((ch = *name++) != '\0') | |
206 | { | |
207 | h = (h << 4) + ch; | |
208 | if ((g = (h & 0xf0000000)) != 0) | |
209 | { | |
210 | h ^= g >> 24; | |
211 | /* The ELF ABI says `h &= ~g', but this is equivalent in | |
212 | this case and on some machines one insn instead of two. */ | |
213 | h ^= g; | |
214 | } | |
215 | } | |
32dfa85d | 216 | return h & 0xffffffff; |
252b5132 RH |
217 | } |
218 | ||
fdc90cb4 JJ |
219 | /* DT_GNU_HASH hash function. Do not change this function; you will |
220 | cause invalid hash tables to be generated. */ | |
221 | ||
222 | unsigned long | |
223 | bfd_elf_gnu_hash (const char *namearg) | |
224 | { | |
225 | const unsigned char *name = (const unsigned char *) namearg; | |
226 | unsigned long h = 5381; | |
227 | unsigned char ch; | |
228 | ||
229 | while ((ch = *name++) != '\0') | |
230 | h = (h << 5) + h + ch; | |
231 | return h & 0xffffffff; | |
232 | } | |
233 | ||
0c8d6e5c AM |
234 | /* Create a tdata field OBJECT_SIZE bytes in length, zeroed out and with |
235 | the object_id field of an elf_obj_tdata field set to OBJECT_ID. */ | |
b34976b6 | 236 | bfd_boolean |
0c8d6e5c | 237 | bfd_elf_allocate_object (bfd *abfd, |
0ffa91dd | 238 | size_t object_size, |
4dfe6ac6 | 239 | enum elf_target_id object_id) |
252b5132 | 240 | { |
0ffa91dd NC |
241 | BFD_ASSERT (object_size >= sizeof (struct elf_obj_tdata)); |
242 | abfd->tdata.any = bfd_zalloc (abfd, object_size); | |
243 | if (abfd->tdata.any == NULL) | |
244 | return FALSE; | |
252b5132 | 245 | |
0ffa91dd | 246 | elf_object_id (abfd) = object_id; |
c0355132 AM |
247 | if (abfd->direction != read_direction) |
248 | { | |
249 | struct output_elf_obj_tdata *o = bfd_zalloc (abfd, sizeof *o); | |
250 | if (o == NULL) | |
251 | return FALSE; | |
252 | elf_tdata (abfd)->o = o; | |
253 | elf_program_header_size (abfd) = (bfd_size_type) -1; | |
254 | } | |
b34976b6 | 255 | return TRUE; |
252b5132 RH |
256 | } |
257 | ||
0ffa91dd NC |
258 | |
259 | bfd_boolean | |
ae95ffa6 | 260 | bfd_elf_make_object (bfd *abfd) |
0ffa91dd | 261 | { |
ae95ffa6 | 262 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
0ffa91dd | 263 | return bfd_elf_allocate_object (abfd, sizeof (struct elf_obj_tdata), |
ae95ffa6 | 264 | bed->target_id); |
0ffa91dd NC |
265 | } |
266 | ||
b34976b6 | 267 | bfd_boolean |
217aa764 | 268 | bfd_elf_mkcorefile (bfd *abfd) |
252b5132 | 269 | { |
c044fabd | 270 | /* I think this can be done just like an object file. */ |
228e534f AM |
271 | if (!abfd->xvec->_bfd_set_format[(int) bfd_object] (abfd)) |
272 | return FALSE; | |
273 | elf_tdata (abfd)->core = bfd_zalloc (abfd, sizeof (*elf_tdata (abfd)->core)); | |
274 | return elf_tdata (abfd)->core != NULL; | |
252b5132 RH |
275 | } |
276 | ||
72a80a16 | 277 | static char * |
217aa764 | 278 | bfd_elf_get_str_section (bfd *abfd, unsigned int shindex) |
252b5132 RH |
279 | { |
280 | Elf_Internal_Shdr **i_shdrp; | |
f075ee0c | 281 | bfd_byte *shstrtab = NULL; |
dc810e39 AM |
282 | file_ptr offset; |
283 | bfd_size_type shstrtabsize; | |
252b5132 RH |
284 | |
285 | i_shdrp = elf_elfsections (abfd); | |
74f2e02b AM |
286 | if (i_shdrp == 0 |
287 | || shindex >= elf_numsections (abfd) | |
288 | || i_shdrp[shindex] == 0) | |
f075ee0c | 289 | return NULL; |
252b5132 | 290 | |
f075ee0c | 291 | shstrtab = i_shdrp[shindex]->contents; |
252b5132 RH |
292 | if (shstrtab == NULL) |
293 | { | |
c044fabd | 294 | /* No cached one, attempt to read, and cache what we read. */ |
252b5132 RH |
295 | offset = i_shdrp[shindex]->sh_offset; |
296 | shstrtabsize = i_shdrp[shindex]->sh_size; | |
c6c60d09 JJ |
297 | |
298 | /* Allocate and clear an extra byte at the end, to prevent crashes | |
299 | in case the string table is not terminated. */ | |
3471d59d | 300 | if (shstrtabsize + 1 <= 1 |
95a6d235 | 301 | || shstrtabsize > bfd_get_file_size (abfd) |
06614111 NC |
302 | || bfd_seek (abfd, offset, SEEK_SET) != 0 |
303 | || (shstrtab = (bfd_byte *) bfd_alloc (abfd, shstrtabsize + 1)) == NULL) | |
c6c60d09 JJ |
304 | shstrtab = NULL; |
305 | else if (bfd_bread (shstrtab, shstrtabsize, abfd) != shstrtabsize) | |
306 | { | |
307 | if (bfd_get_error () != bfd_error_system_call) | |
308 | bfd_set_error (bfd_error_file_truncated); | |
06614111 | 309 | bfd_release (abfd, shstrtab); |
c6c60d09 | 310 | shstrtab = NULL; |
3471d59d CC |
311 | /* Once we've failed to read it, make sure we don't keep |
312 | trying. Otherwise, we'll keep allocating space for | |
313 | the string table over and over. */ | |
314 | i_shdrp[shindex]->sh_size = 0; | |
c6c60d09 JJ |
315 | } |
316 | else | |
317 | shstrtab[shstrtabsize] = '\0'; | |
217aa764 | 318 | i_shdrp[shindex]->contents = shstrtab; |
252b5132 | 319 | } |
f075ee0c | 320 | return (char *) shstrtab; |
252b5132 RH |
321 | } |
322 | ||
323 | char * | |
217aa764 AM |
324 | bfd_elf_string_from_elf_section (bfd *abfd, |
325 | unsigned int shindex, | |
326 | unsigned int strindex) | |
252b5132 RH |
327 | { |
328 | Elf_Internal_Shdr *hdr; | |
329 | ||
330 | if (strindex == 0) | |
331 | return ""; | |
332 | ||
74f2e02b AM |
333 | if (elf_elfsections (abfd) == NULL || shindex >= elf_numsections (abfd)) |
334 | return NULL; | |
335 | ||
252b5132 RH |
336 | hdr = elf_elfsections (abfd)[shindex]; |
337 | ||
06614111 NC |
338 | if (hdr->contents == NULL) |
339 | { | |
340 | if (hdr->sh_type != SHT_STRTAB && hdr->sh_type < SHT_LOOS) | |
341 | { | |
342 | /* PR 17512: file: f057ec89. */ | |
695344c0 | 343 | /* xgettext:c-format */ |
871b3ab2 | 344 | _bfd_error_handler (_("%pB: attempt to load strings from" |
63a5468a | 345 | " a non-string section (number %d)"), |
06614111 NC |
346 | abfd, shindex); |
347 | return NULL; | |
348 | } | |
b1fa9dd6 | 349 | |
06614111 NC |
350 | if (bfd_elf_get_str_section (abfd, shindex) == NULL) |
351 | return NULL; | |
352 | } | |
252b5132 RH |
353 | |
354 | if (strindex >= hdr->sh_size) | |
355 | { | |
1b3a8575 | 356 | unsigned int shstrndx = elf_elfheader(abfd)->e_shstrndx; |
4eca0228 | 357 | _bfd_error_handler |
695344c0 | 358 | /* xgettext:c-format */ |
2dcf00ce AM |
359 | (_("%pB: invalid string offset %u >= %" PRIu64 " for section `%s'"), |
360 | abfd, strindex, (uint64_t) hdr->sh_size, | |
1b3a8575 | 361 | (shindex == shstrndx && strindex == hdr->sh_name |
252b5132 | 362 | ? ".shstrtab" |
1b3a8575 | 363 | : bfd_elf_string_from_elf_section (abfd, shstrndx, hdr->sh_name))); |
45b222d6 | 364 | return NULL; |
252b5132 RH |
365 | } |
366 | ||
367 | return ((char *) hdr->contents) + strindex; | |
368 | } | |
369 | ||
6cdc0ccc AM |
370 | /* Read and convert symbols to internal format. |
371 | SYMCOUNT specifies the number of symbols to read, starting from | |
372 | symbol SYMOFFSET. If any of INTSYM_BUF, EXTSYM_BUF or EXTSHNDX_BUF | |
373 | are non-NULL, they are used to store the internal symbols, external | |
b7c368d0 NC |
374 | symbols, and symbol section index extensions, respectively. |
375 | Returns a pointer to the internal symbol buffer (malloced if necessary) | |
376 | or NULL if there were no symbols or some kind of problem. */ | |
6cdc0ccc AM |
377 | |
378 | Elf_Internal_Sym * | |
217aa764 AM |
379 | bfd_elf_get_elf_syms (bfd *ibfd, |
380 | Elf_Internal_Shdr *symtab_hdr, | |
381 | size_t symcount, | |
382 | size_t symoffset, | |
383 | Elf_Internal_Sym *intsym_buf, | |
384 | void *extsym_buf, | |
385 | Elf_External_Sym_Shndx *extshndx_buf) | |
6cdc0ccc AM |
386 | { |
387 | Elf_Internal_Shdr *shndx_hdr; | |
217aa764 | 388 | void *alloc_ext; |
df622259 | 389 | const bfd_byte *esym; |
6cdc0ccc AM |
390 | Elf_External_Sym_Shndx *alloc_extshndx; |
391 | Elf_External_Sym_Shndx *shndx; | |
4dd07732 | 392 | Elf_Internal_Sym *alloc_intsym; |
6cdc0ccc AM |
393 | Elf_Internal_Sym *isym; |
394 | Elf_Internal_Sym *isymend; | |
9c5bfbb7 | 395 | const struct elf_backend_data *bed; |
6cdc0ccc AM |
396 | size_t extsym_size; |
397 | bfd_size_type amt; | |
398 | file_ptr pos; | |
399 | ||
e44a2c9c AM |
400 | if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour) |
401 | abort (); | |
402 | ||
6cdc0ccc AM |
403 | if (symcount == 0) |
404 | return intsym_buf; | |
405 | ||
406 | /* Normal syms might have section extension entries. */ | |
407 | shndx_hdr = NULL; | |
6a40cf0c NC |
408 | if (elf_symtab_shndx_list (ibfd) != NULL) |
409 | { | |
410 | elf_section_list * entry; | |
411 | Elf_Internal_Shdr **sections = elf_elfsections (ibfd); | |
412 | ||
413 | /* Find an index section that is linked to this symtab section. */ | |
414 | for (entry = elf_symtab_shndx_list (ibfd); entry != NULL; entry = entry->next) | |
315350be NC |
415 | { |
416 | /* PR 20063. */ | |
417 | if (entry->hdr.sh_link >= elf_numsections (ibfd)) | |
418 | continue; | |
419 | ||
420 | if (sections[entry->hdr.sh_link] == symtab_hdr) | |
421 | { | |
422 | shndx_hdr = & entry->hdr; | |
423 | break; | |
424 | }; | |
425 | } | |
6a40cf0c NC |
426 | |
427 | if (shndx_hdr == NULL) | |
428 | { | |
429 | if (symtab_hdr == & elf_symtab_hdr (ibfd)) | |
430 | /* Not really accurate, but this was how the old code used to work. */ | |
431 | shndx_hdr = & elf_symtab_shndx_list (ibfd)->hdr; | |
432 | /* Otherwise we do nothing. The assumption is that | |
433 | the index table will not be needed. */ | |
434 | } | |
435 | } | |
6cdc0ccc AM |
436 | |
437 | /* Read the symbols. */ | |
438 | alloc_ext = NULL; | |
439 | alloc_extshndx = NULL; | |
4dd07732 | 440 | alloc_intsym = NULL; |
6cdc0ccc AM |
441 | bed = get_elf_backend_data (ibfd); |
442 | extsym_size = bed->s->sizeof_sym; | |
ef53be89 | 443 | amt = (bfd_size_type) symcount * extsym_size; |
6cdc0ccc AM |
444 | pos = symtab_hdr->sh_offset + symoffset * extsym_size; |
445 | if (extsym_buf == NULL) | |
446 | { | |
d0fb9a8d | 447 | alloc_ext = bfd_malloc2 (symcount, extsym_size); |
6cdc0ccc AM |
448 | extsym_buf = alloc_ext; |
449 | } | |
450 | if (extsym_buf == NULL | |
451 | || bfd_seek (ibfd, pos, SEEK_SET) != 0 | |
452 | || bfd_bread (extsym_buf, amt, ibfd) != amt) | |
453 | { | |
454 | intsym_buf = NULL; | |
455 | goto out; | |
456 | } | |
457 | ||
458 | if (shndx_hdr == NULL || shndx_hdr->sh_size == 0) | |
459 | extshndx_buf = NULL; | |
460 | else | |
461 | { | |
ef53be89 | 462 | amt = (bfd_size_type) symcount * sizeof (Elf_External_Sym_Shndx); |
6cdc0ccc AM |
463 | pos = shndx_hdr->sh_offset + symoffset * sizeof (Elf_External_Sym_Shndx); |
464 | if (extshndx_buf == NULL) | |
465 | { | |
a50b1753 | 466 | alloc_extshndx = (Elf_External_Sym_Shndx *) |
07d6d2b8 | 467 | bfd_malloc2 (symcount, sizeof (Elf_External_Sym_Shndx)); |
6cdc0ccc AM |
468 | extshndx_buf = alloc_extshndx; |
469 | } | |
470 | if (extshndx_buf == NULL | |
471 | || bfd_seek (ibfd, pos, SEEK_SET) != 0 | |
472 | || bfd_bread (extshndx_buf, amt, ibfd) != amt) | |
473 | { | |
474 | intsym_buf = NULL; | |
475 | goto out; | |
476 | } | |
477 | } | |
478 | ||
479 | if (intsym_buf == NULL) | |
480 | { | |
a50b1753 | 481 | alloc_intsym = (Elf_Internal_Sym *) |
07d6d2b8 | 482 | bfd_malloc2 (symcount, sizeof (Elf_Internal_Sym)); |
4dd07732 | 483 | intsym_buf = alloc_intsym; |
6cdc0ccc AM |
484 | if (intsym_buf == NULL) |
485 | goto out; | |
486 | } | |
487 | ||
488 | /* Convert the symbols to internal form. */ | |
489 | isymend = intsym_buf + symcount; | |
a50b1753 | 490 | for (esym = (const bfd_byte *) extsym_buf, isym = intsym_buf, |
07d6d2b8 | 491 | shndx = extshndx_buf; |
6cdc0ccc AM |
492 | isym < isymend; |
493 | esym += extsym_size, isym++, shndx = shndx != NULL ? shndx + 1 : NULL) | |
8384fb8f AM |
494 | if (!(*bed->s->swap_symbol_in) (ibfd, esym, shndx, isym)) |
495 | { | |
496 | symoffset += (esym - (bfd_byte *) extsym_buf) / extsym_size; | |
695344c0 | 497 | /* xgettext:c-format */ |
871b3ab2 | 498 | _bfd_error_handler (_("%pB symbol number %lu references" |
63a5468a | 499 | " nonexistent SHT_SYMTAB_SHNDX section"), |
4eca0228 | 500 | ibfd, (unsigned long) symoffset); |
4dd07732 AM |
501 | if (alloc_intsym != NULL) |
502 | free (alloc_intsym); | |
8384fb8f AM |
503 | intsym_buf = NULL; |
504 | goto out; | |
505 | } | |
6cdc0ccc AM |
506 | |
507 | out: | |
508 | if (alloc_ext != NULL) | |
509 | free (alloc_ext); | |
510 | if (alloc_extshndx != NULL) | |
511 | free (alloc_extshndx); | |
512 | ||
513 | return intsym_buf; | |
514 | } | |
515 | ||
5cab59f6 AM |
516 | /* Look up a symbol name. */ |
517 | const char * | |
be8dd2ca AM |
518 | bfd_elf_sym_name (bfd *abfd, |
519 | Elf_Internal_Shdr *symtab_hdr, | |
26c61ae5 L |
520 | Elf_Internal_Sym *isym, |
521 | asection *sym_sec) | |
5cab59f6 | 522 | { |
26c61ae5 | 523 | const char *name; |
5cab59f6 | 524 | unsigned int iname = isym->st_name; |
be8dd2ca | 525 | unsigned int shindex = symtab_hdr->sh_link; |
26c61ae5 | 526 | |
138f35cc JJ |
527 | if (iname == 0 && ELF_ST_TYPE (isym->st_info) == STT_SECTION |
528 | /* Check for a bogus st_shndx to avoid crashing. */ | |
4fbb74a6 | 529 | && isym->st_shndx < elf_numsections (abfd)) |
5cab59f6 AM |
530 | { |
531 | iname = elf_elfsections (abfd)[isym->st_shndx]->sh_name; | |
532 | shindex = elf_elfheader (abfd)->e_shstrndx; | |
533 | } | |
534 | ||
26c61ae5 L |
535 | name = bfd_elf_string_from_elf_section (abfd, shindex, iname); |
536 | if (name == NULL) | |
537 | name = "(null)"; | |
538 | else if (sym_sec && *name == '\0') | |
539 | name = bfd_section_name (abfd, sym_sec); | |
540 | ||
541 | return name; | |
5cab59f6 AM |
542 | } |
543 | ||
dbb410c3 AM |
544 | /* Elf_Internal_Shdr->contents is an array of these for SHT_GROUP |
545 | sections. The first element is the flags, the rest are section | |
546 | pointers. */ | |
547 | ||
548 | typedef union elf_internal_group { | |
549 | Elf_Internal_Shdr *shdr; | |
550 | unsigned int flags; | |
551 | } Elf_Internal_Group; | |
552 | ||
b885599b AM |
553 | /* Return the name of the group signature symbol. Why isn't the |
554 | signature just a string? */ | |
555 | ||
556 | static const char * | |
217aa764 | 557 | group_signature (bfd *abfd, Elf_Internal_Shdr *ghdr) |
b885599b | 558 | { |
9dce4196 | 559 | Elf_Internal_Shdr *hdr; |
9dce4196 AM |
560 | unsigned char esym[sizeof (Elf64_External_Sym)]; |
561 | Elf_External_Sym_Shndx eshndx; | |
562 | Elf_Internal_Sym isym; | |
b885599b | 563 | |
13792e9d L |
564 | /* First we need to ensure the symbol table is available. Make sure |
565 | that it is a symbol table section. */ | |
4fbb74a6 AM |
566 | if (ghdr->sh_link >= elf_numsections (abfd)) |
567 | return NULL; | |
13792e9d L |
568 | hdr = elf_elfsections (abfd) [ghdr->sh_link]; |
569 | if (hdr->sh_type != SHT_SYMTAB | |
570 | || ! bfd_section_from_shdr (abfd, ghdr->sh_link)) | |
b885599b AM |
571 | return NULL; |
572 | ||
9dce4196 AM |
573 | /* Go read the symbol. */ |
574 | hdr = &elf_tdata (abfd)->symtab_hdr; | |
6cdc0ccc AM |
575 | if (bfd_elf_get_elf_syms (abfd, hdr, 1, ghdr->sh_info, |
576 | &isym, esym, &eshndx) == NULL) | |
b885599b | 577 | return NULL; |
9dce4196 | 578 | |
26c61ae5 | 579 | return bfd_elf_sym_name (abfd, hdr, &isym, NULL); |
b885599b AM |
580 | } |
581 | ||
dbb410c3 AM |
582 | /* Set next_in_group list pointer, and group name for NEWSECT. */ |
583 | ||
b34976b6 | 584 | static bfd_boolean |
217aa764 | 585 | setup_group (bfd *abfd, Elf_Internal_Shdr *hdr, asection *newsect) |
dbb410c3 AM |
586 | { |
587 | unsigned int num_group = elf_tdata (abfd)->num_group; | |
588 | ||
589 | /* If num_group is zero, read in all SHT_GROUP sections. The count | |
590 | is set to -1 if there are no SHT_GROUP sections. */ | |
591 | if (num_group == 0) | |
592 | { | |
593 | unsigned int i, shnum; | |
594 | ||
595 | /* First count the number of groups. If we have a SHT_GROUP | |
596 | section with just a flag word (ie. sh_size is 4), ignore it. */ | |
9ad5cbcf | 597 | shnum = elf_numsections (abfd); |
dbb410c3 | 598 | num_group = 0; |
08a40648 | 599 | |
44534af3 | 600 | #define IS_VALID_GROUP_SECTION_HEADER(shdr, minsize) \ |
1783205a | 601 | ( (shdr)->sh_type == SHT_GROUP \ |
44534af3 | 602 | && (shdr)->sh_size >= minsize \ |
1783205a NC |
603 | && (shdr)->sh_entsize == GRP_ENTRY_SIZE \ |
604 | && ((shdr)->sh_size % GRP_ENTRY_SIZE) == 0) | |
08a40648 | 605 | |
dbb410c3 AM |
606 | for (i = 0; i < shnum; i++) |
607 | { | |
608 | Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i]; | |
1783205a | 609 | |
44534af3 | 610 | if (IS_VALID_GROUP_SECTION_HEADER (shdr, 2 * GRP_ENTRY_SIZE)) |
dbb410c3 AM |
611 | num_group += 1; |
612 | } | |
613 | ||
614 | if (num_group == 0) | |
20dbb49d L |
615 | { |
616 | num_group = (unsigned) -1; | |
617 | elf_tdata (abfd)->num_group = num_group; | |
ce497010 | 618 | elf_tdata (abfd)->group_sect_ptr = NULL; |
20dbb49d L |
619 | } |
620 | else | |
dbb410c3 AM |
621 | { |
622 | /* We keep a list of elf section headers for group sections, | |
623 | so we can find them quickly. */ | |
20dbb49d | 624 | bfd_size_type amt; |
d0fb9a8d | 625 | |
20dbb49d | 626 | elf_tdata (abfd)->num_group = num_group; |
a50b1753 | 627 | elf_tdata (abfd)->group_sect_ptr = (Elf_Internal_Shdr **) |
07d6d2b8 | 628 | bfd_alloc2 (abfd, num_group, sizeof (Elf_Internal_Shdr *)); |
dbb410c3 | 629 | if (elf_tdata (abfd)->group_sect_ptr == NULL) |
b34976b6 | 630 | return FALSE; |
4bba0fb1 AM |
631 | memset (elf_tdata (abfd)->group_sect_ptr, 0, |
632 | num_group * sizeof (Elf_Internal_Shdr *)); | |
dbb410c3 | 633 | num_group = 0; |
ce497010 | 634 | |
dbb410c3 AM |
635 | for (i = 0; i < shnum; i++) |
636 | { | |
637 | Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i]; | |
1783205a | 638 | |
44534af3 | 639 | if (IS_VALID_GROUP_SECTION_HEADER (shdr, 2 * GRP_ENTRY_SIZE)) |
dbb410c3 | 640 | { |
973ffd63 | 641 | unsigned char *src; |
dbb410c3 AM |
642 | Elf_Internal_Group *dest; |
643 | ||
07d6d2b8 AM |
644 | /* Make sure the group section has a BFD section |
645 | attached to it. */ | |
646 | if (!bfd_section_from_shdr (abfd, i)) | |
647 | return FALSE; | |
648 | ||
dbb410c3 AM |
649 | /* Add to list of sections. */ |
650 | elf_tdata (abfd)->group_sect_ptr[num_group] = shdr; | |
651 | num_group += 1; | |
652 | ||
653 | /* Read the raw contents. */ | |
654 | BFD_ASSERT (sizeof (*dest) >= 4); | |
655 | amt = shdr->sh_size * sizeof (*dest) / 4; | |
a50b1753 | 656 | shdr->contents = (unsigned char *) |
07d6d2b8 | 657 | bfd_alloc2 (abfd, shdr->sh_size, sizeof (*dest) / 4); |
1783205a NC |
658 | /* PR binutils/4110: Handle corrupt group headers. */ |
659 | if (shdr->contents == NULL) | |
660 | { | |
661 | _bfd_error_handler | |
695344c0 | 662 | /* xgettext:c-format */ |
871b3ab2 | 663 | (_("%pB: corrupt size field in group section" |
2dcf00ce AM |
664 | " header: %#" PRIx64), |
665 | abfd, (uint64_t) shdr->sh_size); | |
1783205a | 666 | bfd_set_error (bfd_error_bad_value); |
493a3386 NC |
667 | -- num_group; |
668 | continue; | |
1783205a NC |
669 | } |
670 | ||
671 | memset (shdr->contents, 0, amt); | |
672 | ||
673 | if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0 | |
dbb410c3 AM |
674 | || (bfd_bread (shdr->contents, shdr->sh_size, abfd) |
675 | != shdr->sh_size)) | |
493a3386 NC |
676 | { |
677 | _bfd_error_handler | |
695344c0 | 678 | /* xgettext:c-format */ |
871b3ab2 | 679 | (_("%pB: invalid size field in group section" |
2dcf00ce AM |
680 | " header: %#" PRIx64 ""), |
681 | abfd, (uint64_t) shdr->sh_size); | |
493a3386 NC |
682 | bfd_set_error (bfd_error_bad_value); |
683 | -- num_group; | |
63a5468a AM |
684 | /* PR 17510: If the group contents are even |
685 | partially corrupt, do not allow any of the | |
686 | contents to be used. */ | |
493a3386 NC |
687 | memset (shdr->contents, 0, amt); |
688 | continue; | |
689 | } | |
708d7d0d | 690 | |
dbb410c3 AM |
691 | /* Translate raw contents, a flag word followed by an |
692 | array of elf section indices all in target byte order, | |
693 | to the flag word followed by an array of elf section | |
694 | pointers. */ | |
695 | src = shdr->contents + shdr->sh_size; | |
696 | dest = (Elf_Internal_Group *) (shdr->contents + amt); | |
06614111 | 697 | |
dbb410c3 AM |
698 | while (1) |
699 | { | |
700 | unsigned int idx; | |
701 | ||
702 | src -= 4; | |
703 | --dest; | |
704 | idx = H_GET_32 (abfd, src); | |
705 | if (src == shdr->contents) | |
706 | { | |
707 | dest->flags = idx; | |
b885599b AM |
708 | if (shdr->bfd_section != NULL && (idx & GRP_COMDAT)) |
709 | shdr->bfd_section->flags | |
710 | |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD; | |
dbb410c3 AM |
711 | break; |
712 | } | |
4bba0fb1 | 713 | if (idx < shnum) |
bae363f1 L |
714 | { |
715 | dest->shdr = elf_elfsections (abfd)[idx]; | |
716 | /* PR binutils/23199: All sections in a | |
717 | section group should be marked with | |
718 | SHF_GROUP. But some tools generate | |
719 | broken objects without SHF_GROUP. Fix | |
720 | them up here. */ | |
721 | dest->shdr->sh_flags |= SHF_GROUP; | |
722 | } | |
4bba0fb1 AM |
723 | if (idx >= shnum |
724 | || dest->shdr->sh_type == SHT_GROUP) | |
dbb410c3 | 725 | { |
4eca0228 | 726 | _bfd_error_handler |
4bba0fb1 AM |
727 | (_("%pB: invalid entry in SHT_GROUP section [%u]"), |
728 | abfd, i); | |
729 | dest->shdr = NULL; | |
dbb410c3 | 730 | } |
dbb410c3 AM |
731 | } |
732 | } | |
733 | } | |
493a3386 NC |
734 | |
735 | /* PR 17510: Corrupt binaries might contain invalid groups. */ | |
736 | if (num_group != (unsigned) elf_tdata (abfd)->num_group) | |
737 | { | |
738 | elf_tdata (abfd)->num_group = num_group; | |
739 | ||
740 | /* If all groups are invalid then fail. */ | |
741 | if (num_group == 0) | |
742 | { | |
743 | elf_tdata (abfd)->group_sect_ptr = NULL; | |
744 | elf_tdata (abfd)->num_group = num_group = -1; | |
4eca0228 | 745 | _bfd_error_handler |
871b3ab2 | 746 | (_("%pB: no valid group sections found"), abfd); |
493a3386 NC |
747 | bfd_set_error (bfd_error_bad_value); |
748 | } | |
749 | } | |
dbb410c3 AM |
750 | } |
751 | } | |
752 | ||
753 | if (num_group != (unsigned) -1) | |
754 | { | |
564e11c9 JW |
755 | unsigned int search_offset = elf_tdata (abfd)->group_search_offset; |
756 | unsigned int j; | |
dbb410c3 | 757 | |
564e11c9 | 758 | for (j = 0; j < num_group; j++) |
dbb410c3 | 759 | { |
564e11c9 JW |
760 | /* Begin search from previous found group. */ |
761 | unsigned i = (j + search_offset) % num_group; | |
762 | ||
dbb410c3 | 763 | Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i]; |
ce497010 | 764 | Elf_Internal_Group *idx; |
0c54f692 | 765 | bfd_size_type n_elt; |
ce497010 NC |
766 | |
767 | if (shdr == NULL) | |
768 | continue; | |
769 | ||
770 | idx = (Elf_Internal_Group *) shdr->contents; | |
0c54f692 NC |
771 | if (idx == NULL || shdr->sh_size < 4) |
772 | { | |
773 | /* See PR 21957 for a reproducer. */ | |
774 | /* xgettext:c-format */ | |
871b3ab2 | 775 | _bfd_error_handler (_("%pB: group section '%pA' has no contents"), |
0c54f692 NC |
776 | abfd, shdr->bfd_section); |
777 | elf_tdata (abfd)->group_sect_ptr[i] = NULL; | |
778 | bfd_set_error (bfd_error_bad_value); | |
779 | return FALSE; | |
780 | } | |
ce497010 | 781 | n_elt = shdr->sh_size / 4; |
dbb410c3 AM |
782 | |
783 | /* Look through this group's sections to see if current | |
784 | section is a member. */ | |
785 | while (--n_elt != 0) | |
786 | if ((++idx)->shdr == hdr) | |
787 | { | |
e0e8c97f | 788 | asection *s = NULL; |
dbb410c3 AM |
789 | |
790 | /* We are a member of this group. Go looking through | |
791 | other members to see if any others are linked via | |
792 | next_in_group. */ | |
793 | idx = (Elf_Internal_Group *) shdr->contents; | |
794 | n_elt = shdr->sh_size / 4; | |
795 | while (--n_elt != 0) | |
4bba0fb1 AM |
796 | if ((++idx)->shdr != NULL |
797 | && (s = idx->shdr->bfd_section) != NULL | |
945906ff | 798 | && elf_next_in_group (s) != NULL) |
dbb410c3 AM |
799 | break; |
800 | if (n_elt != 0) | |
801 | { | |
dbb410c3 AM |
802 | /* Snarf the group name from other member, and |
803 | insert current section in circular list. */ | |
945906ff AM |
804 | elf_group_name (newsect) = elf_group_name (s); |
805 | elf_next_in_group (newsect) = elf_next_in_group (s); | |
806 | elf_next_in_group (s) = newsect; | |
dbb410c3 AM |
807 | } |
808 | else | |
809 | { | |
dbb410c3 AM |
810 | const char *gname; |
811 | ||
b885599b AM |
812 | gname = group_signature (abfd, shdr); |
813 | if (gname == NULL) | |
b34976b6 | 814 | return FALSE; |
945906ff | 815 | elf_group_name (newsect) = gname; |
dbb410c3 AM |
816 | |
817 | /* Start a circular list with one element. */ | |
945906ff | 818 | elf_next_in_group (newsect) = newsect; |
dbb410c3 | 819 | } |
b885599b | 820 | |
9dce4196 AM |
821 | /* If the group section has been created, point to the |
822 | new member. */ | |
dbb410c3 | 823 | if (shdr->bfd_section != NULL) |
945906ff | 824 | elf_next_in_group (shdr->bfd_section) = newsect; |
b885599b | 825 | |
564e11c9 JW |
826 | elf_tdata (abfd)->group_search_offset = i; |
827 | j = num_group - 1; | |
dbb410c3 AM |
828 | break; |
829 | } | |
830 | } | |
831 | } | |
832 | ||
945906ff | 833 | if (elf_group_name (newsect) == NULL) |
dbb410c3 | 834 | { |
695344c0 | 835 | /* xgettext:c-format */ |
871b3ab2 | 836 | _bfd_error_handler (_("%pB: no group info for section '%pA'"), |
4eca0228 | 837 | abfd, newsect); |
493a3386 | 838 | return FALSE; |
dbb410c3 | 839 | } |
b34976b6 | 840 | return TRUE; |
dbb410c3 AM |
841 | } |
842 | ||
3d7f7666 | 843 | bfd_boolean |
dd863624 | 844 | _bfd_elf_setup_sections (bfd *abfd) |
3d7f7666 L |
845 | { |
846 | unsigned int i; | |
847 | unsigned int num_group = elf_tdata (abfd)->num_group; | |
848 | bfd_boolean result = TRUE; | |
dd863624 L |
849 | asection *s; |
850 | ||
851 | /* Process SHF_LINK_ORDER. */ | |
852 | for (s = abfd->sections; s != NULL; s = s->next) | |
853 | { | |
854 | Elf_Internal_Shdr *this_hdr = &elf_section_data (s)->this_hdr; | |
855 | if ((this_hdr->sh_flags & SHF_LINK_ORDER) != 0) | |
856 | { | |
857 | unsigned int elfsec = this_hdr->sh_link; | |
858 | /* FIXME: The old Intel compiler and old strip/objcopy may | |
859 | not set the sh_link or sh_info fields. Hence we could | |
860 | get the situation where elfsec is 0. */ | |
861 | if (elfsec == 0) | |
862 | { | |
4fbb74a6 | 863 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
dd863624 L |
864 | if (bed->link_order_error_handler) |
865 | bed->link_order_error_handler | |
695344c0 | 866 | /* xgettext:c-format */ |
871b3ab2 | 867 | (_("%pB: warning: sh_link not set for section `%pA'"), |
dd863624 L |
868 | abfd, s); |
869 | } | |
870 | else | |
871 | { | |
91d6fa6a | 872 | asection *linksec = NULL; |
25bbc984 | 873 | |
4fbb74a6 AM |
874 | if (elfsec < elf_numsections (abfd)) |
875 | { | |
876 | this_hdr = elf_elfsections (abfd)[elfsec]; | |
91d6fa6a | 877 | linksec = this_hdr->bfd_section; |
4fbb74a6 | 878 | } |
25bbc984 L |
879 | |
880 | /* PR 1991, 2008: | |
881 | Some strip/objcopy may leave an incorrect value in | |
882 | sh_link. We don't want to proceed. */ | |
91d6fa6a | 883 | if (linksec == NULL) |
25bbc984 | 884 | { |
4eca0228 | 885 | _bfd_error_handler |
695344c0 | 886 | /* xgettext:c-format */ |
871b3ab2 | 887 | (_("%pB: sh_link [%d] in section `%pA' is incorrect"), |
c08bb8dd | 888 | s->owner, elfsec, s); |
25bbc984 L |
889 | result = FALSE; |
890 | } | |
891 | ||
91d6fa6a | 892 | elf_linked_to_section (s) = linksec; |
dd863624 L |
893 | } |
894 | } | |
53720c49 AM |
895 | else if (this_hdr->sh_type == SHT_GROUP |
896 | && elf_next_in_group (s) == NULL) | |
897 | { | |
4eca0228 | 898 | _bfd_error_handler |
695344c0 | 899 | /* xgettext:c-format */ |
871b3ab2 | 900 | (_("%pB: SHT_GROUP section [index %d] has no SHF_GROUP sections"), |
53720c49 AM |
901 | abfd, elf_section_data (s)->this_idx); |
902 | result = FALSE; | |
903 | } | |
dd863624 | 904 | } |
3d7f7666 | 905 | |
dd863624 | 906 | /* Process section groups. */ |
3d7f7666 L |
907 | if (num_group == (unsigned) -1) |
908 | return result; | |
909 | ||
910 | for (i = 0; i < num_group; i++) | |
911 | { | |
912 | Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i]; | |
4b0e8a5f NC |
913 | Elf_Internal_Group *idx; |
914 | unsigned int n_elt; | |
3d7f7666 | 915 | |
4b0e8a5f NC |
916 | /* PR binutils/18758: Beware of corrupt binaries with invalid group data. */ |
917 | if (shdr == NULL || shdr->bfd_section == NULL || shdr->contents == NULL) | |
918 | { | |
4eca0228 | 919 | _bfd_error_handler |
695344c0 | 920 | /* xgettext:c-format */ |
871b3ab2 | 921 | (_("%pB: section group entry number %u is corrupt"), |
4b0e8a5f NC |
922 | abfd, i); |
923 | result = FALSE; | |
924 | continue; | |
925 | } | |
926 | ||
927 | idx = (Elf_Internal_Group *) shdr->contents; | |
928 | n_elt = shdr->sh_size / 4; | |
1b786873 | 929 | |
3d7f7666 | 930 | while (--n_elt != 0) |
24d3e51b NC |
931 | { |
932 | ++ idx; | |
933 | ||
934 | if (idx->shdr == NULL) | |
935 | continue; | |
936 | else if (idx->shdr->bfd_section) | |
937 | elf_sec_group (idx->shdr->bfd_section) = shdr->bfd_section; | |
db4677b8 AM |
938 | else if (idx->shdr->sh_type != SHT_RELA |
939 | && idx->shdr->sh_type != SHT_REL) | |
24d3e51b NC |
940 | { |
941 | /* There are some unknown sections in the group. */ | |
942 | _bfd_error_handler | |
943 | /* xgettext:c-format */ | |
871b3ab2 | 944 | (_("%pB: unknown type [%#x] section `%s' in group [%pA]"), |
24d3e51b NC |
945 | abfd, |
946 | idx->shdr->sh_type, | |
947 | bfd_elf_string_from_elf_section (abfd, | |
948 | (elf_elfheader (abfd) | |
949 | ->e_shstrndx), | |
950 | idx->shdr->sh_name), | |
951 | shdr->bfd_section); | |
952 | result = FALSE; | |
953 | } | |
954 | } | |
3d7f7666 | 955 | } |
24d3e51b | 956 | |
3d7f7666 L |
957 | return result; |
958 | } | |
959 | ||
72adc230 AM |
960 | bfd_boolean |
961 | bfd_elf_is_group_section (bfd *abfd ATTRIBUTE_UNUSED, const asection *sec) | |
962 | { | |
963 | return elf_next_in_group (sec) != NULL; | |
964 | } | |
965 | ||
f6fe1ccd L |
966 | static char * |
967 | convert_debug_to_zdebug (bfd *abfd, const char *name) | |
968 | { | |
969 | unsigned int len = strlen (name); | |
970 | char *new_name = bfd_alloc (abfd, len + 2); | |
971 | if (new_name == NULL) | |
972 | return NULL; | |
973 | new_name[0] = '.'; | |
974 | new_name[1] = 'z'; | |
975 | memcpy (new_name + 2, name + 1, len); | |
976 | return new_name; | |
977 | } | |
978 | ||
979 | static char * | |
980 | convert_zdebug_to_debug (bfd *abfd, const char *name) | |
981 | { | |
982 | unsigned int len = strlen (name); | |
983 | char *new_name = bfd_alloc (abfd, len); | |
984 | if (new_name == NULL) | |
985 | return NULL; | |
986 | new_name[0] = '.'; | |
987 | memcpy (new_name + 1, name + 2, len - 1); | |
988 | return new_name; | |
989 | } | |
990 | ||
252b5132 RH |
991 | /* Make a BFD section from an ELF section. We store a pointer to the |
992 | BFD section in the bfd_section field of the header. */ | |
993 | ||
b34976b6 | 994 | bfd_boolean |
217aa764 AM |
995 | _bfd_elf_make_section_from_shdr (bfd *abfd, |
996 | Elf_Internal_Shdr *hdr, | |
6dc132d9 L |
997 | const char *name, |
998 | int shindex) | |
252b5132 RH |
999 | { |
1000 | asection *newsect; | |
1001 | flagword flags; | |
9c5bfbb7 | 1002 | const struct elf_backend_data *bed; |
252b5132 RH |
1003 | |
1004 | if (hdr->bfd_section != NULL) | |
4e011fb5 | 1005 | return TRUE; |
252b5132 RH |
1006 | |
1007 | newsect = bfd_make_section_anyway (abfd, name); | |
1008 | if (newsect == NULL) | |
b34976b6 | 1009 | return FALSE; |
252b5132 | 1010 | |
1829f4b2 AM |
1011 | hdr->bfd_section = newsect; |
1012 | elf_section_data (newsect)->this_hdr = *hdr; | |
6dc132d9 | 1013 | elf_section_data (newsect)->this_idx = shindex; |
1829f4b2 | 1014 | |
2f89ff8d L |
1015 | /* Always use the real type/flags. */ |
1016 | elf_section_type (newsect) = hdr->sh_type; | |
1017 | elf_section_flags (newsect) = hdr->sh_flags; | |
1018 | ||
252b5132 RH |
1019 | newsect->filepos = hdr->sh_offset; |
1020 | ||
1021 | if (! bfd_set_section_vma (abfd, newsect, hdr->sh_addr) | |
1022 | || ! bfd_set_section_size (abfd, newsect, hdr->sh_size) | |
1023 | || ! bfd_set_section_alignment (abfd, newsect, | |
72de5009 | 1024 | bfd_log2 (hdr->sh_addralign))) |
b34976b6 | 1025 | return FALSE; |
252b5132 RH |
1026 | |
1027 | flags = SEC_NO_FLAGS; | |
1028 | if (hdr->sh_type != SHT_NOBITS) | |
1029 | flags |= SEC_HAS_CONTENTS; | |
dbb410c3 | 1030 | if (hdr->sh_type == SHT_GROUP) |
7bdf4127 | 1031 | flags |= SEC_GROUP; |
252b5132 RH |
1032 | if ((hdr->sh_flags & SHF_ALLOC) != 0) |
1033 | { | |
1034 | flags |= SEC_ALLOC; | |
1035 | if (hdr->sh_type != SHT_NOBITS) | |
1036 | flags |= SEC_LOAD; | |
1037 | } | |
1038 | if ((hdr->sh_flags & SHF_WRITE) == 0) | |
1039 | flags |= SEC_READONLY; | |
1040 | if ((hdr->sh_flags & SHF_EXECINSTR) != 0) | |
1041 | flags |= SEC_CODE; | |
1042 | else if ((flags & SEC_LOAD) != 0) | |
1043 | flags |= SEC_DATA; | |
f5fa8ca2 JJ |
1044 | if ((hdr->sh_flags & SHF_MERGE) != 0) |
1045 | { | |
1046 | flags |= SEC_MERGE; | |
1047 | newsect->entsize = hdr->sh_entsize; | |
f5fa8ca2 | 1048 | } |
84865015 NC |
1049 | if ((hdr->sh_flags & SHF_STRINGS) != 0) |
1050 | flags |= SEC_STRINGS; | |
dbb410c3 AM |
1051 | if (hdr->sh_flags & SHF_GROUP) |
1052 | if (!setup_group (abfd, hdr, newsect)) | |
b34976b6 | 1053 | return FALSE; |
13ae64f3 JJ |
1054 | if ((hdr->sh_flags & SHF_TLS) != 0) |
1055 | flags |= SEC_THREAD_LOCAL; | |
18ae9cc1 L |
1056 | if ((hdr->sh_flags & SHF_EXCLUDE) != 0) |
1057 | flags |= SEC_EXCLUDE; | |
252b5132 | 1058 | |
3d2b39cf | 1059 | if ((flags & SEC_ALLOC) == 0) |
7a6cc5fb | 1060 | { |
3d2b39cf L |
1061 | /* The debugging sections appear to be recognized only by name, |
1062 | not any sort of flag. Their SEC_ALLOC bits are cleared. */ | |
3d2b39cf L |
1063 | if (name [0] == '.') |
1064 | { | |
f073ced3 AM |
1065 | const char *p; |
1066 | int n; | |
1067 | if (name[1] == 'd') | |
1068 | p = ".debug", n = 6; | |
1069 | else if (name[1] == 'g' && name[2] == 'n') | |
1070 | p = ".gnu.linkonce.wi.", n = 17; | |
1071 | else if (name[1] == 'g' && name[2] == 'd') | |
1072 | p = ".gdb_index", n = 11; /* yes we really do mean 11. */ | |
1073 | else if (name[1] == 'l') | |
1074 | p = ".line", n = 5; | |
1075 | else if (name[1] == 's') | |
1076 | p = ".stab", n = 5; | |
1077 | else if (name[1] == 'z') | |
1078 | p = ".zdebug", n = 7; | |
1079 | else | |
1080 | p = NULL, n = 0; | |
1081 | if (p != NULL && strncmp (name, p, n) == 0) | |
3d2b39cf L |
1082 | flags |= SEC_DEBUGGING; |
1083 | } | |
1084 | } | |
252b5132 RH |
1085 | |
1086 | /* As a GNU extension, if the name begins with .gnu.linkonce, we | |
1087 | only link a single copy of the section. This is used to support | |
1088 | g++. g++ will emit each template expansion in its own section. | |
1089 | The symbols will be defined as weak, so that multiple definitions | |
1090 | are permitted. The GNU linker extension is to actually discard | |
1091 | all but one of the sections. */ | |
0112cd26 | 1092 | if (CONST_STRNEQ (name, ".gnu.linkonce") |
b885599b | 1093 | && elf_next_in_group (newsect) == NULL) |
252b5132 RH |
1094 | flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD; |
1095 | ||
fa152c49 JW |
1096 | bed = get_elf_backend_data (abfd); |
1097 | if (bed->elf_backend_section_flags) | |
1098 | if (! bed->elf_backend_section_flags (&flags, hdr)) | |
b34976b6 | 1099 | return FALSE; |
fa152c49 | 1100 | |
252b5132 | 1101 | if (! bfd_set_section_flags (abfd, newsect, flags)) |
b34976b6 | 1102 | return FALSE; |
252b5132 | 1103 | |
718175fa JK |
1104 | /* We do not parse the PT_NOTE segments as we are interested even in the |
1105 | separate debug info files which may have the segments offsets corrupted. | |
1106 | PT_NOTEs from the core files are currently not parsed using BFD. */ | |
1107 | if (hdr->sh_type == SHT_NOTE) | |
1108 | { | |
baea7ef1 | 1109 | bfd_byte *contents; |
718175fa | 1110 | |
baea7ef1 | 1111 | if (!bfd_malloc_and_get_section (abfd, newsect, &contents)) |
718175fa JK |
1112 | return FALSE; |
1113 | ||
276da9b3 L |
1114 | elf_parse_notes (abfd, (char *) contents, hdr->sh_size, |
1115 | hdr->sh_offset, hdr->sh_addralign); | |
718175fa JK |
1116 | free (contents); |
1117 | } | |
1118 | ||
252b5132 RH |
1119 | if ((flags & SEC_ALLOC) != 0) |
1120 | { | |
1121 | Elf_Internal_Phdr *phdr; | |
6ffd7900 AM |
1122 | unsigned int i, nload; |
1123 | ||
1124 | /* Some ELF linkers produce binaries with all the program header | |
1125 | p_paddr fields zero. If we have such a binary with more than | |
1126 | one PT_LOAD header, then leave the section lma equal to vma | |
1127 | so that we don't create sections with overlapping lma. */ | |
1128 | phdr = elf_tdata (abfd)->phdr; | |
1129 | for (nload = 0, i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++) | |
1130 | if (phdr->p_paddr != 0) | |
1131 | break; | |
1132 | else if (phdr->p_type == PT_LOAD && phdr->p_memsz != 0) | |
1133 | ++nload; | |
1134 | if (i >= elf_elfheader (abfd)->e_phnum && nload > 1) | |
1135 | return TRUE; | |
252b5132 | 1136 | |
252b5132 RH |
1137 | phdr = elf_tdata (abfd)->phdr; |
1138 | for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++) | |
1139 | { | |
86b2281f AM |
1140 | if (((phdr->p_type == PT_LOAD |
1141 | && (hdr->sh_flags & SHF_TLS) == 0) | |
1142 | || phdr->p_type == PT_TLS) | |
9a83a553 | 1143 | && ELF_SECTION_IN_SEGMENT (hdr, phdr)) |
252b5132 | 1144 | { |
88967714 AM |
1145 | if ((flags & SEC_LOAD) == 0) |
1146 | newsect->lma = (phdr->p_paddr | |
1147 | + hdr->sh_addr - phdr->p_vaddr); | |
1148 | else | |
1149 | /* We used to use the same adjustment for SEC_LOAD | |
1150 | sections, but that doesn't work if the segment | |
1151 | is packed with code from multiple VMAs. | |
1152 | Instead we calculate the section LMA based on | |
1153 | the segment LMA. It is assumed that the | |
1154 | segment will contain sections with contiguous | |
1155 | LMAs, even if the VMAs are not. */ | |
1156 | newsect->lma = (phdr->p_paddr | |
1157 | + hdr->sh_offset - phdr->p_offset); | |
1158 | ||
1159 | /* With contiguous segments, we can't tell from file | |
1160 | offsets whether a section with zero size should | |
1161 | be placed at the end of one segment or the | |
1162 | beginning of the next. Decide based on vaddr. */ | |
1163 | if (hdr->sh_addr >= phdr->p_vaddr | |
1164 | && (hdr->sh_addr + hdr->sh_size | |
1165 | <= phdr->p_vaddr + phdr->p_memsz)) | |
1166 | break; | |
252b5132 RH |
1167 | } |
1168 | } | |
1169 | } | |
1170 | ||
4a114e3e L |
1171 | /* Compress/decompress DWARF debug sections with names: .debug_* and |
1172 | .zdebug_*, after the section flags is set. */ | |
1173 | if ((flags & SEC_DEBUGGING) | |
1174 | && ((name[1] == 'd' && name[6] == '_') | |
1175 | || (name[1] == 'z' && name[7] == '_'))) | |
1176 | { | |
1177 | enum { nothing, compress, decompress } action = nothing; | |
151411f8 | 1178 | int compression_header_size; |
dab394de | 1179 | bfd_size_type uncompressed_size; |
151411f8 L |
1180 | bfd_boolean compressed |
1181 | = bfd_is_section_compressed_with_header (abfd, newsect, | |
dab394de L |
1182 | &compression_header_size, |
1183 | &uncompressed_size); | |
4a114e3e | 1184 | |
151411f8 | 1185 | if (compressed) |
4a114e3e L |
1186 | { |
1187 | /* Compressed section. Check if we should decompress. */ | |
1188 | if ((abfd->flags & BFD_DECOMPRESS)) | |
1189 | action = decompress; | |
1190 | } | |
151411f8 L |
1191 | |
1192 | /* Compress the uncompressed section or convert from/to .zdebug* | |
1193 | section. Check if we should compress. */ | |
1194 | if (action == nothing) | |
4a114e3e | 1195 | { |
151411f8 L |
1196 | if (newsect->size != 0 |
1197 | && (abfd->flags & BFD_COMPRESS) | |
1198 | && compression_header_size >= 0 | |
dab394de | 1199 | && uncompressed_size > 0 |
151411f8 L |
1200 | && (!compressed |
1201 | || ((compression_header_size > 0) | |
1202 | != ((abfd->flags & BFD_COMPRESS_GABI) != 0)))) | |
4a114e3e | 1203 | action = compress; |
151411f8 L |
1204 | else |
1205 | return TRUE; | |
4a114e3e L |
1206 | } |
1207 | ||
151411f8 | 1208 | if (action == compress) |
4a114e3e | 1209 | { |
4a114e3e L |
1210 | if (!bfd_init_section_compress_status (abfd, newsect)) |
1211 | { | |
4eca0228 | 1212 | _bfd_error_handler |
695344c0 | 1213 | /* xgettext:c-format */ |
871b3ab2 | 1214 | (_("%pB: unable to initialize compress status for section %s"), |
4a114e3e L |
1215 | abfd, name); |
1216 | return FALSE; | |
1217 | } | |
151411f8 L |
1218 | } |
1219 | else | |
1220 | { | |
4a114e3e L |
1221 | if (!bfd_init_section_decompress_status (abfd, newsect)) |
1222 | { | |
4eca0228 | 1223 | _bfd_error_handler |
695344c0 | 1224 | /* xgettext:c-format */ |
871b3ab2 | 1225 | (_("%pB: unable to initialize decompress status for section %s"), |
4a114e3e L |
1226 | abfd, name); |
1227 | return FALSE; | |
1228 | } | |
151411f8 L |
1229 | } |
1230 | ||
f6fe1ccd | 1231 | if (abfd->is_linker_input) |
151411f8 | 1232 | { |
f6fe1ccd L |
1233 | if (name[1] == 'z' |
1234 | && (action == decompress | |
1235 | || (action == compress | |
1236 | && (abfd->flags & BFD_COMPRESS_GABI) != 0))) | |
4e011fb5 | 1237 | { |
f6fe1ccd L |
1238 | /* Convert section name from .zdebug_* to .debug_* so |
1239 | that linker will consider this section as a debug | |
1240 | section. */ | |
1241 | char *new_name = convert_zdebug_to_debug (abfd, name); | |
151411f8 L |
1242 | if (new_name == NULL) |
1243 | return FALSE; | |
f6fe1ccd | 1244 | bfd_rename_section (abfd, newsect, new_name); |
151411f8 | 1245 | } |
4a114e3e | 1246 | } |
f6fe1ccd L |
1247 | else |
1248 | /* For objdump, don't rename the section. For objcopy, delay | |
1249 | section rename to elf_fake_sections. */ | |
1250 | newsect->flags |= SEC_ELF_RENAME; | |
4a114e3e L |
1251 | } |
1252 | ||
b34976b6 | 1253 | return TRUE; |
252b5132 RH |
1254 | } |
1255 | ||
84865015 NC |
1256 | const char *const bfd_elf_section_type_names[] = |
1257 | { | |
252b5132 RH |
1258 | "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB", |
1259 | "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE", | |
1260 | "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM", | |
1261 | }; | |
1262 | ||
1049f94e | 1263 | /* ELF relocs are against symbols. If we are producing relocatable |
252b5132 RH |
1264 | output, and the reloc is against an external symbol, and nothing |
1265 | has given us any additional addend, the resulting reloc will also | |
1266 | be against the same symbol. In such a case, we don't want to | |
1267 | change anything about the way the reloc is handled, since it will | |
1268 | all be done at final link time. Rather than put special case code | |
1269 | into bfd_perform_relocation, all the reloc types use this howto | |
1270 | function. It just short circuits the reloc if producing | |
1049f94e | 1271 | relocatable output against an external symbol. */ |
252b5132 | 1272 | |
252b5132 | 1273 | bfd_reloc_status_type |
217aa764 AM |
1274 | bfd_elf_generic_reloc (bfd *abfd ATTRIBUTE_UNUSED, |
1275 | arelent *reloc_entry, | |
1276 | asymbol *symbol, | |
1277 | void *data ATTRIBUTE_UNUSED, | |
1278 | asection *input_section, | |
1279 | bfd *output_bfd, | |
1280 | char **error_message ATTRIBUTE_UNUSED) | |
1281 | { | |
1282 | if (output_bfd != NULL | |
252b5132 RH |
1283 | && (symbol->flags & BSF_SECTION_SYM) == 0 |
1284 | && (! reloc_entry->howto->partial_inplace | |
1285 | || reloc_entry->addend == 0)) | |
1286 | { | |
1287 | reloc_entry->address += input_section->output_offset; | |
1288 | return bfd_reloc_ok; | |
1289 | } | |
1290 | ||
1291 | return bfd_reloc_continue; | |
1292 | } | |
1293 | \f | |
84865015 NC |
1294 | /* Returns TRUE if section A matches section B. |
1295 | Names, addresses and links may be different, but everything else | |
1296 | should be the same. */ | |
1297 | ||
1298 | static bfd_boolean | |
5522f910 NC |
1299 | section_match (const Elf_Internal_Shdr * a, |
1300 | const Elf_Internal_Shdr * b) | |
84865015 | 1301 | { |
ac85e67c AM |
1302 | if (a->sh_type != b->sh_type |
1303 | || ((a->sh_flags ^ b->sh_flags) & ~SHF_INFO_LINK) != 0 | |
1304 | || a->sh_addralign != b->sh_addralign | |
1305 | || a->sh_entsize != b->sh_entsize) | |
1306 | return FALSE; | |
1307 | if (a->sh_type == SHT_SYMTAB | |
1308 | || a->sh_type == SHT_STRTAB) | |
1309 | return TRUE; | |
1310 | return a->sh_size == b->sh_size; | |
84865015 NC |
1311 | } |
1312 | ||
1313 | /* Find a section in OBFD that has the same characteristics | |
1314 | as IHEADER. Return the index of this section or SHN_UNDEF if | |
1315 | none can be found. Check's section HINT first, as this is likely | |
1316 | to be the correct section. */ | |
1317 | ||
1318 | static unsigned int | |
5cc4ca83 ST |
1319 | find_link (const bfd *obfd, const Elf_Internal_Shdr *iheader, |
1320 | const unsigned int hint) | |
84865015 NC |
1321 | { |
1322 | Elf_Internal_Shdr ** oheaders = elf_elfsections (obfd); | |
1323 | unsigned int i; | |
1324 | ||
a55c9876 NC |
1325 | BFD_ASSERT (iheader != NULL); |
1326 | ||
1327 | /* See PR 20922 for a reproducer of the NULL test. */ | |
5cc4ca83 ST |
1328 | if (hint < elf_numsections (obfd) |
1329 | && oheaders[hint] != NULL | |
a55c9876 | 1330 | && section_match (oheaders[hint], iheader)) |
84865015 NC |
1331 | return hint; |
1332 | ||
1333 | for (i = 1; i < elf_numsections (obfd); i++) | |
1334 | { | |
1335 | Elf_Internal_Shdr * oheader = oheaders[i]; | |
1336 | ||
a55c9876 NC |
1337 | if (oheader == NULL) |
1338 | continue; | |
84865015 NC |
1339 | if (section_match (oheader, iheader)) |
1340 | /* FIXME: Do we care if there is a potential for | |
1341 | multiple matches ? */ | |
1342 | return i; | |
1343 | } | |
1344 | ||
1345 | return SHN_UNDEF; | |
1346 | } | |
1347 | ||
5522f910 NC |
1348 | /* PR 19938: Attempt to set the ELF section header fields of an OS or |
1349 | Processor specific section, based upon a matching input section. | |
1350 | Returns TRUE upon success, FALSE otherwise. */ | |
07d6d2b8 | 1351 | |
5522f910 NC |
1352 | static bfd_boolean |
1353 | copy_special_section_fields (const bfd *ibfd, | |
1354 | bfd *obfd, | |
1355 | const Elf_Internal_Shdr *iheader, | |
1356 | Elf_Internal_Shdr *oheader, | |
1357 | const unsigned int secnum) | |
1358 | { | |
1359 | const struct elf_backend_data *bed = get_elf_backend_data (obfd); | |
1360 | const Elf_Internal_Shdr **iheaders = (const Elf_Internal_Shdr **) elf_elfsections (ibfd); | |
1361 | bfd_boolean changed = FALSE; | |
1362 | unsigned int sh_link; | |
1363 | ||
1364 | if (oheader->sh_type == SHT_NOBITS) | |
1365 | { | |
1366 | /* This is a feature for objcopy --only-keep-debug: | |
1367 | When a section's type is changed to NOBITS, we preserve | |
1368 | the sh_link and sh_info fields so that they can be | |
1369 | matched up with the original. | |
1370 | ||
1371 | Note: Strictly speaking these assignments are wrong. | |
1372 | The sh_link and sh_info fields should point to the | |
1373 | relevent sections in the output BFD, which may not be in | |
1374 | the same location as they were in the input BFD. But | |
1375 | the whole point of this action is to preserve the | |
1376 | original values of the sh_link and sh_info fields, so | |
1377 | that they can be matched up with the section headers in | |
1378 | the original file. So strictly speaking we may be | |
1379 | creating an invalid ELF file, but it is only for a file | |
1380 | that just contains debug info and only for sections | |
1381 | without any contents. */ | |
1382 | if (oheader->sh_link == 0) | |
1383 | oheader->sh_link = iheader->sh_link; | |
1384 | if (oheader->sh_info == 0) | |
1385 | oheader->sh_info = iheader->sh_info; | |
1386 | return TRUE; | |
1387 | } | |
1388 | ||
1389 | /* Allow the target a chance to decide how these fields should be set. */ | |
1390 | if (bed->elf_backend_copy_special_section_fields != NULL | |
1391 | && bed->elf_backend_copy_special_section_fields | |
1392 | (ibfd, obfd, iheader, oheader)) | |
1393 | return TRUE; | |
1394 | ||
1395 | /* We have an iheader which might match oheader, and which has non-zero | |
1396 | sh_info and/or sh_link fields. Attempt to follow those links and find | |
1397 | the section in the output bfd which corresponds to the linked section | |
1398 | in the input bfd. */ | |
1399 | if (iheader->sh_link != SHN_UNDEF) | |
1400 | { | |
4f3ca05b NC |
1401 | /* See PR 20931 for a reproducer. */ |
1402 | if (iheader->sh_link >= elf_numsections (ibfd)) | |
1403 | { | |
76cfced5 | 1404 | _bfd_error_handler |
4f3ca05b | 1405 | /* xgettext:c-format */ |
9793eb77 | 1406 | (_("%pB: invalid sh_link field (%d) in section number %d"), |
4f3ca05b NC |
1407 | ibfd, iheader->sh_link, secnum); |
1408 | return FALSE; | |
1409 | } | |
1410 | ||
5522f910 NC |
1411 | sh_link = find_link (obfd, iheaders[iheader->sh_link], iheader->sh_link); |
1412 | if (sh_link != SHN_UNDEF) | |
1413 | { | |
1414 | oheader->sh_link = sh_link; | |
1415 | changed = TRUE; | |
1416 | } | |
1417 | else | |
1418 | /* FIXME: Should we install iheader->sh_link | |
1419 | if we could not find a match ? */ | |
76cfced5 | 1420 | _bfd_error_handler |
695344c0 | 1421 | /* xgettext:c-format */ |
9793eb77 | 1422 | (_("%pB: failed to find link section for section %d"), obfd, secnum); |
5522f910 NC |
1423 | } |
1424 | ||
1425 | if (iheader->sh_info) | |
1426 | { | |
1427 | /* The sh_info field can hold arbitrary information, but if the | |
1428 | SHF_LINK_INFO flag is set then it should be interpreted as a | |
1429 | section index. */ | |
1430 | if (iheader->sh_flags & SHF_INFO_LINK) | |
1431 | { | |
1432 | sh_link = find_link (obfd, iheaders[iheader->sh_info], | |
1433 | iheader->sh_info); | |
1434 | if (sh_link != SHN_UNDEF) | |
1435 | oheader->sh_flags |= SHF_INFO_LINK; | |
1436 | } | |
1437 | else | |
1438 | /* No idea what it means - just copy it. */ | |
1439 | sh_link = iheader->sh_info; | |
1440 | ||
1441 | if (sh_link != SHN_UNDEF) | |
1442 | { | |
1443 | oheader->sh_info = sh_link; | |
1444 | changed = TRUE; | |
1445 | } | |
1446 | else | |
76cfced5 | 1447 | _bfd_error_handler |
695344c0 | 1448 | /* xgettext:c-format */ |
9793eb77 | 1449 | (_("%pB: failed to find info section for section %d"), obfd, secnum); |
5522f910 NC |
1450 | } |
1451 | ||
1452 | return changed; | |
1453 | } | |
07d6d2b8 | 1454 | |
0ac4564e L |
1455 | /* Copy the program header and other data from one object module to |
1456 | another. */ | |
252b5132 | 1457 | |
b34976b6 | 1458 | bfd_boolean |
217aa764 | 1459 | _bfd_elf_copy_private_bfd_data (bfd *ibfd, bfd *obfd) |
2d502050 | 1460 | { |
5522f910 NC |
1461 | const Elf_Internal_Shdr **iheaders = (const Elf_Internal_Shdr **) elf_elfsections (ibfd); |
1462 | Elf_Internal_Shdr **oheaders = elf_elfsections (obfd); | |
1463 | const struct elf_backend_data *bed; | |
84865015 NC |
1464 | unsigned int i; |
1465 | ||
2d502050 | 1466 | if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour |
84865015 | 1467 | || bfd_get_flavour (obfd) != bfd_target_elf_flavour) |
b34976b6 | 1468 | return TRUE; |
2d502050 | 1469 | |
57b828ef L |
1470 | if (!elf_flags_init (obfd)) |
1471 | { | |
1472 | elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags; | |
1473 | elf_flags_init (obfd) = TRUE; | |
1474 | } | |
2d502050 | 1475 | |
0ac4564e | 1476 | elf_gp (obfd) = elf_gp (ibfd); |
57b828ef L |
1477 | |
1478 | /* Also copy the EI_OSABI field. */ | |
1479 | elf_elfheader (obfd)->e_ident[EI_OSABI] = | |
1480 | elf_elfheader (ibfd)->e_ident[EI_OSABI]; | |
104d59d1 | 1481 | |
5522f910 NC |
1482 | /* If set, copy the EI_ABIVERSION field. */ |
1483 | if (elf_elfheader (ibfd)->e_ident[EI_ABIVERSION]) | |
1484 | elf_elfheader (obfd)->e_ident[EI_ABIVERSION] | |
1485 | = elf_elfheader (ibfd)->e_ident[EI_ABIVERSION]; | |
07d6d2b8 | 1486 | |
104d59d1 JM |
1487 | /* Copy object attributes. */ |
1488 | _bfd_elf_copy_obj_attributes (ibfd, obfd); | |
63b9bbb7 | 1489 | |
84865015 NC |
1490 | if (iheaders == NULL || oheaders == NULL) |
1491 | return TRUE; | |
63b9bbb7 | 1492 | |
5522f910 NC |
1493 | bed = get_elf_backend_data (obfd); |
1494 | ||
1495 | /* Possibly copy other fields in the section header. */ | |
84865015 | 1496 | for (i = 1; i < elf_numsections (obfd); i++) |
63b9bbb7 | 1497 | { |
84865015 NC |
1498 | unsigned int j; |
1499 | Elf_Internal_Shdr * oheader = oheaders[i]; | |
63b9bbb7 | 1500 | |
5522f910 NC |
1501 | /* Ignore ordinary sections. SHT_NOBITS sections are considered however |
1502 | because of a special case need for generating separate debug info | |
1503 | files. See below for more details. */ | |
84865015 NC |
1504 | if (oheader == NULL |
1505 | || (oheader->sh_type != SHT_NOBITS | |
5522f910 NC |
1506 | && oheader->sh_type < SHT_LOOS)) |
1507 | continue; | |
1508 | ||
1509 | /* Ignore empty sections, and sections whose | |
1510 | fields have already been initialised. */ | |
1511 | if (oheader->sh_size == 0 | |
84865015 NC |
1512 | || (oheader->sh_info != 0 && oheader->sh_link != 0)) |
1513 | continue; | |
63b9bbb7 | 1514 | |
84865015 | 1515 | /* Scan for the matching section in the input bfd. |
5522f910 NC |
1516 | First we try for a direct mapping between the input and output sections. */ |
1517 | for (j = 1; j < elf_numsections (ibfd); j++) | |
1518 | { | |
1519 | const Elf_Internal_Shdr * iheader = iheaders[j]; | |
1520 | ||
1521 | if (iheader == NULL) | |
1522 | continue; | |
1523 | ||
1524 | if (oheader->bfd_section != NULL | |
1525 | && iheader->bfd_section != NULL | |
1526 | && iheader->bfd_section->output_section != NULL | |
1527 | && iheader->bfd_section->output_section == oheader->bfd_section) | |
1528 | { | |
1529 | /* We have found a connection from the input section to the | |
1530 | output section. Attempt to copy the header fields. If | |
1531 | this fails then do not try any further sections - there | |
1532 | should only be a one-to-one mapping between input and output. */ | |
1533 | if (! copy_special_section_fields (ibfd, obfd, iheader, oheader, i)) | |
1534 | j = elf_numsections (ibfd); | |
1535 | break; | |
1536 | } | |
1537 | } | |
1538 | ||
1539 | if (j < elf_numsections (ibfd)) | |
1540 | continue; | |
1541 | ||
1542 | /* That failed. So try to deduce the corresponding input section. | |
84865015 NC |
1543 | Unfortunately we cannot compare names as the output string table |
1544 | is empty, so instead we check size, address and type. */ | |
1545 | for (j = 1; j < elf_numsections (ibfd); j++) | |
1546 | { | |
5522f910 | 1547 | const Elf_Internal_Shdr * iheader = iheaders[j]; |
84865015 | 1548 | |
5522f910 NC |
1549 | if (iheader == NULL) |
1550 | continue; | |
1551 | ||
1552 | /* Try matching fields in the input section's header. | |
1553 | Since --only-keep-debug turns all non-debug sections into | |
84865015 NC |
1554 | SHT_NOBITS sections, the output SHT_NOBITS type matches any |
1555 | input type. */ | |
1556 | if ((oheader->sh_type == SHT_NOBITS | |
1557 | || iheader->sh_type == oheader->sh_type) | |
5522f910 NC |
1558 | && (iheader->sh_flags & ~ SHF_INFO_LINK) |
1559 | == (oheader->sh_flags & ~ SHF_INFO_LINK) | |
84865015 NC |
1560 | && iheader->sh_addralign == oheader->sh_addralign |
1561 | && iheader->sh_entsize == oheader->sh_entsize | |
1562 | && iheader->sh_size == oheader->sh_size | |
1563 | && iheader->sh_addr == oheader->sh_addr | |
1564 | && (iheader->sh_info != oheader->sh_info | |
1565 | || iheader->sh_link != oheader->sh_link)) | |
63b9bbb7 | 1566 | { |
5522f910 NC |
1567 | if (copy_special_section_fields (ibfd, obfd, iheader, oheader, i)) |
1568 | break; | |
63b9bbb7 NC |
1569 | } |
1570 | } | |
5522f910 NC |
1571 | |
1572 | if (j == elf_numsections (ibfd) && oheader->sh_type >= SHT_LOOS) | |
1573 | { | |
1574 | /* Final attempt. Call the backend copy function | |
1575 | with a NULL input section. */ | |
1576 | if (bed->elf_backend_copy_special_section_fields != NULL) | |
1577 | bed->elf_backend_copy_special_section_fields (ibfd, obfd, NULL, oheader); | |
1578 | } | |
63b9bbb7 NC |
1579 | } |
1580 | ||
b34976b6 | 1581 | return TRUE; |
2d502050 L |
1582 | } |
1583 | ||
cedc298e L |
1584 | static const char * |
1585 | get_segment_type (unsigned int p_type) | |
1586 | { | |
1587 | const char *pt; | |
1588 | switch (p_type) | |
1589 | { | |
1590 | case PT_NULL: pt = "NULL"; break; | |
1591 | case PT_LOAD: pt = "LOAD"; break; | |
1592 | case PT_DYNAMIC: pt = "DYNAMIC"; break; | |
1593 | case PT_INTERP: pt = "INTERP"; break; | |
1594 | case PT_NOTE: pt = "NOTE"; break; | |
1595 | case PT_SHLIB: pt = "SHLIB"; break; | |
1596 | case PT_PHDR: pt = "PHDR"; break; | |
1597 | case PT_TLS: pt = "TLS"; break; | |
1598 | case PT_GNU_EH_FRAME: pt = "EH_FRAME"; break; | |
2b05f1b7 | 1599 | case PT_GNU_STACK: pt = "STACK"; break; |
cedc298e L |
1600 | case PT_GNU_RELRO: pt = "RELRO"; break; |
1601 | default: pt = NULL; break; | |
1602 | } | |
1603 | return pt; | |
1604 | } | |
1605 | ||
f0b79d91 L |
1606 | /* Print out the program headers. */ |
1607 | ||
b34976b6 | 1608 | bfd_boolean |
217aa764 | 1609 | _bfd_elf_print_private_bfd_data (bfd *abfd, void *farg) |
252b5132 | 1610 | { |
a50b1753 | 1611 | FILE *f = (FILE *) farg; |
252b5132 RH |
1612 | Elf_Internal_Phdr *p; |
1613 | asection *s; | |
1614 | bfd_byte *dynbuf = NULL; | |
1615 | ||
1616 | p = elf_tdata (abfd)->phdr; | |
1617 | if (p != NULL) | |
1618 | { | |
1619 | unsigned int i, c; | |
1620 | ||
1621 | fprintf (f, _("\nProgram Header:\n")); | |
1622 | c = elf_elfheader (abfd)->e_phnum; | |
1623 | for (i = 0; i < c; i++, p++) | |
1624 | { | |
cedc298e | 1625 | const char *pt = get_segment_type (p->p_type); |
252b5132 RH |
1626 | char buf[20]; |
1627 | ||
cedc298e | 1628 | if (pt == NULL) |
252b5132 | 1629 | { |
cedc298e L |
1630 | sprintf (buf, "0x%lx", p->p_type); |
1631 | pt = buf; | |
252b5132 | 1632 | } |
dc810e39 | 1633 | fprintf (f, "%8s off 0x", pt); |
60b89a18 | 1634 | bfd_fprintf_vma (abfd, f, p->p_offset); |
252b5132 | 1635 | fprintf (f, " vaddr 0x"); |
60b89a18 | 1636 | bfd_fprintf_vma (abfd, f, p->p_vaddr); |
252b5132 | 1637 | fprintf (f, " paddr 0x"); |
60b89a18 | 1638 | bfd_fprintf_vma (abfd, f, p->p_paddr); |
252b5132 RH |
1639 | fprintf (f, " align 2**%u\n", bfd_log2 (p->p_align)); |
1640 | fprintf (f, " filesz 0x"); | |
60b89a18 | 1641 | bfd_fprintf_vma (abfd, f, p->p_filesz); |
252b5132 | 1642 | fprintf (f, " memsz 0x"); |
60b89a18 | 1643 | bfd_fprintf_vma (abfd, f, p->p_memsz); |
252b5132 RH |
1644 | fprintf (f, " flags %c%c%c", |
1645 | (p->p_flags & PF_R) != 0 ? 'r' : '-', | |
1646 | (p->p_flags & PF_W) != 0 ? 'w' : '-', | |
1647 | (p->p_flags & PF_X) != 0 ? 'x' : '-'); | |
dc810e39 AM |
1648 | if ((p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X)) != 0) |
1649 | fprintf (f, " %lx", p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X)); | |
252b5132 RH |
1650 | fprintf (f, "\n"); |
1651 | } | |
1652 | } | |
1653 | ||
1654 | s = bfd_get_section_by_name (abfd, ".dynamic"); | |
1655 | if (s != NULL) | |
1656 | { | |
cb33740c | 1657 | unsigned int elfsec; |
dc810e39 | 1658 | unsigned long shlink; |
252b5132 RH |
1659 | bfd_byte *extdyn, *extdynend; |
1660 | size_t extdynsize; | |
217aa764 | 1661 | void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *); |
252b5132 RH |
1662 | |
1663 | fprintf (f, _("\nDynamic Section:\n")); | |
1664 | ||
eea6121a | 1665 | if (!bfd_malloc_and_get_section (abfd, s, &dynbuf)) |
252b5132 RH |
1666 | goto error_return; |
1667 | ||
1668 | elfsec = _bfd_elf_section_from_bfd_section (abfd, s); | |
cb33740c | 1669 | if (elfsec == SHN_BAD) |
252b5132 | 1670 | goto error_return; |
dc810e39 | 1671 | shlink = elf_elfsections (abfd)[elfsec]->sh_link; |
252b5132 RH |
1672 | |
1673 | extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn; | |
1674 | swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in; | |
1675 | ||
1676 | extdyn = dynbuf; | |
06614111 NC |
1677 | /* PR 17512: file: 6f427532. */ |
1678 | if (s->size < extdynsize) | |
1679 | goto error_return; | |
eea6121a | 1680 | extdynend = extdyn + s->size; |
1036838a | 1681 | /* PR 17512: file: id:000006,sig:06,src:000000,op:flip4,pos:5664. |
07d6d2b8 | 1682 | Fix range check. */ |
1036838a | 1683 | for (; extdyn <= (extdynend - extdynsize); extdyn += extdynsize) |
252b5132 RH |
1684 | { |
1685 | Elf_Internal_Dyn dyn; | |
ad9563d6 | 1686 | const char *name = ""; |
252b5132 | 1687 | char ab[20]; |
b34976b6 | 1688 | bfd_boolean stringp; |
ad9563d6 | 1689 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
252b5132 | 1690 | |
217aa764 | 1691 | (*swap_dyn_in) (abfd, extdyn, &dyn); |
252b5132 RH |
1692 | |
1693 | if (dyn.d_tag == DT_NULL) | |
1694 | break; | |
1695 | ||
b34976b6 | 1696 | stringp = FALSE; |
252b5132 RH |
1697 | switch (dyn.d_tag) |
1698 | { | |
1699 | default: | |
ad9563d6 CM |
1700 | if (bed->elf_backend_get_target_dtag) |
1701 | name = (*bed->elf_backend_get_target_dtag) (dyn.d_tag); | |
1702 | ||
1703 | if (!strcmp (name, "")) | |
1704 | { | |
cd9af601 | 1705 | sprintf (ab, "%#" BFD_VMA_FMT "x", dyn.d_tag); |
ad9563d6 CM |
1706 | name = ab; |
1707 | } | |
252b5132 RH |
1708 | break; |
1709 | ||
b34976b6 | 1710 | case DT_NEEDED: name = "NEEDED"; stringp = TRUE; break; |
252b5132 RH |
1711 | case DT_PLTRELSZ: name = "PLTRELSZ"; break; |
1712 | case DT_PLTGOT: name = "PLTGOT"; break; | |
1713 | case DT_HASH: name = "HASH"; break; | |
1714 | case DT_STRTAB: name = "STRTAB"; break; | |
1715 | case DT_SYMTAB: name = "SYMTAB"; break; | |
1716 | case DT_RELA: name = "RELA"; break; | |
1717 | case DT_RELASZ: name = "RELASZ"; break; | |
1718 | case DT_RELAENT: name = "RELAENT"; break; | |
1719 | case DT_STRSZ: name = "STRSZ"; break; | |
1720 | case DT_SYMENT: name = "SYMENT"; break; | |
1721 | case DT_INIT: name = "INIT"; break; | |
1722 | case DT_FINI: name = "FINI"; break; | |
b34976b6 AM |
1723 | case DT_SONAME: name = "SONAME"; stringp = TRUE; break; |
1724 | case DT_RPATH: name = "RPATH"; stringp = TRUE; break; | |
252b5132 RH |
1725 | case DT_SYMBOLIC: name = "SYMBOLIC"; break; |
1726 | case DT_REL: name = "REL"; break; | |
1727 | case DT_RELSZ: name = "RELSZ"; break; | |
1728 | case DT_RELENT: name = "RELENT"; break; | |
1729 | case DT_PLTREL: name = "PLTREL"; break; | |
1730 | case DT_DEBUG: name = "DEBUG"; break; | |
1731 | case DT_TEXTREL: name = "TEXTREL"; break; | |
1732 | case DT_JMPREL: name = "JMPREL"; break; | |
94558834 L |
1733 | case DT_BIND_NOW: name = "BIND_NOW"; break; |
1734 | case DT_INIT_ARRAY: name = "INIT_ARRAY"; break; | |
1735 | case DT_FINI_ARRAY: name = "FINI_ARRAY"; break; | |
1736 | case DT_INIT_ARRAYSZ: name = "INIT_ARRAYSZ"; break; | |
1737 | case DT_FINI_ARRAYSZ: name = "FINI_ARRAYSZ"; break; | |
b34976b6 | 1738 | case DT_RUNPATH: name = "RUNPATH"; stringp = TRUE; break; |
94558834 L |
1739 | case DT_FLAGS: name = "FLAGS"; break; |
1740 | case DT_PREINIT_ARRAY: name = "PREINIT_ARRAY"; break; | |
1741 | case DT_PREINIT_ARRAYSZ: name = "PREINIT_ARRAYSZ"; break; | |
d48188b9 | 1742 | case DT_CHECKSUM: name = "CHECKSUM"; break; |
94558834 L |
1743 | case DT_PLTPADSZ: name = "PLTPADSZ"; break; |
1744 | case DT_MOVEENT: name = "MOVEENT"; break; | |
1745 | case DT_MOVESZ: name = "MOVESZ"; break; | |
1746 | case DT_FEATURE: name = "FEATURE"; break; | |
1747 | case DT_POSFLAG_1: name = "POSFLAG_1"; break; | |
1748 | case DT_SYMINSZ: name = "SYMINSZ"; break; | |
1749 | case DT_SYMINENT: name = "SYMINENT"; break; | |
b34976b6 AM |
1750 | case DT_CONFIG: name = "CONFIG"; stringp = TRUE; break; |
1751 | case DT_DEPAUDIT: name = "DEPAUDIT"; stringp = TRUE; break; | |
1752 | case DT_AUDIT: name = "AUDIT"; stringp = TRUE; break; | |
94558834 L |
1753 | case DT_PLTPAD: name = "PLTPAD"; break; |
1754 | case DT_MOVETAB: name = "MOVETAB"; break; | |
1755 | case DT_SYMINFO: name = "SYMINFO"; break; | |
1756 | case DT_RELACOUNT: name = "RELACOUNT"; break; | |
1757 | case DT_RELCOUNT: name = "RELCOUNT"; break; | |
1758 | case DT_FLAGS_1: name = "FLAGS_1"; break; | |
252b5132 RH |
1759 | case DT_VERSYM: name = "VERSYM"; break; |
1760 | case DT_VERDEF: name = "VERDEF"; break; | |
1761 | case DT_VERDEFNUM: name = "VERDEFNUM"; break; | |
1762 | case DT_VERNEED: name = "VERNEED"; break; | |
1763 | case DT_VERNEEDNUM: name = "VERNEEDNUM"; break; | |
b34976b6 | 1764 | case DT_AUXILIARY: name = "AUXILIARY"; stringp = TRUE; break; |
94558834 | 1765 | case DT_USED: name = "USED"; break; |
b34976b6 | 1766 | case DT_FILTER: name = "FILTER"; stringp = TRUE; break; |
fdc90cb4 | 1767 | case DT_GNU_HASH: name = "GNU_HASH"; break; |
252b5132 RH |
1768 | } |
1769 | ||
ad9563d6 | 1770 | fprintf (f, " %-20s ", name); |
252b5132 | 1771 | if (! stringp) |
a1f3c56e AN |
1772 | { |
1773 | fprintf (f, "0x"); | |
1774 | bfd_fprintf_vma (abfd, f, dyn.d_un.d_val); | |
1775 | } | |
252b5132 RH |
1776 | else |
1777 | { | |
1778 | const char *string; | |
dc810e39 | 1779 | unsigned int tagv = dyn.d_un.d_val; |
252b5132 | 1780 | |
dc810e39 | 1781 | string = bfd_elf_string_from_elf_section (abfd, shlink, tagv); |
252b5132 RH |
1782 | if (string == NULL) |
1783 | goto error_return; | |
1784 | fprintf (f, "%s", string); | |
1785 | } | |
1786 | fprintf (f, "\n"); | |
1787 | } | |
1788 | ||
1789 | free (dynbuf); | |
1790 | dynbuf = NULL; | |
1791 | } | |
1792 | ||
1793 | if ((elf_dynverdef (abfd) != 0 && elf_tdata (abfd)->verdef == NULL) | |
1794 | || (elf_dynverref (abfd) != 0 && elf_tdata (abfd)->verref == NULL)) | |
1795 | { | |
fc0e6df6 | 1796 | if (! _bfd_elf_slurp_version_tables (abfd, FALSE)) |
b34976b6 | 1797 | return FALSE; |
252b5132 RH |
1798 | } |
1799 | ||
1800 | if (elf_dynverdef (abfd) != 0) | |
1801 | { | |
1802 | Elf_Internal_Verdef *t; | |
1803 | ||
1804 | fprintf (f, _("\nVersion definitions:\n")); | |
1805 | for (t = elf_tdata (abfd)->verdef; t != NULL; t = t->vd_nextdef) | |
1806 | { | |
1807 | fprintf (f, "%d 0x%2.2x 0x%8.8lx %s\n", t->vd_ndx, | |
d0fb9a8d JJ |
1808 | t->vd_flags, t->vd_hash, |
1809 | t->vd_nodename ? t->vd_nodename : "<corrupt>"); | |
1810 | if (t->vd_auxptr != NULL && t->vd_auxptr->vda_nextptr != NULL) | |
252b5132 RH |
1811 | { |
1812 | Elf_Internal_Verdaux *a; | |
1813 | ||
1814 | fprintf (f, "\t"); | |
1815 | for (a = t->vd_auxptr->vda_nextptr; | |
1816 | a != NULL; | |
1817 | a = a->vda_nextptr) | |
d0fb9a8d JJ |
1818 | fprintf (f, "%s ", |
1819 | a->vda_nodename ? a->vda_nodename : "<corrupt>"); | |
252b5132 RH |
1820 | fprintf (f, "\n"); |
1821 | } | |
1822 | } | |
1823 | } | |
1824 | ||
1825 | if (elf_dynverref (abfd) != 0) | |
1826 | { | |
1827 | Elf_Internal_Verneed *t; | |
1828 | ||
1829 | fprintf (f, _("\nVersion References:\n")); | |
1830 | for (t = elf_tdata (abfd)->verref; t != NULL; t = t->vn_nextref) | |
1831 | { | |
1832 | Elf_Internal_Vernaux *a; | |
1833 | ||
d0fb9a8d JJ |
1834 | fprintf (f, _(" required from %s:\n"), |
1835 | t->vn_filename ? t->vn_filename : "<corrupt>"); | |
252b5132 RH |
1836 | for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) |
1837 | fprintf (f, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a->vna_hash, | |
d0fb9a8d JJ |
1838 | a->vna_flags, a->vna_other, |
1839 | a->vna_nodename ? a->vna_nodename : "<corrupt>"); | |
252b5132 RH |
1840 | } |
1841 | } | |
1842 | ||
b34976b6 | 1843 | return TRUE; |
252b5132 RH |
1844 | |
1845 | error_return: | |
1846 | if (dynbuf != NULL) | |
1847 | free (dynbuf); | |
b34976b6 | 1848 | return FALSE; |
252b5132 RH |
1849 | } |
1850 | ||
bb4d2ac2 L |
1851 | /* Get version string. */ |
1852 | ||
1853 | const char * | |
60bb06bc L |
1854 | _bfd_elf_get_symbol_version_string (bfd *abfd, asymbol *symbol, |
1855 | bfd_boolean *hidden) | |
bb4d2ac2 L |
1856 | { |
1857 | const char *version_string = NULL; | |
1858 | if (elf_dynversym (abfd) != 0 | |
1859 | && (elf_dynverdef (abfd) != 0 || elf_dynverref (abfd) != 0)) | |
1860 | { | |
1861 | unsigned int vernum = ((elf_symbol_type *) symbol)->version; | |
1862 | ||
1863 | *hidden = (vernum & VERSYM_HIDDEN) != 0; | |
1864 | vernum &= VERSYM_VERSION; | |
1865 | ||
1866 | if (vernum == 0) | |
1867 | version_string = ""; | |
1f6f5dba L |
1868 | else if (vernum == 1 |
1869 | && (vernum > elf_tdata (abfd)->cverdefs | |
1870 | || (elf_tdata (abfd)->verdef[0].vd_flags | |
1871 | == VER_FLG_BASE))) | |
bb4d2ac2 L |
1872 | version_string = "Base"; |
1873 | else if (vernum <= elf_tdata (abfd)->cverdefs) | |
1874 | version_string = | |
1875 | elf_tdata (abfd)->verdef[vernum - 1].vd_nodename; | |
1876 | else | |
1877 | { | |
1878 | Elf_Internal_Verneed *t; | |
1879 | ||
7a815dd5 | 1880 | version_string = _("<corrupt>"); |
bb4d2ac2 L |
1881 | for (t = elf_tdata (abfd)->verref; |
1882 | t != NULL; | |
1883 | t = t->vn_nextref) | |
1884 | { | |
1885 | Elf_Internal_Vernaux *a; | |
1886 | ||
1887 | for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) | |
1888 | { | |
1889 | if (a->vna_other == vernum) | |
1890 | { | |
1891 | version_string = a->vna_nodename; | |
1892 | break; | |
1893 | } | |
1894 | } | |
1895 | } | |
1896 | } | |
1897 | } | |
1898 | return version_string; | |
1899 | } | |
1900 | ||
252b5132 RH |
1901 | /* Display ELF-specific fields of a symbol. */ |
1902 | ||
1903 | void | |
217aa764 AM |
1904 | bfd_elf_print_symbol (bfd *abfd, |
1905 | void *filep, | |
1906 | asymbol *symbol, | |
1907 | bfd_print_symbol_type how) | |
252b5132 | 1908 | { |
a50b1753 | 1909 | FILE *file = (FILE *) filep; |
252b5132 RH |
1910 | switch (how) |
1911 | { | |
1912 | case bfd_print_symbol_name: | |
1913 | fprintf (file, "%s", symbol->name); | |
1914 | break; | |
1915 | case bfd_print_symbol_more: | |
1916 | fprintf (file, "elf "); | |
60b89a18 | 1917 | bfd_fprintf_vma (abfd, file, symbol->value); |
cd9af601 | 1918 | fprintf (file, " %x", symbol->flags); |
252b5132 RH |
1919 | break; |
1920 | case bfd_print_symbol_all: | |
1921 | { | |
4e8a9624 AM |
1922 | const char *section_name; |
1923 | const char *name = NULL; | |
9c5bfbb7 | 1924 | const struct elf_backend_data *bed; |
7a13edea | 1925 | unsigned char st_other; |
dbb410c3 | 1926 | bfd_vma val; |
bb4d2ac2 L |
1927 | const char *version_string; |
1928 | bfd_boolean hidden; | |
c044fabd | 1929 | |
252b5132 | 1930 | section_name = symbol->section ? symbol->section->name : "(*none*)"; |
587ff49e RH |
1931 | |
1932 | bed = get_elf_backend_data (abfd); | |
1933 | if (bed->elf_backend_print_symbol_all) | |
c044fabd | 1934 | name = (*bed->elf_backend_print_symbol_all) (abfd, filep, symbol); |
587ff49e RH |
1935 | |
1936 | if (name == NULL) | |
1937 | { | |
7ee38065 | 1938 | name = symbol->name; |
217aa764 | 1939 | bfd_print_symbol_vandf (abfd, file, symbol); |
587ff49e RH |
1940 | } |
1941 | ||
252b5132 RH |
1942 | fprintf (file, " %s\t", section_name); |
1943 | /* Print the "other" value for a symbol. For common symbols, | |
1944 | we've already printed the size; now print the alignment. | |
1945 | For other symbols, we have no specified alignment, and | |
1946 | we've printed the address; now print the size. */ | |
dcf6c779 | 1947 | if (symbol->section && bfd_is_com_section (symbol->section)) |
dbb410c3 AM |
1948 | val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_value; |
1949 | else | |
1950 | val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_size; | |
1951 | bfd_fprintf_vma (abfd, file, val); | |
252b5132 RH |
1952 | |
1953 | /* If we have version information, print it. */ | |
60bb06bc L |
1954 | version_string = _bfd_elf_get_symbol_version_string (abfd, |
1955 | symbol, | |
1956 | &hidden); | |
bb4d2ac2 | 1957 | if (version_string) |
252b5132 | 1958 | { |
bb4d2ac2 | 1959 | if (!hidden) |
252b5132 RH |
1960 | fprintf (file, " %-11s", version_string); |
1961 | else | |
1962 | { | |
1963 | int i; | |
1964 | ||
1965 | fprintf (file, " (%s)", version_string); | |
1966 | for (i = 10 - strlen (version_string); i > 0; --i) | |
1967 | putc (' ', file); | |
1968 | } | |
1969 | } | |
1970 | ||
1971 | /* If the st_other field is not zero, print it. */ | |
7a13edea | 1972 | st_other = ((elf_symbol_type *) symbol)->internal_elf_sym.st_other; |
c044fabd | 1973 | |
7a13edea NC |
1974 | switch (st_other) |
1975 | { | |
1976 | case 0: break; | |
1977 | case STV_INTERNAL: fprintf (file, " .internal"); break; | |
1978 | case STV_HIDDEN: fprintf (file, " .hidden"); break; | |
1979 | case STV_PROTECTED: fprintf (file, " .protected"); break; | |
1980 | default: | |
1981 | /* Some other non-defined flags are also present, so print | |
1982 | everything hex. */ | |
1983 | fprintf (file, " 0x%02x", (unsigned int) st_other); | |
1984 | } | |
252b5132 | 1985 | |
587ff49e | 1986 | fprintf (file, " %s", name); |
252b5132 RH |
1987 | } |
1988 | break; | |
1989 | } | |
1990 | } | |
252b5132 RH |
1991 | \f |
1992 | /* ELF .o/exec file reading */ | |
1993 | ||
c044fabd | 1994 | /* Create a new bfd section from an ELF section header. */ |
252b5132 | 1995 | |
b34976b6 | 1996 | bfd_boolean |
217aa764 | 1997 | bfd_section_from_shdr (bfd *abfd, unsigned int shindex) |
252b5132 | 1998 | { |
4fbb74a6 AM |
1999 | Elf_Internal_Shdr *hdr; |
2000 | Elf_Internal_Ehdr *ehdr; | |
2001 | const struct elf_backend_data *bed; | |
90937f86 | 2002 | const char *name; |
bf67003b NC |
2003 | bfd_boolean ret = TRUE; |
2004 | static bfd_boolean * sections_being_created = NULL; | |
5a4b0ccc | 2005 | static bfd * sections_being_created_abfd = NULL; |
bf67003b | 2006 | static unsigned int nesting = 0; |
252b5132 | 2007 | |
4fbb74a6 AM |
2008 | if (shindex >= elf_numsections (abfd)) |
2009 | return FALSE; | |
2010 | ||
bf67003b NC |
2011 | if (++ nesting > 3) |
2012 | { | |
2013 | /* PR17512: A corrupt ELF binary might contain a recursive group of | |
67ce483b | 2014 | sections, with each the string indices pointing to the next in the |
bf67003b NC |
2015 | loop. Detect this here, by refusing to load a section that we are |
2016 | already in the process of loading. We only trigger this test if | |
2017 | we have nested at least three sections deep as normal ELF binaries | |
5a4b0ccc NC |
2018 | can expect to recurse at least once. |
2019 | ||
2020 | FIXME: It would be better if this array was attached to the bfd, | |
2021 | rather than being held in a static pointer. */ | |
2022 | ||
2023 | if (sections_being_created_abfd != abfd) | |
2024 | sections_being_created = NULL; | |
bf67003b NC |
2025 | if (sections_being_created == NULL) |
2026 | { | |
2027 | /* FIXME: It would be more efficient to attach this array to the bfd somehow. */ | |
2028 | sections_being_created = (bfd_boolean *) | |
2029 | bfd_zalloc (abfd, elf_numsections (abfd) * sizeof (bfd_boolean)); | |
5a4b0ccc | 2030 | sections_being_created_abfd = abfd; |
bf67003b NC |
2031 | } |
2032 | if (sections_being_created [shindex]) | |
2033 | { | |
4eca0228 | 2034 | _bfd_error_handler |
871b3ab2 | 2035 | (_("%pB: warning: loop in section dependencies detected"), abfd); |
bf67003b NC |
2036 | return FALSE; |
2037 | } | |
2038 | sections_being_created [shindex] = TRUE; | |
2039 | } | |
2040 | ||
4fbb74a6 AM |
2041 | hdr = elf_elfsections (abfd)[shindex]; |
2042 | ehdr = elf_elfheader (abfd); | |
2043 | name = bfd_elf_string_from_elf_section (abfd, ehdr->e_shstrndx, | |
1b3a8575 | 2044 | hdr->sh_name); |
933d961a | 2045 | if (name == NULL) |
bf67003b | 2046 | goto fail; |
252b5132 | 2047 | |
4fbb74a6 | 2048 | bed = get_elf_backend_data (abfd); |
252b5132 RH |
2049 | switch (hdr->sh_type) |
2050 | { | |
2051 | case SHT_NULL: | |
2052 | /* Inactive section. Throw it away. */ | |
bf67003b | 2053 | goto success; |
252b5132 | 2054 | |
bf67003b NC |
2055 | case SHT_PROGBITS: /* Normal section with contents. */ |
2056 | case SHT_NOBITS: /* .bss section. */ | |
2057 | case SHT_HASH: /* .hash section. */ | |
2058 | case SHT_NOTE: /* .note section. */ | |
25e27870 L |
2059 | case SHT_INIT_ARRAY: /* .init_array section. */ |
2060 | case SHT_FINI_ARRAY: /* .fini_array section. */ | |
2061 | case SHT_PREINIT_ARRAY: /* .preinit_array section. */ | |
7f1204bb | 2062 | case SHT_GNU_LIBLIST: /* .gnu.liblist section. */ |
fdc90cb4 | 2063 | case SHT_GNU_HASH: /* .gnu.hash section. */ |
bf67003b NC |
2064 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); |
2065 | goto success; | |
252b5132 | 2066 | |
797fc050 | 2067 | case SHT_DYNAMIC: /* Dynamic linking information. */ |
6dc132d9 | 2068 | if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex)) |
bf67003b NC |
2069 | goto fail; |
2070 | ||
cfcac11d NC |
2071 | if (hdr->sh_link > elf_numsections (abfd)) |
2072 | { | |
caa83f8b | 2073 | /* PR 10478: Accept Solaris binaries with a sh_link |
cfcac11d NC |
2074 | field set to SHN_BEFORE or SHN_AFTER. */ |
2075 | switch (bfd_get_arch (abfd)) | |
2076 | { | |
caa83f8b | 2077 | case bfd_arch_i386: |
cfcac11d NC |
2078 | case bfd_arch_sparc: |
2079 | if (hdr->sh_link == (SHN_LORESERVE & 0xffff) /* SHN_BEFORE */ | |
2080 | || hdr->sh_link == ((SHN_LORESERVE + 1) & 0xffff) /* SHN_AFTER */) | |
2081 | break; | |
2082 | /* Otherwise fall through. */ | |
2083 | default: | |
bf67003b | 2084 | goto fail; |
cfcac11d NC |
2085 | } |
2086 | } | |
2087 | else if (elf_elfsections (abfd)[hdr->sh_link] == NULL) | |
bf67003b | 2088 | goto fail; |
cfcac11d | 2089 | else if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_STRTAB) |
797fc050 AM |
2090 | { |
2091 | Elf_Internal_Shdr *dynsymhdr; | |
2092 | ||
2093 | /* The shared libraries distributed with hpux11 have a bogus | |
2094 | sh_link field for the ".dynamic" section. Find the | |
2095 | string table for the ".dynsym" section instead. */ | |
2096 | if (elf_dynsymtab (abfd) != 0) | |
2097 | { | |
2098 | dynsymhdr = elf_elfsections (abfd)[elf_dynsymtab (abfd)]; | |
2099 | hdr->sh_link = dynsymhdr->sh_link; | |
2100 | } | |
2101 | else | |
2102 | { | |
2103 | unsigned int i, num_sec; | |
2104 | ||
2105 | num_sec = elf_numsections (abfd); | |
2106 | for (i = 1; i < num_sec; i++) | |
2107 | { | |
2108 | dynsymhdr = elf_elfsections (abfd)[i]; | |
2109 | if (dynsymhdr->sh_type == SHT_DYNSYM) | |
2110 | { | |
2111 | hdr->sh_link = dynsymhdr->sh_link; | |
2112 | break; | |
2113 | } | |
2114 | } | |
2115 | } | |
2116 | } | |
bf67003b | 2117 | goto success; |
797fc050 | 2118 | |
bf67003b | 2119 | case SHT_SYMTAB: /* A symbol table. */ |
252b5132 | 2120 | if (elf_onesymtab (abfd) == shindex) |
bf67003b | 2121 | goto success; |
252b5132 | 2122 | |
a50b2160 | 2123 | if (hdr->sh_entsize != bed->s->sizeof_sym) |
bf67003b NC |
2124 | goto fail; |
2125 | ||
3337c1e5 | 2126 | if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size) |
eee3b786 AM |
2127 | { |
2128 | if (hdr->sh_size != 0) | |
bf67003b | 2129 | goto fail; |
eee3b786 AM |
2130 | /* Some assemblers erroneously set sh_info to one with a |
2131 | zero sh_size. ld sees this as a global symbol count | |
2132 | of (unsigned) -1. Fix it here. */ | |
2133 | hdr->sh_info = 0; | |
bf67003b | 2134 | goto success; |
eee3b786 | 2135 | } |
bf67003b | 2136 | |
16ad13ec NC |
2137 | /* PR 18854: A binary might contain more than one symbol table. |
2138 | Unusual, but possible. Warn, but continue. */ | |
2139 | if (elf_onesymtab (abfd) != 0) | |
2140 | { | |
4eca0228 | 2141 | _bfd_error_handler |
695344c0 | 2142 | /* xgettext:c-format */ |
871b3ab2 | 2143 | (_("%pB: warning: multiple symbol tables detected" |
63a5468a | 2144 | " - ignoring the table in section %u"), |
16ad13ec NC |
2145 | abfd, shindex); |
2146 | goto success; | |
2147 | } | |
252b5132 | 2148 | elf_onesymtab (abfd) = shindex; |
6a40cf0c NC |
2149 | elf_symtab_hdr (abfd) = *hdr; |
2150 | elf_elfsections (abfd)[shindex] = hdr = & elf_symtab_hdr (abfd); | |
252b5132 RH |
2151 | abfd->flags |= HAS_SYMS; |
2152 | ||
2153 | /* Sometimes a shared object will map in the symbol table. If | |
08a40648 AM |
2154 | SHF_ALLOC is set, and this is a shared object, then we also |
2155 | treat this section as a BFD section. We can not base the | |
2156 | decision purely on SHF_ALLOC, because that flag is sometimes | |
2157 | set in a relocatable object file, which would confuse the | |
2158 | linker. */ | |
252b5132 RH |
2159 | if ((hdr->sh_flags & SHF_ALLOC) != 0 |
2160 | && (abfd->flags & DYNAMIC) != 0 | |
6dc132d9 L |
2161 | && ! _bfd_elf_make_section_from_shdr (abfd, hdr, name, |
2162 | shindex)) | |
bf67003b | 2163 | goto fail; |
252b5132 | 2164 | |
1b3a8575 AM |
2165 | /* Go looking for SHT_SYMTAB_SHNDX too, since if there is one we |
2166 | can't read symbols without that section loaded as well. It | |
2167 | is most likely specified by the next section header. */ | |
6a40cf0c NC |
2168 | { |
2169 | elf_section_list * entry; | |
2170 | unsigned int i, num_sec; | |
1b3a8575 | 2171 | |
6a40cf0c NC |
2172 | for (entry = elf_symtab_shndx_list (abfd); entry != NULL; entry = entry->next) |
2173 | if (entry->hdr.sh_link == shindex) | |
2174 | goto success; | |
2175 | ||
2176 | num_sec = elf_numsections (abfd); | |
2177 | for (i = shindex + 1; i < num_sec; i++) | |
2178 | { | |
2179 | Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i]; | |
2180 | ||
2181 | if (hdr2->sh_type == SHT_SYMTAB_SHNDX | |
2182 | && hdr2->sh_link == shindex) | |
2183 | break; | |
2184 | } | |
2185 | ||
2186 | if (i == num_sec) | |
2187 | for (i = 1; i < shindex; i++) | |
1b3a8575 AM |
2188 | { |
2189 | Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i]; | |
6a40cf0c | 2190 | |
1b3a8575 AM |
2191 | if (hdr2->sh_type == SHT_SYMTAB_SHNDX |
2192 | && hdr2->sh_link == shindex) | |
2193 | break; | |
2194 | } | |
6a40cf0c NC |
2195 | |
2196 | if (i != shindex) | |
2197 | ret = bfd_section_from_shdr (abfd, i); | |
2198 | /* else FIXME: we have failed to find the symbol table - should we issue an error ? */ | |
2199 | goto success; | |
2200 | } | |
252b5132 | 2201 | |
bf67003b | 2202 | case SHT_DYNSYM: /* A dynamic symbol table. */ |
252b5132 | 2203 | if (elf_dynsymtab (abfd) == shindex) |
bf67003b | 2204 | goto success; |
252b5132 | 2205 | |
a50b2160 | 2206 | if (hdr->sh_entsize != bed->s->sizeof_sym) |
bf67003b NC |
2207 | goto fail; |
2208 | ||
eee3b786 AM |
2209 | if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size) |
2210 | { | |
2211 | if (hdr->sh_size != 0) | |
bf67003b NC |
2212 | goto fail; |
2213 | ||
eee3b786 AM |
2214 | /* Some linkers erroneously set sh_info to one with a |
2215 | zero sh_size. ld sees this as a global symbol count | |
2216 | of (unsigned) -1. Fix it here. */ | |
2217 | hdr->sh_info = 0; | |
bf67003b | 2218 | goto success; |
eee3b786 | 2219 | } |
bf67003b | 2220 | |
16ad13ec NC |
2221 | /* PR 18854: A binary might contain more than one dynamic symbol table. |
2222 | Unusual, but possible. Warn, but continue. */ | |
2223 | if (elf_dynsymtab (abfd) != 0) | |
2224 | { | |
4eca0228 | 2225 | _bfd_error_handler |
695344c0 | 2226 | /* xgettext:c-format */ |
871b3ab2 | 2227 | (_("%pB: warning: multiple dynamic symbol tables detected" |
63a5468a | 2228 | " - ignoring the table in section %u"), |
16ad13ec NC |
2229 | abfd, shindex); |
2230 | goto success; | |
2231 | } | |
252b5132 RH |
2232 | elf_dynsymtab (abfd) = shindex; |
2233 | elf_tdata (abfd)->dynsymtab_hdr = *hdr; | |
2234 | elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->dynsymtab_hdr; | |
2235 | abfd->flags |= HAS_SYMS; | |
2236 | ||
2237 | /* Besides being a symbol table, we also treat this as a regular | |
2238 | section, so that objcopy can handle it. */ | |
bf67003b NC |
2239 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); |
2240 | goto success; | |
252b5132 | 2241 | |
bf67003b | 2242 | case SHT_SYMTAB_SHNDX: /* Symbol section indices when >64k sections. */ |
6a40cf0c NC |
2243 | { |
2244 | elf_section_list * entry; | |
9ad5cbcf | 2245 | |
6a40cf0c NC |
2246 | for (entry = elf_symtab_shndx_list (abfd); entry != NULL; entry = entry->next) |
2247 | if (entry->ndx == shindex) | |
2248 | goto success; | |
07d6d2b8 | 2249 | |
6a40cf0c NC |
2250 | entry = bfd_alloc (abfd, sizeof * entry); |
2251 | if (entry == NULL) | |
2252 | goto fail; | |
2253 | entry->ndx = shindex; | |
2254 | entry->hdr = * hdr; | |
2255 | entry->next = elf_symtab_shndx_list (abfd); | |
2256 | elf_symtab_shndx_list (abfd) = entry; | |
2257 | elf_elfsections (abfd)[shindex] = & entry->hdr; | |
2258 | goto success; | |
2259 | } | |
9ad5cbcf | 2260 | |
bf67003b | 2261 | case SHT_STRTAB: /* A string table. */ |
252b5132 | 2262 | if (hdr->bfd_section != NULL) |
bf67003b NC |
2263 | goto success; |
2264 | ||
252b5132 RH |
2265 | if (ehdr->e_shstrndx == shindex) |
2266 | { | |
2267 | elf_tdata (abfd)->shstrtab_hdr = *hdr; | |
2268 | elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->shstrtab_hdr; | |
bf67003b | 2269 | goto success; |
252b5132 | 2270 | } |
bf67003b | 2271 | |
1b3a8575 AM |
2272 | if (elf_elfsections (abfd)[elf_onesymtab (abfd)]->sh_link == shindex) |
2273 | { | |
2274 | symtab_strtab: | |
2275 | elf_tdata (abfd)->strtab_hdr = *hdr; | |
2276 | elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->strtab_hdr; | |
bf67003b | 2277 | goto success; |
1b3a8575 | 2278 | } |
bf67003b | 2279 | |
1b3a8575 AM |
2280 | if (elf_elfsections (abfd)[elf_dynsymtab (abfd)]->sh_link == shindex) |
2281 | { | |
2282 | dynsymtab_strtab: | |
2283 | elf_tdata (abfd)->dynstrtab_hdr = *hdr; | |
2284 | hdr = &elf_tdata (abfd)->dynstrtab_hdr; | |
2285 | elf_elfsections (abfd)[shindex] = hdr; | |
2286 | /* We also treat this as a regular section, so that objcopy | |
2287 | can handle it. */ | |
bf67003b NC |
2288 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, |
2289 | shindex); | |
2290 | goto success; | |
1b3a8575 | 2291 | } |
252b5132 | 2292 | |
1b3a8575 AM |
2293 | /* If the string table isn't one of the above, then treat it as a |
2294 | regular section. We need to scan all the headers to be sure, | |
2295 | just in case this strtab section appeared before the above. */ | |
2296 | if (elf_onesymtab (abfd) == 0 || elf_dynsymtab (abfd) == 0) | |
2297 | { | |
2298 | unsigned int i, num_sec; | |
252b5132 | 2299 | |
1b3a8575 AM |
2300 | num_sec = elf_numsections (abfd); |
2301 | for (i = 1; i < num_sec; i++) | |
2302 | { | |
2303 | Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i]; | |
2304 | if (hdr2->sh_link == shindex) | |
2305 | { | |
933d961a JJ |
2306 | /* Prevent endless recursion on broken objects. */ |
2307 | if (i == shindex) | |
bf67003b | 2308 | goto fail; |
1b3a8575 | 2309 | if (! bfd_section_from_shdr (abfd, i)) |
bf67003b | 2310 | goto fail; |
1b3a8575 AM |
2311 | if (elf_onesymtab (abfd) == i) |
2312 | goto symtab_strtab; | |
2313 | if (elf_dynsymtab (abfd) == i) | |
2314 | goto dynsymtab_strtab; | |
2315 | } | |
2316 | } | |
2317 | } | |
bf67003b NC |
2318 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); |
2319 | goto success; | |
252b5132 RH |
2320 | |
2321 | case SHT_REL: | |
2322 | case SHT_RELA: | |
2323 | /* *These* do a lot of work -- but build no sections! */ | |
2324 | { | |
2325 | asection *target_sect; | |
d4730f92 | 2326 | Elf_Internal_Shdr *hdr2, **p_hdr; |
9ad5cbcf | 2327 | unsigned int num_sec = elf_numsections (abfd); |
d4730f92 | 2328 | struct bfd_elf_section_data *esdt; |
252b5132 | 2329 | |
aa2ca951 JJ |
2330 | if (hdr->sh_entsize |
2331 | != (bfd_size_type) (hdr->sh_type == SHT_REL | |
a50b2160 | 2332 | ? bed->s->sizeof_rel : bed->s->sizeof_rela)) |
bf67003b | 2333 | goto fail; |
a50b2160 | 2334 | |
03ae5f59 | 2335 | /* Check for a bogus link to avoid crashing. */ |
4fbb74a6 | 2336 | if (hdr->sh_link >= num_sec) |
03ae5f59 | 2337 | { |
4eca0228 | 2338 | _bfd_error_handler |
695344c0 | 2339 | /* xgettext:c-format */ |
871b3ab2 | 2340 | (_("%pB: invalid link %u for reloc section %s (index %u)"), |
4eca0228 | 2341 | abfd, hdr->sh_link, name, shindex); |
bf67003b NC |
2342 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, |
2343 | shindex); | |
2344 | goto success; | |
03ae5f59 ILT |
2345 | } |
2346 | ||
252b5132 RH |
2347 | /* For some incomprehensible reason Oracle distributes |
2348 | libraries for Solaris in which some of the objects have | |
2349 | bogus sh_link fields. It would be nice if we could just | |
2350 | reject them, but, unfortunately, some people need to use | |
2351 | them. We scan through the section headers; if we find only | |
2352 | one suitable symbol table, we clobber the sh_link to point | |
83b89087 L |
2353 | to it. I hope this doesn't break anything. |
2354 | ||
2355 | Don't do it on executable nor shared library. */ | |
2356 | if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0 | |
2357 | && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_SYMTAB | |
252b5132 RH |
2358 | && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_DYNSYM) |
2359 | { | |
9ad5cbcf | 2360 | unsigned int scan; |
252b5132 RH |
2361 | int found; |
2362 | ||
2363 | found = 0; | |
9ad5cbcf | 2364 | for (scan = 1; scan < num_sec; scan++) |
252b5132 RH |
2365 | { |
2366 | if (elf_elfsections (abfd)[scan]->sh_type == SHT_SYMTAB | |
2367 | || elf_elfsections (abfd)[scan]->sh_type == SHT_DYNSYM) | |
2368 | { | |
2369 | if (found != 0) | |
2370 | { | |
2371 | found = 0; | |
2372 | break; | |
2373 | } | |
2374 | found = scan; | |
2375 | } | |
2376 | } | |
2377 | if (found != 0) | |
2378 | hdr->sh_link = found; | |
2379 | } | |
2380 | ||
2381 | /* Get the symbol table. */ | |
1b3a8575 AM |
2382 | if ((elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_SYMTAB |
2383 | || elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_DYNSYM) | |
252b5132 | 2384 | && ! bfd_section_from_shdr (abfd, hdr->sh_link)) |
bf67003b | 2385 | goto fail; |
252b5132 | 2386 | |
a4bcd733 AM |
2387 | /* If this is an alloc section in an executable or shared |
2388 | library, or the reloc section does not use the main symbol | |
2389 | table we don't treat it as a reloc section. BFD can't | |
2390 | adequately represent such a section, so at least for now, | |
2391 | we don't try. We just present it as a normal section. We | |
2392 | also can't use it as a reloc section if it points to the | |
2393 | null section, an invalid section, another reloc section, or | |
2394 | its sh_link points to the null section. */ | |
2395 | if (((abfd->flags & (DYNAMIC | EXEC_P)) != 0 | |
2396 | && (hdr->sh_flags & SHF_ALLOC) != 0) | |
83b89087 | 2397 | || hdr->sh_link == SHN_UNDEF |
a4bcd733 | 2398 | || hdr->sh_link != elf_onesymtab (abfd) |
185ef66d | 2399 | || hdr->sh_info == SHN_UNDEF |
185ef66d AM |
2400 | || hdr->sh_info >= num_sec |
2401 | || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_REL | |
2402 | || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_RELA) | |
bf67003b NC |
2403 | { |
2404 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, | |
2405 | shindex); | |
2406 | goto success; | |
2407 | } | |
252b5132 RH |
2408 | |
2409 | if (! bfd_section_from_shdr (abfd, hdr->sh_info)) | |
bf67003b NC |
2410 | goto fail; |
2411 | ||
252b5132 RH |
2412 | target_sect = bfd_section_from_elf_index (abfd, hdr->sh_info); |
2413 | if (target_sect == NULL) | |
bf67003b | 2414 | goto fail; |
252b5132 | 2415 | |
d4730f92 BS |
2416 | esdt = elf_section_data (target_sect); |
2417 | if (hdr->sh_type == SHT_RELA) | |
2418 | p_hdr = &esdt->rela.hdr; | |
252b5132 | 2419 | else |
d4730f92 BS |
2420 | p_hdr = &esdt->rel.hdr; |
2421 | ||
06614111 NC |
2422 | /* PR 17512: file: 0b4f81b7. */ |
2423 | if (*p_hdr != NULL) | |
2424 | goto fail; | |
ef53be89 | 2425 | hdr2 = (Elf_Internal_Shdr *) bfd_alloc (abfd, sizeof (*hdr2)); |
d4730f92 | 2426 | if (hdr2 == NULL) |
bf67003b | 2427 | goto fail; |
252b5132 | 2428 | *hdr2 = *hdr; |
d4730f92 | 2429 | *p_hdr = hdr2; |
252b5132 | 2430 | elf_elfsections (abfd)[shindex] = hdr2; |
056bafd4 MR |
2431 | target_sect->reloc_count += (NUM_SHDR_ENTRIES (hdr) |
2432 | * bed->s->int_rels_per_ext_rel); | |
252b5132 RH |
2433 | target_sect->flags |= SEC_RELOC; |
2434 | target_sect->relocation = NULL; | |
2435 | target_sect->rel_filepos = hdr->sh_offset; | |
bf572ba0 MM |
2436 | /* In the section to which the relocations apply, mark whether |
2437 | its relocations are of the REL or RELA variety. */ | |
72730e0c | 2438 | if (hdr->sh_size != 0) |
d4730f92 BS |
2439 | { |
2440 | if (hdr->sh_type == SHT_RELA) | |
2441 | target_sect->use_rela_p = 1; | |
2442 | } | |
252b5132 | 2443 | abfd->flags |= HAS_RELOC; |
bf67003b | 2444 | goto success; |
252b5132 | 2445 | } |
252b5132 RH |
2446 | |
2447 | case SHT_GNU_verdef: | |
2448 | elf_dynverdef (abfd) = shindex; | |
2449 | elf_tdata (abfd)->dynverdef_hdr = *hdr; | |
bf67003b NC |
2450 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); |
2451 | goto success; | |
252b5132 RH |
2452 | |
2453 | case SHT_GNU_versym: | |
a50b2160 | 2454 | if (hdr->sh_entsize != sizeof (Elf_External_Versym)) |
bf67003b NC |
2455 | goto fail; |
2456 | ||
252b5132 RH |
2457 | elf_dynversym (abfd) = shindex; |
2458 | elf_tdata (abfd)->dynversym_hdr = *hdr; | |
bf67003b NC |
2459 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); |
2460 | goto success; | |
252b5132 RH |
2461 | |
2462 | case SHT_GNU_verneed: | |
2463 | elf_dynverref (abfd) = shindex; | |
2464 | elf_tdata (abfd)->dynverref_hdr = *hdr; | |
bf67003b NC |
2465 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); |
2466 | goto success; | |
252b5132 RH |
2467 | |
2468 | case SHT_SHLIB: | |
bf67003b | 2469 | goto success; |
252b5132 | 2470 | |
dbb410c3 | 2471 | case SHT_GROUP: |
44534af3 | 2472 | if (! IS_VALID_GROUP_SECTION_HEADER (hdr, GRP_ENTRY_SIZE)) |
bf67003b NC |
2473 | goto fail; |
2474 | ||
6dc132d9 | 2475 | if (!_bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex)) |
bf67003b NC |
2476 | goto fail; |
2477 | ||
bf67003b | 2478 | goto success; |
dbb410c3 | 2479 | |
252b5132 | 2480 | default: |
104d59d1 JM |
2481 | /* Possibly an attributes section. */ |
2482 | if (hdr->sh_type == SHT_GNU_ATTRIBUTES | |
2483 | || hdr->sh_type == bed->obj_attrs_section_type) | |
2484 | { | |
2485 | if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex)) | |
bf67003b | 2486 | goto fail; |
104d59d1 | 2487 | _bfd_elf_parse_attributes (abfd, hdr); |
bf67003b | 2488 | goto success; |
104d59d1 JM |
2489 | } |
2490 | ||
252b5132 | 2491 | /* Check for any processor-specific section types. */ |
3eb70a79 | 2492 | if (bed->elf_backend_section_from_shdr (abfd, hdr, name, shindex)) |
bf67003b | 2493 | goto success; |
3eb70a79 L |
2494 | |
2495 | if (hdr->sh_type >= SHT_LOUSER && hdr->sh_type <= SHT_HIUSER) | |
2496 | { | |
2497 | if ((hdr->sh_flags & SHF_ALLOC) != 0) | |
2498 | /* FIXME: How to properly handle allocated section reserved | |
2499 | for applications? */ | |
4eca0228 | 2500 | _bfd_error_handler |
695344c0 | 2501 | /* xgettext:c-format */ |
871b3ab2 | 2502 | (_("%pB: unknown type [%#x] section `%s'"), |
76cfced5 | 2503 | abfd, hdr->sh_type, name); |
3eb70a79 | 2504 | else |
bf67003b NC |
2505 | { |
2506 | /* Allow sections reserved for applications. */ | |
2507 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, | |
2508 | shindex); | |
2509 | goto success; | |
2510 | } | |
3eb70a79 L |
2511 | } |
2512 | else if (hdr->sh_type >= SHT_LOPROC | |
2513 | && hdr->sh_type <= SHT_HIPROC) | |
2514 | /* FIXME: We should handle this section. */ | |
4eca0228 | 2515 | _bfd_error_handler |
695344c0 | 2516 | /* xgettext:c-format */ |
871b3ab2 | 2517 | (_("%pB: unknown type [%#x] section `%s'"), |
76cfced5 | 2518 | abfd, hdr->sh_type, name); |
3eb70a79 | 2519 | else if (hdr->sh_type >= SHT_LOOS && hdr->sh_type <= SHT_HIOS) |
ff15b240 NC |
2520 | { |
2521 | /* Unrecognised OS-specific sections. */ | |
2522 | if ((hdr->sh_flags & SHF_OS_NONCONFORMING) != 0) | |
2523 | /* SHF_OS_NONCONFORMING indicates that special knowledge is | |
08a40648 | 2524 | required to correctly process the section and the file should |
ff15b240 | 2525 | be rejected with an error message. */ |
4eca0228 | 2526 | _bfd_error_handler |
695344c0 | 2527 | /* xgettext:c-format */ |
871b3ab2 | 2528 | (_("%pB: unknown type [%#x] section `%s'"), |
76cfced5 | 2529 | abfd, hdr->sh_type, name); |
ff15b240 | 2530 | else |
bf67003b NC |
2531 | { |
2532 | /* Otherwise it should be processed. */ | |
2533 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); | |
2534 | goto success; | |
2535 | } | |
ff15b240 | 2536 | } |
3eb70a79 L |
2537 | else |
2538 | /* FIXME: We should handle this section. */ | |
4eca0228 | 2539 | _bfd_error_handler |
695344c0 | 2540 | /* xgettext:c-format */ |
871b3ab2 | 2541 | (_("%pB: unknown type [%#x] section `%s'"), |
76cfced5 | 2542 | abfd, hdr->sh_type, name); |
3eb70a79 | 2543 | |
bf67003b | 2544 | goto fail; |
252b5132 RH |
2545 | } |
2546 | ||
bf67003b NC |
2547 | fail: |
2548 | ret = FALSE; | |
2549 | success: | |
e5b470e2 | 2550 | if (sections_being_created && sections_being_created_abfd == abfd) |
bf67003b NC |
2551 | sections_being_created [shindex] = FALSE; |
2552 | if (-- nesting == 0) | |
5a4b0ccc NC |
2553 | { |
2554 | sections_being_created = NULL; | |
2555 | sections_being_created_abfd = abfd; | |
2556 | } | |
bf67003b | 2557 | return ret; |
252b5132 RH |
2558 | } |
2559 | ||
87d72d41 | 2560 | /* Return the local symbol specified by ABFD, R_SYMNDX. */ |
ec338859 | 2561 | |
87d72d41 AM |
2562 | Elf_Internal_Sym * |
2563 | bfd_sym_from_r_symndx (struct sym_cache *cache, | |
2564 | bfd *abfd, | |
2565 | unsigned long r_symndx) | |
ec338859 | 2566 | { |
ec338859 AM |
2567 | unsigned int ent = r_symndx % LOCAL_SYM_CACHE_SIZE; |
2568 | ||
a5d1b3b5 AM |
2569 | if (cache->abfd != abfd || cache->indx[ent] != r_symndx) |
2570 | { | |
2571 | Elf_Internal_Shdr *symtab_hdr; | |
2572 | unsigned char esym[sizeof (Elf64_External_Sym)]; | |
2573 | Elf_External_Sym_Shndx eshndx; | |
ec338859 | 2574 | |
a5d1b3b5 AM |
2575 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; |
2576 | if (bfd_elf_get_elf_syms (abfd, symtab_hdr, 1, r_symndx, | |
87d72d41 | 2577 | &cache->sym[ent], esym, &eshndx) == NULL) |
a5d1b3b5 | 2578 | return NULL; |
9ad5cbcf | 2579 | |
a5d1b3b5 AM |
2580 | if (cache->abfd != abfd) |
2581 | { | |
2582 | memset (cache->indx, -1, sizeof (cache->indx)); | |
2583 | cache->abfd = abfd; | |
2584 | } | |
2585 | cache->indx[ent] = r_symndx; | |
ec338859 | 2586 | } |
a5d1b3b5 | 2587 | |
87d72d41 | 2588 | return &cache->sym[ent]; |
ec338859 AM |
2589 | } |
2590 | ||
252b5132 RH |
2591 | /* Given an ELF section number, retrieve the corresponding BFD |
2592 | section. */ | |
2593 | ||
2594 | asection * | |
91d6fa6a | 2595 | bfd_section_from_elf_index (bfd *abfd, unsigned int sec_index) |
252b5132 | 2596 | { |
91d6fa6a | 2597 | if (sec_index >= elf_numsections (abfd)) |
252b5132 | 2598 | return NULL; |
91d6fa6a | 2599 | return elf_elfsections (abfd)[sec_index]->bfd_section; |
252b5132 RH |
2600 | } |
2601 | ||
b35d266b | 2602 | static const struct bfd_elf_special_section special_sections_b[] = |
2f89ff8d | 2603 | { |
0112cd26 | 2604 | { STRING_COMMA_LEN (".bss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE }, |
07d6d2b8 | 2605 | { NULL, 0, 0, 0, 0 } |
7f4d3958 L |
2606 | }; |
2607 | ||
b35d266b | 2608 | static const struct bfd_elf_special_section special_sections_c[] = |
7f4d3958 | 2609 | { |
0112cd26 | 2610 | { STRING_COMMA_LEN (".comment"), 0, SHT_PROGBITS, 0 }, |
07d6d2b8 | 2611 | { NULL, 0, 0, 0, 0 } |
7f4d3958 L |
2612 | }; |
2613 | ||
b35d266b | 2614 | static const struct bfd_elf_special_section special_sections_d[] = |
7f4d3958 | 2615 | { |
07d6d2b8 AM |
2616 | { STRING_COMMA_LEN (".data"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE }, |
2617 | { STRING_COMMA_LEN (".data1"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE }, | |
a9a72a65 DE |
2618 | /* There are more DWARF sections than these, but they needn't be added here |
2619 | unless you have to cope with broken compilers that don't emit section | |
2620 | attributes or you want to help the user writing assembler. */ | |
07d6d2b8 AM |
2621 | { STRING_COMMA_LEN (".debug"), 0, SHT_PROGBITS, 0 }, |
2622 | { STRING_COMMA_LEN (".debug_line"), 0, SHT_PROGBITS, 0 }, | |
2623 | { STRING_COMMA_LEN (".debug_info"), 0, SHT_PROGBITS, 0 }, | |
2624 | { STRING_COMMA_LEN (".debug_abbrev"), 0, SHT_PROGBITS, 0 }, | |
0112cd26 | 2625 | { STRING_COMMA_LEN (".debug_aranges"), 0, SHT_PROGBITS, 0 }, |
07d6d2b8 AM |
2626 | { STRING_COMMA_LEN (".dynamic"), 0, SHT_DYNAMIC, SHF_ALLOC }, |
2627 | { STRING_COMMA_LEN (".dynstr"), 0, SHT_STRTAB, SHF_ALLOC }, | |
2628 | { STRING_COMMA_LEN (".dynsym"), 0, SHT_DYNSYM, SHF_ALLOC }, | |
2629 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2630 | }; |
2631 | ||
b35d266b | 2632 | static const struct bfd_elf_special_section special_sections_f[] = |
7f4d3958 | 2633 | { |
07d6d2b8 | 2634 | { STRING_COMMA_LEN (".fini"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR }, |
6f9dbcd4 | 2635 | { STRING_COMMA_LEN (".fini_array"), -2, SHT_FINI_ARRAY, SHF_ALLOC + SHF_WRITE }, |
07d6d2b8 | 2636 | { NULL, 0 , 0, 0, 0 } |
7f4d3958 L |
2637 | }; |
2638 | ||
b35d266b | 2639 | static const struct bfd_elf_special_section special_sections_g[] = |
7f4d3958 | 2640 | { |
0112cd26 | 2641 | { STRING_COMMA_LEN (".gnu.linkonce.b"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE }, |
07d6d2b8 AM |
2642 | { STRING_COMMA_LEN (".gnu.lto_"), -1, SHT_PROGBITS, SHF_EXCLUDE }, |
2643 | { STRING_COMMA_LEN (".got"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE }, | |
2644 | { STRING_COMMA_LEN (".gnu.version"), 0, SHT_GNU_versym, 0 }, | |
0112cd26 NC |
2645 | { STRING_COMMA_LEN (".gnu.version_d"), 0, SHT_GNU_verdef, 0 }, |
2646 | { STRING_COMMA_LEN (".gnu.version_r"), 0, SHT_GNU_verneed, 0 }, | |
07d6d2b8 AM |
2647 | { STRING_COMMA_LEN (".gnu.liblist"), 0, SHT_GNU_LIBLIST, SHF_ALLOC }, |
2648 | { STRING_COMMA_LEN (".gnu.conflict"), 0, SHT_RELA, SHF_ALLOC }, | |
2649 | { STRING_COMMA_LEN (".gnu.hash"), 0, SHT_GNU_HASH, SHF_ALLOC }, | |
2650 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2651 | }; |
2652 | ||
b35d266b | 2653 | static const struct bfd_elf_special_section special_sections_h[] = |
7f4d3958 | 2654 | { |
07d6d2b8 AM |
2655 | { STRING_COMMA_LEN (".hash"), 0, SHT_HASH, SHF_ALLOC }, |
2656 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2657 | }; |
2658 | ||
b35d266b | 2659 | static const struct bfd_elf_special_section special_sections_i[] = |
7f4d3958 | 2660 | { |
07d6d2b8 | 2661 | { STRING_COMMA_LEN (".init"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR }, |
6f9dbcd4 | 2662 | { STRING_COMMA_LEN (".init_array"), -2, SHT_INIT_ARRAY, SHF_ALLOC + SHF_WRITE }, |
07d6d2b8 AM |
2663 | { STRING_COMMA_LEN (".interp"), 0, SHT_PROGBITS, 0 }, |
2664 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2665 | }; |
2666 | ||
b35d266b | 2667 | static const struct bfd_elf_special_section special_sections_l[] = |
7f4d3958 | 2668 | { |
0112cd26 | 2669 | { STRING_COMMA_LEN (".line"), 0, SHT_PROGBITS, 0 }, |
07d6d2b8 | 2670 | { NULL, 0, 0, 0, 0 } |
7f4d3958 L |
2671 | }; |
2672 | ||
b35d266b | 2673 | static const struct bfd_elf_special_section special_sections_n[] = |
7f4d3958 | 2674 | { |
0112cd26 | 2675 | { STRING_COMMA_LEN (".note.GNU-stack"), 0, SHT_PROGBITS, 0 }, |
07d6d2b8 AM |
2676 | { STRING_COMMA_LEN (".note"), -1, SHT_NOTE, 0 }, |
2677 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2678 | }; |
2679 | ||
b35d266b | 2680 | static const struct bfd_elf_special_section special_sections_p[] = |
7f4d3958 | 2681 | { |
6f9dbcd4 | 2682 | { STRING_COMMA_LEN (".preinit_array"), -2, SHT_PREINIT_ARRAY, SHF_ALLOC + SHF_WRITE }, |
07d6d2b8 AM |
2683 | { STRING_COMMA_LEN (".plt"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR }, |
2684 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2685 | }; |
2686 | ||
b35d266b | 2687 | static const struct bfd_elf_special_section special_sections_r[] = |
7f4d3958 | 2688 | { |
0112cd26 NC |
2689 | { STRING_COMMA_LEN (".rodata"), -2, SHT_PROGBITS, SHF_ALLOC }, |
2690 | { STRING_COMMA_LEN (".rodata1"), 0, SHT_PROGBITS, SHF_ALLOC }, | |
07d6d2b8 AM |
2691 | { STRING_COMMA_LEN (".rela"), -1, SHT_RELA, 0 }, |
2692 | { STRING_COMMA_LEN (".rel"), -1, SHT_REL, 0 }, | |
2693 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2694 | }; |
2695 | ||
b35d266b | 2696 | static const struct bfd_elf_special_section special_sections_s[] = |
7f4d3958 | 2697 | { |
0112cd26 NC |
2698 | { STRING_COMMA_LEN (".shstrtab"), 0, SHT_STRTAB, 0 }, |
2699 | { STRING_COMMA_LEN (".strtab"), 0, SHT_STRTAB, 0 }, | |
2700 | { STRING_COMMA_LEN (".symtab"), 0, SHT_SYMTAB, 0 }, | |
60ff4dc4 HPN |
2701 | /* See struct bfd_elf_special_section declaration for the semantics of |
2702 | this special case where .prefix_length != strlen (.prefix). */ | |
2703 | { ".stabstr", 5, 3, SHT_STRTAB, 0 }, | |
07d6d2b8 | 2704 | { NULL, 0, 0, 0, 0 } |
2f89ff8d L |
2705 | }; |
2706 | ||
b35d266b | 2707 | static const struct bfd_elf_special_section special_sections_t[] = |
7f4d3958 | 2708 | { |
07d6d2b8 AM |
2709 | { STRING_COMMA_LEN (".text"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR }, |
2710 | { STRING_COMMA_LEN (".tbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS }, | |
0112cd26 | 2711 | { STRING_COMMA_LEN (".tdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS }, |
07d6d2b8 | 2712 | { NULL, 0, 0, 0, 0 } |
7f4d3958 L |
2713 | }; |
2714 | ||
1b315056 CS |
2715 | static const struct bfd_elf_special_section special_sections_z[] = |
2716 | { | |
07d6d2b8 AM |
2717 | { STRING_COMMA_LEN (".zdebug_line"), 0, SHT_PROGBITS, 0 }, |
2718 | { STRING_COMMA_LEN (".zdebug_info"), 0, SHT_PROGBITS, 0 }, | |
1b315056 CS |
2719 | { STRING_COMMA_LEN (".zdebug_abbrev"), 0, SHT_PROGBITS, 0 }, |
2720 | { STRING_COMMA_LEN (".zdebug_aranges"), 0, SHT_PROGBITS, 0 }, | |
07d6d2b8 | 2721 | { NULL, 0, 0, 0, 0 } |
1b315056 CS |
2722 | }; |
2723 | ||
e4c93b56 | 2724 | static const struct bfd_elf_special_section * const special_sections[] = |
7f4d3958 | 2725 | { |
7f4d3958 | 2726 | special_sections_b, /* 'b' */ |
98ece1b3 | 2727 | special_sections_c, /* 'c' */ |
7f4d3958 L |
2728 | special_sections_d, /* 'd' */ |
2729 | NULL, /* 'e' */ | |
2730 | special_sections_f, /* 'f' */ | |
2731 | special_sections_g, /* 'g' */ | |
2732 | special_sections_h, /* 'h' */ | |
2733 | special_sections_i, /* 'i' */ | |
2734 | NULL, /* 'j' */ | |
2735 | NULL, /* 'k' */ | |
2736 | special_sections_l, /* 'l' */ | |
2737 | NULL, /* 'm' */ | |
2738 | special_sections_n, /* 'n' */ | |
2739 | NULL, /* 'o' */ | |
2740 | special_sections_p, /* 'p' */ | |
2741 | NULL, /* 'q' */ | |
2742 | special_sections_r, /* 'r' */ | |
2743 | special_sections_s, /* 's' */ | |
2744 | special_sections_t, /* 't' */ | |
1b315056 CS |
2745 | NULL, /* 'u' */ |
2746 | NULL, /* 'v' */ | |
2747 | NULL, /* 'w' */ | |
2748 | NULL, /* 'x' */ | |
2749 | NULL, /* 'y' */ | |
2750 | special_sections_z /* 'z' */ | |
7f4d3958 L |
2751 | }; |
2752 | ||
551b43fd AM |
2753 | const struct bfd_elf_special_section * |
2754 | _bfd_elf_get_special_section (const char *name, | |
2755 | const struct bfd_elf_special_section *spec, | |
2756 | unsigned int rela) | |
2f89ff8d L |
2757 | { |
2758 | int i; | |
7f4d3958 | 2759 | int len; |
7f4d3958 | 2760 | |
551b43fd | 2761 | len = strlen (name); |
7f4d3958 | 2762 | |
551b43fd | 2763 | for (i = 0; spec[i].prefix != NULL; i++) |
7dcb9820 AM |
2764 | { |
2765 | int suffix_len; | |
551b43fd | 2766 | int prefix_len = spec[i].prefix_length; |
7dcb9820 AM |
2767 | |
2768 | if (len < prefix_len) | |
2769 | continue; | |
551b43fd | 2770 | if (memcmp (name, spec[i].prefix, prefix_len) != 0) |
7dcb9820 AM |
2771 | continue; |
2772 | ||
551b43fd | 2773 | suffix_len = spec[i].suffix_length; |
7dcb9820 AM |
2774 | if (suffix_len <= 0) |
2775 | { | |
2776 | if (name[prefix_len] != 0) | |
2777 | { | |
2778 | if (suffix_len == 0) | |
2779 | continue; | |
2780 | if (name[prefix_len] != '.' | |
2781 | && (suffix_len == -2 | |
551b43fd | 2782 | || (rela && spec[i].type == SHT_REL))) |
7dcb9820 AM |
2783 | continue; |
2784 | } | |
2785 | } | |
2786 | else | |
2787 | { | |
2788 | if (len < prefix_len + suffix_len) | |
2789 | continue; | |
2790 | if (memcmp (name + len - suffix_len, | |
551b43fd | 2791 | spec[i].prefix + prefix_len, |
7dcb9820 AM |
2792 | suffix_len) != 0) |
2793 | continue; | |
2794 | } | |
551b43fd | 2795 | return &spec[i]; |
7dcb9820 | 2796 | } |
2f89ff8d L |
2797 | |
2798 | return NULL; | |
2799 | } | |
2800 | ||
7dcb9820 | 2801 | const struct bfd_elf_special_section * |
29ef7005 | 2802 | _bfd_elf_get_sec_type_attr (bfd *abfd, asection *sec) |
2f89ff8d | 2803 | { |
551b43fd AM |
2804 | int i; |
2805 | const struct bfd_elf_special_section *spec; | |
29ef7005 | 2806 | const struct elf_backend_data *bed; |
2f89ff8d L |
2807 | |
2808 | /* See if this is one of the special sections. */ | |
551b43fd AM |
2809 | if (sec->name == NULL) |
2810 | return NULL; | |
2f89ff8d | 2811 | |
29ef7005 L |
2812 | bed = get_elf_backend_data (abfd); |
2813 | spec = bed->special_sections; | |
2814 | if (spec) | |
2815 | { | |
2816 | spec = _bfd_elf_get_special_section (sec->name, | |
2817 | bed->special_sections, | |
2818 | sec->use_rela_p); | |
2819 | if (spec != NULL) | |
2820 | return spec; | |
2821 | } | |
2822 | ||
551b43fd AM |
2823 | if (sec->name[0] != '.') |
2824 | return NULL; | |
2f89ff8d | 2825 | |
551b43fd | 2826 | i = sec->name[1] - 'b'; |
1b315056 | 2827 | if (i < 0 || i > 'z' - 'b') |
551b43fd AM |
2828 | return NULL; |
2829 | ||
2830 | spec = special_sections[i]; | |
2f89ff8d | 2831 | |
551b43fd AM |
2832 | if (spec == NULL) |
2833 | return NULL; | |
2834 | ||
2835 | return _bfd_elf_get_special_section (sec->name, spec, sec->use_rela_p); | |
2f89ff8d L |
2836 | } |
2837 | ||
b34976b6 | 2838 | bfd_boolean |
217aa764 | 2839 | _bfd_elf_new_section_hook (bfd *abfd, asection *sec) |
252b5132 RH |
2840 | { |
2841 | struct bfd_elf_section_data *sdata; | |
551b43fd | 2842 | const struct elf_backend_data *bed; |
7dcb9820 | 2843 | const struct bfd_elf_special_section *ssect; |
252b5132 | 2844 | |
f0abc2a1 AM |
2845 | sdata = (struct bfd_elf_section_data *) sec->used_by_bfd; |
2846 | if (sdata == NULL) | |
2847 | { | |
a50b1753 | 2848 | sdata = (struct bfd_elf_section_data *) bfd_zalloc (abfd, |
07d6d2b8 | 2849 | sizeof (*sdata)); |
f0abc2a1 AM |
2850 | if (sdata == NULL) |
2851 | return FALSE; | |
217aa764 | 2852 | sec->used_by_bfd = sdata; |
f0abc2a1 | 2853 | } |
bf572ba0 | 2854 | |
551b43fd AM |
2855 | /* Indicate whether or not this section should use RELA relocations. */ |
2856 | bed = get_elf_backend_data (abfd); | |
2857 | sec->use_rela_p = bed->default_use_rela_p; | |
2858 | ||
e843e0f8 L |
2859 | /* When we read a file, we don't need to set ELF section type and |
2860 | flags. They will be overridden in _bfd_elf_make_section_from_shdr | |
2861 | anyway. We will set ELF section type and flags for all linker | |
2862 | created sections. If user specifies BFD section flags, we will | |
2863 | set ELF section type and flags based on BFD section flags in | |
02ecc8e9 L |
2864 | elf_fake_sections. Special handling for .init_array/.fini_array |
2865 | output sections since they may contain .ctors/.dtors input | |
2866 | sections. We don't want _bfd_elf_init_private_section_data to | |
2867 | copy ELF section type from .ctors/.dtors input sections. */ | |
2868 | if (abfd->direction != read_direction | |
3496cb2a | 2869 | || (sec->flags & SEC_LINKER_CREATED) != 0) |
2f89ff8d | 2870 | { |
551b43fd | 2871 | ssect = (*bed->get_sec_type_attr) (abfd, sec); |
02ecc8e9 L |
2872 | if (ssect != NULL |
2873 | && (!sec->flags | |
2874 | || (sec->flags & SEC_LINKER_CREATED) != 0 | |
2875 | || ssect->type == SHT_INIT_ARRAY | |
2876 | || ssect->type == SHT_FINI_ARRAY)) | |
a31501e9 L |
2877 | { |
2878 | elf_section_type (sec) = ssect->type; | |
2879 | elf_section_flags (sec) = ssect->attr; | |
2880 | } | |
2f89ff8d L |
2881 | } |
2882 | ||
f592407e | 2883 | return _bfd_generic_new_section_hook (abfd, sec); |
252b5132 RH |
2884 | } |
2885 | ||
2886 | /* Create a new bfd section from an ELF program header. | |
2887 | ||
2888 | Since program segments have no names, we generate a synthetic name | |
2889 | of the form segment<NUM>, where NUM is generally the index in the | |
2890 | program header table. For segments that are split (see below) we | |
2891 | generate the names segment<NUM>a and segment<NUM>b. | |
2892 | ||
2893 | Note that some program segments may have a file size that is different than | |
2894 | (less than) the memory size. All this means is that at execution the | |
2895 | system must allocate the amount of memory specified by the memory size, | |
2896 | but only initialize it with the first "file size" bytes read from the | |
2897 | file. This would occur for example, with program segments consisting | |
2898 | of combined data+bss. | |
2899 | ||
2900 | To handle the above situation, this routine generates TWO bfd sections | |
2901 | for the single program segment. The first has the length specified by | |
2902 | the file size of the segment, and the second has the length specified | |
2903 | by the difference between the two sizes. In effect, the segment is split | |
d5191d0c | 2904 | into its initialized and uninitialized parts. |
252b5132 RH |
2905 | |
2906 | */ | |
2907 | ||
b34976b6 | 2908 | bfd_boolean |
217aa764 AM |
2909 | _bfd_elf_make_section_from_phdr (bfd *abfd, |
2910 | Elf_Internal_Phdr *hdr, | |
91d6fa6a | 2911 | int hdr_index, |
a50b1753 | 2912 | const char *type_name) |
252b5132 RH |
2913 | { |
2914 | asection *newsect; | |
2915 | char *name; | |
2916 | char namebuf[64]; | |
d4c88bbb | 2917 | size_t len; |
252b5132 RH |
2918 | int split; |
2919 | ||
2920 | split = ((hdr->p_memsz > 0) | |
2921 | && (hdr->p_filesz > 0) | |
2922 | && (hdr->p_memsz > hdr->p_filesz)); | |
d5191d0c AM |
2923 | |
2924 | if (hdr->p_filesz > 0) | |
252b5132 | 2925 | { |
91d6fa6a | 2926 | sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "a" : ""); |
d5191d0c | 2927 | len = strlen (namebuf) + 1; |
a50b1753 | 2928 | name = (char *) bfd_alloc (abfd, len); |
d5191d0c AM |
2929 | if (!name) |
2930 | return FALSE; | |
2931 | memcpy (name, namebuf, len); | |
2932 | newsect = bfd_make_section (abfd, name); | |
2933 | if (newsect == NULL) | |
2934 | return FALSE; | |
2935 | newsect->vma = hdr->p_vaddr; | |
2936 | newsect->lma = hdr->p_paddr; | |
2937 | newsect->size = hdr->p_filesz; | |
2938 | newsect->filepos = hdr->p_offset; | |
2939 | newsect->flags |= SEC_HAS_CONTENTS; | |
2940 | newsect->alignment_power = bfd_log2 (hdr->p_align); | |
2941 | if (hdr->p_type == PT_LOAD) | |
252b5132 | 2942 | { |
d5191d0c AM |
2943 | newsect->flags |= SEC_ALLOC; |
2944 | newsect->flags |= SEC_LOAD; | |
2945 | if (hdr->p_flags & PF_X) | |
2946 | { | |
2947 | /* FIXME: all we known is that it has execute PERMISSION, | |
2948 | may be data. */ | |
2949 | newsect->flags |= SEC_CODE; | |
2950 | } | |
2951 | } | |
2952 | if (!(hdr->p_flags & PF_W)) | |
2953 | { | |
2954 | newsect->flags |= SEC_READONLY; | |
252b5132 | 2955 | } |
252b5132 RH |
2956 | } |
2957 | ||
d5191d0c | 2958 | if (hdr->p_memsz > hdr->p_filesz) |
252b5132 | 2959 | { |
d5191d0c AM |
2960 | bfd_vma align; |
2961 | ||
91d6fa6a | 2962 | sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "b" : ""); |
d4c88bbb | 2963 | len = strlen (namebuf) + 1; |
a50b1753 | 2964 | name = (char *) bfd_alloc (abfd, len); |
252b5132 | 2965 | if (!name) |
b34976b6 | 2966 | return FALSE; |
d4c88bbb | 2967 | memcpy (name, namebuf, len); |
252b5132 RH |
2968 | newsect = bfd_make_section (abfd, name); |
2969 | if (newsect == NULL) | |
b34976b6 | 2970 | return FALSE; |
252b5132 RH |
2971 | newsect->vma = hdr->p_vaddr + hdr->p_filesz; |
2972 | newsect->lma = hdr->p_paddr + hdr->p_filesz; | |
eea6121a | 2973 | newsect->size = hdr->p_memsz - hdr->p_filesz; |
d5191d0c AM |
2974 | newsect->filepos = hdr->p_offset + hdr->p_filesz; |
2975 | align = newsect->vma & -newsect->vma; | |
2976 | if (align == 0 || align > hdr->p_align) | |
2977 | align = hdr->p_align; | |
2978 | newsect->alignment_power = bfd_log2 (align); | |
252b5132 RH |
2979 | if (hdr->p_type == PT_LOAD) |
2980 | { | |
d5191d0c AM |
2981 | /* Hack for gdb. Segments that have not been modified do |
2982 | not have their contents written to a core file, on the | |
2983 | assumption that a debugger can find the contents in the | |
2984 | executable. We flag this case by setting the fake | |
2985 | section size to zero. Note that "real" bss sections will | |
2986 | always have their contents dumped to the core file. */ | |
2987 | if (bfd_get_format (abfd) == bfd_core) | |
2988 | newsect->size = 0; | |
252b5132 RH |
2989 | newsect->flags |= SEC_ALLOC; |
2990 | if (hdr->p_flags & PF_X) | |
2991 | newsect->flags |= SEC_CODE; | |
2992 | } | |
2993 | if (!(hdr->p_flags & PF_W)) | |
2994 | newsect->flags |= SEC_READONLY; | |
2995 | } | |
2996 | ||
b34976b6 | 2997 | return TRUE; |
252b5132 RH |
2998 | } |
2999 | ||
b34976b6 | 3000 | bfd_boolean |
91d6fa6a | 3001 | bfd_section_from_phdr (bfd *abfd, Elf_Internal_Phdr *hdr, int hdr_index) |
20cfcaae | 3002 | { |
9c5bfbb7 | 3003 | const struct elf_backend_data *bed; |
20cfcaae NC |
3004 | |
3005 | switch (hdr->p_type) | |
3006 | { | |
3007 | case PT_NULL: | |
91d6fa6a | 3008 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "null"); |
20cfcaae NC |
3009 | |
3010 | case PT_LOAD: | |
91d6fa6a | 3011 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "load"); |
20cfcaae NC |
3012 | |
3013 | case PT_DYNAMIC: | |
91d6fa6a | 3014 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "dynamic"); |
20cfcaae NC |
3015 | |
3016 | case PT_INTERP: | |
91d6fa6a | 3017 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "interp"); |
20cfcaae NC |
3018 | |
3019 | case PT_NOTE: | |
91d6fa6a | 3020 | if (! _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "note")) |
b34976b6 | 3021 | return FALSE; |
276da9b3 L |
3022 | if (! elf_read_notes (abfd, hdr->p_offset, hdr->p_filesz, |
3023 | hdr->p_align)) | |
b34976b6 AM |
3024 | return FALSE; |
3025 | return TRUE; | |
20cfcaae NC |
3026 | |
3027 | case PT_SHLIB: | |
91d6fa6a | 3028 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "shlib"); |
20cfcaae NC |
3029 | |
3030 | case PT_PHDR: | |
91d6fa6a | 3031 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "phdr"); |
20cfcaae | 3032 | |
811072d8 | 3033 | case PT_GNU_EH_FRAME: |
91d6fa6a | 3034 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, |
811072d8 RM |
3035 | "eh_frame_hdr"); |
3036 | ||
2b05f1b7 | 3037 | case PT_GNU_STACK: |
91d6fa6a | 3038 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "stack"); |
9ee5e499 | 3039 | |
8c37241b | 3040 | case PT_GNU_RELRO: |
91d6fa6a | 3041 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "relro"); |
8c37241b | 3042 | |
20cfcaae | 3043 | default: |
8c1acd09 | 3044 | /* Check for any processor-specific program segment types. */ |
20cfcaae | 3045 | bed = get_elf_backend_data (abfd); |
91d6fa6a | 3046 | return bed->elf_backend_section_from_phdr (abfd, hdr, hdr_index, "proc"); |
20cfcaae NC |
3047 | } |
3048 | } | |
3049 | ||
d4730f92 BS |
3050 | /* Return the REL_HDR for SEC, assuming there is only a single one, either |
3051 | REL or RELA. */ | |
3052 | ||
3053 | Elf_Internal_Shdr * | |
3054 | _bfd_elf_single_rel_hdr (asection *sec) | |
3055 | { | |
3056 | if (elf_section_data (sec)->rel.hdr) | |
3057 | { | |
3058 | BFD_ASSERT (elf_section_data (sec)->rela.hdr == NULL); | |
3059 | return elf_section_data (sec)->rel.hdr; | |
3060 | } | |
3061 | else | |
3062 | return elf_section_data (sec)->rela.hdr; | |
3063 | } | |
3064 | ||
3e19fb8f L |
3065 | static bfd_boolean |
3066 | _bfd_elf_set_reloc_sh_name (bfd *abfd, | |
3067 | Elf_Internal_Shdr *rel_hdr, | |
3068 | const char *sec_name, | |
3069 | bfd_boolean use_rela_p) | |
3070 | { | |
3071 | char *name = (char *) bfd_alloc (abfd, | |
3072 | sizeof ".rela" + strlen (sec_name)); | |
3073 | if (name == NULL) | |
3074 | return FALSE; | |
3075 | ||
3076 | sprintf (name, "%s%s", use_rela_p ? ".rela" : ".rel", sec_name); | |
3077 | rel_hdr->sh_name = | |
3078 | (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), name, | |
3079 | FALSE); | |
3080 | if (rel_hdr->sh_name == (unsigned int) -1) | |
3081 | return FALSE; | |
3082 | ||
3083 | return TRUE; | |
3084 | } | |
3085 | ||
d4730f92 BS |
3086 | /* Allocate and initialize a section-header for a new reloc section, |
3087 | containing relocations against ASECT. It is stored in RELDATA. If | |
3088 | USE_RELA_P is TRUE, we use RELA relocations; otherwise, we use REL | |
3089 | relocations. */ | |
23bc299b | 3090 | |
5d13b3b3 | 3091 | static bfd_boolean |
217aa764 | 3092 | _bfd_elf_init_reloc_shdr (bfd *abfd, |
d4730f92 | 3093 | struct bfd_elf_section_reloc_data *reldata, |
f6fe1ccd | 3094 | const char *sec_name, |
3e19fb8f L |
3095 | bfd_boolean use_rela_p, |
3096 | bfd_boolean delay_st_name_p) | |
23bc299b | 3097 | { |
d4730f92 | 3098 | Elf_Internal_Shdr *rel_hdr; |
9c5bfbb7 | 3099 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
d4730f92 | 3100 | |
d4730f92 | 3101 | BFD_ASSERT (reldata->hdr == NULL); |
ef53be89 | 3102 | rel_hdr = bfd_zalloc (abfd, sizeof (*rel_hdr)); |
d4730f92 | 3103 | reldata->hdr = rel_hdr; |
23bc299b | 3104 | |
3e19fb8f L |
3105 | if (delay_st_name_p) |
3106 | rel_hdr->sh_name = (unsigned int) -1; | |
3107 | else if (!_bfd_elf_set_reloc_sh_name (abfd, rel_hdr, sec_name, | |
3108 | use_rela_p)) | |
b34976b6 | 3109 | return FALSE; |
23bc299b MM |
3110 | rel_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL; |
3111 | rel_hdr->sh_entsize = (use_rela_p | |
3112 | ? bed->s->sizeof_rela | |
3113 | : bed->s->sizeof_rel); | |
72de5009 | 3114 | rel_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align; |
28e07a05 | 3115 | rel_hdr->sh_flags = 0; |
23bc299b MM |
3116 | rel_hdr->sh_addr = 0; |
3117 | rel_hdr->sh_size = 0; | |
3118 | rel_hdr->sh_offset = 0; | |
3119 | ||
b34976b6 | 3120 | return TRUE; |
23bc299b MM |
3121 | } |
3122 | ||
94be91de JB |
3123 | /* Return the default section type based on the passed in section flags. */ |
3124 | ||
3125 | int | |
3126 | bfd_elf_get_default_section_type (flagword flags) | |
3127 | { | |
3128 | if ((flags & SEC_ALLOC) != 0 | |
2e76e85a | 3129 | && (flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0) |
94be91de JB |
3130 | return SHT_NOBITS; |
3131 | return SHT_PROGBITS; | |
3132 | } | |
3133 | ||
d4730f92 BS |
3134 | struct fake_section_arg |
3135 | { | |
3136 | struct bfd_link_info *link_info; | |
3137 | bfd_boolean failed; | |
3138 | }; | |
3139 | ||
252b5132 RH |
3140 | /* Set up an ELF internal section header for a section. */ |
3141 | ||
252b5132 | 3142 | static void |
d4730f92 | 3143 | elf_fake_sections (bfd *abfd, asection *asect, void *fsarg) |
252b5132 | 3144 | { |
d4730f92 | 3145 | struct fake_section_arg *arg = (struct fake_section_arg *)fsarg; |
9c5bfbb7 | 3146 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
d4730f92 | 3147 | struct bfd_elf_section_data *esd = elf_section_data (asect); |
252b5132 | 3148 | Elf_Internal_Shdr *this_hdr; |
0414f35b | 3149 | unsigned int sh_type; |
0ce398f1 | 3150 | const char *name = asect->name; |
3e19fb8f | 3151 | bfd_boolean delay_st_name_p = FALSE; |
252b5132 | 3152 | |
d4730f92 | 3153 | if (arg->failed) |
252b5132 RH |
3154 | { |
3155 | /* We already failed; just get out of the bfd_map_over_sections | |
08a40648 | 3156 | loop. */ |
252b5132 RH |
3157 | return; |
3158 | } | |
3159 | ||
d4730f92 | 3160 | this_hdr = &esd->this_hdr; |
252b5132 | 3161 | |
f6fe1ccd | 3162 | if (arg->link_info) |
0ce398f1 | 3163 | { |
f6fe1ccd L |
3164 | /* ld: compress DWARF debug sections with names: .debug_*. */ |
3165 | if ((arg->link_info->compress_debug & COMPRESS_DEBUG) | |
3166 | && (asect->flags & SEC_DEBUGGING) | |
3167 | && name[1] == 'd' | |
3168 | && name[6] == '_') | |
3169 | { | |
3170 | /* Set SEC_ELF_COMPRESS to indicate this section should be | |
3171 | compressed. */ | |
3172 | asect->flags |= SEC_ELF_COMPRESS; | |
0ce398f1 | 3173 | |
dd905818 | 3174 | /* If this section will be compressed, delay adding section |
3e19fb8f L |
3175 | name to section name section after it is compressed in |
3176 | _bfd_elf_assign_file_positions_for_non_load. */ | |
3177 | delay_st_name_p = TRUE; | |
f6fe1ccd L |
3178 | } |
3179 | } | |
3180 | else if ((asect->flags & SEC_ELF_RENAME)) | |
3181 | { | |
3182 | /* objcopy: rename output DWARF debug section. */ | |
3183 | if ((abfd->flags & (BFD_DECOMPRESS | BFD_COMPRESS_GABI))) | |
3184 | { | |
3185 | /* When we decompress or compress with SHF_COMPRESSED, | |
3186 | convert section name from .zdebug_* to .debug_* if | |
3187 | needed. */ | |
3188 | if (name[1] == 'z') | |
3189 | { | |
3190 | char *new_name = convert_zdebug_to_debug (abfd, name); | |
3191 | if (new_name == NULL) | |
3192 | { | |
3193 | arg->failed = TRUE; | |
3194 | return; | |
3195 | } | |
3196 | name = new_name; | |
3197 | } | |
3198 | } | |
3199 | else if (asect->compress_status == COMPRESS_SECTION_DONE) | |
0ce398f1 | 3200 | { |
f6fe1ccd L |
3201 | /* PR binutils/18087: Compression does not always make a |
3202 | section smaller. So only rename the section when | |
3203 | compression has actually taken place. If input section | |
3204 | name is .zdebug_*, we should never compress it again. */ | |
3205 | char *new_name = convert_debug_to_zdebug (abfd, name); | |
0ce398f1 L |
3206 | if (new_name == NULL) |
3207 | { | |
3208 | arg->failed = TRUE; | |
3209 | return; | |
3210 | } | |
f6fe1ccd L |
3211 | BFD_ASSERT (name[1] != 'z'); |
3212 | name = new_name; | |
0ce398f1 L |
3213 | } |
3214 | } | |
3215 | ||
3e19fb8f L |
3216 | if (delay_st_name_p) |
3217 | this_hdr->sh_name = (unsigned int) -1; | |
3218 | else | |
252b5132 | 3219 | { |
3e19fb8f L |
3220 | this_hdr->sh_name |
3221 | = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), | |
3222 | name, FALSE); | |
3223 | if (this_hdr->sh_name == (unsigned int) -1) | |
3224 | { | |
3225 | arg->failed = TRUE; | |
3226 | return; | |
3227 | } | |
252b5132 RH |
3228 | } |
3229 | ||
a4d8e49b | 3230 | /* Don't clear sh_flags. Assembler may set additional bits. */ |
252b5132 RH |
3231 | |
3232 | if ((asect->flags & SEC_ALLOC) != 0 | |
3233 | || asect->user_set_vma) | |
3234 | this_hdr->sh_addr = asect->vma; | |
3235 | else | |
3236 | this_hdr->sh_addr = 0; | |
3237 | ||
3238 | this_hdr->sh_offset = 0; | |
eea6121a | 3239 | this_hdr->sh_size = asect->size; |
252b5132 | 3240 | this_hdr->sh_link = 0; |
c86934ce NC |
3241 | /* PR 17512: file: 0eb809fe, 8b0535ee. */ |
3242 | if (asect->alignment_power >= (sizeof (bfd_vma) * 8) - 1) | |
3243 | { | |
4eca0228 | 3244 | _bfd_error_handler |
695344c0 | 3245 | /* xgettext:c-format */ |
9793eb77 | 3246 | (_("%pB: error: alignment power %d of section `%pA' is too big"), |
c08bb8dd | 3247 | abfd, asect->alignment_power, asect); |
c86934ce NC |
3248 | arg->failed = TRUE; |
3249 | return; | |
3250 | } | |
72de5009 | 3251 | this_hdr->sh_addralign = (bfd_vma) 1 << asect->alignment_power; |
252b5132 RH |
3252 | /* The sh_entsize and sh_info fields may have been set already by |
3253 | copy_private_section_data. */ | |
3254 | ||
3255 | this_hdr->bfd_section = asect; | |
3256 | this_hdr->contents = NULL; | |
3257 | ||
3cddba1e L |
3258 | /* If the section type is unspecified, we set it based on |
3259 | asect->flags. */ | |
98ece1b3 AM |
3260 | if ((asect->flags & SEC_GROUP) != 0) |
3261 | sh_type = SHT_GROUP; | |
98ece1b3 | 3262 | else |
94be91de | 3263 | sh_type = bfd_elf_get_default_section_type (asect->flags); |
98ece1b3 | 3264 | |
3cddba1e | 3265 | if (this_hdr->sh_type == SHT_NULL) |
98ece1b3 AM |
3266 | this_hdr->sh_type = sh_type; |
3267 | else if (this_hdr->sh_type == SHT_NOBITS | |
3268 | && sh_type == SHT_PROGBITS | |
3269 | && (asect->flags & SEC_ALLOC) != 0) | |
3cddba1e | 3270 | { |
98ece1b3 AM |
3271 | /* Warn if we are changing a NOBITS section to PROGBITS, but |
3272 | allow the link to proceed. This can happen when users link | |
3273 | non-bss input sections to bss output sections, or emit data | |
3274 | to a bss output section via a linker script. */ | |
4eca0228 | 3275 | _bfd_error_handler |
871b3ab2 | 3276 | (_("warning: section `%pA' type changed to PROGBITS"), asect); |
98ece1b3 | 3277 | this_hdr->sh_type = sh_type; |
3cddba1e L |
3278 | } |
3279 | ||
2f89ff8d | 3280 | switch (this_hdr->sh_type) |
252b5132 | 3281 | { |
2f89ff8d | 3282 | default: |
2f89ff8d L |
3283 | break; |
3284 | ||
3285 | case SHT_STRTAB: | |
2f89ff8d L |
3286 | case SHT_NOTE: |
3287 | case SHT_NOBITS: | |
3288 | case SHT_PROGBITS: | |
3289 | break; | |
606851fb AM |
3290 | |
3291 | case SHT_INIT_ARRAY: | |
3292 | case SHT_FINI_ARRAY: | |
3293 | case SHT_PREINIT_ARRAY: | |
3294 | this_hdr->sh_entsize = bed->s->arch_size / 8; | |
3295 | break; | |
2f89ff8d L |
3296 | |
3297 | case SHT_HASH: | |
c7ac6ff8 | 3298 | this_hdr->sh_entsize = bed->s->sizeof_hash_entry; |
2f89ff8d | 3299 | break; |
5de3bf90 | 3300 | |
2f89ff8d | 3301 | case SHT_DYNSYM: |
252b5132 | 3302 | this_hdr->sh_entsize = bed->s->sizeof_sym; |
2f89ff8d L |
3303 | break; |
3304 | ||
3305 | case SHT_DYNAMIC: | |
252b5132 | 3306 | this_hdr->sh_entsize = bed->s->sizeof_dyn; |
2f89ff8d L |
3307 | break; |
3308 | ||
3309 | case SHT_RELA: | |
3310 | if (get_elf_backend_data (abfd)->may_use_rela_p) | |
3311 | this_hdr->sh_entsize = bed->s->sizeof_rela; | |
3312 | break; | |
3313 | ||
3314 | case SHT_REL: | |
3315 | if (get_elf_backend_data (abfd)->may_use_rel_p) | |
3316 | this_hdr->sh_entsize = bed->s->sizeof_rel; | |
3317 | break; | |
3318 | ||
3319 | case SHT_GNU_versym: | |
252b5132 | 3320 | this_hdr->sh_entsize = sizeof (Elf_External_Versym); |
2f89ff8d L |
3321 | break; |
3322 | ||
3323 | case SHT_GNU_verdef: | |
252b5132 RH |
3324 | this_hdr->sh_entsize = 0; |
3325 | /* objcopy or strip will copy over sh_info, but may not set | |
08a40648 AM |
3326 | cverdefs. The linker will set cverdefs, but sh_info will be |
3327 | zero. */ | |
252b5132 RH |
3328 | if (this_hdr->sh_info == 0) |
3329 | this_hdr->sh_info = elf_tdata (abfd)->cverdefs; | |
3330 | else | |
3331 | BFD_ASSERT (elf_tdata (abfd)->cverdefs == 0 | |
3332 | || this_hdr->sh_info == elf_tdata (abfd)->cverdefs); | |
2f89ff8d L |
3333 | break; |
3334 | ||
3335 | case SHT_GNU_verneed: | |
252b5132 RH |
3336 | this_hdr->sh_entsize = 0; |
3337 | /* objcopy or strip will copy over sh_info, but may not set | |
08a40648 AM |
3338 | cverrefs. The linker will set cverrefs, but sh_info will be |
3339 | zero. */ | |
252b5132 RH |
3340 | if (this_hdr->sh_info == 0) |
3341 | this_hdr->sh_info = elf_tdata (abfd)->cverrefs; | |
3342 | else | |
3343 | BFD_ASSERT (elf_tdata (abfd)->cverrefs == 0 | |
3344 | || this_hdr->sh_info == elf_tdata (abfd)->cverrefs); | |
2f89ff8d L |
3345 | break; |
3346 | ||
3347 | case SHT_GROUP: | |
1783205a | 3348 | this_hdr->sh_entsize = GRP_ENTRY_SIZE; |
2f89ff8d | 3349 | break; |
fdc90cb4 JJ |
3350 | |
3351 | case SHT_GNU_HASH: | |
3352 | this_hdr->sh_entsize = bed->s->arch_size == 64 ? 0 : 4; | |
3353 | break; | |
dbb410c3 | 3354 | } |
252b5132 RH |
3355 | |
3356 | if ((asect->flags & SEC_ALLOC) != 0) | |
3357 | this_hdr->sh_flags |= SHF_ALLOC; | |
3358 | if ((asect->flags & SEC_READONLY) == 0) | |
3359 | this_hdr->sh_flags |= SHF_WRITE; | |
3360 | if ((asect->flags & SEC_CODE) != 0) | |
3361 | this_hdr->sh_flags |= SHF_EXECINSTR; | |
f5fa8ca2 JJ |
3362 | if ((asect->flags & SEC_MERGE) != 0) |
3363 | { | |
3364 | this_hdr->sh_flags |= SHF_MERGE; | |
3365 | this_hdr->sh_entsize = asect->entsize; | |
f5fa8ca2 | 3366 | } |
84865015 NC |
3367 | if ((asect->flags & SEC_STRINGS) != 0) |
3368 | this_hdr->sh_flags |= SHF_STRINGS; | |
1126897b | 3369 | if ((asect->flags & SEC_GROUP) == 0 && elf_group_name (asect) != NULL) |
dbb410c3 | 3370 | this_hdr->sh_flags |= SHF_GROUP; |
13ae64f3 | 3371 | if ((asect->flags & SEC_THREAD_LOCAL) != 0) |
704afa60 JJ |
3372 | { |
3373 | this_hdr->sh_flags |= SHF_TLS; | |
3a800eb9 AM |
3374 | if (asect->size == 0 |
3375 | && (asect->flags & SEC_HAS_CONTENTS) == 0) | |
704afa60 | 3376 | { |
3a800eb9 | 3377 | struct bfd_link_order *o = asect->map_tail.link_order; |
b34976b6 | 3378 | |
704afa60 | 3379 | this_hdr->sh_size = 0; |
3a800eb9 AM |
3380 | if (o != NULL) |
3381 | { | |
704afa60 | 3382 | this_hdr->sh_size = o->offset + o->size; |
3a800eb9 AM |
3383 | if (this_hdr->sh_size != 0) |
3384 | this_hdr->sh_type = SHT_NOBITS; | |
3385 | } | |
704afa60 JJ |
3386 | } |
3387 | } | |
18ae9cc1 L |
3388 | if ((asect->flags & (SEC_GROUP | SEC_EXCLUDE)) == SEC_EXCLUDE) |
3389 | this_hdr->sh_flags |= SHF_EXCLUDE; | |
252b5132 | 3390 | |
d4730f92 BS |
3391 | /* If the section has relocs, set up a section header for the |
3392 | SHT_REL[A] section. If two relocation sections are required for | |
3393 | this section, it is up to the processor-specific back-end to | |
3394 | create the other. */ | |
3395 | if ((asect->flags & SEC_RELOC) != 0) | |
3396 | { | |
3397 | /* When doing a relocatable link, create both REL and RELA sections if | |
3398 | needed. */ | |
3399 | if (arg->link_info | |
3400 | /* Do the normal setup if we wouldn't create any sections here. */ | |
3401 | && esd->rel.count + esd->rela.count > 0 | |
0e1862bb L |
3402 | && (bfd_link_relocatable (arg->link_info) |
3403 | || arg->link_info->emitrelocations)) | |
d4730f92 BS |
3404 | { |
3405 | if (esd->rel.count && esd->rel.hdr == NULL | |
28e07a05 | 3406 | && !_bfd_elf_init_reloc_shdr (abfd, &esd->rel, name, |
db4677b8 | 3407 | FALSE, delay_st_name_p)) |
d4730f92 BS |
3408 | { |
3409 | arg->failed = TRUE; | |
3410 | return; | |
3411 | } | |
3412 | if (esd->rela.count && esd->rela.hdr == NULL | |
28e07a05 | 3413 | && !_bfd_elf_init_reloc_shdr (abfd, &esd->rela, name, |
db4677b8 | 3414 | TRUE, delay_st_name_p)) |
d4730f92 BS |
3415 | { |
3416 | arg->failed = TRUE; | |
3417 | return; | |
3418 | } | |
3419 | } | |
3420 | else if (!_bfd_elf_init_reloc_shdr (abfd, | |
3421 | (asect->use_rela_p | |
3422 | ? &esd->rela : &esd->rel), | |
f6fe1ccd | 3423 | name, |
3e19fb8f L |
3424 | asect->use_rela_p, |
3425 | delay_st_name_p)) | |
db4677b8 | 3426 | { |
d4730f92 | 3427 | arg->failed = TRUE; |
db4677b8 AM |
3428 | return; |
3429 | } | |
d4730f92 BS |
3430 | } |
3431 | ||
252b5132 | 3432 | /* Check for processor-specific section types. */ |
0414f35b | 3433 | sh_type = this_hdr->sh_type; |
e1fddb6b AO |
3434 | if (bed->elf_backend_fake_sections |
3435 | && !(*bed->elf_backend_fake_sections) (abfd, this_hdr, asect)) | |
db4677b8 AM |
3436 | { |
3437 | arg->failed = TRUE; | |
3438 | return; | |
3439 | } | |
252b5132 | 3440 | |
42bb2e33 | 3441 | if (sh_type == SHT_NOBITS && asect->size != 0) |
0414f35b AM |
3442 | { |
3443 | /* Don't change the header type from NOBITS if we are being | |
42bb2e33 | 3444 | called for objcopy --only-keep-debug. */ |
0414f35b AM |
3445 | this_hdr->sh_type = sh_type; |
3446 | } | |
252b5132 RH |
3447 | } |
3448 | ||
bcacc0f5 AM |
3449 | /* Fill in the contents of a SHT_GROUP section. Called from |
3450 | _bfd_elf_compute_section_file_positions for gas, objcopy, and | |
3451 | when ELF targets use the generic linker, ld. Called for ld -r | |
3452 | from bfd_elf_final_link. */ | |
dbb410c3 | 3453 | |
1126897b | 3454 | void |
217aa764 | 3455 | bfd_elf_set_group_contents (bfd *abfd, asection *sec, void *failedptrarg) |
dbb410c3 | 3456 | { |
a50b1753 | 3457 | bfd_boolean *failedptr = (bfd_boolean *) failedptrarg; |
9dce4196 | 3458 | asection *elt, *first; |
dbb410c3 | 3459 | unsigned char *loc; |
b34976b6 | 3460 | bfd_boolean gas; |
dbb410c3 | 3461 | |
7e4111ad L |
3462 | /* Ignore linker created group section. See elfNN_ia64_object_p in |
3463 | elfxx-ia64.c. */ | |
3464 | if (((sec->flags & (SEC_GROUP | SEC_LINKER_CREATED)) != SEC_GROUP) | |
dbb410c3 AM |
3465 | || *failedptr) |
3466 | return; | |
3467 | ||
bcacc0f5 AM |
3468 | if (elf_section_data (sec)->this_hdr.sh_info == 0) |
3469 | { | |
3470 | unsigned long symindx = 0; | |
3471 | ||
3472 | /* elf_group_id will have been set up by objcopy and the | |
3473 | generic linker. */ | |
3474 | if (elf_group_id (sec) != NULL) | |
3475 | symindx = elf_group_id (sec)->udata.i; | |
1126897b | 3476 | |
bcacc0f5 AM |
3477 | if (symindx == 0) |
3478 | { | |
3479 | /* If called from the assembler, swap_out_syms will have set up | |
3480 | elf_section_syms. */ | |
3481 | BFD_ASSERT (elf_section_syms (abfd) != NULL); | |
3482 | symindx = elf_section_syms (abfd)[sec->index]->udata.i; | |
3483 | } | |
3484 | elf_section_data (sec)->this_hdr.sh_info = symindx; | |
3485 | } | |
3486 | else if (elf_section_data (sec)->this_hdr.sh_info == (unsigned int) -2) | |
1126897b | 3487 | { |
bcacc0f5 AM |
3488 | /* The ELF backend linker sets sh_info to -2 when the group |
3489 | signature symbol is global, and thus the index can't be | |
3490 | set until all local symbols are output. */ | |
53720c49 AM |
3491 | asection *igroup; |
3492 | struct bfd_elf_section_data *sec_data; | |
3493 | unsigned long symndx; | |
3494 | unsigned long extsymoff; | |
bcacc0f5 AM |
3495 | struct elf_link_hash_entry *h; |
3496 | ||
53720c49 AM |
3497 | /* The point of this little dance to the first SHF_GROUP section |
3498 | then back to the SHT_GROUP section is that this gets us to | |
3499 | the SHT_GROUP in the input object. */ | |
3500 | igroup = elf_sec_group (elf_next_in_group (sec)); | |
3501 | sec_data = elf_section_data (igroup); | |
3502 | symndx = sec_data->this_hdr.sh_info; | |
3503 | extsymoff = 0; | |
bcacc0f5 AM |
3504 | if (!elf_bad_symtab (igroup->owner)) |
3505 | { | |
3506 | Elf_Internal_Shdr *symtab_hdr; | |
3507 | ||
3508 | symtab_hdr = &elf_tdata (igroup->owner)->symtab_hdr; | |
3509 | extsymoff = symtab_hdr->sh_info; | |
3510 | } | |
3511 | h = elf_sym_hashes (igroup->owner)[symndx - extsymoff]; | |
3512 | while (h->root.type == bfd_link_hash_indirect | |
3513 | || h->root.type == bfd_link_hash_warning) | |
3514 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
3515 | ||
3516 | elf_section_data (sec)->this_hdr.sh_info = h->indx; | |
1126897b | 3517 | } |
dbb410c3 | 3518 | |
1126897b | 3519 | /* The contents won't be allocated for "ld -r" or objcopy. */ |
b34976b6 | 3520 | gas = TRUE; |
dbb410c3 AM |
3521 | if (sec->contents == NULL) |
3522 | { | |
b34976b6 | 3523 | gas = FALSE; |
a50b1753 | 3524 | sec->contents = (unsigned char *) bfd_alloc (abfd, sec->size); |
9dce4196 AM |
3525 | |
3526 | /* Arrange for the section to be written out. */ | |
3527 | elf_section_data (sec)->this_hdr.contents = sec->contents; | |
dbb410c3 AM |
3528 | if (sec->contents == NULL) |
3529 | { | |
b34976b6 | 3530 | *failedptr = TRUE; |
dbb410c3 AM |
3531 | return; |
3532 | } | |
3533 | } | |
3534 | ||
eea6121a | 3535 | loc = sec->contents + sec->size; |
dbb410c3 | 3536 | |
9dce4196 AM |
3537 | /* Get the pointer to the first section in the group that gas |
3538 | squirreled away here. objcopy arranges for this to be set to the | |
3539 | start of the input section group. */ | |
3540 | first = elt = elf_next_in_group (sec); | |
dbb410c3 AM |
3541 | |
3542 | /* First element is a flag word. Rest of section is elf section | |
3543 | indices for all the sections of the group. Write them backwards | |
3544 | just to keep the group in the same order as given in .section | |
3545 | directives, not that it matters. */ | |
3546 | while (elt != NULL) | |
3547 | { | |
9dce4196 | 3548 | asection *s; |
9dce4196 | 3549 | |
9dce4196 | 3550 | s = elt; |
415f38a6 AM |
3551 | if (!gas) |
3552 | s = s->output_section; | |
3553 | if (s != NULL | |
3554 | && !bfd_is_abs_section (s)) | |
01e1a5bc | 3555 | { |
db4677b8 | 3556 | struct bfd_elf_section_data *elf_sec = elf_section_data (s); |
28e07a05 AM |
3557 | struct bfd_elf_section_data *input_elf_sec = elf_section_data (elt); |
3558 | ||
3559 | if (elf_sec->rel.hdr != NULL | |
3560 | && (gas | |
3561 | || (input_elf_sec->rel.hdr != NULL | |
3562 | && input_elf_sec->rel.hdr->sh_flags & SHF_GROUP) != 0)) | |
db4677b8 | 3563 | { |
28e07a05 | 3564 | elf_sec->rel.hdr->sh_flags |= SHF_GROUP; |
db4677b8 AM |
3565 | loc -= 4; |
3566 | H_PUT_32 (abfd, elf_sec->rel.idx, loc); | |
3567 | } | |
28e07a05 AM |
3568 | if (elf_sec->rela.hdr != NULL |
3569 | && (gas | |
3570 | || (input_elf_sec->rela.hdr != NULL | |
3571 | && input_elf_sec->rela.hdr->sh_flags & SHF_GROUP) != 0)) | |
db4677b8 | 3572 | { |
28e07a05 | 3573 | elf_sec->rela.hdr->sh_flags |= SHF_GROUP; |
db4677b8 AM |
3574 | loc -= 4; |
3575 | H_PUT_32 (abfd, elf_sec->rela.idx, loc); | |
3576 | } | |
01e1a5bc | 3577 | loc -= 4; |
db4677b8 | 3578 | H_PUT_32 (abfd, elf_sec->this_idx, loc); |
01e1a5bc | 3579 | } |
945906ff | 3580 | elt = elf_next_in_group (elt); |
9dce4196 AM |
3581 | if (elt == first) |
3582 | break; | |
dbb410c3 AM |
3583 | } |
3584 | ||
7bdf4127 AB |
3585 | loc -= 4; |
3586 | BFD_ASSERT (loc == sec->contents); | |
dbb410c3 | 3587 | |
9dce4196 | 3588 | H_PUT_32 (abfd, sec->flags & SEC_LINK_ONCE ? GRP_COMDAT : 0, loc); |
dbb410c3 AM |
3589 | } |
3590 | ||
bce964aa AM |
3591 | /* Given NAME, the name of a relocation section stripped of its |
3592 | .rel/.rela prefix, return the section in ABFD to which the | |
3593 | relocations apply. */ | |
bd53a53a L |
3594 | |
3595 | asection * | |
bce964aa AM |
3596 | _bfd_elf_plt_get_reloc_section (bfd *abfd, const char *name) |
3597 | { | |
3598 | /* If a target needs .got.plt section, relocations in rela.plt/rel.plt | |
3599 | section likely apply to .got.plt or .got section. */ | |
3600 | if (get_elf_backend_data (abfd)->want_got_plt | |
3601 | && strcmp (name, ".plt") == 0) | |
3602 | { | |
3603 | asection *sec; | |
3604 | ||
3605 | name = ".got.plt"; | |
3606 | sec = bfd_get_section_by_name (abfd, name); | |
3607 | if (sec != NULL) | |
3608 | return sec; | |
3609 | name = ".got"; | |
3610 | } | |
3611 | ||
3612 | return bfd_get_section_by_name (abfd, name); | |
3613 | } | |
3614 | ||
3615 | /* Return the section to which RELOC_SEC applies. */ | |
3616 | ||
3617 | static asection * | |
3618 | elf_get_reloc_section (asection *reloc_sec) | |
bd53a53a L |
3619 | { |
3620 | const char *name; | |
3621 | unsigned int type; | |
3622 | bfd *abfd; | |
bce964aa | 3623 | const struct elf_backend_data *bed; |
bd53a53a L |
3624 | |
3625 | type = elf_section_data (reloc_sec)->this_hdr.sh_type; | |
3626 | if (type != SHT_REL && type != SHT_RELA) | |
3627 | return NULL; | |
3628 | ||
3629 | /* We look up the section the relocs apply to by name. */ | |
3630 | name = reloc_sec->name; | |
bce964aa AM |
3631 | if (strncmp (name, ".rel", 4) != 0) |
3632 | return NULL; | |
3633 | name += 4; | |
3634 | if (type == SHT_RELA && *name++ != 'a') | |
3635 | return NULL; | |
bd53a53a | 3636 | |
bd53a53a | 3637 | abfd = reloc_sec->owner; |
bce964aa AM |
3638 | bed = get_elf_backend_data (abfd); |
3639 | return bed->get_reloc_section (abfd, name); | |
bd53a53a L |
3640 | } |
3641 | ||
252b5132 RH |
3642 | /* Assign all ELF section numbers. The dummy first section is handled here |
3643 | too. The link/info pointers for the standard section types are filled | |
3644 | in here too, while we're at it. */ | |
3645 | ||
b34976b6 | 3646 | static bfd_boolean |
da9f89d4 | 3647 | assign_section_numbers (bfd *abfd, struct bfd_link_info *link_info) |
252b5132 RH |
3648 | { |
3649 | struct elf_obj_tdata *t = elf_tdata (abfd); | |
3650 | asection *sec; | |
3e19fb8f | 3651 | unsigned int section_number; |
252b5132 | 3652 | Elf_Internal_Shdr **i_shdrp; |
47cc2cf5 | 3653 | struct bfd_elf_section_data *d; |
3516e984 | 3654 | bfd_boolean need_symtab; |
252b5132 RH |
3655 | |
3656 | section_number = 1; | |
3657 | ||
2b0f7ef9 JJ |
3658 | _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd)); |
3659 | ||
da9f89d4 | 3660 | /* SHT_GROUP sections are in relocatable files only. */ |
7bdf4127 | 3661 | if (link_info == NULL || !link_info->resolve_section_groups) |
252b5132 | 3662 | { |
ef53be89 | 3663 | size_t reloc_count = 0; |
14f2c699 | 3664 | |
da9f89d4 | 3665 | /* Put SHT_GROUP sections first. */ |
04dd1667 | 3666 | for (sec = abfd->sections; sec != NULL; sec = sec->next) |
47cc2cf5 | 3667 | { |
5daa8fe7 | 3668 | d = elf_section_data (sec); |
da9f89d4 L |
3669 | |
3670 | if (d->this_hdr.sh_type == SHT_GROUP) | |
08a40648 | 3671 | { |
5daa8fe7 | 3672 | if (sec->flags & SEC_LINKER_CREATED) |
da9f89d4 L |
3673 | { |
3674 | /* Remove the linker created SHT_GROUP sections. */ | |
5daa8fe7 | 3675 | bfd_section_list_remove (abfd, sec); |
da9f89d4 | 3676 | abfd->section_count--; |
da9f89d4 | 3677 | } |
08a40648 | 3678 | else |
4fbb74a6 | 3679 | d->this_idx = section_number++; |
da9f89d4 | 3680 | } |
14f2c699 L |
3681 | |
3682 | /* Count relocations. */ | |
3683 | reloc_count += sec->reloc_count; | |
47cc2cf5 | 3684 | } |
14f2c699 L |
3685 | |
3686 | /* Clear HAS_RELOC if there are no relocations. */ | |
3687 | if (reloc_count == 0) | |
3688 | abfd->flags &= ~HAS_RELOC; | |
47cc2cf5 PB |
3689 | } |
3690 | ||
3691 | for (sec = abfd->sections; sec; sec = sec->next) | |
3692 | { | |
3693 | d = elf_section_data (sec); | |
3694 | ||
3695 | if (d->this_hdr.sh_type != SHT_GROUP) | |
4fbb74a6 | 3696 | d->this_idx = section_number++; |
3e19fb8f L |
3697 | if (d->this_hdr.sh_name != (unsigned int) -1) |
3698 | _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->this_hdr.sh_name); | |
d4730f92 | 3699 | if (d->rel.hdr) |
2b0f7ef9 | 3700 | { |
d4730f92 | 3701 | d->rel.idx = section_number++; |
3e19fb8f L |
3702 | if (d->rel.hdr->sh_name != (unsigned int) -1) |
3703 | _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel.hdr->sh_name); | |
2b0f7ef9 | 3704 | } |
d4730f92 BS |
3705 | else |
3706 | d->rel.idx = 0; | |
23bc299b | 3707 | |
d4730f92 | 3708 | if (d->rela.hdr) |
2b0f7ef9 | 3709 | { |
d4730f92 | 3710 | d->rela.idx = section_number++; |
3e19fb8f L |
3711 | if (d->rela.hdr->sh_name != (unsigned int) -1) |
3712 | _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rela.hdr->sh_name); | |
2b0f7ef9 | 3713 | } |
23bc299b | 3714 | else |
d4730f92 | 3715 | d->rela.idx = 0; |
252b5132 RH |
3716 | } |
3717 | ||
3516e984 L |
3718 | need_symtab = (bfd_get_symcount (abfd) > 0 |
3719 | || (link_info == NULL | |
3720 | && ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC)) | |
3721 | == HAS_RELOC))); | |
3722 | if (need_symtab) | |
252b5132 | 3723 | { |
12bd6957 | 3724 | elf_onesymtab (abfd) = section_number++; |
2b0f7ef9 | 3725 | _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->symtab_hdr.sh_name); |
4fbb74a6 | 3726 | if (section_number > ((SHN_LORESERVE - 2) & 0xFFFF)) |
9ad5cbcf | 3727 | { |
6a40cf0c NC |
3728 | elf_section_list * entry; |
3729 | ||
3730 | BFD_ASSERT (elf_symtab_shndx_list (abfd) == NULL); | |
3731 | ||
3732 | entry = bfd_zalloc (abfd, sizeof * entry); | |
3733 | entry->ndx = section_number++; | |
3734 | elf_symtab_shndx_list (abfd) = entry; | |
3735 | entry->hdr.sh_name | |
9ad5cbcf | 3736 | = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), |
b34976b6 | 3737 | ".symtab_shndx", FALSE); |
6a40cf0c | 3738 | if (entry->hdr.sh_name == (unsigned int) -1) |
b34976b6 | 3739 | return FALSE; |
9ad5cbcf | 3740 | } |
12bd6957 | 3741 | elf_strtab_sec (abfd) = section_number++; |
2b0f7ef9 | 3742 | _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->strtab_hdr.sh_name); |
252b5132 RH |
3743 | } |
3744 | ||
dd905818 NC |
3745 | elf_shstrtab_sec (abfd) = section_number++; |
3746 | _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->shstrtab_hdr.sh_name); | |
3747 | elf_elfheader (abfd)->e_shstrndx = elf_shstrtab_sec (abfd); | |
3748 | ||
1c52a645 L |
3749 | if (section_number >= SHN_LORESERVE) |
3750 | { | |
695344c0 | 3751 | /* xgettext:c-format */ |
871b3ab2 | 3752 | _bfd_error_handler (_("%pB: too many sections: %u"), |
1c52a645 L |
3753 | abfd, section_number); |
3754 | return FALSE; | |
3755 | } | |
3756 | ||
9ad5cbcf | 3757 | elf_numsections (abfd) = section_number; |
252b5132 RH |
3758 | elf_elfheader (abfd)->e_shnum = section_number; |
3759 | ||
3760 | /* Set up the list of section header pointers, in agreement with the | |
3761 | indices. */ | |
a50b1753 | 3762 | i_shdrp = (Elf_Internal_Shdr **) bfd_zalloc2 (abfd, section_number, |
07d6d2b8 | 3763 | sizeof (Elf_Internal_Shdr *)); |
252b5132 | 3764 | if (i_shdrp == NULL) |
b34976b6 | 3765 | return FALSE; |
252b5132 | 3766 | |
a50b1753 | 3767 | i_shdrp[0] = (Elf_Internal_Shdr *) bfd_zalloc (abfd, |
07d6d2b8 | 3768 | sizeof (Elf_Internal_Shdr)); |
252b5132 RH |
3769 | if (i_shdrp[0] == NULL) |
3770 | { | |
3771 | bfd_release (abfd, i_shdrp); | |
b34976b6 | 3772 | return FALSE; |
252b5132 | 3773 | } |
252b5132 RH |
3774 | |
3775 | elf_elfsections (abfd) = i_shdrp; | |
3776 | ||
12bd6957 | 3777 | i_shdrp[elf_shstrtab_sec (abfd)] = &t->shstrtab_hdr; |
3516e984 | 3778 | if (need_symtab) |
252b5132 | 3779 | { |
12bd6957 | 3780 | i_shdrp[elf_onesymtab (abfd)] = &t->symtab_hdr; |
4fbb74a6 | 3781 | if (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF)) |
9ad5cbcf | 3782 | { |
6a40cf0c NC |
3783 | elf_section_list * entry = elf_symtab_shndx_list (abfd); |
3784 | BFD_ASSERT (entry != NULL); | |
3785 | i_shdrp[entry->ndx] = & entry->hdr; | |
3786 | entry->hdr.sh_link = elf_onesymtab (abfd); | |
9ad5cbcf | 3787 | } |
12bd6957 AM |
3788 | i_shdrp[elf_strtab_sec (abfd)] = &t->strtab_hdr; |
3789 | t->symtab_hdr.sh_link = elf_strtab_sec (abfd); | |
252b5132 | 3790 | } |
38ce5b11 | 3791 | |
252b5132 RH |
3792 | for (sec = abfd->sections; sec; sec = sec->next) |
3793 | { | |
252b5132 | 3794 | asection *s; |
252b5132 | 3795 | |
91d6fa6a NC |
3796 | d = elf_section_data (sec); |
3797 | ||
252b5132 | 3798 | i_shdrp[d->this_idx] = &d->this_hdr; |
d4730f92 BS |
3799 | if (d->rel.idx != 0) |
3800 | i_shdrp[d->rel.idx] = d->rel.hdr; | |
3801 | if (d->rela.idx != 0) | |
3802 | i_shdrp[d->rela.idx] = d->rela.hdr; | |
252b5132 RH |
3803 | |
3804 | /* Fill in the sh_link and sh_info fields while we're at it. */ | |
3805 | ||
3806 | /* sh_link of a reloc section is the section index of the symbol | |
3807 | table. sh_info is the section index of the section to which | |
3808 | the relocation entries apply. */ | |
d4730f92 | 3809 | if (d->rel.idx != 0) |
252b5132 | 3810 | { |
12bd6957 | 3811 | d->rel.hdr->sh_link = elf_onesymtab (abfd); |
d4730f92 | 3812 | d->rel.hdr->sh_info = d->this_idx; |
9ef5d938 | 3813 | d->rel.hdr->sh_flags |= SHF_INFO_LINK; |
252b5132 | 3814 | } |
d4730f92 | 3815 | if (d->rela.idx != 0) |
23bc299b | 3816 | { |
12bd6957 | 3817 | d->rela.hdr->sh_link = elf_onesymtab (abfd); |
d4730f92 | 3818 | d->rela.hdr->sh_info = d->this_idx; |
9ef5d938 | 3819 | d->rela.hdr->sh_flags |= SHF_INFO_LINK; |
23bc299b | 3820 | } |
252b5132 | 3821 | |
38ce5b11 L |
3822 | /* We need to set up sh_link for SHF_LINK_ORDER. */ |
3823 | if ((d->this_hdr.sh_flags & SHF_LINK_ORDER) != 0) | |
3824 | { | |
3825 | s = elf_linked_to_section (sec); | |
3826 | if (s) | |
38ce5b11 | 3827 | { |
f2876037 | 3828 | /* elf_linked_to_section points to the input section. */ |
ccd2ec6a | 3829 | if (link_info != NULL) |
38ce5b11 | 3830 | { |
f2876037 | 3831 | /* Check discarded linkonce section. */ |
dbaa2011 | 3832 | if (discarded_section (s)) |
38ce5b11 | 3833 | { |
ccd2ec6a | 3834 | asection *kept; |
4eca0228 | 3835 | _bfd_error_handler |
695344c0 | 3836 | /* xgettext:c-format */ |
871b3ab2 AM |
3837 | (_("%pB: sh_link of section `%pA' points to" |
3838 | " discarded section `%pA' of `%pB'"), | |
ccd2ec6a L |
3839 | abfd, d->this_hdr.bfd_section, |
3840 | s, s->owner); | |
3841 | /* Point to the kept section if it has the same | |
3842 | size as the discarded one. */ | |
c0f00686 | 3843 | kept = _bfd_elf_check_kept_section (s, link_info); |
ccd2ec6a | 3844 | if (kept == NULL) |
185d09ad | 3845 | { |
ccd2ec6a L |
3846 | bfd_set_error (bfd_error_bad_value); |
3847 | return FALSE; | |
185d09ad | 3848 | } |
ccd2ec6a | 3849 | s = kept; |
38ce5b11 | 3850 | } |
e424ecc8 | 3851 | |
ccd2ec6a L |
3852 | s = s->output_section; |
3853 | BFD_ASSERT (s != NULL); | |
38ce5b11 | 3854 | } |
f2876037 L |
3855 | else |
3856 | { | |
3857 | /* Handle objcopy. */ | |
3858 | if (s->output_section == NULL) | |
3859 | { | |
4eca0228 | 3860 | _bfd_error_handler |
695344c0 | 3861 | /* xgettext:c-format */ |
871b3ab2 AM |
3862 | (_("%pB: sh_link of section `%pA' points to" |
3863 | " removed section `%pA' of `%pB'"), | |
f2876037 L |
3864 | abfd, d->this_hdr.bfd_section, s, s->owner); |
3865 | bfd_set_error (bfd_error_bad_value); | |
3866 | return FALSE; | |
3867 | } | |
3868 | s = s->output_section; | |
3869 | } | |
ccd2ec6a L |
3870 | d->this_hdr.sh_link = elf_section_data (s)->this_idx; |
3871 | } | |
3872 | else | |
3873 | { | |
3874 | /* PR 290: | |
3875 | The Intel C compiler generates SHT_IA_64_UNWIND with | |
3876 | SHF_LINK_ORDER. But it doesn't set the sh_link or | |
3877 | sh_info fields. Hence we could get the situation | |
08a40648 | 3878 | where s is NULL. */ |
ccd2ec6a L |
3879 | const struct elf_backend_data *bed |
3880 | = get_elf_backend_data (abfd); | |
3881 | if (bed->link_order_error_handler) | |
3882 | bed->link_order_error_handler | |
695344c0 | 3883 | /* xgettext:c-format */ |
871b3ab2 | 3884 | (_("%pB: warning: sh_link not set for section `%pA'"), |
ccd2ec6a | 3885 | abfd, sec); |
38ce5b11 L |
3886 | } |
3887 | } | |
3888 | ||
252b5132 RH |
3889 | switch (d->this_hdr.sh_type) |
3890 | { | |
3891 | case SHT_REL: | |
3892 | case SHT_RELA: | |
3893 | /* A reloc section which we are treating as a normal BFD | |
3894 | section. sh_link is the section index of the symbol | |
3895 | table. sh_info is the section index of the section to | |
3896 | which the relocation entries apply. We assume that an | |
3897 | allocated reloc section uses the dynamic symbol table. | |
3898 | FIXME: How can we be sure? */ | |
3899 | s = bfd_get_section_by_name (abfd, ".dynsym"); | |
3900 | if (s != NULL) | |
3901 | d->this_hdr.sh_link = elf_section_data (s)->this_idx; | |
3902 | ||
bce964aa | 3903 | s = elf_get_reloc_section (sec); |
252b5132 | 3904 | if (s != NULL) |
9ef5d938 L |
3905 | { |
3906 | d->this_hdr.sh_info = elf_section_data (s)->this_idx; | |
3907 | d->this_hdr.sh_flags |= SHF_INFO_LINK; | |
3908 | } | |
252b5132 RH |
3909 | break; |
3910 | ||
3911 | case SHT_STRTAB: | |
3912 | /* We assume that a section named .stab*str is a stabs | |
3913 | string section. We look for a section with the same name | |
3914 | but without the trailing ``str'', and set its sh_link | |
3915 | field to point to this section. */ | |
0112cd26 | 3916 | if (CONST_STRNEQ (sec->name, ".stab") |
252b5132 RH |
3917 | && strcmp (sec->name + strlen (sec->name) - 3, "str") == 0) |
3918 | { | |
3919 | size_t len; | |
3920 | char *alc; | |
3921 | ||
3922 | len = strlen (sec->name); | |
a50b1753 | 3923 | alc = (char *) bfd_malloc (len - 2); |
252b5132 | 3924 | if (alc == NULL) |
b34976b6 | 3925 | return FALSE; |
d4c88bbb | 3926 | memcpy (alc, sec->name, len - 3); |
252b5132 RH |
3927 | alc[len - 3] = '\0'; |
3928 | s = bfd_get_section_by_name (abfd, alc); | |
3929 | free (alc); | |
3930 | if (s != NULL) | |
3931 | { | |
3932 | elf_section_data (s)->this_hdr.sh_link = d->this_idx; | |
3933 | ||
3934 | /* This is a .stab section. */ | |
0594c12d AM |
3935 | if (elf_section_data (s)->this_hdr.sh_entsize == 0) |
3936 | elf_section_data (s)->this_hdr.sh_entsize | |
3937 | = 4 + 2 * bfd_get_arch_size (abfd) / 8; | |
252b5132 RH |
3938 | } |
3939 | } | |
3940 | break; | |
3941 | ||
3942 | case SHT_DYNAMIC: | |
3943 | case SHT_DYNSYM: | |
3944 | case SHT_GNU_verneed: | |
3945 | case SHT_GNU_verdef: | |
3946 | /* sh_link is the section header index of the string table | |
3947 | used for the dynamic entries, or the symbol table, or the | |
3948 | version strings. */ | |
3949 | s = bfd_get_section_by_name (abfd, ".dynstr"); | |
3950 | if (s != NULL) | |
3951 | d->this_hdr.sh_link = elf_section_data (s)->this_idx; | |
3952 | break; | |
3953 | ||
7f1204bb JJ |
3954 | case SHT_GNU_LIBLIST: |
3955 | /* sh_link is the section header index of the prelink library | |
08a40648 AM |
3956 | list used for the dynamic entries, or the symbol table, or |
3957 | the version strings. */ | |
7f1204bb JJ |
3958 | s = bfd_get_section_by_name (abfd, (sec->flags & SEC_ALLOC) |
3959 | ? ".dynstr" : ".gnu.libstr"); | |
3960 | if (s != NULL) | |
3961 | d->this_hdr.sh_link = elf_section_data (s)->this_idx; | |
3962 | break; | |
3963 | ||
252b5132 | 3964 | case SHT_HASH: |
fdc90cb4 | 3965 | case SHT_GNU_HASH: |
252b5132 RH |
3966 | case SHT_GNU_versym: |
3967 | /* sh_link is the section header index of the symbol table | |
3968 | this hash table or version table is for. */ | |
3969 | s = bfd_get_section_by_name (abfd, ".dynsym"); | |
3970 | if (s != NULL) | |
3971 | d->this_hdr.sh_link = elf_section_data (s)->this_idx; | |
3972 | break; | |
dbb410c3 AM |
3973 | |
3974 | case SHT_GROUP: | |
12bd6957 | 3975 | d->this_hdr.sh_link = elf_onesymtab (abfd); |
252b5132 RH |
3976 | } |
3977 | } | |
3978 | ||
3e19fb8f L |
3979 | /* Delay setting sh_name to _bfd_elf_write_object_contents so that |
3980 | _bfd_elf_assign_file_positions_for_non_load can convert DWARF | |
3981 | debug section name from .debug_* to .zdebug_* if needed. */ | |
3982 | ||
b34976b6 | 3983 | return TRUE; |
252b5132 RH |
3984 | } |
3985 | ||
5372391b | 3986 | static bfd_boolean |
217aa764 | 3987 | sym_is_global (bfd *abfd, asymbol *sym) |
252b5132 RH |
3988 | { |
3989 | /* If the backend has a special mapping, use it. */ | |
9c5bfbb7 | 3990 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
217aa764 AM |
3991 | if (bed->elf_backend_sym_is_global) |
3992 | return (*bed->elf_backend_sym_is_global) (abfd, sym); | |
252b5132 | 3993 | |
e47bf690 | 3994 | return ((sym->flags & (BSF_GLOBAL | BSF_WEAK | BSF_GNU_UNIQUE)) != 0 |
252b5132 RH |
3995 | || bfd_is_und_section (bfd_get_section (sym)) |
3996 | || bfd_is_com_section (bfd_get_section (sym))); | |
3997 | } | |
3998 | ||
76359541 TP |
3999 | /* Filter global symbols of ABFD to include in the import library. All |
4000 | SYMCOUNT symbols of ABFD can be examined from their pointers in | |
4001 | SYMS. Pointers of symbols to keep should be stored contiguously at | |
4002 | the beginning of that array. | |
4003 | ||
4004 | Returns the number of symbols to keep. */ | |
4005 | ||
4006 | unsigned int | |
4007 | _bfd_elf_filter_global_symbols (bfd *abfd, struct bfd_link_info *info, | |
4008 | asymbol **syms, long symcount) | |
4009 | { | |
4010 | long src_count, dst_count = 0; | |
4011 | ||
4012 | for (src_count = 0; src_count < symcount; src_count++) | |
4013 | { | |
4014 | asymbol *sym = syms[src_count]; | |
4015 | char *name = (char *) bfd_asymbol_name (sym); | |
4016 | struct bfd_link_hash_entry *h; | |
4017 | ||
4018 | if (!sym_is_global (abfd, sym)) | |
4019 | continue; | |
4020 | ||
4021 | h = bfd_link_hash_lookup (info->hash, name, FALSE, FALSE, FALSE); | |
5df1bc57 AM |
4022 | if (h == NULL) |
4023 | continue; | |
76359541 TP |
4024 | if (h->type != bfd_link_hash_defined && h->type != bfd_link_hash_defweak) |
4025 | continue; | |
76359541 TP |
4026 | if (h->linker_def || h->ldscript_def) |
4027 | continue; | |
4028 | ||
4029 | syms[dst_count++] = sym; | |
4030 | } | |
4031 | ||
4032 | syms[dst_count] = NULL; | |
4033 | ||
4034 | return dst_count; | |
4035 | } | |
4036 | ||
5372391b | 4037 | /* Don't output section symbols for sections that are not going to be |
c6d8cab4 | 4038 | output, that are duplicates or there is no BFD section. */ |
5372391b AM |
4039 | |
4040 | static bfd_boolean | |
4041 | ignore_section_sym (bfd *abfd, asymbol *sym) | |
4042 | { | |
c6d8cab4 L |
4043 | elf_symbol_type *type_ptr; |
4044 | ||
db0c309f NC |
4045 | if (sym == NULL) |
4046 | return FALSE; | |
4047 | ||
c6d8cab4 L |
4048 | if ((sym->flags & BSF_SECTION_SYM) == 0) |
4049 | return FALSE; | |
4050 | ||
db0c309f NC |
4051 | if (sym->section == NULL) |
4052 | return TRUE; | |
4053 | ||
c6d8cab4 L |
4054 | type_ptr = elf_symbol_from (abfd, sym); |
4055 | return ((type_ptr != NULL | |
4056 | && type_ptr->internal_elf_sym.st_shndx != 0 | |
4057 | && bfd_is_abs_section (sym->section)) | |
4058 | || !(sym->section->owner == abfd | |
db0c309f NC |
4059 | || (sym->section->output_section != NULL |
4060 | && sym->section->output_section->owner == abfd | |
2633a79c AM |
4061 | && sym->section->output_offset == 0) |
4062 | || bfd_is_abs_section (sym->section))); | |
5372391b AM |
4063 | } |
4064 | ||
2633a79c AM |
4065 | /* Map symbol from it's internal number to the external number, moving |
4066 | all local symbols to be at the head of the list. */ | |
4067 | ||
b34976b6 | 4068 | static bfd_boolean |
12bd6957 | 4069 | elf_map_symbols (bfd *abfd, unsigned int *pnum_locals) |
252b5132 | 4070 | { |
dc810e39 | 4071 | unsigned int symcount = bfd_get_symcount (abfd); |
252b5132 RH |
4072 | asymbol **syms = bfd_get_outsymbols (abfd); |
4073 | asymbol **sect_syms; | |
dc810e39 AM |
4074 | unsigned int num_locals = 0; |
4075 | unsigned int num_globals = 0; | |
4076 | unsigned int num_locals2 = 0; | |
4077 | unsigned int num_globals2 = 0; | |
7292b3ac | 4078 | unsigned int max_index = 0; |
dc810e39 | 4079 | unsigned int idx; |
252b5132 RH |
4080 | asection *asect; |
4081 | asymbol **new_syms; | |
252b5132 RH |
4082 | |
4083 | #ifdef DEBUG | |
4084 | fprintf (stderr, "elf_map_symbols\n"); | |
4085 | fflush (stderr); | |
4086 | #endif | |
4087 | ||
252b5132 RH |
4088 | for (asect = abfd->sections; asect; asect = asect->next) |
4089 | { | |
4090 | if (max_index < asect->index) | |
4091 | max_index = asect->index; | |
4092 | } | |
4093 | ||
4094 | max_index++; | |
a50b1753 | 4095 | sect_syms = (asymbol **) bfd_zalloc2 (abfd, max_index, sizeof (asymbol *)); |
252b5132 | 4096 | if (sect_syms == NULL) |
b34976b6 | 4097 | return FALSE; |
252b5132 | 4098 | elf_section_syms (abfd) = sect_syms; |
4e89ac30 | 4099 | elf_num_section_syms (abfd) = max_index; |
252b5132 | 4100 | |
079e9a2f AM |
4101 | /* Init sect_syms entries for any section symbols we have already |
4102 | decided to output. */ | |
252b5132 RH |
4103 | for (idx = 0; idx < symcount; idx++) |
4104 | { | |
dc810e39 | 4105 | asymbol *sym = syms[idx]; |
c044fabd | 4106 | |
252b5132 | 4107 | if ((sym->flags & BSF_SECTION_SYM) != 0 |
0f0a5e58 | 4108 | && sym->value == 0 |
2633a79c AM |
4109 | && !ignore_section_sym (abfd, sym) |
4110 | && !bfd_is_abs_section (sym->section)) | |
252b5132 | 4111 | { |
5372391b | 4112 | asection *sec = sym->section; |
252b5132 | 4113 | |
5372391b AM |
4114 | if (sec->owner != abfd) |
4115 | sec = sec->output_section; | |
252b5132 | 4116 | |
5372391b | 4117 | sect_syms[sec->index] = syms[idx]; |
252b5132 RH |
4118 | } |
4119 | } | |
4120 | ||
252b5132 RH |
4121 | /* Classify all of the symbols. */ |
4122 | for (idx = 0; idx < symcount; idx++) | |
4123 | { | |
2633a79c | 4124 | if (sym_is_global (abfd, syms[idx])) |
252b5132 | 4125 | num_globals++; |
2633a79c AM |
4126 | else if (!ignore_section_sym (abfd, syms[idx])) |
4127 | num_locals++; | |
252b5132 | 4128 | } |
079e9a2f | 4129 | |
5372391b | 4130 | /* We will be adding a section symbol for each normal BFD section. Most |
079e9a2f AM |
4131 | sections will already have a section symbol in outsymbols, but |
4132 | eg. SHT_GROUP sections will not, and we need the section symbol mapped | |
4133 | at least in that case. */ | |
252b5132 RH |
4134 | for (asect = abfd->sections; asect; asect = asect->next) |
4135 | { | |
079e9a2f | 4136 | if (sect_syms[asect->index] == NULL) |
252b5132 | 4137 | { |
079e9a2f | 4138 | if (!sym_is_global (abfd, asect->symbol)) |
252b5132 RH |
4139 | num_locals++; |
4140 | else | |
4141 | num_globals++; | |
252b5132 RH |
4142 | } |
4143 | } | |
4144 | ||
4145 | /* Now sort the symbols so the local symbols are first. */ | |
a50b1753 | 4146 | new_syms = (asymbol **) bfd_alloc2 (abfd, num_locals + num_globals, |
07d6d2b8 | 4147 | sizeof (asymbol *)); |
dc810e39 | 4148 | |
252b5132 | 4149 | if (new_syms == NULL) |
b34976b6 | 4150 | return FALSE; |
252b5132 RH |
4151 | |
4152 | for (idx = 0; idx < symcount; idx++) | |
4153 | { | |
4154 | asymbol *sym = syms[idx]; | |
dc810e39 | 4155 | unsigned int i; |
252b5132 | 4156 | |
2633a79c AM |
4157 | if (sym_is_global (abfd, sym)) |
4158 | i = num_locals + num_globals2++; | |
4159 | else if (!ignore_section_sym (abfd, sym)) | |
252b5132 RH |
4160 | i = num_locals2++; |
4161 | else | |
2633a79c | 4162 | continue; |
252b5132 RH |
4163 | new_syms[i] = sym; |
4164 | sym->udata.i = i + 1; | |
4165 | } | |
4166 | for (asect = abfd->sections; asect; asect = asect->next) | |
4167 | { | |
079e9a2f | 4168 | if (sect_syms[asect->index] == NULL) |
252b5132 | 4169 | { |
079e9a2f | 4170 | asymbol *sym = asect->symbol; |
dc810e39 | 4171 | unsigned int i; |
252b5132 | 4172 | |
079e9a2f | 4173 | sect_syms[asect->index] = sym; |
252b5132 RH |
4174 | if (!sym_is_global (abfd, sym)) |
4175 | i = num_locals2++; | |
4176 | else | |
4177 | i = num_locals + num_globals2++; | |
4178 | new_syms[i] = sym; | |
4179 | sym->udata.i = i + 1; | |
4180 | } | |
4181 | } | |
4182 | ||
4183 | bfd_set_symtab (abfd, new_syms, num_locals + num_globals); | |
4184 | ||
12bd6957 | 4185 | *pnum_locals = num_locals; |
b34976b6 | 4186 | return TRUE; |
252b5132 RH |
4187 | } |
4188 | ||
4189 | /* Align to the maximum file alignment that could be required for any | |
4190 | ELF data structure. */ | |
4191 | ||
268b6b39 | 4192 | static inline file_ptr |
217aa764 | 4193 | align_file_position (file_ptr off, int align) |
252b5132 RH |
4194 | { |
4195 | return (off + align - 1) & ~(align - 1); | |
4196 | } | |
4197 | ||
4198 | /* Assign a file position to a section, optionally aligning to the | |
4199 | required section alignment. */ | |
4200 | ||
217aa764 AM |
4201 | file_ptr |
4202 | _bfd_elf_assign_file_position_for_section (Elf_Internal_Shdr *i_shdrp, | |
4203 | file_ptr offset, | |
4204 | bfd_boolean align) | |
252b5132 | 4205 | { |
72de5009 AM |
4206 | if (align && i_shdrp->sh_addralign > 1) |
4207 | offset = BFD_ALIGN (offset, i_shdrp->sh_addralign); | |
252b5132 RH |
4208 | i_shdrp->sh_offset = offset; |
4209 | if (i_shdrp->bfd_section != NULL) | |
4210 | i_shdrp->bfd_section->filepos = offset; | |
4211 | if (i_shdrp->sh_type != SHT_NOBITS) | |
4212 | offset += i_shdrp->sh_size; | |
4213 | return offset; | |
4214 | } | |
4215 | ||
4216 | /* Compute the file positions we are going to put the sections at, and | |
4217 | otherwise prepare to begin writing out the ELF file. If LINK_INFO | |
4218 | is not NULL, this is being called by the ELF backend linker. */ | |
4219 | ||
b34976b6 | 4220 | bfd_boolean |
217aa764 AM |
4221 | _bfd_elf_compute_section_file_positions (bfd *abfd, |
4222 | struct bfd_link_info *link_info) | |
252b5132 | 4223 | { |
9c5bfbb7 | 4224 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
d4730f92 | 4225 | struct fake_section_arg fsargs; |
b34976b6 | 4226 | bfd_boolean failed; |
ef10c3ac | 4227 | struct elf_strtab_hash *strtab = NULL; |
252b5132 | 4228 | Elf_Internal_Shdr *shstrtab_hdr; |
3516e984 | 4229 | bfd_boolean need_symtab; |
252b5132 RH |
4230 | |
4231 | if (abfd->output_has_begun) | |
b34976b6 | 4232 | return TRUE; |
252b5132 RH |
4233 | |
4234 | /* Do any elf backend specific processing first. */ | |
4235 | if (bed->elf_backend_begin_write_processing) | |
4236 | (*bed->elf_backend_begin_write_processing) (abfd, link_info); | |
4237 | ||
4238 | if (! prep_headers (abfd)) | |
b34976b6 | 4239 | return FALSE; |
252b5132 | 4240 | |
e6c51ed4 | 4241 | /* Post process the headers if necessary. */ |
78245035 | 4242 | (*bed->elf_backend_post_process_headers) (abfd, link_info); |
e6c51ed4 | 4243 | |
d4730f92 BS |
4244 | fsargs.failed = FALSE; |
4245 | fsargs.link_info = link_info; | |
4246 | bfd_map_over_sections (abfd, elf_fake_sections, &fsargs); | |
4247 | if (fsargs.failed) | |
b34976b6 | 4248 | return FALSE; |
252b5132 | 4249 | |
da9f89d4 | 4250 | if (!assign_section_numbers (abfd, link_info)) |
b34976b6 | 4251 | return FALSE; |
252b5132 RH |
4252 | |
4253 | /* The backend linker builds symbol table information itself. */ | |
3516e984 L |
4254 | need_symtab = (link_info == NULL |
4255 | && (bfd_get_symcount (abfd) > 0 | |
4256 | || ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC)) | |
4257 | == HAS_RELOC))); | |
4258 | if (need_symtab) | |
252b5132 RH |
4259 | { |
4260 | /* Non-zero if doing a relocatable link. */ | |
4261 | int relocatable_p = ! (abfd->flags & (EXEC_P | DYNAMIC)); | |
4262 | ||
4263 | if (! swap_out_syms (abfd, &strtab, relocatable_p)) | |
b34976b6 | 4264 | return FALSE; |
252b5132 RH |
4265 | } |
4266 | ||
d4730f92 | 4267 | failed = FALSE; |
1126897b | 4268 | if (link_info == NULL) |
dbb410c3 | 4269 | { |
1126897b | 4270 | bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed); |
dbb410c3 | 4271 | if (failed) |
b34976b6 | 4272 | return FALSE; |
dbb410c3 AM |
4273 | } |
4274 | ||
252b5132 RH |
4275 | shstrtab_hdr = &elf_tdata (abfd)->shstrtab_hdr; |
4276 | /* sh_name was set in prep_headers. */ | |
4277 | shstrtab_hdr->sh_type = SHT_STRTAB; | |
84865015 | 4278 | shstrtab_hdr->sh_flags = bed->elf_strtab_flags; |
252b5132 | 4279 | shstrtab_hdr->sh_addr = 0; |
946748d5 | 4280 | /* sh_size is set in _bfd_elf_assign_file_positions_for_non_load. */ |
252b5132 RH |
4281 | shstrtab_hdr->sh_entsize = 0; |
4282 | shstrtab_hdr->sh_link = 0; | |
4283 | shstrtab_hdr->sh_info = 0; | |
3e19fb8f | 4284 | /* sh_offset is set in _bfd_elf_assign_file_positions_for_non_load. */ |
252b5132 RH |
4285 | shstrtab_hdr->sh_addralign = 1; |
4286 | ||
c84fca4d | 4287 | if (!assign_file_positions_except_relocs (abfd, link_info)) |
b34976b6 | 4288 | return FALSE; |
252b5132 | 4289 | |
3516e984 | 4290 | if (need_symtab) |
252b5132 RH |
4291 | { |
4292 | file_ptr off; | |
4293 | Elf_Internal_Shdr *hdr; | |
4294 | ||
12bd6957 | 4295 | off = elf_next_file_pos (abfd); |
252b5132 | 4296 | |
6a40cf0c | 4297 | hdr = & elf_symtab_hdr (abfd); |
b34976b6 | 4298 | off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE); |
252b5132 | 4299 | |
6a40cf0c NC |
4300 | if (elf_symtab_shndx_list (abfd) != NULL) |
4301 | { | |
4302 | hdr = & elf_symtab_shndx_list (abfd)->hdr; | |
4303 | if (hdr->sh_size != 0) | |
4304 | off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE); | |
4305 | /* FIXME: What about other symtab_shndx sections in the list ? */ | |
4306 | } | |
9ad5cbcf | 4307 | |
252b5132 | 4308 | hdr = &elf_tdata (abfd)->strtab_hdr; |
b34976b6 | 4309 | off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE); |
252b5132 | 4310 | |
12bd6957 | 4311 | elf_next_file_pos (abfd) = off; |
252b5132 RH |
4312 | |
4313 | /* Now that we know where the .strtab section goes, write it | |
08a40648 | 4314 | out. */ |
252b5132 | 4315 | if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0 |
ef10c3ac | 4316 | || ! _bfd_elf_strtab_emit (abfd, strtab)) |
b34976b6 | 4317 | return FALSE; |
ef10c3ac | 4318 | _bfd_elf_strtab_free (strtab); |
252b5132 RH |
4319 | } |
4320 | ||
b34976b6 | 4321 | abfd->output_has_begun = TRUE; |
252b5132 | 4322 | |
b34976b6 | 4323 | return TRUE; |
252b5132 RH |
4324 | } |
4325 | ||
8ded5a0f AM |
4326 | /* Make an initial estimate of the size of the program header. If we |
4327 | get the number wrong here, we'll redo section placement. */ | |
4328 | ||
4329 | static bfd_size_type | |
4330 | get_program_header_size (bfd *abfd, struct bfd_link_info *info) | |
4331 | { | |
4332 | size_t segs; | |
4333 | asection *s; | |
2b05f1b7 | 4334 | const struct elf_backend_data *bed; |
8ded5a0f AM |
4335 | |
4336 | /* Assume we will need exactly two PT_LOAD segments: one for text | |
4337 | and one for data. */ | |
4338 | segs = 2; | |
4339 | ||
4340 | s = bfd_get_section_by_name (abfd, ".interp"); | |
1b9e270b | 4341 | if (s != NULL && (s->flags & SEC_LOAD) != 0 && s->size != 0) |
8ded5a0f AM |
4342 | { |
4343 | /* If we have a loadable interpreter section, we need a | |
4344 | PT_INTERP segment. In this case, assume we also need a | |
4345 | PT_PHDR segment, although that may not be true for all | |
4346 | targets. */ | |
e9a38e0f | 4347 | segs += 2; |
8ded5a0f AM |
4348 | } |
4349 | ||
4350 | if (bfd_get_section_by_name (abfd, ".dynamic") != NULL) | |
4351 | { | |
4352 | /* We need a PT_DYNAMIC segment. */ | |
4353 | ++segs; | |
f210dcff | 4354 | } |
08a40648 | 4355 | |
ceae84aa | 4356 | if (info != NULL && info->relro) |
f210dcff L |
4357 | { |
4358 | /* We need a PT_GNU_RELRO segment. */ | |
4359 | ++segs; | |
8ded5a0f AM |
4360 | } |
4361 | ||
12bd6957 | 4362 | if (elf_eh_frame_hdr (abfd)) |
8ded5a0f AM |
4363 | { |
4364 | /* We need a PT_GNU_EH_FRAME segment. */ | |
4365 | ++segs; | |
4366 | } | |
4367 | ||
12bd6957 | 4368 | if (elf_stack_flags (abfd)) |
8ded5a0f | 4369 | { |
2b05f1b7 L |
4370 | /* We need a PT_GNU_STACK segment. */ |
4371 | ++segs; | |
4372 | } | |
94b11780 | 4373 | |
2b05f1b7 L |
4374 | for (s = abfd->sections; s != NULL; s = s->next) |
4375 | { | |
8ded5a0f | 4376 | if ((s->flags & SEC_LOAD) != 0 |
23e463ed | 4377 | && elf_section_type (s) == SHT_NOTE) |
8ded5a0f | 4378 | { |
23e463ed | 4379 | unsigned int alignment_power; |
8ded5a0f AM |
4380 | /* We need a PT_NOTE segment. */ |
4381 | ++segs; | |
23e463ed L |
4382 | /* Try to create just one PT_NOTE segment for all adjacent |
4383 | loadable SHT_NOTE sections. gABI requires that within a | |
4384 | PT_NOTE segment (and also inside of each SHT_NOTE section) | |
4385 | each note should have the same alignment. So we check | |
4386 | whether the sections are correctly aligned. */ | |
4387 | alignment_power = s->alignment_power; | |
4388 | while (s->next != NULL | |
4389 | && s->next->alignment_power == alignment_power | |
4390 | && (s->next->flags & SEC_LOAD) != 0 | |
4391 | && elf_section_type (s->next) == SHT_NOTE) | |
4392 | s = s->next; | |
8ded5a0f AM |
4393 | } |
4394 | } | |
4395 | ||
4396 | for (s = abfd->sections; s != NULL; s = s->next) | |
4397 | { | |
4398 | if (s->flags & SEC_THREAD_LOCAL) | |
4399 | { | |
4400 | /* We need a PT_TLS segment. */ | |
4401 | ++segs; | |
4402 | break; | |
4403 | } | |
4404 | } | |
4405 | ||
2b05f1b7 | 4406 | bed = get_elf_backend_data (abfd); |
a91e1603 L |
4407 | |
4408 | if ((abfd->flags & D_PAGED) != 0) | |
4409 | { | |
4410 | /* Add a PT_GNU_MBIND segment for each mbind section. */ | |
4411 | unsigned int page_align_power = bfd_log2 (bed->commonpagesize); | |
4412 | for (s = abfd->sections; s != NULL; s = s->next) | |
4413 | if (elf_section_flags (s) & SHF_GNU_MBIND) | |
4414 | { | |
4415 | if (elf_section_data (s)->this_hdr.sh_info | |
4416 | > PT_GNU_MBIND_NUM) | |
4417 | { | |
4418 | _bfd_error_handler | |
4419 | /* xgettext:c-format */ | |
871b3ab2 | 4420 | (_("%pB: GNU_MBIN section `%pA' has invalid sh_info field: %d"), |
a91e1603 L |
4421 | abfd, s, elf_section_data (s)->this_hdr.sh_info); |
4422 | continue; | |
4423 | } | |
4424 | /* Align mbind section to page size. */ | |
4425 | if (s->alignment_power < page_align_power) | |
4426 | s->alignment_power = page_align_power; | |
4427 | segs ++; | |
4428 | } | |
4429 | } | |
4430 | ||
4431 | /* Let the backend count up any program headers it might need. */ | |
4432 | if (bed->elf_backend_additional_program_headers) | |
8ded5a0f AM |
4433 | { |
4434 | int a; | |
4435 | ||
4436 | a = (*bed->elf_backend_additional_program_headers) (abfd, info); | |
4437 | if (a == -1) | |
4438 | abort (); | |
4439 | segs += a; | |
4440 | } | |
4441 | ||
4442 | return segs * bed->s->sizeof_phdr; | |
4443 | } | |
4444 | ||
2ea37f1c NC |
4445 | /* Find the segment that contains the output_section of section. */ |
4446 | ||
4447 | Elf_Internal_Phdr * | |
4448 | _bfd_elf_find_segment_containing_section (bfd * abfd, asection * section) | |
4449 | { | |
4450 | struct elf_segment_map *m; | |
4451 | Elf_Internal_Phdr *p; | |
4452 | ||
12bd6957 | 4453 | for (m = elf_seg_map (abfd), p = elf_tdata (abfd)->phdr; |
2ea37f1c NC |
4454 | m != NULL; |
4455 | m = m->next, p++) | |
4456 | { | |
4457 | int i; | |
4458 | ||
4459 | for (i = m->count - 1; i >= 0; i--) | |
4460 | if (m->sections[i] == section) | |
4461 | return p; | |
4462 | } | |
4463 | ||
4464 | return NULL; | |
4465 | } | |
4466 | ||
252b5132 RH |
4467 | /* Create a mapping from a set of sections to a program segment. */ |
4468 | ||
217aa764 AM |
4469 | static struct elf_segment_map * |
4470 | make_mapping (bfd *abfd, | |
4471 | asection **sections, | |
4472 | unsigned int from, | |
4473 | unsigned int to, | |
4474 | bfd_boolean phdr) | |
252b5132 RH |
4475 | { |
4476 | struct elf_segment_map *m; | |
4477 | unsigned int i; | |
4478 | asection **hdrpp; | |
dc810e39 | 4479 | bfd_size_type amt; |
252b5132 | 4480 | |
00bee008 AM |
4481 | amt = sizeof (struct elf_segment_map) - sizeof (asection *); |
4482 | amt += (to - from) * sizeof (asection *); | |
a50b1753 | 4483 | m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); |
252b5132 RH |
4484 | if (m == NULL) |
4485 | return NULL; | |
4486 | m->next = NULL; | |
4487 | m->p_type = PT_LOAD; | |
4488 | for (i = from, hdrpp = sections + from; i < to; i++, hdrpp++) | |
4489 | m->sections[i - from] = *hdrpp; | |
4490 | m->count = to - from; | |
4491 | ||
4492 | if (from == 0 && phdr) | |
4493 | { | |
4494 | /* Include the headers in the first PT_LOAD segment. */ | |
4495 | m->includes_filehdr = 1; | |
4496 | m->includes_phdrs = 1; | |
4497 | } | |
4498 | ||
4499 | return m; | |
4500 | } | |
4501 | ||
229fcec5 MM |
4502 | /* Create the PT_DYNAMIC segment, which includes DYNSEC. Returns NULL |
4503 | on failure. */ | |
4504 | ||
4505 | struct elf_segment_map * | |
4506 | _bfd_elf_make_dynamic_segment (bfd *abfd, asection *dynsec) | |
4507 | { | |
4508 | struct elf_segment_map *m; | |
4509 | ||
a50b1753 | 4510 | m = (struct elf_segment_map *) bfd_zalloc (abfd, |
07d6d2b8 | 4511 | sizeof (struct elf_segment_map)); |
229fcec5 MM |
4512 | if (m == NULL) |
4513 | return NULL; | |
4514 | m->next = NULL; | |
4515 | m->p_type = PT_DYNAMIC; | |
4516 | m->count = 1; | |
4517 | m->sections[0] = dynsec; | |
08a40648 | 4518 | |
229fcec5 MM |
4519 | return m; |
4520 | } | |
4521 | ||
8ded5a0f | 4522 | /* Possibly add or remove segments from the segment map. */ |
252b5132 | 4523 | |
b34976b6 | 4524 | static bfd_boolean |
3dea8fca AM |
4525 | elf_modify_segment_map (bfd *abfd, |
4526 | struct bfd_link_info *info, | |
4527 | bfd_boolean remove_empty_load) | |
252b5132 | 4528 | { |
252e386e | 4529 | struct elf_segment_map **m; |
8ded5a0f | 4530 | const struct elf_backend_data *bed; |
252b5132 | 4531 | |
8ded5a0f AM |
4532 | /* The placement algorithm assumes that non allocated sections are |
4533 | not in PT_LOAD segments. We ensure this here by removing such | |
4534 | sections from the segment map. We also remove excluded | |
252e386e AM |
4535 | sections. Finally, any PT_LOAD segment without sections is |
4536 | removed. */ | |
12bd6957 | 4537 | m = &elf_seg_map (abfd); |
252e386e | 4538 | while (*m) |
8ded5a0f AM |
4539 | { |
4540 | unsigned int i, new_count; | |
252b5132 | 4541 | |
252e386e | 4542 | for (new_count = 0, i = 0; i < (*m)->count; i++) |
8ded5a0f | 4543 | { |
252e386e AM |
4544 | if (((*m)->sections[i]->flags & SEC_EXCLUDE) == 0 |
4545 | && (((*m)->sections[i]->flags & SEC_ALLOC) != 0 | |
4546 | || (*m)->p_type != PT_LOAD)) | |
8ded5a0f | 4547 | { |
252e386e AM |
4548 | (*m)->sections[new_count] = (*m)->sections[i]; |
4549 | new_count++; | |
8ded5a0f AM |
4550 | } |
4551 | } | |
252e386e | 4552 | (*m)->count = new_count; |
252b5132 | 4553 | |
1a9ccd70 NC |
4554 | if (remove_empty_load |
4555 | && (*m)->p_type == PT_LOAD | |
4556 | && (*m)->count == 0 | |
4557 | && !(*m)->includes_phdrs) | |
252e386e AM |
4558 | *m = (*m)->next; |
4559 | else | |
4560 | m = &(*m)->next; | |
8ded5a0f | 4561 | } |
252b5132 | 4562 | |
8ded5a0f AM |
4563 | bed = get_elf_backend_data (abfd); |
4564 | if (bed->elf_backend_modify_segment_map != NULL) | |
252b5132 | 4565 | { |
252e386e | 4566 | if (!(*bed->elf_backend_modify_segment_map) (abfd, info)) |
8ded5a0f | 4567 | return FALSE; |
252b5132 | 4568 | } |
252b5132 | 4569 | |
8ded5a0f AM |
4570 | return TRUE; |
4571 | } | |
252b5132 | 4572 | |
dbc88fc1 AM |
4573 | #define IS_TBSS(s) \ |
4574 | ((s->flags & (SEC_THREAD_LOCAL | SEC_LOAD)) == SEC_THREAD_LOCAL) | |
4575 | ||
8ded5a0f | 4576 | /* Set up a mapping from BFD sections to program segments. */ |
252b5132 | 4577 | |
8ded5a0f AM |
4578 | bfd_boolean |
4579 | _bfd_elf_map_sections_to_segments (bfd *abfd, struct bfd_link_info *info) | |
4580 | { | |
4581 | unsigned int count; | |
4582 | struct elf_segment_map *m; | |
4583 | asection **sections = NULL; | |
4584 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
3dea8fca | 4585 | bfd_boolean no_user_phdrs; |
252b5132 | 4586 | |
12bd6957 | 4587 | no_user_phdrs = elf_seg_map (abfd) == NULL; |
d324f6d6 RM |
4588 | |
4589 | if (info != NULL) | |
4590 | info->user_phdrs = !no_user_phdrs; | |
4591 | ||
3dea8fca | 4592 | if (no_user_phdrs && bfd_count_sections (abfd) != 0) |
252b5132 | 4593 | { |
8ded5a0f AM |
4594 | asection *s; |
4595 | unsigned int i; | |
4596 | struct elf_segment_map *mfirst; | |
4597 | struct elf_segment_map **pm; | |
4598 | asection *last_hdr; | |
4599 | bfd_vma last_size; | |
00bee008 | 4600 | unsigned int hdr_index; |
8ded5a0f AM |
4601 | bfd_vma maxpagesize; |
4602 | asection **hdrpp; | |
64029e93 | 4603 | bfd_boolean phdr_in_segment; |
8ded5a0f | 4604 | bfd_boolean writable; |
2888249f | 4605 | bfd_boolean executable; |
8ded5a0f AM |
4606 | int tls_count = 0; |
4607 | asection *first_tls = NULL; | |
a91e1603 | 4608 | asection *first_mbind = NULL; |
8ded5a0f AM |
4609 | asection *dynsec, *eh_frame_hdr; |
4610 | bfd_size_type amt; | |
8d06853e | 4611 | bfd_vma addr_mask, wrap_to = 0; |
64029e93 | 4612 | bfd_size_type phdr_size; |
252b5132 | 4613 | |
8ded5a0f | 4614 | /* Select the allocated sections, and sort them. */ |
252b5132 | 4615 | |
a50b1753 | 4616 | sections = (asection **) bfd_malloc2 (bfd_count_sections (abfd), |
07d6d2b8 | 4617 | sizeof (asection *)); |
8ded5a0f | 4618 | if (sections == NULL) |
252b5132 | 4619 | goto error_return; |
252b5132 | 4620 | |
8d06853e AM |
4621 | /* Calculate top address, avoiding undefined behaviour of shift |
4622 | left operator when shift count is equal to size of type | |
4623 | being shifted. */ | |
4624 | addr_mask = ((bfd_vma) 1 << (bfd_arch_bits_per_address (abfd) - 1)) - 1; | |
4625 | addr_mask = (addr_mask << 1) + 1; | |
4626 | ||
8ded5a0f AM |
4627 | i = 0; |
4628 | for (s = abfd->sections; s != NULL; s = s->next) | |
4629 | { | |
4630 | if ((s->flags & SEC_ALLOC) != 0) | |
4631 | { | |
4632 | sections[i] = s; | |
4633 | ++i; | |
8d06853e AM |
4634 | /* A wrapping section potentially clashes with header. */ |
4635 | if (((s->lma + s->size) & addr_mask) < (s->lma & addr_mask)) | |
4636 | wrap_to = (s->lma + s->size) & addr_mask; | |
8ded5a0f AM |
4637 | } |
4638 | } | |
4639 | BFD_ASSERT (i <= bfd_count_sections (abfd)); | |
4640 | count = i; | |
252b5132 | 4641 | |
8ded5a0f | 4642 | qsort (sections, (size_t) count, sizeof (asection *), elf_sort_sections); |
252b5132 | 4643 | |
64029e93 AM |
4644 | phdr_size = elf_program_header_size (abfd); |
4645 | if (phdr_size == (bfd_size_type) -1) | |
4646 | phdr_size = get_program_header_size (abfd, info); | |
4647 | phdr_size += bed->s->sizeof_ehdr; | |
4648 | maxpagesize = bed->maxpagesize; | |
4649 | if (maxpagesize == 0) | |
4650 | maxpagesize = 1; | |
4651 | phdr_in_segment = info != NULL && info->load_phdrs; | |
4652 | if (count != 0 | |
4653 | && (((sections[0]->lma & addr_mask) & (maxpagesize - 1)) | |
4654 | >= (phdr_size & (maxpagesize - 1)))) | |
4655 | /* For compatibility with old scripts that may not be using | |
4656 | SIZEOF_HEADERS, add headers when it looks like space has | |
4657 | been left for them. */ | |
4658 | phdr_in_segment = TRUE; | |
252b5132 | 4659 | |
64029e93 | 4660 | /* Build the mapping. */ |
8ded5a0f AM |
4661 | mfirst = NULL; |
4662 | pm = &mfirst; | |
252b5132 | 4663 | |
8ded5a0f AM |
4664 | /* If we have a .interp section, then create a PT_PHDR segment for |
4665 | the program headers and a PT_INTERP segment for the .interp | |
4666 | section. */ | |
4667 | s = bfd_get_section_by_name (abfd, ".interp"); | |
1b9e270b | 4668 | if (s != NULL && (s->flags & SEC_LOAD) != 0 && s->size != 0) |
8ded5a0f AM |
4669 | { |
4670 | amt = sizeof (struct elf_segment_map); | |
a50b1753 | 4671 | m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); |
8ded5a0f AM |
4672 | if (m == NULL) |
4673 | goto error_return; | |
4674 | m->next = NULL; | |
4675 | m->p_type = PT_PHDR; | |
f882209d | 4676 | m->p_flags = PF_R; |
8ded5a0f AM |
4677 | m->p_flags_valid = 1; |
4678 | m->includes_phdrs = 1; | |
64029e93 | 4679 | phdr_in_segment = TRUE; |
8ded5a0f AM |
4680 | *pm = m; |
4681 | pm = &m->next; | |
252b5132 | 4682 | |
8ded5a0f | 4683 | amt = sizeof (struct elf_segment_map); |
a50b1753 | 4684 | m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); |
8ded5a0f AM |
4685 | if (m == NULL) |
4686 | goto error_return; | |
4687 | m->next = NULL; | |
4688 | m->p_type = PT_INTERP; | |
4689 | m->count = 1; | |
4690 | m->sections[0] = s; | |
4691 | ||
4692 | *pm = m; | |
4693 | pm = &m->next; | |
252b5132 | 4694 | } |
8ded5a0f AM |
4695 | |
4696 | /* Look through the sections. We put sections in the same program | |
4697 | segment when the start of the second section can be placed within | |
4698 | a few bytes of the end of the first section. */ | |
4699 | last_hdr = NULL; | |
4700 | last_size = 0; | |
00bee008 | 4701 | hdr_index = 0; |
8ded5a0f | 4702 | writable = FALSE; |
2888249f | 4703 | executable = FALSE; |
8ded5a0f AM |
4704 | dynsec = bfd_get_section_by_name (abfd, ".dynamic"); |
4705 | if (dynsec != NULL | |
4706 | && (dynsec->flags & SEC_LOAD) == 0) | |
4707 | dynsec = NULL; | |
4708 | ||
64029e93 AM |
4709 | if ((abfd->flags & D_PAGED) == 0) |
4710 | phdr_in_segment = FALSE; | |
4711 | ||
8ded5a0f AM |
4712 | /* Deal with -Ttext or something similar such that the first section |
4713 | is not adjacent to the program headers. This is an | |
4714 | approximation, since at this point we don't know exactly how many | |
4715 | program headers we will need. */ | |
64029e93 | 4716 | if (phdr_in_segment && count > 0) |
252b5132 | 4717 | { |
64029e93 AM |
4718 | bfd_vma phdr_lma; |
4719 | bfd_boolean separate_phdr = FALSE; | |
4720 | ||
4721 | phdr_lma = (sections[0]->lma - phdr_size) & addr_mask & -maxpagesize; | |
4722 | if (info != NULL | |
4723 | && info->separate_code | |
4724 | && (sections[0]->flags & SEC_CODE) != 0) | |
1a9ccd70 | 4725 | { |
64029e93 AM |
4726 | /* If data sections should be separate from code and |
4727 | thus not executable, and the first section is | |
4728 | executable then put the file and program headers in | |
4729 | their own PT_LOAD. */ | |
4730 | separate_phdr = TRUE; | |
4731 | if ((((phdr_lma + phdr_size - 1) & addr_mask & -maxpagesize) | |
4732 | == (sections[0]->lma & addr_mask & -maxpagesize))) | |
4733 | { | |
4734 | /* The file and program headers are currently on the | |
4735 | same page as the first section. Put them on the | |
4736 | previous page if we can. */ | |
4737 | if (phdr_lma >= maxpagesize) | |
4738 | phdr_lma -= maxpagesize; | |
4739 | else | |
4740 | separate_phdr = FALSE; | |
4741 | } | |
4742 | } | |
4743 | if ((sections[0]->lma & addr_mask) < phdr_lma | |
4744 | || (sections[0]->lma & addr_mask) < phdr_size) | |
4745 | /* If file and program headers would be placed at the end | |
4746 | of memory then it's probably better to omit them. */ | |
4747 | phdr_in_segment = FALSE; | |
4748 | else if (phdr_lma < wrap_to) | |
4749 | /* If a section wraps around to where we'll be placing | |
4750 | file and program headers, then the headers will be | |
4751 | overwritten. */ | |
4752 | phdr_in_segment = FALSE; | |
4753 | else if (separate_phdr) | |
4754 | { | |
4755 | m = make_mapping (abfd, sections, 0, 0, phdr_in_segment); | |
4756 | if (m == NULL) | |
4757 | goto error_return; | |
4758 | m->p_paddr = phdr_lma; | |
4759 | m->p_vaddr_offset | |
4760 | = (sections[0]->vma - phdr_size) & addr_mask & -maxpagesize; | |
4761 | m->p_paddr_valid = 1; | |
4762 | *pm = m; | |
4763 | pm = &m->next; | |
4764 | phdr_in_segment = FALSE; | |
1a9ccd70 | 4765 | } |
252b5132 RH |
4766 | } |
4767 | ||
8ded5a0f | 4768 | for (i = 0, hdrpp = sections; i < count; i++, hdrpp++) |
252b5132 | 4769 | { |
8ded5a0f AM |
4770 | asection *hdr; |
4771 | bfd_boolean new_segment; | |
4772 | ||
4773 | hdr = *hdrpp; | |
4774 | ||
4775 | /* See if this section and the last one will fit in the same | |
4776 | segment. */ | |
4777 | ||
4778 | if (last_hdr == NULL) | |
4779 | { | |
4780 | /* If we don't have a segment yet, then we don't need a new | |
4781 | one (we build the last one after this loop). */ | |
4782 | new_segment = FALSE; | |
4783 | } | |
4784 | else if (last_hdr->lma - last_hdr->vma != hdr->lma - hdr->vma) | |
4785 | { | |
4786 | /* If this section has a different relation between the | |
4787 | virtual address and the load address, then we need a new | |
4788 | segment. */ | |
4789 | new_segment = TRUE; | |
4790 | } | |
b5599592 AM |
4791 | else if (hdr->lma < last_hdr->lma + last_size |
4792 | || last_hdr->lma + last_size < last_hdr->lma) | |
4793 | { | |
4794 | /* If this section has a load address that makes it overlap | |
4795 | the previous section, then we need a new segment. */ | |
4796 | new_segment = TRUE; | |
4797 | } | |
76cb3a89 AM |
4798 | else if ((abfd->flags & D_PAGED) != 0 |
4799 | && (((last_hdr->lma + last_size - 1) & -maxpagesize) | |
4800 | == (hdr->lma & -maxpagesize))) | |
4801 | { | |
4802 | /* If we are demand paged then we can't map two disk | |
4803 | pages onto the same memory page. */ | |
4804 | new_segment = FALSE; | |
4805 | } | |
39948a60 NC |
4806 | /* In the next test we have to be careful when last_hdr->lma is close |
4807 | to the end of the address space. If the aligned address wraps | |
4808 | around to the start of the address space, then there are no more | |
4809 | pages left in memory and it is OK to assume that the current | |
4810 | section can be included in the current segment. */ | |
76cb3a89 AM |
4811 | else if ((BFD_ALIGN (last_hdr->lma + last_size, maxpagesize) |
4812 | + maxpagesize > last_hdr->lma) | |
4813 | && (BFD_ALIGN (last_hdr->lma + last_size, maxpagesize) | |
4814 | + maxpagesize <= hdr->lma)) | |
8ded5a0f AM |
4815 | { |
4816 | /* If putting this section in this segment would force us to | |
4817 | skip a page in the segment, then we need a new segment. */ | |
4818 | new_segment = TRUE; | |
4819 | } | |
4820 | else if ((last_hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0 | |
76cb3a89 | 4821 | && (hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) != 0) |
8ded5a0f | 4822 | { |
e5654c0f AM |
4823 | /* We don't want to put a loaded section after a |
4824 | nonloaded (ie. bss style) section in the same segment | |
4825 | as that will force the non-loaded section to be loaded. | |
76cb3a89 | 4826 | Consider .tbss sections as loaded for this purpose. */ |
8ded5a0f AM |
4827 | new_segment = TRUE; |
4828 | } | |
4829 | else if ((abfd->flags & D_PAGED) == 0) | |
4830 | { | |
4831 | /* If the file is not demand paged, which means that we | |
4832 | don't require the sections to be correctly aligned in the | |
4833 | file, then there is no other reason for a new segment. */ | |
4834 | new_segment = FALSE; | |
4835 | } | |
2888249f L |
4836 | else if (info != NULL |
4837 | && info->separate_code | |
4838 | && executable != ((hdr->flags & SEC_CODE) != 0)) | |
4839 | { | |
4840 | new_segment = TRUE; | |
4841 | } | |
8ded5a0f | 4842 | else if (! writable |
76cb3a89 | 4843 | && (hdr->flags & SEC_READONLY) == 0) |
8ded5a0f AM |
4844 | { |
4845 | /* We don't want to put a writable section in a read only | |
76cb3a89 | 4846 | segment. */ |
8ded5a0f AM |
4847 | new_segment = TRUE; |
4848 | } | |
4849 | else | |
4850 | { | |
4851 | /* Otherwise, we can use the same segment. */ | |
4852 | new_segment = FALSE; | |
4853 | } | |
4854 | ||
2889e75b | 4855 | /* Allow interested parties a chance to override our decision. */ |
ceae84aa AM |
4856 | if (last_hdr != NULL |
4857 | && info != NULL | |
4858 | && info->callbacks->override_segment_assignment != NULL) | |
4859 | new_segment | |
4860 | = info->callbacks->override_segment_assignment (info, abfd, hdr, | |
4861 | last_hdr, | |
4862 | new_segment); | |
2889e75b | 4863 | |
8ded5a0f AM |
4864 | if (! new_segment) |
4865 | { | |
4866 | if ((hdr->flags & SEC_READONLY) == 0) | |
4867 | writable = TRUE; | |
2888249f L |
4868 | if ((hdr->flags & SEC_CODE) != 0) |
4869 | executable = TRUE; | |
8ded5a0f AM |
4870 | last_hdr = hdr; |
4871 | /* .tbss sections effectively have zero size. */ | |
dbc88fc1 | 4872 | last_size = !IS_TBSS (hdr) ? hdr->size : 0; |
8ded5a0f AM |
4873 | continue; |
4874 | } | |
4875 | ||
4876 | /* We need a new program segment. We must create a new program | |
00bee008 | 4877 | header holding all the sections from hdr_index until hdr. */ |
8ded5a0f | 4878 | |
00bee008 | 4879 | m = make_mapping (abfd, sections, hdr_index, i, phdr_in_segment); |
8ded5a0f AM |
4880 | if (m == NULL) |
4881 | goto error_return; | |
4882 | ||
4883 | *pm = m; | |
4884 | pm = &m->next; | |
4885 | ||
252b5132 | 4886 | if ((hdr->flags & SEC_READONLY) == 0) |
b34976b6 | 4887 | writable = TRUE; |
8ded5a0f AM |
4888 | else |
4889 | writable = FALSE; | |
4890 | ||
2888249f L |
4891 | if ((hdr->flags & SEC_CODE) == 0) |
4892 | executable = FALSE; | |
4893 | else | |
4894 | executable = TRUE; | |
4895 | ||
baaff79e JJ |
4896 | last_hdr = hdr; |
4897 | /* .tbss sections effectively have zero size. */ | |
dbc88fc1 | 4898 | last_size = !IS_TBSS (hdr) ? hdr->size : 0; |
00bee008 | 4899 | hdr_index = i; |
8ded5a0f | 4900 | phdr_in_segment = FALSE; |
252b5132 RH |
4901 | } |
4902 | ||
86b2281f AM |
4903 | /* Create a final PT_LOAD program segment, but not if it's just |
4904 | for .tbss. */ | |
4905 | if (last_hdr != NULL | |
00bee008 | 4906 | && (i - hdr_index != 1 |
dbc88fc1 | 4907 | || !IS_TBSS (last_hdr))) |
8ded5a0f | 4908 | { |
00bee008 | 4909 | m = make_mapping (abfd, sections, hdr_index, i, phdr_in_segment); |
8ded5a0f AM |
4910 | if (m == NULL) |
4911 | goto error_return; | |
252b5132 | 4912 | |
8ded5a0f AM |
4913 | *pm = m; |
4914 | pm = &m->next; | |
4915 | } | |
252b5132 | 4916 | |
8ded5a0f AM |
4917 | /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */ |
4918 | if (dynsec != NULL) | |
4919 | { | |
4920 | m = _bfd_elf_make_dynamic_segment (abfd, dynsec); | |
4921 | if (m == NULL) | |
4922 | goto error_return; | |
4923 | *pm = m; | |
4924 | pm = &m->next; | |
4925 | } | |
252b5132 | 4926 | |
23e463ed | 4927 | /* For each batch of consecutive loadable SHT_NOTE sections, |
1c5265b5 JJ |
4928 | add a PT_NOTE segment. We don't use bfd_get_section_by_name, |
4929 | because if we link together nonloadable .note sections and | |
4930 | loadable .note sections, we will generate two .note sections | |
23e463ed | 4931 | in the output file. */ |
8ded5a0f AM |
4932 | for (s = abfd->sections; s != NULL; s = s->next) |
4933 | { | |
4934 | if ((s->flags & SEC_LOAD) != 0 | |
23e463ed | 4935 | && elf_section_type (s) == SHT_NOTE) |
8ded5a0f | 4936 | { |
1c5265b5 | 4937 | asection *s2; |
23e463ed | 4938 | unsigned int alignment_power = s->alignment_power; |
91d6fa6a NC |
4939 | |
4940 | count = 1; | |
23e463ed L |
4941 | for (s2 = s; s2->next != NULL; s2 = s2->next) |
4942 | { | |
4943 | if (s2->next->alignment_power == alignment_power | |
4944 | && (s2->next->flags & SEC_LOAD) != 0 | |
4945 | && elf_section_type (s2->next) == SHT_NOTE | |
4946 | && align_power (s2->lma + s2->size, | |
4947 | alignment_power) | |
4948 | == s2->next->lma) | |
4949 | count++; | |
4950 | else | |
4951 | break; | |
4952 | } | |
00bee008 AM |
4953 | amt = sizeof (struct elf_segment_map) - sizeof (asection *); |
4954 | amt += count * sizeof (asection *); | |
a50b1753 | 4955 | m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); |
8ded5a0f AM |
4956 | if (m == NULL) |
4957 | goto error_return; | |
4958 | m->next = NULL; | |
4959 | m->p_type = PT_NOTE; | |
1c5265b5 JJ |
4960 | m->count = count; |
4961 | while (count > 1) | |
4962 | { | |
4963 | m->sections[m->count - count--] = s; | |
4964 | BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0); | |
4965 | s = s->next; | |
4966 | } | |
4967 | m->sections[m->count - 1] = s; | |
4968 | BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0); | |
8ded5a0f AM |
4969 | *pm = m; |
4970 | pm = &m->next; | |
4971 | } | |
4972 | if (s->flags & SEC_THREAD_LOCAL) | |
4973 | { | |
4974 | if (! tls_count) | |
4975 | first_tls = s; | |
4976 | tls_count++; | |
4977 | } | |
a91e1603 L |
4978 | if (first_mbind == NULL |
4979 | && (elf_section_flags (s) & SHF_GNU_MBIND) != 0) | |
4980 | first_mbind = s; | |
8ded5a0f | 4981 | } |
252b5132 | 4982 | |
8ded5a0f AM |
4983 | /* If there are any SHF_TLS output sections, add PT_TLS segment. */ |
4984 | if (tls_count > 0) | |
4985 | { | |
00bee008 AM |
4986 | amt = sizeof (struct elf_segment_map) - sizeof (asection *); |
4987 | amt += tls_count * sizeof (asection *); | |
a50b1753 | 4988 | m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); |
8ded5a0f AM |
4989 | if (m == NULL) |
4990 | goto error_return; | |
4991 | m->next = NULL; | |
4992 | m->p_type = PT_TLS; | |
4993 | m->count = tls_count; | |
4994 | /* Mandated PF_R. */ | |
4995 | m->p_flags = PF_R; | |
4996 | m->p_flags_valid = 1; | |
d923cae0 | 4997 | s = first_tls; |
91d6fa6a | 4998 | for (i = 0; i < (unsigned int) tls_count; ++i) |
8ded5a0f | 4999 | { |
d923cae0 L |
5000 | if ((s->flags & SEC_THREAD_LOCAL) == 0) |
5001 | { | |
5002 | _bfd_error_handler | |
871b3ab2 | 5003 | (_("%pB: TLS sections are not adjacent:"), abfd); |
d923cae0 L |
5004 | s = first_tls; |
5005 | i = 0; | |
5006 | while (i < (unsigned int) tls_count) | |
5007 | { | |
5008 | if ((s->flags & SEC_THREAD_LOCAL) != 0) | |
5009 | { | |
871b3ab2 | 5010 | _bfd_error_handler (_(" TLS: %pA"), s); |
d923cae0 L |
5011 | i++; |
5012 | } | |
5013 | else | |
871b3ab2 | 5014 | _bfd_error_handler (_(" non-TLS: %pA"), s); |
d923cae0 L |
5015 | s = s->next; |
5016 | } | |
5017 | bfd_set_error (bfd_error_bad_value); | |
5018 | goto error_return; | |
5019 | } | |
5020 | m->sections[i] = s; | |
5021 | s = s->next; | |
8ded5a0f | 5022 | } |
252b5132 | 5023 | |
8ded5a0f AM |
5024 | *pm = m; |
5025 | pm = &m->next; | |
5026 | } | |
252b5132 | 5027 | |
a91e1603 L |
5028 | if (first_mbind && (abfd->flags & D_PAGED) != 0) |
5029 | for (s = first_mbind; s != NULL; s = s->next) | |
5030 | if ((elf_section_flags (s) & SHF_GNU_MBIND) != 0 | |
5031 | && (elf_section_data (s)->this_hdr.sh_info | |
5032 | <= PT_GNU_MBIND_NUM)) | |
5033 | { | |
5034 | /* Mandated PF_R. */ | |
5035 | unsigned long p_flags = PF_R; | |
5036 | if ((s->flags & SEC_READONLY) == 0) | |
5037 | p_flags |= PF_W; | |
5038 | if ((s->flags & SEC_CODE) != 0) | |
5039 | p_flags |= PF_X; | |
5040 | ||
5041 | amt = sizeof (struct elf_segment_map) + sizeof (asection *); | |
5042 | m = bfd_zalloc (abfd, amt); | |
5043 | if (m == NULL) | |
5044 | goto error_return; | |
5045 | m->next = NULL; | |
5046 | m->p_type = (PT_GNU_MBIND_LO | |
5047 | + elf_section_data (s)->this_hdr.sh_info); | |
5048 | m->count = 1; | |
5049 | m->p_flags_valid = 1; | |
5050 | m->sections[0] = s; | |
5051 | m->p_flags = p_flags; | |
5052 | ||
5053 | *pm = m; | |
5054 | pm = &m->next; | |
5055 | } | |
5056 | ||
8ded5a0f AM |
5057 | /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME |
5058 | segment. */ | |
12bd6957 | 5059 | eh_frame_hdr = elf_eh_frame_hdr (abfd); |
8ded5a0f AM |
5060 | if (eh_frame_hdr != NULL |
5061 | && (eh_frame_hdr->output_section->flags & SEC_LOAD) != 0) | |
252b5132 | 5062 | { |
dc810e39 | 5063 | amt = sizeof (struct elf_segment_map); |
a50b1753 | 5064 | m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); |
252b5132 RH |
5065 | if (m == NULL) |
5066 | goto error_return; | |
5067 | m->next = NULL; | |
8ded5a0f | 5068 | m->p_type = PT_GNU_EH_FRAME; |
252b5132 | 5069 | m->count = 1; |
8ded5a0f | 5070 | m->sections[0] = eh_frame_hdr->output_section; |
252b5132 RH |
5071 | |
5072 | *pm = m; | |
5073 | pm = &m->next; | |
5074 | } | |
13ae64f3 | 5075 | |
12bd6957 | 5076 | if (elf_stack_flags (abfd)) |
13ae64f3 | 5077 | { |
8ded5a0f | 5078 | amt = sizeof (struct elf_segment_map); |
a50b1753 | 5079 | m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); |
8ded5a0f AM |
5080 | if (m == NULL) |
5081 | goto error_return; | |
5082 | m->next = NULL; | |
2b05f1b7 | 5083 | m->p_type = PT_GNU_STACK; |
12bd6957 | 5084 | m->p_flags = elf_stack_flags (abfd); |
04c3a755 | 5085 | m->p_align = bed->stack_align; |
8ded5a0f | 5086 | m->p_flags_valid = 1; |
04c3a755 NS |
5087 | m->p_align_valid = m->p_align != 0; |
5088 | if (info->stacksize > 0) | |
5089 | { | |
5090 | m->p_size = info->stacksize; | |
5091 | m->p_size_valid = 1; | |
5092 | } | |
252b5132 | 5093 | |
8ded5a0f AM |
5094 | *pm = m; |
5095 | pm = &m->next; | |
5096 | } | |
65765700 | 5097 | |
ceae84aa | 5098 | if (info != NULL && info->relro) |
8ded5a0f | 5099 | { |
f210dcff L |
5100 | for (m = mfirst; m != NULL; m = m->next) |
5101 | { | |
3832a4d8 AM |
5102 | if (m->p_type == PT_LOAD |
5103 | && m->count != 0 | |
5104 | && m->sections[0]->vma >= info->relro_start | |
5105 | && m->sections[0]->vma < info->relro_end) | |
f210dcff | 5106 | { |
3832a4d8 AM |
5107 | i = m->count; |
5108 | while (--i != (unsigned) -1) | |
5109 | if ((m->sections[i]->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) | |
5110 | == (SEC_LOAD | SEC_HAS_CONTENTS)) | |
5111 | break; | |
5112 | ||
43a8475c | 5113 | if (i != (unsigned) -1) |
f210dcff L |
5114 | break; |
5115 | } | |
be01b344 | 5116 | } |
f210dcff L |
5117 | |
5118 | /* Make a PT_GNU_RELRO segment only when it isn't empty. */ | |
5119 | if (m != NULL) | |
5120 | { | |
5121 | amt = sizeof (struct elf_segment_map); | |
a50b1753 | 5122 | m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); |
f210dcff L |
5123 | if (m == NULL) |
5124 | goto error_return; | |
5125 | m->next = NULL; | |
5126 | m->p_type = PT_GNU_RELRO; | |
f210dcff L |
5127 | *pm = m; |
5128 | pm = &m->next; | |
5129 | } | |
8ded5a0f | 5130 | } |
9ee5e499 | 5131 | |
8ded5a0f | 5132 | free (sections); |
12bd6957 | 5133 | elf_seg_map (abfd) = mfirst; |
9ee5e499 JJ |
5134 | } |
5135 | ||
3dea8fca | 5136 | if (!elf_modify_segment_map (abfd, info, no_user_phdrs)) |
8ded5a0f | 5137 | return FALSE; |
8c37241b | 5138 | |
12bd6957 | 5139 | for (count = 0, m = elf_seg_map (abfd); m != NULL; m = m->next) |
8ded5a0f | 5140 | ++count; |
12bd6957 | 5141 | elf_program_header_size (abfd) = count * bed->s->sizeof_phdr; |
252b5132 | 5142 | |
b34976b6 | 5143 | return TRUE; |
252b5132 RH |
5144 | |
5145 | error_return: | |
5146 | if (sections != NULL) | |
5147 | free (sections); | |
b34976b6 | 5148 | return FALSE; |
252b5132 RH |
5149 | } |
5150 | ||
5151 | /* Sort sections by address. */ | |
5152 | ||
5153 | static int | |
217aa764 | 5154 | elf_sort_sections (const void *arg1, const void *arg2) |
252b5132 RH |
5155 | { |
5156 | const asection *sec1 = *(const asection **) arg1; | |
5157 | const asection *sec2 = *(const asection **) arg2; | |
eecdbe52 | 5158 | bfd_size_type size1, size2; |
252b5132 RH |
5159 | |
5160 | /* Sort by LMA first, since this is the address used to | |
5161 | place the section into a segment. */ | |
5162 | if (sec1->lma < sec2->lma) | |
5163 | return -1; | |
5164 | else if (sec1->lma > sec2->lma) | |
5165 | return 1; | |
5166 | ||
5167 | /* Then sort by VMA. Normally the LMA and the VMA will be | |
5168 | the same, and this will do nothing. */ | |
5169 | if (sec1->vma < sec2->vma) | |
5170 | return -1; | |
5171 | else if (sec1->vma > sec2->vma) | |
5172 | return 1; | |
5173 | ||
5174 | /* Put !SEC_LOAD sections after SEC_LOAD ones. */ | |
5175 | ||
07c6e936 | 5176 | #define TOEND(x) (((x)->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0) |
252b5132 RH |
5177 | |
5178 | if (TOEND (sec1)) | |
5179 | { | |
5180 | if (TOEND (sec2)) | |
00a7cdc5 | 5181 | { |
67ce483b | 5182 | /* If the indices are the same, do not return 0 |
00a7cdc5 NC |
5183 | here, but continue to try the next comparison. */ |
5184 | if (sec1->target_index - sec2->target_index != 0) | |
5185 | return sec1->target_index - sec2->target_index; | |
5186 | } | |
252b5132 RH |
5187 | else |
5188 | return 1; | |
5189 | } | |
00a7cdc5 | 5190 | else if (TOEND (sec2)) |
252b5132 RH |
5191 | return -1; |
5192 | ||
5193 | #undef TOEND | |
5194 | ||
00a7cdc5 NC |
5195 | /* Sort by size, to put zero sized sections |
5196 | before others at the same address. */ | |
252b5132 | 5197 | |
eea6121a AM |
5198 | size1 = (sec1->flags & SEC_LOAD) ? sec1->size : 0; |
5199 | size2 = (sec2->flags & SEC_LOAD) ? sec2->size : 0; | |
eecdbe52 JJ |
5200 | |
5201 | if (size1 < size2) | |
252b5132 | 5202 | return -1; |
eecdbe52 | 5203 | if (size1 > size2) |
252b5132 RH |
5204 | return 1; |
5205 | ||
5206 | return sec1->target_index - sec2->target_index; | |
5207 | } | |
5208 | ||
340b6d91 AC |
5209 | /* Ian Lance Taylor writes: |
5210 | ||
5211 | We shouldn't be using % with a negative signed number. That's just | |
5212 | not good. We have to make sure either that the number is not | |
5213 | negative, or that the number has an unsigned type. When the types | |
5214 | are all the same size they wind up as unsigned. When file_ptr is a | |
5215 | larger signed type, the arithmetic winds up as signed long long, | |
5216 | which is wrong. | |
5217 | ||
5218 | What we're trying to say here is something like ``increase OFF by | |
5219 | the least amount that will cause it to be equal to the VMA modulo | |
5220 | the page size.'' */ | |
5221 | /* In other words, something like: | |
5222 | ||
5223 | vma_offset = m->sections[0]->vma % bed->maxpagesize; | |
5224 | off_offset = off % bed->maxpagesize; | |
5225 | if (vma_offset < off_offset) | |
5226 | adjustment = vma_offset + bed->maxpagesize - off_offset; | |
5227 | else | |
5228 | adjustment = vma_offset - off_offset; | |
08a40648 | 5229 | |
de194d85 | 5230 | which can be collapsed into the expression below. */ |
340b6d91 AC |
5231 | |
5232 | static file_ptr | |
5233 | vma_page_aligned_bias (bfd_vma vma, ufile_ptr off, bfd_vma maxpagesize) | |
5234 | { | |
dc9155b2 NC |
5235 | /* PR binutils/16199: Handle an alignment of zero. */ |
5236 | if (maxpagesize == 0) | |
5237 | maxpagesize = 1; | |
340b6d91 AC |
5238 | return ((vma - off) % maxpagesize); |
5239 | } | |
5240 | ||
6d33f217 L |
5241 | static void |
5242 | print_segment_map (const struct elf_segment_map *m) | |
5243 | { | |
5244 | unsigned int j; | |
5245 | const char *pt = get_segment_type (m->p_type); | |
5246 | char buf[32]; | |
5247 | ||
5248 | if (pt == NULL) | |
5249 | { | |
5250 | if (m->p_type >= PT_LOPROC && m->p_type <= PT_HIPROC) | |
5251 | sprintf (buf, "LOPROC+%7.7x", | |
5252 | (unsigned int) (m->p_type - PT_LOPROC)); | |
5253 | else if (m->p_type >= PT_LOOS && m->p_type <= PT_HIOS) | |
5254 | sprintf (buf, "LOOS+%7.7x", | |
5255 | (unsigned int) (m->p_type - PT_LOOS)); | |
5256 | else | |
5257 | snprintf (buf, sizeof (buf), "%8.8x", | |
5258 | (unsigned int) m->p_type); | |
5259 | pt = buf; | |
5260 | } | |
4a97a0e5 | 5261 | fflush (stdout); |
6d33f217 L |
5262 | fprintf (stderr, "%s:", pt); |
5263 | for (j = 0; j < m->count; j++) | |
5264 | fprintf (stderr, " %s", m->sections [j]->name); | |
5265 | putc ('\n',stderr); | |
4a97a0e5 | 5266 | fflush (stderr); |
6d33f217 L |
5267 | } |
5268 | ||
32812159 AM |
5269 | static bfd_boolean |
5270 | write_zeros (bfd *abfd, file_ptr pos, bfd_size_type len) | |
5271 | { | |
5272 | void *buf; | |
5273 | bfd_boolean ret; | |
5274 | ||
5275 | if (bfd_seek (abfd, pos, SEEK_SET) != 0) | |
5276 | return FALSE; | |
5277 | buf = bfd_zmalloc (len); | |
5278 | if (buf == NULL) | |
5279 | return FALSE; | |
5280 | ret = bfd_bwrite (buf, len, abfd) == len; | |
5281 | free (buf); | |
5282 | return ret; | |
5283 | } | |
5284 | ||
252b5132 RH |
5285 | /* Assign file positions to the sections based on the mapping from |
5286 | sections to segments. This function also sets up some fields in | |
f3520d2f | 5287 | the file header. */ |
252b5132 | 5288 | |
b34976b6 | 5289 | static bfd_boolean |
f3520d2f AM |
5290 | assign_file_positions_for_load_sections (bfd *abfd, |
5291 | struct bfd_link_info *link_info) | |
252b5132 RH |
5292 | { |
5293 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
252b5132 | 5294 | struct elf_segment_map *m; |
252b5132 | 5295 | Elf_Internal_Phdr *phdrs; |
252b5132 | 5296 | Elf_Internal_Phdr *p; |
02bf8d82 | 5297 | file_ptr off; |
3f570048 | 5298 | bfd_size_type maxpagesize; |
a8c75b76 | 5299 | unsigned int pt_load_count = 0; |
f3520d2f | 5300 | unsigned int alloc; |
0920dee7 | 5301 | unsigned int i, j; |
2b0bc088 | 5302 | bfd_vma header_pad = 0; |
252b5132 | 5303 | |
e36284ab | 5304 | if (link_info == NULL |
ceae84aa | 5305 | && !_bfd_elf_map_sections_to_segments (abfd, link_info)) |
8ded5a0f | 5306 | return FALSE; |
252b5132 | 5307 | |
8ded5a0f | 5308 | alloc = 0; |
12bd6957 | 5309 | for (m = elf_seg_map (abfd); m != NULL; m = m->next) |
2b0bc088 NC |
5310 | { |
5311 | ++alloc; | |
5312 | if (m->header_size) | |
5313 | header_pad = m->header_size; | |
5314 | } | |
252b5132 | 5315 | |
82f2dbf7 NC |
5316 | if (alloc) |
5317 | { | |
5318 | elf_elfheader (abfd)->e_phoff = bed->s->sizeof_ehdr; | |
5319 | elf_elfheader (abfd)->e_phentsize = bed->s->sizeof_phdr; | |
5320 | } | |
5321 | else | |
5322 | { | |
5323 | /* PR binutils/12467. */ | |
5324 | elf_elfheader (abfd)->e_phoff = 0; | |
5325 | elf_elfheader (abfd)->e_phentsize = 0; | |
5326 | } | |
d324f6d6 | 5327 | |
8ded5a0f | 5328 | elf_elfheader (abfd)->e_phnum = alloc; |
252b5132 | 5329 | |
12bd6957 AM |
5330 | if (elf_program_header_size (abfd) == (bfd_size_type) -1) |
5331 | elf_program_header_size (abfd) = alloc * bed->s->sizeof_phdr; | |
8ded5a0f | 5332 | else |
12bd6957 | 5333 | BFD_ASSERT (elf_program_header_size (abfd) |
59e0647f | 5334 | >= alloc * bed->s->sizeof_phdr); |
252b5132 RH |
5335 | |
5336 | if (alloc == 0) | |
f3520d2f | 5337 | { |
12bd6957 | 5338 | elf_next_file_pos (abfd) = bed->s->sizeof_ehdr; |
8ded5a0f | 5339 | return TRUE; |
f3520d2f | 5340 | } |
252b5132 | 5341 | |
12bd6957 | 5342 | /* We're writing the size in elf_program_header_size (abfd), |
57268894 HPN |
5343 | see assign_file_positions_except_relocs, so make sure we have |
5344 | that amount allocated, with trailing space cleared. | |
12bd6957 AM |
5345 | The variable alloc contains the computed need, while |
5346 | elf_program_header_size (abfd) contains the size used for the | |
57268894 HPN |
5347 | layout. |
5348 | See ld/emultempl/elf-generic.em:gld${EMULATION_NAME}_map_segments | |
5349 | where the layout is forced to according to a larger size in the | |
5350 | last iterations for the testcase ld-elf/header. */ | |
12bd6957 | 5351 | BFD_ASSERT (elf_program_header_size (abfd) % bed->s->sizeof_phdr |
57268894 | 5352 | == 0); |
a50b1753 NC |
5353 | phdrs = (Elf_Internal_Phdr *) |
5354 | bfd_zalloc2 (abfd, | |
07d6d2b8 AM |
5355 | (elf_program_header_size (abfd) / bed->s->sizeof_phdr), |
5356 | sizeof (Elf_Internal_Phdr)); | |
f3520d2f | 5357 | elf_tdata (abfd)->phdr = phdrs; |
252b5132 | 5358 | if (phdrs == NULL) |
b34976b6 | 5359 | return FALSE; |
252b5132 | 5360 | |
3f570048 AM |
5361 | maxpagesize = 1; |
5362 | if ((abfd->flags & D_PAGED) != 0) | |
5363 | maxpagesize = bed->maxpagesize; | |
5364 | ||
252b5132 RH |
5365 | off = bed->s->sizeof_ehdr; |
5366 | off += alloc * bed->s->sizeof_phdr; | |
2b0bc088 NC |
5367 | if (header_pad < (bfd_vma) off) |
5368 | header_pad = 0; | |
5369 | else | |
5370 | header_pad -= off; | |
5371 | off += header_pad; | |
252b5132 | 5372 | |
12bd6957 | 5373 | for (m = elf_seg_map (abfd), p = phdrs, j = 0; |
252b5132 | 5374 | m != NULL; |
0920dee7 | 5375 | m = m->next, p++, j++) |
252b5132 | 5376 | { |
252b5132 | 5377 | asection **secpp; |
bf988460 AM |
5378 | bfd_vma off_adjust; |
5379 | bfd_boolean no_contents; | |
252b5132 RH |
5380 | |
5381 | /* If elf_segment_map is not from map_sections_to_segments, the | |
08a40648 | 5382 | sections may not be correctly ordered. NOTE: sorting should |
52e9b619 MS |
5383 | not be done to the PT_NOTE section of a corefile, which may |
5384 | contain several pseudo-sections artificially created by bfd. | |
5385 | Sorting these pseudo-sections breaks things badly. */ | |
47d9a591 AM |
5386 | if (m->count > 1 |
5387 | && !(elf_elfheader (abfd)->e_type == ET_CORE | |
52e9b619 | 5388 | && m->p_type == PT_NOTE)) |
252b5132 RH |
5389 | qsort (m->sections, (size_t) m->count, sizeof (asection *), |
5390 | elf_sort_sections); | |
5391 | ||
b301b248 AM |
5392 | /* An ELF segment (described by Elf_Internal_Phdr) may contain a |
5393 | number of sections with contents contributing to both p_filesz | |
5394 | and p_memsz, followed by a number of sections with no contents | |
5395 | that just contribute to p_memsz. In this loop, OFF tracks next | |
02bf8d82 | 5396 | available file offset for PT_LOAD and PT_NOTE segments. */ |
252b5132 | 5397 | p->p_type = m->p_type; |
28a7f3e7 | 5398 | p->p_flags = m->p_flags; |
252b5132 | 5399 | |
3f570048 | 5400 | if (m->count == 0) |
5d695627 | 5401 | p->p_vaddr = m->p_vaddr_offset; |
3f570048 | 5402 | else |
5d695627 | 5403 | p->p_vaddr = m->sections[0]->vma + m->p_vaddr_offset; |
3f570048 AM |
5404 | |
5405 | if (m->p_paddr_valid) | |
5406 | p->p_paddr = m->p_paddr; | |
5407 | else if (m->count == 0) | |
5408 | p->p_paddr = 0; | |
5409 | else | |
5d695627 | 5410 | p->p_paddr = m->sections[0]->lma + m->p_vaddr_offset; |
3f570048 AM |
5411 | |
5412 | if (p->p_type == PT_LOAD | |
5413 | && (abfd->flags & D_PAGED) != 0) | |
5414 | { | |
5415 | /* p_align in demand paged PT_LOAD segments effectively stores | |
5416 | the maximum page size. When copying an executable with | |
5417 | objcopy, we set m->p_align from the input file. Use this | |
5418 | value for maxpagesize rather than bed->maxpagesize, which | |
5419 | may be different. Note that we use maxpagesize for PT_TLS | |
5420 | segment alignment later in this function, so we are relying | |
5421 | on at least one PT_LOAD segment appearing before a PT_TLS | |
5422 | segment. */ | |
5423 | if (m->p_align_valid) | |
5424 | maxpagesize = m->p_align; | |
5425 | ||
5426 | p->p_align = maxpagesize; | |
a8c75b76 | 5427 | pt_load_count += 1; |
3f570048 | 5428 | } |
3271a814 NS |
5429 | else if (m->p_align_valid) |
5430 | p->p_align = m->p_align; | |
e970b90a DJ |
5431 | else if (m->count == 0) |
5432 | p->p_align = 1 << bed->s->log_file_align; | |
3f570048 AM |
5433 | else |
5434 | p->p_align = 0; | |
5435 | ||
bf988460 AM |
5436 | no_contents = FALSE; |
5437 | off_adjust = 0; | |
252b5132 | 5438 | if (p->p_type == PT_LOAD |
b301b248 | 5439 | && m->count > 0) |
252b5132 | 5440 | { |
b301b248 | 5441 | bfd_size_type align; |
a49e53ed | 5442 | unsigned int align_power = 0; |
b301b248 | 5443 | |
3271a814 NS |
5444 | if (m->p_align_valid) |
5445 | align = p->p_align; | |
5446 | else | |
252b5132 | 5447 | { |
3271a814 NS |
5448 | for (i = 0, secpp = m->sections; i < m->count; i++, secpp++) |
5449 | { | |
5450 | unsigned int secalign; | |
08a40648 | 5451 | |
3271a814 NS |
5452 | secalign = bfd_get_section_alignment (abfd, *secpp); |
5453 | if (secalign > align_power) | |
5454 | align_power = secalign; | |
5455 | } | |
5456 | align = (bfd_size_type) 1 << align_power; | |
5457 | if (align < maxpagesize) | |
5458 | align = maxpagesize; | |
b301b248 | 5459 | } |
252b5132 | 5460 | |
02bf8d82 AM |
5461 | for (i = 0; i < m->count; i++) |
5462 | if ((m->sections[i]->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0) | |
5463 | /* If we aren't making room for this section, then | |
5464 | it must be SHT_NOBITS regardless of what we've | |
5465 | set via struct bfd_elf_special_section. */ | |
5466 | elf_section_type (m->sections[i]) = SHT_NOBITS; | |
5467 | ||
bf988460 | 5468 | /* Find out whether this segment contains any loadable |
aea274d3 AM |
5469 | sections. */ |
5470 | no_contents = TRUE; | |
5471 | for (i = 0; i < m->count; i++) | |
5472 | if (elf_section_type (m->sections[i]) != SHT_NOBITS) | |
5473 | { | |
5474 | no_contents = FALSE; | |
5475 | break; | |
5476 | } | |
bf988460 | 5477 | |
85cfcbfb | 5478 | off_adjust = vma_page_aligned_bias (p->p_vaddr, off, align); |
a8c75b76 AM |
5479 | |
5480 | /* Broken hardware and/or kernel require that files do not | |
5481 | map the same page with different permissions on some hppa | |
5482 | processors. */ | |
5483 | if (pt_load_count > 1 | |
5484 | && bed->no_page_alias | |
5485 | && (off & (maxpagesize - 1)) != 0 | |
5486 | && (off & -maxpagesize) == ((off + off_adjust) & -maxpagesize)) | |
5487 | off_adjust += maxpagesize; | |
bf988460 AM |
5488 | off += off_adjust; |
5489 | if (no_contents) | |
5490 | { | |
5491 | /* We shouldn't need to align the segment on disk since | |
5492 | the segment doesn't need file space, but the gABI | |
5493 | arguably requires the alignment and glibc ld.so | |
5494 | checks it. So to comply with the alignment | |
5495 | requirement but not waste file space, we adjust | |
5496 | p_offset for just this segment. (OFF_ADJUST is | |
5497 | subtracted from OFF later.) This may put p_offset | |
5498 | past the end of file, but that shouldn't matter. */ | |
5499 | } | |
5500 | else | |
5501 | off_adjust = 0; | |
252b5132 | 5502 | } |
b1a6d0b1 NC |
5503 | /* Make sure the .dynamic section is the first section in the |
5504 | PT_DYNAMIC segment. */ | |
5505 | else if (p->p_type == PT_DYNAMIC | |
5506 | && m->count > 1 | |
5507 | && strcmp (m->sections[0]->name, ".dynamic") != 0) | |
5508 | { | |
5509 | _bfd_error_handler | |
871b3ab2 | 5510 | (_("%pB: The first section in the PT_DYNAMIC segment" |
63a5468a | 5511 | " is not the .dynamic section"), |
b301b248 | 5512 | abfd); |
b1a6d0b1 NC |
5513 | bfd_set_error (bfd_error_bad_value); |
5514 | return FALSE; | |
5515 | } | |
3f001e84 JK |
5516 | /* Set the note section type to SHT_NOTE. */ |
5517 | else if (p->p_type == PT_NOTE) | |
5518 | for (i = 0; i < m->count; i++) | |
5519 | elf_section_type (m->sections[i]) = SHT_NOTE; | |
252b5132 | 5520 | |
252b5132 RH |
5521 | p->p_offset = 0; |
5522 | p->p_filesz = 0; | |
5523 | p->p_memsz = 0; | |
5524 | ||
5525 | if (m->includes_filehdr) | |
5526 | { | |
bf988460 | 5527 | if (!m->p_flags_valid) |
252b5132 | 5528 | p->p_flags |= PF_R; |
252b5132 RH |
5529 | p->p_filesz = bed->s->sizeof_ehdr; |
5530 | p->p_memsz = bed->s->sizeof_ehdr; | |
5531 | if (m->count > 0) | |
5532 | { | |
9ab82472 L |
5533 | if (p->p_vaddr < (bfd_vma) off |
5534 | || (!m->p_paddr_valid | |
5535 | && p->p_paddr < (bfd_vma) off)) | |
252b5132 | 5536 | { |
4eca0228 | 5537 | _bfd_error_handler |
9793eb77 | 5538 | (_("%pB: not enough room for program headers," |
63a5468a | 5539 | " try linking with -N"), |
b301b248 | 5540 | abfd); |
252b5132 | 5541 | bfd_set_error (bfd_error_bad_value); |
b34976b6 | 5542 | return FALSE; |
252b5132 RH |
5543 | } |
5544 | ||
5545 | p->p_vaddr -= off; | |
bf988460 | 5546 | if (!m->p_paddr_valid) |
252b5132 RH |
5547 | p->p_paddr -= off; |
5548 | } | |
252b5132 RH |
5549 | } |
5550 | ||
5551 | if (m->includes_phdrs) | |
5552 | { | |
bf988460 | 5553 | if (!m->p_flags_valid) |
252b5132 RH |
5554 | p->p_flags |= PF_R; |
5555 | ||
f3520d2f | 5556 | if (!m->includes_filehdr) |
252b5132 RH |
5557 | { |
5558 | p->p_offset = bed->s->sizeof_ehdr; | |
5559 | ||
5560 | if (m->count > 0) | |
5561 | { | |
252b5132 | 5562 | p->p_vaddr -= off - p->p_offset; |
bf988460 | 5563 | if (!m->p_paddr_valid) |
252b5132 RH |
5564 | p->p_paddr -= off - p->p_offset; |
5565 | } | |
252b5132 RH |
5566 | } |
5567 | ||
5568 | p->p_filesz += alloc * bed->s->sizeof_phdr; | |
5569 | p->p_memsz += alloc * bed->s->sizeof_phdr; | |
2b0bc088 NC |
5570 | if (m->count) |
5571 | { | |
5572 | p->p_filesz += header_pad; | |
5573 | p->p_memsz += header_pad; | |
5574 | } | |
252b5132 RH |
5575 | } |
5576 | ||
5577 | if (p->p_type == PT_LOAD | |
5578 | || (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core)) | |
5579 | { | |
bf988460 | 5580 | if (!m->includes_filehdr && !m->includes_phdrs) |
02bf8d82 | 5581 | p->p_offset = off; |
252b5132 RH |
5582 | else |
5583 | { | |
5584 | file_ptr adjust; | |
5585 | ||
5586 | adjust = off - (p->p_offset + p->p_filesz); | |
bf988460 AM |
5587 | if (!no_contents) |
5588 | p->p_filesz += adjust; | |
252b5132 RH |
5589 | p->p_memsz += adjust; |
5590 | } | |
5591 | } | |
5592 | ||
1ea63fd2 AM |
5593 | /* Set up p_filesz, p_memsz, p_align and p_flags from the section |
5594 | maps. Set filepos for sections in PT_LOAD segments, and in | |
5595 | core files, for sections in PT_NOTE segments. | |
5596 | assign_file_positions_for_non_load_sections will set filepos | |
5597 | for other sections and update p_filesz for other segments. */ | |
252b5132 RH |
5598 | for (i = 0, secpp = m->sections; i < m->count; i++, secpp++) |
5599 | { | |
5600 | asection *sec; | |
252b5132 | 5601 | bfd_size_type align; |
627b32bc | 5602 | Elf_Internal_Shdr *this_hdr; |
252b5132 RH |
5603 | |
5604 | sec = *secpp; | |
02bf8d82 | 5605 | this_hdr = &elf_section_data (sec)->this_hdr; |
3f570048 | 5606 | align = (bfd_size_type) 1 << bfd_get_section_alignment (abfd, sec); |
252b5132 | 5607 | |
88967714 AM |
5608 | if ((p->p_type == PT_LOAD |
5609 | || p->p_type == PT_TLS) | |
5610 | && (this_hdr->sh_type != SHT_NOBITS | |
5611 | || ((this_hdr->sh_flags & SHF_ALLOC) != 0 | |
5612 | && ((this_hdr->sh_flags & SHF_TLS) == 0 | |
5613 | || p->p_type == PT_TLS)))) | |
252b5132 | 5614 | { |
b5599592 AM |
5615 | bfd_vma p_start = p->p_paddr; |
5616 | bfd_vma p_end = p_start + p->p_memsz; | |
5617 | bfd_vma s_start = sec->lma; | |
5618 | bfd_vma adjust = s_start - p_end; | |
252b5132 | 5619 | |
a2d1e028 L |
5620 | if (adjust != 0 |
5621 | && (s_start < p_end | |
5622 | || p_end < p_start)) | |
252b5132 | 5623 | { |
4eca0228 | 5624 | _bfd_error_handler |
695344c0 | 5625 | /* xgettext:c-format */ |
2dcf00ce AM |
5626 | (_("%pB: section %pA lma %#" PRIx64 " adjusted to %#" PRIx64), |
5627 | abfd, sec, (uint64_t) s_start, (uint64_t) p_end); | |
88967714 | 5628 | adjust = 0; |
b5599592 | 5629 | sec->lma = p_end; |
1cfb7d1e | 5630 | } |
3ac9b6c9 | 5631 | p->p_memsz += adjust; |
1cfb7d1e | 5632 | |
88967714 AM |
5633 | if (this_hdr->sh_type != SHT_NOBITS) |
5634 | { | |
32812159 AM |
5635 | if (p->p_filesz + adjust < p->p_memsz) |
5636 | { | |
5637 | /* We have a PROGBITS section following NOBITS ones. | |
07d6d2b8 | 5638 | Allocate file space for the NOBITS section(s) and |
32812159 AM |
5639 | zero it. */ |
5640 | adjust = p->p_memsz - p->p_filesz; | |
5641 | if (!write_zeros (abfd, off, adjust)) | |
5642 | return FALSE; | |
5643 | } | |
88967714 AM |
5644 | off += adjust; |
5645 | p->p_filesz += adjust; | |
252b5132 | 5646 | } |
252b5132 RH |
5647 | } |
5648 | ||
5649 | if (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core) | |
5650 | { | |
b301b248 AM |
5651 | /* The section at i == 0 is the one that actually contains |
5652 | everything. */ | |
4a938328 MS |
5653 | if (i == 0) |
5654 | { | |
627b32bc | 5655 | this_hdr->sh_offset = sec->filepos = off; |
6a3cd2b4 AM |
5656 | off += this_hdr->sh_size; |
5657 | p->p_filesz = this_hdr->sh_size; | |
b301b248 AM |
5658 | p->p_memsz = 0; |
5659 | p->p_align = 1; | |
252b5132 | 5660 | } |
4a938328 | 5661 | else |
252b5132 | 5662 | { |
b301b248 | 5663 | /* The rest are fake sections that shouldn't be written. */ |
252b5132 | 5664 | sec->filepos = 0; |
eea6121a | 5665 | sec->size = 0; |
b301b248 AM |
5666 | sec->flags = 0; |
5667 | continue; | |
252b5132 | 5668 | } |
252b5132 RH |
5669 | } |
5670 | else | |
5671 | { | |
1e951488 | 5672 | if (p->p_type == PT_LOAD) |
b301b248 | 5673 | { |
1e951488 AM |
5674 | this_hdr->sh_offset = sec->filepos = off; |
5675 | if (this_hdr->sh_type != SHT_NOBITS) | |
5676 | off += this_hdr->sh_size; | |
5677 | } | |
5678 | else if (this_hdr->sh_type == SHT_NOBITS | |
5679 | && (this_hdr->sh_flags & SHF_TLS) != 0 | |
5680 | && this_hdr->sh_offset == 0) | |
5681 | { | |
5682 | /* This is a .tbss section that didn't get a PT_LOAD. | |
5683 | (See _bfd_elf_map_sections_to_segments "Create a | |
5684 | final PT_LOAD".) Set sh_offset to the value it | |
5685 | would have if we had created a zero p_filesz and | |
5686 | p_memsz PT_LOAD header for the section. This | |
5687 | also makes the PT_TLS header have the same | |
5688 | p_offset value. */ | |
5689 | bfd_vma adjust = vma_page_aligned_bias (this_hdr->sh_addr, | |
5690 | off, align); | |
5691 | this_hdr->sh_offset = sec->filepos = off + adjust; | |
b301b248 | 5692 | } |
252b5132 | 5693 | |
02bf8d82 | 5694 | if (this_hdr->sh_type != SHT_NOBITS) |
b301b248 | 5695 | { |
6a3cd2b4 | 5696 | p->p_filesz += this_hdr->sh_size; |
02bf8d82 AM |
5697 | /* A load section without SHF_ALLOC is something like |
5698 | a note section in a PT_NOTE segment. These take | |
5699 | file space but are not loaded into memory. */ | |
5700 | if ((this_hdr->sh_flags & SHF_ALLOC) != 0) | |
6a3cd2b4 | 5701 | p->p_memsz += this_hdr->sh_size; |
b301b248 | 5702 | } |
6a3cd2b4 | 5703 | else if ((this_hdr->sh_flags & SHF_ALLOC) != 0) |
13ae64f3 | 5704 | { |
6a3cd2b4 AM |
5705 | if (p->p_type == PT_TLS) |
5706 | p->p_memsz += this_hdr->sh_size; | |
5707 | ||
5708 | /* .tbss is special. It doesn't contribute to p_memsz of | |
5709 | normal segments. */ | |
5710 | else if ((this_hdr->sh_flags & SHF_TLS) == 0) | |
5711 | p->p_memsz += this_hdr->sh_size; | |
13ae64f3 JJ |
5712 | } |
5713 | ||
b10a8ae0 L |
5714 | if (align > p->p_align |
5715 | && !m->p_align_valid | |
5716 | && (p->p_type != PT_LOAD | |
5717 | || (abfd->flags & D_PAGED) == 0)) | |
252b5132 RH |
5718 | p->p_align = align; |
5719 | } | |
5720 | ||
bf988460 | 5721 | if (!m->p_flags_valid) |
252b5132 RH |
5722 | { |
5723 | p->p_flags |= PF_R; | |
02bf8d82 | 5724 | if ((this_hdr->sh_flags & SHF_EXECINSTR) != 0) |
252b5132 | 5725 | p->p_flags |= PF_X; |
02bf8d82 | 5726 | if ((this_hdr->sh_flags & SHF_WRITE) != 0) |
252b5132 RH |
5727 | p->p_flags |= PF_W; |
5728 | } | |
5729 | } | |
43a8475c | 5730 | |
bf988460 | 5731 | off -= off_adjust; |
0920dee7 | 5732 | |
7c928300 AM |
5733 | /* Check that all sections are in a PT_LOAD segment. |
5734 | Don't check funky gdb generated core files. */ | |
5735 | if (p->p_type == PT_LOAD && bfd_get_format (abfd) != bfd_core) | |
9a83a553 AM |
5736 | { |
5737 | bfd_boolean check_vma = TRUE; | |
5738 | ||
5739 | for (i = 1; i < m->count; i++) | |
5740 | if (m->sections[i]->vma == m->sections[i - 1]->vma | |
5741 | && ELF_SECTION_SIZE (&(elf_section_data (m->sections[i]) | |
5742 | ->this_hdr), p) != 0 | |
5743 | && ELF_SECTION_SIZE (&(elf_section_data (m->sections[i - 1]) | |
5744 | ->this_hdr), p) != 0) | |
0920dee7 | 5745 | { |
9a83a553 AM |
5746 | /* Looks like we have overlays packed into the segment. */ |
5747 | check_vma = FALSE; | |
5748 | break; | |
0920dee7 | 5749 | } |
9a83a553 AM |
5750 | |
5751 | for (i = 0; i < m->count; i++) | |
5752 | { | |
5753 | Elf_Internal_Shdr *this_hdr; | |
5754 | asection *sec; | |
5755 | ||
5756 | sec = m->sections[i]; | |
5757 | this_hdr = &(elf_section_data(sec)->this_hdr); | |
86b2281f AM |
5758 | if (!ELF_SECTION_IN_SEGMENT_1 (this_hdr, p, check_vma, 0) |
5759 | && !ELF_TBSS_SPECIAL (this_hdr, p)) | |
9a83a553 | 5760 | { |
4eca0228 | 5761 | _bfd_error_handler |
695344c0 | 5762 | /* xgettext:c-format */ |
871b3ab2 | 5763 | (_("%pB: section `%pA' can't be allocated in segment %d"), |
9a83a553 AM |
5764 | abfd, sec, j); |
5765 | print_segment_map (m); | |
5766 | } | |
5767 | } | |
5768 | } | |
252b5132 RH |
5769 | } |
5770 | ||
12bd6957 | 5771 | elf_next_file_pos (abfd) = off; |
f3520d2f AM |
5772 | return TRUE; |
5773 | } | |
5774 | ||
5775 | /* Assign file positions for the other sections. */ | |
5776 | ||
5777 | static bfd_boolean | |
5778 | assign_file_positions_for_non_load_sections (bfd *abfd, | |
5779 | struct bfd_link_info *link_info) | |
5780 | { | |
5781 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
5782 | Elf_Internal_Shdr **i_shdrpp; | |
e06efbf1 | 5783 | Elf_Internal_Shdr **hdrpp, **end_hdrpp; |
f3520d2f AM |
5784 | Elf_Internal_Phdr *phdrs; |
5785 | Elf_Internal_Phdr *p; | |
5786 | struct elf_segment_map *m; | |
62655c7b | 5787 | struct elf_segment_map *hdrs_segment; |
f3520d2f AM |
5788 | bfd_vma filehdr_vaddr, filehdr_paddr; |
5789 | bfd_vma phdrs_vaddr, phdrs_paddr; | |
5790 | file_ptr off; | |
f3520d2f AM |
5791 | unsigned int count; |
5792 | ||
5c182d5f | 5793 | i_shdrpp = elf_elfsections (abfd); |
e06efbf1 | 5794 | end_hdrpp = i_shdrpp + elf_numsections (abfd); |
12bd6957 | 5795 | off = elf_next_file_pos (abfd); |
e06efbf1 | 5796 | for (hdrpp = i_shdrpp + 1; hdrpp < end_hdrpp; hdrpp++) |
5c182d5f | 5797 | { |
5c182d5f AM |
5798 | Elf_Internal_Shdr *hdr; |
5799 | ||
5800 | hdr = *hdrpp; | |
5801 | if (hdr->bfd_section != NULL | |
252e386e AM |
5802 | && (hdr->bfd_section->filepos != 0 |
5803 | || (hdr->sh_type == SHT_NOBITS | |
5804 | && hdr->contents == NULL))) | |
627b32bc | 5805 | BFD_ASSERT (hdr->sh_offset == hdr->bfd_section->filepos); |
5c182d5f AM |
5806 | else if ((hdr->sh_flags & SHF_ALLOC) != 0) |
5807 | { | |
e8d2ba53 | 5808 | if (hdr->sh_size != 0) |
4eca0228 | 5809 | _bfd_error_handler |
695344c0 | 5810 | /* xgettext:c-format */ |
871b3ab2 | 5811 | (_("%pB: warning: allocated section `%s' not in segment"), |
e8d2ba53 AM |
5812 | abfd, |
5813 | (hdr->bfd_section == NULL | |
5814 | ? "*unknown*" | |
5815 | : hdr->bfd_section->name)); | |
3ba71138 L |
5816 | /* We don't need to page align empty sections. */ |
5817 | if ((abfd->flags & D_PAGED) != 0 && hdr->sh_size != 0) | |
5c182d5f AM |
5818 | off += vma_page_aligned_bias (hdr->sh_addr, off, |
5819 | bed->maxpagesize); | |
5820 | else | |
5821 | off += vma_page_aligned_bias (hdr->sh_addr, off, | |
5822 | hdr->sh_addralign); | |
5823 | off = _bfd_elf_assign_file_position_for_section (hdr, off, | |
5824 | FALSE); | |
5825 | } | |
5826 | else if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA) | |
5827 | && hdr->bfd_section == NULL) | |
0ce398f1 L |
5828 | || (hdr->bfd_section != NULL |
5829 | && (hdr->bfd_section->flags & SEC_ELF_COMPRESS)) | |
5830 | /* Compress DWARF debug sections. */ | |
12bd6957 | 5831 | || hdr == i_shdrpp[elf_onesymtab (abfd)] |
6a40cf0c NC |
5832 | || (elf_symtab_shndx_list (abfd) != NULL |
5833 | && hdr == i_shdrpp[elf_symtab_shndx_list (abfd)->ndx]) | |
3e19fb8f L |
5834 | || hdr == i_shdrpp[elf_strtab_sec (abfd)] |
5835 | || hdr == i_shdrpp[elf_shstrtab_sec (abfd)]) | |
5c182d5f AM |
5836 | hdr->sh_offset = -1; |
5837 | else | |
5838 | off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE); | |
5c182d5f AM |
5839 | } |
5840 | ||
252b5132 RH |
5841 | /* Now that we have set the section file positions, we can set up |
5842 | the file positions for the non PT_LOAD segments. */ | |
f3520d2f AM |
5843 | count = 0; |
5844 | filehdr_vaddr = 0; | |
5845 | filehdr_paddr = 0; | |
5846 | phdrs_vaddr = bed->maxpagesize + bed->s->sizeof_ehdr; | |
5847 | phdrs_paddr = 0; | |
62655c7b | 5848 | hdrs_segment = NULL; |
f3520d2f | 5849 | phdrs = elf_tdata (abfd)->phdr; |
12bd6957 | 5850 | for (m = elf_seg_map (abfd), p = phdrs; m != NULL; m = m->next, p++) |
f3520d2f AM |
5851 | { |
5852 | ++count; | |
5853 | if (p->p_type != PT_LOAD) | |
5854 | continue; | |
5855 | ||
5856 | if (m->includes_filehdr) | |
5857 | { | |
5858 | filehdr_vaddr = p->p_vaddr; | |
5859 | filehdr_paddr = p->p_paddr; | |
5860 | } | |
5861 | if (m->includes_phdrs) | |
5862 | { | |
5863 | phdrs_vaddr = p->p_vaddr; | |
5864 | phdrs_paddr = p->p_paddr; | |
5865 | if (m->includes_filehdr) | |
5866 | { | |
62655c7b | 5867 | hdrs_segment = m; |
f3520d2f AM |
5868 | phdrs_vaddr += bed->s->sizeof_ehdr; |
5869 | phdrs_paddr += bed->s->sizeof_ehdr; | |
5870 | } | |
5871 | } | |
5872 | } | |
5873 | ||
62655c7b RM |
5874 | if (hdrs_segment != NULL && link_info != NULL) |
5875 | { | |
5876 | /* There is a segment that contains both the file headers and the | |
5877 | program headers, so provide a symbol __ehdr_start pointing there. | |
5878 | A program can use this to examine itself robustly. */ | |
5879 | ||
5880 | struct elf_link_hash_entry *hash | |
5881 | = elf_link_hash_lookup (elf_hash_table (link_info), "__ehdr_start", | |
5882 | FALSE, FALSE, TRUE); | |
5883 | /* If the symbol was referenced and not defined, define it. */ | |
5884 | if (hash != NULL | |
5885 | && (hash->root.type == bfd_link_hash_new | |
5886 | || hash->root.type == bfd_link_hash_undefined | |
5887 | || hash->root.type == bfd_link_hash_undefweak | |
5888 | || hash->root.type == bfd_link_hash_common)) | |
5889 | { | |
5890 | asection *s = NULL; | |
5891 | if (hdrs_segment->count != 0) | |
5892 | /* The segment contains sections, so use the first one. */ | |
5893 | s = hdrs_segment->sections[0]; | |
5894 | else | |
5895 | /* Use the first (i.e. lowest-addressed) section in any segment. */ | |
12bd6957 | 5896 | for (m = elf_seg_map (abfd); m != NULL; m = m->next) |
62655c7b RM |
5897 | if (m->count != 0) |
5898 | { | |
5899 | s = m->sections[0]; | |
5900 | break; | |
5901 | } | |
5902 | ||
5903 | if (s != NULL) | |
5904 | { | |
5905 | hash->root.u.def.value = filehdr_vaddr - s->vma; | |
5906 | hash->root.u.def.section = s; | |
5907 | } | |
5908 | else | |
5909 | { | |
5910 | hash->root.u.def.value = filehdr_vaddr; | |
5911 | hash->root.u.def.section = bfd_abs_section_ptr; | |
5912 | } | |
5913 | ||
5914 | hash->root.type = bfd_link_hash_defined; | |
5915 | hash->def_regular = 1; | |
5916 | hash->non_elf = 0; | |
5917 | } | |
5918 | } | |
5919 | ||
12bd6957 | 5920 | for (m = elf_seg_map (abfd), p = phdrs; m != NULL; m = m->next, p++) |
252b5132 | 5921 | { |
129af99f | 5922 | if (p->p_type == PT_GNU_RELRO) |
252b5132 | 5923 | { |
f2731e0c | 5924 | bfd_vma start, end; |
01f7e10c | 5925 | bfd_boolean ok; |
1ea63fd2 | 5926 | |
129af99f | 5927 | if (link_info != NULL) |
8c37241b | 5928 | { |
129af99f | 5929 | /* During linking the range of the RELRO segment is passed |
f2731e0c AM |
5930 | in link_info. Note that there may be padding between |
5931 | relro_start and the first RELRO section. */ | |
5932 | start = link_info->relro_start; | |
5933 | end = link_info->relro_end; | |
5934 | } | |
5935 | else if (m->count != 0) | |
5936 | { | |
5937 | if (!m->p_size_valid) | |
5938 | abort (); | |
5939 | start = m->sections[0]->vma; | |
5940 | end = start + m->p_size; | |
5941 | } | |
5942 | else | |
5943 | { | |
5944 | start = 0; | |
5945 | end = 0; | |
5946 | } | |
5947 | ||
01f7e10c | 5948 | ok = FALSE; |
f2731e0c AM |
5949 | if (start < end) |
5950 | { | |
5951 | struct elf_segment_map *lm; | |
5952 | const Elf_Internal_Phdr *lp; | |
5953 | unsigned int i; | |
5954 | ||
5955 | /* Find a LOAD segment containing a section in the RELRO | |
5956 | segment. */ | |
12bd6957 | 5957 | for (lm = elf_seg_map (abfd), lp = phdrs; |
3146fac4 AM |
5958 | lm != NULL; |
5959 | lm = lm->next, lp++) | |
8c37241b JJ |
5960 | { |
5961 | if (lp->p_type == PT_LOAD | |
3146fac4 | 5962 | && lm->count != 0 |
dbc88fc1 AM |
5963 | && (lm->sections[lm->count - 1]->vma |
5964 | + (!IS_TBSS (lm->sections[lm->count - 1]) | |
5965 | ? lm->sections[lm->count - 1]->size | |
5966 | : 0)) > start | |
f2731e0c | 5967 | && lm->sections[0]->vma < end) |
8c37241b JJ |
5968 | break; |
5969 | } | |
f2731e0c | 5970 | |
01f7e10c | 5971 | if (lm != NULL) |
129af99f | 5972 | { |
01f7e10c AM |
5973 | /* Find the section starting the RELRO segment. */ |
5974 | for (i = 0; i < lm->count; i++) | |
5975 | { | |
5976 | asection *s = lm->sections[i]; | |
5977 | if (s->vma >= start | |
5978 | && s->vma < end | |
5979 | && s->size != 0) | |
5980 | break; | |
5981 | } | |
5982 | ||
5983 | if (i < lm->count) | |
5984 | { | |
5985 | p->p_vaddr = lm->sections[i]->vma; | |
5986 | p->p_paddr = lm->sections[i]->lma; | |
5987 | p->p_offset = lm->sections[i]->filepos; | |
5988 | p->p_memsz = end - p->p_vaddr; | |
5989 | p->p_filesz = p->p_memsz; | |
5990 | ||
5991 | /* The RELRO segment typically ends a few bytes | |
5992 | into .got.plt but other layouts are possible. | |
5993 | In cases where the end does not match any | |
5994 | loaded section (for instance is in file | |
5995 | padding), trim p_filesz back to correspond to | |
5996 | the end of loaded section contents. */ | |
5997 | if (p->p_filesz > lp->p_vaddr + lp->p_filesz - p->p_vaddr) | |
5998 | p->p_filesz = lp->p_vaddr + lp->p_filesz - p->p_vaddr; | |
5999 | ||
6000 | /* Preserve the alignment and flags if they are | |
6001 | valid. The gold linker generates RW/4 for | |
6002 | the PT_GNU_RELRO section. It is better for | |
6003 | objcopy/strip to honor these attributes | |
6004 | otherwise gdb will choke when using separate | |
6005 | debug files. */ | |
6006 | if (!m->p_align_valid) | |
6007 | p->p_align = 1; | |
6008 | if (!m->p_flags_valid) | |
6009 | p->p_flags = PF_R; | |
6010 | ok = TRUE; | |
6011 | } | |
129af99f | 6012 | } |
b84a33b5 | 6013 | } |
01f7e10c AM |
6014 | if (link_info != NULL) |
6015 | BFD_ASSERT (ok); | |
6016 | if (!ok) | |
6017 | memset (p, 0, sizeof *p); | |
129af99f | 6018 | } |
04c3a755 NS |
6019 | else if (p->p_type == PT_GNU_STACK) |
6020 | { | |
6021 | if (m->p_size_valid) | |
6022 | p->p_memsz = m->p_size; | |
6023 | } | |
129af99f AS |
6024 | else if (m->count != 0) |
6025 | { | |
e06efbf1 | 6026 | unsigned int i; |
1a9ccd70 | 6027 | |
129af99f AS |
6028 | if (p->p_type != PT_LOAD |
6029 | && (p->p_type != PT_NOTE | |
6030 | || bfd_get_format (abfd) != bfd_core)) | |
6031 | { | |
1a9ccd70 NC |
6032 | /* A user specified segment layout may include a PHDR |
6033 | segment that overlaps with a LOAD segment... */ | |
6034 | if (p->p_type == PT_PHDR) | |
6035 | { | |
6036 | m->count = 0; | |
6037 | continue; | |
6038 | } | |
6039 | ||
c86934ce NC |
6040 | if (m->includes_filehdr || m->includes_phdrs) |
6041 | { | |
b1fa9dd6 | 6042 | /* PR 17512: file: 2195325e. */ |
4eca0228 | 6043 | _bfd_error_handler |
871b3ab2 | 6044 | (_("%pB: error: non-load segment %d includes file header " |
76cfced5 AM |
6045 | "and/or program header"), |
6046 | abfd, (int) (p - phdrs)); | |
c86934ce NC |
6047 | return FALSE; |
6048 | } | |
129af99f | 6049 | |
86b2281f | 6050 | p->p_filesz = 0; |
129af99f | 6051 | p->p_offset = m->sections[0]->filepos; |
86b2281f AM |
6052 | for (i = m->count; i-- != 0;) |
6053 | { | |
6054 | asection *sect = m->sections[i]; | |
6055 | Elf_Internal_Shdr *hdr = &elf_section_data (sect)->this_hdr; | |
6056 | if (hdr->sh_type != SHT_NOBITS) | |
6057 | { | |
6058 | p->p_filesz = (sect->filepos - m->sections[0]->filepos | |
6059 | + hdr->sh_size); | |
6060 | break; | |
6061 | } | |
6062 | } | |
129af99f AS |
6063 | } |
6064 | } | |
6065 | else if (m->includes_filehdr) | |
6066 | { | |
6067 | p->p_vaddr = filehdr_vaddr; | |
6068 | if (! m->p_paddr_valid) | |
6069 | p->p_paddr = filehdr_paddr; | |
6070 | } | |
6071 | else if (m->includes_phdrs) | |
6072 | { | |
6073 | p->p_vaddr = phdrs_vaddr; | |
6074 | if (! m->p_paddr_valid) | |
6075 | p->p_paddr = phdrs_paddr; | |
252b5132 RH |
6076 | } |
6077 | } | |
6078 | ||
12bd6957 | 6079 | elf_next_file_pos (abfd) = off; |
252b5132 | 6080 | |
b34976b6 | 6081 | return TRUE; |
252b5132 RH |
6082 | } |
6083 | ||
6a40cf0c NC |
6084 | static elf_section_list * |
6085 | find_section_in_list (unsigned int i, elf_section_list * list) | |
6086 | { | |
6087 | for (;list != NULL; list = list->next) | |
6088 | if (list->ndx == i) | |
6089 | break; | |
6090 | return list; | |
6091 | } | |
6092 | ||
252b5132 RH |
6093 | /* Work out the file positions of all the sections. This is called by |
6094 | _bfd_elf_compute_section_file_positions. All the section sizes and | |
6095 | VMAs must be known before this is called. | |
6096 | ||
e0638f70 AM |
6097 | Reloc sections come in two flavours: Those processed specially as |
6098 | "side-channel" data attached to a section to which they apply, and | |
6099 | those that bfd doesn't process as relocations. The latter sort are | |
6100 | stored in a normal bfd section by bfd_section_from_shdr. We don't | |
6101 | consider the former sort here, unless they form part of the loadable | |
6102 | image. Reloc sections not assigned here will be handled later by | |
6103 | assign_file_positions_for_relocs. | |
252b5132 RH |
6104 | |
6105 | We also don't set the positions of the .symtab and .strtab here. */ | |
6106 | ||
b34976b6 | 6107 | static bfd_boolean |
c84fca4d AO |
6108 | assign_file_positions_except_relocs (bfd *abfd, |
6109 | struct bfd_link_info *link_info) | |
252b5132 | 6110 | { |
5c182d5f AM |
6111 | struct elf_obj_tdata *tdata = elf_tdata (abfd); |
6112 | Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd); | |
9c5bfbb7 | 6113 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
252b5132 RH |
6114 | |
6115 | if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0 | |
6116 | && bfd_get_format (abfd) != bfd_core) | |
6117 | { | |
5c182d5f AM |
6118 | Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd); |
6119 | unsigned int num_sec = elf_numsections (abfd); | |
252b5132 RH |
6120 | Elf_Internal_Shdr **hdrpp; |
6121 | unsigned int i; | |
a485e98e | 6122 | file_ptr off; |
252b5132 RH |
6123 | |
6124 | /* Start after the ELF header. */ | |
6125 | off = i_ehdrp->e_ehsize; | |
6126 | ||
6127 | /* We are not creating an executable, which means that we are | |
6128 | not creating a program header, and that the actual order of | |
6129 | the sections in the file is unimportant. */ | |
9ad5cbcf | 6130 | for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++) |
252b5132 RH |
6131 | { |
6132 | Elf_Internal_Shdr *hdr; | |
6133 | ||
6134 | hdr = *hdrpp; | |
e0638f70 AM |
6135 | if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA) |
6136 | && hdr->bfd_section == NULL) | |
0ce398f1 L |
6137 | || (hdr->bfd_section != NULL |
6138 | && (hdr->bfd_section->flags & SEC_ELF_COMPRESS)) | |
6139 | /* Compress DWARF debug sections. */ | |
12bd6957 | 6140 | || i == elf_onesymtab (abfd) |
6a40cf0c NC |
6141 | || (elf_symtab_shndx_list (abfd) != NULL |
6142 | && hdr == i_shdrpp[elf_symtab_shndx_list (abfd)->ndx]) | |
3e19fb8f L |
6143 | || i == elf_strtab_sec (abfd) |
6144 | || i == elf_shstrtab_sec (abfd)) | |
252b5132 RH |
6145 | { |
6146 | hdr->sh_offset = -1; | |
252b5132 | 6147 | } |
9ad5cbcf | 6148 | else |
b34976b6 | 6149 | off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE); |
252b5132 | 6150 | } |
a485e98e AM |
6151 | |
6152 | elf_next_file_pos (abfd) = off; | |
252b5132 RH |
6153 | } |
6154 | else | |
6155 | { | |
f3520d2f AM |
6156 | unsigned int alloc; |
6157 | ||
252b5132 | 6158 | /* Assign file positions for the loaded sections based on the |
08a40648 | 6159 | assignment of sections to segments. */ |
f3520d2f AM |
6160 | if (!assign_file_positions_for_load_sections (abfd, link_info)) |
6161 | return FALSE; | |
6162 | ||
6163 | /* And for non-load sections. */ | |
6164 | if (!assign_file_positions_for_non_load_sections (abfd, link_info)) | |
6165 | return FALSE; | |
6166 | ||
e36284ab AM |
6167 | if (bed->elf_backend_modify_program_headers != NULL) |
6168 | { | |
6169 | if (!(*bed->elf_backend_modify_program_headers) (abfd, link_info)) | |
6170 | return FALSE; | |
6171 | } | |
6172 | ||
58e7ebac | 6173 | /* Set e_type in ELF header to ET_EXEC for -pie -Ttext-segment=. */ |
0e1862bb | 6174 | if (link_info != NULL && bfd_link_pie (link_info)) |
58e7ebac L |
6175 | { |
6176 | unsigned int num_segments = elf_elfheader (abfd)->e_phnum; | |
6177 | Elf_Internal_Phdr *segment = elf_tdata (abfd)->phdr; | |
6178 | Elf_Internal_Phdr *end_segment = &segment[num_segments]; | |
6179 | ||
6180 | /* Find the lowest p_vaddr in PT_LOAD segments. */ | |
6181 | bfd_vma p_vaddr = (bfd_vma) -1; | |
6182 | for (; segment < end_segment; segment++) | |
6183 | if (segment->p_type == PT_LOAD && p_vaddr > segment->p_vaddr) | |
6184 | p_vaddr = segment->p_vaddr; | |
6185 | ||
6186 | /* Set e_type to ET_EXEC if the lowest p_vaddr in PT_LOAD | |
6187 | segments is non-zero. */ | |
6188 | if (p_vaddr) | |
6189 | i_ehdrp->e_type = ET_EXEC; | |
6190 | } | |
6191 | ||
f3520d2f | 6192 | /* Write out the program headers. */ |
6838f2be | 6193 | alloc = elf_elfheader (abfd)->e_phnum; |
1a9ccd70 NC |
6194 | if (alloc == 0) |
6195 | return TRUE; | |
6196 | ||
1a9ccd70 NC |
6197 | /* PR ld/20815 - Check that the program header segment, if present, will |
6198 | be loaded into memory. FIXME: The check below is not sufficient as | |
6199 | really all PT_LOAD segments should be checked before issuing an error | |
6200 | message. Plus the PHDR segment does not have to be the first segment | |
6201 | in the program header table. But this version of the check should | |
cd584857 NC |
6202 | catch all real world use cases. |
6203 | ||
6204 | FIXME: We used to have code here to sort the PT_LOAD segments into | |
6205 | ascending order, as per the ELF spec. But this breaks some programs, | |
6206 | including the Linux kernel. But really either the spec should be | |
07d6d2b8 | 6207 | changed or the programs updated. */ |
1a9ccd70 | 6208 | if (alloc > 1 |
cd584857 | 6209 | && tdata->phdr[0].p_type == PT_PHDR |
d00dd7dc AM |
6210 | && (bed->elf_backend_allow_non_load_phdr == NULL |
6211 | || !bed->elf_backend_allow_non_load_phdr (abfd, tdata->phdr, | |
6212 | alloc)) | |
cd584857 NC |
6213 | && tdata->phdr[1].p_type == PT_LOAD |
6214 | && (tdata->phdr[1].p_vaddr > tdata->phdr[0].p_vaddr | |
6838f2be AM |
6215 | || (tdata->phdr[1].p_vaddr + tdata->phdr[1].p_memsz |
6216 | < tdata->phdr[0].p_vaddr + tdata->phdr[0].p_memsz))) | |
1a9ccd70 NC |
6217 | { |
6218 | /* The fix for this error is usually to edit the linker script being | |
6219 | used and set up the program headers manually. Either that or | |
6220 | leave room for the headers at the start of the SECTIONS. */ | |
6838f2be AM |
6221 | _bfd_error_handler (_("%pB: error: PHDR segment not covered" |
6222 | " by LOAD segment"), | |
1a9ccd70 | 6223 | abfd); |
1a9ccd70 NC |
6224 | return FALSE; |
6225 | } | |
6226 | ||
f3520d2f | 6227 | if (bfd_seek (abfd, (bfd_signed_vma) bed->s->sizeof_ehdr, SEEK_SET) != 0 |
cd584857 NC |
6228 | || bed->s->write_out_phdrs (abfd, tdata->phdr, alloc) != 0) |
6229 | return FALSE; | |
252b5132 RH |
6230 | } |
6231 | ||
b34976b6 | 6232 | return TRUE; |
252b5132 RH |
6233 | } |
6234 | ||
b34976b6 | 6235 | static bfd_boolean |
217aa764 | 6236 | prep_headers (bfd *abfd) |
252b5132 | 6237 | { |
3d540e93 | 6238 | Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form. */ |
2b0f7ef9 | 6239 | struct elf_strtab_hash *shstrtab; |
9c5bfbb7 | 6240 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
252b5132 RH |
6241 | |
6242 | i_ehdrp = elf_elfheader (abfd); | |
252b5132 | 6243 | |
2b0f7ef9 | 6244 | shstrtab = _bfd_elf_strtab_init (); |
252b5132 | 6245 | if (shstrtab == NULL) |
b34976b6 | 6246 | return FALSE; |
252b5132 RH |
6247 | |
6248 | elf_shstrtab (abfd) = shstrtab; | |
6249 | ||
6250 | i_ehdrp->e_ident[EI_MAG0] = ELFMAG0; | |
6251 | i_ehdrp->e_ident[EI_MAG1] = ELFMAG1; | |
6252 | i_ehdrp->e_ident[EI_MAG2] = ELFMAG2; | |
6253 | i_ehdrp->e_ident[EI_MAG3] = ELFMAG3; | |
6254 | ||
6255 | i_ehdrp->e_ident[EI_CLASS] = bed->s->elfclass; | |
6256 | i_ehdrp->e_ident[EI_DATA] = | |
6257 | bfd_big_endian (abfd) ? ELFDATA2MSB : ELFDATA2LSB; | |
6258 | i_ehdrp->e_ident[EI_VERSION] = bed->s->ev_current; | |
6259 | ||
252b5132 RH |
6260 | if ((abfd->flags & DYNAMIC) != 0) |
6261 | i_ehdrp->e_type = ET_DYN; | |
6262 | else if ((abfd->flags & EXEC_P) != 0) | |
6263 | i_ehdrp->e_type = ET_EXEC; | |
6264 | else if (bfd_get_format (abfd) == bfd_core) | |
6265 | i_ehdrp->e_type = ET_CORE; | |
6266 | else | |
6267 | i_ehdrp->e_type = ET_REL; | |
6268 | ||
6269 | switch (bfd_get_arch (abfd)) | |
6270 | { | |
6271 | case bfd_arch_unknown: | |
6272 | i_ehdrp->e_machine = EM_NONE; | |
6273 | break; | |
aa4f99bb AO |
6274 | |
6275 | /* There used to be a long list of cases here, each one setting | |
6276 | e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE | |
6277 | in the corresponding bfd definition. To avoid duplication, | |
6278 | the switch was removed. Machines that need special handling | |
6279 | can generally do it in elf_backend_final_write_processing(), | |
6280 | unless they need the information earlier than the final write. | |
6281 | Such need can generally be supplied by replacing the tests for | |
6282 | e_machine with the conditions used to determine it. */ | |
252b5132 | 6283 | default: |
9c5bfbb7 AM |
6284 | i_ehdrp->e_machine = bed->elf_machine_code; |
6285 | } | |
aa4f99bb | 6286 | |
252b5132 RH |
6287 | i_ehdrp->e_version = bed->s->ev_current; |
6288 | i_ehdrp->e_ehsize = bed->s->sizeof_ehdr; | |
6289 | ||
c044fabd | 6290 | /* No program header, for now. */ |
252b5132 RH |
6291 | i_ehdrp->e_phoff = 0; |
6292 | i_ehdrp->e_phentsize = 0; | |
6293 | i_ehdrp->e_phnum = 0; | |
6294 | ||
c044fabd | 6295 | /* Each bfd section is section header entry. */ |
252b5132 RH |
6296 | i_ehdrp->e_entry = bfd_get_start_address (abfd); |
6297 | i_ehdrp->e_shentsize = bed->s->sizeof_shdr; | |
6298 | ||
c044fabd | 6299 | /* If we're building an executable, we'll need a program header table. */ |
252b5132 | 6300 | if (abfd->flags & EXEC_P) |
0e71e495 BE |
6301 | /* It all happens later. */ |
6302 | ; | |
252b5132 RH |
6303 | else |
6304 | { | |
6305 | i_ehdrp->e_phentsize = 0; | |
252b5132 RH |
6306 | i_ehdrp->e_phoff = 0; |
6307 | } | |
6308 | ||
6309 | elf_tdata (abfd)->symtab_hdr.sh_name = | |
b34976b6 | 6310 | (unsigned int) _bfd_elf_strtab_add (shstrtab, ".symtab", FALSE); |
252b5132 | 6311 | elf_tdata (abfd)->strtab_hdr.sh_name = |
b34976b6 | 6312 | (unsigned int) _bfd_elf_strtab_add (shstrtab, ".strtab", FALSE); |
252b5132 | 6313 | elf_tdata (abfd)->shstrtab_hdr.sh_name = |
b34976b6 | 6314 | (unsigned int) _bfd_elf_strtab_add (shstrtab, ".shstrtab", FALSE); |
252b5132 | 6315 | if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1 |
17ca87fc | 6316 | || elf_tdata (abfd)->strtab_hdr.sh_name == (unsigned int) -1 |
252b5132 | 6317 | || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1) |
b34976b6 | 6318 | return FALSE; |
252b5132 | 6319 | |
b34976b6 | 6320 | return TRUE; |
252b5132 RH |
6321 | } |
6322 | ||
6323 | /* Assign file positions for all the reloc sections which are not part | |
a485e98e | 6324 | of the loadable file image, and the file position of section headers. */ |
252b5132 | 6325 | |
0ce398f1 L |
6326 | static bfd_boolean |
6327 | _bfd_elf_assign_file_positions_for_non_load (bfd *abfd) | |
252b5132 RH |
6328 | { |
6329 | file_ptr off; | |
e06efbf1 | 6330 | Elf_Internal_Shdr **shdrpp, **end_shdrpp; |
3e19fb8f | 6331 | Elf_Internal_Shdr *shdrp; |
a485e98e AM |
6332 | Elf_Internal_Ehdr *i_ehdrp; |
6333 | const struct elf_backend_data *bed; | |
252b5132 | 6334 | |
12bd6957 | 6335 | off = elf_next_file_pos (abfd); |
252b5132 | 6336 | |
e06efbf1 L |
6337 | shdrpp = elf_elfsections (abfd); |
6338 | end_shdrpp = shdrpp + elf_numsections (abfd); | |
6339 | for (shdrpp++; shdrpp < end_shdrpp; shdrpp++) | |
252b5132 | 6340 | { |
252b5132 | 6341 | shdrp = *shdrpp; |
0ce398f1 L |
6342 | if (shdrp->sh_offset == -1) |
6343 | { | |
3e19fb8f | 6344 | asection *sec = shdrp->bfd_section; |
0ce398f1 L |
6345 | bfd_boolean is_rel = (shdrp->sh_type == SHT_REL |
6346 | || shdrp->sh_type == SHT_RELA); | |
6347 | if (is_rel | |
3e19fb8f | 6348 | || (sec != NULL && (sec->flags & SEC_ELF_COMPRESS))) |
0ce398f1 L |
6349 | { |
6350 | if (!is_rel) | |
6351 | { | |
3e19fb8f L |
6352 | const char *name = sec->name; |
6353 | struct bfd_elf_section_data *d; | |
6354 | ||
0ce398f1 | 6355 | /* Compress DWARF debug sections. */ |
3e19fb8f | 6356 | if (!bfd_compress_section (abfd, sec, |
0ce398f1 L |
6357 | shdrp->contents)) |
6358 | return FALSE; | |
3e19fb8f L |
6359 | |
6360 | if (sec->compress_status == COMPRESS_SECTION_DONE | |
6361 | && (abfd->flags & BFD_COMPRESS_GABI) == 0) | |
6362 | { | |
6363 | /* If section is compressed with zlib-gnu, convert | |
6364 | section name from .debug_* to .zdebug_*. */ | |
6365 | char *new_name | |
6366 | = convert_debug_to_zdebug (abfd, name); | |
6367 | if (new_name == NULL) | |
6368 | return FALSE; | |
6369 | name = new_name; | |
6370 | } | |
dd905818 | 6371 | /* Add section name to section name section. */ |
3e19fb8f L |
6372 | if (shdrp->sh_name != (unsigned int) -1) |
6373 | abort (); | |
6374 | shdrp->sh_name | |
6375 | = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), | |
6376 | name, FALSE); | |
6377 | d = elf_section_data (sec); | |
6378 | ||
dd905818 | 6379 | /* Add reloc section name to section name section. */ |
3e19fb8f L |
6380 | if (d->rel.hdr |
6381 | && !_bfd_elf_set_reloc_sh_name (abfd, | |
6382 | d->rel.hdr, | |
6383 | name, FALSE)) | |
6384 | return FALSE; | |
6385 | if (d->rela.hdr | |
6386 | && !_bfd_elf_set_reloc_sh_name (abfd, | |
6387 | d->rela.hdr, | |
91cb26da | 6388 | name, TRUE)) |
3e19fb8f L |
6389 | return FALSE; |
6390 | ||
0ce398f1 | 6391 | /* Update section size and contents. */ |
3e19fb8f L |
6392 | shdrp->sh_size = sec->size; |
6393 | shdrp->contents = sec->contents; | |
0ce398f1 L |
6394 | shdrp->bfd_section->contents = NULL; |
6395 | } | |
6396 | off = _bfd_elf_assign_file_position_for_section (shdrp, | |
6397 | off, | |
6398 | TRUE); | |
6399 | } | |
6400 | } | |
252b5132 RH |
6401 | } |
6402 | ||
3e19fb8f L |
6403 | /* Place section name section after DWARF debug sections have been |
6404 | compressed. */ | |
6405 | _bfd_elf_strtab_finalize (elf_shstrtab (abfd)); | |
6406 | shdrp = &elf_tdata (abfd)->shstrtab_hdr; | |
6407 | shdrp->sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd)); | |
6408 | off = _bfd_elf_assign_file_position_for_section (shdrp, off, TRUE); | |
6409 | ||
6410 | /* Place the section headers. */ | |
a485e98e AM |
6411 | i_ehdrp = elf_elfheader (abfd); |
6412 | bed = get_elf_backend_data (abfd); | |
6413 | off = align_file_position (off, 1 << bed->s->log_file_align); | |
6414 | i_ehdrp->e_shoff = off; | |
6415 | off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize; | |
12bd6957 | 6416 | elf_next_file_pos (abfd) = off; |
0ce398f1 L |
6417 | |
6418 | return TRUE; | |
252b5132 RH |
6419 | } |
6420 | ||
b34976b6 | 6421 | bfd_boolean |
217aa764 | 6422 | _bfd_elf_write_object_contents (bfd *abfd) |
252b5132 | 6423 | { |
9c5bfbb7 | 6424 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
252b5132 | 6425 | Elf_Internal_Shdr **i_shdrp; |
b34976b6 | 6426 | bfd_boolean failed; |
9ad5cbcf | 6427 | unsigned int count, num_sec; |
30e8ee25 | 6428 | struct elf_obj_tdata *t; |
252b5132 RH |
6429 | |
6430 | if (! abfd->output_has_begun | |
217aa764 | 6431 | && ! _bfd_elf_compute_section_file_positions (abfd, NULL)) |
b34976b6 | 6432 | return FALSE; |
db727370 JL |
6433 | /* Do not rewrite ELF data when the BFD has been opened for update. |
6434 | abfd->output_has_begun was set to TRUE on opening, so creation of new | |
6435 | sections, and modification of existing section sizes was restricted. | |
6436 | This means the ELF header, program headers and section headers can't have | |
6437 | changed. | |
6438 | If the contents of any sections has been modified, then those changes have | |
6439 | already been written to the BFD. */ | |
6440 | else if (abfd->direction == both_direction) | |
6441 | { | |
6442 | BFD_ASSERT (abfd->output_has_begun); | |
6443 | return TRUE; | |
6444 | } | |
252b5132 RH |
6445 | |
6446 | i_shdrp = elf_elfsections (abfd); | |
252b5132 | 6447 | |
b34976b6 | 6448 | failed = FALSE; |
252b5132 RH |
6449 | bfd_map_over_sections (abfd, bed->s->write_relocs, &failed); |
6450 | if (failed) | |
b34976b6 | 6451 | return FALSE; |
252b5132 | 6452 | |
0ce398f1 L |
6453 | if (!_bfd_elf_assign_file_positions_for_non_load (abfd)) |
6454 | return FALSE; | |
252b5132 | 6455 | |
c044fabd | 6456 | /* After writing the headers, we need to write the sections too... */ |
9ad5cbcf AM |
6457 | num_sec = elf_numsections (abfd); |
6458 | for (count = 1; count < num_sec; count++) | |
252b5132 | 6459 | { |
3e19fb8f L |
6460 | i_shdrp[count]->sh_name |
6461 | = _bfd_elf_strtab_offset (elf_shstrtab (abfd), | |
6462 | i_shdrp[count]->sh_name); | |
252b5132 | 6463 | if (bed->elf_backend_section_processing) |
75506100 MR |
6464 | if (!(*bed->elf_backend_section_processing) (abfd, i_shdrp[count])) |
6465 | return FALSE; | |
252b5132 RH |
6466 | if (i_shdrp[count]->contents) |
6467 | { | |
dc810e39 AM |
6468 | bfd_size_type amt = i_shdrp[count]->sh_size; |
6469 | ||
252b5132 | 6470 | if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0 |
dc810e39 | 6471 | || bfd_bwrite (i_shdrp[count]->contents, amt, abfd) != amt) |
b34976b6 | 6472 | return FALSE; |
252b5132 RH |
6473 | } |
6474 | } | |
6475 | ||
6476 | /* Write out the section header names. */ | |
30e8ee25 | 6477 | t = elf_tdata (abfd); |
26ae6d5e | 6478 | if (elf_shstrtab (abfd) != NULL |
30e8ee25 | 6479 | && (bfd_seek (abfd, t->shstrtab_hdr.sh_offset, SEEK_SET) != 0 |
08a40648 | 6480 | || !_bfd_elf_strtab_emit (abfd, elf_shstrtab (abfd)))) |
b34976b6 | 6481 | return FALSE; |
252b5132 RH |
6482 | |
6483 | if (bed->elf_backend_final_write_processing) | |
12bd6957 | 6484 | (*bed->elf_backend_final_write_processing) (abfd, elf_linker (abfd)); |
252b5132 | 6485 | |
ff59fc36 RM |
6486 | if (!bed->s->write_shdrs_and_ehdr (abfd)) |
6487 | return FALSE; | |
6488 | ||
6489 | /* This is last since write_shdrs_and_ehdr can touch i_shdrp[0]. */ | |
c0355132 AM |
6490 | if (t->o->build_id.after_write_object_contents != NULL) |
6491 | return (*t->o->build_id.after_write_object_contents) (abfd); | |
ff59fc36 RM |
6492 | |
6493 | return TRUE; | |
252b5132 RH |
6494 | } |
6495 | ||
b34976b6 | 6496 | bfd_boolean |
217aa764 | 6497 | _bfd_elf_write_corefile_contents (bfd *abfd) |
252b5132 | 6498 | { |
c044fabd | 6499 | /* Hopefully this can be done just like an object file. */ |
252b5132 RH |
6500 | return _bfd_elf_write_object_contents (abfd); |
6501 | } | |
c044fabd KH |
6502 | |
6503 | /* Given a section, search the header to find them. */ | |
6504 | ||
cb33740c | 6505 | unsigned int |
198beae2 | 6506 | _bfd_elf_section_from_bfd_section (bfd *abfd, struct bfd_section *asect) |
252b5132 | 6507 | { |
9c5bfbb7 | 6508 | const struct elf_backend_data *bed; |
91d6fa6a | 6509 | unsigned int sec_index; |
252b5132 | 6510 | |
9ad5cbcf AM |
6511 | if (elf_section_data (asect) != NULL |
6512 | && elf_section_data (asect)->this_idx != 0) | |
6513 | return elf_section_data (asect)->this_idx; | |
6514 | ||
6515 | if (bfd_is_abs_section (asect)) | |
91d6fa6a | 6516 | sec_index = SHN_ABS; |
af746e92 | 6517 | else if (bfd_is_com_section (asect)) |
91d6fa6a | 6518 | sec_index = SHN_COMMON; |
af746e92 | 6519 | else if (bfd_is_und_section (asect)) |
91d6fa6a | 6520 | sec_index = SHN_UNDEF; |
af746e92 | 6521 | else |
91d6fa6a | 6522 | sec_index = SHN_BAD; |
252b5132 | 6523 | |
af746e92 | 6524 | bed = get_elf_backend_data (abfd); |
252b5132 RH |
6525 | if (bed->elf_backend_section_from_bfd_section) |
6526 | { | |
91d6fa6a | 6527 | int retval = sec_index; |
9ad5cbcf | 6528 | |
af746e92 AM |
6529 | if ((*bed->elf_backend_section_from_bfd_section) (abfd, asect, &retval)) |
6530 | return retval; | |
252b5132 RH |
6531 | } |
6532 | ||
91d6fa6a | 6533 | if (sec_index == SHN_BAD) |
af746e92 | 6534 | bfd_set_error (bfd_error_nonrepresentable_section); |
252b5132 | 6535 | |
91d6fa6a | 6536 | return sec_index; |
252b5132 RH |
6537 | } |
6538 | ||
6539 | /* Given a BFD symbol, return the index in the ELF symbol table, or -1 | |
6540 | on error. */ | |
6541 | ||
6542 | int | |
217aa764 | 6543 | _bfd_elf_symbol_from_bfd_symbol (bfd *abfd, asymbol **asym_ptr_ptr) |
252b5132 RH |
6544 | { |
6545 | asymbol *asym_ptr = *asym_ptr_ptr; | |
6546 | int idx; | |
6547 | flagword flags = asym_ptr->flags; | |
6548 | ||
6549 | /* When gas creates relocations against local labels, it creates its | |
6550 | own symbol for the section, but does put the symbol into the | |
6551 | symbol chain, so udata is 0. When the linker is generating | |
6552 | relocatable output, this section symbol may be for one of the | |
6553 | input sections rather than the output section. */ | |
6554 | if (asym_ptr->udata.i == 0 | |
6555 | && (flags & BSF_SECTION_SYM) | |
6556 | && asym_ptr->section) | |
6557 | { | |
5372391b | 6558 | asection *sec; |
252b5132 RH |
6559 | int indx; |
6560 | ||
5372391b AM |
6561 | sec = asym_ptr->section; |
6562 | if (sec->owner != abfd && sec->output_section != NULL) | |
6563 | sec = sec->output_section; | |
6564 | if (sec->owner == abfd | |
6565 | && (indx = sec->index) < elf_num_section_syms (abfd) | |
4e89ac30 | 6566 | && elf_section_syms (abfd)[indx] != NULL) |
252b5132 RH |
6567 | asym_ptr->udata.i = elf_section_syms (abfd)[indx]->udata.i; |
6568 | } | |
6569 | ||
6570 | idx = asym_ptr->udata.i; | |
6571 | ||
6572 | if (idx == 0) | |
6573 | { | |
6574 | /* This case can occur when using --strip-symbol on a symbol | |
08a40648 | 6575 | which is used in a relocation entry. */ |
4eca0228 | 6576 | _bfd_error_handler |
695344c0 | 6577 | /* xgettext:c-format */ |
871b3ab2 | 6578 | (_("%pB: symbol `%s' required but not present"), |
d003868e | 6579 | abfd, bfd_asymbol_name (asym_ptr)); |
252b5132 RH |
6580 | bfd_set_error (bfd_error_no_symbols); |
6581 | return -1; | |
6582 | } | |
6583 | ||
6584 | #if DEBUG & 4 | |
6585 | { | |
6586 | fprintf (stderr, | |
cd9af601 AM |
6587 | "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8x\n", |
6588 | (long) asym_ptr, asym_ptr->name, idx, flags); | |
252b5132 RH |
6589 | fflush (stderr); |
6590 | } | |
6591 | #endif | |
6592 | ||
6593 | return idx; | |
6594 | } | |
6595 | ||
84d1d650 | 6596 | /* Rewrite program header information. */ |
252b5132 | 6597 | |
b34976b6 | 6598 | static bfd_boolean |
84d1d650 | 6599 | rewrite_elf_program_header (bfd *ibfd, bfd *obfd) |
252b5132 | 6600 | { |
b34976b6 AM |
6601 | Elf_Internal_Ehdr *iehdr; |
6602 | struct elf_segment_map *map; | |
6603 | struct elf_segment_map *map_first; | |
6604 | struct elf_segment_map **pointer_to_map; | |
6605 | Elf_Internal_Phdr *segment; | |
6606 | asection *section; | |
6607 | unsigned int i; | |
6608 | unsigned int num_segments; | |
6609 | bfd_boolean phdr_included = FALSE; | |
5c44b38e | 6610 | bfd_boolean p_paddr_valid; |
b34976b6 AM |
6611 | bfd_vma maxpagesize; |
6612 | struct elf_segment_map *phdr_adjust_seg = NULL; | |
6613 | unsigned int phdr_adjust_num = 0; | |
9c5bfbb7 | 6614 | const struct elf_backend_data *bed; |
bc67d8a6 | 6615 | |
caf47ea6 | 6616 | bed = get_elf_backend_data (ibfd); |
252b5132 RH |
6617 | iehdr = elf_elfheader (ibfd); |
6618 | ||
bc67d8a6 | 6619 | map_first = NULL; |
c044fabd | 6620 | pointer_to_map = &map_first; |
252b5132 RH |
6621 | |
6622 | num_segments = elf_elfheader (ibfd)->e_phnum; | |
bc67d8a6 NC |
6623 | maxpagesize = get_elf_backend_data (obfd)->maxpagesize; |
6624 | ||
6625 | /* Returns the end address of the segment + 1. */ | |
aecc8f8a AM |
6626 | #define SEGMENT_END(segment, start) \ |
6627 | (start + (segment->p_memsz > segment->p_filesz \ | |
6628 | ? segment->p_memsz : segment->p_filesz)) | |
bc67d8a6 | 6629 | |
eecdbe52 JJ |
6630 | #define SECTION_SIZE(section, segment) \ |
6631 | (((section->flags & (SEC_HAS_CONTENTS | SEC_THREAD_LOCAL)) \ | |
6632 | != SEC_THREAD_LOCAL || segment->p_type == PT_TLS) \ | |
eea6121a | 6633 | ? section->size : 0) |
eecdbe52 | 6634 | |
b34976b6 | 6635 | /* Returns TRUE if the given section is contained within |
bc67d8a6 | 6636 | the given segment. VMA addresses are compared. */ |
aecc8f8a AM |
6637 | #define IS_CONTAINED_BY_VMA(section, segment) \ |
6638 | (section->vma >= segment->p_vaddr \ | |
eecdbe52 | 6639 | && (section->vma + SECTION_SIZE (section, segment) \ |
aecc8f8a | 6640 | <= (SEGMENT_END (segment, segment->p_vaddr)))) |
c044fabd | 6641 | |
b34976b6 | 6642 | /* Returns TRUE if the given section is contained within |
bc67d8a6 | 6643 | the given segment. LMA addresses are compared. */ |
aecc8f8a AM |
6644 | #define IS_CONTAINED_BY_LMA(section, segment, base) \ |
6645 | (section->lma >= base \ | |
eecdbe52 | 6646 | && (section->lma + SECTION_SIZE (section, segment) \ |
aecc8f8a | 6647 | <= SEGMENT_END (segment, base))) |
252b5132 | 6648 | |
0efc80c8 L |
6649 | /* Handle PT_NOTE segment. */ |
6650 | #define IS_NOTE(p, s) \ | |
aecc8f8a | 6651 | (p->p_type == PT_NOTE \ |
0efc80c8 | 6652 | && elf_section_type (s) == SHT_NOTE \ |
aecc8f8a | 6653 | && (bfd_vma) s->filepos >= p->p_offset \ |
cb3ff1e5 | 6654 | && ((bfd_vma) s->filepos + s->size \ |
aecc8f8a | 6655 | <= p->p_offset + p->p_filesz)) |
252b5132 | 6656 | |
0efc80c8 L |
6657 | /* Special case: corefile "NOTE" section containing regs, prpsinfo |
6658 | etc. */ | |
6659 | #define IS_COREFILE_NOTE(p, s) \ | |
6660 | (IS_NOTE (p, s) \ | |
6661 | && bfd_get_format (ibfd) == bfd_core \ | |
6662 | && s->vma == 0 \ | |
6663 | && s->lma == 0) | |
6664 | ||
252b5132 RH |
6665 | /* The complicated case when p_vaddr is 0 is to handle the Solaris |
6666 | linker, which generates a PT_INTERP section with p_vaddr and | |
6667 | p_memsz set to 0. */ | |
aecc8f8a AM |
6668 | #define IS_SOLARIS_PT_INTERP(p, s) \ |
6669 | (p->p_vaddr == 0 \ | |
6670 | && p->p_paddr == 0 \ | |
6671 | && p->p_memsz == 0 \ | |
6672 | && p->p_filesz > 0 \ | |
6673 | && (s->flags & SEC_HAS_CONTENTS) != 0 \ | |
eea6121a | 6674 | && s->size > 0 \ |
aecc8f8a | 6675 | && (bfd_vma) s->filepos >= p->p_offset \ |
cb3ff1e5 | 6676 | && ((bfd_vma) s->filepos + s->size \ |
aecc8f8a | 6677 | <= p->p_offset + p->p_filesz)) |
5c440b1e | 6678 | |
bc67d8a6 NC |
6679 | /* Decide if the given section should be included in the given segment. |
6680 | A section will be included if: | |
f5ffc919 | 6681 | 1. It is within the address space of the segment -- we use the LMA |
08a40648 | 6682 | if that is set for the segment and the VMA otherwise, |
0efc80c8 | 6683 | 2. It is an allocated section or a NOTE section in a PT_NOTE |
d324f6d6 | 6684 | segment. |
bc67d8a6 | 6685 | 3. There is an output section associated with it, |
eecdbe52 | 6686 | 4. The section has not already been allocated to a previous segment. |
2b05f1b7 | 6687 | 5. PT_GNU_STACK segments do not include any sections. |
03394ac9 | 6688 | 6. PT_TLS segment includes only SHF_TLS sections. |
6f79b219 JJ |
6689 | 7. SHF_TLS sections are only in PT_TLS or PT_LOAD segments. |
6690 | 8. PT_DYNAMIC should not contain empty sections at the beginning | |
08a40648 | 6691 | (with the possible exception of .dynamic). */ |
9f17e2a6 | 6692 | #define IS_SECTION_IN_INPUT_SEGMENT(section, segment, bed) \ |
2b05f1b7 L |
6693 | ((((segment->p_paddr \ |
6694 | ? IS_CONTAINED_BY_LMA (section, segment, segment->p_paddr) \ | |
6695 | : IS_CONTAINED_BY_VMA (section, segment)) \ | |
6696 | && (section->flags & SEC_ALLOC) != 0) \ | |
0efc80c8 | 6697 | || IS_NOTE (segment, section)) \ |
2b05f1b7 L |
6698 | && segment->p_type != PT_GNU_STACK \ |
6699 | && (segment->p_type != PT_TLS \ | |
6700 | || (section->flags & SEC_THREAD_LOCAL)) \ | |
6701 | && (segment->p_type == PT_LOAD \ | |
6702 | || segment->p_type == PT_TLS \ | |
6703 | || (section->flags & SEC_THREAD_LOCAL) == 0) \ | |
6704 | && (segment->p_type != PT_DYNAMIC \ | |
6705 | || SECTION_SIZE (section, segment) > 0 \ | |
6706 | || (segment->p_paddr \ | |
6707 | ? segment->p_paddr != section->lma \ | |
6708 | : segment->p_vaddr != section->vma) \ | |
6709 | || (strcmp (bfd_get_section_name (ibfd, section), ".dynamic") \ | |
6710 | == 0)) \ | |
9933dc52 | 6711 | && (segment->p_type != PT_LOAD || !section->segment_mark)) |
bc67d8a6 | 6712 | |
9f17e2a6 L |
6713 | /* If the output section of a section in the input segment is NULL, |
6714 | it is removed from the corresponding output segment. */ | |
6715 | #define INCLUDE_SECTION_IN_SEGMENT(section, segment, bed) \ | |
6716 | (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed) \ | |
6717 | && section->output_section != NULL) | |
6718 | ||
b34976b6 | 6719 | /* Returns TRUE iff seg1 starts after the end of seg2. */ |
b5f852ea NC |
6720 | #define SEGMENT_AFTER_SEGMENT(seg1, seg2, field) \ |
6721 | (seg1->field >= SEGMENT_END (seg2, seg2->field)) | |
6722 | ||
6723 | /* Returns TRUE iff seg1 and seg2 overlap. Segments overlap iff both | |
6724 | their VMA address ranges and their LMA address ranges overlap. | |
6725 | It is possible to have overlapping VMA ranges without overlapping LMA | |
6726 | ranges. RedBoot images for example can have both .data and .bss mapped | |
6727 | to the same VMA range, but with the .data section mapped to a different | |
6728 | LMA. */ | |
aecc8f8a | 6729 | #define SEGMENT_OVERLAPS(seg1, seg2) \ |
b5f852ea | 6730 | ( !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_vaddr) \ |
08a40648 | 6731 | || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_vaddr)) \ |
b5f852ea | 6732 | && !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_paddr) \ |
08a40648 | 6733 | || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_paddr))) |
bc67d8a6 NC |
6734 | |
6735 | /* Initialise the segment mark field. */ | |
6736 | for (section = ibfd->sections; section != NULL; section = section->next) | |
b34976b6 | 6737 | section->segment_mark = FALSE; |
bc67d8a6 | 6738 | |
5c44b38e AM |
6739 | /* The Solaris linker creates program headers in which all the |
6740 | p_paddr fields are zero. When we try to objcopy or strip such a | |
6741 | file, we get confused. Check for this case, and if we find it | |
6742 | don't set the p_paddr_valid fields. */ | |
6743 | p_paddr_valid = FALSE; | |
6744 | for (i = 0, segment = elf_tdata (ibfd)->phdr; | |
6745 | i < num_segments; | |
6746 | i++, segment++) | |
6747 | if (segment->p_paddr != 0) | |
6748 | { | |
6749 | p_paddr_valid = TRUE; | |
6750 | break; | |
6751 | } | |
6752 | ||
252b5132 | 6753 | /* Scan through the segments specified in the program header |
bc67d8a6 | 6754 | of the input BFD. For this first scan we look for overlaps |
9ad5cbcf | 6755 | in the loadable segments. These can be created by weird |
aecc8f8a | 6756 | parameters to objcopy. Also, fix some solaris weirdness. */ |
bc67d8a6 NC |
6757 | for (i = 0, segment = elf_tdata (ibfd)->phdr; |
6758 | i < num_segments; | |
c044fabd | 6759 | i++, segment++) |
252b5132 | 6760 | { |
252b5132 | 6761 | unsigned int j; |
c044fabd | 6762 | Elf_Internal_Phdr *segment2; |
252b5132 | 6763 | |
aecc8f8a AM |
6764 | if (segment->p_type == PT_INTERP) |
6765 | for (section = ibfd->sections; section; section = section->next) | |
6766 | if (IS_SOLARIS_PT_INTERP (segment, section)) | |
6767 | { | |
6768 | /* Mininal change so that the normal section to segment | |
4cc11e76 | 6769 | assignment code will work. */ |
aecc8f8a AM |
6770 | segment->p_vaddr = section->vma; |
6771 | break; | |
6772 | } | |
6773 | ||
bc67d8a6 | 6774 | if (segment->p_type != PT_LOAD) |
b10a8ae0 L |
6775 | { |
6776 | /* Remove PT_GNU_RELRO segment. */ | |
6777 | if (segment->p_type == PT_GNU_RELRO) | |
6778 | segment->p_type = PT_NULL; | |
6779 | continue; | |
6780 | } | |
c044fabd | 6781 | |
bc67d8a6 | 6782 | /* Determine if this segment overlaps any previous segments. */ |
0067a569 | 6783 | for (j = 0, segment2 = elf_tdata (ibfd)->phdr; j < i; j++, segment2++) |
bc67d8a6 NC |
6784 | { |
6785 | bfd_signed_vma extra_length; | |
c044fabd | 6786 | |
bc67d8a6 | 6787 | if (segment2->p_type != PT_LOAD |
0067a569 | 6788 | || !SEGMENT_OVERLAPS (segment, segment2)) |
bc67d8a6 | 6789 | continue; |
c044fabd | 6790 | |
bc67d8a6 NC |
6791 | /* Merge the two segments together. */ |
6792 | if (segment2->p_vaddr < segment->p_vaddr) | |
6793 | { | |
c044fabd | 6794 | /* Extend SEGMENT2 to include SEGMENT and then delete |
08a40648 | 6795 | SEGMENT. */ |
0067a569 AM |
6796 | extra_length = (SEGMENT_END (segment, segment->p_vaddr) |
6797 | - SEGMENT_END (segment2, segment2->p_vaddr)); | |
c044fabd | 6798 | |
bc67d8a6 NC |
6799 | if (extra_length > 0) |
6800 | { | |
0067a569 | 6801 | segment2->p_memsz += extra_length; |
bc67d8a6 NC |
6802 | segment2->p_filesz += extra_length; |
6803 | } | |
c044fabd | 6804 | |
bc67d8a6 | 6805 | segment->p_type = PT_NULL; |
c044fabd | 6806 | |
bc67d8a6 NC |
6807 | /* Since we have deleted P we must restart the outer loop. */ |
6808 | i = 0; | |
6809 | segment = elf_tdata (ibfd)->phdr; | |
6810 | break; | |
6811 | } | |
6812 | else | |
6813 | { | |
c044fabd | 6814 | /* Extend SEGMENT to include SEGMENT2 and then delete |
08a40648 | 6815 | SEGMENT2. */ |
0067a569 AM |
6816 | extra_length = (SEGMENT_END (segment2, segment2->p_vaddr) |
6817 | - SEGMENT_END (segment, segment->p_vaddr)); | |
c044fabd | 6818 | |
bc67d8a6 NC |
6819 | if (extra_length > 0) |
6820 | { | |
0067a569 | 6821 | segment->p_memsz += extra_length; |
bc67d8a6 NC |
6822 | segment->p_filesz += extra_length; |
6823 | } | |
c044fabd | 6824 | |
bc67d8a6 NC |
6825 | segment2->p_type = PT_NULL; |
6826 | } | |
6827 | } | |
6828 | } | |
c044fabd | 6829 | |
bc67d8a6 NC |
6830 | /* The second scan attempts to assign sections to segments. */ |
6831 | for (i = 0, segment = elf_tdata (ibfd)->phdr; | |
6832 | i < num_segments; | |
0067a569 | 6833 | i++, segment++) |
bc67d8a6 | 6834 | { |
0067a569 AM |
6835 | unsigned int section_count; |
6836 | asection **sections; | |
6837 | asection *output_section; | |
6838 | unsigned int isec; | |
9933dc52 AM |
6839 | asection *matching_lma; |
6840 | asection *suggested_lma; | |
0067a569 | 6841 | unsigned int j; |
dc810e39 | 6842 | bfd_size_type amt; |
0067a569 | 6843 | asection *first_section; |
bc67d8a6 NC |
6844 | |
6845 | if (segment->p_type == PT_NULL) | |
6846 | continue; | |
c044fabd | 6847 | |
9f17e2a6 | 6848 | first_section = NULL; |
bc67d8a6 | 6849 | /* Compute how many sections might be placed into this segment. */ |
b5f852ea NC |
6850 | for (section = ibfd->sections, section_count = 0; |
6851 | section != NULL; | |
6852 | section = section->next) | |
9f17e2a6 L |
6853 | { |
6854 | /* Find the first section in the input segment, which may be | |
6855 | removed from the corresponding output segment. */ | |
6856 | if (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed)) | |
6857 | { | |
6858 | if (first_section == NULL) | |
6859 | first_section = section; | |
6860 | if (section->output_section != NULL) | |
6861 | ++section_count; | |
6862 | } | |
6863 | } | |
811072d8 | 6864 | |
b5f852ea NC |
6865 | /* Allocate a segment map big enough to contain |
6866 | all of the sections we have selected. */ | |
00bee008 AM |
6867 | amt = sizeof (struct elf_segment_map) - sizeof (asection *); |
6868 | amt += (bfd_size_type) section_count * sizeof (asection *); | |
a50b1753 | 6869 | map = (struct elf_segment_map *) bfd_zalloc (obfd, amt); |
bc67d8a6 | 6870 | if (map == NULL) |
b34976b6 | 6871 | return FALSE; |
252b5132 RH |
6872 | |
6873 | /* Initialise the fields of the segment map. Default to | |
6874 | using the physical address of the segment in the input BFD. */ | |
0067a569 AM |
6875 | map->next = NULL; |
6876 | map->p_type = segment->p_type; | |
6877 | map->p_flags = segment->p_flags; | |
bc67d8a6 | 6878 | map->p_flags_valid = 1; |
55d55ac7 | 6879 | |
9f17e2a6 L |
6880 | /* If the first section in the input segment is removed, there is |
6881 | no need to preserve segment physical address in the corresponding | |
6882 | output segment. */ | |
945c025a | 6883 | if (!first_section || first_section->output_section != NULL) |
9f17e2a6 L |
6884 | { |
6885 | map->p_paddr = segment->p_paddr; | |
5c44b38e | 6886 | map->p_paddr_valid = p_paddr_valid; |
9f17e2a6 | 6887 | } |
252b5132 RH |
6888 | |
6889 | /* Determine if this segment contains the ELF file header | |
6890 | and if it contains the program headers themselves. */ | |
bc67d8a6 NC |
6891 | map->includes_filehdr = (segment->p_offset == 0 |
6892 | && segment->p_filesz >= iehdr->e_ehsize); | |
bc67d8a6 | 6893 | map->includes_phdrs = 0; |
252b5132 | 6894 | |
0067a569 | 6895 | if (!phdr_included || segment->p_type != PT_LOAD) |
252b5132 | 6896 | { |
bc67d8a6 NC |
6897 | map->includes_phdrs = |
6898 | (segment->p_offset <= (bfd_vma) iehdr->e_phoff | |
6899 | && (segment->p_offset + segment->p_filesz | |
252b5132 RH |
6900 | >= ((bfd_vma) iehdr->e_phoff |
6901 | + iehdr->e_phnum * iehdr->e_phentsize))); | |
c044fabd | 6902 | |
bc67d8a6 | 6903 | if (segment->p_type == PT_LOAD && map->includes_phdrs) |
b34976b6 | 6904 | phdr_included = TRUE; |
252b5132 RH |
6905 | } |
6906 | ||
bc67d8a6 | 6907 | if (section_count == 0) |
252b5132 RH |
6908 | { |
6909 | /* Special segments, such as the PT_PHDR segment, may contain | |
6910 | no sections, but ordinary, loadable segments should contain | |
1ed89aa9 | 6911 | something. They are allowed by the ELF spec however, so only |
07d6d2b8 | 6912 | a warning is produced. |
f98450c6 NC |
6913 | There is however the valid use case of embedded systems which |
6914 | have segments with p_filesz of 0 and a p_memsz > 0 to initialize | |
6915 | flash memory with zeros. No warning is shown for that case. */ | |
6916 | if (segment->p_type == PT_LOAD | |
6917 | && (segment->p_filesz > 0 || segment->p_memsz == 0)) | |
6918 | /* xgettext:c-format */ | |
9793eb77 AM |
6919 | _bfd_error_handler |
6920 | (_("%pB: warning: empty loadable segment detected" | |
6921 | " at vaddr=%#" PRIx64 ", is this intentional?"), | |
6922 | ibfd, (uint64_t) segment->p_vaddr); | |
252b5132 | 6923 | |
5d695627 | 6924 | map->p_vaddr_offset = segment->p_vaddr; |
bc67d8a6 | 6925 | map->count = 0; |
c044fabd KH |
6926 | *pointer_to_map = map; |
6927 | pointer_to_map = &map->next; | |
252b5132 RH |
6928 | |
6929 | continue; | |
6930 | } | |
6931 | ||
6932 | /* Now scan the sections in the input BFD again and attempt | |
6933 | to add their corresponding output sections to the segment map. | |
6934 | The problem here is how to handle an output section which has | |
6935 | been moved (ie had its LMA changed). There are four possibilities: | |
6936 | ||
6937 | 1. None of the sections have been moved. | |
6938 | In this case we can continue to use the segment LMA from the | |
6939 | input BFD. | |
6940 | ||
6941 | 2. All of the sections have been moved by the same amount. | |
6942 | In this case we can change the segment's LMA to match the LMA | |
6943 | of the first section. | |
6944 | ||
6945 | 3. Some of the sections have been moved, others have not. | |
6946 | In this case those sections which have not been moved can be | |
6947 | placed in the current segment which will have to have its size, | |
6948 | and possibly its LMA changed, and a new segment or segments will | |
6949 | have to be created to contain the other sections. | |
6950 | ||
b5f852ea | 6951 | 4. The sections have been moved, but not by the same amount. |
252b5132 RH |
6952 | In this case we can change the segment's LMA to match the LMA |
6953 | of the first section and we will have to create a new segment | |
6954 | or segments to contain the other sections. | |
6955 | ||
6956 | In order to save time, we allocate an array to hold the section | |
6957 | pointers that we are interested in. As these sections get assigned | |
6958 | to a segment, they are removed from this array. */ | |
6959 | ||
a50b1753 | 6960 | sections = (asection **) bfd_malloc2 (section_count, sizeof (asection *)); |
252b5132 | 6961 | if (sections == NULL) |
b34976b6 | 6962 | return FALSE; |
252b5132 RH |
6963 | |
6964 | /* Step One: Scan for segment vs section LMA conflicts. | |
6965 | Also add the sections to the section array allocated above. | |
6966 | Also add the sections to the current segment. In the common | |
6967 | case, where the sections have not been moved, this means that | |
6968 | we have completely filled the segment, and there is nothing | |
6969 | more to do. */ | |
252b5132 | 6970 | isec = 0; |
9933dc52 AM |
6971 | matching_lma = NULL; |
6972 | suggested_lma = NULL; | |
252b5132 | 6973 | |
461c4b2e | 6974 | for (section = first_section, j = 0; |
bc67d8a6 NC |
6975 | section != NULL; |
6976 | section = section->next) | |
252b5132 | 6977 | { |
caf47ea6 | 6978 | if (INCLUDE_SECTION_IN_SEGMENT (section, segment, bed)) |
c0f7859b | 6979 | { |
bc67d8a6 NC |
6980 | output_section = section->output_section; |
6981 | ||
0067a569 | 6982 | sections[j++] = section; |
252b5132 RH |
6983 | |
6984 | /* The Solaris native linker always sets p_paddr to 0. | |
6985 | We try to catch that case here, and set it to the | |
5e8d7549 NC |
6986 | correct value. Note - some backends require that |
6987 | p_paddr be left as zero. */ | |
5c44b38e | 6988 | if (!p_paddr_valid |
4455705d | 6989 | && segment->p_vaddr != 0 |
0067a569 | 6990 | && !bed->want_p_paddr_set_to_zero |
252b5132 | 6991 | && isec == 0 |
bc67d8a6 | 6992 | && output_section->lma != 0 |
9933dc52 AM |
6993 | && (align_power (segment->p_vaddr |
6994 | + (map->includes_filehdr | |
6995 | ? iehdr->e_ehsize : 0) | |
6996 | + (map->includes_phdrs | |
6997 | ? iehdr->e_phnum * iehdr->e_phentsize | |
6998 | : 0), | |
6999 | output_section->alignment_power) | |
7000 | == output_section->vma)) | |
bc67d8a6 | 7001 | map->p_paddr = segment->p_vaddr; |
252b5132 RH |
7002 | |
7003 | /* Match up the physical address of the segment with the | |
7004 | LMA address of the output section. */ | |
bc67d8a6 | 7005 | if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr) |
5e8d7549 | 7006 | || IS_COREFILE_NOTE (segment, section) |
0067a569 AM |
7007 | || (bed->want_p_paddr_set_to_zero |
7008 | && IS_CONTAINED_BY_VMA (output_section, segment))) | |
252b5132 | 7009 | { |
9933dc52 AM |
7010 | if (matching_lma == NULL |
7011 | || output_section->lma < matching_lma->lma) | |
7012 | matching_lma = output_section; | |
252b5132 RH |
7013 | |
7014 | /* We assume that if the section fits within the segment | |
bc67d8a6 | 7015 | then it does not overlap any other section within that |
252b5132 | 7016 | segment. */ |
0067a569 AM |
7017 | map->sections[isec++] = output_section; |
7018 | } | |
9933dc52 AM |
7019 | else if (suggested_lma == NULL) |
7020 | suggested_lma = output_section; | |
147d51c2 L |
7021 | |
7022 | if (j == section_count) | |
7023 | break; | |
252b5132 RH |
7024 | } |
7025 | } | |
7026 | ||
bc67d8a6 | 7027 | BFD_ASSERT (j == section_count); |
252b5132 RH |
7028 | |
7029 | /* Step Two: Adjust the physical address of the current segment, | |
7030 | if necessary. */ | |
bc67d8a6 | 7031 | if (isec == section_count) |
252b5132 RH |
7032 | { |
7033 | /* All of the sections fitted within the segment as currently | |
7034 | specified. This is the default case. Add the segment to | |
7035 | the list of built segments and carry on to process the next | |
7036 | program header in the input BFD. */ | |
bc67d8a6 | 7037 | map->count = section_count; |
c044fabd KH |
7038 | *pointer_to_map = map; |
7039 | pointer_to_map = &map->next; | |
08a40648 | 7040 | |
5c44b38e AM |
7041 | if (p_paddr_valid |
7042 | && !bed->want_p_paddr_set_to_zero | |
9933dc52 | 7043 | && matching_lma->lma != map->p_paddr |
5c44b38e AM |
7044 | && !map->includes_filehdr |
7045 | && !map->includes_phdrs) | |
3271a814 NS |
7046 | /* There is some padding before the first section in the |
7047 | segment. So, we must account for that in the output | |
7048 | segment's vma. */ | |
5d695627 | 7049 | map->p_vaddr_offset = map->p_paddr - matching_lma->lma; |
08a40648 | 7050 | |
252b5132 RH |
7051 | free (sections); |
7052 | continue; | |
7053 | } | |
252b5132 RH |
7054 | else |
7055 | { | |
9933dc52 AM |
7056 | /* Change the current segment's physical address to match |
7057 | the LMA of the first section that fitted, or if no | |
7058 | section fitted, the first section. */ | |
7059 | if (matching_lma == NULL) | |
7060 | matching_lma = suggested_lma; | |
7061 | ||
7062 | map->p_paddr = matching_lma->lma; | |
72730e0c | 7063 | |
bc67d8a6 NC |
7064 | /* Offset the segment physical address from the lma |
7065 | to allow for space taken up by elf headers. */ | |
9933dc52 | 7066 | if (map->includes_phdrs) |
010c8431 | 7067 | { |
9933dc52 AM |
7068 | map->p_paddr -= iehdr->e_phnum * iehdr->e_phentsize; |
7069 | ||
7070 | /* iehdr->e_phnum is just an estimate of the number | |
7071 | of program headers that we will need. Make a note | |
7072 | here of the number we used and the segment we chose | |
7073 | to hold these headers, so that we can adjust the | |
7074 | offset when we know the correct value. */ | |
7075 | phdr_adjust_num = iehdr->e_phnum; | |
7076 | phdr_adjust_seg = map; | |
010c8431 | 7077 | } |
252b5132 | 7078 | |
9933dc52 | 7079 | if (map->includes_filehdr) |
bc67d8a6 | 7080 | { |
9933dc52 AM |
7081 | bfd_vma align = (bfd_vma) 1 << matching_lma->alignment_power; |
7082 | map->p_paddr -= iehdr->e_ehsize; | |
7083 | /* We've subtracted off the size of headers from the | |
7084 | first section lma, but there may have been some | |
7085 | alignment padding before that section too. Try to | |
7086 | account for that by adjusting the segment lma down to | |
7087 | the same alignment. */ | |
7088 | if (segment->p_align != 0 && segment->p_align < align) | |
7089 | align = segment->p_align; | |
7090 | map->p_paddr &= -align; | |
bc67d8a6 | 7091 | } |
252b5132 RH |
7092 | } |
7093 | ||
7094 | /* Step Three: Loop over the sections again, this time assigning | |
caf47ea6 | 7095 | those that fit to the current segment and removing them from the |
252b5132 RH |
7096 | sections array; but making sure not to leave large gaps. Once all |
7097 | possible sections have been assigned to the current segment it is | |
7098 | added to the list of built segments and if sections still remain | |
7099 | to be assigned, a new segment is constructed before repeating | |
7100 | the loop. */ | |
7101 | isec = 0; | |
7102 | do | |
7103 | { | |
bc67d8a6 | 7104 | map->count = 0; |
9933dc52 | 7105 | suggested_lma = NULL; |
252b5132 RH |
7106 | |
7107 | /* Fill the current segment with sections that fit. */ | |
bc67d8a6 | 7108 | for (j = 0; j < section_count; j++) |
252b5132 | 7109 | { |
bc67d8a6 | 7110 | section = sections[j]; |
252b5132 | 7111 | |
bc67d8a6 | 7112 | if (section == NULL) |
252b5132 RH |
7113 | continue; |
7114 | ||
bc67d8a6 | 7115 | output_section = section->output_section; |
252b5132 | 7116 | |
bc67d8a6 | 7117 | BFD_ASSERT (output_section != NULL); |
c044fabd | 7118 | |
bc67d8a6 NC |
7119 | if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr) |
7120 | || IS_COREFILE_NOTE (segment, section)) | |
252b5132 | 7121 | { |
bc67d8a6 | 7122 | if (map->count == 0) |
252b5132 RH |
7123 | { |
7124 | /* If the first section in a segment does not start at | |
bc67d8a6 NC |
7125 | the beginning of the segment, then something is |
7126 | wrong. */ | |
9933dc52 AM |
7127 | if (align_power (map->p_paddr |
7128 | + (map->includes_filehdr | |
7129 | ? iehdr->e_ehsize : 0) | |
7130 | + (map->includes_phdrs | |
7131 | ? iehdr->e_phnum * iehdr->e_phentsize | |
7132 | : 0), | |
7133 | output_section->alignment_power) | |
7134 | != output_section->lma) | |
252b5132 RH |
7135 | abort (); |
7136 | } | |
7137 | else | |
7138 | { | |
0067a569 | 7139 | asection *prev_sec; |
252b5132 | 7140 | |
bc67d8a6 | 7141 | prev_sec = map->sections[map->count - 1]; |
252b5132 RH |
7142 | |
7143 | /* If the gap between the end of the previous section | |
bc67d8a6 NC |
7144 | and the start of this section is more than |
7145 | maxpagesize then we need to start a new segment. */ | |
eea6121a | 7146 | if ((BFD_ALIGN (prev_sec->lma + prev_sec->size, |
079e9a2f | 7147 | maxpagesize) |
caf47ea6 | 7148 | < BFD_ALIGN (output_section->lma, maxpagesize)) |
0067a569 | 7149 | || (prev_sec->lma + prev_sec->size |
079e9a2f | 7150 | > output_section->lma)) |
252b5132 | 7151 | { |
9933dc52 AM |
7152 | if (suggested_lma == NULL) |
7153 | suggested_lma = output_section; | |
252b5132 RH |
7154 | |
7155 | continue; | |
7156 | } | |
7157 | } | |
7158 | ||
bc67d8a6 | 7159 | map->sections[map->count++] = output_section; |
252b5132 RH |
7160 | ++isec; |
7161 | sections[j] = NULL; | |
9933dc52 AM |
7162 | if (segment->p_type == PT_LOAD) |
7163 | section->segment_mark = TRUE; | |
0067a569 | 7164 | } |
9933dc52 AM |
7165 | else if (suggested_lma == NULL) |
7166 | suggested_lma = output_section; | |
252b5132 RH |
7167 | } |
7168 | ||
bc67d8a6 | 7169 | BFD_ASSERT (map->count > 0); |
252b5132 RH |
7170 | |
7171 | /* Add the current segment to the list of built segments. */ | |
c044fabd KH |
7172 | *pointer_to_map = map; |
7173 | pointer_to_map = &map->next; | |
252b5132 | 7174 | |
bc67d8a6 | 7175 | if (isec < section_count) |
252b5132 RH |
7176 | { |
7177 | /* We still have not allocated all of the sections to | |
7178 | segments. Create a new segment here, initialise it | |
7179 | and carry on looping. */ | |
00bee008 AM |
7180 | amt = sizeof (struct elf_segment_map) - sizeof (asection *); |
7181 | amt += (bfd_size_type) section_count * sizeof (asection *); | |
5964fc3a | 7182 | map = (struct elf_segment_map *) bfd_zalloc (obfd, amt); |
bc67d8a6 | 7183 | if (map == NULL) |
5ed6aba4 NC |
7184 | { |
7185 | free (sections); | |
7186 | return FALSE; | |
7187 | } | |
252b5132 RH |
7188 | |
7189 | /* Initialise the fields of the segment map. Set the physical | |
7190 | physical address to the LMA of the first section that has | |
7191 | not yet been assigned. */ | |
0067a569 AM |
7192 | map->next = NULL; |
7193 | map->p_type = segment->p_type; | |
7194 | map->p_flags = segment->p_flags; | |
7195 | map->p_flags_valid = 1; | |
9933dc52 | 7196 | map->p_paddr = suggested_lma->lma; |
5c44b38e | 7197 | map->p_paddr_valid = p_paddr_valid; |
bc67d8a6 | 7198 | map->includes_filehdr = 0; |
0067a569 | 7199 | map->includes_phdrs = 0; |
252b5132 RH |
7200 | } |
7201 | } | |
bc67d8a6 | 7202 | while (isec < section_count); |
252b5132 RH |
7203 | |
7204 | free (sections); | |
7205 | } | |
7206 | ||
12bd6957 | 7207 | elf_seg_map (obfd) = map_first; |
bc67d8a6 NC |
7208 | |
7209 | /* If we had to estimate the number of program headers that were | |
9ad5cbcf | 7210 | going to be needed, then check our estimate now and adjust |
bc67d8a6 NC |
7211 | the offset if necessary. */ |
7212 | if (phdr_adjust_seg != NULL) | |
7213 | { | |
7214 | unsigned int count; | |
c044fabd | 7215 | |
bc67d8a6 | 7216 | for (count = 0, map = map_first; map != NULL; map = map->next) |
c044fabd | 7217 | count++; |
252b5132 | 7218 | |
bc67d8a6 NC |
7219 | if (count > phdr_adjust_num) |
7220 | phdr_adjust_seg->p_paddr | |
7221 | -= (count - phdr_adjust_num) * iehdr->e_phentsize; | |
9933dc52 AM |
7222 | |
7223 | for (map = map_first; map != NULL; map = map->next) | |
7224 | if (map->p_type == PT_PHDR) | |
7225 | { | |
7226 | bfd_vma adjust | |
7227 | = phdr_adjust_seg->includes_filehdr ? iehdr->e_ehsize : 0; | |
7228 | map->p_paddr = phdr_adjust_seg->p_paddr + adjust; | |
7229 | break; | |
7230 | } | |
bc67d8a6 | 7231 | } |
c044fabd | 7232 | |
bc67d8a6 | 7233 | #undef SEGMENT_END |
eecdbe52 | 7234 | #undef SECTION_SIZE |
bc67d8a6 NC |
7235 | #undef IS_CONTAINED_BY_VMA |
7236 | #undef IS_CONTAINED_BY_LMA | |
0efc80c8 | 7237 | #undef IS_NOTE |
252b5132 | 7238 | #undef IS_COREFILE_NOTE |
bc67d8a6 | 7239 | #undef IS_SOLARIS_PT_INTERP |
9f17e2a6 | 7240 | #undef IS_SECTION_IN_INPUT_SEGMENT |
bc67d8a6 NC |
7241 | #undef INCLUDE_SECTION_IN_SEGMENT |
7242 | #undef SEGMENT_AFTER_SEGMENT | |
7243 | #undef SEGMENT_OVERLAPS | |
b34976b6 | 7244 | return TRUE; |
252b5132 RH |
7245 | } |
7246 | ||
84d1d650 L |
7247 | /* Copy ELF program header information. */ |
7248 | ||
7249 | static bfd_boolean | |
7250 | copy_elf_program_header (bfd *ibfd, bfd *obfd) | |
7251 | { | |
7252 | Elf_Internal_Ehdr *iehdr; | |
7253 | struct elf_segment_map *map; | |
7254 | struct elf_segment_map *map_first; | |
7255 | struct elf_segment_map **pointer_to_map; | |
7256 | Elf_Internal_Phdr *segment; | |
7257 | unsigned int i; | |
7258 | unsigned int num_segments; | |
7259 | bfd_boolean phdr_included = FALSE; | |
88967714 | 7260 | bfd_boolean p_paddr_valid; |
84d1d650 L |
7261 | |
7262 | iehdr = elf_elfheader (ibfd); | |
7263 | ||
7264 | map_first = NULL; | |
7265 | pointer_to_map = &map_first; | |
7266 | ||
88967714 AM |
7267 | /* If all the segment p_paddr fields are zero, don't set |
7268 | map->p_paddr_valid. */ | |
7269 | p_paddr_valid = FALSE; | |
84d1d650 | 7270 | num_segments = elf_elfheader (ibfd)->e_phnum; |
88967714 AM |
7271 | for (i = 0, segment = elf_tdata (ibfd)->phdr; |
7272 | i < num_segments; | |
7273 | i++, segment++) | |
7274 | if (segment->p_paddr != 0) | |
7275 | { | |
7276 | p_paddr_valid = TRUE; | |
7277 | break; | |
7278 | } | |
7279 | ||
84d1d650 L |
7280 | for (i = 0, segment = elf_tdata (ibfd)->phdr; |
7281 | i < num_segments; | |
7282 | i++, segment++) | |
7283 | { | |
7284 | asection *section; | |
7285 | unsigned int section_count; | |
7286 | bfd_size_type amt; | |
7287 | Elf_Internal_Shdr *this_hdr; | |
53020534 | 7288 | asection *first_section = NULL; |
a76e6f2f | 7289 | asection *lowest_section; |
2542e49e | 7290 | bfd_boolean no_contents = TRUE; |
84d1d650 | 7291 | |
84d1d650 L |
7292 | /* Compute how many sections are in this segment. */ |
7293 | for (section = ibfd->sections, section_count = 0; | |
7294 | section != NULL; | |
7295 | section = section->next) | |
7296 | { | |
7297 | this_hdr = &(elf_section_data(section)->this_hdr); | |
f4638467 | 7298 | if (ELF_SECTION_IN_SEGMENT (this_hdr, segment)) |
3271a814 | 7299 | { |
a76e6f2f AM |
7300 | if (first_section == NULL) |
7301 | first_section = section; | |
2542e49e JL |
7302 | if (elf_section_type (section) != SHT_NOBITS) |
7303 | no_contents = FALSE; | |
3271a814 NS |
7304 | section_count++; |
7305 | } | |
84d1d650 L |
7306 | } |
7307 | ||
7308 | /* Allocate a segment map big enough to contain | |
7309 | all of the sections we have selected. */ | |
00bee008 AM |
7310 | amt = sizeof (struct elf_segment_map) - sizeof (asection *); |
7311 | amt += (bfd_size_type) section_count * sizeof (asection *); | |
a50b1753 | 7312 | map = (struct elf_segment_map *) bfd_zalloc (obfd, amt); |
84d1d650 L |
7313 | if (map == NULL) |
7314 | return FALSE; | |
7315 | ||
7316 | /* Initialize the fields of the output segment map with the | |
7317 | input segment. */ | |
7318 | map->next = NULL; | |
7319 | map->p_type = segment->p_type; | |
7320 | map->p_flags = segment->p_flags; | |
7321 | map->p_flags_valid = 1; | |
7322 | map->p_paddr = segment->p_paddr; | |
88967714 | 7323 | map->p_paddr_valid = p_paddr_valid; |
3f570048 AM |
7324 | map->p_align = segment->p_align; |
7325 | map->p_align_valid = 1; | |
3271a814 | 7326 | map->p_vaddr_offset = 0; |
84d1d650 | 7327 | |
04c3a755 NS |
7328 | if (map->p_type == PT_GNU_RELRO |
7329 | || map->p_type == PT_GNU_STACK) | |
b10a8ae0 L |
7330 | { |
7331 | /* The PT_GNU_RELRO segment may contain the first a few | |
7332 | bytes in the .got.plt section even if the whole .got.plt | |
7333 | section isn't in the PT_GNU_RELRO segment. We won't | |
04c3a755 NS |
7334 | change the size of the PT_GNU_RELRO segment. |
7335 | Similarly, PT_GNU_STACK size is significant on uclinux | |
7336 | systems. */ | |
9433b9b1 | 7337 | map->p_size = segment->p_memsz; |
b10a8ae0 L |
7338 | map->p_size_valid = 1; |
7339 | } | |
7340 | ||
84d1d650 L |
7341 | /* Determine if this segment contains the ELF file header |
7342 | and if it contains the program headers themselves. */ | |
7343 | map->includes_filehdr = (segment->p_offset == 0 | |
7344 | && segment->p_filesz >= iehdr->e_ehsize); | |
7345 | ||
7346 | map->includes_phdrs = 0; | |
7347 | if (! phdr_included || segment->p_type != PT_LOAD) | |
7348 | { | |
7349 | map->includes_phdrs = | |
7350 | (segment->p_offset <= (bfd_vma) iehdr->e_phoff | |
7351 | && (segment->p_offset + segment->p_filesz | |
7352 | >= ((bfd_vma) iehdr->e_phoff | |
7353 | + iehdr->e_phnum * iehdr->e_phentsize))); | |
7354 | ||
7355 | if (segment->p_type == PT_LOAD && map->includes_phdrs) | |
7356 | phdr_included = TRUE; | |
7357 | } | |
7358 | ||
bbefd0a9 | 7359 | lowest_section = NULL; |
84d1d650 L |
7360 | if (section_count != 0) |
7361 | { | |
7362 | unsigned int isec = 0; | |
7363 | ||
53020534 | 7364 | for (section = first_section; |
84d1d650 L |
7365 | section != NULL; |
7366 | section = section->next) | |
7367 | { | |
7368 | this_hdr = &(elf_section_data(section)->this_hdr); | |
f4638467 | 7369 | if (ELF_SECTION_IN_SEGMENT (this_hdr, segment)) |
53020534 L |
7370 | { |
7371 | map->sections[isec++] = section->output_section; | |
a76e6f2f AM |
7372 | if ((section->flags & SEC_ALLOC) != 0) |
7373 | { | |
7374 | bfd_vma seg_off; | |
7375 | ||
bbefd0a9 AM |
7376 | if (lowest_section == NULL |
7377 | || section->lma < lowest_section->lma) | |
fb8a5684 AM |
7378 | lowest_section = section; |
7379 | ||
a76e6f2f AM |
7380 | /* Section lmas are set up from PT_LOAD header |
7381 | p_paddr in _bfd_elf_make_section_from_shdr. | |
7382 | If this header has a p_paddr that disagrees | |
7383 | with the section lma, flag the p_paddr as | |
7384 | invalid. */ | |
7385 | if ((section->flags & SEC_LOAD) != 0) | |
7386 | seg_off = this_hdr->sh_offset - segment->p_offset; | |
7387 | else | |
7388 | seg_off = this_hdr->sh_addr - segment->p_vaddr; | |
7389 | if (section->lma - segment->p_paddr != seg_off) | |
7390 | map->p_paddr_valid = FALSE; | |
7391 | } | |
53020534 L |
7392 | if (isec == section_count) |
7393 | break; | |
7394 | } | |
84d1d650 L |
7395 | } |
7396 | } | |
7397 | ||
a76e6f2f | 7398 | if (map->includes_filehdr && lowest_section != NULL) |
2542e49e JL |
7399 | { |
7400 | /* Try to keep the space used by the headers plus any | |
7401 | padding fixed. If there are sections with file contents | |
7402 | in this segment then the lowest sh_offset is the best | |
7403 | guess. Otherwise the segment only has file contents for | |
7404 | the headers, and p_filesz is the best guess. */ | |
7405 | if (no_contents) | |
7406 | map->header_size = segment->p_filesz; | |
7407 | else | |
7408 | map->header_size = lowest_section->filepos; | |
7409 | } | |
d324f6d6 | 7410 | |
5d695627 AM |
7411 | if (section_count == 0) |
7412 | map->p_vaddr_offset = segment->p_vaddr; | |
7413 | else if (!map->includes_phdrs | |
7414 | && !map->includes_filehdr | |
7415 | && map->p_paddr_valid) | |
7416 | /* Account for padding before the first section. */ | |
7417 | map->p_vaddr_offset = (segment->p_paddr | |
7418 | - (lowest_section ? lowest_section->lma : 0)); | |
a76e6f2f | 7419 | |
84d1d650 L |
7420 | map->count = section_count; |
7421 | *pointer_to_map = map; | |
7422 | pointer_to_map = &map->next; | |
7423 | } | |
7424 | ||
12bd6957 | 7425 | elf_seg_map (obfd) = map_first; |
84d1d650 L |
7426 | return TRUE; |
7427 | } | |
7428 | ||
7429 | /* Copy private BFD data. This copies or rewrites ELF program header | |
7430 | information. */ | |
7431 | ||
7432 | static bfd_boolean | |
7433 | copy_private_bfd_data (bfd *ibfd, bfd *obfd) | |
7434 | { | |
84d1d650 L |
7435 | if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour |
7436 | || bfd_get_flavour (obfd) != bfd_target_elf_flavour) | |
7437 | return TRUE; | |
7438 | ||
7439 | if (elf_tdata (ibfd)->phdr == NULL) | |
7440 | return TRUE; | |
7441 | ||
7442 | if (ibfd->xvec == obfd->xvec) | |
7443 | { | |
cb3ff1e5 NC |
7444 | /* Check to see if any sections in the input BFD |
7445 | covered by ELF program header have changed. */ | |
d55ce4e2 | 7446 | Elf_Internal_Phdr *segment; |
84d1d650 L |
7447 | asection *section, *osec; |
7448 | unsigned int i, num_segments; | |
7449 | Elf_Internal_Shdr *this_hdr; | |
147d51c2 L |
7450 | const struct elf_backend_data *bed; |
7451 | ||
7452 | bed = get_elf_backend_data (ibfd); | |
7453 | ||
7454 | /* Regenerate the segment map if p_paddr is set to 0. */ | |
7455 | if (bed->want_p_paddr_set_to_zero) | |
7456 | goto rewrite; | |
84d1d650 L |
7457 | |
7458 | /* Initialize the segment mark field. */ | |
7459 | for (section = obfd->sections; section != NULL; | |
7460 | section = section->next) | |
7461 | section->segment_mark = FALSE; | |
7462 | ||
7463 | num_segments = elf_elfheader (ibfd)->e_phnum; | |
7464 | for (i = 0, segment = elf_tdata (ibfd)->phdr; | |
7465 | i < num_segments; | |
7466 | i++, segment++) | |
7467 | { | |
5f6999aa NC |
7468 | /* PR binutils/3535. The Solaris linker always sets the p_paddr |
7469 | and p_memsz fields of special segments (DYNAMIC, INTERP) to 0 | |
7470 | which severly confuses things, so always regenerate the segment | |
7471 | map in this case. */ | |
7472 | if (segment->p_paddr == 0 | |
7473 | && segment->p_memsz == 0 | |
7474 | && (segment->p_type == PT_INTERP || segment->p_type == PT_DYNAMIC)) | |
cb3ff1e5 | 7475 | goto rewrite; |
5f6999aa | 7476 | |
84d1d650 L |
7477 | for (section = ibfd->sections; |
7478 | section != NULL; section = section->next) | |
7479 | { | |
7480 | /* We mark the output section so that we know it comes | |
7481 | from the input BFD. */ | |
7482 | osec = section->output_section; | |
7483 | if (osec) | |
7484 | osec->segment_mark = TRUE; | |
7485 | ||
7486 | /* Check if this section is covered by the segment. */ | |
7487 | this_hdr = &(elf_section_data(section)->this_hdr); | |
f4638467 | 7488 | if (ELF_SECTION_IN_SEGMENT (this_hdr, segment)) |
84d1d650 L |
7489 | { |
7490 | /* FIXME: Check if its output section is changed or | |
7491 | removed. What else do we need to check? */ | |
7492 | if (osec == NULL | |
7493 | || section->flags != osec->flags | |
7494 | || section->lma != osec->lma | |
7495 | || section->vma != osec->vma | |
7496 | || section->size != osec->size | |
7497 | || section->rawsize != osec->rawsize | |
7498 | || section->alignment_power != osec->alignment_power) | |
7499 | goto rewrite; | |
7500 | } | |
7501 | } | |
7502 | } | |
7503 | ||
cb3ff1e5 | 7504 | /* Check to see if any output section do not come from the |
84d1d650 L |
7505 | input BFD. */ |
7506 | for (section = obfd->sections; section != NULL; | |
7507 | section = section->next) | |
7508 | { | |
535b785f | 7509 | if (!section->segment_mark) |
84d1d650 L |
7510 | goto rewrite; |
7511 | else | |
7512 | section->segment_mark = FALSE; | |
7513 | } | |
7514 | ||
7515 | return copy_elf_program_header (ibfd, obfd); | |
7516 | } | |
7517 | ||
7518 | rewrite: | |
f1d85785 L |
7519 | if (ibfd->xvec == obfd->xvec) |
7520 | { | |
7521 | /* When rewriting program header, set the output maxpagesize to | |
7522 | the maximum alignment of input PT_LOAD segments. */ | |
7523 | Elf_Internal_Phdr *segment; | |
7524 | unsigned int i; | |
7525 | unsigned int num_segments = elf_elfheader (ibfd)->e_phnum; | |
7526 | bfd_vma maxpagesize = 0; | |
7527 | ||
7528 | for (i = 0, segment = elf_tdata (ibfd)->phdr; | |
7529 | i < num_segments; | |
7530 | i++, segment++) | |
7531 | if (segment->p_type == PT_LOAD | |
7532 | && maxpagesize < segment->p_align) | |
c86934ce NC |
7533 | { |
7534 | /* PR 17512: file: f17299af. */ | |
7535 | if (segment->p_align > (bfd_vma) 1 << ((sizeof (bfd_vma) * 8) - 2)) | |
695344c0 | 7536 | /* xgettext:c-format */ |
2dcf00ce AM |
7537 | _bfd_error_handler (_("%pB: warning: segment alignment of %#" |
7538 | PRIx64 " is too large"), | |
7539 | ibfd, (uint64_t) segment->p_align); | |
c86934ce NC |
7540 | else |
7541 | maxpagesize = segment->p_align; | |
7542 | } | |
f1d85785 L |
7543 | |
7544 | if (maxpagesize != get_elf_backend_data (obfd)->maxpagesize) | |
7545 | bfd_emul_set_maxpagesize (bfd_get_target (obfd), maxpagesize); | |
7546 | } | |
7547 | ||
84d1d650 L |
7548 | return rewrite_elf_program_header (ibfd, obfd); |
7549 | } | |
7550 | ||
ccd2ec6a L |
7551 | /* Initialize private output section information from input section. */ |
7552 | ||
7553 | bfd_boolean | |
7554 | _bfd_elf_init_private_section_data (bfd *ibfd, | |
7555 | asection *isec, | |
7556 | bfd *obfd, | |
7557 | asection *osec, | |
7558 | struct bfd_link_info *link_info) | |
7559 | ||
7560 | { | |
7561 | Elf_Internal_Shdr *ihdr, *ohdr; | |
0e1862bb L |
7562 | bfd_boolean final_link = (link_info != NULL |
7563 | && !bfd_link_relocatable (link_info)); | |
ccd2ec6a L |
7564 | |
7565 | if (ibfd->xvec->flavour != bfd_target_elf_flavour | |
7566 | || obfd->xvec->flavour != bfd_target_elf_flavour) | |
7567 | return TRUE; | |
7568 | ||
ba85c43e NC |
7569 | BFD_ASSERT (elf_section_data (osec) != NULL); |
7570 | ||
dfa7b0b8 AM |
7571 | /* For objcopy and relocatable link, don't copy the output ELF |
7572 | section type from input if the output BFD section flags have been | |
7573 | set to something different. For a final link allow some flags | |
7574 | that the linker clears to differ. */ | |
42bb2e33 | 7575 | if (elf_section_type (osec) == SHT_NULL |
dfa7b0b8 AM |
7576 | && (osec->flags == isec->flags |
7577 | || (final_link | |
7578 | && ((osec->flags ^ isec->flags) | |
0814be7d | 7579 | & ~(SEC_LINK_ONCE | SEC_LINK_DUPLICATES | SEC_RELOC)) == 0))) |
42bb2e33 | 7580 | elf_section_type (osec) = elf_section_type (isec); |
d270463e L |
7581 | |
7582 | /* FIXME: Is this correct for all OS/PROC specific flags? */ | |
7583 | elf_section_flags (osec) |= (elf_section_flags (isec) | |
7584 | & (SHF_MASKOS | SHF_MASKPROC)); | |
ccd2ec6a | 7585 | |
a91e1603 L |
7586 | /* Copy sh_info from input for mbind section. */ |
7587 | if (elf_section_flags (isec) & SHF_GNU_MBIND) | |
7588 | elf_section_data (osec)->this_hdr.sh_info | |
7589 | = elf_section_data (isec)->this_hdr.sh_info; | |
7590 | ||
ccd2ec6a L |
7591 | /* Set things up for objcopy and relocatable link. The output |
7592 | SHT_GROUP section will have its elf_next_in_group pointing back | |
7593 | to the input group members. Ignore linker created group section. | |
7594 | See elfNN_ia64_object_p in elfxx-ia64.c. */ | |
7bdf4127 AB |
7595 | if ((link_info == NULL |
7596 | || !link_info->resolve_section_groups) | |
7597 | && (elf_sec_group (isec) == NULL | |
7598 | || (elf_sec_group (isec)->flags & SEC_LINKER_CREATED) == 0)) | |
ccd2ec6a | 7599 | { |
7bdf4127 AB |
7600 | if (elf_section_flags (isec) & SHF_GROUP) |
7601 | elf_section_flags (osec) |= SHF_GROUP; | |
7602 | elf_next_in_group (osec) = elf_next_in_group (isec); | |
7603 | elf_section_data (osec)->group = elf_section_data (isec)->group; | |
ccd2ec6a L |
7604 | } |
7605 | ||
7bdf4127 AB |
7606 | /* If not decompress, preserve SHF_COMPRESSED. */ |
7607 | if (!final_link && (ibfd->flags & BFD_DECOMPRESS) == 0) | |
7608 | elf_section_flags (osec) |= (elf_section_flags (isec) | |
7609 | & SHF_COMPRESSED); | |
7610 | ||
ccd2ec6a L |
7611 | ihdr = &elf_section_data (isec)->this_hdr; |
7612 | ||
7613 | /* We need to handle elf_linked_to_section for SHF_LINK_ORDER. We | |
7614 | don't use the output section of the linked-to section since it | |
7615 | may be NULL at this point. */ | |
7616 | if ((ihdr->sh_flags & SHF_LINK_ORDER) != 0) | |
7617 | { | |
7618 | ohdr = &elf_section_data (osec)->this_hdr; | |
7619 | ohdr->sh_flags |= SHF_LINK_ORDER; | |
7620 | elf_linked_to_section (osec) = elf_linked_to_section (isec); | |
7621 | } | |
7622 | ||
7623 | osec->use_rela_p = isec->use_rela_p; | |
7624 | ||
7625 | return TRUE; | |
7626 | } | |
7627 | ||
252b5132 RH |
7628 | /* Copy private section information. This copies over the entsize |
7629 | field, and sometimes the info field. */ | |
7630 | ||
b34976b6 | 7631 | bfd_boolean |
217aa764 AM |
7632 | _bfd_elf_copy_private_section_data (bfd *ibfd, |
7633 | asection *isec, | |
7634 | bfd *obfd, | |
7635 | asection *osec) | |
252b5132 RH |
7636 | { |
7637 | Elf_Internal_Shdr *ihdr, *ohdr; | |
7638 | ||
7639 | if (ibfd->xvec->flavour != bfd_target_elf_flavour | |
7640 | || obfd->xvec->flavour != bfd_target_elf_flavour) | |
b34976b6 | 7641 | return TRUE; |
252b5132 | 7642 | |
252b5132 RH |
7643 | ihdr = &elf_section_data (isec)->this_hdr; |
7644 | ohdr = &elf_section_data (osec)->this_hdr; | |
7645 | ||
7646 | ohdr->sh_entsize = ihdr->sh_entsize; | |
7647 | ||
7648 | if (ihdr->sh_type == SHT_SYMTAB | |
7649 | || ihdr->sh_type == SHT_DYNSYM | |
7650 | || ihdr->sh_type == SHT_GNU_verneed | |
7651 | || ihdr->sh_type == SHT_GNU_verdef) | |
7652 | ohdr->sh_info = ihdr->sh_info; | |
7653 | ||
ccd2ec6a L |
7654 | return _bfd_elf_init_private_section_data (ibfd, isec, obfd, osec, |
7655 | NULL); | |
252b5132 RH |
7656 | } |
7657 | ||
d0bf826b AM |
7658 | /* Look at all the SHT_GROUP sections in IBFD, making any adjustments |
7659 | necessary if we are removing either the SHT_GROUP section or any of | |
7660 | the group member sections. DISCARDED is the value that a section's | |
7661 | output_section has if the section will be discarded, NULL when this | |
7662 | function is called from objcopy, bfd_abs_section_ptr when called | |
7663 | from the linker. */ | |
80fccad2 BW |
7664 | |
7665 | bfd_boolean | |
d0bf826b | 7666 | _bfd_elf_fixup_group_sections (bfd *ibfd, asection *discarded) |
80fccad2 | 7667 | { |
30288845 AM |
7668 | asection *isec; |
7669 | ||
30288845 | 7670 | for (isec = ibfd->sections; isec != NULL; isec = isec->next) |
415f38a6 | 7671 | if (elf_section_type (isec) == SHT_GROUP) |
30288845 AM |
7672 | { |
7673 | asection *first = elf_next_in_group (isec); | |
7674 | asection *s = first; | |
d0bf826b AM |
7675 | bfd_size_type removed = 0; |
7676 | ||
30288845 AM |
7677 | while (s != NULL) |
7678 | { | |
415f38a6 AM |
7679 | /* If this member section is being output but the |
7680 | SHT_GROUP section is not, then clear the group info | |
7681 | set up by _bfd_elf_copy_private_section_data. */ | |
d0bf826b AM |
7682 | if (s->output_section != discarded |
7683 | && isec->output_section == discarded) | |
30288845 AM |
7684 | { |
7685 | elf_section_flags (s->output_section) &= ~SHF_GROUP; | |
7686 | elf_group_name (s->output_section) = NULL; | |
7687 | } | |
415f38a6 AM |
7688 | /* Conversely, if the member section is not being output |
7689 | but the SHT_GROUP section is, then adjust its size. */ | |
d0bf826b AM |
7690 | else if (s->output_section == discarded |
7691 | && isec->output_section != discarded) | |
6e5e9d58 AM |
7692 | { |
7693 | struct bfd_elf_section_data *elf_sec = elf_section_data (s); | |
7694 | removed += 4; | |
7695 | if (elf_sec->rel.hdr != NULL | |
7696 | && (elf_sec->rel.hdr->sh_flags & SHF_GROUP) != 0) | |
7697 | removed += 4; | |
7698 | if (elf_sec->rela.hdr != NULL | |
7699 | && (elf_sec->rela.hdr->sh_flags & SHF_GROUP) != 0) | |
7700 | removed += 4; | |
7701 | } | |
30288845 AM |
7702 | s = elf_next_in_group (s); |
7703 | if (s == first) | |
7704 | break; | |
7705 | } | |
d0bf826b AM |
7706 | if (removed != 0) |
7707 | { | |
7708 | if (discarded != NULL) | |
7709 | { | |
7710 | /* If we've been called for ld -r, then we need to | |
6e5e9d58 | 7711 | adjust the input section size. */ |
d0bf826b AM |
7712 | if (isec->rawsize == 0) |
7713 | isec->rawsize = isec->size; | |
7714 | isec->size = isec->rawsize - removed; | |
6e5e9d58 AM |
7715 | if (isec->size <= 4) |
7716 | { | |
7717 | isec->size = 0; | |
7718 | isec->flags |= SEC_EXCLUDE; | |
7719 | } | |
d0bf826b AM |
7720 | } |
7721 | else | |
7722 | { | |
7723 | /* Adjust the output section size when called from | |
7724 | objcopy. */ | |
7725 | isec->output_section->size -= removed; | |
6e5e9d58 AM |
7726 | if (isec->output_section->size <= 4) |
7727 | { | |
7728 | isec->output_section->size = 0; | |
7729 | isec->output_section->flags |= SEC_EXCLUDE; | |
7730 | } | |
d0bf826b AM |
7731 | } |
7732 | } | |
30288845 AM |
7733 | } |
7734 | ||
80fccad2 BW |
7735 | return TRUE; |
7736 | } | |
7737 | ||
d0bf826b AM |
7738 | /* Copy private header information. */ |
7739 | ||
7740 | bfd_boolean | |
7741 | _bfd_elf_copy_private_header_data (bfd *ibfd, bfd *obfd) | |
7742 | { | |
7743 | if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour | |
7744 | || bfd_get_flavour (obfd) != bfd_target_elf_flavour) | |
7745 | return TRUE; | |
7746 | ||
7747 | /* Copy over private BFD data if it has not already been copied. | |
7748 | This must be done here, rather than in the copy_private_bfd_data | |
7749 | entry point, because the latter is called after the section | |
7750 | contents have been set, which means that the program headers have | |
7751 | already been worked out. */ | |
12bd6957 | 7752 | if (elf_seg_map (obfd) == NULL && elf_tdata (ibfd)->phdr != NULL) |
d0bf826b AM |
7753 | { |
7754 | if (! copy_private_bfd_data (ibfd, obfd)) | |
7755 | return FALSE; | |
7756 | } | |
7757 | ||
7758 | return _bfd_elf_fixup_group_sections (ibfd, NULL); | |
7759 | } | |
7760 | ||
252b5132 RH |
7761 | /* Copy private symbol information. If this symbol is in a section |
7762 | which we did not map into a BFD section, try to map the section | |
7763 | index correctly. We use special macro definitions for the mapped | |
7764 | section indices; these definitions are interpreted by the | |
7765 | swap_out_syms function. */ | |
7766 | ||
9ad5cbcf AM |
7767 | #define MAP_ONESYMTAB (SHN_HIOS + 1) |
7768 | #define MAP_DYNSYMTAB (SHN_HIOS + 2) | |
7769 | #define MAP_STRTAB (SHN_HIOS + 3) | |
7770 | #define MAP_SHSTRTAB (SHN_HIOS + 4) | |
7771 | #define MAP_SYM_SHNDX (SHN_HIOS + 5) | |
252b5132 | 7772 | |
b34976b6 | 7773 | bfd_boolean |
217aa764 AM |
7774 | _bfd_elf_copy_private_symbol_data (bfd *ibfd, |
7775 | asymbol *isymarg, | |
7776 | bfd *obfd, | |
7777 | asymbol *osymarg) | |
252b5132 RH |
7778 | { |
7779 | elf_symbol_type *isym, *osym; | |
7780 | ||
7781 | if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour | |
7782 | || bfd_get_flavour (obfd) != bfd_target_elf_flavour) | |
b34976b6 | 7783 | return TRUE; |
252b5132 RH |
7784 | |
7785 | isym = elf_symbol_from (ibfd, isymarg); | |
7786 | osym = elf_symbol_from (obfd, osymarg); | |
7787 | ||
7788 | if (isym != NULL | |
8424d8f5 | 7789 | && isym->internal_elf_sym.st_shndx != 0 |
252b5132 RH |
7790 | && osym != NULL |
7791 | && bfd_is_abs_section (isym->symbol.section)) | |
7792 | { | |
7793 | unsigned int shndx; | |
7794 | ||
7795 | shndx = isym->internal_elf_sym.st_shndx; | |
7796 | if (shndx == elf_onesymtab (ibfd)) | |
7797 | shndx = MAP_ONESYMTAB; | |
7798 | else if (shndx == elf_dynsymtab (ibfd)) | |
7799 | shndx = MAP_DYNSYMTAB; | |
12bd6957 | 7800 | else if (shndx == elf_strtab_sec (ibfd)) |
252b5132 | 7801 | shndx = MAP_STRTAB; |
12bd6957 | 7802 | else if (shndx == elf_shstrtab_sec (ibfd)) |
252b5132 | 7803 | shndx = MAP_SHSTRTAB; |
6a40cf0c | 7804 | else if (find_section_in_list (shndx, elf_symtab_shndx_list (ibfd))) |
9ad5cbcf | 7805 | shndx = MAP_SYM_SHNDX; |
252b5132 RH |
7806 | osym->internal_elf_sym.st_shndx = shndx; |
7807 | } | |
7808 | ||
b34976b6 | 7809 | return TRUE; |
252b5132 RH |
7810 | } |
7811 | ||
7812 | /* Swap out the symbols. */ | |
7813 | ||
b34976b6 | 7814 | static bfd_boolean |
217aa764 | 7815 | swap_out_syms (bfd *abfd, |
ef10c3ac | 7816 | struct elf_strtab_hash **sttp, |
217aa764 | 7817 | int relocatable_p) |
252b5132 | 7818 | { |
9c5bfbb7 | 7819 | const struct elf_backend_data *bed; |
079e9a2f AM |
7820 | int symcount; |
7821 | asymbol **syms; | |
ef10c3ac | 7822 | struct elf_strtab_hash *stt; |
079e9a2f | 7823 | Elf_Internal_Shdr *symtab_hdr; |
9ad5cbcf | 7824 | Elf_Internal_Shdr *symtab_shndx_hdr; |
079e9a2f | 7825 | Elf_Internal_Shdr *symstrtab_hdr; |
ef10c3ac | 7826 | struct elf_sym_strtab *symstrtab; |
f075ee0c AM |
7827 | bfd_byte *outbound_syms; |
7828 | bfd_byte *outbound_shndx; | |
ef10c3ac L |
7829 | unsigned long outbound_syms_index; |
7830 | unsigned long outbound_shndx_index; | |
079e9a2f | 7831 | int idx; |
12bd6957 | 7832 | unsigned int num_locals; |
079e9a2f | 7833 | bfd_size_type amt; |
174fd7f9 | 7834 | bfd_boolean name_local_sections; |
252b5132 | 7835 | |
12bd6957 | 7836 | if (!elf_map_symbols (abfd, &num_locals)) |
b34976b6 | 7837 | return FALSE; |
252b5132 | 7838 | |
c044fabd | 7839 | /* Dump out the symtabs. */ |
ef10c3ac | 7840 | stt = _bfd_elf_strtab_init (); |
079e9a2f | 7841 | if (stt == NULL) |
b34976b6 | 7842 | return FALSE; |
252b5132 | 7843 | |
079e9a2f AM |
7844 | bed = get_elf_backend_data (abfd); |
7845 | symcount = bfd_get_symcount (abfd); | |
7846 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; | |
7847 | symtab_hdr->sh_type = SHT_SYMTAB; | |
7848 | symtab_hdr->sh_entsize = bed->s->sizeof_sym; | |
7849 | symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1); | |
12bd6957 | 7850 | symtab_hdr->sh_info = num_locals + 1; |
72de5009 | 7851 | symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align; |
079e9a2f AM |
7852 | |
7853 | symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr; | |
7854 | symstrtab_hdr->sh_type = SHT_STRTAB; | |
7855 | ||
ef10c3ac L |
7856 | /* Allocate buffer to swap out the .strtab section. */ |
7857 | symstrtab = (struct elf_sym_strtab *) bfd_malloc ((symcount + 1) | |
7858 | * sizeof (*symstrtab)); | |
7859 | if (symstrtab == NULL) | |
7860 | { | |
7861 | _bfd_elf_strtab_free (stt); | |
7862 | return FALSE; | |
7863 | } | |
7864 | ||
a50b1753 | 7865 | outbound_syms = (bfd_byte *) bfd_alloc2 (abfd, 1 + symcount, |
07d6d2b8 | 7866 | bed->s->sizeof_sym); |
079e9a2f | 7867 | if (outbound_syms == NULL) |
5ed6aba4 | 7868 | { |
ef10c3ac L |
7869 | error_return: |
7870 | _bfd_elf_strtab_free (stt); | |
7871 | free (symstrtab); | |
5ed6aba4 NC |
7872 | return FALSE; |
7873 | } | |
217aa764 | 7874 | symtab_hdr->contents = outbound_syms; |
ef10c3ac | 7875 | outbound_syms_index = 0; |
252b5132 | 7876 | |
9ad5cbcf | 7877 | outbound_shndx = NULL; |
ef10c3ac | 7878 | outbound_shndx_index = 0; |
6a40cf0c NC |
7879 | |
7880 | if (elf_symtab_shndx_list (abfd)) | |
9ad5cbcf | 7881 | { |
6a40cf0c NC |
7882 | symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr; |
7883 | if (symtab_shndx_hdr->sh_name != 0) | |
7884 | { | |
7885 | amt = (bfd_size_type) (1 + symcount) * sizeof (Elf_External_Sym_Shndx); | |
7886 | outbound_shndx = (bfd_byte *) | |
7887 | bfd_zalloc2 (abfd, 1 + symcount, sizeof (Elf_External_Sym_Shndx)); | |
7888 | if (outbound_shndx == NULL) | |
7889 | goto error_return; | |
5ed6aba4 | 7890 | |
6a40cf0c NC |
7891 | symtab_shndx_hdr->contents = outbound_shndx; |
7892 | symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX; | |
7893 | symtab_shndx_hdr->sh_size = amt; | |
7894 | symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx); | |
7895 | symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx); | |
7896 | } | |
7897 | /* FIXME: What about any other headers in the list ? */ | |
9ad5cbcf AM |
7898 | } |
7899 | ||
589e6347 | 7900 | /* Now generate the data (for "contents"). */ |
079e9a2f AM |
7901 | { |
7902 | /* Fill in zeroth symbol and swap it out. */ | |
7903 | Elf_Internal_Sym sym; | |
7904 | sym.st_name = 0; | |
7905 | sym.st_value = 0; | |
7906 | sym.st_size = 0; | |
7907 | sym.st_info = 0; | |
7908 | sym.st_other = 0; | |
7909 | sym.st_shndx = SHN_UNDEF; | |
35fc36a8 | 7910 | sym.st_target_internal = 0; |
ef10c3ac L |
7911 | symstrtab[0].sym = sym; |
7912 | symstrtab[0].dest_index = outbound_syms_index; | |
7913 | symstrtab[0].destshndx_index = outbound_shndx_index; | |
7914 | outbound_syms_index++; | |
9ad5cbcf | 7915 | if (outbound_shndx != NULL) |
ef10c3ac | 7916 | outbound_shndx_index++; |
079e9a2f | 7917 | } |
252b5132 | 7918 | |
174fd7f9 RS |
7919 | name_local_sections |
7920 | = (bed->elf_backend_name_local_section_symbols | |
7921 | && bed->elf_backend_name_local_section_symbols (abfd)); | |
7922 | ||
079e9a2f | 7923 | syms = bfd_get_outsymbols (abfd); |
ef10c3ac | 7924 | for (idx = 0; idx < symcount;) |
252b5132 | 7925 | { |
252b5132 | 7926 | Elf_Internal_Sym sym; |
079e9a2f AM |
7927 | bfd_vma value = syms[idx]->value; |
7928 | elf_symbol_type *type_ptr; | |
7929 | flagword flags = syms[idx]->flags; | |
7930 | int type; | |
252b5132 | 7931 | |
174fd7f9 RS |
7932 | if (!name_local_sections |
7933 | && (flags & (BSF_SECTION_SYM | BSF_GLOBAL)) == BSF_SECTION_SYM) | |
079e9a2f AM |
7934 | { |
7935 | /* Local section symbols have no name. */ | |
ef10c3ac | 7936 | sym.st_name = (unsigned long) -1; |
079e9a2f AM |
7937 | } |
7938 | else | |
7939 | { | |
ef10c3ac L |
7940 | /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize |
7941 | to get the final offset for st_name. */ | |
7942 | sym.st_name | |
7943 | = (unsigned long) _bfd_elf_strtab_add (stt, syms[idx]->name, | |
7944 | FALSE); | |
079e9a2f | 7945 | if (sym.st_name == (unsigned long) -1) |
ef10c3ac | 7946 | goto error_return; |
079e9a2f | 7947 | } |
252b5132 | 7948 | |
079e9a2f | 7949 | type_ptr = elf_symbol_from (abfd, syms[idx]); |
252b5132 | 7950 | |
079e9a2f AM |
7951 | if ((flags & BSF_SECTION_SYM) == 0 |
7952 | && bfd_is_com_section (syms[idx]->section)) | |
7953 | { | |
7954 | /* ELF common symbols put the alignment into the `value' field, | |
7955 | and the size into the `size' field. This is backwards from | |
7956 | how BFD handles it, so reverse it here. */ | |
7957 | sym.st_size = value; | |
7958 | if (type_ptr == NULL | |
7959 | || type_ptr->internal_elf_sym.st_value == 0) | |
7960 | sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value)); | |
7961 | else | |
7962 | sym.st_value = type_ptr->internal_elf_sym.st_value; | |
7963 | sym.st_shndx = _bfd_elf_section_from_bfd_section | |
7964 | (abfd, syms[idx]->section); | |
7965 | } | |
7966 | else | |
7967 | { | |
7968 | asection *sec = syms[idx]->section; | |
cb33740c | 7969 | unsigned int shndx; |
252b5132 | 7970 | |
079e9a2f AM |
7971 | if (sec->output_section) |
7972 | { | |
7973 | value += sec->output_offset; | |
7974 | sec = sec->output_section; | |
7975 | } | |
589e6347 | 7976 | |
079e9a2f AM |
7977 | /* Don't add in the section vma for relocatable output. */ |
7978 | if (! relocatable_p) | |
7979 | value += sec->vma; | |
7980 | sym.st_value = value; | |
7981 | sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0; | |
7982 | ||
7983 | if (bfd_is_abs_section (sec) | |
7984 | && type_ptr != NULL | |
7985 | && type_ptr->internal_elf_sym.st_shndx != 0) | |
7986 | { | |
7987 | /* This symbol is in a real ELF section which we did | |
7988 | not create as a BFD section. Undo the mapping done | |
7989 | by copy_private_symbol_data. */ | |
7990 | shndx = type_ptr->internal_elf_sym.st_shndx; | |
7991 | switch (shndx) | |
7992 | { | |
7993 | case MAP_ONESYMTAB: | |
7994 | shndx = elf_onesymtab (abfd); | |
7995 | break; | |
7996 | case MAP_DYNSYMTAB: | |
7997 | shndx = elf_dynsymtab (abfd); | |
7998 | break; | |
7999 | case MAP_STRTAB: | |
12bd6957 | 8000 | shndx = elf_strtab_sec (abfd); |
079e9a2f AM |
8001 | break; |
8002 | case MAP_SHSTRTAB: | |
12bd6957 | 8003 | shndx = elf_shstrtab_sec (abfd); |
079e9a2f | 8004 | break; |
9ad5cbcf | 8005 | case MAP_SYM_SHNDX: |
6a40cf0c NC |
8006 | if (elf_symtab_shndx_list (abfd)) |
8007 | shndx = elf_symtab_shndx_list (abfd)->ndx; | |
9ad5cbcf | 8008 | break; |
079e9a2f | 8009 | default: |
15bc576a | 8010 | shndx = SHN_ABS; |
079e9a2f AM |
8011 | break; |
8012 | } | |
8013 | } | |
8014 | else | |
8015 | { | |
8016 | shndx = _bfd_elf_section_from_bfd_section (abfd, sec); | |
252b5132 | 8017 | |
cb33740c | 8018 | if (shndx == SHN_BAD) |
079e9a2f AM |
8019 | { |
8020 | asection *sec2; | |
8021 | ||
8022 | /* Writing this would be a hell of a lot easier if | |
8023 | we had some decent documentation on bfd, and | |
8024 | knew what to expect of the library, and what to | |
8025 | demand of applications. For example, it | |
8026 | appears that `objcopy' might not set the | |
8027 | section of a symbol to be a section that is | |
8028 | actually in the output file. */ | |
8029 | sec2 = bfd_get_section_by_name (abfd, sec->name); | |
5df1bc57 AM |
8030 | if (sec2 != NULL) |
8031 | shndx = _bfd_elf_section_from_bfd_section (abfd, sec2); | |
8032 | if (shndx == SHN_BAD) | |
589e6347 | 8033 | { |
695344c0 | 8034 | /* xgettext:c-format */ |
9793eb77 AM |
8035 | _bfd_error_handler |
8036 | (_("unable to find equivalent output section" | |
8037 | " for symbol '%s' from section '%s'"), | |
8038 | syms[idx]->name ? syms[idx]->name : "<Local sym>", | |
8039 | sec->name); | |
811072d8 | 8040 | bfd_set_error (bfd_error_invalid_operation); |
ef10c3ac | 8041 | goto error_return; |
589e6347 | 8042 | } |
079e9a2f AM |
8043 | } |
8044 | } | |
252b5132 | 8045 | |
079e9a2f AM |
8046 | sym.st_shndx = shndx; |
8047 | } | |
252b5132 | 8048 | |
13ae64f3 JJ |
8049 | if ((flags & BSF_THREAD_LOCAL) != 0) |
8050 | type = STT_TLS; | |
d8045f23 NC |
8051 | else if ((flags & BSF_GNU_INDIRECT_FUNCTION) != 0) |
8052 | type = STT_GNU_IFUNC; | |
13ae64f3 | 8053 | else if ((flags & BSF_FUNCTION) != 0) |
079e9a2f AM |
8054 | type = STT_FUNC; |
8055 | else if ((flags & BSF_OBJECT) != 0) | |
8056 | type = STT_OBJECT; | |
d9352518 DB |
8057 | else if ((flags & BSF_RELC) != 0) |
8058 | type = STT_RELC; | |
8059 | else if ((flags & BSF_SRELC) != 0) | |
8060 | type = STT_SRELC; | |
079e9a2f AM |
8061 | else |
8062 | type = STT_NOTYPE; | |
252b5132 | 8063 | |
13ae64f3 JJ |
8064 | if (syms[idx]->section->flags & SEC_THREAD_LOCAL) |
8065 | type = STT_TLS; | |
8066 | ||
589e6347 | 8067 | /* Processor-specific types. */ |
079e9a2f AM |
8068 | if (type_ptr != NULL |
8069 | && bed->elf_backend_get_symbol_type) | |
8070 | type = ((*bed->elf_backend_get_symbol_type) | |
8071 | (&type_ptr->internal_elf_sym, type)); | |
252b5132 | 8072 | |
079e9a2f AM |
8073 | if (flags & BSF_SECTION_SYM) |
8074 | { | |
8075 | if (flags & BSF_GLOBAL) | |
8076 | sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION); | |
8077 | else | |
8078 | sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION); | |
8079 | } | |
8080 | else if (bfd_is_com_section (syms[idx]->section)) | |
0a40daed | 8081 | { |
b8871f35 L |
8082 | if (type != STT_TLS) |
8083 | { | |
8084 | if ((abfd->flags & BFD_CONVERT_ELF_COMMON)) | |
8085 | type = ((abfd->flags & BFD_USE_ELF_STT_COMMON) | |
8086 | ? STT_COMMON : STT_OBJECT); | |
8087 | else | |
8088 | type = ((flags & BSF_ELF_COMMON) != 0 | |
8089 | ? STT_COMMON : STT_OBJECT); | |
8090 | } | |
8091 | sym.st_info = ELF_ST_INFO (STB_GLOBAL, type); | |
0a40daed | 8092 | } |
079e9a2f AM |
8093 | else if (bfd_is_und_section (syms[idx]->section)) |
8094 | sym.st_info = ELF_ST_INFO (((flags & BSF_WEAK) | |
8095 | ? STB_WEAK | |
8096 | : STB_GLOBAL), | |
8097 | type); | |
8098 | else if (flags & BSF_FILE) | |
8099 | sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE); | |
8100 | else | |
8101 | { | |
8102 | int bind = STB_LOCAL; | |
252b5132 | 8103 | |
079e9a2f AM |
8104 | if (flags & BSF_LOCAL) |
8105 | bind = STB_LOCAL; | |
3e7a7d11 NC |
8106 | else if (flags & BSF_GNU_UNIQUE) |
8107 | bind = STB_GNU_UNIQUE; | |
079e9a2f AM |
8108 | else if (flags & BSF_WEAK) |
8109 | bind = STB_WEAK; | |
8110 | else if (flags & BSF_GLOBAL) | |
8111 | bind = STB_GLOBAL; | |
252b5132 | 8112 | |
079e9a2f AM |
8113 | sym.st_info = ELF_ST_INFO (bind, type); |
8114 | } | |
252b5132 | 8115 | |
079e9a2f | 8116 | if (type_ptr != NULL) |
35fc36a8 RS |
8117 | { |
8118 | sym.st_other = type_ptr->internal_elf_sym.st_other; | |
8119 | sym.st_target_internal | |
8120 | = type_ptr->internal_elf_sym.st_target_internal; | |
8121 | } | |
079e9a2f | 8122 | else |
35fc36a8 RS |
8123 | { |
8124 | sym.st_other = 0; | |
8125 | sym.st_target_internal = 0; | |
8126 | } | |
252b5132 | 8127 | |
ef10c3ac L |
8128 | idx++; |
8129 | symstrtab[idx].sym = sym; | |
8130 | symstrtab[idx].dest_index = outbound_syms_index; | |
8131 | symstrtab[idx].destshndx_index = outbound_shndx_index; | |
8132 | ||
8133 | outbound_syms_index++; | |
9ad5cbcf | 8134 | if (outbound_shndx != NULL) |
ef10c3ac L |
8135 | outbound_shndx_index++; |
8136 | } | |
8137 | ||
8138 | /* Finalize the .strtab section. */ | |
8139 | _bfd_elf_strtab_finalize (stt); | |
8140 | ||
8141 | /* Swap out the .strtab section. */ | |
8142 | for (idx = 0; idx <= symcount; idx++) | |
8143 | { | |
8144 | struct elf_sym_strtab *elfsym = &symstrtab[idx]; | |
8145 | if (elfsym->sym.st_name == (unsigned long) -1) | |
8146 | elfsym->sym.st_name = 0; | |
8147 | else | |
8148 | elfsym->sym.st_name = _bfd_elf_strtab_offset (stt, | |
8149 | elfsym->sym.st_name); | |
8150 | bed->s->swap_symbol_out (abfd, &elfsym->sym, | |
8151 | (outbound_syms | |
8152 | + (elfsym->dest_index | |
8153 | * bed->s->sizeof_sym)), | |
8154 | (outbound_shndx | |
8155 | + (elfsym->destshndx_index | |
8156 | * sizeof (Elf_External_Sym_Shndx)))); | |
079e9a2f | 8157 | } |
ef10c3ac | 8158 | free (symstrtab); |
252b5132 | 8159 | |
079e9a2f | 8160 | *sttp = stt; |
ef10c3ac | 8161 | symstrtab_hdr->sh_size = _bfd_elf_strtab_size (stt); |
079e9a2f | 8162 | symstrtab_hdr->sh_type = SHT_STRTAB; |
84865015 | 8163 | symstrtab_hdr->sh_flags = bed->elf_strtab_flags; |
079e9a2f AM |
8164 | symstrtab_hdr->sh_addr = 0; |
8165 | symstrtab_hdr->sh_entsize = 0; | |
8166 | symstrtab_hdr->sh_link = 0; | |
8167 | symstrtab_hdr->sh_info = 0; | |
8168 | symstrtab_hdr->sh_addralign = 1; | |
252b5132 | 8169 | |
b34976b6 | 8170 | return TRUE; |
252b5132 RH |
8171 | } |
8172 | ||
8173 | /* Return the number of bytes required to hold the symtab vector. | |
8174 | ||
8175 | Note that we base it on the count plus 1, since we will null terminate | |
8176 | the vector allocated based on this size. However, the ELF symbol table | |
8177 | always has a dummy entry as symbol #0, so it ends up even. */ | |
8178 | ||
8179 | long | |
217aa764 | 8180 | _bfd_elf_get_symtab_upper_bound (bfd *abfd) |
252b5132 RH |
8181 | { |
8182 | long symcount; | |
8183 | long symtab_size; | |
8184 | Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr; | |
8185 | ||
8186 | symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym; | |
b99d1833 AM |
8187 | symtab_size = (symcount + 1) * (sizeof (asymbol *)); |
8188 | if (symcount > 0) | |
8189 | symtab_size -= sizeof (asymbol *); | |
252b5132 RH |
8190 | |
8191 | return symtab_size; | |
8192 | } | |
8193 | ||
8194 | long | |
217aa764 | 8195 | _bfd_elf_get_dynamic_symtab_upper_bound (bfd *abfd) |
252b5132 RH |
8196 | { |
8197 | long symcount; | |
8198 | long symtab_size; | |
8199 | Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr; | |
8200 | ||
8201 | if (elf_dynsymtab (abfd) == 0) | |
8202 | { | |
8203 | bfd_set_error (bfd_error_invalid_operation); | |
8204 | return -1; | |
8205 | } | |
8206 | ||
8207 | symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym; | |
b99d1833 AM |
8208 | symtab_size = (symcount + 1) * (sizeof (asymbol *)); |
8209 | if (symcount > 0) | |
8210 | symtab_size -= sizeof (asymbol *); | |
252b5132 RH |
8211 | |
8212 | return symtab_size; | |
8213 | } | |
8214 | ||
8215 | long | |
217aa764 AM |
8216 | _bfd_elf_get_reloc_upper_bound (bfd *abfd ATTRIBUTE_UNUSED, |
8217 | sec_ptr asect) | |
252b5132 RH |
8218 | { |
8219 | return (asect->reloc_count + 1) * sizeof (arelent *); | |
8220 | } | |
8221 | ||
8222 | /* Canonicalize the relocs. */ | |
8223 | ||
8224 | long | |
217aa764 AM |
8225 | _bfd_elf_canonicalize_reloc (bfd *abfd, |
8226 | sec_ptr section, | |
8227 | arelent **relptr, | |
8228 | asymbol **symbols) | |
252b5132 RH |
8229 | { |
8230 | arelent *tblptr; | |
8231 | unsigned int i; | |
9c5bfbb7 | 8232 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
252b5132 | 8233 | |
b34976b6 | 8234 | if (! bed->s->slurp_reloc_table (abfd, section, symbols, FALSE)) |
252b5132 RH |
8235 | return -1; |
8236 | ||
8237 | tblptr = section->relocation; | |
8238 | for (i = 0; i < section->reloc_count; i++) | |
8239 | *relptr++ = tblptr++; | |
8240 | ||
8241 | *relptr = NULL; | |
8242 | ||
8243 | return section->reloc_count; | |
8244 | } | |
8245 | ||
8246 | long | |
6cee3f79 | 8247 | _bfd_elf_canonicalize_symtab (bfd *abfd, asymbol **allocation) |
252b5132 | 8248 | { |
9c5bfbb7 | 8249 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
217aa764 | 8250 | long symcount = bed->s->slurp_symbol_table (abfd, allocation, FALSE); |
252b5132 RH |
8251 | |
8252 | if (symcount >= 0) | |
8253 | bfd_get_symcount (abfd) = symcount; | |
8254 | return symcount; | |
8255 | } | |
8256 | ||
8257 | long | |
217aa764 AM |
8258 | _bfd_elf_canonicalize_dynamic_symtab (bfd *abfd, |
8259 | asymbol **allocation) | |
252b5132 | 8260 | { |
9c5bfbb7 | 8261 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
217aa764 | 8262 | long symcount = bed->s->slurp_symbol_table (abfd, allocation, TRUE); |
1f70368c DJ |
8263 | |
8264 | if (symcount >= 0) | |
8265 | bfd_get_dynamic_symcount (abfd) = symcount; | |
8266 | return symcount; | |
252b5132 RH |
8267 | } |
8268 | ||
8615f3f2 AM |
8269 | /* Return the size required for the dynamic reloc entries. Any loadable |
8270 | section that was actually installed in the BFD, and has type SHT_REL | |
8271 | or SHT_RELA, and uses the dynamic symbol table, is considered to be a | |
8272 | dynamic reloc section. */ | |
252b5132 RH |
8273 | |
8274 | long | |
217aa764 | 8275 | _bfd_elf_get_dynamic_reloc_upper_bound (bfd *abfd) |
252b5132 RH |
8276 | { |
8277 | long ret; | |
8278 | asection *s; | |
8279 | ||
8280 | if (elf_dynsymtab (abfd) == 0) | |
8281 | { | |
8282 | bfd_set_error (bfd_error_invalid_operation); | |
8283 | return -1; | |
8284 | } | |
8285 | ||
8286 | ret = sizeof (arelent *); | |
8287 | for (s = abfd->sections; s != NULL; s = s->next) | |
266b05cf | 8288 | if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd) |
252b5132 RH |
8289 | && (elf_section_data (s)->this_hdr.sh_type == SHT_REL |
8290 | || elf_section_data (s)->this_hdr.sh_type == SHT_RELA)) | |
eea6121a | 8291 | ret += ((s->size / elf_section_data (s)->this_hdr.sh_entsize) |
252b5132 RH |
8292 | * sizeof (arelent *)); |
8293 | ||
8294 | return ret; | |
8295 | } | |
8296 | ||
8615f3f2 AM |
8297 | /* Canonicalize the dynamic relocation entries. Note that we return the |
8298 | dynamic relocations as a single block, although they are actually | |
8299 | associated with particular sections; the interface, which was | |
8300 | designed for SunOS style shared libraries, expects that there is only | |
8301 | one set of dynamic relocs. Any loadable section that was actually | |
8302 | installed in the BFD, and has type SHT_REL or SHT_RELA, and uses the | |
8303 | dynamic symbol table, is considered to be a dynamic reloc section. */ | |
252b5132 RH |
8304 | |
8305 | long | |
217aa764 AM |
8306 | _bfd_elf_canonicalize_dynamic_reloc (bfd *abfd, |
8307 | arelent **storage, | |
8308 | asymbol **syms) | |
252b5132 | 8309 | { |
217aa764 | 8310 | bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean); |
252b5132 RH |
8311 | asection *s; |
8312 | long ret; | |
8313 | ||
8314 | if (elf_dynsymtab (abfd) == 0) | |
8315 | { | |
8316 | bfd_set_error (bfd_error_invalid_operation); | |
8317 | return -1; | |
8318 | } | |
8319 | ||
8320 | slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table; | |
8321 | ret = 0; | |
8322 | for (s = abfd->sections; s != NULL; s = s->next) | |
8323 | { | |
266b05cf | 8324 | if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd) |
252b5132 RH |
8325 | && (elf_section_data (s)->this_hdr.sh_type == SHT_REL |
8326 | || elf_section_data (s)->this_hdr.sh_type == SHT_RELA)) | |
8327 | { | |
8328 | arelent *p; | |
8329 | long count, i; | |
8330 | ||
b34976b6 | 8331 | if (! (*slurp_relocs) (abfd, s, syms, TRUE)) |
252b5132 | 8332 | return -1; |
eea6121a | 8333 | count = s->size / elf_section_data (s)->this_hdr.sh_entsize; |
252b5132 RH |
8334 | p = s->relocation; |
8335 | for (i = 0; i < count; i++) | |
8336 | *storage++ = p++; | |
8337 | ret += count; | |
8338 | } | |
8339 | } | |
8340 | ||
8341 | *storage = NULL; | |
8342 | ||
8343 | return ret; | |
8344 | } | |
8345 | \f | |
8346 | /* Read in the version information. */ | |
8347 | ||
b34976b6 | 8348 | bfd_boolean |
fc0e6df6 | 8349 | _bfd_elf_slurp_version_tables (bfd *abfd, bfd_boolean default_imported_symver) |
252b5132 RH |
8350 | { |
8351 | bfd_byte *contents = NULL; | |
fc0e6df6 PB |
8352 | unsigned int freeidx = 0; |
8353 | ||
8354 | if (elf_dynverref (abfd) != 0) | |
8355 | { | |
8356 | Elf_Internal_Shdr *hdr; | |
8357 | Elf_External_Verneed *everneed; | |
8358 | Elf_Internal_Verneed *iverneed; | |
8359 | unsigned int i; | |
d0fb9a8d | 8360 | bfd_byte *contents_end; |
fc0e6df6 PB |
8361 | |
8362 | hdr = &elf_tdata (abfd)->dynverref_hdr; | |
8363 | ||
bd61e135 AM |
8364 | if (hdr->sh_info == 0 |
8365 | || hdr->sh_info > hdr->sh_size / sizeof (Elf_External_Verneed)) | |
d0fb9a8d | 8366 | { |
601a03ba | 8367 | error_return_bad_verref: |
4eca0228 | 8368 | _bfd_error_handler |
871b3ab2 | 8369 | (_("%pB: .gnu.version_r invalid entry"), abfd); |
601a03ba | 8370 | bfd_set_error (bfd_error_bad_value); |
d0fb9a8d JJ |
8371 | error_return_verref: |
8372 | elf_tdata (abfd)->verref = NULL; | |
8373 | elf_tdata (abfd)->cverrefs = 0; | |
8374 | goto error_return; | |
8375 | } | |
601a03ba AM |
8376 | |
8377 | contents = (bfd_byte *) bfd_malloc (hdr->sh_size); | |
8378 | if (contents == NULL) | |
8379 | goto error_return_verref; | |
8380 | ||
fc0e6df6 PB |
8381 | if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0 |
8382 | || bfd_bread (contents, hdr->sh_size, abfd) != hdr->sh_size) | |
d0fb9a8d | 8383 | goto error_return_verref; |
fc0e6df6 | 8384 | |
601a03ba | 8385 | elf_tdata (abfd)->verref = (Elf_Internal_Verneed *) |
bd61e135 | 8386 | bfd_alloc2 (abfd, hdr->sh_info, sizeof (Elf_Internal_Verneed)); |
601a03ba AM |
8387 | |
8388 | if (elf_tdata (abfd)->verref == NULL) | |
d0fb9a8d JJ |
8389 | goto error_return_verref; |
8390 | ||
8391 | BFD_ASSERT (sizeof (Elf_External_Verneed) | |
8392 | == sizeof (Elf_External_Vernaux)); | |
8393 | contents_end = contents + hdr->sh_size - sizeof (Elf_External_Verneed); | |
fc0e6df6 PB |
8394 | everneed = (Elf_External_Verneed *) contents; |
8395 | iverneed = elf_tdata (abfd)->verref; | |
8396 | for (i = 0; i < hdr->sh_info; i++, iverneed++) | |
8397 | { | |
8398 | Elf_External_Vernaux *evernaux; | |
8399 | Elf_Internal_Vernaux *ivernaux; | |
8400 | unsigned int j; | |
8401 | ||
8402 | _bfd_elf_swap_verneed_in (abfd, everneed, iverneed); | |
8403 | ||
8404 | iverneed->vn_bfd = abfd; | |
8405 | ||
8406 | iverneed->vn_filename = | |
8407 | bfd_elf_string_from_elf_section (abfd, hdr->sh_link, | |
8408 | iverneed->vn_file); | |
8409 | if (iverneed->vn_filename == NULL) | |
601a03ba | 8410 | goto error_return_bad_verref; |
fc0e6df6 | 8411 | |
d0fb9a8d JJ |
8412 | if (iverneed->vn_cnt == 0) |
8413 | iverneed->vn_auxptr = NULL; | |
8414 | else | |
8415 | { | |
a50b1753 | 8416 | iverneed->vn_auxptr = (struct elf_internal_vernaux *) |
07d6d2b8 AM |
8417 | bfd_alloc2 (abfd, iverneed->vn_cnt, |
8418 | sizeof (Elf_Internal_Vernaux)); | |
d0fb9a8d JJ |
8419 | if (iverneed->vn_auxptr == NULL) |
8420 | goto error_return_verref; | |
8421 | } | |
8422 | ||
8423 | if (iverneed->vn_aux | |
8424 | > (size_t) (contents_end - (bfd_byte *) everneed)) | |
601a03ba | 8425 | goto error_return_bad_verref; |
fc0e6df6 PB |
8426 | |
8427 | evernaux = ((Elf_External_Vernaux *) | |
8428 | ((bfd_byte *) everneed + iverneed->vn_aux)); | |
8429 | ivernaux = iverneed->vn_auxptr; | |
8430 | for (j = 0; j < iverneed->vn_cnt; j++, ivernaux++) | |
8431 | { | |
8432 | _bfd_elf_swap_vernaux_in (abfd, evernaux, ivernaux); | |
8433 | ||
8434 | ivernaux->vna_nodename = | |
8435 | bfd_elf_string_from_elf_section (abfd, hdr->sh_link, | |
8436 | ivernaux->vna_name); | |
8437 | if (ivernaux->vna_nodename == NULL) | |
601a03ba | 8438 | goto error_return_bad_verref; |
fc0e6df6 | 8439 | |
25ff461f AM |
8440 | if (ivernaux->vna_other > freeidx) |
8441 | freeidx = ivernaux->vna_other; | |
8442 | ||
8443 | ivernaux->vna_nextptr = NULL; | |
8444 | if (ivernaux->vna_next == 0) | |
8445 | { | |
8446 | iverneed->vn_cnt = j + 1; | |
8447 | break; | |
8448 | } | |
fc0e6df6 PB |
8449 | if (j + 1 < iverneed->vn_cnt) |
8450 | ivernaux->vna_nextptr = ivernaux + 1; | |
fc0e6df6 | 8451 | |
d0fb9a8d JJ |
8452 | if (ivernaux->vna_next |
8453 | > (size_t) (contents_end - (bfd_byte *) evernaux)) | |
601a03ba | 8454 | goto error_return_bad_verref; |
d0fb9a8d | 8455 | |
fc0e6df6 PB |
8456 | evernaux = ((Elf_External_Vernaux *) |
8457 | ((bfd_byte *) evernaux + ivernaux->vna_next)); | |
fc0e6df6 PB |
8458 | } |
8459 | ||
25ff461f AM |
8460 | iverneed->vn_nextref = NULL; |
8461 | if (iverneed->vn_next == 0) | |
8462 | break; | |
fc0e6df6 PB |
8463 | if (i + 1 < hdr->sh_info) |
8464 | iverneed->vn_nextref = iverneed + 1; | |
fc0e6df6 | 8465 | |
d0fb9a8d JJ |
8466 | if (iverneed->vn_next |
8467 | > (size_t) (contents_end - (bfd_byte *) everneed)) | |
601a03ba | 8468 | goto error_return_bad_verref; |
d0fb9a8d | 8469 | |
fc0e6df6 PB |
8470 | everneed = ((Elf_External_Verneed *) |
8471 | ((bfd_byte *) everneed + iverneed->vn_next)); | |
8472 | } | |
25ff461f | 8473 | elf_tdata (abfd)->cverrefs = i; |
fc0e6df6 PB |
8474 | |
8475 | free (contents); | |
8476 | contents = NULL; | |
8477 | } | |
252b5132 RH |
8478 | |
8479 | if (elf_dynverdef (abfd) != 0) | |
8480 | { | |
8481 | Elf_Internal_Shdr *hdr; | |
8482 | Elf_External_Verdef *everdef; | |
8483 | Elf_Internal_Verdef *iverdef; | |
f631889e UD |
8484 | Elf_Internal_Verdef *iverdefarr; |
8485 | Elf_Internal_Verdef iverdefmem; | |
252b5132 | 8486 | unsigned int i; |
062e2358 | 8487 | unsigned int maxidx; |
d0fb9a8d | 8488 | bfd_byte *contents_end_def, *contents_end_aux; |
252b5132 RH |
8489 | |
8490 | hdr = &elf_tdata (abfd)->dynverdef_hdr; | |
8491 | ||
601a03ba AM |
8492 | if (hdr->sh_info == 0 || hdr->sh_size < sizeof (Elf_External_Verdef)) |
8493 | { | |
8494 | error_return_bad_verdef: | |
4eca0228 | 8495 | _bfd_error_handler |
871b3ab2 | 8496 | (_("%pB: .gnu.version_d invalid entry"), abfd); |
601a03ba AM |
8497 | bfd_set_error (bfd_error_bad_value); |
8498 | error_return_verdef: | |
8499 | elf_tdata (abfd)->verdef = NULL; | |
8500 | elf_tdata (abfd)->cverdefs = 0; | |
8501 | goto error_return; | |
8502 | } | |
8503 | ||
a50b1753 | 8504 | contents = (bfd_byte *) bfd_malloc (hdr->sh_size); |
252b5132 | 8505 | if (contents == NULL) |
601a03ba | 8506 | goto error_return_verdef; |
252b5132 | 8507 | if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0 |
217aa764 | 8508 | || bfd_bread (contents, hdr->sh_size, abfd) != hdr->sh_size) |
601a03ba | 8509 | goto error_return_verdef; |
d0fb9a8d JJ |
8510 | |
8511 | BFD_ASSERT (sizeof (Elf_External_Verdef) | |
8512 | >= sizeof (Elf_External_Verdaux)); | |
8513 | contents_end_def = contents + hdr->sh_size | |
8514 | - sizeof (Elf_External_Verdef); | |
8515 | contents_end_aux = contents + hdr->sh_size | |
8516 | - sizeof (Elf_External_Verdaux); | |
8517 | ||
f631889e UD |
8518 | /* We know the number of entries in the section but not the maximum |
8519 | index. Therefore we have to run through all entries and find | |
8520 | the maximum. */ | |
252b5132 | 8521 | everdef = (Elf_External_Verdef *) contents; |
f631889e UD |
8522 | maxidx = 0; |
8523 | for (i = 0; i < hdr->sh_info; ++i) | |
8524 | { | |
8525 | _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem); | |
8526 | ||
601a03ba AM |
8527 | if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) == 0) |
8528 | goto error_return_bad_verdef; | |
062e2358 AM |
8529 | if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) > maxidx) |
8530 | maxidx = iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION); | |
f631889e | 8531 | |
25ff461f AM |
8532 | if (iverdefmem.vd_next == 0) |
8533 | break; | |
8534 | ||
d0fb9a8d JJ |
8535 | if (iverdefmem.vd_next |
8536 | > (size_t) (contents_end_def - (bfd_byte *) everdef)) | |
601a03ba | 8537 | goto error_return_bad_verdef; |
d0fb9a8d | 8538 | |
f631889e UD |
8539 | everdef = ((Elf_External_Verdef *) |
8540 | ((bfd_byte *) everdef + iverdefmem.vd_next)); | |
8541 | } | |
8542 | ||
fc0e6df6 PB |
8543 | if (default_imported_symver) |
8544 | { | |
8545 | if (freeidx > maxidx) | |
8546 | maxidx = ++freeidx; | |
8547 | else | |
8548 | freeidx = ++maxidx; | |
8549 | } | |
201159ec | 8550 | |
601a03ba AM |
8551 | elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *) |
8552 | bfd_zalloc2 (abfd, maxidx, sizeof (Elf_Internal_Verdef)); | |
f631889e | 8553 | if (elf_tdata (abfd)->verdef == NULL) |
601a03ba | 8554 | goto error_return_verdef; |
f631889e UD |
8555 | |
8556 | elf_tdata (abfd)->cverdefs = maxidx; | |
8557 | ||
8558 | everdef = (Elf_External_Verdef *) contents; | |
8559 | iverdefarr = elf_tdata (abfd)->verdef; | |
8560 | for (i = 0; i < hdr->sh_info; i++) | |
252b5132 RH |
8561 | { |
8562 | Elf_External_Verdaux *everdaux; | |
8563 | Elf_Internal_Verdaux *iverdaux; | |
8564 | unsigned int j; | |
8565 | ||
f631889e UD |
8566 | _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem); |
8567 | ||
d0fb9a8d | 8568 | if ((iverdefmem.vd_ndx & VERSYM_VERSION) == 0) |
601a03ba | 8569 | goto error_return_bad_verdef; |
d0fb9a8d | 8570 | |
f631889e | 8571 | iverdef = &iverdefarr[(iverdefmem.vd_ndx & VERSYM_VERSION) - 1]; |
595bce75 | 8572 | memcpy (iverdef, &iverdefmem, offsetof (Elf_Internal_Verdef, vd_bfd)); |
252b5132 RH |
8573 | |
8574 | iverdef->vd_bfd = abfd; | |
8575 | ||
d0fb9a8d JJ |
8576 | if (iverdef->vd_cnt == 0) |
8577 | iverdef->vd_auxptr = NULL; | |
8578 | else | |
8579 | { | |
a50b1753 | 8580 | iverdef->vd_auxptr = (struct elf_internal_verdaux *) |
07d6d2b8 AM |
8581 | bfd_alloc2 (abfd, iverdef->vd_cnt, |
8582 | sizeof (Elf_Internal_Verdaux)); | |
d0fb9a8d JJ |
8583 | if (iverdef->vd_auxptr == NULL) |
8584 | goto error_return_verdef; | |
8585 | } | |
8586 | ||
8587 | if (iverdef->vd_aux | |
8588 | > (size_t) (contents_end_aux - (bfd_byte *) everdef)) | |
601a03ba | 8589 | goto error_return_bad_verdef; |
252b5132 RH |
8590 | |
8591 | everdaux = ((Elf_External_Verdaux *) | |
8592 | ((bfd_byte *) everdef + iverdef->vd_aux)); | |
8593 | iverdaux = iverdef->vd_auxptr; | |
8594 | for (j = 0; j < iverdef->vd_cnt; j++, iverdaux++) | |
8595 | { | |
8596 | _bfd_elf_swap_verdaux_in (abfd, everdaux, iverdaux); | |
8597 | ||
8598 | iverdaux->vda_nodename = | |
8599 | bfd_elf_string_from_elf_section (abfd, hdr->sh_link, | |
8600 | iverdaux->vda_name); | |
8601 | if (iverdaux->vda_nodename == NULL) | |
601a03ba | 8602 | goto error_return_bad_verdef; |
252b5132 | 8603 | |
25ff461f AM |
8604 | iverdaux->vda_nextptr = NULL; |
8605 | if (iverdaux->vda_next == 0) | |
8606 | { | |
8607 | iverdef->vd_cnt = j + 1; | |
8608 | break; | |
8609 | } | |
252b5132 RH |
8610 | if (j + 1 < iverdef->vd_cnt) |
8611 | iverdaux->vda_nextptr = iverdaux + 1; | |
252b5132 | 8612 | |
d0fb9a8d JJ |
8613 | if (iverdaux->vda_next |
8614 | > (size_t) (contents_end_aux - (bfd_byte *) everdaux)) | |
601a03ba | 8615 | goto error_return_bad_verdef; |
d0fb9a8d | 8616 | |
252b5132 RH |
8617 | everdaux = ((Elf_External_Verdaux *) |
8618 | ((bfd_byte *) everdaux + iverdaux->vda_next)); | |
8619 | } | |
8620 | ||
595bce75 | 8621 | iverdef->vd_nodename = NULL; |
d0fb9a8d JJ |
8622 | if (iverdef->vd_cnt) |
8623 | iverdef->vd_nodename = iverdef->vd_auxptr->vda_nodename; | |
252b5132 | 8624 | |
25ff461f AM |
8625 | iverdef->vd_nextdef = NULL; |
8626 | if (iverdef->vd_next == 0) | |
8627 | break; | |
d0fb9a8d | 8628 | if ((size_t) (iverdef - iverdefarr) + 1 < maxidx) |
252b5132 | 8629 | iverdef->vd_nextdef = iverdef + 1; |
252b5132 RH |
8630 | |
8631 | everdef = ((Elf_External_Verdef *) | |
8632 | ((bfd_byte *) everdef + iverdef->vd_next)); | |
8633 | } | |
8634 | ||
8635 | free (contents); | |
8636 | contents = NULL; | |
8637 | } | |
fc0e6df6 | 8638 | else if (default_imported_symver) |
252b5132 | 8639 | { |
fc0e6df6 PB |
8640 | if (freeidx < 3) |
8641 | freeidx = 3; | |
8642 | else | |
8643 | freeidx++; | |
252b5132 | 8644 | |
a50b1753 | 8645 | elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *) |
07d6d2b8 | 8646 | bfd_zalloc2 (abfd, freeidx, sizeof (Elf_Internal_Verdef)); |
fc0e6df6 | 8647 | if (elf_tdata (abfd)->verdef == NULL) |
252b5132 RH |
8648 | goto error_return; |
8649 | ||
fc0e6df6 PB |
8650 | elf_tdata (abfd)->cverdefs = freeidx; |
8651 | } | |
252b5132 | 8652 | |
fc0e6df6 PB |
8653 | /* Create a default version based on the soname. */ |
8654 | if (default_imported_symver) | |
8655 | { | |
8656 | Elf_Internal_Verdef *iverdef; | |
8657 | Elf_Internal_Verdaux *iverdaux; | |
252b5132 | 8658 | |
5bb3703f | 8659 | iverdef = &elf_tdata (abfd)->verdef[freeidx - 1]; |
252b5132 | 8660 | |
fc0e6df6 PB |
8661 | iverdef->vd_version = VER_DEF_CURRENT; |
8662 | iverdef->vd_flags = 0; | |
8663 | iverdef->vd_ndx = freeidx; | |
8664 | iverdef->vd_cnt = 1; | |
252b5132 | 8665 | |
fc0e6df6 | 8666 | iverdef->vd_bfd = abfd; |
252b5132 | 8667 | |
fc0e6df6 PB |
8668 | iverdef->vd_nodename = bfd_elf_get_dt_soname (abfd); |
8669 | if (iverdef->vd_nodename == NULL) | |
d0fb9a8d | 8670 | goto error_return_verdef; |
fc0e6df6 | 8671 | iverdef->vd_nextdef = NULL; |
601a03ba AM |
8672 | iverdef->vd_auxptr = ((struct elf_internal_verdaux *) |
8673 | bfd_zalloc (abfd, sizeof (Elf_Internal_Verdaux))); | |
d0fb9a8d JJ |
8674 | if (iverdef->vd_auxptr == NULL) |
8675 | goto error_return_verdef; | |
252b5132 | 8676 | |
fc0e6df6 PB |
8677 | iverdaux = iverdef->vd_auxptr; |
8678 | iverdaux->vda_nodename = iverdef->vd_nodename; | |
252b5132 RH |
8679 | } |
8680 | ||
b34976b6 | 8681 | return TRUE; |
252b5132 RH |
8682 | |
8683 | error_return: | |
5ed6aba4 | 8684 | if (contents != NULL) |
252b5132 | 8685 | free (contents); |
b34976b6 | 8686 | return FALSE; |
252b5132 RH |
8687 | } |
8688 | \f | |
8689 | asymbol * | |
217aa764 | 8690 | _bfd_elf_make_empty_symbol (bfd *abfd) |
252b5132 RH |
8691 | { |
8692 | elf_symbol_type *newsym; | |
8693 | ||
201159ec | 8694 | newsym = (elf_symbol_type *) bfd_zalloc (abfd, sizeof * newsym); |
252b5132 RH |
8695 | if (!newsym) |
8696 | return NULL; | |
201159ec NC |
8697 | newsym->symbol.the_bfd = abfd; |
8698 | return &newsym->symbol; | |
252b5132 RH |
8699 | } |
8700 | ||
8701 | void | |
217aa764 AM |
8702 | _bfd_elf_get_symbol_info (bfd *abfd ATTRIBUTE_UNUSED, |
8703 | asymbol *symbol, | |
8704 | symbol_info *ret) | |
252b5132 RH |
8705 | { |
8706 | bfd_symbol_info (symbol, ret); | |
8707 | } | |
8708 | ||
8709 | /* Return whether a symbol name implies a local symbol. Most targets | |
8710 | use this function for the is_local_label_name entry point, but some | |
8711 | override it. */ | |
8712 | ||
b34976b6 | 8713 | bfd_boolean |
217aa764 AM |
8714 | _bfd_elf_is_local_label_name (bfd *abfd ATTRIBUTE_UNUSED, |
8715 | const char *name) | |
252b5132 RH |
8716 | { |
8717 | /* Normal local symbols start with ``.L''. */ | |
8718 | if (name[0] == '.' && name[1] == 'L') | |
b34976b6 | 8719 | return TRUE; |
252b5132 RH |
8720 | |
8721 | /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate | |
8722 | DWARF debugging symbols starting with ``..''. */ | |
8723 | if (name[0] == '.' && name[1] == '.') | |
b34976b6 | 8724 | return TRUE; |
252b5132 RH |
8725 | |
8726 | /* gcc will sometimes generate symbols beginning with ``_.L_'' when | |
8727 | emitting DWARF debugging output. I suspect this is actually a | |
8728 | small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call | |
8729 | ASM_GENERATE_INTERNAL_LABEL, and this causes the leading | |
8730 | underscore to be emitted on some ELF targets). For ease of use, | |
8731 | we treat such symbols as local. */ | |
8732 | if (name[0] == '_' && name[1] == '.' && name[2] == 'L' && name[3] == '_') | |
b34976b6 | 8733 | return TRUE; |
252b5132 | 8734 | |
b1fa9dd6 NC |
8735 | /* Treat assembler generated fake symbols, dollar local labels and |
8736 | forward-backward labels (aka local labels) as locals. | |
8737 | These labels have the form: | |
8738 | ||
07d6d2b8 | 8739 | L0^A.* (fake symbols) |
b1fa9dd6 NC |
8740 | |
8741 | [.]?L[0123456789]+{^A|^B}[0123456789]* (local labels) | |
8742 | ||
8743 | Versions which start with .L will have already been matched above, | |
8744 | so we only need to match the rest. */ | |
8745 | if (name[0] == 'L' && ISDIGIT (name[1])) | |
8746 | { | |
8747 | bfd_boolean ret = FALSE; | |
8748 | const char * p; | |
8749 | char c; | |
8750 | ||
8751 | for (p = name + 2; (c = *p); p++) | |
8752 | { | |
8753 | if (c == 1 || c == 2) | |
8754 | { | |
8755 | if (c == 1 && p == name + 2) | |
8756 | /* A fake symbol. */ | |
8757 | return TRUE; | |
8758 | ||
8759 | /* FIXME: We are being paranoid here and treating symbols like | |
8760 | L0^Bfoo as if there were non-local, on the grounds that the | |
8761 | assembler will never generate them. But can any symbol | |
8762 | containing an ASCII value in the range 1-31 ever be anything | |
8763 | other than some kind of local ? */ | |
8764 | ret = TRUE; | |
8765 | } | |
8766 | ||
8767 | if (! ISDIGIT (c)) | |
8768 | { | |
8769 | ret = FALSE; | |
8770 | break; | |
8771 | } | |
8772 | } | |
8773 | return ret; | |
8774 | } | |
ffa54770 | 8775 | |
b34976b6 | 8776 | return FALSE; |
252b5132 RH |
8777 | } |
8778 | ||
8779 | alent * | |
217aa764 AM |
8780 | _bfd_elf_get_lineno (bfd *abfd ATTRIBUTE_UNUSED, |
8781 | asymbol *symbol ATTRIBUTE_UNUSED) | |
252b5132 RH |
8782 | { |
8783 | abort (); | |
8784 | return NULL; | |
8785 | } | |
8786 | ||
b34976b6 | 8787 | bfd_boolean |
217aa764 AM |
8788 | _bfd_elf_set_arch_mach (bfd *abfd, |
8789 | enum bfd_architecture arch, | |
8790 | unsigned long machine) | |
252b5132 RH |
8791 | { |
8792 | /* If this isn't the right architecture for this backend, and this | |
8793 | isn't the generic backend, fail. */ | |
8794 | if (arch != get_elf_backend_data (abfd)->arch | |
8795 | && arch != bfd_arch_unknown | |
8796 | && get_elf_backend_data (abfd)->arch != bfd_arch_unknown) | |
b34976b6 | 8797 | return FALSE; |
252b5132 RH |
8798 | |
8799 | return bfd_default_set_arch_mach (abfd, arch, machine); | |
8800 | } | |
8801 | ||
d1fad7c6 NC |
8802 | /* Find the nearest line to a particular section and offset, |
8803 | for error reporting. */ | |
8804 | ||
b34976b6 | 8805 | bfd_boolean |
217aa764 | 8806 | _bfd_elf_find_nearest_line (bfd *abfd, |
217aa764 | 8807 | asymbol **symbols, |
fb167eb2 | 8808 | asection *section, |
217aa764 AM |
8809 | bfd_vma offset, |
8810 | const char **filename_ptr, | |
8811 | const char **functionname_ptr, | |
fb167eb2 AM |
8812 | unsigned int *line_ptr, |
8813 | unsigned int *discriminator_ptr) | |
d1fad7c6 | 8814 | { |
b34976b6 | 8815 | bfd_boolean found; |
d1fad7c6 | 8816 | |
fb167eb2 | 8817 | if (_bfd_dwarf2_find_nearest_line (abfd, symbols, NULL, section, offset, |
4e8a9624 | 8818 | filename_ptr, functionname_ptr, |
fb167eb2 AM |
8819 | line_ptr, discriminator_ptr, |
8820 | dwarf_debug_sections, 0, | |
e00e8198 AM |
8821 | &elf_tdata (abfd)->dwarf2_find_line_info) |
8822 | || _bfd_dwarf1_find_nearest_line (abfd, symbols, section, offset, | |
8823 | filename_ptr, functionname_ptr, | |
8824 | line_ptr)) | |
d1fad7c6 NC |
8825 | { |
8826 | if (!*functionname_ptr) | |
e00e8198 AM |
8827 | _bfd_elf_find_function (abfd, symbols, section, offset, |
8828 | *filename_ptr ? NULL : filename_ptr, | |
8829 | functionname_ptr); | |
b34976b6 | 8830 | return TRUE; |
d1fad7c6 NC |
8831 | } |
8832 | ||
8833 | if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset, | |
4e8a9624 AM |
8834 | &found, filename_ptr, |
8835 | functionname_ptr, line_ptr, | |
8836 | &elf_tdata (abfd)->line_info)) | |
b34976b6 | 8837 | return FALSE; |
dc43ada5 | 8838 | if (found && (*functionname_ptr || *line_ptr)) |
b34976b6 | 8839 | return TRUE; |
d1fad7c6 NC |
8840 | |
8841 | if (symbols == NULL) | |
b34976b6 | 8842 | return FALSE; |
d1fad7c6 | 8843 | |
e00e8198 AM |
8844 | if (! _bfd_elf_find_function (abfd, symbols, section, offset, |
8845 | filename_ptr, functionname_ptr)) | |
b34976b6 | 8846 | return FALSE; |
d1fad7c6 | 8847 | |
252b5132 | 8848 | *line_ptr = 0; |
b34976b6 | 8849 | return TRUE; |
252b5132 RH |
8850 | } |
8851 | ||
5420f73d L |
8852 | /* Find the line for a symbol. */ |
8853 | ||
8854 | bfd_boolean | |
8855 | _bfd_elf_find_line (bfd *abfd, asymbol **symbols, asymbol *symbol, | |
8856 | const char **filename_ptr, unsigned int *line_ptr) | |
9b8d1a36 | 8857 | { |
fb167eb2 AM |
8858 | return _bfd_dwarf2_find_nearest_line (abfd, symbols, symbol, NULL, 0, |
8859 | filename_ptr, NULL, line_ptr, NULL, | |
8860 | dwarf_debug_sections, 0, | |
8861 | &elf_tdata (abfd)->dwarf2_find_line_info); | |
5420f73d L |
8862 | } |
8863 | ||
4ab527b0 FF |
8864 | /* After a call to bfd_find_nearest_line, successive calls to |
8865 | bfd_find_inliner_info can be used to get source information about | |
8866 | each level of function inlining that terminated at the address | |
8867 | passed to bfd_find_nearest_line. Currently this is only supported | |
8868 | for DWARF2 with appropriate DWARF3 extensions. */ | |
8869 | ||
8870 | bfd_boolean | |
8871 | _bfd_elf_find_inliner_info (bfd *abfd, | |
8872 | const char **filename_ptr, | |
8873 | const char **functionname_ptr, | |
8874 | unsigned int *line_ptr) | |
8875 | { | |
8876 | bfd_boolean found; | |
8877 | found = _bfd_dwarf2_find_inliner_info (abfd, filename_ptr, | |
8878 | functionname_ptr, line_ptr, | |
8879 | & elf_tdata (abfd)->dwarf2_find_line_info); | |
8880 | return found; | |
8881 | } | |
8882 | ||
252b5132 | 8883 | int |
a6b96beb | 8884 | _bfd_elf_sizeof_headers (bfd *abfd, struct bfd_link_info *info) |
252b5132 | 8885 | { |
8ded5a0f AM |
8886 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
8887 | int ret = bed->s->sizeof_ehdr; | |
252b5132 | 8888 | |
0e1862bb | 8889 | if (!bfd_link_relocatable (info)) |
8ded5a0f | 8890 | { |
12bd6957 | 8891 | bfd_size_type phdr_size = elf_program_header_size (abfd); |
8ded5a0f | 8892 | |
62d7a5f6 AM |
8893 | if (phdr_size == (bfd_size_type) -1) |
8894 | { | |
8895 | struct elf_segment_map *m; | |
8896 | ||
8897 | phdr_size = 0; | |
12bd6957 | 8898 | for (m = elf_seg_map (abfd); m != NULL; m = m->next) |
62d7a5f6 | 8899 | phdr_size += bed->s->sizeof_phdr; |
8ded5a0f | 8900 | |
62d7a5f6 AM |
8901 | if (phdr_size == 0) |
8902 | phdr_size = get_program_header_size (abfd, info); | |
8903 | } | |
8ded5a0f | 8904 | |
12bd6957 | 8905 | elf_program_header_size (abfd) = phdr_size; |
8ded5a0f AM |
8906 | ret += phdr_size; |
8907 | } | |
8908 | ||
252b5132 RH |
8909 | return ret; |
8910 | } | |
8911 | ||
b34976b6 | 8912 | bfd_boolean |
217aa764 AM |
8913 | _bfd_elf_set_section_contents (bfd *abfd, |
8914 | sec_ptr section, | |
0f867abe | 8915 | const void *location, |
217aa764 AM |
8916 | file_ptr offset, |
8917 | bfd_size_type count) | |
252b5132 RH |
8918 | { |
8919 | Elf_Internal_Shdr *hdr; | |
1b6aeedb | 8920 | file_ptr pos; |
252b5132 RH |
8921 | |
8922 | if (! abfd->output_has_begun | |
217aa764 | 8923 | && ! _bfd_elf_compute_section_file_positions (abfd, NULL)) |
b34976b6 | 8924 | return FALSE; |
252b5132 | 8925 | |
0ce398f1 L |
8926 | if (!count) |
8927 | return TRUE; | |
8928 | ||
252b5132 | 8929 | hdr = &elf_section_data (section)->this_hdr; |
0ce398f1 L |
8930 | if (hdr->sh_offset == (file_ptr) -1) |
8931 | { | |
8932 | /* We must compress this section. Write output to the buffer. */ | |
8933 | unsigned char *contents = hdr->contents; | |
8934 | if ((offset + count) > hdr->sh_size | |
8935 | || (section->flags & SEC_ELF_COMPRESS) == 0 | |
8936 | || contents == NULL) | |
8937 | abort (); | |
8938 | memcpy (contents + offset, location, count); | |
8939 | return TRUE; | |
8940 | } | |
dc810e39 AM |
8941 | pos = hdr->sh_offset + offset; |
8942 | if (bfd_seek (abfd, pos, SEEK_SET) != 0 | |
8943 | || bfd_bwrite (location, count, abfd) != count) | |
b34976b6 | 8944 | return FALSE; |
252b5132 | 8945 | |
b34976b6 | 8946 | return TRUE; |
252b5132 RH |
8947 | } |
8948 | ||
f3185997 | 8949 | bfd_boolean |
217aa764 AM |
8950 | _bfd_elf_no_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED, |
8951 | arelent *cache_ptr ATTRIBUTE_UNUSED, | |
8952 | Elf_Internal_Rela *dst ATTRIBUTE_UNUSED) | |
252b5132 RH |
8953 | { |
8954 | abort (); | |
f3185997 | 8955 | return FALSE; |
252b5132 RH |
8956 | } |
8957 | ||
252b5132 RH |
8958 | /* Try to convert a non-ELF reloc into an ELF one. */ |
8959 | ||
b34976b6 | 8960 | bfd_boolean |
217aa764 | 8961 | _bfd_elf_validate_reloc (bfd *abfd, arelent *areloc) |
252b5132 | 8962 | { |
c044fabd | 8963 | /* Check whether we really have an ELF howto. */ |
252b5132 RH |
8964 | |
8965 | if ((*areloc->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec) | |
8966 | { | |
8967 | bfd_reloc_code_real_type code; | |
8968 | reloc_howto_type *howto; | |
8969 | ||
8970 | /* Alien reloc: Try to determine its type to replace it with an | |
c044fabd | 8971 | equivalent ELF reloc. */ |
252b5132 RH |
8972 | |
8973 | if (areloc->howto->pc_relative) | |
8974 | { | |
8975 | switch (areloc->howto->bitsize) | |
8976 | { | |
8977 | case 8: | |
8978 | code = BFD_RELOC_8_PCREL; | |
8979 | break; | |
8980 | case 12: | |
8981 | code = BFD_RELOC_12_PCREL; | |
8982 | break; | |
8983 | case 16: | |
8984 | code = BFD_RELOC_16_PCREL; | |
8985 | break; | |
8986 | case 24: | |
8987 | code = BFD_RELOC_24_PCREL; | |
8988 | break; | |
8989 | case 32: | |
8990 | code = BFD_RELOC_32_PCREL; | |
8991 | break; | |
8992 | case 64: | |
8993 | code = BFD_RELOC_64_PCREL; | |
8994 | break; | |
8995 | default: | |
8996 | goto fail; | |
8997 | } | |
8998 | ||
8999 | howto = bfd_reloc_type_lookup (abfd, code); | |
9000 | ||
9001 | if (areloc->howto->pcrel_offset != howto->pcrel_offset) | |
9002 | { | |
9003 | if (howto->pcrel_offset) | |
9004 | areloc->addend += areloc->address; | |
9005 | else | |
9006 | areloc->addend -= areloc->address; /* addend is unsigned!! */ | |
9007 | } | |
9008 | } | |
9009 | else | |
9010 | { | |
9011 | switch (areloc->howto->bitsize) | |
9012 | { | |
9013 | case 8: | |
9014 | code = BFD_RELOC_8; | |
9015 | break; | |
9016 | case 14: | |
9017 | code = BFD_RELOC_14; | |
9018 | break; | |
9019 | case 16: | |
9020 | code = BFD_RELOC_16; | |
9021 | break; | |
9022 | case 26: | |
9023 | code = BFD_RELOC_26; | |
9024 | break; | |
9025 | case 32: | |
9026 | code = BFD_RELOC_32; | |
9027 | break; | |
9028 | case 64: | |
9029 | code = BFD_RELOC_64; | |
9030 | break; | |
9031 | default: | |
9032 | goto fail; | |
9033 | } | |
9034 | ||
9035 | howto = bfd_reloc_type_lookup (abfd, code); | |
9036 | } | |
9037 | ||
9038 | if (howto) | |
9039 | areloc->howto = howto; | |
9040 | else | |
9041 | goto fail; | |
9042 | } | |
9043 | ||
b34976b6 | 9044 | return TRUE; |
252b5132 RH |
9045 | |
9046 | fail: | |
0aa13fee AM |
9047 | /* xgettext:c-format */ |
9048 | _bfd_error_handler (_("%pB: %s unsupported"), | |
9049 | abfd, areloc->howto->name); | |
252b5132 | 9050 | bfd_set_error (bfd_error_bad_value); |
b34976b6 | 9051 | return FALSE; |
252b5132 RH |
9052 | } |
9053 | ||
b34976b6 | 9054 | bfd_boolean |
217aa764 | 9055 | _bfd_elf_close_and_cleanup (bfd *abfd) |
252b5132 | 9056 | { |
d9071b0c TG |
9057 | struct elf_obj_tdata *tdata = elf_tdata (abfd); |
9058 | if (bfd_get_format (abfd) == bfd_object && tdata != NULL) | |
252b5132 | 9059 | { |
c0355132 | 9060 | if (elf_tdata (abfd)->o != NULL && elf_shstrtab (abfd) != NULL) |
2b0f7ef9 | 9061 | _bfd_elf_strtab_free (elf_shstrtab (abfd)); |
d9071b0c | 9062 | _bfd_dwarf2_cleanup_debug_info (abfd, &tdata->dwarf2_find_line_info); |
252b5132 RH |
9063 | } |
9064 | ||
9065 | return _bfd_generic_close_and_cleanup (abfd); | |
9066 | } | |
9067 | ||
9068 | /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY | |
9069 | in the relocation's offset. Thus we cannot allow any sort of sanity | |
9070 | range-checking to interfere. There is nothing else to do in processing | |
9071 | this reloc. */ | |
9072 | ||
9073 | bfd_reloc_status_type | |
217aa764 AM |
9074 | _bfd_elf_rel_vtable_reloc_fn |
9075 | (bfd *abfd ATTRIBUTE_UNUSED, arelent *re ATTRIBUTE_UNUSED, | |
fc0a2244 | 9076 | struct bfd_symbol *symbol ATTRIBUTE_UNUSED, |
217aa764 AM |
9077 | void *data ATTRIBUTE_UNUSED, asection *is ATTRIBUTE_UNUSED, |
9078 | bfd *obfd ATTRIBUTE_UNUSED, char **errmsg ATTRIBUTE_UNUSED) | |
252b5132 RH |
9079 | { |
9080 | return bfd_reloc_ok; | |
9081 | } | |
252b5132 RH |
9082 | \f |
9083 | /* Elf core file support. Much of this only works on native | |
9084 | toolchains, since we rely on knowing the | |
9085 | machine-dependent procfs structure in order to pick | |
c044fabd | 9086 | out details about the corefile. */ |
252b5132 RH |
9087 | |
9088 | #ifdef HAVE_SYS_PROCFS_H | |
16231b7b DG |
9089 | /* Needed for new procfs interface on sparc-solaris. */ |
9090 | # define _STRUCTURED_PROC 1 | |
252b5132 RH |
9091 | # include <sys/procfs.h> |
9092 | #endif | |
9093 | ||
261b8d08 PA |
9094 | /* Return a PID that identifies a "thread" for threaded cores, or the |
9095 | PID of the main process for non-threaded cores. */ | |
252b5132 RH |
9096 | |
9097 | static int | |
217aa764 | 9098 | elfcore_make_pid (bfd *abfd) |
252b5132 | 9099 | { |
261b8d08 PA |
9100 | int pid; |
9101 | ||
228e534f | 9102 | pid = elf_tdata (abfd)->core->lwpid; |
261b8d08 | 9103 | if (pid == 0) |
228e534f | 9104 | pid = elf_tdata (abfd)->core->pid; |
261b8d08 PA |
9105 | |
9106 | return pid; | |
252b5132 RH |
9107 | } |
9108 | ||
252b5132 RH |
9109 | /* If there isn't a section called NAME, make one, using |
9110 | data from SECT. Note, this function will generate a | |
9111 | reference to NAME, so you shouldn't deallocate or | |
c044fabd | 9112 | overwrite it. */ |
252b5132 | 9113 | |
b34976b6 | 9114 | static bfd_boolean |
217aa764 | 9115 | elfcore_maybe_make_sect (bfd *abfd, char *name, asection *sect) |
252b5132 | 9116 | { |
c044fabd | 9117 | asection *sect2; |
252b5132 RH |
9118 | |
9119 | if (bfd_get_section_by_name (abfd, name) != NULL) | |
b34976b6 | 9120 | return TRUE; |
252b5132 | 9121 | |
117ed4f8 | 9122 | sect2 = bfd_make_section_with_flags (abfd, name, sect->flags); |
252b5132 | 9123 | if (sect2 == NULL) |
b34976b6 | 9124 | return FALSE; |
252b5132 | 9125 | |
eea6121a | 9126 | sect2->size = sect->size; |
252b5132 | 9127 | sect2->filepos = sect->filepos; |
252b5132 | 9128 | sect2->alignment_power = sect->alignment_power; |
b34976b6 | 9129 | return TRUE; |
252b5132 RH |
9130 | } |
9131 | ||
bb0082d6 AM |
9132 | /* Create a pseudosection containing SIZE bytes at FILEPOS. This |
9133 | actually creates up to two pseudosections: | |
9134 | - For the single-threaded case, a section named NAME, unless | |
9135 | such a section already exists. | |
9136 | - For the multi-threaded case, a section named "NAME/PID", where | |
9137 | PID is elfcore_make_pid (abfd). | |
24d3e51b | 9138 | Both pseudosections have identical contents. */ |
b34976b6 | 9139 | bfd_boolean |
217aa764 AM |
9140 | _bfd_elfcore_make_pseudosection (bfd *abfd, |
9141 | char *name, | |
9142 | size_t size, | |
9143 | ufile_ptr filepos) | |
bb0082d6 AM |
9144 | { |
9145 | char buf[100]; | |
9146 | char *threaded_name; | |
d4c88bbb | 9147 | size_t len; |
bb0082d6 AM |
9148 | asection *sect; |
9149 | ||
9150 | /* Build the section name. */ | |
9151 | ||
9152 | sprintf (buf, "%s/%d", name, elfcore_make_pid (abfd)); | |
d4c88bbb | 9153 | len = strlen (buf) + 1; |
a50b1753 | 9154 | threaded_name = (char *) bfd_alloc (abfd, len); |
bb0082d6 | 9155 | if (threaded_name == NULL) |
b34976b6 | 9156 | return FALSE; |
d4c88bbb | 9157 | memcpy (threaded_name, buf, len); |
bb0082d6 | 9158 | |
117ed4f8 AM |
9159 | sect = bfd_make_section_anyway_with_flags (abfd, threaded_name, |
9160 | SEC_HAS_CONTENTS); | |
bb0082d6 | 9161 | if (sect == NULL) |
b34976b6 | 9162 | return FALSE; |
eea6121a | 9163 | sect->size = size; |
bb0082d6 | 9164 | sect->filepos = filepos; |
bb0082d6 AM |
9165 | sect->alignment_power = 2; |
9166 | ||
936e320b | 9167 | return elfcore_maybe_make_sect (abfd, name, sect); |
bb0082d6 AM |
9168 | } |
9169 | ||
252b5132 | 9170 | /* prstatus_t exists on: |
4a938328 | 9171 | solaris 2.5+ |
252b5132 RH |
9172 | linux 2.[01] + glibc |
9173 | unixware 4.2 | |
9174 | */ | |
9175 | ||
9176 | #if defined (HAVE_PRSTATUS_T) | |
a7b97311 | 9177 | |
b34976b6 | 9178 | static bfd_boolean |
217aa764 | 9179 | elfcore_grok_prstatus (bfd *abfd, Elf_Internal_Note *note) |
252b5132 | 9180 | { |
eea6121a | 9181 | size_t size; |
7ee38065 | 9182 | int offset; |
252b5132 | 9183 | |
4a938328 MS |
9184 | if (note->descsz == sizeof (prstatus_t)) |
9185 | { | |
9186 | prstatus_t prstat; | |
252b5132 | 9187 | |
eea6121a | 9188 | size = sizeof (prstat.pr_reg); |
7ee38065 | 9189 | offset = offsetof (prstatus_t, pr_reg); |
4a938328 | 9190 | memcpy (&prstat, note->descdata, sizeof (prstat)); |
252b5132 | 9191 | |
fa49d224 NC |
9192 | /* Do not overwrite the core signal if it |
9193 | has already been set by another thread. */ | |
228e534f AM |
9194 | if (elf_tdata (abfd)->core->signal == 0) |
9195 | elf_tdata (abfd)->core->signal = prstat.pr_cursig; | |
9196 | if (elf_tdata (abfd)->core->pid == 0) | |
9197 | elf_tdata (abfd)->core->pid = prstat.pr_pid; | |
252b5132 | 9198 | |
4a938328 MS |
9199 | /* pr_who exists on: |
9200 | solaris 2.5+ | |
9201 | unixware 4.2 | |
9202 | pr_who doesn't exist on: | |
9203 | linux 2.[01] | |
9204 | */ | |
252b5132 | 9205 | #if defined (HAVE_PRSTATUS_T_PR_WHO) |
228e534f | 9206 | elf_tdata (abfd)->core->lwpid = prstat.pr_who; |
261b8d08 | 9207 | #else |
228e534f | 9208 | elf_tdata (abfd)->core->lwpid = prstat.pr_pid; |
252b5132 | 9209 | #endif |
4a938328 | 9210 | } |
7ee38065 | 9211 | #if defined (HAVE_PRSTATUS32_T) |
4a938328 MS |
9212 | else if (note->descsz == sizeof (prstatus32_t)) |
9213 | { | |
9214 | /* 64-bit host, 32-bit corefile */ | |
9215 | prstatus32_t prstat; | |
9216 | ||
eea6121a | 9217 | size = sizeof (prstat.pr_reg); |
7ee38065 | 9218 | offset = offsetof (prstatus32_t, pr_reg); |
4a938328 MS |
9219 | memcpy (&prstat, note->descdata, sizeof (prstat)); |
9220 | ||
fa49d224 NC |
9221 | /* Do not overwrite the core signal if it |
9222 | has already been set by another thread. */ | |
228e534f AM |
9223 | if (elf_tdata (abfd)->core->signal == 0) |
9224 | elf_tdata (abfd)->core->signal = prstat.pr_cursig; | |
9225 | if (elf_tdata (abfd)->core->pid == 0) | |
9226 | elf_tdata (abfd)->core->pid = prstat.pr_pid; | |
4a938328 MS |
9227 | |
9228 | /* pr_who exists on: | |
9229 | solaris 2.5+ | |
9230 | unixware 4.2 | |
9231 | pr_who doesn't exist on: | |
9232 | linux 2.[01] | |
9233 | */ | |
7ee38065 | 9234 | #if defined (HAVE_PRSTATUS32_T_PR_WHO) |
228e534f | 9235 | elf_tdata (abfd)->core->lwpid = prstat.pr_who; |
261b8d08 | 9236 | #else |
228e534f | 9237 | elf_tdata (abfd)->core->lwpid = prstat.pr_pid; |
4a938328 MS |
9238 | #endif |
9239 | } | |
7ee38065 | 9240 | #endif /* HAVE_PRSTATUS32_T */ |
4a938328 MS |
9241 | else |
9242 | { | |
9243 | /* Fail - we don't know how to handle any other | |
9244 | note size (ie. data object type). */ | |
b34976b6 | 9245 | return TRUE; |
4a938328 | 9246 | } |
252b5132 | 9247 | |
bb0082d6 | 9248 | /* Make a ".reg/999" section and a ".reg" section. */ |
936e320b | 9249 | return _bfd_elfcore_make_pseudosection (abfd, ".reg", |
eea6121a | 9250 | size, note->descpos + offset); |
252b5132 RH |
9251 | } |
9252 | #endif /* defined (HAVE_PRSTATUS_T) */ | |
9253 | ||
bb0082d6 | 9254 | /* Create a pseudosection containing the exact contents of NOTE. */ |
b34976b6 | 9255 | static bfd_boolean |
217aa764 AM |
9256 | elfcore_make_note_pseudosection (bfd *abfd, |
9257 | char *name, | |
9258 | Elf_Internal_Note *note) | |
252b5132 | 9259 | { |
936e320b AM |
9260 | return _bfd_elfcore_make_pseudosection (abfd, name, |
9261 | note->descsz, note->descpos); | |
252b5132 RH |
9262 | } |
9263 | ||
ff08c6bb JB |
9264 | /* There isn't a consistent prfpregset_t across platforms, |
9265 | but it doesn't matter, because we don't have to pick this | |
c044fabd KH |
9266 | data structure apart. */ |
9267 | ||
b34976b6 | 9268 | static bfd_boolean |
217aa764 | 9269 | elfcore_grok_prfpreg (bfd *abfd, Elf_Internal_Note *note) |
ff08c6bb JB |
9270 | { |
9271 | return elfcore_make_note_pseudosection (abfd, ".reg2", note); | |
9272 | } | |
9273 | ||
ff08c6bb | 9274 | /* Linux dumps the Intel SSE regs in a note named "LINUX" with a note |
971d4640 | 9275 | type of NT_PRXFPREG. Just include the whole note's contents |
ff08c6bb | 9276 | literally. */ |
c044fabd | 9277 | |
b34976b6 | 9278 | static bfd_boolean |
217aa764 | 9279 | elfcore_grok_prxfpreg (bfd *abfd, Elf_Internal_Note *note) |
ff08c6bb JB |
9280 | { |
9281 | return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note); | |
9282 | } | |
9283 | ||
4339cae0 L |
9284 | /* Linux dumps the Intel XSAVE extended state in a note named "LINUX" |
9285 | with a note type of NT_X86_XSTATE. Just include the whole note's | |
9286 | contents literally. */ | |
9287 | ||
9288 | static bfd_boolean | |
9289 | elfcore_grok_xstatereg (bfd *abfd, Elf_Internal_Note *note) | |
9290 | { | |
9291 | return elfcore_make_note_pseudosection (abfd, ".reg-xstate", note); | |
9292 | } | |
9293 | ||
97753bd5 AM |
9294 | static bfd_boolean |
9295 | elfcore_grok_ppc_vmx (bfd *abfd, Elf_Internal_Note *note) | |
9296 | { | |
9297 | return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vmx", note); | |
9298 | } | |
9299 | ||
89eeb0bc LM |
9300 | static bfd_boolean |
9301 | elfcore_grok_ppc_vsx (bfd *abfd, Elf_Internal_Note *note) | |
9302 | { | |
9303 | return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vsx", note); | |
9304 | } | |
97753bd5 | 9305 | |
cb2366c1 EBM |
9306 | static bfd_boolean |
9307 | elfcore_grok_ppc_tar (bfd *abfd, Elf_Internal_Note *note) | |
9308 | { | |
9309 | return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tar", note); | |
9310 | } | |
9311 | ||
9312 | static bfd_boolean | |
9313 | elfcore_grok_ppc_ppr (bfd *abfd, Elf_Internal_Note *note) | |
9314 | { | |
9315 | return elfcore_make_note_pseudosection (abfd, ".reg-ppc-ppr", note); | |
9316 | } | |
9317 | ||
9318 | static bfd_boolean | |
9319 | elfcore_grok_ppc_dscr (bfd *abfd, Elf_Internal_Note *note) | |
9320 | { | |
9321 | return elfcore_make_note_pseudosection (abfd, ".reg-ppc-dscr", note); | |
9322 | } | |
9323 | ||
9324 | static bfd_boolean | |
9325 | elfcore_grok_ppc_ebb (bfd *abfd, Elf_Internal_Note *note) | |
9326 | { | |
9327 | return elfcore_make_note_pseudosection (abfd, ".reg-ppc-ebb", note); | |
9328 | } | |
9329 | ||
9330 | static bfd_boolean | |
9331 | elfcore_grok_ppc_pmu (bfd *abfd, Elf_Internal_Note *note) | |
9332 | { | |
9333 | return elfcore_make_note_pseudosection (abfd, ".reg-ppc-pmu", note); | |
9334 | } | |
9335 | ||
9336 | static bfd_boolean | |
9337 | elfcore_grok_ppc_tm_cgpr (bfd *abfd, Elf_Internal_Note *note) | |
9338 | { | |
9339 | return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cgpr", note); | |
9340 | } | |
9341 | ||
9342 | static bfd_boolean | |
9343 | elfcore_grok_ppc_tm_cfpr (bfd *abfd, Elf_Internal_Note *note) | |
9344 | { | |
9345 | return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cfpr", note); | |
9346 | } | |
9347 | ||
9348 | static bfd_boolean | |
9349 | elfcore_grok_ppc_tm_cvmx (bfd *abfd, Elf_Internal_Note *note) | |
9350 | { | |
9351 | return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cvmx", note); | |
9352 | } | |
9353 | ||
9354 | static bfd_boolean | |
9355 | elfcore_grok_ppc_tm_cvsx (bfd *abfd, Elf_Internal_Note *note) | |
9356 | { | |
9357 | return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cvsx", note); | |
9358 | } | |
9359 | ||
9360 | static bfd_boolean | |
9361 | elfcore_grok_ppc_tm_spr (bfd *abfd, Elf_Internal_Note *note) | |
9362 | { | |
9363 | return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-spr", note); | |
9364 | } | |
9365 | ||
9366 | static bfd_boolean | |
9367 | elfcore_grok_ppc_tm_ctar (bfd *abfd, Elf_Internal_Note *note) | |
9368 | { | |
9369 | return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-ctar", note); | |
9370 | } | |
9371 | ||
9372 | static bfd_boolean | |
9373 | elfcore_grok_ppc_tm_cppr (bfd *abfd, Elf_Internal_Note *note) | |
9374 | { | |
9375 | return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cppr", note); | |
9376 | } | |
9377 | ||
9378 | static bfd_boolean | |
9379 | elfcore_grok_ppc_tm_cdscr (bfd *abfd, Elf_Internal_Note *note) | |
9380 | { | |
9381 | return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cdscr", note); | |
9382 | } | |
9383 | ||
0675e188 UW |
9384 | static bfd_boolean |
9385 | elfcore_grok_s390_high_gprs (bfd *abfd, Elf_Internal_Note *note) | |
9386 | { | |
9387 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-high-gprs", note); | |
9388 | } | |
9389 | ||
d7eeb400 MS |
9390 | static bfd_boolean |
9391 | elfcore_grok_s390_timer (bfd *abfd, Elf_Internal_Note *note) | |
9392 | { | |
9393 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-timer", note); | |
9394 | } | |
9395 | ||
9396 | static bfd_boolean | |
9397 | elfcore_grok_s390_todcmp (bfd *abfd, Elf_Internal_Note *note) | |
9398 | { | |
9399 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-todcmp", note); | |
9400 | } | |
9401 | ||
9402 | static bfd_boolean | |
9403 | elfcore_grok_s390_todpreg (bfd *abfd, Elf_Internal_Note *note) | |
9404 | { | |
9405 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-todpreg", note); | |
9406 | } | |
9407 | ||
9408 | static bfd_boolean | |
9409 | elfcore_grok_s390_ctrs (bfd *abfd, Elf_Internal_Note *note) | |
9410 | { | |
9411 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-ctrs", note); | |
9412 | } | |
9413 | ||
9414 | static bfd_boolean | |
9415 | elfcore_grok_s390_prefix (bfd *abfd, Elf_Internal_Note *note) | |
9416 | { | |
9417 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-prefix", note); | |
9418 | } | |
9419 | ||
355b81d9 UW |
9420 | static bfd_boolean |
9421 | elfcore_grok_s390_last_break (bfd *abfd, Elf_Internal_Note *note) | |
9422 | { | |
9423 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-last-break", note); | |
9424 | } | |
9425 | ||
9426 | static bfd_boolean | |
9427 | elfcore_grok_s390_system_call (bfd *abfd, Elf_Internal_Note *note) | |
9428 | { | |
9429 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-system-call", note); | |
9430 | } | |
9431 | ||
abb3f6cc NC |
9432 | static bfd_boolean |
9433 | elfcore_grok_s390_tdb (bfd *abfd, Elf_Internal_Note *note) | |
9434 | { | |
9435 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-tdb", note); | |
9436 | } | |
9437 | ||
4ef9f41a AA |
9438 | static bfd_boolean |
9439 | elfcore_grok_s390_vxrs_low (bfd *abfd, Elf_Internal_Note *note) | |
9440 | { | |
9441 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-vxrs-low", note); | |
9442 | } | |
9443 | ||
9444 | static bfd_boolean | |
9445 | elfcore_grok_s390_vxrs_high (bfd *abfd, Elf_Internal_Note *note) | |
9446 | { | |
9447 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-vxrs-high", note); | |
9448 | } | |
9449 | ||
88ab90e8 AA |
9450 | static bfd_boolean |
9451 | elfcore_grok_s390_gs_cb (bfd *abfd, Elf_Internal_Note *note) | |
9452 | { | |
9453 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-gs-cb", note); | |
9454 | } | |
9455 | ||
9456 | static bfd_boolean | |
9457 | elfcore_grok_s390_gs_bc (bfd *abfd, Elf_Internal_Note *note) | |
9458 | { | |
9459 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-gs-bc", note); | |
9460 | } | |
9461 | ||
faa9a424 UW |
9462 | static bfd_boolean |
9463 | elfcore_grok_arm_vfp (bfd *abfd, Elf_Internal_Note *note) | |
9464 | { | |
9465 | return elfcore_make_note_pseudosection (abfd, ".reg-arm-vfp", note); | |
9466 | } | |
9467 | ||
652451f8 YZ |
9468 | static bfd_boolean |
9469 | elfcore_grok_aarch_tls (bfd *abfd, Elf_Internal_Note *note) | |
9470 | { | |
9471 | return elfcore_make_note_pseudosection (abfd, ".reg-aarch-tls", note); | |
9472 | } | |
9473 | ||
9474 | static bfd_boolean | |
9475 | elfcore_grok_aarch_hw_break (bfd *abfd, Elf_Internal_Note *note) | |
9476 | { | |
9477 | return elfcore_make_note_pseudosection (abfd, ".reg-aarch-hw-break", note); | |
9478 | } | |
9479 | ||
9480 | static bfd_boolean | |
9481 | elfcore_grok_aarch_hw_watch (bfd *abfd, Elf_Internal_Note *note) | |
9482 | { | |
9483 | return elfcore_make_note_pseudosection (abfd, ".reg-aarch-hw-watch", note); | |
9484 | } | |
9485 | ||
ad1cc4e4 AH |
9486 | static bfd_boolean |
9487 | elfcore_grok_aarch_sve (bfd *abfd, Elf_Internal_Note *note) | |
9488 | { | |
9489 | return elfcore_make_note_pseudosection (abfd, ".reg-aarch-sve", note); | |
9490 | } | |
9491 | ||
252b5132 | 9492 | #if defined (HAVE_PRPSINFO_T) |
4a938328 | 9493 | typedef prpsinfo_t elfcore_psinfo_t; |
7ee38065 | 9494 | #if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */ |
4a938328 MS |
9495 | typedef prpsinfo32_t elfcore_psinfo32_t; |
9496 | #endif | |
252b5132 RH |
9497 | #endif |
9498 | ||
9499 | #if defined (HAVE_PSINFO_T) | |
4a938328 | 9500 | typedef psinfo_t elfcore_psinfo_t; |
7ee38065 | 9501 | #if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */ |
4a938328 MS |
9502 | typedef psinfo32_t elfcore_psinfo32_t; |
9503 | #endif | |
252b5132 RH |
9504 | #endif |
9505 | ||
252b5132 RH |
9506 | /* return a malloc'ed copy of a string at START which is at |
9507 | most MAX bytes long, possibly without a terminating '\0'. | |
c044fabd | 9508 | the copy will always have a terminating '\0'. */ |
252b5132 | 9509 | |
936e320b | 9510 | char * |
217aa764 | 9511 | _bfd_elfcore_strndup (bfd *abfd, char *start, size_t max) |
252b5132 | 9512 | { |
dc810e39 | 9513 | char *dups; |
a50b1753 | 9514 | char *end = (char *) memchr (start, '\0', max); |
dc810e39 | 9515 | size_t len; |
252b5132 RH |
9516 | |
9517 | if (end == NULL) | |
9518 | len = max; | |
9519 | else | |
9520 | len = end - start; | |
9521 | ||
a50b1753 | 9522 | dups = (char *) bfd_alloc (abfd, len + 1); |
dc810e39 | 9523 | if (dups == NULL) |
252b5132 RH |
9524 | return NULL; |
9525 | ||
dc810e39 AM |
9526 | memcpy (dups, start, len); |
9527 | dups[len] = '\0'; | |
252b5132 | 9528 | |
dc810e39 | 9529 | return dups; |
252b5132 RH |
9530 | } |
9531 | ||
bb0082d6 | 9532 | #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) |
b34976b6 | 9533 | static bfd_boolean |
217aa764 | 9534 | elfcore_grok_psinfo (bfd *abfd, Elf_Internal_Note *note) |
252b5132 | 9535 | { |
4a938328 MS |
9536 | if (note->descsz == sizeof (elfcore_psinfo_t)) |
9537 | { | |
9538 | elfcore_psinfo_t psinfo; | |
252b5132 | 9539 | |
7ee38065 | 9540 | memcpy (&psinfo, note->descdata, sizeof (psinfo)); |
252b5132 | 9541 | |
335e41d4 | 9542 | #if defined (HAVE_PSINFO_T_PR_PID) || defined (HAVE_PRPSINFO_T_PR_PID) |
228e534f | 9543 | elf_tdata (abfd)->core->pid = psinfo.pr_pid; |
335e41d4 | 9544 | #endif |
228e534f | 9545 | elf_tdata (abfd)->core->program |
936e320b AM |
9546 | = _bfd_elfcore_strndup (abfd, psinfo.pr_fname, |
9547 | sizeof (psinfo.pr_fname)); | |
252b5132 | 9548 | |
228e534f | 9549 | elf_tdata (abfd)->core->command |
936e320b AM |
9550 | = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs, |
9551 | sizeof (psinfo.pr_psargs)); | |
4a938328 | 9552 | } |
7ee38065 | 9553 | #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T) |
4a938328 MS |
9554 | else if (note->descsz == sizeof (elfcore_psinfo32_t)) |
9555 | { | |
9556 | /* 64-bit host, 32-bit corefile */ | |
9557 | elfcore_psinfo32_t psinfo; | |
9558 | ||
7ee38065 | 9559 | memcpy (&psinfo, note->descdata, sizeof (psinfo)); |
252b5132 | 9560 | |
335e41d4 | 9561 | #if defined (HAVE_PSINFO32_T_PR_PID) || defined (HAVE_PRPSINFO32_T_PR_PID) |
228e534f | 9562 | elf_tdata (abfd)->core->pid = psinfo.pr_pid; |
335e41d4 | 9563 | #endif |
228e534f | 9564 | elf_tdata (abfd)->core->program |
936e320b AM |
9565 | = _bfd_elfcore_strndup (abfd, psinfo.pr_fname, |
9566 | sizeof (psinfo.pr_fname)); | |
4a938328 | 9567 | |
228e534f | 9568 | elf_tdata (abfd)->core->command |
936e320b AM |
9569 | = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs, |
9570 | sizeof (psinfo.pr_psargs)); | |
4a938328 MS |
9571 | } |
9572 | #endif | |
9573 | ||
9574 | else | |
9575 | { | |
9576 | /* Fail - we don't know how to handle any other | |
9577 | note size (ie. data object type). */ | |
b34976b6 | 9578 | return TRUE; |
4a938328 | 9579 | } |
252b5132 RH |
9580 | |
9581 | /* Note that for some reason, a spurious space is tacked | |
9582 | onto the end of the args in some (at least one anyway) | |
c044fabd | 9583 | implementations, so strip it off if it exists. */ |
252b5132 RH |
9584 | |
9585 | { | |
228e534f | 9586 | char *command = elf_tdata (abfd)->core->command; |
252b5132 RH |
9587 | int n = strlen (command); |
9588 | ||
9589 | if (0 < n && command[n - 1] == ' ') | |
9590 | command[n - 1] = '\0'; | |
9591 | } | |
9592 | ||
b34976b6 | 9593 | return TRUE; |
252b5132 RH |
9594 | } |
9595 | #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */ | |
9596 | ||
252b5132 | 9597 | #if defined (HAVE_PSTATUS_T) |
b34976b6 | 9598 | static bfd_boolean |
217aa764 | 9599 | elfcore_grok_pstatus (bfd *abfd, Elf_Internal_Note *note) |
252b5132 | 9600 | { |
f572a39d AM |
9601 | if (note->descsz == sizeof (pstatus_t) |
9602 | #if defined (HAVE_PXSTATUS_T) | |
9603 | || note->descsz == sizeof (pxstatus_t) | |
9604 | #endif | |
9605 | ) | |
4a938328 MS |
9606 | { |
9607 | pstatus_t pstat; | |
252b5132 | 9608 | |
4a938328 | 9609 | memcpy (&pstat, note->descdata, sizeof (pstat)); |
252b5132 | 9610 | |
228e534f | 9611 | elf_tdata (abfd)->core->pid = pstat.pr_pid; |
4a938328 | 9612 | } |
7ee38065 | 9613 | #if defined (HAVE_PSTATUS32_T) |
4a938328 MS |
9614 | else if (note->descsz == sizeof (pstatus32_t)) |
9615 | { | |
9616 | /* 64-bit host, 32-bit corefile */ | |
9617 | pstatus32_t pstat; | |
252b5132 | 9618 | |
4a938328 | 9619 | memcpy (&pstat, note->descdata, sizeof (pstat)); |
252b5132 | 9620 | |
228e534f | 9621 | elf_tdata (abfd)->core->pid = pstat.pr_pid; |
4a938328 MS |
9622 | } |
9623 | #endif | |
252b5132 RH |
9624 | /* Could grab some more details from the "representative" |
9625 | lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an | |
c044fabd | 9626 | NT_LWPSTATUS note, presumably. */ |
252b5132 | 9627 | |
b34976b6 | 9628 | return TRUE; |
252b5132 RH |
9629 | } |
9630 | #endif /* defined (HAVE_PSTATUS_T) */ | |
9631 | ||
252b5132 | 9632 | #if defined (HAVE_LWPSTATUS_T) |
b34976b6 | 9633 | static bfd_boolean |
217aa764 | 9634 | elfcore_grok_lwpstatus (bfd *abfd, Elf_Internal_Note *note) |
252b5132 RH |
9635 | { |
9636 | lwpstatus_t lwpstat; | |
9637 | char buf[100]; | |
c044fabd | 9638 | char *name; |
d4c88bbb | 9639 | size_t len; |
c044fabd | 9640 | asection *sect; |
252b5132 | 9641 | |
f572a39d AM |
9642 | if (note->descsz != sizeof (lwpstat) |
9643 | #if defined (HAVE_LWPXSTATUS_T) | |
9644 | && note->descsz != sizeof (lwpxstatus_t) | |
9645 | #endif | |
9646 | ) | |
b34976b6 | 9647 | return TRUE; |
252b5132 RH |
9648 | |
9649 | memcpy (&lwpstat, note->descdata, sizeof (lwpstat)); | |
9650 | ||
228e534f | 9651 | elf_tdata (abfd)->core->lwpid = lwpstat.pr_lwpid; |
a1504221 JB |
9652 | /* Do not overwrite the core signal if it has already been set by |
9653 | another thread. */ | |
228e534f AM |
9654 | if (elf_tdata (abfd)->core->signal == 0) |
9655 | elf_tdata (abfd)->core->signal = lwpstat.pr_cursig; | |
252b5132 | 9656 | |
c044fabd | 9657 | /* Make a ".reg/999" section. */ |
252b5132 RH |
9658 | |
9659 | sprintf (buf, ".reg/%d", elfcore_make_pid (abfd)); | |
d4c88bbb | 9660 | len = strlen (buf) + 1; |
217aa764 | 9661 | name = bfd_alloc (abfd, len); |
252b5132 | 9662 | if (name == NULL) |
b34976b6 | 9663 | return FALSE; |
d4c88bbb | 9664 | memcpy (name, buf, len); |
252b5132 | 9665 | |
117ed4f8 | 9666 | sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); |
252b5132 | 9667 | if (sect == NULL) |
b34976b6 | 9668 | return FALSE; |
252b5132 RH |
9669 | |
9670 | #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT) | |
eea6121a | 9671 | sect->size = sizeof (lwpstat.pr_context.uc_mcontext.gregs); |
252b5132 RH |
9672 | sect->filepos = note->descpos |
9673 | + offsetof (lwpstatus_t, pr_context.uc_mcontext.gregs); | |
9674 | #endif | |
9675 | ||
9676 | #if defined (HAVE_LWPSTATUS_T_PR_REG) | |
eea6121a | 9677 | sect->size = sizeof (lwpstat.pr_reg); |
252b5132 RH |
9678 | sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_reg); |
9679 | #endif | |
9680 | ||
252b5132 RH |
9681 | sect->alignment_power = 2; |
9682 | ||
9683 | if (!elfcore_maybe_make_sect (abfd, ".reg", sect)) | |
b34976b6 | 9684 | return FALSE; |
252b5132 RH |
9685 | |
9686 | /* Make a ".reg2/999" section */ | |
9687 | ||
9688 | sprintf (buf, ".reg2/%d", elfcore_make_pid (abfd)); | |
d4c88bbb | 9689 | len = strlen (buf) + 1; |
217aa764 | 9690 | name = bfd_alloc (abfd, len); |
252b5132 | 9691 | if (name == NULL) |
b34976b6 | 9692 | return FALSE; |
d4c88bbb | 9693 | memcpy (name, buf, len); |
252b5132 | 9694 | |
117ed4f8 | 9695 | sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); |
252b5132 | 9696 | if (sect == NULL) |
b34976b6 | 9697 | return FALSE; |
252b5132 RH |
9698 | |
9699 | #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT) | |
eea6121a | 9700 | sect->size = sizeof (lwpstat.pr_context.uc_mcontext.fpregs); |
252b5132 RH |
9701 | sect->filepos = note->descpos |
9702 | + offsetof (lwpstatus_t, pr_context.uc_mcontext.fpregs); | |
9703 | #endif | |
9704 | ||
9705 | #if defined (HAVE_LWPSTATUS_T_PR_FPREG) | |
eea6121a | 9706 | sect->size = sizeof (lwpstat.pr_fpreg); |
252b5132 RH |
9707 | sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_fpreg); |
9708 | #endif | |
9709 | ||
252b5132 RH |
9710 | sect->alignment_power = 2; |
9711 | ||
936e320b | 9712 | return elfcore_maybe_make_sect (abfd, ".reg2", sect); |
252b5132 RH |
9713 | } |
9714 | #endif /* defined (HAVE_LWPSTATUS_T) */ | |
9715 | ||
b34976b6 | 9716 | static bfd_boolean |
217aa764 | 9717 | elfcore_grok_win32pstatus (bfd *abfd, Elf_Internal_Note *note) |
16e9c715 NC |
9718 | { |
9719 | char buf[30]; | |
c044fabd | 9720 | char *name; |
d4c88bbb | 9721 | size_t len; |
c044fabd | 9722 | asection *sect; |
4a6636fb PA |
9723 | int type; |
9724 | int is_active_thread; | |
9725 | bfd_vma base_addr; | |
16e9c715 | 9726 | |
4a6636fb | 9727 | if (note->descsz < 728) |
b34976b6 | 9728 | return TRUE; |
16e9c715 | 9729 | |
4a6636fb PA |
9730 | if (! CONST_STRNEQ (note->namedata, "win32")) |
9731 | return TRUE; | |
9732 | ||
9733 | type = bfd_get_32 (abfd, note->descdata); | |
c044fabd | 9734 | |
4a6636fb | 9735 | switch (type) |
16e9c715 | 9736 | { |
4a6636fb | 9737 | case 1 /* NOTE_INFO_PROCESS */: |
228e534f | 9738 | /* FIXME: need to add ->core->command. */ |
4a6636fb | 9739 | /* process_info.pid */ |
228e534f | 9740 | elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, note->descdata + 8); |
4a6636fb | 9741 | /* process_info.signal */ |
228e534f | 9742 | elf_tdata (abfd)->core->signal = bfd_get_32 (abfd, note->descdata + 12); |
c044fabd | 9743 | break; |
16e9c715 | 9744 | |
4a6636fb | 9745 | case 2 /* NOTE_INFO_THREAD */: |
16e9c715 | 9746 | /* Make a ".reg/999" section. */ |
4a6636fb PA |
9747 | /* thread_info.tid */ |
9748 | sprintf (buf, ".reg/%ld", (long) bfd_get_32 (abfd, note->descdata + 8)); | |
c044fabd | 9749 | |
d4c88bbb | 9750 | len = strlen (buf) + 1; |
a50b1753 | 9751 | name = (char *) bfd_alloc (abfd, len); |
16e9c715 | 9752 | if (name == NULL) |
b34976b6 | 9753 | return FALSE; |
c044fabd | 9754 | |
d4c88bbb | 9755 | memcpy (name, buf, len); |
16e9c715 | 9756 | |
117ed4f8 | 9757 | sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); |
16e9c715 | 9758 | if (sect == NULL) |
b34976b6 | 9759 | return FALSE; |
c044fabd | 9760 | |
4a6636fb PA |
9761 | /* sizeof (thread_info.thread_context) */ |
9762 | sect->size = 716; | |
9763 | /* offsetof (thread_info.thread_context) */ | |
9764 | sect->filepos = note->descpos + 12; | |
16e9c715 NC |
9765 | sect->alignment_power = 2; |
9766 | ||
4a6636fb PA |
9767 | /* thread_info.is_active_thread */ |
9768 | is_active_thread = bfd_get_32 (abfd, note->descdata + 8); | |
9769 | ||
9770 | if (is_active_thread) | |
16e9c715 | 9771 | if (! elfcore_maybe_make_sect (abfd, ".reg", sect)) |
b34976b6 | 9772 | return FALSE; |
16e9c715 NC |
9773 | break; |
9774 | ||
4a6636fb | 9775 | case 3 /* NOTE_INFO_MODULE */: |
16e9c715 | 9776 | /* Make a ".module/xxxxxxxx" section. */ |
4a6636fb PA |
9777 | /* module_info.base_address */ |
9778 | base_addr = bfd_get_32 (abfd, note->descdata + 4); | |
0af1713e | 9779 | sprintf (buf, ".module/%08lx", (unsigned long) base_addr); |
c044fabd | 9780 | |
d4c88bbb | 9781 | len = strlen (buf) + 1; |
a50b1753 | 9782 | name = (char *) bfd_alloc (abfd, len); |
16e9c715 | 9783 | if (name == NULL) |
b34976b6 | 9784 | return FALSE; |
c044fabd | 9785 | |
d4c88bbb | 9786 | memcpy (name, buf, len); |
252b5132 | 9787 | |
117ed4f8 | 9788 | sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); |
c044fabd | 9789 | |
16e9c715 | 9790 | if (sect == NULL) |
b34976b6 | 9791 | return FALSE; |
c044fabd | 9792 | |
eea6121a | 9793 | sect->size = note->descsz; |
16e9c715 | 9794 | sect->filepos = note->descpos; |
16e9c715 NC |
9795 | sect->alignment_power = 2; |
9796 | break; | |
9797 | ||
9798 | default: | |
b34976b6 | 9799 | return TRUE; |
16e9c715 NC |
9800 | } |
9801 | ||
b34976b6 | 9802 | return TRUE; |
16e9c715 | 9803 | } |
252b5132 | 9804 | |
b34976b6 | 9805 | static bfd_boolean |
217aa764 | 9806 | elfcore_grok_note (bfd *abfd, Elf_Internal_Note *note) |
252b5132 | 9807 | { |
9c5bfbb7 | 9808 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
bb0082d6 | 9809 | |
252b5132 RH |
9810 | switch (note->type) |
9811 | { | |
9812 | default: | |
b34976b6 | 9813 | return TRUE; |
252b5132 | 9814 | |
252b5132 | 9815 | case NT_PRSTATUS: |
bb0082d6 AM |
9816 | if (bed->elf_backend_grok_prstatus) |
9817 | if ((*bed->elf_backend_grok_prstatus) (abfd, note)) | |
b34976b6 | 9818 | return TRUE; |
bb0082d6 | 9819 | #if defined (HAVE_PRSTATUS_T) |
252b5132 | 9820 | return elfcore_grok_prstatus (abfd, note); |
bb0082d6 | 9821 | #else |
b34976b6 | 9822 | return TRUE; |
252b5132 RH |
9823 | #endif |
9824 | ||
9825 | #if defined (HAVE_PSTATUS_T) | |
9826 | case NT_PSTATUS: | |
9827 | return elfcore_grok_pstatus (abfd, note); | |
9828 | #endif | |
9829 | ||
9830 | #if defined (HAVE_LWPSTATUS_T) | |
9831 | case NT_LWPSTATUS: | |
9832 | return elfcore_grok_lwpstatus (abfd, note); | |
9833 | #endif | |
9834 | ||
9835 | case NT_FPREGSET: /* FIXME: rename to NT_PRFPREG */ | |
9836 | return elfcore_grok_prfpreg (abfd, note); | |
9837 | ||
c044fabd | 9838 | case NT_WIN32PSTATUS: |
16e9c715 | 9839 | return elfcore_grok_win32pstatus (abfd, note); |
16e9c715 | 9840 | |
c044fabd | 9841 | case NT_PRXFPREG: /* Linux SSE extension */ |
e377ab71 MK |
9842 | if (note->namesz == 6 |
9843 | && strcmp (note->namedata, "LINUX") == 0) | |
ff08c6bb JB |
9844 | return elfcore_grok_prxfpreg (abfd, note); |
9845 | else | |
b34976b6 | 9846 | return TRUE; |
ff08c6bb | 9847 | |
4339cae0 L |
9848 | case NT_X86_XSTATE: /* Linux XSAVE extension */ |
9849 | if (note->namesz == 6 | |
9850 | && strcmp (note->namedata, "LINUX") == 0) | |
9851 | return elfcore_grok_xstatereg (abfd, note); | |
9852 | else | |
9853 | return TRUE; | |
9854 | ||
97753bd5 AM |
9855 | case NT_PPC_VMX: |
9856 | if (note->namesz == 6 | |
9857 | && strcmp (note->namedata, "LINUX") == 0) | |
9858 | return elfcore_grok_ppc_vmx (abfd, note); | |
9859 | else | |
9860 | return TRUE; | |
9861 | ||
89eeb0bc LM |
9862 | case NT_PPC_VSX: |
9863 | if (note->namesz == 6 | |
07d6d2b8 AM |
9864 | && strcmp (note->namedata, "LINUX") == 0) |
9865 | return elfcore_grok_ppc_vsx (abfd, note); | |
89eeb0bc | 9866 | else |
07d6d2b8 | 9867 | return TRUE; |
89eeb0bc | 9868 | |
cb2366c1 EBM |
9869 | case NT_PPC_TAR: |
9870 | if (note->namesz == 6 | |
9871 | && strcmp (note->namedata, "LINUX") == 0) | |
9872 | return elfcore_grok_ppc_tar (abfd, note); | |
9873 | else | |
9874 | return TRUE; | |
9875 | ||
9876 | case NT_PPC_PPR: | |
9877 | if (note->namesz == 6 | |
9878 | && strcmp (note->namedata, "LINUX") == 0) | |
9879 | return elfcore_grok_ppc_ppr (abfd, note); | |
9880 | else | |
9881 | return TRUE; | |
9882 | ||
9883 | case NT_PPC_DSCR: | |
9884 | if (note->namesz == 6 | |
9885 | && strcmp (note->namedata, "LINUX") == 0) | |
9886 | return elfcore_grok_ppc_dscr (abfd, note); | |
9887 | else | |
9888 | return TRUE; | |
9889 | ||
9890 | case NT_PPC_EBB: | |
9891 | if (note->namesz == 6 | |
9892 | && strcmp (note->namedata, "LINUX") == 0) | |
9893 | return elfcore_grok_ppc_ebb (abfd, note); | |
9894 | else | |
9895 | return TRUE; | |
9896 | ||
9897 | case NT_PPC_PMU: | |
9898 | if (note->namesz == 6 | |
9899 | && strcmp (note->namedata, "LINUX") == 0) | |
9900 | return elfcore_grok_ppc_pmu (abfd, note); | |
9901 | else | |
9902 | return TRUE; | |
9903 | ||
9904 | case NT_PPC_TM_CGPR: | |
9905 | if (note->namesz == 6 | |
9906 | && strcmp (note->namedata, "LINUX") == 0) | |
9907 | return elfcore_grok_ppc_tm_cgpr (abfd, note); | |
9908 | else | |
9909 | return TRUE; | |
9910 | ||
9911 | case NT_PPC_TM_CFPR: | |
9912 | if (note->namesz == 6 | |
9913 | && strcmp (note->namedata, "LINUX") == 0) | |
9914 | return elfcore_grok_ppc_tm_cfpr (abfd, note); | |
9915 | else | |
9916 | return TRUE; | |
9917 | ||
9918 | case NT_PPC_TM_CVMX: | |
9919 | if (note->namesz == 6 | |
9920 | && strcmp (note->namedata, "LINUX") == 0) | |
9921 | return elfcore_grok_ppc_tm_cvmx (abfd, note); | |
9922 | else | |
9923 | return TRUE; | |
9924 | ||
9925 | case NT_PPC_TM_CVSX: | |
9926 | if (note->namesz == 6 | |
9927 | && strcmp (note->namedata, "LINUX") == 0) | |
9928 | return elfcore_grok_ppc_tm_cvsx (abfd, note); | |
9929 | else | |
9930 | return TRUE; | |
9931 | ||
9932 | case NT_PPC_TM_SPR: | |
9933 | if (note->namesz == 6 | |
9934 | && strcmp (note->namedata, "LINUX") == 0) | |
9935 | return elfcore_grok_ppc_tm_spr (abfd, note); | |
9936 | else | |
9937 | return TRUE; | |
9938 | ||
9939 | case NT_PPC_TM_CTAR: | |
9940 | if (note->namesz == 6 | |
9941 | && strcmp (note->namedata, "LINUX") == 0) | |
9942 | return elfcore_grok_ppc_tm_ctar (abfd, note); | |
9943 | else | |
9944 | return TRUE; | |
9945 | ||
9946 | case NT_PPC_TM_CPPR: | |
9947 | if (note->namesz == 6 | |
9948 | && strcmp (note->namedata, "LINUX") == 0) | |
9949 | return elfcore_grok_ppc_tm_cppr (abfd, note); | |
9950 | else | |
9951 | return TRUE; | |
9952 | ||
9953 | case NT_PPC_TM_CDSCR: | |
9954 | if (note->namesz == 6 | |
9955 | && strcmp (note->namedata, "LINUX") == 0) | |
9956 | return elfcore_grok_ppc_tm_cdscr (abfd, note); | |
9957 | else | |
9958 | return TRUE; | |
9959 | ||
0675e188 UW |
9960 | case NT_S390_HIGH_GPRS: |
9961 | if (note->namesz == 6 | |
07d6d2b8 AM |
9962 | && strcmp (note->namedata, "LINUX") == 0) |
9963 | return elfcore_grok_s390_high_gprs (abfd, note); | |
0675e188 | 9964 | else |
07d6d2b8 | 9965 | return TRUE; |
0675e188 | 9966 | |
d7eeb400 MS |
9967 | case NT_S390_TIMER: |
9968 | if (note->namesz == 6 | |
07d6d2b8 AM |
9969 | && strcmp (note->namedata, "LINUX") == 0) |
9970 | return elfcore_grok_s390_timer (abfd, note); | |
d7eeb400 | 9971 | else |
07d6d2b8 | 9972 | return TRUE; |
d7eeb400 MS |
9973 | |
9974 | case NT_S390_TODCMP: | |
9975 | if (note->namesz == 6 | |
07d6d2b8 AM |
9976 | && strcmp (note->namedata, "LINUX") == 0) |
9977 | return elfcore_grok_s390_todcmp (abfd, note); | |
d7eeb400 | 9978 | else |
07d6d2b8 | 9979 | return TRUE; |
d7eeb400 MS |
9980 | |
9981 | case NT_S390_TODPREG: | |
9982 | if (note->namesz == 6 | |
07d6d2b8 AM |
9983 | && strcmp (note->namedata, "LINUX") == 0) |
9984 | return elfcore_grok_s390_todpreg (abfd, note); | |
d7eeb400 | 9985 | else |
07d6d2b8 | 9986 | return TRUE; |
d7eeb400 MS |
9987 | |
9988 | case NT_S390_CTRS: | |
9989 | if (note->namesz == 6 | |
07d6d2b8 AM |
9990 | && strcmp (note->namedata, "LINUX") == 0) |
9991 | return elfcore_grok_s390_ctrs (abfd, note); | |
d7eeb400 | 9992 | else |
07d6d2b8 | 9993 | return TRUE; |
d7eeb400 MS |
9994 | |
9995 | case NT_S390_PREFIX: | |
9996 | if (note->namesz == 6 | |
07d6d2b8 AM |
9997 | && strcmp (note->namedata, "LINUX") == 0) |
9998 | return elfcore_grok_s390_prefix (abfd, note); | |
d7eeb400 | 9999 | else |
07d6d2b8 | 10000 | return TRUE; |
d7eeb400 | 10001 | |
355b81d9 UW |
10002 | case NT_S390_LAST_BREAK: |
10003 | if (note->namesz == 6 | |
07d6d2b8 AM |
10004 | && strcmp (note->namedata, "LINUX") == 0) |
10005 | return elfcore_grok_s390_last_break (abfd, note); | |
355b81d9 | 10006 | else |
07d6d2b8 | 10007 | return TRUE; |
355b81d9 UW |
10008 | |
10009 | case NT_S390_SYSTEM_CALL: | |
10010 | if (note->namesz == 6 | |
07d6d2b8 AM |
10011 | && strcmp (note->namedata, "LINUX") == 0) |
10012 | return elfcore_grok_s390_system_call (abfd, note); | |
355b81d9 | 10013 | else |
07d6d2b8 | 10014 | return TRUE; |
355b81d9 | 10015 | |
abb3f6cc NC |
10016 | case NT_S390_TDB: |
10017 | if (note->namesz == 6 | |
07d6d2b8 AM |
10018 | && strcmp (note->namedata, "LINUX") == 0) |
10019 | return elfcore_grok_s390_tdb (abfd, note); | |
abb3f6cc | 10020 | else |
07d6d2b8 | 10021 | return TRUE; |
abb3f6cc | 10022 | |
4ef9f41a AA |
10023 | case NT_S390_VXRS_LOW: |
10024 | if (note->namesz == 6 | |
10025 | && strcmp (note->namedata, "LINUX") == 0) | |
10026 | return elfcore_grok_s390_vxrs_low (abfd, note); | |
10027 | else | |
10028 | return TRUE; | |
10029 | ||
10030 | case NT_S390_VXRS_HIGH: | |
10031 | if (note->namesz == 6 | |
10032 | && strcmp (note->namedata, "LINUX") == 0) | |
10033 | return elfcore_grok_s390_vxrs_high (abfd, note); | |
10034 | else | |
10035 | return TRUE; | |
10036 | ||
88ab90e8 AA |
10037 | case NT_S390_GS_CB: |
10038 | if (note->namesz == 6 | |
10039 | && strcmp (note->namedata, "LINUX") == 0) | |
8fe09d74 | 10040 | return elfcore_grok_s390_gs_cb (abfd, note); |
88ab90e8 AA |
10041 | else |
10042 | return TRUE; | |
10043 | ||
10044 | case NT_S390_GS_BC: | |
10045 | if (note->namesz == 6 | |
10046 | && strcmp (note->namedata, "LINUX") == 0) | |
8fe09d74 | 10047 | return elfcore_grok_s390_gs_bc (abfd, note); |
88ab90e8 AA |
10048 | else |
10049 | return TRUE; | |
10050 | ||
faa9a424 UW |
10051 | case NT_ARM_VFP: |
10052 | if (note->namesz == 6 | |
10053 | && strcmp (note->namedata, "LINUX") == 0) | |
10054 | return elfcore_grok_arm_vfp (abfd, note); | |
10055 | else | |
10056 | return TRUE; | |
10057 | ||
652451f8 YZ |
10058 | case NT_ARM_TLS: |
10059 | if (note->namesz == 6 | |
10060 | && strcmp (note->namedata, "LINUX") == 0) | |
10061 | return elfcore_grok_aarch_tls (abfd, note); | |
10062 | else | |
10063 | return TRUE; | |
10064 | ||
10065 | case NT_ARM_HW_BREAK: | |
10066 | if (note->namesz == 6 | |
10067 | && strcmp (note->namedata, "LINUX") == 0) | |
10068 | return elfcore_grok_aarch_hw_break (abfd, note); | |
10069 | else | |
10070 | return TRUE; | |
10071 | ||
10072 | case NT_ARM_HW_WATCH: | |
10073 | if (note->namesz == 6 | |
10074 | && strcmp (note->namedata, "LINUX") == 0) | |
10075 | return elfcore_grok_aarch_hw_watch (abfd, note); | |
10076 | else | |
10077 | return TRUE; | |
10078 | ||
ad1cc4e4 AH |
10079 | case NT_ARM_SVE: |
10080 | if (note->namesz == 6 | |
10081 | && strcmp (note->namedata, "LINUX") == 0) | |
10082 | return elfcore_grok_aarch_sve (abfd, note); | |
10083 | else | |
10084 | return TRUE; | |
10085 | ||
252b5132 RH |
10086 | case NT_PRPSINFO: |
10087 | case NT_PSINFO: | |
bb0082d6 AM |
10088 | if (bed->elf_backend_grok_psinfo) |
10089 | if ((*bed->elf_backend_grok_psinfo) (abfd, note)) | |
b34976b6 | 10090 | return TRUE; |
bb0082d6 | 10091 | #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) |
252b5132 | 10092 | return elfcore_grok_psinfo (abfd, note); |
bb0082d6 | 10093 | #else |
b34976b6 | 10094 | return TRUE; |
252b5132 | 10095 | #endif |
3333a7c3 RM |
10096 | |
10097 | case NT_AUXV: | |
10098 | { | |
117ed4f8 AM |
10099 | asection *sect = bfd_make_section_anyway_with_flags (abfd, ".auxv", |
10100 | SEC_HAS_CONTENTS); | |
3333a7c3 RM |
10101 | |
10102 | if (sect == NULL) | |
10103 | return FALSE; | |
eea6121a | 10104 | sect->size = note->descsz; |
3333a7c3 | 10105 | sect->filepos = note->descpos; |
3333a7c3 RM |
10106 | sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32; |
10107 | ||
10108 | return TRUE; | |
10109 | } | |
9015683b | 10110 | |
451b7c33 TT |
10111 | case NT_FILE: |
10112 | return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.file", | |
10113 | note); | |
10114 | ||
9015683b TT |
10115 | case NT_SIGINFO: |
10116 | return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.siginfo", | |
10117 | note); | |
5b2c414d | 10118 | |
252b5132 RH |
10119 | } |
10120 | } | |
10121 | ||
718175fa JK |
10122 | static bfd_boolean |
10123 | elfobj_grok_gnu_build_id (bfd *abfd, Elf_Internal_Note *note) | |
10124 | { | |
c74f7d1c | 10125 | struct bfd_build_id* build_id; |
30e8ee25 AM |
10126 | |
10127 | if (note->descsz == 0) | |
10128 | return FALSE; | |
10129 | ||
c74f7d1c JT |
10130 | build_id = bfd_alloc (abfd, sizeof (struct bfd_build_id) - 1 + note->descsz); |
10131 | if (build_id == NULL) | |
718175fa JK |
10132 | return FALSE; |
10133 | ||
c74f7d1c JT |
10134 | build_id->size = note->descsz; |
10135 | memcpy (build_id->data, note->descdata, note->descsz); | |
10136 | abfd->build_id = build_id; | |
718175fa JK |
10137 | |
10138 | return TRUE; | |
10139 | } | |
10140 | ||
10141 | static bfd_boolean | |
10142 | elfobj_grok_gnu_note (bfd *abfd, Elf_Internal_Note *note) | |
10143 | { | |
10144 | switch (note->type) | |
10145 | { | |
10146 | default: | |
10147 | return TRUE; | |
10148 | ||
46bed679 L |
10149 | case NT_GNU_PROPERTY_TYPE_0: |
10150 | return _bfd_elf_parse_gnu_properties (abfd, note); | |
10151 | ||
718175fa JK |
10152 | case NT_GNU_BUILD_ID: |
10153 | return elfobj_grok_gnu_build_id (abfd, note); | |
10154 | } | |
10155 | } | |
10156 | ||
e21e5835 NC |
10157 | static bfd_boolean |
10158 | elfobj_grok_stapsdt_note_1 (bfd *abfd, Elf_Internal_Note *note) | |
10159 | { | |
10160 | struct sdt_note *cur = | |
10161 | (struct sdt_note *) bfd_alloc (abfd, sizeof (struct sdt_note) | |
10162 | + note->descsz); | |
10163 | ||
10164 | cur->next = (struct sdt_note *) (elf_tdata (abfd))->sdt_note_head; | |
10165 | cur->size = (bfd_size_type) note->descsz; | |
10166 | memcpy (cur->data, note->descdata, note->descsz); | |
10167 | ||
10168 | elf_tdata (abfd)->sdt_note_head = cur; | |
10169 | ||
10170 | return TRUE; | |
10171 | } | |
10172 | ||
10173 | static bfd_boolean | |
10174 | elfobj_grok_stapsdt_note (bfd *abfd, Elf_Internal_Note *note) | |
10175 | { | |
10176 | switch (note->type) | |
10177 | { | |
10178 | case NT_STAPSDT: | |
10179 | return elfobj_grok_stapsdt_note_1 (abfd, note); | |
10180 | ||
10181 | default: | |
10182 | return TRUE; | |
10183 | } | |
10184 | } | |
10185 | ||
aa1ed4a9 JB |
10186 | static bfd_boolean |
10187 | elfcore_grok_freebsd_psinfo (bfd *abfd, Elf_Internal_Note *note) | |
10188 | { | |
10189 | size_t offset; | |
10190 | ||
b5430a3c | 10191 | switch (elf_elfheader (abfd)->e_ident[EI_CLASS]) |
aa1ed4a9 | 10192 | { |
b5430a3c | 10193 | case ELFCLASS32: |
0064d223 JB |
10194 | if (note->descsz < 108) |
10195 | return FALSE; | |
aa1ed4a9 JB |
10196 | break; |
10197 | ||
b5430a3c | 10198 | case ELFCLASS64: |
0064d223 JB |
10199 | if (note->descsz < 120) |
10200 | return FALSE; | |
aa1ed4a9 JB |
10201 | break; |
10202 | ||
10203 | default: | |
10204 | return FALSE; | |
10205 | } | |
10206 | ||
0064d223 JB |
10207 | /* Check for version 1 in pr_version. */ |
10208 | if (bfd_h_get_32 (abfd, (bfd_byte *) note->descdata) != 1) | |
10209 | return FALSE; | |
80a04378 | 10210 | |
0064d223 JB |
10211 | offset = 4; |
10212 | ||
10213 | /* Skip over pr_psinfosz. */ | |
b5430a3c | 10214 | if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS32) |
0064d223 JB |
10215 | offset += 4; |
10216 | else | |
10217 | { | |
10218 | offset += 4; /* Padding before pr_psinfosz. */ | |
10219 | offset += 8; | |
10220 | } | |
10221 | ||
aa1ed4a9 JB |
10222 | /* pr_fname is PRFNAMESZ (16) + 1 bytes in size. */ |
10223 | elf_tdata (abfd)->core->program | |
10224 | = _bfd_elfcore_strndup (abfd, note->descdata + offset, 17); | |
10225 | offset += 17; | |
10226 | ||
10227 | /* pr_psargs is PRARGSZ (80) + 1 bytes in size. */ | |
10228 | elf_tdata (abfd)->core->command | |
10229 | = _bfd_elfcore_strndup (abfd, note->descdata + offset, 81); | |
0064d223 JB |
10230 | offset += 81; |
10231 | ||
10232 | /* Padding before pr_pid. */ | |
10233 | offset += 2; | |
10234 | ||
10235 | /* The pr_pid field was added in version "1a". */ | |
10236 | if (note->descsz < offset + 4) | |
10237 | return TRUE; | |
10238 | ||
10239 | elf_tdata (abfd)->core->pid | |
10240 | = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset); | |
aa1ed4a9 JB |
10241 | |
10242 | return TRUE; | |
10243 | } | |
10244 | ||
10245 | static bfd_boolean | |
10246 | elfcore_grok_freebsd_prstatus (bfd *abfd, Elf_Internal_Note *note) | |
10247 | { | |
10248 | size_t offset; | |
10249 | size_t size; | |
24d3e51b | 10250 | size_t min_size; |
aa1ed4a9 | 10251 | |
24d3e51b NC |
10252 | /* Compute offset of pr_getregsz, skipping over pr_statussz. |
10253 | Also compute minimum size of this note. */ | |
b5430a3c | 10254 | switch (elf_elfheader (abfd)->e_ident[EI_CLASS]) |
aa1ed4a9 | 10255 | { |
b5430a3c | 10256 | case ELFCLASS32: |
24d3e51b NC |
10257 | offset = 4 + 4; |
10258 | min_size = offset + (4 * 2) + 4 + 4 + 4; | |
aa1ed4a9 JB |
10259 | break; |
10260 | ||
b5430a3c | 10261 | case ELFCLASS64: |
24d3e51b NC |
10262 | offset = 4 + 4 + 8; /* Includes padding before pr_statussz. */ |
10263 | min_size = offset + (8 * 2) + 4 + 4 + 4 + 4; | |
aa1ed4a9 JB |
10264 | break; |
10265 | ||
10266 | default: | |
10267 | return FALSE; | |
10268 | } | |
10269 | ||
24d3e51b NC |
10270 | if (note->descsz < min_size) |
10271 | return FALSE; | |
10272 | ||
10273 | /* Check for version 1 in pr_version. */ | |
10274 | if (bfd_h_get_32 (abfd, (bfd_byte *) note->descdata) != 1) | |
10275 | return FALSE; | |
aa1ed4a9 | 10276 | |
24d3e51b NC |
10277 | /* Extract size of pr_reg from pr_gregsetsz. */ |
10278 | /* Skip over pr_gregsetsz and pr_fpregsetsz. */ | |
b5430a3c | 10279 | if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS32) |
24d3e51b NC |
10280 | { |
10281 | size = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset); | |
10282 | offset += 4 * 2; | |
10283 | } | |
b5430a3c | 10284 | else |
24d3e51b NC |
10285 | { |
10286 | size = bfd_h_get_64 (abfd, (bfd_byte *) note->descdata + offset); | |
10287 | offset += 8 * 2; | |
10288 | } | |
aa1ed4a9 | 10289 | |
24d3e51b | 10290 | /* Skip over pr_osreldate. */ |
aa1ed4a9 JB |
10291 | offset += 4; |
10292 | ||
24d3e51b | 10293 | /* Read signal from pr_cursig. */ |
aa1ed4a9 JB |
10294 | if (elf_tdata (abfd)->core->signal == 0) |
10295 | elf_tdata (abfd)->core->signal | |
10296 | = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset); | |
10297 | offset += 4; | |
10298 | ||
24d3e51b | 10299 | /* Read TID from pr_pid. */ |
aa1ed4a9 JB |
10300 | elf_tdata (abfd)->core->lwpid |
10301 | = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset); | |
10302 | offset += 4; | |
10303 | ||
24d3e51b | 10304 | /* Padding before pr_reg. */ |
b5430a3c | 10305 | if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS64) |
aa1ed4a9 JB |
10306 | offset += 4; |
10307 | ||
24d3e51b NC |
10308 | /* Make sure that there is enough data remaining in the note. */ |
10309 | if ((note->descsz - offset) < size) | |
10310 | return FALSE; | |
10311 | ||
aa1ed4a9 JB |
10312 | /* Make a ".reg/999" section and a ".reg" section. */ |
10313 | return _bfd_elfcore_make_pseudosection (abfd, ".reg", | |
10314 | size, note->descpos + offset); | |
10315 | } | |
10316 | ||
10317 | static bfd_boolean | |
10318 | elfcore_grok_freebsd_note (bfd *abfd, Elf_Internal_Note *note) | |
10319 | { | |
544c67cd JB |
10320 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
10321 | ||
aa1ed4a9 JB |
10322 | switch (note->type) |
10323 | { | |
10324 | case NT_PRSTATUS: | |
544c67cd JB |
10325 | if (bed->elf_backend_grok_freebsd_prstatus) |
10326 | if ((*bed->elf_backend_grok_freebsd_prstatus) (abfd, note)) | |
10327 | return TRUE; | |
aa1ed4a9 JB |
10328 | return elfcore_grok_freebsd_prstatus (abfd, note); |
10329 | ||
10330 | case NT_FPREGSET: | |
10331 | return elfcore_grok_prfpreg (abfd, note); | |
10332 | ||
10333 | case NT_PRPSINFO: | |
10334 | return elfcore_grok_freebsd_psinfo (abfd, note); | |
10335 | ||
10336 | case NT_FREEBSD_THRMISC: | |
10337 | if (note->namesz == 8) | |
10338 | return elfcore_make_note_pseudosection (abfd, ".thrmisc", note); | |
10339 | else | |
10340 | return TRUE; | |
10341 | ||
ddb2bbcf JB |
10342 | case NT_FREEBSD_PROCSTAT_PROC: |
10343 | return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.proc", | |
10344 | note); | |
10345 | ||
10346 | case NT_FREEBSD_PROCSTAT_FILES: | |
10347 | return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.files", | |
10348 | note); | |
10349 | ||
10350 | case NT_FREEBSD_PROCSTAT_VMMAP: | |
10351 | return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.vmmap", | |
10352 | note); | |
10353 | ||
3350c5f5 JB |
10354 | case NT_FREEBSD_PROCSTAT_AUXV: |
10355 | { | |
10356 | asection *sect = bfd_make_section_anyway_with_flags (abfd, ".auxv", | |
10357 | SEC_HAS_CONTENTS); | |
10358 | ||
10359 | if (sect == NULL) | |
10360 | return FALSE; | |
10361 | sect->size = note->descsz - 4; | |
10362 | sect->filepos = note->descpos + 4; | |
10363 | sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32; | |
10364 | ||
10365 | return TRUE; | |
10366 | } | |
10367 | ||
aa1ed4a9 JB |
10368 | case NT_X86_XSTATE: |
10369 | if (note->namesz == 8) | |
10370 | return elfcore_grok_xstatereg (abfd, note); | |
10371 | else | |
10372 | return TRUE; | |
10373 | ||
e6f3b9c3 JB |
10374 | case NT_FREEBSD_PTLWPINFO: |
10375 | return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.lwpinfo", | |
10376 | note); | |
10377 | ||
6d5be5d6 JB |
10378 | case NT_ARM_VFP: |
10379 | return elfcore_grok_arm_vfp (abfd, note); | |
10380 | ||
aa1ed4a9 JB |
10381 | default: |
10382 | return TRUE; | |
10383 | } | |
10384 | } | |
10385 | ||
b34976b6 | 10386 | static bfd_boolean |
217aa764 | 10387 | elfcore_netbsd_get_lwpid (Elf_Internal_Note *note, int *lwpidp) |
50b2bdb7 AM |
10388 | { |
10389 | char *cp; | |
10390 | ||
10391 | cp = strchr (note->namedata, '@'); | |
10392 | if (cp != NULL) | |
10393 | { | |
d2b64500 | 10394 | *lwpidp = atoi(cp + 1); |
b34976b6 | 10395 | return TRUE; |
50b2bdb7 | 10396 | } |
b34976b6 | 10397 | return FALSE; |
50b2bdb7 AM |
10398 | } |
10399 | ||
b34976b6 | 10400 | static bfd_boolean |
217aa764 | 10401 | elfcore_grok_netbsd_procinfo (bfd *abfd, Elf_Internal_Note *note) |
50b2bdb7 | 10402 | { |
80a04378 NC |
10403 | if (note->descsz <= 0x7c + 31) |
10404 | return FALSE; | |
10405 | ||
50b2bdb7 | 10406 | /* Signal number at offset 0x08. */ |
228e534f | 10407 | elf_tdata (abfd)->core->signal |
50b2bdb7 AM |
10408 | = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08); |
10409 | ||
10410 | /* Process ID at offset 0x50. */ | |
228e534f | 10411 | elf_tdata (abfd)->core->pid |
50b2bdb7 AM |
10412 | = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x50); |
10413 | ||
10414 | /* Command name at 0x7c (max 32 bytes, including nul). */ | |
228e534f | 10415 | elf_tdata (abfd)->core->command |
50b2bdb7 AM |
10416 | = _bfd_elfcore_strndup (abfd, note->descdata + 0x7c, 31); |
10417 | ||
7720ba9f MK |
10418 | return elfcore_make_note_pseudosection (abfd, ".note.netbsdcore.procinfo", |
10419 | note); | |
50b2bdb7 AM |
10420 | } |
10421 | ||
b34976b6 | 10422 | static bfd_boolean |
217aa764 | 10423 | elfcore_grok_netbsd_note (bfd *abfd, Elf_Internal_Note *note) |
50b2bdb7 AM |
10424 | { |
10425 | int lwp; | |
10426 | ||
10427 | if (elfcore_netbsd_get_lwpid (note, &lwp)) | |
228e534f | 10428 | elf_tdata (abfd)->core->lwpid = lwp; |
50b2bdb7 | 10429 | |
b4db1224 | 10430 | if (note->type == NT_NETBSDCORE_PROCINFO) |
50b2bdb7 AM |
10431 | { |
10432 | /* NetBSD-specific core "procinfo". Note that we expect to | |
08a40648 AM |
10433 | find this note before any of the others, which is fine, |
10434 | since the kernel writes this note out first when it | |
10435 | creates a core file. */ | |
47d9a591 | 10436 | |
50b2bdb7 AM |
10437 | return elfcore_grok_netbsd_procinfo (abfd, note); |
10438 | } | |
10439 | ||
b4db1224 JT |
10440 | /* As of Jan 2002 there are no other machine-independent notes |
10441 | defined for NetBSD core files. If the note type is less | |
10442 | than the start of the machine-dependent note types, we don't | |
10443 | understand it. */ | |
47d9a591 | 10444 | |
b4db1224 | 10445 | if (note->type < NT_NETBSDCORE_FIRSTMACH) |
b34976b6 | 10446 | return TRUE; |
50b2bdb7 AM |
10447 | |
10448 | ||
10449 | switch (bfd_get_arch (abfd)) | |
10450 | { | |
08a40648 AM |
10451 | /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and |
10452 | PT_GETFPREGS == mach+2. */ | |
50b2bdb7 AM |
10453 | |
10454 | case bfd_arch_alpha: | |
10455 | case bfd_arch_sparc: | |
10456 | switch (note->type) | |
08a40648 AM |
10457 | { |
10458 | case NT_NETBSDCORE_FIRSTMACH+0: | |
10459 | return elfcore_make_note_pseudosection (abfd, ".reg", note); | |
50b2bdb7 | 10460 | |
08a40648 AM |
10461 | case NT_NETBSDCORE_FIRSTMACH+2: |
10462 | return elfcore_make_note_pseudosection (abfd, ".reg2", note); | |
50b2bdb7 | 10463 | |
08a40648 AM |
10464 | default: |
10465 | return TRUE; | |
10466 | } | |
50b2bdb7 | 10467 | |
08a40648 AM |
10468 | /* On all other arch's, PT_GETREGS == mach+1 and |
10469 | PT_GETFPREGS == mach+3. */ | |
50b2bdb7 AM |
10470 | |
10471 | default: | |
10472 | switch (note->type) | |
08a40648 AM |
10473 | { |
10474 | case NT_NETBSDCORE_FIRSTMACH+1: | |
10475 | return elfcore_make_note_pseudosection (abfd, ".reg", note); | |
50b2bdb7 | 10476 | |
08a40648 AM |
10477 | case NT_NETBSDCORE_FIRSTMACH+3: |
10478 | return elfcore_make_note_pseudosection (abfd, ".reg2", note); | |
50b2bdb7 | 10479 | |
08a40648 AM |
10480 | default: |
10481 | return TRUE; | |
10482 | } | |
50b2bdb7 AM |
10483 | } |
10484 | /* NOTREACHED */ | |
10485 | } | |
10486 | ||
67cc5033 MK |
10487 | static bfd_boolean |
10488 | elfcore_grok_openbsd_procinfo (bfd *abfd, Elf_Internal_Note *note) | |
10489 | { | |
80a04378 NC |
10490 | if (note->descsz <= 0x48 + 31) |
10491 | return FALSE; | |
10492 | ||
67cc5033 | 10493 | /* Signal number at offset 0x08. */ |
228e534f | 10494 | elf_tdata (abfd)->core->signal |
67cc5033 MK |
10495 | = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08); |
10496 | ||
10497 | /* Process ID at offset 0x20. */ | |
228e534f | 10498 | elf_tdata (abfd)->core->pid |
67cc5033 MK |
10499 | = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x20); |
10500 | ||
10501 | /* Command name at 0x48 (max 32 bytes, including nul). */ | |
228e534f | 10502 | elf_tdata (abfd)->core->command |
67cc5033 MK |
10503 | = _bfd_elfcore_strndup (abfd, note->descdata + 0x48, 31); |
10504 | ||
10505 | return TRUE; | |
10506 | } | |
10507 | ||
10508 | static bfd_boolean | |
10509 | elfcore_grok_openbsd_note (bfd *abfd, Elf_Internal_Note *note) | |
10510 | { | |
10511 | if (note->type == NT_OPENBSD_PROCINFO) | |
10512 | return elfcore_grok_openbsd_procinfo (abfd, note); | |
10513 | ||
10514 | if (note->type == NT_OPENBSD_REGS) | |
10515 | return elfcore_make_note_pseudosection (abfd, ".reg", note); | |
10516 | ||
10517 | if (note->type == NT_OPENBSD_FPREGS) | |
10518 | return elfcore_make_note_pseudosection (abfd, ".reg2", note); | |
10519 | ||
10520 | if (note->type == NT_OPENBSD_XFPREGS) | |
10521 | return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note); | |
10522 | ||
10523 | if (note->type == NT_OPENBSD_AUXV) | |
10524 | { | |
10525 | asection *sect = bfd_make_section_anyway_with_flags (abfd, ".auxv", | |
10526 | SEC_HAS_CONTENTS); | |
10527 | ||
10528 | if (sect == NULL) | |
10529 | return FALSE; | |
10530 | sect->size = note->descsz; | |
10531 | sect->filepos = note->descpos; | |
10532 | sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32; | |
10533 | ||
10534 | return TRUE; | |
10535 | } | |
10536 | ||
10537 | if (note->type == NT_OPENBSD_WCOOKIE) | |
10538 | { | |
10539 | asection *sect = bfd_make_section_anyway_with_flags (abfd, ".wcookie", | |
10540 | SEC_HAS_CONTENTS); | |
10541 | ||
10542 | if (sect == NULL) | |
10543 | return FALSE; | |
10544 | sect->size = note->descsz; | |
10545 | sect->filepos = note->descpos; | |
10546 | sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32; | |
10547 | ||
10548 | return TRUE; | |
10549 | } | |
10550 | ||
10551 | return TRUE; | |
10552 | } | |
10553 | ||
07c6e936 | 10554 | static bfd_boolean |
d3fd4074 | 10555 | elfcore_grok_nto_status (bfd *abfd, Elf_Internal_Note *note, long *tid) |
07c6e936 NC |
10556 | { |
10557 | void *ddata = note->descdata; | |
10558 | char buf[100]; | |
10559 | char *name; | |
10560 | asection *sect; | |
f8843e87 AM |
10561 | short sig; |
10562 | unsigned flags; | |
07c6e936 | 10563 | |
80a04378 NC |
10564 | if (note->descsz < 16) |
10565 | return FALSE; | |
10566 | ||
07c6e936 | 10567 | /* nto_procfs_status 'pid' field is at offset 0. */ |
228e534f | 10568 | elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, (bfd_byte *) ddata); |
07c6e936 | 10569 | |
f8843e87 AM |
10570 | /* nto_procfs_status 'tid' field is at offset 4. Pass it back. */ |
10571 | *tid = bfd_get_32 (abfd, (bfd_byte *) ddata + 4); | |
10572 | ||
10573 | /* nto_procfs_status 'flags' field is at offset 8. */ | |
10574 | flags = bfd_get_32 (abfd, (bfd_byte *) ddata + 8); | |
07c6e936 NC |
10575 | |
10576 | /* nto_procfs_status 'what' field is at offset 14. */ | |
f8843e87 AM |
10577 | if ((sig = bfd_get_16 (abfd, (bfd_byte *) ddata + 14)) > 0) |
10578 | { | |
228e534f AM |
10579 | elf_tdata (abfd)->core->signal = sig; |
10580 | elf_tdata (abfd)->core->lwpid = *tid; | |
f8843e87 | 10581 | } |
07c6e936 | 10582 | |
f8843e87 AM |
10583 | /* _DEBUG_FLAG_CURTID (current thread) is 0x80. Some cores |
10584 | do not come from signals so we make sure we set the current | |
10585 | thread just in case. */ | |
10586 | if (flags & 0x00000080) | |
228e534f | 10587 | elf_tdata (abfd)->core->lwpid = *tid; |
07c6e936 NC |
10588 | |
10589 | /* Make a ".qnx_core_status/%d" section. */ | |
d3fd4074 | 10590 | sprintf (buf, ".qnx_core_status/%ld", *tid); |
07c6e936 | 10591 | |
a50b1753 | 10592 | name = (char *) bfd_alloc (abfd, strlen (buf) + 1); |
07c6e936 NC |
10593 | if (name == NULL) |
10594 | return FALSE; | |
10595 | strcpy (name, buf); | |
10596 | ||
117ed4f8 | 10597 | sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); |
07c6e936 NC |
10598 | if (sect == NULL) |
10599 | return FALSE; | |
10600 | ||
07d6d2b8 AM |
10601 | sect->size = note->descsz; |
10602 | sect->filepos = note->descpos; | |
07c6e936 NC |
10603 | sect->alignment_power = 2; |
10604 | ||
10605 | return (elfcore_maybe_make_sect (abfd, ".qnx_core_status", sect)); | |
10606 | } | |
10607 | ||
10608 | static bfd_boolean | |
d69f560c KW |
10609 | elfcore_grok_nto_regs (bfd *abfd, |
10610 | Elf_Internal_Note *note, | |
d3fd4074 | 10611 | long tid, |
d69f560c | 10612 | char *base) |
07c6e936 NC |
10613 | { |
10614 | char buf[100]; | |
10615 | char *name; | |
10616 | asection *sect; | |
10617 | ||
d69f560c | 10618 | /* Make a "(base)/%d" section. */ |
d3fd4074 | 10619 | sprintf (buf, "%s/%ld", base, tid); |
07c6e936 | 10620 | |
a50b1753 | 10621 | name = (char *) bfd_alloc (abfd, strlen (buf) + 1); |
07c6e936 NC |
10622 | if (name == NULL) |
10623 | return FALSE; | |
10624 | strcpy (name, buf); | |
10625 | ||
117ed4f8 | 10626 | sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); |
07c6e936 NC |
10627 | if (sect == NULL) |
10628 | return FALSE; | |
10629 | ||
07d6d2b8 AM |
10630 | sect->size = note->descsz; |
10631 | sect->filepos = note->descpos; | |
07c6e936 NC |
10632 | sect->alignment_power = 2; |
10633 | ||
f8843e87 | 10634 | /* This is the current thread. */ |
228e534f | 10635 | if (elf_tdata (abfd)->core->lwpid == tid) |
d69f560c | 10636 | return elfcore_maybe_make_sect (abfd, base, sect); |
f8843e87 AM |
10637 | |
10638 | return TRUE; | |
07c6e936 NC |
10639 | } |
10640 | ||
10641 | #define BFD_QNT_CORE_INFO 7 | |
10642 | #define BFD_QNT_CORE_STATUS 8 | |
10643 | #define BFD_QNT_CORE_GREG 9 | |
10644 | #define BFD_QNT_CORE_FPREG 10 | |
10645 | ||
10646 | static bfd_boolean | |
217aa764 | 10647 | elfcore_grok_nto_note (bfd *abfd, Elf_Internal_Note *note) |
07c6e936 NC |
10648 | { |
10649 | /* Every GREG section has a STATUS section before it. Store the | |
811072d8 | 10650 | tid from the previous call to pass down to the next gregs |
07c6e936 | 10651 | function. */ |
d3fd4074 | 10652 | static long tid = 1; |
07c6e936 NC |
10653 | |
10654 | switch (note->type) | |
10655 | { | |
d69f560c KW |
10656 | case BFD_QNT_CORE_INFO: |
10657 | return elfcore_make_note_pseudosection (abfd, ".qnx_core_info", note); | |
10658 | case BFD_QNT_CORE_STATUS: | |
10659 | return elfcore_grok_nto_status (abfd, note, &tid); | |
10660 | case BFD_QNT_CORE_GREG: | |
10661 | return elfcore_grok_nto_regs (abfd, note, tid, ".reg"); | |
10662 | case BFD_QNT_CORE_FPREG: | |
10663 | return elfcore_grok_nto_regs (abfd, note, tid, ".reg2"); | |
10664 | default: | |
10665 | return TRUE; | |
07c6e936 NC |
10666 | } |
10667 | } | |
10668 | ||
b15fa79e AM |
10669 | static bfd_boolean |
10670 | elfcore_grok_spu_note (bfd *abfd, Elf_Internal_Note *note) | |
10671 | { | |
10672 | char *name; | |
10673 | asection *sect; | |
10674 | size_t len; | |
10675 | ||
10676 | /* Use note name as section name. */ | |
10677 | len = note->namesz; | |
a50b1753 | 10678 | name = (char *) bfd_alloc (abfd, len); |
b15fa79e AM |
10679 | if (name == NULL) |
10680 | return FALSE; | |
10681 | memcpy (name, note->namedata, len); | |
10682 | name[len - 1] = '\0'; | |
10683 | ||
10684 | sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); | |
10685 | if (sect == NULL) | |
10686 | return FALSE; | |
10687 | ||
07d6d2b8 AM |
10688 | sect->size = note->descsz; |
10689 | sect->filepos = note->descpos; | |
b15fa79e AM |
10690 | sect->alignment_power = 1; |
10691 | ||
10692 | return TRUE; | |
10693 | } | |
10694 | ||
7c76fa91 MS |
10695 | /* Function: elfcore_write_note |
10696 | ||
47d9a591 | 10697 | Inputs: |
a39f3346 | 10698 | buffer to hold note, and current size of buffer |
7c76fa91 MS |
10699 | name of note |
10700 | type of note | |
10701 | data for note | |
10702 | size of data for note | |
10703 | ||
a39f3346 AM |
10704 | Writes note to end of buffer. ELF64 notes are written exactly as |
10705 | for ELF32, despite the current (as of 2006) ELF gabi specifying | |
10706 | that they ought to have 8-byte namesz and descsz field, and have | |
10707 | 8-byte alignment. Other writers, eg. Linux kernel, do the same. | |
10708 | ||
7c76fa91 | 10709 | Return: |
a39f3346 | 10710 | Pointer to realloc'd buffer, *BUFSIZ updated. */ |
7c76fa91 MS |
10711 | |
10712 | char * | |
a39f3346 | 10713 | elfcore_write_note (bfd *abfd, |
217aa764 | 10714 | char *buf, |
a39f3346 | 10715 | int *bufsiz, |
217aa764 | 10716 | const char *name, |
a39f3346 | 10717 | int type, |
217aa764 | 10718 | const void *input, |
a39f3346 | 10719 | int size) |
7c76fa91 MS |
10720 | { |
10721 | Elf_External_Note *xnp; | |
d4c88bbb | 10722 | size_t namesz; |
d4c88bbb | 10723 | size_t newspace; |
a39f3346 | 10724 | char *dest; |
7c76fa91 | 10725 | |
d4c88bbb | 10726 | namesz = 0; |
d4c88bbb | 10727 | if (name != NULL) |
a39f3346 | 10728 | namesz = strlen (name) + 1; |
d4c88bbb | 10729 | |
a39f3346 | 10730 | newspace = 12 + ((namesz + 3) & -4) + ((size + 3) & -4); |
d4c88bbb | 10731 | |
a50b1753 | 10732 | buf = (char *) realloc (buf, *bufsiz + newspace); |
14b1c01e AM |
10733 | if (buf == NULL) |
10734 | return buf; | |
a39f3346 | 10735 | dest = buf + *bufsiz; |
7c76fa91 MS |
10736 | *bufsiz += newspace; |
10737 | xnp = (Elf_External_Note *) dest; | |
10738 | H_PUT_32 (abfd, namesz, xnp->namesz); | |
10739 | H_PUT_32 (abfd, size, xnp->descsz); | |
10740 | H_PUT_32 (abfd, type, xnp->type); | |
d4c88bbb AM |
10741 | dest = xnp->name; |
10742 | if (name != NULL) | |
10743 | { | |
10744 | memcpy (dest, name, namesz); | |
10745 | dest += namesz; | |
a39f3346 | 10746 | while (namesz & 3) |
d4c88bbb AM |
10747 | { |
10748 | *dest++ = '\0'; | |
a39f3346 | 10749 | ++namesz; |
d4c88bbb AM |
10750 | } |
10751 | } | |
10752 | memcpy (dest, input, size); | |
a39f3346 AM |
10753 | dest += size; |
10754 | while (size & 3) | |
10755 | { | |
10756 | *dest++ = '\0'; | |
10757 | ++size; | |
10758 | } | |
10759 | return buf; | |
7c76fa91 MS |
10760 | } |
10761 | ||
602f1657 AM |
10762 | /* gcc-8 warns (*) on all the strncpy calls in this function about |
10763 | possible string truncation. The "truncation" is not a bug. We | |
10764 | have an external representation of structs with fields that are not | |
10765 | necessarily NULL terminated and corresponding internal | |
10766 | representation fields that are one larger so that they can always | |
10767 | be NULL terminated. | |
10768 | gcc versions between 4.2 and 4.6 do not allow pragma control of | |
10769 | diagnostics inside functions, giving a hard error if you try to use | |
10770 | the finer control available with later versions. | |
10771 | gcc prior to 4.2 warns about diagnostic push and pop. | |
10772 | gcc-5, gcc-6 and gcc-7 warn that -Wstringop-truncation is unknown, | |
10773 | unless you also add #pragma GCC diagnostic ignored "-Wpragma". | |
10774 | (*) Depending on your system header files! */ | |
d99b4b92 | 10775 | #if GCC_VERSION >= 8000 |
602f1657 AM |
10776 | # pragma GCC diagnostic push |
10777 | # pragma GCC diagnostic ignored "-Wstringop-truncation" | |
d99b4b92 | 10778 | #endif |
7c76fa91 | 10779 | char * |
217aa764 AM |
10780 | elfcore_write_prpsinfo (bfd *abfd, |
10781 | char *buf, | |
10782 | int *bufsiz, | |
10783 | const char *fname, | |
10784 | const char *psargs) | |
7c76fa91 | 10785 | { |
183e98be AM |
10786 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
10787 | ||
10788 | if (bed->elf_backend_write_core_note != NULL) | |
10789 | { | |
10790 | char *ret; | |
10791 | ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz, | |
10792 | NT_PRPSINFO, fname, psargs); | |
10793 | if (ret != NULL) | |
10794 | return ret; | |
10795 | } | |
7c76fa91 | 10796 | |
1f20dca5 | 10797 | #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) |
602f1657 | 10798 | # if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T) |
183e98be AM |
10799 | if (bed->s->elfclass == ELFCLASS32) |
10800 | { | |
602f1657 | 10801 | # if defined (HAVE_PSINFO32_T) |
183e98be AM |
10802 | psinfo32_t data; |
10803 | int note_type = NT_PSINFO; | |
602f1657 | 10804 | # else |
183e98be AM |
10805 | prpsinfo32_t data; |
10806 | int note_type = NT_PRPSINFO; | |
602f1657 | 10807 | # endif |
183e98be AM |
10808 | |
10809 | memset (&data, 0, sizeof (data)); | |
10810 | strncpy (data.pr_fname, fname, sizeof (data.pr_fname)); | |
10811 | strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs)); | |
10812 | return elfcore_write_note (abfd, buf, bufsiz, | |
1f20dca5 | 10813 | "CORE", note_type, &data, sizeof (data)); |
183e98be AM |
10814 | } |
10815 | else | |
602f1657 | 10816 | # endif |
183e98be | 10817 | { |
602f1657 | 10818 | # if defined (HAVE_PSINFO_T) |
183e98be AM |
10819 | psinfo_t data; |
10820 | int note_type = NT_PSINFO; | |
602f1657 | 10821 | # else |
183e98be AM |
10822 | prpsinfo_t data; |
10823 | int note_type = NT_PRPSINFO; | |
602f1657 | 10824 | # endif |
7c76fa91 | 10825 | |
183e98be AM |
10826 | memset (&data, 0, sizeof (data)); |
10827 | strncpy (data.pr_fname, fname, sizeof (data.pr_fname)); | |
10828 | strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs)); | |
10829 | return elfcore_write_note (abfd, buf, bufsiz, | |
1f20dca5 | 10830 | "CORE", note_type, &data, sizeof (data)); |
183e98be | 10831 | } |
7c76fa91 MS |
10832 | #endif /* PSINFO_T or PRPSINFO_T */ |
10833 | ||
1f20dca5 UW |
10834 | free (buf); |
10835 | return NULL; | |
10836 | } | |
d99b4b92 | 10837 | #if GCC_VERSION >= 8000 |
602f1657 | 10838 | # pragma GCC diagnostic pop |
d99b4b92 | 10839 | #endif |
1f20dca5 | 10840 | |
70a38d42 SDJ |
10841 | char * |
10842 | elfcore_write_linux_prpsinfo32 | |
10843 | (bfd *abfd, char *buf, int *bufsiz, | |
10844 | const struct elf_internal_linux_prpsinfo *prpsinfo) | |
10845 | { | |
a2f63b2e MR |
10846 | if (get_elf_backend_data (abfd)->linux_prpsinfo32_ugid16) |
10847 | { | |
10848 | struct elf_external_linux_prpsinfo32_ugid16 data; | |
10849 | ||
10850 | swap_linux_prpsinfo32_ugid16_out (abfd, prpsinfo, &data); | |
10851 | return elfcore_write_note (abfd, buf, bufsiz, "CORE", NT_PRPSINFO, | |
10852 | &data, sizeof (data)); | |
10853 | } | |
10854 | else | |
10855 | { | |
10856 | struct elf_external_linux_prpsinfo32_ugid32 data; | |
70a38d42 | 10857 | |
a2f63b2e MR |
10858 | swap_linux_prpsinfo32_ugid32_out (abfd, prpsinfo, &data); |
10859 | return elfcore_write_note (abfd, buf, bufsiz, "CORE", NT_PRPSINFO, | |
10860 | &data, sizeof (data)); | |
10861 | } | |
70a38d42 SDJ |
10862 | } |
10863 | ||
10864 | char * | |
10865 | elfcore_write_linux_prpsinfo64 | |
10866 | (bfd *abfd, char *buf, int *bufsiz, | |
10867 | const struct elf_internal_linux_prpsinfo *prpsinfo) | |
10868 | { | |
3c9a7b0d MR |
10869 | if (get_elf_backend_data (abfd)->linux_prpsinfo64_ugid16) |
10870 | { | |
10871 | struct elf_external_linux_prpsinfo64_ugid16 data; | |
10872 | ||
10873 | swap_linux_prpsinfo64_ugid16_out (abfd, prpsinfo, &data); | |
10874 | return elfcore_write_note (abfd, buf, bufsiz, | |
10875 | "CORE", NT_PRPSINFO, &data, sizeof (data)); | |
10876 | } | |
10877 | else | |
10878 | { | |
10879 | struct elf_external_linux_prpsinfo64_ugid32 data; | |
70a38d42 | 10880 | |
3c9a7b0d MR |
10881 | swap_linux_prpsinfo64_ugid32_out (abfd, prpsinfo, &data); |
10882 | return elfcore_write_note (abfd, buf, bufsiz, | |
10883 | "CORE", NT_PRPSINFO, &data, sizeof (data)); | |
10884 | } | |
70a38d42 SDJ |
10885 | } |
10886 | ||
7c76fa91 | 10887 | char * |
217aa764 AM |
10888 | elfcore_write_prstatus (bfd *abfd, |
10889 | char *buf, | |
10890 | int *bufsiz, | |
10891 | long pid, | |
10892 | int cursig, | |
10893 | const void *gregs) | |
7c76fa91 | 10894 | { |
183e98be | 10895 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
7c76fa91 | 10896 | |
183e98be AM |
10897 | if (bed->elf_backend_write_core_note != NULL) |
10898 | { | |
10899 | char *ret; | |
10900 | ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz, | |
10901 | NT_PRSTATUS, | |
10902 | pid, cursig, gregs); | |
10903 | if (ret != NULL) | |
10904 | return ret; | |
10905 | } | |
10906 | ||
1f20dca5 | 10907 | #if defined (HAVE_PRSTATUS_T) |
183e98be AM |
10908 | #if defined (HAVE_PRSTATUS32_T) |
10909 | if (bed->s->elfclass == ELFCLASS32) | |
10910 | { | |
10911 | prstatus32_t prstat; | |
10912 | ||
10913 | memset (&prstat, 0, sizeof (prstat)); | |
10914 | prstat.pr_pid = pid; | |
10915 | prstat.pr_cursig = cursig; | |
10916 | memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg)); | |
1f20dca5 | 10917 | return elfcore_write_note (abfd, buf, bufsiz, "CORE", |
183e98be AM |
10918 | NT_PRSTATUS, &prstat, sizeof (prstat)); |
10919 | } | |
10920 | else | |
10921 | #endif | |
10922 | { | |
10923 | prstatus_t prstat; | |
10924 | ||
10925 | memset (&prstat, 0, sizeof (prstat)); | |
10926 | prstat.pr_pid = pid; | |
10927 | prstat.pr_cursig = cursig; | |
10928 | memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg)); | |
1f20dca5 | 10929 | return elfcore_write_note (abfd, buf, bufsiz, "CORE", |
183e98be AM |
10930 | NT_PRSTATUS, &prstat, sizeof (prstat)); |
10931 | } | |
7c76fa91 MS |
10932 | #endif /* HAVE_PRSTATUS_T */ |
10933 | ||
1f20dca5 UW |
10934 | free (buf); |
10935 | return NULL; | |
10936 | } | |
10937 | ||
51316059 MS |
10938 | #if defined (HAVE_LWPSTATUS_T) |
10939 | char * | |
217aa764 AM |
10940 | elfcore_write_lwpstatus (bfd *abfd, |
10941 | char *buf, | |
10942 | int *bufsiz, | |
10943 | long pid, | |
10944 | int cursig, | |
10945 | const void *gregs) | |
51316059 MS |
10946 | { |
10947 | lwpstatus_t lwpstat; | |
183e98be | 10948 | const char *note_name = "CORE"; |
51316059 MS |
10949 | |
10950 | memset (&lwpstat, 0, sizeof (lwpstat)); | |
10951 | lwpstat.pr_lwpid = pid >> 16; | |
10952 | lwpstat.pr_cursig = cursig; | |
10953 | #if defined (HAVE_LWPSTATUS_T_PR_REG) | |
d1e8523e | 10954 | memcpy (&lwpstat.pr_reg, gregs, sizeof (lwpstat.pr_reg)); |
51316059 MS |
10955 | #elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT) |
10956 | #if !defined(gregs) | |
10957 | memcpy (lwpstat.pr_context.uc_mcontext.gregs, | |
10958 | gregs, sizeof (lwpstat.pr_context.uc_mcontext.gregs)); | |
10959 | #else | |
10960 | memcpy (lwpstat.pr_context.uc_mcontext.__gregs, | |
10961 | gregs, sizeof (lwpstat.pr_context.uc_mcontext.__gregs)); | |
10962 | #endif | |
10963 | #endif | |
47d9a591 | 10964 | return elfcore_write_note (abfd, buf, bufsiz, note_name, |
51316059 MS |
10965 | NT_LWPSTATUS, &lwpstat, sizeof (lwpstat)); |
10966 | } | |
10967 | #endif /* HAVE_LWPSTATUS_T */ | |
10968 | ||
7c76fa91 MS |
10969 | #if defined (HAVE_PSTATUS_T) |
10970 | char * | |
217aa764 AM |
10971 | elfcore_write_pstatus (bfd *abfd, |
10972 | char *buf, | |
10973 | int *bufsiz, | |
10974 | long pid, | |
6c10990d NC |
10975 | int cursig ATTRIBUTE_UNUSED, |
10976 | const void *gregs ATTRIBUTE_UNUSED) | |
7c76fa91 | 10977 | { |
183e98be AM |
10978 | const char *note_name = "CORE"; |
10979 | #if defined (HAVE_PSTATUS32_T) | |
10980 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
7c76fa91 | 10981 | |
183e98be AM |
10982 | if (bed->s->elfclass == ELFCLASS32) |
10983 | { | |
10984 | pstatus32_t pstat; | |
10985 | ||
10986 | memset (&pstat, 0, sizeof (pstat)); | |
10987 | pstat.pr_pid = pid & 0xffff; | |
10988 | buf = elfcore_write_note (abfd, buf, bufsiz, note_name, | |
10989 | NT_PSTATUS, &pstat, sizeof (pstat)); | |
10990 | return buf; | |
10991 | } | |
10992 | else | |
10993 | #endif | |
10994 | { | |
10995 | pstatus_t pstat; | |
10996 | ||
10997 | memset (&pstat, 0, sizeof (pstat)); | |
10998 | pstat.pr_pid = pid & 0xffff; | |
10999 | buf = elfcore_write_note (abfd, buf, bufsiz, note_name, | |
11000 | NT_PSTATUS, &pstat, sizeof (pstat)); | |
11001 | return buf; | |
11002 | } | |
7c76fa91 MS |
11003 | } |
11004 | #endif /* HAVE_PSTATUS_T */ | |
11005 | ||
11006 | char * | |
217aa764 AM |
11007 | elfcore_write_prfpreg (bfd *abfd, |
11008 | char *buf, | |
11009 | int *bufsiz, | |
11010 | const void *fpregs, | |
11011 | int size) | |
7c76fa91 | 11012 | { |
183e98be | 11013 | const char *note_name = "CORE"; |
47d9a591 | 11014 | return elfcore_write_note (abfd, buf, bufsiz, |
7c76fa91 MS |
11015 | note_name, NT_FPREGSET, fpregs, size); |
11016 | } | |
11017 | ||
11018 | char * | |
217aa764 AM |
11019 | elfcore_write_prxfpreg (bfd *abfd, |
11020 | char *buf, | |
11021 | int *bufsiz, | |
11022 | const void *xfpregs, | |
11023 | int size) | |
7c76fa91 MS |
11024 | { |
11025 | char *note_name = "LINUX"; | |
47d9a591 | 11026 | return elfcore_write_note (abfd, buf, bufsiz, |
7c76fa91 MS |
11027 | note_name, NT_PRXFPREG, xfpregs, size); |
11028 | } | |
11029 | ||
4339cae0 L |
11030 | char * |
11031 | elfcore_write_xstatereg (bfd *abfd, char *buf, int *bufsiz, | |
11032 | const void *xfpregs, int size) | |
11033 | { | |
97de3545 JB |
11034 | char *note_name; |
11035 | if (get_elf_backend_data (abfd)->elf_osabi == ELFOSABI_FREEBSD) | |
11036 | note_name = "FreeBSD"; | |
11037 | else | |
11038 | note_name = "LINUX"; | |
4339cae0 L |
11039 | return elfcore_write_note (abfd, buf, bufsiz, |
11040 | note_name, NT_X86_XSTATE, xfpregs, size); | |
11041 | } | |
11042 | ||
97753bd5 AM |
11043 | char * |
11044 | elfcore_write_ppc_vmx (bfd *abfd, | |
11045 | char *buf, | |
11046 | int *bufsiz, | |
11047 | const void *ppc_vmx, | |
11048 | int size) | |
11049 | { | |
11050 | char *note_name = "LINUX"; | |
11051 | return elfcore_write_note (abfd, buf, bufsiz, | |
11052 | note_name, NT_PPC_VMX, ppc_vmx, size); | |
11053 | } | |
11054 | ||
89eeb0bc LM |
11055 | char * |
11056 | elfcore_write_ppc_vsx (bfd *abfd, | |
07d6d2b8 AM |
11057 | char *buf, |
11058 | int *bufsiz, | |
11059 | const void *ppc_vsx, | |
11060 | int size) | |
89eeb0bc LM |
11061 | { |
11062 | char *note_name = "LINUX"; | |
11063 | return elfcore_write_note (abfd, buf, bufsiz, | |
07d6d2b8 | 11064 | note_name, NT_PPC_VSX, ppc_vsx, size); |
89eeb0bc LM |
11065 | } |
11066 | ||
cb2366c1 EBM |
11067 | char * |
11068 | elfcore_write_ppc_tar (bfd *abfd, | |
11069 | char *buf, | |
11070 | int *bufsiz, | |
11071 | const void *ppc_tar, | |
11072 | int size) | |
11073 | { | |
11074 | char *note_name = "LINUX"; | |
11075 | return elfcore_write_note (abfd, buf, bufsiz, | |
11076 | note_name, NT_PPC_TAR, ppc_tar, size); | |
11077 | } | |
11078 | ||
11079 | char * | |
11080 | elfcore_write_ppc_ppr (bfd *abfd, | |
11081 | char *buf, | |
11082 | int *bufsiz, | |
11083 | const void *ppc_ppr, | |
11084 | int size) | |
11085 | { | |
11086 | char *note_name = "LINUX"; | |
11087 | return elfcore_write_note (abfd, buf, bufsiz, | |
11088 | note_name, NT_PPC_PPR, ppc_ppr, size); | |
11089 | } | |
11090 | ||
11091 | char * | |
11092 | elfcore_write_ppc_dscr (bfd *abfd, | |
11093 | char *buf, | |
11094 | int *bufsiz, | |
11095 | const void *ppc_dscr, | |
11096 | int size) | |
11097 | { | |
11098 | char *note_name = "LINUX"; | |
11099 | return elfcore_write_note (abfd, buf, bufsiz, | |
11100 | note_name, NT_PPC_DSCR, ppc_dscr, size); | |
11101 | } | |
11102 | ||
11103 | char * | |
11104 | elfcore_write_ppc_ebb (bfd *abfd, | |
11105 | char *buf, | |
11106 | int *bufsiz, | |
11107 | const void *ppc_ebb, | |
11108 | int size) | |
11109 | { | |
11110 | char *note_name = "LINUX"; | |
11111 | return elfcore_write_note (abfd, buf, bufsiz, | |
11112 | note_name, NT_PPC_EBB, ppc_ebb, size); | |
11113 | } | |
11114 | ||
11115 | char * | |
11116 | elfcore_write_ppc_pmu (bfd *abfd, | |
11117 | char *buf, | |
11118 | int *bufsiz, | |
11119 | const void *ppc_pmu, | |
11120 | int size) | |
11121 | { | |
11122 | char *note_name = "LINUX"; | |
11123 | return elfcore_write_note (abfd, buf, bufsiz, | |
11124 | note_name, NT_PPC_PMU, ppc_pmu, size); | |
11125 | } | |
11126 | ||
11127 | char * | |
11128 | elfcore_write_ppc_tm_cgpr (bfd *abfd, | |
11129 | char *buf, | |
11130 | int *bufsiz, | |
11131 | const void *ppc_tm_cgpr, | |
11132 | int size) | |
11133 | { | |
11134 | char *note_name = "LINUX"; | |
11135 | return elfcore_write_note (abfd, buf, bufsiz, | |
11136 | note_name, NT_PPC_TM_CGPR, ppc_tm_cgpr, size); | |
11137 | } | |
11138 | ||
11139 | char * | |
11140 | elfcore_write_ppc_tm_cfpr (bfd *abfd, | |
11141 | char *buf, | |
11142 | int *bufsiz, | |
11143 | const void *ppc_tm_cfpr, | |
11144 | int size) | |
11145 | { | |
11146 | char *note_name = "LINUX"; | |
11147 | return elfcore_write_note (abfd, buf, bufsiz, | |
11148 | note_name, NT_PPC_TM_CFPR, ppc_tm_cfpr, size); | |
11149 | } | |
11150 | ||
11151 | char * | |
11152 | elfcore_write_ppc_tm_cvmx (bfd *abfd, | |
11153 | char *buf, | |
11154 | int *bufsiz, | |
11155 | const void *ppc_tm_cvmx, | |
11156 | int size) | |
11157 | { | |
11158 | char *note_name = "LINUX"; | |
11159 | return elfcore_write_note (abfd, buf, bufsiz, | |
11160 | note_name, NT_PPC_TM_CVMX, ppc_tm_cvmx, size); | |
11161 | } | |
11162 | ||
11163 | char * | |
11164 | elfcore_write_ppc_tm_cvsx (bfd *abfd, | |
11165 | char *buf, | |
11166 | int *bufsiz, | |
11167 | const void *ppc_tm_cvsx, | |
11168 | int size) | |
11169 | { | |
11170 | char *note_name = "LINUX"; | |
11171 | return elfcore_write_note (abfd, buf, bufsiz, | |
11172 | note_name, NT_PPC_TM_CVSX, ppc_tm_cvsx, size); | |
11173 | } | |
11174 | ||
11175 | char * | |
11176 | elfcore_write_ppc_tm_spr (bfd *abfd, | |
11177 | char *buf, | |
11178 | int *bufsiz, | |
11179 | const void *ppc_tm_spr, | |
11180 | int size) | |
11181 | { | |
11182 | char *note_name = "LINUX"; | |
11183 | return elfcore_write_note (abfd, buf, bufsiz, | |
11184 | note_name, NT_PPC_TM_SPR, ppc_tm_spr, size); | |
11185 | } | |
11186 | ||
11187 | char * | |
11188 | elfcore_write_ppc_tm_ctar (bfd *abfd, | |
11189 | char *buf, | |
11190 | int *bufsiz, | |
11191 | const void *ppc_tm_ctar, | |
11192 | int size) | |
11193 | { | |
11194 | char *note_name = "LINUX"; | |
11195 | return elfcore_write_note (abfd, buf, bufsiz, | |
11196 | note_name, NT_PPC_TM_CTAR, ppc_tm_ctar, size); | |
11197 | } | |
11198 | ||
11199 | char * | |
11200 | elfcore_write_ppc_tm_cppr (bfd *abfd, | |
11201 | char *buf, | |
11202 | int *bufsiz, | |
11203 | const void *ppc_tm_cppr, | |
11204 | int size) | |
11205 | { | |
11206 | char *note_name = "LINUX"; | |
11207 | return elfcore_write_note (abfd, buf, bufsiz, | |
11208 | note_name, NT_PPC_TM_CPPR, ppc_tm_cppr, size); | |
11209 | } | |
11210 | ||
11211 | char * | |
11212 | elfcore_write_ppc_tm_cdscr (bfd *abfd, | |
11213 | char *buf, | |
11214 | int *bufsiz, | |
11215 | const void *ppc_tm_cdscr, | |
11216 | int size) | |
11217 | { | |
11218 | char *note_name = "LINUX"; | |
11219 | return elfcore_write_note (abfd, buf, bufsiz, | |
11220 | note_name, NT_PPC_TM_CDSCR, ppc_tm_cdscr, size); | |
11221 | } | |
11222 | ||
0675e188 UW |
11223 | static char * |
11224 | elfcore_write_s390_high_gprs (bfd *abfd, | |
11225 | char *buf, | |
11226 | int *bufsiz, | |
11227 | const void *s390_high_gprs, | |
11228 | int size) | |
11229 | { | |
11230 | char *note_name = "LINUX"; | |
11231 | return elfcore_write_note (abfd, buf, bufsiz, | |
07d6d2b8 | 11232 | note_name, NT_S390_HIGH_GPRS, |
0675e188 UW |
11233 | s390_high_gprs, size); |
11234 | } | |
11235 | ||
d7eeb400 MS |
11236 | char * |
11237 | elfcore_write_s390_timer (bfd *abfd, | |
07d6d2b8 AM |
11238 | char *buf, |
11239 | int *bufsiz, | |
11240 | const void *s390_timer, | |
11241 | int size) | |
d7eeb400 MS |
11242 | { |
11243 | char *note_name = "LINUX"; | |
11244 | return elfcore_write_note (abfd, buf, bufsiz, | |
07d6d2b8 | 11245 | note_name, NT_S390_TIMER, s390_timer, size); |
d7eeb400 MS |
11246 | } |
11247 | ||
11248 | char * | |
11249 | elfcore_write_s390_todcmp (bfd *abfd, | |
07d6d2b8 AM |
11250 | char *buf, |
11251 | int *bufsiz, | |
11252 | const void *s390_todcmp, | |
11253 | int size) | |
d7eeb400 MS |
11254 | { |
11255 | char *note_name = "LINUX"; | |
11256 | return elfcore_write_note (abfd, buf, bufsiz, | |
07d6d2b8 | 11257 | note_name, NT_S390_TODCMP, s390_todcmp, size); |
d7eeb400 MS |
11258 | } |
11259 | ||
11260 | char * | |
11261 | elfcore_write_s390_todpreg (bfd *abfd, | |
07d6d2b8 AM |
11262 | char *buf, |
11263 | int *bufsiz, | |
11264 | const void *s390_todpreg, | |
11265 | int size) | |
d7eeb400 MS |
11266 | { |
11267 | char *note_name = "LINUX"; | |
11268 | return elfcore_write_note (abfd, buf, bufsiz, | |
07d6d2b8 | 11269 | note_name, NT_S390_TODPREG, s390_todpreg, size); |
d7eeb400 MS |
11270 | } |
11271 | ||
11272 | char * | |
11273 | elfcore_write_s390_ctrs (bfd *abfd, | |
07d6d2b8 AM |
11274 | char *buf, |
11275 | int *bufsiz, | |
11276 | const void *s390_ctrs, | |
11277 | int size) | |
d7eeb400 MS |
11278 | { |
11279 | char *note_name = "LINUX"; | |
11280 | return elfcore_write_note (abfd, buf, bufsiz, | |
07d6d2b8 | 11281 | note_name, NT_S390_CTRS, s390_ctrs, size); |
d7eeb400 MS |
11282 | } |
11283 | ||
11284 | char * | |
11285 | elfcore_write_s390_prefix (bfd *abfd, | |
07d6d2b8 AM |
11286 | char *buf, |
11287 | int *bufsiz, | |
11288 | const void *s390_prefix, | |
11289 | int size) | |
d7eeb400 MS |
11290 | { |
11291 | char *note_name = "LINUX"; | |
11292 | return elfcore_write_note (abfd, buf, bufsiz, | |
07d6d2b8 | 11293 | note_name, NT_S390_PREFIX, s390_prefix, size); |
d7eeb400 MS |
11294 | } |
11295 | ||
355b81d9 UW |
11296 | char * |
11297 | elfcore_write_s390_last_break (bfd *abfd, | |
11298 | char *buf, | |
11299 | int *bufsiz, | |
11300 | const void *s390_last_break, | |
11301 | int size) | |
11302 | { | |
11303 | char *note_name = "LINUX"; | |
11304 | return elfcore_write_note (abfd, buf, bufsiz, | |
07d6d2b8 | 11305 | note_name, NT_S390_LAST_BREAK, |
355b81d9 UW |
11306 | s390_last_break, size); |
11307 | } | |
11308 | ||
11309 | char * | |
11310 | elfcore_write_s390_system_call (bfd *abfd, | |
11311 | char *buf, | |
11312 | int *bufsiz, | |
11313 | const void *s390_system_call, | |
11314 | int size) | |
11315 | { | |
11316 | char *note_name = "LINUX"; | |
11317 | return elfcore_write_note (abfd, buf, bufsiz, | |
07d6d2b8 | 11318 | note_name, NT_S390_SYSTEM_CALL, |
355b81d9 UW |
11319 | s390_system_call, size); |
11320 | } | |
11321 | ||
abb3f6cc NC |
11322 | char * |
11323 | elfcore_write_s390_tdb (bfd *abfd, | |
11324 | char *buf, | |
11325 | int *bufsiz, | |
11326 | const void *s390_tdb, | |
11327 | int size) | |
11328 | { | |
11329 | char *note_name = "LINUX"; | |
11330 | return elfcore_write_note (abfd, buf, bufsiz, | |
07d6d2b8 | 11331 | note_name, NT_S390_TDB, s390_tdb, size); |
abb3f6cc NC |
11332 | } |
11333 | ||
4ef9f41a AA |
11334 | char * |
11335 | elfcore_write_s390_vxrs_low (bfd *abfd, | |
11336 | char *buf, | |
11337 | int *bufsiz, | |
11338 | const void *s390_vxrs_low, | |
11339 | int size) | |
11340 | { | |
11341 | char *note_name = "LINUX"; | |
11342 | return elfcore_write_note (abfd, buf, bufsiz, | |
11343 | note_name, NT_S390_VXRS_LOW, s390_vxrs_low, size); | |
11344 | } | |
11345 | ||
11346 | char * | |
11347 | elfcore_write_s390_vxrs_high (bfd *abfd, | |
11348 | char *buf, | |
11349 | int *bufsiz, | |
11350 | const void *s390_vxrs_high, | |
11351 | int size) | |
11352 | { | |
11353 | char *note_name = "LINUX"; | |
11354 | return elfcore_write_note (abfd, buf, bufsiz, | |
11355 | note_name, NT_S390_VXRS_HIGH, | |
11356 | s390_vxrs_high, size); | |
11357 | } | |
11358 | ||
88ab90e8 AA |
11359 | char * |
11360 | elfcore_write_s390_gs_cb (bfd *abfd, | |
11361 | char *buf, | |
11362 | int *bufsiz, | |
11363 | const void *s390_gs_cb, | |
11364 | int size) | |
11365 | { | |
11366 | char *note_name = "LINUX"; | |
11367 | return elfcore_write_note (abfd, buf, bufsiz, | |
11368 | note_name, NT_S390_GS_CB, | |
11369 | s390_gs_cb, size); | |
11370 | } | |
11371 | ||
11372 | char * | |
11373 | elfcore_write_s390_gs_bc (bfd *abfd, | |
11374 | char *buf, | |
11375 | int *bufsiz, | |
11376 | const void *s390_gs_bc, | |
11377 | int size) | |
11378 | { | |
11379 | char *note_name = "LINUX"; | |
11380 | return elfcore_write_note (abfd, buf, bufsiz, | |
11381 | note_name, NT_S390_GS_BC, | |
11382 | s390_gs_bc, size); | |
11383 | } | |
11384 | ||
faa9a424 UW |
11385 | char * |
11386 | elfcore_write_arm_vfp (bfd *abfd, | |
11387 | char *buf, | |
11388 | int *bufsiz, | |
11389 | const void *arm_vfp, | |
11390 | int size) | |
11391 | { | |
11392 | char *note_name = "LINUX"; | |
11393 | return elfcore_write_note (abfd, buf, bufsiz, | |
11394 | note_name, NT_ARM_VFP, arm_vfp, size); | |
11395 | } | |
11396 | ||
652451f8 YZ |
11397 | char * |
11398 | elfcore_write_aarch_tls (bfd *abfd, | |
11399 | char *buf, | |
11400 | int *bufsiz, | |
11401 | const void *aarch_tls, | |
11402 | int size) | |
11403 | { | |
11404 | char *note_name = "LINUX"; | |
11405 | return elfcore_write_note (abfd, buf, bufsiz, | |
11406 | note_name, NT_ARM_TLS, aarch_tls, size); | |
11407 | } | |
11408 | ||
11409 | char * | |
11410 | elfcore_write_aarch_hw_break (bfd *abfd, | |
11411 | char *buf, | |
11412 | int *bufsiz, | |
11413 | const void *aarch_hw_break, | |
11414 | int size) | |
11415 | { | |
11416 | char *note_name = "LINUX"; | |
11417 | return elfcore_write_note (abfd, buf, bufsiz, | |
11418 | note_name, NT_ARM_HW_BREAK, aarch_hw_break, size); | |
11419 | } | |
11420 | ||
11421 | char * | |
11422 | elfcore_write_aarch_hw_watch (bfd *abfd, | |
11423 | char *buf, | |
11424 | int *bufsiz, | |
11425 | const void *aarch_hw_watch, | |
11426 | int size) | |
11427 | { | |
11428 | char *note_name = "LINUX"; | |
11429 | return elfcore_write_note (abfd, buf, bufsiz, | |
11430 | note_name, NT_ARM_HW_WATCH, aarch_hw_watch, size); | |
11431 | } | |
11432 | ||
ad1cc4e4 AH |
11433 | char * |
11434 | elfcore_write_aarch_sve (bfd *abfd, | |
11435 | char *buf, | |
11436 | int *bufsiz, | |
11437 | const void *aarch_sve, | |
11438 | int size) | |
11439 | { | |
11440 | char *note_name = "LINUX"; | |
11441 | return elfcore_write_note (abfd, buf, bufsiz, | |
11442 | note_name, NT_ARM_SVE, aarch_sve, size); | |
11443 | } | |
11444 | ||
bb864ac1 CES |
11445 | char * |
11446 | elfcore_write_register_note (bfd *abfd, | |
11447 | char *buf, | |
11448 | int *bufsiz, | |
11449 | const char *section, | |
11450 | const void *data, | |
11451 | int size) | |
11452 | { | |
11453 | if (strcmp (section, ".reg2") == 0) | |
11454 | return elfcore_write_prfpreg (abfd, buf, bufsiz, data, size); | |
11455 | if (strcmp (section, ".reg-xfp") == 0) | |
11456 | return elfcore_write_prxfpreg (abfd, buf, bufsiz, data, size); | |
4339cae0 L |
11457 | if (strcmp (section, ".reg-xstate") == 0) |
11458 | return elfcore_write_xstatereg (abfd, buf, bufsiz, data, size); | |
bb864ac1 CES |
11459 | if (strcmp (section, ".reg-ppc-vmx") == 0) |
11460 | return elfcore_write_ppc_vmx (abfd, buf, bufsiz, data, size); | |
89eeb0bc LM |
11461 | if (strcmp (section, ".reg-ppc-vsx") == 0) |
11462 | return elfcore_write_ppc_vsx (abfd, buf, bufsiz, data, size); | |
cb2366c1 EBM |
11463 | if (strcmp (section, ".reg-ppc-tar") == 0) |
11464 | return elfcore_write_ppc_tar (abfd, buf, bufsiz, data, size); | |
11465 | if (strcmp (section, ".reg-ppc-ppr") == 0) | |
11466 | return elfcore_write_ppc_ppr (abfd, buf, bufsiz, data, size); | |
11467 | if (strcmp (section, ".reg-ppc-dscr") == 0) | |
11468 | return elfcore_write_ppc_dscr (abfd, buf, bufsiz, data, size); | |
11469 | if (strcmp (section, ".reg-ppc-ebb") == 0) | |
11470 | return elfcore_write_ppc_ebb (abfd, buf, bufsiz, data, size); | |
11471 | if (strcmp (section, ".reg-ppc-pmu") == 0) | |
11472 | return elfcore_write_ppc_pmu (abfd, buf, bufsiz, data, size); | |
11473 | if (strcmp (section, ".reg-ppc-tm-cgpr") == 0) | |
11474 | return elfcore_write_ppc_tm_cgpr (abfd, buf, bufsiz, data, size); | |
11475 | if (strcmp (section, ".reg-ppc-tm-cfpr") == 0) | |
11476 | return elfcore_write_ppc_tm_cfpr (abfd, buf, bufsiz, data, size); | |
11477 | if (strcmp (section, ".reg-ppc-tm-cvmx") == 0) | |
11478 | return elfcore_write_ppc_tm_cvmx (abfd, buf, bufsiz, data, size); | |
11479 | if (strcmp (section, ".reg-ppc-tm-cvsx") == 0) | |
11480 | return elfcore_write_ppc_tm_cvsx (abfd, buf, bufsiz, data, size); | |
11481 | if (strcmp (section, ".reg-ppc-tm-spr") == 0) | |
11482 | return elfcore_write_ppc_tm_spr (abfd, buf, bufsiz, data, size); | |
11483 | if (strcmp (section, ".reg-ppc-tm-ctar") == 0) | |
11484 | return elfcore_write_ppc_tm_ctar (abfd, buf, bufsiz, data, size); | |
11485 | if (strcmp (section, ".reg-ppc-tm-cppr") == 0) | |
11486 | return elfcore_write_ppc_tm_cppr (abfd, buf, bufsiz, data, size); | |
11487 | if (strcmp (section, ".reg-ppc-tm-cdscr") == 0) | |
11488 | return elfcore_write_ppc_tm_cdscr (abfd, buf, bufsiz, data, size); | |
0675e188 UW |
11489 | if (strcmp (section, ".reg-s390-high-gprs") == 0) |
11490 | return elfcore_write_s390_high_gprs (abfd, buf, bufsiz, data, size); | |
d7eeb400 MS |
11491 | if (strcmp (section, ".reg-s390-timer") == 0) |
11492 | return elfcore_write_s390_timer (abfd, buf, bufsiz, data, size); | |
11493 | if (strcmp (section, ".reg-s390-todcmp") == 0) | |
11494 | return elfcore_write_s390_todcmp (abfd, buf, bufsiz, data, size); | |
11495 | if (strcmp (section, ".reg-s390-todpreg") == 0) | |
11496 | return elfcore_write_s390_todpreg (abfd, buf, bufsiz, data, size); | |
11497 | if (strcmp (section, ".reg-s390-ctrs") == 0) | |
11498 | return elfcore_write_s390_ctrs (abfd, buf, bufsiz, data, size); | |
11499 | if (strcmp (section, ".reg-s390-prefix") == 0) | |
11500 | return elfcore_write_s390_prefix (abfd, buf, bufsiz, data, size); | |
355b81d9 UW |
11501 | if (strcmp (section, ".reg-s390-last-break") == 0) |
11502 | return elfcore_write_s390_last_break (abfd, buf, bufsiz, data, size); | |
11503 | if (strcmp (section, ".reg-s390-system-call") == 0) | |
11504 | return elfcore_write_s390_system_call (abfd, buf, bufsiz, data, size); | |
abb3f6cc NC |
11505 | if (strcmp (section, ".reg-s390-tdb") == 0) |
11506 | return elfcore_write_s390_tdb (abfd, buf, bufsiz, data, size); | |
4ef9f41a AA |
11507 | if (strcmp (section, ".reg-s390-vxrs-low") == 0) |
11508 | return elfcore_write_s390_vxrs_low (abfd, buf, bufsiz, data, size); | |
11509 | if (strcmp (section, ".reg-s390-vxrs-high") == 0) | |
11510 | return elfcore_write_s390_vxrs_high (abfd, buf, bufsiz, data, size); | |
88ab90e8 AA |
11511 | if (strcmp (section, ".reg-s390-gs-cb") == 0) |
11512 | return elfcore_write_s390_gs_cb (abfd, buf, bufsiz, data, size); | |
11513 | if (strcmp (section, ".reg-s390-gs-bc") == 0) | |
11514 | return elfcore_write_s390_gs_bc (abfd, buf, bufsiz, data, size); | |
faa9a424 UW |
11515 | if (strcmp (section, ".reg-arm-vfp") == 0) |
11516 | return elfcore_write_arm_vfp (abfd, buf, bufsiz, data, size); | |
652451f8 YZ |
11517 | if (strcmp (section, ".reg-aarch-tls") == 0) |
11518 | return elfcore_write_aarch_tls (abfd, buf, bufsiz, data, size); | |
11519 | if (strcmp (section, ".reg-aarch-hw-break") == 0) | |
11520 | return elfcore_write_aarch_hw_break (abfd, buf, bufsiz, data, size); | |
11521 | if (strcmp (section, ".reg-aarch-hw-watch") == 0) | |
11522 | return elfcore_write_aarch_hw_watch (abfd, buf, bufsiz, data, size); | |
ad1cc4e4 AH |
11523 | if (strcmp (section, ".reg-aarch-sve") == 0) |
11524 | return elfcore_write_aarch_sve (abfd, buf, bufsiz, data, size); | |
bb864ac1 CES |
11525 | return NULL; |
11526 | } | |
11527 | ||
b34976b6 | 11528 | static bfd_boolean |
276da9b3 L |
11529 | elf_parse_notes (bfd *abfd, char *buf, size_t size, file_ptr offset, |
11530 | size_t align) | |
252b5132 | 11531 | { |
c044fabd | 11532 | char *p; |
252b5132 | 11533 | |
276da9b3 L |
11534 | /* NB: CORE PT_NOTE segments may have p_align values of 0 or 1. |
11535 | gABI specifies that PT_NOTE alignment should be aligned to 4 | |
11536 | bytes for 32-bit objects and to 8 bytes for 64-bit objects. If | |
11537 | align is less than 4, we use 4 byte alignment. */ | |
11538 | if (align < 4) | |
11539 | align = 4; | |
ef135d43 NC |
11540 | if (align != 4 && align != 8) |
11541 | return FALSE; | |
276da9b3 | 11542 | |
252b5132 RH |
11543 | p = buf; |
11544 | while (p < buf + size) | |
11545 | { | |
c044fabd | 11546 | Elf_External_Note *xnp = (Elf_External_Note *) p; |
252b5132 RH |
11547 | Elf_Internal_Note in; |
11548 | ||
baea7ef1 AM |
11549 | if (offsetof (Elf_External_Note, name) > buf - p + size) |
11550 | return FALSE; | |
11551 | ||
dc810e39 | 11552 | in.type = H_GET_32 (abfd, xnp->type); |
252b5132 | 11553 | |
dc810e39 | 11554 | in.namesz = H_GET_32 (abfd, xnp->namesz); |
252b5132 | 11555 | in.namedata = xnp->name; |
baea7ef1 AM |
11556 | if (in.namesz > buf - in.namedata + size) |
11557 | return FALSE; | |
252b5132 | 11558 | |
dc810e39 | 11559 | in.descsz = H_GET_32 (abfd, xnp->descsz); |
276da9b3 | 11560 | in.descdata = p + ELF_NOTE_DESC_OFFSET (in.namesz, align); |
252b5132 | 11561 | in.descpos = offset + (in.descdata - buf); |
baea7ef1 AM |
11562 | if (in.descsz != 0 |
11563 | && (in.descdata >= buf + size | |
11564 | || in.descsz > buf - in.descdata + size)) | |
11565 | return FALSE; | |
252b5132 | 11566 | |
718175fa | 11567 | switch (bfd_get_format (abfd)) |
07d6d2b8 | 11568 | { |
718175fa JK |
11569 | default: |
11570 | return TRUE; | |
11571 | ||
11572 | case bfd_core: | |
f64e188b | 11573 | { |
8acbedd6 | 11574 | #define GROKER_ELEMENT(S,F) {S, sizeof (S) - 1, F} |
f64e188b | 11575 | struct |
718175fa | 11576 | { |
f64e188b | 11577 | const char * string; |
8acbedd6 | 11578 | size_t len; |
f64e188b | 11579 | bfd_boolean (* func)(bfd *, Elf_Internal_Note *); |
718175fa | 11580 | } |
f64e188b | 11581 | grokers[] = |
b15fa79e | 11582 | { |
8acbedd6 | 11583 | GROKER_ELEMENT ("", elfcore_grok_note), |
aa1ed4a9 | 11584 | GROKER_ELEMENT ("FreeBSD", elfcore_grok_freebsd_note), |
8acbedd6 KS |
11585 | GROKER_ELEMENT ("NetBSD-CORE", elfcore_grok_netbsd_note), |
11586 | GROKER_ELEMENT ( "OpenBSD", elfcore_grok_openbsd_note), | |
11587 | GROKER_ELEMENT ("QNX", elfcore_grok_nto_note), | |
11588 | GROKER_ELEMENT ("SPU/", elfcore_grok_spu_note) | |
f64e188b | 11589 | }; |
8acbedd6 | 11590 | #undef GROKER_ELEMENT |
f64e188b NC |
11591 | int i; |
11592 | ||
11593 | for (i = ARRAY_SIZE (grokers); i--;) | |
8acbedd6 KS |
11594 | { |
11595 | if (in.namesz >= grokers[i].len | |
11596 | && strncmp (in.namedata, grokers[i].string, | |
11597 | grokers[i].len) == 0) | |
11598 | { | |
11599 | if (! grokers[i].func (abfd, & in)) | |
11600 | return FALSE; | |
11601 | break; | |
11602 | } | |
11603 | } | |
f64e188b NC |
11604 | break; |
11605 | } | |
718175fa JK |
11606 | |
11607 | case bfd_object: | |
11608 | if (in.namesz == sizeof "GNU" && strcmp (in.namedata, "GNU") == 0) | |
11609 | { | |
11610 | if (! elfobj_grok_gnu_note (abfd, &in)) | |
11611 | return FALSE; | |
11612 | } | |
e21e5835 NC |
11613 | else if (in.namesz == sizeof "stapsdt" |
11614 | && strcmp (in.namedata, "stapsdt") == 0) | |
11615 | { | |
11616 | if (! elfobj_grok_stapsdt_note (abfd, &in)) | |
11617 | return FALSE; | |
11618 | } | |
718175fa | 11619 | break; |
08a40648 | 11620 | } |
252b5132 | 11621 | |
276da9b3 | 11622 | p += ELF_NOTE_NEXT_OFFSET (in.namesz, in.descsz, align); |
252b5132 RH |
11623 | } |
11624 | ||
718175fa JK |
11625 | return TRUE; |
11626 | } | |
11627 | ||
11628 | static bfd_boolean | |
276da9b3 L |
11629 | elf_read_notes (bfd *abfd, file_ptr offset, bfd_size_type size, |
11630 | size_t align) | |
718175fa JK |
11631 | { |
11632 | char *buf; | |
11633 | ||
957e1fc1 | 11634 | if (size == 0 || (size + 1) == 0) |
718175fa JK |
11635 | return TRUE; |
11636 | ||
11637 | if (bfd_seek (abfd, offset, SEEK_SET) != 0) | |
11638 | return FALSE; | |
11639 | ||
f64e188b | 11640 | buf = (char *) bfd_malloc (size + 1); |
718175fa JK |
11641 | if (buf == NULL) |
11642 | return FALSE; | |
11643 | ||
f64e188b NC |
11644 | /* PR 17512: file: ec08f814 |
11645 | 0-termintate the buffer so that string searches will not overflow. */ | |
11646 | buf[size] = 0; | |
11647 | ||
718175fa | 11648 | if (bfd_bread (buf, size, abfd) != size |
276da9b3 | 11649 | || !elf_parse_notes (abfd, buf, size, offset, align)) |
718175fa JK |
11650 | { |
11651 | free (buf); | |
11652 | return FALSE; | |
11653 | } | |
11654 | ||
252b5132 | 11655 | free (buf); |
b34976b6 | 11656 | return TRUE; |
252b5132 | 11657 | } |
98d8431c JB |
11658 | \f |
11659 | /* Providing external access to the ELF program header table. */ | |
11660 | ||
11661 | /* Return an upper bound on the number of bytes required to store a | |
11662 | copy of ABFD's program header table entries. Return -1 if an error | |
11663 | occurs; bfd_get_error will return an appropriate code. */ | |
c044fabd | 11664 | |
98d8431c | 11665 | long |
217aa764 | 11666 | bfd_get_elf_phdr_upper_bound (bfd *abfd) |
98d8431c JB |
11667 | { |
11668 | if (abfd->xvec->flavour != bfd_target_elf_flavour) | |
11669 | { | |
11670 | bfd_set_error (bfd_error_wrong_format); | |
11671 | return -1; | |
11672 | } | |
11673 | ||
936e320b | 11674 | return elf_elfheader (abfd)->e_phnum * sizeof (Elf_Internal_Phdr); |
98d8431c JB |
11675 | } |
11676 | ||
98d8431c JB |
11677 | /* Copy ABFD's program header table entries to *PHDRS. The entries |
11678 | will be stored as an array of Elf_Internal_Phdr structures, as | |
11679 | defined in include/elf/internal.h. To find out how large the | |
11680 | buffer needs to be, call bfd_get_elf_phdr_upper_bound. | |
11681 | ||
11682 | Return the number of program header table entries read, or -1 if an | |
11683 | error occurs; bfd_get_error will return an appropriate code. */ | |
c044fabd | 11684 | |
98d8431c | 11685 | int |
217aa764 | 11686 | bfd_get_elf_phdrs (bfd *abfd, void *phdrs) |
98d8431c JB |
11687 | { |
11688 | int num_phdrs; | |
11689 | ||
11690 | if (abfd->xvec->flavour != bfd_target_elf_flavour) | |
11691 | { | |
11692 | bfd_set_error (bfd_error_wrong_format); | |
11693 | return -1; | |
11694 | } | |
11695 | ||
11696 | num_phdrs = elf_elfheader (abfd)->e_phnum; | |
01bcaf63 TT |
11697 | if (num_phdrs != 0) |
11698 | memcpy (phdrs, elf_tdata (abfd)->phdr, | |
11699 | num_phdrs * sizeof (Elf_Internal_Phdr)); | |
98d8431c JB |
11700 | |
11701 | return num_phdrs; | |
11702 | } | |
ae4221d7 | 11703 | |
db6751f2 | 11704 | enum elf_reloc_type_class |
7e612e98 AM |
11705 | _bfd_elf_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED, |
11706 | const asection *rel_sec ATTRIBUTE_UNUSED, | |
11707 | const Elf_Internal_Rela *rela ATTRIBUTE_UNUSED) | |
db6751f2 JJ |
11708 | { |
11709 | return reloc_class_normal; | |
11710 | } | |
f8df10f4 | 11711 | |
47d9a591 | 11712 | /* For RELA architectures, return the relocation value for a |
f8df10f4 JJ |
11713 | relocation against a local symbol. */ |
11714 | ||
11715 | bfd_vma | |
217aa764 AM |
11716 | _bfd_elf_rela_local_sym (bfd *abfd, |
11717 | Elf_Internal_Sym *sym, | |
8517fae7 | 11718 | asection **psec, |
217aa764 | 11719 | Elf_Internal_Rela *rel) |
f8df10f4 | 11720 | { |
8517fae7 | 11721 | asection *sec = *psec; |
f8df10f4 JJ |
11722 | bfd_vma relocation; |
11723 | ||
11724 | relocation = (sec->output_section->vma | |
11725 | + sec->output_offset | |
11726 | + sym->st_value); | |
11727 | if ((sec->flags & SEC_MERGE) | |
c629eae0 | 11728 | && ELF_ST_TYPE (sym->st_info) == STT_SECTION |
dbaa2011 | 11729 | && sec->sec_info_type == SEC_INFO_TYPE_MERGE) |
f8df10f4 | 11730 | { |
f8df10f4 | 11731 | rel->r_addend = |
8517fae7 | 11732 | _bfd_merged_section_offset (abfd, psec, |
65765700 | 11733 | elf_section_data (sec)->sec_info, |
753731ee AM |
11734 | sym->st_value + rel->r_addend); |
11735 | if (sec != *psec) | |
11736 | { | |
11737 | /* If we have changed the section, and our original section is | |
11738 | marked with SEC_EXCLUDE, it means that the original | |
11739 | SEC_MERGE section has been completely subsumed in some | |
11740 | other SEC_MERGE section. In this case, we need to leave | |
11741 | some info around for --emit-relocs. */ | |
11742 | if ((sec->flags & SEC_EXCLUDE) != 0) | |
11743 | sec->kept_section = *psec; | |
11744 | sec = *psec; | |
11745 | } | |
8517fae7 AM |
11746 | rel->r_addend -= relocation; |
11747 | rel->r_addend += sec->output_section->vma + sec->output_offset; | |
f8df10f4 JJ |
11748 | } |
11749 | return relocation; | |
11750 | } | |
c629eae0 JJ |
11751 | |
11752 | bfd_vma | |
217aa764 AM |
11753 | _bfd_elf_rel_local_sym (bfd *abfd, |
11754 | Elf_Internal_Sym *sym, | |
11755 | asection **psec, | |
11756 | bfd_vma addend) | |
47d9a591 | 11757 | { |
c629eae0 JJ |
11758 | asection *sec = *psec; |
11759 | ||
dbaa2011 | 11760 | if (sec->sec_info_type != SEC_INFO_TYPE_MERGE) |
c629eae0 JJ |
11761 | return sym->st_value + addend; |
11762 | ||
11763 | return _bfd_merged_section_offset (abfd, psec, | |
65765700 | 11764 | elf_section_data (sec)->sec_info, |
753731ee | 11765 | sym->st_value + addend); |
c629eae0 JJ |
11766 | } |
11767 | ||
37b01f6a DG |
11768 | /* Adjust an address within a section. Given OFFSET within SEC, return |
11769 | the new offset within the section, based upon changes made to the | |
11770 | section. Returns -1 if the offset is now invalid. | |
11771 | The offset (in abnd out) is in target sized bytes, however big a | |
11772 | byte may be. */ | |
11773 | ||
c629eae0 | 11774 | bfd_vma |
217aa764 | 11775 | _bfd_elf_section_offset (bfd *abfd, |
92e4ec35 | 11776 | struct bfd_link_info *info, |
217aa764 AM |
11777 | asection *sec, |
11778 | bfd_vma offset) | |
c629eae0 | 11779 | { |
68bfbfcc | 11780 | switch (sec->sec_info_type) |
65765700 | 11781 | { |
dbaa2011 | 11782 | case SEC_INFO_TYPE_STABS: |
eea6121a AM |
11783 | return _bfd_stab_section_offset (sec, elf_section_data (sec)->sec_info, |
11784 | offset); | |
dbaa2011 | 11785 | case SEC_INFO_TYPE_EH_FRAME: |
92e4ec35 | 11786 | return _bfd_elf_eh_frame_section_offset (abfd, info, sec, offset); |
37b01f6a | 11787 | |
65765700 | 11788 | default: |
310fd250 L |
11789 | if ((sec->flags & SEC_ELF_REVERSE_COPY) != 0) |
11790 | { | |
37b01f6a | 11791 | /* Reverse the offset. */ |
310fd250 L |
11792 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
11793 | bfd_size_type address_size = bed->s->arch_size / 8; | |
37b01f6a DG |
11794 | |
11795 | /* address_size and sec->size are in octets. Convert | |
11796 | to bytes before subtracting the original offset. */ | |
11797 | offset = (sec->size - address_size) / bfd_octets_per_byte (abfd) - offset; | |
310fd250 | 11798 | } |
65765700 JJ |
11799 | return offset; |
11800 | } | |
c629eae0 | 11801 | } |
3333a7c3 RM |
11802 | \f |
11803 | /* Create a new BFD as if by bfd_openr. Rather than opening a file, | |
11804 | reconstruct an ELF file by reading the segments out of remote memory | |
11805 | based on the ELF file header at EHDR_VMA and the ELF program headers it | |
11806 | points to. If not null, *LOADBASEP is filled in with the difference | |
11807 | between the VMAs from which the segments were read, and the VMAs the | |
11808 | file headers (and hence BFD's idea of each section's VMA) put them at. | |
11809 | ||
11810 | The function TARGET_READ_MEMORY is called to copy LEN bytes from the | |
11811 | remote memory at target address VMA into the local buffer at MYADDR; it | |
11812 | should return zero on success or an `errno' code on failure. TEMPL must | |
11813 | be a BFD for an ELF target with the word size and byte order found in | |
11814 | the remote memory. */ | |
11815 | ||
11816 | bfd * | |
217aa764 AM |
11817 | bfd_elf_bfd_from_remote_memory |
11818 | (bfd *templ, | |
11819 | bfd_vma ehdr_vma, | |
f0a5d95a | 11820 | bfd_size_type size, |
217aa764 | 11821 | bfd_vma *loadbasep, |
fe78531d | 11822 | int (*target_read_memory) (bfd_vma, bfd_byte *, bfd_size_type)) |
3333a7c3 RM |
11823 | { |
11824 | return (*get_elf_backend_data (templ)->elf_backend_bfd_from_remote_memory) | |
5979d6b6 | 11825 | (templ, ehdr_vma, size, loadbasep, target_read_memory); |
3333a7c3 | 11826 | } |
4c45e5c9 JJ |
11827 | \f |
11828 | long | |
c9727e01 AM |
11829 | _bfd_elf_get_synthetic_symtab (bfd *abfd, |
11830 | long symcount ATTRIBUTE_UNUSED, | |
11831 | asymbol **syms ATTRIBUTE_UNUSED, | |
8615f3f2 | 11832 | long dynsymcount, |
c9727e01 AM |
11833 | asymbol **dynsyms, |
11834 | asymbol **ret) | |
4c45e5c9 JJ |
11835 | { |
11836 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
11837 | asection *relplt; | |
11838 | asymbol *s; | |
11839 | const char *relplt_name; | |
11840 | bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean); | |
11841 | arelent *p; | |
11842 | long count, i, n; | |
11843 | size_t size; | |
11844 | Elf_Internal_Shdr *hdr; | |
11845 | char *names; | |
11846 | asection *plt; | |
11847 | ||
8615f3f2 AM |
11848 | *ret = NULL; |
11849 | ||
90e3cdf2 JJ |
11850 | if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0) |
11851 | return 0; | |
11852 | ||
8615f3f2 AM |
11853 | if (dynsymcount <= 0) |
11854 | return 0; | |
11855 | ||
4c45e5c9 JJ |
11856 | if (!bed->plt_sym_val) |
11857 | return 0; | |
11858 | ||
11859 | relplt_name = bed->relplt_name; | |
11860 | if (relplt_name == NULL) | |
d35fd659 | 11861 | relplt_name = bed->rela_plts_and_copies_p ? ".rela.plt" : ".rel.plt"; |
4c45e5c9 JJ |
11862 | relplt = bfd_get_section_by_name (abfd, relplt_name); |
11863 | if (relplt == NULL) | |
11864 | return 0; | |
11865 | ||
11866 | hdr = &elf_section_data (relplt)->this_hdr; | |
11867 | if (hdr->sh_link != elf_dynsymtab (abfd) | |
11868 | || (hdr->sh_type != SHT_REL && hdr->sh_type != SHT_RELA)) | |
11869 | return 0; | |
11870 | ||
11871 | plt = bfd_get_section_by_name (abfd, ".plt"); | |
11872 | if (plt == NULL) | |
11873 | return 0; | |
11874 | ||
11875 | slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table; | |
c9727e01 | 11876 | if (! (*slurp_relocs) (abfd, relplt, dynsyms, TRUE)) |
4c45e5c9 JJ |
11877 | return -1; |
11878 | ||
eea6121a | 11879 | count = relplt->size / hdr->sh_entsize; |
4c45e5c9 JJ |
11880 | size = count * sizeof (asymbol); |
11881 | p = relplt->relocation; | |
cb53bf42 | 11882 | for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel) |
041de40d AM |
11883 | { |
11884 | size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt"); | |
11885 | if (p->addend != 0) | |
11886 | { | |
11887 | #ifdef BFD64 | |
11888 | size += sizeof ("+0x") - 1 + 8 + 8 * (bed->s->elfclass == ELFCLASS64); | |
11889 | #else | |
11890 | size += sizeof ("+0x") - 1 + 8; | |
11891 | #endif | |
11892 | } | |
11893 | } | |
4c45e5c9 | 11894 | |
a50b1753 | 11895 | s = *ret = (asymbol *) bfd_malloc (size); |
4c45e5c9 JJ |
11896 | if (s == NULL) |
11897 | return -1; | |
11898 | ||
11899 | names = (char *) (s + count); | |
11900 | p = relplt->relocation; | |
11901 | n = 0; | |
cb53bf42 | 11902 | for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel) |
4c45e5c9 JJ |
11903 | { |
11904 | size_t len; | |
11905 | bfd_vma addr; | |
11906 | ||
11907 | addr = bed->plt_sym_val (i, plt, p); | |
11908 | if (addr == (bfd_vma) -1) | |
11909 | continue; | |
11910 | ||
11911 | *s = **p->sym_ptr_ptr; | |
65a7a66f AM |
11912 | /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since |
11913 | we are defining a symbol, ensure one of them is set. */ | |
11914 | if ((s->flags & BSF_LOCAL) == 0) | |
11915 | s->flags |= BSF_GLOBAL; | |
6ba2a415 | 11916 | s->flags |= BSF_SYNTHETIC; |
4c45e5c9 JJ |
11917 | s->section = plt; |
11918 | s->value = addr - plt->vma; | |
11919 | s->name = names; | |
8f39ba8e | 11920 | s->udata.p = NULL; |
4c45e5c9 JJ |
11921 | len = strlen ((*p->sym_ptr_ptr)->name); |
11922 | memcpy (names, (*p->sym_ptr_ptr)->name, len); | |
11923 | names += len; | |
041de40d AM |
11924 | if (p->addend != 0) |
11925 | { | |
1d770845 | 11926 | char buf[30], *a; |
d324f6d6 | 11927 | |
041de40d AM |
11928 | memcpy (names, "+0x", sizeof ("+0x") - 1); |
11929 | names += sizeof ("+0x") - 1; | |
1d770845 L |
11930 | bfd_sprintf_vma (abfd, buf, p->addend); |
11931 | for (a = buf; *a == '0'; ++a) | |
11932 | ; | |
11933 | len = strlen (a); | |
11934 | memcpy (names, a, len); | |
11935 | names += len; | |
041de40d | 11936 | } |
4c45e5c9 JJ |
11937 | memcpy (names, "@plt", sizeof ("@plt")); |
11938 | names += sizeof ("@plt"); | |
8f39ba8e | 11939 | ++s, ++n; |
4c45e5c9 JJ |
11940 | } |
11941 | ||
11942 | return n; | |
11943 | } | |
3d7f7666 | 11944 | |
821e6ff6 AM |
11945 | /* It is only used by x86-64 so far. |
11946 | ??? This repeats *COM* id of zero. sec->id is supposed to be unique, | |
7eacd66b AM |
11947 | but current usage would allow all of _bfd_std_section to be zero. */ |
11948 | static const asymbol lcomm_sym | |
11949 | = GLOBAL_SYM_INIT ("LARGE_COMMON", &_bfd_elf_large_com_section); | |
3b22753a | 11950 | asection _bfd_elf_large_com_section |
7eacd66b | 11951 | = BFD_FAKE_SECTION (_bfd_elf_large_com_section, &lcomm_sym, |
821e6ff6 | 11952 | "LARGE_COMMON", 0, SEC_IS_COMMON); |
ecca9871 | 11953 | |
d1036acb | 11954 | void |
78245035 L |
11955 | _bfd_elf_post_process_headers (bfd * abfd, |
11956 | struct bfd_link_info * link_info ATTRIBUTE_UNUSED) | |
d1036acb L |
11957 | { |
11958 | Elf_Internal_Ehdr * i_ehdrp; /* ELF file header, internal form. */ | |
11959 | ||
11960 | i_ehdrp = elf_elfheader (abfd); | |
11961 | ||
11962 | i_ehdrp->e_ident[EI_OSABI] = get_elf_backend_data (abfd)->elf_osabi; | |
d8045f23 NC |
11963 | |
11964 | /* To make things simpler for the loader on Linux systems we set the | |
9c55345c | 11965 | osabi field to ELFOSABI_GNU if the binary contains symbols of |
f64b2e8d | 11966 | the STT_GNU_IFUNC type or STB_GNU_UNIQUE binding. */ |
d8045f23 | 11967 | if (i_ehdrp->e_ident[EI_OSABI] == ELFOSABI_NONE |
f64b2e8d | 11968 | && elf_tdata (abfd)->has_gnu_symbols) |
9c55345c | 11969 | i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_GNU; |
d1036acb | 11970 | } |
fcb93ecf PB |
11971 | |
11972 | ||
11973 | /* Return TRUE for ELF symbol types that represent functions. | |
11974 | This is the default version of this function, which is sufficient for | |
d8045f23 | 11975 | most targets. It returns true if TYPE is STT_FUNC or STT_GNU_IFUNC. */ |
fcb93ecf PB |
11976 | |
11977 | bfd_boolean | |
11978 | _bfd_elf_is_function_type (unsigned int type) | |
11979 | { | |
d8045f23 NC |
11980 | return (type == STT_FUNC |
11981 | || type == STT_GNU_IFUNC); | |
fcb93ecf | 11982 | } |
9f296da3 | 11983 | |
aef36ac1 AM |
11984 | /* If the ELF symbol SYM might be a function in SEC, return the |
11985 | function size and set *CODE_OFF to the function's entry point, | |
11986 | otherwise return zero. */ | |
9f296da3 | 11987 | |
aef36ac1 AM |
11988 | bfd_size_type |
11989 | _bfd_elf_maybe_function_sym (const asymbol *sym, asection *sec, | |
11990 | bfd_vma *code_off) | |
9f296da3 | 11991 | { |
aef36ac1 AM |
11992 | bfd_size_type size; |
11993 | ||
ff9e0f5b | 11994 | if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT |
aef36ac1 AM |
11995 | | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0 |
11996 | || sym->section != sec) | |
11997 | return 0; | |
ff9e0f5b | 11998 | |
ff9e0f5b | 11999 | *code_off = sym->value; |
aef36ac1 AM |
12000 | size = 0; |
12001 | if (!(sym->flags & BSF_SYNTHETIC)) | |
12002 | size = ((elf_symbol_type *) sym)->internal_elf_sym.st_size; | |
12003 | if (size == 0) | |
12004 | size = 1; | |
12005 | return size; | |
9f296da3 | 12006 | } |