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 | 53 | static bfd_boolean prep_headers (bfd *); |
ef10c3ac | 54 | static bfd_boolean swap_out_syms (bfd *, struct elf_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 | } | |
b1fa9dd6 | 345 | |
06614111 NC |
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 | 1069 | int compression_header_size; |
dab394de | 1070 | bfd_size_type uncompressed_size; |
151411f8 L |
1071 | bfd_boolean compressed |
1072 | = bfd_is_section_compressed_with_header (abfd, newsect, | |
dab394de L |
1073 | &compression_header_size, |
1074 | &uncompressed_size); | |
4a114e3e | 1075 | |
151411f8 | 1076 | if (compressed) |
4a114e3e L |
1077 | { |
1078 | /* Compressed section. Check if we should decompress. */ | |
1079 | if ((abfd->flags & BFD_DECOMPRESS)) | |
1080 | action = decompress; | |
1081 | } | |
151411f8 L |
1082 | |
1083 | /* Compress the uncompressed section or convert from/to .zdebug* | |
1084 | section. Check if we should compress. */ | |
1085 | if (action == nothing) | |
4a114e3e | 1086 | { |
151411f8 L |
1087 | if (newsect->size != 0 |
1088 | && (abfd->flags & BFD_COMPRESS) | |
1089 | && compression_header_size >= 0 | |
dab394de | 1090 | && uncompressed_size > 0 |
151411f8 L |
1091 | && (!compressed |
1092 | || ((compression_header_size > 0) | |
1093 | != ((abfd->flags & BFD_COMPRESS_GABI) != 0)))) | |
4a114e3e | 1094 | action = compress; |
151411f8 L |
1095 | else |
1096 | return TRUE; | |
4a114e3e L |
1097 | } |
1098 | ||
151411f8 | 1099 | if (action == compress) |
4a114e3e | 1100 | { |
4a114e3e L |
1101 | if (!bfd_init_section_compress_status (abfd, newsect)) |
1102 | { | |
1103 | (*_bfd_error_handler) | |
bc823199 | 1104 | (_("%B: unable to initialize compress status for section %s"), |
4a114e3e L |
1105 | abfd, name); |
1106 | return FALSE; | |
1107 | } | |
151411f8 L |
1108 | } |
1109 | else | |
1110 | { | |
4a114e3e L |
1111 | if (!bfd_init_section_decompress_status (abfd, newsect)) |
1112 | { | |
1113 | (*_bfd_error_handler) | |
bc823199 | 1114 | (_("%B: unable to initialize decompress status for section %s"), |
4a114e3e L |
1115 | abfd, name); |
1116 | return FALSE; | |
1117 | } | |
151411f8 L |
1118 | } |
1119 | ||
f6fe1ccd | 1120 | if (abfd->is_linker_input) |
151411f8 | 1121 | { |
f6fe1ccd L |
1122 | if (name[1] == 'z' |
1123 | && (action == decompress | |
1124 | || (action == compress | |
1125 | && (abfd->flags & BFD_COMPRESS_GABI) != 0))) | |
4e011fb5 | 1126 | { |
f6fe1ccd L |
1127 | /* Convert section name from .zdebug_* to .debug_* so |
1128 | that linker will consider this section as a debug | |
1129 | section. */ | |
1130 | char *new_name = convert_zdebug_to_debug (abfd, name); | |
151411f8 L |
1131 | if (new_name == NULL) |
1132 | return FALSE; | |
f6fe1ccd | 1133 | bfd_rename_section (abfd, newsect, new_name); |
151411f8 | 1134 | } |
4a114e3e | 1135 | } |
f6fe1ccd L |
1136 | else |
1137 | /* For objdump, don't rename the section. For objcopy, delay | |
1138 | section rename to elf_fake_sections. */ | |
1139 | newsect->flags |= SEC_ELF_RENAME; | |
4a114e3e L |
1140 | } |
1141 | ||
b34976b6 | 1142 | return TRUE; |
252b5132 RH |
1143 | } |
1144 | ||
252b5132 RH |
1145 | const char *const bfd_elf_section_type_names[] = { |
1146 | "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB", | |
1147 | "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE", | |
1148 | "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM", | |
1149 | }; | |
1150 | ||
1049f94e | 1151 | /* ELF relocs are against symbols. If we are producing relocatable |
252b5132 RH |
1152 | output, and the reloc is against an external symbol, and nothing |
1153 | has given us any additional addend, the resulting reloc will also | |
1154 | be against the same symbol. In such a case, we don't want to | |
1155 | change anything about the way the reloc is handled, since it will | |
1156 | all be done at final link time. Rather than put special case code | |
1157 | into bfd_perform_relocation, all the reloc types use this howto | |
1158 | function. It just short circuits the reloc if producing | |
1049f94e | 1159 | relocatable output against an external symbol. */ |
252b5132 | 1160 | |
252b5132 | 1161 | bfd_reloc_status_type |
217aa764 AM |
1162 | bfd_elf_generic_reloc (bfd *abfd ATTRIBUTE_UNUSED, |
1163 | arelent *reloc_entry, | |
1164 | asymbol *symbol, | |
1165 | void *data ATTRIBUTE_UNUSED, | |
1166 | asection *input_section, | |
1167 | bfd *output_bfd, | |
1168 | char **error_message ATTRIBUTE_UNUSED) | |
1169 | { | |
1170 | if (output_bfd != NULL | |
252b5132 RH |
1171 | && (symbol->flags & BSF_SECTION_SYM) == 0 |
1172 | && (! reloc_entry->howto->partial_inplace | |
1173 | || reloc_entry->addend == 0)) | |
1174 | { | |
1175 | reloc_entry->address += input_section->output_offset; | |
1176 | return bfd_reloc_ok; | |
1177 | } | |
1178 | ||
1179 | return bfd_reloc_continue; | |
1180 | } | |
1181 | \f | |
0ac4564e L |
1182 | /* Copy the program header and other data from one object module to |
1183 | another. */ | |
252b5132 | 1184 | |
b34976b6 | 1185 | bfd_boolean |
217aa764 | 1186 | _bfd_elf_copy_private_bfd_data (bfd *ibfd, bfd *obfd) |
2d502050 L |
1187 | { |
1188 | if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour | |
1189 | || bfd_get_flavour (obfd) != bfd_target_elf_flavour) | |
b34976b6 | 1190 | return TRUE; |
2d502050 | 1191 | |
57b828ef L |
1192 | if (!elf_flags_init (obfd)) |
1193 | { | |
1194 | elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags; | |
1195 | elf_flags_init (obfd) = TRUE; | |
1196 | } | |
2d502050 | 1197 | |
0ac4564e | 1198 | elf_gp (obfd) = elf_gp (ibfd); |
57b828ef L |
1199 | |
1200 | /* Also copy the EI_OSABI field. */ | |
1201 | elf_elfheader (obfd)->e_ident[EI_OSABI] = | |
1202 | elf_elfheader (ibfd)->e_ident[EI_OSABI]; | |
104d59d1 JM |
1203 | |
1204 | /* Copy object attributes. */ | |
1205 | _bfd_elf_copy_obj_attributes (ibfd, obfd); | |
b34976b6 | 1206 | return TRUE; |
2d502050 L |
1207 | } |
1208 | ||
cedc298e L |
1209 | static const char * |
1210 | get_segment_type (unsigned int p_type) | |
1211 | { | |
1212 | const char *pt; | |
1213 | switch (p_type) | |
1214 | { | |
1215 | case PT_NULL: pt = "NULL"; break; | |
1216 | case PT_LOAD: pt = "LOAD"; break; | |
1217 | case PT_DYNAMIC: pt = "DYNAMIC"; break; | |
1218 | case PT_INTERP: pt = "INTERP"; break; | |
1219 | case PT_NOTE: pt = "NOTE"; break; | |
1220 | case PT_SHLIB: pt = "SHLIB"; break; | |
1221 | case PT_PHDR: pt = "PHDR"; break; | |
1222 | case PT_TLS: pt = "TLS"; break; | |
1223 | case PT_GNU_EH_FRAME: pt = "EH_FRAME"; break; | |
2b05f1b7 | 1224 | case PT_GNU_STACK: pt = "STACK"; break; |
cedc298e L |
1225 | case PT_GNU_RELRO: pt = "RELRO"; break; |
1226 | default: pt = NULL; break; | |
1227 | } | |
1228 | return pt; | |
1229 | } | |
1230 | ||
f0b79d91 L |
1231 | /* Print out the program headers. */ |
1232 | ||
b34976b6 | 1233 | bfd_boolean |
217aa764 | 1234 | _bfd_elf_print_private_bfd_data (bfd *abfd, void *farg) |
252b5132 | 1235 | { |
a50b1753 | 1236 | FILE *f = (FILE *) farg; |
252b5132 RH |
1237 | Elf_Internal_Phdr *p; |
1238 | asection *s; | |
1239 | bfd_byte *dynbuf = NULL; | |
1240 | ||
1241 | p = elf_tdata (abfd)->phdr; | |
1242 | if (p != NULL) | |
1243 | { | |
1244 | unsigned int i, c; | |
1245 | ||
1246 | fprintf (f, _("\nProgram Header:\n")); | |
1247 | c = elf_elfheader (abfd)->e_phnum; | |
1248 | for (i = 0; i < c; i++, p++) | |
1249 | { | |
cedc298e | 1250 | const char *pt = get_segment_type (p->p_type); |
252b5132 RH |
1251 | char buf[20]; |
1252 | ||
cedc298e | 1253 | if (pt == NULL) |
252b5132 | 1254 | { |
cedc298e L |
1255 | sprintf (buf, "0x%lx", p->p_type); |
1256 | pt = buf; | |
252b5132 | 1257 | } |
dc810e39 | 1258 | fprintf (f, "%8s off 0x", pt); |
60b89a18 | 1259 | bfd_fprintf_vma (abfd, f, p->p_offset); |
252b5132 | 1260 | fprintf (f, " vaddr 0x"); |
60b89a18 | 1261 | bfd_fprintf_vma (abfd, f, p->p_vaddr); |
252b5132 | 1262 | fprintf (f, " paddr 0x"); |
60b89a18 | 1263 | bfd_fprintf_vma (abfd, f, p->p_paddr); |
252b5132 RH |
1264 | fprintf (f, " align 2**%u\n", bfd_log2 (p->p_align)); |
1265 | fprintf (f, " filesz 0x"); | |
60b89a18 | 1266 | bfd_fprintf_vma (abfd, f, p->p_filesz); |
252b5132 | 1267 | fprintf (f, " memsz 0x"); |
60b89a18 | 1268 | bfd_fprintf_vma (abfd, f, p->p_memsz); |
252b5132 RH |
1269 | fprintf (f, " flags %c%c%c", |
1270 | (p->p_flags & PF_R) != 0 ? 'r' : '-', | |
1271 | (p->p_flags & PF_W) != 0 ? 'w' : '-', | |
1272 | (p->p_flags & PF_X) != 0 ? 'x' : '-'); | |
dc810e39 AM |
1273 | if ((p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X)) != 0) |
1274 | fprintf (f, " %lx", p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X)); | |
252b5132 RH |
1275 | fprintf (f, "\n"); |
1276 | } | |
1277 | } | |
1278 | ||
1279 | s = bfd_get_section_by_name (abfd, ".dynamic"); | |
1280 | if (s != NULL) | |
1281 | { | |
cb33740c | 1282 | unsigned int elfsec; |
dc810e39 | 1283 | unsigned long shlink; |
252b5132 RH |
1284 | bfd_byte *extdyn, *extdynend; |
1285 | size_t extdynsize; | |
217aa764 | 1286 | void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *); |
252b5132 RH |
1287 | |
1288 | fprintf (f, _("\nDynamic Section:\n")); | |
1289 | ||
eea6121a | 1290 | if (!bfd_malloc_and_get_section (abfd, s, &dynbuf)) |
252b5132 RH |
1291 | goto error_return; |
1292 | ||
1293 | elfsec = _bfd_elf_section_from_bfd_section (abfd, s); | |
cb33740c | 1294 | if (elfsec == SHN_BAD) |
252b5132 | 1295 | goto error_return; |
dc810e39 | 1296 | shlink = elf_elfsections (abfd)[elfsec]->sh_link; |
252b5132 RH |
1297 | |
1298 | extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn; | |
1299 | swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in; | |
1300 | ||
1301 | extdyn = dynbuf; | |
06614111 NC |
1302 | /* PR 17512: file: 6f427532. */ |
1303 | if (s->size < extdynsize) | |
1304 | goto error_return; | |
eea6121a | 1305 | extdynend = extdyn + s->size; |
1036838a NC |
1306 | /* PR 17512: file: id:000006,sig:06,src:000000,op:flip4,pos:5664. |
1307 | Fix range check. */ | |
1308 | for (; extdyn <= (extdynend - extdynsize); extdyn += extdynsize) | |
252b5132 RH |
1309 | { |
1310 | Elf_Internal_Dyn dyn; | |
ad9563d6 | 1311 | const char *name = ""; |
252b5132 | 1312 | char ab[20]; |
b34976b6 | 1313 | bfd_boolean stringp; |
ad9563d6 | 1314 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
252b5132 | 1315 | |
217aa764 | 1316 | (*swap_dyn_in) (abfd, extdyn, &dyn); |
252b5132 RH |
1317 | |
1318 | if (dyn.d_tag == DT_NULL) | |
1319 | break; | |
1320 | ||
b34976b6 | 1321 | stringp = FALSE; |
252b5132 RH |
1322 | switch (dyn.d_tag) |
1323 | { | |
1324 | default: | |
ad9563d6 CM |
1325 | if (bed->elf_backend_get_target_dtag) |
1326 | name = (*bed->elf_backend_get_target_dtag) (dyn.d_tag); | |
1327 | ||
1328 | if (!strcmp (name, "")) | |
1329 | { | |
1330 | sprintf (ab, "0x%lx", (unsigned long) dyn.d_tag); | |
1331 | name = ab; | |
1332 | } | |
252b5132 RH |
1333 | break; |
1334 | ||
b34976b6 | 1335 | case DT_NEEDED: name = "NEEDED"; stringp = TRUE; break; |
252b5132 RH |
1336 | case DT_PLTRELSZ: name = "PLTRELSZ"; break; |
1337 | case DT_PLTGOT: name = "PLTGOT"; break; | |
1338 | case DT_HASH: name = "HASH"; break; | |
1339 | case DT_STRTAB: name = "STRTAB"; break; | |
1340 | case DT_SYMTAB: name = "SYMTAB"; break; | |
1341 | case DT_RELA: name = "RELA"; break; | |
1342 | case DT_RELASZ: name = "RELASZ"; break; | |
1343 | case DT_RELAENT: name = "RELAENT"; break; | |
1344 | case DT_STRSZ: name = "STRSZ"; break; | |
1345 | case DT_SYMENT: name = "SYMENT"; break; | |
1346 | case DT_INIT: name = "INIT"; break; | |
1347 | case DT_FINI: name = "FINI"; break; | |
b34976b6 AM |
1348 | case DT_SONAME: name = "SONAME"; stringp = TRUE; break; |
1349 | case DT_RPATH: name = "RPATH"; stringp = TRUE; break; | |
252b5132 RH |
1350 | case DT_SYMBOLIC: name = "SYMBOLIC"; break; |
1351 | case DT_REL: name = "REL"; break; | |
1352 | case DT_RELSZ: name = "RELSZ"; break; | |
1353 | case DT_RELENT: name = "RELENT"; break; | |
1354 | case DT_PLTREL: name = "PLTREL"; break; | |
1355 | case DT_DEBUG: name = "DEBUG"; break; | |
1356 | case DT_TEXTREL: name = "TEXTREL"; break; | |
1357 | case DT_JMPREL: name = "JMPREL"; break; | |
94558834 L |
1358 | case DT_BIND_NOW: name = "BIND_NOW"; break; |
1359 | case DT_INIT_ARRAY: name = "INIT_ARRAY"; break; | |
1360 | case DT_FINI_ARRAY: name = "FINI_ARRAY"; break; | |
1361 | case DT_INIT_ARRAYSZ: name = "INIT_ARRAYSZ"; break; | |
1362 | case DT_FINI_ARRAYSZ: name = "FINI_ARRAYSZ"; break; | |
b34976b6 | 1363 | case DT_RUNPATH: name = "RUNPATH"; stringp = TRUE; break; |
94558834 L |
1364 | case DT_FLAGS: name = "FLAGS"; break; |
1365 | case DT_PREINIT_ARRAY: name = "PREINIT_ARRAY"; break; | |
1366 | case DT_PREINIT_ARRAYSZ: name = "PREINIT_ARRAYSZ"; break; | |
d48188b9 | 1367 | case DT_CHECKSUM: name = "CHECKSUM"; break; |
94558834 L |
1368 | case DT_PLTPADSZ: name = "PLTPADSZ"; break; |
1369 | case DT_MOVEENT: name = "MOVEENT"; break; | |
1370 | case DT_MOVESZ: name = "MOVESZ"; break; | |
1371 | case DT_FEATURE: name = "FEATURE"; break; | |
1372 | case DT_POSFLAG_1: name = "POSFLAG_1"; break; | |
1373 | case DT_SYMINSZ: name = "SYMINSZ"; break; | |
1374 | case DT_SYMINENT: name = "SYMINENT"; break; | |
b34976b6 AM |
1375 | case DT_CONFIG: name = "CONFIG"; stringp = TRUE; break; |
1376 | case DT_DEPAUDIT: name = "DEPAUDIT"; stringp = TRUE; break; | |
1377 | case DT_AUDIT: name = "AUDIT"; stringp = TRUE; break; | |
94558834 L |
1378 | case DT_PLTPAD: name = "PLTPAD"; break; |
1379 | case DT_MOVETAB: name = "MOVETAB"; break; | |
1380 | case DT_SYMINFO: name = "SYMINFO"; break; | |
1381 | case DT_RELACOUNT: name = "RELACOUNT"; break; | |
1382 | case DT_RELCOUNT: name = "RELCOUNT"; break; | |
1383 | case DT_FLAGS_1: name = "FLAGS_1"; break; | |
252b5132 RH |
1384 | case DT_VERSYM: name = "VERSYM"; break; |
1385 | case DT_VERDEF: name = "VERDEF"; break; | |
1386 | case DT_VERDEFNUM: name = "VERDEFNUM"; break; | |
1387 | case DT_VERNEED: name = "VERNEED"; break; | |
1388 | case DT_VERNEEDNUM: name = "VERNEEDNUM"; break; | |
b34976b6 | 1389 | case DT_AUXILIARY: name = "AUXILIARY"; stringp = TRUE; break; |
94558834 | 1390 | case DT_USED: name = "USED"; break; |
b34976b6 | 1391 | case DT_FILTER: name = "FILTER"; stringp = TRUE; break; |
fdc90cb4 | 1392 | case DT_GNU_HASH: name = "GNU_HASH"; break; |
252b5132 RH |
1393 | } |
1394 | ||
ad9563d6 | 1395 | fprintf (f, " %-20s ", name); |
252b5132 | 1396 | if (! stringp) |
a1f3c56e AN |
1397 | { |
1398 | fprintf (f, "0x"); | |
1399 | bfd_fprintf_vma (abfd, f, dyn.d_un.d_val); | |
1400 | } | |
252b5132 RH |
1401 | else |
1402 | { | |
1403 | const char *string; | |
dc810e39 | 1404 | unsigned int tagv = dyn.d_un.d_val; |
252b5132 | 1405 | |
dc810e39 | 1406 | string = bfd_elf_string_from_elf_section (abfd, shlink, tagv); |
252b5132 RH |
1407 | if (string == NULL) |
1408 | goto error_return; | |
1409 | fprintf (f, "%s", string); | |
1410 | } | |
1411 | fprintf (f, "\n"); | |
1412 | } | |
1413 | ||
1414 | free (dynbuf); | |
1415 | dynbuf = NULL; | |
1416 | } | |
1417 | ||
1418 | if ((elf_dynverdef (abfd) != 0 && elf_tdata (abfd)->verdef == NULL) | |
1419 | || (elf_dynverref (abfd) != 0 && elf_tdata (abfd)->verref == NULL)) | |
1420 | { | |
fc0e6df6 | 1421 | if (! _bfd_elf_slurp_version_tables (abfd, FALSE)) |
b34976b6 | 1422 | return FALSE; |
252b5132 RH |
1423 | } |
1424 | ||
1425 | if (elf_dynverdef (abfd) != 0) | |
1426 | { | |
1427 | Elf_Internal_Verdef *t; | |
1428 | ||
1429 | fprintf (f, _("\nVersion definitions:\n")); | |
1430 | for (t = elf_tdata (abfd)->verdef; t != NULL; t = t->vd_nextdef) | |
1431 | { | |
1432 | fprintf (f, "%d 0x%2.2x 0x%8.8lx %s\n", t->vd_ndx, | |
d0fb9a8d JJ |
1433 | t->vd_flags, t->vd_hash, |
1434 | t->vd_nodename ? t->vd_nodename : "<corrupt>"); | |
1435 | if (t->vd_auxptr != NULL && t->vd_auxptr->vda_nextptr != NULL) | |
252b5132 RH |
1436 | { |
1437 | Elf_Internal_Verdaux *a; | |
1438 | ||
1439 | fprintf (f, "\t"); | |
1440 | for (a = t->vd_auxptr->vda_nextptr; | |
1441 | a != NULL; | |
1442 | a = a->vda_nextptr) | |
d0fb9a8d JJ |
1443 | fprintf (f, "%s ", |
1444 | a->vda_nodename ? a->vda_nodename : "<corrupt>"); | |
252b5132 RH |
1445 | fprintf (f, "\n"); |
1446 | } | |
1447 | } | |
1448 | } | |
1449 | ||
1450 | if (elf_dynverref (abfd) != 0) | |
1451 | { | |
1452 | Elf_Internal_Verneed *t; | |
1453 | ||
1454 | fprintf (f, _("\nVersion References:\n")); | |
1455 | for (t = elf_tdata (abfd)->verref; t != NULL; t = t->vn_nextref) | |
1456 | { | |
1457 | Elf_Internal_Vernaux *a; | |
1458 | ||
d0fb9a8d JJ |
1459 | fprintf (f, _(" required from %s:\n"), |
1460 | t->vn_filename ? t->vn_filename : "<corrupt>"); | |
252b5132 RH |
1461 | for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) |
1462 | fprintf (f, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a->vna_hash, | |
d0fb9a8d JJ |
1463 | a->vna_flags, a->vna_other, |
1464 | a->vna_nodename ? a->vna_nodename : "<corrupt>"); | |
252b5132 RH |
1465 | } |
1466 | } | |
1467 | ||
b34976b6 | 1468 | return TRUE; |
252b5132 RH |
1469 | |
1470 | error_return: | |
1471 | if (dynbuf != NULL) | |
1472 | free (dynbuf); | |
b34976b6 | 1473 | return FALSE; |
252b5132 RH |
1474 | } |
1475 | ||
bb4d2ac2 L |
1476 | /* Get version string. */ |
1477 | ||
1478 | const char * | |
60bb06bc L |
1479 | _bfd_elf_get_symbol_version_string (bfd *abfd, asymbol *symbol, |
1480 | bfd_boolean *hidden) | |
bb4d2ac2 L |
1481 | { |
1482 | const char *version_string = NULL; | |
1483 | if (elf_dynversym (abfd) != 0 | |
1484 | && (elf_dynverdef (abfd) != 0 || elf_dynverref (abfd) != 0)) | |
1485 | { | |
1486 | unsigned int vernum = ((elf_symbol_type *) symbol)->version; | |
1487 | ||
1488 | *hidden = (vernum & VERSYM_HIDDEN) != 0; | |
1489 | vernum &= VERSYM_VERSION; | |
1490 | ||
1491 | if (vernum == 0) | |
1492 | version_string = ""; | |
1493 | else if (vernum == 1) | |
1494 | version_string = "Base"; | |
1495 | else if (vernum <= elf_tdata (abfd)->cverdefs) | |
1496 | version_string = | |
1497 | elf_tdata (abfd)->verdef[vernum - 1].vd_nodename; | |
1498 | else | |
1499 | { | |
1500 | Elf_Internal_Verneed *t; | |
1501 | ||
1502 | version_string = ""; | |
1503 | for (t = elf_tdata (abfd)->verref; | |
1504 | t != NULL; | |
1505 | t = t->vn_nextref) | |
1506 | { | |
1507 | Elf_Internal_Vernaux *a; | |
1508 | ||
1509 | for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) | |
1510 | { | |
1511 | if (a->vna_other == vernum) | |
1512 | { | |
1513 | version_string = a->vna_nodename; | |
1514 | break; | |
1515 | } | |
1516 | } | |
1517 | } | |
1518 | } | |
1519 | } | |
1520 | return version_string; | |
1521 | } | |
1522 | ||
252b5132 RH |
1523 | /* Display ELF-specific fields of a symbol. */ |
1524 | ||
1525 | void | |
217aa764 AM |
1526 | bfd_elf_print_symbol (bfd *abfd, |
1527 | void *filep, | |
1528 | asymbol *symbol, | |
1529 | bfd_print_symbol_type how) | |
252b5132 | 1530 | { |
a50b1753 | 1531 | FILE *file = (FILE *) filep; |
252b5132 RH |
1532 | switch (how) |
1533 | { | |
1534 | case bfd_print_symbol_name: | |
1535 | fprintf (file, "%s", symbol->name); | |
1536 | break; | |
1537 | case bfd_print_symbol_more: | |
1538 | fprintf (file, "elf "); | |
60b89a18 | 1539 | bfd_fprintf_vma (abfd, file, symbol->value); |
0af1713e | 1540 | fprintf (file, " %lx", (unsigned long) symbol->flags); |
252b5132 RH |
1541 | break; |
1542 | case bfd_print_symbol_all: | |
1543 | { | |
4e8a9624 AM |
1544 | const char *section_name; |
1545 | const char *name = NULL; | |
9c5bfbb7 | 1546 | const struct elf_backend_data *bed; |
7a13edea | 1547 | unsigned char st_other; |
dbb410c3 | 1548 | bfd_vma val; |
bb4d2ac2 L |
1549 | const char *version_string; |
1550 | bfd_boolean hidden; | |
c044fabd | 1551 | |
252b5132 | 1552 | section_name = symbol->section ? symbol->section->name : "(*none*)"; |
587ff49e RH |
1553 | |
1554 | bed = get_elf_backend_data (abfd); | |
1555 | if (bed->elf_backend_print_symbol_all) | |
c044fabd | 1556 | name = (*bed->elf_backend_print_symbol_all) (abfd, filep, symbol); |
587ff49e RH |
1557 | |
1558 | if (name == NULL) | |
1559 | { | |
7ee38065 | 1560 | name = symbol->name; |
217aa764 | 1561 | bfd_print_symbol_vandf (abfd, file, symbol); |
587ff49e RH |
1562 | } |
1563 | ||
252b5132 RH |
1564 | fprintf (file, " %s\t", section_name); |
1565 | /* Print the "other" value for a symbol. For common symbols, | |
1566 | we've already printed the size; now print the alignment. | |
1567 | For other symbols, we have no specified alignment, and | |
1568 | we've printed the address; now print the size. */ | |
dcf6c779 | 1569 | if (symbol->section && bfd_is_com_section (symbol->section)) |
dbb410c3 AM |
1570 | val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_value; |
1571 | else | |
1572 | val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_size; | |
1573 | bfd_fprintf_vma (abfd, file, val); | |
252b5132 RH |
1574 | |
1575 | /* If we have version information, print it. */ | |
60bb06bc L |
1576 | version_string = _bfd_elf_get_symbol_version_string (abfd, |
1577 | symbol, | |
1578 | &hidden); | |
bb4d2ac2 | 1579 | if (version_string) |
252b5132 | 1580 | { |
bb4d2ac2 | 1581 | if (!hidden) |
252b5132 RH |
1582 | fprintf (file, " %-11s", version_string); |
1583 | else | |
1584 | { | |
1585 | int i; | |
1586 | ||
1587 | fprintf (file, " (%s)", version_string); | |
1588 | for (i = 10 - strlen (version_string); i > 0; --i) | |
1589 | putc (' ', file); | |
1590 | } | |
1591 | } | |
1592 | ||
1593 | /* If the st_other field is not zero, print it. */ | |
7a13edea | 1594 | st_other = ((elf_symbol_type *) symbol)->internal_elf_sym.st_other; |
c044fabd | 1595 | |
7a13edea NC |
1596 | switch (st_other) |
1597 | { | |
1598 | case 0: break; | |
1599 | case STV_INTERNAL: fprintf (file, " .internal"); break; | |
1600 | case STV_HIDDEN: fprintf (file, " .hidden"); break; | |
1601 | case STV_PROTECTED: fprintf (file, " .protected"); break; | |
1602 | default: | |
1603 | /* Some other non-defined flags are also present, so print | |
1604 | everything hex. */ | |
1605 | fprintf (file, " 0x%02x", (unsigned int) st_other); | |
1606 | } | |
252b5132 | 1607 | |
587ff49e | 1608 | fprintf (file, " %s", name); |
252b5132 RH |
1609 | } |
1610 | break; | |
1611 | } | |
1612 | } | |
252b5132 RH |
1613 | \f |
1614 | /* ELF .o/exec file reading */ | |
1615 | ||
c044fabd | 1616 | /* Create a new bfd section from an ELF section header. */ |
252b5132 | 1617 | |
b34976b6 | 1618 | bfd_boolean |
217aa764 | 1619 | bfd_section_from_shdr (bfd *abfd, unsigned int shindex) |
252b5132 | 1620 | { |
4fbb74a6 AM |
1621 | Elf_Internal_Shdr *hdr; |
1622 | Elf_Internal_Ehdr *ehdr; | |
1623 | const struct elf_backend_data *bed; | |
90937f86 | 1624 | const char *name; |
bf67003b NC |
1625 | bfd_boolean ret = TRUE; |
1626 | static bfd_boolean * sections_being_created = NULL; | |
5a4b0ccc | 1627 | static bfd * sections_being_created_abfd = NULL; |
bf67003b | 1628 | static unsigned int nesting = 0; |
252b5132 | 1629 | |
4fbb74a6 AM |
1630 | if (shindex >= elf_numsections (abfd)) |
1631 | return FALSE; | |
1632 | ||
bf67003b NC |
1633 | if (++ nesting > 3) |
1634 | { | |
1635 | /* PR17512: A corrupt ELF binary might contain a recursive group of | |
06614111 | 1636 | sections, with each the string indicies pointing to the next in the |
bf67003b NC |
1637 | loop. Detect this here, by refusing to load a section that we are |
1638 | already in the process of loading. We only trigger this test if | |
1639 | we have nested at least three sections deep as normal ELF binaries | |
5a4b0ccc NC |
1640 | can expect to recurse at least once. |
1641 | ||
1642 | FIXME: It would be better if this array was attached to the bfd, | |
1643 | rather than being held in a static pointer. */ | |
1644 | ||
1645 | if (sections_being_created_abfd != abfd) | |
1646 | sections_being_created = NULL; | |
bf67003b NC |
1647 | if (sections_being_created == NULL) |
1648 | { | |
1649 | /* FIXME: It would be more efficient to attach this array to the bfd somehow. */ | |
1650 | sections_being_created = (bfd_boolean *) | |
1651 | bfd_zalloc (abfd, elf_numsections (abfd) * sizeof (bfd_boolean)); | |
5a4b0ccc | 1652 | sections_being_created_abfd = abfd; |
bf67003b NC |
1653 | } |
1654 | if (sections_being_created [shindex]) | |
1655 | { | |
1656 | (*_bfd_error_handler) | |
1657 | (_("%B: warning: loop in section dependencies detected"), abfd); | |
1658 | return FALSE; | |
1659 | } | |
1660 | sections_being_created [shindex] = TRUE; | |
1661 | } | |
1662 | ||
4fbb74a6 AM |
1663 | hdr = elf_elfsections (abfd)[shindex]; |
1664 | ehdr = elf_elfheader (abfd); | |
1665 | name = bfd_elf_string_from_elf_section (abfd, ehdr->e_shstrndx, | |
1b3a8575 | 1666 | hdr->sh_name); |
933d961a | 1667 | if (name == NULL) |
bf67003b | 1668 | goto fail; |
252b5132 | 1669 | |
4fbb74a6 | 1670 | bed = get_elf_backend_data (abfd); |
252b5132 RH |
1671 | switch (hdr->sh_type) |
1672 | { | |
1673 | case SHT_NULL: | |
1674 | /* Inactive section. Throw it away. */ | |
bf67003b | 1675 | goto success; |
252b5132 | 1676 | |
bf67003b NC |
1677 | case SHT_PROGBITS: /* Normal section with contents. */ |
1678 | case SHT_NOBITS: /* .bss section. */ | |
1679 | case SHT_HASH: /* .hash section. */ | |
1680 | case SHT_NOTE: /* .note section. */ | |
25e27870 L |
1681 | case SHT_INIT_ARRAY: /* .init_array section. */ |
1682 | case SHT_FINI_ARRAY: /* .fini_array section. */ | |
1683 | case SHT_PREINIT_ARRAY: /* .preinit_array section. */ | |
7f1204bb | 1684 | case SHT_GNU_LIBLIST: /* .gnu.liblist section. */ |
fdc90cb4 | 1685 | case SHT_GNU_HASH: /* .gnu.hash section. */ |
bf67003b NC |
1686 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); |
1687 | goto success; | |
252b5132 | 1688 | |
797fc050 | 1689 | case SHT_DYNAMIC: /* Dynamic linking information. */ |
6dc132d9 | 1690 | if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex)) |
bf67003b NC |
1691 | goto fail; |
1692 | ||
cfcac11d NC |
1693 | if (hdr->sh_link > elf_numsections (abfd)) |
1694 | { | |
caa83f8b | 1695 | /* PR 10478: Accept Solaris binaries with a sh_link |
cfcac11d NC |
1696 | field set to SHN_BEFORE or SHN_AFTER. */ |
1697 | switch (bfd_get_arch (abfd)) | |
1698 | { | |
caa83f8b | 1699 | case bfd_arch_i386: |
cfcac11d NC |
1700 | case bfd_arch_sparc: |
1701 | if (hdr->sh_link == (SHN_LORESERVE & 0xffff) /* SHN_BEFORE */ | |
1702 | || hdr->sh_link == ((SHN_LORESERVE + 1) & 0xffff) /* SHN_AFTER */) | |
1703 | break; | |
1704 | /* Otherwise fall through. */ | |
1705 | default: | |
bf67003b | 1706 | goto fail; |
cfcac11d NC |
1707 | } |
1708 | } | |
1709 | else if (elf_elfsections (abfd)[hdr->sh_link] == NULL) | |
bf67003b | 1710 | goto fail; |
cfcac11d | 1711 | else if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_STRTAB) |
797fc050 AM |
1712 | { |
1713 | Elf_Internal_Shdr *dynsymhdr; | |
1714 | ||
1715 | /* The shared libraries distributed with hpux11 have a bogus | |
1716 | sh_link field for the ".dynamic" section. Find the | |
1717 | string table for the ".dynsym" section instead. */ | |
1718 | if (elf_dynsymtab (abfd) != 0) | |
1719 | { | |
1720 | dynsymhdr = elf_elfsections (abfd)[elf_dynsymtab (abfd)]; | |
1721 | hdr->sh_link = dynsymhdr->sh_link; | |
1722 | } | |
1723 | else | |
1724 | { | |
1725 | unsigned int i, num_sec; | |
1726 | ||
1727 | num_sec = elf_numsections (abfd); | |
1728 | for (i = 1; i < num_sec; i++) | |
1729 | { | |
1730 | dynsymhdr = elf_elfsections (abfd)[i]; | |
1731 | if (dynsymhdr->sh_type == SHT_DYNSYM) | |
1732 | { | |
1733 | hdr->sh_link = dynsymhdr->sh_link; | |
1734 | break; | |
1735 | } | |
1736 | } | |
1737 | } | |
1738 | } | |
bf67003b | 1739 | goto success; |
797fc050 | 1740 | |
bf67003b | 1741 | case SHT_SYMTAB: /* A symbol table. */ |
252b5132 | 1742 | if (elf_onesymtab (abfd) == shindex) |
bf67003b | 1743 | goto success; |
252b5132 | 1744 | |
a50b2160 | 1745 | if (hdr->sh_entsize != bed->s->sizeof_sym) |
bf67003b NC |
1746 | goto fail; |
1747 | ||
3337c1e5 | 1748 | if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size) |
eee3b786 AM |
1749 | { |
1750 | if (hdr->sh_size != 0) | |
bf67003b | 1751 | goto fail; |
eee3b786 AM |
1752 | /* Some assemblers erroneously set sh_info to one with a |
1753 | zero sh_size. ld sees this as a global symbol count | |
1754 | of (unsigned) -1. Fix it here. */ | |
1755 | hdr->sh_info = 0; | |
bf67003b | 1756 | goto success; |
eee3b786 | 1757 | } |
bf67003b | 1758 | |
252b5132 RH |
1759 | BFD_ASSERT (elf_onesymtab (abfd) == 0); |
1760 | elf_onesymtab (abfd) = shindex; | |
1761 | elf_tdata (abfd)->symtab_hdr = *hdr; | |
1762 | elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->symtab_hdr; | |
1763 | abfd->flags |= HAS_SYMS; | |
1764 | ||
1765 | /* Sometimes a shared object will map in the symbol table. If | |
08a40648 AM |
1766 | SHF_ALLOC is set, and this is a shared object, then we also |
1767 | treat this section as a BFD section. We can not base the | |
1768 | decision purely on SHF_ALLOC, because that flag is sometimes | |
1769 | set in a relocatable object file, which would confuse the | |
1770 | linker. */ | |
252b5132 RH |
1771 | if ((hdr->sh_flags & SHF_ALLOC) != 0 |
1772 | && (abfd->flags & DYNAMIC) != 0 | |
6dc132d9 L |
1773 | && ! _bfd_elf_make_section_from_shdr (abfd, hdr, name, |
1774 | shindex)) | |
bf67003b | 1775 | goto fail; |
252b5132 | 1776 | |
1b3a8575 AM |
1777 | /* Go looking for SHT_SYMTAB_SHNDX too, since if there is one we |
1778 | can't read symbols without that section loaded as well. It | |
1779 | is most likely specified by the next section header. */ | |
1780 | if (elf_elfsections (abfd)[elf_symtab_shndx (abfd)]->sh_link != shindex) | |
1781 | { | |
1782 | unsigned int i, num_sec; | |
1783 | ||
1784 | num_sec = elf_numsections (abfd); | |
1785 | for (i = shindex + 1; i < num_sec; i++) | |
1786 | { | |
1787 | Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i]; | |
1788 | if (hdr2->sh_type == SHT_SYMTAB_SHNDX | |
1789 | && hdr2->sh_link == shindex) | |
1790 | break; | |
1791 | } | |
1792 | if (i == num_sec) | |
1793 | for (i = 1; i < shindex; i++) | |
1794 | { | |
1795 | Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i]; | |
1796 | if (hdr2->sh_type == SHT_SYMTAB_SHNDX | |
1797 | && hdr2->sh_link == shindex) | |
1798 | break; | |
1799 | } | |
1800 | if (i != shindex) | |
bf67003b | 1801 | ret = bfd_section_from_shdr (abfd, i); |
1b3a8575 | 1802 | } |
bf67003b | 1803 | goto success; |
252b5132 | 1804 | |
bf67003b | 1805 | case SHT_DYNSYM: /* A dynamic symbol table. */ |
252b5132 | 1806 | if (elf_dynsymtab (abfd) == shindex) |
bf67003b | 1807 | goto success; |
252b5132 | 1808 | |
a50b2160 | 1809 | if (hdr->sh_entsize != bed->s->sizeof_sym) |
bf67003b NC |
1810 | goto fail; |
1811 | ||
eee3b786 AM |
1812 | if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size) |
1813 | { | |
1814 | if (hdr->sh_size != 0) | |
bf67003b NC |
1815 | goto fail; |
1816 | ||
eee3b786 AM |
1817 | /* Some linkers erroneously set sh_info to one with a |
1818 | zero sh_size. ld sees this as a global symbol count | |
1819 | of (unsigned) -1. Fix it here. */ | |
1820 | hdr->sh_info = 0; | |
bf67003b | 1821 | goto success; |
eee3b786 | 1822 | } |
bf67003b | 1823 | |
252b5132 RH |
1824 | BFD_ASSERT (elf_dynsymtab (abfd) == 0); |
1825 | elf_dynsymtab (abfd) = shindex; | |
1826 | elf_tdata (abfd)->dynsymtab_hdr = *hdr; | |
1827 | elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->dynsymtab_hdr; | |
1828 | abfd->flags |= HAS_SYMS; | |
1829 | ||
1830 | /* Besides being a symbol table, we also treat this as a regular | |
1831 | section, so that objcopy can handle it. */ | |
bf67003b NC |
1832 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); |
1833 | goto success; | |
252b5132 | 1834 | |
bf67003b | 1835 | case SHT_SYMTAB_SHNDX: /* Symbol section indices when >64k sections. */ |
9ad5cbcf | 1836 | if (elf_symtab_shndx (abfd) == shindex) |
bf67003b | 1837 | goto success; |
9ad5cbcf | 1838 | |
1b3a8575 | 1839 | BFD_ASSERT (elf_symtab_shndx (abfd) == 0); |
9ad5cbcf AM |
1840 | elf_symtab_shndx (abfd) = shindex; |
1841 | elf_tdata (abfd)->symtab_shndx_hdr = *hdr; | |
1842 | elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->symtab_shndx_hdr; | |
bf67003b | 1843 | goto success; |
9ad5cbcf | 1844 | |
bf67003b | 1845 | case SHT_STRTAB: /* A string table. */ |
252b5132 | 1846 | if (hdr->bfd_section != NULL) |
bf67003b NC |
1847 | goto success; |
1848 | ||
252b5132 RH |
1849 | if (ehdr->e_shstrndx == shindex) |
1850 | { | |
1851 | elf_tdata (abfd)->shstrtab_hdr = *hdr; | |
1852 | elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->shstrtab_hdr; | |
bf67003b | 1853 | goto success; |
252b5132 | 1854 | } |
bf67003b | 1855 | |
1b3a8575 AM |
1856 | if (elf_elfsections (abfd)[elf_onesymtab (abfd)]->sh_link == shindex) |
1857 | { | |
1858 | symtab_strtab: | |
1859 | elf_tdata (abfd)->strtab_hdr = *hdr; | |
1860 | elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->strtab_hdr; | |
bf67003b | 1861 | goto success; |
1b3a8575 | 1862 | } |
bf67003b | 1863 | |
1b3a8575 AM |
1864 | if (elf_elfsections (abfd)[elf_dynsymtab (abfd)]->sh_link == shindex) |
1865 | { | |
1866 | dynsymtab_strtab: | |
1867 | elf_tdata (abfd)->dynstrtab_hdr = *hdr; | |
1868 | hdr = &elf_tdata (abfd)->dynstrtab_hdr; | |
1869 | elf_elfsections (abfd)[shindex] = hdr; | |
1870 | /* We also treat this as a regular section, so that objcopy | |
1871 | can handle it. */ | |
bf67003b NC |
1872 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, |
1873 | shindex); | |
1874 | goto success; | |
1b3a8575 | 1875 | } |
252b5132 | 1876 | |
1b3a8575 AM |
1877 | /* If the string table isn't one of the above, then treat it as a |
1878 | regular section. We need to scan all the headers to be sure, | |
1879 | just in case this strtab section appeared before the above. */ | |
1880 | if (elf_onesymtab (abfd) == 0 || elf_dynsymtab (abfd) == 0) | |
1881 | { | |
1882 | unsigned int i, num_sec; | |
252b5132 | 1883 | |
1b3a8575 AM |
1884 | num_sec = elf_numsections (abfd); |
1885 | for (i = 1; i < num_sec; i++) | |
1886 | { | |
1887 | Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i]; | |
1888 | if (hdr2->sh_link == shindex) | |
1889 | { | |
933d961a JJ |
1890 | /* Prevent endless recursion on broken objects. */ |
1891 | if (i == shindex) | |
bf67003b | 1892 | goto fail; |
1b3a8575 | 1893 | if (! bfd_section_from_shdr (abfd, i)) |
bf67003b | 1894 | goto fail; |
1b3a8575 AM |
1895 | if (elf_onesymtab (abfd) == i) |
1896 | goto symtab_strtab; | |
1897 | if (elf_dynsymtab (abfd) == i) | |
1898 | goto dynsymtab_strtab; | |
1899 | } | |
1900 | } | |
1901 | } | |
bf67003b NC |
1902 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); |
1903 | goto success; | |
252b5132 RH |
1904 | |
1905 | case SHT_REL: | |
1906 | case SHT_RELA: | |
1907 | /* *These* do a lot of work -- but build no sections! */ | |
1908 | { | |
1909 | asection *target_sect; | |
d4730f92 | 1910 | Elf_Internal_Shdr *hdr2, **p_hdr; |
9ad5cbcf | 1911 | unsigned int num_sec = elf_numsections (abfd); |
d4730f92 BS |
1912 | struct bfd_elf_section_data *esdt; |
1913 | bfd_size_type amt; | |
252b5132 | 1914 | |
aa2ca951 JJ |
1915 | if (hdr->sh_entsize |
1916 | != (bfd_size_type) (hdr->sh_type == SHT_REL | |
a50b2160 | 1917 | ? bed->s->sizeof_rel : bed->s->sizeof_rela)) |
bf67003b | 1918 | goto fail; |
a50b2160 | 1919 | |
03ae5f59 | 1920 | /* Check for a bogus link to avoid crashing. */ |
4fbb74a6 | 1921 | if (hdr->sh_link >= num_sec) |
03ae5f59 ILT |
1922 | { |
1923 | ((*_bfd_error_handler) | |
d003868e AM |
1924 | (_("%B: invalid link %lu for reloc section %s (index %u)"), |
1925 | abfd, hdr->sh_link, name, shindex)); | |
bf67003b NC |
1926 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, |
1927 | shindex); | |
1928 | goto success; | |
03ae5f59 ILT |
1929 | } |
1930 | ||
252b5132 RH |
1931 | /* For some incomprehensible reason Oracle distributes |
1932 | libraries for Solaris in which some of the objects have | |
1933 | bogus sh_link fields. It would be nice if we could just | |
1934 | reject them, but, unfortunately, some people need to use | |
1935 | them. We scan through the section headers; if we find only | |
1936 | one suitable symbol table, we clobber the sh_link to point | |
83b89087 L |
1937 | to it. I hope this doesn't break anything. |
1938 | ||
1939 | Don't do it on executable nor shared library. */ | |
1940 | if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0 | |
1941 | && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_SYMTAB | |
252b5132 RH |
1942 | && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_DYNSYM) |
1943 | { | |
9ad5cbcf | 1944 | unsigned int scan; |
252b5132 RH |
1945 | int found; |
1946 | ||
1947 | found = 0; | |
9ad5cbcf | 1948 | for (scan = 1; scan < num_sec; scan++) |
252b5132 RH |
1949 | { |
1950 | if (elf_elfsections (abfd)[scan]->sh_type == SHT_SYMTAB | |
1951 | || elf_elfsections (abfd)[scan]->sh_type == SHT_DYNSYM) | |
1952 | { | |
1953 | if (found != 0) | |
1954 | { | |
1955 | found = 0; | |
1956 | break; | |
1957 | } | |
1958 | found = scan; | |
1959 | } | |
1960 | } | |
1961 | if (found != 0) | |
1962 | hdr->sh_link = found; | |
1963 | } | |
1964 | ||
1965 | /* Get the symbol table. */ | |
1b3a8575 AM |
1966 | if ((elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_SYMTAB |
1967 | || elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_DYNSYM) | |
252b5132 | 1968 | && ! bfd_section_from_shdr (abfd, hdr->sh_link)) |
bf67003b | 1969 | goto fail; |
252b5132 RH |
1970 | |
1971 | /* If this reloc section does not use the main symbol table we | |
1972 | don't treat it as a reloc section. BFD can't adequately | |
1973 | represent such a section, so at least for now, we don't | |
c044fabd | 1974 | try. We just present it as a normal section. We also |
60bcf0fa | 1975 | can't use it as a reloc section if it points to the null |
83b89087 L |
1976 | section, an invalid section, another reloc section, or its |
1977 | sh_link points to the null section. */ | |
185ef66d | 1978 | if (hdr->sh_link != elf_onesymtab (abfd) |
83b89087 | 1979 | || hdr->sh_link == SHN_UNDEF |
185ef66d | 1980 | || hdr->sh_info == SHN_UNDEF |
185ef66d AM |
1981 | || hdr->sh_info >= num_sec |
1982 | || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_REL | |
1983 | || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_RELA) | |
bf67003b NC |
1984 | { |
1985 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, | |
1986 | shindex); | |
1987 | goto success; | |
1988 | } | |
252b5132 RH |
1989 | |
1990 | if (! bfd_section_from_shdr (abfd, hdr->sh_info)) | |
bf67003b NC |
1991 | goto fail; |
1992 | ||
252b5132 RH |
1993 | target_sect = bfd_section_from_elf_index (abfd, hdr->sh_info); |
1994 | if (target_sect == NULL) | |
bf67003b | 1995 | goto fail; |
252b5132 | 1996 | |
d4730f92 BS |
1997 | esdt = elf_section_data (target_sect); |
1998 | if (hdr->sh_type == SHT_RELA) | |
1999 | p_hdr = &esdt->rela.hdr; | |
252b5132 | 2000 | else |
d4730f92 BS |
2001 | p_hdr = &esdt->rel.hdr; |
2002 | ||
06614111 NC |
2003 | /* PR 17512: file: 0b4f81b7. */ |
2004 | if (*p_hdr != NULL) | |
2005 | goto fail; | |
d4730f92 BS |
2006 | amt = sizeof (*hdr2); |
2007 | hdr2 = (Elf_Internal_Shdr *) bfd_alloc (abfd, amt); | |
2008 | if (hdr2 == NULL) | |
bf67003b | 2009 | goto fail; |
252b5132 | 2010 | *hdr2 = *hdr; |
d4730f92 | 2011 | *p_hdr = hdr2; |
252b5132 | 2012 | elf_elfsections (abfd)[shindex] = hdr2; |
d9bc7a44 | 2013 | target_sect->reloc_count += NUM_SHDR_ENTRIES (hdr); |
252b5132 RH |
2014 | target_sect->flags |= SEC_RELOC; |
2015 | target_sect->relocation = NULL; | |
2016 | target_sect->rel_filepos = hdr->sh_offset; | |
bf572ba0 MM |
2017 | /* In the section to which the relocations apply, mark whether |
2018 | its relocations are of the REL or RELA variety. */ | |
72730e0c | 2019 | if (hdr->sh_size != 0) |
d4730f92 BS |
2020 | { |
2021 | if (hdr->sh_type == SHT_RELA) | |
2022 | target_sect->use_rela_p = 1; | |
2023 | } | |
252b5132 | 2024 | abfd->flags |= HAS_RELOC; |
bf67003b | 2025 | goto success; |
252b5132 | 2026 | } |
252b5132 RH |
2027 | |
2028 | case SHT_GNU_verdef: | |
2029 | elf_dynverdef (abfd) = shindex; | |
2030 | elf_tdata (abfd)->dynverdef_hdr = *hdr; | |
bf67003b NC |
2031 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); |
2032 | goto success; | |
252b5132 RH |
2033 | |
2034 | case SHT_GNU_versym: | |
a50b2160 | 2035 | if (hdr->sh_entsize != sizeof (Elf_External_Versym)) |
bf67003b NC |
2036 | goto fail; |
2037 | ||
252b5132 RH |
2038 | elf_dynversym (abfd) = shindex; |
2039 | elf_tdata (abfd)->dynversym_hdr = *hdr; | |
bf67003b NC |
2040 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); |
2041 | goto success; | |
252b5132 RH |
2042 | |
2043 | case SHT_GNU_verneed: | |
2044 | elf_dynverref (abfd) = shindex; | |
2045 | elf_tdata (abfd)->dynverref_hdr = *hdr; | |
bf67003b NC |
2046 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); |
2047 | goto success; | |
252b5132 RH |
2048 | |
2049 | case SHT_SHLIB: | |
bf67003b | 2050 | goto success; |
252b5132 | 2051 | |
dbb410c3 | 2052 | case SHT_GROUP: |
44534af3 | 2053 | if (! IS_VALID_GROUP_SECTION_HEADER (hdr, GRP_ENTRY_SIZE)) |
bf67003b NC |
2054 | goto fail; |
2055 | ||
6dc132d9 | 2056 | if (!_bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex)) |
bf67003b NC |
2057 | goto fail; |
2058 | ||
dbb410c3 AM |
2059 | if (hdr->contents != NULL) |
2060 | { | |
2061 | Elf_Internal_Group *idx = (Elf_Internal_Group *) hdr->contents; | |
06614111 | 2062 | unsigned int n_elt = hdr->sh_size / sizeof (* idx); |
dbb410c3 AM |
2063 | asection *s; |
2064 | ||
06614111 NC |
2065 | if (n_elt == 0) |
2066 | goto fail; | |
b885599b AM |
2067 | if (idx->flags & GRP_COMDAT) |
2068 | hdr->bfd_section->flags | |
2069 | |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD; | |
2070 | ||
45c5e9ed L |
2071 | /* We try to keep the same section order as it comes in. */ |
2072 | idx += n_elt; | |
06614111 | 2073 | |
dbb410c3 | 2074 | while (--n_elt != 0) |
1783205a NC |
2075 | { |
2076 | --idx; | |
2077 | ||
2078 | if (idx->shdr != NULL | |
2079 | && (s = idx->shdr->bfd_section) != NULL | |
2080 | && elf_next_in_group (s) != NULL) | |
2081 | { | |
2082 | elf_next_in_group (hdr->bfd_section) = s; | |
2083 | break; | |
2084 | } | |
2085 | } | |
dbb410c3 | 2086 | } |
bf67003b | 2087 | goto success; |
dbb410c3 | 2088 | |
252b5132 | 2089 | default: |
104d59d1 JM |
2090 | /* Possibly an attributes section. */ |
2091 | if (hdr->sh_type == SHT_GNU_ATTRIBUTES | |
2092 | || hdr->sh_type == bed->obj_attrs_section_type) | |
2093 | { | |
2094 | if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex)) | |
bf67003b | 2095 | goto fail; |
104d59d1 | 2096 | _bfd_elf_parse_attributes (abfd, hdr); |
bf67003b | 2097 | goto success; |
104d59d1 JM |
2098 | } |
2099 | ||
252b5132 | 2100 | /* Check for any processor-specific section types. */ |
3eb70a79 | 2101 | if (bed->elf_backend_section_from_shdr (abfd, hdr, name, shindex)) |
bf67003b | 2102 | goto success; |
3eb70a79 L |
2103 | |
2104 | if (hdr->sh_type >= SHT_LOUSER && hdr->sh_type <= SHT_HIUSER) | |
2105 | { | |
2106 | if ((hdr->sh_flags & SHF_ALLOC) != 0) | |
2107 | /* FIXME: How to properly handle allocated section reserved | |
2108 | for applications? */ | |
2109 | (*_bfd_error_handler) | |
2110 | (_("%B: don't know how to handle allocated, application " | |
2111 | "specific section `%s' [0x%8x]"), | |
2112 | abfd, name, hdr->sh_type); | |
2113 | else | |
bf67003b NC |
2114 | { |
2115 | /* Allow sections reserved for applications. */ | |
2116 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, | |
2117 | shindex); | |
2118 | goto success; | |
2119 | } | |
3eb70a79 L |
2120 | } |
2121 | else if (hdr->sh_type >= SHT_LOPROC | |
2122 | && hdr->sh_type <= SHT_HIPROC) | |
2123 | /* FIXME: We should handle this section. */ | |
2124 | (*_bfd_error_handler) | |
2125 | (_("%B: don't know how to handle processor specific section " | |
2126 | "`%s' [0x%8x]"), | |
2127 | abfd, name, hdr->sh_type); | |
2128 | else if (hdr->sh_type >= SHT_LOOS && hdr->sh_type <= SHT_HIOS) | |
ff15b240 NC |
2129 | { |
2130 | /* Unrecognised OS-specific sections. */ | |
2131 | if ((hdr->sh_flags & SHF_OS_NONCONFORMING) != 0) | |
2132 | /* SHF_OS_NONCONFORMING indicates that special knowledge is | |
08a40648 | 2133 | required to correctly process the section and the file should |
ff15b240 NC |
2134 | be rejected with an error message. */ |
2135 | (*_bfd_error_handler) | |
2136 | (_("%B: don't know how to handle OS specific section " | |
2137 | "`%s' [0x%8x]"), | |
2138 | abfd, name, hdr->sh_type); | |
2139 | else | |
bf67003b NC |
2140 | { |
2141 | /* Otherwise it should be processed. */ | |
2142 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); | |
2143 | goto success; | |
2144 | } | |
ff15b240 | 2145 | } |
3eb70a79 L |
2146 | else |
2147 | /* FIXME: We should handle this section. */ | |
2148 | (*_bfd_error_handler) | |
2149 | (_("%B: don't know how to handle section `%s' [0x%8x]"), | |
2150 | abfd, name, hdr->sh_type); | |
2151 | ||
bf67003b | 2152 | goto fail; |
252b5132 RH |
2153 | } |
2154 | ||
bf67003b NC |
2155 | fail: |
2156 | ret = FALSE; | |
2157 | success: | |
e5b470e2 | 2158 | if (sections_being_created && sections_being_created_abfd == abfd) |
bf67003b NC |
2159 | sections_being_created [shindex] = FALSE; |
2160 | if (-- nesting == 0) | |
5a4b0ccc NC |
2161 | { |
2162 | sections_being_created = NULL; | |
2163 | sections_being_created_abfd = abfd; | |
2164 | } | |
bf67003b | 2165 | return ret; |
252b5132 RH |
2166 | } |
2167 | ||
87d72d41 | 2168 | /* Return the local symbol specified by ABFD, R_SYMNDX. */ |
ec338859 | 2169 | |
87d72d41 AM |
2170 | Elf_Internal_Sym * |
2171 | bfd_sym_from_r_symndx (struct sym_cache *cache, | |
2172 | bfd *abfd, | |
2173 | unsigned long r_symndx) | |
ec338859 | 2174 | { |
ec338859 AM |
2175 | unsigned int ent = r_symndx % LOCAL_SYM_CACHE_SIZE; |
2176 | ||
a5d1b3b5 AM |
2177 | if (cache->abfd != abfd || cache->indx[ent] != r_symndx) |
2178 | { | |
2179 | Elf_Internal_Shdr *symtab_hdr; | |
2180 | unsigned char esym[sizeof (Elf64_External_Sym)]; | |
2181 | Elf_External_Sym_Shndx eshndx; | |
ec338859 | 2182 | |
a5d1b3b5 AM |
2183 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; |
2184 | if (bfd_elf_get_elf_syms (abfd, symtab_hdr, 1, r_symndx, | |
87d72d41 | 2185 | &cache->sym[ent], esym, &eshndx) == NULL) |
a5d1b3b5 | 2186 | return NULL; |
9ad5cbcf | 2187 | |
a5d1b3b5 AM |
2188 | if (cache->abfd != abfd) |
2189 | { | |
2190 | memset (cache->indx, -1, sizeof (cache->indx)); | |
2191 | cache->abfd = abfd; | |
2192 | } | |
2193 | cache->indx[ent] = r_symndx; | |
ec338859 | 2194 | } |
a5d1b3b5 | 2195 | |
87d72d41 | 2196 | return &cache->sym[ent]; |
ec338859 AM |
2197 | } |
2198 | ||
252b5132 RH |
2199 | /* Given an ELF section number, retrieve the corresponding BFD |
2200 | section. */ | |
2201 | ||
2202 | asection * | |
91d6fa6a | 2203 | bfd_section_from_elf_index (bfd *abfd, unsigned int sec_index) |
252b5132 | 2204 | { |
91d6fa6a | 2205 | if (sec_index >= elf_numsections (abfd)) |
252b5132 | 2206 | return NULL; |
91d6fa6a | 2207 | return elf_elfsections (abfd)[sec_index]->bfd_section; |
252b5132 RH |
2208 | } |
2209 | ||
b35d266b | 2210 | static const struct bfd_elf_special_section special_sections_b[] = |
2f89ff8d | 2211 | { |
0112cd26 NC |
2212 | { STRING_COMMA_LEN (".bss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE }, |
2213 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2214 | }; |
2215 | ||
b35d266b | 2216 | static const struct bfd_elf_special_section special_sections_c[] = |
7f4d3958 | 2217 | { |
0112cd26 NC |
2218 | { STRING_COMMA_LEN (".comment"), 0, SHT_PROGBITS, 0 }, |
2219 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2220 | }; |
2221 | ||
b35d266b | 2222 | static const struct bfd_elf_special_section special_sections_d[] = |
7f4d3958 | 2223 | { |
0112cd26 NC |
2224 | { STRING_COMMA_LEN (".data"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE }, |
2225 | { STRING_COMMA_LEN (".data1"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE }, | |
a9a72a65 DE |
2226 | /* There are more DWARF sections than these, but they needn't be added here |
2227 | unless you have to cope with broken compilers that don't emit section | |
2228 | attributes or you want to help the user writing assembler. */ | |
0112cd26 NC |
2229 | { STRING_COMMA_LEN (".debug"), 0, SHT_PROGBITS, 0 }, |
2230 | { STRING_COMMA_LEN (".debug_line"), 0, SHT_PROGBITS, 0 }, | |
2231 | { STRING_COMMA_LEN (".debug_info"), 0, SHT_PROGBITS, 0 }, | |
2232 | { STRING_COMMA_LEN (".debug_abbrev"), 0, SHT_PROGBITS, 0 }, | |
2233 | { STRING_COMMA_LEN (".debug_aranges"), 0, SHT_PROGBITS, 0 }, | |
2234 | { STRING_COMMA_LEN (".dynamic"), 0, SHT_DYNAMIC, SHF_ALLOC }, | |
2235 | { STRING_COMMA_LEN (".dynstr"), 0, SHT_STRTAB, SHF_ALLOC }, | |
2236 | { STRING_COMMA_LEN (".dynsym"), 0, SHT_DYNSYM, SHF_ALLOC }, | |
2237 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2238 | }; |
2239 | ||
b35d266b | 2240 | static const struct bfd_elf_special_section special_sections_f[] = |
7f4d3958 | 2241 | { |
0112cd26 NC |
2242 | { STRING_COMMA_LEN (".fini"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR }, |
2243 | { STRING_COMMA_LEN (".fini_array"), 0, SHT_FINI_ARRAY, SHF_ALLOC + SHF_WRITE }, | |
2244 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2245 | }; |
2246 | ||
b35d266b | 2247 | static const struct bfd_elf_special_section special_sections_g[] = |
7f4d3958 | 2248 | { |
0112cd26 | 2249 | { STRING_COMMA_LEN (".gnu.linkonce.b"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE }, |
68efed41 | 2250 | { STRING_COMMA_LEN (".gnu.lto_"), -1, SHT_PROGBITS, SHF_EXCLUDE }, |
0112cd26 NC |
2251 | { STRING_COMMA_LEN (".got"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE }, |
2252 | { STRING_COMMA_LEN (".gnu.version"), 0, SHT_GNU_versym, 0 }, | |
2253 | { STRING_COMMA_LEN (".gnu.version_d"), 0, SHT_GNU_verdef, 0 }, | |
2254 | { STRING_COMMA_LEN (".gnu.version_r"), 0, SHT_GNU_verneed, 0 }, | |
2255 | { STRING_COMMA_LEN (".gnu.liblist"), 0, SHT_GNU_LIBLIST, SHF_ALLOC }, | |
2256 | { STRING_COMMA_LEN (".gnu.conflict"), 0, SHT_RELA, SHF_ALLOC }, | |
2257 | { STRING_COMMA_LEN (".gnu.hash"), 0, SHT_GNU_HASH, SHF_ALLOC }, | |
2258 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2259 | }; |
2260 | ||
b35d266b | 2261 | static const struct bfd_elf_special_section special_sections_h[] = |
7f4d3958 | 2262 | { |
0112cd26 NC |
2263 | { STRING_COMMA_LEN (".hash"), 0, SHT_HASH, SHF_ALLOC }, |
2264 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2265 | }; |
2266 | ||
b35d266b | 2267 | static const struct bfd_elf_special_section special_sections_i[] = |
7f4d3958 | 2268 | { |
0112cd26 NC |
2269 | { STRING_COMMA_LEN (".init"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR }, |
2270 | { STRING_COMMA_LEN (".init_array"), 0, SHT_INIT_ARRAY, SHF_ALLOC + SHF_WRITE }, | |
2271 | { STRING_COMMA_LEN (".interp"), 0, SHT_PROGBITS, 0 }, | |
2272 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2273 | }; |
2274 | ||
b35d266b | 2275 | static const struct bfd_elf_special_section special_sections_l[] = |
7f4d3958 | 2276 | { |
0112cd26 NC |
2277 | { STRING_COMMA_LEN (".line"), 0, SHT_PROGBITS, 0 }, |
2278 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2279 | }; |
2280 | ||
b35d266b | 2281 | static const struct bfd_elf_special_section special_sections_n[] = |
7f4d3958 | 2282 | { |
0112cd26 NC |
2283 | { STRING_COMMA_LEN (".note.GNU-stack"), 0, SHT_PROGBITS, 0 }, |
2284 | { STRING_COMMA_LEN (".note"), -1, SHT_NOTE, 0 }, | |
2285 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2286 | }; |
2287 | ||
b35d266b | 2288 | static const struct bfd_elf_special_section special_sections_p[] = |
7f4d3958 | 2289 | { |
0112cd26 NC |
2290 | { STRING_COMMA_LEN (".preinit_array"), 0, SHT_PREINIT_ARRAY, SHF_ALLOC + SHF_WRITE }, |
2291 | { STRING_COMMA_LEN (".plt"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR }, | |
2292 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2293 | }; |
2294 | ||
b35d266b | 2295 | static const struct bfd_elf_special_section special_sections_r[] = |
7f4d3958 | 2296 | { |
0112cd26 NC |
2297 | { STRING_COMMA_LEN (".rodata"), -2, SHT_PROGBITS, SHF_ALLOC }, |
2298 | { STRING_COMMA_LEN (".rodata1"), 0, SHT_PROGBITS, SHF_ALLOC }, | |
2299 | { STRING_COMMA_LEN (".rela"), -1, SHT_RELA, 0 }, | |
2300 | { STRING_COMMA_LEN (".rel"), -1, SHT_REL, 0 }, | |
2301 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2302 | }; |
2303 | ||
b35d266b | 2304 | static const struct bfd_elf_special_section special_sections_s[] = |
7f4d3958 | 2305 | { |
0112cd26 NC |
2306 | { STRING_COMMA_LEN (".shstrtab"), 0, SHT_STRTAB, 0 }, |
2307 | { STRING_COMMA_LEN (".strtab"), 0, SHT_STRTAB, 0 }, | |
2308 | { STRING_COMMA_LEN (".symtab"), 0, SHT_SYMTAB, 0 }, | |
60ff4dc4 HPN |
2309 | /* See struct bfd_elf_special_section declaration for the semantics of |
2310 | this special case where .prefix_length != strlen (.prefix). */ | |
2311 | { ".stabstr", 5, 3, SHT_STRTAB, 0 }, | |
0112cd26 | 2312 | { NULL, 0, 0, 0, 0 } |
2f89ff8d L |
2313 | }; |
2314 | ||
b35d266b | 2315 | static const struct bfd_elf_special_section special_sections_t[] = |
7f4d3958 | 2316 | { |
0112cd26 NC |
2317 | { STRING_COMMA_LEN (".text"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR }, |
2318 | { STRING_COMMA_LEN (".tbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS }, | |
2319 | { STRING_COMMA_LEN (".tdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS }, | |
2320 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2321 | }; |
2322 | ||
1b315056 CS |
2323 | static const struct bfd_elf_special_section special_sections_z[] = |
2324 | { | |
2325 | { STRING_COMMA_LEN (".zdebug_line"), 0, SHT_PROGBITS, 0 }, | |
2326 | { STRING_COMMA_LEN (".zdebug_info"), 0, SHT_PROGBITS, 0 }, | |
2327 | { STRING_COMMA_LEN (".zdebug_abbrev"), 0, SHT_PROGBITS, 0 }, | |
2328 | { STRING_COMMA_LEN (".zdebug_aranges"), 0, SHT_PROGBITS, 0 }, | |
2329 | { NULL, 0, 0, 0, 0 } | |
2330 | }; | |
2331 | ||
e4c93b56 | 2332 | static const struct bfd_elf_special_section * const special_sections[] = |
7f4d3958 | 2333 | { |
7f4d3958 | 2334 | special_sections_b, /* 'b' */ |
98ece1b3 | 2335 | special_sections_c, /* 'c' */ |
7f4d3958 L |
2336 | special_sections_d, /* 'd' */ |
2337 | NULL, /* 'e' */ | |
2338 | special_sections_f, /* 'f' */ | |
2339 | special_sections_g, /* 'g' */ | |
2340 | special_sections_h, /* 'h' */ | |
2341 | special_sections_i, /* 'i' */ | |
2342 | NULL, /* 'j' */ | |
2343 | NULL, /* 'k' */ | |
2344 | special_sections_l, /* 'l' */ | |
2345 | NULL, /* 'm' */ | |
2346 | special_sections_n, /* 'n' */ | |
2347 | NULL, /* 'o' */ | |
2348 | special_sections_p, /* 'p' */ | |
2349 | NULL, /* 'q' */ | |
2350 | special_sections_r, /* 'r' */ | |
2351 | special_sections_s, /* 's' */ | |
2352 | special_sections_t, /* 't' */ | |
1b315056 CS |
2353 | NULL, /* 'u' */ |
2354 | NULL, /* 'v' */ | |
2355 | NULL, /* 'w' */ | |
2356 | NULL, /* 'x' */ | |
2357 | NULL, /* 'y' */ | |
2358 | special_sections_z /* 'z' */ | |
7f4d3958 L |
2359 | }; |
2360 | ||
551b43fd AM |
2361 | const struct bfd_elf_special_section * |
2362 | _bfd_elf_get_special_section (const char *name, | |
2363 | const struct bfd_elf_special_section *spec, | |
2364 | unsigned int rela) | |
2f89ff8d L |
2365 | { |
2366 | int i; | |
7f4d3958 | 2367 | int len; |
7f4d3958 | 2368 | |
551b43fd | 2369 | len = strlen (name); |
7f4d3958 | 2370 | |
551b43fd | 2371 | for (i = 0; spec[i].prefix != NULL; i++) |
7dcb9820 AM |
2372 | { |
2373 | int suffix_len; | |
551b43fd | 2374 | int prefix_len = spec[i].prefix_length; |
7dcb9820 AM |
2375 | |
2376 | if (len < prefix_len) | |
2377 | continue; | |
551b43fd | 2378 | if (memcmp (name, spec[i].prefix, prefix_len) != 0) |
7dcb9820 AM |
2379 | continue; |
2380 | ||
551b43fd | 2381 | suffix_len = spec[i].suffix_length; |
7dcb9820 AM |
2382 | if (suffix_len <= 0) |
2383 | { | |
2384 | if (name[prefix_len] != 0) | |
2385 | { | |
2386 | if (suffix_len == 0) | |
2387 | continue; | |
2388 | if (name[prefix_len] != '.' | |
2389 | && (suffix_len == -2 | |
551b43fd | 2390 | || (rela && spec[i].type == SHT_REL))) |
7dcb9820 AM |
2391 | continue; |
2392 | } | |
2393 | } | |
2394 | else | |
2395 | { | |
2396 | if (len < prefix_len + suffix_len) | |
2397 | continue; | |
2398 | if (memcmp (name + len - suffix_len, | |
551b43fd | 2399 | spec[i].prefix + prefix_len, |
7dcb9820 AM |
2400 | suffix_len) != 0) |
2401 | continue; | |
2402 | } | |
551b43fd | 2403 | return &spec[i]; |
7dcb9820 | 2404 | } |
2f89ff8d L |
2405 | |
2406 | return NULL; | |
2407 | } | |
2408 | ||
7dcb9820 | 2409 | const struct bfd_elf_special_section * |
29ef7005 | 2410 | _bfd_elf_get_sec_type_attr (bfd *abfd, asection *sec) |
2f89ff8d | 2411 | { |
551b43fd AM |
2412 | int i; |
2413 | const struct bfd_elf_special_section *spec; | |
29ef7005 | 2414 | const struct elf_backend_data *bed; |
2f89ff8d L |
2415 | |
2416 | /* See if this is one of the special sections. */ | |
551b43fd AM |
2417 | if (sec->name == NULL) |
2418 | return NULL; | |
2f89ff8d | 2419 | |
29ef7005 L |
2420 | bed = get_elf_backend_data (abfd); |
2421 | spec = bed->special_sections; | |
2422 | if (spec) | |
2423 | { | |
2424 | spec = _bfd_elf_get_special_section (sec->name, | |
2425 | bed->special_sections, | |
2426 | sec->use_rela_p); | |
2427 | if (spec != NULL) | |
2428 | return spec; | |
2429 | } | |
2430 | ||
551b43fd AM |
2431 | if (sec->name[0] != '.') |
2432 | return NULL; | |
2f89ff8d | 2433 | |
551b43fd | 2434 | i = sec->name[1] - 'b'; |
1b315056 | 2435 | if (i < 0 || i > 'z' - 'b') |
551b43fd AM |
2436 | return NULL; |
2437 | ||
2438 | spec = special_sections[i]; | |
2f89ff8d | 2439 | |
551b43fd AM |
2440 | if (spec == NULL) |
2441 | return NULL; | |
2442 | ||
2443 | return _bfd_elf_get_special_section (sec->name, spec, sec->use_rela_p); | |
2f89ff8d L |
2444 | } |
2445 | ||
b34976b6 | 2446 | bfd_boolean |
217aa764 | 2447 | _bfd_elf_new_section_hook (bfd *abfd, asection *sec) |
252b5132 RH |
2448 | { |
2449 | struct bfd_elf_section_data *sdata; | |
551b43fd | 2450 | const struct elf_backend_data *bed; |
7dcb9820 | 2451 | const struct bfd_elf_special_section *ssect; |
252b5132 | 2452 | |
f0abc2a1 AM |
2453 | sdata = (struct bfd_elf_section_data *) sec->used_by_bfd; |
2454 | if (sdata == NULL) | |
2455 | { | |
a50b1753 NC |
2456 | sdata = (struct bfd_elf_section_data *) bfd_zalloc (abfd, |
2457 | sizeof (*sdata)); | |
f0abc2a1 AM |
2458 | if (sdata == NULL) |
2459 | return FALSE; | |
217aa764 | 2460 | sec->used_by_bfd = sdata; |
f0abc2a1 | 2461 | } |
bf572ba0 | 2462 | |
551b43fd AM |
2463 | /* Indicate whether or not this section should use RELA relocations. */ |
2464 | bed = get_elf_backend_data (abfd); | |
2465 | sec->use_rela_p = bed->default_use_rela_p; | |
2466 | ||
e843e0f8 L |
2467 | /* When we read a file, we don't need to set ELF section type and |
2468 | flags. They will be overridden in _bfd_elf_make_section_from_shdr | |
2469 | anyway. We will set ELF section type and flags for all linker | |
2470 | created sections. If user specifies BFD section flags, we will | |
2471 | set ELF section type and flags based on BFD section flags in | |
02ecc8e9 L |
2472 | elf_fake_sections. Special handling for .init_array/.fini_array |
2473 | output sections since they may contain .ctors/.dtors input | |
2474 | sections. We don't want _bfd_elf_init_private_section_data to | |
2475 | copy ELF section type from .ctors/.dtors input sections. */ | |
2476 | if (abfd->direction != read_direction | |
3496cb2a | 2477 | || (sec->flags & SEC_LINKER_CREATED) != 0) |
2f89ff8d | 2478 | { |
551b43fd | 2479 | ssect = (*bed->get_sec_type_attr) (abfd, sec); |
02ecc8e9 L |
2480 | if (ssect != NULL |
2481 | && (!sec->flags | |
2482 | || (sec->flags & SEC_LINKER_CREATED) != 0 | |
2483 | || ssect->type == SHT_INIT_ARRAY | |
2484 | || ssect->type == SHT_FINI_ARRAY)) | |
a31501e9 L |
2485 | { |
2486 | elf_section_type (sec) = ssect->type; | |
2487 | elf_section_flags (sec) = ssect->attr; | |
2488 | } | |
2f89ff8d L |
2489 | } |
2490 | ||
f592407e | 2491 | return _bfd_generic_new_section_hook (abfd, sec); |
252b5132 RH |
2492 | } |
2493 | ||
2494 | /* Create a new bfd section from an ELF program header. | |
2495 | ||
2496 | Since program segments have no names, we generate a synthetic name | |
2497 | of the form segment<NUM>, where NUM is generally the index in the | |
2498 | program header table. For segments that are split (see below) we | |
2499 | generate the names segment<NUM>a and segment<NUM>b. | |
2500 | ||
2501 | Note that some program segments may have a file size that is different than | |
2502 | (less than) the memory size. All this means is that at execution the | |
2503 | system must allocate the amount of memory specified by the memory size, | |
2504 | but only initialize it with the first "file size" bytes read from the | |
2505 | file. This would occur for example, with program segments consisting | |
2506 | of combined data+bss. | |
2507 | ||
2508 | To handle the above situation, this routine generates TWO bfd sections | |
2509 | for the single program segment. The first has the length specified by | |
2510 | the file size of the segment, and the second has the length specified | |
2511 | by the difference between the two sizes. In effect, the segment is split | |
d5191d0c | 2512 | into its initialized and uninitialized parts. |
252b5132 RH |
2513 | |
2514 | */ | |
2515 | ||
b34976b6 | 2516 | bfd_boolean |
217aa764 AM |
2517 | _bfd_elf_make_section_from_phdr (bfd *abfd, |
2518 | Elf_Internal_Phdr *hdr, | |
91d6fa6a | 2519 | int hdr_index, |
a50b1753 | 2520 | const char *type_name) |
252b5132 RH |
2521 | { |
2522 | asection *newsect; | |
2523 | char *name; | |
2524 | char namebuf[64]; | |
d4c88bbb | 2525 | size_t len; |
252b5132 RH |
2526 | int split; |
2527 | ||
2528 | split = ((hdr->p_memsz > 0) | |
2529 | && (hdr->p_filesz > 0) | |
2530 | && (hdr->p_memsz > hdr->p_filesz)); | |
d5191d0c AM |
2531 | |
2532 | if (hdr->p_filesz > 0) | |
252b5132 | 2533 | { |
91d6fa6a | 2534 | sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "a" : ""); |
d5191d0c | 2535 | len = strlen (namebuf) + 1; |
a50b1753 | 2536 | name = (char *) bfd_alloc (abfd, len); |
d5191d0c AM |
2537 | if (!name) |
2538 | return FALSE; | |
2539 | memcpy (name, namebuf, len); | |
2540 | newsect = bfd_make_section (abfd, name); | |
2541 | if (newsect == NULL) | |
2542 | return FALSE; | |
2543 | newsect->vma = hdr->p_vaddr; | |
2544 | newsect->lma = hdr->p_paddr; | |
2545 | newsect->size = hdr->p_filesz; | |
2546 | newsect->filepos = hdr->p_offset; | |
2547 | newsect->flags |= SEC_HAS_CONTENTS; | |
2548 | newsect->alignment_power = bfd_log2 (hdr->p_align); | |
2549 | if (hdr->p_type == PT_LOAD) | |
252b5132 | 2550 | { |
d5191d0c AM |
2551 | newsect->flags |= SEC_ALLOC; |
2552 | newsect->flags |= SEC_LOAD; | |
2553 | if (hdr->p_flags & PF_X) | |
2554 | { | |
2555 | /* FIXME: all we known is that it has execute PERMISSION, | |
2556 | may be data. */ | |
2557 | newsect->flags |= SEC_CODE; | |
2558 | } | |
2559 | } | |
2560 | if (!(hdr->p_flags & PF_W)) | |
2561 | { | |
2562 | newsect->flags |= SEC_READONLY; | |
252b5132 | 2563 | } |
252b5132 RH |
2564 | } |
2565 | ||
d5191d0c | 2566 | if (hdr->p_memsz > hdr->p_filesz) |
252b5132 | 2567 | { |
d5191d0c AM |
2568 | bfd_vma align; |
2569 | ||
91d6fa6a | 2570 | sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "b" : ""); |
d4c88bbb | 2571 | len = strlen (namebuf) + 1; |
a50b1753 | 2572 | name = (char *) bfd_alloc (abfd, len); |
252b5132 | 2573 | if (!name) |
b34976b6 | 2574 | return FALSE; |
d4c88bbb | 2575 | memcpy (name, namebuf, len); |
252b5132 RH |
2576 | newsect = bfd_make_section (abfd, name); |
2577 | if (newsect == NULL) | |
b34976b6 | 2578 | return FALSE; |
252b5132 RH |
2579 | newsect->vma = hdr->p_vaddr + hdr->p_filesz; |
2580 | newsect->lma = hdr->p_paddr + hdr->p_filesz; | |
eea6121a | 2581 | newsect->size = hdr->p_memsz - hdr->p_filesz; |
d5191d0c AM |
2582 | newsect->filepos = hdr->p_offset + hdr->p_filesz; |
2583 | align = newsect->vma & -newsect->vma; | |
2584 | if (align == 0 || align > hdr->p_align) | |
2585 | align = hdr->p_align; | |
2586 | newsect->alignment_power = bfd_log2 (align); | |
252b5132 RH |
2587 | if (hdr->p_type == PT_LOAD) |
2588 | { | |
d5191d0c AM |
2589 | /* Hack for gdb. Segments that have not been modified do |
2590 | not have their contents written to a core file, on the | |
2591 | assumption that a debugger can find the contents in the | |
2592 | executable. We flag this case by setting the fake | |
2593 | section size to zero. Note that "real" bss sections will | |
2594 | always have their contents dumped to the core file. */ | |
2595 | if (bfd_get_format (abfd) == bfd_core) | |
2596 | newsect->size = 0; | |
252b5132 RH |
2597 | newsect->flags |= SEC_ALLOC; |
2598 | if (hdr->p_flags & PF_X) | |
2599 | newsect->flags |= SEC_CODE; | |
2600 | } | |
2601 | if (!(hdr->p_flags & PF_W)) | |
2602 | newsect->flags |= SEC_READONLY; | |
2603 | } | |
2604 | ||
b34976b6 | 2605 | return TRUE; |
252b5132 RH |
2606 | } |
2607 | ||
b34976b6 | 2608 | bfd_boolean |
91d6fa6a | 2609 | bfd_section_from_phdr (bfd *abfd, Elf_Internal_Phdr *hdr, int hdr_index) |
20cfcaae | 2610 | { |
9c5bfbb7 | 2611 | const struct elf_backend_data *bed; |
20cfcaae NC |
2612 | |
2613 | switch (hdr->p_type) | |
2614 | { | |
2615 | case PT_NULL: | |
91d6fa6a | 2616 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "null"); |
20cfcaae NC |
2617 | |
2618 | case PT_LOAD: | |
91d6fa6a | 2619 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "load"); |
20cfcaae NC |
2620 | |
2621 | case PT_DYNAMIC: | |
91d6fa6a | 2622 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "dynamic"); |
20cfcaae NC |
2623 | |
2624 | case PT_INTERP: | |
91d6fa6a | 2625 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "interp"); |
20cfcaae NC |
2626 | |
2627 | case PT_NOTE: | |
91d6fa6a | 2628 | if (! _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "note")) |
b34976b6 | 2629 | return FALSE; |
718175fa | 2630 | if (! elf_read_notes (abfd, hdr->p_offset, hdr->p_filesz)) |
b34976b6 AM |
2631 | return FALSE; |
2632 | return TRUE; | |
20cfcaae NC |
2633 | |
2634 | case PT_SHLIB: | |
91d6fa6a | 2635 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "shlib"); |
20cfcaae NC |
2636 | |
2637 | case PT_PHDR: | |
91d6fa6a | 2638 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "phdr"); |
20cfcaae | 2639 | |
811072d8 | 2640 | case PT_GNU_EH_FRAME: |
91d6fa6a | 2641 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, |
811072d8 RM |
2642 | "eh_frame_hdr"); |
2643 | ||
2b05f1b7 | 2644 | case PT_GNU_STACK: |
91d6fa6a | 2645 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "stack"); |
9ee5e499 | 2646 | |
8c37241b | 2647 | case PT_GNU_RELRO: |
91d6fa6a | 2648 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "relro"); |
8c37241b | 2649 | |
20cfcaae | 2650 | default: |
8c1acd09 | 2651 | /* Check for any processor-specific program segment types. */ |
20cfcaae | 2652 | bed = get_elf_backend_data (abfd); |
91d6fa6a | 2653 | return bed->elf_backend_section_from_phdr (abfd, hdr, hdr_index, "proc"); |
20cfcaae NC |
2654 | } |
2655 | } | |
2656 | ||
d4730f92 BS |
2657 | /* Return the REL_HDR for SEC, assuming there is only a single one, either |
2658 | REL or RELA. */ | |
2659 | ||
2660 | Elf_Internal_Shdr * | |
2661 | _bfd_elf_single_rel_hdr (asection *sec) | |
2662 | { | |
2663 | if (elf_section_data (sec)->rel.hdr) | |
2664 | { | |
2665 | BFD_ASSERT (elf_section_data (sec)->rela.hdr == NULL); | |
2666 | return elf_section_data (sec)->rel.hdr; | |
2667 | } | |
2668 | else | |
2669 | return elf_section_data (sec)->rela.hdr; | |
2670 | } | |
2671 | ||
3e19fb8f L |
2672 | static bfd_boolean |
2673 | _bfd_elf_set_reloc_sh_name (bfd *abfd, | |
2674 | Elf_Internal_Shdr *rel_hdr, | |
2675 | const char *sec_name, | |
2676 | bfd_boolean use_rela_p) | |
2677 | { | |
2678 | char *name = (char *) bfd_alloc (abfd, | |
2679 | sizeof ".rela" + strlen (sec_name)); | |
2680 | if (name == NULL) | |
2681 | return FALSE; | |
2682 | ||
2683 | sprintf (name, "%s%s", use_rela_p ? ".rela" : ".rel", sec_name); | |
2684 | rel_hdr->sh_name = | |
2685 | (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), name, | |
2686 | FALSE); | |
2687 | if (rel_hdr->sh_name == (unsigned int) -1) | |
2688 | return FALSE; | |
2689 | ||
2690 | return TRUE; | |
2691 | } | |
2692 | ||
d4730f92 BS |
2693 | /* Allocate and initialize a section-header for a new reloc section, |
2694 | containing relocations against ASECT. It is stored in RELDATA. If | |
2695 | USE_RELA_P is TRUE, we use RELA relocations; otherwise, we use REL | |
2696 | relocations. */ | |
23bc299b | 2697 | |
5d13b3b3 | 2698 | static bfd_boolean |
217aa764 | 2699 | _bfd_elf_init_reloc_shdr (bfd *abfd, |
d4730f92 | 2700 | struct bfd_elf_section_reloc_data *reldata, |
f6fe1ccd | 2701 | const char *sec_name, |
3e19fb8f L |
2702 | bfd_boolean use_rela_p, |
2703 | bfd_boolean delay_st_name_p) | |
23bc299b | 2704 | { |
d4730f92 | 2705 | Elf_Internal_Shdr *rel_hdr; |
9c5bfbb7 | 2706 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
d4730f92 BS |
2707 | bfd_size_type amt; |
2708 | ||
2709 | amt = sizeof (Elf_Internal_Shdr); | |
2710 | BFD_ASSERT (reldata->hdr == NULL); | |
2711 | rel_hdr = bfd_zalloc (abfd, amt); | |
2712 | reldata->hdr = rel_hdr; | |
23bc299b | 2713 | |
3e19fb8f L |
2714 | if (delay_st_name_p) |
2715 | rel_hdr->sh_name = (unsigned int) -1; | |
2716 | else if (!_bfd_elf_set_reloc_sh_name (abfd, rel_hdr, sec_name, | |
2717 | use_rela_p)) | |
b34976b6 | 2718 | return FALSE; |
23bc299b MM |
2719 | rel_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL; |
2720 | rel_hdr->sh_entsize = (use_rela_p | |
2721 | ? bed->s->sizeof_rela | |
2722 | : bed->s->sizeof_rel); | |
72de5009 | 2723 | rel_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align; |
23bc299b MM |
2724 | rel_hdr->sh_flags = 0; |
2725 | rel_hdr->sh_addr = 0; | |
2726 | rel_hdr->sh_size = 0; | |
2727 | rel_hdr->sh_offset = 0; | |
2728 | ||
b34976b6 | 2729 | return TRUE; |
23bc299b MM |
2730 | } |
2731 | ||
94be91de JB |
2732 | /* Return the default section type based on the passed in section flags. */ |
2733 | ||
2734 | int | |
2735 | bfd_elf_get_default_section_type (flagword flags) | |
2736 | { | |
2737 | if ((flags & SEC_ALLOC) != 0 | |
2e76e85a | 2738 | && (flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0) |
94be91de JB |
2739 | return SHT_NOBITS; |
2740 | return SHT_PROGBITS; | |
2741 | } | |
2742 | ||
d4730f92 BS |
2743 | struct fake_section_arg |
2744 | { | |
2745 | struct bfd_link_info *link_info; | |
2746 | bfd_boolean failed; | |
2747 | }; | |
2748 | ||
252b5132 RH |
2749 | /* Set up an ELF internal section header for a section. */ |
2750 | ||
252b5132 | 2751 | static void |
d4730f92 | 2752 | elf_fake_sections (bfd *abfd, asection *asect, void *fsarg) |
252b5132 | 2753 | { |
d4730f92 | 2754 | struct fake_section_arg *arg = (struct fake_section_arg *)fsarg; |
9c5bfbb7 | 2755 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
d4730f92 | 2756 | struct bfd_elf_section_data *esd = elf_section_data (asect); |
252b5132 | 2757 | Elf_Internal_Shdr *this_hdr; |
0414f35b | 2758 | unsigned int sh_type; |
0ce398f1 | 2759 | const char *name = asect->name; |
3e19fb8f | 2760 | bfd_boolean delay_st_name_p = FALSE; |
252b5132 | 2761 | |
d4730f92 | 2762 | if (arg->failed) |
252b5132 RH |
2763 | { |
2764 | /* We already failed; just get out of the bfd_map_over_sections | |
08a40648 | 2765 | loop. */ |
252b5132 RH |
2766 | return; |
2767 | } | |
2768 | ||
d4730f92 | 2769 | this_hdr = &esd->this_hdr; |
252b5132 | 2770 | |
f6fe1ccd | 2771 | if (arg->link_info) |
0ce398f1 | 2772 | { |
f6fe1ccd L |
2773 | /* ld: compress DWARF debug sections with names: .debug_*. */ |
2774 | if ((arg->link_info->compress_debug & COMPRESS_DEBUG) | |
2775 | && (asect->flags & SEC_DEBUGGING) | |
2776 | && name[1] == 'd' | |
2777 | && name[6] == '_') | |
2778 | { | |
2779 | /* Set SEC_ELF_COMPRESS to indicate this section should be | |
2780 | compressed. */ | |
2781 | asect->flags |= SEC_ELF_COMPRESS; | |
0ce398f1 | 2782 | |
3e19fb8f L |
2783 | /* If this section will be compressed, delay adding setion |
2784 | name to section name section after it is compressed in | |
2785 | _bfd_elf_assign_file_positions_for_non_load. */ | |
2786 | delay_st_name_p = TRUE; | |
f6fe1ccd L |
2787 | } |
2788 | } | |
2789 | else if ((asect->flags & SEC_ELF_RENAME)) | |
2790 | { | |
2791 | /* objcopy: rename output DWARF debug section. */ | |
2792 | if ((abfd->flags & (BFD_DECOMPRESS | BFD_COMPRESS_GABI))) | |
2793 | { | |
2794 | /* When we decompress or compress with SHF_COMPRESSED, | |
2795 | convert section name from .zdebug_* to .debug_* if | |
2796 | needed. */ | |
2797 | if (name[1] == 'z') | |
2798 | { | |
2799 | char *new_name = convert_zdebug_to_debug (abfd, name); | |
2800 | if (new_name == NULL) | |
2801 | { | |
2802 | arg->failed = TRUE; | |
2803 | return; | |
2804 | } | |
2805 | name = new_name; | |
2806 | } | |
2807 | } | |
2808 | else if (asect->compress_status == COMPRESS_SECTION_DONE) | |
0ce398f1 | 2809 | { |
f6fe1ccd L |
2810 | /* PR binutils/18087: Compression does not always make a |
2811 | section smaller. So only rename the section when | |
2812 | compression has actually taken place. If input section | |
2813 | name is .zdebug_*, we should never compress it again. */ | |
2814 | char *new_name = convert_debug_to_zdebug (abfd, name); | |
0ce398f1 L |
2815 | if (new_name == NULL) |
2816 | { | |
2817 | arg->failed = TRUE; | |
2818 | return; | |
2819 | } | |
f6fe1ccd L |
2820 | BFD_ASSERT (name[1] != 'z'); |
2821 | name = new_name; | |
0ce398f1 L |
2822 | } |
2823 | } | |
2824 | ||
3e19fb8f L |
2825 | if (delay_st_name_p) |
2826 | this_hdr->sh_name = (unsigned int) -1; | |
2827 | else | |
252b5132 | 2828 | { |
3e19fb8f L |
2829 | this_hdr->sh_name |
2830 | = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), | |
2831 | name, FALSE); | |
2832 | if (this_hdr->sh_name == (unsigned int) -1) | |
2833 | { | |
2834 | arg->failed = TRUE; | |
2835 | return; | |
2836 | } | |
252b5132 RH |
2837 | } |
2838 | ||
a4d8e49b | 2839 | /* Don't clear sh_flags. Assembler may set additional bits. */ |
252b5132 RH |
2840 | |
2841 | if ((asect->flags & SEC_ALLOC) != 0 | |
2842 | || asect->user_set_vma) | |
2843 | this_hdr->sh_addr = asect->vma; | |
2844 | else | |
2845 | this_hdr->sh_addr = 0; | |
2846 | ||
2847 | this_hdr->sh_offset = 0; | |
eea6121a | 2848 | this_hdr->sh_size = asect->size; |
252b5132 | 2849 | this_hdr->sh_link = 0; |
c86934ce NC |
2850 | /* PR 17512: file: 0eb809fe, 8b0535ee. */ |
2851 | if (asect->alignment_power >= (sizeof (bfd_vma) * 8) - 1) | |
2852 | { | |
2853 | (*_bfd_error_handler) | |
2854 | (_("%B: error: Alignment power %d of section `%A' is too big"), | |
2855 | abfd, asect, asect->alignment_power); | |
2856 | arg->failed = TRUE; | |
2857 | return; | |
2858 | } | |
72de5009 | 2859 | this_hdr->sh_addralign = (bfd_vma) 1 << asect->alignment_power; |
252b5132 RH |
2860 | /* The sh_entsize and sh_info fields may have been set already by |
2861 | copy_private_section_data. */ | |
2862 | ||
2863 | this_hdr->bfd_section = asect; | |
2864 | this_hdr->contents = NULL; | |
2865 | ||
3cddba1e L |
2866 | /* If the section type is unspecified, we set it based on |
2867 | asect->flags. */ | |
98ece1b3 AM |
2868 | if ((asect->flags & SEC_GROUP) != 0) |
2869 | sh_type = SHT_GROUP; | |
98ece1b3 | 2870 | else |
94be91de | 2871 | sh_type = bfd_elf_get_default_section_type (asect->flags); |
98ece1b3 | 2872 | |
3cddba1e | 2873 | if (this_hdr->sh_type == SHT_NULL) |
98ece1b3 AM |
2874 | this_hdr->sh_type = sh_type; |
2875 | else if (this_hdr->sh_type == SHT_NOBITS | |
2876 | && sh_type == SHT_PROGBITS | |
2877 | && (asect->flags & SEC_ALLOC) != 0) | |
3cddba1e | 2878 | { |
98ece1b3 AM |
2879 | /* Warn if we are changing a NOBITS section to PROGBITS, but |
2880 | allow the link to proceed. This can happen when users link | |
2881 | non-bss input sections to bss output sections, or emit data | |
2882 | to a bss output section via a linker script. */ | |
2883 | (*_bfd_error_handler) | |
58f0869b | 2884 | (_("warning: section `%A' type changed to PROGBITS"), asect); |
98ece1b3 | 2885 | this_hdr->sh_type = sh_type; |
3cddba1e L |
2886 | } |
2887 | ||
2f89ff8d | 2888 | switch (this_hdr->sh_type) |
252b5132 | 2889 | { |
2f89ff8d | 2890 | default: |
2f89ff8d L |
2891 | break; |
2892 | ||
2893 | case SHT_STRTAB: | |
2894 | case SHT_INIT_ARRAY: | |
2895 | case SHT_FINI_ARRAY: | |
2896 | case SHT_PREINIT_ARRAY: | |
2897 | case SHT_NOTE: | |
2898 | case SHT_NOBITS: | |
2899 | case SHT_PROGBITS: | |
2900 | break; | |
2901 | ||
2902 | case SHT_HASH: | |
c7ac6ff8 | 2903 | this_hdr->sh_entsize = bed->s->sizeof_hash_entry; |
2f89ff8d | 2904 | break; |
5de3bf90 | 2905 | |
2f89ff8d | 2906 | case SHT_DYNSYM: |
252b5132 | 2907 | this_hdr->sh_entsize = bed->s->sizeof_sym; |
2f89ff8d L |
2908 | break; |
2909 | ||
2910 | case SHT_DYNAMIC: | |
252b5132 | 2911 | this_hdr->sh_entsize = bed->s->sizeof_dyn; |
2f89ff8d L |
2912 | break; |
2913 | ||
2914 | case SHT_RELA: | |
2915 | if (get_elf_backend_data (abfd)->may_use_rela_p) | |
2916 | this_hdr->sh_entsize = bed->s->sizeof_rela; | |
2917 | break; | |
2918 | ||
2919 | case SHT_REL: | |
2920 | if (get_elf_backend_data (abfd)->may_use_rel_p) | |
2921 | this_hdr->sh_entsize = bed->s->sizeof_rel; | |
2922 | break; | |
2923 | ||
2924 | case SHT_GNU_versym: | |
252b5132 | 2925 | this_hdr->sh_entsize = sizeof (Elf_External_Versym); |
2f89ff8d L |
2926 | break; |
2927 | ||
2928 | case SHT_GNU_verdef: | |
252b5132 RH |
2929 | this_hdr->sh_entsize = 0; |
2930 | /* objcopy or strip will copy over sh_info, but may not set | |
08a40648 AM |
2931 | cverdefs. The linker will set cverdefs, but sh_info will be |
2932 | zero. */ | |
252b5132 RH |
2933 | if (this_hdr->sh_info == 0) |
2934 | this_hdr->sh_info = elf_tdata (abfd)->cverdefs; | |
2935 | else | |
2936 | BFD_ASSERT (elf_tdata (abfd)->cverdefs == 0 | |
2937 | || this_hdr->sh_info == elf_tdata (abfd)->cverdefs); | |
2f89ff8d L |
2938 | break; |
2939 | ||
2940 | case SHT_GNU_verneed: | |
252b5132 RH |
2941 | this_hdr->sh_entsize = 0; |
2942 | /* objcopy or strip will copy over sh_info, but may not set | |
08a40648 AM |
2943 | cverrefs. The linker will set cverrefs, but sh_info will be |
2944 | zero. */ | |
252b5132 RH |
2945 | if (this_hdr->sh_info == 0) |
2946 | this_hdr->sh_info = elf_tdata (abfd)->cverrefs; | |
2947 | else | |
2948 | BFD_ASSERT (elf_tdata (abfd)->cverrefs == 0 | |
2949 | || this_hdr->sh_info == elf_tdata (abfd)->cverrefs); | |
2f89ff8d L |
2950 | break; |
2951 | ||
2952 | case SHT_GROUP: | |
1783205a | 2953 | this_hdr->sh_entsize = GRP_ENTRY_SIZE; |
2f89ff8d | 2954 | break; |
fdc90cb4 JJ |
2955 | |
2956 | case SHT_GNU_HASH: | |
2957 | this_hdr->sh_entsize = bed->s->arch_size == 64 ? 0 : 4; | |
2958 | break; | |
dbb410c3 | 2959 | } |
252b5132 RH |
2960 | |
2961 | if ((asect->flags & SEC_ALLOC) != 0) | |
2962 | this_hdr->sh_flags |= SHF_ALLOC; | |
2963 | if ((asect->flags & SEC_READONLY) == 0) | |
2964 | this_hdr->sh_flags |= SHF_WRITE; | |
2965 | if ((asect->flags & SEC_CODE) != 0) | |
2966 | this_hdr->sh_flags |= SHF_EXECINSTR; | |
f5fa8ca2 JJ |
2967 | if ((asect->flags & SEC_MERGE) != 0) |
2968 | { | |
2969 | this_hdr->sh_flags |= SHF_MERGE; | |
2970 | this_hdr->sh_entsize = asect->entsize; | |
2971 | if ((asect->flags & SEC_STRINGS) != 0) | |
2972 | this_hdr->sh_flags |= SHF_STRINGS; | |
2973 | } | |
1126897b | 2974 | if ((asect->flags & SEC_GROUP) == 0 && elf_group_name (asect) != NULL) |
dbb410c3 | 2975 | this_hdr->sh_flags |= SHF_GROUP; |
13ae64f3 | 2976 | if ((asect->flags & SEC_THREAD_LOCAL) != 0) |
704afa60 JJ |
2977 | { |
2978 | this_hdr->sh_flags |= SHF_TLS; | |
3a800eb9 AM |
2979 | if (asect->size == 0 |
2980 | && (asect->flags & SEC_HAS_CONTENTS) == 0) | |
704afa60 | 2981 | { |
3a800eb9 | 2982 | struct bfd_link_order *o = asect->map_tail.link_order; |
b34976b6 | 2983 | |
704afa60 | 2984 | this_hdr->sh_size = 0; |
3a800eb9 AM |
2985 | if (o != NULL) |
2986 | { | |
704afa60 | 2987 | this_hdr->sh_size = o->offset + o->size; |
3a800eb9 AM |
2988 | if (this_hdr->sh_size != 0) |
2989 | this_hdr->sh_type = SHT_NOBITS; | |
2990 | } | |
704afa60 JJ |
2991 | } |
2992 | } | |
18ae9cc1 L |
2993 | if ((asect->flags & (SEC_GROUP | SEC_EXCLUDE)) == SEC_EXCLUDE) |
2994 | this_hdr->sh_flags |= SHF_EXCLUDE; | |
252b5132 | 2995 | |
d4730f92 BS |
2996 | /* If the section has relocs, set up a section header for the |
2997 | SHT_REL[A] section. If two relocation sections are required for | |
2998 | this section, it is up to the processor-specific back-end to | |
2999 | create the other. */ | |
3000 | if ((asect->flags & SEC_RELOC) != 0) | |
3001 | { | |
3002 | /* When doing a relocatable link, create both REL and RELA sections if | |
3003 | needed. */ | |
3004 | if (arg->link_info | |
3005 | /* Do the normal setup if we wouldn't create any sections here. */ | |
3006 | && esd->rel.count + esd->rela.count > 0 | |
3007 | && (arg->link_info->relocatable || arg->link_info->emitrelocations)) | |
3008 | { | |
3009 | if (esd->rel.count && esd->rel.hdr == NULL | |
3e19fb8f L |
3010 | && !_bfd_elf_init_reloc_shdr (abfd, &esd->rel, name, FALSE, |
3011 | delay_st_name_p)) | |
d4730f92 BS |
3012 | { |
3013 | arg->failed = TRUE; | |
3014 | return; | |
3015 | } | |
3016 | if (esd->rela.count && esd->rela.hdr == NULL | |
3e19fb8f L |
3017 | && !_bfd_elf_init_reloc_shdr (abfd, &esd->rela, name, TRUE, |
3018 | delay_st_name_p)) | |
d4730f92 BS |
3019 | { |
3020 | arg->failed = TRUE; | |
3021 | return; | |
3022 | } | |
3023 | } | |
3024 | else if (!_bfd_elf_init_reloc_shdr (abfd, | |
3025 | (asect->use_rela_p | |
3026 | ? &esd->rela : &esd->rel), | |
f6fe1ccd | 3027 | name, |
3e19fb8f L |
3028 | asect->use_rela_p, |
3029 | delay_st_name_p)) | |
d4730f92 BS |
3030 | arg->failed = TRUE; |
3031 | } | |
3032 | ||
252b5132 | 3033 | /* Check for processor-specific section types. */ |
0414f35b | 3034 | sh_type = this_hdr->sh_type; |
e1fddb6b AO |
3035 | if (bed->elf_backend_fake_sections |
3036 | && !(*bed->elf_backend_fake_sections) (abfd, this_hdr, asect)) | |
d4730f92 | 3037 | arg->failed = TRUE; |
252b5132 | 3038 | |
42bb2e33 | 3039 | if (sh_type == SHT_NOBITS && asect->size != 0) |
0414f35b AM |
3040 | { |
3041 | /* Don't change the header type from NOBITS if we are being | |
42bb2e33 | 3042 | called for objcopy --only-keep-debug. */ |
0414f35b AM |
3043 | this_hdr->sh_type = sh_type; |
3044 | } | |
252b5132 RH |
3045 | } |
3046 | ||
bcacc0f5 AM |
3047 | /* Fill in the contents of a SHT_GROUP section. Called from |
3048 | _bfd_elf_compute_section_file_positions for gas, objcopy, and | |
3049 | when ELF targets use the generic linker, ld. Called for ld -r | |
3050 | from bfd_elf_final_link. */ | |
dbb410c3 | 3051 | |
1126897b | 3052 | void |
217aa764 | 3053 | bfd_elf_set_group_contents (bfd *abfd, asection *sec, void *failedptrarg) |
dbb410c3 | 3054 | { |
a50b1753 | 3055 | bfd_boolean *failedptr = (bfd_boolean *) failedptrarg; |
9dce4196 | 3056 | asection *elt, *first; |
dbb410c3 | 3057 | unsigned char *loc; |
b34976b6 | 3058 | bfd_boolean gas; |
dbb410c3 | 3059 | |
7e4111ad L |
3060 | /* Ignore linker created group section. See elfNN_ia64_object_p in |
3061 | elfxx-ia64.c. */ | |
3062 | if (((sec->flags & (SEC_GROUP | SEC_LINKER_CREATED)) != SEC_GROUP) | |
dbb410c3 AM |
3063 | || *failedptr) |
3064 | return; | |
3065 | ||
bcacc0f5 AM |
3066 | if (elf_section_data (sec)->this_hdr.sh_info == 0) |
3067 | { | |
3068 | unsigned long symindx = 0; | |
3069 | ||
3070 | /* elf_group_id will have been set up by objcopy and the | |
3071 | generic linker. */ | |
3072 | if (elf_group_id (sec) != NULL) | |
3073 | symindx = elf_group_id (sec)->udata.i; | |
1126897b | 3074 | |
bcacc0f5 AM |
3075 | if (symindx == 0) |
3076 | { | |
3077 | /* If called from the assembler, swap_out_syms will have set up | |
3078 | elf_section_syms. */ | |
3079 | BFD_ASSERT (elf_section_syms (abfd) != NULL); | |
3080 | symindx = elf_section_syms (abfd)[sec->index]->udata.i; | |
3081 | } | |
3082 | elf_section_data (sec)->this_hdr.sh_info = symindx; | |
3083 | } | |
3084 | else if (elf_section_data (sec)->this_hdr.sh_info == (unsigned int) -2) | |
1126897b | 3085 | { |
bcacc0f5 AM |
3086 | /* The ELF backend linker sets sh_info to -2 when the group |
3087 | signature symbol is global, and thus the index can't be | |
3088 | set until all local symbols are output. */ | |
3089 | asection *igroup = elf_sec_group (elf_next_in_group (sec)); | |
3090 | struct bfd_elf_section_data *sec_data = elf_section_data (igroup); | |
3091 | unsigned long symndx = sec_data->this_hdr.sh_info; | |
3092 | unsigned long extsymoff = 0; | |
3093 | struct elf_link_hash_entry *h; | |
3094 | ||
3095 | if (!elf_bad_symtab (igroup->owner)) | |
3096 | { | |
3097 | Elf_Internal_Shdr *symtab_hdr; | |
3098 | ||
3099 | symtab_hdr = &elf_tdata (igroup->owner)->symtab_hdr; | |
3100 | extsymoff = symtab_hdr->sh_info; | |
3101 | } | |
3102 | h = elf_sym_hashes (igroup->owner)[symndx - extsymoff]; | |
3103 | while (h->root.type == bfd_link_hash_indirect | |
3104 | || h->root.type == bfd_link_hash_warning) | |
3105 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
3106 | ||
3107 | elf_section_data (sec)->this_hdr.sh_info = h->indx; | |
1126897b | 3108 | } |
dbb410c3 | 3109 | |
1126897b | 3110 | /* The contents won't be allocated for "ld -r" or objcopy. */ |
b34976b6 | 3111 | gas = TRUE; |
dbb410c3 AM |
3112 | if (sec->contents == NULL) |
3113 | { | |
b34976b6 | 3114 | gas = FALSE; |
a50b1753 | 3115 | sec->contents = (unsigned char *) bfd_alloc (abfd, sec->size); |
9dce4196 AM |
3116 | |
3117 | /* Arrange for the section to be written out. */ | |
3118 | elf_section_data (sec)->this_hdr.contents = sec->contents; | |
dbb410c3 AM |
3119 | if (sec->contents == NULL) |
3120 | { | |
b34976b6 | 3121 | *failedptr = TRUE; |
dbb410c3 AM |
3122 | return; |
3123 | } | |
3124 | } | |
3125 | ||
eea6121a | 3126 | loc = sec->contents + sec->size; |
dbb410c3 | 3127 | |
9dce4196 AM |
3128 | /* Get the pointer to the first section in the group that gas |
3129 | squirreled away here. objcopy arranges for this to be set to the | |
3130 | start of the input section group. */ | |
3131 | first = elt = elf_next_in_group (sec); | |
dbb410c3 AM |
3132 | |
3133 | /* First element is a flag word. Rest of section is elf section | |
3134 | indices for all the sections of the group. Write them backwards | |
3135 | just to keep the group in the same order as given in .section | |
3136 | directives, not that it matters. */ | |
3137 | while (elt != NULL) | |
3138 | { | |
9dce4196 | 3139 | asection *s; |
9dce4196 | 3140 | |
9dce4196 | 3141 | s = elt; |
415f38a6 AM |
3142 | if (!gas) |
3143 | s = s->output_section; | |
3144 | if (s != NULL | |
3145 | && !bfd_is_abs_section (s)) | |
01e1a5bc | 3146 | { |
415f38a6 AM |
3147 | unsigned int idx = elf_section_data (s)->this_idx; |
3148 | ||
01e1a5bc | 3149 | loc -= 4; |
01e1a5bc NC |
3150 | H_PUT_32 (abfd, idx, loc); |
3151 | } | |
945906ff | 3152 | elt = elf_next_in_group (elt); |
9dce4196 AM |
3153 | if (elt == first) |
3154 | break; | |
dbb410c3 AM |
3155 | } |
3156 | ||
3d7f7666 | 3157 | if ((loc -= 4) != sec->contents) |
9dce4196 | 3158 | abort (); |
dbb410c3 | 3159 | |
9dce4196 | 3160 | H_PUT_32 (abfd, sec->flags & SEC_LINK_ONCE ? GRP_COMDAT : 0, loc); |
dbb410c3 AM |
3161 | } |
3162 | ||
bd53a53a L |
3163 | /* Return the section which RELOC_SEC applies to. */ |
3164 | ||
3165 | asection * | |
3166 | _bfd_elf_get_reloc_section (asection *reloc_sec) | |
3167 | { | |
3168 | const char *name; | |
3169 | unsigned int type; | |
3170 | bfd *abfd; | |
3171 | ||
3172 | if (reloc_sec == NULL) | |
3173 | return NULL; | |
3174 | ||
3175 | type = elf_section_data (reloc_sec)->this_hdr.sh_type; | |
3176 | if (type != SHT_REL && type != SHT_RELA) | |
3177 | return NULL; | |
3178 | ||
3179 | /* We look up the section the relocs apply to by name. */ | |
3180 | name = reloc_sec->name; | |
3181 | if (type == SHT_REL) | |
3182 | name += 4; | |
3183 | else | |
3184 | name += 5; | |
3185 | ||
3186 | /* If a target needs .got.plt section, relocations in rela.plt/rel.plt | |
3187 | section apply to .got.plt section. */ | |
3188 | abfd = reloc_sec->owner; | |
3189 | if (get_elf_backend_data (abfd)->want_got_plt | |
3190 | && strcmp (name, ".plt") == 0) | |
87070c08 AM |
3191 | { |
3192 | /* .got.plt is a linker created input section. It may be mapped | |
3193 | to some other output section. Try two likely sections. */ | |
3194 | name = ".got.plt"; | |
3195 | reloc_sec = bfd_get_section_by_name (abfd, name); | |
3196 | if (reloc_sec != NULL) | |
3197 | return reloc_sec; | |
3198 | name = ".got"; | |
3199 | } | |
bd53a53a L |
3200 | |
3201 | reloc_sec = bfd_get_section_by_name (abfd, name); | |
3202 | return reloc_sec; | |
3203 | } | |
3204 | ||
252b5132 RH |
3205 | /* Assign all ELF section numbers. The dummy first section is handled here |
3206 | too. The link/info pointers for the standard section types are filled | |
3207 | in here too, while we're at it. */ | |
3208 | ||
b34976b6 | 3209 | static bfd_boolean |
da9f89d4 | 3210 | assign_section_numbers (bfd *abfd, struct bfd_link_info *link_info) |
252b5132 RH |
3211 | { |
3212 | struct elf_obj_tdata *t = elf_tdata (abfd); | |
3213 | asection *sec; | |
3e19fb8f | 3214 | unsigned int section_number; |
252b5132 | 3215 | Elf_Internal_Shdr **i_shdrp; |
47cc2cf5 | 3216 | struct bfd_elf_section_data *d; |
3516e984 | 3217 | bfd_boolean need_symtab; |
252b5132 RH |
3218 | |
3219 | section_number = 1; | |
3220 | ||
2b0f7ef9 JJ |
3221 | _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd)); |
3222 | ||
da9f89d4 L |
3223 | /* SHT_GROUP sections are in relocatable files only. */ |
3224 | if (link_info == NULL || link_info->relocatable) | |
252b5132 | 3225 | { |
da9f89d4 | 3226 | /* Put SHT_GROUP sections first. */ |
04dd1667 | 3227 | for (sec = abfd->sections; sec != NULL; sec = sec->next) |
47cc2cf5 | 3228 | { |
5daa8fe7 | 3229 | d = elf_section_data (sec); |
da9f89d4 L |
3230 | |
3231 | if (d->this_hdr.sh_type == SHT_GROUP) | |
08a40648 | 3232 | { |
5daa8fe7 | 3233 | if (sec->flags & SEC_LINKER_CREATED) |
da9f89d4 L |
3234 | { |
3235 | /* Remove the linker created SHT_GROUP sections. */ | |
5daa8fe7 | 3236 | bfd_section_list_remove (abfd, sec); |
da9f89d4 | 3237 | abfd->section_count--; |
da9f89d4 | 3238 | } |
08a40648 | 3239 | else |
4fbb74a6 | 3240 | d->this_idx = section_number++; |
da9f89d4 | 3241 | } |
47cc2cf5 PB |
3242 | } |
3243 | } | |
3244 | ||
3245 | for (sec = abfd->sections; sec; sec = sec->next) | |
3246 | { | |
3247 | d = elf_section_data (sec); | |
3248 | ||
3249 | if (d->this_hdr.sh_type != SHT_GROUP) | |
4fbb74a6 | 3250 | d->this_idx = section_number++; |
3e19fb8f L |
3251 | if (d->this_hdr.sh_name != (unsigned int) -1) |
3252 | _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->this_hdr.sh_name); | |
d4730f92 | 3253 | if (d->rel.hdr) |
2b0f7ef9 | 3254 | { |
d4730f92 | 3255 | d->rel.idx = section_number++; |
3e19fb8f L |
3256 | if (d->rel.hdr->sh_name != (unsigned int) -1) |
3257 | _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel.hdr->sh_name); | |
2b0f7ef9 | 3258 | } |
d4730f92 BS |
3259 | else |
3260 | d->rel.idx = 0; | |
23bc299b | 3261 | |
d4730f92 | 3262 | if (d->rela.hdr) |
2b0f7ef9 | 3263 | { |
d4730f92 | 3264 | d->rela.idx = section_number++; |
3e19fb8f L |
3265 | if (d->rela.hdr->sh_name != (unsigned int) -1) |
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 | ||
9ad5cbcf | 3304 | elf_numsections (abfd) = section_number; |
252b5132 RH |
3305 | elf_elfheader (abfd)->e_shnum = section_number; |
3306 | ||
3307 | /* Set up the list of section header pointers, in agreement with the | |
3308 | indices. */ | |
a50b1753 NC |
3309 | i_shdrp = (Elf_Internal_Shdr **) bfd_zalloc2 (abfd, section_number, |
3310 | sizeof (Elf_Internal_Shdr *)); | |
252b5132 | 3311 | if (i_shdrp == NULL) |
b34976b6 | 3312 | return FALSE; |
252b5132 | 3313 | |
a50b1753 NC |
3314 | i_shdrp[0] = (Elf_Internal_Shdr *) bfd_zalloc (abfd, |
3315 | sizeof (Elf_Internal_Shdr)); | |
252b5132 RH |
3316 | if (i_shdrp[0] == NULL) |
3317 | { | |
3318 | bfd_release (abfd, i_shdrp); | |
b34976b6 | 3319 | return FALSE; |
252b5132 | 3320 | } |
252b5132 RH |
3321 | |
3322 | elf_elfsections (abfd) = i_shdrp; | |
3323 | ||
12bd6957 | 3324 | i_shdrp[elf_shstrtab_sec (abfd)] = &t->shstrtab_hdr; |
3516e984 | 3325 | if (need_symtab) |
252b5132 | 3326 | { |
12bd6957 | 3327 | i_shdrp[elf_onesymtab (abfd)] = &t->symtab_hdr; |
4fbb74a6 | 3328 | if (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF)) |
9ad5cbcf | 3329 | { |
12bd6957 AM |
3330 | i_shdrp[elf_symtab_shndx (abfd)] = &t->symtab_shndx_hdr; |
3331 | t->symtab_shndx_hdr.sh_link = elf_onesymtab (abfd); | |
9ad5cbcf | 3332 | } |
12bd6957 AM |
3333 | i_shdrp[elf_strtab_sec (abfd)] = &t->strtab_hdr; |
3334 | t->symtab_hdr.sh_link = elf_strtab_sec (abfd); | |
252b5132 | 3335 | } |
38ce5b11 | 3336 | |
252b5132 RH |
3337 | for (sec = abfd->sections; sec; sec = sec->next) |
3338 | { | |
252b5132 | 3339 | asection *s; |
252b5132 | 3340 | |
91d6fa6a NC |
3341 | d = elf_section_data (sec); |
3342 | ||
252b5132 | 3343 | i_shdrp[d->this_idx] = &d->this_hdr; |
d4730f92 BS |
3344 | if (d->rel.idx != 0) |
3345 | i_shdrp[d->rel.idx] = d->rel.hdr; | |
3346 | if (d->rela.idx != 0) | |
3347 | i_shdrp[d->rela.idx] = d->rela.hdr; | |
252b5132 RH |
3348 | |
3349 | /* Fill in the sh_link and sh_info fields while we're at it. */ | |
3350 | ||
3351 | /* sh_link of a reloc section is the section index of the symbol | |
3352 | table. sh_info is the section index of the section to which | |
3353 | the relocation entries apply. */ | |
d4730f92 | 3354 | if (d->rel.idx != 0) |
252b5132 | 3355 | { |
12bd6957 | 3356 | d->rel.hdr->sh_link = elf_onesymtab (abfd); |
d4730f92 | 3357 | d->rel.hdr->sh_info = d->this_idx; |
9ef5d938 | 3358 | d->rel.hdr->sh_flags |= SHF_INFO_LINK; |
252b5132 | 3359 | } |
d4730f92 | 3360 | if (d->rela.idx != 0) |
23bc299b | 3361 | { |
12bd6957 | 3362 | d->rela.hdr->sh_link = elf_onesymtab (abfd); |
d4730f92 | 3363 | d->rela.hdr->sh_info = d->this_idx; |
9ef5d938 | 3364 | d->rela.hdr->sh_flags |= SHF_INFO_LINK; |
23bc299b | 3365 | } |
252b5132 | 3366 | |
38ce5b11 L |
3367 | /* We need to set up sh_link for SHF_LINK_ORDER. */ |
3368 | if ((d->this_hdr.sh_flags & SHF_LINK_ORDER) != 0) | |
3369 | { | |
3370 | s = elf_linked_to_section (sec); | |
3371 | if (s) | |
38ce5b11 | 3372 | { |
f2876037 | 3373 | /* elf_linked_to_section points to the input section. */ |
ccd2ec6a | 3374 | if (link_info != NULL) |
38ce5b11 | 3375 | { |
f2876037 | 3376 | /* Check discarded linkonce section. */ |
dbaa2011 | 3377 | if (discarded_section (s)) |
38ce5b11 | 3378 | { |
ccd2ec6a L |
3379 | asection *kept; |
3380 | (*_bfd_error_handler) | |
3381 | (_("%B: sh_link of section `%A' points to discarded section `%A' of `%B'"), | |
3382 | abfd, d->this_hdr.bfd_section, | |
3383 | s, s->owner); | |
3384 | /* Point to the kept section if it has the same | |
3385 | size as the discarded one. */ | |
c0f00686 | 3386 | kept = _bfd_elf_check_kept_section (s, link_info); |
ccd2ec6a | 3387 | if (kept == NULL) |
185d09ad | 3388 | { |
ccd2ec6a L |
3389 | bfd_set_error (bfd_error_bad_value); |
3390 | return FALSE; | |
185d09ad | 3391 | } |
ccd2ec6a | 3392 | s = kept; |
38ce5b11 | 3393 | } |
e424ecc8 | 3394 | |
ccd2ec6a L |
3395 | s = s->output_section; |
3396 | BFD_ASSERT (s != NULL); | |
38ce5b11 | 3397 | } |
f2876037 L |
3398 | else |
3399 | { | |
3400 | /* Handle objcopy. */ | |
3401 | if (s->output_section == NULL) | |
3402 | { | |
3403 | (*_bfd_error_handler) | |
3404 | (_("%B: sh_link of section `%A' points to removed section `%A' of `%B'"), | |
3405 | abfd, d->this_hdr.bfd_section, s, s->owner); | |
3406 | bfd_set_error (bfd_error_bad_value); | |
3407 | return FALSE; | |
3408 | } | |
3409 | s = s->output_section; | |
3410 | } | |
ccd2ec6a L |
3411 | d->this_hdr.sh_link = elf_section_data (s)->this_idx; |
3412 | } | |
3413 | else | |
3414 | { | |
3415 | /* PR 290: | |
3416 | The Intel C compiler generates SHT_IA_64_UNWIND with | |
3417 | SHF_LINK_ORDER. But it doesn't set the sh_link or | |
3418 | sh_info fields. Hence we could get the situation | |
08a40648 | 3419 | where s is NULL. */ |
ccd2ec6a L |
3420 | const struct elf_backend_data *bed |
3421 | = get_elf_backend_data (abfd); | |
3422 | if (bed->link_order_error_handler) | |
3423 | bed->link_order_error_handler | |
3424 | (_("%B: warning: sh_link not set for section `%A'"), | |
3425 | abfd, sec); | |
38ce5b11 L |
3426 | } |
3427 | } | |
3428 | ||
252b5132 RH |
3429 | switch (d->this_hdr.sh_type) |
3430 | { | |
3431 | case SHT_REL: | |
3432 | case SHT_RELA: | |
3433 | /* A reloc section which we are treating as a normal BFD | |
3434 | section. sh_link is the section index of the symbol | |
3435 | table. sh_info is the section index of the section to | |
3436 | which the relocation entries apply. We assume that an | |
3437 | allocated reloc section uses the dynamic symbol table. | |
3438 | FIXME: How can we be sure? */ | |
3439 | s = bfd_get_section_by_name (abfd, ".dynsym"); | |
3440 | if (s != NULL) | |
3441 | d->this_hdr.sh_link = elf_section_data (s)->this_idx; | |
3442 | ||
bd53a53a | 3443 | s = get_elf_backend_data (abfd)->get_reloc_section (sec); |
252b5132 | 3444 | if (s != NULL) |
9ef5d938 L |
3445 | { |
3446 | d->this_hdr.sh_info = elf_section_data (s)->this_idx; | |
3447 | d->this_hdr.sh_flags |= SHF_INFO_LINK; | |
3448 | } | |
252b5132 RH |
3449 | break; |
3450 | ||
3451 | case SHT_STRTAB: | |
3452 | /* We assume that a section named .stab*str is a stabs | |
3453 | string section. We look for a section with the same name | |
3454 | but without the trailing ``str'', and set its sh_link | |
3455 | field to point to this section. */ | |
0112cd26 | 3456 | if (CONST_STRNEQ (sec->name, ".stab") |
252b5132 RH |
3457 | && strcmp (sec->name + strlen (sec->name) - 3, "str") == 0) |
3458 | { | |
3459 | size_t len; | |
3460 | char *alc; | |
3461 | ||
3462 | len = strlen (sec->name); | |
a50b1753 | 3463 | alc = (char *) bfd_malloc (len - 2); |
252b5132 | 3464 | if (alc == NULL) |
b34976b6 | 3465 | return FALSE; |
d4c88bbb | 3466 | memcpy (alc, sec->name, len - 3); |
252b5132 RH |
3467 | alc[len - 3] = '\0'; |
3468 | s = bfd_get_section_by_name (abfd, alc); | |
3469 | free (alc); | |
3470 | if (s != NULL) | |
3471 | { | |
3472 | elf_section_data (s)->this_hdr.sh_link = d->this_idx; | |
3473 | ||
3474 | /* This is a .stab section. */ | |
0594c12d AM |
3475 | if (elf_section_data (s)->this_hdr.sh_entsize == 0) |
3476 | elf_section_data (s)->this_hdr.sh_entsize | |
3477 | = 4 + 2 * bfd_get_arch_size (abfd) / 8; | |
252b5132 RH |
3478 | } |
3479 | } | |
3480 | break; | |
3481 | ||
3482 | case SHT_DYNAMIC: | |
3483 | case SHT_DYNSYM: | |
3484 | case SHT_GNU_verneed: | |
3485 | case SHT_GNU_verdef: | |
3486 | /* sh_link is the section header index of the string table | |
3487 | used for the dynamic entries, or the symbol table, or the | |
3488 | version strings. */ | |
3489 | s = bfd_get_section_by_name (abfd, ".dynstr"); | |
3490 | if (s != NULL) | |
3491 | d->this_hdr.sh_link = elf_section_data (s)->this_idx; | |
3492 | break; | |
3493 | ||
7f1204bb JJ |
3494 | case SHT_GNU_LIBLIST: |
3495 | /* sh_link is the section header index of the prelink library | |
08a40648 AM |
3496 | list used for the dynamic entries, or the symbol table, or |
3497 | the version strings. */ | |
7f1204bb JJ |
3498 | s = bfd_get_section_by_name (abfd, (sec->flags & SEC_ALLOC) |
3499 | ? ".dynstr" : ".gnu.libstr"); | |
3500 | if (s != NULL) | |
3501 | d->this_hdr.sh_link = elf_section_data (s)->this_idx; | |
3502 | break; | |
3503 | ||
252b5132 | 3504 | case SHT_HASH: |
fdc90cb4 | 3505 | case SHT_GNU_HASH: |
252b5132 RH |
3506 | case SHT_GNU_versym: |
3507 | /* sh_link is the section header index of the symbol table | |
3508 | this hash table or version table is for. */ | |
3509 | s = bfd_get_section_by_name (abfd, ".dynsym"); | |
3510 | if (s != NULL) | |
3511 | d->this_hdr.sh_link = elf_section_data (s)->this_idx; | |
3512 | break; | |
dbb410c3 AM |
3513 | |
3514 | case SHT_GROUP: | |
12bd6957 | 3515 | d->this_hdr.sh_link = elf_onesymtab (abfd); |
252b5132 RH |
3516 | } |
3517 | } | |
3518 | ||
3e19fb8f L |
3519 | /* Delay setting sh_name to _bfd_elf_write_object_contents so that |
3520 | _bfd_elf_assign_file_positions_for_non_load can convert DWARF | |
3521 | debug section name from .debug_* to .zdebug_* if needed. */ | |
3522 | ||
b34976b6 | 3523 | return TRUE; |
252b5132 RH |
3524 | } |
3525 | ||
5372391b | 3526 | static bfd_boolean |
217aa764 | 3527 | sym_is_global (bfd *abfd, asymbol *sym) |
252b5132 RH |
3528 | { |
3529 | /* If the backend has a special mapping, use it. */ | |
9c5bfbb7 | 3530 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
217aa764 AM |
3531 | if (bed->elf_backend_sym_is_global) |
3532 | return (*bed->elf_backend_sym_is_global) (abfd, sym); | |
252b5132 | 3533 | |
e47bf690 | 3534 | return ((sym->flags & (BSF_GLOBAL | BSF_WEAK | BSF_GNU_UNIQUE)) != 0 |
252b5132 RH |
3535 | || bfd_is_und_section (bfd_get_section (sym)) |
3536 | || bfd_is_com_section (bfd_get_section (sym))); | |
3537 | } | |
3538 | ||
5372391b | 3539 | /* Don't output section symbols for sections that are not going to be |
c6d8cab4 | 3540 | output, that are duplicates or there is no BFD section. */ |
5372391b AM |
3541 | |
3542 | static bfd_boolean | |
3543 | ignore_section_sym (bfd *abfd, asymbol *sym) | |
3544 | { | |
c6d8cab4 L |
3545 | elf_symbol_type *type_ptr; |
3546 | ||
3547 | if ((sym->flags & BSF_SECTION_SYM) == 0) | |
3548 | return FALSE; | |
3549 | ||
3550 | type_ptr = elf_symbol_from (abfd, sym); | |
3551 | return ((type_ptr != NULL | |
3552 | && type_ptr->internal_elf_sym.st_shndx != 0 | |
3553 | && bfd_is_abs_section (sym->section)) | |
3554 | || !(sym->section->owner == abfd | |
0f0a5e58 | 3555 | || (sym->section->output_section->owner == abfd |
2633a79c AM |
3556 | && sym->section->output_offset == 0) |
3557 | || bfd_is_abs_section (sym->section))); | |
5372391b AM |
3558 | } |
3559 | ||
2633a79c AM |
3560 | /* Map symbol from it's internal number to the external number, moving |
3561 | all local symbols to be at the head of the list. */ | |
3562 | ||
b34976b6 | 3563 | static bfd_boolean |
12bd6957 | 3564 | elf_map_symbols (bfd *abfd, unsigned int *pnum_locals) |
252b5132 | 3565 | { |
dc810e39 | 3566 | unsigned int symcount = bfd_get_symcount (abfd); |
252b5132 RH |
3567 | asymbol **syms = bfd_get_outsymbols (abfd); |
3568 | asymbol **sect_syms; | |
dc810e39 AM |
3569 | unsigned int num_locals = 0; |
3570 | unsigned int num_globals = 0; | |
3571 | unsigned int num_locals2 = 0; | |
3572 | unsigned int num_globals2 = 0; | |
252b5132 | 3573 | int max_index = 0; |
dc810e39 | 3574 | unsigned int idx; |
252b5132 RH |
3575 | asection *asect; |
3576 | asymbol **new_syms; | |
252b5132 RH |
3577 | |
3578 | #ifdef DEBUG | |
3579 | fprintf (stderr, "elf_map_symbols\n"); | |
3580 | fflush (stderr); | |
3581 | #endif | |
3582 | ||
252b5132 RH |
3583 | for (asect = abfd->sections; asect; asect = asect->next) |
3584 | { | |
3585 | if (max_index < asect->index) | |
3586 | max_index = asect->index; | |
3587 | } | |
3588 | ||
3589 | max_index++; | |
a50b1753 | 3590 | sect_syms = (asymbol **) bfd_zalloc2 (abfd, max_index, sizeof (asymbol *)); |
252b5132 | 3591 | if (sect_syms == NULL) |
b34976b6 | 3592 | return FALSE; |
252b5132 | 3593 | elf_section_syms (abfd) = sect_syms; |
4e89ac30 | 3594 | elf_num_section_syms (abfd) = max_index; |
252b5132 | 3595 | |
079e9a2f AM |
3596 | /* Init sect_syms entries for any section symbols we have already |
3597 | decided to output. */ | |
252b5132 RH |
3598 | for (idx = 0; idx < symcount; idx++) |
3599 | { | |
dc810e39 | 3600 | asymbol *sym = syms[idx]; |
c044fabd | 3601 | |
252b5132 | 3602 | if ((sym->flags & BSF_SECTION_SYM) != 0 |
0f0a5e58 | 3603 | && sym->value == 0 |
2633a79c AM |
3604 | && !ignore_section_sym (abfd, sym) |
3605 | && !bfd_is_abs_section (sym->section)) | |
252b5132 | 3606 | { |
5372391b | 3607 | asection *sec = sym->section; |
252b5132 | 3608 | |
5372391b AM |
3609 | if (sec->owner != abfd) |
3610 | sec = sec->output_section; | |
252b5132 | 3611 | |
5372391b | 3612 | sect_syms[sec->index] = syms[idx]; |
252b5132 RH |
3613 | } |
3614 | } | |
3615 | ||
252b5132 RH |
3616 | /* Classify all of the symbols. */ |
3617 | for (idx = 0; idx < symcount; idx++) | |
3618 | { | |
2633a79c | 3619 | if (sym_is_global (abfd, syms[idx])) |
252b5132 | 3620 | num_globals++; |
2633a79c AM |
3621 | else if (!ignore_section_sym (abfd, syms[idx])) |
3622 | num_locals++; | |
252b5132 | 3623 | } |
079e9a2f | 3624 | |
5372391b | 3625 | /* We will be adding a section symbol for each normal BFD section. Most |
079e9a2f AM |
3626 | sections will already have a section symbol in outsymbols, but |
3627 | eg. SHT_GROUP sections will not, and we need the section symbol mapped | |
3628 | at least in that case. */ | |
252b5132 RH |
3629 | for (asect = abfd->sections; asect; asect = asect->next) |
3630 | { | |
079e9a2f | 3631 | if (sect_syms[asect->index] == NULL) |
252b5132 | 3632 | { |
079e9a2f | 3633 | if (!sym_is_global (abfd, asect->symbol)) |
252b5132 RH |
3634 | num_locals++; |
3635 | else | |
3636 | num_globals++; | |
252b5132 RH |
3637 | } |
3638 | } | |
3639 | ||
3640 | /* Now sort the symbols so the local symbols are first. */ | |
a50b1753 NC |
3641 | new_syms = (asymbol **) bfd_alloc2 (abfd, num_locals + num_globals, |
3642 | sizeof (asymbol *)); | |
dc810e39 | 3643 | |
252b5132 | 3644 | if (new_syms == NULL) |
b34976b6 | 3645 | return FALSE; |
252b5132 RH |
3646 | |
3647 | for (idx = 0; idx < symcount; idx++) | |
3648 | { | |
3649 | asymbol *sym = syms[idx]; | |
dc810e39 | 3650 | unsigned int i; |
252b5132 | 3651 | |
2633a79c AM |
3652 | if (sym_is_global (abfd, sym)) |
3653 | i = num_locals + num_globals2++; | |
3654 | else if (!ignore_section_sym (abfd, sym)) | |
252b5132 RH |
3655 | i = num_locals2++; |
3656 | else | |
2633a79c | 3657 | continue; |
252b5132 RH |
3658 | new_syms[i] = sym; |
3659 | sym->udata.i = i + 1; | |
3660 | } | |
3661 | for (asect = abfd->sections; asect; asect = asect->next) | |
3662 | { | |
079e9a2f | 3663 | if (sect_syms[asect->index] == NULL) |
252b5132 | 3664 | { |
079e9a2f | 3665 | asymbol *sym = asect->symbol; |
dc810e39 | 3666 | unsigned int i; |
252b5132 | 3667 | |
079e9a2f | 3668 | sect_syms[asect->index] = sym; |
252b5132 RH |
3669 | if (!sym_is_global (abfd, sym)) |
3670 | i = num_locals2++; | |
3671 | else | |
3672 | i = num_locals + num_globals2++; | |
3673 | new_syms[i] = sym; | |
3674 | sym->udata.i = i + 1; | |
3675 | } | |
3676 | } | |
3677 | ||
3678 | bfd_set_symtab (abfd, new_syms, num_locals + num_globals); | |
3679 | ||
12bd6957 | 3680 | *pnum_locals = num_locals; |
b34976b6 | 3681 | return TRUE; |
252b5132 RH |
3682 | } |
3683 | ||
3684 | /* Align to the maximum file alignment that could be required for any | |
3685 | ELF data structure. */ | |
3686 | ||
268b6b39 | 3687 | static inline file_ptr |
217aa764 | 3688 | align_file_position (file_ptr off, int align) |
252b5132 RH |
3689 | { |
3690 | return (off + align - 1) & ~(align - 1); | |
3691 | } | |
3692 | ||
3693 | /* Assign a file position to a section, optionally aligning to the | |
3694 | required section alignment. */ | |
3695 | ||
217aa764 AM |
3696 | file_ptr |
3697 | _bfd_elf_assign_file_position_for_section (Elf_Internal_Shdr *i_shdrp, | |
3698 | file_ptr offset, | |
3699 | bfd_boolean align) | |
252b5132 | 3700 | { |
72de5009 AM |
3701 | if (align && i_shdrp->sh_addralign > 1) |
3702 | offset = BFD_ALIGN (offset, i_shdrp->sh_addralign); | |
252b5132 RH |
3703 | i_shdrp->sh_offset = offset; |
3704 | if (i_shdrp->bfd_section != NULL) | |
3705 | i_shdrp->bfd_section->filepos = offset; | |
3706 | if (i_shdrp->sh_type != SHT_NOBITS) | |
3707 | offset += i_shdrp->sh_size; | |
3708 | return offset; | |
3709 | } | |
3710 | ||
3711 | /* Compute the file positions we are going to put the sections at, and | |
3712 | otherwise prepare to begin writing out the ELF file. If LINK_INFO | |
3713 | is not NULL, this is being called by the ELF backend linker. */ | |
3714 | ||
b34976b6 | 3715 | bfd_boolean |
217aa764 AM |
3716 | _bfd_elf_compute_section_file_positions (bfd *abfd, |
3717 | struct bfd_link_info *link_info) | |
252b5132 | 3718 | { |
9c5bfbb7 | 3719 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
d4730f92 | 3720 | struct fake_section_arg fsargs; |
b34976b6 | 3721 | bfd_boolean failed; |
ef10c3ac | 3722 | struct elf_strtab_hash *strtab = NULL; |
252b5132 | 3723 | Elf_Internal_Shdr *shstrtab_hdr; |
3516e984 | 3724 | bfd_boolean need_symtab; |
252b5132 RH |
3725 | |
3726 | if (abfd->output_has_begun) | |
b34976b6 | 3727 | return TRUE; |
252b5132 RH |
3728 | |
3729 | /* Do any elf backend specific processing first. */ | |
3730 | if (bed->elf_backend_begin_write_processing) | |
3731 | (*bed->elf_backend_begin_write_processing) (abfd, link_info); | |
3732 | ||
3733 | if (! prep_headers (abfd)) | |
b34976b6 | 3734 | return FALSE; |
252b5132 | 3735 | |
e6c51ed4 | 3736 | /* Post process the headers if necessary. */ |
78245035 | 3737 | (*bed->elf_backend_post_process_headers) (abfd, link_info); |
e6c51ed4 | 3738 | |
d4730f92 BS |
3739 | fsargs.failed = FALSE; |
3740 | fsargs.link_info = link_info; | |
3741 | bfd_map_over_sections (abfd, elf_fake_sections, &fsargs); | |
3742 | if (fsargs.failed) | |
b34976b6 | 3743 | return FALSE; |
252b5132 | 3744 | |
da9f89d4 | 3745 | if (!assign_section_numbers (abfd, link_info)) |
b34976b6 | 3746 | return FALSE; |
252b5132 RH |
3747 | |
3748 | /* The backend linker builds symbol table information itself. */ | |
3516e984 L |
3749 | need_symtab = (link_info == NULL |
3750 | && (bfd_get_symcount (abfd) > 0 | |
3751 | || ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC)) | |
3752 | == HAS_RELOC))); | |
3753 | if (need_symtab) | |
252b5132 RH |
3754 | { |
3755 | /* Non-zero if doing a relocatable link. */ | |
3756 | int relocatable_p = ! (abfd->flags & (EXEC_P | DYNAMIC)); | |
3757 | ||
3758 | if (! swap_out_syms (abfd, &strtab, relocatable_p)) | |
b34976b6 | 3759 | return FALSE; |
252b5132 RH |
3760 | } |
3761 | ||
d4730f92 | 3762 | failed = FALSE; |
1126897b | 3763 | if (link_info == NULL) |
dbb410c3 | 3764 | { |
1126897b | 3765 | bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed); |
dbb410c3 | 3766 | if (failed) |
b34976b6 | 3767 | return FALSE; |
dbb410c3 AM |
3768 | } |
3769 | ||
252b5132 RH |
3770 | shstrtab_hdr = &elf_tdata (abfd)->shstrtab_hdr; |
3771 | /* sh_name was set in prep_headers. */ | |
3772 | shstrtab_hdr->sh_type = SHT_STRTAB; | |
3773 | shstrtab_hdr->sh_flags = 0; | |
3774 | shstrtab_hdr->sh_addr = 0; | |
946748d5 | 3775 | /* sh_size is set in _bfd_elf_assign_file_positions_for_non_load. */ |
252b5132 RH |
3776 | shstrtab_hdr->sh_entsize = 0; |
3777 | shstrtab_hdr->sh_link = 0; | |
3778 | shstrtab_hdr->sh_info = 0; | |
3e19fb8f | 3779 | /* sh_offset is set in _bfd_elf_assign_file_positions_for_non_load. */ |
252b5132 RH |
3780 | shstrtab_hdr->sh_addralign = 1; |
3781 | ||
c84fca4d | 3782 | if (!assign_file_positions_except_relocs (abfd, link_info)) |
b34976b6 | 3783 | return FALSE; |
252b5132 | 3784 | |
3516e984 | 3785 | if (need_symtab) |
252b5132 RH |
3786 | { |
3787 | file_ptr off; | |
3788 | Elf_Internal_Shdr *hdr; | |
3789 | ||
12bd6957 | 3790 | off = elf_next_file_pos (abfd); |
252b5132 RH |
3791 | |
3792 | hdr = &elf_tdata (abfd)->symtab_hdr; | |
b34976b6 | 3793 | off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE); |
252b5132 | 3794 | |
9ad5cbcf AM |
3795 | hdr = &elf_tdata (abfd)->symtab_shndx_hdr; |
3796 | if (hdr->sh_size != 0) | |
b34976b6 | 3797 | off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE); |
9ad5cbcf | 3798 | |
252b5132 | 3799 | hdr = &elf_tdata (abfd)->strtab_hdr; |
b34976b6 | 3800 | off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE); |
252b5132 | 3801 | |
12bd6957 | 3802 | elf_next_file_pos (abfd) = off; |
252b5132 RH |
3803 | |
3804 | /* Now that we know where the .strtab section goes, write it | |
08a40648 | 3805 | out. */ |
252b5132 | 3806 | if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0 |
ef10c3ac | 3807 | || ! _bfd_elf_strtab_emit (abfd, strtab)) |
b34976b6 | 3808 | return FALSE; |
ef10c3ac | 3809 | _bfd_elf_strtab_free (strtab); |
252b5132 RH |
3810 | } |
3811 | ||
b34976b6 | 3812 | abfd->output_has_begun = TRUE; |
252b5132 | 3813 | |
b34976b6 | 3814 | return TRUE; |
252b5132 RH |
3815 | } |
3816 | ||
8ded5a0f AM |
3817 | /* Make an initial estimate of the size of the program header. If we |
3818 | get the number wrong here, we'll redo section placement. */ | |
3819 | ||
3820 | static bfd_size_type | |
3821 | get_program_header_size (bfd *abfd, struct bfd_link_info *info) | |
3822 | { | |
3823 | size_t segs; | |
3824 | asection *s; | |
2b05f1b7 | 3825 | const struct elf_backend_data *bed; |
8ded5a0f AM |
3826 | |
3827 | /* Assume we will need exactly two PT_LOAD segments: one for text | |
3828 | and one for data. */ | |
3829 | segs = 2; | |
3830 | ||
3831 | s = bfd_get_section_by_name (abfd, ".interp"); | |
3832 | if (s != NULL && (s->flags & SEC_LOAD) != 0) | |
3833 | { | |
3834 | /* If we have a loadable interpreter section, we need a | |
3835 | PT_INTERP segment. In this case, assume we also need a | |
3836 | PT_PHDR segment, although that may not be true for all | |
3837 | targets. */ | |
3838 | segs += 2; | |
3839 | } | |
3840 | ||
3841 | if (bfd_get_section_by_name (abfd, ".dynamic") != NULL) | |
3842 | { | |
3843 | /* We need a PT_DYNAMIC segment. */ | |
3844 | ++segs; | |
f210dcff | 3845 | } |
08a40648 | 3846 | |
ceae84aa | 3847 | if (info != NULL && info->relro) |
f210dcff L |
3848 | { |
3849 | /* We need a PT_GNU_RELRO segment. */ | |
3850 | ++segs; | |
8ded5a0f AM |
3851 | } |
3852 | ||
12bd6957 | 3853 | if (elf_eh_frame_hdr (abfd)) |
8ded5a0f AM |
3854 | { |
3855 | /* We need a PT_GNU_EH_FRAME segment. */ | |
3856 | ++segs; | |
3857 | } | |
3858 | ||
12bd6957 | 3859 | if (elf_stack_flags (abfd)) |
8ded5a0f | 3860 | { |
2b05f1b7 L |
3861 | /* We need a PT_GNU_STACK segment. */ |
3862 | ++segs; | |
3863 | } | |
94b11780 | 3864 | |
2b05f1b7 L |
3865 | for (s = abfd->sections; s != NULL; s = s->next) |
3866 | { | |
8ded5a0f | 3867 | if ((s->flags & SEC_LOAD) != 0 |
0112cd26 | 3868 | && CONST_STRNEQ (s->name, ".note")) |
8ded5a0f AM |
3869 | { |
3870 | /* We need a PT_NOTE segment. */ | |
3871 | ++segs; | |
1c5265b5 JJ |
3872 | /* Try to create just one PT_NOTE segment |
3873 | for all adjacent loadable .note* sections. | |
3874 | gABI requires that within a PT_NOTE segment | |
3875 | (and also inside of each SHT_NOTE section) | |
3876 | each note is padded to a multiple of 4 size, | |
3877 | so we check whether the sections are correctly | |
3878 | aligned. */ | |
3879 | if (s->alignment_power == 2) | |
3880 | while (s->next != NULL | |
3881 | && s->next->alignment_power == 2 | |
3882 | && (s->next->flags & SEC_LOAD) != 0 | |
3883 | && CONST_STRNEQ (s->next->name, ".note")) | |
3884 | s = s->next; | |
8ded5a0f AM |
3885 | } |
3886 | } | |
3887 | ||
3888 | for (s = abfd->sections; s != NULL; s = s->next) | |
3889 | { | |
3890 | if (s->flags & SEC_THREAD_LOCAL) | |
3891 | { | |
3892 | /* We need a PT_TLS segment. */ | |
3893 | ++segs; | |
3894 | break; | |
3895 | } | |
3896 | } | |
3897 | ||
3898 | /* Let the backend count up any program headers it might need. */ | |
2b05f1b7 | 3899 | bed = get_elf_backend_data (abfd); |
8ded5a0f AM |
3900 | if (bed->elf_backend_additional_program_headers) |
3901 | { | |
3902 | int a; | |
3903 | ||
3904 | a = (*bed->elf_backend_additional_program_headers) (abfd, info); | |
3905 | if (a == -1) | |
3906 | abort (); | |
3907 | segs += a; | |
3908 | } | |
3909 | ||
3910 | return segs * bed->s->sizeof_phdr; | |
3911 | } | |
3912 | ||
2ea37f1c NC |
3913 | /* Find the segment that contains the output_section of section. */ |
3914 | ||
3915 | Elf_Internal_Phdr * | |
3916 | _bfd_elf_find_segment_containing_section (bfd * abfd, asection * section) | |
3917 | { | |
3918 | struct elf_segment_map *m; | |
3919 | Elf_Internal_Phdr *p; | |
3920 | ||
12bd6957 | 3921 | for (m = elf_seg_map (abfd), p = elf_tdata (abfd)->phdr; |
2ea37f1c NC |
3922 | m != NULL; |
3923 | m = m->next, p++) | |
3924 | { | |
3925 | int i; | |
3926 | ||
3927 | for (i = m->count - 1; i >= 0; i--) | |
3928 | if (m->sections[i] == section) | |
3929 | return p; | |
3930 | } | |
3931 | ||
3932 | return NULL; | |
3933 | } | |
3934 | ||
252b5132 RH |
3935 | /* Create a mapping from a set of sections to a program segment. */ |
3936 | ||
217aa764 AM |
3937 | static struct elf_segment_map * |
3938 | make_mapping (bfd *abfd, | |
3939 | asection **sections, | |
3940 | unsigned int from, | |
3941 | unsigned int to, | |
3942 | bfd_boolean phdr) | |
252b5132 RH |
3943 | { |
3944 | struct elf_segment_map *m; | |
3945 | unsigned int i; | |
3946 | asection **hdrpp; | |
dc810e39 | 3947 | bfd_size_type amt; |
252b5132 | 3948 | |
dc810e39 AM |
3949 | amt = sizeof (struct elf_segment_map); |
3950 | amt += (to - from - 1) * sizeof (asection *); | |
a50b1753 | 3951 | m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); |
252b5132 RH |
3952 | if (m == NULL) |
3953 | return NULL; | |
3954 | m->next = NULL; | |
3955 | m->p_type = PT_LOAD; | |
3956 | for (i = from, hdrpp = sections + from; i < to; i++, hdrpp++) | |
3957 | m->sections[i - from] = *hdrpp; | |
3958 | m->count = to - from; | |
3959 | ||
3960 | if (from == 0 && phdr) | |
3961 | { | |
3962 | /* Include the headers in the first PT_LOAD segment. */ | |
3963 | m->includes_filehdr = 1; | |
3964 | m->includes_phdrs = 1; | |
3965 | } | |
3966 | ||
3967 | return m; | |
3968 | } | |
3969 | ||
229fcec5 MM |
3970 | /* Create the PT_DYNAMIC segment, which includes DYNSEC. Returns NULL |
3971 | on failure. */ | |
3972 | ||
3973 | struct elf_segment_map * | |
3974 | _bfd_elf_make_dynamic_segment (bfd *abfd, asection *dynsec) | |
3975 | { | |
3976 | struct elf_segment_map *m; | |
3977 | ||
a50b1753 NC |
3978 | m = (struct elf_segment_map *) bfd_zalloc (abfd, |
3979 | sizeof (struct elf_segment_map)); | |
229fcec5 MM |
3980 | if (m == NULL) |
3981 | return NULL; | |
3982 | m->next = NULL; | |
3983 | m->p_type = PT_DYNAMIC; | |
3984 | m->count = 1; | |
3985 | m->sections[0] = dynsec; | |
08a40648 | 3986 | |
229fcec5 MM |
3987 | return m; |
3988 | } | |
3989 | ||
8ded5a0f | 3990 | /* Possibly add or remove segments from the segment map. */ |
252b5132 | 3991 | |
b34976b6 | 3992 | static bfd_boolean |
3dea8fca AM |
3993 | elf_modify_segment_map (bfd *abfd, |
3994 | struct bfd_link_info *info, | |
3995 | bfd_boolean remove_empty_load) | |
252b5132 | 3996 | { |
252e386e | 3997 | struct elf_segment_map **m; |
8ded5a0f | 3998 | const struct elf_backend_data *bed; |
252b5132 | 3999 | |
8ded5a0f AM |
4000 | /* The placement algorithm assumes that non allocated sections are |
4001 | not in PT_LOAD segments. We ensure this here by removing such | |
4002 | sections from the segment map. We also remove excluded | |
252e386e AM |
4003 | sections. Finally, any PT_LOAD segment without sections is |
4004 | removed. */ | |
12bd6957 | 4005 | m = &elf_seg_map (abfd); |
252e386e | 4006 | while (*m) |
8ded5a0f AM |
4007 | { |
4008 | unsigned int i, new_count; | |
252b5132 | 4009 | |
252e386e | 4010 | for (new_count = 0, i = 0; i < (*m)->count; i++) |
8ded5a0f | 4011 | { |
252e386e AM |
4012 | if (((*m)->sections[i]->flags & SEC_EXCLUDE) == 0 |
4013 | && (((*m)->sections[i]->flags & SEC_ALLOC) != 0 | |
4014 | || (*m)->p_type != PT_LOAD)) | |
8ded5a0f | 4015 | { |
252e386e AM |
4016 | (*m)->sections[new_count] = (*m)->sections[i]; |
4017 | new_count++; | |
8ded5a0f AM |
4018 | } |
4019 | } | |
252e386e | 4020 | (*m)->count = new_count; |
252b5132 | 4021 | |
3dea8fca | 4022 | if (remove_empty_load && (*m)->p_type == PT_LOAD && (*m)->count == 0) |
252e386e AM |
4023 | *m = (*m)->next; |
4024 | else | |
4025 | m = &(*m)->next; | |
8ded5a0f | 4026 | } |
252b5132 | 4027 | |
8ded5a0f AM |
4028 | bed = get_elf_backend_data (abfd); |
4029 | if (bed->elf_backend_modify_segment_map != NULL) | |
252b5132 | 4030 | { |
252e386e | 4031 | if (!(*bed->elf_backend_modify_segment_map) (abfd, info)) |
8ded5a0f | 4032 | return FALSE; |
252b5132 | 4033 | } |
252b5132 | 4034 | |
8ded5a0f AM |
4035 | return TRUE; |
4036 | } | |
252b5132 | 4037 | |
8ded5a0f | 4038 | /* Set up a mapping from BFD sections to program segments. */ |
252b5132 | 4039 | |
8ded5a0f AM |
4040 | bfd_boolean |
4041 | _bfd_elf_map_sections_to_segments (bfd *abfd, struct bfd_link_info *info) | |
4042 | { | |
4043 | unsigned int count; | |
4044 | struct elf_segment_map *m; | |
4045 | asection **sections = NULL; | |
4046 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
3dea8fca | 4047 | bfd_boolean no_user_phdrs; |
252b5132 | 4048 | |
12bd6957 | 4049 | no_user_phdrs = elf_seg_map (abfd) == NULL; |
d324f6d6 RM |
4050 | |
4051 | if (info != NULL) | |
4052 | info->user_phdrs = !no_user_phdrs; | |
4053 | ||
3dea8fca | 4054 | if (no_user_phdrs && bfd_count_sections (abfd) != 0) |
252b5132 | 4055 | { |
8ded5a0f AM |
4056 | asection *s; |
4057 | unsigned int i; | |
4058 | struct elf_segment_map *mfirst; | |
4059 | struct elf_segment_map **pm; | |
4060 | asection *last_hdr; | |
4061 | bfd_vma last_size; | |
4062 | unsigned int phdr_index; | |
4063 | bfd_vma maxpagesize; | |
4064 | asection **hdrpp; | |
4065 | bfd_boolean phdr_in_segment = TRUE; | |
4066 | bfd_boolean writable; | |
4067 | int tls_count = 0; | |
4068 | asection *first_tls = NULL; | |
4069 | asection *dynsec, *eh_frame_hdr; | |
4070 | bfd_size_type amt; | |
8d06853e | 4071 | bfd_vma addr_mask, wrap_to = 0; |
252b5132 | 4072 | |
8ded5a0f | 4073 | /* Select the allocated sections, and sort them. */ |
252b5132 | 4074 | |
a50b1753 NC |
4075 | sections = (asection **) bfd_malloc2 (bfd_count_sections (abfd), |
4076 | sizeof (asection *)); | |
8ded5a0f | 4077 | if (sections == NULL) |
252b5132 | 4078 | goto error_return; |
252b5132 | 4079 | |
8d06853e AM |
4080 | /* Calculate top address, avoiding undefined behaviour of shift |
4081 | left operator when shift count is equal to size of type | |
4082 | being shifted. */ | |
4083 | addr_mask = ((bfd_vma) 1 << (bfd_arch_bits_per_address (abfd) - 1)) - 1; | |
4084 | addr_mask = (addr_mask << 1) + 1; | |
4085 | ||
8ded5a0f AM |
4086 | i = 0; |
4087 | for (s = abfd->sections; s != NULL; s = s->next) | |
4088 | { | |
4089 | if ((s->flags & SEC_ALLOC) != 0) | |
4090 | { | |
4091 | sections[i] = s; | |
4092 | ++i; | |
8d06853e AM |
4093 | /* A wrapping section potentially clashes with header. */ |
4094 | if (((s->lma + s->size) & addr_mask) < (s->lma & addr_mask)) | |
4095 | wrap_to = (s->lma + s->size) & addr_mask; | |
8ded5a0f AM |
4096 | } |
4097 | } | |
4098 | BFD_ASSERT (i <= bfd_count_sections (abfd)); | |
4099 | count = i; | |
252b5132 | 4100 | |
8ded5a0f | 4101 | qsort (sections, (size_t) count, sizeof (asection *), elf_sort_sections); |
252b5132 | 4102 | |
8ded5a0f | 4103 | /* Build the mapping. */ |
252b5132 | 4104 | |
8ded5a0f AM |
4105 | mfirst = NULL; |
4106 | pm = &mfirst; | |
252b5132 | 4107 | |
8ded5a0f AM |
4108 | /* If we have a .interp section, then create a PT_PHDR segment for |
4109 | the program headers and a PT_INTERP segment for the .interp | |
4110 | section. */ | |
4111 | s = bfd_get_section_by_name (abfd, ".interp"); | |
4112 | if (s != NULL && (s->flags & SEC_LOAD) != 0) | |
4113 | { | |
4114 | amt = sizeof (struct elf_segment_map); | |
a50b1753 | 4115 | m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); |
8ded5a0f AM |
4116 | if (m == NULL) |
4117 | goto error_return; | |
4118 | m->next = NULL; | |
4119 | m->p_type = PT_PHDR; | |
4120 | /* FIXME: UnixWare and Solaris set PF_X, Irix 5 does not. */ | |
4121 | m->p_flags = PF_R | PF_X; | |
4122 | m->p_flags_valid = 1; | |
4123 | m->includes_phdrs = 1; | |
252b5132 | 4124 | |
8ded5a0f AM |
4125 | *pm = m; |
4126 | pm = &m->next; | |
252b5132 | 4127 | |
8ded5a0f | 4128 | amt = sizeof (struct elf_segment_map); |
a50b1753 | 4129 | m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); |
8ded5a0f AM |
4130 | if (m == NULL) |
4131 | goto error_return; | |
4132 | m->next = NULL; | |
4133 | m->p_type = PT_INTERP; | |
4134 | m->count = 1; | |
4135 | m->sections[0] = s; | |
4136 | ||
4137 | *pm = m; | |
4138 | pm = &m->next; | |
252b5132 | 4139 | } |
8ded5a0f AM |
4140 | |
4141 | /* Look through the sections. We put sections in the same program | |
4142 | segment when the start of the second section can be placed within | |
4143 | a few bytes of the end of the first section. */ | |
4144 | last_hdr = NULL; | |
4145 | last_size = 0; | |
4146 | phdr_index = 0; | |
4147 | maxpagesize = bed->maxpagesize; | |
063bb025 NC |
4148 | /* PR 17512: file: c8455299. |
4149 | Avoid divide-by-zero errors later on. | |
4150 | FIXME: Should we abort if the maxpagesize is zero ? */ | |
4151 | if (maxpagesize == 0) | |
4152 | maxpagesize = 1; | |
8ded5a0f AM |
4153 | writable = FALSE; |
4154 | dynsec = bfd_get_section_by_name (abfd, ".dynamic"); | |
4155 | if (dynsec != NULL | |
4156 | && (dynsec->flags & SEC_LOAD) == 0) | |
4157 | dynsec = NULL; | |
4158 | ||
4159 | /* Deal with -Ttext or something similar such that the first section | |
4160 | is not adjacent to the program headers. This is an | |
4161 | approximation, since at this point we don't know exactly how many | |
4162 | program headers we will need. */ | |
4163 | if (count > 0) | |
252b5132 | 4164 | { |
12bd6957 | 4165 | bfd_size_type phdr_size = elf_program_header_size (abfd); |
8ded5a0f | 4166 | |
62d7a5f6 | 4167 | if (phdr_size == (bfd_size_type) -1) |
8ded5a0f | 4168 | phdr_size = get_program_header_size (abfd, info); |
d2bcb0d1 | 4169 | phdr_size += bed->s->sizeof_ehdr; |
8ded5a0f | 4170 | if ((abfd->flags & D_PAGED) == 0 |
8d06853e AM |
4171 | || (sections[0]->lma & addr_mask) < phdr_size |
4172 | || ((sections[0]->lma & addr_mask) % maxpagesize | |
4173 | < phdr_size % maxpagesize) | |
4174 | || (sections[0]->lma & addr_mask & -maxpagesize) < wrap_to) | |
8ded5a0f | 4175 | phdr_in_segment = FALSE; |
252b5132 RH |
4176 | } |
4177 | ||
8ded5a0f | 4178 | for (i = 0, hdrpp = sections; i < count; i++, hdrpp++) |
252b5132 | 4179 | { |
8ded5a0f AM |
4180 | asection *hdr; |
4181 | bfd_boolean new_segment; | |
4182 | ||
4183 | hdr = *hdrpp; | |
4184 | ||
4185 | /* See if this section and the last one will fit in the same | |
4186 | segment. */ | |
4187 | ||
4188 | if (last_hdr == NULL) | |
4189 | { | |
4190 | /* If we don't have a segment yet, then we don't need a new | |
4191 | one (we build the last one after this loop). */ | |
4192 | new_segment = FALSE; | |
4193 | } | |
4194 | else if (last_hdr->lma - last_hdr->vma != hdr->lma - hdr->vma) | |
4195 | { | |
4196 | /* If this section has a different relation between the | |
4197 | virtual address and the load address, then we need a new | |
4198 | segment. */ | |
4199 | new_segment = TRUE; | |
4200 | } | |
b5599592 AM |
4201 | else if (hdr->lma < last_hdr->lma + last_size |
4202 | || last_hdr->lma + last_size < last_hdr->lma) | |
4203 | { | |
4204 | /* If this section has a load address that makes it overlap | |
4205 | the previous section, then we need a new segment. */ | |
4206 | new_segment = TRUE; | |
4207 | } | |
39948a60 NC |
4208 | /* In the next test we have to be careful when last_hdr->lma is close |
4209 | to the end of the address space. If the aligned address wraps | |
4210 | around to the start of the address space, then there are no more | |
4211 | pages left in memory and it is OK to assume that the current | |
4212 | section can be included in the current segment. */ | |
4213 | else if ((BFD_ALIGN (last_hdr->lma + last_size, maxpagesize) + maxpagesize | |
4214 | > last_hdr->lma) | |
4215 | && (BFD_ALIGN (last_hdr->lma + last_size, maxpagesize) + maxpagesize | |
4ff73856 | 4216 | <= hdr->lma)) |
8ded5a0f AM |
4217 | { |
4218 | /* If putting this section in this segment would force us to | |
4219 | skip a page in the segment, then we need a new segment. */ | |
4220 | new_segment = TRUE; | |
4221 | } | |
4222 | else if ((last_hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0 | |
4223 | && (hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) != 0) | |
4224 | { | |
4225 | /* We don't want to put a loadable section after a | |
4226 | nonloadable section in the same segment. | |
4227 | Consider .tbss sections as loadable for this purpose. */ | |
4228 | new_segment = TRUE; | |
4229 | } | |
4230 | else if ((abfd->flags & D_PAGED) == 0) | |
4231 | { | |
4232 | /* If the file is not demand paged, which means that we | |
4233 | don't require the sections to be correctly aligned in the | |
4234 | file, then there is no other reason for a new segment. */ | |
4235 | new_segment = FALSE; | |
4236 | } | |
4237 | else if (! writable | |
4238 | && (hdr->flags & SEC_READONLY) == 0 | |
8d06853e AM |
4239 | && (((last_hdr->lma + last_size - 1) & -maxpagesize) |
4240 | != (hdr->lma & -maxpagesize))) | |
8ded5a0f AM |
4241 | { |
4242 | /* We don't want to put a writable section in a read only | |
4243 | segment, unless they are on the same page in memory | |
4244 | anyhow. We already know that the last section does not | |
4245 | bring us past the current section on the page, so the | |
4246 | only case in which the new section is not on the same | |
4247 | page as the previous section is when the previous section | |
4248 | ends precisely on a page boundary. */ | |
4249 | new_segment = TRUE; | |
4250 | } | |
4251 | else | |
4252 | { | |
4253 | /* Otherwise, we can use the same segment. */ | |
4254 | new_segment = FALSE; | |
4255 | } | |
4256 | ||
2889e75b | 4257 | /* Allow interested parties a chance to override our decision. */ |
ceae84aa AM |
4258 | if (last_hdr != NULL |
4259 | && info != NULL | |
4260 | && info->callbacks->override_segment_assignment != NULL) | |
4261 | new_segment | |
4262 | = info->callbacks->override_segment_assignment (info, abfd, hdr, | |
4263 | last_hdr, | |
4264 | new_segment); | |
2889e75b | 4265 | |
8ded5a0f AM |
4266 | if (! new_segment) |
4267 | { | |
4268 | if ((hdr->flags & SEC_READONLY) == 0) | |
4269 | writable = TRUE; | |
4270 | last_hdr = hdr; | |
4271 | /* .tbss sections effectively have zero size. */ | |
4272 | if ((hdr->flags & (SEC_THREAD_LOCAL | SEC_LOAD)) | |
4273 | != SEC_THREAD_LOCAL) | |
4274 | last_size = hdr->size; | |
4275 | else | |
4276 | last_size = 0; | |
4277 | continue; | |
4278 | } | |
4279 | ||
4280 | /* We need a new program segment. We must create a new program | |
4281 | header holding all the sections from phdr_index until hdr. */ | |
4282 | ||
4283 | m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment); | |
4284 | if (m == NULL) | |
4285 | goto error_return; | |
4286 | ||
4287 | *pm = m; | |
4288 | pm = &m->next; | |
4289 | ||
252b5132 | 4290 | if ((hdr->flags & SEC_READONLY) == 0) |
b34976b6 | 4291 | writable = TRUE; |
8ded5a0f AM |
4292 | else |
4293 | writable = FALSE; | |
4294 | ||
baaff79e JJ |
4295 | last_hdr = hdr; |
4296 | /* .tbss sections effectively have zero size. */ | |
e5caec89 | 4297 | if ((hdr->flags & (SEC_THREAD_LOCAL | SEC_LOAD)) != SEC_THREAD_LOCAL) |
eea6121a | 4298 | last_size = hdr->size; |
baaff79e JJ |
4299 | else |
4300 | last_size = 0; | |
8ded5a0f AM |
4301 | phdr_index = i; |
4302 | phdr_in_segment = FALSE; | |
252b5132 RH |
4303 | } |
4304 | ||
86b2281f AM |
4305 | /* Create a final PT_LOAD program segment, but not if it's just |
4306 | for .tbss. */ | |
4307 | if (last_hdr != NULL | |
4308 | && (i - phdr_index != 1 | |
4309 | || ((last_hdr->flags & (SEC_THREAD_LOCAL | SEC_LOAD)) | |
4310 | != SEC_THREAD_LOCAL))) | |
8ded5a0f AM |
4311 | { |
4312 | m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment); | |
4313 | if (m == NULL) | |
4314 | goto error_return; | |
252b5132 | 4315 | |
8ded5a0f AM |
4316 | *pm = m; |
4317 | pm = &m->next; | |
4318 | } | |
252b5132 | 4319 | |
8ded5a0f AM |
4320 | /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */ |
4321 | if (dynsec != NULL) | |
4322 | { | |
4323 | m = _bfd_elf_make_dynamic_segment (abfd, dynsec); | |
4324 | if (m == NULL) | |
4325 | goto error_return; | |
4326 | *pm = m; | |
4327 | pm = &m->next; | |
4328 | } | |
252b5132 | 4329 | |
1c5265b5 JJ |
4330 | /* For each batch of consecutive loadable .note sections, |
4331 | add a PT_NOTE segment. We don't use bfd_get_section_by_name, | |
4332 | because if we link together nonloadable .note sections and | |
4333 | loadable .note sections, we will generate two .note sections | |
4334 | in the output file. FIXME: Using names for section types is | |
4335 | bogus anyhow. */ | |
8ded5a0f AM |
4336 | for (s = abfd->sections; s != NULL; s = s->next) |
4337 | { | |
4338 | if ((s->flags & SEC_LOAD) != 0 | |
0112cd26 | 4339 | && CONST_STRNEQ (s->name, ".note")) |
8ded5a0f | 4340 | { |
1c5265b5 | 4341 | asection *s2; |
91d6fa6a NC |
4342 | |
4343 | count = 1; | |
8ded5a0f | 4344 | amt = sizeof (struct elf_segment_map); |
1c5265b5 JJ |
4345 | if (s->alignment_power == 2) |
4346 | for (s2 = s; s2->next != NULL; s2 = s2->next) | |
55b581a6 JJ |
4347 | { |
4348 | if (s2->next->alignment_power == 2 | |
4349 | && (s2->next->flags & SEC_LOAD) != 0 | |
4350 | && CONST_STRNEQ (s2->next->name, ".note") | |
8d06853e AM |
4351 | && align_power (s2->lma + s2->size, 2) |
4352 | == s2->next->lma) | |
55b581a6 JJ |
4353 | count++; |
4354 | else | |
4355 | break; | |
4356 | } | |
1c5265b5 | 4357 | amt += (count - 1) * sizeof (asection *); |
a50b1753 | 4358 | m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); |
8ded5a0f AM |
4359 | if (m == NULL) |
4360 | goto error_return; | |
4361 | m->next = NULL; | |
4362 | m->p_type = PT_NOTE; | |
1c5265b5 JJ |
4363 | m->count = count; |
4364 | while (count > 1) | |
4365 | { | |
4366 | m->sections[m->count - count--] = s; | |
4367 | BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0); | |
4368 | s = s->next; | |
4369 | } | |
4370 | m->sections[m->count - 1] = s; | |
4371 | BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0); | |
8ded5a0f AM |
4372 | *pm = m; |
4373 | pm = &m->next; | |
4374 | } | |
4375 | if (s->flags & SEC_THREAD_LOCAL) | |
4376 | { | |
4377 | if (! tls_count) | |
4378 | first_tls = s; | |
4379 | tls_count++; | |
4380 | } | |
4381 | } | |
252b5132 | 4382 | |
8ded5a0f AM |
4383 | /* If there are any SHF_TLS output sections, add PT_TLS segment. */ |
4384 | if (tls_count > 0) | |
4385 | { | |
8ded5a0f AM |
4386 | amt = sizeof (struct elf_segment_map); |
4387 | amt += (tls_count - 1) * sizeof (asection *); | |
a50b1753 | 4388 | m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); |
8ded5a0f AM |
4389 | if (m == NULL) |
4390 | goto error_return; | |
4391 | m->next = NULL; | |
4392 | m->p_type = PT_TLS; | |
4393 | m->count = tls_count; | |
4394 | /* Mandated PF_R. */ | |
4395 | m->p_flags = PF_R; | |
4396 | m->p_flags_valid = 1; | |
d923cae0 | 4397 | s = first_tls; |
91d6fa6a | 4398 | for (i = 0; i < (unsigned int) tls_count; ++i) |
8ded5a0f | 4399 | { |
d923cae0 L |
4400 | if ((s->flags & SEC_THREAD_LOCAL) == 0) |
4401 | { | |
4402 | _bfd_error_handler | |
4403 | (_("%B: TLS sections are not adjacent:"), abfd); | |
4404 | s = first_tls; | |
4405 | i = 0; | |
4406 | while (i < (unsigned int) tls_count) | |
4407 | { | |
4408 | if ((s->flags & SEC_THREAD_LOCAL) != 0) | |
4409 | { | |
4410 | _bfd_error_handler (_(" TLS: %A"), s); | |
4411 | i++; | |
4412 | } | |
4413 | else | |
4414 | _bfd_error_handler (_(" non-TLS: %A"), s); | |
4415 | s = s->next; | |
4416 | } | |
4417 | bfd_set_error (bfd_error_bad_value); | |
4418 | goto error_return; | |
4419 | } | |
4420 | m->sections[i] = s; | |
4421 | s = s->next; | |
8ded5a0f | 4422 | } |
252b5132 | 4423 | |
8ded5a0f AM |
4424 | *pm = m; |
4425 | pm = &m->next; | |
4426 | } | |
252b5132 | 4427 | |
8ded5a0f AM |
4428 | /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME |
4429 | segment. */ | |
12bd6957 | 4430 | eh_frame_hdr = elf_eh_frame_hdr (abfd); |
8ded5a0f AM |
4431 | if (eh_frame_hdr != NULL |
4432 | && (eh_frame_hdr->output_section->flags & SEC_LOAD) != 0) | |
252b5132 | 4433 | { |
dc810e39 | 4434 | amt = sizeof (struct elf_segment_map); |
a50b1753 | 4435 | m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); |
252b5132 RH |
4436 | if (m == NULL) |
4437 | goto error_return; | |
4438 | m->next = NULL; | |
8ded5a0f | 4439 | m->p_type = PT_GNU_EH_FRAME; |
252b5132 | 4440 | m->count = 1; |
8ded5a0f | 4441 | m->sections[0] = eh_frame_hdr->output_section; |
252b5132 RH |
4442 | |
4443 | *pm = m; | |
4444 | pm = &m->next; | |
4445 | } | |
13ae64f3 | 4446 | |
12bd6957 | 4447 | if (elf_stack_flags (abfd)) |
13ae64f3 | 4448 | { |
8ded5a0f | 4449 | amt = sizeof (struct elf_segment_map); |
a50b1753 | 4450 | m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); |
8ded5a0f AM |
4451 | if (m == NULL) |
4452 | goto error_return; | |
4453 | m->next = NULL; | |
2b05f1b7 | 4454 | m->p_type = PT_GNU_STACK; |
12bd6957 | 4455 | m->p_flags = elf_stack_flags (abfd); |
04c3a755 | 4456 | m->p_align = bed->stack_align; |
8ded5a0f | 4457 | m->p_flags_valid = 1; |
04c3a755 NS |
4458 | m->p_align_valid = m->p_align != 0; |
4459 | if (info->stacksize > 0) | |
4460 | { | |
4461 | m->p_size = info->stacksize; | |
4462 | m->p_size_valid = 1; | |
4463 | } | |
252b5132 | 4464 | |
8ded5a0f AM |
4465 | *pm = m; |
4466 | pm = &m->next; | |
4467 | } | |
65765700 | 4468 | |
ceae84aa | 4469 | if (info != NULL && info->relro) |
8ded5a0f | 4470 | { |
f210dcff L |
4471 | for (m = mfirst; m != NULL; m = m->next) |
4472 | { | |
3832a4d8 AM |
4473 | if (m->p_type == PT_LOAD |
4474 | && m->count != 0 | |
4475 | && m->sections[0]->vma >= info->relro_start | |
4476 | && m->sections[0]->vma < info->relro_end) | |
f210dcff | 4477 | { |
3832a4d8 AM |
4478 | i = m->count; |
4479 | while (--i != (unsigned) -1) | |
4480 | if ((m->sections[i]->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) | |
4481 | == (SEC_LOAD | SEC_HAS_CONTENTS)) | |
4482 | break; | |
4483 | ||
43a8475c | 4484 | if (i != (unsigned) -1) |
f210dcff L |
4485 | break; |
4486 | } | |
be01b344 | 4487 | } |
f210dcff L |
4488 | |
4489 | /* Make a PT_GNU_RELRO segment only when it isn't empty. */ | |
4490 | if (m != NULL) | |
4491 | { | |
4492 | amt = sizeof (struct elf_segment_map); | |
a50b1753 | 4493 | m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); |
f210dcff L |
4494 | if (m == NULL) |
4495 | goto error_return; | |
4496 | m->next = NULL; | |
4497 | m->p_type = PT_GNU_RELRO; | |
4498 | m->p_flags = PF_R; | |
4499 | m->p_flags_valid = 1; | |
4500 | ||
4501 | *pm = m; | |
4502 | pm = &m->next; | |
4503 | } | |
8ded5a0f | 4504 | } |
9ee5e499 | 4505 | |
8ded5a0f | 4506 | free (sections); |
12bd6957 | 4507 | elf_seg_map (abfd) = mfirst; |
9ee5e499 JJ |
4508 | } |
4509 | ||
3dea8fca | 4510 | if (!elf_modify_segment_map (abfd, info, no_user_phdrs)) |
8ded5a0f | 4511 | return FALSE; |
8c37241b | 4512 | |
12bd6957 | 4513 | for (count = 0, m = elf_seg_map (abfd); m != NULL; m = m->next) |
8ded5a0f | 4514 | ++count; |
12bd6957 | 4515 | elf_program_header_size (abfd) = count * bed->s->sizeof_phdr; |
252b5132 | 4516 | |
b34976b6 | 4517 | return TRUE; |
252b5132 RH |
4518 | |
4519 | error_return: | |
4520 | if (sections != NULL) | |
4521 | free (sections); | |
b34976b6 | 4522 | return FALSE; |
252b5132 RH |
4523 | } |
4524 | ||
4525 | /* Sort sections by address. */ | |
4526 | ||
4527 | static int | |
217aa764 | 4528 | elf_sort_sections (const void *arg1, const void *arg2) |
252b5132 RH |
4529 | { |
4530 | const asection *sec1 = *(const asection **) arg1; | |
4531 | const asection *sec2 = *(const asection **) arg2; | |
eecdbe52 | 4532 | bfd_size_type size1, size2; |
252b5132 RH |
4533 | |
4534 | /* Sort by LMA first, since this is the address used to | |
4535 | place the section into a segment. */ | |
4536 | if (sec1->lma < sec2->lma) | |
4537 | return -1; | |
4538 | else if (sec1->lma > sec2->lma) | |
4539 | return 1; | |
4540 | ||
4541 | /* Then sort by VMA. Normally the LMA and the VMA will be | |
4542 | the same, and this will do nothing. */ | |
4543 | if (sec1->vma < sec2->vma) | |
4544 | return -1; | |
4545 | else if (sec1->vma > sec2->vma) | |
4546 | return 1; | |
4547 | ||
4548 | /* Put !SEC_LOAD sections after SEC_LOAD ones. */ | |
4549 | ||
07c6e936 | 4550 | #define TOEND(x) (((x)->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0) |
252b5132 RH |
4551 | |
4552 | if (TOEND (sec1)) | |
4553 | { | |
4554 | if (TOEND (sec2)) | |
00a7cdc5 NC |
4555 | { |
4556 | /* If the indicies are the same, do not return 0 | |
4557 | here, but continue to try the next comparison. */ | |
4558 | if (sec1->target_index - sec2->target_index != 0) | |
4559 | return sec1->target_index - sec2->target_index; | |
4560 | } | |
252b5132 RH |
4561 | else |
4562 | return 1; | |
4563 | } | |
00a7cdc5 | 4564 | else if (TOEND (sec2)) |
252b5132 RH |
4565 | return -1; |
4566 | ||
4567 | #undef TOEND | |
4568 | ||
00a7cdc5 NC |
4569 | /* Sort by size, to put zero sized sections |
4570 | before others at the same address. */ | |
252b5132 | 4571 | |
eea6121a AM |
4572 | size1 = (sec1->flags & SEC_LOAD) ? sec1->size : 0; |
4573 | size2 = (sec2->flags & SEC_LOAD) ? sec2->size : 0; | |
eecdbe52 JJ |
4574 | |
4575 | if (size1 < size2) | |
252b5132 | 4576 | return -1; |
eecdbe52 | 4577 | if (size1 > size2) |
252b5132 RH |
4578 | return 1; |
4579 | ||
4580 | return sec1->target_index - sec2->target_index; | |
4581 | } | |
4582 | ||
340b6d91 AC |
4583 | /* Ian Lance Taylor writes: |
4584 | ||
4585 | We shouldn't be using % with a negative signed number. That's just | |
4586 | not good. We have to make sure either that the number is not | |
4587 | negative, or that the number has an unsigned type. When the types | |
4588 | are all the same size they wind up as unsigned. When file_ptr is a | |
4589 | larger signed type, the arithmetic winds up as signed long long, | |
4590 | which is wrong. | |
4591 | ||
4592 | What we're trying to say here is something like ``increase OFF by | |
4593 | the least amount that will cause it to be equal to the VMA modulo | |
4594 | the page size.'' */ | |
4595 | /* In other words, something like: | |
4596 | ||
4597 | vma_offset = m->sections[0]->vma % bed->maxpagesize; | |
4598 | off_offset = off % bed->maxpagesize; | |
4599 | if (vma_offset < off_offset) | |
4600 | adjustment = vma_offset + bed->maxpagesize - off_offset; | |
4601 | else | |
4602 | adjustment = vma_offset - off_offset; | |
08a40648 | 4603 | |
340b6d91 AC |
4604 | which can can be collapsed into the expression below. */ |
4605 | ||
4606 | static file_ptr | |
4607 | vma_page_aligned_bias (bfd_vma vma, ufile_ptr off, bfd_vma maxpagesize) | |
4608 | { | |
dc9155b2 NC |
4609 | /* PR binutils/16199: Handle an alignment of zero. */ |
4610 | if (maxpagesize == 0) | |
4611 | maxpagesize = 1; | |
340b6d91 AC |
4612 | return ((vma - off) % maxpagesize); |
4613 | } | |
4614 | ||
6d33f217 L |
4615 | static void |
4616 | print_segment_map (const struct elf_segment_map *m) | |
4617 | { | |
4618 | unsigned int j; | |
4619 | const char *pt = get_segment_type (m->p_type); | |
4620 | char buf[32]; | |
4621 | ||
4622 | if (pt == NULL) | |
4623 | { | |
4624 | if (m->p_type >= PT_LOPROC && m->p_type <= PT_HIPROC) | |
4625 | sprintf (buf, "LOPROC+%7.7x", | |
4626 | (unsigned int) (m->p_type - PT_LOPROC)); | |
4627 | else if (m->p_type >= PT_LOOS && m->p_type <= PT_HIOS) | |
4628 | sprintf (buf, "LOOS+%7.7x", | |
4629 | (unsigned int) (m->p_type - PT_LOOS)); | |
4630 | else | |
4631 | snprintf (buf, sizeof (buf), "%8.8x", | |
4632 | (unsigned int) m->p_type); | |
4633 | pt = buf; | |
4634 | } | |
4a97a0e5 | 4635 | fflush (stdout); |
6d33f217 L |
4636 | fprintf (stderr, "%s:", pt); |
4637 | for (j = 0; j < m->count; j++) | |
4638 | fprintf (stderr, " %s", m->sections [j]->name); | |
4639 | putc ('\n',stderr); | |
4a97a0e5 | 4640 | fflush (stderr); |
6d33f217 L |
4641 | } |
4642 | ||
32812159 AM |
4643 | static bfd_boolean |
4644 | write_zeros (bfd *abfd, file_ptr pos, bfd_size_type len) | |
4645 | { | |
4646 | void *buf; | |
4647 | bfd_boolean ret; | |
4648 | ||
4649 | if (bfd_seek (abfd, pos, SEEK_SET) != 0) | |
4650 | return FALSE; | |
4651 | buf = bfd_zmalloc (len); | |
4652 | if (buf == NULL) | |
4653 | return FALSE; | |
4654 | ret = bfd_bwrite (buf, len, abfd) == len; | |
4655 | free (buf); | |
4656 | return ret; | |
4657 | } | |
4658 | ||
252b5132 RH |
4659 | /* Assign file positions to the sections based on the mapping from |
4660 | sections to segments. This function also sets up some fields in | |
f3520d2f | 4661 | the file header. */ |
252b5132 | 4662 | |
b34976b6 | 4663 | static bfd_boolean |
f3520d2f AM |
4664 | assign_file_positions_for_load_sections (bfd *abfd, |
4665 | struct bfd_link_info *link_info) | |
252b5132 RH |
4666 | { |
4667 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
252b5132 | 4668 | struct elf_segment_map *m; |
252b5132 | 4669 | Elf_Internal_Phdr *phdrs; |
252b5132 | 4670 | Elf_Internal_Phdr *p; |
02bf8d82 | 4671 | file_ptr off; |
3f570048 | 4672 | bfd_size_type maxpagesize; |
f3520d2f | 4673 | unsigned int alloc; |
0920dee7 | 4674 | unsigned int i, j; |
2b0bc088 | 4675 | bfd_vma header_pad = 0; |
252b5132 | 4676 | |
e36284ab | 4677 | if (link_info == NULL |
ceae84aa | 4678 | && !_bfd_elf_map_sections_to_segments (abfd, link_info)) |
8ded5a0f | 4679 | return FALSE; |
252b5132 | 4680 | |
8ded5a0f | 4681 | alloc = 0; |
12bd6957 | 4682 | for (m = elf_seg_map (abfd); m != NULL; m = m->next) |
2b0bc088 NC |
4683 | { |
4684 | ++alloc; | |
4685 | if (m->header_size) | |
4686 | header_pad = m->header_size; | |
4687 | } | |
252b5132 | 4688 | |
82f2dbf7 NC |
4689 | if (alloc) |
4690 | { | |
4691 | elf_elfheader (abfd)->e_phoff = bed->s->sizeof_ehdr; | |
4692 | elf_elfheader (abfd)->e_phentsize = bed->s->sizeof_phdr; | |
4693 | } | |
4694 | else | |
4695 | { | |
4696 | /* PR binutils/12467. */ | |
4697 | elf_elfheader (abfd)->e_phoff = 0; | |
4698 | elf_elfheader (abfd)->e_phentsize = 0; | |
4699 | } | |
d324f6d6 | 4700 | |
8ded5a0f | 4701 | elf_elfheader (abfd)->e_phnum = alloc; |
252b5132 | 4702 | |
12bd6957 AM |
4703 | if (elf_program_header_size (abfd) == (bfd_size_type) -1) |
4704 | elf_program_header_size (abfd) = alloc * bed->s->sizeof_phdr; | |
8ded5a0f | 4705 | else |
12bd6957 | 4706 | BFD_ASSERT (elf_program_header_size (abfd) |
59e0647f | 4707 | >= alloc * bed->s->sizeof_phdr); |
252b5132 RH |
4708 | |
4709 | if (alloc == 0) | |
f3520d2f | 4710 | { |
12bd6957 | 4711 | elf_next_file_pos (abfd) = bed->s->sizeof_ehdr; |
8ded5a0f | 4712 | return TRUE; |
f3520d2f | 4713 | } |
252b5132 | 4714 | |
12bd6957 | 4715 | /* We're writing the size in elf_program_header_size (abfd), |
57268894 HPN |
4716 | see assign_file_positions_except_relocs, so make sure we have |
4717 | that amount allocated, with trailing space cleared. | |
12bd6957 AM |
4718 | The variable alloc contains the computed need, while |
4719 | elf_program_header_size (abfd) contains the size used for the | |
57268894 HPN |
4720 | layout. |
4721 | See ld/emultempl/elf-generic.em:gld${EMULATION_NAME}_map_segments | |
4722 | where the layout is forced to according to a larger size in the | |
4723 | last iterations for the testcase ld-elf/header. */ | |
12bd6957 | 4724 | BFD_ASSERT (elf_program_header_size (abfd) % bed->s->sizeof_phdr |
57268894 | 4725 | == 0); |
a50b1753 NC |
4726 | phdrs = (Elf_Internal_Phdr *) |
4727 | bfd_zalloc2 (abfd, | |
12bd6957 | 4728 | (elf_program_header_size (abfd) / bed->s->sizeof_phdr), |
a50b1753 | 4729 | sizeof (Elf_Internal_Phdr)); |
f3520d2f | 4730 | elf_tdata (abfd)->phdr = phdrs; |
252b5132 | 4731 | if (phdrs == NULL) |
b34976b6 | 4732 | return FALSE; |
252b5132 | 4733 | |
3f570048 AM |
4734 | maxpagesize = 1; |
4735 | if ((abfd->flags & D_PAGED) != 0) | |
4736 | maxpagesize = bed->maxpagesize; | |
4737 | ||
252b5132 RH |
4738 | off = bed->s->sizeof_ehdr; |
4739 | off += alloc * bed->s->sizeof_phdr; | |
2b0bc088 NC |
4740 | if (header_pad < (bfd_vma) off) |
4741 | header_pad = 0; | |
4742 | else | |
4743 | header_pad -= off; | |
4744 | off += header_pad; | |
252b5132 | 4745 | |
12bd6957 | 4746 | for (m = elf_seg_map (abfd), p = phdrs, j = 0; |
252b5132 | 4747 | m != NULL; |
0920dee7 | 4748 | m = m->next, p++, j++) |
252b5132 | 4749 | { |
252b5132 | 4750 | asection **secpp; |
bf988460 AM |
4751 | bfd_vma off_adjust; |
4752 | bfd_boolean no_contents; | |
252b5132 RH |
4753 | |
4754 | /* If elf_segment_map is not from map_sections_to_segments, the | |
08a40648 | 4755 | sections may not be correctly ordered. NOTE: sorting should |
52e9b619 MS |
4756 | not be done to the PT_NOTE section of a corefile, which may |
4757 | contain several pseudo-sections artificially created by bfd. | |
4758 | Sorting these pseudo-sections breaks things badly. */ | |
47d9a591 AM |
4759 | if (m->count > 1 |
4760 | && !(elf_elfheader (abfd)->e_type == ET_CORE | |
52e9b619 | 4761 | && m->p_type == PT_NOTE)) |
252b5132 RH |
4762 | qsort (m->sections, (size_t) m->count, sizeof (asection *), |
4763 | elf_sort_sections); | |
4764 | ||
b301b248 AM |
4765 | /* An ELF segment (described by Elf_Internal_Phdr) may contain a |
4766 | number of sections with contents contributing to both p_filesz | |
4767 | and p_memsz, followed by a number of sections with no contents | |
4768 | that just contribute to p_memsz. In this loop, OFF tracks next | |
02bf8d82 | 4769 | available file offset for PT_LOAD and PT_NOTE segments. */ |
252b5132 | 4770 | p->p_type = m->p_type; |
28a7f3e7 | 4771 | p->p_flags = m->p_flags; |
252b5132 | 4772 | |
3f570048 AM |
4773 | if (m->count == 0) |
4774 | p->p_vaddr = 0; | |
4775 | else | |
3271a814 | 4776 | p->p_vaddr = m->sections[0]->vma - m->p_vaddr_offset; |
3f570048 AM |
4777 | |
4778 | if (m->p_paddr_valid) | |
4779 | p->p_paddr = m->p_paddr; | |
4780 | else if (m->count == 0) | |
4781 | p->p_paddr = 0; | |
4782 | else | |
08a40648 | 4783 | p->p_paddr = m->sections[0]->lma - m->p_vaddr_offset; |
3f570048 AM |
4784 | |
4785 | if (p->p_type == PT_LOAD | |
4786 | && (abfd->flags & D_PAGED) != 0) | |
4787 | { | |
4788 | /* p_align in demand paged PT_LOAD segments effectively stores | |
4789 | the maximum page size. When copying an executable with | |
4790 | objcopy, we set m->p_align from the input file. Use this | |
4791 | value for maxpagesize rather than bed->maxpagesize, which | |
4792 | may be different. Note that we use maxpagesize for PT_TLS | |
4793 | segment alignment later in this function, so we are relying | |
4794 | on at least one PT_LOAD segment appearing before a PT_TLS | |
4795 | segment. */ | |
4796 | if (m->p_align_valid) | |
4797 | maxpagesize = m->p_align; | |
4798 | ||
4799 | p->p_align = maxpagesize; | |
4800 | } | |
3271a814 NS |
4801 | else if (m->p_align_valid) |
4802 | p->p_align = m->p_align; | |
e970b90a DJ |
4803 | else if (m->count == 0) |
4804 | p->p_align = 1 << bed->s->log_file_align; | |
3f570048 AM |
4805 | else |
4806 | p->p_align = 0; | |
4807 | ||
bf988460 AM |
4808 | no_contents = FALSE; |
4809 | off_adjust = 0; | |
252b5132 | 4810 | if (p->p_type == PT_LOAD |
b301b248 | 4811 | && m->count > 0) |
252b5132 | 4812 | { |
b301b248 | 4813 | bfd_size_type align; |
a49e53ed | 4814 | unsigned int align_power = 0; |
b301b248 | 4815 | |
3271a814 NS |
4816 | if (m->p_align_valid) |
4817 | align = p->p_align; | |
4818 | else | |
252b5132 | 4819 | { |
3271a814 NS |
4820 | for (i = 0, secpp = m->sections; i < m->count; i++, secpp++) |
4821 | { | |
4822 | unsigned int secalign; | |
08a40648 | 4823 | |
3271a814 NS |
4824 | secalign = bfd_get_section_alignment (abfd, *secpp); |
4825 | if (secalign > align_power) | |
4826 | align_power = secalign; | |
4827 | } | |
4828 | align = (bfd_size_type) 1 << align_power; | |
4829 | if (align < maxpagesize) | |
4830 | align = maxpagesize; | |
b301b248 | 4831 | } |
252b5132 | 4832 | |
02bf8d82 AM |
4833 | for (i = 0; i < m->count; i++) |
4834 | if ((m->sections[i]->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0) | |
4835 | /* If we aren't making room for this section, then | |
4836 | it must be SHT_NOBITS regardless of what we've | |
4837 | set via struct bfd_elf_special_section. */ | |
4838 | elf_section_type (m->sections[i]) = SHT_NOBITS; | |
4839 | ||
bf988460 | 4840 | /* Find out whether this segment contains any loadable |
aea274d3 AM |
4841 | sections. */ |
4842 | no_contents = TRUE; | |
4843 | for (i = 0; i < m->count; i++) | |
4844 | if (elf_section_type (m->sections[i]) != SHT_NOBITS) | |
4845 | { | |
4846 | no_contents = FALSE; | |
4847 | break; | |
4848 | } | |
bf988460 | 4849 | |
85cfcbfb | 4850 | off_adjust = vma_page_aligned_bias (p->p_vaddr, off, align); |
bf988460 AM |
4851 | off += off_adjust; |
4852 | if (no_contents) | |
4853 | { | |
4854 | /* We shouldn't need to align the segment on disk since | |
4855 | the segment doesn't need file space, but the gABI | |
4856 | arguably requires the alignment and glibc ld.so | |
4857 | checks it. So to comply with the alignment | |
4858 | requirement but not waste file space, we adjust | |
4859 | p_offset for just this segment. (OFF_ADJUST is | |
4860 | subtracted from OFF later.) This may put p_offset | |
4861 | past the end of file, but that shouldn't matter. */ | |
4862 | } | |
4863 | else | |
4864 | off_adjust = 0; | |
252b5132 | 4865 | } |
b1a6d0b1 NC |
4866 | /* Make sure the .dynamic section is the first section in the |
4867 | PT_DYNAMIC segment. */ | |
4868 | else if (p->p_type == PT_DYNAMIC | |
4869 | && m->count > 1 | |
4870 | && strcmp (m->sections[0]->name, ".dynamic") != 0) | |
4871 | { | |
4872 | _bfd_error_handler | |
b301b248 AM |
4873 | (_("%B: The first section in the PT_DYNAMIC segment is not the .dynamic section"), |
4874 | abfd); | |
b1a6d0b1 NC |
4875 | bfd_set_error (bfd_error_bad_value); |
4876 | return FALSE; | |
4877 | } | |
3f001e84 JK |
4878 | /* Set the note section type to SHT_NOTE. */ |
4879 | else if (p->p_type == PT_NOTE) | |
4880 | for (i = 0; i < m->count; i++) | |
4881 | elf_section_type (m->sections[i]) = SHT_NOTE; | |
252b5132 | 4882 | |
252b5132 RH |
4883 | p->p_offset = 0; |
4884 | p->p_filesz = 0; | |
4885 | p->p_memsz = 0; | |
4886 | ||
4887 | if (m->includes_filehdr) | |
4888 | { | |
bf988460 | 4889 | if (!m->p_flags_valid) |
252b5132 | 4890 | p->p_flags |= PF_R; |
252b5132 RH |
4891 | p->p_filesz = bed->s->sizeof_ehdr; |
4892 | p->p_memsz = bed->s->sizeof_ehdr; | |
4893 | if (m->count > 0) | |
4894 | { | |
252b5132 RH |
4895 | if (p->p_vaddr < (bfd_vma) off) |
4896 | { | |
caf47ea6 | 4897 | (*_bfd_error_handler) |
b301b248 AM |
4898 | (_("%B: Not enough room for program headers, try linking with -N"), |
4899 | abfd); | |
252b5132 | 4900 | bfd_set_error (bfd_error_bad_value); |
b34976b6 | 4901 | return FALSE; |
252b5132 RH |
4902 | } |
4903 | ||
4904 | p->p_vaddr -= off; | |
bf988460 | 4905 | if (!m->p_paddr_valid) |
252b5132 RH |
4906 | p->p_paddr -= off; |
4907 | } | |
252b5132 RH |
4908 | } |
4909 | ||
4910 | if (m->includes_phdrs) | |
4911 | { | |
bf988460 | 4912 | if (!m->p_flags_valid) |
252b5132 RH |
4913 | p->p_flags |= PF_R; |
4914 | ||
f3520d2f | 4915 | if (!m->includes_filehdr) |
252b5132 RH |
4916 | { |
4917 | p->p_offset = bed->s->sizeof_ehdr; | |
4918 | ||
4919 | if (m->count > 0) | |
4920 | { | |
252b5132 | 4921 | p->p_vaddr -= off - p->p_offset; |
bf988460 | 4922 | if (!m->p_paddr_valid) |
252b5132 RH |
4923 | p->p_paddr -= off - p->p_offset; |
4924 | } | |
252b5132 RH |
4925 | } |
4926 | ||
4927 | p->p_filesz += alloc * bed->s->sizeof_phdr; | |
4928 | p->p_memsz += alloc * bed->s->sizeof_phdr; | |
2b0bc088 NC |
4929 | if (m->count) |
4930 | { | |
4931 | p->p_filesz += header_pad; | |
4932 | p->p_memsz += header_pad; | |
4933 | } | |
252b5132 RH |
4934 | } |
4935 | ||
4936 | if (p->p_type == PT_LOAD | |
4937 | || (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core)) | |
4938 | { | |
bf988460 | 4939 | if (!m->includes_filehdr && !m->includes_phdrs) |
02bf8d82 | 4940 | p->p_offset = off; |
252b5132 RH |
4941 | else |
4942 | { | |
4943 | file_ptr adjust; | |
4944 | ||
4945 | adjust = off - (p->p_offset + p->p_filesz); | |
bf988460 AM |
4946 | if (!no_contents) |
4947 | p->p_filesz += adjust; | |
252b5132 RH |
4948 | p->p_memsz += adjust; |
4949 | } | |
4950 | } | |
4951 | ||
1ea63fd2 AM |
4952 | /* Set up p_filesz, p_memsz, p_align and p_flags from the section |
4953 | maps. Set filepos for sections in PT_LOAD segments, and in | |
4954 | core files, for sections in PT_NOTE segments. | |
4955 | assign_file_positions_for_non_load_sections will set filepos | |
4956 | for other sections and update p_filesz for other segments. */ | |
252b5132 RH |
4957 | for (i = 0, secpp = m->sections; i < m->count; i++, secpp++) |
4958 | { | |
4959 | asection *sec; | |
252b5132 | 4960 | bfd_size_type align; |
627b32bc | 4961 | Elf_Internal_Shdr *this_hdr; |
252b5132 RH |
4962 | |
4963 | sec = *secpp; | |
02bf8d82 | 4964 | this_hdr = &elf_section_data (sec)->this_hdr; |
3f570048 | 4965 | align = (bfd_size_type) 1 << bfd_get_section_alignment (abfd, sec); |
252b5132 | 4966 | |
88967714 AM |
4967 | if ((p->p_type == PT_LOAD |
4968 | || p->p_type == PT_TLS) | |
4969 | && (this_hdr->sh_type != SHT_NOBITS | |
4970 | || ((this_hdr->sh_flags & SHF_ALLOC) != 0 | |
4971 | && ((this_hdr->sh_flags & SHF_TLS) == 0 | |
4972 | || p->p_type == PT_TLS)))) | |
252b5132 | 4973 | { |
b5599592 AM |
4974 | bfd_vma p_start = p->p_paddr; |
4975 | bfd_vma p_end = p_start + p->p_memsz; | |
4976 | bfd_vma s_start = sec->lma; | |
4977 | bfd_vma adjust = s_start - p_end; | |
252b5132 | 4978 | |
a2d1e028 L |
4979 | if (adjust != 0 |
4980 | && (s_start < p_end | |
4981 | || p_end < p_start)) | |
252b5132 | 4982 | { |
88967714 | 4983 | (*_bfd_error_handler) |
b5599592 AM |
4984 | (_("%B: section %A lma %#lx adjusted to %#lx"), abfd, sec, |
4985 | (unsigned long) s_start, (unsigned long) p_end); | |
88967714 | 4986 | adjust = 0; |
b5599592 | 4987 | sec->lma = p_end; |
1cfb7d1e | 4988 | } |
3ac9b6c9 | 4989 | p->p_memsz += adjust; |
1cfb7d1e | 4990 | |
88967714 AM |
4991 | if (this_hdr->sh_type != SHT_NOBITS) |
4992 | { | |
32812159 AM |
4993 | if (p->p_filesz + adjust < p->p_memsz) |
4994 | { | |
4995 | /* We have a PROGBITS section following NOBITS ones. | |
4996 | Allocate file space for the NOBITS section(s) and | |
4997 | zero it. */ | |
4998 | adjust = p->p_memsz - p->p_filesz; | |
4999 | if (!write_zeros (abfd, off, adjust)) | |
5000 | return FALSE; | |
5001 | } | |
88967714 AM |
5002 | off += adjust; |
5003 | p->p_filesz += adjust; | |
252b5132 | 5004 | } |
252b5132 RH |
5005 | } |
5006 | ||
5007 | if (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core) | |
5008 | { | |
b301b248 AM |
5009 | /* The section at i == 0 is the one that actually contains |
5010 | everything. */ | |
4a938328 MS |
5011 | if (i == 0) |
5012 | { | |
627b32bc | 5013 | this_hdr->sh_offset = sec->filepos = off; |
6a3cd2b4 AM |
5014 | off += this_hdr->sh_size; |
5015 | p->p_filesz = this_hdr->sh_size; | |
b301b248 AM |
5016 | p->p_memsz = 0; |
5017 | p->p_align = 1; | |
252b5132 | 5018 | } |
4a938328 | 5019 | else |
252b5132 | 5020 | { |
b301b248 | 5021 | /* The rest are fake sections that shouldn't be written. */ |
252b5132 | 5022 | sec->filepos = 0; |
eea6121a | 5023 | sec->size = 0; |
b301b248 AM |
5024 | sec->flags = 0; |
5025 | continue; | |
252b5132 | 5026 | } |
252b5132 RH |
5027 | } |
5028 | else | |
5029 | { | |
1e951488 | 5030 | if (p->p_type == PT_LOAD) |
b301b248 | 5031 | { |
1e951488 AM |
5032 | this_hdr->sh_offset = sec->filepos = off; |
5033 | if (this_hdr->sh_type != SHT_NOBITS) | |
5034 | off += this_hdr->sh_size; | |
5035 | } | |
5036 | else if (this_hdr->sh_type == SHT_NOBITS | |
5037 | && (this_hdr->sh_flags & SHF_TLS) != 0 | |
5038 | && this_hdr->sh_offset == 0) | |
5039 | { | |
5040 | /* This is a .tbss section that didn't get a PT_LOAD. | |
5041 | (See _bfd_elf_map_sections_to_segments "Create a | |
5042 | final PT_LOAD".) Set sh_offset to the value it | |
5043 | would have if we had created a zero p_filesz and | |
5044 | p_memsz PT_LOAD header for the section. This | |
5045 | also makes the PT_TLS header have the same | |
5046 | p_offset value. */ | |
5047 | bfd_vma adjust = vma_page_aligned_bias (this_hdr->sh_addr, | |
5048 | off, align); | |
5049 | this_hdr->sh_offset = sec->filepos = off + adjust; | |
b301b248 | 5050 | } |
252b5132 | 5051 | |
02bf8d82 | 5052 | if (this_hdr->sh_type != SHT_NOBITS) |
b301b248 | 5053 | { |
6a3cd2b4 | 5054 | p->p_filesz += this_hdr->sh_size; |
02bf8d82 AM |
5055 | /* A load section without SHF_ALLOC is something like |
5056 | a note section in a PT_NOTE segment. These take | |
5057 | file space but are not loaded into memory. */ | |
5058 | if ((this_hdr->sh_flags & SHF_ALLOC) != 0) | |
6a3cd2b4 | 5059 | p->p_memsz += this_hdr->sh_size; |
b301b248 | 5060 | } |
6a3cd2b4 | 5061 | else if ((this_hdr->sh_flags & SHF_ALLOC) != 0) |
13ae64f3 | 5062 | { |
6a3cd2b4 AM |
5063 | if (p->p_type == PT_TLS) |
5064 | p->p_memsz += this_hdr->sh_size; | |
5065 | ||
5066 | /* .tbss is special. It doesn't contribute to p_memsz of | |
5067 | normal segments. */ | |
5068 | else if ((this_hdr->sh_flags & SHF_TLS) == 0) | |
5069 | p->p_memsz += this_hdr->sh_size; | |
13ae64f3 JJ |
5070 | } |
5071 | ||
b10a8ae0 L |
5072 | if (align > p->p_align |
5073 | && !m->p_align_valid | |
5074 | && (p->p_type != PT_LOAD | |
5075 | || (abfd->flags & D_PAGED) == 0)) | |
252b5132 RH |
5076 | p->p_align = align; |
5077 | } | |
5078 | ||
bf988460 | 5079 | if (!m->p_flags_valid) |
252b5132 RH |
5080 | { |
5081 | p->p_flags |= PF_R; | |
02bf8d82 | 5082 | if ((this_hdr->sh_flags & SHF_EXECINSTR) != 0) |
252b5132 | 5083 | p->p_flags |= PF_X; |
02bf8d82 | 5084 | if ((this_hdr->sh_flags & SHF_WRITE) != 0) |
252b5132 RH |
5085 | p->p_flags |= PF_W; |
5086 | } | |
5087 | } | |
43a8475c | 5088 | |
bf988460 | 5089 | off -= off_adjust; |
0920dee7 | 5090 | |
7c928300 AM |
5091 | /* Check that all sections are in a PT_LOAD segment. |
5092 | Don't check funky gdb generated core files. */ | |
5093 | if (p->p_type == PT_LOAD && bfd_get_format (abfd) != bfd_core) | |
9a83a553 AM |
5094 | { |
5095 | bfd_boolean check_vma = TRUE; | |
5096 | ||
5097 | for (i = 1; i < m->count; i++) | |
5098 | if (m->sections[i]->vma == m->sections[i - 1]->vma | |
5099 | && ELF_SECTION_SIZE (&(elf_section_data (m->sections[i]) | |
5100 | ->this_hdr), p) != 0 | |
5101 | && ELF_SECTION_SIZE (&(elf_section_data (m->sections[i - 1]) | |
5102 | ->this_hdr), p) != 0) | |
0920dee7 | 5103 | { |
9a83a553 AM |
5104 | /* Looks like we have overlays packed into the segment. */ |
5105 | check_vma = FALSE; | |
5106 | break; | |
0920dee7 | 5107 | } |
9a83a553 AM |
5108 | |
5109 | for (i = 0; i < m->count; i++) | |
5110 | { | |
5111 | Elf_Internal_Shdr *this_hdr; | |
5112 | asection *sec; | |
5113 | ||
5114 | sec = m->sections[i]; | |
5115 | this_hdr = &(elf_section_data(sec)->this_hdr); | |
86b2281f AM |
5116 | if (!ELF_SECTION_IN_SEGMENT_1 (this_hdr, p, check_vma, 0) |
5117 | && !ELF_TBSS_SPECIAL (this_hdr, p)) | |
9a83a553 AM |
5118 | { |
5119 | (*_bfd_error_handler) | |
5120 | (_("%B: section `%A' can't be allocated in segment %d"), | |
5121 | abfd, sec, j); | |
5122 | print_segment_map (m); | |
5123 | } | |
5124 | } | |
5125 | } | |
252b5132 RH |
5126 | } |
5127 | ||
12bd6957 | 5128 | elf_next_file_pos (abfd) = off; |
f3520d2f AM |
5129 | return TRUE; |
5130 | } | |
5131 | ||
5132 | /* Assign file positions for the other sections. */ | |
5133 | ||
5134 | static bfd_boolean | |
5135 | assign_file_positions_for_non_load_sections (bfd *abfd, | |
5136 | struct bfd_link_info *link_info) | |
5137 | { | |
5138 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
5139 | Elf_Internal_Shdr **i_shdrpp; | |
e06efbf1 | 5140 | Elf_Internal_Shdr **hdrpp, **end_hdrpp; |
f3520d2f AM |
5141 | Elf_Internal_Phdr *phdrs; |
5142 | Elf_Internal_Phdr *p; | |
5143 | struct elf_segment_map *m; | |
62655c7b | 5144 | struct elf_segment_map *hdrs_segment; |
f3520d2f AM |
5145 | bfd_vma filehdr_vaddr, filehdr_paddr; |
5146 | bfd_vma phdrs_vaddr, phdrs_paddr; | |
5147 | file_ptr off; | |
f3520d2f AM |
5148 | unsigned int count; |
5149 | ||
5c182d5f | 5150 | i_shdrpp = elf_elfsections (abfd); |
e06efbf1 | 5151 | end_hdrpp = i_shdrpp + elf_numsections (abfd); |
12bd6957 | 5152 | off = elf_next_file_pos (abfd); |
e06efbf1 | 5153 | for (hdrpp = i_shdrpp + 1; hdrpp < end_hdrpp; hdrpp++) |
5c182d5f | 5154 | { |
5c182d5f AM |
5155 | Elf_Internal_Shdr *hdr; |
5156 | ||
5157 | hdr = *hdrpp; | |
5158 | if (hdr->bfd_section != NULL | |
252e386e AM |
5159 | && (hdr->bfd_section->filepos != 0 |
5160 | || (hdr->sh_type == SHT_NOBITS | |
5161 | && hdr->contents == NULL))) | |
627b32bc | 5162 | BFD_ASSERT (hdr->sh_offset == hdr->bfd_section->filepos); |
5c182d5f AM |
5163 | else if ((hdr->sh_flags & SHF_ALLOC) != 0) |
5164 | { | |
e8d2ba53 AM |
5165 | if (hdr->sh_size != 0) |
5166 | (*_bfd_error_handler) | |
5167 | (_("%B: warning: allocated section `%s' not in segment"), | |
5168 | abfd, | |
5169 | (hdr->bfd_section == NULL | |
5170 | ? "*unknown*" | |
5171 | : hdr->bfd_section->name)); | |
3ba71138 L |
5172 | /* We don't need to page align empty sections. */ |
5173 | if ((abfd->flags & D_PAGED) != 0 && hdr->sh_size != 0) | |
5c182d5f AM |
5174 | off += vma_page_aligned_bias (hdr->sh_addr, off, |
5175 | bed->maxpagesize); | |
5176 | else | |
5177 | off += vma_page_aligned_bias (hdr->sh_addr, off, | |
5178 | hdr->sh_addralign); | |
5179 | off = _bfd_elf_assign_file_position_for_section (hdr, off, | |
5180 | FALSE); | |
5181 | } | |
5182 | else if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA) | |
5183 | && hdr->bfd_section == NULL) | |
0ce398f1 L |
5184 | || (hdr->bfd_section != NULL |
5185 | && (hdr->bfd_section->flags & SEC_ELF_COMPRESS)) | |
5186 | /* Compress DWARF debug sections. */ | |
12bd6957 AM |
5187 | || hdr == i_shdrpp[elf_onesymtab (abfd)] |
5188 | || hdr == i_shdrpp[elf_symtab_shndx (abfd)] | |
3e19fb8f L |
5189 | || hdr == i_shdrpp[elf_strtab_sec (abfd)] |
5190 | || hdr == i_shdrpp[elf_shstrtab_sec (abfd)]) | |
5c182d5f AM |
5191 | hdr->sh_offset = -1; |
5192 | else | |
5193 | off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE); | |
5c182d5f AM |
5194 | } |
5195 | ||
252b5132 RH |
5196 | /* Now that we have set the section file positions, we can set up |
5197 | the file positions for the non PT_LOAD segments. */ | |
f3520d2f AM |
5198 | count = 0; |
5199 | filehdr_vaddr = 0; | |
5200 | filehdr_paddr = 0; | |
5201 | phdrs_vaddr = bed->maxpagesize + bed->s->sizeof_ehdr; | |
5202 | phdrs_paddr = 0; | |
62655c7b | 5203 | hdrs_segment = NULL; |
f3520d2f | 5204 | phdrs = elf_tdata (abfd)->phdr; |
12bd6957 | 5205 | for (m = elf_seg_map (abfd), p = phdrs; m != NULL; m = m->next, p++) |
f3520d2f AM |
5206 | { |
5207 | ++count; | |
5208 | if (p->p_type != PT_LOAD) | |
5209 | continue; | |
5210 | ||
5211 | if (m->includes_filehdr) | |
5212 | { | |
5213 | filehdr_vaddr = p->p_vaddr; | |
5214 | filehdr_paddr = p->p_paddr; | |
5215 | } | |
5216 | if (m->includes_phdrs) | |
5217 | { | |
5218 | phdrs_vaddr = p->p_vaddr; | |
5219 | phdrs_paddr = p->p_paddr; | |
5220 | if (m->includes_filehdr) | |
5221 | { | |
62655c7b | 5222 | hdrs_segment = m; |
f3520d2f AM |
5223 | phdrs_vaddr += bed->s->sizeof_ehdr; |
5224 | phdrs_paddr += bed->s->sizeof_ehdr; | |
5225 | } | |
5226 | } | |
5227 | } | |
5228 | ||
62655c7b RM |
5229 | if (hdrs_segment != NULL && link_info != NULL) |
5230 | { | |
5231 | /* There is a segment that contains both the file headers and the | |
5232 | program headers, so provide a symbol __ehdr_start pointing there. | |
5233 | A program can use this to examine itself robustly. */ | |
5234 | ||
5235 | struct elf_link_hash_entry *hash | |
5236 | = elf_link_hash_lookup (elf_hash_table (link_info), "__ehdr_start", | |
5237 | FALSE, FALSE, TRUE); | |
5238 | /* If the symbol was referenced and not defined, define it. */ | |
5239 | if (hash != NULL | |
5240 | && (hash->root.type == bfd_link_hash_new | |
5241 | || hash->root.type == bfd_link_hash_undefined | |
5242 | || hash->root.type == bfd_link_hash_undefweak | |
5243 | || hash->root.type == bfd_link_hash_common)) | |
5244 | { | |
5245 | asection *s = NULL; | |
5246 | if (hdrs_segment->count != 0) | |
5247 | /* The segment contains sections, so use the first one. */ | |
5248 | s = hdrs_segment->sections[0]; | |
5249 | else | |
5250 | /* Use the first (i.e. lowest-addressed) section in any segment. */ | |
12bd6957 | 5251 | for (m = elf_seg_map (abfd); m != NULL; m = m->next) |
62655c7b RM |
5252 | if (m->count != 0) |
5253 | { | |
5254 | s = m->sections[0]; | |
5255 | break; | |
5256 | } | |
5257 | ||
5258 | if (s != NULL) | |
5259 | { | |
5260 | hash->root.u.def.value = filehdr_vaddr - s->vma; | |
5261 | hash->root.u.def.section = s; | |
5262 | } | |
5263 | else | |
5264 | { | |
5265 | hash->root.u.def.value = filehdr_vaddr; | |
5266 | hash->root.u.def.section = bfd_abs_section_ptr; | |
5267 | } | |
5268 | ||
5269 | hash->root.type = bfd_link_hash_defined; | |
5270 | hash->def_regular = 1; | |
5271 | hash->non_elf = 0; | |
5272 | } | |
5273 | } | |
5274 | ||
12bd6957 | 5275 | for (m = elf_seg_map (abfd), p = phdrs; m != NULL; m = m->next, p++) |
252b5132 | 5276 | { |
129af99f | 5277 | if (p->p_type == PT_GNU_RELRO) |
252b5132 | 5278 | { |
b84a33b5 | 5279 | const Elf_Internal_Phdr *lp; |
3146fac4 | 5280 | struct elf_segment_map *lm; |
1ea63fd2 | 5281 | |
129af99f | 5282 | if (link_info != NULL) |
8c37241b | 5283 | { |
129af99f AS |
5284 | /* During linking the range of the RELRO segment is passed |
5285 | in link_info. */ | |
12bd6957 | 5286 | for (lm = elf_seg_map (abfd), lp = phdrs; |
3146fac4 AM |
5287 | lm != NULL; |
5288 | lm = lm->next, lp++) | |
8c37241b JJ |
5289 | { |
5290 | if (lp->p_type == PT_LOAD | |
b84a33b5 | 5291 | && lp->p_vaddr < link_info->relro_end |
3146fac4 AM |
5292 | && lm->count != 0 |
5293 | && lm->sections[0]->vma >= link_info->relro_start) | |
8c37241b JJ |
5294 | break; |
5295 | } | |
8981c88a | 5296 | |
3146fac4 | 5297 | BFD_ASSERT (lm != NULL); |
8c37241b | 5298 | } |
129af99f AS |
5299 | else |
5300 | { | |
5301 | /* Otherwise we are copying an executable or shared | |
b84a33b5 | 5302 | library, but we need to use the same linker logic. */ |
129af99f AS |
5303 | for (lp = phdrs; lp < phdrs + count; ++lp) |
5304 | { | |
5305 | if (lp->p_type == PT_LOAD | |
5306 | && lp->p_paddr == p->p_paddr) | |
5307 | break; | |
5308 | } | |
b84a33b5 AM |
5309 | } |
5310 | ||
5311 | if (lp < phdrs + count) | |
5312 | { | |
5313 | p->p_vaddr = lp->p_vaddr; | |
5314 | p->p_paddr = lp->p_paddr; | |
5315 | p->p_offset = lp->p_offset; | |
5316 | if (link_info != NULL) | |
5317 | p->p_filesz = link_info->relro_end - lp->p_vaddr; | |
5318 | else if (m->p_size_valid) | |
5319 | p->p_filesz = m->p_size; | |
129af99f AS |
5320 | else |
5321 | abort (); | |
b84a33b5 | 5322 | p->p_memsz = p->p_filesz; |
f3944f72 L |
5323 | /* Preserve the alignment and flags if they are valid. The |
5324 | gold linker generates RW/4 for the PT_GNU_RELRO section. | |
5325 | It is better for objcopy/strip to honor these attributes | |
5326 | otherwise gdb will choke when using separate debug files. | |
5327 | */ | |
5328 | if (!m->p_align_valid) | |
5329 | p->p_align = 1; | |
5330 | if (!m->p_flags_valid) | |
5331 | p->p_flags = (lp->p_flags & ~PF_W); | |
129af99f | 5332 | } |
9433b9b1 | 5333 | else |
b84a33b5 AM |
5334 | { |
5335 | memset (p, 0, sizeof *p); | |
5336 | p->p_type = PT_NULL; | |
5337 | } | |
129af99f | 5338 | } |
04c3a755 NS |
5339 | else if (p->p_type == PT_GNU_STACK) |
5340 | { | |
5341 | if (m->p_size_valid) | |
5342 | p->p_memsz = m->p_size; | |
5343 | } | |
129af99f AS |
5344 | else if (m->count != 0) |
5345 | { | |
e06efbf1 | 5346 | unsigned int i; |
129af99f AS |
5347 | if (p->p_type != PT_LOAD |
5348 | && (p->p_type != PT_NOTE | |
5349 | || bfd_get_format (abfd) != bfd_core)) | |
5350 | { | |
c86934ce NC |
5351 | if (m->includes_filehdr || m->includes_phdrs) |
5352 | { | |
b1fa9dd6 | 5353 | /* PR 17512: file: 2195325e. */ |
c86934ce NC |
5354 | (*_bfd_error_handler) |
5355 | (_("%B: warning: non-load segment includes file header and/or program header"), | |
5356 | abfd); | |
5357 | return FALSE; | |
5358 | } | |
129af99f | 5359 | |
86b2281f | 5360 | p->p_filesz = 0; |
129af99f | 5361 | p->p_offset = m->sections[0]->filepos; |
86b2281f AM |
5362 | for (i = m->count; i-- != 0;) |
5363 | { | |
5364 | asection *sect = m->sections[i]; | |
5365 | Elf_Internal_Shdr *hdr = &elf_section_data (sect)->this_hdr; | |
5366 | if (hdr->sh_type != SHT_NOBITS) | |
5367 | { | |
5368 | p->p_filesz = (sect->filepos - m->sections[0]->filepos | |
5369 | + hdr->sh_size); | |
5370 | break; | |
5371 | } | |
5372 | } | |
129af99f AS |
5373 | } |
5374 | } | |
5375 | else if (m->includes_filehdr) | |
5376 | { | |
5377 | p->p_vaddr = filehdr_vaddr; | |
5378 | if (! m->p_paddr_valid) | |
5379 | p->p_paddr = filehdr_paddr; | |
5380 | } | |
5381 | else if (m->includes_phdrs) | |
5382 | { | |
5383 | p->p_vaddr = phdrs_vaddr; | |
5384 | if (! m->p_paddr_valid) | |
5385 | p->p_paddr = phdrs_paddr; | |
252b5132 RH |
5386 | } |
5387 | } | |
5388 | ||
12bd6957 | 5389 | elf_next_file_pos (abfd) = off; |
252b5132 | 5390 | |
b34976b6 | 5391 | return TRUE; |
252b5132 RH |
5392 | } |
5393 | ||
252b5132 RH |
5394 | /* Work out the file positions of all the sections. This is called by |
5395 | _bfd_elf_compute_section_file_positions. All the section sizes and | |
5396 | VMAs must be known before this is called. | |
5397 | ||
e0638f70 AM |
5398 | Reloc sections come in two flavours: Those processed specially as |
5399 | "side-channel" data attached to a section to which they apply, and | |
5400 | those that bfd doesn't process as relocations. The latter sort are | |
5401 | stored in a normal bfd section by bfd_section_from_shdr. We don't | |
5402 | consider the former sort here, unless they form part of the loadable | |
5403 | image. Reloc sections not assigned here will be handled later by | |
5404 | assign_file_positions_for_relocs. | |
252b5132 RH |
5405 | |
5406 | We also don't set the positions of the .symtab and .strtab here. */ | |
5407 | ||
b34976b6 | 5408 | static bfd_boolean |
c84fca4d AO |
5409 | assign_file_positions_except_relocs (bfd *abfd, |
5410 | struct bfd_link_info *link_info) | |
252b5132 | 5411 | { |
5c182d5f AM |
5412 | struct elf_obj_tdata *tdata = elf_tdata (abfd); |
5413 | Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd); | |
9c5bfbb7 | 5414 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
252b5132 RH |
5415 | |
5416 | if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0 | |
5417 | && bfd_get_format (abfd) != bfd_core) | |
5418 | { | |
5c182d5f AM |
5419 | Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd); |
5420 | unsigned int num_sec = elf_numsections (abfd); | |
252b5132 RH |
5421 | Elf_Internal_Shdr **hdrpp; |
5422 | unsigned int i; | |
a485e98e | 5423 | file_ptr off; |
252b5132 RH |
5424 | |
5425 | /* Start after the ELF header. */ | |
5426 | off = i_ehdrp->e_ehsize; | |
5427 | ||
5428 | /* We are not creating an executable, which means that we are | |
5429 | not creating a program header, and that the actual order of | |
5430 | the sections in the file is unimportant. */ | |
9ad5cbcf | 5431 | for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++) |
252b5132 RH |
5432 | { |
5433 | Elf_Internal_Shdr *hdr; | |
5434 | ||
5435 | hdr = *hdrpp; | |
e0638f70 AM |
5436 | if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA) |
5437 | && hdr->bfd_section == NULL) | |
0ce398f1 L |
5438 | || (hdr->bfd_section != NULL |
5439 | && (hdr->bfd_section->flags & SEC_ELF_COMPRESS)) | |
5440 | /* Compress DWARF debug sections. */ | |
12bd6957 AM |
5441 | || i == elf_onesymtab (abfd) |
5442 | || i == elf_symtab_shndx (abfd) | |
3e19fb8f L |
5443 | || i == elf_strtab_sec (abfd) |
5444 | || i == elf_shstrtab_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; |
3e19fb8f | 5600 | Elf_Internal_Shdr *shdrp; |
a485e98e AM |
5601 | Elf_Internal_Ehdr *i_ehdrp; |
5602 | const struct elf_backend_data *bed; | |
252b5132 | 5603 | |
12bd6957 | 5604 | off = elf_next_file_pos (abfd); |
252b5132 | 5605 | |
e06efbf1 L |
5606 | shdrpp = elf_elfsections (abfd); |
5607 | end_shdrpp = shdrpp + elf_numsections (abfd); | |
5608 | for (shdrpp++; shdrpp < end_shdrpp; shdrpp++) | |
252b5132 | 5609 | { |
252b5132 | 5610 | shdrp = *shdrpp; |
0ce398f1 L |
5611 | if (shdrp->sh_offset == -1) |
5612 | { | |
3e19fb8f | 5613 | asection *sec = shdrp->bfd_section; |
0ce398f1 L |
5614 | bfd_boolean is_rel = (shdrp->sh_type == SHT_REL |
5615 | || shdrp->sh_type == SHT_RELA); | |
5616 | if (is_rel | |
3e19fb8f | 5617 | || (sec != NULL && (sec->flags & SEC_ELF_COMPRESS))) |
0ce398f1 L |
5618 | { |
5619 | if (!is_rel) | |
5620 | { | |
3e19fb8f L |
5621 | const char *name = sec->name; |
5622 | struct bfd_elf_section_data *d; | |
5623 | ||
0ce398f1 | 5624 | /* Compress DWARF debug sections. */ |
3e19fb8f | 5625 | if (!bfd_compress_section (abfd, sec, |
0ce398f1 L |
5626 | shdrp->contents)) |
5627 | return FALSE; | |
3e19fb8f L |
5628 | |
5629 | if (sec->compress_status == COMPRESS_SECTION_DONE | |
5630 | && (abfd->flags & BFD_COMPRESS_GABI) == 0) | |
5631 | { | |
5632 | /* If section is compressed with zlib-gnu, convert | |
5633 | section name from .debug_* to .zdebug_*. */ | |
5634 | char *new_name | |
5635 | = convert_debug_to_zdebug (abfd, name); | |
5636 | if (new_name == NULL) | |
5637 | return FALSE; | |
5638 | name = new_name; | |
5639 | } | |
5640 | /* Add setion name to section name section. */ | |
5641 | if (shdrp->sh_name != (unsigned int) -1) | |
5642 | abort (); | |
5643 | shdrp->sh_name | |
5644 | = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), | |
5645 | name, FALSE); | |
5646 | d = elf_section_data (sec); | |
5647 | ||
5648 | /* Add reloc setion name to section name section. */ | |
5649 | if (d->rel.hdr | |
5650 | && !_bfd_elf_set_reloc_sh_name (abfd, | |
5651 | d->rel.hdr, | |
5652 | name, FALSE)) | |
5653 | return FALSE; | |
5654 | if (d->rela.hdr | |
5655 | && !_bfd_elf_set_reloc_sh_name (abfd, | |
5656 | d->rela.hdr, | |
91cb26da | 5657 | name, TRUE)) |
3e19fb8f L |
5658 | return FALSE; |
5659 | ||
0ce398f1 | 5660 | /* Update section size and contents. */ |
3e19fb8f L |
5661 | shdrp->sh_size = sec->size; |
5662 | shdrp->contents = sec->contents; | |
0ce398f1 L |
5663 | shdrp->bfd_section->contents = NULL; |
5664 | } | |
5665 | off = _bfd_elf_assign_file_position_for_section (shdrp, | |
5666 | off, | |
5667 | TRUE); | |
5668 | } | |
5669 | } | |
252b5132 RH |
5670 | } |
5671 | ||
3e19fb8f L |
5672 | /* Place section name section after DWARF debug sections have been |
5673 | compressed. */ | |
5674 | _bfd_elf_strtab_finalize (elf_shstrtab (abfd)); | |
5675 | shdrp = &elf_tdata (abfd)->shstrtab_hdr; | |
5676 | shdrp->sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd)); | |
5677 | off = _bfd_elf_assign_file_position_for_section (shdrp, off, TRUE); | |
5678 | ||
5679 | /* Place the section headers. */ | |
a485e98e AM |
5680 | i_ehdrp = elf_elfheader (abfd); |
5681 | bed = get_elf_backend_data (abfd); | |
5682 | off = align_file_position (off, 1 << bed->s->log_file_align); | |
5683 | i_ehdrp->e_shoff = off; | |
5684 | off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize; | |
12bd6957 | 5685 | elf_next_file_pos (abfd) = off; |
0ce398f1 L |
5686 | |
5687 | return TRUE; | |
252b5132 RH |
5688 | } |
5689 | ||
b34976b6 | 5690 | bfd_boolean |
217aa764 | 5691 | _bfd_elf_write_object_contents (bfd *abfd) |
252b5132 | 5692 | { |
9c5bfbb7 | 5693 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
252b5132 | 5694 | Elf_Internal_Shdr **i_shdrp; |
b34976b6 | 5695 | bfd_boolean failed; |
9ad5cbcf | 5696 | unsigned int count, num_sec; |
30e8ee25 | 5697 | struct elf_obj_tdata *t; |
252b5132 RH |
5698 | |
5699 | if (! abfd->output_has_begun | |
217aa764 | 5700 | && ! _bfd_elf_compute_section_file_positions (abfd, NULL)) |
b34976b6 | 5701 | return FALSE; |
252b5132 RH |
5702 | |
5703 | i_shdrp = elf_elfsections (abfd); | |
252b5132 | 5704 | |
b34976b6 | 5705 | failed = FALSE; |
252b5132 RH |
5706 | bfd_map_over_sections (abfd, bed->s->write_relocs, &failed); |
5707 | if (failed) | |
b34976b6 | 5708 | return FALSE; |
252b5132 | 5709 | |
0ce398f1 L |
5710 | if (!_bfd_elf_assign_file_positions_for_non_load (abfd)) |
5711 | return FALSE; | |
252b5132 | 5712 | |
c044fabd | 5713 | /* After writing the headers, we need to write the sections too... */ |
9ad5cbcf AM |
5714 | num_sec = elf_numsections (abfd); |
5715 | for (count = 1; count < num_sec; count++) | |
252b5132 | 5716 | { |
3e19fb8f L |
5717 | i_shdrp[count]->sh_name |
5718 | = _bfd_elf_strtab_offset (elf_shstrtab (abfd), | |
5719 | i_shdrp[count]->sh_name); | |
252b5132 RH |
5720 | if (bed->elf_backend_section_processing) |
5721 | (*bed->elf_backend_section_processing) (abfd, i_shdrp[count]); | |
5722 | if (i_shdrp[count]->contents) | |
5723 | { | |
dc810e39 AM |
5724 | bfd_size_type amt = i_shdrp[count]->sh_size; |
5725 | ||
252b5132 | 5726 | if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0 |
dc810e39 | 5727 | || bfd_bwrite (i_shdrp[count]->contents, amt, abfd) != amt) |
b34976b6 | 5728 | return FALSE; |
252b5132 RH |
5729 | } |
5730 | } | |
5731 | ||
5732 | /* Write out the section header names. */ | |
30e8ee25 | 5733 | t = elf_tdata (abfd); |
26ae6d5e | 5734 | if (elf_shstrtab (abfd) != NULL |
30e8ee25 | 5735 | && (bfd_seek (abfd, t->shstrtab_hdr.sh_offset, SEEK_SET) != 0 |
08a40648 | 5736 | || !_bfd_elf_strtab_emit (abfd, elf_shstrtab (abfd)))) |
b34976b6 | 5737 | return FALSE; |
252b5132 RH |
5738 | |
5739 | if (bed->elf_backend_final_write_processing) | |
12bd6957 | 5740 | (*bed->elf_backend_final_write_processing) (abfd, elf_linker (abfd)); |
252b5132 | 5741 | |
ff59fc36 RM |
5742 | if (!bed->s->write_shdrs_and_ehdr (abfd)) |
5743 | return FALSE; | |
5744 | ||
5745 | /* This is last since write_shdrs_and_ehdr can touch i_shdrp[0]. */ | |
c0355132 AM |
5746 | if (t->o->build_id.after_write_object_contents != NULL) |
5747 | return (*t->o->build_id.after_write_object_contents) (abfd); | |
ff59fc36 RM |
5748 | |
5749 | return TRUE; | |
252b5132 RH |
5750 | } |
5751 | ||
b34976b6 | 5752 | bfd_boolean |
217aa764 | 5753 | _bfd_elf_write_corefile_contents (bfd *abfd) |
252b5132 | 5754 | { |
c044fabd | 5755 | /* Hopefully this can be done just like an object file. */ |
252b5132 RH |
5756 | return _bfd_elf_write_object_contents (abfd); |
5757 | } | |
c044fabd KH |
5758 | |
5759 | /* Given a section, search the header to find them. */ | |
5760 | ||
cb33740c | 5761 | unsigned int |
198beae2 | 5762 | _bfd_elf_section_from_bfd_section (bfd *abfd, struct bfd_section *asect) |
252b5132 | 5763 | { |
9c5bfbb7 | 5764 | const struct elf_backend_data *bed; |
91d6fa6a | 5765 | unsigned int sec_index; |
252b5132 | 5766 | |
9ad5cbcf AM |
5767 | if (elf_section_data (asect) != NULL |
5768 | && elf_section_data (asect)->this_idx != 0) | |
5769 | return elf_section_data (asect)->this_idx; | |
5770 | ||
5771 | if (bfd_is_abs_section (asect)) | |
91d6fa6a | 5772 | sec_index = SHN_ABS; |
af746e92 | 5773 | else if (bfd_is_com_section (asect)) |
91d6fa6a | 5774 | sec_index = SHN_COMMON; |
af746e92 | 5775 | else if (bfd_is_und_section (asect)) |
91d6fa6a | 5776 | sec_index = SHN_UNDEF; |
af746e92 | 5777 | else |
91d6fa6a | 5778 | sec_index = SHN_BAD; |
252b5132 | 5779 | |
af746e92 | 5780 | bed = get_elf_backend_data (abfd); |
252b5132 RH |
5781 | if (bed->elf_backend_section_from_bfd_section) |
5782 | { | |
91d6fa6a | 5783 | int retval = sec_index; |
9ad5cbcf | 5784 | |
af746e92 AM |
5785 | if ((*bed->elf_backend_section_from_bfd_section) (abfd, asect, &retval)) |
5786 | return retval; | |
252b5132 RH |
5787 | } |
5788 | ||
91d6fa6a | 5789 | if (sec_index == SHN_BAD) |
af746e92 | 5790 | bfd_set_error (bfd_error_nonrepresentable_section); |
252b5132 | 5791 | |
91d6fa6a | 5792 | return sec_index; |
252b5132 RH |
5793 | } |
5794 | ||
5795 | /* Given a BFD symbol, return the index in the ELF symbol table, or -1 | |
5796 | on error. */ | |
5797 | ||
5798 | int | |
217aa764 | 5799 | _bfd_elf_symbol_from_bfd_symbol (bfd *abfd, asymbol **asym_ptr_ptr) |
252b5132 RH |
5800 | { |
5801 | asymbol *asym_ptr = *asym_ptr_ptr; | |
5802 | int idx; | |
5803 | flagword flags = asym_ptr->flags; | |
5804 | ||
5805 | /* When gas creates relocations against local labels, it creates its | |
5806 | own symbol for the section, but does put the symbol into the | |
5807 | symbol chain, so udata is 0. When the linker is generating | |
5808 | relocatable output, this section symbol may be for one of the | |
5809 | input sections rather than the output section. */ | |
5810 | if (asym_ptr->udata.i == 0 | |
5811 | && (flags & BSF_SECTION_SYM) | |
5812 | && asym_ptr->section) | |
5813 | { | |
5372391b | 5814 | asection *sec; |
252b5132 RH |
5815 | int indx; |
5816 | ||
5372391b AM |
5817 | sec = asym_ptr->section; |
5818 | if (sec->owner != abfd && sec->output_section != NULL) | |
5819 | sec = sec->output_section; | |
5820 | if (sec->owner == abfd | |
5821 | && (indx = sec->index) < elf_num_section_syms (abfd) | |
4e89ac30 | 5822 | && elf_section_syms (abfd)[indx] != NULL) |
252b5132 RH |
5823 | asym_ptr->udata.i = elf_section_syms (abfd)[indx]->udata.i; |
5824 | } | |
5825 | ||
5826 | idx = asym_ptr->udata.i; | |
5827 | ||
5828 | if (idx == 0) | |
5829 | { | |
5830 | /* This case can occur when using --strip-symbol on a symbol | |
08a40648 | 5831 | which is used in a relocation entry. */ |
252b5132 | 5832 | (*_bfd_error_handler) |
d003868e AM |
5833 | (_("%B: symbol `%s' required but not present"), |
5834 | abfd, bfd_asymbol_name (asym_ptr)); | |
252b5132 RH |
5835 | bfd_set_error (bfd_error_no_symbols); |
5836 | return -1; | |
5837 | } | |
5838 | ||
5839 | #if DEBUG & 4 | |
5840 | { | |
5841 | fprintf (stderr, | |
9ccb8af9 AM |
5842 | "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8lx\n", |
5843 | (long) asym_ptr, asym_ptr->name, idx, (long) flags); | |
252b5132 RH |
5844 | fflush (stderr); |
5845 | } | |
5846 | #endif | |
5847 | ||
5848 | return idx; | |
5849 | } | |
5850 | ||
84d1d650 | 5851 | /* Rewrite program header information. */ |
252b5132 | 5852 | |
b34976b6 | 5853 | static bfd_boolean |
84d1d650 | 5854 | rewrite_elf_program_header (bfd *ibfd, bfd *obfd) |
252b5132 | 5855 | { |
b34976b6 AM |
5856 | Elf_Internal_Ehdr *iehdr; |
5857 | struct elf_segment_map *map; | |
5858 | struct elf_segment_map *map_first; | |
5859 | struct elf_segment_map **pointer_to_map; | |
5860 | Elf_Internal_Phdr *segment; | |
5861 | asection *section; | |
5862 | unsigned int i; | |
5863 | unsigned int num_segments; | |
5864 | bfd_boolean phdr_included = FALSE; | |
5c44b38e | 5865 | bfd_boolean p_paddr_valid; |
b34976b6 AM |
5866 | bfd_vma maxpagesize; |
5867 | struct elf_segment_map *phdr_adjust_seg = NULL; | |
5868 | unsigned int phdr_adjust_num = 0; | |
9c5bfbb7 | 5869 | const struct elf_backend_data *bed; |
bc67d8a6 | 5870 | |
caf47ea6 | 5871 | bed = get_elf_backend_data (ibfd); |
252b5132 RH |
5872 | iehdr = elf_elfheader (ibfd); |
5873 | ||
bc67d8a6 | 5874 | map_first = NULL; |
c044fabd | 5875 | pointer_to_map = &map_first; |
252b5132 RH |
5876 | |
5877 | num_segments = elf_elfheader (ibfd)->e_phnum; | |
bc67d8a6 NC |
5878 | maxpagesize = get_elf_backend_data (obfd)->maxpagesize; |
5879 | ||
5880 | /* Returns the end address of the segment + 1. */ | |
aecc8f8a AM |
5881 | #define SEGMENT_END(segment, start) \ |
5882 | (start + (segment->p_memsz > segment->p_filesz \ | |
5883 | ? segment->p_memsz : segment->p_filesz)) | |
bc67d8a6 | 5884 | |
eecdbe52 JJ |
5885 | #define SECTION_SIZE(section, segment) \ |
5886 | (((section->flags & (SEC_HAS_CONTENTS | SEC_THREAD_LOCAL)) \ | |
5887 | != SEC_THREAD_LOCAL || segment->p_type == PT_TLS) \ | |
eea6121a | 5888 | ? section->size : 0) |
eecdbe52 | 5889 | |
b34976b6 | 5890 | /* Returns TRUE if the given section is contained within |
bc67d8a6 | 5891 | the given segment. VMA addresses are compared. */ |
aecc8f8a AM |
5892 | #define IS_CONTAINED_BY_VMA(section, segment) \ |
5893 | (section->vma >= segment->p_vaddr \ | |
eecdbe52 | 5894 | && (section->vma + SECTION_SIZE (section, segment) \ |
aecc8f8a | 5895 | <= (SEGMENT_END (segment, segment->p_vaddr)))) |
c044fabd | 5896 | |
b34976b6 | 5897 | /* Returns TRUE if the given section is contained within |
bc67d8a6 | 5898 | the given segment. LMA addresses are compared. */ |
aecc8f8a AM |
5899 | #define IS_CONTAINED_BY_LMA(section, segment, base) \ |
5900 | (section->lma >= base \ | |
eecdbe52 | 5901 | && (section->lma + SECTION_SIZE (section, segment) \ |
aecc8f8a | 5902 | <= SEGMENT_END (segment, base))) |
252b5132 | 5903 | |
0efc80c8 L |
5904 | /* Handle PT_NOTE segment. */ |
5905 | #define IS_NOTE(p, s) \ | |
aecc8f8a | 5906 | (p->p_type == PT_NOTE \ |
0efc80c8 | 5907 | && elf_section_type (s) == SHT_NOTE \ |
aecc8f8a | 5908 | && (bfd_vma) s->filepos >= p->p_offset \ |
cb3ff1e5 | 5909 | && ((bfd_vma) s->filepos + s->size \ |
aecc8f8a | 5910 | <= p->p_offset + p->p_filesz)) |
252b5132 | 5911 | |
0efc80c8 L |
5912 | /* Special case: corefile "NOTE" section containing regs, prpsinfo |
5913 | etc. */ | |
5914 | #define IS_COREFILE_NOTE(p, s) \ | |
5915 | (IS_NOTE (p, s) \ | |
5916 | && bfd_get_format (ibfd) == bfd_core \ | |
5917 | && s->vma == 0 \ | |
5918 | && s->lma == 0) | |
5919 | ||
252b5132 RH |
5920 | /* The complicated case when p_vaddr is 0 is to handle the Solaris |
5921 | linker, which generates a PT_INTERP section with p_vaddr and | |
5922 | p_memsz set to 0. */ | |
aecc8f8a AM |
5923 | #define IS_SOLARIS_PT_INTERP(p, s) \ |
5924 | (p->p_vaddr == 0 \ | |
5925 | && p->p_paddr == 0 \ | |
5926 | && p->p_memsz == 0 \ | |
5927 | && p->p_filesz > 0 \ | |
5928 | && (s->flags & SEC_HAS_CONTENTS) != 0 \ | |
eea6121a | 5929 | && s->size > 0 \ |
aecc8f8a | 5930 | && (bfd_vma) s->filepos >= p->p_offset \ |
cb3ff1e5 | 5931 | && ((bfd_vma) s->filepos + s->size \ |
aecc8f8a | 5932 | <= p->p_offset + p->p_filesz)) |
5c440b1e | 5933 | |
bc67d8a6 NC |
5934 | /* Decide if the given section should be included in the given segment. |
5935 | A section will be included if: | |
f5ffc919 | 5936 | 1. It is within the address space of the segment -- we use the LMA |
08a40648 | 5937 | if that is set for the segment and the VMA otherwise, |
0efc80c8 | 5938 | 2. It is an allocated section or a NOTE section in a PT_NOTE |
d324f6d6 | 5939 | segment. |
bc67d8a6 | 5940 | 3. There is an output section associated with it, |
eecdbe52 | 5941 | 4. The section has not already been allocated to a previous segment. |
2b05f1b7 | 5942 | 5. PT_GNU_STACK segments do not include any sections. |
03394ac9 | 5943 | 6. PT_TLS segment includes only SHF_TLS sections. |
6f79b219 JJ |
5944 | 7. SHF_TLS sections are only in PT_TLS or PT_LOAD segments. |
5945 | 8. PT_DYNAMIC should not contain empty sections at the beginning | |
08a40648 | 5946 | (with the possible exception of .dynamic). */ |
9f17e2a6 | 5947 | #define IS_SECTION_IN_INPUT_SEGMENT(section, segment, bed) \ |
2b05f1b7 L |
5948 | ((((segment->p_paddr \ |
5949 | ? IS_CONTAINED_BY_LMA (section, segment, segment->p_paddr) \ | |
5950 | : IS_CONTAINED_BY_VMA (section, segment)) \ | |
5951 | && (section->flags & SEC_ALLOC) != 0) \ | |
0efc80c8 | 5952 | || IS_NOTE (segment, section)) \ |
2b05f1b7 L |
5953 | && segment->p_type != PT_GNU_STACK \ |
5954 | && (segment->p_type != PT_TLS \ | |
5955 | || (section->flags & SEC_THREAD_LOCAL)) \ | |
5956 | && (segment->p_type == PT_LOAD \ | |
5957 | || segment->p_type == PT_TLS \ | |
5958 | || (section->flags & SEC_THREAD_LOCAL) == 0) \ | |
5959 | && (segment->p_type != PT_DYNAMIC \ | |
5960 | || SECTION_SIZE (section, segment) > 0 \ | |
5961 | || (segment->p_paddr \ | |
5962 | ? segment->p_paddr != section->lma \ | |
5963 | : segment->p_vaddr != section->vma) \ | |
5964 | || (strcmp (bfd_get_section_name (ibfd, section), ".dynamic") \ | |
5965 | == 0)) \ | |
0067a569 | 5966 | && !section->segment_mark) |
bc67d8a6 | 5967 | |
9f17e2a6 L |
5968 | /* If the output section of a section in the input segment is NULL, |
5969 | it is removed from the corresponding output segment. */ | |
5970 | #define INCLUDE_SECTION_IN_SEGMENT(section, segment, bed) \ | |
5971 | (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed) \ | |
5972 | && section->output_section != NULL) | |
5973 | ||
b34976b6 | 5974 | /* Returns TRUE iff seg1 starts after the end of seg2. */ |
b5f852ea NC |
5975 | #define SEGMENT_AFTER_SEGMENT(seg1, seg2, field) \ |
5976 | (seg1->field >= SEGMENT_END (seg2, seg2->field)) | |
5977 | ||
5978 | /* Returns TRUE iff seg1 and seg2 overlap. Segments overlap iff both | |
5979 | their VMA address ranges and their LMA address ranges overlap. | |
5980 | It is possible to have overlapping VMA ranges without overlapping LMA | |
5981 | ranges. RedBoot images for example can have both .data and .bss mapped | |
5982 | to the same VMA range, but with the .data section mapped to a different | |
5983 | LMA. */ | |
aecc8f8a | 5984 | #define SEGMENT_OVERLAPS(seg1, seg2) \ |
b5f852ea | 5985 | ( !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_vaddr) \ |
08a40648 | 5986 | || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_vaddr)) \ |
b5f852ea | 5987 | && !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_paddr) \ |
08a40648 | 5988 | || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_paddr))) |
bc67d8a6 NC |
5989 | |
5990 | /* Initialise the segment mark field. */ | |
5991 | for (section = ibfd->sections; section != NULL; section = section->next) | |
b34976b6 | 5992 | section->segment_mark = FALSE; |
bc67d8a6 | 5993 | |
5c44b38e AM |
5994 | /* The Solaris linker creates program headers in which all the |
5995 | p_paddr fields are zero. When we try to objcopy or strip such a | |
5996 | file, we get confused. Check for this case, and if we find it | |
5997 | don't set the p_paddr_valid fields. */ | |
5998 | p_paddr_valid = FALSE; | |
5999 | for (i = 0, segment = elf_tdata (ibfd)->phdr; | |
6000 | i < num_segments; | |
6001 | i++, segment++) | |
6002 | if (segment->p_paddr != 0) | |
6003 | { | |
6004 | p_paddr_valid = TRUE; | |
6005 | break; | |
6006 | } | |
6007 | ||
252b5132 | 6008 | /* Scan through the segments specified in the program header |
bc67d8a6 | 6009 | of the input BFD. For this first scan we look for overlaps |
9ad5cbcf | 6010 | in the loadable segments. These can be created by weird |
aecc8f8a | 6011 | parameters to objcopy. Also, fix some solaris weirdness. */ |
bc67d8a6 NC |
6012 | for (i = 0, segment = elf_tdata (ibfd)->phdr; |
6013 | i < num_segments; | |
c044fabd | 6014 | i++, segment++) |
252b5132 | 6015 | { |
252b5132 | 6016 | unsigned int j; |
c044fabd | 6017 | Elf_Internal_Phdr *segment2; |
252b5132 | 6018 | |
aecc8f8a AM |
6019 | if (segment->p_type == PT_INTERP) |
6020 | for (section = ibfd->sections; section; section = section->next) | |
6021 | if (IS_SOLARIS_PT_INTERP (segment, section)) | |
6022 | { | |
6023 | /* Mininal change so that the normal section to segment | |
4cc11e76 | 6024 | assignment code will work. */ |
aecc8f8a AM |
6025 | segment->p_vaddr = section->vma; |
6026 | break; | |
6027 | } | |
6028 | ||
bc67d8a6 | 6029 | if (segment->p_type != PT_LOAD) |
b10a8ae0 L |
6030 | { |
6031 | /* Remove PT_GNU_RELRO segment. */ | |
6032 | if (segment->p_type == PT_GNU_RELRO) | |
6033 | segment->p_type = PT_NULL; | |
6034 | continue; | |
6035 | } | |
c044fabd | 6036 | |
bc67d8a6 | 6037 | /* Determine if this segment overlaps any previous segments. */ |
0067a569 | 6038 | for (j = 0, segment2 = elf_tdata (ibfd)->phdr; j < i; j++, segment2++) |
bc67d8a6 NC |
6039 | { |
6040 | bfd_signed_vma extra_length; | |
c044fabd | 6041 | |
bc67d8a6 | 6042 | if (segment2->p_type != PT_LOAD |
0067a569 | 6043 | || !SEGMENT_OVERLAPS (segment, segment2)) |
bc67d8a6 | 6044 | continue; |
c044fabd | 6045 | |
bc67d8a6 NC |
6046 | /* Merge the two segments together. */ |
6047 | if (segment2->p_vaddr < segment->p_vaddr) | |
6048 | { | |
c044fabd | 6049 | /* Extend SEGMENT2 to include SEGMENT and then delete |
08a40648 | 6050 | SEGMENT. */ |
0067a569 AM |
6051 | extra_length = (SEGMENT_END (segment, segment->p_vaddr) |
6052 | - SEGMENT_END (segment2, segment2->p_vaddr)); | |
c044fabd | 6053 | |
bc67d8a6 NC |
6054 | if (extra_length > 0) |
6055 | { | |
0067a569 | 6056 | segment2->p_memsz += extra_length; |
bc67d8a6 NC |
6057 | segment2->p_filesz += extra_length; |
6058 | } | |
c044fabd | 6059 | |
bc67d8a6 | 6060 | segment->p_type = PT_NULL; |
c044fabd | 6061 | |
bc67d8a6 NC |
6062 | /* Since we have deleted P we must restart the outer loop. */ |
6063 | i = 0; | |
6064 | segment = elf_tdata (ibfd)->phdr; | |
6065 | break; | |
6066 | } | |
6067 | else | |
6068 | { | |
c044fabd | 6069 | /* Extend SEGMENT to include SEGMENT2 and then delete |
08a40648 | 6070 | SEGMENT2. */ |
0067a569 AM |
6071 | extra_length = (SEGMENT_END (segment2, segment2->p_vaddr) |
6072 | - SEGMENT_END (segment, segment->p_vaddr)); | |
c044fabd | 6073 | |
bc67d8a6 NC |
6074 | if (extra_length > 0) |
6075 | { | |
0067a569 | 6076 | segment->p_memsz += extra_length; |
bc67d8a6 NC |
6077 | segment->p_filesz += extra_length; |
6078 | } | |
c044fabd | 6079 | |
bc67d8a6 NC |
6080 | segment2->p_type = PT_NULL; |
6081 | } | |
6082 | } | |
6083 | } | |
c044fabd | 6084 | |
bc67d8a6 NC |
6085 | /* The second scan attempts to assign sections to segments. */ |
6086 | for (i = 0, segment = elf_tdata (ibfd)->phdr; | |
6087 | i < num_segments; | |
0067a569 | 6088 | i++, segment++) |
bc67d8a6 | 6089 | { |
0067a569 AM |
6090 | unsigned int section_count; |
6091 | asection **sections; | |
6092 | asection *output_section; | |
6093 | unsigned int isec; | |
6094 | bfd_vma matching_lma; | |
6095 | bfd_vma suggested_lma; | |
6096 | unsigned int j; | |
dc810e39 | 6097 | bfd_size_type amt; |
0067a569 AM |
6098 | asection *first_section; |
6099 | bfd_boolean first_matching_lma; | |
6100 | bfd_boolean first_suggested_lma; | |
bc67d8a6 NC |
6101 | |
6102 | if (segment->p_type == PT_NULL) | |
6103 | continue; | |
c044fabd | 6104 | |
9f17e2a6 | 6105 | first_section = NULL; |
bc67d8a6 | 6106 | /* Compute how many sections might be placed into this segment. */ |
b5f852ea NC |
6107 | for (section = ibfd->sections, section_count = 0; |
6108 | section != NULL; | |
6109 | section = section->next) | |
9f17e2a6 L |
6110 | { |
6111 | /* Find the first section in the input segment, which may be | |
6112 | removed from the corresponding output segment. */ | |
6113 | if (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed)) | |
6114 | { | |
6115 | if (first_section == NULL) | |
6116 | first_section = section; | |
6117 | if (section->output_section != NULL) | |
6118 | ++section_count; | |
6119 | } | |
6120 | } | |
811072d8 | 6121 | |
b5f852ea NC |
6122 | /* Allocate a segment map big enough to contain |
6123 | all of the sections we have selected. */ | |
dc810e39 AM |
6124 | amt = sizeof (struct elf_segment_map); |
6125 | amt += ((bfd_size_type) section_count - 1) * sizeof (asection *); | |
a50b1753 | 6126 | map = (struct elf_segment_map *) bfd_zalloc (obfd, amt); |
bc67d8a6 | 6127 | if (map == NULL) |
b34976b6 | 6128 | return FALSE; |
252b5132 RH |
6129 | |
6130 | /* Initialise the fields of the segment map. Default to | |
6131 | using the physical address of the segment in the input BFD. */ | |
0067a569 AM |
6132 | map->next = NULL; |
6133 | map->p_type = segment->p_type; | |
6134 | map->p_flags = segment->p_flags; | |
bc67d8a6 | 6135 | map->p_flags_valid = 1; |
55d55ac7 | 6136 | |
9f17e2a6 L |
6137 | /* If the first section in the input segment is removed, there is |
6138 | no need to preserve segment physical address in the corresponding | |
6139 | output segment. */ | |
945c025a | 6140 | if (!first_section || first_section->output_section != NULL) |
9f17e2a6 L |
6141 | { |
6142 | map->p_paddr = segment->p_paddr; | |
5c44b38e | 6143 | map->p_paddr_valid = p_paddr_valid; |
9f17e2a6 | 6144 | } |
252b5132 RH |
6145 | |
6146 | /* Determine if this segment contains the ELF file header | |
6147 | and if it contains the program headers themselves. */ | |
bc67d8a6 NC |
6148 | map->includes_filehdr = (segment->p_offset == 0 |
6149 | && segment->p_filesz >= iehdr->e_ehsize); | |
bc67d8a6 | 6150 | map->includes_phdrs = 0; |
252b5132 | 6151 | |
0067a569 | 6152 | if (!phdr_included || segment->p_type != PT_LOAD) |
252b5132 | 6153 | { |
bc67d8a6 NC |
6154 | map->includes_phdrs = |
6155 | (segment->p_offset <= (bfd_vma) iehdr->e_phoff | |
6156 | && (segment->p_offset + segment->p_filesz | |
252b5132 RH |
6157 | >= ((bfd_vma) iehdr->e_phoff |
6158 | + iehdr->e_phnum * iehdr->e_phentsize))); | |
c044fabd | 6159 | |
bc67d8a6 | 6160 | if (segment->p_type == PT_LOAD && map->includes_phdrs) |
b34976b6 | 6161 | phdr_included = TRUE; |
252b5132 RH |
6162 | } |
6163 | ||
bc67d8a6 | 6164 | if (section_count == 0) |
252b5132 RH |
6165 | { |
6166 | /* Special segments, such as the PT_PHDR segment, may contain | |
6167 | no sections, but ordinary, loadable segments should contain | |
1ed89aa9 NC |
6168 | something. They are allowed by the ELF spec however, so only |
6169 | a warning is produced. */ | |
bc67d8a6 | 6170 | if (segment->p_type == PT_LOAD) |
c86934ce NC |
6171 | (*_bfd_error_handler) (_("\ |
6172 | %B: warning: Empty loadable segment detected, is this intentional ?"), | |
0067a569 | 6173 | ibfd); |
252b5132 | 6174 | |
bc67d8a6 | 6175 | map->count = 0; |
c044fabd KH |
6176 | *pointer_to_map = map; |
6177 | pointer_to_map = &map->next; | |
252b5132 RH |
6178 | |
6179 | continue; | |
6180 | } | |
6181 | ||
6182 | /* Now scan the sections in the input BFD again and attempt | |
6183 | to add their corresponding output sections to the segment map. | |
6184 | The problem here is how to handle an output section which has | |
6185 | been moved (ie had its LMA changed). There are four possibilities: | |
6186 | ||
6187 | 1. None of the sections have been moved. | |
6188 | In this case we can continue to use the segment LMA from the | |
6189 | input BFD. | |
6190 | ||
6191 | 2. All of the sections have been moved by the same amount. | |
6192 | In this case we can change the segment's LMA to match the LMA | |
6193 | of the first section. | |
6194 | ||
6195 | 3. Some of the sections have been moved, others have not. | |
6196 | In this case those sections which have not been moved can be | |
6197 | placed in the current segment which will have to have its size, | |
6198 | and possibly its LMA changed, and a new segment or segments will | |
6199 | have to be created to contain the other sections. | |
6200 | ||
b5f852ea | 6201 | 4. The sections have been moved, but not by the same amount. |
252b5132 RH |
6202 | In this case we can change the segment's LMA to match the LMA |
6203 | of the first section and we will have to create a new segment | |
6204 | or segments to contain the other sections. | |
6205 | ||
6206 | In order to save time, we allocate an array to hold the section | |
6207 | pointers that we are interested in. As these sections get assigned | |
6208 | to a segment, they are removed from this array. */ | |
6209 | ||
a50b1753 | 6210 | sections = (asection **) bfd_malloc2 (section_count, sizeof (asection *)); |
252b5132 | 6211 | if (sections == NULL) |
b34976b6 | 6212 | return FALSE; |
252b5132 RH |
6213 | |
6214 | /* Step One: Scan for segment vs section LMA conflicts. | |
6215 | Also add the sections to the section array allocated above. | |
6216 | Also add the sections to the current segment. In the common | |
6217 | case, where the sections have not been moved, this means that | |
6218 | we have completely filled the segment, and there is nothing | |
6219 | more to do. */ | |
252b5132 | 6220 | isec = 0; |
72730e0c | 6221 | matching_lma = 0; |
252b5132 | 6222 | suggested_lma = 0; |
0067a569 AM |
6223 | first_matching_lma = TRUE; |
6224 | first_suggested_lma = TRUE; | |
252b5132 | 6225 | |
147d51c2 | 6226 | for (section = ibfd->sections; |
bc67d8a6 NC |
6227 | section != NULL; |
6228 | section = section->next) | |
147d51c2 L |
6229 | if (section == first_section) |
6230 | break; | |
6231 | ||
6232 | for (j = 0; section != NULL; section = section->next) | |
252b5132 | 6233 | { |
caf47ea6 | 6234 | if (INCLUDE_SECTION_IN_SEGMENT (section, segment, bed)) |
c0f7859b | 6235 | { |
bc67d8a6 NC |
6236 | output_section = section->output_section; |
6237 | ||
0067a569 | 6238 | sections[j++] = section; |
252b5132 RH |
6239 | |
6240 | /* The Solaris native linker always sets p_paddr to 0. | |
6241 | We try to catch that case here, and set it to the | |
5e8d7549 NC |
6242 | correct value. Note - some backends require that |
6243 | p_paddr be left as zero. */ | |
5c44b38e | 6244 | if (!p_paddr_valid |
4455705d | 6245 | && segment->p_vaddr != 0 |
0067a569 | 6246 | && !bed->want_p_paddr_set_to_zero |
252b5132 | 6247 | && isec == 0 |
bc67d8a6 | 6248 | && output_section->lma != 0 |
0067a569 AM |
6249 | && output_section->vma == (segment->p_vaddr |
6250 | + (map->includes_filehdr | |
6251 | ? iehdr->e_ehsize | |
6252 | : 0) | |
6253 | + (map->includes_phdrs | |
6254 | ? (iehdr->e_phnum | |
6255 | * iehdr->e_phentsize) | |
6256 | : 0))) | |
bc67d8a6 | 6257 | map->p_paddr = segment->p_vaddr; |
252b5132 RH |
6258 | |
6259 | /* Match up the physical address of the segment with the | |
6260 | LMA address of the output section. */ | |
bc67d8a6 | 6261 | if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr) |
5e8d7549 | 6262 | || IS_COREFILE_NOTE (segment, section) |
0067a569 AM |
6263 | || (bed->want_p_paddr_set_to_zero |
6264 | && IS_CONTAINED_BY_VMA (output_section, segment))) | |
252b5132 | 6265 | { |
0067a569 AM |
6266 | if (first_matching_lma || output_section->lma < matching_lma) |
6267 | { | |
6268 | matching_lma = output_section->lma; | |
6269 | first_matching_lma = FALSE; | |
6270 | } | |
252b5132 RH |
6271 | |
6272 | /* We assume that if the section fits within the segment | |
bc67d8a6 | 6273 | then it does not overlap any other section within that |
252b5132 | 6274 | segment. */ |
0067a569 AM |
6275 | map->sections[isec++] = output_section; |
6276 | } | |
6277 | else if (first_suggested_lma) | |
6278 | { | |
6279 | suggested_lma = output_section->lma; | |
6280 | first_suggested_lma = FALSE; | |
252b5132 | 6281 | } |
147d51c2 L |
6282 | |
6283 | if (j == section_count) | |
6284 | break; | |
252b5132 RH |
6285 | } |
6286 | } | |
6287 | ||
bc67d8a6 | 6288 | BFD_ASSERT (j == section_count); |
252b5132 RH |
6289 | |
6290 | /* Step Two: Adjust the physical address of the current segment, | |
6291 | if necessary. */ | |
bc67d8a6 | 6292 | if (isec == section_count) |
252b5132 RH |
6293 | { |
6294 | /* All of the sections fitted within the segment as currently | |
6295 | specified. This is the default case. Add the segment to | |
6296 | the list of built segments and carry on to process the next | |
6297 | program header in the input BFD. */ | |
bc67d8a6 | 6298 | map->count = section_count; |
c044fabd KH |
6299 | *pointer_to_map = map; |
6300 | pointer_to_map = &map->next; | |
08a40648 | 6301 | |
5c44b38e AM |
6302 | if (p_paddr_valid |
6303 | && !bed->want_p_paddr_set_to_zero | |
147d51c2 | 6304 | && matching_lma != map->p_paddr |
5c44b38e AM |
6305 | && !map->includes_filehdr |
6306 | && !map->includes_phdrs) | |
3271a814 NS |
6307 | /* There is some padding before the first section in the |
6308 | segment. So, we must account for that in the output | |
6309 | segment's vma. */ | |
6310 | map->p_vaddr_offset = matching_lma - map->p_paddr; | |
08a40648 | 6311 | |
252b5132 RH |
6312 | free (sections); |
6313 | continue; | |
6314 | } | |
252b5132 RH |
6315 | else |
6316 | { | |
0067a569 | 6317 | if (!first_matching_lma) |
72730e0c AM |
6318 | { |
6319 | /* At least one section fits inside the current segment. | |
6320 | Keep it, but modify its physical address to match the | |
6321 | LMA of the first section that fitted. */ | |
bc67d8a6 | 6322 | map->p_paddr = matching_lma; |
72730e0c AM |
6323 | } |
6324 | else | |
6325 | { | |
6326 | /* None of the sections fitted inside the current segment. | |
6327 | Change the current segment's physical address to match | |
6328 | the LMA of the first section. */ | |
bc67d8a6 | 6329 | map->p_paddr = suggested_lma; |
72730e0c AM |
6330 | } |
6331 | ||
bc67d8a6 NC |
6332 | /* Offset the segment physical address from the lma |
6333 | to allow for space taken up by elf headers. */ | |
6334 | if (map->includes_filehdr) | |
010c8431 AM |
6335 | { |
6336 | if (map->p_paddr >= iehdr->e_ehsize) | |
6337 | map->p_paddr -= iehdr->e_ehsize; | |
6338 | else | |
6339 | { | |
6340 | map->includes_filehdr = FALSE; | |
6341 | map->includes_phdrs = FALSE; | |
6342 | } | |
6343 | } | |
252b5132 | 6344 | |
bc67d8a6 NC |
6345 | if (map->includes_phdrs) |
6346 | { | |
010c8431 AM |
6347 | if (map->p_paddr >= iehdr->e_phnum * iehdr->e_phentsize) |
6348 | { | |
6349 | map->p_paddr -= iehdr->e_phnum * iehdr->e_phentsize; | |
6350 | ||
6351 | /* iehdr->e_phnum is just an estimate of the number | |
6352 | of program headers that we will need. Make a note | |
6353 | here of the number we used and the segment we chose | |
6354 | to hold these headers, so that we can adjust the | |
6355 | offset when we know the correct value. */ | |
6356 | phdr_adjust_num = iehdr->e_phnum; | |
6357 | phdr_adjust_seg = map; | |
6358 | } | |
6359 | else | |
6360 | map->includes_phdrs = FALSE; | |
bc67d8a6 | 6361 | } |
252b5132 RH |
6362 | } |
6363 | ||
6364 | /* Step Three: Loop over the sections again, this time assigning | |
caf47ea6 | 6365 | those that fit to the current segment and removing them from the |
252b5132 RH |
6366 | sections array; but making sure not to leave large gaps. Once all |
6367 | possible sections have been assigned to the current segment it is | |
6368 | added to the list of built segments and if sections still remain | |
6369 | to be assigned, a new segment is constructed before repeating | |
6370 | the loop. */ | |
6371 | isec = 0; | |
6372 | do | |
6373 | { | |
bc67d8a6 | 6374 | map->count = 0; |
252b5132 | 6375 | suggested_lma = 0; |
0067a569 | 6376 | first_suggested_lma = TRUE; |
252b5132 RH |
6377 | |
6378 | /* Fill the current segment with sections that fit. */ | |
bc67d8a6 | 6379 | for (j = 0; j < section_count; j++) |
252b5132 | 6380 | { |
bc67d8a6 | 6381 | section = sections[j]; |
252b5132 | 6382 | |
bc67d8a6 | 6383 | if (section == NULL) |
252b5132 RH |
6384 | continue; |
6385 | ||
bc67d8a6 | 6386 | output_section = section->output_section; |
252b5132 | 6387 | |
bc67d8a6 | 6388 | BFD_ASSERT (output_section != NULL); |
c044fabd | 6389 | |
bc67d8a6 NC |
6390 | if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr) |
6391 | || IS_COREFILE_NOTE (segment, section)) | |
252b5132 | 6392 | { |
bc67d8a6 | 6393 | if (map->count == 0) |
252b5132 RH |
6394 | { |
6395 | /* If the first section in a segment does not start at | |
bc67d8a6 NC |
6396 | the beginning of the segment, then something is |
6397 | wrong. */ | |
0067a569 AM |
6398 | if (output_section->lma |
6399 | != (map->p_paddr | |
6400 | + (map->includes_filehdr ? iehdr->e_ehsize : 0) | |
6401 | + (map->includes_phdrs | |
6402 | ? iehdr->e_phnum * iehdr->e_phentsize | |
6403 | : 0))) | |
252b5132 RH |
6404 | abort (); |
6405 | } | |
6406 | else | |
6407 | { | |
0067a569 | 6408 | asection *prev_sec; |
252b5132 | 6409 | |
bc67d8a6 | 6410 | prev_sec = map->sections[map->count - 1]; |
252b5132 RH |
6411 | |
6412 | /* If the gap between the end of the previous section | |
bc67d8a6 NC |
6413 | and the start of this section is more than |
6414 | maxpagesize then we need to start a new segment. */ | |
eea6121a | 6415 | if ((BFD_ALIGN (prev_sec->lma + prev_sec->size, |
079e9a2f | 6416 | maxpagesize) |
caf47ea6 | 6417 | < BFD_ALIGN (output_section->lma, maxpagesize)) |
0067a569 | 6418 | || (prev_sec->lma + prev_sec->size |
079e9a2f | 6419 | > output_section->lma)) |
252b5132 | 6420 | { |
0067a569 AM |
6421 | if (first_suggested_lma) |
6422 | { | |
6423 | suggested_lma = output_section->lma; | |
6424 | first_suggested_lma = FALSE; | |
6425 | } | |
252b5132 RH |
6426 | |
6427 | continue; | |
6428 | } | |
6429 | } | |
6430 | ||
bc67d8a6 | 6431 | map->sections[map->count++] = output_section; |
252b5132 RH |
6432 | ++isec; |
6433 | sections[j] = NULL; | |
b34976b6 | 6434 | section->segment_mark = TRUE; |
252b5132 | 6435 | } |
0067a569 AM |
6436 | else if (first_suggested_lma) |
6437 | { | |
6438 | suggested_lma = output_section->lma; | |
6439 | first_suggested_lma = FALSE; | |
6440 | } | |
252b5132 RH |
6441 | } |
6442 | ||
bc67d8a6 | 6443 | BFD_ASSERT (map->count > 0); |
252b5132 RH |
6444 | |
6445 | /* Add the current segment to the list of built segments. */ | |
c044fabd KH |
6446 | *pointer_to_map = map; |
6447 | pointer_to_map = &map->next; | |
252b5132 | 6448 | |
bc67d8a6 | 6449 | if (isec < section_count) |
252b5132 RH |
6450 | { |
6451 | /* We still have not allocated all of the sections to | |
6452 | segments. Create a new segment here, initialise it | |
6453 | and carry on looping. */ | |
dc810e39 AM |
6454 | amt = sizeof (struct elf_segment_map); |
6455 | amt += ((bfd_size_type) section_count - 1) * sizeof (asection *); | |
5964fc3a | 6456 | map = (struct elf_segment_map *) bfd_zalloc (obfd, amt); |
bc67d8a6 | 6457 | if (map == NULL) |
5ed6aba4 NC |
6458 | { |
6459 | free (sections); | |
6460 | return FALSE; | |
6461 | } | |
252b5132 RH |
6462 | |
6463 | /* Initialise the fields of the segment map. Set the physical | |
6464 | physical address to the LMA of the first section that has | |
6465 | not yet been assigned. */ | |
0067a569 AM |
6466 | map->next = NULL; |
6467 | map->p_type = segment->p_type; | |
6468 | map->p_flags = segment->p_flags; | |
6469 | map->p_flags_valid = 1; | |
6470 | map->p_paddr = suggested_lma; | |
5c44b38e | 6471 | map->p_paddr_valid = p_paddr_valid; |
bc67d8a6 | 6472 | map->includes_filehdr = 0; |
0067a569 | 6473 | map->includes_phdrs = 0; |
252b5132 RH |
6474 | } |
6475 | } | |
bc67d8a6 | 6476 | while (isec < section_count); |
252b5132 RH |
6477 | |
6478 | free (sections); | |
6479 | } | |
6480 | ||
12bd6957 | 6481 | elf_seg_map (obfd) = map_first; |
bc67d8a6 NC |
6482 | |
6483 | /* If we had to estimate the number of program headers that were | |
9ad5cbcf | 6484 | going to be needed, then check our estimate now and adjust |
bc67d8a6 NC |
6485 | the offset if necessary. */ |
6486 | if (phdr_adjust_seg != NULL) | |
6487 | { | |
6488 | unsigned int count; | |
c044fabd | 6489 | |
bc67d8a6 | 6490 | for (count = 0, map = map_first; map != NULL; map = map->next) |
c044fabd | 6491 | count++; |
252b5132 | 6492 | |
bc67d8a6 NC |
6493 | if (count > phdr_adjust_num) |
6494 | phdr_adjust_seg->p_paddr | |
6495 | -= (count - phdr_adjust_num) * iehdr->e_phentsize; | |
6496 | } | |
c044fabd | 6497 | |
bc67d8a6 | 6498 | #undef SEGMENT_END |
eecdbe52 | 6499 | #undef SECTION_SIZE |
bc67d8a6 NC |
6500 | #undef IS_CONTAINED_BY_VMA |
6501 | #undef IS_CONTAINED_BY_LMA | |
0efc80c8 | 6502 | #undef IS_NOTE |
252b5132 | 6503 | #undef IS_COREFILE_NOTE |
bc67d8a6 | 6504 | #undef IS_SOLARIS_PT_INTERP |
9f17e2a6 | 6505 | #undef IS_SECTION_IN_INPUT_SEGMENT |
bc67d8a6 NC |
6506 | #undef INCLUDE_SECTION_IN_SEGMENT |
6507 | #undef SEGMENT_AFTER_SEGMENT | |
6508 | #undef SEGMENT_OVERLAPS | |
b34976b6 | 6509 | return TRUE; |
252b5132 RH |
6510 | } |
6511 | ||
84d1d650 L |
6512 | /* Copy ELF program header information. */ |
6513 | ||
6514 | static bfd_boolean | |
6515 | copy_elf_program_header (bfd *ibfd, bfd *obfd) | |
6516 | { | |
6517 | Elf_Internal_Ehdr *iehdr; | |
6518 | struct elf_segment_map *map; | |
6519 | struct elf_segment_map *map_first; | |
6520 | struct elf_segment_map **pointer_to_map; | |
6521 | Elf_Internal_Phdr *segment; | |
6522 | unsigned int i; | |
6523 | unsigned int num_segments; | |
6524 | bfd_boolean phdr_included = FALSE; | |
88967714 | 6525 | bfd_boolean p_paddr_valid; |
84d1d650 L |
6526 | |
6527 | iehdr = elf_elfheader (ibfd); | |
6528 | ||
6529 | map_first = NULL; | |
6530 | pointer_to_map = &map_first; | |
6531 | ||
88967714 AM |
6532 | /* If all the segment p_paddr fields are zero, don't set |
6533 | map->p_paddr_valid. */ | |
6534 | p_paddr_valid = FALSE; | |
84d1d650 | 6535 | num_segments = elf_elfheader (ibfd)->e_phnum; |
88967714 AM |
6536 | for (i = 0, segment = elf_tdata (ibfd)->phdr; |
6537 | i < num_segments; | |
6538 | i++, segment++) | |
6539 | if (segment->p_paddr != 0) | |
6540 | { | |
6541 | p_paddr_valid = TRUE; | |
6542 | break; | |
6543 | } | |
6544 | ||
84d1d650 L |
6545 | for (i = 0, segment = elf_tdata (ibfd)->phdr; |
6546 | i < num_segments; | |
6547 | i++, segment++) | |
6548 | { | |
6549 | asection *section; | |
6550 | unsigned int section_count; | |
6551 | bfd_size_type amt; | |
6552 | Elf_Internal_Shdr *this_hdr; | |
53020534 | 6553 | asection *first_section = NULL; |
a76e6f2f | 6554 | asection *lowest_section; |
84d1d650 | 6555 | |
84d1d650 L |
6556 | /* Compute how many sections are in this segment. */ |
6557 | for (section = ibfd->sections, section_count = 0; | |
6558 | section != NULL; | |
6559 | section = section->next) | |
6560 | { | |
6561 | this_hdr = &(elf_section_data(section)->this_hdr); | |
f4638467 | 6562 | if (ELF_SECTION_IN_SEGMENT (this_hdr, segment)) |
3271a814 | 6563 | { |
a76e6f2f AM |
6564 | if (first_section == NULL) |
6565 | first_section = section; | |
3271a814 NS |
6566 | section_count++; |
6567 | } | |
84d1d650 L |
6568 | } |
6569 | ||
6570 | /* Allocate a segment map big enough to contain | |
6571 | all of the sections we have selected. */ | |
6572 | amt = sizeof (struct elf_segment_map); | |
6573 | if (section_count != 0) | |
6574 | amt += ((bfd_size_type) section_count - 1) * sizeof (asection *); | |
a50b1753 | 6575 | map = (struct elf_segment_map *) bfd_zalloc (obfd, amt); |
84d1d650 L |
6576 | if (map == NULL) |
6577 | return FALSE; | |
6578 | ||
6579 | /* Initialize the fields of the output segment map with the | |
6580 | input segment. */ | |
6581 | map->next = NULL; | |
6582 | map->p_type = segment->p_type; | |
6583 | map->p_flags = segment->p_flags; | |
6584 | map->p_flags_valid = 1; | |
6585 | map->p_paddr = segment->p_paddr; | |
88967714 | 6586 | map->p_paddr_valid = p_paddr_valid; |
3f570048 AM |
6587 | map->p_align = segment->p_align; |
6588 | map->p_align_valid = 1; | |
3271a814 | 6589 | map->p_vaddr_offset = 0; |
84d1d650 | 6590 | |
04c3a755 NS |
6591 | if (map->p_type == PT_GNU_RELRO |
6592 | || map->p_type == PT_GNU_STACK) | |
b10a8ae0 L |
6593 | { |
6594 | /* The PT_GNU_RELRO segment may contain the first a few | |
6595 | bytes in the .got.plt section even if the whole .got.plt | |
6596 | section isn't in the PT_GNU_RELRO segment. We won't | |
04c3a755 NS |
6597 | change the size of the PT_GNU_RELRO segment. |
6598 | Similarly, PT_GNU_STACK size is significant on uclinux | |
6599 | systems. */ | |
9433b9b1 | 6600 | map->p_size = segment->p_memsz; |
b10a8ae0 L |
6601 | map->p_size_valid = 1; |
6602 | } | |
6603 | ||
84d1d650 L |
6604 | /* Determine if this segment contains the ELF file header |
6605 | and if it contains the program headers themselves. */ | |
6606 | map->includes_filehdr = (segment->p_offset == 0 | |
6607 | && segment->p_filesz >= iehdr->e_ehsize); | |
6608 | ||
6609 | map->includes_phdrs = 0; | |
6610 | if (! phdr_included || segment->p_type != PT_LOAD) | |
6611 | { | |
6612 | map->includes_phdrs = | |
6613 | (segment->p_offset <= (bfd_vma) iehdr->e_phoff | |
6614 | && (segment->p_offset + segment->p_filesz | |
6615 | >= ((bfd_vma) iehdr->e_phoff | |
6616 | + iehdr->e_phnum * iehdr->e_phentsize))); | |
6617 | ||
6618 | if (segment->p_type == PT_LOAD && map->includes_phdrs) | |
6619 | phdr_included = TRUE; | |
6620 | } | |
6621 | ||
bbefd0a9 | 6622 | lowest_section = NULL; |
84d1d650 L |
6623 | if (section_count != 0) |
6624 | { | |
6625 | unsigned int isec = 0; | |
6626 | ||
53020534 | 6627 | for (section = first_section; |
84d1d650 L |
6628 | section != NULL; |
6629 | section = section->next) | |
6630 | { | |
6631 | this_hdr = &(elf_section_data(section)->this_hdr); | |
f4638467 | 6632 | if (ELF_SECTION_IN_SEGMENT (this_hdr, segment)) |
53020534 L |
6633 | { |
6634 | map->sections[isec++] = section->output_section; | |
a76e6f2f AM |
6635 | if ((section->flags & SEC_ALLOC) != 0) |
6636 | { | |
6637 | bfd_vma seg_off; | |
6638 | ||
bbefd0a9 AM |
6639 | if (lowest_section == NULL |
6640 | || section->lma < lowest_section->lma) | |
fb8a5684 AM |
6641 | lowest_section = section; |
6642 | ||
a76e6f2f AM |
6643 | /* Section lmas are set up from PT_LOAD header |
6644 | p_paddr in _bfd_elf_make_section_from_shdr. | |
6645 | If this header has a p_paddr that disagrees | |
6646 | with the section lma, flag the p_paddr as | |
6647 | invalid. */ | |
6648 | if ((section->flags & SEC_LOAD) != 0) | |
6649 | seg_off = this_hdr->sh_offset - segment->p_offset; | |
6650 | else | |
6651 | seg_off = this_hdr->sh_addr - segment->p_vaddr; | |
6652 | if (section->lma - segment->p_paddr != seg_off) | |
6653 | map->p_paddr_valid = FALSE; | |
6654 | } | |
53020534 L |
6655 | if (isec == section_count) |
6656 | break; | |
6657 | } | |
84d1d650 L |
6658 | } |
6659 | } | |
6660 | ||
a76e6f2f AM |
6661 | if (map->includes_filehdr && lowest_section != NULL) |
6662 | /* We need to keep the space used by the headers fixed. */ | |
6663 | map->header_size = lowest_section->vma - segment->p_vaddr; | |
d324f6d6 | 6664 | |
a76e6f2f AM |
6665 | if (!map->includes_phdrs |
6666 | && !map->includes_filehdr | |
6667 | && map->p_paddr_valid) | |
6668 | /* There is some other padding before the first section. */ | |
6669 | map->p_vaddr_offset = ((lowest_section ? lowest_section->lma : 0) | |
6670 | - segment->p_paddr); | |
6671 | ||
84d1d650 L |
6672 | map->count = section_count; |
6673 | *pointer_to_map = map; | |
6674 | pointer_to_map = &map->next; | |
6675 | } | |
6676 | ||
12bd6957 | 6677 | elf_seg_map (obfd) = map_first; |
84d1d650 L |
6678 | return TRUE; |
6679 | } | |
6680 | ||
6681 | /* Copy private BFD data. This copies or rewrites ELF program header | |
6682 | information. */ | |
6683 | ||
6684 | static bfd_boolean | |
6685 | copy_private_bfd_data (bfd *ibfd, bfd *obfd) | |
6686 | { | |
84d1d650 L |
6687 | if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour |
6688 | || bfd_get_flavour (obfd) != bfd_target_elf_flavour) | |
6689 | return TRUE; | |
6690 | ||
6691 | if (elf_tdata (ibfd)->phdr == NULL) | |
6692 | return TRUE; | |
6693 | ||
6694 | if (ibfd->xvec == obfd->xvec) | |
6695 | { | |
cb3ff1e5 NC |
6696 | /* Check to see if any sections in the input BFD |
6697 | covered by ELF program header have changed. */ | |
d55ce4e2 | 6698 | Elf_Internal_Phdr *segment; |
84d1d650 L |
6699 | asection *section, *osec; |
6700 | unsigned int i, num_segments; | |
6701 | Elf_Internal_Shdr *this_hdr; | |
147d51c2 L |
6702 | const struct elf_backend_data *bed; |
6703 | ||
6704 | bed = get_elf_backend_data (ibfd); | |
6705 | ||
6706 | /* Regenerate the segment map if p_paddr is set to 0. */ | |
6707 | if (bed->want_p_paddr_set_to_zero) | |
6708 | goto rewrite; | |
84d1d650 L |
6709 | |
6710 | /* Initialize the segment mark field. */ | |
6711 | for (section = obfd->sections; section != NULL; | |
6712 | section = section->next) | |
6713 | section->segment_mark = FALSE; | |
6714 | ||
6715 | num_segments = elf_elfheader (ibfd)->e_phnum; | |
6716 | for (i = 0, segment = elf_tdata (ibfd)->phdr; | |
6717 | i < num_segments; | |
6718 | i++, segment++) | |
6719 | { | |
5f6999aa NC |
6720 | /* PR binutils/3535. The Solaris linker always sets the p_paddr |
6721 | and p_memsz fields of special segments (DYNAMIC, INTERP) to 0 | |
6722 | which severly confuses things, so always regenerate the segment | |
6723 | map in this case. */ | |
6724 | if (segment->p_paddr == 0 | |
6725 | && segment->p_memsz == 0 | |
6726 | && (segment->p_type == PT_INTERP || segment->p_type == PT_DYNAMIC)) | |
cb3ff1e5 | 6727 | goto rewrite; |
5f6999aa | 6728 | |
84d1d650 L |
6729 | for (section = ibfd->sections; |
6730 | section != NULL; section = section->next) | |
6731 | { | |
6732 | /* We mark the output section so that we know it comes | |
6733 | from the input BFD. */ | |
6734 | osec = section->output_section; | |
6735 | if (osec) | |
6736 | osec->segment_mark = TRUE; | |
6737 | ||
6738 | /* Check if this section is covered by the segment. */ | |
6739 | this_hdr = &(elf_section_data(section)->this_hdr); | |
f4638467 | 6740 | if (ELF_SECTION_IN_SEGMENT (this_hdr, segment)) |
84d1d650 L |
6741 | { |
6742 | /* FIXME: Check if its output section is changed or | |
6743 | removed. What else do we need to check? */ | |
6744 | if (osec == NULL | |
6745 | || section->flags != osec->flags | |
6746 | || section->lma != osec->lma | |
6747 | || section->vma != osec->vma | |
6748 | || section->size != osec->size | |
6749 | || section->rawsize != osec->rawsize | |
6750 | || section->alignment_power != osec->alignment_power) | |
6751 | goto rewrite; | |
6752 | } | |
6753 | } | |
6754 | } | |
6755 | ||
cb3ff1e5 | 6756 | /* Check to see if any output section do not come from the |
84d1d650 L |
6757 | input BFD. */ |
6758 | for (section = obfd->sections; section != NULL; | |
6759 | section = section->next) | |
6760 | { | |
6761 | if (section->segment_mark == FALSE) | |
6762 | goto rewrite; | |
6763 | else | |
6764 | section->segment_mark = FALSE; | |
6765 | } | |
6766 | ||
6767 | return copy_elf_program_header (ibfd, obfd); | |
6768 | } | |
6769 | ||
6770 | rewrite: | |
f1d85785 L |
6771 | if (ibfd->xvec == obfd->xvec) |
6772 | { | |
6773 | /* When rewriting program header, set the output maxpagesize to | |
6774 | the maximum alignment of input PT_LOAD segments. */ | |
6775 | Elf_Internal_Phdr *segment; | |
6776 | unsigned int i; | |
6777 | unsigned int num_segments = elf_elfheader (ibfd)->e_phnum; | |
6778 | bfd_vma maxpagesize = 0; | |
6779 | ||
6780 | for (i = 0, segment = elf_tdata (ibfd)->phdr; | |
6781 | i < num_segments; | |
6782 | i++, segment++) | |
6783 | if (segment->p_type == PT_LOAD | |
6784 | && maxpagesize < segment->p_align) | |
c86934ce NC |
6785 | { |
6786 | /* PR 17512: file: f17299af. */ | |
6787 | if (segment->p_align > (bfd_vma) 1 << ((sizeof (bfd_vma) * 8) - 2)) | |
6788 | (*_bfd_error_handler) (_("\ | |
6789 | %B: warning: segment alignment of 0x%llx is too large"), | |
6790 | ibfd, (long long) segment->p_align); | |
6791 | else | |
6792 | maxpagesize = segment->p_align; | |
6793 | } | |
f1d85785 L |
6794 | |
6795 | if (maxpagesize != get_elf_backend_data (obfd)->maxpagesize) | |
6796 | bfd_emul_set_maxpagesize (bfd_get_target (obfd), maxpagesize); | |
6797 | } | |
6798 | ||
84d1d650 L |
6799 | return rewrite_elf_program_header (ibfd, obfd); |
6800 | } | |
6801 | ||
ccd2ec6a L |
6802 | /* Initialize private output section information from input section. */ |
6803 | ||
6804 | bfd_boolean | |
6805 | _bfd_elf_init_private_section_data (bfd *ibfd, | |
6806 | asection *isec, | |
6807 | bfd *obfd, | |
6808 | asection *osec, | |
6809 | struct bfd_link_info *link_info) | |
6810 | ||
6811 | { | |
6812 | Elf_Internal_Shdr *ihdr, *ohdr; | |
dfa7b0b8 | 6813 | bfd_boolean final_link = link_info != NULL && !link_info->relocatable; |
ccd2ec6a L |
6814 | |
6815 | if (ibfd->xvec->flavour != bfd_target_elf_flavour | |
6816 | || obfd->xvec->flavour != bfd_target_elf_flavour) | |
6817 | return TRUE; | |
6818 | ||
ba85c43e NC |
6819 | BFD_ASSERT (elf_section_data (osec) != NULL); |
6820 | ||
dfa7b0b8 AM |
6821 | /* For objcopy and relocatable link, don't copy the output ELF |
6822 | section type from input if the output BFD section flags have been | |
6823 | set to something different. For a final link allow some flags | |
6824 | that the linker clears to differ. */ | |
42bb2e33 | 6825 | if (elf_section_type (osec) == SHT_NULL |
dfa7b0b8 AM |
6826 | && (osec->flags == isec->flags |
6827 | || (final_link | |
6828 | && ((osec->flags ^ isec->flags) | |
0814be7d | 6829 | & ~(SEC_LINK_ONCE | SEC_LINK_DUPLICATES | SEC_RELOC)) == 0))) |
42bb2e33 | 6830 | elf_section_type (osec) = elf_section_type (isec); |
d270463e L |
6831 | |
6832 | /* FIXME: Is this correct for all OS/PROC specific flags? */ | |
6833 | elf_section_flags (osec) |= (elf_section_flags (isec) | |
6834 | & (SHF_MASKOS | SHF_MASKPROC)); | |
ccd2ec6a L |
6835 | |
6836 | /* Set things up for objcopy and relocatable link. The output | |
6837 | SHT_GROUP section will have its elf_next_in_group pointing back | |
6838 | to the input group members. Ignore linker created group section. | |
6839 | See elfNN_ia64_object_p in elfxx-ia64.c. */ | |
dfa7b0b8 | 6840 | if (!final_link) |
ccd2ec6a L |
6841 | { |
6842 | if (elf_sec_group (isec) == NULL | |
6843 | || (elf_sec_group (isec)->flags & SEC_LINKER_CREATED) == 0) | |
6844 | { | |
6845 | if (elf_section_flags (isec) & SHF_GROUP) | |
6846 | elf_section_flags (osec) |= SHF_GROUP; | |
6847 | elf_next_in_group (osec) = elf_next_in_group (isec); | |
9659de1c | 6848 | elf_section_data (osec)->group = elf_section_data (isec)->group; |
ccd2ec6a | 6849 | } |
68f5ff89 L |
6850 | |
6851 | /* If not decompress, preserve SHF_COMPRESSED. */ | |
6852 | if ((ibfd->flags & BFD_DECOMPRESS) == 0) | |
6853 | elf_section_flags (osec) |= (elf_section_flags (isec) | |
6854 | & SHF_COMPRESSED); | |
ccd2ec6a L |
6855 | } |
6856 | ||
6857 | ihdr = &elf_section_data (isec)->this_hdr; | |
6858 | ||
6859 | /* We need to handle elf_linked_to_section for SHF_LINK_ORDER. We | |
6860 | don't use the output section of the linked-to section since it | |
6861 | may be NULL at this point. */ | |
6862 | if ((ihdr->sh_flags & SHF_LINK_ORDER) != 0) | |
6863 | { | |
6864 | ohdr = &elf_section_data (osec)->this_hdr; | |
6865 | ohdr->sh_flags |= SHF_LINK_ORDER; | |
6866 | elf_linked_to_section (osec) = elf_linked_to_section (isec); | |
6867 | } | |
6868 | ||
6869 | osec->use_rela_p = isec->use_rela_p; | |
6870 | ||
6871 | return TRUE; | |
6872 | } | |
6873 | ||
252b5132 RH |
6874 | /* Copy private section information. This copies over the entsize |
6875 | field, and sometimes the info field. */ | |
6876 | ||
b34976b6 | 6877 | bfd_boolean |
217aa764 AM |
6878 | _bfd_elf_copy_private_section_data (bfd *ibfd, |
6879 | asection *isec, | |
6880 | bfd *obfd, | |
6881 | asection *osec) | |
252b5132 RH |
6882 | { |
6883 | Elf_Internal_Shdr *ihdr, *ohdr; | |
6884 | ||
6885 | if (ibfd->xvec->flavour != bfd_target_elf_flavour | |
6886 | || obfd->xvec->flavour != bfd_target_elf_flavour) | |
b34976b6 | 6887 | return TRUE; |
252b5132 | 6888 | |
252b5132 RH |
6889 | ihdr = &elf_section_data (isec)->this_hdr; |
6890 | ohdr = &elf_section_data (osec)->this_hdr; | |
6891 | ||
6892 | ohdr->sh_entsize = ihdr->sh_entsize; | |
6893 | ||
6894 | if (ihdr->sh_type == SHT_SYMTAB | |
6895 | || ihdr->sh_type == SHT_DYNSYM | |
6896 | || ihdr->sh_type == SHT_GNU_verneed | |
6897 | || ihdr->sh_type == SHT_GNU_verdef) | |
6898 | ohdr->sh_info = ihdr->sh_info; | |
6899 | ||
ccd2ec6a L |
6900 | return _bfd_elf_init_private_section_data (ibfd, isec, obfd, osec, |
6901 | NULL); | |
252b5132 RH |
6902 | } |
6903 | ||
d0bf826b AM |
6904 | /* Look at all the SHT_GROUP sections in IBFD, making any adjustments |
6905 | necessary if we are removing either the SHT_GROUP section or any of | |
6906 | the group member sections. DISCARDED is the value that a section's | |
6907 | output_section has if the section will be discarded, NULL when this | |
6908 | function is called from objcopy, bfd_abs_section_ptr when called | |
6909 | from the linker. */ | |
80fccad2 BW |
6910 | |
6911 | bfd_boolean | |
d0bf826b | 6912 | _bfd_elf_fixup_group_sections (bfd *ibfd, asection *discarded) |
80fccad2 | 6913 | { |
30288845 AM |
6914 | asection *isec; |
6915 | ||
30288845 | 6916 | for (isec = ibfd->sections; isec != NULL; isec = isec->next) |
415f38a6 | 6917 | if (elf_section_type (isec) == SHT_GROUP) |
30288845 AM |
6918 | { |
6919 | asection *first = elf_next_in_group (isec); | |
6920 | asection *s = first; | |
d0bf826b AM |
6921 | bfd_size_type removed = 0; |
6922 | ||
30288845 AM |
6923 | while (s != NULL) |
6924 | { | |
415f38a6 AM |
6925 | /* If this member section is being output but the |
6926 | SHT_GROUP section is not, then clear the group info | |
6927 | set up by _bfd_elf_copy_private_section_data. */ | |
d0bf826b AM |
6928 | if (s->output_section != discarded |
6929 | && isec->output_section == discarded) | |
30288845 AM |
6930 | { |
6931 | elf_section_flags (s->output_section) &= ~SHF_GROUP; | |
6932 | elf_group_name (s->output_section) = NULL; | |
6933 | } | |
415f38a6 AM |
6934 | /* Conversely, if the member section is not being output |
6935 | but the SHT_GROUP section is, then adjust its size. */ | |
d0bf826b AM |
6936 | else if (s->output_section == discarded |
6937 | && isec->output_section != discarded) | |
6938 | removed += 4; | |
30288845 AM |
6939 | s = elf_next_in_group (s); |
6940 | if (s == first) | |
6941 | break; | |
6942 | } | |
d0bf826b AM |
6943 | if (removed != 0) |
6944 | { | |
6945 | if (discarded != NULL) | |
6946 | { | |
6947 | /* If we've been called for ld -r, then we need to | |
6948 | adjust the input section size. This function may | |
6949 | be called multiple times, so save the original | |
6950 | size. */ | |
6951 | if (isec->rawsize == 0) | |
6952 | isec->rawsize = isec->size; | |
6953 | isec->size = isec->rawsize - removed; | |
6954 | } | |
6955 | else | |
6956 | { | |
6957 | /* Adjust the output section size when called from | |
6958 | objcopy. */ | |
6959 | isec->output_section->size -= removed; | |
6960 | } | |
6961 | } | |
30288845 AM |
6962 | } |
6963 | ||
80fccad2 BW |
6964 | return TRUE; |
6965 | } | |
6966 | ||
d0bf826b AM |
6967 | /* Copy private header information. */ |
6968 | ||
6969 | bfd_boolean | |
6970 | _bfd_elf_copy_private_header_data (bfd *ibfd, bfd *obfd) | |
6971 | { | |
6972 | if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour | |
6973 | || bfd_get_flavour (obfd) != bfd_target_elf_flavour) | |
6974 | return TRUE; | |
6975 | ||
6976 | /* Copy over private BFD data if it has not already been copied. | |
6977 | This must be done here, rather than in the copy_private_bfd_data | |
6978 | entry point, because the latter is called after the section | |
6979 | contents have been set, which means that the program headers have | |
6980 | already been worked out. */ | |
12bd6957 | 6981 | if (elf_seg_map (obfd) == NULL && elf_tdata (ibfd)->phdr != NULL) |
d0bf826b AM |
6982 | { |
6983 | if (! copy_private_bfd_data (ibfd, obfd)) | |
6984 | return FALSE; | |
6985 | } | |
6986 | ||
6987 | return _bfd_elf_fixup_group_sections (ibfd, NULL); | |
6988 | } | |
6989 | ||
252b5132 RH |
6990 | /* Copy private symbol information. If this symbol is in a section |
6991 | which we did not map into a BFD section, try to map the section | |
6992 | index correctly. We use special macro definitions for the mapped | |
6993 | section indices; these definitions are interpreted by the | |
6994 | swap_out_syms function. */ | |
6995 | ||
9ad5cbcf AM |
6996 | #define MAP_ONESYMTAB (SHN_HIOS + 1) |
6997 | #define MAP_DYNSYMTAB (SHN_HIOS + 2) | |
6998 | #define MAP_STRTAB (SHN_HIOS + 3) | |
6999 | #define MAP_SHSTRTAB (SHN_HIOS + 4) | |
7000 | #define MAP_SYM_SHNDX (SHN_HIOS + 5) | |
252b5132 | 7001 | |
b34976b6 | 7002 | bfd_boolean |
217aa764 AM |
7003 | _bfd_elf_copy_private_symbol_data (bfd *ibfd, |
7004 | asymbol *isymarg, | |
7005 | bfd *obfd, | |
7006 | asymbol *osymarg) | |
252b5132 RH |
7007 | { |
7008 | elf_symbol_type *isym, *osym; | |
7009 | ||
7010 | if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour | |
7011 | || bfd_get_flavour (obfd) != bfd_target_elf_flavour) | |
b34976b6 | 7012 | return TRUE; |
252b5132 RH |
7013 | |
7014 | isym = elf_symbol_from (ibfd, isymarg); | |
7015 | osym = elf_symbol_from (obfd, osymarg); | |
7016 | ||
7017 | if (isym != NULL | |
8424d8f5 | 7018 | && isym->internal_elf_sym.st_shndx != 0 |
252b5132 RH |
7019 | && osym != NULL |
7020 | && bfd_is_abs_section (isym->symbol.section)) | |
7021 | { | |
7022 | unsigned int shndx; | |
7023 | ||
7024 | shndx = isym->internal_elf_sym.st_shndx; | |
7025 | if (shndx == elf_onesymtab (ibfd)) | |
7026 | shndx = MAP_ONESYMTAB; | |
7027 | else if (shndx == elf_dynsymtab (ibfd)) | |
7028 | shndx = MAP_DYNSYMTAB; | |
12bd6957 | 7029 | else if (shndx == elf_strtab_sec (ibfd)) |
252b5132 | 7030 | shndx = MAP_STRTAB; |
12bd6957 | 7031 | else if (shndx == elf_shstrtab_sec (ibfd)) |
252b5132 | 7032 | shndx = MAP_SHSTRTAB; |
12bd6957 | 7033 | else if (shndx == elf_symtab_shndx (ibfd)) |
9ad5cbcf | 7034 | shndx = MAP_SYM_SHNDX; |
252b5132 RH |
7035 | osym->internal_elf_sym.st_shndx = shndx; |
7036 | } | |
7037 | ||
b34976b6 | 7038 | return TRUE; |
252b5132 RH |
7039 | } |
7040 | ||
7041 | /* Swap out the symbols. */ | |
7042 | ||
b34976b6 | 7043 | static bfd_boolean |
217aa764 | 7044 | swap_out_syms (bfd *abfd, |
ef10c3ac | 7045 | struct elf_strtab_hash **sttp, |
217aa764 | 7046 | int relocatable_p) |
252b5132 | 7047 | { |
9c5bfbb7 | 7048 | const struct elf_backend_data *bed; |
079e9a2f AM |
7049 | int symcount; |
7050 | asymbol **syms; | |
ef10c3ac | 7051 | struct elf_strtab_hash *stt; |
079e9a2f | 7052 | Elf_Internal_Shdr *symtab_hdr; |
9ad5cbcf | 7053 | Elf_Internal_Shdr *symtab_shndx_hdr; |
079e9a2f | 7054 | Elf_Internal_Shdr *symstrtab_hdr; |
ef10c3ac | 7055 | struct elf_sym_strtab *symstrtab; |
f075ee0c AM |
7056 | bfd_byte *outbound_syms; |
7057 | bfd_byte *outbound_shndx; | |
ef10c3ac L |
7058 | unsigned long outbound_syms_index; |
7059 | unsigned long outbound_shndx_index; | |
079e9a2f | 7060 | int idx; |
12bd6957 | 7061 | unsigned int num_locals; |
079e9a2f | 7062 | bfd_size_type amt; |
174fd7f9 | 7063 | bfd_boolean name_local_sections; |
252b5132 | 7064 | |
12bd6957 | 7065 | if (!elf_map_symbols (abfd, &num_locals)) |
b34976b6 | 7066 | return FALSE; |
252b5132 | 7067 | |
c044fabd | 7068 | /* Dump out the symtabs. */ |
ef10c3ac | 7069 | stt = _bfd_elf_strtab_init (); |
079e9a2f | 7070 | if (stt == NULL) |
b34976b6 | 7071 | return FALSE; |
252b5132 | 7072 | |
079e9a2f AM |
7073 | bed = get_elf_backend_data (abfd); |
7074 | symcount = bfd_get_symcount (abfd); | |
7075 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; | |
7076 | symtab_hdr->sh_type = SHT_SYMTAB; | |
7077 | symtab_hdr->sh_entsize = bed->s->sizeof_sym; | |
7078 | symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1); | |
12bd6957 | 7079 | symtab_hdr->sh_info = num_locals + 1; |
72de5009 | 7080 | symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align; |
079e9a2f AM |
7081 | |
7082 | symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr; | |
7083 | symstrtab_hdr->sh_type = SHT_STRTAB; | |
7084 | ||
ef10c3ac L |
7085 | /* Allocate buffer to swap out the .strtab section. */ |
7086 | symstrtab = (struct elf_sym_strtab *) bfd_malloc ((symcount + 1) | |
7087 | * sizeof (*symstrtab)); | |
7088 | if (symstrtab == NULL) | |
7089 | { | |
7090 | _bfd_elf_strtab_free (stt); | |
7091 | return FALSE; | |
7092 | } | |
7093 | ||
a50b1753 NC |
7094 | outbound_syms = (bfd_byte *) bfd_alloc2 (abfd, 1 + symcount, |
7095 | bed->s->sizeof_sym); | |
079e9a2f | 7096 | if (outbound_syms == NULL) |
5ed6aba4 | 7097 | { |
ef10c3ac L |
7098 | error_return: |
7099 | _bfd_elf_strtab_free (stt); | |
7100 | free (symstrtab); | |
5ed6aba4 NC |
7101 | return FALSE; |
7102 | } | |
217aa764 | 7103 | symtab_hdr->contents = outbound_syms; |
ef10c3ac | 7104 | outbound_syms_index = 0; |
252b5132 | 7105 | |
9ad5cbcf | 7106 | outbound_shndx = NULL; |
ef10c3ac | 7107 | outbound_shndx_index = 0; |
9ad5cbcf AM |
7108 | symtab_shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr; |
7109 | if (symtab_shndx_hdr->sh_name != 0) | |
7110 | { | |
7111 | amt = (bfd_size_type) (1 + symcount) * sizeof (Elf_External_Sym_Shndx); | |
a50b1753 NC |
7112 | outbound_shndx = (bfd_byte *) |
7113 | bfd_zalloc2 (abfd, 1 + symcount, sizeof (Elf_External_Sym_Shndx)); | |
9ad5cbcf | 7114 | if (outbound_shndx == NULL) |
ef10c3ac | 7115 | goto error_return; |
5ed6aba4 | 7116 | |
9ad5cbcf AM |
7117 | symtab_shndx_hdr->contents = outbound_shndx; |
7118 | symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX; | |
7119 | symtab_shndx_hdr->sh_size = amt; | |
7120 | symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx); | |
7121 | symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx); | |
7122 | } | |
7123 | ||
589e6347 | 7124 | /* Now generate the data (for "contents"). */ |
079e9a2f AM |
7125 | { |
7126 | /* Fill in zeroth symbol and swap it out. */ | |
7127 | Elf_Internal_Sym sym; | |
7128 | sym.st_name = 0; | |
7129 | sym.st_value = 0; | |
7130 | sym.st_size = 0; | |
7131 | sym.st_info = 0; | |
7132 | sym.st_other = 0; | |
7133 | sym.st_shndx = SHN_UNDEF; | |
35fc36a8 | 7134 | sym.st_target_internal = 0; |
ef10c3ac L |
7135 | symstrtab[0].sym = sym; |
7136 | symstrtab[0].dest_index = outbound_syms_index; | |
7137 | symstrtab[0].destshndx_index = outbound_shndx_index; | |
7138 | outbound_syms_index++; | |
9ad5cbcf | 7139 | if (outbound_shndx != NULL) |
ef10c3ac | 7140 | outbound_shndx_index++; |
079e9a2f | 7141 | } |
252b5132 | 7142 | |
174fd7f9 RS |
7143 | name_local_sections |
7144 | = (bed->elf_backend_name_local_section_symbols | |
7145 | && bed->elf_backend_name_local_section_symbols (abfd)); | |
7146 | ||
079e9a2f | 7147 | syms = bfd_get_outsymbols (abfd); |
ef10c3ac | 7148 | for (idx = 0; idx < symcount;) |
252b5132 | 7149 | { |
252b5132 | 7150 | Elf_Internal_Sym sym; |
079e9a2f AM |
7151 | bfd_vma value = syms[idx]->value; |
7152 | elf_symbol_type *type_ptr; | |
7153 | flagword flags = syms[idx]->flags; | |
7154 | int type; | |
252b5132 | 7155 | |
174fd7f9 RS |
7156 | if (!name_local_sections |
7157 | && (flags & (BSF_SECTION_SYM | BSF_GLOBAL)) == BSF_SECTION_SYM) | |
079e9a2f AM |
7158 | { |
7159 | /* Local section symbols have no name. */ | |
ef10c3ac | 7160 | sym.st_name = (unsigned long) -1; |
079e9a2f AM |
7161 | } |
7162 | else | |
7163 | { | |
ef10c3ac L |
7164 | /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize |
7165 | to get the final offset for st_name. */ | |
7166 | sym.st_name | |
7167 | = (unsigned long) _bfd_elf_strtab_add (stt, syms[idx]->name, | |
7168 | FALSE); | |
079e9a2f | 7169 | if (sym.st_name == (unsigned long) -1) |
ef10c3ac | 7170 | goto error_return; |
079e9a2f | 7171 | } |
252b5132 | 7172 | |
079e9a2f | 7173 | type_ptr = elf_symbol_from (abfd, syms[idx]); |
252b5132 | 7174 | |
079e9a2f AM |
7175 | if ((flags & BSF_SECTION_SYM) == 0 |
7176 | && bfd_is_com_section (syms[idx]->section)) | |
7177 | { | |
7178 | /* ELF common symbols put the alignment into the `value' field, | |
7179 | and the size into the `size' field. This is backwards from | |
7180 | how BFD handles it, so reverse it here. */ | |
7181 | sym.st_size = value; | |
7182 | if (type_ptr == NULL | |
7183 | || type_ptr->internal_elf_sym.st_value == 0) | |
7184 | sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value)); | |
7185 | else | |
7186 | sym.st_value = type_ptr->internal_elf_sym.st_value; | |
7187 | sym.st_shndx = _bfd_elf_section_from_bfd_section | |
7188 | (abfd, syms[idx]->section); | |
7189 | } | |
7190 | else | |
7191 | { | |
7192 | asection *sec = syms[idx]->section; | |
cb33740c | 7193 | unsigned int shndx; |
252b5132 | 7194 | |
079e9a2f AM |
7195 | if (sec->output_section) |
7196 | { | |
7197 | value += sec->output_offset; | |
7198 | sec = sec->output_section; | |
7199 | } | |
589e6347 | 7200 | |
079e9a2f AM |
7201 | /* Don't add in the section vma for relocatable output. */ |
7202 | if (! relocatable_p) | |
7203 | value += sec->vma; | |
7204 | sym.st_value = value; | |
7205 | sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0; | |
7206 | ||
7207 | if (bfd_is_abs_section (sec) | |
7208 | && type_ptr != NULL | |
7209 | && type_ptr->internal_elf_sym.st_shndx != 0) | |
7210 | { | |
7211 | /* This symbol is in a real ELF section which we did | |
7212 | not create as a BFD section. Undo the mapping done | |
7213 | by copy_private_symbol_data. */ | |
7214 | shndx = type_ptr->internal_elf_sym.st_shndx; | |
7215 | switch (shndx) | |
7216 | { | |
7217 | case MAP_ONESYMTAB: | |
7218 | shndx = elf_onesymtab (abfd); | |
7219 | break; | |
7220 | case MAP_DYNSYMTAB: | |
7221 | shndx = elf_dynsymtab (abfd); | |
7222 | break; | |
7223 | case MAP_STRTAB: | |
12bd6957 | 7224 | shndx = elf_strtab_sec (abfd); |
079e9a2f AM |
7225 | break; |
7226 | case MAP_SHSTRTAB: | |
12bd6957 | 7227 | shndx = elf_shstrtab_sec (abfd); |
079e9a2f | 7228 | break; |
9ad5cbcf | 7229 | case MAP_SYM_SHNDX: |
12bd6957 | 7230 | shndx = elf_symtab_shndx (abfd); |
9ad5cbcf | 7231 | break; |
079e9a2f | 7232 | default: |
15bc576a | 7233 | shndx = SHN_ABS; |
079e9a2f AM |
7234 | break; |
7235 | } | |
7236 | } | |
7237 | else | |
7238 | { | |
7239 | shndx = _bfd_elf_section_from_bfd_section (abfd, sec); | |
252b5132 | 7240 | |
cb33740c | 7241 | if (shndx == SHN_BAD) |
079e9a2f AM |
7242 | { |
7243 | asection *sec2; | |
7244 | ||
7245 | /* Writing this would be a hell of a lot easier if | |
7246 | we had some decent documentation on bfd, and | |
7247 | knew what to expect of the library, and what to | |
7248 | demand of applications. For example, it | |
7249 | appears that `objcopy' might not set the | |
7250 | section of a symbol to be a section that is | |
7251 | actually in the output file. */ | |
7252 | sec2 = bfd_get_section_by_name (abfd, sec->name); | |
589e6347 NC |
7253 | if (sec2 == NULL) |
7254 | { | |
7255 | _bfd_error_handler (_("\ | |
7256 | Unable to find equivalent output section for symbol '%s' from section '%s'"), | |
7257 | syms[idx]->name ? syms[idx]->name : "<Local sym>", | |
7258 | sec->name); | |
811072d8 | 7259 | bfd_set_error (bfd_error_invalid_operation); |
ef10c3ac | 7260 | goto error_return; |
589e6347 | 7261 | } |
811072d8 | 7262 | |
079e9a2f | 7263 | shndx = _bfd_elf_section_from_bfd_section (abfd, sec2); |
cb33740c | 7264 | BFD_ASSERT (shndx != SHN_BAD); |
079e9a2f AM |
7265 | } |
7266 | } | |
252b5132 | 7267 | |
079e9a2f AM |
7268 | sym.st_shndx = shndx; |
7269 | } | |
252b5132 | 7270 | |
13ae64f3 JJ |
7271 | if ((flags & BSF_THREAD_LOCAL) != 0) |
7272 | type = STT_TLS; | |
d8045f23 NC |
7273 | else if ((flags & BSF_GNU_INDIRECT_FUNCTION) != 0) |
7274 | type = STT_GNU_IFUNC; | |
13ae64f3 | 7275 | else if ((flags & BSF_FUNCTION) != 0) |
079e9a2f AM |
7276 | type = STT_FUNC; |
7277 | else if ((flags & BSF_OBJECT) != 0) | |
7278 | type = STT_OBJECT; | |
d9352518 DB |
7279 | else if ((flags & BSF_RELC) != 0) |
7280 | type = STT_RELC; | |
7281 | else if ((flags & BSF_SRELC) != 0) | |
7282 | type = STT_SRELC; | |
079e9a2f AM |
7283 | else |
7284 | type = STT_NOTYPE; | |
252b5132 | 7285 | |
13ae64f3 JJ |
7286 | if (syms[idx]->section->flags & SEC_THREAD_LOCAL) |
7287 | type = STT_TLS; | |
7288 | ||
589e6347 | 7289 | /* Processor-specific types. */ |
079e9a2f AM |
7290 | if (type_ptr != NULL |
7291 | && bed->elf_backend_get_symbol_type) | |
7292 | type = ((*bed->elf_backend_get_symbol_type) | |
7293 | (&type_ptr->internal_elf_sym, type)); | |
252b5132 | 7294 | |
079e9a2f AM |
7295 | if (flags & BSF_SECTION_SYM) |
7296 | { | |
7297 | if (flags & BSF_GLOBAL) | |
7298 | sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION); | |
7299 | else | |
7300 | sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION); | |
7301 | } | |
7302 | else if (bfd_is_com_section (syms[idx]->section)) | |
0a40daed | 7303 | { |
504b7d20 | 7304 | #ifdef USE_STT_COMMON |
0a40daed MK |
7305 | if (type == STT_OBJECT) |
7306 | sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_COMMON); | |
7307 | else | |
504b7d20 | 7308 | #endif |
c91e322a | 7309 | sym.st_info = ELF_ST_INFO (STB_GLOBAL, type); |
0a40daed | 7310 | } |
079e9a2f AM |
7311 | else if (bfd_is_und_section (syms[idx]->section)) |
7312 | sym.st_info = ELF_ST_INFO (((flags & BSF_WEAK) | |
7313 | ? STB_WEAK | |
7314 | : STB_GLOBAL), | |
7315 | type); | |
7316 | else if (flags & BSF_FILE) | |
7317 | sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE); | |
7318 | else | |
7319 | { | |
7320 | int bind = STB_LOCAL; | |
252b5132 | 7321 | |
079e9a2f AM |
7322 | if (flags & BSF_LOCAL) |
7323 | bind = STB_LOCAL; | |
3e7a7d11 NC |
7324 | else if (flags & BSF_GNU_UNIQUE) |
7325 | bind = STB_GNU_UNIQUE; | |
079e9a2f AM |
7326 | else if (flags & BSF_WEAK) |
7327 | bind = STB_WEAK; | |
7328 | else if (flags & BSF_GLOBAL) | |
7329 | bind = STB_GLOBAL; | |
252b5132 | 7330 | |
079e9a2f AM |
7331 | sym.st_info = ELF_ST_INFO (bind, type); |
7332 | } | |
252b5132 | 7333 | |
079e9a2f | 7334 | if (type_ptr != NULL) |
35fc36a8 RS |
7335 | { |
7336 | sym.st_other = type_ptr->internal_elf_sym.st_other; | |
7337 | sym.st_target_internal | |
7338 | = type_ptr->internal_elf_sym.st_target_internal; | |
7339 | } | |
079e9a2f | 7340 | else |
35fc36a8 RS |
7341 | { |
7342 | sym.st_other = 0; | |
7343 | sym.st_target_internal = 0; | |
7344 | } | |
252b5132 | 7345 | |
ef10c3ac L |
7346 | idx++; |
7347 | symstrtab[idx].sym = sym; | |
7348 | symstrtab[idx].dest_index = outbound_syms_index; | |
7349 | symstrtab[idx].destshndx_index = outbound_shndx_index; | |
7350 | ||
7351 | outbound_syms_index++; | |
9ad5cbcf | 7352 | if (outbound_shndx != NULL) |
ef10c3ac L |
7353 | outbound_shndx_index++; |
7354 | } | |
7355 | ||
7356 | /* Finalize the .strtab section. */ | |
7357 | _bfd_elf_strtab_finalize (stt); | |
7358 | ||
7359 | /* Swap out the .strtab section. */ | |
7360 | for (idx = 0; idx <= symcount; idx++) | |
7361 | { | |
7362 | struct elf_sym_strtab *elfsym = &symstrtab[idx]; | |
7363 | if (elfsym->sym.st_name == (unsigned long) -1) | |
7364 | elfsym->sym.st_name = 0; | |
7365 | else | |
7366 | elfsym->sym.st_name = _bfd_elf_strtab_offset (stt, | |
7367 | elfsym->sym.st_name); | |
7368 | bed->s->swap_symbol_out (abfd, &elfsym->sym, | |
7369 | (outbound_syms | |
7370 | + (elfsym->dest_index | |
7371 | * bed->s->sizeof_sym)), | |
7372 | (outbound_shndx | |
7373 | + (elfsym->destshndx_index | |
7374 | * sizeof (Elf_External_Sym_Shndx)))); | |
079e9a2f | 7375 | } |
ef10c3ac | 7376 | free (symstrtab); |
252b5132 | 7377 | |
079e9a2f | 7378 | *sttp = stt; |
ef10c3ac | 7379 | symstrtab_hdr->sh_size = _bfd_elf_strtab_size (stt); |
079e9a2f | 7380 | symstrtab_hdr->sh_type = SHT_STRTAB; |
252b5132 | 7381 | |
079e9a2f AM |
7382 | symstrtab_hdr->sh_flags = 0; |
7383 | symstrtab_hdr->sh_addr = 0; | |
7384 | symstrtab_hdr->sh_entsize = 0; | |
7385 | symstrtab_hdr->sh_link = 0; | |
7386 | symstrtab_hdr->sh_info = 0; | |
7387 | symstrtab_hdr->sh_addralign = 1; | |
252b5132 | 7388 | |
b34976b6 | 7389 | return TRUE; |
252b5132 RH |
7390 | } |
7391 | ||
7392 | /* Return the number of bytes required to hold the symtab vector. | |
7393 | ||
7394 | Note that we base it on the count plus 1, since we will null terminate | |
7395 | the vector allocated based on this size. However, the ELF symbol table | |
7396 | always has a dummy entry as symbol #0, so it ends up even. */ | |
7397 | ||
7398 | long | |
217aa764 | 7399 | _bfd_elf_get_symtab_upper_bound (bfd *abfd) |
252b5132 RH |
7400 | { |
7401 | long symcount; | |
7402 | long symtab_size; | |
7403 | Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr; | |
7404 | ||
7405 | symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym; | |
b99d1833 AM |
7406 | symtab_size = (symcount + 1) * (sizeof (asymbol *)); |
7407 | if (symcount > 0) | |
7408 | symtab_size -= sizeof (asymbol *); | |
252b5132 RH |
7409 | |
7410 | return symtab_size; | |
7411 | } | |
7412 | ||
7413 | long | |
217aa764 | 7414 | _bfd_elf_get_dynamic_symtab_upper_bound (bfd *abfd) |
252b5132 RH |
7415 | { |
7416 | long symcount; | |
7417 | long symtab_size; | |
7418 | Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr; | |
7419 | ||
7420 | if (elf_dynsymtab (abfd) == 0) | |
7421 | { | |
7422 | bfd_set_error (bfd_error_invalid_operation); | |
7423 | return -1; | |
7424 | } | |
7425 | ||
7426 | symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym; | |
b99d1833 AM |
7427 | symtab_size = (symcount + 1) * (sizeof (asymbol *)); |
7428 | if (symcount > 0) | |
7429 | symtab_size -= sizeof (asymbol *); | |
252b5132 RH |
7430 | |
7431 | return symtab_size; | |
7432 | } | |
7433 | ||
7434 | long | |
217aa764 AM |
7435 | _bfd_elf_get_reloc_upper_bound (bfd *abfd ATTRIBUTE_UNUSED, |
7436 | sec_ptr asect) | |
252b5132 RH |
7437 | { |
7438 | return (asect->reloc_count + 1) * sizeof (arelent *); | |
7439 | } | |
7440 | ||
7441 | /* Canonicalize the relocs. */ | |
7442 | ||
7443 | long | |
217aa764 AM |
7444 | _bfd_elf_canonicalize_reloc (bfd *abfd, |
7445 | sec_ptr section, | |
7446 | arelent **relptr, | |
7447 | asymbol **symbols) | |
252b5132 RH |
7448 | { |
7449 | arelent *tblptr; | |
7450 | unsigned int i; | |
9c5bfbb7 | 7451 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
252b5132 | 7452 | |
b34976b6 | 7453 | if (! bed->s->slurp_reloc_table (abfd, section, symbols, FALSE)) |
252b5132 RH |
7454 | return -1; |
7455 | ||
7456 | tblptr = section->relocation; | |
7457 | for (i = 0; i < section->reloc_count; i++) | |
7458 | *relptr++ = tblptr++; | |
7459 | ||
7460 | *relptr = NULL; | |
7461 | ||
7462 | return section->reloc_count; | |
7463 | } | |
7464 | ||
7465 | long | |
6cee3f79 | 7466 | _bfd_elf_canonicalize_symtab (bfd *abfd, asymbol **allocation) |
252b5132 | 7467 | { |
9c5bfbb7 | 7468 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
217aa764 | 7469 | long symcount = bed->s->slurp_symbol_table (abfd, allocation, FALSE); |
252b5132 RH |
7470 | |
7471 | if (symcount >= 0) | |
7472 | bfd_get_symcount (abfd) = symcount; | |
7473 | return symcount; | |
7474 | } | |
7475 | ||
7476 | long | |
217aa764 AM |
7477 | _bfd_elf_canonicalize_dynamic_symtab (bfd *abfd, |
7478 | asymbol **allocation) | |
252b5132 | 7479 | { |
9c5bfbb7 | 7480 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
217aa764 | 7481 | long symcount = bed->s->slurp_symbol_table (abfd, allocation, TRUE); |
1f70368c DJ |
7482 | |
7483 | if (symcount >= 0) | |
7484 | bfd_get_dynamic_symcount (abfd) = symcount; | |
7485 | return symcount; | |
252b5132 RH |
7486 | } |
7487 | ||
8615f3f2 AM |
7488 | /* Return the size required for the dynamic reloc entries. Any loadable |
7489 | section that was actually installed in the BFD, and has type SHT_REL | |
7490 | or SHT_RELA, and uses the dynamic symbol table, is considered to be a | |
7491 | dynamic reloc section. */ | |
252b5132 RH |
7492 | |
7493 | long | |
217aa764 | 7494 | _bfd_elf_get_dynamic_reloc_upper_bound (bfd *abfd) |
252b5132 RH |
7495 | { |
7496 | long ret; | |
7497 | asection *s; | |
7498 | ||
7499 | if (elf_dynsymtab (abfd) == 0) | |
7500 | { | |
7501 | bfd_set_error (bfd_error_invalid_operation); | |
7502 | return -1; | |
7503 | } | |
7504 | ||
7505 | ret = sizeof (arelent *); | |
7506 | for (s = abfd->sections; s != NULL; s = s->next) | |
266b05cf | 7507 | if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd) |
252b5132 RH |
7508 | && (elf_section_data (s)->this_hdr.sh_type == SHT_REL |
7509 | || elf_section_data (s)->this_hdr.sh_type == SHT_RELA)) | |
eea6121a | 7510 | ret += ((s->size / elf_section_data (s)->this_hdr.sh_entsize) |
252b5132 RH |
7511 | * sizeof (arelent *)); |
7512 | ||
7513 | return ret; | |
7514 | } | |
7515 | ||
8615f3f2 AM |
7516 | /* Canonicalize the dynamic relocation entries. Note that we return the |
7517 | dynamic relocations as a single block, although they are actually | |
7518 | associated with particular sections; the interface, which was | |
7519 | designed for SunOS style shared libraries, expects that there is only | |
7520 | one set of dynamic relocs. Any loadable section that was actually | |
7521 | installed in the BFD, and has type SHT_REL or SHT_RELA, and uses the | |
7522 | dynamic symbol table, is considered to be a dynamic reloc section. */ | |
252b5132 RH |
7523 | |
7524 | long | |
217aa764 AM |
7525 | _bfd_elf_canonicalize_dynamic_reloc (bfd *abfd, |
7526 | arelent **storage, | |
7527 | asymbol **syms) | |
252b5132 | 7528 | { |
217aa764 | 7529 | bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean); |
252b5132 RH |
7530 | asection *s; |
7531 | long ret; | |
7532 | ||
7533 | if (elf_dynsymtab (abfd) == 0) | |
7534 | { | |
7535 | bfd_set_error (bfd_error_invalid_operation); | |
7536 | return -1; | |
7537 | } | |
7538 | ||
7539 | slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table; | |
7540 | ret = 0; | |
7541 | for (s = abfd->sections; s != NULL; s = s->next) | |
7542 | { | |
266b05cf | 7543 | if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd) |
252b5132 RH |
7544 | && (elf_section_data (s)->this_hdr.sh_type == SHT_REL |
7545 | || elf_section_data (s)->this_hdr.sh_type == SHT_RELA)) | |
7546 | { | |
7547 | arelent *p; | |
7548 | long count, i; | |
7549 | ||
b34976b6 | 7550 | if (! (*slurp_relocs) (abfd, s, syms, TRUE)) |
252b5132 | 7551 | return -1; |
eea6121a | 7552 | count = s->size / elf_section_data (s)->this_hdr.sh_entsize; |
252b5132 RH |
7553 | p = s->relocation; |
7554 | for (i = 0; i < count; i++) | |
7555 | *storage++ = p++; | |
7556 | ret += count; | |
7557 | } | |
7558 | } | |
7559 | ||
7560 | *storage = NULL; | |
7561 | ||
7562 | return ret; | |
7563 | } | |
7564 | \f | |
7565 | /* Read in the version information. */ | |
7566 | ||
b34976b6 | 7567 | bfd_boolean |
fc0e6df6 | 7568 | _bfd_elf_slurp_version_tables (bfd *abfd, bfd_boolean default_imported_symver) |
252b5132 RH |
7569 | { |
7570 | bfd_byte *contents = NULL; | |
fc0e6df6 PB |
7571 | unsigned int freeidx = 0; |
7572 | ||
7573 | if (elf_dynverref (abfd) != 0) | |
7574 | { | |
7575 | Elf_Internal_Shdr *hdr; | |
7576 | Elf_External_Verneed *everneed; | |
7577 | Elf_Internal_Verneed *iverneed; | |
7578 | unsigned int i; | |
d0fb9a8d | 7579 | bfd_byte *contents_end; |
fc0e6df6 PB |
7580 | |
7581 | hdr = &elf_tdata (abfd)->dynverref_hdr; | |
7582 | ||
601a03ba | 7583 | if (hdr->sh_info == 0 || hdr->sh_size < sizeof (Elf_External_Verneed)) |
d0fb9a8d | 7584 | { |
601a03ba AM |
7585 | error_return_bad_verref: |
7586 | (*_bfd_error_handler) | |
7587 | (_("%B: .gnu.version_r invalid entry"), abfd); | |
7588 | bfd_set_error (bfd_error_bad_value); | |
d0fb9a8d JJ |
7589 | error_return_verref: |
7590 | elf_tdata (abfd)->verref = NULL; | |
7591 | elf_tdata (abfd)->cverrefs = 0; | |
7592 | goto error_return; | |
7593 | } | |
601a03ba AM |
7594 | |
7595 | contents = (bfd_byte *) bfd_malloc (hdr->sh_size); | |
7596 | if (contents == NULL) | |
7597 | goto error_return_verref; | |
7598 | ||
fc0e6df6 PB |
7599 | if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0 |
7600 | || bfd_bread (contents, hdr->sh_size, abfd) != hdr->sh_size) | |
d0fb9a8d | 7601 | goto error_return_verref; |
fc0e6df6 | 7602 | |
601a03ba AM |
7603 | elf_tdata (abfd)->verref = (Elf_Internal_Verneed *) |
7604 | bfd_zalloc2 (abfd, hdr->sh_info, sizeof (Elf_Internal_Verneed)); | |
7605 | ||
7606 | if (elf_tdata (abfd)->verref == NULL) | |
d0fb9a8d JJ |
7607 | goto error_return_verref; |
7608 | ||
7609 | BFD_ASSERT (sizeof (Elf_External_Verneed) | |
7610 | == sizeof (Elf_External_Vernaux)); | |
7611 | contents_end = contents + hdr->sh_size - sizeof (Elf_External_Verneed); | |
fc0e6df6 PB |
7612 | everneed = (Elf_External_Verneed *) contents; |
7613 | iverneed = elf_tdata (abfd)->verref; | |
7614 | for (i = 0; i < hdr->sh_info; i++, iverneed++) | |
7615 | { | |
7616 | Elf_External_Vernaux *evernaux; | |
7617 | Elf_Internal_Vernaux *ivernaux; | |
7618 | unsigned int j; | |
7619 | ||
7620 | _bfd_elf_swap_verneed_in (abfd, everneed, iverneed); | |
7621 | ||
7622 | iverneed->vn_bfd = abfd; | |
7623 | ||
7624 | iverneed->vn_filename = | |
7625 | bfd_elf_string_from_elf_section (abfd, hdr->sh_link, | |
7626 | iverneed->vn_file); | |
7627 | if (iverneed->vn_filename == NULL) | |
601a03ba | 7628 | goto error_return_bad_verref; |
fc0e6df6 | 7629 | |
d0fb9a8d JJ |
7630 | if (iverneed->vn_cnt == 0) |
7631 | iverneed->vn_auxptr = NULL; | |
7632 | else | |
7633 | { | |
a50b1753 NC |
7634 | iverneed->vn_auxptr = (struct elf_internal_vernaux *) |
7635 | bfd_alloc2 (abfd, iverneed->vn_cnt, | |
7636 | sizeof (Elf_Internal_Vernaux)); | |
d0fb9a8d JJ |
7637 | if (iverneed->vn_auxptr == NULL) |
7638 | goto error_return_verref; | |
7639 | } | |
7640 | ||
7641 | if (iverneed->vn_aux | |
7642 | > (size_t) (contents_end - (bfd_byte *) everneed)) | |
601a03ba | 7643 | goto error_return_bad_verref; |
fc0e6df6 PB |
7644 | |
7645 | evernaux = ((Elf_External_Vernaux *) | |
7646 | ((bfd_byte *) everneed + iverneed->vn_aux)); | |
7647 | ivernaux = iverneed->vn_auxptr; | |
7648 | for (j = 0; j < iverneed->vn_cnt; j++, ivernaux++) | |
7649 | { | |
7650 | _bfd_elf_swap_vernaux_in (abfd, evernaux, ivernaux); | |
7651 | ||
7652 | ivernaux->vna_nodename = | |
7653 | bfd_elf_string_from_elf_section (abfd, hdr->sh_link, | |
7654 | ivernaux->vna_name); | |
7655 | if (ivernaux->vna_nodename == NULL) | |
601a03ba | 7656 | goto error_return_bad_verref; |
fc0e6df6 | 7657 | |
25ff461f AM |
7658 | if (ivernaux->vna_other > freeidx) |
7659 | freeidx = ivernaux->vna_other; | |
7660 | ||
7661 | ivernaux->vna_nextptr = NULL; | |
7662 | if (ivernaux->vna_next == 0) | |
7663 | { | |
7664 | iverneed->vn_cnt = j + 1; | |
7665 | break; | |
7666 | } | |
fc0e6df6 PB |
7667 | if (j + 1 < iverneed->vn_cnt) |
7668 | ivernaux->vna_nextptr = ivernaux + 1; | |
fc0e6df6 | 7669 | |
d0fb9a8d JJ |
7670 | if (ivernaux->vna_next |
7671 | > (size_t) (contents_end - (bfd_byte *) evernaux)) | |
601a03ba | 7672 | goto error_return_bad_verref; |
d0fb9a8d | 7673 | |
fc0e6df6 PB |
7674 | evernaux = ((Elf_External_Vernaux *) |
7675 | ((bfd_byte *) evernaux + ivernaux->vna_next)); | |
fc0e6df6 PB |
7676 | } |
7677 | ||
25ff461f AM |
7678 | iverneed->vn_nextref = NULL; |
7679 | if (iverneed->vn_next == 0) | |
7680 | break; | |
fc0e6df6 PB |
7681 | if (i + 1 < hdr->sh_info) |
7682 | iverneed->vn_nextref = iverneed + 1; | |
fc0e6df6 | 7683 | |
d0fb9a8d JJ |
7684 | if (iverneed->vn_next |
7685 | > (size_t) (contents_end - (bfd_byte *) everneed)) | |
601a03ba | 7686 | goto error_return_bad_verref; |
d0fb9a8d | 7687 | |
fc0e6df6 PB |
7688 | everneed = ((Elf_External_Verneed *) |
7689 | ((bfd_byte *) everneed + iverneed->vn_next)); | |
7690 | } | |
25ff461f | 7691 | elf_tdata (abfd)->cverrefs = i; |
fc0e6df6 PB |
7692 | |
7693 | free (contents); | |
7694 | contents = NULL; | |
7695 | } | |
252b5132 RH |
7696 | |
7697 | if (elf_dynverdef (abfd) != 0) | |
7698 | { | |
7699 | Elf_Internal_Shdr *hdr; | |
7700 | Elf_External_Verdef *everdef; | |
7701 | Elf_Internal_Verdef *iverdef; | |
f631889e UD |
7702 | Elf_Internal_Verdef *iverdefarr; |
7703 | Elf_Internal_Verdef iverdefmem; | |
252b5132 | 7704 | unsigned int i; |
062e2358 | 7705 | unsigned int maxidx; |
d0fb9a8d | 7706 | bfd_byte *contents_end_def, *contents_end_aux; |
252b5132 RH |
7707 | |
7708 | hdr = &elf_tdata (abfd)->dynverdef_hdr; | |
7709 | ||
601a03ba AM |
7710 | if (hdr->sh_info == 0 || hdr->sh_size < sizeof (Elf_External_Verdef)) |
7711 | { | |
7712 | error_return_bad_verdef: | |
7713 | (*_bfd_error_handler) | |
7714 | (_("%B: .gnu.version_d invalid entry"), abfd); | |
7715 | bfd_set_error (bfd_error_bad_value); | |
7716 | error_return_verdef: | |
7717 | elf_tdata (abfd)->verdef = NULL; | |
7718 | elf_tdata (abfd)->cverdefs = 0; | |
7719 | goto error_return; | |
7720 | } | |
7721 | ||
a50b1753 | 7722 | contents = (bfd_byte *) bfd_malloc (hdr->sh_size); |
252b5132 | 7723 | if (contents == NULL) |
601a03ba | 7724 | goto error_return_verdef; |
252b5132 | 7725 | if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0 |
217aa764 | 7726 | || bfd_bread (contents, hdr->sh_size, abfd) != hdr->sh_size) |
601a03ba | 7727 | goto error_return_verdef; |
d0fb9a8d JJ |
7728 | |
7729 | BFD_ASSERT (sizeof (Elf_External_Verdef) | |
7730 | >= sizeof (Elf_External_Verdaux)); | |
7731 | contents_end_def = contents + hdr->sh_size | |
7732 | - sizeof (Elf_External_Verdef); | |
7733 | contents_end_aux = contents + hdr->sh_size | |
7734 | - sizeof (Elf_External_Verdaux); | |
7735 | ||
f631889e UD |
7736 | /* We know the number of entries in the section but not the maximum |
7737 | index. Therefore we have to run through all entries and find | |
7738 | the maximum. */ | |
252b5132 | 7739 | everdef = (Elf_External_Verdef *) contents; |
f631889e UD |
7740 | maxidx = 0; |
7741 | for (i = 0; i < hdr->sh_info; ++i) | |
7742 | { | |
7743 | _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem); | |
7744 | ||
601a03ba AM |
7745 | if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) == 0) |
7746 | goto error_return_bad_verdef; | |
062e2358 AM |
7747 | if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) > maxidx) |
7748 | maxidx = iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION); | |
f631889e | 7749 | |
25ff461f AM |
7750 | if (iverdefmem.vd_next == 0) |
7751 | break; | |
7752 | ||
d0fb9a8d JJ |
7753 | if (iverdefmem.vd_next |
7754 | > (size_t) (contents_end_def - (bfd_byte *) everdef)) | |
601a03ba | 7755 | goto error_return_bad_verdef; |
d0fb9a8d | 7756 | |
f631889e UD |
7757 | everdef = ((Elf_External_Verdef *) |
7758 | ((bfd_byte *) everdef + iverdefmem.vd_next)); | |
7759 | } | |
7760 | ||
fc0e6df6 PB |
7761 | if (default_imported_symver) |
7762 | { | |
7763 | if (freeidx > maxidx) | |
7764 | maxidx = ++freeidx; | |
7765 | else | |
7766 | freeidx = ++maxidx; | |
7767 | } | |
201159ec | 7768 | |
601a03ba AM |
7769 | elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *) |
7770 | bfd_zalloc2 (abfd, maxidx, sizeof (Elf_Internal_Verdef)); | |
f631889e | 7771 | if (elf_tdata (abfd)->verdef == NULL) |
601a03ba | 7772 | goto error_return_verdef; |
f631889e UD |
7773 | |
7774 | elf_tdata (abfd)->cverdefs = maxidx; | |
7775 | ||
7776 | everdef = (Elf_External_Verdef *) contents; | |
7777 | iverdefarr = elf_tdata (abfd)->verdef; | |
7778 | for (i = 0; i < hdr->sh_info; i++) | |
252b5132 RH |
7779 | { |
7780 | Elf_External_Verdaux *everdaux; | |
7781 | Elf_Internal_Verdaux *iverdaux; | |
7782 | unsigned int j; | |
7783 | ||
f631889e UD |
7784 | _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem); |
7785 | ||
d0fb9a8d | 7786 | if ((iverdefmem.vd_ndx & VERSYM_VERSION) == 0) |
601a03ba | 7787 | goto error_return_bad_verdef; |
d0fb9a8d | 7788 | |
f631889e UD |
7789 | iverdef = &iverdefarr[(iverdefmem.vd_ndx & VERSYM_VERSION) - 1]; |
7790 | memcpy (iverdef, &iverdefmem, sizeof (Elf_Internal_Verdef)); | |
252b5132 RH |
7791 | |
7792 | iverdef->vd_bfd = abfd; | |
7793 | ||
d0fb9a8d JJ |
7794 | if (iverdef->vd_cnt == 0) |
7795 | iverdef->vd_auxptr = NULL; | |
7796 | else | |
7797 | { | |
a50b1753 NC |
7798 | iverdef->vd_auxptr = (struct elf_internal_verdaux *) |
7799 | bfd_alloc2 (abfd, iverdef->vd_cnt, | |
7800 | sizeof (Elf_Internal_Verdaux)); | |
d0fb9a8d JJ |
7801 | if (iverdef->vd_auxptr == NULL) |
7802 | goto error_return_verdef; | |
7803 | } | |
7804 | ||
7805 | if (iverdef->vd_aux | |
7806 | > (size_t) (contents_end_aux - (bfd_byte *) everdef)) | |
601a03ba | 7807 | goto error_return_bad_verdef; |
252b5132 RH |
7808 | |
7809 | everdaux = ((Elf_External_Verdaux *) | |
7810 | ((bfd_byte *) everdef + iverdef->vd_aux)); | |
7811 | iverdaux = iverdef->vd_auxptr; | |
7812 | for (j = 0; j < iverdef->vd_cnt; j++, iverdaux++) | |
7813 | { | |
7814 | _bfd_elf_swap_verdaux_in (abfd, everdaux, iverdaux); | |
7815 | ||
7816 | iverdaux->vda_nodename = | |
7817 | bfd_elf_string_from_elf_section (abfd, hdr->sh_link, | |
7818 | iverdaux->vda_name); | |
7819 | if (iverdaux->vda_nodename == NULL) | |
601a03ba | 7820 | goto error_return_bad_verdef; |
252b5132 | 7821 | |
25ff461f AM |
7822 | iverdaux->vda_nextptr = NULL; |
7823 | if (iverdaux->vda_next == 0) | |
7824 | { | |
7825 | iverdef->vd_cnt = j + 1; | |
7826 | break; | |
7827 | } | |
252b5132 RH |
7828 | if (j + 1 < iverdef->vd_cnt) |
7829 | iverdaux->vda_nextptr = iverdaux + 1; | |
252b5132 | 7830 | |
d0fb9a8d JJ |
7831 | if (iverdaux->vda_next |
7832 | > (size_t) (contents_end_aux - (bfd_byte *) everdaux)) | |
601a03ba | 7833 | goto error_return_bad_verdef; |
d0fb9a8d | 7834 | |
252b5132 RH |
7835 | everdaux = ((Elf_External_Verdaux *) |
7836 | ((bfd_byte *) everdaux + iverdaux->vda_next)); | |
7837 | } | |
7838 | ||
d0fb9a8d JJ |
7839 | if (iverdef->vd_cnt) |
7840 | iverdef->vd_nodename = iverdef->vd_auxptr->vda_nodename; | |
252b5132 | 7841 | |
25ff461f AM |
7842 | iverdef->vd_nextdef = NULL; |
7843 | if (iverdef->vd_next == 0) | |
7844 | break; | |
d0fb9a8d | 7845 | if ((size_t) (iverdef - iverdefarr) + 1 < maxidx) |
252b5132 | 7846 | iverdef->vd_nextdef = iverdef + 1; |
252b5132 RH |
7847 | |
7848 | everdef = ((Elf_External_Verdef *) | |
7849 | ((bfd_byte *) everdef + iverdef->vd_next)); | |
7850 | } | |
7851 | ||
7852 | free (contents); | |
7853 | contents = NULL; | |
7854 | } | |
fc0e6df6 | 7855 | else if (default_imported_symver) |
252b5132 | 7856 | { |
fc0e6df6 PB |
7857 | if (freeidx < 3) |
7858 | freeidx = 3; | |
7859 | else | |
7860 | freeidx++; | |
252b5132 | 7861 | |
a50b1753 NC |
7862 | elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *) |
7863 | bfd_zalloc2 (abfd, freeidx, sizeof (Elf_Internal_Verdef)); | |
fc0e6df6 | 7864 | if (elf_tdata (abfd)->verdef == NULL) |
252b5132 RH |
7865 | goto error_return; |
7866 | ||
fc0e6df6 PB |
7867 | elf_tdata (abfd)->cverdefs = freeidx; |
7868 | } | |
252b5132 | 7869 | |
fc0e6df6 PB |
7870 | /* Create a default version based on the soname. */ |
7871 | if (default_imported_symver) | |
7872 | { | |
7873 | Elf_Internal_Verdef *iverdef; | |
7874 | Elf_Internal_Verdaux *iverdaux; | |
252b5132 | 7875 | |
5bb3703f | 7876 | iverdef = &elf_tdata (abfd)->verdef[freeidx - 1]; |
252b5132 | 7877 | |
fc0e6df6 PB |
7878 | iverdef->vd_version = VER_DEF_CURRENT; |
7879 | iverdef->vd_flags = 0; | |
7880 | iverdef->vd_ndx = freeidx; | |
7881 | iverdef->vd_cnt = 1; | |
252b5132 | 7882 | |
fc0e6df6 | 7883 | iverdef->vd_bfd = abfd; |
252b5132 | 7884 | |
fc0e6df6 PB |
7885 | iverdef->vd_nodename = bfd_elf_get_dt_soname (abfd); |
7886 | if (iverdef->vd_nodename == NULL) | |
d0fb9a8d | 7887 | goto error_return_verdef; |
fc0e6df6 | 7888 | iverdef->vd_nextdef = NULL; |
601a03ba AM |
7889 | iverdef->vd_auxptr = ((struct elf_internal_verdaux *) |
7890 | bfd_zalloc (abfd, sizeof (Elf_Internal_Verdaux))); | |
d0fb9a8d JJ |
7891 | if (iverdef->vd_auxptr == NULL) |
7892 | goto error_return_verdef; | |
252b5132 | 7893 | |
fc0e6df6 PB |
7894 | iverdaux = iverdef->vd_auxptr; |
7895 | iverdaux->vda_nodename = iverdef->vd_nodename; | |
252b5132 RH |
7896 | } |
7897 | ||
b34976b6 | 7898 | return TRUE; |
252b5132 RH |
7899 | |
7900 | error_return: | |
5ed6aba4 | 7901 | if (contents != NULL) |
252b5132 | 7902 | free (contents); |
b34976b6 | 7903 | return FALSE; |
252b5132 RH |
7904 | } |
7905 | \f | |
7906 | asymbol * | |
217aa764 | 7907 | _bfd_elf_make_empty_symbol (bfd *abfd) |
252b5132 RH |
7908 | { |
7909 | elf_symbol_type *newsym; | |
7910 | ||
201159ec | 7911 | newsym = (elf_symbol_type *) bfd_zalloc (abfd, sizeof * newsym); |
252b5132 RH |
7912 | if (!newsym) |
7913 | return NULL; | |
201159ec NC |
7914 | newsym->symbol.the_bfd = abfd; |
7915 | return &newsym->symbol; | |
252b5132 RH |
7916 | } |
7917 | ||
7918 | void | |
217aa764 AM |
7919 | _bfd_elf_get_symbol_info (bfd *abfd ATTRIBUTE_UNUSED, |
7920 | asymbol *symbol, | |
7921 | symbol_info *ret) | |
252b5132 RH |
7922 | { |
7923 | bfd_symbol_info (symbol, ret); | |
7924 | } | |
7925 | ||
7926 | /* Return whether a symbol name implies a local symbol. Most targets | |
7927 | use this function for the is_local_label_name entry point, but some | |
7928 | override it. */ | |
7929 | ||
b34976b6 | 7930 | bfd_boolean |
217aa764 AM |
7931 | _bfd_elf_is_local_label_name (bfd *abfd ATTRIBUTE_UNUSED, |
7932 | const char *name) | |
252b5132 RH |
7933 | { |
7934 | /* Normal local symbols start with ``.L''. */ | |
7935 | if (name[0] == '.' && name[1] == 'L') | |
b34976b6 | 7936 | return TRUE; |
252b5132 RH |
7937 | |
7938 | /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate | |
7939 | DWARF debugging symbols starting with ``..''. */ | |
7940 | if (name[0] == '.' && name[1] == '.') | |
b34976b6 | 7941 | return TRUE; |
252b5132 RH |
7942 | |
7943 | /* gcc will sometimes generate symbols beginning with ``_.L_'' when | |
7944 | emitting DWARF debugging output. I suspect this is actually a | |
7945 | small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call | |
7946 | ASM_GENERATE_INTERNAL_LABEL, and this causes the leading | |
7947 | underscore to be emitted on some ELF targets). For ease of use, | |
7948 | we treat such symbols as local. */ | |
7949 | if (name[0] == '_' && name[1] == '.' && name[2] == 'L' && name[3] == '_') | |
b34976b6 | 7950 | return TRUE; |
252b5132 | 7951 | |
b1fa9dd6 NC |
7952 | /* Treat assembler generated fake symbols, dollar local labels and |
7953 | forward-backward labels (aka local labels) as locals. | |
7954 | These labels have the form: | |
7955 | ||
7956 | L0^A.* (fake symbols) | |
7957 | ||
7958 | [.]?L[0123456789]+{^A|^B}[0123456789]* (local labels) | |
7959 | ||
7960 | Versions which start with .L will have already been matched above, | |
7961 | so we only need to match the rest. */ | |
7962 | if (name[0] == 'L' && ISDIGIT (name[1])) | |
7963 | { | |
7964 | bfd_boolean ret = FALSE; | |
7965 | const char * p; | |
7966 | char c; | |
7967 | ||
7968 | for (p = name + 2; (c = *p); p++) | |
7969 | { | |
7970 | if (c == 1 || c == 2) | |
7971 | { | |
7972 | if (c == 1 && p == name + 2) | |
7973 | /* A fake symbol. */ | |
7974 | return TRUE; | |
7975 | ||
7976 | /* FIXME: We are being paranoid here and treating symbols like | |
7977 | L0^Bfoo as if there were non-local, on the grounds that the | |
7978 | assembler will never generate them. But can any symbol | |
7979 | containing an ASCII value in the range 1-31 ever be anything | |
7980 | other than some kind of local ? */ | |
7981 | ret = TRUE; | |
7982 | } | |
7983 | ||
7984 | if (! ISDIGIT (c)) | |
7985 | { | |
7986 | ret = FALSE; | |
7987 | break; | |
7988 | } | |
7989 | } | |
7990 | return ret; | |
7991 | } | |
ffa54770 | 7992 | |
b34976b6 | 7993 | return FALSE; |
252b5132 RH |
7994 | } |
7995 | ||
7996 | alent * | |
217aa764 AM |
7997 | _bfd_elf_get_lineno (bfd *abfd ATTRIBUTE_UNUSED, |
7998 | asymbol *symbol ATTRIBUTE_UNUSED) | |
252b5132 RH |
7999 | { |
8000 | abort (); | |
8001 | return NULL; | |
8002 | } | |
8003 | ||
b34976b6 | 8004 | bfd_boolean |
217aa764 AM |
8005 | _bfd_elf_set_arch_mach (bfd *abfd, |
8006 | enum bfd_architecture arch, | |
8007 | unsigned long machine) | |
252b5132 RH |
8008 | { |
8009 | /* If this isn't the right architecture for this backend, and this | |
8010 | isn't the generic backend, fail. */ | |
8011 | if (arch != get_elf_backend_data (abfd)->arch | |
8012 | && arch != bfd_arch_unknown | |
8013 | && get_elf_backend_data (abfd)->arch != bfd_arch_unknown) | |
b34976b6 | 8014 | return FALSE; |
252b5132 RH |
8015 | |
8016 | return bfd_default_set_arch_mach (abfd, arch, machine); | |
8017 | } | |
8018 | ||
d1fad7c6 NC |
8019 | /* Find the nearest line to a particular section and offset, |
8020 | for error reporting. */ | |
8021 | ||
b34976b6 | 8022 | bfd_boolean |
217aa764 | 8023 | _bfd_elf_find_nearest_line (bfd *abfd, |
217aa764 | 8024 | asymbol **symbols, |
fb167eb2 | 8025 | asection *section, |
217aa764 AM |
8026 | bfd_vma offset, |
8027 | const char **filename_ptr, | |
8028 | const char **functionname_ptr, | |
fb167eb2 AM |
8029 | unsigned int *line_ptr, |
8030 | unsigned int *discriminator_ptr) | |
d1fad7c6 | 8031 | { |
b34976b6 | 8032 | bfd_boolean found; |
d1fad7c6 | 8033 | |
fb167eb2 | 8034 | if (_bfd_dwarf2_find_nearest_line (abfd, symbols, NULL, section, offset, |
4e8a9624 | 8035 | filename_ptr, functionname_ptr, |
fb167eb2 AM |
8036 | line_ptr, discriminator_ptr, |
8037 | dwarf_debug_sections, 0, | |
e00e8198 AM |
8038 | &elf_tdata (abfd)->dwarf2_find_line_info) |
8039 | || _bfd_dwarf1_find_nearest_line (abfd, symbols, section, offset, | |
8040 | filename_ptr, functionname_ptr, | |
8041 | line_ptr)) | |
d1fad7c6 NC |
8042 | { |
8043 | if (!*functionname_ptr) | |
e00e8198 AM |
8044 | _bfd_elf_find_function (abfd, symbols, section, offset, |
8045 | *filename_ptr ? NULL : filename_ptr, | |
8046 | functionname_ptr); | |
b34976b6 | 8047 | return TRUE; |
d1fad7c6 NC |
8048 | } |
8049 | ||
8050 | if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset, | |
4e8a9624 AM |
8051 | &found, filename_ptr, |
8052 | functionname_ptr, line_ptr, | |
8053 | &elf_tdata (abfd)->line_info)) | |
b34976b6 | 8054 | return FALSE; |
dc43ada5 | 8055 | if (found && (*functionname_ptr || *line_ptr)) |
b34976b6 | 8056 | return TRUE; |
d1fad7c6 NC |
8057 | |
8058 | if (symbols == NULL) | |
b34976b6 | 8059 | return FALSE; |
d1fad7c6 | 8060 | |
e00e8198 AM |
8061 | if (! _bfd_elf_find_function (abfd, symbols, section, offset, |
8062 | filename_ptr, functionname_ptr)) | |
b34976b6 | 8063 | return FALSE; |
d1fad7c6 | 8064 | |
252b5132 | 8065 | *line_ptr = 0; |
b34976b6 | 8066 | return TRUE; |
252b5132 RH |
8067 | } |
8068 | ||
5420f73d L |
8069 | /* Find the line for a symbol. */ |
8070 | ||
8071 | bfd_boolean | |
8072 | _bfd_elf_find_line (bfd *abfd, asymbol **symbols, asymbol *symbol, | |
8073 | const char **filename_ptr, unsigned int *line_ptr) | |
9b8d1a36 | 8074 | { |
fb167eb2 AM |
8075 | return _bfd_dwarf2_find_nearest_line (abfd, symbols, symbol, NULL, 0, |
8076 | filename_ptr, NULL, line_ptr, NULL, | |
8077 | dwarf_debug_sections, 0, | |
8078 | &elf_tdata (abfd)->dwarf2_find_line_info); | |
5420f73d L |
8079 | } |
8080 | ||
4ab527b0 FF |
8081 | /* After a call to bfd_find_nearest_line, successive calls to |
8082 | bfd_find_inliner_info can be used to get source information about | |
8083 | each level of function inlining that terminated at the address | |
8084 | passed to bfd_find_nearest_line. Currently this is only supported | |
8085 | for DWARF2 with appropriate DWARF3 extensions. */ | |
8086 | ||
8087 | bfd_boolean | |
8088 | _bfd_elf_find_inliner_info (bfd *abfd, | |
8089 | const char **filename_ptr, | |
8090 | const char **functionname_ptr, | |
8091 | unsigned int *line_ptr) | |
8092 | { | |
8093 | bfd_boolean found; | |
8094 | found = _bfd_dwarf2_find_inliner_info (abfd, filename_ptr, | |
8095 | functionname_ptr, line_ptr, | |
8096 | & elf_tdata (abfd)->dwarf2_find_line_info); | |
8097 | return found; | |
8098 | } | |
8099 | ||
252b5132 | 8100 | int |
a6b96beb | 8101 | _bfd_elf_sizeof_headers (bfd *abfd, struct bfd_link_info *info) |
252b5132 | 8102 | { |
8ded5a0f AM |
8103 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
8104 | int ret = bed->s->sizeof_ehdr; | |
252b5132 | 8105 | |
a6b96beb | 8106 | if (!info->relocatable) |
8ded5a0f | 8107 | { |
12bd6957 | 8108 | bfd_size_type phdr_size = elf_program_header_size (abfd); |
8ded5a0f | 8109 | |
62d7a5f6 AM |
8110 | if (phdr_size == (bfd_size_type) -1) |
8111 | { | |
8112 | struct elf_segment_map *m; | |
8113 | ||
8114 | phdr_size = 0; | |
12bd6957 | 8115 | for (m = elf_seg_map (abfd); m != NULL; m = m->next) |
62d7a5f6 | 8116 | phdr_size += bed->s->sizeof_phdr; |
8ded5a0f | 8117 | |
62d7a5f6 AM |
8118 | if (phdr_size == 0) |
8119 | phdr_size = get_program_header_size (abfd, info); | |
8120 | } | |
8ded5a0f | 8121 | |
12bd6957 | 8122 | elf_program_header_size (abfd) = phdr_size; |
8ded5a0f AM |
8123 | ret += phdr_size; |
8124 | } | |
8125 | ||
252b5132 RH |
8126 | return ret; |
8127 | } | |
8128 | ||
b34976b6 | 8129 | bfd_boolean |
217aa764 AM |
8130 | _bfd_elf_set_section_contents (bfd *abfd, |
8131 | sec_ptr section, | |
0f867abe | 8132 | const void *location, |
217aa764 AM |
8133 | file_ptr offset, |
8134 | bfd_size_type count) | |
252b5132 RH |
8135 | { |
8136 | Elf_Internal_Shdr *hdr; | |
1b6aeedb | 8137 | file_ptr pos; |
252b5132 RH |
8138 | |
8139 | if (! abfd->output_has_begun | |
217aa764 | 8140 | && ! _bfd_elf_compute_section_file_positions (abfd, NULL)) |
b34976b6 | 8141 | return FALSE; |
252b5132 | 8142 | |
0ce398f1 L |
8143 | if (!count) |
8144 | return TRUE; | |
8145 | ||
252b5132 | 8146 | hdr = &elf_section_data (section)->this_hdr; |
0ce398f1 L |
8147 | if (hdr->sh_offset == (file_ptr) -1) |
8148 | { | |
8149 | /* We must compress this section. Write output to the buffer. */ | |
8150 | unsigned char *contents = hdr->contents; | |
8151 | if ((offset + count) > hdr->sh_size | |
8152 | || (section->flags & SEC_ELF_COMPRESS) == 0 | |
8153 | || contents == NULL) | |
8154 | abort (); | |
8155 | memcpy (contents + offset, location, count); | |
8156 | return TRUE; | |
8157 | } | |
dc810e39 AM |
8158 | pos = hdr->sh_offset + offset; |
8159 | if (bfd_seek (abfd, pos, SEEK_SET) != 0 | |
8160 | || bfd_bwrite (location, count, abfd) != count) | |
b34976b6 | 8161 | return FALSE; |
252b5132 | 8162 | |
b34976b6 | 8163 | return TRUE; |
252b5132 RH |
8164 | } |
8165 | ||
8166 | void | |
217aa764 AM |
8167 | _bfd_elf_no_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED, |
8168 | arelent *cache_ptr ATTRIBUTE_UNUSED, | |
8169 | Elf_Internal_Rela *dst ATTRIBUTE_UNUSED) | |
252b5132 RH |
8170 | { |
8171 | abort (); | |
8172 | } | |
8173 | ||
252b5132 RH |
8174 | /* Try to convert a non-ELF reloc into an ELF one. */ |
8175 | ||
b34976b6 | 8176 | bfd_boolean |
217aa764 | 8177 | _bfd_elf_validate_reloc (bfd *abfd, arelent *areloc) |
252b5132 | 8178 | { |
c044fabd | 8179 | /* Check whether we really have an ELF howto. */ |
252b5132 RH |
8180 | |
8181 | if ((*areloc->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec) | |
8182 | { | |
8183 | bfd_reloc_code_real_type code; | |
8184 | reloc_howto_type *howto; | |
8185 | ||
8186 | /* Alien reloc: Try to determine its type to replace it with an | |
c044fabd | 8187 | equivalent ELF reloc. */ |
252b5132 RH |
8188 | |
8189 | if (areloc->howto->pc_relative) | |
8190 | { | |
8191 | switch (areloc->howto->bitsize) | |
8192 | { | |
8193 | case 8: | |
8194 | code = BFD_RELOC_8_PCREL; | |
8195 | break; | |
8196 | case 12: | |
8197 | code = BFD_RELOC_12_PCREL; | |
8198 | break; | |
8199 | case 16: | |
8200 | code = BFD_RELOC_16_PCREL; | |
8201 | break; | |
8202 | case 24: | |
8203 | code = BFD_RELOC_24_PCREL; | |
8204 | break; | |
8205 | case 32: | |
8206 | code = BFD_RELOC_32_PCREL; | |
8207 | break; | |
8208 | case 64: | |
8209 | code = BFD_RELOC_64_PCREL; | |
8210 | break; | |
8211 | default: | |
8212 | goto fail; | |
8213 | } | |
8214 | ||
8215 | howto = bfd_reloc_type_lookup (abfd, code); | |
8216 | ||
8217 | if (areloc->howto->pcrel_offset != howto->pcrel_offset) | |
8218 | { | |
8219 | if (howto->pcrel_offset) | |
8220 | areloc->addend += areloc->address; | |
8221 | else | |
8222 | areloc->addend -= areloc->address; /* addend is unsigned!! */ | |
8223 | } | |
8224 | } | |
8225 | else | |
8226 | { | |
8227 | switch (areloc->howto->bitsize) | |
8228 | { | |
8229 | case 8: | |
8230 | code = BFD_RELOC_8; | |
8231 | break; | |
8232 | case 14: | |
8233 | code = BFD_RELOC_14; | |
8234 | break; | |
8235 | case 16: | |
8236 | code = BFD_RELOC_16; | |
8237 | break; | |
8238 | case 26: | |
8239 | code = BFD_RELOC_26; | |
8240 | break; | |
8241 | case 32: | |
8242 | code = BFD_RELOC_32; | |
8243 | break; | |
8244 | case 64: | |
8245 | code = BFD_RELOC_64; | |
8246 | break; | |
8247 | default: | |
8248 | goto fail; | |
8249 | } | |
8250 | ||
8251 | howto = bfd_reloc_type_lookup (abfd, code); | |
8252 | } | |
8253 | ||
8254 | if (howto) | |
8255 | areloc->howto = howto; | |
8256 | else | |
8257 | goto fail; | |
8258 | } | |
8259 | ||
b34976b6 | 8260 | return TRUE; |
252b5132 RH |
8261 | |
8262 | fail: | |
8263 | (*_bfd_error_handler) | |
d003868e AM |
8264 | (_("%B: unsupported relocation type %s"), |
8265 | abfd, areloc->howto->name); | |
252b5132 | 8266 | bfd_set_error (bfd_error_bad_value); |
b34976b6 | 8267 | return FALSE; |
252b5132 RH |
8268 | } |
8269 | ||
b34976b6 | 8270 | bfd_boolean |
217aa764 | 8271 | _bfd_elf_close_and_cleanup (bfd *abfd) |
252b5132 | 8272 | { |
d9071b0c TG |
8273 | struct elf_obj_tdata *tdata = elf_tdata (abfd); |
8274 | if (bfd_get_format (abfd) == bfd_object && tdata != NULL) | |
252b5132 | 8275 | { |
c0355132 | 8276 | if (elf_tdata (abfd)->o != NULL && elf_shstrtab (abfd) != NULL) |
2b0f7ef9 | 8277 | _bfd_elf_strtab_free (elf_shstrtab (abfd)); |
d9071b0c | 8278 | _bfd_dwarf2_cleanup_debug_info (abfd, &tdata->dwarf2_find_line_info); |
252b5132 RH |
8279 | } |
8280 | ||
8281 | return _bfd_generic_close_and_cleanup (abfd); | |
8282 | } | |
8283 | ||
8284 | /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY | |
8285 | in the relocation's offset. Thus we cannot allow any sort of sanity | |
8286 | range-checking to interfere. There is nothing else to do in processing | |
8287 | this reloc. */ | |
8288 | ||
8289 | bfd_reloc_status_type | |
217aa764 AM |
8290 | _bfd_elf_rel_vtable_reloc_fn |
8291 | (bfd *abfd ATTRIBUTE_UNUSED, arelent *re ATTRIBUTE_UNUSED, | |
fc0a2244 | 8292 | struct bfd_symbol *symbol ATTRIBUTE_UNUSED, |
217aa764 AM |
8293 | void *data ATTRIBUTE_UNUSED, asection *is ATTRIBUTE_UNUSED, |
8294 | bfd *obfd ATTRIBUTE_UNUSED, char **errmsg ATTRIBUTE_UNUSED) | |
252b5132 RH |
8295 | { |
8296 | return bfd_reloc_ok; | |
8297 | } | |
252b5132 RH |
8298 | \f |
8299 | /* Elf core file support. Much of this only works on native | |
8300 | toolchains, since we rely on knowing the | |
8301 | machine-dependent procfs structure in order to pick | |
c044fabd | 8302 | out details about the corefile. */ |
252b5132 RH |
8303 | |
8304 | #ifdef HAVE_SYS_PROCFS_H | |
16231b7b DG |
8305 | /* Needed for new procfs interface on sparc-solaris. */ |
8306 | # define _STRUCTURED_PROC 1 | |
252b5132 RH |
8307 | # include <sys/procfs.h> |
8308 | #endif | |
8309 | ||
261b8d08 PA |
8310 | /* Return a PID that identifies a "thread" for threaded cores, or the |
8311 | PID of the main process for non-threaded cores. */ | |
252b5132 RH |
8312 | |
8313 | static int | |
217aa764 | 8314 | elfcore_make_pid (bfd *abfd) |
252b5132 | 8315 | { |
261b8d08 PA |
8316 | int pid; |
8317 | ||
228e534f | 8318 | pid = elf_tdata (abfd)->core->lwpid; |
261b8d08 | 8319 | if (pid == 0) |
228e534f | 8320 | pid = elf_tdata (abfd)->core->pid; |
261b8d08 PA |
8321 | |
8322 | return pid; | |
252b5132 RH |
8323 | } |
8324 | ||
252b5132 RH |
8325 | /* If there isn't a section called NAME, make one, using |
8326 | data from SECT. Note, this function will generate a | |
8327 | reference to NAME, so you shouldn't deallocate or | |
c044fabd | 8328 | overwrite it. */ |
252b5132 | 8329 | |
b34976b6 | 8330 | static bfd_boolean |
217aa764 | 8331 | elfcore_maybe_make_sect (bfd *abfd, char *name, asection *sect) |
252b5132 | 8332 | { |
c044fabd | 8333 | asection *sect2; |
252b5132 RH |
8334 | |
8335 | if (bfd_get_section_by_name (abfd, name) != NULL) | |
b34976b6 | 8336 | return TRUE; |
252b5132 | 8337 | |
117ed4f8 | 8338 | sect2 = bfd_make_section_with_flags (abfd, name, sect->flags); |
252b5132 | 8339 | if (sect2 == NULL) |
b34976b6 | 8340 | return FALSE; |
252b5132 | 8341 | |
eea6121a | 8342 | sect2->size = sect->size; |
252b5132 | 8343 | sect2->filepos = sect->filepos; |
252b5132 | 8344 | sect2->alignment_power = sect->alignment_power; |
b34976b6 | 8345 | return TRUE; |
252b5132 RH |
8346 | } |
8347 | ||
bb0082d6 AM |
8348 | /* Create a pseudosection containing SIZE bytes at FILEPOS. This |
8349 | actually creates up to two pseudosections: | |
8350 | - For the single-threaded case, a section named NAME, unless | |
8351 | such a section already exists. | |
8352 | - For the multi-threaded case, a section named "NAME/PID", where | |
8353 | PID is elfcore_make_pid (abfd). | |
8354 | Both pseudosections have identical contents. */ | |
b34976b6 | 8355 | bfd_boolean |
217aa764 AM |
8356 | _bfd_elfcore_make_pseudosection (bfd *abfd, |
8357 | char *name, | |
8358 | size_t size, | |
8359 | ufile_ptr filepos) | |
bb0082d6 AM |
8360 | { |
8361 | char buf[100]; | |
8362 | char *threaded_name; | |
d4c88bbb | 8363 | size_t len; |
bb0082d6 AM |
8364 | asection *sect; |
8365 | ||
8366 | /* Build the section name. */ | |
8367 | ||
8368 | sprintf (buf, "%s/%d", name, elfcore_make_pid (abfd)); | |
d4c88bbb | 8369 | len = strlen (buf) + 1; |
a50b1753 | 8370 | threaded_name = (char *) bfd_alloc (abfd, len); |
bb0082d6 | 8371 | if (threaded_name == NULL) |
b34976b6 | 8372 | return FALSE; |
d4c88bbb | 8373 | memcpy (threaded_name, buf, len); |
bb0082d6 | 8374 | |
117ed4f8 AM |
8375 | sect = bfd_make_section_anyway_with_flags (abfd, threaded_name, |
8376 | SEC_HAS_CONTENTS); | |
bb0082d6 | 8377 | if (sect == NULL) |
b34976b6 | 8378 | return FALSE; |
eea6121a | 8379 | sect->size = size; |
bb0082d6 | 8380 | sect->filepos = filepos; |
bb0082d6 AM |
8381 | sect->alignment_power = 2; |
8382 | ||
936e320b | 8383 | return elfcore_maybe_make_sect (abfd, name, sect); |
bb0082d6 AM |
8384 | } |
8385 | ||
252b5132 | 8386 | /* prstatus_t exists on: |
4a938328 | 8387 | solaris 2.5+ |
252b5132 RH |
8388 | linux 2.[01] + glibc |
8389 | unixware 4.2 | |
8390 | */ | |
8391 | ||
8392 | #if defined (HAVE_PRSTATUS_T) | |
a7b97311 | 8393 | |
b34976b6 | 8394 | static bfd_boolean |
217aa764 | 8395 | elfcore_grok_prstatus (bfd *abfd, Elf_Internal_Note *note) |
252b5132 | 8396 | { |
eea6121a | 8397 | size_t size; |
7ee38065 | 8398 | int offset; |
252b5132 | 8399 | |
4a938328 MS |
8400 | if (note->descsz == sizeof (prstatus_t)) |
8401 | { | |
8402 | prstatus_t prstat; | |
252b5132 | 8403 | |
eea6121a | 8404 | size = sizeof (prstat.pr_reg); |
7ee38065 | 8405 | offset = offsetof (prstatus_t, pr_reg); |
4a938328 | 8406 | memcpy (&prstat, note->descdata, sizeof (prstat)); |
252b5132 | 8407 | |
fa49d224 NC |
8408 | /* Do not overwrite the core signal if it |
8409 | has already been set by another thread. */ | |
228e534f AM |
8410 | if (elf_tdata (abfd)->core->signal == 0) |
8411 | elf_tdata (abfd)->core->signal = prstat.pr_cursig; | |
8412 | if (elf_tdata (abfd)->core->pid == 0) | |
8413 | elf_tdata (abfd)->core->pid = prstat.pr_pid; | |
252b5132 | 8414 | |
4a938328 MS |
8415 | /* pr_who exists on: |
8416 | solaris 2.5+ | |
8417 | unixware 4.2 | |
8418 | pr_who doesn't exist on: | |
8419 | linux 2.[01] | |
8420 | */ | |
252b5132 | 8421 | #if defined (HAVE_PRSTATUS_T_PR_WHO) |
228e534f | 8422 | elf_tdata (abfd)->core->lwpid = prstat.pr_who; |
261b8d08 | 8423 | #else |
228e534f | 8424 | elf_tdata (abfd)->core->lwpid = prstat.pr_pid; |
252b5132 | 8425 | #endif |
4a938328 | 8426 | } |
7ee38065 | 8427 | #if defined (HAVE_PRSTATUS32_T) |
4a938328 MS |
8428 | else if (note->descsz == sizeof (prstatus32_t)) |
8429 | { | |
8430 | /* 64-bit host, 32-bit corefile */ | |
8431 | prstatus32_t prstat; | |
8432 | ||
eea6121a | 8433 | size = sizeof (prstat.pr_reg); |
7ee38065 | 8434 | offset = offsetof (prstatus32_t, pr_reg); |
4a938328 MS |
8435 | memcpy (&prstat, note->descdata, sizeof (prstat)); |
8436 | ||
fa49d224 NC |
8437 | /* Do not overwrite the core signal if it |
8438 | has already been set by another thread. */ | |
228e534f AM |
8439 | if (elf_tdata (abfd)->core->signal == 0) |
8440 | elf_tdata (abfd)->core->signal = prstat.pr_cursig; | |
8441 | if (elf_tdata (abfd)->core->pid == 0) | |
8442 | elf_tdata (abfd)->core->pid = prstat.pr_pid; | |
4a938328 MS |
8443 | |
8444 | /* pr_who exists on: | |
8445 | solaris 2.5+ | |
8446 | unixware 4.2 | |
8447 | pr_who doesn't exist on: | |
8448 | linux 2.[01] | |
8449 | */ | |
7ee38065 | 8450 | #if defined (HAVE_PRSTATUS32_T_PR_WHO) |
228e534f | 8451 | elf_tdata (abfd)->core->lwpid = prstat.pr_who; |
261b8d08 | 8452 | #else |
228e534f | 8453 | elf_tdata (abfd)->core->lwpid = prstat.pr_pid; |
4a938328 MS |
8454 | #endif |
8455 | } | |
7ee38065 | 8456 | #endif /* HAVE_PRSTATUS32_T */ |
4a938328 MS |
8457 | else |
8458 | { | |
8459 | /* Fail - we don't know how to handle any other | |
8460 | note size (ie. data object type). */ | |
b34976b6 | 8461 | return TRUE; |
4a938328 | 8462 | } |
252b5132 | 8463 | |
bb0082d6 | 8464 | /* Make a ".reg/999" section and a ".reg" section. */ |
936e320b | 8465 | return _bfd_elfcore_make_pseudosection (abfd, ".reg", |
eea6121a | 8466 | size, note->descpos + offset); |
252b5132 RH |
8467 | } |
8468 | #endif /* defined (HAVE_PRSTATUS_T) */ | |
8469 | ||
bb0082d6 | 8470 | /* Create a pseudosection containing the exact contents of NOTE. */ |
b34976b6 | 8471 | static bfd_boolean |
217aa764 AM |
8472 | elfcore_make_note_pseudosection (bfd *abfd, |
8473 | char *name, | |
8474 | Elf_Internal_Note *note) | |
252b5132 | 8475 | { |
936e320b AM |
8476 | return _bfd_elfcore_make_pseudosection (abfd, name, |
8477 | note->descsz, note->descpos); | |
252b5132 RH |
8478 | } |
8479 | ||
ff08c6bb JB |
8480 | /* There isn't a consistent prfpregset_t across platforms, |
8481 | but it doesn't matter, because we don't have to pick this | |
c044fabd KH |
8482 | data structure apart. */ |
8483 | ||
b34976b6 | 8484 | static bfd_boolean |
217aa764 | 8485 | elfcore_grok_prfpreg (bfd *abfd, Elf_Internal_Note *note) |
ff08c6bb JB |
8486 | { |
8487 | return elfcore_make_note_pseudosection (abfd, ".reg2", note); | |
8488 | } | |
8489 | ||
ff08c6bb | 8490 | /* Linux dumps the Intel SSE regs in a note named "LINUX" with a note |
971d4640 | 8491 | type of NT_PRXFPREG. Just include the whole note's contents |
ff08c6bb | 8492 | literally. */ |
c044fabd | 8493 | |
b34976b6 | 8494 | static bfd_boolean |
217aa764 | 8495 | elfcore_grok_prxfpreg (bfd *abfd, Elf_Internal_Note *note) |
ff08c6bb JB |
8496 | { |
8497 | return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note); | |
8498 | } | |
8499 | ||
4339cae0 L |
8500 | /* Linux dumps the Intel XSAVE extended state in a note named "LINUX" |
8501 | with a note type of NT_X86_XSTATE. Just include the whole note's | |
8502 | contents literally. */ | |
8503 | ||
8504 | static bfd_boolean | |
8505 | elfcore_grok_xstatereg (bfd *abfd, Elf_Internal_Note *note) | |
8506 | { | |
8507 | return elfcore_make_note_pseudosection (abfd, ".reg-xstate", note); | |
8508 | } | |
8509 | ||
97753bd5 AM |
8510 | static bfd_boolean |
8511 | elfcore_grok_ppc_vmx (bfd *abfd, Elf_Internal_Note *note) | |
8512 | { | |
8513 | return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vmx", note); | |
8514 | } | |
8515 | ||
89eeb0bc LM |
8516 | static bfd_boolean |
8517 | elfcore_grok_ppc_vsx (bfd *abfd, Elf_Internal_Note *note) | |
8518 | { | |
8519 | return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vsx", note); | |
8520 | } | |
97753bd5 | 8521 | |
0675e188 UW |
8522 | static bfd_boolean |
8523 | elfcore_grok_s390_high_gprs (bfd *abfd, Elf_Internal_Note *note) | |
8524 | { | |
8525 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-high-gprs", note); | |
8526 | } | |
8527 | ||
d7eeb400 MS |
8528 | static bfd_boolean |
8529 | elfcore_grok_s390_timer (bfd *abfd, Elf_Internal_Note *note) | |
8530 | { | |
8531 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-timer", note); | |
8532 | } | |
8533 | ||
8534 | static bfd_boolean | |
8535 | elfcore_grok_s390_todcmp (bfd *abfd, Elf_Internal_Note *note) | |
8536 | { | |
8537 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-todcmp", note); | |
8538 | } | |
8539 | ||
8540 | static bfd_boolean | |
8541 | elfcore_grok_s390_todpreg (bfd *abfd, Elf_Internal_Note *note) | |
8542 | { | |
8543 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-todpreg", note); | |
8544 | } | |
8545 | ||
8546 | static bfd_boolean | |
8547 | elfcore_grok_s390_ctrs (bfd *abfd, Elf_Internal_Note *note) | |
8548 | { | |
8549 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-ctrs", note); | |
8550 | } | |
8551 | ||
8552 | static bfd_boolean | |
8553 | elfcore_grok_s390_prefix (bfd *abfd, Elf_Internal_Note *note) | |
8554 | { | |
8555 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-prefix", note); | |
8556 | } | |
8557 | ||
355b81d9 UW |
8558 | static bfd_boolean |
8559 | elfcore_grok_s390_last_break (bfd *abfd, Elf_Internal_Note *note) | |
8560 | { | |
8561 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-last-break", note); | |
8562 | } | |
8563 | ||
8564 | static bfd_boolean | |
8565 | elfcore_grok_s390_system_call (bfd *abfd, Elf_Internal_Note *note) | |
8566 | { | |
8567 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-system-call", note); | |
8568 | } | |
8569 | ||
abb3f6cc NC |
8570 | static bfd_boolean |
8571 | elfcore_grok_s390_tdb (bfd *abfd, Elf_Internal_Note *note) | |
8572 | { | |
8573 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-tdb", note); | |
8574 | } | |
8575 | ||
4ef9f41a AA |
8576 | static bfd_boolean |
8577 | elfcore_grok_s390_vxrs_low (bfd *abfd, Elf_Internal_Note *note) | |
8578 | { | |
8579 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-vxrs-low", note); | |
8580 | } | |
8581 | ||
8582 | static bfd_boolean | |
8583 | elfcore_grok_s390_vxrs_high (bfd *abfd, Elf_Internal_Note *note) | |
8584 | { | |
8585 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-vxrs-high", note); | |
8586 | } | |
8587 | ||
faa9a424 UW |
8588 | static bfd_boolean |
8589 | elfcore_grok_arm_vfp (bfd *abfd, Elf_Internal_Note *note) | |
8590 | { | |
8591 | return elfcore_make_note_pseudosection (abfd, ".reg-arm-vfp", note); | |
8592 | } | |
8593 | ||
652451f8 YZ |
8594 | static bfd_boolean |
8595 | elfcore_grok_aarch_tls (bfd *abfd, Elf_Internal_Note *note) | |
8596 | { | |
8597 | return elfcore_make_note_pseudosection (abfd, ".reg-aarch-tls", note); | |
8598 | } | |
8599 | ||
8600 | static bfd_boolean | |
8601 | elfcore_grok_aarch_hw_break (bfd *abfd, Elf_Internal_Note *note) | |
8602 | { | |
8603 | return elfcore_make_note_pseudosection (abfd, ".reg-aarch-hw-break", note); | |
8604 | } | |
8605 | ||
8606 | static bfd_boolean | |
8607 | elfcore_grok_aarch_hw_watch (bfd *abfd, Elf_Internal_Note *note) | |
8608 | { | |
8609 | return elfcore_make_note_pseudosection (abfd, ".reg-aarch-hw-watch", note); | |
8610 | } | |
8611 | ||
252b5132 | 8612 | #if defined (HAVE_PRPSINFO_T) |
4a938328 | 8613 | typedef prpsinfo_t elfcore_psinfo_t; |
7ee38065 | 8614 | #if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */ |
4a938328 MS |
8615 | typedef prpsinfo32_t elfcore_psinfo32_t; |
8616 | #endif | |
252b5132 RH |
8617 | #endif |
8618 | ||
8619 | #if defined (HAVE_PSINFO_T) | |
4a938328 | 8620 | typedef psinfo_t elfcore_psinfo_t; |
7ee38065 | 8621 | #if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */ |
4a938328 MS |
8622 | typedef psinfo32_t elfcore_psinfo32_t; |
8623 | #endif | |
252b5132 RH |
8624 | #endif |
8625 | ||
252b5132 RH |
8626 | /* return a malloc'ed copy of a string at START which is at |
8627 | most MAX bytes long, possibly without a terminating '\0'. | |
c044fabd | 8628 | the copy will always have a terminating '\0'. */ |
252b5132 | 8629 | |
936e320b | 8630 | char * |
217aa764 | 8631 | _bfd_elfcore_strndup (bfd *abfd, char *start, size_t max) |
252b5132 | 8632 | { |
dc810e39 | 8633 | char *dups; |
a50b1753 | 8634 | char *end = (char *) memchr (start, '\0', max); |
dc810e39 | 8635 | size_t len; |
252b5132 RH |
8636 | |
8637 | if (end == NULL) | |
8638 | len = max; | |
8639 | else | |
8640 | len = end - start; | |
8641 | ||
a50b1753 | 8642 | dups = (char *) bfd_alloc (abfd, len + 1); |
dc810e39 | 8643 | if (dups == NULL) |
252b5132 RH |
8644 | return NULL; |
8645 | ||
dc810e39 AM |
8646 | memcpy (dups, start, len); |
8647 | dups[len] = '\0'; | |
252b5132 | 8648 | |
dc810e39 | 8649 | return dups; |
252b5132 RH |
8650 | } |
8651 | ||
bb0082d6 | 8652 | #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) |
b34976b6 | 8653 | static bfd_boolean |
217aa764 | 8654 | elfcore_grok_psinfo (bfd *abfd, Elf_Internal_Note *note) |
252b5132 | 8655 | { |
4a938328 MS |
8656 | if (note->descsz == sizeof (elfcore_psinfo_t)) |
8657 | { | |
8658 | elfcore_psinfo_t psinfo; | |
252b5132 | 8659 | |
7ee38065 | 8660 | memcpy (&psinfo, note->descdata, sizeof (psinfo)); |
252b5132 | 8661 | |
335e41d4 | 8662 | #if defined (HAVE_PSINFO_T_PR_PID) || defined (HAVE_PRPSINFO_T_PR_PID) |
228e534f | 8663 | elf_tdata (abfd)->core->pid = psinfo.pr_pid; |
335e41d4 | 8664 | #endif |
228e534f | 8665 | elf_tdata (abfd)->core->program |
936e320b AM |
8666 | = _bfd_elfcore_strndup (abfd, psinfo.pr_fname, |
8667 | sizeof (psinfo.pr_fname)); | |
252b5132 | 8668 | |
228e534f | 8669 | elf_tdata (abfd)->core->command |
936e320b AM |
8670 | = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs, |
8671 | sizeof (psinfo.pr_psargs)); | |
4a938328 | 8672 | } |
7ee38065 | 8673 | #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T) |
4a938328 MS |
8674 | else if (note->descsz == sizeof (elfcore_psinfo32_t)) |
8675 | { | |
8676 | /* 64-bit host, 32-bit corefile */ | |
8677 | elfcore_psinfo32_t psinfo; | |
8678 | ||
7ee38065 | 8679 | memcpy (&psinfo, note->descdata, sizeof (psinfo)); |
252b5132 | 8680 | |
335e41d4 | 8681 | #if defined (HAVE_PSINFO32_T_PR_PID) || defined (HAVE_PRPSINFO32_T_PR_PID) |
228e534f | 8682 | elf_tdata (abfd)->core->pid = psinfo.pr_pid; |
335e41d4 | 8683 | #endif |
228e534f | 8684 | elf_tdata (abfd)->core->program |
936e320b AM |
8685 | = _bfd_elfcore_strndup (abfd, psinfo.pr_fname, |
8686 | sizeof (psinfo.pr_fname)); | |
4a938328 | 8687 | |
228e534f | 8688 | elf_tdata (abfd)->core->command |
936e320b AM |
8689 | = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs, |
8690 | sizeof (psinfo.pr_psargs)); | |
4a938328 MS |
8691 | } |
8692 | #endif | |
8693 | ||
8694 | else | |
8695 | { | |
8696 | /* Fail - we don't know how to handle any other | |
8697 | note size (ie. data object type). */ | |
b34976b6 | 8698 | return TRUE; |
4a938328 | 8699 | } |
252b5132 RH |
8700 | |
8701 | /* Note that for some reason, a spurious space is tacked | |
8702 | onto the end of the args in some (at least one anyway) | |
c044fabd | 8703 | implementations, so strip it off if it exists. */ |
252b5132 RH |
8704 | |
8705 | { | |
228e534f | 8706 | char *command = elf_tdata (abfd)->core->command; |
252b5132 RH |
8707 | int n = strlen (command); |
8708 | ||
8709 | if (0 < n && command[n - 1] == ' ') | |
8710 | command[n - 1] = '\0'; | |
8711 | } | |
8712 | ||
b34976b6 | 8713 | return TRUE; |
252b5132 RH |
8714 | } |
8715 | #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */ | |
8716 | ||
252b5132 | 8717 | #if defined (HAVE_PSTATUS_T) |
b34976b6 | 8718 | static bfd_boolean |
217aa764 | 8719 | elfcore_grok_pstatus (bfd *abfd, Elf_Internal_Note *note) |
252b5132 | 8720 | { |
f572a39d AM |
8721 | if (note->descsz == sizeof (pstatus_t) |
8722 | #if defined (HAVE_PXSTATUS_T) | |
8723 | || note->descsz == sizeof (pxstatus_t) | |
8724 | #endif | |
8725 | ) | |
4a938328 MS |
8726 | { |
8727 | pstatus_t pstat; | |
252b5132 | 8728 | |
4a938328 | 8729 | memcpy (&pstat, note->descdata, sizeof (pstat)); |
252b5132 | 8730 | |
228e534f | 8731 | elf_tdata (abfd)->core->pid = pstat.pr_pid; |
4a938328 | 8732 | } |
7ee38065 | 8733 | #if defined (HAVE_PSTATUS32_T) |
4a938328 MS |
8734 | else if (note->descsz == sizeof (pstatus32_t)) |
8735 | { | |
8736 | /* 64-bit host, 32-bit corefile */ | |
8737 | pstatus32_t pstat; | |
252b5132 | 8738 | |
4a938328 | 8739 | memcpy (&pstat, note->descdata, sizeof (pstat)); |
252b5132 | 8740 | |
228e534f | 8741 | elf_tdata (abfd)->core->pid = pstat.pr_pid; |
4a938328 MS |
8742 | } |
8743 | #endif | |
252b5132 RH |
8744 | /* Could grab some more details from the "representative" |
8745 | lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an | |
c044fabd | 8746 | NT_LWPSTATUS note, presumably. */ |
252b5132 | 8747 | |
b34976b6 | 8748 | return TRUE; |
252b5132 RH |
8749 | } |
8750 | #endif /* defined (HAVE_PSTATUS_T) */ | |
8751 | ||
252b5132 | 8752 | #if defined (HAVE_LWPSTATUS_T) |
b34976b6 | 8753 | static bfd_boolean |
217aa764 | 8754 | elfcore_grok_lwpstatus (bfd *abfd, Elf_Internal_Note *note) |
252b5132 RH |
8755 | { |
8756 | lwpstatus_t lwpstat; | |
8757 | char buf[100]; | |
c044fabd | 8758 | char *name; |
d4c88bbb | 8759 | size_t len; |
c044fabd | 8760 | asection *sect; |
252b5132 | 8761 | |
f572a39d AM |
8762 | if (note->descsz != sizeof (lwpstat) |
8763 | #if defined (HAVE_LWPXSTATUS_T) | |
8764 | && note->descsz != sizeof (lwpxstatus_t) | |
8765 | #endif | |
8766 | ) | |
b34976b6 | 8767 | return TRUE; |
252b5132 RH |
8768 | |
8769 | memcpy (&lwpstat, note->descdata, sizeof (lwpstat)); | |
8770 | ||
228e534f | 8771 | elf_tdata (abfd)->core->lwpid = lwpstat.pr_lwpid; |
a1504221 JB |
8772 | /* Do not overwrite the core signal if it has already been set by |
8773 | another thread. */ | |
228e534f AM |
8774 | if (elf_tdata (abfd)->core->signal == 0) |
8775 | elf_tdata (abfd)->core->signal = lwpstat.pr_cursig; | |
252b5132 | 8776 | |
c044fabd | 8777 | /* Make a ".reg/999" section. */ |
252b5132 RH |
8778 | |
8779 | sprintf (buf, ".reg/%d", elfcore_make_pid (abfd)); | |
d4c88bbb | 8780 | len = strlen (buf) + 1; |
217aa764 | 8781 | name = bfd_alloc (abfd, len); |
252b5132 | 8782 | if (name == NULL) |
b34976b6 | 8783 | return FALSE; |
d4c88bbb | 8784 | memcpy (name, buf, len); |
252b5132 | 8785 | |
117ed4f8 | 8786 | sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); |
252b5132 | 8787 | if (sect == NULL) |
b34976b6 | 8788 | return FALSE; |
252b5132 RH |
8789 | |
8790 | #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT) | |
eea6121a | 8791 | sect->size = sizeof (lwpstat.pr_context.uc_mcontext.gregs); |
252b5132 RH |
8792 | sect->filepos = note->descpos |
8793 | + offsetof (lwpstatus_t, pr_context.uc_mcontext.gregs); | |
8794 | #endif | |
8795 | ||
8796 | #if defined (HAVE_LWPSTATUS_T_PR_REG) | |
eea6121a | 8797 | sect->size = sizeof (lwpstat.pr_reg); |
252b5132 RH |
8798 | sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_reg); |
8799 | #endif | |
8800 | ||
252b5132 RH |
8801 | sect->alignment_power = 2; |
8802 | ||
8803 | if (!elfcore_maybe_make_sect (abfd, ".reg", sect)) | |
b34976b6 | 8804 | return FALSE; |
252b5132 RH |
8805 | |
8806 | /* Make a ".reg2/999" section */ | |
8807 | ||
8808 | sprintf (buf, ".reg2/%d", elfcore_make_pid (abfd)); | |
d4c88bbb | 8809 | len = strlen (buf) + 1; |
217aa764 | 8810 | name = bfd_alloc (abfd, len); |
252b5132 | 8811 | if (name == NULL) |
b34976b6 | 8812 | return FALSE; |
d4c88bbb | 8813 | memcpy (name, buf, len); |
252b5132 | 8814 | |
117ed4f8 | 8815 | sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); |
252b5132 | 8816 | if (sect == NULL) |
b34976b6 | 8817 | return FALSE; |
252b5132 RH |
8818 | |
8819 | #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT) | |
eea6121a | 8820 | sect->size = sizeof (lwpstat.pr_context.uc_mcontext.fpregs); |
252b5132 RH |
8821 | sect->filepos = note->descpos |
8822 | + offsetof (lwpstatus_t, pr_context.uc_mcontext.fpregs); | |
8823 | #endif | |
8824 | ||
8825 | #if defined (HAVE_LWPSTATUS_T_PR_FPREG) | |
eea6121a | 8826 | sect->size = sizeof (lwpstat.pr_fpreg); |
252b5132 RH |
8827 | sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_fpreg); |
8828 | #endif | |
8829 | ||
252b5132 RH |
8830 | sect->alignment_power = 2; |
8831 | ||
936e320b | 8832 | return elfcore_maybe_make_sect (abfd, ".reg2", sect); |
252b5132 RH |
8833 | } |
8834 | #endif /* defined (HAVE_LWPSTATUS_T) */ | |
8835 | ||
b34976b6 | 8836 | static bfd_boolean |
217aa764 | 8837 | elfcore_grok_win32pstatus (bfd *abfd, Elf_Internal_Note *note) |
16e9c715 NC |
8838 | { |
8839 | char buf[30]; | |
c044fabd | 8840 | char *name; |
d4c88bbb | 8841 | size_t len; |
c044fabd | 8842 | asection *sect; |
4a6636fb PA |
8843 | int type; |
8844 | int is_active_thread; | |
8845 | bfd_vma base_addr; | |
16e9c715 | 8846 | |
4a6636fb | 8847 | if (note->descsz < 728) |
b34976b6 | 8848 | return TRUE; |
16e9c715 | 8849 | |
4a6636fb PA |
8850 | if (! CONST_STRNEQ (note->namedata, "win32")) |
8851 | return TRUE; | |
8852 | ||
8853 | type = bfd_get_32 (abfd, note->descdata); | |
c044fabd | 8854 | |
4a6636fb | 8855 | switch (type) |
16e9c715 | 8856 | { |
4a6636fb | 8857 | case 1 /* NOTE_INFO_PROCESS */: |
228e534f | 8858 | /* FIXME: need to add ->core->command. */ |
4a6636fb | 8859 | /* process_info.pid */ |
228e534f | 8860 | elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, note->descdata + 8); |
4a6636fb | 8861 | /* process_info.signal */ |
228e534f | 8862 | elf_tdata (abfd)->core->signal = bfd_get_32 (abfd, note->descdata + 12); |
c044fabd | 8863 | break; |
16e9c715 | 8864 | |
4a6636fb | 8865 | case 2 /* NOTE_INFO_THREAD */: |
16e9c715 | 8866 | /* Make a ".reg/999" section. */ |
4a6636fb PA |
8867 | /* thread_info.tid */ |
8868 | sprintf (buf, ".reg/%ld", (long) bfd_get_32 (abfd, note->descdata + 8)); | |
c044fabd | 8869 | |
d4c88bbb | 8870 | len = strlen (buf) + 1; |
a50b1753 | 8871 | name = (char *) bfd_alloc (abfd, len); |
16e9c715 | 8872 | if (name == NULL) |
b34976b6 | 8873 | return FALSE; |
c044fabd | 8874 | |
d4c88bbb | 8875 | memcpy (name, buf, len); |
16e9c715 | 8876 | |
117ed4f8 | 8877 | sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); |
16e9c715 | 8878 | if (sect == NULL) |
b34976b6 | 8879 | return FALSE; |
c044fabd | 8880 | |
4a6636fb PA |
8881 | /* sizeof (thread_info.thread_context) */ |
8882 | sect->size = 716; | |
8883 | /* offsetof (thread_info.thread_context) */ | |
8884 | sect->filepos = note->descpos + 12; | |
16e9c715 NC |
8885 | sect->alignment_power = 2; |
8886 | ||
4a6636fb PA |
8887 | /* thread_info.is_active_thread */ |
8888 | is_active_thread = bfd_get_32 (abfd, note->descdata + 8); | |
8889 | ||
8890 | if (is_active_thread) | |
16e9c715 | 8891 | if (! elfcore_maybe_make_sect (abfd, ".reg", sect)) |
b34976b6 | 8892 | return FALSE; |
16e9c715 NC |
8893 | break; |
8894 | ||
4a6636fb | 8895 | case 3 /* NOTE_INFO_MODULE */: |
16e9c715 | 8896 | /* Make a ".module/xxxxxxxx" section. */ |
4a6636fb PA |
8897 | /* module_info.base_address */ |
8898 | base_addr = bfd_get_32 (abfd, note->descdata + 4); | |
0af1713e | 8899 | sprintf (buf, ".module/%08lx", (unsigned long) base_addr); |
c044fabd | 8900 | |
d4c88bbb | 8901 | len = strlen (buf) + 1; |
a50b1753 | 8902 | name = (char *) bfd_alloc (abfd, len); |
16e9c715 | 8903 | if (name == NULL) |
b34976b6 | 8904 | return FALSE; |
c044fabd | 8905 | |
d4c88bbb | 8906 | memcpy (name, buf, len); |
252b5132 | 8907 | |
117ed4f8 | 8908 | sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); |
c044fabd | 8909 | |
16e9c715 | 8910 | if (sect == NULL) |
b34976b6 | 8911 | return FALSE; |
c044fabd | 8912 | |
eea6121a | 8913 | sect->size = note->descsz; |
16e9c715 | 8914 | sect->filepos = note->descpos; |
16e9c715 NC |
8915 | sect->alignment_power = 2; |
8916 | break; | |
8917 | ||
8918 | default: | |
b34976b6 | 8919 | return TRUE; |
16e9c715 NC |
8920 | } |
8921 | ||
b34976b6 | 8922 | return TRUE; |
16e9c715 | 8923 | } |
252b5132 | 8924 | |
b34976b6 | 8925 | static bfd_boolean |
217aa764 | 8926 | elfcore_grok_note (bfd *abfd, Elf_Internal_Note *note) |
252b5132 | 8927 | { |
9c5bfbb7 | 8928 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
bb0082d6 | 8929 | |
252b5132 RH |
8930 | switch (note->type) |
8931 | { | |
8932 | default: | |
b34976b6 | 8933 | return TRUE; |
252b5132 | 8934 | |
252b5132 | 8935 | case NT_PRSTATUS: |
bb0082d6 AM |
8936 | if (bed->elf_backend_grok_prstatus) |
8937 | if ((*bed->elf_backend_grok_prstatus) (abfd, note)) | |
b34976b6 | 8938 | return TRUE; |
bb0082d6 | 8939 | #if defined (HAVE_PRSTATUS_T) |
252b5132 | 8940 | return elfcore_grok_prstatus (abfd, note); |
bb0082d6 | 8941 | #else |
b34976b6 | 8942 | return TRUE; |
252b5132 RH |
8943 | #endif |
8944 | ||
8945 | #if defined (HAVE_PSTATUS_T) | |
8946 | case NT_PSTATUS: | |
8947 | return elfcore_grok_pstatus (abfd, note); | |
8948 | #endif | |
8949 | ||
8950 | #if defined (HAVE_LWPSTATUS_T) | |
8951 | case NT_LWPSTATUS: | |
8952 | return elfcore_grok_lwpstatus (abfd, note); | |
8953 | #endif | |
8954 | ||
8955 | case NT_FPREGSET: /* FIXME: rename to NT_PRFPREG */ | |
8956 | return elfcore_grok_prfpreg (abfd, note); | |
8957 | ||
c044fabd | 8958 | case NT_WIN32PSTATUS: |
16e9c715 | 8959 | return elfcore_grok_win32pstatus (abfd, note); |
16e9c715 | 8960 | |
c044fabd | 8961 | case NT_PRXFPREG: /* Linux SSE extension */ |
e377ab71 MK |
8962 | if (note->namesz == 6 |
8963 | && strcmp (note->namedata, "LINUX") == 0) | |
ff08c6bb JB |
8964 | return elfcore_grok_prxfpreg (abfd, note); |
8965 | else | |
b34976b6 | 8966 | return TRUE; |
ff08c6bb | 8967 | |
4339cae0 L |
8968 | case NT_X86_XSTATE: /* Linux XSAVE extension */ |
8969 | if (note->namesz == 6 | |
8970 | && strcmp (note->namedata, "LINUX") == 0) | |
8971 | return elfcore_grok_xstatereg (abfd, note); | |
97de3545 JB |
8972 | else if (note->namesz == 8 |
8973 | && strcmp (note->namedata, "FreeBSD") == 0) | |
8974 | return elfcore_grok_xstatereg (abfd, note); | |
4339cae0 L |
8975 | else |
8976 | return TRUE; | |
8977 | ||
97753bd5 AM |
8978 | case NT_PPC_VMX: |
8979 | if (note->namesz == 6 | |
8980 | && strcmp (note->namedata, "LINUX") == 0) | |
8981 | return elfcore_grok_ppc_vmx (abfd, note); | |
8982 | else | |
8983 | return TRUE; | |
8984 | ||
89eeb0bc LM |
8985 | case NT_PPC_VSX: |
8986 | if (note->namesz == 6 | |
8987 | && strcmp (note->namedata, "LINUX") == 0) | |
8988 | return elfcore_grok_ppc_vsx (abfd, note); | |
8989 | else | |
8990 | return TRUE; | |
8991 | ||
0675e188 UW |
8992 | case NT_S390_HIGH_GPRS: |
8993 | if (note->namesz == 6 | |
8994 | && strcmp (note->namedata, "LINUX") == 0) | |
8995 | return elfcore_grok_s390_high_gprs (abfd, note); | |
8996 | else | |
8997 | return TRUE; | |
8998 | ||
d7eeb400 MS |
8999 | case NT_S390_TIMER: |
9000 | if (note->namesz == 6 | |
9001 | && strcmp (note->namedata, "LINUX") == 0) | |
9002 | return elfcore_grok_s390_timer (abfd, note); | |
9003 | else | |
9004 | return TRUE; | |
9005 | ||
9006 | case NT_S390_TODCMP: | |
9007 | if (note->namesz == 6 | |
9008 | && strcmp (note->namedata, "LINUX") == 0) | |
9009 | return elfcore_grok_s390_todcmp (abfd, note); | |
9010 | else | |
9011 | return TRUE; | |
9012 | ||
9013 | case NT_S390_TODPREG: | |
9014 | if (note->namesz == 6 | |
9015 | && strcmp (note->namedata, "LINUX") == 0) | |
9016 | return elfcore_grok_s390_todpreg (abfd, note); | |
9017 | else | |
9018 | return TRUE; | |
9019 | ||
9020 | case NT_S390_CTRS: | |
9021 | if (note->namesz == 6 | |
9022 | && strcmp (note->namedata, "LINUX") == 0) | |
9023 | return elfcore_grok_s390_ctrs (abfd, note); | |
9024 | else | |
9025 | return TRUE; | |
9026 | ||
9027 | case NT_S390_PREFIX: | |
9028 | if (note->namesz == 6 | |
9029 | && strcmp (note->namedata, "LINUX") == 0) | |
9030 | return elfcore_grok_s390_prefix (abfd, note); | |
9031 | else | |
9032 | return TRUE; | |
9033 | ||
355b81d9 UW |
9034 | case NT_S390_LAST_BREAK: |
9035 | if (note->namesz == 6 | |
9036 | && strcmp (note->namedata, "LINUX") == 0) | |
9037 | return elfcore_grok_s390_last_break (abfd, note); | |
9038 | else | |
9039 | return TRUE; | |
9040 | ||
9041 | case NT_S390_SYSTEM_CALL: | |
9042 | if (note->namesz == 6 | |
9043 | && strcmp (note->namedata, "LINUX") == 0) | |
9044 | return elfcore_grok_s390_system_call (abfd, note); | |
9045 | else | |
9046 | return TRUE; | |
9047 | ||
abb3f6cc NC |
9048 | case NT_S390_TDB: |
9049 | if (note->namesz == 6 | |
9050 | && strcmp (note->namedata, "LINUX") == 0) | |
9051 | return elfcore_grok_s390_tdb (abfd, note); | |
9052 | else | |
9053 | return TRUE; | |
9054 | ||
4ef9f41a AA |
9055 | case NT_S390_VXRS_LOW: |
9056 | if (note->namesz == 6 | |
9057 | && strcmp (note->namedata, "LINUX") == 0) | |
9058 | return elfcore_grok_s390_vxrs_low (abfd, note); | |
9059 | else | |
9060 | return TRUE; | |
9061 | ||
9062 | case NT_S390_VXRS_HIGH: | |
9063 | if (note->namesz == 6 | |
9064 | && strcmp (note->namedata, "LINUX") == 0) | |
9065 | return elfcore_grok_s390_vxrs_high (abfd, note); | |
9066 | else | |
9067 | return TRUE; | |
9068 | ||
faa9a424 UW |
9069 | case NT_ARM_VFP: |
9070 | if (note->namesz == 6 | |
9071 | && strcmp (note->namedata, "LINUX") == 0) | |
9072 | return elfcore_grok_arm_vfp (abfd, note); | |
9073 | else | |
9074 | return TRUE; | |
9075 | ||
652451f8 YZ |
9076 | case NT_ARM_TLS: |
9077 | if (note->namesz == 6 | |
9078 | && strcmp (note->namedata, "LINUX") == 0) | |
9079 | return elfcore_grok_aarch_tls (abfd, note); | |
9080 | else | |
9081 | return TRUE; | |
9082 | ||
9083 | case NT_ARM_HW_BREAK: | |
9084 | if (note->namesz == 6 | |
9085 | && strcmp (note->namedata, "LINUX") == 0) | |
9086 | return elfcore_grok_aarch_hw_break (abfd, note); | |
9087 | else | |
9088 | return TRUE; | |
9089 | ||
9090 | case NT_ARM_HW_WATCH: | |
9091 | if (note->namesz == 6 | |
9092 | && strcmp (note->namedata, "LINUX") == 0) | |
9093 | return elfcore_grok_aarch_hw_watch (abfd, note); | |
9094 | else | |
9095 | return TRUE; | |
9096 | ||
252b5132 RH |
9097 | case NT_PRPSINFO: |
9098 | case NT_PSINFO: | |
bb0082d6 AM |
9099 | if (bed->elf_backend_grok_psinfo) |
9100 | if ((*bed->elf_backend_grok_psinfo) (abfd, note)) | |
b34976b6 | 9101 | return TRUE; |
bb0082d6 | 9102 | #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) |
252b5132 | 9103 | return elfcore_grok_psinfo (abfd, note); |
bb0082d6 | 9104 | #else |
b34976b6 | 9105 | return TRUE; |
252b5132 | 9106 | #endif |
3333a7c3 RM |
9107 | |
9108 | case NT_AUXV: | |
9109 | { | |
117ed4f8 AM |
9110 | asection *sect = bfd_make_section_anyway_with_flags (abfd, ".auxv", |
9111 | SEC_HAS_CONTENTS); | |
3333a7c3 RM |
9112 | |
9113 | if (sect == NULL) | |
9114 | return FALSE; | |
eea6121a | 9115 | sect->size = note->descsz; |
3333a7c3 | 9116 | sect->filepos = note->descpos; |
3333a7c3 RM |
9117 | sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32; |
9118 | ||
9119 | return TRUE; | |
9120 | } | |
9015683b | 9121 | |
451b7c33 TT |
9122 | case NT_FILE: |
9123 | return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.file", | |
9124 | note); | |
9125 | ||
9015683b TT |
9126 | case NT_SIGINFO: |
9127 | return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.siginfo", | |
9128 | note); | |
252b5132 RH |
9129 | } |
9130 | } | |
9131 | ||
718175fa JK |
9132 | static bfd_boolean |
9133 | elfobj_grok_gnu_build_id (bfd *abfd, Elf_Internal_Note *note) | |
9134 | { | |
c74f7d1c | 9135 | struct bfd_build_id* build_id; |
30e8ee25 AM |
9136 | |
9137 | if (note->descsz == 0) | |
9138 | return FALSE; | |
9139 | ||
c74f7d1c JT |
9140 | build_id = bfd_alloc (abfd, sizeof (struct bfd_build_id) - 1 + note->descsz); |
9141 | if (build_id == NULL) | |
718175fa JK |
9142 | return FALSE; |
9143 | ||
c74f7d1c JT |
9144 | build_id->size = note->descsz; |
9145 | memcpy (build_id->data, note->descdata, note->descsz); | |
9146 | abfd->build_id = build_id; | |
718175fa JK |
9147 | |
9148 | return TRUE; | |
9149 | } | |
9150 | ||
9151 | static bfd_boolean | |
9152 | elfobj_grok_gnu_note (bfd *abfd, Elf_Internal_Note *note) | |
9153 | { | |
9154 | switch (note->type) | |
9155 | { | |
9156 | default: | |
9157 | return TRUE; | |
9158 | ||
9159 | case NT_GNU_BUILD_ID: | |
9160 | return elfobj_grok_gnu_build_id (abfd, note); | |
9161 | } | |
9162 | } | |
9163 | ||
e21e5835 NC |
9164 | static bfd_boolean |
9165 | elfobj_grok_stapsdt_note_1 (bfd *abfd, Elf_Internal_Note *note) | |
9166 | { | |
9167 | struct sdt_note *cur = | |
9168 | (struct sdt_note *) bfd_alloc (abfd, sizeof (struct sdt_note) | |
9169 | + note->descsz); | |
9170 | ||
9171 | cur->next = (struct sdt_note *) (elf_tdata (abfd))->sdt_note_head; | |
9172 | cur->size = (bfd_size_type) note->descsz; | |
9173 | memcpy (cur->data, note->descdata, note->descsz); | |
9174 | ||
9175 | elf_tdata (abfd)->sdt_note_head = cur; | |
9176 | ||
9177 | return TRUE; | |
9178 | } | |
9179 | ||
9180 | static bfd_boolean | |
9181 | elfobj_grok_stapsdt_note (bfd *abfd, Elf_Internal_Note *note) | |
9182 | { | |
9183 | switch (note->type) | |
9184 | { | |
9185 | case NT_STAPSDT: | |
9186 | return elfobj_grok_stapsdt_note_1 (abfd, note); | |
9187 | ||
9188 | default: | |
9189 | return TRUE; | |
9190 | } | |
9191 | } | |
9192 | ||
b34976b6 | 9193 | static bfd_boolean |
217aa764 | 9194 | elfcore_netbsd_get_lwpid (Elf_Internal_Note *note, int *lwpidp) |
50b2bdb7 AM |
9195 | { |
9196 | char *cp; | |
9197 | ||
9198 | cp = strchr (note->namedata, '@'); | |
9199 | if (cp != NULL) | |
9200 | { | |
d2b64500 | 9201 | *lwpidp = atoi(cp + 1); |
b34976b6 | 9202 | return TRUE; |
50b2bdb7 | 9203 | } |
b34976b6 | 9204 | return FALSE; |
50b2bdb7 AM |
9205 | } |
9206 | ||
b34976b6 | 9207 | static bfd_boolean |
217aa764 | 9208 | elfcore_grok_netbsd_procinfo (bfd *abfd, Elf_Internal_Note *note) |
50b2bdb7 | 9209 | { |
50b2bdb7 | 9210 | /* Signal number at offset 0x08. */ |
228e534f | 9211 | elf_tdata (abfd)->core->signal |
50b2bdb7 AM |
9212 | = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08); |
9213 | ||
9214 | /* Process ID at offset 0x50. */ | |
228e534f | 9215 | elf_tdata (abfd)->core->pid |
50b2bdb7 AM |
9216 | = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x50); |
9217 | ||
9218 | /* Command name at 0x7c (max 32 bytes, including nul). */ | |
228e534f | 9219 | elf_tdata (abfd)->core->command |
50b2bdb7 AM |
9220 | = _bfd_elfcore_strndup (abfd, note->descdata + 0x7c, 31); |
9221 | ||
7720ba9f MK |
9222 | return elfcore_make_note_pseudosection (abfd, ".note.netbsdcore.procinfo", |
9223 | note); | |
50b2bdb7 AM |
9224 | } |
9225 | ||
b34976b6 | 9226 | static bfd_boolean |
217aa764 | 9227 | elfcore_grok_netbsd_note (bfd *abfd, Elf_Internal_Note *note) |
50b2bdb7 AM |
9228 | { |
9229 | int lwp; | |
9230 | ||
9231 | if (elfcore_netbsd_get_lwpid (note, &lwp)) | |
228e534f | 9232 | elf_tdata (abfd)->core->lwpid = lwp; |
50b2bdb7 | 9233 | |
b4db1224 | 9234 | if (note->type == NT_NETBSDCORE_PROCINFO) |
50b2bdb7 AM |
9235 | { |
9236 | /* NetBSD-specific core "procinfo". Note that we expect to | |
08a40648 AM |
9237 | find this note before any of the others, which is fine, |
9238 | since the kernel writes this note out first when it | |
9239 | creates a core file. */ | |
47d9a591 | 9240 | |
50b2bdb7 AM |
9241 | return elfcore_grok_netbsd_procinfo (abfd, note); |
9242 | } | |
9243 | ||
b4db1224 JT |
9244 | /* As of Jan 2002 there are no other machine-independent notes |
9245 | defined for NetBSD core files. If the note type is less | |
9246 | than the start of the machine-dependent note types, we don't | |
9247 | understand it. */ | |
47d9a591 | 9248 | |
b4db1224 | 9249 | if (note->type < NT_NETBSDCORE_FIRSTMACH) |
b34976b6 | 9250 | return TRUE; |
50b2bdb7 AM |
9251 | |
9252 | ||
9253 | switch (bfd_get_arch (abfd)) | |
9254 | { | |
08a40648 AM |
9255 | /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and |
9256 | PT_GETFPREGS == mach+2. */ | |
50b2bdb7 AM |
9257 | |
9258 | case bfd_arch_alpha: | |
9259 | case bfd_arch_sparc: | |
9260 | switch (note->type) | |
08a40648 AM |
9261 | { |
9262 | case NT_NETBSDCORE_FIRSTMACH+0: | |
9263 | return elfcore_make_note_pseudosection (abfd, ".reg", note); | |
50b2bdb7 | 9264 | |
08a40648 AM |
9265 | case NT_NETBSDCORE_FIRSTMACH+2: |
9266 | return elfcore_make_note_pseudosection (abfd, ".reg2", note); | |
50b2bdb7 | 9267 | |
08a40648 AM |
9268 | default: |
9269 | return TRUE; | |
9270 | } | |
50b2bdb7 | 9271 | |
08a40648 AM |
9272 | /* On all other arch's, PT_GETREGS == mach+1 and |
9273 | PT_GETFPREGS == mach+3. */ | |
50b2bdb7 AM |
9274 | |
9275 | default: | |
9276 | switch (note->type) | |
08a40648 AM |
9277 | { |
9278 | case NT_NETBSDCORE_FIRSTMACH+1: | |
9279 | return elfcore_make_note_pseudosection (abfd, ".reg", note); | |
50b2bdb7 | 9280 | |
08a40648 AM |
9281 | case NT_NETBSDCORE_FIRSTMACH+3: |
9282 | return elfcore_make_note_pseudosection (abfd, ".reg2", note); | |
50b2bdb7 | 9283 | |
08a40648 AM |
9284 | default: |
9285 | return TRUE; | |
9286 | } | |
50b2bdb7 AM |
9287 | } |
9288 | /* NOTREACHED */ | |
9289 | } | |
9290 | ||
67cc5033 MK |
9291 | static bfd_boolean |
9292 | elfcore_grok_openbsd_procinfo (bfd *abfd, Elf_Internal_Note *note) | |
9293 | { | |
9294 | /* Signal number at offset 0x08. */ | |
228e534f | 9295 | elf_tdata (abfd)->core->signal |
67cc5033 MK |
9296 | = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08); |
9297 | ||
9298 | /* Process ID at offset 0x20. */ | |
228e534f | 9299 | elf_tdata (abfd)->core->pid |
67cc5033 MK |
9300 | = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x20); |
9301 | ||
9302 | /* Command name at 0x48 (max 32 bytes, including nul). */ | |
228e534f | 9303 | elf_tdata (abfd)->core->command |
67cc5033 MK |
9304 | = _bfd_elfcore_strndup (abfd, note->descdata + 0x48, 31); |
9305 | ||
9306 | return TRUE; | |
9307 | } | |
9308 | ||
9309 | static bfd_boolean | |
9310 | elfcore_grok_openbsd_note (bfd *abfd, Elf_Internal_Note *note) | |
9311 | { | |
9312 | if (note->type == NT_OPENBSD_PROCINFO) | |
9313 | return elfcore_grok_openbsd_procinfo (abfd, note); | |
9314 | ||
9315 | if (note->type == NT_OPENBSD_REGS) | |
9316 | return elfcore_make_note_pseudosection (abfd, ".reg", note); | |
9317 | ||
9318 | if (note->type == NT_OPENBSD_FPREGS) | |
9319 | return elfcore_make_note_pseudosection (abfd, ".reg2", note); | |
9320 | ||
9321 | if (note->type == NT_OPENBSD_XFPREGS) | |
9322 | return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note); | |
9323 | ||
9324 | if (note->type == NT_OPENBSD_AUXV) | |
9325 | { | |
9326 | asection *sect = bfd_make_section_anyway_with_flags (abfd, ".auxv", | |
9327 | SEC_HAS_CONTENTS); | |
9328 | ||
9329 | if (sect == NULL) | |
9330 | return FALSE; | |
9331 | sect->size = note->descsz; | |
9332 | sect->filepos = note->descpos; | |
9333 | sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32; | |
9334 | ||
9335 | return TRUE; | |
9336 | } | |
9337 | ||
9338 | if (note->type == NT_OPENBSD_WCOOKIE) | |
9339 | { | |
9340 | asection *sect = bfd_make_section_anyway_with_flags (abfd, ".wcookie", | |
9341 | SEC_HAS_CONTENTS); | |
9342 | ||
9343 | if (sect == NULL) | |
9344 | return FALSE; | |
9345 | sect->size = note->descsz; | |
9346 | sect->filepos = note->descpos; | |
9347 | sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32; | |
9348 | ||
9349 | return TRUE; | |
9350 | } | |
9351 | ||
9352 | return TRUE; | |
9353 | } | |
9354 | ||
07c6e936 | 9355 | static bfd_boolean |
d3fd4074 | 9356 | elfcore_grok_nto_status (bfd *abfd, Elf_Internal_Note *note, long *tid) |
07c6e936 NC |
9357 | { |
9358 | void *ddata = note->descdata; | |
9359 | char buf[100]; | |
9360 | char *name; | |
9361 | asection *sect; | |
f8843e87 AM |
9362 | short sig; |
9363 | unsigned flags; | |
07c6e936 NC |
9364 | |
9365 | /* nto_procfs_status 'pid' field is at offset 0. */ | |
228e534f | 9366 | elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, (bfd_byte *) ddata); |
07c6e936 | 9367 | |
f8843e87 AM |
9368 | /* nto_procfs_status 'tid' field is at offset 4. Pass it back. */ |
9369 | *tid = bfd_get_32 (abfd, (bfd_byte *) ddata + 4); | |
9370 | ||
9371 | /* nto_procfs_status 'flags' field is at offset 8. */ | |
9372 | flags = bfd_get_32 (abfd, (bfd_byte *) ddata + 8); | |
07c6e936 NC |
9373 | |
9374 | /* nto_procfs_status 'what' field is at offset 14. */ | |
f8843e87 AM |
9375 | if ((sig = bfd_get_16 (abfd, (bfd_byte *) ddata + 14)) > 0) |
9376 | { | |
228e534f AM |
9377 | elf_tdata (abfd)->core->signal = sig; |
9378 | elf_tdata (abfd)->core->lwpid = *tid; | |
f8843e87 | 9379 | } |
07c6e936 | 9380 | |
f8843e87 AM |
9381 | /* _DEBUG_FLAG_CURTID (current thread) is 0x80. Some cores |
9382 | do not come from signals so we make sure we set the current | |
9383 | thread just in case. */ | |
9384 | if (flags & 0x00000080) | |
228e534f | 9385 | elf_tdata (abfd)->core->lwpid = *tid; |
07c6e936 NC |
9386 | |
9387 | /* Make a ".qnx_core_status/%d" section. */ | |
d3fd4074 | 9388 | sprintf (buf, ".qnx_core_status/%ld", *tid); |
07c6e936 | 9389 | |
a50b1753 | 9390 | name = (char *) bfd_alloc (abfd, strlen (buf) + 1); |
07c6e936 NC |
9391 | if (name == NULL) |
9392 | return FALSE; | |
9393 | strcpy (name, buf); | |
9394 | ||
117ed4f8 | 9395 | sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); |
07c6e936 NC |
9396 | if (sect == NULL) |
9397 | return FALSE; | |
9398 | ||
eea6121a | 9399 | sect->size = note->descsz; |
07c6e936 | 9400 | sect->filepos = note->descpos; |
07c6e936 NC |
9401 | sect->alignment_power = 2; |
9402 | ||
9403 | return (elfcore_maybe_make_sect (abfd, ".qnx_core_status", sect)); | |
9404 | } | |
9405 | ||
9406 | static bfd_boolean | |
d69f560c KW |
9407 | elfcore_grok_nto_regs (bfd *abfd, |
9408 | Elf_Internal_Note *note, | |
d3fd4074 | 9409 | long tid, |
d69f560c | 9410 | char *base) |
07c6e936 NC |
9411 | { |
9412 | char buf[100]; | |
9413 | char *name; | |
9414 | asection *sect; | |
9415 | ||
d69f560c | 9416 | /* Make a "(base)/%d" section. */ |
d3fd4074 | 9417 | sprintf (buf, "%s/%ld", base, tid); |
07c6e936 | 9418 | |
a50b1753 | 9419 | name = (char *) bfd_alloc (abfd, strlen (buf) + 1); |
07c6e936 NC |
9420 | if (name == NULL) |
9421 | return FALSE; | |
9422 | strcpy (name, buf); | |
9423 | ||
117ed4f8 | 9424 | sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); |
07c6e936 NC |
9425 | if (sect == NULL) |
9426 | return FALSE; | |
9427 | ||
eea6121a | 9428 | sect->size = note->descsz; |
07c6e936 | 9429 | sect->filepos = note->descpos; |
07c6e936 NC |
9430 | sect->alignment_power = 2; |
9431 | ||
f8843e87 | 9432 | /* This is the current thread. */ |
228e534f | 9433 | if (elf_tdata (abfd)->core->lwpid == tid) |
d69f560c | 9434 | return elfcore_maybe_make_sect (abfd, base, sect); |
f8843e87 AM |
9435 | |
9436 | return TRUE; | |
07c6e936 NC |
9437 | } |
9438 | ||
9439 | #define BFD_QNT_CORE_INFO 7 | |
9440 | #define BFD_QNT_CORE_STATUS 8 | |
9441 | #define BFD_QNT_CORE_GREG 9 | |
9442 | #define BFD_QNT_CORE_FPREG 10 | |
9443 | ||
9444 | static bfd_boolean | |
217aa764 | 9445 | elfcore_grok_nto_note (bfd *abfd, Elf_Internal_Note *note) |
07c6e936 NC |
9446 | { |
9447 | /* Every GREG section has a STATUS section before it. Store the | |
811072d8 | 9448 | tid from the previous call to pass down to the next gregs |
07c6e936 | 9449 | function. */ |
d3fd4074 | 9450 | static long tid = 1; |
07c6e936 NC |
9451 | |
9452 | switch (note->type) | |
9453 | { | |
d69f560c KW |
9454 | case BFD_QNT_CORE_INFO: |
9455 | return elfcore_make_note_pseudosection (abfd, ".qnx_core_info", note); | |
9456 | case BFD_QNT_CORE_STATUS: | |
9457 | return elfcore_grok_nto_status (abfd, note, &tid); | |
9458 | case BFD_QNT_CORE_GREG: | |
9459 | return elfcore_grok_nto_regs (abfd, note, tid, ".reg"); | |
9460 | case BFD_QNT_CORE_FPREG: | |
9461 | return elfcore_grok_nto_regs (abfd, note, tid, ".reg2"); | |
9462 | default: | |
9463 | return TRUE; | |
07c6e936 NC |
9464 | } |
9465 | } | |
9466 | ||
b15fa79e AM |
9467 | static bfd_boolean |
9468 | elfcore_grok_spu_note (bfd *abfd, Elf_Internal_Note *note) | |
9469 | { | |
9470 | char *name; | |
9471 | asection *sect; | |
9472 | size_t len; | |
9473 | ||
9474 | /* Use note name as section name. */ | |
9475 | len = note->namesz; | |
a50b1753 | 9476 | name = (char *) bfd_alloc (abfd, len); |
b15fa79e AM |
9477 | if (name == NULL) |
9478 | return FALSE; | |
9479 | memcpy (name, note->namedata, len); | |
9480 | name[len - 1] = '\0'; | |
9481 | ||
9482 | sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); | |
9483 | if (sect == NULL) | |
9484 | return FALSE; | |
9485 | ||
9486 | sect->size = note->descsz; | |
9487 | sect->filepos = note->descpos; | |
9488 | sect->alignment_power = 1; | |
9489 | ||
9490 | return TRUE; | |
9491 | } | |
9492 | ||
7c76fa91 MS |
9493 | /* Function: elfcore_write_note |
9494 | ||
47d9a591 | 9495 | Inputs: |
a39f3346 | 9496 | buffer to hold note, and current size of buffer |
7c76fa91 MS |
9497 | name of note |
9498 | type of note | |
9499 | data for note | |
9500 | size of data for note | |
9501 | ||
a39f3346 AM |
9502 | Writes note to end of buffer. ELF64 notes are written exactly as |
9503 | for ELF32, despite the current (as of 2006) ELF gabi specifying | |
9504 | that they ought to have 8-byte namesz and descsz field, and have | |
9505 | 8-byte alignment. Other writers, eg. Linux kernel, do the same. | |
9506 | ||
7c76fa91 | 9507 | Return: |
a39f3346 | 9508 | Pointer to realloc'd buffer, *BUFSIZ updated. */ |
7c76fa91 MS |
9509 | |
9510 | char * | |
a39f3346 | 9511 | elfcore_write_note (bfd *abfd, |
217aa764 | 9512 | char *buf, |
a39f3346 | 9513 | int *bufsiz, |
217aa764 | 9514 | const char *name, |
a39f3346 | 9515 | int type, |
217aa764 | 9516 | const void *input, |
a39f3346 | 9517 | int size) |
7c76fa91 MS |
9518 | { |
9519 | Elf_External_Note *xnp; | |
d4c88bbb | 9520 | size_t namesz; |
d4c88bbb | 9521 | size_t newspace; |
a39f3346 | 9522 | char *dest; |
7c76fa91 | 9523 | |
d4c88bbb | 9524 | namesz = 0; |
d4c88bbb | 9525 | if (name != NULL) |
a39f3346 | 9526 | namesz = strlen (name) + 1; |
d4c88bbb | 9527 | |
a39f3346 | 9528 | newspace = 12 + ((namesz + 3) & -4) + ((size + 3) & -4); |
d4c88bbb | 9529 | |
a50b1753 | 9530 | buf = (char *) realloc (buf, *bufsiz + newspace); |
14b1c01e AM |
9531 | if (buf == NULL) |
9532 | return buf; | |
a39f3346 | 9533 | dest = buf + *bufsiz; |
7c76fa91 MS |
9534 | *bufsiz += newspace; |
9535 | xnp = (Elf_External_Note *) dest; | |
9536 | H_PUT_32 (abfd, namesz, xnp->namesz); | |
9537 | H_PUT_32 (abfd, size, xnp->descsz); | |
9538 | H_PUT_32 (abfd, type, xnp->type); | |
d4c88bbb AM |
9539 | dest = xnp->name; |
9540 | if (name != NULL) | |
9541 | { | |
9542 | memcpy (dest, name, namesz); | |
9543 | dest += namesz; | |
a39f3346 | 9544 | while (namesz & 3) |
d4c88bbb AM |
9545 | { |
9546 | *dest++ = '\0'; | |
a39f3346 | 9547 | ++namesz; |
d4c88bbb AM |
9548 | } |
9549 | } | |
9550 | memcpy (dest, input, size); | |
a39f3346 AM |
9551 | dest += size; |
9552 | while (size & 3) | |
9553 | { | |
9554 | *dest++ = '\0'; | |
9555 | ++size; | |
9556 | } | |
9557 | return buf; | |
7c76fa91 MS |
9558 | } |
9559 | ||
7c76fa91 | 9560 | char * |
217aa764 AM |
9561 | elfcore_write_prpsinfo (bfd *abfd, |
9562 | char *buf, | |
9563 | int *bufsiz, | |
9564 | const char *fname, | |
9565 | const char *psargs) | |
7c76fa91 | 9566 | { |
183e98be AM |
9567 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
9568 | ||
9569 | if (bed->elf_backend_write_core_note != NULL) | |
9570 | { | |
9571 | char *ret; | |
9572 | ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz, | |
9573 | NT_PRPSINFO, fname, psargs); | |
9574 | if (ret != NULL) | |
9575 | return ret; | |
9576 | } | |
7c76fa91 | 9577 | |
1f20dca5 | 9578 | #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) |
183e98be AM |
9579 | #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T) |
9580 | if (bed->s->elfclass == ELFCLASS32) | |
9581 | { | |
9582 | #if defined (HAVE_PSINFO32_T) | |
9583 | psinfo32_t data; | |
9584 | int note_type = NT_PSINFO; | |
9585 | #else | |
9586 | prpsinfo32_t data; | |
9587 | int note_type = NT_PRPSINFO; | |
9588 | #endif | |
9589 | ||
9590 | memset (&data, 0, sizeof (data)); | |
9591 | strncpy (data.pr_fname, fname, sizeof (data.pr_fname)); | |
9592 | strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs)); | |
9593 | return elfcore_write_note (abfd, buf, bufsiz, | |
1f20dca5 | 9594 | "CORE", note_type, &data, sizeof (data)); |
183e98be AM |
9595 | } |
9596 | else | |
9597 | #endif | |
9598 | { | |
7c76fa91 | 9599 | #if defined (HAVE_PSINFO_T) |
183e98be AM |
9600 | psinfo_t data; |
9601 | int note_type = NT_PSINFO; | |
7c76fa91 | 9602 | #else |
183e98be AM |
9603 | prpsinfo_t data; |
9604 | int note_type = NT_PRPSINFO; | |
7c76fa91 MS |
9605 | #endif |
9606 | ||
183e98be AM |
9607 | memset (&data, 0, sizeof (data)); |
9608 | strncpy (data.pr_fname, fname, sizeof (data.pr_fname)); | |
9609 | strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs)); | |
9610 | return elfcore_write_note (abfd, buf, bufsiz, | |
1f20dca5 | 9611 | "CORE", note_type, &data, sizeof (data)); |
183e98be | 9612 | } |
7c76fa91 MS |
9613 | #endif /* PSINFO_T or PRPSINFO_T */ |
9614 | ||
1f20dca5 UW |
9615 | free (buf); |
9616 | return NULL; | |
9617 | } | |
9618 | ||
70a38d42 SDJ |
9619 | char * |
9620 | elfcore_write_linux_prpsinfo32 | |
9621 | (bfd *abfd, char *buf, int *bufsiz, | |
9622 | const struct elf_internal_linux_prpsinfo *prpsinfo) | |
9623 | { | |
9624 | struct elf_external_linux_prpsinfo32 data; | |
9625 | ||
9626 | memset (&data, 0, sizeof (data)); | |
9627 | LINUX_PRPSINFO32_SWAP_FIELDS (abfd, prpsinfo, data); | |
9628 | ||
9629 | return elfcore_write_note (abfd, buf, bufsiz, "CORE", NT_PRPSINFO, | |
9630 | &data, sizeof (data)); | |
9631 | } | |
9632 | ||
9633 | char * | |
9634 | elfcore_write_linux_prpsinfo64 | |
9635 | (bfd *abfd, char *buf, int *bufsiz, | |
9636 | const struct elf_internal_linux_prpsinfo *prpsinfo) | |
9637 | { | |
9638 | struct elf_external_linux_prpsinfo64 data; | |
9639 | ||
9640 | memset (&data, 0, sizeof (data)); | |
9641 | LINUX_PRPSINFO64_SWAP_FIELDS (abfd, prpsinfo, data); | |
9642 | ||
9643 | return elfcore_write_note (abfd, buf, bufsiz, | |
9644 | "CORE", NT_PRPSINFO, &data, sizeof (data)); | |
9645 | } | |
9646 | ||
7c76fa91 | 9647 | char * |
217aa764 AM |
9648 | elfcore_write_prstatus (bfd *abfd, |
9649 | char *buf, | |
9650 | int *bufsiz, | |
9651 | long pid, | |
9652 | int cursig, | |
9653 | const void *gregs) | |
7c76fa91 | 9654 | { |
183e98be | 9655 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
7c76fa91 | 9656 | |
183e98be AM |
9657 | if (bed->elf_backend_write_core_note != NULL) |
9658 | { | |
9659 | char *ret; | |
9660 | ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz, | |
9661 | NT_PRSTATUS, | |
9662 | pid, cursig, gregs); | |
9663 | if (ret != NULL) | |
9664 | return ret; | |
9665 | } | |
9666 | ||
1f20dca5 | 9667 | #if defined (HAVE_PRSTATUS_T) |
183e98be AM |
9668 | #if defined (HAVE_PRSTATUS32_T) |
9669 | if (bed->s->elfclass == ELFCLASS32) | |
9670 | { | |
9671 | prstatus32_t prstat; | |
9672 | ||
9673 | memset (&prstat, 0, sizeof (prstat)); | |
9674 | prstat.pr_pid = pid; | |
9675 | prstat.pr_cursig = cursig; | |
9676 | memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg)); | |
1f20dca5 | 9677 | return elfcore_write_note (abfd, buf, bufsiz, "CORE", |
183e98be AM |
9678 | NT_PRSTATUS, &prstat, sizeof (prstat)); |
9679 | } | |
9680 | else | |
9681 | #endif | |
9682 | { | |
9683 | prstatus_t prstat; | |
9684 | ||
9685 | memset (&prstat, 0, sizeof (prstat)); | |
9686 | prstat.pr_pid = pid; | |
9687 | prstat.pr_cursig = cursig; | |
9688 | memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg)); | |
1f20dca5 | 9689 | return elfcore_write_note (abfd, buf, bufsiz, "CORE", |
183e98be AM |
9690 | NT_PRSTATUS, &prstat, sizeof (prstat)); |
9691 | } | |
7c76fa91 MS |
9692 | #endif /* HAVE_PRSTATUS_T */ |
9693 | ||
1f20dca5 UW |
9694 | free (buf); |
9695 | return NULL; | |
9696 | } | |
9697 | ||
51316059 MS |
9698 | #if defined (HAVE_LWPSTATUS_T) |
9699 | char * | |
217aa764 AM |
9700 | elfcore_write_lwpstatus (bfd *abfd, |
9701 | char *buf, | |
9702 | int *bufsiz, | |
9703 | long pid, | |
9704 | int cursig, | |
9705 | const void *gregs) | |
51316059 MS |
9706 | { |
9707 | lwpstatus_t lwpstat; | |
183e98be | 9708 | const char *note_name = "CORE"; |
51316059 MS |
9709 | |
9710 | memset (&lwpstat, 0, sizeof (lwpstat)); | |
9711 | lwpstat.pr_lwpid = pid >> 16; | |
9712 | lwpstat.pr_cursig = cursig; | |
9713 | #if defined (HAVE_LWPSTATUS_T_PR_REG) | |
d1e8523e | 9714 | memcpy (&lwpstat.pr_reg, gregs, sizeof (lwpstat.pr_reg)); |
51316059 MS |
9715 | #elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT) |
9716 | #if !defined(gregs) | |
9717 | memcpy (lwpstat.pr_context.uc_mcontext.gregs, | |
9718 | gregs, sizeof (lwpstat.pr_context.uc_mcontext.gregs)); | |
9719 | #else | |
9720 | memcpy (lwpstat.pr_context.uc_mcontext.__gregs, | |
9721 | gregs, sizeof (lwpstat.pr_context.uc_mcontext.__gregs)); | |
9722 | #endif | |
9723 | #endif | |
47d9a591 | 9724 | return elfcore_write_note (abfd, buf, bufsiz, note_name, |
51316059 MS |
9725 | NT_LWPSTATUS, &lwpstat, sizeof (lwpstat)); |
9726 | } | |
9727 | #endif /* HAVE_LWPSTATUS_T */ | |
9728 | ||
7c76fa91 MS |
9729 | #if defined (HAVE_PSTATUS_T) |
9730 | char * | |
217aa764 AM |
9731 | elfcore_write_pstatus (bfd *abfd, |
9732 | char *buf, | |
9733 | int *bufsiz, | |
9734 | long pid, | |
6c10990d NC |
9735 | int cursig ATTRIBUTE_UNUSED, |
9736 | const void *gregs ATTRIBUTE_UNUSED) | |
7c76fa91 | 9737 | { |
183e98be AM |
9738 | const char *note_name = "CORE"; |
9739 | #if defined (HAVE_PSTATUS32_T) | |
9740 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
7c76fa91 | 9741 | |
183e98be AM |
9742 | if (bed->s->elfclass == ELFCLASS32) |
9743 | { | |
9744 | pstatus32_t pstat; | |
9745 | ||
9746 | memset (&pstat, 0, sizeof (pstat)); | |
9747 | pstat.pr_pid = pid & 0xffff; | |
9748 | buf = elfcore_write_note (abfd, buf, bufsiz, note_name, | |
9749 | NT_PSTATUS, &pstat, sizeof (pstat)); | |
9750 | return buf; | |
9751 | } | |
9752 | else | |
9753 | #endif | |
9754 | { | |
9755 | pstatus_t pstat; | |
9756 | ||
9757 | memset (&pstat, 0, sizeof (pstat)); | |
9758 | pstat.pr_pid = pid & 0xffff; | |
9759 | buf = elfcore_write_note (abfd, buf, bufsiz, note_name, | |
9760 | NT_PSTATUS, &pstat, sizeof (pstat)); | |
9761 | return buf; | |
9762 | } | |
7c76fa91 MS |
9763 | } |
9764 | #endif /* HAVE_PSTATUS_T */ | |
9765 | ||
9766 | char * | |
217aa764 AM |
9767 | elfcore_write_prfpreg (bfd *abfd, |
9768 | char *buf, | |
9769 | int *bufsiz, | |
9770 | const void *fpregs, | |
9771 | int size) | |
7c76fa91 | 9772 | { |
183e98be | 9773 | const char *note_name = "CORE"; |
47d9a591 | 9774 | return elfcore_write_note (abfd, buf, bufsiz, |
7c76fa91 MS |
9775 | note_name, NT_FPREGSET, fpregs, size); |
9776 | } | |
9777 | ||
9778 | char * | |
217aa764 AM |
9779 | elfcore_write_prxfpreg (bfd *abfd, |
9780 | char *buf, | |
9781 | int *bufsiz, | |
9782 | const void *xfpregs, | |
9783 | int size) | |
7c76fa91 MS |
9784 | { |
9785 | char *note_name = "LINUX"; | |
47d9a591 | 9786 | return elfcore_write_note (abfd, buf, bufsiz, |
7c76fa91 MS |
9787 | note_name, NT_PRXFPREG, xfpregs, size); |
9788 | } | |
9789 | ||
4339cae0 L |
9790 | char * |
9791 | elfcore_write_xstatereg (bfd *abfd, char *buf, int *bufsiz, | |
9792 | const void *xfpregs, int size) | |
9793 | { | |
97de3545 JB |
9794 | char *note_name; |
9795 | if (get_elf_backend_data (abfd)->elf_osabi == ELFOSABI_FREEBSD) | |
9796 | note_name = "FreeBSD"; | |
9797 | else | |
9798 | note_name = "LINUX"; | |
4339cae0 L |
9799 | return elfcore_write_note (abfd, buf, bufsiz, |
9800 | note_name, NT_X86_XSTATE, xfpregs, size); | |
9801 | } | |
9802 | ||
97753bd5 AM |
9803 | char * |
9804 | elfcore_write_ppc_vmx (bfd *abfd, | |
9805 | char *buf, | |
9806 | int *bufsiz, | |
9807 | const void *ppc_vmx, | |
9808 | int size) | |
9809 | { | |
9810 | char *note_name = "LINUX"; | |
9811 | return elfcore_write_note (abfd, buf, bufsiz, | |
9812 | note_name, NT_PPC_VMX, ppc_vmx, size); | |
9813 | } | |
9814 | ||
89eeb0bc LM |
9815 | char * |
9816 | elfcore_write_ppc_vsx (bfd *abfd, | |
9817 | char *buf, | |
9818 | int *bufsiz, | |
9819 | const void *ppc_vsx, | |
9820 | int size) | |
9821 | { | |
9822 | char *note_name = "LINUX"; | |
9823 | return elfcore_write_note (abfd, buf, bufsiz, | |
9824 | note_name, NT_PPC_VSX, ppc_vsx, size); | |
9825 | } | |
9826 | ||
0675e188 UW |
9827 | static char * |
9828 | elfcore_write_s390_high_gprs (bfd *abfd, | |
9829 | char *buf, | |
9830 | int *bufsiz, | |
9831 | const void *s390_high_gprs, | |
9832 | int size) | |
9833 | { | |
9834 | char *note_name = "LINUX"; | |
9835 | return elfcore_write_note (abfd, buf, bufsiz, | |
9836 | note_name, NT_S390_HIGH_GPRS, | |
9837 | s390_high_gprs, size); | |
9838 | } | |
9839 | ||
d7eeb400 MS |
9840 | char * |
9841 | elfcore_write_s390_timer (bfd *abfd, | |
9842 | char *buf, | |
9843 | int *bufsiz, | |
9844 | const void *s390_timer, | |
9845 | int size) | |
9846 | { | |
9847 | char *note_name = "LINUX"; | |
9848 | return elfcore_write_note (abfd, buf, bufsiz, | |
9849 | note_name, NT_S390_TIMER, s390_timer, size); | |
9850 | } | |
9851 | ||
9852 | char * | |
9853 | elfcore_write_s390_todcmp (bfd *abfd, | |
9854 | char *buf, | |
9855 | int *bufsiz, | |
9856 | const void *s390_todcmp, | |
9857 | int size) | |
9858 | { | |
9859 | char *note_name = "LINUX"; | |
9860 | return elfcore_write_note (abfd, buf, bufsiz, | |
9861 | note_name, NT_S390_TODCMP, s390_todcmp, size); | |
9862 | } | |
9863 | ||
9864 | char * | |
9865 | elfcore_write_s390_todpreg (bfd *abfd, | |
9866 | char *buf, | |
9867 | int *bufsiz, | |
9868 | const void *s390_todpreg, | |
9869 | int size) | |
9870 | { | |
9871 | char *note_name = "LINUX"; | |
9872 | return elfcore_write_note (abfd, buf, bufsiz, | |
9873 | note_name, NT_S390_TODPREG, s390_todpreg, size); | |
9874 | } | |
9875 | ||
9876 | char * | |
9877 | elfcore_write_s390_ctrs (bfd *abfd, | |
9878 | char *buf, | |
9879 | int *bufsiz, | |
9880 | const void *s390_ctrs, | |
9881 | int size) | |
9882 | { | |
9883 | char *note_name = "LINUX"; | |
9884 | return elfcore_write_note (abfd, buf, bufsiz, | |
9885 | note_name, NT_S390_CTRS, s390_ctrs, size); | |
9886 | } | |
9887 | ||
9888 | char * | |
9889 | elfcore_write_s390_prefix (bfd *abfd, | |
9890 | char *buf, | |
9891 | int *bufsiz, | |
9892 | const void *s390_prefix, | |
9893 | int size) | |
9894 | { | |
9895 | char *note_name = "LINUX"; | |
9896 | return elfcore_write_note (abfd, buf, bufsiz, | |
9897 | note_name, NT_S390_PREFIX, s390_prefix, size); | |
9898 | } | |
9899 | ||
355b81d9 UW |
9900 | char * |
9901 | elfcore_write_s390_last_break (bfd *abfd, | |
9902 | char *buf, | |
9903 | int *bufsiz, | |
9904 | const void *s390_last_break, | |
9905 | int size) | |
9906 | { | |
9907 | char *note_name = "LINUX"; | |
9908 | return elfcore_write_note (abfd, buf, bufsiz, | |
9909 | note_name, NT_S390_LAST_BREAK, | |
9910 | s390_last_break, size); | |
9911 | } | |
9912 | ||
9913 | char * | |
9914 | elfcore_write_s390_system_call (bfd *abfd, | |
9915 | char *buf, | |
9916 | int *bufsiz, | |
9917 | const void *s390_system_call, | |
9918 | int size) | |
9919 | { | |
9920 | char *note_name = "LINUX"; | |
9921 | return elfcore_write_note (abfd, buf, bufsiz, | |
9922 | note_name, NT_S390_SYSTEM_CALL, | |
9923 | s390_system_call, size); | |
9924 | } | |
9925 | ||
abb3f6cc NC |
9926 | char * |
9927 | elfcore_write_s390_tdb (bfd *abfd, | |
9928 | char *buf, | |
9929 | int *bufsiz, | |
9930 | const void *s390_tdb, | |
9931 | int size) | |
9932 | { | |
9933 | char *note_name = "LINUX"; | |
9934 | return elfcore_write_note (abfd, buf, bufsiz, | |
9935 | note_name, NT_S390_TDB, s390_tdb, size); | |
9936 | } | |
9937 | ||
4ef9f41a AA |
9938 | char * |
9939 | elfcore_write_s390_vxrs_low (bfd *abfd, | |
9940 | char *buf, | |
9941 | int *bufsiz, | |
9942 | const void *s390_vxrs_low, | |
9943 | int size) | |
9944 | { | |
9945 | char *note_name = "LINUX"; | |
9946 | return elfcore_write_note (abfd, buf, bufsiz, | |
9947 | note_name, NT_S390_VXRS_LOW, s390_vxrs_low, size); | |
9948 | } | |
9949 | ||
9950 | char * | |
9951 | elfcore_write_s390_vxrs_high (bfd *abfd, | |
9952 | char *buf, | |
9953 | int *bufsiz, | |
9954 | const void *s390_vxrs_high, | |
9955 | int size) | |
9956 | { | |
9957 | char *note_name = "LINUX"; | |
9958 | return elfcore_write_note (abfd, buf, bufsiz, | |
9959 | note_name, NT_S390_VXRS_HIGH, | |
9960 | s390_vxrs_high, size); | |
9961 | } | |
9962 | ||
faa9a424 UW |
9963 | char * |
9964 | elfcore_write_arm_vfp (bfd *abfd, | |
9965 | char *buf, | |
9966 | int *bufsiz, | |
9967 | const void *arm_vfp, | |
9968 | int size) | |
9969 | { | |
9970 | char *note_name = "LINUX"; | |
9971 | return elfcore_write_note (abfd, buf, bufsiz, | |
9972 | note_name, NT_ARM_VFP, arm_vfp, size); | |
9973 | } | |
9974 | ||
652451f8 YZ |
9975 | char * |
9976 | elfcore_write_aarch_tls (bfd *abfd, | |
9977 | char *buf, | |
9978 | int *bufsiz, | |
9979 | const void *aarch_tls, | |
9980 | int size) | |
9981 | { | |
9982 | char *note_name = "LINUX"; | |
9983 | return elfcore_write_note (abfd, buf, bufsiz, | |
9984 | note_name, NT_ARM_TLS, aarch_tls, size); | |
9985 | } | |
9986 | ||
9987 | char * | |
9988 | elfcore_write_aarch_hw_break (bfd *abfd, | |
9989 | char *buf, | |
9990 | int *bufsiz, | |
9991 | const void *aarch_hw_break, | |
9992 | int size) | |
9993 | { | |
9994 | char *note_name = "LINUX"; | |
9995 | return elfcore_write_note (abfd, buf, bufsiz, | |
9996 | note_name, NT_ARM_HW_BREAK, aarch_hw_break, size); | |
9997 | } | |
9998 | ||
9999 | char * | |
10000 | elfcore_write_aarch_hw_watch (bfd *abfd, | |
10001 | char *buf, | |
10002 | int *bufsiz, | |
10003 | const void *aarch_hw_watch, | |
10004 | int size) | |
10005 | { | |
10006 | char *note_name = "LINUX"; | |
10007 | return elfcore_write_note (abfd, buf, bufsiz, | |
10008 | note_name, NT_ARM_HW_WATCH, aarch_hw_watch, size); | |
10009 | } | |
10010 | ||
bb864ac1 CES |
10011 | char * |
10012 | elfcore_write_register_note (bfd *abfd, | |
10013 | char *buf, | |
10014 | int *bufsiz, | |
10015 | const char *section, | |
10016 | const void *data, | |
10017 | int size) | |
10018 | { | |
10019 | if (strcmp (section, ".reg2") == 0) | |
10020 | return elfcore_write_prfpreg (abfd, buf, bufsiz, data, size); | |
10021 | if (strcmp (section, ".reg-xfp") == 0) | |
10022 | return elfcore_write_prxfpreg (abfd, buf, bufsiz, data, size); | |
4339cae0 L |
10023 | if (strcmp (section, ".reg-xstate") == 0) |
10024 | return elfcore_write_xstatereg (abfd, buf, bufsiz, data, size); | |
bb864ac1 CES |
10025 | if (strcmp (section, ".reg-ppc-vmx") == 0) |
10026 | return elfcore_write_ppc_vmx (abfd, buf, bufsiz, data, size); | |
89eeb0bc LM |
10027 | if (strcmp (section, ".reg-ppc-vsx") == 0) |
10028 | return elfcore_write_ppc_vsx (abfd, buf, bufsiz, data, size); | |
0675e188 UW |
10029 | if (strcmp (section, ".reg-s390-high-gprs") == 0) |
10030 | return elfcore_write_s390_high_gprs (abfd, buf, bufsiz, data, size); | |
d7eeb400 MS |
10031 | if (strcmp (section, ".reg-s390-timer") == 0) |
10032 | return elfcore_write_s390_timer (abfd, buf, bufsiz, data, size); | |
10033 | if (strcmp (section, ".reg-s390-todcmp") == 0) | |
10034 | return elfcore_write_s390_todcmp (abfd, buf, bufsiz, data, size); | |
10035 | if (strcmp (section, ".reg-s390-todpreg") == 0) | |
10036 | return elfcore_write_s390_todpreg (abfd, buf, bufsiz, data, size); | |
10037 | if (strcmp (section, ".reg-s390-ctrs") == 0) | |
10038 | return elfcore_write_s390_ctrs (abfd, buf, bufsiz, data, size); | |
10039 | if (strcmp (section, ".reg-s390-prefix") == 0) | |
10040 | return elfcore_write_s390_prefix (abfd, buf, bufsiz, data, size); | |
355b81d9 UW |
10041 | if (strcmp (section, ".reg-s390-last-break") == 0) |
10042 | return elfcore_write_s390_last_break (abfd, buf, bufsiz, data, size); | |
10043 | if (strcmp (section, ".reg-s390-system-call") == 0) | |
10044 | return elfcore_write_s390_system_call (abfd, buf, bufsiz, data, size); | |
abb3f6cc NC |
10045 | if (strcmp (section, ".reg-s390-tdb") == 0) |
10046 | return elfcore_write_s390_tdb (abfd, buf, bufsiz, data, size); | |
4ef9f41a AA |
10047 | if (strcmp (section, ".reg-s390-vxrs-low") == 0) |
10048 | return elfcore_write_s390_vxrs_low (abfd, buf, bufsiz, data, size); | |
10049 | if (strcmp (section, ".reg-s390-vxrs-high") == 0) | |
10050 | return elfcore_write_s390_vxrs_high (abfd, buf, bufsiz, data, size); | |
faa9a424 UW |
10051 | if (strcmp (section, ".reg-arm-vfp") == 0) |
10052 | return elfcore_write_arm_vfp (abfd, buf, bufsiz, data, size); | |
652451f8 YZ |
10053 | if (strcmp (section, ".reg-aarch-tls") == 0) |
10054 | return elfcore_write_aarch_tls (abfd, buf, bufsiz, data, size); | |
10055 | if (strcmp (section, ".reg-aarch-hw-break") == 0) | |
10056 | return elfcore_write_aarch_hw_break (abfd, buf, bufsiz, data, size); | |
10057 | if (strcmp (section, ".reg-aarch-hw-watch") == 0) | |
10058 | return elfcore_write_aarch_hw_watch (abfd, buf, bufsiz, data, size); | |
bb864ac1 CES |
10059 | return NULL; |
10060 | } | |
10061 | ||
b34976b6 | 10062 | static bfd_boolean |
718175fa | 10063 | elf_parse_notes (bfd *abfd, char *buf, size_t size, file_ptr offset) |
252b5132 | 10064 | { |
c044fabd | 10065 | char *p; |
252b5132 | 10066 | |
252b5132 RH |
10067 | p = buf; |
10068 | while (p < buf + size) | |
10069 | { | |
c044fabd KH |
10070 | /* FIXME: bad alignment assumption. */ |
10071 | Elf_External_Note *xnp = (Elf_External_Note *) p; | |
252b5132 RH |
10072 | Elf_Internal_Note in; |
10073 | ||
baea7ef1 AM |
10074 | if (offsetof (Elf_External_Note, name) > buf - p + size) |
10075 | return FALSE; | |
10076 | ||
dc810e39 | 10077 | in.type = H_GET_32 (abfd, xnp->type); |
252b5132 | 10078 | |
dc810e39 | 10079 | in.namesz = H_GET_32 (abfd, xnp->namesz); |
252b5132 | 10080 | in.namedata = xnp->name; |
baea7ef1 AM |
10081 | if (in.namesz > buf - in.namedata + size) |
10082 | return FALSE; | |
252b5132 | 10083 | |
dc810e39 | 10084 | in.descsz = H_GET_32 (abfd, xnp->descsz); |
252b5132 RH |
10085 | in.descdata = in.namedata + BFD_ALIGN (in.namesz, 4); |
10086 | in.descpos = offset + (in.descdata - buf); | |
baea7ef1 AM |
10087 | if (in.descsz != 0 |
10088 | && (in.descdata >= buf + size | |
10089 | || in.descsz > buf - in.descdata + size)) | |
10090 | return FALSE; | |
252b5132 | 10091 | |
718175fa JK |
10092 | switch (bfd_get_format (abfd)) |
10093 | { | |
10094 | default: | |
10095 | return TRUE; | |
10096 | ||
10097 | case bfd_core: | |
f64e188b | 10098 | { |
8acbedd6 | 10099 | #define GROKER_ELEMENT(S,F) {S, sizeof (S) - 1, F} |
f64e188b | 10100 | struct |
718175fa | 10101 | { |
f64e188b | 10102 | const char * string; |
8acbedd6 | 10103 | size_t len; |
f64e188b | 10104 | bfd_boolean (* func)(bfd *, Elf_Internal_Note *); |
718175fa | 10105 | } |
f64e188b | 10106 | grokers[] = |
b15fa79e | 10107 | { |
8acbedd6 KS |
10108 | GROKER_ELEMENT ("", elfcore_grok_note), |
10109 | GROKER_ELEMENT ("NetBSD-CORE", elfcore_grok_netbsd_note), | |
10110 | GROKER_ELEMENT ( "OpenBSD", elfcore_grok_openbsd_note), | |
10111 | GROKER_ELEMENT ("QNX", elfcore_grok_nto_note), | |
10112 | GROKER_ELEMENT ("SPU/", elfcore_grok_spu_note) | |
f64e188b | 10113 | }; |
8acbedd6 | 10114 | #undef GROKER_ELEMENT |
f64e188b NC |
10115 | int i; |
10116 | ||
10117 | for (i = ARRAY_SIZE (grokers); i--;) | |
8acbedd6 KS |
10118 | { |
10119 | if (in.namesz >= grokers[i].len | |
10120 | && strncmp (in.namedata, grokers[i].string, | |
10121 | grokers[i].len) == 0) | |
10122 | { | |
10123 | if (! grokers[i].func (abfd, & in)) | |
10124 | return FALSE; | |
10125 | break; | |
10126 | } | |
10127 | } | |
f64e188b NC |
10128 | break; |
10129 | } | |
718175fa JK |
10130 | |
10131 | case bfd_object: | |
10132 | if (in.namesz == sizeof "GNU" && strcmp (in.namedata, "GNU") == 0) | |
10133 | { | |
10134 | if (! elfobj_grok_gnu_note (abfd, &in)) | |
10135 | return FALSE; | |
10136 | } | |
e21e5835 NC |
10137 | else if (in.namesz == sizeof "stapsdt" |
10138 | && strcmp (in.namedata, "stapsdt") == 0) | |
10139 | { | |
10140 | if (! elfobj_grok_stapsdt_note (abfd, &in)) | |
10141 | return FALSE; | |
10142 | } | |
718175fa | 10143 | break; |
08a40648 | 10144 | } |
252b5132 RH |
10145 | |
10146 | p = in.descdata + BFD_ALIGN (in.descsz, 4); | |
10147 | } | |
10148 | ||
718175fa JK |
10149 | return TRUE; |
10150 | } | |
10151 | ||
10152 | static bfd_boolean | |
10153 | elf_read_notes (bfd *abfd, file_ptr offset, bfd_size_type size) | |
10154 | { | |
10155 | char *buf; | |
10156 | ||
10157 | if (size <= 0) | |
10158 | return TRUE; | |
10159 | ||
10160 | if (bfd_seek (abfd, offset, SEEK_SET) != 0) | |
10161 | return FALSE; | |
10162 | ||
f64e188b | 10163 | buf = (char *) bfd_malloc (size + 1); |
718175fa JK |
10164 | if (buf == NULL) |
10165 | return FALSE; | |
10166 | ||
f64e188b NC |
10167 | /* PR 17512: file: ec08f814 |
10168 | 0-termintate the buffer so that string searches will not overflow. */ | |
10169 | buf[size] = 0; | |
10170 | ||
718175fa JK |
10171 | if (bfd_bread (buf, size, abfd) != size |
10172 | || !elf_parse_notes (abfd, buf, size, offset)) | |
10173 | { | |
10174 | free (buf); | |
10175 | return FALSE; | |
10176 | } | |
10177 | ||
252b5132 | 10178 | free (buf); |
b34976b6 | 10179 | return TRUE; |
252b5132 | 10180 | } |
98d8431c JB |
10181 | \f |
10182 | /* Providing external access to the ELF program header table. */ | |
10183 | ||
10184 | /* Return an upper bound on the number of bytes required to store a | |
10185 | copy of ABFD's program header table entries. Return -1 if an error | |
10186 | occurs; bfd_get_error will return an appropriate code. */ | |
c044fabd | 10187 | |
98d8431c | 10188 | long |
217aa764 | 10189 | bfd_get_elf_phdr_upper_bound (bfd *abfd) |
98d8431c JB |
10190 | { |
10191 | if (abfd->xvec->flavour != bfd_target_elf_flavour) | |
10192 | { | |
10193 | bfd_set_error (bfd_error_wrong_format); | |
10194 | return -1; | |
10195 | } | |
10196 | ||
936e320b | 10197 | return elf_elfheader (abfd)->e_phnum * sizeof (Elf_Internal_Phdr); |
98d8431c JB |
10198 | } |
10199 | ||
98d8431c JB |
10200 | /* Copy ABFD's program header table entries to *PHDRS. The entries |
10201 | will be stored as an array of Elf_Internal_Phdr structures, as | |
10202 | defined in include/elf/internal.h. To find out how large the | |
10203 | buffer needs to be, call bfd_get_elf_phdr_upper_bound. | |
10204 | ||
10205 | Return the number of program header table entries read, or -1 if an | |
10206 | error occurs; bfd_get_error will return an appropriate code. */ | |
c044fabd | 10207 | |
98d8431c | 10208 | int |
217aa764 | 10209 | bfd_get_elf_phdrs (bfd *abfd, void *phdrs) |
98d8431c JB |
10210 | { |
10211 | int num_phdrs; | |
10212 | ||
10213 | if (abfd->xvec->flavour != bfd_target_elf_flavour) | |
10214 | { | |
10215 | bfd_set_error (bfd_error_wrong_format); | |
10216 | return -1; | |
10217 | } | |
10218 | ||
10219 | num_phdrs = elf_elfheader (abfd)->e_phnum; | |
c044fabd | 10220 | memcpy (phdrs, elf_tdata (abfd)->phdr, |
98d8431c JB |
10221 | num_phdrs * sizeof (Elf_Internal_Phdr)); |
10222 | ||
10223 | return num_phdrs; | |
10224 | } | |
ae4221d7 | 10225 | |
db6751f2 | 10226 | enum elf_reloc_type_class |
7e612e98 AM |
10227 | _bfd_elf_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED, |
10228 | const asection *rel_sec ATTRIBUTE_UNUSED, | |
10229 | const Elf_Internal_Rela *rela ATTRIBUTE_UNUSED) | |
db6751f2 JJ |
10230 | { |
10231 | return reloc_class_normal; | |
10232 | } | |
f8df10f4 | 10233 | |
47d9a591 | 10234 | /* For RELA architectures, return the relocation value for a |
f8df10f4 JJ |
10235 | relocation against a local symbol. */ |
10236 | ||
10237 | bfd_vma | |
217aa764 AM |
10238 | _bfd_elf_rela_local_sym (bfd *abfd, |
10239 | Elf_Internal_Sym *sym, | |
8517fae7 | 10240 | asection **psec, |
217aa764 | 10241 | Elf_Internal_Rela *rel) |
f8df10f4 | 10242 | { |
8517fae7 | 10243 | asection *sec = *psec; |
f8df10f4 JJ |
10244 | bfd_vma relocation; |
10245 | ||
10246 | relocation = (sec->output_section->vma | |
10247 | + sec->output_offset | |
10248 | + sym->st_value); | |
10249 | if ((sec->flags & SEC_MERGE) | |
c629eae0 | 10250 | && ELF_ST_TYPE (sym->st_info) == STT_SECTION |
dbaa2011 | 10251 | && sec->sec_info_type == SEC_INFO_TYPE_MERGE) |
f8df10f4 | 10252 | { |
f8df10f4 | 10253 | rel->r_addend = |
8517fae7 | 10254 | _bfd_merged_section_offset (abfd, psec, |
65765700 | 10255 | elf_section_data (sec)->sec_info, |
753731ee AM |
10256 | sym->st_value + rel->r_addend); |
10257 | if (sec != *psec) | |
10258 | { | |
10259 | /* If we have changed the section, and our original section is | |
10260 | marked with SEC_EXCLUDE, it means that the original | |
10261 | SEC_MERGE section has been completely subsumed in some | |
10262 | other SEC_MERGE section. In this case, we need to leave | |
10263 | some info around for --emit-relocs. */ | |
10264 | if ((sec->flags & SEC_EXCLUDE) != 0) | |
10265 | sec->kept_section = *psec; | |
10266 | sec = *psec; | |
10267 | } | |
8517fae7 AM |
10268 | rel->r_addend -= relocation; |
10269 | rel->r_addend += sec->output_section->vma + sec->output_offset; | |
f8df10f4 JJ |
10270 | } |
10271 | return relocation; | |
10272 | } | |
c629eae0 JJ |
10273 | |
10274 | bfd_vma | |
217aa764 AM |
10275 | _bfd_elf_rel_local_sym (bfd *abfd, |
10276 | Elf_Internal_Sym *sym, | |
10277 | asection **psec, | |
10278 | bfd_vma addend) | |
47d9a591 | 10279 | { |
c629eae0 JJ |
10280 | asection *sec = *psec; |
10281 | ||
dbaa2011 | 10282 | if (sec->sec_info_type != SEC_INFO_TYPE_MERGE) |
c629eae0 JJ |
10283 | return sym->st_value + addend; |
10284 | ||
10285 | return _bfd_merged_section_offset (abfd, psec, | |
65765700 | 10286 | elf_section_data (sec)->sec_info, |
753731ee | 10287 | sym->st_value + addend); |
c629eae0 JJ |
10288 | } |
10289 | ||
10290 | bfd_vma | |
217aa764 | 10291 | _bfd_elf_section_offset (bfd *abfd, |
92e4ec35 | 10292 | struct bfd_link_info *info, |
217aa764 AM |
10293 | asection *sec, |
10294 | bfd_vma offset) | |
c629eae0 | 10295 | { |
68bfbfcc | 10296 | switch (sec->sec_info_type) |
65765700 | 10297 | { |
dbaa2011 | 10298 | case SEC_INFO_TYPE_STABS: |
eea6121a AM |
10299 | return _bfd_stab_section_offset (sec, elf_section_data (sec)->sec_info, |
10300 | offset); | |
dbaa2011 | 10301 | case SEC_INFO_TYPE_EH_FRAME: |
92e4ec35 | 10302 | return _bfd_elf_eh_frame_section_offset (abfd, info, sec, offset); |
65765700 | 10303 | default: |
310fd250 L |
10304 | if ((sec->flags & SEC_ELF_REVERSE_COPY) != 0) |
10305 | { | |
10306 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
10307 | bfd_size_type address_size = bed->s->arch_size / 8; | |
10308 | offset = sec->size - offset - address_size; | |
10309 | } | |
65765700 JJ |
10310 | return offset; |
10311 | } | |
c629eae0 | 10312 | } |
3333a7c3 RM |
10313 | \f |
10314 | /* Create a new BFD as if by bfd_openr. Rather than opening a file, | |
10315 | reconstruct an ELF file by reading the segments out of remote memory | |
10316 | based on the ELF file header at EHDR_VMA and the ELF program headers it | |
10317 | points to. If not null, *LOADBASEP is filled in with the difference | |
10318 | between the VMAs from which the segments were read, and the VMAs the | |
10319 | file headers (and hence BFD's idea of each section's VMA) put them at. | |
10320 | ||
10321 | The function TARGET_READ_MEMORY is called to copy LEN bytes from the | |
10322 | remote memory at target address VMA into the local buffer at MYADDR; it | |
10323 | should return zero on success or an `errno' code on failure. TEMPL must | |
10324 | be a BFD for an ELF target with the word size and byte order found in | |
10325 | the remote memory. */ | |
10326 | ||
10327 | bfd * | |
217aa764 AM |
10328 | bfd_elf_bfd_from_remote_memory |
10329 | (bfd *templ, | |
10330 | bfd_vma ehdr_vma, | |
f0a5d95a | 10331 | bfd_size_type size, |
217aa764 | 10332 | bfd_vma *loadbasep, |
fe78531d | 10333 | int (*target_read_memory) (bfd_vma, bfd_byte *, bfd_size_type)) |
3333a7c3 RM |
10334 | { |
10335 | return (*get_elf_backend_data (templ)->elf_backend_bfd_from_remote_memory) | |
5979d6b6 | 10336 | (templ, ehdr_vma, size, loadbasep, target_read_memory); |
3333a7c3 | 10337 | } |
4c45e5c9 JJ |
10338 | \f |
10339 | long | |
c9727e01 AM |
10340 | _bfd_elf_get_synthetic_symtab (bfd *abfd, |
10341 | long symcount ATTRIBUTE_UNUSED, | |
10342 | asymbol **syms ATTRIBUTE_UNUSED, | |
8615f3f2 | 10343 | long dynsymcount, |
c9727e01 AM |
10344 | asymbol **dynsyms, |
10345 | asymbol **ret) | |
4c45e5c9 JJ |
10346 | { |
10347 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
10348 | asection *relplt; | |
10349 | asymbol *s; | |
10350 | const char *relplt_name; | |
10351 | bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean); | |
10352 | arelent *p; | |
10353 | long count, i, n; | |
10354 | size_t size; | |
10355 | Elf_Internal_Shdr *hdr; | |
10356 | char *names; | |
10357 | asection *plt; | |
10358 | ||
8615f3f2 AM |
10359 | *ret = NULL; |
10360 | ||
90e3cdf2 JJ |
10361 | if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0) |
10362 | return 0; | |
10363 | ||
8615f3f2 AM |
10364 | if (dynsymcount <= 0) |
10365 | return 0; | |
10366 | ||
4c45e5c9 JJ |
10367 | if (!bed->plt_sym_val) |
10368 | return 0; | |
10369 | ||
10370 | relplt_name = bed->relplt_name; | |
10371 | if (relplt_name == NULL) | |
d35fd659 | 10372 | relplt_name = bed->rela_plts_and_copies_p ? ".rela.plt" : ".rel.plt"; |
4c45e5c9 JJ |
10373 | relplt = bfd_get_section_by_name (abfd, relplt_name); |
10374 | if (relplt == NULL) | |
10375 | return 0; | |
10376 | ||
10377 | hdr = &elf_section_data (relplt)->this_hdr; | |
10378 | if (hdr->sh_link != elf_dynsymtab (abfd) | |
10379 | || (hdr->sh_type != SHT_REL && hdr->sh_type != SHT_RELA)) | |
10380 | return 0; | |
10381 | ||
10382 | plt = bfd_get_section_by_name (abfd, ".plt"); | |
10383 | if (plt == NULL) | |
10384 | return 0; | |
10385 | ||
10386 | slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table; | |
c9727e01 | 10387 | if (! (*slurp_relocs) (abfd, relplt, dynsyms, TRUE)) |
4c45e5c9 JJ |
10388 | return -1; |
10389 | ||
eea6121a | 10390 | count = relplt->size / hdr->sh_entsize; |
4c45e5c9 JJ |
10391 | size = count * sizeof (asymbol); |
10392 | p = relplt->relocation; | |
cb53bf42 | 10393 | for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel) |
041de40d AM |
10394 | { |
10395 | size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt"); | |
10396 | if (p->addend != 0) | |
10397 | { | |
10398 | #ifdef BFD64 | |
10399 | size += sizeof ("+0x") - 1 + 8 + 8 * (bed->s->elfclass == ELFCLASS64); | |
10400 | #else | |
10401 | size += sizeof ("+0x") - 1 + 8; | |
10402 | #endif | |
10403 | } | |
10404 | } | |
4c45e5c9 | 10405 | |
a50b1753 | 10406 | s = *ret = (asymbol *) bfd_malloc (size); |
4c45e5c9 JJ |
10407 | if (s == NULL) |
10408 | return -1; | |
10409 | ||
10410 | names = (char *) (s + count); | |
10411 | p = relplt->relocation; | |
10412 | n = 0; | |
cb53bf42 | 10413 | for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel) |
4c45e5c9 JJ |
10414 | { |
10415 | size_t len; | |
10416 | bfd_vma addr; | |
10417 | ||
10418 | addr = bed->plt_sym_val (i, plt, p); | |
10419 | if (addr == (bfd_vma) -1) | |
10420 | continue; | |
10421 | ||
10422 | *s = **p->sym_ptr_ptr; | |
65a7a66f AM |
10423 | /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since |
10424 | we are defining a symbol, ensure one of them is set. */ | |
10425 | if ((s->flags & BSF_LOCAL) == 0) | |
10426 | s->flags |= BSF_GLOBAL; | |
6ba2a415 | 10427 | s->flags |= BSF_SYNTHETIC; |
4c45e5c9 JJ |
10428 | s->section = plt; |
10429 | s->value = addr - plt->vma; | |
10430 | s->name = names; | |
8f39ba8e | 10431 | s->udata.p = NULL; |
4c45e5c9 JJ |
10432 | len = strlen ((*p->sym_ptr_ptr)->name); |
10433 | memcpy (names, (*p->sym_ptr_ptr)->name, len); | |
10434 | names += len; | |
041de40d AM |
10435 | if (p->addend != 0) |
10436 | { | |
1d770845 | 10437 | char buf[30], *a; |
d324f6d6 | 10438 | |
041de40d AM |
10439 | memcpy (names, "+0x", sizeof ("+0x") - 1); |
10440 | names += sizeof ("+0x") - 1; | |
1d770845 L |
10441 | bfd_sprintf_vma (abfd, buf, p->addend); |
10442 | for (a = buf; *a == '0'; ++a) | |
10443 | ; | |
10444 | len = strlen (a); | |
10445 | memcpy (names, a, len); | |
10446 | names += len; | |
041de40d | 10447 | } |
4c45e5c9 JJ |
10448 | memcpy (names, "@plt", sizeof ("@plt")); |
10449 | names += sizeof ("@plt"); | |
8f39ba8e | 10450 | ++s, ++n; |
4c45e5c9 JJ |
10451 | } |
10452 | ||
10453 | return n; | |
10454 | } | |
3d7f7666 | 10455 | |
3b22753a L |
10456 | /* It is only used by x86-64 so far. */ |
10457 | asection _bfd_elf_large_com_section | |
10458 | = BFD_FAKE_SECTION (_bfd_elf_large_com_section, | |
f592407e | 10459 | SEC_IS_COMMON, NULL, "LARGE_COMMON", 0); |
ecca9871 | 10460 | |
d1036acb | 10461 | void |
78245035 L |
10462 | _bfd_elf_post_process_headers (bfd * abfd, |
10463 | struct bfd_link_info * link_info ATTRIBUTE_UNUSED) | |
d1036acb L |
10464 | { |
10465 | Elf_Internal_Ehdr * i_ehdrp; /* ELF file header, internal form. */ | |
10466 | ||
10467 | i_ehdrp = elf_elfheader (abfd); | |
10468 | ||
10469 | i_ehdrp->e_ident[EI_OSABI] = get_elf_backend_data (abfd)->elf_osabi; | |
d8045f23 NC |
10470 | |
10471 | /* To make things simpler for the loader on Linux systems we set the | |
9c55345c | 10472 | osabi field to ELFOSABI_GNU if the binary contains symbols of |
f64b2e8d | 10473 | the STT_GNU_IFUNC type or STB_GNU_UNIQUE binding. */ |
d8045f23 | 10474 | if (i_ehdrp->e_ident[EI_OSABI] == ELFOSABI_NONE |
f64b2e8d | 10475 | && elf_tdata (abfd)->has_gnu_symbols) |
9c55345c | 10476 | i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_GNU; |
d1036acb | 10477 | } |
fcb93ecf PB |
10478 | |
10479 | ||
10480 | /* Return TRUE for ELF symbol types that represent functions. | |
10481 | This is the default version of this function, which is sufficient for | |
d8045f23 | 10482 | most targets. It returns true if TYPE is STT_FUNC or STT_GNU_IFUNC. */ |
fcb93ecf PB |
10483 | |
10484 | bfd_boolean | |
10485 | _bfd_elf_is_function_type (unsigned int type) | |
10486 | { | |
d8045f23 NC |
10487 | return (type == STT_FUNC |
10488 | || type == STT_GNU_IFUNC); | |
fcb93ecf | 10489 | } |
9f296da3 | 10490 | |
aef36ac1 AM |
10491 | /* If the ELF symbol SYM might be a function in SEC, return the |
10492 | function size and set *CODE_OFF to the function's entry point, | |
10493 | otherwise return zero. */ | |
9f296da3 | 10494 | |
aef36ac1 AM |
10495 | bfd_size_type |
10496 | _bfd_elf_maybe_function_sym (const asymbol *sym, asection *sec, | |
10497 | bfd_vma *code_off) | |
9f296da3 | 10498 | { |
aef36ac1 AM |
10499 | bfd_size_type size; |
10500 | ||
ff9e0f5b | 10501 | if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT |
aef36ac1 AM |
10502 | | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0 |
10503 | || sym->section != sec) | |
10504 | return 0; | |
ff9e0f5b | 10505 | |
ff9e0f5b | 10506 | *code_off = sym->value; |
aef36ac1 AM |
10507 | size = 0; |
10508 | if (!(sym->flags & BSF_SYNTHETIC)) | |
10509 | size = ((elf_symbol_type *) sym)->internal_elf_sym.st_size; | |
10510 | if (size == 0) | |
10511 | size = 1; | |
10512 | return size; | |
9f296da3 | 10513 | } |