Commit | Line | Data |
---|---|---|
252b5132 | 1 | /* ELF executable support for BFD. |
340b6d91 | 2 | |
b90efa5b | 3 | Copyright (C) 1993-2015 Free Software Foundation, Inc. |
252b5132 | 4 | |
5e8d7549 | 5 | This file is part of BFD, the Binary File Descriptor library. |
252b5132 | 6 | |
5e8d7549 NC |
7 | This program is free software; you can redistribute it and/or modify |
8 | it under the terms of the GNU General Public License as published by | |
cd123cb7 | 9 | the Free Software Foundation; either version 3 of the License, or |
5e8d7549 | 10 | (at your option) any later version. |
252b5132 | 11 | |
5e8d7549 NC |
12 | This program is distributed in the hope that it will be useful, |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
252b5132 | 16 | |
5e8d7549 | 17 | You should have received a copy of the GNU General Public License |
b34976b6 | 18 | along with this program; if not, write to the Free Software |
cd123cb7 NC |
19 | Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, |
20 | MA 02110-1301, USA. */ | |
21 | ||
252b5132 | 22 | |
1b74d094 BW |
23 | /* |
24 | SECTION | |
252b5132 RH |
25 | ELF backends |
26 | ||
27 | BFD support for ELF formats is being worked on. | |
28 | Currently, the best supported back ends are for sparc and i386 | |
29 | (running svr4 or Solaris 2). | |
30 | ||
31 | Documentation of the internals of the support code still needs | |
32 | to be written. The code is changing quickly enough that we | |
661a3fd4 | 33 | haven't bothered yet. */ |
252b5132 | 34 | |
7ee38065 MS |
35 | /* For sparc64-cross-sparc32. */ |
36 | #define _SYSCALL32 | |
252b5132 | 37 | #include "sysdep.h" |
3db64b00 | 38 | #include "bfd.h" |
252b5132 RH |
39 | #include "bfdlink.h" |
40 | #include "libbfd.h" | |
41 | #define ARCH_SIZE 0 | |
42 | #include "elf-bfd.h" | |
e0e8c97f | 43 | #include "libiberty.h" |
ff59fc36 | 44 | #include "safe-ctype.h" |
70a38d42 | 45 | #include "elf-linux-psinfo.h" |
252b5132 | 46 | |
8bc7f138 L |
47 | #ifdef CORE_HEADER |
48 | #include CORE_HEADER | |
49 | #endif | |
50 | ||
217aa764 | 51 | static int elf_sort_sections (const void *, const void *); |
c84fca4d | 52 | static bfd_boolean assign_file_positions_except_relocs (bfd *, struct bfd_link_info *); |
217aa764 AM |
53 | static bfd_boolean prep_headers (bfd *); |
54 | static bfd_boolean swap_out_syms (bfd *, struct bfd_strtab_hash **, int) ; | |
718175fa JK |
55 | static bfd_boolean elf_read_notes (bfd *, file_ptr, bfd_size_type) ; |
56 | static bfd_boolean elf_parse_notes (bfd *abfd, char *buf, size_t size, | |
57 | file_ptr offset); | |
50b2bdb7 | 58 | |
252b5132 RH |
59 | /* Swap version information in and out. The version information is |
60 | currently size independent. If that ever changes, this code will | |
61 | need to move into elfcode.h. */ | |
62 | ||
63 | /* Swap in a Verdef structure. */ | |
64 | ||
65 | void | |
217aa764 AM |
66 | _bfd_elf_swap_verdef_in (bfd *abfd, |
67 | const Elf_External_Verdef *src, | |
68 | Elf_Internal_Verdef *dst) | |
252b5132 | 69 | { |
dc810e39 AM |
70 | dst->vd_version = H_GET_16 (abfd, src->vd_version); |
71 | dst->vd_flags = H_GET_16 (abfd, src->vd_flags); | |
72 | dst->vd_ndx = H_GET_16 (abfd, src->vd_ndx); | |
73 | dst->vd_cnt = H_GET_16 (abfd, src->vd_cnt); | |
74 | dst->vd_hash = H_GET_32 (abfd, src->vd_hash); | |
75 | dst->vd_aux = H_GET_32 (abfd, src->vd_aux); | |
76 | dst->vd_next = H_GET_32 (abfd, src->vd_next); | |
252b5132 RH |
77 | } |
78 | ||
79 | /* Swap out a Verdef structure. */ | |
80 | ||
81 | void | |
217aa764 AM |
82 | _bfd_elf_swap_verdef_out (bfd *abfd, |
83 | const Elf_Internal_Verdef *src, | |
84 | Elf_External_Verdef *dst) | |
252b5132 | 85 | { |
dc810e39 AM |
86 | H_PUT_16 (abfd, src->vd_version, dst->vd_version); |
87 | H_PUT_16 (abfd, src->vd_flags, dst->vd_flags); | |
88 | H_PUT_16 (abfd, src->vd_ndx, dst->vd_ndx); | |
89 | H_PUT_16 (abfd, src->vd_cnt, dst->vd_cnt); | |
90 | H_PUT_32 (abfd, src->vd_hash, dst->vd_hash); | |
91 | H_PUT_32 (abfd, src->vd_aux, dst->vd_aux); | |
92 | H_PUT_32 (abfd, src->vd_next, dst->vd_next); | |
252b5132 RH |
93 | } |
94 | ||
95 | /* Swap in a Verdaux structure. */ | |
96 | ||
97 | void | |
217aa764 AM |
98 | _bfd_elf_swap_verdaux_in (bfd *abfd, |
99 | const Elf_External_Verdaux *src, | |
100 | Elf_Internal_Verdaux *dst) | |
252b5132 | 101 | { |
dc810e39 AM |
102 | dst->vda_name = H_GET_32 (abfd, src->vda_name); |
103 | dst->vda_next = H_GET_32 (abfd, src->vda_next); | |
252b5132 RH |
104 | } |
105 | ||
106 | /* Swap out a Verdaux structure. */ | |
107 | ||
108 | void | |
217aa764 AM |
109 | _bfd_elf_swap_verdaux_out (bfd *abfd, |
110 | const Elf_Internal_Verdaux *src, | |
111 | Elf_External_Verdaux *dst) | |
252b5132 | 112 | { |
dc810e39 AM |
113 | H_PUT_32 (abfd, src->vda_name, dst->vda_name); |
114 | H_PUT_32 (abfd, src->vda_next, dst->vda_next); | |
252b5132 RH |
115 | } |
116 | ||
117 | /* Swap in a Verneed structure. */ | |
118 | ||
119 | void | |
217aa764 AM |
120 | _bfd_elf_swap_verneed_in (bfd *abfd, |
121 | const Elf_External_Verneed *src, | |
122 | Elf_Internal_Verneed *dst) | |
252b5132 | 123 | { |
dc810e39 AM |
124 | dst->vn_version = H_GET_16 (abfd, src->vn_version); |
125 | dst->vn_cnt = H_GET_16 (abfd, src->vn_cnt); | |
126 | dst->vn_file = H_GET_32 (abfd, src->vn_file); | |
127 | dst->vn_aux = H_GET_32 (abfd, src->vn_aux); | |
128 | dst->vn_next = H_GET_32 (abfd, src->vn_next); | |
252b5132 RH |
129 | } |
130 | ||
131 | /* Swap out a Verneed structure. */ | |
132 | ||
133 | void | |
217aa764 AM |
134 | _bfd_elf_swap_verneed_out (bfd *abfd, |
135 | const Elf_Internal_Verneed *src, | |
136 | Elf_External_Verneed *dst) | |
252b5132 | 137 | { |
dc810e39 AM |
138 | H_PUT_16 (abfd, src->vn_version, dst->vn_version); |
139 | H_PUT_16 (abfd, src->vn_cnt, dst->vn_cnt); | |
140 | H_PUT_32 (abfd, src->vn_file, dst->vn_file); | |
141 | H_PUT_32 (abfd, src->vn_aux, dst->vn_aux); | |
142 | H_PUT_32 (abfd, src->vn_next, dst->vn_next); | |
252b5132 RH |
143 | } |
144 | ||
145 | /* Swap in a Vernaux structure. */ | |
146 | ||
147 | void | |
217aa764 AM |
148 | _bfd_elf_swap_vernaux_in (bfd *abfd, |
149 | const Elf_External_Vernaux *src, | |
150 | Elf_Internal_Vernaux *dst) | |
252b5132 | 151 | { |
dc810e39 AM |
152 | dst->vna_hash = H_GET_32 (abfd, src->vna_hash); |
153 | dst->vna_flags = H_GET_16 (abfd, src->vna_flags); | |
154 | dst->vna_other = H_GET_16 (abfd, src->vna_other); | |
155 | dst->vna_name = H_GET_32 (abfd, src->vna_name); | |
156 | dst->vna_next = H_GET_32 (abfd, src->vna_next); | |
252b5132 RH |
157 | } |
158 | ||
159 | /* Swap out a Vernaux structure. */ | |
160 | ||
161 | void | |
217aa764 AM |
162 | _bfd_elf_swap_vernaux_out (bfd *abfd, |
163 | const Elf_Internal_Vernaux *src, | |
164 | Elf_External_Vernaux *dst) | |
252b5132 | 165 | { |
dc810e39 AM |
166 | H_PUT_32 (abfd, src->vna_hash, dst->vna_hash); |
167 | H_PUT_16 (abfd, src->vna_flags, dst->vna_flags); | |
168 | H_PUT_16 (abfd, src->vna_other, dst->vna_other); | |
169 | H_PUT_32 (abfd, src->vna_name, dst->vna_name); | |
170 | H_PUT_32 (abfd, src->vna_next, dst->vna_next); | |
252b5132 RH |
171 | } |
172 | ||
173 | /* Swap in a Versym structure. */ | |
174 | ||
175 | void | |
217aa764 AM |
176 | _bfd_elf_swap_versym_in (bfd *abfd, |
177 | const Elf_External_Versym *src, | |
178 | Elf_Internal_Versym *dst) | |
252b5132 | 179 | { |
dc810e39 | 180 | dst->vs_vers = H_GET_16 (abfd, src->vs_vers); |
252b5132 RH |
181 | } |
182 | ||
183 | /* Swap out a Versym structure. */ | |
184 | ||
185 | void | |
217aa764 AM |
186 | _bfd_elf_swap_versym_out (bfd *abfd, |
187 | const Elf_Internal_Versym *src, | |
188 | Elf_External_Versym *dst) | |
252b5132 | 189 | { |
dc810e39 | 190 | H_PUT_16 (abfd, src->vs_vers, dst->vs_vers); |
252b5132 RH |
191 | } |
192 | ||
193 | /* Standard ELF hash function. Do not change this function; you will | |
194 | cause invalid hash tables to be generated. */ | |
3a99b017 | 195 | |
252b5132 | 196 | unsigned long |
217aa764 | 197 | bfd_elf_hash (const char *namearg) |
252b5132 | 198 | { |
3a99b017 | 199 | const unsigned char *name = (const unsigned char *) namearg; |
252b5132 RH |
200 | unsigned long h = 0; |
201 | unsigned long g; | |
202 | int ch; | |
203 | ||
204 | while ((ch = *name++) != '\0') | |
205 | { | |
206 | h = (h << 4) + ch; | |
207 | if ((g = (h & 0xf0000000)) != 0) | |
208 | { | |
209 | h ^= g >> 24; | |
210 | /* The ELF ABI says `h &= ~g', but this is equivalent in | |
211 | this case and on some machines one insn instead of two. */ | |
212 | h ^= g; | |
213 | } | |
214 | } | |
32dfa85d | 215 | return h & 0xffffffff; |
252b5132 RH |
216 | } |
217 | ||
fdc90cb4 JJ |
218 | /* DT_GNU_HASH hash function. Do not change this function; you will |
219 | cause invalid hash tables to be generated. */ | |
220 | ||
221 | unsigned long | |
222 | bfd_elf_gnu_hash (const char *namearg) | |
223 | { | |
224 | const unsigned char *name = (const unsigned char *) namearg; | |
225 | unsigned long h = 5381; | |
226 | unsigned char ch; | |
227 | ||
228 | while ((ch = *name++) != '\0') | |
229 | h = (h << 5) + h + ch; | |
230 | return h & 0xffffffff; | |
231 | } | |
232 | ||
0c8d6e5c AM |
233 | /* Create a tdata field OBJECT_SIZE bytes in length, zeroed out and with |
234 | the object_id field of an elf_obj_tdata field set to OBJECT_ID. */ | |
b34976b6 | 235 | bfd_boolean |
0c8d6e5c | 236 | bfd_elf_allocate_object (bfd *abfd, |
0ffa91dd | 237 | size_t object_size, |
4dfe6ac6 | 238 | enum elf_target_id object_id) |
252b5132 | 239 | { |
0ffa91dd NC |
240 | BFD_ASSERT (object_size >= sizeof (struct elf_obj_tdata)); |
241 | abfd->tdata.any = bfd_zalloc (abfd, object_size); | |
242 | if (abfd->tdata.any == NULL) | |
243 | return FALSE; | |
252b5132 | 244 | |
0ffa91dd | 245 | elf_object_id (abfd) = object_id; |
c0355132 AM |
246 | if (abfd->direction != read_direction) |
247 | { | |
248 | struct output_elf_obj_tdata *o = bfd_zalloc (abfd, sizeof *o); | |
249 | if (o == NULL) | |
250 | return FALSE; | |
251 | elf_tdata (abfd)->o = o; | |
252 | elf_program_header_size (abfd) = (bfd_size_type) -1; | |
253 | } | |
b34976b6 | 254 | return TRUE; |
252b5132 RH |
255 | } |
256 | ||
0ffa91dd NC |
257 | |
258 | bfd_boolean | |
ae95ffa6 | 259 | bfd_elf_make_object (bfd *abfd) |
0ffa91dd | 260 | { |
ae95ffa6 | 261 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
0ffa91dd | 262 | return bfd_elf_allocate_object (abfd, sizeof (struct elf_obj_tdata), |
ae95ffa6 | 263 | bed->target_id); |
0ffa91dd NC |
264 | } |
265 | ||
b34976b6 | 266 | bfd_boolean |
217aa764 | 267 | bfd_elf_mkcorefile (bfd *abfd) |
252b5132 | 268 | { |
c044fabd | 269 | /* I think this can be done just like an object file. */ |
228e534f AM |
270 | if (!abfd->xvec->_bfd_set_format[(int) bfd_object] (abfd)) |
271 | return FALSE; | |
272 | elf_tdata (abfd)->core = bfd_zalloc (abfd, sizeof (*elf_tdata (abfd)->core)); | |
273 | return elf_tdata (abfd)->core != NULL; | |
252b5132 RH |
274 | } |
275 | ||
72a80a16 | 276 | static char * |
217aa764 | 277 | bfd_elf_get_str_section (bfd *abfd, unsigned int shindex) |
252b5132 RH |
278 | { |
279 | Elf_Internal_Shdr **i_shdrp; | |
f075ee0c | 280 | bfd_byte *shstrtab = NULL; |
dc810e39 AM |
281 | file_ptr offset; |
282 | bfd_size_type shstrtabsize; | |
252b5132 RH |
283 | |
284 | i_shdrp = elf_elfsections (abfd); | |
74f2e02b AM |
285 | if (i_shdrp == 0 |
286 | || shindex >= elf_numsections (abfd) | |
287 | || i_shdrp[shindex] == 0) | |
f075ee0c | 288 | return NULL; |
252b5132 | 289 | |
f075ee0c | 290 | shstrtab = i_shdrp[shindex]->contents; |
252b5132 RH |
291 | if (shstrtab == NULL) |
292 | { | |
c044fabd | 293 | /* No cached one, attempt to read, and cache what we read. */ |
252b5132 RH |
294 | offset = i_shdrp[shindex]->sh_offset; |
295 | shstrtabsize = i_shdrp[shindex]->sh_size; | |
c6c60d09 JJ |
296 | |
297 | /* Allocate and clear an extra byte at the end, to prevent crashes | |
298 | in case the string table is not terminated. */ | |
3471d59d | 299 | if (shstrtabsize + 1 <= 1 |
06614111 NC |
300 | || bfd_seek (abfd, offset, SEEK_SET) != 0 |
301 | || (shstrtab = (bfd_byte *) bfd_alloc (abfd, shstrtabsize + 1)) == NULL) | |
c6c60d09 JJ |
302 | shstrtab = NULL; |
303 | else if (bfd_bread (shstrtab, shstrtabsize, abfd) != shstrtabsize) | |
304 | { | |
305 | if (bfd_get_error () != bfd_error_system_call) | |
306 | bfd_set_error (bfd_error_file_truncated); | |
06614111 | 307 | bfd_release (abfd, shstrtab); |
c6c60d09 | 308 | shstrtab = NULL; |
3471d59d CC |
309 | /* Once we've failed to read it, make sure we don't keep |
310 | trying. Otherwise, we'll keep allocating space for | |
311 | the string table over and over. */ | |
312 | i_shdrp[shindex]->sh_size = 0; | |
c6c60d09 JJ |
313 | } |
314 | else | |
315 | shstrtab[shstrtabsize] = '\0'; | |
217aa764 | 316 | i_shdrp[shindex]->contents = shstrtab; |
252b5132 | 317 | } |
f075ee0c | 318 | return (char *) shstrtab; |
252b5132 RH |
319 | } |
320 | ||
321 | char * | |
217aa764 AM |
322 | bfd_elf_string_from_elf_section (bfd *abfd, |
323 | unsigned int shindex, | |
324 | unsigned int strindex) | |
252b5132 RH |
325 | { |
326 | Elf_Internal_Shdr *hdr; | |
327 | ||
328 | if (strindex == 0) | |
329 | return ""; | |
330 | ||
74f2e02b AM |
331 | if (elf_elfsections (abfd) == NULL || shindex >= elf_numsections (abfd)) |
332 | return NULL; | |
333 | ||
252b5132 RH |
334 | hdr = elf_elfsections (abfd)[shindex]; |
335 | ||
06614111 NC |
336 | if (hdr->contents == NULL) |
337 | { | |
338 | if (hdr->sh_type != SHT_STRTAB && hdr->sh_type < SHT_LOOS) | |
339 | { | |
340 | /* PR 17512: file: f057ec89. */ | |
341 | _bfd_error_handler (_("%B: attempt to load strings from a non-string section (number %d)"), | |
342 | abfd, shindex); | |
343 | return NULL; | |
344 | } | |
345 | ||
346 | if (bfd_elf_get_str_section (abfd, shindex) == NULL) | |
347 | return NULL; | |
348 | } | |
252b5132 RH |
349 | |
350 | if (strindex >= hdr->sh_size) | |
351 | { | |
1b3a8575 | 352 | unsigned int shstrndx = elf_elfheader(abfd)->e_shstrndx; |
252b5132 | 353 | (*_bfd_error_handler) |
d003868e AM |
354 | (_("%B: invalid string offset %u >= %lu for section `%s'"), |
355 | abfd, strindex, (unsigned long) hdr->sh_size, | |
1b3a8575 | 356 | (shindex == shstrndx && strindex == hdr->sh_name |
252b5132 | 357 | ? ".shstrtab" |
1b3a8575 | 358 | : bfd_elf_string_from_elf_section (abfd, shstrndx, hdr->sh_name))); |
45b222d6 | 359 | return NULL; |
252b5132 RH |
360 | } |
361 | ||
362 | return ((char *) hdr->contents) + strindex; | |
363 | } | |
364 | ||
6cdc0ccc AM |
365 | /* Read and convert symbols to internal format. |
366 | SYMCOUNT specifies the number of symbols to read, starting from | |
367 | symbol SYMOFFSET. If any of INTSYM_BUF, EXTSYM_BUF or EXTSHNDX_BUF | |
368 | are non-NULL, they are used to store the internal symbols, external | |
b7c368d0 NC |
369 | symbols, and symbol section index extensions, respectively. |
370 | Returns a pointer to the internal symbol buffer (malloced if necessary) | |
371 | or NULL if there were no symbols or some kind of problem. */ | |
6cdc0ccc AM |
372 | |
373 | Elf_Internal_Sym * | |
217aa764 AM |
374 | bfd_elf_get_elf_syms (bfd *ibfd, |
375 | Elf_Internal_Shdr *symtab_hdr, | |
376 | size_t symcount, | |
377 | size_t symoffset, | |
378 | Elf_Internal_Sym *intsym_buf, | |
379 | void *extsym_buf, | |
380 | Elf_External_Sym_Shndx *extshndx_buf) | |
6cdc0ccc AM |
381 | { |
382 | Elf_Internal_Shdr *shndx_hdr; | |
217aa764 | 383 | void *alloc_ext; |
df622259 | 384 | const bfd_byte *esym; |
6cdc0ccc AM |
385 | Elf_External_Sym_Shndx *alloc_extshndx; |
386 | Elf_External_Sym_Shndx *shndx; | |
4dd07732 | 387 | Elf_Internal_Sym *alloc_intsym; |
6cdc0ccc AM |
388 | Elf_Internal_Sym *isym; |
389 | Elf_Internal_Sym *isymend; | |
9c5bfbb7 | 390 | const struct elf_backend_data *bed; |
6cdc0ccc AM |
391 | size_t extsym_size; |
392 | bfd_size_type amt; | |
393 | file_ptr pos; | |
394 | ||
e44a2c9c AM |
395 | if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour) |
396 | abort (); | |
397 | ||
6cdc0ccc AM |
398 | if (symcount == 0) |
399 | return intsym_buf; | |
400 | ||
401 | /* Normal syms might have section extension entries. */ | |
402 | shndx_hdr = NULL; | |
403 | if (symtab_hdr == &elf_tdata (ibfd)->symtab_hdr) | |
404 | shndx_hdr = &elf_tdata (ibfd)->symtab_shndx_hdr; | |
405 | ||
406 | /* Read the symbols. */ | |
407 | alloc_ext = NULL; | |
408 | alloc_extshndx = NULL; | |
4dd07732 | 409 | alloc_intsym = NULL; |
6cdc0ccc AM |
410 | bed = get_elf_backend_data (ibfd); |
411 | extsym_size = bed->s->sizeof_sym; | |
412 | amt = symcount * extsym_size; | |
413 | pos = symtab_hdr->sh_offset + symoffset * extsym_size; | |
414 | if (extsym_buf == NULL) | |
415 | { | |
d0fb9a8d | 416 | alloc_ext = bfd_malloc2 (symcount, extsym_size); |
6cdc0ccc AM |
417 | extsym_buf = alloc_ext; |
418 | } | |
419 | if (extsym_buf == NULL | |
420 | || bfd_seek (ibfd, pos, SEEK_SET) != 0 | |
421 | || bfd_bread (extsym_buf, amt, ibfd) != amt) | |
422 | { | |
423 | intsym_buf = NULL; | |
424 | goto out; | |
425 | } | |
426 | ||
427 | if (shndx_hdr == NULL || shndx_hdr->sh_size == 0) | |
428 | extshndx_buf = NULL; | |
429 | else | |
430 | { | |
431 | amt = symcount * sizeof (Elf_External_Sym_Shndx); | |
432 | pos = shndx_hdr->sh_offset + symoffset * sizeof (Elf_External_Sym_Shndx); | |
433 | if (extshndx_buf == NULL) | |
434 | { | |
a50b1753 NC |
435 | alloc_extshndx = (Elf_External_Sym_Shndx *) |
436 | bfd_malloc2 (symcount, sizeof (Elf_External_Sym_Shndx)); | |
6cdc0ccc AM |
437 | extshndx_buf = alloc_extshndx; |
438 | } | |
439 | if (extshndx_buf == NULL | |
440 | || bfd_seek (ibfd, pos, SEEK_SET) != 0 | |
441 | || bfd_bread (extshndx_buf, amt, ibfd) != amt) | |
442 | { | |
443 | intsym_buf = NULL; | |
444 | goto out; | |
445 | } | |
446 | } | |
447 | ||
448 | if (intsym_buf == NULL) | |
449 | { | |
a50b1753 NC |
450 | alloc_intsym = (Elf_Internal_Sym *) |
451 | bfd_malloc2 (symcount, sizeof (Elf_Internal_Sym)); | |
4dd07732 | 452 | intsym_buf = alloc_intsym; |
6cdc0ccc AM |
453 | if (intsym_buf == NULL) |
454 | goto out; | |
455 | } | |
456 | ||
457 | /* Convert the symbols to internal form. */ | |
458 | isymend = intsym_buf + symcount; | |
a50b1753 NC |
459 | for (esym = (const bfd_byte *) extsym_buf, isym = intsym_buf, |
460 | shndx = extshndx_buf; | |
6cdc0ccc AM |
461 | isym < isymend; |
462 | esym += extsym_size, isym++, shndx = shndx != NULL ? shndx + 1 : NULL) | |
8384fb8f AM |
463 | if (!(*bed->s->swap_symbol_in) (ibfd, esym, shndx, isym)) |
464 | { | |
465 | symoffset += (esym - (bfd_byte *) extsym_buf) / extsym_size; | |
466 | (*_bfd_error_handler) (_("%B symbol number %lu references " | |
467 | "nonexistent SHT_SYMTAB_SHNDX section"), | |
468 | ibfd, (unsigned long) symoffset); | |
4dd07732 AM |
469 | if (alloc_intsym != NULL) |
470 | free (alloc_intsym); | |
8384fb8f AM |
471 | intsym_buf = NULL; |
472 | goto out; | |
473 | } | |
6cdc0ccc AM |
474 | |
475 | out: | |
476 | if (alloc_ext != NULL) | |
477 | free (alloc_ext); | |
478 | if (alloc_extshndx != NULL) | |
479 | free (alloc_extshndx); | |
480 | ||
481 | return intsym_buf; | |
482 | } | |
483 | ||
5cab59f6 AM |
484 | /* Look up a symbol name. */ |
485 | const char * | |
be8dd2ca AM |
486 | bfd_elf_sym_name (bfd *abfd, |
487 | Elf_Internal_Shdr *symtab_hdr, | |
26c61ae5 L |
488 | Elf_Internal_Sym *isym, |
489 | asection *sym_sec) | |
5cab59f6 | 490 | { |
26c61ae5 | 491 | const char *name; |
5cab59f6 | 492 | unsigned int iname = isym->st_name; |
be8dd2ca | 493 | unsigned int shindex = symtab_hdr->sh_link; |
26c61ae5 | 494 | |
138f35cc JJ |
495 | if (iname == 0 && ELF_ST_TYPE (isym->st_info) == STT_SECTION |
496 | /* Check for a bogus st_shndx to avoid crashing. */ | |
4fbb74a6 | 497 | && isym->st_shndx < elf_numsections (abfd)) |
5cab59f6 AM |
498 | { |
499 | iname = elf_elfsections (abfd)[isym->st_shndx]->sh_name; | |
500 | shindex = elf_elfheader (abfd)->e_shstrndx; | |
501 | } | |
502 | ||
26c61ae5 L |
503 | name = bfd_elf_string_from_elf_section (abfd, shindex, iname); |
504 | if (name == NULL) | |
505 | name = "(null)"; | |
506 | else if (sym_sec && *name == '\0') | |
507 | name = bfd_section_name (abfd, sym_sec); | |
508 | ||
509 | return name; | |
5cab59f6 AM |
510 | } |
511 | ||
dbb410c3 AM |
512 | /* Elf_Internal_Shdr->contents is an array of these for SHT_GROUP |
513 | sections. The first element is the flags, the rest are section | |
514 | pointers. */ | |
515 | ||
516 | typedef union elf_internal_group { | |
517 | Elf_Internal_Shdr *shdr; | |
518 | unsigned int flags; | |
519 | } Elf_Internal_Group; | |
520 | ||
b885599b AM |
521 | /* Return the name of the group signature symbol. Why isn't the |
522 | signature just a string? */ | |
523 | ||
524 | static const char * | |
217aa764 | 525 | group_signature (bfd *abfd, Elf_Internal_Shdr *ghdr) |
b885599b | 526 | { |
9dce4196 | 527 | Elf_Internal_Shdr *hdr; |
9dce4196 AM |
528 | unsigned char esym[sizeof (Elf64_External_Sym)]; |
529 | Elf_External_Sym_Shndx eshndx; | |
530 | Elf_Internal_Sym isym; | |
b885599b | 531 | |
13792e9d L |
532 | /* First we need to ensure the symbol table is available. Make sure |
533 | that it is a symbol table section. */ | |
4fbb74a6 AM |
534 | if (ghdr->sh_link >= elf_numsections (abfd)) |
535 | return NULL; | |
13792e9d L |
536 | hdr = elf_elfsections (abfd) [ghdr->sh_link]; |
537 | if (hdr->sh_type != SHT_SYMTAB | |
538 | || ! bfd_section_from_shdr (abfd, ghdr->sh_link)) | |
b885599b AM |
539 | return NULL; |
540 | ||
9dce4196 AM |
541 | /* Go read the symbol. */ |
542 | hdr = &elf_tdata (abfd)->symtab_hdr; | |
6cdc0ccc AM |
543 | if (bfd_elf_get_elf_syms (abfd, hdr, 1, ghdr->sh_info, |
544 | &isym, esym, &eshndx) == NULL) | |
b885599b | 545 | return NULL; |
9dce4196 | 546 | |
26c61ae5 | 547 | return bfd_elf_sym_name (abfd, hdr, &isym, NULL); |
b885599b AM |
548 | } |
549 | ||
dbb410c3 AM |
550 | /* Set next_in_group list pointer, and group name for NEWSECT. */ |
551 | ||
b34976b6 | 552 | static bfd_boolean |
217aa764 | 553 | setup_group (bfd *abfd, Elf_Internal_Shdr *hdr, asection *newsect) |
dbb410c3 AM |
554 | { |
555 | unsigned int num_group = elf_tdata (abfd)->num_group; | |
556 | ||
557 | /* If num_group is zero, read in all SHT_GROUP sections. The count | |
558 | is set to -1 if there are no SHT_GROUP sections. */ | |
559 | if (num_group == 0) | |
560 | { | |
561 | unsigned int i, shnum; | |
562 | ||
563 | /* First count the number of groups. If we have a SHT_GROUP | |
564 | section with just a flag word (ie. sh_size is 4), ignore it. */ | |
9ad5cbcf | 565 | shnum = elf_numsections (abfd); |
dbb410c3 | 566 | num_group = 0; |
08a40648 | 567 | |
44534af3 | 568 | #define IS_VALID_GROUP_SECTION_HEADER(shdr, minsize) \ |
1783205a | 569 | ( (shdr)->sh_type == SHT_GROUP \ |
44534af3 | 570 | && (shdr)->sh_size >= minsize \ |
1783205a NC |
571 | && (shdr)->sh_entsize == GRP_ENTRY_SIZE \ |
572 | && ((shdr)->sh_size % GRP_ENTRY_SIZE) == 0) | |
08a40648 | 573 | |
dbb410c3 AM |
574 | for (i = 0; i < shnum; i++) |
575 | { | |
576 | Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i]; | |
1783205a | 577 | |
44534af3 | 578 | if (IS_VALID_GROUP_SECTION_HEADER (shdr, 2 * GRP_ENTRY_SIZE)) |
dbb410c3 AM |
579 | num_group += 1; |
580 | } | |
581 | ||
582 | if (num_group == 0) | |
20dbb49d L |
583 | { |
584 | num_group = (unsigned) -1; | |
585 | elf_tdata (abfd)->num_group = num_group; | |
586 | } | |
587 | else | |
dbb410c3 AM |
588 | { |
589 | /* We keep a list of elf section headers for group sections, | |
590 | so we can find them quickly. */ | |
20dbb49d | 591 | bfd_size_type amt; |
d0fb9a8d | 592 | |
20dbb49d | 593 | elf_tdata (abfd)->num_group = num_group; |
a50b1753 NC |
594 | elf_tdata (abfd)->group_sect_ptr = (Elf_Internal_Shdr **) |
595 | bfd_alloc2 (abfd, num_group, sizeof (Elf_Internal_Shdr *)); | |
dbb410c3 | 596 | if (elf_tdata (abfd)->group_sect_ptr == NULL) |
b34976b6 | 597 | return FALSE; |
dbb410c3 AM |
598 | |
599 | num_group = 0; | |
600 | for (i = 0; i < shnum; i++) | |
601 | { | |
602 | Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i]; | |
1783205a | 603 | |
44534af3 | 604 | if (IS_VALID_GROUP_SECTION_HEADER (shdr, 2 * GRP_ENTRY_SIZE)) |
dbb410c3 | 605 | { |
973ffd63 | 606 | unsigned char *src; |
dbb410c3 AM |
607 | Elf_Internal_Group *dest; |
608 | ||
609 | /* Add to list of sections. */ | |
610 | elf_tdata (abfd)->group_sect_ptr[num_group] = shdr; | |
611 | num_group += 1; | |
612 | ||
613 | /* Read the raw contents. */ | |
614 | BFD_ASSERT (sizeof (*dest) >= 4); | |
615 | amt = shdr->sh_size * sizeof (*dest) / 4; | |
a50b1753 NC |
616 | shdr->contents = (unsigned char *) |
617 | bfd_alloc2 (abfd, shdr->sh_size, sizeof (*dest) / 4); | |
1783205a NC |
618 | /* PR binutils/4110: Handle corrupt group headers. */ |
619 | if (shdr->contents == NULL) | |
620 | { | |
621 | _bfd_error_handler | |
493a3386 | 622 | (_("%B: corrupt size field in group section header: 0x%lx"), abfd, shdr->sh_size); |
1783205a | 623 | bfd_set_error (bfd_error_bad_value); |
493a3386 NC |
624 | -- num_group; |
625 | continue; | |
1783205a NC |
626 | } |
627 | ||
628 | memset (shdr->contents, 0, amt); | |
629 | ||
630 | if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0 | |
dbb410c3 AM |
631 | || (bfd_bread (shdr->contents, shdr->sh_size, abfd) |
632 | != shdr->sh_size)) | |
493a3386 NC |
633 | { |
634 | _bfd_error_handler | |
635 | (_("%B: invalid size field in group section header: 0x%lx"), abfd, shdr->sh_size); | |
636 | bfd_set_error (bfd_error_bad_value); | |
637 | -- num_group; | |
638 | /* PR 17510: If the group contents are even partially | |
639 | corrupt, do not allow any of the contents to be used. */ | |
640 | memset (shdr->contents, 0, amt); | |
641 | continue; | |
642 | } | |
708d7d0d | 643 | |
dbb410c3 AM |
644 | /* Translate raw contents, a flag word followed by an |
645 | array of elf section indices all in target byte order, | |
646 | to the flag word followed by an array of elf section | |
647 | pointers. */ | |
648 | src = shdr->contents + shdr->sh_size; | |
649 | dest = (Elf_Internal_Group *) (shdr->contents + amt); | |
06614111 | 650 | |
dbb410c3 AM |
651 | while (1) |
652 | { | |
653 | unsigned int idx; | |
654 | ||
655 | src -= 4; | |
656 | --dest; | |
657 | idx = H_GET_32 (abfd, src); | |
658 | if (src == shdr->contents) | |
659 | { | |
660 | dest->flags = idx; | |
b885599b AM |
661 | if (shdr->bfd_section != NULL && (idx & GRP_COMDAT)) |
662 | shdr->bfd_section->flags | |
663 | |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD; | |
dbb410c3 AM |
664 | break; |
665 | } | |
666 | if (idx >= shnum) | |
667 | { | |
668 | ((*_bfd_error_handler) | |
d003868e | 669 | (_("%B: invalid SHT_GROUP entry"), abfd)); |
dbb410c3 AM |
670 | idx = 0; |
671 | } | |
672 | dest->shdr = elf_elfsections (abfd)[idx]; | |
673 | } | |
674 | } | |
675 | } | |
493a3386 NC |
676 | |
677 | /* PR 17510: Corrupt binaries might contain invalid groups. */ | |
678 | if (num_group != (unsigned) elf_tdata (abfd)->num_group) | |
679 | { | |
680 | elf_tdata (abfd)->num_group = num_group; | |
681 | ||
682 | /* If all groups are invalid then fail. */ | |
683 | if (num_group == 0) | |
684 | { | |
685 | elf_tdata (abfd)->group_sect_ptr = NULL; | |
686 | elf_tdata (abfd)->num_group = num_group = -1; | |
687 | (*_bfd_error_handler) (_("%B: no valid group sections found"), abfd); | |
688 | bfd_set_error (bfd_error_bad_value); | |
689 | } | |
690 | } | |
dbb410c3 AM |
691 | } |
692 | } | |
693 | ||
694 | if (num_group != (unsigned) -1) | |
695 | { | |
696 | unsigned int i; | |
697 | ||
698 | for (i = 0; i < num_group; i++) | |
699 | { | |
700 | Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i]; | |
701 | Elf_Internal_Group *idx = (Elf_Internal_Group *) shdr->contents; | |
702 | unsigned int n_elt = shdr->sh_size / 4; | |
703 | ||
704 | /* Look through this group's sections to see if current | |
705 | section is a member. */ | |
706 | while (--n_elt != 0) | |
707 | if ((++idx)->shdr == hdr) | |
708 | { | |
e0e8c97f | 709 | asection *s = NULL; |
dbb410c3 AM |
710 | |
711 | /* We are a member of this group. Go looking through | |
712 | other members to see if any others are linked via | |
713 | next_in_group. */ | |
714 | idx = (Elf_Internal_Group *) shdr->contents; | |
715 | n_elt = shdr->sh_size / 4; | |
716 | while (--n_elt != 0) | |
717 | if ((s = (++idx)->shdr->bfd_section) != NULL | |
945906ff | 718 | && elf_next_in_group (s) != NULL) |
dbb410c3 AM |
719 | break; |
720 | if (n_elt != 0) | |
721 | { | |
dbb410c3 AM |
722 | /* Snarf the group name from other member, and |
723 | insert current section in circular list. */ | |
945906ff AM |
724 | elf_group_name (newsect) = elf_group_name (s); |
725 | elf_next_in_group (newsect) = elf_next_in_group (s); | |
726 | elf_next_in_group (s) = newsect; | |
dbb410c3 AM |
727 | } |
728 | else | |
729 | { | |
dbb410c3 AM |
730 | const char *gname; |
731 | ||
b885599b AM |
732 | gname = group_signature (abfd, shdr); |
733 | if (gname == NULL) | |
b34976b6 | 734 | return FALSE; |
945906ff | 735 | elf_group_name (newsect) = gname; |
dbb410c3 AM |
736 | |
737 | /* Start a circular list with one element. */ | |
945906ff | 738 | elf_next_in_group (newsect) = newsect; |
dbb410c3 | 739 | } |
b885599b | 740 | |
9dce4196 AM |
741 | /* If the group section has been created, point to the |
742 | new member. */ | |
dbb410c3 | 743 | if (shdr->bfd_section != NULL) |
945906ff | 744 | elf_next_in_group (shdr->bfd_section) = newsect; |
b885599b | 745 | |
dbb410c3 AM |
746 | i = num_group - 1; |
747 | break; | |
748 | } | |
749 | } | |
750 | } | |
751 | ||
945906ff | 752 | if (elf_group_name (newsect) == NULL) |
dbb410c3 | 753 | { |
d003868e AM |
754 | (*_bfd_error_handler) (_("%B: no group info for section %A"), |
755 | abfd, newsect); | |
493a3386 | 756 | return FALSE; |
dbb410c3 | 757 | } |
b34976b6 | 758 | return TRUE; |
dbb410c3 AM |
759 | } |
760 | ||
3d7f7666 | 761 | bfd_boolean |
dd863624 | 762 | _bfd_elf_setup_sections (bfd *abfd) |
3d7f7666 L |
763 | { |
764 | unsigned int i; | |
765 | unsigned int num_group = elf_tdata (abfd)->num_group; | |
766 | bfd_boolean result = TRUE; | |
dd863624 L |
767 | asection *s; |
768 | ||
769 | /* Process SHF_LINK_ORDER. */ | |
770 | for (s = abfd->sections; s != NULL; s = s->next) | |
771 | { | |
772 | Elf_Internal_Shdr *this_hdr = &elf_section_data (s)->this_hdr; | |
773 | if ((this_hdr->sh_flags & SHF_LINK_ORDER) != 0) | |
774 | { | |
775 | unsigned int elfsec = this_hdr->sh_link; | |
776 | /* FIXME: The old Intel compiler and old strip/objcopy may | |
777 | not set the sh_link or sh_info fields. Hence we could | |
778 | get the situation where elfsec is 0. */ | |
779 | if (elfsec == 0) | |
780 | { | |
4fbb74a6 | 781 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
dd863624 L |
782 | if (bed->link_order_error_handler) |
783 | bed->link_order_error_handler | |
784 | (_("%B: warning: sh_link not set for section `%A'"), | |
785 | abfd, s); | |
786 | } | |
787 | else | |
788 | { | |
91d6fa6a | 789 | asection *linksec = NULL; |
25bbc984 | 790 | |
4fbb74a6 AM |
791 | if (elfsec < elf_numsections (abfd)) |
792 | { | |
793 | this_hdr = elf_elfsections (abfd)[elfsec]; | |
91d6fa6a | 794 | linksec = this_hdr->bfd_section; |
4fbb74a6 | 795 | } |
25bbc984 L |
796 | |
797 | /* PR 1991, 2008: | |
798 | Some strip/objcopy may leave an incorrect value in | |
799 | sh_link. We don't want to proceed. */ | |
91d6fa6a | 800 | if (linksec == NULL) |
25bbc984 L |
801 | { |
802 | (*_bfd_error_handler) | |
803 | (_("%B: sh_link [%d] in section `%A' is incorrect"), | |
804 | s->owner, s, elfsec); | |
805 | result = FALSE; | |
806 | } | |
807 | ||
91d6fa6a | 808 | elf_linked_to_section (s) = linksec; |
dd863624 L |
809 | } |
810 | } | |
811 | } | |
3d7f7666 | 812 | |
dd863624 | 813 | /* Process section groups. */ |
3d7f7666 L |
814 | if (num_group == (unsigned) -1) |
815 | return result; | |
816 | ||
817 | for (i = 0; i < num_group; i++) | |
818 | { | |
819 | Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i]; | |
820 | Elf_Internal_Group *idx = (Elf_Internal_Group *) shdr->contents; | |
821 | unsigned int n_elt = shdr->sh_size / 4; | |
822 | ||
823 | while (--n_elt != 0) | |
824 | if ((++idx)->shdr->bfd_section) | |
825 | elf_sec_group (idx->shdr->bfd_section) = shdr->bfd_section; | |
826 | else if (idx->shdr->sh_type == SHT_RELA | |
827 | || idx->shdr->sh_type == SHT_REL) | |
828 | /* We won't include relocation sections in section groups in | |
829 | output object files. We adjust the group section size here | |
830 | so that relocatable link will work correctly when | |
831 | relocation sections are in section group in input object | |
832 | files. */ | |
833 | shdr->bfd_section->size -= 4; | |
834 | else | |
835 | { | |
836 | /* There are some unknown sections in the group. */ | |
837 | (*_bfd_error_handler) | |
d003868e AM |
838 | (_("%B: unknown [%d] section `%s' in group [%s]"), |
839 | abfd, | |
3d7f7666 | 840 | (unsigned int) idx->shdr->sh_type, |
1b3a8575 AM |
841 | bfd_elf_string_from_elf_section (abfd, |
842 | (elf_elfheader (abfd) | |
843 | ->e_shstrndx), | |
844 | idx->shdr->sh_name), | |
3d7f7666 L |
845 | shdr->bfd_section->name); |
846 | result = FALSE; | |
847 | } | |
848 | } | |
849 | return result; | |
850 | } | |
851 | ||
72adc230 AM |
852 | bfd_boolean |
853 | bfd_elf_is_group_section (bfd *abfd ATTRIBUTE_UNUSED, const asection *sec) | |
854 | { | |
855 | return elf_next_in_group (sec) != NULL; | |
856 | } | |
857 | ||
f6fe1ccd L |
858 | static char * |
859 | convert_debug_to_zdebug (bfd *abfd, const char *name) | |
860 | { | |
861 | unsigned int len = strlen (name); | |
862 | char *new_name = bfd_alloc (abfd, len + 2); | |
863 | if (new_name == NULL) | |
864 | return NULL; | |
865 | new_name[0] = '.'; | |
866 | new_name[1] = 'z'; | |
867 | memcpy (new_name + 2, name + 1, len); | |
868 | return new_name; | |
869 | } | |
870 | ||
871 | static char * | |
872 | convert_zdebug_to_debug (bfd *abfd, const char *name) | |
873 | { | |
874 | unsigned int len = strlen (name); | |
875 | char *new_name = bfd_alloc (abfd, len); | |
876 | if (new_name == NULL) | |
877 | return NULL; | |
878 | new_name[0] = '.'; | |
879 | memcpy (new_name + 1, name + 2, len - 1); | |
880 | return new_name; | |
881 | } | |
882 | ||
252b5132 RH |
883 | /* Make a BFD section from an ELF section. We store a pointer to the |
884 | BFD section in the bfd_section field of the header. */ | |
885 | ||
b34976b6 | 886 | bfd_boolean |
217aa764 AM |
887 | _bfd_elf_make_section_from_shdr (bfd *abfd, |
888 | Elf_Internal_Shdr *hdr, | |
6dc132d9 L |
889 | const char *name, |
890 | int shindex) | |
252b5132 RH |
891 | { |
892 | asection *newsect; | |
893 | flagword flags; | |
9c5bfbb7 | 894 | const struct elf_backend_data *bed; |
252b5132 RH |
895 | |
896 | if (hdr->bfd_section != NULL) | |
4e011fb5 | 897 | return TRUE; |
252b5132 RH |
898 | |
899 | newsect = bfd_make_section_anyway (abfd, name); | |
900 | if (newsect == NULL) | |
b34976b6 | 901 | return FALSE; |
252b5132 | 902 | |
1829f4b2 AM |
903 | hdr->bfd_section = newsect; |
904 | elf_section_data (newsect)->this_hdr = *hdr; | |
6dc132d9 | 905 | elf_section_data (newsect)->this_idx = shindex; |
1829f4b2 | 906 | |
2f89ff8d L |
907 | /* Always use the real type/flags. */ |
908 | elf_section_type (newsect) = hdr->sh_type; | |
909 | elf_section_flags (newsect) = hdr->sh_flags; | |
910 | ||
252b5132 RH |
911 | newsect->filepos = hdr->sh_offset; |
912 | ||
913 | if (! bfd_set_section_vma (abfd, newsect, hdr->sh_addr) | |
914 | || ! bfd_set_section_size (abfd, newsect, hdr->sh_size) | |
915 | || ! bfd_set_section_alignment (abfd, newsect, | |
72de5009 | 916 | bfd_log2 (hdr->sh_addralign))) |
b34976b6 | 917 | return FALSE; |
252b5132 RH |
918 | |
919 | flags = SEC_NO_FLAGS; | |
920 | if (hdr->sh_type != SHT_NOBITS) | |
921 | flags |= SEC_HAS_CONTENTS; | |
dbb410c3 | 922 | if (hdr->sh_type == SHT_GROUP) |
b3096250 | 923 | flags |= SEC_GROUP | SEC_EXCLUDE; |
252b5132 RH |
924 | if ((hdr->sh_flags & SHF_ALLOC) != 0) |
925 | { | |
926 | flags |= SEC_ALLOC; | |
927 | if (hdr->sh_type != SHT_NOBITS) | |
928 | flags |= SEC_LOAD; | |
929 | } | |
930 | if ((hdr->sh_flags & SHF_WRITE) == 0) | |
931 | flags |= SEC_READONLY; | |
932 | if ((hdr->sh_flags & SHF_EXECINSTR) != 0) | |
933 | flags |= SEC_CODE; | |
934 | else if ((flags & SEC_LOAD) != 0) | |
935 | flags |= SEC_DATA; | |
f5fa8ca2 JJ |
936 | if ((hdr->sh_flags & SHF_MERGE) != 0) |
937 | { | |
938 | flags |= SEC_MERGE; | |
939 | newsect->entsize = hdr->sh_entsize; | |
940 | if ((hdr->sh_flags & SHF_STRINGS) != 0) | |
941 | flags |= SEC_STRINGS; | |
942 | } | |
dbb410c3 AM |
943 | if (hdr->sh_flags & SHF_GROUP) |
944 | if (!setup_group (abfd, hdr, newsect)) | |
b34976b6 | 945 | return FALSE; |
13ae64f3 JJ |
946 | if ((hdr->sh_flags & SHF_TLS) != 0) |
947 | flags |= SEC_THREAD_LOCAL; | |
18ae9cc1 L |
948 | if ((hdr->sh_flags & SHF_EXCLUDE) != 0) |
949 | flags |= SEC_EXCLUDE; | |
252b5132 | 950 | |
3d2b39cf | 951 | if ((flags & SEC_ALLOC) == 0) |
7a6cc5fb | 952 | { |
3d2b39cf L |
953 | /* The debugging sections appear to be recognized only by name, |
954 | not any sort of flag. Their SEC_ALLOC bits are cleared. */ | |
3d2b39cf L |
955 | if (name [0] == '.') |
956 | { | |
f073ced3 AM |
957 | const char *p; |
958 | int n; | |
959 | if (name[1] == 'd') | |
960 | p = ".debug", n = 6; | |
961 | else if (name[1] == 'g' && name[2] == 'n') | |
962 | p = ".gnu.linkonce.wi.", n = 17; | |
963 | else if (name[1] == 'g' && name[2] == 'd') | |
964 | p = ".gdb_index", n = 11; /* yes we really do mean 11. */ | |
965 | else if (name[1] == 'l') | |
966 | p = ".line", n = 5; | |
967 | else if (name[1] == 's') | |
968 | p = ".stab", n = 5; | |
969 | else if (name[1] == 'z') | |
970 | p = ".zdebug", n = 7; | |
971 | else | |
972 | p = NULL, n = 0; | |
973 | if (p != NULL && strncmp (name, p, n) == 0) | |
3d2b39cf L |
974 | flags |= SEC_DEBUGGING; |
975 | } | |
976 | } | |
252b5132 RH |
977 | |
978 | /* As a GNU extension, if the name begins with .gnu.linkonce, we | |
979 | only link a single copy of the section. This is used to support | |
980 | g++. g++ will emit each template expansion in its own section. | |
981 | The symbols will be defined as weak, so that multiple definitions | |
982 | are permitted. The GNU linker extension is to actually discard | |
983 | all but one of the sections. */ | |
0112cd26 | 984 | if (CONST_STRNEQ (name, ".gnu.linkonce") |
b885599b | 985 | && elf_next_in_group (newsect) == NULL) |
252b5132 RH |
986 | flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD; |
987 | ||
fa152c49 JW |
988 | bed = get_elf_backend_data (abfd); |
989 | if (bed->elf_backend_section_flags) | |
990 | if (! bed->elf_backend_section_flags (&flags, hdr)) | |
b34976b6 | 991 | return FALSE; |
fa152c49 | 992 | |
252b5132 | 993 | if (! bfd_set_section_flags (abfd, newsect, flags)) |
b34976b6 | 994 | return FALSE; |
252b5132 | 995 | |
718175fa JK |
996 | /* We do not parse the PT_NOTE segments as we are interested even in the |
997 | separate debug info files which may have the segments offsets corrupted. | |
998 | PT_NOTEs from the core files are currently not parsed using BFD. */ | |
999 | if (hdr->sh_type == SHT_NOTE) | |
1000 | { | |
baea7ef1 | 1001 | bfd_byte *contents; |
718175fa | 1002 | |
baea7ef1 | 1003 | if (!bfd_malloc_and_get_section (abfd, newsect, &contents)) |
718175fa JK |
1004 | return FALSE; |
1005 | ||
baea7ef1 | 1006 | elf_parse_notes (abfd, (char *) contents, hdr->sh_size, -1); |
718175fa JK |
1007 | free (contents); |
1008 | } | |
1009 | ||
252b5132 RH |
1010 | if ((flags & SEC_ALLOC) != 0) |
1011 | { | |
1012 | Elf_Internal_Phdr *phdr; | |
6ffd7900 AM |
1013 | unsigned int i, nload; |
1014 | ||
1015 | /* Some ELF linkers produce binaries with all the program header | |
1016 | p_paddr fields zero. If we have such a binary with more than | |
1017 | one PT_LOAD header, then leave the section lma equal to vma | |
1018 | so that we don't create sections with overlapping lma. */ | |
1019 | phdr = elf_tdata (abfd)->phdr; | |
1020 | for (nload = 0, i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++) | |
1021 | if (phdr->p_paddr != 0) | |
1022 | break; | |
1023 | else if (phdr->p_type == PT_LOAD && phdr->p_memsz != 0) | |
1024 | ++nload; | |
1025 | if (i >= elf_elfheader (abfd)->e_phnum && nload > 1) | |
1026 | return TRUE; | |
252b5132 | 1027 | |
252b5132 RH |
1028 | phdr = elf_tdata (abfd)->phdr; |
1029 | for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++) | |
1030 | { | |
86b2281f AM |
1031 | if (((phdr->p_type == PT_LOAD |
1032 | && (hdr->sh_flags & SHF_TLS) == 0) | |
1033 | || phdr->p_type == PT_TLS) | |
9a83a553 | 1034 | && ELF_SECTION_IN_SEGMENT (hdr, phdr)) |
252b5132 | 1035 | { |
88967714 AM |
1036 | if ((flags & SEC_LOAD) == 0) |
1037 | newsect->lma = (phdr->p_paddr | |
1038 | + hdr->sh_addr - phdr->p_vaddr); | |
1039 | else | |
1040 | /* We used to use the same adjustment for SEC_LOAD | |
1041 | sections, but that doesn't work if the segment | |
1042 | is packed with code from multiple VMAs. | |
1043 | Instead we calculate the section LMA based on | |
1044 | the segment LMA. It is assumed that the | |
1045 | segment will contain sections with contiguous | |
1046 | LMAs, even if the VMAs are not. */ | |
1047 | newsect->lma = (phdr->p_paddr | |
1048 | + hdr->sh_offset - phdr->p_offset); | |
1049 | ||
1050 | /* With contiguous segments, we can't tell from file | |
1051 | offsets whether a section with zero size should | |
1052 | be placed at the end of one segment or the | |
1053 | beginning of the next. Decide based on vaddr. */ | |
1054 | if (hdr->sh_addr >= phdr->p_vaddr | |
1055 | && (hdr->sh_addr + hdr->sh_size | |
1056 | <= phdr->p_vaddr + phdr->p_memsz)) | |
1057 | break; | |
252b5132 RH |
1058 | } |
1059 | } | |
1060 | } | |
1061 | ||
4a114e3e L |
1062 | /* Compress/decompress DWARF debug sections with names: .debug_* and |
1063 | .zdebug_*, after the section flags is set. */ | |
1064 | if ((flags & SEC_DEBUGGING) | |
1065 | && ((name[1] == 'd' && name[6] == '_') | |
1066 | || (name[1] == 'z' && name[7] == '_'))) | |
1067 | { | |
1068 | enum { nothing, compress, decompress } action = nothing; | |
151411f8 L |
1069 | int compression_header_size; |
1070 | bfd_boolean compressed | |
1071 | = bfd_is_section_compressed_with_header (abfd, newsect, | |
1072 | &compression_header_size); | |
4a114e3e | 1073 | |
151411f8 | 1074 | if (compressed) |
4a114e3e L |
1075 | { |
1076 | /* Compressed section. Check if we should decompress. */ | |
1077 | if ((abfd->flags & BFD_DECOMPRESS)) | |
1078 | action = decompress; | |
1079 | } | |
151411f8 L |
1080 | |
1081 | /* Compress the uncompressed section or convert from/to .zdebug* | |
1082 | section. Check if we should compress. */ | |
1083 | if (action == nothing) | |
4a114e3e | 1084 | { |
151411f8 L |
1085 | if (newsect->size != 0 |
1086 | && (abfd->flags & BFD_COMPRESS) | |
1087 | && compression_header_size >= 0 | |
1088 | && (!compressed | |
1089 | || ((compression_header_size > 0) | |
1090 | != ((abfd->flags & BFD_COMPRESS_GABI) != 0)))) | |
4a114e3e | 1091 | action = compress; |
151411f8 L |
1092 | else |
1093 | return TRUE; | |
4a114e3e L |
1094 | } |
1095 | ||
151411f8 | 1096 | if (action == compress) |
4a114e3e | 1097 | { |
4a114e3e L |
1098 | if (!bfd_init_section_compress_status (abfd, newsect)) |
1099 | { | |
1100 | (*_bfd_error_handler) | |
bc823199 | 1101 | (_("%B: unable to initialize compress status for section %s"), |
4a114e3e L |
1102 | abfd, name); |
1103 | return FALSE; | |
1104 | } | |
151411f8 L |
1105 | } |
1106 | else | |
1107 | { | |
4a114e3e L |
1108 | if (!bfd_init_section_decompress_status (abfd, newsect)) |
1109 | { | |
1110 | (*_bfd_error_handler) | |
bc823199 | 1111 | (_("%B: unable to initialize decompress status for section %s"), |
4a114e3e L |
1112 | abfd, name); |
1113 | return FALSE; | |
1114 | } | |
151411f8 L |
1115 | } |
1116 | ||
f6fe1ccd | 1117 | if (abfd->is_linker_input) |
151411f8 | 1118 | { |
f6fe1ccd L |
1119 | if (name[1] == 'z' |
1120 | && (action == decompress | |
1121 | || (action == compress | |
1122 | && (abfd->flags & BFD_COMPRESS_GABI) != 0))) | |
4e011fb5 | 1123 | { |
f6fe1ccd L |
1124 | /* Convert section name from .zdebug_* to .debug_* so |
1125 | that linker will consider this section as a debug | |
1126 | section. */ | |
1127 | char *new_name = convert_zdebug_to_debug (abfd, name); | |
151411f8 L |
1128 | if (new_name == NULL) |
1129 | return FALSE; | |
f6fe1ccd | 1130 | bfd_rename_section (abfd, newsect, new_name); |
151411f8 | 1131 | } |
4a114e3e | 1132 | } |
f6fe1ccd L |
1133 | else |
1134 | /* For objdump, don't rename the section. For objcopy, delay | |
1135 | section rename to elf_fake_sections. */ | |
1136 | newsect->flags |= SEC_ELF_RENAME; | |
4a114e3e L |
1137 | } |
1138 | ||
b34976b6 | 1139 | return TRUE; |
252b5132 RH |
1140 | } |
1141 | ||
252b5132 RH |
1142 | const char *const bfd_elf_section_type_names[] = { |
1143 | "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB", | |
1144 | "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE", | |
1145 | "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM", | |
1146 | }; | |
1147 | ||
1049f94e | 1148 | /* ELF relocs are against symbols. If we are producing relocatable |
252b5132 RH |
1149 | output, and the reloc is against an external symbol, and nothing |
1150 | has given us any additional addend, the resulting reloc will also | |
1151 | be against the same symbol. In such a case, we don't want to | |
1152 | change anything about the way the reloc is handled, since it will | |
1153 | all be done at final link time. Rather than put special case code | |
1154 | into bfd_perform_relocation, all the reloc types use this howto | |
1155 | function. It just short circuits the reloc if producing | |
1049f94e | 1156 | relocatable output against an external symbol. */ |
252b5132 | 1157 | |
252b5132 | 1158 | bfd_reloc_status_type |
217aa764 AM |
1159 | bfd_elf_generic_reloc (bfd *abfd ATTRIBUTE_UNUSED, |
1160 | arelent *reloc_entry, | |
1161 | asymbol *symbol, | |
1162 | void *data ATTRIBUTE_UNUSED, | |
1163 | asection *input_section, | |
1164 | bfd *output_bfd, | |
1165 | char **error_message ATTRIBUTE_UNUSED) | |
1166 | { | |
1167 | if (output_bfd != NULL | |
252b5132 RH |
1168 | && (symbol->flags & BSF_SECTION_SYM) == 0 |
1169 | && (! reloc_entry->howto->partial_inplace | |
1170 | || reloc_entry->addend == 0)) | |
1171 | { | |
1172 | reloc_entry->address += input_section->output_offset; | |
1173 | return bfd_reloc_ok; | |
1174 | } | |
1175 | ||
1176 | return bfd_reloc_continue; | |
1177 | } | |
1178 | \f | |
0ac4564e L |
1179 | /* Copy the program header and other data from one object module to |
1180 | another. */ | |
252b5132 | 1181 | |
b34976b6 | 1182 | bfd_boolean |
217aa764 | 1183 | _bfd_elf_copy_private_bfd_data (bfd *ibfd, bfd *obfd) |
2d502050 L |
1184 | { |
1185 | if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour | |
1186 | || bfd_get_flavour (obfd) != bfd_target_elf_flavour) | |
b34976b6 | 1187 | return TRUE; |
2d502050 | 1188 | |
57b828ef L |
1189 | if (!elf_flags_init (obfd)) |
1190 | { | |
1191 | elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags; | |
1192 | elf_flags_init (obfd) = TRUE; | |
1193 | } | |
2d502050 | 1194 | |
0ac4564e | 1195 | elf_gp (obfd) = elf_gp (ibfd); |
57b828ef L |
1196 | |
1197 | /* Also copy the EI_OSABI field. */ | |
1198 | elf_elfheader (obfd)->e_ident[EI_OSABI] = | |
1199 | elf_elfheader (ibfd)->e_ident[EI_OSABI]; | |
104d59d1 JM |
1200 | |
1201 | /* Copy object attributes. */ | |
1202 | _bfd_elf_copy_obj_attributes (ibfd, obfd); | |
b34976b6 | 1203 | return TRUE; |
2d502050 L |
1204 | } |
1205 | ||
cedc298e L |
1206 | static const char * |
1207 | get_segment_type (unsigned int p_type) | |
1208 | { | |
1209 | const char *pt; | |
1210 | switch (p_type) | |
1211 | { | |
1212 | case PT_NULL: pt = "NULL"; break; | |
1213 | case PT_LOAD: pt = "LOAD"; break; | |
1214 | case PT_DYNAMIC: pt = "DYNAMIC"; break; | |
1215 | case PT_INTERP: pt = "INTERP"; break; | |
1216 | case PT_NOTE: pt = "NOTE"; break; | |
1217 | case PT_SHLIB: pt = "SHLIB"; break; | |
1218 | case PT_PHDR: pt = "PHDR"; break; | |
1219 | case PT_TLS: pt = "TLS"; break; | |
1220 | case PT_GNU_EH_FRAME: pt = "EH_FRAME"; break; | |
2b05f1b7 | 1221 | case PT_GNU_STACK: pt = "STACK"; break; |
cedc298e L |
1222 | case PT_GNU_RELRO: pt = "RELRO"; break; |
1223 | default: pt = NULL; break; | |
1224 | } | |
1225 | return pt; | |
1226 | } | |
1227 | ||
f0b79d91 L |
1228 | /* Print out the program headers. */ |
1229 | ||
b34976b6 | 1230 | bfd_boolean |
217aa764 | 1231 | _bfd_elf_print_private_bfd_data (bfd *abfd, void *farg) |
252b5132 | 1232 | { |
a50b1753 | 1233 | FILE *f = (FILE *) farg; |
252b5132 RH |
1234 | Elf_Internal_Phdr *p; |
1235 | asection *s; | |
1236 | bfd_byte *dynbuf = NULL; | |
1237 | ||
1238 | p = elf_tdata (abfd)->phdr; | |
1239 | if (p != NULL) | |
1240 | { | |
1241 | unsigned int i, c; | |
1242 | ||
1243 | fprintf (f, _("\nProgram Header:\n")); | |
1244 | c = elf_elfheader (abfd)->e_phnum; | |
1245 | for (i = 0; i < c; i++, p++) | |
1246 | { | |
cedc298e | 1247 | const char *pt = get_segment_type (p->p_type); |
252b5132 RH |
1248 | char buf[20]; |
1249 | ||
cedc298e | 1250 | if (pt == NULL) |
252b5132 | 1251 | { |
cedc298e L |
1252 | sprintf (buf, "0x%lx", p->p_type); |
1253 | pt = buf; | |
252b5132 | 1254 | } |
dc810e39 | 1255 | fprintf (f, "%8s off 0x", pt); |
60b89a18 | 1256 | bfd_fprintf_vma (abfd, f, p->p_offset); |
252b5132 | 1257 | fprintf (f, " vaddr 0x"); |
60b89a18 | 1258 | bfd_fprintf_vma (abfd, f, p->p_vaddr); |
252b5132 | 1259 | fprintf (f, " paddr 0x"); |
60b89a18 | 1260 | bfd_fprintf_vma (abfd, f, p->p_paddr); |
252b5132 RH |
1261 | fprintf (f, " align 2**%u\n", bfd_log2 (p->p_align)); |
1262 | fprintf (f, " filesz 0x"); | |
60b89a18 | 1263 | bfd_fprintf_vma (abfd, f, p->p_filesz); |
252b5132 | 1264 | fprintf (f, " memsz 0x"); |
60b89a18 | 1265 | bfd_fprintf_vma (abfd, f, p->p_memsz); |
252b5132 RH |
1266 | fprintf (f, " flags %c%c%c", |
1267 | (p->p_flags & PF_R) != 0 ? 'r' : '-', | |
1268 | (p->p_flags & PF_W) != 0 ? 'w' : '-', | |
1269 | (p->p_flags & PF_X) != 0 ? 'x' : '-'); | |
dc810e39 AM |
1270 | if ((p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X)) != 0) |
1271 | fprintf (f, " %lx", p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X)); | |
252b5132 RH |
1272 | fprintf (f, "\n"); |
1273 | } | |
1274 | } | |
1275 | ||
1276 | s = bfd_get_section_by_name (abfd, ".dynamic"); | |
1277 | if (s != NULL) | |
1278 | { | |
cb33740c | 1279 | unsigned int elfsec; |
dc810e39 | 1280 | unsigned long shlink; |
252b5132 RH |
1281 | bfd_byte *extdyn, *extdynend; |
1282 | size_t extdynsize; | |
217aa764 | 1283 | void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *); |
252b5132 RH |
1284 | |
1285 | fprintf (f, _("\nDynamic Section:\n")); | |
1286 | ||
eea6121a | 1287 | if (!bfd_malloc_and_get_section (abfd, s, &dynbuf)) |
252b5132 RH |
1288 | goto error_return; |
1289 | ||
1290 | elfsec = _bfd_elf_section_from_bfd_section (abfd, s); | |
cb33740c | 1291 | if (elfsec == SHN_BAD) |
252b5132 | 1292 | goto error_return; |
dc810e39 | 1293 | shlink = elf_elfsections (abfd)[elfsec]->sh_link; |
252b5132 RH |
1294 | |
1295 | extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn; | |
1296 | swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in; | |
1297 | ||
1298 | extdyn = dynbuf; | |
06614111 NC |
1299 | /* PR 17512: file: 6f427532. */ |
1300 | if (s->size < extdynsize) | |
1301 | goto error_return; | |
eea6121a | 1302 | extdynend = extdyn + s->size; |
1036838a NC |
1303 | /* PR 17512: file: id:000006,sig:06,src:000000,op:flip4,pos:5664. |
1304 | Fix range check. */ | |
1305 | for (; extdyn <= (extdynend - extdynsize); extdyn += extdynsize) | |
252b5132 RH |
1306 | { |
1307 | Elf_Internal_Dyn dyn; | |
ad9563d6 | 1308 | const char *name = ""; |
252b5132 | 1309 | char ab[20]; |
b34976b6 | 1310 | bfd_boolean stringp; |
ad9563d6 | 1311 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
252b5132 | 1312 | |
217aa764 | 1313 | (*swap_dyn_in) (abfd, extdyn, &dyn); |
252b5132 RH |
1314 | |
1315 | if (dyn.d_tag == DT_NULL) | |
1316 | break; | |
1317 | ||
b34976b6 | 1318 | stringp = FALSE; |
252b5132 RH |
1319 | switch (dyn.d_tag) |
1320 | { | |
1321 | default: | |
ad9563d6 CM |
1322 | if (bed->elf_backend_get_target_dtag) |
1323 | name = (*bed->elf_backend_get_target_dtag) (dyn.d_tag); | |
1324 | ||
1325 | if (!strcmp (name, "")) | |
1326 | { | |
1327 | sprintf (ab, "0x%lx", (unsigned long) dyn.d_tag); | |
1328 | name = ab; | |
1329 | } | |
252b5132 RH |
1330 | break; |
1331 | ||
b34976b6 | 1332 | case DT_NEEDED: name = "NEEDED"; stringp = TRUE; break; |
252b5132 RH |
1333 | case DT_PLTRELSZ: name = "PLTRELSZ"; break; |
1334 | case DT_PLTGOT: name = "PLTGOT"; break; | |
1335 | case DT_HASH: name = "HASH"; break; | |
1336 | case DT_STRTAB: name = "STRTAB"; break; | |
1337 | case DT_SYMTAB: name = "SYMTAB"; break; | |
1338 | case DT_RELA: name = "RELA"; break; | |
1339 | case DT_RELASZ: name = "RELASZ"; break; | |
1340 | case DT_RELAENT: name = "RELAENT"; break; | |
1341 | case DT_STRSZ: name = "STRSZ"; break; | |
1342 | case DT_SYMENT: name = "SYMENT"; break; | |
1343 | case DT_INIT: name = "INIT"; break; | |
1344 | case DT_FINI: name = "FINI"; break; | |
b34976b6 AM |
1345 | case DT_SONAME: name = "SONAME"; stringp = TRUE; break; |
1346 | case DT_RPATH: name = "RPATH"; stringp = TRUE; break; | |
252b5132 RH |
1347 | case DT_SYMBOLIC: name = "SYMBOLIC"; break; |
1348 | case DT_REL: name = "REL"; break; | |
1349 | case DT_RELSZ: name = "RELSZ"; break; | |
1350 | case DT_RELENT: name = "RELENT"; break; | |
1351 | case DT_PLTREL: name = "PLTREL"; break; | |
1352 | case DT_DEBUG: name = "DEBUG"; break; | |
1353 | case DT_TEXTREL: name = "TEXTREL"; break; | |
1354 | case DT_JMPREL: name = "JMPREL"; break; | |
94558834 L |
1355 | case DT_BIND_NOW: name = "BIND_NOW"; break; |
1356 | case DT_INIT_ARRAY: name = "INIT_ARRAY"; break; | |
1357 | case DT_FINI_ARRAY: name = "FINI_ARRAY"; break; | |
1358 | case DT_INIT_ARRAYSZ: name = "INIT_ARRAYSZ"; break; | |
1359 | case DT_FINI_ARRAYSZ: name = "FINI_ARRAYSZ"; break; | |
b34976b6 | 1360 | case DT_RUNPATH: name = "RUNPATH"; stringp = TRUE; break; |
94558834 L |
1361 | case DT_FLAGS: name = "FLAGS"; break; |
1362 | case DT_PREINIT_ARRAY: name = "PREINIT_ARRAY"; break; | |
1363 | case DT_PREINIT_ARRAYSZ: name = "PREINIT_ARRAYSZ"; break; | |
d48188b9 | 1364 | case DT_CHECKSUM: name = "CHECKSUM"; break; |
94558834 L |
1365 | case DT_PLTPADSZ: name = "PLTPADSZ"; break; |
1366 | case DT_MOVEENT: name = "MOVEENT"; break; | |
1367 | case DT_MOVESZ: name = "MOVESZ"; break; | |
1368 | case DT_FEATURE: name = "FEATURE"; break; | |
1369 | case DT_POSFLAG_1: name = "POSFLAG_1"; break; | |
1370 | case DT_SYMINSZ: name = "SYMINSZ"; break; | |
1371 | case DT_SYMINENT: name = "SYMINENT"; break; | |
b34976b6 AM |
1372 | case DT_CONFIG: name = "CONFIG"; stringp = TRUE; break; |
1373 | case DT_DEPAUDIT: name = "DEPAUDIT"; stringp = TRUE; break; | |
1374 | case DT_AUDIT: name = "AUDIT"; stringp = TRUE; break; | |
94558834 L |
1375 | case DT_PLTPAD: name = "PLTPAD"; break; |
1376 | case DT_MOVETAB: name = "MOVETAB"; break; | |
1377 | case DT_SYMINFO: name = "SYMINFO"; break; | |
1378 | case DT_RELACOUNT: name = "RELACOUNT"; break; | |
1379 | case DT_RELCOUNT: name = "RELCOUNT"; break; | |
1380 | case DT_FLAGS_1: name = "FLAGS_1"; break; | |
252b5132 RH |
1381 | case DT_VERSYM: name = "VERSYM"; break; |
1382 | case DT_VERDEF: name = "VERDEF"; break; | |
1383 | case DT_VERDEFNUM: name = "VERDEFNUM"; break; | |
1384 | case DT_VERNEED: name = "VERNEED"; break; | |
1385 | case DT_VERNEEDNUM: name = "VERNEEDNUM"; break; | |
b34976b6 | 1386 | case DT_AUXILIARY: name = "AUXILIARY"; stringp = TRUE; break; |
94558834 | 1387 | case DT_USED: name = "USED"; break; |
b34976b6 | 1388 | case DT_FILTER: name = "FILTER"; stringp = TRUE; break; |
fdc90cb4 | 1389 | case DT_GNU_HASH: name = "GNU_HASH"; break; |
252b5132 RH |
1390 | } |
1391 | ||
ad9563d6 | 1392 | fprintf (f, " %-20s ", name); |
252b5132 | 1393 | if (! stringp) |
a1f3c56e AN |
1394 | { |
1395 | fprintf (f, "0x"); | |
1396 | bfd_fprintf_vma (abfd, f, dyn.d_un.d_val); | |
1397 | } | |
252b5132 RH |
1398 | else |
1399 | { | |
1400 | const char *string; | |
dc810e39 | 1401 | unsigned int tagv = dyn.d_un.d_val; |
252b5132 | 1402 | |
dc810e39 | 1403 | string = bfd_elf_string_from_elf_section (abfd, shlink, tagv); |
252b5132 RH |
1404 | if (string == NULL) |
1405 | goto error_return; | |
1406 | fprintf (f, "%s", string); | |
1407 | } | |
1408 | fprintf (f, "\n"); | |
1409 | } | |
1410 | ||
1411 | free (dynbuf); | |
1412 | dynbuf = NULL; | |
1413 | } | |
1414 | ||
1415 | if ((elf_dynverdef (abfd) != 0 && elf_tdata (abfd)->verdef == NULL) | |
1416 | || (elf_dynverref (abfd) != 0 && elf_tdata (abfd)->verref == NULL)) | |
1417 | { | |
fc0e6df6 | 1418 | if (! _bfd_elf_slurp_version_tables (abfd, FALSE)) |
b34976b6 | 1419 | return FALSE; |
252b5132 RH |
1420 | } |
1421 | ||
1422 | if (elf_dynverdef (abfd) != 0) | |
1423 | { | |
1424 | Elf_Internal_Verdef *t; | |
1425 | ||
1426 | fprintf (f, _("\nVersion definitions:\n")); | |
1427 | for (t = elf_tdata (abfd)->verdef; t != NULL; t = t->vd_nextdef) | |
1428 | { | |
1429 | fprintf (f, "%d 0x%2.2x 0x%8.8lx %s\n", t->vd_ndx, | |
d0fb9a8d JJ |
1430 | t->vd_flags, t->vd_hash, |
1431 | t->vd_nodename ? t->vd_nodename : "<corrupt>"); | |
1432 | if (t->vd_auxptr != NULL && t->vd_auxptr->vda_nextptr != NULL) | |
252b5132 RH |
1433 | { |
1434 | Elf_Internal_Verdaux *a; | |
1435 | ||
1436 | fprintf (f, "\t"); | |
1437 | for (a = t->vd_auxptr->vda_nextptr; | |
1438 | a != NULL; | |
1439 | a = a->vda_nextptr) | |
d0fb9a8d JJ |
1440 | fprintf (f, "%s ", |
1441 | a->vda_nodename ? a->vda_nodename : "<corrupt>"); | |
252b5132 RH |
1442 | fprintf (f, "\n"); |
1443 | } | |
1444 | } | |
1445 | } | |
1446 | ||
1447 | if (elf_dynverref (abfd) != 0) | |
1448 | { | |
1449 | Elf_Internal_Verneed *t; | |
1450 | ||
1451 | fprintf (f, _("\nVersion References:\n")); | |
1452 | for (t = elf_tdata (abfd)->verref; t != NULL; t = t->vn_nextref) | |
1453 | { | |
1454 | Elf_Internal_Vernaux *a; | |
1455 | ||
d0fb9a8d JJ |
1456 | fprintf (f, _(" required from %s:\n"), |
1457 | t->vn_filename ? t->vn_filename : "<corrupt>"); | |
252b5132 RH |
1458 | for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) |
1459 | fprintf (f, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a->vna_hash, | |
d0fb9a8d JJ |
1460 | a->vna_flags, a->vna_other, |
1461 | a->vna_nodename ? a->vna_nodename : "<corrupt>"); | |
252b5132 RH |
1462 | } |
1463 | } | |
1464 | ||
b34976b6 | 1465 | return TRUE; |
252b5132 RH |
1466 | |
1467 | error_return: | |
1468 | if (dynbuf != NULL) | |
1469 | free (dynbuf); | |
b34976b6 | 1470 | return FALSE; |
252b5132 RH |
1471 | } |
1472 | ||
bb4d2ac2 L |
1473 | /* Get version string. */ |
1474 | ||
1475 | const char * | |
60bb06bc L |
1476 | _bfd_elf_get_symbol_version_string (bfd *abfd, asymbol *symbol, |
1477 | bfd_boolean *hidden) | |
bb4d2ac2 L |
1478 | { |
1479 | const char *version_string = NULL; | |
1480 | if (elf_dynversym (abfd) != 0 | |
1481 | && (elf_dynverdef (abfd) != 0 || elf_dynverref (abfd) != 0)) | |
1482 | { | |
1483 | unsigned int vernum = ((elf_symbol_type *) symbol)->version; | |
1484 | ||
1485 | *hidden = (vernum & VERSYM_HIDDEN) != 0; | |
1486 | vernum &= VERSYM_VERSION; | |
1487 | ||
1488 | if (vernum == 0) | |
1489 | version_string = ""; | |
1490 | else if (vernum == 1) | |
1491 | version_string = "Base"; | |
1492 | else if (vernum <= elf_tdata (abfd)->cverdefs) | |
1493 | version_string = | |
1494 | elf_tdata (abfd)->verdef[vernum - 1].vd_nodename; | |
1495 | else | |
1496 | { | |
1497 | Elf_Internal_Verneed *t; | |
1498 | ||
1499 | version_string = ""; | |
1500 | for (t = elf_tdata (abfd)->verref; | |
1501 | t != NULL; | |
1502 | t = t->vn_nextref) | |
1503 | { | |
1504 | Elf_Internal_Vernaux *a; | |
1505 | ||
1506 | for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) | |
1507 | { | |
1508 | if (a->vna_other == vernum) | |
1509 | { | |
1510 | version_string = a->vna_nodename; | |
1511 | break; | |
1512 | } | |
1513 | } | |
1514 | } | |
1515 | } | |
1516 | } | |
1517 | return version_string; | |
1518 | } | |
1519 | ||
252b5132 RH |
1520 | /* Display ELF-specific fields of a symbol. */ |
1521 | ||
1522 | void | |
217aa764 AM |
1523 | bfd_elf_print_symbol (bfd *abfd, |
1524 | void *filep, | |
1525 | asymbol *symbol, | |
1526 | bfd_print_symbol_type how) | |
252b5132 | 1527 | { |
a50b1753 | 1528 | FILE *file = (FILE *) filep; |
252b5132 RH |
1529 | switch (how) |
1530 | { | |
1531 | case bfd_print_symbol_name: | |
1532 | fprintf (file, "%s", symbol->name); | |
1533 | break; | |
1534 | case bfd_print_symbol_more: | |
1535 | fprintf (file, "elf "); | |
60b89a18 | 1536 | bfd_fprintf_vma (abfd, file, symbol->value); |
0af1713e | 1537 | fprintf (file, " %lx", (unsigned long) symbol->flags); |
252b5132 RH |
1538 | break; |
1539 | case bfd_print_symbol_all: | |
1540 | { | |
4e8a9624 AM |
1541 | const char *section_name; |
1542 | const char *name = NULL; | |
9c5bfbb7 | 1543 | const struct elf_backend_data *bed; |
7a13edea | 1544 | unsigned char st_other; |
dbb410c3 | 1545 | bfd_vma val; |
bb4d2ac2 L |
1546 | const char *version_string; |
1547 | bfd_boolean hidden; | |
c044fabd | 1548 | |
252b5132 | 1549 | section_name = symbol->section ? symbol->section->name : "(*none*)"; |
587ff49e RH |
1550 | |
1551 | bed = get_elf_backend_data (abfd); | |
1552 | if (bed->elf_backend_print_symbol_all) | |
c044fabd | 1553 | name = (*bed->elf_backend_print_symbol_all) (abfd, filep, symbol); |
587ff49e RH |
1554 | |
1555 | if (name == NULL) | |
1556 | { | |
7ee38065 | 1557 | name = symbol->name; |
217aa764 | 1558 | bfd_print_symbol_vandf (abfd, file, symbol); |
587ff49e RH |
1559 | } |
1560 | ||
252b5132 RH |
1561 | fprintf (file, " %s\t", section_name); |
1562 | /* Print the "other" value for a symbol. For common symbols, | |
1563 | we've already printed the size; now print the alignment. | |
1564 | For other symbols, we have no specified alignment, and | |
1565 | we've printed the address; now print the size. */ | |
dcf6c779 | 1566 | if (symbol->section && bfd_is_com_section (symbol->section)) |
dbb410c3 AM |
1567 | val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_value; |
1568 | else | |
1569 | val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_size; | |
1570 | bfd_fprintf_vma (abfd, file, val); | |
252b5132 RH |
1571 | |
1572 | /* If we have version information, print it. */ | |
60bb06bc L |
1573 | version_string = _bfd_elf_get_symbol_version_string (abfd, |
1574 | symbol, | |
1575 | &hidden); | |
bb4d2ac2 | 1576 | if (version_string) |
252b5132 | 1577 | { |
bb4d2ac2 | 1578 | if (!hidden) |
252b5132 RH |
1579 | fprintf (file, " %-11s", version_string); |
1580 | else | |
1581 | { | |
1582 | int i; | |
1583 | ||
1584 | fprintf (file, " (%s)", version_string); | |
1585 | for (i = 10 - strlen (version_string); i > 0; --i) | |
1586 | putc (' ', file); | |
1587 | } | |
1588 | } | |
1589 | ||
1590 | /* If the st_other field is not zero, print it. */ | |
7a13edea | 1591 | st_other = ((elf_symbol_type *) symbol)->internal_elf_sym.st_other; |
c044fabd | 1592 | |
7a13edea NC |
1593 | switch (st_other) |
1594 | { | |
1595 | case 0: break; | |
1596 | case STV_INTERNAL: fprintf (file, " .internal"); break; | |
1597 | case STV_HIDDEN: fprintf (file, " .hidden"); break; | |
1598 | case STV_PROTECTED: fprintf (file, " .protected"); break; | |
1599 | default: | |
1600 | /* Some other non-defined flags are also present, so print | |
1601 | everything hex. */ | |
1602 | fprintf (file, " 0x%02x", (unsigned int) st_other); | |
1603 | } | |
252b5132 | 1604 | |
587ff49e | 1605 | fprintf (file, " %s", name); |
252b5132 RH |
1606 | } |
1607 | break; | |
1608 | } | |
1609 | } | |
252b5132 | 1610 | |
252b5132 RH |
1611 | /* Allocate an ELF string table--force the first byte to be zero. */ |
1612 | ||
1613 | struct bfd_strtab_hash * | |
217aa764 | 1614 | _bfd_elf_stringtab_init (void) |
252b5132 RH |
1615 | { |
1616 | struct bfd_strtab_hash *ret; | |
1617 | ||
1618 | ret = _bfd_stringtab_init (); | |
1619 | if (ret != NULL) | |
1620 | { | |
1621 | bfd_size_type loc; | |
1622 | ||
b34976b6 | 1623 | loc = _bfd_stringtab_add (ret, "", TRUE, FALSE); |
252b5132 RH |
1624 | BFD_ASSERT (loc == 0 || loc == (bfd_size_type) -1); |
1625 | if (loc == (bfd_size_type) -1) | |
1626 | { | |
1627 | _bfd_stringtab_free (ret); | |
1628 | ret = NULL; | |
1629 | } | |
1630 | } | |
1631 | return ret; | |
1632 | } | |
1633 | \f | |
1634 | /* ELF .o/exec file reading */ | |
1635 | ||
c044fabd | 1636 | /* Create a new bfd section from an ELF section header. */ |
252b5132 | 1637 | |
b34976b6 | 1638 | bfd_boolean |
217aa764 | 1639 | bfd_section_from_shdr (bfd *abfd, unsigned int shindex) |
252b5132 | 1640 | { |
4fbb74a6 AM |
1641 | Elf_Internal_Shdr *hdr; |
1642 | Elf_Internal_Ehdr *ehdr; | |
1643 | const struct elf_backend_data *bed; | |
90937f86 | 1644 | const char *name; |
bf67003b NC |
1645 | bfd_boolean ret = TRUE; |
1646 | static bfd_boolean * sections_being_created = NULL; | |
5a4b0ccc | 1647 | static bfd * sections_being_created_abfd = NULL; |
bf67003b | 1648 | static unsigned int nesting = 0; |
252b5132 | 1649 | |
4fbb74a6 AM |
1650 | if (shindex >= elf_numsections (abfd)) |
1651 | return FALSE; | |
1652 | ||
bf67003b NC |
1653 | if (++ nesting > 3) |
1654 | { | |
1655 | /* PR17512: A corrupt ELF binary might contain a recursive group of | |
06614111 | 1656 | sections, with each the string indicies pointing to the next in the |
bf67003b NC |
1657 | loop. Detect this here, by refusing to load a section that we are |
1658 | already in the process of loading. We only trigger this test if | |
1659 | we have nested at least three sections deep as normal ELF binaries | |
5a4b0ccc NC |
1660 | can expect to recurse at least once. |
1661 | ||
1662 | FIXME: It would be better if this array was attached to the bfd, | |
1663 | rather than being held in a static pointer. */ | |
1664 | ||
1665 | if (sections_being_created_abfd != abfd) | |
1666 | sections_being_created = NULL; | |
bf67003b NC |
1667 | if (sections_being_created == NULL) |
1668 | { | |
1669 | /* FIXME: It would be more efficient to attach this array to the bfd somehow. */ | |
1670 | sections_being_created = (bfd_boolean *) | |
1671 | bfd_zalloc (abfd, elf_numsections (abfd) * sizeof (bfd_boolean)); | |
5a4b0ccc | 1672 | sections_being_created_abfd = abfd; |
bf67003b NC |
1673 | } |
1674 | if (sections_being_created [shindex]) | |
1675 | { | |
1676 | (*_bfd_error_handler) | |
1677 | (_("%B: warning: loop in section dependencies detected"), abfd); | |
1678 | return FALSE; | |
1679 | } | |
1680 | sections_being_created [shindex] = TRUE; | |
1681 | } | |
1682 | ||
4fbb74a6 AM |
1683 | hdr = elf_elfsections (abfd)[shindex]; |
1684 | ehdr = elf_elfheader (abfd); | |
1685 | name = bfd_elf_string_from_elf_section (abfd, ehdr->e_shstrndx, | |
1b3a8575 | 1686 | hdr->sh_name); |
933d961a | 1687 | if (name == NULL) |
bf67003b | 1688 | goto fail; |
252b5132 | 1689 | |
4fbb74a6 | 1690 | bed = get_elf_backend_data (abfd); |
252b5132 RH |
1691 | switch (hdr->sh_type) |
1692 | { | |
1693 | case SHT_NULL: | |
1694 | /* Inactive section. Throw it away. */ | |
bf67003b | 1695 | goto success; |
252b5132 | 1696 | |
bf67003b NC |
1697 | case SHT_PROGBITS: /* Normal section with contents. */ |
1698 | case SHT_NOBITS: /* .bss section. */ | |
1699 | case SHT_HASH: /* .hash section. */ | |
1700 | case SHT_NOTE: /* .note section. */ | |
25e27870 L |
1701 | case SHT_INIT_ARRAY: /* .init_array section. */ |
1702 | case SHT_FINI_ARRAY: /* .fini_array section. */ | |
1703 | case SHT_PREINIT_ARRAY: /* .preinit_array section. */ | |
7f1204bb | 1704 | case SHT_GNU_LIBLIST: /* .gnu.liblist section. */ |
fdc90cb4 | 1705 | case SHT_GNU_HASH: /* .gnu.hash section. */ |
bf67003b NC |
1706 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); |
1707 | goto success; | |
252b5132 | 1708 | |
797fc050 | 1709 | case SHT_DYNAMIC: /* Dynamic linking information. */ |
6dc132d9 | 1710 | if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex)) |
bf67003b NC |
1711 | goto fail; |
1712 | ||
cfcac11d NC |
1713 | if (hdr->sh_link > elf_numsections (abfd)) |
1714 | { | |
caa83f8b | 1715 | /* PR 10478: Accept Solaris binaries with a sh_link |
cfcac11d NC |
1716 | field set to SHN_BEFORE or SHN_AFTER. */ |
1717 | switch (bfd_get_arch (abfd)) | |
1718 | { | |
caa83f8b | 1719 | case bfd_arch_i386: |
cfcac11d NC |
1720 | case bfd_arch_sparc: |
1721 | if (hdr->sh_link == (SHN_LORESERVE & 0xffff) /* SHN_BEFORE */ | |
1722 | || hdr->sh_link == ((SHN_LORESERVE + 1) & 0xffff) /* SHN_AFTER */) | |
1723 | break; | |
1724 | /* Otherwise fall through. */ | |
1725 | default: | |
bf67003b | 1726 | goto fail; |
cfcac11d NC |
1727 | } |
1728 | } | |
1729 | else if (elf_elfsections (abfd)[hdr->sh_link] == NULL) | |
bf67003b | 1730 | goto fail; |
cfcac11d | 1731 | else if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_STRTAB) |
797fc050 AM |
1732 | { |
1733 | Elf_Internal_Shdr *dynsymhdr; | |
1734 | ||
1735 | /* The shared libraries distributed with hpux11 have a bogus | |
1736 | sh_link field for the ".dynamic" section. Find the | |
1737 | string table for the ".dynsym" section instead. */ | |
1738 | if (elf_dynsymtab (abfd) != 0) | |
1739 | { | |
1740 | dynsymhdr = elf_elfsections (abfd)[elf_dynsymtab (abfd)]; | |
1741 | hdr->sh_link = dynsymhdr->sh_link; | |
1742 | } | |
1743 | else | |
1744 | { | |
1745 | unsigned int i, num_sec; | |
1746 | ||
1747 | num_sec = elf_numsections (abfd); | |
1748 | for (i = 1; i < num_sec; i++) | |
1749 | { | |
1750 | dynsymhdr = elf_elfsections (abfd)[i]; | |
1751 | if (dynsymhdr->sh_type == SHT_DYNSYM) | |
1752 | { | |
1753 | hdr->sh_link = dynsymhdr->sh_link; | |
1754 | break; | |
1755 | } | |
1756 | } | |
1757 | } | |
1758 | } | |
bf67003b | 1759 | goto success; |
797fc050 | 1760 | |
bf67003b | 1761 | case SHT_SYMTAB: /* A symbol table. */ |
252b5132 | 1762 | if (elf_onesymtab (abfd) == shindex) |
bf67003b | 1763 | goto success; |
252b5132 | 1764 | |
a50b2160 | 1765 | if (hdr->sh_entsize != bed->s->sizeof_sym) |
bf67003b NC |
1766 | goto fail; |
1767 | ||
3337c1e5 | 1768 | if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size) |
eee3b786 AM |
1769 | { |
1770 | if (hdr->sh_size != 0) | |
bf67003b | 1771 | goto fail; |
eee3b786 AM |
1772 | /* Some assemblers erroneously set sh_info to one with a |
1773 | zero sh_size. ld sees this as a global symbol count | |
1774 | of (unsigned) -1. Fix it here. */ | |
1775 | hdr->sh_info = 0; | |
bf67003b | 1776 | goto success; |
eee3b786 | 1777 | } |
bf67003b | 1778 | |
252b5132 RH |
1779 | BFD_ASSERT (elf_onesymtab (abfd) == 0); |
1780 | elf_onesymtab (abfd) = shindex; | |
1781 | elf_tdata (abfd)->symtab_hdr = *hdr; | |
1782 | elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->symtab_hdr; | |
1783 | abfd->flags |= HAS_SYMS; | |
1784 | ||
1785 | /* Sometimes a shared object will map in the symbol table. If | |
08a40648 AM |
1786 | SHF_ALLOC is set, and this is a shared object, then we also |
1787 | treat this section as a BFD section. We can not base the | |
1788 | decision purely on SHF_ALLOC, because that flag is sometimes | |
1789 | set in a relocatable object file, which would confuse the | |
1790 | linker. */ | |
252b5132 RH |
1791 | if ((hdr->sh_flags & SHF_ALLOC) != 0 |
1792 | && (abfd->flags & DYNAMIC) != 0 | |
6dc132d9 L |
1793 | && ! _bfd_elf_make_section_from_shdr (abfd, hdr, name, |
1794 | shindex)) | |
bf67003b | 1795 | goto fail; |
252b5132 | 1796 | |
1b3a8575 AM |
1797 | /* Go looking for SHT_SYMTAB_SHNDX too, since if there is one we |
1798 | can't read symbols without that section loaded as well. It | |
1799 | is most likely specified by the next section header. */ | |
1800 | if (elf_elfsections (abfd)[elf_symtab_shndx (abfd)]->sh_link != shindex) | |
1801 | { | |
1802 | unsigned int i, num_sec; | |
1803 | ||
1804 | num_sec = elf_numsections (abfd); | |
1805 | for (i = shindex + 1; i < num_sec; i++) | |
1806 | { | |
1807 | Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i]; | |
1808 | if (hdr2->sh_type == SHT_SYMTAB_SHNDX | |
1809 | && hdr2->sh_link == shindex) | |
1810 | break; | |
1811 | } | |
1812 | if (i == num_sec) | |
1813 | for (i = 1; i < shindex; i++) | |
1814 | { | |
1815 | Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i]; | |
1816 | if (hdr2->sh_type == SHT_SYMTAB_SHNDX | |
1817 | && hdr2->sh_link == shindex) | |
1818 | break; | |
1819 | } | |
1820 | if (i != shindex) | |
bf67003b | 1821 | ret = bfd_section_from_shdr (abfd, i); |
1b3a8575 | 1822 | } |
bf67003b | 1823 | goto success; |
252b5132 | 1824 | |
bf67003b | 1825 | case SHT_DYNSYM: /* A dynamic symbol table. */ |
252b5132 | 1826 | if (elf_dynsymtab (abfd) == shindex) |
bf67003b | 1827 | goto success; |
252b5132 | 1828 | |
a50b2160 | 1829 | if (hdr->sh_entsize != bed->s->sizeof_sym) |
bf67003b NC |
1830 | goto fail; |
1831 | ||
eee3b786 AM |
1832 | if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size) |
1833 | { | |
1834 | if (hdr->sh_size != 0) | |
bf67003b NC |
1835 | goto fail; |
1836 | ||
eee3b786 AM |
1837 | /* Some linkers erroneously set sh_info to one with a |
1838 | zero sh_size. ld sees this as a global symbol count | |
1839 | of (unsigned) -1. Fix it here. */ | |
1840 | hdr->sh_info = 0; | |
bf67003b | 1841 | goto success; |
eee3b786 | 1842 | } |
bf67003b | 1843 | |
252b5132 RH |
1844 | BFD_ASSERT (elf_dynsymtab (abfd) == 0); |
1845 | elf_dynsymtab (abfd) = shindex; | |
1846 | elf_tdata (abfd)->dynsymtab_hdr = *hdr; | |
1847 | elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->dynsymtab_hdr; | |
1848 | abfd->flags |= HAS_SYMS; | |
1849 | ||
1850 | /* Besides being a symbol table, we also treat this as a regular | |
1851 | section, so that objcopy can handle it. */ | |
bf67003b NC |
1852 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); |
1853 | goto success; | |
252b5132 | 1854 | |
bf67003b | 1855 | case SHT_SYMTAB_SHNDX: /* Symbol section indices when >64k sections. */ |
9ad5cbcf | 1856 | if (elf_symtab_shndx (abfd) == shindex) |
bf67003b | 1857 | goto success; |
9ad5cbcf | 1858 | |
1b3a8575 | 1859 | BFD_ASSERT (elf_symtab_shndx (abfd) == 0); |
9ad5cbcf AM |
1860 | elf_symtab_shndx (abfd) = shindex; |
1861 | elf_tdata (abfd)->symtab_shndx_hdr = *hdr; | |
1862 | elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->symtab_shndx_hdr; | |
bf67003b | 1863 | goto success; |
9ad5cbcf | 1864 | |
bf67003b | 1865 | case SHT_STRTAB: /* A string table. */ |
252b5132 | 1866 | if (hdr->bfd_section != NULL) |
bf67003b NC |
1867 | goto success; |
1868 | ||
252b5132 RH |
1869 | if (ehdr->e_shstrndx == shindex) |
1870 | { | |
1871 | elf_tdata (abfd)->shstrtab_hdr = *hdr; | |
1872 | elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->shstrtab_hdr; | |
bf67003b | 1873 | goto success; |
252b5132 | 1874 | } |
bf67003b | 1875 | |
1b3a8575 AM |
1876 | if (elf_elfsections (abfd)[elf_onesymtab (abfd)]->sh_link == shindex) |
1877 | { | |
1878 | symtab_strtab: | |
1879 | elf_tdata (abfd)->strtab_hdr = *hdr; | |
1880 | elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->strtab_hdr; | |
bf67003b | 1881 | goto success; |
1b3a8575 | 1882 | } |
bf67003b | 1883 | |
1b3a8575 AM |
1884 | if (elf_elfsections (abfd)[elf_dynsymtab (abfd)]->sh_link == shindex) |
1885 | { | |
1886 | dynsymtab_strtab: | |
1887 | elf_tdata (abfd)->dynstrtab_hdr = *hdr; | |
1888 | hdr = &elf_tdata (abfd)->dynstrtab_hdr; | |
1889 | elf_elfsections (abfd)[shindex] = hdr; | |
1890 | /* We also treat this as a regular section, so that objcopy | |
1891 | can handle it. */ | |
bf67003b NC |
1892 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, |
1893 | shindex); | |
1894 | goto success; | |
1b3a8575 | 1895 | } |
252b5132 | 1896 | |
1b3a8575 AM |
1897 | /* If the string table isn't one of the above, then treat it as a |
1898 | regular section. We need to scan all the headers to be sure, | |
1899 | just in case this strtab section appeared before the above. */ | |
1900 | if (elf_onesymtab (abfd) == 0 || elf_dynsymtab (abfd) == 0) | |
1901 | { | |
1902 | unsigned int i, num_sec; | |
252b5132 | 1903 | |
1b3a8575 AM |
1904 | num_sec = elf_numsections (abfd); |
1905 | for (i = 1; i < num_sec; i++) | |
1906 | { | |
1907 | Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i]; | |
1908 | if (hdr2->sh_link == shindex) | |
1909 | { | |
933d961a JJ |
1910 | /* Prevent endless recursion on broken objects. */ |
1911 | if (i == shindex) | |
bf67003b | 1912 | goto fail; |
1b3a8575 | 1913 | if (! bfd_section_from_shdr (abfd, i)) |
bf67003b | 1914 | goto fail; |
1b3a8575 AM |
1915 | if (elf_onesymtab (abfd) == i) |
1916 | goto symtab_strtab; | |
1917 | if (elf_dynsymtab (abfd) == i) | |
1918 | goto dynsymtab_strtab; | |
1919 | } | |
1920 | } | |
1921 | } | |
bf67003b NC |
1922 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); |
1923 | goto success; | |
252b5132 RH |
1924 | |
1925 | case SHT_REL: | |
1926 | case SHT_RELA: | |
1927 | /* *These* do a lot of work -- but build no sections! */ | |
1928 | { | |
1929 | asection *target_sect; | |
d4730f92 | 1930 | Elf_Internal_Shdr *hdr2, **p_hdr; |
9ad5cbcf | 1931 | unsigned int num_sec = elf_numsections (abfd); |
d4730f92 BS |
1932 | struct bfd_elf_section_data *esdt; |
1933 | bfd_size_type amt; | |
252b5132 | 1934 | |
aa2ca951 JJ |
1935 | if (hdr->sh_entsize |
1936 | != (bfd_size_type) (hdr->sh_type == SHT_REL | |
a50b2160 | 1937 | ? bed->s->sizeof_rel : bed->s->sizeof_rela)) |
bf67003b | 1938 | goto fail; |
a50b2160 | 1939 | |
03ae5f59 | 1940 | /* Check for a bogus link to avoid crashing. */ |
4fbb74a6 | 1941 | if (hdr->sh_link >= num_sec) |
03ae5f59 ILT |
1942 | { |
1943 | ((*_bfd_error_handler) | |
d003868e AM |
1944 | (_("%B: invalid link %lu for reloc section %s (index %u)"), |
1945 | abfd, hdr->sh_link, name, shindex)); | |
bf67003b NC |
1946 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, |
1947 | shindex); | |
1948 | goto success; | |
03ae5f59 ILT |
1949 | } |
1950 | ||
252b5132 RH |
1951 | /* For some incomprehensible reason Oracle distributes |
1952 | libraries for Solaris in which some of the objects have | |
1953 | bogus sh_link fields. It would be nice if we could just | |
1954 | reject them, but, unfortunately, some people need to use | |
1955 | them. We scan through the section headers; if we find only | |
1956 | one suitable symbol table, we clobber the sh_link to point | |
83b89087 L |
1957 | to it. I hope this doesn't break anything. |
1958 | ||
1959 | Don't do it on executable nor shared library. */ | |
1960 | if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0 | |
1961 | && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_SYMTAB | |
252b5132 RH |
1962 | && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_DYNSYM) |
1963 | { | |
9ad5cbcf | 1964 | unsigned int scan; |
252b5132 RH |
1965 | int found; |
1966 | ||
1967 | found = 0; | |
9ad5cbcf | 1968 | for (scan = 1; scan < num_sec; scan++) |
252b5132 RH |
1969 | { |
1970 | if (elf_elfsections (abfd)[scan]->sh_type == SHT_SYMTAB | |
1971 | || elf_elfsections (abfd)[scan]->sh_type == SHT_DYNSYM) | |
1972 | { | |
1973 | if (found != 0) | |
1974 | { | |
1975 | found = 0; | |
1976 | break; | |
1977 | } | |
1978 | found = scan; | |
1979 | } | |
1980 | } | |
1981 | if (found != 0) | |
1982 | hdr->sh_link = found; | |
1983 | } | |
1984 | ||
1985 | /* Get the symbol table. */ | |
1b3a8575 AM |
1986 | if ((elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_SYMTAB |
1987 | || elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_DYNSYM) | |
252b5132 | 1988 | && ! bfd_section_from_shdr (abfd, hdr->sh_link)) |
bf67003b | 1989 | goto fail; |
252b5132 RH |
1990 | |
1991 | /* If this reloc section does not use the main symbol table we | |
1992 | don't treat it as a reloc section. BFD can't adequately | |
1993 | represent such a section, so at least for now, we don't | |
c044fabd | 1994 | try. We just present it as a normal section. We also |
60bcf0fa | 1995 | can't use it as a reloc section if it points to the null |
83b89087 L |
1996 | section, an invalid section, another reloc section, or its |
1997 | sh_link points to the null section. */ | |
185ef66d | 1998 | if (hdr->sh_link != elf_onesymtab (abfd) |
83b89087 | 1999 | || hdr->sh_link == SHN_UNDEF |
185ef66d | 2000 | || hdr->sh_info == SHN_UNDEF |
185ef66d AM |
2001 | || hdr->sh_info >= num_sec |
2002 | || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_REL | |
2003 | || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_RELA) | |
bf67003b NC |
2004 | { |
2005 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, | |
2006 | shindex); | |
2007 | goto success; | |
2008 | } | |
252b5132 RH |
2009 | |
2010 | if (! bfd_section_from_shdr (abfd, hdr->sh_info)) | |
bf67003b NC |
2011 | goto fail; |
2012 | ||
252b5132 RH |
2013 | target_sect = bfd_section_from_elf_index (abfd, hdr->sh_info); |
2014 | if (target_sect == NULL) | |
bf67003b | 2015 | goto fail; |
252b5132 | 2016 | |
d4730f92 BS |
2017 | esdt = elf_section_data (target_sect); |
2018 | if (hdr->sh_type == SHT_RELA) | |
2019 | p_hdr = &esdt->rela.hdr; | |
252b5132 | 2020 | else |
d4730f92 BS |
2021 | p_hdr = &esdt->rel.hdr; |
2022 | ||
06614111 NC |
2023 | /* PR 17512: file: 0b4f81b7. */ |
2024 | if (*p_hdr != NULL) | |
2025 | goto fail; | |
d4730f92 BS |
2026 | amt = sizeof (*hdr2); |
2027 | hdr2 = (Elf_Internal_Shdr *) bfd_alloc (abfd, amt); | |
2028 | if (hdr2 == NULL) | |
bf67003b | 2029 | goto fail; |
252b5132 | 2030 | *hdr2 = *hdr; |
d4730f92 | 2031 | *p_hdr = hdr2; |
252b5132 | 2032 | elf_elfsections (abfd)[shindex] = hdr2; |
d9bc7a44 | 2033 | target_sect->reloc_count += NUM_SHDR_ENTRIES (hdr); |
252b5132 RH |
2034 | target_sect->flags |= SEC_RELOC; |
2035 | target_sect->relocation = NULL; | |
2036 | target_sect->rel_filepos = hdr->sh_offset; | |
bf572ba0 MM |
2037 | /* In the section to which the relocations apply, mark whether |
2038 | its relocations are of the REL or RELA variety. */ | |
72730e0c | 2039 | if (hdr->sh_size != 0) |
d4730f92 BS |
2040 | { |
2041 | if (hdr->sh_type == SHT_RELA) | |
2042 | target_sect->use_rela_p = 1; | |
2043 | } | |
252b5132 | 2044 | abfd->flags |= HAS_RELOC; |
bf67003b | 2045 | goto success; |
252b5132 | 2046 | } |
252b5132 RH |
2047 | |
2048 | case SHT_GNU_verdef: | |
2049 | elf_dynverdef (abfd) = shindex; | |
2050 | elf_tdata (abfd)->dynverdef_hdr = *hdr; | |
bf67003b NC |
2051 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); |
2052 | goto success; | |
252b5132 RH |
2053 | |
2054 | case SHT_GNU_versym: | |
a50b2160 | 2055 | if (hdr->sh_entsize != sizeof (Elf_External_Versym)) |
bf67003b NC |
2056 | goto fail; |
2057 | ||
252b5132 RH |
2058 | elf_dynversym (abfd) = shindex; |
2059 | elf_tdata (abfd)->dynversym_hdr = *hdr; | |
bf67003b NC |
2060 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); |
2061 | goto success; | |
252b5132 RH |
2062 | |
2063 | case SHT_GNU_verneed: | |
2064 | elf_dynverref (abfd) = shindex; | |
2065 | elf_tdata (abfd)->dynverref_hdr = *hdr; | |
bf67003b NC |
2066 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); |
2067 | goto success; | |
252b5132 RH |
2068 | |
2069 | case SHT_SHLIB: | |
bf67003b | 2070 | goto success; |
252b5132 | 2071 | |
dbb410c3 | 2072 | case SHT_GROUP: |
44534af3 | 2073 | if (! IS_VALID_GROUP_SECTION_HEADER (hdr, GRP_ENTRY_SIZE)) |
bf67003b NC |
2074 | goto fail; |
2075 | ||
6dc132d9 | 2076 | if (!_bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex)) |
bf67003b NC |
2077 | goto fail; |
2078 | ||
dbb410c3 AM |
2079 | if (hdr->contents != NULL) |
2080 | { | |
2081 | Elf_Internal_Group *idx = (Elf_Internal_Group *) hdr->contents; | |
06614111 | 2082 | unsigned int n_elt = hdr->sh_size / sizeof (* idx); |
dbb410c3 AM |
2083 | asection *s; |
2084 | ||
06614111 NC |
2085 | if (n_elt == 0) |
2086 | goto fail; | |
b885599b AM |
2087 | if (idx->flags & GRP_COMDAT) |
2088 | hdr->bfd_section->flags | |
2089 | |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD; | |
2090 | ||
45c5e9ed L |
2091 | /* We try to keep the same section order as it comes in. */ |
2092 | idx += n_elt; | |
06614111 | 2093 | |
dbb410c3 | 2094 | while (--n_elt != 0) |
1783205a NC |
2095 | { |
2096 | --idx; | |
2097 | ||
2098 | if (idx->shdr != NULL | |
2099 | && (s = idx->shdr->bfd_section) != NULL | |
2100 | && elf_next_in_group (s) != NULL) | |
2101 | { | |
2102 | elf_next_in_group (hdr->bfd_section) = s; | |
2103 | break; | |
2104 | } | |
2105 | } | |
dbb410c3 | 2106 | } |
bf67003b | 2107 | goto success; |
dbb410c3 | 2108 | |
252b5132 | 2109 | default: |
104d59d1 JM |
2110 | /* Possibly an attributes section. */ |
2111 | if (hdr->sh_type == SHT_GNU_ATTRIBUTES | |
2112 | || hdr->sh_type == bed->obj_attrs_section_type) | |
2113 | { | |
2114 | if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex)) | |
bf67003b | 2115 | goto fail; |
104d59d1 | 2116 | _bfd_elf_parse_attributes (abfd, hdr); |
bf67003b | 2117 | goto success; |
104d59d1 JM |
2118 | } |
2119 | ||
252b5132 | 2120 | /* Check for any processor-specific section types. */ |
3eb70a79 | 2121 | if (bed->elf_backend_section_from_shdr (abfd, hdr, name, shindex)) |
bf67003b | 2122 | goto success; |
3eb70a79 L |
2123 | |
2124 | if (hdr->sh_type >= SHT_LOUSER && hdr->sh_type <= SHT_HIUSER) | |
2125 | { | |
2126 | if ((hdr->sh_flags & SHF_ALLOC) != 0) | |
2127 | /* FIXME: How to properly handle allocated section reserved | |
2128 | for applications? */ | |
2129 | (*_bfd_error_handler) | |
2130 | (_("%B: don't know how to handle allocated, application " | |
2131 | "specific section `%s' [0x%8x]"), | |
2132 | abfd, name, hdr->sh_type); | |
2133 | else | |
bf67003b NC |
2134 | { |
2135 | /* Allow sections reserved for applications. */ | |
2136 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, | |
2137 | shindex); | |
2138 | goto success; | |
2139 | } | |
3eb70a79 L |
2140 | } |
2141 | else if (hdr->sh_type >= SHT_LOPROC | |
2142 | && hdr->sh_type <= SHT_HIPROC) | |
2143 | /* FIXME: We should handle this section. */ | |
2144 | (*_bfd_error_handler) | |
2145 | (_("%B: don't know how to handle processor specific section " | |
2146 | "`%s' [0x%8x]"), | |
2147 | abfd, name, hdr->sh_type); | |
2148 | else if (hdr->sh_type >= SHT_LOOS && hdr->sh_type <= SHT_HIOS) | |
ff15b240 NC |
2149 | { |
2150 | /* Unrecognised OS-specific sections. */ | |
2151 | if ((hdr->sh_flags & SHF_OS_NONCONFORMING) != 0) | |
2152 | /* SHF_OS_NONCONFORMING indicates that special knowledge is | |
08a40648 | 2153 | required to correctly process the section and the file should |
ff15b240 NC |
2154 | be rejected with an error message. */ |
2155 | (*_bfd_error_handler) | |
2156 | (_("%B: don't know how to handle OS specific section " | |
2157 | "`%s' [0x%8x]"), | |
2158 | abfd, name, hdr->sh_type); | |
2159 | else | |
bf67003b NC |
2160 | { |
2161 | /* Otherwise it should be processed. */ | |
2162 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); | |
2163 | goto success; | |
2164 | } | |
ff15b240 | 2165 | } |
3eb70a79 L |
2166 | else |
2167 | /* FIXME: We should handle this section. */ | |
2168 | (*_bfd_error_handler) | |
2169 | (_("%B: don't know how to handle section `%s' [0x%8x]"), | |
2170 | abfd, name, hdr->sh_type); | |
2171 | ||
bf67003b | 2172 | goto fail; |
252b5132 RH |
2173 | } |
2174 | ||
bf67003b NC |
2175 | fail: |
2176 | ret = FALSE; | |
2177 | success: | |
e5b470e2 | 2178 | if (sections_being_created && sections_being_created_abfd == abfd) |
bf67003b NC |
2179 | sections_being_created [shindex] = FALSE; |
2180 | if (-- nesting == 0) | |
5a4b0ccc NC |
2181 | { |
2182 | sections_being_created = NULL; | |
2183 | sections_being_created_abfd = abfd; | |
2184 | } | |
bf67003b | 2185 | return ret; |
252b5132 RH |
2186 | } |
2187 | ||
87d72d41 | 2188 | /* Return the local symbol specified by ABFD, R_SYMNDX. */ |
ec338859 | 2189 | |
87d72d41 AM |
2190 | Elf_Internal_Sym * |
2191 | bfd_sym_from_r_symndx (struct sym_cache *cache, | |
2192 | bfd *abfd, | |
2193 | unsigned long r_symndx) | |
ec338859 | 2194 | { |
ec338859 AM |
2195 | unsigned int ent = r_symndx % LOCAL_SYM_CACHE_SIZE; |
2196 | ||
a5d1b3b5 AM |
2197 | if (cache->abfd != abfd || cache->indx[ent] != r_symndx) |
2198 | { | |
2199 | Elf_Internal_Shdr *symtab_hdr; | |
2200 | unsigned char esym[sizeof (Elf64_External_Sym)]; | |
2201 | Elf_External_Sym_Shndx eshndx; | |
ec338859 | 2202 | |
a5d1b3b5 AM |
2203 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; |
2204 | if (bfd_elf_get_elf_syms (abfd, symtab_hdr, 1, r_symndx, | |
87d72d41 | 2205 | &cache->sym[ent], esym, &eshndx) == NULL) |
a5d1b3b5 | 2206 | return NULL; |
9ad5cbcf | 2207 | |
a5d1b3b5 AM |
2208 | if (cache->abfd != abfd) |
2209 | { | |
2210 | memset (cache->indx, -1, sizeof (cache->indx)); | |
2211 | cache->abfd = abfd; | |
2212 | } | |
2213 | cache->indx[ent] = r_symndx; | |
ec338859 | 2214 | } |
a5d1b3b5 | 2215 | |
87d72d41 | 2216 | return &cache->sym[ent]; |
ec338859 AM |
2217 | } |
2218 | ||
252b5132 RH |
2219 | /* Given an ELF section number, retrieve the corresponding BFD |
2220 | section. */ | |
2221 | ||
2222 | asection * | |
91d6fa6a | 2223 | bfd_section_from_elf_index (bfd *abfd, unsigned int sec_index) |
252b5132 | 2224 | { |
91d6fa6a | 2225 | if (sec_index >= elf_numsections (abfd)) |
252b5132 | 2226 | return NULL; |
91d6fa6a | 2227 | return elf_elfsections (abfd)[sec_index]->bfd_section; |
252b5132 RH |
2228 | } |
2229 | ||
b35d266b | 2230 | static const struct bfd_elf_special_section special_sections_b[] = |
2f89ff8d | 2231 | { |
0112cd26 NC |
2232 | { STRING_COMMA_LEN (".bss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE }, |
2233 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2234 | }; |
2235 | ||
b35d266b | 2236 | static const struct bfd_elf_special_section special_sections_c[] = |
7f4d3958 | 2237 | { |
0112cd26 NC |
2238 | { STRING_COMMA_LEN (".comment"), 0, SHT_PROGBITS, 0 }, |
2239 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2240 | }; |
2241 | ||
b35d266b | 2242 | static const struct bfd_elf_special_section special_sections_d[] = |
7f4d3958 | 2243 | { |
0112cd26 NC |
2244 | { STRING_COMMA_LEN (".data"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE }, |
2245 | { STRING_COMMA_LEN (".data1"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE }, | |
a9a72a65 DE |
2246 | /* There are more DWARF sections than these, but they needn't be added here |
2247 | unless you have to cope with broken compilers that don't emit section | |
2248 | attributes or you want to help the user writing assembler. */ | |
0112cd26 NC |
2249 | { STRING_COMMA_LEN (".debug"), 0, SHT_PROGBITS, 0 }, |
2250 | { STRING_COMMA_LEN (".debug_line"), 0, SHT_PROGBITS, 0 }, | |
2251 | { STRING_COMMA_LEN (".debug_info"), 0, SHT_PROGBITS, 0 }, | |
2252 | { STRING_COMMA_LEN (".debug_abbrev"), 0, SHT_PROGBITS, 0 }, | |
2253 | { STRING_COMMA_LEN (".debug_aranges"), 0, SHT_PROGBITS, 0 }, | |
2254 | { STRING_COMMA_LEN (".dynamic"), 0, SHT_DYNAMIC, SHF_ALLOC }, | |
2255 | { STRING_COMMA_LEN (".dynstr"), 0, SHT_STRTAB, SHF_ALLOC }, | |
2256 | { STRING_COMMA_LEN (".dynsym"), 0, SHT_DYNSYM, SHF_ALLOC }, | |
2257 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2258 | }; |
2259 | ||
b35d266b | 2260 | static const struct bfd_elf_special_section special_sections_f[] = |
7f4d3958 | 2261 | { |
0112cd26 NC |
2262 | { STRING_COMMA_LEN (".fini"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR }, |
2263 | { STRING_COMMA_LEN (".fini_array"), 0, SHT_FINI_ARRAY, SHF_ALLOC + SHF_WRITE }, | |
2264 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2265 | }; |
2266 | ||
b35d266b | 2267 | static const struct bfd_elf_special_section special_sections_g[] = |
7f4d3958 | 2268 | { |
0112cd26 | 2269 | { STRING_COMMA_LEN (".gnu.linkonce.b"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE }, |
68efed41 | 2270 | { STRING_COMMA_LEN (".gnu.lto_"), -1, SHT_PROGBITS, SHF_EXCLUDE }, |
0112cd26 NC |
2271 | { STRING_COMMA_LEN (".got"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE }, |
2272 | { STRING_COMMA_LEN (".gnu.version"), 0, SHT_GNU_versym, 0 }, | |
2273 | { STRING_COMMA_LEN (".gnu.version_d"), 0, SHT_GNU_verdef, 0 }, | |
2274 | { STRING_COMMA_LEN (".gnu.version_r"), 0, SHT_GNU_verneed, 0 }, | |
2275 | { STRING_COMMA_LEN (".gnu.liblist"), 0, SHT_GNU_LIBLIST, SHF_ALLOC }, | |
2276 | { STRING_COMMA_LEN (".gnu.conflict"), 0, SHT_RELA, SHF_ALLOC }, | |
2277 | { STRING_COMMA_LEN (".gnu.hash"), 0, SHT_GNU_HASH, SHF_ALLOC }, | |
2278 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2279 | }; |
2280 | ||
b35d266b | 2281 | static const struct bfd_elf_special_section special_sections_h[] = |
7f4d3958 | 2282 | { |
0112cd26 NC |
2283 | { STRING_COMMA_LEN (".hash"), 0, SHT_HASH, SHF_ALLOC }, |
2284 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2285 | }; |
2286 | ||
b35d266b | 2287 | static const struct bfd_elf_special_section special_sections_i[] = |
7f4d3958 | 2288 | { |
0112cd26 NC |
2289 | { STRING_COMMA_LEN (".init"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR }, |
2290 | { STRING_COMMA_LEN (".init_array"), 0, SHT_INIT_ARRAY, SHF_ALLOC + SHF_WRITE }, | |
2291 | { STRING_COMMA_LEN (".interp"), 0, SHT_PROGBITS, 0 }, | |
2292 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2293 | }; |
2294 | ||
b35d266b | 2295 | static const struct bfd_elf_special_section special_sections_l[] = |
7f4d3958 | 2296 | { |
0112cd26 NC |
2297 | { STRING_COMMA_LEN (".line"), 0, SHT_PROGBITS, 0 }, |
2298 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2299 | }; |
2300 | ||
b35d266b | 2301 | static const struct bfd_elf_special_section special_sections_n[] = |
7f4d3958 | 2302 | { |
0112cd26 NC |
2303 | { STRING_COMMA_LEN (".note.GNU-stack"), 0, SHT_PROGBITS, 0 }, |
2304 | { STRING_COMMA_LEN (".note"), -1, SHT_NOTE, 0 }, | |
2305 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2306 | }; |
2307 | ||
b35d266b | 2308 | static const struct bfd_elf_special_section special_sections_p[] = |
7f4d3958 | 2309 | { |
0112cd26 NC |
2310 | { STRING_COMMA_LEN (".preinit_array"), 0, SHT_PREINIT_ARRAY, SHF_ALLOC + SHF_WRITE }, |
2311 | { STRING_COMMA_LEN (".plt"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR }, | |
2312 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2313 | }; |
2314 | ||
b35d266b | 2315 | static const struct bfd_elf_special_section special_sections_r[] = |
7f4d3958 | 2316 | { |
0112cd26 NC |
2317 | { STRING_COMMA_LEN (".rodata"), -2, SHT_PROGBITS, SHF_ALLOC }, |
2318 | { STRING_COMMA_LEN (".rodata1"), 0, SHT_PROGBITS, SHF_ALLOC }, | |
2319 | { STRING_COMMA_LEN (".rela"), -1, SHT_RELA, 0 }, | |
2320 | { STRING_COMMA_LEN (".rel"), -1, SHT_REL, 0 }, | |
2321 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2322 | }; |
2323 | ||
b35d266b | 2324 | static const struct bfd_elf_special_section special_sections_s[] = |
7f4d3958 | 2325 | { |
0112cd26 NC |
2326 | { STRING_COMMA_LEN (".shstrtab"), 0, SHT_STRTAB, 0 }, |
2327 | { STRING_COMMA_LEN (".strtab"), 0, SHT_STRTAB, 0 }, | |
2328 | { STRING_COMMA_LEN (".symtab"), 0, SHT_SYMTAB, 0 }, | |
60ff4dc4 HPN |
2329 | /* See struct bfd_elf_special_section declaration for the semantics of |
2330 | this special case where .prefix_length != strlen (.prefix). */ | |
2331 | { ".stabstr", 5, 3, SHT_STRTAB, 0 }, | |
0112cd26 | 2332 | { NULL, 0, 0, 0, 0 } |
2f89ff8d L |
2333 | }; |
2334 | ||
b35d266b | 2335 | static const struct bfd_elf_special_section special_sections_t[] = |
7f4d3958 | 2336 | { |
0112cd26 NC |
2337 | { STRING_COMMA_LEN (".text"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR }, |
2338 | { STRING_COMMA_LEN (".tbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS }, | |
2339 | { STRING_COMMA_LEN (".tdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS }, | |
2340 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2341 | }; |
2342 | ||
1b315056 CS |
2343 | static const struct bfd_elf_special_section special_sections_z[] = |
2344 | { | |
2345 | { STRING_COMMA_LEN (".zdebug_line"), 0, SHT_PROGBITS, 0 }, | |
2346 | { STRING_COMMA_LEN (".zdebug_info"), 0, SHT_PROGBITS, 0 }, | |
2347 | { STRING_COMMA_LEN (".zdebug_abbrev"), 0, SHT_PROGBITS, 0 }, | |
2348 | { STRING_COMMA_LEN (".zdebug_aranges"), 0, SHT_PROGBITS, 0 }, | |
2349 | { NULL, 0, 0, 0, 0 } | |
2350 | }; | |
2351 | ||
e4c93b56 | 2352 | static const struct bfd_elf_special_section * const special_sections[] = |
7f4d3958 | 2353 | { |
7f4d3958 | 2354 | special_sections_b, /* 'b' */ |
98ece1b3 | 2355 | special_sections_c, /* 'c' */ |
7f4d3958 L |
2356 | special_sections_d, /* 'd' */ |
2357 | NULL, /* 'e' */ | |
2358 | special_sections_f, /* 'f' */ | |
2359 | special_sections_g, /* 'g' */ | |
2360 | special_sections_h, /* 'h' */ | |
2361 | special_sections_i, /* 'i' */ | |
2362 | NULL, /* 'j' */ | |
2363 | NULL, /* 'k' */ | |
2364 | special_sections_l, /* 'l' */ | |
2365 | NULL, /* 'm' */ | |
2366 | special_sections_n, /* 'n' */ | |
2367 | NULL, /* 'o' */ | |
2368 | special_sections_p, /* 'p' */ | |
2369 | NULL, /* 'q' */ | |
2370 | special_sections_r, /* 'r' */ | |
2371 | special_sections_s, /* 's' */ | |
2372 | special_sections_t, /* 't' */ | |
1b315056 CS |
2373 | NULL, /* 'u' */ |
2374 | NULL, /* 'v' */ | |
2375 | NULL, /* 'w' */ | |
2376 | NULL, /* 'x' */ | |
2377 | NULL, /* 'y' */ | |
2378 | special_sections_z /* 'z' */ | |
7f4d3958 L |
2379 | }; |
2380 | ||
551b43fd AM |
2381 | const struct bfd_elf_special_section * |
2382 | _bfd_elf_get_special_section (const char *name, | |
2383 | const struct bfd_elf_special_section *spec, | |
2384 | unsigned int rela) | |
2f89ff8d L |
2385 | { |
2386 | int i; | |
7f4d3958 | 2387 | int len; |
7f4d3958 | 2388 | |
551b43fd | 2389 | len = strlen (name); |
7f4d3958 | 2390 | |
551b43fd | 2391 | for (i = 0; spec[i].prefix != NULL; i++) |
7dcb9820 AM |
2392 | { |
2393 | int suffix_len; | |
551b43fd | 2394 | int prefix_len = spec[i].prefix_length; |
7dcb9820 AM |
2395 | |
2396 | if (len < prefix_len) | |
2397 | continue; | |
551b43fd | 2398 | if (memcmp (name, spec[i].prefix, prefix_len) != 0) |
7dcb9820 AM |
2399 | continue; |
2400 | ||
551b43fd | 2401 | suffix_len = spec[i].suffix_length; |
7dcb9820 AM |
2402 | if (suffix_len <= 0) |
2403 | { | |
2404 | if (name[prefix_len] != 0) | |
2405 | { | |
2406 | if (suffix_len == 0) | |
2407 | continue; | |
2408 | if (name[prefix_len] != '.' | |
2409 | && (suffix_len == -2 | |
551b43fd | 2410 | || (rela && spec[i].type == SHT_REL))) |
7dcb9820 AM |
2411 | continue; |
2412 | } | |
2413 | } | |
2414 | else | |
2415 | { | |
2416 | if (len < prefix_len + suffix_len) | |
2417 | continue; | |
2418 | if (memcmp (name + len - suffix_len, | |
551b43fd | 2419 | spec[i].prefix + prefix_len, |
7dcb9820 AM |
2420 | suffix_len) != 0) |
2421 | continue; | |
2422 | } | |
551b43fd | 2423 | return &spec[i]; |
7dcb9820 | 2424 | } |
2f89ff8d L |
2425 | |
2426 | return NULL; | |
2427 | } | |
2428 | ||
7dcb9820 | 2429 | const struct bfd_elf_special_section * |
29ef7005 | 2430 | _bfd_elf_get_sec_type_attr (bfd *abfd, asection *sec) |
2f89ff8d | 2431 | { |
551b43fd AM |
2432 | int i; |
2433 | const struct bfd_elf_special_section *spec; | |
29ef7005 | 2434 | const struct elf_backend_data *bed; |
2f89ff8d L |
2435 | |
2436 | /* See if this is one of the special sections. */ | |
551b43fd AM |
2437 | if (sec->name == NULL) |
2438 | return NULL; | |
2f89ff8d | 2439 | |
29ef7005 L |
2440 | bed = get_elf_backend_data (abfd); |
2441 | spec = bed->special_sections; | |
2442 | if (spec) | |
2443 | { | |
2444 | spec = _bfd_elf_get_special_section (sec->name, | |
2445 | bed->special_sections, | |
2446 | sec->use_rela_p); | |
2447 | if (spec != NULL) | |
2448 | return spec; | |
2449 | } | |
2450 | ||
551b43fd AM |
2451 | if (sec->name[0] != '.') |
2452 | return NULL; | |
2f89ff8d | 2453 | |
551b43fd | 2454 | i = sec->name[1] - 'b'; |
1b315056 | 2455 | if (i < 0 || i > 'z' - 'b') |
551b43fd AM |
2456 | return NULL; |
2457 | ||
2458 | spec = special_sections[i]; | |
2f89ff8d | 2459 | |
551b43fd AM |
2460 | if (spec == NULL) |
2461 | return NULL; | |
2462 | ||
2463 | return _bfd_elf_get_special_section (sec->name, spec, sec->use_rela_p); | |
2f89ff8d L |
2464 | } |
2465 | ||
b34976b6 | 2466 | bfd_boolean |
217aa764 | 2467 | _bfd_elf_new_section_hook (bfd *abfd, asection *sec) |
252b5132 RH |
2468 | { |
2469 | struct bfd_elf_section_data *sdata; | |
551b43fd | 2470 | const struct elf_backend_data *bed; |
7dcb9820 | 2471 | const struct bfd_elf_special_section *ssect; |
252b5132 | 2472 | |
f0abc2a1 AM |
2473 | sdata = (struct bfd_elf_section_data *) sec->used_by_bfd; |
2474 | if (sdata == NULL) | |
2475 | { | |
a50b1753 NC |
2476 | sdata = (struct bfd_elf_section_data *) bfd_zalloc (abfd, |
2477 | sizeof (*sdata)); | |
f0abc2a1 AM |
2478 | if (sdata == NULL) |
2479 | return FALSE; | |
217aa764 | 2480 | sec->used_by_bfd = sdata; |
f0abc2a1 | 2481 | } |
bf572ba0 | 2482 | |
551b43fd AM |
2483 | /* Indicate whether or not this section should use RELA relocations. */ |
2484 | bed = get_elf_backend_data (abfd); | |
2485 | sec->use_rela_p = bed->default_use_rela_p; | |
2486 | ||
e843e0f8 L |
2487 | /* When we read a file, we don't need to set ELF section type and |
2488 | flags. They will be overridden in _bfd_elf_make_section_from_shdr | |
2489 | anyway. We will set ELF section type and flags for all linker | |
2490 | created sections. If user specifies BFD section flags, we will | |
2491 | set ELF section type and flags based on BFD section flags in | |
02ecc8e9 L |
2492 | elf_fake_sections. Special handling for .init_array/.fini_array |
2493 | output sections since they may contain .ctors/.dtors input | |
2494 | sections. We don't want _bfd_elf_init_private_section_data to | |
2495 | copy ELF section type from .ctors/.dtors input sections. */ | |
2496 | if (abfd->direction != read_direction | |
3496cb2a | 2497 | || (sec->flags & SEC_LINKER_CREATED) != 0) |
2f89ff8d | 2498 | { |
551b43fd | 2499 | ssect = (*bed->get_sec_type_attr) (abfd, sec); |
02ecc8e9 L |
2500 | if (ssect != NULL |
2501 | && (!sec->flags | |
2502 | || (sec->flags & SEC_LINKER_CREATED) != 0 | |
2503 | || ssect->type == SHT_INIT_ARRAY | |
2504 | || ssect->type == SHT_FINI_ARRAY)) | |
a31501e9 L |
2505 | { |
2506 | elf_section_type (sec) = ssect->type; | |
2507 | elf_section_flags (sec) = ssect->attr; | |
2508 | } | |
2f89ff8d L |
2509 | } |
2510 | ||
f592407e | 2511 | return _bfd_generic_new_section_hook (abfd, sec); |
252b5132 RH |
2512 | } |
2513 | ||
2514 | /* Create a new bfd section from an ELF program header. | |
2515 | ||
2516 | Since program segments have no names, we generate a synthetic name | |
2517 | of the form segment<NUM>, where NUM is generally the index in the | |
2518 | program header table. For segments that are split (see below) we | |
2519 | generate the names segment<NUM>a and segment<NUM>b. | |
2520 | ||
2521 | Note that some program segments may have a file size that is different than | |
2522 | (less than) the memory size. All this means is that at execution the | |
2523 | system must allocate the amount of memory specified by the memory size, | |
2524 | but only initialize it with the first "file size" bytes read from the | |
2525 | file. This would occur for example, with program segments consisting | |
2526 | of combined data+bss. | |
2527 | ||
2528 | To handle the above situation, this routine generates TWO bfd sections | |
2529 | for the single program segment. The first has the length specified by | |
2530 | the file size of the segment, and the second has the length specified | |
2531 | by the difference between the two sizes. In effect, the segment is split | |
d5191d0c | 2532 | into its initialized and uninitialized parts. |
252b5132 RH |
2533 | |
2534 | */ | |
2535 | ||
b34976b6 | 2536 | bfd_boolean |
217aa764 AM |
2537 | _bfd_elf_make_section_from_phdr (bfd *abfd, |
2538 | Elf_Internal_Phdr *hdr, | |
91d6fa6a | 2539 | int hdr_index, |
a50b1753 | 2540 | const char *type_name) |
252b5132 RH |
2541 | { |
2542 | asection *newsect; | |
2543 | char *name; | |
2544 | char namebuf[64]; | |
d4c88bbb | 2545 | size_t len; |
252b5132 RH |
2546 | int split; |
2547 | ||
2548 | split = ((hdr->p_memsz > 0) | |
2549 | && (hdr->p_filesz > 0) | |
2550 | && (hdr->p_memsz > hdr->p_filesz)); | |
d5191d0c AM |
2551 | |
2552 | if (hdr->p_filesz > 0) | |
252b5132 | 2553 | { |
91d6fa6a | 2554 | sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "a" : ""); |
d5191d0c | 2555 | len = strlen (namebuf) + 1; |
a50b1753 | 2556 | name = (char *) bfd_alloc (abfd, len); |
d5191d0c AM |
2557 | if (!name) |
2558 | return FALSE; | |
2559 | memcpy (name, namebuf, len); | |
2560 | newsect = bfd_make_section (abfd, name); | |
2561 | if (newsect == NULL) | |
2562 | return FALSE; | |
2563 | newsect->vma = hdr->p_vaddr; | |
2564 | newsect->lma = hdr->p_paddr; | |
2565 | newsect->size = hdr->p_filesz; | |
2566 | newsect->filepos = hdr->p_offset; | |
2567 | newsect->flags |= SEC_HAS_CONTENTS; | |
2568 | newsect->alignment_power = bfd_log2 (hdr->p_align); | |
2569 | if (hdr->p_type == PT_LOAD) | |
252b5132 | 2570 | { |
d5191d0c AM |
2571 | newsect->flags |= SEC_ALLOC; |
2572 | newsect->flags |= SEC_LOAD; | |
2573 | if (hdr->p_flags & PF_X) | |
2574 | { | |
2575 | /* FIXME: all we known is that it has execute PERMISSION, | |
2576 | may be data. */ | |
2577 | newsect->flags |= SEC_CODE; | |
2578 | } | |
2579 | } | |
2580 | if (!(hdr->p_flags & PF_W)) | |
2581 | { | |
2582 | newsect->flags |= SEC_READONLY; | |
252b5132 | 2583 | } |
252b5132 RH |
2584 | } |
2585 | ||
d5191d0c | 2586 | if (hdr->p_memsz > hdr->p_filesz) |
252b5132 | 2587 | { |
d5191d0c AM |
2588 | bfd_vma align; |
2589 | ||
91d6fa6a | 2590 | sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "b" : ""); |
d4c88bbb | 2591 | len = strlen (namebuf) + 1; |
a50b1753 | 2592 | name = (char *) bfd_alloc (abfd, len); |
252b5132 | 2593 | if (!name) |
b34976b6 | 2594 | return FALSE; |
d4c88bbb | 2595 | memcpy (name, namebuf, len); |
252b5132 RH |
2596 | newsect = bfd_make_section (abfd, name); |
2597 | if (newsect == NULL) | |
b34976b6 | 2598 | return FALSE; |
252b5132 RH |
2599 | newsect->vma = hdr->p_vaddr + hdr->p_filesz; |
2600 | newsect->lma = hdr->p_paddr + hdr->p_filesz; | |
eea6121a | 2601 | newsect->size = hdr->p_memsz - hdr->p_filesz; |
d5191d0c AM |
2602 | newsect->filepos = hdr->p_offset + hdr->p_filesz; |
2603 | align = newsect->vma & -newsect->vma; | |
2604 | if (align == 0 || align > hdr->p_align) | |
2605 | align = hdr->p_align; | |
2606 | newsect->alignment_power = bfd_log2 (align); | |
252b5132 RH |
2607 | if (hdr->p_type == PT_LOAD) |
2608 | { | |
d5191d0c AM |
2609 | /* Hack for gdb. Segments that have not been modified do |
2610 | not have their contents written to a core file, on the | |
2611 | assumption that a debugger can find the contents in the | |
2612 | executable. We flag this case by setting the fake | |
2613 | section size to zero. Note that "real" bss sections will | |
2614 | always have their contents dumped to the core file. */ | |
2615 | if (bfd_get_format (abfd) == bfd_core) | |
2616 | newsect->size = 0; | |
252b5132 RH |
2617 | newsect->flags |= SEC_ALLOC; |
2618 | if (hdr->p_flags & PF_X) | |
2619 | newsect->flags |= SEC_CODE; | |
2620 | } | |
2621 | if (!(hdr->p_flags & PF_W)) | |
2622 | newsect->flags |= SEC_READONLY; | |
2623 | } | |
2624 | ||
b34976b6 | 2625 | return TRUE; |
252b5132 RH |
2626 | } |
2627 | ||
b34976b6 | 2628 | bfd_boolean |
91d6fa6a | 2629 | bfd_section_from_phdr (bfd *abfd, Elf_Internal_Phdr *hdr, int hdr_index) |
20cfcaae | 2630 | { |
9c5bfbb7 | 2631 | const struct elf_backend_data *bed; |
20cfcaae NC |
2632 | |
2633 | switch (hdr->p_type) | |
2634 | { | |
2635 | case PT_NULL: | |
91d6fa6a | 2636 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "null"); |
20cfcaae NC |
2637 | |
2638 | case PT_LOAD: | |
91d6fa6a | 2639 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "load"); |
20cfcaae NC |
2640 | |
2641 | case PT_DYNAMIC: | |
91d6fa6a | 2642 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "dynamic"); |
20cfcaae NC |
2643 | |
2644 | case PT_INTERP: | |
91d6fa6a | 2645 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "interp"); |
20cfcaae NC |
2646 | |
2647 | case PT_NOTE: | |
91d6fa6a | 2648 | if (! _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "note")) |
b34976b6 | 2649 | return FALSE; |
718175fa | 2650 | if (! elf_read_notes (abfd, hdr->p_offset, hdr->p_filesz)) |
b34976b6 AM |
2651 | return FALSE; |
2652 | return TRUE; | |
20cfcaae NC |
2653 | |
2654 | case PT_SHLIB: | |
91d6fa6a | 2655 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "shlib"); |
20cfcaae NC |
2656 | |
2657 | case PT_PHDR: | |
91d6fa6a | 2658 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "phdr"); |
20cfcaae | 2659 | |
811072d8 | 2660 | case PT_GNU_EH_FRAME: |
91d6fa6a | 2661 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, |
811072d8 RM |
2662 | "eh_frame_hdr"); |
2663 | ||
2b05f1b7 | 2664 | case PT_GNU_STACK: |
91d6fa6a | 2665 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "stack"); |
9ee5e499 | 2666 | |
8c37241b | 2667 | case PT_GNU_RELRO: |
91d6fa6a | 2668 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "relro"); |
8c37241b | 2669 | |
20cfcaae | 2670 | default: |
8c1acd09 | 2671 | /* Check for any processor-specific program segment types. */ |
20cfcaae | 2672 | bed = get_elf_backend_data (abfd); |
91d6fa6a | 2673 | return bed->elf_backend_section_from_phdr (abfd, hdr, hdr_index, "proc"); |
20cfcaae NC |
2674 | } |
2675 | } | |
2676 | ||
d4730f92 BS |
2677 | /* Return the REL_HDR for SEC, assuming there is only a single one, either |
2678 | REL or RELA. */ | |
2679 | ||
2680 | Elf_Internal_Shdr * | |
2681 | _bfd_elf_single_rel_hdr (asection *sec) | |
2682 | { | |
2683 | if (elf_section_data (sec)->rel.hdr) | |
2684 | { | |
2685 | BFD_ASSERT (elf_section_data (sec)->rela.hdr == NULL); | |
2686 | return elf_section_data (sec)->rel.hdr; | |
2687 | } | |
2688 | else | |
2689 | return elf_section_data (sec)->rela.hdr; | |
2690 | } | |
2691 | ||
2692 | /* Allocate and initialize a section-header for a new reloc section, | |
2693 | containing relocations against ASECT. It is stored in RELDATA. If | |
2694 | USE_RELA_P is TRUE, we use RELA relocations; otherwise, we use REL | |
2695 | relocations. */ | |
23bc299b | 2696 | |
5d13b3b3 | 2697 | static bfd_boolean |
217aa764 | 2698 | _bfd_elf_init_reloc_shdr (bfd *abfd, |
d4730f92 | 2699 | struct bfd_elf_section_reloc_data *reldata, |
f6fe1ccd | 2700 | const char *sec_name, |
217aa764 | 2701 | bfd_boolean use_rela_p) |
23bc299b | 2702 | { |
d4730f92 | 2703 | Elf_Internal_Shdr *rel_hdr; |
23bc299b | 2704 | char *name; |
9c5bfbb7 | 2705 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
d4730f92 BS |
2706 | bfd_size_type amt; |
2707 | ||
2708 | amt = sizeof (Elf_Internal_Shdr); | |
2709 | BFD_ASSERT (reldata->hdr == NULL); | |
2710 | rel_hdr = bfd_zalloc (abfd, amt); | |
2711 | reldata->hdr = rel_hdr; | |
23bc299b | 2712 | |
f6fe1ccd | 2713 | amt = sizeof ".rela" + strlen (sec_name); |
a50b1753 | 2714 | name = (char *) bfd_alloc (abfd, amt); |
23bc299b | 2715 | if (name == NULL) |
b34976b6 | 2716 | return FALSE; |
f6fe1ccd | 2717 | sprintf (name, "%s%s", use_rela_p ? ".rela" : ".rel", sec_name); |
23bc299b | 2718 | rel_hdr->sh_name = |
2b0f7ef9 | 2719 | (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), name, |
b34976b6 | 2720 | FALSE); |
23bc299b | 2721 | if (rel_hdr->sh_name == (unsigned int) -1) |
b34976b6 | 2722 | return FALSE; |
23bc299b MM |
2723 | rel_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL; |
2724 | rel_hdr->sh_entsize = (use_rela_p | |
2725 | ? bed->s->sizeof_rela | |
2726 | : bed->s->sizeof_rel); | |
72de5009 | 2727 | rel_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align; |
23bc299b MM |
2728 | rel_hdr->sh_flags = 0; |
2729 | rel_hdr->sh_addr = 0; | |
2730 | rel_hdr->sh_size = 0; | |
2731 | rel_hdr->sh_offset = 0; | |
2732 | ||
b34976b6 | 2733 | return TRUE; |
23bc299b MM |
2734 | } |
2735 | ||
94be91de JB |
2736 | /* Return the default section type based on the passed in section flags. */ |
2737 | ||
2738 | int | |
2739 | bfd_elf_get_default_section_type (flagword flags) | |
2740 | { | |
2741 | if ((flags & SEC_ALLOC) != 0 | |
2e76e85a | 2742 | && (flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0) |
94be91de JB |
2743 | return SHT_NOBITS; |
2744 | return SHT_PROGBITS; | |
2745 | } | |
2746 | ||
d4730f92 BS |
2747 | struct fake_section_arg |
2748 | { | |
2749 | struct bfd_link_info *link_info; | |
2750 | bfd_boolean failed; | |
2751 | }; | |
2752 | ||
252b5132 RH |
2753 | /* Set up an ELF internal section header for a section. */ |
2754 | ||
252b5132 | 2755 | static void |
d4730f92 | 2756 | elf_fake_sections (bfd *abfd, asection *asect, void *fsarg) |
252b5132 | 2757 | { |
d4730f92 | 2758 | struct fake_section_arg *arg = (struct fake_section_arg *)fsarg; |
9c5bfbb7 | 2759 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
d4730f92 | 2760 | struct bfd_elf_section_data *esd = elf_section_data (asect); |
252b5132 | 2761 | Elf_Internal_Shdr *this_hdr; |
0414f35b | 2762 | unsigned int sh_type; |
0ce398f1 | 2763 | const char *name = asect->name; |
252b5132 | 2764 | |
d4730f92 | 2765 | if (arg->failed) |
252b5132 RH |
2766 | { |
2767 | /* We already failed; just get out of the bfd_map_over_sections | |
08a40648 | 2768 | loop. */ |
252b5132 RH |
2769 | return; |
2770 | } | |
2771 | ||
d4730f92 | 2772 | this_hdr = &esd->this_hdr; |
252b5132 | 2773 | |
f6fe1ccd | 2774 | if (arg->link_info) |
0ce398f1 | 2775 | { |
f6fe1ccd L |
2776 | /* ld: compress DWARF debug sections with names: .debug_*. */ |
2777 | if ((arg->link_info->compress_debug & COMPRESS_DEBUG) | |
2778 | && (asect->flags & SEC_DEBUGGING) | |
2779 | && name[1] == 'd' | |
2780 | && name[6] == '_') | |
2781 | { | |
2782 | /* Set SEC_ELF_COMPRESS to indicate this section should be | |
2783 | compressed. */ | |
2784 | asect->flags |= SEC_ELF_COMPRESS; | |
0ce398f1 | 2785 | |
f6fe1ccd L |
2786 | if (arg->link_info->compress_debug != COMPRESS_DEBUG_GABI_ZLIB) |
2787 | { | |
2788 | /* If SHF_COMPRESSED isn't used, rename compressed DWARF | |
2789 | debug section to .zdebug_*. */ | |
2790 | char *new_name = convert_debug_to_zdebug (abfd, name); | |
2791 | if (new_name == NULL) | |
2792 | { | |
2793 | arg->failed = TRUE; | |
2794 | return; | |
2795 | } | |
2796 | bfd_rename_section (abfd, asect, new_name); | |
2797 | name = asect->name; | |
2798 | } | |
2799 | } | |
2800 | } | |
2801 | else if ((asect->flags & SEC_ELF_RENAME)) | |
2802 | { | |
2803 | /* objcopy: rename output DWARF debug section. */ | |
2804 | if ((abfd->flags & (BFD_DECOMPRESS | BFD_COMPRESS_GABI))) | |
2805 | { | |
2806 | /* When we decompress or compress with SHF_COMPRESSED, | |
2807 | convert section name from .zdebug_* to .debug_* if | |
2808 | needed. */ | |
2809 | if (name[1] == 'z') | |
2810 | { | |
2811 | char *new_name = convert_zdebug_to_debug (abfd, name); | |
2812 | if (new_name == NULL) | |
2813 | { | |
2814 | arg->failed = TRUE; | |
2815 | return; | |
2816 | } | |
2817 | name = new_name; | |
2818 | } | |
2819 | } | |
2820 | else if (asect->compress_status == COMPRESS_SECTION_DONE) | |
0ce398f1 | 2821 | { |
f6fe1ccd L |
2822 | /* PR binutils/18087: Compression does not always make a |
2823 | section smaller. So only rename the section when | |
2824 | compression has actually taken place. If input section | |
2825 | name is .zdebug_*, we should never compress it again. */ | |
2826 | char *new_name = convert_debug_to_zdebug (abfd, name); | |
0ce398f1 L |
2827 | if (new_name == NULL) |
2828 | { | |
2829 | arg->failed = TRUE; | |
2830 | return; | |
2831 | } | |
f6fe1ccd L |
2832 | BFD_ASSERT (name[1] != 'z'); |
2833 | name = new_name; | |
0ce398f1 L |
2834 | } |
2835 | } | |
2836 | ||
e57b5356 | 2837 | this_hdr->sh_name = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), |
0ce398f1 | 2838 | name, FALSE); |
e57b5356 | 2839 | if (this_hdr->sh_name == (unsigned int) -1) |
252b5132 | 2840 | { |
d4730f92 | 2841 | arg->failed = TRUE; |
252b5132 RH |
2842 | return; |
2843 | } | |
2844 | ||
a4d8e49b | 2845 | /* Don't clear sh_flags. Assembler may set additional bits. */ |
252b5132 RH |
2846 | |
2847 | if ((asect->flags & SEC_ALLOC) != 0 | |
2848 | || asect->user_set_vma) | |
2849 | this_hdr->sh_addr = asect->vma; | |
2850 | else | |
2851 | this_hdr->sh_addr = 0; | |
2852 | ||
2853 | this_hdr->sh_offset = 0; | |
eea6121a | 2854 | this_hdr->sh_size = asect->size; |
252b5132 | 2855 | this_hdr->sh_link = 0; |
c86934ce NC |
2856 | /* PR 17512: file: 0eb809fe, 8b0535ee. */ |
2857 | if (asect->alignment_power >= (sizeof (bfd_vma) * 8) - 1) | |
2858 | { | |
2859 | (*_bfd_error_handler) | |
2860 | (_("%B: error: Alignment power %d of section `%A' is too big"), | |
2861 | abfd, asect, asect->alignment_power); | |
2862 | arg->failed = TRUE; | |
2863 | return; | |
2864 | } | |
72de5009 | 2865 | this_hdr->sh_addralign = (bfd_vma) 1 << asect->alignment_power; |
252b5132 RH |
2866 | /* The sh_entsize and sh_info fields may have been set already by |
2867 | copy_private_section_data. */ | |
2868 | ||
2869 | this_hdr->bfd_section = asect; | |
2870 | this_hdr->contents = NULL; | |
2871 | ||
3cddba1e L |
2872 | /* If the section type is unspecified, we set it based on |
2873 | asect->flags. */ | |
98ece1b3 AM |
2874 | if ((asect->flags & SEC_GROUP) != 0) |
2875 | sh_type = SHT_GROUP; | |
98ece1b3 | 2876 | else |
94be91de | 2877 | sh_type = bfd_elf_get_default_section_type (asect->flags); |
98ece1b3 | 2878 | |
3cddba1e | 2879 | if (this_hdr->sh_type == SHT_NULL) |
98ece1b3 AM |
2880 | this_hdr->sh_type = sh_type; |
2881 | else if (this_hdr->sh_type == SHT_NOBITS | |
2882 | && sh_type == SHT_PROGBITS | |
2883 | && (asect->flags & SEC_ALLOC) != 0) | |
3cddba1e | 2884 | { |
98ece1b3 AM |
2885 | /* Warn if we are changing a NOBITS section to PROGBITS, but |
2886 | allow the link to proceed. This can happen when users link | |
2887 | non-bss input sections to bss output sections, or emit data | |
2888 | to a bss output section via a linker script. */ | |
2889 | (*_bfd_error_handler) | |
58f0869b | 2890 | (_("warning: section `%A' type changed to PROGBITS"), asect); |
98ece1b3 | 2891 | this_hdr->sh_type = sh_type; |
3cddba1e L |
2892 | } |
2893 | ||
2f89ff8d | 2894 | switch (this_hdr->sh_type) |
252b5132 | 2895 | { |
2f89ff8d | 2896 | default: |
2f89ff8d L |
2897 | break; |
2898 | ||
2899 | case SHT_STRTAB: | |
2900 | case SHT_INIT_ARRAY: | |
2901 | case SHT_FINI_ARRAY: | |
2902 | case SHT_PREINIT_ARRAY: | |
2903 | case SHT_NOTE: | |
2904 | case SHT_NOBITS: | |
2905 | case SHT_PROGBITS: | |
2906 | break; | |
2907 | ||
2908 | case SHT_HASH: | |
c7ac6ff8 | 2909 | this_hdr->sh_entsize = bed->s->sizeof_hash_entry; |
2f89ff8d | 2910 | break; |
5de3bf90 | 2911 | |
2f89ff8d | 2912 | case SHT_DYNSYM: |
252b5132 | 2913 | this_hdr->sh_entsize = bed->s->sizeof_sym; |
2f89ff8d L |
2914 | break; |
2915 | ||
2916 | case SHT_DYNAMIC: | |
252b5132 | 2917 | this_hdr->sh_entsize = bed->s->sizeof_dyn; |
2f89ff8d L |
2918 | break; |
2919 | ||
2920 | case SHT_RELA: | |
2921 | if (get_elf_backend_data (abfd)->may_use_rela_p) | |
2922 | this_hdr->sh_entsize = bed->s->sizeof_rela; | |
2923 | break; | |
2924 | ||
2925 | case SHT_REL: | |
2926 | if (get_elf_backend_data (abfd)->may_use_rel_p) | |
2927 | this_hdr->sh_entsize = bed->s->sizeof_rel; | |
2928 | break; | |
2929 | ||
2930 | case SHT_GNU_versym: | |
252b5132 | 2931 | this_hdr->sh_entsize = sizeof (Elf_External_Versym); |
2f89ff8d L |
2932 | break; |
2933 | ||
2934 | case SHT_GNU_verdef: | |
252b5132 RH |
2935 | this_hdr->sh_entsize = 0; |
2936 | /* objcopy or strip will copy over sh_info, but may not set | |
08a40648 AM |
2937 | cverdefs. The linker will set cverdefs, but sh_info will be |
2938 | zero. */ | |
252b5132 RH |
2939 | if (this_hdr->sh_info == 0) |
2940 | this_hdr->sh_info = elf_tdata (abfd)->cverdefs; | |
2941 | else | |
2942 | BFD_ASSERT (elf_tdata (abfd)->cverdefs == 0 | |
2943 | || this_hdr->sh_info == elf_tdata (abfd)->cverdefs); | |
2f89ff8d L |
2944 | break; |
2945 | ||
2946 | case SHT_GNU_verneed: | |
252b5132 RH |
2947 | this_hdr->sh_entsize = 0; |
2948 | /* objcopy or strip will copy over sh_info, but may not set | |
08a40648 AM |
2949 | cverrefs. The linker will set cverrefs, but sh_info will be |
2950 | zero. */ | |
252b5132 RH |
2951 | if (this_hdr->sh_info == 0) |
2952 | this_hdr->sh_info = elf_tdata (abfd)->cverrefs; | |
2953 | else | |
2954 | BFD_ASSERT (elf_tdata (abfd)->cverrefs == 0 | |
2955 | || this_hdr->sh_info == elf_tdata (abfd)->cverrefs); | |
2f89ff8d L |
2956 | break; |
2957 | ||
2958 | case SHT_GROUP: | |
1783205a | 2959 | this_hdr->sh_entsize = GRP_ENTRY_SIZE; |
2f89ff8d | 2960 | break; |
fdc90cb4 JJ |
2961 | |
2962 | case SHT_GNU_HASH: | |
2963 | this_hdr->sh_entsize = bed->s->arch_size == 64 ? 0 : 4; | |
2964 | break; | |
dbb410c3 | 2965 | } |
252b5132 RH |
2966 | |
2967 | if ((asect->flags & SEC_ALLOC) != 0) | |
2968 | this_hdr->sh_flags |= SHF_ALLOC; | |
2969 | if ((asect->flags & SEC_READONLY) == 0) | |
2970 | this_hdr->sh_flags |= SHF_WRITE; | |
2971 | if ((asect->flags & SEC_CODE) != 0) | |
2972 | this_hdr->sh_flags |= SHF_EXECINSTR; | |
f5fa8ca2 JJ |
2973 | if ((asect->flags & SEC_MERGE) != 0) |
2974 | { | |
2975 | this_hdr->sh_flags |= SHF_MERGE; | |
2976 | this_hdr->sh_entsize = asect->entsize; | |
2977 | if ((asect->flags & SEC_STRINGS) != 0) | |
2978 | this_hdr->sh_flags |= SHF_STRINGS; | |
2979 | } | |
1126897b | 2980 | if ((asect->flags & SEC_GROUP) == 0 && elf_group_name (asect) != NULL) |
dbb410c3 | 2981 | this_hdr->sh_flags |= SHF_GROUP; |
13ae64f3 | 2982 | if ((asect->flags & SEC_THREAD_LOCAL) != 0) |
704afa60 JJ |
2983 | { |
2984 | this_hdr->sh_flags |= SHF_TLS; | |
3a800eb9 AM |
2985 | if (asect->size == 0 |
2986 | && (asect->flags & SEC_HAS_CONTENTS) == 0) | |
704afa60 | 2987 | { |
3a800eb9 | 2988 | struct bfd_link_order *o = asect->map_tail.link_order; |
b34976b6 | 2989 | |
704afa60 | 2990 | this_hdr->sh_size = 0; |
3a800eb9 AM |
2991 | if (o != NULL) |
2992 | { | |
704afa60 | 2993 | this_hdr->sh_size = o->offset + o->size; |
3a800eb9 AM |
2994 | if (this_hdr->sh_size != 0) |
2995 | this_hdr->sh_type = SHT_NOBITS; | |
2996 | } | |
704afa60 JJ |
2997 | } |
2998 | } | |
18ae9cc1 L |
2999 | if ((asect->flags & (SEC_GROUP | SEC_EXCLUDE)) == SEC_EXCLUDE) |
3000 | this_hdr->sh_flags |= SHF_EXCLUDE; | |
252b5132 | 3001 | |
d4730f92 BS |
3002 | /* If the section has relocs, set up a section header for the |
3003 | SHT_REL[A] section. If two relocation sections are required for | |
3004 | this section, it is up to the processor-specific back-end to | |
3005 | create the other. */ | |
3006 | if ((asect->flags & SEC_RELOC) != 0) | |
3007 | { | |
3008 | /* When doing a relocatable link, create both REL and RELA sections if | |
3009 | needed. */ | |
3010 | if (arg->link_info | |
3011 | /* Do the normal setup if we wouldn't create any sections here. */ | |
3012 | && esd->rel.count + esd->rela.count > 0 | |
3013 | && (arg->link_info->relocatable || arg->link_info->emitrelocations)) | |
3014 | { | |
3015 | if (esd->rel.count && esd->rel.hdr == NULL | |
f6fe1ccd | 3016 | && !_bfd_elf_init_reloc_shdr (abfd, &esd->rel, name, FALSE)) |
d4730f92 BS |
3017 | { |
3018 | arg->failed = TRUE; | |
3019 | return; | |
3020 | } | |
3021 | if (esd->rela.count && esd->rela.hdr == NULL | |
f6fe1ccd | 3022 | && !_bfd_elf_init_reloc_shdr (abfd, &esd->rela, name, TRUE)) |
d4730f92 BS |
3023 | { |
3024 | arg->failed = TRUE; | |
3025 | return; | |
3026 | } | |
3027 | } | |
3028 | else if (!_bfd_elf_init_reloc_shdr (abfd, | |
3029 | (asect->use_rela_p | |
3030 | ? &esd->rela : &esd->rel), | |
f6fe1ccd | 3031 | name, |
d4730f92 BS |
3032 | asect->use_rela_p)) |
3033 | arg->failed = TRUE; | |
3034 | } | |
3035 | ||
252b5132 | 3036 | /* Check for processor-specific section types. */ |
0414f35b | 3037 | sh_type = this_hdr->sh_type; |
e1fddb6b AO |
3038 | if (bed->elf_backend_fake_sections |
3039 | && !(*bed->elf_backend_fake_sections) (abfd, this_hdr, asect)) | |
d4730f92 | 3040 | arg->failed = TRUE; |
252b5132 | 3041 | |
42bb2e33 | 3042 | if (sh_type == SHT_NOBITS && asect->size != 0) |
0414f35b AM |
3043 | { |
3044 | /* Don't change the header type from NOBITS if we are being | |
42bb2e33 | 3045 | called for objcopy --only-keep-debug. */ |
0414f35b AM |
3046 | this_hdr->sh_type = sh_type; |
3047 | } | |
252b5132 RH |
3048 | } |
3049 | ||
bcacc0f5 AM |
3050 | /* Fill in the contents of a SHT_GROUP section. Called from |
3051 | _bfd_elf_compute_section_file_positions for gas, objcopy, and | |
3052 | when ELF targets use the generic linker, ld. Called for ld -r | |
3053 | from bfd_elf_final_link. */ | |
dbb410c3 | 3054 | |
1126897b | 3055 | void |
217aa764 | 3056 | bfd_elf_set_group_contents (bfd *abfd, asection *sec, void *failedptrarg) |
dbb410c3 | 3057 | { |
a50b1753 | 3058 | bfd_boolean *failedptr = (bfd_boolean *) failedptrarg; |
9dce4196 | 3059 | asection *elt, *first; |
dbb410c3 | 3060 | unsigned char *loc; |
b34976b6 | 3061 | bfd_boolean gas; |
dbb410c3 | 3062 | |
7e4111ad L |
3063 | /* Ignore linker created group section. See elfNN_ia64_object_p in |
3064 | elfxx-ia64.c. */ | |
3065 | if (((sec->flags & (SEC_GROUP | SEC_LINKER_CREATED)) != SEC_GROUP) | |
dbb410c3 AM |
3066 | || *failedptr) |
3067 | return; | |
3068 | ||
bcacc0f5 AM |
3069 | if (elf_section_data (sec)->this_hdr.sh_info == 0) |
3070 | { | |
3071 | unsigned long symindx = 0; | |
3072 | ||
3073 | /* elf_group_id will have been set up by objcopy and the | |
3074 | generic linker. */ | |
3075 | if (elf_group_id (sec) != NULL) | |
3076 | symindx = elf_group_id (sec)->udata.i; | |
1126897b | 3077 | |
bcacc0f5 AM |
3078 | if (symindx == 0) |
3079 | { | |
3080 | /* If called from the assembler, swap_out_syms will have set up | |
3081 | elf_section_syms. */ | |
3082 | BFD_ASSERT (elf_section_syms (abfd) != NULL); | |
3083 | symindx = elf_section_syms (abfd)[sec->index]->udata.i; | |
3084 | } | |
3085 | elf_section_data (sec)->this_hdr.sh_info = symindx; | |
3086 | } | |
3087 | else if (elf_section_data (sec)->this_hdr.sh_info == (unsigned int) -2) | |
1126897b | 3088 | { |
bcacc0f5 AM |
3089 | /* The ELF backend linker sets sh_info to -2 when the group |
3090 | signature symbol is global, and thus the index can't be | |
3091 | set until all local symbols are output. */ | |
3092 | asection *igroup = elf_sec_group (elf_next_in_group (sec)); | |
3093 | struct bfd_elf_section_data *sec_data = elf_section_data (igroup); | |
3094 | unsigned long symndx = sec_data->this_hdr.sh_info; | |
3095 | unsigned long extsymoff = 0; | |
3096 | struct elf_link_hash_entry *h; | |
3097 | ||
3098 | if (!elf_bad_symtab (igroup->owner)) | |
3099 | { | |
3100 | Elf_Internal_Shdr *symtab_hdr; | |
3101 | ||
3102 | symtab_hdr = &elf_tdata (igroup->owner)->symtab_hdr; | |
3103 | extsymoff = symtab_hdr->sh_info; | |
3104 | } | |
3105 | h = elf_sym_hashes (igroup->owner)[symndx - extsymoff]; | |
3106 | while (h->root.type == bfd_link_hash_indirect | |
3107 | || h->root.type == bfd_link_hash_warning) | |
3108 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
3109 | ||
3110 | elf_section_data (sec)->this_hdr.sh_info = h->indx; | |
1126897b | 3111 | } |
dbb410c3 | 3112 | |
1126897b | 3113 | /* The contents won't be allocated for "ld -r" or objcopy. */ |
b34976b6 | 3114 | gas = TRUE; |
dbb410c3 AM |
3115 | if (sec->contents == NULL) |
3116 | { | |
b34976b6 | 3117 | gas = FALSE; |
a50b1753 | 3118 | sec->contents = (unsigned char *) bfd_alloc (abfd, sec->size); |
9dce4196 AM |
3119 | |
3120 | /* Arrange for the section to be written out. */ | |
3121 | elf_section_data (sec)->this_hdr.contents = sec->contents; | |
dbb410c3 AM |
3122 | if (sec->contents == NULL) |
3123 | { | |
b34976b6 | 3124 | *failedptr = TRUE; |
dbb410c3 AM |
3125 | return; |
3126 | } | |
3127 | } | |
3128 | ||
eea6121a | 3129 | loc = sec->contents + sec->size; |
dbb410c3 | 3130 | |
9dce4196 AM |
3131 | /* Get the pointer to the first section in the group that gas |
3132 | squirreled away here. objcopy arranges for this to be set to the | |
3133 | start of the input section group. */ | |
3134 | first = elt = elf_next_in_group (sec); | |
dbb410c3 AM |
3135 | |
3136 | /* First element is a flag word. Rest of section is elf section | |
3137 | indices for all the sections of the group. Write them backwards | |
3138 | just to keep the group in the same order as given in .section | |
3139 | directives, not that it matters. */ | |
3140 | while (elt != NULL) | |
3141 | { | |
9dce4196 | 3142 | asection *s; |
9dce4196 | 3143 | |
9dce4196 | 3144 | s = elt; |
415f38a6 AM |
3145 | if (!gas) |
3146 | s = s->output_section; | |
3147 | if (s != NULL | |
3148 | && !bfd_is_abs_section (s)) | |
01e1a5bc | 3149 | { |
415f38a6 AM |
3150 | unsigned int idx = elf_section_data (s)->this_idx; |
3151 | ||
01e1a5bc | 3152 | loc -= 4; |
01e1a5bc NC |
3153 | H_PUT_32 (abfd, idx, loc); |
3154 | } | |
945906ff | 3155 | elt = elf_next_in_group (elt); |
9dce4196 AM |
3156 | if (elt == first) |
3157 | break; | |
dbb410c3 AM |
3158 | } |
3159 | ||
3d7f7666 | 3160 | if ((loc -= 4) != sec->contents) |
9dce4196 | 3161 | abort (); |
dbb410c3 | 3162 | |
9dce4196 | 3163 | H_PUT_32 (abfd, sec->flags & SEC_LINK_ONCE ? GRP_COMDAT : 0, loc); |
dbb410c3 AM |
3164 | } |
3165 | ||
bd53a53a L |
3166 | /* Return the section which RELOC_SEC applies to. */ |
3167 | ||
3168 | asection * | |
3169 | _bfd_elf_get_reloc_section (asection *reloc_sec) | |
3170 | { | |
3171 | const char *name; | |
3172 | unsigned int type; | |
3173 | bfd *abfd; | |
3174 | ||
3175 | if (reloc_sec == NULL) | |
3176 | return NULL; | |
3177 | ||
3178 | type = elf_section_data (reloc_sec)->this_hdr.sh_type; | |
3179 | if (type != SHT_REL && type != SHT_RELA) | |
3180 | return NULL; | |
3181 | ||
3182 | /* We look up the section the relocs apply to by name. */ | |
3183 | name = reloc_sec->name; | |
3184 | if (type == SHT_REL) | |
3185 | name += 4; | |
3186 | else | |
3187 | name += 5; | |
3188 | ||
3189 | /* If a target needs .got.plt section, relocations in rela.plt/rel.plt | |
3190 | section apply to .got.plt section. */ | |
3191 | abfd = reloc_sec->owner; | |
3192 | if (get_elf_backend_data (abfd)->want_got_plt | |
3193 | && strcmp (name, ".plt") == 0) | |
87070c08 AM |
3194 | { |
3195 | /* .got.plt is a linker created input section. It may be mapped | |
3196 | to some other output section. Try two likely sections. */ | |
3197 | name = ".got.plt"; | |
3198 | reloc_sec = bfd_get_section_by_name (abfd, name); | |
3199 | if (reloc_sec != NULL) | |
3200 | return reloc_sec; | |
3201 | name = ".got"; | |
3202 | } | |
bd53a53a L |
3203 | |
3204 | reloc_sec = bfd_get_section_by_name (abfd, name); | |
3205 | return reloc_sec; | |
3206 | } | |
3207 | ||
252b5132 RH |
3208 | /* Assign all ELF section numbers. The dummy first section is handled here |
3209 | too. The link/info pointers for the standard section types are filled | |
3210 | in here too, while we're at it. */ | |
3211 | ||
b34976b6 | 3212 | static bfd_boolean |
da9f89d4 | 3213 | assign_section_numbers (bfd *abfd, struct bfd_link_info *link_info) |
252b5132 RH |
3214 | { |
3215 | struct elf_obj_tdata *t = elf_tdata (abfd); | |
3216 | asection *sec; | |
2b0f7ef9 | 3217 | unsigned int section_number, secn; |
252b5132 | 3218 | Elf_Internal_Shdr **i_shdrp; |
47cc2cf5 | 3219 | struct bfd_elf_section_data *d; |
3516e984 | 3220 | bfd_boolean need_symtab; |
252b5132 RH |
3221 | |
3222 | section_number = 1; | |
3223 | ||
2b0f7ef9 JJ |
3224 | _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd)); |
3225 | ||
da9f89d4 L |
3226 | /* SHT_GROUP sections are in relocatable files only. */ |
3227 | if (link_info == NULL || link_info->relocatable) | |
252b5132 | 3228 | { |
da9f89d4 | 3229 | /* Put SHT_GROUP sections first. */ |
04dd1667 | 3230 | for (sec = abfd->sections; sec != NULL; sec = sec->next) |
47cc2cf5 | 3231 | { |
5daa8fe7 | 3232 | d = elf_section_data (sec); |
da9f89d4 L |
3233 | |
3234 | if (d->this_hdr.sh_type == SHT_GROUP) | |
08a40648 | 3235 | { |
5daa8fe7 | 3236 | if (sec->flags & SEC_LINKER_CREATED) |
da9f89d4 L |
3237 | { |
3238 | /* Remove the linker created SHT_GROUP sections. */ | |
5daa8fe7 | 3239 | bfd_section_list_remove (abfd, sec); |
da9f89d4 | 3240 | abfd->section_count--; |
da9f89d4 | 3241 | } |
08a40648 | 3242 | else |
4fbb74a6 | 3243 | d->this_idx = section_number++; |
da9f89d4 | 3244 | } |
47cc2cf5 PB |
3245 | } |
3246 | } | |
3247 | ||
3248 | for (sec = abfd->sections; sec; sec = sec->next) | |
3249 | { | |
3250 | d = elf_section_data (sec); | |
3251 | ||
3252 | if (d->this_hdr.sh_type != SHT_GROUP) | |
4fbb74a6 | 3253 | d->this_idx = section_number++; |
2b0f7ef9 | 3254 | _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->this_hdr.sh_name); |
d4730f92 | 3255 | if (d->rel.hdr) |
2b0f7ef9 | 3256 | { |
d4730f92 BS |
3257 | d->rel.idx = section_number++; |
3258 | _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel.hdr->sh_name); | |
2b0f7ef9 | 3259 | } |
d4730f92 BS |
3260 | else |
3261 | d->rel.idx = 0; | |
23bc299b | 3262 | |
d4730f92 | 3263 | if (d->rela.hdr) |
2b0f7ef9 | 3264 | { |
d4730f92 BS |
3265 | d->rela.idx = section_number++; |
3266 | _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rela.hdr->sh_name); | |
2b0f7ef9 | 3267 | } |
23bc299b | 3268 | else |
d4730f92 | 3269 | d->rela.idx = 0; |
252b5132 RH |
3270 | } |
3271 | ||
12bd6957 | 3272 | elf_shstrtab_sec (abfd) = section_number++; |
2b0f7ef9 | 3273 | _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->shstrtab_hdr.sh_name); |
12bd6957 | 3274 | elf_elfheader (abfd)->e_shstrndx = elf_shstrtab_sec (abfd); |
252b5132 | 3275 | |
3516e984 L |
3276 | need_symtab = (bfd_get_symcount (abfd) > 0 |
3277 | || (link_info == NULL | |
3278 | && ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC)) | |
3279 | == HAS_RELOC))); | |
3280 | if (need_symtab) | |
252b5132 | 3281 | { |
12bd6957 | 3282 | elf_onesymtab (abfd) = section_number++; |
2b0f7ef9 | 3283 | _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->symtab_hdr.sh_name); |
4fbb74a6 | 3284 | if (section_number > ((SHN_LORESERVE - 2) & 0xFFFF)) |
9ad5cbcf | 3285 | { |
12bd6957 | 3286 | elf_symtab_shndx (abfd) = section_number++; |
9ad5cbcf AM |
3287 | t->symtab_shndx_hdr.sh_name |
3288 | = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), | |
b34976b6 | 3289 | ".symtab_shndx", FALSE); |
9ad5cbcf | 3290 | if (t->symtab_shndx_hdr.sh_name == (unsigned int) -1) |
b34976b6 | 3291 | return FALSE; |
9ad5cbcf | 3292 | } |
12bd6957 | 3293 | elf_strtab_sec (abfd) = section_number++; |
2b0f7ef9 | 3294 | _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->strtab_hdr.sh_name); |
252b5132 RH |
3295 | } |
3296 | ||
1c52a645 L |
3297 | if (section_number >= SHN_LORESERVE) |
3298 | { | |
3299 | _bfd_error_handler (_("%B: too many sections: %u"), | |
3300 | abfd, section_number); | |
3301 | return FALSE; | |
3302 | } | |
3303 | ||
2b0f7ef9 JJ |
3304 | _bfd_elf_strtab_finalize (elf_shstrtab (abfd)); |
3305 | t->shstrtab_hdr.sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd)); | |
9ad5cbcf AM |
3306 | |
3307 | elf_numsections (abfd) = section_number; | |
252b5132 RH |
3308 | elf_elfheader (abfd)->e_shnum = section_number; |
3309 | ||
3310 | /* Set up the list of section header pointers, in agreement with the | |
3311 | indices. */ | |
a50b1753 NC |
3312 | i_shdrp = (Elf_Internal_Shdr **) bfd_zalloc2 (abfd, section_number, |
3313 | sizeof (Elf_Internal_Shdr *)); | |
252b5132 | 3314 | if (i_shdrp == NULL) |
b34976b6 | 3315 | return FALSE; |
252b5132 | 3316 | |
a50b1753 NC |
3317 | i_shdrp[0] = (Elf_Internal_Shdr *) bfd_zalloc (abfd, |
3318 | sizeof (Elf_Internal_Shdr)); | |
252b5132 RH |
3319 | if (i_shdrp[0] == NULL) |
3320 | { | |
3321 | bfd_release (abfd, i_shdrp); | |
b34976b6 | 3322 | return FALSE; |
252b5132 | 3323 | } |
252b5132 RH |
3324 | |
3325 | elf_elfsections (abfd) = i_shdrp; | |
3326 | ||
12bd6957 | 3327 | i_shdrp[elf_shstrtab_sec (abfd)] = &t->shstrtab_hdr; |
3516e984 | 3328 | if (need_symtab) |
252b5132 | 3329 | { |
12bd6957 | 3330 | i_shdrp[elf_onesymtab (abfd)] = &t->symtab_hdr; |
4fbb74a6 | 3331 | if (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF)) |
9ad5cbcf | 3332 | { |
12bd6957 AM |
3333 | i_shdrp[elf_symtab_shndx (abfd)] = &t->symtab_shndx_hdr; |
3334 | t->symtab_shndx_hdr.sh_link = elf_onesymtab (abfd); | |
9ad5cbcf | 3335 | } |
12bd6957 AM |
3336 | i_shdrp[elf_strtab_sec (abfd)] = &t->strtab_hdr; |
3337 | t->symtab_hdr.sh_link = elf_strtab_sec (abfd); | |
252b5132 | 3338 | } |
38ce5b11 | 3339 | |
252b5132 RH |
3340 | for (sec = abfd->sections; sec; sec = sec->next) |
3341 | { | |
252b5132 | 3342 | asection *s; |
252b5132 | 3343 | |
91d6fa6a NC |
3344 | d = elf_section_data (sec); |
3345 | ||
252b5132 | 3346 | i_shdrp[d->this_idx] = &d->this_hdr; |
d4730f92 BS |
3347 | if (d->rel.idx != 0) |
3348 | i_shdrp[d->rel.idx] = d->rel.hdr; | |
3349 | if (d->rela.idx != 0) | |
3350 | i_shdrp[d->rela.idx] = d->rela.hdr; | |
252b5132 RH |
3351 | |
3352 | /* Fill in the sh_link and sh_info fields while we're at it. */ | |
3353 | ||
3354 | /* sh_link of a reloc section is the section index of the symbol | |
3355 | table. sh_info is the section index of the section to which | |
3356 | the relocation entries apply. */ | |
d4730f92 | 3357 | if (d->rel.idx != 0) |
252b5132 | 3358 | { |
12bd6957 | 3359 | d->rel.hdr->sh_link = elf_onesymtab (abfd); |
d4730f92 | 3360 | d->rel.hdr->sh_info = d->this_idx; |
9ef5d938 | 3361 | d->rel.hdr->sh_flags |= SHF_INFO_LINK; |
252b5132 | 3362 | } |
d4730f92 | 3363 | if (d->rela.idx != 0) |
23bc299b | 3364 | { |
12bd6957 | 3365 | d->rela.hdr->sh_link = elf_onesymtab (abfd); |
d4730f92 | 3366 | d->rela.hdr->sh_info = d->this_idx; |
9ef5d938 | 3367 | d->rela.hdr->sh_flags |= SHF_INFO_LINK; |
23bc299b | 3368 | } |
252b5132 | 3369 | |
38ce5b11 L |
3370 | /* We need to set up sh_link for SHF_LINK_ORDER. */ |
3371 | if ((d->this_hdr.sh_flags & SHF_LINK_ORDER) != 0) | |
3372 | { | |
3373 | s = elf_linked_to_section (sec); | |
3374 | if (s) | |
38ce5b11 | 3375 | { |
f2876037 | 3376 | /* elf_linked_to_section points to the input section. */ |
ccd2ec6a | 3377 | if (link_info != NULL) |
38ce5b11 | 3378 | { |
f2876037 | 3379 | /* Check discarded linkonce section. */ |
dbaa2011 | 3380 | if (discarded_section (s)) |
38ce5b11 | 3381 | { |
ccd2ec6a L |
3382 | asection *kept; |
3383 | (*_bfd_error_handler) | |
3384 | (_("%B: sh_link of section `%A' points to discarded section `%A' of `%B'"), | |
3385 | abfd, d->this_hdr.bfd_section, | |
3386 | s, s->owner); | |
3387 | /* Point to the kept section if it has the same | |
3388 | size as the discarded one. */ | |
c0f00686 | 3389 | kept = _bfd_elf_check_kept_section (s, link_info); |
ccd2ec6a | 3390 | if (kept == NULL) |
185d09ad | 3391 | { |
ccd2ec6a L |
3392 | bfd_set_error (bfd_error_bad_value); |
3393 | return FALSE; | |
185d09ad | 3394 | } |
ccd2ec6a | 3395 | s = kept; |
38ce5b11 | 3396 | } |
e424ecc8 | 3397 | |
ccd2ec6a L |
3398 | s = s->output_section; |
3399 | BFD_ASSERT (s != NULL); | |
38ce5b11 | 3400 | } |
f2876037 L |
3401 | else |
3402 | { | |
3403 | /* Handle objcopy. */ | |
3404 | if (s->output_section == NULL) | |
3405 | { | |
3406 | (*_bfd_error_handler) | |
3407 | (_("%B: sh_link of section `%A' points to removed section `%A' of `%B'"), | |
3408 | abfd, d->this_hdr.bfd_section, s, s->owner); | |
3409 | bfd_set_error (bfd_error_bad_value); | |
3410 | return FALSE; | |
3411 | } | |
3412 | s = s->output_section; | |
3413 | } | |
ccd2ec6a L |
3414 | d->this_hdr.sh_link = elf_section_data (s)->this_idx; |
3415 | } | |
3416 | else | |
3417 | { | |
3418 | /* PR 290: | |
3419 | The Intel C compiler generates SHT_IA_64_UNWIND with | |
3420 | SHF_LINK_ORDER. But it doesn't set the sh_link or | |
3421 | sh_info fields. Hence we could get the situation | |
08a40648 | 3422 | where s is NULL. */ |
ccd2ec6a L |
3423 | const struct elf_backend_data *bed |
3424 | = get_elf_backend_data (abfd); | |
3425 | if (bed->link_order_error_handler) | |
3426 | bed->link_order_error_handler | |
3427 | (_("%B: warning: sh_link not set for section `%A'"), | |
3428 | abfd, sec); | |
38ce5b11 L |
3429 | } |
3430 | } | |
3431 | ||
252b5132 RH |
3432 | switch (d->this_hdr.sh_type) |
3433 | { | |
3434 | case SHT_REL: | |
3435 | case SHT_RELA: | |
3436 | /* A reloc section which we are treating as a normal BFD | |
3437 | section. sh_link is the section index of the symbol | |
3438 | table. sh_info is the section index of the section to | |
3439 | which the relocation entries apply. We assume that an | |
3440 | allocated reloc section uses the dynamic symbol table. | |
3441 | FIXME: How can we be sure? */ | |
3442 | s = bfd_get_section_by_name (abfd, ".dynsym"); | |
3443 | if (s != NULL) | |
3444 | d->this_hdr.sh_link = elf_section_data (s)->this_idx; | |
3445 | ||
bd53a53a | 3446 | s = get_elf_backend_data (abfd)->get_reloc_section (sec); |
252b5132 | 3447 | if (s != NULL) |
9ef5d938 L |
3448 | { |
3449 | d->this_hdr.sh_info = elf_section_data (s)->this_idx; | |
3450 | d->this_hdr.sh_flags |= SHF_INFO_LINK; | |
3451 | } | |
252b5132 RH |
3452 | break; |
3453 | ||
3454 | case SHT_STRTAB: | |
3455 | /* We assume that a section named .stab*str is a stabs | |
3456 | string section. We look for a section with the same name | |
3457 | but without the trailing ``str'', and set its sh_link | |
3458 | field to point to this section. */ | |
0112cd26 | 3459 | if (CONST_STRNEQ (sec->name, ".stab") |
252b5132 RH |
3460 | && strcmp (sec->name + strlen (sec->name) - 3, "str") == 0) |
3461 | { | |
3462 | size_t len; | |
3463 | char *alc; | |
3464 | ||
3465 | len = strlen (sec->name); | |
a50b1753 | 3466 | alc = (char *) bfd_malloc (len - 2); |
252b5132 | 3467 | if (alc == NULL) |
b34976b6 | 3468 | return FALSE; |
d4c88bbb | 3469 | memcpy (alc, sec->name, len - 3); |
252b5132 RH |
3470 | alc[len - 3] = '\0'; |
3471 | s = bfd_get_section_by_name (abfd, alc); | |
3472 | free (alc); | |
3473 | if (s != NULL) | |
3474 | { | |
3475 | elf_section_data (s)->this_hdr.sh_link = d->this_idx; | |
3476 | ||
3477 | /* This is a .stab section. */ | |
0594c12d AM |
3478 | if (elf_section_data (s)->this_hdr.sh_entsize == 0) |
3479 | elf_section_data (s)->this_hdr.sh_entsize | |
3480 | = 4 + 2 * bfd_get_arch_size (abfd) / 8; | |
252b5132 RH |
3481 | } |
3482 | } | |
3483 | break; | |
3484 | ||
3485 | case SHT_DYNAMIC: | |
3486 | case SHT_DYNSYM: | |
3487 | case SHT_GNU_verneed: | |
3488 | case SHT_GNU_verdef: | |
3489 | /* sh_link is the section header index of the string table | |
3490 | used for the dynamic entries, or the symbol table, or the | |
3491 | version strings. */ | |
3492 | s = bfd_get_section_by_name (abfd, ".dynstr"); | |
3493 | if (s != NULL) | |
3494 | d->this_hdr.sh_link = elf_section_data (s)->this_idx; | |
3495 | break; | |
3496 | ||
7f1204bb JJ |
3497 | case SHT_GNU_LIBLIST: |
3498 | /* sh_link is the section header index of the prelink library | |
08a40648 AM |
3499 | list used for the dynamic entries, or the symbol table, or |
3500 | the version strings. */ | |
7f1204bb JJ |
3501 | s = bfd_get_section_by_name (abfd, (sec->flags & SEC_ALLOC) |
3502 | ? ".dynstr" : ".gnu.libstr"); | |
3503 | if (s != NULL) | |
3504 | d->this_hdr.sh_link = elf_section_data (s)->this_idx; | |
3505 | break; | |
3506 | ||
252b5132 | 3507 | case SHT_HASH: |
fdc90cb4 | 3508 | case SHT_GNU_HASH: |
252b5132 RH |
3509 | case SHT_GNU_versym: |
3510 | /* sh_link is the section header index of the symbol table | |
3511 | this hash table or version table is for. */ | |
3512 | s = bfd_get_section_by_name (abfd, ".dynsym"); | |
3513 | if (s != NULL) | |
3514 | d->this_hdr.sh_link = elf_section_data (s)->this_idx; | |
3515 | break; | |
dbb410c3 AM |
3516 | |
3517 | case SHT_GROUP: | |
12bd6957 | 3518 | d->this_hdr.sh_link = elf_onesymtab (abfd); |
252b5132 RH |
3519 | } |
3520 | } | |
3521 | ||
2b0f7ef9 | 3522 | for (secn = 1; secn < section_number; ++secn) |
e9702963 L |
3523 | i_shdrp[secn]->sh_name = _bfd_elf_strtab_offset (elf_shstrtab (abfd), |
3524 | i_shdrp[secn]->sh_name); | |
b34976b6 | 3525 | return TRUE; |
252b5132 RH |
3526 | } |
3527 | ||
5372391b | 3528 | static bfd_boolean |
217aa764 | 3529 | sym_is_global (bfd *abfd, asymbol *sym) |
252b5132 RH |
3530 | { |
3531 | /* If the backend has a special mapping, use it. */ | |
9c5bfbb7 | 3532 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
217aa764 AM |
3533 | if (bed->elf_backend_sym_is_global) |
3534 | return (*bed->elf_backend_sym_is_global) (abfd, sym); | |
252b5132 | 3535 | |
e47bf690 | 3536 | return ((sym->flags & (BSF_GLOBAL | BSF_WEAK | BSF_GNU_UNIQUE)) != 0 |
252b5132 RH |
3537 | || bfd_is_und_section (bfd_get_section (sym)) |
3538 | || bfd_is_com_section (bfd_get_section (sym))); | |
3539 | } | |
3540 | ||
5372391b | 3541 | /* Don't output section symbols for sections that are not going to be |
c6d8cab4 | 3542 | output, that are duplicates or there is no BFD section. */ |
5372391b AM |
3543 | |
3544 | static bfd_boolean | |
3545 | ignore_section_sym (bfd *abfd, asymbol *sym) | |
3546 | { | |
c6d8cab4 L |
3547 | elf_symbol_type *type_ptr; |
3548 | ||
3549 | if ((sym->flags & BSF_SECTION_SYM) == 0) | |
3550 | return FALSE; | |
3551 | ||
3552 | type_ptr = elf_symbol_from (abfd, sym); | |
3553 | return ((type_ptr != NULL | |
3554 | && type_ptr->internal_elf_sym.st_shndx != 0 | |
3555 | && bfd_is_abs_section (sym->section)) | |
3556 | || !(sym->section->owner == abfd | |
0f0a5e58 | 3557 | || (sym->section->output_section->owner == abfd |
2633a79c AM |
3558 | && sym->section->output_offset == 0) |
3559 | || bfd_is_abs_section (sym->section))); | |
5372391b AM |
3560 | } |
3561 | ||
2633a79c AM |
3562 | /* Map symbol from it's internal number to the external number, moving |
3563 | all local symbols to be at the head of the list. */ | |
3564 | ||
b34976b6 | 3565 | static bfd_boolean |
12bd6957 | 3566 | elf_map_symbols (bfd *abfd, unsigned int *pnum_locals) |
252b5132 | 3567 | { |
dc810e39 | 3568 | unsigned int symcount = bfd_get_symcount (abfd); |
252b5132 RH |
3569 | asymbol **syms = bfd_get_outsymbols (abfd); |
3570 | asymbol **sect_syms; | |
dc810e39 AM |
3571 | unsigned int num_locals = 0; |
3572 | unsigned int num_globals = 0; | |
3573 | unsigned int num_locals2 = 0; | |
3574 | unsigned int num_globals2 = 0; | |
252b5132 | 3575 | int max_index = 0; |
dc810e39 | 3576 | unsigned int idx; |
252b5132 RH |
3577 | asection *asect; |
3578 | asymbol **new_syms; | |
252b5132 RH |
3579 | |
3580 | #ifdef DEBUG | |
3581 | fprintf (stderr, "elf_map_symbols\n"); | |
3582 | fflush (stderr); | |
3583 | #endif | |
3584 | ||
252b5132 RH |
3585 | for (asect = abfd->sections; asect; asect = asect->next) |
3586 | { | |
3587 | if (max_index < asect->index) | |
3588 | max_index = asect->index; | |
3589 | } | |
3590 | ||
3591 | max_index++; | |
a50b1753 | 3592 | sect_syms = (asymbol **) bfd_zalloc2 (abfd, max_index, sizeof (asymbol *)); |
252b5132 | 3593 | if (sect_syms == NULL) |
b34976b6 | 3594 | return FALSE; |
252b5132 | 3595 | elf_section_syms (abfd) = sect_syms; |
4e89ac30 | 3596 | elf_num_section_syms (abfd) = max_index; |
252b5132 | 3597 | |
079e9a2f AM |
3598 | /* Init sect_syms entries for any section symbols we have already |
3599 | decided to output. */ | |
252b5132 RH |
3600 | for (idx = 0; idx < symcount; idx++) |
3601 | { | |
dc810e39 | 3602 | asymbol *sym = syms[idx]; |
c044fabd | 3603 | |
252b5132 | 3604 | if ((sym->flags & BSF_SECTION_SYM) != 0 |
0f0a5e58 | 3605 | && sym->value == 0 |
2633a79c AM |
3606 | && !ignore_section_sym (abfd, sym) |
3607 | && !bfd_is_abs_section (sym->section)) | |
252b5132 | 3608 | { |
5372391b | 3609 | asection *sec = sym->section; |
252b5132 | 3610 | |
5372391b AM |
3611 | if (sec->owner != abfd) |
3612 | sec = sec->output_section; | |
252b5132 | 3613 | |
5372391b | 3614 | sect_syms[sec->index] = syms[idx]; |
252b5132 RH |
3615 | } |
3616 | } | |
3617 | ||
252b5132 RH |
3618 | /* Classify all of the symbols. */ |
3619 | for (idx = 0; idx < symcount; idx++) | |
3620 | { | |
2633a79c | 3621 | if (sym_is_global (abfd, syms[idx])) |
252b5132 | 3622 | num_globals++; |
2633a79c AM |
3623 | else if (!ignore_section_sym (abfd, syms[idx])) |
3624 | num_locals++; | |
252b5132 | 3625 | } |
079e9a2f | 3626 | |
5372391b | 3627 | /* We will be adding a section symbol for each normal BFD section. Most |
079e9a2f AM |
3628 | sections will already have a section symbol in outsymbols, but |
3629 | eg. SHT_GROUP sections will not, and we need the section symbol mapped | |
3630 | at least in that case. */ | |
252b5132 RH |
3631 | for (asect = abfd->sections; asect; asect = asect->next) |
3632 | { | |
079e9a2f | 3633 | if (sect_syms[asect->index] == NULL) |
252b5132 | 3634 | { |
079e9a2f | 3635 | if (!sym_is_global (abfd, asect->symbol)) |
252b5132 RH |
3636 | num_locals++; |
3637 | else | |
3638 | num_globals++; | |
252b5132 RH |
3639 | } |
3640 | } | |
3641 | ||
3642 | /* Now sort the symbols so the local symbols are first. */ | |
a50b1753 NC |
3643 | new_syms = (asymbol **) bfd_alloc2 (abfd, num_locals + num_globals, |
3644 | sizeof (asymbol *)); | |
dc810e39 | 3645 | |
252b5132 | 3646 | if (new_syms == NULL) |
b34976b6 | 3647 | return FALSE; |
252b5132 RH |
3648 | |
3649 | for (idx = 0; idx < symcount; idx++) | |
3650 | { | |
3651 | asymbol *sym = syms[idx]; | |
dc810e39 | 3652 | unsigned int i; |
252b5132 | 3653 | |
2633a79c AM |
3654 | if (sym_is_global (abfd, sym)) |
3655 | i = num_locals + num_globals2++; | |
3656 | else if (!ignore_section_sym (abfd, sym)) | |
252b5132 RH |
3657 | i = num_locals2++; |
3658 | else | |
2633a79c | 3659 | continue; |
252b5132 RH |
3660 | new_syms[i] = sym; |
3661 | sym->udata.i = i + 1; | |
3662 | } | |
3663 | for (asect = abfd->sections; asect; asect = asect->next) | |
3664 | { | |
079e9a2f | 3665 | if (sect_syms[asect->index] == NULL) |
252b5132 | 3666 | { |
079e9a2f | 3667 | asymbol *sym = asect->symbol; |
dc810e39 | 3668 | unsigned int i; |
252b5132 | 3669 | |
079e9a2f | 3670 | sect_syms[asect->index] = sym; |
252b5132 RH |
3671 | if (!sym_is_global (abfd, sym)) |
3672 | i = num_locals2++; | |
3673 | else | |
3674 | i = num_locals + num_globals2++; | |
3675 | new_syms[i] = sym; | |
3676 | sym->udata.i = i + 1; | |
3677 | } | |
3678 | } | |
3679 | ||
3680 | bfd_set_symtab (abfd, new_syms, num_locals + num_globals); | |
3681 | ||
12bd6957 | 3682 | *pnum_locals = num_locals; |
b34976b6 | 3683 | return TRUE; |
252b5132 RH |
3684 | } |
3685 | ||
3686 | /* Align to the maximum file alignment that could be required for any | |
3687 | ELF data structure. */ | |
3688 | ||
268b6b39 | 3689 | static inline file_ptr |
217aa764 | 3690 | align_file_position (file_ptr off, int align) |
252b5132 RH |
3691 | { |
3692 | return (off + align - 1) & ~(align - 1); | |
3693 | } | |
3694 | ||
3695 | /* Assign a file position to a section, optionally aligning to the | |
3696 | required section alignment. */ | |
3697 | ||
217aa764 AM |
3698 | file_ptr |
3699 | _bfd_elf_assign_file_position_for_section (Elf_Internal_Shdr *i_shdrp, | |
3700 | file_ptr offset, | |
3701 | bfd_boolean align) | |
252b5132 | 3702 | { |
72de5009 AM |
3703 | if (align && i_shdrp->sh_addralign > 1) |
3704 | offset = BFD_ALIGN (offset, i_shdrp->sh_addralign); | |
252b5132 RH |
3705 | i_shdrp->sh_offset = offset; |
3706 | if (i_shdrp->bfd_section != NULL) | |
3707 | i_shdrp->bfd_section->filepos = offset; | |
3708 | if (i_shdrp->sh_type != SHT_NOBITS) | |
3709 | offset += i_shdrp->sh_size; | |
3710 | return offset; | |
3711 | } | |
3712 | ||
3713 | /* Compute the file positions we are going to put the sections at, and | |
3714 | otherwise prepare to begin writing out the ELF file. If LINK_INFO | |
3715 | is not NULL, this is being called by the ELF backend linker. */ | |
3716 | ||
b34976b6 | 3717 | bfd_boolean |
217aa764 AM |
3718 | _bfd_elf_compute_section_file_positions (bfd *abfd, |
3719 | struct bfd_link_info *link_info) | |
252b5132 | 3720 | { |
9c5bfbb7 | 3721 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
d4730f92 | 3722 | struct fake_section_arg fsargs; |
b34976b6 | 3723 | bfd_boolean failed; |
4b6c0f2f | 3724 | struct bfd_strtab_hash *strtab = NULL; |
252b5132 | 3725 | Elf_Internal_Shdr *shstrtab_hdr; |
3516e984 | 3726 | bfd_boolean need_symtab; |
252b5132 RH |
3727 | |
3728 | if (abfd->output_has_begun) | |
b34976b6 | 3729 | return TRUE; |
252b5132 RH |
3730 | |
3731 | /* Do any elf backend specific processing first. */ | |
3732 | if (bed->elf_backend_begin_write_processing) | |
3733 | (*bed->elf_backend_begin_write_processing) (abfd, link_info); | |
3734 | ||
3735 | if (! prep_headers (abfd)) | |
b34976b6 | 3736 | return FALSE; |
252b5132 | 3737 | |
e6c51ed4 | 3738 | /* Post process the headers if necessary. */ |
78245035 | 3739 | (*bed->elf_backend_post_process_headers) (abfd, link_info); |
e6c51ed4 | 3740 | |
d4730f92 BS |
3741 | fsargs.failed = FALSE; |
3742 | fsargs.link_info = link_info; | |
3743 | bfd_map_over_sections (abfd, elf_fake_sections, &fsargs); | |
3744 | if (fsargs.failed) | |
b34976b6 | 3745 | return FALSE; |
252b5132 | 3746 | |
da9f89d4 | 3747 | if (!assign_section_numbers (abfd, link_info)) |
b34976b6 | 3748 | return FALSE; |
252b5132 RH |
3749 | |
3750 | /* The backend linker builds symbol table information itself. */ | |
3516e984 L |
3751 | need_symtab = (link_info == NULL |
3752 | && (bfd_get_symcount (abfd) > 0 | |
3753 | || ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC)) | |
3754 | == HAS_RELOC))); | |
3755 | if (need_symtab) | |
252b5132 RH |
3756 | { |
3757 | /* Non-zero if doing a relocatable link. */ | |
3758 | int relocatable_p = ! (abfd->flags & (EXEC_P | DYNAMIC)); | |
3759 | ||
3760 | if (! swap_out_syms (abfd, &strtab, relocatable_p)) | |
b34976b6 | 3761 | return FALSE; |
252b5132 RH |
3762 | } |
3763 | ||
d4730f92 | 3764 | failed = FALSE; |
1126897b | 3765 | if (link_info == NULL) |
dbb410c3 | 3766 | { |
1126897b | 3767 | bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed); |
dbb410c3 | 3768 | if (failed) |
b34976b6 | 3769 | return FALSE; |
dbb410c3 AM |
3770 | } |
3771 | ||
252b5132 RH |
3772 | shstrtab_hdr = &elf_tdata (abfd)->shstrtab_hdr; |
3773 | /* sh_name was set in prep_headers. */ | |
3774 | shstrtab_hdr->sh_type = SHT_STRTAB; | |
3775 | shstrtab_hdr->sh_flags = 0; | |
3776 | shstrtab_hdr->sh_addr = 0; | |
2b0f7ef9 | 3777 | shstrtab_hdr->sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd)); |
252b5132 RH |
3778 | shstrtab_hdr->sh_entsize = 0; |
3779 | shstrtab_hdr->sh_link = 0; | |
3780 | shstrtab_hdr->sh_info = 0; | |
3781 | /* sh_offset is set in assign_file_positions_except_relocs. */ | |
3782 | shstrtab_hdr->sh_addralign = 1; | |
3783 | ||
c84fca4d | 3784 | if (!assign_file_positions_except_relocs (abfd, link_info)) |
b34976b6 | 3785 | return FALSE; |
252b5132 | 3786 | |
3516e984 | 3787 | if (need_symtab) |
252b5132 RH |
3788 | { |
3789 | file_ptr off; | |
3790 | Elf_Internal_Shdr *hdr; | |
3791 | ||
12bd6957 | 3792 | off = elf_next_file_pos (abfd); |
252b5132 RH |
3793 | |
3794 | hdr = &elf_tdata (abfd)->symtab_hdr; | |
b34976b6 | 3795 | off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE); |
252b5132 | 3796 | |
9ad5cbcf AM |
3797 | hdr = &elf_tdata (abfd)->symtab_shndx_hdr; |
3798 | if (hdr->sh_size != 0) | |
b34976b6 | 3799 | off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE); |
9ad5cbcf | 3800 | |
252b5132 | 3801 | hdr = &elf_tdata (abfd)->strtab_hdr; |
b34976b6 | 3802 | off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE); |
252b5132 | 3803 | |
12bd6957 | 3804 | elf_next_file_pos (abfd) = off; |
252b5132 RH |
3805 | |
3806 | /* Now that we know where the .strtab section goes, write it | |
08a40648 | 3807 | out. */ |
252b5132 RH |
3808 | if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0 |
3809 | || ! _bfd_stringtab_emit (abfd, strtab)) | |
b34976b6 | 3810 | return FALSE; |
252b5132 RH |
3811 | _bfd_stringtab_free (strtab); |
3812 | } | |
3813 | ||
b34976b6 | 3814 | abfd->output_has_begun = TRUE; |
252b5132 | 3815 | |
b34976b6 | 3816 | return TRUE; |
252b5132 RH |
3817 | } |
3818 | ||
8ded5a0f AM |
3819 | /* Make an initial estimate of the size of the program header. If we |
3820 | get the number wrong here, we'll redo section placement. */ | |
3821 | ||
3822 | static bfd_size_type | |
3823 | get_program_header_size (bfd *abfd, struct bfd_link_info *info) | |
3824 | { | |
3825 | size_t segs; | |
3826 | asection *s; | |
2b05f1b7 | 3827 | const struct elf_backend_data *bed; |
8ded5a0f AM |
3828 | |
3829 | /* Assume we will need exactly two PT_LOAD segments: one for text | |
3830 | and one for data. */ | |
3831 | segs = 2; | |
3832 | ||
3833 | s = bfd_get_section_by_name (abfd, ".interp"); | |
3834 | if (s != NULL && (s->flags & SEC_LOAD) != 0) | |
3835 | { | |
3836 | /* If we have a loadable interpreter section, we need a | |
3837 | PT_INTERP segment. In this case, assume we also need a | |
3838 | PT_PHDR segment, although that may not be true for all | |
3839 | targets. */ | |
3840 | segs += 2; | |
3841 | } | |
3842 | ||
3843 | if (bfd_get_section_by_name (abfd, ".dynamic") != NULL) | |
3844 | { | |
3845 | /* We need a PT_DYNAMIC segment. */ | |
3846 | ++segs; | |
f210dcff | 3847 | } |
08a40648 | 3848 | |
ceae84aa | 3849 | if (info != NULL && info->relro) |
f210dcff L |
3850 | { |
3851 | /* We need a PT_GNU_RELRO segment. */ | |
3852 | ++segs; | |
8ded5a0f AM |
3853 | } |
3854 | ||
12bd6957 | 3855 | if (elf_eh_frame_hdr (abfd)) |
8ded5a0f AM |
3856 | { |
3857 | /* We need a PT_GNU_EH_FRAME segment. */ | |
3858 | ++segs; | |
3859 | } | |
3860 | ||
12bd6957 | 3861 | if (elf_stack_flags (abfd)) |
8ded5a0f | 3862 | { |
2b05f1b7 L |
3863 | /* We need a PT_GNU_STACK segment. */ |
3864 | ++segs; | |
3865 | } | |
94b11780 | 3866 | |
2b05f1b7 L |
3867 | for (s = abfd->sections; s != NULL; s = s->next) |
3868 | { | |
8ded5a0f | 3869 | if ((s->flags & SEC_LOAD) != 0 |
0112cd26 | 3870 | && CONST_STRNEQ (s->name, ".note")) |
8ded5a0f AM |
3871 | { |
3872 | /* We need a PT_NOTE segment. */ | |
3873 | ++segs; | |
1c5265b5 JJ |
3874 | /* Try to create just one PT_NOTE segment |
3875 | for all adjacent loadable .note* sections. | |
3876 | gABI requires that within a PT_NOTE segment | |
3877 | (and also inside of each SHT_NOTE section) | |
3878 | each note is padded to a multiple of 4 size, | |
3879 | so we check whether the sections are correctly | |
3880 | aligned. */ | |
3881 | if (s->alignment_power == 2) | |
3882 | while (s->next != NULL | |
3883 | && s->next->alignment_power == 2 | |
3884 | && (s->next->flags & SEC_LOAD) != 0 | |
3885 | && CONST_STRNEQ (s->next->name, ".note")) | |
3886 | s = s->next; | |
8ded5a0f AM |
3887 | } |
3888 | } | |
3889 | ||
3890 | for (s = abfd->sections; s != NULL; s = s->next) | |
3891 | { | |
3892 | if (s->flags & SEC_THREAD_LOCAL) | |
3893 | { | |
3894 | /* We need a PT_TLS segment. */ | |
3895 | ++segs; | |
3896 | break; | |
3897 | } | |
3898 | } | |
3899 | ||
3900 | /* Let the backend count up any program headers it might need. */ | |
2b05f1b7 | 3901 | bed = get_elf_backend_data (abfd); |
8ded5a0f AM |
3902 | if (bed->elf_backend_additional_program_headers) |
3903 | { | |
3904 | int a; | |
3905 | ||
3906 | a = (*bed->elf_backend_additional_program_headers) (abfd, info); | |
3907 | if (a == -1) | |
3908 | abort (); | |
3909 | segs += a; | |
3910 | } | |
3911 | ||
3912 | return segs * bed->s->sizeof_phdr; | |
3913 | } | |
3914 | ||
2ea37f1c NC |
3915 | /* Find the segment that contains the output_section of section. */ |
3916 | ||
3917 | Elf_Internal_Phdr * | |
3918 | _bfd_elf_find_segment_containing_section (bfd * abfd, asection * section) | |
3919 | { | |
3920 | struct elf_segment_map *m; | |
3921 | Elf_Internal_Phdr *p; | |
3922 | ||
12bd6957 | 3923 | for (m = elf_seg_map (abfd), p = elf_tdata (abfd)->phdr; |
2ea37f1c NC |
3924 | m != NULL; |
3925 | m = m->next, p++) | |
3926 | { | |
3927 | int i; | |
3928 | ||
3929 | for (i = m->count - 1; i >= 0; i--) | |
3930 | if (m->sections[i] == section) | |
3931 | return p; | |
3932 | } | |
3933 | ||
3934 | return NULL; | |
3935 | } | |
3936 | ||
252b5132 RH |
3937 | /* Create a mapping from a set of sections to a program segment. */ |
3938 | ||
217aa764 AM |
3939 | static struct elf_segment_map * |
3940 | make_mapping (bfd *abfd, | |
3941 | asection **sections, | |
3942 | unsigned int from, | |
3943 | unsigned int to, | |
3944 | bfd_boolean phdr) | |
252b5132 RH |
3945 | { |
3946 | struct elf_segment_map *m; | |
3947 | unsigned int i; | |
3948 | asection **hdrpp; | |
dc810e39 | 3949 | bfd_size_type amt; |
252b5132 | 3950 | |
dc810e39 AM |
3951 | amt = sizeof (struct elf_segment_map); |
3952 | amt += (to - from - 1) * sizeof (asection *); | |
a50b1753 | 3953 | m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); |
252b5132 RH |
3954 | if (m == NULL) |
3955 | return NULL; | |
3956 | m->next = NULL; | |
3957 | m->p_type = PT_LOAD; | |
3958 | for (i = from, hdrpp = sections + from; i < to; i++, hdrpp++) | |
3959 | m->sections[i - from] = *hdrpp; | |
3960 | m->count = to - from; | |
3961 | ||
3962 | if (from == 0 && phdr) | |
3963 | { | |
3964 | /* Include the headers in the first PT_LOAD segment. */ | |
3965 | m->includes_filehdr = 1; | |
3966 | m->includes_phdrs = 1; | |
3967 | } | |
3968 | ||
3969 | return m; | |
3970 | } | |
3971 | ||
229fcec5 MM |
3972 | /* Create the PT_DYNAMIC segment, which includes DYNSEC. Returns NULL |
3973 | on failure. */ | |
3974 | ||
3975 | struct elf_segment_map * | |
3976 | _bfd_elf_make_dynamic_segment (bfd *abfd, asection *dynsec) | |
3977 | { | |
3978 | struct elf_segment_map *m; | |
3979 | ||
a50b1753 NC |
3980 | m = (struct elf_segment_map *) bfd_zalloc (abfd, |
3981 | sizeof (struct elf_segment_map)); | |
229fcec5 MM |
3982 | if (m == NULL) |
3983 | return NULL; | |
3984 | m->next = NULL; | |
3985 | m->p_type = PT_DYNAMIC; | |
3986 | m->count = 1; | |
3987 | m->sections[0] = dynsec; | |
08a40648 | 3988 | |
229fcec5 MM |
3989 | return m; |
3990 | } | |
3991 | ||
8ded5a0f | 3992 | /* Possibly add or remove segments from the segment map. */ |
252b5132 | 3993 | |
b34976b6 | 3994 | static bfd_boolean |
3dea8fca AM |
3995 | elf_modify_segment_map (bfd *abfd, |
3996 | struct bfd_link_info *info, | |
3997 | bfd_boolean remove_empty_load) | |
252b5132 | 3998 | { |
252e386e | 3999 | struct elf_segment_map **m; |
8ded5a0f | 4000 | const struct elf_backend_data *bed; |
252b5132 | 4001 | |
8ded5a0f AM |
4002 | /* The placement algorithm assumes that non allocated sections are |
4003 | not in PT_LOAD segments. We ensure this here by removing such | |
4004 | sections from the segment map. We also remove excluded | |
252e386e AM |
4005 | sections. Finally, any PT_LOAD segment without sections is |
4006 | removed. */ | |
12bd6957 | 4007 | m = &elf_seg_map (abfd); |
252e386e | 4008 | while (*m) |
8ded5a0f AM |
4009 | { |
4010 | unsigned int i, new_count; | |
252b5132 | 4011 | |
252e386e | 4012 | for (new_count = 0, i = 0; i < (*m)->count; i++) |
8ded5a0f | 4013 | { |
252e386e AM |
4014 | if (((*m)->sections[i]->flags & SEC_EXCLUDE) == 0 |
4015 | && (((*m)->sections[i]->flags & SEC_ALLOC) != 0 | |
4016 | || (*m)->p_type != PT_LOAD)) | |
8ded5a0f | 4017 | { |
252e386e AM |
4018 | (*m)->sections[new_count] = (*m)->sections[i]; |
4019 | new_count++; | |
8ded5a0f AM |
4020 | } |
4021 | } | |
252e386e | 4022 | (*m)->count = new_count; |
252b5132 | 4023 | |
3dea8fca | 4024 | if (remove_empty_load && (*m)->p_type == PT_LOAD && (*m)->count == 0) |
252e386e AM |
4025 | *m = (*m)->next; |
4026 | else | |
4027 | m = &(*m)->next; | |
8ded5a0f | 4028 | } |
252b5132 | 4029 | |
8ded5a0f AM |
4030 | bed = get_elf_backend_data (abfd); |
4031 | if (bed->elf_backend_modify_segment_map != NULL) | |
252b5132 | 4032 | { |
252e386e | 4033 | if (!(*bed->elf_backend_modify_segment_map) (abfd, info)) |
8ded5a0f | 4034 | return FALSE; |
252b5132 | 4035 | } |
252b5132 | 4036 | |
8ded5a0f AM |
4037 | return TRUE; |
4038 | } | |
252b5132 | 4039 | |
8ded5a0f | 4040 | /* Set up a mapping from BFD sections to program segments. */ |
252b5132 | 4041 | |
8ded5a0f AM |
4042 | bfd_boolean |
4043 | _bfd_elf_map_sections_to_segments (bfd *abfd, struct bfd_link_info *info) | |
4044 | { | |
4045 | unsigned int count; | |
4046 | struct elf_segment_map *m; | |
4047 | asection **sections = NULL; | |
4048 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
3dea8fca | 4049 | bfd_boolean no_user_phdrs; |
252b5132 | 4050 | |
12bd6957 | 4051 | no_user_phdrs = elf_seg_map (abfd) == NULL; |
d324f6d6 RM |
4052 | |
4053 | if (info != NULL) | |
4054 | info->user_phdrs = !no_user_phdrs; | |
4055 | ||
3dea8fca | 4056 | if (no_user_phdrs && bfd_count_sections (abfd) != 0) |
252b5132 | 4057 | { |
8ded5a0f AM |
4058 | asection *s; |
4059 | unsigned int i; | |
4060 | struct elf_segment_map *mfirst; | |
4061 | struct elf_segment_map **pm; | |
4062 | asection *last_hdr; | |
4063 | bfd_vma last_size; | |
4064 | unsigned int phdr_index; | |
4065 | bfd_vma maxpagesize; | |
4066 | asection **hdrpp; | |
4067 | bfd_boolean phdr_in_segment = TRUE; | |
4068 | bfd_boolean writable; | |
4069 | int tls_count = 0; | |
4070 | asection *first_tls = NULL; | |
4071 | asection *dynsec, *eh_frame_hdr; | |
4072 | bfd_size_type amt; | |
8d06853e | 4073 | bfd_vma addr_mask, wrap_to = 0; |
252b5132 | 4074 | |
8ded5a0f | 4075 | /* Select the allocated sections, and sort them. */ |
252b5132 | 4076 | |
a50b1753 NC |
4077 | sections = (asection **) bfd_malloc2 (bfd_count_sections (abfd), |
4078 | sizeof (asection *)); | |
8ded5a0f | 4079 | if (sections == NULL) |
252b5132 | 4080 | goto error_return; |
252b5132 | 4081 | |
8d06853e AM |
4082 | /* Calculate top address, avoiding undefined behaviour of shift |
4083 | left operator when shift count is equal to size of type | |
4084 | being shifted. */ | |
4085 | addr_mask = ((bfd_vma) 1 << (bfd_arch_bits_per_address (abfd) - 1)) - 1; | |
4086 | addr_mask = (addr_mask << 1) + 1; | |
4087 | ||
8ded5a0f AM |
4088 | i = 0; |
4089 | for (s = abfd->sections; s != NULL; s = s->next) | |
4090 | { | |
4091 | if ((s->flags & SEC_ALLOC) != 0) | |
4092 | { | |
4093 | sections[i] = s; | |
4094 | ++i; | |
8d06853e AM |
4095 | /* A wrapping section potentially clashes with header. */ |
4096 | if (((s->lma + s->size) & addr_mask) < (s->lma & addr_mask)) | |
4097 | wrap_to = (s->lma + s->size) & addr_mask; | |
8ded5a0f AM |
4098 | } |
4099 | } | |
4100 | BFD_ASSERT (i <= bfd_count_sections (abfd)); | |
4101 | count = i; | |
252b5132 | 4102 | |
8ded5a0f | 4103 | qsort (sections, (size_t) count, sizeof (asection *), elf_sort_sections); |
252b5132 | 4104 | |
8ded5a0f | 4105 | /* Build the mapping. */ |
252b5132 | 4106 | |
8ded5a0f AM |
4107 | mfirst = NULL; |
4108 | pm = &mfirst; | |
252b5132 | 4109 | |
8ded5a0f AM |
4110 | /* If we have a .interp section, then create a PT_PHDR segment for |
4111 | the program headers and a PT_INTERP segment for the .interp | |
4112 | section. */ | |
4113 | s = bfd_get_section_by_name (abfd, ".interp"); | |
4114 | if (s != NULL && (s->flags & SEC_LOAD) != 0) | |
4115 | { | |
4116 | amt = sizeof (struct elf_segment_map); | |
a50b1753 | 4117 | m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); |
8ded5a0f AM |
4118 | if (m == NULL) |
4119 | goto error_return; | |
4120 | m->next = NULL; | |
4121 | m->p_type = PT_PHDR; | |
4122 | /* FIXME: UnixWare and Solaris set PF_X, Irix 5 does not. */ | |
4123 | m->p_flags = PF_R | PF_X; | |
4124 | m->p_flags_valid = 1; | |
4125 | m->includes_phdrs = 1; | |
252b5132 | 4126 | |
8ded5a0f AM |
4127 | *pm = m; |
4128 | pm = &m->next; | |
252b5132 | 4129 | |
8ded5a0f | 4130 | amt = sizeof (struct elf_segment_map); |
a50b1753 | 4131 | m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); |
8ded5a0f AM |
4132 | if (m == NULL) |
4133 | goto error_return; | |
4134 | m->next = NULL; | |
4135 | m->p_type = PT_INTERP; | |
4136 | m->count = 1; | |
4137 | m->sections[0] = s; | |
4138 | ||
4139 | *pm = m; | |
4140 | pm = &m->next; | |
252b5132 | 4141 | } |
8ded5a0f AM |
4142 | |
4143 | /* Look through the sections. We put sections in the same program | |
4144 | segment when the start of the second section can be placed within | |
4145 | a few bytes of the end of the first section. */ | |
4146 | last_hdr = NULL; | |
4147 | last_size = 0; | |
4148 | phdr_index = 0; | |
4149 | maxpagesize = bed->maxpagesize; | |
063bb025 NC |
4150 | /* PR 17512: file: c8455299. |
4151 | Avoid divide-by-zero errors later on. | |
4152 | FIXME: Should we abort if the maxpagesize is zero ? */ | |
4153 | if (maxpagesize == 0) | |
4154 | maxpagesize = 1; | |
8ded5a0f AM |
4155 | writable = FALSE; |
4156 | dynsec = bfd_get_section_by_name (abfd, ".dynamic"); | |
4157 | if (dynsec != NULL | |
4158 | && (dynsec->flags & SEC_LOAD) == 0) | |
4159 | dynsec = NULL; | |
4160 | ||
4161 | /* Deal with -Ttext or something similar such that the first section | |
4162 | is not adjacent to the program headers. This is an | |
4163 | approximation, since at this point we don't know exactly how many | |
4164 | program headers we will need. */ | |
4165 | if (count > 0) | |
252b5132 | 4166 | { |
12bd6957 | 4167 | bfd_size_type phdr_size = elf_program_header_size (abfd); |
8ded5a0f | 4168 | |
62d7a5f6 | 4169 | if (phdr_size == (bfd_size_type) -1) |
8ded5a0f | 4170 | phdr_size = get_program_header_size (abfd, info); |
d2bcb0d1 | 4171 | phdr_size += bed->s->sizeof_ehdr; |
8ded5a0f | 4172 | if ((abfd->flags & D_PAGED) == 0 |
8d06853e AM |
4173 | || (sections[0]->lma & addr_mask) < phdr_size |
4174 | || ((sections[0]->lma & addr_mask) % maxpagesize | |
4175 | < phdr_size % maxpagesize) | |
4176 | || (sections[0]->lma & addr_mask & -maxpagesize) < wrap_to) | |
8ded5a0f | 4177 | phdr_in_segment = FALSE; |
252b5132 RH |
4178 | } |
4179 | ||
8ded5a0f | 4180 | for (i = 0, hdrpp = sections; i < count; i++, hdrpp++) |
252b5132 | 4181 | { |
8ded5a0f AM |
4182 | asection *hdr; |
4183 | bfd_boolean new_segment; | |
4184 | ||
4185 | hdr = *hdrpp; | |
4186 | ||
4187 | /* See if this section and the last one will fit in the same | |
4188 | segment. */ | |
4189 | ||
4190 | if (last_hdr == NULL) | |
4191 | { | |
4192 | /* If we don't have a segment yet, then we don't need a new | |
4193 | one (we build the last one after this loop). */ | |
4194 | new_segment = FALSE; | |
4195 | } | |
4196 | else if (last_hdr->lma - last_hdr->vma != hdr->lma - hdr->vma) | |
4197 | { | |
4198 | /* If this section has a different relation between the | |
4199 | virtual address and the load address, then we need a new | |
4200 | segment. */ | |
4201 | new_segment = TRUE; | |
4202 | } | |
b5599592 AM |
4203 | else if (hdr->lma < last_hdr->lma + last_size |
4204 | || last_hdr->lma + last_size < last_hdr->lma) | |
4205 | { | |
4206 | /* If this section has a load address that makes it overlap | |
4207 | the previous section, then we need a new segment. */ | |
4208 | new_segment = TRUE; | |
4209 | } | |
39948a60 NC |
4210 | /* In the next test we have to be careful when last_hdr->lma is close |
4211 | to the end of the address space. If the aligned address wraps | |
4212 | around to the start of the address space, then there are no more | |
4213 | pages left in memory and it is OK to assume that the current | |
4214 | section can be included in the current segment. */ | |
4215 | else if ((BFD_ALIGN (last_hdr->lma + last_size, maxpagesize) + maxpagesize | |
4216 | > last_hdr->lma) | |
4217 | && (BFD_ALIGN (last_hdr->lma + last_size, maxpagesize) + maxpagesize | |
4ff73856 | 4218 | <= hdr->lma)) |
8ded5a0f AM |
4219 | { |
4220 | /* If putting this section in this segment would force us to | |
4221 | skip a page in the segment, then we need a new segment. */ | |
4222 | new_segment = TRUE; | |
4223 | } | |
4224 | else if ((last_hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0 | |
4225 | && (hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) != 0) | |
4226 | { | |
4227 | /* We don't want to put a loadable section after a | |
4228 | nonloadable section in the same segment. | |
4229 | Consider .tbss sections as loadable for this purpose. */ | |
4230 | new_segment = TRUE; | |
4231 | } | |
4232 | else if ((abfd->flags & D_PAGED) == 0) | |
4233 | { | |
4234 | /* If the file is not demand paged, which means that we | |
4235 | don't require the sections to be correctly aligned in the | |
4236 | file, then there is no other reason for a new segment. */ | |
4237 | new_segment = FALSE; | |
4238 | } | |
4239 | else if (! writable | |
4240 | && (hdr->flags & SEC_READONLY) == 0 | |
8d06853e AM |
4241 | && (((last_hdr->lma + last_size - 1) & -maxpagesize) |
4242 | != (hdr->lma & -maxpagesize))) | |
8ded5a0f AM |
4243 | { |
4244 | /* We don't want to put a writable section in a read only | |
4245 | segment, unless they are on the same page in memory | |
4246 | anyhow. We already know that the last section does not | |
4247 | bring us past the current section on the page, so the | |
4248 | only case in which the new section is not on the same | |
4249 | page as the previous section is when the previous section | |
4250 | ends precisely on a page boundary. */ | |
4251 | new_segment = TRUE; | |
4252 | } | |
4253 | else | |
4254 | { | |
4255 | /* Otherwise, we can use the same segment. */ | |
4256 | new_segment = FALSE; | |
4257 | } | |
4258 | ||
2889e75b | 4259 | /* Allow interested parties a chance to override our decision. */ |
ceae84aa AM |
4260 | if (last_hdr != NULL |
4261 | && info != NULL | |
4262 | && info->callbacks->override_segment_assignment != NULL) | |
4263 | new_segment | |
4264 | = info->callbacks->override_segment_assignment (info, abfd, hdr, | |
4265 | last_hdr, | |
4266 | new_segment); | |
2889e75b | 4267 | |
8ded5a0f AM |
4268 | if (! new_segment) |
4269 | { | |
4270 | if ((hdr->flags & SEC_READONLY) == 0) | |
4271 | writable = TRUE; | |
4272 | last_hdr = hdr; | |
4273 | /* .tbss sections effectively have zero size. */ | |
4274 | if ((hdr->flags & (SEC_THREAD_LOCAL | SEC_LOAD)) | |
4275 | != SEC_THREAD_LOCAL) | |
4276 | last_size = hdr->size; | |
4277 | else | |
4278 | last_size = 0; | |
4279 | continue; | |
4280 | } | |
4281 | ||
4282 | /* We need a new program segment. We must create a new program | |
4283 | header holding all the sections from phdr_index until hdr. */ | |
4284 | ||
4285 | m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment); | |
4286 | if (m == NULL) | |
4287 | goto error_return; | |
4288 | ||
4289 | *pm = m; | |
4290 | pm = &m->next; | |
4291 | ||
252b5132 | 4292 | if ((hdr->flags & SEC_READONLY) == 0) |
b34976b6 | 4293 | writable = TRUE; |
8ded5a0f AM |
4294 | else |
4295 | writable = FALSE; | |
4296 | ||
baaff79e JJ |
4297 | last_hdr = hdr; |
4298 | /* .tbss sections effectively have zero size. */ | |
e5caec89 | 4299 | if ((hdr->flags & (SEC_THREAD_LOCAL | SEC_LOAD)) != SEC_THREAD_LOCAL) |
eea6121a | 4300 | last_size = hdr->size; |
baaff79e JJ |
4301 | else |
4302 | last_size = 0; | |
8ded5a0f AM |
4303 | phdr_index = i; |
4304 | phdr_in_segment = FALSE; | |
252b5132 RH |
4305 | } |
4306 | ||
86b2281f AM |
4307 | /* Create a final PT_LOAD program segment, but not if it's just |
4308 | for .tbss. */ | |
4309 | if (last_hdr != NULL | |
4310 | && (i - phdr_index != 1 | |
4311 | || ((last_hdr->flags & (SEC_THREAD_LOCAL | SEC_LOAD)) | |
4312 | != SEC_THREAD_LOCAL))) | |
8ded5a0f AM |
4313 | { |
4314 | m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment); | |
4315 | if (m == NULL) | |
4316 | goto error_return; | |
252b5132 | 4317 | |
8ded5a0f AM |
4318 | *pm = m; |
4319 | pm = &m->next; | |
4320 | } | |
252b5132 | 4321 | |
8ded5a0f AM |
4322 | /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */ |
4323 | if (dynsec != NULL) | |
4324 | { | |
4325 | m = _bfd_elf_make_dynamic_segment (abfd, dynsec); | |
4326 | if (m == NULL) | |
4327 | goto error_return; | |
4328 | *pm = m; | |
4329 | pm = &m->next; | |
4330 | } | |
252b5132 | 4331 | |
1c5265b5 JJ |
4332 | /* For each batch of consecutive loadable .note sections, |
4333 | add a PT_NOTE segment. We don't use bfd_get_section_by_name, | |
4334 | because if we link together nonloadable .note sections and | |
4335 | loadable .note sections, we will generate two .note sections | |
4336 | in the output file. FIXME: Using names for section types is | |
4337 | bogus anyhow. */ | |
8ded5a0f AM |
4338 | for (s = abfd->sections; s != NULL; s = s->next) |
4339 | { | |
4340 | if ((s->flags & SEC_LOAD) != 0 | |
0112cd26 | 4341 | && CONST_STRNEQ (s->name, ".note")) |
8ded5a0f | 4342 | { |
1c5265b5 | 4343 | asection *s2; |
91d6fa6a NC |
4344 | |
4345 | count = 1; | |
8ded5a0f | 4346 | amt = sizeof (struct elf_segment_map); |
1c5265b5 JJ |
4347 | if (s->alignment_power == 2) |
4348 | for (s2 = s; s2->next != NULL; s2 = s2->next) | |
55b581a6 JJ |
4349 | { |
4350 | if (s2->next->alignment_power == 2 | |
4351 | && (s2->next->flags & SEC_LOAD) != 0 | |
4352 | && CONST_STRNEQ (s2->next->name, ".note") | |
8d06853e AM |
4353 | && align_power (s2->lma + s2->size, 2) |
4354 | == s2->next->lma) | |
55b581a6 JJ |
4355 | count++; |
4356 | else | |
4357 | break; | |
4358 | } | |
1c5265b5 | 4359 | amt += (count - 1) * sizeof (asection *); |
a50b1753 | 4360 | m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); |
8ded5a0f AM |
4361 | if (m == NULL) |
4362 | goto error_return; | |
4363 | m->next = NULL; | |
4364 | m->p_type = PT_NOTE; | |
1c5265b5 JJ |
4365 | m->count = count; |
4366 | while (count > 1) | |
4367 | { | |
4368 | m->sections[m->count - count--] = s; | |
4369 | BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0); | |
4370 | s = s->next; | |
4371 | } | |
4372 | m->sections[m->count - 1] = s; | |
4373 | BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0); | |
8ded5a0f AM |
4374 | *pm = m; |
4375 | pm = &m->next; | |
4376 | } | |
4377 | if (s->flags & SEC_THREAD_LOCAL) | |
4378 | { | |
4379 | if (! tls_count) | |
4380 | first_tls = s; | |
4381 | tls_count++; | |
4382 | } | |
4383 | } | |
252b5132 | 4384 | |
8ded5a0f AM |
4385 | /* If there are any SHF_TLS output sections, add PT_TLS segment. */ |
4386 | if (tls_count > 0) | |
4387 | { | |
8ded5a0f AM |
4388 | amt = sizeof (struct elf_segment_map); |
4389 | amt += (tls_count - 1) * sizeof (asection *); | |
a50b1753 | 4390 | m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); |
8ded5a0f AM |
4391 | if (m == NULL) |
4392 | goto error_return; | |
4393 | m->next = NULL; | |
4394 | m->p_type = PT_TLS; | |
4395 | m->count = tls_count; | |
4396 | /* Mandated PF_R. */ | |
4397 | m->p_flags = PF_R; | |
4398 | m->p_flags_valid = 1; | |
d923cae0 | 4399 | s = first_tls; |
91d6fa6a | 4400 | for (i = 0; i < (unsigned int) tls_count; ++i) |
8ded5a0f | 4401 | { |
d923cae0 L |
4402 | if ((s->flags & SEC_THREAD_LOCAL) == 0) |
4403 | { | |
4404 | _bfd_error_handler | |
4405 | (_("%B: TLS sections are not adjacent:"), abfd); | |
4406 | s = first_tls; | |
4407 | i = 0; | |
4408 | while (i < (unsigned int) tls_count) | |
4409 | { | |
4410 | if ((s->flags & SEC_THREAD_LOCAL) != 0) | |
4411 | { | |
4412 | _bfd_error_handler (_(" TLS: %A"), s); | |
4413 | i++; | |
4414 | } | |
4415 | else | |
4416 | _bfd_error_handler (_(" non-TLS: %A"), s); | |
4417 | s = s->next; | |
4418 | } | |
4419 | bfd_set_error (bfd_error_bad_value); | |
4420 | goto error_return; | |
4421 | } | |
4422 | m->sections[i] = s; | |
4423 | s = s->next; | |
8ded5a0f | 4424 | } |
252b5132 | 4425 | |
8ded5a0f AM |
4426 | *pm = m; |
4427 | pm = &m->next; | |
4428 | } | |
252b5132 | 4429 | |
8ded5a0f AM |
4430 | /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME |
4431 | segment. */ | |
12bd6957 | 4432 | eh_frame_hdr = elf_eh_frame_hdr (abfd); |
8ded5a0f AM |
4433 | if (eh_frame_hdr != NULL |
4434 | && (eh_frame_hdr->output_section->flags & SEC_LOAD) != 0) | |
252b5132 | 4435 | { |
dc810e39 | 4436 | amt = sizeof (struct elf_segment_map); |
a50b1753 | 4437 | m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); |
252b5132 RH |
4438 | if (m == NULL) |
4439 | goto error_return; | |
4440 | m->next = NULL; | |
8ded5a0f | 4441 | m->p_type = PT_GNU_EH_FRAME; |
252b5132 | 4442 | m->count = 1; |
8ded5a0f | 4443 | m->sections[0] = eh_frame_hdr->output_section; |
252b5132 RH |
4444 | |
4445 | *pm = m; | |
4446 | pm = &m->next; | |
4447 | } | |
13ae64f3 | 4448 | |
12bd6957 | 4449 | if (elf_stack_flags (abfd)) |
13ae64f3 | 4450 | { |
8ded5a0f | 4451 | amt = sizeof (struct elf_segment_map); |
a50b1753 | 4452 | m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); |
8ded5a0f AM |
4453 | if (m == NULL) |
4454 | goto error_return; | |
4455 | m->next = NULL; | |
2b05f1b7 | 4456 | m->p_type = PT_GNU_STACK; |
12bd6957 | 4457 | m->p_flags = elf_stack_flags (abfd); |
04c3a755 | 4458 | m->p_align = bed->stack_align; |
8ded5a0f | 4459 | m->p_flags_valid = 1; |
04c3a755 NS |
4460 | m->p_align_valid = m->p_align != 0; |
4461 | if (info->stacksize > 0) | |
4462 | { | |
4463 | m->p_size = info->stacksize; | |
4464 | m->p_size_valid = 1; | |
4465 | } | |
252b5132 | 4466 | |
8ded5a0f AM |
4467 | *pm = m; |
4468 | pm = &m->next; | |
4469 | } | |
65765700 | 4470 | |
ceae84aa | 4471 | if (info != NULL && info->relro) |
8ded5a0f | 4472 | { |
f210dcff L |
4473 | for (m = mfirst; m != NULL; m = m->next) |
4474 | { | |
3832a4d8 AM |
4475 | if (m->p_type == PT_LOAD |
4476 | && m->count != 0 | |
4477 | && m->sections[0]->vma >= info->relro_start | |
4478 | && m->sections[0]->vma < info->relro_end) | |
f210dcff | 4479 | { |
3832a4d8 AM |
4480 | i = m->count; |
4481 | while (--i != (unsigned) -1) | |
4482 | if ((m->sections[i]->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) | |
4483 | == (SEC_LOAD | SEC_HAS_CONTENTS)) | |
4484 | break; | |
4485 | ||
43a8475c | 4486 | if (i != (unsigned) -1) |
f210dcff L |
4487 | break; |
4488 | } | |
be01b344 | 4489 | } |
f210dcff L |
4490 | |
4491 | /* Make a PT_GNU_RELRO segment only when it isn't empty. */ | |
4492 | if (m != NULL) | |
4493 | { | |
4494 | amt = sizeof (struct elf_segment_map); | |
a50b1753 | 4495 | m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); |
f210dcff L |
4496 | if (m == NULL) |
4497 | goto error_return; | |
4498 | m->next = NULL; | |
4499 | m->p_type = PT_GNU_RELRO; | |
4500 | m->p_flags = PF_R; | |
4501 | m->p_flags_valid = 1; | |
4502 | ||
4503 | *pm = m; | |
4504 | pm = &m->next; | |
4505 | } | |
8ded5a0f | 4506 | } |
9ee5e499 | 4507 | |
8ded5a0f | 4508 | free (sections); |
12bd6957 | 4509 | elf_seg_map (abfd) = mfirst; |
9ee5e499 JJ |
4510 | } |
4511 | ||
3dea8fca | 4512 | if (!elf_modify_segment_map (abfd, info, no_user_phdrs)) |
8ded5a0f | 4513 | return FALSE; |
8c37241b | 4514 | |
12bd6957 | 4515 | for (count = 0, m = elf_seg_map (abfd); m != NULL; m = m->next) |
8ded5a0f | 4516 | ++count; |
12bd6957 | 4517 | elf_program_header_size (abfd) = count * bed->s->sizeof_phdr; |
252b5132 | 4518 | |
b34976b6 | 4519 | return TRUE; |
252b5132 RH |
4520 | |
4521 | error_return: | |
4522 | if (sections != NULL) | |
4523 | free (sections); | |
b34976b6 | 4524 | return FALSE; |
252b5132 RH |
4525 | } |
4526 | ||
4527 | /* Sort sections by address. */ | |
4528 | ||
4529 | static int | |
217aa764 | 4530 | elf_sort_sections (const void *arg1, const void *arg2) |
252b5132 RH |
4531 | { |
4532 | const asection *sec1 = *(const asection **) arg1; | |
4533 | const asection *sec2 = *(const asection **) arg2; | |
eecdbe52 | 4534 | bfd_size_type size1, size2; |
252b5132 RH |
4535 | |
4536 | /* Sort by LMA first, since this is the address used to | |
4537 | place the section into a segment. */ | |
4538 | if (sec1->lma < sec2->lma) | |
4539 | return -1; | |
4540 | else if (sec1->lma > sec2->lma) | |
4541 | return 1; | |
4542 | ||
4543 | /* Then sort by VMA. Normally the LMA and the VMA will be | |
4544 | the same, and this will do nothing. */ | |
4545 | if (sec1->vma < sec2->vma) | |
4546 | return -1; | |
4547 | else if (sec1->vma > sec2->vma) | |
4548 | return 1; | |
4549 | ||
4550 | /* Put !SEC_LOAD sections after SEC_LOAD ones. */ | |
4551 | ||
07c6e936 | 4552 | #define TOEND(x) (((x)->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0) |
252b5132 RH |
4553 | |
4554 | if (TOEND (sec1)) | |
4555 | { | |
4556 | if (TOEND (sec2)) | |
00a7cdc5 NC |
4557 | { |
4558 | /* If the indicies are the same, do not return 0 | |
4559 | here, but continue to try the next comparison. */ | |
4560 | if (sec1->target_index - sec2->target_index != 0) | |
4561 | return sec1->target_index - sec2->target_index; | |
4562 | } | |
252b5132 RH |
4563 | else |
4564 | return 1; | |
4565 | } | |
00a7cdc5 | 4566 | else if (TOEND (sec2)) |
252b5132 RH |
4567 | return -1; |
4568 | ||
4569 | #undef TOEND | |
4570 | ||
00a7cdc5 NC |
4571 | /* Sort by size, to put zero sized sections |
4572 | before others at the same address. */ | |
252b5132 | 4573 | |
eea6121a AM |
4574 | size1 = (sec1->flags & SEC_LOAD) ? sec1->size : 0; |
4575 | size2 = (sec2->flags & SEC_LOAD) ? sec2->size : 0; | |
eecdbe52 JJ |
4576 | |
4577 | if (size1 < size2) | |
252b5132 | 4578 | return -1; |
eecdbe52 | 4579 | if (size1 > size2) |
252b5132 RH |
4580 | return 1; |
4581 | ||
4582 | return sec1->target_index - sec2->target_index; | |
4583 | } | |
4584 | ||
340b6d91 AC |
4585 | /* Ian Lance Taylor writes: |
4586 | ||
4587 | We shouldn't be using % with a negative signed number. That's just | |
4588 | not good. We have to make sure either that the number is not | |
4589 | negative, or that the number has an unsigned type. When the types | |
4590 | are all the same size they wind up as unsigned. When file_ptr is a | |
4591 | larger signed type, the arithmetic winds up as signed long long, | |
4592 | which is wrong. | |
4593 | ||
4594 | What we're trying to say here is something like ``increase OFF by | |
4595 | the least amount that will cause it to be equal to the VMA modulo | |
4596 | the page size.'' */ | |
4597 | /* In other words, something like: | |
4598 | ||
4599 | vma_offset = m->sections[0]->vma % bed->maxpagesize; | |
4600 | off_offset = off % bed->maxpagesize; | |
4601 | if (vma_offset < off_offset) | |
4602 | adjustment = vma_offset + bed->maxpagesize - off_offset; | |
4603 | else | |
4604 | adjustment = vma_offset - off_offset; | |
08a40648 | 4605 | |
340b6d91 AC |
4606 | which can can be collapsed into the expression below. */ |
4607 | ||
4608 | static file_ptr | |
4609 | vma_page_aligned_bias (bfd_vma vma, ufile_ptr off, bfd_vma maxpagesize) | |
4610 | { | |
dc9155b2 NC |
4611 | /* PR binutils/16199: Handle an alignment of zero. */ |
4612 | if (maxpagesize == 0) | |
4613 | maxpagesize = 1; | |
340b6d91 AC |
4614 | return ((vma - off) % maxpagesize); |
4615 | } | |
4616 | ||
6d33f217 L |
4617 | static void |
4618 | print_segment_map (const struct elf_segment_map *m) | |
4619 | { | |
4620 | unsigned int j; | |
4621 | const char *pt = get_segment_type (m->p_type); | |
4622 | char buf[32]; | |
4623 | ||
4624 | if (pt == NULL) | |
4625 | { | |
4626 | if (m->p_type >= PT_LOPROC && m->p_type <= PT_HIPROC) | |
4627 | sprintf (buf, "LOPROC+%7.7x", | |
4628 | (unsigned int) (m->p_type - PT_LOPROC)); | |
4629 | else if (m->p_type >= PT_LOOS && m->p_type <= PT_HIOS) | |
4630 | sprintf (buf, "LOOS+%7.7x", | |
4631 | (unsigned int) (m->p_type - PT_LOOS)); | |
4632 | else | |
4633 | snprintf (buf, sizeof (buf), "%8.8x", | |
4634 | (unsigned int) m->p_type); | |
4635 | pt = buf; | |
4636 | } | |
4a97a0e5 | 4637 | fflush (stdout); |
6d33f217 L |
4638 | fprintf (stderr, "%s:", pt); |
4639 | for (j = 0; j < m->count; j++) | |
4640 | fprintf (stderr, " %s", m->sections [j]->name); | |
4641 | putc ('\n',stderr); | |
4a97a0e5 | 4642 | fflush (stderr); |
6d33f217 L |
4643 | } |
4644 | ||
32812159 AM |
4645 | static bfd_boolean |
4646 | write_zeros (bfd *abfd, file_ptr pos, bfd_size_type len) | |
4647 | { | |
4648 | void *buf; | |
4649 | bfd_boolean ret; | |
4650 | ||
4651 | if (bfd_seek (abfd, pos, SEEK_SET) != 0) | |
4652 | return FALSE; | |
4653 | buf = bfd_zmalloc (len); | |
4654 | if (buf == NULL) | |
4655 | return FALSE; | |
4656 | ret = bfd_bwrite (buf, len, abfd) == len; | |
4657 | free (buf); | |
4658 | return ret; | |
4659 | } | |
4660 | ||
252b5132 RH |
4661 | /* Assign file positions to the sections based on the mapping from |
4662 | sections to segments. This function also sets up some fields in | |
f3520d2f | 4663 | the file header. */ |
252b5132 | 4664 | |
b34976b6 | 4665 | static bfd_boolean |
f3520d2f AM |
4666 | assign_file_positions_for_load_sections (bfd *abfd, |
4667 | struct bfd_link_info *link_info) | |
252b5132 RH |
4668 | { |
4669 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
252b5132 | 4670 | struct elf_segment_map *m; |
252b5132 | 4671 | Elf_Internal_Phdr *phdrs; |
252b5132 | 4672 | Elf_Internal_Phdr *p; |
02bf8d82 | 4673 | file_ptr off; |
3f570048 | 4674 | bfd_size_type maxpagesize; |
f3520d2f | 4675 | unsigned int alloc; |
0920dee7 | 4676 | unsigned int i, j; |
2b0bc088 | 4677 | bfd_vma header_pad = 0; |
252b5132 | 4678 | |
e36284ab | 4679 | if (link_info == NULL |
ceae84aa | 4680 | && !_bfd_elf_map_sections_to_segments (abfd, link_info)) |
8ded5a0f | 4681 | return FALSE; |
252b5132 | 4682 | |
8ded5a0f | 4683 | alloc = 0; |
12bd6957 | 4684 | for (m = elf_seg_map (abfd); m != NULL; m = m->next) |
2b0bc088 NC |
4685 | { |
4686 | ++alloc; | |
4687 | if (m->header_size) | |
4688 | header_pad = m->header_size; | |
4689 | } | |
252b5132 | 4690 | |
82f2dbf7 NC |
4691 | if (alloc) |
4692 | { | |
4693 | elf_elfheader (abfd)->e_phoff = bed->s->sizeof_ehdr; | |
4694 | elf_elfheader (abfd)->e_phentsize = bed->s->sizeof_phdr; | |
4695 | } | |
4696 | else | |
4697 | { | |
4698 | /* PR binutils/12467. */ | |
4699 | elf_elfheader (abfd)->e_phoff = 0; | |
4700 | elf_elfheader (abfd)->e_phentsize = 0; | |
4701 | } | |
d324f6d6 | 4702 | |
8ded5a0f | 4703 | elf_elfheader (abfd)->e_phnum = alloc; |
252b5132 | 4704 | |
12bd6957 AM |
4705 | if (elf_program_header_size (abfd) == (bfd_size_type) -1) |
4706 | elf_program_header_size (abfd) = alloc * bed->s->sizeof_phdr; | |
8ded5a0f | 4707 | else |
12bd6957 | 4708 | BFD_ASSERT (elf_program_header_size (abfd) |
59e0647f | 4709 | >= alloc * bed->s->sizeof_phdr); |
252b5132 RH |
4710 | |
4711 | if (alloc == 0) | |
f3520d2f | 4712 | { |
12bd6957 | 4713 | elf_next_file_pos (abfd) = bed->s->sizeof_ehdr; |
8ded5a0f | 4714 | return TRUE; |
f3520d2f | 4715 | } |
252b5132 | 4716 | |
12bd6957 | 4717 | /* We're writing the size in elf_program_header_size (abfd), |
57268894 HPN |
4718 | see assign_file_positions_except_relocs, so make sure we have |
4719 | that amount allocated, with trailing space cleared. | |
12bd6957 AM |
4720 | The variable alloc contains the computed need, while |
4721 | elf_program_header_size (abfd) contains the size used for the | |
57268894 HPN |
4722 | layout. |
4723 | See ld/emultempl/elf-generic.em:gld${EMULATION_NAME}_map_segments | |
4724 | where the layout is forced to according to a larger size in the | |
4725 | last iterations for the testcase ld-elf/header. */ | |
12bd6957 | 4726 | BFD_ASSERT (elf_program_header_size (abfd) % bed->s->sizeof_phdr |
57268894 | 4727 | == 0); |
a50b1753 NC |
4728 | phdrs = (Elf_Internal_Phdr *) |
4729 | bfd_zalloc2 (abfd, | |
12bd6957 | 4730 | (elf_program_header_size (abfd) / bed->s->sizeof_phdr), |
a50b1753 | 4731 | sizeof (Elf_Internal_Phdr)); |
f3520d2f | 4732 | elf_tdata (abfd)->phdr = phdrs; |
252b5132 | 4733 | if (phdrs == NULL) |
b34976b6 | 4734 | return FALSE; |
252b5132 | 4735 | |
3f570048 AM |
4736 | maxpagesize = 1; |
4737 | if ((abfd->flags & D_PAGED) != 0) | |
4738 | maxpagesize = bed->maxpagesize; | |
4739 | ||
252b5132 RH |
4740 | off = bed->s->sizeof_ehdr; |
4741 | off += alloc * bed->s->sizeof_phdr; | |
2b0bc088 NC |
4742 | if (header_pad < (bfd_vma) off) |
4743 | header_pad = 0; | |
4744 | else | |
4745 | header_pad -= off; | |
4746 | off += header_pad; | |
252b5132 | 4747 | |
12bd6957 | 4748 | for (m = elf_seg_map (abfd), p = phdrs, j = 0; |
252b5132 | 4749 | m != NULL; |
0920dee7 | 4750 | m = m->next, p++, j++) |
252b5132 | 4751 | { |
252b5132 | 4752 | asection **secpp; |
bf988460 AM |
4753 | bfd_vma off_adjust; |
4754 | bfd_boolean no_contents; | |
252b5132 RH |
4755 | |
4756 | /* If elf_segment_map is not from map_sections_to_segments, the | |
08a40648 | 4757 | sections may not be correctly ordered. NOTE: sorting should |
52e9b619 MS |
4758 | not be done to the PT_NOTE section of a corefile, which may |
4759 | contain several pseudo-sections artificially created by bfd. | |
4760 | Sorting these pseudo-sections breaks things badly. */ | |
47d9a591 AM |
4761 | if (m->count > 1 |
4762 | && !(elf_elfheader (abfd)->e_type == ET_CORE | |
52e9b619 | 4763 | && m->p_type == PT_NOTE)) |
252b5132 RH |
4764 | qsort (m->sections, (size_t) m->count, sizeof (asection *), |
4765 | elf_sort_sections); | |
4766 | ||
b301b248 AM |
4767 | /* An ELF segment (described by Elf_Internal_Phdr) may contain a |
4768 | number of sections with contents contributing to both p_filesz | |
4769 | and p_memsz, followed by a number of sections with no contents | |
4770 | that just contribute to p_memsz. In this loop, OFF tracks next | |
02bf8d82 | 4771 | available file offset for PT_LOAD and PT_NOTE segments. */ |
252b5132 | 4772 | p->p_type = m->p_type; |
28a7f3e7 | 4773 | p->p_flags = m->p_flags; |
252b5132 | 4774 | |
3f570048 AM |
4775 | if (m->count == 0) |
4776 | p->p_vaddr = 0; | |
4777 | else | |
3271a814 | 4778 | p->p_vaddr = m->sections[0]->vma - m->p_vaddr_offset; |
3f570048 AM |
4779 | |
4780 | if (m->p_paddr_valid) | |
4781 | p->p_paddr = m->p_paddr; | |
4782 | else if (m->count == 0) | |
4783 | p->p_paddr = 0; | |
4784 | else | |
08a40648 | 4785 | p->p_paddr = m->sections[0]->lma - m->p_vaddr_offset; |
3f570048 AM |
4786 | |
4787 | if (p->p_type == PT_LOAD | |
4788 | && (abfd->flags & D_PAGED) != 0) | |
4789 | { | |
4790 | /* p_align in demand paged PT_LOAD segments effectively stores | |
4791 | the maximum page size. When copying an executable with | |
4792 | objcopy, we set m->p_align from the input file. Use this | |
4793 | value for maxpagesize rather than bed->maxpagesize, which | |
4794 | may be different. Note that we use maxpagesize for PT_TLS | |
4795 | segment alignment later in this function, so we are relying | |
4796 | on at least one PT_LOAD segment appearing before a PT_TLS | |
4797 | segment. */ | |
4798 | if (m->p_align_valid) | |
4799 | maxpagesize = m->p_align; | |
4800 | ||
4801 | p->p_align = maxpagesize; | |
4802 | } | |
3271a814 NS |
4803 | else if (m->p_align_valid) |
4804 | p->p_align = m->p_align; | |
e970b90a DJ |
4805 | else if (m->count == 0) |
4806 | p->p_align = 1 << bed->s->log_file_align; | |
3f570048 AM |
4807 | else |
4808 | p->p_align = 0; | |
4809 | ||
bf988460 AM |
4810 | no_contents = FALSE; |
4811 | off_adjust = 0; | |
252b5132 | 4812 | if (p->p_type == PT_LOAD |
b301b248 | 4813 | && m->count > 0) |
252b5132 | 4814 | { |
b301b248 | 4815 | bfd_size_type align; |
a49e53ed | 4816 | unsigned int align_power = 0; |
b301b248 | 4817 | |
3271a814 NS |
4818 | if (m->p_align_valid) |
4819 | align = p->p_align; | |
4820 | else | |
252b5132 | 4821 | { |
3271a814 NS |
4822 | for (i = 0, secpp = m->sections; i < m->count; i++, secpp++) |
4823 | { | |
4824 | unsigned int secalign; | |
08a40648 | 4825 | |
3271a814 NS |
4826 | secalign = bfd_get_section_alignment (abfd, *secpp); |
4827 | if (secalign > align_power) | |
4828 | align_power = secalign; | |
4829 | } | |
4830 | align = (bfd_size_type) 1 << align_power; | |
4831 | if (align < maxpagesize) | |
4832 | align = maxpagesize; | |
b301b248 | 4833 | } |
252b5132 | 4834 | |
02bf8d82 AM |
4835 | for (i = 0; i < m->count; i++) |
4836 | if ((m->sections[i]->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0) | |
4837 | /* If we aren't making room for this section, then | |
4838 | it must be SHT_NOBITS regardless of what we've | |
4839 | set via struct bfd_elf_special_section. */ | |
4840 | elf_section_type (m->sections[i]) = SHT_NOBITS; | |
4841 | ||
bf988460 | 4842 | /* Find out whether this segment contains any loadable |
aea274d3 AM |
4843 | sections. */ |
4844 | no_contents = TRUE; | |
4845 | for (i = 0; i < m->count; i++) | |
4846 | if (elf_section_type (m->sections[i]) != SHT_NOBITS) | |
4847 | { | |
4848 | no_contents = FALSE; | |
4849 | break; | |
4850 | } | |
bf988460 | 4851 | |
85cfcbfb | 4852 | off_adjust = vma_page_aligned_bias (p->p_vaddr, off, align); |
bf988460 AM |
4853 | off += off_adjust; |
4854 | if (no_contents) | |
4855 | { | |
4856 | /* We shouldn't need to align the segment on disk since | |
4857 | the segment doesn't need file space, but the gABI | |
4858 | arguably requires the alignment and glibc ld.so | |
4859 | checks it. So to comply with the alignment | |
4860 | requirement but not waste file space, we adjust | |
4861 | p_offset for just this segment. (OFF_ADJUST is | |
4862 | subtracted from OFF later.) This may put p_offset | |
4863 | past the end of file, but that shouldn't matter. */ | |
4864 | } | |
4865 | else | |
4866 | off_adjust = 0; | |
252b5132 | 4867 | } |
b1a6d0b1 NC |
4868 | /* Make sure the .dynamic section is the first section in the |
4869 | PT_DYNAMIC segment. */ | |
4870 | else if (p->p_type == PT_DYNAMIC | |
4871 | && m->count > 1 | |
4872 | && strcmp (m->sections[0]->name, ".dynamic") != 0) | |
4873 | { | |
4874 | _bfd_error_handler | |
b301b248 AM |
4875 | (_("%B: The first section in the PT_DYNAMIC segment is not the .dynamic section"), |
4876 | abfd); | |
b1a6d0b1 NC |
4877 | bfd_set_error (bfd_error_bad_value); |
4878 | return FALSE; | |
4879 | } | |
3f001e84 JK |
4880 | /* Set the note section type to SHT_NOTE. */ |
4881 | else if (p->p_type == PT_NOTE) | |
4882 | for (i = 0; i < m->count; i++) | |
4883 | elf_section_type (m->sections[i]) = SHT_NOTE; | |
252b5132 | 4884 | |
252b5132 RH |
4885 | p->p_offset = 0; |
4886 | p->p_filesz = 0; | |
4887 | p->p_memsz = 0; | |
4888 | ||
4889 | if (m->includes_filehdr) | |
4890 | { | |
bf988460 | 4891 | if (!m->p_flags_valid) |
252b5132 | 4892 | p->p_flags |= PF_R; |
252b5132 RH |
4893 | p->p_filesz = bed->s->sizeof_ehdr; |
4894 | p->p_memsz = bed->s->sizeof_ehdr; | |
4895 | if (m->count > 0) | |
4896 | { | |
252b5132 RH |
4897 | if (p->p_vaddr < (bfd_vma) off) |
4898 | { | |
caf47ea6 | 4899 | (*_bfd_error_handler) |
b301b248 AM |
4900 | (_("%B: Not enough room for program headers, try linking with -N"), |
4901 | abfd); | |
252b5132 | 4902 | bfd_set_error (bfd_error_bad_value); |
b34976b6 | 4903 | return FALSE; |
252b5132 RH |
4904 | } |
4905 | ||
4906 | p->p_vaddr -= off; | |
bf988460 | 4907 | if (!m->p_paddr_valid) |
252b5132 RH |
4908 | p->p_paddr -= off; |
4909 | } | |
252b5132 RH |
4910 | } |
4911 | ||
4912 | if (m->includes_phdrs) | |
4913 | { | |
bf988460 | 4914 | if (!m->p_flags_valid) |
252b5132 RH |
4915 | p->p_flags |= PF_R; |
4916 | ||
f3520d2f | 4917 | if (!m->includes_filehdr) |
252b5132 RH |
4918 | { |
4919 | p->p_offset = bed->s->sizeof_ehdr; | |
4920 | ||
4921 | if (m->count > 0) | |
4922 | { | |
252b5132 | 4923 | p->p_vaddr -= off - p->p_offset; |
bf988460 | 4924 | if (!m->p_paddr_valid) |
252b5132 RH |
4925 | p->p_paddr -= off - p->p_offset; |
4926 | } | |
252b5132 RH |
4927 | } |
4928 | ||
4929 | p->p_filesz += alloc * bed->s->sizeof_phdr; | |
4930 | p->p_memsz += alloc * bed->s->sizeof_phdr; | |
2b0bc088 NC |
4931 | if (m->count) |
4932 | { | |
4933 | p->p_filesz += header_pad; | |
4934 | p->p_memsz += header_pad; | |
4935 | } | |
252b5132 RH |
4936 | } |
4937 | ||
4938 | if (p->p_type == PT_LOAD | |
4939 | || (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core)) | |
4940 | { | |
bf988460 | 4941 | if (!m->includes_filehdr && !m->includes_phdrs) |
02bf8d82 | 4942 | p->p_offset = off; |
252b5132 RH |
4943 | else |
4944 | { | |
4945 | file_ptr adjust; | |
4946 | ||
4947 | adjust = off - (p->p_offset + p->p_filesz); | |
bf988460 AM |
4948 | if (!no_contents) |
4949 | p->p_filesz += adjust; | |
252b5132 RH |
4950 | p->p_memsz += adjust; |
4951 | } | |
4952 | } | |
4953 | ||
1ea63fd2 AM |
4954 | /* Set up p_filesz, p_memsz, p_align and p_flags from the section |
4955 | maps. Set filepos for sections in PT_LOAD segments, and in | |
4956 | core files, for sections in PT_NOTE segments. | |
4957 | assign_file_positions_for_non_load_sections will set filepos | |
4958 | for other sections and update p_filesz for other segments. */ | |
252b5132 RH |
4959 | for (i = 0, secpp = m->sections; i < m->count; i++, secpp++) |
4960 | { | |
4961 | asection *sec; | |
252b5132 | 4962 | bfd_size_type align; |
627b32bc | 4963 | Elf_Internal_Shdr *this_hdr; |
252b5132 RH |
4964 | |
4965 | sec = *secpp; | |
02bf8d82 | 4966 | this_hdr = &elf_section_data (sec)->this_hdr; |
3f570048 | 4967 | align = (bfd_size_type) 1 << bfd_get_section_alignment (abfd, sec); |
252b5132 | 4968 | |
88967714 AM |
4969 | if ((p->p_type == PT_LOAD |
4970 | || p->p_type == PT_TLS) | |
4971 | && (this_hdr->sh_type != SHT_NOBITS | |
4972 | || ((this_hdr->sh_flags & SHF_ALLOC) != 0 | |
4973 | && ((this_hdr->sh_flags & SHF_TLS) == 0 | |
4974 | || p->p_type == PT_TLS)))) | |
252b5132 | 4975 | { |
b5599592 AM |
4976 | bfd_vma p_start = p->p_paddr; |
4977 | bfd_vma p_end = p_start + p->p_memsz; | |
4978 | bfd_vma s_start = sec->lma; | |
4979 | bfd_vma adjust = s_start - p_end; | |
252b5132 | 4980 | |
a2d1e028 L |
4981 | if (adjust != 0 |
4982 | && (s_start < p_end | |
4983 | || p_end < p_start)) | |
252b5132 | 4984 | { |
88967714 | 4985 | (*_bfd_error_handler) |
b5599592 AM |
4986 | (_("%B: section %A lma %#lx adjusted to %#lx"), abfd, sec, |
4987 | (unsigned long) s_start, (unsigned long) p_end); | |
88967714 | 4988 | adjust = 0; |
b5599592 | 4989 | sec->lma = p_end; |
1cfb7d1e | 4990 | } |
3ac9b6c9 | 4991 | p->p_memsz += adjust; |
1cfb7d1e | 4992 | |
88967714 AM |
4993 | if (this_hdr->sh_type != SHT_NOBITS) |
4994 | { | |
32812159 AM |
4995 | if (p->p_filesz + adjust < p->p_memsz) |
4996 | { | |
4997 | /* We have a PROGBITS section following NOBITS ones. | |
4998 | Allocate file space for the NOBITS section(s) and | |
4999 | zero it. */ | |
5000 | adjust = p->p_memsz - p->p_filesz; | |
5001 | if (!write_zeros (abfd, off, adjust)) | |
5002 | return FALSE; | |
5003 | } | |
88967714 AM |
5004 | off += adjust; |
5005 | p->p_filesz += adjust; | |
252b5132 | 5006 | } |
252b5132 RH |
5007 | } |
5008 | ||
5009 | if (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core) | |
5010 | { | |
b301b248 AM |
5011 | /* The section at i == 0 is the one that actually contains |
5012 | everything. */ | |
4a938328 MS |
5013 | if (i == 0) |
5014 | { | |
627b32bc | 5015 | this_hdr->sh_offset = sec->filepos = off; |
6a3cd2b4 AM |
5016 | off += this_hdr->sh_size; |
5017 | p->p_filesz = this_hdr->sh_size; | |
b301b248 AM |
5018 | p->p_memsz = 0; |
5019 | p->p_align = 1; | |
252b5132 | 5020 | } |
4a938328 | 5021 | else |
252b5132 | 5022 | { |
b301b248 | 5023 | /* The rest are fake sections that shouldn't be written. */ |
252b5132 | 5024 | sec->filepos = 0; |
eea6121a | 5025 | sec->size = 0; |
b301b248 AM |
5026 | sec->flags = 0; |
5027 | continue; | |
252b5132 | 5028 | } |
252b5132 RH |
5029 | } |
5030 | else | |
5031 | { | |
1e951488 | 5032 | if (p->p_type == PT_LOAD) |
b301b248 | 5033 | { |
1e951488 AM |
5034 | this_hdr->sh_offset = sec->filepos = off; |
5035 | if (this_hdr->sh_type != SHT_NOBITS) | |
5036 | off += this_hdr->sh_size; | |
5037 | } | |
5038 | else if (this_hdr->sh_type == SHT_NOBITS | |
5039 | && (this_hdr->sh_flags & SHF_TLS) != 0 | |
5040 | && this_hdr->sh_offset == 0) | |
5041 | { | |
5042 | /* This is a .tbss section that didn't get a PT_LOAD. | |
5043 | (See _bfd_elf_map_sections_to_segments "Create a | |
5044 | final PT_LOAD".) Set sh_offset to the value it | |
5045 | would have if we had created a zero p_filesz and | |
5046 | p_memsz PT_LOAD header for the section. This | |
5047 | also makes the PT_TLS header have the same | |
5048 | p_offset value. */ | |
5049 | bfd_vma adjust = vma_page_aligned_bias (this_hdr->sh_addr, | |
5050 | off, align); | |
5051 | this_hdr->sh_offset = sec->filepos = off + adjust; | |
b301b248 | 5052 | } |
252b5132 | 5053 | |
02bf8d82 | 5054 | if (this_hdr->sh_type != SHT_NOBITS) |
b301b248 | 5055 | { |
6a3cd2b4 | 5056 | p->p_filesz += this_hdr->sh_size; |
02bf8d82 AM |
5057 | /* A load section without SHF_ALLOC is something like |
5058 | a note section in a PT_NOTE segment. These take | |
5059 | file space but are not loaded into memory. */ | |
5060 | if ((this_hdr->sh_flags & SHF_ALLOC) != 0) | |
6a3cd2b4 | 5061 | p->p_memsz += this_hdr->sh_size; |
b301b248 | 5062 | } |
6a3cd2b4 | 5063 | else if ((this_hdr->sh_flags & SHF_ALLOC) != 0) |
13ae64f3 | 5064 | { |
6a3cd2b4 AM |
5065 | if (p->p_type == PT_TLS) |
5066 | p->p_memsz += this_hdr->sh_size; | |
5067 | ||
5068 | /* .tbss is special. It doesn't contribute to p_memsz of | |
5069 | normal segments. */ | |
5070 | else if ((this_hdr->sh_flags & SHF_TLS) == 0) | |
5071 | p->p_memsz += this_hdr->sh_size; | |
13ae64f3 JJ |
5072 | } |
5073 | ||
b10a8ae0 L |
5074 | if (align > p->p_align |
5075 | && !m->p_align_valid | |
5076 | && (p->p_type != PT_LOAD | |
5077 | || (abfd->flags & D_PAGED) == 0)) | |
252b5132 RH |
5078 | p->p_align = align; |
5079 | } | |
5080 | ||
bf988460 | 5081 | if (!m->p_flags_valid) |
252b5132 RH |
5082 | { |
5083 | p->p_flags |= PF_R; | |
02bf8d82 | 5084 | if ((this_hdr->sh_flags & SHF_EXECINSTR) != 0) |
252b5132 | 5085 | p->p_flags |= PF_X; |
02bf8d82 | 5086 | if ((this_hdr->sh_flags & SHF_WRITE) != 0) |
252b5132 RH |
5087 | p->p_flags |= PF_W; |
5088 | } | |
5089 | } | |
43a8475c | 5090 | |
bf988460 | 5091 | off -= off_adjust; |
0920dee7 | 5092 | |
7c928300 AM |
5093 | /* Check that all sections are in a PT_LOAD segment. |
5094 | Don't check funky gdb generated core files. */ | |
5095 | if (p->p_type == PT_LOAD && bfd_get_format (abfd) != bfd_core) | |
9a83a553 AM |
5096 | { |
5097 | bfd_boolean check_vma = TRUE; | |
5098 | ||
5099 | for (i = 1; i < m->count; i++) | |
5100 | if (m->sections[i]->vma == m->sections[i - 1]->vma | |
5101 | && ELF_SECTION_SIZE (&(elf_section_data (m->sections[i]) | |
5102 | ->this_hdr), p) != 0 | |
5103 | && ELF_SECTION_SIZE (&(elf_section_data (m->sections[i - 1]) | |
5104 | ->this_hdr), p) != 0) | |
0920dee7 | 5105 | { |
9a83a553 AM |
5106 | /* Looks like we have overlays packed into the segment. */ |
5107 | check_vma = FALSE; | |
5108 | break; | |
0920dee7 | 5109 | } |
9a83a553 AM |
5110 | |
5111 | for (i = 0; i < m->count; i++) | |
5112 | { | |
5113 | Elf_Internal_Shdr *this_hdr; | |
5114 | asection *sec; | |
5115 | ||
5116 | sec = m->sections[i]; | |
5117 | this_hdr = &(elf_section_data(sec)->this_hdr); | |
86b2281f AM |
5118 | if (!ELF_SECTION_IN_SEGMENT_1 (this_hdr, p, check_vma, 0) |
5119 | && !ELF_TBSS_SPECIAL (this_hdr, p)) | |
9a83a553 AM |
5120 | { |
5121 | (*_bfd_error_handler) | |
5122 | (_("%B: section `%A' can't be allocated in segment %d"), | |
5123 | abfd, sec, j); | |
5124 | print_segment_map (m); | |
5125 | } | |
5126 | } | |
5127 | } | |
252b5132 RH |
5128 | } |
5129 | ||
12bd6957 | 5130 | elf_next_file_pos (abfd) = off; |
f3520d2f AM |
5131 | return TRUE; |
5132 | } | |
5133 | ||
5134 | /* Assign file positions for the other sections. */ | |
5135 | ||
5136 | static bfd_boolean | |
5137 | assign_file_positions_for_non_load_sections (bfd *abfd, | |
5138 | struct bfd_link_info *link_info) | |
5139 | { | |
5140 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
5141 | Elf_Internal_Shdr **i_shdrpp; | |
e06efbf1 | 5142 | Elf_Internal_Shdr **hdrpp, **end_hdrpp; |
f3520d2f AM |
5143 | Elf_Internal_Phdr *phdrs; |
5144 | Elf_Internal_Phdr *p; | |
5145 | struct elf_segment_map *m; | |
62655c7b | 5146 | struct elf_segment_map *hdrs_segment; |
f3520d2f AM |
5147 | bfd_vma filehdr_vaddr, filehdr_paddr; |
5148 | bfd_vma phdrs_vaddr, phdrs_paddr; | |
5149 | file_ptr off; | |
f3520d2f AM |
5150 | unsigned int count; |
5151 | ||
5c182d5f | 5152 | i_shdrpp = elf_elfsections (abfd); |
e06efbf1 | 5153 | end_hdrpp = i_shdrpp + elf_numsections (abfd); |
12bd6957 | 5154 | off = elf_next_file_pos (abfd); |
e06efbf1 | 5155 | for (hdrpp = i_shdrpp + 1; hdrpp < end_hdrpp; hdrpp++) |
5c182d5f | 5156 | { |
5c182d5f AM |
5157 | Elf_Internal_Shdr *hdr; |
5158 | ||
5159 | hdr = *hdrpp; | |
5160 | if (hdr->bfd_section != NULL | |
252e386e AM |
5161 | && (hdr->bfd_section->filepos != 0 |
5162 | || (hdr->sh_type == SHT_NOBITS | |
5163 | && hdr->contents == NULL))) | |
627b32bc | 5164 | BFD_ASSERT (hdr->sh_offset == hdr->bfd_section->filepos); |
5c182d5f AM |
5165 | else if ((hdr->sh_flags & SHF_ALLOC) != 0) |
5166 | { | |
e8d2ba53 AM |
5167 | if (hdr->sh_size != 0) |
5168 | (*_bfd_error_handler) | |
5169 | (_("%B: warning: allocated section `%s' not in segment"), | |
5170 | abfd, | |
5171 | (hdr->bfd_section == NULL | |
5172 | ? "*unknown*" | |
5173 | : hdr->bfd_section->name)); | |
3ba71138 L |
5174 | /* We don't need to page align empty sections. */ |
5175 | if ((abfd->flags & D_PAGED) != 0 && hdr->sh_size != 0) | |
5c182d5f AM |
5176 | off += vma_page_aligned_bias (hdr->sh_addr, off, |
5177 | bed->maxpagesize); | |
5178 | else | |
5179 | off += vma_page_aligned_bias (hdr->sh_addr, off, | |
5180 | hdr->sh_addralign); | |
5181 | off = _bfd_elf_assign_file_position_for_section (hdr, off, | |
5182 | FALSE); | |
5183 | } | |
5184 | else if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA) | |
5185 | && hdr->bfd_section == NULL) | |
0ce398f1 L |
5186 | || (hdr->bfd_section != NULL |
5187 | && (hdr->bfd_section->flags & SEC_ELF_COMPRESS)) | |
5188 | /* Compress DWARF debug sections. */ | |
12bd6957 AM |
5189 | || hdr == i_shdrpp[elf_onesymtab (abfd)] |
5190 | || hdr == i_shdrpp[elf_symtab_shndx (abfd)] | |
5191 | || hdr == i_shdrpp[elf_strtab_sec (abfd)]) | |
5c182d5f AM |
5192 | hdr->sh_offset = -1; |
5193 | else | |
5194 | off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE); | |
5c182d5f AM |
5195 | } |
5196 | ||
252b5132 RH |
5197 | /* Now that we have set the section file positions, we can set up |
5198 | the file positions for the non PT_LOAD segments. */ | |
f3520d2f AM |
5199 | count = 0; |
5200 | filehdr_vaddr = 0; | |
5201 | filehdr_paddr = 0; | |
5202 | phdrs_vaddr = bed->maxpagesize + bed->s->sizeof_ehdr; | |
5203 | phdrs_paddr = 0; | |
62655c7b | 5204 | hdrs_segment = NULL; |
f3520d2f | 5205 | phdrs = elf_tdata (abfd)->phdr; |
12bd6957 | 5206 | for (m = elf_seg_map (abfd), p = phdrs; m != NULL; m = m->next, p++) |
f3520d2f AM |
5207 | { |
5208 | ++count; | |
5209 | if (p->p_type != PT_LOAD) | |
5210 | continue; | |
5211 | ||
5212 | if (m->includes_filehdr) | |
5213 | { | |
5214 | filehdr_vaddr = p->p_vaddr; | |
5215 | filehdr_paddr = p->p_paddr; | |
5216 | } | |
5217 | if (m->includes_phdrs) | |
5218 | { | |
5219 | phdrs_vaddr = p->p_vaddr; | |
5220 | phdrs_paddr = p->p_paddr; | |
5221 | if (m->includes_filehdr) | |
5222 | { | |
62655c7b | 5223 | hdrs_segment = m; |
f3520d2f AM |
5224 | phdrs_vaddr += bed->s->sizeof_ehdr; |
5225 | phdrs_paddr += bed->s->sizeof_ehdr; | |
5226 | } | |
5227 | } | |
5228 | } | |
5229 | ||
62655c7b RM |
5230 | if (hdrs_segment != NULL && link_info != NULL) |
5231 | { | |
5232 | /* There is a segment that contains both the file headers and the | |
5233 | program headers, so provide a symbol __ehdr_start pointing there. | |
5234 | A program can use this to examine itself robustly. */ | |
5235 | ||
5236 | struct elf_link_hash_entry *hash | |
5237 | = elf_link_hash_lookup (elf_hash_table (link_info), "__ehdr_start", | |
5238 | FALSE, FALSE, TRUE); | |
5239 | /* If the symbol was referenced and not defined, define it. */ | |
5240 | if (hash != NULL | |
5241 | && (hash->root.type == bfd_link_hash_new | |
5242 | || hash->root.type == bfd_link_hash_undefined | |
5243 | || hash->root.type == bfd_link_hash_undefweak | |
5244 | || hash->root.type == bfd_link_hash_common)) | |
5245 | { | |
5246 | asection *s = NULL; | |
5247 | if (hdrs_segment->count != 0) | |
5248 | /* The segment contains sections, so use the first one. */ | |
5249 | s = hdrs_segment->sections[0]; | |
5250 | else | |
5251 | /* Use the first (i.e. lowest-addressed) section in any segment. */ | |
12bd6957 | 5252 | for (m = elf_seg_map (abfd); m != NULL; m = m->next) |
62655c7b RM |
5253 | if (m->count != 0) |
5254 | { | |
5255 | s = m->sections[0]; | |
5256 | break; | |
5257 | } | |
5258 | ||
5259 | if (s != NULL) | |
5260 | { | |
5261 | hash->root.u.def.value = filehdr_vaddr - s->vma; | |
5262 | hash->root.u.def.section = s; | |
5263 | } | |
5264 | else | |
5265 | { | |
5266 | hash->root.u.def.value = filehdr_vaddr; | |
5267 | hash->root.u.def.section = bfd_abs_section_ptr; | |
5268 | } | |
5269 | ||
5270 | hash->root.type = bfd_link_hash_defined; | |
5271 | hash->def_regular = 1; | |
5272 | hash->non_elf = 0; | |
5273 | } | |
5274 | } | |
5275 | ||
12bd6957 | 5276 | for (m = elf_seg_map (abfd), p = phdrs; m != NULL; m = m->next, p++) |
252b5132 | 5277 | { |
129af99f | 5278 | if (p->p_type == PT_GNU_RELRO) |
252b5132 | 5279 | { |
b84a33b5 | 5280 | const Elf_Internal_Phdr *lp; |
3146fac4 | 5281 | struct elf_segment_map *lm; |
1ea63fd2 | 5282 | |
129af99f | 5283 | if (link_info != NULL) |
8c37241b | 5284 | { |
129af99f AS |
5285 | /* During linking the range of the RELRO segment is passed |
5286 | in link_info. */ | |
12bd6957 | 5287 | for (lm = elf_seg_map (abfd), lp = phdrs; |
3146fac4 AM |
5288 | lm != NULL; |
5289 | lm = lm->next, lp++) | |
8c37241b JJ |
5290 | { |
5291 | if (lp->p_type == PT_LOAD | |
b84a33b5 | 5292 | && lp->p_vaddr < link_info->relro_end |
3146fac4 AM |
5293 | && lm->count != 0 |
5294 | && lm->sections[0]->vma >= link_info->relro_start) | |
8c37241b JJ |
5295 | break; |
5296 | } | |
8981c88a | 5297 | |
3146fac4 | 5298 | BFD_ASSERT (lm != NULL); |
8c37241b | 5299 | } |
129af99f AS |
5300 | else |
5301 | { | |
5302 | /* Otherwise we are copying an executable or shared | |
b84a33b5 | 5303 | library, but we need to use the same linker logic. */ |
129af99f AS |
5304 | for (lp = phdrs; lp < phdrs + count; ++lp) |
5305 | { | |
5306 | if (lp->p_type == PT_LOAD | |
5307 | && lp->p_paddr == p->p_paddr) | |
5308 | break; | |
5309 | } | |
b84a33b5 AM |
5310 | } |
5311 | ||
5312 | if (lp < phdrs + count) | |
5313 | { | |
5314 | p->p_vaddr = lp->p_vaddr; | |
5315 | p->p_paddr = lp->p_paddr; | |
5316 | p->p_offset = lp->p_offset; | |
5317 | if (link_info != NULL) | |
5318 | p->p_filesz = link_info->relro_end - lp->p_vaddr; | |
5319 | else if (m->p_size_valid) | |
5320 | p->p_filesz = m->p_size; | |
129af99f AS |
5321 | else |
5322 | abort (); | |
b84a33b5 | 5323 | p->p_memsz = p->p_filesz; |
f3944f72 L |
5324 | /* Preserve the alignment and flags if they are valid. The |
5325 | gold linker generates RW/4 for the PT_GNU_RELRO section. | |
5326 | It is better for objcopy/strip to honor these attributes | |
5327 | otherwise gdb will choke when using separate debug files. | |
5328 | */ | |
5329 | if (!m->p_align_valid) | |
5330 | p->p_align = 1; | |
5331 | if (!m->p_flags_valid) | |
5332 | p->p_flags = (lp->p_flags & ~PF_W); | |
129af99f | 5333 | } |
9433b9b1 | 5334 | else |
b84a33b5 AM |
5335 | { |
5336 | memset (p, 0, sizeof *p); | |
5337 | p->p_type = PT_NULL; | |
5338 | } | |
129af99f | 5339 | } |
04c3a755 NS |
5340 | else if (p->p_type == PT_GNU_STACK) |
5341 | { | |
5342 | if (m->p_size_valid) | |
5343 | p->p_memsz = m->p_size; | |
5344 | } | |
129af99f AS |
5345 | else if (m->count != 0) |
5346 | { | |
e06efbf1 | 5347 | unsigned int i; |
129af99f AS |
5348 | if (p->p_type != PT_LOAD |
5349 | && (p->p_type != PT_NOTE | |
5350 | || bfd_get_format (abfd) != bfd_core)) | |
5351 | { | |
c86934ce NC |
5352 | if (m->includes_filehdr || m->includes_phdrs) |
5353 | { | |
5354 | /* PR 17512: file: 2195325e. */ | |
5355 | (*_bfd_error_handler) | |
5356 | (_("%B: warning: non-load segment includes file header and/or program header"), | |
5357 | abfd); | |
5358 | return FALSE; | |
5359 | } | |
129af99f | 5360 | |
86b2281f | 5361 | p->p_filesz = 0; |
129af99f | 5362 | p->p_offset = m->sections[0]->filepos; |
86b2281f AM |
5363 | for (i = m->count; i-- != 0;) |
5364 | { | |
5365 | asection *sect = m->sections[i]; | |
5366 | Elf_Internal_Shdr *hdr = &elf_section_data (sect)->this_hdr; | |
5367 | if (hdr->sh_type != SHT_NOBITS) | |
5368 | { | |
5369 | p->p_filesz = (sect->filepos - m->sections[0]->filepos | |
5370 | + hdr->sh_size); | |
5371 | break; | |
5372 | } | |
5373 | } | |
129af99f AS |
5374 | } |
5375 | } | |
5376 | else if (m->includes_filehdr) | |
5377 | { | |
5378 | p->p_vaddr = filehdr_vaddr; | |
5379 | if (! m->p_paddr_valid) | |
5380 | p->p_paddr = filehdr_paddr; | |
5381 | } | |
5382 | else if (m->includes_phdrs) | |
5383 | { | |
5384 | p->p_vaddr = phdrs_vaddr; | |
5385 | if (! m->p_paddr_valid) | |
5386 | p->p_paddr = phdrs_paddr; | |
252b5132 RH |
5387 | } |
5388 | } | |
5389 | ||
12bd6957 | 5390 | elf_next_file_pos (abfd) = off; |
252b5132 | 5391 | |
b34976b6 | 5392 | return TRUE; |
252b5132 RH |
5393 | } |
5394 | ||
252b5132 RH |
5395 | /* Work out the file positions of all the sections. This is called by |
5396 | _bfd_elf_compute_section_file_positions. All the section sizes and | |
5397 | VMAs must be known before this is called. | |
5398 | ||
e0638f70 AM |
5399 | Reloc sections come in two flavours: Those processed specially as |
5400 | "side-channel" data attached to a section to which they apply, and | |
5401 | those that bfd doesn't process as relocations. The latter sort are | |
5402 | stored in a normal bfd section by bfd_section_from_shdr. We don't | |
5403 | consider the former sort here, unless they form part of the loadable | |
5404 | image. Reloc sections not assigned here will be handled later by | |
5405 | assign_file_positions_for_relocs. | |
252b5132 RH |
5406 | |
5407 | We also don't set the positions of the .symtab and .strtab here. */ | |
5408 | ||
b34976b6 | 5409 | static bfd_boolean |
c84fca4d AO |
5410 | assign_file_positions_except_relocs (bfd *abfd, |
5411 | struct bfd_link_info *link_info) | |
252b5132 | 5412 | { |
5c182d5f AM |
5413 | struct elf_obj_tdata *tdata = elf_tdata (abfd); |
5414 | Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd); | |
9c5bfbb7 | 5415 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
252b5132 RH |
5416 | |
5417 | if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0 | |
5418 | && bfd_get_format (abfd) != bfd_core) | |
5419 | { | |
5c182d5f AM |
5420 | Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd); |
5421 | unsigned int num_sec = elf_numsections (abfd); | |
252b5132 RH |
5422 | Elf_Internal_Shdr **hdrpp; |
5423 | unsigned int i; | |
a485e98e | 5424 | file_ptr off; |
252b5132 RH |
5425 | |
5426 | /* Start after the ELF header. */ | |
5427 | off = i_ehdrp->e_ehsize; | |
5428 | ||
5429 | /* We are not creating an executable, which means that we are | |
5430 | not creating a program header, and that the actual order of | |
5431 | the sections in the file is unimportant. */ | |
9ad5cbcf | 5432 | for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++) |
252b5132 RH |
5433 | { |
5434 | Elf_Internal_Shdr *hdr; | |
5435 | ||
5436 | hdr = *hdrpp; | |
e0638f70 AM |
5437 | if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA) |
5438 | && hdr->bfd_section == NULL) | |
0ce398f1 L |
5439 | || (hdr->bfd_section != NULL |
5440 | && (hdr->bfd_section->flags & SEC_ELF_COMPRESS)) | |
5441 | /* Compress DWARF debug sections. */ | |
12bd6957 AM |
5442 | || i == elf_onesymtab (abfd) |
5443 | || i == elf_symtab_shndx (abfd) | |
5444 | || i == elf_strtab_sec (abfd)) | |
252b5132 RH |
5445 | { |
5446 | hdr->sh_offset = -1; | |
252b5132 | 5447 | } |
9ad5cbcf | 5448 | else |
b34976b6 | 5449 | off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE); |
252b5132 | 5450 | } |
a485e98e AM |
5451 | |
5452 | elf_next_file_pos (abfd) = off; | |
252b5132 RH |
5453 | } |
5454 | else | |
5455 | { | |
f3520d2f AM |
5456 | unsigned int alloc; |
5457 | ||
252b5132 | 5458 | /* Assign file positions for the loaded sections based on the |
08a40648 | 5459 | assignment of sections to segments. */ |
f3520d2f AM |
5460 | if (!assign_file_positions_for_load_sections (abfd, link_info)) |
5461 | return FALSE; | |
5462 | ||
5463 | /* And for non-load sections. */ | |
5464 | if (!assign_file_positions_for_non_load_sections (abfd, link_info)) | |
5465 | return FALSE; | |
5466 | ||
e36284ab AM |
5467 | if (bed->elf_backend_modify_program_headers != NULL) |
5468 | { | |
5469 | if (!(*bed->elf_backend_modify_program_headers) (abfd, link_info)) | |
5470 | return FALSE; | |
5471 | } | |
5472 | ||
58e7ebac L |
5473 | /* Set e_type in ELF header to ET_EXEC for -pie -Ttext-segment=. */ |
5474 | if (link_info != NULL | |
5475 | && link_info->executable | |
5476 | && link_info->shared) | |
5477 | { | |
5478 | unsigned int num_segments = elf_elfheader (abfd)->e_phnum; | |
5479 | Elf_Internal_Phdr *segment = elf_tdata (abfd)->phdr; | |
5480 | Elf_Internal_Phdr *end_segment = &segment[num_segments]; | |
5481 | ||
5482 | /* Find the lowest p_vaddr in PT_LOAD segments. */ | |
5483 | bfd_vma p_vaddr = (bfd_vma) -1; | |
5484 | for (; segment < end_segment; segment++) | |
5485 | if (segment->p_type == PT_LOAD && p_vaddr > segment->p_vaddr) | |
5486 | p_vaddr = segment->p_vaddr; | |
5487 | ||
5488 | /* Set e_type to ET_EXEC if the lowest p_vaddr in PT_LOAD | |
5489 | segments is non-zero. */ | |
5490 | if (p_vaddr) | |
5491 | i_ehdrp->e_type = ET_EXEC; | |
5492 | } | |
5493 | ||
f3520d2f | 5494 | /* Write out the program headers. */ |
12bd6957 | 5495 | alloc = elf_program_header_size (abfd) / bed->s->sizeof_phdr; |
f3520d2f AM |
5496 | if (bfd_seek (abfd, (bfd_signed_vma) bed->s->sizeof_ehdr, SEEK_SET) != 0 |
5497 | || bed->s->write_out_phdrs (abfd, tdata->phdr, alloc) != 0) | |
b34976b6 | 5498 | return FALSE; |
252b5132 RH |
5499 | } |
5500 | ||
b34976b6 | 5501 | return TRUE; |
252b5132 RH |
5502 | } |
5503 | ||
b34976b6 | 5504 | static bfd_boolean |
217aa764 | 5505 | prep_headers (bfd *abfd) |
252b5132 | 5506 | { |
3d540e93 | 5507 | Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form. */ |
2b0f7ef9 | 5508 | struct elf_strtab_hash *shstrtab; |
9c5bfbb7 | 5509 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
252b5132 RH |
5510 | |
5511 | i_ehdrp = elf_elfheader (abfd); | |
252b5132 | 5512 | |
2b0f7ef9 | 5513 | shstrtab = _bfd_elf_strtab_init (); |
252b5132 | 5514 | if (shstrtab == NULL) |
b34976b6 | 5515 | return FALSE; |
252b5132 RH |
5516 | |
5517 | elf_shstrtab (abfd) = shstrtab; | |
5518 | ||
5519 | i_ehdrp->e_ident[EI_MAG0] = ELFMAG0; | |
5520 | i_ehdrp->e_ident[EI_MAG1] = ELFMAG1; | |
5521 | i_ehdrp->e_ident[EI_MAG2] = ELFMAG2; | |
5522 | i_ehdrp->e_ident[EI_MAG3] = ELFMAG3; | |
5523 | ||
5524 | i_ehdrp->e_ident[EI_CLASS] = bed->s->elfclass; | |
5525 | i_ehdrp->e_ident[EI_DATA] = | |
5526 | bfd_big_endian (abfd) ? ELFDATA2MSB : ELFDATA2LSB; | |
5527 | i_ehdrp->e_ident[EI_VERSION] = bed->s->ev_current; | |
5528 | ||
252b5132 RH |
5529 | if ((abfd->flags & DYNAMIC) != 0) |
5530 | i_ehdrp->e_type = ET_DYN; | |
5531 | else if ((abfd->flags & EXEC_P) != 0) | |
5532 | i_ehdrp->e_type = ET_EXEC; | |
5533 | else if (bfd_get_format (abfd) == bfd_core) | |
5534 | i_ehdrp->e_type = ET_CORE; | |
5535 | else | |
5536 | i_ehdrp->e_type = ET_REL; | |
5537 | ||
5538 | switch (bfd_get_arch (abfd)) | |
5539 | { | |
5540 | case bfd_arch_unknown: | |
5541 | i_ehdrp->e_machine = EM_NONE; | |
5542 | break; | |
aa4f99bb AO |
5543 | |
5544 | /* There used to be a long list of cases here, each one setting | |
5545 | e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE | |
5546 | in the corresponding bfd definition. To avoid duplication, | |
5547 | the switch was removed. Machines that need special handling | |
5548 | can generally do it in elf_backend_final_write_processing(), | |
5549 | unless they need the information earlier than the final write. | |
5550 | Such need can generally be supplied by replacing the tests for | |
5551 | e_machine with the conditions used to determine it. */ | |
252b5132 | 5552 | default: |
9c5bfbb7 AM |
5553 | i_ehdrp->e_machine = bed->elf_machine_code; |
5554 | } | |
aa4f99bb | 5555 | |
252b5132 RH |
5556 | i_ehdrp->e_version = bed->s->ev_current; |
5557 | i_ehdrp->e_ehsize = bed->s->sizeof_ehdr; | |
5558 | ||
c044fabd | 5559 | /* No program header, for now. */ |
252b5132 RH |
5560 | i_ehdrp->e_phoff = 0; |
5561 | i_ehdrp->e_phentsize = 0; | |
5562 | i_ehdrp->e_phnum = 0; | |
5563 | ||
c044fabd | 5564 | /* Each bfd section is section header entry. */ |
252b5132 RH |
5565 | i_ehdrp->e_entry = bfd_get_start_address (abfd); |
5566 | i_ehdrp->e_shentsize = bed->s->sizeof_shdr; | |
5567 | ||
c044fabd | 5568 | /* If we're building an executable, we'll need a program header table. */ |
252b5132 | 5569 | if (abfd->flags & EXEC_P) |
0e71e495 BE |
5570 | /* It all happens later. */ |
5571 | ; | |
252b5132 RH |
5572 | else |
5573 | { | |
5574 | i_ehdrp->e_phentsize = 0; | |
252b5132 RH |
5575 | i_ehdrp->e_phoff = 0; |
5576 | } | |
5577 | ||
5578 | elf_tdata (abfd)->symtab_hdr.sh_name = | |
b34976b6 | 5579 | (unsigned int) _bfd_elf_strtab_add (shstrtab, ".symtab", FALSE); |
252b5132 | 5580 | elf_tdata (abfd)->strtab_hdr.sh_name = |
b34976b6 | 5581 | (unsigned int) _bfd_elf_strtab_add (shstrtab, ".strtab", FALSE); |
252b5132 | 5582 | elf_tdata (abfd)->shstrtab_hdr.sh_name = |
b34976b6 | 5583 | (unsigned int) _bfd_elf_strtab_add (shstrtab, ".shstrtab", FALSE); |
252b5132 | 5584 | if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1 |
17ca87fc | 5585 | || elf_tdata (abfd)->strtab_hdr.sh_name == (unsigned int) -1 |
252b5132 | 5586 | || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1) |
b34976b6 | 5587 | return FALSE; |
252b5132 | 5588 | |
b34976b6 | 5589 | return TRUE; |
252b5132 RH |
5590 | } |
5591 | ||
5592 | /* Assign file positions for all the reloc sections which are not part | |
a485e98e | 5593 | of the loadable file image, and the file position of section headers. */ |
252b5132 | 5594 | |
0ce398f1 L |
5595 | static bfd_boolean |
5596 | _bfd_elf_assign_file_positions_for_non_load (bfd *abfd) | |
252b5132 RH |
5597 | { |
5598 | file_ptr off; | |
e06efbf1 | 5599 | Elf_Internal_Shdr **shdrpp, **end_shdrpp; |
a485e98e AM |
5600 | Elf_Internal_Ehdr *i_ehdrp; |
5601 | const struct elf_backend_data *bed; | |
252b5132 | 5602 | |
12bd6957 | 5603 | off = elf_next_file_pos (abfd); |
252b5132 | 5604 | |
e06efbf1 L |
5605 | shdrpp = elf_elfsections (abfd); |
5606 | end_shdrpp = shdrpp + elf_numsections (abfd); | |
5607 | for (shdrpp++; shdrpp < end_shdrpp; shdrpp++) | |
252b5132 RH |
5608 | { |
5609 | Elf_Internal_Shdr *shdrp; | |
5610 | ||
5611 | shdrp = *shdrpp; | |
0ce398f1 L |
5612 | if (shdrp->sh_offset == -1) |
5613 | { | |
5614 | bfd_boolean is_rel = (shdrp->sh_type == SHT_REL | |
5615 | || shdrp->sh_type == SHT_RELA); | |
5616 | if (is_rel | |
5617 | || (shdrp->bfd_section != NULL | |
5618 | && (shdrp->bfd_section->flags & SEC_ELF_COMPRESS))) | |
5619 | { | |
5620 | if (!is_rel) | |
5621 | { | |
5622 | /* Compress DWARF debug sections. */ | |
5623 | if (!bfd_compress_section (abfd, shdrp->bfd_section, | |
5624 | shdrp->contents)) | |
5625 | return FALSE; | |
5626 | /* Update section size and contents. */ | |
5627 | shdrp->sh_size = shdrp->bfd_section->size; | |
5628 | shdrp->contents = shdrp->bfd_section->contents; | |
5629 | shdrp->bfd_section->contents = NULL; | |
5630 | } | |
5631 | off = _bfd_elf_assign_file_position_for_section (shdrp, | |
5632 | off, | |
5633 | TRUE); | |
5634 | } | |
5635 | } | |
252b5132 RH |
5636 | } |
5637 | ||
a485e98e AM |
5638 | /* Place the section headers. */ |
5639 | i_ehdrp = elf_elfheader (abfd); | |
5640 | bed = get_elf_backend_data (abfd); | |
5641 | off = align_file_position (off, 1 << bed->s->log_file_align); | |
5642 | i_ehdrp->e_shoff = off; | |
5643 | off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize; | |
12bd6957 | 5644 | elf_next_file_pos (abfd) = off; |
0ce398f1 L |
5645 | |
5646 | return TRUE; | |
252b5132 RH |
5647 | } |
5648 | ||
b34976b6 | 5649 | bfd_boolean |
217aa764 | 5650 | _bfd_elf_write_object_contents (bfd *abfd) |
252b5132 | 5651 | { |
9c5bfbb7 | 5652 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
252b5132 | 5653 | Elf_Internal_Shdr **i_shdrp; |
b34976b6 | 5654 | bfd_boolean failed; |
9ad5cbcf | 5655 | unsigned int count, num_sec; |
30e8ee25 | 5656 | struct elf_obj_tdata *t; |
252b5132 RH |
5657 | |
5658 | if (! abfd->output_has_begun | |
217aa764 | 5659 | && ! _bfd_elf_compute_section_file_positions (abfd, NULL)) |
b34976b6 | 5660 | return FALSE; |
252b5132 RH |
5661 | |
5662 | i_shdrp = elf_elfsections (abfd); | |
252b5132 | 5663 | |
b34976b6 | 5664 | failed = FALSE; |
252b5132 RH |
5665 | bfd_map_over_sections (abfd, bed->s->write_relocs, &failed); |
5666 | if (failed) | |
b34976b6 | 5667 | return FALSE; |
252b5132 | 5668 | |
0ce398f1 L |
5669 | if (!_bfd_elf_assign_file_positions_for_non_load (abfd)) |
5670 | return FALSE; | |
252b5132 | 5671 | |
c044fabd | 5672 | /* After writing the headers, we need to write the sections too... */ |
9ad5cbcf AM |
5673 | num_sec = elf_numsections (abfd); |
5674 | for (count = 1; count < num_sec; count++) | |
252b5132 RH |
5675 | { |
5676 | if (bed->elf_backend_section_processing) | |
5677 | (*bed->elf_backend_section_processing) (abfd, i_shdrp[count]); | |
5678 | if (i_shdrp[count]->contents) | |
5679 | { | |
dc810e39 AM |
5680 | bfd_size_type amt = i_shdrp[count]->sh_size; |
5681 | ||
252b5132 | 5682 | if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0 |
dc810e39 | 5683 | || bfd_bwrite (i_shdrp[count]->contents, amt, abfd) != amt) |
b34976b6 | 5684 | return FALSE; |
252b5132 RH |
5685 | } |
5686 | } | |
5687 | ||
5688 | /* Write out the section header names. */ | |
30e8ee25 | 5689 | t = elf_tdata (abfd); |
26ae6d5e | 5690 | if (elf_shstrtab (abfd) != NULL |
30e8ee25 | 5691 | && (bfd_seek (abfd, t->shstrtab_hdr.sh_offset, SEEK_SET) != 0 |
08a40648 | 5692 | || !_bfd_elf_strtab_emit (abfd, elf_shstrtab (abfd)))) |
b34976b6 | 5693 | return FALSE; |
252b5132 RH |
5694 | |
5695 | if (bed->elf_backend_final_write_processing) | |
12bd6957 | 5696 | (*bed->elf_backend_final_write_processing) (abfd, elf_linker (abfd)); |
252b5132 | 5697 | |
ff59fc36 RM |
5698 | if (!bed->s->write_shdrs_and_ehdr (abfd)) |
5699 | return FALSE; | |
5700 | ||
5701 | /* This is last since write_shdrs_and_ehdr can touch i_shdrp[0]. */ | |
c0355132 AM |
5702 | if (t->o->build_id.after_write_object_contents != NULL) |
5703 | return (*t->o->build_id.after_write_object_contents) (abfd); | |
ff59fc36 RM |
5704 | |
5705 | return TRUE; | |
252b5132 RH |
5706 | } |
5707 | ||
b34976b6 | 5708 | bfd_boolean |
217aa764 | 5709 | _bfd_elf_write_corefile_contents (bfd *abfd) |
252b5132 | 5710 | { |
c044fabd | 5711 | /* Hopefully this can be done just like an object file. */ |
252b5132 RH |
5712 | return _bfd_elf_write_object_contents (abfd); |
5713 | } | |
c044fabd KH |
5714 | |
5715 | /* Given a section, search the header to find them. */ | |
5716 | ||
cb33740c | 5717 | unsigned int |
198beae2 | 5718 | _bfd_elf_section_from_bfd_section (bfd *abfd, struct bfd_section *asect) |
252b5132 | 5719 | { |
9c5bfbb7 | 5720 | const struct elf_backend_data *bed; |
91d6fa6a | 5721 | unsigned int sec_index; |
252b5132 | 5722 | |
9ad5cbcf AM |
5723 | if (elf_section_data (asect) != NULL |
5724 | && elf_section_data (asect)->this_idx != 0) | |
5725 | return elf_section_data (asect)->this_idx; | |
5726 | ||
5727 | if (bfd_is_abs_section (asect)) | |
91d6fa6a | 5728 | sec_index = SHN_ABS; |
af746e92 | 5729 | else if (bfd_is_com_section (asect)) |
91d6fa6a | 5730 | sec_index = SHN_COMMON; |
af746e92 | 5731 | else if (bfd_is_und_section (asect)) |
91d6fa6a | 5732 | sec_index = SHN_UNDEF; |
af746e92 | 5733 | else |
91d6fa6a | 5734 | sec_index = SHN_BAD; |
252b5132 | 5735 | |
af746e92 | 5736 | bed = get_elf_backend_data (abfd); |
252b5132 RH |
5737 | if (bed->elf_backend_section_from_bfd_section) |
5738 | { | |
91d6fa6a | 5739 | int retval = sec_index; |
9ad5cbcf | 5740 | |
af746e92 AM |
5741 | if ((*bed->elf_backend_section_from_bfd_section) (abfd, asect, &retval)) |
5742 | return retval; | |
252b5132 RH |
5743 | } |
5744 | ||
91d6fa6a | 5745 | if (sec_index == SHN_BAD) |
af746e92 | 5746 | bfd_set_error (bfd_error_nonrepresentable_section); |
252b5132 | 5747 | |
91d6fa6a | 5748 | return sec_index; |
252b5132 RH |
5749 | } |
5750 | ||
5751 | /* Given a BFD symbol, return the index in the ELF symbol table, or -1 | |
5752 | on error. */ | |
5753 | ||
5754 | int | |
217aa764 | 5755 | _bfd_elf_symbol_from_bfd_symbol (bfd *abfd, asymbol **asym_ptr_ptr) |
252b5132 RH |
5756 | { |
5757 | asymbol *asym_ptr = *asym_ptr_ptr; | |
5758 | int idx; | |
5759 | flagword flags = asym_ptr->flags; | |
5760 | ||
5761 | /* When gas creates relocations against local labels, it creates its | |
5762 | own symbol for the section, but does put the symbol into the | |
5763 | symbol chain, so udata is 0. When the linker is generating | |
5764 | relocatable output, this section symbol may be for one of the | |
5765 | input sections rather than the output section. */ | |
5766 | if (asym_ptr->udata.i == 0 | |
5767 | && (flags & BSF_SECTION_SYM) | |
5768 | && asym_ptr->section) | |
5769 | { | |
5372391b | 5770 | asection *sec; |
252b5132 RH |
5771 | int indx; |
5772 | ||
5372391b AM |
5773 | sec = asym_ptr->section; |
5774 | if (sec->owner != abfd && sec->output_section != NULL) | |
5775 | sec = sec->output_section; | |
5776 | if (sec->owner == abfd | |
5777 | && (indx = sec->index) < elf_num_section_syms (abfd) | |
4e89ac30 | 5778 | && elf_section_syms (abfd)[indx] != NULL) |
252b5132 RH |
5779 | asym_ptr->udata.i = elf_section_syms (abfd)[indx]->udata.i; |
5780 | } | |
5781 | ||
5782 | idx = asym_ptr->udata.i; | |
5783 | ||
5784 | if (idx == 0) | |
5785 | { | |
5786 | /* This case can occur when using --strip-symbol on a symbol | |
08a40648 | 5787 | which is used in a relocation entry. */ |
252b5132 | 5788 | (*_bfd_error_handler) |
d003868e AM |
5789 | (_("%B: symbol `%s' required but not present"), |
5790 | abfd, bfd_asymbol_name (asym_ptr)); | |
252b5132 RH |
5791 | bfd_set_error (bfd_error_no_symbols); |
5792 | return -1; | |
5793 | } | |
5794 | ||
5795 | #if DEBUG & 4 | |
5796 | { | |
5797 | fprintf (stderr, | |
9ccb8af9 AM |
5798 | "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8lx\n", |
5799 | (long) asym_ptr, asym_ptr->name, idx, (long) flags); | |
252b5132 RH |
5800 | fflush (stderr); |
5801 | } | |
5802 | #endif | |
5803 | ||
5804 | return idx; | |
5805 | } | |
5806 | ||
84d1d650 | 5807 | /* Rewrite program header information. */ |
252b5132 | 5808 | |
b34976b6 | 5809 | static bfd_boolean |
84d1d650 | 5810 | rewrite_elf_program_header (bfd *ibfd, bfd *obfd) |
252b5132 | 5811 | { |
b34976b6 AM |
5812 | Elf_Internal_Ehdr *iehdr; |
5813 | struct elf_segment_map *map; | |
5814 | struct elf_segment_map *map_first; | |
5815 | struct elf_segment_map **pointer_to_map; | |
5816 | Elf_Internal_Phdr *segment; | |
5817 | asection *section; | |
5818 | unsigned int i; | |
5819 | unsigned int num_segments; | |
5820 | bfd_boolean phdr_included = FALSE; | |
5c44b38e | 5821 | bfd_boolean p_paddr_valid; |
b34976b6 AM |
5822 | bfd_vma maxpagesize; |
5823 | struct elf_segment_map *phdr_adjust_seg = NULL; | |
5824 | unsigned int phdr_adjust_num = 0; | |
9c5bfbb7 | 5825 | const struct elf_backend_data *bed; |
bc67d8a6 | 5826 | |
caf47ea6 | 5827 | bed = get_elf_backend_data (ibfd); |
252b5132 RH |
5828 | iehdr = elf_elfheader (ibfd); |
5829 | ||
bc67d8a6 | 5830 | map_first = NULL; |
c044fabd | 5831 | pointer_to_map = &map_first; |
252b5132 RH |
5832 | |
5833 | num_segments = elf_elfheader (ibfd)->e_phnum; | |
bc67d8a6 NC |
5834 | maxpagesize = get_elf_backend_data (obfd)->maxpagesize; |
5835 | ||
5836 | /* Returns the end address of the segment + 1. */ | |
aecc8f8a AM |
5837 | #define SEGMENT_END(segment, start) \ |
5838 | (start + (segment->p_memsz > segment->p_filesz \ | |
5839 | ? segment->p_memsz : segment->p_filesz)) | |
bc67d8a6 | 5840 | |
eecdbe52 JJ |
5841 | #define SECTION_SIZE(section, segment) \ |
5842 | (((section->flags & (SEC_HAS_CONTENTS | SEC_THREAD_LOCAL)) \ | |
5843 | != SEC_THREAD_LOCAL || segment->p_type == PT_TLS) \ | |
eea6121a | 5844 | ? section->size : 0) |
eecdbe52 | 5845 | |
b34976b6 | 5846 | /* Returns TRUE if the given section is contained within |
bc67d8a6 | 5847 | the given segment. VMA addresses are compared. */ |
aecc8f8a AM |
5848 | #define IS_CONTAINED_BY_VMA(section, segment) \ |
5849 | (section->vma >= segment->p_vaddr \ | |
eecdbe52 | 5850 | && (section->vma + SECTION_SIZE (section, segment) \ |
aecc8f8a | 5851 | <= (SEGMENT_END (segment, segment->p_vaddr)))) |
c044fabd | 5852 | |
b34976b6 | 5853 | /* Returns TRUE if the given section is contained within |
bc67d8a6 | 5854 | the given segment. LMA addresses are compared. */ |
aecc8f8a AM |
5855 | #define IS_CONTAINED_BY_LMA(section, segment, base) \ |
5856 | (section->lma >= base \ | |
eecdbe52 | 5857 | && (section->lma + SECTION_SIZE (section, segment) \ |
aecc8f8a | 5858 | <= SEGMENT_END (segment, base))) |
252b5132 | 5859 | |
0efc80c8 L |
5860 | /* Handle PT_NOTE segment. */ |
5861 | #define IS_NOTE(p, s) \ | |
aecc8f8a | 5862 | (p->p_type == PT_NOTE \ |
0efc80c8 | 5863 | && elf_section_type (s) == SHT_NOTE \ |
aecc8f8a | 5864 | && (bfd_vma) s->filepos >= p->p_offset \ |
cb3ff1e5 | 5865 | && ((bfd_vma) s->filepos + s->size \ |
aecc8f8a | 5866 | <= p->p_offset + p->p_filesz)) |
252b5132 | 5867 | |
0efc80c8 L |
5868 | /* Special case: corefile "NOTE" section containing regs, prpsinfo |
5869 | etc. */ | |
5870 | #define IS_COREFILE_NOTE(p, s) \ | |
5871 | (IS_NOTE (p, s) \ | |
5872 | && bfd_get_format (ibfd) == bfd_core \ | |
5873 | && s->vma == 0 \ | |
5874 | && s->lma == 0) | |
5875 | ||
252b5132 RH |
5876 | /* The complicated case when p_vaddr is 0 is to handle the Solaris |
5877 | linker, which generates a PT_INTERP section with p_vaddr and | |
5878 | p_memsz set to 0. */ | |
aecc8f8a AM |
5879 | #define IS_SOLARIS_PT_INTERP(p, s) \ |
5880 | (p->p_vaddr == 0 \ | |
5881 | && p->p_paddr == 0 \ | |
5882 | && p->p_memsz == 0 \ | |
5883 | && p->p_filesz > 0 \ | |
5884 | && (s->flags & SEC_HAS_CONTENTS) != 0 \ | |
eea6121a | 5885 | && s->size > 0 \ |
aecc8f8a | 5886 | && (bfd_vma) s->filepos >= p->p_offset \ |
cb3ff1e5 | 5887 | && ((bfd_vma) s->filepos + s->size \ |
aecc8f8a | 5888 | <= p->p_offset + p->p_filesz)) |
5c440b1e | 5889 | |
bc67d8a6 NC |
5890 | /* Decide if the given section should be included in the given segment. |
5891 | A section will be included if: | |
f5ffc919 | 5892 | 1. It is within the address space of the segment -- we use the LMA |
08a40648 | 5893 | if that is set for the segment and the VMA otherwise, |
0efc80c8 | 5894 | 2. It is an allocated section or a NOTE section in a PT_NOTE |
d324f6d6 | 5895 | segment. |
bc67d8a6 | 5896 | 3. There is an output section associated with it, |
eecdbe52 | 5897 | 4. The section has not already been allocated to a previous segment. |
2b05f1b7 | 5898 | 5. PT_GNU_STACK segments do not include any sections. |
03394ac9 | 5899 | 6. PT_TLS segment includes only SHF_TLS sections. |
6f79b219 JJ |
5900 | 7. SHF_TLS sections are only in PT_TLS or PT_LOAD segments. |
5901 | 8. PT_DYNAMIC should not contain empty sections at the beginning | |
08a40648 | 5902 | (with the possible exception of .dynamic). */ |
9f17e2a6 | 5903 | #define IS_SECTION_IN_INPUT_SEGMENT(section, segment, bed) \ |
2b05f1b7 L |
5904 | ((((segment->p_paddr \ |
5905 | ? IS_CONTAINED_BY_LMA (section, segment, segment->p_paddr) \ | |
5906 | : IS_CONTAINED_BY_VMA (section, segment)) \ | |
5907 | && (section->flags & SEC_ALLOC) != 0) \ | |
0efc80c8 | 5908 | || IS_NOTE (segment, section)) \ |
2b05f1b7 L |
5909 | && segment->p_type != PT_GNU_STACK \ |
5910 | && (segment->p_type != PT_TLS \ | |
5911 | || (section->flags & SEC_THREAD_LOCAL)) \ | |
5912 | && (segment->p_type == PT_LOAD \ | |
5913 | || segment->p_type == PT_TLS \ | |
5914 | || (section->flags & SEC_THREAD_LOCAL) == 0) \ | |
5915 | && (segment->p_type != PT_DYNAMIC \ | |
5916 | || SECTION_SIZE (section, segment) > 0 \ | |
5917 | || (segment->p_paddr \ | |
5918 | ? segment->p_paddr != section->lma \ | |
5919 | : segment->p_vaddr != section->vma) \ | |
5920 | || (strcmp (bfd_get_section_name (ibfd, section), ".dynamic") \ | |
5921 | == 0)) \ | |
0067a569 | 5922 | && !section->segment_mark) |
bc67d8a6 | 5923 | |
9f17e2a6 L |
5924 | /* If the output section of a section in the input segment is NULL, |
5925 | it is removed from the corresponding output segment. */ | |
5926 | #define INCLUDE_SECTION_IN_SEGMENT(section, segment, bed) \ | |
5927 | (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed) \ | |
5928 | && section->output_section != NULL) | |
5929 | ||
b34976b6 | 5930 | /* Returns TRUE iff seg1 starts after the end of seg2. */ |
b5f852ea NC |
5931 | #define SEGMENT_AFTER_SEGMENT(seg1, seg2, field) \ |
5932 | (seg1->field >= SEGMENT_END (seg2, seg2->field)) | |
5933 | ||
5934 | /* Returns TRUE iff seg1 and seg2 overlap. Segments overlap iff both | |
5935 | their VMA address ranges and their LMA address ranges overlap. | |
5936 | It is possible to have overlapping VMA ranges without overlapping LMA | |
5937 | ranges. RedBoot images for example can have both .data and .bss mapped | |
5938 | to the same VMA range, but with the .data section mapped to a different | |
5939 | LMA. */ | |
aecc8f8a | 5940 | #define SEGMENT_OVERLAPS(seg1, seg2) \ |
b5f852ea | 5941 | ( !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_vaddr) \ |
08a40648 | 5942 | || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_vaddr)) \ |
b5f852ea | 5943 | && !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_paddr) \ |
08a40648 | 5944 | || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_paddr))) |
bc67d8a6 NC |
5945 | |
5946 | /* Initialise the segment mark field. */ | |
5947 | for (section = ibfd->sections; section != NULL; section = section->next) | |
b34976b6 | 5948 | section->segment_mark = FALSE; |
bc67d8a6 | 5949 | |
5c44b38e AM |
5950 | /* The Solaris linker creates program headers in which all the |
5951 | p_paddr fields are zero. When we try to objcopy or strip such a | |
5952 | file, we get confused. Check for this case, and if we find it | |
5953 | don't set the p_paddr_valid fields. */ | |
5954 | p_paddr_valid = FALSE; | |
5955 | for (i = 0, segment = elf_tdata (ibfd)->phdr; | |
5956 | i < num_segments; | |
5957 | i++, segment++) | |
5958 | if (segment->p_paddr != 0) | |
5959 | { | |
5960 | p_paddr_valid = TRUE; | |
5961 | break; | |
5962 | } | |
5963 | ||
252b5132 | 5964 | /* Scan through the segments specified in the program header |
bc67d8a6 | 5965 | of the input BFD. For this first scan we look for overlaps |
9ad5cbcf | 5966 | in the loadable segments. These can be created by weird |
aecc8f8a | 5967 | parameters to objcopy. Also, fix some solaris weirdness. */ |
bc67d8a6 NC |
5968 | for (i = 0, segment = elf_tdata (ibfd)->phdr; |
5969 | i < num_segments; | |
c044fabd | 5970 | i++, segment++) |
252b5132 | 5971 | { |
252b5132 | 5972 | unsigned int j; |
c044fabd | 5973 | Elf_Internal_Phdr *segment2; |
252b5132 | 5974 | |
aecc8f8a AM |
5975 | if (segment->p_type == PT_INTERP) |
5976 | for (section = ibfd->sections; section; section = section->next) | |
5977 | if (IS_SOLARIS_PT_INTERP (segment, section)) | |
5978 | { | |
5979 | /* Mininal change so that the normal section to segment | |
4cc11e76 | 5980 | assignment code will work. */ |
aecc8f8a AM |
5981 | segment->p_vaddr = section->vma; |
5982 | break; | |
5983 | } | |
5984 | ||
bc67d8a6 | 5985 | if (segment->p_type != PT_LOAD) |
b10a8ae0 L |
5986 | { |
5987 | /* Remove PT_GNU_RELRO segment. */ | |
5988 | if (segment->p_type == PT_GNU_RELRO) | |
5989 | segment->p_type = PT_NULL; | |
5990 | continue; | |
5991 | } | |
c044fabd | 5992 | |
bc67d8a6 | 5993 | /* Determine if this segment overlaps any previous segments. */ |
0067a569 | 5994 | for (j = 0, segment2 = elf_tdata (ibfd)->phdr; j < i; j++, segment2++) |
bc67d8a6 NC |
5995 | { |
5996 | bfd_signed_vma extra_length; | |
c044fabd | 5997 | |
bc67d8a6 | 5998 | if (segment2->p_type != PT_LOAD |
0067a569 | 5999 | || !SEGMENT_OVERLAPS (segment, segment2)) |
bc67d8a6 | 6000 | continue; |
c044fabd | 6001 | |
bc67d8a6 NC |
6002 | /* Merge the two segments together. */ |
6003 | if (segment2->p_vaddr < segment->p_vaddr) | |
6004 | { | |
c044fabd | 6005 | /* Extend SEGMENT2 to include SEGMENT and then delete |
08a40648 | 6006 | SEGMENT. */ |
0067a569 AM |
6007 | extra_length = (SEGMENT_END (segment, segment->p_vaddr) |
6008 | - SEGMENT_END (segment2, segment2->p_vaddr)); | |
c044fabd | 6009 | |
bc67d8a6 NC |
6010 | if (extra_length > 0) |
6011 | { | |
0067a569 | 6012 | segment2->p_memsz += extra_length; |
bc67d8a6 NC |
6013 | segment2->p_filesz += extra_length; |
6014 | } | |
c044fabd | 6015 | |
bc67d8a6 | 6016 | segment->p_type = PT_NULL; |
c044fabd | 6017 | |
bc67d8a6 NC |
6018 | /* Since we have deleted P we must restart the outer loop. */ |
6019 | i = 0; | |
6020 | segment = elf_tdata (ibfd)->phdr; | |
6021 | break; | |
6022 | } | |
6023 | else | |
6024 | { | |
c044fabd | 6025 | /* Extend SEGMENT to include SEGMENT2 and then delete |
08a40648 | 6026 | SEGMENT2. */ |
0067a569 AM |
6027 | extra_length = (SEGMENT_END (segment2, segment2->p_vaddr) |
6028 | - SEGMENT_END (segment, segment->p_vaddr)); | |
c044fabd | 6029 | |
bc67d8a6 NC |
6030 | if (extra_length > 0) |
6031 | { | |
0067a569 | 6032 | segment->p_memsz += extra_length; |
bc67d8a6 NC |
6033 | segment->p_filesz += extra_length; |
6034 | } | |
c044fabd | 6035 | |
bc67d8a6 NC |
6036 | segment2->p_type = PT_NULL; |
6037 | } | |
6038 | } | |
6039 | } | |
c044fabd | 6040 | |
bc67d8a6 NC |
6041 | /* The second scan attempts to assign sections to segments. */ |
6042 | for (i = 0, segment = elf_tdata (ibfd)->phdr; | |
6043 | i < num_segments; | |
0067a569 | 6044 | i++, segment++) |
bc67d8a6 | 6045 | { |
0067a569 AM |
6046 | unsigned int section_count; |
6047 | asection **sections; | |
6048 | asection *output_section; | |
6049 | unsigned int isec; | |
6050 | bfd_vma matching_lma; | |
6051 | bfd_vma suggested_lma; | |
6052 | unsigned int j; | |
dc810e39 | 6053 | bfd_size_type amt; |
0067a569 AM |
6054 | asection *first_section; |
6055 | bfd_boolean first_matching_lma; | |
6056 | bfd_boolean first_suggested_lma; | |
bc67d8a6 NC |
6057 | |
6058 | if (segment->p_type == PT_NULL) | |
6059 | continue; | |
c044fabd | 6060 | |
9f17e2a6 | 6061 | first_section = NULL; |
bc67d8a6 | 6062 | /* Compute how many sections might be placed into this segment. */ |
b5f852ea NC |
6063 | for (section = ibfd->sections, section_count = 0; |
6064 | section != NULL; | |
6065 | section = section->next) | |
9f17e2a6 L |
6066 | { |
6067 | /* Find the first section in the input segment, which may be | |
6068 | removed from the corresponding output segment. */ | |
6069 | if (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed)) | |
6070 | { | |
6071 | if (first_section == NULL) | |
6072 | first_section = section; | |
6073 | if (section->output_section != NULL) | |
6074 | ++section_count; | |
6075 | } | |
6076 | } | |
811072d8 | 6077 | |
b5f852ea NC |
6078 | /* Allocate a segment map big enough to contain |
6079 | all of the sections we have selected. */ | |
dc810e39 AM |
6080 | amt = sizeof (struct elf_segment_map); |
6081 | amt += ((bfd_size_type) section_count - 1) * sizeof (asection *); | |
a50b1753 | 6082 | map = (struct elf_segment_map *) bfd_zalloc (obfd, amt); |
bc67d8a6 | 6083 | if (map == NULL) |
b34976b6 | 6084 | return FALSE; |
252b5132 RH |
6085 | |
6086 | /* Initialise the fields of the segment map. Default to | |
6087 | using the physical address of the segment in the input BFD. */ | |
0067a569 AM |
6088 | map->next = NULL; |
6089 | map->p_type = segment->p_type; | |
6090 | map->p_flags = segment->p_flags; | |
bc67d8a6 | 6091 | map->p_flags_valid = 1; |
55d55ac7 | 6092 | |
9f17e2a6 L |
6093 | /* If the first section in the input segment is removed, there is |
6094 | no need to preserve segment physical address in the corresponding | |
6095 | output segment. */ | |
945c025a | 6096 | if (!first_section || first_section->output_section != NULL) |
9f17e2a6 L |
6097 | { |
6098 | map->p_paddr = segment->p_paddr; | |
5c44b38e | 6099 | map->p_paddr_valid = p_paddr_valid; |
9f17e2a6 | 6100 | } |
252b5132 RH |
6101 | |
6102 | /* Determine if this segment contains the ELF file header | |
6103 | and if it contains the program headers themselves. */ | |
bc67d8a6 NC |
6104 | map->includes_filehdr = (segment->p_offset == 0 |
6105 | && segment->p_filesz >= iehdr->e_ehsize); | |
bc67d8a6 | 6106 | map->includes_phdrs = 0; |
252b5132 | 6107 | |
0067a569 | 6108 | if (!phdr_included || segment->p_type != PT_LOAD) |
252b5132 | 6109 | { |
bc67d8a6 NC |
6110 | map->includes_phdrs = |
6111 | (segment->p_offset <= (bfd_vma) iehdr->e_phoff | |
6112 | && (segment->p_offset + segment->p_filesz | |
252b5132 RH |
6113 | >= ((bfd_vma) iehdr->e_phoff |
6114 | + iehdr->e_phnum * iehdr->e_phentsize))); | |
c044fabd | 6115 | |
bc67d8a6 | 6116 | if (segment->p_type == PT_LOAD && map->includes_phdrs) |
b34976b6 | 6117 | phdr_included = TRUE; |
252b5132 RH |
6118 | } |
6119 | ||
bc67d8a6 | 6120 | if (section_count == 0) |
252b5132 RH |
6121 | { |
6122 | /* Special segments, such as the PT_PHDR segment, may contain | |
6123 | no sections, but ordinary, loadable segments should contain | |
1ed89aa9 NC |
6124 | something. They are allowed by the ELF spec however, so only |
6125 | a warning is produced. */ | |
bc67d8a6 | 6126 | if (segment->p_type == PT_LOAD) |
c86934ce NC |
6127 | (*_bfd_error_handler) (_("\ |
6128 | %B: warning: Empty loadable segment detected, is this intentional ?"), | |
0067a569 | 6129 | ibfd); |
252b5132 | 6130 | |
bc67d8a6 | 6131 | map->count = 0; |
c044fabd KH |
6132 | *pointer_to_map = map; |
6133 | pointer_to_map = &map->next; | |
252b5132 RH |
6134 | |
6135 | continue; | |
6136 | } | |
6137 | ||
6138 | /* Now scan the sections in the input BFD again and attempt | |
6139 | to add their corresponding output sections to the segment map. | |
6140 | The problem here is how to handle an output section which has | |
6141 | been moved (ie had its LMA changed). There are four possibilities: | |
6142 | ||
6143 | 1. None of the sections have been moved. | |
6144 | In this case we can continue to use the segment LMA from the | |
6145 | input BFD. | |
6146 | ||
6147 | 2. All of the sections have been moved by the same amount. | |
6148 | In this case we can change the segment's LMA to match the LMA | |
6149 | of the first section. | |
6150 | ||
6151 | 3. Some of the sections have been moved, others have not. | |
6152 | In this case those sections which have not been moved can be | |
6153 | placed in the current segment which will have to have its size, | |
6154 | and possibly its LMA changed, and a new segment or segments will | |
6155 | have to be created to contain the other sections. | |
6156 | ||
b5f852ea | 6157 | 4. The sections have been moved, but not by the same amount. |
252b5132 RH |
6158 | In this case we can change the segment's LMA to match the LMA |
6159 | of the first section and we will have to create a new segment | |
6160 | or segments to contain the other sections. | |
6161 | ||
6162 | In order to save time, we allocate an array to hold the section | |
6163 | pointers that we are interested in. As these sections get assigned | |
6164 | to a segment, they are removed from this array. */ | |
6165 | ||
a50b1753 | 6166 | sections = (asection **) bfd_malloc2 (section_count, sizeof (asection *)); |
252b5132 | 6167 | if (sections == NULL) |
b34976b6 | 6168 | return FALSE; |
252b5132 RH |
6169 | |
6170 | /* Step One: Scan for segment vs section LMA conflicts. | |
6171 | Also add the sections to the section array allocated above. | |
6172 | Also add the sections to the current segment. In the common | |
6173 | case, where the sections have not been moved, this means that | |
6174 | we have completely filled the segment, and there is nothing | |
6175 | more to do. */ | |
252b5132 | 6176 | isec = 0; |
72730e0c | 6177 | matching_lma = 0; |
252b5132 | 6178 | suggested_lma = 0; |
0067a569 AM |
6179 | first_matching_lma = TRUE; |
6180 | first_suggested_lma = TRUE; | |
252b5132 | 6181 | |
147d51c2 | 6182 | for (section = ibfd->sections; |
bc67d8a6 NC |
6183 | section != NULL; |
6184 | section = section->next) | |
147d51c2 L |
6185 | if (section == first_section) |
6186 | break; | |
6187 | ||
6188 | for (j = 0; section != NULL; section = section->next) | |
252b5132 | 6189 | { |
caf47ea6 | 6190 | if (INCLUDE_SECTION_IN_SEGMENT (section, segment, bed)) |
c0f7859b | 6191 | { |
bc67d8a6 NC |
6192 | output_section = section->output_section; |
6193 | ||
0067a569 | 6194 | sections[j++] = section; |
252b5132 RH |
6195 | |
6196 | /* The Solaris native linker always sets p_paddr to 0. | |
6197 | We try to catch that case here, and set it to the | |
5e8d7549 NC |
6198 | correct value. Note - some backends require that |
6199 | p_paddr be left as zero. */ | |
5c44b38e | 6200 | if (!p_paddr_valid |
4455705d | 6201 | && segment->p_vaddr != 0 |
0067a569 | 6202 | && !bed->want_p_paddr_set_to_zero |
252b5132 | 6203 | && isec == 0 |
bc67d8a6 | 6204 | && output_section->lma != 0 |
0067a569 AM |
6205 | && output_section->vma == (segment->p_vaddr |
6206 | + (map->includes_filehdr | |
6207 | ? iehdr->e_ehsize | |
6208 | : 0) | |
6209 | + (map->includes_phdrs | |
6210 | ? (iehdr->e_phnum | |
6211 | * iehdr->e_phentsize) | |
6212 | : 0))) | |
bc67d8a6 | 6213 | map->p_paddr = segment->p_vaddr; |
252b5132 RH |
6214 | |
6215 | /* Match up the physical address of the segment with the | |
6216 | LMA address of the output section. */ | |
bc67d8a6 | 6217 | if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr) |
5e8d7549 | 6218 | || IS_COREFILE_NOTE (segment, section) |
0067a569 AM |
6219 | || (bed->want_p_paddr_set_to_zero |
6220 | && IS_CONTAINED_BY_VMA (output_section, segment))) | |
252b5132 | 6221 | { |
0067a569 AM |
6222 | if (first_matching_lma || output_section->lma < matching_lma) |
6223 | { | |
6224 | matching_lma = output_section->lma; | |
6225 | first_matching_lma = FALSE; | |
6226 | } | |
252b5132 RH |
6227 | |
6228 | /* We assume that if the section fits within the segment | |
bc67d8a6 | 6229 | then it does not overlap any other section within that |
252b5132 | 6230 | segment. */ |
0067a569 AM |
6231 | map->sections[isec++] = output_section; |
6232 | } | |
6233 | else if (first_suggested_lma) | |
6234 | { | |
6235 | suggested_lma = output_section->lma; | |
6236 | first_suggested_lma = FALSE; | |
252b5132 | 6237 | } |
147d51c2 L |
6238 | |
6239 | if (j == section_count) | |
6240 | break; | |
252b5132 RH |
6241 | } |
6242 | } | |
6243 | ||
bc67d8a6 | 6244 | BFD_ASSERT (j == section_count); |
252b5132 RH |
6245 | |
6246 | /* Step Two: Adjust the physical address of the current segment, | |
6247 | if necessary. */ | |
bc67d8a6 | 6248 | if (isec == section_count) |
252b5132 RH |
6249 | { |
6250 | /* All of the sections fitted within the segment as currently | |
6251 | specified. This is the default case. Add the segment to | |
6252 | the list of built segments and carry on to process the next | |
6253 | program header in the input BFD. */ | |
bc67d8a6 | 6254 | map->count = section_count; |
c044fabd KH |
6255 | *pointer_to_map = map; |
6256 | pointer_to_map = &map->next; | |
08a40648 | 6257 | |
5c44b38e AM |
6258 | if (p_paddr_valid |
6259 | && !bed->want_p_paddr_set_to_zero | |
147d51c2 | 6260 | && matching_lma != map->p_paddr |
5c44b38e AM |
6261 | && !map->includes_filehdr |
6262 | && !map->includes_phdrs) | |
3271a814 NS |
6263 | /* There is some padding before the first section in the |
6264 | segment. So, we must account for that in the output | |
6265 | segment's vma. */ | |
6266 | map->p_vaddr_offset = matching_lma - map->p_paddr; | |
08a40648 | 6267 | |
252b5132 RH |
6268 | free (sections); |
6269 | continue; | |
6270 | } | |
252b5132 RH |
6271 | else |
6272 | { | |
0067a569 | 6273 | if (!first_matching_lma) |
72730e0c AM |
6274 | { |
6275 | /* At least one section fits inside the current segment. | |
6276 | Keep it, but modify its physical address to match the | |
6277 | LMA of the first section that fitted. */ | |
bc67d8a6 | 6278 | map->p_paddr = matching_lma; |
72730e0c AM |
6279 | } |
6280 | else | |
6281 | { | |
6282 | /* None of the sections fitted inside the current segment. | |
6283 | Change the current segment's physical address to match | |
6284 | the LMA of the first section. */ | |
bc67d8a6 | 6285 | map->p_paddr = suggested_lma; |
72730e0c AM |
6286 | } |
6287 | ||
bc67d8a6 NC |
6288 | /* Offset the segment physical address from the lma |
6289 | to allow for space taken up by elf headers. */ | |
6290 | if (map->includes_filehdr) | |
010c8431 AM |
6291 | { |
6292 | if (map->p_paddr >= iehdr->e_ehsize) | |
6293 | map->p_paddr -= iehdr->e_ehsize; | |
6294 | else | |
6295 | { | |
6296 | map->includes_filehdr = FALSE; | |
6297 | map->includes_phdrs = FALSE; | |
6298 | } | |
6299 | } | |
252b5132 | 6300 | |
bc67d8a6 NC |
6301 | if (map->includes_phdrs) |
6302 | { | |
010c8431 AM |
6303 | if (map->p_paddr >= iehdr->e_phnum * iehdr->e_phentsize) |
6304 | { | |
6305 | map->p_paddr -= iehdr->e_phnum * iehdr->e_phentsize; | |
6306 | ||
6307 | /* iehdr->e_phnum is just an estimate of the number | |
6308 | of program headers that we will need. Make a note | |
6309 | here of the number we used and the segment we chose | |
6310 | to hold these headers, so that we can adjust the | |
6311 | offset when we know the correct value. */ | |
6312 | phdr_adjust_num = iehdr->e_phnum; | |
6313 | phdr_adjust_seg = map; | |
6314 | } | |
6315 | else | |
6316 | map->includes_phdrs = FALSE; | |
bc67d8a6 | 6317 | } |
252b5132 RH |
6318 | } |
6319 | ||
6320 | /* Step Three: Loop over the sections again, this time assigning | |
caf47ea6 | 6321 | those that fit to the current segment and removing them from the |
252b5132 RH |
6322 | sections array; but making sure not to leave large gaps. Once all |
6323 | possible sections have been assigned to the current segment it is | |
6324 | added to the list of built segments and if sections still remain | |
6325 | to be assigned, a new segment is constructed before repeating | |
6326 | the loop. */ | |
6327 | isec = 0; | |
6328 | do | |
6329 | { | |
bc67d8a6 | 6330 | map->count = 0; |
252b5132 | 6331 | suggested_lma = 0; |
0067a569 | 6332 | first_suggested_lma = TRUE; |
252b5132 RH |
6333 | |
6334 | /* Fill the current segment with sections that fit. */ | |
bc67d8a6 | 6335 | for (j = 0; j < section_count; j++) |
252b5132 | 6336 | { |
bc67d8a6 | 6337 | section = sections[j]; |
252b5132 | 6338 | |
bc67d8a6 | 6339 | if (section == NULL) |
252b5132 RH |
6340 | continue; |
6341 | ||
bc67d8a6 | 6342 | output_section = section->output_section; |
252b5132 | 6343 | |
bc67d8a6 | 6344 | BFD_ASSERT (output_section != NULL); |
c044fabd | 6345 | |
bc67d8a6 NC |
6346 | if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr) |
6347 | || IS_COREFILE_NOTE (segment, section)) | |
252b5132 | 6348 | { |
bc67d8a6 | 6349 | if (map->count == 0) |
252b5132 RH |
6350 | { |
6351 | /* If the first section in a segment does not start at | |
bc67d8a6 NC |
6352 | the beginning of the segment, then something is |
6353 | wrong. */ | |
0067a569 AM |
6354 | if (output_section->lma |
6355 | != (map->p_paddr | |
6356 | + (map->includes_filehdr ? iehdr->e_ehsize : 0) | |
6357 | + (map->includes_phdrs | |
6358 | ? iehdr->e_phnum * iehdr->e_phentsize | |
6359 | : 0))) | |
252b5132 RH |
6360 | abort (); |
6361 | } | |
6362 | else | |
6363 | { | |
0067a569 | 6364 | asection *prev_sec; |
252b5132 | 6365 | |
bc67d8a6 | 6366 | prev_sec = map->sections[map->count - 1]; |
252b5132 RH |
6367 | |
6368 | /* If the gap between the end of the previous section | |
bc67d8a6 NC |
6369 | and the start of this section is more than |
6370 | maxpagesize then we need to start a new segment. */ | |
eea6121a | 6371 | if ((BFD_ALIGN (prev_sec->lma + prev_sec->size, |
079e9a2f | 6372 | maxpagesize) |
caf47ea6 | 6373 | < BFD_ALIGN (output_section->lma, maxpagesize)) |
0067a569 | 6374 | || (prev_sec->lma + prev_sec->size |
079e9a2f | 6375 | > output_section->lma)) |
252b5132 | 6376 | { |
0067a569 AM |
6377 | if (first_suggested_lma) |
6378 | { | |
6379 | suggested_lma = output_section->lma; | |
6380 | first_suggested_lma = FALSE; | |
6381 | } | |
252b5132 RH |
6382 | |
6383 | continue; | |
6384 | } | |
6385 | } | |
6386 | ||
bc67d8a6 | 6387 | map->sections[map->count++] = output_section; |
252b5132 RH |
6388 | ++isec; |
6389 | sections[j] = NULL; | |
b34976b6 | 6390 | section->segment_mark = TRUE; |
252b5132 | 6391 | } |
0067a569 AM |
6392 | else if (first_suggested_lma) |
6393 | { | |
6394 | suggested_lma = output_section->lma; | |
6395 | first_suggested_lma = FALSE; | |
6396 | } | |
252b5132 RH |
6397 | } |
6398 | ||
bc67d8a6 | 6399 | BFD_ASSERT (map->count > 0); |
252b5132 RH |
6400 | |
6401 | /* Add the current segment to the list of built segments. */ | |
c044fabd KH |
6402 | *pointer_to_map = map; |
6403 | pointer_to_map = &map->next; | |
252b5132 | 6404 | |
bc67d8a6 | 6405 | if (isec < section_count) |
252b5132 RH |
6406 | { |
6407 | /* We still have not allocated all of the sections to | |
6408 | segments. Create a new segment here, initialise it | |
6409 | and carry on looping. */ | |
dc810e39 AM |
6410 | amt = sizeof (struct elf_segment_map); |
6411 | amt += ((bfd_size_type) section_count - 1) * sizeof (asection *); | |
5964fc3a | 6412 | map = (struct elf_segment_map *) bfd_zalloc (obfd, amt); |
bc67d8a6 | 6413 | if (map == NULL) |
5ed6aba4 NC |
6414 | { |
6415 | free (sections); | |
6416 | return FALSE; | |
6417 | } | |
252b5132 RH |
6418 | |
6419 | /* Initialise the fields of the segment map. Set the physical | |
6420 | physical address to the LMA of the first section that has | |
6421 | not yet been assigned. */ | |
0067a569 AM |
6422 | map->next = NULL; |
6423 | map->p_type = segment->p_type; | |
6424 | map->p_flags = segment->p_flags; | |
6425 | map->p_flags_valid = 1; | |
6426 | map->p_paddr = suggested_lma; | |
5c44b38e | 6427 | map->p_paddr_valid = p_paddr_valid; |
bc67d8a6 | 6428 | map->includes_filehdr = 0; |
0067a569 | 6429 | map->includes_phdrs = 0; |
252b5132 RH |
6430 | } |
6431 | } | |
bc67d8a6 | 6432 | while (isec < section_count); |
252b5132 RH |
6433 | |
6434 | free (sections); | |
6435 | } | |
6436 | ||
12bd6957 | 6437 | elf_seg_map (obfd) = map_first; |
bc67d8a6 NC |
6438 | |
6439 | /* If we had to estimate the number of program headers that were | |
9ad5cbcf | 6440 | going to be needed, then check our estimate now and adjust |
bc67d8a6 NC |
6441 | the offset if necessary. */ |
6442 | if (phdr_adjust_seg != NULL) | |
6443 | { | |
6444 | unsigned int count; | |
c044fabd | 6445 | |
bc67d8a6 | 6446 | for (count = 0, map = map_first; map != NULL; map = map->next) |
c044fabd | 6447 | count++; |
252b5132 | 6448 | |
bc67d8a6 NC |
6449 | if (count > phdr_adjust_num) |
6450 | phdr_adjust_seg->p_paddr | |
6451 | -= (count - phdr_adjust_num) * iehdr->e_phentsize; | |
6452 | } | |
c044fabd | 6453 | |
bc67d8a6 | 6454 | #undef SEGMENT_END |
eecdbe52 | 6455 | #undef SECTION_SIZE |
bc67d8a6 NC |
6456 | #undef IS_CONTAINED_BY_VMA |
6457 | #undef IS_CONTAINED_BY_LMA | |
0efc80c8 | 6458 | #undef IS_NOTE |
252b5132 | 6459 | #undef IS_COREFILE_NOTE |
bc67d8a6 | 6460 | #undef IS_SOLARIS_PT_INTERP |
9f17e2a6 | 6461 | #undef IS_SECTION_IN_INPUT_SEGMENT |
bc67d8a6 NC |
6462 | #undef INCLUDE_SECTION_IN_SEGMENT |
6463 | #undef SEGMENT_AFTER_SEGMENT | |
6464 | #undef SEGMENT_OVERLAPS | |
b34976b6 | 6465 | return TRUE; |
252b5132 RH |
6466 | } |
6467 | ||
84d1d650 L |
6468 | /* Copy ELF program header information. */ |
6469 | ||
6470 | static bfd_boolean | |
6471 | copy_elf_program_header (bfd *ibfd, bfd *obfd) | |
6472 | { | |
6473 | Elf_Internal_Ehdr *iehdr; | |
6474 | struct elf_segment_map *map; | |
6475 | struct elf_segment_map *map_first; | |
6476 | struct elf_segment_map **pointer_to_map; | |
6477 | Elf_Internal_Phdr *segment; | |
6478 | unsigned int i; | |
6479 | unsigned int num_segments; | |
6480 | bfd_boolean phdr_included = FALSE; | |
88967714 | 6481 | bfd_boolean p_paddr_valid; |
84d1d650 L |
6482 | |
6483 | iehdr = elf_elfheader (ibfd); | |
6484 | ||
6485 | map_first = NULL; | |
6486 | pointer_to_map = &map_first; | |
6487 | ||
88967714 AM |
6488 | /* If all the segment p_paddr fields are zero, don't set |
6489 | map->p_paddr_valid. */ | |
6490 | p_paddr_valid = FALSE; | |
84d1d650 | 6491 | num_segments = elf_elfheader (ibfd)->e_phnum; |
88967714 AM |
6492 | for (i = 0, segment = elf_tdata (ibfd)->phdr; |
6493 | i < num_segments; | |
6494 | i++, segment++) | |
6495 | if (segment->p_paddr != 0) | |
6496 | { | |
6497 | p_paddr_valid = TRUE; | |
6498 | break; | |
6499 | } | |
6500 | ||
84d1d650 L |
6501 | for (i = 0, segment = elf_tdata (ibfd)->phdr; |
6502 | i < num_segments; | |
6503 | i++, segment++) | |
6504 | { | |
6505 | asection *section; | |
6506 | unsigned int section_count; | |
6507 | bfd_size_type amt; | |
6508 | Elf_Internal_Shdr *this_hdr; | |
53020534 | 6509 | asection *first_section = NULL; |
a76e6f2f | 6510 | asection *lowest_section; |
84d1d650 | 6511 | |
84d1d650 L |
6512 | /* Compute how many sections are in this segment. */ |
6513 | for (section = ibfd->sections, section_count = 0; | |
6514 | section != NULL; | |
6515 | section = section->next) | |
6516 | { | |
6517 | this_hdr = &(elf_section_data(section)->this_hdr); | |
f4638467 | 6518 | if (ELF_SECTION_IN_SEGMENT (this_hdr, segment)) |
3271a814 | 6519 | { |
a76e6f2f AM |
6520 | if (first_section == NULL) |
6521 | first_section = section; | |
3271a814 NS |
6522 | section_count++; |
6523 | } | |
84d1d650 L |
6524 | } |
6525 | ||
6526 | /* Allocate a segment map big enough to contain | |
6527 | all of the sections we have selected. */ | |
6528 | amt = sizeof (struct elf_segment_map); | |
6529 | if (section_count != 0) | |
6530 | amt += ((bfd_size_type) section_count - 1) * sizeof (asection *); | |
a50b1753 | 6531 | map = (struct elf_segment_map *) bfd_zalloc (obfd, amt); |
84d1d650 L |
6532 | if (map == NULL) |
6533 | return FALSE; | |
6534 | ||
6535 | /* Initialize the fields of the output segment map with the | |
6536 | input segment. */ | |
6537 | map->next = NULL; | |
6538 | map->p_type = segment->p_type; | |
6539 | map->p_flags = segment->p_flags; | |
6540 | map->p_flags_valid = 1; | |
6541 | map->p_paddr = segment->p_paddr; | |
88967714 | 6542 | map->p_paddr_valid = p_paddr_valid; |
3f570048 AM |
6543 | map->p_align = segment->p_align; |
6544 | map->p_align_valid = 1; | |
3271a814 | 6545 | map->p_vaddr_offset = 0; |
84d1d650 | 6546 | |
04c3a755 NS |
6547 | if (map->p_type == PT_GNU_RELRO |
6548 | || map->p_type == PT_GNU_STACK) | |
b10a8ae0 L |
6549 | { |
6550 | /* The PT_GNU_RELRO segment may contain the first a few | |
6551 | bytes in the .got.plt section even if the whole .got.plt | |
6552 | section isn't in the PT_GNU_RELRO segment. We won't | |
04c3a755 NS |
6553 | change the size of the PT_GNU_RELRO segment. |
6554 | Similarly, PT_GNU_STACK size is significant on uclinux | |
6555 | systems. */ | |
9433b9b1 | 6556 | map->p_size = segment->p_memsz; |
b10a8ae0 L |
6557 | map->p_size_valid = 1; |
6558 | } | |
6559 | ||
84d1d650 L |
6560 | /* Determine if this segment contains the ELF file header |
6561 | and if it contains the program headers themselves. */ | |
6562 | map->includes_filehdr = (segment->p_offset == 0 | |
6563 | && segment->p_filesz >= iehdr->e_ehsize); | |
6564 | ||
6565 | map->includes_phdrs = 0; | |
6566 | if (! phdr_included || segment->p_type != PT_LOAD) | |
6567 | { | |
6568 | map->includes_phdrs = | |
6569 | (segment->p_offset <= (bfd_vma) iehdr->e_phoff | |
6570 | && (segment->p_offset + segment->p_filesz | |
6571 | >= ((bfd_vma) iehdr->e_phoff | |
6572 | + iehdr->e_phnum * iehdr->e_phentsize))); | |
6573 | ||
6574 | if (segment->p_type == PT_LOAD && map->includes_phdrs) | |
6575 | phdr_included = TRUE; | |
6576 | } | |
6577 | ||
bbefd0a9 | 6578 | lowest_section = NULL; |
84d1d650 L |
6579 | if (section_count != 0) |
6580 | { | |
6581 | unsigned int isec = 0; | |
6582 | ||
53020534 | 6583 | for (section = first_section; |
84d1d650 L |
6584 | section != NULL; |
6585 | section = section->next) | |
6586 | { | |
6587 | this_hdr = &(elf_section_data(section)->this_hdr); | |
f4638467 | 6588 | if (ELF_SECTION_IN_SEGMENT (this_hdr, segment)) |
53020534 L |
6589 | { |
6590 | map->sections[isec++] = section->output_section; | |
a76e6f2f AM |
6591 | if ((section->flags & SEC_ALLOC) != 0) |
6592 | { | |
6593 | bfd_vma seg_off; | |
6594 | ||
bbefd0a9 AM |
6595 | if (lowest_section == NULL |
6596 | || section->lma < lowest_section->lma) | |
fb8a5684 AM |
6597 | lowest_section = section; |
6598 | ||
a76e6f2f AM |
6599 | /* Section lmas are set up from PT_LOAD header |
6600 | p_paddr in _bfd_elf_make_section_from_shdr. | |
6601 | If this header has a p_paddr that disagrees | |
6602 | with the section lma, flag the p_paddr as | |
6603 | invalid. */ | |
6604 | if ((section->flags & SEC_LOAD) != 0) | |
6605 | seg_off = this_hdr->sh_offset - segment->p_offset; | |
6606 | else | |
6607 | seg_off = this_hdr->sh_addr - segment->p_vaddr; | |
6608 | if (section->lma - segment->p_paddr != seg_off) | |
6609 | map->p_paddr_valid = FALSE; | |
6610 | } | |
53020534 L |
6611 | if (isec == section_count) |
6612 | break; | |
6613 | } | |
84d1d650 L |
6614 | } |
6615 | } | |
6616 | ||
a76e6f2f AM |
6617 | if (map->includes_filehdr && lowest_section != NULL) |
6618 | /* We need to keep the space used by the headers fixed. */ | |
6619 | map->header_size = lowest_section->vma - segment->p_vaddr; | |
d324f6d6 | 6620 | |
a76e6f2f AM |
6621 | if (!map->includes_phdrs |
6622 | && !map->includes_filehdr | |
6623 | && map->p_paddr_valid) | |
6624 | /* There is some other padding before the first section. */ | |
6625 | map->p_vaddr_offset = ((lowest_section ? lowest_section->lma : 0) | |
6626 | - segment->p_paddr); | |
6627 | ||
84d1d650 L |
6628 | map->count = section_count; |
6629 | *pointer_to_map = map; | |
6630 | pointer_to_map = &map->next; | |
6631 | } | |
6632 | ||
12bd6957 | 6633 | elf_seg_map (obfd) = map_first; |
84d1d650 L |
6634 | return TRUE; |
6635 | } | |
6636 | ||
6637 | /* Copy private BFD data. This copies or rewrites ELF program header | |
6638 | information. */ | |
6639 | ||
6640 | static bfd_boolean | |
6641 | copy_private_bfd_data (bfd *ibfd, bfd *obfd) | |
6642 | { | |
84d1d650 L |
6643 | if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour |
6644 | || bfd_get_flavour (obfd) != bfd_target_elf_flavour) | |
6645 | return TRUE; | |
6646 | ||
6647 | if (elf_tdata (ibfd)->phdr == NULL) | |
6648 | return TRUE; | |
6649 | ||
6650 | if (ibfd->xvec == obfd->xvec) | |
6651 | { | |
cb3ff1e5 NC |
6652 | /* Check to see if any sections in the input BFD |
6653 | covered by ELF program header have changed. */ | |
d55ce4e2 | 6654 | Elf_Internal_Phdr *segment; |
84d1d650 L |
6655 | asection *section, *osec; |
6656 | unsigned int i, num_segments; | |
6657 | Elf_Internal_Shdr *this_hdr; | |
147d51c2 L |
6658 | const struct elf_backend_data *bed; |
6659 | ||
6660 | bed = get_elf_backend_data (ibfd); | |
6661 | ||
6662 | /* Regenerate the segment map if p_paddr is set to 0. */ | |
6663 | if (bed->want_p_paddr_set_to_zero) | |
6664 | goto rewrite; | |
84d1d650 L |
6665 | |
6666 | /* Initialize the segment mark field. */ | |
6667 | for (section = obfd->sections; section != NULL; | |
6668 | section = section->next) | |
6669 | section->segment_mark = FALSE; | |
6670 | ||
6671 | num_segments = elf_elfheader (ibfd)->e_phnum; | |
6672 | for (i = 0, segment = elf_tdata (ibfd)->phdr; | |
6673 | i < num_segments; | |
6674 | i++, segment++) | |
6675 | { | |
5f6999aa NC |
6676 | /* PR binutils/3535. The Solaris linker always sets the p_paddr |
6677 | and p_memsz fields of special segments (DYNAMIC, INTERP) to 0 | |
6678 | which severly confuses things, so always regenerate the segment | |
6679 | map in this case. */ | |
6680 | if (segment->p_paddr == 0 | |
6681 | && segment->p_memsz == 0 | |
6682 | && (segment->p_type == PT_INTERP || segment->p_type == PT_DYNAMIC)) | |
cb3ff1e5 | 6683 | goto rewrite; |
5f6999aa | 6684 | |
84d1d650 L |
6685 | for (section = ibfd->sections; |
6686 | section != NULL; section = section->next) | |
6687 | { | |
6688 | /* We mark the output section so that we know it comes | |
6689 | from the input BFD. */ | |
6690 | osec = section->output_section; | |
6691 | if (osec) | |
6692 | osec->segment_mark = TRUE; | |
6693 | ||
6694 | /* Check if this section is covered by the segment. */ | |
6695 | this_hdr = &(elf_section_data(section)->this_hdr); | |
f4638467 | 6696 | if (ELF_SECTION_IN_SEGMENT (this_hdr, segment)) |
84d1d650 L |
6697 | { |
6698 | /* FIXME: Check if its output section is changed or | |
6699 | removed. What else do we need to check? */ | |
6700 | if (osec == NULL | |
6701 | || section->flags != osec->flags | |
6702 | || section->lma != osec->lma | |
6703 | || section->vma != osec->vma | |
6704 | || section->size != osec->size | |
6705 | || section->rawsize != osec->rawsize | |
6706 | || section->alignment_power != osec->alignment_power) | |
6707 | goto rewrite; | |
6708 | } | |
6709 | } | |
6710 | } | |
6711 | ||
cb3ff1e5 | 6712 | /* Check to see if any output section do not come from the |
84d1d650 L |
6713 | input BFD. */ |
6714 | for (section = obfd->sections; section != NULL; | |
6715 | section = section->next) | |
6716 | { | |
6717 | if (section->segment_mark == FALSE) | |
6718 | goto rewrite; | |
6719 | else | |
6720 | section->segment_mark = FALSE; | |
6721 | } | |
6722 | ||
6723 | return copy_elf_program_header (ibfd, obfd); | |
6724 | } | |
6725 | ||
6726 | rewrite: | |
f1d85785 L |
6727 | if (ibfd->xvec == obfd->xvec) |
6728 | { | |
6729 | /* When rewriting program header, set the output maxpagesize to | |
6730 | the maximum alignment of input PT_LOAD segments. */ | |
6731 | Elf_Internal_Phdr *segment; | |
6732 | unsigned int i; | |
6733 | unsigned int num_segments = elf_elfheader (ibfd)->e_phnum; | |
6734 | bfd_vma maxpagesize = 0; | |
6735 | ||
6736 | for (i = 0, segment = elf_tdata (ibfd)->phdr; | |
6737 | i < num_segments; | |
6738 | i++, segment++) | |
6739 | if (segment->p_type == PT_LOAD | |
6740 | && maxpagesize < segment->p_align) | |
c86934ce NC |
6741 | { |
6742 | /* PR 17512: file: f17299af. */ | |
6743 | if (segment->p_align > (bfd_vma) 1 << ((sizeof (bfd_vma) * 8) - 2)) | |
6744 | (*_bfd_error_handler) (_("\ | |
6745 | %B: warning: segment alignment of 0x%llx is too large"), | |
6746 | ibfd, (long long) segment->p_align); | |
6747 | else | |
6748 | maxpagesize = segment->p_align; | |
6749 | } | |
f1d85785 L |
6750 | |
6751 | if (maxpagesize != get_elf_backend_data (obfd)->maxpagesize) | |
6752 | bfd_emul_set_maxpagesize (bfd_get_target (obfd), maxpagesize); | |
6753 | } | |
6754 | ||
84d1d650 L |
6755 | return rewrite_elf_program_header (ibfd, obfd); |
6756 | } | |
6757 | ||
ccd2ec6a L |
6758 | /* Initialize private output section information from input section. */ |
6759 | ||
6760 | bfd_boolean | |
6761 | _bfd_elf_init_private_section_data (bfd *ibfd, | |
6762 | asection *isec, | |
6763 | bfd *obfd, | |
6764 | asection *osec, | |
6765 | struct bfd_link_info *link_info) | |
6766 | ||
6767 | { | |
6768 | Elf_Internal_Shdr *ihdr, *ohdr; | |
dfa7b0b8 | 6769 | bfd_boolean final_link = link_info != NULL && !link_info->relocatable; |
ccd2ec6a L |
6770 | |
6771 | if (ibfd->xvec->flavour != bfd_target_elf_flavour | |
6772 | || obfd->xvec->flavour != bfd_target_elf_flavour) | |
6773 | return TRUE; | |
6774 | ||
ba85c43e NC |
6775 | BFD_ASSERT (elf_section_data (osec) != NULL); |
6776 | ||
dfa7b0b8 AM |
6777 | /* For objcopy and relocatable link, don't copy the output ELF |
6778 | section type from input if the output BFD section flags have been | |
6779 | set to something different. For a final link allow some flags | |
6780 | that the linker clears to differ. */ | |
42bb2e33 | 6781 | if (elf_section_type (osec) == SHT_NULL |
dfa7b0b8 AM |
6782 | && (osec->flags == isec->flags |
6783 | || (final_link | |
6784 | && ((osec->flags ^ isec->flags) | |
0814be7d | 6785 | & ~(SEC_LINK_ONCE | SEC_LINK_DUPLICATES | SEC_RELOC)) == 0))) |
42bb2e33 | 6786 | elf_section_type (osec) = elf_section_type (isec); |
d270463e L |
6787 | |
6788 | /* FIXME: Is this correct for all OS/PROC specific flags? */ | |
6789 | elf_section_flags (osec) |= (elf_section_flags (isec) | |
6790 | & (SHF_MASKOS | SHF_MASKPROC)); | |
ccd2ec6a L |
6791 | |
6792 | /* Set things up for objcopy and relocatable link. The output | |
6793 | SHT_GROUP section will have its elf_next_in_group pointing back | |
6794 | to the input group members. Ignore linker created group section. | |
6795 | See elfNN_ia64_object_p in elfxx-ia64.c. */ | |
dfa7b0b8 | 6796 | if (!final_link) |
ccd2ec6a L |
6797 | { |
6798 | if (elf_sec_group (isec) == NULL | |
6799 | || (elf_sec_group (isec)->flags & SEC_LINKER_CREATED) == 0) | |
6800 | { | |
6801 | if (elf_section_flags (isec) & SHF_GROUP) | |
6802 | elf_section_flags (osec) |= SHF_GROUP; | |
6803 | elf_next_in_group (osec) = elf_next_in_group (isec); | |
9659de1c | 6804 | elf_section_data (osec)->group = elf_section_data (isec)->group; |
ccd2ec6a | 6805 | } |
68f5ff89 L |
6806 | |
6807 | /* If not decompress, preserve SHF_COMPRESSED. */ | |
6808 | if ((ibfd->flags & BFD_DECOMPRESS) == 0) | |
6809 | elf_section_flags (osec) |= (elf_section_flags (isec) | |
6810 | & SHF_COMPRESSED); | |
ccd2ec6a L |
6811 | } |
6812 | ||
6813 | ihdr = &elf_section_data (isec)->this_hdr; | |
6814 | ||
6815 | /* We need to handle elf_linked_to_section for SHF_LINK_ORDER. We | |
6816 | don't use the output section of the linked-to section since it | |
6817 | may be NULL at this point. */ | |
6818 | if ((ihdr->sh_flags & SHF_LINK_ORDER) != 0) | |
6819 | { | |
6820 | ohdr = &elf_section_data (osec)->this_hdr; | |
6821 | ohdr->sh_flags |= SHF_LINK_ORDER; | |
6822 | elf_linked_to_section (osec) = elf_linked_to_section (isec); | |
6823 | } | |
6824 | ||
6825 | osec->use_rela_p = isec->use_rela_p; | |
6826 | ||
6827 | return TRUE; | |
6828 | } | |
6829 | ||
252b5132 RH |
6830 | /* Copy private section information. This copies over the entsize |
6831 | field, and sometimes the info field. */ | |
6832 | ||
b34976b6 | 6833 | bfd_boolean |
217aa764 AM |
6834 | _bfd_elf_copy_private_section_data (bfd *ibfd, |
6835 | asection *isec, | |
6836 | bfd *obfd, | |
6837 | asection *osec) | |
252b5132 RH |
6838 | { |
6839 | Elf_Internal_Shdr *ihdr, *ohdr; | |
6840 | ||
6841 | if (ibfd->xvec->flavour != bfd_target_elf_flavour | |
6842 | || obfd->xvec->flavour != bfd_target_elf_flavour) | |
b34976b6 | 6843 | return TRUE; |
252b5132 | 6844 | |
252b5132 RH |
6845 | ihdr = &elf_section_data (isec)->this_hdr; |
6846 | ohdr = &elf_section_data (osec)->this_hdr; | |
6847 | ||
6848 | ohdr->sh_entsize = ihdr->sh_entsize; | |
6849 | ||
6850 | if (ihdr->sh_type == SHT_SYMTAB | |
6851 | || ihdr->sh_type == SHT_DYNSYM | |
6852 | || ihdr->sh_type == SHT_GNU_verneed | |
6853 | || ihdr->sh_type == SHT_GNU_verdef) | |
6854 | ohdr->sh_info = ihdr->sh_info; | |
6855 | ||
ccd2ec6a L |
6856 | return _bfd_elf_init_private_section_data (ibfd, isec, obfd, osec, |
6857 | NULL); | |
252b5132 RH |
6858 | } |
6859 | ||
d0bf826b AM |
6860 | /* Look at all the SHT_GROUP sections in IBFD, making any adjustments |
6861 | necessary if we are removing either the SHT_GROUP section or any of | |
6862 | the group member sections. DISCARDED is the value that a section's | |
6863 | output_section has if the section will be discarded, NULL when this | |
6864 | function is called from objcopy, bfd_abs_section_ptr when called | |
6865 | from the linker. */ | |
80fccad2 BW |
6866 | |
6867 | bfd_boolean | |
d0bf826b | 6868 | _bfd_elf_fixup_group_sections (bfd *ibfd, asection *discarded) |
80fccad2 | 6869 | { |
30288845 AM |
6870 | asection *isec; |
6871 | ||
30288845 | 6872 | for (isec = ibfd->sections; isec != NULL; isec = isec->next) |
415f38a6 | 6873 | if (elf_section_type (isec) == SHT_GROUP) |
30288845 AM |
6874 | { |
6875 | asection *first = elf_next_in_group (isec); | |
6876 | asection *s = first; | |
d0bf826b AM |
6877 | bfd_size_type removed = 0; |
6878 | ||
30288845 AM |
6879 | while (s != NULL) |
6880 | { | |
415f38a6 AM |
6881 | /* If this member section is being output but the |
6882 | SHT_GROUP section is not, then clear the group info | |
6883 | set up by _bfd_elf_copy_private_section_data. */ | |
d0bf826b AM |
6884 | if (s->output_section != discarded |
6885 | && isec->output_section == discarded) | |
30288845 AM |
6886 | { |
6887 | elf_section_flags (s->output_section) &= ~SHF_GROUP; | |
6888 | elf_group_name (s->output_section) = NULL; | |
6889 | } | |
415f38a6 AM |
6890 | /* Conversely, if the member section is not being output |
6891 | but the SHT_GROUP section is, then adjust its size. */ | |
d0bf826b AM |
6892 | else if (s->output_section == discarded |
6893 | && isec->output_section != discarded) | |
6894 | removed += 4; | |
30288845 AM |
6895 | s = elf_next_in_group (s); |
6896 | if (s == first) | |
6897 | break; | |
6898 | } | |
d0bf826b AM |
6899 | if (removed != 0) |
6900 | { | |
6901 | if (discarded != NULL) | |
6902 | { | |
6903 | /* If we've been called for ld -r, then we need to | |
6904 | adjust the input section size. This function may | |
6905 | be called multiple times, so save the original | |
6906 | size. */ | |
6907 | if (isec->rawsize == 0) | |
6908 | isec->rawsize = isec->size; | |
6909 | isec->size = isec->rawsize - removed; | |
6910 | } | |
6911 | else | |
6912 | { | |
6913 | /* Adjust the output section size when called from | |
6914 | objcopy. */ | |
6915 | isec->output_section->size -= removed; | |
6916 | } | |
6917 | } | |
30288845 AM |
6918 | } |
6919 | ||
80fccad2 BW |
6920 | return TRUE; |
6921 | } | |
6922 | ||
d0bf826b AM |
6923 | /* Copy private header information. */ |
6924 | ||
6925 | bfd_boolean | |
6926 | _bfd_elf_copy_private_header_data (bfd *ibfd, bfd *obfd) | |
6927 | { | |
6928 | if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour | |
6929 | || bfd_get_flavour (obfd) != bfd_target_elf_flavour) | |
6930 | return TRUE; | |
6931 | ||
6932 | /* Copy over private BFD data if it has not already been copied. | |
6933 | This must be done here, rather than in the copy_private_bfd_data | |
6934 | entry point, because the latter is called after the section | |
6935 | contents have been set, which means that the program headers have | |
6936 | already been worked out. */ | |
12bd6957 | 6937 | if (elf_seg_map (obfd) == NULL && elf_tdata (ibfd)->phdr != NULL) |
d0bf826b AM |
6938 | { |
6939 | if (! copy_private_bfd_data (ibfd, obfd)) | |
6940 | return FALSE; | |
6941 | } | |
6942 | ||
6943 | return _bfd_elf_fixup_group_sections (ibfd, NULL); | |
6944 | } | |
6945 | ||
252b5132 RH |
6946 | /* Copy private symbol information. If this symbol is in a section |
6947 | which we did not map into a BFD section, try to map the section | |
6948 | index correctly. We use special macro definitions for the mapped | |
6949 | section indices; these definitions are interpreted by the | |
6950 | swap_out_syms function. */ | |
6951 | ||
9ad5cbcf AM |
6952 | #define MAP_ONESYMTAB (SHN_HIOS + 1) |
6953 | #define MAP_DYNSYMTAB (SHN_HIOS + 2) | |
6954 | #define MAP_STRTAB (SHN_HIOS + 3) | |
6955 | #define MAP_SHSTRTAB (SHN_HIOS + 4) | |
6956 | #define MAP_SYM_SHNDX (SHN_HIOS + 5) | |
252b5132 | 6957 | |
b34976b6 | 6958 | bfd_boolean |
217aa764 AM |
6959 | _bfd_elf_copy_private_symbol_data (bfd *ibfd, |
6960 | asymbol *isymarg, | |
6961 | bfd *obfd, | |
6962 | asymbol *osymarg) | |
252b5132 RH |
6963 | { |
6964 | elf_symbol_type *isym, *osym; | |
6965 | ||
6966 | if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour | |
6967 | || bfd_get_flavour (obfd) != bfd_target_elf_flavour) | |
b34976b6 | 6968 | return TRUE; |
252b5132 RH |
6969 | |
6970 | isym = elf_symbol_from (ibfd, isymarg); | |
6971 | osym = elf_symbol_from (obfd, osymarg); | |
6972 | ||
6973 | if (isym != NULL | |
8424d8f5 | 6974 | && isym->internal_elf_sym.st_shndx != 0 |
252b5132 RH |
6975 | && osym != NULL |
6976 | && bfd_is_abs_section (isym->symbol.section)) | |
6977 | { | |
6978 | unsigned int shndx; | |
6979 | ||
6980 | shndx = isym->internal_elf_sym.st_shndx; | |
6981 | if (shndx == elf_onesymtab (ibfd)) | |
6982 | shndx = MAP_ONESYMTAB; | |
6983 | else if (shndx == elf_dynsymtab (ibfd)) | |
6984 | shndx = MAP_DYNSYMTAB; | |
12bd6957 | 6985 | else if (shndx == elf_strtab_sec (ibfd)) |
252b5132 | 6986 | shndx = MAP_STRTAB; |
12bd6957 | 6987 | else if (shndx == elf_shstrtab_sec (ibfd)) |
252b5132 | 6988 | shndx = MAP_SHSTRTAB; |
12bd6957 | 6989 | else if (shndx == elf_symtab_shndx (ibfd)) |
9ad5cbcf | 6990 | shndx = MAP_SYM_SHNDX; |
252b5132 RH |
6991 | osym->internal_elf_sym.st_shndx = shndx; |
6992 | } | |
6993 | ||
b34976b6 | 6994 | return TRUE; |
252b5132 RH |
6995 | } |
6996 | ||
6997 | /* Swap out the symbols. */ | |
6998 | ||
b34976b6 | 6999 | static bfd_boolean |
217aa764 AM |
7000 | swap_out_syms (bfd *abfd, |
7001 | struct bfd_strtab_hash **sttp, | |
7002 | int relocatable_p) | |
252b5132 | 7003 | { |
9c5bfbb7 | 7004 | const struct elf_backend_data *bed; |
079e9a2f AM |
7005 | int symcount; |
7006 | asymbol **syms; | |
7007 | struct bfd_strtab_hash *stt; | |
7008 | Elf_Internal_Shdr *symtab_hdr; | |
9ad5cbcf | 7009 | Elf_Internal_Shdr *symtab_shndx_hdr; |
079e9a2f | 7010 | Elf_Internal_Shdr *symstrtab_hdr; |
f075ee0c AM |
7011 | bfd_byte *outbound_syms; |
7012 | bfd_byte *outbound_shndx; | |
079e9a2f | 7013 | int idx; |
12bd6957 | 7014 | unsigned int num_locals; |
079e9a2f | 7015 | bfd_size_type amt; |
174fd7f9 | 7016 | bfd_boolean name_local_sections; |
252b5132 | 7017 | |
12bd6957 | 7018 | if (!elf_map_symbols (abfd, &num_locals)) |
b34976b6 | 7019 | return FALSE; |
252b5132 | 7020 | |
c044fabd | 7021 | /* Dump out the symtabs. */ |
079e9a2f AM |
7022 | stt = _bfd_elf_stringtab_init (); |
7023 | if (stt == NULL) | |
b34976b6 | 7024 | return FALSE; |
252b5132 | 7025 | |
079e9a2f AM |
7026 | bed = get_elf_backend_data (abfd); |
7027 | symcount = bfd_get_symcount (abfd); | |
7028 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; | |
7029 | symtab_hdr->sh_type = SHT_SYMTAB; | |
7030 | symtab_hdr->sh_entsize = bed->s->sizeof_sym; | |
7031 | symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1); | |
12bd6957 | 7032 | symtab_hdr->sh_info = num_locals + 1; |
72de5009 | 7033 | symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align; |
079e9a2f AM |
7034 | |
7035 | symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr; | |
7036 | symstrtab_hdr->sh_type = SHT_STRTAB; | |
7037 | ||
a50b1753 NC |
7038 | outbound_syms = (bfd_byte *) bfd_alloc2 (abfd, 1 + symcount, |
7039 | bed->s->sizeof_sym); | |
079e9a2f | 7040 | if (outbound_syms == NULL) |
5ed6aba4 NC |
7041 | { |
7042 | _bfd_stringtab_free (stt); | |
7043 | return FALSE; | |
7044 | } | |
217aa764 | 7045 | symtab_hdr->contents = outbound_syms; |
252b5132 | 7046 | |
9ad5cbcf AM |
7047 | outbound_shndx = NULL; |
7048 | symtab_shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr; | |
7049 | if (symtab_shndx_hdr->sh_name != 0) | |
7050 | { | |
7051 | amt = (bfd_size_type) (1 + symcount) * sizeof (Elf_External_Sym_Shndx); | |
a50b1753 NC |
7052 | outbound_shndx = (bfd_byte *) |
7053 | bfd_zalloc2 (abfd, 1 + symcount, sizeof (Elf_External_Sym_Shndx)); | |
9ad5cbcf | 7054 | if (outbound_shndx == NULL) |
5ed6aba4 NC |
7055 | { |
7056 | _bfd_stringtab_free (stt); | |
7057 | return FALSE; | |
7058 | } | |
7059 | ||
9ad5cbcf AM |
7060 | symtab_shndx_hdr->contents = outbound_shndx; |
7061 | symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX; | |
7062 | symtab_shndx_hdr->sh_size = amt; | |
7063 | symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx); | |
7064 | symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx); | |
7065 | } | |
7066 | ||
589e6347 | 7067 | /* Now generate the data (for "contents"). */ |
079e9a2f AM |
7068 | { |
7069 | /* Fill in zeroth symbol and swap it out. */ | |
7070 | Elf_Internal_Sym sym; | |
7071 | sym.st_name = 0; | |
7072 | sym.st_value = 0; | |
7073 | sym.st_size = 0; | |
7074 | sym.st_info = 0; | |
7075 | sym.st_other = 0; | |
7076 | sym.st_shndx = SHN_UNDEF; | |
35fc36a8 | 7077 | sym.st_target_internal = 0; |
9ad5cbcf | 7078 | bed->s->swap_symbol_out (abfd, &sym, outbound_syms, outbound_shndx); |
079e9a2f | 7079 | outbound_syms += bed->s->sizeof_sym; |
9ad5cbcf AM |
7080 | if (outbound_shndx != NULL) |
7081 | outbound_shndx += sizeof (Elf_External_Sym_Shndx); | |
079e9a2f | 7082 | } |
252b5132 | 7083 | |
174fd7f9 RS |
7084 | name_local_sections |
7085 | = (bed->elf_backend_name_local_section_symbols | |
7086 | && bed->elf_backend_name_local_section_symbols (abfd)); | |
7087 | ||
079e9a2f AM |
7088 | syms = bfd_get_outsymbols (abfd); |
7089 | for (idx = 0; idx < symcount; idx++) | |
252b5132 | 7090 | { |
252b5132 | 7091 | Elf_Internal_Sym sym; |
079e9a2f AM |
7092 | bfd_vma value = syms[idx]->value; |
7093 | elf_symbol_type *type_ptr; | |
7094 | flagword flags = syms[idx]->flags; | |
7095 | int type; | |
252b5132 | 7096 | |
174fd7f9 RS |
7097 | if (!name_local_sections |
7098 | && (flags & (BSF_SECTION_SYM | BSF_GLOBAL)) == BSF_SECTION_SYM) | |
079e9a2f AM |
7099 | { |
7100 | /* Local section symbols have no name. */ | |
7101 | sym.st_name = 0; | |
7102 | } | |
7103 | else | |
7104 | { | |
7105 | sym.st_name = (unsigned long) _bfd_stringtab_add (stt, | |
7106 | syms[idx]->name, | |
b34976b6 | 7107 | TRUE, FALSE); |
079e9a2f | 7108 | if (sym.st_name == (unsigned long) -1) |
5ed6aba4 NC |
7109 | { |
7110 | _bfd_stringtab_free (stt); | |
7111 | return FALSE; | |
7112 | } | |
079e9a2f | 7113 | } |
252b5132 | 7114 | |
079e9a2f | 7115 | type_ptr = elf_symbol_from (abfd, syms[idx]); |
252b5132 | 7116 | |
079e9a2f AM |
7117 | if ((flags & BSF_SECTION_SYM) == 0 |
7118 | && bfd_is_com_section (syms[idx]->section)) | |
7119 | { | |
7120 | /* ELF common symbols put the alignment into the `value' field, | |
7121 | and the size into the `size' field. This is backwards from | |
7122 | how BFD handles it, so reverse it here. */ | |
7123 | sym.st_size = value; | |
7124 | if (type_ptr == NULL | |
7125 | || type_ptr->internal_elf_sym.st_value == 0) | |
7126 | sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value)); | |
7127 | else | |
7128 | sym.st_value = type_ptr->internal_elf_sym.st_value; | |
7129 | sym.st_shndx = _bfd_elf_section_from_bfd_section | |
7130 | (abfd, syms[idx]->section); | |
7131 | } | |
7132 | else | |
7133 | { | |
7134 | asection *sec = syms[idx]->section; | |
cb33740c | 7135 | unsigned int shndx; |
252b5132 | 7136 | |
079e9a2f AM |
7137 | if (sec->output_section) |
7138 | { | |
7139 | value += sec->output_offset; | |
7140 | sec = sec->output_section; | |
7141 | } | |
589e6347 | 7142 | |
079e9a2f AM |
7143 | /* Don't add in the section vma for relocatable output. */ |
7144 | if (! relocatable_p) | |
7145 | value += sec->vma; | |
7146 | sym.st_value = value; | |
7147 | sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0; | |
7148 | ||
7149 | if (bfd_is_abs_section (sec) | |
7150 | && type_ptr != NULL | |
7151 | && type_ptr->internal_elf_sym.st_shndx != 0) | |
7152 | { | |
7153 | /* This symbol is in a real ELF section which we did | |
7154 | not create as a BFD section. Undo the mapping done | |
7155 | by copy_private_symbol_data. */ | |
7156 | shndx = type_ptr->internal_elf_sym.st_shndx; | |
7157 | switch (shndx) | |
7158 | { | |
7159 | case MAP_ONESYMTAB: | |
7160 | shndx = elf_onesymtab (abfd); | |
7161 | break; | |
7162 | case MAP_DYNSYMTAB: | |
7163 | shndx = elf_dynsymtab (abfd); | |
7164 | break; | |
7165 | case MAP_STRTAB: | |
12bd6957 | 7166 | shndx = elf_strtab_sec (abfd); |
079e9a2f AM |
7167 | break; |
7168 | case MAP_SHSTRTAB: | |
12bd6957 | 7169 | shndx = elf_shstrtab_sec (abfd); |
079e9a2f | 7170 | break; |
9ad5cbcf | 7171 | case MAP_SYM_SHNDX: |
12bd6957 | 7172 | shndx = elf_symtab_shndx (abfd); |
9ad5cbcf | 7173 | break; |
079e9a2f | 7174 | default: |
15bc576a | 7175 | shndx = SHN_ABS; |
079e9a2f AM |
7176 | break; |
7177 | } | |
7178 | } | |
7179 | else | |
7180 | { | |
7181 | shndx = _bfd_elf_section_from_bfd_section (abfd, sec); | |
252b5132 | 7182 | |
cb33740c | 7183 | if (shndx == SHN_BAD) |
079e9a2f AM |
7184 | { |
7185 | asection *sec2; | |
7186 | ||
7187 | /* Writing this would be a hell of a lot easier if | |
7188 | we had some decent documentation on bfd, and | |
7189 | knew what to expect of the library, and what to | |
7190 | demand of applications. For example, it | |
7191 | appears that `objcopy' might not set the | |
7192 | section of a symbol to be a section that is | |
7193 | actually in the output file. */ | |
7194 | sec2 = bfd_get_section_by_name (abfd, sec->name); | |
589e6347 NC |
7195 | if (sec2 == NULL) |
7196 | { | |
7197 | _bfd_error_handler (_("\ | |
7198 | Unable to find equivalent output section for symbol '%s' from section '%s'"), | |
7199 | syms[idx]->name ? syms[idx]->name : "<Local sym>", | |
7200 | sec->name); | |
811072d8 | 7201 | bfd_set_error (bfd_error_invalid_operation); |
5ed6aba4 | 7202 | _bfd_stringtab_free (stt); |
589e6347 NC |
7203 | return FALSE; |
7204 | } | |
811072d8 | 7205 | |
079e9a2f | 7206 | shndx = _bfd_elf_section_from_bfd_section (abfd, sec2); |
cb33740c | 7207 | BFD_ASSERT (shndx != SHN_BAD); |
079e9a2f AM |
7208 | } |
7209 | } | |
252b5132 | 7210 | |
079e9a2f AM |
7211 | sym.st_shndx = shndx; |
7212 | } | |
252b5132 | 7213 | |
13ae64f3 JJ |
7214 | if ((flags & BSF_THREAD_LOCAL) != 0) |
7215 | type = STT_TLS; | |
d8045f23 NC |
7216 | else if ((flags & BSF_GNU_INDIRECT_FUNCTION) != 0) |
7217 | type = STT_GNU_IFUNC; | |
13ae64f3 | 7218 | else if ((flags & BSF_FUNCTION) != 0) |
079e9a2f AM |
7219 | type = STT_FUNC; |
7220 | else if ((flags & BSF_OBJECT) != 0) | |
7221 | type = STT_OBJECT; | |
d9352518 DB |
7222 | else if ((flags & BSF_RELC) != 0) |
7223 | type = STT_RELC; | |
7224 | else if ((flags & BSF_SRELC) != 0) | |
7225 | type = STT_SRELC; | |
079e9a2f AM |
7226 | else |
7227 | type = STT_NOTYPE; | |
252b5132 | 7228 | |
13ae64f3 JJ |
7229 | if (syms[idx]->section->flags & SEC_THREAD_LOCAL) |
7230 | type = STT_TLS; | |
7231 | ||
589e6347 | 7232 | /* Processor-specific types. */ |
079e9a2f AM |
7233 | if (type_ptr != NULL |
7234 | && bed->elf_backend_get_symbol_type) | |
7235 | type = ((*bed->elf_backend_get_symbol_type) | |
7236 | (&type_ptr->internal_elf_sym, type)); | |
252b5132 | 7237 | |
079e9a2f AM |
7238 | if (flags & BSF_SECTION_SYM) |
7239 | { | |
7240 | if (flags & BSF_GLOBAL) | |
7241 | sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION); | |
7242 | else | |
7243 | sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION); | |
7244 | } | |
7245 | else if (bfd_is_com_section (syms[idx]->section)) | |
0a40daed | 7246 | { |
504b7d20 | 7247 | #ifdef USE_STT_COMMON |
0a40daed MK |
7248 | if (type == STT_OBJECT) |
7249 | sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_COMMON); | |
7250 | else | |
504b7d20 | 7251 | #endif |
c91e322a | 7252 | sym.st_info = ELF_ST_INFO (STB_GLOBAL, type); |
0a40daed | 7253 | } |
079e9a2f AM |
7254 | else if (bfd_is_und_section (syms[idx]->section)) |
7255 | sym.st_info = ELF_ST_INFO (((flags & BSF_WEAK) | |
7256 | ? STB_WEAK | |
7257 | : STB_GLOBAL), | |
7258 | type); | |
7259 | else if (flags & BSF_FILE) | |
7260 | sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE); | |
7261 | else | |
7262 | { | |
7263 | int bind = STB_LOCAL; | |
252b5132 | 7264 | |
079e9a2f AM |
7265 | if (flags & BSF_LOCAL) |
7266 | bind = STB_LOCAL; | |
3e7a7d11 NC |
7267 | else if (flags & BSF_GNU_UNIQUE) |
7268 | bind = STB_GNU_UNIQUE; | |
079e9a2f AM |
7269 | else if (flags & BSF_WEAK) |
7270 | bind = STB_WEAK; | |
7271 | else if (flags & BSF_GLOBAL) | |
7272 | bind = STB_GLOBAL; | |
252b5132 | 7273 | |
079e9a2f AM |
7274 | sym.st_info = ELF_ST_INFO (bind, type); |
7275 | } | |
252b5132 | 7276 | |
079e9a2f | 7277 | if (type_ptr != NULL) |
35fc36a8 RS |
7278 | { |
7279 | sym.st_other = type_ptr->internal_elf_sym.st_other; | |
7280 | sym.st_target_internal | |
7281 | = type_ptr->internal_elf_sym.st_target_internal; | |
7282 | } | |
079e9a2f | 7283 | else |
35fc36a8 RS |
7284 | { |
7285 | sym.st_other = 0; | |
7286 | sym.st_target_internal = 0; | |
7287 | } | |
252b5132 | 7288 | |
9ad5cbcf | 7289 | bed->s->swap_symbol_out (abfd, &sym, outbound_syms, outbound_shndx); |
079e9a2f | 7290 | outbound_syms += bed->s->sizeof_sym; |
9ad5cbcf AM |
7291 | if (outbound_shndx != NULL) |
7292 | outbound_shndx += sizeof (Elf_External_Sym_Shndx); | |
079e9a2f | 7293 | } |
252b5132 | 7294 | |
079e9a2f AM |
7295 | *sttp = stt; |
7296 | symstrtab_hdr->sh_size = _bfd_stringtab_size (stt); | |
7297 | symstrtab_hdr->sh_type = SHT_STRTAB; | |
252b5132 | 7298 | |
079e9a2f AM |
7299 | symstrtab_hdr->sh_flags = 0; |
7300 | symstrtab_hdr->sh_addr = 0; | |
7301 | symstrtab_hdr->sh_entsize = 0; | |
7302 | symstrtab_hdr->sh_link = 0; | |
7303 | symstrtab_hdr->sh_info = 0; | |
7304 | symstrtab_hdr->sh_addralign = 1; | |
252b5132 | 7305 | |
b34976b6 | 7306 | return TRUE; |
252b5132 RH |
7307 | } |
7308 | ||
7309 | /* Return the number of bytes required to hold the symtab vector. | |
7310 | ||
7311 | Note that we base it on the count plus 1, since we will null terminate | |
7312 | the vector allocated based on this size. However, the ELF symbol table | |
7313 | always has a dummy entry as symbol #0, so it ends up even. */ | |
7314 | ||
7315 | long | |
217aa764 | 7316 | _bfd_elf_get_symtab_upper_bound (bfd *abfd) |
252b5132 RH |
7317 | { |
7318 | long symcount; | |
7319 | long symtab_size; | |
7320 | Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr; | |
7321 | ||
7322 | symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym; | |
b99d1833 AM |
7323 | symtab_size = (symcount + 1) * (sizeof (asymbol *)); |
7324 | if (symcount > 0) | |
7325 | symtab_size -= sizeof (asymbol *); | |
252b5132 RH |
7326 | |
7327 | return symtab_size; | |
7328 | } | |
7329 | ||
7330 | long | |
217aa764 | 7331 | _bfd_elf_get_dynamic_symtab_upper_bound (bfd *abfd) |
252b5132 RH |
7332 | { |
7333 | long symcount; | |
7334 | long symtab_size; | |
7335 | Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr; | |
7336 | ||
7337 | if (elf_dynsymtab (abfd) == 0) | |
7338 | { | |
7339 | bfd_set_error (bfd_error_invalid_operation); | |
7340 | return -1; | |
7341 | } | |
7342 | ||
7343 | symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym; | |
b99d1833 AM |
7344 | symtab_size = (symcount + 1) * (sizeof (asymbol *)); |
7345 | if (symcount > 0) | |
7346 | symtab_size -= sizeof (asymbol *); | |
252b5132 RH |
7347 | |
7348 | return symtab_size; | |
7349 | } | |
7350 | ||
7351 | long | |
217aa764 AM |
7352 | _bfd_elf_get_reloc_upper_bound (bfd *abfd ATTRIBUTE_UNUSED, |
7353 | sec_ptr asect) | |
252b5132 RH |
7354 | { |
7355 | return (asect->reloc_count + 1) * sizeof (arelent *); | |
7356 | } | |
7357 | ||
7358 | /* Canonicalize the relocs. */ | |
7359 | ||
7360 | long | |
217aa764 AM |
7361 | _bfd_elf_canonicalize_reloc (bfd *abfd, |
7362 | sec_ptr section, | |
7363 | arelent **relptr, | |
7364 | asymbol **symbols) | |
252b5132 RH |
7365 | { |
7366 | arelent *tblptr; | |
7367 | unsigned int i; | |
9c5bfbb7 | 7368 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
252b5132 | 7369 | |
b34976b6 | 7370 | if (! bed->s->slurp_reloc_table (abfd, section, symbols, FALSE)) |
252b5132 RH |
7371 | return -1; |
7372 | ||
7373 | tblptr = section->relocation; | |
7374 | for (i = 0; i < section->reloc_count; i++) | |
7375 | *relptr++ = tblptr++; | |
7376 | ||
7377 | *relptr = NULL; | |
7378 | ||
7379 | return section->reloc_count; | |
7380 | } | |
7381 | ||
7382 | long | |
6cee3f79 | 7383 | _bfd_elf_canonicalize_symtab (bfd *abfd, asymbol **allocation) |
252b5132 | 7384 | { |
9c5bfbb7 | 7385 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
217aa764 | 7386 | long symcount = bed->s->slurp_symbol_table (abfd, allocation, FALSE); |
252b5132 RH |
7387 | |
7388 | if (symcount >= 0) | |
7389 | bfd_get_symcount (abfd) = symcount; | |
7390 | return symcount; | |
7391 | } | |
7392 | ||
7393 | long | |
217aa764 AM |
7394 | _bfd_elf_canonicalize_dynamic_symtab (bfd *abfd, |
7395 | asymbol **allocation) | |
252b5132 | 7396 | { |
9c5bfbb7 | 7397 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
217aa764 | 7398 | long symcount = bed->s->slurp_symbol_table (abfd, allocation, TRUE); |
1f70368c DJ |
7399 | |
7400 | if (symcount >= 0) | |
7401 | bfd_get_dynamic_symcount (abfd) = symcount; | |
7402 | return symcount; | |
252b5132 RH |
7403 | } |
7404 | ||
8615f3f2 AM |
7405 | /* Return the size required for the dynamic reloc entries. Any loadable |
7406 | section that was actually installed in the BFD, and has type SHT_REL | |
7407 | or SHT_RELA, and uses the dynamic symbol table, is considered to be a | |
7408 | dynamic reloc section. */ | |
252b5132 RH |
7409 | |
7410 | long | |
217aa764 | 7411 | _bfd_elf_get_dynamic_reloc_upper_bound (bfd *abfd) |
252b5132 RH |
7412 | { |
7413 | long ret; | |
7414 | asection *s; | |
7415 | ||
7416 | if (elf_dynsymtab (abfd) == 0) | |
7417 | { | |
7418 | bfd_set_error (bfd_error_invalid_operation); | |
7419 | return -1; | |
7420 | } | |
7421 | ||
7422 | ret = sizeof (arelent *); | |
7423 | for (s = abfd->sections; s != NULL; s = s->next) | |
266b05cf | 7424 | if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd) |
252b5132 RH |
7425 | && (elf_section_data (s)->this_hdr.sh_type == SHT_REL |
7426 | || elf_section_data (s)->this_hdr.sh_type == SHT_RELA)) | |
eea6121a | 7427 | ret += ((s->size / elf_section_data (s)->this_hdr.sh_entsize) |
252b5132 RH |
7428 | * sizeof (arelent *)); |
7429 | ||
7430 | return ret; | |
7431 | } | |
7432 | ||
8615f3f2 AM |
7433 | /* Canonicalize the dynamic relocation entries. Note that we return the |
7434 | dynamic relocations as a single block, although they are actually | |
7435 | associated with particular sections; the interface, which was | |
7436 | designed for SunOS style shared libraries, expects that there is only | |
7437 | one set of dynamic relocs. Any loadable section that was actually | |
7438 | installed in the BFD, and has type SHT_REL or SHT_RELA, and uses the | |
7439 | dynamic symbol table, is considered to be a dynamic reloc section. */ | |
252b5132 RH |
7440 | |
7441 | long | |
217aa764 AM |
7442 | _bfd_elf_canonicalize_dynamic_reloc (bfd *abfd, |
7443 | arelent **storage, | |
7444 | asymbol **syms) | |
252b5132 | 7445 | { |
217aa764 | 7446 | bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean); |
252b5132 RH |
7447 | asection *s; |
7448 | long ret; | |
7449 | ||
7450 | if (elf_dynsymtab (abfd) == 0) | |
7451 | { | |
7452 | bfd_set_error (bfd_error_invalid_operation); | |
7453 | return -1; | |
7454 | } | |
7455 | ||
7456 | slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table; | |
7457 | ret = 0; | |
7458 | for (s = abfd->sections; s != NULL; s = s->next) | |
7459 | { | |
266b05cf | 7460 | if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd) |
252b5132 RH |
7461 | && (elf_section_data (s)->this_hdr.sh_type == SHT_REL |
7462 | || elf_section_data (s)->this_hdr.sh_type == SHT_RELA)) | |
7463 | { | |
7464 | arelent *p; | |
7465 | long count, i; | |
7466 | ||
b34976b6 | 7467 | if (! (*slurp_relocs) (abfd, s, syms, TRUE)) |
252b5132 | 7468 | return -1; |
eea6121a | 7469 | count = s->size / elf_section_data (s)->this_hdr.sh_entsize; |
252b5132 RH |
7470 | p = s->relocation; |
7471 | for (i = 0; i < count; i++) | |
7472 | *storage++ = p++; | |
7473 | ret += count; | |
7474 | } | |
7475 | } | |
7476 | ||
7477 | *storage = NULL; | |
7478 | ||
7479 | return ret; | |
7480 | } | |
7481 | \f | |
7482 | /* Read in the version information. */ | |
7483 | ||
b34976b6 | 7484 | bfd_boolean |
fc0e6df6 | 7485 | _bfd_elf_slurp_version_tables (bfd *abfd, bfd_boolean default_imported_symver) |
252b5132 RH |
7486 | { |
7487 | bfd_byte *contents = NULL; | |
fc0e6df6 PB |
7488 | unsigned int freeidx = 0; |
7489 | ||
7490 | if (elf_dynverref (abfd) != 0) | |
7491 | { | |
7492 | Elf_Internal_Shdr *hdr; | |
7493 | Elf_External_Verneed *everneed; | |
7494 | Elf_Internal_Verneed *iverneed; | |
7495 | unsigned int i; | |
d0fb9a8d | 7496 | bfd_byte *contents_end; |
fc0e6df6 PB |
7497 | |
7498 | hdr = &elf_tdata (abfd)->dynverref_hdr; | |
7499 | ||
601a03ba | 7500 | if (hdr->sh_info == 0 || hdr->sh_size < sizeof (Elf_External_Verneed)) |
d0fb9a8d | 7501 | { |
601a03ba AM |
7502 | error_return_bad_verref: |
7503 | (*_bfd_error_handler) | |
7504 | (_("%B: .gnu.version_r invalid entry"), abfd); | |
7505 | bfd_set_error (bfd_error_bad_value); | |
d0fb9a8d JJ |
7506 | error_return_verref: |
7507 | elf_tdata (abfd)->verref = NULL; | |
7508 | elf_tdata (abfd)->cverrefs = 0; | |
7509 | goto error_return; | |
7510 | } | |
601a03ba AM |
7511 | |
7512 | contents = (bfd_byte *) bfd_malloc (hdr->sh_size); | |
7513 | if (contents == NULL) | |
7514 | goto error_return_verref; | |
7515 | ||
fc0e6df6 PB |
7516 | if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0 |
7517 | || bfd_bread (contents, hdr->sh_size, abfd) != hdr->sh_size) | |
d0fb9a8d | 7518 | goto error_return_verref; |
fc0e6df6 | 7519 | |
601a03ba AM |
7520 | elf_tdata (abfd)->verref = (Elf_Internal_Verneed *) |
7521 | bfd_zalloc2 (abfd, hdr->sh_info, sizeof (Elf_Internal_Verneed)); | |
7522 | ||
7523 | if (elf_tdata (abfd)->verref == NULL) | |
d0fb9a8d JJ |
7524 | goto error_return_verref; |
7525 | ||
7526 | BFD_ASSERT (sizeof (Elf_External_Verneed) | |
7527 | == sizeof (Elf_External_Vernaux)); | |
7528 | contents_end = contents + hdr->sh_size - sizeof (Elf_External_Verneed); | |
fc0e6df6 PB |
7529 | everneed = (Elf_External_Verneed *) contents; |
7530 | iverneed = elf_tdata (abfd)->verref; | |
7531 | for (i = 0; i < hdr->sh_info; i++, iverneed++) | |
7532 | { | |
7533 | Elf_External_Vernaux *evernaux; | |
7534 | Elf_Internal_Vernaux *ivernaux; | |
7535 | unsigned int j; | |
7536 | ||
7537 | _bfd_elf_swap_verneed_in (abfd, everneed, iverneed); | |
7538 | ||
7539 | iverneed->vn_bfd = abfd; | |
7540 | ||
7541 | iverneed->vn_filename = | |
7542 | bfd_elf_string_from_elf_section (abfd, hdr->sh_link, | |
7543 | iverneed->vn_file); | |
7544 | if (iverneed->vn_filename == NULL) | |
601a03ba | 7545 | goto error_return_bad_verref; |
fc0e6df6 | 7546 | |
d0fb9a8d JJ |
7547 | if (iverneed->vn_cnt == 0) |
7548 | iverneed->vn_auxptr = NULL; | |
7549 | else | |
7550 | { | |
a50b1753 NC |
7551 | iverneed->vn_auxptr = (struct elf_internal_vernaux *) |
7552 | bfd_alloc2 (abfd, iverneed->vn_cnt, | |
7553 | sizeof (Elf_Internal_Vernaux)); | |
d0fb9a8d JJ |
7554 | if (iverneed->vn_auxptr == NULL) |
7555 | goto error_return_verref; | |
7556 | } | |
7557 | ||
7558 | if (iverneed->vn_aux | |
7559 | > (size_t) (contents_end - (bfd_byte *) everneed)) | |
601a03ba | 7560 | goto error_return_bad_verref; |
fc0e6df6 PB |
7561 | |
7562 | evernaux = ((Elf_External_Vernaux *) | |
7563 | ((bfd_byte *) everneed + iverneed->vn_aux)); | |
7564 | ivernaux = iverneed->vn_auxptr; | |
7565 | for (j = 0; j < iverneed->vn_cnt; j++, ivernaux++) | |
7566 | { | |
7567 | _bfd_elf_swap_vernaux_in (abfd, evernaux, ivernaux); | |
7568 | ||
7569 | ivernaux->vna_nodename = | |
7570 | bfd_elf_string_from_elf_section (abfd, hdr->sh_link, | |
7571 | ivernaux->vna_name); | |
7572 | if (ivernaux->vna_nodename == NULL) | |
601a03ba | 7573 | goto error_return_bad_verref; |
fc0e6df6 | 7574 | |
25ff461f AM |
7575 | if (ivernaux->vna_other > freeidx) |
7576 | freeidx = ivernaux->vna_other; | |
7577 | ||
7578 | ivernaux->vna_nextptr = NULL; | |
7579 | if (ivernaux->vna_next == 0) | |
7580 | { | |
7581 | iverneed->vn_cnt = j + 1; | |
7582 | break; | |
7583 | } | |
fc0e6df6 PB |
7584 | if (j + 1 < iverneed->vn_cnt) |
7585 | ivernaux->vna_nextptr = ivernaux + 1; | |
fc0e6df6 | 7586 | |
d0fb9a8d JJ |
7587 | if (ivernaux->vna_next |
7588 | > (size_t) (contents_end - (bfd_byte *) evernaux)) | |
601a03ba | 7589 | goto error_return_bad_verref; |
d0fb9a8d | 7590 | |
fc0e6df6 PB |
7591 | evernaux = ((Elf_External_Vernaux *) |
7592 | ((bfd_byte *) evernaux + ivernaux->vna_next)); | |
fc0e6df6 PB |
7593 | } |
7594 | ||
25ff461f AM |
7595 | iverneed->vn_nextref = NULL; |
7596 | if (iverneed->vn_next == 0) | |
7597 | break; | |
fc0e6df6 PB |
7598 | if (i + 1 < hdr->sh_info) |
7599 | iverneed->vn_nextref = iverneed + 1; | |
fc0e6df6 | 7600 | |
d0fb9a8d JJ |
7601 | if (iverneed->vn_next |
7602 | > (size_t) (contents_end - (bfd_byte *) everneed)) | |
601a03ba | 7603 | goto error_return_bad_verref; |
d0fb9a8d | 7604 | |
fc0e6df6 PB |
7605 | everneed = ((Elf_External_Verneed *) |
7606 | ((bfd_byte *) everneed + iverneed->vn_next)); | |
7607 | } | |
25ff461f | 7608 | elf_tdata (abfd)->cverrefs = i; |
fc0e6df6 PB |
7609 | |
7610 | free (contents); | |
7611 | contents = NULL; | |
7612 | } | |
252b5132 RH |
7613 | |
7614 | if (elf_dynverdef (abfd) != 0) | |
7615 | { | |
7616 | Elf_Internal_Shdr *hdr; | |
7617 | Elf_External_Verdef *everdef; | |
7618 | Elf_Internal_Verdef *iverdef; | |
f631889e UD |
7619 | Elf_Internal_Verdef *iverdefarr; |
7620 | Elf_Internal_Verdef iverdefmem; | |
252b5132 | 7621 | unsigned int i; |
062e2358 | 7622 | unsigned int maxidx; |
d0fb9a8d | 7623 | bfd_byte *contents_end_def, *contents_end_aux; |
252b5132 RH |
7624 | |
7625 | hdr = &elf_tdata (abfd)->dynverdef_hdr; | |
7626 | ||
601a03ba AM |
7627 | if (hdr->sh_info == 0 || hdr->sh_size < sizeof (Elf_External_Verdef)) |
7628 | { | |
7629 | error_return_bad_verdef: | |
7630 | (*_bfd_error_handler) | |
7631 | (_("%B: .gnu.version_d invalid entry"), abfd); | |
7632 | bfd_set_error (bfd_error_bad_value); | |
7633 | error_return_verdef: | |
7634 | elf_tdata (abfd)->verdef = NULL; | |
7635 | elf_tdata (abfd)->cverdefs = 0; | |
7636 | goto error_return; | |
7637 | } | |
7638 | ||
a50b1753 | 7639 | contents = (bfd_byte *) bfd_malloc (hdr->sh_size); |
252b5132 | 7640 | if (contents == NULL) |
601a03ba | 7641 | goto error_return_verdef; |
252b5132 | 7642 | if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0 |
217aa764 | 7643 | || bfd_bread (contents, hdr->sh_size, abfd) != hdr->sh_size) |
601a03ba | 7644 | goto error_return_verdef; |
d0fb9a8d JJ |
7645 | |
7646 | BFD_ASSERT (sizeof (Elf_External_Verdef) | |
7647 | >= sizeof (Elf_External_Verdaux)); | |
7648 | contents_end_def = contents + hdr->sh_size | |
7649 | - sizeof (Elf_External_Verdef); | |
7650 | contents_end_aux = contents + hdr->sh_size | |
7651 | - sizeof (Elf_External_Verdaux); | |
7652 | ||
f631889e UD |
7653 | /* We know the number of entries in the section but not the maximum |
7654 | index. Therefore we have to run through all entries and find | |
7655 | the maximum. */ | |
252b5132 | 7656 | everdef = (Elf_External_Verdef *) contents; |
f631889e UD |
7657 | maxidx = 0; |
7658 | for (i = 0; i < hdr->sh_info; ++i) | |
7659 | { | |
7660 | _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem); | |
7661 | ||
601a03ba AM |
7662 | if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) == 0) |
7663 | goto error_return_bad_verdef; | |
062e2358 AM |
7664 | if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) > maxidx) |
7665 | maxidx = iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION); | |
f631889e | 7666 | |
25ff461f AM |
7667 | if (iverdefmem.vd_next == 0) |
7668 | break; | |
7669 | ||
d0fb9a8d JJ |
7670 | if (iverdefmem.vd_next |
7671 | > (size_t) (contents_end_def - (bfd_byte *) everdef)) | |
601a03ba | 7672 | goto error_return_bad_verdef; |
d0fb9a8d | 7673 | |
f631889e UD |
7674 | everdef = ((Elf_External_Verdef *) |
7675 | ((bfd_byte *) everdef + iverdefmem.vd_next)); | |
7676 | } | |
7677 | ||
fc0e6df6 PB |
7678 | if (default_imported_symver) |
7679 | { | |
7680 | if (freeidx > maxidx) | |
7681 | maxidx = ++freeidx; | |
7682 | else | |
7683 | freeidx = ++maxidx; | |
7684 | } | |
201159ec | 7685 | |
601a03ba AM |
7686 | elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *) |
7687 | bfd_zalloc2 (abfd, maxidx, sizeof (Elf_Internal_Verdef)); | |
f631889e | 7688 | if (elf_tdata (abfd)->verdef == NULL) |
601a03ba | 7689 | goto error_return_verdef; |
f631889e UD |
7690 | |
7691 | elf_tdata (abfd)->cverdefs = maxidx; | |
7692 | ||
7693 | everdef = (Elf_External_Verdef *) contents; | |
7694 | iverdefarr = elf_tdata (abfd)->verdef; | |
7695 | for (i = 0; i < hdr->sh_info; i++) | |
252b5132 RH |
7696 | { |
7697 | Elf_External_Verdaux *everdaux; | |
7698 | Elf_Internal_Verdaux *iverdaux; | |
7699 | unsigned int j; | |
7700 | ||
f631889e UD |
7701 | _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem); |
7702 | ||
d0fb9a8d | 7703 | if ((iverdefmem.vd_ndx & VERSYM_VERSION) == 0) |
601a03ba | 7704 | goto error_return_bad_verdef; |
d0fb9a8d | 7705 | |
f631889e UD |
7706 | iverdef = &iverdefarr[(iverdefmem.vd_ndx & VERSYM_VERSION) - 1]; |
7707 | memcpy (iverdef, &iverdefmem, sizeof (Elf_Internal_Verdef)); | |
252b5132 RH |
7708 | |
7709 | iverdef->vd_bfd = abfd; | |
7710 | ||
d0fb9a8d JJ |
7711 | if (iverdef->vd_cnt == 0) |
7712 | iverdef->vd_auxptr = NULL; | |
7713 | else | |
7714 | { | |
a50b1753 NC |
7715 | iverdef->vd_auxptr = (struct elf_internal_verdaux *) |
7716 | bfd_alloc2 (abfd, iverdef->vd_cnt, | |
7717 | sizeof (Elf_Internal_Verdaux)); | |
d0fb9a8d JJ |
7718 | if (iverdef->vd_auxptr == NULL) |
7719 | goto error_return_verdef; | |
7720 | } | |
7721 | ||
7722 | if (iverdef->vd_aux | |
7723 | > (size_t) (contents_end_aux - (bfd_byte *) everdef)) | |
601a03ba | 7724 | goto error_return_bad_verdef; |
252b5132 RH |
7725 | |
7726 | everdaux = ((Elf_External_Verdaux *) | |
7727 | ((bfd_byte *) everdef + iverdef->vd_aux)); | |
7728 | iverdaux = iverdef->vd_auxptr; | |
7729 | for (j = 0; j < iverdef->vd_cnt; j++, iverdaux++) | |
7730 | { | |
7731 | _bfd_elf_swap_verdaux_in (abfd, everdaux, iverdaux); | |
7732 | ||
7733 | iverdaux->vda_nodename = | |
7734 | bfd_elf_string_from_elf_section (abfd, hdr->sh_link, | |
7735 | iverdaux->vda_name); | |
7736 | if (iverdaux->vda_nodename == NULL) | |
601a03ba | 7737 | goto error_return_bad_verdef; |
252b5132 | 7738 | |
25ff461f AM |
7739 | iverdaux->vda_nextptr = NULL; |
7740 | if (iverdaux->vda_next == 0) | |
7741 | { | |
7742 | iverdef->vd_cnt = j + 1; | |
7743 | break; | |
7744 | } | |
252b5132 RH |
7745 | if (j + 1 < iverdef->vd_cnt) |
7746 | iverdaux->vda_nextptr = iverdaux + 1; | |
252b5132 | 7747 | |
d0fb9a8d JJ |
7748 | if (iverdaux->vda_next |
7749 | > (size_t) (contents_end_aux - (bfd_byte *) everdaux)) | |
601a03ba | 7750 | goto error_return_bad_verdef; |
d0fb9a8d | 7751 | |
252b5132 RH |
7752 | everdaux = ((Elf_External_Verdaux *) |
7753 | ((bfd_byte *) everdaux + iverdaux->vda_next)); | |
7754 | } | |
7755 | ||
d0fb9a8d JJ |
7756 | if (iverdef->vd_cnt) |
7757 | iverdef->vd_nodename = iverdef->vd_auxptr->vda_nodename; | |
252b5132 | 7758 | |
25ff461f AM |
7759 | iverdef->vd_nextdef = NULL; |
7760 | if (iverdef->vd_next == 0) | |
7761 | break; | |
d0fb9a8d | 7762 | if ((size_t) (iverdef - iverdefarr) + 1 < maxidx) |
252b5132 | 7763 | iverdef->vd_nextdef = iverdef + 1; |
252b5132 RH |
7764 | |
7765 | everdef = ((Elf_External_Verdef *) | |
7766 | ((bfd_byte *) everdef + iverdef->vd_next)); | |
7767 | } | |
7768 | ||
7769 | free (contents); | |
7770 | contents = NULL; | |
7771 | } | |
fc0e6df6 | 7772 | else if (default_imported_symver) |
252b5132 | 7773 | { |
fc0e6df6 PB |
7774 | if (freeidx < 3) |
7775 | freeidx = 3; | |
7776 | else | |
7777 | freeidx++; | |
252b5132 | 7778 | |
a50b1753 NC |
7779 | elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *) |
7780 | bfd_zalloc2 (abfd, freeidx, sizeof (Elf_Internal_Verdef)); | |
fc0e6df6 | 7781 | if (elf_tdata (abfd)->verdef == NULL) |
252b5132 RH |
7782 | goto error_return; |
7783 | ||
fc0e6df6 PB |
7784 | elf_tdata (abfd)->cverdefs = freeidx; |
7785 | } | |
252b5132 | 7786 | |
fc0e6df6 PB |
7787 | /* Create a default version based on the soname. */ |
7788 | if (default_imported_symver) | |
7789 | { | |
7790 | Elf_Internal_Verdef *iverdef; | |
7791 | Elf_Internal_Verdaux *iverdaux; | |
252b5132 | 7792 | |
5bb3703f | 7793 | iverdef = &elf_tdata (abfd)->verdef[freeidx - 1]; |
252b5132 | 7794 | |
fc0e6df6 PB |
7795 | iverdef->vd_version = VER_DEF_CURRENT; |
7796 | iverdef->vd_flags = 0; | |
7797 | iverdef->vd_ndx = freeidx; | |
7798 | iverdef->vd_cnt = 1; | |
252b5132 | 7799 | |
fc0e6df6 | 7800 | iverdef->vd_bfd = abfd; |
252b5132 | 7801 | |
fc0e6df6 PB |
7802 | iverdef->vd_nodename = bfd_elf_get_dt_soname (abfd); |
7803 | if (iverdef->vd_nodename == NULL) | |
d0fb9a8d | 7804 | goto error_return_verdef; |
fc0e6df6 | 7805 | iverdef->vd_nextdef = NULL; |
601a03ba AM |
7806 | iverdef->vd_auxptr = ((struct elf_internal_verdaux *) |
7807 | bfd_zalloc (abfd, sizeof (Elf_Internal_Verdaux))); | |
d0fb9a8d JJ |
7808 | if (iverdef->vd_auxptr == NULL) |
7809 | goto error_return_verdef; | |
252b5132 | 7810 | |
fc0e6df6 PB |
7811 | iverdaux = iverdef->vd_auxptr; |
7812 | iverdaux->vda_nodename = iverdef->vd_nodename; | |
252b5132 RH |
7813 | } |
7814 | ||
b34976b6 | 7815 | return TRUE; |
252b5132 RH |
7816 | |
7817 | error_return: | |
5ed6aba4 | 7818 | if (contents != NULL) |
252b5132 | 7819 | free (contents); |
b34976b6 | 7820 | return FALSE; |
252b5132 RH |
7821 | } |
7822 | \f | |
7823 | asymbol * | |
217aa764 | 7824 | _bfd_elf_make_empty_symbol (bfd *abfd) |
252b5132 RH |
7825 | { |
7826 | elf_symbol_type *newsym; | |
7827 | ||
201159ec | 7828 | newsym = (elf_symbol_type *) bfd_zalloc (abfd, sizeof * newsym); |
252b5132 RH |
7829 | if (!newsym) |
7830 | return NULL; | |
201159ec NC |
7831 | newsym->symbol.the_bfd = abfd; |
7832 | return &newsym->symbol; | |
252b5132 RH |
7833 | } |
7834 | ||
7835 | void | |
217aa764 AM |
7836 | _bfd_elf_get_symbol_info (bfd *abfd ATTRIBUTE_UNUSED, |
7837 | asymbol *symbol, | |
7838 | symbol_info *ret) | |
252b5132 RH |
7839 | { |
7840 | bfd_symbol_info (symbol, ret); | |
7841 | } | |
7842 | ||
7843 | /* Return whether a symbol name implies a local symbol. Most targets | |
7844 | use this function for the is_local_label_name entry point, but some | |
7845 | override it. */ | |
7846 | ||
b34976b6 | 7847 | bfd_boolean |
217aa764 AM |
7848 | _bfd_elf_is_local_label_name (bfd *abfd ATTRIBUTE_UNUSED, |
7849 | const char *name) | |
252b5132 RH |
7850 | { |
7851 | /* Normal local symbols start with ``.L''. */ | |
7852 | if (name[0] == '.' && name[1] == 'L') | |
b34976b6 | 7853 | return TRUE; |
252b5132 RH |
7854 | |
7855 | /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate | |
7856 | DWARF debugging symbols starting with ``..''. */ | |
7857 | if (name[0] == '.' && name[1] == '.') | |
b34976b6 | 7858 | return TRUE; |
252b5132 RH |
7859 | |
7860 | /* gcc will sometimes generate symbols beginning with ``_.L_'' when | |
7861 | emitting DWARF debugging output. I suspect this is actually a | |
7862 | small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call | |
7863 | ASM_GENERATE_INTERNAL_LABEL, and this causes the leading | |
7864 | underscore to be emitted on some ELF targets). For ease of use, | |
7865 | we treat such symbols as local. */ | |
7866 | if (name[0] == '_' && name[1] == '.' && name[2] == 'L' && name[3] == '_') | |
b34976b6 | 7867 | return TRUE; |
252b5132 | 7868 | |
ffa54770 NC |
7869 | /* Treat assembler generated local labels as local. */ |
7870 | if (name[0] == 'L' && name[strlen (name) - 1] < 32) | |
7871 | return TRUE; | |
7872 | ||
b34976b6 | 7873 | return FALSE; |
252b5132 RH |
7874 | } |
7875 | ||
7876 | alent * | |
217aa764 AM |
7877 | _bfd_elf_get_lineno (bfd *abfd ATTRIBUTE_UNUSED, |
7878 | asymbol *symbol ATTRIBUTE_UNUSED) | |
252b5132 RH |
7879 | { |
7880 | abort (); | |
7881 | return NULL; | |
7882 | } | |
7883 | ||
b34976b6 | 7884 | bfd_boolean |
217aa764 AM |
7885 | _bfd_elf_set_arch_mach (bfd *abfd, |
7886 | enum bfd_architecture arch, | |
7887 | unsigned long machine) | |
252b5132 RH |
7888 | { |
7889 | /* If this isn't the right architecture for this backend, and this | |
7890 | isn't the generic backend, fail. */ | |
7891 | if (arch != get_elf_backend_data (abfd)->arch | |
7892 | && arch != bfd_arch_unknown | |
7893 | && get_elf_backend_data (abfd)->arch != bfd_arch_unknown) | |
b34976b6 | 7894 | return FALSE; |
252b5132 RH |
7895 | |
7896 | return bfd_default_set_arch_mach (abfd, arch, machine); | |
7897 | } | |
7898 | ||
d1fad7c6 NC |
7899 | /* Find the nearest line to a particular section and offset, |
7900 | for error reporting. */ | |
7901 | ||
b34976b6 | 7902 | bfd_boolean |
217aa764 | 7903 | _bfd_elf_find_nearest_line (bfd *abfd, |
217aa764 | 7904 | asymbol **symbols, |
fb167eb2 | 7905 | asection *section, |
217aa764 AM |
7906 | bfd_vma offset, |
7907 | const char **filename_ptr, | |
7908 | const char **functionname_ptr, | |
fb167eb2 AM |
7909 | unsigned int *line_ptr, |
7910 | unsigned int *discriminator_ptr) | |
d1fad7c6 | 7911 | { |
b34976b6 | 7912 | bfd_boolean found; |
d1fad7c6 | 7913 | |
fb167eb2 | 7914 | if (_bfd_dwarf2_find_nearest_line (abfd, symbols, NULL, section, offset, |
4e8a9624 | 7915 | filename_ptr, functionname_ptr, |
fb167eb2 AM |
7916 | line_ptr, discriminator_ptr, |
7917 | dwarf_debug_sections, 0, | |
e00e8198 AM |
7918 | &elf_tdata (abfd)->dwarf2_find_line_info) |
7919 | || _bfd_dwarf1_find_nearest_line (abfd, symbols, section, offset, | |
7920 | filename_ptr, functionname_ptr, | |
7921 | line_ptr)) | |
d1fad7c6 NC |
7922 | { |
7923 | if (!*functionname_ptr) | |
e00e8198 AM |
7924 | _bfd_elf_find_function (abfd, symbols, section, offset, |
7925 | *filename_ptr ? NULL : filename_ptr, | |
7926 | functionname_ptr); | |
b34976b6 | 7927 | return TRUE; |
d1fad7c6 NC |
7928 | } |
7929 | ||
7930 | if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset, | |
4e8a9624 AM |
7931 | &found, filename_ptr, |
7932 | functionname_ptr, line_ptr, | |
7933 | &elf_tdata (abfd)->line_info)) | |
b34976b6 | 7934 | return FALSE; |
dc43ada5 | 7935 | if (found && (*functionname_ptr || *line_ptr)) |
b34976b6 | 7936 | return TRUE; |
d1fad7c6 NC |
7937 | |
7938 | if (symbols == NULL) | |
b34976b6 | 7939 | return FALSE; |
d1fad7c6 | 7940 | |
e00e8198 AM |
7941 | if (! _bfd_elf_find_function (abfd, symbols, section, offset, |
7942 | filename_ptr, functionname_ptr)) | |
b34976b6 | 7943 | return FALSE; |
d1fad7c6 | 7944 | |
252b5132 | 7945 | *line_ptr = 0; |
b34976b6 | 7946 | return TRUE; |
252b5132 RH |
7947 | } |
7948 | ||
5420f73d L |
7949 | /* Find the line for a symbol. */ |
7950 | ||
7951 | bfd_boolean | |
7952 | _bfd_elf_find_line (bfd *abfd, asymbol **symbols, asymbol *symbol, | |
7953 | const char **filename_ptr, unsigned int *line_ptr) | |
9b8d1a36 | 7954 | { |
fb167eb2 AM |
7955 | return _bfd_dwarf2_find_nearest_line (abfd, symbols, symbol, NULL, 0, |
7956 | filename_ptr, NULL, line_ptr, NULL, | |
7957 | dwarf_debug_sections, 0, | |
7958 | &elf_tdata (abfd)->dwarf2_find_line_info); | |
5420f73d L |
7959 | } |
7960 | ||
4ab527b0 FF |
7961 | /* After a call to bfd_find_nearest_line, successive calls to |
7962 | bfd_find_inliner_info can be used to get source information about | |
7963 | each level of function inlining that terminated at the address | |
7964 | passed to bfd_find_nearest_line. Currently this is only supported | |
7965 | for DWARF2 with appropriate DWARF3 extensions. */ | |
7966 | ||
7967 | bfd_boolean | |
7968 | _bfd_elf_find_inliner_info (bfd *abfd, | |
7969 | const char **filename_ptr, | |
7970 | const char **functionname_ptr, | |
7971 | unsigned int *line_ptr) | |
7972 | { | |
7973 | bfd_boolean found; | |
7974 | found = _bfd_dwarf2_find_inliner_info (abfd, filename_ptr, | |
7975 | functionname_ptr, line_ptr, | |
7976 | & elf_tdata (abfd)->dwarf2_find_line_info); | |
7977 | return found; | |
7978 | } | |
7979 | ||
252b5132 | 7980 | int |
a6b96beb | 7981 | _bfd_elf_sizeof_headers (bfd *abfd, struct bfd_link_info *info) |
252b5132 | 7982 | { |
8ded5a0f AM |
7983 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
7984 | int ret = bed->s->sizeof_ehdr; | |
252b5132 | 7985 | |
a6b96beb | 7986 | if (!info->relocatable) |
8ded5a0f | 7987 | { |
12bd6957 | 7988 | bfd_size_type phdr_size = elf_program_header_size (abfd); |
8ded5a0f | 7989 | |
62d7a5f6 AM |
7990 | if (phdr_size == (bfd_size_type) -1) |
7991 | { | |
7992 | struct elf_segment_map *m; | |
7993 | ||
7994 | phdr_size = 0; | |
12bd6957 | 7995 | for (m = elf_seg_map (abfd); m != NULL; m = m->next) |
62d7a5f6 | 7996 | phdr_size += bed->s->sizeof_phdr; |
8ded5a0f | 7997 | |
62d7a5f6 AM |
7998 | if (phdr_size == 0) |
7999 | phdr_size = get_program_header_size (abfd, info); | |
8000 | } | |
8ded5a0f | 8001 | |
12bd6957 | 8002 | elf_program_header_size (abfd) = phdr_size; |
8ded5a0f AM |
8003 | ret += phdr_size; |
8004 | } | |
8005 | ||
252b5132 RH |
8006 | return ret; |
8007 | } | |
8008 | ||
b34976b6 | 8009 | bfd_boolean |
217aa764 AM |
8010 | _bfd_elf_set_section_contents (bfd *abfd, |
8011 | sec_ptr section, | |
0f867abe | 8012 | const void *location, |
217aa764 AM |
8013 | file_ptr offset, |
8014 | bfd_size_type count) | |
252b5132 RH |
8015 | { |
8016 | Elf_Internal_Shdr *hdr; | |
1b6aeedb | 8017 | file_ptr pos; |
252b5132 RH |
8018 | |
8019 | if (! abfd->output_has_begun | |
217aa764 | 8020 | && ! _bfd_elf_compute_section_file_positions (abfd, NULL)) |
b34976b6 | 8021 | return FALSE; |
252b5132 | 8022 | |
0ce398f1 L |
8023 | if (!count) |
8024 | return TRUE; | |
8025 | ||
252b5132 | 8026 | hdr = &elf_section_data (section)->this_hdr; |
0ce398f1 L |
8027 | if (hdr->sh_offset == (file_ptr) -1) |
8028 | { | |
8029 | /* We must compress this section. Write output to the buffer. */ | |
8030 | unsigned char *contents = hdr->contents; | |
8031 | if ((offset + count) > hdr->sh_size | |
8032 | || (section->flags & SEC_ELF_COMPRESS) == 0 | |
8033 | || contents == NULL) | |
8034 | abort (); | |
8035 | memcpy (contents + offset, location, count); | |
8036 | return TRUE; | |
8037 | } | |
dc810e39 AM |
8038 | pos = hdr->sh_offset + offset; |
8039 | if (bfd_seek (abfd, pos, SEEK_SET) != 0 | |
8040 | || bfd_bwrite (location, count, abfd) != count) | |
b34976b6 | 8041 | return FALSE; |
252b5132 | 8042 | |
b34976b6 | 8043 | return TRUE; |
252b5132 RH |
8044 | } |
8045 | ||
8046 | void | |
217aa764 AM |
8047 | _bfd_elf_no_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED, |
8048 | arelent *cache_ptr ATTRIBUTE_UNUSED, | |
8049 | Elf_Internal_Rela *dst ATTRIBUTE_UNUSED) | |
252b5132 RH |
8050 | { |
8051 | abort (); | |
8052 | } | |
8053 | ||
252b5132 RH |
8054 | /* Try to convert a non-ELF reloc into an ELF one. */ |
8055 | ||
b34976b6 | 8056 | bfd_boolean |
217aa764 | 8057 | _bfd_elf_validate_reloc (bfd *abfd, arelent *areloc) |
252b5132 | 8058 | { |
c044fabd | 8059 | /* Check whether we really have an ELF howto. */ |
252b5132 RH |
8060 | |
8061 | if ((*areloc->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec) | |
8062 | { | |
8063 | bfd_reloc_code_real_type code; | |
8064 | reloc_howto_type *howto; | |
8065 | ||
8066 | /* Alien reloc: Try to determine its type to replace it with an | |
c044fabd | 8067 | equivalent ELF reloc. */ |
252b5132 RH |
8068 | |
8069 | if (areloc->howto->pc_relative) | |
8070 | { | |
8071 | switch (areloc->howto->bitsize) | |
8072 | { | |
8073 | case 8: | |
8074 | code = BFD_RELOC_8_PCREL; | |
8075 | break; | |
8076 | case 12: | |
8077 | code = BFD_RELOC_12_PCREL; | |
8078 | break; | |
8079 | case 16: | |
8080 | code = BFD_RELOC_16_PCREL; | |
8081 | break; | |
8082 | case 24: | |
8083 | code = BFD_RELOC_24_PCREL; | |
8084 | break; | |
8085 | case 32: | |
8086 | code = BFD_RELOC_32_PCREL; | |
8087 | break; | |
8088 | case 64: | |
8089 | code = BFD_RELOC_64_PCREL; | |
8090 | break; | |
8091 | default: | |
8092 | goto fail; | |
8093 | } | |
8094 | ||
8095 | howto = bfd_reloc_type_lookup (abfd, code); | |
8096 | ||
8097 | if (areloc->howto->pcrel_offset != howto->pcrel_offset) | |
8098 | { | |
8099 | if (howto->pcrel_offset) | |
8100 | areloc->addend += areloc->address; | |
8101 | else | |
8102 | areloc->addend -= areloc->address; /* addend is unsigned!! */ | |
8103 | } | |
8104 | } | |
8105 | else | |
8106 | { | |
8107 | switch (areloc->howto->bitsize) | |
8108 | { | |
8109 | case 8: | |
8110 | code = BFD_RELOC_8; | |
8111 | break; | |
8112 | case 14: | |
8113 | code = BFD_RELOC_14; | |
8114 | break; | |
8115 | case 16: | |
8116 | code = BFD_RELOC_16; | |
8117 | break; | |
8118 | case 26: | |
8119 | code = BFD_RELOC_26; | |
8120 | break; | |
8121 | case 32: | |
8122 | code = BFD_RELOC_32; | |
8123 | break; | |
8124 | case 64: | |
8125 | code = BFD_RELOC_64; | |
8126 | break; | |
8127 | default: | |
8128 | goto fail; | |
8129 | } | |
8130 | ||
8131 | howto = bfd_reloc_type_lookup (abfd, code); | |
8132 | } | |
8133 | ||
8134 | if (howto) | |
8135 | areloc->howto = howto; | |
8136 | else | |
8137 | goto fail; | |
8138 | } | |
8139 | ||
b34976b6 | 8140 | return TRUE; |
252b5132 RH |
8141 | |
8142 | fail: | |
8143 | (*_bfd_error_handler) | |
d003868e AM |
8144 | (_("%B: unsupported relocation type %s"), |
8145 | abfd, areloc->howto->name); | |
252b5132 | 8146 | bfd_set_error (bfd_error_bad_value); |
b34976b6 | 8147 | return FALSE; |
252b5132 RH |
8148 | } |
8149 | ||
b34976b6 | 8150 | bfd_boolean |
217aa764 | 8151 | _bfd_elf_close_and_cleanup (bfd *abfd) |
252b5132 | 8152 | { |
d9071b0c TG |
8153 | struct elf_obj_tdata *tdata = elf_tdata (abfd); |
8154 | if (bfd_get_format (abfd) == bfd_object && tdata != NULL) | |
252b5132 | 8155 | { |
c0355132 | 8156 | if (elf_tdata (abfd)->o != NULL && elf_shstrtab (abfd) != NULL) |
2b0f7ef9 | 8157 | _bfd_elf_strtab_free (elf_shstrtab (abfd)); |
d9071b0c | 8158 | _bfd_dwarf2_cleanup_debug_info (abfd, &tdata->dwarf2_find_line_info); |
252b5132 RH |
8159 | } |
8160 | ||
8161 | return _bfd_generic_close_and_cleanup (abfd); | |
8162 | } | |
8163 | ||
8164 | /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY | |
8165 | in the relocation's offset. Thus we cannot allow any sort of sanity | |
8166 | range-checking to interfere. There is nothing else to do in processing | |
8167 | this reloc. */ | |
8168 | ||
8169 | bfd_reloc_status_type | |
217aa764 AM |
8170 | _bfd_elf_rel_vtable_reloc_fn |
8171 | (bfd *abfd ATTRIBUTE_UNUSED, arelent *re ATTRIBUTE_UNUSED, | |
fc0a2244 | 8172 | struct bfd_symbol *symbol ATTRIBUTE_UNUSED, |
217aa764 AM |
8173 | void *data ATTRIBUTE_UNUSED, asection *is ATTRIBUTE_UNUSED, |
8174 | bfd *obfd ATTRIBUTE_UNUSED, char **errmsg ATTRIBUTE_UNUSED) | |
252b5132 RH |
8175 | { |
8176 | return bfd_reloc_ok; | |
8177 | } | |
252b5132 RH |
8178 | \f |
8179 | /* Elf core file support. Much of this only works on native | |
8180 | toolchains, since we rely on knowing the | |
8181 | machine-dependent procfs structure in order to pick | |
c044fabd | 8182 | out details about the corefile. */ |
252b5132 RH |
8183 | |
8184 | #ifdef HAVE_SYS_PROCFS_H | |
16231b7b DG |
8185 | /* Needed for new procfs interface on sparc-solaris. */ |
8186 | # define _STRUCTURED_PROC 1 | |
252b5132 RH |
8187 | # include <sys/procfs.h> |
8188 | #endif | |
8189 | ||
261b8d08 PA |
8190 | /* Return a PID that identifies a "thread" for threaded cores, or the |
8191 | PID of the main process for non-threaded cores. */ | |
252b5132 RH |
8192 | |
8193 | static int | |
217aa764 | 8194 | elfcore_make_pid (bfd *abfd) |
252b5132 | 8195 | { |
261b8d08 PA |
8196 | int pid; |
8197 | ||
228e534f | 8198 | pid = elf_tdata (abfd)->core->lwpid; |
261b8d08 | 8199 | if (pid == 0) |
228e534f | 8200 | pid = elf_tdata (abfd)->core->pid; |
261b8d08 PA |
8201 | |
8202 | return pid; | |
252b5132 RH |
8203 | } |
8204 | ||
252b5132 RH |
8205 | /* If there isn't a section called NAME, make one, using |
8206 | data from SECT. Note, this function will generate a | |
8207 | reference to NAME, so you shouldn't deallocate or | |
c044fabd | 8208 | overwrite it. */ |
252b5132 | 8209 | |
b34976b6 | 8210 | static bfd_boolean |
217aa764 | 8211 | elfcore_maybe_make_sect (bfd *abfd, char *name, asection *sect) |
252b5132 | 8212 | { |
c044fabd | 8213 | asection *sect2; |
252b5132 RH |
8214 | |
8215 | if (bfd_get_section_by_name (abfd, name) != NULL) | |
b34976b6 | 8216 | return TRUE; |
252b5132 | 8217 | |
117ed4f8 | 8218 | sect2 = bfd_make_section_with_flags (abfd, name, sect->flags); |
252b5132 | 8219 | if (sect2 == NULL) |
b34976b6 | 8220 | return FALSE; |
252b5132 | 8221 | |
eea6121a | 8222 | sect2->size = sect->size; |
252b5132 | 8223 | sect2->filepos = sect->filepos; |
252b5132 | 8224 | sect2->alignment_power = sect->alignment_power; |
b34976b6 | 8225 | return TRUE; |
252b5132 RH |
8226 | } |
8227 | ||
bb0082d6 AM |
8228 | /* Create a pseudosection containing SIZE bytes at FILEPOS. This |
8229 | actually creates up to two pseudosections: | |
8230 | - For the single-threaded case, a section named NAME, unless | |
8231 | such a section already exists. | |
8232 | - For the multi-threaded case, a section named "NAME/PID", where | |
8233 | PID is elfcore_make_pid (abfd). | |
8234 | Both pseudosections have identical contents. */ | |
b34976b6 | 8235 | bfd_boolean |
217aa764 AM |
8236 | _bfd_elfcore_make_pseudosection (bfd *abfd, |
8237 | char *name, | |
8238 | size_t size, | |
8239 | ufile_ptr filepos) | |
bb0082d6 AM |
8240 | { |
8241 | char buf[100]; | |
8242 | char *threaded_name; | |
d4c88bbb | 8243 | size_t len; |
bb0082d6 AM |
8244 | asection *sect; |
8245 | ||
8246 | /* Build the section name. */ | |
8247 | ||
8248 | sprintf (buf, "%s/%d", name, elfcore_make_pid (abfd)); | |
d4c88bbb | 8249 | len = strlen (buf) + 1; |
a50b1753 | 8250 | threaded_name = (char *) bfd_alloc (abfd, len); |
bb0082d6 | 8251 | if (threaded_name == NULL) |
b34976b6 | 8252 | return FALSE; |
d4c88bbb | 8253 | memcpy (threaded_name, buf, len); |
bb0082d6 | 8254 | |
117ed4f8 AM |
8255 | sect = bfd_make_section_anyway_with_flags (abfd, threaded_name, |
8256 | SEC_HAS_CONTENTS); | |
bb0082d6 | 8257 | if (sect == NULL) |
b34976b6 | 8258 | return FALSE; |
eea6121a | 8259 | sect->size = size; |
bb0082d6 | 8260 | sect->filepos = filepos; |
bb0082d6 AM |
8261 | sect->alignment_power = 2; |
8262 | ||
936e320b | 8263 | return elfcore_maybe_make_sect (abfd, name, sect); |
bb0082d6 AM |
8264 | } |
8265 | ||
252b5132 | 8266 | /* prstatus_t exists on: |
4a938328 | 8267 | solaris 2.5+ |
252b5132 RH |
8268 | linux 2.[01] + glibc |
8269 | unixware 4.2 | |
8270 | */ | |
8271 | ||
8272 | #if defined (HAVE_PRSTATUS_T) | |
a7b97311 | 8273 | |
b34976b6 | 8274 | static bfd_boolean |
217aa764 | 8275 | elfcore_grok_prstatus (bfd *abfd, Elf_Internal_Note *note) |
252b5132 | 8276 | { |
eea6121a | 8277 | size_t size; |
7ee38065 | 8278 | int offset; |
252b5132 | 8279 | |
4a938328 MS |
8280 | if (note->descsz == sizeof (prstatus_t)) |
8281 | { | |
8282 | prstatus_t prstat; | |
252b5132 | 8283 | |
eea6121a | 8284 | size = sizeof (prstat.pr_reg); |
7ee38065 | 8285 | offset = offsetof (prstatus_t, pr_reg); |
4a938328 | 8286 | memcpy (&prstat, note->descdata, sizeof (prstat)); |
252b5132 | 8287 | |
fa49d224 NC |
8288 | /* Do not overwrite the core signal if it |
8289 | has already been set by another thread. */ | |
228e534f AM |
8290 | if (elf_tdata (abfd)->core->signal == 0) |
8291 | elf_tdata (abfd)->core->signal = prstat.pr_cursig; | |
8292 | if (elf_tdata (abfd)->core->pid == 0) | |
8293 | elf_tdata (abfd)->core->pid = prstat.pr_pid; | |
252b5132 | 8294 | |
4a938328 MS |
8295 | /* pr_who exists on: |
8296 | solaris 2.5+ | |
8297 | unixware 4.2 | |
8298 | pr_who doesn't exist on: | |
8299 | linux 2.[01] | |
8300 | */ | |
252b5132 | 8301 | #if defined (HAVE_PRSTATUS_T_PR_WHO) |
228e534f | 8302 | elf_tdata (abfd)->core->lwpid = prstat.pr_who; |
261b8d08 | 8303 | #else |
228e534f | 8304 | elf_tdata (abfd)->core->lwpid = prstat.pr_pid; |
252b5132 | 8305 | #endif |
4a938328 | 8306 | } |
7ee38065 | 8307 | #if defined (HAVE_PRSTATUS32_T) |
4a938328 MS |
8308 | else if (note->descsz == sizeof (prstatus32_t)) |
8309 | { | |
8310 | /* 64-bit host, 32-bit corefile */ | |
8311 | prstatus32_t prstat; | |
8312 | ||
eea6121a | 8313 | size = sizeof (prstat.pr_reg); |
7ee38065 | 8314 | offset = offsetof (prstatus32_t, pr_reg); |
4a938328 MS |
8315 | memcpy (&prstat, note->descdata, sizeof (prstat)); |
8316 | ||
fa49d224 NC |
8317 | /* Do not overwrite the core signal if it |
8318 | has already been set by another thread. */ | |
228e534f AM |
8319 | if (elf_tdata (abfd)->core->signal == 0) |
8320 | elf_tdata (abfd)->core->signal = prstat.pr_cursig; | |
8321 | if (elf_tdata (abfd)->core->pid == 0) | |
8322 | elf_tdata (abfd)->core->pid = prstat.pr_pid; | |
4a938328 MS |
8323 | |
8324 | /* pr_who exists on: | |
8325 | solaris 2.5+ | |
8326 | unixware 4.2 | |
8327 | pr_who doesn't exist on: | |
8328 | linux 2.[01] | |
8329 | */ | |
7ee38065 | 8330 | #if defined (HAVE_PRSTATUS32_T_PR_WHO) |
228e534f | 8331 | elf_tdata (abfd)->core->lwpid = prstat.pr_who; |
261b8d08 | 8332 | #else |
228e534f | 8333 | elf_tdata (abfd)->core->lwpid = prstat.pr_pid; |
4a938328 MS |
8334 | #endif |
8335 | } | |
7ee38065 | 8336 | #endif /* HAVE_PRSTATUS32_T */ |
4a938328 MS |
8337 | else |
8338 | { | |
8339 | /* Fail - we don't know how to handle any other | |
8340 | note size (ie. data object type). */ | |
b34976b6 | 8341 | return TRUE; |
4a938328 | 8342 | } |
252b5132 | 8343 | |
bb0082d6 | 8344 | /* Make a ".reg/999" section and a ".reg" section. */ |
936e320b | 8345 | return _bfd_elfcore_make_pseudosection (abfd, ".reg", |
eea6121a | 8346 | size, note->descpos + offset); |
252b5132 RH |
8347 | } |
8348 | #endif /* defined (HAVE_PRSTATUS_T) */ | |
8349 | ||
bb0082d6 | 8350 | /* Create a pseudosection containing the exact contents of NOTE. */ |
b34976b6 | 8351 | static bfd_boolean |
217aa764 AM |
8352 | elfcore_make_note_pseudosection (bfd *abfd, |
8353 | char *name, | |
8354 | Elf_Internal_Note *note) | |
252b5132 | 8355 | { |
936e320b AM |
8356 | return _bfd_elfcore_make_pseudosection (abfd, name, |
8357 | note->descsz, note->descpos); | |
252b5132 RH |
8358 | } |
8359 | ||
ff08c6bb JB |
8360 | /* There isn't a consistent prfpregset_t across platforms, |
8361 | but it doesn't matter, because we don't have to pick this | |
c044fabd KH |
8362 | data structure apart. */ |
8363 | ||
b34976b6 | 8364 | static bfd_boolean |
217aa764 | 8365 | elfcore_grok_prfpreg (bfd *abfd, Elf_Internal_Note *note) |
ff08c6bb JB |
8366 | { |
8367 | return elfcore_make_note_pseudosection (abfd, ".reg2", note); | |
8368 | } | |
8369 | ||
ff08c6bb | 8370 | /* Linux dumps the Intel SSE regs in a note named "LINUX" with a note |
971d4640 | 8371 | type of NT_PRXFPREG. Just include the whole note's contents |
ff08c6bb | 8372 | literally. */ |
c044fabd | 8373 | |
b34976b6 | 8374 | static bfd_boolean |
217aa764 | 8375 | elfcore_grok_prxfpreg (bfd *abfd, Elf_Internal_Note *note) |
ff08c6bb JB |
8376 | { |
8377 | return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note); | |
8378 | } | |
8379 | ||
4339cae0 L |
8380 | /* Linux dumps the Intel XSAVE extended state in a note named "LINUX" |
8381 | with a note type of NT_X86_XSTATE. Just include the whole note's | |
8382 | contents literally. */ | |
8383 | ||
8384 | static bfd_boolean | |
8385 | elfcore_grok_xstatereg (bfd *abfd, Elf_Internal_Note *note) | |
8386 | { | |
8387 | return elfcore_make_note_pseudosection (abfd, ".reg-xstate", note); | |
8388 | } | |
8389 | ||
97753bd5 AM |
8390 | static bfd_boolean |
8391 | elfcore_grok_ppc_vmx (bfd *abfd, Elf_Internal_Note *note) | |
8392 | { | |
8393 | return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vmx", note); | |
8394 | } | |
8395 | ||
89eeb0bc LM |
8396 | static bfd_boolean |
8397 | elfcore_grok_ppc_vsx (bfd *abfd, Elf_Internal_Note *note) | |
8398 | { | |
8399 | return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vsx", note); | |
8400 | } | |
97753bd5 | 8401 | |
0675e188 UW |
8402 | static bfd_boolean |
8403 | elfcore_grok_s390_high_gprs (bfd *abfd, Elf_Internal_Note *note) | |
8404 | { | |
8405 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-high-gprs", note); | |
8406 | } | |
8407 | ||
d7eeb400 MS |
8408 | static bfd_boolean |
8409 | elfcore_grok_s390_timer (bfd *abfd, Elf_Internal_Note *note) | |
8410 | { | |
8411 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-timer", note); | |
8412 | } | |
8413 | ||
8414 | static bfd_boolean | |
8415 | elfcore_grok_s390_todcmp (bfd *abfd, Elf_Internal_Note *note) | |
8416 | { | |
8417 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-todcmp", note); | |
8418 | } | |
8419 | ||
8420 | static bfd_boolean | |
8421 | elfcore_grok_s390_todpreg (bfd *abfd, Elf_Internal_Note *note) | |
8422 | { | |
8423 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-todpreg", note); | |
8424 | } | |
8425 | ||
8426 | static bfd_boolean | |
8427 | elfcore_grok_s390_ctrs (bfd *abfd, Elf_Internal_Note *note) | |
8428 | { | |
8429 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-ctrs", note); | |
8430 | } | |
8431 | ||
8432 | static bfd_boolean | |
8433 | elfcore_grok_s390_prefix (bfd *abfd, Elf_Internal_Note *note) | |
8434 | { | |
8435 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-prefix", note); | |
8436 | } | |
8437 | ||
355b81d9 UW |
8438 | static bfd_boolean |
8439 | elfcore_grok_s390_last_break (bfd *abfd, Elf_Internal_Note *note) | |
8440 | { | |
8441 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-last-break", note); | |
8442 | } | |
8443 | ||
8444 | static bfd_boolean | |
8445 | elfcore_grok_s390_system_call (bfd *abfd, Elf_Internal_Note *note) | |
8446 | { | |
8447 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-system-call", note); | |
8448 | } | |
8449 | ||
abb3f6cc NC |
8450 | static bfd_boolean |
8451 | elfcore_grok_s390_tdb (bfd *abfd, Elf_Internal_Note *note) | |
8452 | { | |
8453 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-tdb", note); | |
8454 | } | |
8455 | ||
4ef9f41a AA |
8456 | static bfd_boolean |
8457 | elfcore_grok_s390_vxrs_low (bfd *abfd, Elf_Internal_Note *note) | |
8458 | { | |
8459 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-vxrs-low", note); | |
8460 | } | |
8461 | ||
8462 | static bfd_boolean | |
8463 | elfcore_grok_s390_vxrs_high (bfd *abfd, Elf_Internal_Note *note) | |
8464 | { | |
8465 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-vxrs-high", note); | |
8466 | } | |
8467 | ||
faa9a424 UW |
8468 | static bfd_boolean |
8469 | elfcore_grok_arm_vfp (bfd *abfd, Elf_Internal_Note *note) | |
8470 | { | |
8471 | return elfcore_make_note_pseudosection (abfd, ".reg-arm-vfp", note); | |
8472 | } | |
8473 | ||
652451f8 YZ |
8474 | static bfd_boolean |
8475 | elfcore_grok_aarch_tls (bfd *abfd, Elf_Internal_Note *note) | |
8476 | { | |
8477 | return elfcore_make_note_pseudosection (abfd, ".reg-aarch-tls", note); | |
8478 | } | |
8479 | ||
8480 | static bfd_boolean | |
8481 | elfcore_grok_aarch_hw_break (bfd *abfd, Elf_Internal_Note *note) | |
8482 | { | |
8483 | return elfcore_make_note_pseudosection (abfd, ".reg-aarch-hw-break", note); | |
8484 | } | |
8485 | ||
8486 | static bfd_boolean | |
8487 | elfcore_grok_aarch_hw_watch (bfd *abfd, Elf_Internal_Note *note) | |
8488 | { | |
8489 | return elfcore_make_note_pseudosection (abfd, ".reg-aarch-hw-watch", note); | |
8490 | } | |
8491 | ||
252b5132 | 8492 | #if defined (HAVE_PRPSINFO_T) |
4a938328 | 8493 | typedef prpsinfo_t elfcore_psinfo_t; |
7ee38065 | 8494 | #if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */ |
4a938328 MS |
8495 | typedef prpsinfo32_t elfcore_psinfo32_t; |
8496 | #endif | |
252b5132 RH |
8497 | #endif |
8498 | ||
8499 | #if defined (HAVE_PSINFO_T) | |
4a938328 | 8500 | typedef psinfo_t elfcore_psinfo_t; |
7ee38065 | 8501 | #if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */ |
4a938328 MS |
8502 | typedef psinfo32_t elfcore_psinfo32_t; |
8503 | #endif | |
252b5132 RH |
8504 | #endif |
8505 | ||
252b5132 RH |
8506 | /* return a malloc'ed copy of a string at START which is at |
8507 | most MAX bytes long, possibly without a terminating '\0'. | |
c044fabd | 8508 | the copy will always have a terminating '\0'. */ |
252b5132 | 8509 | |
936e320b | 8510 | char * |
217aa764 | 8511 | _bfd_elfcore_strndup (bfd *abfd, char *start, size_t max) |
252b5132 | 8512 | { |
dc810e39 | 8513 | char *dups; |
a50b1753 | 8514 | char *end = (char *) memchr (start, '\0', max); |
dc810e39 | 8515 | size_t len; |
252b5132 RH |
8516 | |
8517 | if (end == NULL) | |
8518 | len = max; | |
8519 | else | |
8520 | len = end - start; | |
8521 | ||
a50b1753 | 8522 | dups = (char *) bfd_alloc (abfd, len + 1); |
dc810e39 | 8523 | if (dups == NULL) |
252b5132 RH |
8524 | return NULL; |
8525 | ||
dc810e39 AM |
8526 | memcpy (dups, start, len); |
8527 | dups[len] = '\0'; | |
252b5132 | 8528 | |
dc810e39 | 8529 | return dups; |
252b5132 RH |
8530 | } |
8531 | ||
bb0082d6 | 8532 | #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) |
b34976b6 | 8533 | static bfd_boolean |
217aa764 | 8534 | elfcore_grok_psinfo (bfd *abfd, Elf_Internal_Note *note) |
252b5132 | 8535 | { |
4a938328 MS |
8536 | if (note->descsz == sizeof (elfcore_psinfo_t)) |
8537 | { | |
8538 | elfcore_psinfo_t psinfo; | |
252b5132 | 8539 | |
7ee38065 | 8540 | memcpy (&psinfo, note->descdata, sizeof (psinfo)); |
252b5132 | 8541 | |
335e41d4 | 8542 | #if defined (HAVE_PSINFO_T_PR_PID) || defined (HAVE_PRPSINFO_T_PR_PID) |
228e534f | 8543 | elf_tdata (abfd)->core->pid = psinfo.pr_pid; |
335e41d4 | 8544 | #endif |
228e534f | 8545 | elf_tdata (abfd)->core->program |
936e320b AM |
8546 | = _bfd_elfcore_strndup (abfd, psinfo.pr_fname, |
8547 | sizeof (psinfo.pr_fname)); | |
252b5132 | 8548 | |
228e534f | 8549 | elf_tdata (abfd)->core->command |
936e320b AM |
8550 | = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs, |
8551 | sizeof (psinfo.pr_psargs)); | |
4a938328 | 8552 | } |
7ee38065 | 8553 | #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T) |
4a938328 MS |
8554 | else if (note->descsz == sizeof (elfcore_psinfo32_t)) |
8555 | { | |
8556 | /* 64-bit host, 32-bit corefile */ | |
8557 | elfcore_psinfo32_t psinfo; | |
8558 | ||
7ee38065 | 8559 | memcpy (&psinfo, note->descdata, sizeof (psinfo)); |
252b5132 | 8560 | |
335e41d4 | 8561 | #if defined (HAVE_PSINFO32_T_PR_PID) || defined (HAVE_PRPSINFO32_T_PR_PID) |
228e534f | 8562 | elf_tdata (abfd)->core->pid = psinfo.pr_pid; |
335e41d4 | 8563 | #endif |
228e534f | 8564 | elf_tdata (abfd)->core->program |
936e320b AM |
8565 | = _bfd_elfcore_strndup (abfd, psinfo.pr_fname, |
8566 | sizeof (psinfo.pr_fname)); | |
4a938328 | 8567 | |
228e534f | 8568 | elf_tdata (abfd)->core->command |
936e320b AM |
8569 | = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs, |
8570 | sizeof (psinfo.pr_psargs)); | |
4a938328 MS |
8571 | } |
8572 | #endif | |
8573 | ||
8574 | else | |
8575 | { | |
8576 | /* Fail - we don't know how to handle any other | |
8577 | note size (ie. data object type). */ | |
b34976b6 | 8578 | return TRUE; |
4a938328 | 8579 | } |
252b5132 RH |
8580 | |
8581 | /* Note that for some reason, a spurious space is tacked | |
8582 | onto the end of the args in some (at least one anyway) | |
c044fabd | 8583 | implementations, so strip it off if it exists. */ |
252b5132 RH |
8584 | |
8585 | { | |
228e534f | 8586 | char *command = elf_tdata (abfd)->core->command; |
252b5132 RH |
8587 | int n = strlen (command); |
8588 | ||
8589 | if (0 < n && command[n - 1] == ' ') | |
8590 | command[n - 1] = '\0'; | |
8591 | } | |
8592 | ||
b34976b6 | 8593 | return TRUE; |
252b5132 RH |
8594 | } |
8595 | #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */ | |
8596 | ||
252b5132 | 8597 | #if defined (HAVE_PSTATUS_T) |
b34976b6 | 8598 | static bfd_boolean |
217aa764 | 8599 | elfcore_grok_pstatus (bfd *abfd, Elf_Internal_Note *note) |
252b5132 | 8600 | { |
f572a39d AM |
8601 | if (note->descsz == sizeof (pstatus_t) |
8602 | #if defined (HAVE_PXSTATUS_T) | |
8603 | || note->descsz == sizeof (pxstatus_t) | |
8604 | #endif | |
8605 | ) | |
4a938328 MS |
8606 | { |
8607 | pstatus_t pstat; | |
252b5132 | 8608 | |
4a938328 | 8609 | memcpy (&pstat, note->descdata, sizeof (pstat)); |
252b5132 | 8610 | |
228e534f | 8611 | elf_tdata (abfd)->core->pid = pstat.pr_pid; |
4a938328 | 8612 | } |
7ee38065 | 8613 | #if defined (HAVE_PSTATUS32_T) |
4a938328 MS |
8614 | else if (note->descsz == sizeof (pstatus32_t)) |
8615 | { | |
8616 | /* 64-bit host, 32-bit corefile */ | |
8617 | pstatus32_t pstat; | |
252b5132 | 8618 | |
4a938328 | 8619 | memcpy (&pstat, note->descdata, sizeof (pstat)); |
252b5132 | 8620 | |
228e534f | 8621 | elf_tdata (abfd)->core->pid = pstat.pr_pid; |
4a938328 MS |
8622 | } |
8623 | #endif | |
252b5132 RH |
8624 | /* Could grab some more details from the "representative" |
8625 | lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an | |
c044fabd | 8626 | NT_LWPSTATUS note, presumably. */ |
252b5132 | 8627 | |
b34976b6 | 8628 | return TRUE; |
252b5132 RH |
8629 | } |
8630 | #endif /* defined (HAVE_PSTATUS_T) */ | |
8631 | ||
252b5132 | 8632 | #if defined (HAVE_LWPSTATUS_T) |
b34976b6 | 8633 | static bfd_boolean |
217aa764 | 8634 | elfcore_grok_lwpstatus (bfd *abfd, Elf_Internal_Note *note) |
252b5132 RH |
8635 | { |
8636 | lwpstatus_t lwpstat; | |
8637 | char buf[100]; | |
c044fabd | 8638 | char *name; |
d4c88bbb | 8639 | size_t len; |
c044fabd | 8640 | asection *sect; |
252b5132 | 8641 | |
f572a39d AM |
8642 | if (note->descsz != sizeof (lwpstat) |
8643 | #if defined (HAVE_LWPXSTATUS_T) | |
8644 | && note->descsz != sizeof (lwpxstatus_t) | |
8645 | #endif | |
8646 | ) | |
b34976b6 | 8647 | return TRUE; |
252b5132 RH |
8648 | |
8649 | memcpy (&lwpstat, note->descdata, sizeof (lwpstat)); | |
8650 | ||
228e534f | 8651 | elf_tdata (abfd)->core->lwpid = lwpstat.pr_lwpid; |
a1504221 JB |
8652 | /* Do not overwrite the core signal if it has already been set by |
8653 | another thread. */ | |
228e534f AM |
8654 | if (elf_tdata (abfd)->core->signal == 0) |
8655 | elf_tdata (abfd)->core->signal = lwpstat.pr_cursig; | |
252b5132 | 8656 | |
c044fabd | 8657 | /* Make a ".reg/999" section. */ |
252b5132 RH |
8658 | |
8659 | sprintf (buf, ".reg/%d", elfcore_make_pid (abfd)); | |
d4c88bbb | 8660 | len = strlen (buf) + 1; |
217aa764 | 8661 | name = bfd_alloc (abfd, len); |
252b5132 | 8662 | if (name == NULL) |
b34976b6 | 8663 | return FALSE; |
d4c88bbb | 8664 | memcpy (name, buf, len); |
252b5132 | 8665 | |
117ed4f8 | 8666 | sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); |
252b5132 | 8667 | if (sect == NULL) |
b34976b6 | 8668 | return FALSE; |
252b5132 RH |
8669 | |
8670 | #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT) | |
eea6121a | 8671 | sect->size = sizeof (lwpstat.pr_context.uc_mcontext.gregs); |
252b5132 RH |
8672 | sect->filepos = note->descpos |
8673 | + offsetof (lwpstatus_t, pr_context.uc_mcontext.gregs); | |
8674 | #endif | |
8675 | ||
8676 | #if defined (HAVE_LWPSTATUS_T_PR_REG) | |
eea6121a | 8677 | sect->size = sizeof (lwpstat.pr_reg); |
252b5132 RH |
8678 | sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_reg); |
8679 | #endif | |
8680 | ||
252b5132 RH |
8681 | sect->alignment_power = 2; |
8682 | ||
8683 | if (!elfcore_maybe_make_sect (abfd, ".reg", sect)) | |
b34976b6 | 8684 | return FALSE; |
252b5132 RH |
8685 | |
8686 | /* Make a ".reg2/999" section */ | |
8687 | ||
8688 | sprintf (buf, ".reg2/%d", elfcore_make_pid (abfd)); | |
d4c88bbb | 8689 | len = strlen (buf) + 1; |
217aa764 | 8690 | name = bfd_alloc (abfd, len); |
252b5132 | 8691 | if (name == NULL) |
b34976b6 | 8692 | return FALSE; |
d4c88bbb | 8693 | memcpy (name, buf, len); |
252b5132 | 8694 | |
117ed4f8 | 8695 | sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); |
252b5132 | 8696 | if (sect == NULL) |
b34976b6 | 8697 | return FALSE; |
252b5132 RH |
8698 | |
8699 | #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT) | |
eea6121a | 8700 | sect->size = sizeof (lwpstat.pr_context.uc_mcontext.fpregs); |
252b5132 RH |
8701 | sect->filepos = note->descpos |
8702 | + offsetof (lwpstatus_t, pr_context.uc_mcontext.fpregs); | |
8703 | #endif | |
8704 | ||
8705 | #if defined (HAVE_LWPSTATUS_T_PR_FPREG) | |
eea6121a | 8706 | sect->size = sizeof (lwpstat.pr_fpreg); |
252b5132 RH |
8707 | sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_fpreg); |
8708 | #endif | |
8709 | ||
252b5132 RH |
8710 | sect->alignment_power = 2; |
8711 | ||
936e320b | 8712 | return elfcore_maybe_make_sect (abfd, ".reg2", sect); |
252b5132 RH |
8713 | } |
8714 | #endif /* defined (HAVE_LWPSTATUS_T) */ | |
8715 | ||
b34976b6 | 8716 | static bfd_boolean |
217aa764 | 8717 | elfcore_grok_win32pstatus (bfd *abfd, Elf_Internal_Note *note) |
16e9c715 NC |
8718 | { |
8719 | char buf[30]; | |
c044fabd | 8720 | char *name; |
d4c88bbb | 8721 | size_t len; |
c044fabd | 8722 | asection *sect; |
4a6636fb PA |
8723 | int type; |
8724 | int is_active_thread; | |
8725 | bfd_vma base_addr; | |
16e9c715 | 8726 | |
4a6636fb | 8727 | if (note->descsz < 728) |
b34976b6 | 8728 | return TRUE; |
16e9c715 | 8729 | |
4a6636fb PA |
8730 | if (! CONST_STRNEQ (note->namedata, "win32")) |
8731 | return TRUE; | |
8732 | ||
8733 | type = bfd_get_32 (abfd, note->descdata); | |
c044fabd | 8734 | |
4a6636fb | 8735 | switch (type) |
16e9c715 | 8736 | { |
4a6636fb | 8737 | case 1 /* NOTE_INFO_PROCESS */: |
228e534f | 8738 | /* FIXME: need to add ->core->command. */ |
4a6636fb | 8739 | /* process_info.pid */ |
228e534f | 8740 | elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, note->descdata + 8); |
4a6636fb | 8741 | /* process_info.signal */ |
228e534f | 8742 | elf_tdata (abfd)->core->signal = bfd_get_32 (abfd, note->descdata + 12); |
c044fabd | 8743 | break; |
16e9c715 | 8744 | |
4a6636fb | 8745 | case 2 /* NOTE_INFO_THREAD */: |
16e9c715 | 8746 | /* Make a ".reg/999" section. */ |
4a6636fb PA |
8747 | /* thread_info.tid */ |
8748 | sprintf (buf, ".reg/%ld", (long) bfd_get_32 (abfd, note->descdata + 8)); | |
c044fabd | 8749 | |
d4c88bbb | 8750 | len = strlen (buf) + 1; |
a50b1753 | 8751 | name = (char *) bfd_alloc (abfd, len); |
16e9c715 | 8752 | if (name == NULL) |
b34976b6 | 8753 | return FALSE; |
c044fabd | 8754 | |
d4c88bbb | 8755 | memcpy (name, buf, len); |
16e9c715 | 8756 | |
117ed4f8 | 8757 | sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); |
16e9c715 | 8758 | if (sect == NULL) |
b34976b6 | 8759 | return FALSE; |
c044fabd | 8760 | |
4a6636fb PA |
8761 | /* sizeof (thread_info.thread_context) */ |
8762 | sect->size = 716; | |
8763 | /* offsetof (thread_info.thread_context) */ | |
8764 | sect->filepos = note->descpos + 12; | |
16e9c715 NC |
8765 | sect->alignment_power = 2; |
8766 | ||
4a6636fb PA |
8767 | /* thread_info.is_active_thread */ |
8768 | is_active_thread = bfd_get_32 (abfd, note->descdata + 8); | |
8769 | ||
8770 | if (is_active_thread) | |
16e9c715 | 8771 | if (! elfcore_maybe_make_sect (abfd, ".reg", sect)) |
b34976b6 | 8772 | return FALSE; |
16e9c715 NC |
8773 | break; |
8774 | ||
4a6636fb | 8775 | case 3 /* NOTE_INFO_MODULE */: |
16e9c715 | 8776 | /* Make a ".module/xxxxxxxx" section. */ |
4a6636fb PA |
8777 | /* module_info.base_address */ |
8778 | base_addr = bfd_get_32 (abfd, note->descdata + 4); | |
0af1713e | 8779 | sprintf (buf, ".module/%08lx", (unsigned long) base_addr); |
c044fabd | 8780 | |
d4c88bbb | 8781 | len = strlen (buf) + 1; |
a50b1753 | 8782 | name = (char *) bfd_alloc (abfd, len); |
16e9c715 | 8783 | if (name == NULL) |
b34976b6 | 8784 | return FALSE; |
c044fabd | 8785 | |
d4c88bbb | 8786 | memcpy (name, buf, len); |
252b5132 | 8787 | |
117ed4f8 | 8788 | sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); |
c044fabd | 8789 | |
16e9c715 | 8790 | if (sect == NULL) |
b34976b6 | 8791 | return FALSE; |
c044fabd | 8792 | |
eea6121a | 8793 | sect->size = note->descsz; |
16e9c715 | 8794 | sect->filepos = note->descpos; |
16e9c715 NC |
8795 | sect->alignment_power = 2; |
8796 | break; | |
8797 | ||
8798 | default: | |
b34976b6 | 8799 | return TRUE; |
16e9c715 NC |
8800 | } |
8801 | ||
b34976b6 | 8802 | return TRUE; |
16e9c715 | 8803 | } |
252b5132 | 8804 | |
b34976b6 | 8805 | static bfd_boolean |
217aa764 | 8806 | elfcore_grok_note (bfd *abfd, Elf_Internal_Note *note) |
252b5132 | 8807 | { |
9c5bfbb7 | 8808 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
bb0082d6 | 8809 | |
252b5132 RH |
8810 | switch (note->type) |
8811 | { | |
8812 | default: | |
b34976b6 | 8813 | return TRUE; |
252b5132 | 8814 | |
252b5132 | 8815 | case NT_PRSTATUS: |
bb0082d6 AM |
8816 | if (bed->elf_backend_grok_prstatus) |
8817 | if ((*bed->elf_backend_grok_prstatus) (abfd, note)) | |
b34976b6 | 8818 | return TRUE; |
bb0082d6 | 8819 | #if defined (HAVE_PRSTATUS_T) |
252b5132 | 8820 | return elfcore_grok_prstatus (abfd, note); |
bb0082d6 | 8821 | #else |
b34976b6 | 8822 | return TRUE; |
252b5132 RH |
8823 | #endif |
8824 | ||
8825 | #if defined (HAVE_PSTATUS_T) | |
8826 | case NT_PSTATUS: | |
8827 | return elfcore_grok_pstatus (abfd, note); | |
8828 | #endif | |
8829 | ||
8830 | #if defined (HAVE_LWPSTATUS_T) | |
8831 | case NT_LWPSTATUS: | |
8832 | return elfcore_grok_lwpstatus (abfd, note); | |
8833 | #endif | |
8834 | ||
8835 | case NT_FPREGSET: /* FIXME: rename to NT_PRFPREG */ | |
8836 | return elfcore_grok_prfpreg (abfd, note); | |
8837 | ||
c044fabd | 8838 | case NT_WIN32PSTATUS: |
16e9c715 | 8839 | return elfcore_grok_win32pstatus (abfd, note); |
16e9c715 | 8840 | |
c044fabd | 8841 | case NT_PRXFPREG: /* Linux SSE extension */ |
e377ab71 MK |
8842 | if (note->namesz == 6 |
8843 | && strcmp (note->namedata, "LINUX") == 0) | |
ff08c6bb JB |
8844 | return elfcore_grok_prxfpreg (abfd, note); |
8845 | else | |
b34976b6 | 8846 | return TRUE; |
ff08c6bb | 8847 | |
4339cae0 L |
8848 | case NT_X86_XSTATE: /* Linux XSAVE extension */ |
8849 | if (note->namesz == 6 | |
8850 | && strcmp (note->namedata, "LINUX") == 0) | |
8851 | return elfcore_grok_xstatereg (abfd, note); | |
97de3545 JB |
8852 | else if (note->namesz == 8 |
8853 | && strcmp (note->namedata, "FreeBSD") == 0) | |
8854 | return elfcore_grok_xstatereg (abfd, note); | |
4339cae0 L |
8855 | else |
8856 | return TRUE; | |
8857 | ||
97753bd5 AM |
8858 | case NT_PPC_VMX: |
8859 | if (note->namesz == 6 | |
8860 | && strcmp (note->namedata, "LINUX") == 0) | |
8861 | return elfcore_grok_ppc_vmx (abfd, note); | |
8862 | else | |
8863 | return TRUE; | |
8864 | ||
89eeb0bc LM |
8865 | case NT_PPC_VSX: |
8866 | if (note->namesz == 6 | |
8867 | && strcmp (note->namedata, "LINUX") == 0) | |
8868 | return elfcore_grok_ppc_vsx (abfd, note); | |
8869 | else | |
8870 | return TRUE; | |
8871 | ||
0675e188 UW |
8872 | case NT_S390_HIGH_GPRS: |
8873 | if (note->namesz == 6 | |
8874 | && strcmp (note->namedata, "LINUX") == 0) | |
8875 | return elfcore_grok_s390_high_gprs (abfd, note); | |
8876 | else | |
8877 | return TRUE; | |
8878 | ||
d7eeb400 MS |
8879 | case NT_S390_TIMER: |
8880 | if (note->namesz == 6 | |
8881 | && strcmp (note->namedata, "LINUX") == 0) | |
8882 | return elfcore_grok_s390_timer (abfd, note); | |
8883 | else | |
8884 | return TRUE; | |
8885 | ||
8886 | case NT_S390_TODCMP: | |
8887 | if (note->namesz == 6 | |
8888 | && strcmp (note->namedata, "LINUX") == 0) | |
8889 | return elfcore_grok_s390_todcmp (abfd, note); | |
8890 | else | |
8891 | return TRUE; | |
8892 | ||
8893 | case NT_S390_TODPREG: | |
8894 | if (note->namesz == 6 | |
8895 | && strcmp (note->namedata, "LINUX") == 0) | |
8896 | return elfcore_grok_s390_todpreg (abfd, note); | |
8897 | else | |
8898 | return TRUE; | |
8899 | ||
8900 | case NT_S390_CTRS: | |
8901 | if (note->namesz == 6 | |
8902 | && strcmp (note->namedata, "LINUX") == 0) | |
8903 | return elfcore_grok_s390_ctrs (abfd, note); | |
8904 | else | |
8905 | return TRUE; | |
8906 | ||
8907 | case NT_S390_PREFIX: | |
8908 | if (note->namesz == 6 | |
8909 | && strcmp (note->namedata, "LINUX") == 0) | |
8910 | return elfcore_grok_s390_prefix (abfd, note); | |
8911 | else | |
8912 | return TRUE; | |
8913 | ||
355b81d9 UW |
8914 | case NT_S390_LAST_BREAK: |
8915 | if (note->namesz == 6 | |
8916 | && strcmp (note->namedata, "LINUX") == 0) | |
8917 | return elfcore_grok_s390_last_break (abfd, note); | |
8918 | else | |
8919 | return TRUE; | |
8920 | ||
8921 | case NT_S390_SYSTEM_CALL: | |
8922 | if (note->namesz == 6 | |
8923 | && strcmp (note->namedata, "LINUX") == 0) | |
8924 | return elfcore_grok_s390_system_call (abfd, note); | |
8925 | else | |
8926 | return TRUE; | |
8927 | ||
abb3f6cc NC |
8928 | case NT_S390_TDB: |
8929 | if (note->namesz == 6 | |
8930 | && strcmp (note->namedata, "LINUX") == 0) | |
8931 | return elfcore_grok_s390_tdb (abfd, note); | |
8932 | else | |
8933 | return TRUE; | |
8934 | ||
4ef9f41a AA |
8935 | case NT_S390_VXRS_LOW: |
8936 | if (note->namesz == 6 | |
8937 | && strcmp (note->namedata, "LINUX") == 0) | |
8938 | return elfcore_grok_s390_vxrs_low (abfd, note); | |
8939 | else | |
8940 | return TRUE; | |
8941 | ||
8942 | case NT_S390_VXRS_HIGH: | |
8943 | if (note->namesz == 6 | |
8944 | && strcmp (note->namedata, "LINUX") == 0) | |
8945 | return elfcore_grok_s390_vxrs_high (abfd, note); | |
8946 | else | |
8947 | return TRUE; | |
8948 | ||
faa9a424 UW |
8949 | case NT_ARM_VFP: |
8950 | if (note->namesz == 6 | |
8951 | && strcmp (note->namedata, "LINUX") == 0) | |
8952 | return elfcore_grok_arm_vfp (abfd, note); | |
8953 | else | |
8954 | return TRUE; | |
8955 | ||
652451f8 YZ |
8956 | case NT_ARM_TLS: |
8957 | if (note->namesz == 6 | |
8958 | && strcmp (note->namedata, "LINUX") == 0) | |
8959 | return elfcore_grok_aarch_tls (abfd, note); | |
8960 | else | |
8961 | return TRUE; | |
8962 | ||
8963 | case NT_ARM_HW_BREAK: | |
8964 | if (note->namesz == 6 | |
8965 | && strcmp (note->namedata, "LINUX") == 0) | |
8966 | return elfcore_grok_aarch_hw_break (abfd, note); | |
8967 | else | |
8968 | return TRUE; | |
8969 | ||
8970 | case NT_ARM_HW_WATCH: | |
8971 | if (note->namesz == 6 | |
8972 | && strcmp (note->namedata, "LINUX") == 0) | |
8973 | return elfcore_grok_aarch_hw_watch (abfd, note); | |
8974 | else | |
8975 | return TRUE; | |
8976 | ||
252b5132 RH |
8977 | case NT_PRPSINFO: |
8978 | case NT_PSINFO: | |
bb0082d6 AM |
8979 | if (bed->elf_backend_grok_psinfo) |
8980 | if ((*bed->elf_backend_grok_psinfo) (abfd, note)) | |
b34976b6 | 8981 | return TRUE; |
bb0082d6 | 8982 | #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) |
252b5132 | 8983 | return elfcore_grok_psinfo (abfd, note); |
bb0082d6 | 8984 | #else |
b34976b6 | 8985 | return TRUE; |
252b5132 | 8986 | #endif |
3333a7c3 RM |
8987 | |
8988 | case NT_AUXV: | |
8989 | { | |
117ed4f8 AM |
8990 | asection *sect = bfd_make_section_anyway_with_flags (abfd, ".auxv", |
8991 | SEC_HAS_CONTENTS); | |
3333a7c3 RM |
8992 | |
8993 | if (sect == NULL) | |
8994 | return FALSE; | |
eea6121a | 8995 | sect->size = note->descsz; |
3333a7c3 | 8996 | sect->filepos = note->descpos; |
3333a7c3 RM |
8997 | sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32; |
8998 | ||
8999 | return TRUE; | |
9000 | } | |
9015683b | 9001 | |
451b7c33 TT |
9002 | case NT_FILE: |
9003 | return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.file", | |
9004 | note); | |
9005 | ||
9015683b TT |
9006 | case NT_SIGINFO: |
9007 | return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.siginfo", | |
9008 | note); | |
252b5132 RH |
9009 | } |
9010 | } | |
9011 | ||
718175fa JK |
9012 | static bfd_boolean |
9013 | elfobj_grok_gnu_build_id (bfd *abfd, Elf_Internal_Note *note) | |
9014 | { | |
30e8ee25 AM |
9015 | struct elf_obj_tdata *t; |
9016 | ||
9017 | if (note->descsz == 0) | |
9018 | return FALSE; | |
9019 | ||
9020 | t = elf_tdata (abfd); | |
c0355132 | 9021 | t->build_id = bfd_alloc (abfd, sizeof (*t->build_id) - 1 + note->descsz); |
30e8ee25 | 9022 | if (t->build_id == NULL) |
718175fa JK |
9023 | return FALSE; |
9024 | ||
c0355132 AM |
9025 | t->build_id->size = note->descsz; |
9026 | memcpy (t->build_id->data, note->descdata, note->descsz); | |
718175fa JK |
9027 | |
9028 | return TRUE; | |
9029 | } | |
9030 | ||
9031 | static bfd_boolean | |
9032 | elfobj_grok_gnu_note (bfd *abfd, Elf_Internal_Note *note) | |
9033 | { | |
9034 | switch (note->type) | |
9035 | { | |
9036 | default: | |
9037 | return TRUE; | |
9038 | ||
9039 | case NT_GNU_BUILD_ID: | |
9040 | return elfobj_grok_gnu_build_id (abfd, note); | |
9041 | } | |
9042 | } | |
9043 | ||
e21e5835 NC |
9044 | static bfd_boolean |
9045 | elfobj_grok_stapsdt_note_1 (bfd *abfd, Elf_Internal_Note *note) | |
9046 | { | |
9047 | struct sdt_note *cur = | |
9048 | (struct sdt_note *) bfd_alloc (abfd, sizeof (struct sdt_note) | |
9049 | + note->descsz); | |
9050 | ||
9051 | cur->next = (struct sdt_note *) (elf_tdata (abfd))->sdt_note_head; | |
9052 | cur->size = (bfd_size_type) note->descsz; | |
9053 | memcpy (cur->data, note->descdata, note->descsz); | |
9054 | ||
9055 | elf_tdata (abfd)->sdt_note_head = cur; | |
9056 | ||
9057 | return TRUE; | |
9058 | } | |
9059 | ||
9060 | static bfd_boolean | |
9061 | elfobj_grok_stapsdt_note (bfd *abfd, Elf_Internal_Note *note) | |
9062 | { | |
9063 | switch (note->type) | |
9064 | { | |
9065 | case NT_STAPSDT: | |
9066 | return elfobj_grok_stapsdt_note_1 (abfd, note); | |
9067 | ||
9068 | default: | |
9069 | return TRUE; | |
9070 | } | |
9071 | } | |
9072 | ||
b34976b6 | 9073 | static bfd_boolean |
217aa764 | 9074 | elfcore_netbsd_get_lwpid (Elf_Internal_Note *note, int *lwpidp) |
50b2bdb7 AM |
9075 | { |
9076 | char *cp; | |
9077 | ||
9078 | cp = strchr (note->namedata, '@'); | |
9079 | if (cp != NULL) | |
9080 | { | |
d2b64500 | 9081 | *lwpidp = atoi(cp + 1); |
b34976b6 | 9082 | return TRUE; |
50b2bdb7 | 9083 | } |
b34976b6 | 9084 | return FALSE; |
50b2bdb7 AM |
9085 | } |
9086 | ||
b34976b6 | 9087 | static bfd_boolean |
217aa764 | 9088 | elfcore_grok_netbsd_procinfo (bfd *abfd, Elf_Internal_Note *note) |
50b2bdb7 | 9089 | { |
50b2bdb7 | 9090 | /* Signal number at offset 0x08. */ |
228e534f | 9091 | elf_tdata (abfd)->core->signal |
50b2bdb7 AM |
9092 | = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08); |
9093 | ||
9094 | /* Process ID at offset 0x50. */ | |
228e534f | 9095 | elf_tdata (abfd)->core->pid |
50b2bdb7 AM |
9096 | = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x50); |
9097 | ||
9098 | /* Command name at 0x7c (max 32 bytes, including nul). */ | |
228e534f | 9099 | elf_tdata (abfd)->core->command |
50b2bdb7 AM |
9100 | = _bfd_elfcore_strndup (abfd, note->descdata + 0x7c, 31); |
9101 | ||
7720ba9f MK |
9102 | return elfcore_make_note_pseudosection (abfd, ".note.netbsdcore.procinfo", |
9103 | note); | |
50b2bdb7 AM |
9104 | } |
9105 | ||
b34976b6 | 9106 | static bfd_boolean |
217aa764 | 9107 | elfcore_grok_netbsd_note (bfd *abfd, Elf_Internal_Note *note) |
50b2bdb7 AM |
9108 | { |
9109 | int lwp; | |
9110 | ||
9111 | if (elfcore_netbsd_get_lwpid (note, &lwp)) | |
228e534f | 9112 | elf_tdata (abfd)->core->lwpid = lwp; |
50b2bdb7 | 9113 | |
b4db1224 | 9114 | if (note->type == NT_NETBSDCORE_PROCINFO) |
50b2bdb7 AM |
9115 | { |
9116 | /* NetBSD-specific core "procinfo". Note that we expect to | |
08a40648 AM |
9117 | find this note before any of the others, which is fine, |
9118 | since the kernel writes this note out first when it | |
9119 | creates a core file. */ | |
47d9a591 | 9120 | |
50b2bdb7 AM |
9121 | return elfcore_grok_netbsd_procinfo (abfd, note); |
9122 | } | |
9123 | ||
b4db1224 JT |
9124 | /* As of Jan 2002 there are no other machine-independent notes |
9125 | defined for NetBSD core files. If the note type is less | |
9126 | than the start of the machine-dependent note types, we don't | |
9127 | understand it. */ | |
47d9a591 | 9128 | |
b4db1224 | 9129 | if (note->type < NT_NETBSDCORE_FIRSTMACH) |
b34976b6 | 9130 | return TRUE; |
50b2bdb7 AM |
9131 | |
9132 | ||
9133 | switch (bfd_get_arch (abfd)) | |
9134 | { | |
08a40648 AM |
9135 | /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and |
9136 | PT_GETFPREGS == mach+2. */ | |
50b2bdb7 AM |
9137 | |
9138 | case bfd_arch_alpha: | |
9139 | case bfd_arch_sparc: | |
9140 | switch (note->type) | |
08a40648 AM |
9141 | { |
9142 | case NT_NETBSDCORE_FIRSTMACH+0: | |
9143 | return elfcore_make_note_pseudosection (abfd, ".reg", note); | |
50b2bdb7 | 9144 | |
08a40648 AM |
9145 | case NT_NETBSDCORE_FIRSTMACH+2: |
9146 | return elfcore_make_note_pseudosection (abfd, ".reg2", note); | |
50b2bdb7 | 9147 | |
08a40648 AM |
9148 | default: |
9149 | return TRUE; | |
9150 | } | |
50b2bdb7 | 9151 | |
08a40648 AM |
9152 | /* On all other arch's, PT_GETREGS == mach+1 and |
9153 | PT_GETFPREGS == mach+3. */ | |
50b2bdb7 AM |
9154 | |
9155 | default: | |
9156 | switch (note->type) | |
08a40648 AM |
9157 | { |
9158 | case NT_NETBSDCORE_FIRSTMACH+1: | |
9159 | return elfcore_make_note_pseudosection (abfd, ".reg", note); | |
50b2bdb7 | 9160 | |
08a40648 AM |
9161 | case NT_NETBSDCORE_FIRSTMACH+3: |
9162 | return elfcore_make_note_pseudosection (abfd, ".reg2", note); | |
50b2bdb7 | 9163 | |
08a40648 AM |
9164 | default: |
9165 | return TRUE; | |
9166 | } | |
50b2bdb7 AM |
9167 | } |
9168 | /* NOTREACHED */ | |
9169 | } | |
9170 | ||
67cc5033 MK |
9171 | static bfd_boolean |
9172 | elfcore_grok_openbsd_procinfo (bfd *abfd, Elf_Internal_Note *note) | |
9173 | { | |
9174 | /* Signal number at offset 0x08. */ | |
228e534f | 9175 | elf_tdata (abfd)->core->signal |
67cc5033 MK |
9176 | = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08); |
9177 | ||
9178 | /* Process ID at offset 0x20. */ | |
228e534f | 9179 | elf_tdata (abfd)->core->pid |
67cc5033 MK |
9180 | = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x20); |
9181 | ||
9182 | /* Command name at 0x48 (max 32 bytes, including nul). */ | |
228e534f | 9183 | elf_tdata (abfd)->core->command |
67cc5033 MK |
9184 | = _bfd_elfcore_strndup (abfd, note->descdata + 0x48, 31); |
9185 | ||
9186 | return TRUE; | |
9187 | } | |
9188 | ||
9189 | static bfd_boolean | |
9190 | elfcore_grok_openbsd_note (bfd *abfd, Elf_Internal_Note *note) | |
9191 | { | |
9192 | if (note->type == NT_OPENBSD_PROCINFO) | |
9193 | return elfcore_grok_openbsd_procinfo (abfd, note); | |
9194 | ||
9195 | if (note->type == NT_OPENBSD_REGS) | |
9196 | return elfcore_make_note_pseudosection (abfd, ".reg", note); | |
9197 | ||
9198 | if (note->type == NT_OPENBSD_FPREGS) | |
9199 | return elfcore_make_note_pseudosection (abfd, ".reg2", note); | |
9200 | ||
9201 | if (note->type == NT_OPENBSD_XFPREGS) | |
9202 | return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note); | |
9203 | ||
9204 | if (note->type == NT_OPENBSD_AUXV) | |
9205 | { | |
9206 | asection *sect = bfd_make_section_anyway_with_flags (abfd, ".auxv", | |
9207 | SEC_HAS_CONTENTS); | |
9208 | ||
9209 | if (sect == NULL) | |
9210 | return FALSE; | |
9211 | sect->size = note->descsz; | |
9212 | sect->filepos = note->descpos; | |
9213 | sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32; | |
9214 | ||
9215 | return TRUE; | |
9216 | } | |
9217 | ||
9218 | if (note->type == NT_OPENBSD_WCOOKIE) | |
9219 | { | |
9220 | asection *sect = bfd_make_section_anyway_with_flags (abfd, ".wcookie", | |
9221 | SEC_HAS_CONTENTS); | |
9222 | ||
9223 | if (sect == NULL) | |
9224 | return FALSE; | |
9225 | sect->size = note->descsz; | |
9226 | sect->filepos = note->descpos; | |
9227 | sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32; | |
9228 | ||
9229 | return TRUE; | |
9230 | } | |
9231 | ||
9232 | return TRUE; | |
9233 | } | |
9234 | ||
07c6e936 | 9235 | static bfd_boolean |
d3fd4074 | 9236 | elfcore_grok_nto_status (bfd *abfd, Elf_Internal_Note *note, long *tid) |
07c6e936 NC |
9237 | { |
9238 | void *ddata = note->descdata; | |
9239 | char buf[100]; | |
9240 | char *name; | |
9241 | asection *sect; | |
f8843e87 AM |
9242 | short sig; |
9243 | unsigned flags; | |
07c6e936 NC |
9244 | |
9245 | /* nto_procfs_status 'pid' field is at offset 0. */ | |
228e534f | 9246 | elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, (bfd_byte *) ddata); |
07c6e936 | 9247 | |
f8843e87 AM |
9248 | /* nto_procfs_status 'tid' field is at offset 4. Pass it back. */ |
9249 | *tid = bfd_get_32 (abfd, (bfd_byte *) ddata + 4); | |
9250 | ||
9251 | /* nto_procfs_status 'flags' field is at offset 8. */ | |
9252 | flags = bfd_get_32 (abfd, (bfd_byte *) ddata + 8); | |
07c6e936 NC |
9253 | |
9254 | /* nto_procfs_status 'what' field is at offset 14. */ | |
f8843e87 AM |
9255 | if ((sig = bfd_get_16 (abfd, (bfd_byte *) ddata + 14)) > 0) |
9256 | { | |
228e534f AM |
9257 | elf_tdata (abfd)->core->signal = sig; |
9258 | elf_tdata (abfd)->core->lwpid = *tid; | |
f8843e87 | 9259 | } |
07c6e936 | 9260 | |
f8843e87 AM |
9261 | /* _DEBUG_FLAG_CURTID (current thread) is 0x80. Some cores |
9262 | do not come from signals so we make sure we set the current | |
9263 | thread just in case. */ | |
9264 | if (flags & 0x00000080) | |
228e534f | 9265 | elf_tdata (abfd)->core->lwpid = *tid; |
07c6e936 NC |
9266 | |
9267 | /* Make a ".qnx_core_status/%d" section. */ | |
d3fd4074 | 9268 | sprintf (buf, ".qnx_core_status/%ld", *tid); |
07c6e936 | 9269 | |
a50b1753 | 9270 | name = (char *) bfd_alloc (abfd, strlen (buf) + 1); |
07c6e936 NC |
9271 | if (name == NULL) |
9272 | return FALSE; | |
9273 | strcpy (name, buf); | |
9274 | ||
117ed4f8 | 9275 | sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); |
07c6e936 NC |
9276 | if (sect == NULL) |
9277 | return FALSE; | |
9278 | ||
eea6121a | 9279 | sect->size = note->descsz; |
07c6e936 | 9280 | sect->filepos = note->descpos; |
07c6e936 NC |
9281 | sect->alignment_power = 2; |
9282 | ||
9283 | return (elfcore_maybe_make_sect (abfd, ".qnx_core_status", sect)); | |
9284 | } | |
9285 | ||
9286 | static bfd_boolean | |
d69f560c KW |
9287 | elfcore_grok_nto_regs (bfd *abfd, |
9288 | Elf_Internal_Note *note, | |
d3fd4074 | 9289 | long tid, |
d69f560c | 9290 | char *base) |
07c6e936 NC |
9291 | { |
9292 | char buf[100]; | |
9293 | char *name; | |
9294 | asection *sect; | |
9295 | ||
d69f560c | 9296 | /* Make a "(base)/%d" section. */ |
d3fd4074 | 9297 | sprintf (buf, "%s/%ld", base, tid); |
07c6e936 | 9298 | |
a50b1753 | 9299 | name = (char *) bfd_alloc (abfd, strlen (buf) + 1); |
07c6e936 NC |
9300 | if (name == NULL) |
9301 | return FALSE; | |
9302 | strcpy (name, buf); | |
9303 | ||
117ed4f8 | 9304 | sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); |
07c6e936 NC |
9305 | if (sect == NULL) |
9306 | return FALSE; | |
9307 | ||
eea6121a | 9308 | sect->size = note->descsz; |
07c6e936 | 9309 | sect->filepos = note->descpos; |
07c6e936 NC |
9310 | sect->alignment_power = 2; |
9311 | ||
f8843e87 | 9312 | /* This is the current thread. */ |
228e534f | 9313 | if (elf_tdata (abfd)->core->lwpid == tid) |
d69f560c | 9314 | return elfcore_maybe_make_sect (abfd, base, sect); |
f8843e87 AM |
9315 | |
9316 | return TRUE; | |
07c6e936 NC |
9317 | } |
9318 | ||
9319 | #define BFD_QNT_CORE_INFO 7 | |
9320 | #define BFD_QNT_CORE_STATUS 8 | |
9321 | #define BFD_QNT_CORE_GREG 9 | |
9322 | #define BFD_QNT_CORE_FPREG 10 | |
9323 | ||
9324 | static bfd_boolean | |
217aa764 | 9325 | elfcore_grok_nto_note (bfd *abfd, Elf_Internal_Note *note) |
07c6e936 NC |
9326 | { |
9327 | /* Every GREG section has a STATUS section before it. Store the | |
811072d8 | 9328 | tid from the previous call to pass down to the next gregs |
07c6e936 | 9329 | function. */ |
d3fd4074 | 9330 | static long tid = 1; |
07c6e936 NC |
9331 | |
9332 | switch (note->type) | |
9333 | { | |
d69f560c KW |
9334 | case BFD_QNT_CORE_INFO: |
9335 | return elfcore_make_note_pseudosection (abfd, ".qnx_core_info", note); | |
9336 | case BFD_QNT_CORE_STATUS: | |
9337 | return elfcore_grok_nto_status (abfd, note, &tid); | |
9338 | case BFD_QNT_CORE_GREG: | |
9339 | return elfcore_grok_nto_regs (abfd, note, tid, ".reg"); | |
9340 | case BFD_QNT_CORE_FPREG: | |
9341 | return elfcore_grok_nto_regs (abfd, note, tid, ".reg2"); | |
9342 | default: | |
9343 | return TRUE; | |
07c6e936 NC |
9344 | } |
9345 | } | |
9346 | ||
b15fa79e AM |
9347 | static bfd_boolean |
9348 | elfcore_grok_spu_note (bfd *abfd, Elf_Internal_Note *note) | |
9349 | { | |
9350 | char *name; | |
9351 | asection *sect; | |
9352 | size_t len; | |
9353 | ||
9354 | /* Use note name as section name. */ | |
9355 | len = note->namesz; | |
a50b1753 | 9356 | name = (char *) bfd_alloc (abfd, len); |
b15fa79e AM |
9357 | if (name == NULL) |
9358 | return FALSE; | |
9359 | memcpy (name, note->namedata, len); | |
9360 | name[len - 1] = '\0'; | |
9361 | ||
9362 | sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); | |
9363 | if (sect == NULL) | |
9364 | return FALSE; | |
9365 | ||
9366 | sect->size = note->descsz; | |
9367 | sect->filepos = note->descpos; | |
9368 | sect->alignment_power = 1; | |
9369 | ||
9370 | return TRUE; | |
9371 | } | |
9372 | ||
7c76fa91 MS |
9373 | /* Function: elfcore_write_note |
9374 | ||
47d9a591 | 9375 | Inputs: |
a39f3346 | 9376 | buffer to hold note, and current size of buffer |
7c76fa91 MS |
9377 | name of note |
9378 | type of note | |
9379 | data for note | |
9380 | size of data for note | |
9381 | ||
a39f3346 AM |
9382 | Writes note to end of buffer. ELF64 notes are written exactly as |
9383 | for ELF32, despite the current (as of 2006) ELF gabi specifying | |
9384 | that they ought to have 8-byte namesz and descsz field, and have | |
9385 | 8-byte alignment. Other writers, eg. Linux kernel, do the same. | |
9386 | ||
7c76fa91 | 9387 | Return: |
a39f3346 | 9388 | Pointer to realloc'd buffer, *BUFSIZ updated. */ |
7c76fa91 MS |
9389 | |
9390 | char * | |
a39f3346 | 9391 | elfcore_write_note (bfd *abfd, |
217aa764 | 9392 | char *buf, |
a39f3346 | 9393 | int *bufsiz, |
217aa764 | 9394 | const char *name, |
a39f3346 | 9395 | int type, |
217aa764 | 9396 | const void *input, |
a39f3346 | 9397 | int size) |
7c76fa91 MS |
9398 | { |
9399 | Elf_External_Note *xnp; | |
d4c88bbb | 9400 | size_t namesz; |
d4c88bbb | 9401 | size_t newspace; |
a39f3346 | 9402 | char *dest; |
7c76fa91 | 9403 | |
d4c88bbb | 9404 | namesz = 0; |
d4c88bbb | 9405 | if (name != NULL) |
a39f3346 | 9406 | namesz = strlen (name) + 1; |
d4c88bbb | 9407 | |
a39f3346 | 9408 | newspace = 12 + ((namesz + 3) & -4) + ((size + 3) & -4); |
d4c88bbb | 9409 | |
a50b1753 | 9410 | buf = (char *) realloc (buf, *bufsiz + newspace); |
14b1c01e AM |
9411 | if (buf == NULL) |
9412 | return buf; | |
a39f3346 | 9413 | dest = buf + *bufsiz; |
7c76fa91 MS |
9414 | *bufsiz += newspace; |
9415 | xnp = (Elf_External_Note *) dest; | |
9416 | H_PUT_32 (abfd, namesz, xnp->namesz); | |
9417 | H_PUT_32 (abfd, size, xnp->descsz); | |
9418 | H_PUT_32 (abfd, type, xnp->type); | |
d4c88bbb AM |
9419 | dest = xnp->name; |
9420 | if (name != NULL) | |
9421 | { | |
9422 | memcpy (dest, name, namesz); | |
9423 | dest += namesz; | |
a39f3346 | 9424 | while (namesz & 3) |
d4c88bbb AM |
9425 | { |
9426 | *dest++ = '\0'; | |
a39f3346 | 9427 | ++namesz; |
d4c88bbb AM |
9428 | } |
9429 | } | |
9430 | memcpy (dest, input, size); | |
a39f3346 AM |
9431 | dest += size; |
9432 | while (size & 3) | |
9433 | { | |
9434 | *dest++ = '\0'; | |
9435 | ++size; | |
9436 | } | |
9437 | return buf; | |
7c76fa91 MS |
9438 | } |
9439 | ||
7c76fa91 | 9440 | char * |
217aa764 AM |
9441 | elfcore_write_prpsinfo (bfd *abfd, |
9442 | char *buf, | |
9443 | int *bufsiz, | |
9444 | const char *fname, | |
9445 | const char *psargs) | |
7c76fa91 | 9446 | { |
183e98be AM |
9447 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
9448 | ||
9449 | if (bed->elf_backend_write_core_note != NULL) | |
9450 | { | |
9451 | char *ret; | |
9452 | ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz, | |
9453 | NT_PRPSINFO, fname, psargs); | |
9454 | if (ret != NULL) | |
9455 | return ret; | |
9456 | } | |
7c76fa91 | 9457 | |
1f20dca5 | 9458 | #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) |
183e98be AM |
9459 | #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T) |
9460 | if (bed->s->elfclass == ELFCLASS32) | |
9461 | { | |
9462 | #if defined (HAVE_PSINFO32_T) | |
9463 | psinfo32_t data; | |
9464 | int note_type = NT_PSINFO; | |
9465 | #else | |
9466 | prpsinfo32_t data; | |
9467 | int note_type = NT_PRPSINFO; | |
9468 | #endif | |
9469 | ||
9470 | memset (&data, 0, sizeof (data)); | |
9471 | strncpy (data.pr_fname, fname, sizeof (data.pr_fname)); | |
9472 | strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs)); | |
9473 | return elfcore_write_note (abfd, buf, bufsiz, | |
1f20dca5 | 9474 | "CORE", note_type, &data, sizeof (data)); |
183e98be AM |
9475 | } |
9476 | else | |
9477 | #endif | |
9478 | { | |
7c76fa91 | 9479 | #if defined (HAVE_PSINFO_T) |
183e98be AM |
9480 | psinfo_t data; |
9481 | int note_type = NT_PSINFO; | |
7c76fa91 | 9482 | #else |
183e98be AM |
9483 | prpsinfo_t data; |
9484 | int note_type = NT_PRPSINFO; | |
7c76fa91 MS |
9485 | #endif |
9486 | ||
183e98be AM |
9487 | memset (&data, 0, sizeof (data)); |
9488 | strncpy (data.pr_fname, fname, sizeof (data.pr_fname)); | |
9489 | strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs)); | |
9490 | return elfcore_write_note (abfd, buf, bufsiz, | |
1f20dca5 | 9491 | "CORE", note_type, &data, sizeof (data)); |
183e98be | 9492 | } |
7c76fa91 MS |
9493 | #endif /* PSINFO_T or PRPSINFO_T */ |
9494 | ||
1f20dca5 UW |
9495 | free (buf); |
9496 | return NULL; | |
9497 | } | |
9498 | ||
70a38d42 SDJ |
9499 | char * |
9500 | elfcore_write_linux_prpsinfo32 | |
9501 | (bfd *abfd, char *buf, int *bufsiz, | |
9502 | const struct elf_internal_linux_prpsinfo *prpsinfo) | |
9503 | { | |
9504 | struct elf_external_linux_prpsinfo32 data; | |
9505 | ||
9506 | memset (&data, 0, sizeof (data)); | |
9507 | LINUX_PRPSINFO32_SWAP_FIELDS (abfd, prpsinfo, data); | |
9508 | ||
9509 | return elfcore_write_note (abfd, buf, bufsiz, "CORE", NT_PRPSINFO, | |
9510 | &data, sizeof (data)); | |
9511 | } | |
9512 | ||
9513 | char * | |
9514 | elfcore_write_linux_prpsinfo64 | |
9515 | (bfd *abfd, char *buf, int *bufsiz, | |
9516 | const struct elf_internal_linux_prpsinfo *prpsinfo) | |
9517 | { | |
9518 | struct elf_external_linux_prpsinfo64 data; | |
9519 | ||
9520 | memset (&data, 0, sizeof (data)); | |
9521 | LINUX_PRPSINFO64_SWAP_FIELDS (abfd, prpsinfo, data); | |
9522 | ||
9523 | return elfcore_write_note (abfd, buf, bufsiz, | |
9524 | "CORE", NT_PRPSINFO, &data, sizeof (data)); | |
9525 | } | |
9526 | ||
7c76fa91 | 9527 | char * |
217aa764 AM |
9528 | elfcore_write_prstatus (bfd *abfd, |
9529 | char *buf, | |
9530 | int *bufsiz, | |
9531 | long pid, | |
9532 | int cursig, | |
9533 | const void *gregs) | |
7c76fa91 | 9534 | { |
183e98be | 9535 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
7c76fa91 | 9536 | |
183e98be AM |
9537 | if (bed->elf_backend_write_core_note != NULL) |
9538 | { | |
9539 | char *ret; | |
9540 | ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz, | |
9541 | NT_PRSTATUS, | |
9542 | pid, cursig, gregs); | |
9543 | if (ret != NULL) | |
9544 | return ret; | |
9545 | } | |
9546 | ||
1f20dca5 | 9547 | #if defined (HAVE_PRSTATUS_T) |
183e98be AM |
9548 | #if defined (HAVE_PRSTATUS32_T) |
9549 | if (bed->s->elfclass == ELFCLASS32) | |
9550 | { | |
9551 | prstatus32_t prstat; | |
9552 | ||
9553 | memset (&prstat, 0, sizeof (prstat)); | |
9554 | prstat.pr_pid = pid; | |
9555 | prstat.pr_cursig = cursig; | |
9556 | memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg)); | |
1f20dca5 | 9557 | return elfcore_write_note (abfd, buf, bufsiz, "CORE", |
183e98be AM |
9558 | NT_PRSTATUS, &prstat, sizeof (prstat)); |
9559 | } | |
9560 | else | |
9561 | #endif | |
9562 | { | |
9563 | prstatus_t prstat; | |
9564 | ||
9565 | memset (&prstat, 0, sizeof (prstat)); | |
9566 | prstat.pr_pid = pid; | |
9567 | prstat.pr_cursig = cursig; | |
9568 | memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg)); | |
1f20dca5 | 9569 | return elfcore_write_note (abfd, buf, bufsiz, "CORE", |
183e98be AM |
9570 | NT_PRSTATUS, &prstat, sizeof (prstat)); |
9571 | } | |
7c76fa91 MS |
9572 | #endif /* HAVE_PRSTATUS_T */ |
9573 | ||
1f20dca5 UW |
9574 | free (buf); |
9575 | return NULL; | |
9576 | } | |
9577 | ||
51316059 MS |
9578 | #if defined (HAVE_LWPSTATUS_T) |
9579 | char * | |
217aa764 AM |
9580 | elfcore_write_lwpstatus (bfd *abfd, |
9581 | char *buf, | |
9582 | int *bufsiz, | |
9583 | long pid, | |
9584 | int cursig, | |
9585 | const void *gregs) | |
51316059 MS |
9586 | { |
9587 | lwpstatus_t lwpstat; | |
183e98be | 9588 | const char *note_name = "CORE"; |
51316059 MS |
9589 | |
9590 | memset (&lwpstat, 0, sizeof (lwpstat)); | |
9591 | lwpstat.pr_lwpid = pid >> 16; | |
9592 | lwpstat.pr_cursig = cursig; | |
9593 | #if defined (HAVE_LWPSTATUS_T_PR_REG) | |
d1e8523e | 9594 | memcpy (&lwpstat.pr_reg, gregs, sizeof (lwpstat.pr_reg)); |
51316059 MS |
9595 | #elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT) |
9596 | #if !defined(gregs) | |
9597 | memcpy (lwpstat.pr_context.uc_mcontext.gregs, | |
9598 | gregs, sizeof (lwpstat.pr_context.uc_mcontext.gregs)); | |
9599 | #else | |
9600 | memcpy (lwpstat.pr_context.uc_mcontext.__gregs, | |
9601 | gregs, sizeof (lwpstat.pr_context.uc_mcontext.__gregs)); | |
9602 | #endif | |
9603 | #endif | |
47d9a591 | 9604 | return elfcore_write_note (abfd, buf, bufsiz, note_name, |
51316059 MS |
9605 | NT_LWPSTATUS, &lwpstat, sizeof (lwpstat)); |
9606 | } | |
9607 | #endif /* HAVE_LWPSTATUS_T */ | |
9608 | ||
7c76fa91 MS |
9609 | #if defined (HAVE_PSTATUS_T) |
9610 | char * | |
217aa764 AM |
9611 | elfcore_write_pstatus (bfd *abfd, |
9612 | char *buf, | |
9613 | int *bufsiz, | |
9614 | long pid, | |
6c10990d NC |
9615 | int cursig ATTRIBUTE_UNUSED, |
9616 | const void *gregs ATTRIBUTE_UNUSED) | |
7c76fa91 | 9617 | { |
183e98be AM |
9618 | const char *note_name = "CORE"; |
9619 | #if defined (HAVE_PSTATUS32_T) | |
9620 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
7c76fa91 | 9621 | |
183e98be AM |
9622 | if (bed->s->elfclass == ELFCLASS32) |
9623 | { | |
9624 | pstatus32_t pstat; | |
9625 | ||
9626 | memset (&pstat, 0, sizeof (pstat)); | |
9627 | pstat.pr_pid = pid & 0xffff; | |
9628 | buf = elfcore_write_note (abfd, buf, bufsiz, note_name, | |
9629 | NT_PSTATUS, &pstat, sizeof (pstat)); | |
9630 | return buf; | |
9631 | } | |
9632 | else | |
9633 | #endif | |
9634 | { | |
9635 | pstatus_t pstat; | |
9636 | ||
9637 | memset (&pstat, 0, sizeof (pstat)); | |
9638 | pstat.pr_pid = pid & 0xffff; | |
9639 | buf = elfcore_write_note (abfd, buf, bufsiz, note_name, | |
9640 | NT_PSTATUS, &pstat, sizeof (pstat)); | |
9641 | return buf; | |
9642 | } | |
7c76fa91 MS |
9643 | } |
9644 | #endif /* HAVE_PSTATUS_T */ | |
9645 | ||
9646 | char * | |
217aa764 AM |
9647 | elfcore_write_prfpreg (bfd *abfd, |
9648 | char *buf, | |
9649 | int *bufsiz, | |
9650 | const void *fpregs, | |
9651 | int size) | |
7c76fa91 | 9652 | { |
183e98be | 9653 | const char *note_name = "CORE"; |
47d9a591 | 9654 | return elfcore_write_note (abfd, buf, bufsiz, |
7c76fa91 MS |
9655 | note_name, NT_FPREGSET, fpregs, size); |
9656 | } | |
9657 | ||
9658 | char * | |
217aa764 AM |
9659 | elfcore_write_prxfpreg (bfd *abfd, |
9660 | char *buf, | |
9661 | int *bufsiz, | |
9662 | const void *xfpregs, | |
9663 | int size) | |
7c76fa91 MS |
9664 | { |
9665 | char *note_name = "LINUX"; | |
47d9a591 | 9666 | return elfcore_write_note (abfd, buf, bufsiz, |
7c76fa91 MS |
9667 | note_name, NT_PRXFPREG, xfpregs, size); |
9668 | } | |
9669 | ||
4339cae0 L |
9670 | char * |
9671 | elfcore_write_xstatereg (bfd *abfd, char *buf, int *bufsiz, | |
9672 | const void *xfpregs, int size) | |
9673 | { | |
97de3545 JB |
9674 | char *note_name; |
9675 | if (get_elf_backend_data (abfd)->elf_osabi == ELFOSABI_FREEBSD) | |
9676 | note_name = "FreeBSD"; | |
9677 | else | |
9678 | note_name = "LINUX"; | |
4339cae0 L |
9679 | return elfcore_write_note (abfd, buf, bufsiz, |
9680 | note_name, NT_X86_XSTATE, xfpregs, size); | |
9681 | } | |
9682 | ||
97753bd5 AM |
9683 | char * |
9684 | elfcore_write_ppc_vmx (bfd *abfd, | |
9685 | char *buf, | |
9686 | int *bufsiz, | |
9687 | const void *ppc_vmx, | |
9688 | int size) | |
9689 | { | |
9690 | char *note_name = "LINUX"; | |
9691 | return elfcore_write_note (abfd, buf, bufsiz, | |
9692 | note_name, NT_PPC_VMX, ppc_vmx, size); | |
9693 | } | |
9694 | ||
89eeb0bc LM |
9695 | char * |
9696 | elfcore_write_ppc_vsx (bfd *abfd, | |
9697 | char *buf, | |
9698 | int *bufsiz, | |
9699 | const void *ppc_vsx, | |
9700 | int size) | |
9701 | { | |
9702 | char *note_name = "LINUX"; | |
9703 | return elfcore_write_note (abfd, buf, bufsiz, | |
9704 | note_name, NT_PPC_VSX, ppc_vsx, size); | |
9705 | } | |
9706 | ||
0675e188 UW |
9707 | static char * |
9708 | elfcore_write_s390_high_gprs (bfd *abfd, | |
9709 | char *buf, | |
9710 | int *bufsiz, | |
9711 | const void *s390_high_gprs, | |
9712 | int size) | |
9713 | { | |
9714 | char *note_name = "LINUX"; | |
9715 | return elfcore_write_note (abfd, buf, bufsiz, | |
9716 | note_name, NT_S390_HIGH_GPRS, | |
9717 | s390_high_gprs, size); | |
9718 | } | |
9719 | ||
d7eeb400 MS |
9720 | char * |
9721 | elfcore_write_s390_timer (bfd *abfd, | |
9722 | char *buf, | |
9723 | int *bufsiz, | |
9724 | const void *s390_timer, | |
9725 | int size) | |
9726 | { | |
9727 | char *note_name = "LINUX"; | |
9728 | return elfcore_write_note (abfd, buf, bufsiz, | |
9729 | note_name, NT_S390_TIMER, s390_timer, size); | |
9730 | } | |
9731 | ||
9732 | char * | |
9733 | elfcore_write_s390_todcmp (bfd *abfd, | |
9734 | char *buf, | |
9735 | int *bufsiz, | |
9736 | const void *s390_todcmp, | |
9737 | int size) | |
9738 | { | |
9739 | char *note_name = "LINUX"; | |
9740 | return elfcore_write_note (abfd, buf, bufsiz, | |
9741 | note_name, NT_S390_TODCMP, s390_todcmp, size); | |
9742 | } | |
9743 | ||
9744 | char * | |
9745 | elfcore_write_s390_todpreg (bfd *abfd, | |
9746 | char *buf, | |
9747 | int *bufsiz, | |
9748 | const void *s390_todpreg, | |
9749 | int size) | |
9750 | { | |
9751 | char *note_name = "LINUX"; | |
9752 | return elfcore_write_note (abfd, buf, bufsiz, | |
9753 | note_name, NT_S390_TODPREG, s390_todpreg, size); | |
9754 | } | |
9755 | ||
9756 | char * | |
9757 | elfcore_write_s390_ctrs (bfd *abfd, | |
9758 | char *buf, | |
9759 | int *bufsiz, | |
9760 | const void *s390_ctrs, | |
9761 | int size) | |
9762 | { | |
9763 | char *note_name = "LINUX"; | |
9764 | return elfcore_write_note (abfd, buf, bufsiz, | |
9765 | note_name, NT_S390_CTRS, s390_ctrs, size); | |
9766 | } | |
9767 | ||
9768 | char * | |
9769 | elfcore_write_s390_prefix (bfd *abfd, | |
9770 | char *buf, | |
9771 | int *bufsiz, | |
9772 | const void *s390_prefix, | |
9773 | int size) | |
9774 | { | |
9775 | char *note_name = "LINUX"; | |
9776 | return elfcore_write_note (abfd, buf, bufsiz, | |
9777 | note_name, NT_S390_PREFIX, s390_prefix, size); | |
9778 | } | |
9779 | ||
355b81d9 UW |
9780 | char * |
9781 | elfcore_write_s390_last_break (bfd *abfd, | |
9782 | char *buf, | |
9783 | int *bufsiz, | |
9784 | const void *s390_last_break, | |
9785 | int size) | |
9786 | { | |
9787 | char *note_name = "LINUX"; | |
9788 | return elfcore_write_note (abfd, buf, bufsiz, | |
9789 | note_name, NT_S390_LAST_BREAK, | |
9790 | s390_last_break, size); | |
9791 | } | |
9792 | ||
9793 | char * | |
9794 | elfcore_write_s390_system_call (bfd *abfd, | |
9795 | char *buf, | |
9796 | int *bufsiz, | |
9797 | const void *s390_system_call, | |
9798 | int size) | |
9799 | { | |
9800 | char *note_name = "LINUX"; | |
9801 | return elfcore_write_note (abfd, buf, bufsiz, | |
9802 | note_name, NT_S390_SYSTEM_CALL, | |
9803 | s390_system_call, size); | |
9804 | } | |
9805 | ||
abb3f6cc NC |
9806 | char * |
9807 | elfcore_write_s390_tdb (bfd *abfd, | |
9808 | char *buf, | |
9809 | int *bufsiz, | |
9810 | const void *s390_tdb, | |
9811 | int size) | |
9812 | { | |
9813 | char *note_name = "LINUX"; | |
9814 | return elfcore_write_note (abfd, buf, bufsiz, | |
9815 | note_name, NT_S390_TDB, s390_tdb, size); | |
9816 | } | |
9817 | ||
4ef9f41a AA |
9818 | char * |
9819 | elfcore_write_s390_vxrs_low (bfd *abfd, | |
9820 | char *buf, | |
9821 | int *bufsiz, | |
9822 | const void *s390_vxrs_low, | |
9823 | int size) | |
9824 | { | |
9825 | char *note_name = "LINUX"; | |
9826 | return elfcore_write_note (abfd, buf, bufsiz, | |
9827 | note_name, NT_S390_VXRS_LOW, s390_vxrs_low, size); | |
9828 | } | |
9829 | ||
9830 | char * | |
9831 | elfcore_write_s390_vxrs_high (bfd *abfd, | |
9832 | char *buf, | |
9833 | int *bufsiz, | |
9834 | const void *s390_vxrs_high, | |
9835 | int size) | |
9836 | { | |
9837 | char *note_name = "LINUX"; | |
9838 | return elfcore_write_note (abfd, buf, bufsiz, | |
9839 | note_name, NT_S390_VXRS_HIGH, | |
9840 | s390_vxrs_high, size); | |
9841 | } | |
9842 | ||
faa9a424 UW |
9843 | char * |
9844 | elfcore_write_arm_vfp (bfd *abfd, | |
9845 | char *buf, | |
9846 | int *bufsiz, | |
9847 | const void *arm_vfp, | |
9848 | int size) | |
9849 | { | |
9850 | char *note_name = "LINUX"; | |
9851 | return elfcore_write_note (abfd, buf, bufsiz, | |
9852 | note_name, NT_ARM_VFP, arm_vfp, size); | |
9853 | } | |
9854 | ||
652451f8 YZ |
9855 | char * |
9856 | elfcore_write_aarch_tls (bfd *abfd, | |
9857 | char *buf, | |
9858 | int *bufsiz, | |
9859 | const void *aarch_tls, | |
9860 | int size) | |
9861 | { | |
9862 | char *note_name = "LINUX"; | |
9863 | return elfcore_write_note (abfd, buf, bufsiz, | |
9864 | note_name, NT_ARM_TLS, aarch_tls, size); | |
9865 | } | |
9866 | ||
9867 | char * | |
9868 | elfcore_write_aarch_hw_break (bfd *abfd, | |
9869 | char *buf, | |
9870 | int *bufsiz, | |
9871 | const void *aarch_hw_break, | |
9872 | int size) | |
9873 | { | |
9874 | char *note_name = "LINUX"; | |
9875 | return elfcore_write_note (abfd, buf, bufsiz, | |
9876 | note_name, NT_ARM_HW_BREAK, aarch_hw_break, size); | |
9877 | } | |
9878 | ||
9879 | char * | |
9880 | elfcore_write_aarch_hw_watch (bfd *abfd, | |
9881 | char *buf, | |
9882 | int *bufsiz, | |
9883 | const void *aarch_hw_watch, | |
9884 | int size) | |
9885 | { | |
9886 | char *note_name = "LINUX"; | |
9887 | return elfcore_write_note (abfd, buf, bufsiz, | |
9888 | note_name, NT_ARM_HW_WATCH, aarch_hw_watch, size); | |
9889 | } | |
9890 | ||
bb864ac1 CES |
9891 | char * |
9892 | elfcore_write_register_note (bfd *abfd, | |
9893 | char *buf, | |
9894 | int *bufsiz, | |
9895 | const char *section, | |
9896 | const void *data, | |
9897 | int size) | |
9898 | { | |
9899 | if (strcmp (section, ".reg2") == 0) | |
9900 | return elfcore_write_prfpreg (abfd, buf, bufsiz, data, size); | |
9901 | if (strcmp (section, ".reg-xfp") == 0) | |
9902 | return elfcore_write_prxfpreg (abfd, buf, bufsiz, data, size); | |
4339cae0 L |
9903 | if (strcmp (section, ".reg-xstate") == 0) |
9904 | return elfcore_write_xstatereg (abfd, buf, bufsiz, data, size); | |
bb864ac1 CES |
9905 | if (strcmp (section, ".reg-ppc-vmx") == 0) |
9906 | return elfcore_write_ppc_vmx (abfd, buf, bufsiz, data, size); | |
89eeb0bc LM |
9907 | if (strcmp (section, ".reg-ppc-vsx") == 0) |
9908 | return elfcore_write_ppc_vsx (abfd, buf, bufsiz, data, size); | |
0675e188 UW |
9909 | if (strcmp (section, ".reg-s390-high-gprs") == 0) |
9910 | return elfcore_write_s390_high_gprs (abfd, buf, bufsiz, data, size); | |
d7eeb400 MS |
9911 | if (strcmp (section, ".reg-s390-timer") == 0) |
9912 | return elfcore_write_s390_timer (abfd, buf, bufsiz, data, size); | |
9913 | if (strcmp (section, ".reg-s390-todcmp") == 0) | |
9914 | return elfcore_write_s390_todcmp (abfd, buf, bufsiz, data, size); | |
9915 | if (strcmp (section, ".reg-s390-todpreg") == 0) | |
9916 | return elfcore_write_s390_todpreg (abfd, buf, bufsiz, data, size); | |
9917 | if (strcmp (section, ".reg-s390-ctrs") == 0) | |
9918 | return elfcore_write_s390_ctrs (abfd, buf, bufsiz, data, size); | |
9919 | if (strcmp (section, ".reg-s390-prefix") == 0) | |
9920 | return elfcore_write_s390_prefix (abfd, buf, bufsiz, data, size); | |
355b81d9 UW |
9921 | if (strcmp (section, ".reg-s390-last-break") == 0) |
9922 | return elfcore_write_s390_last_break (abfd, buf, bufsiz, data, size); | |
9923 | if (strcmp (section, ".reg-s390-system-call") == 0) | |
9924 | return elfcore_write_s390_system_call (abfd, buf, bufsiz, data, size); | |
abb3f6cc NC |
9925 | if (strcmp (section, ".reg-s390-tdb") == 0) |
9926 | return elfcore_write_s390_tdb (abfd, buf, bufsiz, data, size); | |
4ef9f41a AA |
9927 | if (strcmp (section, ".reg-s390-vxrs-low") == 0) |
9928 | return elfcore_write_s390_vxrs_low (abfd, buf, bufsiz, data, size); | |
9929 | if (strcmp (section, ".reg-s390-vxrs-high") == 0) | |
9930 | return elfcore_write_s390_vxrs_high (abfd, buf, bufsiz, data, size); | |
faa9a424 UW |
9931 | if (strcmp (section, ".reg-arm-vfp") == 0) |
9932 | return elfcore_write_arm_vfp (abfd, buf, bufsiz, data, size); | |
652451f8 YZ |
9933 | if (strcmp (section, ".reg-aarch-tls") == 0) |
9934 | return elfcore_write_aarch_tls (abfd, buf, bufsiz, data, size); | |
9935 | if (strcmp (section, ".reg-aarch-hw-break") == 0) | |
9936 | return elfcore_write_aarch_hw_break (abfd, buf, bufsiz, data, size); | |
9937 | if (strcmp (section, ".reg-aarch-hw-watch") == 0) | |
9938 | return elfcore_write_aarch_hw_watch (abfd, buf, bufsiz, data, size); | |
bb864ac1 CES |
9939 | return NULL; |
9940 | } | |
9941 | ||
b34976b6 | 9942 | static bfd_boolean |
718175fa | 9943 | elf_parse_notes (bfd *abfd, char *buf, size_t size, file_ptr offset) |
252b5132 | 9944 | { |
c044fabd | 9945 | char *p; |
252b5132 | 9946 | |
252b5132 RH |
9947 | p = buf; |
9948 | while (p < buf + size) | |
9949 | { | |
c044fabd KH |
9950 | /* FIXME: bad alignment assumption. */ |
9951 | Elf_External_Note *xnp = (Elf_External_Note *) p; | |
252b5132 RH |
9952 | Elf_Internal_Note in; |
9953 | ||
baea7ef1 AM |
9954 | if (offsetof (Elf_External_Note, name) > buf - p + size) |
9955 | return FALSE; | |
9956 | ||
dc810e39 | 9957 | in.type = H_GET_32 (abfd, xnp->type); |
252b5132 | 9958 | |
dc810e39 | 9959 | in.namesz = H_GET_32 (abfd, xnp->namesz); |
252b5132 | 9960 | in.namedata = xnp->name; |
baea7ef1 AM |
9961 | if (in.namesz > buf - in.namedata + size) |
9962 | return FALSE; | |
252b5132 | 9963 | |
dc810e39 | 9964 | in.descsz = H_GET_32 (abfd, xnp->descsz); |
252b5132 RH |
9965 | in.descdata = in.namedata + BFD_ALIGN (in.namesz, 4); |
9966 | in.descpos = offset + (in.descdata - buf); | |
baea7ef1 AM |
9967 | if (in.descsz != 0 |
9968 | && (in.descdata >= buf + size | |
9969 | || in.descsz > buf - in.descdata + size)) | |
9970 | return FALSE; | |
252b5132 | 9971 | |
718175fa JK |
9972 | switch (bfd_get_format (abfd)) |
9973 | { | |
9974 | default: | |
9975 | return TRUE; | |
9976 | ||
9977 | case bfd_core: | |
f64e188b | 9978 | { |
8acbedd6 | 9979 | #define GROKER_ELEMENT(S,F) {S, sizeof (S) - 1, F} |
f64e188b | 9980 | struct |
718175fa | 9981 | { |
f64e188b | 9982 | const char * string; |
8acbedd6 | 9983 | size_t len; |
f64e188b | 9984 | bfd_boolean (* func)(bfd *, Elf_Internal_Note *); |
718175fa | 9985 | } |
f64e188b | 9986 | grokers[] = |
b15fa79e | 9987 | { |
8acbedd6 KS |
9988 | GROKER_ELEMENT ("", elfcore_grok_note), |
9989 | GROKER_ELEMENT ("NetBSD-CORE", elfcore_grok_netbsd_note), | |
9990 | GROKER_ELEMENT ( "OpenBSD", elfcore_grok_openbsd_note), | |
9991 | GROKER_ELEMENT ("QNX", elfcore_grok_nto_note), | |
9992 | GROKER_ELEMENT ("SPU/", elfcore_grok_spu_note) | |
f64e188b | 9993 | }; |
8acbedd6 | 9994 | #undef GROKER_ELEMENT |
f64e188b NC |
9995 | int i; |
9996 | ||
9997 | for (i = ARRAY_SIZE (grokers); i--;) | |
8acbedd6 KS |
9998 | { |
9999 | if (in.namesz >= grokers[i].len | |
10000 | && strncmp (in.namedata, grokers[i].string, | |
10001 | grokers[i].len) == 0) | |
10002 | { | |
10003 | if (! grokers[i].func (abfd, & in)) | |
10004 | return FALSE; | |
10005 | break; | |
10006 | } | |
10007 | } | |
f64e188b NC |
10008 | break; |
10009 | } | |
718175fa JK |
10010 | |
10011 | case bfd_object: | |
10012 | if (in.namesz == sizeof "GNU" && strcmp (in.namedata, "GNU") == 0) | |
10013 | { | |
10014 | if (! elfobj_grok_gnu_note (abfd, &in)) | |
10015 | return FALSE; | |
10016 | } | |
e21e5835 NC |
10017 | else if (in.namesz == sizeof "stapsdt" |
10018 | && strcmp (in.namedata, "stapsdt") == 0) | |
10019 | { | |
10020 | if (! elfobj_grok_stapsdt_note (abfd, &in)) | |
10021 | return FALSE; | |
10022 | } | |
718175fa | 10023 | break; |
08a40648 | 10024 | } |
252b5132 RH |
10025 | |
10026 | p = in.descdata + BFD_ALIGN (in.descsz, 4); | |
10027 | } | |
10028 | ||
718175fa JK |
10029 | return TRUE; |
10030 | } | |
10031 | ||
10032 | static bfd_boolean | |
10033 | elf_read_notes (bfd *abfd, file_ptr offset, bfd_size_type size) | |
10034 | { | |
10035 | char *buf; | |
10036 | ||
10037 | if (size <= 0) | |
10038 | return TRUE; | |
10039 | ||
10040 | if (bfd_seek (abfd, offset, SEEK_SET) != 0) | |
10041 | return FALSE; | |
10042 | ||
f64e188b | 10043 | buf = (char *) bfd_malloc (size + 1); |
718175fa JK |
10044 | if (buf == NULL) |
10045 | return FALSE; | |
10046 | ||
f64e188b NC |
10047 | /* PR 17512: file: ec08f814 |
10048 | 0-termintate the buffer so that string searches will not overflow. */ | |
10049 | buf[size] = 0; | |
10050 | ||
718175fa JK |
10051 | if (bfd_bread (buf, size, abfd) != size |
10052 | || !elf_parse_notes (abfd, buf, size, offset)) | |
10053 | { | |
10054 | free (buf); | |
10055 | return FALSE; | |
10056 | } | |
10057 | ||
252b5132 | 10058 | free (buf); |
b34976b6 | 10059 | return TRUE; |
252b5132 | 10060 | } |
98d8431c JB |
10061 | \f |
10062 | /* Providing external access to the ELF program header table. */ | |
10063 | ||
10064 | /* Return an upper bound on the number of bytes required to store a | |
10065 | copy of ABFD's program header table entries. Return -1 if an error | |
10066 | occurs; bfd_get_error will return an appropriate code. */ | |
c044fabd | 10067 | |
98d8431c | 10068 | long |
217aa764 | 10069 | bfd_get_elf_phdr_upper_bound (bfd *abfd) |
98d8431c JB |
10070 | { |
10071 | if (abfd->xvec->flavour != bfd_target_elf_flavour) | |
10072 | { | |
10073 | bfd_set_error (bfd_error_wrong_format); | |
10074 | return -1; | |
10075 | } | |
10076 | ||
936e320b | 10077 | return elf_elfheader (abfd)->e_phnum * sizeof (Elf_Internal_Phdr); |
98d8431c JB |
10078 | } |
10079 | ||
98d8431c JB |
10080 | /* Copy ABFD's program header table entries to *PHDRS. The entries |
10081 | will be stored as an array of Elf_Internal_Phdr structures, as | |
10082 | defined in include/elf/internal.h. To find out how large the | |
10083 | buffer needs to be, call bfd_get_elf_phdr_upper_bound. | |
10084 | ||
10085 | Return the number of program header table entries read, or -1 if an | |
10086 | error occurs; bfd_get_error will return an appropriate code. */ | |
c044fabd | 10087 | |
98d8431c | 10088 | int |
217aa764 | 10089 | bfd_get_elf_phdrs (bfd *abfd, void *phdrs) |
98d8431c JB |
10090 | { |
10091 | int num_phdrs; | |
10092 | ||
10093 | if (abfd->xvec->flavour != bfd_target_elf_flavour) | |
10094 | { | |
10095 | bfd_set_error (bfd_error_wrong_format); | |
10096 | return -1; | |
10097 | } | |
10098 | ||
10099 | num_phdrs = elf_elfheader (abfd)->e_phnum; | |
c044fabd | 10100 | memcpy (phdrs, elf_tdata (abfd)->phdr, |
98d8431c JB |
10101 | num_phdrs * sizeof (Elf_Internal_Phdr)); |
10102 | ||
10103 | return num_phdrs; | |
10104 | } | |
ae4221d7 | 10105 | |
db6751f2 | 10106 | enum elf_reloc_type_class |
7e612e98 AM |
10107 | _bfd_elf_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED, |
10108 | const asection *rel_sec ATTRIBUTE_UNUSED, | |
10109 | const Elf_Internal_Rela *rela ATTRIBUTE_UNUSED) | |
db6751f2 JJ |
10110 | { |
10111 | return reloc_class_normal; | |
10112 | } | |
f8df10f4 | 10113 | |
47d9a591 | 10114 | /* For RELA architectures, return the relocation value for a |
f8df10f4 JJ |
10115 | relocation against a local symbol. */ |
10116 | ||
10117 | bfd_vma | |
217aa764 AM |
10118 | _bfd_elf_rela_local_sym (bfd *abfd, |
10119 | Elf_Internal_Sym *sym, | |
8517fae7 | 10120 | asection **psec, |
217aa764 | 10121 | Elf_Internal_Rela *rel) |
f8df10f4 | 10122 | { |
8517fae7 | 10123 | asection *sec = *psec; |
f8df10f4 JJ |
10124 | bfd_vma relocation; |
10125 | ||
10126 | relocation = (sec->output_section->vma | |
10127 | + sec->output_offset | |
10128 | + sym->st_value); | |
10129 | if ((sec->flags & SEC_MERGE) | |
c629eae0 | 10130 | && ELF_ST_TYPE (sym->st_info) == STT_SECTION |
dbaa2011 | 10131 | && sec->sec_info_type == SEC_INFO_TYPE_MERGE) |
f8df10f4 | 10132 | { |
f8df10f4 | 10133 | rel->r_addend = |
8517fae7 | 10134 | _bfd_merged_section_offset (abfd, psec, |
65765700 | 10135 | elf_section_data (sec)->sec_info, |
753731ee AM |
10136 | sym->st_value + rel->r_addend); |
10137 | if (sec != *psec) | |
10138 | { | |
10139 | /* If we have changed the section, and our original section is | |
10140 | marked with SEC_EXCLUDE, it means that the original | |
10141 | SEC_MERGE section has been completely subsumed in some | |
10142 | other SEC_MERGE section. In this case, we need to leave | |
10143 | some info around for --emit-relocs. */ | |
10144 | if ((sec->flags & SEC_EXCLUDE) != 0) | |
10145 | sec->kept_section = *psec; | |
10146 | sec = *psec; | |
10147 | } | |
8517fae7 AM |
10148 | rel->r_addend -= relocation; |
10149 | rel->r_addend += sec->output_section->vma + sec->output_offset; | |
f8df10f4 JJ |
10150 | } |
10151 | return relocation; | |
10152 | } | |
c629eae0 JJ |
10153 | |
10154 | bfd_vma | |
217aa764 AM |
10155 | _bfd_elf_rel_local_sym (bfd *abfd, |
10156 | Elf_Internal_Sym *sym, | |
10157 | asection **psec, | |
10158 | bfd_vma addend) | |
47d9a591 | 10159 | { |
c629eae0 JJ |
10160 | asection *sec = *psec; |
10161 | ||
dbaa2011 | 10162 | if (sec->sec_info_type != SEC_INFO_TYPE_MERGE) |
c629eae0 JJ |
10163 | return sym->st_value + addend; |
10164 | ||
10165 | return _bfd_merged_section_offset (abfd, psec, | |
65765700 | 10166 | elf_section_data (sec)->sec_info, |
753731ee | 10167 | sym->st_value + addend); |
c629eae0 JJ |
10168 | } |
10169 | ||
10170 | bfd_vma | |
217aa764 | 10171 | _bfd_elf_section_offset (bfd *abfd, |
92e4ec35 | 10172 | struct bfd_link_info *info, |
217aa764 AM |
10173 | asection *sec, |
10174 | bfd_vma offset) | |
c629eae0 | 10175 | { |
68bfbfcc | 10176 | switch (sec->sec_info_type) |
65765700 | 10177 | { |
dbaa2011 | 10178 | case SEC_INFO_TYPE_STABS: |
eea6121a AM |
10179 | return _bfd_stab_section_offset (sec, elf_section_data (sec)->sec_info, |
10180 | offset); | |
dbaa2011 | 10181 | case SEC_INFO_TYPE_EH_FRAME: |
92e4ec35 | 10182 | return _bfd_elf_eh_frame_section_offset (abfd, info, sec, offset); |
65765700 | 10183 | default: |
310fd250 L |
10184 | if ((sec->flags & SEC_ELF_REVERSE_COPY) != 0) |
10185 | { | |
10186 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
10187 | bfd_size_type address_size = bed->s->arch_size / 8; | |
10188 | offset = sec->size - offset - address_size; | |
10189 | } | |
65765700 JJ |
10190 | return offset; |
10191 | } | |
c629eae0 | 10192 | } |
3333a7c3 RM |
10193 | \f |
10194 | /* Create a new BFD as if by bfd_openr. Rather than opening a file, | |
10195 | reconstruct an ELF file by reading the segments out of remote memory | |
10196 | based on the ELF file header at EHDR_VMA and the ELF program headers it | |
10197 | points to. If not null, *LOADBASEP is filled in with the difference | |
10198 | between the VMAs from which the segments were read, and the VMAs the | |
10199 | file headers (and hence BFD's idea of each section's VMA) put them at. | |
10200 | ||
10201 | The function TARGET_READ_MEMORY is called to copy LEN bytes from the | |
10202 | remote memory at target address VMA into the local buffer at MYADDR; it | |
10203 | should return zero on success or an `errno' code on failure. TEMPL must | |
10204 | be a BFD for an ELF target with the word size and byte order found in | |
10205 | the remote memory. */ | |
10206 | ||
10207 | bfd * | |
217aa764 AM |
10208 | bfd_elf_bfd_from_remote_memory |
10209 | (bfd *templ, | |
10210 | bfd_vma ehdr_vma, | |
f0a5d95a | 10211 | bfd_size_type size, |
217aa764 | 10212 | bfd_vma *loadbasep, |
fe78531d | 10213 | int (*target_read_memory) (bfd_vma, bfd_byte *, bfd_size_type)) |
3333a7c3 RM |
10214 | { |
10215 | return (*get_elf_backend_data (templ)->elf_backend_bfd_from_remote_memory) | |
5979d6b6 | 10216 | (templ, ehdr_vma, size, loadbasep, target_read_memory); |
3333a7c3 | 10217 | } |
4c45e5c9 JJ |
10218 | \f |
10219 | long | |
c9727e01 AM |
10220 | _bfd_elf_get_synthetic_symtab (bfd *abfd, |
10221 | long symcount ATTRIBUTE_UNUSED, | |
10222 | asymbol **syms ATTRIBUTE_UNUSED, | |
8615f3f2 | 10223 | long dynsymcount, |
c9727e01 AM |
10224 | asymbol **dynsyms, |
10225 | asymbol **ret) | |
4c45e5c9 JJ |
10226 | { |
10227 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
10228 | asection *relplt; | |
10229 | asymbol *s; | |
10230 | const char *relplt_name; | |
10231 | bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean); | |
10232 | arelent *p; | |
10233 | long count, i, n; | |
10234 | size_t size; | |
10235 | Elf_Internal_Shdr *hdr; | |
10236 | char *names; | |
10237 | asection *plt; | |
10238 | ||
8615f3f2 AM |
10239 | *ret = NULL; |
10240 | ||
90e3cdf2 JJ |
10241 | if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0) |
10242 | return 0; | |
10243 | ||
8615f3f2 AM |
10244 | if (dynsymcount <= 0) |
10245 | return 0; | |
10246 | ||
4c45e5c9 JJ |
10247 | if (!bed->plt_sym_val) |
10248 | return 0; | |
10249 | ||
10250 | relplt_name = bed->relplt_name; | |
10251 | if (relplt_name == NULL) | |
d35fd659 | 10252 | relplt_name = bed->rela_plts_and_copies_p ? ".rela.plt" : ".rel.plt"; |
4c45e5c9 JJ |
10253 | relplt = bfd_get_section_by_name (abfd, relplt_name); |
10254 | if (relplt == NULL) | |
10255 | return 0; | |
10256 | ||
10257 | hdr = &elf_section_data (relplt)->this_hdr; | |
10258 | if (hdr->sh_link != elf_dynsymtab (abfd) | |
10259 | || (hdr->sh_type != SHT_REL && hdr->sh_type != SHT_RELA)) | |
10260 | return 0; | |
10261 | ||
10262 | plt = bfd_get_section_by_name (abfd, ".plt"); | |
10263 | if (plt == NULL) | |
10264 | return 0; | |
10265 | ||
10266 | slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table; | |
c9727e01 | 10267 | if (! (*slurp_relocs) (abfd, relplt, dynsyms, TRUE)) |
4c45e5c9 JJ |
10268 | return -1; |
10269 | ||
eea6121a | 10270 | count = relplt->size / hdr->sh_entsize; |
4c45e5c9 JJ |
10271 | size = count * sizeof (asymbol); |
10272 | p = relplt->relocation; | |
cb53bf42 | 10273 | for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel) |
041de40d AM |
10274 | { |
10275 | size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt"); | |
10276 | if (p->addend != 0) | |
10277 | { | |
10278 | #ifdef BFD64 | |
10279 | size += sizeof ("+0x") - 1 + 8 + 8 * (bed->s->elfclass == ELFCLASS64); | |
10280 | #else | |
10281 | size += sizeof ("+0x") - 1 + 8; | |
10282 | #endif | |
10283 | } | |
10284 | } | |
4c45e5c9 | 10285 | |
a50b1753 | 10286 | s = *ret = (asymbol *) bfd_malloc (size); |
4c45e5c9 JJ |
10287 | if (s == NULL) |
10288 | return -1; | |
10289 | ||
10290 | names = (char *) (s + count); | |
10291 | p = relplt->relocation; | |
10292 | n = 0; | |
cb53bf42 | 10293 | for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel) |
4c45e5c9 JJ |
10294 | { |
10295 | size_t len; | |
10296 | bfd_vma addr; | |
10297 | ||
10298 | addr = bed->plt_sym_val (i, plt, p); | |
10299 | if (addr == (bfd_vma) -1) | |
10300 | continue; | |
10301 | ||
10302 | *s = **p->sym_ptr_ptr; | |
65a7a66f AM |
10303 | /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since |
10304 | we are defining a symbol, ensure one of them is set. */ | |
10305 | if ((s->flags & BSF_LOCAL) == 0) | |
10306 | s->flags |= BSF_GLOBAL; | |
6ba2a415 | 10307 | s->flags |= BSF_SYNTHETIC; |
4c45e5c9 JJ |
10308 | s->section = plt; |
10309 | s->value = addr - plt->vma; | |
10310 | s->name = names; | |
8f39ba8e | 10311 | s->udata.p = NULL; |
4c45e5c9 JJ |
10312 | len = strlen ((*p->sym_ptr_ptr)->name); |
10313 | memcpy (names, (*p->sym_ptr_ptr)->name, len); | |
10314 | names += len; | |
041de40d AM |
10315 | if (p->addend != 0) |
10316 | { | |
1d770845 | 10317 | char buf[30], *a; |
d324f6d6 | 10318 | |
041de40d AM |
10319 | memcpy (names, "+0x", sizeof ("+0x") - 1); |
10320 | names += sizeof ("+0x") - 1; | |
1d770845 L |
10321 | bfd_sprintf_vma (abfd, buf, p->addend); |
10322 | for (a = buf; *a == '0'; ++a) | |
10323 | ; | |
10324 | len = strlen (a); | |
10325 | memcpy (names, a, len); | |
10326 | names += len; | |
041de40d | 10327 | } |
4c45e5c9 JJ |
10328 | memcpy (names, "@plt", sizeof ("@plt")); |
10329 | names += sizeof ("@plt"); | |
8f39ba8e | 10330 | ++s, ++n; |
4c45e5c9 JJ |
10331 | } |
10332 | ||
10333 | return n; | |
10334 | } | |
3d7f7666 | 10335 | |
3b22753a L |
10336 | /* It is only used by x86-64 so far. */ |
10337 | asection _bfd_elf_large_com_section | |
10338 | = BFD_FAKE_SECTION (_bfd_elf_large_com_section, | |
f592407e | 10339 | SEC_IS_COMMON, NULL, "LARGE_COMMON", 0); |
ecca9871 | 10340 | |
d1036acb | 10341 | void |
78245035 L |
10342 | _bfd_elf_post_process_headers (bfd * abfd, |
10343 | struct bfd_link_info * link_info ATTRIBUTE_UNUSED) | |
d1036acb L |
10344 | { |
10345 | Elf_Internal_Ehdr * i_ehdrp; /* ELF file header, internal form. */ | |
10346 | ||
10347 | i_ehdrp = elf_elfheader (abfd); | |
10348 | ||
10349 | i_ehdrp->e_ident[EI_OSABI] = get_elf_backend_data (abfd)->elf_osabi; | |
d8045f23 NC |
10350 | |
10351 | /* To make things simpler for the loader on Linux systems we set the | |
9c55345c | 10352 | osabi field to ELFOSABI_GNU if the binary contains symbols of |
f64b2e8d | 10353 | the STT_GNU_IFUNC type or STB_GNU_UNIQUE binding. */ |
d8045f23 | 10354 | if (i_ehdrp->e_ident[EI_OSABI] == ELFOSABI_NONE |
f64b2e8d | 10355 | && elf_tdata (abfd)->has_gnu_symbols) |
9c55345c | 10356 | i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_GNU; |
d1036acb | 10357 | } |
fcb93ecf PB |
10358 | |
10359 | ||
10360 | /* Return TRUE for ELF symbol types that represent functions. | |
10361 | This is the default version of this function, which is sufficient for | |
d8045f23 | 10362 | most targets. It returns true if TYPE is STT_FUNC or STT_GNU_IFUNC. */ |
fcb93ecf PB |
10363 | |
10364 | bfd_boolean | |
10365 | _bfd_elf_is_function_type (unsigned int type) | |
10366 | { | |
d8045f23 NC |
10367 | return (type == STT_FUNC |
10368 | || type == STT_GNU_IFUNC); | |
fcb93ecf | 10369 | } |
9f296da3 | 10370 | |
aef36ac1 AM |
10371 | /* If the ELF symbol SYM might be a function in SEC, return the |
10372 | function size and set *CODE_OFF to the function's entry point, | |
10373 | otherwise return zero. */ | |
9f296da3 | 10374 | |
aef36ac1 AM |
10375 | bfd_size_type |
10376 | _bfd_elf_maybe_function_sym (const asymbol *sym, asection *sec, | |
10377 | bfd_vma *code_off) | |
9f296da3 | 10378 | { |
aef36ac1 AM |
10379 | bfd_size_type size; |
10380 | ||
ff9e0f5b | 10381 | if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT |
aef36ac1 AM |
10382 | | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0 |
10383 | || sym->section != sec) | |
10384 | return 0; | |
ff9e0f5b | 10385 | |
ff9e0f5b | 10386 | *code_off = sym->value; |
aef36ac1 AM |
10387 | size = 0; |
10388 | if (!(sym->flags & BSF_SYNTHETIC)) | |
10389 | size = ((elf_symbol_type *) sym)->internal_elf_sym.st_size; | |
10390 | if (size == 0) | |
10391 | size = 1; | |
10392 | return size; | |
9f296da3 | 10393 | } |