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 |
a50b1753 | 300 | || (shstrtab = (bfd_byte *) bfd_alloc (abfd, shstrtabsize + 1)) == NULL |
c6c60d09 JJ |
301 | || bfd_seek (abfd, offset, SEEK_SET) != 0) |
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); | |
307 | shstrtab = NULL; | |
3471d59d CC |
308 | /* Once we've failed to read it, make sure we don't keep |
309 | trying. Otherwise, we'll keep allocating space for | |
310 | the string table over and over. */ | |
311 | i_shdrp[shindex]->sh_size = 0; | |
c6c60d09 JJ |
312 | } |
313 | else | |
314 | shstrtab[shstrtabsize] = '\0'; | |
217aa764 | 315 | i_shdrp[shindex]->contents = shstrtab; |
252b5132 | 316 | } |
f075ee0c | 317 | return (char *) shstrtab; |
252b5132 RH |
318 | } |
319 | ||
320 | char * | |
217aa764 AM |
321 | bfd_elf_string_from_elf_section (bfd *abfd, |
322 | unsigned int shindex, | |
323 | unsigned int strindex) | |
252b5132 RH |
324 | { |
325 | Elf_Internal_Shdr *hdr; | |
326 | ||
327 | if (strindex == 0) | |
328 | return ""; | |
329 | ||
74f2e02b AM |
330 | if (elf_elfsections (abfd) == NULL || shindex >= elf_numsections (abfd)) |
331 | return NULL; | |
332 | ||
252b5132 RH |
333 | hdr = elf_elfsections (abfd)[shindex]; |
334 | ||
335 | if (hdr->contents == NULL | |
336 | && bfd_elf_get_str_section (abfd, shindex) == NULL) | |
337 | return NULL; | |
338 | ||
339 | if (strindex >= hdr->sh_size) | |
340 | { | |
1b3a8575 | 341 | unsigned int shstrndx = elf_elfheader(abfd)->e_shstrndx; |
252b5132 | 342 | (*_bfd_error_handler) |
d003868e AM |
343 | (_("%B: invalid string offset %u >= %lu for section `%s'"), |
344 | abfd, strindex, (unsigned long) hdr->sh_size, | |
1b3a8575 | 345 | (shindex == shstrndx && strindex == hdr->sh_name |
252b5132 | 346 | ? ".shstrtab" |
1b3a8575 | 347 | : bfd_elf_string_from_elf_section (abfd, shstrndx, hdr->sh_name))); |
45b222d6 | 348 | return NULL; |
252b5132 RH |
349 | } |
350 | ||
351 | return ((char *) hdr->contents) + strindex; | |
352 | } | |
353 | ||
6cdc0ccc AM |
354 | /* Read and convert symbols to internal format. |
355 | SYMCOUNT specifies the number of symbols to read, starting from | |
356 | symbol SYMOFFSET. If any of INTSYM_BUF, EXTSYM_BUF or EXTSHNDX_BUF | |
357 | are non-NULL, they are used to store the internal symbols, external | |
b7c368d0 NC |
358 | symbols, and symbol section index extensions, respectively. |
359 | Returns a pointer to the internal symbol buffer (malloced if necessary) | |
360 | or NULL if there were no symbols or some kind of problem. */ | |
6cdc0ccc AM |
361 | |
362 | Elf_Internal_Sym * | |
217aa764 AM |
363 | bfd_elf_get_elf_syms (bfd *ibfd, |
364 | Elf_Internal_Shdr *symtab_hdr, | |
365 | size_t symcount, | |
366 | size_t symoffset, | |
367 | Elf_Internal_Sym *intsym_buf, | |
368 | void *extsym_buf, | |
369 | Elf_External_Sym_Shndx *extshndx_buf) | |
6cdc0ccc AM |
370 | { |
371 | Elf_Internal_Shdr *shndx_hdr; | |
217aa764 | 372 | void *alloc_ext; |
df622259 | 373 | const bfd_byte *esym; |
6cdc0ccc AM |
374 | Elf_External_Sym_Shndx *alloc_extshndx; |
375 | Elf_External_Sym_Shndx *shndx; | |
4dd07732 | 376 | Elf_Internal_Sym *alloc_intsym; |
6cdc0ccc AM |
377 | Elf_Internal_Sym *isym; |
378 | Elf_Internal_Sym *isymend; | |
9c5bfbb7 | 379 | const struct elf_backend_data *bed; |
6cdc0ccc AM |
380 | size_t extsym_size; |
381 | bfd_size_type amt; | |
382 | file_ptr pos; | |
383 | ||
e44a2c9c AM |
384 | if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour) |
385 | abort (); | |
386 | ||
6cdc0ccc AM |
387 | if (symcount == 0) |
388 | return intsym_buf; | |
389 | ||
390 | /* Normal syms might have section extension entries. */ | |
391 | shndx_hdr = NULL; | |
392 | if (symtab_hdr == &elf_tdata (ibfd)->symtab_hdr) | |
393 | shndx_hdr = &elf_tdata (ibfd)->symtab_shndx_hdr; | |
394 | ||
395 | /* Read the symbols. */ | |
396 | alloc_ext = NULL; | |
397 | alloc_extshndx = NULL; | |
4dd07732 | 398 | alloc_intsym = NULL; |
6cdc0ccc AM |
399 | bed = get_elf_backend_data (ibfd); |
400 | extsym_size = bed->s->sizeof_sym; | |
401 | amt = symcount * extsym_size; | |
402 | pos = symtab_hdr->sh_offset + symoffset * extsym_size; | |
403 | if (extsym_buf == NULL) | |
404 | { | |
d0fb9a8d | 405 | alloc_ext = bfd_malloc2 (symcount, extsym_size); |
6cdc0ccc AM |
406 | extsym_buf = alloc_ext; |
407 | } | |
408 | if (extsym_buf == NULL | |
409 | || bfd_seek (ibfd, pos, SEEK_SET) != 0 | |
410 | || bfd_bread (extsym_buf, amt, ibfd) != amt) | |
411 | { | |
412 | intsym_buf = NULL; | |
413 | goto out; | |
414 | } | |
415 | ||
416 | if (shndx_hdr == NULL || shndx_hdr->sh_size == 0) | |
417 | extshndx_buf = NULL; | |
418 | else | |
419 | { | |
420 | amt = symcount * sizeof (Elf_External_Sym_Shndx); | |
421 | pos = shndx_hdr->sh_offset + symoffset * sizeof (Elf_External_Sym_Shndx); | |
422 | if (extshndx_buf == NULL) | |
423 | { | |
a50b1753 NC |
424 | alloc_extshndx = (Elf_External_Sym_Shndx *) |
425 | bfd_malloc2 (symcount, sizeof (Elf_External_Sym_Shndx)); | |
6cdc0ccc AM |
426 | extshndx_buf = alloc_extshndx; |
427 | } | |
428 | if (extshndx_buf == NULL | |
429 | || bfd_seek (ibfd, pos, SEEK_SET) != 0 | |
430 | || bfd_bread (extshndx_buf, amt, ibfd) != amt) | |
431 | { | |
432 | intsym_buf = NULL; | |
433 | goto out; | |
434 | } | |
435 | } | |
436 | ||
437 | if (intsym_buf == NULL) | |
438 | { | |
a50b1753 NC |
439 | alloc_intsym = (Elf_Internal_Sym *) |
440 | bfd_malloc2 (symcount, sizeof (Elf_Internal_Sym)); | |
4dd07732 | 441 | intsym_buf = alloc_intsym; |
6cdc0ccc AM |
442 | if (intsym_buf == NULL) |
443 | goto out; | |
444 | } | |
445 | ||
446 | /* Convert the symbols to internal form. */ | |
447 | isymend = intsym_buf + symcount; | |
a50b1753 NC |
448 | for (esym = (const bfd_byte *) extsym_buf, isym = intsym_buf, |
449 | shndx = extshndx_buf; | |
6cdc0ccc AM |
450 | isym < isymend; |
451 | esym += extsym_size, isym++, shndx = shndx != NULL ? shndx + 1 : NULL) | |
8384fb8f AM |
452 | if (!(*bed->s->swap_symbol_in) (ibfd, esym, shndx, isym)) |
453 | { | |
454 | symoffset += (esym - (bfd_byte *) extsym_buf) / extsym_size; | |
455 | (*_bfd_error_handler) (_("%B symbol number %lu references " | |
456 | "nonexistent SHT_SYMTAB_SHNDX section"), | |
457 | ibfd, (unsigned long) symoffset); | |
4dd07732 AM |
458 | if (alloc_intsym != NULL) |
459 | free (alloc_intsym); | |
8384fb8f AM |
460 | intsym_buf = NULL; |
461 | goto out; | |
462 | } | |
6cdc0ccc AM |
463 | |
464 | out: | |
465 | if (alloc_ext != NULL) | |
466 | free (alloc_ext); | |
467 | if (alloc_extshndx != NULL) | |
468 | free (alloc_extshndx); | |
469 | ||
470 | return intsym_buf; | |
471 | } | |
472 | ||
5cab59f6 AM |
473 | /* Look up a symbol name. */ |
474 | const char * | |
be8dd2ca AM |
475 | bfd_elf_sym_name (bfd *abfd, |
476 | Elf_Internal_Shdr *symtab_hdr, | |
26c61ae5 L |
477 | Elf_Internal_Sym *isym, |
478 | asection *sym_sec) | |
5cab59f6 | 479 | { |
26c61ae5 | 480 | const char *name; |
5cab59f6 | 481 | unsigned int iname = isym->st_name; |
be8dd2ca | 482 | unsigned int shindex = symtab_hdr->sh_link; |
26c61ae5 | 483 | |
138f35cc JJ |
484 | if (iname == 0 && ELF_ST_TYPE (isym->st_info) == STT_SECTION |
485 | /* Check for a bogus st_shndx to avoid crashing. */ | |
4fbb74a6 | 486 | && isym->st_shndx < elf_numsections (abfd)) |
5cab59f6 AM |
487 | { |
488 | iname = elf_elfsections (abfd)[isym->st_shndx]->sh_name; | |
489 | shindex = elf_elfheader (abfd)->e_shstrndx; | |
490 | } | |
491 | ||
26c61ae5 L |
492 | name = bfd_elf_string_from_elf_section (abfd, shindex, iname); |
493 | if (name == NULL) | |
494 | name = "(null)"; | |
495 | else if (sym_sec && *name == '\0') | |
496 | name = bfd_section_name (abfd, sym_sec); | |
497 | ||
498 | return name; | |
5cab59f6 AM |
499 | } |
500 | ||
dbb410c3 AM |
501 | /* Elf_Internal_Shdr->contents is an array of these for SHT_GROUP |
502 | sections. The first element is the flags, the rest are section | |
503 | pointers. */ | |
504 | ||
505 | typedef union elf_internal_group { | |
506 | Elf_Internal_Shdr *shdr; | |
507 | unsigned int flags; | |
508 | } Elf_Internal_Group; | |
509 | ||
b885599b AM |
510 | /* Return the name of the group signature symbol. Why isn't the |
511 | signature just a string? */ | |
512 | ||
513 | static const char * | |
217aa764 | 514 | group_signature (bfd *abfd, Elf_Internal_Shdr *ghdr) |
b885599b | 515 | { |
9dce4196 | 516 | Elf_Internal_Shdr *hdr; |
9dce4196 AM |
517 | unsigned char esym[sizeof (Elf64_External_Sym)]; |
518 | Elf_External_Sym_Shndx eshndx; | |
519 | Elf_Internal_Sym isym; | |
b885599b | 520 | |
13792e9d L |
521 | /* First we need to ensure the symbol table is available. Make sure |
522 | that it is a symbol table section. */ | |
4fbb74a6 AM |
523 | if (ghdr->sh_link >= elf_numsections (abfd)) |
524 | return NULL; | |
13792e9d L |
525 | hdr = elf_elfsections (abfd) [ghdr->sh_link]; |
526 | if (hdr->sh_type != SHT_SYMTAB | |
527 | || ! bfd_section_from_shdr (abfd, ghdr->sh_link)) | |
b885599b AM |
528 | return NULL; |
529 | ||
9dce4196 AM |
530 | /* Go read the symbol. */ |
531 | hdr = &elf_tdata (abfd)->symtab_hdr; | |
6cdc0ccc AM |
532 | if (bfd_elf_get_elf_syms (abfd, hdr, 1, ghdr->sh_info, |
533 | &isym, esym, &eshndx) == NULL) | |
b885599b | 534 | return NULL; |
9dce4196 | 535 | |
26c61ae5 | 536 | return bfd_elf_sym_name (abfd, hdr, &isym, NULL); |
b885599b AM |
537 | } |
538 | ||
dbb410c3 AM |
539 | /* Set next_in_group list pointer, and group name for NEWSECT. */ |
540 | ||
b34976b6 | 541 | static bfd_boolean |
217aa764 | 542 | setup_group (bfd *abfd, Elf_Internal_Shdr *hdr, asection *newsect) |
dbb410c3 AM |
543 | { |
544 | unsigned int num_group = elf_tdata (abfd)->num_group; | |
545 | ||
546 | /* If num_group is zero, read in all SHT_GROUP sections. The count | |
547 | is set to -1 if there are no SHT_GROUP sections. */ | |
548 | if (num_group == 0) | |
549 | { | |
550 | unsigned int i, shnum; | |
551 | ||
552 | /* First count the number of groups. If we have a SHT_GROUP | |
553 | section with just a flag word (ie. sh_size is 4), ignore it. */ | |
9ad5cbcf | 554 | shnum = elf_numsections (abfd); |
dbb410c3 | 555 | num_group = 0; |
08a40648 | 556 | |
44534af3 | 557 | #define IS_VALID_GROUP_SECTION_HEADER(shdr, minsize) \ |
1783205a | 558 | ( (shdr)->sh_type == SHT_GROUP \ |
44534af3 | 559 | && (shdr)->sh_size >= minsize \ |
1783205a NC |
560 | && (shdr)->sh_entsize == GRP_ENTRY_SIZE \ |
561 | && ((shdr)->sh_size % GRP_ENTRY_SIZE) == 0) | |
08a40648 | 562 | |
dbb410c3 AM |
563 | for (i = 0; i < shnum; i++) |
564 | { | |
565 | Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i]; | |
1783205a | 566 | |
44534af3 | 567 | if (IS_VALID_GROUP_SECTION_HEADER (shdr, 2 * GRP_ENTRY_SIZE)) |
dbb410c3 AM |
568 | num_group += 1; |
569 | } | |
570 | ||
571 | if (num_group == 0) | |
20dbb49d L |
572 | { |
573 | num_group = (unsigned) -1; | |
574 | elf_tdata (abfd)->num_group = num_group; | |
575 | } | |
576 | else | |
dbb410c3 AM |
577 | { |
578 | /* We keep a list of elf section headers for group sections, | |
579 | so we can find them quickly. */ | |
20dbb49d | 580 | bfd_size_type amt; |
d0fb9a8d | 581 | |
20dbb49d | 582 | elf_tdata (abfd)->num_group = num_group; |
a50b1753 NC |
583 | elf_tdata (abfd)->group_sect_ptr = (Elf_Internal_Shdr **) |
584 | bfd_alloc2 (abfd, num_group, sizeof (Elf_Internal_Shdr *)); | |
dbb410c3 | 585 | if (elf_tdata (abfd)->group_sect_ptr == NULL) |
b34976b6 | 586 | return FALSE; |
dbb410c3 AM |
587 | |
588 | num_group = 0; | |
589 | for (i = 0; i < shnum; i++) | |
590 | { | |
591 | Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i]; | |
1783205a | 592 | |
44534af3 | 593 | if (IS_VALID_GROUP_SECTION_HEADER (shdr, 2 * GRP_ENTRY_SIZE)) |
dbb410c3 | 594 | { |
973ffd63 | 595 | unsigned char *src; |
dbb410c3 AM |
596 | Elf_Internal_Group *dest; |
597 | ||
598 | /* Add to list of sections. */ | |
599 | elf_tdata (abfd)->group_sect_ptr[num_group] = shdr; | |
600 | num_group += 1; | |
601 | ||
602 | /* Read the raw contents. */ | |
603 | BFD_ASSERT (sizeof (*dest) >= 4); | |
604 | amt = shdr->sh_size * sizeof (*dest) / 4; | |
a50b1753 NC |
605 | shdr->contents = (unsigned char *) |
606 | bfd_alloc2 (abfd, shdr->sh_size, sizeof (*dest) / 4); | |
1783205a NC |
607 | /* PR binutils/4110: Handle corrupt group headers. */ |
608 | if (shdr->contents == NULL) | |
609 | { | |
610 | _bfd_error_handler | |
493a3386 | 611 | (_("%B: corrupt size field in group section header: 0x%lx"), abfd, shdr->sh_size); |
1783205a | 612 | bfd_set_error (bfd_error_bad_value); |
493a3386 NC |
613 | -- num_group; |
614 | continue; | |
1783205a NC |
615 | } |
616 | ||
617 | memset (shdr->contents, 0, amt); | |
618 | ||
619 | if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0 | |
dbb410c3 AM |
620 | || (bfd_bread (shdr->contents, shdr->sh_size, abfd) |
621 | != shdr->sh_size)) | |
493a3386 NC |
622 | { |
623 | _bfd_error_handler | |
624 | (_("%B: invalid size field in group section header: 0x%lx"), abfd, shdr->sh_size); | |
625 | bfd_set_error (bfd_error_bad_value); | |
626 | -- num_group; | |
627 | /* PR 17510: If the group contents are even partially | |
628 | corrupt, do not allow any of the contents to be used. */ | |
629 | memset (shdr->contents, 0, amt); | |
630 | continue; | |
631 | } | |
708d7d0d | 632 | |
dbb410c3 AM |
633 | /* Translate raw contents, a flag word followed by an |
634 | array of elf section indices all in target byte order, | |
635 | to the flag word followed by an array of elf section | |
636 | pointers. */ | |
637 | src = shdr->contents + shdr->sh_size; | |
638 | dest = (Elf_Internal_Group *) (shdr->contents + amt); | |
639 | while (1) | |
640 | { | |
641 | unsigned int idx; | |
642 | ||
643 | src -= 4; | |
644 | --dest; | |
645 | idx = H_GET_32 (abfd, src); | |
646 | if (src == shdr->contents) | |
647 | { | |
648 | dest->flags = idx; | |
b885599b AM |
649 | if (shdr->bfd_section != NULL && (idx & GRP_COMDAT)) |
650 | shdr->bfd_section->flags | |
651 | |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD; | |
dbb410c3 AM |
652 | break; |
653 | } | |
654 | if (idx >= shnum) | |
655 | { | |
656 | ((*_bfd_error_handler) | |
d003868e | 657 | (_("%B: invalid SHT_GROUP entry"), abfd)); |
dbb410c3 AM |
658 | idx = 0; |
659 | } | |
660 | dest->shdr = elf_elfsections (abfd)[idx]; | |
661 | } | |
662 | } | |
663 | } | |
493a3386 NC |
664 | |
665 | /* PR 17510: Corrupt binaries might contain invalid groups. */ | |
666 | if (num_group != (unsigned) elf_tdata (abfd)->num_group) | |
667 | { | |
668 | elf_tdata (abfd)->num_group = num_group; | |
669 | ||
670 | /* If all groups are invalid then fail. */ | |
671 | if (num_group == 0) | |
672 | { | |
673 | elf_tdata (abfd)->group_sect_ptr = NULL; | |
674 | elf_tdata (abfd)->num_group = num_group = -1; | |
675 | (*_bfd_error_handler) (_("%B: no valid group sections found"), abfd); | |
676 | bfd_set_error (bfd_error_bad_value); | |
677 | } | |
678 | } | |
dbb410c3 AM |
679 | } |
680 | } | |
681 | ||
682 | if (num_group != (unsigned) -1) | |
683 | { | |
684 | unsigned int i; | |
685 | ||
686 | for (i = 0; i < num_group; i++) | |
687 | { | |
688 | Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i]; | |
689 | Elf_Internal_Group *idx = (Elf_Internal_Group *) shdr->contents; | |
690 | unsigned int n_elt = shdr->sh_size / 4; | |
691 | ||
692 | /* Look through this group's sections to see if current | |
693 | section is a member. */ | |
694 | while (--n_elt != 0) | |
695 | if ((++idx)->shdr == hdr) | |
696 | { | |
e0e8c97f | 697 | asection *s = NULL; |
dbb410c3 AM |
698 | |
699 | /* We are a member of this group. Go looking through | |
700 | other members to see if any others are linked via | |
701 | next_in_group. */ | |
702 | idx = (Elf_Internal_Group *) shdr->contents; | |
703 | n_elt = shdr->sh_size / 4; | |
704 | while (--n_elt != 0) | |
705 | if ((s = (++idx)->shdr->bfd_section) != NULL | |
945906ff | 706 | && elf_next_in_group (s) != NULL) |
dbb410c3 AM |
707 | break; |
708 | if (n_elt != 0) | |
709 | { | |
dbb410c3 AM |
710 | /* Snarf the group name from other member, and |
711 | insert current section in circular list. */ | |
945906ff AM |
712 | elf_group_name (newsect) = elf_group_name (s); |
713 | elf_next_in_group (newsect) = elf_next_in_group (s); | |
714 | elf_next_in_group (s) = newsect; | |
dbb410c3 AM |
715 | } |
716 | else | |
717 | { | |
dbb410c3 AM |
718 | const char *gname; |
719 | ||
b885599b AM |
720 | gname = group_signature (abfd, shdr); |
721 | if (gname == NULL) | |
b34976b6 | 722 | return FALSE; |
945906ff | 723 | elf_group_name (newsect) = gname; |
dbb410c3 AM |
724 | |
725 | /* Start a circular list with one element. */ | |
945906ff | 726 | elf_next_in_group (newsect) = newsect; |
dbb410c3 | 727 | } |
b885599b | 728 | |
9dce4196 AM |
729 | /* If the group section has been created, point to the |
730 | new member. */ | |
dbb410c3 | 731 | if (shdr->bfd_section != NULL) |
945906ff | 732 | elf_next_in_group (shdr->bfd_section) = newsect; |
b885599b | 733 | |
dbb410c3 AM |
734 | i = num_group - 1; |
735 | break; | |
736 | } | |
737 | } | |
738 | } | |
739 | ||
945906ff | 740 | if (elf_group_name (newsect) == NULL) |
dbb410c3 | 741 | { |
d003868e AM |
742 | (*_bfd_error_handler) (_("%B: no group info for section %A"), |
743 | abfd, newsect); | |
493a3386 | 744 | return FALSE; |
dbb410c3 | 745 | } |
b34976b6 | 746 | return TRUE; |
dbb410c3 AM |
747 | } |
748 | ||
3d7f7666 | 749 | bfd_boolean |
dd863624 | 750 | _bfd_elf_setup_sections (bfd *abfd) |
3d7f7666 L |
751 | { |
752 | unsigned int i; | |
753 | unsigned int num_group = elf_tdata (abfd)->num_group; | |
754 | bfd_boolean result = TRUE; | |
dd863624 L |
755 | asection *s; |
756 | ||
757 | /* Process SHF_LINK_ORDER. */ | |
758 | for (s = abfd->sections; s != NULL; s = s->next) | |
759 | { | |
760 | Elf_Internal_Shdr *this_hdr = &elf_section_data (s)->this_hdr; | |
761 | if ((this_hdr->sh_flags & SHF_LINK_ORDER) != 0) | |
762 | { | |
763 | unsigned int elfsec = this_hdr->sh_link; | |
764 | /* FIXME: The old Intel compiler and old strip/objcopy may | |
765 | not set the sh_link or sh_info fields. Hence we could | |
766 | get the situation where elfsec is 0. */ | |
767 | if (elfsec == 0) | |
768 | { | |
4fbb74a6 | 769 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
dd863624 L |
770 | if (bed->link_order_error_handler) |
771 | bed->link_order_error_handler | |
772 | (_("%B: warning: sh_link not set for section `%A'"), | |
773 | abfd, s); | |
774 | } | |
775 | else | |
776 | { | |
91d6fa6a | 777 | asection *linksec = NULL; |
25bbc984 | 778 | |
4fbb74a6 AM |
779 | if (elfsec < elf_numsections (abfd)) |
780 | { | |
781 | this_hdr = elf_elfsections (abfd)[elfsec]; | |
91d6fa6a | 782 | linksec = this_hdr->bfd_section; |
4fbb74a6 | 783 | } |
25bbc984 L |
784 | |
785 | /* PR 1991, 2008: | |
786 | Some strip/objcopy may leave an incorrect value in | |
787 | sh_link. We don't want to proceed. */ | |
91d6fa6a | 788 | if (linksec == NULL) |
25bbc984 L |
789 | { |
790 | (*_bfd_error_handler) | |
791 | (_("%B: sh_link [%d] in section `%A' is incorrect"), | |
792 | s->owner, s, elfsec); | |
793 | result = FALSE; | |
794 | } | |
795 | ||
91d6fa6a | 796 | elf_linked_to_section (s) = linksec; |
dd863624 L |
797 | } |
798 | } | |
799 | } | |
3d7f7666 | 800 | |
dd863624 | 801 | /* Process section groups. */ |
3d7f7666 L |
802 | if (num_group == (unsigned) -1) |
803 | return result; | |
804 | ||
805 | for (i = 0; i < num_group; i++) | |
806 | { | |
807 | Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i]; | |
808 | Elf_Internal_Group *idx = (Elf_Internal_Group *) shdr->contents; | |
809 | unsigned int n_elt = shdr->sh_size / 4; | |
810 | ||
811 | while (--n_elt != 0) | |
812 | if ((++idx)->shdr->bfd_section) | |
813 | elf_sec_group (idx->shdr->bfd_section) = shdr->bfd_section; | |
814 | else if (idx->shdr->sh_type == SHT_RELA | |
815 | || idx->shdr->sh_type == SHT_REL) | |
816 | /* We won't include relocation sections in section groups in | |
817 | output object files. We adjust the group section size here | |
818 | so that relocatable link will work correctly when | |
819 | relocation sections are in section group in input object | |
820 | files. */ | |
821 | shdr->bfd_section->size -= 4; | |
822 | else | |
823 | { | |
824 | /* There are some unknown sections in the group. */ | |
825 | (*_bfd_error_handler) | |
d003868e AM |
826 | (_("%B: unknown [%d] section `%s' in group [%s]"), |
827 | abfd, | |
3d7f7666 | 828 | (unsigned int) idx->shdr->sh_type, |
1b3a8575 AM |
829 | bfd_elf_string_from_elf_section (abfd, |
830 | (elf_elfheader (abfd) | |
831 | ->e_shstrndx), | |
832 | idx->shdr->sh_name), | |
3d7f7666 L |
833 | shdr->bfd_section->name); |
834 | result = FALSE; | |
835 | } | |
836 | } | |
837 | return result; | |
838 | } | |
839 | ||
72adc230 AM |
840 | bfd_boolean |
841 | bfd_elf_is_group_section (bfd *abfd ATTRIBUTE_UNUSED, const asection *sec) | |
842 | { | |
843 | return elf_next_in_group (sec) != NULL; | |
844 | } | |
845 | ||
252b5132 RH |
846 | /* Make a BFD section from an ELF section. We store a pointer to the |
847 | BFD section in the bfd_section field of the header. */ | |
848 | ||
b34976b6 | 849 | bfd_boolean |
217aa764 AM |
850 | _bfd_elf_make_section_from_shdr (bfd *abfd, |
851 | Elf_Internal_Shdr *hdr, | |
6dc132d9 L |
852 | const char *name, |
853 | int shindex) | |
252b5132 RH |
854 | { |
855 | asection *newsect; | |
856 | flagword flags; | |
9c5bfbb7 | 857 | const struct elf_backend_data *bed; |
252b5132 RH |
858 | |
859 | if (hdr->bfd_section != NULL) | |
4e011fb5 | 860 | return TRUE; |
252b5132 RH |
861 | |
862 | newsect = bfd_make_section_anyway (abfd, name); | |
863 | if (newsect == NULL) | |
b34976b6 | 864 | return FALSE; |
252b5132 | 865 | |
1829f4b2 AM |
866 | hdr->bfd_section = newsect; |
867 | elf_section_data (newsect)->this_hdr = *hdr; | |
6dc132d9 | 868 | elf_section_data (newsect)->this_idx = shindex; |
1829f4b2 | 869 | |
2f89ff8d L |
870 | /* Always use the real type/flags. */ |
871 | elf_section_type (newsect) = hdr->sh_type; | |
872 | elf_section_flags (newsect) = hdr->sh_flags; | |
873 | ||
252b5132 RH |
874 | newsect->filepos = hdr->sh_offset; |
875 | ||
876 | if (! bfd_set_section_vma (abfd, newsect, hdr->sh_addr) | |
877 | || ! bfd_set_section_size (abfd, newsect, hdr->sh_size) | |
878 | || ! bfd_set_section_alignment (abfd, newsect, | |
72de5009 | 879 | bfd_log2 (hdr->sh_addralign))) |
b34976b6 | 880 | return FALSE; |
252b5132 RH |
881 | |
882 | flags = SEC_NO_FLAGS; | |
883 | if (hdr->sh_type != SHT_NOBITS) | |
884 | flags |= SEC_HAS_CONTENTS; | |
dbb410c3 | 885 | if (hdr->sh_type == SHT_GROUP) |
b3096250 | 886 | flags |= SEC_GROUP | SEC_EXCLUDE; |
252b5132 RH |
887 | if ((hdr->sh_flags & SHF_ALLOC) != 0) |
888 | { | |
889 | flags |= SEC_ALLOC; | |
890 | if (hdr->sh_type != SHT_NOBITS) | |
891 | flags |= SEC_LOAD; | |
892 | } | |
893 | if ((hdr->sh_flags & SHF_WRITE) == 0) | |
894 | flags |= SEC_READONLY; | |
895 | if ((hdr->sh_flags & SHF_EXECINSTR) != 0) | |
896 | flags |= SEC_CODE; | |
897 | else if ((flags & SEC_LOAD) != 0) | |
898 | flags |= SEC_DATA; | |
f5fa8ca2 JJ |
899 | if ((hdr->sh_flags & SHF_MERGE) != 0) |
900 | { | |
901 | flags |= SEC_MERGE; | |
902 | newsect->entsize = hdr->sh_entsize; | |
903 | if ((hdr->sh_flags & SHF_STRINGS) != 0) | |
904 | flags |= SEC_STRINGS; | |
905 | } | |
dbb410c3 AM |
906 | if (hdr->sh_flags & SHF_GROUP) |
907 | if (!setup_group (abfd, hdr, newsect)) | |
b34976b6 | 908 | return FALSE; |
13ae64f3 JJ |
909 | if ((hdr->sh_flags & SHF_TLS) != 0) |
910 | flags |= SEC_THREAD_LOCAL; | |
18ae9cc1 L |
911 | if ((hdr->sh_flags & SHF_EXCLUDE) != 0) |
912 | flags |= SEC_EXCLUDE; | |
252b5132 | 913 | |
3d2b39cf | 914 | if ((flags & SEC_ALLOC) == 0) |
7a6cc5fb | 915 | { |
3d2b39cf L |
916 | /* The debugging sections appear to be recognized only by name, |
917 | not any sort of flag. Their SEC_ALLOC bits are cleared. */ | |
3d2b39cf L |
918 | if (name [0] == '.') |
919 | { | |
f073ced3 AM |
920 | const char *p; |
921 | int n; | |
922 | if (name[1] == 'd') | |
923 | p = ".debug", n = 6; | |
924 | else if (name[1] == 'g' && name[2] == 'n') | |
925 | p = ".gnu.linkonce.wi.", n = 17; | |
926 | else if (name[1] == 'g' && name[2] == 'd') | |
927 | p = ".gdb_index", n = 11; /* yes we really do mean 11. */ | |
928 | else if (name[1] == 'l') | |
929 | p = ".line", n = 5; | |
930 | else if (name[1] == 's') | |
931 | p = ".stab", n = 5; | |
932 | else if (name[1] == 'z') | |
933 | p = ".zdebug", n = 7; | |
934 | else | |
935 | p = NULL, n = 0; | |
936 | if (p != NULL && strncmp (name, p, n) == 0) | |
3d2b39cf L |
937 | flags |= SEC_DEBUGGING; |
938 | } | |
939 | } | |
252b5132 RH |
940 | |
941 | /* As a GNU extension, if the name begins with .gnu.linkonce, we | |
942 | only link a single copy of the section. This is used to support | |
943 | g++. g++ will emit each template expansion in its own section. | |
944 | The symbols will be defined as weak, so that multiple definitions | |
945 | are permitted. The GNU linker extension is to actually discard | |
946 | all but one of the sections. */ | |
0112cd26 | 947 | if (CONST_STRNEQ (name, ".gnu.linkonce") |
b885599b | 948 | && elf_next_in_group (newsect) == NULL) |
252b5132 RH |
949 | flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD; |
950 | ||
fa152c49 JW |
951 | bed = get_elf_backend_data (abfd); |
952 | if (bed->elf_backend_section_flags) | |
953 | if (! bed->elf_backend_section_flags (&flags, hdr)) | |
b34976b6 | 954 | return FALSE; |
fa152c49 | 955 | |
252b5132 | 956 | if (! bfd_set_section_flags (abfd, newsect, flags)) |
b34976b6 | 957 | return FALSE; |
252b5132 | 958 | |
718175fa JK |
959 | /* We do not parse the PT_NOTE segments as we are interested even in the |
960 | separate debug info files which may have the segments offsets corrupted. | |
961 | PT_NOTEs from the core files are currently not parsed using BFD. */ | |
962 | if (hdr->sh_type == SHT_NOTE) | |
963 | { | |
baea7ef1 | 964 | bfd_byte *contents; |
718175fa | 965 | |
baea7ef1 | 966 | if (!bfd_malloc_and_get_section (abfd, newsect, &contents)) |
718175fa JK |
967 | return FALSE; |
968 | ||
baea7ef1 | 969 | elf_parse_notes (abfd, (char *) contents, hdr->sh_size, -1); |
718175fa JK |
970 | free (contents); |
971 | } | |
972 | ||
252b5132 RH |
973 | if ((flags & SEC_ALLOC) != 0) |
974 | { | |
975 | Elf_Internal_Phdr *phdr; | |
6ffd7900 AM |
976 | unsigned int i, nload; |
977 | ||
978 | /* Some ELF linkers produce binaries with all the program header | |
979 | p_paddr fields zero. If we have such a binary with more than | |
980 | one PT_LOAD header, then leave the section lma equal to vma | |
981 | so that we don't create sections with overlapping lma. */ | |
982 | phdr = elf_tdata (abfd)->phdr; | |
983 | for (nload = 0, i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++) | |
984 | if (phdr->p_paddr != 0) | |
985 | break; | |
986 | else if (phdr->p_type == PT_LOAD && phdr->p_memsz != 0) | |
987 | ++nload; | |
988 | if (i >= elf_elfheader (abfd)->e_phnum && nload > 1) | |
989 | return TRUE; | |
252b5132 | 990 | |
252b5132 RH |
991 | phdr = elf_tdata (abfd)->phdr; |
992 | for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++) | |
993 | { | |
86b2281f AM |
994 | if (((phdr->p_type == PT_LOAD |
995 | && (hdr->sh_flags & SHF_TLS) == 0) | |
996 | || phdr->p_type == PT_TLS) | |
9a83a553 | 997 | && ELF_SECTION_IN_SEGMENT (hdr, phdr)) |
252b5132 | 998 | { |
88967714 AM |
999 | if ((flags & SEC_LOAD) == 0) |
1000 | newsect->lma = (phdr->p_paddr | |
1001 | + hdr->sh_addr - phdr->p_vaddr); | |
1002 | else | |
1003 | /* We used to use the same adjustment for SEC_LOAD | |
1004 | sections, but that doesn't work if the segment | |
1005 | is packed with code from multiple VMAs. | |
1006 | Instead we calculate the section LMA based on | |
1007 | the segment LMA. It is assumed that the | |
1008 | segment will contain sections with contiguous | |
1009 | LMAs, even if the VMAs are not. */ | |
1010 | newsect->lma = (phdr->p_paddr | |
1011 | + hdr->sh_offset - phdr->p_offset); | |
1012 | ||
1013 | /* With contiguous segments, we can't tell from file | |
1014 | offsets whether a section with zero size should | |
1015 | be placed at the end of one segment or the | |
1016 | beginning of the next. Decide based on vaddr. */ | |
1017 | if (hdr->sh_addr >= phdr->p_vaddr | |
1018 | && (hdr->sh_addr + hdr->sh_size | |
1019 | <= phdr->p_vaddr + phdr->p_memsz)) | |
1020 | break; | |
252b5132 RH |
1021 | } |
1022 | } | |
1023 | } | |
1024 | ||
4a114e3e L |
1025 | /* Compress/decompress DWARF debug sections with names: .debug_* and |
1026 | .zdebug_*, after the section flags is set. */ | |
1027 | if ((flags & SEC_DEBUGGING) | |
1028 | && ((name[1] == 'd' && name[6] == '_') | |
1029 | || (name[1] == 'z' && name[7] == '_'))) | |
1030 | { | |
1031 | enum { nothing, compress, decompress } action = nothing; | |
4e011fb5 | 1032 | char *new_name; |
4a114e3e L |
1033 | |
1034 | if (bfd_is_section_compressed (abfd, newsect)) | |
1035 | { | |
1036 | /* Compressed section. Check if we should decompress. */ | |
1037 | if ((abfd->flags & BFD_DECOMPRESS)) | |
1038 | action = decompress; | |
1039 | } | |
1040 | else | |
1041 | { | |
1042 | /* Normal section. Check if we should compress. */ | |
5a5ed5b0 | 1043 | if ((abfd->flags & BFD_COMPRESS) && newsect->size != 0) |
4a114e3e L |
1044 | action = compress; |
1045 | } | |
1046 | ||
4e011fb5 | 1047 | new_name = NULL; |
4a114e3e L |
1048 | switch (action) |
1049 | { | |
1050 | case nothing: | |
1051 | break; | |
1052 | case compress: | |
1053 | if (!bfd_init_section_compress_status (abfd, newsect)) | |
1054 | { | |
1055 | (*_bfd_error_handler) | |
bc823199 | 1056 | (_("%B: unable to initialize compress status for section %s"), |
4a114e3e L |
1057 | abfd, name); |
1058 | return FALSE; | |
1059 | } | |
4e011fb5 AM |
1060 | if (name[1] != 'z') |
1061 | { | |
1062 | unsigned int len = strlen (name); | |
1063 | ||
1064 | new_name = bfd_alloc (abfd, len + 2); | |
1065 | if (new_name == NULL) | |
1066 | return FALSE; | |
1067 | new_name[0] = '.'; | |
1068 | new_name[1] = 'z'; | |
1069 | memcpy (new_name + 2, name + 1, len); | |
1070 | } | |
4a114e3e L |
1071 | break; |
1072 | case decompress: | |
1073 | if (!bfd_init_section_decompress_status (abfd, newsect)) | |
1074 | { | |
1075 | (*_bfd_error_handler) | |
bc823199 | 1076 | (_("%B: unable to initialize decompress status for section %s"), |
4a114e3e L |
1077 | abfd, name); |
1078 | return FALSE; | |
1079 | } | |
4e011fb5 AM |
1080 | if (name[1] == 'z') |
1081 | { | |
1082 | unsigned int len = strlen (name); | |
1083 | ||
1084 | new_name = bfd_alloc (abfd, len); | |
1085 | if (new_name == NULL) | |
1086 | return FALSE; | |
1087 | new_name[0] = '.'; | |
1088 | memcpy (new_name + 1, name + 2, len - 1); | |
1089 | } | |
4a114e3e L |
1090 | break; |
1091 | } | |
4e011fb5 AM |
1092 | if (new_name != NULL) |
1093 | bfd_rename_section (abfd, newsect, new_name); | |
4a114e3e L |
1094 | } |
1095 | ||
b34976b6 | 1096 | return TRUE; |
252b5132 RH |
1097 | } |
1098 | ||
252b5132 RH |
1099 | const char *const bfd_elf_section_type_names[] = { |
1100 | "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB", | |
1101 | "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE", | |
1102 | "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM", | |
1103 | }; | |
1104 | ||
1049f94e | 1105 | /* ELF relocs are against symbols. If we are producing relocatable |
252b5132 RH |
1106 | output, and the reloc is against an external symbol, and nothing |
1107 | has given us any additional addend, the resulting reloc will also | |
1108 | be against the same symbol. In such a case, we don't want to | |
1109 | change anything about the way the reloc is handled, since it will | |
1110 | all be done at final link time. Rather than put special case code | |
1111 | into bfd_perform_relocation, all the reloc types use this howto | |
1112 | function. It just short circuits the reloc if producing | |
1049f94e | 1113 | relocatable output against an external symbol. */ |
252b5132 | 1114 | |
252b5132 | 1115 | bfd_reloc_status_type |
217aa764 AM |
1116 | bfd_elf_generic_reloc (bfd *abfd ATTRIBUTE_UNUSED, |
1117 | arelent *reloc_entry, | |
1118 | asymbol *symbol, | |
1119 | void *data ATTRIBUTE_UNUSED, | |
1120 | asection *input_section, | |
1121 | bfd *output_bfd, | |
1122 | char **error_message ATTRIBUTE_UNUSED) | |
1123 | { | |
1124 | if (output_bfd != NULL | |
252b5132 RH |
1125 | && (symbol->flags & BSF_SECTION_SYM) == 0 |
1126 | && (! reloc_entry->howto->partial_inplace | |
1127 | || reloc_entry->addend == 0)) | |
1128 | { | |
1129 | reloc_entry->address += input_section->output_offset; | |
1130 | return bfd_reloc_ok; | |
1131 | } | |
1132 | ||
1133 | return bfd_reloc_continue; | |
1134 | } | |
1135 | \f | |
0ac4564e L |
1136 | /* Copy the program header and other data from one object module to |
1137 | another. */ | |
252b5132 | 1138 | |
b34976b6 | 1139 | bfd_boolean |
217aa764 | 1140 | _bfd_elf_copy_private_bfd_data (bfd *ibfd, bfd *obfd) |
2d502050 L |
1141 | { |
1142 | if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour | |
1143 | || bfd_get_flavour (obfd) != bfd_target_elf_flavour) | |
b34976b6 | 1144 | return TRUE; |
2d502050 | 1145 | |
57b828ef L |
1146 | if (!elf_flags_init (obfd)) |
1147 | { | |
1148 | elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags; | |
1149 | elf_flags_init (obfd) = TRUE; | |
1150 | } | |
2d502050 | 1151 | |
0ac4564e | 1152 | elf_gp (obfd) = elf_gp (ibfd); |
57b828ef L |
1153 | |
1154 | /* Also copy the EI_OSABI field. */ | |
1155 | elf_elfheader (obfd)->e_ident[EI_OSABI] = | |
1156 | elf_elfheader (ibfd)->e_ident[EI_OSABI]; | |
104d59d1 JM |
1157 | |
1158 | /* Copy object attributes. */ | |
1159 | _bfd_elf_copy_obj_attributes (ibfd, obfd); | |
b34976b6 | 1160 | return TRUE; |
2d502050 L |
1161 | } |
1162 | ||
cedc298e L |
1163 | static const char * |
1164 | get_segment_type (unsigned int p_type) | |
1165 | { | |
1166 | const char *pt; | |
1167 | switch (p_type) | |
1168 | { | |
1169 | case PT_NULL: pt = "NULL"; break; | |
1170 | case PT_LOAD: pt = "LOAD"; break; | |
1171 | case PT_DYNAMIC: pt = "DYNAMIC"; break; | |
1172 | case PT_INTERP: pt = "INTERP"; break; | |
1173 | case PT_NOTE: pt = "NOTE"; break; | |
1174 | case PT_SHLIB: pt = "SHLIB"; break; | |
1175 | case PT_PHDR: pt = "PHDR"; break; | |
1176 | case PT_TLS: pt = "TLS"; break; | |
1177 | case PT_GNU_EH_FRAME: pt = "EH_FRAME"; break; | |
2b05f1b7 | 1178 | case PT_GNU_STACK: pt = "STACK"; break; |
cedc298e L |
1179 | case PT_GNU_RELRO: pt = "RELRO"; break; |
1180 | default: pt = NULL; break; | |
1181 | } | |
1182 | return pt; | |
1183 | } | |
1184 | ||
f0b79d91 L |
1185 | /* Print out the program headers. */ |
1186 | ||
b34976b6 | 1187 | bfd_boolean |
217aa764 | 1188 | _bfd_elf_print_private_bfd_data (bfd *abfd, void *farg) |
252b5132 | 1189 | { |
a50b1753 | 1190 | FILE *f = (FILE *) farg; |
252b5132 RH |
1191 | Elf_Internal_Phdr *p; |
1192 | asection *s; | |
1193 | bfd_byte *dynbuf = NULL; | |
1194 | ||
1195 | p = elf_tdata (abfd)->phdr; | |
1196 | if (p != NULL) | |
1197 | { | |
1198 | unsigned int i, c; | |
1199 | ||
1200 | fprintf (f, _("\nProgram Header:\n")); | |
1201 | c = elf_elfheader (abfd)->e_phnum; | |
1202 | for (i = 0; i < c; i++, p++) | |
1203 | { | |
cedc298e | 1204 | const char *pt = get_segment_type (p->p_type); |
252b5132 RH |
1205 | char buf[20]; |
1206 | ||
cedc298e | 1207 | if (pt == NULL) |
252b5132 | 1208 | { |
cedc298e L |
1209 | sprintf (buf, "0x%lx", p->p_type); |
1210 | pt = buf; | |
252b5132 | 1211 | } |
dc810e39 | 1212 | fprintf (f, "%8s off 0x", pt); |
60b89a18 | 1213 | bfd_fprintf_vma (abfd, f, p->p_offset); |
252b5132 | 1214 | fprintf (f, " vaddr 0x"); |
60b89a18 | 1215 | bfd_fprintf_vma (abfd, f, p->p_vaddr); |
252b5132 | 1216 | fprintf (f, " paddr 0x"); |
60b89a18 | 1217 | bfd_fprintf_vma (abfd, f, p->p_paddr); |
252b5132 RH |
1218 | fprintf (f, " align 2**%u\n", bfd_log2 (p->p_align)); |
1219 | fprintf (f, " filesz 0x"); | |
60b89a18 | 1220 | bfd_fprintf_vma (abfd, f, p->p_filesz); |
252b5132 | 1221 | fprintf (f, " memsz 0x"); |
60b89a18 | 1222 | bfd_fprintf_vma (abfd, f, p->p_memsz); |
252b5132 RH |
1223 | fprintf (f, " flags %c%c%c", |
1224 | (p->p_flags & PF_R) != 0 ? 'r' : '-', | |
1225 | (p->p_flags & PF_W) != 0 ? 'w' : '-', | |
1226 | (p->p_flags & PF_X) != 0 ? 'x' : '-'); | |
dc810e39 AM |
1227 | if ((p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X)) != 0) |
1228 | fprintf (f, " %lx", p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X)); | |
252b5132 RH |
1229 | fprintf (f, "\n"); |
1230 | } | |
1231 | } | |
1232 | ||
1233 | s = bfd_get_section_by_name (abfd, ".dynamic"); | |
1234 | if (s != NULL) | |
1235 | { | |
cb33740c | 1236 | unsigned int elfsec; |
dc810e39 | 1237 | unsigned long shlink; |
252b5132 RH |
1238 | bfd_byte *extdyn, *extdynend; |
1239 | size_t extdynsize; | |
217aa764 | 1240 | void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *); |
252b5132 RH |
1241 | |
1242 | fprintf (f, _("\nDynamic Section:\n")); | |
1243 | ||
eea6121a | 1244 | if (!bfd_malloc_and_get_section (abfd, s, &dynbuf)) |
252b5132 RH |
1245 | goto error_return; |
1246 | ||
1247 | elfsec = _bfd_elf_section_from_bfd_section (abfd, s); | |
cb33740c | 1248 | if (elfsec == SHN_BAD) |
252b5132 | 1249 | goto error_return; |
dc810e39 | 1250 | shlink = elf_elfsections (abfd)[elfsec]->sh_link; |
252b5132 RH |
1251 | |
1252 | extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn; | |
1253 | swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in; | |
1254 | ||
1255 | extdyn = dynbuf; | |
eea6121a | 1256 | extdynend = extdyn + s->size; |
1036838a NC |
1257 | /* PR 17512: file: id:000006,sig:06,src:000000,op:flip4,pos:5664. |
1258 | Fix range check. */ | |
1259 | for (; extdyn <= (extdynend - extdynsize); extdyn += extdynsize) | |
252b5132 RH |
1260 | { |
1261 | Elf_Internal_Dyn dyn; | |
ad9563d6 | 1262 | const char *name = ""; |
252b5132 | 1263 | char ab[20]; |
b34976b6 | 1264 | bfd_boolean stringp; |
ad9563d6 | 1265 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
252b5132 | 1266 | |
217aa764 | 1267 | (*swap_dyn_in) (abfd, extdyn, &dyn); |
252b5132 RH |
1268 | |
1269 | if (dyn.d_tag == DT_NULL) | |
1270 | break; | |
1271 | ||
b34976b6 | 1272 | stringp = FALSE; |
252b5132 RH |
1273 | switch (dyn.d_tag) |
1274 | { | |
1275 | default: | |
ad9563d6 CM |
1276 | if (bed->elf_backend_get_target_dtag) |
1277 | name = (*bed->elf_backend_get_target_dtag) (dyn.d_tag); | |
1278 | ||
1279 | if (!strcmp (name, "")) | |
1280 | { | |
1281 | sprintf (ab, "0x%lx", (unsigned long) dyn.d_tag); | |
1282 | name = ab; | |
1283 | } | |
252b5132 RH |
1284 | break; |
1285 | ||
b34976b6 | 1286 | case DT_NEEDED: name = "NEEDED"; stringp = TRUE; break; |
252b5132 RH |
1287 | case DT_PLTRELSZ: name = "PLTRELSZ"; break; |
1288 | case DT_PLTGOT: name = "PLTGOT"; break; | |
1289 | case DT_HASH: name = "HASH"; break; | |
1290 | case DT_STRTAB: name = "STRTAB"; break; | |
1291 | case DT_SYMTAB: name = "SYMTAB"; break; | |
1292 | case DT_RELA: name = "RELA"; break; | |
1293 | case DT_RELASZ: name = "RELASZ"; break; | |
1294 | case DT_RELAENT: name = "RELAENT"; break; | |
1295 | case DT_STRSZ: name = "STRSZ"; break; | |
1296 | case DT_SYMENT: name = "SYMENT"; break; | |
1297 | case DT_INIT: name = "INIT"; break; | |
1298 | case DT_FINI: name = "FINI"; break; | |
b34976b6 AM |
1299 | case DT_SONAME: name = "SONAME"; stringp = TRUE; break; |
1300 | case DT_RPATH: name = "RPATH"; stringp = TRUE; break; | |
252b5132 RH |
1301 | case DT_SYMBOLIC: name = "SYMBOLIC"; break; |
1302 | case DT_REL: name = "REL"; break; | |
1303 | case DT_RELSZ: name = "RELSZ"; break; | |
1304 | case DT_RELENT: name = "RELENT"; break; | |
1305 | case DT_PLTREL: name = "PLTREL"; break; | |
1306 | case DT_DEBUG: name = "DEBUG"; break; | |
1307 | case DT_TEXTREL: name = "TEXTREL"; break; | |
1308 | case DT_JMPREL: name = "JMPREL"; break; | |
94558834 L |
1309 | case DT_BIND_NOW: name = "BIND_NOW"; break; |
1310 | case DT_INIT_ARRAY: name = "INIT_ARRAY"; break; | |
1311 | case DT_FINI_ARRAY: name = "FINI_ARRAY"; break; | |
1312 | case DT_INIT_ARRAYSZ: name = "INIT_ARRAYSZ"; break; | |
1313 | case DT_FINI_ARRAYSZ: name = "FINI_ARRAYSZ"; break; | |
b34976b6 | 1314 | case DT_RUNPATH: name = "RUNPATH"; stringp = TRUE; break; |
94558834 L |
1315 | case DT_FLAGS: name = "FLAGS"; break; |
1316 | case DT_PREINIT_ARRAY: name = "PREINIT_ARRAY"; break; | |
1317 | case DT_PREINIT_ARRAYSZ: name = "PREINIT_ARRAYSZ"; break; | |
d48188b9 | 1318 | case DT_CHECKSUM: name = "CHECKSUM"; break; |
94558834 L |
1319 | case DT_PLTPADSZ: name = "PLTPADSZ"; break; |
1320 | case DT_MOVEENT: name = "MOVEENT"; break; | |
1321 | case DT_MOVESZ: name = "MOVESZ"; break; | |
1322 | case DT_FEATURE: name = "FEATURE"; break; | |
1323 | case DT_POSFLAG_1: name = "POSFLAG_1"; break; | |
1324 | case DT_SYMINSZ: name = "SYMINSZ"; break; | |
1325 | case DT_SYMINENT: name = "SYMINENT"; break; | |
b34976b6 AM |
1326 | case DT_CONFIG: name = "CONFIG"; stringp = TRUE; break; |
1327 | case DT_DEPAUDIT: name = "DEPAUDIT"; stringp = TRUE; break; | |
1328 | case DT_AUDIT: name = "AUDIT"; stringp = TRUE; break; | |
94558834 L |
1329 | case DT_PLTPAD: name = "PLTPAD"; break; |
1330 | case DT_MOVETAB: name = "MOVETAB"; break; | |
1331 | case DT_SYMINFO: name = "SYMINFO"; break; | |
1332 | case DT_RELACOUNT: name = "RELACOUNT"; break; | |
1333 | case DT_RELCOUNT: name = "RELCOUNT"; break; | |
1334 | case DT_FLAGS_1: name = "FLAGS_1"; break; | |
252b5132 RH |
1335 | case DT_VERSYM: name = "VERSYM"; break; |
1336 | case DT_VERDEF: name = "VERDEF"; break; | |
1337 | case DT_VERDEFNUM: name = "VERDEFNUM"; break; | |
1338 | case DT_VERNEED: name = "VERNEED"; break; | |
1339 | case DT_VERNEEDNUM: name = "VERNEEDNUM"; break; | |
b34976b6 | 1340 | case DT_AUXILIARY: name = "AUXILIARY"; stringp = TRUE; break; |
94558834 | 1341 | case DT_USED: name = "USED"; break; |
b34976b6 | 1342 | case DT_FILTER: name = "FILTER"; stringp = TRUE; break; |
fdc90cb4 | 1343 | case DT_GNU_HASH: name = "GNU_HASH"; break; |
252b5132 RH |
1344 | } |
1345 | ||
ad9563d6 | 1346 | fprintf (f, " %-20s ", name); |
252b5132 | 1347 | if (! stringp) |
a1f3c56e AN |
1348 | { |
1349 | fprintf (f, "0x"); | |
1350 | bfd_fprintf_vma (abfd, f, dyn.d_un.d_val); | |
1351 | } | |
252b5132 RH |
1352 | else |
1353 | { | |
1354 | const char *string; | |
dc810e39 | 1355 | unsigned int tagv = dyn.d_un.d_val; |
252b5132 | 1356 | |
dc810e39 | 1357 | string = bfd_elf_string_from_elf_section (abfd, shlink, tagv); |
252b5132 RH |
1358 | if (string == NULL) |
1359 | goto error_return; | |
1360 | fprintf (f, "%s", string); | |
1361 | } | |
1362 | fprintf (f, "\n"); | |
1363 | } | |
1364 | ||
1365 | free (dynbuf); | |
1366 | dynbuf = NULL; | |
1367 | } | |
1368 | ||
1369 | if ((elf_dynverdef (abfd) != 0 && elf_tdata (abfd)->verdef == NULL) | |
1370 | || (elf_dynverref (abfd) != 0 && elf_tdata (abfd)->verref == NULL)) | |
1371 | { | |
fc0e6df6 | 1372 | if (! _bfd_elf_slurp_version_tables (abfd, FALSE)) |
b34976b6 | 1373 | return FALSE; |
252b5132 RH |
1374 | } |
1375 | ||
1376 | if (elf_dynverdef (abfd) != 0) | |
1377 | { | |
1378 | Elf_Internal_Verdef *t; | |
1379 | ||
1380 | fprintf (f, _("\nVersion definitions:\n")); | |
1381 | for (t = elf_tdata (abfd)->verdef; t != NULL; t = t->vd_nextdef) | |
1382 | { | |
1383 | fprintf (f, "%d 0x%2.2x 0x%8.8lx %s\n", t->vd_ndx, | |
d0fb9a8d JJ |
1384 | t->vd_flags, t->vd_hash, |
1385 | t->vd_nodename ? t->vd_nodename : "<corrupt>"); | |
1386 | if (t->vd_auxptr != NULL && t->vd_auxptr->vda_nextptr != NULL) | |
252b5132 RH |
1387 | { |
1388 | Elf_Internal_Verdaux *a; | |
1389 | ||
1390 | fprintf (f, "\t"); | |
1391 | for (a = t->vd_auxptr->vda_nextptr; | |
1392 | a != NULL; | |
1393 | a = a->vda_nextptr) | |
d0fb9a8d JJ |
1394 | fprintf (f, "%s ", |
1395 | a->vda_nodename ? a->vda_nodename : "<corrupt>"); | |
252b5132 RH |
1396 | fprintf (f, "\n"); |
1397 | } | |
1398 | } | |
1399 | } | |
1400 | ||
1401 | if (elf_dynverref (abfd) != 0) | |
1402 | { | |
1403 | Elf_Internal_Verneed *t; | |
1404 | ||
1405 | fprintf (f, _("\nVersion References:\n")); | |
1406 | for (t = elf_tdata (abfd)->verref; t != NULL; t = t->vn_nextref) | |
1407 | { | |
1408 | Elf_Internal_Vernaux *a; | |
1409 | ||
d0fb9a8d JJ |
1410 | fprintf (f, _(" required from %s:\n"), |
1411 | t->vn_filename ? t->vn_filename : "<corrupt>"); | |
252b5132 RH |
1412 | for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) |
1413 | fprintf (f, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a->vna_hash, | |
d0fb9a8d JJ |
1414 | a->vna_flags, a->vna_other, |
1415 | a->vna_nodename ? a->vna_nodename : "<corrupt>"); | |
252b5132 RH |
1416 | } |
1417 | } | |
1418 | ||
b34976b6 | 1419 | return TRUE; |
252b5132 RH |
1420 | |
1421 | error_return: | |
1422 | if (dynbuf != NULL) | |
1423 | free (dynbuf); | |
b34976b6 | 1424 | return FALSE; |
252b5132 RH |
1425 | } |
1426 | ||
bb4d2ac2 L |
1427 | /* Get version string. */ |
1428 | ||
1429 | const char * | |
60bb06bc L |
1430 | _bfd_elf_get_symbol_version_string (bfd *abfd, asymbol *symbol, |
1431 | bfd_boolean *hidden) | |
bb4d2ac2 L |
1432 | { |
1433 | const char *version_string = NULL; | |
1434 | if (elf_dynversym (abfd) != 0 | |
1435 | && (elf_dynverdef (abfd) != 0 || elf_dynverref (abfd) != 0)) | |
1436 | { | |
1437 | unsigned int vernum = ((elf_symbol_type *) symbol)->version; | |
1438 | ||
1439 | *hidden = (vernum & VERSYM_HIDDEN) != 0; | |
1440 | vernum &= VERSYM_VERSION; | |
1441 | ||
1442 | if (vernum == 0) | |
1443 | version_string = ""; | |
1444 | else if (vernum == 1) | |
1445 | version_string = "Base"; | |
1446 | else if (vernum <= elf_tdata (abfd)->cverdefs) | |
1447 | version_string = | |
1448 | elf_tdata (abfd)->verdef[vernum - 1].vd_nodename; | |
1449 | else | |
1450 | { | |
1451 | Elf_Internal_Verneed *t; | |
1452 | ||
1453 | version_string = ""; | |
1454 | for (t = elf_tdata (abfd)->verref; | |
1455 | t != NULL; | |
1456 | t = t->vn_nextref) | |
1457 | { | |
1458 | Elf_Internal_Vernaux *a; | |
1459 | ||
1460 | for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) | |
1461 | { | |
1462 | if (a->vna_other == vernum) | |
1463 | { | |
1464 | version_string = a->vna_nodename; | |
1465 | break; | |
1466 | } | |
1467 | } | |
1468 | } | |
1469 | } | |
1470 | } | |
1471 | return version_string; | |
1472 | } | |
1473 | ||
252b5132 RH |
1474 | /* Display ELF-specific fields of a symbol. */ |
1475 | ||
1476 | void | |
217aa764 AM |
1477 | bfd_elf_print_symbol (bfd *abfd, |
1478 | void *filep, | |
1479 | asymbol *symbol, | |
1480 | bfd_print_symbol_type how) | |
252b5132 | 1481 | { |
a50b1753 | 1482 | FILE *file = (FILE *) filep; |
252b5132 RH |
1483 | switch (how) |
1484 | { | |
1485 | case bfd_print_symbol_name: | |
1486 | fprintf (file, "%s", symbol->name); | |
1487 | break; | |
1488 | case bfd_print_symbol_more: | |
1489 | fprintf (file, "elf "); | |
60b89a18 | 1490 | bfd_fprintf_vma (abfd, file, symbol->value); |
0af1713e | 1491 | fprintf (file, " %lx", (unsigned long) symbol->flags); |
252b5132 RH |
1492 | break; |
1493 | case bfd_print_symbol_all: | |
1494 | { | |
4e8a9624 AM |
1495 | const char *section_name; |
1496 | const char *name = NULL; | |
9c5bfbb7 | 1497 | const struct elf_backend_data *bed; |
7a13edea | 1498 | unsigned char st_other; |
dbb410c3 | 1499 | bfd_vma val; |
bb4d2ac2 L |
1500 | const char *version_string; |
1501 | bfd_boolean hidden; | |
c044fabd | 1502 | |
252b5132 | 1503 | section_name = symbol->section ? symbol->section->name : "(*none*)"; |
587ff49e RH |
1504 | |
1505 | bed = get_elf_backend_data (abfd); | |
1506 | if (bed->elf_backend_print_symbol_all) | |
c044fabd | 1507 | name = (*bed->elf_backend_print_symbol_all) (abfd, filep, symbol); |
587ff49e RH |
1508 | |
1509 | if (name == NULL) | |
1510 | { | |
7ee38065 | 1511 | name = symbol->name; |
217aa764 | 1512 | bfd_print_symbol_vandf (abfd, file, symbol); |
587ff49e RH |
1513 | } |
1514 | ||
252b5132 RH |
1515 | fprintf (file, " %s\t", section_name); |
1516 | /* Print the "other" value for a symbol. For common symbols, | |
1517 | we've already printed the size; now print the alignment. | |
1518 | For other symbols, we have no specified alignment, and | |
1519 | we've printed the address; now print the size. */ | |
dcf6c779 | 1520 | if (symbol->section && bfd_is_com_section (symbol->section)) |
dbb410c3 AM |
1521 | val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_value; |
1522 | else | |
1523 | val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_size; | |
1524 | bfd_fprintf_vma (abfd, file, val); | |
252b5132 RH |
1525 | |
1526 | /* If we have version information, print it. */ | |
60bb06bc L |
1527 | version_string = _bfd_elf_get_symbol_version_string (abfd, |
1528 | symbol, | |
1529 | &hidden); | |
bb4d2ac2 | 1530 | if (version_string) |
252b5132 | 1531 | { |
bb4d2ac2 | 1532 | if (!hidden) |
252b5132 RH |
1533 | fprintf (file, " %-11s", version_string); |
1534 | else | |
1535 | { | |
1536 | int i; | |
1537 | ||
1538 | fprintf (file, " (%s)", version_string); | |
1539 | for (i = 10 - strlen (version_string); i > 0; --i) | |
1540 | putc (' ', file); | |
1541 | } | |
1542 | } | |
1543 | ||
1544 | /* If the st_other field is not zero, print it. */ | |
7a13edea | 1545 | st_other = ((elf_symbol_type *) symbol)->internal_elf_sym.st_other; |
c044fabd | 1546 | |
7a13edea NC |
1547 | switch (st_other) |
1548 | { | |
1549 | case 0: break; | |
1550 | case STV_INTERNAL: fprintf (file, " .internal"); break; | |
1551 | case STV_HIDDEN: fprintf (file, " .hidden"); break; | |
1552 | case STV_PROTECTED: fprintf (file, " .protected"); break; | |
1553 | default: | |
1554 | /* Some other non-defined flags are also present, so print | |
1555 | everything hex. */ | |
1556 | fprintf (file, " 0x%02x", (unsigned int) st_other); | |
1557 | } | |
252b5132 | 1558 | |
587ff49e | 1559 | fprintf (file, " %s", name); |
252b5132 RH |
1560 | } |
1561 | break; | |
1562 | } | |
1563 | } | |
252b5132 | 1564 | |
252b5132 RH |
1565 | /* Allocate an ELF string table--force the first byte to be zero. */ |
1566 | ||
1567 | struct bfd_strtab_hash * | |
217aa764 | 1568 | _bfd_elf_stringtab_init (void) |
252b5132 RH |
1569 | { |
1570 | struct bfd_strtab_hash *ret; | |
1571 | ||
1572 | ret = _bfd_stringtab_init (); | |
1573 | if (ret != NULL) | |
1574 | { | |
1575 | bfd_size_type loc; | |
1576 | ||
b34976b6 | 1577 | loc = _bfd_stringtab_add (ret, "", TRUE, FALSE); |
252b5132 RH |
1578 | BFD_ASSERT (loc == 0 || loc == (bfd_size_type) -1); |
1579 | if (loc == (bfd_size_type) -1) | |
1580 | { | |
1581 | _bfd_stringtab_free (ret); | |
1582 | ret = NULL; | |
1583 | } | |
1584 | } | |
1585 | return ret; | |
1586 | } | |
1587 | \f | |
1588 | /* ELF .o/exec file reading */ | |
1589 | ||
c044fabd | 1590 | /* Create a new bfd section from an ELF section header. */ |
252b5132 | 1591 | |
b34976b6 | 1592 | bfd_boolean |
217aa764 | 1593 | bfd_section_from_shdr (bfd *abfd, unsigned int shindex) |
252b5132 | 1594 | { |
4fbb74a6 AM |
1595 | Elf_Internal_Shdr *hdr; |
1596 | Elf_Internal_Ehdr *ehdr; | |
1597 | const struct elf_backend_data *bed; | |
90937f86 | 1598 | const char *name; |
bf67003b NC |
1599 | bfd_boolean ret = TRUE; |
1600 | static bfd_boolean * sections_being_created = NULL; | |
5a4b0ccc | 1601 | static bfd * sections_being_created_abfd = NULL; |
bf67003b | 1602 | static unsigned int nesting = 0; |
252b5132 | 1603 | |
4fbb74a6 AM |
1604 | if (shindex >= elf_numsections (abfd)) |
1605 | return FALSE; | |
1606 | ||
bf67003b NC |
1607 | if (++ nesting > 3) |
1608 | { | |
1609 | /* PR17512: A corrupt ELF binary might contain a recursive group of | |
1610 | sections, each the string indicies pointing to the next in the | |
1611 | loop. Detect this here, by refusing to load a section that we are | |
1612 | already in the process of loading. We only trigger this test if | |
1613 | we have nested at least three sections deep as normal ELF binaries | |
5a4b0ccc NC |
1614 | can expect to recurse at least once. |
1615 | ||
1616 | FIXME: It would be better if this array was attached to the bfd, | |
1617 | rather than being held in a static pointer. */ | |
1618 | ||
1619 | if (sections_being_created_abfd != abfd) | |
1620 | sections_being_created = NULL; | |
bf67003b NC |
1621 | if (sections_being_created == NULL) |
1622 | { | |
1623 | /* FIXME: It would be more efficient to attach this array to the bfd somehow. */ | |
1624 | sections_being_created = (bfd_boolean *) | |
1625 | bfd_zalloc (abfd, elf_numsections (abfd) * sizeof (bfd_boolean)); | |
5a4b0ccc | 1626 | sections_being_created_abfd = abfd; |
bf67003b NC |
1627 | } |
1628 | if (sections_being_created [shindex]) | |
1629 | { | |
1630 | (*_bfd_error_handler) | |
1631 | (_("%B: warning: loop in section dependencies detected"), abfd); | |
1632 | return FALSE; | |
1633 | } | |
1634 | sections_being_created [shindex] = TRUE; | |
1635 | } | |
1636 | ||
4fbb74a6 AM |
1637 | hdr = elf_elfsections (abfd)[shindex]; |
1638 | ehdr = elf_elfheader (abfd); | |
1639 | name = bfd_elf_string_from_elf_section (abfd, ehdr->e_shstrndx, | |
1b3a8575 | 1640 | hdr->sh_name); |
933d961a | 1641 | if (name == NULL) |
bf67003b | 1642 | goto fail; |
252b5132 | 1643 | |
4fbb74a6 | 1644 | bed = get_elf_backend_data (abfd); |
252b5132 RH |
1645 | switch (hdr->sh_type) |
1646 | { | |
1647 | case SHT_NULL: | |
1648 | /* Inactive section. Throw it away. */ | |
bf67003b | 1649 | goto success; |
252b5132 | 1650 | |
bf67003b NC |
1651 | case SHT_PROGBITS: /* Normal section with contents. */ |
1652 | case SHT_NOBITS: /* .bss section. */ | |
1653 | case SHT_HASH: /* .hash section. */ | |
1654 | case SHT_NOTE: /* .note section. */ | |
25e27870 L |
1655 | case SHT_INIT_ARRAY: /* .init_array section. */ |
1656 | case SHT_FINI_ARRAY: /* .fini_array section. */ | |
1657 | case SHT_PREINIT_ARRAY: /* .preinit_array section. */ | |
7f1204bb | 1658 | case SHT_GNU_LIBLIST: /* .gnu.liblist section. */ |
fdc90cb4 | 1659 | case SHT_GNU_HASH: /* .gnu.hash section. */ |
bf67003b NC |
1660 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); |
1661 | goto success; | |
252b5132 | 1662 | |
797fc050 | 1663 | case SHT_DYNAMIC: /* Dynamic linking information. */ |
6dc132d9 | 1664 | if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex)) |
bf67003b NC |
1665 | goto fail; |
1666 | ||
cfcac11d NC |
1667 | if (hdr->sh_link > elf_numsections (abfd)) |
1668 | { | |
caa83f8b | 1669 | /* PR 10478: Accept Solaris binaries with a sh_link |
cfcac11d NC |
1670 | field set to SHN_BEFORE or SHN_AFTER. */ |
1671 | switch (bfd_get_arch (abfd)) | |
1672 | { | |
caa83f8b | 1673 | case bfd_arch_i386: |
cfcac11d NC |
1674 | case bfd_arch_sparc: |
1675 | if (hdr->sh_link == (SHN_LORESERVE & 0xffff) /* SHN_BEFORE */ | |
1676 | || hdr->sh_link == ((SHN_LORESERVE + 1) & 0xffff) /* SHN_AFTER */) | |
1677 | break; | |
1678 | /* Otherwise fall through. */ | |
1679 | default: | |
bf67003b | 1680 | goto fail; |
cfcac11d NC |
1681 | } |
1682 | } | |
1683 | else if (elf_elfsections (abfd)[hdr->sh_link] == NULL) | |
bf67003b | 1684 | goto fail; |
cfcac11d | 1685 | else if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_STRTAB) |
797fc050 AM |
1686 | { |
1687 | Elf_Internal_Shdr *dynsymhdr; | |
1688 | ||
1689 | /* The shared libraries distributed with hpux11 have a bogus | |
1690 | sh_link field for the ".dynamic" section. Find the | |
1691 | string table for the ".dynsym" section instead. */ | |
1692 | if (elf_dynsymtab (abfd) != 0) | |
1693 | { | |
1694 | dynsymhdr = elf_elfsections (abfd)[elf_dynsymtab (abfd)]; | |
1695 | hdr->sh_link = dynsymhdr->sh_link; | |
1696 | } | |
1697 | else | |
1698 | { | |
1699 | unsigned int i, num_sec; | |
1700 | ||
1701 | num_sec = elf_numsections (abfd); | |
1702 | for (i = 1; i < num_sec; i++) | |
1703 | { | |
1704 | dynsymhdr = elf_elfsections (abfd)[i]; | |
1705 | if (dynsymhdr->sh_type == SHT_DYNSYM) | |
1706 | { | |
1707 | hdr->sh_link = dynsymhdr->sh_link; | |
1708 | break; | |
1709 | } | |
1710 | } | |
1711 | } | |
1712 | } | |
bf67003b | 1713 | goto success; |
797fc050 | 1714 | |
bf67003b | 1715 | case SHT_SYMTAB: /* A symbol table. */ |
252b5132 | 1716 | if (elf_onesymtab (abfd) == shindex) |
bf67003b | 1717 | goto success; |
252b5132 | 1718 | |
a50b2160 | 1719 | if (hdr->sh_entsize != bed->s->sizeof_sym) |
bf67003b NC |
1720 | goto fail; |
1721 | ||
3337c1e5 | 1722 | if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size) |
eee3b786 AM |
1723 | { |
1724 | if (hdr->sh_size != 0) | |
bf67003b | 1725 | goto fail; |
eee3b786 AM |
1726 | /* Some assemblers erroneously set sh_info to one with a |
1727 | zero sh_size. ld sees this as a global symbol count | |
1728 | of (unsigned) -1. Fix it here. */ | |
1729 | hdr->sh_info = 0; | |
bf67003b | 1730 | goto success; |
eee3b786 | 1731 | } |
bf67003b | 1732 | |
252b5132 RH |
1733 | BFD_ASSERT (elf_onesymtab (abfd) == 0); |
1734 | elf_onesymtab (abfd) = shindex; | |
1735 | elf_tdata (abfd)->symtab_hdr = *hdr; | |
1736 | elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->symtab_hdr; | |
1737 | abfd->flags |= HAS_SYMS; | |
1738 | ||
1739 | /* Sometimes a shared object will map in the symbol table. If | |
08a40648 AM |
1740 | SHF_ALLOC is set, and this is a shared object, then we also |
1741 | treat this section as a BFD section. We can not base the | |
1742 | decision purely on SHF_ALLOC, because that flag is sometimes | |
1743 | set in a relocatable object file, which would confuse the | |
1744 | linker. */ | |
252b5132 RH |
1745 | if ((hdr->sh_flags & SHF_ALLOC) != 0 |
1746 | && (abfd->flags & DYNAMIC) != 0 | |
6dc132d9 L |
1747 | && ! _bfd_elf_make_section_from_shdr (abfd, hdr, name, |
1748 | shindex)) | |
bf67003b | 1749 | goto fail; |
252b5132 | 1750 | |
1b3a8575 AM |
1751 | /* Go looking for SHT_SYMTAB_SHNDX too, since if there is one we |
1752 | can't read symbols without that section loaded as well. It | |
1753 | is most likely specified by the next section header. */ | |
1754 | if (elf_elfsections (abfd)[elf_symtab_shndx (abfd)]->sh_link != shindex) | |
1755 | { | |
1756 | unsigned int i, num_sec; | |
1757 | ||
1758 | num_sec = elf_numsections (abfd); | |
1759 | for (i = shindex + 1; i < num_sec; i++) | |
1760 | { | |
1761 | Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i]; | |
1762 | if (hdr2->sh_type == SHT_SYMTAB_SHNDX | |
1763 | && hdr2->sh_link == shindex) | |
1764 | break; | |
1765 | } | |
1766 | if (i == num_sec) | |
1767 | for (i = 1; i < shindex; i++) | |
1768 | { | |
1769 | Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i]; | |
1770 | if (hdr2->sh_type == SHT_SYMTAB_SHNDX | |
1771 | && hdr2->sh_link == shindex) | |
1772 | break; | |
1773 | } | |
1774 | if (i != shindex) | |
bf67003b | 1775 | ret = bfd_section_from_shdr (abfd, i); |
1b3a8575 | 1776 | } |
bf67003b | 1777 | goto success; |
252b5132 | 1778 | |
bf67003b | 1779 | case SHT_DYNSYM: /* A dynamic symbol table. */ |
252b5132 | 1780 | if (elf_dynsymtab (abfd) == shindex) |
bf67003b | 1781 | goto success; |
252b5132 | 1782 | |
a50b2160 | 1783 | if (hdr->sh_entsize != bed->s->sizeof_sym) |
bf67003b NC |
1784 | goto fail; |
1785 | ||
eee3b786 AM |
1786 | if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size) |
1787 | { | |
1788 | if (hdr->sh_size != 0) | |
bf67003b NC |
1789 | goto fail; |
1790 | ||
eee3b786 AM |
1791 | /* Some linkers erroneously set sh_info to one with a |
1792 | zero sh_size. ld sees this as a global symbol count | |
1793 | of (unsigned) -1. Fix it here. */ | |
1794 | hdr->sh_info = 0; | |
bf67003b | 1795 | goto success; |
eee3b786 | 1796 | } |
bf67003b | 1797 | |
252b5132 RH |
1798 | BFD_ASSERT (elf_dynsymtab (abfd) == 0); |
1799 | elf_dynsymtab (abfd) = shindex; | |
1800 | elf_tdata (abfd)->dynsymtab_hdr = *hdr; | |
1801 | elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->dynsymtab_hdr; | |
1802 | abfd->flags |= HAS_SYMS; | |
1803 | ||
1804 | /* Besides being a symbol table, we also treat this as a regular | |
1805 | section, so that objcopy can handle it. */ | |
bf67003b NC |
1806 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); |
1807 | goto success; | |
252b5132 | 1808 | |
bf67003b | 1809 | case SHT_SYMTAB_SHNDX: /* Symbol section indices when >64k sections. */ |
9ad5cbcf | 1810 | if (elf_symtab_shndx (abfd) == shindex) |
bf67003b | 1811 | goto success; |
9ad5cbcf | 1812 | |
1b3a8575 | 1813 | BFD_ASSERT (elf_symtab_shndx (abfd) == 0); |
9ad5cbcf AM |
1814 | elf_symtab_shndx (abfd) = shindex; |
1815 | elf_tdata (abfd)->symtab_shndx_hdr = *hdr; | |
1816 | elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->symtab_shndx_hdr; | |
bf67003b | 1817 | goto success; |
9ad5cbcf | 1818 | |
bf67003b | 1819 | case SHT_STRTAB: /* A string table. */ |
252b5132 | 1820 | if (hdr->bfd_section != NULL) |
bf67003b NC |
1821 | goto success; |
1822 | ||
252b5132 RH |
1823 | if (ehdr->e_shstrndx == shindex) |
1824 | { | |
1825 | elf_tdata (abfd)->shstrtab_hdr = *hdr; | |
1826 | elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->shstrtab_hdr; | |
bf67003b | 1827 | goto success; |
252b5132 | 1828 | } |
bf67003b | 1829 | |
1b3a8575 AM |
1830 | if (elf_elfsections (abfd)[elf_onesymtab (abfd)]->sh_link == shindex) |
1831 | { | |
1832 | symtab_strtab: | |
1833 | elf_tdata (abfd)->strtab_hdr = *hdr; | |
1834 | elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->strtab_hdr; | |
bf67003b | 1835 | goto success; |
1b3a8575 | 1836 | } |
bf67003b | 1837 | |
1b3a8575 AM |
1838 | if (elf_elfsections (abfd)[elf_dynsymtab (abfd)]->sh_link == shindex) |
1839 | { | |
1840 | dynsymtab_strtab: | |
1841 | elf_tdata (abfd)->dynstrtab_hdr = *hdr; | |
1842 | hdr = &elf_tdata (abfd)->dynstrtab_hdr; | |
1843 | elf_elfsections (abfd)[shindex] = hdr; | |
1844 | /* We also treat this as a regular section, so that objcopy | |
1845 | can handle it. */ | |
bf67003b NC |
1846 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, |
1847 | shindex); | |
1848 | goto success; | |
1b3a8575 | 1849 | } |
252b5132 | 1850 | |
1b3a8575 AM |
1851 | /* If the string table isn't one of the above, then treat it as a |
1852 | regular section. We need to scan all the headers to be sure, | |
1853 | just in case this strtab section appeared before the above. */ | |
1854 | if (elf_onesymtab (abfd) == 0 || elf_dynsymtab (abfd) == 0) | |
1855 | { | |
1856 | unsigned int i, num_sec; | |
252b5132 | 1857 | |
1b3a8575 AM |
1858 | num_sec = elf_numsections (abfd); |
1859 | for (i = 1; i < num_sec; i++) | |
1860 | { | |
1861 | Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i]; | |
1862 | if (hdr2->sh_link == shindex) | |
1863 | { | |
933d961a JJ |
1864 | /* Prevent endless recursion on broken objects. */ |
1865 | if (i == shindex) | |
bf67003b | 1866 | goto fail; |
1b3a8575 | 1867 | if (! bfd_section_from_shdr (abfd, i)) |
bf67003b | 1868 | goto fail; |
1b3a8575 AM |
1869 | if (elf_onesymtab (abfd) == i) |
1870 | goto symtab_strtab; | |
1871 | if (elf_dynsymtab (abfd) == i) | |
1872 | goto dynsymtab_strtab; | |
1873 | } | |
1874 | } | |
1875 | } | |
bf67003b NC |
1876 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); |
1877 | goto success; | |
252b5132 RH |
1878 | |
1879 | case SHT_REL: | |
1880 | case SHT_RELA: | |
1881 | /* *These* do a lot of work -- but build no sections! */ | |
1882 | { | |
1883 | asection *target_sect; | |
d4730f92 | 1884 | Elf_Internal_Shdr *hdr2, **p_hdr; |
9ad5cbcf | 1885 | unsigned int num_sec = elf_numsections (abfd); |
d4730f92 BS |
1886 | struct bfd_elf_section_data *esdt; |
1887 | bfd_size_type amt; | |
252b5132 | 1888 | |
aa2ca951 JJ |
1889 | if (hdr->sh_entsize |
1890 | != (bfd_size_type) (hdr->sh_type == SHT_REL | |
a50b2160 | 1891 | ? bed->s->sizeof_rel : bed->s->sizeof_rela)) |
bf67003b | 1892 | goto fail; |
a50b2160 | 1893 | |
03ae5f59 | 1894 | /* Check for a bogus link to avoid crashing. */ |
4fbb74a6 | 1895 | if (hdr->sh_link >= num_sec) |
03ae5f59 ILT |
1896 | { |
1897 | ((*_bfd_error_handler) | |
d003868e AM |
1898 | (_("%B: invalid link %lu for reloc section %s (index %u)"), |
1899 | abfd, hdr->sh_link, name, shindex)); | |
bf67003b NC |
1900 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, |
1901 | shindex); | |
1902 | goto success; | |
03ae5f59 ILT |
1903 | } |
1904 | ||
252b5132 RH |
1905 | /* For some incomprehensible reason Oracle distributes |
1906 | libraries for Solaris in which some of the objects have | |
1907 | bogus sh_link fields. It would be nice if we could just | |
1908 | reject them, but, unfortunately, some people need to use | |
1909 | them. We scan through the section headers; if we find only | |
1910 | one suitable symbol table, we clobber the sh_link to point | |
83b89087 L |
1911 | to it. I hope this doesn't break anything. |
1912 | ||
1913 | Don't do it on executable nor shared library. */ | |
1914 | if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0 | |
1915 | && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_SYMTAB | |
252b5132 RH |
1916 | && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_DYNSYM) |
1917 | { | |
9ad5cbcf | 1918 | unsigned int scan; |
252b5132 RH |
1919 | int found; |
1920 | ||
1921 | found = 0; | |
9ad5cbcf | 1922 | for (scan = 1; scan < num_sec; scan++) |
252b5132 RH |
1923 | { |
1924 | if (elf_elfsections (abfd)[scan]->sh_type == SHT_SYMTAB | |
1925 | || elf_elfsections (abfd)[scan]->sh_type == SHT_DYNSYM) | |
1926 | { | |
1927 | if (found != 0) | |
1928 | { | |
1929 | found = 0; | |
1930 | break; | |
1931 | } | |
1932 | found = scan; | |
1933 | } | |
1934 | } | |
1935 | if (found != 0) | |
1936 | hdr->sh_link = found; | |
1937 | } | |
1938 | ||
1939 | /* Get the symbol table. */ | |
1b3a8575 AM |
1940 | if ((elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_SYMTAB |
1941 | || elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_DYNSYM) | |
252b5132 | 1942 | && ! bfd_section_from_shdr (abfd, hdr->sh_link)) |
bf67003b | 1943 | goto fail; |
252b5132 RH |
1944 | |
1945 | /* If this reloc section does not use the main symbol table we | |
1946 | don't treat it as a reloc section. BFD can't adequately | |
1947 | represent such a section, so at least for now, we don't | |
c044fabd | 1948 | try. We just present it as a normal section. We also |
60bcf0fa | 1949 | can't use it as a reloc section if it points to the null |
83b89087 L |
1950 | section, an invalid section, another reloc section, or its |
1951 | sh_link points to the null section. */ | |
185ef66d | 1952 | if (hdr->sh_link != elf_onesymtab (abfd) |
83b89087 | 1953 | || hdr->sh_link == SHN_UNDEF |
185ef66d | 1954 | || hdr->sh_info == SHN_UNDEF |
185ef66d AM |
1955 | || hdr->sh_info >= num_sec |
1956 | || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_REL | |
1957 | || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_RELA) | |
bf67003b NC |
1958 | { |
1959 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, | |
1960 | shindex); | |
1961 | goto success; | |
1962 | } | |
252b5132 RH |
1963 | |
1964 | if (! bfd_section_from_shdr (abfd, hdr->sh_info)) | |
bf67003b NC |
1965 | goto fail; |
1966 | ||
252b5132 RH |
1967 | target_sect = bfd_section_from_elf_index (abfd, hdr->sh_info); |
1968 | if (target_sect == NULL) | |
bf67003b | 1969 | goto fail; |
252b5132 | 1970 | |
d4730f92 BS |
1971 | esdt = elf_section_data (target_sect); |
1972 | if (hdr->sh_type == SHT_RELA) | |
1973 | p_hdr = &esdt->rela.hdr; | |
252b5132 | 1974 | else |
d4730f92 BS |
1975 | p_hdr = &esdt->rel.hdr; |
1976 | ||
1977 | BFD_ASSERT (*p_hdr == NULL); | |
1978 | amt = sizeof (*hdr2); | |
1979 | hdr2 = (Elf_Internal_Shdr *) bfd_alloc (abfd, amt); | |
1980 | if (hdr2 == NULL) | |
bf67003b | 1981 | goto fail; |
252b5132 | 1982 | *hdr2 = *hdr; |
d4730f92 | 1983 | *p_hdr = hdr2; |
252b5132 | 1984 | elf_elfsections (abfd)[shindex] = hdr2; |
d9bc7a44 | 1985 | target_sect->reloc_count += NUM_SHDR_ENTRIES (hdr); |
252b5132 RH |
1986 | target_sect->flags |= SEC_RELOC; |
1987 | target_sect->relocation = NULL; | |
1988 | target_sect->rel_filepos = hdr->sh_offset; | |
bf572ba0 MM |
1989 | /* In the section to which the relocations apply, mark whether |
1990 | its relocations are of the REL or RELA variety. */ | |
72730e0c | 1991 | if (hdr->sh_size != 0) |
d4730f92 BS |
1992 | { |
1993 | if (hdr->sh_type == SHT_RELA) | |
1994 | target_sect->use_rela_p = 1; | |
1995 | } | |
252b5132 | 1996 | abfd->flags |= HAS_RELOC; |
bf67003b | 1997 | goto success; |
252b5132 | 1998 | } |
252b5132 RH |
1999 | |
2000 | case SHT_GNU_verdef: | |
2001 | elf_dynverdef (abfd) = shindex; | |
2002 | elf_tdata (abfd)->dynverdef_hdr = *hdr; | |
bf67003b NC |
2003 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); |
2004 | goto success; | |
252b5132 RH |
2005 | |
2006 | case SHT_GNU_versym: | |
a50b2160 | 2007 | if (hdr->sh_entsize != sizeof (Elf_External_Versym)) |
bf67003b NC |
2008 | goto fail; |
2009 | ||
252b5132 RH |
2010 | elf_dynversym (abfd) = shindex; |
2011 | elf_tdata (abfd)->dynversym_hdr = *hdr; | |
bf67003b NC |
2012 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); |
2013 | goto success; | |
252b5132 RH |
2014 | |
2015 | case SHT_GNU_verneed: | |
2016 | elf_dynverref (abfd) = shindex; | |
2017 | elf_tdata (abfd)->dynverref_hdr = *hdr; | |
bf67003b NC |
2018 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); |
2019 | goto success; | |
252b5132 RH |
2020 | |
2021 | case SHT_SHLIB: | |
bf67003b | 2022 | goto success; |
252b5132 | 2023 | |
dbb410c3 | 2024 | case SHT_GROUP: |
44534af3 | 2025 | if (! IS_VALID_GROUP_SECTION_HEADER (hdr, GRP_ENTRY_SIZE)) |
bf67003b NC |
2026 | goto fail; |
2027 | ||
6dc132d9 | 2028 | if (!_bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex)) |
bf67003b NC |
2029 | goto fail; |
2030 | ||
dbb410c3 AM |
2031 | if (hdr->contents != NULL) |
2032 | { | |
2033 | Elf_Internal_Group *idx = (Elf_Internal_Group *) hdr->contents; | |
1783205a | 2034 | unsigned int n_elt = hdr->sh_size / GRP_ENTRY_SIZE; |
dbb410c3 AM |
2035 | asection *s; |
2036 | ||
b885599b AM |
2037 | if (idx->flags & GRP_COMDAT) |
2038 | hdr->bfd_section->flags | |
2039 | |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD; | |
2040 | ||
45c5e9ed L |
2041 | /* We try to keep the same section order as it comes in. */ |
2042 | idx += n_elt; | |
dbb410c3 | 2043 | while (--n_elt != 0) |
1783205a NC |
2044 | { |
2045 | --idx; | |
2046 | ||
2047 | if (idx->shdr != NULL | |
2048 | && (s = idx->shdr->bfd_section) != NULL | |
2049 | && elf_next_in_group (s) != NULL) | |
2050 | { | |
2051 | elf_next_in_group (hdr->bfd_section) = s; | |
2052 | break; | |
2053 | } | |
2054 | } | |
dbb410c3 | 2055 | } |
bf67003b | 2056 | goto success; |
dbb410c3 | 2057 | |
252b5132 | 2058 | default: |
104d59d1 JM |
2059 | /* Possibly an attributes section. */ |
2060 | if (hdr->sh_type == SHT_GNU_ATTRIBUTES | |
2061 | || hdr->sh_type == bed->obj_attrs_section_type) | |
2062 | { | |
2063 | if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex)) | |
bf67003b | 2064 | goto fail; |
104d59d1 | 2065 | _bfd_elf_parse_attributes (abfd, hdr); |
bf67003b | 2066 | goto success; |
104d59d1 JM |
2067 | } |
2068 | ||
252b5132 | 2069 | /* Check for any processor-specific section types. */ |
3eb70a79 | 2070 | if (bed->elf_backend_section_from_shdr (abfd, hdr, name, shindex)) |
bf67003b | 2071 | goto success; |
3eb70a79 L |
2072 | |
2073 | if (hdr->sh_type >= SHT_LOUSER && hdr->sh_type <= SHT_HIUSER) | |
2074 | { | |
2075 | if ((hdr->sh_flags & SHF_ALLOC) != 0) | |
2076 | /* FIXME: How to properly handle allocated section reserved | |
2077 | for applications? */ | |
2078 | (*_bfd_error_handler) | |
2079 | (_("%B: don't know how to handle allocated, application " | |
2080 | "specific section `%s' [0x%8x]"), | |
2081 | abfd, name, hdr->sh_type); | |
2082 | else | |
bf67003b NC |
2083 | { |
2084 | /* Allow sections reserved for applications. */ | |
2085 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, | |
2086 | shindex); | |
2087 | goto success; | |
2088 | } | |
3eb70a79 L |
2089 | } |
2090 | else if (hdr->sh_type >= SHT_LOPROC | |
2091 | && hdr->sh_type <= SHT_HIPROC) | |
2092 | /* FIXME: We should handle this section. */ | |
2093 | (*_bfd_error_handler) | |
2094 | (_("%B: don't know how to handle processor specific section " | |
2095 | "`%s' [0x%8x]"), | |
2096 | abfd, name, hdr->sh_type); | |
2097 | else if (hdr->sh_type >= SHT_LOOS && hdr->sh_type <= SHT_HIOS) | |
ff15b240 NC |
2098 | { |
2099 | /* Unrecognised OS-specific sections. */ | |
2100 | if ((hdr->sh_flags & SHF_OS_NONCONFORMING) != 0) | |
2101 | /* SHF_OS_NONCONFORMING indicates that special knowledge is | |
08a40648 | 2102 | required to correctly process the section and the file should |
ff15b240 NC |
2103 | be rejected with an error message. */ |
2104 | (*_bfd_error_handler) | |
2105 | (_("%B: don't know how to handle OS specific section " | |
2106 | "`%s' [0x%8x]"), | |
2107 | abfd, name, hdr->sh_type); | |
2108 | else | |
bf67003b NC |
2109 | { |
2110 | /* Otherwise it should be processed. */ | |
2111 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); | |
2112 | goto success; | |
2113 | } | |
ff15b240 | 2114 | } |
3eb70a79 L |
2115 | else |
2116 | /* FIXME: We should handle this section. */ | |
2117 | (*_bfd_error_handler) | |
2118 | (_("%B: don't know how to handle section `%s' [0x%8x]"), | |
2119 | abfd, name, hdr->sh_type); | |
2120 | ||
bf67003b | 2121 | goto fail; |
252b5132 RH |
2122 | } |
2123 | ||
bf67003b NC |
2124 | fail: |
2125 | ret = FALSE; | |
2126 | success: | |
e5b470e2 | 2127 | if (sections_being_created && sections_being_created_abfd == abfd) |
bf67003b NC |
2128 | sections_being_created [shindex] = FALSE; |
2129 | if (-- nesting == 0) | |
5a4b0ccc NC |
2130 | { |
2131 | sections_being_created = NULL; | |
2132 | sections_being_created_abfd = abfd; | |
2133 | } | |
bf67003b | 2134 | return ret; |
252b5132 RH |
2135 | } |
2136 | ||
87d72d41 | 2137 | /* Return the local symbol specified by ABFD, R_SYMNDX. */ |
ec338859 | 2138 | |
87d72d41 AM |
2139 | Elf_Internal_Sym * |
2140 | bfd_sym_from_r_symndx (struct sym_cache *cache, | |
2141 | bfd *abfd, | |
2142 | unsigned long r_symndx) | |
ec338859 | 2143 | { |
ec338859 AM |
2144 | unsigned int ent = r_symndx % LOCAL_SYM_CACHE_SIZE; |
2145 | ||
a5d1b3b5 AM |
2146 | if (cache->abfd != abfd || cache->indx[ent] != r_symndx) |
2147 | { | |
2148 | Elf_Internal_Shdr *symtab_hdr; | |
2149 | unsigned char esym[sizeof (Elf64_External_Sym)]; | |
2150 | Elf_External_Sym_Shndx eshndx; | |
ec338859 | 2151 | |
a5d1b3b5 AM |
2152 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; |
2153 | if (bfd_elf_get_elf_syms (abfd, symtab_hdr, 1, r_symndx, | |
87d72d41 | 2154 | &cache->sym[ent], esym, &eshndx) == NULL) |
a5d1b3b5 | 2155 | return NULL; |
9ad5cbcf | 2156 | |
a5d1b3b5 AM |
2157 | if (cache->abfd != abfd) |
2158 | { | |
2159 | memset (cache->indx, -1, sizeof (cache->indx)); | |
2160 | cache->abfd = abfd; | |
2161 | } | |
2162 | cache->indx[ent] = r_symndx; | |
ec338859 | 2163 | } |
a5d1b3b5 | 2164 | |
87d72d41 | 2165 | return &cache->sym[ent]; |
ec338859 AM |
2166 | } |
2167 | ||
252b5132 RH |
2168 | /* Given an ELF section number, retrieve the corresponding BFD |
2169 | section. */ | |
2170 | ||
2171 | asection * | |
91d6fa6a | 2172 | bfd_section_from_elf_index (bfd *abfd, unsigned int sec_index) |
252b5132 | 2173 | { |
91d6fa6a | 2174 | if (sec_index >= elf_numsections (abfd)) |
252b5132 | 2175 | return NULL; |
91d6fa6a | 2176 | return elf_elfsections (abfd)[sec_index]->bfd_section; |
252b5132 RH |
2177 | } |
2178 | ||
b35d266b | 2179 | static const struct bfd_elf_special_section special_sections_b[] = |
2f89ff8d | 2180 | { |
0112cd26 NC |
2181 | { STRING_COMMA_LEN (".bss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE }, |
2182 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2183 | }; |
2184 | ||
b35d266b | 2185 | static const struct bfd_elf_special_section special_sections_c[] = |
7f4d3958 | 2186 | { |
0112cd26 NC |
2187 | { STRING_COMMA_LEN (".comment"), 0, SHT_PROGBITS, 0 }, |
2188 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2189 | }; |
2190 | ||
b35d266b | 2191 | static const struct bfd_elf_special_section special_sections_d[] = |
7f4d3958 | 2192 | { |
0112cd26 NC |
2193 | { STRING_COMMA_LEN (".data"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE }, |
2194 | { STRING_COMMA_LEN (".data1"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE }, | |
a9a72a65 DE |
2195 | /* There are more DWARF sections than these, but they needn't be added here |
2196 | unless you have to cope with broken compilers that don't emit section | |
2197 | attributes or you want to help the user writing assembler. */ | |
0112cd26 NC |
2198 | { STRING_COMMA_LEN (".debug"), 0, SHT_PROGBITS, 0 }, |
2199 | { STRING_COMMA_LEN (".debug_line"), 0, SHT_PROGBITS, 0 }, | |
2200 | { STRING_COMMA_LEN (".debug_info"), 0, SHT_PROGBITS, 0 }, | |
2201 | { STRING_COMMA_LEN (".debug_abbrev"), 0, SHT_PROGBITS, 0 }, | |
2202 | { STRING_COMMA_LEN (".debug_aranges"), 0, SHT_PROGBITS, 0 }, | |
2203 | { STRING_COMMA_LEN (".dynamic"), 0, SHT_DYNAMIC, SHF_ALLOC }, | |
2204 | { STRING_COMMA_LEN (".dynstr"), 0, SHT_STRTAB, SHF_ALLOC }, | |
2205 | { STRING_COMMA_LEN (".dynsym"), 0, SHT_DYNSYM, SHF_ALLOC }, | |
2206 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2207 | }; |
2208 | ||
b35d266b | 2209 | static const struct bfd_elf_special_section special_sections_f[] = |
7f4d3958 | 2210 | { |
0112cd26 NC |
2211 | { STRING_COMMA_LEN (".fini"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR }, |
2212 | { STRING_COMMA_LEN (".fini_array"), 0, SHT_FINI_ARRAY, SHF_ALLOC + SHF_WRITE }, | |
2213 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2214 | }; |
2215 | ||
b35d266b | 2216 | static const struct bfd_elf_special_section special_sections_g[] = |
7f4d3958 | 2217 | { |
0112cd26 | 2218 | { STRING_COMMA_LEN (".gnu.linkonce.b"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE }, |
68efed41 | 2219 | { STRING_COMMA_LEN (".gnu.lto_"), -1, SHT_PROGBITS, SHF_EXCLUDE }, |
0112cd26 NC |
2220 | { STRING_COMMA_LEN (".got"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE }, |
2221 | { STRING_COMMA_LEN (".gnu.version"), 0, SHT_GNU_versym, 0 }, | |
2222 | { STRING_COMMA_LEN (".gnu.version_d"), 0, SHT_GNU_verdef, 0 }, | |
2223 | { STRING_COMMA_LEN (".gnu.version_r"), 0, SHT_GNU_verneed, 0 }, | |
2224 | { STRING_COMMA_LEN (".gnu.liblist"), 0, SHT_GNU_LIBLIST, SHF_ALLOC }, | |
2225 | { STRING_COMMA_LEN (".gnu.conflict"), 0, SHT_RELA, SHF_ALLOC }, | |
2226 | { STRING_COMMA_LEN (".gnu.hash"), 0, SHT_GNU_HASH, SHF_ALLOC }, | |
2227 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2228 | }; |
2229 | ||
b35d266b | 2230 | static const struct bfd_elf_special_section special_sections_h[] = |
7f4d3958 | 2231 | { |
0112cd26 NC |
2232 | { STRING_COMMA_LEN (".hash"), 0, SHT_HASH, SHF_ALLOC }, |
2233 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2234 | }; |
2235 | ||
b35d266b | 2236 | static const struct bfd_elf_special_section special_sections_i[] = |
7f4d3958 | 2237 | { |
0112cd26 NC |
2238 | { STRING_COMMA_LEN (".init"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR }, |
2239 | { STRING_COMMA_LEN (".init_array"), 0, SHT_INIT_ARRAY, SHF_ALLOC + SHF_WRITE }, | |
2240 | { STRING_COMMA_LEN (".interp"), 0, SHT_PROGBITS, 0 }, | |
2241 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2242 | }; |
2243 | ||
b35d266b | 2244 | static const struct bfd_elf_special_section special_sections_l[] = |
7f4d3958 | 2245 | { |
0112cd26 NC |
2246 | { STRING_COMMA_LEN (".line"), 0, SHT_PROGBITS, 0 }, |
2247 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2248 | }; |
2249 | ||
b35d266b | 2250 | static const struct bfd_elf_special_section special_sections_n[] = |
7f4d3958 | 2251 | { |
0112cd26 NC |
2252 | { STRING_COMMA_LEN (".note.GNU-stack"), 0, SHT_PROGBITS, 0 }, |
2253 | { STRING_COMMA_LEN (".note"), -1, SHT_NOTE, 0 }, | |
2254 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2255 | }; |
2256 | ||
b35d266b | 2257 | static const struct bfd_elf_special_section special_sections_p[] = |
7f4d3958 | 2258 | { |
0112cd26 NC |
2259 | { STRING_COMMA_LEN (".preinit_array"), 0, SHT_PREINIT_ARRAY, SHF_ALLOC + SHF_WRITE }, |
2260 | { STRING_COMMA_LEN (".plt"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR }, | |
2261 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2262 | }; |
2263 | ||
b35d266b | 2264 | static const struct bfd_elf_special_section special_sections_r[] = |
7f4d3958 | 2265 | { |
0112cd26 NC |
2266 | { STRING_COMMA_LEN (".rodata"), -2, SHT_PROGBITS, SHF_ALLOC }, |
2267 | { STRING_COMMA_LEN (".rodata1"), 0, SHT_PROGBITS, SHF_ALLOC }, | |
2268 | { STRING_COMMA_LEN (".rela"), -1, SHT_RELA, 0 }, | |
2269 | { STRING_COMMA_LEN (".rel"), -1, SHT_REL, 0 }, | |
2270 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2271 | }; |
2272 | ||
b35d266b | 2273 | static const struct bfd_elf_special_section special_sections_s[] = |
7f4d3958 | 2274 | { |
0112cd26 NC |
2275 | { STRING_COMMA_LEN (".shstrtab"), 0, SHT_STRTAB, 0 }, |
2276 | { STRING_COMMA_LEN (".strtab"), 0, SHT_STRTAB, 0 }, | |
2277 | { STRING_COMMA_LEN (".symtab"), 0, SHT_SYMTAB, 0 }, | |
60ff4dc4 HPN |
2278 | /* See struct bfd_elf_special_section declaration for the semantics of |
2279 | this special case where .prefix_length != strlen (.prefix). */ | |
2280 | { ".stabstr", 5, 3, SHT_STRTAB, 0 }, | |
0112cd26 | 2281 | { NULL, 0, 0, 0, 0 } |
2f89ff8d L |
2282 | }; |
2283 | ||
b35d266b | 2284 | static const struct bfd_elf_special_section special_sections_t[] = |
7f4d3958 | 2285 | { |
0112cd26 NC |
2286 | { STRING_COMMA_LEN (".text"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR }, |
2287 | { STRING_COMMA_LEN (".tbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS }, | |
2288 | { STRING_COMMA_LEN (".tdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS }, | |
2289 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2290 | }; |
2291 | ||
1b315056 CS |
2292 | static const struct bfd_elf_special_section special_sections_z[] = |
2293 | { | |
2294 | { STRING_COMMA_LEN (".zdebug_line"), 0, SHT_PROGBITS, 0 }, | |
2295 | { STRING_COMMA_LEN (".zdebug_info"), 0, SHT_PROGBITS, 0 }, | |
2296 | { STRING_COMMA_LEN (".zdebug_abbrev"), 0, SHT_PROGBITS, 0 }, | |
2297 | { STRING_COMMA_LEN (".zdebug_aranges"), 0, SHT_PROGBITS, 0 }, | |
2298 | { NULL, 0, 0, 0, 0 } | |
2299 | }; | |
2300 | ||
e4c93b56 | 2301 | static const struct bfd_elf_special_section * const special_sections[] = |
7f4d3958 | 2302 | { |
7f4d3958 | 2303 | special_sections_b, /* 'b' */ |
98ece1b3 | 2304 | special_sections_c, /* 'c' */ |
7f4d3958 L |
2305 | special_sections_d, /* 'd' */ |
2306 | NULL, /* 'e' */ | |
2307 | special_sections_f, /* 'f' */ | |
2308 | special_sections_g, /* 'g' */ | |
2309 | special_sections_h, /* 'h' */ | |
2310 | special_sections_i, /* 'i' */ | |
2311 | NULL, /* 'j' */ | |
2312 | NULL, /* 'k' */ | |
2313 | special_sections_l, /* 'l' */ | |
2314 | NULL, /* 'm' */ | |
2315 | special_sections_n, /* 'n' */ | |
2316 | NULL, /* 'o' */ | |
2317 | special_sections_p, /* 'p' */ | |
2318 | NULL, /* 'q' */ | |
2319 | special_sections_r, /* 'r' */ | |
2320 | special_sections_s, /* 's' */ | |
2321 | special_sections_t, /* 't' */ | |
1b315056 CS |
2322 | NULL, /* 'u' */ |
2323 | NULL, /* 'v' */ | |
2324 | NULL, /* 'w' */ | |
2325 | NULL, /* 'x' */ | |
2326 | NULL, /* 'y' */ | |
2327 | special_sections_z /* 'z' */ | |
7f4d3958 L |
2328 | }; |
2329 | ||
551b43fd AM |
2330 | const struct bfd_elf_special_section * |
2331 | _bfd_elf_get_special_section (const char *name, | |
2332 | const struct bfd_elf_special_section *spec, | |
2333 | unsigned int rela) | |
2f89ff8d L |
2334 | { |
2335 | int i; | |
7f4d3958 | 2336 | int len; |
7f4d3958 | 2337 | |
551b43fd | 2338 | len = strlen (name); |
7f4d3958 | 2339 | |
551b43fd | 2340 | for (i = 0; spec[i].prefix != NULL; i++) |
7dcb9820 AM |
2341 | { |
2342 | int suffix_len; | |
551b43fd | 2343 | int prefix_len = spec[i].prefix_length; |
7dcb9820 AM |
2344 | |
2345 | if (len < prefix_len) | |
2346 | continue; | |
551b43fd | 2347 | if (memcmp (name, spec[i].prefix, prefix_len) != 0) |
7dcb9820 AM |
2348 | continue; |
2349 | ||
551b43fd | 2350 | suffix_len = spec[i].suffix_length; |
7dcb9820 AM |
2351 | if (suffix_len <= 0) |
2352 | { | |
2353 | if (name[prefix_len] != 0) | |
2354 | { | |
2355 | if (suffix_len == 0) | |
2356 | continue; | |
2357 | if (name[prefix_len] != '.' | |
2358 | && (suffix_len == -2 | |
551b43fd | 2359 | || (rela && spec[i].type == SHT_REL))) |
7dcb9820 AM |
2360 | continue; |
2361 | } | |
2362 | } | |
2363 | else | |
2364 | { | |
2365 | if (len < prefix_len + suffix_len) | |
2366 | continue; | |
2367 | if (memcmp (name + len - suffix_len, | |
551b43fd | 2368 | spec[i].prefix + prefix_len, |
7dcb9820 AM |
2369 | suffix_len) != 0) |
2370 | continue; | |
2371 | } | |
551b43fd | 2372 | return &spec[i]; |
7dcb9820 | 2373 | } |
2f89ff8d L |
2374 | |
2375 | return NULL; | |
2376 | } | |
2377 | ||
7dcb9820 | 2378 | const struct bfd_elf_special_section * |
29ef7005 | 2379 | _bfd_elf_get_sec_type_attr (bfd *abfd, asection *sec) |
2f89ff8d | 2380 | { |
551b43fd AM |
2381 | int i; |
2382 | const struct bfd_elf_special_section *spec; | |
29ef7005 | 2383 | const struct elf_backend_data *bed; |
2f89ff8d L |
2384 | |
2385 | /* See if this is one of the special sections. */ | |
551b43fd AM |
2386 | if (sec->name == NULL) |
2387 | return NULL; | |
2f89ff8d | 2388 | |
29ef7005 L |
2389 | bed = get_elf_backend_data (abfd); |
2390 | spec = bed->special_sections; | |
2391 | if (spec) | |
2392 | { | |
2393 | spec = _bfd_elf_get_special_section (sec->name, | |
2394 | bed->special_sections, | |
2395 | sec->use_rela_p); | |
2396 | if (spec != NULL) | |
2397 | return spec; | |
2398 | } | |
2399 | ||
551b43fd AM |
2400 | if (sec->name[0] != '.') |
2401 | return NULL; | |
2f89ff8d | 2402 | |
551b43fd | 2403 | i = sec->name[1] - 'b'; |
1b315056 | 2404 | if (i < 0 || i > 'z' - 'b') |
551b43fd AM |
2405 | return NULL; |
2406 | ||
2407 | spec = special_sections[i]; | |
2f89ff8d | 2408 | |
551b43fd AM |
2409 | if (spec == NULL) |
2410 | return NULL; | |
2411 | ||
2412 | return _bfd_elf_get_special_section (sec->name, spec, sec->use_rela_p); | |
2f89ff8d L |
2413 | } |
2414 | ||
b34976b6 | 2415 | bfd_boolean |
217aa764 | 2416 | _bfd_elf_new_section_hook (bfd *abfd, asection *sec) |
252b5132 RH |
2417 | { |
2418 | struct bfd_elf_section_data *sdata; | |
551b43fd | 2419 | const struct elf_backend_data *bed; |
7dcb9820 | 2420 | const struct bfd_elf_special_section *ssect; |
252b5132 | 2421 | |
f0abc2a1 AM |
2422 | sdata = (struct bfd_elf_section_data *) sec->used_by_bfd; |
2423 | if (sdata == NULL) | |
2424 | { | |
a50b1753 NC |
2425 | sdata = (struct bfd_elf_section_data *) bfd_zalloc (abfd, |
2426 | sizeof (*sdata)); | |
f0abc2a1 AM |
2427 | if (sdata == NULL) |
2428 | return FALSE; | |
217aa764 | 2429 | sec->used_by_bfd = sdata; |
f0abc2a1 | 2430 | } |
bf572ba0 | 2431 | |
551b43fd AM |
2432 | /* Indicate whether or not this section should use RELA relocations. */ |
2433 | bed = get_elf_backend_data (abfd); | |
2434 | sec->use_rela_p = bed->default_use_rela_p; | |
2435 | ||
e843e0f8 L |
2436 | /* When we read a file, we don't need to set ELF section type and |
2437 | flags. They will be overridden in _bfd_elf_make_section_from_shdr | |
2438 | anyway. We will set ELF section type and flags for all linker | |
2439 | created sections. If user specifies BFD section flags, we will | |
2440 | set ELF section type and flags based on BFD section flags in | |
02ecc8e9 L |
2441 | elf_fake_sections. Special handling for .init_array/.fini_array |
2442 | output sections since they may contain .ctors/.dtors input | |
2443 | sections. We don't want _bfd_elf_init_private_section_data to | |
2444 | copy ELF section type from .ctors/.dtors input sections. */ | |
2445 | if (abfd->direction != read_direction | |
3496cb2a | 2446 | || (sec->flags & SEC_LINKER_CREATED) != 0) |
2f89ff8d | 2447 | { |
551b43fd | 2448 | ssect = (*bed->get_sec_type_attr) (abfd, sec); |
02ecc8e9 L |
2449 | if (ssect != NULL |
2450 | && (!sec->flags | |
2451 | || (sec->flags & SEC_LINKER_CREATED) != 0 | |
2452 | || ssect->type == SHT_INIT_ARRAY | |
2453 | || ssect->type == SHT_FINI_ARRAY)) | |
a31501e9 L |
2454 | { |
2455 | elf_section_type (sec) = ssect->type; | |
2456 | elf_section_flags (sec) = ssect->attr; | |
2457 | } | |
2f89ff8d L |
2458 | } |
2459 | ||
f592407e | 2460 | return _bfd_generic_new_section_hook (abfd, sec); |
252b5132 RH |
2461 | } |
2462 | ||
2463 | /* Create a new bfd section from an ELF program header. | |
2464 | ||
2465 | Since program segments have no names, we generate a synthetic name | |
2466 | of the form segment<NUM>, where NUM is generally the index in the | |
2467 | program header table. For segments that are split (see below) we | |
2468 | generate the names segment<NUM>a and segment<NUM>b. | |
2469 | ||
2470 | Note that some program segments may have a file size that is different than | |
2471 | (less than) the memory size. All this means is that at execution the | |
2472 | system must allocate the amount of memory specified by the memory size, | |
2473 | but only initialize it with the first "file size" bytes read from the | |
2474 | file. This would occur for example, with program segments consisting | |
2475 | of combined data+bss. | |
2476 | ||
2477 | To handle the above situation, this routine generates TWO bfd sections | |
2478 | for the single program segment. The first has the length specified by | |
2479 | the file size of the segment, and the second has the length specified | |
2480 | by the difference between the two sizes. In effect, the segment is split | |
d5191d0c | 2481 | into its initialized and uninitialized parts. |
252b5132 RH |
2482 | |
2483 | */ | |
2484 | ||
b34976b6 | 2485 | bfd_boolean |
217aa764 AM |
2486 | _bfd_elf_make_section_from_phdr (bfd *abfd, |
2487 | Elf_Internal_Phdr *hdr, | |
91d6fa6a | 2488 | int hdr_index, |
a50b1753 | 2489 | const char *type_name) |
252b5132 RH |
2490 | { |
2491 | asection *newsect; | |
2492 | char *name; | |
2493 | char namebuf[64]; | |
d4c88bbb | 2494 | size_t len; |
252b5132 RH |
2495 | int split; |
2496 | ||
2497 | split = ((hdr->p_memsz > 0) | |
2498 | && (hdr->p_filesz > 0) | |
2499 | && (hdr->p_memsz > hdr->p_filesz)); | |
d5191d0c AM |
2500 | |
2501 | if (hdr->p_filesz > 0) | |
252b5132 | 2502 | { |
91d6fa6a | 2503 | sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "a" : ""); |
d5191d0c | 2504 | len = strlen (namebuf) + 1; |
a50b1753 | 2505 | name = (char *) bfd_alloc (abfd, len); |
d5191d0c AM |
2506 | if (!name) |
2507 | return FALSE; | |
2508 | memcpy (name, namebuf, len); | |
2509 | newsect = bfd_make_section (abfd, name); | |
2510 | if (newsect == NULL) | |
2511 | return FALSE; | |
2512 | newsect->vma = hdr->p_vaddr; | |
2513 | newsect->lma = hdr->p_paddr; | |
2514 | newsect->size = hdr->p_filesz; | |
2515 | newsect->filepos = hdr->p_offset; | |
2516 | newsect->flags |= SEC_HAS_CONTENTS; | |
2517 | newsect->alignment_power = bfd_log2 (hdr->p_align); | |
2518 | if (hdr->p_type == PT_LOAD) | |
252b5132 | 2519 | { |
d5191d0c AM |
2520 | newsect->flags |= SEC_ALLOC; |
2521 | newsect->flags |= SEC_LOAD; | |
2522 | if (hdr->p_flags & PF_X) | |
2523 | { | |
2524 | /* FIXME: all we known is that it has execute PERMISSION, | |
2525 | may be data. */ | |
2526 | newsect->flags |= SEC_CODE; | |
2527 | } | |
2528 | } | |
2529 | if (!(hdr->p_flags & PF_W)) | |
2530 | { | |
2531 | newsect->flags |= SEC_READONLY; | |
252b5132 | 2532 | } |
252b5132 RH |
2533 | } |
2534 | ||
d5191d0c | 2535 | if (hdr->p_memsz > hdr->p_filesz) |
252b5132 | 2536 | { |
d5191d0c AM |
2537 | bfd_vma align; |
2538 | ||
91d6fa6a | 2539 | sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "b" : ""); |
d4c88bbb | 2540 | len = strlen (namebuf) + 1; |
a50b1753 | 2541 | name = (char *) bfd_alloc (abfd, len); |
252b5132 | 2542 | if (!name) |
b34976b6 | 2543 | return FALSE; |
d4c88bbb | 2544 | memcpy (name, namebuf, len); |
252b5132 RH |
2545 | newsect = bfd_make_section (abfd, name); |
2546 | if (newsect == NULL) | |
b34976b6 | 2547 | return FALSE; |
252b5132 RH |
2548 | newsect->vma = hdr->p_vaddr + hdr->p_filesz; |
2549 | newsect->lma = hdr->p_paddr + hdr->p_filesz; | |
eea6121a | 2550 | newsect->size = hdr->p_memsz - hdr->p_filesz; |
d5191d0c AM |
2551 | newsect->filepos = hdr->p_offset + hdr->p_filesz; |
2552 | align = newsect->vma & -newsect->vma; | |
2553 | if (align == 0 || align > hdr->p_align) | |
2554 | align = hdr->p_align; | |
2555 | newsect->alignment_power = bfd_log2 (align); | |
252b5132 RH |
2556 | if (hdr->p_type == PT_LOAD) |
2557 | { | |
d5191d0c AM |
2558 | /* Hack for gdb. Segments that have not been modified do |
2559 | not have their contents written to a core file, on the | |
2560 | assumption that a debugger can find the contents in the | |
2561 | executable. We flag this case by setting the fake | |
2562 | section size to zero. Note that "real" bss sections will | |
2563 | always have their contents dumped to the core file. */ | |
2564 | if (bfd_get_format (abfd) == bfd_core) | |
2565 | newsect->size = 0; | |
252b5132 RH |
2566 | newsect->flags |= SEC_ALLOC; |
2567 | if (hdr->p_flags & PF_X) | |
2568 | newsect->flags |= SEC_CODE; | |
2569 | } | |
2570 | if (!(hdr->p_flags & PF_W)) | |
2571 | newsect->flags |= SEC_READONLY; | |
2572 | } | |
2573 | ||
b34976b6 | 2574 | return TRUE; |
252b5132 RH |
2575 | } |
2576 | ||
b34976b6 | 2577 | bfd_boolean |
91d6fa6a | 2578 | bfd_section_from_phdr (bfd *abfd, Elf_Internal_Phdr *hdr, int hdr_index) |
20cfcaae | 2579 | { |
9c5bfbb7 | 2580 | const struct elf_backend_data *bed; |
20cfcaae NC |
2581 | |
2582 | switch (hdr->p_type) | |
2583 | { | |
2584 | case PT_NULL: | |
91d6fa6a | 2585 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "null"); |
20cfcaae NC |
2586 | |
2587 | case PT_LOAD: | |
91d6fa6a | 2588 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "load"); |
20cfcaae NC |
2589 | |
2590 | case PT_DYNAMIC: | |
91d6fa6a | 2591 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "dynamic"); |
20cfcaae NC |
2592 | |
2593 | case PT_INTERP: | |
91d6fa6a | 2594 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "interp"); |
20cfcaae NC |
2595 | |
2596 | case PT_NOTE: | |
91d6fa6a | 2597 | if (! _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "note")) |
b34976b6 | 2598 | return FALSE; |
718175fa | 2599 | if (! elf_read_notes (abfd, hdr->p_offset, hdr->p_filesz)) |
b34976b6 AM |
2600 | return FALSE; |
2601 | return TRUE; | |
20cfcaae NC |
2602 | |
2603 | case PT_SHLIB: | |
91d6fa6a | 2604 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "shlib"); |
20cfcaae NC |
2605 | |
2606 | case PT_PHDR: | |
91d6fa6a | 2607 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "phdr"); |
20cfcaae | 2608 | |
811072d8 | 2609 | case PT_GNU_EH_FRAME: |
91d6fa6a | 2610 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, |
811072d8 RM |
2611 | "eh_frame_hdr"); |
2612 | ||
2b05f1b7 | 2613 | case PT_GNU_STACK: |
91d6fa6a | 2614 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "stack"); |
9ee5e499 | 2615 | |
8c37241b | 2616 | case PT_GNU_RELRO: |
91d6fa6a | 2617 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "relro"); |
8c37241b | 2618 | |
20cfcaae | 2619 | default: |
8c1acd09 | 2620 | /* Check for any processor-specific program segment types. */ |
20cfcaae | 2621 | bed = get_elf_backend_data (abfd); |
91d6fa6a | 2622 | return bed->elf_backend_section_from_phdr (abfd, hdr, hdr_index, "proc"); |
20cfcaae NC |
2623 | } |
2624 | } | |
2625 | ||
d4730f92 BS |
2626 | /* Return the REL_HDR for SEC, assuming there is only a single one, either |
2627 | REL or RELA. */ | |
2628 | ||
2629 | Elf_Internal_Shdr * | |
2630 | _bfd_elf_single_rel_hdr (asection *sec) | |
2631 | { | |
2632 | if (elf_section_data (sec)->rel.hdr) | |
2633 | { | |
2634 | BFD_ASSERT (elf_section_data (sec)->rela.hdr == NULL); | |
2635 | return elf_section_data (sec)->rel.hdr; | |
2636 | } | |
2637 | else | |
2638 | return elf_section_data (sec)->rela.hdr; | |
2639 | } | |
2640 | ||
2641 | /* Allocate and initialize a section-header for a new reloc section, | |
2642 | containing relocations against ASECT. It is stored in RELDATA. If | |
2643 | USE_RELA_P is TRUE, we use RELA relocations; otherwise, we use REL | |
2644 | relocations. */ | |
23bc299b | 2645 | |
5d13b3b3 | 2646 | static bfd_boolean |
217aa764 | 2647 | _bfd_elf_init_reloc_shdr (bfd *abfd, |
d4730f92 | 2648 | struct bfd_elf_section_reloc_data *reldata, |
217aa764 AM |
2649 | asection *asect, |
2650 | bfd_boolean use_rela_p) | |
23bc299b | 2651 | { |
d4730f92 | 2652 | Elf_Internal_Shdr *rel_hdr; |
23bc299b | 2653 | char *name; |
9c5bfbb7 | 2654 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
d4730f92 BS |
2655 | bfd_size_type amt; |
2656 | ||
2657 | amt = sizeof (Elf_Internal_Shdr); | |
2658 | BFD_ASSERT (reldata->hdr == NULL); | |
2659 | rel_hdr = bfd_zalloc (abfd, amt); | |
2660 | reldata->hdr = rel_hdr; | |
23bc299b | 2661 | |
d324f6d6 | 2662 | amt = sizeof ".rela" + strlen (asect->name); |
a50b1753 | 2663 | name = (char *) bfd_alloc (abfd, amt); |
23bc299b | 2664 | if (name == NULL) |
b34976b6 | 2665 | return FALSE; |
23bc299b MM |
2666 | sprintf (name, "%s%s", use_rela_p ? ".rela" : ".rel", asect->name); |
2667 | rel_hdr->sh_name = | |
2b0f7ef9 | 2668 | (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), name, |
b34976b6 | 2669 | FALSE); |
23bc299b | 2670 | if (rel_hdr->sh_name == (unsigned int) -1) |
b34976b6 | 2671 | return FALSE; |
23bc299b MM |
2672 | rel_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL; |
2673 | rel_hdr->sh_entsize = (use_rela_p | |
2674 | ? bed->s->sizeof_rela | |
2675 | : bed->s->sizeof_rel); | |
72de5009 | 2676 | rel_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align; |
23bc299b MM |
2677 | rel_hdr->sh_flags = 0; |
2678 | rel_hdr->sh_addr = 0; | |
2679 | rel_hdr->sh_size = 0; | |
2680 | rel_hdr->sh_offset = 0; | |
2681 | ||
b34976b6 | 2682 | return TRUE; |
23bc299b MM |
2683 | } |
2684 | ||
94be91de JB |
2685 | /* Return the default section type based on the passed in section flags. */ |
2686 | ||
2687 | int | |
2688 | bfd_elf_get_default_section_type (flagword flags) | |
2689 | { | |
2690 | if ((flags & SEC_ALLOC) != 0 | |
2e76e85a | 2691 | && (flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0) |
94be91de JB |
2692 | return SHT_NOBITS; |
2693 | return SHT_PROGBITS; | |
2694 | } | |
2695 | ||
d4730f92 BS |
2696 | struct fake_section_arg |
2697 | { | |
2698 | struct bfd_link_info *link_info; | |
2699 | bfd_boolean failed; | |
2700 | }; | |
2701 | ||
252b5132 RH |
2702 | /* Set up an ELF internal section header for a section. */ |
2703 | ||
252b5132 | 2704 | static void |
d4730f92 | 2705 | elf_fake_sections (bfd *abfd, asection *asect, void *fsarg) |
252b5132 | 2706 | { |
d4730f92 | 2707 | struct fake_section_arg *arg = (struct fake_section_arg *)fsarg; |
9c5bfbb7 | 2708 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
d4730f92 | 2709 | struct bfd_elf_section_data *esd = elf_section_data (asect); |
252b5132 | 2710 | Elf_Internal_Shdr *this_hdr; |
0414f35b | 2711 | unsigned int sh_type; |
252b5132 | 2712 | |
d4730f92 | 2713 | if (arg->failed) |
252b5132 RH |
2714 | { |
2715 | /* We already failed; just get out of the bfd_map_over_sections | |
08a40648 | 2716 | loop. */ |
252b5132 RH |
2717 | return; |
2718 | } | |
2719 | ||
d4730f92 | 2720 | this_hdr = &esd->this_hdr; |
252b5132 | 2721 | |
e57b5356 AM |
2722 | this_hdr->sh_name = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), |
2723 | asect->name, FALSE); | |
2724 | if (this_hdr->sh_name == (unsigned int) -1) | |
252b5132 | 2725 | { |
d4730f92 | 2726 | arg->failed = TRUE; |
252b5132 RH |
2727 | return; |
2728 | } | |
2729 | ||
a4d8e49b | 2730 | /* Don't clear sh_flags. Assembler may set additional bits. */ |
252b5132 RH |
2731 | |
2732 | if ((asect->flags & SEC_ALLOC) != 0 | |
2733 | || asect->user_set_vma) | |
2734 | this_hdr->sh_addr = asect->vma; | |
2735 | else | |
2736 | this_hdr->sh_addr = 0; | |
2737 | ||
2738 | this_hdr->sh_offset = 0; | |
eea6121a | 2739 | this_hdr->sh_size = asect->size; |
252b5132 | 2740 | this_hdr->sh_link = 0; |
72de5009 | 2741 | this_hdr->sh_addralign = (bfd_vma) 1 << asect->alignment_power; |
252b5132 RH |
2742 | /* The sh_entsize and sh_info fields may have been set already by |
2743 | copy_private_section_data. */ | |
2744 | ||
2745 | this_hdr->bfd_section = asect; | |
2746 | this_hdr->contents = NULL; | |
2747 | ||
3cddba1e L |
2748 | /* If the section type is unspecified, we set it based on |
2749 | asect->flags. */ | |
98ece1b3 AM |
2750 | if ((asect->flags & SEC_GROUP) != 0) |
2751 | sh_type = SHT_GROUP; | |
98ece1b3 | 2752 | else |
94be91de | 2753 | sh_type = bfd_elf_get_default_section_type (asect->flags); |
98ece1b3 | 2754 | |
3cddba1e | 2755 | if (this_hdr->sh_type == SHT_NULL) |
98ece1b3 AM |
2756 | this_hdr->sh_type = sh_type; |
2757 | else if (this_hdr->sh_type == SHT_NOBITS | |
2758 | && sh_type == SHT_PROGBITS | |
2759 | && (asect->flags & SEC_ALLOC) != 0) | |
3cddba1e | 2760 | { |
98ece1b3 AM |
2761 | /* Warn if we are changing a NOBITS section to PROGBITS, but |
2762 | allow the link to proceed. This can happen when users link | |
2763 | non-bss input sections to bss output sections, or emit data | |
2764 | to a bss output section via a linker script. */ | |
2765 | (*_bfd_error_handler) | |
58f0869b | 2766 | (_("warning: section `%A' type changed to PROGBITS"), asect); |
98ece1b3 | 2767 | this_hdr->sh_type = sh_type; |
3cddba1e L |
2768 | } |
2769 | ||
2f89ff8d | 2770 | switch (this_hdr->sh_type) |
252b5132 | 2771 | { |
2f89ff8d | 2772 | default: |
2f89ff8d L |
2773 | break; |
2774 | ||
2775 | case SHT_STRTAB: | |
2776 | case SHT_INIT_ARRAY: | |
2777 | case SHT_FINI_ARRAY: | |
2778 | case SHT_PREINIT_ARRAY: | |
2779 | case SHT_NOTE: | |
2780 | case SHT_NOBITS: | |
2781 | case SHT_PROGBITS: | |
2782 | break; | |
2783 | ||
2784 | case SHT_HASH: | |
c7ac6ff8 | 2785 | this_hdr->sh_entsize = bed->s->sizeof_hash_entry; |
2f89ff8d | 2786 | break; |
5de3bf90 | 2787 | |
2f89ff8d | 2788 | case SHT_DYNSYM: |
252b5132 | 2789 | this_hdr->sh_entsize = bed->s->sizeof_sym; |
2f89ff8d L |
2790 | break; |
2791 | ||
2792 | case SHT_DYNAMIC: | |
252b5132 | 2793 | this_hdr->sh_entsize = bed->s->sizeof_dyn; |
2f89ff8d L |
2794 | break; |
2795 | ||
2796 | case SHT_RELA: | |
2797 | if (get_elf_backend_data (abfd)->may_use_rela_p) | |
2798 | this_hdr->sh_entsize = bed->s->sizeof_rela; | |
2799 | break; | |
2800 | ||
2801 | case SHT_REL: | |
2802 | if (get_elf_backend_data (abfd)->may_use_rel_p) | |
2803 | this_hdr->sh_entsize = bed->s->sizeof_rel; | |
2804 | break; | |
2805 | ||
2806 | case SHT_GNU_versym: | |
252b5132 | 2807 | this_hdr->sh_entsize = sizeof (Elf_External_Versym); |
2f89ff8d L |
2808 | break; |
2809 | ||
2810 | case SHT_GNU_verdef: | |
252b5132 RH |
2811 | this_hdr->sh_entsize = 0; |
2812 | /* objcopy or strip will copy over sh_info, but may not set | |
08a40648 AM |
2813 | cverdefs. The linker will set cverdefs, but sh_info will be |
2814 | zero. */ | |
252b5132 RH |
2815 | if (this_hdr->sh_info == 0) |
2816 | this_hdr->sh_info = elf_tdata (abfd)->cverdefs; | |
2817 | else | |
2818 | BFD_ASSERT (elf_tdata (abfd)->cverdefs == 0 | |
2819 | || this_hdr->sh_info == elf_tdata (abfd)->cverdefs); | |
2f89ff8d L |
2820 | break; |
2821 | ||
2822 | case SHT_GNU_verneed: | |
252b5132 RH |
2823 | this_hdr->sh_entsize = 0; |
2824 | /* objcopy or strip will copy over sh_info, but may not set | |
08a40648 AM |
2825 | cverrefs. The linker will set cverrefs, but sh_info will be |
2826 | zero. */ | |
252b5132 RH |
2827 | if (this_hdr->sh_info == 0) |
2828 | this_hdr->sh_info = elf_tdata (abfd)->cverrefs; | |
2829 | else | |
2830 | BFD_ASSERT (elf_tdata (abfd)->cverrefs == 0 | |
2831 | || this_hdr->sh_info == elf_tdata (abfd)->cverrefs); | |
2f89ff8d L |
2832 | break; |
2833 | ||
2834 | case SHT_GROUP: | |
1783205a | 2835 | this_hdr->sh_entsize = GRP_ENTRY_SIZE; |
2f89ff8d | 2836 | break; |
fdc90cb4 JJ |
2837 | |
2838 | case SHT_GNU_HASH: | |
2839 | this_hdr->sh_entsize = bed->s->arch_size == 64 ? 0 : 4; | |
2840 | break; | |
dbb410c3 | 2841 | } |
252b5132 RH |
2842 | |
2843 | if ((asect->flags & SEC_ALLOC) != 0) | |
2844 | this_hdr->sh_flags |= SHF_ALLOC; | |
2845 | if ((asect->flags & SEC_READONLY) == 0) | |
2846 | this_hdr->sh_flags |= SHF_WRITE; | |
2847 | if ((asect->flags & SEC_CODE) != 0) | |
2848 | this_hdr->sh_flags |= SHF_EXECINSTR; | |
f5fa8ca2 JJ |
2849 | if ((asect->flags & SEC_MERGE) != 0) |
2850 | { | |
2851 | this_hdr->sh_flags |= SHF_MERGE; | |
2852 | this_hdr->sh_entsize = asect->entsize; | |
2853 | if ((asect->flags & SEC_STRINGS) != 0) | |
2854 | this_hdr->sh_flags |= SHF_STRINGS; | |
2855 | } | |
1126897b | 2856 | if ((asect->flags & SEC_GROUP) == 0 && elf_group_name (asect) != NULL) |
dbb410c3 | 2857 | this_hdr->sh_flags |= SHF_GROUP; |
13ae64f3 | 2858 | if ((asect->flags & SEC_THREAD_LOCAL) != 0) |
704afa60 JJ |
2859 | { |
2860 | this_hdr->sh_flags |= SHF_TLS; | |
3a800eb9 AM |
2861 | if (asect->size == 0 |
2862 | && (asect->flags & SEC_HAS_CONTENTS) == 0) | |
704afa60 | 2863 | { |
3a800eb9 | 2864 | struct bfd_link_order *o = asect->map_tail.link_order; |
b34976b6 | 2865 | |
704afa60 | 2866 | this_hdr->sh_size = 0; |
3a800eb9 AM |
2867 | if (o != NULL) |
2868 | { | |
704afa60 | 2869 | this_hdr->sh_size = o->offset + o->size; |
3a800eb9 AM |
2870 | if (this_hdr->sh_size != 0) |
2871 | this_hdr->sh_type = SHT_NOBITS; | |
2872 | } | |
704afa60 JJ |
2873 | } |
2874 | } | |
18ae9cc1 L |
2875 | if ((asect->flags & (SEC_GROUP | SEC_EXCLUDE)) == SEC_EXCLUDE) |
2876 | this_hdr->sh_flags |= SHF_EXCLUDE; | |
252b5132 | 2877 | |
d4730f92 BS |
2878 | /* If the section has relocs, set up a section header for the |
2879 | SHT_REL[A] section. If two relocation sections are required for | |
2880 | this section, it is up to the processor-specific back-end to | |
2881 | create the other. */ | |
2882 | if ((asect->flags & SEC_RELOC) != 0) | |
2883 | { | |
2884 | /* When doing a relocatable link, create both REL and RELA sections if | |
2885 | needed. */ | |
2886 | if (arg->link_info | |
2887 | /* Do the normal setup if we wouldn't create any sections here. */ | |
2888 | && esd->rel.count + esd->rela.count > 0 | |
2889 | && (arg->link_info->relocatable || arg->link_info->emitrelocations)) | |
2890 | { | |
2891 | if (esd->rel.count && esd->rel.hdr == NULL | |
2892 | && !_bfd_elf_init_reloc_shdr (abfd, &esd->rel, asect, FALSE)) | |
2893 | { | |
2894 | arg->failed = TRUE; | |
2895 | return; | |
2896 | } | |
2897 | if (esd->rela.count && esd->rela.hdr == NULL | |
2898 | && !_bfd_elf_init_reloc_shdr (abfd, &esd->rela, asect, TRUE)) | |
2899 | { | |
2900 | arg->failed = TRUE; | |
2901 | return; | |
2902 | } | |
2903 | } | |
2904 | else if (!_bfd_elf_init_reloc_shdr (abfd, | |
2905 | (asect->use_rela_p | |
2906 | ? &esd->rela : &esd->rel), | |
2907 | asect, | |
2908 | asect->use_rela_p)) | |
2909 | arg->failed = TRUE; | |
2910 | } | |
2911 | ||
252b5132 | 2912 | /* Check for processor-specific section types. */ |
0414f35b | 2913 | sh_type = this_hdr->sh_type; |
e1fddb6b AO |
2914 | if (bed->elf_backend_fake_sections |
2915 | && !(*bed->elf_backend_fake_sections) (abfd, this_hdr, asect)) | |
d4730f92 | 2916 | arg->failed = TRUE; |
252b5132 | 2917 | |
42bb2e33 | 2918 | if (sh_type == SHT_NOBITS && asect->size != 0) |
0414f35b AM |
2919 | { |
2920 | /* Don't change the header type from NOBITS if we are being | |
42bb2e33 | 2921 | called for objcopy --only-keep-debug. */ |
0414f35b AM |
2922 | this_hdr->sh_type = sh_type; |
2923 | } | |
252b5132 RH |
2924 | } |
2925 | ||
bcacc0f5 AM |
2926 | /* Fill in the contents of a SHT_GROUP section. Called from |
2927 | _bfd_elf_compute_section_file_positions for gas, objcopy, and | |
2928 | when ELF targets use the generic linker, ld. Called for ld -r | |
2929 | from bfd_elf_final_link. */ | |
dbb410c3 | 2930 | |
1126897b | 2931 | void |
217aa764 | 2932 | bfd_elf_set_group_contents (bfd *abfd, asection *sec, void *failedptrarg) |
dbb410c3 | 2933 | { |
a50b1753 | 2934 | bfd_boolean *failedptr = (bfd_boolean *) failedptrarg; |
9dce4196 | 2935 | asection *elt, *first; |
dbb410c3 | 2936 | unsigned char *loc; |
b34976b6 | 2937 | bfd_boolean gas; |
dbb410c3 | 2938 | |
7e4111ad L |
2939 | /* Ignore linker created group section. See elfNN_ia64_object_p in |
2940 | elfxx-ia64.c. */ | |
2941 | if (((sec->flags & (SEC_GROUP | SEC_LINKER_CREATED)) != SEC_GROUP) | |
dbb410c3 AM |
2942 | || *failedptr) |
2943 | return; | |
2944 | ||
bcacc0f5 AM |
2945 | if (elf_section_data (sec)->this_hdr.sh_info == 0) |
2946 | { | |
2947 | unsigned long symindx = 0; | |
2948 | ||
2949 | /* elf_group_id will have been set up by objcopy and the | |
2950 | generic linker. */ | |
2951 | if (elf_group_id (sec) != NULL) | |
2952 | symindx = elf_group_id (sec)->udata.i; | |
1126897b | 2953 | |
bcacc0f5 AM |
2954 | if (symindx == 0) |
2955 | { | |
2956 | /* If called from the assembler, swap_out_syms will have set up | |
2957 | elf_section_syms. */ | |
2958 | BFD_ASSERT (elf_section_syms (abfd) != NULL); | |
2959 | symindx = elf_section_syms (abfd)[sec->index]->udata.i; | |
2960 | } | |
2961 | elf_section_data (sec)->this_hdr.sh_info = symindx; | |
2962 | } | |
2963 | else if (elf_section_data (sec)->this_hdr.sh_info == (unsigned int) -2) | |
1126897b | 2964 | { |
bcacc0f5 AM |
2965 | /* The ELF backend linker sets sh_info to -2 when the group |
2966 | signature symbol is global, and thus the index can't be | |
2967 | set until all local symbols are output. */ | |
2968 | asection *igroup = elf_sec_group (elf_next_in_group (sec)); | |
2969 | struct bfd_elf_section_data *sec_data = elf_section_data (igroup); | |
2970 | unsigned long symndx = sec_data->this_hdr.sh_info; | |
2971 | unsigned long extsymoff = 0; | |
2972 | struct elf_link_hash_entry *h; | |
2973 | ||
2974 | if (!elf_bad_symtab (igroup->owner)) | |
2975 | { | |
2976 | Elf_Internal_Shdr *symtab_hdr; | |
2977 | ||
2978 | symtab_hdr = &elf_tdata (igroup->owner)->symtab_hdr; | |
2979 | extsymoff = symtab_hdr->sh_info; | |
2980 | } | |
2981 | h = elf_sym_hashes (igroup->owner)[symndx - extsymoff]; | |
2982 | while (h->root.type == bfd_link_hash_indirect | |
2983 | || h->root.type == bfd_link_hash_warning) | |
2984 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
2985 | ||
2986 | elf_section_data (sec)->this_hdr.sh_info = h->indx; | |
1126897b | 2987 | } |
dbb410c3 | 2988 | |
1126897b | 2989 | /* The contents won't be allocated for "ld -r" or objcopy. */ |
b34976b6 | 2990 | gas = TRUE; |
dbb410c3 AM |
2991 | if (sec->contents == NULL) |
2992 | { | |
b34976b6 | 2993 | gas = FALSE; |
a50b1753 | 2994 | sec->contents = (unsigned char *) bfd_alloc (abfd, sec->size); |
9dce4196 AM |
2995 | |
2996 | /* Arrange for the section to be written out. */ | |
2997 | elf_section_data (sec)->this_hdr.contents = sec->contents; | |
dbb410c3 AM |
2998 | if (sec->contents == NULL) |
2999 | { | |
b34976b6 | 3000 | *failedptr = TRUE; |
dbb410c3 AM |
3001 | return; |
3002 | } | |
3003 | } | |
3004 | ||
eea6121a | 3005 | loc = sec->contents + sec->size; |
dbb410c3 | 3006 | |
9dce4196 AM |
3007 | /* Get the pointer to the first section in the group that gas |
3008 | squirreled away here. objcopy arranges for this to be set to the | |
3009 | start of the input section group. */ | |
3010 | first = elt = elf_next_in_group (sec); | |
dbb410c3 AM |
3011 | |
3012 | /* First element is a flag word. Rest of section is elf section | |
3013 | indices for all the sections of the group. Write them backwards | |
3014 | just to keep the group in the same order as given in .section | |
3015 | directives, not that it matters. */ | |
3016 | while (elt != NULL) | |
3017 | { | |
9dce4196 | 3018 | asection *s; |
9dce4196 | 3019 | |
9dce4196 | 3020 | s = elt; |
415f38a6 AM |
3021 | if (!gas) |
3022 | s = s->output_section; | |
3023 | if (s != NULL | |
3024 | && !bfd_is_abs_section (s)) | |
01e1a5bc | 3025 | { |
415f38a6 AM |
3026 | unsigned int idx = elf_section_data (s)->this_idx; |
3027 | ||
01e1a5bc | 3028 | loc -= 4; |
01e1a5bc NC |
3029 | H_PUT_32 (abfd, idx, loc); |
3030 | } | |
945906ff | 3031 | elt = elf_next_in_group (elt); |
9dce4196 AM |
3032 | if (elt == first) |
3033 | break; | |
dbb410c3 AM |
3034 | } |
3035 | ||
3d7f7666 | 3036 | if ((loc -= 4) != sec->contents) |
9dce4196 | 3037 | abort (); |
dbb410c3 | 3038 | |
9dce4196 | 3039 | H_PUT_32 (abfd, sec->flags & SEC_LINK_ONCE ? GRP_COMDAT : 0, loc); |
dbb410c3 AM |
3040 | } |
3041 | ||
252b5132 RH |
3042 | /* Assign all ELF section numbers. The dummy first section is handled here |
3043 | too. The link/info pointers for the standard section types are filled | |
3044 | in here too, while we're at it. */ | |
3045 | ||
b34976b6 | 3046 | static bfd_boolean |
da9f89d4 | 3047 | assign_section_numbers (bfd *abfd, struct bfd_link_info *link_info) |
252b5132 RH |
3048 | { |
3049 | struct elf_obj_tdata *t = elf_tdata (abfd); | |
3050 | asection *sec; | |
2b0f7ef9 | 3051 | unsigned int section_number, secn; |
252b5132 | 3052 | Elf_Internal_Shdr **i_shdrp; |
47cc2cf5 | 3053 | struct bfd_elf_section_data *d; |
3516e984 | 3054 | bfd_boolean need_symtab; |
252b5132 RH |
3055 | |
3056 | section_number = 1; | |
3057 | ||
2b0f7ef9 JJ |
3058 | _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd)); |
3059 | ||
da9f89d4 L |
3060 | /* SHT_GROUP sections are in relocatable files only. */ |
3061 | if (link_info == NULL || link_info->relocatable) | |
252b5132 | 3062 | { |
da9f89d4 | 3063 | /* Put SHT_GROUP sections first. */ |
04dd1667 | 3064 | for (sec = abfd->sections; sec != NULL; sec = sec->next) |
47cc2cf5 | 3065 | { |
5daa8fe7 | 3066 | d = elf_section_data (sec); |
da9f89d4 L |
3067 | |
3068 | if (d->this_hdr.sh_type == SHT_GROUP) | |
08a40648 | 3069 | { |
5daa8fe7 | 3070 | if (sec->flags & SEC_LINKER_CREATED) |
da9f89d4 L |
3071 | { |
3072 | /* Remove the linker created SHT_GROUP sections. */ | |
5daa8fe7 | 3073 | bfd_section_list_remove (abfd, sec); |
da9f89d4 | 3074 | abfd->section_count--; |
da9f89d4 | 3075 | } |
08a40648 | 3076 | else |
4fbb74a6 | 3077 | d->this_idx = section_number++; |
da9f89d4 | 3078 | } |
47cc2cf5 PB |
3079 | } |
3080 | } | |
3081 | ||
3082 | for (sec = abfd->sections; sec; sec = sec->next) | |
3083 | { | |
3084 | d = elf_section_data (sec); | |
3085 | ||
3086 | if (d->this_hdr.sh_type != SHT_GROUP) | |
4fbb74a6 | 3087 | d->this_idx = section_number++; |
2b0f7ef9 | 3088 | _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->this_hdr.sh_name); |
d4730f92 | 3089 | if (d->rel.hdr) |
2b0f7ef9 | 3090 | { |
d4730f92 BS |
3091 | d->rel.idx = section_number++; |
3092 | _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel.hdr->sh_name); | |
2b0f7ef9 | 3093 | } |
d4730f92 BS |
3094 | else |
3095 | d->rel.idx = 0; | |
23bc299b | 3096 | |
d4730f92 | 3097 | if (d->rela.hdr) |
2b0f7ef9 | 3098 | { |
d4730f92 BS |
3099 | d->rela.idx = section_number++; |
3100 | _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rela.hdr->sh_name); | |
2b0f7ef9 | 3101 | } |
23bc299b | 3102 | else |
d4730f92 | 3103 | d->rela.idx = 0; |
252b5132 RH |
3104 | } |
3105 | ||
12bd6957 | 3106 | elf_shstrtab_sec (abfd) = section_number++; |
2b0f7ef9 | 3107 | _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->shstrtab_hdr.sh_name); |
12bd6957 | 3108 | elf_elfheader (abfd)->e_shstrndx = elf_shstrtab_sec (abfd); |
252b5132 | 3109 | |
3516e984 L |
3110 | need_symtab = (bfd_get_symcount (abfd) > 0 |
3111 | || (link_info == NULL | |
3112 | && ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC)) | |
3113 | == HAS_RELOC))); | |
3114 | if (need_symtab) | |
252b5132 | 3115 | { |
12bd6957 | 3116 | elf_onesymtab (abfd) = section_number++; |
2b0f7ef9 | 3117 | _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->symtab_hdr.sh_name); |
4fbb74a6 | 3118 | if (section_number > ((SHN_LORESERVE - 2) & 0xFFFF)) |
9ad5cbcf | 3119 | { |
12bd6957 | 3120 | elf_symtab_shndx (abfd) = section_number++; |
9ad5cbcf AM |
3121 | t->symtab_shndx_hdr.sh_name |
3122 | = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), | |
b34976b6 | 3123 | ".symtab_shndx", FALSE); |
9ad5cbcf | 3124 | if (t->symtab_shndx_hdr.sh_name == (unsigned int) -1) |
b34976b6 | 3125 | return FALSE; |
9ad5cbcf | 3126 | } |
12bd6957 | 3127 | elf_strtab_sec (abfd) = section_number++; |
2b0f7ef9 | 3128 | _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->strtab_hdr.sh_name); |
252b5132 RH |
3129 | } |
3130 | ||
1c52a645 L |
3131 | if (section_number >= SHN_LORESERVE) |
3132 | { | |
3133 | _bfd_error_handler (_("%B: too many sections: %u"), | |
3134 | abfd, section_number); | |
3135 | return FALSE; | |
3136 | } | |
3137 | ||
2b0f7ef9 JJ |
3138 | _bfd_elf_strtab_finalize (elf_shstrtab (abfd)); |
3139 | t->shstrtab_hdr.sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd)); | |
9ad5cbcf AM |
3140 | |
3141 | elf_numsections (abfd) = section_number; | |
252b5132 RH |
3142 | elf_elfheader (abfd)->e_shnum = section_number; |
3143 | ||
3144 | /* Set up the list of section header pointers, in agreement with the | |
3145 | indices. */ | |
a50b1753 NC |
3146 | i_shdrp = (Elf_Internal_Shdr **) bfd_zalloc2 (abfd, section_number, |
3147 | sizeof (Elf_Internal_Shdr *)); | |
252b5132 | 3148 | if (i_shdrp == NULL) |
b34976b6 | 3149 | return FALSE; |
252b5132 | 3150 | |
a50b1753 NC |
3151 | i_shdrp[0] = (Elf_Internal_Shdr *) bfd_zalloc (abfd, |
3152 | sizeof (Elf_Internal_Shdr)); | |
252b5132 RH |
3153 | if (i_shdrp[0] == NULL) |
3154 | { | |
3155 | bfd_release (abfd, i_shdrp); | |
b34976b6 | 3156 | return FALSE; |
252b5132 | 3157 | } |
252b5132 RH |
3158 | |
3159 | elf_elfsections (abfd) = i_shdrp; | |
3160 | ||
12bd6957 | 3161 | i_shdrp[elf_shstrtab_sec (abfd)] = &t->shstrtab_hdr; |
3516e984 | 3162 | if (need_symtab) |
252b5132 | 3163 | { |
12bd6957 | 3164 | i_shdrp[elf_onesymtab (abfd)] = &t->symtab_hdr; |
4fbb74a6 | 3165 | if (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF)) |
9ad5cbcf | 3166 | { |
12bd6957 AM |
3167 | i_shdrp[elf_symtab_shndx (abfd)] = &t->symtab_shndx_hdr; |
3168 | t->symtab_shndx_hdr.sh_link = elf_onesymtab (abfd); | |
9ad5cbcf | 3169 | } |
12bd6957 AM |
3170 | i_shdrp[elf_strtab_sec (abfd)] = &t->strtab_hdr; |
3171 | t->symtab_hdr.sh_link = elf_strtab_sec (abfd); | |
252b5132 | 3172 | } |
38ce5b11 | 3173 | |
252b5132 RH |
3174 | for (sec = abfd->sections; sec; sec = sec->next) |
3175 | { | |
252b5132 RH |
3176 | asection *s; |
3177 | const char *name; | |
3178 | ||
91d6fa6a NC |
3179 | d = elf_section_data (sec); |
3180 | ||
252b5132 | 3181 | i_shdrp[d->this_idx] = &d->this_hdr; |
d4730f92 BS |
3182 | if (d->rel.idx != 0) |
3183 | i_shdrp[d->rel.idx] = d->rel.hdr; | |
3184 | if (d->rela.idx != 0) | |
3185 | i_shdrp[d->rela.idx] = d->rela.hdr; | |
252b5132 RH |
3186 | |
3187 | /* Fill in the sh_link and sh_info fields while we're at it. */ | |
3188 | ||
3189 | /* sh_link of a reloc section is the section index of the symbol | |
3190 | table. sh_info is the section index of the section to which | |
3191 | the relocation entries apply. */ | |
d4730f92 | 3192 | if (d->rel.idx != 0) |
252b5132 | 3193 | { |
12bd6957 | 3194 | d->rel.hdr->sh_link = elf_onesymtab (abfd); |
d4730f92 | 3195 | d->rel.hdr->sh_info = d->this_idx; |
9ef5d938 | 3196 | d->rel.hdr->sh_flags |= SHF_INFO_LINK; |
252b5132 | 3197 | } |
d4730f92 | 3198 | if (d->rela.idx != 0) |
23bc299b | 3199 | { |
12bd6957 | 3200 | d->rela.hdr->sh_link = elf_onesymtab (abfd); |
d4730f92 | 3201 | d->rela.hdr->sh_info = d->this_idx; |
9ef5d938 | 3202 | d->rela.hdr->sh_flags |= SHF_INFO_LINK; |
23bc299b | 3203 | } |
252b5132 | 3204 | |
38ce5b11 L |
3205 | /* We need to set up sh_link for SHF_LINK_ORDER. */ |
3206 | if ((d->this_hdr.sh_flags & SHF_LINK_ORDER) != 0) | |
3207 | { | |
3208 | s = elf_linked_to_section (sec); | |
3209 | if (s) | |
38ce5b11 | 3210 | { |
f2876037 | 3211 | /* elf_linked_to_section points to the input section. */ |
ccd2ec6a | 3212 | if (link_info != NULL) |
38ce5b11 | 3213 | { |
f2876037 | 3214 | /* Check discarded linkonce section. */ |
dbaa2011 | 3215 | if (discarded_section (s)) |
38ce5b11 | 3216 | { |
ccd2ec6a L |
3217 | asection *kept; |
3218 | (*_bfd_error_handler) | |
3219 | (_("%B: sh_link of section `%A' points to discarded section `%A' of `%B'"), | |
3220 | abfd, d->this_hdr.bfd_section, | |
3221 | s, s->owner); | |
3222 | /* Point to the kept section if it has the same | |
3223 | size as the discarded one. */ | |
c0f00686 | 3224 | kept = _bfd_elf_check_kept_section (s, link_info); |
ccd2ec6a | 3225 | if (kept == NULL) |
185d09ad | 3226 | { |
ccd2ec6a L |
3227 | bfd_set_error (bfd_error_bad_value); |
3228 | return FALSE; | |
185d09ad | 3229 | } |
ccd2ec6a | 3230 | s = kept; |
38ce5b11 | 3231 | } |
e424ecc8 | 3232 | |
ccd2ec6a L |
3233 | s = s->output_section; |
3234 | BFD_ASSERT (s != NULL); | |
38ce5b11 | 3235 | } |
f2876037 L |
3236 | else |
3237 | { | |
3238 | /* Handle objcopy. */ | |
3239 | if (s->output_section == NULL) | |
3240 | { | |
3241 | (*_bfd_error_handler) | |
3242 | (_("%B: sh_link of section `%A' points to removed section `%A' of `%B'"), | |
3243 | abfd, d->this_hdr.bfd_section, s, s->owner); | |
3244 | bfd_set_error (bfd_error_bad_value); | |
3245 | return FALSE; | |
3246 | } | |
3247 | s = s->output_section; | |
3248 | } | |
ccd2ec6a L |
3249 | d->this_hdr.sh_link = elf_section_data (s)->this_idx; |
3250 | } | |
3251 | else | |
3252 | { | |
3253 | /* PR 290: | |
3254 | The Intel C compiler generates SHT_IA_64_UNWIND with | |
3255 | SHF_LINK_ORDER. But it doesn't set the sh_link or | |
3256 | sh_info fields. Hence we could get the situation | |
08a40648 | 3257 | where s is NULL. */ |
ccd2ec6a L |
3258 | const struct elf_backend_data *bed |
3259 | = get_elf_backend_data (abfd); | |
3260 | if (bed->link_order_error_handler) | |
3261 | bed->link_order_error_handler | |
3262 | (_("%B: warning: sh_link not set for section `%A'"), | |
3263 | abfd, sec); | |
38ce5b11 L |
3264 | } |
3265 | } | |
3266 | ||
252b5132 RH |
3267 | switch (d->this_hdr.sh_type) |
3268 | { | |
3269 | case SHT_REL: | |
3270 | case SHT_RELA: | |
3271 | /* A reloc section which we are treating as a normal BFD | |
3272 | section. sh_link is the section index of the symbol | |
3273 | table. sh_info is the section index of the section to | |
3274 | which the relocation entries apply. We assume that an | |
3275 | allocated reloc section uses the dynamic symbol table. | |
3276 | FIXME: How can we be sure? */ | |
3277 | s = bfd_get_section_by_name (abfd, ".dynsym"); | |
3278 | if (s != NULL) | |
3279 | d->this_hdr.sh_link = elf_section_data (s)->this_idx; | |
3280 | ||
3281 | /* We look up the section the relocs apply to by name. */ | |
3282 | name = sec->name; | |
3283 | if (d->this_hdr.sh_type == SHT_REL) | |
3284 | name += 4; | |
3285 | else | |
3286 | name += 5; | |
3287 | s = bfd_get_section_by_name (abfd, name); | |
3288 | if (s != NULL) | |
9ef5d938 L |
3289 | { |
3290 | d->this_hdr.sh_info = elf_section_data (s)->this_idx; | |
3291 | d->this_hdr.sh_flags |= SHF_INFO_LINK; | |
3292 | } | |
252b5132 RH |
3293 | break; |
3294 | ||
3295 | case SHT_STRTAB: | |
3296 | /* We assume that a section named .stab*str is a stabs | |
3297 | string section. We look for a section with the same name | |
3298 | but without the trailing ``str'', and set its sh_link | |
3299 | field to point to this section. */ | |
0112cd26 | 3300 | if (CONST_STRNEQ (sec->name, ".stab") |
252b5132 RH |
3301 | && strcmp (sec->name + strlen (sec->name) - 3, "str") == 0) |
3302 | { | |
3303 | size_t len; | |
3304 | char *alc; | |
3305 | ||
3306 | len = strlen (sec->name); | |
a50b1753 | 3307 | alc = (char *) bfd_malloc (len - 2); |
252b5132 | 3308 | if (alc == NULL) |
b34976b6 | 3309 | return FALSE; |
d4c88bbb | 3310 | memcpy (alc, sec->name, len - 3); |
252b5132 RH |
3311 | alc[len - 3] = '\0'; |
3312 | s = bfd_get_section_by_name (abfd, alc); | |
3313 | free (alc); | |
3314 | if (s != NULL) | |
3315 | { | |
3316 | elf_section_data (s)->this_hdr.sh_link = d->this_idx; | |
3317 | ||
3318 | /* This is a .stab section. */ | |
0594c12d AM |
3319 | if (elf_section_data (s)->this_hdr.sh_entsize == 0) |
3320 | elf_section_data (s)->this_hdr.sh_entsize | |
3321 | = 4 + 2 * bfd_get_arch_size (abfd) / 8; | |
252b5132 RH |
3322 | } |
3323 | } | |
3324 | break; | |
3325 | ||
3326 | case SHT_DYNAMIC: | |
3327 | case SHT_DYNSYM: | |
3328 | case SHT_GNU_verneed: | |
3329 | case SHT_GNU_verdef: | |
3330 | /* sh_link is the section header index of the string table | |
3331 | used for the dynamic entries, or the symbol table, or the | |
3332 | version strings. */ | |
3333 | s = bfd_get_section_by_name (abfd, ".dynstr"); | |
3334 | if (s != NULL) | |
3335 | d->this_hdr.sh_link = elf_section_data (s)->this_idx; | |
3336 | break; | |
3337 | ||
7f1204bb JJ |
3338 | case SHT_GNU_LIBLIST: |
3339 | /* sh_link is the section header index of the prelink library | |
08a40648 AM |
3340 | list used for the dynamic entries, or the symbol table, or |
3341 | the version strings. */ | |
7f1204bb JJ |
3342 | s = bfd_get_section_by_name (abfd, (sec->flags & SEC_ALLOC) |
3343 | ? ".dynstr" : ".gnu.libstr"); | |
3344 | if (s != NULL) | |
3345 | d->this_hdr.sh_link = elf_section_data (s)->this_idx; | |
3346 | break; | |
3347 | ||
252b5132 | 3348 | case SHT_HASH: |
fdc90cb4 | 3349 | case SHT_GNU_HASH: |
252b5132 RH |
3350 | case SHT_GNU_versym: |
3351 | /* sh_link is the section header index of the symbol table | |
3352 | this hash table or version table is for. */ | |
3353 | s = bfd_get_section_by_name (abfd, ".dynsym"); | |
3354 | if (s != NULL) | |
3355 | d->this_hdr.sh_link = elf_section_data (s)->this_idx; | |
3356 | break; | |
dbb410c3 AM |
3357 | |
3358 | case SHT_GROUP: | |
12bd6957 | 3359 | d->this_hdr.sh_link = elf_onesymtab (abfd); |
252b5132 RH |
3360 | } |
3361 | } | |
3362 | ||
2b0f7ef9 | 3363 | for (secn = 1; secn < section_number; ++secn) |
9ad5cbcf AM |
3364 | if (i_shdrp[secn] == NULL) |
3365 | i_shdrp[secn] = i_shdrp[0]; | |
3366 | else | |
3367 | i_shdrp[secn]->sh_name = _bfd_elf_strtab_offset (elf_shstrtab (abfd), | |
3368 | i_shdrp[secn]->sh_name); | |
b34976b6 | 3369 | return TRUE; |
252b5132 RH |
3370 | } |
3371 | ||
5372391b | 3372 | static bfd_boolean |
217aa764 | 3373 | sym_is_global (bfd *abfd, asymbol *sym) |
252b5132 RH |
3374 | { |
3375 | /* If the backend has a special mapping, use it. */ | |
9c5bfbb7 | 3376 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
217aa764 AM |
3377 | if (bed->elf_backend_sym_is_global) |
3378 | return (*bed->elf_backend_sym_is_global) (abfd, sym); | |
252b5132 | 3379 | |
e47bf690 | 3380 | return ((sym->flags & (BSF_GLOBAL | BSF_WEAK | BSF_GNU_UNIQUE)) != 0 |
252b5132 RH |
3381 | || bfd_is_und_section (bfd_get_section (sym)) |
3382 | || bfd_is_com_section (bfd_get_section (sym))); | |
3383 | } | |
3384 | ||
5372391b | 3385 | /* Don't output section symbols for sections that are not going to be |
c6d8cab4 | 3386 | output, that are duplicates or there is no BFD section. */ |
5372391b AM |
3387 | |
3388 | static bfd_boolean | |
3389 | ignore_section_sym (bfd *abfd, asymbol *sym) | |
3390 | { | |
c6d8cab4 L |
3391 | elf_symbol_type *type_ptr; |
3392 | ||
3393 | if ((sym->flags & BSF_SECTION_SYM) == 0) | |
3394 | return FALSE; | |
3395 | ||
3396 | type_ptr = elf_symbol_from (abfd, sym); | |
3397 | return ((type_ptr != NULL | |
3398 | && type_ptr->internal_elf_sym.st_shndx != 0 | |
3399 | && bfd_is_abs_section (sym->section)) | |
3400 | || !(sym->section->owner == abfd | |
0f0a5e58 | 3401 | || (sym->section->output_section->owner == abfd |
2633a79c AM |
3402 | && sym->section->output_offset == 0) |
3403 | || bfd_is_abs_section (sym->section))); | |
5372391b AM |
3404 | } |
3405 | ||
2633a79c AM |
3406 | /* Map symbol from it's internal number to the external number, moving |
3407 | all local symbols to be at the head of the list. */ | |
3408 | ||
b34976b6 | 3409 | static bfd_boolean |
12bd6957 | 3410 | elf_map_symbols (bfd *abfd, unsigned int *pnum_locals) |
252b5132 | 3411 | { |
dc810e39 | 3412 | unsigned int symcount = bfd_get_symcount (abfd); |
252b5132 RH |
3413 | asymbol **syms = bfd_get_outsymbols (abfd); |
3414 | asymbol **sect_syms; | |
dc810e39 AM |
3415 | unsigned int num_locals = 0; |
3416 | unsigned int num_globals = 0; | |
3417 | unsigned int num_locals2 = 0; | |
3418 | unsigned int num_globals2 = 0; | |
252b5132 | 3419 | int max_index = 0; |
dc810e39 | 3420 | unsigned int idx; |
252b5132 RH |
3421 | asection *asect; |
3422 | asymbol **new_syms; | |
252b5132 RH |
3423 | |
3424 | #ifdef DEBUG | |
3425 | fprintf (stderr, "elf_map_symbols\n"); | |
3426 | fflush (stderr); | |
3427 | #endif | |
3428 | ||
252b5132 RH |
3429 | for (asect = abfd->sections; asect; asect = asect->next) |
3430 | { | |
3431 | if (max_index < asect->index) | |
3432 | max_index = asect->index; | |
3433 | } | |
3434 | ||
3435 | max_index++; | |
a50b1753 | 3436 | sect_syms = (asymbol **) bfd_zalloc2 (abfd, max_index, sizeof (asymbol *)); |
252b5132 | 3437 | if (sect_syms == NULL) |
b34976b6 | 3438 | return FALSE; |
252b5132 | 3439 | elf_section_syms (abfd) = sect_syms; |
4e89ac30 | 3440 | elf_num_section_syms (abfd) = max_index; |
252b5132 | 3441 | |
079e9a2f AM |
3442 | /* Init sect_syms entries for any section symbols we have already |
3443 | decided to output. */ | |
252b5132 RH |
3444 | for (idx = 0; idx < symcount; idx++) |
3445 | { | |
dc810e39 | 3446 | asymbol *sym = syms[idx]; |
c044fabd | 3447 | |
252b5132 | 3448 | if ((sym->flags & BSF_SECTION_SYM) != 0 |
0f0a5e58 | 3449 | && sym->value == 0 |
2633a79c AM |
3450 | && !ignore_section_sym (abfd, sym) |
3451 | && !bfd_is_abs_section (sym->section)) | |
252b5132 | 3452 | { |
5372391b | 3453 | asection *sec = sym->section; |
252b5132 | 3454 | |
5372391b AM |
3455 | if (sec->owner != abfd) |
3456 | sec = sec->output_section; | |
252b5132 | 3457 | |
5372391b | 3458 | sect_syms[sec->index] = syms[idx]; |
252b5132 RH |
3459 | } |
3460 | } | |
3461 | ||
252b5132 RH |
3462 | /* Classify all of the symbols. */ |
3463 | for (idx = 0; idx < symcount; idx++) | |
3464 | { | |
2633a79c | 3465 | if (sym_is_global (abfd, syms[idx])) |
252b5132 | 3466 | num_globals++; |
2633a79c AM |
3467 | else if (!ignore_section_sym (abfd, syms[idx])) |
3468 | num_locals++; | |
252b5132 | 3469 | } |
079e9a2f | 3470 | |
5372391b | 3471 | /* We will be adding a section symbol for each normal BFD section. Most |
079e9a2f AM |
3472 | sections will already have a section symbol in outsymbols, but |
3473 | eg. SHT_GROUP sections will not, and we need the section symbol mapped | |
3474 | at least in that case. */ | |
252b5132 RH |
3475 | for (asect = abfd->sections; asect; asect = asect->next) |
3476 | { | |
079e9a2f | 3477 | if (sect_syms[asect->index] == NULL) |
252b5132 | 3478 | { |
079e9a2f | 3479 | if (!sym_is_global (abfd, asect->symbol)) |
252b5132 RH |
3480 | num_locals++; |
3481 | else | |
3482 | num_globals++; | |
252b5132 RH |
3483 | } |
3484 | } | |
3485 | ||
3486 | /* Now sort the symbols so the local symbols are first. */ | |
a50b1753 NC |
3487 | new_syms = (asymbol **) bfd_alloc2 (abfd, num_locals + num_globals, |
3488 | sizeof (asymbol *)); | |
dc810e39 | 3489 | |
252b5132 | 3490 | if (new_syms == NULL) |
b34976b6 | 3491 | return FALSE; |
252b5132 RH |
3492 | |
3493 | for (idx = 0; idx < symcount; idx++) | |
3494 | { | |
3495 | asymbol *sym = syms[idx]; | |
dc810e39 | 3496 | unsigned int i; |
252b5132 | 3497 | |
2633a79c AM |
3498 | if (sym_is_global (abfd, sym)) |
3499 | i = num_locals + num_globals2++; | |
3500 | else if (!ignore_section_sym (abfd, sym)) | |
252b5132 RH |
3501 | i = num_locals2++; |
3502 | else | |
2633a79c | 3503 | continue; |
252b5132 RH |
3504 | new_syms[i] = sym; |
3505 | sym->udata.i = i + 1; | |
3506 | } | |
3507 | for (asect = abfd->sections; asect; asect = asect->next) | |
3508 | { | |
079e9a2f | 3509 | if (sect_syms[asect->index] == NULL) |
252b5132 | 3510 | { |
079e9a2f | 3511 | asymbol *sym = asect->symbol; |
dc810e39 | 3512 | unsigned int i; |
252b5132 | 3513 | |
079e9a2f | 3514 | sect_syms[asect->index] = sym; |
252b5132 RH |
3515 | if (!sym_is_global (abfd, sym)) |
3516 | i = num_locals2++; | |
3517 | else | |
3518 | i = num_locals + num_globals2++; | |
3519 | new_syms[i] = sym; | |
3520 | sym->udata.i = i + 1; | |
3521 | } | |
3522 | } | |
3523 | ||
3524 | bfd_set_symtab (abfd, new_syms, num_locals + num_globals); | |
3525 | ||
12bd6957 | 3526 | *pnum_locals = num_locals; |
b34976b6 | 3527 | return TRUE; |
252b5132 RH |
3528 | } |
3529 | ||
3530 | /* Align to the maximum file alignment that could be required for any | |
3531 | ELF data structure. */ | |
3532 | ||
268b6b39 | 3533 | static inline file_ptr |
217aa764 | 3534 | align_file_position (file_ptr off, int align) |
252b5132 RH |
3535 | { |
3536 | return (off + align - 1) & ~(align - 1); | |
3537 | } | |
3538 | ||
3539 | /* Assign a file position to a section, optionally aligning to the | |
3540 | required section alignment. */ | |
3541 | ||
217aa764 AM |
3542 | file_ptr |
3543 | _bfd_elf_assign_file_position_for_section (Elf_Internal_Shdr *i_shdrp, | |
3544 | file_ptr offset, | |
3545 | bfd_boolean align) | |
252b5132 | 3546 | { |
72de5009 AM |
3547 | if (align && i_shdrp->sh_addralign > 1) |
3548 | offset = BFD_ALIGN (offset, i_shdrp->sh_addralign); | |
252b5132 RH |
3549 | i_shdrp->sh_offset = offset; |
3550 | if (i_shdrp->bfd_section != NULL) | |
3551 | i_shdrp->bfd_section->filepos = offset; | |
3552 | if (i_shdrp->sh_type != SHT_NOBITS) | |
3553 | offset += i_shdrp->sh_size; | |
3554 | return offset; | |
3555 | } | |
3556 | ||
3557 | /* Compute the file positions we are going to put the sections at, and | |
3558 | otherwise prepare to begin writing out the ELF file. If LINK_INFO | |
3559 | is not NULL, this is being called by the ELF backend linker. */ | |
3560 | ||
b34976b6 | 3561 | bfd_boolean |
217aa764 AM |
3562 | _bfd_elf_compute_section_file_positions (bfd *abfd, |
3563 | struct bfd_link_info *link_info) | |
252b5132 | 3564 | { |
9c5bfbb7 | 3565 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
d4730f92 | 3566 | struct fake_section_arg fsargs; |
b34976b6 | 3567 | bfd_boolean failed; |
4b6c0f2f | 3568 | struct bfd_strtab_hash *strtab = NULL; |
252b5132 | 3569 | Elf_Internal_Shdr *shstrtab_hdr; |
3516e984 | 3570 | bfd_boolean need_symtab; |
252b5132 RH |
3571 | |
3572 | if (abfd->output_has_begun) | |
b34976b6 | 3573 | return TRUE; |
252b5132 RH |
3574 | |
3575 | /* Do any elf backend specific processing first. */ | |
3576 | if (bed->elf_backend_begin_write_processing) | |
3577 | (*bed->elf_backend_begin_write_processing) (abfd, link_info); | |
3578 | ||
3579 | if (! prep_headers (abfd)) | |
b34976b6 | 3580 | return FALSE; |
252b5132 | 3581 | |
e6c51ed4 | 3582 | /* Post process the headers if necessary. */ |
78245035 | 3583 | (*bed->elf_backend_post_process_headers) (abfd, link_info); |
e6c51ed4 | 3584 | |
d4730f92 BS |
3585 | fsargs.failed = FALSE; |
3586 | fsargs.link_info = link_info; | |
3587 | bfd_map_over_sections (abfd, elf_fake_sections, &fsargs); | |
3588 | if (fsargs.failed) | |
b34976b6 | 3589 | return FALSE; |
252b5132 | 3590 | |
da9f89d4 | 3591 | if (!assign_section_numbers (abfd, link_info)) |
b34976b6 | 3592 | return FALSE; |
252b5132 RH |
3593 | |
3594 | /* The backend linker builds symbol table information itself. */ | |
3516e984 L |
3595 | need_symtab = (link_info == NULL |
3596 | && (bfd_get_symcount (abfd) > 0 | |
3597 | || ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC)) | |
3598 | == HAS_RELOC))); | |
3599 | if (need_symtab) | |
252b5132 RH |
3600 | { |
3601 | /* Non-zero if doing a relocatable link. */ | |
3602 | int relocatable_p = ! (abfd->flags & (EXEC_P | DYNAMIC)); | |
3603 | ||
3604 | if (! swap_out_syms (abfd, &strtab, relocatable_p)) | |
b34976b6 | 3605 | return FALSE; |
252b5132 RH |
3606 | } |
3607 | ||
d4730f92 | 3608 | failed = FALSE; |
1126897b | 3609 | if (link_info == NULL) |
dbb410c3 | 3610 | { |
1126897b | 3611 | bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed); |
dbb410c3 | 3612 | if (failed) |
b34976b6 | 3613 | return FALSE; |
dbb410c3 AM |
3614 | } |
3615 | ||
252b5132 RH |
3616 | shstrtab_hdr = &elf_tdata (abfd)->shstrtab_hdr; |
3617 | /* sh_name was set in prep_headers. */ | |
3618 | shstrtab_hdr->sh_type = SHT_STRTAB; | |
3619 | shstrtab_hdr->sh_flags = 0; | |
3620 | shstrtab_hdr->sh_addr = 0; | |
2b0f7ef9 | 3621 | shstrtab_hdr->sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd)); |
252b5132 RH |
3622 | shstrtab_hdr->sh_entsize = 0; |
3623 | shstrtab_hdr->sh_link = 0; | |
3624 | shstrtab_hdr->sh_info = 0; | |
3625 | /* sh_offset is set in assign_file_positions_except_relocs. */ | |
3626 | shstrtab_hdr->sh_addralign = 1; | |
3627 | ||
c84fca4d | 3628 | if (!assign_file_positions_except_relocs (abfd, link_info)) |
b34976b6 | 3629 | return FALSE; |
252b5132 | 3630 | |
3516e984 | 3631 | if (need_symtab) |
252b5132 RH |
3632 | { |
3633 | file_ptr off; | |
3634 | Elf_Internal_Shdr *hdr; | |
3635 | ||
12bd6957 | 3636 | off = elf_next_file_pos (abfd); |
252b5132 RH |
3637 | |
3638 | hdr = &elf_tdata (abfd)->symtab_hdr; | |
b34976b6 | 3639 | off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE); |
252b5132 | 3640 | |
9ad5cbcf AM |
3641 | hdr = &elf_tdata (abfd)->symtab_shndx_hdr; |
3642 | if (hdr->sh_size != 0) | |
b34976b6 | 3643 | off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE); |
9ad5cbcf | 3644 | |
252b5132 | 3645 | hdr = &elf_tdata (abfd)->strtab_hdr; |
b34976b6 | 3646 | off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE); |
252b5132 | 3647 | |
12bd6957 | 3648 | elf_next_file_pos (abfd) = off; |
252b5132 RH |
3649 | |
3650 | /* Now that we know where the .strtab section goes, write it | |
08a40648 | 3651 | out. */ |
252b5132 RH |
3652 | if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0 |
3653 | || ! _bfd_stringtab_emit (abfd, strtab)) | |
b34976b6 | 3654 | return FALSE; |
252b5132 RH |
3655 | _bfd_stringtab_free (strtab); |
3656 | } | |
3657 | ||
b34976b6 | 3658 | abfd->output_has_begun = TRUE; |
252b5132 | 3659 | |
b34976b6 | 3660 | return TRUE; |
252b5132 RH |
3661 | } |
3662 | ||
8ded5a0f AM |
3663 | /* Make an initial estimate of the size of the program header. If we |
3664 | get the number wrong here, we'll redo section placement. */ | |
3665 | ||
3666 | static bfd_size_type | |
3667 | get_program_header_size (bfd *abfd, struct bfd_link_info *info) | |
3668 | { | |
3669 | size_t segs; | |
3670 | asection *s; | |
2b05f1b7 | 3671 | const struct elf_backend_data *bed; |
8ded5a0f AM |
3672 | |
3673 | /* Assume we will need exactly two PT_LOAD segments: one for text | |
3674 | and one for data. */ | |
3675 | segs = 2; | |
3676 | ||
3677 | s = bfd_get_section_by_name (abfd, ".interp"); | |
3678 | if (s != NULL && (s->flags & SEC_LOAD) != 0) | |
3679 | { | |
3680 | /* If we have a loadable interpreter section, we need a | |
3681 | PT_INTERP segment. In this case, assume we also need a | |
3682 | PT_PHDR segment, although that may not be true for all | |
3683 | targets. */ | |
3684 | segs += 2; | |
3685 | } | |
3686 | ||
3687 | if (bfd_get_section_by_name (abfd, ".dynamic") != NULL) | |
3688 | { | |
3689 | /* We need a PT_DYNAMIC segment. */ | |
3690 | ++segs; | |
f210dcff | 3691 | } |
08a40648 | 3692 | |
ceae84aa | 3693 | if (info != NULL && info->relro) |
f210dcff L |
3694 | { |
3695 | /* We need a PT_GNU_RELRO segment. */ | |
3696 | ++segs; | |
8ded5a0f AM |
3697 | } |
3698 | ||
12bd6957 | 3699 | if (elf_eh_frame_hdr (abfd)) |
8ded5a0f AM |
3700 | { |
3701 | /* We need a PT_GNU_EH_FRAME segment. */ | |
3702 | ++segs; | |
3703 | } | |
3704 | ||
12bd6957 | 3705 | if (elf_stack_flags (abfd)) |
8ded5a0f | 3706 | { |
2b05f1b7 L |
3707 | /* We need a PT_GNU_STACK segment. */ |
3708 | ++segs; | |
3709 | } | |
94b11780 | 3710 | |
2b05f1b7 L |
3711 | for (s = abfd->sections; s != NULL; s = s->next) |
3712 | { | |
8ded5a0f | 3713 | if ((s->flags & SEC_LOAD) != 0 |
0112cd26 | 3714 | && CONST_STRNEQ (s->name, ".note")) |
8ded5a0f AM |
3715 | { |
3716 | /* We need a PT_NOTE segment. */ | |
3717 | ++segs; | |
1c5265b5 JJ |
3718 | /* Try to create just one PT_NOTE segment |
3719 | for all adjacent loadable .note* sections. | |
3720 | gABI requires that within a PT_NOTE segment | |
3721 | (and also inside of each SHT_NOTE section) | |
3722 | each note is padded to a multiple of 4 size, | |
3723 | so we check whether the sections are correctly | |
3724 | aligned. */ | |
3725 | if (s->alignment_power == 2) | |
3726 | while (s->next != NULL | |
3727 | && s->next->alignment_power == 2 | |
3728 | && (s->next->flags & SEC_LOAD) != 0 | |
3729 | && CONST_STRNEQ (s->next->name, ".note")) | |
3730 | s = s->next; | |
8ded5a0f AM |
3731 | } |
3732 | } | |
3733 | ||
3734 | for (s = abfd->sections; s != NULL; s = s->next) | |
3735 | { | |
3736 | if (s->flags & SEC_THREAD_LOCAL) | |
3737 | { | |
3738 | /* We need a PT_TLS segment. */ | |
3739 | ++segs; | |
3740 | break; | |
3741 | } | |
3742 | } | |
3743 | ||
3744 | /* Let the backend count up any program headers it might need. */ | |
2b05f1b7 | 3745 | bed = get_elf_backend_data (abfd); |
8ded5a0f AM |
3746 | if (bed->elf_backend_additional_program_headers) |
3747 | { | |
3748 | int a; | |
3749 | ||
3750 | a = (*bed->elf_backend_additional_program_headers) (abfd, info); | |
3751 | if (a == -1) | |
3752 | abort (); | |
3753 | segs += a; | |
3754 | } | |
3755 | ||
3756 | return segs * bed->s->sizeof_phdr; | |
3757 | } | |
3758 | ||
2ea37f1c NC |
3759 | /* Find the segment that contains the output_section of section. */ |
3760 | ||
3761 | Elf_Internal_Phdr * | |
3762 | _bfd_elf_find_segment_containing_section (bfd * abfd, asection * section) | |
3763 | { | |
3764 | struct elf_segment_map *m; | |
3765 | Elf_Internal_Phdr *p; | |
3766 | ||
12bd6957 | 3767 | for (m = elf_seg_map (abfd), p = elf_tdata (abfd)->phdr; |
2ea37f1c NC |
3768 | m != NULL; |
3769 | m = m->next, p++) | |
3770 | { | |
3771 | int i; | |
3772 | ||
3773 | for (i = m->count - 1; i >= 0; i--) | |
3774 | if (m->sections[i] == section) | |
3775 | return p; | |
3776 | } | |
3777 | ||
3778 | return NULL; | |
3779 | } | |
3780 | ||
252b5132 RH |
3781 | /* Create a mapping from a set of sections to a program segment. */ |
3782 | ||
217aa764 AM |
3783 | static struct elf_segment_map * |
3784 | make_mapping (bfd *abfd, | |
3785 | asection **sections, | |
3786 | unsigned int from, | |
3787 | unsigned int to, | |
3788 | bfd_boolean phdr) | |
252b5132 RH |
3789 | { |
3790 | struct elf_segment_map *m; | |
3791 | unsigned int i; | |
3792 | asection **hdrpp; | |
dc810e39 | 3793 | bfd_size_type amt; |
252b5132 | 3794 | |
dc810e39 AM |
3795 | amt = sizeof (struct elf_segment_map); |
3796 | amt += (to - from - 1) * sizeof (asection *); | |
a50b1753 | 3797 | m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); |
252b5132 RH |
3798 | if (m == NULL) |
3799 | return NULL; | |
3800 | m->next = NULL; | |
3801 | m->p_type = PT_LOAD; | |
3802 | for (i = from, hdrpp = sections + from; i < to; i++, hdrpp++) | |
3803 | m->sections[i - from] = *hdrpp; | |
3804 | m->count = to - from; | |
3805 | ||
3806 | if (from == 0 && phdr) | |
3807 | { | |
3808 | /* Include the headers in the first PT_LOAD segment. */ | |
3809 | m->includes_filehdr = 1; | |
3810 | m->includes_phdrs = 1; | |
3811 | } | |
3812 | ||
3813 | return m; | |
3814 | } | |
3815 | ||
229fcec5 MM |
3816 | /* Create the PT_DYNAMIC segment, which includes DYNSEC. Returns NULL |
3817 | on failure. */ | |
3818 | ||
3819 | struct elf_segment_map * | |
3820 | _bfd_elf_make_dynamic_segment (bfd *abfd, asection *dynsec) | |
3821 | { | |
3822 | struct elf_segment_map *m; | |
3823 | ||
a50b1753 NC |
3824 | m = (struct elf_segment_map *) bfd_zalloc (abfd, |
3825 | sizeof (struct elf_segment_map)); | |
229fcec5 MM |
3826 | if (m == NULL) |
3827 | return NULL; | |
3828 | m->next = NULL; | |
3829 | m->p_type = PT_DYNAMIC; | |
3830 | m->count = 1; | |
3831 | m->sections[0] = dynsec; | |
08a40648 | 3832 | |
229fcec5 MM |
3833 | return m; |
3834 | } | |
3835 | ||
8ded5a0f | 3836 | /* Possibly add or remove segments from the segment map. */ |
252b5132 | 3837 | |
b34976b6 | 3838 | static bfd_boolean |
3dea8fca AM |
3839 | elf_modify_segment_map (bfd *abfd, |
3840 | struct bfd_link_info *info, | |
3841 | bfd_boolean remove_empty_load) | |
252b5132 | 3842 | { |
252e386e | 3843 | struct elf_segment_map **m; |
8ded5a0f | 3844 | const struct elf_backend_data *bed; |
252b5132 | 3845 | |
8ded5a0f AM |
3846 | /* The placement algorithm assumes that non allocated sections are |
3847 | not in PT_LOAD segments. We ensure this here by removing such | |
3848 | sections from the segment map. We also remove excluded | |
252e386e AM |
3849 | sections. Finally, any PT_LOAD segment without sections is |
3850 | removed. */ | |
12bd6957 | 3851 | m = &elf_seg_map (abfd); |
252e386e | 3852 | while (*m) |
8ded5a0f AM |
3853 | { |
3854 | unsigned int i, new_count; | |
252b5132 | 3855 | |
252e386e | 3856 | for (new_count = 0, i = 0; i < (*m)->count; i++) |
8ded5a0f | 3857 | { |
252e386e AM |
3858 | if (((*m)->sections[i]->flags & SEC_EXCLUDE) == 0 |
3859 | && (((*m)->sections[i]->flags & SEC_ALLOC) != 0 | |
3860 | || (*m)->p_type != PT_LOAD)) | |
8ded5a0f | 3861 | { |
252e386e AM |
3862 | (*m)->sections[new_count] = (*m)->sections[i]; |
3863 | new_count++; | |
8ded5a0f AM |
3864 | } |
3865 | } | |
252e386e | 3866 | (*m)->count = new_count; |
252b5132 | 3867 | |
3dea8fca | 3868 | if (remove_empty_load && (*m)->p_type == PT_LOAD && (*m)->count == 0) |
252e386e AM |
3869 | *m = (*m)->next; |
3870 | else | |
3871 | m = &(*m)->next; | |
8ded5a0f | 3872 | } |
252b5132 | 3873 | |
8ded5a0f AM |
3874 | bed = get_elf_backend_data (abfd); |
3875 | if (bed->elf_backend_modify_segment_map != NULL) | |
252b5132 | 3876 | { |
252e386e | 3877 | if (!(*bed->elf_backend_modify_segment_map) (abfd, info)) |
8ded5a0f | 3878 | return FALSE; |
252b5132 | 3879 | } |
252b5132 | 3880 | |
8ded5a0f AM |
3881 | return TRUE; |
3882 | } | |
252b5132 | 3883 | |
8ded5a0f | 3884 | /* Set up a mapping from BFD sections to program segments. */ |
252b5132 | 3885 | |
8ded5a0f AM |
3886 | bfd_boolean |
3887 | _bfd_elf_map_sections_to_segments (bfd *abfd, struct bfd_link_info *info) | |
3888 | { | |
3889 | unsigned int count; | |
3890 | struct elf_segment_map *m; | |
3891 | asection **sections = NULL; | |
3892 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
3dea8fca | 3893 | bfd_boolean no_user_phdrs; |
252b5132 | 3894 | |
12bd6957 | 3895 | no_user_phdrs = elf_seg_map (abfd) == NULL; |
d324f6d6 RM |
3896 | |
3897 | if (info != NULL) | |
3898 | info->user_phdrs = !no_user_phdrs; | |
3899 | ||
3dea8fca | 3900 | if (no_user_phdrs && bfd_count_sections (abfd) != 0) |
252b5132 | 3901 | { |
8ded5a0f AM |
3902 | asection *s; |
3903 | unsigned int i; | |
3904 | struct elf_segment_map *mfirst; | |
3905 | struct elf_segment_map **pm; | |
3906 | asection *last_hdr; | |
3907 | bfd_vma last_size; | |
3908 | unsigned int phdr_index; | |
3909 | bfd_vma maxpagesize; | |
3910 | asection **hdrpp; | |
3911 | bfd_boolean phdr_in_segment = TRUE; | |
3912 | bfd_boolean writable; | |
3913 | int tls_count = 0; | |
3914 | asection *first_tls = NULL; | |
3915 | asection *dynsec, *eh_frame_hdr; | |
3916 | bfd_size_type amt; | |
8d06853e | 3917 | bfd_vma addr_mask, wrap_to = 0; |
252b5132 | 3918 | |
8ded5a0f | 3919 | /* Select the allocated sections, and sort them. */ |
252b5132 | 3920 | |
a50b1753 NC |
3921 | sections = (asection **) bfd_malloc2 (bfd_count_sections (abfd), |
3922 | sizeof (asection *)); | |
8ded5a0f | 3923 | if (sections == NULL) |
252b5132 | 3924 | goto error_return; |
252b5132 | 3925 | |
8d06853e AM |
3926 | /* Calculate top address, avoiding undefined behaviour of shift |
3927 | left operator when shift count is equal to size of type | |
3928 | being shifted. */ | |
3929 | addr_mask = ((bfd_vma) 1 << (bfd_arch_bits_per_address (abfd) - 1)) - 1; | |
3930 | addr_mask = (addr_mask << 1) + 1; | |
3931 | ||
8ded5a0f AM |
3932 | i = 0; |
3933 | for (s = abfd->sections; s != NULL; s = s->next) | |
3934 | { | |
3935 | if ((s->flags & SEC_ALLOC) != 0) | |
3936 | { | |
3937 | sections[i] = s; | |
3938 | ++i; | |
8d06853e AM |
3939 | /* A wrapping section potentially clashes with header. */ |
3940 | if (((s->lma + s->size) & addr_mask) < (s->lma & addr_mask)) | |
3941 | wrap_to = (s->lma + s->size) & addr_mask; | |
8ded5a0f AM |
3942 | } |
3943 | } | |
3944 | BFD_ASSERT (i <= bfd_count_sections (abfd)); | |
3945 | count = i; | |
252b5132 | 3946 | |
8ded5a0f | 3947 | qsort (sections, (size_t) count, sizeof (asection *), elf_sort_sections); |
252b5132 | 3948 | |
8ded5a0f | 3949 | /* Build the mapping. */ |
252b5132 | 3950 | |
8ded5a0f AM |
3951 | mfirst = NULL; |
3952 | pm = &mfirst; | |
252b5132 | 3953 | |
8ded5a0f AM |
3954 | /* If we have a .interp section, then create a PT_PHDR segment for |
3955 | the program headers and a PT_INTERP segment for the .interp | |
3956 | section. */ | |
3957 | s = bfd_get_section_by_name (abfd, ".interp"); | |
3958 | if (s != NULL && (s->flags & SEC_LOAD) != 0) | |
3959 | { | |
3960 | amt = sizeof (struct elf_segment_map); | |
a50b1753 | 3961 | m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); |
8ded5a0f AM |
3962 | if (m == NULL) |
3963 | goto error_return; | |
3964 | m->next = NULL; | |
3965 | m->p_type = PT_PHDR; | |
3966 | /* FIXME: UnixWare and Solaris set PF_X, Irix 5 does not. */ | |
3967 | m->p_flags = PF_R | PF_X; | |
3968 | m->p_flags_valid = 1; | |
3969 | m->includes_phdrs = 1; | |
252b5132 | 3970 | |
8ded5a0f AM |
3971 | *pm = m; |
3972 | pm = &m->next; | |
252b5132 | 3973 | |
8ded5a0f | 3974 | amt = sizeof (struct elf_segment_map); |
a50b1753 | 3975 | m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); |
8ded5a0f AM |
3976 | if (m == NULL) |
3977 | goto error_return; | |
3978 | m->next = NULL; | |
3979 | m->p_type = PT_INTERP; | |
3980 | m->count = 1; | |
3981 | m->sections[0] = s; | |
3982 | ||
3983 | *pm = m; | |
3984 | pm = &m->next; | |
252b5132 | 3985 | } |
8ded5a0f AM |
3986 | |
3987 | /* Look through the sections. We put sections in the same program | |
3988 | segment when the start of the second section can be placed within | |
3989 | a few bytes of the end of the first section. */ | |
3990 | last_hdr = NULL; | |
3991 | last_size = 0; | |
3992 | phdr_index = 0; | |
3993 | maxpagesize = bed->maxpagesize; | |
3994 | writable = FALSE; | |
3995 | dynsec = bfd_get_section_by_name (abfd, ".dynamic"); | |
3996 | if (dynsec != NULL | |
3997 | && (dynsec->flags & SEC_LOAD) == 0) | |
3998 | dynsec = NULL; | |
3999 | ||
4000 | /* Deal with -Ttext or something similar such that the first section | |
4001 | is not adjacent to the program headers. This is an | |
4002 | approximation, since at this point we don't know exactly how many | |
4003 | program headers we will need. */ | |
4004 | if (count > 0) | |
252b5132 | 4005 | { |
12bd6957 | 4006 | bfd_size_type phdr_size = elf_program_header_size (abfd); |
8ded5a0f | 4007 | |
62d7a5f6 | 4008 | if (phdr_size == (bfd_size_type) -1) |
8ded5a0f | 4009 | phdr_size = get_program_header_size (abfd, info); |
d2bcb0d1 | 4010 | phdr_size += bed->s->sizeof_ehdr; |
8ded5a0f | 4011 | if ((abfd->flags & D_PAGED) == 0 |
8d06853e AM |
4012 | || (sections[0]->lma & addr_mask) < phdr_size |
4013 | || ((sections[0]->lma & addr_mask) % maxpagesize | |
4014 | < phdr_size % maxpagesize) | |
4015 | || (sections[0]->lma & addr_mask & -maxpagesize) < wrap_to) | |
8ded5a0f | 4016 | phdr_in_segment = FALSE; |
252b5132 RH |
4017 | } |
4018 | ||
8ded5a0f | 4019 | for (i = 0, hdrpp = sections; i < count; i++, hdrpp++) |
252b5132 | 4020 | { |
8ded5a0f AM |
4021 | asection *hdr; |
4022 | bfd_boolean new_segment; | |
4023 | ||
4024 | hdr = *hdrpp; | |
4025 | ||
4026 | /* See if this section and the last one will fit in the same | |
4027 | segment. */ | |
4028 | ||
4029 | if (last_hdr == NULL) | |
4030 | { | |
4031 | /* If we don't have a segment yet, then we don't need a new | |
4032 | one (we build the last one after this loop). */ | |
4033 | new_segment = FALSE; | |
4034 | } | |
4035 | else if (last_hdr->lma - last_hdr->vma != hdr->lma - hdr->vma) | |
4036 | { | |
4037 | /* If this section has a different relation between the | |
4038 | virtual address and the load address, then we need a new | |
4039 | segment. */ | |
4040 | new_segment = TRUE; | |
4041 | } | |
b5599592 AM |
4042 | else if (hdr->lma < last_hdr->lma + last_size |
4043 | || last_hdr->lma + last_size < last_hdr->lma) | |
4044 | { | |
4045 | /* If this section has a load address that makes it overlap | |
4046 | the previous section, then we need a new segment. */ | |
4047 | new_segment = TRUE; | |
4048 | } | |
39948a60 NC |
4049 | /* In the next test we have to be careful when last_hdr->lma is close |
4050 | to the end of the address space. If the aligned address wraps | |
4051 | around to the start of the address space, then there are no more | |
4052 | pages left in memory and it is OK to assume that the current | |
4053 | section can be included in the current segment. */ | |
4054 | else if ((BFD_ALIGN (last_hdr->lma + last_size, maxpagesize) + maxpagesize | |
4055 | > last_hdr->lma) | |
4056 | && (BFD_ALIGN (last_hdr->lma + last_size, maxpagesize) + maxpagesize | |
4ff73856 | 4057 | <= hdr->lma)) |
8ded5a0f AM |
4058 | { |
4059 | /* If putting this section in this segment would force us to | |
4060 | skip a page in the segment, then we need a new segment. */ | |
4061 | new_segment = TRUE; | |
4062 | } | |
4063 | else if ((last_hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0 | |
4064 | && (hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) != 0) | |
4065 | { | |
4066 | /* We don't want to put a loadable section after a | |
4067 | nonloadable section in the same segment. | |
4068 | Consider .tbss sections as loadable for this purpose. */ | |
4069 | new_segment = TRUE; | |
4070 | } | |
4071 | else if ((abfd->flags & D_PAGED) == 0) | |
4072 | { | |
4073 | /* If the file is not demand paged, which means that we | |
4074 | don't require the sections to be correctly aligned in the | |
4075 | file, then there is no other reason for a new segment. */ | |
4076 | new_segment = FALSE; | |
4077 | } | |
4078 | else if (! writable | |
4079 | && (hdr->flags & SEC_READONLY) == 0 | |
8d06853e AM |
4080 | && (((last_hdr->lma + last_size - 1) & -maxpagesize) |
4081 | != (hdr->lma & -maxpagesize))) | |
8ded5a0f AM |
4082 | { |
4083 | /* We don't want to put a writable section in a read only | |
4084 | segment, unless they are on the same page in memory | |
4085 | anyhow. We already know that the last section does not | |
4086 | bring us past the current section on the page, so the | |
4087 | only case in which the new section is not on the same | |
4088 | page as the previous section is when the previous section | |
4089 | ends precisely on a page boundary. */ | |
4090 | new_segment = TRUE; | |
4091 | } | |
4092 | else | |
4093 | { | |
4094 | /* Otherwise, we can use the same segment. */ | |
4095 | new_segment = FALSE; | |
4096 | } | |
4097 | ||
2889e75b | 4098 | /* Allow interested parties a chance to override our decision. */ |
ceae84aa AM |
4099 | if (last_hdr != NULL |
4100 | && info != NULL | |
4101 | && info->callbacks->override_segment_assignment != NULL) | |
4102 | new_segment | |
4103 | = info->callbacks->override_segment_assignment (info, abfd, hdr, | |
4104 | last_hdr, | |
4105 | new_segment); | |
2889e75b | 4106 | |
8ded5a0f AM |
4107 | if (! new_segment) |
4108 | { | |
4109 | if ((hdr->flags & SEC_READONLY) == 0) | |
4110 | writable = TRUE; | |
4111 | last_hdr = hdr; | |
4112 | /* .tbss sections effectively have zero size. */ | |
4113 | if ((hdr->flags & (SEC_THREAD_LOCAL | SEC_LOAD)) | |
4114 | != SEC_THREAD_LOCAL) | |
4115 | last_size = hdr->size; | |
4116 | else | |
4117 | last_size = 0; | |
4118 | continue; | |
4119 | } | |
4120 | ||
4121 | /* We need a new program segment. We must create a new program | |
4122 | header holding all the sections from phdr_index until hdr. */ | |
4123 | ||
4124 | m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment); | |
4125 | if (m == NULL) | |
4126 | goto error_return; | |
4127 | ||
4128 | *pm = m; | |
4129 | pm = &m->next; | |
4130 | ||
252b5132 | 4131 | if ((hdr->flags & SEC_READONLY) == 0) |
b34976b6 | 4132 | writable = TRUE; |
8ded5a0f AM |
4133 | else |
4134 | writable = FALSE; | |
4135 | ||
baaff79e JJ |
4136 | last_hdr = hdr; |
4137 | /* .tbss sections effectively have zero size. */ | |
e5caec89 | 4138 | if ((hdr->flags & (SEC_THREAD_LOCAL | SEC_LOAD)) != SEC_THREAD_LOCAL) |
eea6121a | 4139 | last_size = hdr->size; |
baaff79e JJ |
4140 | else |
4141 | last_size = 0; | |
8ded5a0f AM |
4142 | phdr_index = i; |
4143 | phdr_in_segment = FALSE; | |
252b5132 RH |
4144 | } |
4145 | ||
86b2281f AM |
4146 | /* Create a final PT_LOAD program segment, but not if it's just |
4147 | for .tbss. */ | |
4148 | if (last_hdr != NULL | |
4149 | && (i - phdr_index != 1 | |
4150 | || ((last_hdr->flags & (SEC_THREAD_LOCAL | SEC_LOAD)) | |
4151 | != SEC_THREAD_LOCAL))) | |
8ded5a0f AM |
4152 | { |
4153 | m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment); | |
4154 | if (m == NULL) | |
4155 | goto error_return; | |
252b5132 | 4156 | |
8ded5a0f AM |
4157 | *pm = m; |
4158 | pm = &m->next; | |
4159 | } | |
252b5132 | 4160 | |
8ded5a0f AM |
4161 | /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */ |
4162 | if (dynsec != NULL) | |
4163 | { | |
4164 | m = _bfd_elf_make_dynamic_segment (abfd, dynsec); | |
4165 | if (m == NULL) | |
4166 | goto error_return; | |
4167 | *pm = m; | |
4168 | pm = &m->next; | |
4169 | } | |
252b5132 | 4170 | |
1c5265b5 JJ |
4171 | /* For each batch of consecutive loadable .note sections, |
4172 | add a PT_NOTE segment. We don't use bfd_get_section_by_name, | |
4173 | because if we link together nonloadable .note sections and | |
4174 | loadable .note sections, we will generate two .note sections | |
4175 | in the output file. FIXME: Using names for section types is | |
4176 | bogus anyhow. */ | |
8ded5a0f AM |
4177 | for (s = abfd->sections; s != NULL; s = s->next) |
4178 | { | |
4179 | if ((s->flags & SEC_LOAD) != 0 | |
0112cd26 | 4180 | && CONST_STRNEQ (s->name, ".note")) |
8ded5a0f | 4181 | { |
1c5265b5 | 4182 | asection *s2; |
91d6fa6a NC |
4183 | |
4184 | count = 1; | |
8ded5a0f | 4185 | amt = sizeof (struct elf_segment_map); |
1c5265b5 JJ |
4186 | if (s->alignment_power == 2) |
4187 | for (s2 = s; s2->next != NULL; s2 = s2->next) | |
55b581a6 JJ |
4188 | { |
4189 | if (s2->next->alignment_power == 2 | |
4190 | && (s2->next->flags & SEC_LOAD) != 0 | |
4191 | && CONST_STRNEQ (s2->next->name, ".note") | |
8d06853e AM |
4192 | && align_power (s2->lma + s2->size, 2) |
4193 | == s2->next->lma) | |
55b581a6 JJ |
4194 | count++; |
4195 | else | |
4196 | break; | |
4197 | } | |
1c5265b5 | 4198 | amt += (count - 1) * sizeof (asection *); |
a50b1753 | 4199 | m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); |
8ded5a0f AM |
4200 | if (m == NULL) |
4201 | goto error_return; | |
4202 | m->next = NULL; | |
4203 | m->p_type = PT_NOTE; | |
1c5265b5 JJ |
4204 | m->count = count; |
4205 | while (count > 1) | |
4206 | { | |
4207 | m->sections[m->count - count--] = s; | |
4208 | BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0); | |
4209 | s = s->next; | |
4210 | } | |
4211 | m->sections[m->count - 1] = s; | |
4212 | BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0); | |
8ded5a0f AM |
4213 | *pm = m; |
4214 | pm = &m->next; | |
4215 | } | |
4216 | if (s->flags & SEC_THREAD_LOCAL) | |
4217 | { | |
4218 | if (! tls_count) | |
4219 | first_tls = s; | |
4220 | tls_count++; | |
4221 | } | |
4222 | } | |
252b5132 | 4223 | |
8ded5a0f AM |
4224 | /* If there are any SHF_TLS output sections, add PT_TLS segment. */ |
4225 | if (tls_count > 0) | |
4226 | { | |
8ded5a0f AM |
4227 | amt = sizeof (struct elf_segment_map); |
4228 | amt += (tls_count - 1) * sizeof (asection *); | |
a50b1753 | 4229 | m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); |
8ded5a0f AM |
4230 | if (m == NULL) |
4231 | goto error_return; | |
4232 | m->next = NULL; | |
4233 | m->p_type = PT_TLS; | |
4234 | m->count = tls_count; | |
4235 | /* Mandated PF_R. */ | |
4236 | m->p_flags = PF_R; | |
4237 | m->p_flags_valid = 1; | |
d923cae0 | 4238 | s = first_tls; |
91d6fa6a | 4239 | for (i = 0; i < (unsigned int) tls_count; ++i) |
8ded5a0f | 4240 | { |
d923cae0 L |
4241 | if ((s->flags & SEC_THREAD_LOCAL) == 0) |
4242 | { | |
4243 | _bfd_error_handler | |
4244 | (_("%B: TLS sections are not adjacent:"), abfd); | |
4245 | s = first_tls; | |
4246 | i = 0; | |
4247 | while (i < (unsigned int) tls_count) | |
4248 | { | |
4249 | if ((s->flags & SEC_THREAD_LOCAL) != 0) | |
4250 | { | |
4251 | _bfd_error_handler (_(" TLS: %A"), s); | |
4252 | i++; | |
4253 | } | |
4254 | else | |
4255 | _bfd_error_handler (_(" non-TLS: %A"), s); | |
4256 | s = s->next; | |
4257 | } | |
4258 | bfd_set_error (bfd_error_bad_value); | |
4259 | goto error_return; | |
4260 | } | |
4261 | m->sections[i] = s; | |
4262 | s = s->next; | |
8ded5a0f | 4263 | } |
252b5132 | 4264 | |
8ded5a0f AM |
4265 | *pm = m; |
4266 | pm = &m->next; | |
4267 | } | |
252b5132 | 4268 | |
8ded5a0f AM |
4269 | /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME |
4270 | segment. */ | |
12bd6957 | 4271 | eh_frame_hdr = elf_eh_frame_hdr (abfd); |
8ded5a0f AM |
4272 | if (eh_frame_hdr != NULL |
4273 | && (eh_frame_hdr->output_section->flags & SEC_LOAD) != 0) | |
252b5132 | 4274 | { |
dc810e39 | 4275 | amt = sizeof (struct elf_segment_map); |
a50b1753 | 4276 | m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); |
252b5132 RH |
4277 | if (m == NULL) |
4278 | goto error_return; | |
4279 | m->next = NULL; | |
8ded5a0f | 4280 | m->p_type = PT_GNU_EH_FRAME; |
252b5132 | 4281 | m->count = 1; |
8ded5a0f | 4282 | m->sections[0] = eh_frame_hdr->output_section; |
252b5132 RH |
4283 | |
4284 | *pm = m; | |
4285 | pm = &m->next; | |
4286 | } | |
13ae64f3 | 4287 | |
12bd6957 | 4288 | if (elf_stack_flags (abfd)) |
13ae64f3 | 4289 | { |
8ded5a0f | 4290 | amt = sizeof (struct elf_segment_map); |
a50b1753 | 4291 | m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); |
8ded5a0f AM |
4292 | if (m == NULL) |
4293 | goto error_return; | |
4294 | m->next = NULL; | |
2b05f1b7 | 4295 | m->p_type = PT_GNU_STACK; |
12bd6957 | 4296 | m->p_flags = elf_stack_flags (abfd); |
04c3a755 | 4297 | m->p_align = bed->stack_align; |
8ded5a0f | 4298 | m->p_flags_valid = 1; |
04c3a755 NS |
4299 | m->p_align_valid = m->p_align != 0; |
4300 | if (info->stacksize > 0) | |
4301 | { | |
4302 | m->p_size = info->stacksize; | |
4303 | m->p_size_valid = 1; | |
4304 | } | |
252b5132 | 4305 | |
8ded5a0f AM |
4306 | *pm = m; |
4307 | pm = &m->next; | |
4308 | } | |
65765700 | 4309 | |
ceae84aa | 4310 | if (info != NULL && info->relro) |
8ded5a0f | 4311 | { |
f210dcff L |
4312 | for (m = mfirst; m != NULL; m = m->next) |
4313 | { | |
3832a4d8 AM |
4314 | if (m->p_type == PT_LOAD |
4315 | && m->count != 0 | |
4316 | && m->sections[0]->vma >= info->relro_start | |
4317 | && m->sections[0]->vma < info->relro_end) | |
f210dcff | 4318 | { |
3832a4d8 AM |
4319 | i = m->count; |
4320 | while (--i != (unsigned) -1) | |
4321 | if ((m->sections[i]->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) | |
4322 | == (SEC_LOAD | SEC_HAS_CONTENTS)) | |
4323 | break; | |
4324 | ||
43a8475c | 4325 | if (i != (unsigned) -1) |
f210dcff L |
4326 | break; |
4327 | } | |
be01b344 | 4328 | } |
f210dcff L |
4329 | |
4330 | /* Make a PT_GNU_RELRO segment only when it isn't empty. */ | |
4331 | if (m != NULL) | |
4332 | { | |
4333 | amt = sizeof (struct elf_segment_map); | |
a50b1753 | 4334 | m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); |
f210dcff L |
4335 | if (m == NULL) |
4336 | goto error_return; | |
4337 | m->next = NULL; | |
4338 | m->p_type = PT_GNU_RELRO; | |
4339 | m->p_flags = PF_R; | |
4340 | m->p_flags_valid = 1; | |
4341 | ||
4342 | *pm = m; | |
4343 | pm = &m->next; | |
4344 | } | |
8ded5a0f | 4345 | } |
9ee5e499 | 4346 | |
8ded5a0f | 4347 | free (sections); |
12bd6957 | 4348 | elf_seg_map (abfd) = mfirst; |
9ee5e499 JJ |
4349 | } |
4350 | ||
3dea8fca | 4351 | if (!elf_modify_segment_map (abfd, info, no_user_phdrs)) |
8ded5a0f | 4352 | return FALSE; |
8c37241b | 4353 | |
12bd6957 | 4354 | for (count = 0, m = elf_seg_map (abfd); m != NULL; m = m->next) |
8ded5a0f | 4355 | ++count; |
12bd6957 | 4356 | elf_program_header_size (abfd) = count * bed->s->sizeof_phdr; |
252b5132 | 4357 | |
b34976b6 | 4358 | return TRUE; |
252b5132 RH |
4359 | |
4360 | error_return: | |
4361 | if (sections != NULL) | |
4362 | free (sections); | |
b34976b6 | 4363 | return FALSE; |
252b5132 RH |
4364 | } |
4365 | ||
4366 | /* Sort sections by address. */ | |
4367 | ||
4368 | static int | |
217aa764 | 4369 | elf_sort_sections (const void *arg1, const void *arg2) |
252b5132 RH |
4370 | { |
4371 | const asection *sec1 = *(const asection **) arg1; | |
4372 | const asection *sec2 = *(const asection **) arg2; | |
eecdbe52 | 4373 | bfd_size_type size1, size2; |
252b5132 RH |
4374 | |
4375 | /* Sort by LMA first, since this is the address used to | |
4376 | place the section into a segment. */ | |
4377 | if (sec1->lma < sec2->lma) | |
4378 | return -1; | |
4379 | else if (sec1->lma > sec2->lma) | |
4380 | return 1; | |
4381 | ||
4382 | /* Then sort by VMA. Normally the LMA and the VMA will be | |
4383 | the same, and this will do nothing. */ | |
4384 | if (sec1->vma < sec2->vma) | |
4385 | return -1; | |
4386 | else if (sec1->vma > sec2->vma) | |
4387 | return 1; | |
4388 | ||
4389 | /* Put !SEC_LOAD sections after SEC_LOAD ones. */ | |
4390 | ||
07c6e936 | 4391 | #define TOEND(x) (((x)->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0) |
252b5132 RH |
4392 | |
4393 | if (TOEND (sec1)) | |
4394 | { | |
4395 | if (TOEND (sec2)) | |
00a7cdc5 NC |
4396 | { |
4397 | /* If the indicies are the same, do not return 0 | |
4398 | here, but continue to try the next comparison. */ | |
4399 | if (sec1->target_index - sec2->target_index != 0) | |
4400 | return sec1->target_index - sec2->target_index; | |
4401 | } | |
252b5132 RH |
4402 | else |
4403 | return 1; | |
4404 | } | |
00a7cdc5 | 4405 | else if (TOEND (sec2)) |
252b5132 RH |
4406 | return -1; |
4407 | ||
4408 | #undef TOEND | |
4409 | ||
00a7cdc5 NC |
4410 | /* Sort by size, to put zero sized sections |
4411 | before others at the same address. */ | |
252b5132 | 4412 | |
eea6121a AM |
4413 | size1 = (sec1->flags & SEC_LOAD) ? sec1->size : 0; |
4414 | size2 = (sec2->flags & SEC_LOAD) ? sec2->size : 0; | |
eecdbe52 JJ |
4415 | |
4416 | if (size1 < size2) | |
252b5132 | 4417 | return -1; |
eecdbe52 | 4418 | if (size1 > size2) |
252b5132 RH |
4419 | return 1; |
4420 | ||
4421 | return sec1->target_index - sec2->target_index; | |
4422 | } | |
4423 | ||
340b6d91 AC |
4424 | /* Ian Lance Taylor writes: |
4425 | ||
4426 | We shouldn't be using % with a negative signed number. That's just | |
4427 | not good. We have to make sure either that the number is not | |
4428 | negative, or that the number has an unsigned type. When the types | |
4429 | are all the same size they wind up as unsigned. When file_ptr is a | |
4430 | larger signed type, the arithmetic winds up as signed long long, | |
4431 | which is wrong. | |
4432 | ||
4433 | What we're trying to say here is something like ``increase OFF by | |
4434 | the least amount that will cause it to be equal to the VMA modulo | |
4435 | the page size.'' */ | |
4436 | /* In other words, something like: | |
4437 | ||
4438 | vma_offset = m->sections[0]->vma % bed->maxpagesize; | |
4439 | off_offset = off % bed->maxpagesize; | |
4440 | if (vma_offset < off_offset) | |
4441 | adjustment = vma_offset + bed->maxpagesize - off_offset; | |
4442 | else | |
4443 | adjustment = vma_offset - off_offset; | |
08a40648 | 4444 | |
340b6d91 AC |
4445 | which can can be collapsed into the expression below. */ |
4446 | ||
4447 | static file_ptr | |
4448 | vma_page_aligned_bias (bfd_vma vma, ufile_ptr off, bfd_vma maxpagesize) | |
4449 | { | |
dc9155b2 NC |
4450 | /* PR binutils/16199: Handle an alignment of zero. */ |
4451 | if (maxpagesize == 0) | |
4452 | maxpagesize = 1; | |
340b6d91 AC |
4453 | return ((vma - off) % maxpagesize); |
4454 | } | |
4455 | ||
6d33f217 L |
4456 | static void |
4457 | print_segment_map (const struct elf_segment_map *m) | |
4458 | { | |
4459 | unsigned int j; | |
4460 | const char *pt = get_segment_type (m->p_type); | |
4461 | char buf[32]; | |
4462 | ||
4463 | if (pt == NULL) | |
4464 | { | |
4465 | if (m->p_type >= PT_LOPROC && m->p_type <= PT_HIPROC) | |
4466 | sprintf (buf, "LOPROC+%7.7x", | |
4467 | (unsigned int) (m->p_type - PT_LOPROC)); | |
4468 | else if (m->p_type >= PT_LOOS && m->p_type <= PT_HIOS) | |
4469 | sprintf (buf, "LOOS+%7.7x", | |
4470 | (unsigned int) (m->p_type - PT_LOOS)); | |
4471 | else | |
4472 | snprintf (buf, sizeof (buf), "%8.8x", | |
4473 | (unsigned int) m->p_type); | |
4474 | pt = buf; | |
4475 | } | |
4a97a0e5 | 4476 | fflush (stdout); |
6d33f217 L |
4477 | fprintf (stderr, "%s:", pt); |
4478 | for (j = 0; j < m->count; j++) | |
4479 | fprintf (stderr, " %s", m->sections [j]->name); | |
4480 | putc ('\n',stderr); | |
4a97a0e5 | 4481 | fflush (stderr); |
6d33f217 L |
4482 | } |
4483 | ||
32812159 AM |
4484 | static bfd_boolean |
4485 | write_zeros (bfd *abfd, file_ptr pos, bfd_size_type len) | |
4486 | { | |
4487 | void *buf; | |
4488 | bfd_boolean ret; | |
4489 | ||
4490 | if (bfd_seek (abfd, pos, SEEK_SET) != 0) | |
4491 | return FALSE; | |
4492 | buf = bfd_zmalloc (len); | |
4493 | if (buf == NULL) | |
4494 | return FALSE; | |
4495 | ret = bfd_bwrite (buf, len, abfd) == len; | |
4496 | free (buf); | |
4497 | return ret; | |
4498 | } | |
4499 | ||
252b5132 RH |
4500 | /* Assign file positions to the sections based on the mapping from |
4501 | sections to segments. This function also sets up some fields in | |
f3520d2f | 4502 | the file header. */ |
252b5132 | 4503 | |
b34976b6 | 4504 | static bfd_boolean |
f3520d2f AM |
4505 | assign_file_positions_for_load_sections (bfd *abfd, |
4506 | struct bfd_link_info *link_info) | |
252b5132 RH |
4507 | { |
4508 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
252b5132 | 4509 | struct elf_segment_map *m; |
252b5132 | 4510 | Elf_Internal_Phdr *phdrs; |
252b5132 | 4511 | Elf_Internal_Phdr *p; |
02bf8d82 | 4512 | file_ptr off; |
3f570048 | 4513 | bfd_size_type maxpagesize; |
f3520d2f | 4514 | unsigned int alloc; |
0920dee7 | 4515 | unsigned int i, j; |
2b0bc088 | 4516 | bfd_vma header_pad = 0; |
252b5132 | 4517 | |
e36284ab | 4518 | if (link_info == NULL |
ceae84aa | 4519 | && !_bfd_elf_map_sections_to_segments (abfd, link_info)) |
8ded5a0f | 4520 | return FALSE; |
252b5132 | 4521 | |
8ded5a0f | 4522 | alloc = 0; |
12bd6957 | 4523 | for (m = elf_seg_map (abfd); m != NULL; m = m->next) |
2b0bc088 NC |
4524 | { |
4525 | ++alloc; | |
4526 | if (m->header_size) | |
4527 | header_pad = m->header_size; | |
4528 | } | |
252b5132 | 4529 | |
82f2dbf7 NC |
4530 | if (alloc) |
4531 | { | |
4532 | elf_elfheader (abfd)->e_phoff = bed->s->sizeof_ehdr; | |
4533 | elf_elfheader (abfd)->e_phentsize = bed->s->sizeof_phdr; | |
4534 | } | |
4535 | else | |
4536 | { | |
4537 | /* PR binutils/12467. */ | |
4538 | elf_elfheader (abfd)->e_phoff = 0; | |
4539 | elf_elfheader (abfd)->e_phentsize = 0; | |
4540 | } | |
d324f6d6 | 4541 | |
8ded5a0f | 4542 | elf_elfheader (abfd)->e_phnum = alloc; |
252b5132 | 4543 | |
12bd6957 AM |
4544 | if (elf_program_header_size (abfd) == (bfd_size_type) -1) |
4545 | elf_program_header_size (abfd) = alloc * bed->s->sizeof_phdr; | |
8ded5a0f | 4546 | else |
12bd6957 | 4547 | BFD_ASSERT (elf_program_header_size (abfd) |
59e0647f | 4548 | >= alloc * bed->s->sizeof_phdr); |
252b5132 RH |
4549 | |
4550 | if (alloc == 0) | |
f3520d2f | 4551 | { |
12bd6957 | 4552 | elf_next_file_pos (abfd) = bed->s->sizeof_ehdr; |
8ded5a0f | 4553 | return TRUE; |
f3520d2f | 4554 | } |
252b5132 | 4555 | |
12bd6957 | 4556 | /* We're writing the size in elf_program_header_size (abfd), |
57268894 HPN |
4557 | see assign_file_positions_except_relocs, so make sure we have |
4558 | that amount allocated, with trailing space cleared. | |
12bd6957 AM |
4559 | The variable alloc contains the computed need, while |
4560 | elf_program_header_size (abfd) contains the size used for the | |
57268894 HPN |
4561 | layout. |
4562 | See ld/emultempl/elf-generic.em:gld${EMULATION_NAME}_map_segments | |
4563 | where the layout is forced to according to a larger size in the | |
4564 | last iterations for the testcase ld-elf/header. */ | |
12bd6957 | 4565 | BFD_ASSERT (elf_program_header_size (abfd) % bed->s->sizeof_phdr |
57268894 | 4566 | == 0); |
a50b1753 NC |
4567 | phdrs = (Elf_Internal_Phdr *) |
4568 | bfd_zalloc2 (abfd, | |
12bd6957 | 4569 | (elf_program_header_size (abfd) / bed->s->sizeof_phdr), |
a50b1753 | 4570 | sizeof (Elf_Internal_Phdr)); |
f3520d2f | 4571 | elf_tdata (abfd)->phdr = phdrs; |
252b5132 | 4572 | if (phdrs == NULL) |
b34976b6 | 4573 | return FALSE; |
252b5132 | 4574 | |
3f570048 AM |
4575 | maxpagesize = 1; |
4576 | if ((abfd->flags & D_PAGED) != 0) | |
4577 | maxpagesize = bed->maxpagesize; | |
4578 | ||
252b5132 RH |
4579 | off = bed->s->sizeof_ehdr; |
4580 | off += alloc * bed->s->sizeof_phdr; | |
2b0bc088 NC |
4581 | if (header_pad < (bfd_vma) off) |
4582 | header_pad = 0; | |
4583 | else | |
4584 | header_pad -= off; | |
4585 | off += header_pad; | |
252b5132 | 4586 | |
12bd6957 | 4587 | for (m = elf_seg_map (abfd), p = phdrs, j = 0; |
252b5132 | 4588 | m != NULL; |
0920dee7 | 4589 | m = m->next, p++, j++) |
252b5132 | 4590 | { |
252b5132 | 4591 | asection **secpp; |
bf988460 AM |
4592 | bfd_vma off_adjust; |
4593 | bfd_boolean no_contents; | |
252b5132 RH |
4594 | |
4595 | /* If elf_segment_map is not from map_sections_to_segments, the | |
08a40648 | 4596 | sections may not be correctly ordered. NOTE: sorting should |
52e9b619 MS |
4597 | not be done to the PT_NOTE section of a corefile, which may |
4598 | contain several pseudo-sections artificially created by bfd. | |
4599 | Sorting these pseudo-sections breaks things badly. */ | |
47d9a591 AM |
4600 | if (m->count > 1 |
4601 | && !(elf_elfheader (abfd)->e_type == ET_CORE | |
52e9b619 | 4602 | && m->p_type == PT_NOTE)) |
252b5132 RH |
4603 | qsort (m->sections, (size_t) m->count, sizeof (asection *), |
4604 | elf_sort_sections); | |
4605 | ||
b301b248 AM |
4606 | /* An ELF segment (described by Elf_Internal_Phdr) may contain a |
4607 | number of sections with contents contributing to both p_filesz | |
4608 | and p_memsz, followed by a number of sections with no contents | |
4609 | that just contribute to p_memsz. In this loop, OFF tracks next | |
02bf8d82 | 4610 | available file offset for PT_LOAD and PT_NOTE segments. */ |
252b5132 | 4611 | p->p_type = m->p_type; |
28a7f3e7 | 4612 | p->p_flags = m->p_flags; |
252b5132 | 4613 | |
3f570048 AM |
4614 | if (m->count == 0) |
4615 | p->p_vaddr = 0; | |
4616 | else | |
3271a814 | 4617 | p->p_vaddr = m->sections[0]->vma - m->p_vaddr_offset; |
3f570048 AM |
4618 | |
4619 | if (m->p_paddr_valid) | |
4620 | p->p_paddr = m->p_paddr; | |
4621 | else if (m->count == 0) | |
4622 | p->p_paddr = 0; | |
4623 | else | |
08a40648 | 4624 | p->p_paddr = m->sections[0]->lma - m->p_vaddr_offset; |
3f570048 AM |
4625 | |
4626 | if (p->p_type == PT_LOAD | |
4627 | && (abfd->flags & D_PAGED) != 0) | |
4628 | { | |
4629 | /* p_align in demand paged PT_LOAD segments effectively stores | |
4630 | the maximum page size. When copying an executable with | |
4631 | objcopy, we set m->p_align from the input file. Use this | |
4632 | value for maxpagesize rather than bed->maxpagesize, which | |
4633 | may be different. Note that we use maxpagesize for PT_TLS | |
4634 | segment alignment later in this function, so we are relying | |
4635 | on at least one PT_LOAD segment appearing before a PT_TLS | |
4636 | segment. */ | |
4637 | if (m->p_align_valid) | |
4638 | maxpagesize = m->p_align; | |
4639 | ||
4640 | p->p_align = maxpagesize; | |
4641 | } | |
3271a814 NS |
4642 | else if (m->p_align_valid) |
4643 | p->p_align = m->p_align; | |
e970b90a DJ |
4644 | else if (m->count == 0) |
4645 | p->p_align = 1 << bed->s->log_file_align; | |
3f570048 AM |
4646 | else |
4647 | p->p_align = 0; | |
4648 | ||
bf988460 AM |
4649 | no_contents = FALSE; |
4650 | off_adjust = 0; | |
252b5132 | 4651 | if (p->p_type == PT_LOAD |
b301b248 | 4652 | && m->count > 0) |
252b5132 | 4653 | { |
b301b248 | 4654 | bfd_size_type align; |
a49e53ed | 4655 | unsigned int align_power = 0; |
b301b248 | 4656 | |
3271a814 NS |
4657 | if (m->p_align_valid) |
4658 | align = p->p_align; | |
4659 | else | |
252b5132 | 4660 | { |
3271a814 NS |
4661 | for (i = 0, secpp = m->sections; i < m->count; i++, secpp++) |
4662 | { | |
4663 | unsigned int secalign; | |
08a40648 | 4664 | |
3271a814 NS |
4665 | secalign = bfd_get_section_alignment (abfd, *secpp); |
4666 | if (secalign > align_power) | |
4667 | align_power = secalign; | |
4668 | } | |
4669 | align = (bfd_size_type) 1 << align_power; | |
4670 | if (align < maxpagesize) | |
4671 | align = maxpagesize; | |
b301b248 | 4672 | } |
252b5132 | 4673 | |
02bf8d82 AM |
4674 | for (i = 0; i < m->count; i++) |
4675 | if ((m->sections[i]->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0) | |
4676 | /* If we aren't making room for this section, then | |
4677 | it must be SHT_NOBITS regardless of what we've | |
4678 | set via struct bfd_elf_special_section. */ | |
4679 | elf_section_type (m->sections[i]) = SHT_NOBITS; | |
4680 | ||
bf988460 | 4681 | /* Find out whether this segment contains any loadable |
aea274d3 AM |
4682 | sections. */ |
4683 | no_contents = TRUE; | |
4684 | for (i = 0; i < m->count; i++) | |
4685 | if (elf_section_type (m->sections[i]) != SHT_NOBITS) | |
4686 | { | |
4687 | no_contents = FALSE; | |
4688 | break; | |
4689 | } | |
bf988460 | 4690 | |
85cfcbfb | 4691 | off_adjust = vma_page_aligned_bias (p->p_vaddr, off, align); |
bf988460 AM |
4692 | off += off_adjust; |
4693 | if (no_contents) | |
4694 | { | |
4695 | /* We shouldn't need to align the segment on disk since | |
4696 | the segment doesn't need file space, but the gABI | |
4697 | arguably requires the alignment and glibc ld.so | |
4698 | checks it. So to comply with the alignment | |
4699 | requirement but not waste file space, we adjust | |
4700 | p_offset for just this segment. (OFF_ADJUST is | |
4701 | subtracted from OFF later.) This may put p_offset | |
4702 | past the end of file, but that shouldn't matter. */ | |
4703 | } | |
4704 | else | |
4705 | off_adjust = 0; | |
252b5132 | 4706 | } |
b1a6d0b1 NC |
4707 | /* Make sure the .dynamic section is the first section in the |
4708 | PT_DYNAMIC segment. */ | |
4709 | else if (p->p_type == PT_DYNAMIC | |
4710 | && m->count > 1 | |
4711 | && strcmp (m->sections[0]->name, ".dynamic") != 0) | |
4712 | { | |
4713 | _bfd_error_handler | |
b301b248 AM |
4714 | (_("%B: The first section in the PT_DYNAMIC segment is not the .dynamic section"), |
4715 | abfd); | |
b1a6d0b1 NC |
4716 | bfd_set_error (bfd_error_bad_value); |
4717 | return FALSE; | |
4718 | } | |
3f001e84 JK |
4719 | /* Set the note section type to SHT_NOTE. */ |
4720 | else if (p->p_type == PT_NOTE) | |
4721 | for (i = 0; i < m->count; i++) | |
4722 | elf_section_type (m->sections[i]) = SHT_NOTE; | |
252b5132 | 4723 | |
252b5132 RH |
4724 | p->p_offset = 0; |
4725 | p->p_filesz = 0; | |
4726 | p->p_memsz = 0; | |
4727 | ||
4728 | if (m->includes_filehdr) | |
4729 | { | |
bf988460 | 4730 | if (!m->p_flags_valid) |
252b5132 | 4731 | p->p_flags |= PF_R; |
252b5132 RH |
4732 | p->p_filesz = bed->s->sizeof_ehdr; |
4733 | p->p_memsz = bed->s->sizeof_ehdr; | |
4734 | if (m->count > 0) | |
4735 | { | |
252b5132 RH |
4736 | if (p->p_vaddr < (bfd_vma) off) |
4737 | { | |
caf47ea6 | 4738 | (*_bfd_error_handler) |
b301b248 AM |
4739 | (_("%B: Not enough room for program headers, try linking with -N"), |
4740 | abfd); | |
252b5132 | 4741 | bfd_set_error (bfd_error_bad_value); |
b34976b6 | 4742 | return FALSE; |
252b5132 RH |
4743 | } |
4744 | ||
4745 | p->p_vaddr -= off; | |
bf988460 | 4746 | if (!m->p_paddr_valid) |
252b5132 RH |
4747 | p->p_paddr -= off; |
4748 | } | |
252b5132 RH |
4749 | } |
4750 | ||
4751 | if (m->includes_phdrs) | |
4752 | { | |
bf988460 | 4753 | if (!m->p_flags_valid) |
252b5132 RH |
4754 | p->p_flags |= PF_R; |
4755 | ||
f3520d2f | 4756 | if (!m->includes_filehdr) |
252b5132 RH |
4757 | { |
4758 | p->p_offset = bed->s->sizeof_ehdr; | |
4759 | ||
4760 | if (m->count > 0) | |
4761 | { | |
252b5132 | 4762 | p->p_vaddr -= off - p->p_offset; |
bf988460 | 4763 | if (!m->p_paddr_valid) |
252b5132 RH |
4764 | p->p_paddr -= off - p->p_offset; |
4765 | } | |
252b5132 RH |
4766 | } |
4767 | ||
4768 | p->p_filesz += alloc * bed->s->sizeof_phdr; | |
4769 | p->p_memsz += alloc * bed->s->sizeof_phdr; | |
2b0bc088 NC |
4770 | if (m->count) |
4771 | { | |
4772 | p->p_filesz += header_pad; | |
4773 | p->p_memsz += header_pad; | |
4774 | } | |
252b5132 RH |
4775 | } |
4776 | ||
4777 | if (p->p_type == PT_LOAD | |
4778 | || (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core)) | |
4779 | { | |
bf988460 | 4780 | if (!m->includes_filehdr && !m->includes_phdrs) |
02bf8d82 | 4781 | p->p_offset = off; |
252b5132 RH |
4782 | else |
4783 | { | |
4784 | file_ptr adjust; | |
4785 | ||
4786 | adjust = off - (p->p_offset + p->p_filesz); | |
bf988460 AM |
4787 | if (!no_contents) |
4788 | p->p_filesz += adjust; | |
252b5132 RH |
4789 | p->p_memsz += adjust; |
4790 | } | |
4791 | } | |
4792 | ||
1ea63fd2 AM |
4793 | /* Set up p_filesz, p_memsz, p_align and p_flags from the section |
4794 | maps. Set filepos for sections in PT_LOAD segments, and in | |
4795 | core files, for sections in PT_NOTE segments. | |
4796 | assign_file_positions_for_non_load_sections will set filepos | |
4797 | for other sections and update p_filesz for other segments. */ | |
252b5132 RH |
4798 | for (i = 0, secpp = m->sections; i < m->count; i++, secpp++) |
4799 | { | |
4800 | asection *sec; | |
252b5132 | 4801 | bfd_size_type align; |
627b32bc | 4802 | Elf_Internal_Shdr *this_hdr; |
252b5132 RH |
4803 | |
4804 | sec = *secpp; | |
02bf8d82 | 4805 | this_hdr = &elf_section_data (sec)->this_hdr; |
3f570048 | 4806 | align = (bfd_size_type) 1 << bfd_get_section_alignment (abfd, sec); |
252b5132 | 4807 | |
88967714 AM |
4808 | if ((p->p_type == PT_LOAD |
4809 | || p->p_type == PT_TLS) | |
4810 | && (this_hdr->sh_type != SHT_NOBITS | |
4811 | || ((this_hdr->sh_flags & SHF_ALLOC) != 0 | |
4812 | && ((this_hdr->sh_flags & SHF_TLS) == 0 | |
4813 | || p->p_type == PT_TLS)))) | |
252b5132 | 4814 | { |
b5599592 AM |
4815 | bfd_vma p_start = p->p_paddr; |
4816 | bfd_vma p_end = p_start + p->p_memsz; | |
4817 | bfd_vma s_start = sec->lma; | |
4818 | bfd_vma adjust = s_start - p_end; | |
252b5132 | 4819 | |
a2d1e028 L |
4820 | if (adjust != 0 |
4821 | && (s_start < p_end | |
4822 | || p_end < p_start)) | |
252b5132 | 4823 | { |
88967714 | 4824 | (*_bfd_error_handler) |
b5599592 AM |
4825 | (_("%B: section %A lma %#lx adjusted to %#lx"), abfd, sec, |
4826 | (unsigned long) s_start, (unsigned long) p_end); | |
88967714 | 4827 | adjust = 0; |
b5599592 | 4828 | sec->lma = p_end; |
1cfb7d1e | 4829 | } |
3ac9b6c9 | 4830 | p->p_memsz += adjust; |
1cfb7d1e | 4831 | |
88967714 AM |
4832 | if (this_hdr->sh_type != SHT_NOBITS) |
4833 | { | |
32812159 AM |
4834 | if (p->p_filesz + adjust < p->p_memsz) |
4835 | { | |
4836 | /* We have a PROGBITS section following NOBITS ones. | |
4837 | Allocate file space for the NOBITS section(s) and | |
4838 | zero it. */ | |
4839 | adjust = p->p_memsz - p->p_filesz; | |
4840 | if (!write_zeros (abfd, off, adjust)) | |
4841 | return FALSE; | |
4842 | } | |
88967714 AM |
4843 | off += adjust; |
4844 | p->p_filesz += adjust; | |
252b5132 | 4845 | } |
252b5132 RH |
4846 | } |
4847 | ||
4848 | if (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core) | |
4849 | { | |
b301b248 AM |
4850 | /* The section at i == 0 is the one that actually contains |
4851 | everything. */ | |
4a938328 MS |
4852 | if (i == 0) |
4853 | { | |
627b32bc | 4854 | this_hdr->sh_offset = sec->filepos = off; |
6a3cd2b4 AM |
4855 | off += this_hdr->sh_size; |
4856 | p->p_filesz = this_hdr->sh_size; | |
b301b248 AM |
4857 | p->p_memsz = 0; |
4858 | p->p_align = 1; | |
252b5132 | 4859 | } |
4a938328 | 4860 | else |
252b5132 | 4861 | { |
b301b248 | 4862 | /* The rest are fake sections that shouldn't be written. */ |
252b5132 | 4863 | sec->filepos = 0; |
eea6121a | 4864 | sec->size = 0; |
b301b248 AM |
4865 | sec->flags = 0; |
4866 | continue; | |
252b5132 | 4867 | } |
252b5132 RH |
4868 | } |
4869 | else | |
4870 | { | |
1e951488 | 4871 | if (p->p_type == PT_LOAD) |
b301b248 | 4872 | { |
1e951488 AM |
4873 | this_hdr->sh_offset = sec->filepos = off; |
4874 | if (this_hdr->sh_type != SHT_NOBITS) | |
4875 | off += this_hdr->sh_size; | |
4876 | } | |
4877 | else if (this_hdr->sh_type == SHT_NOBITS | |
4878 | && (this_hdr->sh_flags & SHF_TLS) != 0 | |
4879 | && this_hdr->sh_offset == 0) | |
4880 | { | |
4881 | /* This is a .tbss section that didn't get a PT_LOAD. | |
4882 | (See _bfd_elf_map_sections_to_segments "Create a | |
4883 | final PT_LOAD".) Set sh_offset to the value it | |
4884 | would have if we had created a zero p_filesz and | |
4885 | p_memsz PT_LOAD header for the section. This | |
4886 | also makes the PT_TLS header have the same | |
4887 | p_offset value. */ | |
4888 | bfd_vma adjust = vma_page_aligned_bias (this_hdr->sh_addr, | |
4889 | off, align); | |
4890 | this_hdr->sh_offset = sec->filepos = off + adjust; | |
b301b248 | 4891 | } |
252b5132 | 4892 | |
02bf8d82 | 4893 | if (this_hdr->sh_type != SHT_NOBITS) |
b301b248 | 4894 | { |
6a3cd2b4 | 4895 | p->p_filesz += this_hdr->sh_size; |
02bf8d82 AM |
4896 | /* A load section without SHF_ALLOC is something like |
4897 | a note section in a PT_NOTE segment. These take | |
4898 | file space but are not loaded into memory. */ | |
4899 | if ((this_hdr->sh_flags & SHF_ALLOC) != 0) | |
6a3cd2b4 | 4900 | p->p_memsz += this_hdr->sh_size; |
b301b248 | 4901 | } |
6a3cd2b4 | 4902 | else if ((this_hdr->sh_flags & SHF_ALLOC) != 0) |
13ae64f3 | 4903 | { |
6a3cd2b4 AM |
4904 | if (p->p_type == PT_TLS) |
4905 | p->p_memsz += this_hdr->sh_size; | |
4906 | ||
4907 | /* .tbss is special. It doesn't contribute to p_memsz of | |
4908 | normal segments. */ | |
4909 | else if ((this_hdr->sh_flags & SHF_TLS) == 0) | |
4910 | p->p_memsz += this_hdr->sh_size; | |
13ae64f3 JJ |
4911 | } |
4912 | ||
b10a8ae0 L |
4913 | if (align > p->p_align |
4914 | && !m->p_align_valid | |
4915 | && (p->p_type != PT_LOAD | |
4916 | || (abfd->flags & D_PAGED) == 0)) | |
252b5132 RH |
4917 | p->p_align = align; |
4918 | } | |
4919 | ||
bf988460 | 4920 | if (!m->p_flags_valid) |
252b5132 RH |
4921 | { |
4922 | p->p_flags |= PF_R; | |
02bf8d82 | 4923 | if ((this_hdr->sh_flags & SHF_EXECINSTR) != 0) |
252b5132 | 4924 | p->p_flags |= PF_X; |
02bf8d82 | 4925 | if ((this_hdr->sh_flags & SHF_WRITE) != 0) |
252b5132 RH |
4926 | p->p_flags |= PF_W; |
4927 | } | |
4928 | } | |
43a8475c | 4929 | |
bf988460 | 4930 | off -= off_adjust; |
0920dee7 | 4931 | |
7c928300 AM |
4932 | /* Check that all sections are in a PT_LOAD segment. |
4933 | Don't check funky gdb generated core files. */ | |
4934 | if (p->p_type == PT_LOAD && bfd_get_format (abfd) != bfd_core) | |
9a83a553 AM |
4935 | { |
4936 | bfd_boolean check_vma = TRUE; | |
4937 | ||
4938 | for (i = 1; i < m->count; i++) | |
4939 | if (m->sections[i]->vma == m->sections[i - 1]->vma | |
4940 | && ELF_SECTION_SIZE (&(elf_section_data (m->sections[i]) | |
4941 | ->this_hdr), p) != 0 | |
4942 | && ELF_SECTION_SIZE (&(elf_section_data (m->sections[i - 1]) | |
4943 | ->this_hdr), p) != 0) | |
0920dee7 | 4944 | { |
9a83a553 AM |
4945 | /* Looks like we have overlays packed into the segment. */ |
4946 | check_vma = FALSE; | |
4947 | break; | |
0920dee7 | 4948 | } |
9a83a553 AM |
4949 | |
4950 | for (i = 0; i < m->count; i++) | |
4951 | { | |
4952 | Elf_Internal_Shdr *this_hdr; | |
4953 | asection *sec; | |
4954 | ||
4955 | sec = m->sections[i]; | |
4956 | this_hdr = &(elf_section_data(sec)->this_hdr); | |
86b2281f AM |
4957 | if (!ELF_SECTION_IN_SEGMENT_1 (this_hdr, p, check_vma, 0) |
4958 | && !ELF_TBSS_SPECIAL (this_hdr, p)) | |
9a83a553 AM |
4959 | { |
4960 | (*_bfd_error_handler) | |
4961 | (_("%B: section `%A' can't be allocated in segment %d"), | |
4962 | abfd, sec, j); | |
4963 | print_segment_map (m); | |
4964 | } | |
4965 | } | |
4966 | } | |
252b5132 RH |
4967 | } |
4968 | ||
12bd6957 | 4969 | elf_next_file_pos (abfd) = off; |
f3520d2f AM |
4970 | return TRUE; |
4971 | } | |
4972 | ||
4973 | /* Assign file positions for the other sections. */ | |
4974 | ||
4975 | static bfd_boolean | |
4976 | assign_file_positions_for_non_load_sections (bfd *abfd, | |
4977 | struct bfd_link_info *link_info) | |
4978 | { | |
4979 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
4980 | Elf_Internal_Shdr **i_shdrpp; | |
4981 | Elf_Internal_Shdr **hdrpp; | |
4982 | Elf_Internal_Phdr *phdrs; | |
4983 | Elf_Internal_Phdr *p; | |
4984 | struct elf_segment_map *m; | |
62655c7b | 4985 | struct elf_segment_map *hdrs_segment; |
f3520d2f AM |
4986 | bfd_vma filehdr_vaddr, filehdr_paddr; |
4987 | bfd_vma phdrs_vaddr, phdrs_paddr; | |
4988 | file_ptr off; | |
4989 | unsigned int num_sec; | |
4990 | unsigned int i; | |
4991 | unsigned int count; | |
4992 | ||
5c182d5f AM |
4993 | i_shdrpp = elf_elfsections (abfd); |
4994 | num_sec = elf_numsections (abfd); | |
12bd6957 | 4995 | off = elf_next_file_pos (abfd); |
5c182d5f AM |
4996 | for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++) |
4997 | { | |
5c182d5f AM |
4998 | Elf_Internal_Shdr *hdr; |
4999 | ||
5000 | hdr = *hdrpp; | |
5001 | if (hdr->bfd_section != NULL | |
252e386e AM |
5002 | && (hdr->bfd_section->filepos != 0 |
5003 | || (hdr->sh_type == SHT_NOBITS | |
5004 | && hdr->contents == NULL))) | |
627b32bc | 5005 | BFD_ASSERT (hdr->sh_offset == hdr->bfd_section->filepos); |
5c182d5f AM |
5006 | else if ((hdr->sh_flags & SHF_ALLOC) != 0) |
5007 | { | |
e8d2ba53 AM |
5008 | if (hdr->sh_size != 0) |
5009 | (*_bfd_error_handler) | |
5010 | (_("%B: warning: allocated section `%s' not in segment"), | |
5011 | abfd, | |
5012 | (hdr->bfd_section == NULL | |
5013 | ? "*unknown*" | |
5014 | : hdr->bfd_section->name)); | |
3ba71138 L |
5015 | /* We don't need to page align empty sections. */ |
5016 | if ((abfd->flags & D_PAGED) != 0 && hdr->sh_size != 0) | |
5c182d5f AM |
5017 | off += vma_page_aligned_bias (hdr->sh_addr, off, |
5018 | bed->maxpagesize); | |
5019 | else | |
5020 | off += vma_page_aligned_bias (hdr->sh_addr, off, | |
5021 | hdr->sh_addralign); | |
5022 | off = _bfd_elf_assign_file_position_for_section (hdr, off, | |
5023 | FALSE); | |
5024 | } | |
5025 | else if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA) | |
5026 | && hdr->bfd_section == NULL) | |
12bd6957 AM |
5027 | || hdr == i_shdrpp[elf_onesymtab (abfd)] |
5028 | || hdr == i_shdrpp[elf_symtab_shndx (abfd)] | |
5029 | || hdr == i_shdrpp[elf_strtab_sec (abfd)]) | |
5c182d5f AM |
5030 | hdr->sh_offset = -1; |
5031 | else | |
5032 | off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE); | |
5c182d5f AM |
5033 | } |
5034 | ||
252b5132 RH |
5035 | /* Now that we have set the section file positions, we can set up |
5036 | the file positions for the non PT_LOAD segments. */ | |
f3520d2f AM |
5037 | count = 0; |
5038 | filehdr_vaddr = 0; | |
5039 | filehdr_paddr = 0; | |
5040 | phdrs_vaddr = bed->maxpagesize + bed->s->sizeof_ehdr; | |
5041 | phdrs_paddr = 0; | |
62655c7b | 5042 | hdrs_segment = NULL; |
f3520d2f | 5043 | phdrs = elf_tdata (abfd)->phdr; |
12bd6957 | 5044 | for (m = elf_seg_map (abfd), p = phdrs; m != NULL; m = m->next, p++) |
f3520d2f AM |
5045 | { |
5046 | ++count; | |
5047 | if (p->p_type != PT_LOAD) | |
5048 | continue; | |
5049 | ||
5050 | if (m->includes_filehdr) | |
5051 | { | |
5052 | filehdr_vaddr = p->p_vaddr; | |
5053 | filehdr_paddr = p->p_paddr; | |
5054 | } | |
5055 | if (m->includes_phdrs) | |
5056 | { | |
5057 | phdrs_vaddr = p->p_vaddr; | |
5058 | phdrs_paddr = p->p_paddr; | |
5059 | if (m->includes_filehdr) | |
5060 | { | |
62655c7b | 5061 | hdrs_segment = m; |
f3520d2f AM |
5062 | phdrs_vaddr += bed->s->sizeof_ehdr; |
5063 | phdrs_paddr += bed->s->sizeof_ehdr; | |
5064 | } | |
5065 | } | |
5066 | } | |
5067 | ||
62655c7b RM |
5068 | if (hdrs_segment != NULL && link_info != NULL) |
5069 | { | |
5070 | /* There is a segment that contains both the file headers and the | |
5071 | program headers, so provide a symbol __ehdr_start pointing there. | |
5072 | A program can use this to examine itself robustly. */ | |
5073 | ||
5074 | struct elf_link_hash_entry *hash | |
5075 | = elf_link_hash_lookup (elf_hash_table (link_info), "__ehdr_start", | |
5076 | FALSE, FALSE, TRUE); | |
5077 | /* If the symbol was referenced and not defined, define it. */ | |
5078 | if (hash != NULL | |
5079 | && (hash->root.type == bfd_link_hash_new | |
5080 | || hash->root.type == bfd_link_hash_undefined | |
5081 | || hash->root.type == bfd_link_hash_undefweak | |
5082 | || hash->root.type == bfd_link_hash_common)) | |
5083 | { | |
5084 | asection *s = NULL; | |
5085 | if (hdrs_segment->count != 0) | |
5086 | /* The segment contains sections, so use the first one. */ | |
5087 | s = hdrs_segment->sections[0]; | |
5088 | else | |
5089 | /* Use the first (i.e. lowest-addressed) section in any segment. */ | |
12bd6957 | 5090 | for (m = elf_seg_map (abfd); m != NULL; m = m->next) |
62655c7b RM |
5091 | if (m->count != 0) |
5092 | { | |
5093 | s = m->sections[0]; | |
5094 | break; | |
5095 | } | |
5096 | ||
5097 | if (s != NULL) | |
5098 | { | |
5099 | hash->root.u.def.value = filehdr_vaddr - s->vma; | |
5100 | hash->root.u.def.section = s; | |
5101 | } | |
5102 | else | |
5103 | { | |
5104 | hash->root.u.def.value = filehdr_vaddr; | |
5105 | hash->root.u.def.section = bfd_abs_section_ptr; | |
5106 | } | |
5107 | ||
5108 | hash->root.type = bfd_link_hash_defined; | |
5109 | hash->def_regular = 1; | |
5110 | hash->non_elf = 0; | |
5111 | } | |
5112 | } | |
5113 | ||
12bd6957 | 5114 | for (m = elf_seg_map (abfd), p = phdrs; m != NULL; m = m->next, p++) |
252b5132 | 5115 | { |
129af99f | 5116 | if (p->p_type == PT_GNU_RELRO) |
252b5132 | 5117 | { |
b84a33b5 | 5118 | const Elf_Internal_Phdr *lp; |
3146fac4 | 5119 | struct elf_segment_map *lm; |
1ea63fd2 | 5120 | |
129af99f | 5121 | if (link_info != NULL) |
8c37241b | 5122 | { |
129af99f AS |
5123 | /* During linking the range of the RELRO segment is passed |
5124 | in link_info. */ | |
12bd6957 | 5125 | for (lm = elf_seg_map (abfd), lp = phdrs; |
3146fac4 AM |
5126 | lm != NULL; |
5127 | lm = lm->next, lp++) | |
8c37241b JJ |
5128 | { |
5129 | if (lp->p_type == PT_LOAD | |
b84a33b5 | 5130 | && lp->p_vaddr < link_info->relro_end |
3146fac4 AM |
5131 | && lm->count != 0 |
5132 | && lm->sections[0]->vma >= link_info->relro_start) | |
8c37241b JJ |
5133 | break; |
5134 | } | |
8981c88a | 5135 | |
3146fac4 | 5136 | BFD_ASSERT (lm != NULL); |
8c37241b | 5137 | } |
129af99f AS |
5138 | else |
5139 | { | |
5140 | /* Otherwise we are copying an executable or shared | |
b84a33b5 | 5141 | library, but we need to use the same linker logic. */ |
129af99f AS |
5142 | for (lp = phdrs; lp < phdrs + count; ++lp) |
5143 | { | |
5144 | if (lp->p_type == PT_LOAD | |
5145 | && lp->p_paddr == p->p_paddr) | |
5146 | break; | |
5147 | } | |
b84a33b5 AM |
5148 | } |
5149 | ||
5150 | if (lp < phdrs + count) | |
5151 | { | |
5152 | p->p_vaddr = lp->p_vaddr; | |
5153 | p->p_paddr = lp->p_paddr; | |
5154 | p->p_offset = lp->p_offset; | |
5155 | if (link_info != NULL) | |
5156 | p->p_filesz = link_info->relro_end - lp->p_vaddr; | |
5157 | else if (m->p_size_valid) | |
5158 | p->p_filesz = m->p_size; | |
129af99f AS |
5159 | else |
5160 | abort (); | |
b84a33b5 | 5161 | p->p_memsz = p->p_filesz; |
f3944f72 L |
5162 | /* Preserve the alignment and flags if they are valid. The |
5163 | gold linker generates RW/4 for the PT_GNU_RELRO section. | |
5164 | It is better for objcopy/strip to honor these attributes | |
5165 | otherwise gdb will choke when using separate debug files. | |
5166 | */ | |
5167 | if (!m->p_align_valid) | |
5168 | p->p_align = 1; | |
5169 | if (!m->p_flags_valid) | |
5170 | p->p_flags = (lp->p_flags & ~PF_W); | |
129af99f | 5171 | } |
9433b9b1 | 5172 | else |
b84a33b5 AM |
5173 | { |
5174 | memset (p, 0, sizeof *p); | |
5175 | p->p_type = PT_NULL; | |
5176 | } | |
129af99f | 5177 | } |
04c3a755 NS |
5178 | else if (p->p_type == PT_GNU_STACK) |
5179 | { | |
5180 | if (m->p_size_valid) | |
5181 | p->p_memsz = m->p_size; | |
5182 | } | |
129af99f AS |
5183 | else if (m->count != 0) |
5184 | { | |
5185 | if (p->p_type != PT_LOAD | |
5186 | && (p->p_type != PT_NOTE | |
5187 | || bfd_get_format (abfd) != bfd_core)) | |
5188 | { | |
129af99f AS |
5189 | BFD_ASSERT (!m->includes_filehdr && !m->includes_phdrs); |
5190 | ||
86b2281f | 5191 | p->p_filesz = 0; |
129af99f | 5192 | p->p_offset = m->sections[0]->filepos; |
86b2281f AM |
5193 | for (i = m->count; i-- != 0;) |
5194 | { | |
5195 | asection *sect = m->sections[i]; | |
5196 | Elf_Internal_Shdr *hdr = &elf_section_data (sect)->this_hdr; | |
5197 | if (hdr->sh_type != SHT_NOBITS) | |
5198 | { | |
5199 | p->p_filesz = (sect->filepos - m->sections[0]->filepos | |
5200 | + hdr->sh_size); | |
5201 | break; | |
5202 | } | |
5203 | } | |
129af99f AS |
5204 | } |
5205 | } | |
5206 | else if (m->includes_filehdr) | |
5207 | { | |
5208 | p->p_vaddr = filehdr_vaddr; | |
5209 | if (! m->p_paddr_valid) | |
5210 | p->p_paddr = filehdr_paddr; | |
5211 | } | |
5212 | else if (m->includes_phdrs) | |
5213 | { | |
5214 | p->p_vaddr = phdrs_vaddr; | |
5215 | if (! m->p_paddr_valid) | |
5216 | p->p_paddr = phdrs_paddr; | |
252b5132 RH |
5217 | } |
5218 | } | |
5219 | ||
12bd6957 | 5220 | elf_next_file_pos (abfd) = off; |
252b5132 | 5221 | |
b34976b6 | 5222 | return TRUE; |
252b5132 RH |
5223 | } |
5224 | ||
252b5132 RH |
5225 | /* Work out the file positions of all the sections. This is called by |
5226 | _bfd_elf_compute_section_file_positions. All the section sizes and | |
5227 | VMAs must be known before this is called. | |
5228 | ||
e0638f70 AM |
5229 | Reloc sections come in two flavours: Those processed specially as |
5230 | "side-channel" data attached to a section to which they apply, and | |
5231 | those that bfd doesn't process as relocations. The latter sort are | |
5232 | stored in a normal bfd section by bfd_section_from_shdr. We don't | |
5233 | consider the former sort here, unless they form part of the loadable | |
5234 | image. Reloc sections not assigned here will be handled later by | |
5235 | assign_file_positions_for_relocs. | |
252b5132 RH |
5236 | |
5237 | We also don't set the positions of the .symtab and .strtab here. */ | |
5238 | ||
b34976b6 | 5239 | static bfd_boolean |
c84fca4d AO |
5240 | assign_file_positions_except_relocs (bfd *abfd, |
5241 | struct bfd_link_info *link_info) | |
252b5132 | 5242 | { |
5c182d5f AM |
5243 | struct elf_obj_tdata *tdata = elf_tdata (abfd); |
5244 | Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd); | |
9c5bfbb7 | 5245 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
252b5132 RH |
5246 | |
5247 | if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0 | |
5248 | && bfd_get_format (abfd) != bfd_core) | |
5249 | { | |
5c182d5f AM |
5250 | Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd); |
5251 | unsigned int num_sec = elf_numsections (abfd); | |
252b5132 RH |
5252 | Elf_Internal_Shdr **hdrpp; |
5253 | unsigned int i; | |
a485e98e | 5254 | file_ptr off; |
252b5132 RH |
5255 | |
5256 | /* Start after the ELF header. */ | |
5257 | off = i_ehdrp->e_ehsize; | |
5258 | ||
5259 | /* We are not creating an executable, which means that we are | |
5260 | not creating a program header, and that the actual order of | |
5261 | the sections in the file is unimportant. */ | |
9ad5cbcf | 5262 | for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++) |
252b5132 RH |
5263 | { |
5264 | Elf_Internal_Shdr *hdr; | |
5265 | ||
5266 | hdr = *hdrpp; | |
e0638f70 AM |
5267 | if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA) |
5268 | && hdr->bfd_section == NULL) | |
12bd6957 AM |
5269 | || i == elf_onesymtab (abfd) |
5270 | || i == elf_symtab_shndx (abfd) | |
5271 | || i == elf_strtab_sec (abfd)) | |
252b5132 RH |
5272 | { |
5273 | hdr->sh_offset = -1; | |
252b5132 | 5274 | } |
9ad5cbcf | 5275 | else |
b34976b6 | 5276 | off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE); |
252b5132 | 5277 | } |
a485e98e AM |
5278 | |
5279 | elf_next_file_pos (abfd) = off; | |
252b5132 RH |
5280 | } |
5281 | else | |
5282 | { | |
f3520d2f AM |
5283 | unsigned int alloc; |
5284 | ||
252b5132 | 5285 | /* Assign file positions for the loaded sections based on the |
08a40648 | 5286 | assignment of sections to segments. */ |
f3520d2f AM |
5287 | if (!assign_file_positions_for_load_sections (abfd, link_info)) |
5288 | return FALSE; | |
5289 | ||
5290 | /* And for non-load sections. */ | |
5291 | if (!assign_file_positions_for_non_load_sections (abfd, link_info)) | |
5292 | return FALSE; | |
5293 | ||
e36284ab AM |
5294 | if (bed->elf_backend_modify_program_headers != NULL) |
5295 | { | |
5296 | if (!(*bed->elf_backend_modify_program_headers) (abfd, link_info)) | |
5297 | return FALSE; | |
5298 | } | |
5299 | ||
58e7ebac L |
5300 | /* Set e_type in ELF header to ET_EXEC for -pie -Ttext-segment=. */ |
5301 | if (link_info != NULL | |
5302 | && link_info->executable | |
5303 | && link_info->shared) | |
5304 | { | |
5305 | unsigned int num_segments = elf_elfheader (abfd)->e_phnum; | |
5306 | Elf_Internal_Phdr *segment = elf_tdata (abfd)->phdr; | |
5307 | Elf_Internal_Phdr *end_segment = &segment[num_segments]; | |
5308 | ||
5309 | /* Find the lowest p_vaddr in PT_LOAD segments. */ | |
5310 | bfd_vma p_vaddr = (bfd_vma) -1; | |
5311 | for (; segment < end_segment; segment++) | |
5312 | if (segment->p_type == PT_LOAD && p_vaddr > segment->p_vaddr) | |
5313 | p_vaddr = segment->p_vaddr; | |
5314 | ||
5315 | /* Set e_type to ET_EXEC if the lowest p_vaddr in PT_LOAD | |
5316 | segments is non-zero. */ | |
5317 | if (p_vaddr) | |
5318 | i_ehdrp->e_type = ET_EXEC; | |
5319 | } | |
5320 | ||
f3520d2f | 5321 | /* Write out the program headers. */ |
12bd6957 | 5322 | alloc = elf_program_header_size (abfd) / bed->s->sizeof_phdr; |
f3520d2f AM |
5323 | if (bfd_seek (abfd, (bfd_signed_vma) bed->s->sizeof_ehdr, SEEK_SET) != 0 |
5324 | || bed->s->write_out_phdrs (abfd, tdata->phdr, alloc) != 0) | |
b34976b6 | 5325 | return FALSE; |
252b5132 RH |
5326 | } |
5327 | ||
b34976b6 | 5328 | return TRUE; |
252b5132 RH |
5329 | } |
5330 | ||
b34976b6 | 5331 | static bfd_boolean |
217aa764 | 5332 | prep_headers (bfd *abfd) |
252b5132 | 5333 | { |
3d540e93 | 5334 | Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form. */ |
2b0f7ef9 | 5335 | struct elf_strtab_hash *shstrtab; |
9c5bfbb7 | 5336 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
252b5132 RH |
5337 | |
5338 | i_ehdrp = elf_elfheader (abfd); | |
252b5132 | 5339 | |
2b0f7ef9 | 5340 | shstrtab = _bfd_elf_strtab_init (); |
252b5132 | 5341 | if (shstrtab == NULL) |
b34976b6 | 5342 | return FALSE; |
252b5132 RH |
5343 | |
5344 | elf_shstrtab (abfd) = shstrtab; | |
5345 | ||
5346 | i_ehdrp->e_ident[EI_MAG0] = ELFMAG0; | |
5347 | i_ehdrp->e_ident[EI_MAG1] = ELFMAG1; | |
5348 | i_ehdrp->e_ident[EI_MAG2] = ELFMAG2; | |
5349 | i_ehdrp->e_ident[EI_MAG3] = ELFMAG3; | |
5350 | ||
5351 | i_ehdrp->e_ident[EI_CLASS] = bed->s->elfclass; | |
5352 | i_ehdrp->e_ident[EI_DATA] = | |
5353 | bfd_big_endian (abfd) ? ELFDATA2MSB : ELFDATA2LSB; | |
5354 | i_ehdrp->e_ident[EI_VERSION] = bed->s->ev_current; | |
5355 | ||
252b5132 RH |
5356 | if ((abfd->flags & DYNAMIC) != 0) |
5357 | i_ehdrp->e_type = ET_DYN; | |
5358 | else if ((abfd->flags & EXEC_P) != 0) | |
5359 | i_ehdrp->e_type = ET_EXEC; | |
5360 | else if (bfd_get_format (abfd) == bfd_core) | |
5361 | i_ehdrp->e_type = ET_CORE; | |
5362 | else | |
5363 | i_ehdrp->e_type = ET_REL; | |
5364 | ||
5365 | switch (bfd_get_arch (abfd)) | |
5366 | { | |
5367 | case bfd_arch_unknown: | |
5368 | i_ehdrp->e_machine = EM_NONE; | |
5369 | break; | |
aa4f99bb AO |
5370 | |
5371 | /* There used to be a long list of cases here, each one setting | |
5372 | e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE | |
5373 | in the corresponding bfd definition. To avoid duplication, | |
5374 | the switch was removed. Machines that need special handling | |
5375 | can generally do it in elf_backend_final_write_processing(), | |
5376 | unless they need the information earlier than the final write. | |
5377 | Such need can generally be supplied by replacing the tests for | |
5378 | e_machine with the conditions used to determine it. */ | |
252b5132 | 5379 | default: |
9c5bfbb7 AM |
5380 | i_ehdrp->e_machine = bed->elf_machine_code; |
5381 | } | |
aa4f99bb | 5382 | |
252b5132 RH |
5383 | i_ehdrp->e_version = bed->s->ev_current; |
5384 | i_ehdrp->e_ehsize = bed->s->sizeof_ehdr; | |
5385 | ||
c044fabd | 5386 | /* No program header, for now. */ |
252b5132 RH |
5387 | i_ehdrp->e_phoff = 0; |
5388 | i_ehdrp->e_phentsize = 0; | |
5389 | i_ehdrp->e_phnum = 0; | |
5390 | ||
c044fabd | 5391 | /* Each bfd section is section header entry. */ |
252b5132 RH |
5392 | i_ehdrp->e_entry = bfd_get_start_address (abfd); |
5393 | i_ehdrp->e_shentsize = bed->s->sizeof_shdr; | |
5394 | ||
c044fabd | 5395 | /* If we're building an executable, we'll need a program header table. */ |
252b5132 | 5396 | if (abfd->flags & EXEC_P) |
0e71e495 BE |
5397 | /* It all happens later. */ |
5398 | ; | |
252b5132 RH |
5399 | else |
5400 | { | |
5401 | i_ehdrp->e_phentsize = 0; | |
252b5132 RH |
5402 | i_ehdrp->e_phoff = 0; |
5403 | } | |
5404 | ||
5405 | elf_tdata (abfd)->symtab_hdr.sh_name = | |
b34976b6 | 5406 | (unsigned int) _bfd_elf_strtab_add (shstrtab, ".symtab", FALSE); |
252b5132 | 5407 | elf_tdata (abfd)->strtab_hdr.sh_name = |
b34976b6 | 5408 | (unsigned int) _bfd_elf_strtab_add (shstrtab, ".strtab", FALSE); |
252b5132 | 5409 | elf_tdata (abfd)->shstrtab_hdr.sh_name = |
b34976b6 | 5410 | (unsigned int) _bfd_elf_strtab_add (shstrtab, ".shstrtab", FALSE); |
252b5132 | 5411 | if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1 |
17ca87fc | 5412 | || elf_tdata (abfd)->strtab_hdr.sh_name == (unsigned int) -1 |
252b5132 | 5413 | || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1) |
b34976b6 | 5414 | return FALSE; |
252b5132 | 5415 | |
b34976b6 | 5416 | return TRUE; |
252b5132 RH |
5417 | } |
5418 | ||
5419 | /* Assign file positions for all the reloc sections which are not part | |
a485e98e | 5420 | of the loadable file image, and the file position of section headers. */ |
252b5132 | 5421 | |
a485e98e | 5422 | static void |
217aa764 | 5423 | _bfd_elf_assign_file_positions_for_relocs (bfd *abfd) |
252b5132 RH |
5424 | { |
5425 | file_ptr off; | |
9ad5cbcf | 5426 | unsigned int i, num_sec; |
252b5132 | 5427 | Elf_Internal_Shdr **shdrpp; |
a485e98e AM |
5428 | Elf_Internal_Ehdr *i_ehdrp; |
5429 | const struct elf_backend_data *bed; | |
252b5132 | 5430 | |
12bd6957 | 5431 | off = elf_next_file_pos (abfd); |
252b5132 | 5432 | |
9ad5cbcf AM |
5433 | num_sec = elf_numsections (abfd); |
5434 | for (i = 1, shdrpp = elf_elfsections (abfd) + 1; i < num_sec; i++, shdrpp++) | |
252b5132 RH |
5435 | { |
5436 | Elf_Internal_Shdr *shdrp; | |
5437 | ||
5438 | shdrp = *shdrpp; | |
5439 | if ((shdrp->sh_type == SHT_REL || shdrp->sh_type == SHT_RELA) | |
5440 | && shdrp->sh_offset == -1) | |
b34976b6 | 5441 | off = _bfd_elf_assign_file_position_for_section (shdrp, off, TRUE); |
252b5132 RH |
5442 | } |
5443 | ||
a485e98e AM |
5444 | /* Place the section headers. */ |
5445 | i_ehdrp = elf_elfheader (abfd); | |
5446 | bed = get_elf_backend_data (abfd); | |
5447 | off = align_file_position (off, 1 << bed->s->log_file_align); | |
5448 | i_ehdrp->e_shoff = off; | |
5449 | off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize; | |
12bd6957 | 5450 | elf_next_file_pos (abfd) = off; |
252b5132 RH |
5451 | } |
5452 | ||
b34976b6 | 5453 | bfd_boolean |
217aa764 | 5454 | _bfd_elf_write_object_contents (bfd *abfd) |
252b5132 | 5455 | { |
9c5bfbb7 | 5456 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
252b5132 | 5457 | Elf_Internal_Shdr **i_shdrp; |
b34976b6 | 5458 | bfd_boolean failed; |
9ad5cbcf | 5459 | unsigned int count, num_sec; |
30e8ee25 | 5460 | struct elf_obj_tdata *t; |
252b5132 RH |
5461 | |
5462 | if (! abfd->output_has_begun | |
217aa764 | 5463 | && ! _bfd_elf_compute_section_file_positions (abfd, NULL)) |
b34976b6 | 5464 | return FALSE; |
252b5132 RH |
5465 | |
5466 | i_shdrp = elf_elfsections (abfd); | |
252b5132 | 5467 | |
b34976b6 | 5468 | failed = FALSE; |
252b5132 RH |
5469 | bfd_map_over_sections (abfd, bed->s->write_relocs, &failed); |
5470 | if (failed) | |
b34976b6 | 5471 | return FALSE; |
252b5132 RH |
5472 | |
5473 | _bfd_elf_assign_file_positions_for_relocs (abfd); | |
5474 | ||
c044fabd | 5475 | /* After writing the headers, we need to write the sections too... */ |
9ad5cbcf AM |
5476 | num_sec = elf_numsections (abfd); |
5477 | for (count = 1; count < num_sec; count++) | |
252b5132 RH |
5478 | { |
5479 | if (bed->elf_backend_section_processing) | |
5480 | (*bed->elf_backend_section_processing) (abfd, i_shdrp[count]); | |
5481 | if (i_shdrp[count]->contents) | |
5482 | { | |
dc810e39 AM |
5483 | bfd_size_type amt = i_shdrp[count]->sh_size; |
5484 | ||
252b5132 | 5485 | if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0 |
dc810e39 | 5486 | || bfd_bwrite (i_shdrp[count]->contents, amt, abfd) != amt) |
b34976b6 | 5487 | return FALSE; |
252b5132 RH |
5488 | } |
5489 | } | |
5490 | ||
5491 | /* Write out the section header names. */ | |
30e8ee25 | 5492 | t = elf_tdata (abfd); |
26ae6d5e | 5493 | if (elf_shstrtab (abfd) != NULL |
30e8ee25 | 5494 | && (bfd_seek (abfd, t->shstrtab_hdr.sh_offset, SEEK_SET) != 0 |
08a40648 | 5495 | || !_bfd_elf_strtab_emit (abfd, elf_shstrtab (abfd)))) |
b34976b6 | 5496 | return FALSE; |
252b5132 RH |
5497 | |
5498 | if (bed->elf_backend_final_write_processing) | |
12bd6957 | 5499 | (*bed->elf_backend_final_write_processing) (abfd, elf_linker (abfd)); |
252b5132 | 5500 | |
ff59fc36 RM |
5501 | if (!bed->s->write_shdrs_and_ehdr (abfd)) |
5502 | return FALSE; | |
5503 | ||
5504 | /* This is last since write_shdrs_and_ehdr can touch i_shdrp[0]. */ | |
c0355132 AM |
5505 | if (t->o->build_id.after_write_object_contents != NULL) |
5506 | return (*t->o->build_id.after_write_object_contents) (abfd); | |
ff59fc36 RM |
5507 | |
5508 | return TRUE; | |
252b5132 RH |
5509 | } |
5510 | ||
b34976b6 | 5511 | bfd_boolean |
217aa764 | 5512 | _bfd_elf_write_corefile_contents (bfd *abfd) |
252b5132 | 5513 | { |
c044fabd | 5514 | /* Hopefully this can be done just like an object file. */ |
252b5132 RH |
5515 | return _bfd_elf_write_object_contents (abfd); |
5516 | } | |
c044fabd KH |
5517 | |
5518 | /* Given a section, search the header to find them. */ | |
5519 | ||
cb33740c | 5520 | unsigned int |
198beae2 | 5521 | _bfd_elf_section_from_bfd_section (bfd *abfd, struct bfd_section *asect) |
252b5132 | 5522 | { |
9c5bfbb7 | 5523 | const struct elf_backend_data *bed; |
91d6fa6a | 5524 | unsigned int sec_index; |
252b5132 | 5525 | |
9ad5cbcf AM |
5526 | if (elf_section_data (asect) != NULL |
5527 | && elf_section_data (asect)->this_idx != 0) | |
5528 | return elf_section_data (asect)->this_idx; | |
5529 | ||
5530 | if (bfd_is_abs_section (asect)) | |
91d6fa6a | 5531 | sec_index = SHN_ABS; |
af746e92 | 5532 | else if (bfd_is_com_section (asect)) |
91d6fa6a | 5533 | sec_index = SHN_COMMON; |
af746e92 | 5534 | else if (bfd_is_und_section (asect)) |
91d6fa6a | 5535 | sec_index = SHN_UNDEF; |
af746e92 | 5536 | else |
91d6fa6a | 5537 | sec_index = SHN_BAD; |
252b5132 | 5538 | |
af746e92 | 5539 | bed = get_elf_backend_data (abfd); |
252b5132 RH |
5540 | if (bed->elf_backend_section_from_bfd_section) |
5541 | { | |
91d6fa6a | 5542 | int retval = sec_index; |
9ad5cbcf | 5543 | |
af746e92 AM |
5544 | if ((*bed->elf_backend_section_from_bfd_section) (abfd, asect, &retval)) |
5545 | return retval; | |
252b5132 RH |
5546 | } |
5547 | ||
91d6fa6a | 5548 | if (sec_index == SHN_BAD) |
af746e92 | 5549 | bfd_set_error (bfd_error_nonrepresentable_section); |
252b5132 | 5550 | |
91d6fa6a | 5551 | return sec_index; |
252b5132 RH |
5552 | } |
5553 | ||
5554 | /* Given a BFD symbol, return the index in the ELF symbol table, or -1 | |
5555 | on error. */ | |
5556 | ||
5557 | int | |
217aa764 | 5558 | _bfd_elf_symbol_from_bfd_symbol (bfd *abfd, asymbol **asym_ptr_ptr) |
252b5132 RH |
5559 | { |
5560 | asymbol *asym_ptr = *asym_ptr_ptr; | |
5561 | int idx; | |
5562 | flagword flags = asym_ptr->flags; | |
5563 | ||
5564 | /* When gas creates relocations against local labels, it creates its | |
5565 | own symbol for the section, but does put the symbol into the | |
5566 | symbol chain, so udata is 0. When the linker is generating | |
5567 | relocatable output, this section symbol may be for one of the | |
5568 | input sections rather than the output section. */ | |
5569 | if (asym_ptr->udata.i == 0 | |
5570 | && (flags & BSF_SECTION_SYM) | |
5571 | && asym_ptr->section) | |
5572 | { | |
5372391b | 5573 | asection *sec; |
252b5132 RH |
5574 | int indx; |
5575 | ||
5372391b AM |
5576 | sec = asym_ptr->section; |
5577 | if (sec->owner != abfd && sec->output_section != NULL) | |
5578 | sec = sec->output_section; | |
5579 | if (sec->owner == abfd | |
5580 | && (indx = sec->index) < elf_num_section_syms (abfd) | |
4e89ac30 | 5581 | && elf_section_syms (abfd)[indx] != NULL) |
252b5132 RH |
5582 | asym_ptr->udata.i = elf_section_syms (abfd)[indx]->udata.i; |
5583 | } | |
5584 | ||
5585 | idx = asym_ptr->udata.i; | |
5586 | ||
5587 | if (idx == 0) | |
5588 | { | |
5589 | /* This case can occur when using --strip-symbol on a symbol | |
08a40648 | 5590 | which is used in a relocation entry. */ |
252b5132 | 5591 | (*_bfd_error_handler) |
d003868e AM |
5592 | (_("%B: symbol `%s' required but not present"), |
5593 | abfd, bfd_asymbol_name (asym_ptr)); | |
252b5132 RH |
5594 | bfd_set_error (bfd_error_no_symbols); |
5595 | return -1; | |
5596 | } | |
5597 | ||
5598 | #if DEBUG & 4 | |
5599 | { | |
5600 | fprintf (stderr, | |
9ccb8af9 AM |
5601 | "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8lx\n", |
5602 | (long) asym_ptr, asym_ptr->name, idx, (long) flags); | |
252b5132 RH |
5603 | fflush (stderr); |
5604 | } | |
5605 | #endif | |
5606 | ||
5607 | return idx; | |
5608 | } | |
5609 | ||
84d1d650 | 5610 | /* Rewrite program header information. */ |
252b5132 | 5611 | |
b34976b6 | 5612 | static bfd_boolean |
84d1d650 | 5613 | rewrite_elf_program_header (bfd *ibfd, bfd *obfd) |
252b5132 | 5614 | { |
b34976b6 AM |
5615 | Elf_Internal_Ehdr *iehdr; |
5616 | struct elf_segment_map *map; | |
5617 | struct elf_segment_map *map_first; | |
5618 | struct elf_segment_map **pointer_to_map; | |
5619 | Elf_Internal_Phdr *segment; | |
5620 | asection *section; | |
5621 | unsigned int i; | |
5622 | unsigned int num_segments; | |
5623 | bfd_boolean phdr_included = FALSE; | |
5c44b38e | 5624 | bfd_boolean p_paddr_valid; |
b34976b6 AM |
5625 | bfd_vma maxpagesize; |
5626 | struct elf_segment_map *phdr_adjust_seg = NULL; | |
5627 | unsigned int phdr_adjust_num = 0; | |
9c5bfbb7 | 5628 | const struct elf_backend_data *bed; |
bc67d8a6 | 5629 | |
caf47ea6 | 5630 | bed = get_elf_backend_data (ibfd); |
252b5132 RH |
5631 | iehdr = elf_elfheader (ibfd); |
5632 | ||
bc67d8a6 | 5633 | map_first = NULL; |
c044fabd | 5634 | pointer_to_map = &map_first; |
252b5132 RH |
5635 | |
5636 | num_segments = elf_elfheader (ibfd)->e_phnum; | |
bc67d8a6 NC |
5637 | maxpagesize = get_elf_backend_data (obfd)->maxpagesize; |
5638 | ||
5639 | /* Returns the end address of the segment + 1. */ | |
aecc8f8a AM |
5640 | #define SEGMENT_END(segment, start) \ |
5641 | (start + (segment->p_memsz > segment->p_filesz \ | |
5642 | ? segment->p_memsz : segment->p_filesz)) | |
bc67d8a6 | 5643 | |
eecdbe52 JJ |
5644 | #define SECTION_SIZE(section, segment) \ |
5645 | (((section->flags & (SEC_HAS_CONTENTS | SEC_THREAD_LOCAL)) \ | |
5646 | != SEC_THREAD_LOCAL || segment->p_type == PT_TLS) \ | |
eea6121a | 5647 | ? section->size : 0) |
eecdbe52 | 5648 | |
b34976b6 | 5649 | /* Returns TRUE if the given section is contained within |
bc67d8a6 | 5650 | the given segment. VMA addresses are compared. */ |
aecc8f8a AM |
5651 | #define IS_CONTAINED_BY_VMA(section, segment) \ |
5652 | (section->vma >= segment->p_vaddr \ | |
eecdbe52 | 5653 | && (section->vma + SECTION_SIZE (section, segment) \ |
aecc8f8a | 5654 | <= (SEGMENT_END (segment, segment->p_vaddr)))) |
c044fabd | 5655 | |
b34976b6 | 5656 | /* Returns TRUE if the given section is contained within |
bc67d8a6 | 5657 | the given segment. LMA addresses are compared. */ |
aecc8f8a AM |
5658 | #define IS_CONTAINED_BY_LMA(section, segment, base) \ |
5659 | (section->lma >= base \ | |
eecdbe52 | 5660 | && (section->lma + SECTION_SIZE (section, segment) \ |
aecc8f8a | 5661 | <= SEGMENT_END (segment, base))) |
252b5132 | 5662 | |
0efc80c8 L |
5663 | /* Handle PT_NOTE segment. */ |
5664 | #define IS_NOTE(p, s) \ | |
aecc8f8a | 5665 | (p->p_type == PT_NOTE \ |
0efc80c8 | 5666 | && elf_section_type (s) == SHT_NOTE \ |
aecc8f8a | 5667 | && (bfd_vma) s->filepos >= p->p_offset \ |
cb3ff1e5 | 5668 | && ((bfd_vma) s->filepos + s->size \ |
aecc8f8a | 5669 | <= p->p_offset + p->p_filesz)) |
252b5132 | 5670 | |
0efc80c8 L |
5671 | /* Special case: corefile "NOTE" section containing regs, prpsinfo |
5672 | etc. */ | |
5673 | #define IS_COREFILE_NOTE(p, s) \ | |
5674 | (IS_NOTE (p, s) \ | |
5675 | && bfd_get_format (ibfd) == bfd_core \ | |
5676 | && s->vma == 0 \ | |
5677 | && s->lma == 0) | |
5678 | ||
252b5132 RH |
5679 | /* The complicated case when p_vaddr is 0 is to handle the Solaris |
5680 | linker, which generates a PT_INTERP section with p_vaddr and | |
5681 | p_memsz set to 0. */ | |
aecc8f8a AM |
5682 | #define IS_SOLARIS_PT_INTERP(p, s) \ |
5683 | (p->p_vaddr == 0 \ | |
5684 | && p->p_paddr == 0 \ | |
5685 | && p->p_memsz == 0 \ | |
5686 | && p->p_filesz > 0 \ | |
5687 | && (s->flags & SEC_HAS_CONTENTS) != 0 \ | |
eea6121a | 5688 | && s->size > 0 \ |
aecc8f8a | 5689 | && (bfd_vma) s->filepos >= p->p_offset \ |
cb3ff1e5 | 5690 | && ((bfd_vma) s->filepos + s->size \ |
aecc8f8a | 5691 | <= p->p_offset + p->p_filesz)) |
5c440b1e | 5692 | |
bc67d8a6 NC |
5693 | /* Decide if the given section should be included in the given segment. |
5694 | A section will be included if: | |
f5ffc919 | 5695 | 1. It is within the address space of the segment -- we use the LMA |
08a40648 | 5696 | if that is set for the segment and the VMA otherwise, |
0efc80c8 | 5697 | 2. It is an allocated section or a NOTE section in a PT_NOTE |
d324f6d6 | 5698 | segment. |
bc67d8a6 | 5699 | 3. There is an output section associated with it, |
eecdbe52 | 5700 | 4. The section has not already been allocated to a previous segment. |
2b05f1b7 | 5701 | 5. PT_GNU_STACK segments do not include any sections. |
03394ac9 | 5702 | 6. PT_TLS segment includes only SHF_TLS sections. |
6f79b219 JJ |
5703 | 7. SHF_TLS sections are only in PT_TLS or PT_LOAD segments. |
5704 | 8. PT_DYNAMIC should not contain empty sections at the beginning | |
08a40648 | 5705 | (with the possible exception of .dynamic). */ |
9f17e2a6 | 5706 | #define IS_SECTION_IN_INPUT_SEGMENT(section, segment, bed) \ |
2b05f1b7 L |
5707 | ((((segment->p_paddr \ |
5708 | ? IS_CONTAINED_BY_LMA (section, segment, segment->p_paddr) \ | |
5709 | : IS_CONTAINED_BY_VMA (section, segment)) \ | |
5710 | && (section->flags & SEC_ALLOC) != 0) \ | |
0efc80c8 | 5711 | || IS_NOTE (segment, section)) \ |
2b05f1b7 L |
5712 | && segment->p_type != PT_GNU_STACK \ |
5713 | && (segment->p_type != PT_TLS \ | |
5714 | || (section->flags & SEC_THREAD_LOCAL)) \ | |
5715 | && (segment->p_type == PT_LOAD \ | |
5716 | || segment->p_type == PT_TLS \ | |
5717 | || (section->flags & SEC_THREAD_LOCAL) == 0) \ | |
5718 | && (segment->p_type != PT_DYNAMIC \ | |
5719 | || SECTION_SIZE (section, segment) > 0 \ | |
5720 | || (segment->p_paddr \ | |
5721 | ? segment->p_paddr != section->lma \ | |
5722 | : segment->p_vaddr != section->vma) \ | |
5723 | || (strcmp (bfd_get_section_name (ibfd, section), ".dynamic") \ | |
5724 | == 0)) \ | |
0067a569 | 5725 | && !section->segment_mark) |
bc67d8a6 | 5726 | |
9f17e2a6 L |
5727 | /* If the output section of a section in the input segment is NULL, |
5728 | it is removed from the corresponding output segment. */ | |
5729 | #define INCLUDE_SECTION_IN_SEGMENT(section, segment, bed) \ | |
5730 | (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed) \ | |
5731 | && section->output_section != NULL) | |
5732 | ||
b34976b6 | 5733 | /* Returns TRUE iff seg1 starts after the end of seg2. */ |
b5f852ea NC |
5734 | #define SEGMENT_AFTER_SEGMENT(seg1, seg2, field) \ |
5735 | (seg1->field >= SEGMENT_END (seg2, seg2->field)) | |
5736 | ||
5737 | /* Returns TRUE iff seg1 and seg2 overlap. Segments overlap iff both | |
5738 | their VMA address ranges and their LMA address ranges overlap. | |
5739 | It is possible to have overlapping VMA ranges without overlapping LMA | |
5740 | ranges. RedBoot images for example can have both .data and .bss mapped | |
5741 | to the same VMA range, but with the .data section mapped to a different | |
5742 | LMA. */ | |
aecc8f8a | 5743 | #define SEGMENT_OVERLAPS(seg1, seg2) \ |
b5f852ea | 5744 | ( !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_vaddr) \ |
08a40648 | 5745 | || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_vaddr)) \ |
b5f852ea | 5746 | && !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_paddr) \ |
08a40648 | 5747 | || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_paddr))) |
bc67d8a6 NC |
5748 | |
5749 | /* Initialise the segment mark field. */ | |
5750 | for (section = ibfd->sections; section != NULL; section = section->next) | |
b34976b6 | 5751 | section->segment_mark = FALSE; |
bc67d8a6 | 5752 | |
5c44b38e AM |
5753 | /* The Solaris linker creates program headers in which all the |
5754 | p_paddr fields are zero. When we try to objcopy or strip such a | |
5755 | file, we get confused. Check for this case, and if we find it | |
5756 | don't set the p_paddr_valid fields. */ | |
5757 | p_paddr_valid = FALSE; | |
5758 | for (i = 0, segment = elf_tdata (ibfd)->phdr; | |
5759 | i < num_segments; | |
5760 | i++, segment++) | |
5761 | if (segment->p_paddr != 0) | |
5762 | { | |
5763 | p_paddr_valid = TRUE; | |
5764 | break; | |
5765 | } | |
5766 | ||
252b5132 | 5767 | /* Scan through the segments specified in the program header |
bc67d8a6 | 5768 | of the input BFD. For this first scan we look for overlaps |
9ad5cbcf | 5769 | in the loadable segments. These can be created by weird |
aecc8f8a | 5770 | parameters to objcopy. Also, fix some solaris weirdness. */ |
bc67d8a6 NC |
5771 | for (i = 0, segment = elf_tdata (ibfd)->phdr; |
5772 | i < num_segments; | |
c044fabd | 5773 | i++, segment++) |
252b5132 | 5774 | { |
252b5132 | 5775 | unsigned int j; |
c044fabd | 5776 | Elf_Internal_Phdr *segment2; |
252b5132 | 5777 | |
aecc8f8a AM |
5778 | if (segment->p_type == PT_INTERP) |
5779 | for (section = ibfd->sections; section; section = section->next) | |
5780 | if (IS_SOLARIS_PT_INTERP (segment, section)) | |
5781 | { | |
5782 | /* Mininal change so that the normal section to segment | |
4cc11e76 | 5783 | assignment code will work. */ |
aecc8f8a AM |
5784 | segment->p_vaddr = section->vma; |
5785 | break; | |
5786 | } | |
5787 | ||
bc67d8a6 | 5788 | if (segment->p_type != PT_LOAD) |
b10a8ae0 L |
5789 | { |
5790 | /* Remove PT_GNU_RELRO segment. */ | |
5791 | if (segment->p_type == PT_GNU_RELRO) | |
5792 | segment->p_type = PT_NULL; | |
5793 | continue; | |
5794 | } | |
c044fabd | 5795 | |
bc67d8a6 | 5796 | /* Determine if this segment overlaps any previous segments. */ |
0067a569 | 5797 | for (j = 0, segment2 = elf_tdata (ibfd)->phdr; j < i; j++, segment2++) |
bc67d8a6 NC |
5798 | { |
5799 | bfd_signed_vma extra_length; | |
c044fabd | 5800 | |
bc67d8a6 | 5801 | if (segment2->p_type != PT_LOAD |
0067a569 | 5802 | || !SEGMENT_OVERLAPS (segment, segment2)) |
bc67d8a6 | 5803 | continue; |
c044fabd | 5804 | |
bc67d8a6 NC |
5805 | /* Merge the two segments together. */ |
5806 | if (segment2->p_vaddr < segment->p_vaddr) | |
5807 | { | |
c044fabd | 5808 | /* Extend SEGMENT2 to include SEGMENT and then delete |
08a40648 | 5809 | SEGMENT. */ |
0067a569 AM |
5810 | extra_length = (SEGMENT_END (segment, segment->p_vaddr) |
5811 | - SEGMENT_END (segment2, segment2->p_vaddr)); | |
c044fabd | 5812 | |
bc67d8a6 NC |
5813 | if (extra_length > 0) |
5814 | { | |
0067a569 | 5815 | segment2->p_memsz += extra_length; |
bc67d8a6 NC |
5816 | segment2->p_filesz += extra_length; |
5817 | } | |
c044fabd | 5818 | |
bc67d8a6 | 5819 | segment->p_type = PT_NULL; |
c044fabd | 5820 | |
bc67d8a6 NC |
5821 | /* Since we have deleted P we must restart the outer loop. */ |
5822 | i = 0; | |
5823 | segment = elf_tdata (ibfd)->phdr; | |
5824 | break; | |
5825 | } | |
5826 | else | |
5827 | { | |
c044fabd | 5828 | /* Extend SEGMENT to include SEGMENT2 and then delete |
08a40648 | 5829 | SEGMENT2. */ |
0067a569 AM |
5830 | extra_length = (SEGMENT_END (segment2, segment2->p_vaddr) |
5831 | - SEGMENT_END (segment, segment->p_vaddr)); | |
c044fabd | 5832 | |
bc67d8a6 NC |
5833 | if (extra_length > 0) |
5834 | { | |
0067a569 | 5835 | segment->p_memsz += extra_length; |
bc67d8a6 NC |
5836 | segment->p_filesz += extra_length; |
5837 | } | |
c044fabd | 5838 | |
bc67d8a6 NC |
5839 | segment2->p_type = PT_NULL; |
5840 | } | |
5841 | } | |
5842 | } | |
c044fabd | 5843 | |
bc67d8a6 NC |
5844 | /* The second scan attempts to assign sections to segments. */ |
5845 | for (i = 0, segment = elf_tdata (ibfd)->phdr; | |
5846 | i < num_segments; | |
0067a569 | 5847 | i++, segment++) |
bc67d8a6 | 5848 | { |
0067a569 AM |
5849 | unsigned int section_count; |
5850 | asection **sections; | |
5851 | asection *output_section; | |
5852 | unsigned int isec; | |
5853 | bfd_vma matching_lma; | |
5854 | bfd_vma suggested_lma; | |
5855 | unsigned int j; | |
dc810e39 | 5856 | bfd_size_type amt; |
0067a569 AM |
5857 | asection *first_section; |
5858 | bfd_boolean first_matching_lma; | |
5859 | bfd_boolean first_suggested_lma; | |
bc67d8a6 NC |
5860 | |
5861 | if (segment->p_type == PT_NULL) | |
5862 | continue; | |
c044fabd | 5863 | |
9f17e2a6 | 5864 | first_section = NULL; |
bc67d8a6 | 5865 | /* Compute how many sections might be placed into this segment. */ |
b5f852ea NC |
5866 | for (section = ibfd->sections, section_count = 0; |
5867 | section != NULL; | |
5868 | section = section->next) | |
9f17e2a6 L |
5869 | { |
5870 | /* Find the first section in the input segment, which may be | |
5871 | removed from the corresponding output segment. */ | |
5872 | if (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed)) | |
5873 | { | |
5874 | if (first_section == NULL) | |
5875 | first_section = section; | |
5876 | if (section->output_section != NULL) | |
5877 | ++section_count; | |
5878 | } | |
5879 | } | |
811072d8 | 5880 | |
b5f852ea NC |
5881 | /* Allocate a segment map big enough to contain |
5882 | all of the sections we have selected. */ | |
dc810e39 AM |
5883 | amt = sizeof (struct elf_segment_map); |
5884 | amt += ((bfd_size_type) section_count - 1) * sizeof (asection *); | |
a50b1753 | 5885 | map = (struct elf_segment_map *) bfd_zalloc (obfd, amt); |
bc67d8a6 | 5886 | if (map == NULL) |
b34976b6 | 5887 | return FALSE; |
252b5132 RH |
5888 | |
5889 | /* Initialise the fields of the segment map. Default to | |
5890 | using the physical address of the segment in the input BFD. */ | |
0067a569 AM |
5891 | map->next = NULL; |
5892 | map->p_type = segment->p_type; | |
5893 | map->p_flags = segment->p_flags; | |
bc67d8a6 | 5894 | map->p_flags_valid = 1; |
55d55ac7 | 5895 | |
9f17e2a6 L |
5896 | /* If the first section in the input segment is removed, there is |
5897 | no need to preserve segment physical address in the corresponding | |
5898 | output segment. */ | |
945c025a | 5899 | if (!first_section || first_section->output_section != NULL) |
9f17e2a6 L |
5900 | { |
5901 | map->p_paddr = segment->p_paddr; | |
5c44b38e | 5902 | map->p_paddr_valid = p_paddr_valid; |
9f17e2a6 | 5903 | } |
252b5132 RH |
5904 | |
5905 | /* Determine if this segment contains the ELF file header | |
5906 | and if it contains the program headers themselves. */ | |
bc67d8a6 NC |
5907 | map->includes_filehdr = (segment->p_offset == 0 |
5908 | && segment->p_filesz >= iehdr->e_ehsize); | |
bc67d8a6 | 5909 | map->includes_phdrs = 0; |
252b5132 | 5910 | |
0067a569 | 5911 | if (!phdr_included || segment->p_type != PT_LOAD) |
252b5132 | 5912 | { |
bc67d8a6 NC |
5913 | map->includes_phdrs = |
5914 | (segment->p_offset <= (bfd_vma) iehdr->e_phoff | |
5915 | && (segment->p_offset + segment->p_filesz | |
252b5132 RH |
5916 | >= ((bfd_vma) iehdr->e_phoff |
5917 | + iehdr->e_phnum * iehdr->e_phentsize))); | |
c044fabd | 5918 | |
bc67d8a6 | 5919 | if (segment->p_type == PT_LOAD && map->includes_phdrs) |
b34976b6 | 5920 | phdr_included = TRUE; |
252b5132 RH |
5921 | } |
5922 | ||
bc67d8a6 | 5923 | if (section_count == 0) |
252b5132 RH |
5924 | { |
5925 | /* Special segments, such as the PT_PHDR segment, may contain | |
5926 | no sections, but ordinary, loadable segments should contain | |
1ed89aa9 NC |
5927 | something. They are allowed by the ELF spec however, so only |
5928 | a warning is produced. */ | |
bc67d8a6 | 5929 | if (segment->p_type == PT_LOAD) |
0067a569 AM |
5930 | (*_bfd_error_handler) (_("%B: warning: Empty loadable segment" |
5931 | " detected, is this intentional ?\n"), | |
5932 | ibfd); | |
252b5132 | 5933 | |
bc67d8a6 | 5934 | map->count = 0; |
c044fabd KH |
5935 | *pointer_to_map = map; |
5936 | pointer_to_map = &map->next; | |
252b5132 RH |
5937 | |
5938 | continue; | |
5939 | } | |
5940 | ||
5941 | /* Now scan the sections in the input BFD again and attempt | |
5942 | to add their corresponding output sections to the segment map. | |
5943 | The problem here is how to handle an output section which has | |
5944 | been moved (ie had its LMA changed). There are four possibilities: | |
5945 | ||
5946 | 1. None of the sections have been moved. | |
5947 | In this case we can continue to use the segment LMA from the | |
5948 | input BFD. | |
5949 | ||
5950 | 2. All of the sections have been moved by the same amount. | |
5951 | In this case we can change the segment's LMA to match the LMA | |
5952 | of the first section. | |
5953 | ||
5954 | 3. Some of the sections have been moved, others have not. | |
5955 | In this case those sections which have not been moved can be | |
5956 | placed in the current segment which will have to have its size, | |
5957 | and possibly its LMA changed, and a new segment or segments will | |
5958 | have to be created to contain the other sections. | |
5959 | ||
b5f852ea | 5960 | 4. The sections have been moved, but not by the same amount. |
252b5132 RH |
5961 | In this case we can change the segment's LMA to match the LMA |
5962 | of the first section and we will have to create a new segment | |
5963 | or segments to contain the other sections. | |
5964 | ||
5965 | In order to save time, we allocate an array to hold the section | |
5966 | pointers that we are interested in. As these sections get assigned | |
5967 | to a segment, they are removed from this array. */ | |
5968 | ||
a50b1753 | 5969 | sections = (asection **) bfd_malloc2 (section_count, sizeof (asection *)); |
252b5132 | 5970 | if (sections == NULL) |
b34976b6 | 5971 | return FALSE; |
252b5132 RH |
5972 | |
5973 | /* Step One: Scan for segment vs section LMA conflicts. | |
5974 | Also add the sections to the section array allocated above. | |
5975 | Also add the sections to the current segment. In the common | |
5976 | case, where the sections have not been moved, this means that | |
5977 | we have completely filled the segment, and there is nothing | |
5978 | more to do. */ | |
252b5132 | 5979 | isec = 0; |
72730e0c | 5980 | matching_lma = 0; |
252b5132 | 5981 | suggested_lma = 0; |
0067a569 AM |
5982 | first_matching_lma = TRUE; |
5983 | first_suggested_lma = TRUE; | |
252b5132 | 5984 | |
147d51c2 | 5985 | for (section = ibfd->sections; |
bc67d8a6 NC |
5986 | section != NULL; |
5987 | section = section->next) | |
147d51c2 L |
5988 | if (section == first_section) |
5989 | break; | |
5990 | ||
5991 | for (j = 0; section != NULL; section = section->next) | |
252b5132 | 5992 | { |
caf47ea6 | 5993 | if (INCLUDE_SECTION_IN_SEGMENT (section, segment, bed)) |
c0f7859b | 5994 | { |
bc67d8a6 NC |
5995 | output_section = section->output_section; |
5996 | ||
0067a569 | 5997 | sections[j++] = section; |
252b5132 RH |
5998 | |
5999 | /* The Solaris native linker always sets p_paddr to 0. | |
6000 | We try to catch that case here, and set it to the | |
5e8d7549 NC |
6001 | correct value. Note - some backends require that |
6002 | p_paddr be left as zero. */ | |
5c44b38e | 6003 | if (!p_paddr_valid |
4455705d | 6004 | && segment->p_vaddr != 0 |
0067a569 | 6005 | && !bed->want_p_paddr_set_to_zero |
252b5132 | 6006 | && isec == 0 |
bc67d8a6 | 6007 | && output_section->lma != 0 |
0067a569 AM |
6008 | && output_section->vma == (segment->p_vaddr |
6009 | + (map->includes_filehdr | |
6010 | ? iehdr->e_ehsize | |
6011 | : 0) | |
6012 | + (map->includes_phdrs | |
6013 | ? (iehdr->e_phnum | |
6014 | * iehdr->e_phentsize) | |
6015 | : 0))) | |
bc67d8a6 | 6016 | map->p_paddr = segment->p_vaddr; |
252b5132 RH |
6017 | |
6018 | /* Match up the physical address of the segment with the | |
6019 | LMA address of the output section. */ | |
bc67d8a6 | 6020 | if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr) |
5e8d7549 | 6021 | || IS_COREFILE_NOTE (segment, section) |
0067a569 AM |
6022 | || (bed->want_p_paddr_set_to_zero |
6023 | && IS_CONTAINED_BY_VMA (output_section, segment))) | |
252b5132 | 6024 | { |
0067a569 AM |
6025 | if (first_matching_lma || output_section->lma < matching_lma) |
6026 | { | |
6027 | matching_lma = output_section->lma; | |
6028 | first_matching_lma = FALSE; | |
6029 | } | |
252b5132 RH |
6030 | |
6031 | /* We assume that if the section fits within the segment | |
bc67d8a6 | 6032 | then it does not overlap any other section within that |
252b5132 | 6033 | segment. */ |
0067a569 AM |
6034 | map->sections[isec++] = output_section; |
6035 | } | |
6036 | else if (first_suggested_lma) | |
6037 | { | |
6038 | suggested_lma = output_section->lma; | |
6039 | first_suggested_lma = FALSE; | |
252b5132 | 6040 | } |
147d51c2 L |
6041 | |
6042 | if (j == section_count) | |
6043 | break; | |
252b5132 RH |
6044 | } |
6045 | } | |
6046 | ||
bc67d8a6 | 6047 | BFD_ASSERT (j == section_count); |
252b5132 RH |
6048 | |
6049 | /* Step Two: Adjust the physical address of the current segment, | |
6050 | if necessary. */ | |
bc67d8a6 | 6051 | if (isec == section_count) |
252b5132 RH |
6052 | { |
6053 | /* All of the sections fitted within the segment as currently | |
6054 | specified. This is the default case. Add the segment to | |
6055 | the list of built segments and carry on to process the next | |
6056 | program header in the input BFD. */ | |
bc67d8a6 | 6057 | map->count = section_count; |
c044fabd KH |
6058 | *pointer_to_map = map; |
6059 | pointer_to_map = &map->next; | |
08a40648 | 6060 | |
5c44b38e AM |
6061 | if (p_paddr_valid |
6062 | && !bed->want_p_paddr_set_to_zero | |
147d51c2 | 6063 | && matching_lma != map->p_paddr |
5c44b38e AM |
6064 | && !map->includes_filehdr |
6065 | && !map->includes_phdrs) | |
3271a814 NS |
6066 | /* There is some padding before the first section in the |
6067 | segment. So, we must account for that in the output | |
6068 | segment's vma. */ | |
6069 | map->p_vaddr_offset = matching_lma - map->p_paddr; | |
08a40648 | 6070 | |
252b5132 RH |
6071 | free (sections); |
6072 | continue; | |
6073 | } | |
252b5132 RH |
6074 | else |
6075 | { | |
0067a569 | 6076 | if (!first_matching_lma) |
72730e0c AM |
6077 | { |
6078 | /* At least one section fits inside the current segment. | |
6079 | Keep it, but modify its physical address to match the | |
6080 | LMA of the first section that fitted. */ | |
bc67d8a6 | 6081 | map->p_paddr = matching_lma; |
72730e0c AM |
6082 | } |
6083 | else | |
6084 | { | |
6085 | /* None of the sections fitted inside the current segment. | |
6086 | Change the current segment's physical address to match | |
6087 | the LMA of the first section. */ | |
bc67d8a6 | 6088 | map->p_paddr = suggested_lma; |
72730e0c AM |
6089 | } |
6090 | ||
bc67d8a6 NC |
6091 | /* Offset the segment physical address from the lma |
6092 | to allow for space taken up by elf headers. */ | |
6093 | if (map->includes_filehdr) | |
010c8431 AM |
6094 | { |
6095 | if (map->p_paddr >= iehdr->e_ehsize) | |
6096 | map->p_paddr -= iehdr->e_ehsize; | |
6097 | else | |
6098 | { | |
6099 | map->includes_filehdr = FALSE; | |
6100 | map->includes_phdrs = FALSE; | |
6101 | } | |
6102 | } | |
252b5132 | 6103 | |
bc67d8a6 NC |
6104 | if (map->includes_phdrs) |
6105 | { | |
010c8431 AM |
6106 | if (map->p_paddr >= iehdr->e_phnum * iehdr->e_phentsize) |
6107 | { | |
6108 | map->p_paddr -= iehdr->e_phnum * iehdr->e_phentsize; | |
6109 | ||
6110 | /* iehdr->e_phnum is just an estimate of the number | |
6111 | of program headers that we will need. Make a note | |
6112 | here of the number we used and the segment we chose | |
6113 | to hold these headers, so that we can adjust the | |
6114 | offset when we know the correct value. */ | |
6115 | phdr_adjust_num = iehdr->e_phnum; | |
6116 | phdr_adjust_seg = map; | |
6117 | } | |
6118 | else | |
6119 | map->includes_phdrs = FALSE; | |
bc67d8a6 | 6120 | } |
252b5132 RH |
6121 | } |
6122 | ||
6123 | /* Step Three: Loop over the sections again, this time assigning | |
caf47ea6 | 6124 | those that fit to the current segment and removing them from the |
252b5132 RH |
6125 | sections array; but making sure not to leave large gaps. Once all |
6126 | possible sections have been assigned to the current segment it is | |
6127 | added to the list of built segments and if sections still remain | |
6128 | to be assigned, a new segment is constructed before repeating | |
6129 | the loop. */ | |
6130 | isec = 0; | |
6131 | do | |
6132 | { | |
bc67d8a6 | 6133 | map->count = 0; |
252b5132 | 6134 | suggested_lma = 0; |
0067a569 | 6135 | first_suggested_lma = TRUE; |
252b5132 RH |
6136 | |
6137 | /* Fill the current segment with sections that fit. */ | |
bc67d8a6 | 6138 | for (j = 0; j < section_count; j++) |
252b5132 | 6139 | { |
bc67d8a6 | 6140 | section = sections[j]; |
252b5132 | 6141 | |
bc67d8a6 | 6142 | if (section == NULL) |
252b5132 RH |
6143 | continue; |
6144 | ||
bc67d8a6 | 6145 | output_section = section->output_section; |
252b5132 | 6146 | |
bc67d8a6 | 6147 | BFD_ASSERT (output_section != NULL); |
c044fabd | 6148 | |
bc67d8a6 NC |
6149 | if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr) |
6150 | || IS_COREFILE_NOTE (segment, section)) | |
252b5132 | 6151 | { |
bc67d8a6 | 6152 | if (map->count == 0) |
252b5132 RH |
6153 | { |
6154 | /* If the first section in a segment does not start at | |
bc67d8a6 NC |
6155 | the beginning of the segment, then something is |
6156 | wrong. */ | |
0067a569 AM |
6157 | if (output_section->lma |
6158 | != (map->p_paddr | |
6159 | + (map->includes_filehdr ? iehdr->e_ehsize : 0) | |
6160 | + (map->includes_phdrs | |
6161 | ? iehdr->e_phnum * iehdr->e_phentsize | |
6162 | : 0))) | |
252b5132 RH |
6163 | abort (); |
6164 | } | |
6165 | else | |
6166 | { | |
0067a569 | 6167 | asection *prev_sec; |
252b5132 | 6168 | |
bc67d8a6 | 6169 | prev_sec = map->sections[map->count - 1]; |
252b5132 RH |
6170 | |
6171 | /* If the gap between the end of the previous section | |
bc67d8a6 NC |
6172 | and the start of this section is more than |
6173 | maxpagesize then we need to start a new segment. */ | |
eea6121a | 6174 | if ((BFD_ALIGN (prev_sec->lma + prev_sec->size, |
079e9a2f | 6175 | maxpagesize) |
caf47ea6 | 6176 | < BFD_ALIGN (output_section->lma, maxpagesize)) |
0067a569 | 6177 | || (prev_sec->lma + prev_sec->size |
079e9a2f | 6178 | > output_section->lma)) |
252b5132 | 6179 | { |
0067a569 AM |
6180 | if (first_suggested_lma) |
6181 | { | |
6182 | suggested_lma = output_section->lma; | |
6183 | first_suggested_lma = FALSE; | |
6184 | } | |
252b5132 RH |
6185 | |
6186 | continue; | |
6187 | } | |
6188 | } | |
6189 | ||
bc67d8a6 | 6190 | map->sections[map->count++] = output_section; |
252b5132 RH |
6191 | ++isec; |
6192 | sections[j] = NULL; | |
b34976b6 | 6193 | section->segment_mark = TRUE; |
252b5132 | 6194 | } |
0067a569 AM |
6195 | else if (first_suggested_lma) |
6196 | { | |
6197 | suggested_lma = output_section->lma; | |
6198 | first_suggested_lma = FALSE; | |
6199 | } | |
252b5132 RH |
6200 | } |
6201 | ||
bc67d8a6 | 6202 | BFD_ASSERT (map->count > 0); |
252b5132 RH |
6203 | |
6204 | /* Add the current segment to the list of built segments. */ | |
c044fabd KH |
6205 | *pointer_to_map = map; |
6206 | pointer_to_map = &map->next; | |
252b5132 | 6207 | |
bc67d8a6 | 6208 | if (isec < section_count) |
252b5132 RH |
6209 | { |
6210 | /* We still have not allocated all of the sections to | |
6211 | segments. Create a new segment here, initialise it | |
6212 | and carry on looping. */ | |
dc810e39 AM |
6213 | amt = sizeof (struct elf_segment_map); |
6214 | amt += ((bfd_size_type) section_count - 1) * sizeof (asection *); | |
5964fc3a | 6215 | map = (struct elf_segment_map *) bfd_zalloc (obfd, amt); |
bc67d8a6 | 6216 | if (map == NULL) |
5ed6aba4 NC |
6217 | { |
6218 | free (sections); | |
6219 | return FALSE; | |
6220 | } | |
252b5132 RH |
6221 | |
6222 | /* Initialise the fields of the segment map. Set the physical | |
6223 | physical address to the LMA of the first section that has | |
6224 | not yet been assigned. */ | |
0067a569 AM |
6225 | map->next = NULL; |
6226 | map->p_type = segment->p_type; | |
6227 | map->p_flags = segment->p_flags; | |
6228 | map->p_flags_valid = 1; | |
6229 | map->p_paddr = suggested_lma; | |
5c44b38e | 6230 | map->p_paddr_valid = p_paddr_valid; |
bc67d8a6 | 6231 | map->includes_filehdr = 0; |
0067a569 | 6232 | map->includes_phdrs = 0; |
252b5132 RH |
6233 | } |
6234 | } | |
bc67d8a6 | 6235 | while (isec < section_count); |
252b5132 RH |
6236 | |
6237 | free (sections); | |
6238 | } | |
6239 | ||
12bd6957 | 6240 | elf_seg_map (obfd) = map_first; |
bc67d8a6 NC |
6241 | |
6242 | /* If we had to estimate the number of program headers that were | |
9ad5cbcf | 6243 | going to be needed, then check our estimate now and adjust |
bc67d8a6 NC |
6244 | the offset if necessary. */ |
6245 | if (phdr_adjust_seg != NULL) | |
6246 | { | |
6247 | unsigned int count; | |
c044fabd | 6248 | |
bc67d8a6 | 6249 | for (count = 0, map = map_first; map != NULL; map = map->next) |
c044fabd | 6250 | count++; |
252b5132 | 6251 | |
bc67d8a6 NC |
6252 | if (count > phdr_adjust_num) |
6253 | phdr_adjust_seg->p_paddr | |
6254 | -= (count - phdr_adjust_num) * iehdr->e_phentsize; | |
6255 | } | |
c044fabd | 6256 | |
bc67d8a6 | 6257 | #undef SEGMENT_END |
eecdbe52 | 6258 | #undef SECTION_SIZE |
bc67d8a6 NC |
6259 | #undef IS_CONTAINED_BY_VMA |
6260 | #undef IS_CONTAINED_BY_LMA | |
0efc80c8 | 6261 | #undef IS_NOTE |
252b5132 | 6262 | #undef IS_COREFILE_NOTE |
bc67d8a6 | 6263 | #undef IS_SOLARIS_PT_INTERP |
9f17e2a6 | 6264 | #undef IS_SECTION_IN_INPUT_SEGMENT |
bc67d8a6 NC |
6265 | #undef INCLUDE_SECTION_IN_SEGMENT |
6266 | #undef SEGMENT_AFTER_SEGMENT | |
6267 | #undef SEGMENT_OVERLAPS | |
b34976b6 | 6268 | return TRUE; |
252b5132 RH |
6269 | } |
6270 | ||
84d1d650 L |
6271 | /* Copy ELF program header information. */ |
6272 | ||
6273 | static bfd_boolean | |
6274 | copy_elf_program_header (bfd *ibfd, bfd *obfd) | |
6275 | { | |
6276 | Elf_Internal_Ehdr *iehdr; | |
6277 | struct elf_segment_map *map; | |
6278 | struct elf_segment_map *map_first; | |
6279 | struct elf_segment_map **pointer_to_map; | |
6280 | Elf_Internal_Phdr *segment; | |
6281 | unsigned int i; | |
6282 | unsigned int num_segments; | |
6283 | bfd_boolean phdr_included = FALSE; | |
88967714 | 6284 | bfd_boolean p_paddr_valid; |
84d1d650 L |
6285 | |
6286 | iehdr = elf_elfheader (ibfd); | |
6287 | ||
6288 | map_first = NULL; | |
6289 | pointer_to_map = &map_first; | |
6290 | ||
88967714 AM |
6291 | /* If all the segment p_paddr fields are zero, don't set |
6292 | map->p_paddr_valid. */ | |
6293 | p_paddr_valid = FALSE; | |
84d1d650 | 6294 | num_segments = elf_elfheader (ibfd)->e_phnum; |
88967714 AM |
6295 | for (i = 0, segment = elf_tdata (ibfd)->phdr; |
6296 | i < num_segments; | |
6297 | i++, segment++) | |
6298 | if (segment->p_paddr != 0) | |
6299 | { | |
6300 | p_paddr_valid = TRUE; | |
6301 | break; | |
6302 | } | |
6303 | ||
84d1d650 L |
6304 | for (i = 0, segment = elf_tdata (ibfd)->phdr; |
6305 | i < num_segments; | |
6306 | i++, segment++) | |
6307 | { | |
6308 | asection *section; | |
6309 | unsigned int section_count; | |
6310 | bfd_size_type amt; | |
6311 | Elf_Internal_Shdr *this_hdr; | |
53020534 | 6312 | asection *first_section = NULL; |
a76e6f2f | 6313 | asection *lowest_section; |
84d1d650 | 6314 | |
84d1d650 L |
6315 | /* Compute how many sections are in this segment. */ |
6316 | for (section = ibfd->sections, section_count = 0; | |
6317 | section != NULL; | |
6318 | section = section->next) | |
6319 | { | |
6320 | this_hdr = &(elf_section_data(section)->this_hdr); | |
f4638467 | 6321 | if (ELF_SECTION_IN_SEGMENT (this_hdr, segment)) |
3271a814 | 6322 | { |
a76e6f2f AM |
6323 | if (first_section == NULL) |
6324 | first_section = section; | |
3271a814 NS |
6325 | section_count++; |
6326 | } | |
84d1d650 L |
6327 | } |
6328 | ||
6329 | /* Allocate a segment map big enough to contain | |
6330 | all of the sections we have selected. */ | |
6331 | amt = sizeof (struct elf_segment_map); | |
6332 | if (section_count != 0) | |
6333 | amt += ((bfd_size_type) section_count - 1) * sizeof (asection *); | |
a50b1753 | 6334 | map = (struct elf_segment_map *) bfd_zalloc (obfd, amt); |
84d1d650 L |
6335 | if (map == NULL) |
6336 | return FALSE; | |
6337 | ||
6338 | /* Initialize the fields of the output segment map with the | |
6339 | input segment. */ | |
6340 | map->next = NULL; | |
6341 | map->p_type = segment->p_type; | |
6342 | map->p_flags = segment->p_flags; | |
6343 | map->p_flags_valid = 1; | |
6344 | map->p_paddr = segment->p_paddr; | |
88967714 | 6345 | map->p_paddr_valid = p_paddr_valid; |
3f570048 AM |
6346 | map->p_align = segment->p_align; |
6347 | map->p_align_valid = 1; | |
3271a814 | 6348 | map->p_vaddr_offset = 0; |
84d1d650 | 6349 | |
04c3a755 NS |
6350 | if (map->p_type == PT_GNU_RELRO |
6351 | || map->p_type == PT_GNU_STACK) | |
b10a8ae0 L |
6352 | { |
6353 | /* The PT_GNU_RELRO segment may contain the first a few | |
6354 | bytes in the .got.plt section even if the whole .got.plt | |
6355 | section isn't in the PT_GNU_RELRO segment. We won't | |
04c3a755 NS |
6356 | change the size of the PT_GNU_RELRO segment. |
6357 | Similarly, PT_GNU_STACK size is significant on uclinux | |
6358 | systems. */ | |
9433b9b1 | 6359 | map->p_size = segment->p_memsz; |
b10a8ae0 L |
6360 | map->p_size_valid = 1; |
6361 | } | |
6362 | ||
84d1d650 L |
6363 | /* Determine if this segment contains the ELF file header |
6364 | and if it contains the program headers themselves. */ | |
6365 | map->includes_filehdr = (segment->p_offset == 0 | |
6366 | && segment->p_filesz >= iehdr->e_ehsize); | |
6367 | ||
6368 | map->includes_phdrs = 0; | |
6369 | if (! phdr_included || segment->p_type != PT_LOAD) | |
6370 | { | |
6371 | map->includes_phdrs = | |
6372 | (segment->p_offset <= (bfd_vma) iehdr->e_phoff | |
6373 | && (segment->p_offset + segment->p_filesz | |
6374 | >= ((bfd_vma) iehdr->e_phoff | |
6375 | + iehdr->e_phnum * iehdr->e_phentsize))); | |
6376 | ||
6377 | if (segment->p_type == PT_LOAD && map->includes_phdrs) | |
6378 | phdr_included = TRUE; | |
6379 | } | |
6380 | ||
bbefd0a9 | 6381 | lowest_section = NULL; |
84d1d650 L |
6382 | if (section_count != 0) |
6383 | { | |
6384 | unsigned int isec = 0; | |
6385 | ||
53020534 | 6386 | for (section = first_section; |
84d1d650 L |
6387 | section != NULL; |
6388 | section = section->next) | |
6389 | { | |
6390 | this_hdr = &(elf_section_data(section)->this_hdr); | |
f4638467 | 6391 | if (ELF_SECTION_IN_SEGMENT (this_hdr, segment)) |
53020534 L |
6392 | { |
6393 | map->sections[isec++] = section->output_section; | |
a76e6f2f AM |
6394 | if ((section->flags & SEC_ALLOC) != 0) |
6395 | { | |
6396 | bfd_vma seg_off; | |
6397 | ||
bbefd0a9 AM |
6398 | if (lowest_section == NULL |
6399 | || section->lma < lowest_section->lma) | |
fb8a5684 AM |
6400 | lowest_section = section; |
6401 | ||
a76e6f2f AM |
6402 | /* Section lmas are set up from PT_LOAD header |
6403 | p_paddr in _bfd_elf_make_section_from_shdr. | |
6404 | If this header has a p_paddr that disagrees | |
6405 | with the section lma, flag the p_paddr as | |
6406 | invalid. */ | |
6407 | if ((section->flags & SEC_LOAD) != 0) | |
6408 | seg_off = this_hdr->sh_offset - segment->p_offset; | |
6409 | else | |
6410 | seg_off = this_hdr->sh_addr - segment->p_vaddr; | |
6411 | if (section->lma - segment->p_paddr != seg_off) | |
6412 | map->p_paddr_valid = FALSE; | |
6413 | } | |
53020534 L |
6414 | if (isec == section_count) |
6415 | break; | |
6416 | } | |
84d1d650 L |
6417 | } |
6418 | } | |
6419 | ||
a76e6f2f AM |
6420 | if (map->includes_filehdr && lowest_section != NULL) |
6421 | /* We need to keep the space used by the headers fixed. */ | |
6422 | map->header_size = lowest_section->vma - segment->p_vaddr; | |
d324f6d6 | 6423 | |
a76e6f2f AM |
6424 | if (!map->includes_phdrs |
6425 | && !map->includes_filehdr | |
6426 | && map->p_paddr_valid) | |
6427 | /* There is some other padding before the first section. */ | |
6428 | map->p_vaddr_offset = ((lowest_section ? lowest_section->lma : 0) | |
6429 | - segment->p_paddr); | |
6430 | ||
84d1d650 L |
6431 | map->count = section_count; |
6432 | *pointer_to_map = map; | |
6433 | pointer_to_map = &map->next; | |
6434 | } | |
6435 | ||
12bd6957 | 6436 | elf_seg_map (obfd) = map_first; |
84d1d650 L |
6437 | return TRUE; |
6438 | } | |
6439 | ||
6440 | /* Copy private BFD data. This copies or rewrites ELF program header | |
6441 | information. */ | |
6442 | ||
6443 | static bfd_boolean | |
6444 | copy_private_bfd_data (bfd *ibfd, bfd *obfd) | |
6445 | { | |
84d1d650 L |
6446 | if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour |
6447 | || bfd_get_flavour (obfd) != bfd_target_elf_flavour) | |
6448 | return TRUE; | |
6449 | ||
6450 | if (elf_tdata (ibfd)->phdr == NULL) | |
6451 | return TRUE; | |
6452 | ||
6453 | if (ibfd->xvec == obfd->xvec) | |
6454 | { | |
cb3ff1e5 NC |
6455 | /* Check to see if any sections in the input BFD |
6456 | covered by ELF program header have changed. */ | |
d55ce4e2 | 6457 | Elf_Internal_Phdr *segment; |
84d1d650 L |
6458 | asection *section, *osec; |
6459 | unsigned int i, num_segments; | |
6460 | Elf_Internal_Shdr *this_hdr; | |
147d51c2 L |
6461 | const struct elf_backend_data *bed; |
6462 | ||
6463 | bed = get_elf_backend_data (ibfd); | |
6464 | ||
6465 | /* Regenerate the segment map if p_paddr is set to 0. */ | |
6466 | if (bed->want_p_paddr_set_to_zero) | |
6467 | goto rewrite; | |
84d1d650 L |
6468 | |
6469 | /* Initialize the segment mark field. */ | |
6470 | for (section = obfd->sections; section != NULL; | |
6471 | section = section->next) | |
6472 | section->segment_mark = FALSE; | |
6473 | ||
6474 | num_segments = elf_elfheader (ibfd)->e_phnum; | |
6475 | for (i = 0, segment = elf_tdata (ibfd)->phdr; | |
6476 | i < num_segments; | |
6477 | i++, segment++) | |
6478 | { | |
5f6999aa NC |
6479 | /* PR binutils/3535. The Solaris linker always sets the p_paddr |
6480 | and p_memsz fields of special segments (DYNAMIC, INTERP) to 0 | |
6481 | which severly confuses things, so always regenerate the segment | |
6482 | map in this case. */ | |
6483 | if (segment->p_paddr == 0 | |
6484 | && segment->p_memsz == 0 | |
6485 | && (segment->p_type == PT_INTERP || segment->p_type == PT_DYNAMIC)) | |
cb3ff1e5 | 6486 | goto rewrite; |
5f6999aa | 6487 | |
84d1d650 L |
6488 | for (section = ibfd->sections; |
6489 | section != NULL; section = section->next) | |
6490 | { | |
6491 | /* We mark the output section so that we know it comes | |
6492 | from the input BFD. */ | |
6493 | osec = section->output_section; | |
6494 | if (osec) | |
6495 | osec->segment_mark = TRUE; | |
6496 | ||
6497 | /* Check if this section is covered by the segment. */ | |
6498 | this_hdr = &(elf_section_data(section)->this_hdr); | |
f4638467 | 6499 | if (ELF_SECTION_IN_SEGMENT (this_hdr, segment)) |
84d1d650 L |
6500 | { |
6501 | /* FIXME: Check if its output section is changed or | |
6502 | removed. What else do we need to check? */ | |
6503 | if (osec == NULL | |
6504 | || section->flags != osec->flags | |
6505 | || section->lma != osec->lma | |
6506 | || section->vma != osec->vma | |
6507 | || section->size != osec->size | |
6508 | || section->rawsize != osec->rawsize | |
6509 | || section->alignment_power != osec->alignment_power) | |
6510 | goto rewrite; | |
6511 | } | |
6512 | } | |
6513 | } | |
6514 | ||
cb3ff1e5 | 6515 | /* Check to see if any output section do not come from the |
84d1d650 L |
6516 | input BFD. */ |
6517 | for (section = obfd->sections; section != NULL; | |
6518 | section = section->next) | |
6519 | { | |
6520 | if (section->segment_mark == FALSE) | |
6521 | goto rewrite; | |
6522 | else | |
6523 | section->segment_mark = FALSE; | |
6524 | } | |
6525 | ||
6526 | return copy_elf_program_header (ibfd, obfd); | |
6527 | } | |
6528 | ||
6529 | rewrite: | |
f1d85785 L |
6530 | if (ibfd->xvec == obfd->xvec) |
6531 | { | |
6532 | /* When rewriting program header, set the output maxpagesize to | |
6533 | the maximum alignment of input PT_LOAD segments. */ | |
6534 | Elf_Internal_Phdr *segment; | |
6535 | unsigned int i; | |
6536 | unsigned int num_segments = elf_elfheader (ibfd)->e_phnum; | |
6537 | bfd_vma maxpagesize = 0; | |
6538 | ||
6539 | for (i = 0, segment = elf_tdata (ibfd)->phdr; | |
6540 | i < num_segments; | |
6541 | i++, segment++) | |
6542 | if (segment->p_type == PT_LOAD | |
6543 | && maxpagesize < segment->p_align) | |
6544 | maxpagesize = segment->p_align; | |
6545 | ||
6546 | if (maxpagesize != get_elf_backend_data (obfd)->maxpagesize) | |
6547 | bfd_emul_set_maxpagesize (bfd_get_target (obfd), maxpagesize); | |
6548 | } | |
6549 | ||
84d1d650 L |
6550 | return rewrite_elf_program_header (ibfd, obfd); |
6551 | } | |
6552 | ||
ccd2ec6a L |
6553 | /* Initialize private output section information from input section. */ |
6554 | ||
6555 | bfd_boolean | |
6556 | _bfd_elf_init_private_section_data (bfd *ibfd, | |
6557 | asection *isec, | |
6558 | bfd *obfd, | |
6559 | asection *osec, | |
6560 | struct bfd_link_info *link_info) | |
6561 | ||
6562 | { | |
6563 | Elf_Internal_Shdr *ihdr, *ohdr; | |
dfa7b0b8 | 6564 | bfd_boolean final_link = link_info != NULL && !link_info->relocatable; |
ccd2ec6a L |
6565 | |
6566 | if (ibfd->xvec->flavour != bfd_target_elf_flavour | |
6567 | || obfd->xvec->flavour != bfd_target_elf_flavour) | |
6568 | return TRUE; | |
6569 | ||
ba85c43e NC |
6570 | BFD_ASSERT (elf_section_data (osec) != NULL); |
6571 | ||
dfa7b0b8 AM |
6572 | /* For objcopy and relocatable link, don't copy the output ELF |
6573 | section type from input if the output BFD section flags have been | |
6574 | set to something different. For a final link allow some flags | |
6575 | that the linker clears to differ. */ | |
42bb2e33 | 6576 | if (elf_section_type (osec) == SHT_NULL |
dfa7b0b8 AM |
6577 | && (osec->flags == isec->flags |
6578 | || (final_link | |
6579 | && ((osec->flags ^ isec->flags) | |
0814be7d | 6580 | & ~(SEC_LINK_ONCE | SEC_LINK_DUPLICATES | SEC_RELOC)) == 0))) |
42bb2e33 | 6581 | elf_section_type (osec) = elf_section_type (isec); |
d270463e L |
6582 | |
6583 | /* FIXME: Is this correct for all OS/PROC specific flags? */ | |
6584 | elf_section_flags (osec) |= (elf_section_flags (isec) | |
6585 | & (SHF_MASKOS | SHF_MASKPROC)); | |
ccd2ec6a L |
6586 | |
6587 | /* Set things up for objcopy and relocatable link. The output | |
6588 | SHT_GROUP section will have its elf_next_in_group pointing back | |
6589 | to the input group members. Ignore linker created group section. | |
6590 | See elfNN_ia64_object_p in elfxx-ia64.c. */ | |
dfa7b0b8 | 6591 | if (!final_link) |
ccd2ec6a L |
6592 | { |
6593 | if (elf_sec_group (isec) == NULL | |
6594 | || (elf_sec_group (isec)->flags & SEC_LINKER_CREATED) == 0) | |
6595 | { | |
6596 | if (elf_section_flags (isec) & SHF_GROUP) | |
6597 | elf_section_flags (osec) |= SHF_GROUP; | |
6598 | elf_next_in_group (osec) = elf_next_in_group (isec); | |
9659de1c | 6599 | elf_section_data (osec)->group = elf_section_data (isec)->group; |
ccd2ec6a L |
6600 | } |
6601 | } | |
6602 | ||
6603 | ihdr = &elf_section_data (isec)->this_hdr; | |
6604 | ||
6605 | /* We need to handle elf_linked_to_section for SHF_LINK_ORDER. We | |
6606 | don't use the output section of the linked-to section since it | |
6607 | may be NULL at this point. */ | |
6608 | if ((ihdr->sh_flags & SHF_LINK_ORDER) != 0) | |
6609 | { | |
6610 | ohdr = &elf_section_data (osec)->this_hdr; | |
6611 | ohdr->sh_flags |= SHF_LINK_ORDER; | |
6612 | elf_linked_to_section (osec) = elf_linked_to_section (isec); | |
6613 | } | |
6614 | ||
6615 | osec->use_rela_p = isec->use_rela_p; | |
6616 | ||
6617 | return TRUE; | |
6618 | } | |
6619 | ||
252b5132 RH |
6620 | /* Copy private section information. This copies over the entsize |
6621 | field, and sometimes the info field. */ | |
6622 | ||
b34976b6 | 6623 | bfd_boolean |
217aa764 AM |
6624 | _bfd_elf_copy_private_section_data (bfd *ibfd, |
6625 | asection *isec, | |
6626 | bfd *obfd, | |
6627 | asection *osec) | |
252b5132 RH |
6628 | { |
6629 | Elf_Internal_Shdr *ihdr, *ohdr; | |
6630 | ||
6631 | if (ibfd->xvec->flavour != bfd_target_elf_flavour | |
6632 | || obfd->xvec->flavour != bfd_target_elf_flavour) | |
b34976b6 | 6633 | return TRUE; |
252b5132 | 6634 | |
252b5132 RH |
6635 | ihdr = &elf_section_data (isec)->this_hdr; |
6636 | ohdr = &elf_section_data (osec)->this_hdr; | |
6637 | ||
6638 | ohdr->sh_entsize = ihdr->sh_entsize; | |
6639 | ||
6640 | if (ihdr->sh_type == SHT_SYMTAB | |
6641 | || ihdr->sh_type == SHT_DYNSYM | |
6642 | || ihdr->sh_type == SHT_GNU_verneed | |
6643 | || ihdr->sh_type == SHT_GNU_verdef) | |
6644 | ohdr->sh_info = ihdr->sh_info; | |
6645 | ||
ccd2ec6a L |
6646 | return _bfd_elf_init_private_section_data (ibfd, isec, obfd, osec, |
6647 | NULL); | |
252b5132 RH |
6648 | } |
6649 | ||
d0bf826b AM |
6650 | /* Look at all the SHT_GROUP sections in IBFD, making any adjustments |
6651 | necessary if we are removing either the SHT_GROUP section or any of | |
6652 | the group member sections. DISCARDED is the value that a section's | |
6653 | output_section has if the section will be discarded, NULL when this | |
6654 | function is called from objcopy, bfd_abs_section_ptr when called | |
6655 | from the linker. */ | |
80fccad2 BW |
6656 | |
6657 | bfd_boolean | |
d0bf826b | 6658 | _bfd_elf_fixup_group_sections (bfd *ibfd, asection *discarded) |
80fccad2 | 6659 | { |
30288845 AM |
6660 | asection *isec; |
6661 | ||
30288845 | 6662 | for (isec = ibfd->sections; isec != NULL; isec = isec->next) |
415f38a6 | 6663 | if (elf_section_type (isec) == SHT_GROUP) |
30288845 AM |
6664 | { |
6665 | asection *first = elf_next_in_group (isec); | |
6666 | asection *s = first; | |
d0bf826b AM |
6667 | bfd_size_type removed = 0; |
6668 | ||
30288845 AM |
6669 | while (s != NULL) |
6670 | { | |
415f38a6 AM |
6671 | /* If this member section is being output but the |
6672 | SHT_GROUP section is not, then clear the group info | |
6673 | set up by _bfd_elf_copy_private_section_data. */ | |
d0bf826b AM |
6674 | if (s->output_section != discarded |
6675 | && isec->output_section == discarded) | |
30288845 AM |
6676 | { |
6677 | elf_section_flags (s->output_section) &= ~SHF_GROUP; | |
6678 | elf_group_name (s->output_section) = NULL; | |
6679 | } | |
415f38a6 AM |
6680 | /* Conversely, if the member section is not being output |
6681 | but the SHT_GROUP section is, then adjust its size. */ | |
d0bf826b AM |
6682 | else if (s->output_section == discarded |
6683 | && isec->output_section != discarded) | |
6684 | removed += 4; | |
30288845 AM |
6685 | s = elf_next_in_group (s); |
6686 | if (s == first) | |
6687 | break; | |
6688 | } | |
d0bf826b AM |
6689 | if (removed != 0) |
6690 | { | |
6691 | if (discarded != NULL) | |
6692 | { | |
6693 | /* If we've been called for ld -r, then we need to | |
6694 | adjust the input section size. This function may | |
6695 | be called multiple times, so save the original | |
6696 | size. */ | |
6697 | if (isec->rawsize == 0) | |
6698 | isec->rawsize = isec->size; | |
6699 | isec->size = isec->rawsize - removed; | |
6700 | } | |
6701 | else | |
6702 | { | |
6703 | /* Adjust the output section size when called from | |
6704 | objcopy. */ | |
6705 | isec->output_section->size -= removed; | |
6706 | } | |
6707 | } | |
30288845 AM |
6708 | } |
6709 | ||
80fccad2 BW |
6710 | return TRUE; |
6711 | } | |
6712 | ||
d0bf826b AM |
6713 | /* Copy private header information. */ |
6714 | ||
6715 | bfd_boolean | |
6716 | _bfd_elf_copy_private_header_data (bfd *ibfd, bfd *obfd) | |
6717 | { | |
6718 | if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour | |
6719 | || bfd_get_flavour (obfd) != bfd_target_elf_flavour) | |
6720 | return TRUE; | |
6721 | ||
6722 | /* Copy over private BFD data if it has not already been copied. | |
6723 | This must be done here, rather than in the copy_private_bfd_data | |
6724 | entry point, because the latter is called after the section | |
6725 | contents have been set, which means that the program headers have | |
6726 | already been worked out. */ | |
12bd6957 | 6727 | if (elf_seg_map (obfd) == NULL && elf_tdata (ibfd)->phdr != NULL) |
d0bf826b AM |
6728 | { |
6729 | if (! copy_private_bfd_data (ibfd, obfd)) | |
6730 | return FALSE; | |
6731 | } | |
6732 | ||
6733 | return _bfd_elf_fixup_group_sections (ibfd, NULL); | |
6734 | } | |
6735 | ||
252b5132 RH |
6736 | /* Copy private symbol information. If this symbol is in a section |
6737 | which we did not map into a BFD section, try to map the section | |
6738 | index correctly. We use special macro definitions for the mapped | |
6739 | section indices; these definitions are interpreted by the | |
6740 | swap_out_syms function. */ | |
6741 | ||
9ad5cbcf AM |
6742 | #define MAP_ONESYMTAB (SHN_HIOS + 1) |
6743 | #define MAP_DYNSYMTAB (SHN_HIOS + 2) | |
6744 | #define MAP_STRTAB (SHN_HIOS + 3) | |
6745 | #define MAP_SHSTRTAB (SHN_HIOS + 4) | |
6746 | #define MAP_SYM_SHNDX (SHN_HIOS + 5) | |
252b5132 | 6747 | |
b34976b6 | 6748 | bfd_boolean |
217aa764 AM |
6749 | _bfd_elf_copy_private_symbol_data (bfd *ibfd, |
6750 | asymbol *isymarg, | |
6751 | bfd *obfd, | |
6752 | asymbol *osymarg) | |
252b5132 RH |
6753 | { |
6754 | elf_symbol_type *isym, *osym; | |
6755 | ||
6756 | if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour | |
6757 | || bfd_get_flavour (obfd) != bfd_target_elf_flavour) | |
b34976b6 | 6758 | return TRUE; |
252b5132 RH |
6759 | |
6760 | isym = elf_symbol_from (ibfd, isymarg); | |
6761 | osym = elf_symbol_from (obfd, osymarg); | |
6762 | ||
6763 | if (isym != NULL | |
8424d8f5 | 6764 | && isym->internal_elf_sym.st_shndx != 0 |
252b5132 RH |
6765 | && osym != NULL |
6766 | && bfd_is_abs_section (isym->symbol.section)) | |
6767 | { | |
6768 | unsigned int shndx; | |
6769 | ||
6770 | shndx = isym->internal_elf_sym.st_shndx; | |
6771 | if (shndx == elf_onesymtab (ibfd)) | |
6772 | shndx = MAP_ONESYMTAB; | |
6773 | else if (shndx == elf_dynsymtab (ibfd)) | |
6774 | shndx = MAP_DYNSYMTAB; | |
12bd6957 | 6775 | else if (shndx == elf_strtab_sec (ibfd)) |
252b5132 | 6776 | shndx = MAP_STRTAB; |
12bd6957 | 6777 | else if (shndx == elf_shstrtab_sec (ibfd)) |
252b5132 | 6778 | shndx = MAP_SHSTRTAB; |
12bd6957 | 6779 | else if (shndx == elf_symtab_shndx (ibfd)) |
9ad5cbcf | 6780 | shndx = MAP_SYM_SHNDX; |
252b5132 RH |
6781 | osym->internal_elf_sym.st_shndx = shndx; |
6782 | } | |
6783 | ||
b34976b6 | 6784 | return TRUE; |
252b5132 RH |
6785 | } |
6786 | ||
6787 | /* Swap out the symbols. */ | |
6788 | ||
b34976b6 | 6789 | static bfd_boolean |
217aa764 AM |
6790 | swap_out_syms (bfd *abfd, |
6791 | struct bfd_strtab_hash **sttp, | |
6792 | int relocatable_p) | |
252b5132 | 6793 | { |
9c5bfbb7 | 6794 | const struct elf_backend_data *bed; |
079e9a2f AM |
6795 | int symcount; |
6796 | asymbol **syms; | |
6797 | struct bfd_strtab_hash *stt; | |
6798 | Elf_Internal_Shdr *symtab_hdr; | |
9ad5cbcf | 6799 | Elf_Internal_Shdr *symtab_shndx_hdr; |
079e9a2f | 6800 | Elf_Internal_Shdr *symstrtab_hdr; |
f075ee0c AM |
6801 | bfd_byte *outbound_syms; |
6802 | bfd_byte *outbound_shndx; | |
079e9a2f | 6803 | int idx; |
12bd6957 | 6804 | unsigned int num_locals; |
079e9a2f | 6805 | bfd_size_type amt; |
174fd7f9 | 6806 | bfd_boolean name_local_sections; |
252b5132 | 6807 | |
12bd6957 | 6808 | if (!elf_map_symbols (abfd, &num_locals)) |
b34976b6 | 6809 | return FALSE; |
252b5132 | 6810 | |
c044fabd | 6811 | /* Dump out the symtabs. */ |
079e9a2f AM |
6812 | stt = _bfd_elf_stringtab_init (); |
6813 | if (stt == NULL) | |
b34976b6 | 6814 | return FALSE; |
252b5132 | 6815 | |
079e9a2f AM |
6816 | bed = get_elf_backend_data (abfd); |
6817 | symcount = bfd_get_symcount (abfd); | |
6818 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; | |
6819 | symtab_hdr->sh_type = SHT_SYMTAB; | |
6820 | symtab_hdr->sh_entsize = bed->s->sizeof_sym; | |
6821 | symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1); | |
12bd6957 | 6822 | symtab_hdr->sh_info = num_locals + 1; |
72de5009 | 6823 | symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align; |
079e9a2f AM |
6824 | |
6825 | symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr; | |
6826 | symstrtab_hdr->sh_type = SHT_STRTAB; | |
6827 | ||
a50b1753 NC |
6828 | outbound_syms = (bfd_byte *) bfd_alloc2 (abfd, 1 + symcount, |
6829 | bed->s->sizeof_sym); | |
079e9a2f | 6830 | if (outbound_syms == NULL) |
5ed6aba4 NC |
6831 | { |
6832 | _bfd_stringtab_free (stt); | |
6833 | return FALSE; | |
6834 | } | |
217aa764 | 6835 | symtab_hdr->contents = outbound_syms; |
252b5132 | 6836 | |
9ad5cbcf AM |
6837 | outbound_shndx = NULL; |
6838 | symtab_shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr; | |
6839 | if (symtab_shndx_hdr->sh_name != 0) | |
6840 | { | |
6841 | amt = (bfd_size_type) (1 + symcount) * sizeof (Elf_External_Sym_Shndx); | |
a50b1753 NC |
6842 | outbound_shndx = (bfd_byte *) |
6843 | bfd_zalloc2 (abfd, 1 + symcount, sizeof (Elf_External_Sym_Shndx)); | |
9ad5cbcf | 6844 | if (outbound_shndx == NULL) |
5ed6aba4 NC |
6845 | { |
6846 | _bfd_stringtab_free (stt); | |
6847 | return FALSE; | |
6848 | } | |
6849 | ||
9ad5cbcf AM |
6850 | symtab_shndx_hdr->contents = outbound_shndx; |
6851 | symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX; | |
6852 | symtab_shndx_hdr->sh_size = amt; | |
6853 | symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx); | |
6854 | symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx); | |
6855 | } | |
6856 | ||
589e6347 | 6857 | /* Now generate the data (for "contents"). */ |
079e9a2f AM |
6858 | { |
6859 | /* Fill in zeroth symbol and swap it out. */ | |
6860 | Elf_Internal_Sym sym; | |
6861 | sym.st_name = 0; | |
6862 | sym.st_value = 0; | |
6863 | sym.st_size = 0; | |
6864 | sym.st_info = 0; | |
6865 | sym.st_other = 0; | |
6866 | sym.st_shndx = SHN_UNDEF; | |
35fc36a8 | 6867 | sym.st_target_internal = 0; |
9ad5cbcf | 6868 | bed->s->swap_symbol_out (abfd, &sym, outbound_syms, outbound_shndx); |
079e9a2f | 6869 | outbound_syms += bed->s->sizeof_sym; |
9ad5cbcf AM |
6870 | if (outbound_shndx != NULL) |
6871 | outbound_shndx += sizeof (Elf_External_Sym_Shndx); | |
079e9a2f | 6872 | } |
252b5132 | 6873 | |
174fd7f9 RS |
6874 | name_local_sections |
6875 | = (bed->elf_backend_name_local_section_symbols | |
6876 | && bed->elf_backend_name_local_section_symbols (abfd)); | |
6877 | ||
079e9a2f AM |
6878 | syms = bfd_get_outsymbols (abfd); |
6879 | for (idx = 0; idx < symcount; idx++) | |
252b5132 | 6880 | { |
252b5132 | 6881 | Elf_Internal_Sym sym; |
079e9a2f AM |
6882 | bfd_vma value = syms[idx]->value; |
6883 | elf_symbol_type *type_ptr; | |
6884 | flagword flags = syms[idx]->flags; | |
6885 | int type; | |
252b5132 | 6886 | |
174fd7f9 RS |
6887 | if (!name_local_sections |
6888 | && (flags & (BSF_SECTION_SYM | BSF_GLOBAL)) == BSF_SECTION_SYM) | |
079e9a2f AM |
6889 | { |
6890 | /* Local section symbols have no name. */ | |
6891 | sym.st_name = 0; | |
6892 | } | |
6893 | else | |
6894 | { | |
6895 | sym.st_name = (unsigned long) _bfd_stringtab_add (stt, | |
6896 | syms[idx]->name, | |
b34976b6 | 6897 | TRUE, FALSE); |
079e9a2f | 6898 | if (sym.st_name == (unsigned long) -1) |
5ed6aba4 NC |
6899 | { |
6900 | _bfd_stringtab_free (stt); | |
6901 | return FALSE; | |
6902 | } | |
079e9a2f | 6903 | } |
252b5132 | 6904 | |
079e9a2f | 6905 | type_ptr = elf_symbol_from (abfd, syms[idx]); |
252b5132 | 6906 | |
079e9a2f AM |
6907 | if ((flags & BSF_SECTION_SYM) == 0 |
6908 | && bfd_is_com_section (syms[idx]->section)) | |
6909 | { | |
6910 | /* ELF common symbols put the alignment into the `value' field, | |
6911 | and the size into the `size' field. This is backwards from | |
6912 | how BFD handles it, so reverse it here. */ | |
6913 | sym.st_size = value; | |
6914 | if (type_ptr == NULL | |
6915 | || type_ptr->internal_elf_sym.st_value == 0) | |
6916 | sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value)); | |
6917 | else | |
6918 | sym.st_value = type_ptr->internal_elf_sym.st_value; | |
6919 | sym.st_shndx = _bfd_elf_section_from_bfd_section | |
6920 | (abfd, syms[idx]->section); | |
6921 | } | |
6922 | else | |
6923 | { | |
6924 | asection *sec = syms[idx]->section; | |
cb33740c | 6925 | unsigned int shndx; |
252b5132 | 6926 | |
079e9a2f AM |
6927 | if (sec->output_section) |
6928 | { | |
6929 | value += sec->output_offset; | |
6930 | sec = sec->output_section; | |
6931 | } | |
589e6347 | 6932 | |
079e9a2f AM |
6933 | /* Don't add in the section vma for relocatable output. */ |
6934 | if (! relocatable_p) | |
6935 | value += sec->vma; | |
6936 | sym.st_value = value; | |
6937 | sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0; | |
6938 | ||
6939 | if (bfd_is_abs_section (sec) | |
6940 | && type_ptr != NULL | |
6941 | && type_ptr->internal_elf_sym.st_shndx != 0) | |
6942 | { | |
6943 | /* This symbol is in a real ELF section which we did | |
6944 | not create as a BFD section. Undo the mapping done | |
6945 | by copy_private_symbol_data. */ | |
6946 | shndx = type_ptr->internal_elf_sym.st_shndx; | |
6947 | switch (shndx) | |
6948 | { | |
6949 | case MAP_ONESYMTAB: | |
6950 | shndx = elf_onesymtab (abfd); | |
6951 | break; | |
6952 | case MAP_DYNSYMTAB: | |
6953 | shndx = elf_dynsymtab (abfd); | |
6954 | break; | |
6955 | case MAP_STRTAB: | |
12bd6957 | 6956 | shndx = elf_strtab_sec (abfd); |
079e9a2f AM |
6957 | break; |
6958 | case MAP_SHSTRTAB: | |
12bd6957 | 6959 | shndx = elf_shstrtab_sec (abfd); |
079e9a2f | 6960 | break; |
9ad5cbcf | 6961 | case MAP_SYM_SHNDX: |
12bd6957 | 6962 | shndx = elf_symtab_shndx (abfd); |
9ad5cbcf | 6963 | break; |
079e9a2f | 6964 | default: |
15bc576a | 6965 | shndx = SHN_ABS; |
079e9a2f AM |
6966 | break; |
6967 | } | |
6968 | } | |
6969 | else | |
6970 | { | |
6971 | shndx = _bfd_elf_section_from_bfd_section (abfd, sec); | |
252b5132 | 6972 | |
cb33740c | 6973 | if (shndx == SHN_BAD) |
079e9a2f AM |
6974 | { |
6975 | asection *sec2; | |
6976 | ||
6977 | /* Writing this would be a hell of a lot easier if | |
6978 | we had some decent documentation on bfd, and | |
6979 | knew what to expect of the library, and what to | |
6980 | demand of applications. For example, it | |
6981 | appears that `objcopy' might not set the | |
6982 | section of a symbol to be a section that is | |
6983 | actually in the output file. */ | |
6984 | sec2 = bfd_get_section_by_name (abfd, sec->name); | |
589e6347 NC |
6985 | if (sec2 == NULL) |
6986 | { | |
6987 | _bfd_error_handler (_("\ | |
6988 | Unable to find equivalent output section for symbol '%s' from section '%s'"), | |
6989 | syms[idx]->name ? syms[idx]->name : "<Local sym>", | |
6990 | sec->name); | |
811072d8 | 6991 | bfd_set_error (bfd_error_invalid_operation); |
5ed6aba4 | 6992 | _bfd_stringtab_free (stt); |
589e6347 NC |
6993 | return FALSE; |
6994 | } | |
811072d8 | 6995 | |
079e9a2f | 6996 | shndx = _bfd_elf_section_from_bfd_section (abfd, sec2); |
cb33740c | 6997 | BFD_ASSERT (shndx != SHN_BAD); |
079e9a2f AM |
6998 | } |
6999 | } | |
252b5132 | 7000 | |
079e9a2f AM |
7001 | sym.st_shndx = shndx; |
7002 | } | |
252b5132 | 7003 | |
13ae64f3 JJ |
7004 | if ((flags & BSF_THREAD_LOCAL) != 0) |
7005 | type = STT_TLS; | |
d8045f23 NC |
7006 | else if ((flags & BSF_GNU_INDIRECT_FUNCTION) != 0) |
7007 | type = STT_GNU_IFUNC; | |
13ae64f3 | 7008 | else if ((flags & BSF_FUNCTION) != 0) |
079e9a2f AM |
7009 | type = STT_FUNC; |
7010 | else if ((flags & BSF_OBJECT) != 0) | |
7011 | type = STT_OBJECT; | |
d9352518 DB |
7012 | else if ((flags & BSF_RELC) != 0) |
7013 | type = STT_RELC; | |
7014 | else if ((flags & BSF_SRELC) != 0) | |
7015 | type = STT_SRELC; | |
079e9a2f AM |
7016 | else |
7017 | type = STT_NOTYPE; | |
252b5132 | 7018 | |
13ae64f3 JJ |
7019 | if (syms[idx]->section->flags & SEC_THREAD_LOCAL) |
7020 | type = STT_TLS; | |
7021 | ||
589e6347 | 7022 | /* Processor-specific types. */ |
079e9a2f AM |
7023 | if (type_ptr != NULL |
7024 | && bed->elf_backend_get_symbol_type) | |
7025 | type = ((*bed->elf_backend_get_symbol_type) | |
7026 | (&type_ptr->internal_elf_sym, type)); | |
252b5132 | 7027 | |
079e9a2f AM |
7028 | if (flags & BSF_SECTION_SYM) |
7029 | { | |
7030 | if (flags & BSF_GLOBAL) | |
7031 | sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION); | |
7032 | else | |
7033 | sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION); | |
7034 | } | |
7035 | else if (bfd_is_com_section (syms[idx]->section)) | |
0a40daed | 7036 | { |
504b7d20 | 7037 | #ifdef USE_STT_COMMON |
0a40daed MK |
7038 | if (type == STT_OBJECT) |
7039 | sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_COMMON); | |
7040 | else | |
504b7d20 | 7041 | #endif |
c91e322a | 7042 | sym.st_info = ELF_ST_INFO (STB_GLOBAL, type); |
0a40daed | 7043 | } |
079e9a2f AM |
7044 | else if (bfd_is_und_section (syms[idx]->section)) |
7045 | sym.st_info = ELF_ST_INFO (((flags & BSF_WEAK) | |
7046 | ? STB_WEAK | |
7047 | : STB_GLOBAL), | |
7048 | type); | |
7049 | else if (flags & BSF_FILE) | |
7050 | sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE); | |
7051 | else | |
7052 | { | |
7053 | int bind = STB_LOCAL; | |
252b5132 | 7054 | |
079e9a2f AM |
7055 | if (flags & BSF_LOCAL) |
7056 | bind = STB_LOCAL; | |
3e7a7d11 NC |
7057 | else if (flags & BSF_GNU_UNIQUE) |
7058 | bind = STB_GNU_UNIQUE; | |
079e9a2f AM |
7059 | else if (flags & BSF_WEAK) |
7060 | bind = STB_WEAK; | |
7061 | else if (flags & BSF_GLOBAL) | |
7062 | bind = STB_GLOBAL; | |
252b5132 | 7063 | |
079e9a2f AM |
7064 | sym.st_info = ELF_ST_INFO (bind, type); |
7065 | } | |
252b5132 | 7066 | |
079e9a2f | 7067 | if (type_ptr != NULL) |
35fc36a8 RS |
7068 | { |
7069 | sym.st_other = type_ptr->internal_elf_sym.st_other; | |
7070 | sym.st_target_internal | |
7071 | = type_ptr->internal_elf_sym.st_target_internal; | |
7072 | } | |
079e9a2f | 7073 | else |
35fc36a8 RS |
7074 | { |
7075 | sym.st_other = 0; | |
7076 | sym.st_target_internal = 0; | |
7077 | } | |
252b5132 | 7078 | |
9ad5cbcf | 7079 | bed->s->swap_symbol_out (abfd, &sym, outbound_syms, outbound_shndx); |
079e9a2f | 7080 | outbound_syms += bed->s->sizeof_sym; |
9ad5cbcf AM |
7081 | if (outbound_shndx != NULL) |
7082 | outbound_shndx += sizeof (Elf_External_Sym_Shndx); | |
079e9a2f | 7083 | } |
252b5132 | 7084 | |
079e9a2f AM |
7085 | *sttp = stt; |
7086 | symstrtab_hdr->sh_size = _bfd_stringtab_size (stt); | |
7087 | symstrtab_hdr->sh_type = SHT_STRTAB; | |
252b5132 | 7088 | |
079e9a2f AM |
7089 | symstrtab_hdr->sh_flags = 0; |
7090 | symstrtab_hdr->sh_addr = 0; | |
7091 | symstrtab_hdr->sh_entsize = 0; | |
7092 | symstrtab_hdr->sh_link = 0; | |
7093 | symstrtab_hdr->sh_info = 0; | |
7094 | symstrtab_hdr->sh_addralign = 1; | |
252b5132 | 7095 | |
b34976b6 | 7096 | return TRUE; |
252b5132 RH |
7097 | } |
7098 | ||
7099 | /* Return the number of bytes required to hold the symtab vector. | |
7100 | ||
7101 | Note that we base it on the count plus 1, since we will null terminate | |
7102 | the vector allocated based on this size. However, the ELF symbol table | |
7103 | always has a dummy entry as symbol #0, so it ends up even. */ | |
7104 | ||
7105 | long | |
217aa764 | 7106 | _bfd_elf_get_symtab_upper_bound (bfd *abfd) |
252b5132 RH |
7107 | { |
7108 | long symcount; | |
7109 | long symtab_size; | |
7110 | Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr; | |
7111 | ||
7112 | symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym; | |
b99d1833 AM |
7113 | symtab_size = (symcount + 1) * (sizeof (asymbol *)); |
7114 | if (symcount > 0) | |
7115 | symtab_size -= sizeof (asymbol *); | |
252b5132 RH |
7116 | |
7117 | return symtab_size; | |
7118 | } | |
7119 | ||
7120 | long | |
217aa764 | 7121 | _bfd_elf_get_dynamic_symtab_upper_bound (bfd *abfd) |
252b5132 RH |
7122 | { |
7123 | long symcount; | |
7124 | long symtab_size; | |
7125 | Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr; | |
7126 | ||
7127 | if (elf_dynsymtab (abfd) == 0) | |
7128 | { | |
7129 | bfd_set_error (bfd_error_invalid_operation); | |
7130 | return -1; | |
7131 | } | |
7132 | ||
7133 | symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym; | |
b99d1833 AM |
7134 | symtab_size = (symcount + 1) * (sizeof (asymbol *)); |
7135 | if (symcount > 0) | |
7136 | symtab_size -= sizeof (asymbol *); | |
252b5132 RH |
7137 | |
7138 | return symtab_size; | |
7139 | } | |
7140 | ||
7141 | long | |
217aa764 AM |
7142 | _bfd_elf_get_reloc_upper_bound (bfd *abfd ATTRIBUTE_UNUSED, |
7143 | sec_ptr asect) | |
252b5132 RH |
7144 | { |
7145 | return (asect->reloc_count + 1) * sizeof (arelent *); | |
7146 | } | |
7147 | ||
7148 | /* Canonicalize the relocs. */ | |
7149 | ||
7150 | long | |
217aa764 AM |
7151 | _bfd_elf_canonicalize_reloc (bfd *abfd, |
7152 | sec_ptr section, | |
7153 | arelent **relptr, | |
7154 | asymbol **symbols) | |
252b5132 RH |
7155 | { |
7156 | arelent *tblptr; | |
7157 | unsigned int i; | |
9c5bfbb7 | 7158 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
252b5132 | 7159 | |
b34976b6 | 7160 | if (! bed->s->slurp_reloc_table (abfd, section, symbols, FALSE)) |
252b5132 RH |
7161 | return -1; |
7162 | ||
7163 | tblptr = section->relocation; | |
7164 | for (i = 0; i < section->reloc_count; i++) | |
7165 | *relptr++ = tblptr++; | |
7166 | ||
7167 | *relptr = NULL; | |
7168 | ||
7169 | return section->reloc_count; | |
7170 | } | |
7171 | ||
7172 | long | |
6cee3f79 | 7173 | _bfd_elf_canonicalize_symtab (bfd *abfd, asymbol **allocation) |
252b5132 | 7174 | { |
9c5bfbb7 | 7175 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
217aa764 | 7176 | long symcount = bed->s->slurp_symbol_table (abfd, allocation, FALSE); |
252b5132 RH |
7177 | |
7178 | if (symcount >= 0) | |
7179 | bfd_get_symcount (abfd) = symcount; | |
7180 | return symcount; | |
7181 | } | |
7182 | ||
7183 | long | |
217aa764 AM |
7184 | _bfd_elf_canonicalize_dynamic_symtab (bfd *abfd, |
7185 | asymbol **allocation) | |
252b5132 | 7186 | { |
9c5bfbb7 | 7187 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
217aa764 | 7188 | long symcount = bed->s->slurp_symbol_table (abfd, allocation, TRUE); |
1f70368c DJ |
7189 | |
7190 | if (symcount >= 0) | |
7191 | bfd_get_dynamic_symcount (abfd) = symcount; | |
7192 | return symcount; | |
252b5132 RH |
7193 | } |
7194 | ||
8615f3f2 AM |
7195 | /* Return the size required for the dynamic reloc entries. Any loadable |
7196 | section that was actually installed in the BFD, and has type SHT_REL | |
7197 | or SHT_RELA, and uses the dynamic symbol table, is considered to be a | |
7198 | dynamic reloc section. */ | |
252b5132 RH |
7199 | |
7200 | long | |
217aa764 | 7201 | _bfd_elf_get_dynamic_reloc_upper_bound (bfd *abfd) |
252b5132 RH |
7202 | { |
7203 | long ret; | |
7204 | asection *s; | |
7205 | ||
7206 | if (elf_dynsymtab (abfd) == 0) | |
7207 | { | |
7208 | bfd_set_error (bfd_error_invalid_operation); | |
7209 | return -1; | |
7210 | } | |
7211 | ||
7212 | ret = sizeof (arelent *); | |
7213 | for (s = abfd->sections; s != NULL; s = s->next) | |
266b05cf | 7214 | if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd) |
252b5132 RH |
7215 | && (elf_section_data (s)->this_hdr.sh_type == SHT_REL |
7216 | || elf_section_data (s)->this_hdr.sh_type == SHT_RELA)) | |
eea6121a | 7217 | ret += ((s->size / elf_section_data (s)->this_hdr.sh_entsize) |
252b5132 RH |
7218 | * sizeof (arelent *)); |
7219 | ||
7220 | return ret; | |
7221 | } | |
7222 | ||
8615f3f2 AM |
7223 | /* Canonicalize the dynamic relocation entries. Note that we return the |
7224 | dynamic relocations as a single block, although they are actually | |
7225 | associated with particular sections; the interface, which was | |
7226 | designed for SunOS style shared libraries, expects that there is only | |
7227 | one set of dynamic relocs. Any loadable section that was actually | |
7228 | installed in the BFD, and has type SHT_REL or SHT_RELA, and uses the | |
7229 | dynamic symbol table, is considered to be a dynamic reloc section. */ | |
252b5132 RH |
7230 | |
7231 | long | |
217aa764 AM |
7232 | _bfd_elf_canonicalize_dynamic_reloc (bfd *abfd, |
7233 | arelent **storage, | |
7234 | asymbol **syms) | |
252b5132 | 7235 | { |
217aa764 | 7236 | bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean); |
252b5132 RH |
7237 | asection *s; |
7238 | long ret; | |
7239 | ||
7240 | if (elf_dynsymtab (abfd) == 0) | |
7241 | { | |
7242 | bfd_set_error (bfd_error_invalid_operation); | |
7243 | return -1; | |
7244 | } | |
7245 | ||
7246 | slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table; | |
7247 | ret = 0; | |
7248 | for (s = abfd->sections; s != NULL; s = s->next) | |
7249 | { | |
266b05cf | 7250 | if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd) |
252b5132 RH |
7251 | && (elf_section_data (s)->this_hdr.sh_type == SHT_REL |
7252 | || elf_section_data (s)->this_hdr.sh_type == SHT_RELA)) | |
7253 | { | |
7254 | arelent *p; | |
7255 | long count, i; | |
7256 | ||
b34976b6 | 7257 | if (! (*slurp_relocs) (abfd, s, syms, TRUE)) |
252b5132 | 7258 | return -1; |
eea6121a | 7259 | count = s->size / elf_section_data (s)->this_hdr.sh_entsize; |
252b5132 RH |
7260 | p = s->relocation; |
7261 | for (i = 0; i < count; i++) | |
7262 | *storage++ = p++; | |
7263 | ret += count; | |
7264 | } | |
7265 | } | |
7266 | ||
7267 | *storage = NULL; | |
7268 | ||
7269 | return ret; | |
7270 | } | |
7271 | \f | |
7272 | /* Read in the version information. */ | |
7273 | ||
b34976b6 | 7274 | bfd_boolean |
fc0e6df6 | 7275 | _bfd_elf_slurp_version_tables (bfd *abfd, bfd_boolean default_imported_symver) |
252b5132 RH |
7276 | { |
7277 | bfd_byte *contents = NULL; | |
fc0e6df6 PB |
7278 | unsigned int freeidx = 0; |
7279 | ||
7280 | if (elf_dynverref (abfd) != 0) | |
7281 | { | |
7282 | Elf_Internal_Shdr *hdr; | |
7283 | Elf_External_Verneed *everneed; | |
7284 | Elf_Internal_Verneed *iverneed; | |
7285 | unsigned int i; | |
d0fb9a8d | 7286 | bfd_byte *contents_end; |
fc0e6df6 PB |
7287 | |
7288 | hdr = &elf_tdata (abfd)->dynverref_hdr; | |
7289 | ||
601a03ba | 7290 | if (hdr->sh_info == 0 || hdr->sh_size < sizeof (Elf_External_Verneed)) |
d0fb9a8d | 7291 | { |
601a03ba AM |
7292 | error_return_bad_verref: |
7293 | (*_bfd_error_handler) | |
7294 | (_("%B: .gnu.version_r invalid entry"), abfd); | |
7295 | bfd_set_error (bfd_error_bad_value); | |
d0fb9a8d JJ |
7296 | error_return_verref: |
7297 | elf_tdata (abfd)->verref = NULL; | |
7298 | elf_tdata (abfd)->cverrefs = 0; | |
7299 | goto error_return; | |
7300 | } | |
601a03ba AM |
7301 | |
7302 | contents = (bfd_byte *) bfd_malloc (hdr->sh_size); | |
7303 | if (contents == NULL) | |
7304 | goto error_return_verref; | |
7305 | ||
fc0e6df6 PB |
7306 | if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0 |
7307 | || bfd_bread (contents, hdr->sh_size, abfd) != hdr->sh_size) | |
d0fb9a8d | 7308 | goto error_return_verref; |
fc0e6df6 | 7309 | |
601a03ba AM |
7310 | elf_tdata (abfd)->cverrefs = hdr->sh_info; |
7311 | elf_tdata (abfd)->verref = (Elf_Internal_Verneed *) | |
7312 | bfd_zalloc2 (abfd, hdr->sh_info, sizeof (Elf_Internal_Verneed)); | |
7313 | ||
7314 | if (elf_tdata (abfd)->verref == NULL) | |
d0fb9a8d JJ |
7315 | goto error_return_verref; |
7316 | ||
7317 | BFD_ASSERT (sizeof (Elf_External_Verneed) | |
7318 | == sizeof (Elf_External_Vernaux)); | |
7319 | contents_end = contents + hdr->sh_size - sizeof (Elf_External_Verneed); | |
fc0e6df6 PB |
7320 | everneed = (Elf_External_Verneed *) contents; |
7321 | iverneed = elf_tdata (abfd)->verref; | |
7322 | for (i = 0; i < hdr->sh_info; i++, iverneed++) | |
7323 | { | |
7324 | Elf_External_Vernaux *evernaux; | |
7325 | Elf_Internal_Vernaux *ivernaux; | |
7326 | unsigned int j; | |
7327 | ||
7328 | _bfd_elf_swap_verneed_in (abfd, everneed, iverneed); | |
7329 | ||
7330 | iverneed->vn_bfd = abfd; | |
7331 | ||
7332 | iverneed->vn_filename = | |
7333 | bfd_elf_string_from_elf_section (abfd, hdr->sh_link, | |
7334 | iverneed->vn_file); | |
7335 | if (iverneed->vn_filename == NULL) | |
601a03ba | 7336 | goto error_return_bad_verref; |
fc0e6df6 | 7337 | |
d0fb9a8d JJ |
7338 | if (iverneed->vn_cnt == 0) |
7339 | iverneed->vn_auxptr = NULL; | |
7340 | else | |
7341 | { | |
a50b1753 NC |
7342 | iverneed->vn_auxptr = (struct elf_internal_vernaux *) |
7343 | bfd_alloc2 (abfd, iverneed->vn_cnt, | |
7344 | sizeof (Elf_Internal_Vernaux)); | |
d0fb9a8d JJ |
7345 | if (iverneed->vn_auxptr == NULL) |
7346 | goto error_return_verref; | |
7347 | } | |
7348 | ||
7349 | if (iverneed->vn_aux | |
7350 | > (size_t) (contents_end - (bfd_byte *) everneed)) | |
601a03ba | 7351 | goto error_return_bad_verref; |
fc0e6df6 PB |
7352 | |
7353 | evernaux = ((Elf_External_Vernaux *) | |
7354 | ((bfd_byte *) everneed + iverneed->vn_aux)); | |
7355 | ivernaux = iverneed->vn_auxptr; | |
7356 | for (j = 0; j < iverneed->vn_cnt; j++, ivernaux++) | |
7357 | { | |
7358 | _bfd_elf_swap_vernaux_in (abfd, evernaux, ivernaux); | |
7359 | ||
7360 | ivernaux->vna_nodename = | |
7361 | bfd_elf_string_from_elf_section (abfd, hdr->sh_link, | |
7362 | ivernaux->vna_name); | |
7363 | if (ivernaux->vna_nodename == NULL) | |
601a03ba | 7364 | goto error_return_bad_verref; |
fc0e6df6 PB |
7365 | |
7366 | if (j + 1 < iverneed->vn_cnt) | |
7367 | ivernaux->vna_nextptr = ivernaux + 1; | |
7368 | else | |
7369 | ivernaux->vna_nextptr = NULL; | |
7370 | ||
d0fb9a8d JJ |
7371 | if (ivernaux->vna_next |
7372 | > (size_t) (contents_end - (bfd_byte *) evernaux)) | |
601a03ba | 7373 | goto error_return_bad_verref; |
d0fb9a8d | 7374 | |
fc0e6df6 PB |
7375 | evernaux = ((Elf_External_Vernaux *) |
7376 | ((bfd_byte *) evernaux + ivernaux->vna_next)); | |
7377 | ||
7378 | if (ivernaux->vna_other > freeidx) | |
7379 | freeidx = ivernaux->vna_other; | |
7380 | } | |
7381 | ||
7382 | if (i + 1 < hdr->sh_info) | |
7383 | iverneed->vn_nextref = iverneed + 1; | |
7384 | else | |
7385 | iverneed->vn_nextref = NULL; | |
7386 | ||
d0fb9a8d JJ |
7387 | if (iverneed->vn_next |
7388 | > (size_t) (contents_end - (bfd_byte *) everneed)) | |
601a03ba | 7389 | goto error_return_bad_verref; |
d0fb9a8d | 7390 | |
fc0e6df6 PB |
7391 | everneed = ((Elf_External_Verneed *) |
7392 | ((bfd_byte *) everneed + iverneed->vn_next)); | |
7393 | } | |
7394 | ||
7395 | free (contents); | |
7396 | contents = NULL; | |
7397 | } | |
252b5132 RH |
7398 | |
7399 | if (elf_dynverdef (abfd) != 0) | |
7400 | { | |
7401 | Elf_Internal_Shdr *hdr; | |
7402 | Elf_External_Verdef *everdef; | |
7403 | Elf_Internal_Verdef *iverdef; | |
f631889e UD |
7404 | Elf_Internal_Verdef *iverdefarr; |
7405 | Elf_Internal_Verdef iverdefmem; | |
252b5132 | 7406 | unsigned int i; |
062e2358 | 7407 | unsigned int maxidx; |
d0fb9a8d | 7408 | bfd_byte *contents_end_def, *contents_end_aux; |
252b5132 RH |
7409 | |
7410 | hdr = &elf_tdata (abfd)->dynverdef_hdr; | |
7411 | ||
601a03ba AM |
7412 | if (hdr->sh_info == 0 || hdr->sh_size < sizeof (Elf_External_Verdef)) |
7413 | { | |
7414 | error_return_bad_verdef: | |
7415 | (*_bfd_error_handler) | |
7416 | (_("%B: .gnu.version_d invalid entry"), abfd); | |
7417 | bfd_set_error (bfd_error_bad_value); | |
7418 | error_return_verdef: | |
7419 | elf_tdata (abfd)->verdef = NULL; | |
7420 | elf_tdata (abfd)->cverdefs = 0; | |
7421 | goto error_return; | |
7422 | } | |
7423 | ||
a50b1753 | 7424 | contents = (bfd_byte *) bfd_malloc (hdr->sh_size); |
252b5132 | 7425 | if (contents == NULL) |
601a03ba | 7426 | goto error_return_verdef; |
252b5132 | 7427 | if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0 |
217aa764 | 7428 | || bfd_bread (contents, hdr->sh_size, abfd) != hdr->sh_size) |
601a03ba | 7429 | goto error_return_verdef; |
d0fb9a8d JJ |
7430 | |
7431 | BFD_ASSERT (sizeof (Elf_External_Verdef) | |
7432 | >= sizeof (Elf_External_Verdaux)); | |
7433 | contents_end_def = contents + hdr->sh_size | |
7434 | - sizeof (Elf_External_Verdef); | |
7435 | contents_end_aux = contents + hdr->sh_size | |
7436 | - sizeof (Elf_External_Verdaux); | |
7437 | ||
f631889e UD |
7438 | /* We know the number of entries in the section but not the maximum |
7439 | index. Therefore we have to run through all entries and find | |
7440 | the maximum. */ | |
252b5132 | 7441 | everdef = (Elf_External_Verdef *) contents; |
f631889e UD |
7442 | maxidx = 0; |
7443 | for (i = 0; i < hdr->sh_info; ++i) | |
7444 | { | |
7445 | _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem); | |
7446 | ||
601a03ba AM |
7447 | if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) == 0) |
7448 | goto error_return_bad_verdef; | |
062e2358 AM |
7449 | if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) > maxidx) |
7450 | maxidx = iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION); | |
f631889e | 7451 | |
d0fb9a8d JJ |
7452 | if (iverdefmem.vd_next |
7453 | > (size_t) (contents_end_def - (bfd_byte *) everdef)) | |
601a03ba | 7454 | goto error_return_bad_verdef; |
d0fb9a8d | 7455 | |
f631889e UD |
7456 | everdef = ((Elf_External_Verdef *) |
7457 | ((bfd_byte *) everdef + iverdefmem.vd_next)); | |
7458 | } | |
7459 | ||
fc0e6df6 PB |
7460 | if (default_imported_symver) |
7461 | { | |
7462 | if (freeidx > maxidx) | |
7463 | maxidx = ++freeidx; | |
7464 | else | |
7465 | freeidx = ++maxidx; | |
7466 | } | |
201159ec | 7467 | |
601a03ba AM |
7468 | elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *) |
7469 | bfd_zalloc2 (abfd, maxidx, sizeof (Elf_Internal_Verdef)); | |
f631889e | 7470 | if (elf_tdata (abfd)->verdef == NULL) |
601a03ba | 7471 | goto error_return_verdef; |
f631889e UD |
7472 | |
7473 | elf_tdata (abfd)->cverdefs = maxidx; | |
7474 | ||
7475 | everdef = (Elf_External_Verdef *) contents; | |
7476 | iverdefarr = elf_tdata (abfd)->verdef; | |
7477 | for (i = 0; i < hdr->sh_info; i++) | |
252b5132 RH |
7478 | { |
7479 | Elf_External_Verdaux *everdaux; | |
7480 | Elf_Internal_Verdaux *iverdaux; | |
7481 | unsigned int j; | |
7482 | ||
f631889e UD |
7483 | _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem); |
7484 | ||
d0fb9a8d | 7485 | if ((iverdefmem.vd_ndx & VERSYM_VERSION) == 0) |
601a03ba | 7486 | goto error_return_bad_verdef; |
d0fb9a8d | 7487 | |
f631889e UD |
7488 | iverdef = &iverdefarr[(iverdefmem.vd_ndx & VERSYM_VERSION) - 1]; |
7489 | memcpy (iverdef, &iverdefmem, sizeof (Elf_Internal_Verdef)); | |
252b5132 RH |
7490 | |
7491 | iverdef->vd_bfd = abfd; | |
7492 | ||
d0fb9a8d JJ |
7493 | if (iverdef->vd_cnt == 0) |
7494 | iverdef->vd_auxptr = NULL; | |
7495 | else | |
7496 | { | |
a50b1753 NC |
7497 | iverdef->vd_auxptr = (struct elf_internal_verdaux *) |
7498 | bfd_alloc2 (abfd, iverdef->vd_cnt, | |
7499 | sizeof (Elf_Internal_Verdaux)); | |
d0fb9a8d JJ |
7500 | if (iverdef->vd_auxptr == NULL) |
7501 | goto error_return_verdef; | |
7502 | } | |
7503 | ||
7504 | if (iverdef->vd_aux | |
7505 | > (size_t) (contents_end_aux - (bfd_byte *) everdef)) | |
601a03ba | 7506 | goto error_return_bad_verdef; |
252b5132 RH |
7507 | |
7508 | everdaux = ((Elf_External_Verdaux *) | |
7509 | ((bfd_byte *) everdef + iverdef->vd_aux)); | |
7510 | iverdaux = iverdef->vd_auxptr; | |
7511 | for (j = 0; j < iverdef->vd_cnt; j++, iverdaux++) | |
7512 | { | |
7513 | _bfd_elf_swap_verdaux_in (abfd, everdaux, iverdaux); | |
7514 | ||
7515 | iverdaux->vda_nodename = | |
7516 | bfd_elf_string_from_elf_section (abfd, hdr->sh_link, | |
7517 | iverdaux->vda_name); | |
7518 | if (iverdaux->vda_nodename == NULL) | |
601a03ba | 7519 | goto error_return_bad_verdef; |
252b5132 RH |
7520 | |
7521 | if (j + 1 < iverdef->vd_cnt) | |
7522 | iverdaux->vda_nextptr = iverdaux + 1; | |
7523 | else | |
7524 | iverdaux->vda_nextptr = NULL; | |
7525 | ||
d0fb9a8d JJ |
7526 | if (iverdaux->vda_next |
7527 | > (size_t) (contents_end_aux - (bfd_byte *) everdaux)) | |
601a03ba | 7528 | goto error_return_bad_verdef; |
d0fb9a8d | 7529 | |
252b5132 RH |
7530 | everdaux = ((Elf_External_Verdaux *) |
7531 | ((bfd_byte *) everdaux + iverdaux->vda_next)); | |
7532 | } | |
7533 | ||
d0fb9a8d JJ |
7534 | if (iverdef->vd_cnt) |
7535 | iverdef->vd_nodename = iverdef->vd_auxptr->vda_nodename; | |
252b5132 | 7536 | |
d0fb9a8d | 7537 | if ((size_t) (iverdef - iverdefarr) + 1 < maxidx) |
252b5132 RH |
7538 | iverdef->vd_nextdef = iverdef + 1; |
7539 | else | |
7540 | iverdef->vd_nextdef = NULL; | |
7541 | ||
7542 | everdef = ((Elf_External_Verdef *) | |
7543 | ((bfd_byte *) everdef + iverdef->vd_next)); | |
7544 | } | |
7545 | ||
7546 | free (contents); | |
7547 | contents = NULL; | |
7548 | } | |
fc0e6df6 | 7549 | else if (default_imported_symver) |
252b5132 | 7550 | { |
fc0e6df6 PB |
7551 | if (freeidx < 3) |
7552 | freeidx = 3; | |
7553 | else | |
7554 | freeidx++; | |
252b5132 | 7555 | |
a50b1753 NC |
7556 | elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *) |
7557 | bfd_zalloc2 (abfd, freeidx, sizeof (Elf_Internal_Verdef)); | |
fc0e6df6 | 7558 | if (elf_tdata (abfd)->verdef == NULL) |
252b5132 RH |
7559 | goto error_return; |
7560 | ||
fc0e6df6 PB |
7561 | elf_tdata (abfd)->cverdefs = freeidx; |
7562 | } | |
252b5132 | 7563 | |
fc0e6df6 PB |
7564 | /* Create a default version based on the soname. */ |
7565 | if (default_imported_symver) | |
7566 | { | |
7567 | Elf_Internal_Verdef *iverdef; | |
7568 | Elf_Internal_Verdaux *iverdaux; | |
252b5132 | 7569 | |
5bb3703f | 7570 | iverdef = &elf_tdata (abfd)->verdef[freeidx - 1]; |
252b5132 | 7571 | |
fc0e6df6 PB |
7572 | iverdef->vd_version = VER_DEF_CURRENT; |
7573 | iverdef->vd_flags = 0; | |
7574 | iverdef->vd_ndx = freeidx; | |
7575 | iverdef->vd_cnt = 1; | |
252b5132 | 7576 | |
fc0e6df6 | 7577 | iverdef->vd_bfd = abfd; |
252b5132 | 7578 | |
fc0e6df6 PB |
7579 | iverdef->vd_nodename = bfd_elf_get_dt_soname (abfd); |
7580 | if (iverdef->vd_nodename == NULL) | |
d0fb9a8d | 7581 | goto error_return_verdef; |
fc0e6df6 | 7582 | iverdef->vd_nextdef = NULL; |
601a03ba AM |
7583 | iverdef->vd_auxptr = ((struct elf_internal_verdaux *) |
7584 | bfd_zalloc (abfd, sizeof (Elf_Internal_Verdaux))); | |
d0fb9a8d JJ |
7585 | if (iverdef->vd_auxptr == NULL) |
7586 | goto error_return_verdef; | |
252b5132 | 7587 | |
fc0e6df6 PB |
7588 | iverdaux = iverdef->vd_auxptr; |
7589 | iverdaux->vda_nodename = iverdef->vd_nodename; | |
252b5132 RH |
7590 | } |
7591 | ||
b34976b6 | 7592 | return TRUE; |
252b5132 RH |
7593 | |
7594 | error_return: | |
5ed6aba4 | 7595 | if (contents != NULL) |
252b5132 | 7596 | free (contents); |
b34976b6 | 7597 | return FALSE; |
252b5132 RH |
7598 | } |
7599 | \f | |
7600 | asymbol * | |
217aa764 | 7601 | _bfd_elf_make_empty_symbol (bfd *abfd) |
252b5132 RH |
7602 | { |
7603 | elf_symbol_type *newsym; | |
7604 | ||
201159ec | 7605 | newsym = (elf_symbol_type *) bfd_zalloc (abfd, sizeof * newsym); |
252b5132 RH |
7606 | if (!newsym) |
7607 | return NULL; | |
201159ec NC |
7608 | newsym->symbol.the_bfd = abfd; |
7609 | return &newsym->symbol; | |
252b5132 RH |
7610 | } |
7611 | ||
7612 | void | |
217aa764 AM |
7613 | _bfd_elf_get_symbol_info (bfd *abfd ATTRIBUTE_UNUSED, |
7614 | asymbol *symbol, | |
7615 | symbol_info *ret) | |
252b5132 RH |
7616 | { |
7617 | bfd_symbol_info (symbol, ret); | |
7618 | } | |
7619 | ||
7620 | /* Return whether a symbol name implies a local symbol. Most targets | |
7621 | use this function for the is_local_label_name entry point, but some | |
7622 | override it. */ | |
7623 | ||
b34976b6 | 7624 | bfd_boolean |
217aa764 AM |
7625 | _bfd_elf_is_local_label_name (bfd *abfd ATTRIBUTE_UNUSED, |
7626 | const char *name) | |
252b5132 RH |
7627 | { |
7628 | /* Normal local symbols start with ``.L''. */ | |
7629 | if (name[0] == '.' && name[1] == 'L') | |
b34976b6 | 7630 | return TRUE; |
252b5132 RH |
7631 | |
7632 | /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate | |
7633 | DWARF debugging symbols starting with ``..''. */ | |
7634 | if (name[0] == '.' && name[1] == '.') | |
b34976b6 | 7635 | return TRUE; |
252b5132 RH |
7636 | |
7637 | /* gcc will sometimes generate symbols beginning with ``_.L_'' when | |
7638 | emitting DWARF debugging output. I suspect this is actually a | |
7639 | small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call | |
7640 | ASM_GENERATE_INTERNAL_LABEL, and this causes the leading | |
7641 | underscore to be emitted on some ELF targets). For ease of use, | |
7642 | we treat such symbols as local. */ | |
7643 | if (name[0] == '_' && name[1] == '.' && name[2] == 'L' && name[3] == '_') | |
b34976b6 | 7644 | return TRUE; |
252b5132 | 7645 | |
b34976b6 | 7646 | return FALSE; |
252b5132 RH |
7647 | } |
7648 | ||
7649 | alent * | |
217aa764 AM |
7650 | _bfd_elf_get_lineno (bfd *abfd ATTRIBUTE_UNUSED, |
7651 | asymbol *symbol ATTRIBUTE_UNUSED) | |
252b5132 RH |
7652 | { |
7653 | abort (); | |
7654 | return NULL; | |
7655 | } | |
7656 | ||
b34976b6 | 7657 | bfd_boolean |
217aa764 AM |
7658 | _bfd_elf_set_arch_mach (bfd *abfd, |
7659 | enum bfd_architecture arch, | |
7660 | unsigned long machine) | |
252b5132 RH |
7661 | { |
7662 | /* If this isn't the right architecture for this backend, and this | |
7663 | isn't the generic backend, fail. */ | |
7664 | if (arch != get_elf_backend_data (abfd)->arch | |
7665 | && arch != bfd_arch_unknown | |
7666 | && get_elf_backend_data (abfd)->arch != bfd_arch_unknown) | |
b34976b6 | 7667 | return FALSE; |
252b5132 RH |
7668 | |
7669 | return bfd_default_set_arch_mach (abfd, arch, machine); | |
7670 | } | |
7671 | ||
d1fad7c6 NC |
7672 | /* Find the function to a particular section and offset, |
7673 | for error reporting. */ | |
252b5132 | 7674 | |
b34976b6 | 7675 | static bfd_boolean |
b9d678e0 | 7676 | elf_find_function (bfd *abfd, |
217aa764 | 7677 | asymbol **symbols, |
fb167eb2 | 7678 | asection *section, |
217aa764 AM |
7679 | bfd_vma offset, |
7680 | const char **filename_ptr, | |
7681 | const char **functionname_ptr) | |
252b5132 | 7682 | { |
619a703e AM |
7683 | struct elf_find_function_cache |
7684 | { | |
7685 | asection *last_section; | |
7686 | asymbol *func; | |
7687 | const char *filename; | |
7688 | bfd_size_type func_size; | |
7689 | } *cache; | |
252b5132 | 7690 | |
a06c7d5a NC |
7691 | if (symbols == NULL) |
7692 | return FALSE; | |
7693 | ||
619a703e AM |
7694 | cache = elf_tdata (abfd)->elf_find_function_cache; |
7695 | if (cache == NULL) | |
7696 | { | |
7697 | cache = bfd_zalloc (abfd, sizeof (*cache)); | |
7698 | elf_tdata (abfd)->elf_find_function_cache = cache; | |
7699 | if (cache == NULL) | |
7700 | return FALSE; | |
7701 | } | |
7702 | if (cache->last_section != section | |
7703 | || cache->func == NULL | |
7704 | || offset < cache->func->value | |
7705 | || offset >= cache->func->value + cache->func_size) | |
aef36ac1 AM |
7706 | { |
7707 | asymbol *file; | |
7708 | bfd_vma low_func; | |
7709 | asymbol **p; | |
7710 | /* ??? Given multiple file symbols, it is impossible to reliably | |
7711 | choose the right file name for global symbols. File symbols are | |
7712 | local symbols, and thus all file symbols must sort before any | |
7713 | global symbols. The ELF spec may be interpreted to say that a | |
7714 | file symbol must sort before other local symbols, but currently | |
7715 | ld -r doesn't do this. So, for ld -r output, it is possible to | |
7716 | make a better choice of file name for local symbols by ignoring | |
7717 | file symbols appearing after a given local symbol. */ | |
7718 | enum { nothing_seen, symbol_seen, file_after_symbol_seen } state; | |
7719 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
7720 | ||
aef36ac1 AM |
7721 | file = NULL; |
7722 | low_func = 0; | |
7723 | state = nothing_seen; | |
619a703e AM |
7724 | cache->filename = NULL; |
7725 | cache->func = NULL; | |
7726 | cache->func_size = 0; | |
7727 | cache->last_section = section; | |
aef36ac1 AM |
7728 | |
7729 | for (p = symbols; *p != NULL; p++) | |
7730 | { | |
7731 | asymbol *sym = *p; | |
7732 | bfd_vma code_off; | |
7733 | bfd_size_type size; | |
7734 | ||
7735 | if ((sym->flags & BSF_FILE) != 0) | |
7736 | { | |
7737 | file = sym; | |
7738 | if (state == symbol_seen) | |
7739 | state = file_after_symbol_seen; | |
7740 | continue; | |
7741 | } | |
ff9e0f5b | 7742 | |
aef36ac1 AM |
7743 | size = bed->maybe_function_sym (sym, section, &code_off); |
7744 | if (size != 0 | |
7745 | && code_off <= offset | |
7746 | && (code_off > low_func | |
7747 | || (code_off == low_func | |
619a703e | 7748 | && size > cache->func_size))) |
aef36ac1 | 7749 | { |
619a703e AM |
7750 | cache->func = sym; |
7751 | cache->func_size = size; | |
7752 | cache->filename = NULL; | |
aef36ac1 | 7753 | low_func = code_off; |
aef36ac1 AM |
7754 | if (file != NULL |
7755 | && ((sym->flags & BSF_LOCAL) != 0 | |
7756 | || state != file_after_symbol_seen)) | |
619a703e | 7757 | cache->filename = bfd_asymbol_name (file); |
aef36ac1 AM |
7758 | } |
7759 | if (state == nothing_seen) | |
7760 | state = symbol_seen; | |
252b5132 RH |
7761 | } |
7762 | } | |
7763 | ||
619a703e | 7764 | if (cache->func == NULL) |
b34976b6 | 7765 | return FALSE; |
252b5132 | 7766 | |
d1fad7c6 | 7767 | if (filename_ptr) |
619a703e | 7768 | *filename_ptr = cache->filename; |
d1fad7c6 | 7769 | if (functionname_ptr) |
619a703e | 7770 | *functionname_ptr = bfd_asymbol_name (cache->func); |
d1fad7c6 | 7771 | |
b34976b6 | 7772 | return TRUE; |
d1fad7c6 NC |
7773 | } |
7774 | ||
7775 | /* Find the nearest line to a particular section and offset, | |
7776 | for error reporting. */ | |
7777 | ||
b34976b6 | 7778 | bfd_boolean |
217aa764 | 7779 | _bfd_elf_find_nearest_line (bfd *abfd, |
217aa764 | 7780 | asymbol **symbols, |
fb167eb2 | 7781 | asection *section, |
217aa764 AM |
7782 | bfd_vma offset, |
7783 | const char **filename_ptr, | |
7784 | const char **functionname_ptr, | |
fb167eb2 AM |
7785 | unsigned int *line_ptr, |
7786 | unsigned int *discriminator_ptr) | |
d1fad7c6 | 7787 | { |
b34976b6 | 7788 | bfd_boolean found; |
d1fad7c6 | 7789 | |
fb167eb2 | 7790 | if (_bfd_dwarf2_find_nearest_line (abfd, symbols, NULL, section, offset, |
4e8a9624 | 7791 | filename_ptr, functionname_ptr, |
fb167eb2 AM |
7792 | line_ptr, discriminator_ptr, |
7793 | dwarf_debug_sections, 0, | |
7794 | &elf_tdata (abfd)->dwarf2_find_line_info)) | |
d1fad7c6 NC |
7795 | { |
7796 | if (!*functionname_ptr) | |
fb167eb2 | 7797 | elf_find_function (abfd, symbols, section, offset, |
4e8a9624 AM |
7798 | *filename_ptr ? NULL : filename_ptr, |
7799 | functionname_ptr); | |
7800 | ||
b34976b6 | 7801 | return TRUE; |
d1fad7c6 NC |
7802 | } |
7803 | ||
fb167eb2 AM |
7804 | if (_bfd_dwarf1_find_nearest_line (abfd, symbols, section, offset, |
7805 | filename_ptr, functionname_ptr, line_ptr)) | |
d1fad7c6 NC |
7806 | { |
7807 | if (!*functionname_ptr) | |
fb167eb2 | 7808 | elf_find_function (abfd, symbols, section, offset, |
4e8a9624 AM |
7809 | *filename_ptr ? NULL : filename_ptr, |
7810 | functionname_ptr); | |
7811 | ||
b34976b6 | 7812 | return TRUE; |
d1fad7c6 NC |
7813 | } |
7814 | ||
7815 | if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset, | |
4e8a9624 AM |
7816 | &found, filename_ptr, |
7817 | functionname_ptr, line_ptr, | |
7818 | &elf_tdata (abfd)->line_info)) | |
b34976b6 | 7819 | return FALSE; |
dc43ada5 | 7820 | if (found && (*functionname_ptr || *line_ptr)) |
b34976b6 | 7821 | return TRUE; |
d1fad7c6 NC |
7822 | |
7823 | if (symbols == NULL) | |
b34976b6 | 7824 | return FALSE; |
d1fad7c6 | 7825 | |
fb167eb2 | 7826 | if (! elf_find_function (abfd, symbols, section, offset, |
4e8a9624 | 7827 | filename_ptr, functionname_ptr)) |
b34976b6 | 7828 | return FALSE; |
d1fad7c6 | 7829 | |
252b5132 | 7830 | *line_ptr = 0; |
b34976b6 | 7831 | return TRUE; |
252b5132 RH |
7832 | } |
7833 | ||
5420f73d L |
7834 | /* Find the line for a symbol. */ |
7835 | ||
7836 | bfd_boolean | |
7837 | _bfd_elf_find_line (bfd *abfd, asymbol **symbols, asymbol *symbol, | |
7838 | const char **filename_ptr, unsigned int *line_ptr) | |
9b8d1a36 | 7839 | { |
fb167eb2 AM |
7840 | return _bfd_dwarf2_find_nearest_line (abfd, symbols, symbol, NULL, 0, |
7841 | filename_ptr, NULL, line_ptr, NULL, | |
7842 | dwarf_debug_sections, 0, | |
7843 | &elf_tdata (abfd)->dwarf2_find_line_info); | |
5420f73d L |
7844 | } |
7845 | ||
4ab527b0 FF |
7846 | /* After a call to bfd_find_nearest_line, successive calls to |
7847 | bfd_find_inliner_info can be used to get source information about | |
7848 | each level of function inlining that terminated at the address | |
7849 | passed to bfd_find_nearest_line. Currently this is only supported | |
7850 | for DWARF2 with appropriate DWARF3 extensions. */ | |
7851 | ||
7852 | bfd_boolean | |
7853 | _bfd_elf_find_inliner_info (bfd *abfd, | |
7854 | const char **filename_ptr, | |
7855 | const char **functionname_ptr, | |
7856 | unsigned int *line_ptr) | |
7857 | { | |
7858 | bfd_boolean found; | |
7859 | found = _bfd_dwarf2_find_inliner_info (abfd, filename_ptr, | |
7860 | functionname_ptr, line_ptr, | |
7861 | & elf_tdata (abfd)->dwarf2_find_line_info); | |
7862 | return found; | |
7863 | } | |
7864 | ||
252b5132 | 7865 | int |
a6b96beb | 7866 | _bfd_elf_sizeof_headers (bfd *abfd, struct bfd_link_info *info) |
252b5132 | 7867 | { |
8ded5a0f AM |
7868 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
7869 | int ret = bed->s->sizeof_ehdr; | |
252b5132 | 7870 | |
a6b96beb | 7871 | if (!info->relocatable) |
8ded5a0f | 7872 | { |
12bd6957 | 7873 | bfd_size_type phdr_size = elf_program_header_size (abfd); |
8ded5a0f | 7874 | |
62d7a5f6 AM |
7875 | if (phdr_size == (bfd_size_type) -1) |
7876 | { | |
7877 | struct elf_segment_map *m; | |
7878 | ||
7879 | phdr_size = 0; | |
12bd6957 | 7880 | for (m = elf_seg_map (abfd); m != NULL; m = m->next) |
62d7a5f6 | 7881 | phdr_size += bed->s->sizeof_phdr; |
8ded5a0f | 7882 | |
62d7a5f6 AM |
7883 | if (phdr_size == 0) |
7884 | phdr_size = get_program_header_size (abfd, info); | |
7885 | } | |
8ded5a0f | 7886 | |
12bd6957 | 7887 | elf_program_header_size (abfd) = phdr_size; |
8ded5a0f AM |
7888 | ret += phdr_size; |
7889 | } | |
7890 | ||
252b5132 RH |
7891 | return ret; |
7892 | } | |
7893 | ||
b34976b6 | 7894 | bfd_boolean |
217aa764 AM |
7895 | _bfd_elf_set_section_contents (bfd *abfd, |
7896 | sec_ptr section, | |
0f867abe | 7897 | const void *location, |
217aa764 AM |
7898 | file_ptr offset, |
7899 | bfd_size_type count) | |
252b5132 RH |
7900 | { |
7901 | Elf_Internal_Shdr *hdr; | |
1b6aeedb | 7902 | file_ptr pos; |
252b5132 RH |
7903 | |
7904 | if (! abfd->output_has_begun | |
217aa764 | 7905 | && ! _bfd_elf_compute_section_file_positions (abfd, NULL)) |
b34976b6 | 7906 | return FALSE; |
252b5132 RH |
7907 | |
7908 | hdr = &elf_section_data (section)->this_hdr; | |
dc810e39 AM |
7909 | pos = hdr->sh_offset + offset; |
7910 | if (bfd_seek (abfd, pos, SEEK_SET) != 0 | |
7911 | || bfd_bwrite (location, count, abfd) != count) | |
b34976b6 | 7912 | return FALSE; |
252b5132 | 7913 | |
b34976b6 | 7914 | return TRUE; |
252b5132 RH |
7915 | } |
7916 | ||
7917 | void | |
217aa764 AM |
7918 | _bfd_elf_no_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED, |
7919 | arelent *cache_ptr ATTRIBUTE_UNUSED, | |
7920 | Elf_Internal_Rela *dst ATTRIBUTE_UNUSED) | |
252b5132 RH |
7921 | { |
7922 | abort (); | |
7923 | } | |
7924 | ||
252b5132 RH |
7925 | /* Try to convert a non-ELF reloc into an ELF one. */ |
7926 | ||
b34976b6 | 7927 | bfd_boolean |
217aa764 | 7928 | _bfd_elf_validate_reloc (bfd *abfd, arelent *areloc) |
252b5132 | 7929 | { |
c044fabd | 7930 | /* Check whether we really have an ELF howto. */ |
252b5132 RH |
7931 | |
7932 | if ((*areloc->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec) | |
7933 | { | |
7934 | bfd_reloc_code_real_type code; | |
7935 | reloc_howto_type *howto; | |
7936 | ||
7937 | /* Alien reloc: Try to determine its type to replace it with an | |
c044fabd | 7938 | equivalent ELF reloc. */ |
252b5132 RH |
7939 | |
7940 | if (areloc->howto->pc_relative) | |
7941 | { | |
7942 | switch (areloc->howto->bitsize) | |
7943 | { | |
7944 | case 8: | |
7945 | code = BFD_RELOC_8_PCREL; | |
7946 | break; | |
7947 | case 12: | |
7948 | code = BFD_RELOC_12_PCREL; | |
7949 | break; | |
7950 | case 16: | |
7951 | code = BFD_RELOC_16_PCREL; | |
7952 | break; | |
7953 | case 24: | |
7954 | code = BFD_RELOC_24_PCREL; | |
7955 | break; | |
7956 | case 32: | |
7957 | code = BFD_RELOC_32_PCREL; | |
7958 | break; | |
7959 | case 64: | |
7960 | code = BFD_RELOC_64_PCREL; | |
7961 | break; | |
7962 | default: | |
7963 | goto fail; | |
7964 | } | |
7965 | ||
7966 | howto = bfd_reloc_type_lookup (abfd, code); | |
7967 | ||
7968 | if (areloc->howto->pcrel_offset != howto->pcrel_offset) | |
7969 | { | |
7970 | if (howto->pcrel_offset) | |
7971 | areloc->addend += areloc->address; | |
7972 | else | |
7973 | areloc->addend -= areloc->address; /* addend is unsigned!! */ | |
7974 | } | |
7975 | } | |
7976 | else | |
7977 | { | |
7978 | switch (areloc->howto->bitsize) | |
7979 | { | |
7980 | case 8: | |
7981 | code = BFD_RELOC_8; | |
7982 | break; | |
7983 | case 14: | |
7984 | code = BFD_RELOC_14; | |
7985 | break; | |
7986 | case 16: | |
7987 | code = BFD_RELOC_16; | |
7988 | break; | |
7989 | case 26: | |
7990 | code = BFD_RELOC_26; | |
7991 | break; | |
7992 | case 32: | |
7993 | code = BFD_RELOC_32; | |
7994 | break; | |
7995 | case 64: | |
7996 | code = BFD_RELOC_64; | |
7997 | break; | |
7998 | default: | |
7999 | goto fail; | |
8000 | } | |
8001 | ||
8002 | howto = bfd_reloc_type_lookup (abfd, code); | |
8003 | } | |
8004 | ||
8005 | if (howto) | |
8006 | areloc->howto = howto; | |
8007 | else | |
8008 | goto fail; | |
8009 | } | |
8010 | ||
b34976b6 | 8011 | return TRUE; |
252b5132 RH |
8012 | |
8013 | fail: | |
8014 | (*_bfd_error_handler) | |
d003868e AM |
8015 | (_("%B: unsupported relocation type %s"), |
8016 | abfd, areloc->howto->name); | |
252b5132 | 8017 | bfd_set_error (bfd_error_bad_value); |
b34976b6 | 8018 | return FALSE; |
252b5132 RH |
8019 | } |
8020 | ||
b34976b6 | 8021 | bfd_boolean |
217aa764 | 8022 | _bfd_elf_close_and_cleanup (bfd *abfd) |
252b5132 | 8023 | { |
d9071b0c TG |
8024 | struct elf_obj_tdata *tdata = elf_tdata (abfd); |
8025 | if (bfd_get_format (abfd) == bfd_object && tdata != NULL) | |
252b5132 | 8026 | { |
c0355132 | 8027 | if (elf_tdata (abfd)->o != NULL && elf_shstrtab (abfd) != NULL) |
2b0f7ef9 | 8028 | _bfd_elf_strtab_free (elf_shstrtab (abfd)); |
d9071b0c | 8029 | _bfd_dwarf2_cleanup_debug_info (abfd, &tdata->dwarf2_find_line_info); |
252b5132 RH |
8030 | } |
8031 | ||
8032 | return _bfd_generic_close_and_cleanup (abfd); | |
8033 | } | |
8034 | ||
8035 | /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY | |
8036 | in the relocation's offset. Thus we cannot allow any sort of sanity | |
8037 | range-checking to interfere. There is nothing else to do in processing | |
8038 | this reloc. */ | |
8039 | ||
8040 | bfd_reloc_status_type | |
217aa764 AM |
8041 | _bfd_elf_rel_vtable_reloc_fn |
8042 | (bfd *abfd ATTRIBUTE_UNUSED, arelent *re ATTRIBUTE_UNUSED, | |
fc0a2244 | 8043 | struct bfd_symbol *symbol ATTRIBUTE_UNUSED, |
217aa764 AM |
8044 | void *data ATTRIBUTE_UNUSED, asection *is ATTRIBUTE_UNUSED, |
8045 | bfd *obfd ATTRIBUTE_UNUSED, char **errmsg ATTRIBUTE_UNUSED) | |
252b5132 RH |
8046 | { |
8047 | return bfd_reloc_ok; | |
8048 | } | |
252b5132 RH |
8049 | \f |
8050 | /* Elf core file support. Much of this only works on native | |
8051 | toolchains, since we rely on knowing the | |
8052 | machine-dependent procfs structure in order to pick | |
c044fabd | 8053 | out details about the corefile. */ |
252b5132 RH |
8054 | |
8055 | #ifdef HAVE_SYS_PROCFS_H | |
16231b7b DG |
8056 | /* Needed for new procfs interface on sparc-solaris. */ |
8057 | # define _STRUCTURED_PROC 1 | |
252b5132 RH |
8058 | # include <sys/procfs.h> |
8059 | #endif | |
8060 | ||
261b8d08 PA |
8061 | /* Return a PID that identifies a "thread" for threaded cores, or the |
8062 | PID of the main process for non-threaded cores. */ | |
252b5132 RH |
8063 | |
8064 | static int | |
217aa764 | 8065 | elfcore_make_pid (bfd *abfd) |
252b5132 | 8066 | { |
261b8d08 PA |
8067 | int pid; |
8068 | ||
228e534f | 8069 | pid = elf_tdata (abfd)->core->lwpid; |
261b8d08 | 8070 | if (pid == 0) |
228e534f | 8071 | pid = elf_tdata (abfd)->core->pid; |
261b8d08 PA |
8072 | |
8073 | return pid; | |
252b5132 RH |
8074 | } |
8075 | ||
252b5132 RH |
8076 | /* If there isn't a section called NAME, make one, using |
8077 | data from SECT. Note, this function will generate a | |
8078 | reference to NAME, so you shouldn't deallocate or | |
c044fabd | 8079 | overwrite it. */ |
252b5132 | 8080 | |
b34976b6 | 8081 | static bfd_boolean |
217aa764 | 8082 | elfcore_maybe_make_sect (bfd *abfd, char *name, asection *sect) |
252b5132 | 8083 | { |
c044fabd | 8084 | asection *sect2; |
252b5132 RH |
8085 | |
8086 | if (bfd_get_section_by_name (abfd, name) != NULL) | |
b34976b6 | 8087 | return TRUE; |
252b5132 | 8088 | |
117ed4f8 | 8089 | sect2 = bfd_make_section_with_flags (abfd, name, sect->flags); |
252b5132 | 8090 | if (sect2 == NULL) |
b34976b6 | 8091 | return FALSE; |
252b5132 | 8092 | |
eea6121a | 8093 | sect2->size = sect->size; |
252b5132 | 8094 | sect2->filepos = sect->filepos; |
252b5132 | 8095 | sect2->alignment_power = sect->alignment_power; |
b34976b6 | 8096 | return TRUE; |
252b5132 RH |
8097 | } |
8098 | ||
bb0082d6 AM |
8099 | /* Create a pseudosection containing SIZE bytes at FILEPOS. This |
8100 | actually creates up to two pseudosections: | |
8101 | - For the single-threaded case, a section named NAME, unless | |
8102 | such a section already exists. | |
8103 | - For the multi-threaded case, a section named "NAME/PID", where | |
8104 | PID is elfcore_make_pid (abfd). | |
8105 | Both pseudosections have identical contents. */ | |
b34976b6 | 8106 | bfd_boolean |
217aa764 AM |
8107 | _bfd_elfcore_make_pseudosection (bfd *abfd, |
8108 | char *name, | |
8109 | size_t size, | |
8110 | ufile_ptr filepos) | |
bb0082d6 AM |
8111 | { |
8112 | char buf[100]; | |
8113 | char *threaded_name; | |
d4c88bbb | 8114 | size_t len; |
bb0082d6 AM |
8115 | asection *sect; |
8116 | ||
8117 | /* Build the section name. */ | |
8118 | ||
8119 | sprintf (buf, "%s/%d", name, elfcore_make_pid (abfd)); | |
d4c88bbb | 8120 | len = strlen (buf) + 1; |
a50b1753 | 8121 | threaded_name = (char *) bfd_alloc (abfd, len); |
bb0082d6 | 8122 | if (threaded_name == NULL) |
b34976b6 | 8123 | return FALSE; |
d4c88bbb | 8124 | memcpy (threaded_name, buf, len); |
bb0082d6 | 8125 | |
117ed4f8 AM |
8126 | sect = bfd_make_section_anyway_with_flags (abfd, threaded_name, |
8127 | SEC_HAS_CONTENTS); | |
bb0082d6 | 8128 | if (sect == NULL) |
b34976b6 | 8129 | return FALSE; |
eea6121a | 8130 | sect->size = size; |
bb0082d6 | 8131 | sect->filepos = filepos; |
bb0082d6 AM |
8132 | sect->alignment_power = 2; |
8133 | ||
936e320b | 8134 | return elfcore_maybe_make_sect (abfd, name, sect); |
bb0082d6 AM |
8135 | } |
8136 | ||
252b5132 | 8137 | /* prstatus_t exists on: |
4a938328 | 8138 | solaris 2.5+ |
252b5132 RH |
8139 | linux 2.[01] + glibc |
8140 | unixware 4.2 | |
8141 | */ | |
8142 | ||
8143 | #if defined (HAVE_PRSTATUS_T) | |
a7b97311 | 8144 | |
b34976b6 | 8145 | static bfd_boolean |
217aa764 | 8146 | elfcore_grok_prstatus (bfd *abfd, Elf_Internal_Note *note) |
252b5132 | 8147 | { |
eea6121a | 8148 | size_t size; |
7ee38065 | 8149 | int offset; |
252b5132 | 8150 | |
4a938328 MS |
8151 | if (note->descsz == sizeof (prstatus_t)) |
8152 | { | |
8153 | prstatus_t prstat; | |
252b5132 | 8154 | |
eea6121a | 8155 | size = sizeof (prstat.pr_reg); |
7ee38065 | 8156 | offset = offsetof (prstatus_t, pr_reg); |
4a938328 | 8157 | memcpy (&prstat, note->descdata, sizeof (prstat)); |
252b5132 | 8158 | |
fa49d224 NC |
8159 | /* Do not overwrite the core signal if it |
8160 | has already been set by another thread. */ | |
228e534f AM |
8161 | if (elf_tdata (abfd)->core->signal == 0) |
8162 | elf_tdata (abfd)->core->signal = prstat.pr_cursig; | |
8163 | if (elf_tdata (abfd)->core->pid == 0) | |
8164 | elf_tdata (abfd)->core->pid = prstat.pr_pid; | |
252b5132 | 8165 | |
4a938328 MS |
8166 | /* pr_who exists on: |
8167 | solaris 2.5+ | |
8168 | unixware 4.2 | |
8169 | pr_who doesn't exist on: | |
8170 | linux 2.[01] | |
8171 | */ | |
252b5132 | 8172 | #if defined (HAVE_PRSTATUS_T_PR_WHO) |
228e534f | 8173 | elf_tdata (abfd)->core->lwpid = prstat.pr_who; |
261b8d08 | 8174 | #else |
228e534f | 8175 | elf_tdata (abfd)->core->lwpid = prstat.pr_pid; |
252b5132 | 8176 | #endif |
4a938328 | 8177 | } |
7ee38065 | 8178 | #if defined (HAVE_PRSTATUS32_T) |
4a938328 MS |
8179 | else if (note->descsz == sizeof (prstatus32_t)) |
8180 | { | |
8181 | /* 64-bit host, 32-bit corefile */ | |
8182 | prstatus32_t prstat; | |
8183 | ||
eea6121a | 8184 | size = sizeof (prstat.pr_reg); |
7ee38065 | 8185 | offset = offsetof (prstatus32_t, pr_reg); |
4a938328 MS |
8186 | memcpy (&prstat, note->descdata, sizeof (prstat)); |
8187 | ||
fa49d224 NC |
8188 | /* Do not overwrite the core signal if it |
8189 | has already been set by another thread. */ | |
228e534f AM |
8190 | if (elf_tdata (abfd)->core->signal == 0) |
8191 | elf_tdata (abfd)->core->signal = prstat.pr_cursig; | |
8192 | if (elf_tdata (abfd)->core->pid == 0) | |
8193 | elf_tdata (abfd)->core->pid = prstat.pr_pid; | |
4a938328 MS |
8194 | |
8195 | /* pr_who exists on: | |
8196 | solaris 2.5+ | |
8197 | unixware 4.2 | |
8198 | pr_who doesn't exist on: | |
8199 | linux 2.[01] | |
8200 | */ | |
7ee38065 | 8201 | #if defined (HAVE_PRSTATUS32_T_PR_WHO) |
228e534f | 8202 | elf_tdata (abfd)->core->lwpid = prstat.pr_who; |
261b8d08 | 8203 | #else |
228e534f | 8204 | elf_tdata (abfd)->core->lwpid = prstat.pr_pid; |
4a938328 MS |
8205 | #endif |
8206 | } | |
7ee38065 | 8207 | #endif /* HAVE_PRSTATUS32_T */ |
4a938328 MS |
8208 | else |
8209 | { | |
8210 | /* Fail - we don't know how to handle any other | |
8211 | note size (ie. data object type). */ | |
b34976b6 | 8212 | return TRUE; |
4a938328 | 8213 | } |
252b5132 | 8214 | |
bb0082d6 | 8215 | /* Make a ".reg/999" section and a ".reg" section. */ |
936e320b | 8216 | return _bfd_elfcore_make_pseudosection (abfd, ".reg", |
eea6121a | 8217 | size, note->descpos + offset); |
252b5132 RH |
8218 | } |
8219 | #endif /* defined (HAVE_PRSTATUS_T) */ | |
8220 | ||
bb0082d6 | 8221 | /* Create a pseudosection containing the exact contents of NOTE. */ |
b34976b6 | 8222 | static bfd_boolean |
217aa764 AM |
8223 | elfcore_make_note_pseudosection (bfd *abfd, |
8224 | char *name, | |
8225 | Elf_Internal_Note *note) | |
252b5132 | 8226 | { |
936e320b AM |
8227 | return _bfd_elfcore_make_pseudosection (abfd, name, |
8228 | note->descsz, note->descpos); | |
252b5132 RH |
8229 | } |
8230 | ||
ff08c6bb JB |
8231 | /* There isn't a consistent prfpregset_t across platforms, |
8232 | but it doesn't matter, because we don't have to pick this | |
c044fabd KH |
8233 | data structure apart. */ |
8234 | ||
b34976b6 | 8235 | static bfd_boolean |
217aa764 | 8236 | elfcore_grok_prfpreg (bfd *abfd, Elf_Internal_Note *note) |
ff08c6bb JB |
8237 | { |
8238 | return elfcore_make_note_pseudosection (abfd, ".reg2", note); | |
8239 | } | |
8240 | ||
ff08c6bb | 8241 | /* Linux dumps the Intel SSE regs in a note named "LINUX" with a note |
971d4640 | 8242 | type of NT_PRXFPREG. Just include the whole note's contents |
ff08c6bb | 8243 | literally. */ |
c044fabd | 8244 | |
b34976b6 | 8245 | static bfd_boolean |
217aa764 | 8246 | elfcore_grok_prxfpreg (bfd *abfd, Elf_Internal_Note *note) |
ff08c6bb JB |
8247 | { |
8248 | return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note); | |
8249 | } | |
8250 | ||
4339cae0 L |
8251 | /* Linux dumps the Intel XSAVE extended state in a note named "LINUX" |
8252 | with a note type of NT_X86_XSTATE. Just include the whole note's | |
8253 | contents literally. */ | |
8254 | ||
8255 | static bfd_boolean | |
8256 | elfcore_grok_xstatereg (bfd *abfd, Elf_Internal_Note *note) | |
8257 | { | |
8258 | return elfcore_make_note_pseudosection (abfd, ".reg-xstate", note); | |
8259 | } | |
8260 | ||
97753bd5 AM |
8261 | static bfd_boolean |
8262 | elfcore_grok_ppc_vmx (bfd *abfd, Elf_Internal_Note *note) | |
8263 | { | |
8264 | return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vmx", note); | |
8265 | } | |
8266 | ||
89eeb0bc LM |
8267 | static bfd_boolean |
8268 | elfcore_grok_ppc_vsx (bfd *abfd, Elf_Internal_Note *note) | |
8269 | { | |
8270 | return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vsx", note); | |
8271 | } | |
97753bd5 | 8272 | |
0675e188 UW |
8273 | static bfd_boolean |
8274 | elfcore_grok_s390_high_gprs (bfd *abfd, Elf_Internal_Note *note) | |
8275 | { | |
8276 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-high-gprs", note); | |
8277 | } | |
8278 | ||
d7eeb400 MS |
8279 | static bfd_boolean |
8280 | elfcore_grok_s390_timer (bfd *abfd, Elf_Internal_Note *note) | |
8281 | { | |
8282 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-timer", note); | |
8283 | } | |
8284 | ||
8285 | static bfd_boolean | |
8286 | elfcore_grok_s390_todcmp (bfd *abfd, Elf_Internal_Note *note) | |
8287 | { | |
8288 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-todcmp", note); | |
8289 | } | |
8290 | ||
8291 | static bfd_boolean | |
8292 | elfcore_grok_s390_todpreg (bfd *abfd, Elf_Internal_Note *note) | |
8293 | { | |
8294 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-todpreg", note); | |
8295 | } | |
8296 | ||
8297 | static bfd_boolean | |
8298 | elfcore_grok_s390_ctrs (bfd *abfd, Elf_Internal_Note *note) | |
8299 | { | |
8300 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-ctrs", note); | |
8301 | } | |
8302 | ||
8303 | static bfd_boolean | |
8304 | elfcore_grok_s390_prefix (bfd *abfd, Elf_Internal_Note *note) | |
8305 | { | |
8306 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-prefix", note); | |
8307 | } | |
8308 | ||
355b81d9 UW |
8309 | static bfd_boolean |
8310 | elfcore_grok_s390_last_break (bfd *abfd, Elf_Internal_Note *note) | |
8311 | { | |
8312 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-last-break", note); | |
8313 | } | |
8314 | ||
8315 | static bfd_boolean | |
8316 | elfcore_grok_s390_system_call (bfd *abfd, Elf_Internal_Note *note) | |
8317 | { | |
8318 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-system-call", note); | |
8319 | } | |
8320 | ||
abb3f6cc NC |
8321 | static bfd_boolean |
8322 | elfcore_grok_s390_tdb (bfd *abfd, Elf_Internal_Note *note) | |
8323 | { | |
8324 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-tdb", note); | |
8325 | } | |
8326 | ||
faa9a424 UW |
8327 | static bfd_boolean |
8328 | elfcore_grok_arm_vfp (bfd *abfd, Elf_Internal_Note *note) | |
8329 | { | |
8330 | return elfcore_make_note_pseudosection (abfd, ".reg-arm-vfp", note); | |
8331 | } | |
8332 | ||
652451f8 YZ |
8333 | static bfd_boolean |
8334 | elfcore_grok_aarch_tls (bfd *abfd, Elf_Internal_Note *note) | |
8335 | { | |
8336 | return elfcore_make_note_pseudosection (abfd, ".reg-aarch-tls", note); | |
8337 | } | |
8338 | ||
8339 | static bfd_boolean | |
8340 | elfcore_grok_aarch_hw_break (bfd *abfd, Elf_Internal_Note *note) | |
8341 | { | |
8342 | return elfcore_make_note_pseudosection (abfd, ".reg-aarch-hw-break", note); | |
8343 | } | |
8344 | ||
8345 | static bfd_boolean | |
8346 | elfcore_grok_aarch_hw_watch (bfd *abfd, Elf_Internal_Note *note) | |
8347 | { | |
8348 | return elfcore_make_note_pseudosection (abfd, ".reg-aarch-hw-watch", note); | |
8349 | } | |
8350 | ||
252b5132 | 8351 | #if defined (HAVE_PRPSINFO_T) |
4a938328 | 8352 | typedef prpsinfo_t elfcore_psinfo_t; |
7ee38065 | 8353 | #if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */ |
4a938328 MS |
8354 | typedef prpsinfo32_t elfcore_psinfo32_t; |
8355 | #endif | |
252b5132 RH |
8356 | #endif |
8357 | ||
8358 | #if defined (HAVE_PSINFO_T) | |
4a938328 | 8359 | typedef psinfo_t elfcore_psinfo_t; |
7ee38065 | 8360 | #if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */ |
4a938328 MS |
8361 | typedef psinfo32_t elfcore_psinfo32_t; |
8362 | #endif | |
252b5132 RH |
8363 | #endif |
8364 | ||
252b5132 RH |
8365 | /* return a malloc'ed copy of a string at START which is at |
8366 | most MAX bytes long, possibly without a terminating '\0'. | |
c044fabd | 8367 | the copy will always have a terminating '\0'. */ |
252b5132 | 8368 | |
936e320b | 8369 | char * |
217aa764 | 8370 | _bfd_elfcore_strndup (bfd *abfd, char *start, size_t max) |
252b5132 | 8371 | { |
dc810e39 | 8372 | char *dups; |
a50b1753 | 8373 | char *end = (char *) memchr (start, '\0', max); |
dc810e39 | 8374 | size_t len; |
252b5132 RH |
8375 | |
8376 | if (end == NULL) | |
8377 | len = max; | |
8378 | else | |
8379 | len = end - start; | |
8380 | ||
a50b1753 | 8381 | dups = (char *) bfd_alloc (abfd, len + 1); |
dc810e39 | 8382 | if (dups == NULL) |
252b5132 RH |
8383 | return NULL; |
8384 | ||
dc810e39 AM |
8385 | memcpy (dups, start, len); |
8386 | dups[len] = '\0'; | |
252b5132 | 8387 | |
dc810e39 | 8388 | return dups; |
252b5132 RH |
8389 | } |
8390 | ||
bb0082d6 | 8391 | #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) |
b34976b6 | 8392 | static bfd_boolean |
217aa764 | 8393 | elfcore_grok_psinfo (bfd *abfd, Elf_Internal_Note *note) |
252b5132 | 8394 | { |
4a938328 MS |
8395 | if (note->descsz == sizeof (elfcore_psinfo_t)) |
8396 | { | |
8397 | elfcore_psinfo_t psinfo; | |
252b5132 | 8398 | |
7ee38065 | 8399 | memcpy (&psinfo, note->descdata, sizeof (psinfo)); |
252b5132 | 8400 | |
335e41d4 | 8401 | #if defined (HAVE_PSINFO_T_PR_PID) || defined (HAVE_PRPSINFO_T_PR_PID) |
228e534f | 8402 | elf_tdata (abfd)->core->pid = psinfo.pr_pid; |
335e41d4 | 8403 | #endif |
228e534f | 8404 | elf_tdata (abfd)->core->program |
936e320b AM |
8405 | = _bfd_elfcore_strndup (abfd, psinfo.pr_fname, |
8406 | sizeof (psinfo.pr_fname)); | |
252b5132 | 8407 | |
228e534f | 8408 | elf_tdata (abfd)->core->command |
936e320b AM |
8409 | = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs, |
8410 | sizeof (psinfo.pr_psargs)); | |
4a938328 | 8411 | } |
7ee38065 | 8412 | #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T) |
4a938328 MS |
8413 | else if (note->descsz == sizeof (elfcore_psinfo32_t)) |
8414 | { | |
8415 | /* 64-bit host, 32-bit corefile */ | |
8416 | elfcore_psinfo32_t psinfo; | |
8417 | ||
7ee38065 | 8418 | memcpy (&psinfo, note->descdata, sizeof (psinfo)); |
252b5132 | 8419 | |
335e41d4 | 8420 | #if defined (HAVE_PSINFO32_T_PR_PID) || defined (HAVE_PRPSINFO32_T_PR_PID) |
228e534f | 8421 | elf_tdata (abfd)->core->pid = psinfo.pr_pid; |
335e41d4 | 8422 | #endif |
228e534f | 8423 | elf_tdata (abfd)->core->program |
936e320b AM |
8424 | = _bfd_elfcore_strndup (abfd, psinfo.pr_fname, |
8425 | sizeof (psinfo.pr_fname)); | |
4a938328 | 8426 | |
228e534f | 8427 | elf_tdata (abfd)->core->command |
936e320b AM |
8428 | = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs, |
8429 | sizeof (psinfo.pr_psargs)); | |
4a938328 MS |
8430 | } |
8431 | #endif | |
8432 | ||
8433 | else | |
8434 | { | |
8435 | /* Fail - we don't know how to handle any other | |
8436 | note size (ie. data object type). */ | |
b34976b6 | 8437 | return TRUE; |
4a938328 | 8438 | } |
252b5132 RH |
8439 | |
8440 | /* Note that for some reason, a spurious space is tacked | |
8441 | onto the end of the args in some (at least one anyway) | |
c044fabd | 8442 | implementations, so strip it off if it exists. */ |
252b5132 RH |
8443 | |
8444 | { | |
228e534f | 8445 | char *command = elf_tdata (abfd)->core->command; |
252b5132 RH |
8446 | int n = strlen (command); |
8447 | ||
8448 | if (0 < n && command[n - 1] == ' ') | |
8449 | command[n - 1] = '\0'; | |
8450 | } | |
8451 | ||
b34976b6 | 8452 | return TRUE; |
252b5132 RH |
8453 | } |
8454 | #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */ | |
8455 | ||
252b5132 | 8456 | #if defined (HAVE_PSTATUS_T) |
b34976b6 | 8457 | static bfd_boolean |
217aa764 | 8458 | elfcore_grok_pstatus (bfd *abfd, Elf_Internal_Note *note) |
252b5132 | 8459 | { |
f572a39d AM |
8460 | if (note->descsz == sizeof (pstatus_t) |
8461 | #if defined (HAVE_PXSTATUS_T) | |
8462 | || note->descsz == sizeof (pxstatus_t) | |
8463 | #endif | |
8464 | ) | |
4a938328 MS |
8465 | { |
8466 | pstatus_t pstat; | |
252b5132 | 8467 | |
4a938328 | 8468 | memcpy (&pstat, note->descdata, sizeof (pstat)); |
252b5132 | 8469 | |
228e534f | 8470 | elf_tdata (abfd)->core->pid = pstat.pr_pid; |
4a938328 | 8471 | } |
7ee38065 | 8472 | #if defined (HAVE_PSTATUS32_T) |
4a938328 MS |
8473 | else if (note->descsz == sizeof (pstatus32_t)) |
8474 | { | |
8475 | /* 64-bit host, 32-bit corefile */ | |
8476 | pstatus32_t pstat; | |
252b5132 | 8477 | |
4a938328 | 8478 | memcpy (&pstat, note->descdata, sizeof (pstat)); |
252b5132 | 8479 | |
228e534f | 8480 | elf_tdata (abfd)->core->pid = pstat.pr_pid; |
4a938328 MS |
8481 | } |
8482 | #endif | |
252b5132 RH |
8483 | /* Could grab some more details from the "representative" |
8484 | lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an | |
c044fabd | 8485 | NT_LWPSTATUS note, presumably. */ |
252b5132 | 8486 | |
b34976b6 | 8487 | return TRUE; |
252b5132 RH |
8488 | } |
8489 | #endif /* defined (HAVE_PSTATUS_T) */ | |
8490 | ||
252b5132 | 8491 | #if defined (HAVE_LWPSTATUS_T) |
b34976b6 | 8492 | static bfd_boolean |
217aa764 | 8493 | elfcore_grok_lwpstatus (bfd *abfd, Elf_Internal_Note *note) |
252b5132 RH |
8494 | { |
8495 | lwpstatus_t lwpstat; | |
8496 | char buf[100]; | |
c044fabd | 8497 | char *name; |
d4c88bbb | 8498 | size_t len; |
c044fabd | 8499 | asection *sect; |
252b5132 | 8500 | |
f572a39d AM |
8501 | if (note->descsz != sizeof (lwpstat) |
8502 | #if defined (HAVE_LWPXSTATUS_T) | |
8503 | && note->descsz != sizeof (lwpxstatus_t) | |
8504 | #endif | |
8505 | ) | |
b34976b6 | 8506 | return TRUE; |
252b5132 RH |
8507 | |
8508 | memcpy (&lwpstat, note->descdata, sizeof (lwpstat)); | |
8509 | ||
228e534f | 8510 | elf_tdata (abfd)->core->lwpid = lwpstat.pr_lwpid; |
a1504221 JB |
8511 | /* Do not overwrite the core signal if it has already been set by |
8512 | another thread. */ | |
228e534f AM |
8513 | if (elf_tdata (abfd)->core->signal == 0) |
8514 | elf_tdata (abfd)->core->signal = lwpstat.pr_cursig; | |
252b5132 | 8515 | |
c044fabd | 8516 | /* Make a ".reg/999" section. */ |
252b5132 RH |
8517 | |
8518 | sprintf (buf, ".reg/%d", elfcore_make_pid (abfd)); | |
d4c88bbb | 8519 | len = strlen (buf) + 1; |
217aa764 | 8520 | name = bfd_alloc (abfd, len); |
252b5132 | 8521 | if (name == NULL) |
b34976b6 | 8522 | return FALSE; |
d4c88bbb | 8523 | memcpy (name, buf, len); |
252b5132 | 8524 | |
117ed4f8 | 8525 | sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); |
252b5132 | 8526 | if (sect == NULL) |
b34976b6 | 8527 | return FALSE; |
252b5132 RH |
8528 | |
8529 | #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT) | |
eea6121a | 8530 | sect->size = sizeof (lwpstat.pr_context.uc_mcontext.gregs); |
252b5132 RH |
8531 | sect->filepos = note->descpos |
8532 | + offsetof (lwpstatus_t, pr_context.uc_mcontext.gregs); | |
8533 | #endif | |
8534 | ||
8535 | #if defined (HAVE_LWPSTATUS_T_PR_REG) | |
eea6121a | 8536 | sect->size = sizeof (lwpstat.pr_reg); |
252b5132 RH |
8537 | sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_reg); |
8538 | #endif | |
8539 | ||
252b5132 RH |
8540 | sect->alignment_power = 2; |
8541 | ||
8542 | if (!elfcore_maybe_make_sect (abfd, ".reg", sect)) | |
b34976b6 | 8543 | return FALSE; |
252b5132 RH |
8544 | |
8545 | /* Make a ".reg2/999" section */ | |
8546 | ||
8547 | sprintf (buf, ".reg2/%d", elfcore_make_pid (abfd)); | |
d4c88bbb | 8548 | len = strlen (buf) + 1; |
217aa764 | 8549 | name = bfd_alloc (abfd, len); |
252b5132 | 8550 | if (name == NULL) |
b34976b6 | 8551 | return FALSE; |
d4c88bbb | 8552 | memcpy (name, buf, len); |
252b5132 | 8553 | |
117ed4f8 | 8554 | sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); |
252b5132 | 8555 | if (sect == NULL) |
b34976b6 | 8556 | return FALSE; |
252b5132 RH |
8557 | |
8558 | #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT) | |
eea6121a | 8559 | sect->size = sizeof (lwpstat.pr_context.uc_mcontext.fpregs); |
252b5132 RH |
8560 | sect->filepos = note->descpos |
8561 | + offsetof (lwpstatus_t, pr_context.uc_mcontext.fpregs); | |
8562 | #endif | |
8563 | ||
8564 | #if defined (HAVE_LWPSTATUS_T_PR_FPREG) | |
eea6121a | 8565 | sect->size = sizeof (lwpstat.pr_fpreg); |
252b5132 RH |
8566 | sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_fpreg); |
8567 | #endif | |
8568 | ||
252b5132 RH |
8569 | sect->alignment_power = 2; |
8570 | ||
936e320b | 8571 | return elfcore_maybe_make_sect (abfd, ".reg2", sect); |
252b5132 RH |
8572 | } |
8573 | #endif /* defined (HAVE_LWPSTATUS_T) */ | |
8574 | ||
b34976b6 | 8575 | static bfd_boolean |
217aa764 | 8576 | elfcore_grok_win32pstatus (bfd *abfd, Elf_Internal_Note *note) |
16e9c715 NC |
8577 | { |
8578 | char buf[30]; | |
c044fabd | 8579 | char *name; |
d4c88bbb | 8580 | size_t len; |
c044fabd | 8581 | asection *sect; |
4a6636fb PA |
8582 | int type; |
8583 | int is_active_thread; | |
8584 | bfd_vma base_addr; | |
16e9c715 | 8585 | |
4a6636fb | 8586 | if (note->descsz < 728) |
b34976b6 | 8587 | return TRUE; |
16e9c715 | 8588 | |
4a6636fb PA |
8589 | if (! CONST_STRNEQ (note->namedata, "win32")) |
8590 | return TRUE; | |
8591 | ||
8592 | type = bfd_get_32 (abfd, note->descdata); | |
c044fabd | 8593 | |
4a6636fb | 8594 | switch (type) |
16e9c715 | 8595 | { |
4a6636fb | 8596 | case 1 /* NOTE_INFO_PROCESS */: |
228e534f | 8597 | /* FIXME: need to add ->core->command. */ |
4a6636fb | 8598 | /* process_info.pid */ |
228e534f | 8599 | elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, note->descdata + 8); |
4a6636fb | 8600 | /* process_info.signal */ |
228e534f | 8601 | elf_tdata (abfd)->core->signal = bfd_get_32 (abfd, note->descdata + 12); |
c044fabd | 8602 | break; |
16e9c715 | 8603 | |
4a6636fb | 8604 | case 2 /* NOTE_INFO_THREAD */: |
16e9c715 | 8605 | /* Make a ".reg/999" section. */ |
4a6636fb PA |
8606 | /* thread_info.tid */ |
8607 | sprintf (buf, ".reg/%ld", (long) bfd_get_32 (abfd, note->descdata + 8)); | |
c044fabd | 8608 | |
d4c88bbb | 8609 | len = strlen (buf) + 1; |
a50b1753 | 8610 | name = (char *) bfd_alloc (abfd, len); |
16e9c715 | 8611 | if (name == NULL) |
b34976b6 | 8612 | return FALSE; |
c044fabd | 8613 | |
d4c88bbb | 8614 | memcpy (name, buf, len); |
16e9c715 | 8615 | |
117ed4f8 | 8616 | sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); |
16e9c715 | 8617 | if (sect == NULL) |
b34976b6 | 8618 | return FALSE; |
c044fabd | 8619 | |
4a6636fb PA |
8620 | /* sizeof (thread_info.thread_context) */ |
8621 | sect->size = 716; | |
8622 | /* offsetof (thread_info.thread_context) */ | |
8623 | sect->filepos = note->descpos + 12; | |
16e9c715 NC |
8624 | sect->alignment_power = 2; |
8625 | ||
4a6636fb PA |
8626 | /* thread_info.is_active_thread */ |
8627 | is_active_thread = bfd_get_32 (abfd, note->descdata + 8); | |
8628 | ||
8629 | if (is_active_thread) | |
16e9c715 | 8630 | if (! elfcore_maybe_make_sect (abfd, ".reg", sect)) |
b34976b6 | 8631 | return FALSE; |
16e9c715 NC |
8632 | break; |
8633 | ||
4a6636fb | 8634 | case 3 /* NOTE_INFO_MODULE */: |
16e9c715 | 8635 | /* Make a ".module/xxxxxxxx" section. */ |
4a6636fb PA |
8636 | /* module_info.base_address */ |
8637 | base_addr = bfd_get_32 (abfd, note->descdata + 4); | |
0af1713e | 8638 | sprintf (buf, ".module/%08lx", (unsigned long) base_addr); |
c044fabd | 8639 | |
d4c88bbb | 8640 | len = strlen (buf) + 1; |
a50b1753 | 8641 | name = (char *) bfd_alloc (abfd, len); |
16e9c715 | 8642 | if (name == NULL) |
b34976b6 | 8643 | return FALSE; |
c044fabd | 8644 | |
d4c88bbb | 8645 | memcpy (name, buf, len); |
252b5132 | 8646 | |
117ed4f8 | 8647 | sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); |
c044fabd | 8648 | |
16e9c715 | 8649 | if (sect == NULL) |
b34976b6 | 8650 | return FALSE; |
c044fabd | 8651 | |
eea6121a | 8652 | sect->size = note->descsz; |
16e9c715 | 8653 | sect->filepos = note->descpos; |
16e9c715 NC |
8654 | sect->alignment_power = 2; |
8655 | break; | |
8656 | ||
8657 | default: | |
b34976b6 | 8658 | return TRUE; |
16e9c715 NC |
8659 | } |
8660 | ||
b34976b6 | 8661 | return TRUE; |
16e9c715 | 8662 | } |
252b5132 | 8663 | |
b34976b6 | 8664 | static bfd_boolean |
217aa764 | 8665 | elfcore_grok_note (bfd *abfd, Elf_Internal_Note *note) |
252b5132 | 8666 | { |
9c5bfbb7 | 8667 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
bb0082d6 | 8668 | |
252b5132 RH |
8669 | switch (note->type) |
8670 | { | |
8671 | default: | |
b34976b6 | 8672 | return TRUE; |
252b5132 | 8673 | |
252b5132 | 8674 | case NT_PRSTATUS: |
bb0082d6 AM |
8675 | if (bed->elf_backend_grok_prstatus) |
8676 | if ((*bed->elf_backend_grok_prstatus) (abfd, note)) | |
b34976b6 | 8677 | return TRUE; |
bb0082d6 | 8678 | #if defined (HAVE_PRSTATUS_T) |
252b5132 | 8679 | return elfcore_grok_prstatus (abfd, note); |
bb0082d6 | 8680 | #else |
b34976b6 | 8681 | return TRUE; |
252b5132 RH |
8682 | #endif |
8683 | ||
8684 | #if defined (HAVE_PSTATUS_T) | |
8685 | case NT_PSTATUS: | |
8686 | return elfcore_grok_pstatus (abfd, note); | |
8687 | #endif | |
8688 | ||
8689 | #if defined (HAVE_LWPSTATUS_T) | |
8690 | case NT_LWPSTATUS: | |
8691 | return elfcore_grok_lwpstatus (abfd, note); | |
8692 | #endif | |
8693 | ||
8694 | case NT_FPREGSET: /* FIXME: rename to NT_PRFPREG */ | |
8695 | return elfcore_grok_prfpreg (abfd, note); | |
8696 | ||
c044fabd | 8697 | case NT_WIN32PSTATUS: |
16e9c715 | 8698 | return elfcore_grok_win32pstatus (abfd, note); |
16e9c715 | 8699 | |
c044fabd | 8700 | case NT_PRXFPREG: /* Linux SSE extension */ |
e377ab71 MK |
8701 | if (note->namesz == 6 |
8702 | && strcmp (note->namedata, "LINUX") == 0) | |
ff08c6bb JB |
8703 | return elfcore_grok_prxfpreg (abfd, note); |
8704 | else | |
b34976b6 | 8705 | return TRUE; |
ff08c6bb | 8706 | |
4339cae0 L |
8707 | case NT_X86_XSTATE: /* Linux XSAVE extension */ |
8708 | if (note->namesz == 6 | |
8709 | && strcmp (note->namedata, "LINUX") == 0) | |
8710 | return elfcore_grok_xstatereg (abfd, note); | |
8711 | else | |
8712 | return TRUE; | |
8713 | ||
97753bd5 AM |
8714 | case NT_PPC_VMX: |
8715 | if (note->namesz == 6 | |
8716 | && strcmp (note->namedata, "LINUX") == 0) | |
8717 | return elfcore_grok_ppc_vmx (abfd, note); | |
8718 | else | |
8719 | return TRUE; | |
8720 | ||
89eeb0bc LM |
8721 | case NT_PPC_VSX: |
8722 | if (note->namesz == 6 | |
8723 | && strcmp (note->namedata, "LINUX") == 0) | |
8724 | return elfcore_grok_ppc_vsx (abfd, note); | |
8725 | else | |
8726 | return TRUE; | |
8727 | ||
0675e188 UW |
8728 | case NT_S390_HIGH_GPRS: |
8729 | if (note->namesz == 6 | |
8730 | && strcmp (note->namedata, "LINUX") == 0) | |
8731 | return elfcore_grok_s390_high_gprs (abfd, note); | |
8732 | else | |
8733 | return TRUE; | |
8734 | ||
d7eeb400 MS |
8735 | case NT_S390_TIMER: |
8736 | if (note->namesz == 6 | |
8737 | && strcmp (note->namedata, "LINUX") == 0) | |
8738 | return elfcore_grok_s390_timer (abfd, note); | |
8739 | else | |
8740 | return TRUE; | |
8741 | ||
8742 | case NT_S390_TODCMP: | |
8743 | if (note->namesz == 6 | |
8744 | && strcmp (note->namedata, "LINUX") == 0) | |
8745 | return elfcore_grok_s390_todcmp (abfd, note); | |
8746 | else | |
8747 | return TRUE; | |
8748 | ||
8749 | case NT_S390_TODPREG: | |
8750 | if (note->namesz == 6 | |
8751 | && strcmp (note->namedata, "LINUX") == 0) | |
8752 | return elfcore_grok_s390_todpreg (abfd, note); | |
8753 | else | |
8754 | return TRUE; | |
8755 | ||
8756 | case NT_S390_CTRS: | |
8757 | if (note->namesz == 6 | |
8758 | && strcmp (note->namedata, "LINUX") == 0) | |
8759 | return elfcore_grok_s390_ctrs (abfd, note); | |
8760 | else | |
8761 | return TRUE; | |
8762 | ||
8763 | case NT_S390_PREFIX: | |
8764 | if (note->namesz == 6 | |
8765 | && strcmp (note->namedata, "LINUX") == 0) | |
8766 | return elfcore_grok_s390_prefix (abfd, note); | |
8767 | else | |
8768 | return TRUE; | |
8769 | ||
355b81d9 UW |
8770 | case NT_S390_LAST_BREAK: |
8771 | if (note->namesz == 6 | |
8772 | && strcmp (note->namedata, "LINUX") == 0) | |
8773 | return elfcore_grok_s390_last_break (abfd, note); | |
8774 | else | |
8775 | return TRUE; | |
8776 | ||
8777 | case NT_S390_SYSTEM_CALL: | |
8778 | if (note->namesz == 6 | |
8779 | && strcmp (note->namedata, "LINUX") == 0) | |
8780 | return elfcore_grok_s390_system_call (abfd, note); | |
8781 | else | |
8782 | return TRUE; | |
8783 | ||
abb3f6cc NC |
8784 | case NT_S390_TDB: |
8785 | if (note->namesz == 6 | |
8786 | && strcmp (note->namedata, "LINUX") == 0) | |
8787 | return elfcore_grok_s390_tdb (abfd, note); | |
8788 | else | |
8789 | return TRUE; | |
8790 | ||
faa9a424 UW |
8791 | case NT_ARM_VFP: |
8792 | if (note->namesz == 6 | |
8793 | && strcmp (note->namedata, "LINUX") == 0) | |
8794 | return elfcore_grok_arm_vfp (abfd, note); | |
8795 | else | |
8796 | return TRUE; | |
8797 | ||
652451f8 YZ |
8798 | case NT_ARM_TLS: |
8799 | if (note->namesz == 6 | |
8800 | && strcmp (note->namedata, "LINUX") == 0) | |
8801 | return elfcore_grok_aarch_tls (abfd, note); | |
8802 | else | |
8803 | return TRUE; | |
8804 | ||
8805 | case NT_ARM_HW_BREAK: | |
8806 | if (note->namesz == 6 | |
8807 | && strcmp (note->namedata, "LINUX") == 0) | |
8808 | return elfcore_grok_aarch_hw_break (abfd, note); | |
8809 | else | |
8810 | return TRUE; | |
8811 | ||
8812 | case NT_ARM_HW_WATCH: | |
8813 | if (note->namesz == 6 | |
8814 | && strcmp (note->namedata, "LINUX") == 0) | |
8815 | return elfcore_grok_aarch_hw_watch (abfd, note); | |
8816 | else | |
8817 | return TRUE; | |
8818 | ||
252b5132 RH |
8819 | case NT_PRPSINFO: |
8820 | case NT_PSINFO: | |
bb0082d6 AM |
8821 | if (bed->elf_backend_grok_psinfo) |
8822 | if ((*bed->elf_backend_grok_psinfo) (abfd, note)) | |
b34976b6 | 8823 | return TRUE; |
bb0082d6 | 8824 | #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) |
252b5132 | 8825 | return elfcore_grok_psinfo (abfd, note); |
bb0082d6 | 8826 | #else |
b34976b6 | 8827 | return TRUE; |
252b5132 | 8828 | #endif |
3333a7c3 RM |
8829 | |
8830 | case NT_AUXV: | |
8831 | { | |
117ed4f8 AM |
8832 | asection *sect = bfd_make_section_anyway_with_flags (abfd, ".auxv", |
8833 | SEC_HAS_CONTENTS); | |
3333a7c3 RM |
8834 | |
8835 | if (sect == NULL) | |
8836 | return FALSE; | |
eea6121a | 8837 | sect->size = note->descsz; |
3333a7c3 | 8838 | sect->filepos = note->descpos; |
3333a7c3 RM |
8839 | sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32; |
8840 | ||
8841 | return TRUE; | |
8842 | } | |
9015683b | 8843 | |
451b7c33 TT |
8844 | case NT_FILE: |
8845 | return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.file", | |
8846 | note); | |
8847 | ||
9015683b TT |
8848 | case NT_SIGINFO: |
8849 | return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.siginfo", | |
8850 | note); | |
252b5132 RH |
8851 | } |
8852 | } | |
8853 | ||
718175fa JK |
8854 | static bfd_boolean |
8855 | elfobj_grok_gnu_build_id (bfd *abfd, Elf_Internal_Note *note) | |
8856 | { | |
30e8ee25 AM |
8857 | struct elf_obj_tdata *t; |
8858 | ||
8859 | if (note->descsz == 0) | |
8860 | return FALSE; | |
8861 | ||
8862 | t = elf_tdata (abfd); | |
c0355132 | 8863 | t->build_id = bfd_alloc (abfd, sizeof (*t->build_id) - 1 + note->descsz); |
30e8ee25 | 8864 | if (t->build_id == NULL) |
718175fa JK |
8865 | return FALSE; |
8866 | ||
c0355132 AM |
8867 | t->build_id->size = note->descsz; |
8868 | memcpy (t->build_id->data, note->descdata, note->descsz); | |
718175fa JK |
8869 | |
8870 | return TRUE; | |
8871 | } | |
8872 | ||
8873 | static bfd_boolean | |
8874 | elfobj_grok_gnu_note (bfd *abfd, Elf_Internal_Note *note) | |
8875 | { | |
8876 | switch (note->type) | |
8877 | { | |
8878 | default: | |
8879 | return TRUE; | |
8880 | ||
8881 | case NT_GNU_BUILD_ID: | |
8882 | return elfobj_grok_gnu_build_id (abfd, note); | |
8883 | } | |
8884 | } | |
8885 | ||
e21e5835 NC |
8886 | static bfd_boolean |
8887 | elfobj_grok_stapsdt_note_1 (bfd *abfd, Elf_Internal_Note *note) | |
8888 | { | |
8889 | struct sdt_note *cur = | |
8890 | (struct sdt_note *) bfd_alloc (abfd, sizeof (struct sdt_note) | |
8891 | + note->descsz); | |
8892 | ||
8893 | cur->next = (struct sdt_note *) (elf_tdata (abfd))->sdt_note_head; | |
8894 | cur->size = (bfd_size_type) note->descsz; | |
8895 | memcpy (cur->data, note->descdata, note->descsz); | |
8896 | ||
8897 | elf_tdata (abfd)->sdt_note_head = cur; | |
8898 | ||
8899 | return TRUE; | |
8900 | } | |
8901 | ||
8902 | static bfd_boolean | |
8903 | elfobj_grok_stapsdt_note (bfd *abfd, Elf_Internal_Note *note) | |
8904 | { | |
8905 | switch (note->type) | |
8906 | { | |
8907 | case NT_STAPSDT: | |
8908 | return elfobj_grok_stapsdt_note_1 (abfd, note); | |
8909 | ||
8910 | default: | |
8911 | return TRUE; | |
8912 | } | |
8913 | } | |
8914 | ||
b34976b6 | 8915 | static bfd_boolean |
217aa764 | 8916 | elfcore_netbsd_get_lwpid (Elf_Internal_Note *note, int *lwpidp) |
50b2bdb7 AM |
8917 | { |
8918 | char *cp; | |
8919 | ||
8920 | cp = strchr (note->namedata, '@'); | |
8921 | if (cp != NULL) | |
8922 | { | |
d2b64500 | 8923 | *lwpidp = atoi(cp + 1); |
b34976b6 | 8924 | return TRUE; |
50b2bdb7 | 8925 | } |
b34976b6 | 8926 | return FALSE; |
50b2bdb7 AM |
8927 | } |
8928 | ||
b34976b6 | 8929 | static bfd_boolean |
217aa764 | 8930 | elfcore_grok_netbsd_procinfo (bfd *abfd, Elf_Internal_Note *note) |
50b2bdb7 | 8931 | { |
50b2bdb7 | 8932 | /* Signal number at offset 0x08. */ |
228e534f | 8933 | elf_tdata (abfd)->core->signal |
50b2bdb7 AM |
8934 | = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08); |
8935 | ||
8936 | /* Process ID at offset 0x50. */ | |
228e534f | 8937 | elf_tdata (abfd)->core->pid |
50b2bdb7 AM |
8938 | = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x50); |
8939 | ||
8940 | /* Command name at 0x7c (max 32 bytes, including nul). */ | |
228e534f | 8941 | elf_tdata (abfd)->core->command |
50b2bdb7 AM |
8942 | = _bfd_elfcore_strndup (abfd, note->descdata + 0x7c, 31); |
8943 | ||
7720ba9f MK |
8944 | return elfcore_make_note_pseudosection (abfd, ".note.netbsdcore.procinfo", |
8945 | note); | |
50b2bdb7 AM |
8946 | } |
8947 | ||
b34976b6 | 8948 | static bfd_boolean |
217aa764 | 8949 | elfcore_grok_netbsd_note (bfd *abfd, Elf_Internal_Note *note) |
50b2bdb7 AM |
8950 | { |
8951 | int lwp; | |
8952 | ||
8953 | if (elfcore_netbsd_get_lwpid (note, &lwp)) | |
228e534f | 8954 | elf_tdata (abfd)->core->lwpid = lwp; |
50b2bdb7 | 8955 | |
b4db1224 | 8956 | if (note->type == NT_NETBSDCORE_PROCINFO) |
50b2bdb7 AM |
8957 | { |
8958 | /* NetBSD-specific core "procinfo". Note that we expect to | |
08a40648 AM |
8959 | find this note before any of the others, which is fine, |
8960 | since the kernel writes this note out first when it | |
8961 | creates a core file. */ | |
47d9a591 | 8962 | |
50b2bdb7 AM |
8963 | return elfcore_grok_netbsd_procinfo (abfd, note); |
8964 | } | |
8965 | ||
b4db1224 JT |
8966 | /* As of Jan 2002 there are no other machine-independent notes |
8967 | defined for NetBSD core files. If the note type is less | |
8968 | than the start of the machine-dependent note types, we don't | |
8969 | understand it. */ | |
47d9a591 | 8970 | |
b4db1224 | 8971 | if (note->type < NT_NETBSDCORE_FIRSTMACH) |
b34976b6 | 8972 | return TRUE; |
50b2bdb7 AM |
8973 | |
8974 | ||
8975 | switch (bfd_get_arch (abfd)) | |
8976 | { | |
08a40648 AM |
8977 | /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and |
8978 | PT_GETFPREGS == mach+2. */ | |
50b2bdb7 AM |
8979 | |
8980 | case bfd_arch_alpha: | |
8981 | case bfd_arch_sparc: | |
8982 | switch (note->type) | |
08a40648 AM |
8983 | { |
8984 | case NT_NETBSDCORE_FIRSTMACH+0: | |
8985 | return elfcore_make_note_pseudosection (abfd, ".reg", note); | |
50b2bdb7 | 8986 | |
08a40648 AM |
8987 | case NT_NETBSDCORE_FIRSTMACH+2: |
8988 | return elfcore_make_note_pseudosection (abfd, ".reg2", note); | |
50b2bdb7 | 8989 | |
08a40648 AM |
8990 | default: |
8991 | return TRUE; | |
8992 | } | |
50b2bdb7 | 8993 | |
08a40648 AM |
8994 | /* On all other arch's, PT_GETREGS == mach+1 and |
8995 | PT_GETFPREGS == mach+3. */ | |
50b2bdb7 AM |
8996 | |
8997 | default: | |
8998 | switch (note->type) | |
08a40648 AM |
8999 | { |
9000 | case NT_NETBSDCORE_FIRSTMACH+1: | |
9001 | return elfcore_make_note_pseudosection (abfd, ".reg", note); | |
50b2bdb7 | 9002 | |
08a40648 AM |
9003 | case NT_NETBSDCORE_FIRSTMACH+3: |
9004 | return elfcore_make_note_pseudosection (abfd, ".reg2", note); | |
50b2bdb7 | 9005 | |
08a40648 AM |
9006 | default: |
9007 | return TRUE; | |
9008 | } | |
50b2bdb7 AM |
9009 | } |
9010 | /* NOTREACHED */ | |
9011 | } | |
9012 | ||
67cc5033 MK |
9013 | static bfd_boolean |
9014 | elfcore_grok_openbsd_procinfo (bfd *abfd, Elf_Internal_Note *note) | |
9015 | { | |
9016 | /* Signal number at offset 0x08. */ | |
228e534f | 9017 | elf_tdata (abfd)->core->signal |
67cc5033 MK |
9018 | = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08); |
9019 | ||
9020 | /* Process ID at offset 0x20. */ | |
228e534f | 9021 | elf_tdata (abfd)->core->pid |
67cc5033 MK |
9022 | = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x20); |
9023 | ||
9024 | /* Command name at 0x48 (max 32 bytes, including nul). */ | |
228e534f | 9025 | elf_tdata (abfd)->core->command |
67cc5033 MK |
9026 | = _bfd_elfcore_strndup (abfd, note->descdata + 0x48, 31); |
9027 | ||
9028 | return TRUE; | |
9029 | } | |
9030 | ||
9031 | static bfd_boolean | |
9032 | elfcore_grok_openbsd_note (bfd *abfd, Elf_Internal_Note *note) | |
9033 | { | |
9034 | if (note->type == NT_OPENBSD_PROCINFO) | |
9035 | return elfcore_grok_openbsd_procinfo (abfd, note); | |
9036 | ||
9037 | if (note->type == NT_OPENBSD_REGS) | |
9038 | return elfcore_make_note_pseudosection (abfd, ".reg", note); | |
9039 | ||
9040 | if (note->type == NT_OPENBSD_FPREGS) | |
9041 | return elfcore_make_note_pseudosection (abfd, ".reg2", note); | |
9042 | ||
9043 | if (note->type == NT_OPENBSD_XFPREGS) | |
9044 | return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note); | |
9045 | ||
9046 | if (note->type == NT_OPENBSD_AUXV) | |
9047 | { | |
9048 | asection *sect = bfd_make_section_anyway_with_flags (abfd, ".auxv", | |
9049 | SEC_HAS_CONTENTS); | |
9050 | ||
9051 | if (sect == NULL) | |
9052 | return FALSE; | |
9053 | sect->size = note->descsz; | |
9054 | sect->filepos = note->descpos; | |
9055 | sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32; | |
9056 | ||
9057 | return TRUE; | |
9058 | } | |
9059 | ||
9060 | if (note->type == NT_OPENBSD_WCOOKIE) | |
9061 | { | |
9062 | asection *sect = bfd_make_section_anyway_with_flags (abfd, ".wcookie", | |
9063 | SEC_HAS_CONTENTS); | |
9064 | ||
9065 | if (sect == NULL) | |
9066 | return FALSE; | |
9067 | sect->size = note->descsz; | |
9068 | sect->filepos = note->descpos; | |
9069 | sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32; | |
9070 | ||
9071 | return TRUE; | |
9072 | } | |
9073 | ||
9074 | return TRUE; | |
9075 | } | |
9076 | ||
07c6e936 | 9077 | static bfd_boolean |
d3fd4074 | 9078 | elfcore_grok_nto_status (bfd *abfd, Elf_Internal_Note *note, long *tid) |
07c6e936 NC |
9079 | { |
9080 | void *ddata = note->descdata; | |
9081 | char buf[100]; | |
9082 | char *name; | |
9083 | asection *sect; | |
f8843e87 AM |
9084 | short sig; |
9085 | unsigned flags; | |
07c6e936 NC |
9086 | |
9087 | /* nto_procfs_status 'pid' field is at offset 0. */ | |
228e534f | 9088 | elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, (bfd_byte *) ddata); |
07c6e936 | 9089 | |
f8843e87 AM |
9090 | /* nto_procfs_status 'tid' field is at offset 4. Pass it back. */ |
9091 | *tid = bfd_get_32 (abfd, (bfd_byte *) ddata + 4); | |
9092 | ||
9093 | /* nto_procfs_status 'flags' field is at offset 8. */ | |
9094 | flags = bfd_get_32 (abfd, (bfd_byte *) ddata + 8); | |
07c6e936 NC |
9095 | |
9096 | /* nto_procfs_status 'what' field is at offset 14. */ | |
f8843e87 AM |
9097 | if ((sig = bfd_get_16 (abfd, (bfd_byte *) ddata + 14)) > 0) |
9098 | { | |
228e534f AM |
9099 | elf_tdata (abfd)->core->signal = sig; |
9100 | elf_tdata (abfd)->core->lwpid = *tid; | |
f8843e87 | 9101 | } |
07c6e936 | 9102 | |
f8843e87 AM |
9103 | /* _DEBUG_FLAG_CURTID (current thread) is 0x80. Some cores |
9104 | do not come from signals so we make sure we set the current | |
9105 | thread just in case. */ | |
9106 | if (flags & 0x00000080) | |
228e534f | 9107 | elf_tdata (abfd)->core->lwpid = *tid; |
07c6e936 NC |
9108 | |
9109 | /* Make a ".qnx_core_status/%d" section. */ | |
d3fd4074 | 9110 | sprintf (buf, ".qnx_core_status/%ld", *tid); |
07c6e936 | 9111 | |
a50b1753 | 9112 | name = (char *) bfd_alloc (abfd, strlen (buf) + 1); |
07c6e936 NC |
9113 | if (name == NULL) |
9114 | return FALSE; | |
9115 | strcpy (name, buf); | |
9116 | ||
117ed4f8 | 9117 | sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); |
07c6e936 NC |
9118 | if (sect == NULL) |
9119 | return FALSE; | |
9120 | ||
eea6121a | 9121 | sect->size = note->descsz; |
07c6e936 | 9122 | sect->filepos = note->descpos; |
07c6e936 NC |
9123 | sect->alignment_power = 2; |
9124 | ||
9125 | return (elfcore_maybe_make_sect (abfd, ".qnx_core_status", sect)); | |
9126 | } | |
9127 | ||
9128 | static bfd_boolean | |
d69f560c KW |
9129 | elfcore_grok_nto_regs (bfd *abfd, |
9130 | Elf_Internal_Note *note, | |
d3fd4074 | 9131 | long tid, |
d69f560c | 9132 | char *base) |
07c6e936 NC |
9133 | { |
9134 | char buf[100]; | |
9135 | char *name; | |
9136 | asection *sect; | |
9137 | ||
d69f560c | 9138 | /* Make a "(base)/%d" section. */ |
d3fd4074 | 9139 | sprintf (buf, "%s/%ld", base, tid); |
07c6e936 | 9140 | |
a50b1753 | 9141 | name = (char *) bfd_alloc (abfd, strlen (buf) + 1); |
07c6e936 NC |
9142 | if (name == NULL) |
9143 | return FALSE; | |
9144 | strcpy (name, buf); | |
9145 | ||
117ed4f8 | 9146 | sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); |
07c6e936 NC |
9147 | if (sect == NULL) |
9148 | return FALSE; | |
9149 | ||
eea6121a | 9150 | sect->size = note->descsz; |
07c6e936 | 9151 | sect->filepos = note->descpos; |
07c6e936 NC |
9152 | sect->alignment_power = 2; |
9153 | ||
f8843e87 | 9154 | /* This is the current thread. */ |
228e534f | 9155 | if (elf_tdata (abfd)->core->lwpid == tid) |
d69f560c | 9156 | return elfcore_maybe_make_sect (abfd, base, sect); |
f8843e87 AM |
9157 | |
9158 | return TRUE; | |
07c6e936 NC |
9159 | } |
9160 | ||
9161 | #define BFD_QNT_CORE_INFO 7 | |
9162 | #define BFD_QNT_CORE_STATUS 8 | |
9163 | #define BFD_QNT_CORE_GREG 9 | |
9164 | #define BFD_QNT_CORE_FPREG 10 | |
9165 | ||
9166 | static bfd_boolean | |
217aa764 | 9167 | elfcore_grok_nto_note (bfd *abfd, Elf_Internal_Note *note) |
07c6e936 NC |
9168 | { |
9169 | /* Every GREG section has a STATUS section before it. Store the | |
811072d8 | 9170 | tid from the previous call to pass down to the next gregs |
07c6e936 | 9171 | function. */ |
d3fd4074 | 9172 | static long tid = 1; |
07c6e936 NC |
9173 | |
9174 | switch (note->type) | |
9175 | { | |
d69f560c KW |
9176 | case BFD_QNT_CORE_INFO: |
9177 | return elfcore_make_note_pseudosection (abfd, ".qnx_core_info", note); | |
9178 | case BFD_QNT_CORE_STATUS: | |
9179 | return elfcore_grok_nto_status (abfd, note, &tid); | |
9180 | case BFD_QNT_CORE_GREG: | |
9181 | return elfcore_grok_nto_regs (abfd, note, tid, ".reg"); | |
9182 | case BFD_QNT_CORE_FPREG: | |
9183 | return elfcore_grok_nto_regs (abfd, note, tid, ".reg2"); | |
9184 | default: | |
9185 | return TRUE; | |
07c6e936 NC |
9186 | } |
9187 | } | |
9188 | ||
b15fa79e AM |
9189 | static bfd_boolean |
9190 | elfcore_grok_spu_note (bfd *abfd, Elf_Internal_Note *note) | |
9191 | { | |
9192 | char *name; | |
9193 | asection *sect; | |
9194 | size_t len; | |
9195 | ||
9196 | /* Use note name as section name. */ | |
9197 | len = note->namesz; | |
a50b1753 | 9198 | name = (char *) bfd_alloc (abfd, len); |
b15fa79e AM |
9199 | if (name == NULL) |
9200 | return FALSE; | |
9201 | memcpy (name, note->namedata, len); | |
9202 | name[len - 1] = '\0'; | |
9203 | ||
9204 | sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); | |
9205 | if (sect == NULL) | |
9206 | return FALSE; | |
9207 | ||
9208 | sect->size = note->descsz; | |
9209 | sect->filepos = note->descpos; | |
9210 | sect->alignment_power = 1; | |
9211 | ||
9212 | return TRUE; | |
9213 | } | |
9214 | ||
7c76fa91 MS |
9215 | /* Function: elfcore_write_note |
9216 | ||
47d9a591 | 9217 | Inputs: |
a39f3346 | 9218 | buffer to hold note, and current size of buffer |
7c76fa91 MS |
9219 | name of note |
9220 | type of note | |
9221 | data for note | |
9222 | size of data for note | |
9223 | ||
a39f3346 AM |
9224 | Writes note to end of buffer. ELF64 notes are written exactly as |
9225 | for ELF32, despite the current (as of 2006) ELF gabi specifying | |
9226 | that they ought to have 8-byte namesz and descsz field, and have | |
9227 | 8-byte alignment. Other writers, eg. Linux kernel, do the same. | |
9228 | ||
7c76fa91 | 9229 | Return: |
a39f3346 | 9230 | Pointer to realloc'd buffer, *BUFSIZ updated. */ |
7c76fa91 MS |
9231 | |
9232 | char * | |
a39f3346 | 9233 | elfcore_write_note (bfd *abfd, |
217aa764 | 9234 | char *buf, |
a39f3346 | 9235 | int *bufsiz, |
217aa764 | 9236 | const char *name, |
a39f3346 | 9237 | int type, |
217aa764 | 9238 | const void *input, |
a39f3346 | 9239 | int size) |
7c76fa91 MS |
9240 | { |
9241 | Elf_External_Note *xnp; | |
d4c88bbb | 9242 | size_t namesz; |
d4c88bbb | 9243 | size_t newspace; |
a39f3346 | 9244 | char *dest; |
7c76fa91 | 9245 | |
d4c88bbb | 9246 | namesz = 0; |
d4c88bbb | 9247 | if (name != NULL) |
a39f3346 | 9248 | namesz = strlen (name) + 1; |
d4c88bbb | 9249 | |
a39f3346 | 9250 | newspace = 12 + ((namesz + 3) & -4) + ((size + 3) & -4); |
d4c88bbb | 9251 | |
a50b1753 | 9252 | buf = (char *) realloc (buf, *bufsiz + newspace); |
14b1c01e AM |
9253 | if (buf == NULL) |
9254 | return buf; | |
a39f3346 | 9255 | dest = buf + *bufsiz; |
7c76fa91 MS |
9256 | *bufsiz += newspace; |
9257 | xnp = (Elf_External_Note *) dest; | |
9258 | H_PUT_32 (abfd, namesz, xnp->namesz); | |
9259 | H_PUT_32 (abfd, size, xnp->descsz); | |
9260 | H_PUT_32 (abfd, type, xnp->type); | |
d4c88bbb AM |
9261 | dest = xnp->name; |
9262 | if (name != NULL) | |
9263 | { | |
9264 | memcpy (dest, name, namesz); | |
9265 | dest += namesz; | |
a39f3346 | 9266 | while (namesz & 3) |
d4c88bbb AM |
9267 | { |
9268 | *dest++ = '\0'; | |
a39f3346 | 9269 | ++namesz; |
d4c88bbb AM |
9270 | } |
9271 | } | |
9272 | memcpy (dest, input, size); | |
a39f3346 AM |
9273 | dest += size; |
9274 | while (size & 3) | |
9275 | { | |
9276 | *dest++ = '\0'; | |
9277 | ++size; | |
9278 | } | |
9279 | return buf; | |
7c76fa91 MS |
9280 | } |
9281 | ||
7c76fa91 | 9282 | char * |
217aa764 AM |
9283 | elfcore_write_prpsinfo (bfd *abfd, |
9284 | char *buf, | |
9285 | int *bufsiz, | |
9286 | const char *fname, | |
9287 | const char *psargs) | |
7c76fa91 | 9288 | { |
183e98be AM |
9289 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
9290 | ||
9291 | if (bed->elf_backend_write_core_note != NULL) | |
9292 | { | |
9293 | char *ret; | |
9294 | ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz, | |
9295 | NT_PRPSINFO, fname, psargs); | |
9296 | if (ret != NULL) | |
9297 | return ret; | |
9298 | } | |
7c76fa91 | 9299 | |
1f20dca5 | 9300 | #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) |
183e98be AM |
9301 | #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T) |
9302 | if (bed->s->elfclass == ELFCLASS32) | |
9303 | { | |
9304 | #if defined (HAVE_PSINFO32_T) | |
9305 | psinfo32_t data; | |
9306 | int note_type = NT_PSINFO; | |
9307 | #else | |
9308 | prpsinfo32_t data; | |
9309 | int note_type = NT_PRPSINFO; | |
9310 | #endif | |
9311 | ||
9312 | memset (&data, 0, sizeof (data)); | |
9313 | strncpy (data.pr_fname, fname, sizeof (data.pr_fname)); | |
9314 | strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs)); | |
9315 | return elfcore_write_note (abfd, buf, bufsiz, | |
1f20dca5 | 9316 | "CORE", note_type, &data, sizeof (data)); |
183e98be AM |
9317 | } |
9318 | else | |
9319 | #endif | |
9320 | { | |
7c76fa91 | 9321 | #if defined (HAVE_PSINFO_T) |
183e98be AM |
9322 | psinfo_t data; |
9323 | int note_type = NT_PSINFO; | |
7c76fa91 | 9324 | #else |
183e98be AM |
9325 | prpsinfo_t data; |
9326 | int note_type = NT_PRPSINFO; | |
7c76fa91 MS |
9327 | #endif |
9328 | ||
183e98be AM |
9329 | memset (&data, 0, sizeof (data)); |
9330 | strncpy (data.pr_fname, fname, sizeof (data.pr_fname)); | |
9331 | strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs)); | |
9332 | return elfcore_write_note (abfd, buf, bufsiz, | |
1f20dca5 | 9333 | "CORE", note_type, &data, sizeof (data)); |
183e98be | 9334 | } |
7c76fa91 MS |
9335 | #endif /* PSINFO_T or PRPSINFO_T */ |
9336 | ||
1f20dca5 UW |
9337 | free (buf); |
9338 | return NULL; | |
9339 | } | |
9340 | ||
70a38d42 SDJ |
9341 | char * |
9342 | elfcore_write_linux_prpsinfo32 | |
9343 | (bfd *abfd, char *buf, int *bufsiz, | |
9344 | const struct elf_internal_linux_prpsinfo *prpsinfo) | |
9345 | { | |
9346 | struct elf_external_linux_prpsinfo32 data; | |
9347 | ||
9348 | memset (&data, 0, sizeof (data)); | |
9349 | LINUX_PRPSINFO32_SWAP_FIELDS (abfd, prpsinfo, data); | |
9350 | ||
9351 | return elfcore_write_note (abfd, buf, bufsiz, "CORE", NT_PRPSINFO, | |
9352 | &data, sizeof (data)); | |
9353 | } | |
9354 | ||
9355 | char * | |
9356 | elfcore_write_linux_prpsinfo64 | |
9357 | (bfd *abfd, char *buf, int *bufsiz, | |
9358 | const struct elf_internal_linux_prpsinfo *prpsinfo) | |
9359 | { | |
9360 | struct elf_external_linux_prpsinfo64 data; | |
9361 | ||
9362 | memset (&data, 0, sizeof (data)); | |
9363 | LINUX_PRPSINFO64_SWAP_FIELDS (abfd, prpsinfo, data); | |
9364 | ||
9365 | return elfcore_write_note (abfd, buf, bufsiz, | |
9366 | "CORE", NT_PRPSINFO, &data, sizeof (data)); | |
9367 | } | |
9368 | ||
7c76fa91 | 9369 | char * |
217aa764 AM |
9370 | elfcore_write_prstatus (bfd *abfd, |
9371 | char *buf, | |
9372 | int *bufsiz, | |
9373 | long pid, | |
9374 | int cursig, | |
9375 | const void *gregs) | |
7c76fa91 | 9376 | { |
183e98be | 9377 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
7c76fa91 | 9378 | |
183e98be AM |
9379 | if (bed->elf_backend_write_core_note != NULL) |
9380 | { | |
9381 | char *ret; | |
9382 | ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz, | |
9383 | NT_PRSTATUS, | |
9384 | pid, cursig, gregs); | |
9385 | if (ret != NULL) | |
9386 | return ret; | |
9387 | } | |
9388 | ||
1f20dca5 | 9389 | #if defined (HAVE_PRSTATUS_T) |
183e98be AM |
9390 | #if defined (HAVE_PRSTATUS32_T) |
9391 | if (bed->s->elfclass == ELFCLASS32) | |
9392 | { | |
9393 | prstatus32_t prstat; | |
9394 | ||
9395 | memset (&prstat, 0, sizeof (prstat)); | |
9396 | prstat.pr_pid = pid; | |
9397 | prstat.pr_cursig = cursig; | |
9398 | memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg)); | |
1f20dca5 | 9399 | return elfcore_write_note (abfd, buf, bufsiz, "CORE", |
183e98be AM |
9400 | NT_PRSTATUS, &prstat, sizeof (prstat)); |
9401 | } | |
9402 | else | |
9403 | #endif | |
9404 | { | |
9405 | prstatus_t prstat; | |
9406 | ||
9407 | memset (&prstat, 0, sizeof (prstat)); | |
9408 | prstat.pr_pid = pid; | |
9409 | prstat.pr_cursig = cursig; | |
9410 | memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg)); | |
1f20dca5 | 9411 | return elfcore_write_note (abfd, buf, bufsiz, "CORE", |
183e98be AM |
9412 | NT_PRSTATUS, &prstat, sizeof (prstat)); |
9413 | } | |
7c76fa91 MS |
9414 | #endif /* HAVE_PRSTATUS_T */ |
9415 | ||
1f20dca5 UW |
9416 | free (buf); |
9417 | return NULL; | |
9418 | } | |
9419 | ||
51316059 MS |
9420 | #if defined (HAVE_LWPSTATUS_T) |
9421 | char * | |
217aa764 AM |
9422 | elfcore_write_lwpstatus (bfd *abfd, |
9423 | char *buf, | |
9424 | int *bufsiz, | |
9425 | long pid, | |
9426 | int cursig, | |
9427 | const void *gregs) | |
51316059 MS |
9428 | { |
9429 | lwpstatus_t lwpstat; | |
183e98be | 9430 | const char *note_name = "CORE"; |
51316059 MS |
9431 | |
9432 | memset (&lwpstat, 0, sizeof (lwpstat)); | |
9433 | lwpstat.pr_lwpid = pid >> 16; | |
9434 | lwpstat.pr_cursig = cursig; | |
9435 | #if defined (HAVE_LWPSTATUS_T_PR_REG) | |
d1e8523e | 9436 | memcpy (&lwpstat.pr_reg, gregs, sizeof (lwpstat.pr_reg)); |
51316059 MS |
9437 | #elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT) |
9438 | #if !defined(gregs) | |
9439 | memcpy (lwpstat.pr_context.uc_mcontext.gregs, | |
9440 | gregs, sizeof (lwpstat.pr_context.uc_mcontext.gregs)); | |
9441 | #else | |
9442 | memcpy (lwpstat.pr_context.uc_mcontext.__gregs, | |
9443 | gregs, sizeof (lwpstat.pr_context.uc_mcontext.__gregs)); | |
9444 | #endif | |
9445 | #endif | |
47d9a591 | 9446 | return elfcore_write_note (abfd, buf, bufsiz, note_name, |
51316059 MS |
9447 | NT_LWPSTATUS, &lwpstat, sizeof (lwpstat)); |
9448 | } | |
9449 | #endif /* HAVE_LWPSTATUS_T */ | |
9450 | ||
7c76fa91 MS |
9451 | #if defined (HAVE_PSTATUS_T) |
9452 | char * | |
217aa764 AM |
9453 | elfcore_write_pstatus (bfd *abfd, |
9454 | char *buf, | |
9455 | int *bufsiz, | |
9456 | long pid, | |
6c10990d NC |
9457 | int cursig ATTRIBUTE_UNUSED, |
9458 | const void *gregs ATTRIBUTE_UNUSED) | |
7c76fa91 | 9459 | { |
183e98be AM |
9460 | const char *note_name = "CORE"; |
9461 | #if defined (HAVE_PSTATUS32_T) | |
9462 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
7c76fa91 | 9463 | |
183e98be AM |
9464 | if (bed->s->elfclass == ELFCLASS32) |
9465 | { | |
9466 | pstatus32_t pstat; | |
9467 | ||
9468 | memset (&pstat, 0, sizeof (pstat)); | |
9469 | pstat.pr_pid = pid & 0xffff; | |
9470 | buf = elfcore_write_note (abfd, buf, bufsiz, note_name, | |
9471 | NT_PSTATUS, &pstat, sizeof (pstat)); | |
9472 | return buf; | |
9473 | } | |
9474 | else | |
9475 | #endif | |
9476 | { | |
9477 | pstatus_t pstat; | |
9478 | ||
9479 | memset (&pstat, 0, sizeof (pstat)); | |
9480 | pstat.pr_pid = pid & 0xffff; | |
9481 | buf = elfcore_write_note (abfd, buf, bufsiz, note_name, | |
9482 | NT_PSTATUS, &pstat, sizeof (pstat)); | |
9483 | return buf; | |
9484 | } | |
7c76fa91 MS |
9485 | } |
9486 | #endif /* HAVE_PSTATUS_T */ | |
9487 | ||
9488 | char * | |
217aa764 AM |
9489 | elfcore_write_prfpreg (bfd *abfd, |
9490 | char *buf, | |
9491 | int *bufsiz, | |
9492 | const void *fpregs, | |
9493 | int size) | |
7c76fa91 | 9494 | { |
183e98be | 9495 | const char *note_name = "CORE"; |
47d9a591 | 9496 | return elfcore_write_note (abfd, buf, bufsiz, |
7c76fa91 MS |
9497 | note_name, NT_FPREGSET, fpregs, size); |
9498 | } | |
9499 | ||
9500 | char * | |
217aa764 AM |
9501 | elfcore_write_prxfpreg (bfd *abfd, |
9502 | char *buf, | |
9503 | int *bufsiz, | |
9504 | const void *xfpregs, | |
9505 | int size) | |
7c76fa91 MS |
9506 | { |
9507 | char *note_name = "LINUX"; | |
47d9a591 | 9508 | return elfcore_write_note (abfd, buf, bufsiz, |
7c76fa91 MS |
9509 | note_name, NT_PRXFPREG, xfpregs, size); |
9510 | } | |
9511 | ||
4339cae0 L |
9512 | char * |
9513 | elfcore_write_xstatereg (bfd *abfd, char *buf, int *bufsiz, | |
9514 | const void *xfpregs, int size) | |
9515 | { | |
9516 | char *note_name = "LINUX"; | |
9517 | return elfcore_write_note (abfd, buf, bufsiz, | |
9518 | note_name, NT_X86_XSTATE, xfpregs, size); | |
9519 | } | |
9520 | ||
97753bd5 AM |
9521 | char * |
9522 | elfcore_write_ppc_vmx (bfd *abfd, | |
9523 | char *buf, | |
9524 | int *bufsiz, | |
9525 | const void *ppc_vmx, | |
9526 | int size) | |
9527 | { | |
9528 | char *note_name = "LINUX"; | |
9529 | return elfcore_write_note (abfd, buf, bufsiz, | |
9530 | note_name, NT_PPC_VMX, ppc_vmx, size); | |
9531 | } | |
9532 | ||
89eeb0bc LM |
9533 | char * |
9534 | elfcore_write_ppc_vsx (bfd *abfd, | |
9535 | char *buf, | |
9536 | int *bufsiz, | |
9537 | const void *ppc_vsx, | |
9538 | int size) | |
9539 | { | |
9540 | char *note_name = "LINUX"; | |
9541 | return elfcore_write_note (abfd, buf, bufsiz, | |
9542 | note_name, NT_PPC_VSX, ppc_vsx, size); | |
9543 | } | |
9544 | ||
0675e188 UW |
9545 | static char * |
9546 | elfcore_write_s390_high_gprs (bfd *abfd, | |
9547 | char *buf, | |
9548 | int *bufsiz, | |
9549 | const void *s390_high_gprs, | |
9550 | int size) | |
9551 | { | |
9552 | char *note_name = "LINUX"; | |
9553 | return elfcore_write_note (abfd, buf, bufsiz, | |
9554 | note_name, NT_S390_HIGH_GPRS, | |
9555 | s390_high_gprs, size); | |
9556 | } | |
9557 | ||
d7eeb400 MS |
9558 | char * |
9559 | elfcore_write_s390_timer (bfd *abfd, | |
9560 | char *buf, | |
9561 | int *bufsiz, | |
9562 | const void *s390_timer, | |
9563 | int size) | |
9564 | { | |
9565 | char *note_name = "LINUX"; | |
9566 | return elfcore_write_note (abfd, buf, bufsiz, | |
9567 | note_name, NT_S390_TIMER, s390_timer, size); | |
9568 | } | |
9569 | ||
9570 | char * | |
9571 | elfcore_write_s390_todcmp (bfd *abfd, | |
9572 | char *buf, | |
9573 | int *bufsiz, | |
9574 | const void *s390_todcmp, | |
9575 | int size) | |
9576 | { | |
9577 | char *note_name = "LINUX"; | |
9578 | return elfcore_write_note (abfd, buf, bufsiz, | |
9579 | note_name, NT_S390_TODCMP, s390_todcmp, size); | |
9580 | } | |
9581 | ||
9582 | char * | |
9583 | elfcore_write_s390_todpreg (bfd *abfd, | |
9584 | char *buf, | |
9585 | int *bufsiz, | |
9586 | const void *s390_todpreg, | |
9587 | int size) | |
9588 | { | |
9589 | char *note_name = "LINUX"; | |
9590 | return elfcore_write_note (abfd, buf, bufsiz, | |
9591 | note_name, NT_S390_TODPREG, s390_todpreg, size); | |
9592 | } | |
9593 | ||
9594 | char * | |
9595 | elfcore_write_s390_ctrs (bfd *abfd, | |
9596 | char *buf, | |
9597 | int *bufsiz, | |
9598 | const void *s390_ctrs, | |
9599 | int size) | |
9600 | { | |
9601 | char *note_name = "LINUX"; | |
9602 | return elfcore_write_note (abfd, buf, bufsiz, | |
9603 | note_name, NT_S390_CTRS, s390_ctrs, size); | |
9604 | } | |
9605 | ||
9606 | char * | |
9607 | elfcore_write_s390_prefix (bfd *abfd, | |
9608 | char *buf, | |
9609 | int *bufsiz, | |
9610 | const void *s390_prefix, | |
9611 | int size) | |
9612 | { | |
9613 | char *note_name = "LINUX"; | |
9614 | return elfcore_write_note (abfd, buf, bufsiz, | |
9615 | note_name, NT_S390_PREFIX, s390_prefix, size); | |
9616 | } | |
9617 | ||
355b81d9 UW |
9618 | char * |
9619 | elfcore_write_s390_last_break (bfd *abfd, | |
9620 | char *buf, | |
9621 | int *bufsiz, | |
9622 | const void *s390_last_break, | |
9623 | int size) | |
9624 | { | |
9625 | char *note_name = "LINUX"; | |
9626 | return elfcore_write_note (abfd, buf, bufsiz, | |
9627 | note_name, NT_S390_LAST_BREAK, | |
9628 | s390_last_break, size); | |
9629 | } | |
9630 | ||
9631 | char * | |
9632 | elfcore_write_s390_system_call (bfd *abfd, | |
9633 | char *buf, | |
9634 | int *bufsiz, | |
9635 | const void *s390_system_call, | |
9636 | int size) | |
9637 | { | |
9638 | char *note_name = "LINUX"; | |
9639 | return elfcore_write_note (abfd, buf, bufsiz, | |
9640 | note_name, NT_S390_SYSTEM_CALL, | |
9641 | s390_system_call, size); | |
9642 | } | |
9643 | ||
abb3f6cc NC |
9644 | char * |
9645 | elfcore_write_s390_tdb (bfd *abfd, | |
9646 | char *buf, | |
9647 | int *bufsiz, | |
9648 | const void *s390_tdb, | |
9649 | int size) | |
9650 | { | |
9651 | char *note_name = "LINUX"; | |
9652 | return elfcore_write_note (abfd, buf, bufsiz, | |
9653 | note_name, NT_S390_TDB, s390_tdb, size); | |
9654 | } | |
9655 | ||
faa9a424 UW |
9656 | char * |
9657 | elfcore_write_arm_vfp (bfd *abfd, | |
9658 | char *buf, | |
9659 | int *bufsiz, | |
9660 | const void *arm_vfp, | |
9661 | int size) | |
9662 | { | |
9663 | char *note_name = "LINUX"; | |
9664 | return elfcore_write_note (abfd, buf, bufsiz, | |
9665 | note_name, NT_ARM_VFP, arm_vfp, size); | |
9666 | } | |
9667 | ||
652451f8 YZ |
9668 | char * |
9669 | elfcore_write_aarch_tls (bfd *abfd, | |
9670 | char *buf, | |
9671 | int *bufsiz, | |
9672 | const void *aarch_tls, | |
9673 | int size) | |
9674 | { | |
9675 | char *note_name = "LINUX"; | |
9676 | return elfcore_write_note (abfd, buf, bufsiz, | |
9677 | note_name, NT_ARM_TLS, aarch_tls, size); | |
9678 | } | |
9679 | ||
9680 | char * | |
9681 | elfcore_write_aarch_hw_break (bfd *abfd, | |
9682 | char *buf, | |
9683 | int *bufsiz, | |
9684 | const void *aarch_hw_break, | |
9685 | int size) | |
9686 | { | |
9687 | char *note_name = "LINUX"; | |
9688 | return elfcore_write_note (abfd, buf, bufsiz, | |
9689 | note_name, NT_ARM_HW_BREAK, aarch_hw_break, size); | |
9690 | } | |
9691 | ||
9692 | char * | |
9693 | elfcore_write_aarch_hw_watch (bfd *abfd, | |
9694 | char *buf, | |
9695 | int *bufsiz, | |
9696 | const void *aarch_hw_watch, | |
9697 | int size) | |
9698 | { | |
9699 | char *note_name = "LINUX"; | |
9700 | return elfcore_write_note (abfd, buf, bufsiz, | |
9701 | note_name, NT_ARM_HW_WATCH, aarch_hw_watch, size); | |
9702 | } | |
9703 | ||
bb864ac1 CES |
9704 | char * |
9705 | elfcore_write_register_note (bfd *abfd, | |
9706 | char *buf, | |
9707 | int *bufsiz, | |
9708 | const char *section, | |
9709 | const void *data, | |
9710 | int size) | |
9711 | { | |
9712 | if (strcmp (section, ".reg2") == 0) | |
9713 | return elfcore_write_prfpreg (abfd, buf, bufsiz, data, size); | |
9714 | if (strcmp (section, ".reg-xfp") == 0) | |
9715 | return elfcore_write_prxfpreg (abfd, buf, bufsiz, data, size); | |
4339cae0 L |
9716 | if (strcmp (section, ".reg-xstate") == 0) |
9717 | return elfcore_write_xstatereg (abfd, buf, bufsiz, data, size); | |
bb864ac1 CES |
9718 | if (strcmp (section, ".reg-ppc-vmx") == 0) |
9719 | return elfcore_write_ppc_vmx (abfd, buf, bufsiz, data, size); | |
89eeb0bc LM |
9720 | if (strcmp (section, ".reg-ppc-vsx") == 0) |
9721 | return elfcore_write_ppc_vsx (abfd, buf, bufsiz, data, size); | |
0675e188 UW |
9722 | if (strcmp (section, ".reg-s390-high-gprs") == 0) |
9723 | return elfcore_write_s390_high_gprs (abfd, buf, bufsiz, data, size); | |
d7eeb400 MS |
9724 | if (strcmp (section, ".reg-s390-timer") == 0) |
9725 | return elfcore_write_s390_timer (abfd, buf, bufsiz, data, size); | |
9726 | if (strcmp (section, ".reg-s390-todcmp") == 0) | |
9727 | return elfcore_write_s390_todcmp (abfd, buf, bufsiz, data, size); | |
9728 | if (strcmp (section, ".reg-s390-todpreg") == 0) | |
9729 | return elfcore_write_s390_todpreg (abfd, buf, bufsiz, data, size); | |
9730 | if (strcmp (section, ".reg-s390-ctrs") == 0) | |
9731 | return elfcore_write_s390_ctrs (abfd, buf, bufsiz, data, size); | |
9732 | if (strcmp (section, ".reg-s390-prefix") == 0) | |
9733 | return elfcore_write_s390_prefix (abfd, buf, bufsiz, data, size); | |
355b81d9 UW |
9734 | if (strcmp (section, ".reg-s390-last-break") == 0) |
9735 | return elfcore_write_s390_last_break (abfd, buf, bufsiz, data, size); | |
9736 | if (strcmp (section, ".reg-s390-system-call") == 0) | |
9737 | return elfcore_write_s390_system_call (abfd, buf, bufsiz, data, size); | |
abb3f6cc NC |
9738 | if (strcmp (section, ".reg-s390-tdb") == 0) |
9739 | return elfcore_write_s390_tdb (abfd, buf, bufsiz, data, size); | |
faa9a424 UW |
9740 | if (strcmp (section, ".reg-arm-vfp") == 0) |
9741 | return elfcore_write_arm_vfp (abfd, buf, bufsiz, data, size); | |
652451f8 YZ |
9742 | if (strcmp (section, ".reg-aarch-tls") == 0) |
9743 | return elfcore_write_aarch_tls (abfd, buf, bufsiz, data, size); | |
9744 | if (strcmp (section, ".reg-aarch-hw-break") == 0) | |
9745 | return elfcore_write_aarch_hw_break (abfd, buf, bufsiz, data, size); | |
9746 | if (strcmp (section, ".reg-aarch-hw-watch") == 0) | |
9747 | return elfcore_write_aarch_hw_watch (abfd, buf, bufsiz, data, size); | |
bb864ac1 CES |
9748 | return NULL; |
9749 | } | |
9750 | ||
b34976b6 | 9751 | static bfd_boolean |
718175fa | 9752 | elf_parse_notes (bfd *abfd, char *buf, size_t size, file_ptr offset) |
252b5132 | 9753 | { |
c044fabd | 9754 | char *p; |
252b5132 | 9755 | |
252b5132 RH |
9756 | p = buf; |
9757 | while (p < buf + size) | |
9758 | { | |
c044fabd KH |
9759 | /* FIXME: bad alignment assumption. */ |
9760 | Elf_External_Note *xnp = (Elf_External_Note *) p; | |
252b5132 RH |
9761 | Elf_Internal_Note in; |
9762 | ||
baea7ef1 AM |
9763 | if (offsetof (Elf_External_Note, name) > buf - p + size) |
9764 | return FALSE; | |
9765 | ||
dc810e39 | 9766 | in.type = H_GET_32 (abfd, xnp->type); |
252b5132 | 9767 | |
dc810e39 | 9768 | in.namesz = H_GET_32 (abfd, xnp->namesz); |
252b5132 | 9769 | in.namedata = xnp->name; |
baea7ef1 AM |
9770 | if (in.namesz > buf - in.namedata + size) |
9771 | return FALSE; | |
252b5132 | 9772 | |
dc810e39 | 9773 | in.descsz = H_GET_32 (abfd, xnp->descsz); |
252b5132 RH |
9774 | in.descdata = in.namedata + BFD_ALIGN (in.namesz, 4); |
9775 | in.descpos = offset + (in.descdata - buf); | |
baea7ef1 AM |
9776 | if (in.descsz != 0 |
9777 | && (in.descdata >= buf + size | |
9778 | || in.descsz > buf - in.descdata + size)) | |
9779 | return FALSE; | |
252b5132 | 9780 | |
718175fa JK |
9781 | switch (bfd_get_format (abfd)) |
9782 | { | |
9783 | default: | |
9784 | return TRUE; | |
9785 | ||
9786 | case bfd_core: | |
9787 | if (CONST_STRNEQ (in.namedata, "NetBSD-CORE")) | |
9788 | { | |
9789 | if (! elfcore_grok_netbsd_note (abfd, &in)) | |
9790 | return FALSE; | |
9791 | } | |
67cc5033 MK |
9792 | else if (CONST_STRNEQ (in.namedata, "OpenBSD")) |
9793 | { | |
9794 | if (! elfcore_grok_openbsd_note (abfd, &in)) | |
9795 | return FALSE; | |
9796 | } | |
718175fa JK |
9797 | else if (CONST_STRNEQ (in.namedata, "QNX")) |
9798 | { | |
9799 | if (! elfcore_grok_nto_note (abfd, &in)) | |
9800 | return FALSE; | |
9801 | } | |
b15fa79e AM |
9802 | else if (CONST_STRNEQ (in.namedata, "SPU/")) |
9803 | { | |
9804 | if (! elfcore_grok_spu_note (abfd, &in)) | |
9805 | return FALSE; | |
9806 | } | |
718175fa JK |
9807 | else |
9808 | { | |
9809 | if (! elfcore_grok_note (abfd, &in)) | |
9810 | return FALSE; | |
9811 | } | |
9812 | break; | |
9813 | ||
9814 | case bfd_object: | |
9815 | if (in.namesz == sizeof "GNU" && strcmp (in.namedata, "GNU") == 0) | |
9816 | { | |
9817 | if (! elfobj_grok_gnu_note (abfd, &in)) | |
9818 | return FALSE; | |
9819 | } | |
e21e5835 NC |
9820 | else if (in.namesz == sizeof "stapsdt" |
9821 | && strcmp (in.namedata, "stapsdt") == 0) | |
9822 | { | |
9823 | if (! elfobj_grok_stapsdt_note (abfd, &in)) | |
9824 | return FALSE; | |
9825 | } | |
718175fa | 9826 | break; |
08a40648 | 9827 | } |
252b5132 RH |
9828 | |
9829 | p = in.descdata + BFD_ALIGN (in.descsz, 4); | |
9830 | } | |
9831 | ||
718175fa JK |
9832 | return TRUE; |
9833 | } | |
9834 | ||
9835 | static bfd_boolean | |
9836 | elf_read_notes (bfd *abfd, file_ptr offset, bfd_size_type size) | |
9837 | { | |
9838 | char *buf; | |
9839 | ||
9840 | if (size <= 0) | |
9841 | return TRUE; | |
9842 | ||
9843 | if (bfd_seek (abfd, offset, SEEK_SET) != 0) | |
9844 | return FALSE; | |
9845 | ||
a50b1753 | 9846 | buf = (char *) bfd_malloc (size); |
718175fa JK |
9847 | if (buf == NULL) |
9848 | return FALSE; | |
9849 | ||
9850 | if (bfd_bread (buf, size, abfd) != size | |
9851 | || !elf_parse_notes (abfd, buf, size, offset)) | |
9852 | { | |
9853 | free (buf); | |
9854 | return FALSE; | |
9855 | } | |
9856 | ||
252b5132 | 9857 | free (buf); |
b34976b6 | 9858 | return TRUE; |
252b5132 | 9859 | } |
98d8431c JB |
9860 | \f |
9861 | /* Providing external access to the ELF program header table. */ | |
9862 | ||
9863 | /* Return an upper bound on the number of bytes required to store a | |
9864 | copy of ABFD's program header table entries. Return -1 if an error | |
9865 | occurs; bfd_get_error will return an appropriate code. */ | |
c044fabd | 9866 | |
98d8431c | 9867 | long |
217aa764 | 9868 | bfd_get_elf_phdr_upper_bound (bfd *abfd) |
98d8431c JB |
9869 | { |
9870 | if (abfd->xvec->flavour != bfd_target_elf_flavour) | |
9871 | { | |
9872 | bfd_set_error (bfd_error_wrong_format); | |
9873 | return -1; | |
9874 | } | |
9875 | ||
936e320b | 9876 | return elf_elfheader (abfd)->e_phnum * sizeof (Elf_Internal_Phdr); |
98d8431c JB |
9877 | } |
9878 | ||
98d8431c JB |
9879 | /* Copy ABFD's program header table entries to *PHDRS. The entries |
9880 | will be stored as an array of Elf_Internal_Phdr structures, as | |
9881 | defined in include/elf/internal.h. To find out how large the | |
9882 | buffer needs to be, call bfd_get_elf_phdr_upper_bound. | |
9883 | ||
9884 | Return the number of program header table entries read, or -1 if an | |
9885 | error occurs; bfd_get_error will return an appropriate code. */ | |
c044fabd | 9886 | |
98d8431c | 9887 | int |
217aa764 | 9888 | bfd_get_elf_phdrs (bfd *abfd, void *phdrs) |
98d8431c JB |
9889 | { |
9890 | int num_phdrs; | |
9891 | ||
9892 | if (abfd->xvec->flavour != bfd_target_elf_flavour) | |
9893 | { | |
9894 | bfd_set_error (bfd_error_wrong_format); | |
9895 | return -1; | |
9896 | } | |
9897 | ||
9898 | num_phdrs = elf_elfheader (abfd)->e_phnum; | |
c044fabd | 9899 | memcpy (phdrs, elf_tdata (abfd)->phdr, |
98d8431c JB |
9900 | num_phdrs * sizeof (Elf_Internal_Phdr)); |
9901 | ||
9902 | return num_phdrs; | |
9903 | } | |
ae4221d7 | 9904 | |
db6751f2 | 9905 | enum elf_reloc_type_class |
7e612e98 AM |
9906 | _bfd_elf_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED, |
9907 | const asection *rel_sec ATTRIBUTE_UNUSED, | |
9908 | const Elf_Internal_Rela *rela ATTRIBUTE_UNUSED) | |
db6751f2 JJ |
9909 | { |
9910 | return reloc_class_normal; | |
9911 | } | |
f8df10f4 | 9912 | |
47d9a591 | 9913 | /* For RELA architectures, return the relocation value for a |
f8df10f4 JJ |
9914 | relocation against a local symbol. */ |
9915 | ||
9916 | bfd_vma | |
217aa764 AM |
9917 | _bfd_elf_rela_local_sym (bfd *abfd, |
9918 | Elf_Internal_Sym *sym, | |
8517fae7 | 9919 | asection **psec, |
217aa764 | 9920 | Elf_Internal_Rela *rel) |
f8df10f4 | 9921 | { |
8517fae7 | 9922 | asection *sec = *psec; |
f8df10f4 JJ |
9923 | bfd_vma relocation; |
9924 | ||
9925 | relocation = (sec->output_section->vma | |
9926 | + sec->output_offset | |
9927 | + sym->st_value); | |
9928 | if ((sec->flags & SEC_MERGE) | |
c629eae0 | 9929 | && ELF_ST_TYPE (sym->st_info) == STT_SECTION |
dbaa2011 | 9930 | && sec->sec_info_type == SEC_INFO_TYPE_MERGE) |
f8df10f4 | 9931 | { |
f8df10f4 | 9932 | rel->r_addend = |
8517fae7 | 9933 | _bfd_merged_section_offset (abfd, psec, |
65765700 | 9934 | elf_section_data (sec)->sec_info, |
753731ee AM |
9935 | sym->st_value + rel->r_addend); |
9936 | if (sec != *psec) | |
9937 | { | |
9938 | /* If we have changed the section, and our original section is | |
9939 | marked with SEC_EXCLUDE, it means that the original | |
9940 | SEC_MERGE section has been completely subsumed in some | |
9941 | other SEC_MERGE section. In this case, we need to leave | |
9942 | some info around for --emit-relocs. */ | |
9943 | if ((sec->flags & SEC_EXCLUDE) != 0) | |
9944 | sec->kept_section = *psec; | |
9945 | sec = *psec; | |
9946 | } | |
8517fae7 AM |
9947 | rel->r_addend -= relocation; |
9948 | rel->r_addend += sec->output_section->vma + sec->output_offset; | |
f8df10f4 JJ |
9949 | } |
9950 | return relocation; | |
9951 | } | |
c629eae0 JJ |
9952 | |
9953 | bfd_vma | |
217aa764 AM |
9954 | _bfd_elf_rel_local_sym (bfd *abfd, |
9955 | Elf_Internal_Sym *sym, | |
9956 | asection **psec, | |
9957 | bfd_vma addend) | |
47d9a591 | 9958 | { |
c629eae0 JJ |
9959 | asection *sec = *psec; |
9960 | ||
dbaa2011 | 9961 | if (sec->sec_info_type != SEC_INFO_TYPE_MERGE) |
c629eae0 JJ |
9962 | return sym->st_value + addend; |
9963 | ||
9964 | return _bfd_merged_section_offset (abfd, psec, | |
65765700 | 9965 | elf_section_data (sec)->sec_info, |
753731ee | 9966 | sym->st_value + addend); |
c629eae0 JJ |
9967 | } |
9968 | ||
9969 | bfd_vma | |
217aa764 | 9970 | _bfd_elf_section_offset (bfd *abfd, |
92e4ec35 | 9971 | struct bfd_link_info *info, |
217aa764 AM |
9972 | asection *sec, |
9973 | bfd_vma offset) | |
c629eae0 | 9974 | { |
68bfbfcc | 9975 | switch (sec->sec_info_type) |
65765700 | 9976 | { |
dbaa2011 | 9977 | case SEC_INFO_TYPE_STABS: |
eea6121a AM |
9978 | return _bfd_stab_section_offset (sec, elf_section_data (sec)->sec_info, |
9979 | offset); | |
dbaa2011 | 9980 | case SEC_INFO_TYPE_EH_FRAME: |
92e4ec35 | 9981 | return _bfd_elf_eh_frame_section_offset (abfd, info, sec, offset); |
65765700 | 9982 | default: |
310fd250 L |
9983 | if ((sec->flags & SEC_ELF_REVERSE_COPY) != 0) |
9984 | { | |
9985 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
9986 | bfd_size_type address_size = bed->s->arch_size / 8; | |
9987 | offset = sec->size - offset - address_size; | |
9988 | } | |
65765700 JJ |
9989 | return offset; |
9990 | } | |
c629eae0 | 9991 | } |
3333a7c3 RM |
9992 | \f |
9993 | /* Create a new BFD as if by bfd_openr. Rather than opening a file, | |
9994 | reconstruct an ELF file by reading the segments out of remote memory | |
9995 | based on the ELF file header at EHDR_VMA and the ELF program headers it | |
9996 | points to. If not null, *LOADBASEP is filled in with the difference | |
9997 | between the VMAs from which the segments were read, and the VMAs the | |
9998 | file headers (and hence BFD's idea of each section's VMA) put them at. | |
9999 | ||
10000 | The function TARGET_READ_MEMORY is called to copy LEN bytes from the | |
10001 | remote memory at target address VMA into the local buffer at MYADDR; it | |
10002 | should return zero on success or an `errno' code on failure. TEMPL must | |
10003 | be a BFD for an ELF target with the word size and byte order found in | |
10004 | the remote memory. */ | |
10005 | ||
10006 | bfd * | |
217aa764 AM |
10007 | bfd_elf_bfd_from_remote_memory |
10008 | (bfd *templ, | |
10009 | bfd_vma ehdr_vma, | |
f0a5d95a | 10010 | bfd_size_type size, |
217aa764 | 10011 | bfd_vma *loadbasep, |
fe78531d | 10012 | int (*target_read_memory) (bfd_vma, bfd_byte *, bfd_size_type)) |
3333a7c3 RM |
10013 | { |
10014 | return (*get_elf_backend_data (templ)->elf_backend_bfd_from_remote_memory) | |
5979d6b6 | 10015 | (templ, ehdr_vma, size, loadbasep, target_read_memory); |
3333a7c3 | 10016 | } |
4c45e5c9 JJ |
10017 | \f |
10018 | long | |
c9727e01 AM |
10019 | _bfd_elf_get_synthetic_symtab (bfd *abfd, |
10020 | long symcount ATTRIBUTE_UNUSED, | |
10021 | asymbol **syms ATTRIBUTE_UNUSED, | |
8615f3f2 | 10022 | long dynsymcount, |
c9727e01 AM |
10023 | asymbol **dynsyms, |
10024 | asymbol **ret) | |
4c45e5c9 JJ |
10025 | { |
10026 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
10027 | asection *relplt; | |
10028 | asymbol *s; | |
10029 | const char *relplt_name; | |
10030 | bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean); | |
10031 | arelent *p; | |
10032 | long count, i, n; | |
10033 | size_t size; | |
10034 | Elf_Internal_Shdr *hdr; | |
10035 | char *names; | |
10036 | asection *plt; | |
10037 | ||
8615f3f2 AM |
10038 | *ret = NULL; |
10039 | ||
90e3cdf2 JJ |
10040 | if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0) |
10041 | return 0; | |
10042 | ||
8615f3f2 AM |
10043 | if (dynsymcount <= 0) |
10044 | return 0; | |
10045 | ||
4c45e5c9 JJ |
10046 | if (!bed->plt_sym_val) |
10047 | return 0; | |
10048 | ||
10049 | relplt_name = bed->relplt_name; | |
10050 | if (relplt_name == NULL) | |
d35fd659 | 10051 | relplt_name = bed->rela_plts_and_copies_p ? ".rela.plt" : ".rel.plt"; |
4c45e5c9 JJ |
10052 | relplt = bfd_get_section_by_name (abfd, relplt_name); |
10053 | if (relplt == NULL) | |
10054 | return 0; | |
10055 | ||
10056 | hdr = &elf_section_data (relplt)->this_hdr; | |
10057 | if (hdr->sh_link != elf_dynsymtab (abfd) | |
10058 | || (hdr->sh_type != SHT_REL && hdr->sh_type != SHT_RELA)) | |
10059 | return 0; | |
10060 | ||
10061 | plt = bfd_get_section_by_name (abfd, ".plt"); | |
10062 | if (plt == NULL) | |
10063 | return 0; | |
10064 | ||
10065 | slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table; | |
c9727e01 | 10066 | if (! (*slurp_relocs) (abfd, relplt, dynsyms, TRUE)) |
4c45e5c9 JJ |
10067 | return -1; |
10068 | ||
eea6121a | 10069 | count = relplt->size / hdr->sh_entsize; |
4c45e5c9 JJ |
10070 | size = count * sizeof (asymbol); |
10071 | p = relplt->relocation; | |
cb53bf42 | 10072 | for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel) |
041de40d AM |
10073 | { |
10074 | size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt"); | |
10075 | if (p->addend != 0) | |
10076 | { | |
10077 | #ifdef BFD64 | |
10078 | size += sizeof ("+0x") - 1 + 8 + 8 * (bed->s->elfclass == ELFCLASS64); | |
10079 | #else | |
10080 | size += sizeof ("+0x") - 1 + 8; | |
10081 | #endif | |
10082 | } | |
10083 | } | |
4c45e5c9 | 10084 | |
a50b1753 | 10085 | s = *ret = (asymbol *) bfd_malloc (size); |
4c45e5c9 JJ |
10086 | if (s == NULL) |
10087 | return -1; | |
10088 | ||
10089 | names = (char *) (s + count); | |
10090 | p = relplt->relocation; | |
10091 | n = 0; | |
cb53bf42 | 10092 | for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel) |
4c45e5c9 JJ |
10093 | { |
10094 | size_t len; | |
10095 | bfd_vma addr; | |
10096 | ||
10097 | addr = bed->plt_sym_val (i, plt, p); | |
10098 | if (addr == (bfd_vma) -1) | |
10099 | continue; | |
10100 | ||
10101 | *s = **p->sym_ptr_ptr; | |
65a7a66f AM |
10102 | /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since |
10103 | we are defining a symbol, ensure one of them is set. */ | |
10104 | if ((s->flags & BSF_LOCAL) == 0) | |
10105 | s->flags |= BSF_GLOBAL; | |
6ba2a415 | 10106 | s->flags |= BSF_SYNTHETIC; |
4c45e5c9 JJ |
10107 | s->section = plt; |
10108 | s->value = addr - plt->vma; | |
10109 | s->name = names; | |
8f39ba8e | 10110 | s->udata.p = NULL; |
4c45e5c9 JJ |
10111 | len = strlen ((*p->sym_ptr_ptr)->name); |
10112 | memcpy (names, (*p->sym_ptr_ptr)->name, len); | |
10113 | names += len; | |
041de40d AM |
10114 | if (p->addend != 0) |
10115 | { | |
1d770845 | 10116 | char buf[30], *a; |
d324f6d6 | 10117 | |
041de40d AM |
10118 | memcpy (names, "+0x", sizeof ("+0x") - 1); |
10119 | names += sizeof ("+0x") - 1; | |
1d770845 L |
10120 | bfd_sprintf_vma (abfd, buf, p->addend); |
10121 | for (a = buf; *a == '0'; ++a) | |
10122 | ; | |
10123 | len = strlen (a); | |
10124 | memcpy (names, a, len); | |
10125 | names += len; | |
041de40d | 10126 | } |
4c45e5c9 JJ |
10127 | memcpy (names, "@plt", sizeof ("@plt")); |
10128 | names += sizeof ("@plt"); | |
8f39ba8e | 10129 | ++s, ++n; |
4c45e5c9 JJ |
10130 | } |
10131 | ||
10132 | return n; | |
10133 | } | |
3d7f7666 | 10134 | |
3b22753a L |
10135 | /* It is only used by x86-64 so far. */ |
10136 | asection _bfd_elf_large_com_section | |
10137 | = BFD_FAKE_SECTION (_bfd_elf_large_com_section, | |
f592407e | 10138 | SEC_IS_COMMON, NULL, "LARGE_COMMON", 0); |
ecca9871 | 10139 | |
d1036acb | 10140 | void |
78245035 L |
10141 | _bfd_elf_post_process_headers (bfd * abfd, |
10142 | struct bfd_link_info * link_info ATTRIBUTE_UNUSED) | |
d1036acb L |
10143 | { |
10144 | Elf_Internal_Ehdr * i_ehdrp; /* ELF file header, internal form. */ | |
10145 | ||
10146 | i_ehdrp = elf_elfheader (abfd); | |
10147 | ||
10148 | i_ehdrp->e_ident[EI_OSABI] = get_elf_backend_data (abfd)->elf_osabi; | |
d8045f23 NC |
10149 | |
10150 | /* To make things simpler for the loader on Linux systems we set the | |
9c55345c | 10151 | osabi field to ELFOSABI_GNU if the binary contains symbols of |
f64b2e8d | 10152 | the STT_GNU_IFUNC type or STB_GNU_UNIQUE binding. */ |
d8045f23 | 10153 | if (i_ehdrp->e_ident[EI_OSABI] == ELFOSABI_NONE |
f64b2e8d | 10154 | && elf_tdata (abfd)->has_gnu_symbols) |
9c55345c | 10155 | i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_GNU; |
d1036acb | 10156 | } |
fcb93ecf PB |
10157 | |
10158 | ||
10159 | /* Return TRUE for ELF symbol types that represent functions. | |
10160 | This is the default version of this function, which is sufficient for | |
d8045f23 | 10161 | most targets. It returns true if TYPE is STT_FUNC or STT_GNU_IFUNC. */ |
fcb93ecf PB |
10162 | |
10163 | bfd_boolean | |
10164 | _bfd_elf_is_function_type (unsigned int type) | |
10165 | { | |
d8045f23 NC |
10166 | return (type == STT_FUNC |
10167 | || type == STT_GNU_IFUNC); | |
fcb93ecf | 10168 | } |
9f296da3 | 10169 | |
aef36ac1 AM |
10170 | /* If the ELF symbol SYM might be a function in SEC, return the |
10171 | function size and set *CODE_OFF to the function's entry point, | |
10172 | otherwise return zero. */ | |
9f296da3 | 10173 | |
aef36ac1 AM |
10174 | bfd_size_type |
10175 | _bfd_elf_maybe_function_sym (const asymbol *sym, asection *sec, | |
10176 | bfd_vma *code_off) | |
9f296da3 | 10177 | { |
aef36ac1 AM |
10178 | bfd_size_type size; |
10179 | ||
ff9e0f5b | 10180 | if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT |
aef36ac1 AM |
10181 | | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0 |
10182 | || sym->section != sec) | |
10183 | return 0; | |
ff9e0f5b | 10184 | |
ff9e0f5b | 10185 | *code_off = sym->value; |
aef36ac1 AM |
10186 | size = 0; |
10187 | if (!(sym->flags & BSF_SYNTHETIC)) | |
10188 | size = ((elf_symbol_type *) sym)->internal_elf_sym.st_size; | |
10189 | if (size == 0) | |
10190 | size = 1; | |
10191 | return size; | |
9f296da3 | 10192 | } |