Commit | Line | Data |
---|---|---|
252b5132 | 1 | /* ELF executable support for BFD. |
340b6d91 | 2 | |
b90efa5b | 3 | Copyright (C) 1993-2015 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" |
70a38d42 | 45 | #include "elf-linux-psinfo.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 AM |
53 | static bfd_boolean prep_headers (bfd *); |
54 | static bfd_boolean swap_out_syms (bfd *, struct bfd_strtab_hash **, int) ; | |
718175fa JK |
55 | static bfd_boolean elf_read_notes (bfd *, file_ptr, bfd_size_type) ; |
56 | static bfd_boolean elf_parse_notes (bfd *abfd, char *buf, size_t size, | |
57 | file_ptr offset); | |
50b2bdb7 | 58 | |
252b5132 RH |
59 | /* Swap version information in and out. The version information is |
60 | currently size independent. If that ever changes, this code will | |
61 | need to move into elfcode.h. */ | |
62 | ||
63 | /* Swap in a Verdef structure. */ | |
64 | ||
65 | void | |
217aa764 AM |
66 | _bfd_elf_swap_verdef_in (bfd *abfd, |
67 | const Elf_External_Verdef *src, | |
68 | Elf_Internal_Verdef *dst) | |
252b5132 | 69 | { |
dc810e39 AM |
70 | dst->vd_version = H_GET_16 (abfd, src->vd_version); |
71 | dst->vd_flags = H_GET_16 (abfd, src->vd_flags); | |
72 | dst->vd_ndx = H_GET_16 (abfd, src->vd_ndx); | |
73 | dst->vd_cnt = H_GET_16 (abfd, src->vd_cnt); | |
74 | dst->vd_hash = H_GET_32 (abfd, src->vd_hash); | |
75 | dst->vd_aux = H_GET_32 (abfd, src->vd_aux); | |
76 | dst->vd_next = H_GET_32 (abfd, src->vd_next); | |
252b5132 RH |
77 | } |
78 | ||
79 | /* Swap out a Verdef structure. */ | |
80 | ||
81 | void | |
217aa764 AM |
82 | _bfd_elf_swap_verdef_out (bfd *abfd, |
83 | const Elf_Internal_Verdef *src, | |
84 | Elf_External_Verdef *dst) | |
252b5132 | 85 | { |
dc810e39 AM |
86 | H_PUT_16 (abfd, src->vd_version, dst->vd_version); |
87 | H_PUT_16 (abfd, src->vd_flags, dst->vd_flags); | |
88 | H_PUT_16 (abfd, src->vd_ndx, dst->vd_ndx); | |
89 | H_PUT_16 (abfd, src->vd_cnt, dst->vd_cnt); | |
90 | H_PUT_32 (abfd, src->vd_hash, dst->vd_hash); | |
91 | H_PUT_32 (abfd, src->vd_aux, dst->vd_aux); | |
92 | H_PUT_32 (abfd, src->vd_next, dst->vd_next); | |
252b5132 RH |
93 | } |
94 | ||
95 | /* Swap in a Verdaux structure. */ | |
96 | ||
97 | void | |
217aa764 AM |
98 | _bfd_elf_swap_verdaux_in (bfd *abfd, |
99 | const Elf_External_Verdaux *src, | |
100 | Elf_Internal_Verdaux *dst) | |
252b5132 | 101 | { |
dc810e39 AM |
102 | dst->vda_name = H_GET_32 (abfd, src->vda_name); |
103 | dst->vda_next = H_GET_32 (abfd, src->vda_next); | |
252b5132 RH |
104 | } |
105 | ||
106 | /* Swap out a Verdaux structure. */ | |
107 | ||
108 | void | |
217aa764 AM |
109 | _bfd_elf_swap_verdaux_out (bfd *abfd, |
110 | const Elf_Internal_Verdaux *src, | |
111 | Elf_External_Verdaux *dst) | |
252b5132 | 112 | { |
dc810e39 AM |
113 | H_PUT_32 (abfd, src->vda_name, dst->vda_name); |
114 | H_PUT_32 (abfd, src->vda_next, dst->vda_next); | |
252b5132 RH |
115 | } |
116 | ||
117 | /* Swap in a Verneed structure. */ | |
118 | ||
119 | void | |
217aa764 AM |
120 | _bfd_elf_swap_verneed_in (bfd *abfd, |
121 | const Elf_External_Verneed *src, | |
122 | Elf_Internal_Verneed *dst) | |
252b5132 | 123 | { |
dc810e39 AM |
124 | dst->vn_version = H_GET_16 (abfd, src->vn_version); |
125 | dst->vn_cnt = H_GET_16 (abfd, src->vn_cnt); | |
126 | dst->vn_file = H_GET_32 (abfd, src->vn_file); | |
127 | dst->vn_aux = H_GET_32 (abfd, src->vn_aux); | |
128 | dst->vn_next = H_GET_32 (abfd, src->vn_next); | |
252b5132 RH |
129 | } |
130 | ||
131 | /* Swap out a Verneed structure. */ | |
132 | ||
133 | void | |
217aa764 AM |
134 | _bfd_elf_swap_verneed_out (bfd *abfd, |
135 | const Elf_Internal_Verneed *src, | |
136 | Elf_External_Verneed *dst) | |
252b5132 | 137 | { |
dc810e39 AM |
138 | H_PUT_16 (abfd, src->vn_version, dst->vn_version); |
139 | H_PUT_16 (abfd, src->vn_cnt, dst->vn_cnt); | |
140 | H_PUT_32 (abfd, src->vn_file, dst->vn_file); | |
141 | H_PUT_32 (abfd, src->vn_aux, dst->vn_aux); | |
142 | H_PUT_32 (abfd, src->vn_next, dst->vn_next); | |
252b5132 RH |
143 | } |
144 | ||
145 | /* Swap in a Vernaux structure. */ | |
146 | ||
147 | void | |
217aa764 AM |
148 | _bfd_elf_swap_vernaux_in (bfd *abfd, |
149 | const Elf_External_Vernaux *src, | |
150 | Elf_Internal_Vernaux *dst) | |
252b5132 | 151 | { |
dc810e39 AM |
152 | dst->vna_hash = H_GET_32 (abfd, src->vna_hash); |
153 | dst->vna_flags = H_GET_16 (abfd, src->vna_flags); | |
154 | dst->vna_other = H_GET_16 (abfd, src->vna_other); | |
155 | dst->vna_name = H_GET_32 (abfd, src->vna_name); | |
156 | dst->vna_next = H_GET_32 (abfd, src->vna_next); | |
252b5132 RH |
157 | } |
158 | ||
159 | /* Swap out a Vernaux structure. */ | |
160 | ||
161 | void | |
217aa764 AM |
162 | _bfd_elf_swap_vernaux_out (bfd *abfd, |
163 | const Elf_Internal_Vernaux *src, | |
164 | Elf_External_Vernaux *dst) | |
252b5132 | 165 | { |
dc810e39 AM |
166 | H_PUT_32 (abfd, src->vna_hash, dst->vna_hash); |
167 | H_PUT_16 (abfd, src->vna_flags, dst->vna_flags); | |
168 | H_PUT_16 (abfd, src->vna_other, dst->vna_other); | |
169 | H_PUT_32 (abfd, src->vna_name, dst->vna_name); | |
170 | H_PUT_32 (abfd, src->vna_next, dst->vna_next); | |
252b5132 RH |
171 | } |
172 | ||
173 | /* Swap in a Versym structure. */ | |
174 | ||
175 | void | |
217aa764 AM |
176 | _bfd_elf_swap_versym_in (bfd *abfd, |
177 | const Elf_External_Versym *src, | |
178 | Elf_Internal_Versym *dst) | |
252b5132 | 179 | { |
dc810e39 | 180 | dst->vs_vers = H_GET_16 (abfd, src->vs_vers); |
252b5132 RH |
181 | } |
182 | ||
183 | /* Swap out a Versym structure. */ | |
184 | ||
185 | void | |
217aa764 AM |
186 | _bfd_elf_swap_versym_out (bfd *abfd, |
187 | const Elf_Internal_Versym *src, | |
188 | Elf_External_Versym *dst) | |
252b5132 | 189 | { |
dc810e39 | 190 | H_PUT_16 (abfd, src->vs_vers, dst->vs_vers); |
252b5132 RH |
191 | } |
192 | ||
193 | /* Standard ELF hash function. Do not change this function; you will | |
194 | cause invalid hash tables to be generated. */ | |
3a99b017 | 195 | |
252b5132 | 196 | unsigned long |
217aa764 | 197 | bfd_elf_hash (const char *namearg) |
252b5132 | 198 | { |
3a99b017 | 199 | const unsigned char *name = (const unsigned char *) namearg; |
252b5132 RH |
200 | unsigned long h = 0; |
201 | unsigned long g; | |
202 | int ch; | |
203 | ||
204 | while ((ch = *name++) != '\0') | |
205 | { | |
206 | h = (h << 4) + ch; | |
207 | if ((g = (h & 0xf0000000)) != 0) | |
208 | { | |
209 | h ^= g >> 24; | |
210 | /* The ELF ABI says `h &= ~g', but this is equivalent in | |
211 | this case and on some machines one insn instead of two. */ | |
212 | h ^= g; | |
213 | } | |
214 | } | |
32dfa85d | 215 | return h & 0xffffffff; |
252b5132 RH |
216 | } |
217 | ||
fdc90cb4 JJ |
218 | /* DT_GNU_HASH hash function. Do not change this function; you will |
219 | cause invalid hash tables to be generated. */ | |
220 | ||
221 | unsigned long | |
222 | bfd_elf_gnu_hash (const char *namearg) | |
223 | { | |
224 | const unsigned char *name = (const unsigned char *) namearg; | |
225 | unsigned long h = 5381; | |
226 | unsigned char ch; | |
227 | ||
228 | while ((ch = *name++) != '\0') | |
229 | h = (h << 5) + h + ch; | |
230 | return h & 0xffffffff; | |
231 | } | |
232 | ||
0c8d6e5c AM |
233 | /* Create a tdata field OBJECT_SIZE bytes in length, zeroed out and with |
234 | the object_id field of an elf_obj_tdata field set to OBJECT_ID. */ | |
b34976b6 | 235 | bfd_boolean |
0c8d6e5c | 236 | bfd_elf_allocate_object (bfd *abfd, |
0ffa91dd | 237 | size_t object_size, |
4dfe6ac6 | 238 | enum elf_target_id object_id) |
252b5132 | 239 | { |
0ffa91dd NC |
240 | BFD_ASSERT (object_size >= sizeof (struct elf_obj_tdata)); |
241 | abfd->tdata.any = bfd_zalloc (abfd, object_size); | |
242 | if (abfd->tdata.any == NULL) | |
243 | return FALSE; | |
252b5132 | 244 | |
0ffa91dd | 245 | elf_object_id (abfd) = object_id; |
c0355132 AM |
246 | if (abfd->direction != read_direction) |
247 | { | |
248 | struct output_elf_obj_tdata *o = bfd_zalloc (abfd, sizeof *o); | |
249 | if (o == NULL) | |
250 | return FALSE; | |
251 | elf_tdata (abfd)->o = o; | |
252 | elf_program_header_size (abfd) = (bfd_size_type) -1; | |
253 | } | |
b34976b6 | 254 | return TRUE; |
252b5132 RH |
255 | } |
256 | ||
0ffa91dd NC |
257 | |
258 | bfd_boolean | |
ae95ffa6 | 259 | bfd_elf_make_object (bfd *abfd) |
0ffa91dd | 260 | { |
ae95ffa6 | 261 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
0ffa91dd | 262 | return bfd_elf_allocate_object (abfd, sizeof (struct elf_obj_tdata), |
ae95ffa6 | 263 | bed->target_id); |
0ffa91dd NC |
264 | } |
265 | ||
b34976b6 | 266 | bfd_boolean |
217aa764 | 267 | bfd_elf_mkcorefile (bfd *abfd) |
252b5132 | 268 | { |
c044fabd | 269 | /* I think this can be done just like an object file. */ |
228e534f AM |
270 | if (!abfd->xvec->_bfd_set_format[(int) bfd_object] (abfd)) |
271 | return FALSE; | |
272 | elf_tdata (abfd)->core = bfd_zalloc (abfd, sizeof (*elf_tdata (abfd)->core)); | |
273 | return elf_tdata (abfd)->core != NULL; | |
252b5132 RH |
274 | } |
275 | ||
72a80a16 | 276 | static char * |
217aa764 | 277 | bfd_elf_get_str_section (bfd *abfd, unsigned int shindex) |
252b5132 RH |
278 | { |
279 | Elf_Internal_Shdr **i_shdrp; | |
f075ee0c | 280 | bfd_byte *shstrtab = NULL; |
dc810e39 AM |
281 | file_ptr offset; |
282 | bfd_size_type shstrtabsize; | |
252b5132 RH |
283 | |
284 | i_shdrp = elf_elfsections (abfd); | |
74f2e02b AM |
285 | if (i_shdrp == 0 |
286 | || shindex >= elf_numsections (abfd) | |
287 | || i_shdrp[shindex] == 0) | |
f075ee0c | 288 | return NULL; |
252b5132 | 289 | |
f075ee0c | 290 | shstrtab = i_shdrp[shindex]->contents; |
252b5132 RH |
291 | if (shstrtab == NULL) |
292 | { | |
c044fabd | 293 | /* No cached one, attempt to read, and cache what we read. */ |
252b5132 RH |
294 | offset = i_shdrp[shindex]->sh_offset; |
295 | shstrtabsize = i_shdrp[shindex]->sh_size; | |
c6c60d09 JJ |
296 | |
297 | /* Allocate and clear an extra byte at the end, to prevent crashes | |
298 | in case the string table is not terminated. */ | |
3471d59d | 299 | if (shstrtabsize + 1 <= 1 |
06614111 NC |
300 | || bfd_seek (abfd, offset, SEEK_SET) != 0 |
301 | || (shstrtab = (bfd_byte *) bfd_alloc (abfd, shstrtabsize + 1)) == NULL) | |
c6c60d09 JJ |
302 | shstrtab = NULL; |
303 | else if (bfd_bread (shstrtab, shstrtabsize, abfd) != shstrtabsize) | |
304 | { | |
305 | if (bfd_get_error () != bfd_error_system_call) | |
306 | bfd_set_error (bfd_error_file_truncated); | |
06614111 | 307 | bfd_release (abfd, shstrtab); |
c6c60d09 | 308 | shstrtab = NULL; |
3471d59d CC |
309 | /* Once we've failed to read it, make sure we don't keep |
310 | trying. Otherwise, we'll keep allocating space for | |
311 | the string table over and over. */ | |
312 | i_shdrp[shindex]->sh_size = 0; | |
c6c60d09 JJ |
313 | } |
314 | else | |
315 | shstrtab[shstrtabsize] = '\0'; | |
217aa764 | 316 | i_shdrp[shindex]->contents = shstrtab; |
252b5132 | 317 | } |
f075ee0c | 318 | return (char *) shstrtab; |
252b5132 RH |
319 | } |
320 | ||
321 | char * | |
217aa764 AM |
322 | bfd_elf_string_from_elf_section (bfd *abfd, |
323 | unsigned int shindex, | |
324 | unsigned int strindex) | |
252b5132 RH |
325 | { |
326 | Elf_Internal_Shdr *hdr; | |
327 | ||
328 | if (strindex == 0) | |
329 | return ""; | |
330 | ||
74f2e02b AM |
331 | if (elf_elfsections (abfd) == NULL || shindex >= elf_numsections (abfd)) |
332 | return NULL; | |
333 | ||
252b5132 RH |
334 | hdr = elf_elfsections (abfd)[shindex]; |
335 | ||
06614111 NC |
336 | if (hdr->contents == NULL) |
337 | { | |
338 | if (hdr->sh_type != SHT_STRTAB && hdr->sh_type < SHT_LOOS) | |
339 | { | |
340 | /* PR 17512: file: f057ec89. */ | |
341 | _bfd_error_handler (_("%B: attempt to load strings from a non-string section (number %d)"), | |
342 | abfd, shindex); | |
343 | return NULL; | |
344 | } | |
345 | ||
346 | if (bfd_elf_get_str_section (abfd, shindex) == NULL) | |
347 | return NULL; | |
348 | } | |
252b5132 RH |
349 | |
350 | if (strindex >= hdr->sh_size) | |
351 | { | |
1b3a8575 | 352 | unsigned int shstrndx = elf_elfheader(abfd)->e_shstrndx; |
252b5132 | 353 | (*_bfd_error_handler) |
d003868e AM |
354 | (_("%B: invalid string offset %u >= %lu for section `%s'"), |
355 | abfd, strindex, (unsigned long) hdr->sh_size, | |
1b3a8575 | 356 | (shindex == shstrndx && strindex == hdr->sh_name |
252b5132 | 357 | ? ".shstrtab" |
1b3a8575 | 358 | : bfd_elf_string_from_elf_section (abfd, shstrndx, hdr->sh_name))); |
45b222d6 | 359 | return NULL; |
252b5132 RH |
360 | } |
361 | ||
362 | return ((char *) hdr->contents) + strindex; | |
363 | } | |
364 | ||
6cdc0ccc AM |
365 | /* Read and convert symbols to internal format. |
366 | SYMCOUNT specifies the number of symbols to read, starting from | |
367 | symbol SYMOFFSET. If any of INTSYM_BUF, EXTSYM_BUF or EXTSHNDX_BUF | |
368 | are non-NULL, they are used to store the internal symbols, external | |
b7c368d0 NC |
369 | symbols, and symbol section index extensions, respectively. |
370 | Returns a pointer to the internal symbol buffer (malloced if necessary) | |
371 | or NULL if there were no symbols or some kind of problem. */ | |
6cdc0ccc AM |
372 | |
373 | Elf_Internal_Sym * | |
217aa764 AM |
374 | bfd_elf_get_elf_syms (bfd *ibfd, |
375 | Elf_Internal_Shdr *symtab_hdr, | |
376 | size_t symcount, | |
377 | size_t symoffset, | |
378 | Elf_Internal_Sym *intsym_buf, | |
379 | void *extsym_buf, | |
380 | Elf_External_Sym_Shndx *extshndx_buf) | |
6cdc0ccc AM |
381 | { |
382 | Elf_Internal_Shdr *shndx_hdr; | |
217aa764 | 383 | void *alloc_ext; |
df622259 | 384 | const bfd_byte *esym; |
6cdc0ccc AM |
385 | Elf_External_Sym_Shndx *alloc_extshndx; |
386 | Elf_External_Sym_Shndx *shndx; | |
4dd07732 | 387 | Elf_Internal_Sym *alloc_intsym; |
6cdc0ccc AM |
388 | Elf_Internal_Sym *isym; |
389 | Elf_Internal_Sym *isymend; | |
9c5bfbb7 | 390 | const struct elf_backend_data *bed; |
6cdc0ccc AM |
391 | size_t extsym_size; |
392 | bfd_size_type amt; | |
393 | file_ptr pos; | |
394 | ||
e44a2c9c AM |
395 | if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour) |
396 | abort (); | |
397 | ||
6cdc0ccc AM |
398 | if (symcount == 0) |
399 | return intsym_buf; | |
400 | ||
401 | /* Normal syms might have section extension entries. */ | |
402 | shndx_hdr = NULL; | |
403 | if (symtab_hdr == &elf_tdata (ibfd)->symtab_hdr) | |
404 | shndx_hdr = &elf_tdata (ibfd)->symtab_shndx_hdr; | |
405 | ||
406 | /* Read the symbols. */ | |
407 | alloc_ext = NULL; | |
408 | alloc_extshndx = NULL; | |
4dd07732 | 409 | alloc_intsym = NULL; |
6cdc0ccc AM |
410 | bed = get_elf_backend_data (ibfd); |
411 | extsym_size = bed->s->sizeof_sym; | |
412 | amt = symcount * extsym_size; | |
413 | pos = symtab_hdr->sh_offset + symoffset * extsym_size; | |
414 | if (extsym_buf == NULL) | |
415 | { | |
d0fb9a8d | 416 | alloc_ext = bfd_malloc2 (symcount, extsym_size); |
6cdc0ccc AM |
417 | extsym_buf = alloc_ext; |
418 | } | |
419 | if (extsym_buf == NULL | |
420 | || bfd_seek (ibfd, pos, SEEK_SET) != 0 | |
421 | || bfd_bread (extsym_buf, amt, ibfd) != amt) | |
422 | { | |
423 | intsym_buf = NULL; | |
424 | goto out; | |
425 | } | |
426 | ||
427 | if (shndx_hdr == NULL || shndx_hdr->sh_size == 0) | |
428 | extshndx_buf = NULL; | |
429 | else | |
430 | { | |
431 | amt = symcount * sizeof (Elf_External_Sym_Shndx); | |
432 | pos = shndx_hdr->sh_offset + symoffset * sizeof (Elf_External_Sym_Shndx); | |
433 | if (extshndx_buf == NULL) | |
434 | { | |
a50b1753 NC |
435 | alloc_extshndx = (Elf_External_Sym_Shndx *) |
436 | bfd_malloc2 (symcount, sizeof (Elf_External_Sym_Shndx)); | |
6cdc0ccc AM |
437 | extshndx_buf = alloc_extshndx; |
438 | } | |
439 | if (extshndx_buf == NULL | |
440 | || bfd_seek (ibfd, pos, SEEK_SET) != 0 | |
441 | || bfd_bread (extshndx_buf, amt, ibfd) != amt) | |
442 | { | |
443 | intsym_buf = NULL; | |
444 | goto out; | |
445 | } | |
446 | } | |
447 | ||
448 | if (intsym_buf == NULL) | |
449 | { | |
a50b1753 NC |
450 | alloc_intsym = (Elf_Internal_Sym *) |
451 | bfd_malloc2 (symcount, sizeof (Elf_Internal_Sym)); | |
4dd07732 | 452 | intsym_buf = alloc_intsym; |
6cdc0ccc AM |
453 | if (intsym_buf == NULL) |
454 | goto out; | |
455 | } | |
456 | ||
457 | /* Convert the symbols to internal form. */ | |
458 | isymend = intsym_buf + symcount; | |
a50b1753 NC |
459 | for (esym = (const bfd_byte *) extsym_buf, isym = intsym_buf, |
460 | shndx = extshndx_buf; | |
6cdc0ccc AM |
461 | isym < isymend; |
462 | esym += extsym_size, isym++, shndx = shndx != NULL ? shndx + 1 : NULL) | |
8384fb8f AM |
463 | if (!(*bed->s->swap_symbol_in) (ibfd, esym, shndx, isym)) |
464 | { | |
465 | symoffset += (esym - (bfd_byte *) extsym_buf) / extsym_size; | |
466 | (*_bfd_error_handler) (_("%B symbol number %lu references " | |
467 | "nonexistent SHT_SYMTAB_SHNDX section"), | |
468 | ibfd, (unsigned long) symoffset); | |
4dd07732 AM |
469 | if (alloc_intsym != NULL) |
470 | free (alloc_intsym); | |
8384fb8f AM |
471 | intsym_buf = NULL; |
472 | goto out; | |
473 | } | |
6cdc0ccc AM |
474 | |
475 | out: | |
476 | if (alloc_ext != NULL) | |
477 | free (alloc_ext); | |
478 | if (alloc_extshndx != NULL) | |
479 | free (alloc_extshndx); | |
480 | ||
481 | return intsym_buf; | |
482 | } | |
483 | ||
5cab59f6 AM |
484 | /* Look up a symbol name. */ |
485 | const char * | |
be8dd2ca AM |
486 | bfd_elf_sym_name (bfd *abfd, |
487 | Elf_Internal_Shdr *symtab_hdr, | |
26c61ae5 L |
488 | Elf_Internal_Sym *isym, |
489 | asection *sym_sec) | |
5cab59f6 | 490 | { |
26c61ae5 | 491 | const char *name; |
5cab59f6 | 492 | unsigned int iname = isym->st_name; |
be8dd2ca | 493 | unsigned int shindex = symtab_hdr->sh_link; |
26c61ae5 | 494 | |
138f35cc JJ |
495 | if (iname == 0 && ELF_ST_TYPE (isym->st_info) == STT_SECTION |
496 | /* Check for a bogus st_shndx to avoid crashing. */ | |
4fbb74a6 | 497 | && isym->st_shndx < elf_numsections (abfd)) |
5cab59f6 AM |
498 | { |
499 | iname = elf_elfsections (abfd)[isym->st_shndx]->sh_name; | |
500 | shindex = elf_elfheader (abfd)->e_shstrndx; | |
501 | } | |
502 | ||
26c61ae5 L |
503 | name = bfd_elf_string_from_elf_section (abfd, shindex, iname); |
504 | if (name == NULL) | |
505 | name = "(null)"; | |
506 | else if (sym_sec && *name == '\0') | |
507 | name = bfd_section_name (abfd, sym_sec); | |
508 | ||
509 | return name; | |
5cab59f6 AM |
510 | } |
511 | ||
dbb410c3 AM |
512 | /* Elf_Internal_Shdr->contents is an array of these for SHT_GROUP |
513 | sections. The first element is the flags, the rest are section | |
514 | pointers. */ | |
515 | ||
516 | typedef union elf_internal_group { | |
517 | Elf_Internal_Shdr *shdr; | |
518 | unsigned int flags; | |
519 | } Elf_Internal_Group; | |
520 | ||
b885599b AM |
521 | /* Return the name of the group signature symbol. Why isn't the |
522 | signature just a string? */ | |
523 | ||
524 | static const char * | |
217aa764 | 525 | group_signature (bfd *abfd, Elf_Internal_Shdr *ghdr) |
b885599b | 526 | { |
9dce4196 | 527 | Elf_Internal_Shdr *hdr; |
9dce4196 AM |
528 | unsigned char esym[sizeof (Elf64_External_Sym)]; |
529 | Elf_External_Sym_Shndx eshndx; | |
530 | Elf_Internal_Sym isym; | |
b885599b | 531 | |
13792e9d L |
532 | /* First we need to ensure the symbol table is available. Make sure |
533 | that it is a symbol table section. */ | |
4fbb74a6 AM |
534 | if (ghdr->sh_link >= elf_numsections (abfd)) |
535 | return NULL; | |
13792e9d L |
536 | hdr = elf_elfsections (abfd) [ghdr->sh_link]; |
537 | if (hdr->sh_type != SHT_SYMTAB | |
538 | || ! bfd_section_from_shdr (abfd, ghdr->sh_link)) | |
b885599b AM |
539 | return NULL; |
540 | ||
9dce4196 AM |
541 | /* Go read the symbol. */ |
542 | hdr = &elf_tdata (abfd)->symtab_hdr; | |
6cdc0ccc AM |
543 | if (bfd_elf_get_elf_syms (abfd, hdr, 1, ghdr->sh_info, |
544 | &isym, esym, &eshndx) == NULL) | |
b885599b | 545 | return NULL; |
9dce4196 | 546 | |
26c61ae5 | 547 | return bfd_elf_sym_name (abfd, hdr, &isym, NULL); |
b885599b AM |
548 | } |
549 | ||
dbb410c3 AM |
550 | /* Set next_in_group list pointer, and group name for NEWSECT. */ |
551 | ||
b34976b6 | 552 | static bfd_boolean |
217aa764 | 553 | setup_group (bfd *abfd, Elf_Internal_Shdr *hdr, asection *newsect) |
dbb410c3 AM |
554 | { |
555 | unsigned int num_group = elf_tdata (abfd)->num_group; | |
556 | ||
557 | /* If num_group is zero, read in all SHT_GROUP sections. The count | |
558 | is set to -1 if there are no SHT_GROUP sections. */ | |
559 | if (num_group == 0) | |
560 | { | |
561 | unsigned int i, shnum; | |
562 | ||
563 | /* First count the number of groups. If we have a SHT_GROUP | |
564 | section with just a flag word (ie. sh_size is 4), ignore it. */ | |
9ad5cbcf | 565 | shnum = elf_numsections (abfd); |
dbb410c3 | 566 | num_group = 0; |
08a40648 | 567 | |
44534af3 | 568 | #define IS_VALID_GROUP_SECTION_HEADER(shdr, minsize) \ |
1783205a | 569 | ( (shdr)->sh_type == SHT_GROUP \ |
44534af3 | 570 | && (shdr)->sh_size >= minsize \ |
1783205a NC |
571 | && (shdr)->sh_entsize == GRP_ENTRY_SIZE \ |
572 | && ((shdr)->sh_size % GRP_ENTRY_SIZE) == 0) | |
08a40648 | 573 | |
dbb410c3 AM |
574 | for (i = 0; i < shnum; i++) |
575 | { | |
576 | Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i]; | |
1783205a | 577 | |
44534af3 | 578 | if (IS_VALID_GROUP_SECTION_HEADER (shdr, 2 * GRP_ENTRY_SIZE)) |
dbb410c3 AM |
579 | num_group += 1; |
580 | } | |
581 | ||
582 | if (num_group == 0) | |
20dbb49d L |
583 | { |
584 | num_group = (unsigned) -1; | |
585 | elf_tdata (abfd)->num_group = num_group; | |
586 | } | |
587 | else | |
dbb410c3 AM |
588 | { |
589 | /* We keep a list of elf section headers for group sections, | |
590 | so we can find them quickly. */ | |
20dbb49d | 591 | bfd_size_type amt; |
d0fb9a8d | 592 | |
20dbb49d | 593 | elf_tdata (abfd)->num_group = num_group; |
a50b1753 NC |
594 | elf_tdata (abfd)->group_sect_ptr = (Elf_Internal_Shdr **) |
595 | bfd_alloc2 (abfd, num_group, sizeof (Elf_Internal_Shdr *)); | |
dbb410c3 | 596 | if (elf_tdata (abfd)->group_sect_ptr == NULL) |
b34976b6 | 597 | return FALSE; |
dbb410c3 AM |
598 | |
599 | num_group = 0; | |
600 | for (i = 0; i < shnum; i++) | |
601 | { | |
602 | Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i]; | |
1783205a | 603 | |
44534af3 | 604 | if (IS_VALID_GROUP_SECTION_HEADER (shdr, 2 * GRP_ENTRY_SIZE)) |
dbb410c3 | 605 | { |
973ffd63 | 606 | unsigned char *src; |
dbb410c3 AM |
607 | Elf_Internal_Group *dest; |
608 | ||
609 | /* Add to list of sections. */ | |
610 | elf_tdata (abfd)->group_sect_ptr[num_group] = shdr; | |
611 | num_group += 1; | |
612 | ||
613 | /* Read the raw contents. */ | |
614 | BFD_ASSERT (sizeof (*dest) >= 4); | |
615 | amt = shdr->sh_size * sizeof (*dest) / 4; | |
a50b1753 NC |
616 | shdr->contents = (unsigned char *) |
617 | bfd_alloc2 (abfd, shdr->sh_size, sizeof (*dest) / 4); | |
1783205a NC |
618 | /* PR binutils/4110: Handle corrupt group headers. */ |
619 | if (shdr->contents == NULL) | |
620 | { | |
621 | _bfd_error_handler | |
493a3386 | 622 | (_("%B: corrupt size field in group section header: 0x%lx"), abfd, shdr->sh_size); |
1783205a | 623 | bfd_set_error (bfd_error_bad_value); |
493a3386 NC |
624 | -- num_group; |
625 | continue; | |
1783205a NC |
626 | } |
627 | ||
628 | memset (shdr->contents, 0, amt); | |
629 | ||
630 | if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0 | |
dbb410c3 AM |
631 | || (bfd_bread (shdr->contents, shdr->sh_size, abfd) |
632 | != shdr->sh_size)) | |
493a3386 NC |
633 | { |
634 | _bfd_error_handler | |
635 | (_("%B: invalid size field in group section header: 0x%lx"), abfd, shdr->sh_size); | |
636 | bfd_set_error (bfd_error_bad_value); | |
637 | -- num_group; | |
638 | /* PR 17510: If the group contents are even partially | |
639 | corrupt, do not allow any of the contents to be used. */ | |
640 | memset (shdr->contents, 0, amt); | |
641 | continue; | |
642 | } | |
708d7d0d | 643 | |
dbb410c3 AM |
644 | /* Translate raw contents, a flag word followed by an |
645 | array of elf section indices all in target byte order, | |
646 | to the flag word followed by an array of elf section | |
647 | pointers. */ | |
648 | src = shdr->contents + shdr->sh_size; | |
649 | dest = (Elf_Internal_Group *) (shdr->contents + amt); | |
06614111 | 650 | |
dbb410c3 AM |
651 | while (1) |
652 | { | |
653 | unsigned int idx; | |
654 | ||
655 | src -= 4; | |
656 | --dest; | |
657 | idx = H_GET_32 (abfd, src); | |
658 | if (src == shdr->contents) | |
659 | { | |
660 | dest->flags = idx; | |
b885599b AM |
661 | if (shdr->bfd_section != NULL && (idx & GRP_COMDAT)) |
662 | shdr->bfd_section->flags | |
663 | |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD; | |
dbb410c3 AM |
664 | break; |
665 | } | |
666 | if (idx >= shnum) | |
667 | { | |
668 | ((*_bfd_error_handler) | |
d003868e | 669 | (_("%B: invalid SHT_GROUP entry"), abfd)); |
dbb410c3 AM |
670 | idx = 0; |
671 | } | |
672 | dest->shdr = elf_elfsections (abfd)[idx]; | |
673 | } | |
674 | } | |
675 | } | |
493a3386 NC |
676 | |
677 | /* PR 17510: Corrupt binaries might contain invalid groups. */ | |
678 | if (num_group != (unsigned) elf_tdata (abfd)->num_group) | |
679 | { | |
680 | elf_tdata (abfd)->num_group = num_group; | |
681 | ||
682 | /* If all groups are invalid then fail. */ | |
683 | if (num_group == 0) | |
684 | { | |
685 | elf_tdata (abfd)->group_sect_ptr = NULL; | |
686 | elf_tdata (abfd)->num_group = num_group = -1; | |
687 | (*_bfd_error_handler) (_("%B: no valid group sections found"), abfd); | |
688 | bfd_set_error (bfd_error_bad_value); | |
689 | } | |
690 | } | |
dbb410c3 AM |
691 | } |
692 | } | |
693 | ||
694 | if (num_group != (unsigned) -1) | |
695 | { | |
696 | unsigned int i; | |
697 | ||
698 | for (i = 0; i < num_group; i++) | |
699 | { | |
700 | Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i]; | |
701 | Elf_Internal_Group *idx = (Elf_Internal_Group *) shdr->contents; | |
702 | unsigned int n_elt = shdr->sh_size / 4; | |
703 | ||
704 | /* Look through this group's sections to see if current | |
705 | section is a member. */ | |
706 | while (--n_elt != 0) | |
707 | if ((++idx)->shdr == hdr) | |
708 | { | |
e0e8c97f | 709 | asection *s = NULL; |
dbb410c3 AM |
710 | |
711 | /* We are a member of this group. Go looking through | |
712 | other members to see if any others are linked via | |
713 | next_in_group. */ | |
714 | idx = (Elf_Internal_Group *) shdr->contents; | |
715 | n_elt = shdr->sh_size / 4; | |
716 | while (--n_elt != 0) | |
717 | if ((s = (++idx)->shdr->bfd_section) != NULL | |
945906ff | 718 | && elf_next_in_group (s) != NULL) |
dbb410c3 AM |
719 | break; |
720 | if (n_elt != 0) | |
721 | { | |
dbb410c3 AM |
722 | /* Snarf the group name from other member, and |
723 | insert current section in circular list. */ | |
945906ff AM |
724 | elf_group_name (newsect) = elf_group_name (s); |
725 | elf_next_in_group (newsect) = elf_next_in_group (s); | |
726 | elf_next_in_group (s) = newsect; | |
dbb410c3 AM |
727 | } |
728 | else | |
729 | { | |
dbb410c3 AM |
730 | const char *gname; |
731 | ||
b885599b AM |
732 | gname = group_signature (abfd, shdr); |
733 | if (gname == NULL) | |
b34976b6 | 734 | return FALSE; |
945906ff | 735 | elf_group_name (newsect) = gname; |
dbb410c3 AM |
736 | |
737 | /* Start a circular list with one element. */ | |
945906ff | 738 | elf_next_in_group (newsect) = newsect; |
dbb410c3 | 739 | } |
b885599b | 740 | |
9dce4196 AM |
741 | /* If the group section has been created, point to the |
742 | new member. */ | |
dbb410c3 | 743 | if (shdr->bfd_section != NULL) |
945906ff | 744 | elf_next_in_group (shdr->bfd_section) = newsect; |
b885599b | 745 | |
dbb410c3 AM |
746 | i = num_group - 1; |
747 | break; | |
748 | } | |
749 | } | |
750 | } | |
751 | ||
945906ff | 752 | if (elf_group_name (newsect) == NULL) |
dbb410c3 | 753 | { |
d003868e AM |
754 | (*_bfd_error_handler) (_("%B: no group info for section %A"), |
755 | abfd, newsect); | |
493a3386 | 756 | return FALSE; |
dbb410c3 | 757 | } |
b34976b6 | 758 | return TRUE; |
dbb410c3 AM |
759 | } |
760 | ||
3d7f7666 | 761 | bfd_boolean |
dd863624 | 762 | _bfd_elf_setup_sections (bfd *abfd) |
3d7f7666 L |
763 | { |
764 | unsigned int i; | |
765 | unsigned int num_group = elf_tdata (abfd)->num_group; | |
766 | bfd_boolean result = TRUE; | |
dd863624 L |
767 | asection *s; |
768 | ||
769 | /* Process SHF_LINK_ORDER. */ | |
770 | for (s = abfd->sections; s != NULL; s = s->next) | |
771 | { | |
772 | Elf_Internal_Shdr *this_hdr = &elf_section_data (s)->this_hdr; | |
773 | if ((this_hdr->sh_flags & SHF_LINK_ORDER) != 0) | |
774 | { | |
775 | unsigned int elfsec = this_hdr->sh_link; | |
776 | /* FIXME: The old Intel compiler and old strip/objcopy may | |
777 | not set the sh_link or sh_info fields. Hence we could | |
778 | get the situation where elfsec is 0. */ | |
779 | if (elfsec == 0) | |
780 | { | |
4fbb74a6 | 781 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
dd863624 L |
782 | if (bed->link_order_error_handler) |
783 | bed->link_order_error_handler | |
784 | (_("%B: warning: sh_link not set for section `%A'"), | |
785 | abfd, s); | |
786 | } | |
787 | else | |
788 | { | |
91d6fa6a | 789 | asection *linksec = NULL; |
25bbc984 | 790 | |
4fbb74a6 AM |
791 | if (elfsec < elf_numsections (abfd)) |
792 | { | |
793 | this_hdr = elf_elfsections (abfd)[elfsec]; | |
91d6fa6a | 794 | linksec = this_hdr->bfd_section; |
4fbb74a6 | 795 | } |
25bbc984 L |
796 | |
797 | /* PR 1991, 2008: | |
798 | Some strip/objcopy may leave an incorrect value in | |
799 | sh_link. We don't want to proceed. */ | |
91d6fa6a | 800 | if (linksec == NULL) |
25bbc984 L |
801 | { |
802 | (*_bfd_error_handler) | |
803 | (_("%B: sh_link [%d] in section `%A' is incorrect"), | |
804 | s->owner, s, elfsec); | |
805 | result = FALSE; | |
806 | } | |
807 | ||
91d6fa6a | 808 | elf_linked_to_section (s) = linksec; |
dd863624 L |
809 | } |
810 | } | |
811 | } | |
3d7f7666 | 812 | |
dd863624 | 813 | /* Process section groups. */ |
3d7f7666 L |
814 | if (num_group == (unsigned) -1) |
815 | return result; | |
816 | ||
817 | for (i = 0; i < num_group; i++) | |
818 | { | |
819 | Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i]; | |
820 | Elf_Internal_Group *idx = (Elf_Internal_Group *) shdr->contents; | |
821 | unsigned int n_elt = shdr->sh_size / 4; | |
822 | ||
823 | while (--n_elt != 0) | |
824 | if ((++idx)->shdr->bfd_section) | |
825 | elf_sec_group (idx->shdr->bfd_section) = shdr->bfd_section; | |
826 | else if (idx->shdr->sh_type == SHT_RELA | |
827 | || idx->shdr->sh_type == SHT_REL) | |
828 | /* We won't include relocation sections in section groups in | |
829 | output object files. We adjust the group section size here | |
830 | so that relocatable link will work correctly when | |
831 | relocation sections are in section group in input object | |
832 | files. */ | |
833 | shdr->bfd_section->size -= 4; | |
834 | else | |
835 | { | |
836 | /* There are some unknown sections in the group. */ | |
837 | (*_bfd_error_handler) | |
d003868e AM |
838 | (_("%B: unknown [%d] section `%s' in group [%s]"), |
839 | abfd, | |
3d7f7666 | 840 | (unsigned int) idx->shdr->sh_type, |
1b3a8575 AM |
841 | bfd_elf_string_from_elf_section (abfd, |
842 | (elf_elfheader (abfd) | |
843 | ->e_shstrndx), | |
844 | idx->shdr->sh_name), | |
3d7f7666 L |
845 | shdr->bfd_section->name); |
846 | result = FALSE; | |
847 | } | |
848 | } | |
849 | return result; | |
850 | } | |
851 | ||
72adc230 AM |
852 | bfd_boolean |
853 | bfd_elf_is_group_section (bfd *abfd ATTRIBUTE_UNUSED, const asection *sec) | |
854 | { | |
855 | return elf_next_in_group (sec) != NULL; | |
856 | } | |
857 | ||
252b5132 RH |
858 | /* Make a BFD section from an ELF section. We store a pointer to the |
859 | BFD section in the bfd_section field of the header. */ | |
860 | ||
b34976b6 | 861 | bfd_boolean |
217aa764 AM |
862 | _bfd_elf_make_section_from_shdr (bfd *abfd, |
863 | Elf_Internal_Shdr *hdr, | |
6dc132d9 L |
864 | const char *name, |
865 | int shindex) | |
252b5132 RH |
866 | { |
867 | asection *newsect; | |
868 | flagword flags; | |
9c5bfbb7 | 869 | const struct elf_backend_data *bed; |
252b5132 RH |
870 | |
871 | if (hdr->bfd_section != NULL) | |
4e011fb5 | 872 | return TRUE; |
252b5132 RH |
873 | |
874 | newsect = bfd_make_section_anyway (abfd, name); | |
875 | if (newsect == NULL) | |
b34976b6 | 876 | return FALSE; |
252b5132 | 877 | |
1829f4b2 AM |
878 | hdr->bfd_section = newsect; |
879 | elf_section_data (newsect)->this_hdr = *hdr; | |
6dc132d9 | 880 | elf_section_data (newsect)->this_idx = shindex; |
1829f4b2 | 881 | |
2f89ff8d L |
882 | /* Always use the real type/flags. */ |
883 | elf_section_type (newsect) = hdr->sh_type; | |
884 | elf_section_flags (newsect) = hdr->sh_flags; | |
885 | ||
252b5132 RH |
886 | newsect->filepos = hdr->sh_offset; |
887 | ||
888 | if (! bfd_set_section_vma (abfd, newsect, hdr->sh_addr) | |
889 | || ! bfd_set_section_size (abfd, newsect, hdr->sh_size) | |
890 | || ! bfd_set_section_alignment (abfd, newsect, | |
72de5009 | 891 | bfd_log2 (hdr->sh_addralign))) |
b34976b6 | 892 | return FALSE; |
252b5132 RH |
893 | |
894 | flags = SEC_NO_FLAGS; | |
895 | if (hdr->sh_type != SHT_NOBITS) | |
896 | flags |= SEC_HAS_CONTENTS; | |
dbb410c3 | 897 | if (hdr->sh_type == SHT_GROUP) |
b3096250 | 898 | flags |= SEC_GROUP | SEC_EXCLUDE; |
252b5132 RH |
899 | if ((hdr->sh_flags & SHF_ALLOC) != 0) |
900 | { | |
901 | flags |= SEC_ALLOC; | |
902 | if (hdr->sh_type != SHT_NOBITS) | |
903 | flags |= SEC_LOAD; | |
904 | } | |
905 | if ((hdr->sh_flags & SHF_WRITE) == 0) | |
906 | flags |= SEC_READONLY; | |
907 | if ((hdr->sh_flags & SHF_EXECINSTR) != 0) | |
908 | flags |= SEC_CODE; | |
909 | else if ((flags & SEC_LOAD) != 0) | |
910 | flags |= SEC_DATA; | |
f5fa8ca2 JJ |
911 | if ((hdr->sh_flags & SHF_MERGE) != 0) |
912 | { | |
913 | flags |= SEC_MERGE; | |
914 | newsect->entsize = hdr->sh_entsize; | |
915 | if ((hdr->sh_flags & SHF_STRINGS) != 0) | |
916 | flags |= SEC_STRINGS; | |
917 | } | |
dbb410c3 AM |
918 | if (hdr->sh_flags & SHF_GROUP) |
919 | if (!setup_group (abfd, hdr, newsect)) | |
b34976b6 | 920 | return FALSE; |
13ae64f3 JJ |
921 | if ((hdr->sh_flags & SHF_TLS) != 0) |
922 | flags |= SEC_THREAD_LOCAL; | |
18ae9cc1 L |
923 | if ((hdr->sh_flags & SHF_EXCLUDE) != 0) |
924 | flags |= SEC_EXCLUDE; | |
252b5132 | 925 | |
3d2b39cf | 926 | if ((flags & SEC_ALLOC) == 0) |
7a6cc5fb | 927 | { |
3d2b39cf L |
928 | /* The debugging sections appear to be recognized only by name, |
929 | not any sort of flag. Their SEC_ALLOC bits are cleared. */ | |
3d2b39cf L |
930 | if (name [0] == '.') |
931 | { | |
f073ced3 AM |
932 | const char *p; |
933 | int n; | |
934 | if (name[1] == 'd') | |
935 | p = ".debug", n = 6; | |
936 | else if (name[1] == 'g' && name[2] == 'n') | |
937 | p = ".gnu.linkonce.wi.", n = 17; | |
938 | else if (name[1] == 'g' && name[2] == 'd') | |
939 | p = ".gdb_index", n = 11; /* yes we really do mean 11. */ | |
940 | else if (name[1] == 'l') | |
941 | p = ".line", n = 5; | |
942 | else if (name[1] == 's') | |
943 | p = ".stab", n = 5; | |
944 | else if (name[1] == 'z') | |
945 | p = ".zdebug", n = 7; | |
946 | else | |
947 | p = NULL, n = 0; | |
948 | if (p != NULL && strncmp (name, p, n) == 0) | |
3d2b39cf L |
949 | flags |= SEC_DEBUGGING; |
950 | } | |
951 | } | |
252b5132 RH |
952 | |
953 | /* As a GNU extension, if the name begins with .gnu.linkonce, we | |
954 | only link a single copy of the section. This is used to support | |
955 | g++. g++ will emit each template expansion in its own section. | |
956 | The symbols will be defined as weak, so that multiple definitions | |
957 | are permitted. The GNU linker extension is to actually discard | |
958 | all but one of the sections. */ | |
0112cd26 | 959 | if (CONST_STRNEQ (name, ".gnu.linkonce") |
b885599b | 960 | && elf_next_in_group (newsect) == NULL) |
252b5132 RH |
961 | flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD; |
962 | ||
fa152c49 JW |
963 | bed = get_elf_backend_data (abfd); |
964 | if (bed->elf_backend_section_flags) | |
965 | if (! bed->elf_backend_section_flags (&flags, hdr)) | |
b34976b6 | 966 | return FALSE; |
fa152c49 | 967 | |
252b5132 | 968 | if (! bfd_set_section_flags (abfd, newsect, flags)) |
b34976b6 | 969 | return FALSE; |
252b5132 | 970 | |
718175fa JK |
971 | /* We do not parse the PT_NOTE segments as we are interested even in the |
972 | separate debug info files which may have the segments offsets corrupted. | |
973 | PT_NOTEs from the core files are currently not parsed using BFD. */ | |
974 | if (hdr->sh_type == SHT_NOTE) | |
975 | { | |
baea7ef1 | 976 | bfd_byte *contents; |
718175fa | 977 | |
baea7ef1 | 978 | if (!bfd_malloc_and_get_section (abfd, newsect, &contents)) |
718175fa JK |
979 | return FALSE; |
980 | ||
baea7ef1 | 981 | elf_parse_notes (abfd, (char *) contents, hdr->sh_size, -1); |
718175fa JK |
982 | free (contents); |
983 | } | |
984 | ||
252b5132 RH |
985 | if ((flags & SEC_ALLOC) != 0) |
986 | { | |
987 | Elf_Internal_Phdr *phdr; | |
6ffd7900 AM |
988 | unsigned int i, nload; |
989 | ||
990 | /* Some ELF linkers produce binaries with all the program header | |
991 | p_paddr fields zero. If we have such a binary with more than | |
992 | one PT_LOAD header, then leave the section lma equal to vma | |
993 | so that we don't create sections with overlapping lma. */ | |
994 | phdr = elf_tdata (abfd)->phdr; | |
995 | for (nload = 0, i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++) | |
996 | if (phdr->p_paddr != 0) | |
997 | break; | |
998 | else if (phdr->p_type == PT_LOAD && phdr->p_memsz != 0) | |
999 | ++nload; | |
1000 | if (i >= elf_elfheader (abfd)->e_phnum && nload > 1) | |
1001 | return TRUE; | |
252b5132 | 1002 | |
252b5132 RH |
1003 | phdr = elf_tdata (abfd)->phdr; |
1004 | for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++) | |
1005 | { | |
86b2281f AM |
1006 | if (((phdr->p_type == PT_LOAD |
1007 | && (hdr->sh_flags & SHF_TLS) == 0) | |
1008 | || phdr->p_type == PT_TLS) | |
9a83a553 | 1009 | && ELF_SECTION_IN_SEGMENT (hdr, phdr)) |
252b5132 | 1010 | { |
88967714 AM |
1011 | if ((flags & SEC_LOAD) == 0) |
1012 | newsect->lma = (phdr->p_paddr | |
1013 | + hdr->sh_addr - phdr->p_vaddr); | |
1014 | else | |
1015 | /* We used to use the same adjustment for SEC_LOAD | |
1016 | sections, but that doesn't work if the segment | |
1017 | is packed with code from multiple VMAs. | |
1018 | Instead we calculate the section LMA based on | |
1019 | the segment LMA. It is assumed that the | |
1020 | segment will contain sections with contiguous | |
1021 | LMAs, even if the VMAs are not. */ | |
1022 | newsect->lma = (phdr->p_paddr | |
1023 | + hdr->sh_offset - phdr->p_offset); | |
1024 | ||
1025 | /* With contiguous segments, we can't tell from file | |
1026 | offsets whether a section with zero size should | |
1027 | be placed at the end of one segment or the | |
1028 | beginning of the next. Decide based on vaddr. */ | |
1029 | if (hdr->sh_addr >= phdr->p_vaddr | |
1030 | && (hdr->sh_addr + hdr->sh_size | |
1031 | <= phdr->p_vaddr + phdr->p_memsz)) | |
1032 | break; | |
252b5132 RH |
1033 | } |
1034 | } | |
1035 | } | |
1036 | ||
4a114e3e L |
1037 | /* Compress/decompress DWARF debug sections with names: .debug_* and |
1038 | .zdebug_*, after the section flags is set. */ | |
1039 | if ((flags & SEC_DEBUGGING) | |
1040 | && ((name[1] == 'd' && name[6] == '_') | |
1041 | || (name[1] == 'z' && name[7] == '_'))) | |
1042 | { | |
1043 | enum { nothing, compress, decompress } action = nothing; | |
4e011fb5 | 1044 | char *new_name; |
4a114e3e L |
1045 | |
1046 | if (bfd_is_section_compressed (abfd, newsect)) | |
1047 | { | |
1048 | /* Compressed section. Check if we should decompress. */ | |
1049 | if ((abfd->flags & BFD_DECOMPRESS)) | |
1050 | action = decompress; | |
1051 | } | |
1052 | else | |
1053 | { | |
1054 | /* Normal section. Check if we should compress. */ | |
5a5ed5b0 | 1055 | if ((abfd->flags & BFD_COMPRESS) && newsect->size != 0) |
4a114e3e L |
1056 | action = compress; |
1057 | } | |
1058 | ||
4e011fb5 | 1059 | new_name = NULL; |
4a114e3e L |
1060 | switch (action) |
1061 | { | |
1062 | case nothing: | |
1063 | break; | |
1064 | case compress: | |
1065 | if (!bfd_init_section_compress_status (abfd, newsect)) | |
1066 | { | |
1067 | (*_bfd_error_handler) | |
bc823199 | 1068 | (_("%B: unable to initialize compress status for section %s"), |
4a114e3e L |
1069 | abfd, name); |
1070 | return FALSE; | |
1071 | } | |
273a4985 JT |
1072 | /* PR binutils/18087: Compression does not always make a section |
1073 | smaller. So only rename the section when compression has | |
1074 | actually taken place. */ | |
1075 | if (newsect->compress_status == COMPRESS_SECTION_DONE) | |
4e011fb5 | 1076 | { |
273a4985 JT |
1077 | if (name[1] != 'z') |
1078 | { | |
1079 | unsigned int len = strlen (name); | |
1080 | ||
1081 | new_name = bfd_alloc (abfd, len + 2); | |
1082 | if (new_name == NULL) | |
1083 | return FALSE; | |
1084 | new_name[0] = '.'; | |
1085 | new_name[1] = 'z'; | |
1086 | memcpy (new_name + 2, name + 1, len); | |
1087 | } | |
4e011fb5 | 1088 | } |
4a114e3e L |
1089 | break; |
1090 | case decompress: | |
1091 | if (!bfd_init_section_decompress_status (abfd, newsect)) | |
1092 | { | |
1093 | (*_bfd_error_handler) | |
bc823199 | 1094 | (_("%B: unable to initialize decompress status for section %s"), |
4a114e3e L |
1095 | abfd, name); |
1096 | return FALSE; | |
1097 | } | |
4e011fb5 AM |
1098 | if (name[1] == 'z') |
1099 | { | |
1100 | unsigned int len = strlen (name); | |
1101 | ||
1102 | new_name = bfd_alloc (abfd, len); | |
1103 | if (new_name == NULL) | |
1104 | return FALSE; | |
1105 | new_name[0] = '.'; | |
1106 | memcpy (new_name + 1, name + 2, len - 1); | |
1107 | } | |
4a114e3e L |
1108 | break; |
1109 | } | |
4e011fb5 AM |
1110 | if (new_name != NULL) |
1111 | bfd_rename_section (abfd, newsect, new_name); | |
4a114e3e L |
1112 | } |
1113 | ||
b34976b6 | 1114 | return TRUE; |
252b5132 RH |
1115 | } |
1116 | ||
252b5132 RH |
1117 | const char *const bfd_elf_section_type_names[] = { |
1118 | "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB", | |
1119 | "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE", | |
1120 | "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM", | |
1121 | }; | |
1122 | ||
1049f94e | 1123 | /* ELF relocs are against symbols. If we are producing relocatable |
252b5132 RH |
1124 | output, and the reloc is against an external symbol, and nothing |
1125 | has given us any additional addend, the resulting reloc will also | |
1126 | be against the same symbol. In such a case, we don't want to | |
1127 | change anything about the way the reloc is handled, since it will | |
1128 | all be done at final link time. Rather than put special case code | |
1129 | into bfd_perform_relocation, all the reloc types use this howto | |
1130 | function. It just short circuits the reloc if producing | |
1049f94e | 1131 | relocatable output against an external symbol. */ |
252b5132 | 1132 | |
252b5132 | 1133 | bfd_reloc_status_type |
217aa764 AM |
1134 | bfd_elf_generic_reloc (bfd *abfd ATTRIBUTE_UNUSED, |
1135 | arelent *reloc_entry, | |
1136 | asymbol *symbol, | |
1137 | void *data ATTRIBUTE_UNUSED, | |
1138 | asection *input_section, | |
1139 | bfd *output_bfd, | |
1140 | char **error_message ATTRIBUTE_UNUSED) | |
1141 | { | |
1142 | if (output_bfd != NULL | |
252b5132 RH |
1143 | && (symbol->flags & BSF_SECTION_SYM) == 0 |
1144 | && (! reloc_entry->howto->partial_inplace | |
1145 | || reloc_entry->addend == 0)) | |
1146 | { | |
1147 | reloc_entry->address += input_section->output_offset; | |
1148 | return bfd_reloc_ok; | |
1149 | } | |
1150 | ||
1151 | return bfd_reloc_continue; | |
1152 | } | |
1153 | \f | |
0ac4564e L |
1154 | /* Copy the program header and other data from one object module to |
1155 | another. */ | |
252b5132 | 1156 | |
b34976b6 | 1157 | bfd_boolean |
217aa764 | 1158 | _bfd_elf_copy_private_bfd_data (bfd *ibfd, bfd *obfd) |
2d502050 L |
1159 | { |
1160 | if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour | |
1161 | || bfd_get_flavour (obfd) != bfd_target_elf_flavour) | |
b34976b6 | 1162 | return TRUE; |
2d502050 | 1163 | |
57b828ef L |
1164 | if (!elf_flags_init (obfd)) |
1165 | { | |
1166 | elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags; | |
1167 | elf_flags_init (obfd) = TRUE; | |
1168 | } | |
2d502050 | 1169 | |
0ac4564e | 1170 | elf_gp (obfd) = elf_gp (ibfd); |
57b828ef L |
1171 | |
1172 | /* Also copy the EI_OSABI field. */ | |
1173 | elf_elfheader (obfd)->e_ident[EI_OSABI] = | |
1174 | elf_elfheader (ibfd)->e_ident[EI_OSABI]; | |
104d59d1 JM |
1175 | |
1176 | /* Copy object attributes. */ | |
1177 | _bfd_elf_copy_obj_attributes (ibfd, obfd); | |
b34976b6 | 1178 | return TRUE; |
2d502050 L |
1179 | } |
1180 | ||
cedc298e L |
1181 | static const char * |
1182 | get_segment_type (unsigned int p_type) | |
1183 | { | |
1184 | const char *pt; | |
1185 | switch (p_type) | |
1186 | { | |
1187 | case PT_NULL: pt = "NULL"; break; | |
1188 | case PT_LOAD: pt = "LOAD"; break; | |
1189 | case PT_DYNAMIC: pt = "DYNAMIC"; break; | |
1190 | case PT_INTERP: pt = "INTERP"; break; | |
1191 | case PT_NOTE: pt = "NOTE"; break; | |
1192 | case PT_SHLIB: pt = "SHLIB"; break; | |
1193 | case PT_PHDR: pt = "PHDR"; break; | |
1194 | case PT_TLS: pt = "TLS"; break; | |
1195 | case PT_GNU_EH_FRAME: pt = "EH_FRAME"; break; | |
2b05f1b7 | 1196 | case PT_GNU_STACK: pt = "STACK"; break; |
cedc298e L |
1197 | case PT_GNU_RELRO: pt = "RELRO"; break; |
1198 | default: pt = NULL; break; | |
1199 | } | |
1200 | return pt; | |
1201 | } | |
1202 | ||
f0b79d91 L |
1203 | /* Print out the program headers. */ |
1204 | ||
b34976b6 | 1205 | bfd_boolean |
217aa764 | 1206 | _bfd_elf_print_private_bfd_data (bfd *abfd, void *farg) |
252b5132 | 1207 | { |
a50b1753 | 1208 | FILE *f = (FILE *) farg; |
252b5132 RH |
1209 | Elf_Internal_Phdr *p; |
1210 | asection *s; | |
1211 | bfd_byte *dynbuf = NULL; | |
1212 | ||
1213 | p = elf_tdata (abfd)->phdr; | |
1214 | if (p != NULL) | |
1215 | { | |
1216 | unsigned int i, c; | |
1217 | ||
1218 | fprintf (f, _("\nProgram Header:\n")); | |
1219 | c = elf_elfheader (abfd)->e_phnum; | |
1220 | for (i = 0; i < c; i++, p++) | |
1221 | { | |
cedc298e | 1222 | const char *pt = get_segment_type (p->p_type); |
252b5132 RH |
1223 | char buf[20]; |
1224 | ||
cedc298e | 1225 | if (pt == NULL) |
252b5132 | 1226 | { |
cedc298e L |
1227 | sprintf (buf, "0x%lx", p->p_type); |
1228 | pt = buf; | |
252b5132 | 1229 | } |
dc810e39 | 1230 | fprintf (f, "%8s off 0x", pt); |
60b89a18 | 1231 | bfd_fprintf_vma (abfd, f, p->p_offset); |
252b5132 | 1232 | fprintf (f, " vaddr 0x"); |
60b89a18 | 1233 | bfd_fprintf_vma (abfd, f, p->p_vaddr); |
252b5132 | 1234 | fprintf (f, " paddr 0x"); |
60b89a18 | 1235 | bfd_fprintf_vma (abfd, f, p->p_paddr); |
252b5132 RH |
1236 | fprintf (f, " align 2**%u\n", bfd_log2 (p->p_align)); |
1237 | fprintf (f, " filesz 0x"); | |
60b89a18 | 1238 | bfd_fprintf_vma (abfd, f, p->p_filesz); |
252b5132 | 1239 | fprintf (f, " memsz 0x"); |
60b89a18 | 1240 | bfd_fprintf_vma (abfd, f, p->p_memsz); |
252b5132 RH |
1241 | fprintf (f, " flags %c%c%c", |
1242 | (p->p_flags & PF_R) != 0 ? 'r' : '-', | |
1243 | (p->p_flags & PF_W) != 0 ? 'w' : '-', | |
1244 | (p->p_flags & PF_X) != 0 ? 'x' : '-'); | |
dc810e39 AM |
1245 | if ((p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X)) != 0) |
1246 | fprintf (f, " %lx", p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X)); | |
252b5132 RH |
1247 | fprintf (f, "\n"); |
1248 | } | |
1249 | } | |
1250 | ||
1251 | s = bfd_get_section_by_name (abfd, ".dynamic"); | |
1252 | if (s != NULL) | |
1253 | { | |
cb33740c | 1254 | unsigned int elfsec; |
dc810e39 | 1255 | unsigned long shlink; |
252b5132 RH |
1256 | bfd_byte *extdyn, *extdynend; |
1257 | size_t extdynsize; | |
217aa764 | 1258 | void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *); |
252b5132 RH |
1259 | |
1260 | fprintf (f, _("\nDynamic Section:\n")); | |
1261 | ||
eea6121a | 1262 | if (!bfd_malloc_and_get_section (abfd, s, &dynbuf)) |
252b5132 RH |
1263 | goto error_return; |
1264 | ||
1265 | elfsec = _bfd_elf_section_from_bfd_section (abfd, s); | |
cb33740c | 1266 | if (elfsec == SHN_BAD) |
252b5132 | 1267 | goto error_return; |
dc810e39 | 1268 | shlink = elf_elfsections (abfd)[elfsec]->sh_link; |
252b5132 RH |
1269 | |
1270 | extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn; | |
1271 | swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in; | |
1272 | ||
1273 | extdyn = dynbuf; | |
06614111 NC |
1274 | /* PR 17512: file: 6f427532. */ |
1275 | if (s->size < extdynsize) | |
1276 | goto error_return; | |
eea6121a | 1277 | extdynend = extdyn + s->size; |
1036838a NC |
1278 | /* PR 17512: file: id:000006,sig:06,src:000000,op:flip4,pos:5664. |
1279 | Fix range check. */ | |
1280 | for (; extdyn <= (extdynend - extdynsize); extdyn += extdynsize) | |
252b5132 RH |
1281 | { |
1282 | Elf_Internal_Dyn dyn; | |
ad9563d6 | 1283 | const char *name = ""; |
252b5132 | 1284 | char ab[20]; |
b34976b6 | 1285 | bfd_boolean stringp; |
ad9563d6 | 1286 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
252b5132 | 1287 | |
217aa764 | 1288 | (*swap_dyn_in) (abfd, extdyn, &dyn); |
252b5132 RH |
1289 | |
1290 | if (dyn.d_tag == DT_NULL) | |
1291 | break; | |
1292 | ||
b34976b6 | 1293 | stringp = FALSE; |
252b5132 RH |
1294 | switch (dyn.d_tag) |
1295 | { | |
1296 | default: | |
ad9563d6 CM |
1297 | if (bed->elf_backend_get_target_dtag) |
1298 | name = (*bed->elf_backend_get_target_dtag) (dyn.d_tag); | |
1299 | ||
1300 | if (!strcmp (name, "")) | |
1301 | { | |
1302 | sprintf (ab, "0x%lx", (unsigned long) dyn.d_tag); | |
1303 | name = ab; | |
1304 | } | |
252b5132 RH |
1305 | break; |
1306 | ||
b34976b6 | 1307 | case DT_NEEDED: name = "NEEDED"; stringp = TRUE; break; |
252b5132 RH |
1308 | case DT_PLTRELSZ: name = "PLTRELSZ"; break; |
1309 | case DT_PLTGOT: name = "PLTGOT"; break; | |
1310 | case DT_HASH: name = "HASH"; break; | |
1311 | case DT_STRTAB: name = "STRTAB"; break; | |
1312 | case DT_SYMTAB: name = "SYMTAB"; break; | |
1313 | case DT_RELA: name = "RELA"; break; | |
1314 | case DT_RELASZ: name = "RELASZ"; break; | |
1315 | case DT_RELAENT: name = "RELAENT"; break; | |
1316 | case DT_STRSZ: name = "STRSZ"; break; | |
1317 | case DT_SYMENT: name = "SYMENT"; break; | |
1318 | case DT_INIT: name = "INIT"; break; | |
1319 | case DT_FINI: name = "FINI"; break; | |
b34976b6 AM |
1320 | case DT_SONAME: name = "SONAME"; stringp = TRUE; break; |
1321 | case DT_RPATH: name = "RPATH"; stringp = TRUE; break; | |
252b5132 RH |
1322 | case DT_SYMBOLIC: name = "SYMBOLIC"; break; |
1323 | case DT_REL: name = "REL"; break; | |
1324 | case DT_RELSZ: name = "RELSZ"; break; | |
1325 | case DT_RELENT: name = "RELENT"; break; | |
1326 | case DT_PLTREL: name = "PLTREL"; break; | |
1327 | case DT_DEBUG: name = "DEBUG"; break; | |
1328 | case DT_TEXTREL: name = "TEXTREL"; break; | |
1329 | case DT_JMPREL: name = "JMPREL"; break; | |
94558834 L |
1330 | case DT_BIND_NOW: name = "BIND_NOW"; break; |
1331 | case DT_INIT_ARRAY: name = "INIT_ARRAY"; break; | |
1332 | case DT_FINI_ARRAY: name = "FINI_ARRAY"; break; | |
1333 | case DT_INIT_ARRAYSZ: name = "INIT_ARRAYSZ"; break; | |
1334 | case DT_FINI_ARRAYSZ: name = "FINI_ARRAYSZ"; break; | |
b34976b6 | 1335 | case DT_RUNPATH: name = "RUNPATH"; stringp = TRUE; break; |
94558834 L |
1336 | case DT_FLAGS: name = "FLAGS"; break; |
1337 | case DT_PREINIT_ARRAY: name = "PREINIT_ARRAY"; break; | |
1338 | case DT_PREINIT_ARRAYSZ: name = "PREINIT_ARRAYSZ"; break; | |
d48188b9 | 1339 | case DT_CHECKSUM: name = "CHECKSUM"; break; |
94558834 L |
1340 | case DT_PLTPADSZ: name = "PLTPADSZ"; break; |
1341 | case DT_MOVEENT: name = "MOVEENT"; break; | |
1342 | case DT_MOVESZ: name = "MOVESZ"; break; | |
1343 | case DT_FEATURE: name = "FEATURE"; break; | |
1344 | case DT_POSFLAG_1: name = "POSFLAG_1"; break; | |
1345 | case DT_SYMINSZ: name = "SYMINSZ"; break; | |
1346 | case DT_SYMINENT: name = "SYMINENT"; break; | |
b34976b6 AM |
1347 | case DT_CONFIG: name = "CONFIG"; stringp = TRUE; break; |
1348 | case DT_DEPAUDIT: name = "DEPAUDIT"; stringp = TRUE; break; | |
1349 | case DT_AUDIT: name = "AUDIT"; stringp = TRUE; break; | |
94558834 L |
1350 | case DT_PLTPAD: name = "PLTPAD"; break; |
1351 | case DT_MOVETAB: name = "MOVETAB"; break; | |
1352 | case DT_SYMINFO: name = "SYMINFO"; break; | |
1353 | case DT_RELACOUNT: name = "RELACOUNT"; break; | |
1354 | case DT_RELCOUNT: name = "RELCOUNT"; break; | |
1355 | case DT_FLAGS_1: name = "FLAGS_1"; break; | |
252b5132 RH |
1356 | case DT_VERSYM: name = "VERSYM"; break; |
1357 | case DT_VERDEF: name = "VERDEF"; break; | |
1358 | case DT_VERDEFNUM: name = "VERDEFNUM"; break; | |
1359 | case DT_VERNEED: name = "VERNEED"; break; | |
1360 | case DT_VERNEEDNUM: name = "VERNEEDNUM"; break; | |
b34976b6 | 1361 | case DT_AUXILIARY: name = "AUXILIARY"; stringp = TRUE; break; |
94558834 | 1362 | case DT_USED: name = "USED"; break; |
b34976b6 | 1363 | case DT_FILTER: name = "FILTER"; stringp = TRUE; break; |
fdc90cb4 | 1364 | case DT_GNU_HASH: name = "GNU_HASH"; break; |
252b5132 RH |
1365 | } |
1366 | ||
ad9563d6 | 1367 | fprintf (f, " %-20s ", name); |
252b5132 | 1368 | if (! stringp) |
a1f3c56e AN |
1369 | { |
1370 | fprintf (f, "0x"); | |
1371 | bfd_fprintf_vma (abfd, f, dyn.d_un.d_val); | |
1372 | } | |
252b5132 RH |
1373 | else |
1374 | { | |
1375 | const char *string; | |
dc810e39 | 1376 | unsigned int tagv = dyn.d_un.d_val; |
252b5132 | 1377 | |
dc810e39 | 1378 | string = bfd_elf_string_from_elf_section (abfd, shlink, tagv); |
252b5132 RH |
1379 | if (string == NULL) |
1380 | goto error_return; | |
1381 | fprintf (f, "%s", string); | |
1382 | } | |
1383 | fprintf (f, "\n"); | |
1384 | } | |
1385 | ||
1386 | free (dynbuf); | |
1387 | dynbuf = NULL; | |
1388 | } | |
1389 | ||
1390 | if ((elf_dynverdef (abfd) != 0 && elf_tdata (abfd)->verdef == NULL) | |
1391 | || (elf_dynverref (abfd) != 0 && elf_tdata (abfd)->verref == NULL)) | |
1392 | { | |
fc0e6df6 | 1393 | if (! _bfd_elf_slurp_version_tables (abfd, FALSE)) |
b34976b6 | 1394 | return FALSE; |
252b5132 RH |
1395 | } |
1396 | ||
1397 | if (elf_dynverdef (abfd) != 0) | |
1398 | { | |
1399 | Elf_Internal_Verdef *t; | |
1400 | ||
1401 | fprintf (f, _("\nVersion definitions:\n")); | |
1402 | for (t = elf_tdata (abfd)->verdef; t != NULL; t = t->vd_nextdef) | |
1403 | { | |
1404 | fprintf (f, "%d 0x%2.2x 0x%8.8lx %s\n", t->vd_ndx, | |
d0fb9a8d JJ |
1405 | t->vd_flags, t->vd_hash, |
1406 | t->vd_nodename ? t->vd_nodename : "<corrupt>"); | |
1407 | if (t->vd_auxptr != NULL && t->vd_auxptr->vda_nextptr != NULL) | |
252b5132 RH |
1408 | { |
1409 | Elf_Internal_Verdaux *a; | |
1410 | ||
1411 | fprintf (f, "\t"); | |
1412 | for (a = t->vd_auxptr->vda_nextptr; | |
1413 | a != NULL; | |
1414 | a = a->vda_nextptr) | |
d0fb9a8d JJ |
1415 | fprintf (f, "%s ", |
1416 | a->vda_nodename ? a->vda_nodename : "<corrupt>"); | |
252b5132 RH |
1417 | fprintf (f, "\n"); |
1418 | } | |
1419 | } | |
1420 | } | |
1421 | ||
1422 | if (elf_dynverref (abfd) != 0) | |
1423 | { | |
1424 | Elf_Internal_Verneed *t; | |
1425 | ||
1426 | fprintf (f, _("\nVersion References:\n")); | |
1427 | for (t = elf_tdata (abfd)->verref; t != NULL; t = t->vn_nextref) | |
1428 | { | |
1429 | Elf_Internal_Vernaux *a; | |
1430 | ||
d0fb9a8d JJ |
1431 | fprintf (f, _(" required from %s:\n"), |
1432 | t->vn_filename ? t->vn_filename : "<corrupt>"); | |
252b5132 RH |
1433 | for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) |
1434 | fprintf (f, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a->vna_hash, | |
d0fb9a8d JJ |
1435 | a->vna_flags, a->vna_other, |
1436 | a->vna_nodename ? a->vna_nodename : "<corrupt>"); | |
252b5132 RH |
1437 | } |
1438 | } | |
1439 | ||
b34976b6 | 1440 | return TRUE; |
252b5132 RH |
1441 | |
1442 | error_return: | |
1443 | if (dynbuf != NULL) | |
1444 | free (dynbuf); | |
b34976b6 | 1445 | return FALSE; |
252b5132 RH |
1446 | } |
1447 | ||
bb4d2ac2 L |
1448 | /* Get version string. */ |
1449 | ||
1450 | const char * | |
60bb06bc L |
1451 | _bfd_elf_get_symbol_version_string (bfd *abfd, asymbol *symbol, |
1452 | bfd_boolean *hidden) | |
bb4d2ac2 L |
1453 | { |
1454 | const char *version_string = NULL; | |
1455 | if (elf_dynversym (abfd) != 0 | |
1456 | && (elf_dynverdef (abfd) != 0 || elf_dynverref (abfd) != 0)) | |
1457 | { | |
1458 | unsigned int vernum = ((elf_symbol_type *) symbol)->version; | |
1459 | ||
1460 | *hidden = (vernum & VERSYM_HIDDEN) != 0; | |
1461 | vernum &= VERSYM_VERSION; | |
1462 | ||
1463 | if (vernum == 0) | |
1464 | version_string = ""; | |
1465 | else if (vernum == 1) | |
1466 | version_string = "Base"; | |
1467 | else if (vernum <= elf_tdata (abfd)->cverdefs) | |
1468 | version_string = | |
1469 | elf_tdata (abfd)->verdef[vernum - 1].vd_nodename; | |
1470 | else | |
1471 | { | |
1472 | Elf_Internal_Verneed *t; | |
1473 | ||
1474 | version_string = ""; | |
1475 | for (t = elf_tdata (abfd)->verref; | |
1476 | t != NULL; | |
1477 | t = t->vn_nextref) | |
1478 | { | |
1479 | Elf_Internal_Vernaux *a; | |
1480 | ||
1481 | for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) | |
1482 | { | |
1483 | if (a->vna_other == vernum) | |
1484 | { | |
1485 | version_string = a->vna_nodename; | |
1486 | break; | |
1487 | } | |
1488 | } | |
1489 | } | |
1490 | } | |
1491 | } | |
1492 | return version_string; | |
1493 | } | |
1494 | ||
252b5132 RH |
1495 | /* Display ELF-specific fields of a symbol. */ |
1496 | ||
1497 | void | |
217aa764 AM |
1498 | bfd_elf_print_symbol (bfd *abfd, |
1499 | void *filep, | |
1500 | asymbol *symbol, | |
1501 | bfd_print_symbol_type how) | |
252b5132 | 1502 | { |
a50b1753 | 1503 | FILE *file = (FILE *) filep; |
252b5132 RH |
1504 | switch (how) |
1505 | { | |
1506 | case bfd_print_symbol_name: | |
1507 | fprintf (file, "%s", symbol->name); | |
1508 | break; | |
1509 | case bfd_print_symbol_more: | |
1510 | fprintf (file, "elf "); | |
60b89a18 | 1511 | bfd_fprintf_vma (abfd, file, symbol->value); |
0af1713e | 1512 | fprintf (file, " %lx", (unsigned long) symbol->flags); |
252b5132 RH |
1513 | break; |
1514 | case bfd_print_symbol_all: | |
1515 | { | |
4e8a9624 AM |
1516 | const char *section_name; |
1517 | const char *name = NULL; | |
9c5bfbb7 | 1518 | const struct elf_backend_data *bed; |
7a13edea | 1519 | unsigned char st_other; |
dbb410c3 | 1520 | bfd_vma val; |
bb4d2ac2 L |
1521 | const char *version_string; |
1522 | bfd_boolean hidden; | |
c044fabd | 1523 | |
252b5132 | 1524 | section_name = symbol->section ? symbol->section->name : "(*none*)"; |
587ff49e RH |
1525 | |
1526 | bed = get_elf_backend_data (abfd); | |
1527 | if (bed->elf_backend_print_symbol_all) | |
c044fabd | 1528 | name = (*bed->elf_backend_print_symbol_all) (abfd, filep, symbol); |
587ff49e RH |
1529 | |
1530 | if (name == NULL) | |
1531 | { | |
7ee38065 | 1532 | name = symbol->name; |
217aa764 | 1533 | bfd_print_symbol_vandf (abfd, file, symbol); |
587ff49e RH |
1534 | } |
1535 | ||
252b5132 RH |
1536 | fprintf (file, " %s\t", section_name); |
1537 | /* Print the "other" value for a symbol. For common symbols, | |
1538 | we've already printed the size; now print the alignment. | |
1539 | For other symbols, we have no specified alignment, and | |
1540 | we've printed the address; now print the size. */ | |
dcf6c779 | 1541 | if (symbol->section && bfd_is_com_section (symbol->section)) |
dbb410c3 AM |
1542 | val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_value; |
1543 | else | |
1544 | val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_size; | |
1545 | bfd_fprintf_vma (abfd, file, val); | |
252b5132 RH |
1546 | |
1547 | /* If we have version information, print it. */ | |
60bb06bc L |
1548 | version_string = _bfd_elf_get_symbol_version_string (abfd, |
1549 | symbol, | |
1550 | &hidden); | |
bb4d2ac2 | 1551 | if (version_string) |
252b5132 | 1552 | { |
bb4d2ac2 | 1553 | if (!hidden) |
252b5132 RH |
1554 | fprintf (file, " %-11s", version_string); |
1555 | else | |
1556 | { | |
1557 | int i; | |
1558 | ||
1559 | fprintf (file, " (%s)", version_string); | |
1560 | for (i = 10 - strlen (version_string); i > 0; --i) | |
1561 | putc (' ', file); | |
1562 | } | |
1563 | } | |
1564 | ||
1565 | /* If the st_other field is not zero, print it. */ | |
7a13edea | 1566 | st_other = ((elf_symbol_type *) symbol)->internal_elf_sym.st_other; |
c044fabd | 1567 | |
7a13edea NC |
1568 | switch (st_other) |
1569 | { | |
1570 | case 0: break; | |
1571 | case STV_INTERNAL: fprintf (file, " .internal"); break; | |
1572 | case STV_HIDDEN: fprintf (file, " .hidden"); break; | |
1573 | case STV_PROTECTED: fprintf (file, " .protected"); break; | |
1574 | default: | |
1575 | /* Some other non-defined flags are also present, so print | |
1576 | everything hex. */ | |
1577 | fprintf (file, " 0x%02x", (unsigned int) st_other); | |
1578 | } | |
252b5132 | 1579 | |
587ff49e | 1580 | fprintf (file, " %s", name); |
252b5132 RH |
1581 | } |
1582 | break; | |
1583 | } | |
1584 | } | |
252b5132 | 1585 | |
252b5132 RH |
1586 | /* Allocate an ELF string table--force the first byte to be zero. */ |
1587 | ||
1588 | struct bfd_strtab_hash * | |
217aa764 | 1589 | _bfd_elf_stringtab_init (void) |
252b5132 RH |
1590 | { |
1591 | struct bfd_strtab_hash *ret; | |
1592 | ||
1593 | ret = _bfd_stringtab_init (); | |
1594 | if (ret != NULL) | |
1595 | { | |
1596 | bfd_size_type loc; | |
1597 | ||
b34976b6 | 1598 | loc = _bfd_stringtab_add (ret, "", TRUE, FALSE); |
252b5132 RH |
1599 | BFD_ASSERT (loc == 0 || loc == (bfd_size_type) -1); |
1600 | if (loc == (bfd_size_type) -1) | |
1601 | { | |
1602 | _bfd_stringtab_free (ret); | |
1603 | ret = NULL; | |
1604 | } | |
1605 | } | |
1606 | return ret; | |
1607 | } | |
1608 | \f | |
1609 | /* ELF .o/exec file reading */ | |
1610 | ||
c044fabd | 1611 | /* Create a new bfd section from an ELF section header. */ |
252b5132 | 1612 | |
b34976b6 | 1613 | bfd_boolean |
217aa764 | 1614 | bfd_section_from_shdr (bfd *abfd, unsigned int shindex) |
252b5132 | 1615 | { |
4fbb74a6 AM |
1616 | Elf_Internal_Shdr *hdr; |
1617 | Elf_Internal_Ehdr *ehdr; | |
1618 | const struct elf_backend_data *bed; | |
90937f86 | 1619 | const char *name; |
bf67003b NC |
1620 | bfd_boolean ret = TRUE; |
1621 | static bfd_boolean * sections_being_created = NULL; | |
5a4b0ccc | 1622 | static bfd * sections_being_created_abfd = NULL; |
bf67003b | 1623 | static unsigned int nesting = 0; |
252b5132 | 1624 | |
4fbb74a6 AM |
1625 | if (shindex >= elf_numsections (abfd)) |
1626 | return FALSE; | |
1627 | ||
bf67003b NC |
1628 | if (++ nesting > 3) |
1629 | { | |
1630 | /* PR17512: A corrupt ELF binary might contain a recursive group of | |
06614111 | 1631 | sections, with each the string indicies pointing to the next in the |
bf67003b NC |
1632 | loop. Detect this here, by refusing to load a section that we are |
1633 | already in the process of loading. We only trigger this test if | |
1634 | we have nested at least three sections deep as normal ELF binaries | |
5a4b0ccc NC |
1635 | can expect to recurse at least once. |
1636 | ||
1637 | FIXME: It would be better if this array was attached to the bfd, | |
1638 | rather than being held in a static pointer. */ | |
1639 | ||
1640 | if (sections_being_created_abfd != abfd) | |
1641 | sections_being_created = NULL; | |
bf67003b NC |
1642 | if (sections_being_created == NULL) |
1643 | { | |
1644 | /* FIXME: It would be more efficient to attach this array to the bfd somehow. */ | |
1645 | sections_being_created = (bfd_boolean *) | |
1646 | bfd_zalloc (abfd, elf_numsections (abfd) * sizeof (bfd_boolean)); | |
5a4b0ccc | 1647 | sections_being_created_abfd = abfd; |
bf67003b NC |
1648 | } |
1649 | if (sections_being_created [shindex]) | |
1650 | { | |
1651 | (*_bfd_error_handler) | |
1652 | (_("%B: warning: loop in section dependencies detected"), abfd); | |
1653 | return FALSE; | |
1654 | } | |
1655 | sections_being_created [shindex] = TRUE; | |
1656 | } | |
1657 | ||
4fbb74a6 AM |
1658 | hdr = elf_elfsections (abfd)[shindex]; |
1659 | ehdr = elf_elfheader (abfd); | |
1660 | name = bfd_elf_string_from_elf_section (abfd, ehdr->e_shstrndx, | |
1b3a8575 | 1661 | hdr->sh_name); |
933d961a | 1662 | if (name == NULL) |
bf67003b | 1663 | goto fail; |
252b5132 | 1664 | |
4fbb74a6 | 1665 | bed = get_elf_backend_data (abfd); |
252b5132 RH |
1666 | switch (hdr->sh_type) |
1667 | { | |
1668 | case SHT_NULL: | |
1669 | /* Inactive section. Throw it away. */ | |
bf67003b | 1670 | goto success; |
252b5132 | 1671 | |
bf67003b NC |
1672 | case SHT_PROGBITS: /* Normal section with contents. */ |
1673 | case SHT_NOBITS: /* .bss section. */ | |
1674 | case SHT_HASH: /* .hash section. */ | |
1675 | case SHT_NOTE: /* .note section. */ | |
25e27870 L |
1676 | case SHT_INIT_ARRAY: /* .init_array section. */ |
1677 | case SHT_FINI_ARRAY: /* .fini_array section. */ | |
1678 | case SHT_PREINIT_ARRAY: /* .preinit_array section. */ | |
7f1204bb | 1679 | case SHT_GNU_LIBLIST: /* .gnu.liblist section. */ |
fdc90cb4 | 1680 | case SHT_GNU_HASH: /* .gnu.hash section. */ |
bf67003b NC |
1681 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); |
1682 | goto success; | |
252b5132 | 1683 | |
797fc050 | 1684 | case SHT_DYNAMIC: /* Dynamic linking information. */ |
6dc132d9 | 1685 | if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex)) |
bf67003b NC |
1686 | goto fail; |
1687 | ||
cfcac11d NC |
1688 | if (hdr->sh_link > elf_numsections (abfd)) |
1689 | { | |
caa83f8b | 1690 | /* PR 10478: Accept Solaris binaries with a sh_link |
cfcac11d NC |
1691 | field set to SHN_BEFORE or SHN_AFTER. */ |
1692 | switch (bfd_get_arch (abfd)) | |
1693 | { | |
caa83f8b | 1694 | case bfd_arch_i386: |
cfcac11d NC |
1695 | case bfd_arch_sparc: |
1696 | if (hdr->sh_link == (SHN_LORESERVE & 0xffff) /* SHN_BEFORE */ | |
1697 | || hdr->sh_link == ((SHN_LORESERVE + 1) & 0xffff) /* SHN_AFTER */) | |
1698 | break; | |
1699 | /* Otherwise fall through. */ | |
1700 | default: | |
bf67003b | 1701 | goto fail; |
cfcac11d NC |
1702 | } |
1703 | } | |
1704 | else if (elf_elfsections (abfd)[hdr->sh_link] == NULL) | |
bf67003b | 1705 | goto fail; |
cfcac11d | 1706 | else if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_STRTAB) |
797fc050 AM |
1707 | { |
1708 | Elf_Internal_Shdr *dynsymhdr; | |
1709 | ||
1710 | /* The shared libraries distributed with hpux11 have a bogus | |
1711 | sh_link field for the ".dynamic" section. Find the | |
1712 | string table for the ".dynsym" section instead. */ | |
1713 | if (elf_dynsymtab (abfd) != 0) | |
1714 | { | |
1715 | dynsymhdr = elf_elfsections (abfd)[elf_dynsymtab (abfd)]; | |
1716 | hdr->sh_link = dynsymhdr->sh_link; | |
1717 | } | |
1718 | else | |
1719 | { | |
1720 | unsigned int i, num_sec; | |
1721 | ||
1722 | num_sec = elf_numsections (abfd); | |
1723 | for (i = 1; i < num_sec; i++) | |
1724 | { | |
1725 | dynsymhdr = elf_elfsections (abfd)[i]; | |
1726 | if (dynsymhdr->sh_type == SHT_DYNSYM) | |
1727 | { | |
1728 | hdr->sh_link = dynsymhdr->sh_link; | |
1729 | break; | |
1730 | } | |
1731 | } | |
1732 | } | |
1733 | } | |
bf67003b | 1734 | goto success; |
797fc050 | 1735 | |
bf67003b | 1736 | case SHT_SYMTAB: /* A symbol table. */ |
252b5132 | 1737 | if (elf_onesymtab (abfd) == shindex) |
bf67003b | 1738 | goto success; |
252b5132 | 1739 | |
a50b2160 | 1740 | if (hdr->sh_entsize != bed->s->sizeof_sym) |
bf67003b NC |
1741 | goto fail; |
1742 | ||
3337c1e5 | 1743 | if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size) |
eee3b786 AM |
1744 | { |
1745 | if (hdr->sh_size != 0) | |
bf67003b | 1746 | goto fail; |
eee3b786 AM |
1747 | /* Some assemblers erroneously set sh_info to one with a |
1748 | zero sh_size. ld sees this as a global symbol count | |
1749 | of (unsigned) -1. Fix it here. */ | |
1750 | hdr->sh_info = 0; | |
bf67003b | 1751 | goto success; |
eee3b786 | 1752 | } |
bf67003b | 1753 | |
252b5132 RH |
1754 | BFD_ASSERT (elf_onesymtab (abfd) == 0); |
1755 | elf_onesymtab (abfd) = shindex; | |
1756 | elf_tdata (abfd)->symtab_hdr = *hdr; | |
1757 | elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->symtab_hdr; | |
1758 | abfd->flags |= HAS_SYMS; | |
1759 | ||
1760 | /* Sometimes a shared object will map in the symbol table. If | |
08a40648 AM |
1761 | SHF_ALLOC is set, and this is a shared object, then we also |
1762 | treat this section as a BFD section. We can not base the | |
1763 | decision purely on SHF_ALLOC, because that flag is sometimes | |
1764 | set in a relocatable object file, which would confuse the | |
1765 | linker. */ | |
252b5132 RH |
1766 | if ((hdr->sh_flags & SHF_ALLOC) != 0 |
1767 | && (abfd->flags & DYNAMIC) != 0 | |
6dc132d9 L |
1768 | && ! _bfd_elf_make_section_from_shdr (abfd, hdr, name, |
1769 | shindex)) | |
bf67003b | 1770 | goto fail; |
252b5132 | 1771 | |
1b3a8575 AM |
1772 | /* Go looking for SHT_SYMTAB_SHNDX too, since if there is one we |
1773 | can't read symbols without that section loaded as well. It | |
1774 | is most likely specified by the next section header. */ | |
1775 | if (elf_elfsections (abfd)[elf_symtab_shndx (abfd)]->sh_link != shindex) | |
1776 | { | |
1777 | unsigned int i, num_sec; | |
1778 | ||
1779 | num_sec = elf_numsections (abfd); | |
1780 | for (i = shindex + 1; i < num_sec; i++) | |
1781 | { | |
1782 | Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i]; | |
1783 | if (hdr2->sh_type == SHT_SYMTAB_SHNDX | |
1784 | && hdr2->sh_link == shindex) | |
1785 | break; | |
1786 | } | |
1787 | if (i == num_sec) | |
1788 | for (i = 1; i < shindex; i++) | |
1789 | { | |
1790 | Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i]; | |
1791 | if (hdr2->sh_type == SHT_SYMTAB_SHNDX | |
1792 | && hdr2->sh_link == shindex) | |
1793 | break; | |
1794 | } | |
1795 | if (i != shindex) | |
bf67003b | 1796 | ret = bfd_section_from_shdr (abfd, i); |
1b3a8575 | 1797 | } |
bf67003b | 1798 | goto success; |
252b5132 | 1799 | |
bf67003b | 1800 | case SHT_DYNSYM: /* A dynamic symbol table. */ |
252b5132 | 1801 | if (elf_dynsymtab (abfd) == shindex) |
bf67003b | 1802 | goto success; |
252b5132 | 1803 | |
a50b2160 | 1804 | if (hdr->sh_entsize != bed->s->sizeof_sym) |
bf67003b NC |
1805 | goto fail; |
1806 | ||
eee3b786 AM |
1807 | if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size) |
1808 | { | |
1809 | if (hdr->sh_size != 0) | |
bf67003b NC |
1810 | goto fail; |
1811 | ||
eee3b786 AM |
1812 | /* Some linkers erroneously set sh_info to one with a |
1813 | zero sh_size. ld sees this as a global symbol count | |
1814 | of (unsigned) -1. Fix it here. */ | |
1815 | hdr->sh_info = 0; | |
bf67003b | 1816 | goto success; |
eee3b786 | 1817 | } |
bf67003b | 1818 | |
252b5132 RH |
1819 | BFD_ASSERT (elf_dynsymtab (abfd) == 0); |
1820 | elf_dynsymtab (abfd) = shindex; | |
1821 | elf_tdata (abfd)->dynsymtab_hdr = *hdr; | |
1822 | elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->dynsymtab_hdr; | |
1823 | abfd->flags |= HAS_SYMS; | |
1824 | ||
1825 | /* Besides being a symbol table, we also treat this as a regular | |
1826 | section, so that objcopy can handle it. */ | |
bf67003b NC |
1827 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); |
1828 | goto success; | |
252b5132 | 1829 | |
bf67003b | 1830 | case SHT_SYMTAB_SHNDX: /* Symbol section indices when >64k sections. */ |
9ad5cbcf | 1831 | if (elf_symtab_shndx (abfd) == shindex) |
bf67003b | 1832 | goto success; |
9ad5cbcf | 1833 | |
1b3a8575 | 1834 | BFD_ASSERT (elf_symtab_shndx (abfd) == 0); |
9ad5cbcf AM |
1835 | elf_symtab_shndx (abfd) = shindex; |
1836 | elf_tdata (abfd)->symtab_shndx_hdr = *hdr; | |
1837 | elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->symtab_shndx_hdr; | |
bf67003b | 1838 | goto success; |
9ad5cbcf | 1839 | |
bf67003b | 1840 | case SHT_STRTAB: /* A string table. */ |
252b5132 | 1841 | if (hdr->bfd_section != NULL) |
bf67003b NC |
1842 | goto success; |
1843 | ||
252b5132 RH |
1844 | if (ehdr->e_shstrndx == shindex) |
1845 | { | |
1846 | elf_tdata (abfd)->shstrtab_hdr = *hdr; | |
1847 | elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->shstrtab_hdr; | |
bf67003b | 1848 | goto success; |
252b5132 | 1849 | } |
bf67003b | 1850 | |
1b3a8575 AM |
1851 | if (elf_elfsections (abfd)[elf_onesymtab (abfd)]->sh_link == shindex) |
1852 | { | |
1853 | symtab_strtab: | |
1854 | elf_tdata (abfd)->strtab_hdr = *hdr; | |
1855 | elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->strtab_hdr; | |
bf67003b | 1856 | goto success; |
1b3a8575 | 1857 | } |
bf67003b | 1858 | |
1b3a8575 AM |
1859 | if (elf_elfsections (abfd)[elf_dynsymtab (abfd)]->sh_link == shindex) |
1860 | { | |
1861 | dynsymtab_strtab: | |
1862 | elf_tdata (abfd)->dynstrtab_hdr = *hdr; | |
1863 | hdr = &elf_tdata (abfd)->dynstrtab_hdr; | |
1864 | elf_elfsections (abfd)[shindex] = hdr; | |
1865 | /* We also treat this as a regular section, so that objcopy | |
1866 | can handle it. */ | |
bf67003b NC |
1867 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, |
1868 | shindex); | |
1869 | goto success; | |
1b3a8575 | 1870 | } |
252b5132 | 1871 | |
1b3a8575 AM |
1872 | /* If the string table isn't one of the above, then treat it as a |
1873 | regular section. We need to scan all the headers to be sure, | |
1874 | just in case this strtab section appeared before the above. */ | |
1875 | if (elf_onesymtab (abfd) == 0 || elf_dynsymtab (abfd) == 0) | |
1876 | { | |
1877 | unsigned int i, num_sec; | |
252b5132 | 1878 | |
1b3a8575 AM |
1879 | num_sec = elf_numsections (abfd); |
1880 | for (i = 1; i < num_sec; i++) | |
1881 | { | |
1882 | Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i]; | |
1883 | if (hdr2->sh_link == shindex) | |
1884 | { | |
933d961a JJ |
1885 | /* Prevent endless recursion on broken objects. */ |
1886 | if (i == shindex) | |
bf67003b | 1887 | goto fail; |
1b3a8575 | 1888 | if (! bfd_section_from_shdr (abfd, i)) |
bf67003b | 1889 | goto fail; |
1b3a8575 AM |
1890 | if (elf_onesymtab (abfd) == i) |
1891 | goto symtab_strtab; | |
1892 | if (elf_dynsymtab (abfd) == i) | |
1893 | goto dynsymtab_strtab; | |
1894 | } | |
1895 | } | |
1896 | } | |
bf67003b NC |
1897 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); |
1898 | goto success; | |
252b5132 RH |
1899 | |
1900 | case SHT_REL: | |
1901 | case SHT_RELA: | |
1902 | /* *These* do a lot of work -- but build no sections! */ | |
1903 | { | |
1904 | asection *target_sect; | |
d4730f92 | 1905 | Elf_Internal_Shdr *hdr2, **p_hdr; |
9ad5cbcf | 1906 | unsigned int num_sec = elf_numsections (abfd); |
d4730f92 BS |
1907 | struct bfd_elf_section_data *esdt; |
1908 | bfd_size_type amt; | |
252b5132 | 1909 | |
aa2ca951 JJ |
1910 | if (hdr->sh_entsize |
1911 | != (bfd_size_type) (hdr->sh_type == SHT_REL | |
a50b2160 | 1912 | ? bed->s->sizeof_rel : bed->s->sizeof_rela)) |
bf67003b | 1913 | goto fail; |
a50b2160 | 1914 | |
03ae5f59 | 1915 | /* Check for a bogus link to avoid crashing. */ |
4fbb74a6 | 1916 | if (hdr->sh_link >= num_sec) |
03ae5f59 ILT |
1917 | { |
1918 | ((*_bfd_error_handler) | |
d003868e AM |
1919 | (_("%B: invalid link %lu for reloc section %s (index %u)"), |
1920 | abfd, hdr->sh_link, name, shindex)); | |
bf67003b NC |
1921 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, |
1922 | shindex); | |
1923 | goto success; | |
03ae5f59 ILT |
1924 | } |
1925 | ||
252b5132 RH |
1926 | /* For some incomprehensible reason Oracle distributes |
1927 | libraries for Solaris in which some of the objects have | |
1928 | bogus sh_link fields. It would be nice if we could just | |
1929 | reject them, but, unfortunately, some people need to use | |
1930 | them. We scan through the section headers; if we find only | |
1931 | one suitable symbol table, we clobber the sh_link to point | |
83b89087 L |
1932 | to it. I hope this doesn't break anything. |
1933 | ||
1934 | Don't do it on executable nor shared library. */ | |
1935 | if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0 | |
1936 | && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_SYMTAB | |
252b5132 RH |
1937 | && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_DYNSYM) |
1938 | { | |
9ad5cbcf | 1939 | unsigned int scan; |
252b5132 RH |
1940 | int found; |
1941 | ||
1942 | found = 0; | |
9ad5cbcf | 1943 | for (scan = 1; scan < num_sec; scan++) |
252b5132 RH |
1944 | { |
1945 | if (elf_elfsections (abfd)[scan]->sh_type == SHT_SYMTAB | |
1946 | || elf_elfsections (abfd)[scan]->sh_type == SHT_DYNSYM) | |
1947 | { | |
1948 | if (found != 0) | |
1949 | { | |
1950 | found = 0; | |
1951 | break; | |
1952 | } | |
1953 | found = scan; | |
1954 | } | |
1955 | } | |
1956 | if (found != 0) | |
1957 | hdr->sh_link = found; | |
1958 | } | |
1959 | ||
1960 | /* Get the symbol table. */ | |
1b3a8575 AM |
1961 | if ((elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_SYMTAB |
1962 | || elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_DYNSYM) | |
252b5132 | 1963 | && ! bfd_section_from_shdr (abfd, hdr->sh_link)) |
bf67003b | 1964 | goto fail; |
252b5132 RH |
1965 | |
1966 | /* If this reloc section does not use the main symbol table we | |
1967 | don't treat it as a reloc section. BFD can't adequately | |
1968 | represent such a section, so at least for now, we don't | |
c044fabd | 1969 | try. We just present it as a normal section. We also |
60bcf0fa | 1970 | can't use it as a reloc section if it points to the null |
83b89087 L |
1971 | section, an invalid section, another reloc section, or its |
1972 | sh_link points to the null section. */ | |
185ef66d | 1973 | if (hdr->sh_link != elf_onesymtab (abfd) |
83b89087 | 1974 | || hdr->sh_link == SHN_UNDEF |
185ef66d | 1975 | || hdr->sh_info == SHN_UNDEF |
185ef66d AM |
1976 | || hdr->sh_info >= num_sec |
1977 | || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_REL | |
1978 | || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_RELA) | |
bf67003b NC |
1979 | { |
1980 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, | |
1981 | shindex); | |
1982 | goto success; | |
1983 | } | |
252b5132 RH |
1984 | |
1985 | if (! bfd_section_from_shdr (abfd, hdr->sh_info)) | |
bf67003b NC |
1986 | goto fail; |
1987 | ||
252b5132 RH |
1988 | target_sect = bfd_section_from_elf_index (abfd, hdr->sh_info); |
1989 | if (target_sect == NULL) | |
bf67003b | 1990 | goto fail; |
252b5132 | 1991 | |
d4730f92 BS |
1992 | esdt = elf_section_data (target_sect); |
1993 | if (hdr->sh_type == SHT_RELA) | |
1994 | p_hdr = &esdt->rela.hdr; | |
252b5132 | 1995 | else |
d4730f92 BS |
1996 | p_hdr = &esdt->rel.hdr; |
1997 | ||
06614111 NC |
1998 | /* PR 17512: file: 0b4f81b7. */ |
1999 | if (*p_hdr != NULL) | |
2000 | goto fail; | |
d4730f92 BS |
2001 | amt = sizeof (*hdr2); |
2002 | hdr2 = (Elf_Internal_Shdr *) bfd_alloc (abfd, amt); | |
2003 | if (hdr2 == NULL) | |
bf67003b | 2004 | goto fail; |
252b5132 | 2005 | *hdr2 = *hdr; |
d4730f92 | 2006 | *p_hdr = hdr2; |
252b5132 | 2007 | elf_elfsections (abfd)[shindex] = hdr2; |
d9bc7a44 | 2008 | target_sect->reloc_count += NUM_SHDR_ENTRIES (hdr); |
252b5132 RH |
2009 | target_sect->flags |= SEC_RELOC; |
2010 | target_sect->relocation = NULL; | |
2011 | target_sect->rel_filepos = hdr->sh_offset; | |
bf572ba0 MM |
2012 | /* In the section to which the relocations apply, mark whether |
2013 | its relocations are of the REL or RELA variety. */ | |
72730e0c | 2014 | if (hdr->sh_size != 0) |
d4730f92 BS |
2015 | { |
2016 | if (hdr->sh_type == SHT_RELA) | |
2017 | target_sect->use_rela_p = 1; | |
2018 | } | |
252b5132 | 2019 | abfd->flags |= HAS_RELOC; |
bf67003b | 2020 | goto success; |
252b5132 | 2021 | } |
252b5132 RH |
2022 | |
2023 | case SHT_GNU_verdef: | |
2024 | elf_dynverdef (abfd) = shindex; | |
2025 | elf_tdata (abfd)->dynverdef_hdr = *hdr; | |
bf67003b NC |
2026 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); |
2027 | goto success; | |
252b5132 RH |
2028 | |
2029 | case SHT_GNU_versym: | |
a50b2160 | 2030 | if (hdr->sh_entsize != sizeof (Elf_External_Versym)) |
bf67003b NC |
2031 | goto fail; |
2032 | ||
252b5132 RH |
2033 | elf_dynversym (abfd) = shindex; |
2034 | elf_tdata (abfd)->dynversym_hdr = *hdr; | |
bf67003b NC |
2035 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); |
2036 | goto success; | |
252b5132 RH |
2037 | |
2038 | case SHT_GNU_verneed: | |
2039 | elf_dynverref (abfd) = shindex; | |
2040 | elf_tdata (abfd)->dynverref_hdr = *hdr; | |
bf67003b NC |
2041 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); |
2042 | goto success; | |
252b5132 RH |
2043 | |
2044 | case SHT_SHLIB: | |
bf67003b | 2045 | goto success; |
252b5132 | 2046 | |
dbb410c3 | 2047 | case SHT_GROUP: |
44534af3 | 2048 | if (! IS_VALID_GROUP_SECTION_HEADER (hdr, GRP_ENTRY_SIZE)) |
bf67003b NC |
2049 | goto fail; |
2050 | ||
6dc132d9 | 2051 | if (!_bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex)) |
bf67003b NC |
2052 | goto fail; |
2053 | ||
dbb410c3 AM |
2054 | if (hdr->contents != NULL) |
2055 | { | |
2056 | Elf_Internal_Group *idx = (Elf_Internal_Group *) hdr->contents; | |
06614111 | 2057 | unsigned int n_elt = hdr->sh_size / sizeof (* idx); |
dbb410c3 AM |
2058 | asection *s; |
2059 | ||
06614111 NC |
2060 | if (n_elt == 0) |
2061 | goto fail; | |
b885599b AM |
2062 | if (idx->flags & GRP_COMDAT) |
2063 | hdr->bfd_section->flags | |
2064 | |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD; | |
2065 | ||
45c5e9ed L |
2066 | /* We try to keep the same section order as it comes in. */ |
2067 | idx += n_elt; | |
06614111 | 2068 | |
dbb410c3 | 2069 | while (--n_elt != 0) |
1783205a NC |
2070 | { |
2071 | --idx; | |
2072 | ||
2073 | if (idx->shdr != NULL | |
2074 | && (s = idx->shdr->bfd_section) != NULL | |
2075 | && elf_next_in_group (s) != NULL) | |
2076 | { | |
2077 | elf_next_in_group (hdr->bfd_section) = s; | |
2078 | break; | |
2079 | } | |
2080 | } | |
dbb410c3 | 2081 | } |
bf67003b | 2082 | goto success; |
dbb410c3 | 2083 | |
252b5132 | 2084 | default: |
104d59d1 JM |
2085 | /* Possibly an attributes section. */ |
2086 | if (hdr->sh_type == SHT_GNU_ATTRIBUTES | |
2087 | || hdr->sh_type == bed->obj_attrs_section_type) | |
2088 | { | |
2089 | if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex)) | |
bf67003b | 2090 | goto fail; |
104d59d1 | 2091 | _bfd_elf_parse_attributes (abfd, hdr); |
bf67003b | 2092 | goto success; |
104d59d1 JM |
2093 | } |
2094 | ||
252b5132 | 2095 | /* Check for any processor-specific section types. */ |
3eb70a79 | 2096 | if (bed->elf_backend_section_from_shdr (abfd, hdr, name, shindex)) |
bf67003b | 2097 | goto success; |
3eb70a79 L |
2098 | |
2099 | if (hdr->sh_type >= SHT_LOUSER && hdr->sh_type <= SHT_HIUSER) | |
2100 | { | |
2101 | if ((hdr->sh_flags & SHF_ALLOC) != 0) | |
2102 | /* FIXME: How to properly handle allocated section reserved | |
2103 | for applications? */ | |
2104 | (*_bfd_error_handler) | |
2105 | (_("%B: don't know how to handle allocated, application " | |
2106 | "specific section `%s' [0x%8x]"), | |
2107 | abfd, name, hdr->sh_type); | |
2108 | else | |
bf67003b NC |
2109 | { |
2110 | /* Allow sections reserved for applications. */ | |
2111 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, | |
2112 | shindex); | |
2113 | goto success; | |
2114 | } | |
3eb70a79 L |
2115 | } |
2116 | else if (hdr->sh_type >= SHT_LOPROC | |
2117 | && hdr->sh_type <= SHT_HIPROC) | |
2118 | /* FIXME: We should handle this section. */ | |
2119 | (*_bfd_error_handler) | |
2120 | (_("%B: don't know how to handle processor specific section " | |
2121 | "`%s' [0x%8x]"), | |
2122 | abfd, name, hdr->sh_type); | |
2123 | else if (hdr->sh_type >= SHT_LOOS && hdr->sh_type <= SHT_HIOS) | |
ff15b240 NC |
2124 | { |
2125 | /* Unrecognised OS-specific sections. */ | |
2126 | if ((hdr->sh_flags & SHF_OS_NONCONFORMING) != 0) | |
2127 | /* SHF_OS_NONCONFORMING indicates that special knowledge is | |
08a40648 | 2128 | required to correctly process the section and the file should |
ff15b240 NC |
2129 | be rejected with an error message. */ |
2130 | (*_bfd_error_handler) | |
2131 | (_("%B: don't know how to handle OS specific section " | |
2132 | "`%s' [0x%8x]"), | |
2133 | abfd, name, hdr->sh_type); | |
2134 | else | |
bf67003b NC |
2135 | { |
2136 | /* Otherwise it should be processed. */ | |
2137 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); | |
2138 | goto success; | |
2139 | } | |
ff15b240 | 2140 | } |
3eb70a79 L |
2141 | else |
2142 | /* FIXME: We should handle this section. */ | |
2143 | (*_bfd_error_handler) | |
2144 | (_("%B: don't know how to handle section `%s' [0x%8x]"), | |
2145 | abfd, name, hdr->sh_type); | |
2146 | ||
bf67003b | 2147 | goto fail; |
252b5132 RH |
2148 | } |
2149 | ||
bf67003b NC |
2150 | fail: |
2151 | ret = FALSE; | |
2152 | success: | |
e5b470e2 | 2153 | if (sections_being_created && sections_being_created_abfd == abfd) |
bf67003b NC |
2154 | sections_being_created [shindex] = FALSE; |
2155 | if (-- nesting == 0) | |
5a4b0ccc NC |
2156 | { |
2157 | sections_being_created = NULL; | |
2158 | sections_being_created_abfd = abfd; | |
2159 | } | |
bf67003b | 2160 | return ret; |
252b5132 RH |
2161 | } |
2162 | ||
87d72d41 | 2163 | /* Return the local symbol specified by ABFD, R_SYMNDX. */ |
ec338859 | 2164 | |
87d72d41 AM |
2165 | Elf_Internal_Sym * |
2166 | bfd_sym_from_r_symndx (struct sym_cache *cache, | |
2167 | bfd *abfd, | |
2168 | unsigned long r_symndx) | |
ec338859 | 2169 | { |
ec338859 AM |
2170 | unsigned int ent = r_symndx % LOCAL_SYM_CACHE_SIZE; |
2171 | ||
a5d1b3b5 AM |
2172 | if (cache->abfd != abfd || cache->indx[ent] != r_symndx) |
2173 | { | |
2174 | Elf_Internal_Shdr *symtab_hdr; | |
2175 | unsigned char esym[sizeof (Elf64_External_Sym)]; | |
2176 | Elf_External_Sym_Shndx eshndx; | |
ec338859 | 2177 | |
a5d1b3b5 AM |
2178 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; |
2179 | if (bfd_elf_get_elf_syms (abfd, symtab_hdr, 1, r_symndx, | |
87d72d41 | 2180 | &cache->sym[ent], esym, &eshndx) == NULL) |
a5d1b3b5 | 2181 | return NULL; |
9ad5cbcf | 2182 | |
a5d1b3b5 AM |
2183 | if (cache->abfd != abfd) |
2184 | { | |
2185 | memset (cache->indx, -1, sizeof (cache->indx)); | |
2186 | cache->abfd = abfd; | |
2187 | } | |
2188 | cache->indx[ent] = r_symndx; | |
ec338859 | 2189 | } |
a5d1b3b5 | 2190 | |
87d72d41 | 2191 | return &cache->sym[ent]; |
ec338859 AM |
2192 | } |
2193 | ||
252b5132 RH |
2194 | /* Given an ELF section number, retrieve the corresponding BFD |
2195 | section. */ | |
2196 | ||
2197 | asection * | |
91d6fa6a | 2198 | bfd_section_from_elf_index (bfd *abfd, unsigned int sec_index) |
252b5132 | 2199 | { |
91d6fa6a | 2200 | if (sec_index >= elf_numsections (abfd)) |
252b5132 | 2201 | return NULL; |
91d6fa6a | 2202 | return elf_elfsections (abfd)[sec_index]->bfd_section; |
252b5132 RH |
2203 | } |
2204 | ||
b35d266b | 2205 | static const struct bfd_elf_special_section special_sections_b[] = |
2f89ff8d | 2206 | { |
0112cd26 NC |
2207 | { STRING_COMMA_LEN (".bss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE }, |
2208 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2209 | }; |
2210 | ||
b35d266b | 2211 | static const struct bfd_elf_special_section special_sections_c[] = |
7f4d3958 | 2212 | { |
0112cd26 NC |
2213 | { STRING_COMMA_LEN (".comment"), 0, SHT_PROGBITS, 0 }, |
2214 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2215 | }; |
2216 | ||
b35d266b | 2217 | static const struct bfd_elf_special_section special_sections_d[] = |
7f4d3958 | 2218 | { |
0112cd26 NC |
2219 | { STRING_COMMA_LEN (".data"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE }, |
2220 | { STRING_COMMA_LEN (".data1"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE }, | |
a9a72a65 DE |
2221 | /* There are more DWARF sections than these, but they needn't be added here |
2222 | unless you have to cope with broken compilers that don't emit section | |
2223 | attributes or you want to help the user writing assembler. */ | |
0112cd26 NC |
2224 | { STRING_COMMA_LEN (".debug"), 0, SHT_PROGBITS, 0 }, |
2225 | { STRING_COMMA_LEN (".debug_line"), 0, SHT_PROGBITS, 0 }, | |
2226 | { STRING_COMMA_LEN (".debug_info"), 0, SHT_PROGBITS, 0 }, | |
2227 | { STRING_COMMA_LEN (".debug_abbrev"), 0, SHT_PROGBITS, 0 }, | |
2228 | { STRING_COMMA_LEN (".debug_aranges"), 0, SHT_PROGBITS, 0 }, | |
2229 | { STRING_COMMA_LEN (".dynamic"), 0, SHT_DYNAMIC, SHF_ALLOC }, | |
2230 | { STRING_COMMA_LEN (".dynstr"), 0, SHT_STRTAB, SHF_ALLOC }, | |
2231 | { STRING_COMMA_LEN (".dynsym"), 0, SHT_DYNSYM, SHF_ALLOC }, | |
2232 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2233 | }; |
2234 | ||
b35d266b | 2235 | static const struct bfd_elf_special_section special_sections_f[] = |
7f4d3958 | 2236 | { |
0112cd26 NC |
2237 | { STRING_COMMA_LEN (".fini"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR }, |
2238 | { STRING_COMMA_LEN (".fini_array"), 0, SHT_FINI_ARRAY, SHF_ALLOC + SHF_WRITE }, | |
2239 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2240 | }; |
2241 | ||
b35d266b | 2242 | static const struct bfd_elf_special_section special_sections_g[] = |
7f4d3958 | 2243 | { |
0112cd26 | 2244 | { STRING_COMMA_LEN (".gnu.linkonce.b"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE }, |
68efed41 | 2245 | { STRING_COMMA_LEN (".gnu.lto_"), -1, SHT_PROGBITS, SHF_EXCLUDE }, |
0112cd26 NC |
2246 | { STRING_COMMA_LEN (".got"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE }, |
2247 | { STRING_COMMA_LEN (".gnu.version"), 0, SHT_GNU_versym, 0 }, | |
2248 | { STRING_COMMA_LEN (".gnu.version_d"), 0, SHT_GNU_verdef, 0 }, | |
2249 | { STRING_COMMA_LEN (".gnu.version_r"), 0, SHT_GNU_verneed, 0 }, | |
2250 | { STRING_COMMA_LEN (".gnu.liblist"), 0, SHT_GNU_LIBLIST, SHF_ALLOC }, | |
2251 | { STRING_COMMA_LEN (".gnu.conflict"), 0, SHT_RELA, SHF_ALLOC }, | |
2252 | { STRING_COMMA_LEN (".gnu.hash"), 0, SHT_GNU_HASH, SHF_ALLOC }, | |
2253 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2254 | }; |
2255 | ||
b35d266b | 2256 | static const struct bfd_elf_special_section special_sections_h[] = |
7f4d3958 | 2257 | { |
0112cd26 NC |
2258 | { STRING_COMMA_LEN (".hash"), 0, SHT_HASH, SHF_ALLOC }, |
2259 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2260 | }; |
2261 | ||
b35d266b | 2262 | static const struct bfd_elf_special_section special_sections_i[] = |
7f4d3958 | 2263 | { |
0112cd26 NC |
2264 | { STRING_COMMA_LEN (".init"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR }, |
2265 | { STRING_COMMA_LEN (".init_array"), 0, SHT_INIT_ARRAY, SHF_ALLOC + SHF_WRITE }, | |
2266 | { STRING_COMMA_LEN (".interp"), 0, SHT_PROGBITS, 0 }, | |
2267 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2268 | }; |
2269 | ||
b35d266b | 2270 | static const struct bfd_elf_special_section special_sections_l[] = |
7f4d3958 | 2271 | { |
0112cd26 NC |
2272 | { STRING_COMMA_LEN (".line"), 0, SHT_PROGBITS, 0 }, |
2273 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2274 | }; |
2275 | ||
b35d266b | 2276 | static const struct bfd_elf_special_section special_sections_n[] = |
7f4d3958 | 2277 | { |
0112cd26 NC |
2278 | { STRING_COMMA_LEN (".note.GNU-stack"), 0, SHT_PROGBITS, 0 }, |
2279 | { STRING_COMMA_LEN (".note"), -1, SHT_NOTE, 0 }, | |
2280 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2281 | }; |
2282 | ||
b35d266b | 2283 | static const struct bfd_elf_special_section special_sections_p[] = |
7f4d3958 | 2284 | { |
0112cd26 NC |
2285 | { STRING_COMMA_LEN (".preinit_array"), 0, SHT_PREINIT_ARRAY, SHF_ALLOC + SHF_WRITE }, |
2286 | { STRING_COMMA_LEN (".plt"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR }, | |
2287 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2288 | }; |
2289 | ||
b35d266b | 2290 | static const struct bfd_elf_special_section special_sections_r[] = |
7f4d3958 | 2291 | { |
0112cd26 NC |
2292 | { STRING_COMMA_LEN (".rodata"), -2, SHT_PROGBITS, SHF_ALLOC }, |
2293 | { STRING_COMMA_LEN (".rodata1"), 0, SHT_PROGBITS, SHF_ALLOC }, | |
2294 | { STRING_COMMA_LEN (".rela"), -1, SHT_RELA, 0 }, | |
2295 | { STRING_COMMA_LEN (".rel"), -1, SHT_REL, 0 }, | |
2296 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2297 | }; |
2298 | ||
b35d266b | 2299 | static const struct bfd_elf_special_section special_sections_s[] = |
7f4d3958 | 2300 | { |
0112cd26 NC |
2301 | { STRING_COMMA_LEN (".shstrtab"), 0, SHT_STRTAB, 0 }, |
2302 | { STRING_COMMA_LEN (".strtab"), 0, SHT_STRTAB, 0 }, | |
2303 | { STRING_COMMA_LEN (".symtab"), 0, SHT_SYMTAB, 0 }, | |
60ff4dc4 HPN |
2304 | /* See struct bfd_elf_special_section declaration for the semantics of |
2305 | this special case where .prefix_length != strlen (.prefix). */ | |
2306 | { ".stabstr", 5, 3, SHT_STRTAB, 0 }, | |
0112cd26 | 2307 | { NULL, 0, 0, 0, 0 } |
2f89ff8d L |
2308 | }; |
2309 | ||
b35d266b | 2310 | static const struct bfd_elf_special_section special_sections_t[] = |
7f4d3958 | 2311 | { |
0112cd26 NC |
2312 | { STRING_COMMA_LEN (".text"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR }, |
2313 | { STRING_COMMA_LEN (".tbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS }, | |
2314 | { STRING_COMMA_LEN (".tdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS }, | |
2315 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2316 | }; |
2317 | ||
1b315056 CS |
2318 | static const struct bfd_elf_special_section special_sections_z[] = |
2319 | { | |
2320 | { STRING_COMMA_LEN (".zdebug_line"), 0, SHT_PROGBITS, 0 }, | |
2321 | { STRING_COMMA_LEN (".zdebug_info"), 0, SHT_PROGBITS, 0 }, | |
2322 | { STRING_COMMA_LEN (".zdebug_abbrev"), 0, SHT_PROGBITS, 0 }, | |
2323 | { STRING_COMMA_LEN (".zdebug_aranges"), 0, SHT_PROGBITS, 0 }, | |
2324 | { NULL, 0, 0, 0, 0 } | |
2325 | }; | |
2326 | ||
e4c93b56 | 2327 | static const struct bfd_elf_special_section * const special_sections[] = |
7f4d3958 | 2328 | { |
7f4d3958 | 2329 | special_sections_b, /* 'b' */ |
98ece1b3 | 2330 | special_sections_c, /* 'c' */ |
7f4d3958 L |
2331 | special_sections_d, /* 'd' */ |
2332 | NULL, /* 'e' */ | |
2333 | special_sections_f, /* 'f' */ | |
2334 | special_sections_g, /* 'g' */ | |
2335 | special_sections_h, /* 'h' */ | |
2336 | special_sections_i, /* 'i' */ | |
2337 | NULL, /* 'j' */ | |
2338 | NULL, /* 'k' */ | |
2339 | special_sections_l, /* 'l' */ | |
2340 | NULL, /* 'm' */ | |
2341 | special_sections_n, /* 'n' */ | |
2342 | NULL, /* 'o' */ | |
2343 | special_sections_p, /* 'p' */ | |
2344 | NULL, /* 'q' */ | |
2345 | special_sections_r, /* 'r' */ | |
2346 | special_sections_s, /* 's' */ | |
2347 | special_sections_t, /* 't' */ | |
1b315056 CS |
2348 | NULL, /* 'u' */ |
2349 | NULL, /* 'v' */ | |
2350 | NULL, /* 'w' */ | |
2351 | NULL, /* 'x' */ | |
2352 | NULL, /* 'y' */ | |
2353 | special_sections_z /* 'z' */ | |
7f4d3958 L |
2354 | }; |
2355 | ||
551b43fd AM |
2356 | const struct bfd_elf_special_section * |
2357 | _bfd_elf_get_special_section (const char *name, | |
2358 | const struct bfd_elf_special_section *spec, | |
2359 | unsigned int rela) | |
2f89ff8d L |
2360 | { |
2361 | int i; | |
7f4d3958 | 2362 | int len; |
7f4d3958 | 2363 | |
551b43fd | 2364 | len = strlen (name); |
7f4d3958 | 2365 | |
551b43fd | 2366 | for (i = 0; spec[i].prefix != NULL; i++) |
7dcb9820 AM |
2367 | { |
2368 | int suffix_len; | |
551b43fd | 2369 | int prefix_len = spec[i].prefix_length; |
7dcb9820 AM |
2370 | |
2371 | if (len < prefix_len) | |
2372 | continue; | |
551b43fd | 2373 | if (memcmp (name, spec[i].prefix, prefix_len) != 0) |
7dcb9820 AM |
2374 | continue; |
2375 | ||
551b43fd | 2376 | suffix_len = spec[i].suffix_length; |
7dcb9820 AM |
2377 | if (suffix_len <= 0) |
2378 | { | |
2379 | if (name[prefix_len] != 0) | |
2380 | { | |
2381 | if (suffix_len == 0) | |
2382 | continue; | |
2383 | if (name[prefix_len] != '.' | |
2384 | && (suffix_len == -2 | |
551b43fd | 2385 | || (rela && spec[i].type == SHT_REL))) |
7dcb9820 AM |
2386 | continue; |
2387 | } | |
2388 | } | |
2389 | else | |
2390 | { | |
2391 | if (len < prefix_len + suffix_len) | |
2392 | continue; | |
2393 | if (memcmp (name + len - suffix_len, | |
551b43fd | 2394 | spec[i].prefix + prefix_len, |
7dcb9820 AM |
2395 | suffix_len) != 0) |
2396 | continue; | |
2397 | } | |
551b43fd | 2398 | return &spec[i]; |
7dcb9820 | 2399 | } |
2f89ff8d L |
2400 | |
2401 | return NULL; | |
2402 | } | |
2403 | ||
7dcb9820 | 2404 | const struct bfd_elf_special_section * |
29ef7005 | 2405 | _bfd_elf_get_sec_type_attr (bfd *abfd, asection *sec) |
2f89ff8d | 2406 | { |
551b43fd AM |
2407 | int i; |
2408 | const struct bfd_elf_special_section *spec; | |
29ef7005 | 2409 | const struct elf_backend_data *bed; |
2f89ff8d L |
2410 | |
2411 | /* See if this is one of the special sections. */ | |
551b43fd AM |
2412 | if (sec->name == NULL) |
2413 | return NULL; | |
2f89ff8d | 2414 | |
29ef7005 L |
2415 | bed = get_elf_backend_data (abfd); |
2416 | spec = bed->special_sections; | |
2417 | if (spec) | |
2418 | { | |
2419 | spec = _bfd_elf_get_special_section (sec->name, | |
2420 | bed->special_sections, | |
2421 | sec->use_rela_p); | |
2422 | if (spec != NULL) | |
2423 | return spec; | |
2424 | } | |
2425 | ||
551b43fd AM |
2426 | if (sec->name[0] != '.') |
2427 | return NULL; | |
2f89ff8d | 2428 | |
551b43fd | 2429 | i = sec->name[1] - 'b'; |
1b315056 | 2430 | if (i < 0 || i > 'z' - 'b') |
551b43fd AM |
2431 | return NULL; |
2432 | ||
2433 | spec = special_sections[i]; | |
2f89ff8d | 2434 | |
551b43fd AM |
2435 | if (spec == NULL) |
2436 | return NULL; | |
2437 | ||
2438 | return _bfd_elf_get_special_section (sec->name, spec, sec->use_rela_p); | |
2f89ff8d L |
2439 | } |
2440 | ||
b34976b6 | 2441 | bfd_boolean |
217aa764 | 2442 | _bfd_elf_new_section_hook (bfd *abfd, asection *sec) |
252b5132 RH |
2443 | { |
2444 | struct bfd_elf_section_data *sdata; | |
551b43fd | 2445 | const struct elf_backend_data *bed; |
7dcb9820 | 2446 | const struct bfd_elf_special_section *ssect; |
252b5132 | 2447 | |
f0abc2a1 AM |
2448 | sdata = (struct bfd_elf_section_data *) sec->used_by_bfd; |
2449 | if (sdata == NULL) | |
2450 | { | |
a50b1753 NC |
2451 | sdata = (struct bfd_elf_section_data *) bfd_zalloc (abfd, |
2452 | sizeof (*sdata)); | |
f0abc2a1 AM |
2453 | if (sdata == NULL) |
2454 | return FALSE; | |
217aa764 | 2455 | sec->used_by_bfd = sdata; |
f0abc2a1 | 2456 | } |
bf572ba0 | 2457 | |
551b43fd AM |
2458 | /* Indicate whether or not this section should use RELA relocations. */ |
2459 | bed = get_elf_backend_data (abfd); | |
2460 | sec->use_rela_p = bed->default_use_rela_p; | |
2461 | ||
e843e0f8 L |
2462 | /* When we read a file, we don't need to set ELF section type and |
2463 | flags. They will be overridden in _bfd_elf_make_section_from_shdr | |
2464 | anyway. We will set ELF section type and flags for all linker | |
2465 | created sections. If user specifies BFD section flags, we will | |
2466 | set ELF section type and flags based on BFD section flags in | |
02ecc8e9 L |
2467 | elf_fake_sections. Special handling for .init_array/.fini_array |
2468 | output sections since they may contain .ctors/.dtors input | |
2469 | sections. We don't want _bfd_elf_init_private_section_data to | |
2470 | copy ELF section type from .ctors/.dtors input sections. */ | |
2471 | if (abfd->direction != read_direction | |
3496cb2a | 2472 | || (sec->flags & SEC_LINKER_CREATED) != 0) |
2f89ff8d | 2473 | { |
551b43fd | 2474 | ssect = (*bed->get_sec_type_attr) (abfd, sec); |
02ecc8e9 L |
2475 | if (ssect != NULL |
2476 | && (!sec->flags | |
2477 | || (sec->flags & SEC_LINKER_CREATED) != 0 | |
2478 | || ssect->type == SHT_INIT_ARRAY | |
2479 | || ssect->type == SHT_FINI_ARRAY)) | |
a31501e9 L |
2480 | { |
2481 | elf_section_type (sec) = ssect->type; | |
2482 | elf_section_flags (sec) = ssect->attr; | |
2483 | } | |
2f89ff8d L |
2484 | } |
2485 | ||
f592407e | 2486 | return _bfd_generic_new_section_hook (abfd, sec); |
252b5132 RH |
2487 | } |
2488 | ||
2489 | /* Create a new bfd section from an ELF program header. | |
2490 | ||
2491 | Since program segments have no names, we generate a synthetic name | |
2492 | of the form segment<NUM>, where NUM is generally the index in the | |
2493 | program header table. For segments that are split (see below) we | |
2494 | generate the names segment<NUM>a and segment<NUM>b. | |
2495 | ||
2496 | Note that some program segments may have a file size that is different than | |
2497 | (less than) the memory size. All this means is that at execution the | |
2498 | system must allocate the amount of memory specified by the memory size, | |
2499 | but only initialize it with the first "file size" bytes read from the | |
2500 | file. This would occur for example, with program segments consisting | |
2501 | of combined data+bss. | |
2502 | ||
2503 | To handle the above situation, this routine generates TWO bfd sections | |
2504 | for the single program segment. The first has the length specified by | |
2505 | the file size of the segment, and the second has the length specified | |
2506 | by the difference between the two sizes. In effect, the segment is split | |
d5191d0c | 2507 | into its initialized and uninitialized parts. |
252b5132 RH |
2508 | |
2509 | */ | |
2510 | ||
b34976b6 | 2511 | bfd_boolean |
217aa764 AM |
2512 | _bfd_elf_make_section_from_phdr (bfd *abfd, |
2513 | Elf_Internal_Phdr *hdr, | |
91d6fa6a | 2514 | int hdr_index, |
a50b1753 | 2515 | const char *type_name) |
252b5132 RH |
2516 | { |
2517 | asection *newsect; | |
2518 | char *name; | |
2519 | char namebuf[64]; | |
d4c88bbb | 2520 | size_t len; |
252b5132 RH |
2521 | int split; |
2522 | ||
2523 | split = ((hdr->p_memsz > 0) | |
2524 | && (hdr->p_filesz > 0) | |
2525 | && (hdr->p_memsz > hdr->p_filesz)); | |
d5191d0c AM |
2526 | |
2527 | if (hdr->p_filesz > 0) | |
252b5132 | 2528 | { |
91d6fa6a | 2529 | sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "a" : ""); |
d5191d0c | 2530 | len = strlen (namebuf) + 1; |
a50b1753 | 2531 | name = (char *) bfd_alloc (abfd, len); |
d5191d0c AM |
2532 | if (!name) |
2533 | return FALSE; | |
2534 | memcpy (name, namebuf, len); | |
2535 | newsect = bfd_make_section (abfd, name); | |
2536 | if (newsect == NULL) | |
2537 | return FALSE; | |
2538 | newsect->vma = hdr->p_vaddr; | |
2539 | newsect->lma = hdr->p_paddr; | |
2540 | newsect->size = hdr->p_filesz; | |
2541 | newsect->filepos = hdr->p_offset; | |
2542 | newsect->flags |= SEC_HAS_CONTENTS; | |
2543 | newsect->alignment_power = bfd_log2 (hdr->p_align); | |
2544 | if (hdr->p_type == PT_LOAD) | |
252b5132 | 2545 | { |
d5191d0c AM |
2546 | newsect->flags |= SEC_ALLOC; |
2547 | newsect->flags |= SEC_LOAD; | |
2548 | if (hdr->p_flags & PF_X) | |
2549 | { | |
2550 | /* FIXME: all we known is that it has execute PERMISSION, | |
2551 | may be data. */ | |
2552 | newsect->flags |= SEC_CODE; | |
2553 | } | |
2554 | } | |
2555 | if (!(hdr->p_flags & PF_W)) | |
2556 | { | |
2557 | newsect->flags |= SEC_READONLY; | |
252b5132 | 2558 | } |
252b5132 RH |
2559 | } |
2560 | ||
d5191d0c | 2561 | if (hdr->p_memsz > hdr->p_filesz) |
252b5132 | 2562 | { |
d5191d0c AM |
2563 | bfd_vma align; |
2564 | ||
91d6fa6a | 2565 | sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "b" : ""); |
d4c88bbb | 2566 | len = strlen (namebuf) + 1; |
a50b1753 | 2567 | name = (char *) bfd_alloc (abfd, len); |
252b5132 | 2568 | if (!name) |
b34976b6 | 2569 | return FALSE; |
d4c88bbb | 2570 | memcpy (name, namebuf, len); |
252b5132 RH |
2571 | newsect = bfd_make_section (abfd, name); |
2572 | if (newsect == NULL) | |
b34976b6 | 2573 | return FALSE; |
252b5132 RH |
2574 | newsect->vma = hdr->p_vaddr + hdr->p_filesz; |
2575 | newsect->lma = hdr->p_paddr + hdr->p_filesz; | |
eea6121a | 2576 | newsect->size = hdr->p_memsz - hdr->p_filesz; |
d5191d0c AM |
2577 | newsect->filepos = hdr->p_offset + hdr->p_filesz; |
2578 | align = newsect->vma & -newsect->vma; | |
2579 | if (align == 0 || align > hdr->p_align) | |
2580 | align = hdr->p_align; | |
2581 | newsect->alignment_power = bfd_log2 (align); | |
252b5132 RH |
2582 | if (hdr->p_type == PT_LOAD) |
2583 | { | |
d5191d0c AM |
2584 | /* Hack for gdb. Segments that have not been modified do |
2585 | not have their contents written to a core file, on the | |
2586 | assumption that a debugger can find the contents in the | |
2587 | executable. We flag this case by setting the fake | |
2588 | section size to zero. Note that "real" bss sections will | |
2589 | always have their contents dumped to the core file. */ | |
2590 | if (bfd_get_format (abfd) == bfd_core) | |
2591 | newsect->size = 0; | |
252b5132 RH |
2592 | newsect->flags |= SEC_ALLOC; |
2593 | if (hdr->p_flags & PF_X) | |
2594 | newsect->flags |= SEC_CODE; | |
2595 | } | |
2596 | if (!(hdr->p_flags & PF_W)) | |
2597 | newsect->flags |= SEC_READONLY; | |
2598 | } | |
2599 | ||
b34976b6 | 2600 | return TRUE; |
252b5132 RH |
2601 | } |
2602 | ||
b34976b6 | 2603 | bfd_boolean |
91d6fa6a | 2604 | bfd_section_from_phdr (bfd *abfd, Elf_Internal_Phdr *hdr, int hdr_index) |
20cfcaae | 2605 | { |
9c5bfbb7 | 2606 | const struct elf_backend_data *bed; |
20cfcaae NC |
2607 | |
2608 | switch (hdr->p_type) | |
2609 | { | |
2610 | case PT_NULL: | |
91d6fa6a | 2611 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "null"); |
20cfcaae NC |
2612 | |
2613 | case PT_LOAD: | |
91d6fa6a | 2614 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "load"); |
20cfcaae NC |
2615 | |
2616 | case PT_DYNAMIC: | |
91d6fa6a | 2617 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "dynamic"); |
20cfcaae NC |
2618 | |
2619 | case PT_INTERP: | |
91d6fa6a | 2620 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "interp"); |
20cfcaae NC |
2621 | |
2622 | case PT_NOTE: | |
91d6fa6a | 2623 | if (! _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "note")) |
b34976b6 | 2624 | return FALSE; |
718175fa | 2625 | if (! elf_read_notes (abfd, hdr->p_offset, hdr->p_filesz)) |
b34976b6 AM |
2626 | return FALSE; |
2627 | return TRUE; | |
20cfcaae NC |
2628 | |
2629 | case PT_SHLIB: | |
91d6fa6a | 2630 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "shlib"); |
20cfcaae NC |
2631 | |
2632 | case PT_PHDR: | |
91d6fa6a | 2633 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "phdr"); |
20cfcaae | 2634 | |
811072d8 | 2635 | case PT_GNU_EH_FRAME: |
91d6fa6a | 2636 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, |
811072d8 RM |
2637 | "eh_frame_hdr"); |
2638 | ||
2b05f1b7 | 2639 | case PT_GNU_STACK: |
91d6fa6a | 2640 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "stack"); |
9ee5e499 | 2641 | |
8c37241b | 2642 | case PT_GNU_RELRO: |
91d6fa6a | 2643 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "relro"); |
8c37241b | 2644 | |
20cfcaae | 2645 | default: |
8c1acd09 | 2646 | /* Check for any processor-specific program segment types. */ |
20cfcaae | 2647 | bed = get_elf_backend_data (abfd); |
91d6fa6a | 2648 | return bed->elf_backend_section_from_phdr (abfd, hdr, hdr_index, "proc"); |
20cfcaae NC |
2649 | } |
2650 | } | |
2651 | ||
d4730f92 BS |
2652 | /* Return the REL_HDR for SEC, assuming there is only a single one, either |
2653 | REL or RELA. */ | |
2654 | ||
2655 | Elf_Internal_Shdr * | |
2656 | _bfd_elf_single_rel_hdr (asection *sec) | |
2657 | { | |
2658 | if (elf_section_data (sec)->rel.hdr) | |
2659 | { | |
2660 | BFD_ASSERT (elf_section_data (sec)->rela.hdr == NULL); | |
2661 | return elf_section_data (sec)->rel.hdr; | |
2662 | } | |
2663 | else | |
2664 | return elf_section_data (sec)->rela.hdr; | |
2665 | } | |
2666 | ||
2667 | /* Allocate and initialize a section-header for a new reloc section, | |
2668 | containing relocations against ASECT. It is stored in RELDATA. If | |
2669 | USE_RELA_P is TRUE, we use RELA relocations; otherwise, we use REL | |
2670 | relocations. */ | |
23bc299b | 2671 | |
5d13b3b3 | 2672 | static bfd_boolean |
217aa764 | 2673 | _bfd_elf_init_reloc_shdr (bfd *abfd, |
d4730f92 | 2674 | struct bfd_elf_section_reloc_data *reldata, |
217aa764 AM |
2675 | asection *asect, |
2676 | bfd_boolean use_rela_p) | |
23bc299b | 2677 | { |
d4730f92 | 2678 | Elf_Internal_Shdr *rel_hdr; |
23bc299b | 2679 | char *name; |
9c5bfbb7 | 2680 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
d4730f92 BS |
2681 | bfd_size_type amt; |
2682 | ||
2683 | amt = sizeof (Elf_Internal_Shdr); | |
2684 | BFD_ASSERT (reldata->hdr == NULL); | |
2685 | rel_hdr = bfd_zalloc (abfd, amt); | |
2686 | reldata->hdr = rel_hdr; | |
23bc299b | 2687 | |
d324f6d6 | 2688 | amt = sizeof ".rela" + strlen (asect->name); |
a50b1753 | 2689 | name = (char *) bfd_alloc (abfd, amt); |
23bc299b | 2690 | if (name == NULL) |
b34976b6 | 2691 | return FALSE; |
23bc299b MM |
2692 | sprintf (name, "%s%s", use_rela_p ? ".rela" : ".rel", asect->name); |
2693 | rel_hdr->sh_name = | |
2b0f7ef9 | 2694 | (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), name, |
b34976b6 | 2695 | FALSE); |
23bc299b | 2696 | if (rel_hdr->sh_name == (unsigned int) -1) |
b34976b6 | 2697 | return FALSE; |
23bc299b MM |
2698 | rel_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL; |
2699 | rel_hdr->sh_entsize = (use_rela_p | |
2700 | ? bed->s->sizeof_rela | |
2701 | : bed->s->sizeof_rel); | |
72de5009 | 2702 | rel_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align; |
23bc299b MM |
2703 | rel_hdr->sh_flags = 0; |
2704 | rel_hdr->sh_addr = 0; | |
2705 | rel_hdr->sh_size = 0; | |
2706 | rel_hdr->sh_offset = 0; | |
2707 | ||
b34976b6 | 2708 | return TRUE; |
23bc299b MM |
2709 | } |
2710 | ||
94be91de JB |
2711 | /* Return the default section type based on the passed in section flags. */ |
2712 | ||
2713 | int | |
2714 | bfd_elf_get_default_section_type (flagword flags) | |
2715 | { | |
2716 | if ((flags & SEC_ALLOC) != 0 | |
2e76e85a | 2717 | && (flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0) |
94be91de JB |
2718 | return SHT_NOBITS; |
2719 | return SHT_PROGBITS; | |
2720 | } | |
2721 | ||
d4730f92 BS |
2722 | struct fake_section_arg |
2723 | { | |
2724 | struct bfd_link_info *link_info; | |
2725 | bfd_boolean failed; | |
2726 | }; | |
2727 | ||
252b5132 RH |
2728 | /* Set up an ELF internal section header for a section. */ |
2729 | ||
252b5132 | 2730 | static void |
d4730f92 | 2731 | elf_fake_sections (bfd *abfd, asection *asect, void *fsarg) |
252b5132 | 2732 | { |
d4730f92 | 2733 | struct fake_section_arg *arg = (struct fake_section_arg *)fsarg; |
9c5bfbb7 | 2734 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
d4730f92 | 2735 | struct bfd_elf_section_data *esd = elf_section_data (asect); |
252b5132 | 2736 | Elf_Internal_Shdr *this_hdr; |
0414f35b | 2737 | unsigned int sh_type; |
252b5132 | 2738 | |
d4730f92 | 2739 | if (arg->failed) |
252b5132 RH |
2740 | { |
2741 | /* We already failed; just get out of the bfd_map_over_sections | |
08a40648 | 2742 | loop. */ |
252b5132 RH |
2743 | return; |
2744 | } | |
2745 | ||
d4730f92 | 2746 | this_hdr = &esd->this_hdr; |
252b5132 | 2747 | |
e57b5356 AM |
2748 | this_hdr->sh_name = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), |
2749 | asect->name, FALSE); | |
2750 | if (this_hdr->sh_name == (unsigned int) -1) | |
252b5132 | 2751 | { |
d4730f92 | 2752 | arg->failed = TRUE; |
252b5132 RH |
2753 | return; |
2754 | } | |
2755 | ||
a4d8e49b | 2756 | /* Don't clear sh_flags. Assembler may set additional bits. */ |
252b5132 RH |
2757 | |
2758 | if ((asect->flags & SEC_ALLOC) != 0 | |
2759 | || asect->user_set_vma) | |
2760 | this_hdr->sh_addr = asect->vma; | |
2761 | else | |
2762 | this_hdr->sh_addr = 0; | |
2763 | ||
2764 | this_hdr->sh_offset = 0; | |
eea6121a | 2765 | this_hdr->sh_size = asect->size; |
252b5132 | 2766 | this_hdr->sh_link = 0; |
c86934ce NC |
2767 | /* PR 17512: file: 0eb809fe, 8b0535ee. */ |
2768 | if (asect->alignment_power >= (sizeof (bfd_vma) * 8) - 1) | |
2769 | { | |
2770 | (*_bfd_error_handler) | |
2771 | (_("%B: error: Alignment power %d of section `%A' is too big"), | |
2772 | abfd, asect, asect->alignment_power); | |
2773 | arg->failed = TRUE; | |
2774 | return; | |
2775 | } | |
72de5009 | 2776 | this_hdr->sh_addralign = (bfd_vma) 1 << asect->alignment_power; |
252b5132 RH |
2777 | /* The sh_entsize and sh_info fields may have been set already by |
2778 | copy_private_section_data. */ | |
2779 | ||
2780 | this_hdr->bfd_section = asect; | |
2781 | this_hdr->contents = NULL; | |
2782 | ||
3cddba1e L |
2783 | /* If the section type is unspecified, we set it based on |
2784 | asect->flags. */ | |
98ece1b3 AM |
2785 | if ((asect->flags & SEC_GROUP) != 0) |
2786 | sh_type = SHT_GROUP; | |
98ece1b3 | 2787 | else |
94be91de | 2788 | sh_type = bfd_elf_get_default_section_type (asect->flags); |
98ece1b3 | 2789 | |
3cddba1e | 2790 | if (this_hdr->sh_type == SHT_NULL) |
98ece1b3 AM |
2791 | this_hdr->sh_type = sh_type; |
2792 | else if (this_hdr->sh_type == SHT_NOBITS | |
2793 | && sh_type == SHT_PROGBITS | |
2794 | && (asect->flags & SEC_ALLOC) != 0) | |
3cddba1e | 2795 | { |
98ece1b3 AM |
2796 | /* Warn if we are changing a NOBITS section to PROGBITS, but |
2797 | allow the link to proceed. This can happen when users link | |
2798 | non-bss input sections to bss output sections, or emit data | |
2799 | to a bss output section via a linker script. */ | |
2800 | (*_bfd_error_handler) | |
58f0869b | 2801 | (_("warning: section `%A' type changed to PROGBITS"), asect); |
98ece1b3 | 2802 | this_hdr->sh_type = sh_type; |
3cddba1e L |
2803 | } |
2804 | ||
2f89ff8d | 2805 | switch (this_hdr->sh_type) |
252b5132 | 2806 | { |
2f89ff8d | 2807 | default: |
2f89ff8d L |
2808 | break; |
2809 | ||
2810 | case SHT_STRTAB: | |
2811 | case SHT_INIT_ARRAY: | |
2812 | case SHT_FINI_ARRAY: | |
2813 | case SHT_PREINIT_ARRAY: | |
2814 | case SHT_NOTE: | |
2815 | case SHT_NOBITS: | |
2816 | case SHT_PROGBITS: | |
2817 | break; | |
2818 | ||
2819 | case SHT_HASH: | |
c7ac6ff8 | 2820 | this_hdr->sh_entsize = bed->s->sizeof_hash_entry; |
2f89ff8d | 2821 | break; |
5de3bf90 | 2822 | |
2f89ff8d | 2823 | case SHT_DYNSYM: |
252b5132 | 2824 | this_hdr->sh_entsize = bed->s->sizeof_sym; |
2f89ff8d L |
2825 | break; |
2826 | ||
2827 | case SHT_DYNAMIC: | |
252b5132 | 2828 | this_hdr->sh_entsize = bed->s->sizeof_dyn; |
2f89ff8d L |
2829 | break; |
2830 | ||
2831 | case SHT_RELA: | |
2832 | if (get_elf_backend_data (abfd)->may_use_rela_p) | |
2833 | this_hdr->sh_entsize = bed->s->sizeof_rela; | |
2834 | break; | |
2835 | ||
2836 | case SHT_REL: | |
2837 | if (get_elf_backend_data (abfd)->may_use_rel_p) | |
2838 | this_hdr->sh_entsize = bed->s->sizeof_rel; | |
2839 | break; | |
2840 | ||
2841 | case SHT_GNU_versym: | |
252b5132 | 2842 | this_hdr->sh_entsize = sizeof (Elf_External_Versym); |
2f89ff8d L |
2843 | break; |
2844 | ||
2845 | case SHT_GNU_verdef: | |
252b5132 RH |
2846 | this_hdr->sh_entsize = 0; |
2847 | /* objcopy or strip will copy over sh_info, but may not set | |
08a40648 AM |
2848 | cverdefs. The linker will set cverdefs, but sh_info will be |
2849 | zero. */ | |
252b5132 RH |
2850 | if (this_hdr->sh_info == 0) |
2851 | this_hdr->sh_info = elf_tdata (abfd)->cverdefs; | |
2852 | else | |
2853 | BFD_ASSERT (elf_tdata (abfd)->cverdefs == 0 | |
2854 | || this_hdr->sh_info == elf_tdata (abfd)->cverdefs); | |
2f89ff8d L |
2855 | break; |
2856 | ||
2857 | case SHT_GNU_verneed: | |
252b5132 RH |
2858 | this_hdr->sh_entsize = 0; |
2859 | /* objcopy or strip will copy over sh_info, but may not set | |
08a40648 AM |
2860 | cverrefs. The linker will set cverrefs, but sh_info will be |
2861 | zero. */ | |
252b5132 RH |
2862 | if (this_hdr->sh_info == 0) |
2863 | this_hdr->sh_info = elf_tdata (abfd)->cverrefs; | |
2864 | else | |
2865 | BFD_ASSERT (elf_tdata (abfd)->cverrefs == 0 | |
2866 | || this_hdr->sh_info == elf_tdata (abfd)->cverrefs); | |
2f89ff8d L |
2867 | break; |
2868 | ||
2869 | case SHT_GROUP: | |
1783205a | 2870 | this_hdr->sh_entsize = GRP_ENTRY_SIZE; |
2f89ff8d | 2871 | break; |
fdc90cb4 JJ |
2872 | |
2873 | case SHT_GNU_HASH: | |
2874 | this_hdr->sh_entsize = bed->s->arch_size == 64 ? 0 : 4; | |
2875 | break; | |
dbb410c3 | 2876 | } |
252b5132 RH |
2877 | |
2878 | if ((asect->flags & SEC_ALLOC) != 0) | |
2879 | this_hdr->sh_flags |= SHF_ALLOC; | |
2880 | if ((asect->flags & SEC_READONLY) == 0) | |
2881 | this_hdr->sh_flags |= SHF_WRITE; | |
2882 | if ((asect->flags & SEC_CODE) != 0) | |
2883 | this_hdr->sh_flags |= SHF_EXECINSTR; | |
f5fa8ca2 JJ |
2884 | if ((asect->flags & SEC_MERGE) != 0) |
2885 | { | |
2886 | this_hdr->sh_flags |= SHF_MERGE; | |
2887 | this_hdr->sh_entsize = asect->entsize; | |
2888 | if ((asect->flags & SEC_STRINGS) != 0) | |
2889 | this_hdr->sh_flags |= SHF_STRINGS; | |
2890 | } | |
1126897b | 2891 | if ((asect->flags & SEC_GROUP) == 0 && elf_group_name (asect) != NULL) |
dbb410c3 | 2892 | this_hdr->sh_flags |= SHF_GROUP; |
13ae64f3 | 2893 | if ((asect->flags & SEC_THREAD_LOCAL) != 0) |
704afa60 JJ |
2894 | { |
2895 | this_hdr->sh_flags |= SHF_TLS; | |
3a800eb9 AM |
2896 | if (asect->size == 0 |
2897 | && (asect->flags & SEC_HAS_CONTENTS) == 0) | |
704afa60 | 2898 | { |
3a800eb9 | 2899 | struct bfd_link_order *o = asect->map_tail.link_order; |
b34976b6 | 2900 | |
704afa60 | 2901 | this_hdr->sh_size = 0; |
3a800eb9 AM |
2902 | if (o != NULL) |
2903 | { | |
704afa60 | 2904 | this_hdr->sh_size = o->offset + o->size; |
3a800eb9 AM |
2905 | if (this_hdr->sh_size != 0) |
2906 | this_hdr->sh_type = SHT_NOBITS; | |
2907 | } | |
704afa60 JJ |
2908 | } |
2909 | } | |
18ae9cc1 L |
2910 | if ((asect->flags & (SEC_GROUP | SEC_EXCLUDE)) == SEC_EXCLUDE) |
2911 | this_hdr->sh_flags |= SHF_EXCLUDE; | |
252b5132 | 2912 | |
d4730f92 BS |
2913 | /* If the section has relocs, set up a section header for the |
2914 | SHT_REL[A] section. If two relocation sections are required for | |
2915 | this section, it is up to the processor-specific back-end to | |
2916 | create the other. */ | |
2917 | if ((asect->flags & SEC_RELOC) != 0) | |
2918 | { | |
2919 | /* When doing a relocatable link, create both REL and RELA sections if | |
2920 | needed. */ | |
2921 | if (arg->link_info | |
2922 | /* Do the normal setup if we wouldn't create any sections here. */ | |
2923 | && esd->rel.count + esd->rela.count > 0 | |
2924 | && (arg->link_info->relocatable || arg->link_info->emitrelocations)) | |
2925 | { | |
2926 | if (esd->rel.count && esd->rel.hdr == NULL | |
2927 | && !_bfd_elf_init_reloc_shdr (abfd, &esd->rel, asect, FALSE)) | |
2928 | { | |
2929 | arg->failed = TRUE; | |
2930 | return; | |
2931 | } | |
2932 | if (esd->rela.count && esd->rela.hdr == NULL | |
2933 | && !_bfd_elf_init_reloc_shdr (abfd, &esd->rela, asect, TRUE)) | |
2934 | { | |
2935 | arg->failed = TRUE; | |
2936 | return; | |
2937 | } | |
2938 | } | |
2939 | else if (!_bfd_elf_init_reloc_shdr (abfd, | |
2940 | (asect->use_rela_p | |
2941 | ? &esd->rela : &esd->rel), | |
2942 | asect, | |
2943 | asect->use_rela_p)) | |
2944 | arg->failed = TRUE; | |
2945 | } | |
2946 | ||
252b5132 | 2947 | /* Check for processor-specific section types. */ |
0414f35b | 2948 | sh_type = this_hdr->sh_type; |
e1fddb6b AO |
2949 | if (bed->elf_backend_fake_sections |
2950 | && !(*bed->elf_backend_fake_sections) (abfd, this_hdr, asect)) | |
d4730f92 | 2951 | arg->failed = TRUE; |
252b5132 | 2952 | |
42bb2e33 | 2953 | if (sh_type == SHT_NOBITS && asect->size != 0) |
0414f35b AM |
2954 | { |
2955 | /* Don't change the header type from NOBITS if we are being | |
42bb2e33 | 2956 | called for objcopy --only-keep-debug. */ |
0414f35b AM |
2957 | this_hdr->sh_type = sh_type; |
2958 | } | |
252b5132 RH |
2959 | } |
2960 | ||
bcacc0f5 AM |
2961 | /* Fill in the contents of a SHT_GROUP section. Called from |
2962 | _bfd_elf_compute_section_file_positions for gas, objcopy, and | |
2963 | when ELF targets use the generic linker, ld. Called for ld -r | |
2964 | from bfd_elf_final_link. */ | |
dbb410c3 | 2965 | |
1126897b | 2966 | void |
217aa764 | 2967 | bfd_elf_set_group_contents (bfd *abfd, asection *sec, void *failedptrarg) |
dbb410c3 | 2968 | { |
a50b1753 | 2969 | bfd_boolean *failedptr = (bfd_boolean *) failedptrarg; |
9dce4196 | 2970 | asection *elt, *first; |
dbb410c3 | 2971 | unsigned char *loc; |
b34976b6 | 2972 | bfd_boolean gas; |
dbb410c3 | 2973 | |
7e4111ad L |
2974 | /* Ignore linker created group section. See elfNN_ia64_object_p in |
2975 | elfxx-ia64.c. */ | |
2976 | if (((sec->flags & (SEC_GROUP | SEC_LINKER_CREATED)) != SEC_GROUP) | |
dbb410c3 AM |
2977 | || *failedptr) |
2978 | return; | |
2979 | ||
bcacc0f5 AM |
2980 | if (elf_section_data (sec)->this_hdr.sh_info == 0) |
2981 | { | |
2982 | unsigned long symindx = 0; | |
2983 | ||
2984 | /* elf_group_id will have been set up by objcopy and the | |
2985 | generic linker. */ | |
2986 | if (elf_group_id (sec) != NULL) | |
2987 | symindx = elf_group_id (sec)->udata.i; | |
1126897b | 2988 | |
bcacc0f5 AM |
2989 | if (symindx == 0) |
2990 | { | |
2991 | /* If called from the assembler, swap_out_syms will have set up | |
2992 | elf_section_syms. */ | |
2993 | BFD_ASSERT (elf_section_syms (abfd) != NULL); | |
2994 | symindx = elf_section_syms (abfd)[sec->index]->udata.i; | |
2995 | } | |
2996 | elf_section_data (sec)->this_hdr.sh_info = symindx; | |
2997 | } | |
2998 | else if (elf_section_data (sec)->this_hdr.sh_info == (unsigned int) -2) | |
1126897b | 2999 | { |
bcacc0f5 AM |
3000 | /* The ELF backend linker sets sh_info to -2 when the group |
3001 | signature symbol is global, and thus the index can't be | |
3002 | set until all local symbols are output. */ | |
3003 | asection *igroup = elf_sec_group (elf_next_in_group (sec)); | |
3004 | struct bfd_elf_section_data *sec_data = elf_section_data (igroup); | |
3005 | unsigned long symndx = sec_data->this_hdr.sh_info; | |
3006 | unsigned long extsymoff = 0; | |
3007 | struct elf_link_hash_entry *h; | |
3008 | ||
3009 | if (!elf_bad_symtab (igroup->owner)) | |
3010 | { | |
3011 | Elf_Internal_Shdr *symtab_hdr; | |
3012 | ||
3013 | symtab_hdr = &elf_tdata (igroup->owner)->symtab_hdr; | |
3014 | extsymoff = symtab_hdr->sh_info; | |
3015 | } | |
3016 | h = elf_sym_hashes (igroup->owner)[symndx - extsymoff]; | |
3017 | while (h->root.type == bfd_link_hash_indirect | |
3018 | || h->root.type == bfd_link_hash_warning) | |
3019 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
3020 | ||
3021 | elf_section_data (sec)->this_hdr.sh_info = h->indx; | |
1126897b | 3022 | } |
dbb410c3 | 3023 | |
1126897b | 3024 | /* The contents won't be allocated for "ld -r" or objcopy. */ |
b34976b6 | 3025 | gas = TRUE; |
dbb410c3 AM |
3026 | if (sec->contents == NULL) |
3027 | { | |
b34976b6 | 3028 | gas = FALSE; |
a50b1753 | 3029 | sec->contents = (unsigned char *) bfd_alloc (abfd, sec->size); |
9dce4196 AM |
3030 | |
3031 | /* Arrange for the section to be written out. */ | |
3032 | elf_section_data (sec)->this_hdr.contents = sec->contents; | |
dbb410c3 AM |
3033 | if (sec->contents == NULL) |
3034 | { | |
b34976b6 | 3035 | *failedptr = TRUE; |
dbb410c3 AM |
3036 | return; |
3037 | } | |
3038 | } | |
3039 | ||
eea6121a | 3040 | loc = sec->contents + sec->size; |
dbb410c3 | 3041 | |
9dce4196 AM |
3042 | /* Get the pointer to the first section in the group that gas |
3043 | squirreled away here. objcopy arranges for this to be set to the | |
3044 | start of the input section group. */ | |
3045 | first = elt = elf_next_in_group (sec); | |
dbb410c3 AM |
3046 | |
3047 | /* First element is a flag word. Rest of section is elf section | |
3048 | indices for all the sections of the group. Write them backwards | |
3049 | just to keep the group in the same order as given in .section | |
3050 | directives, not that it matters. */ | |
3051 | while (elt != NULL) | |
3052 | { | |
9dce4196 | 3053 | asection *s; |
9dce4196 | 3054 | |
9dce4196 | 3055 | s = elt; |
415f38a6 AM |
3056 | if (!gas) |
3057 | s = s->output_section; | |
3058 | if (s != NULL | |
3059 | && !bfd_is_abs_section (s)) | |
01e1a5bc | 3060 | { |
415f38a6 AM |
3061 | unsigned int idx = elf_section_data (s)->this_idx; |
3062 | ||
01e1a5bc | 3063 | loc -= 4; |
01e1a5bc NC |
3064 | H_PUT_32 (abfd, idx, loc); |
3065 | } | |
945906ff | 3066 | elt = elf_next_in_group (elt); |
9dce4196 AM |
3067 | if (elt == first) |
3068 | break; | |
dbb410c3 AM |
3069 | } |
3070 | ||
3d7f7666 | 3071 | if ((loc -= 4) != sec->contents) |
9dce4196 | 3072 | abort (); |
dbb410c3 | 3073 | |
9dce4196 | 3074 | H_PUT_32 (abfd, sec->flags & SEC_LINK_ONCE ? GRP_COMDAT : 0, loc); |
dbb410c3 AM |
3075 | } |
3076 | ||
252b5132 RH |
3077 | /* Assign all ELF section numbers. The dummy first section is handled here |
3078 | too. The link/info pointers for the standard section types are filled | |
3079 | in here too, while we're at it. */ | |
3080 | ||
b34976b6 | 3081 | static bfd_boolean |
da9f89d4 | 3082 | assign_section_numbers (bfd *abfd, struct bfd_link_info *link_info) |
252b5132 RH |
3083 | { |
3084 | struct elf_obj_tdata *t = elf_tdata (abfd); | |
3085 | asection *sec; | |
2b0f7ef9 | 3086 | unsigned int section_number, secn; |
252b5132 | 3087 | Elf_Internal_Shdr **i_shdrp; |
47cc2cf5 | 3088 | struct bfd_elf_section_data *d; |
3516e984 | 3089 | bfd_boolean need_symtab; |
252b5132 RH |
3090 | |
3091 | section_number = 1; | |
3092 | ||
2b0f7ef9 JJ |
3093 | _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd)); |
3094 | ||
da9f89d4 L |
3095 | /* SHT_GROUP sections are in relocatable files only. */ |
3096 | if (link_info == NULL || link_info->relocatable) | |
252b5132 | 3097 | { |
da9f89d4 | 3098 | /* Put SHT_GROUP sections first. */ |
04dd1667 | 3099 | for (sec = abfd->sections; sec != NULL; sec = sec->next) |
47cc2cf5 | 3100 | { |
5daa8fe7 | 3101 | d = elf_section_data (sec); |
da9f89d4 L |
3102 | |
3103 | if (d->this_hdr.sh_type == SHT_GROUP) | |
08a40648 | 3104 | { |
5daa8fe7 | 3105 | if (sec->flags & SEC_LINKER_CREATED) |
da9f89d4 L |
3106 | { |
3107 | /* Remove the linker created SHT_GROUP sections. */ | |
5daa8fe7 | 3108 | bfd_section_list_remove (abfd, sec); |
da9f89d4 | 3109 | abfd->section_count--; |
da9f89d4 | 3110 | } |
08a40648 | 3111 | else |
4fbb74a6 | 3112 | d->this_idx = section_number++; |
da9f89d4 | 3113 | } |
47cc2cf5 PB |
3114 | } |
3115 | } | |
3116 | ||
3117 | for (sec = abfd->sections; sec; sec = sec->next) | |
3118 | { | |
3119 | d = elf_section_data (sec); | |
3120 | ||
3121 | if (d->this_hdr.sh_type != SHT_GROUP) | |
4fbb74a6 | 3122 | d->this_idx = section_number++; |
2b0f7ef9 | 3123 | _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->this_hdr.sh_name); |
d4730f92 | 3124 | if (d->rel.hdr) |
2b0f7ef9 | 3125 | { |
d4730f92 BS |
3126 | d->rel.idx = section_number++; |
3127 | _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel.hdr->sh_name); | |
2b0f7ef9 | 3128 | } |
d4730f92 BS |
3129 | else |
3130 | d->rel.idx = 0; | |
23bc299b | 3131 | |
d4730f92 | 3132 | if (d->rela.hdr) |
2b0f7ef9 | 3133 | { |
d4730f92 BS |
3134 | d->rela.idx = section_number++; |
3135 | _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rela.hdr->sh_name); | |
2b0f7ef9 | 3136 | } |
23bc299b | 3137 | else |
d4730f92 | 3138 | d->rela.idx = 0; |
252b5132 RH |
3139 | } |
3140 | ||
12bd6957 | 3141 | elf_shstrtab_sec (abfd) = section_number++; |
2b0f7ef9 | 3142 | _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->shstrtab_hdr.sh_name); |
12bd6957 | 3143 | elf_elfheader (abfd)->e_shstrndx = elf_shstrtab_sec (abfd); |
252b5132 | 3144 | |
3516e984 L |
3145 | need_symtab = (bfd_get_symcount (abfd) > 0 |
3146 | || (link_info == NULL | |
3147 | && ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC)) | |
3148 | == HAS_RELOC))); | |
3149 | if (need_symtab) | |
252b5132 | 3150 | { |
12bd6957 | 3151 | elf_onesymtab (abfd) = section_number++; |
2b0f7ef9 | 3152 | _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->symtab_hdr.sh_name); |
4fbb74a6 | 3153 | if (section_number > ((SHN_LORESERVE - 2) & 0xFFFF)) |
9ad5cbcf | 3154 | { |
12bd6957 | 3155 | elf_symtab_shndx (abfd) = section_number++; |
9ad5cbcf AM |
3156 | t->symtab_shndx_hdr.sh_name |
3157 | = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), | |
b34976b6 | 3158 | ".symtab_shndx", FALSE); |
9ad5cbcf | 3159 | if (t->symtab_shndx_hdr.sh_name == (unsigned int) -1) |
b34976b6 | 3160 | return FALSE; |
9ad5cbcf | 3161 | } |
12bd6957 | 3162 | elf_strtab_sec (abfd) = section_number++; |
2b0f7ef9 | 3163 | _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->strtab_hdr.sh_name); |
252b5132 RH |
3164 | } |
3165 | ||
1c52a645 L |
3166 | if (section_number >= SHN_LORESERVE) |
3167 | { | |
3168 | _bfd_error_handler (_("%B: too many sections: %u"), | |
3169 | abfd, section_number); | |
3170 | return FALSE; | |
3171 | } | |
3172 | ||
2b0f7ef9 JJ |
3173 | _bfd_elf_strtab_finalize (elf_shstrtab (abfd)); |
3174 | t->shstrtab_hdr.sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd)); | |
9ad5cbcf AM |
3175 | |
3176 | elf_numsections (abfd) = section_number; | |
252b5132 RH |
3177 | elf_elfheader (abfd)->e_shnum = section_number; |
3178 | ||
3179 | /* Set up the list of section header pointers, in agreement with the | |
3180 | indices. */ | |
a50b1753 NC |
3181 | i_shdrp = (Elf_Internal_Shdr **) bfd_zalloc2 (abfd, section_number, |
3182 | sizeof (Elf_Internal_Shdr *)); | |
252b5132 | 3183 | if (i_shdrp == NULL) |
b34976b6 | 3184 | return FALSE; |
252b5132 | 3185 | |
a50b1753 NC |
3186 | i_shdrp[0] = (Elf_Internal_Shdr *) bfd_zalloc (abfd, |
3187 | sizeof (Elf_Internal_Shdr)); | |
252b5132 RH |
3188 | if (i_shdrp[0] == NULL) |
3189 | { | |
3190 | bfd_release (abfd, i_shdrp); | |
b34976b6 | 3191 | return FALSE; |
252b5132 | 3192 | } |
252b5132 RH |
3193 | |
3194 | elf_elfsections (abfd) = i_shdrp; | |
3195 | ||
12bd6957 | 3196 | i_shdrp[elf_shstrtab_sec (abfd)] = &t->shstrtab_hdr; |
3516e984 | 3197 | if (need_symtab) |
252b5132 | 3198 | { |
12bd6957 | 3199 | i_shdrp[elf_onesymtab (abfd)] = &t->symtab_hdr; |
4fbb74a6 | 3200 | if (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF)) |
9ad5cbcf | 3201 | { |
12bd6957 AM |
3202 | i_shdrp[elf_symtab_shndx (abfd)] = &t->symtab_shndx_hdr; |
3203 | t->symtab_shndx_hdr.sh_link = elf_onesymtab (abfd); | |
9ad5cbcf | 3204 | } |
12bd6957 AM |
3205 | i_shdrp[elf_strtab_sec (abfd)] = &t->strtab_hdr; |
3206 | t->symtab_hdr.sh_link = elf_strtab_sec (abfd); | |
252b5132 | 3207 | } |
38ce5b11 | 3208 | |
252b5132 RH |
3209 | for (sec = abfd->sections; sec; sec = sec->next) |
3210 | { | |
252b5132 RH |
3211 | asection *s; |
3212 | const char *name; | |
3213 | ||
91d6fa6a NC |
3214 | d = elf_section_data (sec); |
3215 | ||
252b5132 | 3216 | i_shdrp[d->this_idx] = &d->this_hdr; |
d4730f92 BS |
3217 | if (d->rel.idx != 0) |
3218 | i_shdrp[d->rel.idx] = d->rel.hdr; | |
3219 | if (d->rela.idx != 0) | |
3220 | i_shdrp[d->rela.idx] = d->rela.hdr; | |
252b5132 RH |
3221 | |
3222 | /* Fill in the sh_link and sh_info fields while we're at it. */ | |
3223 | ||
3224 | /* sh_link of a reloc section is the section index of the symbol | |
3225 | table. sh_info is the section index of the section to which | |
3226 | the relocation entries apply. */ | |
d4730f92 | 3227 | if (d->rel.idx != 0) |
252b5132 | 3228 | { |
12bd6957 | 3229 | d->rel.hdr->sh_link = elf_onesymtab (abfd); |
d4730f92 | 3230 | d->rel.hdr->sh_info = d->this_idx; |
9ef5d938 | 3231 | d->rel.hdr->sh_flags |= SHF_INFO_LINK; |
252b5132 | 3232 | } |
d4730f92 | 3233 | if (d->rela.idx != 0) |
23bc299b | 3234 | { |
12bd6957 | 3235 | d->rela.hdr->sh_link = elf_onesymtab (abfd); |
d4730f92 | 3236 | d->rela.hdr->sh_info = d->this_idx; |
9ef5d938 | 3237 | d->rela.hdr->sh_flags |= SHF_INFO_LINK; |
23bc299b | 3238 | } |
252b5132 | 3239 | |
38ce5b11 L |
3240 | /* We need to set up sh_link for SHF_LINK_ORDER. */ |
3241 | if ((d->this_hdr.sh_flags & SHF_LINK_ORDER) != 0) | |
3242 | { | |
3243 | s = elf_linked_to_section (sec); | |
3244 | if (s) | |
38ce5b11 | 3245 | { |
f2876037 | 3246 | /* elf_linked_to_section points to the input section. */ |
ccd2ec6a | 3247 | if (link_info != NULL) |
38ce5b11 | 3248 | { |
f2876037 | 3249 | /* Check discarded linkonce section. */ |
dbaa2011 | 3250 | if (discarded_section (s)) |
38ce5b11 | 3251 | { |
ccd2ec6a L |
3252 | asection *kept; |
3253 | (*_bfd_error_handler) | |
3254 | (_("%B: sh_link of section `%A' points to discarded section `%A' of `%B'"), | |
3255 | abfd, d->this_hdr.bfd_section, | |
3256 | s, s->owner); | |
3257 | /* Point to the kept section if it has the same | |
3258 | size as the discarded one. */ | |
c0f00686 | 3259 | kept = _bfd_elf_check_kept_section (s, link_info); |
ccd2ec6a | 3260 | if (kept == NULL) |
185d09ad | 3261 | { |
ccd2ec6a L |
3262 | bfd_set_error (bfd_error_bad_value); |
3263 | return FALSE; | |
185d09ad | 3264 | } |
ccd2ec6a | 3265 | s = kept; |
38ce5b11 | 3266 | } |
e424ecc8 | 3267 | |
ccd2ec6a L |
3268 | s = s->output_section; |
3269 | BFD_ASSERT (s != NULL); | |
38ce5b11 | 3270 | } |
f2876037 L |
3271 | else |
3272 | { | |
3273 | /* Handle objcopy. */ | |
3274 | if (s->output_section == NULL) | |
3275 | { | |
3276 | (*_bfd_error_handler) | |
3277 | (_("%B: sh_link of section `%A' points to removed section `%A' of `%B'"), | |
3278 | abfd, d->this_hdr.bfd_section, s, s->owner); | |
3279 | bfd_set_error (bfd_error_bad_value); | |
3280 | return FALSE; | |
3281 | } | |
3282 | s = s->output_section; | |
3283 | } | |
ccd2ec6a L |
3284 | d->this_hdr.sh_link = elf_section_data (s)->this_idx; |
3285 | } | |
3286 | else | |
3287 | { | |
3288 | /* PR 290: | |
3289 | The Intel C compiler generates SHT_IA_64_UNWIND with | |
3290 | SHF_LINK_ORDER. But it doesn't set the sh_link or | |
3291 | sh_info fields. Hence we could get the situation | |
08a40648 | 3292 | where s is NULL. */ |
ccd2ec6a L |
3293 | const struct elf_backend_data *bed |
3294 | = get_elf_backend_data (abfd); | |
3295 | if (bed->link_order_error_handler) | |
3296 | bed->link_order_error_handler | |
3297 | (_("%B: warning: sh_link not set for section `%A'"), | |
3298 | abfd, sec); | |
38ce5b11 L |
3299 | } |
3300 | } | |
3301 | ||
252b5132 RH |
3302 | switch (d->this_hdr.sh_type) |
3303 | { | |
3304 | case SHT_REL: | |
3305 | case SHT_RELA: | |
3306 | /* A reloc section which we are treating as a normal BFD | |
3307 | section. sh_link is the section index of the symbol | |
3308 | table. sh_info is the section index of the section to | |
3309 | which the relocation entries apply. We assume that an | |
3310 | allocated reloc section uses the dynamic symbol table. | |
3311 | FIXME: How can we be sure? */ | |
3312 | s = bfd_get_section_by_name (abfd, ".dynsym"); | |
3313 | if (s != NULL) | |
3314 | d->this_hdr.sh_link = elf_section_data (s)->this_idx; | |
3315 | ||
3316 | /* We look up the section the relocs apply to by name. */ | |
3317 | name = sec->name; | |
3318 | if (d->this_hdr.sh_type == SHT_REL) | |
3319 | name += 4; | |
3320 | else | |
3321 | name += 5; | |
3322 | s = bfd_get_section_by_name (abfd, name); | |
3323 | if (s != NULL) | |
9ef5d938 L |
3324 | { |
3325 | d->this_hdr.sh_info = elf_section_data (s)->this_idx; | |
3326 | d->this_hdr.sh_flags |= SHF_INFO_LINK; | |
3327 | } | |
252b5132 RH |
3328 | break; |
3329 | ||
3330 | case SHT_STRTAB: | |
3331 | /* We assume that a section named .stab*str is a stabs | |
3332 | string section. We look for a section with the same name | |
3333 | but without the trailing ``str'', and set its sh_link | |
3334 | field to point to this section. */ | |
0112cd26 | 3335 | if (CONST_STRNEQ (sec->name, ".stab") |
252b5132 RH |
3336 | && strcmp (sec->name + strlen (sec->name) - 3, "str") == 0) |
3337 | { | |
3338 | size_t len; | |
3339 | char *alc; | |
3340 | ||
3341 | len = strlen (sec->name); | |
a50b1753 | 3342 | alc = (char *) bfd_malloc (len - 2); |
252b5132 | 3343 | if (alc == NULL) |
b34976b6 | 3344 | return FALSE; |
d4c88bbb | 3345 | memcpy (alc, sec->name, len - 3); |
252b5132 RH |
3346 | alc[len - 3] = '\0'; |
3347 | s = bfd_get_section_by_name (abfd, alc); | |
3348 | free (alc); | |
3349 | if (s != NULL) | |
3350 | { | |
3351 | elf_section_data (s)->this_hdr.sh_link = d->this_idx; | |
3352 | ||
3353 | /* This is a .stab section. */ | |
0594c12d AM |
3354 | if (elf_section_data (s)->this_hdr.sh_entsize == 0) |
3355 | elf_section_data (s)->this_hdr.sh_entsize | |
3356 | = 4 + 2 * bfd_get_arch_size (abfd) / 8; | |
252b5132 RH |
3357 | } |
3358 | } | |
3359 | break; | |
3360 | ||
3361 | case SHT_DYNAMIC: | |
3362 | case SHT_DYNSYM: | |
3363 | case SHT_GNU_verneed: | |
3364 | case SHT_GNU_verdef: | |
3365 | /* sh_link is the section header index of the string table | |
3366 | used for the dynamic entries, or the symbol table, or the | |
3367 | version strings. */ | |
3368 | s = bfd_get_section_by_name (abfd, ".dynstr"); | |
3369 | if (s != NULL) | |
3370 | d->this_hdr.sh_link = elf_section_data (s)->this_idx; | |
3371 | break; | |
3372 | ||
7f1204bb JJ |
3373 | case SHT_GNU_LIBLIST: |
3374 | /* sh_link is the section header index of the prelink library | |
08a40648 AM |
3375 | list used for the dynamic entries, or the symbol table, or |
3376 | the version strings. */ | |
7f1204bb JJ |
3377 | s = bfd_get_section_by_name (abfd, (sec->flags & SEC_ALLOC) |
3378 | ? ".dynstr" : ".gnu.libstr"); | |
3379 | if (s != NULL) | |
3380 | d->this_hdr.sh_link = elf_section_data (s)->this_idx; | |
3381 | break; | |
3382 | ||
252b5132 | 3383 | case SHT_HASH: |
fdc90cb4 | 3384 | case SHT_GNU_HASH: |
252b5132 RH |
3385 | case SHT_GNU_versym: |
3386 | /* sh_link is the section header index of the symbol table | |
3387 | this hash table or version table is for. */ | |
3388 | s = bfd_get_section_by_name (abfd, ".dynsym"); | |
3389 | if (s != NULL) | |
3390 | d->this_hdr.sh_link = elf_section_data (s)->this_idx; | |
3391 | break; | |
dbb410c3 AM |
3392 | |
3393 | case SHT_GROUP: | |
12bd6957 | 3394 | d->this_hdr.sh_link = elf_onesymtab (abfd); |
252b5132 RH |
3395 | } |
3396 | } | |
3397 | ||
2b0f7ef9 | 3398 | for (secn = 1; secn < section_number; ++secn) |
9ad5cbcf AM |
3399 | if (i_shdrp[secn] == NULL) |
3400 | i_shdrp[secn] = i_shdrp[0]; | |
3401 | else | |
3402 | i_shdrp[secn]->sh_name = _bfd_elf_strtab_offset (elf_shstrtab (abfd), | |
3403 | i_shdrp[secn]->sh_name); | |
b34976b6 | 3404 | return TRUE; |
252b5132 RH |
3405 | } |
3406 | ||
5372391b | 3407 | static bfd_boolean |
217aa764 | 3408 | sym_is_global (bfd *abfd, asymbol *sym) |
252b5132 RH |
3409 | { |
3410 | /* If the backend has a special mapping, use it. */ | |
9c5bfbb7 | 3411 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
217aa764 AM |
3412 | if (bed->elf_backend_sym_is_global) |
3413 | return (*bed->elf_backend_sym_is_global) (abfd, sym); | |
252b5132 | 3414 | |
e47bf690 | 3415 | return ((sym->flags & (BSF_GLOBAL | BSF_WEAK | BSF_GNU_UNIQUE)) != 0 |
252b5132 RH |
3416 | || bfd_is_und_section (bfd_get_section (sym)) |
3417 | || bfd_is_com_section (bfd_get_section (sym))); | |
3418 | } | |
3419 | ||
5372391b | 3420 | /* Don't output section symbols for sections that are not going to be |
c6d8cab4 | 3421 | output, that are duplicates or there is no BFD section. */ |
5372391b AM |
3422 | |
3423 | static bfd_boolean | |
3424 | ignore_section_sym (bfd *abfd, asymbol *sym) | |
3425 | { | |
c6d8cab4 L |
3426 | elf_symbol_type *type_ptr; |
3427 | ||
3428 | if ((sym->flags & BSF_SECTION_SYM) == 0) | |
3429 | return FALSE; | |
3430 | ||
3431 | type_ptr = elf_symbol_from (abfd, sym); | |
3432 | return ((type_ptr != NULL | |
3433 | && type_ptr->internal_elf_sym.st_shndx != 0 | |
3434 | && bfd_is_abs_section (sym->section)) | |
3435 | || !(sym->section->owner == abfd | |
0f0a5e58 | 3436 | || (sym->section->output_section->owner == abfd |
2633a79c AM |
3437 | && sym->section->output_offset == 0) |
3438 | || bfd_is_abs_section (sym->section))); | |
5372391b AM |
3439 | } |
3440 | ||
2633a79c AM |
3441 | /* Map symbol from it's internal number to the external number, moving |
3442 | all local symbols to be at the head of the list. */ | |
3443 | ||
b34976b6 | 3444 | static bfd_boolean |
12bd6957 | 3445 | elf_map_symbols (bfd *abfd, unsigned int *pnum_locals) |
252b5132 | 3446 | { |
dc810e39 | 3447 | unsigned int symcount = bfd_get_symcount (abfd); |
252b5132 RH |
3448 | asymbol **syms = bfd_get_outsymbols (abfd); |
3449 | asymbol **sect_syms; | |
dc810e39 AM |
3450 | unsigned int num_locals = 0; |
3451 | unsigned int num_globals = 0; | |
3452 | unsigned int num_locals2 = 0; | |
3453 | unsigned int num_globals2 = 0; | |
252b5132 | 3454 | int max_index = 0; |
dc810e39 | 3455 | unsigned int idx; |
252b5132 RH |
3456 | asection *asect; |
3457 | asymbol **new_syms; | |
252b5132 RH |
3458 | |
3459 | #ifdef DEBUG | |
3460 | fprintf (stderr, "elf_map_symbols\n"); | |
3461 | fflush (stderr); | |
3462 | #endif | |
3463 | ||
252b5132 RH |
3464 | for (asect = abfd->sections; asect; asect = asect->next) |
3465 | { | |
3466 | if (max_index < asect->index) | |
3467 | max_index = asect->index; | |
3468 | } | |
3469 | ||
3470 | max_index++; | |
a50b1753 | 3471 | sect_syms = (asymbol **) bfd_zalloc2 (abfd, max_index, sizeof (asymbol *)); |
252b5132 | 3472 | if (sect_syms == NULL) |
b34976b6 | 3473 | return FALSE; |
252b5132 | 3474 | elf_section_syms (abfd) = sect_syms; |
4e89ac30 | 3475 | elf_num_section_syms (abfd) = max_index; |
252b5132 | 3476 | |
079e9a2f AM |
3477 | /* Init sect_syms entries for any section symbols we have already |
3478 | decided to output. */ | |
252b5132 RH |
3479 | for (idx = 0; idx < symcount; idx++) |
3480 | { | |
dc810e39 | 3481 | asymbol *sym = syms[idx]; |
c044fabd | 3482 | |
252b5132 | 3483 | if ((sym->flags & BSF_SECTION_SYM) != 0 |
0f0a5e58 | 3484 | && sym->value == 0 |
2633a79c AM |
3485 | && !ignore_section_sym (abfd, sym) |
3486 | && !bfd_is_abs_section (sym->section)) | |
252b5132 | 3487 | { |
5372391b | 3488 | asection *sec = sym->section; |
252b5132 | 3489 | |
5372391b AM |
3490 | if (sec->owner != abfd) |
3491 | sec = sec->output_section; | |
252b5132 | 3492 | |
5372391b | 3493 | sect_syms[sec->index] = syms[idx]; |
252b5132 RH |
3494 | } |
3495 | } | |
3496 | ||
252b5132 RH |
3497 | /* Classify all of the symbols. */ |
3498 | for (idx = 0; idx < symcount; idx++) | |
3499 | { | |
2633a79c | 3500 | if (sym_is_global (abfd, syms[idx])) |
252b5132 | 3501 | num_globals++; |
2633a79c AM |
3502 | else if (!ignore_section_sym (abfd, syms[idx])) |
3503 | num_locals++; | |
252b5132 | 3504 | } |
079e9a2f | 3505 | |
5372391b | 3506 | /* We will be adding a section symbol for each normal BFD section. Most |
079e9a2f AM |
3507 | sections will already have a section symbol in outsymbols, but |
3508 | eg. SHT_GROUP sections will not, and we need the section symbol mapped | |
3509 | at least in that case. */ | |
252b5132 RH |
3510 | for (asect = abfd->sections; asect; asect = asect->next) |
3511 | { | |
079e9a2f | 3512 | if (sect_syms[asect->index] == NULL) |
252b5132 | 3513 | { |
079e9a2f | 3514 | if (!sym_is_global (abfd, asect->symbol)) |
252b5132 RH |
3515 | num_locals++; |
3516 | else | |
3517 | num_globals++; | |
252b5132 RH |
3518 | } |
3519 | } | |
3520 | ||
3521 | /* Now sort the symbols so the local symbols are first. */ | |
a50b1753 NC |
3522 | new_syms = (asymbol **) bfd_alloc2 (abfd, num_locals + num_globals, |
3523 | sizeof (asymbol *)); | |
dc810e39 | 3524 | |
252b5132 | 3525 | if (new_syms == NULL) |
b34976b6 | 3526 | return FALSE; |
252b5132 RH |
3527 | |
3528 | for (idx = 0; idx < symcount; idx++) | |
3529 | { | |
3530 | asymbol *sym = syms[idx]; | |
dc810e39 | 3531 | unsigned int i; |
252b5132 | 3532 | |
2633a79c AM |
3533 | if (sym_is_global (abfd, sym)) |
3534 | i = num_locals + num_globals2++; | |
3535 | else if (!ignore_section_sym (abfd, sym)) | |
252b5132 RH |
3536 | i = num_locals2++; |
3537 | else | |
2633a79c | 3538 | continue; |
252b5132 RH |
3539 | new_syms[i] = sym; |
3540 | sym->udata.i = i + 1; | |
3541 | } | |
3542 | for (asect = abfd->sections; asect; asect = asect->next) | |
3543 | { | |
079e9a2f | 3544 | if (sect_syms[asect->index] == NULL) |
252b5132 | 3545 | { |
079e9a2f | 3546 | asymbol *sym = asect->symbol; |
dc810e39 | 3547 | unsigned int i; |
252b5132 | 3548 | |
079e9a2f | 3549 | sect_syms[asect->index] = sym; |
252b5132 RH |
3550 | if (!sym_is_global (abfd, sym)) |
3551 | i = num_locals2++; | |
3552 | else | |
3553 | i = num_locals + num_globals2++; | |
3554 | new_syms[i] = sym; | |
3555 | sym->udata.i = i + 1; | |
3556 | } | |
3557 | } | |
3558 | ||
3559 | bfd_set_symtab (abfd, new_syms, num_locals + num_globals); | |
3560 | ||
12bd6957 | 3561 | *pnum_locals = num_locals; |
b34976b6 | 3562 | return TRUE; |
252b5132 RH |
3563 | } |
3564 | ||
3565 | /* Align to the maximum file alignment that could be required for any | |
3566 | ELF data structure. */ | |
3567 | ||
268b6b39 | 3568 | static inline file_ptr |
217aa764 | 3569 | align_file_position (file_ptr off, int align) |
252b5132 RH |
3570 | { |
3571 | return (off + align - 1) & ~(align - 1); | |
3572 | } | |
3573 | ||
3574 | /* Assign a file position to a section, optionally aligning to the | |
3575 | required section alignment. */ | |
3576 | ||
217aa764 AM |
3577 | file_ptr |
3578 | _bfd_elf_assign_file_position_for_section (Elf_Internal_Shdr *i_shdrp, | |
3579 | file_ptr offset, | |
3580 | bfd_boolean align) | |
252b5132 | 3581 | { |
72de5009 AM |
3582 | if (align && i_shdrp->sh_addralign > 1) |
3583 | offset = BFD_ALIGN (offset, i_shdrp->sh_addralign); | |
252b5132 RH |
3584 | i_shdrp->sh_offset = offset; |
3585 | if (i_shdrp->bfd_section != NULL) | |
3586 | i_shdrp->bfd_section->filepos = offset; | |
3587 | if (i_shdrp->sh_type != SHT_NOBITS) | |
3588 | offset += i_shdrp->sh_size; | |
3589 | return offset; | |
3590 | } | |
3591 | ||
3592 | /* Compute the file positions we are going to put the sections at, and | |
3593 | otherwise prepare to begin writing out the ELF file. If LINK_INFO | |
3594 | is not NULL, this is being called by the ELF backend linker. */ | |
3595 | ||
b34976b6 | 3596 | bfd_boolean |
217aa764 AM |
3597 | _bfd_elf_compute_section_file_positions (bfd *abfd, |
3598 | struct bfd_link_info *link_info) | |
252b5132 | 3599 | { |
9c5bfbb7 | 3600 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
d4730f92 | 3601 | struct fake_section_arg fsargs; |
b34976b6 | 3602 | bfd_boolean failed; |
4b6c0f2f | 3603 | struct bfd_strtab_hash *strtab = NULL; |
252b5132 | 3604 | Elf_Internal_Shdr *shstrtab_hdr; |
3516e984 | 3605 | bfd_boolean need_symtab; |
252b5132 RH |
3606 | |
3607 | if (abfd->output_has_begun) | |
b34976b6 | 3608 | return TRUE; |
252b5132 RH |
3609 | |
3610 | /* Do any elf backend specific processing first. */ | |
3611 | if (bed->elf_backend_begin_write_processing) | |
3612 | (*bed->elf_backend_begin_write_processing) (abfd, link_info); | |
3613 | ||
3614 | if (! prep_headers (abfd)) | |
b34976b6 | 3615 | return FALSE; |
252b5132 | 3616 | |
e6c51ed4 | 3617 | /* Post process the headers if necessary. */ |
78245035 | 3618 | (*bed->elf_backend_post_process_headers) (abfd, link_info); |
e6c51ed4 | 3619 | |
d4730f92 BS |
3620 | fsargs.failed = FALSE; |
3621 | fsargs.link_info = link_info; | |
3622 | bfd_map_over_sections (abfd, elf_fake_sections, &fsargs); | |
3623 | if (fsargs.failed) | |
b34976b6 | 3624 | return FALSE; |
252b5132 | 3625 | |
da9f89d4 | 3626 | if (!assign_section_numbers (abfd, link_info)) |
b34976b6 | 3627 | return FALSE; |
252b5132 RH |
3628 | |
3629 | /* The backend linker builds symbol table information itself. */ | |
3516e984 L |
3630 | need_symtab = (link_info == NULL |
3631 | && (bfd_get_symcount (abfd) > 0 | |
3632 | || ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC)) | |
3633 | == HAS_RELOC))); | |
3634 | if (need_symtab) | |
252b5132 RH |
3635 | { |
3636 | /* Non-zero if doing a relocatable link. */ | |
3637 | int relocatable_p = ! (abfd->flags & (EXEC_P | DYNAMIC)); | |
3638 | ||
3639 | if (! swap_out_syms (abfd, &strtab, relocatable_p)) | |
b34976b6 | 3640 | return FALSE; |
252b5132 RH |
3641 | } |
3642 | ||
d4730f92 | 3643 | failed = FALSE; |
1126897b | 3644 | if (link_info == NULL) |
dbb410c3 | 3645 | { |
1126897b | 3646 | bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed); |
dbb410c3 | 3647 | if (failed) |
b34976b6 | 3648 | return FALSE; |
dbb410c3 AM |
3649 | } |
3650 | ||
252b5132 RH |
3651 | shstrtab_hdr = &elf_tdata (abfd)->shstrtab_hdr; |
3652 | /* sh_name was set in prep_headers. */ | |
3653 | shstrtab_hdr->sh_type = SHT_STRTAB; | |
3654 | shstrtab_hdr->sh_flags = 0; | |
3655 | shstrtab_hdr->sh_addr = 0; | |
2b0f7ef9 | 3656 | shstrtab_hdr->sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd)); |
252b5132 RH |
3657 | shstrtab_hdr->sh_entsize = 0; |
3658 | shstrtab_hdr->sh_link = 0; | |
3659 | shstrtab_hdr->sh_info = 0; | |
3660 | /* sh_offset is set in assign_file_positions_except_relocs. */ | |
3661 | shstrtab_hdr->sh_addralign = 1; | |
3662 | ||
c84fca4d | 3663 | if (!assign_file_positions_except_relocs (abfd, link_info)) |
b34976b6 | 3664 | return FALSE; |
252b5132 | 3665 | |
3516e984 | 3666 | if (need_symtab) |
252b5132 RH |
3667 | { |
3668 | file_ptr off; | |
3669 | Elf_Internal_Shdr *hdr; | |
3670 | ||
12bd6957 | 3671 | off = elf_next_file_pos (abfd); |
252b5132 RH |
3672 | |
3673 | hdr = &elf_tdata (abfd)->symtab_hdr; | |
b34976b6 | 3674 | off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE); |
252b5132 | 3675 | |
9ad5cbcf AM |
3676 | hdr = &elf_tdata (abfd)->symtab_shndx_hdr; |
3677 | if (hdr->sh_size != 0) | |
b34976b6 | 3678 | off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE); |
9ad5cbcf | 3679 | |
252b5132 | 3680 | hdr = &elf_tdata (abfd)->strtab_hdr; |
b34976b6 | 3681 | off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE); |
252b5132 | 3682 | |
12bd6957 | 3683 | elf_next_file_pos (abfd) = off; |
252b5132 RH |
3684 | |
3685 | /* Now that we know where the .strtab section goes, write it | |
08a40648 | 3686 | out. */ |
252b5132 RH |
3687 | if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0 |
3688 | || ! _bfd_stringtab_emit (abfd, strtab)) | |
b34976b6 | 3689 | return FALSE; |
252b5132 RH |
3690 | _bfd_stringtab_free (strtab); |
3691 | } | |
3692 | ||
b34976b6 | 3693 | abfd->output_has_begun = TRUE; |
252b5132 | 3694 | |
b34976b6 | 3695 | return TRUE; |
252b5132 RH |
3696 | } |
3697 | ||
8ded5a0f AM |
3698 | /* Make an initial estimate of the size of the program header. If we |
3699 | get the number wrong here, we'll redo section placement. */ | |
3700 | ||
3701 | static bfd_size_type | |
3702 | get_program_header_size (bfd *abfd, struct bfd_link_info *info) | |
3703 | { | |
3704 | size_t segs; | |
3705 | asection *s; | |
2b05f1b7 | 3706 | const struct elf_backend_data *bed; |
8ded5a0f AM |
3707 | |
3708 | /* Assume we will need exactly two PT_LOAD segments: one for text | |
3709 | and one for data. */ | |
3710 | segs = 2; | |
3711 | ||
3712 | s = bfd_get_section_by_name (abfd, ".interp"); | |
3713 | if (s != NULL && (s->flags & SEC_LOAD) != 0) | |
3714 | { | |
3715 | /* If we have a loadable interpreter section, we need a | |
3716 | PT_INTERP segment. In this case, assume we also need a | |
3717 | PT_PHDR segment, although that may not be true for all | |
3718 | targets. */ | |
3719 | segs += 2; | |
3720 | } | |
3721 | ||
3722 | if (bfd_get_section_by_name (abfd, ".dynamic") != NULL) | |
3723 | { | |
3724 | /* We need a PT_DYNAMIC segment. */ | |
3725 | ++segs; | |
f210dcff | 3726 | } |
08a40648 | 3727 | |
ceae84aa | 3728 | if (info != NULL && info->relro) |
f210dcff L |
3729 | { |
3730 | /* We need a PT_GNU_RELRO segment. */ | |
3731 | ++segs; | |
8ded5a0f AM |
3732 | } |
3733 | ||
12bd6957 | 3734 | if (elf_eh_frame_hdr (abfd)) |
8ded5a0f AM |
3735 | { |
3736 | /* We need a PT_GNU_EH_FRAME segment. */ | |
3737 | ++segs; | |
3738 | } | |
3739 | ||
12bd6957 | 3740 | if (elf_stack_flags (abfd)) |
8ded5a0f | 3741 | { |
2b05f1b7 L |
3742 | /* We need a PT_GNU_STACK segment. */ |
3743 | ++segs; | |
3744 | } | |
94b11780 | 3745 | |
2b05f1b7 L |
3746 | for (s = abfd->sections; s != NULL; s = s->next) |
3747 | { | |
8ded5a0f | 3748 | if ((s->flags & SEC_LOAD) != 0 |
0112cd26 | 3749 | && CONST_STRNEQ (s->name, ".note")) |
8ded5a0f AM |
3750 | { |
3751 | /* We need a PT_NOTE segment. */ | |
3752 | ++segs; | |
1c5265b5 JJ |
3753 | /* Try to create just one PT_NOTE segment |
3754 | for all adjacent loadable .note* sections. | |
3755 | gABI requires that within a PT_NOTE segment | |
3756 | (and also inside of each SHT_NOTE section) | |
3757 | each note is padded to a multiple of 4 size, | |
3758 | so we check whether the sections are correctly | |
3759 | aligned. */ | |
3760 | if (s->alignment_power == 2) | |
3761 | while (s->next != NULL | |
3762 | && s->next->alignment_power == 2 | |
3763 | && (s->next->flags & SEC_LOAD) != 0 | |
3764 | && CONST_STRNEQ (s->next->name, ".note")) | |
3765 | s = s->next; | |
8ded5a0f AM |
3766 | } |
3767 | } | |
3768 | ||
3769 | for (s = abfd->sections; s != NULL; s = s->next) | |
3770 | { | |
3771 | if (s->flags & SEC_THREAD_LOCAL) | |
3772 | { | |
3773 | /* We need a PT_TLS segment. */ | |
3774 | ++segs; | |
3775 | break; | |
3776 | } | |
3777 | } | |
3778 | ||
3779 | /* Let the backend count up any program headers it might need. */ | |
2b05f1b7 | 3780 | bed = get_elf_backend_data (abfd); |
8ded5a0f AM |
3781 | if (bed->elf_backend_additional_program_headers) |
3782 | { | |
3783 | int a; | |
3784 | ||
3785 | a = (*bed->elf_backend_additional_program_headers) (abfd, info); | |
3786 | if (a == -1) | |
3787 | abort (); | |
3788 | segs += a; | |
3789 | } | |
3790 | ||
3791 | return segs * bed->s->sizeof_phdr; | |
3792 | } | |
3793 | ||
2ea37f1c NC |
3794 | /* Find the segment that contains the output_section of section. */ |
3795 | ||
3796 | Elf_Internal_Phdr * | |
3797 | _bfd_elf_find_segment_containing_section (bfd * abfd, asection * section) | |
3798 | { | |
3799 | struct elf_segment_map *m; | |
3800 | Elf_Internal_Phdr *p; | |
3801 | ||
12bd6957 | 3802 | for (m = elf_seg_map (abfd), p = elf_tdata (abfd)->phdr; |
2ea37f1c NC |
3803 | m != NULL; |
3804 | m = m->next, p++) | |
3805 | { | |
3806 | int i; | |
3807 | ||
3808 | for (i = m->count - 1; i >= 0; i--) | |
3809 | if (m->sections[i] == section) | |
3810 | return p; | |
3811 | } | |
3812 | ||
3813 | return NULL; | |
3814 | } | |
3815 | ||
252b5132 RH |
3816 | /* Create a mapping from a set of sections to a program segment. */ |
3817 | ||
217aa764 AM |
3818 | static struct elf_segment_map * |
3819 | make_mapping (bfd *abfd, | |
3820 | asection **sections, | |
3821 | unsigned int from, | |
3822 | unsigned int to, | |
3823 | bfd_boolean phdr) | |
252b5132 RH |
3824 | { |
3825 | struct elf_segment_map *m; | |
3826 | unsigned int i; | |
3827 | asection **hdrpp; | |
dc810e39 | 3828 | bfd_size_type amt; |
252b5132 | 3829 | |
dc810e39 AM |
3830 | amt = sizeof (struct elf_segment_map); |
3831 | amt += (to - from - 1) * sizeof (asection *); | |
a50b1753 | 3832 | m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); |
252b5132 RH |
3833 | if (m == NULL) |
3834 | return NULL; | |
3835 | m->next = NULL; | |
3836 | m->p_type = PT_LOAD; | |
3837 | for (i = from, hdrpp = sections + from; i < to; i++, hdrpp++) | |
3838 | m->sections[i - from] = *hdrpp; | |
3839 | m->count = to - from; | |
3840 | ||
3841 | if (from == 0 && phdr) | |
3842 | { | |
3843 | /* Include the headers in the first PT_LOAD segment. */ | |
3844 | m->includes_filehdr = 1; | |
3845 | m->includes_phdrs = 1; | |
3846 | } | |
3847 | ||
3848 | return m; | |
3849 | } | |
3850 | ||
229fcec5 MM |
3851 | /* Create the PT_DYNAMIC segment, which includes DYNSEC. Returns NULL |
3852 | on failure. */ | |
3853 | ||
3854 | struct elf_segment_map * | |
3855 | _bfd_elf_make_dynamic_segment (bfd *abfd, asection *dynsec) | |
3856 | { | |
3857 | struct elf_segment_map *m; | |
3858 | ||
a50b1753 NC |
3859 | m = (struct elf_segment_map *) bfd_zalloc (abfd, |
3860 | sizeof (struct elf_segment_map)); | |
229fcec5 MM |
3861 | if (m == NULL) |
3862 | return NULL; | |
3863 | m->next = NULL; | |
3864 | m->p_type = PT_DYNAMIC; | |
3865 | m->count = 1; | |
3866 | m->sections[0] = dynsec; | |
08a40648 | 3867 | |
229fcec5 MM |
3868 | return m; |
3869 | } | |
3870 | ||
8ded5a0f | 3871 | /* Possibly add or remove segments from the segment map. */ |
252b5132 | 3872 | |
b34976b6 | 3873 | static bfd_boolean |
3dea8fca AM |
3874 | elf_modify_segment_map (bfd *abfd, |
3875 | struct bfd_link_info *info, | |
3876 | bfd_boolean remove_empty_load) | |
252b5132 | 3877 | { |
252e386e | 3878 | struct elf_segment_map **m; |
8ded5a0f | 3879 | const struct elf_backend_data *bed; |
252b5132 | 3880 | |
8ded5a0f AM |
3881 | /* The placement algorithm assumes that non allocated sections are |
3882 | not in PT_LOAD segments. We ensure this here by removing such | |
3883 | sections from the segment map. We also remove excluded | |
252e386e AM |
3884 | sections. Finally, any PT_LOAD segment without sections is |
3885 | removed. */ | |
12bd6957 | 3886 | m = &elf_seg_map (abfd); |
252e386e | 3887 | while (*m) |
8ded5a0f AM |
3888 | { |
3889 | unsigned int i, new_count; | |
252b5132 | 3890 | |
252e386e | 3891 | for (new_count = 0, i = 0; i < (*m)->count; i++) |
8ded5a0f | 3892 | { |
252e386e AM |
3893 | if (((*m)->sections[i]->flags & SEC_EXCLUDE) == 0 |
3894 | && (((*m)->sections[i]->flags & SEC_ALLOC) != 0 | |
3895 | || (*m)->p_type != PT_LOAD)) | |
8ded5a0f | 3896 | { |
252e386e AM |
3897 | (*m)->sections[new_count] = (*m)->sections[i]; |
3898 | new_count++; | |
8ded5a0f AM |
3899 | } |
3900 | } | |
252e386e | 3901 | (*m)->count = new_count; |
252b5132 | 3902 | |
3dea8fca | 3903 | if (remove_empty_load && (*m)->p_type == PT_LOAD && (*m)->count == 0) |
252e386e AM |
3904 | *m = (*m)->next; |
3905 | else | |
3906 | m = &(*m)->next; | |
8ded5a0f | 3907 | } |
252b5132 | 3908 | |
8ded5a0f AM |
3909 | bed = get_elf_backend_data (abfd); |
3910 | if (bed->elf_backend_modify_segment_map != NULL) | |
252b5132 | 3911 | { |
252e386e | 3912 | if (!(*bed->elf_backend_modify_segment_map) (abfd, info)) |
8ded5a0f | 3913 | return FALSE; |
252b5132 | 3914 | } |
252b5132 | 3915 | |
8ded5a0f AM |
3916 | return TRUE; |
3917 | } | |
252b5132 | 3918 | |
8ded5a0f | 3919 | /* Set up a mapping from BFD sections to program segments. */ |
252b5132 | 3920 | |
8ded5a0f AM |
3921 | bfd_boolean |
3922 | _bfd_elf_map_sections_to_segments (bfd *abfd, struct bfd_link_info *info) | |
3923 | { | |
3924 | unsigned int count; | |
3925 | struct elf_segment_map *m; | |
3926 | asection **sections = NULL; | |
3927 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
3dea8fca | 3928 | bfd_boolean no_user_phdrs; |
252b5132 | 3929 | |
12bd6957 | 3930 | no_user_phdrs = elf_seg_map (abfd) == NULL; |
d324f6d6 RM |
3931 | |
3932 | if (info != NULL) | |
3933 | info->user_phdrs = !no_user_phdrs; | |
3934 | ||
3dea8fca | 3935 | if (no_user_phdrs && bfd_count_sections (abfd) != 0) |
252b5132 | 3936 | { |
8ded5a0f AM |
3937 | asection *s; |
3938 | unsigned int i; | |
3939 | struct elf_segment_map *mfirst; | |
3940 | struct elf_segment_map **pm; | |
3941 | asection *last_hdr; | |
3942 | bfd_vma last_size; | |
3943 | unsigned int phdr_index; | |
3944 | bfd_vma maxpagesize; | |
3945 | asection **hdrpp; | |
3946 | bfd_boolean phdr_in_segment = TRUE; | |
3947 | bfd_boolean writable; | |
3948 | int tls_count = 0; | |
3949 | asection *first_tls = NULL; | |
3950 | asection *dynsec, *eh_frame_hdr; | |
3951 | bfd_size_type amt; | |
8d06853e | 3952 | bfd_vma addr_mask, wrap_to = 0; |
252b5132 | 3953 | |
8ded5a0f | 3954 | /* Select the allocated sections, and sort them. */ |
252b5132 | 3955 | |
a50b1753 NC |
3956 | sections = (asection **) bfd_malloc2 (bfd_count_sections (abfd), |
3957 | sizeof (asection *)); | |
8ded5a0f | 3958 | if (sections == NULL) |
252b5132 | 3959 | goto error_return; |
252b5132 | 3960 | |
8d06853e AM |
3961 | /* Calculate top address, avoiding undefined behaviour of shift |
3962 | left operator when shift count is equal to size of type | |
3963 | being shifted. */ | |
3964 | addr_mask = ((bfd_vma) 1 << (bfd_arch_bits_per_address (abfd) - 1)) - 1; | |
3965 | addr_mask = (addr_mask << 1) + 1; | |
3966 | ||
8ded5a0f AM |
3967 | i = 0; |
3968 | for (s = abfd->sections; s != NULL; s = s->next) | |
3969 | { | |
3970 | if ((s->flags & SEC_ALLOC) != 0) | |
3971 | { | |
3972 | sections[i] = s; | |
3973 | ++i; | |
8d06853e AM |
3974 | /* A wrapping section potentially clashes with header. */ |
3975 | if (((s->lma + s->size) & addr_mask) < (s->lma & addr_mask)) | |
3976 | wrap_to = (s->lma + s->size) & addr_mask; | |
8ded5a0f AM |
3977 | } |
3978 | } | |
3979 | BFD_ASSERT (i <= bfd_count_sections (abfd)); | |
3980 | count = i; | |
252b5132 | 3981 | |
8ded5a0f | 3982 | qsort (sections, (size_t) count, sizeof (asection *), elf_sort_sections); |
252b5132 | 3983 | |
8ded5a0f | 3984 | /* Build the mapping. */ |
252b5132 | 3985 | |
8ded5a0f AM |
3986 | mfirst = NULL; |
3987 | pm = &mfirst; | |
252b5132 | 3988 | |
8ded5a0f AM |
3989 | /* If we have a .interp section, then create a PT_PHDR segment for |
3990 | the program headers and a PT_INTERP segment for the .interp | |
3991 | section. */ | |
3992 | s = bfd_get_section_by_name (abfd, ".interp"); | |
3993 | if (s != NULL && (s->flags & SEC_LOAD) != 0) | |
3994 | { | |
3995 | amt = sizeof (struct elf_segment_map); | |
a50b1753 | 3996 | m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); |
8ded5a0f AM |
3997 | if (m == NULL) |
3998 | goto error_return; | |
3999 | m->next = NULL; | |
4000 | m->p_type = PT_PHDR; | |
4001 | /* FIXME: UnixWare and Solaris set PF_X, Irix 5 does not. */ | |
4002 | m->p_flags = PF_R | PF_X; | |
4003 | m->p_flags_valid = 1; | |
4004 | m->includes_phdrs = 1; | |
252b5132 | 4005 | |
8ded5a0f AM |
4006 | *pm = m; |
4007 | pm = &m->next; | |
252b5132 | 4008 | |
8ded5a0f | 4009 | amt = sizeof (struct elf_segment_map); |
a50b1753 | 4010 | m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); |
8ded5a0f AM |
4011 | if (m == NULL) |
4012 | goto error_return; | |
4013 | m->next = NULL; | |
4014 | m->p_type = PT_INTERP; | |
4015 | m->count = 1; | |
4016 | m->sections[0] = s; | |
4017 | ||
4018 | *pm = m; | |
4019 | pm = &m->next; | |
252b5132 | 4020 | } |
8ded5a0f AM |
4021 | |
4022 | /* Look through the sections. We put sections in the same program | |
4023 | segment when the start of the second section can be placed within | |
4024 | a few bytes of the end of the first section. */ | |
4025 | last_hdr = NULL; | |
4026 | last_size = 0; | |
4027 | phdr_index = 0; | |
4028 | maxpagesize = bed->maxpagesize; | |
063bb025 NC |
4029 | /* PR 17512: file: c8455299. |
4030 | Avoid divide-by-zero errors later on. | |
4031 | FIXME: Should we abort if the maxpagesize is zero ? */ | |
4032 | if (maxpagesize == 0) | |
4033 | maxpagesize = 1; | |
8ded5a0f AM |
4034 | writable = FALSE; |
4035 | dynsec = bfd_get_section_by_name (abfd, ".dynamic"); | |
4036 | if (dynsec != NULL | |
4037 | && (dynsec->flags & SEC_LOAD) == 0) | |
4038 | dynsec = NULL; | |
4039 | ||
4040 | /* Deal with -Ttext or something similar such that the first section | |
4041 | is not adjacent to the program headers. This is an | |
4042 | approximation, since at this point we don't know exactly how many | |
4043 | program headers we will need. */ | |
4044 | if (count > 0) | |
252b5132 | 4045 | { |
12bd6957 | 4046 | bfd_size_type phdr_size = elf_program_header_size (abfd); |
8ded5a0f | 4047 | |
62d7a5f6 | 4048 | if (phdr_size == (bfd_size_type) -1) |
8ded5a0f | 4049 | phdr_size = get_program_header_size (abfd, info); |
d2bcb0d1 | 4050 | phdr_size += bed->s->sizeof_ehdr; |
8ded5a0f | 4051 | if ((abfd->flags & D_PAGED) == 0 |
8d06853e AM |
4052 | || (sections[0]->lma & addr_mask) < phdr_size |
4053 | || ((sections[0]->lma & addr_mask) % maxpagesize | |
4054 | < phdr_size % maxpagesize) | |
4055 | || (sections[0]->lma & addr_mask & -maxpagesize) < wrap_to) | |
8ded5a0f | 4056 | phdr_in_segment = FALSE; |
252b5132 RH |
4057 | } |
4058 | ||
8ded5a0f | 4059 | for (i = 0, hdrpp = sections; i < count; i++, hdrpp++) |
252b5132 | 4060 | { |
8ded5a0f AM |
4061 | asection *hdr; |
4062 | bfd_boolean new_segment; | |
4063 | ||
4064 | hdr = *hdrpp; | |
4065 | ||
4066 | /* See if this section and the last one will fit in the same | |
4067 | segment. */ | |
4068 | ||
4069 | if (last_hdr == NULL) | |
4070 | { | |
4071 | /* If we don't have a segment yet, then we don't need a new | |
4072 | one (we build the last one after this loop). */ | |
4073 | new_segment = FALSE; | |
4074 | } | |
4075 | else if (last_hdr->lma - last_hdr->vma != hdr->lma - hdr->vma) | |
4076 | { | |
4077 | /* If this section has a different relation between the | |
4078 | virtual address and the load address, then we need a new | |
4079 | segment. */ | |
4080 | new_segment = TRUE; | |
4081 | } | |
b5599592 AM |
4082 | else if (hdr->lma < last_hdr->lma + last_size |
4083 | || last_hdr->lma + last_size < last_hdr->lma) | |
4084 | { | |
4085 | /* If this section has a load address that makes it overlap | |
4086 | the previous section, then we need a new segment. */ | |
4087 | new_segment = TRUE; | |
4088 | } | |
39948a60 NC |
4089 | /* In the next test we have to be careful when last_hdr->lma is close |
4090 | to the end of the address space. If the aligned address wraps | |
4091 | around to the start of the address space, then there are no more | |
4092 | pages left in memory and it is OK to assume that the current | |
4093 | section can be included in the current segment. */ | |
4094 | else if ((BFD_ALIGN (last_hdr->lma + last_size, maxpagesize) + maxpagesize | |
4095 | > last_hdr->lma) | |
4096 | && (BFD_ALIGN (last_hdr->lma + last_size, maxpagesize) + maxpagesize | |
4ff73856 | 4097 | <= hdr->lma)) |
8ded5a0f AM |
4098 | { |
4099 | /* If putting this section in this segment would force us to | |
4100 | skip a page in the segment, then we need a new segment. */ | |
4101 | new_segment = TRUE; | |
4102 | } | |
4103 | else if ((last_hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0 | |
4104 | && (hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) != 0) | |
4105 | { | |
4106 | /* We don't want to put a loadable section after a | |
4107 | nonloadable section in the same segment. | |
4108 | Consider .tbss sections as loadable for this purpose. */ | |
4109 | new_segment = TRUE; | |
4110 | } | |
4111 | else if ((abfd->flags & D_PAGED) == 0) | |
4112 | { | |
4113 | /* If the file is not demand paged, which means that we | |
4114 | don't require the sections to be correctly aligned in the | |
4115 | file, then there is no other reason for a new segment. */ | |
4116 | new_segment = FALSE; | |
4117 | } | |
4118 | else if (! writable | |
4119 | && (hdr->flags & SEC_READONLY) == 0 | |
8d06853e AM |
4120 | && (((last_hdr->lma + last_size - 1) & -maxpagesize) |
4121 | != (hdr->lma & -maxpagesize))) | |
8ded5a0f AM |
4122 | { |
4123 | /* We don't want to put a writable section in a read only | |
4124 | segment, unless they are on the same page in memory | |
4125 | anyhow. We already know that the last section does not | |
4126 | bring us past the current section on the page, so the | |
4127 | only case in which the new section is not on the same | |
4128 | page as the previous section is when the previous section | |
4129 | ends precisely on a page boundary. */ | |
4130 | new_segment = TRUE; | |
4131 | } | |
4132 | else | |
4133 | { | |
4134 | /* Otherwise, we can use the same segment. */ | |
4135 | new_segment = FALSE; | |
4136 | } | |
4137 | ||
2889e75b | 4138 | /* Allow interested parties a chance to override our decision. */ |
ceae84aa AM |
4139 | if (last_hdr != NULL |
4140 | && info != NULL | |
4141 | && info->callbacks->override_segment_assignment != NULL) | |
4142 | new_segment | |
4143 | = info->callbacks->override_segment_assignment (info, abfd, hdr, | |
4144 | last_hdr, | |
4145 | new_segment); | |
2889e75b | 4146 | |
8ded5a0f AM |
4147 | if (! new_segment) |
4148 | { | |
4149 | if ((hdr->flags & SEC_READONLY) == 0) | |
4150 | writable = TRUE; | |
4151 | last_hdr = hdr; | |
4152 | /* .tbss sections effectively have zero size. */ | |
4153 | if ((hdr->flags & (SEC_THREAD_LOCAL | SEC_LOAD)) | |
4154 | != SEC_THREAD_LOCAL) | |
4155 | last_size = hdr->size; | |
4156 | else | |
4157 | last_size = 0; | |
4158 | continue; | |
4159 | } | |
4160 | ||
4161 | /* We need a new program segment. We must create a new program | |
4162 | header holding all the sections from phdr_index until hdr. */ | |
4163 | ||
4164 | m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment); | |
4165 | if (m == NULL) | |
4166 | goto error_return; | |
4167 | ||
4168 | *pm = m; | |
4169 | pm = &m->next; | |
4170 | ||
252b5132 | 4171 | if ((hdr->flags & SEC_READONLY) == 0) |
b34976b6 | 4172 | writable = TRUE; |
8ded5a0f AM |
4173 | else |
4174 | writable = FALSE; | |
4175 | ||
baaff79e JJ |
4176 | last_hdr = hdr; |
4177 | /* .tbss sections effectively have zero size. */ | |
e5caec89 | 4178 | if ((hdr->flags & (SEC_THREAD_LOCAL | SEC_LOAD)) != SEC_THREAD_LOCAL) |
eea6121a | 4179 | last_size = hdr->size; |
baaff79e JJ |
4180 | else |
4181 | last_size = 0; | |
8ded5a0f AM |
4182 | phdr_index = i; |
4183 | phdr_in_segment = FALSE; | |
252b5132 RH |
4184 | } |
4185 | ||
86b2281f AM |
4186 | /* Create a final PT_LOAD program segment, but not if it's just |
4187 | for .tbss. */ | |
4188 | if (last_hdr != NULL | |
4189 | && (i - phdr_index != 1 | |
4190 | || ((last_hdr->flags & (SEC_THREAD_LOCAL | SEC_LOAD)) | |
4191 | != SEC_THREAD_LOCAL))) | |
8ded5a0f AM |
4192 | { |
4193 | m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment); | |
4194 | if (m == NULL) | |
4195 | goto error_return; | |
252b5132 | 4196 | |
8ded5a0f AM |
4197 | *pm = m; |
4198 | pm = &m->next; | |
4199 | } | |
252b5132 | 4200 | |
8ded5a0f AM |
4201 | /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */ |
4202 | if (dynsec != NULL) | |
4203 | { | |
4204 | m = _bfd_elf_make_dynamic_segment (abfd, dynsec); | |
4205 | if (m == NULL) | |
4206 | goto error_return; | |
4207 | *pm = m; | |
4208 | pm = &m->next; | |
4209 | } | |
252b5132 | 4210 | |
1c5265b5 JJ |
4211 | /* For each batch of consecutive loadable .note sections, |
4212 | add a PT_NOTE segment. We don't use bfd_get_section_by_name, | |
4213 | because if we link together nonloadable .note sections and | |
4214 | loadable .note sections, we will generate two .note sections | |
4215 | in the output file. FIXME: Using names for section types is | |
4216 | bogus anyhow. */ | |
8ded5a0f AM |
4217 | for (s = abfd->sections; s != NULL; s = s->next) |
4218 | { | |
4219 | if ((s->flags & SEC_LOAD) != 0 | |
0112cd26 | 4220 | && CONST_STRNEQ (s->name, ".note")) |
8ded5a0f | 4221 | { |
1c5265b5 | 4222 | asection *s2; |
91d6fa6a NC |
4223 | |
4224 | count = 1; | |
8ded5a0f | 4225 | amt = sizeof (struct elf_segment_map); |
1c5265b5 JJ |
4226 | if (s->alignment_power == 2) |
4227 | for (s2 = s; s2->next != NULL; s2 = s2->next) | |
55b581a6 JJ |
4228 | { |
4229 | if (s2->next->alignment_power == 2 | |
4230 | && (s2->next->flags & SEC_LOAD) != 0 | |
4231 | && CONST_STRNEQ (s2->next->name, ".note") | |
8d06853e AM |
4232 | && align_power (s2->lma + s2->size, 2) |
4233 | == s2->next->lma) | |
55b581a6 JJ |
4234 | count++; |
4235 | else | |
4236 | break; | |
4237 | } | |
1c5265b5 | 4238 | amt += (count - 1) * sizeof (asection *); |
a50b1753 | 4239 | m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); |
8ded5a0f AM |
4240 | if (m == NULL) |
4241 | goto error_return; | |
4242 | m->next = NULL; | |
4243 | m->p_type = PT_NOTE; | |
1c5265b5 JJ |
4244 | m->count = count; |
4245 | while (count > 1) | |
4246 | { | |
4247 | m->sections[m->count - count--] = s; | |
4248 | BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0); | |
4249 | s = s->next; | |
4250 | } | |
4251 | m->sections[m->count - 1] = s; | |
4252 | BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0); | |
8ded5a0f AM |
4253 | *pm = m; |
4254 | pm = &m->next; | |
4255 | } | |
4256 | if (s->flags & SEC_THREAD_LOCAL) | |
4257 | { | |
4258 | if (! tls_count) | |
4259 | first_tls = s; | |
4260 | tls_count++; | |
4261 | } | |
4262 | } | |
252b5132 | 4263 | |
8ded5a0f AM |
4264 | /* If there are any SHF_TLS output sections, add PT_TLS segment. */ |
4265 | if (tls_count > 0) | |
4266 | { | |
8ded5a0f AM |
4267 | amt = sizeof (struct elf_segment_map); |
4268 | amt += (tls_count - 1) * sizeof (asection *); | |
a50b1753 | 4269 | m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); |
8ded5a0f AM |
4270 | if (m == NULL) |
4271 | goto error_return; | |
4272 | m->next = NULL; | |
4273 | m->p_type = PT_TLS; | |
4274 | m->count = tls_count; | |
4275 | /* Mandated PF_R. */ | |
4276 | m->p_flags = PF_R; | |
4277 | m->p_flags_valid = 1; | |
d923cae0 | 4278 | s = first_tls; |
91d6fa6a | 4279 | for (i = 0; i < (unsigned int) tls_count; ++i) |
8ded5a0f | 4280 | { |
d923cae0 L |
4281 | if ((s->flags & SEC_THREAD_LOCAL) == 0) |
4282 | { | |
4283 | _bfd_error_handler | |
4284 | (_("%B: TLS sections are not adjacent:"), abfd); | |
4285 | s = first_tls; | |
4286 | i = 0; | |
4287 | while (i < (unsigned int) tls_count) | |
4288 | { | |
4289 | if ((s->flags & SEC_THREAD_LOCAL) != 0) | |
4290 | { | |
4291 | _bfd_error_handler (_(" TLS: %A"), s); | |
4292 | i++; | |
4293 | } | |
4294 | else | |
4295 | _bfd_error_handler (_(" non-TLS: %A"), s); | |
4296 | s = s->next; | |
4297 | } | |
4298 | bfd_set_error (bfd_error_bad_value); | |
4299 | goto error_return; | |
4300 | } | |
4301 | m->sections[i] = s; | |
4302 | s = s->next; | |
8ded5a0f | 4303 | } |
252b5132 | 4304 | |
8ded5a0f AM |
4305 | *pm = m; |
4306 | pm = &m->next; | |
4307 | } | |
252b5132 | 4308 | |
8ded5a0f AM |
4309 | /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME |
4310 | segment. */ | |
12bd6957 | 4311 | eh_frame_hdr = elf_eh_frame_hdr (abfd); |
8ded5a0f AM |
4312 | if (eh_frame_hdr != NULL |
4313 | && (eh_frame_hdr->output_section->flags & SEC_LOAD) != 0) | |
252b5132 | 4314 | { |
dc810e39 | 4315 | amt = sizeof (struct elf_segment_map); |
a50b1753 | 4316 | m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); |
252b5132 RH |
4317 | if (m == NULL) |
4318 | goto error_return; | |
4319 | m->next = NULL; | |
8ded5a0f | 4320 | m->p_type = PT_GNU_EH_FRAME; |
252b5132 | 4321 | m->count = 1; |
8ded5a0f | 4322 | m->sections[0] = eh_frame_hdr->output_section; |
252b5132 RH |
4323 | |
4324 | *pm = m; | |
4325 | pm = &m->next; | |
4326 | } | |
13ae64f3 | 4327 | |
12bd6957 | 4328 | if (elf_stack_flags (abfd)) |
13ae64f3 | 4329 | { |
8ded5a0f | 4330 | amt = sizeof (struct elf_segment_map); |
a50b1753 | 4331 | m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); |
8ded5a0f AM |
4332 | if (m == NULL) |
4333 | goto error_return; | |
4334 | m->next = NULL; | |
2b05f1b7 | 4335 | m->p_type = PT_GNU_STACK; |
12bd6957 | 4336 | m->p_flags = elf_stack_flags (abfd); |
04c3a755 | 4337 | m->p_align = bed->stack_align; |
8ded5a0f | 4338 | m->p_flags_valid = 1; |
04c3a755 NS |
4339 | m->p_align_valid = m->p_align != 0; |
4340 | if (info->stacksize > 0) | |
4341 | { | |
4342 | m->p_size = info->stacksize; | |
4343 | m->p_size_valid = 1; | |
4344 | } | |
252b5132 | 4345 | |
8ded5a0f AM |
4346 | *pm = m; |
4347 | pm = &m->next; | |
4348 | } | |
65765700 | 4349 | |
ceae84aa | 4350 | if (info != NULL && info->relro) |
8ded5a0f | 4351 | { |
f210dcff L |
4352 | for (m = mfirst; m != NULL; m = m->next) |
4353 | { | |
3832a4d8 AM |
4354 | if (m->p_type == PT_LOAD |
4355 | && m->count != 0 | |
4356 | && m->sections[0]->vma >= info->relro_start | |
4357 | && m->sections[0]->vma < info->relro_end) | |
f210dcff | 4358 | { |
3832a4d8 AM |
4359 | i = m->count; |
4360 | while (--i != (unsigned) -1) | |
4361 | if ((m->sections[i]->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) | |
4362 | == (SEC_LOAD | SEC_HAS_CONTENTS)) | |
4363 | break; | |
4364 | ||
43a8475c | 4365 | if (i != (unsigned) -1) |
f210dcff L |
4366 | break; |
4367 | } | |
be01b344 | 4368 | } |
f210dcff L |
4369 | |
4370 | /* Make a PT_GNU_RELRO segment only when it isn't empty. */ | |
4371 | if (m != NULL) | |
4372 | { | |
4373 | amt = sizeof (struct elf_segment_map); | |
a50b1753 | 4374 | m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); |
f210dcff L |
4375 | if (m == NULL) |
4376 | goto error_return; | |
4377 | m->next = NULL; | |
4378 | m->p_type = PT_GNU_RELRO; | |
4379 | m->p_flags = PF_R; | |
4380 | m->p_flags_valid = 1; | |
4381 | ||
4382 | *pm = m; | |
4383 | pm = &m->next; | |
4384 | } | |
8ded5a0f | 4385 | } |
9ee5e499 | 4386 | |
8ded5a0f | 4387 | free (sections); |
12bd6957 | 4388 | elf_seg_map (abfd) = mfirst; |
9ee5e499 JJ |
4389 | } |
4390 | ||
3dea8fca | 4391 | if (!elf_modify_segment_map (abfd, info, no_user_phdrs)) |
8ded5a0f | 4392 | return FALSE; |
8c37241b | 4393 | |
12bd6957 | 4394 | for (count = 0, m = elf_seg_map (abfd); m != NULL; m = m->next) |
8ded5a0f | 4395 | ++count; |
12bd6957 | 4396 | elf_program_header_size (abfd) = count * bed->s->sizeof_phdr; |
252b5132 | 4397 | |
b34976b6 | 4398 | return TRUE; |
252b5132 RH |
4399 | |
4400 | error_return: | |
4401 | if (sections != NULL) | |
4402 | free (sections); | |
b34976b6 | 4403 | return FALSE; |
252b5132 RH |
4404 | } |
4405 | ||
4406 | /* Sort sections by address. */ | |
4407 | ||
4408 | static int | |
217aa764 | 4409 | elf_sort_sections (const void *arg1, const void *arg2) |
252b5132 RH |
4410 | { |
4411 | const asection *sec1 = *(const asection **) arg1; | |
4412 | const asection *sec2 = *(const asection **) arg2; | |
eecdbe52 | 4413 | bfd_size_type size1, size2; |
252b5132 RH |
4414 | |
4415 | /* Sort by LMA first, since this is the address used to | |
4416 | place the section into a segment. */ | |
4417 | if (sec1->lma < sec2->lma) | |
4418 | return -1; | |
4419 | else if (sec1->lma > sec2->lma) | |
4420 | return 1; | |
4421 | ||
4422 | /* Then sort by VMA. Normally the LMA and the VMA will be | |
4423 | the same, and this will do nothing. */ | |
4424 | if (sec1->vma < sec2->vma) | |
4425 | return -1; | |
4426 | else if (sec1->vma > sec2->vma) | |
4427 | return 1; | |
4428 | ||
4429 | /* Put !SEC_LOAD sections after SEC_LOAD ones. */ | |
4430 | ||
07c6e936 | 4431 | #define TOEND(x) (((x)->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0) |
252b5132 RH |
4432 | |
4433 | if (TOEND (sec1)) | |
4434 | { | |
4435 | if (TOEND (sec2)) | |
00a7cdc5 NC |
4436 | { |
4437 | /* If the indicies are the same, do not return 0 | |
4438 | here, but continue to try the next comparison. */ | |
4439 | if (sec1->target_index - sec2->target_index != 0) | |
4440 | return sec1->target_index - sec2->target_index; | |
4441 | } | |
252b5132 RH |
4442 | else |
4443 | return 1; | |
4444 | } | |
00a7cdc5 | 4445 | else if (TOEND (sec2)) |
252b5132 RH |
4446 | return -1; |
4447 | ||
4448 | #undef TOEND | |
4449 | ||
00a7cdc5 NC |
4450 | /* Sort by size, to put zero sized sections |
4451 | before others at the same address. */ | |
252b5132 | 4452 | |
eea6121a AM |
4453 | size1 = (sec1->flags & SEC_LOAD) ? sec1->size : 0; |
4454 | size2 = (sec2->flags & SEC_LOAD) ? sec2->size : 0; | |
eecdbe52 JJ |
4455 | |
4456 | if (size1 < size2) | |
252b5132 | 4457 | return -1; |
eecdbe52 | 4458 | if (size1 > size2) |
252b5132 RH |
4459 | return 1; |
4460 | ||
4461 | return sec1->target_index - sec2->target_index; | |
4462 | } | |
4463 | ||
340b6d91 AC |
4464 | /* Ian Lance Taylor writes: |
4465 | ||
4466 | We shouldn't be using % with a negative signed number. That's just | |
4467 | not good. We have to make sure either that the number is not | |
4468 | negative, or that the number has an unsigned type. When the types | |
4469 | are all the same size they wind up as unsigned. When file_ptr is a | |
4470 | larger signed type, the arithmetic winds up as signed long long, | |
4471 | which is wrong. | |
4472 | ||
4473 | What we're trying to say here is something like ``increase OFF by | |
4474 | the least amount that will cause it to be equal to the VMA modulo | |
4475 | the page size.'' */ | |
4476 | /* In other words, something like: | |
4477 | ||
4478 | vma_offset = m->sections[0]->vma % bed->maxpagesize; | |
4479 | off_offset = off % bed->maxpagesize; | |
4480 | if (vma_offset < off_offset) | |
4481 | adjustment = vma_offset + bed->maxpagesize - off_offset; | |
4482 | else | |
4483 | adjustment = vma_offset - off_offset; | |
08a40648 | 4484 | |
340b6d91 AC |
4485 | which can can be collapsed into the expression below. */ |
4486 | ||
4487 | static file_ptr | |
4488 | vma_page_aligned_bias (bfd_vma vma, ufile_ptr off, bfd_vma maxpagesize) | |
4489 | { | |
dc9155b2 NC |
4490 | /* PR binutils/16199: Handle an alignment of zero. */ |
4491 | if (maxpagesize == 0) | |
4492 | maxpagesize = 1; | |
340b6d91 AC |
4493 | return ((vma - off) % maxpagesize); |
4494 | } | |
4495 | ||
6d33f217 L |
4496 | static void |
4497 | print_segment_map (const struct elf_segment_map *m) | |
4498 | { | |
4499 | unsigned int j; | |
4500 | const char *pt = get_segment_type (m->p_type); | |
4501 | char buf[32]; | |
4502 | ||
4503 | if (pt == NULL) | |
4504 | { | |
4505 | if (m->p_type >= PT_LOPROC && m->p_type <= PT_HIPROC) | |
4506 | sprintf (buf, "LOPROC+%7.7x", | |
4507 | (unsigned int) (m->p_type - PT_LOPROC)); | |
4508 | else if (m->p_type >= PT_LOOS && m->p_type <= PT_HIOS) | |
4509 | sprintf (buf, "LOOS+%7.7x", | |
4510 | (unsigned int) (m->p_type - PT_LOOS)); | |
4511 | else | |
4512 | snprintf (buf, sizeof (buf), "%8.8x", | |
4513 | (unsigned int) m->p_type); | |
4514 | pt = buf; | |
4515 | } | |
4a97a0e5 | 4516 | fflush (stdout); |
6d33f217 L |
4517 | fprintf (stderr, "%s:", pt); |
4518 | for (j = 0; j < m->count; j++) | |
4519 | fprintf (stderr, " %s", m->sections [j]->name); | |
4520 | putc ('\n',stderr); | |
4a97a0e5 | 4521 | fflush (stderr); |
6d33f217 L |
4522 | } |
4523 | ||
32812159 AM |
4524 | static bfd_boolean |
4525 | write_zeros (bfd *abfd, file_ptr pos, bfd_size_type len) | |
4526 | { | |
4527 | void *buf; | |
4528 | bfd_boolean ret; | |
4529 | ||
4530 | if (bfd_seek (abfd, pos, SEEK_SET) != 0) | |
4531 | return FALSE; | |
4532 | buf = bfd_zmalloc (len); | |
4533 | if (buf == NULL) | |
4534 | return FALSE; | |
4535 | ret = bfd_bwrite (buf, len, abfd) == len; | |
4536 | free (buf); | |
4537 | return ret; | |
4538 | } | |
4539 | ||
252b5132 RH |
4540 | /* Assign file positions to the sections based on the mapping from |
4541 | sections to segments. This function also sets up some fields in | |
f3520d2f | 4542 | the file header. */ |
252b5132 | 4543 | |
b34976b6 | 4544 | static bfd_boolean |
f3520d2f AM |
4545 | assign_file_positions_for_load_sections (bfd *abfd, |
4546 | struct bfd_link_info *link_info) | |
252b5132 RH |
4547 | { |
4548 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
252b5132 | 4549 | struct elf_segment_map *m; |
252b5132 | 4550 | Elf_Internal_Phdr *phdrs; |
252b5132 | 4551 | Elf_Internal_Phdr *p; |
02bf8d82 | 4552 | file_ptr off; |
3f570048 | 4553 | bfd_size_type maxpagesize; |
f3520d2f | 4554 | unsigned int alloc; |
0920dee7 | 4555 | unsigned int i, j; |
2b0bc088 | 4556 | bfd_vma header_pad = 0; |
252b5132 | 4557 | |
e36284ab | 4558 | if (link_info == NULL |
ceae84aa | 4559 | && !_bfd_elf_map_sections_to_segments (abfd, link_info)) |
8ded5a0f | 4560 | return FALSE; |
252b5132 | 4561 | |
8ded5a0f | 4562 | alloc = 0; |
12bd6957 | 4563 | for (m = elf_seg_map (abfd); m != NULL; m = m->next) |
2b0bc088 NC |
4564 | { |
4565 | ++alloc; | |
4566 | if (m->header_size) | |
4567 | header_pad = m->header_size; | |
4568 | } | |
252b5132 | 4569 | |
82f2dbf7 NC |
4570 | if (alloc) |
4571 | { | |
4572 | elf_elfheader (abfd)->e_phoff = bed->s->sizeof_ehdr; | |
4573 | elf_elfheader (abfd)->e_phentsize = bed->s->sizeof_phdr; | |
4574 | } | |
4575 | else | |
4576 | { | |
4577 | /* PR binutils/12467. */ | |
4578 | elf_elfheader (abfd)->e_phoff = 0; | |
4579 | elf_elfheader (abfd)->e_phentsize = 0; | |
4580 | } | |
d324f6d6 | 4581 | |
8ded5a0f | 4582 | elf_elfheader (abfd)->e_phnum = alloc; |
252b5132 | 4583 | |
12bd6957 AM |
4584 | if (elf_program_header_size (abfd) == (bfd_size_type) -1) |
4585 | elf_program_header_size (abfd) = alloc * bed->s->sizeof_phdr; | |
8ded5a0f | 4586 | else |
12bd6957 | 4587 | BFD_ASSERT (elf_program_header_size (abfd) |
59e0647f | 4588 | >= alloc * bed->s->sizeof_phdr); |
252b5132 RH |
4589 | |
4590 | if (alloc == 0) | |
f3520d2f | 4591 | { |
12bd6957 | 4592 | elf_next_file_pos (abfd) = bed->s->sizeof_ehdr; |
8ded5a0f | 4593 | return TRUE; |
f3520d2f | 4594 | } |
252b5132 | 4595 | |
12bd6957 | 4596 | /* We're writing the size in elf_program_header_size (abfd), |
57268894 HPN |
4597 | see assign_file_positions_except_relocs, so make sure we have |
4598 | that amount allocated, with trailing space cleared. | |
12bd6957 AM |
4599 | The variable alloc contains the computed need, while |
4600 | elf_program_header_size (abfd) contains the size used for the | |
57268894 HPN |
4601 | layout. |
4602 | See ld/emultempl/elf-generic.em:gld${EMULATION_NAME}_map_segments | |
4603 | where the layout is forced to according to a larger size in the | |
4604 | last iterations for the testcase ld-elf/header. */ | |
12bd6957 | 4605 | BFD_ASSERT (elf_program_header_size (abfd) % bed->s->sizeof_phdr |
57268894 | 4606 | == 0); |
a50b1753 NC |
4607 | phdrs = (Elf_Internal_Phdr *) |
4608 | bfd_zalloc2 (abfd, | |
12bd6957 | 4609 | (elf_program_header_size (abfd) / bed->s->sizeof_phdr), |
a50b1753 | 4610 | sizeof (Elf_Internal_Phdr)); |
f3520d2f | 4611 | elf_tdata (abfd)->phdr = phdrs; |
252b5132 | 4612 | if (phdrs == NULL) |
b34976b6 | 4613 | return FALSE; |
252b5132 | 4614 | |
3f570048 AM |
4615 | maxpagesize = 1; |
4616 | if ((abfd->flags & D_PAGED) != 0) | |
4617 | maxpagesize = bed->maxpagesize; | |
4618 | ||
252b5132 RH |
4619 | off = bed->s->sizeof_ehdr; |
4620 | off += alloc * bed->s->sizeof_phdr; | |
2b0bc088 NC |
4621 | if (header_pad < (bfd_vma) off) |
4622 | header_pad = 0; | |
4623 | else | |
4624 | header_pad -= off; | |
4625 | off += header_pad; | |
252b5132 | 4626 | |
12bd6957 | 4627 | for (m = elf_seg_map (abfd), p = phdrs, j = 0; |
252b5132 | 4628 | m != NULL; |
0920dee7 | 4629 | m = m->next, p++, j++) |
252b5132 | 4630 | { |
252b5132 | 4631 | asection **secpp; |
bf988460 AM |
4632 | bfd_vma off_adjust; |
4633 | bfd_boolean no_contents; | |
252b5132 RH |
4634 | |
4635 | /* If elf_segment_map is not from map_sections_to_segments, the | |
08a40648 | 4636 | sections may not be correctly ordered. NOTE: sorting should |
52e9b619 MS |
4637 | not be done to the PT_NOTE section of a corefile, which may |
4638 | contain several pseudo-sections artificially created by bfd. | |
4639 | Sorting these pseudo-sections breaks things badly. */ | |
47d9a591 AM |
4640 | if (m->count > 1 |
4641 | && !(elf_elfheader (abfd)->e_type == ET_CORE | |
52e9b619 | 4642 | && m->p_type == PT_NOTE)) |
252b5132 RH |
4643 | qsort (m->sections, (size_t) m->count, sizeof (asection *), |
4644 | elf_sort_sections); | |
4645 | ||
b301b248 AM |
4646 | /* An ELF segment (described by Elf_Internal_Phdr) may contain a |
4647 | number of sections with contents contributing to both p_filesz | |
4648 | and p_memsz, followed by a number of sections with no contents | |
4649 | that just contribute to p_memsz. In this loop, OFF tracks next | |
02bf8d82 | 4650 | available file offset for PT_LOAD and PT_NOTE segments. */ |
252b5132 | 4651 | p->p_type = m->p_type; |
28a7f3e7 | 4652 | p->p_flags = m->p_flags; |
252b5132 | 4653 | |
3f570048 AM |
4654 | if (m->count == 0) |
4655 | p->p_vaddr = 0; | |
4656 | else | |
3271a814 | 4657 | p->p_vaddr = m->sections[0]->vma - m->p_vaddr_offset; |
3f570048 AM |
4658 | |
4659 | if (m->p_paddr_valid) | |
4660 | p->p_paddr = m->p_paddr; | |
4661 | else if (m->count == 0) | |
4662 | p->p_paddr = 0; | |
4663 | else | |
08a40648 | 4664 | p->p_paddr = m->sections[0]->lma - m->p_vaddr_offset; |
3f570048 AM |
4665 | |
4666 | if (p->p_type == PT_LOAD | |
4667 | && (abfd->flags & D_PAGED) != 0) | |
4668 | { | |
4669 | /* p_align in demand paged PT_LOAD segments effectively stores | |
4670 | the maximum page size. When copying an executable with | |
4671 | objcopy, we set m->p_align from the input file. Use this | |
4672 | value for maxpagesize rather than bed->maxpagesize, which | |
4673 | may be different. Note that we use maxpagesize for PT_TLS | |
4674 | segment alignment later in this function, so we are relying | |
4675 | on at least one PT_LOAD segment appearing before a PT_TLS | |
4676 | segment. */ | |
4677 | if (m->p_align_valid) | |
4678 | maxpagesize = m->p_align; | |
4679 | ||
4680 | p->p_align = maxpagesize; | |
4681 | } | |
3271a814 NS |
4682 | else if (m->p_align_valid) |
4683 | p->p_align = m->p_align; | |
e970b90a DJ |
4684 | else if (m->count == 0) |
4685 | p->p_align = 1 << bed->s->log_file_align; | |
3f570048 AM |
4686 | else |
4687 | p->p_align = 0; | |
4688 | ||
bf988460 AM |
4689 | no_contents = FALSE; |
4690 | off_adjust = 0; | |
252b5132 | 4691 | if (p->p_type == PT_LOAD |
b301b248 | 4692 | && m->count > 0) |
252b5132 | 4693 | { |
b301b248 | 4694 | bfd_size_type align; |
a49e53ed | 4695 | unsigned int align_power = 0; |
b301b248 | 4696 | |
3271a814 NS |
4697 | if (m->p_align_valid) |
4698 | align = p->p_align; | |
4699 | else | |
252b5132 | 4700 | { |
3271a814 NS |
4701 | for (i = 0, secpp = m->sections; i < m->count; i++, secpp++) |
4702 | { | |
4703 | unsigned int secalign; | |
08a40648 | 4704 | |
3271a814 NS |
4705 | secalign = bfd_get_section_alignment (abfd, *secpp); |
4706 | if (secalign > align_power) | |
4707 | align_power = secalign; | |
4708 | } | |
4709 | align = (bfd_size_type) 1 << align_power; | |
4710 | if (align < maxpagesize) | |
4711 | align = maxpagesize; | |
b301b248 | 4712 | } |
252b5132 | 4713 | |
02bf8d82 AM |
4714 | for (i = 0; i < m->count; i++) |
4715 | if ((m->sections[i]->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0) | |
4716 | /* If we aren't making room for this section, then | |
4717 | it must be SHT_NOBITS regardless of what we've | |
4718 | set via struct bfd_elf_special_section. */ | |
4719 | elf_section_type (m->sections[i]) = SHT_NOBITS; | |
4720 | ||
bf988460 | 4721 | /* Find out whether this segment contains any loadable |
aea274d3 AM |
4722 | sections. */ |
4723 | no_contents = TRUE; | |
4724 | for (i = 0; i < m->count; i++) | |
4725 | if (elf_section_type (m->sections[i]) != SHT_NOBITS) | |
4726 | { | |
4727 | no_contents = FALSE; | |
4728 | break; | |
4729 | } | |
bf988460 | 4730 | |
85cfcbfb | 4731 | off_adjust = vma_page_aligned_bias (p->p_vaddr, off, align); |
bf988460 AM |
4732 | off += off_adjust; |
4733 | if (no_contents) | |
4734 | { | |
4735 | /* We shouldn't need to align the segment on disk since | |
4736 | the segment doesn't need file space, but the gABI | |
4737 | arguably requires the alignment and glibc ld.so | |
4738 | checks it. So to comply with the alignment | |
4739 | requirement but not waste file space, we adjust | |
4740 | p_offset for just this segment. (OFF_ADJUST is | |
4741 | subtracted from OFF later.) This may put p_offset | |
4742 | past the end of file, but that shouldn't matter. */ | |
4743 | } | |
4744 | else | |
4745 | off_adjust = 0; | |
252b5132 | 4746 | } |
b1a6d0b1 NC |
4747 | /* Make sure the .dynamic section is the first section in the |
4748 | PT_DYNAMIC segment. */ | |
4749 | else if (p->p_type == PT_DYNAMIC | |
4750 | && m->count > 1 | |
4751 | && strcmp (m->sections[0]->name, ".dynamic") != 0) | |
4752 | { | |
4753 | _bfd_error_handler | |
b301b248 AM |
4754 | (_("%B: The first section in the PT_DYNAMIC segment is not the .dynamic section"), |
4755 | abfd); | |
b1a6d0b1 NC |
4756 | bfd_set_error (bfd_error_bad_value); |
4757 | return FALSE; | |
4758 | } | |
3f001e84 JK |
4759 | /* Set the note section type to SHT_NOTE. */ |
4760 | else if (p->p_type == PT_NOTE) | |
4761 | for (i = 0; i < m->count; i++) | |
4762 | elf_section_type (m->sections[i]) = SHT_NOTE; | |
252b5132 | 4763 | |
252b5132 RH |
4764 | p->p_offset = 0; |
4765 | p->p_filesz = 0; | |
4766 | p->p_memsz = 0; | |
4767 | ||
4768 | if (m->includes_filehdr) | |
4769 | { | |
bf988460 | 4770 | if (!m->p_flags_valid) |
252b5132 | 4771 | p->p_flags |= PF_R; |
252b5132 RH |
4772 | p->p_filesz = bed->s->sizeof_ehdr; |
4773 | p->p_memsz = bed->s->sizeof_ehdr; | |
4774 | if (m->count > 0) | |
4775 | { | |
252b5132 RH |
4776 | if (p->p_vaddr < (bfd_vma) off) |
4777 | { | |
caf47ea6 | 4778 | (*_bfd_error_handler) |
b301b248 AM |
4779 | (_("%B: Not enough room for program headers, try linking with -N"), |
4780 | abfd); | |
252b5132 | 4781 | bfd_set_error (bfd_error_bad_value); |
b34976b6 | 4782 | return FALSE; |
252b5132 RH |
4783 | } |
4784 | ||
4785 | p->p_vaddr -= off; | |
bf988460 | 4786 | if (!m->p_paddr_valid) |
252b5132 RH |
4787 | p->p_paddr -= off; |
4788 | } | |
252b5132 RH |
4789 | } |
4790 | ||
4791 | if (m->includes_phdrs) | |
4792 | { | |
bf988460 | 4793 | if (!m->p_flags_valid) |
252b5132 RH |
4794 | p->p_flags |= PF_R; |
4795 | ||
f3520d2f | 4796 | if (!m->includes_filehdr) |
252b5132 RH |
4797 | { |
4798 | p->p_offset = bed->s->sizeof_ehdr; | |
4799 | ||
4800 | if (m->count > 0) | |
4801 | { | |
252b5132 | 4802 | p->p_vaddr -= off - p->p_offset; |
bf988460 | 4803 | if (!m->p_paddr_valid) |
252b5132 RH |
4804 | p->p_paddr -= off - p->p_offset; |
4805 | } | |
252b5132 RH |
4806 | } |
4807 | ||
4808 | p->p_filesz += alloc * bed->s->sizeof_phdr; | |
4809 | p->p_memsz += alloc * bed->s->sizeof_phdr; | |
2b0bc088 NC |
4810 | if (m->count) |
4811 | { | |
4812 | p->p_filesz += header_pad; | |
4813 | p->p_memsz += header_pad; | |
4814 | } | |
252b5132 RH |
4815 | } |
4816 | ||
4817 | if (p->p_type == PT_LOAD | |
4818 | || (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core)) | |
4819 | { | |
bf988460 | 4820 | if (!m->includes_filehdr && !m->includes_phdrs) |
02bf8d82 | 4821 | p->p_offset = off; |
252b5132 RH |
4822 | else |
4823 | { | |
4824 | file_ptr adjust; | |
4825 | ||
4826 | adjust = off - (p->p_offset + p->p_filesz); | |
bf988460 AM |
4827 | if (!no_contents) |
4828 | p->p_filesz += adjust; | |
252b5132 RH |
4829 | p->p_memsz += adjust; |
4830 | } | |
4831 | } | |
4832 | ||
1ea63fd2 AM |
4833 | /* Set up p_filesz, p_memsz, p_align and p_flags from the section |
4834 | maps. Set filepos for sections in PT_LOAD segments, and in | |
4835 | core files, for sections in PT_NOTE segments. | |
4836 | assign_file_positions_for_non_load_sections will set filepos | |
4837 | for other sections and update p_filesz for other segments. */ | |
252b5132 RH |
4838 | for (i = 0, secpp = m->sections; i < m->count; i++, secpp++) |
4839 | { | |
4840 | asection *sec; | |
252b5132 | 4841 | bfd_size_type align; |
627b32bc | 4842 | Elf_Internal_Shdr *this_hdr; |
252b5132 RH |
4843 | |
4844 | sec = *secpp; | |
02bf8d82 | 4845 | this_hdr = &elf_section_data (sec)->this_hdr; |
3f570048 | 4846 | align = (bfd_size_type) 1 << bfd_get_section_alignment (abfd, sec); |
252b5132 | 4847 | |
88967714 AM |
4848 | if ((p->p_type == PT_LOAD |
4849 | || p->p_type == PT_TLS) | |
4850 | && (this_hdr->sh_type != SHT_NOBITS | |
4851 | || ((this_hdr->sh_flags & SHF_ALLOC) != 0 | |
4852 | && ((this_hdr->sh_flags & SHF_TLS) == 0 | |
4853 | || p->p_type == PT_TLS)))) | |
252b5132 | 4854 | { |
b5599592 AM |
4855 | bfd_vma p_start = p->p_paddr; |
4856 | bfd_vma p_end = p_start + p->p_memsz; | |
4857 | bfd_vma s_start = sec->lma; | |
4858 | bfd_vma adjust = s_start - p_end; | |
252b5132 | 4859 | |
a2d1e028 L |
4860 | if (adjust != 0 |
4861 | && (s_start < p_end | |
4862 | || p_end < p_start)) | |
252b5132 | 4863 | { |
88967714 | 4864 | (*_bfd_error_handler) |
b5599592 AM |
4865 | (_("%B: section %A lma %#lx adjusted to %#lx"), abfd, sec, |
4866 | (unsigned long) s_start, (unsigned long) p_end); | |
88967714 | 4867 | adjust = 0; |
b5599592 | 4868 | sec->lma = p_end; |
1cfb7d1e | 4869 | } |
3ac9b6c9 | 4870 | p->p_memsz += adjust; |
1cfb7d1e | 4871 | |
88967714 AM |
4872 | if (this_hdr->sh_type != SHT_NOBITS) |
4873 | { | |
32812159 AM |
4874 | if (p->p_filesz + adjust < p->p_memsz) |
4875 | { | |
4876 | /* We have a PROGBITS section following NOBITS ones. | |
4877 | Allocate file space for the NOBITS section(s) and | |
4878 | zero it. */ | |
4879 | adjust = p->p_memsz - p->p_filesz; | |
4880 | if (!write_zeros (abfd, off, adjust)) | |
4881 | return FALSE; | |
4882 | } | |
88967714 AM |
4883 | off += adjust; |
4884 | p->p_filesz += adjust; | |
252b5132 | 4885 | } |
252b5132 RH |
4886 | } |
4887 | ||
4888 | if (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core) | |
4889 | { | |
b301b248 AM |
4890 | /* The section at i == 0 is the one that actually contains |
4891 | everything. */ | |
4a938328 MS |
4892 | if (i == 0) |
4893 | { | |
627b32bc | 4894 | this_hdr->sh_offset = sec->filepos = off; |
6a3cd2b4 AM |
4895 | off += this_hdr->sh_size; |
4896 | p->p_filesz = this_hdr->sh_size; | |
b301b248 AM |
4897 | p->p_memsz = 0; |
4898 | p->p_align = 1; | |
252b5132 | 4899 | } |
4a938328 | 4900 | else |
252b5132 | 4901 | { |
b301b248 | 4902 | /* The rest are fake sections that shouldn't be written. */ |
252b5132 | 4903 | sec->filepos = 0; |
eea6121a | 4904 | sec->size = 0; |
b301b248 AM |
4905 | sec->flags = 0; |
4906 | continue; | |
252b5132 | 4907 | } |
252b5132 RH |
4908 | } |
4909 | else | |
4910 | { | |
1e951488 | 4911 | if (p->p_type == PT_LOAD) |
b301b248 | 4912 | { |
1e951488 AM |
4913 | this_hdr->sh_offset = sec->filepos = off; |
4914 | if (this_hdr->sh_type != SHT_NOBITS) | |
4915 | off += this_hdr->sh_size; | |
4916 | } | |
4917 | else if (this_hdr->sh_type == SHT_NOBITS | |
4918 | && (this_hdr->sh_flags & SHF_TLS) != 0 | |
4919 | && this_hdr->sh_offset == 0) | |
4920 | { | |
4921 | /* This is a .tbss section that didn't get a PT_LOAD. | |
4922 | (See _bfd_elf_map_sections_to_segments "Create a | |
4923 | final PT_LOAD".) Set sh_offset to the value it | |
4924 | would have if we had created a zero p_filesz and | |
4925 | p_memsz PT_LOAD header for the section. This | |
4926 | also makes the PT_TLS header have the same | |
4927 | p_offset value. */ | |
4928 | bfd_vma adjust = vma_page_aligned_bias (this_hdr->sh_addr, | |
4929 | off, align); | |
4930 | this_hdr->sh_offset = sec->filepos = off + adjust; | |
b301b248 | 4931 | } |
252b5132 | 4932 | |
02bf8d82 | 4933 | if (this_hdr->sh_type != SHT_NOBITS) |
b301b248 | 4934 | { |
6a3cd2b4 | 4935 | p->p_filesz += this_hdr->sh_size; |
02bf8d82 AM |
4936 | /* A load section without SHF_ALLOC is something like |
4937 | a note section in a PT_NOTE segment. These take | |
4938 | file space but are not loaded into memory. */ | |
4939 | if ((this_hdr->sh_flags & SHF_ALLOC) != 0) | |
6a3cd2b4 | 4940 | p->p_memsz += this_hdr->sh_size; |
b301b248 | 4941 | } |
6a3cd2b4 | 4942 | else if ((this_hdr->sh_flags & SHF_ALLOC) != 0) |
13ae64f3 | 4943 | { |
6a3cd2b4 AM |
4944 | if (p->p_type == PT_TLS) |
4945 | p->p_memsz += this_hdr->sh_size; | |
4946 | ||
4947 | /* .tbss is special. It doesn't contribute to p_memsz of | |
4948 | normal segments. */ | |
4949 | else if ((this_hdr->sh_flags & SHF_TLS) == 0) | |
4950 | p->p_memsz += this_hdr->sh_size; | |
13ae64f3 JJ |
4951 | } |
4952 | ||
b10a8ae0 L |
4953 | if (align > p->p_align |
4954 | && !m->p_align_valid | |
4955 | && (p->p_type != PT_LOAD | |
4956 | || (abfd->flags & D_PAGED) == 0)) | |
252b5132 RH |
4957 | p->p_align = align; |
4958 | } | |
4959 | ||
bf988460 | 4960 | if (!m->p_flags_valid) |
252b5132 RH |
4961 | { |
4962 | p->p_flags |= PF_R; | |
02bf8d82 | 4963 | if ((this_hdr->sh_flags & SHF_EXECINSTR) != 0) |
252b5132 | 4964 | p->p_flags |= PF_X; |
02bf8d82 | 4965 | if ((this_hdr->sh_flags & SHF_WRITE) != 0) |
252b5132 RH |
4966 | p->p_flags |= PF_W; |
4967 | } | |
4968 | } | |
43a8475c | 4969 | |
bf988460 | 4970 | off -= off_adjust; |
0920dee7 | 4971 | |
7c928300 AM |
4972 | /* Check that all sections are in a PT_LOAD segment. |
4973 | Don't check funky gdb generated core files. */ | |
4974 | if (p->p_type == PT_LOAD && bfd_get_format (abfd) != bfd_core) | |
9a83a553 AM |
4975 | { |
4976 | bfd_boolean check_vma = TRUE; | |
4977 | ||
4978 | for (i = 1; i < m->count; i++) | |
4979 | if (m->sections[i]->vma == m->sections[i - 1]->vma | |
4980 | && ELF_SECTION_SIZE (&(elf_section_data (m->sections[i]) | |
4981 | ->this_hdr), p) != 0 | |
4982 | && ELF_SECTION_SIZE (&(elf_section_data (m->sections[i - 1]) | |
4983 | ->this_hdr), p) != 0) | |
0920dee7 | 4984 | { |
9a83a553 AM |
4985 | /* Looks like we have overlays packed into the segment. */ |
4986 | check_vma = FALSE; | |
4987 | break; | |
0920dee7 | 4988 | } |
9a83a553 AM |
4989 | |
4990 | for (i = 0; i < m->count; i++) | |
4991 | { | |
4992 | Elf_Internal_Shdr *this_hdr; | |
4993 | asection *sec; | |
4994 | ||
4995 | sec = m->sections[i]; | |
4996 | this_hdr = &(elf_section_data(sec)->this_hdr); | |
86b2281f AM |
4997 | if (!ELF_SECTION_IN_SEGMENT_1 (this_hdr, p, check_vma, 0) |
4998 | && !ELF_TBSS_SPECIAL (this_hdr, p)) | |
9a83a553 AM |
4999 | { |
5000 | (*_bfd_error_handler) | |
5001 | (_("%B: section `%A' can't be allocated in segment %d"), | |
5002 | abfd, sec, j); | |
5003 | print_segment_map (m); | |
5004 | } | |
5005 | } | |
5006 | } | |
252b5132 RH |
5007 | } |
5008 | ||
12bd6957 | 5009 | elf_next_file_pos (abfd) = off; |
f3520d2f AM |
5010 | return TRUE; |
5011 | } | |
5012 | ||
5013 | /* Assign file positions for the other sections. */ | |
5014 | ||
5015 | static bfd_boolean | |
5016 | assign_file_positions_for_non_load_sections (bfd *abfd, | |
5017 | struct bfd_link_info *link_info) | |
5018 | { | |
5019 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
5020 | Elf_Internal_Shdr **i_shdrpp; | |
5021 | Elf_Internal_Shdr **hdrpp; | |
5022 | Elf_Internal_Phdr *phdrs; | |
5023 | Elf_Internal_Phdr *p; | |
5024 | struct elf_segment_map *m; | |
62655c7b | 5025 | struct elf_segment_map *hdrs_segment; |
f3520d2f AM |
5026 | bfd_vma filehdr_vaddr, filehdr_paddr; |
5027 | bfd_vma phdrs_vaddr, phdrs_paddr; | |
5028 | file_ptr off; | |
5029 | unsigned int num_sec; | |
5030 | unsigned int i; | |
5031 | unsigned int count; | |
5032 | ||
5c182d5f AM |
5033 | i_shdrpp = elf_elfsections (abfd); |
5034 | num_sec = elf_numsections (abfd); | |
12bd6957 | 5035 | off = elf_next_file_pos (abfd); |
5c182d5f AM |
5036 | for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++) |
5037 | { | |
5c182d5f AM |
5038 | Elf_Internal_Shdr *hdr; |
5039 | ||
5040 | hdr = *hdrpp; | |
5041 | if (hdr->bfd_section != NULL | |
252e386e AM |
5042 | && (hdr->bfd_section->filepos != 0 |
5043 | || (hdr->sh_type == SHT_NOBITS | |
5044 | && hdr->contents == NULL))) | |
627b32bc | 5045 | BFD_ASSERT (hdr->sh_offset == hdr->bfd_section->filepos); |
5c182d5f AM |
5046 | else if ((hdr->sh_flags & SHF_ALLOC) != 0) |
5047 | { | |
e8d2ba53 AM |
5048 | if (hdr->sh_size != 0) |
5049 | (*_bfd_error_handler) | |
5050 | (_("%B: warning: allocated section `%s' not in segment"), | |
5051 | abfd, | |
5052 | (hdr->bfd_section == NULL | |
5053 | ? "*unknown*" | |
5054 | : hdr->bfd_section->name)); | |
3ba71138 L |
5055 | /* We don't need to page align empty sections. */ |
5056 | if ((abfd->flags & D_PAGED) != 0 && hdr->sh_size != 0) | |
5c182d5f AM |
5057 | off += vma_page_aligned_bias (hdr->sh_addr, off, |
5058 | bed->maxpagesize); | |
5059 | else | |
5060 | off += vma_page_aligned_bias (hdr->sh_addr, off, | |
5061 | hdr->sh_addralign); | |
5062 | off = _bfd_elf_assign_file_position_for_section (hdr, off, | |
5063 | FALSE); | |
5064 | } | |
5065 | else if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA) | |
5066 | && hdr->bfd_section == NULL) | |
12bd6957 AM |
5067 | || hdr == i_shdrpp[elf_onesymtab (abfd)] |
5068 | || hdr == i_shdrpp[elf_symtab_shndx (abfd)] | |
5069 | || hdr == i_shdrpp[elf_strtab_sec (abfd)]) | |
5c182d5f AM |
5070 | hdr->sh_offset = -1; |
5071 | else | |
5072 | off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE); | |
5c182d5f AM |
5073 | } |
5074 | ||
252b5132 RH |
5075 | /* Now that we have set the section file positions, we can set up |
5076 | the file positions for the non PT_LOAD segments. */ | |
f3520d2f AM |
5077 | count = 0; |
5078 | filehdr_vaddr = 0; | |
5079 | filehdr_paddr = 0; | |
5080 | phdrs_vaddr = bed->maxpagesize + bed->s->sizeof_ehdr; | |
5081 | phdrs_paddr = 0; | |
62655c7b | 5082 | hdrs_segment = NULL; |
f3520d2f | 5083 | phdrs = elf_tdata (abfd)->phdr; |
12bd6957 | 5084 | for (m = elf_seg_map (abfd), p = phdrs; m != NULL; m = m->next, p++) |
f3520d2f AM |
5085 | { |
5086 | ++count; | |
5087 | if (p->p_type != PT_LOAD) | |
5088 | continue; | |
5089 | ||
5090 | if (m->includes_filehdr) | |
5091 | { | |
5092 | filehdr_vaddr = p->p_vaddr; | |
5093 | filehdr_paddr = p->p_paddr; | |
5094 | } | |
5095 | if (m->includes_phdrs) | |
5096 | { | |
5097 | phdrs_vaddr = p->p_vaddr; | |
5098 | phdrs_paddr = p->p_paddr; | |
5099 | if (m->includes_filehdr) | |
5100 | { | |
62655c7b | 5101 | hdrs_segment = m; |
f3520d2f AM |
5102 | phdrs_vaddr += bed->s->sizeof_ehdr; |
5103 | phdrs_paddr += bed->s->sizeof_ehdr; | |
5104 | } | |
5105 | } | |
5106 | } | |
5107 | ||
62655c7b RM |
5108 | if (hdrs_segment != NULL && link_info != NULL) |
5109 | { | |
5110 | /* There is a segment that contains both the file headers and the | |
5111 | program headers, so provide a symbol __ehdr_start pointing there. | |
5112 | A program can use this to examine itself robustly. */ | |
5113 | ||
5114 | struct elf_link_hash_entry *hash | |
5115 | = elf_link_hash_lookup (elf_hash_table (link_info), "__ehdr_start", | |
5116 | FALSE, FALSE, TRUE); | |
5117 | /* If the symbol was referenced and not defined, define it. */ | |
5118 | if (hash != NULL | |
5119 | && (hash->root.type == bfd_link_hash_new | |
5120 | || hash->root.type == bfd_link_hash_undefined | |
5121 | || hash->root.type == bfd_link_hash_undefweak | |
5122 | || hash->root.type == bfd_link_hash_common)) | |
5123 | { | |
5124 | asection *s = NULL; | |
5125 | if (hdrs_segment->count != 0) | |
5126 | /* The segment contains sections, so use the first one. */ | |
5127 | s = hdrs_segment->sections[0]; | |
5128 | else | |
5129 | /* Use the first (i.e. lowest-addressed) section in any segment. */ | |
12bd6957 | 5130 | for (m = elf_seg_map (abfd); m != NULL; m = m->next) |
62655c7b RM |
5131 | if (m->count != 0) |
5132 | { | |
5133 | s = m->sections[0]; | |
5134 | break; | |
5135 | } | |
5136 | ||
5137 | if (s != NULL) | |
5138 | { | |
5139 | hash->root.u.def.value = filehdr_vaddr - s->vma; | |
5140 | hash->root.u.def.section = s; | |
5141 | } | |
5142 | else | |
5143 | { | |
5144 | hash->root.u.def.value = filehdr_vaddr; | |
5145 | hash->root.u.def.section = bfd_abs_section_ptr; | |
5146 | } | |
5147 | ||
5148 | hash->root.type = bfd_link_hash_defined; | |
5149 | hash->def_regular = 1; | |
5150 | hash->non_elf = 0; | |
5151 | } | |
5152 | } | |
5153 | ||
12bd6957 | 5154 | for (m = elf_seg_map (abfd), p = phdrs; m != NULL; m = m->next, p++) |
252b5132 | 5155 | { |
129af99f | 5156 | if (p->p_type == PT_GNU_RELRO) |
252b5132 | 5157 | { |
b84a33b5 | 5158 | const Elf_Internal_Phdr *lp; |
3146fac4 | 5159 | struct elf_segment_map *lm; |
1ea63fd2 | 5160 | |
129af99f | 5161 | if (link_info != NULL) |
8c37241b | 5162 | { |
129af99f AS |
5163 | /* During linking the range of the RELRO segment is passed |
5164 | in link_info. */ | |
12bd6957 | 5165 | for (lm = elf_seg_map (abfd), lp = phdrs; |
3146fac4 AM |
5166 | lm != NULL; |
5167 | lm = lm->next, lp++) | |
8c37241b JJ |
5168 | { |
5169 | if (lp->p_type == PT_LOAD | |
b84a33b5 | 5170 | && lp->p_vaddr < link_info->relro_end |
3146fac4 AM |
5171 | && lm->count != 0 |
5172 | && lm->sections[0]->vma >= link_info->relro_start) | |
8c37241b JJ |
5173 | break; |
5174 | } | |
8981c88a | 5175 | |
3146fac4 | 5176 | BFD_ASSERT (lm != NULL); |
8c37241b | 5177 | } |
129af99f AS |
5178 | else |
5179 | { | |
5180 | /* Otherwise we are copying an executable or shared | |
b84a33b5 | 5181 | library, but we need to use the same linker logic. */ |
129af99f AS |
5182 | for (lp = phdrs; lp < phdrs + count; ++lp) |
5183 | { | |
5184 | if (lp->p_type == PT_LOAD | |
5185 | && lp->p_paddr == p->p_paddr) | |
5186 | break; | |
5187 | } | |
b84a33b5 AM |
5188 | } |
5189 | ||
5190 | if (lp < phdrs + count) | |
5191 | { | |
5192 | p->p_vaddr = lp->p_vaddr; | |
5193 | p->p_paddr = lp->p_paddr; | |
5194 | p->p_offset = lp->p_offset; | |
5195 | if (link_info != NULL) | |
5196 | p->p_filesz = link_info->relro_end - lp->p_vaddr; | |
5197 | else if (m->p_size_valid) | |
5198 | p->p_filesz = m->p_size; | |
129af99f AS |
5199 | else |
5200 | abort (); | |
b84a33b5 | 5201 | p->p_memsz = p->p_filesz; |
f3944f72 L |
5202 | /* Preserve the alignment and flags if they are valid. The |
5203 | gold linker generates RW/4 for the PT_GNU_RELRO section. | |
5204 | It is better for objcopy/strip to honor these attributes | |
5205 | otherwise gdb will choke when using separate debug files. | |
5206 | */ | |
5207 | if (!m->p_align_valid) | |
5208 | p->p_align = 1; | |
5209 | if (!m->p_flags_valid) | |
5210 | p->p_flags = (lp->p_flags & ~PF_W); | |
129af99f | 5211 | } |
9433b9b1 | 5212 | else |
b84a33b5 AM |
5213 | { |
5214 | memset (p, 0, sizeof *p); | |
5215 | p->p_type = PT_NULL; | |
5216 | } | |
129af99f | 5217 | } |
04c3a755 NS |
5218 | else if (p->p_type == PT_GNU_STACK) |
5219 | { | |
5220 | if (m->p_size_valid) | |
5221 | p->p_memsz = m->p_size; | |
5222 | } | |
129af99f AS |
5223 | else if (m->count != 0) |
5224 | { | |
5225 | if (p->p_type != PT_LOAD | |
5226 | && (p->p_type != PT_NOTE | |
5227 | || bfd_get_format (abfd) != bfd_core)) | |
5228 | { | |
c86934ce NC |
5229 | if (m->includes_filehdr || m->includes_phdrs) |
5230 | { | |
5231 | /* PR 17512: file: 2195325e. */ | |
5232 | (*_bfd_error_handler) | |
5233 | (_("%B: warning: non-load segment includes file header and/or program header"), | |
5234 | abfd); | |
5235 | return FALSE; | |
5236 | } | |
129af99f | 5237 | |
86b2281f | 5238 | p->p_filesz = 0; |
129af99f | 5239 | p->p_offset = m->sections[0]->filepos; |
86b2281f AM |
5240 | for (i = m->count; i-- != 0;) |
5241 | { | |
5242 | asection *sect = m->sections[i]; | |
5243 | Elf_Internal_Shdr *hdr = &elf_section_data (sect)->this_hdr; | |
5244 | if (hdr->sh_type != SHT_NOBITS) | |
5245 | { | |
5246 | p->p_filesz = (sect->filepos - m->sections[0]->filepos | |
5247 | + hdr->sh_size); | |
5248 | break; | |
5249 | } | |
5250 | } | |
129af99f AS |
5251 | } |
5252 | } | |
5253 | else if (m->includes_filehdr) | |
5254 | { | |
5255 | p->p_vaddr = filehdr_vaddr; | |
5256 | if (! m->p_paddr_valid) | |
5257 | p->p_paddr = filehdr_paddr; | |
5258 | } | |
5259 | else if (m->includes_phdrs) | |
5260 | { | |
5261 | p->p_vaddr = phdrs_vaddr; | |
5262 | if (! m->p_paddr_valid) | |
5263 | p->p_paddr = phdrs_paddr; | |
252b5132 RH |
5264 | } |
5265 | } | |
5266 | ||
12bd6957 | 5267 | elf_next_file_pos (abfd) = off; |
252b5132 | 5268 | |
b34976b6 | 5269 | return TRUE; |
252b5132 RH |
5270 | } |
5271 | ||
252b5132 RH |
5272 | /* Work out the file positions of all the sections. This is called by |
5273 | _bfd_elf_compute_section_file_positions. All the section sizes and | |
5274 | VMAs must be known before this is called. | |
5275 | ||
e0638f70 AM |
5276 | Reloc sections come in two flavours: Those processed specially as |
5277 | "side-channel" data attached to a section to which they apply, and | |
5278 | those that bfd doesn't process as relocations. The latter sort are | |
5279 | stored in a normal bfd section by bfd_section_from_shdr. We don't | |
5280 | consider the former sort here, unless they form part of the loadable | |
5281 | image. Reloc sections not assigned here will be handled later by | |
5282 | assign_file_positions_for_relocs. | |
252b5132 RH |
5283 | |
5284 | We also don't set the positions of the .symtab and .strtab here. */ | |
5285 | ||
b34976b6 | 5286 | static bfd_boolean |
c84fca4d AO |
5287 | assign_file_positions_except_relocs (bfd *abfd, |
5288 | struct bfd_link_info *link_info) | |
252b5132 | 5289 | { |
5c182d5f AM |
5290 | struct elf_obj_tdata *tdata = elf_tdata (abfd); |
5291 | Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd); | |
9c5bfbb7 | 5292 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
252b5132 RH |
5293 | |
5294 | if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0 | |
5295 | && bfd_get_format (abfd) != bfd_core) | |
5296 | { | |
5c182d5f AM |
5297 | Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd); |
5298 | unsigned int num_sec = elf_numsections (abfd); | |
252b5132 RH |
5299 | Elf_Internal_Shdr **hdrpp; |
5300 | unsigned int i; | |
a485e98e | 5301 | file_ptr off; |
252b5132 RH |
5302 | |
5303 | /* Start after the ELF header. */ | |
5304 | off = i_ehdrp->e_ehsize; | |
5305 | ||
5306 | /* We are not creating an executable, which means that we are | |
5307 | not creating a program header, and that the actual order of | |
5308 | the sections in the file is unimportant. */ | |
9ad5cbcf | 5309 | for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++) |
252b5132 RH |
5310 | { |
5311 | Elf_Internal_Shdr *hdr; | |
5312 | ||
5313 | hdr = *hdrpp; | |
e0638f70 AM |
5314 | if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA) |
5315 | && hdr->bfd_section == NULL) | |
12bd6957 AM |
5316 | || i == elf_onesymtab (abfd) |
5317 | || i == elf_symtab_shndx (abfd) | |
5318 | || i == elf_strtab_sec (abfd)) | |
252b5132 RH |
5319 | { |
5320 | hdr->sh_offset = -1; | |
252b5132 | 5321 | } |
9ad5cbcf | 5322 | else |
b34976b6 | 5323 | off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE); |
252b5132 | 5324 | } |
a485e98e AM |
5325 | |
5326 | elf_next_file_pos (abfd) = off; | |
252b5132 RH |
5327 | } |
5328 | else | |
5329 | { | |
f3520d2f AM |
5330 | unsigned int alloc; |
5331 | ||
252b5132 | 5332 | /* Assign file positions for the loaded sections based on the |
08a40648 | 5333 | assignment of sections to segments. */ |
f3520d2f AM |
5334 | if (!assign_file_positions_for_load_sections (abfd, link_info)) |
5335 | return FALSE; | |
5336 | ||
5337 | /* And for non-load sections. */ | |
5338 | if (!assign_file_positions_for_non_load_sections (abfd, link_info)) | |
5339 | return FALSE; | |
5340 | ||
e36284ab AM |
5341 | if (bed->elf_backend_modify_program_headers != NULL) |
5342 | { | |
5343 | if (!(*bed->elf_backend_modify_program_headers) (abfd, link_info)) | |
5344 | return FALSE; | |
5345 | } | |
5346 | ||
58e7ebac L |
5347 | /* Set e_type in ELF header to ET_EXEC for -pie -Ttext-segment=. */ |
5348 | if (link_info != NULL | |
5349 | && link_info->executable | |
5350 | && link_info->shared) | |
5351 | { | |
5352 | unsigned int num_segments = elf_elfheader (abfd)->e_phnum; | |
5353 | Elf_Internal_Phdr *segment = elf_tdata (abfd)->phdr; | |
5354 | Elf_Internal_Phdr *end_segment = &segment[num_segments]; | |
5355 | ||
5356 | /* Find the lowest p_vaddr in PT_LOAD segments. */ | |
5357 | bfd_vma p_vaddr = (bfd_vma) -1; | |
5358 | for (; segment < end_segment; segment++) | |
5359 | if (segment->p_type == PT_LOAD && p_vaddr > segment->p_vaddr) | |
5360 | p_vaddr = segment->p_vaddr; | |
5361 | ||
5362 | /* Set e_type to ET_EXEC if the lowest p_vaddr in PT_LOAD | |
5363 | segments is non-zero. */ | |
5364 | if (p_vaddr) | |
5365 | i_ehdrp->e_type = ET_EXEC; | |
5366 | } | |
5367 | ||
f3520d2f | 5368 | /* Write out the program headers. */ |
12bd6957 | 5369 | alloc = elf_program_header_size (abfd) / bed->s->sizeof_phdr; |
f3520d2f AM |
5370 | if (bfd_seek (abfd, (bfd_signed_vma) bed->s->sizeof_ehdr, SEEK_SET) != 0 |
5371 | || bed->s->write_out_phdrs (abfd, tdata->phdr, alloc) != 0) | |
b34976b6 | 5372 | return FALSE; |
252b5132 RH |
5373 | } |
5374 | ||
b34976b6 | 5375 | return TRUE; |
252b5132 RH |
5376 | } |
5377 | ||
b34976b6 | 5378 | static bfd_boolean |
217aa764 | 5379 | prep_headers (bfd *abfd) |
252b5132 | 5380 | { |
3d540e93 | 5381 | Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form. */ |
2b0f7ef9 | 5382 | struct elf_strtab_hash *shstrtab; |
9c5bfbb7 | 5383 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
252b5132 RH |
5384 | |
5385 | i_ehdrp = elf_elfheader (abfd); | |
252b5132 | 5386 | |
2b0f7ef9 | 5387 | shstrtab = _bfd_elf_strtab_init (); |
252b5132 | 5388 | if (shstrtab == NULL) |
b34976b6 | 5389 | return FALSE; |
252b5132 RH |
5390 | |
5391 | elf_shstrtab (abfd) = shstrtab; | |
5392 | ||
5393 | i_ehdrp->e_ident[EI_MAG0] = ELFMAG0; | |
5394 | i_ehdrp->e_ident[EI_MAG1] = ELFMAG1; | |
5395 | i_ehdrp->e_ident[EI_MAG2] = ELFMAG2; | |
5396 | i_ehdrp->e_ident[EI_MAG3] = ELFMAG3; | |
5397 | ||
5398 | i_ehdrp->e_ident[EI_CLASS] = bed->s->elfclass; | |
5399 | i_ehdrp->e_ident[EI_DATA] = | |
5400 | bfd_big_endian (abfd) ? ELFDATA2MSB : ELFDATA2LSB; | |
5401 | i_ehdrp->e_ident[EI_VERSION] = bed->s->ev_current; | |
5402 | ||
252b5132 RH |
5403 | if ((abfd->flags & DYNAMIC) != 0) |
5404 | i_ehdrp->e_type = ET_DYN; | |
5405 | else if ((abfd->flags & EXEC_P) != 0) | |
5406 | i_ehdrp->e_type = ET_EXEC; | |
5407 | else if (bfd_get_format (abfd) == bfd_core) | |
5408 | i_ehdrp->e_type = ET_CORE; | |
5409 | else | |
5410 | i_ehdrp->e_type = ET_REL; | |
5411 | ||
5412 | switch (bfd_get_arch (abfd)) | |
5413 | { | |
5414 | case bfd_arch_unknown: | |
5415 | i_ehdrp->e_machine = EM_NONE; | |
5416 | break; | |
aa4f99bb AO |
5417 | |
5418 | /* There used to be a long list of cases here, each one setting | |
5419 | e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE | |
5420 | in the corresponding bfd definition. To avoid duplication, | |
5421 | the switch was removed. Machines that need special handling | |
5422 | can generally do it in elf_backend_final_write_processing(), | |
5423 | unless they need the information earlier than the final write. | |
5424 | Such need can generally be supplied by replacing the tests for | |
5425 | e_machine with the conditions used to determine it. */ | |
252b5132 | 5426 | default: |
9c5bfbb7 AM |
5427 | i_ehdrp->e_machine = bed->elf_machine_code; |
5428 | } | |
aa4f99bb | 5429 | |
252b5132 RH |
5430 | i_ehdrp->e_version = bed->s->ev_current; |
5431 | i_ehdrp->e_ehsize = bed->s->sizeof_ehdr; | |
5432 | ||
c044fabd | 5433 | /* No program header, for now. */ |
252b5132 RH |
5434 | i_ehdrp->e_phoff = 0; |
5435 | i_ehdrp->e_phentsize = 0; | |
5436 | i_ehdrp->e_phnum = 0; | |
5437 | ||
c044fabd | 5438 | /* Each bfd section is section header entry. */ |
252b5132 RH |
5439 | i_ehdrp->e_entry = bfd_get_start_address (abfd); |
5440 | i_ehdrp->e_shentsize = bed->s->sizeof_shdr; | |
5441 | ||
c044fabd | 5442 | /* If we're building an executable, we'll need a program header table. */ |
252b5132 | 5443 | if (abfd->flags & EXEC_P) |
0e71e495 BE |
5444 | /* It all happens later. */ |
5445 | ; | |
252b5132 RH |
5446 | else |
5447 | { | |
5448 | i_ehdrp->e_phentsize = 0; | |
252b5132 RH |
5449 | i_ehdrp->e_phoff = 0; |
5450 | } | |
5451 | ||
5452 | elf_tdata (abfd)->symtab_hdr.sh_name = | |
b34976b6 | 5453 | (unsigned int) _bfd_elf_strtab_add (shstrtab, ".symtab", FALSE); |
252b5132 | 5454 | elf_tdata (abfd)->strtab_hdr.sh_name = |
b34976b6 | 5455 | (unsigned int) _bfd_elf_strtab_add (shstrtab, ".strtab", FALSE); |
252b5132 | 5456 | elf_tdata (abfd)->shstrtab_hdr.sh_name = |
b34976b6 | 5457 | (unsigned int) _bfd_elf_strtab_add (shstrtab, ".shstrtab", FALSE); |
252b5132 | 5458 | if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1 |
17ca87fc | 5459 | || elf_tdata (abfd)->strtab_hdr.sh_name == (unsigned int) -1 |
252b5132 | 5460 | || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1) |
b34976b6 | 5461 | return FALSE; |
252b5132 | 5462 | |
b34976b6 | 5463 | return TRUE; |
252b5132 RH |
5464 | } |
5465 | ||
5466 | /* Assign file positions for all the reloc sections which are not part | |
a485e98e | 5467 | of the loadable file image, and the file position of section headers. */ |
252b5132 | 5468 | |
a485e98e | 5469 | static void |
217aa764 | 5470 | _bfd_elf_assign_file_positions_for_relocs (bfd *abfd) |
252b5132 RH |
5471 | { |
5472 | file_ptr off; | |
9ad5cbcf | 5473 | unsigned int i, num_sec; |
252b5132 | 5474 | Elf_Internal_Shdr **shdrpp; |
a485e98e AM |
5475 | Elf_Internal_Ehdr *i_ehdrp; |
5476 | const struct elf_backend_data *bed; | |
252b5132 | 5477 | |
12bd6957 | 5478 | off = elf_next_file_pos (abfd); |
252b5132 | 5479 | |
9ad5cbcf AM |
5480 | num_sec = elf_numsections (abfd); |
5481 | for (i = 1, shdrpp = elf_elfsections (abfd) + 1; i < num_sec; i++, shdrpp++) | |
252b5132 RH |
5482 | { |
5483 | Elf_Internal_Shdr *shdrp; | |
5484 | ||
5485 | shdrp = *shdrpp; | |
5486 | if ((shdrp->sh_type == SHT_REL || shdrp->sh_type == SHT_RELA) | |
5487 | && shdrp->sh_offset == -1) | |
b34976b6 | 5488 | off = _bfd_elf_assign_file_position_for_section (shdrp, off, TRUE); |
252b5132 RH |
5489 | } |
5490 | ||
a485e98e AM |
5491 | /* Place the section headers. */ |
5492 | i_ehdrp = elf_elfheader (abfd); | |
5493 | bed = get_elf_backend_data (abfd); | |
5494 | off = align_file_position (off, 1 << bed->s->log_file_align); | |
5495 | i_ehdrp->e_shoff = off; | |
5496 | off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize; | |
12bd6957 | 5497 | elf_next_file_pos (abfd) = off; |
252b5132 RH |
5498 | } |
5499 | ||
b34976b6 | 5500 | bfd_boolean |
217aa764 | 5501 | _bfd_elf_write_object_contents (bfd *abfd) |
252b5132 | 5502 | { |
9c5bfbb7 | 5503 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
252b5132 | 5504 | Elf_Internal_Shdr **i_shdrp; |
b34976b6 | 5505 | bfd_boolean failed; |
9ad5cbcf | 5506 | unsigned int count, num_sec; |
30e8ee25 | 5507 | struct elf_obj_tdata *t; |
252b5132 RH |
5508 | |
5509 | if (! abfd->output_has_begun | |
217aa764 | 5510 | && ! _bfd_elf_compute_section_file_positions (abfd, NULL)) |
b34976b6 | 5511 | return FALSE; |
252b5132 RH |
5512 | |
5513 | i_shdrp = elf_elfsections (abfd); | |
252b5132 | 5514 | |
b34976b6 | 5515 | failed = FALSE; |
252b5132 RH |
5516 | bfd_map_over_sections (abfd, bed->s->write_relocs, &failed); |
5517 | if (failed) | |
b34976b6 | 5518 | return FALSE; |
252b5132 RH |
5519 | |
5520 | _bfd_elf_assign_file_positions_for_relocs (abfd); | |
5521 | ||
c044fabd | 5522 | /* After writing the headers, we need to write the sections too... */ |
9ad5cbcf AM |
5523 | num_sec = elf_numsections (abfd); |
5524 | for (count = 1; count < num_sec; count++) | |
252b5132 RH |
5525 | { |
5526 | if (bed->elf_backend_section_processing) | |
5527 | (*bed->elf_backend_section_processing) (abfd, i_shdrp[count]); | |
5528 | if (i_shdrp[count]->contents) | |
5529 | { | |
dc810e39 AM |
5530 | bfd_size_type amt = i_shdrp[count]->sh_size; |
5531 | ||
252b5132 | 5532 | if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0 |
dc810e39 | 5533 | || bfd_bwrite (i_shdrp[count]->contents, amt, abfd) != amt) |
b34976b6 | 5534 | return FALSE; |
252b5132 RH |
5535 | } |
5536 | } | |
5537 | ||
5538 | /* Write out the section header names. */ | |
30e8ee25 | 5539 | t = elf_tdata (abfd); |
26ae6d5e | 5540 | if (elf_shstrtab (abfd) != NULL |
30e8ee25 | 5541 | && (bfd_seek (abfd, t->shstrtab_hdr.sh_offset, SEEK_SET) != 0 |
08a40648 | 5542 | || !_bfd_elf_strtab_emit (abfd, elf_shstrtab (abfd)))) |
b34976b6 | 5543 | return FALSE; |
252b5132 RH |
5544 | |
5545 | if (bed->elf_backend_final_write_processing) | |
12bd6957 | 5546 | (*bed->elf_backend_final_write_processing) (abfd, elf_linker (abfd)); |
252b5132 | 5547 | |
ff59fc36 RM |
5548 | if (!bed->s->write_shdrs_and_ehdr (abfd)) |
5549 | return FALSE; | |
5550 | ||
5551 | /* This is last since write_shdrs_and_ehdr can touch i_shdrp[0]. */ | |
c0355132 AM |
5552 | if (t->o->build_id.after_write_object_contents != NULL) |
5553 | return (*t->o->build_id.after_write_object_contents) (abfd); | |
ff59fc36 RM |
5554 | |
5555 | return TRUE; | |
252b5132 RH |
5556 | } |
5557 | ||
b34976b6 | 5558 | bfd_boolean |
217aa764 | 5559 | _bfd_elf_write_corefile_contents (bfd *abfd) |
252b5132 | 5560 | { |
c044fabd | 5561 | /* Hopefully this can be done just like an object file. */ |
252b5132 RH |
5562 | return _bfd_elf_write_object_contents (abfd); |
5563 | } | |
c044fabd KH |
5564 | |
5565 | /* Given a section, search the header to find them. */ | |
5566 | ||
cb33740c | 5567 | unsigned int |
198beae2 | 5568 | _bfd_elf_section_from_bfd_section (bfd *abfd, struct bfd_section *asect) |
252b5132 | 5569 | { |
9c5bfbb7 | 5570 | const struct elf_backend_data *bed; |
91d6fa6a | 5571 | unsigned int sec_index; |
252b5132 | 5572 | |
9ad5cbcf AM |
5573 | if (elf_section_data (asect) != NULL |
5574 | && elf_section_data (asect)->this_idx != 0) | |
5575 | return elf_section_data (asect)->this_idx; | |
5576 | ||
5577 | if (bfd_is_abs_section (asect)) | |
91d6fa6a | 5578 | sec_index = SHN_ABS; |
af746e92 | 5579 | else if (bfd_is_com_section (asect)) |
91d6fa6a | 5580 | sec_index = SHN_COMMON; |
af746e92 | 5581 | else if (bfd_is_und_section (asect)) |
91d6fa6a | 5582 | sec_index = SHN_UNDEF; |
af746e92 | 5583 | else |
91d6fa6a | 5584 | sec_index = SHN_BAD; |
252b5132 | 5585 | |
af746e92 | 5586 | bed = get_elf_backend_data (abfd); |
252b5132 RH |
5587 | if (bed->elf_backend_section_from_bfd_section) |
5588 | { | |
91d6fa6a | 5589 | int retval = sec_index; |
9ad5cbcf | 5590 | |
af746e92 AM |
5591 | if ((*bed->elf_backend_section_from_bfd_section) (abfd, asect, &retval)) |
5592 | return retval; | |
252b5132 RH |
5593 | } |
5594 | ||
91d6fa6a | 5595 | if (sec_index == SHN_BAD) |
af746e92 | 5596 | bfd_set_error (bfd_error_nonrepresentable_section); |
252b5132 | 5597 | |
91d6fa6a | 5598 | return sec_index; |
252b5132 RH |
5599 | } |
5600 | ||
5601 | /* Given a BFD symbol, return the index in the ELF symbol table, or -1 | |
5602 | on error. */ | |
5603 | ||
5604 | int | |
217aa764 | 5605 | _bfd_elf_symbol_from_bfd_symbol (bfd *abfd, asymbol **asym_ptr_ptr) |
252b5132 RH |
5606 | { |
5607 | asymbol *asym_ptr = *asym_ptr_ptr; | |
5608 | int idx; | |
5609 | flagword flags = asym_ptr->flags; | |
5610 | ||
5611 | /* When gas creates relocations against local labels, it creates its | |
5612 | own symbol for the section, but does put the symbol into the | |
5613 | symbol chain, so udata is 0. When the linker is generating | |
5614 | relocatable output, this section symbol may be for one of the | |
5615 | input sections rather than the output section. */ | |
5616 | if (asym_ptr->udata.i == 0 | |
5617 | && (flags & BSF_SECTION_SYM) | |
5618 | && asym_ptr->section) | |
5619 | { | |
5372391b | 5620 | asection *sec; |
252b5132 RH |
5621 | int indx; |
5622 | ||
5372391b AM |
5623 | sec = asym_ptr->section; |
5624 | if (sec->owner != abfd && sec->output_section != NULL) | |
5625 | sec = sec->output_section; | |
5626 | if (sec->owner == abfd | |
5627 | && (indx = sec->index) < elf_num_section_syms (abfd) | |
4e89ac30 | 5628 | && elf_section_syms (abfd)[indx] != NULL) |
252b5132 RH |
5629 | asym_ptr->udata.i = elf_section_syms (abfd)[indx]->udata.i; |
5630 | } | |
5631 | ||
5632 | idx = asym_ptr->udata.i; | |
5633 | ||
5634 | if (idx == 0) | |
5635 | { | |
5636 | /* This case can occur when using --strip-symbol on a symbol | |
08a40648 | 5637 | which is used in a relocation entry. */ |
252b5132 | 5638 | (*_bfd_error_handler) |
d003868e AM |
5639 | (_("%B: symbol `%s' required but not present"), |
5640 | abfd, bfd_asymbol_name (asym_ptr)); | |
252b5132 RH |
5641 | bfd_set_error (bfd_error_no_symbols); |
5642 | return -1; | |
5643 | } | |
5644 | ||
5645 | #if DEBUG & 4 | |
5646 | { | |
5647 | fprintf (stderr, | |
9ccb8af9 AM |
5648 | "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8lx\n", |
5649 | (long) asym_ptr, asym_ptr->name, idx, (long) flags); | |
252b5132 RH |
5650 | fflush (stderr); |
5651 | } | |
5652 | #endif | |
5653 | ||
5654 | return idx; | |
5655 | } | |
5656 | ||
84d1d650 | 5657 | /* Rewrite program header information. */ |
252b5132 | 5658 | |
b34976b6 | 5659 | static bfd_boolean |
84d1d650 | 5660 | rewrite_elf_program_header (bfd *ibfd, bfd *obfd) |
252b5132 | 5661 | { |
b34976b6 AM |
5662 | Elf_Internal_Ehdr *iehdr; |
5663 | struct elf_segment_map *map; | |
5664 | struct elf_segment_map *map_first; | |
5665 | struct elf_segment_map **pointer_to_map; | |
5666 | Elf_Internal_Phdr *segment; | |
5667 | asection *section; | |
5668 | unsigned int i; | |
5669 | unsigned int num_segments; | |
5670 | bfd_boolean phdr_included = FALSE; | |
5c44b38e | 5671 | bfd_boolean p_paddr_valid; |
b34976b6 AM |
5672 | bfd_vma maxpagesize; |
5673 | struct elf_segment_map *phdr_adjust_seg = NULL; | |
5674 | unsigned int phdr_adjust_num = 0; | |
9c5bfbb7 | 5675 | const struct elf_backend_data *bed; |
bc67d8a6 | 5676 | |
caf47ea6 | 5677 | bed = get_elf_backend_data (ibfd); |
252b5132 RH |
5678 | iehdr = elf_elfheader (ibfd); |
5679 | ||
bc67d8a6 | 5680 | map_first = NULL; |
c044fabd | 5681 | pointer_to_map = &map_first; |
252b5132 RH |
5682 | |
5683 | num_segments = elf_elfheader (ibfd)->e_phnum; | |
bc67d8a6 NC |
5684 | maxpagesize = get_elf_backend_data (obfd)->maxpagesize; |
5685 | ||
5686 | /* Returns the end address of the segment + 1. */ | |
aecc8f8a AM |
5687 | #define SEGMENT_END(segment, start) \ |
5688 | (start + (segment->p_memsz > segment->p_filesz \ | |
5689 | ? segment->p_memsz : segment->p_filesz)) | |
bc67d8a6 | 5690 | |
eecdbe52 JJ |
5691 | #define SECTION_SIZE(section, segment) \ |
5692 | (((section->flags & (SEC_HAS_CONTENTS | SEC_THREAD_LOCAL)) \ | |
5693 | != SEC_THREAD_LOCAL || segment->p_type == PT_TLS) \ | |
eea6121a | 5694 | ? section->size : 0) |
eecdbe52 | 5695 | |
b34976b6 | 5696 | /* Returns TRUE if the given section is contained within |
bc67d8a6 | 5697 | the given segment. VMA addresses are compared. */ |
aecc8f8a AM |
5698 | #define IS_CONTAINED_BY_VMA(section, segment) \ |
5699 | (section->vma >= segment->p_vaddr \ | |
eecdbe52 | 5700 | && (section->vma + SECTION_SIZE (section, segment) \ |
aecc8f8a | 5701 | <= (SEGMENT_END (segment, segment->p_vaddr)))) |
c044fabd | 5702 | |
b34976b6 | 5703 | /* Returns TRUE if the given section is contained within |
bc67d8a6 | 5704 | the given segment. LMA addresses are compared. */ |
aecc8f8a AM |
5705 | #define IS_CONTAINED_BY_LMA(section, segment, base) \ |
5706 | (section->lma >= base \ | |
eecdbe52 | 5707 | && (section->lma + SECTION_SIZE (section, segment) \ |
aecc8f8a | 5708 | <= SEGMENT_END (segment, base))) |
252b5132 | 5709 | |
0efc80c8 L |
5710 | /* Handle PT_NOTE segment. */ |
5711 | #define IS_NOTE(p, s) \ | |
aecc8f8a | 5712 | (p->p_type == PT_NOTE \ |
0efc80c8 | 5713 | && elf_section_type (s) == SHT_NOTE \ |
aecc8f8a | 5714 | && (bfd_vma) s->filepos >= p->p_offset \ |
cb3ff1e5 | 5715 | && ((bfd_vma) s->filepos + s->size \ |
aecc8f8a | 5716 | <= p->p_offset + p->p_filesz)) |
252b5132 | 5717 | |
0efc80c8 L |
5718 | /* Special case: corefile "NOTE" section containing regs, prpsinfo |
5719 | etc. */ | |
5720 | #define IS_COREFILE_NOTE(p, s) \ | |
5721 | (IS_NOTE (p, s) \ | |
5722 | && bfd_get_format (ibfd) == bfd_core \ | |
5723 | && s->vma == 0 \ | |
5724 | && s->lma == 0) | |
5725 | ||
252b5132 RH |
5726 | /* The complicated case when p_vaddr is 0 is to handle the Solaris |
5727 | linker, which generates a PT_INTERP section with p_vaddr and | |
5728 | p_memsz set to 0. */ | |
aecc8f8a AM |
5729 | #define IS_SOLARIS_PT_INTERP(p, s) \ |
5730 | (p->p_vaddr == 0 \ | |
5731 | && p->p_paddr == 0 \ | |
5732 | && p->p_memsz == 0 \ | |
5733 | && p->p_filesz > 0 \ | |
5734 | && (s->flags & SEC_HAS_CONTENTS) != 0 \ | |
eea6121a | 5735 | && s->size > 0 \ |
aecc8f8a | 5736 | && (bfd_vma) s->filepos >= p->p_offset \ |
cb3ff1e5 | 5737 | && ((bfd_vma) s->filepos + s->size \ |
aecc8f8a | 5738 | <= p->p_offset + p->p_filesz)) |
5c440b1e | 5739 | |
bc67d8a6 NC |
5740 | /* Decide if the given section should be included in the given segment. |
5741 | A section will be included if: | |
f5ffc919 | 5742 | 1. It is within the address space of the segment -- we use the LMA |
08a40648 | 5743 | if that is set for the segment and the VMA otherwise, |
0efc80c8 | 5744 | 2. It is an allocated section or a NOTE section in a PT_NOTE |
d324f6d6 | 5745 | segment. |
bc67d8a6 | 5746 | 3. There is an output section associated with it, |
eecdbe52 | 5747 | 4. The section has not already been allocated to a previous segment. |
2b05f1b7 | 5748 | 5. PT_GNU_STACK segments do not include any sections. |
03394ac9 | 5749 | 6. PT_TLS segment includes only SHF_TLS sections. |
6f79b219 JJ |
5750 | 7. SHF_TLS sections are only in PT_TLS or PT_LOAD segments. |
5751 | 8. PT_DYNAMIC should not contain empty sections at the beginning | |
08a40648 | 5752 | (with the possible exception of .dynamic). */ |
9f17e2a6 | 5753 | #define IS_SECTION_IN_INPUT_SEGMENT(section, segment, bed) \ |
2b05f1b7 L |
5754 | ((((segment->p_paddr \ |
5755 | ? IS_CONTAINED_BY_LMA (section, segment, segment->p_paddr) \ | |
5756 | : IS_CONTAINED_BY_VMA (section, segment)) \ | |
5757 | && (section->flags & SEC_ALLOC) != 0) \ | |
0efc80c8 | 5758 | || IS_NOTE (segment, section)) \ |
2b05f1b7 L |
5759 | && segment->p_type != PT_GNU_STACK \ |
5760 | && (segment->p_type != PT_TLS \ | |
5761 | || (section->flags & SEC_THREAD_LOCAL)) \ | |
5762 | && (segment->p_type == PT_LOAD \ | |
5763 | || segment->p_type == PT_TLS \ | |
5764 | || (section->flags & SEC_THREAD_LOCAL) == 0) \ | |
5765 | && (segment->p_type != PT_DYNAMIC \ | |
5766 | || SECTION_SIZE (section, segment) > 0 \ | |
5767 | || (segment->p_paddr \ | |
5768 | ? segment->p_paddr != section->lma \ | |
5769 | : segment->p_vaddr != section->vma) \ | |
5770 | || (strcmp (bfd_get_section_name (ibfd, section), ".dynamic") \ | |
5771 | == 0)) \ | |
0067a569 | 5772 | && !section->segment_mark) |
bc67d8a6 | 5773 | |
9f17e2a6 L |
5774 | /* If the output section of a section in the input segment is NULL, |
5775 | it is removed from the corresponding output segment. */ | |
5776 | #define INCLUDE_SECTION_IN_SEGMENT(section, segment, bed) \ | |
5777 | (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed) \ | |
5778 | && section->output_section != NULL) | |
5779 | ||
b34976b6 | 5780 | /* Returns TRUE iff seg1 starts after the end of seg2. */ |
b5f852ea NC |
5781 | #define SEGMENT_AFTER_SEGMENT(seg1, seg2, field) \ |
5782 | (seg1->field >= SEGMENT_END (seg2, seg2->field)) | |
5783 | ||
5784 | /* Returns TRUE iff seg1 and seg2 overlap. Segments overlap iff both | |
5785 | their VMA address ranges and their LMA address ranges overlap. | |
5786 | It is possible to have overlapping VMA ranges without overlapping LMA | |
5787 | ranges. RedBoot images for example can have both .data and .bss mapped | |
5788 | to the same VMA range, but with the .data section mapped to a different | |
5789 | LMA. */ | |
aecc8f8a | 5790 | #define SEGMENT_OVERLAPS(seg1, seg2) \ |
b5f852ea | 5791 | ( !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_vaddr) \ |
08a40648 | 5792 | || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_vaddr)) \ |
b5f852ea | 5793 | && !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_paddr) \ |
08a40648 | 5794 | || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_paddr))) |
bc67d8a6 NC |
5795 | |
5796 | /* Initialise the segment mark field. */ | |
5797 | for (section = ibfd->sections; section != NULL; section = section->next) | |
b34976b6 | 5798 | section->segment_mark = FALSE; |
bc67d8a6 | 5799 | |
5c44b38e AM |
5800 | /* The Solaris linker creates program headers in which all the |
5801 | p_paddr fields are zero. When we try to objcopy or strip such a | |
5802 | file, we get confused. Check for this case, and if we find it | |
5803 | don't set the p_paddr_valid fields. */ | |
5804 | p_paddr_valid = FALSE; | |
5805 | for (i = 0, segment = elf_tdata (ibfd)->phdr; | |
5806 | i < num_segments; | |
5807 | i++, segment++) | |
5808 | if (segment->p_paddr != 0) | |
5809 | { | |
5810 | p_paddr_valid = TRUE; | |
5811 | break; | |
5812 | } | |
5813 | ||
252b5132 | 5814 | /* Scan through the segments specified in the program header |
bc67d8a6 | 5815 | of the input BFD. For this first scan we look for overlaps |
9ad5cbcf | 5816 | in the loadable segments. These can be created by weird |
aecc8f8a | 5817 | parameters to objcopy. Also, fix some solaris weirdness. */ |
bc67d8a6 NC |
5818 | for (i = 0, segment = elf_tdata (ibfd)->phdr; |
5819 | i < num_segments; | |
c044fabd | 5820 | i++, segment++) |
252b5132 | 5821 | { |
252b5132 | 5822 | unsigned int j; |
c044fabd | 5823 | Elf_Internal_Phdr *segment2; |
252b5132 | 5824 | |
aecc8f8a AM |
5825 | if (segment->p_type == PT_INTERP) |
5826 | for (section = ibfd->sections; section; section = section->next) | |
5827 | if (IS_SOLARIS_PT_INTERP (segment, section)) | |
5828 | { | |
5829 | /* Mininal change so that the normal section to segment | |
4cc11e76 | 5830 | assignment code will work. */ |
aecc8f8a AM |
5831 | segment->p_vaddr = section->vma; |
5832 | break; | |
5833 | } | |
5834 | ||
bc67d8a6 | 5835 | if (segment->p_type != PT_LOAD) |
b10a8ae0 L |
5836 | { |
5837 | /* Remove PT_GNU_RELRO segment. */ | |
5838 | if (segment->p_type == PT_GNU_RELRO) | |
5839 | segment->p_type = PT_NULL; | |
5840 | continue; | |
5841 | } | |
c044fabd | 5842 | |
bc67d8a6 | 5843 | /* Determine if this segment overlaps any previous segments. */ |
0067a569 | 5844 | for (j = 0, segment2 = elf_tdata (ibfd)->phdr; j < i; j++, segment2++) |
bc67d8a6 NC |
5845 | { |
5846 | bfd_signed_vma extra_length; | |
c044fabd | 5847 | |
bc67d8a6 | 5848 | if (segment2->p_type != PT_LOAD |
0067a569 | 5849 | || !SEGMENT_OVERLAPS (segment, segment2)) |
bc67d8a6 | 5850 | continue; |
c044fabd | 5851 | |
bc67d8a6 NC |
5852 | /* Merge the two segments together. */ |
5853 | if (segment2->p_vaddr < segment->p_vaddr) | |
5854 | { | |
c044fabd | 5855 | /* Extend SEGMENT2 to include SEGMENT and then delete |
08a40648 | 5856 | SEGMENT. */ |
0067a569 AM |
5857 | extra_length = (SEGMENT_END (segment, segment->p_vaddr) |
5858 | - SEGMENT_END (segment2, segment2->p_vaddr)); | |
c044fabd | 5859 | |
bc67d8a6 NC |
5860 | if (extra_length > 0) |
5861 | { | |
0067a569 | 5862 | segment2->p_memsz += extra_length; |
bc67d8a6 NC |
5863 | segment2->p_filesz += extra_length; |
5864 | } | |
c044fabd | 5865 | |
bc67d8a6 | 5866 | segment->p_type = PT_NULL; |
c044fabd | 5867 | |
bc67d8a6 NC |
5868 | /* Since we have deleted P we must restart the outer loop. */ |
5869 | i = 0; | |
5870 | segment = elf_tdata (ibfd)->phdr; | |
5871 | break; | |
5872 | } | |
5873 | else | |
5874 | { | |
c044fabd | 5875 | /* Extend SEGMENT to include SEGMENT2 and then delete |
08a40648 | 5876 | SEGMENT2. */ |
0067a569 AM |
5877 | extra_length = (SEGMENT_END (segment2, segment2->p_vaddr) |
5878 | - SEGMENT_END (segment, segment->p_vaddr)); | |
c044fabd | 5879 | |
bc67d8a6 NC |
5880 | if (extra_length > 0) |
5881 | { | |
0067a569 | 5882 | segment->p_memsz += extra_length; |
bc67d8a6 NC |
5883 | segment->p_filesz += extra_length; |
5884 | } | |
c044fabd | 5885 | |
bc67d8a6 NC |
5886 | segment2->p_type = PT_NULL; |
5887 | } | |
5888 | } | |
5889 | } | |
c044fabd | 5890 | |
bc67d8a6 NC |
5891 | /* The second scan attempts to assign sections to segments. */ |
5892 | for (i = 0, segment = elf_tdata (ibfd)->phdr; | |
5893 | i < num_segments; | |
0067a569 | 5894 | i++, segment++) |
bc67d8a6 | 5895 | { |
0067a569 AM |
5896 | unsigned int section_count; |
5897 | asection **sections; | |
5898 | asection *output_section; | |
5899 | unsigned int isec; | |
5900 | bfd_vma matching_lma; | |
5901 | bfd_vma suggested_lma; | |
5902 | unsigned int j; | |
dc810e39 | 5903 | bfd_size_type amt; |
0067a569 AM |
5904 | asection *first_section; |
5905 | bfd_boolean first_matching_lma; | |
5906 | bfd_boolean first_suggested_lma; | |
bc67d8a6 NC |
5907 | |
5908 | if (segment->p_type == PT_NULL) | |
5909 | continue; | |
c044fabd | 5910 | |
9f17e2a6 | 5911 | first_section = NULL; |
bc67d8a6 | 5912 | /* Compute how many sections might be placed into this segment. */ |
b5f852ea NC |
5913 | for (section = ibfd->sections, section_count = 0; |
5914 | section != NULL; | |
5915 | section = section->next) | |
9f17e2a6 L |
5916 | { |
5917 | /* Find the first section in the input segment, which may be | |
5918 | removed from the corresponding output segment. */ | |
5919 | if (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed)) | |
5920 | { | |
5921 | if (first_section == NULL) | |
5922 | first_section = section; | |
5923 | if (section->output_section != NULL) | |
5924 | ++section_count; | |
5925 | } | |
5926 | } | |
811072d8 | 5927 | |
b5f852ea NC |
5928 | /* Allocate a segment map big enough to contain |
5929 | all of the sections we have selected. */ | |
dc810e39 AM |
5930 | amt = sizeof (struct elf_segment_map); |
5931 | amt += ((bfd_size_type) section_count - 1) * sizeof (asection *); | |
a50b1753 | 5932 | map = (struct elf_segment_map *) bfd_zalloc (obfd, amt); |
bc67d8a6 | 5933 | if (map == NULL) |
b34976b6 | 5934 | return FALSE; |
252b5132 RH |
5935 | |
5936 | /* Initialise the fields of the segment map. Default to | |
5937 | using the physical address of the segment in the input BFD. */ | |
0067a569 AM |
5938 | map->next = NULL; |
5939 | map->p_type = segment->p_type; | |
5940 | map->p_flags = segment->p_flags; | |
bc67d8a6 | 5941 | map->p_flags_valid = 1; |
55d55ac7 | 5942 | |
9f17e2a6 L |
5943 | /* If the first section in the input segment is removed, there is |
5944 | no need to preserve segment physical address in the corresponding | |
5945 | output segment. */ | |
945c025a | 5946 | if (!first_section || first_section->output_section != NULL) |
9f17e2a6 L |
5947 | { |
5948 | map->p_paddr = segment->p_paddr; | |
5c44b38e | 5949 | map->p_paddr_valid = p_paddr_valid; |
9f17e2a6 | 5950 | } |
252b5132 RH |
5951 | |
5952 | /* Determine if this segment contains the ELF file header | |
5953 | and if it contains the program headers themselves. */ | |
bc67d8a6 NC |
5954 | map->includes_filehdr = (segment->p_offset == 0 |
5955 | && segment->p_filesz >= iehdr->e_ehsize); | |
bc67d8a6 | 5956 | map->includes_phdrs = 0; |
252b5132 | 5957 | |
0067a569 | 5958 | if (!phdr_included || segment->p_type != PT_LOAD) |
252b5132 | 5959 | { |
bc67d8a6 NC |
5960 | map->includes_phdrs = |
5961 | (segment->p_offset <= (bfd_vma) iehdr->e_phoff | |
5962 | && (segment->p_offset + segment->p_filesz | |
252b5132 RH |
5963 | >= ((bfd_vma) iehdr->e_phoff |
5964 | + iehdr->e_phnum * iehdr->e_phentsize))); | |
c044fabd | 5965 | |
bc67d8a6 | 5966 | if (segment->p_type == PT_LOAD && map->includes_phdrs) |
b34976b6 | 5967 | phdr_included = TRUE; |
252b5132 RH |
5968 | } |
5969 | ||
bc67d8a6 | 5970 | if (section_count == 0) |
252b5132 RH |
5971 | { |
5972 | /* Special segments, such as the PT_PHDR segment, may contain | |
5973 | no sections, but ordinary, loadable segments should contain | |
1ed89aa9 NC |
5974 | something. They are allowed by the ELF spec however, so only |
5975 | a warning is produced. */ | |
bc67d8a6 | 5976 | if (segment->p_type == PT_LOAD) |
c86934ce NC |
5977 | (*_bfd_error_handler) (_("\ |
5978 | %B: warning: Empty loadable segment detected, is this intentional ?"), | |
0067a569 | 5979 | ibfd); |
252b5132 | 5980 | |
bc67d8a6 | 5981 | map->count = 0; |
c044fabd KH |
5982 | *pointer_to_map = map; |
5983 | pointer_to_map = &map->next; | |
252b5132 RH |
5984 | |
5985 | continue; | |
5986 | } | |
5987 | ||
5988 | /* Now scan the sections in the input BFD again and attempt | |
5989 | to add their corresponding output sections to the segment map. | |
5990 | The problem here is how to handle an output section which has | |
5991 | been moved (ie had its LMA changed). There are four possibilities: | |
5992 | ||
5993 | 1. None of the sections have been moved. | |
5994 | In this case we can continue to use the segment LMA from the | |
5995 | input BFD. | |
5996 | ||
5997 | 2. All of the sections have been moved by the same amount. | |
5998 | In this case we can change the segment's LMA to match the LMA | |
5999 | of the first section. | |
6000 | ||
6001 | 3. Some of the sections have been moved, others have not. | |
6002 | In this case those sections which have not been moved can be | |
6003 | placed in the current segment which will have to have its size, | |
6004 | and possibly its LMA changed, and a new segment or segments will | |
6005 | have to be created to contain the other sections. | |
6006 | ||
b5f852ea | 6007 | 4. The sections have been moved, but not by the same amount. |
252b5132 RH |
6008 | In this case we can change the segment's LMA to match the LMA |
6009 | of the first section and we will have to create a new segment | |
6010 | or segments to contain the other sections. | |
6011 | ||
6012 | In order to save time, we allocate an array to hold the section | |
6013 | pointers that we are interested in. As these sections get assigned | |
6014 | to a segment, they are removed from this array. */ | |
6015 | ||
a50b1753 | 6016 | sections = (asection **) bfd_malloc2 (section_count, sizeof (asection *)); |
252b5132 | 6017 | if (sections == NULL) |
b34976b6 | 6018 | return FALSE; |
252b5132 RH |
6019 | |
6020 | /* Step One: Scan for segment vs section LMA conflicts. | |
6021 | Also add the sections to the section array allocated above. | |
6022 | Also add the sections to the current segment. In the common | |
6023 | case, where the sections have not been moved, this means that | |
6024 | we have completely filled the segment, and there is nothing | |
6025 | more to do. */ | |
252b5132 | 6026 | isec = 0; |
72730e0c | 6027 | matching_lma = 0; |
252b5132 | 6028 | suggested_lma = 0; |
0067a569 AM |
6029 | first_matching_lma = TRUE; |
6030 | first_suggested_lma = TRUE; | |
252b5132 | 6031 | |
147d51c2 | 6032 | for (section = ibfd->sections; |
bc67d8a6 NC |
6033 | section != NULL; |
6034 | section = section->next) | |
147d51c2 L |
6035 | if (section == first_section) |
6036 | break; | |
6037 | ||
6038 | for (j = 0; section != NULL; section = section->next) | |
252b5132 | 6039 | { |
caf47ea6 | 6040 | if (INCLUDE_SECTION_IN_SEGMENT (section, segment, bed)) |
c0f7859b | 6041 | { |
bc67d8a6 NC |
6042 | output_section = section->output_section; |
6043 | ||
0067a569 | 6044 | sections[j++] = section; |
252b5132 RH |
6045 | |
6046 | /* The Solaris native linker always sets p_paddr to 0. | |
6047 | We try to catch that case here, and set it to the | |
5e8d7549 NC |
6048 | correct value. Note - some backends require that |
6049 | p_paddr be left as zero. */ | |
5c44b38e | 6050 | if (!p_paddr_valid |
4455705d | 6051 | && segment->p_vaddr != 0 |
0067a569 | 6052 | && !bed->want_p_paddr_set_to_zero |
252b5132 | 6053 | && isec == 0 |
bc67d8a6 | 6054 | && output_section->lma != 0 |
0067a569 AM |
6055 | && output_section->vma == (segment->p_vaddr |
6056 | + (map->includes_filehdr | |
6057 | ? iehdr->e_ehsize | |
6058 | : 0) | |
6059 | + (map->includes_phdrs | |
6060 | ? (iehdr->e_phnum | |
6061 | * iehdr->e_phentsize) | |
6062 | : 0))) | |
bc67d8a6 | 6063 | map->p_paddr = segment->p_vaddr; |
252b5132 RH |
6064 | |
6065 | /* Match up the physical address of the segment with the | |
6066 | LMA address of the output section. */ | |
bc67d8a6 | 6067 | if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr) |
5e8d7549 | 6068 | || IS_COREFILE_NOTE (segment, section) |
0067a569 AM |
6069 | || (bed->want_p_paddr_set_to_zero |
6070 | && IS_CONTAINED_BY_VMA (output_section, segment))) | |
252b5132 | 6071 | { |
0067a569 AM |
6072 | if (first_matching_lma || output_section->lma < matching_lma) |
6073 | { | |
6074 | matching_lma = output_section->lma; | |
6075 | first_matching_lma = FALSE; | |
6076 | } | |
252b5132 RH |
6077 | |
6078 | /* We assume that if the section fits within the segment | |
bc67d8a6 | 6079 | then it does not overlap any other section within that |
252b5132 | 6080 | segment. */ |
0067a569 AM |
6081 | map->sections[isec++] = output_section; |
6082 | } | |
6083 | else if (first_suggested_lma) | |
6084 | { | |
6085 | suggested_lma = output_section->lma; | |
6086 | first_suggested_lma = FALSE; | |
252b5132 | 6087 | } |
147d51c2 L |
6088 | |
6089 | if (j == section_count) | |
6090 | break; | |
252b5132 RH |
6091 | } |
6092 | } | |
6093 | ||
bc67d8a6 | 6094 | BFD_ASSERT (j == section_count); |
252b5132 RH |
6095 | |
6096 | /* Step Two: Adjust the physical address of the current segment, | |
6097 | if necessary. */ | |
bc67d8a6 | 6098 | if (isec == section_count) |
252b5132 RH |
6099 | { |
6100 | /* All of the sections fitted within the segment as currently | |
6101 | specified. This is the default case. Add the segment to | |
6102 | the list of built segments and carry on to process the next | |
6103 | program header in the input BFD. */ | |
bc67d8a6 | 6104 | map->count = section_count; |
c044fabd KH |
6105 | *pointer_to_map = map; |
6106 | pointer_to_map = &map->next; | |
08a40648 | 6107 | |
5c44b38e AM |
6108 | if (p_paddr_valid |
6109 | && !bed->want_p_paddr_set_to_zero | |
147d51c2 | 6110 | && matching_lma != map->p_paddr |
5c44b38e AM |
6111 | && !map->includes_filehdr |
6112 | && !map->includes_phdrs) | |
3271a814 NS |
6113 | /* There is some padding before the first section in the |
6114 | segment. So, we must account for that in the output | |
6115 | segment's vma. */ | |
6116 | map->p_vaddr_offset = matching_lma - map->p_paddr; | |
08a40648 | 6117 | |
252b5132 RH |
6118 | free (sections); |
6119 | continue; | |
6120 | } | |
252b5132 RH |
6121 | else |
6122 | { | |
0067a569 | 6123 | if (!first_matching_lma) |
72730e0c AM |
6124 | { |
6125 | /* At least one section fits inside the current segment. | |
6126 | Keep it, but modify its physical address to match the | |
6127 | LMA of the first section that fitted. */ | |
bc67d8a6 | 6128 | map->p_paddr = matching_lma; |
72730e0c AM |
6129 | } |
6130 | else | |
6131 | { | |
6132 | /* None of the sections fitted inside the current segment. | |
6133 | Change the current segment's physical address to match | |
6134 | the LMA of the first section. */ | |
bc67d8a6 | 6135 | map->p_paddr = suggested_lma; |
72730e0c AM |
6136 | } |
6137 | ||
bc67d8a6 NC |
6138 | /* Offset the segment physical address from the lma |
6139 | to allow for space taken up by elf headers. */ | |
6140 | if (map->includes_filehdr) | |
010c8431 AM |
6141 | { |
6142 | if (map->p_paddr >= iehdr->e_ehsize) | |
6143 | map->p_paddr -= iehdr->e_ehsize; | |
6144 | else | |
6145 | { | |
6146 | map->includes_filehdr = FALSE; | |
6147 | map->includes_phdrs = FALSE; | |
6148 | } | |
6149 | } | |
252b5132 | 6150 | |
bc67d8a6 NC |
6151 | if (map->includes_phdrs) |
6152 | { | |
010c8431 AM |
6153 | if (map->p_paddr >= iehdr->e_phnum * iehdr->e_phentsize) |
6154 | { | |
6155 | map->p_paddr -= iehdr->e_phnum * iehdr->e_phentsize; | |
6156 | ||
6157 | /* iehdr->e_phnum is just an estimate of the number | |
6158 | of program headers that we will need. Make a note | |
6159 | here of the number we used and the segment we chose | |
6160 | to hold these headers, so that we can adjust the | |
6161 | offset when we know the correct value. */ | |
6162 | phdr_adjust_num = iehdr->e_phnum; | |
6163 | phdr_adjust_seg = map; | |
6164 | } | |
6165 | else | |
6166 | map->includes_phdrs = FALSE; | |
bc67d8a6 | 6167 | } |
252b5132 RH |
6168 | } |
6169 | ||
6170 | /* Step Three: Loop over the sections again, this time assigning | |
caf47ea6 | 6171 | those that fit to the current segment and removing them from the |
252b5132 RH |
6172 | sections array; but making sure not to leave large gaps. Once all |
6173 | possible sections have been assigned to the current segment it is | |
6174 | added to the list of built segments and if sections still remain | |
6175 | to be assigned, a new segment is constructed before repeating | |
6176 | the loop. */ | |
6177 | isec = 0; | |
6178 | do | |
6179 | { | |
bc67d8a6 | 6180 | map->count = 0; |
252b5132 | 6181 | suggested_lma = 0; |
0067a569 | 6182 | first_suggested_lma = TRUE; |
252b5132 RH |
6183 | |
6184 | /* Fill the current segment with sections that fit. */ | |
bc67d8a6 | 6185 | for (j = 0; j < section_count; j++) |
252b5132 | 6186 | { |
bc67d8a6 | 6187 | section = sections[j]; |
252b5132 | 6188 | |
bc67d8a6 | 6189 | if (section == NULL) |
252b5132 RH |
6190 | continue; |
6191 | ||
bc67d8a6 | 6192 | output_section = section->output_section; |
252b5132 | 6193 | |
bc67d8a6 | 6194 | BFD_ASSERT (output_section != NULL); |
c044fabd | 6195 | |
bc67d8a6 NC |
6196 | if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr) |
6197 | || IS_COREFILE_NOTE (segment, section)) | |
252b5132 | 6198 | { |
bc67d8a6 | 6199 | if (map->count == 0) |
252b5132 RH |
6200 | { |
6201 | /* If the first section in a segment does not start at | |
bc67d8a6 NC |
6202 | the beginning of the segment, then something is |
6203 | wrong. */ | |
0067a569 AM |
6204 | if (output_section->lma |
6205 | != (map->p_paddr | |
6206 | + (map->includes_filehdr ? iehdr->e_ehsize : 0) | |
6207 | + (map->includes_phdrs | |
6208 | ? iehdr->e_phnum * iehdr->e_phentsize | |
6209 | : 0))) | |
252b5132 RH |
6210 | abort (); |
6211 | } | |
6212 | else | |
6213 | { | |
0067a569 | 6214 | asection *prev_sec; |
252b5132 | 6215 | |
bc67d8a6 | 6216 | prev_sec = map->sections[map->count - 1]; |
252b5132 RH |
6217 | |
6218 | /* If the gap between the end of the previous section | |
bc67d8a6 NC |
6219 | and the start of this section is more than |
6220 | maxpagesize then we need to start a new segment. */ | |
eea6121a | 6221 | if ((BFD_ALIGN (prev_sec->lma + prev_sec->size, |
079e9a2f | 6222 | maxpagesize) |
caf47ea6 | 6223 | < BFD_ALIGN (output_section->lma, maxpagesize)) |
0067a569 | 6224 | || (prev_sec->lma + prev_sec->size |
079e9a2f | 6225 | > output_section->lma)) |
252b5132 | 6226 | { |
0067a569 AM |
6227 | if (first_suggested_lma) |
6228 | { | |
6229 | suggested_lma = output_section->lma; | |
6230 | first_suggested_lma = FALSE; | |
6231 | } | |
252b5132 RH |
6232 | |
6233 | continue; | |
6234 | } | |
6235 | } | |
6236 | ||
bc67d8a6 | 6237 | map->sections[map->count++] = output_section; |
252b5132 RH |
6238 | ++isec; |
6239 | sections[j] = NULL; | |
b34976b6 | 6240 | section->segment_mark = TRUE; |
252b5132 | 6241 | } |
0067a569 AM |
6242 | else if (first_suggested_lma) |
6243 | { | |
6244 | suggested_lma = output_section->lma; | |
6245 | first_suggested_lma = FALSE; | |
6246 | } | |
252b5132 RH |
6247 | } |
6248 | ||
bc67d8a6 | 6249 | BFD_ASSERT (map->count > 0); |
252b5132 RH |
6250 | |
6251 | /* Add the current segment to the list of built segments. */ | |
c044fabd KH |
6252 | *pointer_to_map = map; |
6253 | pointer_to_map = &map->next; | |
252b5132 | 6254 | |
bc67d8a6 | 6255 | if (isec < section_count) |
252b5132 RH |
6256 | { |
6257 | /* We still have not allocated all of the sections to | |
6258 | segments. Create a new segment here, initialise it | |
6259 | and carry on looping. */ | |
dc810e39 AM |
6260 | amt = sizeof (struct elf_segment_map); |
6261 | amt += ((bfd_size_type) section_count - 1) * sizeof (asection *); | |
5964fc3a | 6262 | map = (struct elf_segment_map *) bfd_zalloc (obfd, amt); |
bc67d8a6 | 6263 | if (map == NULL) |
5ed6aba4 NC |
6264 | { |
6265 | free (sections); | |
6266 | return FALSE; | |
6267 | } | |
252b5132 RH |
6268 | |
6269 | /* Initialise the fields of the segment map. Set the physical | |
6270 | physical address to the LMA of the first section that has | |
6271 | not yet been assigned. */ | |
0067a569 AM |
6272 | map->next = NULL; |
6273 | map->p_type = segment->p_type; | |
6274 | map->p_flags = segment->p_flags; | |
6275 | map->p_flags_valid = 1; | |
6276 | map->p_paddr = suggested_lma; | |
5c44b38e | 6277 | map->p_paddr_valid = p_paddr_valid; |
bc67d8a6 | 6278 | map->includes_filehdr = 0; |
0067a569 | 6279 | map->includes_phdrs = 0; |
252b5132 RH |
6280 | } |
6281 | } | |
bc67d8a6 | 6282 | while (isec < section_count); |
252b5132 RH |
6283 | |
6284 | free (sections); | |
6285 | } | |
6286 | ||
12bd6957 | 6287 | elf_seg_map (obfd) = map_first; |
bc67d8a6 NC |
6288 | |
6289 | /* If we had to estimate the number of program headers that were | |
9ad5cbcf | 6290 | going to be needed, then check our estimate now and adjust |
bc67d8a6 NC |
6291 | the offset if necessary. */ |
6292 | if (phdr_adjust_seg != NULL) | |
6293 | { | |
6294 | unsigned int count; | |
c044fabd | 6295 | |
bc67d8a6 | 6296 | for (count = 0, map = map_first; map != NULL; map = map->next) |
c044fabd | 6297 | count++; |
252b5132 | 6298 | |
bc67d8a6 NC |
6299 | if (count > phdr_adjust_num) |
6300 | phdr_adjust_seg->p_paddr | |
6301 | -= (count - phdr_adjust_num) * iehdr->e_phentsize; | |
6302 | } | |
c044fabd | 6303 | |
bc67d8a6 | 6304 | #undef SEGMENT_END |
eecdbe52 | 6305 | #undef SECTION_SIZE |
bc67d8a6 NC |
6306 | #undef IS_CONTAINED_BY_VMA |
6307 | #undef IS_CONTAINED_BY_LMA | |
0efc80c8 | 6308 | #undef IS_NOTE |
252b5132 | 6309 | #undef IS_COREFILE_NOTE |
bc67d8a6 | 6310 | #undef IS_SOLARIS_PT_INTERP |
9f17e2a6 | 6311 | #undef IS_SECTION_IN_INPUT_SEGMENT |
bc67d8a6 NC |
6312 | #undef INCLUDE_SECTION_IN_SEGMENT |
6313 | #undef SEGMENT_AFTER_SEGMENT | |
6314 | #undef SEGMENT_OVERLAPS | |
b34976b6 | 6315 | return TRUE; |
252b5132 RH |
6316 | } |
6317 | ||
84d1d650 L |
6318 | /* Copy ELF program header information. */ |
6319 | ||
6320 | static bfd_boolean | |
6321 | copy_elf_program_header (bfd *ibfd, bfd *obfd) | |
6322 | { | |
6323 | Elf_Internal_Ehdr *iehdr; | |
6324 | struct elf_segment_map *map; | |
6325 | struct elf_segment_map *map_first; | |
6326 | struct elf_segment_map **pointer_to_map; | |
6327 | Elf_Internal_Phdr *segment; | |
6328 | unsigned int i; | |
6329 | unsigned int num_segments; | |
6330 | bfd_boolean phdr_included = FALSE; | |
88967714 | 6331 | bfd_boolean p_paddr_valid; |
84d1d650 L |
6332 | |
6333 | iehdr = elf_elfheader (ibfd); | |
6334 | ||
6335 | map_first = NULL; | |
6336 | pointer_to_map = &map_first; | |
6337 | ||
88967714 AM |
6338 | /* If all the segment p_paddr fields are zero, don't set |
6339 | map->p_paddr_valid. */ | |
6340 | p_paddr_valid = FALSE; | |
84d1d650 | 6341 | num_segments = elf_elfheader (ibfd)->e_phnum; |
88967714 AM |
6342 | for (i = 0, segment = elf_tdata (ibfd)->phdr; |
6343 | i < num_segments; | |
6344 | i++, segment++) | |
6345 | if (segment->p_paddr != 0) | |
6346 | { | |
6347 | p_paddr_valid = TRUE; | |
6348 | break; | |
6349 | } | |
6350 | ||
84d1d650 L |
6351 | for (i = 0, segment = elf_tdata (ibfd)->phdr; |
6352 | i < num_segments; | |
6353 | i++, segment++) | |
6354 | { | |
6355 | asection *section; | |
6356 | unsigned int section_count; | |
6357 | bfd_size_type amt; | |
6358 | Elf_Internal_Shdr *this_hdr; | |
53020534 | 6359 | asection *first_section = NULL; |
a76e6f2f | 6360 | asection *lowest_section; |
84d1d650 | 6361 | |
84d1d650 L |
6362 | /* Compute how many sections are in this segment. */ |
6363 | for (section = ibfd->sections, section_count = 0; | |
6364 | section != NULL; | |
6365 | section = section->next) | |
6366 | { | |
6367 | this_hdr = &(elf_section_data(section)->this_hdr); | |
f4638467 | 6368 | if (ELF_SECTION_IN_SEGMENT (this_hdr, segment)) |
3271a814 | 6369 | { |
a76e6f2f AM |
6370 | if (first_section == NULL) |
6371 | first_section = section; | |
3271a814 NS |
6372 | section_count++; |
6373 | } | |
84d1d650 L |
6374 | } |
6375 | ||
6376 | /* Allocate a segment map big enough to contain | |
6377 | all of the sections we have selected. */ | |
6378 | amt = sizeof (struct elf_segment_map); | |
6379 | if (section_count != 0) | |
6380 | amt += ((bfd_size_type) section_count - 1) * sizeof (asection *); | |
a50b1753 | 6381 | map = (struct elf_segment_map *) bfd_zalloc (obfd, amt); |
84d1d650 L |
6382 | if (map == NULL) |
6383 | return FALSE; | |
6384 | ||
6385 | /* Initialize the fields of the output segment map with the | |
6386 | input segment. */ | |
6387 | map->next = NULL; | |
6388 | map->p_type = segment->p_type; | |
6389 | map->p_flags = segment->p_flags; | |
6390 | map->p_flags_valid = 1; | |
6391 | map->p_paddr = segment->p_paddr; | |
88967714 | 6392 | map->p_paddr_valid = p_paddr_valid; |
3f570048 AM |
6393 | map->p_align = segment->p_align; |
6394 | map->p_align_valid = 1; | |
3271a814 | 6395 | map->p_vaddr_offset = 0; |
84d1d650 | 6396 | |
04c3a755 NS |
6397 | if (map->p_type == PT_GNU_RELRO |
6398 | || map->p_type == PT_GNU_STACK) | |
b10a8ae0 L |
6399 | { |
6400 | /* The PT_GNU_RELRO segment may contain the first a few | |
6401 | bytes in the .got.plt section even if the whole .got.plt | |
6402 | section isn't in the PT_GNU_RELRO segment. We won't | |
04c3a755 NS |
6403 | change the size of the PT_GNU_RELRO segment. |
6404 | Similarly, PT_GNU_STACK size is significant on uclinux | |
6405 | systems. */ | |
9433b9b1 | 6406 | map->p_size = segment->p_memsz; |
b10a8ae0 L |
6407 | map->p_size_valid = 1; |
6408 | } | |
6409 | ||
84d1d650 L |
6410 | /* Determine if this segment contains the ELF file header |
6411 | and if it contains the program headers themselves. */ | |
6412 | map->includes_filehdr = (segment->p_offset == 0 | |
6413 | && segment->p_filesz >= iehdr->e_ehsize); | |
6414 | ||
6415 | map->includes_phdrs = 0; | |
6416 | if (! phdr_included || segment->p_type != PT_LOAD) | |
6417 | { | |
6418 | map->includes_phdrs = | |
6419 | (segment->p_offset <= (bfd_vma) iehdr->e_phoff | |
6420 | && (segment->p_offset + segment->p_filesz | |
6421 | >= ((bfd_vma) iehdr->e_phoff | |
6422 | + iehdr->e_phnum * iehdr->e_phentsize))); | |
6423 | ||
6424 | if (segment->p_type == PT_LOAD && map->includes_phdrs) | |
6425 | phdr_included = TRUE; | |
6426 | } | |
6427 | ||
bbefd0a9 | 6428 | lowest_section = NULL; |
84d1d650 L |
6429 | if (section_count != 0) |
6430 | { | |
6431 | unsigned int isec = 0; | |
6432 | ||
53020534 | 6433 | for (section = first_section; |
84d1d650 L |
6434 | section != NULL; |
6435 | section = section->next) | |
6436 | { | |
6437 | this_hdr = &(elf_section_data(section)->this_hdr); | |
f4638467 | 6438 | if (ELF_SECTION_IN_SEGMENT (this_hdr, segment)) |
53020534 L |
6439 | { |
6440 | map->sections[isec++] = section->output_section; | |
a76e6f2f AM |
6441 | if ((section->flags & SEC_ALLOC) != 0) |
6442 | { | |
6443 | bfd_vma seg_off; | |
6444 | ||
bbefd0a9 AM |
6445 | if (lowest_section == NULL |
6446 | || section->lma < lowest_section->lma) | |
fb8a5684 AM |
6447 | lowest_section = section; |
6448 | ||
a76e6f2f AM |
6449 | /* Section lmas are set up from PT_LOAD header |
6450 | p_paddr in _bfd_elf_make_section_from_shdr. | |
6451 | If this header has a p_paddr that disagrees | |
6452 | with the section lma, flag the p_paddr as | |
6453 | invalid. */ | |
6454 | if ((section->flags & SEC_LOAD) != 0) | |
6455 | seg_off = this_hdr->sh_offset - segment->p_offset; | |
6456 | else | |
6457 | seg_off = this_hdr->sh_addr - segment->p_vaddr; | |
6458 | if (section->lma - segment->p_paddr != seg_off) | |
6459 | map->p_paddr_valid = FALSE; | |
6460 | } | |
53020534 L |
6461 | if (isec == section_count) |
6462 | break; | |
6463 | } | |
84d1d650 L |
6464 | } |
6465 | } | |
6466 | ||
a76e6f2f AM |
6467 | if (map->includes_filehdr && lowest_section != NULL) |
6468 | /* We need to keep the space used by the headers fixed. */ | |
6469 | map->header_size = lowest_section->vma - segment->p_vaddr; | |
d324f6d6 | 6470 | |
a76e6f2f AM |
6471 | if (!map->includes_phdrs |
6472 | && !map->includes_filehdr | |
6473 | && map->p_paddr_valid) | |
6474 | /* There is some other padding before the first section. */ | |
6475 | map->p_vaddr_offset = ((lowest_section ? lowest_section->lma : 0) | |
6476 | - segment->p_paddr); | |
6477 | ||
84d1d650 L |
6478 | map->count = section_count; |
6479 | *pointer_to_map = map; | |
6480 | pointer_to_map = &map->next; | |
6481 | } | |
6482 | ||
12bd6957 | 6483 | elf_seg_map (obfd) = map_first; |
84d1d650 L |
6484 | return TRUE; |
6485 | } | |
6486 | ||
6487 | /* Copy private BFD data. This copies or rewrites ELF program header | |
6488 | information. */ | |
6489 | ||
6490 | static bfd_boolean | |
6491 | copy_private_bfd_data (bfd *ibfd, bfd *obfd) | |
6492 | { | |
84d1d650 L |
6493 | if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour |
6494 | || bfd_get_flavour (obfd) != bfd_target_elf_flavour) | |
6495 | return TRUE; | |
6496 | ||
6497 | if (elf_tdata (ibfd)->phdr == NULL) | |
6498 | return TRUE; | |
6499 | ||
6500 | if (ibfd->xvec == obfd->xvec) | |
6501 | { | |
cb3ff1e5 NC |
6502 | /* Check to see if any sections in the input BFD |
6503 | covered by ELF program header have changed. */ | |
d55ce4e2 | 6504 | Elf_Internal_Phdr *segment; |
84d1d650 L |
6505 | asection *section, *osec; |
6506 | unsigned int i, num_segments; | |
6507 | Elf_Internal_Shdr *this_hdr; | |
147d51c2 L |
6508 | const struct elf_backend_data *bed; |
6509 | ||
6510 | bed = get_elf_backend_data (ibfd); | |
6511 | ||
6512 | /* Regenerate the segment map if p_paddr is set to 0. */ | |
6513 | if (bed->want_p_paddr_set_to_zero) | |
6514 | goto rewrite; | |
84d1d650 L |
6515 | |
6516 | /* Initialize the segment mark field. */ | |
6517 | for (section = obfd->sections; section != NULL; | |
6518 | section = section->next) | |
6519 | section->segment_mark = FALSE; | |
6520 | ||
6521 | num_segments = elf_elfheader (ibfd)->e_phnum; | |
6522 | for (i = 0, segment = elf_tdata (ibfd)->phdr; | |
6523 | i < num_segments; | |
6524 | i++, segment++) | |
6525 | { | |
5f6999aa NC |
6526 | /* PR binutils/3535. The Solaris linker always sets the p_paddr |
6527 | and p_memsz fields of special segments (DYNAMIC, INTERP) to 0 | |
6528 | which severly confuses things, so always regenerate the segment | |
6529 | map in this case. */ | |
6530 | if (segment->p_paddr == 0 | |
6531 | && segment->p_memsz == 0 | |
6532 | && (segment->p_type == PT_INTERP || segment->p_type == PT_DYNAMIC)) | |
cb3ff1e5 | 6533 | goto rewrite; |
5f6999aa | 6534 | |
84d1d650 L |
6535 | for (section = ibfd->sections; |
6536 | section != NULL; section = section->next) | |
6537 | { | |
6538 | /* We mark the output section so that we know it comes | |
6539 | from the input BFD. */ | |
6540 | osec = section->output_section; | |
6541 | if (osec) | |
6542 | osec->segment_mark = TRUE; | |
6543 | ||
6544 | /* Check if this section is covered by the segment. */ | |
6545 | this_hdr = &(elf_section_data(section)->this_hdr); | |
f4638467 | 6546 | if (ELF_SECTION_IN_SEGMENT (this_hdr, segment)) |
84d1d650 L |
6547 | { |
6548 | /* FIXME: Check if its output section is changed or | |
6549 | removed. What else do we need to check? */ | |
6550 | if (osec == NULL | |
6551 | || section->flags != osec->flags | |
6552 | || section->lma != osec->lma | |
6553 | || section->vma != osec->vma | |
6554 | || section->size != osec->size | |
6555 | || section->rawsize != osec->rawsize | |
6556 | || section->alignment_power != osec->alignment_power) | |
6557 | goto rewrite; | |
6558 | } | |
6559 | } | |
6560 | } | |
6561 | ||
cb3ff1e5 | 6562 | /* Check to see if any output section do not come from the |
84d1d650 L |
6563 | input BFD. */ |
6564 | for (section = obfd->sections; section != NULL; | |
6565 | section = section->next) | |
6566 | { | |
6567 | if (section->segment_mark == FALSE) | |
6568 | goto rewrite; | |
6569 | else | |
6570 | section->segment_mark = FALSE; | |
6571 | } | |
6572 | ||
6573 | return copy_elf_program_header (ibfd, obfd); | |
6574 | } | |
6575 | ||
6576 | rewrite: | |
f1d85785 L |
6577 | if (ibfd->xvec == obfd->xvec) |
6578 | { | |
6579 | /* When rewriting program header, set the output maxpagesize to | |
6580 | the maximum alignment of input PT_LOAD segments. */ | |
6581 | Elf_Internal_Phdr *segment; | |
6582 | unsigned int i; | |
6583 | unsigned int num_segments = elf_elfheader (ibfd)->e_phnum; | |
6584 | bfd_vma maxpagesize = 0; | |
6585 | ||
6586 | for (i = 0, segment = elf_tdata (ibfd)->phdr; | |
6587 | i < num_segments; | |
6588 | i++, segment++) | |
6589 | if (segment->p_type == PT_LOAD | |
6590 | && maxpagesize < segment->p_align) | |
c86934ce NC |
6591 | { |
6592 | /* PR 17512: file: f17299af. */ | |
6593 | if (segment->p_align > (bfd_vma) 1 << ((sizeof (bfd_vma) * 8) - 2)) | |
6594 | (*_bfd_error_handler) (_("\ | |
6595 | %B: warning: segment alignment of 0x%llx is too large"), | |
6596 | ibfd, (long long) segment->p_align); | |
6597 | else | |
6598 | maxpagesize = segment->p_align; | |
6599 | } | |
f1d85785 L |
6600 | |
6601 | if (maxpagesize != get_elf_backend_data (obfd)->maxpagesize) | |
6602 | bfd_emul_set_maxpagesize (bfd_get_target (obfd), maxpagesize); | |
6603 | } | |
6604 | ||
84d1d650 L |
6605 | return rewrite_elf_program_header (ibfd, obfd); |
6606 | } | |
6607 | ||
ccd2ec6a L |
6608 | /* Initialize private output section information from input section. */ |
6609 | ||
6610 | bfd_boolean | |
6611 | _bfd_elf_init_private_section_data (bfd *ibfd, | |
6612 | asection *isec, | |
6613 | bfd *obfd, | |
6614 | asection *osec, | |
6615 | struct bfd_link_info *link_info) | |
6616 | ||
6617 | { | |
6618 | Elf_Internal_Shdr *ihdr, *ohdr; | |
dfa7b0b8 | 6619 | bfd_boolean final_link = link_info != NULL && !link_info->relocatable; |
ccd2ec6a L |
6620 | |
6621 | if (ibfd->xvec->flavour != bfd_target_elf_flavour | |
6622 | || obfd->xvec->flavour != bfd_target_elf_flavour) | |
6623 | return TRUE; | |
6624 | ||
ba85c43e NC |
6625 | BFD_ASSERT (elf_section_data (osec) != NULL); |
6626 | ||
dfa7b0b8 AM |
6627 | /* For objcopy and relocatable link, don't copy the output ELF |
6628 | section type from input if the output BFD section flags have been | |
6629 | set to something different. For a final link allow some flags | |
6630 | that the linker clears to differ. */ | |
42bb2e33 | 6631 | if (elf_section_type (osec) == SHT_NULL |
dfa7b0b8 AM |
6632 | && (osec->flags == isec->flags |
6633 | || (final_link | |
6634 | && ((osec->flags ^ isec->flags) | |
0814be7d | 6635 | & ~(SEC_LINK_ONCE | SEC_LINK_DUPLICATES | SEC_RELOC)) == 0))) |
42bb2e33 | 6636 | elf_section_type (osec) = elf_section_type (isec); |
d270463e L |
6637 | |
6638 | /* FIXME: Is this correct for all OS/PROC specific flags? */ | |
6639 | elf_section_flags (osec) |= (elf_section_flags (isec) | |
6640 | & (SHF_MASKOS | SHF_MASKPROC)); | |
ccd2ec6a L |
6641 | |
6642 | /* Set things up for objcopy and relocatable link. The output | |
6643 | SHT_GROUP section will have its elf_next_in_group pointing back | |
6644 | to the input group members. Ignore linker created group section. | |
6645 | See elfNN_ia64_object_p in elfxx-ia64.c. */ | |
dfa7b0b8 | 6646 | if (!final_link) |
ccd2ec6a L |
6647 | { |
6648 | if (elf_sec_group (isec) == NULL | |
6649 | || (elf_sec_group (isec)->flags & SEC_LINKER_CREATED) == 0) | |
6650 | { | |
6651 | if (elf_section_flags (isec) & SHF_GROUP) | |
6652 | elf_section_flags (osec) |= SHF_GROUP; | |
6653 | elf_next_in_group (osec) = elf_next_in_group (isec); | |
9659de1c | 6654 | elf_section_data (osec)->group = elf_section_data (isec)->group; |
ccd2ec6a L |
6655 | } |
6656 | } | |
6657 | ||
6658 | ihdr = &elf_section_data (isec)->this_hdr; | |
6659 | ||
6660 | /* We need to handle elf_linked_to_section for SHF_LINK_ORDER. We | |
6661 | don't use the output section of the linked-to section since it | |
6662 | may be NULL at this point. */ | |
6663 | if ((ihdr->sh_flags & SHF_LINK_ORDER) != 0) | |
6664 | { | |
6665 | ohdr = &elf_section_data (osec)->this_hdr; | |
6666 | ohdr->sh_flags |= SHF_LINK_ORDER; | |
6667 | elf_linked_to_section (osec) = elf_linked_to_section (isec); | |
6668 | } | |
6669 | ||
6670 | osec->use_rela_p = isec->use_rela_p; | |
6671 | ||
6672 | return TRUE; | |
6673 | } | |
6674 | ||
252b5132 RH |
6675 | /* Copy private section information. This copies over the entsize |
6676 | field, and sometimes the info field. */ | |
6677 | ||
b34976b6 | 6678 | bfd_boolean |
217aa764 AM |
6679 | _bfd_elf_copy_private_section_data (bfd *ibfd, |
6680 | asection *isec, | |
6681 | bfd *obfd, | |
6682 | asection *osec) | |
252b5132 RH |
6683 | { |
6684 | Elf_Internal_Shdr *ihdr, *ohdr; | |
6685 | ||
6686 | if (ibfd->xvec->flavour != bfd_target_elf_flavour | |
6687 | || obfd->xvec->flavour != bfd_target_elf_flavour) | |
b34976b6 | 6688 | return TRUE; |
252b5132 | 6689 | |
252b5132 RH |
6690 | ihdr = &elf_section_data (isec)->this_hdr; |
6691 | ohdr = &elf_section_data (osec)->this_hdr; | |
6692 | ||
6693 | ohdr->sh_entsize = ihdr->sh_entsize; | |
6694 | ||
6695 | if (ihdr->sh_type == SHT_SYMTAB | |
6696 | || ihdr->sh_type == SHT_DYNSYM | |
6697 | || ihdr->sh_type == SHT_GNU_verneed | |
6698 | || ihdr->sh_type == SHT_GNU_verdef) | |
6699 | ohdr->sh_info = ihdr->sh_info; | |
6700 | ||
ccd2ec6a L |
6701 | return _bfd_elf_init_private_section_data (ibfd, isec, obfd, osec, |
6702 | NULL); | |
252b5132 RH |
6703 | } |
6704 | ||
d0bf826b AM |
6705 | /* Look at all the SHT_GROUP sections in IBFD, making any adjustments |
6706 | necessary if we are removing either the SHT_GROUP section or any of | |
6707 | the group member sections. DISCARDED is the value that a section's | |
6708 | output_section has if the section will be discarded, NULL when this | |
6709 | function is called from objcopy, bfd_abs_section_ptr when called | |
6710 | from the linker. */ | |
80fccad2 BW |
6711 | |
6712 | bfd_boolean | |
d0bf826b | 6713 | _bfd_elf_fixup_group_sections (bfd *ibfd, asection *discarded) |
80fccad2 | 6714 | { |
30288845 AM |
6715 | asection *isec; |
6716 | ||
30288845 | 6717 | for (isec = ibfd->sections; isec != NULL; isec = isec->next) |
415f38a6 | 6718 | if (elf_section_type (isec) == SHT_GROUP) |
30288845 AM |
6719 | { |
6720 | asection *first = elf_next_in_group (isec); | |
6721 | asection *s = first; | |
d0bf826b AM |
6722 | bfd_size_type removed = 0; |
6723 | ||
30288845 AM |
6724 | while (s != NULL) |
6725 | { | |
415f38a6 AM |
6726 | /* If this member section is being output but the |
6727 | SHT_GROUP section is not, then clear the group info | |
6728 | set up by _bfd_elf_copy_private_section_data. */ | |
d0bf826b AM |
6729 | if (s->output_section != discarded |
6730 | && isec->output_section == discarded) | |
30288845 AM |
6731 | { |
6732 | elf_section_flags (s->output_section) &= ~SHF_GROUP; | |
6733 | elf_group_name (s->output_section) = NULL; | |
6734 | } | |
415f38a6 AM |
6735 | /* Conversely, if the member section is not being output |
6736 | but the SHT_GROUP section is, then adjust its size. */ | |
d0bf826b AM |
6737 | else if (s->output_section == discarded |
6738 | && isec->output_section != discarded) | |
6739 | removed += 4; | |
30288845 AM |
6740 | s = elf_next_in_group (s); |
6741 | if (s == first) | |
6742 | break; | |
6743 | } | |
d0bf826b AM |
6744 | if (removed != 0) |
6745 | { | |
6746 | if (discarded != NULL) | |
6747 | { | |
6748 | /* If we've been called for ld -r, then we need to | |
6749 | adjust the input section size. This function may | |
6750 | be called multiple times, so save the original | |
6751 | size. */ | |
6752 | if (isec->rawsize == 0) | |
6753 | isec->rawsize = isec->size; | |
6754 | isec->size = isec->rawsize - removed; | |
6755 | } | |
6756 | else | |
6757 | { | |
6758 | /* Adjust the output section size when called from | |
6759 | objcopy. */ | |
6760 | isec->output_section->size -= removed; | |
6761 | } | |
6762 | } | |
30288845 AM |
6763 | } |
6764 | ||
80fccad2 BW |
6765 | return TRUE; |
6766 | } | |
6767 | ||
d0bf826b AM |
6768 | /* Copy private header information. */ |
6769 | ||
6770 | bfd_boolean | |
6771 | _bfd_elf_copy_private_header_data (bfd *ibfd, bfd *obfd) | |
6772 | { | |
6773 | if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour | |
6774 | || bfd_get_flavour (obfd) != bfd_target_elf_flavour) | |
6775 | return TRUE; | |
6776 | ||
6777 | /* Copy over private BFD data if it has not already been copied. | |
6778 | This must be done here, rather than in the copy_private_bfd_data | |
6779 | entry point, because the latter is called after the section | |
6780 | contents have been set, which means that the program headers have | |
6781 | already been worked out. */ | |
12bd6957 | 6782 | if (elf_seg_map (obfd) == NULL && elf_tdata (ibfd)->phdr != NULL) |
d0bf826b AM |
6783 | { |
6784 | if (! copy_private_bfd_data (ibfd, obfd)) | |
6785 | return FALSE; | |
6786 | } | |
6787 | ||
6788 | return _bfd_elf_fixup_group_sections (ibfd, NULL); | |
6789 | } | |
6790 | ||
252b5132 RH |
6791 | /* Copy private symbol information. If this symbol is in a section |
6792 | which we did not map into a BFD section, try to map the section | |
6793 | index correctly. We use special macro definitions for the mapped | |
6794 | section indices; these definitions are interpreted by the | |
6795 | swap_out_syms function. */ | |
6796 | ||
9ad5cbcf AM |
6797 | #define MAP_ONESYMTAB (SHN_HIOS + 1) |
6798 | #define MAP_DYNSYMTAB (SHN_HIOS + 2) | |
6799 | #define MAP_STRTAB (SHN_HIOS + 3) | |
6800 | #define MAP_SHSTRTAB (SHN_HIOS + 4) | |
6801 | #define MAP_SYM_SHNDX (SHN_HIOS + 5) | |
252b5132 | 6802 | |
b34976b6 | 6803 | bfd_boolean |
217aa764 AM |
6804 | _bfd_elf_copy_private_symbol_data (bfd *ibfd, |
6805 | asymbol *isymarg, | |
6806 | bfd *obfd, | |
6807 | asymbol *osymarg) | |
252b5132 RH |
6808 | { |
6809 | elf_symbol_type *isym, *osym; | |
6810 | ||
6811 | if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour | |
6812 | || bfd_get_flavour (obfd) != bfd_target_elf_flavour) | |
b34976b6 | 6813 | return TRUE; |
252b5132 RH |
6814 | |
6815 | isym = elf_symbol_from (ibfd, isymarg); | |
6816 | osym = elf_symbol_from (obfd, osymarg); | |
6817 | ||
6818 | if (isym != NULL | |
8424d8f5 | 6819 | && isym->internal_elf_sym.st_shndx != 0 |
252b5132 RH |
6820 | && osym != NULL |
6821 | && bfd_is_abs_section (isym->symbol.section)) | |
6822 | { | |
6823 | unsigned int shndx; | |
6824 | ||
6825 | shndx = isym->internal_elf_sym.st_shndx; | |
6826 | if (shndx == elf_onesymtab (ibfd)) | |
6827 | shndx = MAP_ONESYMTAB; | |
6828 | else if (shndx == elf_dynsymtab (ibfd)) | |
6829 | shndx = MAP_DYNSYMTAB; | |
12bd6957 | 6830 | else if (shndx == elf_strtab_sec (ibfd)) |
252b5132 | 6831 | shndx = MAP_STRTAB; |
12bd6957 | 6832 | else if (shndx == elf_shstrtab_sec (ibfd)) |
252b5132 | 6833 | shndx = MAP_SHSTRTAB; |
12bd6957 | 6834 | else if (shndx == elf_symtab_shndx (ibfd)) |
9ad5cbcf | 6835 | shndx = MAP_SYM_SHNDX; |
252b5132 RH |
6836 | osym->internal_elf_sym.st_shndx = shndx; |
6837 | } | |
6838 | ||
b34976b6 | 6839 | return TRUE; |
252b5132 RH |
6840 | } |
6841 | ||
6842 | /* Swap out the symbols. */ | |
6843 | ||
b34976b6 | 6844 | static bfd_boolean |
217aa764 AM |
6845 | swap_out_syms (bfd *abfd, |
6846 | struct bfd_strtab_hash **sttp, | |
6847 | int relocatable_p) | |
252b5132 | 6848 | { |
9c5bfbb7 | 6849 | const struct elf_backend_data *bed; |
079e9a2f AM |
6850 | int symcount; |
6851 | asymbol **syms; | |
6852 | struct bfd_strtab_hash *stt; | |
6853 | Elf_Internal_Shdr *symtab_hdr; | |
9ad5cbcf | 6854 | Elf_Internal_Shdr *symtab_shndx_hdr; |
079e9a2f | 6855 | Elf_Internal_Shdr *symstrtab_hdr; |
f075ee0c AM |
6856 | bfd_byte *outbound_syms; |
6857 | bfd_byte *outbound_shndx; | |
079e9a2f | 6858 | int idx; |
12bd6957 | 6859 | unsigned int num_locals; |
079e9a2f | 6860 | bfd_size_type amt; |
174fd7f9 | 6861 | bfd_boolean name_local_sections; |
252b5132 | 6862 | |
12bd6957 | 6863 | if (!elf_map_symbols (abfd, &num_locals)) |
b34976b6 | 6864 | return FALSE; |
252b5132 | 6865 | |
c044fabd | 6866 | /* Dump out the symtabs. */ |
079e9a2f AM |
6867 | stt = _bfd_elf_stringtab_init (); |
6868 | if (stt == NULL) | |
b34976b6 | 6869 | return FALSE; |
252b5132 | 6870 | |
079e9a2f AM |
6871 | bed = get_elf_backend_data (abfd); |
6872 | symcount = bfd_get_symcount (abfd); | |
6873 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; | |
6874 | symtab_hdr->sh_type = SHT_SYMTAB; | |
6875 | symtab_hdr->sh_entsize = bed->s->sizeof_sym; | |
6876 | symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1); | |
12bd6957 | 6877 | symtab_hdr->sh_info = num_locals + 1; |
72de5009 | 6878 | symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align; |
079e9a2f AM |
6879 | |
6880 | symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr; | |
6881 | symstrtab_hdr->sh_type = SHT_STRTAB; | |
6882 | ||
a50b1753 NC |
6883 | outbound_syms = (bfd_byte *) bfd_alloc2 (abfd, 1 + symcount, |
6884 | bed->s->sizeof_sym); | |
079e9a2f | 6885 | if (outbound_syms == NULL) |
5ed6aba4 NC |
6886 | { |
6887 | _bfd_stringtab_free (stt); | |
6888 | return FALSE; | |
6889 | } | |
217aa764 | 6890 | symtab_hdr->contents = outbound_syms; |
252b5132 | 6891 | |
9ad5cbcf AM |
6892 | outbound_shndx = NULL; |
6893 | symtab_shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr; | |
6894 | if (symtab_shndx_hdr->sh_name != 0) | |
6895 | { | |
6896 | amt = (bfd_size_type) (1 + symcount) * sizeof (Elf_External_Sym_Shndx); | |
a50b1753 NC |
6897 | outbound_shndx = (bfd_byte *) |
6898 | bfd_zalloc2 (abfd, 1 + symcount, sizeof (Elf_External_Sym_Shndx)); | |
9ad5cbcf | 6899 | if (outbound_shndx == NULL) |
5ed6aba4 NC |
6900 | { |
6901 | _bfd_stringtab_free (stt); | |
6902 | return FALSE; | |
6903 | } | |
6904 | ||
9ad5cbcf AM |
6905 | symtab_shndx_hdr->contents = outbound_shndx; |
6906 | symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX; | |
6907 | symtab_shndx_hdr->sh_size = amt; | |
6908 | symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx); | |
6909 | symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx); | |
6910 | } | |
6911 | ||
589e6347 | 6912 | /* Now generate the data (for "contents"). */ |
079e9a2f AM |
6913 | { |
6914 | /* Fill in zeroth symbol and swap it out. */ | |
6915 | Elf_Internal_Sym sym; | |
6916 | sym.st_name = 0; | |
6917 | sym.st_value = 0; | |
6918 | sym.st_size = 0; | |
6919 | sym.st_info = 0; | |
6920 | sym.st_other = 0; | |
6921 | sym.st_shndx = SHN_UNDEF; | |
35fc36a8 | 6922 | sym.st_target_internal = 0; |
9ad5cbcf | 6923 | bed->s->swap_symbol_out (abfd, &sym, outbound_syms, outbound_shndx); |
079e9a2f | 6924 | outbound_syms += bed->s->sizeof_sym; |
9ad5cbcf AM |
6925 | if (outbound_shndx != NULL) |
6926 | outbound_shndx += sizeof (Elf_External_Sym_Shndx); | |
079e9a2f | 6927 | } |
252b5132 | 6928 | |
174fd7f9 RS |
6929 | name_local_sections |
6930 | = (bed->elf_backend_name_local_section_symbols | |
6931 | && bed->elf_backend_name_local_section_symbols (abfd)); | |
6932 | ||
079e9a2f AM |
6933 | syms = bfd_get_outsymbols (abfd); |
6934 | for (idx = 0; idx < symcount; idx++) | |
252b5132 | 6935 | { |
252b5132 | 6936 | Elf_Internal_Sym sym; |
079e9a2f AM |
6937 | bfd_vma value = syms[idx]->value; |
6938 | elf_symbol_type *type_ptr; | |
6939 | flagword flags = syms[idx]->flags; | |
6940 | int type; | |
252b5132 | 6941 | |
174fd7f9 RS |
6942 | if (!name_local_sections |
6943 | && (flags & (BSF_SECTION_SYM | BSF_GLOBAL)) == BSF_SECTION_SYM) | |
079e9a2f AM |
6944 | { |
6945 | /* Local section symbols have no name. */ | |
6946 | sym.st_name = 0; | |
6947 | } | |
6948 | else | |
6949 | { | |
6950 | sym.st_name = (unsigned long) _bfd_stringtab_add (stt, | |
6951 | syms[idx]->name, | |
b34976b6 | 6952 | TRUE, FALSE); |
079e9a2f | 6953 | if (sym.st_name == (unsigned long) -1) |
5ed6aba4 NC |
6954 | { |
6955 | _bfd_stringtab_free (stt); | |
6956 | return FALSE; | |
6957 | } | |
079e9a2f | 6958 | } |
252b5132 | 6959 | |
079e9a2f | 6960 | type_ptr = elf_symbol_from (abfd, syms[idx]); |
252b5132 | 6961 | |
079e9a2f AM |
6962 | if ((flags & BSF_SECTION_SYM) == 0 |
6963 | && bfd_is_com_section (syms[idx]->section)) | |
6964 | { | |
6965 | /* ELF common symbols put the alignment into the `value' field, | |
6966 | and the size into the `size' field. This is backwards from | |
6967 | how BFD handles it, so reverse it here. */ | |
6968 | sym.st_size = value; | |
6969 | if (type_ptr == NULL | |
6970 | || type_ptr->internal_elf_sym.st_value == 0) | |
6971 | sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value)); | |
6972 | else | |
6973 | sym.st_value = type_ptr->internal_elf_sym.st_value; | |
6974 | sym.st_shndx = _bfd_elf_section_from_bfd_section | |
6975 | (abfd, syms[idx]->section); | |
6976 | } | |
6977 | else | |
6978 | { | |
6979 | asection *sec = syms[idx]->section; | |
cb33740c | 6980 | unsigned int shndx; |
252b5132 | 6981 | |
079e9a2f AM |
6982 | if (sec->output_section) |
6983 | { | |
6984 | value += sec->output_offset; | |
6985 | sec = sec->output_section; | |
6986 | } | |
589e6347 | 6987 | |
079e9a2f AM |
6988 | /* Don't add in the section vma for relocatable output. */ |
6989 | if (! relocatable_p) | |
6990 | value += sec->vma; | |
6991 | sym.st_value = value; | |
6992 | sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0; | |
6993 | ||
6994 | if (bfd_is_abs_section (sec) | |
6995 | && type_ptr != NULL | |
6996 | && type_ptr->internal_elf_sym.st_shndx != 0) | |
6997 | { | |
6998 | /* This symbol is in a real ELF section which we did | |
6999 | not create as a BFD section. Undo the mapping done | |
7000 | by copy_private_symbol_data. */ | |
7001 | shndx = type_ptr->internal_elf_sym.st_shndx; | |
7002 | switch (shndx) | |
7003 | { | |
7004 | case MAP_ONESYMTAB: | |
7005 | shndx = elf_onesymtab (abfd); | |
7006 | break; | |
7007 | case MAP_DYNSYMTAB: | |
7008 | shndx = elf_dynsymtab (abfd); | |
7009 | break; | |
7010 | case MAP_STRTAB: | |
12bd6957 | 7011 | shndx = elf_strtab_sec (abfd); |
079e9a2f AM |
7012 | break; |
7013 | case MAP_SHSTRTAB: | |
12bd6957 | 7014 | shndx = elf_shstrtab_sec (abfd); |
079e9a2f | 7015 | break; |
9ad5cbcf | 7016 | case MAP_SYM_SHNDX: |
12bd6957 | 7017 | shndx = elf_symtab_shndx (abfd); |
9ad5cbcf | 7018 | break; |
079e9a2f | 7019 | default: |
15bc576a | 7020 | shndx = SHN_ABS; |
079e9a2f AM |
7021 | break; |
7022 | } | |
7023 | } | |
7024 | else | |
7025 | { | |
7026 | shndx = _bfd_elf_section_from_bfd_section (abfd, sec); | |
252b5132 | 7027 | |
cb33740c | 7028 | if (shndx == SHN_BAD) |
079e9a2f AM |
7029 | { |
7030 | asection *sec2; | |
7031 | ||
7032 | /* Writing this would be a hell of a lot easier if | |
7033 | we had some decent documentation on bfd, and | |
7034 | knew what to expect of the library, and what to | |
7035 | demand of applications. For example, it | |
7036 | appears that `objcopy' might not set the | |
7037 | section of a symbol to be a section that is | |
7038 | actually in the output file. */ | |
7039 | sec2 = bfd_get_section_by_name (abfd, sec->name); | |
589e6347 NC |
7040 | if (sec2 == NULL) |
7041 | { | |
7042 | _bfd_error_handler (_("\ | |
7043 | Unable to find equivalent output section for symbol '%s' from section '%s'"), | |
7044 | syms[idx]->name ? syms[idx]->name : "<Local sym>", | |
7045 | sec->name); | |
811072d8 | 7046 | bfd_set_error (bfd_error_invalid_operation); |
5ed6aba4 | 7047 | _bfd_stringtab_free (stt); |
589e6347 NC |
7048 | return FALSE; |
7049 | } | |
811072d8 | 7050 | |
079e9a2f | 7051 | shndx = _bfd_elf_section_from_bfd_section (abfd, sec2); |
cb33740c | 7052 | BFD_ASSERT (shndx != SHN_BAD); |
079e9a2f AM |
7053 | } |
7054 | } | |
252b5132 | 7055 | |
079e9a2f AM |
7056 | sym.st_shndx = shndx; |
7057 | } | |
252b5132 | 7058 | |
13ae64f3 JJ |
7059 | if ((flags & BSF_THREAD_LOCAL) != 0) |
7060 | type = STT_TLS; | |
d8045f23 NC |
7061 | else if ((flags & BSF_GNU_INDIRECT_FUNCTION) != 0) |
7062 | type = STT_GNU_IFUNC; | |
13ae64f3 | 7063 | else if ((flags & BSF_FUNCTION) != 0) |
079e9a2f AM |
7064 | type = STT_FUNC; |
7065 | else if ((flags & BSF_OBJECT) != 0) | |
7066 | type = STT_OBJECT; | |
d9352518 DB |
7067 | else if ((flags & BSF_RELC) != 0) |
7068 | type = STT_RELC; | |
7069 | else if ((flags & BSF_SRELC) != 0) | |
7070 | type = STT_SRELC; | |
079e9a2f AM |
7071 | else |
7072 | type = STT_NOTYPE; | |
252b5132 | 7073 | |
13ae64f3 JJ |
7074 | if (syms[idx]->section->flags & SEC_THREAD_LOCAL) |
7075 | type = STT_TLS; | |
7076 | ||
589e6347 | 7077 | /* Processor-specific types. */ |
079e9a2f AM |
7078 | if (type_ptr != NULL |
7079 | && bed->elf_backend_get_symbol_type) | |
7080 | type = ((*bed->elf_backend_get_symbol_type) | |
7081 | (&type_ptr->internal_elf_sym, type)); | |
252b5132 | 7082 | |
079e9a2f AM |
7083 | if (flags & BSF_SECTION_SYM) |
7084 | { | |
7085 | if (flags & BSF_GLOBAL) | |
7086 | sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION); | |
7087 | else | |
7088 | sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION); | |
7089 | } | |
7090 | else if (bfd_is_com_section (syms[idx]->section)) | |
0a40daed | 7091 | { |
504b7d20 | 7092 | #ifdef USE_STT_COMMON |
0a40daed MK |
7093 | if (type == STT_OBJECT) |
7094 | sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_COMMON); | |
7095 | else | |
504b7d20 | 7096 | #endif |
c91e322a | 7097 | sym.st_info = ELF_ST_INFO (STB_GLOBAL, type); |
0a40daed | 7098 | } |
079e9a2f AM |
7099 | else if (bfd_is_und_section (syms[idx]->section)) |
7100 | sym.st_info = ELF_ST_INFO (((flags & BSF_WEAK) | |
7101 | ? STB_WEAK | |
7102 | : STB_GLOBAL), | |
7103 | type); | |
7104 | else if (flags & BSF_FILE) | |
7105 | sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE); | |
7106 | else | |
7107 | { | |
7108 | int bind = STB_LOCAL; | |
252b5132 | 7109 | |
079e9a2f AM |
7110 | if (flags & BSF_LOCAL) |
7111 | bind = STB_LOCAL; | |
3e7a7d11 NC |
7112 | else if (flags & BSF_GNU_UNIQUE) |
7113 | bind = STB_GNU_UNIQUE; | |
079e9a2f AM |
7114 | else if (flags & BSF_WEAK) |
7115 | bind = STB_WEAK; | |
7116 | else if (flags & BSF_GLOBAL) | |
7117 | bind = STB_GLOBAL; | |
252b5132 | 7118 | |
079e9a2f AM |
7119 | sym.st_info = ELF_ST_INFO (bind, type); |
7120 | } | |
252b5132 | 7121 | |
079e9a2f | 7122 | if (type_ptr != NULL) |
35fc36a8 RS |
7123 | { |
7124 | sym.st_other = type_ptr->internal_elf_sym.st_other; | |
7125 | sym.st_target_internal | |
7126 | = type_ptr->internal_elf_sym.st_target_internal; | |
7127 | } | |
079e9a2f | 7128 | else |
35fc36a8 RS |
7129 | { |
7130 | sym.st_other = 0; | |
7131 | sym.st_target_internal = 0; | |
7132 | } | |
252b5132 | 7133 | |
9ad5cbcf | 7134 | bed->s->swap_symbol_out (abfd, &sym, outbound_syms, outbound_shndx); |
079e9a2f | 7135 | outbound_syms += bed->s->sizeof_sym; |
9ad5cbcf AM |
7136 | if (outbound_shndx != NULL) |
7137 | outbound_shndx += sizeof (Elf_External_Sym_Shndx); | |
079e9a2f | 7138 | } |
252b5132 | 7139 | |
079e9a2f AM |
7140 | *sttp = stt; |
7141 | symstrtab_hdr->sh_size = _bfd_stringtab_size (stt); | |
7142 | symstrtab_hdr->sh_type = SHT_STRTAB; | |
252b5132 | 7143 | |
079e9a2f AM |
7144 | symstrtab_hdr->sh_flags = 0; |
7145 | symstrtab_hdr->sh_addr = 0; | |
7146 | symstrtab_hdr->sh_entsize = 0; | |
7147 | symstrtab_hdr->sh_link = 0; | |
7148 | symstrtab_hdr->sh_info = 0; | |
7149 | symstrtab_hdr->sh_addralign = 1; | |
252b5132 | 7150 | |
b34976b6 | 7151 | return TRUE; |
252b5132 RH |
7152 | } |
7153 | ||
7154 | /* Return the number of bytes required to hold the symtab vector. | |
7155 | ||
7156 | Note that we base it on the count plus 1, since we will null terminate | |
7157 | the vector allocated based on this size. However, the ELF symbol table | |
7158 | always has a dummy entry as symbol #0, so it ends up even. */ | |
7159 | ||
7160 | long | |
217aa764 | 7161 | _bfd_elf_get_symtab_upper_bound (bfd *abfd) |
252b5132 RH |
7162 | { |
7163 | long symcount; | |
7164 | long symtab_size; | |
7165 | Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr; | |
7166 | ||
7167 | symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym; | |
b99d1833 AM |
7168 | symtab_size = (symcount + 1) * (sizeof (asymbol *)); |
7169 | if (symcount > 0) | |
7170 | symtab_size -= sizeof (asymbol *); | |
252b5132 RH |
7171 | |
7172 | return symtab_size; | |
7173 | } | |
7174 | ||
7175 | long | |
217aa764 | 7176 | _bfd_elf_get_dynamic_symtab_upper_bound (bfd *abfd) |
252b5132 RH |
7177 | { |
7178 | long symcount; | |
7179 | long symtab_size; | |
7180 | Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr; | |
7181 | ||
7182 | if (elf_dynsymtab (abfd) == 0) | |
7183 | { | |
7184 | bfd_set_error (bfd_error_invalid_operation); | |
7185 | return -1; | |
7186 | } | |
7187 | ||
7188 | symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym; | |
b99d1833 AM |
7189 | symtab_size = (symcount + 1) * (sizeof (asymbol *)); |
7190 | if (symcount > 0) | |
7191 | symtab_size -= sizeof (asymbol *); | |
252b5132 RH |
7192 | |
7193 | return symtab_size; | |
7194 | } | |
7195 | ||
7196 | long | |
217aa764 AM |
7197 | _bfd_elf_get_reloc_upper_bound (bfd *abfd ATTRIBUTE_UNUSED, |
7198 | sec_ptr asect) | |
252b5132 RH |
7199 | { |
7200 | return (asect->reloc_count + 1) * sizeof (arelent *); | |
7201 | } | |
7202 | ||
7203 | /* Canonicalize the relocs. */ | |
7204 | ||
7205 | long | |
217aa764 AM |
7206 | _bfd_elf_canonicalize_reloc (bfd *abfd, |
7207 | sec_ptr section, | |
7208 | arelent **relptr, | |
7209 | asymbol **symbols) | |
252b5132 RH |
7210 | { |
7211 | arelent *tblptr; | |
7212 | unsigned int i; | |
9c5bfbb7 | 7213 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
252b5132 | 7214 | |
b34976b6 | 7215 | if (! bed->s->slurp_reloc_table (abfd, section, symbols, FALSE)) |
252b5132 RH |
7216 | return -1; |
7217 | ||
7218 | tblptr = section->relocation; | |
7219 | for (i = 0; i < section->reloc_count; i++) | |
7220 | *relptr++ = tblptr++; | |
7221 | ||
7222 | *relptr = NULL; | |
7223 | ||
7224 | return section->reloc_count; | |
7225 | } | |
7226 | ||
7227 | long | |
6cee3f79 | 7228 | _bfd_elf_canonicalize_symtab (bfd *abfd, asymbol **allocation) |
252b5132 | 7229 | { |
9c5bfbb7 | 7230 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
217aa764 | 7231 | long symcount = bed->s->slurp_symbol_table (abfd, allocation, FALSE); |
252b5132 RH |
7232 | |
7233 | if (symcount >= 0) | |
7234 | bfd_get_symcount (abfd) = symcount; | |
7235 | return symcount; | |
7236 | } | |
7237 | ||
7238 | long | |
217aa764 AM |
7239 | _bfd_elf_canonicalize_dynamic_symtab (bfd *abfd, |
7240 | asymbol **allocation) | |
252b5132 | 7241 | { |
9c5bfbb7 | 7242 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
217aa764 | 7243 | long symcount = bed->s->slurp_symbol_table (abfd, allocation, TRUE); |
1f70368c DJ |
7244 | |
7245 | if (symcount >= 0) | |
7246 | bfd_get_dynamic_symcount (abfd) = symcount; | |
7247 | return symcount; | |
252b5132 RH |
7248 | } |
7249 | ||
8615f3f2 AM |
7250 | /* Return the size required for the dynamic reloc entries. Any loadable |
7251 | section that was actually installed in the BFD, and has type SHT_REL | |
7252 | or SHT_RELA, and uses the dynamic symbol table, is considered to be a | |
7253 | dynamic reloc section. */ | |
252b5132 RH |
7254 | |
7255 | long | |
217aa764 | 7256 | _bfd_elf_get_dynamic_reloc_upper_bound (bfd *abfd) |
252b5132 RH |
7257 | { |
7258 | long ret; | |
7259 | asection *s; | |
7260 | ||
7261 | if (elf_dynsymtab (abfd) == 0) | |
7262 | { | |
7263 | bfd_set_error (bfd_error_invalid_operation); | |
7264 | return -1; | |
7265 | } | |
7266 | ||
7267 | ret = sizeof (arelent *); | |
7268 | for (s = abfd->sections; s != NULL; s = s->next) | |
266b05cf | 7269 | if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd) |
252b5132 RH |
7270 | && (elf_section_data (s)->this_hdr.sh_type == SHT_REL |
7271 | || elf_section_data (s)->this_hdr.sh_type == SHT_RELA)) | |
eea6121a | 7272 | ret += ((s->size / elf_section_data (s)->this_hdr.sh_entsize) |
252b5132 RH |
7273 | * sizeof (arelent *)); |
7274 | ||
7275 | return ret; | |
7276 | } | |
7277 | ||
8615f3f2 AM |
7278 | /* Canonicalize the dynamic relocation entries. Note that we return the |
7279 | dynamic relocations as a single block, although they are actually | |
7280 | associated with particular sections; the interface, which was | |
7281 | designed for SunOS style shared libraries, expects that there is only | |
7282 | one set of dynamic relocs. Any loadable section that was actually | |
7283 | installed in the BFD, and has type SHT_REL or SHT_RELA, and uses the | |
7284 | dynamic symbol table, is considered to be a dynamic reloc section. */ | |
252b5132 RH |
7285 | |
7286 | long | |
217aa764 AM |
7287 | _bfd_elf_canonicalize_dynamic_reloc (bfd *abfd, |
7288 | arelent **storage, | |
7289 | asymbol **syms) | |
252b5132 | 7290 | { |
217aa764 | 7291 | bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean); |
252b5132 RH |
7292 | asection *s; |
7293 | long ret; | |
7294 | ||
7295 | if (elf_dynsymtab (abfd) == 0) | |
7296 | { | |
7297 | bfd_set_error (bfd_error_invalid_operation); | |
7298 | return -1; | |
7299 | } | |
7300 | ||
7301 | slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table; | |
7302 | ret = 0; | |
7303 | for (s = abfd->sections; s != NULL; s = s->next) | |
7304 | { | |
266b05cf | 7305 | if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd) |
252b5132 RH |
7306 | && (elf_section_data (s)->this_hdr.sh_type == SHT_REL |
7307 | || elf_section_data (s)->this_hdr.sh_type == SHT_RELA)) | |
7308 | { | |
7309 | arelent *p; | |
7310 | long count, i; | |
7311 | ||
b34976b6 | 7312 | if (! (*slurp_relocs) (abfd, s, syms, TRUE)) |
252b5132 | 7313 | return -1; |
eea6121a | 7314 | count = s->size / elf_section_data (s)->this_hdr.sh_entsize; |
252b5132 RH |
7315 | p = s->relocation; |
7316 | for (i = 0; i < count; i++) | |
7317 | *storage++ = p++; | |
7318 | ret += count; | |
7319 | } | |
7320 | } | |
7321 | ||
7322 | *storage = NULL; | |
7323 | ||
7324 | return ret; | |
7325 | } | |
7326 | \f | |
7327 | /* Read in the version information. */ | |
7328 | ||
b34976b6 | 7329 | bfd_boolean |
fc0e6df6 | 7330 | _bfd_elf_slurp_version_tables (bfd *abfd, bfd_boolean default_imported_symver) |
252b5132 RH |
7331 | { |
7332 | bfd_byte *contents = NULL; | |
fc0e6df6 PB |
7333 | unsigned int freeidx = 0; |
7334 | ||
7335 | if (elf_dynverref (abfd) != 0) | |
7336 | { | |
7337 | Elf_Internal_Shdr *hdr; | |
7338 | Elf_External_Verneed *everneed; | |
7339 | Elf_Internal_Verneed *iverneed; | |
7340 | unsigned int i; | |
d0fb9a8d | 7341 | bfd_byte *contents_end; |
fc0e6df6 PB |
7342 | |
7343 | hdr = &elf_tdata (abfd)->dynverref_hdr; | |
7344 | ||
601a03ba | 7345 | if (hdr->sh_info == 0 || hdr->sh_size < sizeof (Elf_External_Verneed)) |
d0fb9a8d | 7346 | { |
601a03ba AM |
7347 | error_return_bad_verref: |
7348 | (*_bfd_error_handler) | |
7349 | (_("%B: .gnu.version_r invalid entry"), abfd); | |
7350 | bfd_set_error (bfd_error_bad_value); | |
d0fb9a8d JJ |
7351 | error_return_verref: |
7352 | elf_tdata (abfd)->verref = NULL; | |
7353 | elf_tdata (abfd)->cverrefs = 0; | |
7354 | goto error_return; | |
7355 | } | |
601a03ba AM |
7356 | |
7357 | contents = (bfd_byte *) bfd_malloc (hdr->sh_size); | |
7358 | if (contents == NULL) | |
7359 | goto error_return_verref; | |
7360 | ||
fc0e6df6 PB |
7361 | if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0 |
7362 | || bfd_bread (contents, hdr->sh_size, abfd) != hdr->sh_size) | |
d0fb9a8d | 7363 | goto error_return_verref; |
fc0e6df6 | 7364 | |
601a03ba AM |
7365 | elf_tdata (abfd)->verref = (Elf_Internal_Verneed *) |
7366 | bfd_zalloc2 (abfd, hdr->sh_info, sizeof (Elf_Internal_Verneed)); | |
7367 | ||
7368 | if (elf_tdata (abfd)->verref == NULL) | |
d0fb9a8d JJ |
7369 | goto error_return_verref; |
7370 | ||
7371 | BFD_ASSERT (sizeof (Elf_External_Verneed) | |
7372 | == sizeof (Elf_External_Vernaux)); | |
7373 | contents_end = contents + hdr->sh_size - sizeof (Elf_External_Verneed); | |
fc0e6df6 PB |
7374 | everneed = (Elf_External_Verneed *) contents; |
7375 | iverneed = elf_tdata (abfd)->verref; | |
7376 | for (i = 0; i < hdr->sh_info; i++, iverneed++) | |
7377 | { | |
7378 | Elf_External_Vernaux *evernaux; | |
7379 | Elf_Internal_Vernaux *ivernaux; | |
7380 | unsigned int j; | |
7381 | ||
7382 | _bfd_elf_swap_verneed_in (abfd, everneed, iverneed); | |
7383 | ||
7384 | iverneed->vn_bfd = abfd; | |
7385 | ||
7386 | iverneed->vn_filename = | |
7387 | bfd_elf_string_from_elf_section (abfd, hdr->sh_link, | |
7388 | iverneed->vn_file); | |
7389 | if (iverneed->vn_filename == NULL) | |
601a03ba | 7390 | goto error_return_bad_verref; |
fc0e6df6 | 7391 | |
d0fb9a8d JJ |
7392 | if (iverneed->vn_cnt == 0) |
7393 | iverneed->vn_auxptr = NULL; | |
7394 | else | |
7395 | { | |
a50b1753 NC |
7396 | iverneed->vn_auxptr = (struct elf_internal_vernaux *) |
7397 | bfd_alloc2 (abfd, iverneed->vn_cnt, | |
7398 | sizeof (Elf_Internal_Vernaux)); | |
d0fb9a8d JJ |
7399 | if (iverneed->vn_auxptr == NULL) |
7400 | goto error_return_verref; | |
7401 | } | |
7402 | ||
7403 | if (iverneed->vn_aux | |
7404 | > (size_t) (contents_end - (bfd_byte *) everneed)) | |
601a03ba | 7405 | goto error_return_bad_verref; |
fc0e6df6 PB |
7406 | |
7407 | evernaux = ((Elf_External_Vernaux *) | |
7408 | ((bfd_byte *) everneed + iverneed->vn_aux)); | |
7409 | ivernaux = iverneed->vn_auxptr; | |
7410 | for (j = 0; j < iverneed->vn_cnt; j++, ivernaux++) | |
7411 | { | |
7412 | _bfd_elf_swap_vernaux_in (abfd, evernaux, ivernaux); | |
7413 | ||
7414 | ivernaux->vna_nodename = | |
7415 | bfd_elf_string_from_elf_section (abfd, hdr->sh_link, | |
7416 | ivernaux->vna_name); | |
7417 | if (ivernaux->vna_nodename == NULL) | |
601a03ba | 7418 | goto error_return_bad_verref; |
fc0e6df6 | 7419 | |
25ff461f AM |
7420 | if (ivernaux->vna_other > freeidx) |
7421 | freeidx = ivernaux->vna_other; | |
7422 | ||
7423 | ivernaux->vna_nextptr = NULL; | |
7424 | if (ivernaux->vna_next == 0) | |
7425 | { | |
7426 | iverneed->vn_cnt = j + 1; | |
7427 | break; | |
7428 | } | |
fc0e6df6 PB |
7429 | if (j + 1 < iverneed->vn_cnt) |
7430 | ivernaux->vna_nextptr = ivernaux + 1; | |
fc0e6df6 | 7431 | |
d0fb9a8d JJ |
7432 | if (ivernaux->vna_next |
7433 | > (size_t) (contents_end - (bfd_byte *) evernaux)) | |
601a03ba | 7434 | goto error_return_bad_verref; |
d0fb9a8d | 7435 | |
fc0e6df6 PB |
7436 | evernaux = ((Elf_External_Vernaux *) |
7437 | ((bfd_byte *) evernaux + ivernaux->vna_next)); | |
fc0e6df6 PB |
7438 | } |
7439 | ||
25ff461f AM |
7440 | iverneed->vn_nextref = NULL; |
7441 | if (iverneed->vn_next == 0) | |
7442 | break; | |
fc0e6df6 PB |
7443 | if (i + 1 < hdr->sh_info) |
7444 | iverneed->vn_nextref = iverneed + 1; | |
fc0e6df6 | 7445 | |
d0fb9a8d JJ |
7446 | if (iverneed->vn_next |
7447 | > (size_t) (contents_end - (bfd_byte *) everneed)) | |
601a03ba | 7448 | goto error_return_bad_verref; |
d0fb9a8d | 7449 | |
fc0e6df6 PB |
7450 | everneed = ((Elf_External_Verneed *) |
7451 | ((bfd_byte *) everneed + iverneed->vn_next)); | |
7452 | } | |
25ff461f | 7453 | elf_tdata (abfd)->cverrefs = i; |
fc0e6df6 PB |
7454 | |
7455 | free (contents); | |
7456 | contents = NULL; | |
7457 | } | |
252b5132 RH |
7458 | |
7459 | if (elf_dynverdef (abfd) != 0) | |
7460 | { | |
7461 | Elf_Internal_Shdr *hdr; | |
7462 | Elf_External_Verdef *everdef; | |
7463 | Elf_Internal_Verdef *iverdef; | |
f631889e UD |
7464 | Elf_Internal_Verdef *iverdefarr; |
7465 | Elf_Internal_Verdef iverdefmem; | |
252b5132 | 7466 | unsigned int i; |
062e2358 | 7467 | unsigned int maxidx; |
d0fb9a8d | 7468 | bfd_byte *contents_end_def, *contents_end_aux; |
252b5132 RH |
7469 | |
7470 | hdr = &elf_tdata (abfd)->dynverdef_hdr; | |
7471 | ||
601a03ba AM |
7472 | if (hdr->sh_info == 0 || hdr->sh_size < sizeof (Elf_External_Verdef)) |
7473 | { | |
7474 | error_return_bad_verdef: | |
7475 | (*_bfd_error_handler) | |
7476 | (_("%B: .gnu.version_d invalid entry"), abfd); | |
7477 | bfd_set_error (bfd_error_bad_value); | |
7478 | error_return_verdef: | |
7479 | elf_tdata (abfd)->verdef = NULL; | |
7480 | elf_tdata (abfd)->cverdefs = 0; | |
7481 | goto error_return; | |
7482 | } | |
7483 | ||
a50b1753 | 7484 | contents = (bfd_byte *) bfd_malloc (hdr->sh_size); |
252b5132 | 7485 | if (contents == NULL) |
601a03ba | 7486 | goto error_return_verdef; |
252b5132 | 7487 | if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0 |
217aa764 | 7488 | || bfd_bread (contents, hdr->sh_size, abfd) != hdr->sh_size) |
601a03ba | 7489 | goto error_return_verdef; |
d0fb9a8d JJ |
7490 | |
7491 | BFD_ASSERT (sizeof (Elf_External_Verdef) | |
7492 | >= sizeof (Elf_External_Verdaux)); | |
7493 | contents_end_def = contents + hdr->sh_size | |
7494 | - sizeof (Elf_External_Verdef); | |
7495 | contents_end_aux = contents + hdr->sh_size | |
7496 | - sizeof (Elf_External_Verdaux); | |
7497 | ||
f631889e UD |
7498 | /* We know the number of entries in the section but not the maximum |
7499 | index. Therefore we have to run through all entries and find | |
7500 | the maximum. */ | |
252b5132 | 7501 | everdef = (Elf_External_Verdef *) contents; |
f631889e UD |
7502 | maxidx = 0; |
7503 | for (i = 0; i < hdr->sh_info; ++i) | |
7504 | { | |
7505 | _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem); | |
7506 | ||
601a03ba AM |
7507 | if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) == 0) |
7508 | goto error_return_bad_verdef; | |
062e2358 AM |
7509 | if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) > maxidx) |
7510 | maxidx = iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION); | |
f631889e | 7511 | |
25ff461f AM |
7512 | if (iverdefmem.vd_next == 0) |
7513 | break; | |
7514 | ||
d0fb9a8d JJ |
7515 | if (iverdefmem.vd_next |
7516 | > (size_t) (contents_end_def - (bfd_byte *) everdef)) | |
601a03ba | 7517 | goto error_return_bad_verdef; |
d0fb9a8d | 7518 | |
f631889e UD |
7519 | everdef = ((Elf_External_Verdef *) |
7520 | ((bfd_byte *) everdef + iverdefmem.vd_next)); | |
7521 | } | |
7522 | ||
fc0e6df6 PB |
7523 | if (default_imported_symver) |
7524 | { | |
7525 | if (freeidx > maxidx) | |
7526 | maxidx = ++freeidx; | |
7527 | else | |
7528 | freeidx = ++maxidx; | |
7529 | } | |
201159ec | 7530 | |
601a03ba AM |
7531 | elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *) |
7532 | bfd_zalloc2 (abfd, maxidx, sizeof (Elf_Internal_Verdef)); | |
f631889e | 7533 | if (elf_tdata (abfd)->verdef == NULL) |
601a03ba | 7534 | goto error_return_verdef; |
f631889e UD |
7535 | |
7536 | elf_tdata (abfd)->cverdefs = maxidx; | |
7537 | ||
7538 | everdef = (Elf_External_Verdef *) contents; | |
7539 | iverdefarr = elf_tdata (abfd)->verdef; | |
7540 | for (i = 0; i < hdr->sh_info; i++) | |
252b5132 RH |
7541 | { |
7542 | Elf_External_Verdaux *everdaux; | |
7543 | Elf_Internal_Verdaux *iverdaux; | |
7544 | unsigned int j; | |
7545 | ||
f631889e UD |
7546 | _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem); |
7547 | ||
d0fb9a8d | 7548 | if ((iverdefmem.vd_ndx & VERSYM_VERSION) == 0) |
601a03ba | 7549 | goto error_return_bad_verdef; |
d0fb9a8d | 7550 | |
f631889e UD |
7551 | iverdef = &iverdefarr[(iverdefmem.vd_ndx & VERSYM_VERSION) - 1]; |
7552 | memcpy (iverdef, &iverdefmem, sizeof (Elf_Internal_Verdef)); | |
252b5132 RH |
7553 | |
7554 | iverdef->vd_bfd = abfd; | |
7555 | ||
d0fb9a8d JJ |
7556 | if (iverdef->vd_cnt == 0) |
7557 | iverdef->vd_auxptr = NULL; | |
7558 | else | |
7559 | { | |
a50b1753 NC |
7560 | iverdef->vd_auxptr = (struct elf_internal_verdaux *) |
7561 | bfd_alloc2 (abfd, iverdef->vd_cnt, | |
7562 | sizeof (Elf_Internal_Verdaux)); | |
d0fb9a8d JJ |
7563 | if (iverdef->vd_auxptr == NULL) |
7564 | goto error_return_verdef; | |
7565 | } | |
7566 | ||
7567 | if (iverdef->vd_aux | |
7568 | > (size_t) (contents_end_aux - (bfd_byte *) everdef)) | |
601a03ba | 7569 | goto error_return_bad_verdef; |
252b5132 RH |
7570 | |
7571 | everdaux = ((Elf_External_Verdaux *) | |
7572 | ((bfd_byte *) everdef + iverdef->vd_aux)); | |
7573 | iverdaux = iverdef->vd_auxptr; | |
7574 | for (j = 0; j < iverdef->vd_cnt; j++, iverdaux++) | |
7575 | { | |
7576 | _bfd_elf_swap_verdaux_in (abfd, everdaux, iverdaux); | |
7577 | ||
7578 | iverdaux->vda_nodename = | |
7579 | bfd_elf_string_from_elf_section (abfd, hdr->sh_link, | |
7580 | iverdaux->vda_name); | |
7581 | if (iverdaux->vda_nodename == NULL) | |
601a03ba | 7582 | goto error_return_bad_verdef; |
252b5132 | 7583 | |
25ff461f AM |
7584 | iverdaux->vda_nextptr = NULL; |
7585 | if (iverdaux->vda_next == 0) | |
7586 | { | |
7587 | iverdef->vd_cnt = j + 1; | |
7588 | break; | |
7589 | } | |
252b5132 RH |
7590 | if (j + 1 < iverdef->vd_cnt) |
7591 | iverdaux->vda_nextptr = iverdaux + 1; | |
252b5132 | 7592 | |
d0fb9a8d JJ |
7593 | if (iverdaux->vda_next |
7594 | > (size_t) (contents_end_aux - (bfd_byte *) everdaux)) | |
601a03ba | 7595 | goto error_return_bad_verdef; |
d0fb9a8d | 7596 | |
252b5132 RH |
7597 | everdaux = ((Elf_External_Verdaux *) |
7598 | ((bfd_byte *) everdaux + iverdaux->vda_next)); | |
7599 | } | |
7600 | ||
d0fb9a8d JJ |
7601 | if (iverdef->vd_cnt) |
7602 | iverdef->vd_nodename = iverdef->vd_auxptr->vda_nodename; | |
252b5132 | 7603 | |
25ff461f AM |
7604 | iverdef->vd_nextdef = NULL; |
7605 | if (iverdef->vd_next == 0) | |
7606 | break; | |
d0fb9a8d | 7607 | if ((size_t) (iverdef - iverdefarr) + 1 < maxidx) |
252b5132 | 7608 | iverdef->vd_nextdef = iverdef + 1; |
252b5132 RH |
7609 | |
7610 | everdef = ((Elf_External_Verdef *) | |
7611 | ((bfd_byte *) everdef + iverdef->vd_next)); | |
7612 | } | |
7613 | ||
7614 | free (contents); | |
7615 | contents = NULL; | |
7616 | } | |
fc0e6df6 | 7617 | else if (default_imported_symver) |
252b5132 | 7618 | { |
fc0e6df6 PB |
7619 | if (freeidx < 3) |
7620 | freeidx = 3; | |
7621 | else | |
7622 | freeidx++; | |
252b5132 | 7623 | |
a50b1753 NC |
7624 | elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *) |
7625 | bfd_zalloc2 (abfd, freeidx, sizeof (Elf_Internal_Verdef)); | |
fc0e6df6 | 7626 | if (elf_tdata (abfd)->verdef == NULL) |
252b5132 RH |
7627 | goto error_return; |
7628 | ||
fc0e6df6 PB |
7629 | elf_tdata (abfd)->cverdefs = freeidx; |
7630 | } | |
252b5132 | 7631 | |
fc0e6df6 PB |
7632 | /* Create a default version based on the soname. */ |
7633 | if (default_imported_symver) | |
7634 | { | |
7635 | Elf_Internal_Verdef *iverdef; | |
7636 | Elf_Internal_Verdaux *iverdaux; | |
252b5132 | 7637 | |
5bb3703f | 7638 | iverdef = &elf_tdata (abfd)->verdef[freeidx - 1]; |
252b5132 | 7639 | |
fc0e6df6 PB |
7640 | iverdef->vd_version = VER_DEF_CURRENT; |
7641 | iverdef->vd_flags = 0; | |
7642 | iverdef->vd_ndx = freeidx; | |
7643 | iverdef->vd_cnt = 1; | |
252b5132 | 7644 | |
fc0e6df6 | 7645 | iverdef->vd_bfd = abfd; |
252b5132 | 7646 | |
fc0e6df6 PB |
7647 | iverdef->vd_nodename = bfd_elf_get_dt_soname (abfd); |
7648 | if (iverdef->vd_nodename == NULL) | |
d0fb9a8d | 7649 | goto error_return_verdef; |
fc0e6df6 | 7650 | iverdef->vd_nextdef = NULL; |
601a03ba AM |
7651 | iverdef->vd_auxptr = ((struct elf_internal_verdaux *) |
7652 | bfd_zalloc (abfd, sizeof (Elf_Internal_Verdaux))); | |
d0fb9a8d JJ |
7653 | if (iverdef->vd_auxptr == NULL) |
7654 | goto error_return_verdef; | |
252b5132 | 7655 | |
fc0e6df6 PB |
7656 | iverdaux = iverdef->vd_auxptr; |
7657 | iverdaux->vda_nodename = iverdef->vd_nodename; | |
252b5132 RH |
7658 | } |
7659 | ||
b34976b6 | 7660 | return TRUE; |
252b5132 RH |
7661 | |
7662 | error_return: | |
5ed6aba4 | 7663 | if (contents != NULL) |
252b5132 | 7664 | free (contents); |
b34976b6 | 7665 | return FALSE; |
252b5132 RH |
7666 | } |
7667 | \f | |
7668 | asymbol * | |
217aa764 | 7669 | _bfd_elf_make_empty_symbol (bfd *abfd) |
252b5132 RH |
7670 | { |
7671 | elf_symbol_type *newsym; | |
7672 | ||
201159ec | 7673 | newsym = (elf_symbol_type *) bfd_zalloc (abfd, sizeof * newsym); |
252b5132 RH |
7674 | if (!newsym) |
7675 | return NULL; | |
201159ec NC |
7676 | newsym->symbol.the_bfd = abfd; |
7677 | return &newsym->symbol; | |
252b5132 RH |
7678 | } |
7679 | ||
7680 | void | |
217aa764 AM |
7681 | _bfd_elf_get_symbol_info (bfd *abfd ATTRIBUTE_UNUSED, |
7682 | asymbol *symbol, | |
7683 | symbol_info *ret) | |
252b5132 RH |
7684 | { |
7685 | bfd_symbol_info (symbol, ret); | |
7686 | } | |
7687 | ||
7688 | /* Return whether a symbol name implies a local symbol. Most targets | |
7689 | use this function for the is_local_label_name entry point, but some | |
7690 | override it. */ | |
7691 | ||
b34976b6 | 7692 | bfd_boolean |
217aa764 AM |
7693 | _bfd_elf_is_local_label_name (bfd *abfd ATTRIBUTE_UNUSED, |
7694 | const char *name) | |
252b5132 RH |
7695 | { |
7696 | /* Normal local symbols start with ``.L''. */ | |
7697 | if (name[0] == '.' && name[1] == 'L') | |
b34976b6 | 7698 | return TRUE; |
252b5132 RH |
7699 | |
7700 | /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate | |
7701 | DWARF debugging symbols starting with ``..''. */ | |
7702 | if (name[0] == '.' && name[1] == '.') | |
b34976b6 | 7703 | return TRUE; |
252b5132 RH |
7704 | |
7705 | /* gcc will sometimes generate symbols beginning with ``_.L_'' when | |
7706 | emitting DWARF debugging output. I suspect this is actually a | |
7707 | small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call | |
7708 | ASM_GENERATE_INTERNAL_LABEL, and this causes the leading | |
7709 | underscore to be emitted on some ELF targets). For ease of use, | |
7710 | we treat such symbols as local. */ | |
7711 | if (name[0] == '_' && name[1] == '.' && name[2] == 'L' && name[3] == '_') | |
b34976b6 | 7712 | return TRUE; |
252b5132 | 7713 | |
b34976b6 | 7714 | return FALSE; |
252b5132 RH |
7715 | } |
7716 | ||
7717 | alent * | |
217aa764 AM |
7718 | _bfd_elf_get_lineno (bfd *abfd ATTRIBUTE_UNUSED, |
7719 | asymbol *symbol ATTRIBUTE_UNUSED) | |
252b5132 RH |
7720 | { |
7721 | abort (); | |
7722 | return NULL; | |
7723 | } | |
7724 | ||
b34976b6 | 7725 | bfd_boolean |
217aa764 AM |
7726 | _bfd_elf_set_arch_mach (bfd *abfd, |
7727 | enum bfd_architecture arch, | |
7728 | unsigned long machine) | |
252b5132 RH |
7729 | { |
7730 | /* If this isn't the right architecture for this backend, and this | |
7731 | isn't the generic backend, fail. */ | |
7732 | if (arch != get_elf_backend_data (abfd)->arch | |
7733 | && arch != bfd_arch_unknown | |
7734 | && get_elf_backend_data (abfd)->arch != bfd_arch_unknown) | |
b34976b6 | 7735 | return FALSE; |
252b5132 RH |
7736 | |
7737 | return bfd_default_set_arch_mach (abfd, arch, machine); | |
7738 | } | |
7739 | ||
d1fad7c6 NC |
7740 | /* Find the nearest line to a particular section and offset, |
7741 | for error reporting. */ | |
7742 | ||
b34976b6 | 7743 | bfd_boolean |
217aa764 | 7744 | _bfd_elf_find_nearest_line (bfd *abfd, |
217aa764 | 7745 | asymbol **symbols, |
fb167eb2 | 7746 | asection *section, |
217aa764 AM |
7747 | bfd_vma offset, |
7748 | const char **filename_ptr, | |
7749 | const char **functionname_ptr, | |
fb167eb2 AM |
7750 | unsigned int *line_ptr, |
7751 | unsigned int *discriminator_ptr) | |
d1fad7c6 | 7752 | { |
b34976b6 | 7753 | bfd_boolean found; |
d1fad7c6 | 7754 | |
fb167eb2 | 7755 | if (_bfd_dwarf2_find_nearest_line (abfd, symbols, NULL, section, offset, |
4e8a9624 | 7756 | filename_ptr, functionname_ptr, |
fb167eb2 AM |
7757 | line_ptr, discriminator_ptr, |
7758 | dwarf_debug_sections, 0, | |
e00e8198 AM |
7759 | &elf_tdata (abfd)->dwarf2_find_line_info) |
7760 | || _bfd_dwarf1_find_nearest_line (abfd, symbols, section, offset, | |
7761 | filename_ptr, functionname_ptr, | |
7762 | line_ptr)) | |
d1fad7c6 NC |
7763 | { |
7764 | if (!*functionname_ptr) | |
e00e8198 AM |
7765 | _bfd_elf_find_function (abfd, symbols, section, offset, |
7766 | *filename_ptr ? NULL : filename_ptr, | |
7767 | functionname_ptr); | |
b34976b6 | 7768 | return TRUE; |
d1fad7c6 NC |
7769 | } |
7770 | ||
7771 | if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset, | |
4e8a9624 AM |
7772 | &found, filename_ptr, |
7773 | functionname_ptr, line_ptr, | |
7774 | &elf_tdata (abfd)->line_info)) | |
b34976b6 | 7775 | return FALSE; |
dc43ada5 | 7776 | if (found && (*functionname_ptr || *line_ptr)) |
b34976b6 | 7777 | return TRUE; |
d1fad7c6 NC |
7778 | |
7779 | if (symbols == NULL) | |
b34976b6 | 7780 | return FALSE; |
d1fad7c6 | 7781 | |
e00e8198 AM |
7782 | if (! _bfd_elf_find_function (abfd, symbols, section, offset, |
7783 | filename_ptr, functionname_ptr)) | |
b34976b6 | 7784 | return FALSE; |
d1fad7c6 | 7785 | |
252b5132 | 7786 | *line_ptr = 0; |
b34976b6 | 7787 | return TRUE; |
252b5132 RH |
7788 | } |
7789 | ||
5420f73d L |
7790 | /* Find the line for a symbol. */ |
7791 | ||
7792 | bfd_boolean | |
7793 | _bfd_elf_find_line (bfd *abfd, asymbol **symbols, asymbol *symbol, | |
7794 | const char **filename_ptr, unsigned int *line_ptr) | |
9b8d1a36 | 7795 | { |
fb167eb2 AM |
7796 | return _bfd_dwarf2_find_nearest_line (abfd, symbols, symbol, NULL, 0, |
7797 | filename_ptr, NULL, line_ptr, NULL, | |
7798 | dwarf_debug_sections, 0, | |
7799 | &elf_tdata (abfd)->dwarf2_find_line_info); | |
5420f73d L |
7800 | } |
7801 | ||
4ab527b0 FF |
7802 | /* After a call to bfd_find_nearest_line, successive calls to |
7803 | bfd_find_inliner_info can be used to get source information about | |
7804 | each level of function inlining that terminated at the address | |
7805 | passed to bfd_find_nearest_line. Currently this is only supported | |
7806 | for DWARF2 with appropriate DWARF3 extensions. */ | |
7807 | ||
7808 | bfd_boolean | |
7809 | _bfd_elf_find_inliner_info (bfd *abfd, | |
7810 | const char **filename_ptr, | |
7811 | const char **functionname_ptr, | |
7812 | unsigned int *line_ptr) | |
7813 | { | |
7814 | bfd_boolean found; | |
7815 | found = _bfd_dwarf2_find_inliner_info (abfd, filename_ptr, | |
7816 | functionname_ptr, line_ptr, | |
7817 | & elf_tdata (abfd)->dwarf2_find_line_info); | |
7818 | return found; | |
7819 | } | |
7820 | ||
252b5132 | 7821 | int |
a6b96beb | 7822 | _bfd_elf_sizeof_headers (bfd *abfd, struct bfd_link_info *info) |
252b5132 | 7823 | { |
8ded5a0f AM |
7824 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
7825 | int ret = bed->s->sizeof_ehdr; | |
252b5132 | 7826 | |
a6b96beb | 7827 | if (!info->relocatable) |
8ded5a0f | 7828 | { |
12bd6957 | 7829 | bfd_size_type phdr_size = elf_program_header_size (abfd); |
8ded5a0f | 7830 | |
62d7a5f6 AM |
7831 | if (phdr_size == (bfd_size_type) -1) |
7832 | { | |
7833 | struct elf_segment_map *m; | |
7834 | ||
7835 | phdr_size = 0; | |
12bd6957 | 7836 | for (m = elf_seg_map (abfd); m != NULL; m = m->next) |
62d7a5f6 | 7837 | phdr_size += bed->s->sizeof_phdr; |
8ded5a0f | 7838 | |
62d7a5f6 AM |
7839 | if (phdr_size == 0) |
7840 | phdr_size = get_program_header_size (abfd, info); | |
7841 | } | |
8ded5a0f | 7842 | |
12bd6957 | 7843 | elf_program_header_size (abfd) = phdr_size; |
8ded5a0f AM |
7844 | ret += phdr_size; |
7845 | } | |
7846 | ||
252b5132 RH |
7847 | return ret; |
7848 | } | |
7849 | ||
b34976b6 | 7850 | bfd_boolean |
217aa764 AM |
7851 | _bfd_elf_set_section_contents (bfd *abfd, |
7852 | sec_ptr section, | |
0f867abe | 7853 | const void *location, |
217aa764 AM |
7854 | file_ptr offset, |
7855 | bfd_size_type count) | |
252b5132 RH |
7856 | { |
7857 | Elf_Internal_Shdr *hdr; | |
1b6aeedb | 7858 | file_ptr pos; |
252b5132 RH |
7859 | |
7860 | if (! abfd->output_has_begun | |
217aa764 | 7861 | && ! _bfd_elf_compute_section_file_positions (abfd, NULL)) |
b34976b6 | 7862 | return FALSE; |
252b5132 RH |
7863 | |
7864 | hdr = &elf_section_data (section)->this_hdr; | |
dc810e39 AM |
7865 | pos = hdr->sh_offset + offset; |
7866 | if (bfd_seek (abfd, pos, SEEK_SET) != 0 | |
7867 | || bfd_bwrite (location, count, abfd) != count) | |
b34976b6 | 7868 | return FALSE; |
252b5132 | 7869 | |
b34976b6 | 7870 | return TRUE; |
252b5132 RH |
7871 | } |
7872 | ||
7873 | void | |
217aa764 AM |
7874 | _bfd_elf_no_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED, |
7875 | arelent *cache_ptr ATTRIBUTE_UNUSED, | |
7876 | Elf_Internal_Rela *dst ATTRIBUTE_UNUSED) | |
252b5132 RH |
7877 | { |
7878 | abort (); | |
7879 | } | |
7880 | ||
252b5132 RH |
7881 | /* Try to convert a non-ELF reloc into an ELF one. */ |
7882 | ||
b34976b6 | 7883 | bfd_boolean |
217aa764 | 7884 | _bfd_elf_validate_reloc (bfd *abfd, arelent *areloc) |
252b5132 | 7885 | { |
c044fabd | 7886 | /* Check whether we really have an ELF howto. */ |
252b5132 RH |
7887 | |
7888 | if ((*areloc->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec) | |
7889 | { | |
7890 | bfd_reloc_code_real_type code; | |
7891 | reloc_howto_type *howto; | |
7892 | ||
7893 | /* Alien reloc: Try to determine its type to replace it with an | |
c044fabd | 7894 | equivalent ELF reloc. */ |
252b5132 RH |
7895 | |
7896 | if (areloc->howto->pc_relative) | |
7897 | { | |
7898 | switch (areloc->howto->bitsize) | |
7899 | { | |
7900 | case 8: | |
7901 | code = BFD_RELOC_8_PCREL; | |
7902 | break; | |
7903 | case 12: | |
7904 | code = BFD_RELOC_12_PCREL; | |
7905 | break; | |
7906 | case 16: | |
7907 | code = BFD_RELOC_16_PCREL; | |
7908 | break; | |
7909 | case 24: | |
7910 | code = BFD_RELOC_24_PCREL; | |
7911 | break; | |
7912 | case 32: | |
7913 | code = BFD_RELOC_32_PCREL; | |
7914 | break; | |
7915 | case 64: | |
7916 | code = BFD_RELOC_64_PCREL; | |
7917 | break; | |
7918 | default: | |
7919 | goto fail; | |
7920 | } | |
7921 | ||
7922 | howto = bfd_reloc_type_lookup (abfd, code); | |
7923 | ||
7924 | if (areloc->howto->pcrel_offset != howto->pcrel_offset) | |
7925 | { | |
7926 | if (howto->pcrel_offset) | |
7927 | areloc->addend += areloc->address; | |
7928 | else | |
7929 | areloc->addend -= areloc->address; /* addend is unsigned!! */ | |
7930 | } | |
7931 | } | |
7932 | else | |
7933 | { | |
7934 | switch (areloc->howto->bitsize) | |
7935 | { | |
7936 | case 8: | |
7937 | code = BFD_RELOC_8; | |
7938 | break; | |
7939 | case 14: | |
7940 | code = BFD_RELOC_14; | |
7941 | break; | |
7942 | case 16: | |
7943 | code = BFD_RELOC_16; | |
7944 | break; | |
7945 | case 26: | |
7946 | code = BFD_RELOC_26; | |
7947 | break; | |
7948 | case 32: | |
7949 | code = BFD_RELOC_32; | |
7950 | break; | |
7951 | case 64: | |
7952 | code = BFD_RELOC_64; | |
7953 | break; | |
7954 | default: | |
7955 | goto fail; | |
7956 | } | |
7957 | ||
7958 | howto = bfd_reloc_type_lookup (abfd, code); | |
7959 | } | |
7960 | ||
7961 | if (howto) | |
7962 | areloc->howto = howto; | |
7963 | else | |
7964 | goto fail; | |
7965 | } | |
7966 | ||
b34976b6 | 7967 | return TRUE; |
252b5132 RH |
7968 | |
7969 | fail: | |
7970 | (*_bfd_error_handler) | |
d003868e AM |
7971 | (_("%B: unsupported relocation type %s"), |
7972 | abfd, areloc->howto->name); | |
252b5132 | 7973 | bfd_set_error (bfd_error_bad_value); |
b34976b6 | 7974 | return FALSE; |
252b5132 RH |
7975 | } |
7976 | ||
b34976b6 | 7977 | bfd_boolean |
217aa764 | 7978 | _bfd_elf_close_and_cleanup (bfd *abfd) |
252b5132 | 7979 | { |
d9071b0c TG |
7980 | struct elf_obj_tdata *tdata = elf_tdata (abfd); |
7981 | if (bfd_get_format (abfd) == bfd_object && tdata != NULL) | |
252b5132 | 7982 | { |
c0355132 | 7983 | if (elf_tdata (abfd)->o != NULL && elf_shstrtab (abfd) != NULL) |
2b0f7ef9 | 7984 | _bfd_elf_strtab_free (elf_shstrtab (abfd)); |
d9071b0c | 7985 | _bfd_dwarf2_cleanup_debug_info (abfd, &tdata->dwarf2_find_line_info); |
252b5132 RH |
7986 | } |
7987 | ||
7988 | return _bfd_generic_close_and_cleanup (abfd); | |
7989 | } | |
7990 | ||
7991 | /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY | |
7992 | in the relocation's offset. Thus we cannot allow any sort of sanity | |
7993 | range-checking to interfere. There is nothing else to do in processing | |
7994 | this reloc. */ | |
7995 | ||
7996 | bfd_reloc_status_type | |
217aa764 AM |
7997 | _bfd_elf_rel_vtable_reloc_fn |
7998 | (bfd *abfd ATTRIBUTE_UNUSED, arelent *re ATTRIBUTE_UNUSED, | |
fc0a2244 | 7999 | struct bfd_symbol *symbol ATTRIBUTE_UNUSED, |
217aa764 AM |
8000 | void *data ATTRIBUTE_UNUSED, asection *is ATTRIBUTE_UNUSED, |
8001 | bfd *obfd ATTRIBUTE_UNUSED, char **errmsg ATTRIBUTE_UNUSED) | |
252b5132 RH |
8002 | { |
8003 | return bfd_reloc_ok; | |
8004 | } | |
252b5132 RH |
8005 | \f |
8006 | /* Elf core file support. Much of this only works on native | |
8007 | toolchains, since we rely on knowing the | |
8008 | machine-dependent procfs structure in order to pick | |
c044fabd | 8009 | out details about the corefile. */ |
252b5132 RH |
8010 | |
8011 | #ifdef HAVE_SYS_PROCFS_H | |
16231b7b DG |
8012 | /* Needed for new procfs interface on sparc-solaris. */ |
8013 | # define _STRUCTURED_PROC 1 | |
252b5132 RH |
8014 | # include <sys/procfs.h> |
8015 | #endif | |
8016 | ||
261b8d08 PA |
8017 | /* Return a PID that identifies a "thread" for threaded cores, or the |
8018 | PID of the main process for non-threaded cores. */ | |
252b5132 RH |
8019 | |
8020 | static int | |
217aa764 | 8021 | elfcore_make_pid (bfd *abfd) |
252b5132 | 8022 | { |
261b8d08 PA |
8023 | int pid; |
8024 | ||
228e534f | 8025 | pid = elf_tdata (abfd)->core->lwpid; |
261b8d08 | 8026 | if (pid == 0) |
228e534f | 8027 | pid = elf_tdata (abfd)->core->pid; |
261b8d08 PA |
8028 | |
8029 | return pid; | |
252b5132 RH |
8030 | } |
8031 | ||
252b5132 RH |
8032 | /* If there isn't a section called NAME, make one, using |
8033 | data from SECT. Note, this function will generate a | |
8034 | reference to NAME, so you shouldn't deallocate or | |
c044fabd | 8035 | overwrite it. */ |
252b5132 | 8036 | |
b34976b6 | 8037 | static bfd_boolean |
217aa764 | 8038 | elfcore_maybe_make_sect (bfd *abfd, char *name, asection *sect) |
252b5132 | 8039 | { |
c044fabd | 8040 | asection *sect2; |
252b5132 RH |
8041 | |
8042 | if (bfd_get_section_by_name (abfd, name) != NULL) | |
b34976b6 | 8043 | return TRUE; |
252b5132 | 8044 | |
117ed4f8 | 8045 | sect2 = bfd_make_section_with_flags (abfd, name, sect->flags); |
252b5132 | 8046 | if (sect2 == NULL) |
b34976b6 | 8047 | return FALSE; |
252b5132 | 8048 | |
eea6121a | 8049 | sect2->size = sect->size; |
252b5132 | 8050 | sect2->filepos = sect->filepos; |
252b5132 | 8051 | sect2->alignment_power = sect->alignment_power; |
b34976b6 | 8052 | return TRUE; |
252b5132 RH |
8053 | } |
8054 | ||
bb0082d6 AM |
8055 | /* Create a pseudosection containing SIZE bytes at FILEPOS. This |
8056 | actually creates up to two pseudosections: | |
8057 | - For the single-threaded case, a section named NAME, unless | |
8058 | such a section already exists. | |
8059 | - For the multi-threaded case, a section named "NAME/PID", where | |
8060 | PID is elfcore_make_pid (abfd). | |
8061 | Both pseudosections have identical contents. */ | |
b34976b6 | 8062 | bfd_boolean |
217aa764 AM |
8063 | _bfd_elfcore_make_pseudosection (bfd *abfd, |
8064 | char *name, | |
8065 | size_t size, | |
8066 | ufile_ptr filepos) | |
bb0082d6 AM |
8067 | { |
8068 | char buf[100]; | |
8069 | char *threaded_name; | |
d4c88bbb | 8070 | size_t len; |
bb0082d6 AM |
8071 | asection *sect; |
8072 | ||
8073 | /* Build the section name. */ | |
8074 | ||
8075 | sprintf (buf, "%s/%d", name, elfcore_make_pid (abfd)); | |
d4c88bbb | 8076 | len = strlen (buf) + 1; |
a50b1753 | 8077 | threaded_name = (char *) bfd_alloc (abfd, len); |
bb0082d6 | 8078 | if (threaded_name == NULL) |
b34976b6 | 8079 | return FALSE; |
d4c88bbb | 8080 | memcpy (threaded_name, buf, len); |
bb0082d6 | 8081 | |
117ed4f8 AM |
8082 | sect = bfd_make_section_anyway_with_flags (abfd, threaded_name, |
8083 | SEC_HAS_CONTENTS); | |
bb0082d6 | 8084 | if (sect == NULL) |
b34976b6 | 8085 | return FALSE; |
eea6121a | 8086 | sect->size = size; |
bb0082d6 | 8087 | sect->filepos = filepos; |
bb0082d6 AM |
8088 | sect->alignment_power = 2; |
8089 | ||
936e320b | 8090 | return elfcore_maybe_make_sect (abfd, name, sect); |
bb0082d6 AM |
8091 | } |
8092 | ||
252b5132 | 8093 | /* prstatus_t exists on: |
4a938328 | 8094 | solaris 2.5+ |
252b5132 RH |
8095 | linux 2.[01] + glibc |
8096 | unixware 4.2 | |
8097 | */ | |
8098 | ||
8099 | #if defined (HAVE_PRSTATUS_T) | |
a7b97311 | 8100 | |
b34976b6 | 8101 | static bfd_boolean |
217aa764 | 8102 | elfcore_grok_prstatus (bfd *abfd, Elf_Internal_Note *note) |
252b5132 | 8103 | { |
eea6121a | 8104 | size_t size; |
7ee38065 | 8105 | int offset; |
252b5132 | 8106 | |
4a938328 MS |
8107 | if (note->descsz == sizeof (prstatus_t)) |
8108 | { | |
8109 | prstatus_t prstat; | |
252b5132 | 8110 | |
eea6121a | 8111 | size = sizeof (prstat.pr_reg); |
7ee38065 | 8112 | offset = offsetof (prstatus_t, pr_reg); |
4a938328 | 8113 | memcpy (&prstat, note->descdata, sizeof (prstat)); |
252b5132 | 8114 | |
fa49d224 NC |
8115 | /* Do not overwrite the core signal if it |
8116 | has already been set by another thread. */ | |
228e534f AM |
8117 | if (elf_tdata (abfd)->core->signal == 0) |
8118 | elf_tdata (abfd)->core->signal = prstat.pr_cursig; | |
8119 | if (elf_tdata (abfd)->core->pid == 0) | |
8120 | elf_tdata (abfd)->core->pid = prstat.pr_pid; | |
252b5132 | 8121 | |
4a938328 MS |
8122 | /* pr_who exists on: |
8123 | solaris 2.5+ | |
8124 | unixware 4.2 | |
8125 | pr_who doesn't exist on: | |
8126 | linux 2.[01] | |
8127 | */ | |
252b5132 | 8128 | #if defined (HAVE_PRSTATUS_T_PR_WHO) |
228e534f | 8129 | elf_tdata (abfd)->core->lwpid = prstat.pr_who; |
261b8d08 | 8130 | #else |
228e534f | 8131 | elf_tdata (abfd)->core->lwpid = prstat.pr_pid; |
252b5132 | 8132 | #endif |
4a938328 | 8133 | } |
7ee38065 | 8134 | #if defined (HAVE_PRSTATUS32_T) |
4a938328 MS |
8135 | else if (note->descsz == sizeof (prstatus32_t)) |
8136 | { | |
8137 | /* 64-bit host, 32-bit corefile */ | |
8138 | prstatus32_t prstat; | |
8139 | ||
eea6121a | 8140 | size = sizeof (prstat.pr_reg); |
7ee38065 | 8141 | offset = offsetof (prstatus32_t, pr_reg); |
4a938328 MS |
8142 | memcpy (&prstat, note->descdata, sizeof (prstat)); |
8143 | ||
fa49d224 NC |
8144 | /* Do not overwrite the core signal if it |
8145 | has already been set by another thread. */ | |
228e534f AM |
8146 | if (elf_tdata (abfd)->core->signal == 0) |
8147 | elf_tdata (abfd)->core->signal = prstat.pr_cursig; | |
8148 | if (elf_tdata (abfd)->core->pid == 0) | |
8149 | elf_tdata (abfd)->core->pid = prstat.pr_pid; | |
4a938328 MS |
8150 | |
8151 | /* pr_who exists on: | |
8152 | solaris 2.5+ | |
8153 | unixware 4.2 | |
8154 | pr_who doesn't exist on: | |
8155 | linux 2.[01] | |
8156 | */ | |
7ee38065 | 8157 | #if defined (HAVE_PRSTATUS32_T_PR_WHO) |
228e534f | 8158 | elf_tdata (abfd)->core->lwpid = prstat.pr_who; |
261b8d08 | 8159 | #else |
228e534f | 8160 | elf_tdata (abfd)->core->lwpid = prstat.pr_pid; |
4a938328 MS |
8161 | #endif |
8162 | } | |
7ee38065 | 8163 | #endif /* HAVE_PRSTATUS32_T */ |
4a938328 MS |
8164 | else |
8165 | { | |
8166 | /* Fail - we don't know how to handle any other | |
8167 | note size (ie. data object type). */ | |
b34976b6 | 8168 | return TRUE; |
4a938328 | 8169 | } |
252b5132 | 8170 | |
bb0082d6 | 8171 | /* Make a ".reg/999" section and a ".reg" section. */ |
936e320b | 8172 | return _bfd_elfcore_make_pseudosection (abfd, ".reg", |
eea6121a | 8173 | size, note->descpos + offset); |
252b5132 RH |
8174 | } |
8175 | #endif /* defined (HAVE_PRSTATUS_T) */ | |
8176 | ||
bb0082d6 | 8177 | /* Create a pseudosection containing the exact contents of NOTE. */ |
b34976b6 | 8178 | static bfd_boolean |
217aa764 AM |
8179 | elfcore_make_note_pseudosection (bfd *abfd, |
8180 | char *name, | |
8181 | Elf_Internal_Note *note) | |
252b5132 | 8182 | { |
936e320b AM |
8183 | return _bfd_elfcore_make_pseudosection (abfd, name, |
8184 | note->descsz, note->descpos); | |
252b5132 RH |
8185 | } |
8186 | ||
ff08c6bb JB |
8187 | /* There isn't a consistent prfpregset_t across platforms, |
8188 | but it doesn't matter, because we don't have to pick this | |
c044fabd KH |
8189 | data structure apart. */ |
8190 | ||
b34976b6 | 8191 | static bfd_boolean |
217aa764 | 8192 | elfcore_grok_prfpreg (bfd *abfd, Elf_Internal_Note *note) |
ff08c6bb JB |
8193 | { |
8194 | return elfcore_make_note_pseudosection (abfd, ".reg2", note); | |
8195 | } | |
8196 | ||
ff08c6bb | 8197 | /* Linux dumps the Intel SSE regs in a note named "LINUX" with a note |
971d4640 | 8198 | type of NT_PRXFPREG. Just include the whole note's contents |
ff08c6bb | 8199 | literally. */ |
c044fabd | 8200 | |
b34976b6 | 8201 | static bfd_boolean |
217aa764 | 8202 | elfcore_grok_prxfpreg (bfd *abfd, Elf_Internal_Note *note) |
ff08c6bb JB |
8203 | { |
8204 | return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note); | |
8205 | } | |
8206 | ||
4339cae0 L |
8207 | /* Linux dumps the Intel XSAVE extended state in a note named "LINUX" |
8208 | with a note type of NT_X86_XSTATE. Just include the whole note's | |
8209 | contents literally. */ | |
8210 | ||
8211 | static bfd_boolean | |
8212 | elfcore_grok_xstatereg (bfd *abfd, Elf_Internal_Note *note) | |
8213 | { | |
8214 | return elfcore_make_note_pseudosection (abfd, ".reg-xstate", note); | |
8215 | } | |
8216 | ||
97753bd5 AM |
8217 | static bfd_boolean |
8218 | elfcore_grok_ppc_vmx (bfd *abfd, Elf_Internal_Note *note) | |
8219 | { | |
8220 | return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vmx", note); | |
8221 | } | |
8222 | ||
89eeb0bc LM |
8223 | static bfd_boolean |
8224 | elfcore_grok_ppc_vsx (bfd *abfd, Elf_Internal_Note *note) | |
8225 | { | |
8226 | return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vsx", note); | |
8227 | } | |
97753bd5 | 8228 | |
0675e188 UW |
8229 | static bfd_boolean |
8230 | elfcore_grok_s390_high_gprs (bfd *abfd, Elf_Internal_Note *note) | |
8231 | { | |
8232 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-high-gprs", note); | |
8233 | } | |
8234 | ||
d7eeb400 MS |
8235 | static bfd_boolean |
8236 | elfcore_grok_s390_timer (bfd *abfd, Elf_Internal_Note *note) | |
8237 | { | |
8238 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-timer", note); | |
8239 | } | |
8240 | ||
8241 | static bfd_boolean | |
8242 | elfcore_grok_s390_todcmp (bfd *abfd, Elf_Internal_Note *note) | |
8243 | { | |
8244 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-todcmp", note); | |
8245 | } | |
8246 | ||
8247 | static bfd_boolean | |
8248 | elfcore_grok_s390_todpreg (bfd *abfd, Elf_Internal_Note *note) | |
8249 | { | |
8250 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-todpreg", note); | |
8251 | } | |
8252 | ||
8253 | static bfd_boolean | |
8254 | elfcore_grok_s390_ctrs (bfd *abfd, Elf_Internal_Note *note) | |
8255 | { | |
8256 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-ctrs", note); | |
8257 | } | |
8258 | ||
8259 | static bfd_boolean | |
8260 | elfcore_grok_s390_prefix (bfd *abfd, Elf_Internal_Note *note) | |
8261 | { | |
8262 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-prefix", note); | |
8263 | } | |
8264 | ||
355b81d9 UW |
8265 | static bfd_boolean |
8266 | elfcore_grok_s390_last_break (bfd *abfd, Elf_Internal_Note *note) | |
8267 | { | |
8268 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-last-break", note); | |
8269 | } | |
8270 | ||
8271 | static bfd_boolean | |
8272 | elfcore_grok_s390_system_call (bfd *abfd, Elf_Internal_Note *note) | |
8273 | { | |
8274 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-system-call", note); | |
8275 | } | |
8276 | ||
abb3f6cc NC |
8277 | static bfd_boolean |
8278 | elfcore_grok_s390_tdb (bfd *abfd, Elf_Internal_Note *note) | |
8279 | { | |
8280 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-tdb", note); | |
8281 | } | |
8282 | ||
4ef9f41a AA |
8283 | static bfd_boolean |
8284 | elfcore_grok_s390_vxrs_low (bfd *abfd, Elf_Internal_Note *note) | |
8285 | { | |
8286 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-vxrs-low", note); | |
8287 | } | |
8288 | ||
8289 | static bfd_boolean | |
8290 | elfcore_grok_s390_vxrs_high (bfd *abfd, Elf_Internal_Note *note) | |
8291 | { | |
8292 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-vxrs-high", note); | |
8293 | } | |
8294 | ||
faa9a424 UW |
8295 | static bfd_boolean |
8296 | elfcore_grok_arm_vfp (bfd *abfd, Elf_Internal_Note *note) | |
8297 | { | |
8298 | return elfcore_make_note_pseudosection (abfd, ".reg-arm-vfp", note); | |
8299 | } | |
8300 | ||
652451f8 YZ |
8301 | static bfd_boolean |
8302 | elfcore_grok_aarch_tls (bfd *abfd, Elf_Internal_Note *note) | |
8303 | { | |
8304 | return elfcore_make_note_pseudosection (abfd, ".reg-aarch-tls", note); | |
8305 | } | |
8306 | ||
8307 | static bfd_boolean | |
8308 | elfcore_grok_aarch_hw_break (bfd *abfd, Elf_Internal_Note *note) | |
8309 | { | |
8310 | return elfcore_make_note_pseudosection (abfd, ".reg-aarch-hw-break", note); | |
8311 | } | |
8312 | ||
8313 | static bfd_boolean | |
8314 | elfcore_grok_aarch_hw_watch (bfd *abfd, Elf_Internal_Note *note) | |
8315 | { | |
8316 | return elfcore_make_note_pseudosection (abfd, ".reg-aarch-hw-watch", note); | |
8317 | } | |
8318 | ||
252b5132 | 8319 | #if defined (HAVE_PRPSINFO_T) |
4a938328 | 8320 | typedef prpsinfo_t elfcore_psinfo_t; |
7ee38065 | 8321 | #if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */ |
4a938328 MS |
8322 | typedef prpsinfo32_t elfcore_psinfo32_t; |
8323 | #endif | |
252b5132 RH |
8324 | #endif |
8325 | ||
8326 | #if defined (HAVE_PSINFO_T) | |
4a938328 | 8327 | typedef psinfo_t elfcore_psinfo_t; |
7ee38065 | 8328 | #if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */ |
4a938328 MS |
8329 | typedef psinfo32_t elfcore_psinfo32_t; |
8330 | #endif | |
252b5132 RH |
8331 | #endif |
8332 | ||
252b5132 RH |
8333 | /* return a malloc'ed copy of a string at START which is at |
8334 | most MAX bytes long, possibly without a terminating '\0'. | |
c044fabd | 8335 | the copy will always have a terminating '\0'. */ |
252b5132 | 8336 | |
936e320b | 8337 | char * |
217aa764 | 8338 | _bfd_elfcore_strndup (bfd *abfd, char *start, size_t max) |
252b5132 | 8339 | { |
dc810e39 | 8340 | char *dups; |
a50b1753 | 8341 | char *end = (char *) memchr (start, '\0', max); |
dc810e39 | 8342 | size_t len; |
252b5132 RH |
8343 | |
8344 | if (end == NULL) | |
8345 | len = max; | |
8346 | else | |
8347 | len = end - start; | |
8348 | ||
a50b1753 | 8349 | dups = (char *) bfd_alloc (abfd, len + 1); |
dc810e39 | 8350 | if (dups == NULL) |
252b5132 RH |
8351 | return NULL; |
8352 | ||
dc810e39 AM |
8353 | memcpy (dups, start, len); |
8354 | dups[len] = '\0'; | |
252b5132 | 8355 | |
dc810e39 | 8356 | return dups; |
252b5132 RH |
8357 | } |
8358 | ||
bb0082d6 | 8359 | #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) |
b34976b6 | 8360 | static bfd_boolean |
217aa764 | 8361 | elfcore_grok_psinfo (bfd *abfd, Elf_Internal_Note *note) |
252b5132 | 8362 | { |
4a938328 MS |
8363 | if (note->descsz == sizeof (elfcore_psinfo_t)) |
8364 | { | |
8365 | elfcore_psinfo_t psinfo; | |
252b5132 | 8366 | |
7ee38065 | 8367 | memcpy (&psinfo, note->descdata, sizeof (psinfo)); |
252b5132 | 8368 | |
335e41d4 | 8369 | #if defined (HAVE_PSINFO_T_PR_PID) || defined (HAVE_PRPSINFO_T_PR_PID) |
228e534f | 8370 | elf_tdata (abfd)->core->pid = psinfo.pr_pid; |
335e41d4 | 8371 | #endif |
228e534f | 8372 | elf_tdata (abfd)->core->program |
936e320b AM |
8373 | = _bfd_elfcore_strndup (abfd, psinfo.pr_fname, |
8374 | sizeof (psinfo.pr_fname)); | |
252b5132 | 8375 | |
228e534f | 8376 | elf_tdata (abfd)->core->command |
936e320b AM |
8377 | = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs, |
8378 | sizeof (psinfo.pr_psargs)); | |
4a938328 | 8379 | } |
7ee38065 | 8380 | #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T) |
4a938328 MS |
8381 | else if (note->descsz == sizeof (elfcore_psinfo32_t)) |
8382 | { | |
8383 | /* 64-bit host, 32-bit corefile */ | |
8384 | elfcore_psinfo32_t psinfo; | |
8385 | ||
7ee38065 | 8386 | memcpy (&psinfo, note->descdata, sizeof (psinfo)); |
252b5132 | 8387 | |
335e41d4 | 8388 | #if defined (HAVE_PSINFO32_T_PR_PID) || defined (HAVE_PRPSINFO32_T_PR_PID) |
228e534f | 8389 | elf_tdata (abfd)->core->pid = psinfo.pr_pid; |
335e41d4 | 8390 | #endif |
228e534f | 8391 | elf_tdata (abfd)->core->program |
936e320b AM |
8392 | = _bfd_elfcore_strndup (abfd, psinfo.pr_fname, |
8393 | sizeof (psinfo.pr_fname)); | |
4a938328 | 8394 | |
228e534f | 8395 | elf_tdata (abfd)->core->command |
936e320b AM |
8396 | = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs, |
8397 | sizeof (psinfo.pr_psargs)); | |
4a938328 MS |
8398 | } |
8399 | #endif | |
8400 | ||
8401 | else | |
8402 | { | |
8403 | /* Fail - we don't know how to handle any other | |
8404 | note size (ie. data object type). */ | |
b34976b6 | 8405 | return TRUE; |
4a938328 | 8406 | } |
252b5132 RH |
8407 | |
8408 | /* Note that for some reason, a spurious space is tacked | |
8409 | onto the end of the args in some (at least one anyway) | |
c044fabd | 8410 | implementations, so strip it off if it exists. */ |
252b5132 RH |
8411 | |
8412 | { | |
228e534f | 8413 | char *command = elf_tdata (abfd)->core->command; |
252b5132 RH |
8414 | int n = strlen (command); |
8415 | ||
8416 | if (0 < n && command[n - 1] == ' ') | |
8417 | command[n - 1] = '\0'; | |
8418 | } | |
8419 | ||
b34976b6 | 8420 | return TRUE; |
252b5132 RH |
8421 | } |
8422 | #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */ | |
8423 | ||
252b5132 | 8424 | #if defined (HAVE_PSTATUS_T) |
b34976b6 | 8425 | static bfd_boolean |
217aa764 | 8426 | elfcore_grok_pstatus (bfd *abfd, Elf_Internal_Note *note) |
252b5132 | 8427 | { |
f572a39d AM |
8428 | if (note->descsz == sizeof (pstatus_t) |
8429 | #if defined (HAVE_PXSTATUS_T) | |
8430 | || note->descsz == sizeof (pxstatus_t) | |
8431 | #endif | |
8432 | ) | |
4a938328 MS |
8433 | { |
8434 | pstatus_t pstat; | |
252b5132 | 8435 | |
4a938328 | 8436 | memcpy (&pstat, note->descdata, sizeof (pstat)); |
252b5132 | 8437 | |
228e534f | 8438 | elf_tdata (abfd)->core->pid = pstat.pr_pid; |
4a938328 | 8439 | } |
7ee38065 | 8440 | #if defined (HAVE_PSTATUS32_T) |
4a938328 MS |
8441 | else if (note->descsz == sizeof (pstatus32_t)) |
8442 | { | |
8443 | /* 64-bit host, 32-bit corefile */ | |
8444 | pstatus32_t pstat; | |
252b5132 | 8445 | |
4a938328 | 8446 | memcpy (&pstat, note->descdata, sizeof (pstat)); |
252b5132 | 8447 | |
228e534f | 8448 | elf_tdata (abfd)->core->pid = pstat.pr_pid; |
4a938328 MS |
8449 | } |
8450 | #endif | |
252b5132 RH |
8451 | /* Could grab some more details from the "representative" |
8452 | lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an | |
c044fabd | 8453 | NT_LWPSTATUS note, presumably. */ |
252b5132 | 8454 | |
b34976b6 | 8455 | return TRUE; |
252b5132 RH |
8456 | } |
8457 | #endif /* defined (HAVE_PSTATUS_T) */ | |
8458 | ||
252b5132 | 8459 | #if defined (HAVE_LWPSTATUS_T) |
b34976b6 | 8460 | static bfd_boolean |
217aa764 | 8461 | elfcore_grok_lwpstatus (bfd *abfd, Elf_Internal_Note *note) |
252b5132 RH |
8462 | { |
8463 | lwpstatus_t lwpstat; | |
8464 | char buf[100]; | |
c044fabd | 8465 | char *name; |
d4c88bbb | 8466 | size_t len; |
c044fabd | 8467 | asection *sect; |
252b5132 | 8468 | |
f572a39d AM |
8469 | if (note->descsz != sizeof (lwpstat) |
8470 | #if defined (HAVE_LWPXSTATUS_T) | |
8471 | && note->descsz != sizeof (lwpxstatus_t) | |
8472 | #endif | |
8473 | ) | |
b34976b6 | 8474 | return TRUE; |
252b5132 RH |
8475 | |
8476 | memcpy (&lwpstat, note->descdata, sizeof (lwpstat)); | |
8477 | ||
228e534f | 8478 | elf_tdata (abfd)->core->lwpid = lwpstat.pr_lwpid; |
a1504221 JB |
8479 | /* Do not overwrite the core signal if it has already been set by |
8480 | another thread. */ | |
228e534f AM |
8481 | if (elf_tdata (abfd)->core->signal == 0) |
8482 | elf_tdata (abfd)->core->signal = lwpstat.pr_cursig; | |
252b5132 | 8483 | |
c044fabd | 8484 | /* Make a ".reg/999" section. */ |
252b5132 RH |
8485 | |
8486 | sprintf (buf, ".reg/%d", elfcore_make_pid (abfd)); | |
d4c88bbb | 8487 | len = strlen (buf) + 1; |
217aa764 | 8488 | name = bfd_alloc (abfd, len); |
252b5132 | 8489 | if (name == NULL) |
b34976b6 | 8490 | return FALSE; |
d4c88bbb | 8491 | memcpy (name, buf, len); |
252b5132 | 8492 | |
117ed4f8 | 8493 | sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); |
252b5132 | 8494 | if (sect == NULL) |
b34976b6 | 8495 | return FALSE; |
252b5132 RH |
8496 | |
8497 | #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT) | |
eea6121a | 8498 | sect->size = sizeof (lwpstat.pr_context.uc_mcontext.gregs); |
252b5132 RH |
8499 | sect->filepos = note->descpos |
8500 | + offsetof (lwpstatus_t, pr_context.uc_mcontext.gregs); | |
8501 | #endif | |
8502 | ||
8503 | #if defined (HAVE_LWPSTATUS_T_PR_REG) | |
eea6121a | 8504 | sect->size = sizeof (lwpstat.pr_reg); |
252b5132 RH |
8505 | sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_reg); |
8506 | #endif | |
8507 | ||
252b5132 RH |
8508 | sect->alignment_power = 2; |
8509 | ||
8510 | if (!elfcore_maybe_make_sect (abfd, ".reg", sect)) | |
b34976b6 | 8511 | return FALSE; |
252b5132 RH |
8512 | |
8513 | /* Make a ".reg2/999" section */ | |
8514 | ||
8515 | sprintf (buf, ".reg2/%d", elfcore_make_pid (abfd)); | |
d4c88bbb | 8516 | len = strlen (buf) + 1; |
217aa764 | 8517 | name = bfd_alloc (abfd, len); |
252b5132 | 8518 | if (name == NULL) |
b34976b6 | 8519 | return FALSE; |
d4c88bbb | 8520 | memcpy (name, buf, len); |
252b5132 | 8521 | |
117ed4f8 | 8522 | sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); |
252b5132 | 8523 | if (sect == NULL) |
b34976b6 | 8524 | return FALSE; |
252b5132 RH |
8525 | |
8526 | #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT) | |
eea6121a | 8527 | sect->size = sizeof (lwpstat.pr_context.uc_mcontext.fpregs); |
252b5132 RH |
8528 | sect->filepos = note->descpos |
8529 | + offsetof (lwpstatus_t, pr_context.uc_mcontext.fpregs); | |
8530 | #endif | |
8531 | ||
8532 | #if defined (HAVE_LWPSTATUS_T_PR_FPREG) | |
eea6121a | 8533 | sect->size = sizeof (lwpstat.pr_fpreg); |
252b5132 RH |
8534 | sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_fpreg); |
8535 | #endif | |
8536 | ||
252b5132 RH |
8537 | sect->alignment_power = 2; |
8538 | ||
936e320b | 8539 | return elfcore_maybe_make_sect (abfd, ".reg2", sect); |
252b5132 RH |
8540 | } |
8541 | #endif /* defined (HAVE_LWPSTATUS_T) */ | |
8542 | ||
b34976b6 | 8543 | static bfd_boolean |
217aa764 | 8544 | elfcore_grok_win32pstatus (bfd *abfd, Elf_Internal_Note *note) |
16e9c715 NC |
8545 | { |
8546 | char buf[30]; | |
c044fabd | 8547 | char *name; |
d4c88bbb | 8548 | size_t len; |
c044fabd | 8549 | asection *sect; |
4a6636fb PA |
8550 | int type; |
8551 | int is_active_thread; | |
8552 | bfd_vma base_addr; | |
16e9c715 | 8553 | |
4a6636fb | 8554 | if (note->descsz < 728) |
b34976b6 | 8555 | return TRUE; |
16e9c715 | 8556 | |
4a6636fb PA |
8557 | if (! CONST_STRNEQ (note->namedata, "win32")) |
8558 | return TRUE; | |
8559 | ||
8560 | type = bfd_get_32 (abfd, note->descdata); | |
c044fabd | 8561 | |
4a6636fb | 8562 | switch (type) |
16e9c715 | 8563 | { |
4a6636fb | 8564 | case 1 /* NOTE_INFO_PROCESS */: |
228e534f | 8565 | /* FIXME: need to add ->core->command. */ |
4a6636fb | 8566 | /* process_info.pid */ |
228e534f | 8567 | elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, note->descdata + 8); |
4a6636fb | 8568 | /* process_info.signal */ |
228e534f | 8569 | elf_tdata (abfd)->core->signal = bfd_get_32 (abfd, note->descdata + 12); |
c044fabd | 8570 | break; |
16e9c715 | 8571 | |
4a6636fb | 8572 | case 2 /* NOTE_INFO_THREAD */: |
16e9c715 | 8573 | /* Make a ".reg/999" section. */ |
4a6636fb PA |
8574 | /* thread_info.tid */ |
8575 | sprintf (buf, ".reg/%ld", (long) bfd_get_32 (abfd, note->descdata + 8)); | |
c044fabd | 8576 | |
d4c88bbb | 8577 | len = strlen (buf) + 1; |
a50b1753 | 8578 | name = (char *) bfd_alloc (abfd, len); |
16e9c715 | 8579 | if (name == NULL) |
b34976b6 | 8580 | return FALSE; |
c044fabd | 8581 | |
d4c88bbb | 8582 | memcpy (name, buf, len); |
16e9c715 | 8583 | |
117ed4f8 | 8584 | sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); |
16e9c715 | 8585 | if (sect == NULL) |
b34976b6 | 8586 | return FALSE; |
c044fabd | 8587 | |
4a6636fb PA |
8588 | /* sizeof (thread_info.thread_context) */ |
8589 | sect->size = 716; | |
8590 | /* offsetof (thread_info.thread_context) */ | |
8591 | sect->filepos = note->descpos + 12; | |
16e9c715 NC |
8592 | sect->alignment_power = 2; |
8593 | ||
4a6636fb PA |
8594 | /* thread_info.is_active_thread */ |
8595 | is_active_thread = bfd_get_32 (abfd, note->descdata + 8); | |
8596 | ||
8597 | if (is_active_thread) | |
16e9c715 | 8598 | if (! elfcore_maybe_make_sect (abfd, ".reg", sect)) |
b34976b6 | 8599 | return FALSE; |
16e9c715 NC |
8600 | break; |
8601 | ||
4a6636fb | 8602 | case 3 /* NOTE_INFO_MODULE */: |
16e9c715 | 8603 | /* Make a ".module/xxxxxxxx" section. */ |
4a6636fb PA |
8604 | /* module_info.base_address */ |
8605 | base_addr = bfd_get_32 (abfd, note->descdata + 4); | |
0af1713e | 8606 | sprintf (buf, ".module/%08lx", (unsigned long) base_addr); |
c044fabd | 8607 | |
d4c88bbb | 8608 | len = strlen (buf) + 1; |
a50b1753 | 8609 | name = (char *) bfd_alloc (abfd, len); |
16e9c715 | 8610 | if (name == NULL) |
b34976b6 | 8611 | return FALSE; |
c044fabd | 8612 | |
d4c88bbb | 8613 | memcpy (name, buf, len); |
252b5132 | 8614 | |
117ed4f8 | 8615 | sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); |
c044fabd | 8616 | |
16e9c715 | 8617 | if (sect == NULL) |
b34976b6 | 8618 | return FALSE; |
c044fabd | 8619 | |
eea6121a | 8620 | sect->size = note->descsz; |
16e9c715 | 8621 | sect->filepos = note->descpos; |
16e9c715 NC |
8622 | sect->alignment_power = 2; |
8623 | break; | |
8624 | ||
8625 | default: | |
b34976b6 | 8626 | return TRUE; |
16e9c715 NC |
8627 | } |
8628 | ||
b34976b6 | 8629 | return TRUE; |
16e9c715 | 8630 | } |
252b5132 | 8631 | |
b34976b6 | 8632 | static bfd_boolean |
217aa764 | 8633 | elfcore_grok_note (bfd *abfd, Elf_Internal_Note *note) |
252b5132 | 8634 | { |
9c5bfbb7 | 8635 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
bb0082d6 | 8636 | |
252b5132 RH |
8637 | switch (note->type) |
8638 | { | |
8639 | default: | |
b34976b6 | 8640 | return TRUE; |
252b5132 | 8641 | |
252b5132 | 8642 | case NT_PRSTATUS: |
bb0082d6 AM |
8643 | if (bed->elf_backend_grok_prstatus) |
8644 | if ((*bed->elf_backend_grok_prstatus) (abfd, note)) | |
b34976b6 | 8645 | return TRUE; |
bb0082d6 | 8646 | #if defined (HAVE_PRSTATUS_T) |
252b5132 | 8647 | return elfcore_grok_prstatus (abfd, note); |
bb0082d6 | 8648 | #else |
b34976b6 | 8649 | return TRUE; |
252b5132 RH |
8650 | #endif |
8651 | ||
8652 | #if defined (HAVE_PSTATUS_T) | |
8653 | case NT_PSTATUS: | |
8654 | return elfcore_grok_pstatus (abfd, note); | |
8655 | #endif | |
8656 | ||
8657 | #if defined (HAVE_LWPSTATUS_T) | |
8658 | case NT_LWPSTATUS: | |
8659 | return elfcore_grok_lwpstatus (abfd, note); | |
8660 | #endif | |
8661 | ||
8662 | case NT_FPREGSET: /* FIXME: rename to NT_PRFPREG */ | |
8663 | return elfcore_grok_prfpreg (abfd, note); | |
8664 | ||
c044fabd | 8665 | case NT_WIN32PSTATUS: |
16e9c715 | 8666 | return elfcore_grok_win32pstatus (abfd, note); |
16e9c715 | 8667 | |
c044fabd | 8668 | case NT_PRXFPREG: /* Linux SSE extension */ |
e377ab71 MK |
8669 | if (note->namesz == 6 |
8670 | && strcmp (note->namedata, "LINUX") == 0) | |
ff08c6bb JB |
8671 | return elfcore_grok_prxfpreg (abfd, note); |
8672 | else | |
b34976b6 | 8673 | return TRUE; |
ff08c6bb | 8674 | |
4339cae0 L |
8675 | case NT_X86_XSTATE: /* Linux XSAVE extension */ |
8676 | if (note->namesz == 6 | |
8677 | && strcmp (note->namedata, "LINUX") == 0) | |
8678 | return elfcore_grok_xstatereg (abfd, note); | |
8679 | else | |
8680 | return TRUE; | |
8681 | ||
97753bd5 AM |
8682 | case NT_PPC_VMX: |
8683 | if (note->namesz == 6 | |
8684 | && strcmp (note->namedata, "LINUX") == 0) | |
8685 | return elfcore_grok_ppc_vmx (abfd, note); | |
8686 | else | |
8687 | return TRUE; | |
8688 | ||
89eeb0bc LM |
8689 | case NT_PPC_VSX: |
8690 | if (note->namesz == 6 | |
8691 | && strcmp (note->namedata, "LINUX") == 0) | |
8692 | return elfcore_grok_ppc_vsx (abfd, note); | |
8693 | else | |
8694 | return TRUE; | |
8695 | ||
0675e188 UW |
8696 | case NT_S390_HIGH_GPRS: |
8697 | if (note->namesz == 6 | |
8698 | && strcmp (note->namedata, "LINUX") == 0) | |
8699 | return elfcore_grok_s390_high_gprs (abfd, note); | |
8700 | else | |
8701 | return TRUE; | |
8702 | ||
d7eeb400 MS |
8703 | case NT_S390_TIMER: |
8704 | if (note->namesz == 6 | |
8705 | && strcmp (note->namedata, "LINUX") == 0) | |
8706 | return elfcore_grok_s390_timer (abfd, note); | |
8707 | else | |
8708 | return TRUE; | |
8709 | ||
8710 | case NT_S390_TODCMP: | |
8711 | if (note->namesz == 6 | |
8712 | && strcmp (note->namedata, "LINUX") == 0) | |
8713 | return elfcore_grok_s390_todcmp (abfd, note); | |
8714 | else | |
8715 | return TRUE; | |
8716 | ||
8717 | case NT_S390_TODPREG: | |
8718 | if (note->namesz == 6 | |
8719 | && strcmp (note->namedata, "LINUX") == 0) | |
8720 | return elfcore_grok_s390_todpreg (abfd, note); | |
8721 | else | |
8722 | return TRUE; | |
8723 | ||
8724 | case NT_S390_CTRS: | |
8725 | if (note->namesz == 6 | |
8726 | && strcmp (note->namedata, "LINUX") == 0) | |
8727 | return elfcore_grok_s390_ctrs (abfd, note); | |
8728 | else | |
8729 | return TRUE; | |
8730 | ||
8731 | case NT_S390_PREFIX: | |
8732 | if (note->namesz == 6 | |
8733 | && strcmp (note->namedata, "LINUX") == 0) | |
8734 | return elfcore_grok_s390_prefix (abfd, note); | |
8735 | else | |
8736 | return TRUE; | |
8737 | ||
355b81d9 UW |
8738 | case NT_S390_LAST_BREAK: |
8739 | if (note->namesz == 6 | |
8740 | && strcmp (note->namedata, "LINUX") == 0) | |
8741 | return elfcore_grok_s390_last_break (abfd, note); | |
8742 | else | |
8743 | return TRUE; | |
8744 | ||
8745 | case NT_S390_SYSTEM_CALL: | |
8746 | if (note->namesz == 6 | |
8747 | && strcmp (note->namedata, "LINUX") == 0) | |
8748 | return elfcore_grok_s390_system_call (abfd, note); | |
8749 | else | |
8750 | return TRUE; | |
8751 | ||
abb3f6cc NC |
8752 | case NT_S390_TDB: |
8753 | if (note->namesz == 6 | |
8754 | && strcmp (note->namedata, "LINUX") == 0) | |
8755 | return elfcore_grok_s390_tdb (abfd, note); | |
8756 | else | |
8757 | return TRUE; | |
8758 | ||
4ef9f41a AA |
8759 | case NT_S390_VXRS_LOW: |
8760 | if (note->namesz == 6 | |
8761 | && strcmp (note->namedata, "LINUX") == 0) | |
8762 | return elfcore_grok_s390_vxrs_low (abfd, note); | |
8763 | else | |
8764 | return TRUE; | |
8765 | ||
8766 | case NT_S390_VXRS_HIGH: | |
8767 | if (note->namesz == 6 | |
8768 | && strcmp (note->namedata, "LINUX") == 0) | |
8769 | return elfcore_grok_s390_vxrs_high (abfd, note); | |
8770 | else | |
8771 | return TRUE; | |
8772 | ||
faa9a424 UW |
8773 | case NT_ARM_VFP: |
8774 | if (note->namesz == 6 | |
8775 | && strcmp (note->namedata, "LINUX") == 0) | |
8776 | return elfcore_grok_arm_vfp (abfd, note); | |
8777 | else | |
8778 | return TRUE; | |
8779 | ||
652451f8 YZ |
8780 | case NT_ARM_TLS: |
8781 | if (note->namesz == 6 | |
8782 | && strcmp (note->namedata, "LINUX") == 0) | |
8783 | return elfcore_grok_aarch_tls (abfd, note); | |
8784 | else | |
8785 | return TRUE; | |
8786 | ||
8787 | case NT_ARM_HW_BREAK: | |
8788 | if (note->namesz == 6 | |
8789 | && strcmp (note->namedata, "LINUX") == 0) | |
8790 | return elfcore_grok_aarch_hw_break (abfd, note); | |
8791 | else | |
8792 | return TRUE; | |
8793 | ||
8794 | case NT_ARM_HW_WATCH: | |
8795 | if (note->namesz == 6 | |
8796 | && strcmp (note->namedata, "LINUX") == 0) | |
8797 | return elfcore_grok_aarch_hw_watch (abfd, note); | |
8798 | else | |
8799 | return TRUE; | |
8800 | ||
252b5132 RH |
8801 | case NT_PRPSINFO: |
8802 | case NT_PSINFO: | |
bb0082d6 AM |
8803 | if (bed->elf_backend_grok_psinfo) |
8804 | if ((*bed->elf_backend_grok_psinfo) (abfd, note)) | |
b34976b6 | 8805 | return TRUE; |
bb0082d6 | 8806 | #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) |
252b5132 | 8807 | return elfcore_grok_psinfo (abfd, note); |
bb0082d6 | 8808 | #else |
b34976b6 | 8809 | return TRUE; |
252b5132 | 8810 | #endif |
3333a7c3 RM |
8811 | |
8812 | case NT_AUXV: | |
8813 | { | |
117ed4f8 AM |
8814 | asection *sect = bfd_make_section_anyway_with_flags (abfd, ".auxv", |
8815 | SEC_HAS_CONTENTS); | |
3333a7c3 RM |
8816 | |
8817 | if (sect == NULL) | |
8818 | return FALSE; | |
eea6121a | 8819 | sect->size = note->descsz; |
3333a7c3 | 8820 | sect->filepos = note->descpos; |
3333a7c3 RM |
8821 | sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32; |
8822 | ||
8823 | return TRUE; | |
8824 | } | |
9015683b | 8825 | |
451b7c33 TT |
8826 | case NT_FILE: |
8827 | return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.file", | |
8828 | note); | |
8829 | ||
9015683b TT |
8830 | case NT_SIGINFO: |
8831 | return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.siginfo", | |
8832 | note); | |
252b5132 RH |
8833 | } |
8834 | } | |
8835 | ||
718175fa JK |
8836 | static bfd_boolean |
8837 | elfobj_grok_gnu_build_id (bfd *abfd, Elf_Internal_Note *note) | |
8838 | { | |
30e8ee25 AM |
8839 | struct elf_obj_tdata *t; |
8840 | ||
8841 | if (note->descsz == 0) | |
8842 | return FALSE; | |
8843 | ||
8844 | t = elf_tdata (abfd); | |
c0355132 | 8845 | t->build_id = bfd_alloc (abfd, sizeof (*t->build_id) - 1 + note->descsz); |
30e8ee25 | 8846 | if (t->build_id == NULL) |
718175fa JK |
8847 | return FALSE; |
8848 | ||
c0355132 AM |
8849 | t->build_id->size = note->descsz; |
8850 | memcpy (t->build_id->data, note->descdata, note->descsz); | |
718175fa JK |
8851 | |
8852 | return TRUE; | |
8853 | } | |
8854 | ||
8855 | static bfd_boolean | |
8856 | elfobj_grok_gnu_note (bfd *abfd, Elf_Internal_Note *note) | |
8857 | { | |
8858 | switch (note->type) | |
8859 | { | |
8860 | default: | |
8861 | return TRUE; | |
8862 | ||
8863 | case NT_GNU_BUILD_ID: | |
8864 | return elfobj_grok_gnu_build_id (abfd, note); | |
8865 | } | |
8866 | } | |
8867 | ||
e21e5835 NC |
8868 | static bfd_boolean |
8869 | elfobj_grok_stapsdt_note_1 (bfd *abfd, Elf_Internal_Note *note) | |
8870 | { | |
8871 | struct sdt_note *cur = | |
8872 | (struct sdt_note *) bfd_alloc (abfd, sizeof (struct sdt_note) | |
8873 | + note->descsz); | |
8874 | ||
8875 | cur->next = (struct sdt_note *) (elf_tdata (abfd))->sdt_note_head; | |
8876 | cur->size = (bfd_size_type) note->descsz; | |
8877 | memcpy (cur->data, note->descdata, note->descsz); | |
8878 | ||
8879 | elf_tdata (abfd)->sdt_note_head = cur; | |
8880 | ||
8881 | return TRUE; | |
8882 | } | |
8883 | ||
8884 | static bfd_boolean | |
8885 | elfobj_grok_stapsdt_note (bfd *abfd, Elf_Internal_Note *note) | |
8886 | { | |
8887 | switch (note->type) | |
8888 | { | |
8889 | case NT_STAPSDT: | |
8890 | return elfobj_grok_stapsdt_note_1 (abfd, note); | |
8891 | ||
8892 | default: | |
8893 | return TRUE; | |
8894 | } | |
8895 | } | |
8896 | ||
b34976b6 | 8897 | static bfd_boolean |
217aa764 | 8898 | elfcore_netbsd_get_lwpid (Elf_Internal_Note *note, int *lwpidp) |
50b2bdb7 AM |
8899 | { |
8900 | char *cp; | |
8901 | ||
8902 | cp = strchr (note->namedata, '@'); | |
8903 | if (cp != NULL) | |
8904 | { | |
d2b64500 | 8905 | *lwpidp = atoi(cp + 1); |
b34976b6 | 8906 | return TRUE; |
50b2bdb7 | 8907 | } |
b34976b6 | 8908 | return FALSE; |
50b2bdb7 AM |
8909 | } |
8910 | ||
b34976b6 | 8911 | static bfd_boolean |
217aa764 | 8912 | elfcore_grok_netbsd_procinfo (bfd *abfd, Elf_Internal_Note *note) |
50b2bdb7 | 8913 | { |
50b2bdb7 | 8914 | /* Signal number at offset 0x08. */ |
228e534f | 8915 | elf_tdata (abfd)->core->signal |
50b2bdb7 AM |
8916 | = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08); |
8917 | ||
8918 | /* Process ID at offset 0x50. */ | |
228e534f | 8919 | elf_tdata (abfd)->core->pid |
50b2bdb7 AM |
8920 | = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x50); |
8921 | ||
8922 | /* Command name at 0x7c (max 32 bytes, including nul). */ | |
228e534f | 8923 | elf_tdata (abfd)->core->command |
50b2bdb7 AM |
8924 | = _bfd_elfcore_strndup (abfd, note->descdata + 0x7c, 31); |
8925 | ||
7720ba9f MK |
8926 | return elfcore_make_note_pseudosection (abfd, ".note.netbsdcore.procinfo", |
8927 | note); | |
50b2bdb7 AM |
8928 | } |
8929 | ||
b34976b6 | 8930 | static bfd_boolean |
217aa764 | 8931 | elfcore_grok_netbsd_note (bfd *abfd, Elf_Internal_Note *note) |
50b2bdb7 AM |
8932 | { |
8933 | int lwp; | |
8934 | ||
8935 | if (elfcore_netbsd_get_lwpid (note, &lwp)) | |
228e534f | 8936 | elf_tdata (abfd)->core->lwpid = lwp; |
50b2bdb7 | 8937 | |
b4db1224 | 8938 | if (note->type == NT_NETBSDCORE_PROCINFO) |
50b2bdb7 AM |
8939 | { |
8940 | /* NetBSD-specific core "procinfo". Note that we expect to | |
08a40648 AM |
8941 | find this note before any of the others, which is fine, |
8942 | since the kernel writes this note out first when it | |
8943 | creates a core file. */ | |
47d9a591 | 8944 | |
50b2bdb7 AM |
8945 | return elfcore_grok_netbsd_procinfo (abfd, note); |
8946 | } | |
8947 | ||
b4db1224 JT |
8948 | /* As of Jan 2002 there are no other machine-independent notes |
8949 | defined for NetBSD core files. If the note type is less | |
8950 | than the start of the machine-dependent note types, we don't | |
8951 | understand it. */ | |
47d9a591 | 8952 | |
b4db1224 | 8953 | if (note->type < NT_NETBSDCORE_FIRSTMACH) |
b34976b6 | 8954 | return TRUE; |
50b2bdb7 AM |
8955 | |
8956 | ||
8957 | switch (bfd_get_arch (abfd)) | |
8958 | { | |
08a40648 AM |
8959 | /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and |
8960 | PT_GETFPREGS == mach+2. */ | |
50b2bdb7 AM |
8961 | |
8962 | case bfd_arch_alpha: | |
8963 | case bfd_arch_sparc: | |
8964 | switch (note->type) | |
08a40648 AM |
8965 | { |
8966 | case NT_NETBSDCORE_FIRSTMACH+0: | |
8967 | return elfcore_make_note_pseudosection (abfd, ".reg", note); | |
50b2bdb7 | 8968 | |
08a40648 AM |
8969 | case NT_NETBSDCORE_FIRSTMACH+2: |
8970 | return elfcore_make_note_pseudosection (abfd, ".reg2", note); | |
50b2bdb7 | 8971 | |
08a40648 AM |
8972 | default: |
8973 | return TRUE; | |
8974 | } | |
50b2bdb7 | 8975 | |
08a40648 AM |
8976 | /* On all other arch's, PT_GETREGS == mach+1 and |
8977 | PT_GETFPREGS == mach+3. */ | |
50b2bdb7 AM |
8978 | |
8979 | default: | |
8980 | switch (note->type) | |
08a40648 AM |
8981 | { |
8982 | case NT_NETBSDCORE_FIRSTMACH+1: | |
8983 | return elfcore_make_note_pseudosection (abfd, ".reg", note); | |
50b2bdb7 | 8984 | |
08a40648 AM |
8985 | case NT_NETBSDCORE_FIRSTMACH+3: |
8986 | return elfcore_make_note_pseudosection (abfd, ".reg2", note); | |
50b2bdb7 | 8987 | |
08a40648 AM |
8988 | default: |
8989 | return TRUE; | |
8990 | } | |
50b2bdb7 AM |
8991 | } |
8992 | /* NOTREACHED */ | |
8993 | } | |
8994 | ||
67cc5033 MK |
8995 | static bfd_boolean |
8996 | elfcore_grok_openbsd_procinfo (bfd *abfd, Elf_Internal_Note *note) | |
8997 | { | |
8998 | /* Signal number at offset 0x08. */ | |
228e534f | 8999 | elf_tdata (abfd)->core->signal |
67cc5033 MK |
9000 | = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08); |
9001 | ||
9002 | /* Process ID at offset 0x20. */ | |
228e534f | 9003 | elf_tdata (abfd)->core->pid |
67cc5033 MK |
9004 | = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x20); |
9005 | ||
9006 | /* Command name at 0x48 (max 32 bytes, including nul). */ | |
228e534f | 9007 | elf_tdata (abfd)->core->command |
67cc5033 MK |
9008 | = _bfd_elfcore_strndup (abfd, note->descdata + 0x48, 31); |
9009 | ||
9010 | return TRUE; | |
9011 | } | |
9012 | ||
9013 | static bfd_boolean | |
9014 | elfcore_grok_openbsd_note (bfd *abfd, Elf_Internal_Note *note) | |
9015 | { | |
9016 | if (note->type == NT_OPENBSD_PROCINFO) | |
9017 | return elfcore_grok_openbsd_procinfo (abfd, note); | |
9018 | ||
9019 | if (note->type == NT_OPENBSD_REGS) | |
9020 | return elfcore_make_note_pseudosection (abfd, ".reg", note); | |
9021 | ||
9022 | if (note->type == NT_OPENBSD_FPREGS) | |
9023 | return elfcore_make_note_pseudosection (abfd, ".reg2", note); | |
9024 | ||
9025 | if (note->type == NT_OPENBSD_XFPREGS) | |
9026 | return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note); | |
9027 | ||
9028 | if (note->type == NT_OPENBSD_AUXV) | |
9029 | { | |
9030 | asection *sect = bfd_make_section_anyway_with_flags (abfd, ".auxv", | |
9031 | SEC_HAS_CONTENTS); | |
9032 | ||
9033 | if (sect == NULL) | |
9034 | return FALSE; | |
9035 | sect->size = note->descsz; | |
9036 | sect->filepos = note->descpos; | |
9037 | sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32; | |
9038 | ||
9039 | return TRUE; | |
9040 | } | |
9041 | ||
9042 | if (note->type == NT_OPENBSD_WCOOKIE) | |
9043 | { | |
9044 | asection *sect = bfd_make_section_anyway_with_flags (abfd, ".wcookie", | |
9045 | SEC_HAS_CONTENTS); | |
9046 | ||
9047 | if (sect == NULL) | |
9048 | return FALSE; | |
9049 | sect->size = note->descsz; | |
9050 | sect->filepos = note->descpos; | |
9051 | sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32; | |
9052 | ||
9053 | return TRUE; | |
9054 | } | |
9055 | ||
9056 | return TRUE; | |
9057 | } | |
9058 | ||
07c6e936 | 9059 | static bfd_boolean |
d3fd4074 | 9060 | elfcore_grok_nto_status (bfd *abfd, Elf_Internal_Note *note, long *tid) |
07c6e936 NC |
9061 | { |
9062 | void *ddata = note->descdata; | |
9063 | char buf[100]; | |
9064 | char *name; | |
9065 | asection *sect; | |
f8843e87 AM |
9066 | short sig; |
9067 | unsigned flags; | |
07c6e936 NC |
9068 | |
9069 | /* nto_procfs_status 'pid' field is at offset 0. */ | |
228e534f | 9070 | elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, (bfd_byte *) ddata); |
07c6e936 | 9071 | |
f8843e87 AM |
9072 | /* nto_procfs_status 'tid' field is at offset 4. Pass it back. */ |
9073 | *tid = bfd_get_32 (abfd, (bfd_byte *) ddata + 4); | |
9074 | ||
9075 | /* nto_procfs_status 'flags' field is at offset 8. */ | |
9076 | flags = bfd_get_32 (abfd, (bfd_byte *) ddata + 8); | |
07c6e936 NC |
9077 | |
9078 | /* nto_procfs_status 'what' field is at offset 14. */ | |
f8843e87 AM |
9079 | if ((sig = bfd_get_16 (abfd, (bfd_byte *) ddata + 14)) > 0) |
9080 | { | |
228e534f AM |
9081 | elf_tdata (abfd)->core->signal = sig; |
9082 | elf_tdata (abfd)->core->lwpid = *tid; | |
f8843e87 | 9083 | } |
07c6e936 | 9084 | |
f8843e87 AM |
9085 | /* _DEBUG_FLAG_CURTID (current thread) is 0x80. Some cores |
9086 | do not come from signals so we make sure we set the current | |
9087 | thread just in case. */ | |
9088 | if (flags & 0x00000080) | |
228e534f | 9089 | elf_tdata (abfd)->core->lwpid = *tid; |
07c6e936 NC |
9090 | |
9091 | /* Make a ".qnx_core_status/%d" section. */ | |
d3fd4074 | 9092 | sprintf (buf, ".qnx_core_status/%ld", *tid); |
07c6e936 | 9093 | |
a50b1753 | 9094 | name = (char *) bfd_alloc (abfd, strlen (buf) + 1); |
07c6e936 NC |
9095 | if (name == NULL) |
9096 | return FALSE; | |
9097 | strcpy (name, buf); | |
9098 | ||
117ed4f8 | 9099 | sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); |
07c6e936 NC |
9100 | if (sect == NULL) |
9101 | return FALSE; | |
9102 | ||
eea6121a | 9103 | sect->size = note->descsz; |
07c6e936 | 9104 | sect->filepos = note->descpos; |
07c6e936 NC |
9105 | sect->alignment_power = 2; |
9106 | ||
9107 | return (elfcore_maybe_make_sect (abfd, ".qnx_core_status", sect)); | |
9108 | } | |
9109 | ||
9110 | static bfd_boolean | |
d69f560c KW |
9111 | elfcore_grok_nto_regs (bfd *abfd, |
9112 | Elf_Internal_Note *note, | |
d3fd4074 | 9113 | long tid, |
d69f560c | 9114 | char *base) |
07c6e936 NC |
9115 | { |
9116 | char buf[100]; | |
9117 | char *name; | |
9118 | asection *sect; | |
9119 | ||
d69f560c | 9120 | /* Make a "(base)/%d" section. */ |
d3fd4074 | 9121 | sprintf (buf, "%s/%ld", base, tid); |
07c6e936 | 9122 | |
a50b1753 | 9123 | name = (char *) bfd_alloc (abfd, strlen (buf) + 1); |
07c6e936 NC |
9124 | if (name == NULL) |
9125 | return FALSE; | |
9126 | strcpy (name, buf); | |
9127 | ||
117ed4f8 | 9128 | sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); |
07c6e936 NC |
9129 | if (sect == NULL) |
9130 | return FALSE; | |
9131 | ||
eea6121a | 9132 | sect->size = note->descsz; |
07c6e936 | 9133 | sect->filepos = note->descpos; |
07c6e936 NC |
9134 | sect->alignment_power = 2; |
9135 | ||
f8843e87 | 9136 | /* This is the current thread. */ |
228e534f | 9137 | if (elf_tdata (abfd)->core->lwpid == tid) |
d69f560c | 9138 | return elfcore_maybe_make_sect (abfd, base, sect); |
f8843e87 AM |
9139 | |
9140 | return TRUE; | |
07c6e936 NC |
9141 | } |
9142 | ||
9143 | #define BFD_QNT_CORE_INFO 7 | |
9144 | #define BFD_QNT_CORE_STATUS 8 | |
9145 | #define BFD_QNT_CORE_GREG 9 | |
9146 | #define BFD_QNT_CORE_FPREG 10 | |
9147 | ||
9148 | static bfd_boolean | |
217aa764 | 9149 | elfcore_grok_nto_note (bfd *abfd, Elf_Internal_Note *note) |
07c6e936 NC |
9150 | { |
9151 | /* Every GREG section has a STATUS section before it. Store the | |
811072d8 | 9152 | tid from the previous call to pass down to the next gregs |
07c6e936 | 9153 | function. */ |
d3fd4074 | 9154 | static long tid = 1; |
07c6e936 NC |
9155 | |
9156 | switch (note->type) | |
9157 | { | |
d69f560c KW |
9158 | case BFD_QNT_CORE_INFO: |
9159 | return elfcore_make_note_pseudosection (abfd, ".qnx_core_info", note); | |
9160 | case BFD_QNT_CORE_STATUS: | |
9161 | return elfcore_grok_nto_status (abfd, note, &tid); | |
9162 | case BFD_QNT_CORE_GREG: | |
9163 | return elfcore_grok_nto_regs (abfd, note, tid, ".reg"); | |
9164 | case BFD_QNT_CORE_FPREG: | |
9165 | return elfcore_grok_nto_regs (abfd, note, tid, ".reg2"); | |
9166 | default: | |
9167 | return TRUE; | |
07c6e936 NC |
9168 | } |
9169 | } | |
9170 | ||
b15fa79e AM |
9171 | static bfd_boolean |
9172 | elfcore_grok_spu_note (bfd *abfd, Elf_Internal_Note *note) | |
9173 | { | |
9174 | char *name; | |
9175 | asection *sect; | |
9176 | size_t len; | |
9177 | ||
9178 | /* Use note name as section name. */ | |
9179 | len = note->namesz; | |
a50b1753 | 9180 | name = (char *) bfd_alloc (abfd, len); |
b15fa79e AM |
9181 | if (name == NULL) |
9182 | return FALSE; | |
9183 | memcpy (name, note->namedata, len); | |
9184 | name[len - 1] = '\0'; | |
9185 | ||
9186 | sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); | |
9187 | if (sect == NULL) | |
9188 | return FALSE; | |
9189 | ||
9190 | sect->size = note->descsz; | |
9191 | sect->filepos = note->descpos; | |
9192 | sect->alignment_power = 1; | |
9193 | ||
9194 | return TRUE; | |
9195 | } | |
9196 | ||
7c76fa91 MS |
9197 | /* Function: elfcore_write_note |
9198 | ||
47d9a591 | 9199 | Inputs: |
a39f3346 | 9200 | buffer to hold note, and current size of buffer |
7c76fa91 MS |
9201 | name of note |
9202 | type of note | |
9203 | data for note | |
9204 | size of data for note | |
9205 | ||
a39f3346 AM |
9206 | Writes note to end of buffer. ELF64 notes are written exactly as |
9207 | for ELF32, despite the current (as of 2006) ELF gabi specifying | |
9208 | that they ought to have 8-byte namesz and descsz field, and have | |
9209 | 8-byte alignment. Other writers, eg. Linux kernel, do the same. | |
9210 | ||
7c76fa91 | 9211 | Return: |
a39f3346 | 9212 | Pointer to realloc'd buffer, *BUFSIZ updated. */ |
7c76fa91 MS |
9213 | |
9214 | char * | |
a39f3346 | 9215 | elfcore_write_note (bfd *abfd, |
217aa764 | 9216 | char *buf, |
a39f3346 | 9217 | int *bufsiz, |
217aa764 | 9218 | const char *name, |
a39f3346 | 9219 | int type, |
217aa764 | 9220 | const void *input, |
a39f3346 | 9221 | int size) |
7c76fa91 MS |
9222 | { |
9223 | Elf_External_Note *xnp; | |
d4c88bbb | 9224 | size_t namesz; |
d4c88bbb | 9225 | size_t newspace; |
a39f3346 | 9226 | char *dest; |
7c76fa91 | 9227 | |
d4c88bbb | 9228 | namesz = 0; |
d4c88bbb | 9229 | if (name != NULL) |
a39f3346 | 9230 | namesz = strlen (name) + 1; |
d4c88bbb | 9231 | |
a39f3346 | 9232 | newspace = 12 + ((namesz + 3) & -4) + ((size + 3) & -4); |
d4c88bbb | 9233 | |
a50b1753 | 9234 | buf = (char *) realloc (buf, *bufsiz + newspace); |
14b1c01e AM |
9235 | if (buf == NULL) |
9236 | return buf; | |
a39f3346 | 9237 | dest = buf + *bufsiz; |
7c76fa91 MS |
9238 | *bufsiz += newspace; |
9239 | xnp = (Elf_External_Note *) dest; | |
9240 | H_PUT_32 (abfd, namesz, xnp->namesz); | |
9241 | H_PUT_32 (abfd, size, xnp->descsz); | |
9242 | H_PUT_32 (abfd, type, xnp->type); | |
d4c88bbb AM |
9243 | dest = xnp->name; |
9244 | if (name != NULL) | |
9245 | { | |
9246 | memcpy (dest, name, namesz); | |
9247 | dest += namesz; | |
a39f3346 | 9248 | while (namesz & 3) |
d4c88bbb AM |
9249 | { |
9250 | *dest++ = '\0'; | |
a39f3346 | 9251 | ++namesz; |
d4c88bbb AM |
9252 | } |
9253 | } | |
9254 | memcpy (dest, input, size); | |
a39f3346 AM |
9255 | dest += size; |
9256 | while (size & 3) | |
9257 | { | |
9258 | *dest++ = '\0'; | |
9259 | ++size; | |
9260 | } | |
9261 | return buf; | |
7c76fa91 MS |
9262 | } |
9263 | ||
7c76fa91 | 9264 | char * |
217aa764 AM |
9265 | elfcore_write_prpsinfo (bfd *abfd, |
9266 | char *buf, | |
9267 | int *bufsiz, | |
9268 | const char *fname, | |
9269 | const char *psargs) | |
7c76fa91 | 9270 | { |
183e98be AM |
9271 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
9272 | ||
9273 | if (bed->elf_backend_write_core_note != NULL) | |
9274 | { | |
9275 | char *ret; | |
9276 | ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz, | |
9277 | NT_PRPSINFO, fname, psargs); | |
9278 | if (ret != NULL) | |
9279 | return ret; | |
9280 | } | |
7c76fa91 | 9281 | |
1f20dca5 | 9282 | #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) |
183e98be AM |
9283 | #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T) |
9284 | if (bed->s->elfclass == ELFCLASS32) | |
9285 | { | |
9286 | #if defined (HAVE_PSINFO32_T) | |
9287 | psinfo32_t data; | |
9288 | int note_type = NT_PSINFO; | |
9289 | #else | |
9290 | prpsinfo32_t data; | |
9291 | int note_type = NT_PRPSINFO; | |
9292 | #endif | |
9293 | ||
9294 | memset (&data, 0, sizeof (data)); | |
9295 | strncpy (data.pr_fname, fname, sizeof (data.pr_fname)); | |
9296 | strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs)); | |
9297 | return elfcore_write_note (abfd, buf, bufsiz, | |
1f20dca5 | 9298 | "CORE", note_type, &data, sizeof (data)); |
183e98be AM |
9299 | } |
9300 | else | |
9301 | #endif | |
9302 | { | |
7c76fa91 | 9303 | #if defined (HAVE_PSINFO_T) |
183e98be AM |
9304 | psinfo_t data; |
9305 | int note_type = NT_PSINFO; | |
7c76fa91 | 9306 | #else |
183e98be AM |
9307 | prpsinfo_t data; |
9308 | int note_type = NT_PRPSINFO; | |
7c76fa91 MS |
9309 | #endif |
9310 | ||
183e98be AM |
9311 | memset (&data, 0, sizeof (data)); |
9312 | strncpy (data.pr_fname, fname, sizeof (data.pr_fname)); | |
9313 | strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs)); | |
9314 | return elfcore_write_note (abfd, buf, bufsiz, | |
1f20dca5 | 9315 | "CORE", note_type, &data, sizeof (data)); |
183e98be | 9316 | } |
7c76fa91 MS |
9317 | #endif /* PSINFO_T or PRPSINFO_T */ |
9318 | ||
1f20dca5 UW |
9319 | free (buf); |
9320 | return NULL; | |
9321 | } | |
9322 | ||
70a38d42 SDJ |
9323 | char * |
9324 | elfcore_write_linux_prpsinfo32 | |
9325 | (bfd *abfd, char *buf, int *bufsiz, | |
9326 | const struct elf_internal_linux_prpsinfo *prpsinfo) | |
9327 | { | |
9328 | struct elf_external_linux_prpsinfo32 data; | |
9329 | ||
9330 | memset (&data, 0, sizeof (data)); | |
9331 | LINUX_PRPSINFO32_SWAP_FIELDS (abfd, prpsinfo, data); | |
9332 | ||
9333 | return elfcore_write_note (abfd, buf, bufsiz, "CORE", NT_PRPSINFO, | |
9334 | &data, sizeof (data)); | |
9335 | } | |
9336 | ||
9337 | char * | |
9338 | elfcore_write_linux_prpsinfo64 | |
9339 | (bfd *abfd, char *buf, int *bufsiz, | |
9340 | const struct elf_internal_linux_prpsinfo *prpsinfo) | |
9341 | { | |
9342 | struct elf_external_linux_prpsinfo64 data; | |
9343 | ||
9344 | memset (&data, 0, sizeof (data)); | |
9345 | LINUX_PRPSINFO64_SWAP_FIELDS (abfd, prpsinfo, data); | |
9346 | ||
9347 | return elfcore_write_note (abfd, buf, bufsiz, | |
9348 | "CORE", NT_PRPSINFO, &data, sizeof (data)); | |
9349 | } | |
9350 | ||
7c76fa91 | 9351 | char * |
217aa764 AM |
9352 | elfcore_write_prstatus (bfd *abfd, |
9353 | char *buf, | |
9354 | int *bufsiz, | |
9355 | long pid, | |
9356 | int cursig, | |
9357 | const void *gregs) | |
7c76fa91 | 9358 | { |
183e98be | 9359 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
7c76fa91 | 9360 | |
183e98be AM |
9361 | if (bed->elf_backend_write_core_note != NULL) |
9362 | { | |
9363 | char *ret; | |
9364 | ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz, | |
9365 | NT_PRSTATUS, | |
9366 | pid, cursig, gregs); | |
9367 | if (ret != NULL) | |
9368 | return ret; | |
9369 | } | |
9370 | ||
1f20dca5 | 9371 | #if defined (HAVE_PRSTATUS_T) |
183e98be AM |
9372 | #if defined (HAVE_PRSTATUS32_T) |
9373 | if (bed->s->elfclass == ELFCLASS32) | |
9374 | { | |
9375 | prstatus32_t prstat; | |
9376 | ||
9377 | memset (&prstat, 0, sizeof (prstat)); | |
9378 | prstat.pr_pid = pid; | |
9379 | prstat.pr_cursig = cursig; | |
9380 | memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg)); | |
1f20dca5 | 9381 | return elfcore_write_note (abfd, buf, bufsiz, "CORE", |
183e98be AM |
9382 | NT_PRSTATUS, &prstat, sizeof (prstat)); |
9383 | } | |
9384 | else | |
9385 | #endif | |
9386 | { | |
9387 | prstatus_t prstat; | |
9388 | ||
9389 | memset (&prstat, 0, sizeof (prstat)); | |
9390 | prstat.pr_pid = pid; | |
9391 | prstat.pr_cursig = cursig; | |
9392 | memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg)); | |
1f20dca5 | 9393 | return elfcore_write_note (abfd, buf, bufsiz, "CORE", |
183e98be AM |
9394 | NT_PRSTATUS, &prstat, sizeof (prstat)); |
9395 | } | |
7c76fa91 MS |
9396 | #endif /* HAVE_PRSTATUS_T */ |
9397 | ||
1f20dca5 UW |
9398 | free (buf); |
9399 | return NULL; | |
9400 | } | |
9401 | ||
51316059 MS |
9402 | #if defined (HAVE_LWPSTATUS_T) |
9403 | char * | |
217aa764 AM |
9404 | elfcore_write_lwpstatus (bfd *abfd, |
9405 | char *buf, | |
9406 | int *bufsiz, | |
9407 | long pid, | |
9408 | int cursig, | |
9409 | const void *gregs) | |
51316059 MS |
9410 | { |
9411 | lwpstatus_t lwpstat; | |
183e98be | 9412 | const char *note_name = "CORE"; |
51316059 MS |
9413 | |
9414 | memset (&lwpstat, 0, sizeof (lwpstat)); | |
9415 | lwpstat.pr_lwpid = pid >> 16; | |
9416 | lwpstat.pr_cursig = cursig; | |
9417 | #if defined (HAVE_LWPSTATUS_T_PR_REG) | |
d1e8523e | 9418 | memcpy (&lwpstat.pr_reg, gregs, sizeof (lwpstat.pr_reg)); |
51316059 MS |
9419 | #elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT) |
9420 | #if !defined(gregs) | |
9421 | memcpy (lwpstat.pr_context.uc_mcontext.gregs, | |
9422 | gregs, sizeof (lwpstat.pr_context.uc_mcontext.gregs)); | |
9423 | #else | |
9424 | memcpy (lwpstat.pr_context.uc_mcontext.__gregs, | |
9425 | gregs, sizeof (lwpstat.pr_context.uc_mcontext.__gregs)); | |
9426 | #endif | |
9427 | #endif | |
47d9a591 | 9428 | return elfcore_write_note (abfd, buf, bufsiz, note_name, |
51316059 MS |
9429 | NT_LWPSTATUS, &lwpstat, sizeof (lwpstat)); |
9430 | } | |
9431 | #endif /* HAVE_LWPSTATUS_T */ | |
9432 | ||
7c76fa91 MS |
9433 | #if defined (HAVE_PSTATUS_T) |
9434 | char * | |
217aa764 AM |
9435 | elfcore_write_pstatus (bfd *abfd, |
9436 | char *buf, | |
9437 | int *bufsiz, | |
9438 | long pid, | |
6c10990d NC |
9439 | int cursig ATTRIBUTE_UNUSED, |
9440 | const void *gregs ATTRIBUTE_UNUSED) | |
7c76fa91 | 9441 | { |
183e98be AM |
9442 | const char *note_name = "CORE"; |
9443 | #if defined (HAVE_PSTATUS32_T) | |
9444 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
7c76fa91 | 9445 | |
183e98be AM |
9446 | if (bed->s->elfclass == ELFCLASS32) |
9447 | { | |
9448 | pstatus32_t pstat; | |
9449 | ||
9450 | memset (&pstat, 0, sizeof (pstat)); | |
9451 | pstat.pr_pid = pid & 0xffff; | |
9452 | buf = elfcore_write_note (abfd, buf, bufsiz, note_name, | |
9453 | NT_PSTATUS, &pstat, sizeof (pstat)); | |
9454 | return buf; | |
9455 | } | |
9456 | else | |
9457 | #endif | |
9458 | { | |
9459 | pstatus_t pstat; | |
9460 | ||
9461 | memset (&pstat, 0, sizeof (pstat)); | |
9462 | pstat.pr_pid = pid & 0xffff; | |
9463 | buf = elfcore_write_note (abfd, buf, bufsiz, note_name, | |
9464 | NT_PSTATUS, &pstat, sizeof (pstat)); | |
9465 | return buf; | |
9466 | } | |
7c76fa91 MS |
9467 | } |
9468 | #endif /* HAVE_PSTATUS_T */ | |
9469 | ||
9470 | char * | |
217aa764 AM |
9471 | elfcore_write_prfpreg (bfd *abfd, |
9472 | char *buf, | |
9473 | int *bufsiz, | |
9474 | const void *fpregs, | |
9475 | int size) | |
7c76fa91 | 9476 | { |
183e98be | 9477 | const char *note_name = "CORE"; |
47d9a591 | 9478 | return elfcore_write_note (abfd, buf, bufsiz, |
7c76fa91 MS |
9479 | note_name, NT_FPREGSET, fpregs, size); |
9480 | } | |
9481 | ||
9482 | char * | |
217aa764 AM |
9483 | elfcore_write_prxfpreg (bfd *abfd, |
9484 | char *buf, | |
9485 | int *bufsiz, | |
9486 | const void *xfpregs, | |
9487 | int size) | |
7c76fa91 MS |
9488 | { |
9489 | char *note_name = "LINUX"; | |
47d9a591 | 9490 | return elfcore_write_note (abfd, buf, bufsiz, |
7c76fa91 MS |
9491 | note_name, NT_PRXFPREG, xfpregs, size); |
9492 | } | |
9493 | ||
4339cae0 L |
9494 | char * |
9495 | elfcore_write_xstatereg (bfd *abfd, char *buf, int *bufsiz, | |
9496 | const void *xfpregs, int size) | |
9497 | { | |
9498 | char *note_name = "LINUX"; | |
9499 | return elfcore_write_note (abfd, buf, bufsiz, | |
9500 | note_name, NT_X86_XSTATE, xfpregs, size); | |
9501 | } | |
9502 | ||
97753bd5 AM |
9503 | char * |
9504 | elfcore_write_ppc_vmx (bfd *abfd, | |
9505 | char *buf, | |
9506 | int *bufsiz, | |
9507 | const void *ppc_vmx, | |
9508 | int size) | |
9509 | { | |
9510 | char *note_name = "LINUX"; | |
9511 | return elfcore_write_note (abfd, buf, bufsiz, | |
9512 | note_name, NT_PPC_VMX, ppc_vmx, size); | |
9513 | } | |
9514 | ||
89eeb0bc LM |
9515 | char * |
9516 | elfcore_write_ppc_vsx (bfd *abfd, | |
9517 | char *buf, | |
9518 | int *bufsiz, | |
9519 | const void *ppc_vsx, | |
9520 | int size) | |
9521 | { | |
9522 | char *note_name = "LINUX"; | |
9523 | return elfcore_write_note (abfd, buf, bufsiz, | |
9524 | note_name, NT_PPC_VSX, ppc_vsx, size); | |
9525 | } | |
9526 | ||
0675e188 UW |
9527 | static char * |
9528 | elfcore_write_s390_high_gprs (bfd *abfd, | |
9529 | char *buf, | |
9530 | int *bufsiz, | |
9531 | const void *s390_high_gprs, | |
9532 | int size) | |
9533 | { | |
9534 | char *note_name = "LINUX"; | |
9535 | return elfcore_write_note (abfd, buf, bufsiz, | |
9536 | note_name, NT_S390_HIGH_GPRS, | |
9537 | s390_high_gprs, size); | |
9538 | } | |
9539 | ||
d7eeb400 MS |
9540 | char * |
9541 | elfcore_write_s390_timer (bfd *abfd, | |
9542 | char *buf, | |
9543 | int *bufsiz, | |
9544 | const void *s390_timer, | |
9545 | int size) | |
9546 | { | |
9547 | char *note_name = "LINUX"; | |
9548 | return elfcore_write_note (abfd, buf, bufsiz, | |
9549 | note_name, NT_S390_TIMER, s390_timer, size); | |
9550 | } | |
9551 | ||
9552 | char * | |
9553 | elfcore_write_s390_todcmp (bfd *abfd, | |
9554 | char *buf, | |
9555 | int *bufsiz, | |
9556 | const void *s390_todcmp, | |
9557 | int size) | |
9558 | { | |
9559 | char *note_name = "LINUX"; | |
9560 | return elfcore_write_note (abfd, buf, bufsiz, | |
9561 | note_name, NT_S390_TODCMP, s390_todcmp, size); | |
9562 | } | |
9563 | ||
9564 | char * | |
9565 | elfcore_write_s390_todpreg (bfd *abfd, | |
9566 | char *buf, | |
9567 | int *bufsiz, | |
9568 | const void *s390_todpreg, | |
9569 | int size) | |
9570 | { | |
9571 | char *note_name = "LINUX"; | |
9572 | return elfcore_write_note (abfd, buf, bufsiz, | |
9573 | note_name, NT_S390_TODPREG, s390_todpreg, size); | |
9574 | } | |
9575 | ||
9576 | char * | |
9577 | elfcore_write_s390_ctrs (bfd *abfd, | |
9578 | char *buf, | |
9579 | int *bufsiz, | |
9580 | const void *s390_ctrs, | |
9581 | int size) | |
9582 | { | |
9583 | char *note_name = "LINUX"; | |
9584 | return elfcore_write_note (abfd, buf, bufsiz, | |
9585 | note_name, NT_S390_CTRS, s390_ctrs, size); | |
9586 | } | |
9587 | ||
9588 | char * | |
9589 | elfcore_write_s390_prefix (bfd *abfd, | |
9590 | char *buf, | |
9591 | int *bufsiz, | |
9592 | const void *s390_prefix, | |
9593 | int size) | |
9594 | { | |
9595 | char *note_name = "LINUX"; | |
9596 | return elfcore_write_note (abfd, buf, bufsiz, | |
9597 | note_name, NT_S390_PREFIX, s390_prefix, size); | |
9598 | } | |
9599 | ||
355b81d9 UW |
9600 | char * |
9601 | elfcore_write_s390_last_break (bfd *abfd, | |
9602 | char *buf, | |
9603 | int *bufsiz, | |
9604 | const void *s390_last_break, | |
9605 | int size) | |
9606 | { | |
9607 | char *note_name = "LINUX"; | |
9608 | return elfcore_write_note (abfd, buf, bufsiz, | |
9609 | note_name, NT_S390_LAST_BREAK, | |
9610 | s390_last_break, size); | |
9611 | } | |
9612 | ||
9613 | char * | |
9614 | elfcore_write_s390_system_call (bfd *abfd, | |
9615 | char *buf, | |
9616 | int *bufsiz, | |
9617 | const void *s390_system_call, | |
9618 | int size) | |
9619 | { | |
9620 | char *note_name = "LINUX"; | |
9621 | return elfcore_write_note (abfd, buf, bufsiz, | |
9622 | note_name, NT_S390_SYSTEM_CALL, | |
9623 | s390_system_call, size); | |
9624 | } | |
9625 | ||
abb3f6cc NC |
9626 | char * |
9627 | elfcore_write_s390_tdb (bfd *abfd, | |
9628 | char *buf, | |
9629 | int *bufsiz, | |
9630 | const void *s390_tdb, | |
9631 | int size) | |
9632 | { | |
9633 | char *note_name = "LINUX"; | |
9634 | return elfcore_write_note (abfd, buf, bufsiz, | |
9635 | note_name, NT_S390_TDB, s390_tdb, size); | |
9636 | } | |
9637 | ||
4ef9f41a AA |
9638 | char * |
9639 | elfcore_write_s390_vxrs_low (bfd *abfd, | |
9640 | char *buf, | |
9641 | int *bufsiz, | |
9642 | const void *s390_vxrs_low, | |
9643 | int size) | |
9644 | { | |
9645 | char *note_name = "LINUX"; | |
9646 | return elfcore_write_note (abfd, buf, bufsiz, | |
9647 | note_name, NT_S390_VXRS_LOW, s390_vxrs_low, size); | |
9648 | } | |
9649 | ||
9650 | char * | |
9651 | elfcore_write_s390_vxrs_high (bfd *abfd, | |
9652 | char *buf, | |
9653 | int *bufsiz, | |
9654 | const void *s390_vxrs_high, | |
9655 | int size) | |
9656 | { | |
9657 | char *note_name = "LINUX"; | |
9658 | return elfcore_write_note (abfd, buf, bufsiz, | |
9659 | note_name, NT_S390_VXRS_HIGH, | |
9660 | s390_vxrs_high, size); | |
9661 | } | |
9662 | ||
faa9a424 UW |
9663 | char * |
9664 | elfcore_write_arm_vfp (bfd *abfd, | |
9665 | char *buf, | |
9666 | int *bufsiz, | |
9667 | const void *arm_vfp, | |
9668 | int size) | |
9669 | { | |
9670 | char *note_name = "LINUX"; | |
9671 | return elfcore_write_note (abfd, buf, bufsiz, | |
9672 | note_name, NT_ARM_VFP, arm_vfp, size); | |
9673 | } | |
9674 | ||
652451f8 YZ |
9675 | char * |
9676 | elfcore_write_aarch_tls (bfd *abfd, | |
9677 | char *buf, | |
9678 | int *bufsiz, | |
9679 | const void *aarch_tls, | |
9680 | int size) | |
9681 | { | |
9682 | char *note_name = "LINUX"; | |
9683 | return elfcore_write_note (abfd, buf, bufsiz, | |
9684 | note_name, NT_ARM_TLS, aarch_tls, size); | |
9685 | } | |
9686 | ||
9687 | char * | |
9688 | elfcore_write_aarch_hw_break (bfd *abfd, | |
9689 | char *buf, | |
9690 | int *bufsiz, | |
9691 | const void *aarch_hw_break, | |
9692 | int size) | |
9693 | { | |
9694 | char *note_name = "LINUX"; | |
9695 | return elfcore_write_note (abfd, buf, bufsiz, | |
9696 | note_name, NT_ARM_HW_BREAK, aarch_hw_break, size); | |
9697 | } | |
9698 | ||
9699 | char * | |
9700 | elfcore_write_aarch_hw_watch (bfd *abfd, | |
9701 | char *buf, | |
9702 | int *bufsiz, | |
9703 | const void *aarch_hw_watch, | |
9704 | int size) | |
9705 | { | |
9706 | char *note_name = "LINUX"; | |
9707 | return elfcore_write_note (abfd, buf, bufsiz, | |
9708 | note_name, NT_ARM_HW_WATCH, aarch_hw_watch, size); | |
9709 | } | |
9710 | ||
bb864ac1 CES |
9711 | char * |
9712 | elfcore_write_register_note (bfd *abfd, | |
9713 | char *buf, | |
9714 | int *bufsiz, | |
9715 | const char *section, | |
9716 | const void *data, | |
9717 | int size) | |
9718 | { | |
9719 | if (strcmp (section, ".reg2") == 0) | |
9720 | return elfcore_write_prfpreg (abfd, buf, bufsiz, data, size); | |
9721 | if (strcmp (section, ".reg-xfp") == 0) | |
9722 | return elfcore_write_prxfpreg (abfd, buf, bufsiz, data, size); | |
4339cae0 L |
9723 | if (strcmp (section, ".reg-xstate") == 0) |
9724 | return elfcore_write_xstatereg (abfd, buf, bufsiz, data, size); | |
bb864ac1 CES |
9725 | if (strcmp (section, ".reg-ppc-vmx") == 0) |
9726 | return elfcore_write_ppc_vmx (abfd, buf, bufsiz, data, size); | |
89eeb0bc LM |
9727 | if (strcmp (section, ".reg-ppc-vsx") == 0) |
9728 | return elfcore_write_ppc_vsx (abfd, buf, bufsiz, data, size); | |
0675e188 UW |
9729 | if (strcmp (section, ".reg-s390-high-gprs") == 0) |
9730 | return elfcore_write_s390_high_gprs (abfd, buf, bufsiz, data, size); | |
d7eeb400 MS |
9731 | if (strcmp (section, ".reg-s390-timer") == 0) |
9732 | return elfcore_write_s390_timer (abfd, buf, bufsiz, data, size); | |
9733 | if (strcmp (section, ".reg-s390-todcmp") == 0) | |
9734 | return elfcore_write_s390_todcmp (abfd, buf, bufsiz, data, size); | |
9735 | if (strcmp (section, ".reg-s390-todpreg") == 0) | |
9736 | return elfcore_write_s390_todpreg (abfd, buf, bufsiz, data, size); | |
9737 | if (strcmp (section, ".reg-s390-ctrs") == 0) | |
9738 | return elfcore_write_s390_ctrs (abfd, buf, bufsiz, data, size); | |
9739 | if (strcmp (section, ".reg-s390-prefix") == 0) | |
9740 | return elfcore_write_s390_prefix (abfd, buf, bufsiz, data, size); | |
355b81d9 UW |
9741 | if (strcmp (section, ".reg-s390-last-break") == 0) |
9742 | return elfcore_write_s390_last_break (abfd, buf, bufsiz, data, size); | |
9743 | if (strcmp (section, ".reg-s390-system-call") == 0) | |
9744 | return elfcore_write_s390_system_call (abfd, buf, bufsiz, data, size); | |
abb3f6cc NC |
9745 | if (strcmp (section, ".reg-s390-tdb") == 0) |
9746 | return elfcore_write_s390_tdb (abfd, buf, bufsiz, data, size); | |
4ef9f41a AA |
9747 | if (strcmp (section, ".reg-s390-vxrs-low") == 0) |
9748 | return elfcore_write_s390_vxrs_low (abfd, buf, bufsiz, data, size); | |
9749 | if (strcmp (section, ".reg-s390-vxrs-high") == 0) | |
9750 | return elfcore_write_s390_vxrs_high (abfd, buf, bufsiz, data, size); | |
faa9a424 UW |
9751 | if (strcmp (section, ".reg-arm-vfp") == 0) |
9752 | return elfcore_write_arm_vfp (abfd, buf, bufsiz, data, size); | |
652451f8 YZ |
9753 | if (strcmp (section, ".reg-aarch-tls") == 0) |
9754 | return elfcore_write_aarch_tls (abfd, buf, bufsiz, data, size); | |
9755 | if (strcmp (section, ".reg-aarch-hw-break") == 0) | |
9756 | return elfcore_write_aarch_hw_break (abfd, buf, bufsiz, data, size); | |
9757 | if (strcmp (section, ".reg-aarch-hw-watch") == 0) | |
9758 | return elfcore_write_aarch_hw_watch (abfd, buf, bufsiz, data, size); | |
bb864ac1 CES |
9759 | return NULL; |
9760 | } | |
9761 | ||
b34976b6 | 9762 | static bfd_boolean |
718175fa | 9763 | elf_parse_notes (bfd *abfd, char *buf, size_t size, file_ptr offset) |
252b5132 | 9764 | { |
c044fabd | 9765 | char *p; |
252b5132 | 9766 | |
252b5132 RH |
9767 | p = buf; |
9768 | while (p < buf + size) | |
9769 | { | |
c044fabd KH |
9770 | /* FIXME: bad alignment assumption. */ |
9771 | Elf_External_Note *xnp = (Elf_External_Note *) p; | |
252b5132 RH |
9772 | Elf_Internal_Note in; |
9773 | ||
baea7ef1 AM |
9774 | if (offsetof (Elf_External_Note, name) > buf - p + size) |
9775 | return FALSE; | |
9776 | ||
dc810e39 | 9777 | in.type = H_GET_32 (abfd, xnp->type); |
252b5132 | 9778 | |
dc810e39 | 9779 | in.namesz = H_GET_32 (abfd, xnp->namesz); |
252b5132 | 9780 | in.namedata = xnp->name; |
baea7ef1 AM |
9781 | if (in.namesz > buf - in.namedata + size) |
9782 | return FALSE; | |
252b5132 | 9783 | |
dc810e39 | 9784 | in.descsz = H_GET_32 (abfd, xnp->descsz); |
252b5132 RH |
9785 | in.descdata = in.namedata + BFD_ALIGN (in.namesz, 4); |
9786 | in.descpos = offset + (in.descdata - buf); | |
baea7ef1 AM |
9787 | if (in.descsz != 0 |
9788 | && (in.descdata >= buf + size | |
9789 | || in.descsz > buf - in.descdata + size)) | |
9790 | return FALSE; | |
252b5132 | 9791 | |
718175fa JK |
9792 | switch (bfd_get_format (abfd)) |
9793 | { | |
9794 | default: | |
9795 | return TRUE; | |
9796 | ||
9797 | case bfd_core: | |
f64e188b | 9798 | { |
8acbedd6 | 9799 | #define GROKER_ELEMENT(S,F) {S, sizeof (S) - 1, F} |
f64e188b | 9800 | struct |
718175fa | 9801 | { |
f64e188b | 9802 | const char * string; |
8acbedd6 | 9803 | size_t len; |
f64e188b | 9804 | bfd_boolean (* func)(bfd *, Elf_Internal_Note *); |
718175fa | 9805 | } |
f64e188b | 9806 | grokers[] = |
b15fa79e | 9807 | { |
8acbedd6 KS |
9808 | GROKER_ELEMENT ("", elfcore_grok_note), |
9809 | GROKER_ELEMENT ("NetBSD-CORE", elfcore_grok_netbsd_note), | |
9810 | GROKER_ELEMENT ( "OpenBSD", elfcore_grok_openbsd_note), | |
9811 | GROKER_ELEMENT ("QNX", elfcore_grok_nto_note), | |
9812 | GROKER_ELEMENT ("SPU/", elfcore_grok_spu_note) | |
f64e188b | 9813 | }; |
8acbedd6 | 9814 | #undef GROKER_ELEMENT |
f64e188b NC |
9815 | int i; |
9816 | ||
9817 | for (i = ARRAY_SIZE (grokers); i--;) | |
8acbedd6 KS |
9818 | { |
9819 | if (in.namesz >= grokers[i].len | |
9820 | && strncmp (in.namedata, grokers[i].string, | |
9821 | grokers[i].len) == 0) | |
9822 | { | |
9823 | if (! grokers[i].func (abfd, & in)) | |
9824 | return FALSE; | |
9825 | break; | |
9826 | } | |
9827 | } | |
f64e188b NC |
9828 | break; |
9829 | } | |
718175fa JK |
9830 | |
9831 | case bfd_object: | |
9832 | if (in.namesz == sizeof "GNU" && strcmp (in.namedata, "GNU") == 0) | |
9833 | { | |
9834 | if (! elfobj_grok_gnu_note (abfd, &in)) | |
9835 | return FALSE; | |
9836 | } | |
e21e5835 NC |
9837 | else if (in.namesz == sizeof "stapsdt" |
9838 | && strcmp (in.namedata, "stapsdt") == 0) | |
9839 | { | |
9840 | if (! elfobj_grok_stapsdt_note (abfd, &in)) | |
9841 | return FALSE; | |
9842 | } | |
718175fa | 9843 | break; |
08a40648 | 9844 | } |
252b5132 RH |
9845 | |
9846 | p = in.descdata + BFD_ALIGN (in.descsz, 4); | |
9847 | } | |
9848 | ||
718175fa JK |
9849 | return TRUE; |
9850 | } | |
9851 | ||
9852 | static bfd_boolean | |
9853 | elf_read_notes (bfd *abfd, file_ptr offset, bfd_size_type size) | |
9854 | { | |
9855 | char *buf; | |
9856 | ||
9857 | if (size <= 0) | |
9858 | return TRUE; | |
9859 | ||
9860 | if (bfd_seek (abfd, offset, SEEK_SET) != 0) | |
9861 | return FALSE; | |
9862 | ||
f64e188b | 9863 | buf = (char *) bfd_malloc (size + 1); |
718175fa JK |
9864 | if (buf == NULL) |
9865 | return FALSE; | |
9866 | ||
f64e188b NC |
9867 | /* PR 17512: file: ec08f814 |
9868 | 0-termintate the buffer so that string searches will not overflow. */ | |
9869 | buf[size] = 0; | |
9870 | ||
718175fa JK |
9871 | if (bfd_bread (buf, size, abfd) != size |
9872 | || !elf_parse_notes (abfd, buf, size, offset)) | |
9873 | { | |
9874 | free (buf); | |
9875 | return FALSE; | |
9876 | } | |
9877 | ||
252b5132 | 9878 | free (buf); |
b34976b6 | 9879 | return TRUE; |
252b5132 | 9880 | } |
98d8431c JB |
9881 | \f |
9882 | /* Providing external access to the ELF program header table. */ | |
9883 | ||
9884 | /* Return an upper bound on the number of bytes required to store a | |
9885 | copy of ABFD's program header table entries. Return -1 if an error | |
9886 | occurs; bfd_get_error will return an appropriate code. */ | |
c044fabd | 9887 | |
98d8431c | 9888 | long |
217aa764 | 9889 | bfd_get_elf_phdr_upper_bound (bfd *abfd) |
98d8431c JB |
9890 | { |
9891 | if (abfd->xvec->flavour != bfd_target_elf_flavour) | |
9892 | { | |
9893 | bfd_set_error (bfd_error_wrong_format); | |
9894 | return -1; | |
9895 | } | |
9896 | ||
936e320b | 9897 | return elf_elfheader (abfd)->e_phnum * sizeof (Elf_Internal_Phdr); |
98d8431c JB |
9898 | } |
9899 | ||
98d8431c JB |
9900 | /* Copy ABFD's program header table entries to *PHDRS. The entries |
9901 | will be stored as an array of Elf_Internal_Phdr structures, as | |
9902 | defined in include/elf/internal.h. To find out how large the | |
9903 | buffer needs to be, call bfd_get_elf_phdr_upper_bound. | |
9904 | ||
9905 | Return the number of program header table entries read, or -1 if an | |
9906 | error occurs; bfd_get_error will return an appropriate code. */ | |
c044fabd | 9907 | |
98d8431c | 9908 | int |
217aa764 | 9909 | bfd_get_elf_phdrs (bfd *abfd, void *phdrs) |
98d8431c JB |
9910 | { |
9911 | int num_phdrs; | |
9912 | ||
9913 | if (abfd->xvec->flavour != bfd_target_elf_flavour) | |
9914 | { | |
9915 | bfd_set_error (bfd_error_wrong_format); | |
9916 | return -1; | |
9917 | } | |
9918 | ||
9919 | num_phdrs = elf_elfheader (abfd)->e_phnum; | |
c044fabd | 9920 | memcpy (phdrs, elf_tdata (abfd)->phdr, |
98d8431c JB |
9921 | num_phdrs * sizeof (Elf_Internal_Phdr)); |
9922 | ||
9923 | return num_phdrs; | |
9924 | } | |
ae4221d7 | 9925 | |
db6751f2 | 9926 | enum elf_reloc_type_class |
7e612e98 AM |
9927 | _bfd_elf_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED, |
9928 | const asection *rel_sec ATTRIBUTE_UNUSED, | |
9929 | const Elf_Internal_Rela *rela ATTRIBUTE_UNUSED) | |
db6751f2 JJ |
9930 | { |
9931 | return reloc_class_normal; | |
9932 | } | |
f8df10f4 | 9933 | |
47d9a591 | 9934 | /* For RELA architectures, return the relocation value for a |
f8df10f4 JJ |
9935 | relocation against a local symbol. */ |
9936 | ||
9937 | bfd_vma | |
217aa764 AM |
9938 | _bfd_elf_rela_local_sym (bfd *abfd, |
9939 | Elf_Internal_Sym *sym, | |
8517fae7 | 9940 | asection **psec, |
217aa764 | 9941 | Elf_Internal_Rela *rel) |
f8df10f4 | 9942 | { |
8517fae7 | 9943 | asection *sec = *psec; |
f8df10f4 JJ |
9944 | bfd_vma relocation; |
9945 | ||
9946 | relocation = (sec->output_section->vma | |
9947 | + sec->output_offset | |
9948 | + sym->st_value); | |
9949 | if ((sec->flags & SEC_MERGE) | |
c629eae0 | 9950 | && ELF_ST_TYPE (sym->st_info) == STT_SECTION |
dbaa2011 | 9951 | && sec->sec_info_type == SEC_INFO_TYPE_MERGE) |
f8df10f4 | 9952 | { |
f8df10f4 | 9953 | rel->r_addend = |
8517fae7 | 9954 | _bfd_merged_section_offset (abfd, psec, |
65765700 | 9955 | elf_section_data (sec)->sec_info, |
753731ee AM |
9956 | sym->st_value + rel->r_addend); |
9957 | if (sec != *psec) | |
9958 | { | |
9959 | /* If we have changed the section, and our original section is | |
9960 | marked with SEC_EXCLUDE, it means that the original | |
9961 | SEC_MERGE section has been completely subsumed in some | |
9962 | other SEC_MERGE section. In this case, we need to leave | |
9963 | some info around for --emit-relocs. */ | |
9964 | if ((sec->flags & SEC_EXCLUDE) != 0) | |
9965 | sec->kept_section = *psec; | |
9966 | sec = *psec; | |
9967 | } | |
8517fae7 AM |
9968 | rel->r_addend -= relocation; |
9969 | rel->r_addend += sec->output_section->vma + sec->output_offset; | |
f8df10f4 JJ |
9970 | } |
9971 | return relocation; | |
9972 | } | |
c629eae0 JJ |
9973 | |
9974 | bfd_vma | |
217aa764 AM |
9975 | _bfd_elf_rel_local_sym (bfd *abfd, |
9976 | Elf_Internal_Sym *sym, | |
9977 | asection **psec, | |
9978 | bfd_vma addend) | |
47d9a591 | 9979 | { |
c629eae0 JJ |
9980 | asection *sec = *psec; |
9981 | ||
dbaa2011 | 9982 | if (sec->sec_info_type != SEC_INFO_TYPE_MERGE) |
c629eae0 JJ |
9983 | return sym->st_value + addend; |
9984 | ||
9985 | return _bfd_merged_section_offset (abfd, psec, | |
65765700 | 9986 | elf_section_data (sec)->sec_info, |
753731ee | 9987 | sym->st_value + addend); |
c629eae0 JJ |
9988 | } |
9989 | ||
9990 | bfd_vma | |
217aa764 | 9991 | _bfd_elf_section_offset (bfd *abfd, |
92e4ec35 | 9992 | struct bfd_link_info *info, |
217aa764 AM |
9993 | asection *sec, |
9994 | bfd_vma offset) | |
c629eae0 | 9995 | { |
68bfbfcc | 9996 | switch (sec->sec_info_type) |
65765700 | 9997 | { |
dbaa2011 | 9998 | case SEC_INFO_TYPE_STABS: |
eea6121a AM |
9999 | return _bfd_stab_section_offset (sec, elf_section_data (sec)->sec_info, |
10000 | offset); | |
dbaa2011 | 10001 | case SEC_INFO_TYPE_EH_FRAME: |
92e4ec35 | 10002 | return _bfd_elf_eh_frame_section_offset (abfd, info, sec, offset); |
65765700 | 10003 | default: |
310fd250 L |
10004 | if ((sec->flags & SEC_ELF_REVERSE_COPY) != 0) |
10005 | { | |
10006 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
10007 | bfd_size_type address_size = bed->s->arch_size / 8; | |
10008 | offset = sec->size - offset - address_size; | |
10009 | } | |
65765700 JJ |
10010 | return offset; |
10011 | } | |
c629eae0 | 10012 | } |
3333a7c3 RM |
10013 | \f |
10014 | /* Create a new BFD as if by bfd_openr. Rather than opening a file, | |
10015 | reconstruct an ELF file by reading the segments out of remote memory | |
10016 | based on the ELF file header at EHDR_VMA and the ELF program headers it | |
10017 | points to. If not null, *LOADBASEP is filled in with the difference | |
10018 | between the VMAs from which the segments were read, and the VMAs the | |
10019 | file headers (and hence BFD's idea of each section's VMA) put them at. | |
10020 | ||
10021 | The function TARGET_READ_MEMORY is called to copy LEN bytes from the | |
10022 | remote memory at target address VMA into the local buffer at MYADDR; it | |
10023 | should return zero on success or an `errno' code on failure. TEMPL must | |
10024 | be a BFD for an ELF target with the word size and byte order found in | |
10025 | the remote memory. */ | |
10026 | ||
10027 | bfd * | |
217aa764 AM |
10028 | bfd_elf_bfd_from_remote_memory |
10029 | (bfd *templ, | |
10030 | bfd_vma ehdr_vma, | |
f0a5d95a | 10031 | bfd_size_type size, |
217aa764 | 10032 | bfd_vma *loadbasep, |
fe78531d | 10033 | int (*target_read_memory) (bfd_vma, bfd_byte *, bfd_size_type)) |
3333a7c3 RM |
10034 | { |
10035 | return (*get_elf_backend_data (templ)->elf_backend_bfd_from_remote_memory) | |
5979d6b6 | 10036 | (templ, ehdr_vma, size, loadbasep, target_read_memory); |
3333a7c3 | 10037 | } |
4c45e5c9 JJ |
10038 | \f |
10039 | long | |
c9727e01 AM |
10040 | _bfd_elf_get_synthetic_symtab (bfd *abfd, |
10041 | long symcount ATTRIBUTE_UNUSED, | |
10042 | asymbol **syms ATTRIBUTE_UNUSED, | |
8615f3f2 | 10043 | long dynsymcount, |
c9727e01 AM |
10044 | asymbol **dynsyms, |
10045 | asymbol **ret) | |
4c45e5c9 JJ |
10046 | { |
10047 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
10048 | asection *relplt; | |
10049 | asymbol *s; | |
10050 | const char *relplt_name; | |
10051 | bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean); | |
10052 | arelent *p; | |
10053 | long count, i, n; | |
10054 | size_t size; | |
10055 | Elf_Internal_Shdr *hdr; | |
10056 | char *names; | |
10057 | asection *plt; | |
10058 | ||
8615f3f2 AM |
10059 | *ret = NULL; |
10060 | ||
90e3cdf2 JJ |
10061 | if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0) |
10062 | return 0; | |
10063 | ||
8615f3f2 AM |
10064 | if (dynsymcount <= 0) |
10065 | return 0; | |
10066 | ||
4c45e5c9 JJ |
10067 | if (!bed->plt_sym_val) |
10068 | return 0; | |
10069 | ||
10070 | relplt_name = bed->relplt_name; | |
10071 | if (relplt_name == NULL) | |
d35fd659 | 10072 | relplt_name = bed->rela_plts_and_copies_p ? ".rela.plt" : ".rel.plt"; |
4c45e5c9 JJ |
10073 | relplt = bfd_get_section_by_name (abfd, relplt_name); |
10074 | if (relplt == NULL) | |
10075 | return 0; | |
10076 | ||
10077 | hdr = &elf_section_data (relplt)->this_hdr; | |
10078 | if (hdr->sh_link != elf_dynsymtab (abfd) | |
10079 | || (hdr->sh_type != SHT_REL && hdr->sh_type != SHT_RELA)) | |
10080 | return 0; | |
10081 | ||
10082 | plt = bfd_get_section_by_name (abfd, ".plt"); | |
10083 | if (plt == NULL) | |
10084 | return 0; | |
10085 | ||
10086 | slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table; | |
c9727e01 | 10087 | if (! (*slurp_relocs) (abfd, relplt, dynsyms, TRUE)) |
4c45e5c9 JJ |
10088 | return -1; |
10089 | ||
eea6121a | 10090 | count = relplt->size / hdr->sh_entsize; |
4c45e5c9 JJ |
10091 | size = count * sizeof (asymbol); |
10092 | p = relplt->relocation; | |
cb53bf42 | 10093 | for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel) |
041de40d AM |
10094 | { |
10095 | size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt"); | |
10096 | if (p->addend != 0) | |
10097 | { | |
10098 | #ifdef BFD64 | |
10099 | size += sizeof ("+0x") - 1 + 8 + 8 * (bed->s->elfclass == ELFCLASS64); | |
10100 | #else | |
10101 | size += sizeof ("+0x") - 1 + 8; | |
10102 | #endif | |
10103 | } | |
10104 | } | |
4c45e5c9 | 10105 | |
a50b1753 | 10106 | s = *ret = (asymbol *) bfd_malloc (size); |
4c45e5c9 JJ |
10107 | if (s == NULL) |
10108 | return -1; | |
10109 | ||
10110 | names = (char *) (s + count); | |
10111 | p = relplt->relocation; | |
10112 | n = 0; | |
cb53bf42 | 10113 | for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel) |
4c45e5c9 JJ |
10114 | { |
10115 | size_t len; | |
10116 | bfd_vma addr; | |
10117 | ||
10118 | addr = bed->plt_sym_val (i, plt, p); | |
10119 | if (addr == (bfd_vma) -1) | |
10120 | continue; | |
10121 | ||
10122 | *s = **p->sym_ptr_ptr; | |
65a7a66f AM |
10123 | /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since |
10124 | we are defining a symbol, ensure one of them is set. */ | |
10125 | if ((s->flags & BSF_LOCAL) == 0) | |
10126 | s->flags |= BSF_GLOBAL; | |
6ba2a415 | 10127 | s->flags |= BSF_SYNTHETIC; |
4c45e5c9 JJ |
10128 | s->section = plt; |
10129 | s->value = addr - plt->vma; | |
10130 | s->name = names; | |
8f39ba8e | 10131 | s->udata.p = NULL; |
4c45e5c9 JJ |
10132 | len = strlen ((*p->sym_ptr_ptr)->name); |
10133 | memcpy (names, (*p->sym_ptr_ptr)->name, len); | |
10134 | names += len; | |
041de40d AM |
10135 | if (p->addend != 0) |
10136 | { | |
1d770845 | 10137 | char buf[30], *a; |
d324f6d6 | 10138 | |
041de40d AM |
10139 | memcpy (names, "+0x", sizeof ("+0x") - 1); |
10140 | names += sizeof ("+0x") - 1; | |
1d770845 L |
10141 | bfd_sprintf_vma (abfd, buf, p->addend); |
10142 | for (a = buf; *a == '0'; ++a) | |
10143 | ; | |
10144 | len = strlen (a); | |
10145 | memcpy (names, a, len); | |
10146 | names += len; | |
041de40d | 10147 | } |
4c45e5c9 JJ |
10148 | memcpy (names, "@plt", sizeof ("@plt")); |
10149 | names += sizeof ("@plt"); | |
8f39ba8e | 10150 | ++s, ++n; |
4c45e5c9 JJ |
10151 | } |
10152 | ||
10153 | return n; | |
10154 | } | |
3d7f7666 | 10155 | |
3b22753a L |
10156 | /* It is only used by x86-64 so far. */ |
10157 | asection _bfd_elf_large_com_section | |
10158 | = BFD_FAKE_SECTION (_bfd_elf_large_com_section, | |
f592407e | 10159 | SEC_IS_COMMON, NULL, "LARGE_COMMON", 0); |
ecca9871 | 10160 | |
d1036acb | 10161 | void |
78245035 L |
10162 | _bfd_elf_post_process_headers (bfd * abfd, |
10163 | struct bfd_link_info * link_info ATTRIBUTE_UNUSED) | |
d1036acb L |
10164 | { |
10165 | Elf_Internal_Ehdr * i_ehdrp; /* ELF file header, internal form. */ | |
10166 | ||
10167 | i_ehdrp = elf_elfheader (abfd); | |
10168 | ||
10169 | i_ehdrp->e_ident[EI_OSABI] = get_elf_backend_data (abfd)->elf_osabi; | |
d8045f23 NC |
10170 | |
10171 | /* To make things simpler for the loader on Linux systems we set the | |
9c55345c | 10172 | osabi field to ELFOSABI_GNU if the binary contains symbols of |
f64b2e8d | 10173 | the STT_GNU_IFUNC type or STB_GNU_UNIQUE binding. */ |
d8045f23 | 10174 | if (i_ehdrp->e_ident[EI_OSABI] == ELFOSABI_NONE |
f64b2e8d | 10175 | && elf_tdata (abfd)->has_gnu_symbols) |
9c55345c | 10176 | i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_GNU; |
d1036acb | 10177 | } |
fcb93ecf PB |
10178 | |
10179 | ||
10180 | /* Return TRUE for ELF symbol types that represent functions. | |
10181 | This is the default version of this function, which is sufficient for | |
d8045f23 | 10182 | most targets. It returns true if TYPE is STT_FUNC or STT_GNU_IFUNC. */ |
fcb93ecf PB |
10183 | |
10184 | bfd_boolean | |
10185 | _bfd_elf_is_function_type (unsigned int type) | |
10186 | { | |
d8045f23 NC |
10187 | return (type == STT_FUNC |
10188 | || type == STT_GNU_IFUNC); | |
fcb93ecf | 10189 | } |
9f296da3 | 10190 | |
aef36ac1 AM |
10191 | /* If the ELF symbol SYM might be a function in SEC, return the |
10192 | function size and set *CODE_OFF to the function's entry point, | |
10193 | otherwise return zero. */ | |
9f296da3 | 10194 | |
aef36ac1 AM |
10195 | bfd_size_type |
10196 | _bfd_elf_maybe_function_sym (const asymbol *sym, asection *sec, | |
10197 | bfd_vma *code_off) | |
9f296da3 | 10198 | { |
aef36ac1 AM |
10199 | bfd_size_type size; |
10200 | ||
ff9e0f5b | 10201 | if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT |
aef36ac1 AM |
10202 | | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0 |
10203 | || sym->section != sec) | |
10204 | return 0; | |
ff9e0f5b | 10205 | |
ff9e0f5b | 10206 | *code_off = sym->value; |
aef36ac1 AM |
10207 | size = 0; |
10208 | if (!(sym->flags & BSF_SYNTHETIC)) | |
10209 | size = ((elf_symbol_type *) sym)->internal_elf_sym.st_size; | |
10210 | if (size == 0) | |
10211 | size = 1; | |
10212 | return size; | |
9f296da3 | 10213 | } |