Commit | Line | Data |
---|---|---|
252b5132 | 1 | /* ELF executable support for BFD. |
340b6d91 | 2 | |
4b95cf5c | 3 | Copyright (C) 1993-2014 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 | } | |
4e011fb5 AM |
1072 | if (name[1] != 'z') |
1073 | { | |
1074 | unsigned int len = strlen (name); | |
1075 | ||
1076 | new_name = bfd_alloc (abfd, len + 2); | |
1077 | if (new_name == NULL) | |
1078 | return FALSE; | |
1079 | new_name[0] = '.'; | |
1080 | new_name[1] = 'z'; | |
1081 | memcpy (new_name + 2, name + 1, len); | |
1082 | } | |
4a114e3e L |
1083 | break; |
1084 | case decompress: | |
1085 | if (!bfd_init_section_decompress_status (abfd, newsect)) | |
1086 | { | |
1087 | (*_bfd_error_handler) | |
bc823199 | 1088 | (_("%B: unable to initialize decompress status for section %s"), |
4a114e3e L |
1089 | abfd, name); |
1090 | return FALSE; | |
1091 | } | |
4e011fb5 AM |
1092 | if (name[1] == 'z') |
1093 | { | |
1094 | unsigned int len = strlen (name); | |
1095 | ||
1096 | new_name = bfd_alloc (abfd, len); | |
1097 | if (new_name == NULL) | |
1098 | return FALSE; | |
1099 | new_name[0] = '.'; | |
1100 | memcpy (new_name + 1, name + 2, len - 1); | |
1101 | } | |
4a114e3e L |
1102 | break; |
1103 | } | |
4e011fb5 AM |
1104 | if (new_name != NULL) |
1105 | bfd_rename_section (abfd, newsect, new_name); | |
4a114e3e L |
1106 | } |
1107 | ||
b34976b6 | 1108 | return TRUE; |
252b5132 RH |
1109 | } |
1110 | ||
252b5132 RH |
1111 | const char *const bfd_elf_section_type_names[] = { |
1112 | "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB", | |
1113 | "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE", | |
1114 | "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM", | |
1115 | }; | |
1116 | ||
1049f94e | 1117 | /* ELF relocs are against symbols. If we are producing relocatable |
252b5132 RH |
1118 | output, and the reloc is against an external symbol, and nothing |
1119 | has given us any additional addend, the resulting reloc will also | |
1120 | be against the same symbol. In such a case, we don't want to | |
1121 | change anything about the way the reloc is handled, since it will | |
1122 | all be done at final link time. Rather than put special case code | |
1123 | into bfd_perform_relocation, all the reloc types use this howto | |
1124 | function. It just short circuits the reloc if producing | |
1049f94e | 1125 | relocatable output against an external symbol. */ |
252b5132 | 1126 | |
252b5132 | 1127 | bfd_reloc_status_type |
217aa764 AM |
1128 | bfd_elf_generic_reloc (bfd *abfd ATTRIBUTE_UNUSED, |
1129 | arelent *reloc_entry, | |
1130 | asymbol *symbol, | |
1131 | void *data ATTRIBUTE_UNUSED, | |
1132 | asection *input_section, | |
1133 | bfd *output_bfd, | |
1134 | char **error_message ATTRIBUTE_UNUSED) | |
1135 | { | |
1136 | if (output_bfd != NULL | |
252b5132 RH |
1137 | && (symbol->flags & BSF_SECTION_SYM) == 0 |
1138 | && (! reloc_entry->howto->partial_inplace | |
1139 | || reloc_entry->addend == 0)) | |
1140 | { | |
1141 | reloc_entry->address += input_section->output_offset; | |
1142 | return bfd_reloc_ok; | |
1143 | } | |
1144 | ||
1145 | return bfd_reloc_continue; | |
1146 | } | |
1147 | \f | |
0ac4564e L |
1148 | /* Copy the program header and other data from one object module to |
1149 | another. */ | |
252b5132 | 1150 | |
b34976b6 | 1151 | bfd_boolean |
217aa764 | 1152 | _bfd_elf_copy_private_bfd_data (bfd *ibfd, bfd *obfd) |
2d502050 L |
1153 | { |
1154 | if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour | |
1155 | || bfd_get_flavour (obfd) != bfd_target_elf_flavour) | |
b34976b6 | 1156 | return TRUE; |
2d502050 | 1157 | |
57b828ef L |
1158 | if (!elf_flags_init (obfd)) |
1159 | { | |
1160 | elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags; | |
1161 | elf_flags_init (obfd) = TRUE; | |
1162 | } | |
2d502050 | 1163 | |
0ac4564e | 1164 | elf_gp (obfd) = elf_gp (ibfd); |
57b828ef L |
1165 | |
1166 | /* Also copy the EI_OSABI field. */ | |
1167 | elf_elfheader (obfd)->e_ident[EI_OSABI] = | |
1168 | elf_elfheader (ibfd)->e_ident[EI_OSABI]; | |
104d59d1 JM |
1169 | |
1170 | /* Copy object attributes. */ | |
1171 | _bfd_elf_copy_obj_attributes (ibfd, obfd); | |
b34976b6 | 1172 | return TRUE; |
2d502050 L |
1173 | } |
1174 | ||
cedc298e L |
1175 | static const char * |
1176 | get_segment_type (unsigned int p_type) | |
1177 | { | |
1178 | const char *pt; | |
1179 | switch (p_type) | |
1180 | { | |
1181 | case PT_NULL: pt = "NULL"; break; | |
1182 | case PT_LOAD: pt = "LOAD"; break; | |
1183 | case PT_DYNAMIC: pt = "DYNAMIC"; break; | |
1184 | case PT_INTERP: pt = "INTERP"; break; | |
1185 | case PT_NOTE: pt = "NOTE"; break; | |
1186 | case PT_SHLIB: pt = "SHLIB"; break; | |
1187 | case PT_PHDR: pt = "PHDR"; break; | |
1188 | case PT_TLS: pt = "TLS"; break; | |
1189 | case PT_GNU_EH_FRAME: pt = "EH_FRAME"; break; | |
2b05f1b7 | 1190 | case PT_GNU_STACK: pt = "STACK"; break; |
cedc298e L |
1191 | case PT_GNU_RELRO: pt = "RELRO"; break; |
1192 | default: pt = NULL; break; | |
1193 | } | |
1194 | return pt; | |
1195 | } | |
1196 | ||
f0b79d91 L |
1197 | /* Print out the program headers. */ |
1198 | ||
b34976b6 | 1199 | bfd_boolean |
217aa764 | 1200 | _bfd_elf_print_private_bfd_data (bfd *abfd, void *farg) |
252b5132 | 1201 | { |
a50b1753 | 1202 | FILE *f = (FILE *) farg; |
252b5132 RH |
1203 | Elf_Internal_Phdr *p; |
1204 | asection *s; | |
1205 | bfd_byte *dynbuf = NULL; | |
1206 | ||
1207 | p = elf_tdata (abfd)->phdr; | |
1208 | if (p != NULL) | |
1209 | { | |
1210 | unsigned int i, c; | |
1211 | ||
1212 | fprintf (f, _("\nProgram Header:\n")); | |
1213 | c = elf_elfheader (abfd)->e_phnum; | |
1214 | for (i = 0; i < c; i++, p++) | |
1215 | { | |
cedc298e | 1216 | const char *pt = get_segment_type (p->p_type); |
252b5132 RH |
1217 | char buf[20]; |
1218 | ||
cedc298e | 1219 | if (pt == NULL) |
252b5132 | 1220 | { |
cedc298e L |
1221 | sprintf (buf, "0x%lx", p->p_type); |
1222 | pt = buf; | |
252b5132 | 1223 | } |
dc810e39 | 1224 | fprintf (f, "%8s off 0x", pt); |
60b89a18 | 1225 | bfd_fprintf_vma (abfd, f, p->p_offset); |
252b5132 | 1226 | fprintf (f, " vaddr 0x"); |
60b89a18 | 1227 | bfd_fprintf_vma (abfd, f, p->p_vaddr); |
252b5132 | 1228 | fprintf (f, " paddr 0x"); |
60b89a18 | 1229 | bfd_fprintf_vma (abfd, f, p->p_paddr); |
252b5132 RH |
1230 | fprintf (f, " align 2**%u\n", bfd_log2 (p->p_align)); |
1231 | fprintf (f, " filesz 0x"); | |
60b89a18 | 1232 | bfd_fprintf_vma (abfd, f, p->p_filesz); |
252b5132 | 1233 | fprintf (f, " memsz 0x"); |
60b89a18 | 1234 | bfd_fprintf_vma (abfd, f, p->p_memsz); |
252b5132 RH |
1235 | fprintf (f, " flags %c%c%c", |
1236 | (p->p_flags & PF_R) != 0 ? 'r' : '-', | |
1237 | (p->p_flags & PF_W) != 0 ? 'w' : '-', | |
1238 | (p->p_flags & PF_X) != 0 ? 'x' : '-'); | |
dc810e39 AM |
1239 | if ((p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X)) != 0) |
1240 | fprintf (f, " %lx", p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X)); | |
252b5132 RH |
1241 | fprintf (f, "\n"); |
1242 | } | |
1243 | } | |
1244 | ||
1245 | s = bfd_get_section_by_name (abfd, ".dynamic"); | |
1246 | if (s != NULL) | |
1247 | { | |
cb33740c | 1248 | unsigned int elfsec; |
dc810e39 | 1249 | unsigned long shlink; |
252b5132 RH |
1250 | bfd_byte *extdyn, *extdynend; |
1251 | size_t extdynsize; | |
217aa764 | 1252 | void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *); |
252b5132 RH |
1253 | |
1254 | fprintf (f, _("\nDynamic Section:\n")); | |
1255 | ||
eea6121a | 1256 | if (!bfd_malloc_and_get_section (abfd, s, &dynbuf)) |
252b5132 RH |
1257 | goto error_return; |
1258 | ||
1259 | elfsec = _bfd_elf_section_from_bfd_section (abfd, s); | |
cb33740c | 1260 | if (elfsec == SHN_BAD) |
252b5132 | 1261 | goto error_return; |
dc810e39 | 1262 | shlink = elf_elfsections (abfd)[elfsec]->sh_link; |
252b5132 RH |
1263 | |
1264 | extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn; | |
1265 | swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in; | |
1266 | ||
1267 | extdyn = dynbuf; | |
06614111 NC |
1268 | /* PR 17512: file: 6f427532. */ |
1269 | if (s->size < extdynsize) | |
1270 | goto error_return; | |
eea6121a | 1271 | extdynend = extdyn + s->size; |
1036838a NC |
1272 | /* PR 17512: file: id:000006,sig:06,src:000000,op:flip4,pos:5664. |
1273 | Fix range check. */ | |
1274 | for (; extdyn <= (extdynend - extdynsize); extdyn += extdynsize) | |
252b5132 RH |
1275 | { |
1276 | Elf_Internal_Dyn dyn; | |
ad9563d6 | 1277 | const char *name = ""; |
252b5132 | 1278 | char ab[20]; |
b34976b6 | 1279 | bfd_boolean stringp; |
ad9563d6 | 1280 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
252b5132 | 1281 | |
217aa764 | 1282 | (*swap_dyn_in) (abfd, extdyn, &dyn); |
252b5132 RH |
1283 | |
1284 | if (dyn.d_tag == DT_NULL) | |
1285 | break; | |
1286 | ||
b34976b6 | 1287 | stringp = FALSE; |
252b5132 RH |
1288 | switch (dyn.d_tag) |
1289 | { | |
1290 | default: | |
ad9563d6 CM |
1291 | if (bed->elf_backend_get_target_dtag) |
1292 | name = (*bed->elf_backend_get_target_dtag) (dyn.d_tag); | |
1293 | ||
1294 | if (!strcmp (name, "")) | |
1295 | { | |
1296 | sprintf (ab, "0x%lx", (unsigned long) dyn.d_tag); | |
1297 | name = ab; | |
1298 | } | |
252b5132 RH |
1299 | break; |
1300 | ||
b34976b6 | 1301 | case DT_NEEDED: name = "NEEDED"; stringp = TRUE; break; |
252b5132 RH |
1302 | case DT_PLTRELSZ: name = "PLTRELSZ"; break; |
1303 | case DT_PLTGOT: name = "PLTGOT"; break; | |
1304 | case DT_HASH: name = "HASH"; break; | |
1305 | case DT_STRTAB: name = "STRTAB"; break; | |
1306 | case DT_SYMTAB: name = "SYMTAB"; break; | |
1307 | case DT_RELA: name = "RELA"; break; | |
1308 | case DT_RELASZ: name = "RELASZ"; break; | |
1309 | case DT_RELAENT: name = "RELAENT"; break; | |
1310 | case DT_STRSZ: name = "STRSZ"; break; | |
1311 | case DT_SYMENT: name = "SYMENT"; break; | |
1312 | case DT_INIT: name = "INIT"; break; | |
1313 | case DT_FINI: name = "FINI"; break; | |
b34976b6 AM |
1314 | case DT_SONAME: name = "SONAME"; stringp = TRUE; break; |
1315 | case DT_RPATH: name = "RPATH"; stringp = TRUE; break; | |
252b5132 RH |
1316 | case DT_SYMBOLIC: name = "SYMBOLIC"; break; |
1317 | case DT_REL: name = "REL"; break; | |
1318 | case DT_RELSZ: name = "RELSZ"; break; | |
1319 | case DT_RELENT: name = "RELENT"; break; | |
1320 | case DT_PLTREL: name = "PLTREL"; break; | |
1321 | case DT_DEBUG: name = "DEBUG"; break; | |
1322 | case DT_TEXTREL: name = "TEXTREL"; break; | |
1323 | case DT_JMPREL: name = "JMPREL"; break; | |
94558834 L |
1324 | case DT_BIND_NOW: name = "BIND_NOW"; break; |
1325 | case DT_INIT_ARRAY: name = "INIT_ARRAY"; break; | |
1326 | case DT_FINI_ARRAY: name = "FINI_ARRAY"; break; | |
1327 | case DT_INIT_ARRAYSZ: name = "INIT_ARRAYSZ"; break; | |
1328 | case DT_FINI_ARRAYSZ: name = "FINI_ARRAYSZ"; break; | |
b34976b6 | 1329 | case DT_RUNPATH: name = "RUNPATH"; stringp = TRUE; break; |
94558834 L |
1330 | case DT_FLAGS: name = "FLAGS"; break; |
1331 | case DT_PREINIT_ARRAY: name = "PREINIT_ARRAY"; break; | |
1332 | case DT_PREINIT_ARRAYSZ: name = "PREINIT_ARRAYSZ"; break; | |
d48188b9 | 1333 | case DT_CHECKSUM: name = "CHECKSUM"; break; |
94558834 L |
1334 | case DT_PLTPADSZ: name = "PLTPADSZ"; break; |
1335 | case DT_MOVEENT: name = "MOVEENT"; break; | |
1336 | case DT_MOVESZ: name = "MOVESZ"; break; | |
1337 | case DT_FEATURE: name = "FEATURE"; break; | |
1338 | case DT_POSFLAG_1: name = "POSFLAG_1"; break; | |
1339 | case DT_SYMINSZ: name = "SYMINSZ"; break; | |
1340 | case DT_SYMINENT: name = "SYMINENT"; break; | |
b34976b6 AM |
1341 | case DT_CONFIG: name = "CONFIG"; stringp = TRUE; break; |
1342 | case DT_DEPAUDIT: name = "DEPAUDIT"; stringp = TRUE; break; | |
1343 | case DT_AUDIT: name = "AUDIT"; stringp = TRUE; break; | |
94558834 L |
1344 | case DT_PLTPAD: name = "PLTPAD"; break; |
1345 | case DT_MOVETAB: name = "MOVETAB"; break; | |
1346 | case DT_SYMINFO: name = "SYMINFO"; break; | |
1347 | case DT_RELACOUNT: name = "RELACOUNT"; break; | |
1348 | case DT_RELCOUNT: name = "RELCOUNT"; break; | |
1349 | case DT_FLAGS_1: name = "FLAGS_1"; break; | |
252b5132 RH |
1350 | case DT_VERSYM: name = "VERSYM"; break; |
1351 | case DT_VERDEF: name = "VERDEF"; break; | |
1352 | case DT_VERDEFNUM: name = "VERDEFNUM"; break; | |
1353 | case DT_VERNEED: name = "VERNEED"; break; | |
1354 | case DT_VERNEEDNUM: name = "VERNEEDNUM"; break; | |
b34976b6 | 1355 | case DT_AUXILIARY: name = "AUXILIARY"; stringp = TRUE; break; |
94558834 | 1356 | case DT_USED: name = "USED"; break; |
b34976b6 | 1357 | case DT_FILTER: name = "FILTER"; stringp = TRUE; break; |
fdc90cb4 | 1358 | case DT_GNU_HASH: name = "GNU_HASH"; break; |
252b5132 RH |
1359 | } |
1360 | ||
ad9563d6 | 1361 | fprintf (f, " %-20s ", name); |
252b5132 | 1362 | if (! stringp) |
a1f3c56e AN |
1363 | { |
1364 | fprintf (f, "0x"); | |
1365 | bfd_fprintf_vma (abfd, f, dyn.d_un.d_val); | |
1366 | } | |
252b5132 RH |
1367 | else |
1368 | { | |
1369 | const char *string; | |
dc810e39 | 1370 | unsigned int tagv = dyn.d_un.d_val; |
252b5132 | 1371 | |
dc810e39 | 1372 | string = bfd_elf_string_from_elf_section (abfd, shlink, tagv); |
252b5132 RH |
1373 | if (string == NULL) |
1374 | goto error_return; | |
1375 | fprintf (f, "%s", string); | |
1376 | } | |
1377 | fprintf (f, "\n"); | |
1378 | } | |
1379 | ||
1380 | free (dynbuf); | |
1381 | dynbuf = NULL; | |
1382 | } | |
1383 | ||
1384 | if ((elf_dynverdef (abfd) != 0 && elf_tdata (abfd)->verdef == NULL) | |
1385 | || (elf_dynverref (abfd) != 0 && elf_tdata (abfd)->verref == NULL)) | |
1386 | { | |
fc0e6df6 | 1387 | if (! _bfd_elf_slurp_version_tables (abfd, FALSE)) |
b34976b6 | 1388 | return FALSE; |
252b5132 RH |
1389 | } |
1390 | ||
1391 | if (elf_dynverdef (abfd) != 0) | |
1392 | { | |
1393 | Elf_Internal_Verdef *t; | |
1394 | ||
1395 | fprintf (f, _("\nVersion definitions:\n")); | |
1396 | for (t = elf_tdata (abfd)->verdef; t != NULL; t = t->vd_nextdef) | |
1397 | { | |
1398 | fprintf (f, "%d 0x%2.2x 0x%8.8lx %s\n", t->vd_ndx, | |
d0fb9a8d JJ |
1399 | t->vd_flags, t->vd_hash, |
1400 | t->vd_nodename ? t->vd_nodename : "<corrupt>"); | |
1401 | if (t->vd_auxptr != NULL && t->vd_auxptr->vda_nextptr != NULL) | |
252b5132 RH |
1402 | { |
1403 | Elf_Internal_Verdaux *a; | |
1404 | ||
1405 | fprintf (f, "\t"); | |
1406 | for (a = t->vd_auxptr->vda_nextptr; | |
1407 | a != NULL; | |
1408 | a = a->vda_nextptr) | |
d0fb9a8d JJ |
1409 | fprintf (f, "%s ", |
1410 | a->vda_nodename ? a->vda_nodename : "<corrupt>"); | |
252b5132 RH |
1411 | fprintf (f, "\n"); |
1412 | } | |
1413 | } | |
1414 | } | |
1415 | ||
1416 | if (elf_dynverref (abfd) != 0) | |
1417 | { | |
1418 | Elf_Internal_Verneed *t; | |
1419 | ||
1420 | fprintf (f, _("\nVersion References:\n")); | |
1421 | for (t = elf_tdata (abfd)->verref; t != NULL; t = t->vn_nextref) | |
1422 | { | |
1423 | Elf_Internal_Vernaux *a; | |
1424 | ||
d0fb9a8d JJ |
1425 | fprintf (f, _(" required from %s:\n"), |
1426 | t->vn_filename ? t->vn_filename : "<corrupt>"); | |
252b5132 RH |
1427 | for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) |
1428 | fprintf (f, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a->vna_hash, | |
d0fb9a8d JJ |
1429 | a->vna_flags, a->vna_other, |
1430 | a->vna_nodename ? a->vna_nodename : "<corrupt>"); | |
252b5132 RH |
1431 | } |
1432 | } | |
1433 | ||
b34976b6 | 1434 | return TRUE; |
252b5132 RH |
1435 | |
1436 | error_return: | |
1437 | if (dynbuf != NULL) | |
1438 | free (dynbuf); | |
b34976b6 | 1439 | return FALSE; |
252b5132 RH |
1440 | } |
1441 | ||
bb4d2ac2 L |
1442 | /* Get version string. */ |
1443 | ||
1444 | const char * | |
60bb06bc L |
1445 | _bfd_elf_get_symbol_version_string (bfd *abfd, asymbol *symbol, |
1446 | bfd_boolean *hidden) | |
bb4d2ac2 L |
1447 | { |
1448 | const char *version_string = NULL; | |
1449 | if (elf_dynversym (abfd) != 0 | |
1450 | && (elf_dynverdef (abfd) != 0 || elf_dynverref (abfd) != 0)) | |
1451 | { | |
1452 | unsigned int vernum = ((elf_symbol_type *) symbol)->version; | |
1453 | ||
1454 | *hidden = (vernum & VERSYM_HIDDEN) != 0; | |
1455 | vernum &= VERSYM_VERSION; | |
1456 | ||
1457 | if (vernum == 0) | |
1458 | version_string = ""; | |
1459 | else if (vernum == 1) | |
1460 | version_string = "Base"; | |
1461 | else if (vernum <= elf_tdata (abfd)->cverdefs) | |
1462 | version_string = | |
1463 | elf_tdata (abfd)->verdef[vernum - 1].vd_nodename; | |
1464 | else | |
1465 | { | |
1466 | Elf_Internal_Verneed *t; | |
1467 | ||
1468 | version_string = ""; | |
1469 | for (t = elf_tdata (abfd)->verref; | |
1470 | t != NULL; | |
1471 | t = t->vn_nextref) | |
1472 | { | |
1473 | Elf_Internal_Vernaux *a; | |
1474 | ||
1475 | for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) | |
1476 | { | |
1477 | if (a->vna_other == vernum) | |
1478 | { | |
1479 | version_string = a->vna_nodename; | |
1480 | break; | |
1481 | } | |
1482 | } | |
1483 | } | |
1484 | } | |
1485 | } | |
1486 | return version_string; | |
1487 | } | |
1488 | ||
252b5132 RH |
1489 | /* Display ELF-specific fields of a symbol. */ |
1490 | ||
1491 | void | |
217aa764 AM |
1492 | bfd_elf_print_symbol (bfd *abfd, |
1493 | void *filep, | |
1494 | asymbol *symbol, | |
1495 | bfd_print_symbol_type how) | |
252b5132 | 1496 | { |
a50b1753 | 1497 | FILE *file = (FILE *) filep; |
252b5132 RH |
1498 | switch (how) |
1499 | { | |
1500 | case bfd_print_symbol_name: | |
1501 | fprintf (file, "%s", symbol->name); | |
1502 | break; | |
1503 | case bfd_print_symbol_more: | |
1504 | fprintf (file, "elf "); | |
60b89a18 | 1505 | bfd_fprintf_vma (abfd, file, symbol->value); |
0af1713e | 1506 | fprintf (file, " %lx", (unsigned long) symbol->flags); |
252b5132 RH |
1507 | break; |
1508 | case bfd_print_symbol_all: | |
1509 | { | |
4e8a9624 AM |
1510 | const char *section_name; |
1511 | const char *name = NULL; | |
9c5bfbb7 | 1512 | const struct elf_backend_data *bed; |
7a13edea | 1513 | unsigned char st_other; |
dbb410c3 | 1514 | bfd_vma val; |
bb4d2ac2 L |
1515 | const char *version_string; |
1516 | bfd_boolean hidden; | |
c044fabd | 1517 | |
252b5132 | 1518 | section_name = symbol->section ? symbol->section->name : "(*none*)"; |
587ff49e RH |
1519 | |
1520 | bed = get_elf_backend_data (abfd); | |
1521 | if (bed->elf_backend_print_symbol_all) | |
c044fabd | 1522 | name = (*bed->elf_backend_print_symbol_all) (abfd, filep, symbol); |
587ff49e RH |
1523 | |
1524 | if (name == NULL) | |
1525 | { | |
7ee38065 | 1526 | name = symbol->name; |
217aa764 | 1527 | bfd_print_symbol_vandf (abfd, file, symbol); |
587ff49e RH |
1528 | } |
1529 | ||
252b5132 RH |
1530 | fprintf (file, " %s\t", section_name); |
1531 | /* Print the "other" value for a symbol. For common symbols, | |
1532 | we've already printed the size; now print the alignment. | |
1533 | For other symbols, we have no specified alignment, and | |
1534 | we've printed the address; now print the size. */ | |
dcf6c779 | 1535 | if (symbol->section && bfd_is_com_section (symbol->section)) |
dbb410c3 AM |
1536 | val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_value; |
1537 | else | |
1538 | val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_size; | |
1539 | bfd_fprintf_vma (abfd, file, val); | |
252b5132 RH |
1540 | |
1541 | /* If we have version information, print it. */ | |
60bb06bc L |
1542 | version_string = _bfd_elf_get_symbol_version_string (abfd, |
1543 | symbol, | |
1544 | &hidden); | |
bb4d2ac2 | 1545 | if (version_string) |
252b5132 | 1546 | { |
bb4d2ac2 | 1547 | if (!hidden) |
252b5132 RH |
1548 | fprintf (file, " %-11s", version_string); |
1549 | else | |
1550 | { | |
1551 | int i; | |
1552 | ||
1553 | fprintf (file, " (%s)", version_string); | |
1554 | for (i = 10 - strlen (version_string); i > 0; --i) | |
1555 | putc (' ', file); | |
1556 | } | |
1557 | } | |
1558 | ||
1559 | /* If the st_other field is not zero, print it. */ | |
7a13edea | 1560 | st_other = ((elf_symbol_type *) symbol)->internal_elf_sym.st_other; |
c044fabd | 1561 | |
7a13edea NC |
1562 | switch (st_other) |
1563 | { | |
1564 | case 0: break; | |
1565 | case STV_INTERNAL: fprintf (file, " .internal"); break; | |
1566 | case STV_HIDDEN: fprintf (file, " .hidden"); break; | |
1567 | case STV_PROTECTED: fprintf (file, " .protected"); break; | |
1568 | default: | |
1569 | /* Some other non-defined flags are also present, so print | |
1570 | everything hex. */ | |
1571 | fprintf (file, " 0x%02x", (unsigned int) st_other); | |
1572 | } | |
252b5132 | 1573 | |
587ff49e | 1574 | fprintf (file, " %s", name); |
252b5132 RH |
1575 | } |
1576 | break; | |
1577 | } | |
1578 | } | |
252b5132 | 1579 | |
252b5132 RH |
1580 | /* Allocate an ELF string table--force the first byte to be zero. */ |
1581 | ||
1582 | struct bfd_strtab_hash * | |
217aa764 | 1583 | _bfd_elf_stringtab_init (void) |
252b5132 RH |
1584 | { |
1585 | struct bfd_strtab_hash *ret; | |
1586 | ||
1587 | ret = _bfd_stringtab_init (); | |
1588 | if (ret != NULL) | |
1589 | { | |
1590 | bfd_size_type loc; | |
1591 | ||
b34976b6 | 1592 | loc = _bfd_stringtab_add (ret, "", TRUE, FALSE); |
252b5132 RH |
1593 | BFD_ASSERT (loc == 0 || loc == (bfd_size_type) -1); |
1594 | if (loc == (bfd_size_type) -1) | |
1595 | { | |
1596 | _bfd_stringtab_free (ret); | |
1597 | ret = NULL; | |
1598 | } | |
1599 | } | |
1600 | return ret; | |
1601 | } | |
1602 | \f | |
1603 | /* ELF .o/exec file reading */ | |
1604 | ||
c044fabd | 1605 | /* Create a new bfd section from an ELF section header. */ |
252b5132 | 1606 | |
b34976b6 | 1607 | bfd_boolean |
217aa764 | 1608 | bfd_section_from_shdr (bfd *abfd, unsigned int shindex) |
252b5132 | 1609 | { |
4fbb74a6 AM |
1610 | Elf_Internal_Shdr *hdr; |
1611 | Elf_Internal_Ehdr *ehdr; | |
1612 | const struct elf_backend_data *bed; | |
90937f86 | 1613 | const char *name; |
bf67003b NC |
1614 | bfd_boolean ret = TRUE; |
1615 | static bfd_boolean * sections_being_created = NULL; | |
5a4b0ccc | 1616 | static bfd * sections_being_created_abfd = NULL; |
bf67003b | 1617 | static unsigned int nesting = 0; |
252b5132 | 1618 | |
4fbb74a6 AM |
1619 | if (shindex >= elf_numsections (abfd)) |
1620 | return FALSE; | |
1621 | ||
bf67003b NC |
1622 | if (++ nesting > 3) |
1623 | { | |
1624 | /* PR17512: A corrupt ELF binary might contain a recursive group of | |
06614111 | 1625 | sections, with each the string indicies pointing to the next in the |
bf67003b NC |
1626 | loop. Detect this here, by refusing to load a section that we are |
1627 | already in the process of loading. We only trigger this test if | |
1628 | we have nested at least three sections deep as normal ELF binaries | |
5a4b0ccc NC |
1629 | can expect to recurse at least once. |
1630 | ||
1631 | FIXME: It would be better if this array was attached to the bfd, | |
1632 | rather than being held in a static pointer. */ | |
1633 | ||
1634 | if (sections_being_created_abfd != abfd) | |
1635 | sections_being_created = NULL; | |
bf67003b NC |
1636 | if (sections_being_created == NULL) |
1637 | { | |
1638 | /* FIXME: It would be more efficient to attach this array to the bfd somehow. */ | |
1639 | sections_being_created = (bfd_boolean *) | |
1640 | bfd_zalloc (abfd, elf_numsections (abfd) * sizeof (bfd_boolean)); | |
5a4b0ccc | 1641 | sections_being_created_abfd = abfd; |
bf67003b NC |
1642 | } |
1643 | if (sections_being_created [shindex]) | |
1644 | { | |
1645 | (*_bfd_error_handler) | |
1646 | (_("%B: warning: loop in section dependencies detected"), abfd); | |
1647 | return FALSE; | |
1648 | } | |
1649 | sections_being_created [shindex] = TRUE; | |
1650 | } | |
1651 | ||
4fbb74a6 AM |
1652 | hdr = elf_elfsections (abfd)[shindex]; |
1653 | ehdr = elf_elfheader (abfd); | |
1654 | name = bfd_elf_string_from_elf_section (abfd, ehdr->e_shstrndx, | |
1b3a8575 | 1655 | hdr->sh_name); |
933d961a | 1656 | if (name == NULL) |
bf67003b | 1657 | goto fail; |
252b5132 | 1658 | |
4fbb74a6 | 1659 | bed = get_elf_backend_data (abfd); |
252b5132 RH |
1660 | switch (hdr->sh_type) |
1661 | { | |
1662 | case SHT_NULL: | |
1663 | /* Inactive section. Throw it away. */ | |
bf67003b | 1664 | goto success; |
252b5132 | 1665 | |
bf67003b NC |
1666 | case SHT_PROGBITS: /* Normal section with contents. */ |
1667 | case SHT_NOBITS: /* .bss section. */ | |
1668 | case SHT_HASH: /* .hash section. */ | |
1669 | case SHT_NOTE: /* .note section. */ | |
25e27870 L |
1670 | case SHT_INIT_ARRAY: /* .init_array section. */ |
1671 | case SHT_FINI_ARRAY: /* .fini_array section. */ | |
1672 | case SHT_PREINIT_ARRAY: /* .preinit_array section. */ | |
7f1204bb | 1673 | case SHT_GNU_LIBLIST: /* .gnu.liblist section. */ |
fdc90cb4 | 1674 | case SHT_GNU_HASH: /* .gnu.hash section. */ |
bf67003b NC |
1675 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); |
1676 | goto success; | |
252b5132 | 1677 | |
797fc050 | 1678 | case SHT_DYNAMIC: /* Dynamic linking information. */ |
6dc132d9 | 1679 | if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex)) |
bf67003b NC |
1680 | goto fail; |
1681 | ||
cfcac11d NC |
1682 | if (hdr->sh_link > elf_numsections (abfd)) |
1683 | { | |
caa83f8b | 1684 | /* PR 10478: Accept Solaris binaries with a sh_link |
cfcac11d NC |
1685 | field set to SHN_BEFORE or SHN_AFTER. */ |
1686 | switch (bfd_get_arch (abfd)) | |
1687 | { | |
caa83f8b | 1688 | case bfd_arch_i386: |
cfcac11d NC |
1689 | case bfd_arch_sparc: |
1690 | if (hdr->sh_link == (SHN_LORESERVE & 0xffff) /* SHN_BEFORE */ | |
1691 | || hdr->sh_link == ((SHN_LORESERVE + 1) & 0xffff) /* SHN_AFTER */) | |
1692 | break; | |
1693 | /* Otherwise fall through. */ | |
1694 | default: | |
bf67003b | 1695 | goto fail; |
cfcac11d NC |
1696 | } |
1697 | } | |
1698 | else if (elf_elfsections (abfd)[hdr->sh_link] == NULL) | |
bf67003b | 1699 | goto fail; |
cfcac11d | 1700 | else if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_STRTAB) |
797fc050 AM |
1701 | { |
1702 | Elf_Internal_Shdr *dynsymhdr; | |
1703 | ||
1704 | /* The shared libraries distributed with hpux11 have a bogus | |
1705 | sh_link field for the ".dynamic" section. Find the | |
1706 | string table for the ".dynsym" section instead. */ | |
1707 | if (elf_dynsymtab (abfd) != 0) | |
1708 | { | |
1709 | dynsymhdr = elf_elfsections (abfd)[elf_dynsymtab (abfd)]; | |
1710 | hdr->sh_link = dynsymhdr->sh_link; | |
1711 | } | |
1712 | else | |
1713 | { | |
1714 | unsigned int i, num_sec; | |
1715 | ||
1716 | num_sec = elf_numsections (abfd); | |
1717 | for (i = 1; i < num_sec; i++) | |
1718 | { | |
1719 | dynsymhdr = elf_elfsections (abfd)[i]; | |
1720 | if (dynsymhdr->sh_type == SHT_DYNSYM) | |
1721 | { | |
1722 | hdr->sh_link = dynsymhdr->sh_link; | |
1723 | break; | |
1724 | } | |
1725 | } | |
1726 | } | |
1727 | } | |
bf67003b | 1728 | goto success; |
797fc050 | 1729 | |
bf67003b | 1730 | case SHT_SYMTAB: /* A symbol table. */ |
252b5132 | 1731 | if (elf_onesymtab (abfd) == shindex) |
bf67003b | 1732 | goto success; |
252b5132 | 1733 | |
a50b2160 | 1734 | if (hdr->sh_entsize != bed->s->sizeof_sym) |
bf67003b NC |
1735 | goto fail; |
1736 | ||
3337c1e5 | 1737 | if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size) |
eee3b786 AM |
1738 | { |
1739 | if (hdr->sh_size != 0) | |
bf67003b | 1740 | goto fail; |
eee3b786 AM |
1741 | /* Some assemblers erroneously set sh_info to one with a |
1742 | zero sh_size. ld sees this as a global symbol count | |
1743 | of (unsigned) -1. Fix it here. */ | |
1744 | hdr->sh_info = 0; | |
bf67003b | 1745 | goto success; |
eee3b786 | 1746 | } |
bf67003b | 1747 | |
252b5132 RH |
1748 | BFD_ASSERT (elf_onesymtab (abfd) == 0); |
1749 | elf_onesymtab (abfd) = shindex; | |
1750 | elf_tdata (abfd)->symtab_hdr = *hdr; | |
1751 | elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->symtab_hdr; | |
1752 | abfd->flags |= HAS_SYMS; | |
1753 | ||
1754 | /* Sometimes a shared object will map in the symbol table. If | |
08a40648 AM |
1755 | SHF_ALLOC is set, and this is a shared object, then we also |
1756 | treat this section as a BFD section. We can not base the | |
1757 | decision purely on SHF_ALLOC, because that flag is sometimes | |
1758 | set in a relocatable object file, which would confuse the | |
1759 | linker. */ | |
252b5132 RH |
1760 | if ((hdr->sh_flags & SHF_ALLOC) != 0 |
1761 | && (abfd->flags & DYNAMIC) != 0 | |
6dc132d9 L |
1762 | && ! _bfd_elf_make_section_from_shdr (abfd, hdr, name, |
1763 | shindex)) | |
bf67003b | 1764 | goto fail; |
252b5132 | 1765 | |
1b3a8575 AM |
1766 | /* Go looking for SHT_SYMTAB_SHNDX too, since if there is one we |
1767 | can't read symbols without that section loaded as well. It | |
1768 | is most likely specified by the next section header. */ | |
1769 | if (elf_elfsections (abfd)[elf_symtab_shndx (abfd)]->sh_link != shindex) | |
1770 | { | |
1771 | unsigned int i, num_sec; | |
1772 | ||
1773 | num_sec = elf_numsections (abfd); | |
1774 | for (i = shindex + 1; i < num_sec; i++) | |
1775 | { | |
1776 | Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i]; | |
1777 | if (hdr2->sh_type == SHT_SYMTAB_SHNDX | |
1778 | && hdr2->sh_link == shindex) | |
1779 | break; | |
1780 | } | |
1781 | if (i == num_sec) | |
1782 | for (i = 1; i < shindex; i++) | |
1783 | { | |
1784 | Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i]; | |
1785 | if (hdr2->sh_type == SHT_SYMTAB_SHNDX | |
1786 | && hdr2->sh_link == shindex) | |
1787 | break; | |
1788 | } | |
1789 | if (i != shindex) | |
bf67003b | 1790 | ret = bfd_section_from_shdr (abfd, i); |
1b3a8575 | 1791 | } |
bf67003b | 1792 | goto success; |
252b5132 | 1793 | |
bf67003b | 1794 | case SHT_DYNSYM: /* A dynamic symbol table. */ |
252b5132 | 1795 | if (elf_dynsymtab (abfd) == shindex) |
bf67003b | 1796 | goto success; |
252b5132 | 1797 | |
a50b2160 | 1798 | if (hdr->sh_entsize != bed->s->sizeof_sym) |
bf67003b NC |
1799 | goto fail; |
1800 | ||
eee3b786 AM |
1801 | if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size) |
1802 | { | |
1803 | if (hdr->sh_size != 0) | |
bf67003b NC |
1804 | goto fail; |
1805 | ||
eee3b786 AM |
1806 | /* Some linkers erroneously set sh_info to one with a |
1807 | zero sh_size. ld sees this as a global symbol count | |
1808 | of (unsigned) -1. Fix it here. */ | |
1809 | hdr->sh_info = 0; | |
bf67003b | 1810 | goto success; |
eee3b786 | 1811 | } |
bf67003b | 1812 | |
252b5132 RH |
1813 | BFD_ASSERT (elf_dynsymtab (abfd) == 0); |
1814 | elf_dynsymtab (abfd) = shindex; | |
1815 | elf_tdata (abfd)->dynsymtab_hdr = *hdr; | |
1816 | elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->dynsymtab_hdr; | |
1817 | abfd->flags |= HAS_SYMS; | |
1818 | ||
1819 | /* Besides being a symbol table, we also treat this as a regular | |
1820 | section, so that objcopy can handle it. */ | |
bf67003b NC |
1821 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); |
1822 | goto success; | |
252b5132 | 1823 | |
bf67003b | 1824 | case SHT_SYMTAB_SHNDX: /* Symbol section indices when >64k sections. */ |
9ad5cbcf | 1825 | if (elf_symtab_shndx (abfd) == shindex) |
bf67003b | 1826 | goto success; |
9ad5cbcf | 1827 | |
1b3a8575 | 1828 | BFD_ASSERT (elf_symtab_shndx (abfd) == 0); |
9ad5cbcf AM |
1829 | elf_symtab_shndx (abfd) = shindex; |
1830 | elf_tdata (abfd)->symtab_shndx_hdr = *hdr; | |
1831 | elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->symtab_shndx_hdr; | |
bf67003b | 1832 | goto success; |
9ad5cbcf | 1833 | |
bf67003b | 1834 | case SHT_STRTAB: /* A string table. */ |
252b5132 | 1835 | if (hdr->bfd_section != NULL) |
bf67003b NC |
1836 | goto success; |
1837 | ||
252b5132 RH |
1838 | if (ehdr->e_shstrndx == shindex) |
1839 | { | |
1840 | elf_tdata (abfd)->shstrtab_hdr = *hdr; | |
1841 | elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->shstrtab_hdr; | |
bf67003b | 1842 | goto success; |
252b5132 | 1843 | } |
bf67003b | 1844 | |
1b3a8575 AM |
1845 | if (elf_elfsections (abfd)[elf_onesymtab (abfd)]->sh_link == shindex) |
1846 | { | |
1847 | symtab_strtab: | |
1848 | elf_tdata (abfd)->strtab_hdr = *hdr; | |
1849 | elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->strtab_hdr; | |
bf67003b | 1850 | goto success; |
1b3a8575 | 1851 | } |
bf67003b | 1852 | |
1b3a8575 AM |
1853 | if (elf_elfsections (abfd)[elf_dynsymtab (abfd)]->sh_link == shindex) |
1854 | { | |
1855 | dynsymtab_strtab: | |
1856 | elf_tdata (abfd)->dynstrtab_hdr = *hdr; | |
1857 | hdr = &elf_tdata (abfd)->dynstrtab_hdr; | |
1858 | elf_elfsections (abfd)[shindex] = hdr; | |
1859 | /* We also treat this as a regular section, so that objcopy | |
1860 | can handle it. */ | |
bf67003b NC |
1861 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, |
1862 | shindex); | |
1863 | goto success; | |
1b3a8575 | 1864 | } |
252b5132 | 1865 | |
1b3a8575 AM |
1866 | /* If the string table isn't one of the above, then treat it as a |
1867 | regular section. We need to scan all the headers to be sure, | |
1868 | just in case this strtab section appeared before the above. */ | |
1869 | if (elf_onesymtab (abfd) == 0 || elf_dynsymtab (abfd) == 0) | |
1870 | { | |
1871 | unsigned int i, num_sec; | |
252b5132 | 1872 | |
1b3a8575 AM |
1873 | num_sec = elf_numsections (abfd); |
1874 | for (i = 1; i < num_sec; i++) | |
1875 | { | |
1876 | Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i]; | |
1877 | if (hdr2->sh_link == shindex) | |
1878 | { | |
933d961a JJ |
1879 | /* Prevent endless recursion on broken objects. */ |
1880 | if (i == shindex) | |
bf67003b | 1881 | goto fail; |
1b3a8575 | 1882 | if (! bfd_section_from_shdr (abfd, i)) |
bf67003b | 1883 | goto fail; |
1b3a8575 AM |
1884 | if (elf_onesymtab (abfd) == i) |
1885 | goto symtab_strtab; | |
1886 | if (elf_dynsymtab (abfd) == i) | |
1887 | goto dynsymtab_strtab; | |
1888 | } | |
1889 | } | |
1890 | } | |
bf67003b NC |
1891 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); |
1892 | goto success; | |
252b5132 RH |
1893 | |
1894 | case SHT_REL: | |
1895 | case SHT_RELA: | |
1896 | /* *These* do a lot of work -- but build no sections! */ | |
1897 | { | |
1898 | asection *target_sect; | |
d4730f92 | 1899 | Elf_Internal_Shdr *hdr2, **p_hdr; |
9ad5cbcf | 1900 | unsigned int num_sec = elf_numsections (abfd); |
d4730f92 BS |
1901 | struct bfd_elf_section_data *esdt; |
1902 | bfd_size_type amt; | |
252b5132 | 1903 | |
aa2ca951 JJ |
1904 | if (hdr->sh_entsize |
1905 | != (bfd_size_type) (hdr->sh_type == SHT_REL | |
a50b2160 | 1906 | ? bed->s->sizeof_rel : bed->s->sizeof_rela)) |
bf67003b | 1907 | goto fail; |
a50b2160 | 1908 | |
03ae5f59 | 1909 | /* Check for a bogus link to avoid crashing. */ |
4fbb74a6 | 1910 | if (hdr->sh_link >= num_sec) |
03ae5f59 ILT |
1911 | { |
1912 | ((*_bfd_error_handler) | |
d003868e AM |
1913 | (_("%B: invalid link %lu for reloc section %s (index %u)"), |
1914 | abfd, hdr->sh_link, name, shindex)); | |
bf67003b NC |
1915 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, |
1916 | shindex); | |
1917 | goto success; | |
03ae5f59 ILT |
1918 | } |
1919 | ||
252b5132 RH |
1920 | /* For some incomprehensible reason Oracle distributes |
1921 | libraries for Solaris in which some of the objects have | |
1922 | bogus sh_link fields. It would be nice if we could just | |
1923 | reject them, but, unfortunately, some people need to use | |
1924 | them. We scan through the section headers; if we find only | |
1925 | one suitable symbol table, we clobber the sh_link to point | |
83b89087 L |
1926 | to it. I hope this doesn't break anything. |
1927 | ||
1928 | Don't do it on executable nor shared library. */ | |
1929 | if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0 | |
1930 | && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_SYMTAB | |
252b5132 RH |
1931 | && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_DYNSYM) |
1932 | { | |
9ad5cbcf | 1933 | unsigned int scan; |
252b5132 RH |
1934 | int found; |
1935 | ||
1936 | found = 0; | |
9ad5cbcf | 1937 | for (scan = 1; scan < num_sec; scan++) |
252b5132 RH |
1938 | { |
1939 | if (elf_elfsections (abfd)[scan]->sh_type == SHT_SYMTAB | |
1940 | || elf_elfsections (abfd)[scan]->sh_type == SHT_DYNSYM) | |
1941 | { | |
1942 | if (found != 0) | |
1943 | { | |
1944 | found = 0; | |
1945 | break; | |
1946 | } | |
1947 | found = scan; | |
1948 | } | |
1949 | } | |
1950 | if (found != 0) | |
1951 | hdr->sh_link = found; | |
1952 | } | |
1953 | ||
1954 | /* Get the symbol table. */ | |
1b3a8575 AM |
1955 | if ((elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_SYMTAB |
1956 | || elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_DYNSYM) | |
252b5132 | 1957 | && ! bfd_section_from_shdr (abfd, hdr->sh_link)) |
bf67003b | 1958 | goto fail; |
252b5132 RH |
1959 | |
1960 | /* If this reloc section does not use the main symbol table we | |
1961 | don't treat it as a reloc section. BFD can't adequately | |
1962 | represent such a section, so at least for now, we don't | |
c044fabd | 1963 | try. We just present it as a normal section. We also |
60bcf0fa | 1964 | can't use it as a reloc section if it points to the null |
83b89087 L |
1965 | section, an invalid section, another reloc section, or its |
1966 | sh_link points to the null section. */ | |
185ef66d | 1967 | if (hdr->sh_link != elf_onesymtab (abfd) |
83b89087 | 1968 | || hdr->sh_link == SHN_UNDEF |
185ef66d | 1969 | || hdr->sh_info == SHN_UNDEF |
185ef66d AM |
1970 | || hdr->sh_info >= num_sec |
1971 | || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_REL | |
1972 | || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_RELA) | |
bf67003b NC |
1973 | { |
1974 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, | |
1975 | shindex); | |
1976 | goto success; | |
1977 | } | |
252b5132 RH |
1978 | |
1979 | if (! bfd_section_from_shdr (abfd, hdr->sh_info)) | |
bf67003b NC |
1980 | goto fail; |
1981 | ||
252b5132 RH |
1982 | target_sect = bfd_section_from_elf_index (abfd, hdr->sh_info); |
1983 | if (target_sect == NULL) | |
bf67003b | 1984 | goto fail; |
252b5132 | 1985 | |
d4730f92 BS |
1986 | esdt = elf_section_data (target_sect); |
1987 | if (hdr->sh_type == SHT_RELA) | |
1988 | p_hdr = &esdt->rela.hdr; | |
252b5132 | 1989 | else |
d4730f92 BS |
1990 | p_hdr = &esdt->rel.hdr; |
1991 | ||
06614111 NC |
1992 | /* PR 17512: file: 0b4f81b7. */ |
1993 | if (*p_hdr != NULL) | |
1994 | goto fail; | |
d4730f92 BS |
1995 | amt = sizeof (*hdr2); |
1996 | hdr2 = (Elf_Internal_Shdr *) bfd_alloc (abfd, amt); | |
1997 | if (hdr2 == NULL) | |
bf67003b | 1998 | goto fail; |
252b5132 | 1999 | *hdr2 = *hdr; |
d4730f92 | 2000 | *p_hdr = hdr2; |
252b5132 | 2001 | elf_elfsections (abfd)[shindex] = hdr2; |
d9bc7a44 | 2002 | target_sect->reloc_count += NUM_SHDR_ENTRIES (hdr); |
252b5132 RH |
2003 | target_sect->flags |= SEC_RELOC; |
2004 | target_sect->relocation = NULL; | |
2005 | target_sect->rel_filepos = hdr->sh_offset; | |
bf572ba0 MM |
2006 | /* In the section to which the relocations apply, mark whether |
2007 | its relocations are of the REL or RELA variety. */ | |
72730e0c | 2008 | if (hdr->sh_size != 0) |
d4730f92 BS |
2009 | { |
2010 | if (hdr->sh_type == SHT_RELA) | |
2011 | target_sect->use_rela_p = 1; | |
2012 | } | |
252b5132 | 2013 | abfd->flags |= HAS_RELOC; |
bf67003b | 2014 | goto success; |
252b5132 | 2015 | } |
252b5132 RH |
2016 | |
2017 | case SHT_GNU_verdef: | |
2018 | elf_dynverdef (abfd) = shindex; | |
2019 | elf_tdata (abfd)->dynverdef_hdr = *hdr; | |
bf67003b NC |
2020 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); |
2021 | goto success; | |
252b5132 RH |
2022 | |
2023 | case SHT_GNU_versym: | |
a50b2160 | 2024 | if (hdr->sh_entsize != sizeof (Elf_External_Versym)) |
bf67003b NC |
2025 | goto fail; |
2026 | ||
252b5132 RH |
2027 | elf_dynversym (abfd) = shindex; |
2028 | elf_tdata (abfd)->dynversym_hdr = *hdr; | |
bf67003b NC |
2029 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); |
2030 | goto success; | |
252b5132 RH |
2031 | |
2032 | case SHT_GNU_verneed: | |
2033 | elf_dynverref (abfd) = shindex; | |
2034 | elf_tdata (abfd)->dynverref_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_SHLIB: | |
bf67003b | 2039 | goto success; |
252b5132 | 2040 | |
dbb410c3 | 2041 | case SHT_GROUP: |
44534af3 | 2042 | if (! IS_VALID_GROUP_SECTION_HEADER (hdr, GRP_ENTRY_SIZE)) |
bf67003b NC |
2043 | goto fail; |
2044 | ||
6dc132d9 | 2045 | if (!_bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex)) |
bf67003b NC |
2046 | goto fail; |
2047 | ||
dbb410c3 AM |
2048 | if (hdr->contents != NULL) |
2049 | { | |
2050 | Elf_Internal_Group *idx = (Elf_Internal_Group *) hdr->contents; | |
06614111 | 2051 | unsigned int n_elt = hdr->sh_size / sizeof (* idx); |
dbb410c3 AM |
2052 | asection *s; |
2053 | ||
06614111 NC |
2054 | if (n_elt == 0) |
2055 | goto fail; | |
b885599b AM |
2056 | if (idx->flags & GRP_COMDAT) |
2057 | hdr->bfd_section->flags | |
2058 | |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD; | |
2059 | ||
45c5e9ed L |
2060 | /* We try to keep the same section order as it comes in. */ |
2061 | idx += n_elt; | |
06614111 | 2062 | |
dbb410c3 | 2063 | while (--n_elt != 0) |
1783205a NC |
2064 | { |
2065 | --idx; | |
2066 | ||
2067 | if (idx->shdr != NULL | |
2068 | && (s = idx->shdr->bfd_section) != NULL | |
2069 | && elf_next_in_group (s) != NULL) | |
2070 | { | |
2071 | elf_next_in_group (hdr->bfd_section) = s; | |
2072 | break; | |
2073 | } | |
2074 | } | |
dbb410c3 | 2075 | } |
bf67003b | 2076 | goto success; |
dbb410c3 | 2077 | |
252b5132 | 2078 | default: |
104d59d1 JM |
2079 | /* Possibly an attributes section. */ |
2080 | if (hdr->sh_type == SHT_GNU_ATTRIBUTES | |
2081 | || hdr->sh_type == bed->obj_attrs_section_type) | |
2082 | { | |
2083 | if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex)) | |
bf67003b | 2084 | goto fail; |
104d59d1 | 2085 | _bfd_elf_parse_attributes (abfd, hdr); |
bf67003b | 2086 | goto success; |
104d59d1 JM |
2087 | } |
2088 | ||
252b5132 | 2089 | /* Check for any processor-specific section types. */ |
3eb70a79 | 2090 | if (bed->elf_backend_section_from_shdr (abfd, hdr, name, shindex)) |
bf67003b | 2091 | goto success; |
3eb70a79 L |
2092 | |
2093 | if (hdr->sh_type >= SHT_LOUSER && hdr->sh_type <= SHT_HIUSER) | |
2094 | { | |
2095 | if ((hdr->sh_flags & SHF_ALLOC) != 0) | |
2096 | /* FIXME: How to properly handle allocated section reserved | |
2097 | for applications? */ | |
2098 | (*_bfd_error_handler) | |
2099 | (_("%B: don't know how to handle allocated, application " | |
2100 | "specific section `%s' [0x%8x]"), | |
2101 | abfd, name, hdr->sh_type); | |
2102 | else | |
bf67003b NC |
2103 | { |
2104 | /* Allow sections reserved for applications. */ | |
2105 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, | |
2106 | shindex); | |
2107 | goto success; | |
2108 | } | |
3eb70a79 L |
2109 | } |
2110 | else if (hdr->sh_type >= SHT_LOPROC | |
2111 | && hdr->sh_type <= SHT_HIPROC) | |
2112 | /* FIXME: We should handle this section. */ | |
2113 | (*_bfd_error_handler) | |
2114 | (_("%B: don't know how to handle processor specific section " | |
2115 | "`%s' [0x%8x]"), | |
2116 | abfd, name, hdr->sh_type); | |
2117 | else if (hdr->sh_type >= SHT_LOOS && hdr->sh_type <= SHT_HIOS) | |
ff15b240 NC |
2118 | { |
2119 | /* Unrecognised OS-specific sections. */ | |
2120 | if ((hdr->sh_flags & SHF_OS_NONCONFORMING) != 0) | |
2121 | /* SHF_OS_NONCONFORMING indicates that special knowledge is | |
08a40648 | 2122 | required to correctly process the section and the file should |
ff15b240 NC |
2123 | be rejected with an error message. */ |
2124 | (*_bfd_error_handler) | |
2125 | (_("%B: don't know how to handle OS specific section " | |
2126 | "`%s' [0x%8x]"), | |
2127 | abfd, name, hdr->sh_type); | |
2128 | else | |
bf67003b NC |
2129 | { |
2130 | /* Otherwise it should be processed. */ | |
2131 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); | |
2132 | goto success; | |
2133 | } | |
ff15b240 | 2134 | } |
3eb70a79 L |
2135 | else |
2136 | /* FIXME: We should handle this section. */ | |
2137 | (*_bfd_error_handler) | |
2138 | (_("%B: don't know how to handle section `%s' [0x%8x]"), | |
2139 | abfd, name, hdr->sh_type); | |
2140 | ||
bf67003b | 2141 | goto fail; |
252b5132 RH |
2142 | } |
2143 | ||
bf67003b NC |
2144 | fail: |
2145 | ret = FALSE; | |
2146 | success: | |
e5b470e2 | 2147 | if (sections_being_created && sections_being_created_abfd == abfd) |
bf67003b NC |
2148 | sections_being_created [shindex] = FALSE; |
2149 | if (-- nesting == 0) | |
5a4b0ccc NC |
2150 | { |
2151 | sections_being_created = NULL; | |
2152 | sections_being_created_abfd = abfd; | |
2153 | } | |
bf67003b | 2154 | return ret; |
252b5132 RH |
2155 | } |
2156 | ||
87d72d41 | 2157 | /* Return the local symbol specified by ABFD, R_SYMNDX. */ |
ec338859 | 2158 | |
87d72d41 AM |
2159 | Elf_Internal_Sym * |
2160 | bfd_sym_from_r_symndx (struct sym_cache *cache, | |
2161 | bfd *abfd, | |
2162 | unsigned long r_symndx) | |
ec338859 | 2163 | { |
ec338859 AM |
2164 | unsigned int ent = r_symndx % LOCAL_SYM_CACHE_SIZE; |
2165 | ||
a5d1b3b5 AM |
2166 | if (cache->abfd != abfd || cache->indx[ent] != r_symndx) |
2167 | { | |
2168 | Elf_Internal_Shdr *symtab_hdr; | |
2169 | unsigned char esym[sizeof (Elf64_External_Sym)]; | |
2170 | Elf_External_Sym_Shndx eshndx; | |
ec338859 | 2171 | |
a5d1b3b5 AM |
2172 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; |
2173 | if (bfd_elf_get_elf_syms (abfd, symtab_hdr, 1, r_symndx, | |
87d72d41 | 2174 | &cache->sym[ent], esym, &eshndx) == NULL) |
a5d1b3b5 | 2175 | return NULL; |
9ad5cbcf | 2176 | |
a5d1b3b5 AM |
2177 | if (cache->abfd != abfd) |
2178 | { | |
2179 | memset (cache->indx, -1, sizeof (cache->indx)); | |
2180 | cache->abfd = abfd; | |
2181 | } | |
2182 | cache->indx[ent] = r_symndx; | |
ec338859 | 2183 | } |
a5d1b3b5 | 2184 | |
87d72d41 | 2185 | return &cache->sym[ent]; |
ec338859 AM |
2186 | } |
2187 | ||
252b5132 RH |
2188 | /* Given an ELF section number, retrieve the corresponding BFD |
2189 | section. */ | |
2190 | ||
2191 | asection * | |
91d6fa6a | 2192 | bfd_section_from_elf_index (bfd *abfd, unsigned int sec_index) |
252b5132 | 2193 | { |
91d6fa6a | 2194 | if (sec_index >= elf_numsections (abfd)) |
252b5132 | 2195 | return NULL; |
91d6fa6a | 2196 | return elf_elfsections (abfd)[sec_index]->bfd_section; |
252b5132 RH |
2197 | } |
2198 | ||
b35d266b | 2199 | static const struct bfd_elf_special_section special_sections_b[] = |
2f89ff8d | 2200 | { |
0112cd26 NC |
2201 | { STRING_COMMA_LEN (".bss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE }, |
2202 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2203 | }; |
2204 | ||
b35d266b | 2205 | static const struct bfd_elf_special_section special_sections_c[] = |
7f4d3958 | 2206 | { |
0112cd26 NC |
2207 | { STRING_COMMA_LEN (".comment"), 0, SHT_PROGBITS, 0 }, |
2208 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2209 | }; |
2210 | ||
b35d266b | 2211 | static const struct bfd_elf_special_section special_sections_d[] = |
7f4d3958 | 2212 | { |
0112cd26 NC |
2213 | { STRING_COMMA_LEN (".data"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE }, |
2214 | { STRING_COMMA_LEN (".data1"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE }, | |
a9a72a65 DE |
2215 | /* There are more DWARF sections than these, but they needn't be added here |
2216 | unless you have to cope with broken compilers that don't emit section | |
2217 | attributes or you want to help the user writing assembler. */ | |
0112cd26 NC |
2218 | { STRING_COMMA_LEN (".debug"), 0, SHT_PROGBITS, 0 }, |
2219 | { STRING_COMMA_LEN (".debug_line"), 0, SHT_PROGBITS, 0 }, | |
2220 | { STRING_COMMA_LEN (".debug_info"), 0, SHT_PROGBITS, 0 }, | |
2221 | { STRING_COMMA_LEN (".debug_abbrev"), 0, SHT_PROGBITS, 0 }, | |
2222 | { STRING_COMMA_LEN (".debug_aranges"), 0, SHT_PROGBITS, 0 }, | |
2223 | { STRING_COMMA_LEN (".dynamic"), 0, SHT_DYNAMIC, SHF_ALLOC }, | |
2224 | { STRING_COMMA_LEN (".dynstr"), 0, SHT_STRTAB, SHF_ALLOC }, | |
2225 | { STRING_COMMA_LEN (".dynsym"), 0, SHT_DYNSYM, SHF_ALLOC }, | |
2226 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2227 | }; |
2228 | ||
b35d266b | 2229 | static const struct bfd_elf_special_section special_sections_f[] = |
7f4d3958 | 2230 | { |
0112cd26 NC |
2231 | { STRING_COMMA_LEN (".fini"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR }, |
2232 | { STRING_COMMA_LEN (".fini_array"), 0, SHT_FINI_ARRAY, SHF_ALLOC + SHF_WRITE }, | |
2233 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2234 | }; |
2235 | ||
b35d266b | 2236 | static const struct bfd_elf_special_section special_sections_g[] = |
7f4d3958 | 2237 | { |
0112cd26 | 2238 | { STRING_COMMA_LEN (".gnu.linkonce.b"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE }, |
68efed41 | 2239 | { STRING_COMMA_LEN (".gnu.lto_"), -1, SHT_PROGBITS, SHF_EXCLUDE }, |
0112cd26 NC |
2240 | { STRING_COMMA_LEN (".got"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE }, |
2241 | { STRING_COMMA_LEN (".gnu.version"), 0, SHT_GNU_versym, 0 }, | |
2242 | { STRING_COMMA_LEN (".gnu.version_d"), 0, SHT_GNU_verdef, 0 }, | |
2243 | { STRING_COMMA_LEN (".gnu.version_r"), 0, SHT_GNU_verneed, 0 }, | |
2244 | { STRING_COMMA_LEN (".gnu.liblist"), 0, SHT_GNU_LIBLIST, SHF_ALLOC }, | |
2245 | { STRING_COMMA_LEN (".gnu.conflict"), 0, SHT_RELA, SHF_ALLOC }, | |
2246 | { STRING_COMMA_LEN (".gnu.hash"), 0, SHT_GNU_HASH, SHF_ALLOC }, | |
2247 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2248 | }; |
2249 | ||
b35d266b | 2250 | static const struct bfd_elf_special_section special_sections_h[] = |
7f4d3958 | 2251 | { |
0112cd26 NC |
2252 | { STRING_COMMA_LEN (".hash"), 0, SHT_HASH, SHF_ALLOC }, |
2253 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2254 | }; |
2255 | ||
b35d266b | 2256 | static const struct bfd_elf_special_section special_sections_i[] = |
7f4d3958 | 2257 | { |
0112cd26 NC |
2258 | { STRING_COMMA_LEN (".init"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR }, |
2259 | { STRING_COMMA_LEN (".init_array"), 0, SHT_INIT_ARRAY, SHF_ALLOC + SHF_WRITE }, | |
2260 | { STRING_COMMA_LEN (".interp"), 0, SHT_PROGBITS, 0 }, | |
2261 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2262 | }; |
2263 | ||
b35d266b | 2264 | static const struct bfd_elf_special_section special_sections_l[] = |
7f4d3958 | 2265 | { |
0112cd26 NC |
2266 | { STRING_COMMA_LEN (".line"), 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_n[] = |
7f4d3958 | 2271 | { |
0112cd26 NC |
2272 | { STRING_COMMA_LEN (".note.GNU-stack"), 0, SHT_PROGBITS, 0 }, |
2273 | { STRING_COMMA_LEN (".note"), -1, SHT_NOTE, 0 }, | |
2274 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2275 | }; |
2276 | ||
b35d266b | 2277 | static const struct bfd_elf_special_section special_sections_p[] = |
7f4d3958 | 2278 | { |
0112cd26 NC |
2279 | { STRING_COMMA_LEN (".preinit_array"), 0, SHT_PREINIT_ARRAY, SHF_ALLOC + SHF_WRITE }, |
2280 | { STRING_COMMA_LEN (".plt"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR }, | |
2281 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2282 | }; |
2283 | ||
b35d266b | 2284 | static const struct bfd_elf_special_section special_sections_r[] = |
7f4d3958 | 2285 | { |
0112cd26 NC |
2286 | { STRING_COMMA_LEN (".rodata"), -2, SHT_PROGBITS, SHF_ALLOC }, |
2287 | { STRING_COMMA_LEN (".rodata1"), 0, SHT_PROGBITS, SHF_ALLOC }, | |
2288 | { STRING_COMMA_LEN (".rela"), -1, SHT_RELA, 0 }, | |
2289 | { STRING_COMMA_LEN (".rel"), -1, SHT_REL, 0 }, | |
2290 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2291 | }; |
2292 | ||
b35d266b | 2293 | static const struct bfd_elf_special_section special_sections_s[] = |
7f4d3958 | 2294 | { |
0112cd26 NC |
2295 | { STRING_COMMA_LEN (".shstrtab"), 0, SHT_STRTAB, 0 }, |
2296 | { STRING_COMMA_LEN (".strtab"), 0, SHT_STRTAB, 0 }, | |
2297 | { STRING_COMMA_LEN (".symtab"), 0, SHT_SYMTAB, 0 }, | |
60ff4dc4 HPN |
2298 | /* See struct bfd_elf_special_section declaration for the semantics of |
2299 | this special case where .prefix_length != strlen (.prefix). */ | |
2300 | { ".stabstr", 5, 3, SHT_STRTAB, 0 }, | |
0112cd26 | 2301 | { NULL, 0, 0, 0, 0 } |
2f89ff8d L |
2302 | }; |
2303 | ||
b35d266b | 2304 | static const struct bfd_elf_special_section special_sections_t[] = |
7f4d3958 | 2305 | { |
0112cd26 NC |
2306 | { STRING_COMMA_LEN (".text"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR }, |
2307 | { STRING_COMMA_LEN (".tbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS }, | |
2308 | { STRING_COMMA_LEN (".tdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS }, | |
2309 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2310 | }; |
2311 | ||
1b315056 CS |
2312 | static const struct bfd_elf_special_section special_sections_z[] = |
2313 | { | |
2314 | { STRING_COMMA_LEN (".zdebug_line"), 0, SHT_PROGBITS, 0 }, | |
2315 | { STRING_COMMA_LEN (".zdebug_info"), 0, SHT_PROGBITS, 0 }, | |
2316 | { STRING_COMMA_LEN (".zdebug_abbrev"), 0, SHT_PROGBITS, 0 }, | |
2317 | { STRING_COMMA_LEN (".zdebug_aranges"), 0, SHT_PROGBITS, 0 }, | |
2318 | { NULL, 0, 0, 0, 0 } | |
2319 | }; | |
2320 | ||
e4c93b56 | 2321 | static const struct bfd_elf_special_section * const special_sections[] = |
7f4d3958 | 2322 | { |
7f4d3958 | 2323 | special_sections_b, /* 'b' */ |
98ece1b3 | 2324 | special_sections_c, /* 'c' */ |
7f4d3958 L |
2325 | special_sections_d, /* 'd' */ |
2326 | NULL, /* 'e' */ | |
2327 | special_sections_f, /* 'f' */ | |
2328 | special_sections_g, /* 'g' */ | |
2329 | special_sections_h, /* 'h' */ | |
2330 | special_sections_i, /* 'i' */ | |
2331 | NULL, /* 'j' */ | |
2332 | NULL, /* 'k' */ | |
2333 | special_sections_l, /* 'l' */ | |
2334 | NULL, /* 'm' */ | |
2335 | special_sections_n, /* 'n' */ | |
2336 | NULL, /* 'o' */ | |
2337 | special_sections_p, /* 'p' */ | |
2338 | NULL, /* 'q' */ | |
2339 | special_sections_r, /* 'r' */ | |
2340 | special_sections_s, /* 's' */ | |
2341 | special_sections_t, /* 't' */ | |
1b315056 CS |
2342 | NULL, /* 'u' */ |
2343 | NULL, /* 'v' */ | |
2344 | NULL, /* 'w' */ | |
2345 | NULL, /* 'x' */ | |
2346 | NULL, /* 'y' */ | |
2347 | special_sections_z /* 'z' */ | |
7f4d3958 L |
2348 | }; |
2349 | ||
551b43fd AM |
2350 | const struct bfd_elf_special_section * |
2351 | _bfd_elf_get_special_section (const char *name, | |
2352 | const struct bfd_elf_special_section *spec, | |
2353 | unsigned int rela) | |
2f89ff8d L |
2354 | { |
2355 | int i; | |
7f4d3958 | 2356 | int len; |
7f4d3958 | 2357 | |
551b43fd | 2358 | len = strlen (name); |
7f4d3958 | 2359 | |
551b43fd | 2360 | for (i = 0; spec[i].prefix != NULL; i++) |
7dcb9820 AM |
2361 | { |
2362 | int suffix_len; | |
551b43fd | 2363 | int prefix_len = spec[i].prefix_length; |
7dcb9820 AM |
2364 | |
2365 | if (len < prefix_len) | |
2366 | continue; | |
551b43fd | 2367 | if (memcmp (name, spec[i].prefix, prefix_len) != 0) |
7dcb9820 AM |
2368 | continue; |
2369 | ||
551b43fd | 2370 | suffix_len = spec[i].suffix_length; |
7dcb9820 AM |
2371 | if (suffix_len <= 0) |
2372 | { | |
2373 | if (name[prefix_len] != 0) | |
2374 | { | |
2375 | if (suffix_len == 0) | |
2376 | continue; | |
2377 | if (name[prefix_len] != '.' | |
2378 | && (suffix_len == -2 | |
551b43fd | 2379 | || (rela && spec[i].type == SHT_REL))) |
7dcb9820 AM |
2380 | continue; |
2381 | } | |
2382 | } | |
2383 | else | |
2384 | { | |
2385 | if (len < prefix_len + suffix_len) | |
2386 | continue; | |
2387 | if (memcmp (name + len - suffix_len, | |
551b43fd | 2388 | spec[i].prefix + prefix_len, |
7dcb9820 AM |
2389 | suffix_len) != 0) |
2390 | continue; | |
2391 | } | |
551b43fd | 2392 | return &spec[i]; |
7dcb9820 | 2393 | } |
2f89ff8d L |
2394 | |
2395 | return NULL; | |
2396 | } | |
2397 | ||
7dcb9820 | 2398 | const struct bfd_elf_special_section * |
29ef7005 | 2399 | _bfd_elf_get_sec_type_attr (bfd *abfd, asection *sec) |
2f89ff8d | 2400 | { |
551b43fd AM |
2401 | int i; |
2402 | const struct bfd_elf_special_section *spec; | |
29ef7005 | 2403 | const struct elf_backend_data *bed; |
2f89ff8d L |
2404 | |
2405 | /* See if this is one of the special sections. */ | |
551b43fd AM |
2406 | if (sec->name == NULL) |
2407 | return NULL; | |
2f89ff8d | 2408 | |
29ef7005 L |
2409 | bed = get_elf_backend_data (abfd); |
2410 | spec = bed->special_sections; | |
2411 | if (spec) | |
2412 | { | |
2413 | spec = _bfd_elf_get_special_section (sec->name, | |
2414 | bed->special_sections, | |
2415 | sec->use_rela_p); | |
2416 | if (spec != NULL) | |
2417 | return spec; | |
2418 | } | |
2419 | ||
551b43fd AM |
2420 | if (sec->name[0] != '.') |
2421 | return NULL; | |
2f89ff8d | 2422 | |
551b43fd | 2423 | i = sec->name[1] - 'b'; |
1b315056 | 2424 | if (i < 0 || i > 'z' - 'b') |
551b43fd AM |
2425 | return NULL; |
2426 | ||
2427 | spec = special_sections[i]; | |
2f89ff8d | 2428 | |
551b43fd AM |
2429 | if (spec == NULL) |
2430 | return NULL; | |
2431 | ||
2432 | return _bfd_elf_get_special_section (sec->name, spec, sec->use_rela_p); | |
2f89ff8d L |
2433 | } |
2434 | ||
b34976b6 | 2435 | bfd_boolean |
217aa764 | 2436 | _bfd_elf_new_section_hook (bfd *abfd, asection *sec) |
252b5132 RH |
2437 | { |
2438 | struct bfd_elf_section_data *sdata; | |
551b43fd | 2439 | const struct elf_backend_data *bed; |
7dcb9820 | 2440 | const struct bfd_elf_special_section *ssect; |
252b5132 | 2441 | |
f0abc2a1 AM |
2442 | sdata = (struct bfd_elf_section_data *) sec->used_by_bfd; |
2443 | if (sdata == NULL) | |
2444 | { | |
a50b1753 NC |
2445 | sdata = (struct bfd_elf_section_data *) bfd_zalloc (abfd, |
2446 | sizeof (*sdata)); | |
f0abc2a1 AM |
2447 | if (sdata == NULL) |
2448 | return FALSE; | |
217aa764 | 2449 | sec->used_by_bfd = sdata; |
f0abc2a1 | 2450 | } |
bf572ba0 | 2451 | |
551b43fd AM |
2452 | /* Indicate whether or not this section should use RELA relocations. */ |
2453 | bed = get_elf_backend_data (abfd); | |
2454 | sec->use_rela_p = bed->default_use_rela_p; | |
2455 | ||
e843e0f8 L |
2456 | /* When we read a file, we don't need to set ELF section type and |
2457 | flags. They will be overridden in _bfd_elf_make_section_from_shdr | |
2458 | anyway. We will set ELF section type and flags for all linker | |
2459 | created sections. If user specifies BFD section flags, we will | |
2460 | set ELF section type and flags based on BFD section flags in | |
02ecc8e9 L |
2461 | elf_fake_sections. Special handling for .init_array/.fini_array |
2462 | output sections since they may contain .ctors/.dtors input | |
2463 | sections. We don't want _bfd_elf_init_private_section_data to | |
2464 | copy ELF section type from .ctors/.dtors input sections. */ | |
2465 | if (abfd->direction != read_direction | |
3496cb2a | 2466 | || (sec->flags & SEC_LINKER_CREATED) != 0) |
2f89ff8d | 2467 | { |
551b43fd | 2468 | ssect = (*bed->get_sec_type_attr) (abfd, sec); |
02ecc8e9 L |
2469 | if (ssect != NULL |
2470 | && (!sec->flags | |
2471 | || (sec->flags & SEC_LINKER_CREATED) != 0 | |
2472 | || ssect->type == SHT_INIT_ARRAY | |
2473 | || ssect->type == SHT_FINI_ARRAY)) | |
a31501e9 L |
2474 | { |
2475 | elf_section_type (sec) = ssect->type; | |
2476 | elf_section_flags (sec) = ssect->attr; | |
2477 | } | |
2f89ff8d L |
2478 | } |
2479 | ||
f592407e | 2480 | return _bfd_generic_new_section_hook (abfd, sec); |
252b5132 RH |
2481 | } |
2482 | ||
2483 | /* Create a new bfd section from an ELF program header. | |
2484 | ||
2485 | Since program segments have no names, we generate a synthetic name | |
2486 | of the form segment<NUM>, where NUM is generally the index in the | |
2487 | program header table. For segments that are split (see below) we | |
2488 | generate the names segment<NUM>a and segment<NUM>b. | |
2489 | ||
2490 | Note that some program segments may have a file size that is different than | |
2491 | (less than) the memory size. All this means is that at execution the | |
2492 | system must allocate the amount of memory specified by the memory size, | |
2493 | but only initialize it with the first "file size" bytes read from the | |
2494 | file. This would occur for example, with program segments consisting | |
2495 | of combined data+bss. | |
2496 | ||
2497 | To handle the above situation, this routine generates TWO bfd sections | |
2498 | for the single program segment. The first has the length specified by | |
2499 | the file size of the segment, and the second has the length specified | |
2500 | by the difference between the two sizes. In effect, the segment is split | |
d5191d0c | 2501 | into its initialized and uninitialized parts. |
252b5132 RH |
2502 | |
2503 | */ | |
2504 | ||
b34976b6 | 2505 | bfd_boolean |
217aa764 AM |
2506 | _bfd_elf_make_section_from_phdr (bfd *abfd, |
2507 | Elf_Internal_Phdr *hdr, | |
91d6fa6a | 2508 | int hdr_index, |
a50b1753 | 2509 | const char *type_name) |
252b5132 RH |
2510 | { |
2511 | asection *newsect; | |
2512 | char *name; | |
2513 | char namebuf[64]; | |
d4c88bbb | 2514 | size_t len; |
252b5132 RH |
2515 | int split; |
2516 | ||
2517 | split = ((hdr->p_memsz > 0) | |
2518 | && (hdr->p_filesz > 0) | |
2519 | && (hdr->p_memsz > hdr->p_filesz)); | |
d5191d0c AM |
2520 | |
2521 | if (hdr->p_filesz > 0) | |
252b5132 | 2522 | { |
91d6fa6a | 2523 | sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "a" : ""); |
d5191d0c | 2524 | len = strlen (namebuf) + 1; |
a50b1753 | 2525 | name = (char *) bfd_alloc (abfd, len); |
d5191d0c AM |
2526 | if (!name) |
2527 | return FALSE; | |
2528 | memcpy (name, namebuf, len); | |
2529 | newsect = bfd_make_section (abfd, name); | |
2530 | if (newsect == NULL) | |
2531 | return FALSE; | |
2532 | newsect->vma = hdr->p_vaddr; | |
2533 | newsect->lma = hdr->p_paddr; | |
2534 | newsect->size = hdr->p_filesz; | |
2535 | newsect->filepos = hdr->p_offset; | |
2536 | newsect->flags |= SEC_HAS_CONTENTS; | |
2537 | newsect->alignment_power = bfd_log2 (hdr->p_align); | |
2538 | if (hdr->p_type == PT_LOAD) | |
252b5132 | 2539 | { |
d5191d0c AM |
2540 | newsect->flags |= SEC_ALLOC; |
2541 | newsect->flags |= SEC_LOAD; | |
2542 | if (hdr->p_flags & PF_X) | |
2543 | { | |
2544 | /* FIXME: all we known is that it has execute PERMISSION, | |
2545 | may be data. */ | |
2546 | newsect->flags |= SEC_CODE; | |
2547 | } | |
2548 | } | |
2549 | if (!(hdr->p_flags & PF_W)) | |
2550 | { | |
2551 | newsect->flags |= SEC_READONLY; | |
252b5132 | 2552 | } |
252b5132 RH |
2553 | } |
2554 | ||
d5191d0c | 2555 | if (hdr->p_memsz > hdr->p_filesz) |
252b5132 | 2556 | { |
d5191d0c AM |
2557 | bfd_vma align; |
2558 | ||
91d6fa6a | 2559 | sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "b" : ""); |
d4c88bbb | 2560 | len = strlen (namebuf) + 1; |
a50b1753 | 2561 | name = (char *) bfd_alloc (abfd, len); |
252b5132 | 2562 | if (!name) |
b34976b6 | 2563 | return FALSE; |
d4c88bbb | 2564 | memcpy (name, namebuf, len); |
252b5132 RH |
2565 | newsect = bfd_make_section (abfd, name); |
2566 | if (newsect == NULL) | |
b34976b6 | 2567 | return FALSE; |
252b5132 RH |
2568 | newsect->vma = hdr->p_vaddr + hdr->p_filesz; |
2569 | newsect->lma = hdr->p_paddr + hdr->p_filesz; | |
eea6121a | 2570 | newsect->size = hdr->p_memsz - hdr->p_filesz; |
d5191d0c AM |
2571 | newsect->filepos = hdr->p_offset + hdr->p_filesz; |
2572 | align = newsect->vma & -newsect->vma; | |
2573 | if (align == 0 || align > hdr->p_align) | |
2574 | align = hdr->p_align; | |
2575 | newsect->alignment_power = bfd_log2 (align); | |
252b5132 RH |
2576 | if (hdr->p_type == PT_LOAD) |
2577 | { | |
d5191d0c AM |
2578 | /* Hack for gdb. Segments that have not been modified do |
2579 | not have their contents written to a core file, on the | |
2580 | assumption that a debugger can find the contents in the | |
2581 | executable. We flag this case by setting the fake | |
2582 | section size to zero. Note that "real" bss sections will | |
2583 | always have their contents dumped to the core file. */ | |
2584 | if (bfd_get_format (abfd) == bfd_core) | |
2585 | newsect->size = 0; | |
252b5132 RH |
2586 | newsect->flags |= SEC_ALLOC; |
2587 | if (hdr->p_flags & PF_X) | |
2588 | newsect->flags |= SEC_CODE; | |
2589 | } | |
2590 | if (!(hdr->p_flags & PF_W)) | |
2591 | newsect->flags |= SEC_READONLY; | |
2592 | } | |
2593 | ||
b34976b6 | 2594 | return TRUE; |
252b5132 RH |
2595 | } |
2596 | ||
b34976b6 | 2597 | bfd_boolean |
91d6fa6a | 2598 | bfd_section_from_phdr (bfd *abfd, Elf_Internal_Phdr *hdr, int hdr_index) |
20cfcaae | 2599 | { |
9c5bfbb7 | 2600 | const struct elf_backend_data *bed; |
20cfcaae NC |
2601 | |
2602 | switch (hdr->p_type) | |
2603 | { | |
2604 | case PT_NULL: | |
91d6fa6a | 2605 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "null"); |
20cfcaae NC |
2606 | |
2607 | case PT_LOAD: | |
91d6fa6a | 2608 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "load"); |
20cfcaae NC |
2609 | |
2610 | case PT_DYNAMIC: | |
91d6fa6a | 2611 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "dynamic"); |
20cfcaae NC |
2612 | |
2613 | case PT_INTERP: | |
91d6fa6a | 2614 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "interp"); |
20cfcaae NC |
2615 | |
2616 | case PT_NOTE: | |
91d6fa6a | 2617 | if (! _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "note")) |
b34976b6 | 2618 | return FALSE; |
718175fa | 2619 | if (! elf_read_notes (abfd, hdr->p_offset, hdr->p_filesz)) |
b34976b6 AM |
2620 | return FALSE; |
2621 | return TRUE; | |
20cfcaae NC |
2622 | |
2623 | case PT_SHLIB: | |
91d6fa6a | 2624 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "shlib"); |
20cfcaae NC |
2625 | |
2626 | case PT_PHDR: | |
91d6fa6a | 2627 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "phdr"); |
20cfcaae | 2628 | |
811072d8 | 2629 | case PT_GNU_EH_FRAME: |
91d6fa6a | 2630 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, |
811072d8 RM |
2631 | "eh_frame_hdr"); |
2632 | ||
2b05f1b7 | 2633 | case PT_GNU_STACK: |
91d6fa6a | 2634 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "stack"); |
9ee5e499 | 2635 | |
8c37241b | 2636 | case PT_GNU_RELRO: |
91d6fa6a | 2637 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "relro"); |
8c37241b | 2638 | |
20cfcaae | 2639 | default: |
8c1acd09 | 2640 | /* Check for any processor-specific program segment types. */ |
20cfcaae | 2641 | bed = get_elf_backend_data (abfd); |
91d6fa6a | 2642 | return bed->elf_backend_section_from_phdr (abfd, hdr, hdr_index, "proc"); |
20cfcaae NC |
2643 | } |
2644 | } | |
2645 | ||
d4730f92 BS |
2646 | /* Return the REL_HDR for SEC, assuming there is only a single one, either |
2647 | REL or RELA. */ | |
2648 | ||
2649 | Elf_Internal_Shdr * | |
2650 | _bfd_elf_single_rel_hdr (asection *sec) | |
2651 | { | |
2652 | if (elf_section_data (sec)->rel.hdr) | |
2653 | { | |
2654 | BFD_ASSERT (elf_section_data (sec)->rela.hdr == NULL); | |
2655 | return elf_section_data (sec)->rel.hdr; | |
2656 | } | |
2657 | else | |
2658 | return elf_section_data (sec)->rela.hdr; | |
2659 | } | |
2660 | ||
2661 | /* Allocate and initialize a section-header for a new reloc section, | |
2662 | containing relocations against ASECT. It is stored in RELDATA. If | |
2663 | USE_RELA_P is TRUE, we use RELA relocations; otherwise, we use REL | |
2664 | relocations. */ | |
23bc299b | 2665 | |
5d13b3b3 | 2666 | static bfd_boolean |
217aa764 | 2667 | _bfd_elf_init_reloc_shdr (bfd *abfd, |
d4730f92 | 2668 | struct bfd_elf_section_reloc_data *reldata, |
217aa764 AM |
2669 | asection *asect, |
2670 | bfd_boolean use_rela_p) | |
23bc299b | 2671 | { |
d4730f92 | 2672 | Elf_Internal_Shdr *rel_hdr; |
23bc299b | 2673 | char *name; |
9c5bfbb7 | 2674 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
d4730f92 BS |
2675 | bfd_size_type amt; |
2676 | ||
2677 | amt = sizeof (Elf_Internal_Shdr); | |
2678 | BFD_ASSERT (reldata->hdr == NULL); | |
2679 | rel_hdr = bfd_zalloc (abfd, amt); | |
2680 | reldata->hdr = rel_hdr; | |
23bc299b | 2681 | |
d324f6d6 | 2682 | amt = sizeof ".rela" + strlen (asect->name); |
a50b1753 | 2683 | name = (char *) bfd_alloc (abfd, amt); |
23bc299b | 2684 | if (name == NULL) |
b34976b6 | 2685 | return FALSE; |
23bc299b MM |
2686 | sprintf (name, "%s%s", use_rela_p ? ".rela" : ".rel", asect->name); |
2687 | rel_hdr->sh_name = | |
2b0f7ef9 | 2688 | (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), name, |
b34976b6 | 2689 | FALSE); |
23bc299b | 2690 | if (rel_hdr->sh_name == (unsigned int) -1) |
b34976b6 | 2691 | return FALSE; |
23bc299b MM |
2692 | rel_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL; |
2693 | rel_hdr->sh_entsize = (use_rela_p | |
2694 | ? bed->s->sizeof_rela | |
2695 | : bed->s->sizeof_rel); | |
72de5009 | 2696 | rel_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align; |
23bc299b MM |
2697 | rel_hdr->sh_flags = 0; |
2698 | rel_hdr->sh_addr = 0; | |
2699 | rel_hdr->sh_size = 0; | |
2700 | rel_hdr->sh_offset = 0; | |
2701 | ||
b34976b6 | 2702 | return TRUE; |
23bc299b MM |
2703 | } |
2704 | ||
94be91de JB |
2705 | /* Return the default section type based on the passed in section flags. */ |
2706 | ||
2707 | int | |
2708 | bfd_elf_get_default_section_type (flagword flags) | |
2709 | { | |
2710 | if ((flags & SEC_ALLOC) != 0 | |
2e76e85a | 2711 | && (flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0) |
94be91de JB |
2712 | return SHT_NOBITS; |
2713 | return SHT_PROGBITS; | |
2714 | } | |
2715 | ||
d4730f92 BS |
2716 | struct fake_section_arg |
2717 | { | |
2718 | struct bfd_link_info *link_info; | |
2719 | bfd_boolean failed; | |
2720 | }; | |
2721 | ||
252b5132 RH |
2722 | /* Set up an ELF internal section header for a section. */ |
2723 | ||
252b5132 | 2724 | static void |
d4730f92 | 2725 | elf_fake_sections (bfd *abfd, asection *asect, void *fsarg) |
252b5132 | 2726 | { |
d4730f92 | 2727 | struct fake_section_arg *arg = (struct fake_section_arg *)fsarg; |
9c5bfbb7 | 2728 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
d4730f92 | 2729 | struct bfd_elf_section_data *esd = elf_section_data (asect); |
252b5132 | 2730 | Elf_Internal_Shdr *this_hdr; |
0414f35b | 2731 | unsigned int sh_type; |
252b5132 | 2732 | |
d4730f92 | 2733 | if (arg->failed) |
252b5132 RH |
2734 | { |
2735 | /* We already failed; just get out of the bfd_map_over_sections | |
08a40648 | 2736 | loop. */ |
252b5132 RH |
2737 | return; |
2738 | } | |
2739 | ||
d4730f92 | 2740 | this_hdr = &esd->this_hdr; |
252b5132 | 2741 | |
e57b5356 AM |
2742 | this_hdr->sh_name = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), |
2743 | asect->name, FALSE); | |
2744 | if (this_hdr->sh_name == (unsigned int) -1) | |
252b5132 | 2745 | { |
d4730f92 | 2746 | arg->failed = TRUE; |
252b5132 RH |
2747 | return; |
2748 | } | |
2749 | ||
a4d8e49b | 2750 | /* Don't clear sh_flags. Assembler may set additional bits. */ |
252b5132 RH |
2751 | |
2752 | if ((asect->flags & SEC_ALLOC) != 0 | |
2753 | || asect->user_set_vma) | |
2754 | this_hdr->sh_addr = asect->vma; | |
2755 | else | |
2756 | this_hdr->sh_addr = 0; | |
2757 | ||
2758 | this_hdr->sh_offset = 0; | |
eea6121a | 2759 | this_hdr->sh_size = asect->size; |
252b5132 | 2760 | this_hdr->sh_link = 0; |
72de5009 | 2761 | this_hdr->sh_addralign = (bfd_vma) 1 << asect->alignment_power; |
252b5132 RH |
2762 | /* The sh_entsize and sh_info fields may have been set already by |
2763 | copy_private_section_data. */ | |
2764 | ||
2765 | this_hdr->bfd_section = asect; | |
2766 | this_hdr->contents = NULL; | |
2767 | ||
3cddba1e L |
2768 | /* If the section type is unspecified, we set it based on |
2769 | asect->flags. */ | |
98ece1b3 AM |
2770 | if ((asect->flags & SEC_GROUP) != 0) |
2771 | sh_type = SHT_GROUP; | |
98ece1b3 | 2772 | else |
94be91de | 2773 | sh_type = bfd_elf_get_default_section_type (asect->flags); |
98ece1b3 | 2774 | |
3cddba1e | 2775 | if (this_hdr->sh_type == SHT_NULL) |
98ece1b3 AM |
2776 | this_hdr->sh_type = sh_type; |
2777 | else if (this_hdr->sh_type == SHT_NOBITS | |
2778 | && sh_type == SHT_PROGBITS | |
2779 | && (asect->flags & SEC_ALLOC) != 0) | |
3cddba1e | 2780 | { |
98ece1b3 AM |
2781 | /* Warn if we are changing a NOBITS section to PROGBITS, but |
2782 | allow the link to proceed. This can happen when users link | |
2783 | non-bss input sections to bss output sections, or emit data | |
2784 | to a bss output section via a linker script. */ | |
2785 | (*_bfd_error_handler) | |
58f0869b | 2786 | (_("warning: section `%A' type changed to PROGBITS"), asect); |
98ece1b3 | 2787 | this_hdr->sh_type = sh_type; |
3cddba1e L |
2788 | } |
2789 | ||
2f89ff8d | 2790 | switch (this_hdr->sh_type) |
252b5132 | 2791 | { |
2f89ff8d | 2792 | default: |
2f89ff8d L |
2793 | break; |
2794 | ||
2795 | case SHT_STRTAB: | |
2796 | case SHT_INIT_ARRAY: | |
2797 | case SHT_FINI_ARRAY: | |
2798 | case SHT_PREINIT_ARRAY: | |
2799 | case SHT_NOTE: | |
2800 | case SHT_NOBITS: | |
2801 | case SHT_PROGBITS: | |
2802 | break; | |
2803 | ||
2804 | case SHT_HASH: | |
c7ac6ff8 | 2805 | this_hdr->sh_entsize = bed->s->sizeof_hash_entry; |
2f89ff8d | 2806 | break; |
5de3bf90 | 2807 | |
2f89ff8d | 2808 | case SHT_DYNSYM: |
252b5132 | 2809 | this_hdr->sh_entsize = bed->s->sizeof_sym; |
2f89ff8d L |
2810 | break; |
2811 | ||
2812 | case SHT_DYNAMIC: | |
252b5132 | 2813 | this_hdr->sh_entsize = bed->s->sizeof_dyn; |
2f89ff8d L |
2814 | break; |
2815 | ||
2816 | case SHT_RELA: | |
2817 | if (get_elf_backend_data (abfd)->may_use_rela_p) | |
2818 | this_hdr->sh_entsize = bed->s->sizeof_rela; | |
2819 | break; | |
2820 | ||
2821 | case SHT_REL: | |
2822 | if (get_elf_backend_data (abfd)->may_use_rel_p) | |
2823 | this_hdr->sh_entsize = bed->s->sizeof_rel; | |
2824 | break; | |
2825 | ||
2826 | case SHT_GNU_versym: | |
252b5132 | 2827 | this_hdr->sh_entsize = sizeof (Elf_External_Versym); |
2f89ff8d L |
2828 | break; |
2829 | ||
2830 | case SHT_GNU_verdef: | |
252b5132 RH |
2831 | this_hdr->sh_entsize = 0; |
2832 | /* objcopy or strip will copy over sh_info, but may not set | |
08a40648 AM |
2833 | cverdefs. The linker will set cverdefs, but sh_info will be |
2834 | zero. */ | |
252b5132 RH |
2835 | if (this_hdr->sh_info == 0) |
2836 | this_hdr->sh_info = elf_tdata (abfd)->cverdefs; | |
2837 | else | |
2838 | BFD_ASSERT (elf_tdata (abfd)->cverdefs == 0 | |
2839 | || this_hdr->sh_info == elf_tdata (abfd)->cverdefs); | |
2f89ff8d L |
2840 | break; |
2841 | ||
2842 | case SHT_GNU_verneed: | |
252b5132 RH |
2843 | this_hdr->sh_entsize = 0; |
2844 | /* objcopy or strip will copy over sh_info, but may not set | |
08a40648 AM |
2845 | cverrefs. The linker will set cverrefs, but sh_info will be |
2846 | zero. */ | |
252b5132 RH |
2847 | if (this_hdr->sh_info == 0) |
2848 | this_hdr->sh_info = elf_tdata (abfd)->cverrefs; | |
2849 | else | |
2850 | BFD_ASSERT (elf_tdata (abfd)->cverrefs == 0 | |
2851 | || this_hdr->sh_info == elf_tdata (abfd)->cverrefs); | |
2f89ff8d L |
2852 | break; |
2853 | ||
2854 | case SHT_GROUP: | |
1783205a | 2855 | this_hdr->sh_entsize = GRP_ENTRY_SIZE; |
2f89ff8d | 2856 | break; |
fdc90cb4 JJ |
2857 | |
2858 | case SHT_GNU_HASH: | |
2859 | this_hdr->sh_entsize = bed->s->arch_size == 64 ? 0 : 4; | |
2860 | break; | |
dbb410c3 | 2861 | } |
252b5132 RH |
2862 | |
2863 | if ((asect->flags & SEC_ALLOC) != 0) | |
2864 | this_hdr->sh_flags |= SHF_ALLOC; | |
2865 | if ((asect->flags & SEC_READONLY) == 0) | |
2866 | this_hdr->sh_flags |= SHF_WRITE; | |
2867 | if ((asect->flags & SEC_CODE) != 0) | |
2868 | this_hdr->sh_flags |= SHF_EXECINSTR; | |
f5fa8ca2 JJ |
2869 | if ((asect->flags & SEC_MERGE) != 0) |
2870 | { | |
2871 | this_hdr->sh_flags |= SHF_MERGE; | |
2872 | this_hdr->sh_entsize = asect->entsize; | |
2873 | if ((asect->flags & SEC_STRINGS) != 0) | |
2874 | this_hdr->sh_flags |= SHF_STRINGS; | |
2875 | } | |
1126897b | 2876 | if ((asect->flags & SEC_GROUP) == 0 && elf_group_name (asect) != NULL) |
dbb410c3 | 2877 | this_hdr->sh_flags |= SHF_GROUP; |
13ae64f3 | 2878 | if ((asect->flags & SEC_THREAD_LOCAL) != 0) |
704afa60 JJ |
2879 | { |
2880 | this_hdr->sh_flags |= SHF_TLS; | |
3a800eb9 AM |
2881 | if (asect->size == 0 |
2882 | && (asect->flags & SEC_HAS_CONTENTS) == 0) | |
704afa60 | 2883 | { |
3a800eb9 | 2884 | struct bfd_link_order *o = asect->map_tail.link_order; |
b34976b6 | 2885 | |
704afa60 | 2886 | this_hdr->sh_size = 0; |
3a800eb9 AM |
2887 | if (o != NULL) |
2888 | { | |
704afa60 | 2889 | this_hdr->sh_size = o->offset + o->size; |
3a800eb9 AM |
2890 | if (this_hdr->sh_size != 0) |
2891 | this_hdr->sh_type = SHT_NOBITS; | |
2892 | } | |
704afa60 JJ |
2893 | } |
2894 | } | |
18ae9cc1 L |
2895 | if ((asect->flags & (SEC_GROUP | SEC_EXCLUDE)) == SEC_EXCLUDE) |
2896 | this_hdr->sh_flags |= SHF_EXCLUDE; | |
252b5132 | 2897 | |
d4730f92 BS |
2898 | /* If the section has relocs, set up a section header for the |
2899 | SHT_REL[A] section. If two relocation sections are required for | |
2900 | this section, it is up to the processor-specific back-end to | |
2901 | create the other. */ | |
2902 | if ((asect->flags & SEC_RELOC) != 0) | |
2903 | { | |
2904 | /* When doing a relocatable link, create both REL and RELA sections if | |
2905 | needed. */ | |
2906 | if (arg->link_info | |
2907 | /* Do the normal setup if we wouldn't create any sections here. */ | |
2908 | && esd->rel.count + esd->rela.count > 0 | |
2909 | && (arg->link_info->relocatable || arg->link_info->emitrelocations)) | |
2910 | { | |
2911 | if (esd->rel.count && esd->rel.hdr == NULL | |
2912 | && !_bfd_elf_init_reloc_shdr (abfd, &esd->rel, asect, FALSE)) | |
2913 | { | |
2914 | arg->failed = TRUE; | |
2915 | return; | |
2916 | } | |
2917 | if (esd->rela.count && esd->rela.hdr == NULL | |
2918 | && !_bfd_elf_init_reloc_shdr (abfd, &esd->rela, asect, TRUE)) | |
2919 | { | |
2920 | arg->failed = TRUE; | |
2921 | return; | |
2922 | } | |
2923 | } | |
2924 | else if (!_bfd_elf_init_reloc_shdr (abfd, | |
2925 | (asect->use_rela_p | |
2926 | ? &esd->rela : &esd->rel), | |
2927 | asect, | |
2928 | asect->use_rela_p)) | |
2929 | arg->failed = TRUE; | |
2930 | } | |
2931 | ||
252b5132 | 2932 | /* Check for processor-specific section types. */ |
0414f35b | 2933 | sh_type = this_hdr->sh_type; |
e1fddb6b AO |
2934 | if (bed->elf_backend_fake_sections |
2935 | && !(*bed->elf_backend_fake_sections) (abfd, this_hdr, asect)) | |
d4730f92 | 2936 | arg->failed = TRUE; |
252b5132 | 2937 | |
42bb2e33 | 2938 | if (sh_type == SHT_NOBITS && asect->size != 0) |
0414f35b AM |
2939 | { |
2940 | /* Don't change the header type from NOBITS if we are being | |
42bb2e33 | 2941 | called for objcopy --only-keep-debug. */ |
0414f35b AM |
2942 | this_hdr->sh_type = sh_type; |
2943 | } | |
252b5132 RH |
2944 | } |
2945 | ||
bcacc0f5 AM |
2946 | /* Fill in the contents of a SHT_GROUP section. Called from |
2947 | _bfd_elf_compute_section_file_positions for gas, objcopy, and | |
2948 | when ELF targets use the generic linker, ld. Called for ld -r | |
2949 | from bfd_elf_final_link. */ | |
dbb410c3 | 2950 | |
1126897b | 2951 | void |
217aa764 | 2952 | bfd_elf_set_group_contents (bfd *abfd, asection *sec, void *failedptrarg) |
dbb410c3 | 2953 | { |
a50b1753 | 2954 | bfd_boolean *failedptr = (bfd_boolean *) failedptrarg; |
9dce4196 | 2955 | asection *elt, *first; |
dbb410c3 | 2956 | unsigned char *loc; |
b34976b6 | 2957 | bfd_boolean gas; |
dbb410c3 | 2958 | |
7e4111ad L |
2959 | /* Ignore linker created group section. See elfNN_ia64_object_p in |
2960 | elfxx-ia64.c. */ | |
2961 | if (((sec->flags & (SEC_GROUP | SEC_LINKER_CREATED)) != SEC_GROUP) | |
dbb410c3 AM |
2962 | || *failedptr) |
2963 | return; | |
2964 | ||
bcacc0f5 AM |
2965 | if (elf_section_data (sec)->this_hdr.sh_info == 0) |
2966 | { | |
2967 | unsigned long symindx = 0; | |
2968 | ||
2969 | /* elf_group_id will have been set up by objcopy and the | |
2970 | generic linker. */ | |
2971 | if (elf_group_id (sec) != NULL) | |
2972 | symindx = elf_group_id (sec)->udata.i; | |
1126897b | 2973 | |
bcacc0f5 AM |
2974 | if (symindx == 0) |
2975 | { | |
2976 | /* If called from the assembler, swap_out_syms will have set up | |
2977 | elf_section_syms. */ | |
2978 | BFD_ASSERT (elf_section_syms (abfd) != NULL); | |
2979 | symindx = elf_section_syms (abfd)[sec->index]->udata.i; | |
2980 | } | |
2981 | elf_section_data (sec)->this_hdr.sh_info = symindx; | |
2982 | } | |
2983 | else if (elf_section_data (sec)->this_hdr.sh_info == (unsigned int) -2) | |
1126897b | 2984 | { |
bcacc0f5 AM |
2985 | /* The ELF backend linker sets sh_info to -2 when the group |
2986 | signature symbol is global, and thus the index can't be | |
2987 | set until all local symbols are output. */ | |
2988 | asection *igroup = elf_sec_group (elf_next_in_group (sec)); | |
2989 | struct bfd_elf_section_data *sec_data = elf_section_data (igroup); | |
2990 | unsigned long symndx = sec_data->this_hdr.sh_info; | |
2991 | unsigned long extsymoff = 0; | |
2992 | struct elf_link_hash_entry *h; | |
2993 | ||
2994 | if (!elf_bad_symtab (igroup->owner)) | |
2995 | { | |
2996 | Elf_Internal_Shdr *symtab_hdr; | |
2997 | ||
2998 | symtab_hdr = &elf_tdata (igroup->owner)->symtab_hdr; | |
2999 | extsymoff = symtab_hdr->sh_info; | |
3000 | } | |
3001 | h = elf_sym_hashes (igroup->owner)[symndx - extsymoff]; | |
3002 | while (h->root.type == bfd_link_hash_indirect | |
3003 | || h->root.type == bfd_link_hash_warning) | |
3004 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
3005 | ||
3006 | elf_section_data (sec)->this_hdr.sh_info = h->indx; | |
1126897b | 3007 | } |
dbb410c3 | 3008 | |
1126897b | 3009 | /* The contents won't be allocated for "ld -r" or objcopy. */ |
b34976b6 | 3010 | gas = TRUE; |
dbb410c3 AM |
3011 | if (sec->contents == NULL) |
3012 | { | |
b34976b6 | 3013 | gas = FALSE; |
a50b1753 | 3014 | sec->contents = (unsigned char *) bfd_alloc (abfd, sec->size); |
9dce4196 AM |
3015 | |
3016 | /* Arrange for the section to be written out. */ | |
3017 | elf_section_data (sec)->this_hdr.contents = sec->contents; | |
dbb410c3 AM |
3018 | if (sec->contents == NULL) |
3019 | { | |
b34976b6 | 3020 | *failedptr = TRUE; |
dbb410c3 AM |
3021 | return; |
3022 | } | |
3023 | } | |
3024 | ||
eea6121a | 3025 | loc = sec->contents + sec->size; |
dbb410c3 | 3026 | |
9dce4196 AM |
3027 | /* Get the pointer to the first section in the group that gas |
3028 | squirreled away here. objcopy arranges for this to be set to the | |
3029 | start of the input section group. */ | |
3030 | first = elt = elf_next_in_group (sec); | |
dbb410c3 AM |
3031 | |
3032 | /* First element is a flag word. Rest of section is elf section | |
3033 | indices for all the sections of the group. Write them backwards | |
3034 | just to keep the group in the same order as given in .section | |
3035 | directives, not that it matters. */ | |
3036 | while (elt != NULL) | |
3037 | { | |
9dce4196 | 3038 | asection *s; |
9dce4196 | 3039 | |
9dce4196 | 3040 | s = elt; |
415f38a6 AM |
3041 | if (!gas) |
3042 | s = s->output_section; | |
3043 | if (s != NULL | |
3044 | && !bfd_is_abs_section (s)) | |
01e1a5bc | 3045 | { |
415f38a6 AM |
3046 | unsigned int idx = elf_section_data (s)->this_idx; |
3047 | ||
01e1a5bc | 3048 | loc -= 4; |
01e1a5bc NC |
3049 | H_PUT_32 (abfd, idx, loc); |
3050 | } | |
945906ff | 3051 | elt = elf_next_in_group (elt); |
9dce4196 AM |
3052 | if (elt == first) |
3053 | break; | |
dbb410c3 AM |
3054 | } |
3055 | ||
3d7f7666 | 3056 | if ((loc -= 4) != sec->contents) |
9dce4196 | 3057 | abort (); |
dbb410c3 | 3058 | |
9dce4196 | 3059 | H_PUT_32 (abfd, sec->flags & SEC_LINK_ONCE ? GRP_COMDAT : 0, loc); |
dbb410c3 AM |
3060 | } |
3061 | ||
252b5132 RH |
3062 | /* Assign all ELF section numbers. The dummy first section is handled here |
3063 | too. The link/info pointers for the standard section types are filled | |
3064 | in here too, while we're at it. */ | |
3065 | ||
b34976b6 | 3066 | static bfd_boolean |
da9f89d4 | 3067 | assign_section_numbers (bfd *abfd, struct bfd_link_info *link_info) |
252b5132 RH |
3068 | { |
3069 | struct elf_obj_tdata *t = elf_tdata (abfd); | |
3070 | asection *sec; | |
2b0f7ef9 | 3071 | unsigned int section_number, secn; |
252b5132 | 3072 | Elf_Internal_Shdr **i_shdrp; |
47cc2cf5 | 3073 | struct bfd_elf_section_data *d; |
3516e984 | 3074 | bfd_boolean need_symtab; |
252b5132 RH |
3075 | |
3076 | section_number = 1; | |
3077 | ||
2b0f7ef9 JJ |
3078 | _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd)); |
3079 | ||
da9f89d4 L |
3080 | /* SHT_GROUP sections are in relocatable files only. */ |
3081 | if (link_info == NULL || link_info->relocatable) | |
252b5132 | 3082 | { |
da9f89d4 | 3083 | /* Put SHT_GROUP sections first. */ |
04dd1667 | 3084 | for (sec = abfd->sections; sec != NULL; sec = sec->next) |
47cc2cf5 | 3085 | { |
5daa8fe7 | 3086 | d = elf_section_data (sec); |
da9f89d4 L |
3087 | |
3088 | if (d->this_hdr.sh_type == SHT_GROUP) | |
08a40648 | 3089 | { |
5daa8fe7 | 3090 | if (sec->flags & SEC_LINKER_CREATED) |
da9f89d4 L |
3091 | { |
3092 | /* Remove the linker created SHT_GROUP sections. */ | |
5daa8fe7 | 3093 | bfd_section_list_remove (abfd, sec); |
da9f89d4 | 3094 | abfd->section_count--; |
da9f89d4 | 3095 | } |
08a40648 | 3096 | else |
4fbb74a6 | 3097 | d->this_idx = section_number++; |
da9f89d4 | 3098 | } |
47cc2cf5 PB |
3099 | } |
3100 | } | |
3101 | ||
3102 | for (sec = abfd->sections; sec; sec = sec->next) | |
3103 | { | |
3104 | d = elf_section_data (sec); | |
3105 | ||
3106 | if (d->this_hdr.sh_type != SHT_GROUP) | |
4fbb74a6 | 3107 | d->this_idx = section_number++; |
2b0f7ef9 | 3108 | _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->this_hdr.sh_name); |
d4730f92 | 3109 | if (d->rel.hdr) |
2b0f7ef9 | 3110 | { |
d4730f92 BS |
3111 | d->rel.idx = section_number++; |
3112 | _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel.hdr->sh_name); | |
2b0f7ef9 | 3113 | } |
d4730f92 BS |
3114 | else |
3115 | d->rel.idx = 0; | |
23bc299b | 3116 | |
d4730f92 | 3117 | if (d->rela.hdr) |
2b0f7ef9 | 3118 | { |
d4730f92 BS |
3119 | d->rela.idx = section_number++; |
3120 | _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rela.hdr->sh_name); | |
2b0f7ef9 | 3121 | } |
23bc299b | 3122 | else |
d4730f92 | 3123 | d->rela.idx = 0; |
252b5132 RH |
3124 | } |
3125 | ||
12bd6957 | 3126 | elf_shstrtab_sec (abfd) = section_number++; |
2b0f7ef9 | 3127 | _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->shstrtab_hdr.sh_name); |
12bd6957 | 3128 | elf_elfheader (abfd)->e_shstrndx = elf_shstrtab_sec (abfd); |
252b5132 | 3129 | |
3516e984 L |
3130 | need_symtab = (bfd_get_symcount (abfd) > 0 |
3131 | || (link_info == NULL | |
3132 | && ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC)) | |
3133 | == HAS_RELOC))); | |
3134 | if (need_symtab) | |
252b5132 | 3135 | { |
12bd6957 | 3136 | elf_onesymtab (abfd) = section_number++; |
2b0f7ef9 | 3137 | _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->symtab_hdr.sh_name); |
4fbb74a6 | 3138 | if (section_number > ((SHN_LORESERVE - 2) & 0xFFFF)) |
9ad5cbcf | 3139 | { |
12bd6957 | 3140 | elf_symtab_shndx (abfd) = section_number++; |
9ad5cbcf AM |
3141 | t->symtab_shndx_hdr.sh_name |
3142 | = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), | |
b34976b6 | 3143 | ".symtab_shndx", FALSE); |
9ad5cbcf | 3144 | if (t->symtab_shndx_hdr.sh_name == (unsigned int) -1) |
b34976b6 | 3145 | return FALSE; |
9ad5cbcf | 3146 | } |
12bd6957 | 3147 | elf_strtab_sec (abfd) = section_number++; |
2b0f7ef9 | 3148 | _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->strtab_hdr.sh_name); |
252b5132 RH |
3149 | } |
3150 | ||
1c52a645 L |
3151 | if (section_number >= SHN_LORESERVE) |
3152 | { | |
3153 | _bfd_error_handler (_("%B: too many sections: %u"), | |
3154 | abfd, section_number); | |
3155 | return FALSE; | |
3156 | } | |
3157 | ||
2b0f7ef9 JJ |
3158 | _bfd_elf_strtab_finalize (elf_shstrtab (abfd)); |
3159 | t->shstrtab_hdr.sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd)); | |
9ad5cbcf AM |
3160 | |
3161 | elf_numsections (abfd) = section_number; | |
252b5132 RH |
3162 | elf_elfheader (abfd)->e_shnum = section_number; |
3163 | ||
3164 | /* Set up the list of section header pointers, in agreement with the | |
3165 | indices. */ | |
a50b1753 NC |
3166 | i_shdrp = (Elf_Internal_Shdr **) bfd_zalloc2 (abfd, section_number, |
3167 | sizeof (Elf_Internal_Shdr *)); | |
252b5132 | 3168 | if (i_shdrp == NULL) |
b34976b6 | 3169 | return FALSE; |
252b5132 | 3170 | |
a50b1753 NC |
3171 | i_shdrp[0] = (Elf_Internal_Shdr *) bfd_zalloc (abfd, |
3172 | sizeof (Elf_Internal_Shdr)); | |
252b5132 RH |
3173 | if (i_shdrp[0] == NULL) |
3174 | { | |
3175 | bfd_release (abfd, i_shdrp); | |
b34976b6 | 3176 | return FALSE; |
252b5132 | 3177 | } |
252b5132 RH |
3178 | |
3179 | elf_elfsections (abfd) = i_shdrp; | |
3180 | ||
12bd6957 | 3181 | i_shdrp[elf_shstrtab_sec (abfd)] = &t->shstrtab_hdr; |
3516e984 | 3182 | if (need_symtab) |
252b5132 | 3183 | { |
12bd6957 | 3184 | i_shdrp[elf_onesymtab (abfd)] = &t->symtab_hdr; |
4fbb74a6 | 3185 | if (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF)) |
9ad5cbcf | 3186 | { |
12bd6957 AM |
3187 | i_shdrp[elf_symtab_shndx (abfd)] = &t->symtab_shndx_hdr; |
3188 | t->symtab_shndx_hdr.sh_link = elf_onesymtab (abfd); | |
9ad5cbcf | 3189 | } |
12bd6957 AM |
3190 | i_shdrp[elf_strtab_sec (abfd)] = &t->strtab_hdr; |
3191 | t->symtab_hdr.sh_link = elf_strtab_sec (abfd); | |
252b5132 | 3192 | } |
38ce5b11 | 3193 | |
252b5132 RH |
3194 | for (sec = abfd->sections; sec; sec = sec->next) |
3195 | { | |
252b5132 RH |
3196 | asection *s; |
3197 | const char *name; | |
3198 | ||
91d6fa6a NC |
3199 | d = elf_section_data (sec); |
3200 | ||
252b5132 | 3201 | i_shdrp[d->this_idx] = &d->this_hdr; |
d4730f92 BS |
3202 | if (d->rel.idx != 0) |
3203 | i_shdrp[d->rel.idx] = d->rel.hdr; | |
3204 | if (d->rela.idx != 0) | |
3205 | i_shdrp[d->rela.idx] = d->rela.hdr; | |
252b5132 RH |
3206 | |
3207 | /* Fill in the sh_link and sh_info fields while we're at it. */ | |
3208 | ||
3209 | /* sh_link of a reloc section is the section index of the symbol | |
3210 | table. sh_info is the section index of the section to which | |
3211 | the relocation entries apply. */ | |
d4730f92 | 3212 | if (d->rel.idx != 0) |
252b5132 | 3213 | { |
12bd6957 | 3214 | d->rel.hdr->sh_link = elf_onesymtab (abfd); |
d4730f92 | 3215 | d->rel.hdr->sh_info = d->this_idx; |
9ef5d938 | 3216 | d->rel.hdr->sh_flags |= SHF_INFO_LINK; |
252b5132 | 3217 | } |
d4730f92 | 3218 | if (d->rela.idx != 0) |
23bc299b | 3219 | { |
12bd6957 | 3220 | d->rela.hdr->sh_link = elf_onesymtab (abfd); |
d4730f92 | 3221 | d->rela.hdr->sh_info = d->this_idx; |
9ef5d938 | 3222 | d->rela.hdr->sh_flags |= SHF_INFO_LINK; |
23bc299b | 3223 | } |
252b5132 | 3224 | |
38ce5b11 L |
3225 | /* We need to set up sh_link for SHF_LINK_ORDER. */ |
3226 | if ((d->this_hdr.sh_flags & SHF_LINK_ORDER) != 0) | |
3227 | { | |
3228 | s = elf_linked_to_section (sec); | |
3229 | if (s) | |
38ce5b11 | 3230 | { |
f2876037 | 3231 | /* elf_linked_to_section points to the input section. */ |
ccd2ec6a | 3232 | if (link_info != NULL) |
38ce5b11 | 3233 | { |
f2876037 | 3234 | /* Check discarded linkonce section. */ |
dbaa2011 | 3235 | if (discarded_section (s)) |
38ce5b11 | 3236 | { |
ccd2ec6a L |
3237 | asection *kept; |
3238 | (*_bfd_error_handler) | |
3239 | (_("%B: sh_link of section `%A' points to discarded section `%A' of `%B'"), | |
3240 | abfd, d->this_hdr.bfd_section, | |
3241 | s, s->owner); | |
3242 | /* Point to the kept section if it has the same | |
3243 | size as the discarded one. */ | |
c0f00686 | 3244 | kept = _bfd_elf_check_kept_section (s, link_info); |
ccd2ec6a | 3245 | if (kept == NULL) |
185d09ad | 3246 | { |
ccd2ec6a L |
3247 | bfd_set_error (bfd_error_bad_value); |
3248 | return FALSE; | |
185d09ad | 3249 | } |
ccd2ec6a | 3250 | s = kept; |
38ce5b11 | 3251 | } |
e424ecc8 | 3252 | |
ccd2ec6a L |
3253 | s = s->output_section; |
3254 | BFD_ASSERT (s != NULL); | |
38ce5b11 | 3255 | } |
f2876037 L |
3256 | else |
3257 | { | |
3258 | /* Handle objcopy. */ | |
3259 | if (s->output_section == NULL) | |
3260 | { | |
3261 | (*_bfd_error_handler) | |
3262 | (_("%B: sh_link of section `%A' points to removed section `%A' of `%B'"), | |
3263 | abfd, d->this_hdr.bfd_section, s, s->owner); | |
3264 | bfd_set_error (bfd_error_bad_value); | |
3265 | return FALSE; | |
3266 | } | |
3267 | s = s->output_section; | |
3268 | } | |
ccd2ec6a L |
3269 | d->this_hdr.sh_link = elf_section_data (s)->this_idx; |
3270 | } | |
3271 | else | |
3272 | { | |
3273 | /* PR 290: | |
3274 | The Intel C compiler generates SHT_IA_64_UNWIND with | |
3275 | SHF_LINK_ORDER. But it doesn't set the sh_link or | |
3276 | sh_info fields. Hence we could get the situation | |
08a40648 | 3277 | where s is NULL. */ |
ccd2ec6a L |
3278 | const struct elf_backend_data *bed |
3279 | = get_elf_backend_data (abfd); | |
3280 | if (bed->link_order_error_handler) | |
3281 | bed->link_order_error_handler | |
3282 | (_("%B: warning: sh_link not set for section `%A'"), | |
3283 | abfd, sec); | |
38ce5b11 L |
3284 | } |
3285 | } | |
3286 | ||
252b5132 RH |
3287 | switch (d->this_hdr.sh_type) |
3288 | { | |
3289 | case SHT_REL: | |
3290 | case SHT_RELA: | |
3291 | /* A reloc section which we are treating as a normal BFD | |
3292 | section. sh_link is the section index of the symbol | |
3293 | table. sh_info is the section index of the section to | |
3294 | which the relocation entries apply. We assume that an | |
3295 | allocated reloc section uses the dynamic symbol table. | |
3296 | FIXME: How can we be sure? */ | |
3297 | s = bfd_get_section_by_name (abfd, ".dynsym"); | |
3298 | if (s != NULL) | |
3299 | d->this_hdr.sh_link = elf_section_data (s)->this_idx; | |
3300 | ||
3301 | /* We look up the section the relocs apply to by name. */ | |
3302 | name = sec->name; | |
3303 | if (d->this_hdr.sh_type == SHT_REL) | |
3304 | name += 4; | |
3305 | else | |
3306 | name += 5; | |
3307 | s = bfd_get_section_by_name (abfd, name); | |
3308 | if (s != NULL) | |
9ef5d938 L |
3309 | { |
3310 | d->this_hdr.sh_info = elf_section_data (s)->this_idx; | |
3311 | d->this_hdr.sh_flags |= SHF_INFO_LINK; | |
3312 | } | |
252b5132 RH |
3313 | break; |
3314 | ||
3315 | case SHT_STRTAB: | |
3316 | /* We assume that a section named .stab*str is a stabs | |
3317 | string section. We look for a section with the same name | |
3318 | but without the trailing ``str'', and set its sh_link | |
3319 | field to point to this section. */ | |
0112cd26 | 3320 | if (CONST_STRNEQ (sec->name, ".stab") |
252b5132 RH |
3321 | && strcmp (sec->name + strlen (sec->name) - 3, "str") == 0) |
3322 | { | |
3323 | size_t len; | |
3324 | char *alc; | |
3325 | ||
3326 | len = strlen (sec->name); | |
a50b1753 | 3327 | alc = (char *) bfd_malloc (len - 2); |
252b5132 | 3328 | if (alc == NULL) |
b34976b6 | 3329 | return FALSE; |
d4c88bbb | 3330 | memcpy (alc, sec->name, len - 3); |
252b5132 RH |
3331 | alc[len - 3] = '\0'; |
3332 | s = bfd_get_section_by_name (abfd, alc); | |
3333 | free (alc); | |
3334 | if (s != NULL) | |
3335 | { | |
3336 | elf_section_data (s)->this_hdr.sh_link = d->this_idx; | |
3337 | ||
3338 | /* This is a .stab section. */ | |
0594c12d AM |
3339 | if (elf_section_data (s)->this_hdr.sh_entsize == 0) |
3340 | elf_section_data (s)->this_hdr.sh_entsize | |
3341 | = 4 + 2 * bfd_get_arch_size (abfd) / 8; | |
252b5132 RH |
3342 | } |
3343 | } | |
3344 | break; | |
3345 | ||
3346 | case SHT_DYNAMIC: | |
3347 | case SHT_DYNSYM: | |
3348 | case SHT_GNU_verneed: | |
3349 | case SHT_GNU_verdef: | |
3350 | /* sh_link is the section header index of the string table | |
3351 | used for the dynamic entries, or the symbol table, or the | |
3352 | version strings. */ | |
3353 | s = bfd_get_section_by_name (abfd, ".dynstr"); | |
3354 | if (s != NULL) | |
3355 | d->this_hdr.sh_link = elf_section_data (s)->this_idx; | |
3356 | break; | |
3357 | ||
7f1204bb JJ |
3358 | case SHT_GNU_LIBLIST: |
3359 | /* sh_link is the section header index of the prelink library | |
08a40648 AM |
3360 | list used for the dynamic entries, or the symbol table, or |
3361 | the version strings. */ | |
7f1204bb JJ |
3362 | s = bfd_get_section_by_name (abfd, (sec->flags & SEC_ALLOC) |
3363 | ? ".dynstr" : ".gnu.libstr"); | |
3364 | if (s != NULL) | |
3365 | d->this_hdr.sh_link = elf_section_data (s)->this_idx; | |
3366 | break; | |
3367 | ||
252b5132 | 3368 | case SHT_HASH: |
fdc90cb4 | 3369 | case SHT_GNU_HASH: |
252b5132 RH |
3370 | case SHT_GNU_versym: |
3371 | /* sh_link is the section header index of the symbol table | |
3372 | this hash table or version table is for. */ | |
3373 | s = bfd_get_section_by_name (abfd, ".dynsym"); | |
3374 | if (s != NULL) | |
3375 | d->this_hdr.sh_link = elf_section_data (s)->this_idx; | |
3376 | break; | |
dbb410c3 AM |
3377 | |
3378 | case SHT_GROUP: | |
12bd6957 | 3379 | d->this_hdr.sh_link = elf_onesymtab (abfd); |
252b5132 RH |
3380 | } |
3381 | } | |
3382 | ||
2b0f7ef9 | 3383 | for (secn = 1; secn < section_number; ++secn) |
9ad5cbcf AM |
3384 | if (i_shdrp[secn] == NULL) |
3385 | i_shdrp[secn] = i_shdrp[0]; | |
3386 | else | |
3387 | i_shdrp[secn]->sh_name = _bfd_elf_strtab_offset (elf_shstrtab (abfd), | |
3388 | i_shdrp[secn]->sh_name); | |
b34976b6 | 3389 | return TRUE; |
252b5132 RH |
3390 | } |
3391 | ||
5372391b | 3392 | static bfd_boolean |
217aa764 | 3393 | sym_is_global (bfd *abfd, asymbol *sym) |
252b5132 RH |
3394 | { |
3395 | /* If the backend has a special mapping, use it. */ | |
9c5bfbb7 | 3396 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
217aa764 AM |
3397 | if (bed->elf_backend_sym_is_global) |
3398 | return (*bed->elf_backend_sym_is_global) (abfd, sym); | |
252b5132 | 3399 | |
e47bf690 | 3400 | return ((sym->flags & (BSF_GLOBAL | BSF_WEAK | BSF_GNU_UNIQUE)) != 0 |
252b5132 RH |
3401 | || bfd_is_und_section (bfd_get_section (sym)) |
3402 | || bfd_is_com_section (bfd_get_section (sym))); | |
3403 | } | |
3404 | ||
5372391b | 3405 | /* Don't output section symbols for sections that are not going to be |
c6d8cab4 | 3406 | output, that are duplicates or there is no BFD section. */ |
5372391b AM |
3407 | |
3408 | static bfd_boolean | |
3409 | ignore_section_sym (bfd *abfd, asymbol *sym) | |
3410 | { | |
c6d8cab4 L |
3411 | elf_symbol_type *type_ptr; |
3412 | ||
3413 | if ((sym->flags & BSF_SECTION_SYM) == 0) | |
3414 | return FALSE; | |
3415 | ||
3416 | type_ptr = elf_symbol_from (abfd, sym); | |
3417 | return ((type_ptr != NULL | |
3418 | && type_ptr->internal_elf_sym.st_shndx != 0 | |
3419 | && bfd_is_abs_section (sym->section)) | |
3420 | || !(sym->section->owner == abfd | |
0f0a5e58 | 3421 | || (sym->section->output_section->owner == abfd |
2633a79c AM |
3422 | && sym->section->output_offset == 0) |
3423 | || bfd_is_abs_section (sym->section))); | |
5372391b AM |
3424 | } |
3425 | ||
2633a79c AM |
3426 | /* Map symbol from it's internal number to the external number, moving |
3427 | all local symbols to be at the head of the list. */ | |
3428 | ||
b34976b6 | 3429 | static bfd_boolean |
12bd6957 | 3430 | elf_map_symbols (bfd *abfd, unsigned int *pnum_locals) |
252b5132 | 3431 | { |
dc810e39 | 3432 | unsigned int symcount = bfd_get_symcount (abfd); |
252b5132 RH |
3433 | asymbol **syms = bfd_get_outsymbols (abfd); |
3434 | asymbol **sect_syms; | |
dc810e39 AM |
3435 | unsigned int num_locals = 0; |
3436 | unsigned int num_globals = 0; | |
3437 | unsigned int num_locals2 = 0; | |
3438 | unsigned int num_globals2 = 0; | |
252b5132 | 3439 | int max_index = 0; |
dc810e39 | 3440 | unsigned int idx; |
252b5132 RH |
3441 | asection *asect; |
3442 | asymbol **new_syms; | |
252b5132 RH |
3443 | |
3444 | #ifdef DEBUG | |
3445 | fprintf (stderr, "elf_map_symbols\n"); | |
3446 | fflush (stderr); | |
3447 | #endif | |
3448 | ||
252b5132 RH |
3449 | for (asect = abfd->sections; asect; asect = asect->next) |
3450 | { | |
3451 | if (max_index < asect->index) | |
3452 | max_index = asect->index; | |
3453 | } | |
3454 | ||
3455 | max_index++; | |
a50b1753 | 3456 | sect_syms = (asymbol **) bfd_zalloc2 (abfd, max_index, sizeof (asymbol *)); |
252b5132 | 3457 | if (sect_syms == NULL) |
b34976b6 | 3458 | return FALSE; |
252b5132 | 3459 | elf_section_syms (abfd) = sect_syms; |
4e89ac30 | 3460 | elf_num_section_syms (abfd) = max_index; |
252b5132 | 3461 | |
079e9a2f AM |
3462 | /* Init sect_syms entries for any section symbols we have already |
3463 | decided to output. */ | |
252b5132 RH |
3464 | for (idx = 0; idx < symcount; idx++) |
3465 | { | |
dc810e39 | 3466 | asymbol *sym = syms[idx]; |
c044fabd | 3467 | |
252b5132 | 3468 | if ((sym->flags & BSF_SECTION_SYM) != 0 |
0f0a5e58 | 3469 | && sym->value == 0 |
2633a79c AM |
3470 | && !ignore_section_sym (abfd, sym) |
3471 | && !bfd_is_abs_section (sym->section)) | |
252b5132 | 3472 | { |
5372391b | 3473 | asection *sec = sym->section; |
252b5132 | 3474 | |
5372391b AM |
3475 | if (sec->owner != abfd) |
3476 | sec = sec->output_section; | |
252b5132 | 3477 | |
5372391b | 3478 | sect_syms[sec->index] = syms[idx]; |
252b5132 RH |
3479 | } |
3480 | } | |
3481 | ||
252b5132 RH |
3482 | /* Classify all of the symbols. */ |
3483 | for (idx = 0; idx < symcount; idx++) | |
3484 | { | |
2633a79c | 3485 | if (sym_is_global (abfd, syms[idx])) |
252b5132 | 3486 | num_globals++; |
2633a79c AM |
3487 | else if (!ignore_section_sym (abfd, syms[idx])) |
3488 | num_locals++; | |
252b5132 | 3489 | } |
079e9a2f | 3490 | |
5372391b | 3491 | /* We will be adding a section symbol for each normal BFD section. Most |
079e9a2f AM |
3492 | sections will already have a section symbol in outsymbols, but |
3493 | eg. SHT_GROUP sections will not, and we need the section symbol mapped | |
3494 | at least in that case. */ | |
252b5132 RH |
3495 | for (asect = abfd->sections; asect; asect = asect->next) |
3496 | { | |
079e9a2f | 3497 | if (sect_syms[asect->index] == NULL) |
252b5132 | 3498 | { |
079e9a2f | 3499 | if (!sym_is_global (abfd, asect->symbol)) |
252b5132 RH |
3500 | num_locals++; |
3501 | else | |
3502 | num_globals++; | |
252b5132 RH |
3503 | } |
3504 | } | |
3505 | ||
3506 | /* Now sort the symbols so the local symbols are first. */ | |
a50b1753 NC |
3507 | new_syms = (asymbol **) bfd_alloc2 (abfd, num_locals + num_globals, |
3508 | sizeof (asymbol *)); | |
dc810e39 | 3509 | |
252b5132 | 3510 | if (new_syms == NULL) |
b34976b6 | 3511 | return FALSE; |
252b5132 RH |
3512 | |
3513 | for (idx = 0; idx < symcount; idx++) | |
3514 | { | |
3515 | asymbol *sym = syms[idx]; | |
dc810e39 | 3516 | unsigned int i; |
252b5132 | 3517 | |
2633a79c AM |
3518 | if (sym_is_global (abfd, sym)) |
3519 | i = num_locals + num_globals2++; | |
3520 | else if (!ignore_section_sym (abfd, sym)) | |
252b5132 RH |
3521 | i = num_locals2++; |
3522 | else | |
2633a79c | 3523 | continue; |
252b5132 RH |
3524 | new_syms[i] = sym; |
3525 | sym->udata.i = i + 1; | |
3526 | } | |
3527 | for (asect = abfd->sections; asect; asect = asect->next) | |
3528 | { | |
079e9a2f | 3529 | if (sect_syms[asect->index] == NULL) |
252b5132 | 3530 | { |
079e9a2f | 3531 | asymbol *sym = asect->symbol; |
dc810e39 | 3532 | unsigned int i; |
252b5132 | 3533 | |
079e9a2f | 3534 | sect_syms[asect->index] = sym; |
252b5132 RH |
3535 | if (!sym_is_global (abfd, sym)) |
3536 | i = num_locals2++; | |
3537 | else | |
3538 | i = num_locals + num_globals2++; | |
3539 | new_syms[i] = sym; | |
3540 | sym->udata.i = i + 1; | |
3541 | } | |
3542 | } | |
3543 | ||
3544 | bfd_set_symtab (abfd, new_syms, num_locals + num_globals); | |
3545 | ||
12bd6957 | 3546 | *pnum_locals = num_locals; |
b34976b6 | 3547 | return TRUE; |
252b5132 RH |
3548 | } |
3549 | ||
3550 | /* Align to the maximum file alignment that could be required for any | |
3551 | ELF data structure. */ | |
3552 | ||
268b6b39 | 3553 | static inline file_ptr |
217aa764 | 3554 | align_file_position (file_ptr off, int align) |
252b5132 RH |
3555 | { |
3556 | return (off + align - 1) & ~(align - 1); | |
3557 | } | |
3558 | ||
3559 | /* Assign a file position to a section, optionally aligning to the | |
3560 | required section alignment. */ | |
3561 | ||
217aa764 AM |
3562 | file_ptr |
3563 | _bfd_elf_assign_file_position_for_section (Elf_Internal_Shdr *i_shdrp, | |
3564 | file_ptr offset, | |
3565 | bfd_boolean align) | |
252b5132 | 3566 | { |
72de5009 AM |
3567 | if (align && i_shdrp->sh_addralign > 1) |
3568 | offset = BFD_ALIGN (offset, i_shdrp->sh_addralign); | |
252b5132 RH |
3569 | i_shdrp->sh_offset = offset; |
3570 | if (i_shdrp->bfd_section != NULL) | |
3571 | i_shdrp->bfd_section->filepos = offset; | |
3572 | if (i_shdrp->sh_type != SHT_NOBITS) | |
3573 | offset += i_shdrp->sh_size; | |
3574 | return offset; | |
3575 | } | |
3576 | ||
3577 | /* Compute the file positions we are going to put the sections at, and | |
3578 | otherwise prepare to begin writing out the ELF file. If LINK_INFO | |
3579 | is not NULL, this is being called by the ELF backend linker. */ | |
3580 | ||
b34976b6 | 3581 | bfd_boolean |
217aa764 AM |
3582 | _bfd_elf_compute_section_file_positions (bfd *abfd, |
3583 | struct bfd_link_info *link_info) | |
252b5132 | 3584 | { |
9c5bfbb7 | 3585 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
d4730f92 | 3586 | struct fake_section_arg fsargs; |
b34976b6 | 3587 | bfd_boolean failed; |
4b6c0f2f | 3588 | struct bfd_strtab_hash *strtab = NULL; |
252b5132 | 3589 | Elf_Internal_Shdr *shstrtab_hdr; |
3516e984 | 3590 | bfd_boolean need_symtab; |
252b5132 RH |
3591 | |
3592 | if (abfd->output_has_begun) | |
b34976b6 | 3593 | return TRUE; |
252b5132 RH |
3594 | |
3595 | /* Do any elf backend specific processing first. */ | |
3596 | if (bed->elf_backend_begin_write_processing) | |
3597 | (*bed->elf_backend_begin_write_processing) (abfd, link_info); | |
3598 | ||
3599 | if (! prep_headers (abfd)) | |
b34976b6 | 3600 | return FALSE; |
252b5132 | 3601 | |
e6c51ed4 | 3602 | /* Post process the headers if necessary. */ |
78245035 | 3603 | (*bed->elf_backend_post_process_headers) (abfd, link_info); |
e6c51ed4 | 3604 | |
d4730f92 BS |
3605 | fsargs.failed = FALSE; |
3606 | fsargs.link_info = link_info; | |
3607 | bfd_map_over_sections (abfd, elf_fake_sections, &fsargs); | |
3608 | if (fsargs.failed) | |
b34976b6 | 3609 | return FALSE; |
252b5132 | 3610 | |
da9f89d4 | 3611 | if (!assign_section_numbers (abfd, link_info)) |
b34976b6 | 3612 | return FALSE; |
252b5132 RH |
3613 | |
3614 | /* The backend linker builds symbol table information itself. */ | |
3516e984 L |
3615 | need_symtab = (link_info == NULL |
3616 | && (bfd_get_symcount (abfd) > 0 | |
3617 | || ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC)) | |
3618 | == HAS_RELOC))); | |
3619 | if (need_symtab) | |
252b5132 RH |
3620 | { |
3621 | /* Non-zero if doing a relocatable link. */ | |
3622 | int relocatable_p = ! (abfd->flags & (EXEC_P | DYNAMIC)); | |
3623 | ||
3624 | if (! swap_out_syms (abfd, &strtab, relocatable_p)) | |
b34976b6 | 3625 | return FALSE; |
252b5132 RH |
3626 | } |
3627 | ||
d4730f92 | 3628 | failed = FALSE; |
1126897b | 3629 | if (link_info == NULL) |
dbb410c3 | 3630 | { |
1126897b | 3631 | bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed); |
dbb410c3 | 3632 | if (failed) |
b34976b6 | 3633 | return FALSE; |
dbb410c3 AM |
3634 | } |
3635 | ||
252b5132 RH |
3636 | shstrtab_hdr = &elf_tdata (abfd)->shstrtab_hdr; |
3637 | /* sh_name was set in prep_headers. */ | |
3638 | shstrtab_hdr->sh_type = SHT_STRTAB; | |
3639 | shstrtab_hdr->sh_flags = 0; | |
3640 | shstrtab_hdr->sh_addr = 0; | |
2b0f7ef9 | 3641 | shstrtab_hdr->sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd)); |
252b5132 RH |
3642 | shstrtab_hdr->sh_entsize = 0; |
3643 | shstrtab_hdr->sh_link = 0; | |
3644 | shstrtab_hdr->sh_info = 0; | |
3645 | /* sh_offset is set in assign_file_positions_except_relocs. */ | |
3646 | shstrtab_hdr->sh_addralign = 1; | |
3647 | ||
c84fca4d | 3648 | if (!assign_file_positions_except_relocs (abfd, link_info)) |
b34976b6 | 3649 | return FALSE; |
252b5132 | 3650 | |
3516e984 | 3651 | if (need_symtab) |
252b5132 RH |
3652 | { |
3653 | file_ptr off; | |
3654 | Elf_Internal_Shdr *hdr; | |
3655 | ||
12bd6957 | 3656 | off = elf_next_file_pos (abfd); |
252b5132 RH |
3657 | |
3658 | hdr = &elf_tdata (abfd)->symtab_hdr; | |
b34976b6 | 3659 | off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE); |
252b5132 | 3660 | |
9ad5cbcf AM |
3661 | hdr = &elf_tdata (abfd)->symtab_shndx_hdr; |
3662 | if (hdr->sh_size != 0) | |
b34976b6 | 3663 | off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE); |
9ad5cbcf | 3664 | |
252b5132 | 3665 | hdr = &elf_tdata (abfd)->strtab_hdr; |
b34976b6 | 3666 | off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE); |
252b5132 | 3667 | |
12bd6957 | 3668 | elf_next_file_pos (abfd) = off; |
252b5132 RH |
3669 | |
3670 | /* Now that we know where the .strtab section goes, write it | |
08a40648 | 3671 | out. */ |
252b5132 RH |
3672 | if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0 |
3673 | || ! _bfd_stringtab_emit (abfd, strtab)) | |
b34976b6 | 3674 | return FALSE; |
252b5132 RH |
3675 | _bfd_stringtab_free (strtab); |
3676 | } | |
3677 | ||
b34976b6 | 3678 | abfd->output_has_begun = TRUE; |
252b5132 | 3679 | |
b34976b6 | 3680 | return TRUE; |
252b5132 RH |
3681 | } |
3682 | ||
8ded5a0f AM |
3683 | /* Make an initial estimate of the size of the program header. If we |
3684 | get the number wrong here, we'll redo section placement. */ | |
3685 | ||
3686 | static bfd_size_type | |
3687 | get_program_header_size (bfd *abfd, struct bfd_link_info *info) | |
3688 | { | |
3689 | size_t segs; | |
3690 | asection *s; | |
2b05f1b7 | 3691 | const struct elf_backend_data *bed; |
8ded5a0f AM |
3692 | |
3693 | /* Assume we will need exactly two PT_LOAD segments: one for text | |
3694 | and one for data. */ | |
3695 | segs = 2; | |
3696 | ||
3697 | s = bfd_get_section_by_name (abfd, ".interp"); | |
3698 | if (s != NULL && (s->flags & SEC_LOAD) != 0) | |
3699 | { | |
3700 | /* If we have a loadable interpreter section, we need a | |
3701 | PT_INTERP segment. In this case, assume we also need a | |
3702 | PT_PHDR segment, although that may not be true for all | |
3703 | targets. */ | |
3704 | segs += 2; | |
3705 | } | |
3706 | ||
3707 | if (bfd_get_section_by_name (abfd, ".dynamic") != NULL) | |
3708 | { | |
3709 | /* We need a PT_DYNAMIC segment. */ | |
3710 | ++segs; | |
f210dcff | 3711 | } |
08a40648 | 3712 | |
ceae84aa | 3713 | if (info != NULL && info->relro) |
f210dcff L |
3714 | { |
3715 | /* We need a PT_GNU_RELRO segment. */ | |
3716 | ++segs; | |
8ded5a0f AM |
3717 | } |
3718 | ||
12bd6957 | 3719 | if (elf_eh_frame_hdr (abfd)) |
8ded5a0f AM |
3720 | { |
3721 | /* We need a PT_GNU_EH_FRAME segment. */ | |
3722 | ++segs; | |
3723 | } | |
3724 | ||
12bd6957 | 3725 | if (elf_stack_flags (abfd)) |
8ded5a0f | 3726 | { |
2b05f1b7 L |
3727 | /* We need a PT_GNU_STACK segment. */ |
3728 | ++segs; | |
3729 | } | |
94b11780 | 3730 | |
2b05f1b7 L |
3731 | for (s = abfd->sections; s != NULL; s = s->next) |
3732 | { | |
8ded5a0f | 3733 | if ((s->flags & SEC_LOAD) != 0 |
0112cd26 | 3734 | && CONST_STRNEQ (s->name, ".note")) |
8ded5a0f AM |
3735 | { |
3736 | /* We need a PT_NOTE segment. */ | |
3737 | ++segs; | |
1c5265b5 JJ |
3738 | /* Try to create just one PT_NOTE segment |
3739 | for all adjacent loadable .note* sections. | |
3740 | gABI requires that within a PT_NOTE segment | |
3741 | (and also inside of each SHT_NOTE section) | |
3742 | each note is padded to a multiple of 4 size, | |
3743 | so we check whether the sections are correctly | |
3744 | aligned. */ | |
3745 | if (s->alignment_power == 2) | |
3746 | while (s->next != NULL | |
3747 | && s->next->alignment_power == 2 | |
3748 | && (s->next->flags & SEC_LOAD) != 0 | |
3749 | && CONST_STRNEQ (s->next->name, ".note")) | |
3750 | s = s->next; | |
8ded5a0f AM |
3751 | } |
3752 | } | |
3753 | ||
3754 | for (s = abfd->sections; s != NULL; s = s->next) | |
3755 | { | |
3756 | if (s->flags & SEC_THREAD_LOCAL) | |
3757 | { | |
3758 | /* We need a PT_TLS segment. */ | |
3759 | ++segs; | |
3760 | break; | |
3761 | } | |
3762 | } | |
3763 | ||
3764 | /* Let the backend count up any program headers it might need. */ | |
2b05f1b7 | 3765 | bed = get_elf_backend_data (abfd); |
8ded5a0f AM |
3766 | if (bed->elf_backend_additional_program_headers) |
3767 | { | |
3768 | int a; | |
3769 | ||
3770 | a = (*bed->elf_backend_additional_program_headers) (abfd, info); | |
3771 | if (a == -1) | |
3772 | abort (); | |
3773 | segs += a; | |
3774 | } | |
3775 | ||
3776 | return segs * bed->s->sizeof_phdr; | |
3777 | } | |
3778 | ||
2ea37f1c NC |
3779 | /* Find the segment that contains the output_section of section. */ |
3780 | ||
3781 | Elf_Internal_Phdr * | |
3782 | _bfd_elf_find_segment_containing_section (bfd * abfd, asection * section) | |
3783 | { | |
3784 | struct elf_segment_map *m; | |
3785 | Elf_Internal_Phdr *p; | |
3786 | ||
12bd6957 | 3787 | for (m = elf_seg_map (abfd), p = elf_tdata (abfd)->phdr; |
2ea37f1c NC |
3788 | m != NULL; |
3789 | m = m->next, p++) | |
3790 | { | |
3791 | int i; | |
3792 | ||
3793 | for (i = m->count - 1; i >= 0; i--) | |
3794 | if (m->sections[i] == section) | |
3795 | return p; | |
3796 | } | |
3797 | ||
3798 | return NULL; | |
3799 | } | |
3800 | ||
252b5132 RH |
3801 | /* Create a mapping from a set of sections to a program segment. */ |
3802 | ||
217aa764 AM |
3803 | static struct elf_segment_map * |
3804 | make_mapping (bfd *abfd, | |
3805 | asection **sections, | |
3806 | unsigned int from, | |
3807 | unsigned int to, | |
3808 | bfd_boolean phdr) | |
252b5132 RH |
3809 | { |
3810 | struct elf_segment_map *m; | |
3811 | unsigned int i; | |
3812 | asection **hdrpp; | |
dc810e39 | 3813 | bfd_size_type amt; |
252b5132 | 3814 | |
dc810e39 AM |
3815 | amt = sizeof (struct elf_segment_map); |
3816 | amt += (to - from - 1) * sizeof (asection *); | |
a50b1753 | 3817 | m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); |
252b5132 RH |
3818 | if (m == NULL) |
3819 | return NULL; | |
3820 | m->next = NULL; | |
3821 | m->p_type = PT_LOAD; | |
3822 | for (i = from, hdrpp = sections + from; i < to; i++, hdrpp++) | |
3823 | m->sections[i - from] = *hdrpp; | |
3824 | m->count = to - from; | |
3825 | ||
3826 | if (from == 0 && phdr) | |
3827 | { | |
3828 | /* Include the headers in the first PT_LOAD segment. */ | |
3829 | m->includes_filehdr = 1; | |
3830 | m->includes_phdrs = 1; | |
3831 | } | |
3832 | ||
3833 | return m; | |
3834 | } | |
3835 | ||
229fcec5 MM |
3836 | /* Create the PT_DYNAMIC segment, which includes DYNSEC. Returns NULL |
3837 | on failure. */ | |
3838 | ||
3839 | struct elf_segment_map * | |
3840 | _bfd_elf_make_dynamic_segment (bfd *abfd, asection *dynsec) | |
3841 | { | |
3842 | struct elf_segment_map *m; | |
3843 | ||
a50b1753 NC |
3844 | m = (struct elf_segment_map *) bfd_zalloc (abfd, |
3845 | sizeof (struct elf_segment_map)); | |
229fcec5 MM |
3846 | if (m == NULL) |
3847 | return NULL; | |
3848 | m->next = NULL; | |
3849 | m->p_type = PT_DYNAMIC; | |
3850 | m->count = 1; | |
3851 | m->sections[0] = dynsec; | |
08a40648 | 3852 | |
229fcec5 MM |
3853 | return m; |
3854 | } | |
3855 | ||
8ded5a0f | 3856 | /* Possibly add or remove segments from the segment map. */ |
252b5132 | 3857 | |
b34976b6 | 3858 | static bfd_boolean |
3dea8fca AM |
3859 | elf_modify_segment_map (bfd *abfd, |
3860 | struct bfd_link_info *info, | |
3861 | bfd_boolean remove_empty_load) | |
252b5132 | 3862 | { |
252e386e | 3863 | struct elf_segment_map **m; |
8ded5a0f | 3864 | const struct elf_backend_data *bed; |
252b5132 | 3865 | |
8ded5a0f AM |
3866 | /* The placement algorithm assumes that non allocated sections are |
3867 | not in PT_LOAD segments. We ensure this here by removing such | |
3868 | sections from the segment map. We also remove excluded | |
252e386e AM |
3869 | sections. Finally, any PT_LOAD segment without sections is |
3870 | removed. */ | |
12bd6957 | 3871 | m = &elf_seg_map (abfd); |
252e386e | 3872 | while (*m) |
8ded5a0f AM |
3873 | { |
3874 | unsigned int i, new_count; | |
252b5132 | 3875 | |
252e386e | 3876 | for (new_count = 0, i = 0; i < (*m)->count; i++) |
8ded5a0f | 3877 | { |
252e386e AM |
3878 | if (((*m)->sections[i]->flags & SEC_EXCLUDE) == 0 |
3879 | && (((*m)->sections[i]->flags & SEC_ALLOC) != 0 | |
3880 | || (*m)->p_type != PT_LOAD)) | |
8ded5a0f | 3881 | { |
252e386e AM |
3882 | (*m)->sections[new_count] = (*m)->sections[i]; |
3883 | new_count++; | |
8ded5a0f AM |
3884 | } |
3885 | } | |
252e386e | 3886 | (*m)->count = new_count; |
252b5132 | 3887 | |
3dea8fca | 3888 | if (remove_empty_load && (*m)->p_type == PT_LOAD && (*m)->count == 0) |
252e386e AM |
3889 | *m = (*m)->next; |
3890 | else | |
3891 | m = &(*m)->next; | |
8ded5a0f | 3892 | } |
252b5132 | 3893 | |
8ded5a0f AM |
3894 | bed = get_elf_backend_data (abfd); |
3895 | if (bed->elf_backend_modify_segment_map != NULL) | |
252b5132 | 3896 | { |
252e386e | 3897 | if (!(*bed->elf_backend_modify_segment_map) (abfd, info)) |
8ded5a0f | 3898 | return FALSE; |
252b5132 | 3899 | } |
252b5132 | 3900 | |
8ded5a0f AM |
3901 | return TRUE; |
3902 | } | |
252b5132 | 3903 | |
8ded5a0f | 3904 | /* Set up a mapping from BFD sections to program segments. */ |
252b5132 | 3905 | |
8ded5a0f AM |
3906 | bfd_boolean |
3907 | _bfd_elf_map_sections_to_segments (bfd *abfd, struct bfd_link_info *info) | |
3908 | { | |
3909 | unsigned int count; | |
3910 | struct elf_segment_map *m; | |
3911 | asection **sections = NULL; | |
3912 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
3dea8fca | 3913 | bfd_boolean no_user_phdrs; |
252b5132 | 3914 | |
12bd6957 | 3915 | no_user_phdrs = elf_seg_map (abfd) == NULL; |
d324f6d6 RM |
3916 | |
3917 | if (info != NULL) | |
3918 | info->user_phdrs = !no_user_phdrs; | |
3919 | ||
3dea8fca | 3920 | if (no_user_phdrs && bfd_count_sections (abfd) != 0) |
252b5132 | 3921 | { |
8ded5a0f AM |
3922 | asection *s; |
3923 | unsigned int i; | |
3924 | struct elf_segment_map *mfirst; | |
3925 | struct elf_segment_map **pm; | |
3926 | asection *last_hdr; | |
3927 | bfd_vma last_size; | |
3928 | unsigned int phdr_index; | |
3929 | bfd_vma maxpagesize; | |
3930 | asection **hdrpp; | |
3931 | bfd_boolean phdr_in_segment = TRUE; | |
3932 | bfd_boolean writable; | |
3933 | int tls_count = 0; | |
3934 | asection *first_tls = NULL; | |
3935 | asection *dynsec, *eh_frame_hdr; | |
3936 | bfd_size_type amt; | |
8d06853e | 3937 | bfd_vma addr_mask, wrap_to = 0; |
252b5132 | 3938 | |
8ded5a0f | 3939 | /* Select the allocated sections, and sort them. */ |
252b5132 | 3940 | |
a50b1753 NC |
3941 | sections = (asection **) bfd_malloc2 (bfd_count_sections (abfd), |
3942 | sizeof (asection *)); | |
8ded5a0f | 3943 | if (sections == NULL) |
252b5132 | 3944 | goto error_return; |
252b5132 | 3945 | |
8d06853e AM |
3946 | /* Calculate top address, avoiding undefined behaviour of shift |
3947 | left operator when shift count is equal to size of type | |
3948 | being shifted. */ | |
3949 | addr_mask = ((bfd_vma) 1 << (bfd_arch_bits_per_address (abfd) - 1)) - 1; | |
3950 | addr_mask = (addr_mask << 1) + 1; | |
3951 | ||
8ded5a0f AM |
3952 | i = 0; |
3953 | for (s = abfd->sections; s != NULL; s = s->next) | |
3954 | { | |
3955 | if ((s->flags & SEC_ALLOC) != 0) | |
3956 | { | |
3957 | sections[i] = s; | |
3958 | ++i; | |
8d06853e AM |
3959 | /* A wrapping section potentially clashes with header. */ |
3960 | if (((s->lma + s->size) & addr_mask) < (s->lma & addr_mask)) | |
3961 | wrap_to = (s->lma + s->size) & addr_mask; | |
8ded5a0f AM |
3962 | } |
3963 | } | |
3964 | BFD_ASSERT (i <= bfd_count_sections (abfd)); | |
3965 | count = i; | |
252b5132 | 3966 | |
8ded5a0f | 3967 | qsort (sections, (size_t) count, sizeof (asection *), elf_sort_sections); |
252b5132 | 3968 | |
8ded5a0f | 3969 | /* Build the mapping. */ |
252b5132 | 3970 | |
8ded5a0f AM |
3971 | mfirst = NULL; |
3972 | pm = &mfirst; | |
252b5132 | 3973 | |
8ded5a0f AM |
3974 | /* If we have a .interp section, then create a PT_PHDR segment for |
3975 | the program headers and a PT_INTERP segment for the .interp | |
3976 | section. */ | |
3977 | s = bfd_get_section_by_name (abfd, ".interp"); | |
3978 | if (s != NULL && (s->flags & SEC_LOAD) != 0) | |
3979 | { | |
3980 | amt = sizeof (struct elf_segment_map); | |
a50b1753 | 3981 | m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); |
8ded5a0f AM |
3982 | if (m == NULL) |
3983 | goto error_return; | |
3984 | m->next = NULL; | |
3985 | m->p_type = PT_PHDR; | |
3986 | /* FIXME: UnixWare and Solaris set PF_X, Irix 5 does not. */ | |
3987 | m->p_flags = PF_R | PF_X; | |
3988 | m->p_flags_valid = 1; | |
3989 | m->includes_phdrs = 1; | |
252b5132 | 3990 | |
8ded5a0f AM |
3991 | *pm = m; |
3992 | pm = &m->next; | |
252b5132 | 3993 | |
8ded5a0f | 3994 | amt = sizeof (struct elf_segment_map); |
a50b1753 | 3995 | m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); |
8ded5a0f AM |
3996 | if (m == NULL) |
3997 | goto error_return; | |
3998 | m->next = NULL; | |
3999 | m->p_type = PT_INTERP; | |
4000 | m->count = 1; | |
4001 | m->sections[0] = s; | |
4002 | ||
4003 | *pm = m; | |
4004 | pm = &m->next; | |
252b5132 | 4005 | } |
8ded5a0f AM |
4006 | |
4007 | /* Look through the sections. We put sections in the same program | |
4008 | segment when the start of the second section can be placed within | |
4009 | a few bytes of the end of the first section. */ | |
4010 | last_hdr = NULL; | |
4011 | last_size = 0; | |
4012 | phdr_index = 0; | |
4013 | maxpagesize = bed->maxpagesize; | |
4014 | writable = FALSE; | |
4015 | dynsec = bfd_get_section_by_name (abfd, ".dynamic"); | |
4016 | if (dynsec != NULL | |
4017 | && (dynsec->flags & SEC_LOAD) == 0) | |
4018 | dynsec = NULL; | |
4019 | ||
4020 | /* Deal with -Ttext or something similar such that the first section | |
4021 | is not adjacent to the program headers. This is an | |
4022 | approximation, since at this point we don't know exactly how many | |
4023 | program headers we will need. */ | |
4024 | if (count > 0) | |
252b5132 | 4025 | { |
12bd6957 | 4026 | bfd_size_type phdr_size = elf_program_header_size (abfd); |
8ded5a0f | 4027 | |
62d7a5f6 | 4028 | if (phdr_size == (bfd_size_type) -1) |
8ded5a0f | 4029 | phdr_size = get_program_header_size (abfd, info); |
d2bcb0d1 | 4030 | phdr_size += bed->s->sizeof_ehdr; |
8ded5a0f | 4031 | if ((abfd->flags & D_PAGED) == 0 |
8d06853e AM |
4032 | || (sections[0]->lma & addr_mask) < phdr_size |
4033 | || ((sections[0]->lma & addr_mask) % maxpagesize | |
4034 | < phdr_size % maxpagesize) | |
4035 | || (sections[0]->lma & addr_mask & -maxpagesize) < wrap_to) | |
8ded5a0f | 4036 | phdr_in_segment = FALSE; |
252b5132 RH |
4037 | } |
4038 | ||
8ded5a0f | 4039 | for (i = 0, hdrpp = sections; i < count; i++, hdrpp++) |
252b5132 | 4040 | { |
8ded5a0f AM |
4041 | asection *hdr; |
4042 | bfd_boolean new_segment; | |
4043 | ||
4044 | hdr = *hdrpp; | |
4045 | ||
4046 | /* See if this section and the last one will fit in the same | |
4047 | segment. */ | |
4048 | ||
4049 | if (last_hdr == NULL) | |
4050 | { | |
4051 | /* If we don't have a segment yet, then we don't need a new | |
4052 | one (we build the last one after this loop). */ | |
4053 | new_segment = FALSE; | |
4054 | } | |
4055 | else if (last_hdr->lma - last_hdr->vma != hdr->lma - hdr->vma) | |
4056 | { | |
4057 | /* If this section has a different relation between the | |
4058 | virtual address and the load address, then we need a new | |
4059 | segment. */ | |
4060 | new_segment = TRUE; | |
4061 | } | |
b5599592 AM |
4062 | else if (hdr->lma < last_hdr->lma + last_size |
4063 | || last_hdr->lma + last_size < last_hdr->lma) | |
4064 | { | |
4065 | /* If this section has a load address that makes it overlap | |
4066 | the previous section, then we need a new segment. */ | |
4067 | new_segment = TRUE; | |
4068 | } | |
39948a60 NC |
4069 | /* In the next test we have to be careful when last_hdr->lma is close |
4070 | to the end of the address space. If the aligned address wraps | |
4071 | around to the start of the address space, then there are no more | |
4072 | pages left in memory and it is OK to assume that the current | |
4073 | section can be included in the current segment. */ | |
4074 | else if ((BFD_ALIGN (last_hdr->lma + last_size, maxpagesize) + maxpagesize | |
4075 | > last_hdr->lma) | |
4076 | && (BFD_ALIGN (last_hdr->lma + last_size, maxpagesize) + maxpagesize | |
4ff73856 | 4077 | <= hdr->lma)) |
8ded5a0f AM |
4078 | { |
4079 | /* If putting this section in this segment would force us to | |
4080 | skip a page in the segment, then we need a new segment. */ | |
4081 | new_segment = TRUE; | |
4082 | } | |
4083 | else if ((last_hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0 | |
4084 | && (hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) != 0) | |
4085 | { | |
4086 | /* We don't want to put a loadable section after a | |
4087 | nonloadable section in the same segment. | |
4088 | Consider .tbss sections as loadable for this purpose. */ | |
4089 | new_segment = TRUE; | |
4090 | } | |
4091 | else if ((abfd->flags & D_PAGED) == 0) | |
4092 | { | |
4093 | /* If the file is not demand paged, which means that we | |
4094 | don't require the sections to be correctly aligned in the | |
4095 | file, then there is no other reason for a new segment. */ | |
4096 | new_segment = FALSE; | |
4097 | } | |
4098 | else if (! writable | |
4099 | && (hdr->flags & SEC_READONLY) == 0 | |
8d06853e AM |
4100 | && (((last_hdr->lma + last_size - 1) & -maxpagesize) |
4101 | != (hdr->lma & -maxpagesize))) | |
8ded5a0f AM |
4102 | { |
4103 | /* We don't want to put a writable section in a read only | |
4104 | segment, unless they are on the same page in memory | |
4105 | anyhow. We already know that the last section does not | |
4106 | bring us past the current section on the page, so the | |
4107 | only case in which the new section is not on the same | |
4108 | page as the previous section is when the previous section | |
4109 | ends precisely on a page boundary. */ | |
4110 | new_segment = TRUE; | |
4111 | } | |
4112 | else | |
4113 | { | |
4114 | /* Otherwise, we can use the same segment. */ | |
4115 | new_segment = FALSE; | |
4116 | } | |
4117 | ||
2889e75b | 4118 | /* Allow interested parties a chance to override our decision. */ |
ceae84aa AM |
4119 | if (last_hdr != NULL |
4120 | && info != NULL | |
4121 | && info->callbacks->override_segment_assignment != NULL) | |
4122 | new_segment | |
4123 | = info->callbacks->override_segment_assignment (info, abfd, hdr, | |
4124 | last_hdr, | |
4125 | new_segment); | |
2889e75b | 4126 | |
8ded5a0f AM |
4127 | if (! new_segment) |
4128 | { | |
4129 | if ((hdr->flags & SEC_READONLY) == 0) | |
4130 | writable = TRUE; | |
4131 | last_hdr = hdr; | |
4132 | /* .tbss sections effectively have zero size. */ | |
4133 | if ((hdr->flags & (SEC_THREAD_LOCAL | SEC_LOAD)) | |
4134 | != SEC_THREAD_LOCAL) | |
4135 | last_size = hdr->size; | |
4136 | else | |
4137 | last_size = 0; | |
4138 | continue; | |
4139 | } | |
4140 | ||
4141 | /* We need a new program segment. We must create a new program | |
4142 | header holding all the sections from phdr_index until hdr. */ | |
4143 | ||
4144 | m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment); | |
4145 | if (m == NULL) | |
4146 | goto error_return; | |
4147 | ||
4148 | *pm = m; | |
4149 | pm = &m->next; | |
4150 | ||
252b5132 | 4151 | if ((hdr->flags & SEC_READONLY) == 0) |
b34976b6 | 4152 | writable = TRUE; |
8ded5a0f AM |
4153 | else |
4154 | writable = FALSE; | |
4155 | ||
baaff79e JJ |
4156 | last_hdr = hdr; |
4157 | /* .tbss sections effectively have zero size. */ | |
e5caec89 | 4158 | if ((hdr->flags & (SEC_THREAD_LOCAL | SEC_LOAD)) != SEC_THREAD_LOCAL) |
eea6121a | 4159 | last_size = hdr->size; |
baaff79e JJ |
4160 | else |
4161 | last_size = 0; | |
8ded5a0f AM |
4162 | phdr_index = i; |
4163 | phdr_in_segment = FALSE; | |
252b5132 RH |
4164 | } |
4165 | ||
86b2281f AM |
4166 | /* Create a final PT_LOAD program segment, but not if it's just |
4167 | for .tbss. */ | |
4168 | if (last_hdr != NULL | |
4169 | && (i - phdr_index != 1 | |
4170 | || ((last_hdr->flags & (SEC_THREAD_LOCAL | SEC_LOAD)) | |
4171 | != SEC_THREAD_LOCAL))) | |
8ded5a0f AM |
4172 | { |
4173 | m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment); | |
4174 | if (m == NULL) | |
4175 | goto error_return; | |
252b5132 | 4176 | |
8ded5a0f AM |
4177 | *pm = m; |
4178 | pm = &m->next; | |
4179 | } | |
252b5132 | 4180 | |
8ded5a0f AM |
4181 | /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */ |
4182 | if (dynsec != NULL) | |
4183 | { | |
4184 | m = _bfd_elf_make_dynamic_segment (abfd, dynsec); | |
4185 | if (m == NULL) | |
4186 | goto error_return; | |
4187 | *pm = m; | |
4188 | pm = &m->next; | |
4189 | } | |
252b5132 | 4190 | |
1c5265b5 JJ |
4191 | /* For each batch of consecutive loadable .note sections, |
4192 | add a PT_NOTE segment. We don't use bfd_get_section_by_name, | |
4193 | because if we link together nonloadable .note sections and | |
4194 | loadable .note sections, we will generate two .note sections | |
4195 | in the output file. FIXME: Using names for section types is | |
4196 | bogus anyhow. */ | |
8ded5a0f AM |
4197 | for (s = abfd->sections; s != NULL; s = s->next) |
4198 | { | |
4199 | if ((s->flags & SEC_LOAD) != 0 | |
0112cd26 | 4200 | && CONST_STRNEQ (s->name, ".note")) |
8ded5a0f | 4201 | { |
1c5265b5 | 4202 | asection *s2; |
91d6fa6a NC |
4203 | |
4204 | count = 1; | |
8ded5a0f | 4205 | amt = sizeof (struct elf_segment_map); |
1c5265b5 JJ |
4206 | if (s->alignment_power == 2) |
4207 | for (s2 = s; s2->next != NULL; s2 = s2->next) | |
55b581a6 JJ |
4208 | { |
4209 | if (s2->next->alignment_power == 2 | |
4210 | && (s2->next->flags & SEC_LOAD) != 0 | |
4211 | && CONST_STRNEQ (s2->next->name, ".note") | |
8d06853e AM |
4212 | && align_power (s2->lma + s2->size, 2) |
4213 | == s2->next->lma) | |
55b581a6 JJ |
4214 | count++; |
4215 | else | |
4216 | break; | |
4217 | } | |
1c5265b5 | 4218 | amt += (count - 1) * sizeof (asection *); |
a50b1753 | 4219 | m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); |
8ded5a0f AM |
4220 | if (m == NULL) |
4221 | goto error_return; | |
4222 | m->next = NULL; | |
4223 | m->p_type = PT_NOTE; | |
1c5265b5 JJ |
4224 | m->count = count; |
4225 | while (count > 1) | |
4226 | { | |
4227 | m->sections[m->count - count--] = s; | |
4228 | BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0); | |
4229 | s = s->next; | |
4230 | } | |
4231 | m->sections[m->count - 1] = s; | |
4232 | BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0); | |
8ded5a0f AM |
4233 | *pm = m; |
4234 | pm = &m->next; | |
4235 | } | |
4236 | if (s->flags & SEC_THREAD_LOCAL) | |
4237 | { | |
4238 | if (! tls_count) | |
4239 | first_tls = s; | |
4240 | tls_count++; | |
4241 | } | |
4242 | } | |
252b5132 | 4243 | |
8ded5a0f AM |
4244 | /* If there are any SHF_TLS output sections, add PT_TLS segment. */ |
4245 | if (tls_count > 0) | |
4246 | { | |
8ded5a0f AM |
4247 | amt = sizeof (struct elf_segment_map); |
4248 | amt += (tls_count - 1) * sizeof (asection *); | |
a50b1753 | 4249 | m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); |
8ded5a0f AM |
4250 | if (m == NULL) |
4251 | goto error_return; | |
4252 | m->next = NULL; | |
4253 | m->p_type = PT_TLS; | |
4254 | m->count = tls_count; | |
4255 | /* Mandated PF_R. */ | |
4256 | m->p_flags = PF_R; | |
4257 | m->p_flags_valid = 1; | |
d923cae0 | 4258 | s = first_tls; |
91d6fa6a | 4259 | for (i = 0; i < (unsigned int) tls_count; ++i) |
8ded5a0f | 4260 | { |
d923cae0 L |
4261 | if ((s->flags & SEC_THREAD_LOCAL) == 0) |
4262 | { | |
4263 | _bfd_error_handler | |
4264 | (_("%B: TLS sections are not adjacent:"), abfd); | |
4265 | s = first_tls; | |
4266 | i = 0; | |
4267 | while (i < (unsigned int) tls_count) | |
4268 | { | |
4269 | if ((s->flags & SEC_THREAD_LOCAL) != 0) | |
4270 | { | |
4271 | _bfd_error_handler (_(" TLS: %A"), s); | |
4272 | i++; | |
4273 | } | |
4274 | else | |
4275 | _bfd_error_handler (_(" non-TLS: %A"), s); | |
4276 | s = s->next; | |
4277 | } | |
4278 | bfd_set_error (bfd_error_bad_value); | |
4279 | goto error_return; | |
4280 | } | |
4281 | m->sections[i] = s; | |
4282 | s = s->next; | |
8ded5a0f | 4283 | } |
252b5132 | 4284 | |
8ded5a0f AM |
4285 | *pm = m; |
4286 | pm = &m->next; | |
4287 | } | |
252b5132 | 4288 | |
8ded5a0f AM |
4289 | /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME |
4290 | segment. */ | |
12bd6957 | 4291 | eh_frame_hdr = elf_eh_frame_hdr (abfd); |
8ded5a0f AM |
4292 | if (eh_frame_hdr != NULL |
4293 | && (eh_frame_hdr->output_section->flags & SEC_LOAD) != 0) | |
252b5132 | 4294 | { |
dc810e39 | 4295 | amt = sizeof (struct elf_segment_map); |
a50b1753 | 4296 | m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); |
252b5132 RH |
4297 | if (m == NULL) |
4298 | goto error_return; | |
4299 | m->next = NULL; | |
8ded5a0f | 4300 | m->p_type = PT_GNU_EH_FRAME; |
252b5132 | 4301 | m->count = 1; |
8ded5a0f | 4302 | m->sections[0] = eh_frame_hdr->output_section; |
252b5132 RH |
4303 | |
4304 | *pm = m; | |
4305 | pm = &m->next; | |
4306 | } | |
13ae64f3 | 4307 | |
12bd6957 | 4308 | if (elf_stack_flags (abfd)) |
13ae64f3 | 4309 | { |
8ded5a0f | 4310 | amt = sizeof (struct elf_segment_map); |
a50b1753 | 4311 | m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); |
8ded5a0f AM |
4312 | if (m == NULL) |
4313 | goto error_return; | |
4314 | m->next = NULL; | |
2b05f1b7 | 4315 | m->p_type = PT_GNU_STACK; |
12bd6957 | 4316 | m->p_flags = elf_stack_flags (abfd); |
04c3a755 | 4317 | m->p_align = bed->stack_align; |
8ded5a0f | 4318 | m->p_flags_valid = 1; |
04c3a755 NS |
4319 | m->p_align_valid = m->p_align != 0; |
4320 | if (info->stacksize > 0) | |
4321 | { | |
4322 | m->p_size = info->stacksize; | |
4323 | m->p_size_valid = 1; | |
4324 | } | |
252b5132 | 4325 | |
8ded5a0f AM |
4326 | *pm = m; |
4327 | pm = &m->next; | |
4328 | } | |
65765700 | 4329 | |
ceae84aa | 4330 | if (info != NULL && info->relro) |
8ded5a0f | 4331 | { |
f210dcff L |
4332 | for (m = mfirst; m != NULL; m = m->next) |
4333 | { | |
3832a4d8 AM |
4334 | if (m->p_type == PT_LOAD |
4335 | && m->count != 0 | |
4336 | && m->sections[0]->vma >= info->relro_start | |
4337 | && m->sections[0]->vma < info->relro_end) | |
f210dcff | 4338 | { |
3832a4d8 AM |
4339 | i = m->count; |
4340 | while (--i != (unsigned) -1) | |
4341 | if ((m->sections[i]->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) | |
4342 | == (SEC_LOAD | SEC_HAS_CONTENTS)) | |
4343 | break; | |
4344 | ||
43a8475c | 4345 | if (i != (unsigned) -1) |
f210dcff L |
4346 | break; |
4347 | } | |
be01b344 | 4348 | } |
f210dcff L |
4349 | |
4350 | /* Make a PT_GNU_RELRO segment only when it isn't empty. */ | |
4351 | if (m != NULL) | |
4352 | { | |
4353 | amt = sizeof (struct elf_segment_map); | |
a50b1753 | 4354 | m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); |
f210dcff L |
4355 | if (m == NULL) |
4356 | goto error_return; | |
4357 | m->next = NULL; | |
4358 | m->p_type = PT_GNU_RELRO; | |
4359 | m->p_flags = PF_R; | |
4360 | m->p_flags_valid = 1; | |
4361 | ||
4362 | *pm = m; | |
4363 | pm = &m->next; | |
4364 | } | |
8ded5a0f | 4365 | } |
9ee5e499 | 4366 | |
8ded5a0f | 4367 | free (sections); |
12bd6957 | 4368 | elf_seg_map (abfd) = mfirst; |
9ee5e499 JJ |
4369 | } |
4370 | ||
3dea8fca | 4371 | if (!elf_modify_segment_map (abfd, info, no_user_phdrs)) |
8ded5a0f | 4372 | return FALSE; |
8c37241b | 4373 | |
12bd6957 | 4374 | for (count = 0, m = elf_seg_map (abfd); m != NULL; m = m->next) |
8ded5a0f | 4375 | ++count; |
12bd6957 | 4376 | elf_program_header_size (abfd) = count * bed->s->sizeof_phdr; |
252b5132 | 4377 | |
b34976b6 | 4378 | return TRUE; |
252b5132 RH |
4379 | |
4380 | error_return: | |
4381 | if (sections != NULL) | |
4382 | free (sections); | |
b34976b6 | 4383 | return FALSE; |
252b5132 RH |
4384 | } |
4385 | ||
4386 | /* Sort sections by address. */ | |
4387 | ||
4388 | static int | |
217aa764 | 4389 | elf_sort_sections (const void *arg1, const void *arg2) |
252b5132 RH |
4390 | { |
4391 | const asection *sec1 = *(const asection **) arg1; | |
4392 | const asection *sec2 = *(const asection **) arg2; | |
eecdbe52 | 4393 | bfd_size_type size1, size2; |
252b5132 RH |
4394 | |
4395 | /* Sort by LMA first, since this is the address used to | |
4396 | place the section into a segment. */ | |
4397 | if (sec1->lma < sec2->lma) | |
4398 | return -1; | |
4399 | else if (sec1->lma > sec2->lma) | |
4400 | return 1; | |
4401 | ||
4402 | /* Then sort by VMA. Normally the LMA and the VMA will be | |
4403 | the same, and this will do nothing. */ | |
4404 | if (sec1->vma < sec2->vma) | |
4405 | return -1; | |
4406 | else if (sec1->vma > sec2->vma) | |
4407 | return 1; | |
4408 | ||
4409 | /* Put !SEC_LOAD sections after SEC_LOAD ones. */ | |
4410 | ||
07c6e936 | 4411 | #define TOEND(x) (((x)->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0) |
252b5132 RH |
4412 | |
4413 | if (TOEND (sec1)) | |
4414 | { | |
4415 | if (TOEND (sec2)) | |
00a7cdc5 NC |
4416 | { |
4417 | /* If the indicies are the same, do not return 0 | |
4418 | here, but continue to try the next comparison. */ | |
4419 | if (sec1->target_index - sec2->target_index != 0) | |
4420 | return sec1->target_index - sec2->target_index; | |
4421 | } | |
252b5132 RH |
4422 | else |
4423 | return 1; | |
4424 | } | |
00a7cdc5 | 4425 | else if (TOEND (sec2)) |
252b5132 RH |
4426 | return -1; |
4427 | ||
4428 | #undef TOEND | |
4429 | ||
00a7cdc5 NC |
4430 | /* Sort by size, to put zero sized sections |
4431 | before others at the same address. */ | |
252b5132 | 4432 | |
eea6121a AM |
4433 | size1 = (sec1->flags & SEC_LOAD) ? sec1->size : 0; |
4434 | size2 = (sec2->flags & SEC_LOAD) ? sec2->size : 0; | |
eecdbe52 JJ |
4435 | |
4436 | if (size1 < size2) | |
252b5132 | 4437 | return -1; |
eecdbe52 | 4438 | if (size1 > size2) |
252b5132 RH |
4439 | return 1; |
4440 | ||
4441 | return sec1->target_index - sec2->target_index; | |
4442 | } | |
4443 | ||
340b6d91 AC |
4444 | /* Ian Lance Taylor writes: |
4445 | ||
4446 | We shouldn't be using % with a negative signed number. That's just | |
4447 | not good. We have to make sure either that the number is not | |
4448 | negative, or that the number has an unsigned type. When the types | |
4449 | are all the same size they wind up as unsigned. When file_ptr is a | |
4450 | larger signed type, the arithmetic winds up as signed long long, | |
4451 | which is wrong. | |
4452 | ||
4453 | What we're trying to say here is something like ``increase OFF by | |
4454 | the least amount that will cause it to be equal to the VMA modulo | |
4455 | the page size.'' */ | |
4456 | /* In other words, something like: | |
4457 | ||
4458 | vma_offset = m->sections[0]->vma % bed->maxpagesize; | |
4459 | off_offset = off % bed->maxpagesize; | |
4460 | if (vma_offset < off_offset) | |
4461 | adjustment = vma_offset + bed->maxpagesize - off_offset; | |
4462 | else | |
4463 | adjustment = vma_offset - off_offset; | |
08a40648 | 4464 | |
340b6d91 AC |
4465 | which can can be collapsed into the expression below. */ |
4466 | ||
4467 | static file_ptr | |
4468 | vma_page_aligned_bias (bfd_vma vma, ufile_ptr off, bfd_vma maxpagesize) | |
4469 | { | |
dc9155b2 NC |
4470 | /* PR binutils/16199: Handle an alignment of zero. */ |
4471 | if (maxpagesize == 0) | |
4472 | maxpagesize = 1; | |
340b6d91 AC |
4473 | return ((vma - off) % maxpagesize); |
4474 | } | |
4475 | ||
6d33f217 L |
4476 | static void |
4477 | print_segment_map (const struct elf_segment_map *m) | |
4478 | { | |
4479 | unsigned int j; | |
4480 | const char *pt = get_segment_type (m->p_type); | |
4481 | char buf[32]; | |
4482 | ||
4483 | if (pt == NULL) | |
4484 | { | |
4485 | if (m->p_type >= PT_LOPROC && m->p_type <= PT_HIPROC) | |
4486 | sprintf (buf, "LOPROC+%7.7x", | |
4487 | (unsigned int) (m->p_type - PT_LOPROC)); | |
4488 | else if (m->p_type >= PT_LOOS && m->p_type <= PT_HIOS) | |
4489 | sprintf (buf, "LOOS+%7.7x", | |
4490 | (unsigned int) (m->p_type - PT_LOOS)); | |
4491 | else | |
4492 | snprintf (buf, sizeof (buf), "%8.8x", | |
4493 | (unsigned int) m->p_type); | |
4494 | pt = buf; | |
4495 | } | |
4a97a0e5 | 4496 | fflush (stdout); |
6d33f217 L |
4497 | fprintf (stderr, "%s:", pt); |
4498 | for (j = 0; j < m->count; j++) | |
4499 | fprintf (stderr, " %s", m->sections [j]->name); | |
4500 | putc ('\n',stderr); | |
4a97a0e5 | 4501 | fflush (stderr); |
6d33f217 L |
4502 | } |
4503 | ||
32812159 AM |
4504 | static bfd_boolean |
4505 | write_zeros (bfd *abfd, file_ptr pos, bfd_size_type len) | |
4506 | { | |
4507 | void *buf; | |
4508 | bfd_boolean ret; | |
4509 | ||
4510 | if (bfd_seek (abfd, pos, SEEK_SET) != 0) | |
4511 | return FALSE; | |
4512 | buf = bfd_zmalloc (len); | |
4513 | if (buf == NULL) | |
4514 | return FALSE; | |
4515 | ret = bfd_bwrite (buf, len, abfd) == len; | |
4516 | free (buf); | |
4517 | return ret; | |
4518 | } | |
4519 | ||
252b5132 RH |
4520 | /* Assign file positions to the sections based on the mapping from |
4521 | sections to segments. This function also sets up some fields in | |
f3520d2f | 4522 | the file header. */ |
252b5132 | 4523 | |
b34976b6 | 4524 | static bfd_boolean |
f3520d2f AM |
4525 | assign_file_positions_for_load_sections (bfd *abfd, |
4526 | struct bfd_link_info *link_info) | |
252b5132 RH |
4527 | { |
4528 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
252b5132 | 4529 | struct elf_segment_map *m; |
252b5132 | 4530 | Elf_Internal_Phdr *phdrs; |
252b5132 | 4531 | Elf_Internal_Phdr *p; |
02bf8d82 | 4532 | file_ptr off; |
3f570048 | 4533 | bfd_size_type maxpagesize; |
f3520d2f | 4534 | unsigned int alloc; |
0920dee7 | 4535 | unsigned int i, j; |
2b0bc088 | 4536 | bfd_vma header_pad = 0; |
252b5132 | 4537 | |
e36284ab | 4538 | if (link_info == NULL |
ceae84aa | 4539 | && !_bfd_elf_map_sections_to_segments (abfd, link_info)) |
8ded5a0f | 4540 | return FALSE; |
252b5132 | 4541 | |
8ded5a0f | 4542 | alloc = 0; |
12bd6957 | 4543 | for (m = elf_seg_map (abfd); m != NULL; m = m->next) |
2b0bc088 NC |
4544 | { |
4545 | ++alloc; | |
4546 | if (m->header_size) | |
4547 | header_pad = m->header_size; | |
4548 | } | |
252b5132 | 4549 | |
82f2dbf7 NC |
4550 | if (alloc) |
4551 | { | |
4552 | elf_elfheader (abfd)->e_phoff = bed->s->sizeof_ehdr; | |
4553 | elf_elfheader (abfd)->e_phentsize = bed->s->sizeof_phdr; | |
4554 | } | |
4555 | else | |
4556 | { | |
4557 | /* PR binutils/12467. */ | |
4558 | elf_elfheader (abfd)->e_phoff = 0; | |
4559 | elf_elfheader (abfd)->e_phentsize = 0; | |
4560 | } | |
d324f6d6 | 4561 | |
8ded5a0f | 4562 | elf_elfheader (abfd)->e_phnum = alloc; |
252b5132 | 4563 | |
12bd6957 AM |
4564 | if (elf_program_header_size (abfd) == (bfd_size_type) -1) |
4565 | elf_program_header_size (abfd) = alloc * bed->s->sizeof_phdr; | |
8ded5a0f | 4566 | else |
12bd6957 | 4567 | BFD_ASSERT (elf_program_header_size (abfd) |
59e0647f | 4568 | >= alloc * bed->s->sizeof_phdr); |
252b5132 RH |
4569 | |
4570 | if (alloc == 0) | |
f3520d2f | 4571 | { |
12bd6957 | 4572 | elf_next_file_pos (abfd) = bed->s->sizeof_ehdr; |
8ded5a0f | 4573 | return TRUE; |
f3520d2f | 4574 | } |
252b5132 | 4575 | |
12bd6957 | 4576 | /* We're writing the size in elf_program_header_size (abfd), |
57268894 HPN |
4577 | see assign_file_positions_except_relocs, so make sure we have |
4578 | that amount allocated, with trailing space cleared. | |
12bd6957 AM |
4579 | The variable alloc contains the computed need, while |
4580 | elf_program_header_size (abfd) contains the size used for the | |
57268894 HPN |
4581 | layout. |
4582 | See ld/emultempl/elf-generic.em:gld${EMULATION_NAME}_map_segments | |
4583 | where the layout is forced to according to a larger size in the | |
4584 | last iterations for the testcase ld-elf/header. */ | |
12bd6957 | 4585 | BFD_ASSERT (elf_program_header_size (abfd) % bed->s->sizeof_phdr |
57268894 | 4586 | == 0); |
a50b1753 NC |
4587 | phdrs = (Elf_Internal_Phdr *) |
4588 | bfd_zalloc2 (abfd, | |
12bd6957 | 4589 | (elf_program_header_size (abfd) / bed->s->sizeof_phdr), |
a50b1753 | 4590 | sizeof (Elf_Internal_Phdr)); |
f3520d2f | 4591 | elf_tdata (abfd)->phdr = phdrs; |
252b5132 | 4592 | if (phdrs == NULL) |
b34976b6 | 4593 | return FALSE; |
252b5132 | 4594 | |
3f570048 AM |
4595 | maxpagesize = 1; |
4596 | if ((abfd->flags & D_PAGED) != 0) | |
4597 | maxpagesize = bed->maxpagesize; | |
4598 | ||
252b5132 RH |
4599 | off = bed->s->sizeof_ehdr; |
4600 | off += alloc * bed->s->sizeof_phdr; | |
2b0bc088 NC |
4601 | if (header_pad < (bfd_vma) off) |
4602 | header_pad = 0; | |
4603 | else | |
4604 | header_pad -= off; | |
4605 | off += header_pad; | |
252b5132 | 4606 | |
12bd6957 | 4607 | for (m = elf_seg_map (abfd), p = phdrs, j = 0; |
252b5132 | 4608 | m != NULL; |
0920dee7 | 4609 | m = m->next, p++, j++) |
252b5132 | 4610 | { |
252b5132 | 4611 | asection **secpp; |
bf988460 AM |
4612 | bfd_vma off_adjust; |
4613 | bfd_boolean no_contents; | |
252b5132 RH |
4614 | |
4615 | /* If elf_segment_map is not from map_sections_to_segments, the | |
08a40648 | 4616 | sections may not be correctly ordered. NOTE: sorting should |
52e9b619 MS |
4617 | not be done to the PT_NOTE section of a corefile, which may |
4618 | contain several pseudo-sections artificially created by bfd. | |
4619 | Sorting these pseudo-sections breaks things badly. */ | |
47d9a591 AM |
4620 | if (m->count > 1 |
4621 | && !(elf_elfheader (abfd)->e_type == ET_CORE | |
52e9b619 | 4622 | && m->p_type == PT_NOTE)) |
252b5132 RH |
4623 | qsort (m->sections, (size_t) m->count, sizeof (asection *), |
4624 | elf_sort_sections); | |
4625 | ||
b301b248 AM |
4626 | /* An ELF segment (described by Elf_Internal_Phdr) may contain a |
4627 | number of sections with contents contributing to both p_filesz | |
4628 | and p_memsz, followed by a number of sections with no contents | |
4629 | that just contribute to p_memsz. In this loop, OFF tracks next | |
02bf8d82 | 4630 | available file offset for PT_LOAD and PT_NOTE segments. */ |
252b5132 | 4631 | p->p_type = m->p_type; |
28a7f3e7 | 4632 | p->p_flags = m->p_flags; |
252b5132 | 4633 | |
3f570048 AM |
4634 | if (m->count == 0) |
4635 | p->p_vaddr = 0; | |
4636 | else | |
3271a814 | 4637 | p->p_vaddr = m->sections[0]->vma - m->p_vaddr_offset; |
3f570048 AM |
4638 | |
4639 | if (m->p_paddr_valid) | |
4640 | p->p_paddr = m->p_paddr; | |
4641 | else if (m->count == 0) | |
4642 | p->p_paddr = 0; | |
4643 | else | |
08a40648 | 4644 | p->p_paddr = m->sections[0]->lma - m->p_vaddr_offset; |
3f570048 AM |
4645 | |
4646 | if (p->p_type == PT_LOAD | |
4647 | && (abfd->flags & D_PAGED) != 0) | |
4648 | { | |
4649 | /* p_align in demand paged PT_LOAD segments effectively stores | |
4650 | the maximum page size. When copying an executable with | |
4651 | objcopy, we set m->p_align from the input file. Use this | |
4652 | value for maxpagesize rather than bed->maxpagesize, which | |
4653 | may be different. Note that we use maxpagesize for PT_TLS | |
4654 | segment alignment later in this function, so we are relying | |
4655 | on at least one PT_LOAD segment appearing before a PT_TLS | |
4656 | segment. */ | |
4657 | if (m->p_align_valid) | |
4658 | maxpagesize = m->p_align; | |
4659 | ||
4660 | p->p_align = maxpagesize; | |
4661 | } | |
3271a814 NS |
4662 | else if (m->p_align_valid) |
4663 | p->p_align = m->p_align; | |
e970b90a DJ |
4664 | else if (m->count == 0) |
4665 | p->p_align = 1 << bed->s->log_file_align; | |
3f570048 AM |
4666 | else |
4667 | p->p_align = 0; | |
4668 | ||
bf988460 AM |
4669 | no_contents = FALSE; |
4670 | off_adjust = 0; | |
252b5132 | 4671 | if (p->p_type == PT_LOAD |
b301b248 | 4672 | && m->count > 0) |
252b5132 | 4673 | { |
b301b248 | 4674 | bfd_size_type align; |
a49e53ed | 4675 | unsigned int align_power = 0; |
b301b248 | 4676 | |
3271a814 NS |
4677 | if (m->p_align_valid) |
4678 | align = p->p_align; | |
4679 | else | |
252b5132 | 4680 | { |
3271a814 NS |
4681 | for (i = 0, secpp = m->sections; i < m->count; i++, secpp++) |
4682 | { | |
4683 | unsigned int secalign; | |
08a40648 | 4684 | |
3271a814 NS |
4685 | secalign = bfd_get_section_alignment (abfd, *secpp); |
4686 | if (secalign > align_power) | |
4687 | align_power = secalign; | |
4688 | } | |
4689 | align = (bfd_size_type) 1 << align_power; | |
4690 | if (align < maxpagesize) | |
4691 | align = maxpagesize; | |
b301b248 | 4692 | } |
252b5132 | 4693 | |
02bf8d82 AM |
4694 | for (i = 0; i < m->count; i++) |
4695 | if ((m->sections[i]->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0) | |
4696 | /* If we aren't making room for this section, then | |
4697 | it must be SHT_NOBITS regardless of what we've | |
4698 | set via struct bfd_elf_special_section. */ | |
4699 | elf_section_type (m->sections[i]) = SHT_NOBITS; | |
4700 | ||
bf988460 | 4701 | /* Find out whether this segment contains any loadable |
aea274d3 AM |
4702 | sections. */ |
4703 | no_contents = TRUE; | |
4704 | for (i = 0; i < m->count; i++) | |
4705 | if (elf_section_type (m->sections[i]) != SHT_NOBITS) | |
4706 | { | |
4707 | no_contents = FALSE; | |
4708 | break; | |
4709 | } | |
bf988460 | 4710 | |
85cfcbfb | 4711 | off_adjust = vma_page_aligned_bias (p->p_vaddr, off, align); |
bf988460 AM |
4712 | off += off_adjust; |
4713 | if (no_contents) | |
4714 | { | |
4715 | /* We shouldn't need to align the segment on disk since | |
4716 | the segment doesn't need file space, but the gABI | |
4717 | arguably requires the alignment and glibc ld.so | |
4718 | checks it. So to comply with the alignment | |
4719 | requirement but not waste file space, we adjust | |
4720 | p_offset for just this segment. (OFF_ADJUST is | |
4721 | subtracted from OFF later.) This may put p_offset | |
4722 | past the end of file, but that shouldn't matter. */ | |
4723 | } | |
4724 | else | |
4725 | off_adjust = 0; | |
252b5132 | 4726 | } |
b1a6d0b1 NC |
4727 | /* Make sure the .dynamic section is the first section in the |
4728 | PT_DYNAMIC segment. */ | |
4729 | else if (p->p_type == PT_DYNAMIC | |
4730 | && m->count > 1 | |
4731 | && strcmp (m->sections[0]->name, ".dynamic") != 0) | |
4732 | { | |
4733 | _bfd_error_handler | |
b301b248 AM |
4734 | (_("%B: The first section in the PT_DYNAMIC segment is not the .dynamic section"), |
4735 | abfd); | |
b1a6d0b1 NC |
4736 | bfd_set_error (bfd_error_bad_value); |
4737 | return FALSE; | |
4738 | } | |
3f001e84 JK |
4739 | /* Set the note section type to SHT_NOTE. */ |
4740 | else if (p->p_type == PT_NOTE) | |
4741 | for (i = 0; i < m->count; i++) | |
4742 | elf_section_type (m->sections[i]) = SHT_NOTE; | |
252b5132 | 4743 | |
252b5132 RH |
4744 | p->p_offset = 0; |
4745 | p->p_filesz = 0; | |
4746 | p->p_memsz = 0; | |
4747 | ||
4748 | if (m->includes_filehdr) | |
4749 | { | |
bf988460 | 4750 | if (!m->p_flags_valid) |
252b5132 | 4751 | p->p_flags |= PF_R; |
252b5132 RH |
4752 | p->p_filesz = bed->s->sizeof_ehdr; |
4753 | p->p_memsz = bed->s->sizeof_ehdr; | |
4754 | if (m->count > 0) | |
4755 | { | |
252b5132 RH |
4756 | if (p->p_vaddr < (bfd_vma) off) |
4757 | { | |
caf47ea6 | 4758 | (*_bfd_error_handler) |
b301b248 AM |
4759 | (_("%B: Not enough room for program headers, try linking with -N"), |
4760 | abfd); | |
252b5132 | 4761 | bfd_set_error (bfd_error_bad_value); |
b34976b6 | 4762 | return FALSE; |
252b5132 RH |
4763 | } |
4764 | ||
4765 | p->p_vaddr -= off; | |
bf988460 | 4766 | if (!m->p_paddr_valid) |
252b5132 RH |
4767 | p->p_paddr -= off; |
4768 | } | |
252b5132 RH |
4769 | } |
4770 | ||
4771 | if (m->includes_phdrs) | |
4772 | { | |
bf988460 | 4773 | if (!m->p_flags_valid) |
252b5132 RH |
4774 | p->p_flags |= PF_R; |
4775 | ||
f3520d2f | 4776 | if (!m->includes_filehdr) |
252b5132 RH |
4777 | { |
4778 | p->p_offset = bed->s->sizeof_ehdr; | |
4779 | ||
4780 | if (m->count > 0) | |
4781 | { | |
252b5132 | 4782 | p->p_vaddr -= off - p->p_offset; |
bf988460 | 4783 | if (!m->p_paddr_valid) |
252b5132 RH |
4784 | p->p_paddr -= off - p->p_offset; |
4785 | } | |
252b5132 RH |
4786 | } |
4787 | ||
4788 | p->p_filesz += alloc * bed->s->sizeof_phdr; | |
4789 | p->p_memsz += alloc * bed->s->sizeof_phdr; | |
2b0bc088 NC |
4790 | if (m->count) |
4791 | { | |
4792 | p->p_filesz += header_pad; | |
4793 | p->p_memsz += header_pad; | |
4794 | } | |
252b5132 RH |
4795 | } |
4796 | ||
4797 | if (p->p_type == PT_LOAD | |
4798 | || (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core)) | |
4799 | { | |
bf988460 | 4800 | if (!m->includes_filehdr && !m->includes_phdrs) |
02bf8d82 | 4801 | p->p_offset = off; |
252b5132 RH |
4802 | else |
4803 | { | |
4804 | file_ptr adjust; | |
4805 | ||
4806 | adjust = off - (p->p_offset + p->p_filesz); | |
bf988460 AM |
4807 | if (!no_contents) |
4808 | p->p_filesz += adjust; | |
252b5132 RH |
4809 | p->p_memsz += adjust; |
4810 | } | |
4811 | } | |
4812 | ||
1ea63fd2 AM |
4813 | /* Set up p_filesz, p_memsz, p_align and p_flags from the section |
4814 | maps. Set filepos for sections in PT_LOAD segments, and in | |
4815 | core files, for sections in PT_NOTE segments. | |
4816 | assign_file_positions_for_non_load_sections will set filepos | |
4817 | for other sections and update p_filesz for other segments. */ | |
252b5132 RH |
4818 | for (i = 0, secpp = m->sections; i < m->count; i++, secpp++) |
4819 | { | |
4820 | asection *sec; | |
252b5132 | 4821 | bfd_size_type align; |
627b32bc | 4822 | Elf_Internal_Shdr *this_hdr; |
252b5132 RH |
4823 | |
4824 | sec = *secpp; | |
02bf8d82 | 4825 | this_hdr = &elf_section_data (sec)->this_hdr; |
3f570048 | 4826 | align = (bfd_size_type) 1 << bfd_get_section_alignment (abfd, sec); |
252b5132 | 4827 | |
88967714 AM |
4828 | if ((p->p_type == PT_LOAD |
4829 | || p->p_type == PT_TLS) | |
4830 | && (this_hdr->sh_type != SHT_NOBITS | |
4831 | || ((this_hdr->sh_flags & SHF_ALLOC) != 0 | |
4832 | && ((this_hdr->sh_flags & SHF_TLS) == 0 | |
4833 | || p->p_type == PT_TLS)))) | |
252b5132 | 4834 | { |
b5599592 AM |
4835 | bfd_vma p_start = p->p_paddr; |
4836 | bfd_vma p_end = p_start + p->p_memsz; | |
4837 | bfd_vma s_start = sec->lma; | |
4838 | bfd_vma adjust = s_start - p_end; | |
252b5132 | 4839 | |
a2d1e028 L |
4840 | if (adjust != 0 |
4841 | && (s_start < p_end | |
4842 | || p_end < p_start)) | |
252b5132 | 4843 | { |
88967714 | 4844 | (*_bfd_error_handler) |
b5599592 AM |
4845 | (_("%B: section %A lma %#lx adjusted to %#lx"), abfd, sec, |
4846 | (unsigned long) s_start, (unsigned long) p_end); | |
88967714 | 4847 | adjust = 0; |
b5599592 | 4848 | sec->lma = p_end; |
1cfb7d1e | 4849 | } |
3ac9b6c9 | 4850 | p->p_memsz += adjust; |
1cfb7d1e | 4851 | |
88967714 AM |
4852 | if (this_hdr->sh_type != SHT_NOBITS) |
4853 | { | |
32812159 AM |
4854 | if (p->p_filesz + adjust < p->p_memsz) |
4855 | { | |
4856 | /* We have a PROGBITS section following NOBITS ones. | |
4857 | Allocate file space for the NOBITS section(s) and | |
4858 | zero it. */ | |
4859 | adjust = p->p_memsz - p->p_filesz; | |
4860 | if (!write_zeros (abfd, off, adjust)) | |
4861 | return FALSE; | |
4862 | } | |
88967714 AM |
4863 | off += adjust; |
4864 | p->p_filesz += adjust; | |
252b5132 | 4865 | } |
252b5132 RH |
4866 | } |
4867 | ||
4868 | if (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core) | |
4869 | { | |
b301b248 AM |
4870 | /* The section at i == 0 is the one that actually contains |
4871 | everything. */ | |
4a938328 MS |
4872 | if (i == 0) |
4873 | { | |
627b32bc | 4874 | this_hdr->sh_offset = sec->filepos = off; |
6a3cd2b4 AM |
4875 | off += this_hdr->sh_size; |
4876 | p->p_filesz = this_hdr->sh_size; | |
b301b248 AM |
4877 | p->p_memsz = 0; |
4878 | p->p_align = 1; | |
252b5132 | 4879 | } |
4a938328 | 4880 | else |
252b5132 | 4881 | { |
b301b248 | 4882 | /* The rest are fake sections that shouldn't be written. */ |
252b5132 | 4883 | sec->filepos = 0; |
eea6121a | 4884 | sec->size = 0; |
b301b248 AM |
4885 | sec->flags = 0; |
4886 | continue; | |
252b5132 | 4887 | } |
252b5132 RH |
4888 | } |
4889 | else | |
4890 | { | |
1e951488 | 4891 | if (p->p_type == PT_LOAD) |
b301b248 | 4892 | { |
1e951488 AM |
4893 | this_hdr->sh_offset = sec->filepos = off; |
4894 | if (this_hdr->sh_type != SHT_NOBITS) | |
4895 | off += this_hdr->sh_size; | |
4896 | } | |
4897 | else if (this_hdr->sh_type == SHT_NOBITS | |
4898 | && (this_hdr->sh_flags & SHF_TLS) != 0 | |
4899 | && this_hdr->sh_offset == 0) | |
4900 | { | |
4901 | /* This is a .tbss section that didn't get a PT_LOAD. | |
4902 | (See _bfd_elf_map_sections_to_segments "Create a | |
4903 | final PT_LOAD".) Set sh_offset to the value it | |
4904 | would have if we had created a zero p_filesz and | |
4905 | p_memsz PT_LOAD header for the section. This | |
4906 | also makes the PT_TLS header have the same | |
4907 | p_offset value. */ | |
4908 | bfd_vma adjust = vma_page_aligned_bias (this_hdr->sh_addr, | |
4909 | off, align); | |
4910 | this_hdr->sh_offset = sec->filepos = off + adjust; | |
b301b248 | 4911 | } |
252b5132 | 4912 | |
02bf8d82 | 4913 | if (this_hdr->sh_type != SHT_NOBITS) |
b301b248 | 4914 | { |
6a3cd2b4 | 4915 | p->p_filesz += this_hdr->sh_size; |
02bf8d82 AM |
4916 | /* A load section without SHF_ALLOC is something like |
4917 | a note section in a PT_NOTE segment. These take | |
4918 | file space but are not loaded into memory. */ | |
4919 | if ((this_hdr->sh_flags & SHF_ALLOC) != 0) | |
6a3cd2b4 | 4920 | p->p_memsz += this_hdr->sh_size; |
b301b248 | 4921 | } |
6a3cd2b4 | 4922 | else if ((this_hdr->sh_flags & SHF_ALLOC) != 0) |
13ae64f3 | 4923 | { |
6a3cd2b4 AM |
4924 | if (p->p_type == PT_TLS) |
4925 | p->p_memsz += this_hdr->sh_size; | |
4926 | ||
4927 | /* .tbss is special. It doesn't contribute to p_memsz of | |
4928 | normal segments. */ | |
4929 | else if ((this_hdr->sh_flags & SHF_TLS) == 0) | |
4930 | p->p_memsz += this_hdr->sh_size; | |
13ae64f3 JJ |
4931 | } |
4932 | ||
b10a8ae0 L |
4933 | if (align > p->p_align |
4934 | && !m->p_align_valid | |
4935 | && (p->p_type != PT_LOAD | |
4936 | || (abfd->flags & D_PAGED) == 0)) | |
252b5132 RH |
4937 | p->p_align = align; |
4938 | } | |
4939 | ||
bf988460 | 4940 | if (!m->p_flags_valid) |
252b5132 RH |
4941 | { |
4942 | p->p_flags |= PF_R; | |
02bf8d82 | 4943 | if ((this_hdr->sh_flags & SHF_EXECINSTR) != 0) |
252b5132 | 4944 | p->p_flags |= PF_X; |
02bf8d82 | 4945 | if ((this_hdr->sh_flags & SHF_WRITE) != 0) |
252b5132 RH |
4946 | p->p_flags |= PF_W; |
4947 | } | |
4948 | } | |
43a8475c | 4949 | |
bf988460 | 4950 | off -= off_adjust; |
0920dee7 | 4951 | |
7c928300 AM |
4952 | /* Check that all sections are in a PT_LOAD segment. |
4953 | Don't check funky gdb generated core files. */ | |
4954 | if (p->p_type == PT_LOAD && bfd_get_format (abfd) != bfd_core) | |
9a83a553 AM |
4955 | { |
4956 | bfd_boolean check_vma = TRUE; | |
4957 | ||
4958 | for (i = 1; i < m->count; i++) | |
4959 | if (m->sections[i]->vma == m->sections[i - 1]->vma | |
4960 | && ELF_SECTION_SIZE (&(elf_section_data (m->sections[i]) | |
4961 | ->this_hdr), p) != 0 | |
4962 | && ELF_SECTION_SIZE (&(elf_section_data (m->sections[i - 1]) | |
4963 | ->this_hdr), p) != 0) | |
0920dee7 | 4964 | { |
9a83a553 AM |
4965 | /* Looks like we have overlays packed into the segment. */ |
4966 | check_vma = FALSE; | |
4967 | break; | |
0920dee7 | 4968 | } |
9a83a553 AM |
4969 | |
4970 | for (i = 0; i < m->count; i++) | |
4971 | { | |
4972 | Elf_Internal_Shdr *this_hdr; | |
4973 | asection *sec; | |
4974 | ||
4975 | sec = m->sections[i]; | |
4976 | this_hdr = &(elf_section_data(sec)->this_hdr); | |
86b2281f AM |
4977 | if (!ELF_SECTION_IN_SEGMENT_1 (this_hdr, p, check_vma, 0) |
4978 | && !ELF_TBSS_SPECIAL (this_hdr, p)) | |
9a83a553 AM |
4979 | { |
4980 | (*_bfd_error_handler) | |
4981 | (_("%B: section `%A' can't be allocated in segment %d"), | |
4982 | abfd, sec, j); | |
4983 | print_segment_map (m); | |
4984 | } | |
4985 | } | |
4986 | } | |
252b5132 RH |
4987 | } |
4988 | ||
12bd6957 | 4989 | elf_next_file_pos (abfd) = off; |
f3520d2f AM |
4990 | return TRUE; |
4991 | } | |
4992 | ||
4993 | /* Assign file positions for the other sections. */ | |
4994 | ||
4995 | static bfd_boolean | |
4996 | assign_file_positions_for_non_load_sections (bfd *abfd, | |
4997 | struct bfd_link_info *link_info) | |
4998 | { | |
4999 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
5000 | Elf_Internal_Shdr **i_shdrpp; | |
5001 | Elf_Internal_Shdr **hdrpp; | |
5002 | Elf_Internal_Phdr *phdrs; | |
5003 | Elf_Internal_Phdr *p; | |
5004 | struct elf_segment_map *m; | |
62655c7b | 5005 | struct elf_segment_map *hdrs_segment; |
f3520d2f AM |
5006 | bfd_vma filehdr_vaddr, filehdr_paddr; |
5007 | bfd_vma phdrs_vaddr, phdrs_paddr; | |
5008 | file_ptr off; | |
5009 | unsigned int num_sec; | |
5010 | unsigned int i; | |
5011 | unsigned int count; | |
5012 | ||
5c182d5f AM |
5013 | i_shdrpp = elf_elfsections (abfd); |
5014 | num_sec = elf_numsections (abfd); | |
12bd6957 | 5015 | off = elf_next_file_pos (abfd); |
5c182d5f AM |
5016 | for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++) |
5017 | { | |
5c182d5f AM |
5018 | Elf_Internal_Shdr *hdr; |
5019 | ||
5020 | hdr = *hdrpp; | |
5021 | if (hdr->bfd_section != NULL | |
252e386e AM |
5022 | && (hdr->bfd_section->filepos != 0 |
5023 | || (hdr->sh_type == SHT_NOBITS | |
5024 | && hdr->contents == NULL))) | |
627b32bc | 5025 | BFD_ASSERT (hdr->sh_offset == hdr->bfd_section->filepos); |
5c182d5f AM |
5026 | else if ((hdr->sh_flags & SHF_ALLOC) != 0) |
5027 | { | |
e8d2ba53 AM |
5028 | if (hdr->sh_size != 0) |
5029 | (*_bfd_error_handler) | |
5030 | (_("%B: warning: allocated section `%s' not in segment"), | |
5031 | abfd, | |
5032 | (hdr->bfd_section == NULL | |
5033 | ? "*unknown*" | |
5034 | : hdr->bfd_section->name)); | |
3ba71138 L |
5035 | /* We don't need to page align empty sections. */ |
5036 | if ((abfd->flags & D_PAGED) != 0 && hdr->sh_size != 0) | |
5c182d5f AM |
5037 | off += vma_page_aligned_bias (hdr->sh_addr, off, |
5038 | bed->maxpagesize); | |
5039 | else | |
5040 | off += vma_page_aligned_bias (hdr->sh_addr, off, | |
5041 | hdr->sh_addralign); | |
5042 | off = _bfd_elf_assign_file_position_for_section (hdr, off, | |
5043 | FALSE); | |
5044 | } | |
5045 | else if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA) | |
5046 | && hdr->bfd_section == NULL) | |
12bd6957 AM |
5047 | || hdr == i_shdrpp[elf_onesymtab (abfd)] |
5048 | || hdr == i_shdrpp[elf_symtab_shndx (abfd)] | |
5049 | || hdr == i_shdrpp[elf_strtab_sec (abfd)]) | |
5c182d5f AM |
5050 | hdr->sh_offset = -1; |
5051 | else | |
5052 | off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE); | |
5c182d5f AM |
5053 | } |
5054 | ||
252b5132 RH |
5055 | /* Now that we have set the section file positions, we can set up |
5056 | the file positions for the non PT_LOAD segments. */ | |
f3520d2f AM |
5057 | count = 0; |
5058 | filehdr_vaddr = 0; | |
5059 | filehdr_paddr = 0; | |
5060 | phdrs_vaddr = bed->maxpagesize + bed->s->sizeof_ehdr; | |
5061 | phdrs_paddr = 0; | |
62655c7b | 5062 | hdrs_segment = NULL; |
f3520d2f | 5063 | phdrs = elf_tdata (abfd)->phdr; |
12bd6957 | 5064 | for (m = elf_seg_map (abfd), p = phdrs; m != NULL; m = m->next, p++) |
f3520d2f AM |
5065 | { |
5066 | ++count; | |
5067 | if (p->p_type != PT_LOAD) | |
5068 | continue; | |
5069 | ||
5070 | if (m->includes_filehdr) | |
5071 | { | |
5072 | filehdr_vaddr = p->p_vaddr; | |
5073 | filehdr_paddr = p->p_paddr; | |
5074 | } | |
5075 | if (m->includes_phdrs) | |
5076 | { | |
5077 | phdrs_vaddr = p->p_vaddr; | |
5078 | phdrs_paddr = p->p_paddr; | |
5079 | if (m->includes_filehdr) | |
5080 | { | |
62655c7b | 5081 | hdrs_segment = m; |
f3520d2f AM |
5082 | phdrs_vaddr += bed->s->sizeof_ehdr; |
5083 | phdrs_paddr += bed->s->sizeof_ehdr; | |
5084 | } | |
5085 | } | |
5086 | } | |
5087 | ||
62655c7b RM |
5088 | if (hdrs_segment != NULL && link_info != NULL) |
5089 | { | |
5090 | /* There is a segment that contains both the file headers and the | |
5091 | program headers, so provide a symbol __ehdr_start pointing there. | |
5092 | A program can use this to examine itself robustly. */ | |
5093 | ||
5094 | struct elf_link_hash_entry *hash | |
5095 | = elf_link_hash_lookup (elf_hash_table (link_info), "__ehdr_start", | |
5096 | FALSE, FALSE, TRUE); | |
5097 | /* If the symbol was referenced and not defined, define it. */ | |
5098 | if (hash != NULL | |
5099 | && (hash->root.type == bfd_link_hash_new | |
5100 | || hash->root.type == bfd_link_hash_undefined | |
5101 | || hash->root.type == bfd_link_hash_undefweak | |
5102 | || hash->root.type == bfd_link_hash_common)) | |
5103 | { | |
5104 | asection *s = NULL; | |
5105 | if (hdrs_segment->count != 0) | |
5106 | /* The segment contains sections, so use the first one. */ | |
5107 | s = hdrs_segment->sections[0]; | |
5108 | else | |
5109 | /* Use the first (i.e. lowest-addressed) section in any segment. */ | |
12bd6957 | 5110 | for (m = elf_seg_map (abfd); m != NULL; m = m->next) |
62655c7b RM |
5111 | if (m->count != 0) |
5112 | { | |
5113 | s = m->sections[0]; | |
5114 | break; | |
5115 | } | |
5116 | ||
5117 | if (s != NULL) | |
5118 | { | |
5119 | hash->root.u.def.value = filehdr_vaddr - s->vma; | |
5120 | hash->root.u.def.section = s; | |
5121 | } | |
5122 | else | |
5123 | { | |
5124 | hash->root.u.def.value = filehdr_vaddr; | |
5125 | hash->root.u.def.section = bfd_abs_section_ptr; | |
5126 | } | |
5127 | ||
5128 | hash->root.type = bfd_link_hash_defined; | |
5129 | hash->def_regular = 1; | |
5130 | hash->non_elf = 0; | |
5131 | } | |
5132 | } | |
5133 | ||
12bd6957 | 5134 | for (m = elf_seg_map (abfd), p = phdrs; m != NULL; m = m->next, p++) |
252b5132 | 5135 | { |
129af99f | 5136 | if (p->p_type == PT_GNU_RELRO) |
252b5132 | 5137 | { |
b84a33b5 | 5138 | const Elf_Internal_Phdr *lp; |
3146fac4 | 5139 | struct elf_segment_map *lm; |
1ea63fd2 | 5140 | |
129af99f | 5141 | if (link_info != NULL) |
8c37241b | 5142 | { |
129af99f AS |
5143 | /* During linking the range of the RELRO segment is passed |
5144 | in link_info. */ | |
12bd6957 | 5145 | for (lm = elf_seg_map (abfd), lp = phdrs; |
3146fac4 AM |
5146 | lm != NULL; |
5147 | lm = lm->next, lp++) | |
8c37241b JJ |
5148 | { |
5149 | if (lp->p_type == PT_LOAD | |
b84a33b5 | 5150 | && lp->p_vaddr < link_info->relro_end |
3146fac4 AM |
5151 | && lm->count != 0 |
5152 | && lm->sections[0]->vma >= link_info->relro_start) | |
8c37241b JJ |
5153 | break; |
5154 | } | |
8981c88a | 5155 | |
3146fac4 | 5156 | BFD_ASSERT (lm != NULL); |
8c37241b | 5157 | } |
129af99f AS |
5158 | else |
5159 | { | |
5160 | /* Otherwise we are copying an executable or shared | |
b84a33b5 | 5161 | library, but we need to use the same linker logic. */ |
129af99f AS |
5162 | for (lp = phdrs; lp < phdrs + count; ++lp) |
5163 | { | |
5164 | if (lp->p_type == PT_LOAD | |
5165 | && lp->p_paddr == p->p_paddr) | |
5166 | break; | |
5167 | } | |
b84a33b5 AM |
5168 | } |
5169 | ||
5170 | if (lp < phdrs + count) | |
5171 | { | |
5172 | p->p_vaddr = lp->p_vaddr; | |
5173 | p->p_paddr = lp->p_paddr; | |
5174 | p->p_offset = lp->p_offset; | |
5175 | if (link_info != NULL) | |
5176 | p->p_filesz = link_info->relro_end - lp->p_vaddr; | |
5177 | else if (m->p_size_valid) | |
5178 | p->p_filesz = m->p_size; | |
129af99f AS |
5179 | else |
5180 | abort (); | |
b84a33b5 | 5181 | p->p_memsz = p->p_filesz; |
f3944f72 L |
5182 | /* Preserve the alignment and flags if they are valid. The |
5183 | gold linker generates RW/4 for the PT_GNU_RELRO section. | |
5184 | It is better for objcopy/strip to honor these attributes | |
5185 | otherwise gdb will choke when using separate debug files. | |
5186 | */ | |
5187 | if (!m->p_align_valid) | |
5188 | p->p_align = 1; | |
5189 | if (!m->p_flags_valid) | |
5190 | p->p_flags = (lp->p_flags & ~PF_W); | |
129af99f | 5191 | } |
9433b9b1 | 5192 | else |
b84a33b5 AM |
5193 | { |
5194 | memset (p, 0, sizeof *p); | |
5195 | p->p_type = PT_NULL; | |
5196 | } | |
129af99f | 5197 | } |
04c3a755 NS |
5198 | else if (p->p_type == PT_GNU_STACK) |
5199 | { | |
5200 | if (m->p_size_valid) | |
5201 | p->p_memsz = m->p_size; | |
5202 | } | |
129af99f AS |
5203 | else if (m->count != 0) |
5204 | { | |
5205 | if (p->p_type != PT_LOAD | |
5206 | && (p->p_type != PT_NOTE | |
5207 | || bfd_get_format (abfd) != bfd_core)) | |
5208 | { | |
129af99f AS |
5209 | BFD_ASSERT (!m->includes_filehdr && !m->includes_phdrs); |
5210 | ||
86b2281f | 5211 | p->p_filesz = 0; |
129af99f | 5212 | p->p_offset = m->sections[0]->filepos; |
86b2281f AM |
5213 | for (i = m->count; i-- != 0;) |
5214 | { | |
5215 | asection *sect = m->sections[i]; | |
5216 | Elf_Internal_Shdr *hdr = &elf_section_data (sect)->this_hdr; | |
5217 | if (hdr->sh_type != SHT_NOBITS) | |
5218 | { | |
5219 | p->p_filesz = (sect->filepos - m->sections[0]->filepos | |
5220 | + hdr->sh_size); | |
5221 | break; | |
5222 | } | |
5223 | } | |
129af99f AS |
5224 | } |
5225 | } | |
5226 | else if (m->includes_filehdr) | |
5227 | { | |
5228 | p->p_vaddr = filehdr_vaddr; | |
5229 | if (! m->p_paddr_valid) | |
5230 | p->p_paddr = filehdr_paddr; | |
5231 | } | |
5232 | else if (m->includes_phdrs) | |
5233 | { | |
5234 | p->p_vaddr = phdrs_vaddr; | |
5235 | if (! m->p_paddr_valid) | |
5236 | p->p_paddr = phdrs_paddr; | |
252b5132 RH |
5237 | } |
5238 | } | |
5239 | ||
12bd6957 | 5240 | elf_next_file_pos (abfd) = off; |
252b5132 | 5241 | |
b34976b6 | 5242 | return TRUE; |
252b5132 RH |
5243 | } |
5244 | ||
252b5132 RH |
5245 | /* Work out the file positions of all the sections. This is called by |
5246 | _bfd_elf_compute_section_file_positions. All the section sizes and | |
5247 | VMAs must be known before this is called. | |
5248 | ||
e0638f70 AM |
5249 | Reloc sections come in two flavours: Those processed specially as |
5250 | "side-channel" data attached to a section to which they apply, and | |
5251 | those that bfd doesn't process as relocations. The latter sort are | |
5252 | stored in a normal bfd section by bfd_section_from_shdr. We don't | |
5253 | consider the former sort here, unless they form part of the loadable | |
5254 | image. Reloc sections not assigned here will be handled later by | |
5255 | assign_file_positions_for_relocs. | |
252b5132 RH |
5256 | |
5257 | We also don't set the positions of the .symtab and .strtab here. */ | |
5258 | ||
b34976b6 | 5259 | static bfd_boolean |
c84fca4d AO |
5260 | assign_file_positions_except_relocs (bfd *abfd, |
5261 | struct bfd_link_info *link_info) | |
252b5132 | 5262 | { |
5c182d5f AM |
5263 | struct elf_obj_tdata *tdata = elf_tdata (abfd); |
5264 | Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd); | |
9c5bfbb7 | 5265 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
252b5132 RH |
5266 | |
5267 | if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0 | |
5268 | && bfd_get_format (abfd) != bfd_core) | |
5269 | { | |
5c182d5f AM |
5270 | Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd); |
5271 | unsigned int num_sec = elf_numsections (abfd); | |
252b5132 RH |
5272 | Elf_Internal_Shdr **hdrpp; |
5273 | unsigned int i; | |
a485e98e | 5274 | file_ptr off; |
252b5132 RH |
5275 | |
5276 | /* Start after the ELF header. */ | |
5277 | off = i_ehdrp->e_ehsize; | |
5278 | ||
5279 | /* We are not creating an executable, which means that we are | |
5280 | not creating a program header, and that the actual order of | |
5281 | the sections in the file is unimportant. */ | |
9ad5cbcf | 5282 | for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++) |
252b5132 RH |
5283 | { |
5284 | Elf_Internal_Shdr *hdr; | |
5285 | ||
5286 | hdr = *hdrpp; | |
e0638f70 AM |
5287 | if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA) |
5288 | && hdr->bfd_section == NULL) | |
12bd6957 AM |
5289 | || i == elf_onesymtab (abfd) |
5290 | || i == elf_symtab_shndx (abfd) | |
5291 | || i == elf_strtab_sec (abfd)) | |
252b5132 RH |
5292 | { |
5293 | hdr->sh_offset = -1; | |
252b5132 | 5294 | } |
9ad5cbcf | 5295 | else |
b34976b6 | 5296 | off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE); |
252b5132 | 5297 | } |
a485e98e AM |
5298 | |
5299 | elf_next_file_pos (abfd) = off; | |
252b5132 RH |
5300 | } |
5301 | else | |
5302 | { | |
f3520d2f AM |
5303 | unsigned int alloc; |
5304 | ||
252b5132 | 5305 | /* Assign file positions for the loaded sections based on the |
08a40648 | 5306 | assignment of sections to segments. */ |
f3520d2f AM |
5307 | if (!assign_file_positions_for_load_sections (abfd, link_info)) |
5308 | return FALSE; | |
5309 | ||
5310 | /* And for non-load sections. */ | |
5311 | if (!assign_file_positions_for_non_load_sections (abfd, link_info)) | |
5312 | return FALSE; | |
5313 | ||
e36284ab AM |
5314 | if (bed->elf_backend_modify_program_headers != NULL) |
5315 | { | |
5316 | if (!(*bed->elf_backend_modify_program_headers) (abfd, link_info)) | |
5317 | return FALSE; | |
5318 | } | |
5319 | ||
58e7ebac L |
5320 | /* Set e_type in ELF header to ET_EXEC for -pie -Ttext-segment=. */ |
5321 | if (link_info != NULL | |
5322 | && link_info->executable | |
5323 | && link_info->shared) | |
5324 | { | |
5325 | unsigned int num_segments = elf_elfheader (abfd)->e_phnum; | |
5326 | Elf_Internal_Phdr *segment = elf_tdata (abfd)->phdr; | |
5327 | Elf_Internal_Phdr *end_segment = &segment[num_segments]; | |
5328 | ||
5329 | /* Find the lowest p_vaddr in PT_LOAD segments. */ | |
5330 | bfd_vma p_vaddr = (bfd_vma) -1; | |
5331 | for (; segment < end_segment; segment++) | |
5332 | if (segment->p_type == PT_LOAD && p_vaddr > segment->p_vaddr) | |
5333 | p_vaddr = segment->p_vaddr; | |
5334 | ||
5335 | /* Set e_type to ET_EXEC if the lowest p_vaddr in PT_LOAD | |
5336 | segments is non-zero. */ | |
5337 | if (p_vaddr) | |
5338 | i_ehdrp->e_type = ET_EXEC; | |
5339 | } | |
5340 | ||
f3520d2f | 5341 | /* Write out the program headers. */ |
12bd6957 | 5342 | alloc = elf_program_header_size (abfd) / bed->s->sizeof_phdr; |
f3520d2f AM |
5343 | if (bfd_seek (abfd, (bfd_signed_vma) bed->s->sizeof_ehdr, SEEK_SET) != 0 |
5344 | || bed->s->write_out_phdrs (abfd, tdata->phdr, alloc) != 0) | |
b34976b6 | 5345 | return FALSE; |
252b5132 RH |
5346 | } |
5347 | ||
b34976b6 | 5348 | return TRUE; |
252b5132 RH |
5349 | } |
5350 | ||
b34976b6 | 5351 | static bfd_boolean |
217aa764 | 5352 | prep_headers (bfd *abfd) |
252b5132 | 5353 | { |
3d540e93 | 5354 | Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form. */ |
2b0f7ef9 | 5355 | struct elf_strtab_hash *shstrtab; |
9c5bfbb7 | 5356 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
252b5132 RH |
5357 | |
5358 | i_ehdrp = elf_elfheader (abfd); | |
252b5132 | 5359 | |
2b0f7ef9 | 5360 | shstrtab = _bfd_elf_strtab_init (); |
252b5132 | 5361 | if (shstrtab == NULL) |
b34976b6 | 5362 | return FALSE; |
252b5132 RH |
5363 | |
5364 | elf_shstrtab (abfd) = shstrtab; | |
5365 | ||
5366 | i_ehdrp->e_ident[EI_MAG0] = ELFMAG0; | |
5367 | i_ehdrp->e_ident[EI_MAG1] = ELFMAG1; | |
5368 | i_ehdrp->e_ident[EI_MAG2] = ELFMAG2; | |
5369 | i_ehdrp->e_ident[EI_MAG3] = ELFMAG3; | |
5370 | ||
5371 | i_ehdrp->e_ident[EI_CLASS] = bed->s->elfclass; | |
5372 | i_ehdrp->e_ident[EI_DATA] = | |
5373 | bfd_big_endian (abfd) ? ELFDATA2MSB : ELFDATA2LSB; | |
5374 | i_ehdrp->e_ident[EI_VERSION] = bed->s->ev_current; | |
5375 | ||
252b5132 RH |
5376 | if ((abfd->flags & DYNAMIC) != 0) |
5377 | i_ehdrp->e_type = ET_DYN; | |
5378 | else if ((abfd->flags & EXEC_P) != 0) | |
5379 | i_ehdrp->e_type = ET_EXEC; | |
5380 | else if (bfd_get_format (abfd) == bfd_core) | |
5381 | i_ehdrp->e_type = ET_CORE; | |
5382 | else | |
5383 | i_ehdrp->e_type = ET_REL; | |
5384 | ||
5385 | switch (bfd_get_arch (abfd)) | |
5386 | { | |
5387 | case bfd_arch_unknown: | |
5388 | i_ehdrp->e_machine = EM_NONE; | |
5389 | break; | |
aa4f99bb AO |
5390 | |
5391 | /* There used to be a long list of cases here, each one setting | |
5392 | e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE | |
5393 | in the corresponding bfd definition. To avoid duplication, | |
5394 | the switch was removed. Machines that need special handling | |
5395 | can generally do it in elf_backend_final_write_processing(), | |
5396 | unless they need the information earlier than the final write. | |
5397 | Such need can generally be supplied by replacing the tests for | |
5398 | e_machine with the conditions used to determine it. */ | |
252b5132 | 5399 | default: |
9c5bfbb7 AM |
5400 | i_ehdrp->e_machine = bed->elf_machine_code; |
5401 | } | |
aa4f99bb | 5402 | |
252b5132 RH |
5403 | i_ehdrp->e_version = bed->s->ev_current; |
5404 | i_ehdrp->e_ehsize = bed->s->sizeof_ehdr; | |
5405 | ||
c044fabd | 5406 | /* No program header, for now. */ |
252b5132 RH |
5407 | i_ehdrp->e_phoff = 0; |
5408 | i_ehdrp->e_phentsize = 0; | |
5409 | i_ehdrp->e_phnum = 0; | |
5410 | ||
c044fabd | 5411 | /* Each bfd section is section header entry. */ |
252b5132 RH |
5412 | i_ehdrp->e_entry = bfd_get_start_address (abfd); |
5413 | i_ehdrp->e_shentsize = bed->s->sizeof_shdr; | |
5414 | ||
c044fabd | 5415 | /* If we're building an executable, we'll need a program header table. */ |
252b5132 | 5416 | if (abfd->flags & EXEC_P) |
0e71e495 BE |
5417 | /* It all happens later. */ |
5418 | ; | |
252b5132 RH |
5419 | else |
5420 | { | |
5421 | i_ehdrp->e_phentsize = 0; | |
252b5132 RH |
5422 | i_ehdrp->e_phoff = 0; |
5423 | } | |
5424 | ||
5425 | elf_tdata (abfd)->symtab_hdr.sh_name = | |
b34976b6 | 5426 | (unsigned int) _bfd_elf_strtab_add (shstrtab, ".symtab", FALSE); |
252b5132 | 5427 | elf_tdata (abfd)->strtab_hdr.sh_name = |
b34976b6 | 5428 | (unsigned int) _bfd_elf_strtab_add (shstrtab, ".strtab", FALSE); |
252b5132 | 5429 | elf_tdata (abfd)->shstrtab_hdr.sh_name = |
b34976b6 | 5430 | (unsigned int) _bfd_elf_strtab_add (shstrtab, ".shstrtab", FALSE); |
252b5132 | 5431 | if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1 |
17ca87fc | 5432 | || elf_tdata (abfd)->strtab_hdr.sh_name == (unsigned int) -1 |
252b5132 | 5433 | || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1) |
b34976b6 | 5434 | return FALSE; |
252b5132 | 5435 | |
b34976b6 | 5436 | return TRUE; |
252b5132 RH |
5437 | } |
5438 | ||
5439 | /* Assign file positions for all the reloc sections which are not part | |
a485e98e | 5440 | of the loadable file image, and the file position of section headers. */ |
252b5132 | 5441 | |
a485e98e | 5442 | static void |
217aa764 | 5443 | _bfd_elf_assign_file_positions_for_relocs (bfd *abfd) |
252b5132 RH |
5444 | { |
5445 | file_ptr off; | |
9ad5cbcf | 5446 | unsigned int i, num_sec; |
252b5132 | 5447 | Elf_Internal_Shdr **shdrpp; |
a485e98e AM |
5448 | Elf_Internal_Ehdr *i_ehdrp; |
5449 | const struct elf_backend_data *bed; | |
252b5132 | 5450 | |
12bd6957 | 5451 | off = elf_next_file_pos (abfd); |
252b5132 | 5452 | |
9ad5cbcf AM |
5453 | num_sec = elf_numsections (abfd); |
5454 | for (i = 1, shdrpp = elf_elfsections (abfd) + 1; i < num_sec; i++, shdrpp++) | |
252b5132 RH |
5455 | { |
5456 | Elf_Internal_Shdr *shdrp; | |
5457 | ||
5458 | shdrp = *shdrpp; | |
5459 | if ((shdrp->sh_type == SHT_REL || shdrp->sh_type == SHT_RELA) | |
5460 | && shdrp->sh_offset == -1) | |
b34976b6 | 5461 | off = _bfd_elf_assign_file_position_for_section (shdrp, off, TRUE); |
252b5132 RH |
5462 | } |
5463 | ||
a485e98e AM |
5464 | /* Place the section headers. */ |
5465 | i_ehdrp = elf_elfheader (abfd); | |
5466 | bed = get_elf_backend_data (abfd); | |
5467 | off = align_file_position (off, 1 << bed->s->log_file_align); | |
5468 | i_ehdrp->e_shoff = off; | |
5469 | off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize; | |
12bd6957 | 5470 | elf_next_file_pos (abfd) = off; |
252b5132 RH |
5471 | } |
5472 | ||
b34976b6 | 5473 | bfd_boolean |
217aa764 | 5474 | _bfd_elf_write_object_contents (bfd *abfd) |
252b5132 | 5475 | { |
9c5bfbb7 | 5476 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
252b5132 | 5477 | Elf_Internal_Shdr **i_shdrp; |
b34976b6 | 5478 | bfd_boolean failed; |
9ad5cbcf | 5479 | unsigned int count, num_sec; |
30e8ee25 | 5480 | struct elf_obj_tdata *t; |
252b5132 RH |
5481 | |
5482 | if (! abfd->output_has_begun | |
217aa764 | 5483 | && ! _bfd_elf_compute_section_file_positions (abfd, NULL)) |
b34976b6 | 5484 | return FALSE; |
252b5132 RH |
5485 | |
5486 | i_shdrp = elf_elfsections (abfd); | |
252b5132 | 5487 | |
b34976b6 | 5488 | failed = FALSE; |
252b5132 RH |
5489 | bfd_map_over_sections (abfd, bed->s->write_relocs, &failed); |
5490 | if (failed) | |
b34976b6 | 5491 | return FALSE; |
252b5132 RH |
5492 | |
5493 | _bfd_elf_assign_file_positions_for_relocs (abfd); | |
5494 | ||
c044fabd | 5495 | /* After writing the headers, we need to write the sections too... */ |
9ad5cbcf AM |
5496 | num_sec = elf_numsections (abfd); |
5497 | for (count = 1; count < num_sec; count++) | |
252b5132 RH |
5498 | { |
5499 | if (bed->elf_backend_section_processing) | |
5500 | (*bed->elf_backend_section_processing) (abfd, i_shdrp[count]); | |
5501 | if (i_shdrp[count]->contents) | |
5502 | { | |
dc810e39 AM |
5503 | bfd_size_type amt = i_shdrp[count]->sh_size; |
5504 | ||
252b5132 | 5505 | if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0 |
dc810e39 | 5506 | || bfd_bwrite (i_shdrp[count]->contents, amt, abfd) != amt) |
b34976b6 | 5507 | return FALSE; |
252b5132 RH |
5508 | } |
5509 | } | |
5510 | ||
5511 | /* Write out the section header names. */ | |
30e8ee25 | 5512 | t = elf_tdata (abfd); |
26ae6d5e | 5513 | if (elf_shstrtab (abfd) != NULL |
30e8ee25 | 5514 | && (bfd_seek (abfd, t->shstrtab_hdr.sh_offset, SEEK_SET) != 0 |
08a40648 | 5515 | || !_bfd_elf_strtab_emit (abfd, elf_shstrtab (abfd)))) |
b34976b6 | 5516 | return FALSE; |
252b5132 RH |
5517 | |
5518 | if (bed->elf_backend_final_write_processing) | |
12bd6957 | 5519 | (*bed->elf_backend_final_write_processing) (abfd, elf_linker (abfd)); |
252b5132 | 5520 | |
ff59fc36 RM |
5521 | if (!bed->s->write_shdrs_and_ehdr (abfd)) |
5522 | return FALSE; | |
5523 | ||
5524 | /* This is last since write_shdrs_and_ehdr can touch i_shdrp[0]. */ | |
c0355132 AM |
5525 | if (t->o->build_id.after_write_object_contents != NULL) |
5526 | return (*t->o->build_id.after_write_object_contents) (abfd); | |
ff59fc36 RM |
5527 | |
5528 | return TRUE; | |
252b5132 RH |
5529 | } |
5530 | ||
b34976b6 | 5531 | bfd_boolean |
217aa764 | 5532 | _bfd_elf_write_corefile_contents (bfd *abfd) |
252b5132 | 5533 | { |
c044fabd | 5534 | /* Hopefully this can be done just like an object file. */ |
252b5132 RH |
5535 | return _bfd_elf_write_object_contents (abfd); |
5536 | } | |
c044fabd KH |
5537 | |
5538 | /* Given a section, search the header to find them. */ | |
5539 | ||
cb33740c | 5540 | unsigned int |
198beae2 | 5541 | _bfd_elf_section_from_bfd_section (bfd *abfd, struct bfd_section *asect) |
252b5132 | 5542 | { |
9c5bfbb7 | 5543 | const struct elf_backend_data *bed; |
91d6fa6a | 5544 | unsigned int sec_index; |
252b5132 | 5545 | |
9ad5cbcf AM |
5546 | if (elf_section_data (asect) != NULL |
5547 | && elf_section_data (asect)->this_idx != 0) | |
5548 | return elf_section_data (asect)->this_idx; | |
5549 | ||
5550 | if (bfd_is_abs_section (asect)) | |
91d6fa6a | 5551 | sec_index = SHN_ABS; |
af746e92 | 5552 | else if (bfd_is_com_section (asect)) |
91d6fa6a | 5553 | sec_index = SHN_COMMON; |
af746e92 | 5554 | else if (bfd_is_und_section (asect)) |
91d6fa6a | 5555 | sec_index = SHN_UNDEF; |
af746e92 | 5556 | else |
91d6fa6a | 5557 | sec_index = SHN_BAD; |
252b5132 | 5558 | |
af746e92 | 5559 | bed = get_elf_backend_data (abfd); |
252b5132 RH |
5560 | if (bed->elf_backend_section_from_bfd_section) |
5561 | { | |
91d6fa6a | 5562 | int retval = sec_index; |
9ad5cbcf | 5563 | |
af746e92 AM |
5564 | if ((*bed->elf_backend_section_from_bfd_section) (abfd, asect, &retval)) |
5565 | return retval; | |
252b5132 RH |
5566 | } |
5567 | ||
91d6fa6a | 5568 | if (sec_index == SHN_BAD) |
af746e92 | 5569 | bfd_set_error (bfd_error_nonrepresentable_section); |
252b5132 | 5570 | |
91d6fa6a | 5571 | return sec_index; |
252b5132 RH |
5572 | } |
5573 | ||
5574 | /* Given a BFD symbol, return the index in the ELF symbol table, or -1 | |
5575 | on error. */ | |
5576 | ||
5577 | int | |
217aa764 | 5578 | _bfd_elf_symbol_from_bfd_symbol (bfd *abfd, asymbol **asym_ptr_ptr) |
252b5132 RH |
5579 | { |
5580 | asymbol *asym_ptr = *asym_ptr_ptr; | |
5581 | int idx; | |
5582 | flagword flags = asym_ptr->flags; | |
5583 | ||
5584 | /* When gas creates relocations against local labels, it creates its | |
5585 | own symbol for the section, but does put the symbol into the | |
5586 | symbol chain, so udata is 0. When the linker is generating | |
5587 | relocatable output, this section symbol may be for one of the | |
5588 | input sections rather than the output section. */ | |
5589 | if (asym_ptr->udata.i == 0 | |
5590 | && (flags & BSF_SECTION_SYM) | |
5591 | && asym_ptr->section) | |
5592 | { | |
5372391b | 5593 | asection *sec; |
252b5132 RH |
5594 | int indx; |
5595 | ||
5372391b AM |
5596 | sec = asym_ptr->section; |
5597 | if (sec->owner != abfd && sec->output_section != NULL) | |
5598 | sec = sec->output_section; | |
5599 | if (sec->owner == abfd | |
5600 | && (indx = sec->index) < elf_num_section_syms (abfd) | |
4e89ac30 | 5601 | && elf_section_syms (abfd)[indx] != NULL) |
252b5132 RH |
5602 | asym_ptr->udata.i = elf_section_syms (abfd)[indx]->udata.i; |
5603 | } | |
5604 | ||
5605 | idx = asym_ptr->udata.i; | |
5606 | ||
5607 | if (idx == 0) | |
5608 | { | |
5609 | /* This case can occur when using --strip-symbol on a symbol | |
08a40648 | 5610 | which is used in a relocation entry. */ |
252b5132 | 5611 | (*_bfd_error_handler) |
d003868e AM |
5612 | (_("%B: symbol `%s' required but not present"), |
5613 | abfd, bfd_asymbol_name (asym_ptr)); | |
252b5132 RH |
5614 | bfd_set_error (bfd_error_no_symbols); |
5615 | return -1; | |
5616 | } | |
5617 | ||
5618 | #if DEBUG & 4 | |
5619 | { | |
5620 | fprintf (stderr, | |
9ccb8af9 AM |
5621 | "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8lx\n", |
5622 | (long) asym_ptr, asym_ptr->name, idx, (long) flags); | |
252b5132 RH |
5623 | fflush (stderr); |
5624 | } | |
5625 | #endif | |
5626 | ||
5627 | return idx; | |
5628 | } | |
5629 | ||
84d1d650 | 5630 | /* Rewrite program header information. */ |
252b5132 | 5631 | |
b34976b6 | 5632 | static bfd_boolean |
84d1d650 | 5633 | rewrite_elf_program_header (bfd *ibfd, bfd *obfd) |
252b5132 | 5634 | { |
b34976b6 AM |
5635 | Elf_Internal_Ehdr *iehdr; |
5636 | struct elf_segment_map *map; | |
5637 | struct elf_segment_map *map_first; | |
5638 | struct elf_segment_map **pointer_to_map; | |
5639 | Elf_Internal_Phdr *segment; | |
5640 | asection *section; | |
5641 | unsigned int i; | |
5642 | unsigned int num_segments; | |
5643 | bfd_boolean phdr_included = FALSE; | |
5c44b38e | 5644 | bfd_boolean p_paddr_valid; |
b34976b6 AM |
5645 | bfd_vma maxpagesize; |
5646 | struct elf_segment_map *phdr_adjust_seg = NULL; | |
5647 | unsigned int phdr_adjust_num = 0; | |
9c5bfbb7 | 5648 | const struct elf_backend_data *bed; |
bc67d8a6 | 5649 | |
caf47ea6 | 5650 | bed = get_elf_backend_data (ibfd); |
252b5132 RH |
5651 | iehdr = elf_elfheader (ibfd); |
5652 | ||
bc67d8a6 | 5653 | map_first = NULL; |
c044fabd | 5654 | pointer_to_map = &map_first; |
252b5132 RH |
5655 | |
5656 | num_segments = elf_elfheader (ibfd)->e_phnum; | |
bc67d8a6 NC |
5657 | maxpagesize = get_elf_backend_data (obfd)->maxpagesize; |
5658 | ||
5659 | /* Returns the end address of the segment + 1. */ | |
aecc8f8a AM |
5660 | #define SEGMENT_END(segment, start) \ |
5661 | (start + (segment->p_memsz > segment->p_filesz \ | |
5662 | ? segment->p_memsz : segment->p_filesz)) | |
bc67d8a6 | 5663 | |
eecdbe52 JJ |
5664 | #define SECTION_SIZE(section, segment) \ |
5665 | (((section->flags & (SEC_HAS_CONTENTS | SEC_THREAD_LOCAL)) \ | |
5666 | != SEC_THREAD_LOCAL || segment->p_type == PT_TLS) \ | |
eea6121a | 5667 | ? section->size : 0) |
eecdbe52 | 5668 | |
b34976b6 | 5669 | /* Returns TRUE if the given section is contained within |
bc67d8a6 | 5670 | the given segment. VMA addresses are compared. */ |
aecc8f8a AM |
5671 | #define IS_CONTAINED_BY_VMA(section, segment) \ |
5672 | (section->vma >= segment->p_vaddr \ | |
eecdbe52 | 5673 | && (section->vma + SECTION_SIZE (section, segment) \ |
aecc8f8a | 5674 | <= (SEGMENT_END (segment, segment->p_vaddr)))) |
c044fabd | 5675 | |
b34976b6 | 5676 | /* Returns TRUE if the given section is contained within |
bc67d8a6 | 5677 | the given segment. LMA addresses are compared. */ |
aecc8f8a AM |
5678 | #define IS_CONTAINED_BY_LMA(section, segment, base) \ |
5679 | (section->lma >= base \ | |
eecdbe52 | 5680 | && (section->lma + SECTION_SIZE (section, segment) \ |
aecc8f8a | 5681 | <= SEGMENT_END (segment, base))) |
252b5132 | 5682 | |
0efc80c8 L |
5683 | /* Handle PT_NOTE segment. */ |
5684 | #define IS_NOTE(p, s) \ | |
aecc8f8a | 5685 | (p->p_type == PT_NOTE \ |
0efc80c8 | 5686 | && elf_section_type (s) == SHT_NOTE \ |
aecc8f8a | 5687 | && (bfd_vma) s->filepos >= p->p_offset \ |
cb3ff1e5 | 5688 | && ((bfd_vma) s->filepos + s->size \ |
aecc8f8a | 5689 | <= p->p_offset + p->p_filesz)) |
252b5132 | 5690 | |
0efc80c8 L |
5691 | /* Special case: corefile "NOTE" section containing regs, prpsinfo |
5692 | etc. */ | |
5693 | #define IS_COREFILE_NOTE(p, s) \ | |
5694 | (IS_NOTE (p, s) \ | |
5695 | && bfd_get_format (ibfd) == bfd_core \ | |
5696 | && s->vma == 0 \ | |
5697 | && s->lma == 0) | |
5698 | ||
252b5132 RH |
5699 | /* The complicated case when p_vaddr is 0 is to handle the Solaris |
5700 | linker, which generates a PT_INTERP section with p_vaddr and | |
5701 | p_memsz set to 0. */ | |
aecc8f8a AM |
5702 | #define IS_SOLARIS_PT_INTERP(p, s) \ |
5703 | (p->p_vaddr == 0 \ | |
5704 | && p->p_paddr == 0 \ | |
5705 | && p->p_memsz == 0 \ | |
5706 | && p->p_filesz > 0 \ | |
5707 | && (s->flags & SEC_HAS_CONTENTS) != 0 \ | |
eea6121a | 5708 | && s->size > 0 \ |
aecc8f8a | 5709 | && (bfd_vma) s->filepos >= p->p_offset \ |
cb3ff1e5 | 5710 | && ((bfd_vma) s->filepos + s->size \ |
aecc8f8a | 5711 | <= p->p_offset + p->p_filesz)) |
5c440b1e | 5712 | |
bc67d8a6 NC |
5713 | /* Decide if the given section should be included in the given segment. |
5714 | A section will be included if: | |
f5ffc919 | 5715 | 1. It is within the address space of the segment -- we use the LMA |
08a40648 | 5716 | if that is set for the segment and the VMA otherwise, |
0efc80c8 | 5717 | 2. It is an allocated section or a NOTE section in a PT_NOTE |
d324f6d6 | 5718 | segment. |
bc67d8a6 | 5719 | 3. There is an output section associated with it, |
eecdbe52 | 5720 | 4. The section has not already been allocated to a previous segment. |
2b05f1b7 | 5721 | 5. PT_GNU_STACK segments do not include any sections. |
03394ac9 | 5722 | 6. PT_TLS segment includes only SHF_TLS sections. |
6f79b219 JJ |
5723 | 7. SHF_TLS sections are only in PT_TLS or PT_LOAD segments. |
5724 | 8. PT_DYNAMIC should not contain empty sections at the beginning | |
08a40648 | 5725 | (with the possible exception of .dynamic). */ |
9f17e2a6 | 5726 | #define IS_SECTION_IN_INPUT_SEGMENT(section, segment, bed) \ |
2b05f1b7 L |
5727 | ((((segment->p_paddr \ |
5728 | ? IS_CONTAINED_BY_LMA (section, segment, segment->p_paddr) \ | |
5729 | : IS_CONTAINED_BY_VMA (section, segment)) \ | |
5730 | && (section->flags & SEC_ALLOC) != 0) \ | |
0efc80c8 | 5731 | || IS_NOTE (segment, section)) \ |
2b05f1b7 L |
5732 | && segment->p_type != PT_GNU_STACK \ |
5733 | && (segment->p_type != PT_TLS \ | |
5734 | || (section->flags & SEC_THREAD_LOCAL)) \ | |
5735 | && (segment->p_type == PT_LOAD \ | |
5736 | || segment->p_type == PT_TLS \ | |
5737 | || (section->flags & SEC_THREAD_LOCAL) == 0) \ | |
5738 | && (segment->p_type != PT_DYNAMIC \ | |
5739 | || SECTION_SIZE (section, segment) > 0 \ | |
5740 | || (segment->p_paddr \ | |
5741 | ? segment->p_paddr != section->lma \ | |
5742 | : segment->p_vaddr != section->vma) \ | |
5743 | || (strcmp (bfd_get_section_name (ibfd, section), ".dynamic") \ | |
5744 | == 0)) \ | |
0067a569 | 5745 | && !section->segment_mark) |
bc67d8a6 | 5746 | |
9f17e2a6 L |
5747 | /* If the output section of a section in the input segment is NULL, |
5748 | it is removed from the corresponding output segment. */ | |
5749 | #define INCLUDE_SECTION_IN_SEGMENT(section, segment, bed) \ | |
5750 | (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed) \ | |
5751 | && section->output_section != NULL) | |
5752 | ||
b34976b6 | 5753 | /* Returns TRUE iff seg1 starts after the end of seg2. */ |
b5f852ea NC |
5754 | #define SEGMENT_AFTER_SEGMENT(seg1, seg2, field) \ |
5755 | (seg1->field >= SEGMENT_END (seg2, seg2->field)) | |
5756 | ||
5757 | /* Returns TRUE iff seg1 and seg2 overlap. Segments overlap iff both | |
5758 | their VMA address ranges and their LMA address ranges overlap. | |
5759 | It is possible to have overlapping VMA ranges without overlapping LMA | |
5760 | ranges. RedBoot images for example can have both .data and .bss mapped | |
5761 | to the same VMA range, but with the .data section mapped to a different | |
5762 | LMA. */ | |
aecc8f8a | 5763 | #define SEGMENT_OVERLAPS(seg1, seg2) \ |
b5f852ea | 5764 | ( !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_vaddr) \ |
08a40648 | 5765 | || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_vaddr)) \ |
b5f852ea | 5766 | && !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_paddr) \ |
08a40648 | 5767 | || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_paddr))) |
bc67d8a6 NC |
5768 | |
5769 | /* Initialise the segment mark field. */ | |
5770 | for (section = ibfd->sections; section != NULL; section = section->next) | |
b34976b6 | 5771 | section->segment_mark = FALSE; |
bc67d8a6 | 5772 | |
5c44b38e AM |
5773 | /* The Solaris linker creates program headers in which all the |
5774 | p_paddr fields are zero. When we try to objcopy or strip such a | |
5775 | file, we get confused. Check for this case, and if we find it | |
5776 | don't set the p_paddr_valid fields. */ | |
5777 | p_paddr_valid = FALSE; | |
5778 | for (i = 0, segment = elf_tdata (ibfd)->phdr; | |
5779 | i < num_segments; | |
5780 | i++, segment++) | |
5781 | if (segment->p_paddr != 0) | |
5782 | { | |
5783 | p_paddr_valid = TRUE; | |
5784 | break; | |
5785 | } | |
5786 | ||
252b5132 | 5787 | /* Scan through the segments specified in the program header |
bc67d8a6 | 5788 | of the input BFD. For this first scan we look for overlaps |
9ad5cbcf | 5789 | in the loadable segments. These can be created by weird |
aecc8f8a | 5790 | parameters to objcopy. Also, fix some solaris weirdness. */ |
bc67d8a6 NC |
5791 | for (i = 0, segment = elf_tdata (ibfd)->phdr; |
5792 | i < num_segments; | |
c044fabd | 5793 | i++, segment++) |
252b5132 | 5794 | { |
252b5132 | 5795 | unsigned int j; |
c044fabd | 5796 | Elf_Internal_Phdr *segment2; |
252b5132 | 5797 | |
aecc8f8a AM |
5798 | if (segment->p_type == PT_INTERP) |
5799 | for (section = ibfd->sections; section; section = section->next) | |
5800 | if (IS_SOLARIS_PT_INTERP (segment, section)) | |
5801 | { | |
5802 | /* Mininal change so that the normal section to segment | |
4cc11e76 | 5803 | assignment code will work. */ |
aecc8f8a AM |
5804 | segment->p_vaddr = section->vma; |
5805 | break; | |
5806 | } | |
5807 | ||
bc67d8a6 | 5808 | if (segment->p_type != PT_LOAD) |
b10a8ae0 L |
5809 | { |
5810 | /* Remove PT_GNU_RELRO segment. */ | |
5811 | if (segment->p_type == PT_GNU_RELRO) | |
5812 | segment->p_type = PT_NULL; | |
5813 | continue; | |
5814 | } | |
c044fabd | 5815 | |
bc67d8a6 | 5816 | /* Determine if this segment overlaps any previous segments. */ |
0067a569 | 5817 | for (j = 0, segment2 = elf_tdata (ibfd)->phdr; j < i; j++, segment2++) |
bc67d8a6 NC |
5818 | { |
5819 | bfd_signed_vma extra_length; | |
c044fabd | 5820 | |
bc67d8a6 | 5821 | if (segment2->p_type != PT_LOAD |
0067a569 | 5822 | || !SEGMENT_OVERLAPS (segment, segment2)) |
bc67d8a6 | 5823 | continue; |
c044fabd | 5824 | |
bc67d8a6 NC |
5825 | /* Merge the two segments together. */ |
5826 | if (segment2->p_vaddr < segment->p_vaddr) | |
5827 | { | |
c044fabd | 5828 | /* Extend SEGMENT2 to include SEGMENT and then delete |
08a40648 | 5829 | SEGMENT. */ |
0067a569 AM |
5830 | extra_length = (SEGMENT_END (segment, segment->p_vaddr) |
5831 | - SEGMENT_END (segment2, segment2->p_vaddr)); | |
c044fabd | 5832 | |
bc67d8a6 NC |
5833 | if (extra_length > 0) |
5834 | { | |
0067a569 | 5835 | segment2->p_memsz += extra_length; |
bc67d8a6 NC |
5836 | segment2->p_filesz += extra_length; |
5837 | } | |
c044fabd | 5838 | |
bc67d8a6 | 5839 | segment->p_type = PT_NULL; |
c044fabd | 5840 | |
bc67d8a6 NC |
5841 | /* Since we have deleted P we must restart the outer loop. */ |
5842 | i = 0; | |
5843 | segment = elf_tdata (ibfd)->phdr; | |
5844 | break; | |
5845 | } | |
5846 | else | |
5847 | { | |
c044fabd | 5848 | /* Extend SEGMENT to include SEGMENT2 and then delete |
08a40648 | 5849 | SEGMENT2. */ |
0067a569 AM |
5850 | extra_length = (SEGMENT_END (segment2, segment2->p_vaddr) |
5851 | - SEGMENT_END (segment, segment->p_vaddr)); | |
c044fabd | 5852 | |
bc67d8a6 NC |
5853 | if (extra_length > 0) |
5854 | { | |
0067a569 | 5855 | segment->p_memsz += extra_length; |
bc67d8a6 NC |
5856 | segment->p_filesz += extra_length; |
5857 | } | |
c044fabd | 5858 | |
bc67d8a6 NC |
5859 | segment2->p_type = PT_NULL; |
5860 | } | |
5861 | } | |
5862 | } | |
c044fabd | 5863 | |
bc67d8a6 NC |
5864 | /* The second scan attempts to assign sections to segments. */ |
5865 | for (i = 0, segment = elf_tdata (ibfd)->phdr; | |
5866 | i < num_segments; | |
0067a569 | 5867 | i++, segment++) |
bc67d8a6 | 5868 | { |
0067a569 AM |
5869 | unsigned int section_count; |
5870 | asection **sections; | |
5871 | asection *output_section; | |
5872 | unsigned int isec; | |
5873 | bfd_vma matching_lma; | |
5874 | bfd_vma suggested_lma; | |
5875 | unsigned int j; | |
dc810e39 | 5876 | bfd_size_type amt; |
0067a569 AM |
5877 | asection *first_section; |
5878 | bfd_boolean first_matching_lma; | |
5879 | bfd_boolean first_suggested_lma; | |
bc67d8a6 NC |
5880 | |
5881 | if (segment->p_type == PT_NULL) | |
5882 | continue; | |
c044fabd | 5883 | |
9f17e2a6 | 5884 | first_section = NULL; |
bc67d8a6 | 5885 | /* Compute how many sections might be placed into this segment. */ |
b5f852ea NC |
5886 | for (section = ibfd->sections, section_count = 0; |
5887 | section != NULL; | |
5888 | section = section->next) | |
9f17e2a6 L |
5889 | { |
5890 | /* Find the first section in the input segment, which may be | |
5891 | removed from the corresponding output segment. */ | |
5892 | if (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed)) | |
5893 | { | |
5894 | if (first_section == NULL) | |
5895 | first_section = section; | |
5896 | if (section->output_section != NULL) | |
5897 | ++section_count; | |
5898 | } | |
5899 | } | |
811072d8 | 5900 | |
b5f852ea NC |
5901 | /* Allocate a segment map big enough to contain |
5902 | all of the sections we have selected. */ | |
dc810e39 AM |
5903 | amt = sizeof (struct elf_segment_map); |
5904 | amt += ((bfd_size_type) section_count - 1) * sizeof (asection *); | |
a50b1753 | 5905 | map = (struct elf_segment_map *) bfd_zalloc (obfd, amt); |
bc67d8a6 | 5906 | if (map == NULL) |
b34976b6 | 5907 | return FALSE; |
252b5132 RH |
5908 | |
5909 | /* Initialise the fields of the segment map. Default to | |
5910 | using the physical address of the segment in the input BFD. */ | |
0067a569 AM |
5911 | map->next = NULL; |
5912 | map->p_type = segment->p_type; | |
5913 | map->p_flags = segment->p_flags; | |
bc67d8a6 | 5914 | map->p_flags_valid = 1; |
55d55ac7 | 5915 | |
9f17e2a6 L |
5916 | /* If the first section in the input segment is removed, there is |
5917 | no need to preserve segment physical address in the corresponding | |
5918 | output segment. */ | |
945c025a | 5919 | if (!first_section || first_section->output_section != NULL) |
9f17e2a6 L |
5920 | { |
5921 | map->p_paddr = segment->p_paddr; | |
5c44b38e | 5922 | map->p_paddr_valid = p_paddr_valid; |
9f17e2a6 | 5923 | } |
252b5132 RH |
5924 | |
5925 | /* Determine if this segment contains the ELF file header | |
5926 | and if it contains the program headers themselves. */ | |
bc67d8a6 NC |
5927 | map->includes_filehdr = (segment->p_offset == 0 |
5928 | && segment->p_filesz >= iehdr->e_ehsize); | |
bc67d8a6 | 5929 | map->includes_phdrs = 0; |
252b5132 | 5930 | |
0067a569 | 5931 | if (!phdr_included || segment->p_type != PT_LOAD) |
252b5132 | 5932 | { |
bc67d8a6 NC |
5933 | map->includes_phdrs = |
5934 | (segment->p_offset <= (bfd_vma) iehdr->e_phoff | |
5935 | && (segment->p_offset + segment->p_filesz | |
252b5132 RH |
5936 | >= ((bfd_vma) iehdr->e_phoff |
5937 | + iehdr->e_phnum * iehdr->e_phentsize))); | |
c044fabd | 5938 | |
bc67d8a6 | 5939 | if (segment->p_type == PT_LOAD && map->includes_phdrs) |
b34976b6 | 5940 | phdr_included = TRUE; |
252b5132 RH |
5941 | } |
5942 | ||
bc67d8a6 | 5943 | if (section_count == 0) |
252b5132 RH |
5944 | { |
5945 | /* Special segments, such as the PT_PHDR segment, may contain | |
5946 | no sections, but ordinary, loadable segments should contain | |
1ed89aa9 NC |
5947 | something. They are allowed by the ELF spec however, so only |
5948 | a warning is produced. */ | |
bc67d8a6 | 5949 | if (segment->p_type == PT_LOAD) |
0067a569 AM |
5950 | (*_bfd_error_handler) (_("%B: warning: Empty loadable segment" |
5951 | " detected, is this intentional ?\n"), | |
5952 | ibfd); | |
252b5132 | 5953 | |
bc67d8a6 | 5954 | map->count = 0; |
c044fabd KH |
5955 | *pointer_to_map = map; |
5956 | pointer_to_map = &map->next; | |
252b5132 RH |
5957 | |
5958 | continue; | |
5959 | } | |
5960 | ||
5961 | /* Now scan the sections in the input BFD again and attempt | |
5962 | to add their corresponding output sections to the segment map. | |
5963 | The problem here is how to handle an output section which has | |
5964 | been moved (ie had its LMA changed). There are four possibilities: | |
5965 | ||
5966 | 1. None of the sections have been moved. | |
5967 | In this case we can continue to use the segment LMA from the | |
5968 | input BFD. | |
5969 | ||
5970 | 2. All of the sections have been moved by the same amount. | |
5971 | In this case we can change the segment's LMA to match the LMA | |
5972 | of the first section. | |
5973 | ||
5974 | 3. Some of the sections have been moved, others have not. | |
5975 | In this case those sections which have not been moved can be | |
5976 | placed in the current segment which will have to have its size, | |
5977 | and possibly its LMA changed, and a new segment or segments will | |
5978 | have to be created to contain the other sections. | |
5979 | ||
b5f852ea | 5980 | 4. The sections have been moved, but not by the same amount. |
252b5132 RH |
5981 | In this case we can change the segment's LMA to match the LMA |
5982 | of the first section and we will have to create a new segment | |
5983 | or segments to contain the other sections. | |
5984 | ||
5985 | In order to save time, we allocate an array to hold the section | |
5986 | pointers that we are interested in. As these sections get assigned | |
5987 | to a segment, they are removed from this array. */ | |
5988 | ||
a50b1753 | 5989 | sections = (asection **) bfd_malloc2 (section_count, sizeof (asection *)); |
252b5132 | 5990 | if (sections == NULL) |
b34976b6 | 5991 | return FALSE; |
252b5132 RH |
5992 | |
5993 | /* Step One: Scan for segment vs section LMA conflicts. | |
5994 | Also add the sections to the section array allocated above. | |
5995 | Also add the sections to the current segment. In the common | |
5996 | case, where the sections have not been moved, this means that | |
5997 | we have completely filled the segment, and there is nothing | |
5998 | more to do. */ | |
252b5132 | 5999 | isec = 0; |
72730e0c | 6000 | matching_lma = 0; |
252b5132 | 6001 | suggested_lma = 0; |
0067a569 AM |
6002 | first_matching_lma = TRUE; |
6003 | first_suggested_lma = TRUE; | |
252b5132 | 6004 | |
147d51c2 | 6005 | for (section = ibfd->sections; |
bc67d8a6 NC |
6006 | section != NULL; |
6007 | section = section->next) | |
147d51c2 L |
6008 | if (section == first_section) |
6009 | break; | |
6010 | ||
6011 | for (j = 0; section != NULL; section = section->next) | |
252b5132 | 6012 | { |
caf47ea6 | 6013 | if (INCLUDE_SECTION_IN_SEGMENT (section, segment, bed)) |
c0f7859b | 6014 | { |
bc67d8a6 NC |
6015 | output_section = section->output_section; |
6016 | ||
0067a569 | 6017 | sections[j++] = section; |
252b5132 RH |
6018 | |
6019 | /* The Solaris native linker always sets p_paddr to 0. | |
6020 | We try to catch that case here, and set it to the | |
5e8d7549 NC |
6021 | correct value. Note - some backends require that |
6022 | p_paddr be left as zero. */ | |
5c44b38e | 6023 | if (!p_paddr_valid |
4455705d | 6024 | && segment->p_vaddr != 0 |
0067a569 | 6025 | && !bed->want_p_paddr_set_to_zero |
252b5132 | 6026 | && isec == 0 |
bc67d8a6 | 6027 | && output_section->lma != 0 |
0067a569 AM |
6028 | && output_section->vma == (segment->p_vaddr |
6029 | + (map->includes_filehdr | |
6030 | ? iehdr->e_ehsize | |
6031 | : 0) | |
6032 | + (map->includes_phdrs | |
6033 | ? (iehdr->e_phnum | |
6034 | * iehdr->e_phentsize) | |
6035 | : 0))) | |
bc67d8a6 | 6036 | map->p_paddr = segment->p_vaddr; |
252b5132 RH |
6037 | |
6038 | /* Match up the physical address of the segment with the | |
6039 | LMA address of the output section. */ | |
bc67d8a6 | 6040 | if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr) |
5e8d7549 | 6041 | || IS_COREFILE_NOTE (segment, section) |
0067a569 AM |
6042 | || (bed->want_p_paddr_set_to_zero |
6043 | && IS_CONTAINED_BY_VMA (output_section, segment))) | |
252b5132 | 6044 | { |
0067a569 AM |
6045 | if (first_matching_lma || output_section->lma < matching_lma) |
6046 | { | |
6047 | matching_lma = output_section->lma; | |
6048 | first_matching_lma = FALSE; | |
6049 | } | |
252b5132 RH |
6050 | |
6051 | /* We assume that if the section fits within the segment | |
bc67d8a6 | 6052 | then it does not overlap any other section within that |
252b5132 | 6053 | segment. */ |
0067a569 AM |
6054 | map->sections[isec++] = output_section; |
6055 | } | |
6056 | else if (first_suggested_lma) | |
6057 | { | |
6058 | suggested_lma = output_section->lma; | |
6059 | first_suggested_lma = FALSE; | |
252b5132 | 6060 | } |
147d51c2 L |
6061 | |
6062 | if (j == section_count) | |
6063 | break; | |
252b5132 RH |
6064 | } |
6065 | } | |
6066 | ||
bc67d8a6 | 6067 | BFD_ASSERT (j == section_count); |
252b5132 RH |
6068 | |
6069 | /* Step Two: Adjust the physical address of the current segment, | |
6070 | if necessary. */ | |
bc67d8a6 | 6071 | if (isec == section_count) |
252b5132 RH |
6072 | { |
6073 | /* All of the sections fitted within the segment as currently | |
6074 | specified. This is the default case. Add the segment to | |
6075 | the list of built segments and carry on to process the next | |
6076 | program header in the input BFD. */ | |
bc67d8a6 | 6077 | map->count = section_count; |
c044fabd KH |
6078 | *pointer_to_map = map; |
6079 | pointer_to_map = &map->next; | |
08a40648 | 6080 | |
5c44b38e AM |
6081 | if (p_paddr_valid |
6082 | && !bed->want_p_paddr_set_to_zero | |
147d51c2 | 6083 | && matching_lma != map->p_paddr |
5c44b38e AM |
6084 | && !map->includes_filehdr |
6085 | && !map->includes_phdrs) | |
3271a814 NS |
6086 | /* There is some padding before the first section in the |
6087 | segment. So, we must account for that in the output | |
6088 | segment's vma. */ | |
6089 | map->p_vaddr_offset = matching_lma - map->p_paddr; | |
08a40648 | 6090 | |
252b5132 RH |
6091 | free (sections); |
6092 | continue; | |
6093 | } | |
252b5132 RH |
6094 | else |
6095 | { | |
0067a569 | 6096 | if (!first_matching_lma) |
72730e0c AM |
6097 | { |
6098 | /* At least one section fits inside the current segment. | |
6099 | Keep it, but modify its physical address to match the | |
6100 | LMA of the first section that fitted. */ | |
bc67d8a6 | 6101 | map->p_paddr = matching_lma; |
72730e0c AM |
6102 | } |
6103 | else | |
6104 | { | |
6105 | /* None of the sections fitted inside the current segment. | |
6106 | Change the current segment's physical address to match | |
6107 | the LMA of the first section. */ | |
bc67d8a6 | 6108 | map->p_paddr = suggested_lma; |
72730e0c AM |
6109 | } |
6110 | ||
bc67d8a6 NC |
6111 | /* Offset the segment physical address from the lma |
6112 | to allow for space taken up by elf headers. */ | |
6113 | if (map->includes_filehdr) | |
010c8431 AM |
6114 | { |
6115 | if (map->p_paddr >= iehdr->e_ehsize) | |
6116 | map->p_paddr -= iehdr->e_ehsize; | |
6117 | else | |
6118 | { | |
6119 | map->includes_filehdr = FALSE; | |
6120 | map->includes_phdrs = FALSE; | |
6121 | } | |
6122 | } | |
252b5132 | 6123 | |
bc67d8a6 NC |
6124 | if (map->includes_phdrs) |
6125 | { | |
010c8431 AM |
6126 | if (map->p_paddr >= iehdr->e_phnum * iehdr->e_phentsize) |
6127 | { | |
6128 | map->p_paddr -= iehdr->e_phnum * iehdr->e_phentsize; | |
6129 | ||
6130 | /* iehdr->e_phnum is just an estimate of the number | |
6131 | of program headers that we will need. Make a note | |
6132 | here of the number we used and the segment we chose | |
6133 | to hold these headers, so that we can adjust the | |
6134 | offset when we know the correct value. */ | |
6135 | phdr_adjust_num = iehdr->e_phnum; | |
6136 | phdr_adjust_seg = map; | |
6137 | } | |
6138 | else | |
6139 | map->includes_phdrs = FALSE; | |
bc67d8a6 | 6140 | } |
252b5132 RH |
6141 | } |
6142 | ||
6143 | /* Step Three: Loop over the sections again, this time assigning | |
caf47ea6 | 6144 | those that fit to the current segment and removing them from the |
252b5132 RH |
6145 | sections array; but making sure not to leave large gaps. Once all |
6146 | possible sections have been assigned to the current segment it is | |
6147 | added to the list of built segments and if sections still remain | |
6148 | to be assigned, a new segment is constructed before repeating | |
6149 | the loop. */ | |
6150 | isec = 0; | |
6151 | do | |
6152 | { | |
bc67d8a6 | 6153 | map->count = 0; |
252b5132 | 6154 | suggested_lma = 0; |
0067a569 | 6155 | first_suggested_lma = TRUE; |
252b5132 RH |
6156 | |
6157 | /* Fill the current segment with sections that fit. */ | |
bc67d8a6 | 6158 | for (j = 0; j < section_count; j++) |
252b5132 | 6159 | { |
bc67d8a6 | 6160 | section = sections[j]; |
252b5132 | 6161 | |
bc67d8a6 | 6162 | if (section == NULL) |
252b5132 RH |
6163 | continue; |
6164 | ||
bc67d8a6 | 6165 | output_section = section->output_section; |
252b5132 | 6166 | |
bc67d8a6 | 6167 | BFD_ASSERT (output_section != NULL); |
c044fabd | 6168 | |
bc67d8a6 NC |
6169 | if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr) |
6170 | || IS_COREFILE_NOTE (segment, section)) | |
252b5132 | 6171 | { |
bc67d8a6 | 6172 | if (map->count == 0) |
252b5132 RH |
6173 | { |
6174 | /* If the first section in a segment does not start at | |
bc67d8a6 NC |
6175 | the beginning of the segment, then something is |
6176 | wrong. */ | |
0067a569 AM |
6177 | if (output_section->lma |
6178 | != (map->p_paddr | |
6179 | + (map->includes_filehdr ? iehdr->e_ehsize : 0) | |
6180 | + (map->includes_phdrs | |
6181 | ? iehdr->e_phnum * iehdr->e_phentsize | |
6182 | : 0))) | |
252b5132 RH |
6183 | abort (); |
6184 | } | |
6185 | else | |
6186 | { | |
0067a569 | 6187 | asection *prev_sec; |
252b5132 | 6188 | |
bc67d8a6 | 6189 | prev_sec = map->sections[map->count - 1]; |
252b5132 RH |
6190 | |
6191 | /* If the gap between the end of the previous section | |
bc67d8a6 NC |
6192 | and the start of this section is more than |
6193 | maxpagesize then we need to start a new segment. */ | |
eea6121a | 6194 | if ((BFD_ALIGN (prev_sec->lma + prev_sec->size, |
079e9a2f | 6195 | maxpagesize) |
caf47ea6 | 6196 | < BFD_ALIGN (output_section->lma, maxpagesize)) |
0067a569 | 6197 | || (prev_sec->lma + prev_sec->size |
079e9a2f | 6198 | > output_section->lma)) |
252b5132 | 6199 | { |
0067a569 AM |
6200 | if (first_suggested_lma) |
6201 | { | |
6202 | suggested_lma = output_section->lma; | |
6203 | first_suggested_lma = FALSE; | |
6204 | } | |
252b5132 RH |
6205 | |
6206 | continue; | |
6207 | } | |
6208 | } | |
6209 | ||
bc67d8a6 | 6210 | map->sections[map->count++] = output_section; |
252b5132 RH |
6211 | ++isec; |
6212 | sections[j] = NULL; | |
b34976b6 | 6213 | section->segment_mark = TRUE; |
252b5132 | 6214 | } |
0067a569 AM |
6215 | else if (first_suggested_lma) |
6216 | { | |
6217 | suggested_lma = output_section->lma; | |
6218 | first_suggested_lma = FALSE; | |
6219 | } | |
252b5132 RH |
6220 | } |
6221 | ||
bc67d8a6 | 6222 | BFD_ASSERT (map->count > 0); |
252b5132 RH |
6223 | |
6224 | /* Add the current segment to the list of built segments. */ | |
c044fabd KH |
6225 | *pointer_to_map = map; |
6226 | pointer_to_map = &map->next; | |
252b5132 | 6227 | |
bc67d8a6 | 6228 | if (isec < section_count) |
252b5132 RH |
6229 | { |
6230 | /* We still have not allocated all of the sections to | |
6231 | segments. Create a new segment here, initialise it | |
6232 | and carry on looping. */ | |
dc810e39 AM |
6233 | amt = sizeof (struct elf_segment_map); |
6234 | amt += ((bfd_size_type) section_count - 1) * sizeof (asection *); | |
5964fc3a | 6235 | map = (struct elf_segment_map *) bfd_zalloc (obfd, amt); |
bc67d8a6 | 6236 | if (map == NULL) |
5ed6aba4 NC |
6237 | { |
6238 | free (sections); | |
6239 | return FALSE; | |
6240 | } | |
252b5132 RH |
6241 | |
6242 | /* Initialise the fields of the segment map. Set the physical | |
6243 | physical address to the LMA of the first section that has | |
6244 | not yet been assigned. */ | |
0067a569 AM |
6245 | map->next = NULL; |
6246 | map->p_type = segment->p_type; | |
6247 | map->p_flags = segment->p_flags; | |
6248 | map->p_flags_valid = 1; | |
6249 | map->p_paddr = suggested_lma; | |
5c44b38e | 6250 | map->p_paddr_valid = p_paddr_valid; |
bc67d8a6 | 6251 | map->includes_filehdr = 0; |
0067a569 | 6252 | map->includes_phdrs = 0; |
252b5132 RH |
6253 | } |
6254 | } | |
bc67d8a6 | 6255 | while (isec < section_count); |
252b5132 RH |
6256 | |
6257 | free (sections); | |
6258 | } | |
6259 | ||
12bd6957 | 6260 | elf_seg_map (obfd) = map_first; |
bc67d8a6 NC |
6261 | |
6262 | /* If we had to estimate the number of program headers that were | |
9ad5cbcf | 6263 | going to be needed, then check our estimate now and adjust |
bc67d8a6 NC |
6264 | the offset if necessary. */ |
6265 | if (phdr_adjust_seg != NULL) | |
6266 | { | |
6267 | unsigned int count; | |
c044fabd | 6268 | |
bc67d8a6 | 6269 | for (count = 0, map = map_first; map != NULL; map = map->next) |
c044fabd | 6270 | count++; |
252b5132 | 6271 | |
bc67d8a6 NC |
6272 | if (count > phdr_adjust_num) |
6273 | phdr_adjust_seg->p_paddr | |
6274 | -= (count - phdr_adjust_num) * iehdr->e_phentsize; | |
6275 | } | |
c044fabd | 6276 | |
bc67d8a6 | 6277 | #undef SEGMENT_END |
eecdbe52 | 6278 | #undef SECTION_SIZE |
bc67d8a6 NC |
6279 | #undef IS_CONTAINED_BY_VMA |
6280 | #undef IS_CONTAINED_BY_LMA | |
0efc80c8 | 6281 | #undef IS_NOTE |
252b5132 | 6282 | #undef IS_COREFILE_NOTE |
bc67d8a6 | 6283 | #undef IS_SOLARIS_PT_INTERP |
9f17e2a6 | 6284 | #undef IS_SECTION_IN_INPUT_SEGMENT |
bc67d8a6 NC |
6285 | #undef INCLUDE_SECTION_IN_SEGMENT |
6286 | #undef SEGMENT_AFTER_SEGMENT | |
6287 | #undef SEGMENT_OVERLAPS | |
b34976b6 | 6288 | return TRUE; |
252b5132 RH |
6289 | } |
6290 | ||
84d1d650 L |
6291 | /* Copy ELF program header information. */ |
6292 | ||
6293 | static bfd_boolean | |
6294 | copy_elf_program_header (bfd *ibfd, bfd *obfd) | |
6295 | { | |
6296 | Elf_Internal_Ehdr *iehdr; | |
6297 | struct elf_segment_map *map; | |
6298 | struct elf_segment_map *map_first; | |
6299 | struct elf_segment_map **pointer_to_map; | |
6300 | Elf_Internal_Phdr *segment; | |
6301 | unsigned int i; | |
6302 | unsigned int num_segments; | |
6303 | bfd_boolean phdr_included = FALSE; | |
88967714 | 6304 | bfd_boolean p_paddr_valid; |
84d1d650 L |
6305 | |
6306 | iehdr = elf_elfheader (ibfd); | |
6307 | ||
6308 | map_first = NULL; | |
6309 | pointer_to_map = &map_first; | |
6310 | ||
88967714 AM |
6311 | /* If all the segment p_paddr fields are zero, don't set |
6312 | map->p_paddr_valid. */ | |
6313 | p_paddr_valid = FALSE; | |
84d1d650 | 6314 | num_segments = elf_elfheader (ibfd)->e_phnum; |
88967714 AM |
6315 | for (i = 0, segment = elf_tdata (ibfd)->phdr; |
6316 | i < num_segments; | |
6317 | i++, segment++) | |
6318 | if (segment->p_paddr != 0) | |
6319 | { | |
6320 | p_paddr_valid = TRUE; | |
6321 | break; | |
6322 | } | |
6323 | ||
84d1d650 L |
6324 | for (i = 0, segment = elf_tdata (ibfd)->phdr; |
6325 | i < num_segments; | |
6326 | i++, segment++) | |
6327 | { | |
6328 | asection *section; | |
6329 | unsigned int section_count; | |
6330 | bfd_size_type amt; | |
6331 | Elf_Internal_Shdr *this_hdr; | |
53020534 | 6332 | asection *first_section = NULL; |
a76e6f2f | 6333 | asection *lowest_section; |
84d1d650 | 6334 | |
84d1d650 L |
6335 | /* Compute how many sections are in this segment. */ |
6336 | for (section = ibfd->sections, section_count = 0; | |
6337 | section != NULL; | |
6338 | section = section->next) | |
6339 | { | |
6340 | this_hdr = &(elf_section_data(section)->this_hdr); | |
f4638467 | 6341 | if (ELF_SECTION_IN_SEGMENT (this_hdr, segment)) |
3271a814 | 6342 | { |
a76e6f2f AM |
6343 | if (first_section == NULL) |
6344 | first_section = section; | |
3271a814 NS |
6345 | section_count++; |
6346 | } | |
84d1d650 L |
6347 | } |
6348 | ||
6349 | /* Allocate a segment map big enough to contain | |
6350 | all of the sections we have selected. */ | |
6351 | amt = sizeof (struct elf_segment_map); | |
6352 | if (section_count != 0) | |
6353 | amt += ((bfd_size_type) section_count - 1) * sizeof (asection *); | |
a50b1753 | 6354 | map = (struct elf_segment_map *) bfd_zalloc (obfd, amt); |
84d1d650 L |
6355 | if (map == NULL) |
6356 | return FALSE; | |
6357 | ||
6358 | /* Initialize the fields of the output segment map with the | |
6359 | input segment. */ | |
6360 | map->next = NULL; | |
6361 | map->p_type = segment->p_type; | |
6362 | map->p_flags = segment->p_flags; | |
6363 | map->p_flags_valid = 1; | |
6364 | map->p_paddr = segment->p_paddr; | |
88967714 | 6365 | map->p_paddr_valid = p_paddr_valid; |
3f570048 AM |
6366 | map->p_align = segment->p_align; |
6367 | map->p_align_valid = 1; | |
3271a814 | 6368 | map->p_vaddr_offset = 0; |
84d1d650 | 6369 | |
04c3a755 NS |
6370 | if (map->p_type == PT_GNU_RELRO |
6371 | || map->p_type == PT_GNU_STACK) | |
b10a8ae0 L |
6372 | { |
6373 | /* The PT_GNU_RELRO segment may contain the first a few | |
6374 | bytes in the .got.plt section even if the whole .got.plt | |
6375 | section isn't in the PT_GNU_RELRO segment. We won't | |
04c3a755 NS |
6376 | change the size of the PT_GNU_RELRO segment. |
6377 | Similarly, PT_GNU_STACK size is significant on uclinux | |
6378 | systems. */ | |
9433b9b1 | 6379 | map->p_size = segment->p_memsz; |
b10a8ae0 L |
6380 | map->p_size_valid = 1; |
6381 | } | |
6382 | ||
84d1d650 L |
6383 | /* Determine if this segment contains the ELF file header |
6384 | and if it contains the program headers themselves. */ | |
6385 | map->includes_filehdr = (segment->p_offset == 0 | |
6386 | && segment->p_filesz >= iehdr->e_ehsize); | |
6387 | ||
6388 | map->includes_phdrs = 0; | |
6389 | if (! phdr_included || segment->p_type != PT_LOAD) | |
6390 | { | |
6391 | map->includes_phdrs = | |
6392 | (segment->p_offset <= (bfd_vma) iehdr->e_phoff | |
6393 | && (segment->p_offset + segment->p_filesz | |
6394 | >= ((bfd_vma) iehdr->e_phoff | |
6395 | + iehdr->e_phnum * iehdr->e_phentsize))); | |
6396 | ||
6397 | if (segment->p_type == PT_LOAD && map->includes_phdrs) | |
6398 | phdr_included = TRUE; | |
6399 | } | |
6400 | ||
bbefd0a9 | 6401 | lowest_section = NULL; |
84d1d650 L |
6402 | if (section_count != 0) |
6403 | { | |
6404 | unsigned int isec = 0; | |
6405 | ||
53020534 | 6406 | for (section = first_section; |
84d1d650 L |
6407 | section != NULL; |
6408 | section = section->next) | |
6409 | { | |
6410 | this_hdr = &(elf_section_data(section)->this_hdr); | |
f4638467 | 6411 | if (ELF_SECTION_IN_SEGMENT (this_hdr, segment)) |
53020534 L |
6412 | { |
6413 | map->sections[isec++] = section->output_section; | |
a76e6f2f AM |
6414 | if ((section->flags & SEC_ALLOC) != 0) |
6415 | { | |
6416 | bfd_vma seg_off; | |
6417 | ||
bbefd0a9 AM |
6418 | if (lowest_section == NULL |
6419 | || section->lma < lowest_section->lma) | |
fb8a5684 AM |
6420 | lowest_section = section; |
6421 | ||
a76e6f2f AM |
6422 | /* Section lmas are set up from PT_LOAD header |
6423 | p_paddr in _bfd_elf_make_section_from_shdr. | |
6424 | If this header has a p_paddr that disagrees | |
6425 | with the section lma, flag the p_paddr as | |
6426 | invalid. */ | |
6427 | if ((section->flags & SEC_LOAD) != 0) | |
6428 | seg_off = this_hdr->sh_offset - segment->p_offset; | |
6429 | else | |
6430 | seg_off = this_hdr->sh_addr - segment->p_vaddr; | |
6431 | if (section->lma - segment->p_paddr != seg_off) | |
6432 | map->p_paddr_valid = FALSE; | |
6433 | } | |
53020534 L |
6434 | if (isec == section_count) |
6435 | break; | |
6436 | } | |
84d1d650 L |
6437 | } |
6438 | } | |
6439 | ||
a76e6f2f AM |
6440 | if (map->includes_filehdr && lowest_section != NULL) |
6441 | /* We need to keep the space used by the headers fixed. */ | |
6442 | map->header_size = lowest_section->vma - segment->p_vaddr; | |
d324f6d6 | 6443 | |
a76e6f2f AM |
6444 | if (!map->includes_phdrs |
6445 | && !map->includes_filehdr | |
6446 | && map->p_paddr_valid) | |
6447 | /* There is some other padding before the first section. */ | |
6448 | map->p_vaddr_offset = ((lowest_section ? lowest_section->lma : 0) | |
6449 | - segment->p_paddr); | |
6450 | ||
84d1d650 L |
6451 | map->count = section_count; |
6452 | *pointer_to_map = map; | |
6453 | pointer_to_map = &map->next; | |
6454 | } | |
6455 | ||
12bd6957 | 6456 | elf_seg_map (obfd) = map_first; |
84d1d650 L |
6457 | return TRUE; |
6458 | } | |
6459 | ||
6460 | /* Copy private BFD data. This copies or rewrites ELF program header | |
6461 | information. */ | |
6462 | ||
6463 | static bfd_boolean | |
6464 | copy_private_bfd_data (bfd *ibfd, bfd *obfd) | |
6465 | { | |
84d1d650 L |
6466 | if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour |
6467 | || bfd_get_flavour (obfd) != bfd_target_elf_flavour) | |
6468 | return TRUE; | |
6469 | ||
6470 | if (elf_tdata (ibfd)->phdr == NULL) | |
6471 | return TRUE; | |
6472 | ||
6473 | if (ibfd->xvec == obfd->xvec) | |
6474 | { | |
cb3ff1e5 NC |
6475 | /* Check to see if any sections in the input BFD |
6476 | covered by ELF program header have changed. */ | |
d55ce4e2 | 6477 | Elf_Internal_Phdr *segment; |
84d1d650 L |
6478 | asection *section, *osec; |
6479 | unsigned int i, num_segments; | |
6480 | Elf_Internal_Shdr *this_hdr; | |
147d51c2 L |
6481 | const struct elf_backend_data *bed; |
6482 | ||
6483 | bed = get_elf_backend_data (ibfd); | |
6484 | ||
6485 | /* Regenerate the segment map if p_paddr is set to 0. */ | |
6486 | if (bed->want_p_paddr_set_to_zero) | |
6487 | goto rewrite; | |
84d1d650 L |
6488 | |
6489 | /* Initialize the segment mark field. */ | |
6490 | for (section = obfd->sections; section != NULL; | |
6491 | section = section->next) | |
6492 | section->segment_mark = FALSE; | |
6493 | ||
6494 | num_segments = elf_elfheader (ibfd)->e_phnum; | |
6495 | for (i = 0, segment = elf_tdata (ibfd)->phdr; | |
6496 | i < num_segments; | |
6497 | i++, segment++) | |
6498 | { | |
5f6999aa NC |
6499 | /* PR binutils/3535. The Solaris linker always sets the p_paddr |
6500 | and p_memsz fields of special segments (DYNAMIC, INTERP) to 0 | |
6501 | which severly confuses things, so always regenerate the segment | |
6502 | map in this case. */ | |
6503 | if (segment->p_paddr == 0 | |
6504 | && segment->p_memsz == 0 | |
6505 | && (segment->p_type == PT_INTERP || segment->p_type == PT_DYNAMIC)) | |
cb3ff1e5 | 6506 | goto rewrite; |
5f6999aa | 6507 | |
84d1d650 L |
6508 | for (section = ibfd->sections; |
6509 | section != NULL; section = section->next) | |
6510 | { | |
6511 | /* We mark the output section so that we know it comes | |
6512 | from the input BFD. */ | |
6513 | osec = section->output_section; | |
6514 | if (osec) | |
6515 | osec->segment_mark = TRUE; | |
6516 | ||
6517 | /* Check if this section is covered by the segment. */ | |
6518 | this_hdr = &(elf_section_data(section)->this_hdr); | |
f4638467 | 6519 | if (ELF_SECTION_IN_SEGMENT (this_hdr, segment)) |
84d1d650 L |
6520 | { |
6521 | /* FIXME: Check if its output section is changed or | |
6522 | removed. What else do we need to check? */ | |
6523 | if (osec == NULL | |
6524 | || section->flags != osec->flags | |
6525 | || section->lma != osec->lma | |
6526 | || section->vma != osec->vma | |
6527 | || section->size != osec->size | |
6528 | || section->rawsize != osec->rawsize | |
6529 | || section->alignment_power != osec->alignment_power) | |
6530 | goto rewrite; | |
6531 | } | |
6532 | } | |
6533 | } | |
6534 | ||
cb3ff1e5 | 6535 | /* Check to see if any output section do not come from the |
84d1d650 L |
6536 | input BFD. */ |
6537 | for (section = obfd->sections; section != NULL; | |
6538 | section = section->next) | |
6539 | { | |
6540 | if (section->segment_mark == FALSE) | |
6541 | goto rewrite; | |
6542 | else | |
6543 | section->segment_mark = FALSE; | |
6544 | } | |
6545 | ||
6546 | return copy_elf_program_header (ibfd, obfd); | |
6547 | } | |
6548 | ||
6549 | rewrite: | |
f1d85785 L |
6550 | if (ibfd->xvec == obfd->xvec) |
6551 | { | |
6552 | /* When rewriting program header, set the output maxpagesize to | |
6553 | the maximum alignment of input PT_LOAD segments. */ | |
6554 | Elf_Internal_Phdr *segment; | |
6555 | unsigned int i; | |
6556 | unsigned int num_segments = elf_elfheader (ibfd)->e_phnum; | |
6557 | bfd_vma maxpagesize = 0; | |
6558 | ||
6559 | for (i = 0, segment = elf_tdata (ibfd)->phdr; | |
6560 | i < num_segments; | |
6561 | i++, segment++) | |
6562 | if (segment->p_type == PT_LOAD | |
6563 | && maxpagesize < segment->p_align) | |
6564 | maxpagesize = segment->p_align; | |
6565 | ||
6566 | if (maxpagesize != get_elf_backend_data (obfd)->maxpagesize) | |
6567 | bfd_emul_set_maxpagesize (bfd_get_target (obfd), maxpagesize); | |
6568 | } | |
6569 | ||
84d1d650 L |
6570 | return rewrite_elf_program_header (ibfd, obfd); |
6571 | } | |
6572 | ||
ccd2ec6a L |
6573 | /* Initialize private output section information from input section. */ |
6574 | ||
6575 | bfd_boolean | |
6576 | _bfd_elf_init_private_section_data (bfd *ibfd, | |
6577 | asection *isec, | |
6578 | bfd *obfd, | |
6579 | asection *osec, | |
6580 | struct bfd_link_info *link_info) | |
6581 | ||
6582 | { | |
6583 | Elf_Internal_Shdr *ihdr, *ohdr; | |
dfa7b0b8 | 6584 | bfd_boolean final_link = link_info != NULL && !link_info->relocatable; |
ccd2ec6a L |
6585 | |
6586 | if (ibfd->xvec->flavour != bfd_target_elf_flavour | |
6587 | || obfd->xvec->flavour != bfd_target_elf_flavour) | |
6588 | return TRUE; | |
6589 | ||
ba85c43e NC |
6590 | BFD_ASSERT (elf_section_data (osec) != NULL); |
6591 | ||
dfa7b0b8 AM |
6592 | /* For objcopy and relocatable link, don't copy the output ELF |
6593 | section type from input if the output BFD section flags have been | |
6594 | set to something different. For a final link allow some flags | |
6595 | that the linker clears to differ. */ | |
42bb2e33 | 6596 | if (elf_section_type (osec) == SHT_NULL |
dfa7b0b8 AM |
6597 | && (osec->flags == isec->flags |
6598 | || (final_link | |
6599 | && ((osec->flags ^ isec->flags) | |
0814be7d | 6600 | & ~(SEC_LINK_ONCE | SEC_LINK_DUPLICATES | SEC_RELOC)) == 0))) |
42bb2e33 | 6601 | elf_section_type (osec) = elf_section_type (isec); |
d270463e L |
6602 | |
6603 | /* FIXME: Is this correct for all OS/PROC specific flags? */ | |
6604 | elf_section_flags (osec) |= (elf_section_flags (isec) | |
6605 | & (SHF_MASKOS | SHF_MASKPROC)); | |
ccd2ec6a L |
6606 | |
6607 | /* Set things up for objcopy and relocatable link. The output | |
6608 | SHT_GROUP section will have its elf_next_in_group pointing back | |
6609 | to the input group members. Ignore linker created group section. | |
6610 | See elfNN_ia64_object_p in elfxx-ia64.c. */ | |
dfa7b0b8 | 6611 | if (!final_link) |
ccd2ec6a L |
6612 | { |
6613 | if (elf_sec_group (isec) == NULL | |
6614 | || (elf_sec_group (isec)->flags & SEC_LINKER_CREATED) == 0) | |
6615 | { | |
6616 | if (elf_section_flags (isec) & SHF_GROUP) | |
6617 | elf_section_flags (osec) |= SHF_GROUP; | |
6618 | elf_next_in_group (osec) = elf_next_in_group (isec); | |
9659de1c | 6619 | elf_section_data (osec)->group = elf_section_data (isec)->group; |
ccd2ec6a L |
6620 | } |
6621 | } | |
6622 | ||
6623 | ihdr = &elf_section_data (isec)->this_hdr; | |
6624 | ||
6625 | /* We need to handle elf_linked_to_section for SHF_LINK_ORDER. We | |
6626 | don't use the output section of the linked-to section since it | |
6627 | may be NULL at this point. */ | |
6628 | if ((ihdr->sh_flags & SHF_LINK_ORDER) != 0) | |
6629 | { | |
6630 | ohdr = &elf_section_data (osec)->this_hdr; | |
6631 | ohdr->sh_flags |= SHF_LINK_ORDER; | |
6632 | elf_linked_to_section (osec) = elf_linked_to_section (isec); | |
6633 | } | |
6634 | ||
6635 | osec->use_rela_p = isec->use_rela_p; | |
6636 | ||
6637 | return TRUE; | |
6638 | } | |
6639 | ||
252b5132 RH |
6640 | /* Copy private section information. This copies over the entsize |
6641 | field, and sometimes the info field. */ | |
6642 | ||
b34976b6 | 6643 | bfd_boolean |
217aa764 AM |
6644 | _bfd_elf_copy_private_section_data (bfd *ibfd, |
6645 | asection *isec, | |
6646 | bfd *obfd, | |
6647 | asection *osec) | |
252b5132 RH |
6648 | { |
6649 | Elf_Internal_Shdr *ihdr, *ohdr; | |
6650 | ||
6651 | if (ibfd->xvec->flavour != bfd_target_elf_flavour | |
6652 | || obfd->xvec->flavour != bfd_target_elf_flavour) | |
b34976b6 | 6653 | return TRUE; |
252b5132 | 6654 | |
252b5132 RH |
6655 | ihdr = &elf_section_data (isec)->this_hdr; |
6656 | ohdr = &elf_section_data (osec)->this_hdr; | |
6657 | ||
6658 | ohdr->sh_entsize = ihdr->sh_entsize; | |
6659 | ||
6660 | if (ihdr->sh_type == SHT_SYMTAB | |
6661 | || ihdr->sh_type == SHT_DYNSYM | |
6662 | || ihdr->sh_type == SHT_GNU_verneed | |
6663 | || ihdr->sh_type == SHT_GNU_verdef) | |
6664 | ohdr->sh_info = ihdr->sh_info; | |
6665 | ||
ccd2ec6a L |
6666 | return _bfd_elf_init_private_section_data (ibfd, isec, obfd, osec, |
6667 | NULL); | |
252b5132 RH |
6668 | } |
6669 | ||
d0bf826b AM |
6670 | /* Look at all the SHT_GROUP sections in IBFD, making any adjustments |
6671 | necessary if we are removing either the SHT_GROUP section or any of | |
6672 | the group member sections. DISCARDED is the value that a section's | |
6673 | output_section has if the section will be discarded, NULL when this | |
6674 | function is called from objcopy, bfd_abs_section_ptr when called | |
6675 | from the linker. */ | |
80fccad2 BW |
6676 | |
6677 | bfd_boolean | |
d0bf826b | 6678 | _bfd_elf_fixup_group_sections (bfd *ibfd, asection *discarded) |
80fccad2 | 6679 | { |
30288845 AM |
6680 | asection *isec; |
6681 | ||
30288845 | 6682 | for (isec = ibfd->sections; isec != NULL; isec = isec->next) |
415f38a6 | 6683 | if (elf_section_type (isec) == SHT_GROUP) |
30288845 AM |
6684 | { |
6685 | asection *first = elf_next_in_group (isec); | |
6686 | asection *s = first; | |
d0bf826b AM |
6687 | bfd_size_type removed = 0; |
6688 | ||
30288845 AM |
6689 | while (s != NULL) |
6690 | { | |
415f38a6 AM |
6691 | /* If this member section is being output but the |
6692 | SHT_GROUP section is not, then clear the group info | |
6693 | set up by _bfd_elf_copy_private_section_data. */ | |
d0bf826b AM |
6694 | if (s->output_section != discarded |
6695 | && isec->output_section == discarded) | |
30288845 AM |
6696 | { |
6697 | elf_section_flags (s->output_section) &= ~SHF_GROUP; | |
6698 | elf_group_name (s->output_section) = NULL; | |
6699 | } | |
415f38a6 AM |
6700 | /* Conversely, if the member section is not being output |
6701 | but the SHT_GROUP section is, then adjust its size. */ | |
d0bf826b AM |
6702 | else if (s->output_section == discarded |
6703 | && isec->output_section != discarded) | |
6704 | removed += 4; | |
30288845 AM |
6705 | s = elf_next_in_group (s); |
6706 | if (s == first) | |
6707 | break; | |
6708 | } | |
d0bf826b AM |
6709 | if (removed != 0) |
6710 | { | |
6711 | if (discarded != NULL) | |
6712 | { | |
6713 | /* If we've been called for ld -r, then we need to | |
6714 | adjust the input section size. This function may | |
6715 | be called multiple times, so save the original | |
6716 | size. */ | |
6717 | if (isec->rawsize == 0) | |
6718 | isec->rawsize = isec->size; | |
6719 | isec->size = isec->rawsize - removed; | |
6720 | } | |
6721 | else | |
6722 | { | |
6723 | /* Adjust the output section size when called from | |
6724 | objcopy. */ | |
6725 | isec->output_section->size -= removed; | |
6726 | } | |
6727 | } | |
30288845 AM |
6728 | } |
6729 | ||
80fccad2 BW |
6730 | return TRUE; |
6731 | } | |
6732 | ||
d0bf826b AM |
6733 | /* Copy private header information. */ |
6734 | ||
6735 | bfd_boolean | |
6736 | _bfd_elf_copy_private_header_data (bfd *ibfd, bfd *obfd) | |
6737 | { | |
6738 | if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour | |
6739 | || bfd_get_flavour (obfd) != bfd_target_elf_flavour) | |
6740 | return TRUE; | |
6741 | ||
6742 | /* Copy over private BFD data if it has not already been copied. | |
6743 | This must be done here, rather than in the copy_private_bfd_data | |
6744 | entry point, because the latter is called after the section | |
6745 | contents have been set, which means that the program headers have | |
6746 | already been worked out. */ | |
12bd6957 | 6747 | if (elf_seg_map (obfd) == NULL && elf_tdata (ibfd)->phdr != NULL) |
d0bf826b AM |
6748 | { |
6749 | if (! copy_private_bfd_data (ibfd, obfd)) | |
6750 | return FALSE; | |
6751 | } | |
6752 | ||
6753 | return _bfd_elf_fixup_group_sections (ibfd, NULL); | |
6754 | } | |
6755 | ||
252b5132 RH |
6756 | /* Copy private symbol information. If this symbol is in a section |
6757 | which we did not map into a BFD section, try to map the section | |
6758 | index correctly. We use special macro definitions for the mapped | |
6759 | section indices; these definitions are interpreted by the | |
6760 | swap_out_syms function. */ | |
6761 | ||
9ad5cbcf AM |
6762 | #define MAP_ONESYMTAB (SHN_HIOS + 1) |
6763 | #define MAP_DYNSYMTAB (SHN_HIOS + 2) | |
6764 | #define MAP_STRTAB (SHN_HIOS + 3) | |
6765 | #define MAP_SHSTRTAB (SHN_HIOS + 4) | |
6766 | #define MAP_SYM_SHNDX (SHN_HIOS + 5) | |
252b5132 | 6767 | |
b34976b6 | 6768 | bfd_boolean |
217aa764 AM |
6769 | _bfd_elf_copy_private_symbol_data (bfd *ibfd, |
6770 | asymbol *isymarg, | |
6771 | bfd *obfd, | |
6772 | asymbol *osymarg) | |
252b5132 RH |
6773 | { |
6774 | elf_symbol_type *isym, *osym; | |
6775 | ||
6776 | if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour | |
6777 | || bfd_get_flavour (obfd) != bfd_target_elf_flavour) | |
b34976b6 | 6778 | return TRUE; |
252b5132 RH |
6779 | |
6780 | isym = elf_symbol_from (ibfd, isymarg); | |
6781 | osym = elf_symbol_from (obfd, osymarg); | |
6782 | ||
6783 | if (isym != NULL | |
8424d8f5 | 6784 | && isym->internal_elf_sym.st_shndx != 0 |
252b5132 RH |
6785 | && osym != NULL |
6786 | && bfd_is_abs_section (isym->symbol.section)) | |
6787 | { | |
6788 | unsigned int shndx; | |
6789 | ||
6790 | shndx = isym->internal_elf_sym.st_shndx; | |
6791 | if (shndx == elf_onesymtab (ibfd)) | |
6792 | shndx = MAP_ONESYMTAB; | |
6793 | else if (shndx == elf_dynsymtab (ibfd)) | |
6794 | shndx = MAP_DYNSYMTAB; | |
12bd6957 | 6795 | else if (shndx == elf_strtab_sec (ibfd)) |
252b5132 | 6796 | shndx = MAP_STRTAB; |
12bd6957 | 6797 | else if (shndx == elf_shstrtab_sec (ibfd)) |
252b5132 | 6798 | shndx = MAP_SHSTRTAB; |
12bd6957 | 6799 | else if (shndx == elf_symtab_shndx (ibfd)) |
9ad5cbcf | 6800 | shndx = MAP_SYM_SHNDX; |
252b5132 RH |
6801 | osym->internal_elf_sym.st_shndx = shndx; |
6802 | } | |
6803 | ||
b34976b6 | 6804 | return TRUE; |
252b5132 RH |
6805 | } |
6806 | ||
6807 | /* Swap out the symbols. */ | |
6808 | ||
b34976b6 | 6809 | static bfd_boolean |
217aa764 AM |
6810 | swap_out_syms (bfd *abfd, |
6811 | struct bfd_strtab_hash **sttp, | |
6812 | int relocatable_p) | |
252b5132 | 6813 | { |
9c5bfbb7 | 6814 | const struct elf_backend_data *bed; |
079e9a2f AM |
6815 | int symcount; |
6816 | asymbol **syms; | |
6817 | struct bfd_strtab_hash *stt; | |
6818 | Elf_Internal_Shdr *symtab_hdr; | |
9ad5cbcf | 6819 | Elf_Internal_Shdr *symtab_shndx_hdr; |
079e9a2f | 6820 | Elf_Internal_Shdr *symstrtab_hdr; |
f075ee0c AM |
6821 | bfd_byte *outbound_syms; |
6822 | bfd_byte *outbound_shndx; | |
079e9a2f | 6823 | int idx; |
12bd6957 | 6824 | unsigned int num_locals; |
079e9a2f | 6825 | bfd_size_type amt; |
174fd7f9 | 6826 | bfd_boolean name_local_sections; |
252b5132 | 6827 | |
12bd6957 | 6828 | if (!elf_map_symbols (abfd, &num_locals)) |
b34976b6 | 6829 | return FALSE; |
252b5132 | 6830 | |
c044fabd | 6831 | /* Dump out the symtabs. */ |
079e9a2f AM |
6832 | stt = _bfd_elf_stringtab_init (); |
6833 | if (stt == NULL) | |
b34976b6 | 6834 | return FALSE; |
252b5132 | 6835 | |
079e9a2f AM |
6836 | bed = get_elf_backend_data (abfd); |
6837 | symcount = bfd_get_symcount (abfd); | |
6838 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; | |
6839 | symtab_hdr->sh_type = SHT_SYMTAB; | |
6840 | symtab_hdr->sh_entsize = bed->s->sizeof_sym; | |
6841 | symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1); | |
12bd6957 | 6842 | symtab_hdr->sh_info = num_locals + 1; |
72de5009 | 6843 | symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align; |
079e9a2f AM |
6844 | |
6845 | symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr; | |
6846 | symstrtab_hdr->sh_type = SHT_STRTAB; | |
6847 | ||
a50b1753 NC |
6848 | outbound_syms = (bfd_byte *) bfd_alloc2 (abfd, 1 + symcount, |
6849 | bed->s->sizeof_sym); | |
079e9a2f | 6850 | if (outbound_syms == NULL) |
5ed6aba4 NC |
6851 | { |
6852 | _bfd_stringtab_free (stt); | |
6853 | return FALSE; | |
6854 | } | |
217aa764 | 6855 | symtab_hdr->contents = outbound_syms; |
252b5132 | 6856 | |
9ad5cbcf AM |
6857 | outbound_shndx = NULL; |
6858 | symtab_shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr; | |
6859 | if (symtab_shndx_hdr->sh_name != 0) | |
6860 | { | |
6861 | amt = (bfd_size_type) (1 + symcount) * sizeof (Elf_External_Sym_Shndx); | |
a50b1753 NC |
6862 | outbound_shndx = (bfd_byte *) |
6863 | bfd_zalloc2 (abfd, 1 + symcount, sizeof (Elf_External_Sym_Shndx)); | |
9ad5cbcf | 6864 | if (outbound_shndx == NULL) |
5ed6aba4 NC |
6865 | { |
6866 | _bfd_stringtab_free (stt); | |
6867 | return FALSE; | |
6868 | } | |
6869 | ||
9ad5cbcf AM |
6870 | symtab_shndx_hdr->contents = outbound_shndx; |
6871 | symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX; | |
6872 | symtab_shndx_hdr->sh_size = amt; | |
6873 | symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx); | |
6874 | symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx); | |
6875 | } | |
6876 | ||
589e6347 | 6877 | /* Now generate the data (for "contents"). */ |
079e9a2f AM |
6878 | { |
6879 | /* Fill in zeroth symbol and swap it out. */ | |
6880 | Elf_Internal_Sym sym; | |
6881 | sym.st_name = 0; | |
6882 | sym.st_value = 0; | |
6883 | sym.st_size = 0; | |
6884 | sym.st_info = 0; | |
6885 | sym.st_other = 0; | |
6886 | sym.st_shndx = SHN_UNDEF; | |
35fc36a8 | 6887 | sym.st_target_internal = 0; |
9ad5cbcf | 6888 | bed->s->swap_symbol_out (abfd, &sym, outbound_syms, outbound_shndx); |
079e9a2f | 6889 | outbound_syms += bed->s->sizeof_sym; |
9ad5cbcf AM |
6890 | if (outbound_shndx != NULL) |
6891 | outbound_shndx += sizeof (Elf_External_Sym_Shndx); | |
079e9a2f | 6892 | } |
252b5132 | 6893 | |
174fd7f9 RS |
6894 | name_local_sections |
6895 | = (bed->elf_backend_name_local_section_symbols | |
6896 | && bed->elf_backend_name_local_section_symbols (abfd)); | |
6897 | ||
079e9a2f AM |
6898 | syms = bfd_get_outsymbols (abfd); |
6899 | for (idx = 0; idx < symcount; idx++) | |
252b5132 | 6900 | { |
252b5132 | 6901 | Elf_Internal_Sym sym; |
079e9a2f AM |
6902 | bfd_vma value = syms[idx]->value; |
6903 | elf_symbol_type *type_ptr; | |
6904 | flagword flags = syms[idx]->flags; | |
6905 | int type; | |
252b5132 | 6906 | |
174fd7f9 RS |
6907 | if (!name_local_sections |
6908 | && (flags & (BSF_SECTION_SYM | BSF_GLOBAL)) == BSF_SECTION_SYM) | |
079e9a2f AM |
6909 | { |
6910 | /* Local section symbols have no name. */ | |
6911 | sym.st_name = 0; | |
6912 | } | |
6913 | else | |
6914 | { | |
6915 | sym.st_name = (unsigned long) _bfd_stringtab_add (stt, | |
6916 | syms[idx]->name, | |
b34976b6 | 6917 | TRUE, FALSE); |
079e9a2f | 6918 | if (sym.st_name == (unsigned long) -1) |
5ed6aba4 NC |
6919 | { |
6920 | _bfd_stringtab_free (stt); | |
6921 | return FALSE; | |
6922 | } | |
079e9a2f | 6923 | } |
252b5132 | 6924 | |
079e9a2f | 6925 | type_ptr = elf_symbol_from (abfd, syms[idx]); |
252b5132 | 6926 | |
079e9a2f AM |
6927 | if ((flags & BSF_SECTION_SYM) == 0 |
6928 | && bfd_is_com_section (syms[idx]->section)) | |
6929 | { | |
6930 | /* ELF common symbols put the alignment into the `value' field, | |
6931 | and the size into the `size' field. This is backwards from | |
6932 | how BFD handles it, so reverse it here. */ | |
6933 | sym.st_size = value; | |
6934 | if (type_ptr == NULL | |
6935 | || type_ptr->internal_elf_sym.st_value == 0) | |
6936 | sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value)); | |
6937 | else | |
6938 | sym.st_value = type_ptr->internal_elf_sym.st_value; | |
6939 | sym.st_shndx = _bfd_elf_section_from_bfd_section | |
6940 | (abfd, syms[idx]->section); | |
6941 | } | |
6942 | else | |
6943 | { | |
6944 | asection *sec = syms[idx]->section; | |
cb33740c | 6945 | unsigned int shndx; |
252b5132 | 6946 | |
079e9a2f AM |
6947 | if (sec->output_section) |
6948 | { | |
6949 | value += sec->output_offset; | |
6950 | sec = sec->output_section; | |
6951 | } | |
589e6347 | 6952 | |
079e9a2f AM |
6953 | /* Don't add in the section vma for relocatable output. */ |
6954 | if (! relocatable_p) | |
6955 | value += sec->vma; | |
6956 | sym.st_value = value; | |
6957 | sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0; | |
6958 | ||
6959 | if (bfd_is_abs_section (sec) | |
6960 | && type_ptr != NULL | |
6961 | && type_ptr->internal_elf_sym.st_shndx != 0) | |
6962 | { | |
6963 | /* This symbol is in a real ELF section which we did | |
6964 | not create as a BFD section. Undo the mapping done | |
6965 | by copy_private_symbol_data. */ | |
6966 | shndx = type_ptr->internal_elf_sym.st_shndx; | |
6967 | switch (shndx) | |
6968 | { | |
6969 | case MAP_ONESYMTAB: | |
6970 | shndx = elf_onesymtab (abfd); | |
6971 | break; | |
6972 | case MAP_DYNSYMTAB: | |
6973 | shndx = elf_dynsymtab (abfd); | |
6974 | break; | |
6975 | case MAP_STRTAB: | |
12bd6957 | 6976 | shndx = elf_strtab_sec (abfd); |
079e9a2f AM |
6977 | break; |
6978 | case MAP_SHSTRTAB: | |
12bd6957 | 6979 | shndx = elf_shstrtab_sec (abfd); |
079e9a2f | 6980 | break; |
9ad5cbcf | 6981 | case MAP_SYM_SHNDX: |
12bd6957 | 6982 | shndx = elf_symtab_shndx (abfd); |
9ad5cbcf | 6983 | break; |
079e9a2f | 6984 | default: |
15bc576a | 6985 | shndx = SHN_ABS; |
079e9a2f AM |
6986 | break; |
6987 | } | |
6988 | } | |
6989 | else | |
6990 | { | |
6991 | shndx = _bfd_elf_section_from_bfd_section (abfd, sec); | |
252b5132 | 6992 | |
cb33740c | 6993 | if (shndx == SHN_BAD) |
079e9a2f AM |
6994 | { |
6995 | asection *sec2; | |
6996 | ||
6997 | /* Writing this would be a hell of a lot easier if | |
6998 | we had some decent documentation on bfd, and | |
6999 | knew what to expect of the library, and what to | |
7000 | demand of applications. For example, it | |
7001 | appears that `objcopy' might not set the | |
7002 | section of a symbol to be a section that is | |
7003 | actually in the output file. */ | |
7004 | sec2 = bfd_get_section_by_name (abfd, sec->name); | |
589e6347 NC |
7005 | if (sec2 == NULL) |
7006 | { | |
7007 | _bfd_error_handler (_("\ | |
7008 | Unable to find equivalent output section for symbol '%s' from section '%s'"), | |
7009 | syms[idx]->name ? syms[idx]->name : "<Local sym>", | |
7010 | sec->name); | |
811072d8 | 7011 | bfd_set_error (bfd_error_invalid_operation); |
5ed6aba4 | 7012 | _bfd_stringtab_free (stt); |
589e6347 NC |
7013 | return FALSE; |
7014 | } | |
811072d8 | 7015 | |
079e9a2f | 7016 | shndx = _bfd_elf_section_from_bfd_section (abfd, sec2); |
cb33740c | 7017 | BFD_ASSERT (shndx != SHN_BAD); |
079e9a2f AM |
7018 | } |
7019 | } | |
252b5132 | 7020 | |
079e9a2f AM |
7021 | sym.st_shndx = shndx; |
7022 | } | |
252b5132 | 7023 | |
13ae64f3 JJ |
7024 | if ((flags & BSF_THREAD_LOCAL) != 0) |
7025 | type = STT_TLS; | |
d8045f23 NC |
7026 | else if ((flags & BSF_GNU_INDIRECT_FUNCTION) != 0) |
7027 | type = STT_GNU_IFUNC; | |
13ae64f3 | 7028 | else if ((flags & BSF_FUNCTION) != 0) |
079e9a2f AM |
7029 | type = STT_FUNC; |
7030 | else if ((flags & BSF_OBJECT) != 0) | |
7031 | type = STT_OBJECT; | |
d9352518 DB |
7032 | else if ((flags & BSF_RELC) != 0) |
7033 | type = STT_RELC; | |
7034 | else if ((flags & BSF_SRELC) != 0) | |
7035 | type = STT_SRELC; | |
079e9a2f AM |
7036 | else |
7037 | type = STT_NOTYPE; | |
252b5132 | 7038 | |
13ae64f3 JJ |
7039 | if (syms[idx]->section->flags & SEC_THREAD_LOCAL) |
7040 | type = STT_TLS; | |
7041 | ||
589e6347 | 7042 | /* Processor-specific types. */ |
079e9a2f AM |
7043 | if (type_ptr != NULL |
7044 | && bed->elf_backend_get_symbol_type) | |
7045 | type = ((*bed->elf_backend_get_symbol_type) | |
7046 | (&type_ptr->internal_elf_sym, type)); | |
252b5132 | 7047 | |
079e9a2f AM |
7048 | if (flags & BSF_SECTION_SYM) |
7049 | { | |
7050 | if (flags & BSF_GLOBAL) | |
7051 | sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION); | |
7052 | else | |
7053 | sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION); | |
7054 | } | |
7055 | else if (bfd_is_com_section (syms[idx]->section)) | |
0a40daed | 7056 | { |
504b7d20 | 7057 | #ifdef USE_STT_COMMON |
0a40daed MK |
7058 | if (type == STT_OBJECT) |
7059 | sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_COMMON); | |
7060 | else | |
504b7d20 | 7061 | #endif |
c91e322a | 7062 | sym.st_info = ELF_ST_INFO (STB_GLOBAL, type); |
0a40daed | 7063 | } |
079e9a2f AM |
7064 | else if (bfd_is_und_section (syms[idx]->section)) |
7065 | sym.st_info = ELF_ST_INFO (((flags & BSF_WEAK) | |
7066 | ? STB_WEAK | |
7067 | : STB_GLOBAL), | |
7068 | type); | |
7069 | else if (flags & BSF_FILE) | |
7070 | sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE); | |
7071 | else | |
7072 | { | |
7073 | int bind = STB_LOCAL; | |
252b5132 | 7074 | |
079e9a2f AM |
7075 | if (flags & BSF_LOCAL) |
7076 | bind = STB_LOCAL; | |
3e7a7d11 NC |
7077 | else if (flags & BSF_GNU_UNIQUE) |
7078 | bind = STB_GNU_UNIQUE; | |
079e9a2f AM |
7079 | else if (flags & BSF_WEAK) |
7080 | bind = STB_WEAK; | |
7081 | else if (flags & BSF_GLOBAL) | |
7082 | bind = STB_GLOBAL; | |
252b5132 | 7083 | |
079e9a2f AM |
7084 | sym.st_info = ELF_ST_INFO (bind, type); |
7085 | } | |
252b5132 | 7086 | |
079e9a2f | 7087 | if (type_ptr != NULL) |
35fc36a8 RS |
7088 | { |
7089 | sym.st_other = type_ptr->internal_elf_sym.st_other; | |
7090 | sym.st_target_internal | |
7091 | = type_ptr->internal_elf_sym.st_target_internal; | |
7092 | } | |
079e9a2f | 7093 | else |
35fc36a8 RS |
7094 | { |
7095 | sym.st_other = 0; | |
7096 | sym.st_target_internal = 0; | |
7097 | } | |
252b5132 | 7098 | |
9ad5cbcf | 7099 | bed->s->swap_symbol_out (abfd, &sym, outbound_syms, outbound_shndx); |
079e9a2f | 7100 | outbound_syms += bed->s->sizeof_sym; |
9ad5cbcf AM |
7101 | if (outbound_shndx != NULL) |
7102 | outbound_shndx += sizeof (Elf_External_Sym_Shndx); | |
079e9a2f | 7103 | } |
252b5132 | 7104 | |
079e9a2f AM |
7105 | *sttp = stt; |
7106 | symstrtab_hdr->sh_size = _bfd_stringtab_size (stt); | |
7107 | symstrtab_hdr->sh_type = SHT_STRTAB; | |
252b5132 | 7108 | |
079e9a2f AM |
7109 | symstrtab_hdr->sh_flags = 0; |
7110 | symstrtab_hdr->sh_addr = 0; | |
7111 | symstrtab_hdr->sh_entsize = 0; | |
7112 | symstrtab_hdr->sh_link = 0; | |
7113 | symstrtab_hdr->sh_info = 0; | |
7114 | symstrtab_hdr->sh_addralign = 1; | |
252b5132 | 7115 | |
b34976b6 | 7116 | return TRUE; |
252b5132 RH |
7117 | } |
7118 | ||
7119 | /* Return the number of bytes required to hold the symtab vector. | |
7120 | ||
7121 | Note that we base it on the count plus 1, since we will null terminate | |
7122 | the vector allocated based on this size. However, the ELF symbol table | |
7123 | always has a dummy entry as symbol #0, so it ends up even. */ | |
7124 | ||
7125 | long | |
217aa764 | 7126 | _bfd_elf_get_symtab_upper_bound (bfd *abfd) |
252b5132 RH |
7127 | { |
7128 | long symcount; | |
7129 | long symtab_size; | |
7130 | Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr; | |
7131 | ||
7132 | symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym; | |
b99d1833 AM |
7133 | symtab_size = (symcount + 1) * (sizeof (asymbol *)); |
7134 | if (symcount > 0) | |
7135 | symtab_size -= sizeof (asymbol *); | |
252b5132 RH |
7136 | |
7137 | return symtab_size; | |
7138 | } | |
7139 | ||
7140 | long | |
217aa764 | 7141 | _bfd_elf_get_dynamic_symtab_upper_bound (bfd *abfd) |
252b5132 RH |
7142 | { |
7143 | long symcount; | |
7144 | long symtab_size; | |
7145 | Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr; | |
7146 | ||
7147 | if (elf_dynsymtab (abfd) == 0) | |
7148 | { | |
7149 | bfd_set_error (bfd_error_invalid_operation); | |
7150 | return -1; | |
7151 | } | |
7152 | ||
7153 | symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym; | |
b99d1833 AM |
7154 | symtab_size = (symcount + 1) * (sizeof (asymbol *)); |
7155 | if (symcount > 0) | |
7156 | symtab_size -= sizeof (asymbol *); | |
252b5132 RH |
7157 | |
7158 | return symtab_size; | |
7159 | } | |
7160 | ||
7161 | long | |
217aa764 AM |
7162 | _bfd_elf_get_reloc_upper_bound (bfd *abfd ATTRIBUTE_UNUSED, |
7163 | sec_ptr asect) | |
252b5132 RH |
7164 | { |
7165 | return (asect->reloc_count + 1) * sizeof (arelent *); | |
7166 | } | |
7167 | ||
7168 | /* Canonicalize the relocs. */ | |
7169 | ||
7170 | long | |
217aa764 AM |
7171 | _bfd_elf_canonicalize_reloc (bfd *abfd, |
7172 | sec_ptr section, | |
7173 | arelent **relptr, | |
7174 | asymbol **symbols) | |
252b5132 RH |
7175 | { |
7176 | arelent *tblptr; | |
7177 | unsigned int i; | |
9c5bfbb7 | 7178 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
252b5132 | 7179 | |
b34976b6 | 7180 | if (! bed->s->slurp_reloc_table (abfd, section, symbols, FALSE)) |
252b5132 RH |
7181 | return -1; |
7182 | ||
7183 | tblptr = section->relocation; | |
7184 | for (i = 0; i < section->reloc_count; i++) | |
7185 | *relptr++ = tblptr++; | |
7186 | ||
7187 | *relptr = NULL; | |
7188 | ||
7189 | return section->reloc_count; | |
7190 | } | |
7191 | ||
7192 | long | |
6cee3f79 | 7193 | _bfd_elf_canonicalize_symtab (bfd *abfd, asymbol **allocation) |
252b5132 | 7194 | { |
9c5bfbb7 | 7195 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
217aa764 | 7196 | long symcount = bed->s->slurp_symbol_table (abfd, allocation, FALSE); |
252b5132 RH |
7197 | |
7198 | if (symcount >= 0) | |
7199 | bfd_get_symcount (abfd) = symcount; | |
7200 | return symcount; | |
7201 | } | |
7202 | ||
7203 | long | |
217aa764 AM |
7204 | _bfd_elf_canonicalize_dynamic_symtab (bfd *abfd, |
7205 | asymbol **allocation) | |
252b5132 | 7206 | { |
9c5bfbb7 | 7207 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
217aa764 | 7208 | long symcount = bed->s->slurp_symbol_table (abfd, allocation, TRUE); |
1f70368c DJ |
7209 | |
7210 | if (symcount >= 0) | |
7211 | bfd_get_dynamic_symcount (abfd) = symcount; | |
7212 | return symcount; | |
252b5132 RH |
7213 | } |
7214 | ||
8615f3f2 AM |
7215 | /* Return the size required for the dynamic reloc entries. Any loadable |
7216 | section that was actually installed in the BFD, and has type SHT_REL | |
7217 | or SHT_RELA, and uses the dynamic symbol table, is considered to be a | |
7218 | dynamic reloc section. */ | |
252b5132 RH |
7219 | |
7220 | long | |
217aa764 | 7221 | _bfd_elf_get_dynamic_reloc_upper_bound (bfd *abfd) |
252b5132 RH |
7222 | { |
7223 | long ret; | |
7224 | asection *s; | |
7225 | ||
7226 | if (elf_dynsymtab (abfd) == 0) | |
7227 | { | |
7228 | bfd_set_error (bfd_error_invalid_operation); | |
7229 | return -1; | |
7230 | } | |
7231 | ||
7232 | ret = sizeof (arelent *); | |
7233 | for (s = abfd->sections; s != NULL; s = s->next) | |
266b05cf | 7234 | if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd) |
252b5132 RH |
7235 | && (elf_section_data (s)->this_hdr.sh_type == SHT_REL |
7236 | || elf_section_data (s)->this_hdr.sh_type == SHT_RELA)) | |
eea6121a | 7237 | ret += ((s->size / elf_section_data (s)->this_hdr.sh_entsize) |
252b5132 RH |
7238 | * sizeof (arelent *)); |
7239 | ||
7240 | return ret; | |
7241 | } | |
7242 | ||
8615f3f2 AM |
7243 | /* Canonicalize the dynamic relocation entries. Note that we return the |
7244 | dynamic relocations as a single block, although they are actually | |
7245 | associated with particular sections; the interface, which was | |
7246 | designed for SunOS style shared libraries, expects that there is only | |
7247 | one set of dynamic relocs. Any loadable section that was actually | |
7248 | installed in the BFD, and has type SHT_REL or SHT_RELA, and uses the | |
7249 | dynamic symbol table, is considered to be a dynamic reloc section. */ | |
252b5132 RH |
7250 | |
7251 | long | |
217aa764 AM |
7252 | _bfd_elf_canonicalize_dynamic_reloc (bfd *abfd, |
7253 | arelent **storage, | |
7254 | asymbol **syms) | |
252b5132 | 7255 | { |
217aa764 | 7256 | bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean); |
252b5132 RH |
7257 | asection *s; |
7258 | long ret; | |
7259 | ||
7260 | if (elf_dynsymtab (abfd) == 0) | |
7261 | { | |
7262 | bfd_set_error (bfd_error_invalid_operation); | |
7263 | return -1; | |
7264 | } | |
7265 | ||
7266 | slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table; | |
7267 | ret = 0; | |
7268 | for (s = abfd->sections; s != NULL; s = s->next) | |
7269 | { | |
266b05cf | 7270 | if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd) |
252b5132 RH |
7271 | && (elf_section_data (s)->this_hdr.sh_type == SHT_REL |
7272 | || elf_section_data (s)->this_hdr.sh_type == SHT_RELA)) | |
7273 | { | |
7274 | arelent *p; | |
7275 | long count, i; | |
7276 | ||
b34976b6 | 7277 | if (! (*slurp_relocs) (abfd, s, syms, TRUE)) |
252b5132 | 7278 | return -1; |
eea6121a | 7279 | count = s->size / elf_section_data (s)->this_hdr.sh_entsize; |
252b5132 RH |
7280 | p = s->relocation; |
7281 | for (i = 0; i < count; i++) | |
7282 | *storage++ = p++; | |
7283 | ret += count; | |
7284 | } | |
7285 | } | |
7286 | ||
7287 | *storage = NULL; | |
7288 | ||
7289 | return ret; | |
7290 | } | |
7291 | \f | |
7292 | /* Read in the version information. */ | |
7293 | ||
b34976b6 | 7294 | bfd_boolean |
fc0e6df6 | 7295 | _bfd_elf_slurp_version_tables (bfd *abfd, bfd_boolean default_imported_symver) |
252b5132 RH |
7296 | { |
7297 | bfd_byte *contents = NULL; | |
fc0e6df6 PB |
7298 | unsigned int freeidx = 0; |
7299 | ||
7300 | if (elf_dynverref (abfd) != 0) | |
7301 | { | |
7302 | Elf_Internal_Shdr *hdr; | |
7303 | Elf_External_Verneed *everneed; | |
7304 | Elf_Internal_Verneed *iverneed; | |
7305 | unsigned int i; | |
d0fb9a8d | 7306 | bfd_byte *contents_end; |
fc0e6df6 PB |
7307 | |
7308 | hdr = &elf_tdata (abfd)->dynverref_hdr; | |
7309 | ||
601a03ba | 7310 | if (hdr->sh_info == 0 || hdr->sh_size < sizeof (Elf_External_Verneed)) |
d0fb9a8d | 7311 | { |
601a03ba AM |
7312 | error_return_bad_verref: |
7313 | (*_bfd_error_handler) | |
7314 | (_("%B: .gnu.version_r invalid entry"), abfd); | |
7315 | bfd_set_error (bfd_error_bad_value); | |
d0fb9a8d JJ |
7316 | error_return_verref: |
7317 | elf_tdata (abfd)->verref = NULL; | |
7318 | elf_tdata (abfd)->cverrefs = 0; | |
7319 | goto error_return; | |
7320 | } | |
601a03ba AM |
7321 | |
7322 | contents = (bfd_byte *) bfd_malloc (hdr->sh_size); | |
7323 | if (contents == NULL) | |
7324 | goto error_return_verref; | |
7325 | ||
fc0e6df6 PB |
7326 | if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0 |
7327 | || bfd_bread (contents, hdr->sh_size, abfd) != hdr->sh_size) | |
d0fb9a8d | 7328 | goto error_return_verref; |
fc0e6df6 | 7329 | |
601a03ba AM |
7330 | elf_tdata (abfd)->verref = (Elf_Internal_Verneed *) |
7331 | bfd_zalloc2 (abfd, hdr->sh_info, sizeof (Elf_Internal_Verneed)); | |
7332 | ||
7333 | if (elf_tdata (abfd)->verref == NULL) | |
d0fb9a8d JJ |
7334 | goto error_return_verref; |
7335 | ||
7336 | BFD_ASSERT (sizeof (Elf_External_Verneed) | |
7337 | == sizeof (Elf_External_Vernaux)); | |
7338 | contents_end = contents + hdr->sh_size - sizeof (Elf_External_Verneed); | |
fc0e6df6 PB |
7339 | everneed = (Elf_External_Verneed *) contents; |
7340 | iverneed = elf_tdata (abfd)->verref; | |
7341 | for (i = 0; i < hdr->sh_info; i++, iverneed++) | |
7342 | { | |
7343 | Elf_External_Vernaux *evernaux; | |
7344 | Elf_Internal_Vernaux *ivernaux; | |
7345 | unsigned int j; | |
7346 | ||
7347 | _bfd_elf_swap_verneed_in (abfd, everneed, iverneed); | |
7348 | ||
7349 | iverneed->vn_bfd = abfd; | |
7350 | ||
7351 | iverneed->vn_filename = | |
7352 | bfd_elf_string_from_elf_section (abfd, hdr->sh_link, | |
7353 | iverneed->vn_file); | |
7354 | if (iverneed->vn_filename == NULL) | |
601a03ba | 7355 | goto error_return_bad_verref; |
fc0e6df6 | 7356 | |
d0fb9a8d JJ |
7357 | if (iverneed->vn_cnt == 0) |
7358 | iverneed->vn_auxptr = NULL; | |
7359 | else | |
7360 | { | |
a50b1753 NC |
7361 | iverneed->vn_auxptr = (struct elf_internal_vernaux *) |
7362 | bfd_alloc2 (abfd, iverneed->vn_cnt, | |
7363 | sizeof (Elf_Internal_Vernaux)); | |
d0fb9a8d JJ |
7364 | if (iverneed->vn_auxptr == NULL) |
7365 | goto error_return_verref; | |
7366 | } | |
7367 | ||
7368 | if (iverneed->vn_aux | |
7369 | > (size_t) (contents_end - (bfd_byte *) everneed)) | |
601a03ba | 7370 | goto error_return_bad_verref; |
fc0e6df6 PB |
7371 | |
7372 | evernaux = ((Elf_External_Vernaux *) | |
7373 | ((bfd_byte *) everneed + iverneed->vn_aux)); | |
7374 | ivernaux = iverneed->vn_auxptr; | |
7375 | for (j = 0; j < iverneed->vn_cnt; j++, ivernaux++) | |
7376 | { | |
7377 | _bfd_elf_swap_vernaux_in (abfd, evernaux, ivernaux); | |
7378 | ||
7379 | ivernaux->vna_nodename = | |
7380 | bfd_elf_string_from_elf_section (abfd, hdr->sh_link, | |
7381 | ivernaux->vna_name); | |
7382 | if (ivernaux->vna_nodename == NULL) | |
601a03ba | 7383 | goto error_return_bad_verref; |
fc0e6df6 | 7384 | |
25ff461f AM |
7385 | if (ivernaux->vna_other > freeidx) |
7386 | freeidx = ivernaux->vna_other; | |
7387 | ||
7388 | ivernaux->vna_nextptr = NULL; | |
7389 | if (ivernaux->vna_next == 0) | |
7390 | { | |
7391 | iverneed->vn_cnt = j + 1; | |
7392 | break; | |
7393 | } | |
fc0e6df6 PB |
7394 | if (j + 1 < iverneed->vn_cnt) |
7395 | ivernaux->vna_nextptr = ivernaux + 1; | |
fc0e6df6 | 7396 | |
d0fb9a8d JJ |
7397 | if (ivernaux->vna_next |
7398 | > (size_t) (contents_end - (bfd_byte *) evernaux)) | |
601a03ba | 7399 | goto error_return_bad_verref; |
d0fb9a8d | 7400 | |
fc0e6df6 PB |
7401 | evernaux = ((Elf_External_Vernaux *) |
7402 | ((bfd_byte *) evernaux + ivernaux->vna_next)); | |
fc0e6df6 PB |
7403 | } |
7404 | ||
25ff461f AM |
7405 | iverneed->vn_nextref = NULL; |
7406 | if (iverneed->vn_next == 0) | |
7407 | break; | |
fc0e6df6 PB |
7408 | if (i + 1 < hdr->sh_info) |
7409 | iverneed->vn_nextref = iverneed + 1; | |
fc0e6df6 | 7410 | |
d0fb9a8d JJ |
7411 | if (iverneed->vn_next |
7412 | > (size_t) (contents_end - (bfd_byte *) everneed)) | |
601a03ba | 7413 | goto error_return_bad_verref; |
d0fb9a8d | 7414 | |
fc0e6df6 PB |
7415 | everneed = ((Elf_External_Verneed *) |
7416 | ((bfd_byte *) everneed + iverneed->vn_next)); | |
7417 | } | |
25ff461f | 7418 | elf_tdata (abfd)->cverrefs = i; |
fc0e6df6 PB |
7419 | |
7420 | free (contents); | |
7421 | contents = NULL; | |
7422 | } | |
252b5132 RH |
7423 | |
7424 | if (elf_dynverdef (abfd) != 0) | |
7425 | { | |
7426 | Elf_Internal_Shdr *hdr; | |
7427 | Elf_External_Verdef *everdef; | |
7428 | Elf_Internal_Verdef *iverdef; | |
f631889e UD |
7429 | Elf_Internal_Verdef *iverdefarr; |
7430 | Elf_Internal_Verdef iverdefmem; | |
252b5132 | 7431 | unsigned int i; |
062e2358 | 7432 | unsigned int maxidx; |
d0fb9a8d | 7433 | bfd_byte *contents_end_def, *contents_end_aux; |
252b5132 RH |
7434 | |
7435 | hdr = &elf_tdata (abfd)->dynverdef_hdr; | |
7436 | ||
601a03ba AM |
7437 | if (hdr->sh_info == 0 || hdr->sh_size < sizeof (Elf_External_Verdef)) |
7438 | { | |
7439 | error_return_bad_verdef: | |
7440 | (*_bfd_error_handler) | |
7441 | (_("%B: .gnu.version_d invalid entry"), abfd); | |
7442 | bfd_set_error (bfd_error_bad_value); | |
7443 | error_return_verdef: | |
7444 | elf_tdata (abfd)->verdef = NULL; | |
7445 | elf_tdata (abfd)->cverdefs = 0; | |
7446 | goto error_return; | |
7447 | } | |
7448 | ||
a50b1753 | 7449 | contents = (bfd_byte *) bfd_malloc (hdr->sh_size); |
252b5132 | 7450 | if (contents == NULL) |
601a03ba | 7451 | goto error_return_verdef; |
252b5132 | 7452 | if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0 |
217aa764 | 7453 | || bfd_bread (contents, hdr->sh_size, abfd) != hdr->sh_size) |
601a03ba | 7454 | goto error_return_verdef; |
d0fb9a8d JJ |
7455 | |
7456 | BFD_ASSERT (sizeof (Elf_External_Verdef) | |
7457 | >= sizeof (Elf_External_Verdaux)); | |
7458 | contents_end_def = contents + hdr->sh_size | |
7459 | - sizeof (Elf_External_Verdef); | |
7460 | contents_end_aux = contents + hdr->sh_size | |
7461 | - sizeof (Elf_External_Verdaux); | |
7462 | ||
f631889e UD |
7463 | /* We know the number of entries in the section but not the maximum |
7464 | index. Therefore we have to run through all entries and find | |
7465 | the maximum. */ | |
252b5132 | 7466 | everdef = (Elf_External_Verdef *) contents; |
f631889e UD |
7467 | maxidx = 0; |
7468 | for (i = 0; i < hdr->sh_info; ++i) | |
7469 | { | |
7470 | _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem); | |
7471 | ||
601a03ba AM |
7472 | if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) == 0) |
7473 | goto error_return_bad_verdef; | |
062e2358 AM |
7474 | if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) > maxidx) |
7475 | maxidx = iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION); | |
f631889e | 7476 | |
25ff461f AM |
7477 | if (iverdefmem.vd_next == 0) |
7478 | break; | |
7479 | ||
d0fb9a8d JJ |
7480 | if (iverdefmem.vd_next |
7481 | > (size_t) (contents_end_def - (bfd_byte *) everdef)) | |
601a03ba | 7482 | goto error_return_bad_verdef; |
d0fb9a8d | 7483 | |
f631889e UD |
7484 | everdef = ((Elf_External_Verdef *) |
7485 | ((bfd_byte *) everdef + iverdefmem.vd_next)); | |
7486 | } | |
7487 | ||
fc0e6df6 PB |
7488 | if (default_imported_symver) |
7489 | { | |
7490 | if (freeidx > maxidx) | |
7491 | maxidx = ++freeidx; | |
7492 | else | |
7493 | freeidx = ++maxidx; | |
7494 | } | |
201159ec | 7495 | |
601a03ba AM |
7496 | elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *) |
7497 | bfd_zalloc2 (abfd, maxidx, sizeof (Elf_Internal_Verdef)); | |
f631889e | 7498 | if (elf_tdata (abfd)->verdef == NULL) |
601a03ba | 7499 | goto error_return_verdef; |
f631889e UD |
7500 | |
7501 | elf_tdata (abfd)->cverdefs = maxidx; | |
7502 | ||
7503 | everdef = (Elf_External_Verdef *) contents; | |
7504 | iverdefarr = elf_tdata (abfd)->verdef; | |
7505 | for (i = 0; i < hdr->sh_info; i++) | |
252b5132 RH |
7506 | { |
7507 | Elf_External_Verdaux *everdaux; | |
7508 | Elf_Internal_Verdaux *iverdaux; | |
7509 | unsigned int j; | |
7510 | ||
f631889e UD |
7511 | _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem); |
7512 | ||
d0fb9a8d | 7513 | if ((iverdefmem.vd_ndx & VERSYM_VERSION) == 0) |
601a03ba | 7514 | goto error_return_bad_verdef; |
d0fb9a8d | 7515 | |
f631889e UD |
7516 | iverdef = &iverdefarr[(iverdefmem.vd_ndx & VERSYM_VERSION) - 1]; |
7517 | memcpy (iverdef, &iverdefmem, sizeof (Elf_Internal_Verdef)); | |
252b5132 RH |
7518 | |
7519 | iverdef->vd_bfd = abfd; | |
7520 | ||
d0fb9a8d JJ |
7521 | if (iverdef->vd_cnt == 0) |
7522 | iverdef->vd_auxptr = NULL; | |
7523 | else | |
7524 | { | |
a50b1753 NC |
7525 | iverdef->vd_auxptr = (struct elf_internal_verdaux *) |
7526 | bfd_alloc2 (abfd, iverdef->vd_cnt, | |
7527 | sizeof (Elf_Internal_Verdaux)); | |
d0fb9a8d JJ |
7528 | if (iverdef->vd_auxptr == NULL) |
7529 | goto error_return_verdef; | |
7530 | } | |
7531 | ||
7532 | if (iverdef->vd_aux | |
7533 | > (size_t) (contents_end_aux - (bfd_byte *) everdef)) | |
601a03ba | 7534 | goto error_return_bad_verdef; |
252b5132 RH |
7535 | |
7536 | everdaux = ((Elf_External_Verdaux *) | |
7537 | ((bfd_byte *) everdef + iverdef->vd_aux)); | |
7538 | iverdaux = iverdef->vd_auxptr; | |
7539 | for (j = 0; j < iverdef->vd_cnt; j++, iverdaux++) | |
7540 | { | |
7541 | _bfd_elf_swap_verdaux_in (abfd, everdaux, iverdaux); | |
7542 | ||
7543 | iverdaux->vda_nodename = | |
7544 | bfd_elf_string_from_elf_section (abfd, hdr->sh_link, | |
7545 | iverdaux->vda_name); | |
7546 | if (iverdaux->vda_nodename == NULL) | |
601a03ba | 7547 | goto error_return_bad_verdef; |
252b5132 | 7548 | |
25ff461f AM |
7549 | iverdaux->vda_nextptr = NULL; |
7550 | if (iverdaux->vda_next == 0) | |
7551 | { | |
7552 | iverdef->vd_cnt = j + 1; | |
7553 | break; | |
7554 | } | |
252b5132 RH |
7555 | if (j + 1 < iverdef->vd_cnt) |
7556 | iverdaux->vda_nextptr = iverdaux + 1; | |
252b5132 | 7557 | |
d0fb9a8d JJ |
7558 | if (iverdaux->vda_next |
7559 | > (size_t) (contents_end_aux - (bfd_byte *) everdaux)) | |
601a03ba | 7560 | goto error_return_bad_verdef; |
d0fb9a8d | 7561 | |
252b5132 RH |
7562 | everdaux = ((Elf_External_Verdaux *) |
7563 | ((bfd_byte *) everdaux + iverdaux->vda_next)); | |
7564 | } | |
7565 | ||
d0fb9a8d JJ |
7566 | if (iverdef->vd_cnt) |
7567 | iverdef->vd_nodename = iverdef->vd_auxptr->vda_nodename; | |
252b5132 | 7568 | |
25ff461f AM |
7569 | iverdef->vd_nextdef = NULL; |
7570 | if (iverdef->vd_next == 0) | |
7571 | break; | |
d0fb9a8d | 7572 | if ((size_t) (iverdef - iverdefarr) + 1 < maxidx) |
252b5132 | 7573 | iverdef->vd_nextdef = iverdef + 1; |
252b5132 RH |
7574 | |
7575 | everdef = ((Elf_External_Verdef *) | |
7576 | ((bfd_byte *) everdef + iverdef->vd_next)); | |
7577 | } | |
7578 | ||
7579 | free (contents); | |
7580 | contents = NULL; | |
7581 | } | |
fc0e6df6 | 7582 | else if (default_imported_symver) |
252b5132 | 7583 | { |
fc0e6df6 PB |
7584 | if (freeidx < 3) |
7585 | freeidx = 3; | |
7586 | else | |
7587 | freeidx++; | |
252b5132 | 7588 | |
a50b1753 NC |
7589 | elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *) |
7590 | bfd_zalloc2 (abfd, freeidx, sizeof (Elf_Internal_Verdef)); | |
fc0e6df6 | 7591 | if (elf_tdata (abfd)->verdef == NULL) |
252b5132 RH |
7592 | goto error_return; |
7593 | ||
fc0e6df6 PB |
7594 | elf_tdata (abfd)->cverdefs = freeidx; |
7595 | } | |
252b5132 | 7596 | |
fc0e6df6 PB |
7597 | /* Create a default version based on the soname. */ |
7598 | if (default_imported_symver) | |
7599 | { | |
7600 | Elf_Internal_Verdef *iverdef; | |
7601 | Elf_Internal_Verdaux *iverdaux; | |
252b5132 | 7602 | |
5bb3703f | 7603 | iverdef = &elf_tdata (abfd)->verdef[freeidx - 1]; |
252b5132 | 7604 | |
fc0e6df6 PB |
7605 | iverdef->vd_version = VER_DEF_CURRENT; |
7606 | iverdef->vd_flags = 0; | |
7607 | iverdef->vd_ndx = freeidx; | |
7608 | iverdef->vd_cnt = 1; | |
252b5132 | 7609 | |
fc0e6df6 | 7610 | iverdef->vd_bfd = abfd; |
252b5132 | 7611 | |
fc0e6df6 PB |
7612 | iverdef->vd_nodename = bfd_elf_get_dt_soname (abfd); |
7613 | if (iverdef->vd_nodename == NULL) | |
d0fb9a8d | 7614 | goto error_return_verdef; |
fc0e6df6 | 7615 | iverdef->vd_nextdef = NULL; |
601a03ba AM |
7616 | iverdef->vd_auxptr = ((struct elf_internal_verdaux *) |
7617 | bfd_zalloc (abfd, sizeof (Elf_Internal_Verdaux))); | |
d0fb9a8d JJ |
7618 | if (iverdef->vd_auxptr == NULL) |
7619 | goto error_return_verdef; | |
252b5132 | 7620 | |
fc0e6df6 PB |
7621 | iverdaux = iverdef->vd_auxptr; |
7622 | iverdaux->vda_nodename = iverdef->vd_nodename; | |
252b5132 RH |
7623 | } |
7624 | ||
b34976b6 | 7625 | return TRUE; |
252b5132 RH |
7626 | |
7627 | error_return: | |
5ed6aba4 | 7628 | if (contents != NULL) |
252b5132 | 7629 | free (contents); |
b34976b6 | 7630 | return FALSE; |
252b5132 RH |
7631 | } |
7632 | \f | |
7633 | asymbol * | |
217aa764 | 7634 | _bfd_elf_make_empty_symbol (bfd *abfd) |
252b5132 RH |
7635 | { |
7636 | elf_symbol_type *newsym; | |
7637 | ||
201159ec | 7638 | newsym = (elf_symbol_type *) bfd_zalloc (abfd, sizeof * newsym); |
252b5132 RH |
7639 | if (!newsym) |
7640 | return NULL; | |
201159ec NC |
7641 | newsym->symbol.the_bfd = abfd; |
7642 | return &newsym->symbol; | |
252b5132 RH |
7643 | } |
7644 | ||
7645 | void | |
217aa764 AM |
7646 | _bfd_elf_get_symbol_info (bfd *abfd ATTRIBUTE_UNUSED, |
7647 | asymbol *symbol, | |
7648 | symbol_info *ret) | |
252b5132 RH |
7649 | { |
7650 | bfd_symbol_info (symbol, ret); | |
7651 | } | |
7652 | ||
7653 | /* Return whether a symbol name implies a local symbol. Most targets | |
7654 | use this function for the is_local_label_name entry point, but some | |
7655 | override it. */ | |
7656 | ||
b34976b6 | 7657 | bfd_boolean |
217aa764 AM |
7658 | _bfd_elf_is_local_label_name (bfd *abfd ATTRIBUTE_UNUSED, |
7659 | const char *name) | |
252b5132 RH |
7660 | { |
7661 | /* Normal local symbols start with ``.L''. */ | |
7662 | if (name[0] == '.' && name[1] == 'L') | |
b34976b6 | 7663 | return TRUE; |
252b5132 RH |
7664 | |
7665 | /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate | |
7666 | DWARF debugging symbols starting with ``..''. */ | |
7667 | if (name[0] == '.' && name[1] == '.') | |
b34976b6 | 7668 | return TRUE; |
252b5132 RH |
7669 | |
7670 | /* gcc will sometimes generate symbols beginning with ``_.L_'' when | |
7671 | emitting DWARF debugging output. I suspect this is actually a | |
7672 | small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call | |
7673 | ASM_GENERATE_INTERNAL_LABEL, and this causes the leading | |
7674 | underscore to be emitted on some ELF targets). For ease of use, | |
7675 | we treat such symbols as local. */ | |
7676 | if (name[0] == '_' && name[1] == '.' && name[2] == 'L' && name[3] == '_') | |
b34976b6 | 7677 | return TRUE; |
252b5132 | 7678 | |
b34976b6 | 7679 | return FALSE; |
252b5132 RH |
7680 | } |
7681 | ||
7682 | alent * | |
217aa764 AM |
7683 | _bfd_elf_get_lineno (bfd *abfd ATTRIBUTE_UNUSED, |
7684 | asymbol *symbol ATTRIBUTE_UNUSED) | |
252b5132 RH |
7685 | { |
7686 | abort (); | |
7687 | return NULL; | |
7688 | } | |
7689 | ||
b34976b6 | 7690 | bfd_boolean |
217aa764 AM |
7691 | _bfd_elf_set_arch_mach (bfd *abfd, |
7692 | enum bfd_architecture arch, | |
7693 | unsigned long machine) | |
252b5132 RH |
7694 | { |
7695 | /* If this isn't the right architecture for this backend, and this | |
7696 | isn't the generic backend, fail. */ | |
7697 | if (arch != get_elf_backend_data (abfd)->arch | |
7698 | && arch != bfd_arch_unknown | |
7699 | && get_elf_backend_data (abfd)->arch != bfd_arch_unknown) | |
b34976b6 | 7700 | return FALSE; |
252b5132 RH |
7701 | |
7702 | return bfd_default_set_arch_mach (abfd, arch, machine); | |
7703 | } | |
7704 | ||
d1fad7c6 NC |
7705 | /* Find the function to a particular section and offset, |
7706 | for error reporting. */ | |
252b5132 | 7707 | |
b34976b6 | 7708 | static bfd_boolean |
b9d678e0 | 7709 | elf_find_function (bfd *abfd, |
217aa764 | 7710 | asymbol **symbols, |
fb167eb2 | 7711 | asection *section, |
217aa764 AM |
7712 | bfd_vma offset, |
7713 | const char **filename_ptr, | |
7714 | const char **functionname_ptr) | |
252b5132 | 7715 | { |
619a703e AM |
7716 | struct elf_find_function_cache |
7717 | { | |
7718 | asection *last_section; | |
7719 | asymbol *func; | |
7720 | const char *filename; | |
7721 | bfd_size_type func_size; | |
7722 | } *cache; | |
252b5132 | 7723 | |
a06c7d5a NC |
7724 | if (symbols == NULL) |
7725 | return FALSE; | |
7726 | ||
619a703e AM |
7727 | cache = elf_tdata (abfd)->elf_find_function_cache; |
7728 | if (cache == NULL) | |
7729 | { | |
7730 | cache = bfd_zalloc (abfd, sizeof (*cache)); | |
7731 | elf_tdata (abfd)->elf_find_function_cache = cache; | |
7732 | if (cache == NULL) | |
7733 | return FALSE; | |
7734 | } | |
7735 | if (cache->last_section != section | |
7736 | || cache->func == NULL | |
7737 | || offset < cache->func->value | |
7738 | || offset >= cache->func->value + cache->func_size) | |
aef36ac1 AM |
7739 | { |
7740 | asymbol *file; | |
7741 | bfd_vma low_func; | |
7742 | asymbol **p; | |
7743 | /* ??? Given multiple file symbols, it is impossible to reliably | |
7744 | choose the right file name for global symbols. File symbols are | |
7745 | local symbols, and thus all file symbols must sort before any | |
7746 | global symbols. The ELF spec may be interpreted to say that a | |
7747 | file symbol must sort before other local symbols, but currently | |
7748 | ld -r doesn't do this. So, for ld -r output, it is possible to | |
7749 | make a better choice of file name for local symbols by ignoring | |
7750 | file symbols appearing after a given local symbol. */ | |
7751 | enum { nothing_seen, symbol_seen, file_after_symbol_seen } state; | |
7752 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
7753 | ||
aef36ac1 AM |
7754 | file = NULL; |
7755 | low_func = 0; | |
7756 | state = nothing_seen; | |
619a703e AM |
7757 | cache->filename = NULL; |
7758 | cache->func = NULL; | |
7759 | cache->func_size = 0; | |
7760 | cache->last_section = section; | |
aef36ac1 AM |
7761 | |
7762 | for (p = symbols; *p != NULL; p++) | |
7763 | { | |
7764 | asymbol *sym = *p; | |
7765 | bfd_vma code_off; | |
7766 | bfd_size_type size; | |
7767 | ||
7768 | if ((sym->flags & BSF_FILE) != 0) | |
7769 | { | |
7770 | file = sym; | |
7771 | if (state == symbol_seen) | |
7772 | state = file_after_symbol_seen; | |
7773 | continue; | |
7774 | } | |
ff9e0f5b | 7775 | |
aef36ac1 AM |
7776 | size = bed->maybe_function_sym (sym, section, &code_off); |
7777 | if (size != 0 | |
7778 | && code_off <= offset | |
7779 | && (code_off > low_func | |
7780 | || (code_off == low_func | |
619a703e | 7781 | && size > cache->func_size))) |
aef36ac1 | 7782 | { |
619a703e AM |
7783 | cache->func = sym; |
7784 | cache->func_size = size; | |
7785 | cache->filename = NULL; | |
aef36ac1 | 7786 | low_func = code_off; |
aef36ac1 AM |
7787 | if (file != NULL |
7788 | && ((sym->flags & BSF_LOCAL) != 0 | |
7789 | || state != file_after_symbol_seen)) | |
619a703e | 7790 | cache->filename = bfd_asymbol_name (file); |
aef36ac1 AM |
7791 | } |
7792 | if (state == nothing_seen) | |
7793 | state = symbol_seen; | |
252b5132 RH |
7794 | } |
7795 | } | |
7796 | ||
619a703e | 7797 | if (cache->func == NULL) |
b34976b6 | 7798 | return FALSE; |
252b5132 | 7799 | |
d1fad7c6 | 7800 | if (filename_ptr) |
619a703e | 7801 | *filename_ptr = cache->filename; |
d1fad7c6 | 7802 | if (functionname_ptr) |
619a703e | 7803 | *functionname_ptr = bfd_asymbol_name (cache->func); |
d1fad7c6 | 7804 | |
b34976b6 | 7805 | return TRUE; |
d1fad7c6 NC |
7806 | } |
7807 | ||
7808 | /* Find the nearest line to a particular section and offset, | |
7809 | for error reporting. */ | |
7810 | ||
b34976b6 | 7811 | bfd_boolean |
217aa764 | 7812 | _bfd_elf_find_nearest_line (bfd *abfd, |
217aa764 | 7813 | asymbol **symbols, |
fb167eb2 | 7814 | asection *section, |
217aa764 AM |
7815 | bfd_vma offset, |
7816 | const char **filename_ptr, | |
7817 | const char **functionname_ptr, | |
fb167eb2 AM |
7818 | unsigned int *line_ptr, |
7819 | unsigned int *discriminator_ptr) | |
d1fad7c6 | 7820 | { |
b34976b6 | 7821 | bfd_boolean found; |
d1fad7c6 | 7822 | |
fb167eb2 | 7823 | if (_bfd_dwarf2_find_nearest_line (abfd, symbols, NULL, section, offset, |
4e8a9624 | 7824 | filename_ptr, functionname_ptr, |
fb167eb2 AM |
7825 | line_ptr, discriminator_ptr, |
7826 | dwarf_debug_sections, 0, | |
7827 | &elf_tdata (abfd)->dwarf2_find_line_info)) | |
d1fad7c6 NC |
7828 | { |
7829 | if (!*functionname_ptr) | |
fb167eb2 | 7830 | elf_find_function (abfd, symbols, section, offset, |
4e8a9624 AM |
7831 | *filename_ptr ? NULL : filename_ptr, |
7832 | functionname_ptr); | |
7833 | ||
b34976b6 | 7834 | return TRUE; |
d1fad7c6 NC |
7835 | } |
7836 | ||
fb167eb2 AM |
7837 | if (_bfd_dwarf1_find_nearest_line (abfd, symbols, section, offset, |
7838 | filename_ptr, functionname_ptr, line_ptr)) | |
d1fad7c6 NC |
7839 | { |
7840 | if (!*functionname_ptr) | |
fb167eb2 | 7841 | elf_find_function (abfd, symbols, section, offset, |
4e8a9624 AM |
7842 | *filename_ptr ? NULL : filename_ptr, |
7843 | functionname_ptr); | |
7844 | ||
b34976b6 | 7845 | return TRUE; |
d1fad7c6 NC |
7846 | } |
7847 | ||
7848 | if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset, | |
4e8a9624 AM |
7849 | &found, filename_ptr, |
7850 | functionname_ptr, line_ptr, | |
7851 | &elf_tdata (abfd)->line_info)) | |
b34976b6 | 7852 | return FALSE; |
dc43ada5 | 7853 | if (found && (*functionname_ptr || *line_ptr)) |
b34976b6 | 7854 | return TRUE; |
d1fad7c6 NC |
7855 | |
7856 | if (symbols == NULL) | |
b34976b6 | 7857 | return FALSE; |
d1fad7c6 | 7858 | |
fb167eb2 | 7859 | if (! elf_find_function (abfd, symbols, section, offset, |
4e8a9624 | 7860 | filename_ptr, functionname_ptr)) |
b34976b6 | 7861 | return FALSE; |
d1fad7c6 | 7862 | |
252b5132 | 7863 | *line_ptr = 0; |
b34976b6 | 7864 | return TRUE; |
252b5132 RH |
7865 | } |
7866 | ||
5420f73d L |
7867 | /* Find the line for a symbol. */ |
7868 | ||
7869 | bfd_boolean | |
7870 | _bfd_elf_find_line (bfd *abfd, asymbol **symbols, asymbol *symbol, | |
7871 | const char **filename_ptr, unsigned int *line_ptr) | |
9b8d1a36 | 7872 | { |
fb167eb2 AM |
7873 | return _bfd_dwarf2_find_nearest_line (abfd, symbols, symbol, NULL, 0, |
7874 | filename_ptr, NULL, line_ptr, NULL, | |
7875 | dwarf_debug_sections, 0, | |
7876 | &elf_tdata (abfd)->dwarf2_find_line_info); | |
5420f73d L |
7877 | } |
7878 | ||
4ab527b0 FF |
7879 | /* After a call to bfd_find_nearest_line, successive calls to |
7880 | bfd_find_inliner_info can be used to get source information about | |
7881 | each level of function inlining that terminated at the address | |
7882 | passed to bfd_find_nearest_line. Currently this is only supported | |
7883 | for DWARF2 with appropriate DWARF3 extensions. */ | |
7884 | ||
7885 | bfd_boolean | |
7886 | _bfd_elf_find_inliner_info (bfd *abfd, | |
7887 | const char **filename_ptr, | |
7888 | const char **functionname_ptr, | |
7889 | unsigned int *line_ptr) | |
7890 | { | |
7891 | bfd_boolean found; | |
7892 | found = _bfd_dwarf2_find_inliner_info (abfd, filename_ptr, | |
7893 | functionname_ptr, line_ptr, | |
7894 | & elf_tdata (abfd)->dwarf2_find_line_info); | |
7895 | return found; | |
7896 | } | |
7897 | ||
252b5132 | 7898 | int |
a6b96beb | 7899 | _bfd_elf_sizeof_headers (bfd *abfd, struct bfd_link_info *info) |
252b5132 | 7900 | { |
8ded5a0f AM |
7901 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
7902 | int ret = bed->s->sizeof_ehdr; | |
252b5132 | 7903 | |
a6b96beb | 7904 | if (!info->relocatable) |
8ded5a0f | 7905 | { |
12bd6957 | 7906 | bfd_size_type phdr_size = elf_program_header_size (abfd); |
8ded5a0f | 7907 | |
62d7a5f6 AM |
7908 | if (phdr_size == (bfd_size_type) -1) |
7909 | { | |
7910 | struct elf_segment_map *m; | |
7911 | ||
7912 | phdr_size = 0; | |
12bd6957 | 7913 | for (m = elf_seg_map (abfd); m != NULL; m = m->next) |
62d7a5f6 | 7914 | phdr_size += bed->s->sizeof_phdr; |
8ded5a0f | 7915 | |
62d7a5f6 AM |
7916 | if (phdr_size == 0) |
7917 | phdr_size = get_program_header_size (abfd, info); | |
7918 | } | |
8ded5a0f | 7919 | |
12bd6957 | 7920 | elf_program_header_size (abfd) = phdr_size; |
8ded5a0f AM |
7921 | ret += phdr_size; |
7922 | } | |
7923 | ||
252b5132 RH |
7924 | return ret; |
7925 | } | |
7926 | ||
b34976b6 | 7927 | bfd_boolean |
217aa764 AM |
7928 | _bfd_elf_set_section_contents (bfd *abfd, |
7929 | sec_ptr section, | |
0f867abe | 7930 | const void *location, |
217aa764 AM |
7931 | file_ptr offset, |
7932 | bfd_size_type count) | |
252b5132 RH |
7933 | { |
7934 | Elf_Internal_Shdr *hdr; | |
1b6aeedb | 7935 | file_ptr pos; |
252b5132 RH |
7936 | |
7937 | if (! abfd->output_has_begun | |
217aa764 | 7938 | && ! _bfd_elf_compute_section_file_positions (abfd, NULL)) |
b34976b6 | 7939 | return FALSE; |
252b5132 RH |
7940 | |
7941 | hdr = &elf_section_data (section)->this_hdr; | |
dc810e39 AM |
7942 | pos = hdr->sh_offset + offset; |
7943 | if (bfd_seek (abfd, pos, SEEK_SET) != 0 | |
7944 | || bfd_bwrite (location, count, abfd) != count) | |
b34976b6 | 7945 | return FALSE; |
252b5132 | 7946 | |
b34976b6 | 7947 | return TRUE; |
252b5132 RH |
7948 | } |
7949 | ||
7950 | void | |
217aa764 AM |
7951 | _bfd_elf_no_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED, |
7952 | arelent *cache_ptr ATTRIBUTE_UNUSED, | |
7953 | Elf_Internal_Rela *dst ATTRIBUTE_UNUSED) | |
252b5132 RH |
7954 | { |
7955 | abort (); | |
7956 | } | |
7957 | ||
252b5132 RH |
7958 | /* Try to convert a non-ELF reloc into an ELF one. */ |
7959 | ||
b34976b6 | 7960 | bfd_boolean |
217aa764 | 7961 | _bfd_elf_validate_reloc (bfd *abfd, arelent *areloc) |
252b5132 | 7962 | { |
c044fabd | 7963 | /* Check whether we really have an ELF howto. */ |
252b5132 RH |
7964 | |
7965 | if ((*areloc->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec) | |
7966 | { | |
7967 | bfd_reloc_code_real_type code; | |
7968 | reloc_howto_type *howto; | |
7969 | ||
7970 | /* Alien reloc: Try to determine its type to replace it with an | |
c044fabd | 7971 | equivalent ELF reloc. */ |
252b5132 RH |
7972 | |
7973 | if (areloc->howto->pc_relative) | |
7974 | { | |
7975 | switch (areloc->howto->bitsize) | |
7976 | { | |
7977 | case 8: | |
7978 | code = BFD_RELOC_8_PCREL; | |
7979 | break; | |
7980 | case 12: | |
7981 | code = BFD_RELOC_12_PCREL; | |
7982 | break; | |
7983 | case 16: | |
7984 | code = BFD_RELOC_16_PCREL; | |
7985 | break; | |
7986 | case 24: | |
7987 | code = BFD_RELOC_24_PCREL; | |
7988 | break; | |
7989 | case 32: | |
7990 | code = BFD_RELOC_32_PCREL; | |
7991 | break; | |
7992 | case 64: | |
7993 | code = BFD_RELOC_64_PCREL; | |
7994 | break; | |
7995 | default: | |
7996 | goto fail; | |
7997 | } | |
7998 | ||
7999 | howto = bfd_reloc_type_lookup (abfd, code); | |
8000 | ||
8001 | if (areloc->howto->pcrel_offset != howto->pcrel_offset) | |
8002 | { | |
8003 | if (howto->pcrel_offset) | |
8004 | areloc->addend += areloc->address; | |
8005 | else | |
8006 | areloc->addend -= areloc->address; /* addend is unsigned!! */ | |
8007 | } | |
8008 | } | |
8009 | else | |
8010 | { | |
8011 | switch (areloc->howto->bitsize) | |
8012 | { | |
8013 | case 8: | |
8014 | code = BFD_RELOC_8; | |
8015 | break; | |
8016 | case 14: | |
8017 | code = BFD_RELOC_14; | |
8018 | break; | |
8019 | case 16: | |
8020 | code = BFD_RELOC_16; | |
8021 | break; | |
8022 | case 26: | |
8023 | code = BFD_RELOC_26; | |
8024 | break; | |
8025 | case 32: | |
8026 | code = BFD_RELOC_32; | |
8027 | break; | |
8028 | case 64: | |
8029 | code = BFD_RELOC_64; | |
8030 | break; | |
8031 | default: | |
8032 | goto fail; | |
8033 | } | |
8034 | ||
8035 | howto = bfd_reloc_type_lookup (abfd, code); | |
8036 | } | |
8037 | ||
8038 | if (howto) | |
8039 | areloc->howto = howto; | |
8040 | else | |
8041 | goto fail; | |
8042 | } | |
8043 | ||
b34976b6 | 8044 | return TRUE; |
252b5132 RH |
8045 | |
8046 | fail: | |
8047 | (*_bfd_error_handler) | |
d003868e AM |
8048 | (_("%B: unsupported relocation type %s"), |
8049 | abfd, areloc->howto->name); | |
252b5132 | 8050 | bfd_set_error (bfd_error_bad_value); |
b34976b6 | 8051 | return FALSE; |
252b5132 RH |
8052 | } |
8053 | ||
b34976b6 | 8054 | bfd_boolean |
217aa764 | 8055 | _bfd_elf_close_and_cleanup (bfd *abfd) |
252b5132 | 8056 | { |
d9071b0c TG |
8057 | struct elf_obj_tdata *tdata = elf_tdata (abfd); |
8058 | if (bfd_get_format (abfd) == bfd_object && tdata != NULL) | |
252b5132 | 8059 | { |
c0355132 | 8060 | if (elf_tdata (abfd)->o != NULL && elf_shstrtab (abfd) != NULL) |
2b0f7ef9 | 8061 | _bfd_elf_strtab_free (elf_shstrtab (abfd)); |
d9071b0c | 8062 | _bfd_dwarf2_cleanup_debug_info (abfd, &tdata->dwarf2_find_line_info); |
252b5132 RH |
8063 | } |
8064 | ||
8065 | return _bfd_generic_close_and_cleanup (abfd); | |
8066 | } | |
8067 | ||
8068 | /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY | |
8069 | in the relocation's offset. Thus we cannot allow any sort of sanity | |
8070 | range-checking to interfere. There is nothing else to do in processing | |
8071 | this reloc. */ | |
8072 | ||
8073 | bfd_reloc_status_type | |
217aa764 AM |
8074 | _bfd_elf_rel_vtable_reloc_fn |
8075 | (bfd *abfd ATTRIBUTE_UNUSED, arelent *re ATTRIBUTE_UNUSED, | |
fc0a2244 | 8076 | struct bfd_symbol *symbol ATTRIBUTE_UNUSED, |
217aa764 AM |
8077 | void *data ATTRIBUTE_UNUSED, asection *is ATTRIBUTE_UNUSED, |
8078 | bfd *obfd ATTRIBUTE_UNUSED, char **errmsg ATTRIBUTE_UNUSED) | |
252b5132 RH |
8079 | { |
8080 | return bfd_reloc_ok; | |
8081 | } | |
252b5132 RH |
8082 | \f |
8083 | /* Elf core file support. Much of this only works on native | |
8084 | toolchains, since we rely on knowing the | |
8085 | machine-dependent procfs structure in order to pick | |
c044fabd | 8086 | out details about the corefile. */ |
252b5132 RH |
8087 | |
8088 | #ifdef HAVE_SYS_PROCFS_H | |
16231b7b DG |
8089 | /* Needed for new procfs interface on sparc-solaris. */ |
8090 | # define _STRUCTURED_PROC 1 | |
252b5132 RH |
8091 | # include <sys/procfs.h> |
8092 | #endif | |
8093 | ||
261b8d08 PA |
8094 | /* Return a PID that identifies a "thread" for threaded cores, or the |
8095 | PID of the main process for non-threaded cores. */ | |
252b5132 RH |
8096 | |
8097 | static int | |
217aa764 | 8098 | elfcore_make_pid (bfd *abfd) |
252b5132 | 8099 | { |
261b8d08 PA |
8100 | int pid; |
8101 | ||
228e534f | 8102 | pid = elf_tdata (abfd)->core->lwpid; |
261b8d08 | 8103 | if (pid == 0) |
228e534f | 8104 | pid = elf_tdata (abfd)->core->pid; |
261b8d08 PA |
8105 | |
8106 | return pid; | |
252b5132 RH |
8107 | } |
8108 | ||
252b5132 RH |
8109 | /* If there isn't a section called NAME, make one, using |
8110 | data from SECT. Note, this function will generate a | |
8111 | reference to NAME, so you shouldn't deallocate or | |
c044fabd | 8112 | overwrite it. */ |
252b5132 | 8113 | |
b34976b6 | 8114 | static bfd_boolean |
217aa764 | 8115 | elfcore_maybe_make_sect (bfd *abfd, char *name, asection *sect) |
252b5132 | 8116 | { |
c044fabd | 8117 | asection *sect2; |
252b5132 RH |
8118 | |
8119 | if (bfd_get_section_by_name (abfd, name) != NULL) | |
b34976b6 | 8120 | return TRUE; |
252b5132 | 8121 | |
117ed4f8 | 8122 | sect2 = bfd_make_section_with_flags (abfd, name, sect->flags); |
252b5132 | 8123 | if (sect2 == NULL) |
b34976b6 | 8124 | return FALSE; |
252b5132 | 8125 | |
eea6121a | 8126 | sect2->size = sect->size; |
252b5132 | 8127 | sect2->filepos = sect->filepos; |
252b5132 | 8128 | sect2->alignment_power = sect->alignment_power; |
b34976b6 | 8129 | return TRUE; |
252b5132 RH |
8130 | } |
8131 | ||
bb0082d6 AM |
8132 | /* Create a pseudosection containing SIZE bytes at FILEPOS. This |
8133 | actually creates up to two pseudosections: | |
8134 | - For the single-threaded case, a section named NAME, unless | |
8135 | such a section already exists. | |
8136 | - For the multi-threaded case, a section named "NAME/PID", where | |
8137 | PID is elfcore_make_pid (abfd). | |
8138 | Both pseudosections have identical contents. */ | |
b34976b6 | 8139 | bfd_boolean |
217aa764 AM |
8140 | _bfd_elfcore_make_pseudosection (bfd *abfd, |
8141 | char *name, | |
8142 | size_t size, | |
8143 | ufile_ptr filepos) | |
bb0082d6 AM |
8144 | { |
8145 | char buf[100]; | |
8146 | char *threaded_name; | |
d4c88bbb | 8147 | size_t len; |
bb0082d6 AM |
8148 | asection *sect; |
8149 | ||
8150 | /* Build the section name. */ | |
8151 | ||
8152 | sprintf (buf, "%s/%d", name, elfcore_make_pid (abfd)); | |
d4c88bbb | 8153 | len = strlen (buf) + 1; |
a50b1753 | 8154 | threaded_name = (char *) bfd_alloc (abfd, len); |
bb0082d6 | 8155 | if (threaded_name == NULL) |
b34976b6 | 8156 | return FALSE; |
d4c88bbb | 8157 | memcpy (threaded_name, buf, len); |
bb0082d6 | 8158 | |
117ed4f8 AM |
8159 | sect = bfd_make_section_anyway_with_flags (abfd, threaded_name, |
8160 | SEC_HAS_CONTENTS); | |
bb0082d6 | 8161 | if (sect == NULL) |
b34976b6 | 8162 | return FALSE; |
eea6121a | 8163 | sect->size = size; |
bb0082d6 | 8164 | sect->filepos = filepos; |
bb0082d6 AM |
8165 | sect->alignment_power = 2; |
8166 | ||
936e320b | 8167 | return elfcore_maybe_make_sect (abfd, name, sect); |
bb0082d6 AM |
8168 | } |
8169 | ||
252b5132 | 8170 | /* prstatus_t exists on: |
4a938328 | 8171 | solaris 2.5+ |
252b5132 RH |
8172 | linux 2.[01] + glibc |
8173 | unixware 4.2 | |
8174 | */ | |
8175 | ||
8176 | #if defined (HAVE_PRSTATUS_T) | |
a7b97311 | 8177 | |
b34976b6 | 8178 | static bfd_boolean |
217aa764 | 8179 | elfcore_grok_prstatus (bfd *abfd, Elf_Internal_Note *note) |
252b5132 | 8180 | { |
eea6121a | 8181 | size_t size; |
7ee38065 | 8182 | int offset; |
252b5132 | 8183 | |
4a938328 MS |
8184 | if (note->descsz == sizeof (prstatus_t)) |
8185 | { | |
8186 | prstatus_t prstat; | |
252b5132 | 8187 | |
eea6121a | 8188 | size = sizeof (prstat.pr_reg); |
7ee38065 | 8189 | offset = offsetof (prstatus_t, pr_reg); |
4a938328 | 8190 | memcpy (&prstat, note->descdata, sizeof (prstat)); |
252b5132 | 8191 | |
fa49d224 NC |
8192 | /* Do not overwrite the core signal if it |
8193 | has already been set by another thread. */ | |
228e534f AM |
8194 | if (elf_tdata (abfd)->core->signal == 0) |
8195 | elf_tdata (abfd)->core->signal = prstat.pr_cursig; | |
8196 | if (elf_tdata (abfd)->core->pid == 0) | |
8197 | elf_tdata (abfd)->core->pid = prstat.pr_pid; | |
252b5132 | 8198 | |
4a938328 MS |
8199 | /* pr_who exists on: |
8200 | solaris 2.5+ | |
8201 | unixware 4.2 | |
8202 | pr_who doesn't exist on: | |
8203 | linux 2.[01] | |
8204 | */ | |
252b5132 | 8205 | #if defined (HAVE_PRSTATUS_T_PR_WHO) |
228e534f | 8206 | elf_tdata (abfd)->core->lwpid = prstat.pr_who; |
261b8d08 | 8207 | #else |
228e534f | 8208 | elf_tdata (abfd)->core->lwpid = prstat.pr_pid; |
252b5132 | 8209 | #endif |
4a938328 | 8210 | } |
7ee38065 | 8211 | #if defined (HAVE_PRSTATUS32_T) |
4a938328 MS |
8212 | else if (note->descsz == sizeof (prstatus32_t)) |
8213 | { | |
8214 | /* 64-bit host, 32-bit corefile */ | |
8215 | prstatus32_t prstat; | |
8216 | ||
eea6121a | 8217 | size = sizeof (prstat.pr_reg); |
7ee38065 | 8218 | offset = offsetof (prstatus32_t, pr_reg); |
4a938328 MS |
8219 | memcpy (&prstat, note->descdata, sizeof (prstat)); |
8220 | ||
fa49d224 NC |
8221 | /* Do not overwrite the core signal if it |
8222 | has already been set by another thread. */ | |
228e534f AM |
8223 | if (elf_tdata (abfd)->core->signal == 0) |
8224 | elf_tdata (abfd)->core->signal = prstat.pr_cursig; | |
8225 | if (elf_tdata (abfd)->core->pid == 0) | |
8226 | elf_tdata (abfd)->core->pid = prstat.pr_pid; | |
4a938328 MS |
8227 | |
8228 | /* pr_who exists on: | |
8229 | solaris 2.5+ | |
8230 | unixware 4.2 | |
8231 | pr_who doesn't exist on: | |
8232 | linux 2.[01] | |
8233 | */ | |
7ee38065 | 8234 | #if defined (HAVE_PRSTATUS32_T_PR_WHO) |
228e534f | 8235 | elf_tdata (abfd)->core->lwpid = prstat.pr_who; |
261b8d08 | 8236 | #else |
228e534f | 8237 | elf_tdata (abfd)->core->lwpid = prstat.pr_pid; |
4a938328 MS |
8238 | #endif |
8239 | } | |
7ee38065 | 8240 | #endif /* HAVE_PRSTATUS32_T */ |
4a938328 MS |
8241 | else |
8242 | { | |
8243 | /* Fail - we don't know how to handle any other | |
8244 | note size (ie. data object type). */ | |
b34976b6 | 8245 | return TRUE; |
4a938328 | 8246 | } |
252b5132 | 8247 | |
bb0082d6 | 8248 | /* Make a ".reg/999" section and a ".reg" section. */ |
936e320b | 8249 | return _bfd_elfcore_make_pseudosection (abfd, ".reg", |
eea6121a | 8250 | size, note->descpos + offset); |
252b5132 RH |
8251 | } |
8252 | #endif /* defined (HAVE_PRSTATUS_T) */ | |
8253 | ||
bb0082d6 | 8254 | /* Create a pseudosection containing the exact contents of NOTE. */ |
b34976b6 | 8255 | static bfd_boolean |
217aa764 AM |
8256 | elfcore_make_note_pseudosection (bfd *abfd, |
8257 | char *name, | |
8258 | Elf_Internal_Note *note) | |
252b5132 | 8259 | { |
936e320b AM |
8260 | return _bfd_elfcore_make_pseudosection (abfd, name, |
8261 | note->descsz, note->descpos); | |
252b5132 RH |
8262 | } |
8263 | ||
ff08c6bb JB |
8264 | /* There isn't a consistent prfpregset_t across platforms, |
8265 | but it doesn't matter, because we don't have to pick this | |
c044fabd KH |
8266 | data structure apart. */ |
8267 | ||
b34976b6 | 8268 | static bfd_boolean |
217aa764 | 8269 | elfcore_grok_prfpreg (bfd *abfd, Elf_Internal_Note *note) |
ff08c6bb JB |
8270 | { |
8271 | return elfcore_make_note_pseudosection (abfd, ".reg2", note); | |
8272 | } | |
8273 | ||
ff08c6bb | 8274 | /* Linux dumps the Intel SSE regs in a note named "LINUX" with a note |
971d4640 | 8275 | type of NT_PRXFPREG. Just include the whole note's contents |
ff08c6bb | 8276 | literally. */ |
c044fabd | 8277 | |
b34976b6 | 8278 | static bfd_boolean |
217aa764 | 8279 | elfcore_grok_prxfpreg (bfd *abfd, Elf_Internal_Note *note) |
ff08c6bb JB |
8280 | { |
8281 | return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note); | |
8282 | } | |
8283 | ||
4339cae0 L |
8284 | /* Linux dumps the Intel XSAVE extended state in a note named "LINUX" |
8285 | with a note type of NT_X86_XSTATE. Just include the whole note's | |
8286 | contents literally. */ | |
8287 | ||
8288 | static bfd_boolean | |
8289 | elfcore_grok_xstatereg (bfd *abfd, Elf_Internal_Note *note) | |
8290 | { | |
8291 | return elfcore_make_note_pseudosection (abfd, ".reg-xstate", note); | |
8292 | } | |
8293 | ||
97753bd5 AM |
8294 | static bfd_boolean |
8295 | elfcore_grok_ppc_vmx (bfd *abfd, Elf_Internal_Note *note) | |
8296 | { | |
8297 | return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vmx", note); | |
8298 | } | |
8299 | ||
89eeb0bc LM |
8300 | static bfd_boolean |
8301 | elfcore_grok_ppc_vsx (bfd *abfd, Elf_Internal_Note *note) | |
8302 | { | |
8303 | return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vsx", note); | |
8304 | } | |
97753bd5 | 8305 | |
0675e188 UW |
8306 | static bfd_boolean |
8307 | elfcore_grok_s390_high_gprs (bfd *abfd, Elf_Internal_Note *note) | |
8308 | { | |
8309 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-high-gprs", note); | |
8310 | } | |
8311 | ||
d7eeb400 MS |
8312 | static bfd_boolean |
8313 | elfcore_grok_s390_timer (bfd *abfd, Elf_Internal_Note *note) | |
8314 | { | |
8315 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-timer", note); | |
8316 | } | |
8317 | ||
8318 | static bfd_boolean | |
8319 | elfcore_grok_s390_todcmp (bfd *abfd, Elf_Internal_Note *note) | |
8320 | { | |
8321 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-todcmp", note); | |
8322 | } | |
8323 | ||
8324 | static bfd_boolean | |
8325 | elfcore_grok_s390_todpreg (bfd *abfd, Elf_Internal_Note *note) | |
8326 | { | |
8327 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-todpreg", note); | |
8328 | } | |
8329 | ||
8330 | static bfd_boolean | |
8331 | elfcore_grok_s390_ctrs (bfd *abfd, Elf_Internal_Note *note) | |
8332 | { | |
8333 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-ctrs", note); | |
8334 | } | |
8335 | ||
8336 | static bfd_boolean | |
8337 | elfcore_grok_s390_prefix (bfd *abfd, Elf_Internal_Note *note) | |
8338 | { | |
8339 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-prefix", note); | |
8340 | } | |
8341 | ||
355b81d9 UW |
8342 | static bfd_boolean |
8343 | elfcore_grok_s390_last_break (bfd *abfd, Elf_Internal_Note *note) | |
8344 | { | |
8345 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-last-break", note); | |
8346 | } | |
8347 | ||
8348 | static bfd_boolean | |
8349 | elfcore_grok_s390_system_call (bfd *abfd, Elf_Internal_Note *note) | |
8350 | { | |
8351 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-system-call", note); | |
8352 | } | |
8353 | ||
abb3f6cc NC |
8354 | static bfd_boolean |
8355 | elfcore_grok_s390_tdb (bfd *abfd, Elf_Internal_Note *note) | |
8356 | { | |
8357 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-tdb", note); | |
8358 | } | |
8359 | ||
faa9a424 UW |
8360 | static bfd_boolean |
8361 | elfcore_grok_arm_vfp (bfd *abfd, Elf_Internal_Note *note) | |
8362 | { | |
8363 | return elfcore_make_note_pseudosection (abfd, ".reg-arm-vfp", note); | |
8364 | } | |
8365 | ||
652451f8 YZ |
8366 | static bfd_boolean |
8367 | elfcore_grok_aarch_tls (bfd *abfd, Elf_Internal_Note *note) | |
8368 | { | |
8369 | return elfcore_make_note_pseudosection (abfd, ".reg-aarch-tls", note); | |
8370 | } | |
8371 | ||
8372 | static bfd_boolean | |
8373 | elfcore_grok_aarch_hw_break (bfd *abfd, Elf_Internal_Note *note) | |
8374 | { | |
8375 | return elfcore_make_note_pseudosection (abfd, ".reg-aarch-hw-break", note); | |
8376 | } | |
8377 | ||
8378 | static bfd_boolean | |
8379 | elfcore_grok_aarch_hw_watch (bfd *abfd, Elf_Internal_Note *note) | |
8380 | { | |
8381 | return elfcore_make_note_pseudosection (abfd, ".reg-aarch-hw-watch", note); | |
8382 | } | |
8383 | ||
252b5132 | 8384 | #if defined (HAVE_PRPSINFO_T) |
4a938328 | 8385 | typedef prpsinfo_t elfcore_psinfo_t; |
7ee38065 | 8386 | #if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */ |
4a938328 MS |
8387 | typedef prpsinfo32_t elfcore_psinfo32_t; |
8388 | #endif | |
252b5132 RH |
8389 | #endif |
8390 | ||
8391 | #if defined (HAVE_PSINFO_T) | |
4a938328 | 8392 | typedef psinfo_t elfcore_psinfo_t; |
7ee38065 | 8393 | #if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */ |
4a938328 MS |
8394 | typedef psinfo32_t elfcore_psinfo32_t; |
8395 | #endif | |
252b5132 RH |
8396 | #endif |
8397 | ||
252b5132 RH |
8398 | /* return a malloc'ed copy of a string at START which is at |
8399 | most MAX bytes long, possibly without a terminating '\0'. | |
c044fabd | 8400 | the copy will always have a terminating '\0'. */ |
252b5132 | 8401 | |
936e320b | 8402 | char * |
217aa764 | 8403 | _bfd_elfcore_strndup (bfd *abfd, char *start, size_t max) |
252b5132 | 8404 | { |
dc810e39 | 8405 | char *dups; |
a50b1753 | 8406 | char *end = (char *) memchr (start, '\0', max); |
dc810e39 | 8407 | size_t len; |
252b5132 RH |
8408 | |
8409 | if (end == NULL) | |
8410 | len = max; | |
8411 | else | |
8412 | len = end - start; | |
8413 | ||
a50b1753 | 8414 | dups = (char *) bfd_alloc (abfd, len + 1); |
dc810e39 | 8415 | if (dups == NULL) |
252b5132 RH |
8416 | return NULL; |
8417 | ||
dc810e39 AM |
8418 | memcpy (dups, start, len); |
8419 | dups[len] = '\0'; | |
252b5132 | 8420 | |
dc810e39 | 8421 | return dups; |
252b5132 RH |
8422 | } |
8423 | ||
bb0082d6 | 8424 | #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) |
b34976b6 | 8425 | static bfd_boolean |
217aa764 | 8426 | elfcore_grok_psinfo (bfd *abfd, Elf_Internal_Note *note) |
252b5132 | 8427 | { |
4a938328 MS |
8428 | if (note->descsz == sizeof (elfcore_psinfo_t)) |
8429 | { | |
8430 | elfcore_psinfo_t psinfo; | |
252b5132 | 8431 | |
7ee38065 | 8432 | memcpy (&psinfo, note->descdata, sizeof (psinfo)); |
252b5132 | 8433 | |
335e41d4 | 8434 | #if defined (HAVE_PSINFO_T_PR_PID) || defined (HAVE_PRPSINFO_T_PR_PID) |
228e534f | 8435 | elf_tdata (abfd)->core->pid = psinfo.pr_pid; |
335e41d4 | 8436 | #endif |
228e534f | 8437 | elf_tdata (abfd)->core->program |
936e320b AM |
8438 | = _bfd_elfcore_strndup (abfd, psinfo.pr_fname, |
8439 | sizeof (psinfo.pr_fname)); | |
252b5132 | 8440 | |
228e534f | 8441 | elf_tdata (abfd)->core->command |
936e320b AM |
8442 | = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs, |
8443 | sizeof (psinfo.pr_psargs)); | |
4a938328 | 8444 | } |
7ee38065 | 8445 | #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T) |
4a938328 MS |
8446 | else if (note->descsz == sizeof (elfcore_psinfo32_t)) |
8447 | { | |
8448 | /* 64-bit host, 32-bit corefile */ | |
8449 | elfcore_psinfo32_t psinfo; | |
8450 | ||
7ee38065 | 8451 | memcpy (&psinfo, note->descdata, sizeof (psinfo)); |
252b5132 | 8452 | |
335e41d4 | 8453 | #if defined (HAVE_PSINFO32_T_PR_PID) || defined (HAVE_PRPSINFO32_T_PR_PID) |
228e534f | 8454 | elf_tdata (abfd)->core->pid = psinfo.pr_pid; |
335e41d4 | 8455 | #endif |
228e534f | 8456 | elf_tdata (abfd)->core->program |
936e320b AM |
8457 | = _bfd_elfcore_strndup (abfd, psinfo.pr_fname, |
8458 | sizeof (psinfo.pr_fname)); | |
4a938328 | 8459 | |
228e534f | 8460 | elf_tdata (abfd)->core->command |
936e320b AM |
8461 | = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs, |
8462 | sizeof (psinfo.pr_psargs)); | |
4a938328 MS |
8463 | } |
8464 | #endif | |
8465 | ||
8466 | else | |
8467 | { | |
8468 | /* Fail - we don't know how to handle any other | |
8469 | note size (ie. data object type). */ | |
b34976b6 | 8470 | return TRUE; |
4a938328 | 8471 | } |
252b5132 RH |
8472 | |
8473 | /* Note that for some reason, a spurious space is tacked | |
8474 | onto the end of the args in some (at least one anyway) | |
c044fabd | 8475 | implementations, so strip it off if it exists. */ |
252b5132 RH |
8476 | |
8477 | { | |
228e534f | 8478 | char *command = elf_tdata (abfd)->core->command; |
252b5132 RH |
8479 | int n = strlen (command); |
8480 | ||
8481 | if (0 < n && command[n - 1] == ' ') | |
8482 | command[n - 1] = '\0'; | |
8483 | } | |
8484 | ||
b34976b6 | 8485 | return TRUE; |
252b5132 RH |
8486 | } |
8487 | #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */ | |
8488 | ||
252b5132 | 8489 | #if defined (HAVE_PSTATUS_T) |
b34976b6 | 8490 | static bfd_boolean |
217aa764 | 8491 | elfcore_grok_pstatus (bfd *abfd, Elf_Internal_Note *note) |
252b5132 | 8492 | { |
f572a39d AM |
8493 | if (note->descsz == sizeof (pstatus_t) |
8494 | #if defined (HAVE_PXSTATUS_T) | |
8495 | || note->descsz == sizeof (pxstatus_t) | |
8496 | #endif | |
8497 | ) | |
4a938328 MS |
8498 | { |
8499 | pstatus_t pstat; | |
252b5132 | 8500 | |
4a938328 | 8501 | memcpy (&pstat, note->descdata, sizeof (pstat)); |
252b5132 | 8502 | |
228e534f | 8503 | elf_tdata (abfd)->core->pid = pstat.pr_pid; |
4a938328 | 8504 | } |
7ee38065 | 8505 | #if defined (HAVE_PSTATUS32_T) |
4a938328 MS |
8506 | else if (note->descsz == sizeof (pstatus32_t)) |
8507 | { | |
8508 | /* 64-bit host, 32-bit corefile */ | |
8509 | pstatus32_t pstat; | |
252b5132 | 8510 | |
4a938328 | 8511 | memcpy (&pstat, note->descdata, sizeof (pstat)); |
252b5132 | 8512 | |
228e534f | 8513 | elf_tdata (abfd)->core->pid = pstat.pr_pid; |
4a938328 MS |
8514 | } |
8515 | #endif | |
252b5132 RH |
8516 | /* Could grab some more details from the "representative" |
8517 | lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an | |
c044fabd | 8518 | NT_LWPSTATUS note, presumably. */ |
252b5132 | 8519 | |
b34976b6 | 8520 | return TRUE; |
252b5132 RH |
8521 | } |
8522 | #endif /* defined (HAVE_PSTATUS_T) */ | |
8523 | ||
252b5132 | 8524 | #if defined (HAVE_LWPSTATUS_T) |
b34976b6 | 8525 | static bfd_boolean |
217aa764 | 8526 | elfcore_grok_lwpstatus (bfd *abfd, Elf_Internal_Note *note) |
252b5132 RH |
8527 | { |
8528 | lwpstatus_t lwpstat; | |
8529 | char buf[100]; | |
c044fabd | 8530 | char *name; |
d4c88bbb | 8531 | size_t len; |
c044fabd | 8532 | asection *sect; |
252b5132 | 8533 | |
f572a39d AM |
8534 | if (note->descsz != sizeof (lwpstat) |
8535 | #if defined (HAVE_LWPXSTATUS_T) | |
8536 | && note->descsz != sizeof (lwpxstatus_t) | |
8537 | #endif | |
8538 | ) | |
b34976b6 | 8539 | return TRUE; |
252b5132 RH |
8540 | |
8541 | memcpy (&lwpstat, note->descdata, sizeof (lwpstat)); | |
8542 | ||
228e534f | 8543 | elf_tdata (abfd)->core->lwpid = lwpstat.pr_lwpid; |
a1504221 JB |
8544 | /* Do not overwrite the core signal if it has already been set by |
8545 | another thread. */ | |
228e534f AM |
8546 | if (elf_tdata (abfd)->core->signal == 0) |
8547 | elf_tdata (abfd)->core->signal = lwpstat.pr_cursig; | |
252b5132 | 8548 | |
c044fabd | 8549 | /* Make a ".reg/999" section. */ |
252b5132 RH |
8550 | |
8551 | sprintf (buf, ".reg/%d", elfcore_make_pid (abfd)); | |
d4c88bbb | 8552 | len = strlen (buf) + 1; |
217aa764 | 8553 | name = bfd_alloc (abfd, len); |
252b5132 | 8554 | if (name == NULL) |
b34976b6 | 8555 | return FALSE; |
d4c88bbb | 8556 | memcpy (name, buf, len); |
252b5132 | 8557 | |
117ed4f8 | 8558 | sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); |
252b5132 | 8559 | if (sect == NULL) |
b34976b6 | 8560 | return FALSE; |
252b5132 RH |
8561 | |
8562 | #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT) | |
eea6121a | 8563 | sect->size = sizeof (lwpstat.pr_context.uc_mcontext.gregs); |
252b5132 RH |
8564 | sect->filepos = note->descpos |
8565 | + offsetof (lwpstatus_t, pr_context.uc_mcontext.gregs); | |
8566 | #endif | |
8567 | ||
8568 | #if defined (HAVE_LWPSTATUS_T_PR_REG) | |
eea6121a | 8569 | sect->size = sizeof (lwpstat.pr_reg); |
252b5132 RH |
8570 | sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_reg); |
8571 | #endif | |
8572 | ||
252b5132 RH |
8573 | sect->alignment_power = 2; |
8574 | ||
8575 | if (!elfcore_maybe_make_sect (abfd, ".reg", sect)) | |
b34976b6 | 8576 | return FALSE; |
252b5132 RH |
8577 | |
8578 | /* Make a ".reg2/999" section */ | |
8579 | ||
8580 | sprintf (buf, ".reg2/%d", elfcore_make_pid (abfd)); | |
d4c88bbb | 8581 | len = strlen (buf) + 1; |
217aa764 | 8582 | name = bfd_alloc (abfd, len); |
252b5132 | 8583 | if (name == NULL) |
b34976b6 | 8584 | return FALSE; |
d4c88bbb | 8585 | memcpy (name, buf, len); |
252b5132 | 8586 | |
117ed4f8 | 8587 | sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); |
252b5132 | 8588 | if (sect == NULL) |
b34976b6 | 8589 | return FALSE; |
252b5132 RH |
8590 | |
8591 | #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT) | |
eea6121a | 8592 | sect->size = sizeof (lwpstat.pr_context.uc_mcontext.fpregs); |
252b5132 RH |
8593 | sect->filepos = note->descpos |
8594 | + offsetof (lwpstatus_t, pr_context.uc_mcontext.fpregs); | |
8595 | #endif | |
8596 | ||
8597 | #if defined (HAVE_LWPSTATUS_T_PR_FPREG) | |
eea6121a | 8598 | sect->size = sizeof (lwpstat.pr_fpreg); |
252b5132 RH |
8599 | sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_fpreg); |
8600 | #endif | |
8601 | ||
252b5132 RH |
8602 | sect->alignment_power = 2; |
8603 | ||
936e320b | 8604 | return elfcore_maybe_make_sect (abfd, ".reg2", sect); |
252b5132 RH |
8605 | } |
8606 | #endif /* defined (HAVE_LWPSTATUS_T) */ | |
8607 | ||
b34976b6 | 8608 | static bfd_boolean |
217aa764 | 8609 | elfcore_grok_win32pstatus (bfd *abfd, Elf_Internal_Note *note) |
16e9c715 NC |
8610 | { |
8611 | char buf[30]; | |
c044fabd | 8612 | char *name; |
d4c88bbb | 8613 | size_t len; |
c044fabd | 8614 | asection *sect; |
4a6636fb PA |
8615 | int type; |
8616 | int is_active_thread; | |
8617 | bfd_vma base_addr; | |
16e9c715 | 8618 | |
4a6636fb | 8619 | if (note->descsz < 728) |
b34976b6 | 8620 | return TRUE; |
16e9c715 | 8621 | |
4a6636fb PA |
8622 | if (! CONST_STRNEQ (note->namedata, "win32")) |
8623 | return TRUE; | |
8624 | ||
8625 | type = bfd_get_32 (abfd, note->descdata); | |
c044fabd | 8626 | |
4a6636fb | 8627 | switch (type) |
16e9c715 | 8628 | { |
4a6636fb | 8629 | case 1 /* NOTE_INFO_PROCESS */: |
228e534f | 8630 | /* FIXME: need to add ->core->command. */ |
4a6636fb | 8631 | /* process_info.pid */ |
228e534f | 8632 | elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, note->descdata + 8); |
4a6636fb | 8633 | /* process_info.signal */ |
228e534f | 8634 | elf_tdata (abfd)->core->signal = bfd_get_32 (abfd, note->descdata + 12); |
c044fabd | 8635 | break; |
16e9c715 | 8636 | |
4a6636fb | 8637 | case 2 /* NOTE_INFO_THREAD */: |
16e9c715 | 8638 | /* Make a ".reg/999" section. */ |
4a6636fb PA |
8639 | /* thread_info.tid */ |
8640 | sprintf (buf, ".reg/%ld", (long) bfd_get_32 (abfd, note->descdata + 8)); | |
c044fabd | 8641 | |
d4c88bbb | 8642 | len = strlen (buf) + 1; |
a50b1753 | 8643 | name = (char *) bfd_alloc (abfd, len); |
16e9c715 | 8644 | if (name == NULL) |
b34976b6 | 8645 | return FALSE; |
c044fabd | 8646 | |
d4c88bbb | 8647 | memcpy (name, buf, len); |
16e9c715 | 8648 | |
117ed4f8 | 8649 | sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); |
16e9c715 | 8650 | if (sect == NULL) |
b34976b6 | 8651 | return FALSE; |
c044fabd | 8652 | |
4a6636fb PA |
8653 | /* sizeof (thread_info.thread_context) */ |
8654 | sect->size = 716; | |
8655 | /* offsetof (thread_info.thread_context) */ | |
8656 | sect->filepos = note->descpos + 12; | |
16e9c715 NC |
8657 | sect->alignment_power = 2; |
8658 | ||
4a6636fb PA |
8659 | /* thread_info.is_active_thread */ |
8660 | is_active_thread = bfd_get_32 (abfd, note->descdata + 8); | |
8661 | ||
8662 | if (is_active_thread) | |
16e9c715 | 8663 | if (! elfcore_maybe_make_sect (abfd, ".reg", sect)) |
b34976b6 | 8664 | return FALSE; |
16e9c715 NC |
8665 | break; |
8666 | ||
4a6636fb | 8667 | case 3 /* NOTE_INFO_MODULE */: |
16e9c715 | 8668 | /* Make a ".module/xxxxxxxx" section. */ |
4a6636fb PA |
8669 | /* module_info.base_address */ |
8670 | base_addr = bfd_get_32 (abfd, note->descdata + 4); | |
0af1713e | 8671 | sprintf (buf, ".module/%08lx", (unsigned long) base_addr); |
c044fabd | 8672 | |
d4c88bbb | 8673 | len = strlen (buf) + 1; |
a50b1753 | 8674 | name = (char *) bfd_alloc (abfd, len); |
16e9c715 | 8675 | if (name == NULL) |
b34976b6 | 8676 | return FALSE; |
c044fabd | 8677 | |
d4c88bbb | 8678 | memcpy (name, buf, len); |
252b5132 | 8679 | |
117ed4f8 | 8680 | sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); |
c044fabd | 8681 | |
16e9c715 | 8682 | if (sect == NULL) |
b34976b6 | 8683 | return FALSE; |
c044fabd | 8684 | |
eea6121a | 8685 | sect->size = note->descsz; |
16e9c715 | 8686 | sect->filepos = note->descpos; |
16e9c715 NC |
8687 | sect->alignment_power = 2; |
8688 | break; | |
8689 | ||
8690 | default: | |
b34976b6 | 8691 | return TRUE; |
16e9c715 NC |
8692 | } |
8693 | ||
b34976b6 | 8694 | return TRUE; |
16e9c715 | 8695 | } |
252b5132 | 8696 | |
b34976b6 | 8697 | static bfd_boolean |
217aa764 | 8698 | elfcore_grok_note (bfd *abfd, Elf_Internal_Note *note) |
252b5132 | 8699 | { |
9c5bfbb7 | 8700 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
bb0082d6 | 8701 | |
252b5132 RH |
8702 | switch (note->type) |
8703 | { | |
8704 | default: | |
b34976b6 | 8705 | return TRUE; |
252b5132 | 8706 | |
252b5132 | 8707 | case NT_PRSTATUS: |
bb0082d6 AM |
8708 | if (bed->elf_backend_grok_prstatus) |
8709 | if ((*bed->elf_backend_grok_prstatus) (abfd, note)) | |
b34976b6 | 8710 | return TRUE; |
bb0082d6 | 8711 | #if defined (HAVE_PRSTATUS_T) |
252b5132 | 8712 | return elfcore_grok_prstatus (abfd, note); |
bb0082d6 | 8713 | #else |
b34976b6 | 8714 | return TRUE; |
252b5132 RH |
8715 | #endif |
8716 | ||
8717 | #if defined (HAVE_PSTATUS_T) | |
8718 | case NT_PSTATUS: | |
8719 | return elfcore_grok_pstatus (abfd, note); | |
8720 | #endif | |
8721 | ||
8722 | #if defined (HAVE_LWPSTATUS_T) | |
8723 | case NT_LWPSTATUS: | |
8724 | return elfcore_grok_lwpstatus (abfd, note); | |
8725 | #endif | |
8726 | ||
8727 | case NT_FPREGSET: /* FIXME: rename to NT_PRFPREG */ | |
8728 | return elfcore_grok_prfpreg (abfd, note); | |
8729 | ||
c044fabd | 8730 | case NT_WIN32PSTATUS: |
16e9c715 | 8731 | return elfcore_grok_win32pstatus (abfd, note); |
16e9c715 | 8732 | |
c044fabd | 8733 | case NT_PRXFPREG: /* Linux SSE extension */ |
e377ab71 MK |
8734 | if (note->namesz == 6 |
8735 | && strcmp (note->namedata, "LINUX") == 0) | |
ff08c6bb JB |
8736 | return elfcore_grok_prxfpreg (abfd, note); |
8737 | else | |
b34976b6 | 8738 | return TRUE; |
ff08c6bb | 8739 | |
4339cae0 L |
8740 | case NT_X86_XSTATE: /* Linux XSAVE extension */ |
8741 | if (note->namesz == 6 | |
8742 | && strcmp (note->namedata, "LINUX") == 0) | |
8743 | return elfcore_grok_xstatereg (abfd, note); | |
8744 | else | |
8745 | return TRUE; | |
8746 | ||
97753bd5 AM |
8747 | case NT_PPC_VMX: |
8748 | if (note->namesz == 6 | |
8749 | && strcmp (note->namedata, "LINUX") == 0) | |
8750 | return elfcore_grok_ppc_vmx (abfd, note); | |
8751 | else | |
8752 | return TRUE; | |
8753 | ||
89eeb0bc LM |
8754 | case NT_PPC_VSX: |
8755 | if (note->namesz == 6 | |
8756 | && strcmp (note->namedata, "LINUX") == 0) | |
8757 | return elfcore_grok_ppc_vsx (abfd, note); | |
8758 | else | |
8759 | return TRUE; | |
8760 | ||
0675e188 UW |
8761 | case NT_S390_HIGH_GPRS: |
8762 | if (note->namesz == 6 | |
8763 | && strcmp (note->namedata, "LINUX") == 0) | |
8764 | return elfcore_grok_s390_high_gprs (abfd, note); | |
8765 | else | |
8766 | return TRUE; | |
8767 | ||
d7eeb400 MS |
8768 | case NT_S390_TIMER: |
8769 | if (note->namesz == 6 | |
8770 | && strcmp (note->namedata, "LINUX") == 0) | |
8771 | return elfcore_grok_s390_timer (abfd, note); | |
8772 | else | |
8773 | return TRUE; | |
8774 | ||
8775 | case NT_S390_TODCMP: | |
8776 | if (note->namesz == 6 | |
8777 | && strcmp (note->namedata, "LINUX") == 0) | |
8778 | return elfcore_grok_s390_todcmp (abfd, note); | |
8779 | else | |
8780 | return TRUE; | |
8781 | ||
8782 | case NT_S390_TODPREG: | |
8783 | if (note->namesz == 6 | |
8784 | && strcmp (note->namedata, "LINUX") == 0) | |
8785 | return elfcore_grok_s390_todpreg (abfd, note); | |
8786 | else | |
8787 | return TRUE; | |
8788 | ||
8789 | case NT_S390_CTRS: | |
8790 | if (note->namesz == 6 | |
8791 | && strcmp (note->namedata, "LINUX") == 0) | |
8792 | return elfcore_grok_s390_ctrs (abfd, note); | |
8793 | else | |
8794 | return TRUE; | |
8795 | ||
8796 | case NT_S390_PREFIX: | |
8797 | if (note->namesz == 6 | |
8798 | && strcmp (note->namedata, "LINUX") == 0) | |
8799 | return elfcore_grok_s390_prefix (abfd, note); | |
8800 | else | |
8801 | return TRUE; | |
8802 | ||
355b81d9 UW |
8803 | case NT_S390_LAST_BREAK: |
8804 | if (note->namesz == 6 | |
8805 | && strcmp (note->namedata, "LINUX") == 0) | |
8806 | return elfcore_grok_s390_last_break (abfd, note); | |
8807 | else | |
8808 | return TRUE; | |
8809 | ||
8810 | case NT_S390_SYSTEM_CALL: | |
8811 | if (note->namesz == 6 | |
8812 | && strcmp (note->namedata, "LINUX") == 0) | |
8813 | return elfcore_grok_s390_system_call (abfd, note); | |
8814 | else | |
8815 | return TRUE; | |
8816 | ||
abb3f6cc NC |
8817 | case NT_S390_TDB: |
8818 | if (note->namesz == 6 | |
8819 | && strcmp (note->namedata, "LINUX") == 0) | |
8820 | return elfcore_grok_s390_tdb (abfd, note); | |
8821 | else | |
8822 | return TRUE; | |
8823 | ||
faa9a424 UW |
8824 | case NT_ARM_VFP: |
8825 | if (note->namesz == 6 | |
8826 | && strcmp (note->namedata, "LINUX") == 0) | |
8827 | return elfcore_grok_arm_vfp (abfd, note); | |
8828 | else | |
8829 | return TRUE; | |
8830 | ||
652451f8 YZ |
8831 | case NT_ARM_TLS: |
8832 | if (note->namesz == 6 | |
8833 | && strcmp (note->namedata, "LINUX") == 0) | |
8834 | return elfcore_grok_aarch_tls (abfd, note); | |
8835 | else | |
8836 | return TRUE; | |
8837 | ||
8838 | case NT_ARM_HW_BREAK: | |
8839 | if (note->namesz == 6 | |
8840 | && strcmp (note->namedata, "LINUX") == 0) | |
8841 | return elfcore_grok_aarch_hw_break (abfd, note); | |
8842 | else | |
8843 | return TRUE; | |
8844 | ||
8845 | case NT_ARM_HW_WATCH: | |
8846 | if (note->namesz == 6 | |
8847 | && strcmp (note->namedata, "LINUX") == 0) | |
8848 | return elfcore_grok_aarch_hw_watch (abfd, note); | |
8849 | else | |
8850 | return TRUE; | |
8851 | ||
252b5132 RH |
8852 | case NT_PRPSINFO: |
8853 | case NT_PSINFO: | |
bb0082d6 AM |
8854 | if (bed->elf_backend_grok_psinfo) |
8855 | if ((*bed->elf_backend_grok_psinfo) (abfd, note)) | |
b34976b6 | 8856 | return TRUE; |
bb0082d6 | 8857 | #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) |
252b5132 | 8858 | return elfcore_grok_psinfo (abfd, note); |
bb0082d6 | 8859 | #else |
b34976b6 | 8860 | return TRUE; |
252b5132 | 8861 | #endif |
3333a7c3 RM |
8862 | |
8863 | case NT_AUXV: | |
8864 | { | |
117ed4f8 AM |
8865 | asection *sect = bfd_make_section_anyway_with_flags (abfd, ".auxv", |
8866 | SEC_HAS_CONTENTS); | |
3333a7c3 RM |
8867 | |
8868 | if (sect == NULL) | |
8869 | return FALSE; | |
eea6121a | 8870 | sect->size = note->descsz; |
3333a7c3 | 8871 | sect->filepos = note->descpos; |
3333a7c3 RM |
8872 | sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32; |
8873 | ||
8874 | return TRUE; | |
8875 | } | |
9015683b | 8876 | |
451b7c33 TT |
8877 | case NT_FILE: |
8878 | return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.file", | |
8879 | note); | |
8880 | ||
9015683b TT |
8881 | case NT_SIGINFO: |
8882 | return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.siginfo", | |
8883 | note); | |
252b5132 RH |
8884 | } |
8885 | } | |
8886 | ||
718175fa JK |
8887 | static bfd_boolean |
8888 | elfobj_grok_gnu_build_id (bfd *abfd, Elf_Internal_Note *note) | |
8889 | { | |
30e8ee25 AM |
8890 | struct elf_obj_tdata *t; |
8891 | ||
8892 | if (note->descsz == 0) | |
8893 | return FALSE; | |
8894 | ||
8895 | t = elf_tdata (abfd); | |
c0355132 | 8896 | t->build_id = bfd_alloc (abfd, sizeof (*t->build_id) - 1 + note->descsz); |
30e8ee25 | 8897 | if (t->build_id == NULL) |
718175fa JK |
8898 | return FALSE; |
8899 | ||
c0355132 AM |
8900 | t->build_id->size = note->descsz; |
8901 | memcpy (t->build_id->data, note->descdata, note->descsz); | |
718175fa JK |
8902 | |
8903 | return TRUE; | |
8904 | } | |
8905 | ||
8906 | static bfd_boolean | |
8907 | elfobj_grok_gnu_note (bfd *abfd, Elf_Internal_Note *note) | |
8908 | { | |
8909 | switch (note->type) | |
8910 | { | |
8911 | default: | |
8912 | return TRUE; | |
8913 | ||
8914 | case NT_GNU_BUILD_ID: | |
8915 | return elfobj_grok_gnu_build_id (abfd, note); | |
8916 | } | |
8917 | } | |
8918 | ||
e21e5835 NC |
8919 | static bfd_boolean |
8920 | elfobj_grok_stapsdt_note_1 (bfd *abfd, Elf_Internal_Note *note) | |
8921 | { | |
8922 | struct sdt_note *cur = | |
8923 | (struct sdt_note *) bfd_alloc (abfd, sizeof (struct sdt_note) | |
8924 | + note->descsz); | |
8925 | ||
8926 | cur->next = (struct sdt_note *) (elf_tdata (abfd))->sdt_note_head; | |
8927 | cur->size = (bfd_size_type) note->descsz; | |
8928 | memcpy (cur->data, note->descdata, note->descsz); | |
8929 | ||
8930 | elf_tdata (abfd)->sdt_note_head = cur; | |
8931 | ||
8932 | return TRUE; | |
8933 | } | |
8934 | ||
8935 | static bfd_boolean | |
8936 | elfobj_grok_stapsdt_note (bfd *abfd, Elf_Internal_Note *note) | |
8937 | { | |
8938 | switch (note->type) | |
8939 | { | |
8940 | case NT_STAPSDT: | |
8941 | return elfobj_grok_stapsdt_note_1 (abfd, note); | |
8942 | ||
8943 | default: | |
8944 | return TRUE; | |
8945 | } | |
8946 | } | |
8947 | ||
b34976b6 | 8948 | static bfd_boolean |
217aa764 | 8949 | elfcore_netbsd_get_lwpid (Elf_Internal_Note *note, int *lwpidp) |
50b2bdb7 AM |
8950 | { |
8951 | char *cp; | |
8952 | ||
8953 | cp = strchr (note->namedata, '@'); | |
8954 | if (cp != NULL) | |
8955 | { | |
d2b64500 | 8956 | *lwpidp = atoi(cp + 1); |
b34976b6 | 8957 | return TRUE; |
50b2bdb7 | 8958 | } |
b34976b6 | 8959 | return FALSE; |
50b2bdb7 AM |
8960 | } |
8961 | ||
b34976b6 | 8962 | static bfd_boolean |
217aa764 | 8963 | elfcore_grok_netbsd_procinfo (bfd *abfd, Elf_Internal_Note *note) |
50b2bdb7 | 8964 | { |
50b2bdb7 | 8965 | /* Signal number at offset 0x08. */ |
228e534f | 8966 | elf_tdata (abfd)->core->signal |
50b2bdb7 AM |
8967 | = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08); |
8968 | ||
8969 | /* Process ID at offset 0x50. */ | |
228e534f | 8970 | elf_tdata (abfd)->core->pid |
50b2bdb7 AM |
8971 | = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x50); |
8972 | ||
8973 | /* Command name at 0x7c (max 32 bytes, including nul). */ | |
228e534f | 8974 | elf_tdata (abfd)->core->command |
50b2bdb7 AM |
8975 | = _bfd_elfcore_strndup (abfd, note->descdata + 0x7c, 31); |
8976 | ||
7720ba9f MK |
8977 | return elfcore_make_note_pseudosection (abfd, ".note.netbsdcore.procinfo", |
8978 | note); | |
50b2bdb7 AM |
8979 | } |
8980 | ||
b34976b6 | 8981 | static bfd_boolean |
217aa764 | 8982 | elfcore_grok_netbsd_note (bfd *abfd, Elf_Internal_Note *note) |
50b2bdb7 AM |
8983 | { |
8984 | int lwp; | |
8985 | ||
8986 | if (elfcore_netbsd_get_lwpid (note, &lwp)) | |
228e534f | 8987 | elf_tdata (abfd)->core->lwpid = lwp; |
50b2bdb7 | 8988 | |
b4db1224 | 8989 | if (note->type == NT_NETBSDCORE_PROCINFO) |
50b2bdb7 AM |
8990 | { |
8991 | /* NetBSD-specific core "procinfo". Note that we expect to | |
08a40648 AM |
8992 | find this note before any of the others, which is fine, |
8993 | since the kernel writes this note out first when it | |
8994 | creates a core file. */ | |
47d9a591 | 8995 | |
50b2bdb7 AM |
8996 | return elfcore_grok_netbsd_procinfo (abfd, note); |
8997 | } | |
8998 | ||
b4db1224 JT |
8999 | /* As of Jan 2002 there are no other machine-independent notes |
9000 | defined for NetBSD core files. If the note type is less | |
9001 | than the start of the machine-dependent note types, we don't | |
9002 | understand it. */ | |
47d9a591 | 9003 | |
b4db1224 | 9004 | if (note->type < NT_NETBSDCORE_FIRSTMACH) |
b34976b6 | 9005 | return TRUE; |
50b2bdb7 AM |
9006 | |
9007 | ||
9008 | switch (bfd_get_arch (abfd)) | |
9009 | { | |
08a40648 AM |
9010 | /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and |
9011 | PT_GETFPREGS == mach+2. */ | |
50b2bdb7 AM |
9012 | |
9013 | case bfd_arch_alpha: | |
9014 | case bfd_arch_sparc: | |
9015 | switch (note->type) | |
08a40648 AM |
9016 | { |
9017 | case NT_NETBSDCORE_FIRSTMACH+0: | |
9018 | return elfcore_make_note_pseudosection (abfd, ".reg", note); | |
50b2bdb7 | 9019 | |
08a40648 AM |
9020 | case NT_NETBSDCORE_FIRSTMACH+2: |
9021 | return elfcore_make_note_pseudosection (abfd, ".reg2", note); | |
50b2bdb7 | 9022 | |
08a40648 AM |
9023 | default: |
9024 | return TRUE; | |
9025 | } | |
50b2bdb7 | 9026 | |
08a40648 AM |
9027 | /* On all other arch's, PT_GETREGS == mach+1 and |
9028 | PT_GETFPREGS == mach+3. */ | |
50b2bdb7 AM |
9029 | |
9030 | default: | |
9031 | switch (note->type) | |
08a40648 AM |
9032 | { |
9033 | case NT_NETBSDCORE_FIRSTMACH+1: | |
9034 | return elfcore_make_note_pseudosection (abfd, ".reg", note); | |
50b2bdb7 | 9035 | |
08a40648 AM |
9036 | case NT_NETBSDCORE_FIRSTMACH+3: |
9037 | return elfcore_make_note_pseudosection (abfd, ".reg2", note); | |
50b2bdb7 | 9038 | |
08a40648 AM |
9039 | default: |
9040 | return TRUE; | |
9041 | } | |
50b2bdb7 AM |
9042 | } |
9043 | /* NOTREACHED */ | |
9044 | } | |
9045 | ||
67cc5033 MK |
9046 | static bfd_boolean |
9047 | elfcore_grok_openbsd_procinfo (bfd *abfd, Elf_Internal_Note *note) | |
9048 | { | |
9049 | /* Signal number at offset 0x08. */ | |
228e534f | 9050 | elf_tdata (abfd)->core->signal |
67cc5033 MK |
9051 | = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08); |
9052 | ||
9053 | /* Process ID at offset 0x20. */ | |
228e534f | 9054 | elf_tdata (abfd)->core->pid |
67cc5033 MK |
9055 | = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x20); |
9056 | ||
9057 | /* Command name at 0x48 (max 32 bytes, including nul). */ | |
228e534f | 9058 | elf_tdata (abfd)->core->command |
67cc5033 MK |
9059 | = _bfd_elfcore_strndup (abfd, note->descdata + 0x48, 31); |
9060 | ||
9061 | return TRUE; | |
9062 | } | |
9063 | ||
9064 | static bfd_boolean | |
9065 | elfcore_grok_openbsd_note (bfd *abfd, Elf_Internal_Note *note) | |
9066 | { | |
9067 | if (note->type == NT_OPENBSD_PROCINFO) | |
9068 | return elfcore_grok_openbsd_procinfo (abfd, note); | |
9069 | ||
9070 | if (note->type == NT_OPENBSD_REGS) | |
9071 | return elfcore_make_note_pseudosection (abfd, ".reg", note); | |
9072 | ||
9073 | if (note->type == NT_OPENBSD_FPREGS) | |
9074 | return elfcore_make_note_pseudosection (abfd, ".reg2", note); | |
9075 | ||
9076 | if (note->type == NT_OPENBSD_XFPREGS) | |
9077 | return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note); | |
9078 | ||
9079 | if (note->type == NT_OPENBSD_AUXV) | |
9080 | { | |
9081 | asection *sect = bfd_make_section_anyway_with_flags (abfd, ".auxv", | |
9082 | SEC_HAS_CONTENTS); | |
9083 | ||
9084 | if (sect == NULL) | |
9085 | return FALSE; | |
9086 | sect->size = note->descsz; | |
9087 | sect->filepos = note->descpos; | |
9088 | sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32; | |
9089 | ||
9090 | return TRUE; | |
9091 | } | |
9092 | ||
9093 | if (note->type == NT_OPENBSD_WCOOKIE) | |
9094 | { | |
9095 | asection *sect = bfd_make_section_anyway_with_flags (abfd, ".wcookie", | |
9096 | SEC_HAS_CONTENTS); | |
9097 | ||
9098 | if (sect == NULL) | |
9099 | return FALSE; | |
9100 | sect->size = note->descsz; | |
9101 | sect->filepos = note->descpos; | |
9102 | sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32; | |
9103 | ||
9104 | return TRUE; | |
9105 | } | |
9106 | ||
9107 | return TRUE; | |
9108 | } | |
9109 | ||
07c6e936 | 9110 | static bfd_boolean |
d3fd4074 | 9111 | elfcore_grok_nto_status (bfd *abfd, Elf_Internal_Note *note, long *tid) |
07c6e936 NC |
9112 | { |
9113 | void *ddata = note->descdata; | |
9114 | char buf[100]; | |
9115 | char *name; | |
9116 | asection *sect; | |
f8843e87 AM |
9117 | short sig; |
9118 | unsigned flags; | |
07c6e936 NC |
9119 | |
9120 | /* nto_procfs_status 'pid' field is at offset 0. */ | |
228e534f | 9121 | elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, (bfd_byte *) ddata); |
07c6e936 | 9122 | |
f8843e87 AM |
9123 | /* nto_procfs_status 'tid' field is at offset 4. Pass it back. */ |
9124 | *tid = bfd_get_32 (abfd, (bfd_byte *) ddata + 4); | |
9125 | ||
9126 | /* nto_procfs_status 'flags' field is at offset 8. */ | |
9127 | flags = bfd_get_32 (abfd, (bfd_byte *) ddata + 8); | |
07c6e936 NC |
9128 | |
9129 | /* nto_procfs_status 'what' field is at offset 14. */ | |
f8843e87 AM |
9130 | if ((sig = bfd_get_16 (abfd, (bfd_byte *) ddata + 14)) > 0) |
9131 | { | |
228e534f AM |
9132 | elf_tdata (abfd)->core->signal = sig; |
9133 | elf_tdata (abfd)->core->lwpid = *tid; | |
f8843e87 | 9134 | } |
07c6e936 | 9135 | |
f8843e87 AM |
9136 | /* _DEBUG_FLAG_CURTID (current thread) is 0x80. Some cores |
9137 | do not come from signals so we make sure we set the current | |
9138 | thread just in case. */ | |
9139 | if (flags & 0x00000080) | |
228e534f | 9140 | elf_tdata (abfd)->core->lwpid = *tid; |
07c6e936 NC |
9141 | |
9142 | /* Make a ".qnx_core_status/%d" section. */ | |
d3fd4074 | 9143 | sprintf (buf, ".qnx_core_status/%ld", *tid); |
07c6e936 | 9144 | |
a50b1753 | 9145 | name = (char *) bfd_alloc (abfd, strlen (buf) + 1); |
07c6e936 NC |
9146 | if (name == NULL) |
9147 | return FALSE; | |
9148 | strcpy (name, buf); | |
9149 | ||
117ed4f8 | 9150 | sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); |
07c6e936 NC |
9151 | if (sect == NULL) |
9152 | return FALSE; | |
9153 | ||
eea6121a | 9154 | sect->size = note->descsz; |
07c6e936 | 9155 | sect->filepos = note->descpos; |
07c6e936 NC |
9156 | sect->alignment_power = 2; |
9157 | ||
9158 | return (elfcore_maybe_make_sect (abfd, ".qnx_core_status", sect)); | |
9159 | } | |
9160 | ||
9161 | static bfd_boolean | |
d69f560c KW |
9162 | elfcore_grok_nto_regs (bfd *abfd, |
9163 | Elf_Internal_Note *note, | |
d3fd4074 | 9164 | long tid, |
d69f560c | 9165 | char *base) |
07c6e936 NC |
9166 | { |
9167 | char buf[100]; | |
9168 | char *name; | |
9169 | asection *sect; | |
9170 | ||
d69f560c | 9171 | /* Make a "(base)/%d" section. */ |
d3fd4074 | 9172 | sprintf (buf, "%s/%ld", base, tid); |
07c6e936 | 9173 | |
a50b1753 | 9174 | name = (char *) bfd_alloc (abfd, strlen (buf) + 1); |
07c6e936 NC |
9175 | if (name == NULL) |
9176 | return FALSE; | |
9177 | strcpy (name, buf); | |
9178 | ||
117ed4f8 | 9179 | sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); |
07c6e936 NC |
9180 | if (sect == NULL) |
9181 | return FALSE; | |
9182 | ||
eea6121a | 9183 | sect->size = note->descsz; |
07c6e936 | 9184 | sect->filepos = note->descpos; |
07c6e936 NC |
9185 | sect->alignment_power = 2; |
9186 | ||
f8843e87 | 9187 | /* This is the current thread. */ |
228e534f | 9188 | if (elf_tdata (abfd)->core->lwpid == tid) |
d69f560c | 9189 | return elfcore_maybe_make_sect (abfd, base, sect); |
f8843e87 AM |
9190 | |
9191 | return TRUE; | |
07c6e936 NC |
9192 | } |
9193 | ||
9194 | #define BFD_QNT_CORE_INFO 7 | |
9195 | #define BFD_QNT_CORE_STATUS 8 | |
9196 | #define BFD_QNT_CORE_GREG 9 | |
9197 | #define BFD_QNT_CORE_FPREG 10 | |
9198 | ||
9199 | static bfd_boolean | |
217aa764 | 9200 | elfcore_grok_nto_note (bfd *abfd, Elf_Internal_Note *note) |
07c6e936 NC |
9201 | { |
9202 | /* Every GREG section has a STATUS section before it. Store the | |
811072d8 | 9203 | tid from the previous call to pass down to the next gregs |
07c6e936 | 9204 | function. */ |
d3fd4074 | 9205 | static long tid = 1; |
07c6e936 NC |
9206 | |
9207 | switch (note->type) | |
9208 | { | |
d69f560c KW |
9209 | case BFD_QNT_CORE_INFO: |
9210 | return elfcore_make_note_pseudosection (abfd, ".qnx_core_info", note); | |
9211 | case BFD_QNT_CORE_STATUS: | |
9212 | return elfcore_grok_nto_status (abfd, note, &tid); | |
9213 | case BFD_QNT_CORE_GREG: | |
9214 | return elfcore_grok_nto_regs (abfd, note, tid, ".reg"); | |
9215 | case BFD_QNT_CORE_FPREG: | |
9216 | return elfcore_grok_nto_regs (abfd, note, tid, ".reg2"); | |
9217 | default: | |
9218 | return TRUE; | |
07c6e936 NC |
9219 | } |
9220 | } | |
9221 | ||
b15fa79e AM |
9222 | static bfd_boolean |
9223 | elfcore_grok_spu_note (bfd *abfd, Elf_Internal_Note *note) | |
9224 | { | |
9225 | char *name; | |
9226 | asection *sect; | |
9227 | size_t len; | |
9228 | ||
9229 | /* Use note name as section name. */ | |
9230 | len = note->namesz; | |
a50b1753 | 9231 | name = (char *) bfd_alloc (abfd, len); |
b15fa79e AM |
9232 | if (name == NULL) |
9233 | return FALSE; | |
9234 | memcpy (name, note->namedata, len); | |
9235 | name[len - 1] = '\0'; | |
9236 | ||
9237 | sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); | |
9238 | if (sect == NULL) | |
9239 | return FALSE; | |
9240 | ||
9241 | sect->size = note->descsz; | |
9242 | sect->filepos = note->descpos; | |
9243 | sect->alignment_power = 1; | |
9244 | ||
9245 | return TRUE; | |
9246 | } | |
9247 | ||
7c76fa91 MS |
9248 | /* Function: elfcore_write_note |
9249 | ||
47d9a591 | 9250 | Inputs: |
a39f3346 | 9251 | buffer to hold note, and current size of buffer |
7c76fa91 MS |
9252 | name of note |
9253 | type of note | |
9254 | data for note | |
9255 | size of data for note | |
9256 | ||
a39f3346 AM |
9257 | Writes note to end of buffer. ELF64 notes are written exactly as |
9258 | for ELF32, despite the current (as of 2006) ELF gabi specifying | |
9259 | that they ought to have 8-byte namesz and descsz field, and have | |
9260 | 8-byte alignment. Other writers, eg. Linux kernel, do the same. | |
9261 | ||
7c76fa91 | 9262 | Return: |
a39f3346 | 9263 | Pointer to realloc'd buffer, *BUFSIZ updated. */ |
7c76fa91 MS |
9264 | |
9265 | char * | |
a39f3346 | 9266 | elfcore_write_note (bfd *abfd, |
217aa764 | 9267 | char *buf, |
a39f3346 | 9268 | int *bufsiz, |
217aa764 | 9269 | const char *name, |
a39f3346 | 9270 | int type, |
217aa764 | 9271 | const void *input, |
a39f3346 | 9272 | int size) |
7c76fa91 MS |
9273 | { |
9274 | Elf_External_Note *xnp; | |
d4c88bbb | 9275 | size_t namesz; |
d4c88bbb | 9276 | size_t newspace; |
a39f3346 | 9277 | char *dest; |
7c76fa91 | 9278 | |
d4c88bbb | 9279 | namesz = 0; |
d4c88bbb | 9280 | if (name != NULL) |
a39f3346 | 9281 | namesz = strlen (name) + 1; |
d4c88bbb | 9282 | |
a39f3346 | 9283 | newspace = 12 + ((namesz + 3) & -4) + ((size + 3) & -4); |
d4c88bbb | 9284 | |
a50b1753 | 9285 | buf = (char *) realloc (buf, *bufsiz + newspace); |
14b1c01e AM |
9286 | if (buf == NULL) |
9287 | return buf; | |
a39f3346 | 9288 | dest = buf + *bufsiz; |
7c76fa91 MS |
9289 | *bufsiz += newspace; |
9290 | xnp = (Elf_External_Note *) dest; | |
9291 | H_PUT_32 (abfd, namesz, xnp->namesz); | |
9292 | H_PUT_32 (abfd, size, xnp->descsz); | |
9293 | H_PUT_32 (abfd, type, xnp->type); | |
d4c88bbb AM |
9294 | dest = xnp->name; |
9295 | if (name != NULL) | |
9296 | { | |
9297 | memcpy (dest, name, namesz); | |
9298 | dest += namesz; | |
a39f3346 | 9299 | while (namesz & 3) |
d4c88bbb AM |
9300 | { |
9301 | *dest++ = '\0'; | |
a39f3346 | 9302 | ++namesz; |
d4c88bbb AM |
9303 | } |
9304 | } | |
9305 | memcpy (dest, input, size); | |
a39f3346 AM |
9306 | dest += size; |
9307 | while (size & 3) | |
9308 | { | |
9309 | *dest++ = '\0'; | |
9310 | ++size; | |
9311 | } | |
9312 | return buf; | |
7c76fa91 MS |
9313 | } |
9314 | ||
7c76fa91 | 9315 | char * |
217aa764 AM |
9316 | elfcore_write_prpsinfo (bfd *abfd, |
9317 | char *buf, | |
9318 | int *bufsiz, | |
9319 | const char *fname, | |
9320 | const char *psargs) | |
7c76fa91 | 9321 | { |
183e98be AM |
9322 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
9323 | ||
9324 | if (bed->elf_backend_write_core_note != NULL) | |
9325 | { | |
9326 | char *ret; | |
9327 | ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz, | |
9328 | NT_PRPSINFO, fname, psargs); | |
9329 | if (ret != NULL) | |
9330 | return ret; | |
9331 | } | |
7c76fa91 | 9332 | |
1f20dca5 | 9333 | #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) |
183e98be AM |
9334 | #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T) |
9335 | if (bed->s->elfclass == ELFCLASS32) | |
9336 | { | |
9337 | #if defined (HAVE_PSINFO32_T) | |
9338 | psinfo32_t data; | |
9339 | int note_type = NT_PSINFO; | |
9340 | #else | |
9341 | prpsinfo32_t data; | |
9342 | int note_type = NT_PRPSINFO; | |
9343 | #endif | |
9344 | ||
9345 | memset (&data, 0, sizeof (data)); | |
9346 | strncpy (data.pr_fname, fname, sizeof (data.pr_fname)); | |
9347 | strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs)); | |
9348 | return elfcore_write_note (abfd, buf, bufsiz, | |
1f20dca5 | 9349 | "CORE", note_type, &data, sizeof (data)); |
183e98be AM |
9350 | } |
9351 | else | |
9352 | #endif | |
9353 | { | |
7c76fa91 | 9354 | #if defined (HAVE_PSINFO_T) |
183e98be AM |
9355 | psinfo_t data; |
9356 | int note_type = NT_PSINFO; | |
7c76fa91 | 9357 | #else |
183e98be AM |
9358 | prpsinfo_t data; |
9359 | int note_type = NT_PRPSINFO; | |
7c76fa91 MS |
9360 | #endif |
9361 | ||
183e98be AM |
9362 | memset (&data, 0, sizeof (data)); |
9363 | strncpy (data.pr_fname, fname, sizeof (data.pr_fname)); | |
9364 | strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs)); | |
9365 | return elfcore_write_note (abfd, buf, bufsiz, | |
1f20dca5 | 9366 | "CORE", note_type, &data, sizeof (data)); |
183e98be | 9367 | } |
7c76fa91 MS |
9368 | #endif /* PSINFO_T or PRPSINFO_T */ |
9369 | ||
1f20dca5 UW |
9370 | free (buf); |
9371 | return NULL; | |
9372 | } | |
9373 | ||
70a38d42 SDJ |
9374 | char * |
9375 | elfcore_write_linux_prpsinfo32 | |
9376 | (bfd *abfd, char *buf, int *bufsiz, | |
9377 | const struct elf_internal_linux_prpsinfo *prpsinfo) | |
9378 | { | |
9379 | struct elf_external_linux_prpsinfo32 data; | |
9380 | ||
9381 | memset (&data, 0, sizeof (data)); | |
9382 | LINUX_PRPSINFO32_SWAP_FIELDS (abfd, prpsinfo, data); | |
9383 | ||
9384 | return elfcore_write_note (abfd, buf, bufsiz, "CORE", NT_PRPSINFO, | |
9385 | &data, sizeof (data)); | |
9386 | } | |
9387 | ||
9388 | char * | |
9389 | elfcore_write_linux_prpsinfo64 | |
9390 | (bfd *abfd, char *buf, int *bufsiz, | |
9391 | const struct elf_internal_linux_prpsinfo *prpsinfo) | |
9392 | { | |
9393 | struct elf_external_linux_prpsinfo64 data; | |
9394 | ||
9395 | memset (&data, 0, sizeof (data)); | |
9396 | LINUX_PRPSINFO64_SWAP_FIELDS (abfd, prpsinfo, data); | |
9397 | ||
9398 | return elfcore_write_note (abfd, buf, bufsiz, | |
9399 | "CORE", NT_PRPSINFO, &data, sizeof (data)); | |
9400 | } | |
9401 | ||
7c76fa91 | 9402 | char * |
217aa764 AM |
9403 | elfcore_write_prstatus (bfd *abfd, |
9404 | char *buf, | |
9405 | int *bufsiz, | |
9406 | long pid, | |
9407 | int cursig, | |
9408 | const void *gregs) | |
7c76fa91 | 9409 | { |
183e98be | 9410 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
7c76fa91 | 9411 | |
183e98be AM |
9412 | if (bed->elf_backend_write_core_note != NULL) |
9413 | { | |
9414 | char *ret; | |
9415 | ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz, | |
9416 | NT_PRSTATUS, | |
9417 | pid, cursig, gregs); | |
9418 | if (ret != NULL) | |
9419 | return ret; | |
9420 | } | |
9421 | ||
1f20dca5 | 9422 | #if defined (HAVE_PRSTATUS_T) |
183e98be AM |
9423 | #if defined (HAVE_PRSTATUS32_T) |
9424 | if (bed->s->elfclass == ELFCLASS32) | |
9425 | { | |
9426 | prstatus32_t prstat; | |
9427 | ||
9428 | memset (&prstat, 0, sizeof (prstat)); | |
9429 | prstat.pr_pid = pid; | |
9430 | prstat.pr_cursig = cursig; | |
9431 | memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg)); | |
1f20dca5 | 9432 | return elfcore_write_note (abfd, buf, bufsiz, "CORE", |
183e98be AM |
9433 | NT_PRSTATUS, &prstat, sizeof (prstat)); |
9434 | } | |
9435 | else | |
9436 | #endif | |
9437 | { | |
9438 | prstatus_t prstat; | |
9439 | ||
9440 | memset (&prstat, 0, sizeof (prstat)); | |
9441 | prstat.pr_pid = pid; | |
9442 | prstat.pr_cursig = cursig; | |
9443 | memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg)); | |
1f20dca5 | 9444 | return elfcore_write_note (abfd, buf, bufsiz, "CORE", |
183e98be AM |
9445 | NT_PRSTATUS, &prstat, sizeof (prstat)); |
9446 | } | |
7c76fa91 MS |
9447 | #endif /* HAVE_PRSTATUS_T */ |
9448 | ||
1f20dca5 UW |
9449 | free (buf); |
9450 | return NULL; | |
9451 | } | |
9452 | ||
51316059 MS |
9453 | #if defined (HAVE_LWPSTATUS_T) |
9454 | char * | |
217aa764 AM |
9455 | elfcore_write_lwpstatus (bfd *abfd, |
9456 | char *buf, | |
9457 | int *bufsiz, | |
9458 | long pid, | |
9459 | int cursig, | |
9460 | const void *gregs) | |
51316059 MS |
9461 | { |
9462 | lwpstatus_t lwpstat; | |
183e98be | 9463 | const char *note_name = "CORE"; |
51316059 MS |
9464 | |
9465 | memset (&lwpstat, 0, sizeof (lwpstat)); | |
9466 | lwpstat.pr_lwpid = pid >> 16; | |
9467 | lwpstat.pr_cursig = cursig; | |
9468 | #if defined (HAVE_LWPSTATUS_T_PR_REG) | |
d1e8523e | 9469 | memcpy (&lwpstat.pr_reg, gregs, sizeof (lwpstat.pr_reg)); |
51316059 MS |
9470 | #elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT) |
9471 | #if !defined(gregs) | |
9472 | memcpy (lwpstat.pr_context.uc_mcontext.gregs, | |
9473 | gregs, sizeof (lwpstat.pr_context.uc_mcontext.gregs)); | |
9474 | #else | |
9475 | memcpy (lwpstat.pr_context.uc_mcontext.__gregs, | |
9476 | gregs, sizeof (lwpstat.pr_context.uc_mcontext.__gregs)); | |
9477 | #endif | |
9478 | #endif | |
47d9a591 | 9479 | return elfcore_write_note (abfd, buf, bufsiz, note_name, |
51316059 MS |
9480 | NT_LWPSTATUS, &lwpstat, sizeof (lwpstat)); |
9481 | } | |
9482 | #endif /* HAVE_LWPSTATUS_T */ | |
9483 | ||
7c76fa91 MS |
9484 | #if defined (HAVE_PSTATUS_T) |
9485 | char * | |
217aa764 AM |
9486 | elfcore_write_pstatus (bfd *abfd, |
9487 | char *buf, | |
9488 | int *bufsiz, | |
9489 | long pid, | |
6c10990d NC |
9490 | int cursig ATTRIBUTE_UNUSED, |
9491 | const void *gregs ATTRIBUTE_UNUSED) | |
7c76fa91 | 9492 | { |
183e98be AM |
9493 | const char *note_name = "CORE"; |
9494 | #if defined (HAVE_PSTATUS32_T) | |
9495 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
7c76fa91 | 9496 | |
183e98be AM |
9497 | if (bed->s->elfclass == ELFCLASS32) |
9498 | { | |
9499 | pstatus32_t pstat; | |
9500 | ||
9501 | memset (&pstat, 0, sizeof (pstat)); | |
9502 | pstat.pr_pid = pid & 0xffff; | |
9503 | buf = elfcore_write_note (abfd, buf, bufsiz, note_name, | |
9504 | NT_PSTATUS, &pstat, sizeof (pstat)); | |
9505 | return buf; | |
9506 | } | |
9507 | else | |
9508 | #endif | |
9509 | { | |
9510 | pstatus_t pstat; | |
9511 | ||
9512 | memset (&pstat, 0, sizeof (pstat)); | |
9513 | pstat.pr_pid = pid & 0xffff; | |
9514 | buf = elfcore_write_note (abfd, buf, bufsiz, note_name, | |
9515 | NT_PSTATUS, &pstat, sizeof (pstat)); | |
9516 | return buf; | |
9517 | } | |
7c76fa91 MS |
9518 | } |
9519 | #endif /* HAVE_PSTATUS_T */ | |
9520 | ||
9521 | char * | |
217aa764 AM |
9522 | elfcore_write_prfpreg (bfd *abfd, |
9523 | char *buf, | |
9524 | int *bufsiz, | |
9525 | const void *fpregs, | |
9526 | int size) | |
7c76fa91 | 9527 | { |
183e98be | 9528 | const char *note_name = "CORE"; |
47d9a591 | 9529 | return elfcore_write_note (abfd, buf, bufsiz, |
7c76fa91 MS |
9530 | note_name, NT_FPREGSET, fpregs, size); |
9531 | } | |
9532 | ||
9533 | char * | |
217aa764 AM |
9534 | elfcore_write_prxfpreg (bfd *abfd, |
9535 | char *buf, | |
9536 | int *bufsiz, | |
9537 | const void *xfpregs, | |
9538 | int size) | |
7c76fa91 MS |
9539 | { |
9540 | char *note_name = "LINUX"; | |
47d9a591 | 9541 | return elfcore_write_note (abfd, buf, bufsiz, |
7c76fa91 MS |
9542 | note_name, NT_PRXFPREG, xfpregs, size); |
9543 | } | |
9544 | ||
4339cae0 L |
9545 | char * |
9546 | elfcore_write_xstatereg (bfd *abfd, char *buf, int *bufsiz, | |
9547 | const void *xfpregs, int size) | |
9548 | { | |
9549 | char *note_name = "LINUX"; | |
9550 | return elfcore_write_note (abfd, buf, bufsiz, | |
9551 | note_name, NT_X86_XSTATE, xfpregs, size); | |
9552 | } | |
9553 | ||
97753bd5 AM |
9554 | char * |
9555 | elfcore_write_ppc_vmx (bfd *abfd, | |
9556 | char *buf, | |
9557 | int *bufsiz, | |
9558 | const void *ppc_vmx, | |
9559 | int size) | |
9560 | { | |
9561 | char *note_name = "LINUX"; | |
9562 | return elfcore_write_note (abfd, buf, bufsiz, | |
9563 | note_name, NT_PPC_VMX, ppc_vmx, size); | |
9564 | } | |
9565 | ||
89eeb0bc LM |
9566 | char * |
9567 | elfcore_write_ppc_vsx (bfd *abfd, | |
9568 | char *buf, | |
9569 | int *bufsiz, | |
9570 | const void *ppc_vsx, | |
9571 | int size) | |
9572 | { | |
9573 | char *note_name = "LINUX"; | |
9574 | return elfcore_write_note (abfd, buf, bufsiz, | |
9575 | note_name, NT_PPC_VSX, ppc_vsx, size); | |
9576 | } | |
9577 | ||
0675e188 UW |
9578 | static char * |
9579 | elfcore_write_s390_high_gprs (bfd *abfd, | |
9580 | char *buf, | |
9581 | int *bufsiz, | |
9582 | const void *s390_high_gprs, | |
9583 | int size) | |
9584 | { | |
9585 | char *note_name = "LINUX"; | |
9586 | return elfcore_write_note (abfd, buf, bufsiz, | |
9587 | note_name, NT_S390_HIGH_GPRS, | |
9588 | s390_high_gprs, size); | |
9589 | } | |
9590 | ||
d7eeb400 MS |
9591 | char * |
9592 | elfcore_write_s390_timer (bfd *abfd, | |
9593 | char *buf, | |
9594 | int *bufsiz, | |
9595 | const void *s390_timer, | |
9596 | int size) | |
9597 | { | |
9598 | char *note_name = "LINUX"; | |
9599 | return elfcore_write_note (abfd, buf, bufsiz, | |
9600 | note_name, NT_S390_TIMER, s390_timer, size); | |
9601 | } | |
9602 | ||
9603 | char * | |
9604 | elfcore_write_s390_todcmp (bfd *abfd, | |
9605 | char *buf, | |
9606 | int *bufsiz, | |
9607 | const void *s390_todcmp, | |
9608 | int size) | |
9609 | { | |
9610 | char *note_name = "LINUX"; | |
9611 | return elfcore_write_note (abfd, buf, bufsiz, | |
9612 | note_name, NT_S390_TODCMP, s390_todcmp, size); | |
9613 | } | |
9614 | ||
9615 | char * | |
9616 | elfcore_write_s390_todpreg (bfd *abfd, | |
9617 | char *buf, | |
9618 | int *bufsiz, | |
9619 | const void *s390_todpreg, | |
9620 | int size) | |
9621 | { | |
9622 | char *note_name = "LINUX"; | |
9623 | return elfcore_write_note (abfd, buf, bufsiz, | |
9624 | note_name, NT_S390_TODPREG, s390_todpreg, size); | |
9625 | } | |
9626 | ||
9627 | char * | |
9628 | elfcore_write_s390_ctrs (bfd *abfd, | |
9629 | char *buf, | |
9630 | int *bufsiz, | |
9631 | const void *s390_ctrs, | |
9632 | int size) | |
9633 | { | |
9634 | char *note_name = "LINUX"; | |
9635 | return elfcore_write_note (abfd, buf, bufsiz, | |
9636 | note_name, NT_S390_CTRS, s390_ctrs, size); | |
9637 | } | |
9638 | ||
9639 | char * | |
9640 | elfcore_write_s390_prefix (bfd *abfd, | |
9641 | char *buf, | |
9642 | int *bufsiz, | |
9643 | const void *s390_prefix, | |
9644 | int size) | |
9645 | { | |
9646 | char *note_name = "LINUX"; | |
9647 | return elfcore_write_note (abfd, buf, bufsiz, | |
9648 | note_name, NT_S390_PREFIX, s390_prefix, size); | |
9649 | } | |
9650 | ||
355b81d9 UW |
9651 | char * |
9652 | elfcore_write_s390_last_break (bfd *abfd, | |
9653 | char *buf, | |
9654 | int *bufsiz, | |
9655 | const void *s390_last_break, | |
9656 | int size) | |
9657 | { | |
9658 | char *note_name = "LINUX"; | |
9659 | return elfcore_write_note (abfd, buf, bufsiz, | |
9660 | note_name, NT_S390_LAST_BREAK, | |
9661 | s390_last_break, size); | |
9662 | } | |
9663 | ||
9664 | char * | |
9665 | elfcore_write_s390_system_call (bfd *abfd, | |
9666 | char *buf, | |
9667 | int *bufsiz, | |
9668 | const void *s390_system_call, | |
9669 | int size) | |
9670 | { | |
9671 | char *note_name = "LINUX"; | |
9672 | return elfcore_write_note (abfd, buf, bufsiz, | |
9673 | note_name, NT_S390_SYSTEM_CALL, | |
9674 | s390_system_call, size); | |
9675 | } | |
9676 | ||
abb3f6cc NC |
9677 | char * |
9678 | elfcore_write_s390_tdb (bfd *abfd, | |
9679 | char *buf, | |
9680 | int *bufsiz, | |
9681 | const void *s390_tdb, | |
9682 | int size) | |
9683 | { | |
9684 | char *note_name = "LINUX"; | |
9685 | return elfcore_write_note (abfd, buf, bufsiz, | |
9686 | note_name, NT_S390_TDB, s390_tdb, size); | |
9687 | } | |
9688 | ||
faa9a424 UW |
9689 | char * |
9690 | elfcore_write_arm_vfp (bfd *abfd, | |
9691 | char *buf, | |
9692 | int *bufsiz, | |
9693 | const void *arm_vfp, | |
9694 | int size) | |
9695 | { | |
9696 | char *note_name = "LINUX"; | |
9697 | return elfcore_write_note (abfd, buf, bufsiz, | |
9698 | note_name, NT_ARM_VFP, arm_vfp, size); | |
9699 | } | |
9700 | ||
652451f8 YZ |
9701 | char * |
9702 | elfcore_write_aarch_tls (bfd *abfd, | |
9703 | char *buf, | |
9704 | int *bufsiz, | |
9705 | const void *aarch_tls, | |
9706 | int size) | |
9707 | { | |
9708 | char *note_name = "LINUX"; | |
9709 | return elfcore_write_note (abfd, buf, bufsiz, | |
9710 | note_name, NT_ARM_TLS, aarch_tls, size); | |
9711 | } | |
9712 | ||
9713 | char * | |
9714 | elfcore_write_aarch_hw_break (bfd *abfd, | |
9715 | char *buf, | |
9716 | int *bufsiz, | |
9717 | const void *aarch_hw_break, | |
9718 | int size) | |
9719 | { | |
9720 | char *note_name = "LINUX"; | |
9721 | return elfcore_write_note (abfd, buf, bufsiz, | |
9722 | note_name, NT_ARM_HW_BREAK, aarch_hw_break, size); | |
9723 | } | |
9724 | ||
9725 | char * | |
9726 | elfcore_write_aarch_hw_watch (bfd *abfd, | |
9727 | char *buf, | |
9728 | int *bufsiz, | |
9729 | const void *aarch_hw_watch, | |
9730 | int size) | |
9731 | { | |
9732 | char *note_name = "LINUX"; | |
9733 | return elfcore_write_note (abfd, buf, bufsiz, | |
9734 | note_name, NT_ARM_HW_WATCH, aarch_hw_watch, size); | |
9735 | } | |
9736 | ||
bb864ac1 CES |
9737 | char * |
9738 | elfcore_write_register_note (bfd *abfd, | |
9739 | char *buf, | |
9740 | int *bufsiz, | |
9741 | const char *section, | |
9742 | const void *data, | |
9743 | int size) | |
9744 | { | |
9745 | if (strcmp (section, ".reg2") == 0) | |
9746 | return elfcore_write_prfpreg (abfd, buf, bufsiz, data, size); | |
9747 | if (strcmp (section, ".reg-xfp") == 0) | |
9748 | return elfcore_write_prxfpreg (abfd, buf, bufsiz, data, size); | |
4339cae0 L |
9749 | if (strcmp (section, ".reg-xstate") == 0) |
9750 | return elfcore_write_xstatereg (abfd, buf, bufsiz, data, size); | |
bb864ac1 CES |
9751 | if (strcmp (section, ".reg-ppc-vmx") == 0) |
9752 | return elfcore_write_ppc_vmx (abfd, buf, bufsiz, data, size); | |
89eeb0bc LM |
9753 | if (strcmp (section, ".reg-ppc-vsx") == 0) |
9754 | return elfcore_write_ppc_vsx (abfd, buf, bufsiz, data, size); | |
0675e188 UW |
9755 | if (strcmp (section, ".reg-s390-high-gprs") == 0) |
9756 | return elfcore_write_s390_high_gprs (abfd, buf, bufsiz, data, size); | |
d7eeb400 MS |
9757 | if (strcmp (section, ".reg-s390-timer") == 0) |
9758 | return elfcore_write_s390_timer (abfd, buf, bufsiz, data, size); | |
9759 | if (strcmp (section, ".reg-s390-todcmp") == 0) | |
9760 | return elfcore_write_s390_todcmp (abfd, buf, bufsiz, data, size); | |
9761 | if (strcmp (section, ".reg-s390-todpreg") == 0) | |
9762 | return elfcore_write_s390_todpreg (abfd, buf, bufsiz, data, size); | |
9763 | if (strcmp (section, ".reg-s390-ctrs") == 0) | |
9764 | return elfcore_write_s390_ctrs (abfd, buf, bufsiz, data, size); | |
9765 | if (strcmp (section, ".reg-s390-prefix") == 0) | |
9766 | return elfcore_write_s390_prefix (abfd, buf, bufsiz, data, size); | |
355b81d9 UW |
9767 | if (strcmp (section, ".reg-s390-last-break") == 0) |
9768 | return elfcore_write_s390_last_break (abfd, buf, bufsiz, data, size); | |
9769 | if (strcmp (section, ".reg-s390-system-call") == 0) | |
9770 | return elfcore_write_s390_system_call (abfd, buf, bufsiz, data, size); | |
abb3f6cc NC |
9771 | if (strcmp (section, ".reg-s390-tdb") == 0) |
9772 | return elfcore_write_s390_tdb (abfd, buf, bufsiz, data, size); | |
faa9a424 UW |
9773 | if (strcmp (section, ".reg-arm-vfp") == 0) |
9774 | return elfcore_write_arm_vfp (abfd, buf, bufsiz, data, size); | |
652451f8 YZ |
9775 | if (strcmp (section, ".reg-aarch-tls") == 0) |
9776 | return elfcore_write_aarch_tls (abfd, buf, bufsiz, data, size); | |
9777 | if (strcmp (section, ".reg-aarch-hw-break") == 0) | |
9778 | return elfcore_write_aarch_hw_break (abfd, buf, bufsiz, data, size); | |
9779 | if (strcmp (section, ".reg-aarch-hw-watch") == 0) | |
9780 | return elfcore_write_aarch_hw_watch (abfd, buf, bufsiz, data, size); | |
bb864ac1 CES |
9781 | return NULL; |
9782 | } | |
9783 | ||
b34976b6 | 9784 | static bfd_boolean |
718175fa | 9785 | elf_parse_notes (bfd *abfd, char *buf, size_t size, file_ptr offset) |
252b5132 | 9786 | { |
c044fabd | 9787 | char *p; |
252b5132 | 9788 | |
252b5132 RH |
9789 | p = buf; |
9790 | while (p < buf + size) | |
9791 | { | |
c044fabd KH |
9792 | /* FIXME: bad alignment assumption. */ |
9793 | Elf_External_Note *xnp = (Elf_External_Note *) p; | |
252b5132 RH |
9794 | Elf_Internal_Note in; |
9795 | ||
baea7ef1 AM |
9796 | if (offsetof (Elf_External_Note, name) > buf - p + size) |
9797 | return FALSE; | |
9798 | ||
dc810e39 | 9799 | in.type = H_GET_32 (abfd, xnp->type); |
252b5132 | 9800 | |
dc810e39 | 9801 | in.namesz = H_GET_32 (abfd, xnp->namesz); |
252b5132 | 9802 | in.namedata = xnp->name; |
baea7ef1 AM |
9803 | if (in.namesz > buf - in.namedata + size) |
9804 | return FALSE; | |
252b5132 | 9805 | |
dc810e39 | 9806 | in.descsz = H_GET_32 (abfd, xnp->descsz); |
252b5132 RH |
9807 | in.descdata = in.namedata + BFD_ALIGN (in.namesz, 4); |
9808 | in.descpos = offset + (in.descdata - buf); | |
baea7ef1 AM |
9809 | if (in.descsz != 0 |
9810 | && (in.descdata >= buf + size | |
9811 | || in.descsz > buf - in.descdata + size)) | |
9812 | return FALSE; | |
252b5132 | 9813 | |
718175fa JK |
9814 | switch (bfd_get_format (abfd)) |
9815 | { | |
9816 | default: | |
9817 | return TRUE; | |
9818 | ||
9819 | case bfd_core: | |
f64e188b NC |
9820 | { |
9821 | struct | |
718175fa | 9822 | { |
f64e188b NC |
9823 | const char * string; |
9824 | bfd_boolean (* func)(bfd *, Elf_Internal_Note *); | |
718175fa | 9825 | } |
f64e188b | 9826 | grokers[] = |
b15fa79e | 9827 | { |
f64e188b NC |
9828 | { "", elfcore_grok_note }, |
9829 | { "NetBSD-CORE", elfcore_grok_netbsd_note }, | |
9830 | { "OpenBSD", elfcore_grok_openbsd_note }, | |
9831 | { "QNX", elfcore_grok_nto_note }, | |
9832 | { "SPU/", elfcore_grok_spu_note } | |
9833 | }; | |
9834 | int i; | |
9835 | ||
9836 | for (i = ARRAY_SIZE (grokers); i--;) | |
9837 | if (in.namesz >= sizeof grokers[i].string - 1 | |
9838 | && strncmp (in.namedata, grokers[i].string, | |
9839 | sizeof (grokers[i].string) - 1) == 0) | |
9840 | { | |
9841 | if (! grokers[i].func (abfd, & in)) | |
9842 | return FALSE; | |
9843 | break; | |
9844 | } | |
9845 | break; | |
9846 | } | |
718175fa JK |
9847 | |
9848 | case bfd_object: | |
9849 | if (in.namesz == sizeof "GNU" && strcmp (in.namedata, "GNU") == 0) | |
9850 | { | |
9851 | if (! elfobj_grok_gnu_note (abfd, &in)) | |
9852 | return FALSE; | |
9853 | } | |
e21e5835 NC |
9854 | else if (in.namesz == sizeof "stapsdt" |
9855 | && strcmp (in.namedata, "stapsdt") == 0) | |
9856 | { | |
9857 | if (! elfobj_grok_stapsdt_note (abfd, &in)) | |
9858 | return FALSE; | |
9859 | } | |
718175fa | 9860 | break; |
08a40648 | 9861 | } |
252b5132 RH |
9862 | |
9863 | p = in.descdata + BFD_ALIGN (in.descsz, 4); | |
9864 | } | |
9865 | ||
718175fa JK |
9866 | return TRUE; |
9867 | } | |
9868 | ||
9869 | static bfd_boolean | |
9870 | elf_read_notes (bfd *abfd, file_ptr offset, bfd_size_type size) | |
9871 | { | |
9872 | char *buf; | |
9873 | ||
9874 | if (size <= 0) | |
9875 | return TRUE; | |
9876 | ||
9877 | if (bfd_seek (abfd, offset, SEEK_SET) != 0) | |
9878 | return FALSE; | |
9879 | ||
f64e188b | 9880 | buf = (char *) bfd_malloc (size + 1); |
718175fa JK |
9881 | if (buf == NULL) |
9882 | return FALSE; | |
9883 | ||
f64e188b NC |
9884 | /* PR 17512: file: ec08f814 |
9885 | 0-termintate the buffer so that string searches will not overflow. */ | |
9886 | buf[size] = 0; | |
9887 | ||
718175fa JK |
9888 | if (bfd_bread (buf, size, abfd) != size |
9889 | || !elf_parse_notes (abfd, buf, size, offset)) | |
9890 | { | |
9891 | free (buf); | |
9892 | return FALSE; | |
9893 | } | |
9894 | ||
252b5132 | 9895 | free (buf); |
b34976b6 | 9896 | return TRUE; |
252b5132 | 9897 | } |
98d8431c JB |
9898 | \f |
9899 | /* Providing external access to the ELF program header table. */ | |
9900 | ||
9901 | /* Return an upper bound on the number of bytes required to store a | |
9902 | copy of ABFD's program header table entries. Return -1 if an error | |
9903 | occurs; bfd_get_error will return an appropriate code. */ | |
c044fabd | 9904 | |
98d8431c | 9905 | long |
217aa764 | 9906 | bfd_get_elf_phdr_upper_bound (bfd *abfd) |
98d8431c JB |
9907 | { |
9908 | if (abfd->xvec->flavour != bfd_target_elf_flavour) | |
9909 | { | |
9910 | bfd_set_error (bfd_error_wrong_format); | |
9911 | return -1; | |
9912 | } | |
9913 | ||
936e320b | 9914 | return elf_elfheader (abfd)->e_phnum * sizeof (Elf_Internal_Phdr); |
98d8431c JB |
9915 | } |
9916 | ||
98d8431c JB |
9917 | /* Copy ABFD's program header table entries to *PHDRS. The entries |
9918 | will be stored as an array of Elf_Internal_Phdr structures, as | |
9919 | defined in include/elf/internal.h. To find out how large the | |
9920 | buffer needs to be, call bfd_get_elf_phdr_upper_bound. | |
9921 | ||
9922 | Return the number of program header table entries read, or -1 if an | |
9923 | error occurs; bfd_get_error will return an appropriate code. */ | |
c044fabd | 9924 | |
98d8431c | 9925 | int |
217aa764 | 9926 | bfd_get_elf_phdrs (bfd *abfd, void *phdrs) |
98d8431c JB |
9927 | { |
9928 | int num_phdrs; | |
9929 | ||
9930 | if (abfd->xvec->flavour != bfd_target_elf_flavour) | |
9931 | { | |
9932 | bfd_set_error (bfd_error_wrong_format); | |
9933 | return -1; | |
9934 | } | |
9935 | ||
9936 | num_phdrs = elf_elfheader (abfd)->e_phnum; | |
c044fabd | 9937 | memcpy (phdrs, elf_tdata (abfd)->phdr, |
98d8431c JB |
9938 | num_phdrs * sizeof (Elf_Internal_Phdr)); |
9939 | ||
9940 | return num_phdrs; | |
9941 | } | |
ae4221d7 | 9942 | |
db6751f2 | 9943 | enum elf_reloc_type_class |
7e612e98 AM |
9944 | _bfd_elf_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED, |
9945 | const asection *rel_sec ATTRIBUTE_UNUSED, | |
9946 | const Elf_Internal_Rela *rela ATTRIBUTE_UNUSED) | |
db6751f2 JJ |
9947 | { |
9948 | return reloc_class_normal; | |
9949 | } | |
f8df10f4 | 9950 | |
47d9a591 | 9951 | /* For RELA architectures, return the relocation value for a |
f8df10f4 JJ |
9952 | relocation against a local symbol. */ |
9953 | ||
9954 | bfd_vma | |
217aa764 AM |
9955 | _bfd_elf_rela_local_sym (bfd *abfd, |
9956 | Elf_Internal_Sym *sym, | |
8517fae7 | 9957 | asection **psec, |
217aa764 | 9958 | Elf_Internal_Rela *rel) |
f8df10f4 | 9959 | { |
8517fae7 | 9960 | asection *sec = *psec; |
f8df10f4 JJ |
9961 | bfd_vma relocation; |
9962 | ||
9963 | relocation = (sec->output_section->vma | |
9964 | + sec->output_offset | |
9965 | + sym->st_value); | |
9966 | if ((sec->flags & SEC_MERGE) | |
c629eae0 | 9967 | && ELF_ST_TYPE (sym->st_info) == STT_SECTION |
dbaa2011 | 9968 | && sec->sec_info_type == SEC_INFO_TYPE_MERGE) |
f8df10f4 | 9969 | { |
f8df10f4 | 9970 | rel->r_addend = |
8517fae7 | 9971 | _bfd_merged_section_offset (abfd, psec, |
65765700 | 9972 | elf_section_data (sec)->sec_info, |
753731ee AM |
9973 | sym->st_value + rel->r_addend); |
9974 | if (sec != *psec) | |
9975 | { | |
9976 | /* If we have changed the section, and our original section is | |
9977 | marked with SEC_EXCLUDE, it means that the original | |
9978 | SEC_MERGE section has been completely subsumed in some | |
9979 | other SEC_MERGE section. In this case, we need to leave | |
9980 | some info around for --emit-relocs. */ | |
9981 | if ((sec->flags & SEC_EXCLUDE) != 0) | |
9982 | sec->kept_section = *psec; | |
9983 | sec = *psec; | |
9984 | } | |
8517fae7 AM |
9985 | rel->r_addend -= relocation; |
9986 | rel->r_addend += sec->output_section->vma + sec->output_offset; | |
f8df10f4 JJ |
9987 | } |
9988 | return relocation; | |
9989 | } | |
c629eae0 JJ |
9990 | |
9991 | bfd_vma | |
217aa764 AM |
9992 | _bfd_elf_rel_local_sym (bfd *abfd, |
9993 | Elf_Internal_Sym *sym, | |
9994 | asection **psec, | |
9995 | bfd_vma addend) | |
47d9a591 | 9996 | { |
c629eae0 JJ |
9997 | asection *sec = *psec; |
9998 | ||
dbaa2011 | 9999 | if (sec->sec_info_type != SEC_INFO_TYPE_MERGE) |
c629eae0 JJ |
10000 | return sym->st_value + addend; |
10001 | ||
10002 | return _bfd_merged_section_offset (abfd, psec, | |
65765700 | 10003 | elf_section_data (sec)->sec_info, |
753731ee | 10004 | sym->st_value + addend); |
c629eae0 JJ |
10005 | } |
10006 | ||
10007 | bfd_vma | |
217aa764 | 10008 | _bfd_elf_section_offset (bfd *abfd, |
92e4ec35 | 10009 | struct bfd_link_info *info, |
217aa764 AM |
10010 | asection *sec, |
10011 | bfd_vma offset) | |
c629eae0 | 10012 | { |
68bfbfcc | 10013 | switch (sec->sec_info_type) |
65765700 | 10014 | { |
dbaa2011 | 10015 | case SEC_INFO_TYPE_STABS: |
eea6121a AM |
10016 | return _bfd_stab_section_offset (sec, elf_section_data (sec)->sec_info, |
10017 | offset); | |
dbaa2011 | 10018 | case SEC_INFO_TYPE_EH_FRAME: |
92e4ec35 | 10019 | return _bfd_elf_eh_frame_section_offset (abfd, info, sec, offset); |
65765700 | 10020 | default: |
310fd250 L |
10021 | if ((sec->flags & SEC_ELF_REVERSE_COPY) != 0) |
10022 | { | |
10023 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
10024 | bfd_size_type address_size = bed->s->arch_size / 8; | |
10025 | offset = sec->size - offset - address_size; | |
10026 | } | |
65765700 JJ |
10027 | return offset; |
10028 | } | |
c629eae0 | 10029 | } |
3333a7c3 RM |
10030 | \f |
10031 | /* Create a new BFD as if by bfd_openr. Rather than opening a file, | |
10032 | reconstruct an ELF file by reading the segments out of remote memory | |
10033 | based on the ELF file header at EHDR_VMA and the ELF program headers it | |
10034 | points to. If not null, *LOADBASEP is filled in with the difference | |
10035 | between the VMAs from which the segments were read, and the VMAs the | |
10036 | file headers (and hence BFD's idea of each section's VMA) put them at. | |
10037 | ||
10038 | The function TARGET_READ_MEMORY is called to copy LEN bytes from the | |
10039 | remote memory at target address VMA into the local buffer at MYADDR; it | |
10040 | should return zero on success or an `errno' code on failure. TEMPL must | |
10041 | be a BFD for an ELF target with the word size and byte order found in | |
10042 | the remote memory. */ | |
10043 | ||
10044 | bfd * | |
217aa764 AM |
10045 | bfd_elf_bfd_from_remote_memory |
10046 | (bfd *templ, | |
10047 | bfd_vma ehdr_vma, | |
f0a5d95a | 10048 | bfd_size_type size, |
217aa764 | 10049 | bfd_vma *loadbasep, |
fe78531d | 10050 | int (*target_read_memory) (bfd_vma, bfd_byte *, bfd_size_type)) |
3333a7c3 RM |
10051 | { |
10052 | return (*get_elf_backend_data (templ)->elf_backend_bfd_from_remote_memory) | |
5979d6b6 | 10053 | (templ, ehdr_vma, size, loadbasep, target_read_memory); |
3333a7c3 | 10054 | } |
4c45e5c9 JJ |
10055 | \f |
10056 | long | |
c9727e01 AM |
10057 | _bfd_elf_get_synthetic_symtab (bfd *abfd, |
10058 | long symcount ATTRIBUTE_UNUSED, | |
10059 | asymbol **syms ATTRIBUTE_UNUSED, | |
8615f3f2 | 10060 | long dynsymcount, |
c9727e01 AM |
10061 | asymbol **dynsyms, |
10062 | asymbol **ret) | |
4c45e5c9 JJ |
10063 | { |
10064 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
10065 | asection *relplt; | |
10066 | asymbol *s; | |
10067 | const char *relplt_name; | |
10068 | bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean); | |
10069 | arelent *p; | |
10070 | long count, i, n; | |
10071 | size_t size; | |
10072 | Elf_Internal_Shdr *hdr; | |
10073 | char *names; | |
10074 | asection *plt; | |
10075 | ||
8615f3f2 AM |
10076 | *ret = NULL; |
10077 | ||
90e3cdf2 JJ |
10078 | if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0) |
10079 | return 0; | |
10080 | ||
8615f3f2 AM |
10081 | if (dynsymcount <= 0) |
10082 | return 0; | |
10083 | ||
4c45e5c9 JJ |
10084 | if (!bed->plt_sym_val) |
10085 | return 0; | |
10086 | ||
10087 | relplt_name = bed->relplt_name; | |
10088 | if (relplt_name == NULL) | |
d35fd659 | 10089 | relplt_name = bed->rela_plts_and_copies_p ? ".rela.plt" : ".rel.plt"; |
4c45e5c9 JJ |
10090 | relplt = bfd_get_section_by_name (abfd, relplt_name); |
10091 | if (relplt == NULL) | |
10092 | return 0; | |
10093 | ||
10094 | hdr = &elf_section_data (relplt)->this_hdr; | |
10095 | if (hdr->sh_link != elf_dynsymtab (abfd) | |
10096 | || (hdr->sh_type != SHT_REL && hdr->sh_type != SHT_RELA)) | |
10097 | return 0; | |
10098 | ||
10099 | plt = bfd_get_section_by_name (abfd, ".plt"); | |
10100 | if (plt == NULL) | |
10101 | return 0; | |
10102 | ||
10103 | slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table; | |
c9727e01 | 10104 | if (! (*slurp_relocs) (abfd, relplt, dynsyms, TRUE)) |
4c45e5c9 JJ |
10105 | return -1; |
10106 | ||
eea6121a | 10107 | count = relplt->size / hdr->sh_entsize; |
4c45e5c9 JJ |
10108 | size = count * sizeof (asymbol); |
10109 | p = relplt->relocation; | |
cb53bf42 | 10110 | for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel) |
041de40d AM |
10111 | { |
10112 | size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt"); | |
10113 | if (p->addend != 0) | |
10114 | { | |
10115 | #ifdef BFD64 | |
10116 | size += sizeof ("+0x") - 1 + 8 + 8 * (bed->s->elfclass == ELFCLASS64); | |
10117 | #else | |
10118 | size += sizeof ("+0x") - 1 + 8; | |
10119 | #endif | |
10120 | } | |
10121 | } | |
4c45e5c9 | 10122 | |
a50b1753 | 10123 | s = *ret = (asymbol *) bfd_malloc (size); |
4c45e5c9 JJ |
10124 | if (s == NULL) |
10125 | return -1; | |
10126 | ||
10127 | names = (char *) (s + count); | |
10128 | p = relplt->relocation; | |
10129 | n = 0; | |
cb53bf42 | 10130 | for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel) |
4c45e5c9 JJ |
10131 | { |
10132 | size_t len; | |
10133 | bfd_vma addr; | |
10134 | ||
10135 | addr = bed->plt_sym_val (i, plt, p); | |
10136 | if (addr == (bfd_vma) -1) | |
10137 | continue; | |
10138 | ||
10139 | *s = **p->sym_ptr_ptr; | |
65a7a66f AM |
10140 | /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since |
10141 | we are defining a symbol, ensure one of them is set. */ | |
10142 | if ((s->flags & BSF_LOCAL) == 0) | |
10143 | s->flags |= BSF_GLOBAL; | |
6ba2a415 | 10144 | s->flags |= BSF_SYNTHETIC; |
4c45e5c9 JJ |
10145 | s->section = plt; |
10146 | s->value = addr - plt->vma; | |
10147 | s->name = names; | |
8f39ba8e | 10148 | s->udata.p = NULL; |
4c45e5c9 JJ |
10149 | len = strlen ((*p->sym_ptr_ptr)->name); |
10150 | memcpy (names, (*p->sym_ptr_ptr)->name, len); | |
10151 | names += len; | |
041de40d AM |
10152 | if (p->addend != 0) |
10153 | { | |
1d770845 | 10154 | char buf[30], *a; |
d324f6d6 | 10155 | |
041de40d AM |
10156 | memcpy (names, "+0x", sizeof ("+0x") - 1); |
10157 | names += sizeof ("+0x") - 1; | |
1d770845 L |
10158 | bfd_sprintf_vma (abfd, buf, p->addend); |
10159 | for (a = buf; *a == '0'; ++a) | |
10160 | ; | |
10161 | len = strlen (a); | |
10162 | memcpy (names, a, len); | |
10163 | names += len; | |
041de40d | 10164 | } |
4c45e5c9 JJ |
10165 | memcpy (names, "@plt", sizeof ("@plt")); |
10166 | names += sizeof ("@plt"); | |
8f39ba8e | 10167 | ++s, ++n; |
4c45e5c9 JJ |
10168 | } |
10169 | ||
10170 | return n; | |
10171 | } | |
3d7f7666 | 10172 | |
3b22753a L |
10173 | /* It is only used by x86-64 so far. */ |
10174 | asection _bfd_elf_large_com_section | |
10175 | = BFD_FAKE_SECTION (_bfd_elf_large_com_section, | |
f592407e | 10176 | SEC_IS_COMMON, NULL, "LARGE_COMMON", 0); |
ecca9871 | 10177 | |
d1036acb | 10178 | void |
78245035 L |
10179 | _bfd_elf_post_process_headers (bfd * abfd, |
10180 | struct bfd_link_info * link_info ATTRIBUTE_UNUSED) | |
d1036acb L |
10181 | { |
10182 | Elf_Internal_Ehdr * i_ehdrp; /* ELF file header, internal form. */ | |
10183 | ||
10184 | i_ehdrp = elf_elfheader (abfd); | |
10185 | ||
10186 | i_ehdrp->e_ident[EI_OSABI] = get_elf_backend_data (abfd)->elf_osabi; | |
d8045f23 NC |
10187 | |
10188 | /* To make things simpler for the loader on Linux systems we set the | |
9c55345c | 10189 | osabi field to ELFOSABI_GNU if the binary contains symbols of |
f64b2e8d | 10190 | the STT_GNU_IFUNC type or STB_GNU_UNIQUE binding. */ |
d8045f23 | 10191 | if (i_ehdrp->e_ident[EI_OSABI] == ELFOSABI_NONE |
f64b2e8d | 10192 | && elf_tdata (abfd)->has_gnu_symbols) |
9c55345c | 10193 | i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_GNU; |
d1036acb | 10194 | } |
fcb93ecf PB |
10195 | |
10196 | ||
10197 | /* Return TRUE for ELF symbol types that represent functions. | |
10198 | This is the default version of this function, which is sufficient for | |
d8045f23 | 10199 | most targets. It returns true if TYPE is STT_FUNC or STT_GNU_IFUNC. */ |
fcb93ecf PB |
10200 | |
10201 | bfd_boolean | |
10202 | _bfd_elf_is_function_type (unsigned int type) | |
10203 | { | |
d8045f23 NC |
10204 | return (type == STT_FUNC |
10205 | || type == STT_GNU_IFUNC); | |
fcb93ecf | 10206 | } |
9f296da3 | 10207 | |
aef36ac1 AM |
10208 | /* If the ELF symbol SYM might be a function in SEC, return the |
10209 | function size and set *CODE_OFF to the function's entry point, | |
10210 | otherwise return zero. */ | |
9f296da3 | 10211 | |
aef36ac1 AM |
10212 | bfd_size_type |
10213 | _bfd_elf_maybe_function_sym (const asymbol *sym, asection *sec, | |
10214 | bfd_vma *code_off) | |
9f296da3 | 10215 | { |
aef36ac1 AM |
10216 | bfd_size_type size; |
10217 | ||
ff9e0f5b | 10218 | if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT |
aef36ac1 AM |
10219 | | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0 |
10220 | || sym->section != sec) | |
10221 | return 0; | |
ff9e0f5b | 10222 | |
ff9e0f5b | 10223 | *code_off = sym->value; |
aef36ac1 AM |
10224 | size = 0; |
10225 | if (!(sym->flags & BSF_SYNTHETIC)) | |
10226 | size = ((elf_symbol_type *) sym)->internal_elf_sym.st_size; | |
10227 | if (size == 0) | |
10228 | size = 1; | |
10229 | return size; | |
9f296da3 | 10230 | } |