Commit | Line | Data |
---|---|---|
252b5132 | 1 | /* ELF executable support for BFD. |
340b6d91 | 2 | |
6f2750fe | 3 | Copyright (C) 1993-2016 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" |
de64ce13 | 45 | #include "elf-linux-core.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; | |
6a40cf0c NC |
403 | if (elf_symtab_shndx_list (ibfd) != NULL) |
404 | { | |
405 | elf_section_list * entry; | |
406 | Elf_Internal_Shdr **sections = elf_elfsections (ibfd); | |
407 | ||
408 | /* Find an index section that is linked to this symtab section. */ | |
409 | for (entry = elf_symtab_shndx_list (ibfd); entry != NULL; entry = entry->next) | |
410 | if (sections[entry->hdr.sh_link] == symtab_hdr) | |
411 | { | |
412 | shndx_hdr = & entry->hdr; | |
413 | break; | |
414 | }; | |
415 | ||
416 | if (shndx_hdr == NULL) | |
417 | { | |
418 | if (symtab_hdr == & elf_symtab_hdr (ibfd)) | |
419 | /* Not really accurate, but this was how the old code used to work. */ | |
420 | shndx_hdr = & elf_symtab_shndx_list (ibfd)->hdr; | |
421 | /* Otherwise we do nothing. The assumption is that | |
422 | the index table will not be needed. */ | |
423 | } | |
424 | } | |
6cdc0ccc AM |
425 | |
426 | /* Read the symbols. */ | |
427 | alloc_ext = NULL; | |
428 | alloc_extshndx = NULL; | |
4dd07732 | 429 | alloc_intsym = NULL; |
6cdc0ccc AM |
430 | bed = get_elf_backend_data (ibfd); |
431 | extsym_size = bed->s->sizeof_sym; | |
432 | amt = symcount * extsym_size; | |
433 | pos = symtab_hdr->sh_offset + symoffset * extsym_size; | |
434 | if (extsym_buf == NULL) | |
435 | { | |
d0fb9a8d | 436 | alloc_ext = bfd_malloc2 (symcount, extsym_size); |
6cdc0ccc AM |
437 | extsym_buf = alloc_ext; |
438 | } | |
439 | if (extsym_buf == NULL | |
440 | || bfd_seek (ibfd, pos, SEEK_SET) != 0 | |
441 | || bfd_bread (extsym_buf, amt, ibfd) != amt) | |
442 | { | |
443 | intsym_buf = NULL; | |
444 | goto out; | |
445 | } | |
446 | ||
447 | if (shndx_hdr == NULL || shndx_hdr->sh_size == 0) | |
448 | extshndx_buf = NULL; | |
449 | else | |
450 | { | |
451 | amt = symcount * sizeof (Elf_External_Sym_Shndx); | |
452 | pos = shndx_hdr->sh_offset + symoffset * sizeof (Elf_External_Sym_Shndx); | |
453 | if (extshndx_buf == NULL) | |
454 | { | |
a50b1753 NC |
455 | alloc_extshndx = (Elf_External_Sym_Shndx *) |
456 | bfd_malloc2 (symcount, sizeof (Elf_External_Sym_Shndx)); | |
6cdc0ccc AM |
457 | extshndx_buf = alloc_extshndx; |
458 | } | |
459 | if (extshndx_buf == NULL | |
460 | || bfd_seek (ibfd, pos, SEEK_SET) != 0 | |
461 | || bfd_bread (extshndx_buf, amt, ibfd) != amt) | |
462 | { | |
463 | intsym_buf = NULL; | |
464 | goto out; | |
465 | } | |
466 | } | |
467 | ||
468 | if (intsym_buf == NULL) | |
469 | { | |
a50b1753 NC |
470 | alloc_intsym = (Elf_Internal_Sym *) |
471 | bfd_malloc2 (symcount, sizeof (Elf_Internal_Sym)); | |
4dd07732 | 472 | intsym_buf = alloc_intsym; |
6cdc0ccc AM |
473 | if (intsym_buf == NULL) |
474 | goto out; | |
475 | } | |
476 | ||
477 | /* Convert the symbols to internal form. */ | |
478 | isymend = intsym_buf + symcount; | |
a50b1753 NC |
479 | for (esym = (const bfd_byte *) extsym_buf, isym = intsym_buf, |
480 | shndx = extshndx_buf; | |
6cdc0ccc AM |
481 | isym < isymend; |
482 | esym += extsym_size, isym++, shndx = shndx != NULL ? shndx + 1 : NULL) | |
8384fb8f AM |
483 | if (!(*bed->s->swap_symbol_in) (ibfd, esym, shndx, isym)) |
484 | { | |
485 | symoffset += (esym - (bfd_byte *) extsym_buf) / extsym_size; | |
486 | (*_bfd_error_handler) (_("%B symbol number %lu references " | |
487 | "nonexistent SHT_SYMTAB_SHNDX section"), | |
488 | ibfd, (unsigned long) symoffset); | |
4dd07732 AM |
489 | if (alloc_intsym != NULL) |
490 | free (alloc_intsym); | |
8384fb8f AM |
491 | intsym_buf = NULL; |
492 | goto out; | |
493 | } | |
6cdc0ccc AM |
494 | |
495 | out: | |
496 | if (alloc_ext != NULL) | |
497 | free (alloc_ext); | |
498 | if (alloc_extshndx != NULL) | |
499 | free (alloc_extshndx); | |
500 | ||
501 | return intsym_buf; | |
502 | } | |
503 | ||
5cab59f6 AM |
504 | /* Look up a symbol name. */ |
505 | const char * | |
be8dd2ca AM |
506 | bfd_elf_sym_name (bfd *abfd, |
507 | Elf_Internal_Shdr *symtab_hdr, | |
26c61ae5 L |
508 | Elf_Internal_Sym *isym, |
509 | asection *sym_sec) | |
5cab59f6 | 510 | { |
26c61ae5 | 511 | const char *name; |
5cab59f6 | 512 | unsigned int iname = isym->st_name; |
be8dd2ca | 513 | unsigned int shindex = symtab_hdr->sh_link; |
26c61ae5 | 514 | |
138f35cc JJ |
515 | if (iname == 0 && ELF_ST_TYPE (isym->st_info) == STT_SECTION |
516 | /* Check for a bogus st_shndx to avoid crashing. */ | |
4fbb74a6 | 517 | && isym->st_shndx < elf_numsections (abfd)) |
5cab59f6 AM |
518 | { |
519 | iname = elf_elfsections (abfd)[isym->st_shndx]->sh_name; | |
520 | shindex = elf_elfheader (abfd)->e_shstrndx; | |
521 | } | |
522 | ||
26c61ae5 L |
523 | name = bfd_elf_string_from_elf_section (abfd, shindex, iname); |
524 | if (name == NULL) | |
525 | name = "(null)"; | |
526 | else if (sym_sec && *name == '\0') | |
527 | name = bfd_section_name (abfd, sym_sec); | |
528 | ||
529 | return name; | |
5cab59f6 AM |
530 | } |
531 | ||
dbb410c3 AM |
532 | /* Elf_Internal_Shdr->contents is an array of these for SHT_GROUP |
533 | sections. The first element is the flags, the rest are section | |
534 | pointers. */ | |
535 | ||
536 | typedef union elf_internal_group { | |
537 | Elf_Internal_Shdr *shdr; | |
538 | unsigned int flags; | |
539 | } Elf_Internal_Group; | |
540 | ||
b885599b AM |
541 | /* Return the name of the group signature symbol. Why isn't the |
542 | signature just a string? */ | |
543 | ||
544 | static const char * | |
217aa764 | 545 | group_signature (bfd *abfd, Elf_Internal_Shdr *ghdr) |
b885599b | 546 | { |
9dce4196 | 547 | Elf_Internal_Shdr *hdr; |
9dce4196 AM |
548 | unsigned char esym[sizeof (Elf64_External_Sym)]; |
549 | Elf_External_Sym_Shndx eshndx; | |
550 | Elf_Internal_Sym isym; | |
b885599b | 551 | |
13792e9d L |
552 | /* First we need to ensure the symbol table is available. Make sure |
553 | that it is a symbol table section. */ | |
4fbb74a6 AM |
554 | if (ghdr->sh_link >= elf_numsections (abfd)) |
555 | return NULL; | |
13792e9d L |
556 | hdr = elf_elfsections (abfd) [ghdr->sh_link]; |
557 | if (hdr->sh_type != SHT_SYMTAB | |
558 | || ! bfd_section_from_shdr (abfd, ghdr->sh_link)) | |
b885599b AM |
559 | return NULL; |
560 | ||
9dce4196 AM |
561 | /* Go read the symbol. */ |
562 | hdr = &elf_tdata (abfd)->symtab_hdr; | |
6cdc0ccc AM |
563 | if (bfd_elf_get_elf_syms (abfd, hdr, 1, ghdr->sh_info, |
564 | &isym, esym, &eshndx) == NULL) | |
b885599b | 565 | return NULL; |
9dce4196 | 566 | |
26c61ae5 | 567 | return bfd_elf_sym_name (abfd, hdr, &isym, NULL); |
b885599b AM |
568 | } |
569 | ||
dbb410c3 AM |
570 | /* Set next_in_group list pointer, and group name for NEWSECT. */ |
571 | ||
b34976b6 | 572 | static bfd_boolean |
217aa764 | 573 | setup_group (bfd *abfd, Elf_Internal_Shdr *hdr, asection *newsect) |
dbb410c3 AM |
574 | { |
575 | unsigned int num_group = elf_tdata (abfd)->num_group; | |
576 | ||
577 | /* If num_group is zero, read in all SHT_GROUP sections. The count | |
578 | is set to -1 if there are no SHT_GROUP sections. */ | |
579 | if (num_group == 0) | |
580 | { | |
581 | unsigned int i, shnum; | |
582 | ||
583 | /* First count the number of groups. If we have a SHT_GROUP | |
584 | section with just a flag word (ie. sh_size is 4), ignore it. */ | |
9ad5cbcf | 585 | shnum = elf_numsections (abfd); |
dbb410c3 | 586 | num_group = 0; |
08a40648 | 587 | |
44534af3 | 588 | #define IS_VALID_GROUP_SECTION_HEADER(shdr, minsize) \ |
1783205a | 589 | ( (shdr)->sh_type == SHT_GROUP \ |
44534af3 | 590 | && (shdr)->sh_size >= minsize \ |
1783205a NC |
591 | && (shdr)->sh_entsize == GRP_ENTRY_SIZE \ |
592 | && ((shdr)->sh_size % GRP_ENTRY_SIZE) == 0) | |
08a40648 | 593 | |
dbb410c3 AM |
594 | for (i = 0; i < shnum; i++) |
595 | { | |
596 | Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i]; | |
1783205a | 597 | |
44534af3 | 598 | if (IS_VALID_GROUP_SECTION_HEADER (shdr, 2 * GRP_ENTRY_SIZE)) |
dbb410c3 AM |
599 | num_group += 1; |
600 | } | |
601 | ||
602 | if (num_group == 0) | |
20dbb49d L |
603 | { |
604 | num_group = (unsigned) -1; | |
605 | elf_tdata (abfd)->num_group = num_group; | |
606 | } | |
607 | else | |
dbb410c3 AM |
608 | { |
609 | /* We keep a list of elf section headers for group sections, | |
610 | so we can find them quickly. */ | |
20dbb49d | 611 | bfd_size_type amt; |
d0fb9a8d | 612 | |
20dbb49d | 613 | elf_tdata (abfd)->num_group = num_group; |
a50b1753 NC |
614 | elf_tdata (abfd)->group_sect_ptr = (Elf_Internal_Shdr **) |
615 | bfd_alloc2 (abfd, num_group, sizeof (Elf_Internal_Shdr *)); | |
dbb410c3 | 616 | if (elf_tdata (abfd)->group_sect_ptr == NULL) |
b34976b6 | 617 | return FALSE; |
dbb410c3 AM |
618 | |
619 | num_group = 0; | |
620 | for (i = 0; i < shnum; i++) | |
621 | { | |
622 | Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i]; | |
1783205a | 623 | |
44534af3 | 624 | if (IS_VALID_GROUP_SECTION_HEADER (shdr, 2 * GRP_ENTRY_SIZE)) |
dbb410c3 | 625 | { |
973ffd63 | 626 | unsigned char *src; |
dbb410c3 AM |
627 | Elf_Internal_Group *dest; |
628 | ||
629 | /* Add to list of sections. */ | |
630 | elf_tdata (abfd)->group_sect_ptr[num_group] = shdr; | |
631 | num_group += 1; | |
632 | ||
633 | /* Read the raw contents. */ | |
634 | BFD_ASSERT (sizeof (*dest) >= 4); | |
635 | amt = shdr->sh_size * sizeof (*dest) / 4; | |
a50b1753 NC |
636 | shdr->contents = (unsigned char *) |
637 | bfd_alloc2 (abfd, shdr->sh_size, sizeof (*dest) / 4); | |
1783205a NC |
638 | /* PR binutils/4110: Handle corrupt group headers. */ |
639 | if (shdr->contents == NULL) | |
640 | { | |
641 | _bfd_error_handler | |
493a3386 | 642 | (_("%B: corrupt size field in group section header: 0x%lx"), abfd, shdr->sh_size); |
1783205a | 643 | bfd_set_error (bfd_error_bad_value); |
493a3386 NC |
644 | -- num_group; |
645 | continue; | |
1783205a NC |
646 | } |
647 | ||
648 | memset (shdr->contents, 0, amt); | |
649 | ||
650 | if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0 | |
dbb410c3 AM |
651 | || (bfd_bread (shdr->contents, shdr->sh_size, abfd) |
652 | != shdr->sh_size)) | |
493a3386 NC |
653 | { |
654 | _bfd_error_handler | |
655 | (_("%B: invalid size field in group section header: 0x%lx"), abfd, shdr->sh_size); | |
656 | bfd_set_error (bfd_error_bad_value); | |
657 | -- num_group; | |
658 | /* PR 17510: If the group contents are even partially | |
659 | corrupt, do not allow any of the contents to be used. */ | |
660 | memset (shdr->contents, 0, amt); | |
661 | continue; | |
662 | } | |
708d7d0d | 663 | |
dbb410c3 AM |
664 | /* Translate raw contents, a flag word followed by an |
665 | array of elf section indices all in target byte order, | |
666 | to the flag word followed by an array of elf section | |
667 | pointers. */ | |
668 | src = shdr->contents + shdr->sh_size; | |
669 | dest = (Elf_Internal_Group *) (shdr->contents + amt); | |
06614111 | 670 | |
dbb410c3 AM |
671 | while (1) |
672 | { | |
673 | unsigned int idx; | |
674 | ||
675 | src -= 4; | |
676 | --dest; | |
677 | idx = H_GET_32 (abfd, src); | |
678 | if (src == shdr->contents) | |
679 | { | |
680 | dest->flags = idx; | |
b885599b AM |
681 | if (shdr->bfd_section != NULL && (idx & GRP_COMDAT)) |
682 | shdr->bfd_section->flags | |
683 | |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD; | |
dbb410c3 AM |
684 | break; |
685 | } | |
686 | if (idx >= shnum) | |
687 | { | |
688 | ((*_bfd_error_handler) | |
d003868e | 689 | (_("%B: invalid SHT_GROUP entry"), abfd)); |
dbb410c3 AM |
690 | idx = 0; |
691 | } | |
692 | dest->shdr = elf_elfsections (abfd)[idx]; | |
693 | } | |
694 | } | |
695 | } | |
493a3386 NC |
696 | |
697 | /* PR 17510: Corrupt binaries might contain invalid groups. */ | |
698 | if (num_group != (unsigned) elf_tdata (abfd)->num_group) | |
699 | { | |
700 | elf_tdata (abfd)->num_group = num_group; | |
701 | ||
702 | /* If all groups are invalid then fail. */ | |
703 | if (num_group == 0) | |
704 | { | |
705 | elf_tdata (abfd)->group_sect_ptr = NULL; | |
706 | elf_tdata (abfd)->num_group = num_group = -1; | |
707 | (*_bfd_error_handler) (_("%B: no valid group sections found"), abfd); | |
708 | bfd_set_error (bfd_error_bad_value); | |
709 | } | |
710 | } | |
dbb410c3 AM |
711 | } |
712 | } | |
713 | ||
714 | if (num_group != (unsigned) -1) | |
715 | { | |
716 | unsigned int i; | |
717 | ||
718 | for (i = 0; i < num_group; i++) | |
719 | { | |
720 | Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i]; | |
721 | Elf_Internal_Group *idx = (Elf_Internal_Group *) shdr->contents; | |
722 | unsigned int n_elt = shdr->sh_size / 4; | |
723 | ||
724 | /* Look through this group's sections to see if current | |
725 | section is a member. */ | |
726 | while (--n_elt != 0) | |
727 | if ((++idx)->shdr == hdr) | |
728 | { | |
e0e8c97f | 729 | asection *s = NULL; |
dbb410c3 AM |
730 | |
731 | /* We are a member of this group. Go looking through | |
732 | other members to see if any others are linked via | |
733 | next_in_group. */ | |
734 | idx = (Elf_Internal_Group *) shdr->contents; | |
735 | n_elt = shdr->sh_size / 4; | |
736 | while (--n_elt != 0) | |
737 | if ((s = (++idx)->shdr->bfd_section) != NULL | |
945906ff | 738 | && elf_next_in_group (s) != NULL) |
dbb410c3 AM |
739 | break; |
740 | if (n_elt != 0) | |
741 | { | |
dbb410c3 AM |
742 | /* Snarf the group name from other member, and |
743 | insert current section in circular list. */ | |
945906ff AM |
744 | elf_group_name (newsect) = elf_group_name (s); |
745 | elf_next_in_group (newsect) = elf_next_in_group (s); | |
746 | elf_next_in_group (s) = newsect; | |
dbb410c3 AM |
747 | } |
748 | else | |
749 | { | |
dbb410c3 AM |
750 | const char *gname; |
751 | ||
b885599b AM |
752 | gname = group_signature (abfd, shdr); |
753 | if (gname == NULL) | |
b34976b6 | 754 | return FALSE; |
945906ff | 755 | elf_group_name (newsect) = gname; |
dbb410c3 AM |
756 | |
757 | /* Start a circular list with one element. */ | |
945906ff | 758 | elf_next_in_group (newsect) = newsect; |
dbb410c3 | 759 | } |
b885599b | 760 | |
9dce4196 AM |
761 | /* If the group section has been created, point to the |
762 | new member. */ | |
dbb410c3 | 763 | if (shdr->bfd_section != NULL) |
945906ff | 764 | elf_next_in_group (shdr->bfd_section) = newsect; |
b885599b | 765 | |
dbb410c3 AM |
766 | i = num_group - 1; |
767 | break; | |
768 | } | |
769 | } | |
770 | } | |
771 | ||
945906ff | 772 | if (elf_group_name (newsect) == NULL) |
dbb410c3 | 773 | { |
d003868e AM |
774 | (*_bfd_error_handler) (_("%B: no group info for section %A"), |
775 | abfd, newsect); | |
493a3386 | 776 | return FALSE; |
dbb410c3 | 777 | } |
b34976b6 | 778 | return TRUE; |
dbb410c3 AM |
779 | } |
780 | ||
3d7f7666 | 781 | bfd_boolean |
dd863624 | 782 | _bfd_elf_setup_sections (bfd *abfd) |
3d7f7666 L |
783 | { |
784 | unsigned int i; | |
785 | unsigned int num_group = elf_tdata (abfd)->num_group; | |
786 | bfd_boolean result = TRUE; | |
dd863624 L |
787 | asection *s; |
788 | ||
789 | /* Process SHF_LINK_ORDER. */ | |
790 | for (s = abfd->sections; s != NULL; s = s->next) | |
791 | { | |
792 | Elf_Internal_Shdr *this_hdr = &elf_section_data (s)->this_hdr; | |
793 | if ((this_hdr->sh_flags & SHF_LINK_ORDER) != 0) | |
794 | { | |
795 | unsigned int elfsec = this_hdr->sh_link; | |
796 | /* FIXME: The old Intel compiler and old strip/objcopy may | |
797 | not set the sh_link or sh_info fields. Hence we could | |
798 | get the situation where elfsec is 0. */ | |
799 | if (elfsec == 0) | |
800 | { | |
4fbb74a6 | 801 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
dd863624 L |
802 | if (bed->link_order_error_handler) |
803 | bed->link_order_error_handler | |
804 | (_("%B: warning: sh_link not set for section `%A'"), | |
805 | abfd, s); | |
806 | } | |
807 | else | |
808 | { | |
91d6fa6a | 809 | asection *linksec = NULL; |
25bbc984 | 810 | |
4fbb74a6 AM |
811 | if (elfsec < elf_numsections (abfd)) |
812 | { | |
813 | this_hdr = elf_elfsections (abfd)[elfsec]; | |
91d6fa6a | 814 | linksec = this_hdr->bfd_section; |
4fbb74a6 | 815 | } |
25bbc984 L |
816 | |
817 | /* PR 1991, 2008: | |
818 | Some strip/objcopy may leave an incorrect value in | |
819 | sh_link. We don't want to proceed. */ | |
91d6fa6a | 820 | if (linksec == NULL) |
25bbc984 L |
821 | { |
822 | (*_bfd_error_handler) | |
823 | (_("%B: sh_link [%d] in section `%A' is incorrect"), | |
824 | s->owner, s, elfsec); | |
825 | result = FALSE; | |
826 | } | |
827 | ||
91d6fa6a | 828 | elf_linked_to_section (s) = linksec; |
dd863624 L |
829 | } |
830 | } | |
831 | } | |
3d7f7666 | 832 | |
dd863624 | 833 | /* Process section groups. */ |
3d7f7666 L |
834 | if (num_group == (unsigned) -1) |
835 | return result; | |
836 | ||
837 | for (i = 0; i < num_group; i++) | |
838 | { | |
839 | Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i]; | |
4b0e8a5f NC |
840 | Elf_Internal_Group *idx; |
841 | unsigned int n_elt; | |
3d7f7666 | 842 | |
4b0e8a5f NC |
843 | /* PR binutils/18758: Beware of corrupt binaries with invalid group data. */ |
844 | if (shdr == NULL || shdr->bfd_section == NULL || shdr->contents == NULL) | |
845 | { | |
846 | (*_bfd_error_handler) | |
847 | (_("%B: section group entry number %u is corrupt"), | |
848 | abfd, i); | |
849 | result = FALSE; | |
850 | continue; | |
851 | } | |
852 | ||
853 | idx = (Elf_Internal_Group *) shdr->contents; | |
854 | n_elt = shdr->sh_size / 4; | |
1b786873 | 855 | |
3d7f7666 L |
856 | while (--n_elt != 0) |
857 | if ((++idx)->shdr->bfd_section) | |
858 | elf_sec_group (idx->shdr->bfd_section) = shdr->bfd_section; | |
859 | else if (idx->shdr->sh_type == SHT_RELA | |
860 | || idx->shdr->sh_type == SHT_REL) | |
861 | /* We won't include relocation sections in section groups in | |
862 | output object files. We adjust the group section size here | |
863 | so that relocatable link will work correctly when | |
864 | relocation sections are in section group in input object | |
865 | files. */ | |
866 | shdr->bfd_section->size -= 4; | |
867 | else | |
868 | { | |
869 | /* There are some unknown sections in the group. */ | |
870 | (*_bfd_error_handler) | |
d003868e AM |
871 | (_("%B: unknown [%d] section `%s' in group [%s]"), |
872 | abfd, | |
3d7f7666 | 873 | (unsigned int) idx->shdr->sh_type, |
1b3a8575 AM |
874 | bfd_elf_string_from_elf_section (abfd, |
875 | (elf_elfheader (abfd) | |
876 | ->e_shstrndx), | |
877 | idx->shdr->sh_name), | |
3d7f7666 L |
878 | shdr->bfd_section->name); |
879 | result = FALSE; | |
880 | } | |
881 | } | |
882 | return result; | |
883 | } | |
884 | ||
72adc230 AM |
885 | bfd_boolean |
886 | bfd_elf_is_group_section (bfd *abfd ATTRIBUTE_UNUSED, const asection *sec) | |
887 | { | |
888 | return elf_next_in_group (sec) != NULL; | |
889 | } | |
890 | ||
f6fe1ccd L |
891 | static char * |
892 | convert_debug_to_zdebug (bfd *abfd, const char *name) | |
893 | { | |
894 | unsigned int len = strlen (name); | |
895 | char *new_name = bfd_alloc (abfd, len + 2); | |
896 | if (new_name == NULL) | |
897 | return NULL; | |
898 | new_name[0] = '.'; | |
899 | new_name[1] = 'z'; | |
900 | memcpy (new_name + 2, name + 1, len); | |
901 | return new_name; | |
902 | } | |
903 | ||
904 | static char * | |
905 | convert_zdebug_to_debug (bfd *abfd, const char *name) | |
906 | { | |
907 | unsigned int len = strlen (name); | |
908 | char *new_name = bfd_alloc (abfd, len); | |
909 | if (new_name == NULL) | |
910 | return NULL; | |
911 | new_name[0] = '.'; | |
912 | memcpy (new_name + 1, name + 2, len - 1); | |
913 | return new_name; | |
914 | } | |
915 | ||
252b5132 RH |
916 | /* Make a BFD section from an ELF section. We store a pointer to the |
917 | BFD section in the bfd_section field of the header. */ | |
918 | ||
b34976b6 | 919 | bfd_boolean |
217aa764 AM |
920 | _bfd_elf_make_section_from_shdr (bfd *abfd, |
921 | Elf_Internal_Shdr *hdr, | |
6dc132d9 L |
922 | const char *name, |
923 | int shindex) | |
252b5132 RH |
924 | { |
925 | asection *newsect; | |
926 | flagword flags; | |
9c5bfbb7 | 927 | const struct elf_backend_data *bed; |
252b5132 RH |
928 | |
929 | if (hdr->bfd_section != NULL) | |
4e011fb5 | 930 | return TRUE; |
252b5132 RH |
931 | |
932 | newsect = bfd_make_section_anyway (abfd, name); | |
933 | if (newsect == NULL) | |
b34976b6 | 934 | return FALSE; |
252b5132 | 935 | |
1829f4b2 AM |
936 | hdr->bfd_section = newsect; |
937 | elf_section_data (newsect)->this_hdr = *hdr; | |
6dc132d9 | 938 | elf_section_data (newsect)->this_idx = shindex; |
1829f4b2 | 939 | |
2f89ff8d L |
940 | /* Always use the real type/flags. */ |
941 | elf_section_type (newsect) = hdr->sh_type; | |
942 | elf_section_flags (newsect) = hdr->sh_flags; | |
943 | ||
252b5132 RH |
944 | newsect->filepos = hdr->sh_offset; |
945 | ||
946 | if (! bfd_set_section_vma (abfd, newsect, hdr->sh_addr) | |
947 | || ! bfd_set_section_size (abfd, newsect, hdr->sh_size) | |
948 | || ! bfd_set_section_alignment (abfd, newsect, | |
72de5009 | 949 | bfd_log2 (hdr->sh_addralign))) |
b34976b6 | 950 | return FALSE; |
252b5132 RH |
951 | |
952 | flags = SEC_NO_FLAGS; | |
953 | if (hdr->sh_type != SHT_NOBITS) | |
954 | flags |= SEC_HAS_CONTENTS; | |
dbb410c3 | 955 | if (hdr->sh_type == SHT_GROUP) |
b3096250 | 956 | flags |= SEC_GROUP | SEC_EXCLUDE; |
252b5132 RH |
957 | if ((hdr->sh_flags & SHF_ALLOC) != 0) |
958 | { | |
959 | flags |= SEC_ALLOC; | |
960 | if (hdr->sh_type != SHT_NOBITS) | |
961 | flags |= SEC_LOAD; | |
962 | } | |
963 | if ((hdr->sh_flags & SHF_WRITE) == 0) | |
964 | flags |= SEC_READONLY; | |
965 | if ((hdr->sh_flags & SHF_EXECINSTR) != 0) | |
966 | flags |= SEC_CODE; | |
967 | else if ((flags & SEC_LOAD) != 0) | |
968 | flags |= SEC_DATA; | |
f5fa8ca2 JJ |
969 | if ((hdr->sh_flags & SHF_MERGE) != 0) |
970 | { | |
971 | flags |= SEC_MERGE; | |
972 | newsect->entsize = hdr->sh_entsize; | |
f5fa8ca2 | 973 | } |
84865015 NC |
974 | if ((hdr->sh_flags & SHF_STRINGS) != 0) |
975 | flags |= SEC_STRINGS; | |
dbb410c3 AM |
976 | if (hdr->sh_flags & SHF_GROUP) |
977 | if (!setup_group (abfd, hdr, newsect)) | |
b34976b6 | 978 | return FALSE; |
13ae64f3 JJ |
979 | if ((hdr->sh_flags & SHF_TLS) != 0) |
980 | flags |= SEC_THREAD_LOCAL; | |
18ae9cc1 L |
981 | if ((hdr->sh_flags & SHF_EXCLUDE) != 0) |
982 | flags |= SEC_EXCLUDE; | |
252b5132 | 983 | |
3d2b39cf | 984 | if ((flags & SEC_ALLOC) == 0) |
7a6cc5fb | 985 | { |
3d2b39cf L |
986 | /* The debugging sections appear to be recognized only by name, |
987 | not any sort of flag. Their SEC_ALLOC bits are cleared. */ | |
3d2b39cf L |
988 | if (name [0] == '.') |
989 | { | |
f073ced3 AM |
990 | const char *p; |
991 | int n; | |
992 | if (name[1] == 'd') | |
993 | p = ".debug", n = 6; | |
994 | else if (name[1] == 'g' && name[2] == 'n') | |
995 | p = ".gnu.linkonce.wi.", n = 17; | |
996 | else if (name[1] == 'g' && name[2] == 'd') | |
997 | p = ".gdb_index", n = 11; /* yes we really do mean 11. */ | |
998 | else if (name[1] == 'l') | |
999 | p = ".line", n = 5; | |
1000 | else if (name[1] == 's') | |
1001 | p = ".stab", n = 5; | |
1002 | else if (name[1] == 'z') | |
1003 | p = ".zdebug", n = 7; | |
1004 | else | |
1005 | p = NULL, n = 0; | |
1006 | if (p != NULL && strncmp (name, p, n) == 0) | |
3d2b39cf L |
1007 | flags |= SEC_DEBUGGING; |
1008 | } | |
1009 | } | |
252b5132 RH |
1010 | |
1011 | /* As a GNU extension, if the name begins with .gnu.linkonce, we | |
1012 | only link a single copy of the section. This is used to support | |
1013 | g++. g++ will emit each template expansion in its own section. | |
1014 | The symbols will be defined as weak, so that multiple definitions | |
1015 | are permitted. The GNU linker extension is to actually discard | |
1016 | all but one of the sections. */ | |
0112cd26 | 1017 | if (CONST_STRNEQ (name, ".gnu.linkonce") |
b885599b | 1018 | && elf_next_in_group (newsect) == NULL) |
252b5132 RH |
1019 | flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD; |
1020 | ||
fa152c49 JW |
1021 | bed = get_elf_backend_data (abfd); |
1022 | if (bed->elf_backend_section_flags) | |
1023 | if (! bed->elf_backend_section_flags (&flags, hdr)) | |
b34976b6 | 1024 | return FALSE; |
fa152c49 | 1025 | |
252b5132 | 1026 | if (! bfd_set_section_flags (abfd, newsect, flags)) |
b34976b6 | 1027 | return FALSE; |
252b5132 | 1028 | |
718175fa JK |
1029 | /* We do not parse the PT_NOTE segments as we are interested even in the |
1030 | separate debug info files which may have the segments offsets corrupted. | |
1031 | PT_NOTEs from the core files are currently not parsed using BFD. */ | |
1032 | if (hdr->sh_type == SHT_NOTE) | |
1033 | { | |
baea7ef1 | 1034 | bfd_byte *contents; |
718175fa | 1035 | |
baea7ef1 | 1036 | if (!bfd_malloc_and_get_section (abfd, newsect, &contents)) |
718175fa JK |
1037 | return FALSE; |
1038 | ||
baea7ef1 | 1039 | elf_parse_notes (abfd, (char *) contents, hdr->sh_size, -1); |
718175fa JK |
1040 | free (contents); |
1041 | } | |
1042 | ||
252b5132 RH |
1043 | if ((flags & SEC_ALLOC) != 0) |
1044 | { | |
1045 | Elf_Internal_Phdr *phdr; | |
6ffd7900 AM |
1046 | unsigned int i, nload; |
1047 | ||
1048 | /* Some ELF linkers produce binaries with all the program header | |
1049 | p_paddr fields zero. If we have such a binary with more than | |
1050 | one PT_LOAD header, then leave the section lma equal to vma | |
1051 | so that we don't create sections with overlapping lma. */ | |
1052 | phdr = elf_tdata (abfd)->phdr; | |
1053 | for (nload = 0, i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++) | |
1054 | if (phdr->p_paddr != 0) | |
1055 | break; | |
1056 | else if (phdr->p_type == PT_LOAD && phdr->p_memsz != 0) | |
1057 | ++nload; | |
1058 | if (i >= elf_elfheader (abfd)->e_phnum && nload > 1) | |
1059 | return TRUE; | |
252b5132 | 1060 | |
252b5132 RH |
1061 | phdr = elf_tdata (abfd)->phdr; |
1062 | for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++) | |
1063 | { | |
86b2281f AM |
1064 | if (((phdr->p_type == PT_LOAD |
1065 | && (hdr->sh_flags & SHF_TLS) == 0) | |
1066 | || phdr->p_type == PT_TLS) | |
9a83a553 | 1067 | && ELF_SECTION_IN_SEGMENT (hdr, phdr)) |
252b5132 | 1068 | { |
88967714 AM |
1069 | if ((flags & SEC_LOAD) == 0) |
1070 | newsect->lma = (phdr->p_paddr | |
1071 | + hdr->sh_addr - phdr->p_vaddr); | |
1072 | else | |
1073 | /* We used to use the same adjustment for SEC_LOAD | |
1074 | sections, but that doesn't work if the segment | |
1075 | is packed with code from multiple VMAs. | |
1076 | Instead we calculate the section LMA based on | |
1077 | the segment LMA. It is assumed that the | |
1078 | segment will contain sections with contiguous | |
1079 | LMAs, even if the VMAs are not. */ | |
1080 | newsect->lma = (phdr->p_paddr | |
1081 | + hdr->sh_offset - phdr->p_offset); | |
1082 | ||
1083 | /* With contiguous segments, we can't tell from file | |
1084 | offsets whether a section with zero size should | |
1085 | be placed at the end of one segment or the | |
1086 | beginning of the next. Decide based on vaddr. */ | |
1087 | if (hdr->sh_addr >= phdr->p_vaddr | |
1088 | && (hdr->sh_addr + hdr->sh_size | |
1089 | <= phdr->p_vaddr + phdr->p_memsz)) | |
1090 | break; | |
252b5132 RH |
1091 | } |
1092 | } | |
1093 | } | |
1094 | ||
4a114e3e L |
1095 | /* Compress/decompress DWARF debug sections with names: .debug_* and |
1096 | .zdebug_*, after the section flags is set. */ | |
1097 | if ((flags & SEC_DEBUGGING) | |
1098 | && ((name[1] == 'd' && name[6] == '_') | |
1099 | || (name[1] == 'z' && name[7] == '_'))) | |
1100 | { | |
1101 | enum { nothing, compress, decompress } action = nothing; | |
151411f8 | 1102 | int compression_header_size; |
dab394de | 1103 | bfd_size_type uncompressed_size; |
151411f8 L |
1104 | bfd_boolean compressed |
1105 | = bfd_is_section_compressed_with_header (abfd, newsect, | |
dab394de L |
1106 | &compression_header_size, |
1107 | &uncompressed_size); | |
4a114e3e | 1108 | |
151411f8 | 1109 | if (compressed) |
4a114e3e L |
1110 | { |
1111 | /* Compressed section. Check if we should decompress. */ | |
1112 | if ((abfd->flags & BFD_DECOMPRESS)) | |
1113 | action = decompress; | |
1114 | } | |
151411f8 L |
1115 | |
1116 | /* Compress the uncompressed section or convert from/to .zdebug* | |
1117 | section. Check if we should compress. */ | |
1118 | if (action == nothing) | |
4a114e3e | 1119 | { |
151411f8 L |
1120 | if (newsect->size != 0 |
1121 | && (abfd->flags & BFD_COMPRESS) | |
1122 | && compression_header_size >= 0 | |
dab394de | 1123 | && uncompressed_size > 0 |
151411f8 L |
1124 | && (!compressed |
1125 | || ((compression_header_size > 0) | |
1126 | != ((abfd->flags & BFD_COMPRESS_GABI) != 0)))) | |
4a114e3e | 1127 | action = compress; |
151411f8 L |
1128 | else |
1129 | return TRUE; | |
4a114e3e L |
1130 | } |
1131 | ||
151411f8 | 1132 | if (action == compress) |
4a114e3e | 1133 | { |
4a114e3e L |
1134 | if (!bfd_init_section_compress_status (abfd, newsect)) |
1135 | { | |
1136 | (*_bfd_error_handler) | |
bc823199 | 1137 | (_("%B: unable to initialize compress status for section %s"), |
4a114e3e L |
1138 | abfd, name); |
1139 | return FALSE; | |
1140 | } | |
151411f8 L |
1141 | } |
1142 | else | |
1143 | { | |
4a114e3e L |
1144 | if (!bfd_init_section_decompress_status (abfd, newsect)) |
1145 | { | |
1146 | (*_bfd_error_handler) | |
bc823199 | 1147 | (_("%B: unable to initialize decompress status for section %s"), |
4a114e3e L |
1148 | abfd, name); |
1149 | return FALSE; | |
1150 | } | |
151411f8 L |
1151 | } |
1152 | ||
f6fe1ccd | 1153 | if (abfd->is_linker_input) |
151411f8 | 1154 | { |
f6fe1ccd L |
1155 | if (name[1] == 'z' |
1156 | && (action == decompress | |
1157 | || (action == compress | |
1158 | && (abfd->flags & BFD_COMPRESS_GABI) != 0))) | |
4e011fb5 | 1159 | { |
f6fe1ccd L |
1160 | /* Convert section name from .zdebug_* to .debug_* so |
1161 | that linker will consider this section as a debug | |
1162 | section. */ | |
1163 | char *new_name = convert_zdebug_to_debug (abfd, name); | |
151411f8 L |
1164 | if (new_name == NULL) |
1165 | return FALSE; | |
f6fe1ccd | 1166 | bfd_rename_section (abfd, newsect, new_name); |
151411f8 | 1167 | } |
4a114e3e | 1168 | } |
f6fe1ccd L |
1169 | else |
1170 | /* For objdump, don't rename the section. For objcopy, delay | |
1171 | section rename to elf_fake_sections. */ | |
1172 | newsect->flags |= SEC_ELF_RENAME; | |
4a114e3e L |
1173 | } |
1174 | ||
b34976b6 | 1175 | return TRUE; |
252b5132 RH |
1176 | } |
1177 | ||
84865015 NC |
1178 | const char *const bfd_elf_section_type_names[] = |
1179 | { | |
252b5132 RH |
1180 | "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB", |
1181 | "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE", | |
1182 | "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM", | |
1183 | }; | |
1184 | ||
1049f94e | 1185 | /* ELF relocs are against symbols. If we are producing relocatable |
252b5132 RH |
1186 | output, and the reloc is against an external symbol, and nothing |
1187 | has given us any additional addend, the resulting reloc will also | |
1188 | be against the same symbol. In such a case, we don't want to | |
1189 | change anything about the way the reloc is handled, since it will | |
1190 | all be done at final link time. Rather than put special case code | |
1191 | into bfd_perform_relocation, all the reloc types use this howto | |
1192 | function. It just short circuits the reloc if producing | |
1049f94e | 1193 | relocatable output against an external symbol. */ |
252b5132 | 1194 | |
252b5132 | 1195 | bfd_reloc_status_type |
217aa764 AM |
1196 | bfd_elf_generic_reloc (bfd *abfd ATTRIBUTE_UNUSED, |
1197 | arelent *reloc_entry, | |
1198 | asymbol *symbol, | |
1199 | void *data ATTRIBUTE_UNUSED, | |
1200 | asection *input_section, | |
1201 | bfd *output_bfd, | |
1202 | char **error_message ATTRIBUTE_UNUSED) | |
1203 | { | |
1204 | if (output_bfd != NULL | |
252b5132 RH |
1205 | && (symbol->flags & BSF_SECTION_SYM) == 0 |
1206 | && (! reloc_entry->howto->partial_inplace | |
1207 | || reloc_entry->addend == 0)) | |
1208 | { | |
1209 | reloc_entry->address += input_section->output_offset; | |
1210 | return bfd_reloc_ok; | |
1211 | } | |
1212 | ||
1213 | return bfd_reloc_continue; | |
1214 | } | |
1215 | \f | |
84865015 NC |
1216 | /* Returns TRUE if section A matches section B. |
1217 | Names, addresses and links may be different, but everything else | |
1218 | should be the same. */ | |
1219 | ||
1220 | static bfd_boolean | |
1221 | section_match (Elf_Internal_Shdr * a, Elf_Internal_Shdr * b) | |
1222 | { | |
1223 | return | |
1224 | a->sh_type == b->sh_type | |
1225 | && a->sh_flags == b->sh_flags | |
1226 | && a->sh_addralign == b->sh_addralign | |
1227 | && a->sh_size == b->sh_size | |
1228 | && a->sh_entsize == b->sh_entsize | |
1229 | /* FIXME: Check sh_addr ? */ | |
1230 | ; | |
1231 | } | |
1232 | ||
1233 | /* Find a section in OBFD that has the same characteristics | |
1234 | as IHEADER. Return the index of this section or SHN_UNDEF if | |
1235 | none can be found. Check's section HINT first, as this is likely | |
1236 | to be the correct section. */ | |
1237 | ||
1238 | static unsigned int | |
1239 | find_link (bfd * obfd, Elf_Internal_Shdr * iheader, unsigned int hint) | |
1240 | { | |
1241 | Elf_Internal_Shdr ** oheaders = elf_elfsections (obfd); | |
1242 | unsigned int i; | |
1243 | ||
1244 | if (section_match (oheaders[hint], iheader)) | |
1245 | return hint; | |
1246 | ||
1247 | for (i = 1; i < elf_numsections (obfd); i++) | |
1248 | { | |
1249 | Elf_Internal_Shdr * oheader = oheaders[i]; | |
1250 | ||
1251 | if (section_match (oheader, iheader)) | |
1252 | /* FIXME: Do we care if there is a potential for | |
1253 | multiple matches ? */ | |
1254 | return i; | |
1255 | } | |
1256 | ||
1257 | return SHN_UNDEF; | |
1258 | } | |
1259 | ||
0ac4564e L |
1260 | /* Copy the program header and other data from one object module to |
1261 | another. */ | |
252b5132 | 1262 | |
b34976b6 | 1263 | bfd_boolean |
217aa764 | 1264 | _bfd_elf_copy_private_bfd_data (bfd *ibfd, bfd *obfd) |
2d502050 | 1265 | { |
84865015 NC |
1266 | Elf_Internal_Shdr ** iheaders = elf_elfsections (ibfd); |
1267 | Elf_Internal_Shdr ** oheaders = elf_elfsections (obfd); | |
1268 | unsigned int i; | |
1269 | ||
2d502050 | 1270 | if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour |
84865015 | 1271 | || bfd_get_flavour (obfd) != bfd_target_elf_flavour) |
b34976b6 | 1272 | return TRUE; |
2d502050 | 1273 | |
57b828ef L |
1274 | if (!elf_flags_init (obfd)) |
1275 | { | |
1276 | elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags; | |
1277 | elf_flags_init (obfd) = TRUE; | |
1278 | } | |
2d502050 | 1279 | |
0ac4564e | 1280 | elf_gp (obfd) = elf_gp (ibfd); |
57b828ef L |
1281 | |
1282 | /* Also copy the EI_OSABI field. */ | |
1283 | elf_elfheader (obfd)->e_ident[EI_OSABI] = | |
1284 | elf_elfheader (ibfd)->e_ident[EI_OSABI]; | |
104d59d1 JM |
1285 | |
1286 | /* Copy object attributes. */ | |
1287 | _bfd_elf_copy_obj_attributes (ibfd, obfd); | |
63b9bbb7 | 1288 | |
84865015 NC |
1289 | if (iheaders == NULL || oheaders == NULL) |
1290 | return TRUE; | |
63b9bbb7 | 1291 | |
84865015 NC |
1292 | /* Possibly copy the sh_info and sh_link fields. */ |
1293 | for (i = 1; i < elf_numsections (obfd); i++) | |
63b9bbb7 | 1294 | { |
84865015 NC |
1295 | unsigned int j; |
1296 | Elf_Internal_Shdr * oheader = oheaders[i]; | |
63b9bbb7 | 1297 | |
84865015 NC |
1298 | if (oheader == NULL |
1299 | || (oheader->sh_type != SHT_NOBITS | |
1300 | && oheader->sh_type < SHT_LOOS) | |
1301 | || oheader->sh_size == 0 | |
1302 | || (oheader->sh_info != 0 && oheader->sh_link != 0)) | |
1303 | continue; | |
63b9bbb7 | 1304 | |
84865015 NC |
1305 | /* Scan for the matching section in the input bfd. |
1306 | FIXME: We could use something better than a linear scan here. | |
1307 | Unfortunately we cannot compare names as the output string table | |
1308 | is empty, so instead we check size, address and type. */ | |
1309 | for (j = 1; j < elf_numsections (ibfd); j++) | |
1310 | { | |
1311 | Elf_Internal_Shdr * iheader = iheaders[j]; | |
1312 | ||
1313 | /* Since --only-keep-debug turns all non-debug sections into | |
1314 | SHT_NOBITS sections, the output SHT_NOBITS type matches any | |
1315 | input type. */ | |
1316 | if ((oheader->sh_type == SHT_NOBITS | |
1317 | || iheader->sh_type == oheader->sh_type) | |
1318 | && iheader->sh_flags == oheader->sh_flags | |
1319 | && iheader->sh_addralign == oheader->sh_addralign | |
1320 | && iheader->sh_entsize == oheader->sh_entsize | |
1321 | && iheader->sh_size == oheader->sh_size | |
1322 | && iheader->sh_addr == oheader->sh_addr | |
1323 | && (iheader->sh_info != oheader->sh_info | |
1324 | || iheader->sh_link != oheader->sh_link)) | |
63b9bbb7 | 1325 | { |
84865015 NC |
1326 | /* PR 19938: Attempt to preserve the sh_link and sh_info fields |
1327 | of OS and Processor specific sections. We try harder for | |
1328 | these sections, because this is not just about matching | |
1329 | stripped binaries to their originals. */ | |
1330 | if (oheader->sh_type >= SHT_LOOS) | |
63b9bbb7 | 1331 | { |
84865015 NC |
1332 | const struct elf_backend_data *bed = get_elf_backend_data (obfd); |
1333 | bfd_boolean changed = FALSE; | |
7634c4e6 | 1334 | unsigned int sh_link; |
84865015 NC |
1335 | |
1336 | /* Allow the target a chance to decide how these fields should | |
1337 | be set. */ | |
1338 | if (bed->elf_backend_set_special_section_info_and_link != NULL | |
1339 | && bed->elf_backend_set_special_section_info_and_link | |
1340 | (ibfd, obfd, iheader, oheader)) | |
1341 | break; | |
1342 | ||
1343 | /* We have iheader which matches oheader, but which has | |
1344 | non-zero sh_info and/or sh_link fields. Attempt to | |
1345 | follow those links and find the section in the output | |
1346 | bfd which corresponds to the linked section in the input | |
1347 | bfd. */ | |
1348 | if (iheader->sh_link != SHN_UNDEF) | |
1349 | { | |
7634c4e6 L |
1350 | sh_link = find_link (obfd, |
1351 | iheaders[iheader->sh_link], | |
1352 | iheader->sh_link); | |
1353 | if (sh_link != SHN_UNDEF) | |
84865015 | 1354 | { |
7634c4e6 | 1355 | oheader->sh_link = sh_link; |
84865015 NC |
1356 | changed = TRUE; |
1357 | } | |
1358 | else | |
1359 | /* FIXME: Should we install iheader->sh_link | |
1360 | if we could not find a match ? */ | |
1361 | (* _bfd_error_handler) | |
1362 | (_("%B: Failed to find link section for section %d"), | |
1363 | obfd, i); | |
1364 | } | |
1365 | ||
1366 | if (iheader->sh_info) | |
1367 | { | |
1368 | /* The sh_info field can hold arbitrary information, | |
1369 | but if the SHF_LINK_INFO flag is set then it | |
1370 | should be interpreted as a section index. */ | |
1371 | if (iheader->sh_flags & SHF_INFO_LINK) | |
7634c4e6 L |
1372 | sh_link = find_link (obfd, |
1373 | iheaders[iheader->sh_info], | |
1374 | iheader->sh_info); | |
84865015 NC |
1375 | else |
1376 | /* No idea what it means - just copy it. */ | |
7634c4e6 | 1377 | sh_link = iheader->sh_info; |
84865015 | 1378 | |
7634c4e6 | 1379 | if (sh_link != SHN_UNDEF) |
84865015 | 1380 | { |
7634c4e6 | 1381 | oheader->sh_info = sh_link; |
84865015 NC |
1382 | changed = TRUE; |
1383 | } | |
1384 | else | |
1385 | (* _bfd_error_handler) | |
1386 | (_("%B: Failed to find info section for section %d"), | |
1387 | obfd, i); | |
1388 | } | |
1389 | ||
1390 | if (changed) | |
1391 | break; | |
1392 | } | |
1393 | else | |
1394 | { | |
1395 | /* This is an feature for objcopy --only-keep-debug: | |
1396 | When a section's type is changed to NOBITS, we preserve | |
1397 | the sh_link and sh_info fields so that they can be | |
1398 | matched up with the original. | |
1399 | ||
1400 | Note: Strictly speaking these assignments are wrong. | |
63b9bbb7 NC |
1401 | The sh_link and sh_info fields should point to the |
1402 | relevent sections in the output BFD, which may not be in | |
84865015 NC |
1403 | the same location as they were in the input BFD. But |
1404 | the whole point of this action is to preserve the | |
1405 | original values of the sh_link and sh_info fields, so | |
1406 | that they can be matched up with the section headers in | |
1407 | the original file. So strictly speaking we may be | |
1408 | creating an invalid ELF file, but it is only for a file | |
1409 | that just contains debug info and only for sections | |
1410 | without any contents. */ | |
63b9bbb7 NC |
1411 | if (oheader->sh_link == 0) |
1412 | oheader->sh_link = iheader->sh_link; | |
1413 | if (oheader->sh_info == 0) | |
1414 | oheader->sh_info = iheader->sh_info; | |
1415 | break; | |
1416 | } | |
1417 | } | |
1418 | } | |
1419 | } | |
1420 | ||
b34976b6 | 1421 | return TRUE; |
2d502050 L |
1422 | } |
1423 | ||
cedc298e L |
1424 | static const char * |
1425 | get_segment_type (unsigned int p_type) | |
1426 | { | |
1427 | const char *pt; | |
1428 | switch (p_type) | |
1429 | { | |
1430 | case PT_NULL: pt = "NULL"; break; | |
1431 | case PT_LOAD: pt = "LOAD"; break; | |
1432 | case PT_DYNAMIC: pt = "DYNAMIC"; break; | |
1433 | case PT_INTERP: pt = "INTERP"; break; | |
1434 | case PT_NOTE: pt = "NOTE"; break; | |
1435 | case PT_SHLIB: pt = "SHLIB"; break; | |
1436 | case PT_PHDR: pt = "PHDR"; break; | |
1437 | case PT_TLS: pt = "TLS"; break; | |
1438 | case PT_GNU_EH_FRAME: pt = "EH_FRAME"; break; | |
2b05f1b7 | 1439 | case PT_GNU_STACK: pt = "STACK"; break; |
cedc298e L |
1440 | case PT_GNU_RELRO: pt = "RELRO"; break; |
1441 | default: pt = NULL; break; | |
1442 | } | |
1443 | return pt; | |
1444 | } | |
1445 | ||
f0b79d91 L |
1446 | /* Print out the program headers. */ |
1447 | ||
b34976b6 | 1448 | bfd_boolean |
217aa764 | 1449 | _bfd_elf_print_private_bfd_data (bfd *abfd, void *farg) |
252b5132 | 1450 | { |
a50b1753 | 1451 | FILE *f = (FILE *) farg; |
252b5132 RH |
1452 | Elf_Internal_Phdr *p; |
1453 | asection *s; | |
1454 | bfd_byte *dynbuf = NULL; | |
1455 | ||
1456 | p = elf_tdata (abfd)->phdr; | |
1457 | if (p != NULL) | |
1458 | { | |
1459 | unsigned int i, c; | |
1460 | ||
1461 | fprintf (f, _("\nProgram Header:\n")); | |
1462 | c = elf_elfheader (abfd)->e_phnum; | |
1463 | for (i = 0; i < c; i++, p++) | |
1464 | { | |
cedc298e | 1465 | const char *pt = get_segment_type (p->p_type); |
252b5132 RH |
1466 | char buf[20]; |
1467 | ||
cedc298e | 1468 | if (pt == NULL) |
252b5132 | 1469 | { |
cedc298e L |
1470 | sprintf (buf, "0x%lx", p->p_type); |
1471 | pt = buf; | |
252b5132 | 1472 | } |
dc810e39 | 1473 | fprintf (f, "%8s off 0x", pt); |
60b89a18 | 1474 | bfd_fprintf_vma (abfd, f, p->p_offset); |
252b5132 | 1475 | fprintf (f, " vaddr 0x"); |
60b89a18 | 1476 | bfd_fprintf_vma (abfd, f, p->p_vaddr); |
252b5132 | 1477 | fprintf (f, " paddr 0x"); |
60b89a18 | 1478 | bfd_fprintf_vma (abfd, f, p->p_paddr); |
252b5132 RH |
1479 | fprintf (f, " align 2**%u\n", bfd_log2 (p->p_align)); |
1480 | fprintf (f, " filesz 0x"); | |
60b89a18 | 1481 | bfd_fprintf_vma (abfd, f, p->p_filesz); |
252b5132 | 1482 | fprintf (f, " memsz 0x"); |
60b89a18 | 1483 | bfd_fprintf_vma (abfd, f, p->p_memsz); |
252b5132 RH |
1484 | fprintf (f, " flags %c%c%c", |
1485 | (p->p_flags & PF_R) != 0 ? 'r' : '-', | |
1486 | (p->p_flags & PF_W) != 0 ? 'w' : '-', | |
1487 | (p->p_flags & PF_X) != 0 ? 'x' : '-'); | |
dc810e39 AM |
1488 | if ((p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X)) != 0) |
1489 | fprintf (f, " %lx", p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X)); | |
252b5132 RH |
1490 | fprintf (f, "\n"); |
1491 | } | |
1492 | } | |
1493 | ||
1494 | s = bfd_get_section_by_name (abfd, ".dynamic"); | |
1495 | if (s != NULL) | |
1496 | { | |
cb33740c | 1497 | unsigned int elfsec; |
dc810e39 | 1498 | unsigned long shlink; |
252b5132 RH |
1499 | bfd_byte *extdyn, *extdynend; |
1500 | size_t extdynsize; | |
217aa764 | 1501 | void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *); |
252b5132 RH |
1502 | |
1503 | fprintf (f, _("\nDynamic Section:\n")); | |
1504 | ||
eea6121a | 1505 | if (!bfd_malloc_and_get_section (abfd, s, &dynbuf)) |
252b5132 RH |
1506 | goto error_return; |
1507 | ||
1508 | elfsec = _bfd_elf_section_from_bfd_section (abfd, s); | |
cb33740c | 1509 | if (elfsec == SHN_BAD) |
252b5132 | 1510 | goto error_return; |
dc810e39 | 1511 | shlink = elf_elfsections (abfd)[elfsec]->sh_link; |
252b5132 RH |
1512 | |
1513 | extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn; | |
1514 | swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in; | |
1515 | ||
1516 | extdyn = dynbuf; | |
06614111 NC |
1517 | /* PR 17512: file: 6f427532. */ |
1518 | if (s->size < extdynsize) | |
1519 | goto error_return; | |
eea6121a | 1520 | extdynend = extdyn + s->size; |
1036838a NC |
1521 | /* PR 17512: file: id:000006,sig:06,src:000000,op:flip4,pos:5664. |
1522 | Fix range check. */ | |
1523 | for (; extdyn <= (extdynend - extdynsize); extdyn += extdynsize) | |
252b5132 RH |
1524 | { |
1525 | Elf_Internal_Dyn dyn; | |
ad9563d6 | 1526 | const char *name = ""; |
252b5132 | 1527 | char ab[20]; |
b34976b6 | 1528 | bfd_boolean stringp; |
ad9563d6 | 1529 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
252b5132 | 1530 | |
217aa764 | 1531 | (*swap_dyn_in) (abfd, extdyn, &dyn); |
252b5132 RH |
1532 | |
1533 | if (dyn.d_tag == DT_NULL) | |
1534 | break; | |
1535 | ||
b34976b6 | 1536 | stringp = FALSE; |
252b5132 RH |
1537 | switch (dyn.d_tag) |
1538 | { | |
1539 | default: | |
ad9563d6 CM |
1540 | if (bed->elf_backend_get_target_dtag) |
1541 | name = (*bed->elf_backend_get_target_dtag) (dyn.d_tag); | |
1542 | ||
1543 | if (!strcmp (name, "")) | |
1544 | { | |
1545 | sprintf (ab, "0x%lx", (unsigned long) dyn.d_tag); | |
1546 | name = ab; | |
1547 | } | |
252b5132 RH |
1548 | break; |
1549 | ||
b34976b6 | 1550 | case DT_NEEDED: name = "NEEDED"; stringp = TRUE; break; |
252b5132 RH |
1551 | case DT_PLTRELSZ: name = "PLTRELSZ"; break; |
1552 | case DT_PLTGOT: name = "PLTGOT"; break; | |
1553 | case DT_HASH: name = "HASH"; break; | |
1554 | case DT_STRTAB: name = "STRTAB"; break; | |
1555 | case DT_SYMTAB: name = "SYMTAB"; break; | |
1556 | case DT_RELA: name = "RELA"; break; | |
1557 | case DT_RELASZ: name = "RELASZ"; break; | |
1558 | case DT_RELAENT: name = "RELAENT"; break; | |
1559 | case DT_STRSZ: name = "STRSZ"; break; | |
1560 | case DT_SYMENT: name = "SYMENT"; break; | |
1561 | case DT_INIT: name = "INIT"; break; | |
1562 | case DT_FINI: name = "FINI"; break; | |
b34976b6 AM |
1563 | case DT_SONAME: name = "SONAME"; stringp = TRUE; break; |
1564 | case DT_RPATH: name = "RPATH"; stringp = TRUE; break; | |
252b5132 RH |
1565 | case DT_SYMBOLIC: name = "SYMBOLIC"; break; |
1566 | case DT_REL: name = "REL"; break; | |
1567 | case DT_RELSZ: name = "RELSZ"; break; | |
1568 | case DT_RELENT: name = "RELENT"; break; | |
1569 | case DT_PLTREL: name = "PLTREL"; break; | |
1570 | case DT_DEBUG: name = "DEBUG"; break; | |
1571 | case DT_TEXTREL: name = "TEXTREL"; break; | |
1572 | case DT_JMPREL: name = "JMPREL"; break; | |
94558834 L |
1573 | case DT_BIND_NOW: name = "BIND_NOW"; break; |
1574 | case DT_INIT_ARRAY: name = "INIT_ARRAY"; break; | |
1575 | case DT_FINI_ARRAY: name = "FINI_ARRAY"; break; | |
1576 | case DT_INIT_ARRAYSZ: name = "INIT_ARRAYSZ"; break; | |
1577 | case DT_FINI_ARRAYSZ: name = "FINI_ARRAYSZ"; break; | |
b34976b6 | 1578 | case DT_RUNPATH: name = "RUNPATH"; stringp = TRUE; break; |
94558834 L |
1579 | case DT_FLAGS: name = "FLAGS"; break; |
1580 | case DT_PREINIT_ARRAY: name = "PREINIT_ARRAY"; break; | |
1581 | case DT_PREINIT_ARRAYSZ: name = "PREINIT_ARRAYSZ"; break; | |
d48188b9 | 1582 | case DT_CHECKSUM: name = "CHECKSUM"; break; |
94558834 L |
1583 | case DT_PLTPADSZ: name = "PLTPADSZ"; break; |
1584 | case DT_MOVEENT: name = "MOVEENT"; break; | |
1585 | case DT_MOVESZ: name = "MOVESZ"; break; | |
1586 | case DT_FEATURE: name = "FEATURE"; break; | |
1587 | case DT_POSFLAG_1: name = "POSFLAG_1"; break; | |
1588 | case DT_SYMINSZ: name = "SYMINSZ"; break; | |
1589 | case DT_SYMINENT: name = "SYMINENT"; break; | |
b34976b6 AM |
1590 | case DT_CONFIG: name = "CONFIG"; stringp = TRUE; break; |
1591 | case DT_DEPAUDIT: name = "DEPAUDIT"; stringp = TRUE; break; | |
1592 | case DT_AUDIT: name = "AUDIT"; stringp = TRUE; break; | |
94558834 L |
1593 | case DT_PLTPAD: name = "PLTPAD"; break; |
1594 | case DT_MOVETAB: name = "MOVETAB"; break; | |
1595 | case DT_SYMINFO: name = "SYMINFO"; break; | |
1596 | case DT_RELACOUNT: name = "RELACOUNT"; break; | |
1597 | case DT_RELCOUNT: name = "RELCOUNT"; break; | |
1598 | case DT_FLAGS_1: name = "FLAGS_1"; break; | |
252b5132 RH |
1599 | case DT_VERSYM: name = "VERSYM"; break; |
1600 | case DT_VERDEF: name = "VERDEF"; break; | |
1601 | case DT_VERDEFNUM: name = "VERDEFNUM"; break; | |
1602 | case DT_VERNEED: name = "VERNEED"; break; | |
1603 | case DT_VERNEEDNUM: name = "VERNEEDNUM"; break; | |
b34976b6 | 1604 | case DT_AUXILIARY: name = "AUXILIARY"; stringp = TRUE; break; |
94558834 | 1605 | case DT_USED: name = "USED"; break; |
b34976b6 | 1606 | case DT_FILTER: name = "FILTER"; stringp = TRUE; break; |
fdc90cb4 | 1607 | case DT_GNU_HASH: name = "GNU_HASH"; break; |
252b5132 RH |
1608 | } |
1609 | ||
ad9563d6 | 1610 | fprintf (f, " %-20s ", name); |
252b5132 | 1611 | if (! stringp) |
a1f3c56e AN |
1612 | { |
1613 | fprintf (f, "0x"); | |
1614 | bfd_fprintf_vma (abfd, f, dyn.d_un.d_val); | |
1615 | } | |
252b5132 RH |
1616 | else |
1617 | { | |
1618 | const char *string; | |
dc810e39 | 1619 | unsigned int tagv = dyn.d_un.d_val; |
252b5132 | 1620 | |
dc810e39 | 1621 | string = bfd_elf_string_from_elf_section (abfd, shlink, tagv); |
252b5132 RH |
1622 | if (string == NULL) |
1623 | goto error_return; | |
1624 | fprintf (f, "%s", string); | |
1625 | } | |
1626 | fprintf (f, "\n"); | |
1627 | } | |
1628 | ||
1629 | free (dynbuf); | |
1630 | dynbuf = NULL; | |
1631 | } | |
1632 | ||
1633 | if ((elf_dynverdef (abfd) != 0 && elf_tdata (abfd)->verdef == NULL) | |
1634 | || (elf_dynverref (abfd) != 0 && elf_tdata (abfd)->verref == NULL)) | |
1635 | { | |
fc0e6df6 | 1636 | if (! _bfd_elf_slurp_version_tables (abfd, FALSE)) |
b34976b6 | 1637 | return FALSE; |
252b5132 RH |
1638 | } |
1639 | ||
1640 | if (elf_dynverdef (abfd) != 0) | |
1641 | { | |
1642 | Elf_Internal_Verdef *t; | |
1643 | ||
1644 | fprintf (f, _("\nVersion definitions:\n")); | |
1645 | for (t = elf_tdata (abfd)->verdef; t != NULL; t = t->vd_nextdef) | |
1646 | { | |
1647 | fprintf (f, "%d 0x%2.2x 0x%8.8lx %s\n", t->vd_ndx, | |
d0fb9a8d JJ |
1648 | t->vd_flags, t->vd_hash, |
1649 | t->vd_nodename ? t->vd_nodename : "<corrupt>"); | |
1650 | if (t->vd_auxptr != NULL && t->vd_auxptr->vda_nextptr != NULL) | |
252b5132 RH |
1651 | { |
1652 | Elf_Internal_Verdaux *a; | |
1653 | ||
1654 | fprintf (f, "\t"); | |
1655 | for (a = t->vd_auxptr->vda_nextptr; | |
1656 | a != NULL; | |
1657 | a = a->vda_nextptr) | |
d0fb9a8d JJ |
1658 | fprintf (f, "%s ", |
1659 | a->vda_nodename ? a->vda_nodename : "<corrupt>"); | |
252b5132 RH |
1660 | fprintf (f, "\n"); |
1661 | } | |
1662 | } | |
1663 | } | |
1664 | ||
1665 | if (elf_dynverref (abfd) != 0) | |
1666 | { | |
1667 | Elf_Internal_Verneed *t; | |
1668 | ||
1669 | fprintf (f, _("\nVersion References:\n")); | |
1670 | for (t = elf_tdata (abfd)->verref; t != NULL; t = t->vn_nextref) | |
1671 | { | |
1672 | Elf_Internal_Vernaux *a; | |
1673 | ||
d0fb9a8d JJ |
1674 | fprintf (f, _(" required from %s:\n"), |
1675 | t->vn_filename ? t->vn_filename : "<corrupt>"); | |
252b5132 RH |
1676 | for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) |
1677 | fprintf (f, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a->vna_hash, | |
d0fb9a8d JJ |
1678 | a->vna_flags, a->vna_other, |
1679 | a->vna_nodename ? a->vna_nodename : "<corrupt>"); | |
252b5132 RH |
1680 | } |
1681 | } | |
1682 | ||
b34976b6 | 1683 | return TRUE; |
252b5132 RH |
1684 | |
1685 | error_return: | |
1686 | if (dynbuf != NULL) | |
1687 | free (dynbuf); | |
b34976b6 | 1688 | return FALSE; |
252b5132 RH |
1689 | } |
1690 | ||
bb4d2ac2 L |
1691 | /* Get version string. */ |
1692 | ||
1693 | const char * | |
60bb06bc L |
1694 | _bfd_elf_get_symbol_version_string (bfd *abfd, asymbol *symbol, |
1695 | bfd_boolean *hidden) | |
bb4d2ac2 L |
1696 | { |
1697 | const char *version_string = NULL; | |
1698 | if (elf_dynversym (abfd) != 0 | |
1699 | && (elf_dynverdef (abfd) != 0 || elf_dynverref (abfd) != 0)) | |
1700 | { | |
1701 | unsigned int vernum = ((elf_symbol_type *) symbol)->version; | |
1702 | ||
1703 | *hidden = (vernum & VERSYM_HIDDEN) != 0; | |
1704 | vernum &= VERSYM_VERSION; | |
1705 | ||
1706 | if (vernum == 0) | |
1707 | version_string = ""; | |
1708 | else if (vernum == 1) | |
1709 | version_string = "Base"; | |
1710 | else if (vernum <= elf_tdata (abfd)->cverdefs) | |
1711 | version_string = | |
1712 | elf_tdata (abfd)->verdef[vernum - 1].vd_nodename; | |
1713 | else | |
1714 | { | |
1715 | Elf_Internal_Verneed *t; | |
1716 | ||
1717 | version_string = ""; | |
1718 | for (t = elf_tdata (abfd)->verref; | |
1719 | t != NULL; | |
1720 | t = t->vn_nextref) | |
1721 | { | |
1722 | Elf_Internal_Vernaux *a; | |
1723 | ||
1724 | for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) | |
1725 | { | |
1726 | if (a->vna_other == vernum) | |
1727 | { | |
1728 | version_string = a->vna_nodename; | |
1729 | break; | |
1730 | } | |
1731 | } | |
1732 | } | |
1733 | } | |
1734 | } | |
1735 | return version_string; | |
1736 | } | |
1737 | ||
252b5132 RH |
1738 | /* Display ELF-specific fields of a symbol. */ |
1739 | ||
1740 | void | |
217aa764 AM |
1741 | bfd_elf_print_symbol (bfd *abfd, |
1742 | void *filep, | |
1743 | asymbol *symbol, | |
1744 | bfd_print_symbol_type how) | |
252b5132 | 1745 | { |
a50b1753 | 1746 | FILE *file = (FILE *) filep; |
252b5132 RH |
1747 | switch (how) |
1748 | { | |
1749 | case bfd_print_symbol_name: | |
1750 | fprintf (file, "%s", symbol->name); | |
1751 | break; | |
1752 | case bfd_print_symbol_more: | |
1753 | fprintf (file, "elf "); | |
60b89a18 | 1754 | bfd_fprintf_vma (abfd, file, symbol->value); |
0af1713e | 1755 | fprintf (file, " %lx", (unsigned long) symbol->flags); |
252b5132 RH |
1756 | break; |
1757 | case bfd_print_symbol_all: | |
1758 | { | |
4e8a9624 AM |
1759 | const char *section_name; |
1760 | const char *name = NULL; | |
9c5bfbb7 | 1761 | const struct elf_backend_data *bed; |
7a13edea | 1762 | unsigned char st_other; |
dbb410c3 | 1763 | bfd_vma val; |
bb4d2ac2 L |
1764 | const char *version_string; |
1765 | bfd_boolean hidden; | |
c044fabd | 1766 | |
252b5132 | 1767 | section_name = symbol->section ? symbol->section->name : "(*none*)"; |
587ff49e RH |
1768 | |
1769 | bed = get_elf_backend_data (abfd); | |
1770 | if (bed->elf_backend_print_symbol_all) | |
c044fabd | 1771 | name = (*bed->elf_backend_print_symbol_all) (abfd, filep, symbol); |
587ff49e RH |
1772 | |
1773 | if (name == NULL) | |
1774 | { | |
7ee38065 | 1775 | name = symbol->name; |
217aa764 | 1776 | bfd_print_symbol_vandf (abfd, file, symbol); |
587ff49e RH |
1777 | } |
1778 | ||
252b5132 RH |
1779 | fprintf (file, " %s\t", section_name); |
1780 | /* Print the "other" value for a symbol. For common symbols, | |
1781 | we've already printed the size; now print the alignment. | |
1782 | For other symbols, we have no specified alignment, and | |
1783 | we've printed the address; now print the size. */ | |
dcf6c779 | 1784 | if (symbol->section && bfd_is_com_section (symbol->section)) |
dbb410c3 AM |
1785 | val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_value; |
1786 | else | |
1787 | val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_size; | |
1788 | bfd_fprintf_vma (abfd, file, val); | |
252b5132 RH |
1789 | |
1790 | /* If we have version information, print it. */ | |
60bb06bc L |
1791 | version_string = _bfd_elf_get_symbol_version_string (abfd, |
1792 | symbol, | |
1793 | &hidden); | |
bb4d2ac2 | 1794 | if (version_string) |
252b5132 | 1795 | { |
bb4d2ac2 | 1796 | if (!hidden) |
252b5132 RH |
1797 | fprintf (file, " %-11s", version_string); |
1798 | else | |
1799 | { | |
1800 | int i; | |
1801 | ||
1802 | fprintf (file, " (%s)", version_string); | |
1803 | for (i = 10 - strlen (version_string); i > 0; --i) | |
1804 | putc (' ', file); | |
1805 | } | |
1806 | } | |
1807 | ||
1808 | /* If the st_other field is not zero, print it. */ | |
7a13edea | 1809 | st_other = ((elf_symbol_type *) symbol)->internal_elf_sym.st_other; |
c044fabd | 1810 | |
7a13edea NC |
1811 | switch (st_other) |
1812 | { | |
1813 | case 0: break; | |
1814 | case STV_INTERNAL: fprintf (file, " .internal"); break; | |
1815 | case STV_HIDDEN: fprintf (file, " .hidden"); break; | |
1816 | case STV_PROTECTED: fprintf (file, " .protected"); break; | |
1817 | default: | |
1818 | /* Some other non-defined flags are also present, so print | |
1819 | everything hex. */ | |
1820 | fprintf (file, " 0x%02x", (unsigned int) st_other); | |
1821 | } | |
252b5132 | 1822 | |
587ff49e | 1823 | fprintf (file, " %s", name); |
252b5132 RH |
1824 | } |
1825 | break; | |
1826 | } | |
1827 | } | |
252b5132 RH |
1828 | \f |
1829 | /* ELF .o/exec file reading */ | |
1830 | ||
c044fabd | 1831 | /* Create a new bfd section from an ELF section header. */ |
252b5132 | 1832 | |
b34976b6 | 1833 | bfd_boolean |
217aa764 | 1834 | bfd_section_from_shdr (bfd *abfd, unsigned int shindex) |
252b5132 | 1835 | { |
4fbb74a6 AM |
1836 | Elf_Internal_Shdr *hdr; |
1837 | Elf_Internal_Ehdr *ehdr; | |
1838 | const struct elf_backend_data *bed; | |
90937f86 | 1839 | const char *name; |
bf67003b NC |
1840 | bfd_boolean ret = TRUE; |
1841 | static bfd_boolean * sections_being_created = NULL; | |
5a4b0ccc | 1842 | static bfd * sections_being_created_abfd = NULL; |
bf67003b | 1843 | static unsigned int nesting = 0; |
252b5132 | 1844 | |
4fbb74a6 AM |
1845 | if (shindex >= elf_numsections (abfd)) |
1846 | return FALSE; | |
1847 | ||
bf67003b NC |
1848 | if (++ nesting > 3) |
1849 | { | |
1850 | /* PR17512: A corrupt ELF binary might contain a recursive group of | |
06614111 | 1851 | sections, with each the string indicies pointing to the next in the |
bf67003b NC |
1852 | loop. Detect this here, by refusing to load a section that we are |
1853 | already in the process of loading. We only trigger this test if | |
1854 | we have nested at least three sections deep as normal ELF binaries | |
5a4b0ccc NC |
1855 | can expect to recurse at least once. |
1856 | ||
1857 | FIXME: It would be better if this array was attached to the bfd, | |
1858 | rather than being held in a static pointer. */ | |
1859 | ||
1860 | if (sections_being_created_abfd != abfd) | |
1861 | sections_being_created = NULL; | |
bf67003b NC |
1862 | if (sections_being_created == NULL) |
1863 | { | |
1864 | /* FIXME: It would be more efficient to attach this array to the bfd somehow. */ | |
1865 | sections_being_created = (bfd_boolean *) | |
1866 | bfd_zalloc (abfd, elf_numsections (abfd) * sizeof (bfd_boolean)); | |
5a4b0ccc | 1867 | sections_being_created_abfd = abfd; |
bf67003b NC |
1868 | } |
1869 | if (sections_being_created [shindex]) | |
1870 | { | |
1871 | (*_bfd_error_handler) | |
1872 | (_("%B: warning: loop in section dependencies detected"), abfd); | |
1873 | return FALSE; | |
1874 | } | |
1875 | sections_being_created [shindex] = TRUE; | |
1876 | } | |
1877 | ||
4fbb74a6 AM |
1878 | hdr = elf_elfsections (abfd)[shindex]; |
1879 | ehdr = elf_elfheader (abfd); | |
1880 | name = bfd_elf_string_from_elf_section (abfd, ehdr->e_shstrndx, | |
1b3a8575 | 1881 | hdr->sh_name); |
933d961a | 1882 | if (name == NULL) |
bf67003b | 1883 | goto fail; |
252b5132 | 1884 | |
4fbb74a6 | 1885 | bed = get_elf_backend_data (abfd); |
252b5132 RH |
1886 | switch (hdr->sh_type) |
1887 | { | |
1888 | case SHT_NULL: | |
1889 | /* Inactive section. Throw it away. */ | |
bf67003b | 1890 | goto success; |
252b5132 | 1891 | |
bf67003b NC |
1892 | case SHT_PROGBITS: /* Normal section with contents. */ |
1893 | case SHT_NOBITS: /* .bss section. */ | |
1894 | case SHT_HASH: /* .hash section. */ | |
1895 | case SHT_NOTE: /* .note section. */ | |
25e27870 L |
1896 | case SHT_INIT_ARRAY: /* .init_array section. */ |
1897 | case SHT_FINI_ARRAY: /* .fini_array section. */ | |
1898 | case SHT_PREINIT_ARRAY: /* .preinit_array section. */ | |
7f1204bb | 1899 | case SHT_GNU_LIBLIST: /* .gnu.liblist section. */ |
fdc90cb4 | 1900 | case SHT_GNU_HASH: /* .gnu.hash section. */ |
bf67003b NC |
1901 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); |
1902 | goto success; | |
252b5132 | 1903 | |
797fc050 | 1904 | case SHT_DYNAMIC: /* Dynamic linking information. */ |
6dc132d9 | 1905 | if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex)) |
bf67003b NC |
1906 | goto fail; |
1907 | ||
cfcac11d NC |
1908 | if (hdr->sh_link > elf_numsections (abfd)) |
1909 | { | |
caa83f8b | 1910 | /* PR 10478: Accept Solaris binaries with a sh_link |
cfcac11d NC |
1911 | field set to SHN_BEFORE or SHN_AFTER. */ |
1912 | switch (bfd_get_arch (abfd)) | |
1913 | { | |
caa83f8b | 1914 | case bfd_arch_i386: |
cfcac11d NC |
1915 | case bfd_arch_sparc: |
1916 | if (hdr->sh_link == (SHN_LORESERVE & 0xffff) /* SHN_BEFORE */ | |
1917 | || hdr->sh_link == ((SHN_LORESERVE + 1) & 0xffff) /* SHN_AFTER */) | |
1918 | break; | |
1919 | /* Otherwise fall through. */ | |
1920 | default: | |
bf67003b | 1921 | goto fail; |
cfcac11d NC |
1922 | } |
1923 | } | |
1924 | else if (elf_elfsections (abfd)[hdr->sh_link] == NULL) | |
bf67003b | 1925 | goto fail; |
cfcac11d | 1926 | else if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_STRTAB) |
797fc050 AM |
1927 | { |
1928 | Elf_Internal_Shdr *dynsymhdr; | |
1929 | ||
1930 | /* The shared libraries distributed with hpux11 have a bogus | |
1931 | sh_link field for the ".dynamic" section. Find the | |
1932 | string table for the ".dynsym" section instead. */ | |
1933 | if (elf_dynsymtab (abfd) != 0) | |
1934 | { | |
1935 | dynsymhdr = elf_elfsections (abfd)[elf_dynsymtab (abfd)]; | |
1936 | hdr->sh_link = dynsymhdr->sh_link; | |
1937 | } | |
1938 | else | |
1939 | { | |
1940 | unsigned int i, num_sec; | |
1941 | ||
1942 | num_sec = elf_numsections (abfd); | |
1943 | for (i = 1; i < num_sec; i++) | |
1944 | { | |
1945 | dynsymhdr = elf_elfsections (abfd)[i]; | |
1946 | if (dynsymhdr->sh_type == SHT_DYNSYM) | |
1947 | { | |
1948 | hdr->sh_link = dynsymhdr->sh_link; | |
1949 | break; | |
1950 | } | |
1951 | } | |
1952 | } | |
1953 | } | |
bf67003b | 1954 | goto success; |
797fc050 | 1955 | |
bf67003b | 1956 | case SHT_SYMTAB: /* A symbol table. */ |
252b5132 | 1957 | if (elf_onesymtab (abfd) == shindex) |
bf67003b | 1958 | goto success; |
252b5132 | 1959 | |
a50b2160 | 1960 | if (hdr->sh_entsize != bed->s->sizeof_sym) |
bf67003b NC |
1961 | goto fail; |
1962 | ||
3337c1e5 | 1963 | if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size) |
eee3b786 AM |
1964 | { |
1965 | if (hdr->sh_size != 0) | |
bf67003b | 1966 | goto fail; |
eee3b786 AM |
1967 | /* Some assemblers erroneously set sh_info to one with a |
1968 | zero sh_size. ld sees this as a global symbol count | |
1969 | of (unsigned) -1. Fix it here. */ | |
1970 | hdr->sh_info = 0; | |
bf67003b | 1971 | goto success; |
eee3b786 | 1972 | } |
bf67003b | 1973 | |
16ad13ec NC |
1974 | /* PR 18854: A binary might contain more than one symbol table. |
1975 | Unusual, but possible. Warn, but continue. */ | |
1976 | if (elf_onesymtab (abfd) != 0) | |
1977 | { | |
1978 | (*_bfd_error_handler) | |
1979 | (_("%B: warning: multiple symbol tables detected - ignoring the table in section %u"), | |
1980 | abfd, shindex); | |
1981 | goto success; | |
1982 | } | |
252b5132 | 1983 | elf_onesymtab (abfd) = shindex; |
6a40cf0c NC |
1984 | elf_symtab_hdr (abfd) = *hdr; |
1985 | elf_elfsections (abfd)[shindex] = hdr = & elf_symtab_hdr (abfd); | |
252b5132 RH |
1986 | abfd->flags |= HAS_SYMS; |
1987 | ||
1988 | /* Sometimes a shared object will map in the symbol table. If | |
08a40648 AM |
1989 | SHF_ALLOC is set, and this is a shared object, then we also |
1990 | treat this section as a BFD section. We can not base the | |
1991 | decision purely on SHF_ALLOC, because that flag is sometimes | |
1992 | set in a relocatable object file, which would confuse the | |
1993 | linker. */ | |
252b5132 RH |
1994 | if ((hdr->sh_flags & SHF_ALLOC) != 0 |
1995 | && (abfd->flags & DYNAMIC) != 0 | |
6dc132d9 L |
1996 | && ! _bfd_elf_make_section_from_shdr (abfd, hdr, name, |
1997 | shindex)) | |
bf67003b | 1998 | goto fail; |
252b5132 | 1999 | |
1b3a8575 AM |
2000 | /* Go looking for SHT_SYMTAB_SHNDX too, since if there is one we |
2001 | can't read symbols without that section loaded as well. It | |
2002 | is most likely specified by the next section header. */ | |
6a40cf0c NC |
2003 | { |
2004 | elf_section_list * entry; | |
2005 | unsigned int i, num_sec; | |
1b3a8575 | 2006 | |
6a40cf0c NC |
2007 | for (entry = elf_symtab_shndx_list (abfd); entry != NULL; entry = entry->next) |
2008 | if (entry->hdr.sh_link == shindex) | |
2009 | goto success; | |
2010 | ||
2011 | num_sec = elf_numsections (abfd); | |
2012 | for (i = shindex + 1; i < num_sec; i++) | |
2013 | { | |
2014 | Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i]; | |
2015 | ||
2016 | if (hdr2->sh_type == SHT_SYMTAB_SHNDX | |
2017 | && hdr2->sh_link == shindex) | |
2018 | break; | |
2019 | } | |
2020 | ||
2021 | if (i == num_sec) | |
2022 | for (i = 1; i < shindex; i++) | |
1b3a8575 AM |
2023 | { |
2024 | Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i]; | |
6a40cf0c | 2025 | |
1b3a8575 AM |
2026 | if (hdr2->sh_type == SHT_SYMTAB_SHNDX |
2027 | && hdr2->sh_link == shindex) | |
2028 | break; | |
2029 | } | |
6a40cf0c NC |
2030 | |
2031 | if (i != shindex) | |
2032 | ret = bfd_section_from_shdr (abfd, i); | |
2033 | /* else FIXME: we have failed to find the symbol table - should we issue an error ? */ | |
2034 | goto success; | |
2035 | } | |
252b5132 | 2036 | |
bf67003b | 2037 | case SHT_DYNSYM: /* A dynamic symbol table. */ |
252b5132 | 2038 | if (elf_dynsymtab (abfd) == shindex) |
bf67003b | 2039 | goto success; |
252b5132 | 2040 | |
a50b2160 | 2041 | if (hdr->sh_entsize != bed->s->sizeof_sym) |
bf67003b NC |
2042 | goto fail; |
2043 | ||
eee3b786 AM |
2044 | if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size) |
2045 | { | |
2046 | if (hdr->sh_size != 0) | |
bf67003b NC |
2047 | goto fail; |
2048 | ||
eee3b786 AM |
2049 | /* Some linkers erroneously set sh_info to one with a |
2050 | zero sh_size. ld sees this as a global symbol count | |
2051 | of (unsigned) -1. Fix it here. */ | |
2052 | hdr->sh_info = 0; | |
bf67003b | 2053 | goto success; |
eee3b786 | 2054 | } |
bf67003b | 2055 | |
16ad13ec NC |
2056 | /* PR 18854: A binary might contain more than one dynamic symbol table. |
2057 | Unusual, but possible. Warn, but continue. */ | |
2058 | if (elf_dynsymtab (abfd) != 0) | |
2059 | { | |
2060 | (*_bfd_error_handler) | |
2061 | (_("%B: warning: multiple dynamic symbol tables detected - ignoring the table in section %u"), | |
2062 | abfd, shindex); | |
2063 | goto success; | |
2064 | } | |
252b5132 RH |
2065 | elf_dynsymtab (abfd) = shindex; |
2066 | elf_tdata (abfd)->dynsymtab_hdr = *hdr; | |
2067 | elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->dynsymtab_hdr; | |
2068 | abfd->flags |= HAS_SYMS; | |
2069 | ||
2070 | /* Besides being a symbol table, we also treat this as a regular | |
2071 | section, so that objcopy can handle it. */ | |
bf67003b NC |
2072 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); |
2073 | goto success; | |
252b5132 | 2074 | |
bf67003b | 2075 | case SHT_SYMTAB_SHNDX: /* Symbol section indices when >64k sections. */ |
6a40cf0c NC |
2076 | { |
2077 | elf_section_list * entry; | |
9ad5cbcf | 2078 | |
6a40cf0c NC |
2079 | for (entry = elf_symtab_shndx_list (abfd); entry != NULL; entry = entry->next) |
2080 | if (entry->ndx == shindex) | |
2081 | goto success; | |
2082 | ||
2083 | entry = bfd_alloc (abfd, sizeof * entry); | |
2084 | if (entry == NULL) | |
2085 | goto fail; | |
2086 | entry->ndx = shindex; | |
2087 | entry->hdr = * hdr; | |
2088 | entry->next = elf_symtab_shndx_list (abfd); | |
2089 | elf_symtab_shndx_list (abfd) = entry; | |
2090 | elf_elfsections (abfd)[shindex] = & entry->hdr; | |
2091 | goto success; | |
2092 | } | |
9ad5cbcf | 2093 | |
bf67003b | 2094 | case SHT_STRTAB: /* A string table. */ |
252b5132 | 2095 | if (hdr->bfd_section != NULL) |
bf67003b NC |
2096 | goto success; |
2097 | ||
252b5132 RH |
2098 | if (ehdr->e_shstrndx == shindex) |
2099 | { | |
2100 | elf_tdata (abfd)->shstrtab_hdr = *hdr; | |
2101 | elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->shstrtab_hdr; | |
bf67003b | 2102 | goto success; |
252b5132 | 2103 | } |
bf67003b | 2104 | |
1b3a8575 AM |
2105 | if (elf_elfsections (abfd)[elf_onesymtab (abfd)]->sh_link == shindex) |
2106 | { | |
2107 | symtab_strtab: | |
2108 | elf_tdata (abfd)->strtab_hdr = *hdr; | |
2109 | elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->strtab_hdr; | |
bf67003b | 2110 | goto success; |
1b3a8575 | 2111 | } |
bf67003b | 2112 | |
1b3a8575 AM |
2113 | if (elf_elfsections (abfd)[elf_dynsymtab (abfd)]->sh_link == shindex) |
2114 | { | |
2115 | dynsymtab_strtab: | |
2116 | elf_tdata (abfd)->dynstrtab_hdr = *hdr; | |
2117 | hdr = &elf_tdata (abfd)->dynstrtab_hdr; | |
2118 | elf_elfsections (abfd)[shindex] = hdr; | |
2119 | /* We also treat this as a regular section, so that objcopy | |
2120 | can handle it. */ | |
bf67003b NC |
2121 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, |
2122 | shindex); | |
2123 | goto success; | |
1b3a8575 | 2124 | } |
252b5132 | 2125 | |
1b3a8575 AM |
2126 | /* If the string table isn't one of the above, then treat it as a |
2127 | regular section. We need to scan all the headers to be sure, | |
2128 | just in case this strtab section appeared before the above. */ | |
2129 | if (elf_onesymtab (abfd) == 0 || elf_dynsymtab (abfd) == 0) | |
2130 | { | |
2131 | unsigned int i, num_sec; | |
252b5132 | 2132 | |
1b3a8575 AM |
2133 | num_sec = elf_numsections (abfd); |
2134 | for (i = 1; i < num_sec; i++) | |
2135 | { | |
2136 | Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i]; | |
2137 | if (hdr2->sh_link == shindex) | |
2138 | { | |
933d961a JJ |
2139 | /* Prevent endless recursion on broken objects. */ |
2140 | if (i == shindex) | |
bf67003b | 2141 | goto fail; |
1b3a8575 | 2142 | if (! bfd_section_from_shdr (abfd, i)) |
bf67003b | 2143 | goto fail; |
1b3a8575 AM |
2144 | if (elf_onesymtab (abfd) == i) |
2145 | goto symtab_strtab; | |
2146 | if (elf_dynsymtab (abfd) == i) | |
2147 | goto dynsymtab_strtab; | |
2148 | } | |
2149 | } | |
2150 | } | |
bf67003b NC |
2151 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); |
2152 | goto success; | |
252b5132 RH |
2153 | |
2154 | case SHT_REL: | |
2155 | case SHT_RELA: | |
2156 | /* *These* do a lot of work -- but build no sections! */ | |
2157 | { | |
2158 | asection *target_sect; | |
d4730f92 | 2159 | Elf_Internal_Shdr *hdr2, **p_hdr; |
9ad5cbcf | 2160 | unsigned int num_sec = elf_numsections (abfd); |
d4730f92 BS |
2161 | struct bfd_elf_section_data *esdt; |
2162 | bfd_size_type amt; | |
252b5132 | 2163 | |
aa2ca951 JJ |
2164 | if (hdr->sh_entsize |
2165 | != (bfd_size_type) (hdr->sh_type == SHT_REL | |
a50b2160 | 2166 | ? bed->s->sizeof_rel : bed->s->sizeof_rela)) |
bf67003b | 2167 | goto fail; |
a50b2160 | 2168 | |
03ae5f59 | 2169 | /* Check for a bogus link to avoid crashing. */ |
4fbb74a6 | 2170 | if (hdr->sh_link >= num_sec) |
03ae5f59 ILT |
2171 | { |
2172 | ((*_bfd_error_handler) | |
d003868e AM |
2173 | (_("%B: invalid link %lu for reloc section %s (index %u)"), |
2174 | abfd, hdr->sh_link, name, shindex)); | |
bf67003b NC |
2175 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, |
2176 | shindex); | |
2177 | goto success; | |
03ae5f59 ILT |
2178 | } |
2179 | ||
252b5132 RH |
2180 | /* For some incomprehensible reason Oracle distributes |
2181 | libraries for Solaris in which some of the objects have | |
2182 | bogus sh_link fields. It would be nice if we could just | |
2183 | reject them, but, unfortunately, some people need to use | |
2184 | them. We scan through the section headers; if we find only | |
2185 | one suitable symbol table, we clobber the sh_link to point | |
83b89087 L |
2186 | to it. I hope this doesn't break anything. |
2187 | ||
2188 | Don't do it on executable nor shared library. */ | |
2189 | if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0 | |
2190 | && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_SYMTAB | |
252b5132 RH |
2191 | && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_DYNSYM) |
2192 | { | |
9ad5cbcf | 2193 | unsigned int scan; |
252b5132 RH |
2194 | int found; |
2195 | ||
2196 | found = 0; | |
9ad5cbcf | 2197 | for (scan = 1; scan < num_sec; scan++) |
252b5132 RH |
2198 | { |
2199 | if (elf_elfsections (abfd)[scan]->sh_type == SHT_SYMTAB | |
2200 | || elf_elfsections (abfd)[scan]->sh_type == SHT_DYNSYM) | |
2201 | { | |
2202 | if (found != 0) | |
2203 | { | |
2204 | found = 0; | |
2205 | break; | |
2206 | } | |
2207 | found = scan; | |
2208 | } | |
2209 | } | |
2210 | if (found != 0) | |
2211 | hdr->sh_link = found; | |
2212 | } | |
2213 | ||
2214 | /* Get the symbol table. */ | |
1b3a8575 AM |
2215 | if ((elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_SYMTAB |
2216 | || elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_DYNSYM) | |
252b5132 | 2217 | && ! bfd_section_from_shdr (abfd, hdr->sh_link)) |
bf67003b | 2218 | goto fail; |
252b5132 RH |
2219 | |
2220 | /* If this reloc section does not use the main symbol table we | |
2221 | don't treat it as a reloc section. BFD can't adequately | |
2222 | represent such a section, so at least for now, we don't | |
c044fabd | 2223 | try. We just present it as a normal section. We also |
60bcf0fa | 2224 | can't use it as a reloc section if it points to the null |
83b89087 L |
2225 | section, an invalid section, another reloc section, or its |
2226 | sh_link points to the null section. */ | |
185ef66d | 2227 | if (hdr->sh_link != elf_onesymtab (abfd) |
83b89087 | 2228 | || hdr->sh_link == SHN_UNDEF |
185ef66d | 2229 | || hdr->sh_info == SHN_UNDEF |
185ef66d AM |
2230 | || hdr->sh_info >= num_sec |
2231 | || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_REL | |
2232 | || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_RELA) | |
bf67003b NC |
2233 | { |
2234 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, | |
2235 | shindex); | |
2236 | goto success; | |
2237 | } | |
252b5132 RH |
2238 | |
2239 | if (! bfd_section_from_shdr (abfd, hdr->sh_info)) | |
bf67003b NC |
2240 | goto fail; |
2241 | ||
252b5132 RH |
2242 | target_sect = bfd_section_from_elf_index (abfd, hdr->sh_info); |
2243 | if (target_sect == NULL) | |
bf67003b | 2244 | goto fail; |
252b5132 | 2245 | |
d4730f92 BS |
2246 | esdt = elf_section_data (target_sect); |
2247 | if (hdr->sh_type == SHT_RELA) | |
2248 | p_hdr = &esdt->rela.hdr; | |
252b5132 | 2249 | else |
d4730f92 BS |
2250 | p_hdr = &esdt->rel.hdr; |
2251 | ||
06614111 NC |
2252 | /* PR 17512: file: 0b4f81b7. */ |
2253 | if (*p_hdr != NULL) | |
2254 | goto fail; | |
d4730f92 BS |
2255 | amt = sizeof (*hdr2); |
2256 | hdr2 = (Elf_Internal_Shdr *) bfd_alloc (abfd, amt); | |
2257 | if (hdr2 == NULL) | |
bf67003b | 2258 | goto fail; |
252b5132 | 2259 | *hdr2 = *hdr; |
d4730f92 | 2260 | *p_hdr = hdr2; |
252b5132 | 2261 | elf_elfsections (abfd)[shindex] = hdr2; |
d9bc7a44 | 2262 | target_sect->reloc_count += NUM_SHDR_ENTRIES (hdr); |
252b5132 RH |
2263 | target_sect->flags |= SEC_RELOC; |
2264 | target_sect->relocation = NULL; | |
2265 | target_sect->rel_filepos = hdr->sh_offset; | |
bf572ba0 MM |
2266 | /* In the section to which the relocations apply, mark whether |
2267 | its relocations are of the REL or RELA variety. */ | |
72730e0c | 2268 | if (hdr->sh_size != 0) |
d4730f92 BS |
2269 | { |
2270 | if (hdr->sh_type == SHT_RELA) | |
2271 | target_sect->use_rela_p = 1; | |
2272 | } | |
252b5132 | 2273 | abfd->flags |= HAS_RELOC; |
bf67003b | 2274 | goto success; |
252b5132 | 2275 | } |
252b5132 RH |
2276 | |
2277 | case SHT_GNU_verdef: | |
2278 | elf_dynverdef (abfd) = shindex; | |
2279 | elf_tdata (abfd)->dynverdef_hdr = *hdr; | |
bf67003b NC |
2280 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); |
2281 | goto success; | |
252b5132 RH |
2282 | |
2283 | case SHT_GNU_versym: | |
a50b2160 | 2284 | if (hdr->sh_entsize != sizeof (Elf_External_Versym)) |
bf67003b NC |
2285 | goto fail; |
2286 | ||
252b5132 RH |
2287 | elf_dynversym (abfd) = shindex; |
2288 | elf_tdata (abfd)->dynversym_hdr = *hdr; | |
bf67003b NC |
2289 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); |
2290 | goto success; | |
252b5132 RH |
2291 | |
2292 | case SHT_GNU_verneed: | |
2293 | elf_dynverref (abfd) = shindex; | |
2294 | elf_tdata (abfd)->dynverref_hdr = *hdr; | |
bf67003b NC |
2295 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); |
2296 | goto success; | |
252b5132 RH |
2297 | |
2298 | case SHT_SHLIB: | |
bf67003b | 2299 | goto success; |
252b5132 | 2300 | |
dbb410c3 | 2301 | case SHT_GROUP: |
44534af3 | 2302 | if (! IS_VALID_GROUP_SECTION_HEADER (hdr, GRP_ENTRY_SIZE)) |
bf67003b NC |
2303 | goto fail; |
2304 | ||
6dc132d9 | 2305 | if (!_bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex)) |
bf67003b NC |
2306 | goto fail; |
2307 | ||
dbb410c3 AM |
2308 | if (hdr->contents != NULL) |
2309 | { | |
2310 | Elf_Internal_Group *idx = (Elf_Internal_Group *) hdr->contents; | |
06614111 | 2311 | unsigned int n_elt = hdr->sh_size / sizeof (* idx); |
dbb410c3 AM |
2312 | asection *s; |
2313 | ||
06614111 NC |
2314 | if (n_elt == 0) |
2315 | goto fail; | |
b885599b AM |
2316 | if (idx->flags & GRP_COMDAT) |
2317 | hdr->bfd_section->flags | |
2318 | |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD; | |
2319 | ||
45c5e9ed L |
2320 | /* We try to keep the same section order as it comes in. */ |
2321 | idx += n_elt; | |
06614111 | 2322 | |
dbb410c3 | 2323 | while (--n_elt != 0) |
1783205a NC |
2324 | { |
2325 | --idx; | |
2326 | ||
2327 | if (idx->shdr != NULL | |
2328 | && (s = idx->shdr->bfd_section) != NULL | |
2329 | && elf_next_in_group (s) != NULL) | |
2330 | { | |
2331 | elf_next_in_group (hdr->bfd_section) = s; | |
2332 | break; | |
2333 | } | |
2334 | } | |
dbb410c3 | 2335 | } |
bf67003b | 2336 | goto success; |
dbb410c3 | 2337 | |
252b5132 | 2338 | default: |
104d59d1 JM |
2339 | /* Possibly an attributes section. */ |
2340 | if (hdr->sh_type == SHT_GNU_ATTRIBUTES | |
2341 | || hdr->sh_type == bed->obj_attrs_section_type) | |
2342 | { | |
2343 | if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex)) | |
bf67003b | 2344 | goto fail; |
104d59d1 | 2345 | _bfd_elf_parse_attributes (abfd, hdr); |
bf67003b | 2346 | goto success; |
104d59d1 JM |
2347 | } |
2348 | ||
252b5132 | 2349 | /* Check for any processor-specific section types. */ |
3eb70a79 | 2350 | if (bed->elf_backend_section_from_shdr (abfd, hdr, name, shindex)) |
bf67003b | 2351 | goto success; |
3eb70a79 L |
2352 | |
2353 | if (hdr->sh_type >= SHT_LOUSER && hdr->sh_type <= SHT_HIUSER) | |
2354 | { | |
2355 | if ((hdr->sh_flags & SHF_ALLOC) != 0) | |
2356 | /* FIXME: How to properly handle allocated section reserved | |
2357 | for applications? */ | |
2358 | (*_bfd_error_handler) | |
2359 | (_("%B: don't know how to handle allocated, application " | |
2360 | "specific section `%s' [0x%8x]"), | |
2361 | abfd, name, hdr->sh_type); | |
2362 | else | |
bf67003b NC |
2363 | { |
2364 | /* Allow sections reserved for applications. */ | |
2365 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, | |
2366 | shindex); | |
2367 | goto success; | |
2368 | } | |
3eb70a79 L |
2369 | } |
2370 | else if (hdr->sh_type >= SHT_LOPROC | |
2371 | && hdr->sh_type <= SHT_HIPROC) | |
2372 | /* FIXME: We should handle this section. */ | |
2373 | (*_bfd_error_handler) | |
2374 | (_("%B: don't know how to handle processor specific section " | |
2375 | "`%s' [0x%8x]"), | |
2376 | abfd, name, hdr->sh_type); | |
2377 | else if (hdr->sh_type >= SHT_LOOS && hdr->sh_type <= SHT_HIOS) | |
ff15b240 NC |
2378 | { |
2379 | /* Unrecognised OS-specific sections. */ | |
2380 | if ((hdr->sh_flags & SHF_OS_NONCONFORMING) != 0) | |
2381 | /* SHF_OS_NONCONFORMING indicates that special knowledge is | |
08a40648 | 2382 | required to correctly process the section and the file should |
ff15b240 NC |
2383 | be rejected with an error message. */ |
2384 | (*_bfd_error_handler) | |
2385 | (_("%B: don't know how to handle OS specific section " | |
2386 | "`%s' [0x%8x]"), | |
2387 | abfd, name, hdr->sh_type); | |
2388 | else | |
bf67003b NC |
2389 | { |
2390 | /* Otherwise it should be processed. */ | |
2391 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); | |
2392 | goto success; | |
2393 | } | |
ff15b240 | 2394 | } |
3eb70a79 L |
2395 | else |
2396 | /* FIXME: We should handle this section. */ | |
2397 | (*_bfd_error_handler) | |
2398 | (_("%B: don't know how to handle section `%s' [0x%8x]"), | |
2399 | abfd, name, hdr->sh_type); | |
2400 | ||
bf67003b | 2401 | goto fail; |
252b5132 RH |
2402 | } |
2403 | ||
bf67003b NC |
2404 | fail: |
2405 | ret = FALSE; | |
2406 | success: | |
e5b470e2 | 2407 | if (sections_being_created && sections_being_created_abfd == abfd) |
bf67003b NC |
2408 | sections_being_created [shindex] = FALSE; |
2409 | if (-- nesting == 0) | |
5a4b0ccc NC |
2410 | { |
2411 | sections_being_created = NULL; | |
2412 | sections_being_created_abfd = abfd; | |
2413 | } | |
bf67003b | 2414 | return ret; |
252b5132 RH |
2415 | } |
2416 | ||
87d72d41 | 2417 | /* Return the local symbol specified by ABFD, R_SYMNDX. */ |
ec338859 | 2418 | |
87d72d41 AM |
2419 | Elf_Internal_Sym * |
2420 | bfd_sym_from_r_symndx (struct sym_cache *cache, | |
2421 | bfd *abfd, | |
2422 | unsigned long r_symndx) | |
ec338859 | 2423 | { |
ec338859 AM |
2424 | unsigned int ent = r_symndx % LOCAL_SYM_CACHE_SIZE; |
2425 | ||
a5d1b3b5 AM |
2426 | if (cache->abfd != abfd || cache->indx[ent] != r_symndx) |
2427 | { | |
2428 | Elf_Internal_Shdr *symtab_hdr; | |
2429 | unsigned char esym[sizeof (Elf64_External_Sym)]; | |
2430 | Elf_External_Sym_Shndx eshndx; | |
ec338859 | 2431 | |
a5d1b3b5 AM |
2432 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; |
2433 | if (bfd_elf_get_elf_syms (abfd, symtab_hdr, 1, r_symndx, | |
87d72d41 | 2434 | &cache->sym[ent], esym, &eshndx) == NULL) |
a5d1b3b5 | 2435 | return NULL; |
9ad5cbcf | 2436 | |
a5d1b3b5 AM |
2437 | if (cache->abfd != abfd) |
2438 | { | |
2439 | memset (cache->indx, -1, sizeof (cache->indx)); | |
2440 | cache->abfd = abfd; | |
2441 | } | |
2442 | cache->indx[ent] = r_symndx; | |
ec338859 | 2443 | } |
a5d1b3b5 | 2444 | |
87d72d41 | 2445 | return &cache->sym[ent]; |
ec338859 AM |
2446 | } |
2447 | ||
252b5132 RH |
2448 | /* Given an ELF section number, retrieve the corresponding BFD |
2449 | section. */ | |
2450 | ||
2451 | asection * | |
91d6fa6a | 2452 | bfd_section_from_elf_index (bfd *abfd, unsigned int sec_index) |
252b5132 | 2453 | { |
91d6fa6a | 2454 | if (sec_index >= elf_numsections (abfd)) |
252b5132 | 2455 | return NULL; |
91d6fa6a | 2456 | return elf_elfsections (abfd)[sec_index]->bfd_section; |
252b5132 RH |
2457 | } |
2458 | ||
b35d266b | 2459 | static const struct bfd_elf_special_section special_sections_b[] = |
2f89ff8d | 2460 | { |
0112cd26 NC |
2461 | { STRING_COMMA_LEN (".bss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE }, |
2462 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2463 | }; |
2464 | ||
b35d266b | 2465 | static const struct bfd_elf_special_section special_sections_c[] = |
7f4d3958 | 2466 | { |
0112cd26 NC |
2467 | { STRING_COMMA_LEN (".comment"), 0, SHT_PROGBITS, 0 }, |
2468 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2469 | }; |
2470 | ||
b35d266b | 2471 | static const struct bfd_elf_special_section special_sections_d[] = |
7f4d3958 | 2472 | { |
0112cd26 NC |
2473 | { STRING_COMMA_LEN (".data"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE }, |
2474 | { STRING_COMMA_LEN (".data1"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE }, | |
a9a72a65 DE |
2475 | /* There are more DWARF sections than these, but they needn't be added here |
2476 | unless you have to cope with broken compilers that don't emit section | |
2477 | attributes or you want to help the user writing assembler. */ | |
0112cd26 NC |
2478 | { STRING_COMMA_LEN (".debug"), 0, SHT_PROGBITS, 0 }, |
2479 | { STRING_COMMA_LEN (".debug_line"), 0, SHT_PROGBITS, 0 }, | |
2480 | { STRING_COMMA_LEN (".debug_info"), 0, SHT_PROGBITS, 0 }, | |
2481 | { STRING_COMMA_LEN (".debug_abbrev"), 0, SHT_PROGBITS, 0 }, | |
2482 | { STRING_COMMA_LEN (".debug_aranges"), 0, SHT_PROGBITS, 0 }, | |
2483 | { STRING_COMMA_LEN (".dynamic"), 0, SHT_DYNAMIC, SHF_ALLOC }, | |
2484 | { STRING_COMMA_LEN (".dynstr"), 0, SHT_STRTAB, SHF_ALLOC }, | |
2485 | { STRING_COMMA_LEN (".dynsym"), 0, SHT_DYNSYM, SHF_ALLOC }, | |
2486 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2487 | }; |
2488 | ||
b35d266b | 2489 | static const struct bfd_elf_special_section special_sections_f[] = |
7f4d3958 | 2490 | { |
0112cd26 NC |
2491 | { STRING_COMMA_LEN (".fini"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR }, |
2492 | { STRING_COMMA_LEN (".fini_array"), 0, SHT_FINI_ARRAY, SHF_ALLOC + SHF_WRITE }, | |
2493 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2494 | }; |
2495 | ||
b35d266b | 2496 | static const struct bfd_elf_special_section special_sections_g[] = |
7f4d3958 | 2497 | { |
0112cd26 | 2498 | { STRING_COMMA_LEN (".gnu.linkonce.b"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE }, |
68efed41 | 2499 | { STRING_COMMA_LEN (".gnu.lto_"), -1, SHT_PROGBITS, SHF_EXCLUDE }, |
0112cd26 NC |
2500 | { STRING_COMMA_LEN (".got"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE }, |
2501 | { STRING_COMMA_LEN (".gnu.version"), 0, SHT_GNU_versym, 0 }, | |
2502 | { STRING_COMMA_LEN (".gnu.version_d"), 0, SHT_GNU_verdef, 0 }, | |
2503 | { STRING_COMMA_LEN (".gnu.version_r"), 0, SHT_GNU_verneed, 0 }, | |
2504 | { STRING_COMMA_LEN (".gnu.liblist"), 0, SHT_GNU_LIBLIST, SHF_ALLOC }, | |
2505 | { STRING_COMMA_LEN (".gnu.conflict"), 0, SHT_RELA, SHF_ALLOC }, | |
2506 | { STRING_COMMA_LEN (".gnu.hash"), 0, SHT_GNU_HASH, SHF_ALLOC }, | |
2507 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2508 | }; |
2509 | ||
b35d266b | 2510 | static const struct bfd_elf_special_section special_sections_h[] = |
7f4d3958 | 2511 | { |
0112cd26 NC |
2512 | { STRING_COMMA_LEN (".hash"), 0, SHT_HASH, SHF_ALLOC }, |
2513 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2514 | }; |
2515 | ||
b35d266b | 2516 | static const struct bfd_elf_special_section special_sections_i[] = |
7f4d3958 | 2517 | { |
0112cd26 NC |
2518 | { STRING_COMMA_LEN (".init"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR }, |
2519 | { STRING_COMMA_LEN (".init_array"), 0, SHT_INIT_ARRAY, SHF_ALLOC + SHF_WRITE }, | |
2520 | { STRING_COMMA_LEN (".interp"), 0, SHT_PROGBITS, 0 }, | |
2521 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2522 | }; |
2523 | ||
b35d266b | 2524 | static const struct bfd_elf_special_section special_sections_l[] = |
7f4d3958 | 2525 | { |
0112cd26 NC |
2526 | { STRING_COMMA_LEN (".line"), 0, SHT_PROGBITS, 0 }, |
2527 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2528 | }; |
2529 | ||
b35d266b | 2530 | static const struct bfd_elf_special_section special_sections_n[] = |
7f4d3958 | 2531 | { |
0112cd26 NC |
2532 | { STRING_COMMA_LEN (".note.GNU-stack"), 0, SHT_PROGBITS, 0 }, |
2533 | { STRING_COMMA_LEN (".note"), -1, SHT_NOTE, 0 }, | |
2534 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2535 | }; |
2536 | ||
b35d266b | 2537 | static const struct bfd_elf_special_section special_sections_p[] = |
7f4d3958 | 2538 | { |
0112cd26 NC |
2539 | { STRING_COMMA_LEN (".preinit_array"), 0, SHT_PREINIT_ARRAY, SHF_ALLOC + SHF_WRITE }, |
2540 | { STRING_COMMA_LEN (".plt"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR }, | |
2541 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2542 | }; |
2543 | ||
b35d266b | 2544 | static const struct bfd_elf_special_section special_sections_r[] = |
7f4d3958 | 2545 | { |
0112cd26 NC |
2546 | { STRING_COMMA_LEN (".rodata"), -2, SHT_PROGBITS, SHF_ALLOC }, |
2547 | { STRING_COMMA_LEN (".rodata1"), 0, SHT_PROGBITS, SHF_ALLOC }, | |
2548 | { STRING_COMMA_LEN (".rela"), -1, SHT_RELA, 0 }, | |
2549 | { STRING_COMMA_LEN (".rel"), -1, SHT_REL, 0 }, | |
2550 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2551 | }; |
2552 | ||
b35d266b | 2553 | static const struct bfd_elf_special_section special_sections_s[] = |
7f4d3958 | 2554 | { |
0112cd26 NC |
2555 | { STRING_COMMA_LEN (".shstrtab"), 0, SHT_STRTAB, 0 }, |
2556 | { STRING_COMMA_LEN (".strtab"), 0, SHT_STRTAB, 0 }, | |
2557 | { STRING_COMMA_LEN (".symtab"), 0, SHT_SYMTAB, 0 }, | |
60ff4dc4 HPN |
2558 | /* See struct bfd_elf_special_section declaration for the semantics of |
2559 | this special case where .prefix_length != strlen (.prefix). */ | |
2560 | { ".stabstr", 5, 3, SHT_STRTAB, 0 }, | |
0112cd26 | 2561 | { NULL, 0, 0, 0, 0 } |
2f89ff8d L |
2562 | }; |
2563 | ||
b35d266b | 2564 | static const struct bfd_elf_special_section special_sections_t[] = |
7f4d3958 | 2565 | { |
0112cd26 NC |
2566 | { STRING_COMMA_LEN (".text"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR }, |
2567 | { STRING_COMMA_LEN (".tbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS }, | |
2568 | { STRING_COMMA_LEN (".tdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS }, | |
2569 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2570 | }; |
2571 | ||
1b315056 CS |
2572 | static const struct bfd_elf_special_section special_sections_z[] = |
2573 | { | |
2574 | { STRING_COMMA_LEN (".zdebug_line"), 0, SHT_PROGBITS, 0 }, | |
2575 | { STRING_COMMA_LEN (".zdebug_info"), 0, SHT_PROGBITS, 0 }, | |
2576 | { STRING_COMMA_LEN (".zdebug_abbrev"), 0, SHT_PROGBITS, 0 }, | |
2577 | { STRING_COMMA_LEN (".zdebug_aranges"), 0, SHT_PROGBITS, 0 }, | |
2578 | { NULL, 0, 0, 0, 0 } | |
2579 | }; | |
2580 | ||
e4c93b56 | 2581 | static const struct bfd_elf_special_section * const special_sections[] = |
7f4d3958 | 2582 | { |
7f4d3958 | 2583 | special_sections_b, /* 'b' */ |
98ece1b3 | 2584 | special_sections_c, /* 'c' */ |
7f4d3958 L |
2585 | special_sections_d, /* 'd' */ |
2586 | NULL, /* 'e' */ | |
2587 | special_sections_f, /* 'f' */ | |
2588 | special_sections_g, /* 'g' */ | |
2589 | special_sections_h, /* 'h' */ | |
2590 | special_sections_i, /* 'i' */ | |
2591 | NULL, /* 'j' */ | |
2592 | NULL, /* 'k' */ | |
2593 | special_sections_l, /* 'l' */ | |
2594 | NULL, /* 'm' */ | |
2595 | special_sections_n, /* 'n' */ | |
2596 | NULL, /* 'o' */ | |
2597 | special_sections_p, /* 'p' */ | |
2598 | NULL, /* 'q' */ | |
2599 | special_sections_r, /* 'r' */ | |
2600 | special_sections_s, /* 's' */ | |
2601 | special_sections_t, /* 't' */ | |
1b315056 CS |
2602 | NULL, /* 'u' */ |
2603 | NULL, /* 'v' */ | |
2604 | NULL, /* 'w' */ | |
2605 | NULL, /* 'x' */ | |
2606 | NULL, /* 'y' */ | |
2607 | special_sections_z /* 'z' */ | |
7f4d3958 L |
2608 | }; |
2609 | ||
551b43fd AM |
2610 | const struct bfd_elf_special_section * |
2611 | _bfd_elf_get_special_section (const char *name, | |
2612 | const struct bfd_elf_special_section *spec, | |
2613 | unsigned int rela) | |
2f89ff8d L |
2614 | { |
2615 | int i; | |
7f4d3958 | 2616 | int len; |
7f4d3958 | 2617 | |
551b43fd | 2618 | len = strlen (name); |
7f4d3958 | 2619 | |
551b43fd | 2620 | for (i = 0; spec[i].prefix != NULL; i++) |
7dcb9820 AM |
2621 | { |
2622 | int suffix_len; | |
551b43fd | 2623 | int prefix_len = spec[i].prefix_length; |
7dcb9820 AM |
2624 | |
2625 | if (len < prefix_len) | |
2626 | continue; | |
551b43fd | 2627 | if (memcmp (name, spec[i].prefix, prefix_len) != 0) |
7dcb9820 AM |
2628 | continue; |
2629 | ||
551b43fd | 2630 | suffix_len = spec[i].suffix_length; |
7dcb9820 AM |
2631 | if (suffix_len <= 0) |
2632 | { | |
2633 | if (name[prefix_len] != 0) | |
2634 | { | |
2635 | if (suffix_len == 0) | |
2636 | continue; | |
2637 | if (name[prefix_len] != '.' | |
2638 | && (suffix_len == -2 | |
551b43fd | 2639 | || (rela && spec[i].type == SHT_REL))) |
7dcb9820 AM |
2640 | continue; |
2641 | } | |
2642 | } | |
2643 | else | |
2644 | { | |
2645 | if (len < prefix_len + suffix_len) | |
2646 | continue; | |
2647 | if (memcmp (name + len - suffix_len, | |
551b43fd | 2648 | spec[i].prefix + prefix_len, |
7dcb9820 AM |
2649 | suffix_len) != 0) |
2650 | continue; | |
2651 | } | |
551b43fd | 2652 | return &spec[i]; |
7dcb9820 | 2653 | } |
2f89ff8d L |
2654 | |
2655 | return NULL; | |
2656 | } | |
2657 | ||
7dcb9820 | 2658 | const struct bfd_elf_special_section * |
29ef7005 | 2659 | _bfd_elf_get_sec_type_attr (bfd *abfd, asection *sec) |
2f89ff8d | 2660 | { |
551b43fd AM |
2661 | int i; |
2662 | const struct bfd_elf_special_section *spec; | |
29ef7005 | 2663 | const struct elf_backend_data *bed; |
2f89ff8d L |
2664 | |
2665 | /* See if this is one of the special sections. */ | |
551b43fd AM |
2666 | if (sec->name == NULL) |
2667 | return NULL; | |
2f89ff8d | 2668 | |
29ef7005 L |
2669 | bed = get_elf_backend_data (abfd); |
2670 | spec = bed->special_sections; | |
2671 | if (spec) | |
2672 | { | |
2673 | spec = _bfd_elf_get_special_section (sec->name, | |
2674 | bed->special_sections, | |
2675 | sec->use_rela_p); | |
2676 | if (spec != NULL) | |
2677 | return spec; | |
2678 | } | |
2679 | ||
551b43fd AM |
2680 | if (sec->name[0] != '.') |
2681 | return NULL; | |
2f89ff8d | 2682 | |
551b43fd | 2683 | i = sec->name[1] - 'b'; |
1b315056 | 2684 | if (i < 0 || i > 'z' - 'b') |
551b43fd AM |
2685 | return NULL; |
2686 | ||
2687 | spec = special_sections[i]; | |
2f89ff8d | 2688 | |
551b43fd AM |
2689 | if (spec == NULL) |
2690 | return NULL; | |
2691 | ||
2692 | return _bfd_elf_get_special_section (sec->name, spec, sec->use_rela_p); | |
2f89ff8d L |
2693 | } |
2694 | ||
b34976b6 | 2695 | bfd_boolean |
217aa764 | 2696 | _bfd_elf_new_section_hook (bfd *abfd, asection *sec) |
252b5132 RH |
2697 | { |
2698 | struct bfd_elf_section_data *sdata; | |
551b43fd | 2699 | const struct elf_backend_data *bed; |
7dcb9820 | 2700 | const struct bfd_elf_special_section *ssect; |
252b5132 | 2701 | |
f0abc2a1 AM |
2702 | sdata = (struct bfd_elf_section_data *) sec->used_by_bfd; |
2703 | if (sdata == NULL) | |
2704 | { | |
a50b1753 NC |
2705 | sdata = (struct bfd_elf_section_data *) bfd_zalloc (abfd, |
2706 | sizeof (*sdata)); | |
f0abc2a1 AM |
2707 | if (sdata == NULL) |
2708 | return FALSE; | |
217aa764 | 2709 | sec->used_by_bfd = sdata; |
f0abc2a1 | 2710 | } |
bf572ba0 | 2711 | |
551b43fd AM |
2712 | /* Indicate whether or not this section should use RELA relocations. */ |
2713 | bed = get_elf_backend_data (abfd); | |
2714 | sec->use_rela_p = bed->default_use_rela_p; | |
2715 | ||
e843e0f8 L |
2716 | /* When we read a file, we don't need to set ELF section type and |
2717 | flags. They will be overridden in _bfd_elf_make_section_from_shdr | |
2718 | anyway. We will set ELF section type and flags for all linker | |
2719 | created sections. If user specifies BFD section flags, we will | |
2720 | set ELF section type and flags based on BFD section flags in | |
02ecc8e9 L |
2721 | elf_fake_sections. Special handling for .init_array/.fini_array |
2722 | output sections since they may contain .ctors/.dtors input | |
2723 | sections. We don't want _bfd_elf_init_private_section_data to | |
2724 | copy ELF section type from .ctors/.dtors input sections. */ | |
2725 | if (abfd->direction != read_direction | |
3496cb2a | 2726 | || (sec->flags & SEC_LINKER_CREATED) != 0) |
2f89ff8d | 2727 | { |
551b43fd | 2728 | ssect = (*bed->get_sec_type_attr) (abfd, sec); |
02ecc8e9 L |
2729 | if (ssect != NULL |
2730 | && (!sec->flags | |
2731 | || (sec->flags & SEC_LINKER_CREATED) != 0 | |
2732 | || ssect->type == SHT_INIT_ARRAY | |
2733 | || ssect->type == SHT_FINI_ARRAY)) | |
a31501e9 L |
2734 | { |
2735 | elf_section_type (sec) = ssect->type; | |
2736 | elf_section_flags (sec) = ssect->attr; | |
2737 | } | |
2f89ff8d L |
2738 | } |
2739 | ||
f592407e | 2740 | return _bfd_generic_new_section_hook (abfd, sec); |
252b5132 RH |
2741 | } |
2742 | ||
2743 | /* Create a new bfd section from an ELF program header. | |
2744 | ||
2745 | Since program segments have no names, we generate a synthetic name | |
2746 | of the form segment<NUM>, where NUM is generally the index in the | |
2747 | program header table. For segments that are split (see below) we | |
2748 | generate the names segment<NUM>a and segment<NUM>b. | |
2749 | ||
2750 | Note that some program segments may have a file size that is different than | |
2751 | (less than) the memory size. All this means is that at execution the | |
2752 | system must allocate the amount of memory specified by the memory size, | |
2753 | but only initialize it with the first "file size" bytes read from the | |
2754 | file. This would occur for example, with program segments consisting | |
2755 | of combined data+bss. | |
2756 | ||
2757 | To handle the above situation, this routine generates TWO bfd sections | |
2758 | for the single program segment. The first has the length specified by | |
2759 | the file size of the segment, and the second has the length specified | |
2760 | by the difference between the two sizes. In effect, the segment is split | |
d5191d0c | 2761 | into its initialized and uninitialized parts. |
252b5132 RH |
2762 | |
2763 | */ | |
2764 | ||
b34976b6 | 2765 | bfd_boolean |
217aa764 AM |
2766 | _bfd_elf_make_section_from_phdr (bfd *abfd, |
2767 | Elf_Internal_Phdr *hdr, | |
91d6fa6a | 2768 | int hdr_index, |
a50b1753 | 2769 | const char *type_name) |
252b5132 RH |
2770 | { |
2771 | asection *newsect; | |
2772 | char *name; | |
2773 | char namebuf[64]; | |
d4c88bbb | 2774 | size_t len; |
252b5132 RH |
2775 | int split; |
2776 | ||
2777 | split = ((hdr->p_memsz > 0) | |
2778 | && (hdr->p_filesz > 0) | |
2779 | && (hdr->p_memsz > hdr->p_filesz)); | |
d5191d0c AM |
2780 | |
2781 | if (hdr->p_filesz > 0) | |
252b5132 | 2782 | { |
91d6fa6a | 2783 | sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "a" : ""); |
d5191d0c | 2784 | len = strlen (namebuf) + 1; |
a50b1753 | 2785 | name = (char *) bfd_alloc (abfd, len); |
d5191d0c AM |
2786 | if (!name) |
2787 | return FALSE; | |
2788 | memcpy (name, namebuf, len); | |
2789 | newsect = bfd_make_section (abfd, name); | |
2790 | if (newsect == NULL) | |
2791 | return FALSE; | |
2792 | newsect->vma = hdr->p_vaddr; | |
2793 | newsect->lma = hdr->p_paddr; | |
2794 | newsect->size = hdr->p_filesz; | |
2795 | newsect->filepos = hdr->p_offset; | |
2796 | newsect->flags |= SEC_HAS_CONTENTS; | |
2797 | newsect->alignment_power = bfd_log2 (hdr->p_align); | |
2798 | if (hdr->p_type == PT_LOAD) | |
252b5132 | 2799 | { |
d5191d0c AM |
2800 | newsect->flags |= SEC_ALLOC; |
2801 | newsect->flags |= SEC_LOAD; | |
2802 | if (hdr->p_flags & PF_X) | |
2803 | { | |
2804 | /* FIXME: all we known is that it has execute PERMISSION, | |
2805 | may be data. */ | |
2806 | newsect->flags |= SEC_CODE; | |
2807 | } | |
2808 | } | |
2809 | if (!(hdr->p_flags & PF_W)) | |
2810 | { | |
2811 | newsect->flags |= SEC_READONLY; | |
252b5132 | 2812 | } |
252b5132 RH |
2813 | } |
2814 | ||
d5191d0c | 2815 | if (hdr->p_memsz > hdr->p_filesz) |
252b5132 | 2816 | { |
d5191d0c AM |
2817 | bfd_vma align; |
2818 | ||
91d6fa6a | 2819 | sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "b" : ""); |
d4c88bbb | 2820 | len = strlen (namebuf) + 1; |
a50b1753 | 2821 | name = (char *) bfd_alloc (abfd, len); |
252b5132 | 2822 | if (!name) |
b34976b6 | 2823 | return FALSE; |
d4c88bbb | 2824 | memcpy (name, namebuf, len); |
252b5132 RH |
2825 | newsect = bfd_make_section (abfd, name); |
2826 | if (newsect == NULL) | |
b34976b6 | 2827 | return FALSE; |
252b5132 RH |
2828 | newsect->vma = hdr->p_vaddr + hdr->p_filesz; |
2829 | newsect->lma = hdr->p_paddr + hdr->p_filesz; | |
eea6121a | 2830 | newsect->size = hdr->p_memsz - hdr->p_filesz; |
d5191d0c AM |
2831 | newsect->filepos = hdr->p_offset + hdr->p_filesz; |
2832 | align = newsect->vma & -newsect->vma; | |
2833 | if (align == 0 || align > hdr->p_align) | |
2834 | align = hdr->p_align; | |
2835 | newsect->alignment_power = bfd_log2 (align); | |
252b5132 RH |
2836 | if (hdr->p_type == PT_LOAD) |
2837 | { | |
d5191d0c AM |
2838 | /* Hack for gdb. Segments that have not been modified do |
2839 | not have their contents written to a core file, on the | |
2840 | assumption that a debugger can find the contents in the | |
2841 | executable. We flag this case by setting the fake | |
2842 | section size to zero. Note that "real" bss sections will | |
2843 | always have their contents dumped to the core file. */ | |
2844 | if (bfd_get_format (abfd) == bfd_core) | |
2845 | newsect->size = 0; | |
252b5132 RH |
2846 | newsect->flags |= SEC_ALLOC; |
2847 | if (hdr->p_flags & PF_X) | |
2848 | newsect->flags |= SEC_CODE; | |
2849 | } | |
2850 | if (!(hdr->p_flags & PF_W)) | |
2851 | newsect->flags |= SEC_READONLY; | |
2852 | } | |
2853 | ||
b34976b6 | 2854 | return TRUE; |
252b5132 RH |
2855 | } |
2856 | ||
b34976b6 | 2857 | bfd_boolean |
91d6fa6a | 2858 | bfd_section_from_phdr (bfd *abfd, Elf_Internal_Phdr *hdr, int hdr_index) |
20cfcaae | 2859 | { |
9c5bfbb7 | 2860 | const struct elf_backend_data *bed; |
20cfcaae NC |
2861 | |
2862 | switch (hdr->p_type) | |
2863 | { | |
2864 | case PT_NULL: | |
91d6fa6a | 2865 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "null"); |
20cfcaae NC |
2866 | |
2867 | case PT_LOAD: | |
91d6fa6a | 2868 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "load"); |
20cfcaae NC |
2869 | |
2870 | case PT_DYNAMIC: | |
91d6fa6a | 2871 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "dynamic"); |
20cfcaae NC |
2872 | |
2873 | case PT_INTERP: | |
91d6fa6a | 2874 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "interp"); |
20cfcaae NC |
2875 | |
2876 | case PT_NOTE: | |
91d6fa6a | 2877 | if (! _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "note")) |
b34976b6 | 2878 | return FALSE; |
718175fa | 2879 | if (! elf_read_notes (abfd, hdr->p_offset, hdr->p_filesz)) |
b34976b6 AM |
2880 | return FALSE; |
2881 | return TRUE; | |
20cfcaae NC |
2882 | |
2883 | case PT_SHLIB: | |
91d6fa6a | 2884 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "shlib"); |
20cfcaae NC |
2885 | |
2886 | case PT_PHDR: | |
91d6fa6a | 2887 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "phdr"); |
20cfcaae | 2888 | |
811072d8 | 2889 | case PT_GNU_EH_FRAME: |
91d6fa6a | 2890 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, |
811072d8 RM |
2891 | "eh_frame_hdr"); |
2892 | ||
2b05f1b7 | 2893 | case PT_GNU_STACK: |
91d6fa6a | 2894 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "stack"); |
9ee5e499 | 2895 | |
8c37241b | 2896 | case PT_GNU_RELRO: |
91d6fa6a | 2897 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "relro"); |
8c37241b | 2898 | |
20cfcaae | 2899 | default: |
8c1acd09 | 2900 | /* Check for any processor-specific program segment types. */ |
20cfcaae | 2901 | bed = get_elf_backend_data (abfd); |
91d6fa6a | 2902 | return bed->elf_backend_section_from_phdr (abfd, hdr, hdr_index, "proc"); |
20cfcaae NC |
2903 | } |
2904 | } | |
2905 | ||
d4730f92 BS |
2906 | /* Return the REL_HDR for SEC, assuming there is only a single one, either |
2907 | REL or RELA. */ | |
2908 | ||
2909 | Elf_Internal_Shdr * | |
2910 | _bfd_elf_single_rel_hdr (asection *sec) | |
2911 | { | |
2912 | if (elf_section_data (sec)->rel.hdr) | |
2913 | { | |
2914 | BFD_ASSERT (elf_section_data (sec)->rela.hdr == NULL); | |
2915 | return elf_section_data (sec)->rel.hdr; | |
2916 | } | |
2917 | else | |
2918 | return elf_section_data (sec)->rela.hdr; | |
2919 | } | |
2920 | ||
3e19fb8f L |
2921 | static bfd_boolean |
2922 | _bfd_elf_set_reloc_sh_name (bfd *abfd, | |
2923 | Elf_Internal_Shdr *rel_hdr, | |
2924 | const char *sec_name, | |
2925 | bfd_boolean use_rela_p) | |
2926 | { | |
2927 | char *name = (char *) bfd_alloc (abfd, | |
2928 | sizeof ".rela" + strlen (sec_name)); | |
2929 | if (name == NULL) | |
2930 | return FALSE; | |
2931 | ||
2932 | sprintf (name, "%s%s", use_rela_p ? ".rela" : ".rel", sec_name); | |
2933 | rel_hdr->sh_name = | |
2934 | (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), name, | |
2935 | FALSE); | |
2936 | if (rel_hdr->sh_name == (unsigned int) -1) | |
2937 | return FALSE; | |
2938 | ||
2939 | return TRUE; | |
2940 | } | |
2941 | ||
d4730f92 BS |
2942 | /* Allocate and initialize a section-header for a new reloc section, |
2943 | containing relocations against ASECT. It is stored in RELDATA. If | |
2944 | USE_RELA_P is TRUE, we use RELA relocations; otherwise, we use REL | |
2945 | relocations. */ | |
23bc299b | 2946 | |
5d13b3b3 | 2947 | static bfd_boolean |
217aa764 | 2948 | _bfd_elf_init_reloc_shdr (bfd *abfd, |
d4730f92 | 2949 | struct bfd_elf_section_reloc_data *reldata, |
f6fe1ccd | 2950 | const char *sec_name, |
3e19fb8f L |
2951 | bfd_boolean use_rela_p, |
2952 | bfd_boolean delay_st_name_p) | |
23bc299b | 2953 | { |
d4730f92 | 2954 | Elf_Internal_Shdr *rel_hdr; |
9c5bfbb7 | 2955 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
d4730f92 BS |
2956 | bfd_size_type amt; |
2957 | ||
2958 | amt = sizeof (Elf_Internal_Shdr); | |
2959 | BFD_ASSERT (reldata->hdr == NULL); | |
2960 | rel_hdr = bfd_zalloc (abfd, amt); | |
2961 | reldata->hdr = rel_hdr; | |
23bc299b | 2962 | |
3e19fb8f L |
2963 | if (delay_st_name_p) |
2964 | rel_hdr->sh_name = (unsigned int) -1; | |
2965 | else if (!_bfd_elf_set_reloc_sh_name (abfd, rel_hdr, sec_name, | |
2966 | use_rela_p)) | |
b34976b6 | 2967 | return FALSE; |
23bc299b MM |
2968 | rel_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL; |
2969 | rel_hdr->sh_entsize = (use_rela_p | |
2970 | ? bed->s->sizeof_rela | |
2971 | : bed->s->sizeof_rel); | |
72de5009 | 2972 | rel_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align; |
23bc299b MM |
2973 | rel_hdr->sh_flags = 0; |
2974 | rel_hdr->sh_addr = 0; | |
2975 | rel_hdr->sh_size = 0; | |
2976 | rel_hdr->sh_offset = 0; | |
2977 | ||
b34976b6 | 2978 | return TRUE; |
23bc299b MM |
2979 | } |
2980 | ||
94be91de JB |
2981 | /* Return the default section type based on the passed in section flags. */ |
2982 | ||
2983 | int | |
2984 | bfd_elf_get_default_section_type (flagword flags) | |
2985 | { | |
2986 | if ((flags & SEC_ALLOC) != 0 | |
2e76e85a | 2987 | && (flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0) |
94be91de JB |
2988 | return SHT_NOBITS; |
2989 | return SHT_PROGBITS; | |
2990 | } | |
2991 | ||
d4730f92 BS |
2992 | struct fake_section_arg |
2993 | { | |
2994 | struct bfd_link_info *link_info; | |
2995 | bfd_boolean failed; | |
2996 | }; | |
2997 | ||
252b5132 RH |
2998 | /* Set up an ELF internal section header for a section. */ |
2999 | ||
252b5132 | 3000 | static void |
d4730f92 | 3001 | elf_fake_sections (bfd *abfd, asection *asect, void *fsarg) |
252b5132 | 3002 | { |
d4730f92 | 3003 | struct fake_section_arg *arg = (struct fake_section_arg *)fsarg; |
9c5bfbb7 | 3004 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
d4730f92 | 3005 | struct bfd_elf_section_data *esd = elf_section_data (asect); |
252b5132 | 3006 | Elf_Internal_Shdr *this_hdr; |
0414f35b | 3007 | unsigned int sh_type; |
0ce398f1 | 3008 | const char *name = asect->name; |
3e19fb8f | 3009 | bfd_boolean delay_st_name_p = FALSE; |
252b5132 | 3010 | |
d4730f92 | 3011 | if (arg->failed) |
252b5132 RH |
3012 | { |
3013 | /* We already failed; just get out of the bfd_map_over_sections | |
08a40648 | 3014 | loop. */ |
252b5132 RH |
3015 | return; |
3016 | } | |
3017 | ||
d4730f92 | 3018 | this_hdr = &esd->this_hdr; |
252b5132 | 3019 | |
f6fe1ccd | 3020 | if (arg->link_info) |
0ce398f1 | 3021 | { |
f6fe1ccd L |
3022 | /* ld: compress DWARF debug sections with names: .debug_*. */ |
3023 | if ((arg->link_info->compress_debug & COMPRESS_DEBUG) | |
3024 | && (asect->flags & SEC_DEBUGGING) | |
3025 | && name[1] == 'd' | |
3026 | && name[6] == '_') | |
3027 | { | |
3028 | /* Set SEC_ELF_COMPRESS to indicate this section should be | |
3029 | compressed. */ | |
3030 | asect->flags |= SEC_ELF_COMPRESS; | |
0ce398f1 | 3031 | |
3e19fb8f L |
3032 | /* If this section will be compressed, delay adding setion |
3033 | name to section name section after it is compressed in | |
3034 | _bfd_elf_assign_file_positions_for_non_load. */ | |
3035 | delay_st_name_p = TRUE; | |
f6fe1ccd L |
3036 | } |
3037 | } | |
3038 | else if ((asect->flags & SEC_ELF_RENAME)) | |
3039 | { | |
3040 | /* objcopy: rename output DWARF debug section. */ | |
3041 | if ((abfd->flags & (BFD_DECOMPRESS | BFD_COMPRESS_GABI))) | |
3042 | { | |
3043 | /* When we decompress or compress with SHF_COMPRESSED, | |
3044 | convert section name from .zdebug_* to .debug_* if | |
3045 | needed. */ | |
3046 | if (name[1] == 'z') | |
3047 | { | |
3048 | char *new_name = convert_zdebug_to_debug (abfd, name); | |
3049 | if (new_name == NULL) | |
3050 | { | |
3051 | arg->failed = TRUE; | |
3052 | return; | |
3053 | } | |
3054 | name = new_name; | |
3055 | } | |
3056 | } | |
3057 | else if (asect->compress_status == COMPRESS_SECTION_DONE) | |
0ce398f1 | 3058 | { |
f6fe1ccd L |
3059 | /* PR binutils/18087: Compression does not always make a |
3060 | section smaller. So only rename the section when | |
3061 | compression has actually taken place. If input section | |
3062 | name is .zdebug_*, we should never compress it again. */ | |
3063 | char *new_name = convert_debug_to_zdebug (abfd, name); | |
0ce398f1 L |
3064 | if (new_name == NULL) |
3065 | { | |
3066 | arg->failed = TRUE; | |
3067 | return; | |
3068 | } | |
f6fe1ccd L |
3069 | BFD_ASSERT (name[1] != 'z'); |
3070 | name = new_name; | |
0ce398f1 L |
3071 | } |
3072 | } | |
3073 | ||
3e19fb8f L |
3074 | if (delay_st_name_p) |
3075 | this_hdr->sh_name = (unsigned int) -1; | |
3076 | else | |
252b5132 | 3077 | { |
3e19fb8f L |
3078 | this_hdr->sh_name |
3079 | = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), | |
3080 | name, FALSE); | |
3081 | if (this_hdr->sh_name == (unsigned int) -1) | |
3082 | { | |
3083 | arg->failed = TRUE; | |
3084 | return; | |
3085 | } | |
252b5132 RH |
3086 | } |
3087 | ||
a4d8e49b | 3088 | /* Don't clear sh_flags. Assembler may set additional bits. */ |
252b5132 RH |
3089 | |
3090 | if ((asect->flags & SEC_ALLOC) != 0 | |
3091 | || asect->user_set_vma) | |
3092 | this_hdr->sh_addr = asect->vma; | |
3093 | else | |
3094 | this_hdr->sh_addr = 0; | |
3095 | ||
3096 | this_hdr->sh_offset = 0; | |
eea6121a | 3097 | this_hdr->sh_size = asect->size; |
252b5132 | 3098 | this_hdr->sh_link = 0; |
c86934ce NC |
3099 | /* PR 17512: file: 0eb809fe, 8b0535ee. */ |
3100 | if (asect->alignment_power >= (sizeof (bfd_vma) * 8) - 1) | |
3101 | { | |
3102 | (*_bfd_error_handler) | |
3103 | (_("%B: error: Alignment power %d of section `%A' is too big"), | |
3104 | abfd, asect, asect->alignment_power); | |
3105 | arg->failed = TRUE; | |
3106 | return; | |
3107 | } | |
72de5009 | 3108 | this_hdr->sh_addralign = (bfd_vma) 1 << asect->alignment_power; |
252b5132 RH |
3109 | /* The sh_entsize and sh_info fields may have been set already by |
3110 | copy_private_section_data. */ | |
3111 | ||
3112 | this_hdr->bfd_section = asect; | |
3113 | this_hdr->contents = NULL; | |
3114 | ||
3cddba1e L |
3115 | /* If the section type is unspecified, we set it based on |
3116 | asect->flags. */ | |
98ece1b3 AM |
3117 | if ((asect->flags & SEC_GROUP) != 0) |
3118 | sh_type = SHT_GROUP; | |
98ece1b3 | 3119 | else |
94be91de | 3120 | sh_type = bfd_elf_get_default_section_type (asect->flags); |
98ece1b3 | 3121 | |
3cddba1e | 3122 | if (this_hdr->sh_type == SHT_NULL) |
98ece1b3 AM |
3123 | this_hdr->sh_type = sh_type; |
3124 | else if (this_hdr->sh_type == SHT_NOBITS | |
3125 | && sh_type == SHT_PROGBITS | |
3126 | && (asect->flags & SEC_ALLOC) != 0) | |
3cddba1e | 3127 | { |
98ece1b3 AM |
3128 | /* Warn if we are changing a NOBITS section to PROGBITS, but |
3129 | allow the link to proceed. This can happen when users link | |
3130 | non-bss input sections to bss output sections, or emit data | |
3131 | to a bss output section via a linker script. */ | |
3132 | (*_bfd_error_handler) | |
58f0869b | 3133 | (_("warning: section `%A' type changed to PROGBITS"), asect); |
98ece1b3 | 3134 | this_hdr->sh_type = sh_type; |
3cddba1e L |
3135 | } |
3136 | ||
2f89ff8d | 3137 | switch (this_hdr->sh_type) |
252b5132 | 3138 | { |
2f89ff8d | 3139 | default: |
2f89ff8d L |
3140 | break; |
3141 | ||
3142 | case SHT_STRTAB: | |
3143 | case SHT_INIT_ARRAY: | |
3144 | case SHT_FINI_ARRAY: | |
3145 | case SHT_PREINIT_ARRAY: | |
3146 | case SHT_NOTE: | |
3147 | case SHT_NOBITS: | |
3148 | case SHT_PROGBITS: | |
3149 | break; | |
3150 | ||
3151 | case SHT_HASH: | |
c7ac6ff8 | 3152 | this_hdr->sh_entsize = bed->s->sizeof_hash_entry; |
2f89ff8d | 3153 | break; |
5de3bf90 | 3154 | |
2f89ff8d | 3155 | case SHT_DYNSYM: |
252b5132 | 3156 | this_hdr->sh_entsize = bed->s->sizeof_sym; |
2f89ff8d L |
3157 | break; |
3158 | ||
3159 | case SHT_DYNAMIC: | |
252b5132 | 3160 | this_hdr->sh_entsize = bed->s->sizeof_dyn; |
2f89ff8d L |
3161 | break; |
3162 | ||
3163 | case SHT_RELA: | |
3164 | if (get_elf_backend_data (abfd)->may_use_rela_p) | |
3165 | this_hdr->sh_entsize = bed->s->sizeof_rela; | |
3166 | break; | |
3167 | ||
3168 | case SHT_REL: | |
3169 | if (get_elf_backend_data (abfd)->may_use_rel_p) | |
3170 | this_hdr->sh_entsize = bed->s->sizeof_rel; | |
3171 | break; | |
3172 | ||
3173 | case SHT_GNU_versym: | |
252b5132 | 3174 | this_hdr->sh_entsize = sizeof (Elf_External_Versym); |
2f89ff8d L |
3175 | break; |
3176 | ||
3177 | case SHT_GNU_verdef: | |
252b5132 RH |
3178 | this_hdr->sh_entsize = 0; |
3179 | /* objcopy or strip will copy over sh_info, but may not set | |
08a40648 AM |
3180 | cverdefs. The linker will set cverdefs, but sh_info will be |
3181 | zero. */ | |
252b5132 RH |
3182 | if (this_hdr->sh_info == 0) |
3183 | this_hdr->sh_info = elf_tdata (abfd)->cverdefs; | |
3184 | else | |
3185 | BFD_ASSERT (elf_tdata (abfd)->cverdefs == 0 | |
3186 | || this_hdr->sh_info == elf_tdata (abfd)->cverdefs); | |
2f89ff8d L |
3187 | break; |
3188 | ||
3189 | case SHT_GNU_verneed: | |
252b5132 RH |
3190 | this_hdr->sh_entsize = 0; |
3191 | /* objcopy or strip will copy over sh_info, but may not set | |
08a40648 AM |
3192 | cverrefs. The linker will set cverrefs, but sh_info will be |
3193 | zero. */ | |
252b5132 RH |
3194 | if (this_hdr->sh_info == 0) |
3195 | this_hdr->sh_info = elf_tdata (abfd)->cverrefs; | |
3196 | else | |
3197 | BFD_ASSERT (elf_tdata (abfd)->cverrefs == 0 | |
3198 | || this_hdr->sh_info == elf_tdata (abfd)->cverrefs); | |
2f89ff8d L |
3199 | break; |
3200 | ||
3201 | case SHT_GROUP: | |
1783205a | 3202 | this_hdr->sh_entsize = GRP_ENTRY_SIZE; |
2f89ff8d | 3203 | break; |
fdc90cb4 JJ |
3204 | |
3205 | case SHT_GNU_HASH: | |
3206 | this_hdr->sh_entsize = bed->s->arch_size == 64 ? 0 : 4; | |
3207 | break; | |
dbb410c3 | 3208 | } |
252b5132 RH |
3209 | |
3210 | if ((asect->flags & SEC_ALLOC) != 0) | |
3211 | this_hdr->sh_flags |= SHF_ALLOC; | |
3212 | if ((asect->flags & SEC_READONLY) == 0) | |
3213 | this_hdr->sh_flags |= SHF_WRITE; | |
3214 | if ((asect->flags & SEC_CODE) != 0) | |
3215 | this_hdr->sh_flags |= SHF_EXECINSTR; | |
f5fa8ca2 JJ |
3216 | if ((asect->flags & SEC_MERGE) != 0) |
3217 | { | |
3218 | this_hdr->sh_flags |= SHF_MERGE; | |
3219 | this_hdr->sh_entsize = asect->entsize; | |
f5fa8ca2 | 3220 | } |
84865015 NC |
3221 | if ((asect->flags & SEC_STRINGS) != 0) |
3222 | this_hdr->sh_flags |= SHF_STRINGS; | |
1126897b | 3223 | if ((asect->flags & SEC_GROUP) == 0 && elf_group_name (asect) != NULL) |
dbb410c3 | 3224 | this_hdr->sh_flags |= SHF_GROUP; |
13ae64f3 | 3225 | if ((asect->flags & SEC_THREAD_LOCAL) != 0) |
704afa60 JJ |
3226 | { |
3227 | this_hdr->sh_flags |= SHF_TLS; | |
3a800eb9 AM |
3228 | if (asect->size == 0 |
3229 | && (asect->flags & SEC_HAS_CONTENTS) == 0) | |
704afa60 | 3230 | { |
3a800eb9 | 3231 | struct bfd_link_order *o = asect->map_tail.link_order; |
b34976b6 | 3232 | |
704afa60 | 3233 | this_hdr->sh_size = 0; |
3a800eb9 AM |
3234 | if (o != NULL) |
3235 | { | |
704afa60 | 3236 | this_hdr->sh_size = o->offset + o->size; |
3a800eb9 AM |
3237 | if (this_hdr->sh_size != 0) |
3238 | this_hdr->sh_type = SHT_NOBITS; | |
3239 | } | |
704afa60 JJ |
3240 | } |
3241 | } | |
18ae9cc1 L |
3242 | if ((asect->flags & (SEC_GROUP | SEC_EXCLUDE)) == SEC_EXCLUDE) |
3243 | this_hdr->sh_flags |= SHF_EXCLUDE; | |
252b5132 | 3244 | |
d4730f92 BS |
3245 | /* If the section has relocs, set up a section header for the |
3246 | SHT_REL[A] section. If two relocation sections are required for | |
3247 | this section, it is up to the processor-specific back-end to | |
3248 | create the other. */ | |
3249 | if ((asect->flags & SEC_RELOC) != 0) | |
3250 | { | |
3251 | /* When doing a relocatable link, create both REL and RELA sections if | |
3252 | needed. */ | |
3253 | if (arg->link_info | |
3254 | /* Do the normal setup if we wouldn't create any sections here. */ | |
3255 | && esd->rel.count + esd->rela.count > 0 | |
0e1862bb L |
3256 | && (bfd_link_relocatable (arg->link_info) |
3257 | || arg->link_info->emitrelocations)) | |
d4730f92 BS |
3258 | { |
3259 | if (esd->rel.count && esd->rel.hdr == NULL | |
3e19fb8f L |
3260 | && !_bfd_elf_init_reloc_shdr (abfd, &esd->rel, name, FALSE, |
3261 | delay_st_name_p)) | |
d4730f92 BS |
3262 | { |
3263 | arg->failed = TRUE; | |
3264 | return; | |
3265 | } | |
3266 | if (esd->rela.count && esd->rela.hdr == NULL | |
3e19fb8f L |
3267 | && !_bfd_elf_init_reloc_shdr (abfd, &esd->rela, name, TRUE, |
3268 | delay_st_name_p)) | |
d4730f92 BS |
3269 | { |
3270 | arg->failed = TRUE; | |
3271 | return; | |
3272 | } | |
3273 | } | |
3274 | else if (!_bfd_elf_init_reloc_shdr (abfd, | |
3275 | (asect->use_rela_p | |
3276 | ? &esd->rela : &esd->rel), | |
f6fe1ccd | 3277 | name, |
3e19fb8f L |
3278 | asect->use_rela_p, |
3279 | delay_st_name_p)) | |
d4730f92 BS |
3280 | arg->failed = TRUE; |
3281 | } | |
3282 | ||
252b5132 | 3283 | /* Check for processor-specific section types. */ |
0414f35b | 3284 | sh_type = this_hdr->sh_type; |
e1fddb6b AO |
3285 | if (bed->elf_backend_fake_sections |
3286 | && !(*bed->elf_backend_fake_sections) (abfd, this_hdr, asect)) | |
d4730f92 | 3287 | arg->failed = TRUE; |
252b5132 | 3288 | |
42bb2e33 | 3289 | if (sh_type == SHT_NOBITS && asect->size != 0) |
0414f35b AM |
3290 | { |
3291 | /* Don't change the header type from NOBITS if we are being | |
42bb2e33 | 3292 | called for objcopy --only-keep-debug. */ |
0414f35b AM |
3293 | this_hdr->sh_type = sh_type; |
3294 | } | |
252b5132 RH |
3295 | } |
3296 | ||
bcacc0f5 AM |
3297 | /* Fill in the contents of a SHT_GROUP section. Called from |
3298 | _bfd_elf_compute_section_file_positions for gas, objcopy, and | |
3299 | when ELF targets use the generic linker, ld. Called for ld -r | |
3300 | from bfd_elf_final_link. */ | |
dbb410c3 | 3301 | |
1126897b | 3302 | void |
217aa764 | 3303 | bfd_elf_set_group_contents (bfd *abfd, asection *sec, void *failedptrarg) |
dbb410c3 | 3304 | { |
a50b1753 | 3305 | bfd_boolean *failedptr = (bfd_boolean *) failedptrarg; |
9dce4196 | 3306 | asection *elt, *first; |
dbb410c3 | 3307 | unsigned char *loc; |
b34976b6 | 3308 | bfd_boolean gas; |
dbb410c3 | 3309 | |
7e4111ad L |
3310 | /* Ignore linker created group section. See elfNN_ia64_object_p in |
3311 | elfxx-ia64.c. */ | |
3312 | if (((sec->flags & (SEC_GROUP | SEC_LINKER_CREATED)) != SEC_GROUP) | |
dbb410c3 AM |
3313 | || *failedptr) |
3314 | return; | |
3315 | ||
bcacc0f5 AM |
3316 | if (elf_section_data (sec)->this_hdr.sh_info == 0) |
3317 | { | |
3318 | unsigned long symindx = 0; | |
3319 | ||
3320 | /* elf_group_id will have been set up by objcopy and the | |
3321 | generic linker. */ | |
3322 | if (elf_group_id (sec) != NULL) | |
3323 | symindx = elf_group_id (sec)->udata.i; | |
1126897b | 3324 | |
bcacc0f5 AM |
3325 | if (symindx == 0) |
3326 | { | |
3327 | /* If called from the assembler, swap_out_syms will have set up | |
3328 | elf_section_syms. */ | |
3329 | BFD_ASSERT (elf_section_syms (abfd) != NULL); | |
3330 | symindx = elf_section_syms (abfd)[sec->index]->udata.i; | |
3331 | } | |
3332 | elf_section_data (sec)->this_hdr.sh_info = symindx; | |
3333 | } | |
3334 | else if (elf_section_data (sec)->this_hdr.sh_info == (unsigned int) -2) | |
1126897b | 3335 | { |
bcacc0f5 AM |
3336 | /* The ELF backend linker sets sh_info to -2 when the group |
3337 | signature symbol is global, and thus the index can't be | |
3338 | set until all local symbols are output. */ | |
3339 | asection *igroup = elf_sec_group (elf_next_in_group (sec)); | |
3340 | struct bfd_elf_section_data *sec_data = elf_section_data (igroup); | |
3341 | unsigned long symndx = sec_data->this_hdr.sh_info; | |
3342 | unsigned long extsymoff = 0; | |
3343 | struct elf_link_hash_entry *h; | |
3344 | ||
3345 | if (!elf_bad_symtab (igroup->owner)) | |
3346 | { | |
3347 | Elf_Internal_Shdr *symtab_hdr; | |
3348 | ||
3349 | symtab_hdr = &elf_tdata (igroup->owner)->symtab_hdr; | |
3350 | extsymoff = symtab_hdr->sh_info; | |
3351 | } | |
3352 | h = elf_sym_hashes (igroup->owner)[symndx - extsymoff]; | |
3353 | while (h->root.type == bfd_link_hash_indirect | |
3354 | || h->root.type == bfd_link_hash_warning) | |
3355 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
3356 | ||
3357 | elf_section_data (sec)->this_hdr.sh_info = h->indx; | |
1126897b | 3358 | } |
dbb410c3 | 3359 | |
1126897b | 3360 | /* The contents won't be allocated for "ld -r" or objcopy. */ |
b34976b6 | 3361 | gas = TRUE; |
dbb410c3 AM |
3362 | if (sec->contents == NULL) |
3363 | { | |
b34976b6 | 3364 | gas = FALSE; |
a50b1753 | 3365 | sec->contents = (unsigned char *) bfd_alloc (abfd, sec->size); |
9dce4196 AM |
3366 | |
3367 | /* Arrange for the section to be written out. */ | |
3368 | elf_section_data (sec)->this_hdr.contents = sec->contents; | |
dbb410c3 AM |
3369 | if (sec->contents == NULL) |
3370 | { | |
b34976b6 | 3371 | *failedptr = TRUE; |
dbb410c3 AM |
3372 | return; |
3373 | } | |
3374 | } | |
3375 | ||
eea6121a | 3376 | loc = sec->contents + sec->size; |
dbb410c3 | 3377 | |
9dce4196 AM |
3378 | /* Get the pointer to the first section in the group that gas |
3379 | squirreled away here. objcopy arranges for this to be set to the | |
3380 | start of the input section group. */ | |
3381 | first = elt = elf_next_in_group (sec); | |
dbb410c3 AM |
3382 | |
3383 | /* First element is a flag word. Rest of section is elf section | |
3384 | indices for all the sections of the group. Write them backwards | |
3385 | just to keep the group in the same order as given in .section | |
3386 | directives, not that it matters. */ | |
3387 | while (elt != NULL) | |
3388 | { | |
9dce4196 | 3389 | asection *s; |
9dce4196 | 3390 | |
9dce4196 | 3391 | s = elt; |
415f38a6 AM |
3392 | if (!gas) |
3393 | s = s->output_section; | |
3394 | if (s != NULL | |
3395 | && !bfd_is_abs_section (s)) | |
01e1a5bc | 3396 | { |
415f38a6 AM |
3397 | unsigned int idx = elf_section_data (s)->this_idx; |
3398 | ||
01e1a5bc | 3399 | loc -= 4; |
01e1a5bc NC |
3400 | H_PUT_32 (abfd, idx, loc); |
3401 | } | |
945906ff | 3402 | elt = elf_next_in_group (elt); |
9dce4196 AM |
3403 | if (elt == first) |
3404 | break; | |
dbb410c3 AM |
3405 | } |
3406 | ||
3d7f7666 | 3407 | if ((loc -= 4) != sec->contents) |
9dce4196 | 3408 | abort (); |
dbb410c3 | 3409 | |
9dce4196 | 3410 | H_PUT_32 (abfd, sec->flags & SEC_LINK_ONCE ? GRP_COMDAT : 0, loc); |
dbb410c3 AM |
3411 | } |
3412 | ||
bd53a53a L |
3413 | /* Return the section which RELOC_SEC applies to. */ |
3414 | ||
3415 | asection * | |
3416 | _bfd_elf_get_reloc_section (asection *reloc_sec) | |
3417 | { | |
3418 | const char *name; | |
3419 | unsigned int type; | |
3420 | bfd *abfd; | |
3421 | ||
3422 | if (reloc_sec == NULL) | |
3423 | return NULL; | |
3424 | ||
3425 | type = elf_section_data (reloc_sec)->this_hdr.sh_type; | |
3426 | if (type != SHT_REL && type != SHT_RELA) | |
3427 | return NULL; | |
3428 | ||
3429 | /* We look up the section the relocs apply to by name. */ | |
3430 | name = reloc_sec->name; | |
3431 | if (type == SHT_REL) | |
3432 | name += 4; | |
3433 | else | |
3434 | name += 5; | |
3435 | ||
3436 | /* If a target needs .got.plt section, relocations in rela.plt/rel.plt | |
3437 | section apply to .got.plt section. */ | |
3438 | abfd = reloc_sec->owner; | |
3439 | if (get_elf_backend_data (abfd)->want_got_plt | |
3440 | && strcmp (name, ".plt") == 0) | |
87070c08 AM |
3441 | { |
3442 | /* .got.plt is a linker created input section. It may be mapped | |
3443 | to some other output section. Try two likely sections. */ | |
3444 | name = ".got.plt"; | |
3445 | reloc_sec = bfd_get_section_by_name (abfd, name); | |
3446 | if (reloc_sec != NULL) | |
3447 | return reloc_sec; | |
3448 | name = ".got"; | |
3449 | } | |
bd53a53a L |
3450 | |
3451 | reloc_sec = bfd_get_section_by_name (abfd, name); | |
3452 | return reloc_sec; | |
3453 | } | |
3454 | ||
252b5132 RH |
3455 | /* Assign all ELF section numbers. The dummy first section is handled here |
3456 | too. The link/info pointers for the standard section types are filled | |
3457 | in here too, while we're at it. */ | |
3458 | ||
b34976b6 | 3459 | static bfd_boolean |
da9f89d4 | 3460 | assign_section_numbers (bfd *abfd, struct bfd_link_info *link_info) |
252b5132 RH |
3461 | { |
3462 | struct elf_obj_tdata *t = elf_tdata (abfd); | |
3463 | asection *sec; | |
3e19fb8f | 3464 | unsigned int section_number; |
252b5132 | 3465 | Elf_Internal_Shdr **i_shdrp; |
47cc2cf5 | 3466 | struct bfd_elf_section_data *d; |
3516e984 | 3467 | bfd_boolean need_symtab; |
252b5132 RH |
3468 | |
3469 | section_number = 1; | |
3470 | ||
2b0f7ef9 JJ |
3471 | _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd)); |
3472 | ||
da9f89d4 | 3473 | /* SHT_GROUP sections are in relocatable files only. */ |
0e1862bb | 3474 | if (link_info == NULL || bfd_link_relocatable (link_info)) |
252b5132 | 3475 | { |
14f2c699 L |
3476 | bfd_size_type reloc_count = 0; |
3477 | ||
da9f89d4 | 3478 | /* Put SHT_GROUP sections first. */ |
04dd1667 | 3479 | for (sec = abfd->sections; sec != NULL; sec = sec->next) |
47cc2cf5 | 3480 | { |
5daa8fe7 | 3481 | d = elf_section_data (sec); |
da9f89d4 L |
3482 | |
3483 | if (d->this_hdr.sh_type == SHT_GROUP) | |
08a40648 | 3484 | { |
5daa8fe7 | 3485 | if (sec->flags & SEC_LINKER_CREATED) |
da9f89d4 L |
3486 | { |
3487 | /* Remove the linker created SHT_GROUP sections. */ | |
5daa8fe7 | 3488 | bfd_section_list_remove (abfd, sec); |
da9f89d4 | 3489 | abfd->section_count--; |
da9f89d4 | 3490 | } |
08a40648 | 3491 | else |
4fbb74a6 | 3492 | d->this_idx = section_number++; |
da9f89d4 | 3493 | } |
14f2c699 L |
3494 | |
3495 | /* Count relocations. */ | |
3496 | reloc_count += sec->reloc_count; | |
47cc2cf5 | 3497 | } |
14f2c699 L |
3498 | |
3499 | /* Clear HAS_RELOC if there are no relocations. */ | |
3500 | if (reloc_count == 0) | |
3501 | abfd->flags &= ~HAS_RELOC; | |
47cc2cf5 PB |
3502 | } |
3503 | ||
3504 | for (sec = abfd->sections; sec; sec = sec->next) | |
3505 | { | |
3506 | d = elf_section_data (sec); | |
3507 | ||
3508 | if (d->this_hdr.sh_type != SHT_GROUP) | |
4fbb74a6 | 3509 | d->this_idx = section_number++; |
3e19fb8f L |
3510 | if (d->this_hdr.sh_name != (unsigned int) -1) |
3511 | _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->this_hdr.sh_name); | |
d4730f92 | 3512 | if (d->rel.hdr) |
2b0f7ef9 | 3513 | { |
d4730f92 | 3514 | d->rel.idx = section_number++; |
3e19fb8f L |
3515 | if (d->rel.hdr->sh_name != (unsigned int) -1) |
3516 | _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel.hdr->sh_name); | |
2b0f7ef9 | 3517 | } |
d4730f92 BS |
3518 | else |
3519 | d->rel.idx = 0; | |
23bc299b | 3520 | |
d4730f92 | 3521 | if (d->rela.hdr) |
2b0f7ef9 | 3522 | { |
d4730f92 | 3523 | d->rela.idx = section_number++; |
3e19fb8f L |
3524 | if (d->rela.hdr->sh_name != (unsigned int) -1) |
3525 | _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rela.hdr->sh_name); | |
2b0f7ef9 | 3526 | } |
23bc299b | 3527 | else |
d4730f92 | 3528 | d->rela.idx = 0; |
252b5132 RH |
3529 | } |
3530 | ||
12bd6957 | 3531 | elf_shstrtab_sec (abfd) = section_number++; |
2b0f7ef9 | 3532 | _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->shstrtab_hdr.sh_name); |
12bd6957 | 3533 | elf_elfheader (abfd)->e_shstrndx = elf_shstrtab_sec (abfd); |
252b5132 | 3534 | |
3516e984 L |
3535 | need_symtab = (bfd_get_symcount (abfd) > 0 |
3536 | || (link_info == NULL | |
3537 | && ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC)) | |
3538 | == HAS_RELOC))); | |
3539 | if (need_symtab) | |
252b5132 | 3540 | { |
12bd6957 | 3541 | elf_onesymtab (abfd) = section_number++; |
2b0f7ef9 | 3542 | _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->symtab_hdr.sh_name); |
4fbb74a6 | 3543 | if (section_number > ((SHN_LORESERVE - 2) & 0xFFFF)) |
9ad5cbcf | 3544 | { |
6a40cf0c NC |
3545 | elf_section_list * entry; |
3546 | ||
3547 | BFD_ASSERT (elf_symtab_shndx_list (abfd) == NULL); | |
3548 | ||
3549 | entry = bfd_zalloc (abfd, sizeof * entry); | |
3550 | entry->ndx = section_number++; | |
3551 | elf_symtab_shndx_list (abfd) = entry; | |
3552 | entry->hdr.sh_name | |
9ad5cbcf | 3553 | = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), |
b34976b6 | 3554 | ".symtab_shndx", FALSE); |
6a40cf0c | 3555 | if (entry->hdr.sh_name == (unsigned int) -1) |
b34976b6 | 3556 | return FALSE; |
9ad5cbcf | 3557 | } |
12bd6957 | 3558 | elf_strtab_sec (abfd) = section_number++; |
2b0f7ef9 | 3559 | _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->strtab_hdr.sh_name); |
252b5132 RH |
3560 | } |
3561 | ||
1c52a645 L |
3562 | if (section_number >= SHN_LORESERVE) |
3563 | { | |
3564 | _bfd_error_handler (_("%B: too many sections: %u"), | |
3565 | abfd, section_number); | |
3566 | return FALSE; | |
3567 | } | |
3568 | ||
9ad5cbcf | 3569 | elf_numsections (abfd) = section_number; |
252b5132 RH |
3570 | elf_elfheader (abfd)->e_shnum = section_number; |
3571 | ||
3572 | /* Set up the list of section header pointers, in agreement with the | |
3573 | indices. */ | |
a50b1753 NC |
3574 | i_shdrp = (Elf_Internal_Shdr **) bfd_zalloc2 (abfd, section_number, |
3575 | sizeof (Elf_Internal_Shdr *)); | |
252b5132 | 3576 | if (i_shdrp == NULL) |
b34976b6 | 3577 | return FALSE; |
252b5132 | 3578 | |
a50b1753 NC |
3579 | i_shdrp[0] = (Elf_Internal_Shdr *) bfd_zalloc (abfd, |
3580 | sizeof (Elf_Internal_Shdr)); | |
252b5132 RH |
3581 | if (i_shdrp[0] == NULL) |
3582 | { | |
3583 | bfd_release (abfd, i_shdrp); | |
b34976b6 | 3584 | return FALSE; |
252b5132 | 3585 | } |
252b5132 RH |
3586 | |
3587 | elf_elfsections (abfd) = i_shdrp; | |
3588 | ||
12bd6957 | 3589 | i_shdrp[elf_shstrtab_sec (abfd)] = &t->shstrtab_hdr; |
3516e984 | 3590 | if (need_symtab) |
252b5132 | 3591 | { |
12bd6957 | 3592 | i_shdrp[elf_onesymtab (abfd)] = &t->symtab_hdr; |
4fbb74a6 | 3593 | if (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF)) |
9ad5cbcf | 3594 | { |
6a40cf0c NC |
3595 | elf_section_list * entry = elf_symtab_shndx_list (abfd); |
3596 | BFD_ASSERT (entry != NULL); | |
3597 | i_shdrp[entry->ndx] = & entry->hdr; | |
3598 | entry->hdr.sh_link = elf_onesymtab (abfd); | |
9ad5cbcf | 3599 | } |
12bd6957 AM |
3600 | i_shdrp[elf_strtab_sec (abfd)] = &t->strtab_hdr; |
3601 | t->symtab_hdr.sh_link = elf_strtab_sec (abfd); | |
252b5132 | 3602 | } |
38ce5b11 | 3603 | |
252b5132 RH |
3604 | for (sec = abfd->sections; sec; sec = sec->next) |
3605 | { | |
252b5132 | 3606 | asection *s; |
252b5132 | 3607 | |
91d6fa6a NC |
3608 | d = elf_section_data (sec); |
3609 | ||
252b5132 | 3610 | i_shdrp[d->this_idx] = &d->this_hdr; |
d4730f92 BS |
3611 | if (d->rel.idx != 0) |
3612 | i_shdrp[d->rel.idx] = d->rel.hdr; | |
3613 | if (d->rela.idx != 0) | |
3614 | i_shdrp[d->rela.idx] = d->rela.hdr; | |
252b5132 RH |
3615 | |
3616 | /* Fill in the sh_link and sh_info fields while we're at it. */ | |
3617 | ||
3618 | /* sh_link of a reloc section is the section index of the symbol | |
3619 | table. sh_info is the section index of the section to which | |
3620 | the relocation entries apply. */ | |
d4730f92 | 3621 | if (d->rel.idx != 0) |
252b5132 | 3622 | { |
12bd6957 | 3623 | d->rel.hdr->sh_link = elf_onesymtab (abfd); |
d4730f92 | 3624 | d->rel.hdr->sh_info = d->this_idx; |
9ef5d938 | 3625 | d->rel.hdr->sh_flags |= SHF_INFO_LINK; |
252b5132 | 3626 | } |
d4730f92 | 3627 | if (d->rela.idx != 0) |
23bc299b | 3628 | { |
12bd6957 | 3629 | d->rela.hdr->sh_link = elf_onesymtab (abfd); |
d4730f92 | 3630 | d->rela.hdr->sh_info = d->this_idx; |
9ef5d938 | 3631 | d->rela.hdr->sh_flags |= SHF_INFO_LINK; |
23bc299b | 3632 | } |
252b5132 | 3633 | |
38ce5b11 L |
3634 | /* We need to set up sh_link for SHF_LINK_ORDER. */ |
3635 | if ((d->this_hdr.sh_flags & SHF_LINK_ORDER) != 0) | |
3636 | { | |
3637 | s = elf_linked_to_section (sec); | |
3638 | if (s) | |
38ce5b11 | 3639 | { |
f2876037 | 3640 | /* elf_linked_to_section points to the input section. */ |
ccd2ec6a | 3641 | if (link_info != NULL) |
38ce5b11 | 3642 | { |
f2876037 | 3643 | /* Check discarded linkonce section. */ |
dbaa2011 | 3644 | if (discarded_section (s)) |
38ce5b11 | 3645 | { |
ccd2ec6a L |
3646 | asection *kept; |
3647 | (*_bfd_error_handler) | |
3648 | (_("%B: sh_link of section `%A' points to discarded section `%A' of `%B'"), | |
3649 | abfd, d->this_hdr.bfd_section, | |
3650 | s, s->owner); | |
3651 | /* Point to the kept section if it has the same | |
3652 | size as the discarded one. */ | |
c0f00686 | 3653 | kept = _bfd_elf_check_kept_section (s, link_info); |
ccd2ec6a | 3654 | if (kept == NULL) |
185d09ad | 3655 | { |
ccd2ec6a L |
3656 | bfd_set_error (bfd_error_bad_value); |
3657 | return FALSE; | |
185d09ad | 3658 | } |
ccd2ec6a | 3659 | s = kept; |
38ce5b11 | 3660 | } |
e424ecc8 | 3661 | |
ccd2ec6a L |
3662 | s = s->output_section; |
3663 | BFD_ASSERT (s != NULL); | |
38ce5b11 | 3664 | } |
f2876037 L |
3665 | else |
3666 | { | |
3667 | /* Handle objcopy. */ | |
3668 | if (s->output_section == NULL) | |
3669 | { | |
3670 | (*_bfd_error_handler) | |
3671 | (_("%B: sh_link of section `%A' points to removed section `%A' of `%B'"), | |
3672 | abfd, d->this_hdr.bfd_section, s, s->owner); | |
3673 | bfd_set_error (bfd_error_bad_value); | |
3674 | return FALSE; | |
3675 | } | |
3676 | s = s->output_section; | |
3677 | } | |
ccd2ec6a L |
3678 | d->this_hdr.sh_link = elf_section_data (s)->this_idx; |
3679 | } | |
3680 | else | |
3681 | { | |
3682 | /* PR 290: | |
3683 | The Intel C compiler generates SHT_IA_64_UNWIND with | |
3684 | SHF_LINK_ORDER. But it doesn't set the sh_link or | |
3685 | sh_info fields. Hence we could get the situation | |
08a40648 | 3686 | where s is NULL. */ |
ccd2ec6a L |
3687 | const struct elf_backend_data *bed |
3688 | = get_elf_backend_data (abfd); | |
3689 | if (bed->link_order_error_handler) | |
3690 | bed->link_order_error_handler | |
3691 | (_("%B: warning: sh_link not set for section `%A'"), | |
3692 | abfd, sec); | |
38ce5b11 L |
3693 | } |
3694 | } | |
3695 | ||
252b5132 RH |
3696 | switch (d->this_hdr.sh_type) |
3697 | { | |
3698 | case SHT_REL: | |
3699 | case SHT_RELA: | |
3700 | /* A reloc section which we are treating as a normal BFD | |
3701 | section. sh_link is the section index of the symbol | |
3702 | table. sh_info is the section index of the section to | |
3703 | which the relocation entries apply. We assume that an | |
3704 | allocated reloc section uses the dynamic symbol table. | |
3705 | FIXME: How can we be sure? */ | |
3706 | s = bfd_get_section_by_name (abfd, ".dynsym"); | |
3707 | if (s != NULL) | |
3708 | d->this_hdr.sh_link = elf_section_data (s)->this_idx; | |
3709 | ||
bd53a53a | 3710 | s = get_elf_backend_data (abfd)->get_reloc_section (sec); |
252b5132 | 3711 | if (s != NULL) |
9ef5d938 L |
3712 | { |
3713 | d->this_hdr.sh_info = elf_section_data (s)->this_idx; | |
3714 | d->this_hdr.sh_flags |= SHF_INFO_LINK; | |
3715 | } | |
252b5132 RH |
3716 | break; |
3717 | ||
3718 | case SHT_STRTAB: | |
3719 | /* We assume that a section named .stab*str is a stabs | |
3720 | string section. We look for a section with the same name | |
3721 | but without the trailing ``str'', and set its sh_link | |
3722 | field to point to this section. */ | |
0112cd26 | 3723 | if (CONST_STRNEQ (sec->name, ".stab") |
252b5132 RH |
3724 | && strcmp (sec->name + strlen (sec->name) - 3, "str") == 0) |
3725 | { | |
3726 | size_t len; | |
3727 | char *alc; | |
3728 | ||
3729 | len = strlen (sec->name); | |
a50b1753 | 3730 | alc = (char *) bfd_malloc (len - 2); |
252b5132 | 3731 | if (alc == NULL) |
b34976b6 | 3732 | return FALSE; |
d4c88bbb | 3733 | memcpy (alc, sec->name, len - 3); |
252b5132 RH |
3734 | alc[len - 3] = '\0'; |
3735 | s = bfd_get_section_by_name (abfd, alc); | |
3736 | free (alc); | |
3737 | if (s != NULL) | |
3738 | { | |
3739 | elf_section_data (s)->this_hdr.sh_link = d->this_idx; | |
3740 | ||
3741 | /* This is a .stab section. */ | |
0594c12d AM |
3742 | if (elf_section_data (s)->this_hdr.sh_entsize == 0) |
3743 | elf_section_data (s)->this_hdr.sh_entsize | |
3744 | = 4 + 2 * bfd_get_arch_size (abfd) / 8; | |
252b5132 RH |
3745 | } |
3746 | } | |
3747 | break; | |
3748 | ||
3749 | case SHT_DYNAMIC: | |
3750 | case SHT_DYNSYM: | |
3751 | case SHT_GNU_verneed: | |
3752 | case SHT_GNU_verdef: | |
3753 | /* sh_link is the section header index of the string table | |
3754 | used for the dynamic entries, or the symbol table, or the | |
3755 | version strings. */ | |
3756 | s = bfd_get_section_by_name (abfd, ".dynstr"); | |
3757 | if (s != NULL) | |
3758 | d->this_hdr.sh_link = elf_section_data (s)->this_idx; | |
3759 | break; | |
3760 | ||
7f1204bb JJ |
3761 | case SHT_GNU_LIBLIST: |
3762 | /* sh_link is the section header index of the prelink library | |
08a40648 AM |
3763 | list used for the dynamic entries, or the symbol table, or |
3764 | the version strings. */ | |
7f1204bb JJ |
3765 | s = bfd_get_section_by_name (abfd, (sec->flags & SEC_ALLOC) |
3766 | ? ".dynstr" : ".gnu.libstr"); | |
3767 | if (s != NULL) | |
3768 | d->this_hdr.sh_link = elf_section_data (s)->this_idx; | |
3769 | break; | |
3770 | ||
252b5132 | 3771 | case SHT_HASH: |
fdc90cb4 | 3772 | case SHT_GNU_HASH: |
252b5132 RH |
3773 | case SHT_GNU_versym: |
3774 | /* sh_link is the section header index of the symbol table | |
3775 | this hash table or version table is for. */ | |
3776 | s = bfd_get_section_by_name (abfd, ".dynsym"); | |
3777 | if (s != NULL) | |
3778 | d->this_hdr.sh_link = elf_section_data (s)->this_idx; | |
3779 | break; | |
dbb410c3 AM |
3780 | |
3781 | case SHT_GROUP: | |
12bd6957 | 3782 | d->this_hdr.sh_link = elf_onesymtab (abfd); |
252b5132 RH |
3783 | } |
3784 | } | |
3785 | ||
3e19fb8f L |
3786 | /* Delay setting sh_name to _bfd_elf_write_object_contents so that |
3787 | _bfd_elf_assign_file_positions_for_non_load can convert DWARF | |
3788 | debug section name from .debug_* to .zdebug_* if needed. */ | |
3789 | ||
b34976b6 | 3790 | return TRUE; |
252b5132 RH |
3791 | } |
3792 | ||
5372391b | 3793 | static bfd_boolean |
217aa764 | 3794 | sym_is_global (bfd *abfd, asymbol *sym) |
252b5132 RH |
3795 | { |
3796 | /* If the backend has a special mapping, use it. */ | |
9c5bfbb7 | 3797 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
217aa764 AM |
3798 | if (bed->elf_backend_sym_is_global) |
3799 | return (*bed->elf_backend_sym_is_global) (abfd, sym); | |
252b5132 | 3800 | |
e47bf690 | 3801 | return ((sym->flags & (BSF_GLOBAL | BSF_WEAK | BSF_GNU_UNIQUE)) != 0 |
252b5132 RH |
3802 | || bfd_is_und_section (bfd_get_section (sym)) |
3803 | || bfd_is_com_section (bfd_get_section (sym))); | |
3804 | } | |
3805 | ||
5372391b | 3806 | /* Don't output section symbols for sections that are not going to be |
c6d8cab4 | 3807 | output, that are duplicates or there is no BFD section. */ |
5372391b AM |
3808 | |
3809 | static bfd_boolean | |
3810 | ignore_section_sym (bfd *abfd, asymbol *sym) | |
3811 | { | |
c6d8cab4 L |
3812 | elf_symbol_type *type_ptr; |
3813 | ||
3814 | if ((sym->flags & BSF_SECTION_SYM) == 0) | |
3815 | return FALSE; | |
3816 | ||
3817 | type_ptr = elf_symbol_from (abfd, sym); | |
3818 | return ((type_ptr != NULL | |
3819 | && type_ptr->internal_elf_sym.st_shndx != 0 | |
3820 | && bfd_is_abs_section (sym->section)) | |
3821 | || !(sym->section->owner == abfd | |
0f0a5e58 | 3822 | || (sym->section->output_section->owner == abfd |
2633a79c AM |
3823 | && sym->section->output_offset == 0) |
3824 | || bfd_is_abs_section (sym->section))); | |
5372391b AM |
3825 | } |
3826 | ||
2633a79c AM |
3827 | /* Map symbol from it's internal number to the external number, moving |
3828 | all local symbols to be at the head of the list. */ | |
3829 | ||
b34976b6 | 3830 | static bfd_boolean |
12bd6957 | 3831 | elf_map_symbols (bfd *abfd, unsigned int *pnum_locals) |
252b5132 | 3832 | { |
dc810e39 | 3833 | unsigned int symcount = bfd_get_symcount (abfd); |
252b5132 RH |
3834 | asymbol **syms = bfd_get_outsymbols (abfd); |
3835 | asymbol **sect_syms; | |
dc810e39 AM |
3836 | unsigned int num_locals = 0; |
3837 | unsigned int num_globals = 0; | |
3838 | unsigned int num_locals2 = 0; | |
3839 | unsigned int num_globals2 = 0; | |
7292b3ac | 3840 | unsigned int max_index = 0; |
dc810e39 | 3841 | unsigned int idx; |
252b5132 RH |
3842 | asection *asect; |
3843 | asymbol **new_syms; | |
252b5132 RH |
3844 | |
3845 | #ifdef DEBUG | |
3846 | fprintf (stderr, "elf_map_symbols\n"); | |
3847 | fflush (stderr); | |
3848 | #endif | |
3849 | ||
252b5132 RH |
3850 | for (asect = abfd->sections; asect; asect = asect->next) |
3851 | { | |
3852 | if (max_index < asect->index) | |
3853 | max_index = asect->index; | |
3854 | } | |
3855 | ||
3856 | max_index++; | |
a50b1753 | 3857 | sect_syms = (asymbol **) bfd_zalloc2 (abfd, max_index, sizeof (asymbol *)); |
252b5132 | 3858 | if (sect_syms == NULL) |
b34976b6 | 3859 | return FALSE; |
252b5132 | 3860 | elf_section_syms (abfd) = sect_syms; |
4e89ac30 | 3861 | elf_num_section_syms (abfd) = max_index; |
252b5132 | 3862 | |
079e9a2f AM |
3863 | /* Init sect_syms entries for any section symbols we have already |
3864 | decided to output. */ | |
252b5132 RH |
3865 | for (idx = 0; idx < symcount; idx++) |
3866 | { | |
dc810e39 | 3867 | asymbol *sym = syms[idx]; |
c044fabd | 3868 | |
252b5132 | 3869 | if ((sym->flags & BSF_SECTION_SYM) != 0 |
0f0a5e58 | 3870 | && sym->value == 0 |
2633a79c AM |
3871 | && !ignore_section_sym (abfd, sym) |
3872 | && !bfd_is_abs_section (sym->section)) | |
252b5132 | 3873 | { |
5372391b | 3874 | asection *sec = sym->section; |
252b5132 | 3875 | |
5372391b AM |
3876 | if (sec->owner != abfd) |
3877 | sec = sec->output_section; | |
252b5132 | 3878 | |
5372391b | 3879 | sect_syms[sec->index] = syms[idx]; |
252b5132 RH |
3880 | } |
3881 | } | |
3882 | ||
252b5132 RH |
3883 | /* Classify all of the symbols. */ |
3884 | for (idx = 0; idx < symcount; idx++) | |
3885 | { | |
2633a79c | 3886 | if (sym_is_global (abfd, syms[idx])) |
252b5132 | 3887 | num_globals++; |
2633a79c AM |
3888 | else if (!ignore_section_sym (abfd, syms[idx])) |
3889 | num_locals++; | |
252b5132 | 3890 | } |
079e9a2f | 3891 | |
5372391b | 3892 | /* We will be adding a section symbol for each normal BFD section. Most |
079e9a2f AM |
3893 | sections will already have a section symbol in outsymbols, but |
3894 | eg. SHT_GROUP sections will not, and we need the section symbol mapped | |
3895 | at least in that case. */ | |
252b5132 RH |
3896 | for (asect = abfd->sections; asect; asect = asect->next) |
3897 | { | |
079e9a2f | 3898 | if (sect_syms[asect->index] == NULL) |
252b5132 | 3899 | { |
079e9a2f | 3900 | if (!sym_is_global (abfd, asect->symbol)) |
252b5132 RH |
3901 | num_locals++; |
3902 | else | |
3903 | num_globals++; | |
252b5132 RH |
3904 | } |
3905 | } | |
3906 | ||
3907 | /* Now sort the symbols so the local symbols are first. */ | |
a50b1753 NC |
3908 | new_syms = (asymbol **) bfd_alloc2 (abfd, num_locals + num_globals, |
3909 | sizeof (asymbol *)); | |
dc810e39 | 3910 | |
252b5132 | 3911 | if (new_syms == NULL) |
b34976b6 | 3912 | return FALSE; |
252b5132 RH |
3913 | |
3914 | for (idx = 0; idx < symcount; idx++) | |
3915 | { | |
3916 | asymbol *sym = syms[idx]; | |
dc810e39 | 3917 | unsigned int i; |
252b5132 | 3918 | |
2633a79c AM |
3919 | if (sym_is_global (abfd, sym)) |
3920 | i = num_locals + num_globals2++; | |
3921 | else if (!ignore_section_sym (abfd, sym)) | |
252b5132 RH |
3922 | i = num_locals2++; |
3923 | else | |
2633a79c | 3924 | continue; |
252b5132 RH |
3925 | new_syms[i] = sym; |
3926 | sym->udata.i = i + 1; | |
3927 | } | |
3928 | for (asect = abfd->sections; asect; asect = asect->next) | |
3929 | { | |
079e9a2f | 3930 | if (sect_syms[asect->index] == NULL) |
252b5132 | 3931 | { |
079e9a2f | 3932 | asymbol *sym = asect->symbol; |
dc810e39 | 3933 | unsigned int i; |
252b5132 | 3934 | |
079e9a2f | 3935 | sect_syms[asect->index] = sym; |
252b5132 RH |
3936 | if (!sym_is_global (abfd, sym)) |
3937 | i = num_locals2++; | |
3938 | else | |
3939 | i = num_locals + num_globals2++; | |
3940 | new_syms[i] = sym; | |
3941 | sym->udata.i = i + 1; | |
3942 | } | |
3943 | } | |
3944 | ||
3945 | bfd_set_symtab (abfd, new_syms, num_locals + num_globals); | |
3946 | ||
12bd6957 | 3947 | *pnum_locals = num_locals; |
b34976b6 | 3948 | return TRUE; |
252b5132 RH |
3949 | } |
3950 | ||
3951 | /* Align to the maximum file alignment that could be required for any | |
3952 | ELF data structure. */ | |
3953 | ||
268b6b39 | 3954 | static inline file_ptr |
217aa764 | 3955 | align_file_position (file_ptr off, int align) |
252b5132 RH |
3956 | { |
3957 | return (off + align - 1) & ~(align - 1); | |
3958 | } | |
3959 | ||
3960 | /* Assign a file position to a section, optionally aligning to the | |
3961 | required section alignment. */ | |
3962 | ||
217aa764 AM |
3963 | file_ptr |
3964 | _bfd_elf_assign_file_position_for_section (Elf_Internal_Shdr *i_shdrp, | |
3965 | file_ptr offset, | |
3966 | bfd_boolean align) | |
252b5132 | 3967 | { |
72de5009 AM |
3968 | if (align && i_shdrp->sh_addralign > 1) |
3969 | offset = BFD_ALIGN (offset, i_shdrp->sh_addralign); | |
252b5132 RH |
3970 | i_shdrp->sh_offset = offset; |
3971 | if (i_shdrp->bfd_section != NULL) | |
3972 | i_shdrp->bfd_section->filepos = offset; | |
3973 | if (i_shdrp->sh_type != SHT_NOBITS) | |
3974 | offset += i_shdrp->sh_size; | |
3975 | return offset; | |
3976 | } | |
3977 | ||
3978 | /* Compute the file positions we are going to put the sections at, and | |
3979 | otherwise prepare to begin writing out the ELF file. If LINK_INFO | |
3980 | is not NULL, this is being called by the ELF backend linker. */ | |
3981 | ||
b34976b6 | 3982 | bfd_boolean |
217aa764 AM |
3983 | _bfd_elf_compute_section_file_positions (bfd *abfd, |
3984 | struct bfd_link_info *link_info) | |
252b5132 | 3985 | { |
9c5bfbb7 | 3986 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
d4730f92 | 3987 | struct fake_section_arg fsargs; |
b34976b6 | 3988 | bfd_boolean failed; |
ef10c3ac | 3989 | struct elf_strtab_hash *strtab = NULL; |
252b5132 | 3990 | Elf_Internal_Shdr *shstrtab_hdr; |
3516e984 | 3991 | bfd_boolean need_symtab; |
252b5132 RH |
3992 | |
3993 | if (abfd->output_has_begun) | |
b34976b6 | 3994 | return TRUE; |
252b5132 RH |
3995 | |
3996 | /* Do any elf backend specific processing first. */ | |
3997 | if (bed->elf_backend_begin_write_processing) | |
3998 | (*bed->elf_backend_begin_write_processing) (abfd, link_info); | |
3999 | ||
4000 | if (! prep_headers (abfd)) | |
b34976b6 | 4001 | return FALSE; |
252b5132 | 4002 | |
e6c51ed4 | 4003 | /* Post process the headers if necessary. */ |
78245035 | 4004 | (*bed->elf_backend_post_process_headers) (abfd, link_info); |
e6c51ed4 | 4005 | |
d4730f92 BS |
4006 | fsargs.failed = FALSE; |
4007 | fsargs.link_info = link_info; | |
4008 | bfd_map_over_sections (abfd, elf_fake_sections, &fsargs); | |
4009 | if (fsargs.failed) | |
b34976b6 | 4010 | return FALSE; |
252b5132 | 4011 | |
da9f89d4 | 4012 | if (!assign_section_numbers (abfd, link_info)) |
b34976b6 | 4013 | return FALSE; |
252b5132 RH |
4014 | |
4015 | /* The backend linker builds symbol table information itself. */ | |
3516e984 L |
4016 | need_symtab = (link_info == NULL |
4017 | && (bfd_get_symcount (abfd) > 0 | |
4018 | || ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC)) | |
4019 | == HAS_RELOC))); | |
4020 | if (need_symtab) | |
252b5132 RH |
4021 | { |
4022 | /* Non-zero if doing a relocatable link. */ | |
4023 | int relocatable_p = ! (abfd->flags & (EXEC_P | DYNAMIC)); | |
4024 | ||
4025 | if (! swap_out_syms (abfd, &strtab, relocatable_p)) | |
b34976b6 | 4026 | return FALSE; |
252b5132 RH |
4027 | } |
4028 | ||
d4730f92 | 4029 | failed = FALSE; |
1126897b | 4030 | if (link_info == NULL) |
dbb410c3 | 4031 | { |
1126897b | 4032 | bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed); |
dbb410c3 | 4033 | if (failed) |
b34976b6 | 4034 | return FALSE; |
dbb410c3 AM |
4035 | } |
4036 | ||
252b5132 RH |
4037 | shstrtab_hdr = &elf_tdata (abfd)->shstrtab_hdr; |
4038 | /* sh_name was set in prep_headers. */ | |
4039 | shstrtab_hdr->sh_type = SHT_STRTAB; | |
84865015 | 4040 | shstrtab_hdr->sh_flags = bed->elf_strtab_flags; |
252b5132 | 4041 | shstrtab_hdr->sh_addr = 0; |
946748d5 | 4042 | /* sh_size is set in _bfd_elf_assign_file_positions_for_non_load. */ |
252b5132 RH |
4043 | shstrtab_hdr->sh_entsize = 0; |
4044 | shstrtab_hdr->sh_link = 0; | |
4045 | shstrtab_hdr->sh_info = 0; | |
3e19fb8f | 4046 | /* sh_offset is set in _bfd_elf_assign_file_positions_for_non_load. */ |
252b5132 RH |
4047 | shstrtab_hdr->sh_addralign = 1; |
4048 | ||
c84fca4d | 4049 | if (!assign_file_positions_except_relocs (abfd, link_info)) |
b34976b6 | 4050 | return FALSE; |
252b5132 | 4051 | |
3516e984 | 4052 | if (need_symtab) |
252b5132 RH |
4053 | { |
4054 | file_ptr off; | |
4055 | Elf_Internal_Shdr *hdr; | |
4056 | ||
12bd6957 | 4057 | off = elf_next_file_pos (abfd); |
252b5132 | 4058 | |
6a40cf0c | 4059 | hdr = & elf_symtab_hdr (abfd); |
b34976b6 | 4060 | off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE); |
252b5132 | 4061 | |
6a40cf0c NC |
4062 | if (elf_symtab_shndx_list (abfd) != NULL) |
4063 | { | |
4064 | hdr = & elf_symtab_shndx_list (abfd)->hdr; | |
4065 | if (hdr->sh_size != 0) | |
4066 | off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE); | |
4067 | /* FIXME: What about other symtab_shndx sections in the list ? */ | |
4068 | } | |
9ad5cbcf | 4069 | |
252b5132 | 4070 | hdr = &elf_tdata (abfd)->strtab_hdr; |
b34976b6 | 4071 | off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE); |
252b5132 | 4072 | |
12bd6957 | 4073 | elf_next_file_pos (abfd) = off; |
252b5132 RH |
4074 | |
4075 | /* Now that we know where the .strtab section goes, write it | |
08a40648 | 4076 | out. */ |
252b5132 | 4077 | if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0 |
ef10c3ac | 4078 | || ! _bfd_elf_strtab_emit (abfd, strtab)) |
b34976b6 | 4079 | return FALSE; |
ef10c3ac | 4080 | _bfd_elf_strtab_free (strtab); |
252b5132 RH |
4081 | } |
4082 | ||
b34976b6 | 4083 | abfd->output_has_begun = TRUE; |
252b5132 | 4084 | |
b34976b6 | 4085 | return TRUE; |
252b5132 RH |
4086 | } |
4087 | ||
8ded5a0f AM |
4088 | /* Make an initial estimate of the size of the program header. If we |
4089 | get the number wrong here, we'll redo section placement. */ | |
4090 | ||
4091 | static bfd_size_type | |
4092 | get_program_header_size (bfd *abfd, struct bfd_link_info *info) | |
4093 | { | |
4094 | size_t segs; | |
4095 | asection *s; | |
2b05f1b7 | 4096 | const struct elf_backend_data *bed; |
8ded5a0f AM |
4097 | |
4098 | /* Assume we will need exactly two PT_LOAD segments: one for text | |
4099 | and one for data. */ | |
4100 | segs = 2; | |
4101 | ||
4102 | s = bfd_get_section_by_name (abfd, ".interp"); | |
4103 | if (s != NULL && (s->flags & SEC_LOAD) != 0) | |
4104 | { | |
4105 | /* If we have a loadable interpreter section, we need a | |
4106 | PT_INTERP segment. In this case, assume we also need a | |
4107 | PT_PHDR segment, although that may not be true for all | |
4108 | targets. */ | |
4109 | segs += 2; | |
4110 | } | |
4111 | ||
4112 | if (bfd_get_section_by_name (abfd, ".dynamic") != NULL) | |
4113 | { | |
4114 | /* We need a PT_DYNAMIC segment. */ | |
4115 | ++segs; | |
f210dcff | 4116 | } |
08a40648 | 4117 | |
ceae84aa | 4118 | if (info != NULL && info->relro) |
f210dcff L |
4119 | { |
4120 | /* We need a PT_GNU_RELRO segment. */ | |
4121 | ++segs; | |
8ded5a0f AM |
4122 | } |
4123 | ||
12bd6957 | 4124 | if (elf_eh_frame_hdr (abfd)) |
8ded5a0f AM |
4125 | { |
4126 | /* We need a PT_GNU_EH_FRAME segment. */ | |
4127 | ++segs; | |
4128 | } | |
4129 | ||
12bd6957 | 4130 | if (elf_stack_flags (abfd)) |
8ded5a0f | 4131 | { |
2b05f1b7 L |
4132 | /* We need a PT_GNU_STACK segment. */ |
4133 | ++segs; | |
4134 | } | |
94b11780 | 4135 | |
2b05f1b7 L |
4136 | for (s = abfd->sections; s != NULL; s = s->next) |
4137 | { | |
8ded5a0f | 4138 | if ((s->flags & SEC_LOAD) != 0 |
0112cd26 | 4139 | && CONST_STRNEQ (s->name, ".note")) |
8ded5a0f AM |
4140 | { |
4141 | /* We need a PT_NOTE segment. */ | |
4142 | ++segs; | |
1c5265b5 JJ |
4143 | /* Try to create just one PT_NOTE segment |
4144 | for all adjacent loadable .note* sections. | |
4145 | gABI requires that within a PT_NOTE segment | |
4146 | (and also inside of each SHT_NOTE section) | |
4147 | each note is padded to a multiple of 4 size, | |
4148 | so we check whether the sections are correctly | |
4149 | aligned. */ | |
4150 | if (s->alignment_power == 2) | |
4151 | while (s->next != NULL | |
4152 | && s->next->alignment_power == 2 | |
4153 | && (s->next->flags & SEC_LOAD) != 0 | |
4154 | && CONST_STRNEQ (s->next->name, ".note")) | |
4155 | s = s->next; | |
8ded5a0f AM |
4156 | } |
4157 | } | |
4158 | ||
4159 | for (s = abfd->sections; s != NULL; s = s->next) | |
4160 | { | |
4161 | if (s->flags & SEC_THREAD_LOCAL) | |
4162 | { | |
4163 | /* We need a PT_TLS segment. */ | |
4164 | ++segs; | |
4165 | break; | |
4166 | } | |
4167 | } | |
4168 | ||
4169 | /* Let the backend count up any program headers it might need. */ | |
2b05f1b7 | 4170 | bed = get_elf_backend_data (abfd); |
8ded5a0f AM |
4171 | if (bed->elf_backend_additional_program_headers) |
4172 | { | |
4173 | int a; | |
4174 | ||
4175 | a = (*bed->elf_backend_additional_program_headers) (abfd, info); | |
4176 | if (a == -1) | |
4177 | abort (); | |
4178 | segs += a; | |
4179 | } | |
4180 | ||
4181 | return segs * bed->s->sizeof_phdr; | |
4182 | } | |
4183 | ||
2ea37f1c NC |
4184 | /* Find the segment that contains the output_section of section. */ |
4185 | ||
4186 | Elf_Internal_Phdr * | |
4187 | _bfd_elf_find_segment_containing_section (bfd * abfd, asection * section) | |
4188 | { | |
4189 | struct elf_segment_map *m; | |
4190 | Elf_Internal_Phdr *p; | |
4191 | ||
12bd6957 | 4192 | for (m = elf_seg_map (abfd), p = elf_tdata (abfd)->phdr; |
2ea37f1c NC |
4193 | m != NULL; |
4194 | m = m->next, p++) | |
4195 | { | |
4196 | int i; | |
4197 | ||
4198 | for (i = m->count - 1; i >= 0; i--) | |
4199 | if (m->sections[i] == section) | |
4200 | return p; | |
4201 | } | |
4202 | ||
4203 | return NULL; | |
4204 | } | |
4205 | ||
252b5132 RH |
4206 | /* Create a mapping from a set of sections to a program segment. */ |
4207 | ||
217aa764 AM |
4208 | static struct elf_segment_map * |
4209 | make_mapping (bfd *abfd, | |
4210 | asection **sections, | |
4211 | unsigned int from, | |
4212 | unsigned int to, | |
4213 | bfd_boolean phdr) | |
252b5132 RH |
4214 | { |
4215 | struct elf_segment_map *m; | |
4216 | unsigned int i; | |
4217 | asection **hdrpp; | |
dc810e39 | 4218 | bfd_size_type amt; |
252b5132 | 4219 | |
dc810e39 AM |
4220 | amt = sizeof (struct elf_segment_map); |
4221 | amt += (to - from - 1) * sizeof (asection *); | |
a50b1753 | 4222 | m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); |
252b5132 RH |
4223 | if (m == NULL) |
4224 | return NULL; | |
4225 | m->next = NULL; | |
4226 | m->p_type = PT_LOAD; | |
4227 | for (i = from, hdrpp = sections + from; i < to; i++, hdrpp++) | |
4228 | m->sections[i - from] = *hdrpp; | |
4229 | m->count = to - from; | |
4230 | ||
4231 | if (from == 0 && phdr) | |
4232 | { | |
4233 | /* Include the headers in the first PT_LOAD segment. */ | |
4234 | m->includes_filehdr = 1; | |
4235 | m->includes_phdrs = 1; | |
4236 | } | |
4237 | ||
4238 | return m; | |
4239 | } | |
4240 | ||
229fcec5 MM |
4241 | /* Create the PT_DYNAMIC segment, which includes DYNSEC. Returns NULL |
4242 | on failure. */ | |
4243 | ||
4244 | struct elf_segment_map * | |
4245 | _bfd_elf_make_dynamic_segment (bfd *abfd, asection *dynsec) | |
4246 | { | |
4247 | struct elf_segment_map *m; | |
4248 | ||
a50b1753 NC |
4249 | m = (struct elf_segment_map *) bfd_zalloc (abfd, |
4250 | sizeof (struct elf_segment_map)); | |
229fcec5 MM |
4251 | if (m == NULL) |
4252 | return NULL; | |
4253 | m->next = NULL; | |
4254 | m->p_type = PT_DYNAMIC; | |
4255 | m->count = 1; | |
4256 | m->sections[0] = dynsec; | |
08a40648 | 4257 | |
229fcec5 MM |
4258 | return m; |
4259 | } | |
4260 | ||
8ded5a0f | 4261 | /* Possibly add or remove segments from the segment map. */ |
252b5132 | 4262 | |
b34976b6 | 4263 | static bfd_boolean |
3dea8fca AM |
4264 | elf_modify_segment_map (bfd *abfd, |
4265 | struct bfd_link_info *info, | |
4266 | bfd_boolean remove_empty_load) | |
252b5132 | 4267 | { |
252e386e | 4268 | struct elf_segment_map **m; |
8ded5a0f | 4269 | const struct elf_backend_data *bed; |
252b5132 | 4270 | |
8ded5a0f AM |
4271 | /* The placement algorithm assumes that non allocated sections are |
4272 | not in PT_LOAD segments. We ensure this here by removing such | |
4273 | sections from the segment map. We also remove excluded | |
252e386e AM |
4274 | sections. Finally, any PT_LOAD segment without sections is |
4275 | removed. */ | |
12bd6957 | 4276 | m = &elf_seg_map (abfd); |
252e386e | 4277 | while (*m) |
8ded5a0f AM |
4278 | { |
4279 | unsigned int i, new_count; | |
252b5132 | 4280 | |
252e386e | 4281 | for (new_count = 0, i = 0; i < (*m)->count; i++) |
8ded5a0f | 4282 | { |
252e386e AM |
4283 | if (((*m)->sections[i]->flags & SEC_EXCLUDE) == 0 |
4284 | && (((*m)->sections[i]->flags & SEC_ALLOC) != 0 | |
4285 | || (*m)->p_type != PT_LOAD)) | |
8ded5a0f | 4286 | { |
252e386e AM |
4287 | (*m)->sections[new_count] = (*m)->sections[i]; |
4288 | new_count++; | |
8ded5a0f AM |
4289 | } |
4290 | } | |
252e386e | 4291 | (*m)->count = new_count; |
252b5132 | 4292 | |
3dea8fca | 4293 | if (remove_empty_load && (*m)->p_type == PT_LOAD && (*m)->count == 0) |
252e386e AM |
4294 | *m = (*m)->next; |
4295 | else | |
4296 | m = &(*m)->next; | |
8ded5a0f | 4297 | } |
252b5132 | 4298 | |
8ded5a0f AM |
4299 | bed = get_elf_backend_data (abfd); |
4300 | if (bed->elf_backend_modify_segment_map != NULL) | |
252b5132 | 4301 | { |
252e386e | 4302 | if (!(*bed->elf_backend_modify_segment_map) (abfd, info)) |
8ded5a0f | 4303 | return FALSE; |
252b5132 | 4304 | } |
252b5132 | 4305 | |
8ded5a0f AM |
4306 | return TRUE; |
4307 | } | |
252b5132 | 4308 | |
8ded5a0f | 4309 | /* Set up a mapping from BFD sections to program segments. */ |
252b5132 | 4310 | |
8ded5a0f AM |
4311 | bfd_boolean |
4312 | _bfd_elf_map_sections_to_segments (bfd *abfd, struct bfd_link_info *info) | |
4313 | { | |
4314 | unsigned int count; | |
4315 | struct elf_segment_map *m; | |
4316 | asection **sections = NULL; | |
4317 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
3dea8fca | 4318 | bfd_boolean no_user_phdrs; |
252b5132 | 4319 | |
12bd6957 | 4320 | no_user_phdrs = elf_seg_map (abfd) == NULL; |
d324f6d6 RM |
4321 | |
4322 | if (info != NULL) | |
4323 | info->user_phdrs = !no_user_phdrs; | |
4324 | ||
3dea8fca | 4325 | if (no_user_phdrs && bfd_count_sections (abfd) != 0) |
252b5132 | 4326 | { |
8ded5a0f AM |
4327 | asection *s; |
4328 | unsigned int i; | |
4329 | struct elf_segment_map *mfirst; | |
4330 | struct elf_segment_map **pm; | |
4331 | asection *last_hdr; | |
4332 | bfd_vma last_size; | |
4333 | unsigned int phdr_index; | |
4334 | bfd_vma maxpagesize; | |
4335 | asection **hdrpp; | |
4336 | bfd_boolean phdr_in_segment = TRUE; | |
4337 | bfd_boolean writable; | |
4338 | int tls_count = 0; | |
4339 | asection *first_tls = NULL; | |
4340 | asection *dynsec, *eh_frame_hdr; | |
4341 | bfd_size_type amt; | |
8d06853e | 4342 | bfd_vma addr_mask, wrap_to = 0; |
252b5132 | 4343 | |
8ded5a0f | 4344 | /* Select the allocated sections, and sort them. */ |
252b5132 | 4345 | |
a50b1753 NC |
4346 | sections = (asection **) bfd_malloc2 (bfd_count_sections (abfd), |
4347 | sizeof (asection *)); | |
8ded5a0f | 4348 | if (sections == NULL) |
252b5132 | 4349 | goto error_return; |
252b5132 | 4350 | |
8d06853e AM |
4351 | /* Calculate top address, avoiding undefined behaviour of shift |
4352 | left operator when shift count is equal to size of type | |
4353 | being shifted. */ | |
4354 | addr_mask = ((bfd_vma) 1 << (bfd_arch_bits_per_address (abfd) - 1)) - 1; | |
4355 | addr_mask = (addr_mask << 1) + 1; | |
4356 | ||
8ded5a0f AM |
4357 | i = 0; |
4358 | for (s = abfd->sections; s != NULL; s = s->next) | |
4359 | { | |
4360 | if ((s->flags & SEC_ALLOC) != 0) | |
4361 | { | |
4362 | sections[i] = s; | |
4363 | ++i; | |
8d06853e AM |
4364 | /* A wrapping section potentially clashes with header. */ |
4365 | if (((s->lma + s->size) & addr_mask) < (s->lma & addr_mask)) | |
4366 | wrap_to = (s->lma + s->size) & addr_mask; | |
8ded5a0f AM |
4367 | } |
4368 | } | |
4369 | BFD_ASSERT (i <= bfd_count_sections (abfd)); | |
4370 | count = i; | |
252b5132 | 4371 | |
8ded5a0f | 4372 | qsort (sections, (size_t) count, sizeof (asection *), elf_sort_sections); |
252b5132 | 4373 | |
8ded5a0f | 4374 | /* Build the mapping. */ |
252b5132 | 4375 | |
8ded5a0f AM |
4376 | mfirst = NULL; |
4377 | pm = &mfirst; | |
252b5132 | 4378 | |
8ded5a0f AM |
4379 | /* If we have a .interp section, then create a PT_PHDR segment for |
4380 | the program headers and a PT_INTERP segment for the .interp | |
4381 | section. */ | |
4382 | s = bfd_get_section_by_name (abfd, ".interp"); | |
4383 | if (s != NULL && (s->flags & SEC_LOAD) != 0) | |
4384 | { | |
4385 | amt = sizeof (struct elf_segment_map); | |
a50b1753 | 4386 | m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); |
8ded5a0f AM |
4387 | if (m == NULL) |
4388 | goto error_return; | |
4389 | m->next = NULL; | |
4390 | m->p_type = PT_PHDR; | |
4391 | /* FIXME: UnixWare and Solaris set PF_X, Irix 5 does not. */ | |
4392 | m->p_flags = PF_R | PF_X; | |
4393 | m->p_flags_valid = 1; | |
4394 | m->includes_phdrs = 1; | |
252b5132 | 4395 | |
8ded5a0f AM |
4396 | *pm = m; |
4397 | pm = &m->next; | |
252b5132 | 4398 | |
8ded5a0f | 4399 | amt = sizeof (struct elf_segment_map); |
a50b1753 | 4400 | m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); |
8ded5a0f AM |
4401 | if (m == NULL) |
4402 | goto error_return; | |
4403 | m->next = NULL; | |
4404 | m->p_type = PT_INTERP; | |
4405 | m->count = 1; | |
4406 | m->sections[0] = s; | |
4407 | ||
4408 | *pm = m; | |
4409 | pm = &m->next; | |
252b5132 | 4410 | } |
8ded5a0f AM |
4411 | |
4412 | /* Look through the sections. We put sections in the same program | |
4413 | segment when the start of the second section can be placed within | |
4414 | a few bytes of the end of the first section. */ | |
4415 | last_hdr = NULL; | |
4416 | last_size = 0; | |
4417 | phdr_index = 0; | |
4418 | maxpagesize = bed->maxpagesize; | |
063bb025 NC |
4419 | /* PR 17512: file: c8455299. |
4420 | Avoid divide-by-zero errors later on. | |
4421 | FIXME: Should we abort if the maxpagesize is zero ? */ | |
4422 | if (maxpagesize == 0) | |
4423 | maxpagesize = 1; | |
8ded5a0f AM |
4424 | writable = FALSE; |
4425 | dynsec = bfd_get_section_by_name (abfd, ".dynamic"); | |
4426 | if (dynsec != NULL | |
4427 | && (dynsec->flags & SEC_LOAD) == 0) | |
4428 | dynsec = NULL; | |
4429 | ||
4430 | /* Deal with -Ttext or something similar such that the first section | |
4431 | is not adjacent to the program headers. This is an | |
4432 | approximation, since at this point we don't know exactly how many | |
4433 | program headers we will need. */ | |
4434 | if (count > 0) | |
252b5132 | 4435 | { |
12bd6957 | 4436 | bfd_size_type phdr_size = elf_program_header_size (abfd); |
8ded5a0f | 4437 | |
62d7a5f6 | 4438 | if (phdr_size == (bfd_size_type) -1) |
8ded5a0f | 4439 | phdr_size = get_program_header_size (abfd, info); |
d2bcb0d1 | 4440 | phdr_size += bed->s->sizeof_ehdr; |
8ded5a0f | 4441 | if ((abfd->flags & D_PAGED) == 0 |
8d06853e AM |
4442 | || (sections[0]->lma & addr_mask) < phdr_size |
4443 | || ((sections[0]->lma & addr_mask) % maxpagesize | |
4444 | < phdr_size % maxpagesize) | |
4445 | || (sections[0]->lma & addr_mask & -maxpagesize) < wrap_to) | |
8ded5a0f | 4446 | phdr_in_segment = FALSE; |
252b5132 RH |
4447 | } |
4448 | ||
8ded5a0f | 4449 | for (i = 0, hdrpp = sections; i < count; i++, hdrpp++) |
252b5132 | 4450 | { |
8ded5a0f AM |
4451 | asection *hdr; |
4452 | bfd_boolean new_segment; | |
4453 | ||
4454 | hdr = *hdrpp; | |
4455 | ||
4456 | /* See if this section and the last one will fit in the same | |
4457 | segment. */ | |
4458 | ||
4459 | if (last_hdr == NULL) | |
4460 | { | |
4461 | /* If we don't have a segment yet, then we don't need a new | |
4462 | one (we build the last one after this loop). */ | |
4463 | new_segment = FALSE; | |
4464 | } | |
4465 | else if (last_hdr->lma - last_hdr->vma != hdr->lma - hdr->vma) | |
4466 | { | |
4467 | /* If this section has a different relation between the | |
4468 | virtual address and the load address, then we need a new | |
4469 | segment. */ | |
4470 | new_segment = TRUE; | |
4471 | } | |
b5599592 AM |
4472 | else if (hdr->lma < last_hdr->lma + last_size |
4473 | || last_hdr->lma + last_size < last_hdr->lma) | |
4474 | { | |
4475 | /* If this section has a load address that makes it overlap | |
4476 | the previous section, then we need a new segment. */ | |
4477 | new_segment = TRUE; | |
4478 | } | |
39948a60 NC |
4479 | /* In the next test we have to be careful when last_hdr->lma is close |
4480 | to the end of the address space. If the aligned address wraps | |
4481 | around to the start of the address space, then there are no more | |
4482 | pages left in memory and it is OK to assume that the current | |
4483 | section can be included in the current segment. */ | |
4484 | else if ((BFD_ALIGN (last_hdr->lma + last_size, maxpagesize) + maxpagesize | |
4485 | > last_hdr->lma) | |
4486 | && (BFD_ALIGN (last_hdr->lma + last_size, maxpagesize) + maxpagesize | |
4ff73856 | 4487 | <= hdr->lma)) |
8ded5a0f AM |
4488 | { |
4489 | /* If putting this section in this segment would force us to | |
4490 | skip a page in the segment, then we need a new segment. */ | |
4491 | new_segment = TRUE; | |
4492 | } | |
4493 | else if ((last_hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0 | |
e5654c0f AM |
4494 | && (hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) != 0 |
4495 | && ((abfd->flags & D_PAGED) == 0 | |
4496 | || (((last_hdr->lma + last_size - 1) & -maxpagesize) | |
4497 | != (hdr->lma & -maxpagesize)))) | |
8ded5a0f | 4498 | { |
e5654c0f AM |
4499 | /* We don't want to put a loaded section after a |
4500 | nonloaded (ie. bss style) section in the same segment | |
4501 | as that will force the non-loaded section to be loaded. | |
4502 | Consider .tbss sections as loaded for this purpose. | |
4503 | However, like the writable/non-writable case below, | |
4504 | if they are on the same page then they must be put | |
4505 | in the same segment. */ | |
8ded5a0f AM |
4506 | new_segment = TRUE; |
4507 | } | |
4508 | else if ((abfd->flags & D_PAGED) == 0) | |
4509 | { | |
4510 | /* If the file is not demand paged, which means that we | |
4511 | don't require the sections to be correctly aligned in the | |
4512 | file, then there is no other reason for a new segment. */ | |
4513 | new_segment = FALSE; | |
4514 | } | |
4515 | else if (! writable | |
4516 | && (hdr->flags & SEC_READONLY) == 0 | |
8d06853e AM |
4517 | && (((last_hdr->lma + last_size - 1) & -maxpagesize) |
4518 | != (hdr->lma & -maxpagesize))) | |
8ded5a0f AM |
4519 | { |
4520 | /* We don't want to put a writable section in a read only | |
4521 | segment, unless they are on the same page in memory | |
4522 | anyhow. We already know that the last section does not | |
4523 | bring us past the current section on the page, so the | |
4524 | only case in which the new section is not on the same | |
4525 | page as the previous section is when the previous section | |
4526 | ends precisely on a page boundary. */ | |
4527 | new_segment = TRUE; | |
4528 | } | |
4529 | else | |
4530 | { | |
4531 | /* Otherwise, we can use the same segment. */ | |
4532 | new_segment = FALSE; | |
4533 | } | |
4534 | ||
2889e75b | 4535 | /* Allow interested parties a chance to override our decision. */ |
ceae84aa AM |
4536 | if (last_hdr != NULL |
4537 | && info != NULL | |
4538 | && info->callbacks->override_segment_assignment != NULL) | |
4539 | new_segment | |
4540 | = info->callbacks->override_segment_assignment (info, abfd, hdr, | |
4541 | last_hdr, | |
4542 | new_segment); | |
2889e75b | 4543 | |
8ded5a0f AM |
4544 | if (! new_segment) |
4545 | { | |
4546 | if ((hdr->flags & SEC_READONLY) == 0) | |
4547 | writable = TRUE; | |
4548 | last_hdr = hdr; | |
4549 | /* .tbss sections effectively have zero size. */ | |
4550 | if ((hdr->flags & (SEC_THREAD_LOCAL | SEC_LOAD)) | |
4551 | != SEC_THREAD_LOCAL) | |
4552 | last_size = hdr->size; | |
4553 | else | |
4554 | last_size = 0; | |
4555 | continue; | |
4556 | } | |
4557 | ||
4558 | /* We need a new program segment. We must create a new program | |
4559 | header holding all the sections from phdr_index until hdr. */ | |
4560 | ||
4561 | m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment); | |
4562 | if (m == NULL) | |
4563 | goto error_return; | |
4564 | ||
4565 | *pm = m; | |
4566 | pm = &m->next; | |
4567 | ||
252b5132 | 4568 | if ((hdr->flags & SEC_READONLY) == 0) |
b34976b6 | 4569 | writable = TRUE; |
8ded5a0f AM |
4570 | else |
4571 | writable = FALSE; | |
4572 | ||
baaff79e JJ |
4573 | last_hdr = hdr; |
4574 | /* .tbss sections effectively have zero size. */ | |
e5caec89 | 4575 | if ((hdr->flags & (SEC_THREAD_LOCAL | SEC_LOAD)) != SEC_THREAD_LOCAL) |
eea6121a | 4576 | last_size = hdr->size; |
baaff79e JJ |
4577 | else |
4578 | last_size = 0; | |
8ded5a0f AM |
4579 | phdr_index = i; |
4580 | phdr_in_segment = FALSE; | |
252b5132 RH |
4581 | } |
4582 | ||
86b2281f AM |
4583 | /* Create a final PT_LOAD program segment, but not if it's just |
4584 | for .tbss. */ | |
4585 | if (last_hdr != NULL | |
4586 | && (i - phdr_index != 1 | |
4587 | || ((last_hdr->flags & (SEC_THREAD_LOCAL | SEC_LOAD)) | |
4588 | != SEC_THREAD_LOCAL))) | |
8ded5a0f AM |
4589 | { |
4590 | m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment); | |
4591 | if (m == NULL) | |
4592 | goto error_return; | |
252b5132 | 4593 | |
8ded5a0f AM |
4594 | *pm = m; |
4595 | pm = &m->next; | |
4596 | } | |
252b5132 | 4597 | |
8ded5a0f AM |
4598 | /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */ |
4599 | if (dynsec != NULL) | |
4600 | { | |
4601 | m = _bfd_elf_make_dynamic_segment (abfd, dynsec); | |
4602 | if (m == NULL) | |
4603 | goto error_return; | |
4604 | *pm = m; | |
4605 | pm = &m->next; | |
4606 | } | |
252b5132 | 4607 | |
1c5265b5 JJ |
4608 | /* For each batch of consecutive loadable .note sections, |
4609 | add a PT_NOTE segment. We don't use bfd_get_section_by_name, | |
4610 | because if we link together nonloadable .note sections and | |
4611 | loadable .note sections, we will generate two .note sections | |
4612 | in the output file. FIXME: Using names for section types is | |
4613 | bogus anyhow. */ | |
8ded5a0f AM |
4614 | for (s = abfd->sections; s != NULL; s = s->next) |
4615 | { | |
4616 | if ((s->flags & SEC_LOAD) != 0 | |
0112cd26 | 4617 | && CONST_STRNEQ (s->name, ".note")) |
8ded5a0f | 4618 | { |
1c5265b5 | 4619 | asection *s2; |
91d6fa6a NC |
4620 | |
4621 | count = 1; | |
8ded5a0f | 4622 | amt = sizeof (struct elf_segment_map); |
1c5265b5 JJ |
4623 | if (s->alignment_power == 2) |
4624 | for (s2 = s; s2->next != NULL; s2 = s2->next) | |
55b581a6 JJ |
4625 | { |
4626 | if (s2->next->alignment_power == 2 | |
4627 | && (s2->next->flags & SEC_LOAD) != 0 | |
4628 | && CONST_STRNEQ (s2->next->name, ".note") | |
8d06853e AM |
4629 | && align_power (s2->lma + s2->size, 2) |
4630 | == s2->next->lma) | |
55b581a6 JJ |
4631 | count++; |
4632 | else | |
4633 | break; | |
4634 | } | |
1c5265b5 | 4635 | amt += (count - 1) * sizeof (asection *); |
a50b1753 | 4636 | m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); |
8ded5a0f AM |
4637 | if (m == NULL) |
4638 | goto error_return; | |
4639 | m->next = NULL; | |
4640 | m->p_type = PT_NOTE; | |
1c5265b5 JJ |
4641 | m->count = count; |
4642 | while (count > 1) | |
4643 | { | |
4644 | m->sections[m->count - count--] = s; | |
4645 | BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0); | |
4646 | s = s->next; | |
4647 | } | |
4648 | m->sections[m->count - 1] = s; | |
4649 | BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0); | |
8ded5a0f AM |
4650 | *pm = m; |
4651 | pm = &m->next; | |
4652 | } | |
4653 | if (s->flags & SEC_THREAD_LOCAL) | |
4654 | { | |
4655 | if (! tls_count) | |
4656 | first_tls = s; | |
4657 | tls_count++; | |
4658 | } | |
4659 | } | |
252b5132 | 4660 | |
8ded5a0f AM |
4661 | /* If there are any SHF_TLS output sections, add PT_TLS segment. */ |
4662 | if (tls_count > 0) | |
4663 | { | |
8ded5a0f AM |
4664 | amt = sizeof (struct elf_segment_map); |
4665 | amt += (tls_count - 1) * sizeof (asection *); | |
a50b1753 | 4666 | m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); |
8ded5a0f AM |
4667 | if (m == NULL) |
4668 | goto error_return; | |
4669 | m->next = NULL; | |
4670 | m->p_type = PT_TLS; | |
4671 | m->count = tls_count; | |
4672 | /* Mandated PF_R. */ | |
4673 | m->p_flags = PF_R; | |
4674 | m->p_flags_valid = 1; | |
d923cae0 | 4675 | s = first_tls; |
91d6fa6a | 4676 | for (i = 0; i < (unsigned int) tls_count; ++i) |
8ded5a0f | 4677 | { |
d923cae0 L |
4678 | if ((s->flags & SEC_THREAD_LOCAL) == 0) |
4679 | { | |
4680 | _bfd_error_handler | |
4681 | (_("%B: TLS sections are not adjacent:"), abfd); | |
4682 | s = first_tls; | |
4683 | i = 0; | |
4684 | while (i < (unsigned int) tls_count) | |
4685 | { | |
4686 | if ((s->flags & SEC_THREAD_LOCAL) != 0) | |
4687 | { | |
4688 | _bfd_error_handler (_(" TLS: %A"), s); | |
4689 | i++; | |
4690 | } | |
4691 | else | |
4692 | _bfd_error_handler (_(" non-TLS: %A"), s); | |
4693 | s = s->next; | |
4694 | } | |
4695 | bfd_set_error (bfd_error_bad_value); | |
4696 | goto error_return; | |
4697 | } | |
4698 | m->sections[i] = s; | |
4699 | s = s->next; | |
8ded5a0f | 4700 | } |
252b5132 | 4701 | |
8ded5a0f AM |
4702 | *pm = m; |
4703 | pm = &m->next; | |
4704 | } | |
252b5132 | 4705 | |
8ded5a0f AM |
4706 | /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME |
4707 | segment. */ | |
12bd6957 | 4708 | eh_frame_hdr = elf_eh_frame_hdr (abfd); |
8ded5a0f AM |
4709 | if (eh_frame_hdr != NULL |
4710 | && (eh_frame_hdr->output_section->flags & SEC_LOAD) != 0) | |
252b5132 | 4711 | { |
dc810e39 | 4712 | amt = sizeof (struct elf_segment_map); |
a50b1753 | 4713 | m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); |
252b5132 RH |
4714 | if (m == NULL) |
4715 | goto error_return; | |
4716 | m->next = NULL; | |
8ded5a0f | 4717 | m->p_type = PT_GNU_EH_FRAME; |
252b5132 | 4718 | m->count = 1; |
8ded5a0f | 4719 | m->sections[0] = eh_frame_hdr->output_section; |
252b5132 RH |
4720 | |
4721 | *pm = m; | |
4722 | pm = &m->next; | |
4723 | } | |
13ae64f3 | 4724 | |
12bd6957 | 4725 | if (elf_stack_flags (abfd)) |
13ae64f3 | 4726 | { |
8ded5a0f | 4727 | amt = sizeof (struct elf_segment_map); |
a50b1753 | 4728 | m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); |
8ded5a0f AM |
4729 | if (m == NULL) |
4730 | goto error_return; | |
4731 | m->next = NULL; | |
2b05f1b7 | 4732 | m->p_type = PT_GNU_STACK; |
12bd6957 | 4733 | m->p_flags = elf_stack_flags (abfd); |
04c3a755 | 4734 | m->p_align = bed->stack_align; |
8ded5a0f | 4735 | m->p_flags_valid = 1; |
04c3a755 NS |
4736 | m->p_align_valid = m->p_align != 0; |
4737 | if (info->stacksize > 0) | |
4738 | { | |
4739 | m->p_size = info->stacksize; | |
4740 | m->p_size_valid = 1; | |
4741 | } | |
252b5132 | 4742 | |
8ded5a0f AM |
4743 | *pm = m; |
4744 | pm = &m->next; | |
4745 | } | |
65765700 | 4746 | |
ceae84aa | 4747 | if (info != NULL && info->relro) |
8ded5a0f | 4748 | { |
f210dcff L |
4749 | for (m = mfirst; m != NULL; m = m->next) |
4750 | { | |
3832a4d8 AM |
4751 | if (m->p_type == PT_LOAD |
4752 | && m->count != 0 | |
4753 | && m->sections[0]->vma >= info->relro_start | |
4754 | && m->sections[0]->vma < info->relro_end) | |
f210dcff | 4755 | { |
3832a4d8 AM |
4756 | i = m->count; |
4757 | while (--i != (unsigned) -1) | |
4758 | if ((m->sections[i]->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) | |
4759 | == (SEC_LOAD | SEC_HAS_CONTENTS)) | |
4760 | break; | |
4761 | ||
43a8475c | 4762 | if (i != (unsigned) -1) |
f210dcff L |
4763 | break; |
4764 | } | |
be01b344 | 4765 | } |
f210dcff L |
4766 | |
4767 | /* Make a PT_GNU_RELRO segment only when it isn't empty. */ | |
4768 | if (m != NULL) | |
4769 | { | |
4770 | amt = sizeof (struct elf_segment_map); | |
a50b1753 | 4771 | m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); |
f210dcff L |
4772 | if (m == NULL) |
4773 | goto error_return; | |
4774 | m->next = NULL; | |
4775 | m->p_type = PT_GNU_RELRO; | |
f210dcff L |
4776 | *pm = m; |
4777 | pm = &m->next; | |
4778 | } | |
8ded5a0f | 4779 | } |
9ee5e499 | 4780 | |
8ded5a0f | 4781 | free (sections); |
12bd6957 | 4782 | elf_seg_map (abfd) = mfirst; |
9ee5e499 JJ |
4783 | } |
4784 | ||
3dea8fca | 4785 | if (!elf_modify_segment_map (abfd, info, no_user_phdrs)) |
8ded5a0f | 4786 | return FALSE; |
8c37241b | 4787 | |
12bd6957 | 4788 | for (count = 0, m = elf_seg_map (abfd); m != NULL; m = m->next) |
8ded5a0f | 4789 | ++count; |
12bd6957 | 4790 | elf_program_header_size (abfd) = count * bed->s->sizeof_phdr; |
252b5132 | 4791 | |
b34976b6 | 4792 | return TRUE; |
252b5132 RH |
4793 | |
4794 | error_return: | |
4795 | if (sections != NULL) | |
4796 | free (sections); | |
b34976b6 | 4797 | return FALSE; |
252b5132 RH |
4798 | } |
4799 | ||
4800 | /* Sort sections by address. */ | |
4801 | ||
4802 | static int | |
217aa764 | 4803 | elf_sort_sections (const void *arg1, const void *arg2) |
252b5132 RH |
4804 | { |
4805 | const asection *sec1 = *(const asection **) arg1; | |
4806 | const asection *sec2 = *(const asection **) arg2; | |
eecdbe52 | 4807 | bfd_size_type size1, size2; |
252b5132 RH |
4808 | |
4809 | /* Sort by LMA first, since this is the address used to | |
4810 | place the section into a segment. */ | |
4811 | if (sec1->lma < sec2->lma) | |
4812 | return -1; | |
4813 | else if (sec1->lma > sec2->lma) | |
4814 | return 1; | |
4815 | ||
4816 | /* Then sort by VMA. Normally the LMA and the VMA will be | |
4817 | the same, and this will do nothing. */ | |
4818 | if (sec1->vma < sec2->vma) | |
4819 | return -1; | |
4820 | else if (sec1->vma > sec2->vma) | |
4821 | return 1; | |
4822 | ||
4823 | /* Put !SEC_LOAD sections after SEC_LOAD ones. */ | |
4824 | ||
07c6e936 | 4825 | #define TOEND(x) (((x)->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0) |
252b5132 RH |
4826 | |
4827 | if (TOEND (sec1)) | |
4828 | { | |
4829 | if (TOEND (sec2)) | |
00a7cdc5 NC |
4830 | { |
4831 | /* If the indicies are the same, do not return 0 | |
4832 | here, but continue to try the next comparison. */ | |
4833 | if (sec1->target_index - sec2->target_index != 0) | |
4834 | return sec1->target_index - sec2->target_index; | |
4835 | } | |
252b5132 RH |
4836 | else |
4837 | return 1; | |
4838 | } | |
00a7cdc5 | 4839 | else if (TOEND (sec2)) |
252b5132 RH |
4840 | return -1; |
4841 | ||
4842 | #undef TOEND | |
4843 | ||
00a7cdc5 NC |
4844 | /* Sort by size, to put zero sized sections |
4845 | before others at the same address. */ | |
252b5132 | 4846 | |
eea6121a AM |
4847 | size1 = (sec1->flags & SEC_LOAD) ? sec1->size : 0; |
4848 | size2 = (sec2->flags & SEC_LOAD) ? sec2->size : 0; | |
eecdbe52 JJ |
4849 | |
4850 | if (size1 < size2) | |
252b5132 | 4851 | return -1; |
eecdbe52 | 4852 | if (size1 > size2) |
252b5132 RH |
4853 | return 1; |
4854 | ||
4855 | return sec1->target_index - sec2->target_index; | |
4856 | } | |
4857 | ||
340b6d91 AC |
4858 | /* Ian Lance Taylor writes: |
4859 | ||
4860 | We shouldn't be using % with a negative signed number. That's just | |
4861 | not good. We have to make sure either that the number is not | |
4862 | negative, or that the number has an unsigned type. When the types | |
4863 | are all the same size they wind up as unsigned. When file_ptr is a | |
4864 | larger signed type, the arithmetic winds up as signed long long, | |
4865 | which is wrong. | |
4866 | ||
4867 | What we're trying to say here is something like ``increase OFF by | |
4868 | the least amount that will cause it to be equal to the VMA modulo | |
4869 | the page size.'' */ | |
4870 | /* In other words, something like: | |
4871 | ||
4872 | vma_offset = m->sections[0]->vma % bed->maxpagesize; | |
4873 | off_offset = off % bed->maxpagesize; | |
4874 | if (vma_offset < off_offset) | |
4875 | adjustment = vma_offset + bed->maxpagesize - off_offset; | |
4876 | else | |
4877 | adjustment = vma_offset - off_offset; | |
08a40648 | 4878 | |
340b6d91 AC |
4879 | which can can be collapsed into the expression below. */ |
4880 | ||
4881 | static file_ptr | |
4882 | vma_page_aligned_bias (bfd_vma vma, ufile_ptr off, bfd_vma maxpagesize) | |
4883 | { | |
dc9155b2 NC |
4884 | /* PR binutils/16199: Handle an alignment of zero. */ |
4885 | if (maxpagesize == 0) | |
4886 | maxpagesize = 1; | |
340b6d91 AC |
4887 | return ((vma - off) % maxpagesize); |
4888 | } | |
4889 | ||
6d33f217 L |
4890 | static void |
4891 | print_segment_map (const struct elf_segment_map *m) | |
4892 | { | |
4893 | unsigned int j; | |
4894 | const char *pt = get_segment_type (m->p_type); | |
4895 | char buf[32]; | |
4896 | ||
4897 | if (pt == NULL) | |
4898 | { | |
4899 | if (m->p_type >= PT_LOPROC && m->p_type <= PT_HIPROC) | |
4900 | sprintf (buf, "LOPROC+%7.7x", | |
4901 | (unsigned int) (m->p_type - PT_LOPROC)); | |
4902 | else if (m->p_type >= PT_LOOS && m->p_type <= PT_HIOS) | |
4903 | sprintf (buf, "LOOS+%7.7x", | |
4904 | (unsigned int) (m->p_type - PT_LOOS)); | |
4905 | else | |
4906 | snprintf (buf, sizeof (buf), "%8.8x", | |
4907 | (unsigned int) m->p_type); | |
4908 | pt = buf; | |
4909 | } | |
4a97a0e5 | 4910 | fflush (stdout); |
6d33f217 L |
4911 | fprintf (stderr, "%s:", pt); |
4912 | for (j = 0; j < m->count; j++) | |
4913 | fprintf (stderr, " %s", m->sections [j]->name); | |
4914 | putc ('\n',stderr); | |
4a97a0e5 | 4915 | fflush (stderr); |
6d33f217 L |
4916 | } |
4917 | ||
32812159 AM |
4918 | static bfd_boolean |
4919 | write_zeros (bfd *abfd, file_ptr pos, bfd_size_type len) | |
4920 | { | |
4921 | void *buf; | |
4922 | bfd_boolean ret; | |
4923 | ||
4924 | if (bfd_seek (abfd, pos, SEEK_SET) != 0) | |
4925 | return FALSE; | |
4926 | buf = bfd_zmalloc (len); | |
4927 | if (buf == NULL) | |
4928 | return FALSE; | |
4929 | ret = bfd_bwrite (buf, len, abfd) == len; | |
4930 | free (buf); | |
4931 | return ret; | |
4932 | } | |
4933 | ||
252b5132 RH |
4934 | /* Assign file positions to the sections based on the mapping from |
4935 | sections to segments. This function also sets up some fields in | |
f3520d2f | 4936 | the file header. */ |
252b5132 | 4937 | |
b34976b6 | 4938 | static bfd_boolean |
f3520d2f AM |
4939 | assign_file_positions_for_load_sections (bfd *abfd, |
4940 | struct bfd_link_info *link_info) | |
252b5132 RH |
4941 | { |
4942 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
252b5132 | 4943 | struct elf_segment_map *m; |
252b5132 | 4944 | Elf_Internal_Phdr *phdrs; |
252b5132 | 4945 | Elf_Internal_Phdr *p; |
02bf8d82 | 4946 | file_ptr off; |
3f570048 | 4947 | bfd_size_type maxpagesize; |
f3520d2f | 4948 | unsigned int alloc; |
0920dee7 | 4949 | unsigned int i, j; |
2b0bc088 | 4950 | bfd_vma header_pad = 0; |
252b5132 | 4951 | |
e36284ab | 4952 | if (link_info == NULL |
ceae84aa | 4953 | && !_bfd_elf_map_sections_to_segments (abfd, link_info)) |
8ded5a0f | 4954 | return FALSE; |
252b5132 | 4955 | |
8ded5a0f | 4956 | alloc = 0; |
12bd6957 | 4957 | for (m = elf_seg_map (abfd); m != NULL; m = m->next) |
2b0bc088 NC |
4958 | { |
4959 | ++alloc; | |
4960 | if (m->header_size) | |
4961 | header_pad = m->header_size; | |
4962 | } | |
252b5132 | 4963 | |
82f2dbf7 NC |
4964 | if (alloc) |
4965 | { | |
4966 | elf_elfheader (abfd)->e_phoff = bed->s->sizeof_ehdr; | |
4967 | elf_elfheader (abfd)->e_phentsize = bed->s->sizeof_phdr; | |
4968 | } | |
4969 | else | |
4970 | { | |
4971 | /* PR binutils/12467. */ | |
4972 | elf_elfheader (abfd)->e_phoff = 0; | |
4973 | elf_elfheader (abfd)->e_phentsize = 0; | |
4974 | } | |
d324f6d6 | 4975 | |
8ded5a0f | 4976 | elf_elfheader (abfd)->e_phnum = alloc; |
252b5132 | 4977 | |
12bd6957 AM |
4978 | if (elf_program_header_size (abfd) == (bfd_size_type) -1) |
4979 | elf_program_header_size (abfd) = alloc * bed->s->sizeof_phdr; | |
8ded5a0f | 4980 | else |
12bd6957 | 4981 | BFD_ASSERT (elf_program_header_size (abfd) |
59e0647f | 4982 | >= alloc * bed->s->sizeof_phdr); |
252b5132 RH |
4983 | |
4984 | if (alloc == 0) | |
f3520d2f | 4985 | { |
12bd6957 | 4986 | elf_next_file_pos (abfd) = bed->s->sizeof_ehdr; |
8ded5a0f | 4987 | return TRUE; |
f3520d2f | 4988 | } |
252b5132 | 4989 | |
12bd6957 | 4990 | /* We're writing the size in elf_program_header_size (abfd), |
57268894 HPN |
4991 | see assign_file_positions_except_relocs, so make sure we have |
4992 | that amount allocated, with trailing space cleared. | |
12bd6957 AM |
4993 | The variable alloc contains the computed need, while |
4994 | elf_program_header_size (abfd) contains the size used for the | |
57268894 HPN |
4995 | layout. |
4996 | See ld/emultempl/elf-generic.em:gld${EMULATION_NAME}_map_segments | |
4997 | where the layout is forced to according to a larger size in the | |
4998 | last iterations for the testcase ld-elf/header. */ | |
12bd6957 | 4999 | BFD_ASSERT (elf_program_header_size (abfd) % bed->s->sizeof_phdr |
57268894 | 5000 | == 0); |
a50b1753 NC |
5001 | phdrs = (Elf_Internal_Phdr *) |
5002 | bfd_zalloc2 (abfd, | |
12bd6957 | 5003 | (elf_program_header_size (abfd) / bed->s->sizeof_phdr), |
a50b1753 | 5004 | sizeof (Elf_Internal_Phdr)); |
f3520d2f | 5005 | elf_tdata (abfd)->phdr = phdrs; |
252b5132 | 5006 | if (phdrs == NULL) |
b34976b6 | 5007 | return FALSE; |
252b5132 | 5008 | |
3f570048 AM |
5009 | maxpagesize = 1; |
5010 | if ((abfd->flags & D_PAGED) != 0) | |
5011 | maxpagesize = bed->maxpagesize; | |
5012 | ||
252b5132 RH |
5013 | off = bed->s->sizeof_ehdr; |
5014 | off += alloc * bed->s->sizeof_phdr; | |
2b0bc088 NC |
5015 | if (header_pad < (bfd_vma) off) |
5016 | header_pad = 0; | |
5017 | else | |
5018 | header_pad -= off; | |
5019 | off += header_pad; | |
252b5132 | 5020 | |
12bd6957 | 5021 | for (m = elf_seg_map (abfd), p = phdrs, j = 0; |
252b5132 | 5022 | m != NULL; |
0920dee7 | 5023 | m = m->next, p++, j++) |
252b5132 | 5024 | { |
252b5132 | 5025 | asection **secpp; |
bf988460 AM |
5026 | bfd_vma off_adjust; |
5027 | bfd_boolean no_contents; | |
252b5132 RH |
5028 | |
5029 | /* If elf_segment_map is not from map_sections_to_segments, the | |
08a40648 | 5030 | sections may not be correctly ordered. NOTE: sorting should |
52e9b619 MS |
5031 | not be done to the PT_NOTE section of a corefile, which may |
5032 | contain several pseudo-sections artificially created by bfd. | |
5033 | Sorting these pseudo-sections breaks things badly. */ | |
47d9a591 AM |
5034 | if (m->count > 1 |
5035 | && !(elf_elfheader (abfd)->e_type == ET_CORE | |
52e9b619 | 5036 | && m->p_type == PT_NOTE)) |
252b5132 RH |
5037 | qsort (m->sections, (size_t) m->count, sizeof (asection *), |
5038 | elf_sort_sections); | |
5039 | ||
b301b248 AM |
5040 | /* An ELF segment (described by Elf_Internal_Phdr) may contain a |
5041 | number of sections with contents contributing to both p_filesz | |
5042 | and p_memsz, followed by a number of sections with no contents | |
5043 | that just contribute to p_memsz. In this loop, OFF tracks next | |
02bf8d82 | 5044 | available file offset for PT_LOAD and PT_NOTE segments. */ |
252b5132 | 5045 | p->p_type = m->p_type; |
28a7f3e7 | 5046 | p->p_flags = m->p_flags; |
252b5132 | 5047 | |
3f570048 AM |
5048 | if (m->count == 0) |
5049 | p->p_vaddr = 0; | |
5050 | else | |
3271a814 | 5051 | p->p_vaddr = m->sections[0]->vma - m->p_vaddr_offset; |
3f570048 AM |
5052 | |
5053 | if (m->p_paddr_valid) | |
5054 | p->p_paddr = m->p_paddr; | |
5055 | else if (m->count == 0) | |
5056 | p->p_paddr = 0; | |
5057 | else | |
08a40648 | 5058 | p->p_paddr = m->sections[0]->lma - m->p_vaddr_offset; |
3f570048 AM |
5059 | |
5060 | if (p->p_type == PT_LOAD | |
5061 | && (abfd->flags & D_PAGED) != 0) | |
5062 | { | |
5063 | /* p_align in demand paged PT_LOAD segments effectively stores | |
5064 | the maximum page size. When copying an executable with | |
5065 | objcopy, we set m->p_align from the input file. Use this | |
5066 | value for maxpagesize rather than bed->maxpagesize, which | |
5067 | may be different. Note that we use maxpagesize for PT_TLS | |
5068 | segment alignment later in this function, so we are relying | |
5069 | on at least one PT_LOAD segment appearing before a PT_TLS | |
5070 | segment. */ | |
5071 | if (m->p_align_valid) | |
5072 | maxpagesize = m->p_align; | |
5073 | ||
5074 | p->p_align = maxpagesize; | |
5075 | } | |
3271a814 NS |
5076 | else if (m->p_align_valid) |
5077 | p->p_align = m->p_align; | |
e970b90a DJ |
5078 | else if (m->count == 0) |
5079 | p->p_align = 1 << bed->s->log_file_align; | |
3f570048 AM |
5080 | else |
5081 | p->p_align = 0; | |
5082 | ||
bf988460 AM |
5083 | no_contents = FALSE; |
5084 | off_adjust = 0; | |
252b5132 | 5085 | if (p->p_type == PT_LOAD |
b301b248 | 5086 | && m->count > 0) |
252b5132 | 5087 | { |
b301b248 | 5088 | bfd_size_type align; |
a49e53ed | 5089 | unsigned int align_power = 0; |
b301b248 | 5090 | |
3271a814 NS |
5091 | if (m->p_align_valid) |
5092 | align = p->p_align; | |
5093 | else | |
252b5132 | 5094 | { |
3271a814 NS |
5095 | for (i = 0, secpp = m->sections; i < m->count; i++, secpp++) |
5096 | { | |
5097 | unsigned int secalign; | |
08a40648 | 5098 | |
3271a814 NS |
5099 | secalign = bfd_get_section_alignment (abfd, *secpp); |
5100 | if (secalign > align_power) | |
5101 | align_power = secalign; | |
5102 | } | |
5103 | align = (bfd_size_type) 1 << align_power; | |
5104 | if (align < maxpagesize) | |
5105 | align = maxpagesize; | |
b301b248 | 5106 | } |
252b5132 | 5107 | |
02bf8d82 AM |
5108 | for (i = 0; i < m->count; i++) |
5109 | if ((m->sections[i]->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0) | |
5110 | /* If we aren't making room for this section, then | |
5111 | it must be SHT_NOBITS regardless of what we've | |
5112 | set via struct bfd_elf_special_section. */ | |
5113 | elf_section_type (m->sections[i]) = SHT_NOBITS; | |
5114 | ||
bf988460 | 5115 | /* Find out whether this segment contains any loadable |
aea274d3 AM |
5116 | sections. */ |
5117 | no_contents = TRUE; | |
5118 | for (i = 0; i < m->count; i++) | |
5119 | if (elf_section_type (m->sections[i]) != SHT_NOBITS) | |
5120 | { | |
5121 | no_contents = FALSE; | |
5122 | break; | |
5123 | } | |
bf988460 | 5124 | |
85cfcbfb | 5125 | off_adjust = vma_page_aligned_bias (p->p_vaddr, off, align); |
bf988460 AM |
5126 | off += off_adjust; |
5127 | if (no_contents) | |
5128 | { | |
5129 | /* We shouldn't need to align the segment on disk since | |
5130 | the segment doesn't need file space, but the gABI | |
5131 | arguably requires the alignment and glibc ld.so | |
5132 | checks it. So to comply with the alignment | |
5133 | requirement but not waste file space, we adjust | |
5134 | p_offset for just this segment. (OFF_ADJUST is | |
5135 | subtracted from OFF later.) This may put p_offset | |
5136 | past the end of file, but that shouldn't matter. */ | |
5137 | } | |
5138 | else | |
5139 | off_adjust = 0; | |
252b5132 | 5140 | } |
b1a6d0b1 NC |
5141 | /* Make sure the .dynamic section is the first section in the |
5142 | PT_DYNAMIC segment. */ | |
5143 | else if (p->p_type == PT_DYNAMIC | |
5144 | && m->count > 1 | |
5145 | && strcmp (m->sections[0]->name, ".dynamic") != 0) | |
5146 | { | |
5147 | _bfd_error_handler | |
b301b248 AM |
5148 | (_("%B: The first section in the PT_DYNAMIC segment is not the .dynamic section"), |
5149 | abfd); | |
b1a6d0b1 NC |
5150 | bfd_set_error (bfd_error_bad_value); |
5151 | return FALSE; | |
5152 | } | |
3f001e84 JK |
5153 | /* Set the note section type to SHT_NOTE. */ |
5154 | else if (p->p_type == PT_NOTE) | |
5155 | for (i = 0; i < m->count; i++) | |
5156 | elf_section_type (m->sections[i]) = SHT_NOTE; | |
252b5132 | 5157 | |
252b5132 RH |
5158 | p->p_offset = 0; |
5159 | p->p_filesz = 0; | |
5160 | p->p_memsz = 0; | |
5161 | ||
5162 | if (m->includes_filehdr) | |
5163 | { | |
bf988460 | 5164 | if (!m->p_flags_valid) |
252b5132 | 5165 | p->p_flags |= PF_R; |
252b5132 RH |
5166 | p->p_filesz = bed->s->sizeof_ehdr; |
5167 | p->p_memsz = bed->s->sizeof_ehdr; | |
5168 | if (m->count > 0) | |
5169 | { | |
252b5132 RH |
5170 | if (p->p_vaddr < (bfd_vma) off) |
5171 | { | |
caf47ea6 | 5172 | (*_bfd_error_handler) |
b301b248 AM |
5173 | (_("%B: Not enough room for program headers, try linking with -N"), |
5174 | abfd); | |
252b5132 | 5175 | bfd_set_error (bfd_error_bad_value); |
b34976b6 | 5176 | return FALSE; |
252b5132 RH |
5177 | } |
5178 | ||
5179 | p->p_vaddr -= off; | |
bf988460 | 5180 | if (!m->p_paddr_valid) |
252b5132 RH |
5181 | p->p_paddr -= off; |
5182 | } | |
252b5132 RH |
5183 | } |
5184 | ||
5185 | if (m->includes_phdrs) | |
5186 | { | |
bf988460 | 5187 | if (!m->p_flags_valid) |
252b5132 RH |
5188 | p->p_flags |= PF_R; |
5189 | ||
f3520d2f | 5190 | if (!m->includes_filehdr) |
252b5132 RH |
5191 | { |
5192 | p->p_offset = bed->s->sizeof_ehdr; | |
5193 | ||
5194 | if (m->count > 0) | |
5195 | { | |
252b5132 | 5196 | p->p_vaddr -= off - p->p_offset; |
bf988460 | 5197 | if (!m->p_paddr_valid) |
252b5132 RH |
5198 | p->p_paddr -= off - p->p_offset; |
5199 | } | |
252b5132 RH |
5200 | } |
5201 | ||
5202 | p->p_filesz += alloc * bed->s->sizeof_phdr; | |
5203 | p->p_memsz += alloc * bed->s->sizeof_phdr; | |
2b0bc088 NC |
5204 | if (m->count) |
5205 | { | |
5206 | p->p_filesz += header_pad; | |
5207 | p->p_memsz += header_pad; | |
5208 | } | |
252b5132 RH |
5209 | } |
5210 | ||
5211 | if (p->p_type == PT_LOAD | |
5212 | || (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core)) | |
5213 | { | |
bf988460 | 5214 | if (!m->includes_filehdr && !m->includes_phdrs) |
02bf8d82 | 5215 | p->p_offset = off; |
252b5132 RH |
5216 | else |
5217 | { | |
5218 | file_ptr adjust; | |
5219 | ||
5220 | adjust = off - (p->p_offset + p->p_filesz); | |
bf988460 AM |
5221 | if (!no_contents) |
5222 | p->p_filesz += adjust; | |
252b5132 RH |
5223 | p->p_memsz += adjust; |
5224 | } | |
5225 | } | |
5226 | ||
1ea63fd2 AM |
5227 | /* Set up p_filesz, p_memsz, p_align and p_flags from the section |
5228 | maps. Set filepos for sections in PT_LOAD segments, and in | |
5229 | core files, for sections in PT_NOTE segments. | |
5230 | assign_file_positions_for_non_load_sections will set filepos | |
5231 | for other sections and update p_filesz for other segments. */ | |
252b5132 RH |
5232 | for (i = 0, secpp = m->sections; i < m->count; i++, secpp++) |
5233 | { | |
5234 | asection *sec; | |
252b5132 | 5235 | bfd_size_type align; |
627b32bc | 5236 | Elf_Internal_Shdr *this_hdr; |
252b5132 RH |
5237 | |
5238 | sec = *secpp; | |
02bf8d82 | 5239 | this_hdr = &elf_section_data (sec)->this_hdr; |
3f570048 | 5240 | align = (bfd_size_type) 1 << bfd_get_section_alignment (abfd, sec); |
252b5132 | 5241 | |
88967714 AM |
5242 | if ((p->p_type == PT_LOAD |
5243 | || p->p_type == PT_TLS) | |
5244 | && (this_hdr->sh_type != SHT_NOBITS | |
5245 | || ((this_hdr->sh_flags & SHF_ALLOC) != 0 | |
5246 | && ((this_hdr->sh_flags & SHF_TLS) == 0 | |
5247 | || p->p_type == PT_TLS)))) | |
252b5132 | 5248 | { |
b5599592 AM |
5249 | bfd_vma p_start = p->p_paddr; |
5250 | bfd_vma p_end = p_start + p->p_memsz; | |
5251 | bfd_vma s_start = sec->lma; | |
5252 | bfd_vma adjust = s_start - p_end; | |
252b5132 | 5253 | |
a2d1e028 L |
5254 | if (adjust != 0 |
5255 | && (s_start < p_end | |
5256 | || p_end < p_start)) | |
252b5132 | 5257 | { |
88967714 | 5258 | (*_bfd_error_handler) |
b5599592 AM |
5259 | (_("%B: section %A lma %#lx adjusted to %#lx"), abfd, sec, |
5260 | (unsigned long) s_start, (unsigned long) p_end); | |
88967714 | 5261 | adjust = 0; |
b5599592 | 5262 | sec->lma = p_end; |
1cfb7d1e | 5263 | } |
3ac9b6c9 | 5264 | p->p_memsz += adjust; |
1cfb7d1e | 5265 | |
88967714 AM |
5266 | if (this_hdr->sh_type != SHT_NOBITS) |
5267 | { | |
32812159 AM |
5268 | if (p->p_filesz + adjust < p->p_memsz) |
5269 | { | |
5270 | /* We have a PROGBITS section following NOBITS ones. | |
5271 | Allocate file space for the NOBITS section(s) and | |
5272 | zero it. */ | |
5273 | adjust = p->p_memsz - p->p_filesz; | |
5274 | if (!write_zeros (abfd, off, adjust)) | |
5275 | return FALSE; | |
5276 | } | |
88967714 AM |
5277 | off += adjust; |
5278 | p->p_filesz += adjust; | |
252b5132 | 5279 | } |
252b5132 RH |
5280 | } |
5281 | ||
5282 | if (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core) | |
5283 | { | |
b301b248 AM |
5284 | /* The section at i == 0 is the one that actually contains |
5285 | everything. */ | |
4a938328 MS |
5286 | if (i == 0) |
5287 | { | |
627b32bc | 5288 | this_hdr->sh_offset = sec->filepos = off; |
6a3cd2b4 AM |
5289 | off += this_hdr->sh_size; |
5290 | p->p_filesz = this_hdr->sh_size; | |
b301b248 AM |
5291 | p->p_memsz = 0; |
5292 | p->p_align = 1; | |
252b5132 | 5293 | } |
4a938328 | 5294 | else |
252b5132 | 5295 | { |
b301b248 | 5296 | /* The rest are fake sections that shouldn't be written. */ |
252b5132 | 5297 | sec->filepos = 0; |
eea6121a | 5298 | sec->size = 0; |
b301b248 AM |
5299 | sec->flags = 0; |
5300 | continue; | |
252b5132 | 5301 | } |
252b5132 RH |
5302 | } |
5303 | else | |
5304 | { | |
1e951488 | 5305 | if (p->p_type == PT_LOAD) |
b301b248 | 5306 | { |
1e951488 AM |
5307 | this_hdr->sh_offset = sec->filepos = off; |
5308 | if (this_hdr->sh_type != SHT_NOBITS) | |
5309 | off += this_hdr->sh_size; | |
5310 | } | |
5311 | else if (this_hdr->sh_type == SHT_NOBITS | |
5312 | && (this_hdr->sh_flags & SHF_TLS) != 0 | |
5313 | && this_hdr->sh_offset == 0) | |
5314 | { | |
5315 | /* This is a .tbss section that didn't get a PT_LOAD. | |
5316 | (See _bfd_elf_map_sections_to_segments "Create a | |
5317 | final PT_LOAD".) Set sh_offset to the value it | |
5318 | would have if we had created a zero p_filesz and | |
5319 | p_memsz PT_LOAD header for the section. This | |
5320 | also makes the PT_TLS header have the same | |
5321 | p_offset value. */ | |
5322 | bfd_vma adjust = vma_page_aligned_bias (this_hdr->sh_addr, | |
5323 | off, align); | |
5324 | this_hdr->sh_offset = sec->filepos = off + adjust; | |
b301b248 | 5325 | } |
252b5132 | 5326 | |
02bf8d82 | 5327 | if (this_hdr->sh_type != SHT_NOBITS) |
b301b248 | 5328 | { |
6a3cd2b4 | 5329 | p->p_filesz += this_hdr->sh_size; |
02bf8d82 AM |
5330 | /* A load section without SHF_ALLOC is something like |
5331 | a note section in a PT_NOTE segment. These take | |
5332 | file space but are not loaded into memory. */ | |
5333 | if ((this_hdr->sh_flags & SHF_ALLOC) != 0) | |
6a3cd2b4 | 5334 | p->p_memsz += this_hdr->sh_size; |
b301b248 | 5335 | } |
6a3cd2b4 | 5336 | else if ((this_hdr->sh_flags & SHF_ALLOC) != 0) |
13ae64f3 | 5337 | { |
6a3cd2b4 AM |
5338 | if (p->p_type == PT_TLS) |
5339 | p->p_memsz += this_hdr->sh_size; | |
5340 | ||
5341 | /* .tbss is special. It doesn't contribute to p_memsz of | |
5342 | normal segments. */ | |
5343 | else if ((this_hdr->sh_flags & SHF_TLS) == 0) | |
5344 | p->p_memsz += this_hdr->sh_size; | |
13ae64f3 JJ |
5345 | } |
5346 | ||
b10a8ae0 L |
5347 | if (align > p->p_align |
5348 | && !m->p_align_valid | |
5349 | && (p->p_type != PT_LOAD | |
5350 | || (abfd->flags & D_PAGED) == 0)) | |
252b5132 RH |
5351 | p->p_align = align; |
5352 | } | |
5353 | ||
bf988460 | 5354 | if (!m->p_flags_valid) |
252b5132 RH |
5355 | { |
5356 | p->p_flags |= PF_R; | |
02bf8d82 | 5357 | if ((this_hdr->sh_flags & SHF_EXECINSTR) != 0) |
252b5132 | 5358 | p->p_flags |= PF_X; |
02bf8d82 | 5359 | if ((this_hdr->sh_flags & SHF_WRITE) != 0) |
252b5132 RH |
5360 | p->p_flags |= PF_W; |
5361 | } | |
5362 | } | |
43a8475c | 5363 | |
bf988460 | 5364 | off -= off_adjust; |
0920dee7 | 5365 | |
7c928300 AM |
5366 | /* Check that all sections are in a PT_LOAD segment. |
5367 | Don't check funky gdb generated core files. */ | |
5368 | if (p->p_type == PT_LOAD && bfd_get_format (abfd) != bfd_core) | |
9a83a553 AM |
5369 | { |
5370 | bfd_boolean check_vma = TRUE; | |
5371 | ||
5372 | for (i = 1; i < m->count; i++) | |
5373 | if (m->sections[i]->vma == m->sections[i - 1]->vma | |
5374 | && ELF_SECTION_SIZE (&(elf_section_data (m->sections[i]) | |
5375 | ->this_hdr), p) != 0 | |
5376 | && ELF_SECTION_SIZE (&(elf_section_data (m->sections[i - 1]) | |
5377 | ->this_hdr), p) != 0) | |
0920dee7 | 5378 | { |
9a83a553 AM |
5379 | /* Looks like we have overlays packed into the segment. */ |
5380 | check_vma = FALSE; | |
5381 | break; | |
0920dee7 | 5382 | } |
9a83a553 AM |
5383 | |
5384 | for (i = 0; i < m->count; i++) | |
5385 | { | |
5386 | Elf_Internal_Shdr *this_hdr; | |
5387 | asection *sec; | |
5388 | ||
5389 | sec = m->sections[i]; | |
5390 | this_hdr = &(elf_section_data(sec)->this_hdr); | |
86b2281f AM |
5391 | if (!ELF_SECTION_IN_SEGMENT_1 (this_hdr, p, check_vma, 0) |
5392 | && !ELF_TBSS_SPECIAL (this_hdr, p)) | |
9a83a553 AM |
5393 | { |
5394 | (*_bfd_error_handler) | |
5395 | (_("%B: section `%A' can't be allocated in segment %d"), | |
5396 | abfd, sec, j); | |
5397 | print_segment_map (m); | |
5398 | } | |
5399 | } | |
5400 | } | |
252b5132 RH |
5401 | } |
5402 | ||
12bd6957 | 5403 | elf_next_file_pos (abfd) = off; |
f3520d2f AM |
5404 | return TRUE; |
5405 | } | |
5406 | ||
5407 | /* Assign file positions for the other sections. */ | |
5408 | ||
5409 | static bfd_boolean | |
5410 | assign_file_positions_for_non_load_sections (bfd *abfd, | |
5411 | struct bfd_link_info *link_info) | |
5412 | { | |
5413 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
5414 | Elf_Internal_Shdr **i_shdrpp; | |
e06efbf1 | 5415 | Elf_Internal_Shdr **hdrpp, **end_hdrpp; |
f3520d2f AM |
5416 | Elf_Internal_Phdr *phdrs; |
5417 | Elf_Internal_Phdr *p; | |
5418 | struct elf_segment_map *m; | |
62655c7b | 5419 | struct elf_segment_map *hdrs_segment; |
f3520d2f AM |
5420 | bfd_vma filehdr_vaddr, filehdr_paddr; |
5421 | bfd_vma phdrs_vaddr, phdrs_paddr; | |
5422 | file_ptr off; | |
f3520d2f AM |
5423 | unsigned int count; |
5424 | ||
5c182d5f | 5425 | i_shdrpp = elf_elfsections (abfd); |
e06efbf1 | 5426 | end_hdrpp = i_shdrpp + elf_numsections (abfd); |
12bd6957 | 5427 | off = elf_next_file_pos (abfd); |
e06efbf1 | 5428 | for (hdrpp = i_shdrpp + 1; hdrpp < end_hdrpp; hdrpp++) |
5c182d5f | 5429 | { |
5c182d5f AM |
5430 | Elf_Internal_Shdr *hdr; |
5431 | ||
5432 | hdr = *hdrpp; | |
5433 | if (hdr->bfd_section != NULL | |
252e386e AM |
5434 | && (hdr->bfd_section->filepos != 0 |
5435 | || (hdr->sh_type == SHT_NOBITS | |
5436 | && hdr->contents == NULL))) | |
627b32bc | 5437 | BFD_ASSERT (hdr->sh_offset == hdr->bfd_section->filepos); |
5c182d5f AM |
5438 | else if ((hdr->sh_flags & SHF_ALLOC) != 0) |
5439 | { | |
e8d2ba53 AM |
5440 | if (hdr->sh_size != 0) |
5441 | (*_bfd_error_handler) | |
5442 | (_("%B: warning: allocated section `%s' not in segment"), | |
5443 | abfd, | |
5444 | (hdr->bfd_section == NULL | |
5445 | ? "*unknown*" | |
5446 | : hdr->bfd_section->name)); | |
3ba71138 L |
5447 | /* We don't need to page align empty sections. */ |
5448 | if ((abfd->flags & D_PAGED) != 0 && hdr->sh_size != 0) | |
5c182d5f AM |
5449 | off += vma_page_aligned_bias (hdr->sh_addr, off, |
5450 | bed->maxpagesize); | |
5451 | else | |
5452 | off += vma_page_aligned_bias (hdr->sh_addr, off, | |
5453 | hdr->sh_addralign); | |
5454 | off = _bfd_elf_assign_file_position_for_section (hdr, off, | |
5455 | FALSE); | |
5456 | } | |
5457 | else if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA) | |
5458 | && hdr->bfd_section == NULL) | |
0ce398f1 L |
5459 | || (hdr->bfd_section != NULL |
5460 | && (hdr->bfd_section->flags & SEC_ELF_COMPRESS)) | |
5461 | /* Compress DWARF debug sections. */ | |
12bd6957 | 5462 | || hdr == i_shdrpp[elf_onesymtab (abfd)] |
6a40cf0c NC |
5463 | || (elf_symtab_shndx_list (abfd) != NULL |
5464 | && hdr == i_shdrpp[elf_symtab_shndx_list (abfd)->ndx]) | |
3e19fb8f L |
5465 | || hdr == i_shdrpp[elf_strtab_sec (abfd)] |
5466 | || hdr == i_shdrpp[elf_shstrtab_sec (abfd)]) | |
5c182d5f AM |
5467 | hdr->sh_offset = -1; |
5468 | else | |
5469 | off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE); | |
5c182d5f AM |
5470 | } |
5471 | ||
252b5132 RH |
5472 | /* Now that we have set the section file positions, we can set up |
5473 | the file positions for the non PT_LOAD segments. */ | |
f3520d2f AM |
5474 | count = 0; |
5475 | filehdr_vaddr = 0; | |
5476 | filehdr_paddr = 0; | |
5477 | phdrs_vaddr = bed->maxpagesize + bed->s->sizeof_ehdr; | |
5478 | phdrs_paddr = 0; | |
62655c7b | 5479 | hdrs_segment = NULL; |
f3520d2f | 5480 | phdrs = elf_tdata (abfd)->phdr; |
12bd6957 | 5481 | for (m = elf_seg_map (abfd), p = phdrs; m != NULL; m = m->next, p++) |
f3520d2f AM |
5482 | { |
5483 | ++count; | |
5484 | if (p->p_type != PT_LOAD) | |
5485 | continue; | |
5486 | ||
5487 | if (m->includes_filehdr) | |
5488 | { | |
5489 | filehdr_vaddr = p->p_vaddr; | |
5490 | filehdr_paddr = p->p_paddr; | |
5491 | } | |
5492 | if (m->includes_phdrs) | |
5493 | { | |
5494 | phdrs_vaddr = p->p_vaddr; | |
5495 | phdrs_paddr = p->p_paddr; | |
5496 | if (m->includes_filehdr) | |
5497 | { | |
62655c7b | 5498 | hdrs_segment = m; |
f3520d2f AM |
5499 | phdrs_vaddr += bed->s->sizeof_ehdr; |
5500 | phdrs_paddr += bed->s->sizeof_ehdr; | |
5501 | } | |
5502 | } | |
5503 | } | |
5504 | ||
62655c7b RM |
5505 | if (hdrs_segment != NULL && link_info != NULL) |
5506 | { | |
5507 | /* There is a segment that contains both the file headers and the | |
5508 | program headers, so provide a symbol __ehdr_start pointing there. | |
5509 | A program can use this to examine itself robustly. */ | |
5510 | ||
5511 | struct elf_link_hash_entry *hash | |
5512 | = elf_link_hash_lookup (elf_hash_table (link_info), "__ehdr_start", | |
5513 | FALSE, FALSE, TRUE); | |
5514 | /* If the symbol was referenced and not defined, define it. */ | |
5515 | if (hash != NULL | |
5516 | && (hash->root.type == bfd_link_hash_new | |
5517 | || hash->root.type == bfd_link_hash_undefined | |
5518 | || hash->root.type == bfd_link_hash_undefweak | |
5519 | || hash->root.type == bfd_link_hash_common)) | |
5520 | { | |
5521 | asection *s = NULL; | |
5522 | if (hdrs_segment->count != 0) | |
5523 | /* The segment contains sections, so use the first one. */ | |
5524 | s = hdrs_segment->sections[0]; | |
5525 | else | |
5526 | /* Use the first (i.e. lowest-addressed) section in any segment. */ | |
12bd6957 | 5527 | for (m = elf_seg_map (abfd); m != NULL; m = m->next) |
62655c7b RM |
5528 | if (m->count != 0) |
5529 | { | |
5530 | s = m->sections[0]; | |
5531 | break; | |
5532 | } | |
5533 | ||
5534 | if (s != NULL) | |
5535 | { | |
5536 | hash->root.u.def.value = filehdr_vaddr - s->vma; | |
5537 | hash->root.u.def.section = s; | |
5538 | } | |
5539 | else | |
5540 | { | |
5541 | hash->root.u.def.value = filehdr_vaddr; | |
5542 | hash->root.u.def.section = bfd_abs_section_ptr; | |
5543 | } | |
5544 | ||
5545 | hash->root.type = bfd_link_hash_defined; | |
5546 | hash->def_regular = 1; | |
5547 | hash->non_elf = 0; | |
5548 | } | |
5549 | } | |
5550 | ||
12bd6957 | 5551 | for (m = elf_seg_map (abfd), p = phdrs; m != NULL; m = m->next, p++) |
252b5132 | 5552 | { |
129af99f | 5553 | if (p->p_type == PT_GNU_RELRO) |
252b5132 | 5554 | { |
b84a33b5 | 5555 | const Elf_Internal_Phdr *lp; |
3146fac4 | 5556 | struct elf_segment_map *lm; |
1ea63fd2 | 5557 | |
129af99f | 5558 | if (link_info != NULL) |
8c37241b | 5559 | { |
129af99f AS |
5560 | /* During linking the range of the RELRO segment is passed |
5561 | in link_info. */ | |
12bd6957 | 5562 | for (lm = elf_seg_map (abfd), lp = phdrs; |
3146fac4 AM |
5563 | lm != NULL; |
5564 | lm = lm->next, lp++) | |
8c37241b JJ |
5565 | { |
5566 | if (lp->p_type == PT_LOAD | |
b84a33b5 | 5567 | && lp->p_vaddr < link_info->relro_end |
3146fac4 AM |
5568 | && lm->count != 0 |
5569 | && lm->sections[0]->vma >= link_info->relro_start) | |
8c37241b JJ |
5570 | break; |
5571 | } | |
8981c88a | 5572 | |
3146fac4 | 5573 | BFD_ASSERT (lm != NULL); |
8c37241b | 5574 | } |
129af99f AS |
5575 | else |
5576 | { | |
5577 | /* Otherwise we are copying an executable or shared | |
b84a33b5 | 5578 | library, but we need to use the same linker logic. */ |
129af99f AS |
5579 | for (lp = phdrs; lp < phdrs + count; ++lp) |
5580 | { | |
5581 | if (lp->p_type == PT_LOAD | |
5582 | && lp->p_paddr == p->p_paddr) | |
5583 | break; | |
5584 | } | |
b84a33b5 AM |
5585 | } |
5586 | ||
5587 | if (lp < phdrs + count) | |
5588 | { | |
5589 | p->p_vaddr = lp->p_vaddr; | |
5590 | p->p_paddr = lp->p_paddr; | |
5591 | p->p_offset = lp->p_offset; | |
5592 | if (link_info != NULL) | |
5593 | p->p_filesz = link_info->relro_end - lp->p_vaddr; | |
5594 | else if (m->p_size_valid) | |
5595 | p->p_filesz = m->p_size; | |
129af99f AS |
5596 | else |
5597 | abort (); | |
b84a33b5 | 5598 | p->p_memsz = p->p_filesz; |
f3944f72 L |
5599 | /* Preserve the alignment and flags if they are valid. The |
5600 | gold linker generates RW/4 for the PT_GNU_RELRO section. | |
5601 | It is better for objcopy/strip to honor these attributes | |
5602 | otherwise gdb will choke when using separate debug files. | |
5603 | */ | |
5604 | if (!m->p_align_valid) | |
5605 | p->p_align = 1; | |
5606 | if (!m->p_flags_valid) | |
341b8ee7 | 5607 | p->p_flags = PF_R; |
129af99f | 5608 | } |
9433b9b1 | 5609 | else |
b84a33b5 AM |
5610 | { |
5611 | memset (p, 0, sizeof *p); | |
5612 | p->p_type = PT_NULL; | |
5613 | } | |
129af99f | 5614 | } |
04c3a755 NS |
5615 | else if (p->p_type == PT_GNU_STACK) |
5616 | { | |
5617 | if (m->p_size_valid) | |
5618 | p->p_memsz = m->p_size; | |
5619 | } | |
129af99f AS |
5620 | else if (m->count != 0) |
5621 | { | |
e06efbf1 | 5622 | unsigned int i; |
129af99f AS |
5623 | if (p->p_type != PT_LOAD |
5624 | && (p->p_type != PT_NOTE | |
5625 | || bfd_get_format (abfd) != bfd_core)) | |
5626 | { | |
c86934ce NC |
5627 | if (m->includes_filehdr || m->includes_phdrs) |
5628 | { | |
b1fa9dd6 | 5629 | /* PR 17512: file: 2195325e. */ |
c86934ce NC |
5630 | (*_bfd_error_handler) |
5631 | (_("%B: warning: non-load segment includes file header and/or program header"), | |
5632 | abfd); | |
5633 | return FALSE; | |
5634 | } | |
129af99f | 5635 | |
86b2281f | 5636 | p->p_filesz = 0; |
129af99f | 5637 | p->p_offset = m->sections[0]->filepos; |
86b2281f AM |
5638 | for (i = m->count; i-- != 0;) |
5639 | { | |
5640 | asection *sect = m->sections[i]; | |
5641 | Elf_Internal_Shdr *hdr = &elf_section_data (sect)->this_hdr; | |
5642 | if (hdr->sh_type != SHT_NOBITS) | |
5643 | { | |
5644 | p->p_filesz = (sect->filepos - m->sections[0]->filepos | |
5645 | + hdr->sh_size); | |
5646 | break; | |
5647 | } | |
5648 | } | |
129af99f AS |
5649 | } |
5650 | } | |
5651 | else if (m->includes_filehdr) | |
5652 | { | |
5653 | p->p_vaddr = filehdr_vaddr; | |
5654 | if (! m->p_paddr_valid) | |
5655 | p->p_paddr = filehdr_paddr; | |
5656 | } | |
5657 | else if (m->includes_phdrs) | |
5658 | { | |
5659 | p->p_vaddr = phdrs_vaddr; | |
5660 | if (! m->p_paddr_valid) | |
5661 | p->p_paddr = phdrs_paddr; | |
252b5132 RH |
5662 | } |
5663 | } | |
5664 | ||
12bd6957 | 5665 | elf_next_file_pos (abfd) = off; |
252b5132 | 5666 | |
b34976b6 | 5667 | return TRUE; |
252b5132 RH |
5668 | } |
5669 | ||
6a40cf0c NC |
5670 | static elf_section_list * |
5671 | find_section_in_list (unsigned int i, elf_section_list * list) | |
5672 | { | |
5673 | for (;list != NULL; list = list->next) | |
5674 | if (list->ndx == i) | |
5675 | break; | |
5676 | return list; | |
5677 | } | |
5678 | ||
252b5132 RH |
5679 | /* Work out the file positions of all the sections. This is called by |
5680 | _bfd_elf_compute_section_file_positions. All the section sizes and | |
5681 | VMAs must be known before this is called. | |
5682 | ||
e0638f70 AM |
5683 | Reloc sections come in two flavours: Those processed specially as |
5684 | "side-channel" data attached to a section to which they apply, and | |
5685 | those that bfd doesn't process as relocations. The latter sort are | |
5686 | stored in a normal bfd section by bfd_section_from_shdr. We don't | |
5687 | consider the former sort here, unless they form part of the loadable | |
5688 | image. Reloc sections not assigned here will be handled later by | |
5689 | assign_file_positions_for_relocs. | |
252b5132 RH |
5690 | |
5691 | We also don't set the positions of the .symtab and .strtab here. */ | |
5692 | ||
b34976b6 | 5693 | static bfd_boolean |
c84fca4d AO |
5694 | assign_file_positions_except_relocs (bfd *abfd, |
5695 | struct bfd_link_info *link_info) | |
252b5132 | 5696 | { |
5c182d5f AM |
5697 | struct elf_obj_tdata *tdata = elf_tdata (abfd); |
5698 | Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd); | |
9c5bfbb7 | 5699 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
252b5132 RH |
5700 | |
5701 | if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0 | |
5702 | && bfd_get_format (abfd) != bfd_core) | |
5703 | { | |
5c182d5f AM |
5704 | Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd); |
5705 | unsigned int num_sec = elf_numsections (abfd); | |
252b5132 RH |
5706 | Elf_Internal_Shdr **hdrpp; |
5707 | unsigned int i; | |
a485e98e | 5708 | file_ptr off; |
252b5132 RH |
5709 | |
5710 | /* Start after the ELF header. */ | |
5711 | off = i_ehdrp->e_ehsize; | |
5712 | ||
5713 | /* We are not creating an executable, which means that we are | |
5714 | not creating a program header, and that the actual order of | |
5715 | the sections in the file is unimportant. */ | |
9ad5cbcf | 5716 | for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++) |
252b5132 RH |
5717 | { |
5718 | Elf_Internal_Shdr *hdr; | |
5719 | ||
5720 | hdr = *hdrpp; | |
e0638f70 AM |
5721 | if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA) |
5722 | && hdr->bfd_section == NULL) | |
0ce398f1 L |
5723 | || (hdr->bfd_section != NULL |
5724 | && (hdr->bfd_section->flags & SEC_ELF_COMPRESS)) | |
5725 | /* Compress DWARF debug sections. */ | |
12bd6957 | 5726 | || i == elf_onesymtab (abfd) |
6a40cf0c NC |
5727 | || (elf_symtab_shndx_list (abfd) != NULL |
5728 | && hdr == i_shdrpp[elf_symtab_shndx_list (abfd)->ndx]) | |
3e19fb8f L |
5729 | || i == elf_strtab_sec (abfd) |
5730 | || i == elf_shstrtab_sec (abfd)) | |
252b5132 RH |
5731 | { |
5732 | hdr->sh_offset = -1; | |
252b5132 | 5733 | } |
9ad5cbcf | 5734 | else |
b34976b6 | 5735 | off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE); |
252b5132 | 5736 | } |
a485e98e AM |
5737 | |
5738 | elf_next_file_pos (abfd) = off; | |
252b5132 RH |
5739 | } |
5740 | else | |
5741 | { | |
f3520d2f AM |
5742 | unsigned int alloc; |
5743 | ||
252b5132 | 5744 | /* Assign file positions for the loaded sections based on the |
08a40648 | 5745 | assignment of sections to segments. */ |
f3520d2f AM |
5746 | if (!assign_file_positions_for_load_sections (abfd, link_info)) |
5747 | return FALSE; | |
5748 | ||
5749 | /* And for non-load sections. */ | |
5750 | if (!assign_file_positions_for_non_load_sections (abfd, link_info)) | |
5751 | return FALSE; | |
5752 | ||
e36284ab AM |
5753 | if (bed->elf_backend_modify_program_headers != NULL) |
5754 | { | |
5755 | if (!(*bed->elf_backend_modify_program_headers) (abfd, link_info)) | |
5756 | return FALSE; | |
5757 | } | |
5758 | ||
58e7ebac | 5759 | /* Set e_type in ELF header to ET_EXEC for -pie -Ttext-segment=. */ |
0e1862bb | 5760 | if (link_info != NULL && bfd_link_pie (link_info)) |
58e7ebac L |
5761 | { |
5762 | unsigned int num_segments = elf_elfheader (abfd)->e_phnum; | |
5763 | Elf_Internal_Phdr *segment = elf_tdata (abfd)->phdr; | |
5764 | Elf_Internal_Phdr *end_segment = &segment[num_segments]; | |
5765 | ||
5766 | /* Find the lowest p_vaddr in PT_LOAD segments. */ | |
5767 | bfd_vma p_vaddr = (bfd_vma) -1; | |
5768 | for (; segment < end_segment; segment++) | |
5769 | if (segment->p_type == PT_LOAD && p_vaddr > segment->p_vaddr) | |
5770 | p_vaddr = segment->p_vaddr; | |
5771 | ||
5772 | /* Set e_type to ET_EXEC if the lowest p_vaddr in PT_LOAD | |
5773 | segments is non-zero. */ | |
5774 | if (p_vaddr) | |
5775 | i_ehdrp->e_type = ET_EXEC; | |
5776 | } | |
5777 | ||
f3520d2f | 5778 | /* Write out the program headers. */ |
12bd6957 | 5779 | alloc = elf_program_header_size (abfd) / bed->s->sizeof_phdr; |
f3520d2f AM |
5780 | if (bfd_seek (abfd, (bfd_signed_vma) bed->s->sizeof_ehdr, SEEK_SET) != 0 |
5781 | || bed->s->write_out_phdrs (abfd, tdata->phdr, alloc) != 0) | |
b34976b6 | 5782 | return FALSE; |
252b5132 RH |
5783 | } |
5784 | ||
b34976b6 | 5785 | return TRUE; |
252b5132 RH |
5786 | } |
5787 | ||
b34976b6 | 5788 | static bfd_boolean |
217aa764 | 5789 | prep_headers (bfd *abfd) |
252b5132 | 5790 | { |
3d540e93 | 5791 | Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form. */ |
2b0f7ef9 | 5792 | struct elf_strtab_hash *shstrtab; |
9c5bfbb7 | 5793 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
252b5132 RH |
5794 | |
5795 | i_ehdrp = elf_elfheader (abfd); | |
252b5132 | 5796 | |
2b0f7ef9 | 5797 | shstrtab = _bfd_elf_strtab_init (); |
252b5132 | 5798 | if (shstrtab == NULL) |
b34976b6 | 5799 | return FALSE; |
252b5132 RH |
5800 | |
5801 | elf_shstrtab (abfd) = shstrtab; | |
5802 | ||
5803 | i_ehdrp->e_ident[EI_MAG0] = ELFMAG0; | |
5804 | i_ehdrp->e_ident[EI_MAG1] = ELFMAG1; | |
5805 | i_ehdrp->e_ident[EI_MAG2] = ELFMAG2; | |
5806 | i_ehdrp->e_ident[EI_MAG3] = ELFMAG3; | |
5807 | ||
5808 | i_ehdrp->e_ident[EI_CLASS] = bed->s->elfclass; | |
5809 | i_ehdrp->e_ident[EI_DATA] = | |
5810 | bfd_big_endian (abfd) ? ELFDATA2MSB : ELFDATA2LSB; | |
5811 | i_ehdrp->e_ident[EI_VERSION] = bed->s->ev_current; | |
5812 | ||
252b5132 RH |
5813 | if ((abfd->flags & DYNAMIC) != 0) |
5814 | i_ehdrp->e_type = ET_DYN; | |
5815 | else if ((abfd->flags & EXEC_P) != 0) | |
5816 | i_ehdrp->e_type = ET_EXEC; | |
5817 | else if (bfd_get_format (abfd) == bfd_core) | |
5818 | i_ehdrp->e_type = ET_CORE; | |
5819 | else | |
5820 | i_ehdrp->e_type = ET_REL; | |
5821 | ||
5822 | switch (bfd_get_arch (abfd)) | |
5823 | { | |
5824 | case bfd_arch_unknown: | |
5825 | i_ehdrp->e_machine = EM_NONE; | |
5826 | break; | |
aa4f99bb AO |
5827 | |
5828 | /* There used to be a long list of cases here, each one setting | |
5829 | e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE | |
5830 | in the corresponding bfd definition. To avoid duplication, | |
5831 | the switch was removed. Machines that need special handling | |
5832 | can generally do it in elf_backend_final_write_processing(), | |
5833 | unless they need the information earlier than the final write. | |
5834 | Such need can generally be supplied by replacing the tests for | |
5835 | e_machine with the conditions used to determine it. */ | |
252b5132 | 5836 | default: |
9c5bfbb7 AM |
5837 | i_ehdrp->e_machine = bed->elf_machine_code; |
5838 | } | |
aa4f99bb | 5839 | |
252b5132 RH |
5840 | i_ehdrp->e_version = bed->s->ev_current; |
5841 | i_ehdrp->e_ehsize = bed->s->sizeof_ehdr; | |
5842 | ||
c044fabd | 5843 | /* No program header, for now. */ |
252b5132 RH |
5844 | i_ehdrp->e_phoff = 0; |
5845 | i_ehdrp->e_phentsize = 0; | |
5846 | i_ehdrp->e_phnum = 0; | |
5847 | ||
c044fabd | 5848 | /* Each bfd section is section header entry. */ |
252b5132 RH |
5849 | i_ehdrp->e_entry = bfd_get_start_address (abfd); |
5850 | i_ehdrp->e_shentsize = bed->s->sizeof_shdr; | |
5851 | ||
c044fabd | 5852 | /* If we're building an executable, we'll need a program header table. */ |
252b5132 | 5853 | if (abfd->flags & EXEC_P) |
0e71e495 BE |
5854 | /* It all happens later. */ |
5855 | ; | |
252b5132 RH |
5856 | else |
5857 | { | |
5858 | i_ehdrp->e_phentsize = 0; | |
252b5132 RH |
5859 | i_ehdrp->e_phoff = 0; |
5860 | } | |
5861 | ||
5862 | elf_tdata (abfd)->symtab_hdr.sh_name = | |
b34976b6 | 5863 | (unsigned int) _bfd_elf_strtab_add (shstrtab, ".symtab", FALSE); |
252b5132 | 5864 | elf_tdata (abfd)->strtab_hdr.sh_name = |
b34976b6 | 5865 | (unsigned int) _bfd_elf_strtab_add (shstrtab, ".strtab", FALSE); |
252b5132 | 5866 | elf_tdata (abfd)->shstrtab_hdr.sh_name = |
b34976b6 | 5867 | (unsigned int) _bfd_elf_strtab_add (shstrtab, ".shstrtab", FALSE); |
252b5132 | 5868 | if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1 |
17ca87fc | 5869 | || elf_tdata (abfd)->strtab_hdr.sh_name == (unsigned int) -1 |
252b5132 | 5870 | || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1) |
b34976b6 | 5871 | return FALSE; |
252b5132 | 5872 | |
b34976b6 | 5873 | return TRUE; |
252b5132 RH |
5874 | } |
5875 | ||
5876 | /* Assign file positions for all the reloc sections which are not part | |
a485e98e | 5877 | of the loadable file image, and the file position of section headers. */ |
252b5132 | 5878 | |
0ce398f1 L |
5879 | static bfd_boolean |
5880 | _bfd_elf_assign_file_positions_for_non_load (bfd *abfd) | |
252b5132 RH |
5881 | { |
5882 | file_ptr off; | |
e06efbf1 | 5883 | Elf_Internal_Shdr **shdrpp, **end_shdrpp; |
3e19fb8f | 5884 | Elf_Internal_Shdr *shdrp; |
a485e98e AM |
5885 | Elf_Internal_Ehdr *i_ehdrp; |
5886 | const struct elf_backend_data *bed; | |
252b5132 | 5887 | |
12bd6957 | 5888 | off = elf_next_file_pos (abfd); |
252b5132 | 5889 | |
e06efbf1 L |
5890 | shdrpp = elf_elfsections (abfd); |
5891 | end_shdrpp = shdrpp + elf_numsections (abfd); | |
5892 | for (shdrpp++; shdrpp < end_shdrpp; shdrpp++) | |
252b5132 | 5893 | { |
252b5132 | 5894 | shdrp = *shdrpp; |
0ce398f1 L |
5895 | if (shdrp->sh_offset == -1) |
5896 | { | |
3e19fb8f | 5897 | asection *sec = shdrp->bfd_section; |
0ce398f1 L |
5898 | bfd_boolean is_rel = (shdrp->sh_type == SHT_REL |
5899 | || shdrp->sh_type == SHT_RELA); | |
5900 | if (is_rel | |
3e19fb8f | 5901 | || (sec != NULL && (sec->flags & SEC_ELF_COMPRESS))) |
0ce398f1 L |
5902 | { |
5903 | if (!is_rel) | |
5904 | { | |
3e19fb8f L |
5905 | const char *name = sec->name; |
5906 | struct bfd_elf_section_data *d; | |
5907 | ||
0ce398f1 | 5908 | /* Compress DWARF debug sections. */ |
3e19fb8f | 5909 | if (!bfd_compress_section (abfd, sec, |
0ce398f1 L |
5910 | shdrp->contents)) |
5911 | return FALSE; | |
3e19fb8f L |
5912 | |
5913 | if (sec->compress_status == COMPRESS_SECTION_DONE | |
5914 | && (abfd->flags & BFD_COMPRESS_GABI) == 0) | |
5915 | { | |
5916 | /* If section is compressed with zlib-gnu, convert | |
5917 | section name from .debug_* to .zdebug_*. */ | |
5918 | char *new_name | |
5919 | = convert_debug_to_zdebug (abfd, name); | |
5920 | if (new_name == NULL) | |
5921 | return FALSE; | |
5922 | name = new_name; | |
5923 | } | |
5924 | /* Add setion name to section name section. */ | |
5925 | if (shdrp->sh_name != (unsigned int) -1) | |
5926 | abort (); | |
5927 | shdrp->sh_name | |
5928 | = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), | |
5929 | name, FALSE); | |
5930 | d = elf_section_data (sec); | |
5931 | ||
5932 | /* Add reloc setion name to section name section. */ | |
5933 | if (d->rel.hdr | |
5934 | && !_bfd_elf_set_reloc_sh_name (abfd, | |
5935 | d->rel.hdr, | |
5936 | name, FALSE)) | |
5937 | return FALSE; | |
5938 | if (d->rela.hdr | |
5939 | && !_bfd_elf_set_reloc_sh_name (abfd, | |
5940 | d->rela.hdr, | |
91cb26da | 5941 | name, TRUE)) |
3e19fb8f L |
5942 | return FALSE; |
5943 | ||
0ce398f1 | 5944 | /* Update section size and contents. */ |
3e19fb8f L |
5945 | shdrp->sh_size = sec->size; |
5946 | shdrp->contents = sec->contents; | |
0ce398f1 L |
5947 | shdrp->bfd_section->contents = NULL; |
5948 | } | |
5949 | off = _bfd_elf_assign_file_position_for_section (shdrp, | |
5950 | off, | |
5951 | TRUE); | |
5952 | } | |
5953 | } | |
252b5132 RH |
5954 | } |
5955 | ||
3e19fb8f L |
5956 | /* Place section name section after DWARF debug sections have been |
5957 | compressed. */ | |
5958 | _bfd_elf_strtab_finalize (elf_shstrtab (abfd)); | |
5959 | shdrp = &elf_tdata (abfd)->shstrtab_hdr; | |
5960 | shdrp->sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd)); | |
5961 | off = _bfd_elf_assign_file_position_for_section (shdrp, off, TRUE); | |
5962 | ||
5963 | /* Place the section headers. */ | |
a485e98e AM |
5964 | i_ehdrp = elf_elfheader (abfd); |
5965 | bed = get_elf_backend_data (abfd); | |
5966 | off = align_file_position (off, 1 << bed->s->log_file_align); | |
5967 | i_ehdrp->e_shoff = off; | |
5968 | off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize; | |
12bd6957 | 5969 | elf_next_file_pos (abfd) = off; |
0ce398f1 L |
5970 | |
5971 | return TRUE; | |
252b5132 RH |
5972 | } |
5973 | ||
b34976b6 | 5974 | bfd_boolean |
217aa764 | 5975 | _bfd_elf_write_object_contents (bfd *abfd) |
252b5132 | 5976 | { |
9c5bfbb7 | 5977 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
252b5132 | 5978 | Elf_Internal_Shdr **i_shdrp; |
b34976b6 | 5979 | bfd_boolean failed; |
9ad5cbcf | 5980 | unsigned int count, num_sec; |
30e8ee25 | 5981 | struct elf_obj_tdata *t; |
252b5132 RH |
5982 | |
5983 | if (! abfd->output_has_begun | |
217aa764 | 5984 | && ! _bfd_elf_compute_section_file_positions (abfd, NULL)) |
b34976b6 | 5985 | return FALSE; |
252b5132 RH |
5986 | |
5987 | i_shdrp = elf_elfsections (abfd); | |
252b5132 | 5988 | |
b34976b6 | 5989 | failed = FALSE; |
252b5132 RH |
5990 | bfd_map_over_sections (abfd, bed->s->write_relocs, &failed); |
5991 | if (failed) | |
b34976b6 | 5992 | return FALSE; |
252b5132 | 5993 | |
0ce398f1 L |
5994 | if (!_bfd_elf_assign_file_positions_for_non_load (abfd)) |
5995 | return FALSE; | |
252b5132 | 5996 | |
c044fabd | 5997 | /* After writing the headers, we need to write the sections too... */ |
9ad5cbcf AM |
5998 | num_sec = elf_numsections (abfd); |
5999 | for (count = 1; count < num_sec; count++) | |
252b5132 | 6000 | { |
3e19fb8f L |
6001 | i_shdrp[count]->sh_name |
6002 | = _bfd_elf_strtab_offset (elf_shstrtab (abfd), | |
6003 | i_shdrp[count]->sh_name); | |
252b5132 RH |
6004 | if (bed->elf_backend_section_processing) |
6005 | (*bed->elf_backend_section_processing) (abfd, i_shdrp[count]); | |
6006 | if (i_shdrp[count]->contents) | |
6007 | { | |
dc810e39 AM |
6008 | bfd_size_type amt = i_shdrp[count]->sh_size; |
6009 | ||
252b5132 | 6010 | if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0 |
dc810e39 | 6011 | || bfd_bwrite (i_shdrp[count]->contents, amt, abfd) != amt) |
b34976b6 | 6012 | return FALSE; |
252b5132 RH |
6013 | } |
6014 | } | |
6015 | ||
6016 | /* Write out the section header names. */ | |
30e8ee25 | 6017 | t = elf_tdata (abfd); |
26ae6d5e | 6018 | if (elf_shstrtab (abfd) != NULL |
30e8ee25 | 6019 | && (bfd_seek (abfd, t->shstrtab_hdr.sh_offset, SEEK_SET) != 0 |
08a40648 | 6020 | || !_bfd_elf_strtab_emit (abfd, elf_shstrtab (abfd)))) |
b34976b6 | 6021 | return FALSE; |
252b5132 RH |
6022 | |
6023 | if (bed->elf_backend_final_write_processing) | |
12bd6957 | 6024 | (*bed->elf_backend_final_write_processing) (abfd, elf_linker (abfd)); |
252b5132 | 6025 | |
ff59fc36 RM |
6026 | if (!bed->s->write_shdrs_and_ehdr (abfd)) |
6027 | return FALSE; | |
6028 | ||
6029 | /* This is last since write_shdrs_and_ehdr can touch i_shdrp[0]. */ | |
c0355132 AM |
6030 | if (t->o->build_id.after_write_object_contents != NULL) |
6031 | return (*t->o->build_id.after_write_object_contents) (abfd); | |
ff59fc36 RM |
6032 | |
6033 | return TRUE; | |
252b5132 RH |
6034 | } |
6035 | ||
b34976b6 | 6036 | bfd_boolean |
217aa764 | 6037 | _bfd_elf_write_corefile_contents (bfd *abfd) |
252b5132 | 6038 | { |
c044fabd | 6039 | /* Hopefully this can be done just like an object file. */ |
252b5132 RH |
6040 | return _bfd_elf_write_object_contents (abfd); |
6041 | } | |
c044fabd KH |
6042 | |
6043 | /* Given a section, search the header to find them. */ | |
6044 | ||
cb33740c | 6045 | unsigned int |
198beae2 | 6046 | _bfd_elf_section_from_bfd_section (bfd *abfd, struct bfd_section *asect) |
252b5132 | 6047 | { |
9c5bfbb7 | 6048 | const struct elf_backend_data *bed; |
91d6fa6a | 6049 | unsigned int sec_index; |
252b5132 | 6050 | |
9ad5cbcf AM |
6051 | if (elf_section_data (asect) != NULL |
6052 | && elf_section_data (asect)->this_idx != 0) | |
6053 | return elf_section_data (asect)->this_idx; | |
6054 | ||
6055 | if (bfd_is_abs_section (asect)) | |
91d6fa6a | 6056 | sec_index = SHN_ABS; |
af746e92 | 6057 | else if (bfd_is_com_section (asect)) |
91d6fa6a | 6058 | sec_index = SHN_COMMON; |
af746e92 | 6059 | else if (bfd_is_und_section (asect)) |
91d6fa6a | 6060 | sec_index = SHN_UNDEF; |
af746e92 | 6061 | else |
91d6fa6a | 6062 | sec_index = SHN_BAD; |
252b5132 | 6063 | |
af746e92 | 6064 | bed = get_elf_backend_data (abfd); |
252b5132 RH |
6065 | if (bed->elf_backend_section_from_bfd_section) |
6066 | { | |
91d6fa6a | 6067 | int retval = sec_index; |
9ad5cbcf | 6068 | |
af746e92 AM |
6069 | if ((*bed->elf_backend_section_from_bfd_section) (abfd, asect, &retval)) |
6070 | return retval; | |
252b5132 RH |
6071 | } |
6072 | ||
91d6fa6a | 6073 | if (sec_index == SHN_BAD) |
af746e92 | 6074 | bfd_set_error (bfd_error_nonrepresentable_section); |
252b5132 | 6075 | |
91d6fa6a | 6076 | return sec_index; |
252b5132 RH |
6077 | } |
6078 | ||
6079 | /* Given a BFD symbol, return the index in the ELF symbol table, or -1 | |
6080 | on error. */ | |
6081 | ||
6082 | int | |
217aa764 | 6083 | _bfd_elf_symbol_from_bfd_symbol (bfd *abfd, asymbol **asym_ptr_ptr) |
252b5132 RH |
6084 | { |
6085 | asymbol *asym_ptr = *asym_ptr_ptr; | |
6086 | int idx; | |
6087 | flagword flags = asym_ptr->flags; | |
6088 | ||
6089 | /* When gas creates relocations against local labels, it creates its | |
6090 | own symbol for the section, but does put the symbol into the | |
6091 | symbol chain, so udata is 0. When the linker is generating | |
6092 | relocatable output, this section symbol may be for one of the | |
6093 | input sections rather than the output section. */ | |
6094 | if (asym_ptr->udata.i == 0 | |
6095 | && (flags & BSF_SECTION_SYM) | |
6096 | && asym_ptr->section) | |
6097 | { | |
5372391b | 6098 | asection *sec; |
252b5132 RH |
6099 | int indx; |
6100 | ||
5372391b AM |
6101 | sec = asym_ptr->section; |
6102 | if (sec->owner != abfd && sec->output_section != NULL) | |
6103 | sec = sec->output_section; | |
6104 | if (sec->owner == abfd | |
6105 | && (indx = sec->index) < elf_num_section_syms (abfd) | |
4e89ac30 | 6106 | && elf_section_syms (abfd)[indx] != NULL) |
252b5132 RH |
6107 | asym_ptr->udata.i = elf_section_syms (abfd)[indx]->udata.i; |
6108 | } | |
6109 | ||
6110 | idx = asym_ptr->udata.i; | |
6111 | ||
6112 | if (idx == 0) | |
6113 | { | |
6114 | /* This case can occur when using --strip-symbol on a symbol | |
08a40648 | 6115 | which is used in a relocation entry. */ |
252b5132 | 6116 | (*_bfd_error_handler) |
d003868e AM |
6117 | (_("%B: symbol `%s' required but not present"), |
6118 | abfd, bfd_asymbol_name (asym_ptr)); | |
252b5132 RH |
6119 | bfd_set_error (bfd_error_no_symbols); |
6120 | return -1; | |
6121 | } | |
6122 | ||
6123 | #if DEBUG & 4 | |
6124 | { | |
6125 | fprintf (stderr, | |
9ccb8af9 AM |
6126 | "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8lx\n", |
6127 | (long) asym_ptr, asym_ptr->name, idx, (long) flags); | |
252b5132 RH |
6128 | fflush (stderr); |
6129 | } | |
6130 | #endif | |
6131 | ||
6132 | return idx; | |
6133 | } | |
6134 | ||
84d1d650 | 6135 | /* Rewrite program header information. */ |
252b5132 | 6136 | |
b34976b6 | 6137 | static bfd_boolean |
84d1d650 | 6138 | rewrite_elf_program_header (bfd *ibfd, bfd *obfd) |
252b5132 | 6139 | { |
b34976b6 AM |
6140 | Elf_Internal_Ehdr *iehdr; |
6141 | struct elf_segment_map *map; | |
6142 | struct elf_segment_map *map_first; | |
6143 | struct elf_segment_map **pointer_to_map; | |
6144 | Elf_Internal_Phdr *segment; | |
6145 | asection *section; | |
6146 | unsigned int i; | |
6147 | unsigned int num_segments; | |
6148 | bfd_boolean phdr_included = FALSE; | |
5c44b38e | 6149 | bfd_boolean p_paddr_valid; |
b34976b6 AM |
6150 | bfd_vma maxpagesize; |
6151 | struct elf_segment_map *phdr_adjust_seg = NULL; | |
6152 | unsigned int phdr_adjust_num = 0; | |
9c5bfbb7 | 6153 | const struct elf_backend_data *bed; |
bc67d8a6 | 6154 | |
caf47ea6 | 6155 | bed = get_elf_backend_data (ibfd); |
252b5132 RH |
6156 | iehdr = elf_elfheader (ibfd); |
6157 | ||
bc67d8a6 | 6158 | map_first = NULL; |
c044fabd | 6159 | pointer_to_map = &map_first; |
252b5132 RH |
6160 | |
6161 | num_segments = elf_elfheader (ibfd)->e_phnum; | |
bc67d8a6 NC |
6162 | maxpagesize = get_elf_backend_data (obfd)->maxpagesize; |
6163 | ||
6164 | /* Returns the end address of the segment + 1. */ | |
aecc8f8a AM |
6165 | #define SEGMENT_END(segment, start) \ |
6166 | (start + (segment->p_memsz > segment->p_filesz \ | |
6167 | ? segment->p_memsz : segment->p_filesz)) | |
bc67d8a6 | 6168 | |
eecdbe52 JJ |
6169 | #define SECTION_SIZE(section, segment) \ |
6170 | (((section->flags & (SEC_HAS_CONTENTS | SEC_THREAD_LOCAL)) \ | |
6171 | != SEC_THREAD_LOCAL || segment->p_type == PT_TLS) \ | |
eea6121a | 6172 | ? section->size : 0) |
eecdbe52 | 6173 | |
b34976b6 | 6174 | /* Returns TRUE if the given section is contained within |
bc67d8a6 | 6175 | the given segment. VMA addresses are compared. */ |
aecc8f8a AM |
6176 | #define IS_CONTAINED_BY_VMA(section, segment) \ |
6177 | (section->vma >= segment->p_vaddr \ | |
eecdbe52 | 6178 | && (section->vma + SECTION_SIZE (section, segment) \ |
aecc8f8a | 6179 | <= (SEGMENT_END (segment, segment->p_vaddr)))) |
c044fabd | 6180 | |
b34976b6 | 6181 | /* Returns TRUE if the given section is contained within |
bc67d8a6 | 6182 | the given segment. LMA addresses are compared. */ |
aecc8f8a AM |
6183 | #define IS_CONTAINED_BY_LMA(section, segment, base) \ |
6184 | (section->lma >= base \ | |
eecdbe52 | 6185 | && (section->lma + SECTION_SIZE (section, segment) \ |
aecc8f8a | 6186 | <= SEGMENT_END (segment, base))) |
252b5132 | 6187 | |
0efc80c8 L |
6188 | /* Handle PT_NOTE segment. */ |
6189 | #define IS_NOTE(p, s) \ | |
aecc8f8a | 6190 | (p->p_type == PT_NOTE \ |
0efc80c8 | 6191 | && elf_section_type (s) == SHT_NOTE \ |
aecc8f8a | 6192 | && (bfd_vma) s->filepos >= p->p_offset \ |
cb3ff1e5 | 6193 | && ((bfd_vma) s->filepos + s->size \ |
aecc8f8a | 6194 | <= p->p_offset + p->p_filesz)) |
252b5132 | 6195 | |
0efc80c8 L |
6196 | /* Special case: corefile "NOTE" section containing regs, prpsinfo |
6197 | etc. */ | |
6198 | #define IS_COREFILE_NOTE(p, s) \ | |
6199 | (IS_NOTE (p, s) \ | |
6200 | && bfd_get_format (ibfd) == bfd_core \ | |
6201 | && s->vma == 0 \ | |
6202 | && s->lma == 0) | |
6203 | ||
252b5132 RH |
6204 | /* The complicated case when p_vaddr is 0 is to handle the Solaris |
6205 | linker, which generates a PT_INTERP section with p_vaddr and | |
6206 | p_memsz set to 0. */ | |
aecc8f8a AM |
6207 | #define IS_SOLARIS_PT_INTERP(p, s) \ |
6208 | (p->p_vaddr == 0 \ | |
6209 | && p->p_paddr == 0 \ | |
6210 | && p->p_memsz == 0 \ | |
6211 | && p->p_filesz > 0 \ | |
6212 | && (s->flags & SEC_HAS_CONTENTS) != 0 \ | |
eea6121a | 6213 | && s->size > 0 \ |
aecc8f8a | 6214 | && (bfd_vma) s->filepos >= p->p_offset \ |
cb3ff1e5 | 6215 | && ((bfd_vma) s->filepos + s->size \ |
aecc8f8a | 6216 | <= p->p_offset + p->p_filesz)) |
5c440b1e | 6217 | |
bc67d8a6 NC |
6218 | /* Decide if the given section should be included in the given segment. |
6219 | A section will be included if: | |
f5ffc919 | 6220 | 1. It is within the address space of the segment -- we use the LMA |
08a40648 | 6221 | if that is set for the segment and the VMA otherwise, |
0efc80c8 | 6222 | 2. It is an allocated section or a NOTE section in a PT_NOTE |
d324f6d6 | 6223 | segment. |
bc67d8a6 | 6224 | 3. There is an output section associated with it, |
eecdbe52 | 6225 | 4. The section has not already been allocated to a previous segment. |
2b05f1b7 | 6226 | 5. PT_GNU_STACK segments do not include any sections. |
03394ac9 | 6227 | 6. PT_TLS segment includes only SHF_TLS sections. |
6f79b219 JJ |
6228 | 7. SHF_TLS sections are only in PT_TLS or PT_LOAD segments. |
6229 | 8. PT_DYNAMIC should not contain empty sections at the beginning | |
08a40648 | 6230 | (with the possible exception of .dynamic). */ |
9f17e2a6 | 6231 | #define IS_SECTION_IN_INPUT_SEGMENT(section, segment, bed) \ |
2b05f1b7 L |
6232 | ((((segment->p_paddr \ |
6233 | ? IS_CONTAINED_BY_LMA (section, segment, segment->p_paddr) \ | |
6234 | : IS_CONTAINED_BY_VMA (section, segment)) \ | |
6235 | && (section->flags & SEC_ALLOC) != 0) \ | |
0efc80c8 | 6236 | || IS_NOTE (segment, section)) \ |
2b05f1b7 L |
6237 | && segment->p_type != PT_GNU_STACK \ |
6238 | && (segment->p_type != PT_TLS \ | |
6239 | || (section->flags & SEC_THREAD_LOCAL)) \ | |
6240 | && (segment->p_type == PT_LOAD \ | |
6241 | || segment->p_type == PT_TLS \ | |
6242 | || (section->flags & SEC_THREAD_LOCAL) == 0) \ | |
6243 | && (segment->p_type != PT_DYNAMIC \ | |
6244 | || SECTION_SIZE (section, segment) > 0 \ | |
6245 | || (segment->p_paddr \ | |
6246 | ? segment->p_paddr != section->lma \ | |
6247 | : segment->p_vaddr != section->vma) \ | |
6248 | || (strcmp (bfd_get_section_name (ibfd, section), ".dynamic") \ | |
6249 | == 0)) \ | |
0067a569 | 6250 | && !section->segment_mark) |
bc67d8a6 | 6251 | |
9f17e2a6 L |
6252 | /* If the output section of a section in the input segment is NULL, |
6253 | it is removed from the corresponding output segment. */ | |
6254 | #define INCLUDE_SECTION_IN_SEGMENT(section, segment, bed) \ | |
6255 | (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed) \ | |
6256 | && section->output_section != NULL) | |
6257 | ||
b34976b6 | 6258 | /* Returns TRUE iff seg1 starts after the end of seg2. */ |
b5f852ea NC |
6259 | #define SEGMENT_AFTER_SEGMENT(seg1, seg2, field) \ |
6260 | (seg1->field >= SEGMENT_END (seg2, seg2->field)) | |
6261 | ||
6262 | /* Returns TRUE iff seg1 and seg2 overlap. Segments overlap iff both | |
6263 | their VMA address ranges and their LMA address ranges overlap. | |
6264 | It is possible to have overlapping VMA ranges without overlapping LMA | |
6265 | ranges. RedBoot images for example can have both .data and .bss mapped | |
6266 | to the same VMA range, but with the .data section mapped to a different | |
6267 | LMA. */ | |
aecc8f8a | 6268 | #define SEGMENT_OVERLAPS(seg1, seg2) \ |
b5f852ea | 6269 | ( !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_vaddr) \ |
08a40648 | 6270 | || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_vaddr)) \ |
b5f852ea | 6271 | && !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_paddr) \ |
08a40648 | 6272 | || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_paddr))) |
bc67d8a6 NC |
6273 | |
6274 | /* Initialise the segment mark field. */ | |
6275 | for (section = ibfd->sections; section != NULL; section = section->next) | |
b34976b6 | 6276 | section->segment_mark = FALSE; |
bc67d8a6 | 6277 | |
5c44b38e AM |
6278 | /* The Solaris linker creates program headers in which all the |
6279 | p_paddr fields are zero. When we try to objcopy or strip such a | |
6280 | file, we get confused. Check for this case, and if we find it | |
6281 | don't set the p_paddr_valid fields. */ | |
6282 | p_paddr_valid = FALSE; | |
6283 | for (i = 0, segment = elf_tdata (ibfd)->phdr; | |
6284 | i < num_segments; | |
6285 | i++, segment++) | |
6286 | if (segment->p_paddr != 0) | |
6287 | { | |
6288 | p_paddr_valid = TRUE; | |
6289 | break; | |
6290 | } | |
6291 | ||
252b5132 | 6292 | /* Scan through the segments specified in the program header |
bc67d8a6 | 6293 | of the input BFD. For this first scan we look for overlaps |
9ad5cbcf | 6294 | in the loadable segments. These can be created by weird |
aecc8f8a | 6295 | parameters to objcopy. Also, fix some solaris weirdness. */ |
bc67d8a6 NC |
6296 | for (i = 0, segment = elf_tdata (ibfd)->phdr; |
6297 | i < num_segments; | |
c044fabd | 6298 | i++, segment++) |
252b5132 | 6299 | { |
252b5132 | 6300 | unsigned int j; |
c044fabd | 6301 | Elf_Internal_Phdr *segment2; |
252b5132 | 6302 | |
aecc8f8a AM |
6303 | if (segment->p_type == PT_INTERP) |
6304 | for (section = ibfd->sections; section; section = section->next) | |
6305 | if (IS_SOLARIS_PT_INTERP (segment, section)) | |
6306 | { | |
6307 | /* Mininal change so that the normal section to segment | |
4cc11e76 | 6308 | assignment code will work. */ |
aecc8f8a AM |
6309 | segment->p_vaddr = section->vma; |
6310 | break; | |
6311 | } | |
6312 | ||
bc67d8a6 | 6313 | if (segment->p_type != PT_LOAD) |
b10a8ae0 L |
6314 | { |
6315 | /* Remove PT_GNU_RELRO segment. */ | |
6316 | if (segment->p_type == PT_GNU_RELRO) | |
6317 | segment->p_type = PT_NULL; | |
6318 | continue; | |
6319 | } | |
c044fabd | 6320 | |
bc67d8a6 | 6321 | /* Determine if this segment overlaps any previous segments. */ |
0067a569 | 6322 | for (j = 0, segment2 = elf_tdata (ibfd)->phdr; j < i; j++, segment2++) |
bc67d8a6 NC |
6323 | { |
6324 | bfd_signed_vma extra_length; | |
c044fabd | 6325 | |
bc67d8a6 | 6326 | if (segment2->p_type != PT_LOAD |
0067a569 | 6327 | || !SEGMENT_OVERLAPS (segment, segment2)) |
bc67d8a6 | 6328 | continue; |
c044fabd | 6329 | |
bc67d8a6 NC |
6330 | /* Merge the two segments together. */ |
6331 | if (segment2->p_vaddr < segment->p_vaddr) | |
6332 | { | |
c044fabd | 6333 | /* Extend SEGMENT2 to include SEGMENT and then delete |
08a40648 | 6334 | SEGMENT. */ |
0067a569 AM |
6335 | extra_length = (SEGMENT_END (segment, segment->p_vaddr) |
6336 | - SEGMENT_END (segment2, segment2->p_vaddr)); | |
c044fabd | 6337 | |
bc67d8a6 NC |
6338 | if (extra_length > 0) |
6339 | { | |
0067a569 | 6340 | segment2->p_memsz += extra_length; |
bc67d8a6 NC |
6341 | segment2->p_filesz += extra_length; |
6342 | } | |
c044fabd | 6343 | |
bc67d8a6 | 6344 | segment->p_type = PT_NULL; |
c044fabd | 6345 | |
bc67d8a6 NC |
6346 | /* Since we have deleted P we must restart the outer loop. */ |
6347 | i = 0; | |
6348 | segment = elf_tdata (ibfd)->phdr; | |
6349 | break; | |
6350 | } | |
6351 | else | |
6352 | { | |
c044fabd | 6353 | /* Extend SEGMENT to include SEGMENT2 and then delete |
08a40648 | 6354 | SEGMENT2. */ |
0067a569 AM |
6355 | extra_length = (SEGMENT_END (segment2, segment2->p_vaddr) |
6356 | - SEGMENT_END (segment, segment->p_vaddr)); | |
c044fabd | 6357 | |
bc67d8a6 NC |
6358 | if (extra_length > 0) |
6359 | { | |
0067a569 | 6360 | segment->p_memsz += extra_length; |
bc67d8a6 NC |
6361 | segment->p_filesz += extra_length; |
6362 | } | |
c044fabd | 6363 | |
bc67d8a6 NC |
6364 | segment2->p_type = PT_NULL; |
6365 | } | |
6366 | } | |
6367 | } | |
c044fabd | 6368 | |
bc67d8a6 NC |
6369 | /* The second scan attempts to assign sections to segments. */ |
6370 | for (i = 0, segment = elf_tdata (ibfd)->phdr; | |
6371 | i < num_segments; | |
0067a569 | 6372 | i++, segment++) |
bc67d8a6 | 6373 | { |
0067a569 AM |
6374 | unsigned int section_count; |
6375 | asection **sections; | |
6376 | asection *output_section; | |
6377 | unsigned int isec; | |
6378 | bfd_vma matching_lma; | |
6379 | bfd_vma suggested_lma; | |
6380 | unsigned int j; | |
dc810e39 | 6381 | bfd_size_type amt; |
0067a569 AM |
6382 | asection *first_section; |
6383 | bfd_boolean first_matching_lma; | |
6384 | bfd_boolean first_suggested_lma; | |
bc67d8a6 NC |
6385 | |
6386 | if (segment->p_type == PT_NULL) | |
6387 | continue; | |
c044fabd | 6388 | |
9f17e2a6 | 6389 | first_section = NULL; |
bc67d8a6 | 6390 | /* Compute how many sections might be placed into this segment. */ |
b5f852ea NC |
6391 | for (section = ibfd->sections, section_count = 0; |
6392 | section != NULL; | |
6393 | section = section->next) | |
9f17e2a6 L |
6394 | { |
6395 | /* Find the first section in the input segment, which may be | |
6396 | removed from the corresponding output segment. */ | |
6397 | if (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed)) | |
6398 | { | |
6399 | if (first_section == NULL) | |
6400 | first_section = section; | |
6401 | if (section->output_section != NULL) | |
6402 | ++section_count; | |
6403 | } | |
6404 | } | |
811072d8 | 6405 | |
b5f852ea NC |
6406 | /* Allocate a segment map big enough to contain |
6407 | all of the sections we have selected. */ | |
dc810e39 AM |
6408 | amt = sizeof (struct elf_segment_map); |
6409 | amt += ((bfd_size_type) section_count - 1) * sizeof (asection *); | |
a50b1753 | 6410 | map = (struct elf_segment_map *) bfd_zalloc (obfd, amt); |
bc67d8a6 | 6411 | if (map == NULL) |
b34976b6 | 6412 | return FALSE; |
252b5132 RH |
6413 | |
6414 | /* Initialise the fields of the segment map. Default to | |
6415 | using the physical address of the segment in the input BFD. */ | |
0067a569 AM |
6416 | map->next = NULL; |
6417 | map->p_type = segment->p_type; | |
6418 | map->p_flags = segment->p_flags; | |
bc67d8a6 | 6419 | map->p_flags_valid = 1; |
55d55ac7 | 6420 | |
9f17e2a6 L |
6421 | /* If the first section in the input segment is removed, there is |
6422 | no need to preserve segment physical address in the corresponding | |
6423 | output segment. */ | |
945c025a | 6424 | if (!first_section || first_section->output_section != NULL) |
9f17e2a6 L |
6425 | { |
6426 | map->p_paddr = segment->p_paddr; | |
5c44b38e | 6427 | map->p_paddr_valid = p_paddr_valid; |
9f17e2a6 | 6428 | } |
252b5132 RH |
6429 | |
6430 | /* Determine if this segment contains the ELF file header | |
6431 | and if it contains the program headers themselves. */ | |
bc67d8a6 NC |
6432 | map->includes_filehdr = (segment->p_offset == 0 |
6433 | && segment->p_filesz >= iehdr->e_ehsize); | |
bc67d8a6 | 6434 | map->includes_phdrs = 0; |
252b5132 | 6435 | |
0067a569 | 6436 | if (!phdr_included || segment->p_type != PT_LOAD) |
252b5132 | 6437 | { |
bc67d8a6 NC |
6438 | map->includes_phdrs = |
6439 | (segment->p_offset <= (bfd_vma) iehdr->e_phoff | |
6440 | && (segment->p_offset + segment->p_filesz | |
252b5132 RH |
6441 | >= ((bfd_vma) iehdr->e_phoff |
6442 | + iehdr->e_phnum * iehdr->e_phentsize))); | |
c044fabd | 6443 | |
bc67d8a6 | 6444 | if (segment->p_type == PT_LOAD && map->includes_phdrs) |
b34976b6 | 6445 | phdr_included = TRUE; |
252b5132 RH |
6446 | } |
6447 | ||
bc67d8a6 | 6448 | if (section_count == 0) |
252b5132 RH |
6449 | { |
6450 | /* Special segments, such as the PT_PHDR segment, may contain | |
6451 | no sections, but ordinary, loadable segments should contain | |
1ed89aa9 NC |
6452 | something. They are allowed by the ELF spec however, so only |
6453 | a warning is produced. */ | |
bc67d8a6 | 6454 | if (segment->p_type == PT_LOAD) |
c86934ce NC |
6455 | (*_bfd_error_handler) (_("\ |
6456 | %B: warning: Empty loadable segment detected, is this intentional ?"), | |
0067a569 | 6457 | ibfd); |
252b5132 | 6458 | |
bc67d8a6 | 6459 | map->count = 0; |
c044fabd KH |
6460 | *pointer_to_map = map; |
6461 | pointer_to_map = &map->next; | |
252b5132 RH |
6462 | |
6463 | continue; | |
6464 | } | |
6465 | ||
6466 | /* Now scan the sections in the input BFD again and attempt | |
6467 | to add their corresponding output sections to the segment map. | |
6468 | The problem here is how to handle an output section which has | |
6469 | been moved (ie had its LMA changed). There are four possibilities: | |
6470 | ||
6471 | 1. None of the sections have been moved. | |
6472 | In this case we can continue to use the segment LMA from the | |
6473 | input BFD. | |
6474 | ||
6475 | 2. All of the sections have been moved by the same amount. | |
6476 | In this case we can change the segment's LMA to match the LMA | |
6477 | of the first section. | |
6478 | ||
6479 | 3. Some of the sections have been moved, others have not. | |
6480 | In this case those sections which have not been moved can be | |
6481 | placed in the current segment which will have to have its size, | |
6482 | and possibly its LMA changed, and a new segment or segments will | |
6483 | have to be created to contain the other sections. | |
6484 | ||
b5f852ea | 6485 | 4. The sections have been moved, but not by the same amount. |
252b5132 RH |
6486 | In this case we can change the segment's LMA to match the LMA |
6487 | of the first section and we will have to create a new segment | |
6488 | or segments to contain the other sections. | |
6489 | ||
6490 | In order to save time, we allocate an array to hold the section | |
6491 | pointers that we are interested in. As these sections get assigned | |
6492 | to a segment, they are removed from this array. */ | |
6493 | ||
a50b1753 | 6494 | sections = (asection **) bfd_malloc2 (section_count, sizeof (asection *)); |
252b5132 | 6495 | if (sections == NULL) |
b34976b6 | 6496 | return FALSE; |
252b5132 RH |
6497 | |
6498 | /* Step One: Scan for segment vs section LMA conflicts. | |
6499 | Also add the sections to the section array allocated above. | |
6500 | Also add the sections to the current segment. In the common | |
6501 | case, where the sections have not been moved, this means that | |
6502 | we have completely filled the segment, and there is nothing | |
6503 | more to do. */ | |
252b5132 | 6504 | isec = 0; |
72730e0c | 6505 | matching_lma = 0; |
252b5132 | 6506 | suggested_lma = 0; |
0067a569 AM |
6507 | first_matching_lma = TRUE; |
6508 | first_suggested_lma = TRUE; | |
252b5132 | 6509 | |
461c4b2e | 6510 | for (section = first_section, j = 0; |
bc67d8a6 NC |
6511 | section != NULL; |
6512 | section = section->next) | |
252b5132 | 6513 | { |
caf47ea6 | 6514 | if (INCLUDE_SECTION_IN_SEGMENT (section, segment, bed)) |
c0f7859b | 6515 | { |
bc67d8a6 NC |
6516 | output_section = section->output_section; |
6517 | ||
0067a569 | 6518 | sections[j++] = section; |
252b5132 RH |
6519 | |
6520 | /* The Solaris native linker always sets p_paddr to 0. | |
6521 | We try to catch that case here, and set it to the | |
5e8d7549 NC |
6522 | correct value. Note - some backends require that |
6523 | p_paddr be left as zero. */ | |
5c44b38e | 6524 | if (!p_paddr_valid |
4455705d | 6525 | && segment->p_vaddr != 0 |
0067a569 | 6526 | && !bed->want_p_paddr_set_to_zero |
252b5132 | 6527 | && isec == 0 |
bc67d8a6 | 6528 | && output_section->lma != 0 |
0067a569 AM |
6529 | && output_section->vma == (segment->p_vaddr |
6530 | + (map->includes_filehdr | |
6531 | ? iehdr->e_ehsize | |
6532 | : 0) | |
6533 | + (map->includes_phdrs | |
6534 | ? (iehdr->e_phnum | |
6535 | * iehdr->e_phentsize) | |
6536 | : 0))) | |
bc67d8a6 | 6537 | map->p_paddr = segment->p_vaddr; |
252b5132 RH |
6538 | |
6539 | /* Match up the physical address of the segment with the | |
6540 | LMA address of the output section. */ | |
bc67d8a6 | 6541 | if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr) |
5e8d7549 | 6542 | || IS_COREFILE_NOTE (segment, section) |
0067a569 AM |
6543 | || (bed->want_p_paddr_set_to_zero |
6544 | && IS_CONTAINED_BY_VMA (output_section, segment))) | |
252b5132 | 6545 | { |
0067a569 AM |
6546 | if (first_matching_lma || output_section->lma < matching_lma) |
6547 | { | |
6548 | matching_lma = output_section->lma; | |
6549 | first_matching_lma = FALSE; | |
6550 | } | |
252b5132 RH |
6551 | |
6552 | /* We assume that if the section fits within the segment | |
bc67d8a6 | 6553 | then it does not overlap any other section within that |
252b5132 | 6554 | segment. */ |
0067a569 AM |
6555 | map->sections[isec++] = output_section; |
6556 | } | |
6557 | else if (first_suggested_lma) | |
6558 | { | |
6559 | suggested_lma = output_section->lma; | |
6560 | first_suggested_lma = FALSE; | |
252b5132 | 6561 | } |
147d51c2 L |
6562 | |
6563 | if (j == section_count) | |
6564 | break; | |
252b5132 RH |
6565 | } |
6566 | } | |
6567 | ||
bc67d8a6 | 6568 | BFD_ASSERT (j == section_count); |
252b5132 RH |
6569 | |
6570 | /* Step Two: Adjust the physical address of the current segment, | |
6571 | if necessary. */ | |
bc67d8a6 | 6572 | if (isec == section_count) |
252b5132 RH |
6573 | { |
6574 | /* All of the sections fitted within the segment as currently | |
6575 | specified. This is the default case. Add the segment to | |
6576 | the list of built segments and carry on to process the next | |
6577 | program header in the input BFD. */ | |
bc67d8a6 | 6578 | map->count = section_count; |
c044fabd KH |
6579 | *pointer_to_map = map; |
6580 | pointer_to_map = &map->next; | |
08a40648 | 6581 | |
5c44b38e AM |
6582 | if (p_paddr_valid |
6583 | && !bed->want_p_paddr_set_to_zero | |
147d51c2 | 6584 | && matching_lma != map->p_paddr |
5c44b38e AM |
6585 | && !map->includes_filehdr |
6586 | && !map->includes_phdrs) | |
3271a814 NS |
6587 | /* There is some padding before the first section in the |
6588 | segment. So, we must account for that in the output | |
6589 | segment's vma. */ | |
6590 | map->p_vaddr_offset = matching_lma - map->p_paddr; | |
08a40648 | 6591 | |
252b5132 RH |
6592 | free (sections); |
6593 | continue; | |
6594 | } | |
252b5132 RH |
6595 | else |
6596 | { | |
0067a569 | 6597 | if (!first_matching_lma) |
72730e0c AM |
6598 | { |
6599 | /* At least one section fits inside the current segment. | |
6600 | Keep it, but modify its physical address to match the | |
6601 | LMA of the first section that fitted. */ | |
bc67d8a6 | 6602 | map->p_paddr = matching_lma; |
72730e0c AM |
6603 | } |
6604 | else | |
6605 | { | |
6606 | /* None of the sections fitted inside the current segment. | |
6607 | Change the current segment's physical address to match | |
6608 | the LMA of the first section. */ | |
bc67d8a6 | 6609 | map->p_paddr = suggested_lma; |
72730e0c AM |
6610 | } |
6611 | ||
bc67d8a6 NC |
6612 | /* Offset the segment physical address from the lma |
6613 | to allow for space taken up by elf headers. */ | |
6614 | if (map->includes_filehdr) | |
010c8431 AM |
6615 | { |
6616 | if (map->p_paddr >= iehdr->e_ehsize) | |
6617 | map->p_paddr -= iehdr->e_ehsize; | |
6618 | else | |
6619 | { | |
6620 | map->includes_filehdr = FALSE; | |
6621 | map->includes_phdrs = FALSE; | |
6622 | } | |
6623 | } | |
252b5132 | 6624 | |
bc67d8a6 NC |
6625 | if (map->includes_phdrs) |
6626 | { | |
010c8431 AM |
6627 | if (map->p_paddr >= iehdr->e_phnum * iehdr->e_phentsize) |
6628 | { | |
6629 | map->p_paddr -= iehdr->e_phnum * iehdr->e_phentsize; | |
6630 | ||
6631 | /* iehdr->e_phnum is just an estimate of the number | |
6632 | of program headers that we will need. Make a note | |
6633 | here of the number we used and the segment we chose | |
6634 | to hold these headers, so that we can adjust the | |
6635 | offset when we know the correct value. */ | |
6636 | phdr_adjust_num = iehdr->e_phnum; | |
6637 | phdr_adjust_seg = map; | |
6638 | } | |
6639 | else | |
6640 | map->includes_phdrs = FALSE; | |
bc67d8a6 | 6641 | } |
252b5132 RH |
6642 | } |
6643 | ||
6644 | /* Step Three: Loop over the sections again, this time assigning | |
caf47ea6 | 6645 | those that fit to the current segment and removing them from the |
252b5132 RH |
6646 | sections array; but making sure not to leave large gaps. Once all |
6647 | possible sections have been assigned to the current segment it is | |
6648 | added to the list of built segments and if sections still remain | |
6649 | to be assigned, a new segment is constructed before repeating | |
6650 | the loop. */ | |
6651 | isec = 0; | |
6652 | do | |
6653 | { | |
bc67d8a6 | 6654 | map->count = 0; |
252b5132 | 6655 | suggested_lma = 0; |
0067a569 | 6656 | first_suggested_lma = TRUE; |
252b5132 RH |
6657 | |
6658 | /* Fill the current segment with sections that fit. */ | |
bc67d8a6 | 6659 | for (j = 0; j < section_count; j++) |
252b5132 | 6660 | { |
bc67d8a6 | 6661 | section = sections[j]; |
252b5132 | 6662 | |
bc67d8a6 | 6663 | if (section == NULL) |
252b5132 RH |
6664 | continue; |
6665 | ||
bc67d8a6 | 6666 | output_section = section->output_section; |
252b5132 | 6667 | |
bc67d8a6 | 6668 | BFD_ASSERT (output_section != NULL); |
c044fabd | 6669 | |
bc67d8a6 NC |
6670 | if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr) |
6671 | || IS_COREFILE_NOTE (segment, section)) | |
252b5132 | 6672 | { |
bc67d8a6 | 6673 | if (map->count == 0) |
252b5132 RH |
6674 | { |
6675 | /* If the first section in a segment does not start at | |
bc67d8a6 NC |
6676 | the beginning of the segment, then something is |
6677 | wrong. */ | |
0067a569 AM |
6678 | if (output_section->lma |
6679 | != (map->p_paddr | |
6680 | + (map->includes_filehdr ? iehdr->e_ehsize : 0) | |
6681 | + (map->includes_phdrs | |
6682 | ? iehdr->e_phnum * iehdr->e_phentsize | |
6683 | : 0))) | |
252b5132 RH |
6684 | abort (); |
6685 | } | |
6686 | else | |
6687 | { | |
0067a569 | 6688 | asection *prev_sec; |
252b5132 | 6689 | |
bc67d8a6 | 6690 | prev_sec = map->sections[map->count - 1]; |
252b5132 RH |
6691 | |
6692 | /* If the gap between the end of the previous section | |
bc67d8a6 NC |
6693 | and the start of this section is more than |
6694 | maxpagesize then we need to start a new segment. */ | |
eea6121a | 6695 | if ((BFD_ALIGN (prev_sec->lma + prev_sec->size, |
079e9a2f | 6696 | maxpagesize) |
caf47ea6 | 6697 | < BFD_ALIGN (output_section->lma, maxpagesize)) |
0067a569 | 6698 | || (prev_sec->lma + prev_sec->size |
079e9a2f | 6699 | > output_section->lma)) |
252b5132 | 6700 | { |
0067a569 AM |
6701 | if (first_suggested_lma) |
6702 | { | |
6703 | suggested_lma = output_section->lma; | |
6704 | first_suggested_lma = FALSE; | |
6705 | } | |
252b5132 RH |
6706 | |
6707 | continue; | |
6708 | } | |
6709 | } | |
6710 | ||
bc67d8a6 | 6711 | map->sections[map->count++] = output_section; |
252b5132 RH |
6712 | ++isec; |
6713 | sections[j] = NULL; | |
b34976b6 | 6714 | section->segment_mark = TRUE; |
252b5132 | 6715 | } |
0067a569 AM |
6716 | else if (first_suggested_lma) |
6717 | { | |
6718 | suggested_lma = output_section->lma; | |
6719 | first_suggested_lma = FALSE; | |
6720 | } | |
252b5132 RH |
6721 | } |
6722 | ||
bc67d8a6 | 6723 | BFD_ASSERT (map->count > 0); |
252b5132 RH |
6724 | |
6725 | /* Add the current segment to the list of built segments. */ | |
c044fabd KH |
6726 | *pointer_to_map = map; |
6727 | pointer_to_map = &map->next; | |
252b5132 | 6728 | |
bc67d8a6 | 6729 | if (isec < section_count) |
252b5132 RH |
6730 | { |
6731 | /* We still have not allocated all of the sections to | |
6732 | segments. Create a new segment here, initialise it | |
6733 | and carry on looping. */ | |
dc810e39 AM |
6734 | amt = sizeof (struct elf_segment_map); |
6735 | amt += ((bfd_size_type) section_count - 1) * sizeof (asection *); | |
5964fc3a | 6736 | map = (struct elf_segment_map *) bfd_zalloc (obfd, amt); |
bc67d8a6 | 6737 | if (map == NULL) |
5ed6aba4 NC |
6738 | { |
6739 | free (sections); | |
6740 | return FALSE; | |
6741 | } | |
252b5132 RH |
6742 | |
6743 | /* Initialise the fields of the segment map. Set the physical | |
6744 | physical address to the LMA of the first section that has | |
6745 | not yet been assigned. */ | |
0067a569 AM |
6746 | map->next = NULL; |
6747 | map->p_type = segment->p_type; | |
6748 | map->p_flags = segment->p_flags; | |
6749 | map->p_flags_valid = 1; | |
6750 | map->p_paddr = suggested_lma; | |
5c44b38e | 6751 | map->p_paddr_valid = p_paddr_valid; |
bc67d8a6 | 6752 | map->includes_filehdr = 0; |
0067a569 | 6753 | map->includes_phdrs = 0; |
252b5132 RH |
6754 | } |
6755 | } | |
bc67d8a6 | 6756 | while (isec < section_count); |
252b5132 RH |
6757 | |
6758 | free (sections); | |
6759 | } | |
6760 | ||
12bd6957 | 6761 | elf_seg_map (obfd) = map_first; |
bc67d8a6 NC |
6762 | |
6763 | /* If we had to estimate the number of program headers that were | |
9ad5cbcf | 6764 | going to be needed, then check our estimate now and adjust |
bc67d8a6 NC |
6765 | the offset if necessary. */ |
6766 | if (phdr_adjust_seg != NULL) | |
6767 | { | |
6768 | unsigned int count; | |
c044fabd | 6769 | |
bc67d8a6 | 6770 | for (count = 0, map = map_first; map != NULL; map = map->next) |
c044fabd | 6771 | count++; |
252b5132 | 6772 | |
bc67d8a6 NC |
6773 | if (count > phdr_adjust_num) |
6774 | phdr_adjust_seg->p_paddr | |
6775 | -= (count - phdr_adjust_num) * iehdr->e_phentsize; | |
6776 | } | |
c044fabd | 6777 | |
bc67d8a6 | 6778 | #undef SEGMENT_END |
eecdbe52 | 6779 | #undef SECTION_SIZE |
bc67d8a6 NC |
6780 | #undef IS_CONTAINED_BY_VMA |
6781 | #undef IS_CONTAINED_BY_LMA | |
0efc80c8 | 6782 | #undef IS_NOTE |
252b5132 | 6783 | #undef IS_COREFILE_NOTE |
bc67d8a6 | 6784 | #undef IS_SOLARIS_PT_INTERP |
9f17e2a6 | 6785 | #undef IS_SECTION_IN_INPUT_SEGMENT |
bc67d8a6 NC |
6786 | #undef INCLUDE_SECTION_IN_SEGMENT |
6787 | #undef SEGMENT_AFTER_SEGMENT | |
6788 | #undef SEGMENT_OVERLAPS | |
b34976b6 | 6789 | return TRUE; |
252b5132 RH |
6790 | } |
6791 | ||
84d1d650 L |
6792 | /* Copy ELF program header information. */ |
6793 | ||
6794 | static bfd_boolean | |
6795 | copy_elf_program_header (bfd *ibfd, bfd *obfd) | |
6796 | { | |
6797 | Elf_Internal_Ehdr *iehdr; | |
6798 | struct elf_segment_map *map; | |
6799 | struct elf_segment_map *map_first; | |
6800 | struct elf_segment_map **pointer_to_map; | |
6801 | Elf_Internal_Phdr *segment; | |
6802 | unsigned int i; | |
6803 | unsigned int num_segments; | |
6804 | bfd_boolean phdr_included = FALSE; | |
88967714 | 6805 | bfd_boolean p_paddr_valid; |
84d1d650 L |
6806 | |
6807 | iehdr = elf_elfheader (ibfd); | |
6808 | ||
6809 | map_first = NULL; | |
6810 | pointer_to_map = &map_first; | |
6811 | ||
88967714 AM |
6812 | /* If all the segment p_paddr fields are zero, don't set |
6813 | map->p_paddr_valid. */ | |
6814 | p_paddr_valid = FALSE; | |
84d1d650 | 6815 | num_segments = elf_elfheader (ibfd)->e_phnum; |
88967714 AM |
6816 | for (i = 0, segment = elf_tdata (ibfd)->phdr; |
6817 | i < num_segments; | |
6818 | i++, segment++) | |
6819 | if (segment->p_paddr != 0) | |
6820 | { | |
6821 | p_paddr_valid = TRUE; | |
6822 | break; | |
6823 | } | |
6824 | ||
84d1d650 L |
6825 | for (i = 0, segment = elf_tdata (ibfd)->phdr; |
6826 | i < num_segments; | |
6827 | i++, segment++) | |
6828 | { | |
6829 | asection *section; | |
6830 | unsigned int section_count; | |
6831 | bfd_size_type amt; | |
6832 | Elf_Internal_Shdr *this_hdr; | |
53020534 | 6833 | asection *first_section = NULL; |
a76e6f2f | 6834 | asection *lowest_section; |
84d1d650 | 6835 | |
84d1d650 L |
6836 | /* Compute how many sections are in this segment. */ |
6837 | for (section = ibfd->sections, section_count = 0; | |
6838 | section != NULL; | |
6839 | section = section->next) | |
6840 | { | |
6841 | this_hdr = &(elf_section_data(section)->this_hdr); | |
f4638467 | 6842 | if (ELF_SECTION_IN_SEGMENT (this_hdr, segment)) |
3271a814 | 6843 | { |
a76e6f2f AM |
6844 | if (first_section == NULL) |
6845 | first_section = section; | |
3271a814 NS |
6846 | section_count++; |
6847 | } | |
84d1d650 L |
6848 | } |
6849 | ||
6850 | /* Allocate a segment map big enough to contain | |
6851 | all of the sections we have selected. */ | |
6852 | amt = sizeof (struct elf_segment_map); | |
6853 | if (section_count != 0) | |
6854 | amt += ((bfd_size_type) section_count - 1) * sizeof (asection *); | |
a50b1753 | 6855 | map = (struct elf_segment_map *) bfd_zalloc (obfd, amt); |
84d1d650 L |
6856 | if (map == NULL) |
6857 | return FALSE; | |
6858 | ||
6859 | /* Initialize the fields of the output segment map with the | |
6860 | input segment. */ | |
6861 | map->next = NULL; | |
6862 | map->p_type = segment->p_type; | |
6863 | map->p_flags = segment->p_flags; | |
6864 | map->p_flags_valid = 1; | |
6865 | map->p_paddr = segment->p_paddr; | |
88967714 | 6866 | map->p_paddr_valid = p_paddr_valid; |
3f570048 AM |
6867 | map->p_align = segment->p_align; |
6868 | map->p_align_valid = 1; | |
3271a814 | 6869 | map->p_vaddr_offset = 0; |
84d1d650 | 6870 | |
04c3a755 NS |
6871 | if (map->p_type == PT_GNU_RELRO |
6872 | || map->p_type == PT_GNU_STACK) | |
b10a8ae0 L |
6873 | { |
6874 | /* The PT_GNU_RELRO segment may contain the first a few | |
6875 | bytes in the .got.plt section even if the whole .got.plt | |
6876 | section isn't in the PT_GNU_RELRO segment. We won't | |
04c3a755 NS |
6877 | change the size of the PT_GNU_RELRO segment. |
6878 | Similarly, PT_GNU_STACK size is significant on uclinux | |
6879 | systems. */ | |
9433b9b1 | 6880 | map->p_size = segment->p_memsz; |
b10a8ae0 L |
6881 | map->p_size_valid = 1; |
6882 | } | |
6883 | ||
84d1d650 L |
6884 | /* Determine if this segment contains the ELF file header |
6885 | and if it contains the program headers themselves. */ | |
6886 | map->includes_filehdr = (segment->p_offset == 0 | |
6887 | && segment->p_filesz >= iehdr->e_ehsize); | |
6888 | ||
6889 | map->includes_phdrs = 0; | |
6890 | if (! phdr_included || segment->p_type != PT_LOAD) | |
6891 | { | |
6892 | map->includes_phdrs = | |
6893 | (segment->p_offset <= (bfd_vma) iehdr->e_phoff | |
6894 | && (segment->p_offset + segment->p_filesz | |
6895 | >= ((bfd_vma) iehdr->e_phoff | |
6896 | + iehdr->e_phnum * iehdr->e_phentsize))); | |
6897 | ||
6898 | if (segment->p_type == PT_LOAD && map->includes_phdrs) | |
6899 | phdr_included = TRUE; | |
6900 | } | |
6901 | ||
bbefd0a9 | 6902 | lowest_section = NULL; |
84d1d650 L |
6903 | if (section_count != 0) |
6904 | { | |
6905 | unsigned int isec = 0; | |
6906 | ||
53020534 | 6907 | for (section = first_section; |
84d1d650 L |
6908 | section != NULL; |
6909 | section = section->next) | |
6910 | { | |
6911 | this_hdr = &(elf_section_data(section)->this_hdr); | |
f4638467 | 6912 | if (ELF_SECTION_IN_SEGMENT (this_hdr, segment)) |
53020534 L |
6913 | { |
6914 | map->sections[isec++] = section->output_section; | |
a76e6f2f AM |
6915 | if ((section->flags & SEC_ALLOC) != 0) |
6916 | { | |
6917 | bfd_vma seg_off; | |
6918 | ||
bbefd0a9 AM |
6919 | if (lowest_section == NULL |
6920 | || section->lma < lowest_section->lma) | |
fb8a5684 AM |
6921 | lowest_section = section; |
6922 | ||
a76e6f2f AM |
6923 | /* Section lmas are set up from PT_LOAD header |
6924 | p_paddr in _bfd_elf_make_section_from_shdr. | |
6925 | If this header has a p_paddr that disagrees | |
6926 | with the section lma, flag the p_paddr as | |
6927 | invalid. */ | |
6928 | if ((section->flags & SEC_LOAD) != 0) | |
6929 | seg_off = this_hdr->sh_offset - segment->p_offset; | |
6930 | else | |
6931 | seg_off = this_hdr->sh_addr - segment->p_vaddr; | |
6932 | if (section->lma - segment->p_paddr != seg_off) | |
6933 | map->p_paddr_valid = FALSE; | |
6934 | } | |
53020534 L |
6935 | if (isec == section_count) |
6936 | break; | |
6937 | } | |
84d1d650 L |
6938 | } |
6939 | } | |
6940 | ||
a76e6f2f AM |
6941 | if (map->includes_filehdr && lowest_section != NULL) |
6942 | /* We need to keep the space used by the headers fixed. */ | |
6943 | map->header_size = lowest_section->vma - segment->p_vaddr; | |
d324f6d6 | 6944 | |
a76e6f2f AM |
6945 | if (!map->includes_phdrs |
6946 | && !map->includes_filehdr | |
6947 | && map->p_paddr_valid) | |
6948 | /* There is some other padding before the first section. */ | |
6949 | map->p_vaddr_offset = ((lowest_section ? lowest_section->lma : 0) | |
6950 | - segment->p_paddr); | |
6951 | ||
84d1d650 L |
6952 | map->count = section_count; |
6953 | *pointer_to_map = map; | |
6954 | pointer_to_map = &map->next; | |
6955 | } | |
6956 | ||
12bd6957 | 6957 | elf_seg_map (obfd) = map_first; |
84d1d650 L |
6958 | return TRUE; |
6959 | } | |
6960 | ||
6961 | /* Copy private BFD data. This copies or rewrites ELF program header | |
6962 | information. */ | |
6963 | ||
6964 | static bfd_boolean | |
6965 | copy_private_bfd_data (bfd *ibfd, bfd *obfd) | |
6966 | { | |
84d1d650 L |
6967 | if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour |
6968 | || bfd_get_flavour (obfd) != bfd_target_elf_flavour) | |
6969 | return TRUE; | |
6970 | ||
6971 | if (elf_tdata (ibfd)->phdr == NULL) | |
6972 | return TRUE; | |
6973 | ||
6974 | if (ibfd->xvec == obfd->xvec) | |
6975 | { | |
cb3ff1e5 NC |
6976 | /* Check to see if any sections in the input BFD |
6977 | covered by ELF program header have changed. */ | |
d55ce4e2 | 6978 | Elf_Internal_Phdr *segment; |
84d1d650 L |
6979 | asection *section, *osec; |
6980 | unsigned int i, num_segments; | |
6981 | Elf_Internal_Shdr *this_hdr; | |
147d51c2 L |
6982 | const struct elf_backend_data *bed; |
6983 | ||
6984 | bed = get_elf_backend_data (ibfd); | |
6985 | ||
6986 | /* Regenerate the segment map if p_paddr is set to 0. */ | |
6987 | if (bed->want_p_paddr_set_to_zero) | |
6988 | goto rewrite; | |
84d1d650 L |
6989 | |
6990 | /* Initialize the segment mark field. */ | |
6991 | for (section = obfd->sections; section != NULL; | |
6992 | section = section->next) | |
6993 | section->segment_mark = FALSE; | |
6994 | ||
6995 | num_segments = elf_elfheader (ibfd)->e_phnum; | |
6996 | for (i = 0, segment = elf_tdata (ibfd)->phdr; | |
6997 | i < num_segments; | |
6998 | i++, segment++) | |
6999 | { | |
5f6999aa NC |
7000 | /* PR binutils/3535. The Solaris linker always sets the p_paddr |
7001 | and p_memsz fields of special segments (DYNAMIC, INTERP) to 0 | |
7002 | which severly confuses things, so always regenerate the segment | |
7003 | map in this case. */ | |
7004 | if (segment->p_paddr == 0 | |
7005 | && segment->p_memsz == 0 | |
7006 | && (segment->p_type == PT_INTERP || segment->p_type == PT_DYNAMIC)) | |
cb3ff1e5 | 7007 | goto rewrite; |
5f6999aa | 7008 | |
84d1d650 L |
7009 | for (section = ibfd->sections; |
7010 | section != NULL; section = section->next) | |
7011 | { | |
7012 | /* We mark the output section so that we know it comes | |
7013 | from the input BFD. */ | |
7014 | osec = section->output_section; | |
7015 | if (osec) | |
7016 | osec->segment_mark = TRUE; | |
7017 | ||
7018 | /* Check if this section is covered by the segment. */ | |
7019 | this_hdr = &(elf_section_data(section)->this_hdr); | |
f4638467 | 7020 | if (ELF_SECTION_IN_SEGMENT (this_hdr, segment)) |
84d1d650 L |
7021 | { |
7022 | /* FIXME: Check if its output section is changed or | |
7023 | removed. What else do we need to check? */ | |
7024 | if (osec == NULL | |
7025 | || section->flags != osec->flags | |
7026 | || section->lma != osec->lma | |
7027 | || section->vma != osec->vma | |
7028 | || section->size != osec->size | |
7029 | || section->rawsize != osec->rawsize | |
7030 | || section->alignment_power != osec->alignment_power) | |
7031 | goto rewrite; | |
7032 | } | |
7033 | } | |
7034 | } | |
7035 | ||
cb3ff1e5 | 7036 | /* Check to see if any output section do not come from the |
84d1d650 L |
7037 | input BFD. */ |
7038 | for (section = obfd->sections; section != NULL; | |
7039 | section = section->next) | |
7040 | { | |
7041 | if (section->segment_mark == FALSE) | |
7042 | goto rewrite; | |
7043 | else | |
7044 | section->segment_mark = FALSE; | |
7045 | } | |
7046 | ||
7047 | return copy_elf_program_header (ibfd, obfd); | |
7048 | } | |
7049 | ||
7050 | rewrite: | |
f1d85785 L |
7051 | if (ibfd->xvec == obfd->xvec) |
7052 | { | |
7053 | /* When rewriting program header, set the output maxpagesize to | |
7054 | the maximum alignment of input PT_LOAD segments. */ | |
7055 | Elf_Internal_Phdr *segment; | |
7056 | unsigned int i; | |
7057 | unsigned int num_segments = elf_elfheader (ibfd)->e_phnum; | |
7058 | bfd_vma maxpagesize = 0; | |
7059 | ||
7060 | for (i = 0, segment = elf_tdata (ibfd)->phdr; | |
7061 | i < num_segments; | |
7062 | i++, segment++) | |
7063 | if (segment->p_type == PT_LOAD | |
7064 | && maxpagesize < segment->p_align) | |
c86934ce NC |
7065 | { |
7066 | /* PR 17512: file: f17299af. */ | |
7067 | if (segment->p_align > (bfd_vma) 1 << ((sizeof (bfd_vma) * 8) - 2)) | |
7068 | (*_bfd_error_handler) (_("\ | |
7069 | %B: warning: segment alignment of 0x%llx is too large"), | |
7070 | ibfd, (long long) segment->p_align); | |
7071 | else | |
7072 | maxpagesize = segment->p_align; | |
7073 | } | |
f1d85785 L |
7074 | |
7075 | if (maxpagesize != get_elf_backend_data (obfd)->maxpagesize) | |
7076 | bfd_emul_set_maxpagesize (bfd_get_target (obfd), maxpagesize); | |
7077 | } | |
7078 | ||
84d1d650 L |
7079 | return rewrite_elf_program_header (ibfd, obfd); |
7080 | } | |
7081 | ||
ccd2ec6a L |
7082 | /* Initialize private output section information from input section. */ |
7083 | ||
7084 | bfd_boolean | |
7085 | _bfd_elf_init_private_section_data (bfd *ibfd, | |
7086 | asection *isec, | |
7087 | bfd *obfd, | |
7088 | asection *osec, | |
7089 | struct bfd_link_info *link_info) | |
7090 | ||
7091 | { | |
7092 | Elf_Internal_Shdr *ihdr, *ohdr; | |
0e1862bb L |
7093 | bfd_boolean final_link = (link_info != NULL |
7094 | && !bfd_link_relocatable (link_info)); | |
ccd2ec6a L |
7095 | |
7096 | if (ibfd->xvec->flavour != bfd_target_elf_flavour | |
7097 | || obfd->xvec->flavour != bfd_target_elf_flavour) | |
7098 | return TRUE; | |
7099 | ||
ba85c43e NC |
7100 | BFD_ASSERT (elf_section_data (osec) != NULL); |
7101 | ||
dfa7b0b8 AM |
7102 | /* For objcopy and relocatable link, don't copy the output ELF |
7103 | section type from input if the output BFD section flags have been | |
7104 | set to something different. For a final link allow some flags | |
7105 | that the linker clears to differ. */ | |
42bb2e33 | 7106 | if (elf_section_type (osec) == SHT_NULL |
dfa7b0b8 AM |
7107 | && (osec->flags == isec->flags |
7108 | || (final_link | |
7109 | && ((osec->flags ^ isec->flags) | |
0814be7d | 7110 | & ~(SEC_LINK_ONCE | SEC_LINK_DUPLICATES | SEC_RELOC)) == 0))) |
42bb2e33 | 7111 | elf_section_type (osec) = elf_section_type (isec); |
d270463e L |
7112 | |
7113 | /* FIXME: Is this correct for all OS/PROC specific flags? */ | |
7114 | elf_section_flags (osec) |= (elf_section_flags (isec) | |
7115 | & (SHF_MASKOS | SHF_MASKPROC)); | |
ccd2ec6a L |
7116 | |
7117 | /* Set things up for objcopy and relocatable link. The output | |
7118 | SHT_GROUP section will have its elf_next_in_group pointing back | |
7119 | to the input group members. Ignore linker created group section. | |
7120 | See elfNN_ia64_object_p in elfxx-ia64.c. */ | |
dfa7b0b8 | 7121 | if (!final_link) |
ccd2ec6a L |
7122 | { |
7123 | if (elf_sec_group (isec) == NULL | |
7124 | || (elf_sec_group (isec)->flags & SEC_LINKER_CREATED) == 0) | |
7125 | { | |
7126 | if (elf_section_flags (isec) & SHF_GROUP) | |
7127 | elf_section_flags (osec) |= SHF_GROUP; | |
7128 | elf_next_in_group (osec) = elf_next_in_group (isec); | |
9659de1c | 7129 | elf_section_data (osec)->group = elf_section_data (isec)->group; |
ccd2ec6a | 7130 | } |
68f5ff89 L |
7131 | |
7132 | /* If not decompress, preserve SHF_COMPRESSED. */ | |
7133 | if ((ibfd->flags & BFD_DECOMPRESS) == 0) | |
7134 | elf_section_flags (osec) |= (elf_section_flags (isec) | |
7135 | & SHF_COMPRESSED); | |
ccd2ec6a L |
7136 | } |
7137 | ||
7138 | ihdr = &elf_section_data (isec)->this_hdr; | |
7139 | ||
7140 | /* We need to handle elf_linked_to_section for SHF_LINK_ORDER. We | |
7141 | don't use the output section of the linked-to section since it | |
7142 | may be NULL at this point. */ | |
7143 | if ((ihdr->sh_flags & SHF_LINK_ORDER) != 0) | |
7144 | { | |
7145 | ohdr = &elf_section_data (osec)->this_hdr; | |
7146 | ohdr->sh_flags |= SHF_LINK_ORDER; | |
7147 | elf_linked_to_section (osec) = elf_linked_to_section (isec); | |
7148 | } | |
7149 | ||
7150 | osec->use_rela_p = isec->use_rela_p; | |
7151 | ||
7152 | return TRUE; | |
7153 | } | |
7154 | ||
252b5132 RH |
7155 | /* Copy private section information. This copies over the entsize |
7156 | field, and sometimes the info field. */ | |
7157 | ||
b34976b6 | 7158 | bfd_boolean |
217aa764 AM |
7159 | _bfd_elf_copy_private_section_data (bfd *ibfd, |
7160 | asection *isec, | |
7161 | bfd *obfd, | |
7162 | asection *osec) | |
252b5132 RH |
7163 | { |
7164 | Elf_Internal_Shdr *ihdr, *ohdr; | |
7165 | ||
7166 | if (ibfd->xvec->flavour != bfd_target_elf_flavour | |
7167 | || obfd->xvec->flavour != bfd_target_elf_flavour) | |
b34976b6 | 7168 | return TRUE; |
252b5132 | 7169 | |
252b5132 RH |
7170 | ihdr = &elf_section_data (isec)->this_hdr; |
7171 | ohdr = &elf_section_data (osec)->this_hdr; | |
7172 | ||
7173 | ohdr->sh_entsize = ihdr->sh_entsize; | |
7174 | ||
7175 | if (ihdr->sh_type == SHT_SYMTAB | |
7176 | || ihdr->sh_type == SHT_DYNSYM | |
7177 | || ihdr->sh_type == SHT_GNU_verneed | |
7178 | || ihdr->sh_type == SHT_GNU_verdef) | |
7179 | ohdr->sh_info = ihdr->sh_info; | |
7180 | ||
ccd2ec6a L |
7181 | return _bfd_elf_init_private_section_data (ibfd, isec, obfd, osec, |
7182 | NULL); | |
252b5132 RH |
7183 | } |
7184 | ||
d0bf826b AM |
7185 | /* Look at all the SHT_GROUP sections in IBFD, making any adjustments |
7186 | necessary if we are removing either the SHT_GROUP section or any of | |
7187 | the group member sections. DISCARDED is the value that a section's | |
7188 | output_section has if the section will be discarded, NULL when this | |
7189 | function is called from objcopy, bfd_abs_section_ptr when called | |
7190 | from the linker. */ | |
80fccad2 BW |
7191 | |
7192 | bfd_boolean | |
d0bf826b | 7193 | _bfd_elf_fixup_group_sections (bfd *ibfd, asection *discarded) |
80fccad2 | 7194 | { |
30288845 AM |
7195 | asection *isec; |
7196 | ||
30288845 | 7197 | for (isec = ibfd->sections; isec != NULL; isec = isec->next) |
415f38a6 | 7198 | if (elf_section_type (isec) == SHT_GROUP) |
30288845 AM |
7199 | { |
7200 | asection *first = elf_next_in_group (isec); | |
7201 | asection *s = first; | |
d0bf826b AM |
7202 | bfd_size_type removed = 0; |
7203 | ||
30288845 AM |
7204 | while (s != NULL) |
7205 | { | |
415f38a6 AM |
7206 | /* If this member section is being output but the |
7207 | SHT_GROUP section is not, then clear the group info | |
7208 | set up by _bfd_elf_copy_private_section_data. */ | |
d0bf826b AM |
7209 | if (s->output_section != discarded |
7210 | && isec->output_section == discarded) | |
30288845 AM |
7211 | { |
7212 | elf_section_flags (s->output_section) &= ~SHF_GROUP; | |
7213 | elf_group_name (s->output_section) = NULL; | |
7214 | } | |
415f38a6 AM |
7215 | /* Conversely, if the member section is not being output |
7216 | but the SHT_GROUP section is, then adjust its size. */ | |
d0bf826b AM |
7217 | else if (s->output_section == discarded |
7218 | && isec->output_section != discarded) | |
7219 | removed += 4; | |
30288845 AM |
7220 | s = elf_next_in_group (s); |
7221 | if (s == first) | |
7222 | break; | |
7223 | } | |
d0bf826b AM |
7224 | if (removed != 0) |
7225 | { | |
7226 | if (discarded != NULL) | |
7227 | { | |
7228 | /* If we've been called for ld -r, then we need to | |
7229 | adjust the input section size. This function may | |
7230 | be called multiple times, so save the original | |
7231 | size. */ | |
7232 | if (isec->rawsize == 0) | |
7233 | isec->rawsize = isec->size; | |
7234 | isec->size = isec->rawsize - removed; | |
7235 | } | |
7236 | else | |
7237 | { | |
7238 | /* Adjust the output section size when called from | |
7239 | objcopy. */ | |
7240 | isec->output_section->size -= removed; | |
7241 | } | |
7242 | } | |
30288845 AM |
7243 | } |
7244 | ||
80fccad2 BW |
7245 | return TRUE; |
7246 | } | |
7247 | ||
d0bf826b AM |
7248 | /* Copy private header information. */ |
7249 | ||
7250 | bfd_boolean | |
7251 | _bfd_elf_copy_private_header_data (bfd *ibfd, bfd *obfd) | |
7252 | { | |
7253 | if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour | |
7254 | || bfd_get_flavour (obfd) != bfd_target_elf_flavour) | |
7255 | return TRUE; | |
7256 | ||
7257 | /* Copy over private BFD data if it has not already been copied. | |
7258 | This must be done here, rather than in the copy_private_bfd_data | |
7259 | entry point, because the latter is called after the section | |
7260 | contents have been set, which means that the program headers have | |
7261 | already been worked out. */ | |
12bd6957 | 7262 | if (elf_seg_map (obfd) == NULL && elf_tdata (ibfd)->phdr != NULL) |
d0bf826b AM |
7263 | { |
7264 | if (! copy_private_bfd_data (ibfd, obfd)) | |
7265 | return FALSE; | |
7266 | } | |
7267 | ||
7268 | return _bfd_elf_fixup_group_sections (ibfd, NULL); | |
7269 | } | |
7270 | ||
252b5132 RH |
7271 | /* Copy private symbol information. If this symbol is in a section |
7272 | which we did not map into a BFD section, try to map the section | |
7273 | index correctly. We use special macro definitions for the mapped | |
7274 | section indices; these definitions are interpreted by the | |
7275 | swap_out_syms function. */ | |
7276 | ||
9ad5cbcf AM |
7277 | #define MAP_ONESYMTAB (SHN_HIOS + 1) |
7278 | #define MAP_DYNSYMTAB (SHN_HIOS + 2) | |
7279 | #define MAP_STRTAB (SHN_HIOS + 3) | |
7280 | #define MAP_SHSTRTAB (SHN_HIOS + 4) | |
7281 | #define MAP_SYM_SHNDX (SHN_HIOS + 5) | |
252b5132 | 7282 | |
b34976b6 | 7283 | bfd_boolean |
217aa764 AM |
7284 | _bfd_elf_copy_private_symbol_data (bfd *ibfd, |
7285 | asymbol *isymarg, | |
7286 | bfd *obfd, | |
7287 | asymbol *osymarg) | |
252b5132 RH |
7288 | { |
7289 | elf_symbol_type *isym, *osym; | |
7290 | ||
7291 | if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour | |
7292 | || bfd_get_flavour (obfd) != bfd_target_elf_flavour) | |
b34976b6 | 7293 | return TRUE; |
252b5132 RH |
7294 | |
7295 | isym = elf_symbol_from (ibfd, isymarg); | |
7296 | osym = elf_symbol_from (obfd, osymarg); | |
7297 | ||
7298 | if (isym != NULL | |
8424d8f5 | 7299 | && isym->internal_elf_sym.st_shndx != 0 |
252b5132 RH |
7300 | && osym != NULL |
7301 | && bfd_is_abs_section (isym->symbol.section)) | |
7302 | { | |
7303 | unsigned int shndx; | |
7304 | ||
7305 | shndx = isym->internal_elf_sym.st_shndx; | |
7306 | if (shndx == elf_onesymtab (ibfd)) | |
7307 | shndx = MAP_ONESYMTAB; | |
7308 | else if (shndx == elf_dynsymtab (ibfd)) | |
7309 | shndx = MAP_DYNSYMTAB; | |
12bd6957 | 7310 | else if (shndx == elf_strtab_sec (ibfd)) |
252b5132 | 7311 | shndx = MAP_STRTAB; |
12bd6957 | 7312 | else if (shndx == elf_shstrtab_sec (ibfd)) |
252b5132 | 7313 | shndx = MAP_SHSTRTAB; |
6a40cf0c | 7314 | else if (find_section_in_list (shndx, elf_symtab_shndx_list (ibfd))) |
9ad5cbcf | 7315 | shndx = MAP_SYM_SHNDX; |
252b5132 RH |
7316 | osym->internal_elf_sym.st_shndx = shndx; |
7317 | } | |
7318 | ||
b34976b6 | 7319 | return TRUE; |
252b5132 RH |
7320 | } |
7321 | ||
7322 | /* Swap out the symbols. */ | |
7323 | ||
b34976b6 | 7324 | static bfd_boolean |
217aa764 | 7325 | swap_out_syms (bfd *abfd, |
ef10c3ac | 7326 | struct elf_strtab_hash **sttp, |
217aa764 | 7327 | int relocatable_p) |
252b5132 | 7328 | { |
9c5bfbb7 | 7329 | const struct elf_backend_data *bed; |
079e9a2f AM |
7330 | int symcount; |
7331 | asymbol **syms; | |
ef10c3ac | 7332 | struct elf_strtab_hash *stt; |
079e9a2f | 7333 | Elf_Internal_Shdr *symtab_hdr; |
9ad5cbcf | 7334 | Elf_Internal_Shdr *symtab_shndx_hdr; |
079e9a2f | 7335 | Elf_Internal_Shdr *symstrtab_hdr; |
ef10c3ac | 7336 | struct elf_sym_strtab *symstrtab; |
f075ee0c AM |
7337 | bfd_byte *outbound_syms; |
7338 | bfd_byte *outbound_shndx; | |
ef10c3ac L |
7339 | unsigned long outbound_syms_index; |
7340 | unsigned long outbound_shndx_index; | |
079e9a2f | 7341 | int idx; |
12bd6957 | 7342 | unsigned int num_locals; |
079e9a2f | 7343 | bfd_size_type amt; |
174fd7f9 | 7344 | bfd_boolean name_local_sections; |
252b5132 | 7345 | |
12bd6957 | 7346 | if (!elf_map_symbols (abfd, &num_locals)) |
b34976b6 | 7347 | return FALSE; |
252b5132 | 7348 | |
c044fabd | 7349 | /* Dump out the symtabs. */ |
ef10c3ac | 7350 | stt = _bfd_elf_strtab_init (); |
079e9a2f | 7351 | if (stt == NULL) |
b34976b6 | 7352 | return FALSE; |
252b5132 | 7353 | |
079e9a2f AM |
7354 | bed = get_elf_backend_data (abfd); |
7355 | symcount = bfd_get_symcount (abfd); | |
7356 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; | |
7357 | symtab_hdr->sh_type = SHT_SYMTAB; | |
7358 | symtab_hdr->sh_entsize = bed->s->sizeof_sym; | |
7359 | symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1); | |
12bd6957 | 7360 | symtab_hdr->sh_info = num_locals + 1; |
72de5009 | 7361 | symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align; |
079e9a2f AM |
7362 | |
7363 | symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr; | |
7364 | symstrtab_hdr->sh_type = SHT_STRTAB; | |
7365 | ||
ef10c3ac L |
7366 | /* Allocate buffer to swap out the .strtab section. */ |
7367 | symstrtab = (struct elf_sym_strtab *) bfd_malloc ((symcount + 1) | |
7368 | * sizeof (*symstrtab)); | |
7369 | if (symstrtab == NULL) | |
7370 | { | |
7371 | _bfd_elf_strtab_free (stt); | |
7372 | return FALSE; | |
7373 | } | |
7374 | ||
a50b1753 NC |
7375 | outbound_syms = (bfd_byte *) bfd_alloc2 (abfd, 1 + symcount, |
7376 | bed->s->sizeof_sym); | |
079e9a2f | 7377 | if (outbound_syms == NULL) |
5ed6aba4 | 7378 | { |
ef10c3ac L |
7379 | error_return: |
7380 | _bfd_elf_strtab_free (stt); | |
7381 | free (symstrtab); | |
5ed6aba4 NC |
7382 | return FALSE; |
7383 | } | |
217aa764 | 7384 | symtab_hdr->contents = outbound_syms; |
ef10c3ac | 7385 | outbound_syms_index = 0; |
252b5132 | 7386 | |
9ad5cbcf | 7387 | outbound_shndx = NULL; |
ef10c3ac | 7388 | outbound_shndx_index = 0; |
6a40cf0c NC |
7389 | |
7390 | if (elf_symtab_shndx_list (abfd)) | |
9ad5cbcf | 7391 | { |
6a40cf0c NC |
7392 | symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr; |
7393 | if (symtab_shndx_hdr->sh_name != 0) | |
7394 | { | |
7395 | amt = (bfd_size_type) (1 + symcount) * sizeof (Elf_External_Sym_Shndx); | |
7396 | outbound_shndx = (bfd_byte *) | |
7397 | bfd_zalloc2 (abfd, 1 + symcount, sizeof (Elf_External_Sym_Shndx)); | |
7398 | if (outbound_shndx == NULL) | |
7399 | goto error_return; | |
5ed6aba4 | 7400 | |
6a40cf0c NC |
7401 | symtab_shndx_hdr->contents = outbound_shndx; |
7402 | symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX; | |
7403 | symtab_shndx_hdr->sh_size = amt; | |
7404 | symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx); | |
7405 | symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx); | |
7406 | } | |
7407 | /* FIXME: What about any other headers in the list ? */ | |
9ad5cbcf AM |
7408 | } |
7409 | ||
589e6347 | 7410 | /* Now generate the data (for "contents"). */ |
079e9a2f AM |
7411 | { |
7412 | /* Fill in zeroth symbol and swap it out. */ | |
7413 | Elf_Internal_Sym sym; | |
7414 | sym.st_name = 0; | |
7415 | sym.st_value = 0; | |
7416 | sym.st_size = 0; | |
7417 | sym.st_info = 0; | |
7418 | sym.st_other = 0; | |
7419 | sym.st_shndx = SHN_UNDEF; | |
35fc36a8 | 7420 | sym.st_target_internal = 0; |
ef10c3ac L |
7421 | symstrtab[0].sym = sym; |
7422 | symstrtab[0].dest_index = outbound_syms_index; | |
7423 | symstrtab[0].destshndx_index = outbound_shndx_index; | |
7424 | outbound_syms_index++; | |
9ad5cbcf | 7425 | if (outbound_shndx != NULL) |
ef10c3ac | 7426 | outbound_shndx_index++; |
079e9a2f | 7427 | } |
252b5132 | 7428 | |
174fd7f9 RS |
7429 | name_local_sections |
7430 | = (bed->elf_backend_name_local_section_symbols | |
7431 | && bed->elf_backend_name_local_section_symbols (abfd)); | |
7432 | ||
079e9a2f | 7433 | syms = bfd_get_outsymbols (abfd); |
ef10c3ac | 7434 | for (idx = 0; idx < symcount;) |
252b5132 | 7435 | { |
252b5132 | 7436 | Elf_Internal_Sym sym; |
079e9a2f AM |
7437 | bfd_vma value = syms[idx]->value; |
7438 | elf_symbol_type *type_ptr; | |
7439 | flagword flags = syms[idx]->flags; | |
7440 | int type; | |
252b5132 | 7441 | |
174fd7f9 RS |
7442 | if (!name_local_sections |
7443 | && (flags & (BSF_SECTION_SYM | BSF_GLOBAL)) == BSF_SECTION_SYM) | |
079e9a2f AM |
7444 | { |
7445 | /* Local section symbols have no name. */ | |
ef10c3ac | 7446 | sym.st_name = (unsigned long) -1; |
079e9a2f AM |
7447 | } |
7448 | else | |
7449 | { | |
ef10c3ac L |
7450 | /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize |
7451 | to get the final offset for st_name. */ | |
7452 | sym.st_name | |
7453 | = (unsigned long) _bfd_elf_strtab_add (stt, syms[idx]->name, | |
7454 | FALSE); | |
079e9a2f | 7455 | if (sym.st_name == (unsigned long) -1) |
ef10c3ac | 7456 | goto error_return; |
079e9a2f | 7457 | } |
252b5132 | 7458 | |
079e9a2f | 7459 | type_ptr = elf_symbol_from (abfd, syms[idx]); |
252b5132 | 7460 | |
079e9a2f AM |
7461 | if ((flags & BSF_SECTION_SYM) == 0 |
7462 | && bfd_is_com_section (syms[idx]->section)) | |
7463 | { | |
7464 | /* ELF common symbols put the alignment into the `value' field, | |
7465 | and the size into the `size' field. This is backwards from | |
7466 | how BFD handles it, so reverse it here. */ | |
7467 | sym.st_size = value; | |
7468 | if (type_ptr == NULL | |
7469 | || type_ptr->internal_elf_sym.st_value == 0) | |
7470 | sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value)); | |
7471 | else | |
7472 | sym.st_value = type_ptr->internal_elf_sym.st_value; | |
7473 | sym.st_shndx = _bfd_elf_section_from_bfd_section | |
7474 | (abfd, syms[idx]->section); | |
7475 | } | |
7476 | else | |
7477 | { | |
7478 | asection *sec = syms[idx]->section; | |
cb33740c | 7479 | unsigned int shndx; |
252b5132 | 7480 | |
079e9a2f AM |
7481 | if (sec->output_section) |
7482 | { | |
7483 | value += sec->output_offset; | |
7484 | sec = sec->output_section; | |
7485 | } | |
589e6347 | 7486 | |
079e9a2f AM |
7487 | /* Don't add in the section vma for relocatable output. */ |
7488 | if (! relocatable_p) | |
7489 | value += sec->vma; | |
7490 | sym.st_value = value; | |
7491 | sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0; | |
7492 | ||
7493 | if (bfd_is_abs_section (sec) | |
7494 | && type_ptr != NULL | |
7495 | && type_ptr->internal_elf_sym.st_shndx != 0) | |
7496 | { | |
7497 | /* This symbol is in a real ELF section which we did | |
7498 | not create as a BFD section. Undo the mapping done | |
7499 | by copy_private_symbol_data. */ | |
7500 | shndx = type_ptr->internal_elf_sym.st_shndx; | |
7501 | switch (shndx) | |
7502 | { | |
7503 | case MAP_ONESYMTAB: | |
7504 | shndx = elf_onesymtab (abfd); | |
7505 | break; | |
7506 | case MAP_DYNSYMTAB: | |
7507 | shndx = elf_dynsymtab (abfd); | |
7508 | break; | |
7509 | case MAP_STRTAB: | |
12bd6957 | 7510 | shndx = elf_strtab_sec (abfd); |
079e9a2f AM |
7511 | break; |
7512 | case MAP_SHSTRTAB: | |
12bd6957 | 7513 | shndx = elf_shstrtab_sec (abfd); |
079e9a2f | 7514 | break; |
9ad5cbcf | 7515 | case MAP_SYM_SHNDX: |
6a40cf0c NC |
7516 | if (elf_symtab_shndx_list (abfd)) |
7517 | shndx = elf_symtab_shndx_list (abfd)->ndx; | |
9ad5cbcf | 7518 | break; |
079e9a2f | 7519 | default: |
15bc576a | 7520 | shndx = SHN_ABS; |
079e9a2f AM |
7521 | break; |
7522 | } | |
7523 | } | |
7524 | else | |
7525 | { | |
7526 | shndx = _bfd_elf_section_from_bfd_section (abfd, sec); | |
252b5132 | 7527 | |
cb33740c | 7528 | if (shndx == SHN_BAD) |
079e9a2f AM |
7529 | { |
7530 | asection *sec2; | |
7531 | ||
7532 | /* Writing this would be a hell of a lot easier if | |
7533 | we had some decent documentation on bfd, and | |
7534 | knew what to expect of the library, and what to | |
7535 | demand of applications. For example, it | |
7536 | appears that `objcopy' might not set the | |
7537 | section of a symbol to be a section that is | |
7538 | actually in the output file. */ | |
7539 | sec2 = bfd_get_section_by_name (abfd, sec->name); | |
589e6347 NC |
7540 | if (sec2 == NULL) |
7541 | { | |
7542 | _bfd_error_handler (_("\ | |
7543 | Unable to find equivalent output section for symbol '%s' from section '%s'"), | |
7544 | syms[idx]->name ? syms[idx]->name : "<Local sym>", | |
7545 | sec->name); | |
811072d8 | 7546 | bfd_set_error (bfd_error_invalid_operation); |
ef10c3ac | 7547 | goto error_return; |
589e6347 | 7548 | } |
811072d8 | 7549 | |
079e9a2f | 7550 | shndx = _bfd_elf_section_from_bfd_section (abfd, sec2); |
cb33740c | 7551 | BFD_ASSERT (shndx != SHN_BAD); |
079e9a2f AM |
7552 | } |
7553 | } | |
252b5132 | 7554 | |
079e9a2f AM |
7555 | sym.st_shndx = shndx; |
7556 | } | |
252b5132 | 7557 | |
13ae64f3 JJ |
7558 | if ((flags & BSF_THREAD_LOCAL) != 0) |
7559 | type = STT_TLS; | |
d8045f23 NC |
7560 | else if ((flags & BSF_GNU_INDIRECT_FUNCTION) != 0) |
7561 | type = STT_GNU_IFUNC; | |
13ae64f3 | 7562 | else if ((flags & BSF_FUNCTION) != 0) |
079e9a2f AM |
7563 | type = STT_FUNC; |
7564 | else if ((flags & BSF_OBJECT) != 0) | |
7565 | type = STT_OBJECT; | |
d9352518 DB |
7566 | else if ((flags & BSF_RELC) != 0) |
7567 | type = STT_RELC; | |
7568 | else if ((flags & BSF_SRELC) != 0) | |
7569 | type = STT_SRELC; | |
079e9a2f AM |
7570 | else |
7571 | type = STT_NOTYPE; | |
252b5132 | 7572 | |
13ae64f3 JJ |
7573 | if (syms[idx]->section->flags & SEC_THREAD_LOCAL) |
7574 | type = STT_TLS; | |
7575 | ||
589e6347 | 7576 | /* Processor-specific types. */ |
079e9a2f AM |
7577 | if (type_ptr != NULL |
7578 | && bed->elf_backend_get_symbol_type) | |
7579 | type = ((*bed->elf_backend_get_symbol_type) | |
7580 | (&type_ptr->internal_elf_sym, type)); | |
252b5132 | 7581 | |
079e9a2f AM |
7582 | if (flags & BSF_SECTION_SYM) |
7583 | { | |
7584 | if (flags & BSF_GLOBAL) | |
7585 | sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION); | |
7586 | else | |
7587 | sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION); | |
7588 | } | |
7589 | else if (bfd_is_com_section (syms[idx]->section)) | |
0a40daed | 7590 | { |
b8871f35 L |
7591 | if (type != STT_TLS) |
7592 | { | |
7593 | if ((abfd->flags & BFD_CONVERT_ELF_COMMON)) | |
7594 | type = ((abfd->flags & BFD_USE_ELF_STT_COMMON) | |
7595 | ? STT_COMMON : STT_OBJECT); | |
7596 | else | |
7597 | type = ((flags & BSF_ELF_COMMON) != 0 | |
7598 | ? STT_COMMON : STT_OBJECT); | |
7599 | } | |
7600 | sym.st_info = ELF_ST_INFO (STB_GLOBAL, type); | |
0a40daed | 7601 | } |
079e9a2f AM |
7602 | else if (bfd_is_und_section (syms[idx]->section)) |
7603 | sym.st_info = ELF_ST_INFO (((flags & BSF_WEAK) | |
7604 | ? STB_WEAK | |
7605 | : STB_GLOBAL), | |
7606 | type); | |
7607 | else if (flags & BSF_FILE) | |
7608 | sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE); | |
7609 | else | |
7610 | { | |
7611 | int bind = STB_LOCAL; | |
252b5132 | 7612 | |
079e9a2f AM |
7613 | if (flags & BSF_LOCAL) |
7614 | bind = STB_LOCAL; | |
3e7a7d11 NC |
7615 | else if (flags & BSF_GNU_UNIQUE) |
7616 | bind = STB_GNU_UNIQUE; | |
079e9a2f AM |
7617 | else if (flags & BSF_WEAK) |
7618 | bind = STB_WEAK; | |
7619 | else if (flags & BSF_GLOBAL) | |
7620 | bind = STB_GLOBAL; | |
252b5132 | 7621 | |
079e9a2f AM |
7622 | sym.st_info = ELF_ST_INFO (bind, type); |
7623 | } | |
252b5132 | 7624 | |
079e9a2f | 7625 | if (type_ptr != NULL) |
35fc36a8 RS |
7626 | { |
7627 | sym.st_other = type_ptr->internal_elf_sym.st_other; | |
7628 | sym.st_target_internal | |
7629 | = type_ptr->internal_elf_sym.st_target_internal; | |
7630 | } | |
079e9a2f | 7631 | else |
35fc36a8 RS |
7632 | { |
7633 | sym.st_other = 0; | |
7634 | sym.st_target_internal = 0; | |
7635 | } | |
252b5132 | 7636 | |
ef10c3ac L |
7637 | idx++; |
7638 | symstrtab[idx].sym = sym; | |
7639 | symstrtab[idx].dest_index = outbound_syms_index; | |
7640 | symstrtab[idx].destshndx_index = outbound_shndx_index; | |
7641 | ||
7642 | outbound_syms_index++; | |
9ad5cbcf | 7643 | if (outbound_shndx != NULL) |
ef10c3ac L |
7644 | outbound_shndx_index++; |
7645 | } | |
7646 | ||
7647 | /* Finalize the .strtab section. */ | |
7648 | _bfd_elf_strtab_finalize (stt); | |
7649 | ||
7650 | /* Swap out the .strtab section. */ | |
7651 | for (idx = 0; idx <= symcount; idx++) | |
7652 | { | |
7653 | struct elf_sym_strtab *elfsym = &symstrtab[idx]; | |
7654 | if (elfsym->sym.st_name == (unsigned long) -1) | |
7655 | elfsym->sym.st_name = 0; | |
7656 | else | |
7657 | elfsym->sym.st_name = _bfd_elf_strtab_offset (stt, | |
7658 | elfsym->sym.st_name); | |
7659 | bed->s->swap_symbol_out (abfd, &elfsym->sym, | |
7660 | (outbound_syms | |
7661 | + (elfsym->dest_index | |
7662 | * bed->s->sizeof_sym)), | |
7663 | (outbound_shndx | |
7664 | + (elfsym->destshndx_index | |
7665 | * sizeof (Elf_External_Sym_Shndx)))); | |
079e9a2f | 7666 | } |
ef10c3ac | 7667 | free (symstrtab); |
252b5132 | 7668 | |
079e9a2f | 7669 | *sttp = stt; |
ef10c3ac | 7670 | symstrtab_hdr->sh_size = _bfd_elf_strtab_size (stt); |
079e9a2f | 7671 | symstrtab_hdr->sh_type = SHT_STRTAB; |
84865015 | 7672 | symstrtab_hdr->sh_flags = bed->elf_strtab_flags; |
079e9a2f AM |
7673 | symstrtab_hdr->sh_addr = 0; |
7674 | symstrtab_hdr->sh_entsize = 0; | |
7675 | symstrtab_hdr->sh_link = 0; | |
7676 | symstrtab_hdr->sh_info = 0; | |
7677 | symstrtab_hdr->sh_addralign = 1; | |
252b5132 | 7678 | |
b34976b6 | 7679 | return TRUE; |
252b5132 RH |
7680 | } |
7681 | ||
7682 | /* Return the number of bytes required to hold the symtab vector. | |
7683 | ||
7684 | Note that we base it on the count plus 1, since we will null terminate | |
7685 | the vector allocated based on this size. However, the ELF symbol table | |
7686 | always has a dummy entry as symbol #0, so it ends up even. */ | |
7687 | ||
7688 | long | |
217aa764 | 7689 | _bfd_elf_get_symtab_upper_bound (bfd *abfd) |
252b5132 RH |
7690 | { |
7691 | long symcount; | |
7692 | long symtab_size; | |
7693 | Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr; | |
7694 | ||
7695 | symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym; | |
b99d1833 AM |
7696 | symtab_size = (symcount + 1) * (sizeof (asymbol *)); |
7697 | if (symcount > 0) | |
7698 | symtab_size -= sizeof (asymbol *); | |
252b5132 RH |
7699 | |
7700 | return symtab_size; | |
7701 | } | |
7702 | ||
7703 | long | |
217aa764 | 7704 | _bfd_elf_get_dynamic_symtab_upper_bound (bfd *abfd) |
252b5132 RH |
7705 | { |
7706 | long symcount; | |
7707 | long symtab_size; | |
7708 | Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr; | |
7709 | ||
7710 | if (elf_dynsymtab (abfd) == 0) | |
7711 | { | |
7712 | bfd_set_error (bfd_error_invalid_operation); | |
7713 | return -1; | |
7714 | } | |
7715 | ||
7716 | symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym; | |
b99d1833 AM |
7717 | symtab_size = (symcount + 1) * (sizeof (asymbol *)); |
7718 | if (symcount > 0) | |
7719 | symtab_size -= sizeof (asymbol *); | |
252b5132 RH |
7720 | |
7721 | return symtab_size; | |
7722 | } | |
7723 | ||
7724 | long | |
217aa764 AM |
7725 | _bfd_elf_get_reloc_upper_bound (bfd *abfd ATTRIBUTE_UNUSED, |
7726 | sec_ptr asect) | |
252b5132 RH |
7727 | { |
7728 | return (asect->reloc_count + 1) * sizeof (arelent *); | |
7729 | } | |
7730 | ||
7731 | /* Canonicalize the relocs. */ | |
7732 | ||
7733 | long | |
217aa764 AM |
7734 | _bfd_elf_canonicalize_reloc (bfd *abfd, |
7735 | sec_ptr section, | |
7736 | arelent **relptr, | |
7737 | asymbol **symbols) | |
252b5132 RH |
7738 | { |
7739 | arelent *tblptr; | |
7740 | unsigned int i; | |
9c5bfbb7 | 7741 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
252b5132 | 7742 | |
b34976b6 | 7743 | if (! bed->s->slurp_reloc_table (abfd, section, symbols, FALSE)) |
252b5132 RH |
7744 | return -1; |
7745 | ||
7746 | tblptr = section->relocation; | |
7747 | for (i = 0; i < section->reloc_count; i++) | |
7748 | *relptr++ = tblptr++; | |
7749 | ||
7750 | *relptr = NULL; | |
7751 | ||
7752 | return section->reloc_count; | |
7753 | } | |
7754 | ||
7755 | long | |
6cee3f79 | 7756 | _bfd_elf_canonicalize_symtab (bfd *abfd, asymbol **allocation) |
252b5132 | 7757 | { |
9c5bfbb7 | 7758 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
217aa764 | 7759 | long symcount = bed->s->slurp_symbol_table (abfd, allocation, FALSE); |
252b5132 RH |
7760 | |
7761 | if (symcount >= 0) | |
7762 | bfd_get_symcount (abfd) = symcount; | |
7763 | return symcount; | |
7764 | } | |
7765 | ||
7766 | long | |
217aa764 AM |
7767 | _bfd_elf_canonicalize_dynamic_symtab (bfd *abfd, |
7768 | asymbol **allocation) | |
252b5132 | 7769 | { |
9c5bfbb7 | 7770 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
217aa764 | 7771 | long symcount = bed->s->slurp_symbol_table (abfd, allocation, TRUE); |
1f70368c DJ |
7772 | |
7773 | if (symcount >= 0) | |
7774 | bfd_get_dynamic_symcount (abfd) = symcount; | |
7775 | return symcount; | |
252b5132 RH |
7776 | } |
7777 | ||
8615f3f2 AM |
7778 | /* Return the size required for the dynamic reloc entries. Any loadable |
7779 | section that was actually installed in the BFD, and has type SHT_REL | |
7780 | or SHT_RELA, and uses the dynamic symbol table, is considered to be a | |
7781 | dynamic reloc section. */ | |
252b5132 RH |
7782 | |
7783 | long | |
217aa764 | 7784 | _bfd_elf_get_dynamic_reloc_upper_bound (bfd *abfd) |
252b5132 RH |
7785 | { |
7786 | long ret; | |
7787 | asection *s; | |
7788 | ||
7789 | if (elf_dynsymtab (abfd) == 0) | |
7790 | { | |
7791 | bfd_set_error (bfd_error_invalid_operation); | |
7792 | return -1; | |
7793 | } | |
7794 | ||
7795 | ret = sizeof (arelent *); | |
7796 | for (s = abfd->sections; s != NULL; s = s->next) | |
266b05cf | 7797 | if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd) |
252b5132 RH |
7798 | && (elf_section_data (s)->this_hdr.sh_type == SHT_REL |
7799 | || elf_section_data (s)->this_hdr.sh_type == SHT_RELA)) | |
eea6121a | 7800 | ret += ((s->size / elf_section_data (s)->this_hdr.sh_entsize) |
252b5132 RH |
7801 | * sizeof (arelent *)); |
7802 | ||
7803 | return ret; | |
7804 | } | |
7805 | ||
8615f3f2 AM |
7806 | /* Canonicalize the dynamic relocation entries. Note that we return the |
7807 | dynamic relocations as a single block, although they are actually | |
7808 | associated with particular sections; the interface, which was | |
7809 | designed for SunOS style shared libraries, expects that there is only | |
7810 | one set of dynamic relocs. Any loadable section that was actually | |
7811 | installed in the BFD, and has type SHT_REL or SHT_RELA, and uses the | |
7812 | dynamic symbol table, is considered to be a dynamic reloc section. */ | |
252b5132 RH |
7813 | |
7814 | long | |
217aa764 AM |
7815 | _bfd_elf_canonicalize_dynamic_reloc (bfd *abfd, |
7816 | arelent **storage, | |
7817 | asymbol **syms) | |
252b5132 | 7818 | { |
217aa764 | 7819 | bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean); |
252b5132 RH |
7820 | asection *s; |
7821 | long ret; | |
7822 | ||
7823 | if (elf_dynsymtab (abfd) == 0) | |
7824 | { | |
7825 | bfd_set_error (bfd_error_invalid_operation); | |
7826 | return -1; | |
7827 | } | |
7828 | ||
7829 | slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table; | |
7830 | ret = 0; | |
7831 | for (s = abfd->sections; s != NULL; s = s->next) | |
7832 | { | |
266b05cf | 7833 | if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd) |
252b5132 RH |
7834 | && (elf_section_data (s)->this_hdr.sh_type == SHT_REL |
7835 | || elf_section_data (s)->this_hdr.sh_type == SHT_RELA)) | |
7836 | { | |
7837 | arelent *p; | |
7838 | long count, i; | |
7839 | ||
b34976b6 | 7840 | if (! (*slurp_relocs) (abfd, s, syms, TRUE)) |
252b5132 | 7841 | return -1; |
eea6121a | 7842 | count = s->size / elf_section_data (s)->this_hdr.sh_entsize; |
252b5132 RH |
7843 | p = s->relocation; |
7844 | for (i = 0; i < count; i++) | |
7845 | *storage++ = p++; | |
7846 | ret += count; | |
7847 | } | |
7848 | } | |
7849 | ||
7850 | *storage = NULL; | |
7851 | ||
7852 | return ret; | |
7853 | } | |
7854 | \f | |
7855 | /* Read in the version information. */ | |
7856 | ||
b34976b6 | 7857 | bfd_boolean |
fc0e6df6 | 7858 | _bfd_elf_slurp_version_tables (bfd *abfd, bfd_boolean default_imported_symver) |
252b5132 RH |
7859 | { |
7860 | bfd_byte *contents = NULL; | |
fc0e6df6 PB |
7861 | unsigned int freeidx = 0; |
7862 | ||
7863 | if (elf_dynverref (abfd) != 0) | |
7864 | { | |
7865 | Elf_Internal_Shdr *hdr; | |
7866 | Elf_External_Verneed *everneed; | |
7867 | Elf_Internal_Verneed *iverneed; | |
7868 | unsigned int i; | |
d0fb9a8d | 7869 | bfd_byte *contents_end; |
fc0e6df6 PB |
7870 | |
7871 | hdr = &elf_tdata (abfd)->dynverref_hdr; | |
7872 | ||
601a03ba | 7873 | if (hdr->sh_info == 0 || hdr->sh_size < sizeof (Elf_External_Verneed)) |
d0fb9a8d | 7874 | { |
601a03ba AM |
7875 | error_return_bad_verref: |
7876 | (*_bfd_error_handler) | |
7877 | (_("%B: .gnu.version_r invalid entry"), abfd); | |
7878 | bfd_set_error (bfd_error_bad_value); | |
d0fb9a8d JJ |
7879 | error_return_verref: |
7880 | elf_tdata (abfd)->verref = NULL; | |
7881 | elf_tdata (abfd)->cverrefs = 0; | |
7882 | goto error_return; | |
7883 | } | |
601a03ba AM |
7884 | |
7885 | contents = (bfd_byte *) bfd_malloc (hdr->sh_size); | |
7886 | if (contents == NULL) | |
7887 | goto error_return_verref; | |
7888 | ||
fc0e6df6 PB |
7889 | if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0 |
7890 | || bfd_bread (contents, hdr->sh_size, abfd) != hdr->sh_size) | |
d0fb9a8d | 7891 | goto error_return_verref; |
fc0e6df6 | 7892 | |
601a03ba AM |
7893 | elf_tdata (abfd)->verref = (Elf_Internal_Verneed *) |
7894 | bfd_zalloc2 (abfd, hdr->sh_info, sizeof (Elf_Internal_Verneed)); | |
7895 | ||
7896 | if (elf_tdata (abfd)->verref == NULL) | |
d0fb9a8d JJ |
7897 | goto error_return_verref; |
7898 | ||
7899 | BFD_ASSERT (sizeof (Elf_External_Verneed) | |
7900 | == sizeof (Elf_External_Vernaux)); | |
7901 | contents_end = contents + hdr->sh_size - sizeof (Elf_External_Verneed); | |
fc0e6df6 PB |
7902 | everneed = (Elf_External_Verneed *) contents; |
7903 | iverneed = elf_tdata (abfd)->verref; | |
7904 | for (i = 0; i < hdr->sh_info; i++, iverneed++) | |
7905 | { | |
7906 | Elf_External_Vernaux *evernaux; | |
7907 | Elf_Internal_Vernaux *ivernaux; | |
7908 | unsigned int j; | |
7909 | ||
7910 | _bfd_elf_swap_verneed_in (abfd, everneed, iverneed); | |
7911 | ||
7912 | iverneed->vn_bfd = abfd; | |
7913 | ||
7914 | iverneed->vn_filename = | |
7915 | bfd_elf_string_from_elf_section (abfd, hdr->sh_link, | |
7916 | iverneed->vn_file); | |
7917 | if (iverneed->vn_filename == NULL) | |
601a03ba | 7918 | goto error_return_bad_verref; |
fc0e6df6 | 7919 | |
d0fb9a8d JJ |
7920 | if (iverneed->vn_cnt == 0) |
7921 | iverneed->vn_auxptr = NULL; | |
7922 | else | |
7923 | { | |
a50b1753 NC |
7924 | iverneed->vn_auxptr = (struct elf_internal_vernaux *) |
7925 | bfd_alloc2 (abfd, iverneed->vn_cnt, | |
7926 | sizeof (Elf_Internal_Vernaux)); | |
d0fb9a8d JJ |
7927 | if (iverneed->vn_auxptr == NULL) |
7928 | goto error_return_verref; | |
7929 | } | |
7930 | ||
7931 | if (iverneed->vn_aux | |
7932 | > (size_t) (contents_end - (bfd_byte *) everneed)) | |
601a03ba | 7933 | goto error_return_bad_verref; |
fc0e6df6 PB |
7934 | |
7935 | evernaux = ((Elf_External_Vernaux *) | |
7936 | ((bfd_byte *) everneed + iverneed->vn_aux)); | |
7937 | ivernaux = iverneed->vn_auxptr; | |
7938 | for (j = 0; j < iverneed->vn_cnt; j++, ivernaux++) | |
7939 | { | |
7940 | _bfd_elf_swap_vernaux_in (abfd, evernaux, ivernaux); | |
7941 | ||
7942 | ivernaux->vna_nodename = | |
7943 | bfd_elf_string_from_elf_section (abfd, hdr->sh_link, | |
7944 | ivernaux->vna_name); | |
7945 | if (ivernaux->vna_nodename == NULL) | |
601a03ba | 7946 | goto error_return_bad_verref; |
fc0e6df6 | 7947 | |
25ff461f AM |
7948 | if (ivernaux->vna_other > freeidx) |
7949 | freeidx = ivernaux->vna_other; | |
7950 | ||
7951 | ivernaux->vna_nextptr = NULL; | |
7952 | if (ivernaux->vna_next == 0) | |
7953 | { | |
7954 | iverneed->vn_cnt = j + 1; | |
7955 | break; | |
7956 | } | |
fc0e6df6 PB |
7957 | if (j + 1 < iverneed->vn_cnt) |
7958 | ivernaux->vna_nextptr = ivernaux + 1; | |
fc0e6df6 | 7959 | |
d0fb9a8d JJ |
7960 | if (ivernaux->vna_next |
7961 | > (size_t) (contents_end - (bfd_byte *) evernaux)) | |
601a03ba | 7962 | goto error_return_bad_verref; |
d0fb9a8d | 7963 | |
fc0e6df6 PB |
7964 | evernaux = ((Elf_External_Vernaux *) |
7965 | ((bfd_byte *) evernaux + ivernaux->vna_next)); | |
fc0e6df6 PB |
7966 | } |
7967 | ||
25ff461f AM |
7968 | iverneed->vn_nextref = NULL; |
7969 | if (iverneed->vn_next == 0) | |
7970 | break; | |
fc0e6df6 PB |
7971 | if (i + 1 < hdr->sh_info) |
7972 | iverneed->vn_nextref = iverneed + 1; | |
fc0e6df6 | 7973 | |
d0fb9a8d JJ |
7974 | if (iverneed->vn_next |
7975 | > (size_t) (contents_end - (bfd_byte *) everneed)) | |
601a03ba | 7976 | goto error_return_bad_verref; |
d0fb9a8d | 7977 | |
fc0e6df6 PB |
7978 | everneed = ((Elf_External_Verneed *) |
7979 | ((bfd_byte *) everneed + iverneed->vn_next)); | |
7980 | } | |
25ff461f | 7981 | elf_tdata (abfd)->cverrefs = i; |
fc0e6df6 PB |
7982 | |
7983 | free (contents); | |
7984 | contents = NULL; | |
7985 | } | |
252b5132 RH |
7986 | |
7987 | if (elf_dynverdef (abfd) != 0) | |
7988 | { | |
7989 | Elf_Internal_Shdr *hdr; | |
7990 | Elf_External_Verdef *everdef; | |
7991 | Elf_Internal_Verdef *iverdef; | |
f631889e UD |
7992 | Elf_Internal_Verdef *iverdefarr; |
7993 | Elf_Internal_Verdef iverdefmem; | |
252b5132 | 7994 | unsigned int i; |
062e2358 | 7995 | unsigned int maxidx; |
d0fb9a8d | 7996 | bfd_byte *contents_end_def, *contents_end_aux; |
252b5132 RH |
7997 | |
7998 | hdr = &elf_tdata (abfd)->dynverdef_hdr; | |
7999 | ||
601a03ba AM |
8000 | if (hdr->sh_info == 0 || hdr->sh_size < sizeof (Elf_External_Verdef)) |
8001 | { | |
8002 | error_return_bad_verdef: | |
8003 | (*_bfd_error_handler) | |
8004 | (_("%B: .gnu.version_d invalid entry"), abfd); | |
8005 | bfd_set_error (bfd_error_bad_value); | |
8006 | error_return_verdef: | |
8007 | elf_tdata (abfd)->verdef = NULL; | |
8008 | elf_tdata (abfd)->cverdefs = 0; | |
8009 | goto error_return; | |
8010 | } | |
8011 | ||
a50b1753 | 8012 | contents = (bfd_byte *) bfd_malloc (hdr->sh_size); |
252b5132 | 8013 | if (contents == NULL) |
601a03ba | 8014 | goto error_return_verdef; |
252b5132 | 8015 | if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0 |
217aa764 | 8016 | || bfd_bread (contents, hdr->sh_size, abfd) != hdr->sh_size) |
601a03ba | 8017 | goto error_return_verdef; |
d0fb9a8d JJ |
8018 | |
8019 | BFD_ASSERT (sizeof (Elf_External_Verdef) | |
8020 | >= sizeof (Elf_External_Verdaux)); | |
8021 | contents_end_def = contents + hdr->sh_size | |
8022 | - sizeof (Elf_External_Verdef); | |
8023 | contents_end_aux = contents + hdr->sh_size | |
8024 | - sizeof (Elf_External_Verdaux); | |
8025 | ||
f631889e UD |
8026 | /* We know the number of entries in the section but not the maximum |
8027 | index. Therefore we have to run through all entries and find | |
8028 | the maximum. */ | |
252b5132 | 8029 | everdef = (Elf_External_Verdef *) contents; |
f631889e UD |
8030 | maxidx = 0; |
8031 | for (i = 0; i < hdr->sh_info; ++i) | |
8032 | { | |
8033 | _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem); | |
8034 | ||
601a03ba AM |
8035 | if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) == 0) |
8036 | goto error_return_bad_verdef; | |
062e2358 AM |
8037 | if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) > maxidx) |
8038 | maxidx = iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION); | |
f631889e | 8039 | |
25ff461f AM |
8040 | if (iverdefmem.vd_next == 0) |
8041 | break; | |
8042 | ||
d0fb9a8d JJ |
8043 | if (iverdefmem.vd_next |
8044 | > (size_t) (contents_end_def - (bfd_byte *) everdef)) | |
601a03ba | 8045 | goto error_return_bad_verdef; |
d0fb9a8d | 8046 | |
f631889e UD |
8047 | everdef = ((Elf_External_Verdef *) |
8048 | ((bfd_byte *) everdef + iverdefmem.vd_next)); | |
8049 | } | |
8050 | ||
fc0e6df6 PB |
8051 | if (default_imported_symver) |
8052 | { | |
8053 | if (freeidx > maxidx) | |
8054 | maxidx = ++freeidx; | |
8055 | else | |
8056 | freeidx = ++maxidx; | |
8057 | } | |
201159ec | 8058 | |
601a03ba AM |
8059 | elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *) |
8060 | bfd_zalloc2 (abfd, maxidx, sizeof (Elf_Internal_Verdef)); | |
f631889e | 8061 | if (elf_tdata (abfd)->verdef == NULL) |
601a03ba | 8062 | goto error_return_verdef; |
f631889e UD |
8063 | |
8064 | elf_tdata (abfd)->cverdefs = maxidx; | |
8065 | ||
8066 | everdef = (Elf_External_Verdef *) contents; | |
8067 | iverdefarr = elf_tdata (abfd)->verdef; | |
8068 | for (i = 0; i < hdr->sh_info; i++) | |
252b5132 RH |
8069 | { |
8070 | Elf_External_Verdaux *everdaux; | |
8071 | Elf_Internal_Verdaux *iverdaux; | |
8072 | unsigned int j; | |
8073 | ||
f631889e UD |
8074 | _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem); |
8075 | ||
d0fb9a8d | 8076 | if ((iverdefmem.vd_ndx & VERSYM_VERSION) == 0) |
601a03ba | 8077 | goto error_return_bad_verdef; |
d0fb9a8d | 8078 | |
f631889e | 8079 | iverdef = &iverdefarr[(iverdefmem.vd_ndx & VERSYM_VERSION) - 1]; |
595bce75 | 8080 | memcpy (iverdef, &iverdefmem, offsetof (Elf_Internal_Verdef, vd_bfd)); |
252b5132 RH |
8081 | |
8082 | iverdef->vd_bfd = abfd; | |
8083 | ||
d0fb9a8d JJ |
8084 | if (iverdef->vd_cnt == 0) |
8085 | iverdef->vd_auxptr = NULL; | |
8086 | else | |
8087 | { | |
a50b1753 NC |
8088 | iverdef->vd_auxptr = (struct elf_internal_verdaux *) |
8089 | bfd_alloc2 (abfd, iverdef->vd_cnt, | |
8090 | sizeof (Elf_Internal_Verdaux)); | |
d0fb9a8d JJ |
8091 | if (iverdef->vd_auxptr == NULL) |
8092 | goto error_return_verdef; | |
8093 | } | |
8094 | ||
8095 | if (iverdef->vd_aux | |
8096 | > (size_t) (contents_end_aux - (bfd_byte *) everdef)) | |
601a03ba | 8097 | goto error_return_bad_verdef; |
252b5132 RH |
8098 | |
8099 | everdaux = ((Elf_External_Verdaux *) | |
8100 | ((bfd_byte *) everdef + iverdef->vd_aux)); | |
8101 | iverdaux = iverdef->vd_auxptr; | |
8102 | for (j = 0; j < iverdef->vd_cnt; j++, iverdaux++) | |
8103 | { | |
8104 | _bfd_elf_swap_verdaux_in (abfd, everdaux, iverdaux); | |
8105 | ||
8106 | iverdaux->vda_nodename = | |
8107 | bfd_elf_string_from_elf_section (abfd, hdr->sh_link, | |
8108 | iverdaux->vda_name); | |
8109 | if (iverdaux->vda_nodename == NULL) | |
601a03ba | 8110 | goto error_return_bad_verdef; |
252b5132 | 8111 | |
25ff461f AM |
8112 | iverdaux->vda_nextptr = NULL; |
8113 | if (iverdaux->vda_next == 0) | |
8114 | { | |
8115 | iverdef->vd_cnt = j + 1; | |
8116 | break; | |
8117 | } | |
252b5132 RH |
8118 | if (j + 1 < iverdef->vd_cnt) |
8119 | iverdaux->vda_nextptr = iverdaux + 1; | |
252b5132 | 8120 | |
d0fb9a8d JJ |
8121 | if (iverdaux->vda_next |
8122 | > (size_t) (contents_end_aux - (bfd_byte *) everdaux)) | |
601a03ba | 8123 | goto error_return_bad_verdef; |
d0fb9a8d | 8124 | |
252b5132 RH |
8125 | everdaux = ((Elf_External_Verdaux *) |
8126 | ((bfd_byte *) everdaux + iverdaux->vda_next)); | |
8127 | } | |
8128 | ||
595bce75 | 8129 | iverdef->vd_nodename = NULL; |
d0fb9a8d JJ |
8130 | if (iverdef->vd_cnt) |
8131 | iverdef->vd_nodename = iverdef->vd_auxptr->vda_nodename; | |
252b5132 | 8132 | |
25ff461f AM |
8133 | iverdef->vd_nextdef = NULL; |
8134 | if (iverdef->vd_next == 0) | |
8135 | break; | |
d0fb9a8d | 8136 | if ((size_t) (iverdef - iverdefarr) + 1 < maxidx) |
252b5132 | 8137 | iverdef->vd_nextdef = iverdef + 1; |
252b5132 RH |
8138 | |
8139 | everdef = ((Elf_External_Verdef *) | |
8140 | ((bfd_byte *) everdef + iverdef->vd_next)); | |
8141 | } | |
8142 | ||
8143 | free (contents); | |
8144 | contents = NULL; | |
8145 | } | |
fc0e6df6 | 8146 | else if (default_imported_symver) |
252b5132 | 8147 | { |
fc0e6df6 PB |
8148 | if (freeidx < 3) |
8149 | freeidx = 3; | |
8150 | else | |
8151 | freeidx++; | |
252b5132 | 8152 | |
a50b1753 NC |
8153 | elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *) |
8154 | bfd_zalloc2 (abfd, freeidx, sizeof (Elf_Internal_Verdef)); | |
fc0e6df6 | 8155 | if (elf_tdata (abfd)->verdef == NULL) |
252b5132 RH |
8156 | goto error_return; |
8157 | ||
fc0e6df6 PB |
8158 | elf_tdata (abfd)->cverdefs = freeidx; |
8159 | } | |
252b5132 | 8160 | |
fc0e6df6 PB |
8161 | /* Create a default version based on the soname. */ |
8162 | if (default_imported_symver) | |
8163 | { | |
8164 | Elf_Internal_Verdef *iverdef; | |
8165 | Elf_Internal_Verdaux *iverdaux; | |
252b5132 | 8166 | |
5bb3703f | 8167 | iverdef = &elf_tdata (abfd)->verdef[freeidx - 1]; |
252b5132 | 8168 | |
fc0e6df6 PB |
8169 | iverdef->vd_version = VER_DEF_CURRENT; |
8170 | iverdef->vd_flags = 0; | |
8171 | iverdef->vd_ndx = freeidx; | |
8172 | iverdef->vd_cnt = 1; | |
252b5132 | 8173 | |
fc0e6df6 | 8174 | iverdef->vd_bfd = abfd; |
252b5132 | 8175 | |
fc0e6df6 PB |
8176 | iverdef->vd_nodename = bfd_elf_get_dt_soname (abfd); |
8177 | if (iverdef->vd_nodename == NULL) | |
d0fb9a8d | 8178 | goto error_return_verdef; |
fc0e6df6 | 8179 | iverdef->vd_nextdef = NULL; |
601a03ba AM |
8180 | iverdef->vd_auxptr = ((struct elf_internal_verdaux *) |
8181 | bfd_zalloc (abfd, sizeof (Elf_Internal_Verdaux))); | |
d0fb9a8d JJ |
8182 | if (iverdef->vd_auxptr == NULL) |
8183 | goto error_return_verdef; | |
252b5132 | 8184 | |
fc0e6df6 PB |
8185 | iverdaux = iverdef->vd_auxptr; |
8186 | iverdaux->vda_nodename = iverdef->vd_nodename; | |
252b5132 RH |
8187 | } |
8188 | ||
b34976b6 | 8189 | return TRUE; |
252b5132 RH |
8190 | |
8191 | error_return: | |
5ed6aba4 | 8192 | if (contents != NULL) |
252b5132 | 8193 | free (contents); |
b34976b6 | 8194 | return FALSE; |
252b5132 RH |
8195 | } |
8196 | \f | |
8197 | asymbol * | |
217aa764 | 8198 | _bfd_elf_make_empty_symbol (bfd *abfd) |
252b5132 RH |
8199 | { |
8200 | elf_symbol_type *newsym; | |
8201 | ||
201159ec | 8202 | newsym = (elf_symbol_type *) bfd_zalloc (abfd, sizeof * newsym); |
252b5132 RH |
8203 | if (!newsym) |
8204 | return NULL; | |
201159ec NC |
8205 | newsym->symbol.the_bfd = abfd; |
8206 | return &newsym->symbol; | |
252b5132 RH |
8207 | } |
8208 | ||
8209 | void | |
217aa764 AM |
8210 | _bfd_elf_get_symbol_info (bfd *abfd ATTRIBUTE_UNUSED, |
8211 | asymbol *symbol, | |
8212 | symbol_info *ret) | |
252b5132 RH |
8213 | { |
8214 | bfd_symbol_info (symbol, ret); | |
8215 | } | |
8216 | ||
8217 | /* Return whether a symbol name implies a local symbol. Most targets | |
8218 | use this function for the is_local_label_name entry point, but some | |
8219 | override it. */ | |
8220 | ||
b34976b6 | 8221 | bfd_boolean |
217aa764 AM |
8222 | _bfd_elf_is_local_label_name (bfd *abfd ATTRIBUTE_UNUSED, |
8223 | const char *name) | |
252b5132 RH |
8224 | { |
8225 | /* Normal local symbols start with ``.L''. */ | |
8226 | if (name[0] == '.' && name[1] == 'L') | |
b34976b6 | 8227 | return TRUE; |
252b5132 RH |
8228 | |
8229 | /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate | |
8230 | DWARF debugging symbols starting with ``..''. */ | |
8231 | if (name[0] == '.' && name[1] == '.') | |
b34976b6 | 8232 | return TRUE; |
252b5132 RH |
8233 | |
8234 | /* gcc will sometimes generate symbols beginning with ``_.L_'' when | |
8235 | emitting DWARF debugging output. I suspect this is actually a | |
8236 | small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call | |
8237 | ASM_GENERATE_INTERNAL_LABEL, and this causes the leading | |
8238 | underscore to be emitted on some ELF targets). For ease of use, | |
8239 | we treat such symbols as local. */ | |
8240 | if (name[0] == '_' && name[1] == '.' && name[2] == 'L' && name[3] == '_') | |
b34976b6 | 8241 | return TRUE; |
252b5132 | 8242 | |
b1fa9dd6 NC |
8243 | /* Treat assembler generated fake symbols, dollar local labels and |
8244 | forward-backward labels (aka local labels) as locals. | |
8245 | These labels have the form: | |
8246 | ||
8247 | L0^A.* (fake symbols) | |
8248 | ||
8249 | [.]?L[0123456789]+{^A|^B}[0123456789]* (local labels) | |
8250 | ||
8251 | Versions which start with .L will have already been matched above, | |
8252 | so we only need to match the rest. */ | |
8253 | if (name[0] == 'L' && ISDIGIT (name[1])) | |
8254 | { | |
8255 | bfd_boolean ret = FALSE; | |
8256 | const char * p; | |
8257 | char c; | |
8258 | ||
8259 | for (p = name + 2; (c = *p); p++) | |
8260 | { | |
8261 | if (c == 1 || c == 2) | |
8262 | { | |
8263 | if (c == 1 && p == name + 2) | |
8264 | /* A fake symbol. */ | |
8265 | return TRUE; | |
8266 | ||
8267 | /* FIXME: We are being paranoid here and treating symbols like | |
8268 | L0^Bfoo as if there were non-local, on the grounds that the | |
8269 | assembler will never generate them. But can any symbol | |
8270 | containing an ASCII value in the range 1-31 ever be anything | |
8271 | other than some kind of local ? */ | |
8272 | ret = TRUE; | |
8273 | } | |
8274 | ||
8275 | if (! ISDIGIT (c)) | |
8276 | { | |
8277 | ret = FALSE; | |
8278 | break; | |
8279 | } | |
8280 | } | |
8281 | return ret; | |
8282 | } | |
ffa54770 | 8283 | |
b34976b6 | 8284 | return FALSE; |
252b5132 RH |
8285 | } |
8286 | ||
8287 | alent * | |
217aa764 AM |
8288 | _bfd_elf_get_lineno (bfd *abfd ATTRIBUTE_UNUSED, |
8289 | asymbol *symbol ATTRIBUTE_UNUSED) | |
252b5132 RH |
8290 | { |
8291 | abort (); | |
8292 | return NULL; | |
8293 | } | |
8294 | ||
b34976b6 | 8295 | bfd_boolean |
217aa764 AM |
8296 | _bfd_elf_set_arch_mach (bfd *abfd, |
8297 | enum bfd_architecture arch, | |
8298 | unsigned long machine) | |
252b5132 RH |
8299 | { |
8300 | /* If this isn't the right architecture for this backend, and this | |
8301 | isn't the generic backend, fail. */ | |
8302 | if (arch != get_elf_backend_data (abfd)->arch | |
8303 | && arch != bfd_arch_unknown | |
8304 | && get_elf_backend_data (abfd)->arch != bfd_arch_unknown) | |
b34976b6 | 8305 | return FALSE; |
252b5132 RH |
8306 | |
8307 | return bfd_default_set_arch_mach (abfd, arch, machine); | |
8308 | } | |
8309 | ||
d1fad7c6 NC |
8310 | /* Find the nearest line to a particular section and offset, |
8311 | for error reporting. */ | |
8312 | ||
b34976b6 | 8313 | bfd_boolean |
217aa764 | 8314 | _bfd_elf_find_nearest_line (bfd *abfd, |
217aa764 | 8315 | asymbol **symbols, |
fb167eb2 | 8316 | asection *section, |
217aa764 AM |
8317 | bfd_vma offset, |
8318 | const char **filename_ptr, | |
8319 | const char **functionname_ptr, | |
fb167eb2 AM |
8320 | unsigned int *line_ptr, |
8321 | unsigned int *discriminator_ptr) | |
d1fad7c6 | 8322 | { |
b34976b6 | 8323 | bfd_boolean found; |
d1fad7c6 | 8324 | |
fb167eb2 | 8325 | if (_bfd_dwarf2_find_nearest_line (abfd, symbols, NULL, section, offset, |
4e8a9624 | 8326 | filename_ptr, functionname_ptr, |
fb167eb2 AM |
8327 | line_ptr, discriminator_ptr, |
8328 | dwarf_debug_sections, 0, | |
e00e8198 AM |
8329 | &elf_tdata (abfd)->dwarf2_find_line_info) |
8330 | || _bfd_dwarf1_find_nearest_line (abfd, symbols, section, offset, | |
8331 | filename_ptr, functionname_ptr, | |
8332 | line_ptr)) | |
d1fad7c6 NC |
8333 | { |
8334 | if (!*functionname_ptr) | |
e00e8198 AM |
8335 | _bfd_elf_find_function (abfd, symbols, section, offset, |
8336 | *filename_ptr ? NULL : filename_ptr, | |
8337 | functionname_ptr); | |
b34976b6 | 8338 | return TRUE; |
d1fad7c6 NC |
8339 | } |
8340 | ||
8341 | if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset, | |
4e8a9624 AM |
8342 | &found, filename_ptr, |
8343 | functionname_ptr, line_ptr, | |
8344 | &elf_tdata (abfd)->line_info)) | |
b34976b6 | 8345 | return FALSE; |
dc43ada5 | 8346 | if (found && (*functionname_ptr || *line_ptr)) |
b34976b6 | 8347 | return TRUE; |
d1fad7c6 NC |
8348 | |
8349 | if (symbols == NULL) | |
b34976b6 | 8350 | return FALSE; |
d1fad7c6 | 8351 | |
e00e8198 AM |
8352 | if (! _bfd_elf_find_function (abfd, symbols, section, offset, |
8353 | filename_ptr, functionname_ptr)) | |
b34976b6 | 8354 | return FALSE; |
d1fad7c6 | 8355 | |
252b5132 | 8356 | *line_ptr = 0; |
b34976b6 | 8357 | return TRUE; |
252b5132 RH |
8358 | } |
8359 | ||
5420f73d L |
8360 | /* Find the line for a symbol. */ |
8361 | ||
8362 | bfd_boolean | |
8363 | _bfd_elf_find_line (bfd *abfd, asymbol **symbols, asymbol *symbol, | |
8364 | const char **filename_ptr, unsigned int *line_ptr) | |
9b8d1a36 | 8365 | { |
fb167eb2 AM |
8366 | return _bfd_dwarf2_find_nearest_line (abfd, symbols, symbol, NULL, 0, |
8367 | filename_ptr, NULL, line_ptr, NULL, | |
8368 | dwarf_debug_sections, 0, | |
8369 | &elf_tdata (abfd)->dwarf2_find_line_info); | |
5420f73d L |
8370 | } |
8371 | ||
4ab527b0 FF |
8372 | /* After a call to bfd_find_nearest_line, successive calls to |
8373 | bfd_find_inliner_info can be used to get source information about | |
8374 | each level of function inlining that terminated at the address | |
8375 | passed to bfd_find_nearest_line. Currently this is only supported | |
8376 | for DWARF2 with appropriate DWARF3 extensions. */ | |
8377 | ||
8378 | bfd_boolean | |
8379 | _bfd_elf_find_inliner_info (bfd *abfd, | |
8380 | const char **filename_ptr, | |
8381 | const char **functionname_ptr, | |
8382 | unsigned int *line_ptr) | |
8383 | { | |
8384 | bfd_boolean found; | |
8385 | found = _bfd_dwarf2_find_inliner_info (abfd, filename_ptr, | |
8386 | functionname_ptr, line_ptr, | |
8387 | & elf_tdata (abfd)->dwarf2_find_line_info); | |
8388 | return found; | |
8389 | } | |
8390 | ||
252b5132 | 8391 | int |
a6b96beb | 8392 | _bfd_elf_sizeof_headers (bfd *abfd, struct bfd_link_info *info) |
252b5132 | 8393 | { |
8ded5a0f AM |
8394 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
8395 | int ret = bed->s->sizeof_ehdr; | |
252b5132 | 8396 | |
0e1862bb | 8397 | if (!bfd_link_relocatable (info)) |
8ded5a0f | 8398 | { |
12bd6957 | 8399 | bfd_size_type phdr_size = elf_program_header_size (abfd); |
8ded5a0f | 8400 | |
62d7a5f6 AM |
8401 | if (phdr_size == (bfd_size_type) -1) |
8402 | { | |
8403 | struct elf_segment_map *m; | |
8404 | ||
8405 | phdr_size = 0; | |
12bd6957 | 8406 | for (m = elf_seg_map (abfd); m != NULL; m = m->next) |
62d7a5f6 | 8407 | phdr_size += bed->s->sizeof_phdr; |
8ded5a0f | 8408 | |
62d7a5f6 AM |
8409 | if (phdr_size == 0) |
8410 | phdr_size = get_program_header_size (abfd, info); | |
8411 | } | |
8ded5a0f | 8412 | |
12bd6957 | 8413 | elf_program_header_size (abfd) = phdr_size; |
8ded5a0f AM |
8414 | ret += phdr_size; |
8415 | } | |
8416 | ||
252b5132 RH |
8417 | return ret; |
8418 | } | |
8419 | ||
b34976b6 | 8420 | bfd_boolean |
217aa764 AM |
8421 | _bfd_elf_set_section_contents (bfd *abfd, |
8422 | sec_ptr section, | |
0f867abe | 8423 | const void *location, |
217aa764 AM |
8424 | file_ptr offset, |
8425 | bfd_size_type count) | |
252b5132 RH |
8426 | { |
8427 | Elf_Internal_Shdr *hdr; | |
1b6aeedb | 8428 | file_ptr pos; |
252b5132 RH |
8429 | |
8430 | if (! abfd->output_has_begun | |
217aa764 | 8431 | && ! _bfd_elf_compute_section_file_positions (abfd, NULL)) |
b34976b6 | 8432 | return FALSE; |
252b5132 | 8433 | |
0ce398f1 L |
8434 | if (!count) |
8435 | return TRUE; | |
8436 | ||
252b5132 | 8437 | hdr = &elf_section_data (section)->this_hdr; |
0ce398f1 L |
8438 | if (hdr->sh_offset == (file_ptr) -1) |
8439 | { | |
8440 | /* We must compress this section. Write output to the buffer. */ | |
8441 | unsigned char *contents = hdr->contents; | |
8442 | if ((offset + count) > hdr->sh_size | |
8443 | || (section->flags & SEC_ELF_COMPRESS) == 0 | |
8444 | || contents == NULL) | |
8445 | abort (); | |
8446 | memcpy (contents + offset, location, count); | |
8447 | return TRUE; | |
8448 | } | |
dc810e39 AM |
8449 | pos = hdr->sh_offset + offset; |
8450 | if (bfd_seek (abfd, pos, SEEK_SET) != 0 | |
8451 | || bfd_bwrite (location, count, abfd) != count) | |
b34976b6 | 8452 | return FALSE; |
252b5132 | 8453 | |
b34976b6 | 8454 | return TRUE; |
252b5132 RH |
8455 | } |
8456 | ||
8457 | void | |
217aa764 AM |
8458 | _bfd_elf_no_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED, |
8459 | arelent *cache_ptr ATTRIBUTE_UNUSED, | |
8460 | Elf_Internal_Rela *dst ATTRIBUTE_UNUSED) | |
252b5132 RH |
8461 | { |
8462 | abort (); | |
8463 | } | |
8464 | ||
252b5132 RH |
8465 | /* Try to convert a non-ELF reloc into an ELF one. */ |
8466 | ||
b34976b6 | 8467 | bfd_boolean |
217aa764 | 8468 | _bfd_elf_validate_reloc (bfd *abfd, arelent *areloc) |
252b5132 | 8469 | { |
c044fabd | 8470 | /* Check whether we really have an ELF howto. */ |
252b5132 RH |
8471 | |
8472 | if ((*areloc->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec) | |
8473 | { | |
8474 | bfd_reloc_code_real_type code; | |
8475 | reloc_howto_type *howto; | |
8476 | ||
8477 | /* Alien reloc: Try to determine its type to replace it with an | |
c044fabd | 8478 | equivalent ELF reloc. */ |
252b5132 RH |
8479 | |
8480 | if (areloc->howto->pc_relative) | |
8481 | { | |
8482 | switch (areloc->howto->bitsize) | |
8483 | { | |
8484 | case 8: | |
8485 | code = BFD_RELOC_8_PCREL; | |
8486 | break; | |
8487 | case 12: | |
8488 | code = BFD_RELOC_12_PCREL; | |
8489 | break; | |
8490 | case 16: | |
8491 | code = BFD_RELOC_16_PCREL; | |
8492 | break; | |
8493 | case 24: | |
8494 | code = BFD_RELOC_24_PCREL; | |
8495 | break; | |
8496 | case 32: | |
8497 | code = BFD_RELOC_32_PCREL; | |
8498 | break; | |
8499 | case 64: | |
8500 | code = BFD_RELOC_64_PCREL; | |
8501 | break; | |
8502 | default: | |
8503 | goto fail; | |
8504 | } | |
8505 | ||
8506 | howto = bfd_reloc_type_lookup (abfd, code); | |
8507 | ||
8508 | if (areloc->howto->pcrel_offset != howto->pcrel_offset) | |
8509 | { | |
8510 | if (howto->pcrel_offset) | |
8511 | areloc->addend += areloc->address; | |
8512 | else | |
8513 | areloc->addend -= areloc->address; /* addend is unsigned!! */ | |
8514 | } | |
8515 | } | |
8516 | else | |
8517 | { | |
8518 | switch (areloc->howto->bitsize) | |
8519 | { | |
8520 | case 8: | |
8521 | code = BFD_RELOC_8; | |
8522 | break; | |
8523 | case 14: | |
8524 | code = BFD_RELOC_14; | |
8525 | break; | |
8526 | case 16: | |
8527 | code = BFD_RELOC_16; | |
8528 | break; | |
8529 | case 26: | |
8530 | code = BFD_RELOC_26; | |
8531 | break; | |
8532 | case 32: | |
8533 | code = BFD_RELOC_32; | |
8534 | break; | |
8535 | case 64: | |
8536 | code = BFD_RELOC_64; | |
8537 | break; | |
8538 | default: | |
8539 | goto fail; | |
8540 | } | |
8541 | ||
8542 | howto = bfd_reloc_type_lookup (abfd, code); | |
8543 | } | |
8544 | ||
8545 | if (howto) | |
8546 | areloc->howto = howto; | |
8547 | else | |
8548 | goto fail; | |
8549 | } | |
8550 | ||
b34976b6 | 8551 | return TRUE; |
252b5132 RH |
8552 | |
8553 | fail: | |
8554 | (*_bfd_error_handler) | |
d003868e AM |
8555 | (_("%B: unsupported relocation type %s"), |
8556 | abfd, areloc->howto->name); | |
252b5132 | 8557 | bfd_set_error (bfd_error_bad_value); |
b34976b6 | 8558 | return FALSE; |
252b5132 RH |
8559 | } |
8560 | ||
b34976b6 | 8561 | bfd_boolean |
217aa764 | 8562 | _bfd_elf_close_and_cleanup (bfd *abfd) |
252b5132 | 8563 | { |
d9071b0c TG |
8564 | struct elf_obj_tdata *tdata = elf_tdata (abfd); |
8565 | if (bfd_get_format (abfd) == bfd_object && tdata != NULL) | |
252b5132 | 8566 | { |
c0355132 | 8567 | if (elf_tdata (abfd)->o != NULL && elf_shstrtab (abfd) != NULL) |
2b0f7ef9 | 8568 | _bfd_elf_strtab_free (elf_shstrtab (abfd)); |
d9071b0c | 8569 | _bfd_dwarf2_cleanup_debug_info (abfd, &tdata->dwarf2_find_line_info); |
252b5132 RH |
8570 | } |
8571 | ||
8572 | return _bfd_generic_close_and_cleanup (abfd); | |
8573 | } | |
8574 | ||
8575 | /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY | |
8576 | in the relocation's offset. Thus we cannot allow any sort of sanity | |
8577 | range-checking to interfere. There is nothing else to do in processing | |
8578 | this reloc. */ | |
8579 | ||
8580 | bfd_reloc_status_type | |
217aa764 AM |
8581 | _bfd_elf_rel_vtable_reloc_fn |
8582 | (bfd *abfd ATTRIBUTE_UNUSED, arelent *re ATTRIBUTE_UNUSED, | |
fc0a2244 | 8583 | struct bfd_symbol *symbol ATTRIBUTE_UNUSED, |
217aa764 AM |
8584 | void *data ATTRIBUTE_UNUSED, asection *is ATTRIBUTE_UNUSED, |
8585 | bfd *obfd ATTRIBUTE_UNUSED, char **errmsg ATTRIBUTE_UNUSED) | |
252b5132 RH |
8586 | { |
8587 | return bfd_reloc_ok; | |
8588 | } | |
252b5132 RH |
8589 | \f |
8590 | /* Elf core file support. Much of this only works on native | |
8591 | toolchains, since we rely on knowing the | |
8592 | machine-dependent procfs structure in order to pick | |
c044fabd | 8593 | out details about the corefile. */ |
252b5132 RH |
8594 | |
8595 | #ifdef HAVE_SYS_PROCFS_H | |
16231b7b DG |
8596 | /* Needed for new procfs interface on sparc-solaris. */ |
8597 | # define _STRUCTURED_PROC 1 | |
252b5132 RH |
8598 | # include <sys/procfs.h> |
8599 | #endif | |
8600 | ||
261b8d08 PA |
8601 | /* Return a PID that identifies a "thread" for threaded cores, or the |
8602 | PID of the main process for non-threaded cores. */ | |
252b5132 RH |
8603 | |
8604 | static int | |
217aa764 | 8605 | elfcore_make_pid (bfd *abfd) |
252b5132 | 8606 | { |
261b8d08 PA |
8607 | int pid; |
8608 | ||
228e534f | 8609 | pid = elf_tdata (abfd)->core->lwpid; |
261b8d08 | 8610 | if (pid == 0) |
228e534f | 8611 | pid = elf_tdata (abfd)->core->pid; |
261b8d08 PA |
8612 | |
8613 | return pid; | |
252b5132 RH |
8614 | } |
8615 | ||
252b5132 RH |
8616 | /* If there isn't a section called NAME, make one, using |
8617 | data from SECT. Note, this function will generate a | |
8618 | reference to NAME, so you shouldn't deallocate or | |
c044fabd | 8619 | overwrite it. */ |
252b5132 | 8620 | |
b34976b6 | 8621 | static bfd_boolean |
217aa764 | 8622 | elfcore_maybe_make_sect (bfd *abfd, char *name, asection *sect) |
252b5132 | 8623 | { |
c044fabd | 8624 | asection *sect2; |
252b5132 RH |
8625 | |
8626 | if (bfd_get_section_by_name (abfd, name) != NULL) | |
b34976b6 | 8627 | return TRUE; |
252b5132 | 8628 | |
117ed4f8 | 8629 | sect2 = bfd_make_section_with_flags (abfd, name, sect->flags); |
252b5132 | 8630 | if (sect2 == NULL) |
b34976b6 | 8631 | return FALSE; |
252b5132 | 8632 | |
eea6121a | 8633 | sect2->size = sect->size; |
252b5132 | 8634 | sect2->filepos = sect->filepos; |
252b5132 | 8635 | sect2->alignment_power = sect->alignment_power; |
b34976b6 | 8636 | return TRUE; |
252b5132 RH |
8637 | } |
8638 | ||
bb0082d6 AM |
8639 | /* Create a pseudosection containing SIZE bytes at FILEPOS. This |
8640 | actually creates up to two pseudosections: | |
8641 | - For the single-threaded case, a section named NAME, unless | |
8642 | such a section already exists. | |
8643 | - For the multi-threaded case, a section named "NAME/PID", where | |
8644 | PID is elfcore_make_pid (abfd). | |
8645 | Both pseudosections have identical contents. */ | |
b34976b6 | 8646 | bfd_boolean |
217aa764 AM |
8647 | _bfd_elfcore_make_pseudosection (bfd *abfd, |
8648 | char *name, | |
8649 | size_t size, | |
8650 | ufile_ptr filepos) | |
bb0082d6 AM |
8651 | { |
8652 | char buf[100]; | |
8653 | char *threaded_name; | |
d4c88bbb | 8654 | size_t len; |
bb0082d6 AM |
8655 | asection *sect; |
8656 | ||
8657 | /* Build the section name. */ | |
8658 | ||
8659 | sprintf (buf, "%s/%d", name, elfcore_make_pid (abfd)); | |
d4c88bbb | 8660 | len = strlen (buf) + 1; |
a50b1753 | 8661 | threaded_name = (char *) bfd_alloc (abfd, len); |
bb0082d6 | 8662 | if (threaded_name == NULL) |
b34976b6 | 8663 | return FALSE; |
d4c88bbb | 8664 | memcpy (threaded_name, buf, len); |
bb0082d6 | 8665 | |
117ed4f8 AM |
8666 | sect = bfd_make_section_anyway_with_flags (abfd, threaded_name, |
8667 | SEC_HAS_CONTENTS); | |
bb0082d6 | 8668 | if (sect == NULL) |
b34976b6 | 8669 | return FALSE; |
eea6121a | 8670 | sect->size = size; |
bb0082d6 | 8671 | sect->filepos = filepos; |
bb0082d6 AM |
8672 | sect->alignment_power = 2; |
8673 | ||
936e320b | 8674 | return elfcore_maybe_make_sect (abfd, name, sect); |
bb0082d6 AM |
8675 | } |
8676 | ||
252b5132 | 8677 | /* prstatus_t exists on: |
4a938328 | 8678 | solaris 2.5+ |
252b5132 RH |
8679 | linux 2.[01] + glibc |
8680 | unixware 4.2 | |
8681 | */ | |
8682 | ||
8683 | #if defined (HAVE_PRSTATUS_T) | |
a7b97311 | 8684 | |
b34976b6 | 8685 | static bfd_boolean |
217aa764 | 8686 | elfcore_grok_prstatus (bfd *abfd, Elf_Internal_Note *note) |
252b5132 | 8687 | { |
eea6121a | 8688 | size_t size; |
7ee38065 | 8689 | int offset; |
252b5132 | 8690 | |
4a938328 MS |
8691 | if (note->descsz == sizeof (prstatus_t)) |
8692 | { | |
8693 | prstatus_t prstat; | |
252b5132 | 8694 | |
eea6121a | 8695 | size = sizeof (prstat.pr_reg); |
7ee38065 | 8696 | offset = offsetof (prstatus_t, pr_reg); |
4a938328 | 8697 | memcpy (&prstat, note->descdata, sizeof (prstat)); |
252b5132 | 8698 | |
fa49d224 NC |
8699 | /* Do not overwrite the core signal if it |
8700 | has already been set by another thread. */ | |
228e534f AM |
8701 | if (elf_tdata (abfd)->core->signal == 0) |
8702 | elf_tdata (abfd)->core->signal = prstat.pr_cursig; | |
8703 | if (elf_tdata (abfd)->core->pid == 0) | |
8704 | elf_tdata (abfd)->core->pid = prstat.pr_pid; | |
252b5132 | 8705 | |
4a938328 MS |
8706 | /* pr_who exists on: |
8707 | solaris 2.5+ | |
8708 | unixware 4.2 | |
8709 | pr_who doesn't exist on: | |
8710 | linux 2.[01] | |
8711 | */ | |
252b5132 | 8712 | #if defined (HAVE_PRSTATUS_T_PR_WHO) |
228e534f | 8713 | elf_tdata (abfd)->core->lwpid = prstat.pr_who; |
261b8d08 | 8714 | #else |
228e534f | 8715 | elf_tdata (abfd)->core->lwpid = prstat.pr_pid; |
252b5132 | 8716 | #endif |
4a938328 | 8717 | } |
7ee38065 | 8718 | #if defined (HAVE_PRSTATUS32_T) |
4a938328 MS |
8719 | else if (note->descsz == sizeof (prstatus32_t)) |
8720 | { | |
8721 | /* 64-bit host, 32-bit corefile */ | |
8722 | prstatus32_t prstat; | |
8723 | ||
eea6121a | 8724 | size = sizeof (prstat.pr_reg); |
7ee38065 | 8725 | offset = offsetof (prstatus32_t, pr_reg); |
4a938328 MS |
8726 | memcpy (&prstat, note->descdata, sizeof (prstat)); |
8727 | ||
fa49d224 NC |
8728 | /* Do not overwrite the core signal if it |
8729 | has already been set by another thread. */ | |
228e534f AM |
8730 | if (elf_tdata (abfd)->core->signal == 0) |
8731 | elf_tdata (abfd)->core->signal = prstat.pr_cursig; | |
8732 | if (elf_tdata (abfd)->core->pid == 0) | |
8733 | elf_tdata (abfd)->core->pid = prstat.pr_pid; | |
4a938328 MS |
8734 | |
8735 | /* pr_who exists on: | |
8736 | solaris 2.5+ | |
8737 | unixware 4.2 | |
8738 | pr_who doesn't exist on: | |
8739 | linux 2.[01] | |
8740 | */ | |
7ee38065 | 8741 | #if defined (HAVE_PRSTATUS32_T_PR_WHO) |
228e534f | 8742 | elf_tdata (abfd)->core->lwpid = prstat.pr_who; |
261b8d08 | 8743 | #else |
228e534f | 8744 | elf_tdata (abfd)->core->lwpid = prstat.pr_pid; |
4a938328 MS |
8745 | #endif |
8746 | } | |
7ee38065 | 8747 | #endif /* HAVE_PRSTATUS32_T */ |
4a938328 MS |
8748 | else |
8749 | { | |
8750 | /* Fail - we don't know how to handle any other | |
8751 | note size (ie. data object type). */ | |
b34976b6 | 8752 | return TRUE; |
4a938328 | 8753 | } |
252b5132 | 8754 | |
bb0082d6 | 8755 | /* Make a ".reg/999" section and a ".reg" section. */ |
936e320b | 8756 | return _bfd_elfcore_make_pseudosection (abfd, ".reg", |
eea6121a | 8757 | size, note->descpos + offset); |
252b5132 RH |
8758 | } |
8759 | #endif /* defined (HAVE_PRSTATUS_T) */ | |
8760 | ||
bb0082d6 | 8761 | /* Create a pseudosection containing the exact contents of NOTE. */ |
b34976b6 | 8762 | static bfd_boolean |
217aa764 AM |
8763 | elfcore_make_note_pseudosection (bfd *abfd, |
8764 | char *name, | |
8765 | Elf_Internal_Note *note) | |
252b5132 | 8766 | { |
936e320b AM |
8767 | return _bfd_elfcore_make_pseudosection (abfd, name, |
8768 | note->descsz, note->descpos); | |
252b5132 RH |
8769 | } |
8770 | ||
ff08c6bb JB |
8771 | /* There isn't a consistent prfpregset_t across platforms, |
8772 | but it doesn't matter, because we don't have to pick this | |
c044fabd KH |
8773 | data structure apart. */ |
8774 | ||
b34976b6 | 8775 | static bfd_boolean |
217aa764 | 8776 | elfcore_grok_prfpreg (bfd *abfd, Elf_Internal_Note *note) |
ff08c6bb JB |
8777 | { |
8778 | return elfcore_make_note_pseudosection (abfd, ".reg2", note); | |
8779 | } | |
8780 | ||
ff08c6bb | 8781 | /* Linux dumps the Intel SSE regs in a note named "LINUX" with a note |
971d4640 | 8782 | type of NT_PRXFPREG. Just include the whole note's contents |
ff08c6bb | 8783 | literally. */ |
c044fabd | 8784 | |
b34976b6 | 8785 | static bfd_boolean |
217aa764 | 8786 | elfcore_grok_prxfpreg (bfd *abfd, Elf_Internal_Note *note) |
ff08c6bb JB |
8787 | { |
8788 | return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note); | |
8789 | } | |
8790 | ||
4339cae0 L |
8791 | /* Linux dumps the Intel XSAVE extended state in a note named "LINUX" |
8792 | with a note type of NT_X86_XSTATE. Just include the whole note's | |
8793 | contents literally. */ | |
8794 | ||
8795 | static bfd_boolean | |
8796 | elfcore_grok_xstatereg (bfd *abfd, Elf_Internal_Note *note) | |
8797 | { | |
8798 | return elfcore_make_note_pseudosection (abfd, ".reg-xstate", note); | |
8799 | } | |
8800 | ||
97753bd5 AM |
8801 | static bfd_boolean |
8802 | elfcore_grok_ppc_vmx (bfd *abfd, Elf_Internal_Note *note) | |
8803 | { | |
8804 | return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vmx", note); | |
8805 | } | |
8806 | ||
89eeb0bc LM |
8807 | static bfd_boolean |
8808 | elfcore_grok_ppc_vsx (bfd *abfd, Elf_Internal_Note *note) | |
8809 | { | |
8810 | return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vsx", note); | |
8811 | } | |
97753bd5 | 8812 | |
0675e188 UW |
8813 | static bfd_boolean |
8814 | elfcore_grok_s390_high_gprs (bfd *abfd, Elf_Internal_Note *note) | |
8815 | { | |
8816 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-high-gprs", note); | |
8817 | } | |
8818 | ||
d7eeb400 MS |
8819 | static bfd_boolean |
8820 | elfcore_grok_s390_timer (bfd *abfd, Elf_Internal_Note *note) | |
8821 | { | |
8822 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-timer", note); | |
8823 | } | |
8824 | ||
8825 | static bfd_boolean | |
8826 | elfcore_grok_s390_todcmp (bfd *abfd, Elf_Internal_Note *note) | |
8827 | { | |
8828 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-todcmp", note); | |
8829 | } | |
8830 | ||
8831 | static bfd_boolean | |
8832 | elfcore_grok_s390_todpreg (bfd *abfd, Elf_Internal_Note *note) | |
8833 | { | |
8834 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-todpreg", note); | |
8835 | } | |
8836 | ||
8837 | static bfd_boolean | |
8838 | elfcore_grok_s390_ctrs (bfd *abfd, Elf_Internal_Note *note) | |
8839 | { | |
8840 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-ctrs", note); | |
8841 | } | |
8842 | ||
8843 | static bfd_boolean | |
8844 | elfcore_grok_s390_prefix (bfd *abfd, Elf_Internal_Note *note) | |
8845 | { | |
8846 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-prefix", note); | |
8847 | } | |
8848 | ||
355b81d9 UW |
8849 | static bfd_boolean |
8850 | elfcore_grok_s390_last_break (bfd *abfd, Elf_Internal_Note *note) | |
8851 | { | |
8852 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-last-break", note); | |
8853 | } | |
8854 | ||
8855 | static bfd_boolean | |
8856 | elfcore_grok_s390_system_call (bfd *abfd, Elf_Internal_Note *note) | |
8857 | { | |
8858 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-system-call", note); | |
8859 | } | |
8860 | ||
abb3f6cc NC |
8861 | static bfd_boolean |
8862 | elfcore_grok_s390_tdb (bfd *abfd, Elf_Internal_Note *note) | |
8863 | { | |
8864 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-tdb", note); | |
8865 | } | |
8866 | ||
4ef9f41a AA |
8867 | static bfd_boolean |
8868 | elfcore_grok_s390_vxrs_low (bfd *abfd, Elf_Internal_Note *note) | |
8869 | { | |
8870 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-vxrs-low", note); | |
8871 | } | |
8872 | ||
8873 | static bfd_boolean | |
8874 | elfcore_grok_s390_vxrs_high (bfd *abfd, Elf_Internal_Note *note) | |
8875 | { | |
8876 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-vxrs-high", note); | |
8877 | } | |
8878 | ||
faa9a424 UW |
8879 | static bfd_boolean |
8880 | elfcore_grok_arm_vfp (bfd *abfd, Elf_Internal_Note *note) | |
8881 | { | |
8882 | return elfcore_make_note_pseudosection (abfd, ".reg-arm-vfp", note); | |
8883 | } | |
8884 | ||
652451f8 YZ |
8885 | static bfd_boolean |
8886 | elfcore_grok_aarch_tls (bfd *abfd, Elf_Internal_Note *note) | |
8887 | { | |
8888 | return elfcore_make_note_pseudosection (abfd, ".reg-aarch-tls", note); | |
8889 | } | |
8890 | ||
8891 | static bfd_boolean | |
8892 | elfcore_grok_aarch_hw_break (bfd *abfd, Elf_Internal_Note *note) | |
8893 | { | |
8894 | return elfcore_make_note_pseudosection (abfd, ".reg-aarch-hw-break", note); | |
8895 | } | |
8896 | ||
8897 | static bfd_boolean | |
8898 | elfcore_grok_aarch_hw_watch (bfd *abfd, Elf_Internal_Note *note) | |
8899 | { | |
8900 | return elfcore_make_note_pseudosection (abfd, ".reg-aarch-hw-watch", note); | |
8901 | } | |
8902 | ||
252b5132 | 8903 | #if defined (HAVE_PRPSINFO_T) |
4a938328 | 8904 | typedef prpsinfo_t elfcore_psinfo_t; |
7ee38065 | 8905 | #if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */ |
4a938328 MS |
8906 | typedef prpsinfo32_t elfcore_psinfo32_t; |
8907 | #endif | |
252b5132 RH |
8908 | #endif |
8909 | ||
8910 | #if defined (HAVE_PSINFO_T) | |
4a938328 | 8911 | typedef psinfo_t elfcore_psinfo_t; |
7ee38065 | 8912 | #if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */ |
4a938328 MS |
8913 | typedef psinfo32_t elfcore_psinfo32_t; |
8914 | #endif | |
252b5132 RH |
8915 | #endif |
8916 | ||
252b5132 RH |
8917 | /* return a malloc'ed copy of a string at START which is at |
8918 | most MAX bytes long, possibly without a terminating '\0'. | |
c044fabd | 8919 | the copy will always have a terminating '\0'. */ |
252b5132 | 8920 | |
936e320b | 8921 | char * |
217aa764 | 8922 | _bfd_elfcore_strndup (bfd *abfd, char *start, size_t max) |
252b5132 | 8923 | { |
dc810e39 | 8924 | char *dups; |
a50b1753 | 8925 | char *end = (char *) memchr (start, '\0', max); |
dc810e39 | 8926 | size_t len; |
252b5132 RH |
8927 | |
8928 | if (end == NULL) | |
8929 | len = max; | |
8930 | else | |
8931 | len = end - start; | |
8932 | ||
a50b1753 | 8933 | dups = (char *) bfd_alloc (abfd, len + 1); |
dc810e39 | 8934 | if (dups == NULL) |
252b5132 RH |
8935 | return NULL; |
8936 | ||
dc810e39 AM |
8937 | memcpy (dups, start, len); |
8938 | dups[len] = '\0'; | |
252b5132 | 8939 | |
dc810e39 | 8940 | return dups; |
252b5132 RH |
8941 | } |
8942 | ||
bb0082d6 | 8943 | #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) |
b34976b6 | 8944 | static bfd_boolean |
217aa764 | 8945 | elfcore_grok_psinfo (bfd *abfd, Elf_Internal_Note *note) |
252b5132 | 8946 | { |
4a938328 MS |
8947 | if (note->descsz == sizeof (elfcore_psinfo_t)) |
8948 | { | |
8949 | elfcore_psinfo_t psinfo; | |
252b5132 | 8950 | |
7ee38065 | 8951 | memcpy (&psinfo, note->descdata, sizeof (psinfo)); |
252b5132 | 8952 | |
335e41d4 | 8953 | #if defined (HAVE_PSINFO_T_PR_PID) || defined (HAVE_PRPSINFO_T_PR_PID) |
228e534f | 8954 | elf_tdata (abfd)->core->pid = psinfo.pr_pid; |
335e41d4 | 8955 | #endif |
228e534f | 8956 | elf_tdata (abfd)->core->program |
936e320b AM |
8957 | = _bfd_elfcore_strndup (abfd, psinfo.pr_fname, |
8958 | sizeof (psinfo.pr_fname)); | |
252b5132 | 8959 | |
228e534f | 8960 | elf_tdata (abfd)->core->command |
936e320b AM |
8961 | = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs, |
8962 | sizeof (psinfo.pr_psargs)); | |
4a938328 | 8963 | } |
7ee38065 | 8964 | #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T) |
4a938328 MS |
8965 | else if (note->descsz == sizeof (elfcore_psinfo32_t)) |
8966 | { | |
8967 | /* 64-bit host, 32-bit corefile */ | |
8968 | elfcore_psinfo32_t psinfo; | |
8969 | ||
7ee38065 | 8970 | memcpy (&psinfo, note->descdata, sizeof (psinfo)); |
252b5132 | 8971 | |
335e41d4 | 8972 | #if defined (HAVE_PSINFO32_T_PR_PID) || defined (HAVE_PRPSINFO32_T_PR_PID) |
228e534f | 8973 | elf_tdata (abfd)->core->pid = psinfo.pr_pid; |
335e41d4 | 8974 | #endif |
228e534f | 8975 | elf_tdata (abfd)->core->program |
936e320b AM |
8976 | = _bfd_elfcore_strndup (abfd, psinfo.pr_fname, |
8977 | sizeof (psinfo.pr_fname)); | |
4a938328 | 8978 | |
228e534f | 8979 | elf_tdata (abfd)->core->command |
936e320b AM |
8980 | = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs, |
8981 | sizeof (psinfo.pr_psargs)); | |
4a938328 MS |
8982 | } |
8983 | #endif | |
8984 | ||
8985 | else | |
8986 | { | |
8987 | /* Fail - we don't know how to handle any other | |
8988 | note size (ie. data object type). */ | |
b34976b6 | 8989 | return TRUE; |
4a938328 | 8990 | } |
252b5132 RH |
8991 | |
8992 | /* Note that for some reason, a spurious space is tacked | |
8993 | onto the end of the args in some (at least one anyway) | |
c044fabd | 8994 | implementations, so strip it off if it exists. */ |
252b5132 RH |
8995 | |
8996 | { | |
228e534f | 8997 | char *command = elf_tdata (abfd)->core->command; |
252b5132 RH |
8998 | int n = strlen (command); |
8999 | ||
9000 | if (0 < n && command[n - 1] == ' ') | |
9001 | command[n - 1] = '\0'; | |
9002 | } | |
9003 | ||
b34976b6 | 9004 | return TRUE; |
252b5132 RH |
9005 | } |
9006 | #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */ | |
9007 | ||
252b5132 | 9008 | #if defined (HAVE_PSTATUS_T) |
b34976b6 | 9009 | static bfd_boolean |
217aa764 | 9010 | elfcore_grok_pstatus (bfd *abfd, Elf_Internal_Note *note) |
252b5132 | 9011 | { |
f572a39d AM |
9012 | if (note->descsz == sizeof (pstatus_t) |
9013 | #if defined (HAVE_PXSTATUS_T) | |
9014 | || note->descsz == sizeof (pxstatus_t) | |
9015 | #endif | |
9016 | ) | |
4a938328 MS |
9017 | { |
9018 | pstatus_t pstat; | |
252b5132 | 9019 | |
4a938328 | 9020 | memcpy (&pstat, note->descdata, sizeof (pstat)); |
252b5132 | 9021 | |
228e534f | 9022 | elf_tdata (abfd)->core->pid = pstat.pr_pid; |
4a938328 | 9023 | } |
7ee38065 | 9024 | #if defined (HAVE_PSTATUS32_T) |
4a938328 MS |
9025 | else if (note->descsz == sizeof (pstatus32_t)) |
9026 | { | |
9027 | /* 64-bit host, 32-bit corefile */ | |
9028 | pstatus32_t pstat; | |
252b5132 | 9029 | |
4a938328 | 9030 | memcpy (&pstat, note->descdata, sizeof (pstat)); |
252b5132 | 9031 | |
228e534f | 9032 | elf_tdata (abfd)->core->pid = pstat.pr_pid; |
4a938328 MS |
9033 | } |
9034 | #endif | |
252b5132 RH |
9035 | /* Could grab some more details from the "representative" |
9036 | lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an | |
c044fabd | 9037 | NT_LWPSTATUS note, presumably. */ |
252b5132 | 9038 | |
b34976b6 | 9039 | return TRUE; |
252b5132 RH |
9040 | } |
9041 | #endif /* defined (HAVE_PSTATUS_T) */ | |
9042 | ||
252b5132 | 9043 | #if defined (HAVE_LWPSTATUS_T) |
b34976b6 | 9044 | static bfd_boolean |
217aa764 | 9045 | elfcore_grok_lwpstatus (bfd *abfd, Elf_Internal_Note *note) |
252b5132 RH |
9046 | { |
9047 | lwpstatus_t lwpstat; | |
9048 | char buf[100]; | |
c044fabd | 9049 | char *name; |
d4c88bbb | 9050 | size_t len; |
c044fabd | 9051 | asection *sect; |
252b5132 | 9052 | |
f572a39d AM |
9053 | if (note->descsz != sizeof (lwpstat) |
9054 | #if defined (HAVE_LWPXSTATUS_T) | |
9055 | && note->descsz != sizeof (lwpxstatus_t) | |
9056 | #endif | |
9057 | ) | |
b34976b6 | 9058 | return TRUE; |
252b5132 RH |
9059 | |
9060 | memcpy (&lwpstat, note->descdata, sizeof (lwpstat)); | |
9061 | ||
228e534f | 9062 | elf_tdata (abfd)->core->lwpid = lwpstat.pr_lwpid; |
a1504221 JB |
9063 | /* Do not overwrite the core signal if it has already been set by |
9064 | another thread. */ | |
228e534f AM |
9065 | if (elf_tdata (abfd)->core->signal == 0) |
9066 | elf_tdata (abfd)->core->signal = lwpstat.pr_cursig; | |
252b5132 | 9067 | |
c044fabd | 9068 | /* Make a ".reg/999" section. */ |
252b5132 RH |
9069 | |
9070 | sprintf (buf, ".reg/%d", elfcore_make_pid (abfd)); | |
d4c88bbb | 9071 | len = strlen (buf) + 1; |
217aa764 | 9072 | name = bfd_alloc (abfd, len); |
252b5132 | 9073 | if (name == NULL) |
b34976b6 | 9074 | return FALSE; |
d4c88bbb | 9075 | memcpy (name, buf, len); |
252b5132 | 9076 | |
117ed4f8 | 9077 | sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); |
252b5132 | 9078 | if (sect == NULL) |
b34976b6 | 9079 | return FALSE; |
252b5132 RH |
9080 | |
9081 | #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT) | |
eea6121a | 9082 | sect->size = sizeof (lwpstat.pr_context.uc_mcontext.gregs); |
252b5132 RH |
9083 | sect->filepos = note->descpos |
9084 | + offsetof (lwpstatus_t, pr_context.uc_mcontext.gregs); | |
9085 | #endif | |
9086 | ||
9087 | #if defined (HAVE_LWPSTATUS_T_PR_REG) | |
eea6121a | 9088 | sect->size = sizeof (lwpstat.pr_reg); |
252b5132 RH |
9089 | sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_reg); |
9090 | #endif | |
9091 | ||
252b5132 RH |
9092 | sect->alignment_power = 2; |
9093 | ||
9094 | if (!elfcore_maybe_make_sect (abfd, ".reg", sect)) | |
b34976b6 | 9095 | return FALSE; |
252b5132 RH |
9096 | |
9097 | /* Make a ".reg2/999" section */ | |
9098 | ||
9099 | sprintf (buf, ".reg2/%d", elfcore_make_pid (abfd)); | |
d4c88bbb | 9100 | len = strlen (buf) + 1; |
217aa764 | 9101 | name = bfd_alloc (abfd, len); |
252b5132 | 9102 | if (name == NULL) |
b34976b6 | 9103 | return FALSE; |
d4c88bbb | 9104 | memcpy (name, buf, len); |
252b5132 | 9105 | |
117ed4f8 | 9106 | sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); |
252b5132 | 9107 | if (sect == NULL) |
b34976b6 | 9108 | return FALSE; |
252b5132 RH |
9109 | |
9110 | #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT) | |
eea6121a | 9111 | sect->size = sizeof (lwpstat.pr_context.uc_mcontext.fpregs); |
252b5132 RH |
9112 | sect->filepos = note->descpos |
9113 | + offsetof (lwpstatus_t, pr_context.uc_mcontext.fpregs); | |
9114 | #endif | |
9115 | ||
9116 | #if defined (HAVE_LWPSTATUS_T_PR_FPREG) | |
eea6121a | 9117 | sect->size = sizeof (lwpstat.pr_fpreg); |
252b5132 RH |
9118 | sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_fpreg); |
9119 | #endif | |
9120 | ||
252b5132 RH |
9121 | sect->alignment_power = 2; |
9122 | ||
936e320b | 9123 | return elfcore_maybe_make_sect (abfd, ".reg2", sect); |
252b5132 RH |
9124 | } |
9125 | #endif /* defined (HAVE_LWPSTATUS_T) */ | |
9126 | ||
b34976b6 | 9127 | static bfd_boolean |
217aa764 | 9128 | elfcore_grok_win32pstatus (bfd *abfd, Elf_Internal_Note *note) |
16e9c715 NC |
9129 | { |
9130 | char buf[30]; | |
c044fabd | 9131 | char *name; |
d4c88bbb | 9132 | size_t len; |
c044fabd | 9133 | asection *sect; |
4a6636fb PA |
9134 | int type; |
9135 | int is_active_thread; | |
9136 | bfd_vma base_addr; | |
16e9c715 | 9137 | |
4a6636fb | 9138 | if (note->descsz < 728) |
b34976b6 | 9139 | return TRUE; |
16e9c715 | 9140 | |
4a6636fb PA |
9141 | if (! CONST_STRNEQ (note->namedata, "win32")) |
9142 | return TRUE; | |
9143 | ||
9144 | type = bfd_get_32 (abfd, note->descdata); | |
c044fabd | 9145 | |
4a6636fb | 9146 | switch (type) |
16e9c715 | 9147 | { |
4a6636fb | 9148 | case 1 /* NOTE_INFO_PROCESS */: |
228e534f | 9149 | /* FIXME: need to add ->core->command. */ |
4a6636fb | 9150 | /* process_info.pid */ |
228e534f | 9151 | elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, note->descdata + 8); |
4a6636fb | 9152 | /* process_info.signal */ |
228e534f | 9153 | elf_tdata (abfd)->core->signal = bfd_get_32 (abfd, note->descdata + 12); |
c044fabd | 9154 | break; |
16e9c715 | 9155 | |
4a6636fb | 9156 | case 2 /* NOTE_INFO_THREAD */: |
16e9c715 | 9157 | /* Make a ".reg/999" section. */ |
4a6636fb PA |
9158 | /* thread_info.tid */ |
9159 | sprintf (buf, ".reg/%ld", (long) bfd_get_32 (abfd, note->descdata + 8)); | |
c044fabd | 9160 | |
d4c88bbb | 9161 | len = strlen (buf) + 1; |
a50b1753 | 9162 | name = (char *) bfd_alloc (abfd, len); |
16e9c715 | 9163 | if (name == NULL) |
b34976b6 | 9164 | return FALSE; |
c044fabd | 9165 | |
d4c88bbb | 9166 | memcpy (name, buf, len); |
16e9c715 | 9167 | |
117ed4f8 | 9168 | sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); |
16e9c715 | 9169 | if (sect == NULL) |
b34976b6 | 9170 | return FALSE; |
c044fabd | 9171 | |
4a6636fb PA |
9172 | /* sizeof (thread_info.thread_context) */ |
9173 | sect->size = 716; | |
9174 | /* offsetof (thread_info.thread_context) */ | |
9175 | sect->filepos = note->descpos + 12; | |
16e9c715 NC |
9176 | sect->alignment_power = 2; |
9177 | ||
4a6636fb PA |
9178 | /* thread_info.is_active_thread */ |
9179 | is_active_thread = bfd_get_32 (abfd, note->descdata + 8); | |
9180 | ||
9181 | if (is_active_thread) | |
16e9c715 | 9182 | if (! elfcore_maybe_make_sect (abfd, ".reg", sect)) |
b34976b6 | 9183 | return FALSE; |
16e9c715 NC |
9184 | break; |
9185 | ||
4a6636fb | 9186 | case 3 /* NOTE_INFO_MODULE */: |
16e9c715 | 9187 | /* Make a ".module/xxxxxxxx" section. */ |
4a6636fb PA |
9188 | /* module_info.base_address */ |
9189 | base_addr = bfd_get_32 (abfd, note->descdata + 4); | |
0af1713e | 9190 | sprintf (buf, ".module/%08lx", (unsigned long) base_addr); |
c044fabd | 9191 | |
d4c88bbb | 9192 | len = strlen (buf) + 1; |
a50b1753 | 9193 | name = (char *) bfd_alloc (abfd, len); |
16e9c715 | 9194 | if (name == NULL) |
b34976b6 | 9195 | return FALSE; |
c044fabd | 9196 | |
d4c88bbb | 9197 | memcpy (name, buf, len); |
252b5132 | 9198 | |
117ed4f8 | 9199 | sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); |
c044fabd | 9200 | |
16e9c715 | 9201 | if (sect == NULL) |
b34976b6 | 9202 | return FALSE; |
c044fabd | 9203 | |
eea6121a | 9204 | sect->size = note->descsz; |
16e9c715 | 9205 | sect->filepos = note->descpos; |
16e9c715 NC |
9206 | sect->alignment_power = 2; |
9207 | break; | |
9208 | ||
9209 | default: | |
b34976b6 | 9210 | return TRUE; |
16e9c715 NC |
9211 | } |
9212 | ||
b34976b6 | 9213 | return TRUE; |
16e9c715 | 9214 | } |
252b5132 | 9215 | |
b34976b6 | 9216 | static bfd_boolean |
217aa764 | 9217 | elfcore_grok_note (bfd *abfd, Elf_Internal_Note *note) |
252b5132 | 9218 | { |
9c5bfbb7 | 9219 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
bb0082d6 | 9220 | |
252b5132 RH |
9221 | switch (note->type) |
9222 | { | |
9223 | default: | |
b34976b6 | 9224 | return TRUE; |
252b5132 | 9225 | |
252b5132 | 9226 | case NT_PRSTATUS: |
bb0082d6 AM |
9227 | if (bed->elf_backend_grok_prstatus) |
9228 | if ((*bed->elf_backend_grok_prstatus) (abfd, note)) | |
b34976b6 | 9229 | return TRUE; |
bb0082d6 | 9230 | #if defined (HAVE_PRSTATUS_T) |
252b5132 | 9231 | return elfcore_grok_prstatus (abfd, note); |
bb0082d6 | 9232 | #else |
b34976b6 | 9233 | return TRUE; |
252b5132 RH |
9234 | #endif |
9235 | ||
9236 | #if defined (HAVE_PSTATUS_T) | |
9237 | case NT_PSTATUS: | |
9238 | return elfcore_grok_pstatus (abfd, note); | |
9239 | #endif | |
9240 | ||
9241 | #if defined (HAVE_LWPSTATUS_T) | |
9242 | case NT_LWPSTATUS: | |
9243 | return elfcore_grok_lwpstatus (abfd, note); | |
9244 | #endif | |
9245 | ||
9246 | case NT_FPREGSET: /* FIXME: rename to NT_PRFPREG */ | |
9247 | return elfcore_grok_prfpreg (abfd, note); | |
9248 | ||
c044fabd | 9249 | case NT_WIN32PSTATUS: |
16e9c715 | 9250 | return elfcore_grok_win32pstatus (abfd, note); |
16e9c715 | 9251 | |
c044fabd | 9252 | case NT_PRXFPREG: /* Linux SSE extension */ |
e377ab71 MK |
9253 | if (note->namesz == 6 |
9254 | && strcmp (note->namedata, "LINUX") == 0) | |
ff08c6bb JB |
9255 | return elfcore_grok_prxfpreg (abfd, note); |
9256 | else | |
b34976b6 | 9257 | return TRUE; |
ff08c6bb | 9258 | |
4339cae0 L |
9259 | case NT_X86_XSTATE: /* Linux XSAVE extension */ |
9260 | if (note->namesz == 6 | |
9261 | && strcmp (note->namedata, "LINUX") == 0) | |
9262 | return elfcore_grok_xstatereg (abfd, note); | |
97de3545 JB |
9263 | else if (note->namesz == 8 |
9264 | && strcmp (note->namedata, "FreeBSD") == 0) | |
9265 | return elfcore_grok_xstatereg (abfd, note); | |
4339cae0 L |
9266 | else |
9267 | return TRUE; | |
9268 | ||
97753bd5 AM |
9269 | case NT_PPC_VMX: |
9270 | if (note->namesz == 6 | |
9271 | && strcmp (note->namedata, "LINUX") == 0) | |
9272 | return elfcore_grok_ppc_vmx (abfd, note); | |
9273 | else | |
9274 | return TRUE; | |
9275 | ||
89eeb0bc LM |
9276 | case NT_PPC_VSX: |
9277 | if (note->namesz == 6 | |
9278 | && strcmp (note->namedata, "LINUX") == 0) | |
9279 | return elfcore_grok_ppc_vsx (abfd, note); | |
9280 | else | |
9281 | return TRUE; | |
9282 | ||
0675e188 UW |
9283 | case NT_S390_HIGH_GPRS: |
9284 | if (note->namesz == 6 | |
9285 | && strcmp (note->namedata, "LINUX") == 0) | |
9286 | return elfcore_grok_s390_high_gprs (abfd, note); | |
9287 | else | |
9288 | return TRUE; | |
9289 | ||
d7eeb400 MS |
9290 | case NT_S390_TIMER: |
9291 | if (note->namesz == 6 | |
9292 | && strcmp (note->namedata, "LINUX") == 0) | |
9293 | return elfcore_grok_s390_timer (abfd, note); | |
9294 | else | |
9295 | return TRUE; | |
9296 | ||
9297 | case NT_S390_TODCMP: | |
9298 | if (note->namesz == 6 | |
9299 | && strcmp (note->namedata, "LINUX") == 0) | |
9300 | return elfcore_grok_s390_todcmp (abfd, note); | |
9301 | else | |
9302 | return TRUE; | |
9303 | ||
9304 | case NT_S390_TODPREG: | |
9305 | if (note->namesz == 6 | |
9306 | && strcmp (note->namedata, "LINUX") == 0) | |
9307 | return elfcore_grok_s390_todpreg (abfd, note); | |
9308 | else | |
9309 | return TRUE; | |
9310 | ||
9311 | case NT_S390_CTRS: | |
9312 | if (note->namesz == 6 | |
9313 | && strcmp (note->namedata, "LINUX") == 0) | |
9314 | return elfcore_grok_s390_ctrs (abfd, note); | |
9315 | else | |
9316 | return TRUE; | |
9317 | ||
9318 | case NT_S390_PREFIX: | |
9319 | if (note->namesz == 6 | |
9320 | && strcmp (note->namedata, "LINUX") == 0) | |
9321 | return elfcore_grok_s390_prefix (abfd, note); | |
9322 | else | |
9323 | return TRUE; | |
9324 | ||
355b81d9 UW |
9325 | case NT_S390_LAST_BREAK: |
9326 | if (note->namesz == 6 | |
9327 | && strcmp (note->namedata, "LINUX") == 0) | |
9328 | return elfcore_grok_s390_last_break (abfd, note); | |
9329 | else | |
9330 | return TRUE; | |
9331 | ||
9332 | case NT_S390_SYSTEM_CALL: | |
9333 | if (note->namesz == 6 | |
9334 | && strcmp (note->namedata, "LINUX") == 0) | |
9335 | return elfcore_grok_s390_system_call (abfd, note); | |
9336 | else | |
9337 | return TRUE; | |
9338 | ||
abb3f6cc NC |
9339 | case NT_S390_TDB: |
9340 | if (note->namesz == 6 | |
9341 | && strcmp (note->namedata, "LINUX") == 0) | |
9342 | return elfcore_grok_s390_tdb (abfd, note); | |
9343 | else | |
9344 | return TRUE; | |
9345 | ||
4ef9f41a AA |
9346 | case NT_S390_VXRS_LOW: |
9347 | if (note->namesz == 6 | |
9348 | && strcmp (note->namedata, "LINUX") == 0) | |
9349 | return elfcore_grok_s390_vxrs_low (abfd, note); | |
9350 | else | |
9351 | return TRUE; | |
9352 | ||
9353 | case NT_S390_VXRS_HIGH: | |
9354 | if (note->namesz == 6 | |
9355 | && strcmp (note->namedata, "LINUX") == 0) | |
9356 | return elfcore_grok_s390_vxrs_high (abfd, note); | |
9357 | else | |
9358 | return TRUE; | |
9359 | ||
faa9a424 UW |
9360 | case NT_ARM_VFP: |
9361 | if (note->namesz == 6 | |
9362 | && strcmp (note->namedata, "LINUX") == 0) | |
9363 | return elfcore_grok_arm_vfp (abfd, note); | |
9364 | else | |
9365 | return TRUE; | |
9366 | ||
652451f8 YZ |
9367 | case NT_ARM_TLS: |
9368 | if (note->namesz == 6 | |
9369 | && strcmp (note->namedata, "LINUX") == 0) | |
9370 | return elfcore_grok_aarch_tls (abfd, note); | |
9371 | else | |
9372 | return TRUE; | |
9373 | ||
9374 | case NT_ARM_HW_BREAK: | |
9375 | if (note->namesz == 6 | |
9376 | && strcmp (note->namedata, "LINUX") == 0) | |
9377 | return elfcore_grok_aarch_hw_break (abfd, note); | |
9378 | else | |
9379 | return TRUE; | |
9380 | ||
9381 | case NT_ARM_HW_WATCH: | |
9382 | if (note->namesz == 6 | |
9383 | && strcmp (note->namedata, "LINUX") == 0) | |
9384 | return elfcore_grok_aarch_hw_watch (abfd, note); | |
9385 | else | |
9386 | return TRUE; | |
9387 | ||
252b5132 RH |
9388 | case NT_PRPSINFO: |
9389 | case NT_PSINFO: | |
bb0082d6 AM |
9390 | if (bed->elf_backend_grok_psinfo) |
9391 | if ((*bed->elf_backend_grok_psinfo) (abfd, note)) | |
b34976b6 | 9392 | return TRUE; |
bb0082d6 | 9393 | #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) |
252b5132 | 9394 | return elfcore_grok_psinfo (abfd, note); |
bb0082d6 | 9395 | #else |
b34976b6 | 9396 | return TRUE; |
252b5132 | 9397 | #endif |
3333a7c3 RM |
9398 | |
9399 | case NT_AUXV: | |
9400 | { | |
117ed4f8 AM |
9401 | asection *sect = bfd_make_section_anyway_with_flags (abfd, ".auxv", |
9402 | SEC_HAS_CONTENTS); | |
3333a7c3 RM |
9403 | |
9404 | if (sect == NULL) | |
9405 | return FALSE; | |
eea6121a | 9406 | sect->size = note->descsz; |
3333a7c3 | 9407 | sect->filepos = note->descpos; |
3333a7c3 RM |
9408 | sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32; |
9409 | ||
9410 | return TRUE; | |
9411 | } | |
9015683b | 9412 | |
451b7c33 TT |
9413 | case NT_FILE: |
9414 | return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.file", | |
9415 | note); | |
9416 | ||
9015683b TT |
9417 | case NT_SIGINFO: |
9418 | return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.siginfo", | |
9419 | note); | |
5b2c414d JB |
9420 | |
9421 | case NT_FREEBSD_THRMISC: | |
9422 | if (note->namesz == 8 | |
9423 | && strcmp (note->namedata, "FreeBSD") == 0) | |
9424 | return elfcore_make_note_pseudosection (abfd, ".thrmisc", note); | |
9425 | else | |
9426 | return TRUE; | |
252b5132 RH |
9427 | } |
9428 | } | |
9429 | ||
718175fa JK |
9430 | static bfd_boolean |
9431 | elfobj_grok_gnu_build_id (bfd *abfd, Elf_Internal_Note *note) | |
9432 | { | |
c74f7d1c | 9433 | struct bfd_build_id* build_id; |
30e8ee25 AM |
9434 | |
9435 | if (note->descsz == 0) | |
9436 | return FALSE; | |
9437 | ||
c74f7d1c JT |
9438 | build_id = bfd_alloc (abfd, sizeof (struct bfd_build_id) - 1 + note->descsz); |
9439 | if (build_id == NULL) | |
718175fa JK |
9440 | return FALSE; |
9441 | ||
c74f7d1c JT |
9442 | build_id->size = note->descsz; |
9443 | memcpy (build_id->data, note->descdata, note->descsz); | |
9444 | abfd->build_id = build_id; | |
718175fa JK |
9445 | |
9446 | return TRUE; | |
9447 | } | |
9448 | ||
9449 | static bfd_boolean | |
9450 | elfobj_grok_gnu_note (bfd *abfd, Elf_Internal_Note *note) | |
9451 | { | |
9452 | switch (note->type) | |
9453 | { | |
9454 | default: | |
9455 | return TRUE; | |
9456 | ||
9457 | case NT_GNU_BUILD_ID: | |
9458 | return elfobj_grok_gnu_build_id (abfd, note); | |
9459 | } | |
9460 | } | |
9461 | ||
e21e5835 NC |
9462 | static bfd_boolean |
9463 | elfobj_grok_stapsdt_note_1 (bfd *abfd, Elf_Internal_Note *note) | |
9464 | { | |
9465 | struct sdt_note *cur = | |
9466 | (struct sdt_note *) bfd_alloc (abfd, sizeof (struct sdt_note) | |
9467 | + note->descsz); | |
9468 | ||
9469 | cur->next = (struct sdt_note *) (elf_tdata (abfd))->sdt_note_head; | |
9470 | cur->size = (bfd_size_type) note->descsz; | |
9471 | memcpy (cur->data, note->descdata, note->descsz); | |
9472 | ||
9473 | elf_tdata (abfd)->sdt_note_head = cur; | |
9474 | ||
9475 | return TRUE; | |
9476 | } | |
9477 | ||
9478 | static bfd_boolean | |
9479 | elfobj_grok_stapsdt_note (bfd *abfd, Elf_Internal_Note *note) | |
9480 | { | |
9481 | switch (note->type) | |
9482 | { | |
9483 | case NT_STAPSDT: | |
9484 | return elfobj_grok_stapsdt_note_1 (abfd, note); | |
9485 | ||
9486 | default: | |
9487 | return TRUE; | |
9488 | } | |
9489 | } | |
9490 | ||
b34976b6 | 9491 | static bfd_boolean |
217aa764 | 9492 | elfcore_netbsd_get_lwpid (Elf_Internal_Note *note, int *lwpidp) |
50b2bdb7 AM |
9493 | { |
9494 | char *cp; | |
9495 | ||
9496 | cp = strchr (note->namedata, '@'); | |
9497 | if (cp != NULL) | |
9498 | { | |
d2b64500 | 9499 | *lwpidp = atoi(cp + 1); |
b34976b6 | 9500 | return TRUE; |
50b2bdb7 | 9501 | } |
b34976b6 | 9502 | return FALSE; |
50b2bdb7 AM |
9503 | } |
9504 | ||
b34976b6 | 9505 | static bfd_boolean |
217aa764 | 9506 | elfcore_grok_netbsd_procinfo (bfd *abfd, Elf_Internal_Note *note) |
50b2bdb7 | 9507 | { |
50b2bdb7 | 9508 | /* Signal number at offset 0x08. */ |
228e534f | 9509 | elf_tdata (abfd)->core->signal |
50b2bdb7 AM |
9510 | = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08); |
9511 | ||
9512 | /* Process ID at offset 0x50. */ | |
228e534f | 9513 | elf_tdata (abfd)->core->pid |
50b2bdb7 AM |
9514 | = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x50); |
9515 | ||
9516 | /* Command name at 0x7c (max 32 bytes, including nul). */ | |
228e534f | 9517 | elf_tdata (abfd)->core->command |
50b2bdb7 AM |
9518 | = _bfd_elfcore_strndup (abfd, note->descdata + 0x7c, 31); |
9519 | ||
7720ba9f MK |
9520 | return elfcore_make_note_pseudosection (abfd, ".note.netbsdcore.procinfo", |
9521 | note); | |
50b2bdb7 AM |
9522 | } |
9523 | ||
b34976b6 | 9524 | static bfd_boolean |
217aa764 | 9525 | elfcore_grok_netbsd_note (bfd *abfd, Elf_Internal_Note *note) |
50b2bdb7 AM |
9526 | { |
9527 | int lwp; | |
9528 | ||
9529 | if (elfcore_netbsd_get_lwpid (note, &lwp)) | |
228e534f | 9530 | elf_tdata (abfd)->core->lwpid = lwp; |
50b2bdb7 | 9531 | |
b4db1224 | 9532 | if (note->type == NT_NETBSDCORE_PROCINFO) |
50b2bdb7 AM |
9533 | { |
9534 | /* NetBSD-specific core "procinfo". Note that we expect to | |
08a40648 AM |
9535 | find this note before any of the others, which is fine, |
9536 | since the kernel writes this note out first when it | |
9537 | creates a core file. */ | |
47d9a591 | 9538 | |
50b2bdb7 AM |
9539 | return elfcore_grok_netbsd_procinfo (abfd, note); |
9540 | } | |
9541 | ||
b4db1224 JT |
9542 | /* As of Jan 2002 there are no other machine-independent notes |
9543 | defined for NetBSD core files. If the note type is less | |
9544 | than the start of the machine-dependent note types, we don't | |
9545 | understand it. */ | |
47d9a591 | 9546 | |
b4db1224 | 9547 | if (note->type < NT_NETBSDCORE_FIRSTMACH) |
b34976b6 | 9548 | return TRUE; |
50b2bdb7 AM |
9549 | |
9550 | ||
9551 | switch (bfd_get_arch (abfd)) | |
9552 | { | |
08a40648 AM |
9553 | /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and |
9554 | PT_GETFPREGS == mach+2. */ | |
50b2bdb7 AM |
9555 | |
9556 | case bfd_arch_alpha: | |
9557 | case bfd_arch_sparc: | |
9558 | switch (note->type) | |
08a40648 AM |
9559 | { |
9560 | case NT_NETBSDCORE_FIRSTMACH+0: | |
9561 | return elfcore_make_note_pseudosection (abfd, ".reg", note); | |
50b2bdb7 | 9562 | |
08a40648 AM |
9563 | case NT_NETBSDCORE_FIRSTMACH+2: |
9564 | return elfcore_make_note_pseudosection (abfd, ".reg2", note); | |
50b2bdb7 | 9565 | |
08a40648 AM |
9566 | default: |
9567 | return TRUE; | |
9568 | } | |
50b2bdb7 | 9569 | |
08a40648 AM |
9570 | /* On all other arch's, PT_GETREGS == mach+1 and |
9571 | PT_GETFPREGS == mach+3. */ | |
50b2bdb7 AM |
9572 | |
9573 | default: | |
9574 | switch (note->type) | |
08a40648 AM |
9575 | { |
9576 | case NT_NETBSDCORE_FIRSTMACH+1: | |
9577 | return elfcore_make_note_pseudosection (abfd, ".reg", note); | |
50b2bdb7 | 9578 | |
08a40648 AM |
9579 | case NT_NETBSDCORE_FIRSTMACH+3: |
9580 | return elfcore_make_note_pseudosection (abfd, ".reg2", note); | |
50b2bdb7 | 9581 | |
08a40648 AM |
9582 | default: |
9583 | return TRUE; | |
9584 | } | |
50b2bdb7 AM |
9585 | } |
9586 | /* NOTREACHED */ | |
9587 | } | |
9588 | ||
67cc5033 MK |
9589 | static bfd_boolean |
9590 | elfcore_grok_openbsd_procinfo (bfd *abfd, Elf_Internal_Note *note) | |
9591 | { | |
9592 | /* Signal number at offset 0x08. */ | |
228e534f | 9593 | elf_tdata (abfd)->core->signal |
67cc5033 MK |
9594 | = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08); |
9595 | ||
9596 | /* Process ID at offset 0x20. */ | |
228e534f | 9597 | elf_tdata (abfd)->core->pid |
67cc5033 MK |
9598 | = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x20); |
9599 | ||
9600 | /* Command name at 0x48 (max 32 bytes, including nul). */ | |
228e534f | 9601 | elf_tdata (abfd)->core->command |
67cc5033 MK |
9602 | = _bfd_elfcore_strndup (abfd, note->descdata + 0x48, 31); |
9603 | ||
9604 | return TRUE; | |
9605 | } | |
9606 | ||
9607 | static bfd_boolean | |
9608 | elfcore_grok_openbsd_note (bfd *abfd, Elf_Internal_Note *note) | |
9609 | { | |
9610 | if (note->type == NT_OPENBSD_PROCINFO) | |
9611 | return elfcore_grok_openbsd_procinfo (abfd, note); | |
9612 | ||
9613 | if (note->type == NT_OPENBSD_REGS) | |
9614 | return elfcore_make_note_pseudosection (abfd, ".reg", note); | |
9615 | ||
9616 | if (note->type == NT_OPENBSD_FPREGS) | |
9617 | return elfcore_make_note_pseudosection (abfd, ".reg2", note); | |
9618 | ||
9619 | if (note->type == NT_OPENBSD_XFPREGS) | |
9620 | return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note); | |
9621 | ||
9622 | if (note->type == NT_OPENBSD_AUXV) | |
9623 | { | |
9624 | asection *sect = bfd_make_section_anyway_with_flags (abfd, ".auxv", | |
9625 | SEC_HAS_CONTENTS); | |
9626 | ||
9627 | if (sect == NULL) | |
9628 | return FALSE; | |
9629 | sect->size = note->descsz; | |
9630 | sect->filepos = note->descpos; | |
9631 | sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32; | |
9632 | ||
9633 | return TRUE; | |
9634 | } | |
9635 | ||
9636 | if (note->type == NT_OPENBSD_WCOOKIE) | |
9637 | { | |
9638 | asection *sect = bfd_make_section_anyway_with_flags (abfd, ".wcookie", | |
9639 | SEC_HAS_CONTENTS); | |
9640 | ||
9641 | if (sect == NULL) | |
9642 | return FALSE; | |
9643 | sect->size = note->descsz; | |
9644 | sect->filepos = note->descpos; | |
9645 | sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32; | |
9646 | ||
9647 | return TRUE; | |
9648 | } | |
9649 | ||
9650 | return TRUE; | |
9651 | } | |
9652 | ||
07c6e936 | 9653 | static bfd_boolean |
d3fd4074 | 9654 | elfcore_grok_nto_status (bfd *abfd, Elf_Internal_Note *note, long *tid) |
07c6e936 NC |
9655 | { |
9656 | void *ddata = note->descdata; | |
9657 | char buf[100]; | |
9658 | char *name; | |
9659 | asection *sect; | |
f8843e87 AM |
9660 | short sig; |
9661 | unsigned flags; | |
07c6e936 NC |
9662 | |
9663 | /* nto_procfs_status 'pid' field is at offset 0. */ | |
228e534f | 9664 | elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, (bfd_byte *) ddata); |
07c6e936 | 9665 | |
f8843e87 AM |
9666 | /* nto_procfs_status 'tid' field is at offset 4. Pass it back. */ |
9667 | *tid = bfd_get_32 (abfd, (bfd_byte *) ddata + 4); | |
9668 | ||
9669 | /* nto_procfs_status 'flags' field is at offset 8. */ | |
9670 | flags = bfd_get_32 (abfd, (bfd_byte *) ddata + 8); | |
07c6e936 NC |
9671 | |
9672 | /* nto_procfs_status 'what' field is at offset 14. */ | |
f8843e87 AM |
9673 | if ((sig = bfd_get_16 (abfd, (bfd_byte *) ddata + 14)) > 0) |
9674 | { | |
228e534f AM |
9675 | elf_tdata (abfd)->core->signal = sig; |
9676 | elf_tdata (abfd)->core->lwpid = *tid; | |
f8843e87 | 9677 | } |
07c6e936 | 9678 | |
f8843e87 AM |
9679 | /* _DEBUG_FLAG_CURTID (current thread) is 0x80. Some cores |
9680 | do not come from signals so we make sure we set the current | |
9681 | thread just in case. */ | |
9682 | if (flags & 0x00000080) | |
228e534f | 9683 | elf_tdata (abfd)->core->lwpid = *tid; |
07c6e936 NC |
9684 | |
9685 | /* Make a ".qnx_core_status/%d" section. */ | |
d3fd4074 | 9686 | sprintf (buf, ".qnx_core_status/%ld", *tid); |
07c6e936 | 9687 | |
a50b1753 | 9688 | name = (char *) bfd_alloc (abfd, strlen (buf) + 1); |
07c6e936 NC |
9689 | if (name == NULL) |
9690 | return FALSE; | |
9691 | strcpy (name, buf); | |
9692 | ||
117ed4f8 | 9693 | sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); |
07c6e936 NC |
9694 | if (sect == NULL) |
9695 | return FALSE; | |
9696 | ||
eea6121a | 9697 | sect->size = note->descsz; |
07c6e936 | 9698 | sect->filepos = note->descpos; |
07c6e936 NC |
9699 | sect->alignment_power = 2; |
9700 | ||
9701 | return (elfcore_maybe_make_sect (abfd, ".qnx_core_status", sect)); | |
9702 | } | |
9703 | ||
9704 | static bfd_boolean | |
d69f560c KW |
9705 | elfcore_grok_nto_regs (bfd *abfd, |
9706 | Elf_Internal_Note *note, | |
d3fd4074 | 9707 | long tid, |
d69f560c | 9708 | char *base) |
07c6e936 NC |
9709 | { |
9710 | char buf[100]; | |
9711 | char *name; | |
9712 | asection *sect; | |
9713 | ||
d69f560c | 9714 | /* Make a "(base)/%d" section. */ |
d3fd4074 | 9715 | sprintf (buf, "%s/%ld", base, tid); |
07c6e936 | 9716 | |
a50b1753 | 9717 | name = (char *) bfd_alloc (abfd, strlen (buf) + 1); |
07c6e936 NC |
9718 | if (name == NULL) |
9719 | return FALSE; | |
9720 | strcpy (name, buf); | |
9721 | ||
117ed4f8 | 9722 | sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); |
07c6e936 NC |
9723 | if (sect == NULL) |
9724 | return FALSE; | |
9725 | ||
eea6121a | 9726 | sect->size = note->descsz; |
07c6e936 | 9727 | sect->filepos = note->descpos; |
07c6e936 NC |
9728 | sect->alignment_power = 2; |
9729 | ||
f8843e87 | 9730 | /* This is the current thread. */ |
228e534f | 9731 | if (elf_tdata (abfd)->core->lwpid == tid) |
d69f560c | 9732 | return elfcore_maybe_make_sect (abfd, base, sect); |
f8843e87 AM |
9733 | |
9734 | return TRUE; | |
07c6e936 NC |
9735 | } |
9736 | ||
9737 | #define BFD_QNT_CORE_INFO 7 | |
9738 | #define BFD_QNT_CORE_STATUS 8 | |
9739 | #define BFD_QNT_CORE_GREG 9 | |
9740 | #define BFD_QNT_CORE_FPREG 10 | |
9741 | ||
9742 | static bfd_boolean | |
217aa764 | 9743 | elfcore_grok_nto_note (bfd *abfd, Elf_Internal_Note *note) |
07c6e936 NC |
9744 | { |
9745 | /* Every GREG section has a STATUS section before it. Store the | |
811072d8 | 9746 | tid from the previous call to pass down to the next gregs |
07c6e936 | 9747 | function. */ |
d3fd4074 | 9748 | static long tid = 1; |
07c6e936 NC |
9749 | |
9750 | switch (note->type) | |
9751 | { | |
d69f560c KW |
9752 | case BFD_QNT_CORE_INFO: |
9753 | return elfcore_make_note_pseudosection (abfd, ".qnx_core_info", note); | |
9754 | case BFD_QNT_CORE_STATUS: | |
9755 | return elfcore_grok_nto_status (abfd, note, &tid); | |
9756 | case BFD_QNT_CORE_GREG: | |
9757 | return elfcore_grok_nto_regs (abfd, note, tid, ".reg"); | |
9758 | case BFD_QNT_CORE_FPREG: | |
9759 | return elfcore_grok_nto_regs (abfd, note, tid, ".reg2"); | |
9760 | default: | |
9761 | return TRUE; | |
07c6e936 NC |
9762 | } |
9763 | } | |
9764 | ||
b15fa79e AM |
9765 | static bfd_boolean |
9766 | elfcore_grok_spu_note (bfd *abfd, Elf_Internal_Note *note) | |
9767 | { | |
9768 | char *name; | |
9769 | asection *sect; | |
9770 | size_t len; | |
9771 | ||
9772 | /* Use note name as section name. */ | |
9773 | len = note->namesz; | |
a50b1753 | 9774 | name = (char *) bfd_alloc (abfd, len); |
b15fa79e AM |
9775 | if (name == NULL) |
9776 | return FALSE; | |
9777 | memcpy (name, note->namedata, len); | |
9778 | name[len - 1] = '\0'; | |
9779 | ||
9780 | sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); | |
9781 | if (sect == NULL) | |
9782 | return FALSE; | |
9783 | ||
9784 | sect->size = note->descsz; | |
9785 | sect->filepos = note->descpos; | |
9786 | sect->alignment_power = 1; | |
9787 | ||
9788 | return TRUE; | |
9789 | } | |
9790 | ||
7c76fa91 MS |
9791 | /* Function: elfcore_write_note |
9792 | ||
47d9a591 | 9793 | Inputs: |
a39f3346 | 9794 | buffer to hold note, and current size of buffer |
7c76fa91 MS |
9795 | name of note |
9796 | type of note | |
9797 | data for note | |
9798 | size of data for note | |
9799 | ||
a39f3346 AM |
9800 | Writes note to end of buffer. ELF64 notes are written exactly as |
9801 | for ELF32, despite the current (as of 2006) ELF gabi specifying | |
9802 | that they ought to have 8-byte namesz and descsz field, and have | |
9803 | 8-byte alignment. Other writers, eg. Linux kernel, do the same. | |
9804 | ||
7c76fa91 | 9805 | Return: |
a39f3346 | 9806 | Pointer to realloc'd buffer, *BUFSIZ updated. */ |
7c76fa91 MS |
9807 | |
9808 | char * | |
a39f3346 | 9809 | elfcore_write_note (bfd *abfd, |
217aa764 | 9810 | char *buf, |
a39f3346 | 9811 | int *bufsiz, |
217aa764 | 9812 | const char *name, |
a39f3346 | 9813 | int type, |
217aa764 | 9814 | const void *input, |
a39f3346 | 9815 | int size) |
7c76fa91 MS |
9816 | { |
9817 | Elf_External_Note *xnp; | |
d4c88bbb | 9818 | size_t namesz; |
d4c88bbb | 9819 | size_t newspace; |
a39f3346 | 9820 | char *dest; |
7c76fa91 | 9821 | |
d4c88bbb | 9822 | namesz = 0; |
d4c88bbb | 9823 | if (name != NULL) |
a39f3346 | 9824 | namesz = strlen (name) + 1; |
d4c88bbb | 9825 | |
a39f3346 | 9826 | newspace = 12 + ((namesz + 3) & -4) + ((size + 3) & -4); |
d4c88bbb | 9827 | |
a50b1753 | 9828 | buf = (char *) realloc (buf, *bufsiz + newspace); |
14b1c01e AM |
9829 | if (buf == NULL) |
9830 | return buf; | |
a39f3346 | 9831 | dest = buf + *bufsiz; |
7c76fa91 MS |
9832 | *bufsiz += newspace; |
9833 | xnp = (Elf_External_Note *) dest; | |
9834 | H_PUT_32 (abfd, namesz, xnp->namesz); | |
9835 | H_PUT_32 (abfd, size, xnp->descsz); | |
9836 | H_PUT_32 (abfd, type, xnp->type); | |
d4c88bbb AM |
9837 | dest = xnp->name; |
9838 | if (name != NULL) | |
9839 | { | |
9840 | memcpy (dest, name, namesz); | |
9841 | dest += namesz; | |
a39f3346 | 9842 | while (namesz & 3) |
d4c88bbb AM |
9843 | { |
9844 | *dest++ = '\0'; | |
a39f3346 | 9845 | ++namesz; |
d4c88bbb AM |
9846 | } |
9847 | } | |
9848 | memcpy (dest, input, size); | |
a39f3346 AM |
9849 | dest += size; |
9850 | while (size & 3) | |
9851 | { | |
9852 | *dest++ = '\0'; | |
9853 | ++size; | |
9854 | } | |
9855 | return buf; | |
7c76fa91 MS |
9856 | } |
9857 | ||
7c76fa91 | 9858 | char * |
217aa764 AM |
9859 | elfcore_write_prpsinfo (bfd *abfd, |
9860 | char *buf, | |
9861 | int *bufsiz, | |
9862 | const char *fname, | |
9863 | const char *psargs) | |
7c76fa91 | 9864 | { |
183e98be AM |
9865 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
9866 | ||
9867 | if (bed->elf_backend_write_core_note != NULL) | |
9868 | { | |
9869 | char *ret; | |
9870 | ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz, | |
9871 | NT_PRPSINFO, fname, psargs); | |
9872 | if (ret != NULL) | |
9873 | return ret; | |
9874 | } | |
7c76fa91 | 9875 | |
1f20dca5 | 9876 | #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) |
183e98be AM |
9877 | #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T) |
9878 | if (bed->s->elfclass == ELFCLASS32) | |
9879 | { | |
9880 | #if defined (HAVE_PSINFO32_T) | |
9881 | psinfo32_t data; | |
9882 | int note_type = NT_PSINFO; | |
9883 | #else | |
9884 | prpsinfo32_t data; | |
9885 | int note_type = NT_PRPSINFO; | |
9886 | #endif | |
9887 | ||
9888 | memset (&data, 0, sizeof (data)); | |
9889 | strncpy (data.pr_fname, fname, sizeof (data.pr_fname)); | |
9890 | strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs)); | |
9891 | return elfcore_write_note (abfd, buf, bufsiz, | |
1f20dca5 | 9892 | "CORE", note_type, &data, sizeof (data)); |
183e98be AM |
9893 | } |
9894 | else | |
9895 | #endif | |
9896 | { | |
7c76fa91 | 9897 | #if defined (HAVE_PSINFO_T) |
183e98be AM |
9898 | psinfo_t data; |
9899 | int note_type = NT_PSINFO; | |
7c76fa91 | 9900 | #else |
183e98be AM |
9901 | prpsinfo_t data; |
9902 | int note_type = NT_PRPSINFO; | |
7c76fa91 MS |
9903 | #endif |
9904 | ||
183e98be AM |
9905 | memset (&data, 0, sizeof (data)); |
9906 | strncpy (data.pr_fname, fname, sizeof (data.pr_fname)); | |
9907 | strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs)); | |
9908 | return elfcore_write_note (abfd, buf, bufsiz, | |
1f20dca5 | 9909 | "CORE", note_type, &data, sizeof (data)); |
183e98be | 9910 | } |
7c76fa91 MS |
9911 | #endif /* PSINFO_T or PRPSINFO_T */ |
9912 | ||
1f20dca5 UW |
9913 | free (buf); |
9914 | return NULL; | |
9915 | } | |
9916 | ||
70a38d42 SDJ |
9917 | char * |
9918 | elfcore_write_linux_prpsinfo32 | |
9919 | (bfd *abfd, char *buf, int *bufsiz, | |
9920 | const struct elf_internal_linux_prpsinfo *prpsinfo) | |
9921 | { | |
9922 | struct elf_external_linux_prpsinfo32 data; | |
9923 | ||
aeb70569 | 9924 | swap_linux_prpsinfo32_out (abfd, prpsinfo, &data); |
70a38d42 SDJ |
9925 | return elfcore_write_note (abfd, buf, bufsiz, "CORE", NT_PRPSINFO, |
9926 | &data, sizeof (data)); | |
9927 | } | |
9928 | ||
9929 | char * | |
9930 | elfcore_write_linux_prpsinfo64 | |
9931 | (bfd *abfd, char *buf, int *bufsiz, | |
9932 | const struct elf_internal_linux_prpsinfo *prpsinfo) | |
9933 | { | |
9934 | struct elf_external_linux_prpsinfo64 data; | |
9935 | ||
aeb70569 | 9936 | swap_linux_prpsinfo64_out (abfd, prpsinfo, &data); |
70a38d42 SDJ |
9937 | return elfcore_write_note (abfd, buf, bufsiz, |
9938 | "CORE", NT_PRPSINFO, &data, sizeof (data)); | |
9939 | } | |
9940 | ||
7c76fa91 | 9941 | char * |
217aa764 AM |
9942 | elfcore_write_prstatus (bfd *abfd, |
9943 | char *buf, | |
9944 | int *bufsiz, | |
9945 | long pid, | |
9946 | int cursig, | |
9947 | const void *gregs) | |
7c76fa91 | 9948 | { |
183e98be | 9949 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
7c76fa91 | 9950 | |
183e98be AM |
9951 | if (bed->elf_backend_write_core_note != NULL) |
9952 | { | |
9953 | char *ret; | |
9954 | ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz, | |
9955 | NT_PRSTATUS, | |
9956 | pid, cursig, gregs); | |
9957 | if (ret != NULL) | |
9958 | return ret; | |
9959 | } | |
9960 | ||
1f20dca5 | 9961 | #if defined (HAVE_PRSTATUS_T) |
183e98be AM |
9962 | #if defined (HAVE_PRSTATUS32_T) |
9963 | if (bed->s->elfclass == ELFCLASS32) | |
9964 | { | |
9965 | prstatus32_t prstat; | |
9966 | ||
9967 | memset (&prstat, 0, sizeof (prstat)); | |
9968 | prstat.pr_pid = pid; | |
9969 | prstat.pr_cursig = cursig; | |
9970 | memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg)); | |
1f20dca5 | 9971 | return elfcore_write_note (abfd, buf, bufsiz, "CORE", |
183e98be AM |
9972 | NT_PRSTATUS, &prstat, sizeof (prstat)); |
9973 | } | |
9974 | else | |
9975 | #endif | |
9976 | { | |
9977 | prstatus_t prstat; | |
9978 | ||
9979 | memset (&prstat, 0, sizeof (prstat)); | |
9980 | prstat.pr_pid = pid; | |
9981 | prstat.pr_cursig = cursig; | |
9982 | memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg)); | |
1f20dca5 | 9983 | return elfcore_write_note (abfd, buf, bufsiz, "CORE", |
183e98be AM |
9984 | NT_PRSTATUS, &prstat, sizeof (prstat)); |
9985 | } | |
7c76fa91 MS |
9986 | #endif /* HAVE_PRSTATUS_T */ |
9987 | ||
1f20dca5 UW |
9988 | free (buf); |
9989 | return NULL; | |
9990 | } | |
9991 | ||
51316059 MS |
9992 | #if defined (HAVE_LWPSTATUS_T) |
9993 | char * | |
217aa764 AM |
9994 | elfcore_write_lwpstatus (bfd *abfd, |
9995 | char *buf, | |
9996 | int *bufsiz, | |
9997 | long pid, | |
9998 | int cursig, | |
9999 | const void *gregs) | |
51316059 MS |
10000 | { |
10001 | lwpstatus_t lwpstat; | |
183e98be | 10002 | const char *note_name = "CORE"; |
51316059 MS |
10003 | |
10004 | memset (&lwpstat, 0, sizeof (lwpstat)); | |
10005 | lwpstat.pr_lwpid = pid >> 16; | |
10006 | lwpstat.pr_cursig = cursig; | |
10007 | #if defined (HAVE_LWPSTATUS_T_PR_REG) | |
d1e8523e | 10008 | memcpy (&lwpstat.pr_reg, gregs, sizeof (lwpstat.pr_reg)); |
51316059 MS |
10009 | #elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT) |
10010 | #if !defined(gregs) | |
10011 | memcpy (lwpstat.pr_context.uc_mcontext.gregs, | |
10012 | gregs, sizeof (lwpstat.pr_context.uc_mcontext.gregs)); | |
10013 | #else | |
10014 | memcpy (lwpstat.pr_context.uc_mcontext.__gregs, | |
10015 | gregs, sizeof (lwpstat.pr_context.uc_mcontext.__gregs)); | |
10016 | #endif | |
10017 | #endif | |
47d9a591 | 10018 | return elfcore_write_note (abfd, buf, bufsiz, note_name, |
51316059 MS |
10019 | NT_LWPSTATUS, &lwpstat, sizeof (lwpstat)); |
10020 | } | |
10021 | #endif /* HAVE_LWPSTATUS_T */ | |
10022 | ||
7c76fa91 MS |
10023 | #if defined (HAVE_PSTATUS_T) |
10024 | char * | |
217aa764 AM |
10025 | elfcore_write_pstatus (bfd *abfd, |
10026 | char *buf, | |
10027 | int *bufsiz, | |
10028 | long pid, | |
6c10990d NC |
10029 | int cursig ATTRIBUTE_UNUSED, |
10030 | const void *gregs ATTRIBUTE_UNUSED) | |
7c76fa91 | 10031 | { |
183e98be AM |
10032 | const char *note_name = "CORE"; |
10033 | #if defined (HAVE_PSTATUS32_T) | |
10034 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
7c76fa91 | 10035 | |
183e98be AM |
10036 | if (bed->s->elfclass == ELFCLASS32) |
10037 | { | |
10038 | pstatus32_t pstat; | |
10039 | ||
10040 | memset (&pstat, 0, sizeof (pstat)); | |
10041 | pstat.pr_pid = pid & 0xffff; | |
10042 | buf = elfcore_write_note (abfd, buf, bufsiz, note_name, | |
10043 | NT_PSTATUS, &pstat, sizeof (pstat)); | |
10044 | return buf; | |
10045 | } | |
10046 | else | |
10047 | #endif | |
10048 | { | |
10049 | pstatus_t pstat; | |
10050 | ||
10051 | memset (&pstat, 0, sizeof (pstat)); | |
10052 | pstat.pr_pid = pid & 0xffff; | |
10053 | buf = elfcore_write_note (abfd, buf, bufsiz, note_name, | |
10054 | NT_PSTATUS, &pstat, sizeof (pstat)); | |
10055 | return buf; | |
10056 | } | |
7c76fa91 MS |
10057 | } |
10058 | #endif /* HAVE_PSTATUS_T */ | |
10059 | ||
10060 | char * | |
217aa764 AM |
10061 | elfcore_write_prfpreg (bfd *abfd, |
10062 | char *buf, | |
10063 | int *bufsiz, | |
10064 | const void *fpregs, | |
10065 | int size) | |
7c76fa91 | 10066 | { |
183e98be | 10067 | const char *note_name = "CORE"; |
47d9a591 | 10068 | return elfcore_write_note (abfd, buf, bufsiz, |
7c76fa91 MS |
10069 | note_name, NT_FPREGSET, fpregs, size); |
10070 | } | |
10071 | ||
10072 | char * | |
217aa764 AM |
10073 | elfcore_write_prxfpreg (bfd *abfd, |
10074 | char *buf, | |
10075 | int *bufsiz, | |
10076 | const void *xfpregs, | |
10077 | int size) | |
7c76fa91 MS |
10078 | { |
10079 | char *note_name = "LINUX"; | |
47d9a591 | 10080 | return elfcore_write_note (abfd, buf, bufsiz, |
7c76fa91 MS |
10081 | note_name, NT_PRXFPREG, xfpregs, size); |
10082 | } | |
10083 | ||
4339cae0 L |
10084 | char * |
10085 | elfcore_write_xstatereg (bfd *abfd, char *buf, int *bufsiz, | |
10086 | const void *xfpregs, int size) | |
10087 | { | |
97de3545 JB |
10088 | char *note_name; |
10089 | if (get_elf_backend_data (abfd)->elf_osabi == ELFOSABI_FREEBSD) | |
10090 | note_name = "FreeBSD"; | |
10091 | else | |
10092 | note_name = "LINUX"; | |
4339cae0 L |
10093 | return elfcore_write_note (abfd, buf, bufsiz, |
10094 | note_name, NT_X86_XSTATE, xfpregs, size); | |
10095 | } | |
10096 | ||
97753bd5 AM |
10097 | char * |
10098 | elfcore_write_ppc_vmx (bfd *abfd, | |
10099 | char *buf, | |
10100 | int *bufsiz, | |
10101 | const void *ppc_vmx, | |
10102 | int size) | |
10103 | { | |
10104 | char *note_name = "LINUX"; | |
10105 | return elfcore_write_note (abfd, buf, bufsiz, | |
10106 | note_name, NT_PPC_VMX, ppc_vmx, size); | |
10107 | } | |
10108 | ||
89eeb0bc LM |
10109 | char * |
10110 | elfcore_write_ppc_vsx (bfd *abfd, | |
10111 | char *buf, | |
10112 | int *bufsiz, | |
10113 | const void *ppc_vsx, | |
10114 | int size) | |
10115 | { | |
10116 | char *note_name = "LINUX"; | |
10117 | return elfcore_write_note (abfd, buf, bufsiz, | |
10118 | note_name, NT_PPC_VSX, ppc_vsx, size); | |
10119 | } | |
10120 | ||
0675e188 UW |
10121 | static char * |
10122 | elfcore_write_s390_high_gprs (bfd *abfd, | |
10123 | char *buf, | |
10124 | int *bufsiz, | |
10125 | const void *s390_high_gprs, | |
10126 | int size) | |
10127 | { | |
10128 | char *note_name = "LINUX"; | |
10129 | return elfcore_write_note (abfd, buf, bufsiz, | |
10130 | note_name, NT_S390_HIGH_GPRS, | |
10131 | s390_high_gprs, size); | |
10132 | } | |
10133 | ||
d7eeb400 MS |
10134 | char * |
10135 | elfcore_write_s390_timer (bfd *abfd, | |
10136 | char *buf, | |
10137 | int *bufsiz, | |
10138 | const void *s390_timer, | |
10139 | int size) | |
10140 | { | |
10141 | char *note_name = "LINUX"; | |
10142 | return elfcore_write_note (abfd, buf, bufsiz, | |
10143 | note_name, NT_S390_TIMER, s390_timer, size); | |
10144 | } | |
10145 | ||
10146 | char * | |
10147 | elfcore_write_s390_todcmp (bfd *abfd, | |
10148 | char *buf, | |
10149 | int *bufsiz, | |
10150 | const void *s390_todcmp, | |
10151 | int size) | |
10152 | { | |
10153 | char *note_name = "LINUX"; | |
10154 | return elfcore_write_note (abfd, buf, bufsiz, | |
10155 | note_name, NT_S390_TODCMP, s390_todcmp, size); | |
10156 | } | |
10157 | ||
10158 | char * | |
10159 | elfcore_write_s390_todpreg (bfd *abfd, | |
10160 | char *buf, | |
10161 | int *bufsiz, | |
10162 | const void *s390_todpreg, | |
10163 | int size) | |
10164 | { | |
10165 | char *note_name = "LINUX"; | |
10166 | return elfcore_write_note (abfd, buf, bufsiz, | |
10167 | note_name, NT_S390_TODPREG, s390_todpreg, size); | |
10168 | } | |
10169 | ||
10170 | char * | |
10171 | elfcore_write_s390_ctrs (bfd *abfd, | |
10172 | char *buf, | |
10173 | int *bufsiz, | |
10174 | const void *s390_ctrs, | |
10175 | int size) | |
10176 | { | |
10177 | char *note_name = "LINUX"; | |
10178 | return elfcore_write_note (abfd, buf, bufsiz, | |
10179 | note_name, NT_S390_CTRS, s390_ctrs, size); | |
10180 | } | |
10181 | ||
10182 | char * | |
10183 | elfcore_write_s390_prefix (bfd *abfd, | |
10184 | char *buf, | |
10185 | int *bufsiz, | |
10186 | const void *s390_prefix, | |
10187 | int size) | |
10188 | { | |
10189 | char *note_name = "LINUX"; | |
10190 | return elfcore_write_note (abfd, buf, bufsiz, | |
10191 | note_name, NT_S390_PREFIX, s390_prefix, size); | |
10192 | } | |
10193 | ||
355b81d9 UW |
10194 | char * |
10195 | elfcore_write_s390_last_break (bfd *abfd, | |
10196 | char *buf, | |
10197 | int *bufsiz, | |
10198 | const void *s390_last_break, | |
10199 | int size) | |
10200 | { | |
10201 | char *note_name = "LINUX"; | |
10202 | return elfcore_write_note (abfd, buf, bufsiz, | |
10203 | note_name, NT_S390_LAST_BREAK, | |
10204 | s390_last_break, size); | |
10205 | } | |
10206 | ||
10207 | char * | |
10208 | elfcore_write_s390_system_call (bfd *abfd, | |
10209 | char *buf, | |
10210 | int *bufsiz, | |
10211 | const void *s390_system_call, | |
10212 | int size) | |
10213 | { | |
10214 | char *note_name = "LINUX"; | |
10215 | return elfcore_write_note (abfd, buf, bufsiz, | |
10216 | note_name, NT_S390_SYSTEM_CALL, | |
10217 | s390_system_call, size); | |
10218 | } | |
10219 | ||
abb3f6cc NC |
10220 | char * |
10221 | elfcore_write_s390_tdb (bfd *abfd, | |
10222 | char *buf, | |
10223 | int *bufsiz, | |
10224 | const void *s390_tdb, | |
10225 | int size) | |
10226 | { | |
10227 | char *note_name = "LINUX"; | |
10228 | return elfcore_write_note (abfd, buf, bufsiz, | |
10229 | note_name, NT_S390_TDB, s390_tdb, size); | |
10230 | } | |
10231 | ||
4ef9f41a AA |
10232 | char * |
10233 | elfcore_write_s390_vxrs_low (bfd *abfd, | |
10234 | char *buf, | |
10235 | int *bufsiz, | |
10236 | const void *s390_vxrs_low, | |
10237 | int size) | |
10238 | { | |
10239 | char *note_name = "LINUX"; | |
10240 | return elfcore_write_note (abfd, buf, bufsiz, | |
10241 | note_name, NT_S390_VXRS_LOW, s390_vxrs_low, size); | |
10242 | } | |
10243 | ||
10244 | char * | |
10245 | elfcore_write_s390_vxrs_high (bfd *abfd, | |
10246 | char *buf, | |
10247 | int *bufsiz, | |
10248 | const void *s390_vxrs_high, | |
10249 | int size) | |
10250 | { | |
10251 | char *note_name = "LINUX"; | |
10252 | return elfcore_write_note (abfd, buf, bufsiz, | |
10253 | note_name, NT_S390_VXRS_HIGH, | |
10254 | s390_vxrs_high, size); | |
10255 | } | |
10256 | ||
faa9a424 UW |
10257 | char * |
10258 | elfcore_write_arm_vfp (bfd *abfd, | |
10259 | char *buf, | |
10260 | int *bufsiz, | |
10261 | const void *arm_vfp, | |
10262 | int size) | |
10263 | { | |
10264 | char *note_name = "LINUX"; | |
10265 | return elfcore_write_note (abfd, buf, bufsiz, | |
10266 | note_name, NT_ARM_VFP, arm_vfp, size); | |
10267 | } | |
10268 | ||
652451f8 YZ |
10269 | char * |
10270 | elfcore_write_aarch_tls (bfd *abfd, | |
10271 | char *buf, | |
10272 | int *bufsiz, | |
10273 | const void *aarch_tls, | |
10274 | int size) | |
10275 | { | |
10276 | char *note_name = "LINUX"; | |
10277 | return elfcore_write_note (abfd, buf, bufsiz, | |
10278 | note_name, NT_ARM_TLS, aarch_tls, size); | |
10279 | } | |
10280 | ||
10281 | char * | |
10282 | elfcore_write_aarch_hw_break (bfd *abfd, | |
10283 | char *buf, | |
10284 | int *bufsiz, | |
10285 | const void *aarch_hw_break, | |
10286 | int size) | |
10287 | { | |
10288 | char *note_name = "LINUX"; | |
10289 | return elfcore_write_note (abfd, buf, bufsiz, | |
10290 | note_name, NT_ARM_HW_BREAK, aarch_hw_break, size); | |
10291 | } | |
10292 | ||
10293 | char * | |
10294 | elfcore_write_aarch_hw_watch (bfd *abfd, | |
10295 | char *buf, | |
10296 | int *bufsiz, | |
10297 | const void *aarch_hw_watch, | |
10298 | int size) | |
10299 | { | |
10300 | char *note_name = "LINUX"; | |
10301 | return elfcore_write_note (abfd, buf, bufsiz, | |
10302 | note_name, NT_ARM_HW_WATCH, aarch_hw_watch, size); | |
10303 | } | |
10304 | ||
bb864ac1 CES |
10305 | char * |
10306 | elfcore_write_register_note (bfd *abfd, | |
10307 | char *buf, | |
10308 | int *bufsiz, | |
10309 | const char *section, | |
10310 | const void *data, | |
10311 | int size) | |
10312 | { | |
10313 | if (strcmp (section, ".reg2") == 0) | |
10314 | return elfcore_write_prfpreg (abfd, buf, bufsiz, data, size); | |
10315 | if (strcmp (section, ".reg-xfp") == 0) | |
10316 | return elfcore_write_prxfpreg (abfd, buf, bufsiz, data, size); | |
4339cae0 L |
10317 | if (strcmp (section, ".reg-xstate") == 0) |
10318 | return elfcore_write_xstatereg (abfd, buf, bufsiz, data, size); | |
bb864ac1 CES |
10319 | if (strcmp (section, ".reg-ppc-vmx") == 0) |
10320 | return elfcore_write_ppc_vmx (abfd, buf, bufsiz, data, size); | |
89eeb0bc LM |
10321 | if (strcmp (section, ".reg-ppc-vsx") == 0) |
10322 | return elfcore_write_ppc_vsx (abfd, buf, bufsiz, data, size); | |
0675e188 UW |
10323 | if (strcmp (section, ".reg-s390-high-gprs") == 0) |
10324 | return elfcore_write_s390_high_gprs (abfd, buf, bufsiz, data, size); | |
d7eeb400 MS |
10325 | if (strcmp (section, ".reg-s390-timer") == 0) |
10326 | return elfcore_write_s390_timer (abfd, buf, bufsiz, data, size); | |
10327 | if (strcmp (section, ".reg-s390-todcmp") == 0) | |
10328 | return elfcore_write_s390_todcmp (abfd, buf, bufsiz, data, size); | |
10329 | if (strcmp (section, ".reg-s390-todpreg") == 0) | |
10330 | return elfcore_write_s390_todpreg (abfd, buf, bufsiz, data, size); | |
10331 | if (strcmp (section, ".reg-s390-ctrs") == 0) | |
10332 | return elfcore_write_s390_ctrs (abfd, buf, bufsiz, data, size); | |
10333 | if (strcmp (section, ".reg-s390-prefix") == 0) | |
10334 | return elfcore_write_s390_prefix (abfd, buf, bufsiz, data, size); | |
355b81d9 UW |
10335 | if (strcmp (section, ".reg-s390-last-break") == 0) |
10336 | return elfcore_write_s390_last_break (abfd, buf, bufsiz, data, size); | |
10337 | if (strcmp (section, ".reg-s390-system-call") == 0) | |
10338 | return elfcore_write_s390_system_call (abfd, buf, bufsiz, data, size); | |
abb3f6cc NC |
10339 | if (strcmp (section, ".reg-s390-tdb") == 0) |
10340 | return elfcore_write_s390_tdb (abfd, buf, bufsiz, data, size); | |
4ef9f41a AA |
10341 | if (strcmp (section, ".reg-s390-vxrs-low") == 0) |
10342 | return elfcore_write_s390_vxrs_low (abfd, buf, bufsiz, data, size); | |
10343 | if (strcmp (section, ".reg-s390-vxrs-high") == 0) | |
10344 | return elfcore_write_s390_vxrs_high (abfd, buf, bufsiz, data, size); | |
faa9a424 UW |
10345 | if (strcmp (section, ".reg-arm-vfp") == 0) |
10346 | return elfcore_write_arm_vfp (abfd, buf, bufsiz, data, size); | |
652451f8 YZ |
10347 | if (strcmp (section, ".reg-aarch-tls") == 0) |
10348 | return elfcore_write_aarch_tls (abfd, buf, bufsiz, data, size); | |
10349 | if (strcmp (section, ".reg-aarch-hw-break") == 0) | |
10350 | return elfcore_write_aarch_hw_break (abfd, buf, bufsiz, data, size); | |
10351 | if (strcmp (section, ".reg-aarch-hw-watch") == 0) | |
10352 | return elfcore_write_aarch_hw_watch (abfd, buf, bufsiz, data, size); | |
bb864ac1 CES |
10353 | return NULL; |
10354 | } | |
10355 | ||
b34976b6 | 10356 | static bfd_boolean |
718175fa | 10357 | elf_parse_notes (bfd *abfd, char *buf, size_t size, file_ptr offset) |
252b5132 | 10358 | { |
c044fabd | 10359 | char *p; |
252b5132 | 10360 | |
252b5132 RH |
10361 | p = buf; |
10362 | while (p < buf + size) | |
10363 | { | |
c044fabd KH |
10364 | /* FIXME: bad alignment assumption. */ |
10365 | Elf_External_Note *xnp = (Elf_External_Note *) p; | |
252b5132 RH |
10366 | Elf_Internal_Note in; |
10367 | ||
baea7ef1 AM |
10368 | if (offsetof (Elf_External_Note, name) > buf - p + size) |
10369 | return FALSE; | |
10370 | ||
dc810e39 | 10371 | in.type = H_GET_32 (abfd, xnp->type); |
252b5132 | 10372 | |
dc810e39 | 10373 | in.namesz = H_GET_32 (abfd, xnp->namesz); |
252b5132 | 10374 | in.namedata = xnp->name; |
baea7ef1 AM |
10375 | if (in.namesz > buf - in.namedata + size) |
10376 | return FALSE; | |
252b5132 | 10377 | |
dc810e39 | 10378 | in.descsz = H_GET_32 (abfd, xnp->descsz); |
252b5132 RH |
10379 | in.descdata = in.namedata + BFD_ALIGN (in.namesz, 4); |
10380 | in.descpos = offset + (in.descdata - buf); | |
baea7ef1 AM |
10381 | if (in.descsz != 0 |
10382 | && (in.descdata >= buf + size | |
10383 | || in.descsz > buf - in.descdata + size)) | |
10384 | return FALSE; | |
252b5132 | 10385 | |
718175fa JK |
10386 | switch (bfd_get_format (abfd)) |
10387 | { | |
10388 | default: | |
10389 | return TRUE; | |
10390 | ||
10391 | case bfd_core: | |
f64e188b | 10392 | { |
8acbedd6 | 10393 | #define GROKER_ELEMENT(S,F) {S, sizeof (S) - 1, F} |
f64e188b | 10394 | struct |
718175fa | 10395 | { |
f64e188b | 10396 | const char * string; |
8acbedd6 | 10397 | size_t len; |
f64e188b | 10398 | bfd_boolean (* func)(bfd *, Elf_Internal_Note *); |
718175fa | 10399 | } |
f64e188b | 10400 | grokers[] = |
b15fa79e | 10401 | { |
8acbedd6 KS |
10402 | GROKER_ELEMENT ("", elfcore_grok_note), |
10403 | GROKER_ELEMENT ("NetBSD-CORE", elfcore_grok_netbsd_note), | |
10404 | GROKER_ELEMENT ( "OpenBSD", elfcore_grok_openbsd_note), | |
10405 | GROKER_ELEMENT ("QNX", elfcore_grok_nto_note), | |
10406 | GROKER_ELEMENT ("SPU/", elfcore_grok_spu_note) | |
f64e188b | 10407 | }; |
8acbedd6 | 10408 | #undef GROKER_ELEMENT |
f64e188b NC |
10409 | int i; |
10410 | ||
10411 | for (i = ARRAY_SIZE (grokers); i--;) | |
8acbedd6 KS |
10412 | { |
10413 | if (in.namesz >= grokers[i].len | |
10414 | && strncmp (in.namedata, grokers[i].string, | |
10415 | grokers[i].len) == 0) | |
10416 | { | |
10417 | if (! grokers[i].func (abfd, & in)) | |
10418 | return FALSE; | |
10419 | break; | |
10420 | } | |
10421 | } | |
f64e188b NC |
10422 | break; |
10423 | } | |
718175fa JK |
10424 | |
10425 | case bfd_object: | |
10426 | if (in.namesz == sizeof "GNU" && strcmp (in.namedata, "GNU") == 0) | |
10427 | { | |
10428 | if (! elfobj_grok_gnu_note (abfd, &in)) | |
10429 | return FALSE; | |
10430 | } | |
e21e5835 NC |
10431 | else if (in.namesz == sizeof "stapsdt" |
10432 | && strcmp (in.namedata, "stapsdt") == 0) | |
10433 | { | |
10434 | if (! elfobj_grok_stapsdt_note (abfd, &in)) | |
10435 | return FALSE; | |
10436 | } | |
718175fa | 10437 | break; |
08a40648 | 10438 | } |
252b5132 RH |
10439 | |
10440 | p = in.descdata + BFD_ALIGN (in.descsz, 4); | |
10441 | } | |
10442 | ||
718175fa JK |
10443 | return TRUE; |
10444 | } | |
10445 | ||
10446 | static bfd_boolean | |
10447 | elf_read_notes (bfd *abfd, file_ptr offset, bfd_size_type size) | |
10448 | { | |
10449 | char *buf; | |
10450 | ||
10451 | if (size <= 0) | |
10452 | return TRUE; | |
10453 | ||
10454 | if (bfd_seek (abfd, offset, SEEK_SET) != 0) | |
10455 | return FALSE; | |
10456 | ||
f64e188b | 10457 | buf = (char *) bfd_malloc (size + 1); |
718175fa JK |
10458 | if (buf == NULL) |
10459 | return FALSE; | |
10460 | ||
f64e188b NC |
10461 | /* PR 17512: file: ec08f814 |
10462 | 0-termintate the buffer so that string searches will not overflow. */ | |
10463 | buf[size] = 0; | |
10464 | ||
718175fa JK |
10465 | if (bfd_bread (buf, size, abfd) != size |
10466 | || !elf_parse_notes (abfd, buf, size, offset)) | |
10467 | { | |
10468 | free (buf); | |
10469 | return FALSE; | |
10470 | } | |
10471 | ||
252b5132 | 10472 | free (buf); |
b34976b6 | 10473 | return TRUE; |
252b5132 | 10474 | } |
98d8431c JB |
10475 | \f |
10476 | /* Providing external access to the ELF program header table. */ | |
10477 | ||
10478 | /* Return an upper bound on the number of bytes required to store a | |
10479 | copy of ABFD's program header table entries. Return -1 if an error | |
10480 | occurs; bfd_get_error will return an appropriate code. */ | |
c044fabd | 10481 | |
98d8431c | 10482 | long |
217aa764 | 10483 | bfd_get_elf_phdr_upper_bound (bfd *abfd) |
98d8431c JB |
10484 | { |
10485 | if (abfd->xvec->flavour != bfd_target_elf_flavour) | |
10486 | { | |
10487 | bfd_set_error (bfd_error_wrong_format); | |
10488 | return -1; | |
10489 | } | |
10490 | ||
936e320b | 10491 | return elf_elfheader (abfd)->e_phnum * sizeof (Elf_Internal_Phdr); |
98d8431c JB |
10492 | } |
10493 | ||
98d8431c JB |
10494 | /* Copy ABFD's program header table entries to *PHDRS. The entries |
10495 | will be stored as an array of Elf_Internal_Phdr structures, as | |
10496 | defined in include/elf/internal.h. To find out how large the | |
10497 | buffer needs to be, call bfd_get_elf_phdr_upper_bound. | |
10498 | ||
10499 | Return the number of program header table entries read, or -1 if an | |
10500 | error occurs; bfd_get_error will return an appropriate code. */ | |
c044fabd | 10501 | |
98d8431c | 10502 | int |
217aa764 | 10503 | bfd_get_elf_phdrs (bfd *abfd, void *phdrs) |
98d8431c JB |
10504 | { |
10505 | int num_phdrs; | |
10506 | ||
10507 | if (abfd->xvec->flavour != bfd_target_elf_flavour) | |
10508 | { | |
10509 | bfd_set_error (bfd_error_wrong_format); | |
10510 | return -1; | |
10511 | } | |
10512 | ||
10513 | num_phdrs = elf_elfheader (abfd)->e_phnum; | |
c044fabd | 10514 | memcpy (phdrs, elf_tdata (abfd)->phdr, |
98d8431c JB |
10515 | num_phdrs * sizeof (Elf_Internal_Phdr)); |
10516 | ||
10517 | return num_phdrs; | |
10518 | } | |
ae4221d7 | 10519 | |
db6751f2 | 10520 | enum elf_reloc_type_class |
7e612e98 AM |
10521 | _bfd_elf_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED, |
10522 | const asection *rel_sec ATTRIBUTE_UNUSED, | |
10523 | const Elf_Internal_Rela *rela ATTRIBUTE_UNUSED) | |
db6751f2 JJ |
10524 | { |
10525 | return reloc_class_normal; | |
10526 | } | |
f8df10f4 | 10527 | |
47d9a591 | 10528 | /* For RELA architectures, return the relocation value for a |
f8df10f4 JJ |
10529 | relocation against a local symbol. */ |
10530 | ||
10531 | bfd_vma | |
217aa764 AM |
10532 | _bfd_elf_rela_local_sym (bfd *abfd, |
10533 | Elf_Internal_Sym *sym, | |
8517fae7 | 10534 | asection **psec, |
217aa764 | 10535 | Elf_Internal_Rela *rel) |
f8df10f4 | 10536 | { |
8517fae7 | 10537 | asection *sec = *psec; |
f8df10f4 JJ |
10538 | bfd_vma relocation; |
10539 | ||
10540 | relocation = (sec->output_section->vma | |
10541 | + sec->output_offset | |
10542 | + sym->st_value); | |
10543 | if ((sec->flags & SEC_MERGE) | |
c629eae0 | 10544 | && ELF_ST_TYPE (sym->st_info) == STT_SECTION |
dbaa2011 | 10545 | && sec->sec_info_type == SEC_INFO_TYPE_MERGE) |
f8df10f4 | 10546 | { |
f8df10f4 | 10547 | rel->r_addend = |
8517fae7 | 10548 | _bfd_merged_section_offset (abfd, psec, |
65765700 | 10549 | elf_section_data (sec)->sec_info, |
753731ee AM |
10550 | sym->st_value + rel->r_addend); |
10551 | if (sec != *psec) | |
10552 | { | |
10553 | /* If we have changed the section, and our original section is | |
10554 | marked with SEC_EXCLUDE, it means that the original | |
10555 | SEC_MERGE section has been completely subsumed in some | |
10556 | other SEC_MERGE section. In this case, we need to leave | |
10557 | some info around for --emit-relocs. */ | |
10558 | if ((sec->flags & SEC_EXCLUDE) != 0) | |
10559 | sec->kept_section = *psec; | |
10560 | sec = *psec; | |
10561 | } | |
8517fae7 AM |
10562 | rel->r_addend -= relocation; |
10563 | rel->r_addend += sec->output_section->vma + sec->output_offset; | |
f8df10f4 JJ |
10564 | } |
10565 | return relocation; | |
10566 | } | |
c629eae0 JJ |
10567 | |
10568 | bfd_vma | |
217aa764 AM |
10569 | _bfd_elf_rel_local_sym (bfd *abfd, |
10570 | Elf_Internal_Sym *sym, | |
10571 | asection **psec, | |
10572 | bfd_vma addend) | |
47d9a591 | 10573 | { |
c629eae0 JJ |
10574 | asection *sec = *psec; |
10575 | ||
dbaa2011 | 10576 | if (sec->sec_info_type != SEC_INFO_TYPE_MERGE) |
c629eae0 JJ |
10577 | return sym->st_value + addend; |
10578 | ||
10579 | return _bfd_merged_section_offset (abfd, psec, | |
65765700 | 10580 | elf_section_data (sec)->sec_info, |
753731ee | 10581 | sym->st_value + addend); |
c629eae0 JJ |
10582 | } |
10583 | ||
37b01f6a DG |
10584 | /* Adjust an address within a section. Given OFFSET within SEC, return |
10585 | the new offset within the section, based upon changes made to the | |
10586 | section. Returns -1 if the offset is now invalid. | |
10587 | The offset (in abnd out) is in target sized bytes, however big a | |
10588 | byte may be. */ | |
10589 | ||
c629eae0 | 10590 | bfd_vma |
217aa764 | 10591 | _bfd_elf_section_offset (bfd *abfd, |
92e4ec35 | 10592 | struct bfd_link_info *info, |
217aa764 AM |
10593 | asection *sec, |
10594 | bfd_vma offset) | |
c629eae0 | 10595 | { |
68bfbfcc | 10596 | switch (sec->sec_info_type) |
65765700 | 10597 | { |
dbaa2011 | 10598 | case SEC_INFO_TYPE_STABS: |
eea6121a AM |
10599 | return _bfd_stab_section_offset (sec, elf_section_data (sec)->sec_info, |
10600 | offset); | |
dbaa2011 | 10601 | case SEC_INFO_TYPE_EH_FRAME: |
92e4ec35 | 10602 | return _bfd_elf_eh_frame_section_offset (abfd, info, sec, offset); |
37b01f6a | 10603 | |
65765700 | 10604 | default: |
310fd250 L |
10605 | if ((sec->flags & SEC_ELF_REVERSE_COPY) != 0) |
10606 | { | |
37b01f6a | 10607 | /* Reverse the offset. */ |
310fd250 L |
10608 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
10609 | bfd_size_type address_size = bed->s->arch_size / 8; | |
37b01f6a DG |
10610 | |
10611 | /* address_size and sec->size are in octets. Convert | |
10612 | to bytes before subtracting the original offset. */ | |
10613 | offset = (sec->size - address_size) / bfd_octets_per_byte (abfd) - offset; | |
310fd250 | 10614 | } |
65765700 JJ |
10615 | return offset; |
10616 | } | |
c629eae0 | 10617 | } |
3333a7c3 RM |
10618 | \f |
10619 | /* Create a new BFD as if by bfd_openr. Rather than opening a file, | |
10620 | reconstruct an ELF file by reading the segments out of remote memory | |
10621 | based on the ELF file header at EHDR_VMA and the ELF program headers it | |
10622 | points to. If not null, *LOADBASEP is filled in with the difference | |
10623 | between the VMAs from which the segments were read, and the VMAs the | |
10624 | file headers (and hence BFD's idea of each section's VMA) put them at. | |
10625 | ||
10626 | The function TARGET_READ_MEMORY is called to copy LEN bytes from the | |
10627 | remote memory at target address VMA into the local buffer at MYADDR; it | |
10628 | should return zero on success or an `errno' code on failure. TEMPL must | |
10629 | be a BFD for an ELF target with the word size and byte order found in | |
10630 | the remote memory. */ | |
10631 | ||
10632 | bfd * | |
217aa764 AM |
10633 | bfd_elf_bfd_from_remote_memory |
10634 | (bfd *templ, | |
10635 | bfd_vma ehdr_vma, | |
f0a5d95a | 10636 | bfd_size_type size, |
217aa764 | 10637 | bfd_vma *loadbasep, |
fe78531d | 10638 | int (*target_read_memory) (bfd_vma, bfd_byte *, bfd_size_type)) |
3333a7c3 RM |
10639 | { |
10640 | return (*get_elf_backend_data (templ)->elf_backend_bfd_from_remote_memory) | |
5979d6b6 | 10641 | (templ, ehdr_vma, size, loadbasep, target_read_memory); |
3333a7c3 | 10642 | } |
4c45e5c9 JJ |
10643 | \f |
10644 | long | |
c9727e01 AM |
10645 | _bfd_elf_get_synthetic_symtab (bfd *abfd, |
10646 | long symcount ATTRIBUTE_UNUSED, | |
10647 | asymbol **syms ATTRIBUTE_UNUSED, | |
8615f3f2 | 10648 | long dynsymcount, |
c9727e01 AM |
10649 | asymbol **dynsyms, |
10650 | asymbol **ret) | |
4c45e5c9 JJ |
10651 | { |
10652 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
10653 | asection *relplt; | |
10654 | asymbol *s; | |
10655 | const char *relplt_name; | |
10656 | bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean); | |
10657 | arelent *p; | |
10658 | long count, i, n; | |
10659 | size_t size; | |
10660 | Elf_Internal_Shdr *hdr; | |
10661 | char *names; | |
10662 | asection *plt; | |
10663 | ||
8615f3f2 AM |
10664 | *ret = NULL; |
10665 | ||
90e3cdf2 JJ |
10666 | if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0) |
10667 | return 0; | |
10668 | ||
8615f3f2 AM |
10669 | if (dynsymcount <= 0) |
10670 | return 0; | |
10671 | ||
4c45e5c9 JJ |
10672 | if (!bed->plt_sym_val) |
10673 | return 0; | |
10674 | ||
10675 | relplt_name = bed->relplt_name; | |
10676 | if (relplt_name == NULL) | |
d35fd659 | 10677 | relplt_name = bed->rela_plts_and_copies_p ? ".rela.plt" : ".rel.plt"; |
4c45e5c9 JJ |
10678 | relplt = bfd_get_section_by_name (abfd, relplt_name); |
10679 | if (relplt == NULL) | |
10680 | return 0; | |
10681 | ||
10682 | hdr = &elf_section_data (relplt)->this_hdr; | |
10683 | if (hdr->sh_link != elf_dynsymtab (abfd) | |
10684 | || (hdr->sh_type != SHT_REL && hdr->sh_type != SHT_RELA)) | |
10685 | return 0; | |
10686 | ||
10687 | plt = bfd_get_section_by_name (abfd, ".plt"); | |
10688 | if (plt == NULL) | |
10689 | return 0; | |
10690 | ||
10691 | slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table; | |
c9727e01 | 10692 | if (! (*slurp_relocs) (abfd, relplt, dynsyms, TRUE)) |
4c45e5c9 JJ |
10693 | return -1; |
10694 | ||
eea6121a | 10695 | count = relplt->size / hdr->sh_entsize; |
4c45e5c9 JJ |
10696 | size = count * sizeof (asymbol); |
10697 | p = relplt->relocation; | |
cb53bf42 | 10698 | for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel) |
041de40d AM |
10699 | { |
10700 | size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt"); | |
10701 | if (p->addend != 0) | |
10702 | { | |
10703 | #ifdef BFD64 | |
10704 | size += sizeof ("+0x") - 1 + 8 + 8 * (bed->s->elfclass == ELFCLASS64); | |
10705 | #else | |
10706 | size += sizeof ("+0x") - 1 + 8; | |
10707 | #endif | |
10708 | } | |
10709 | } | |
4c45e5c9 | 10710 | |
a50b1753 | 10711 | s = *ret = (asymbol *) bfd_malloc (size); |
4c45e5c9 JJ |
10712 | if (s == NULL) |
10713 | return -1; | |
10714 | ||
10715 | names = (char *) (s + count); | |
10716 | p = relplt->relocation; | |
10717 | n = 0; | |
cb53bf42 | 10718 | for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel) |
4c45e5c9 JJ |
10719 | { |
10720 | size_t len; | |
10721 | bfd_vma addr; | |
10722 | ||
10723 | addr = bed->plt_sym_val (i, plt, p); | |
10724 | if (addr == (bfd_vma) -1) | |
10725 | continue; | |
10726 | ||
10727 | *s = **p->sym_ptr_ptr; | |
65a7a66f AM |
10728 | /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since |
10729 | we are defining a symbol, ensure one of them is set. */ | |
10730 | if ((s->flags & BSF_LOCAL) == 0) | |
10731 | s->flags |= BSF_GLOBAL; | |
6ba2a415 | 10732 | s->flags |= BSF_SYNTHETIC; |
4c45e5c9 JJ |
10733 | s->section = plt; |
10734 | s->value = addr - plt->vma; | |
10735 | s->name = names; | |
8f39ba8e | 10736 | s->udata.p = NULL; |
4c45e5c9 JJ |
10737 | len = strlen ((*p->sym_ptr_ptr)->name); |
10738 | memcpy (names, (*p->sym_ptr_ptr)->name, len); | |
10739 | names += len; | |
041de40d AM |
10740 | if (p->addend != 0) |
10741 | { | |
1d770845 | 10742 | char buf[30], *a; |
d324f6d6 | 10743 | |
041de40d AM |
10744 | memcpy (names, "+0x", sizeof ("+0x") - 1); |
10745 | names += sizeof ("+0x") - 1; | |
1d770845 L |
10746 | bfd_sprintf_vma (abfd, buf, p->addend); |
10747 | for (a = buf; *a == '0'; ++a) | |
10748 | ; | |
10749 | len = strlen (a); | |
10750 | memcpy (names, a, len); | |
10751 | names += len; | |
041de40d | 10752 | } |
4c45e5c9 JJ |
10753 | memcpy (names, "@plt", sizeof ("@plt")); |
10754 | names += sizeof ("@plt"); | |
8f39ba8e | 10755 | ++s, ++n; |
4c45e5c9 JJ |
10756 | } |
10757 | ||
10758 | return n; | |
10759 | } | |
3d7f7666 | 10760 | |
3b22753a L |
10761 | /* It is only used by x86-64 so far. */ |
10762 | asection _bfd_elf_large_com_section | |
10763 | = BFD_FAKE_SECTION (_bfd_elf_large_com_section, | |
f592407e | 10764 | SEC_IS_COMMON, NULL, "LARGE_COMMON", 0); |
ecca9871 | 10765 | |
d1036acb | 10766 | void |
78245035 L |
10767 | _bfd_elf_post_process_headers (bfd * abfd, |
10768 | struct bfd_link_info * link_info ATTRIBUTE_UNUSED) | |
d1036acb L |
10769 | { |
10770 | Elf_Internal_Ehdr * i_ehdrp; /* ELF file header, internal form. */ | |
10771 | ||
10772 | i_ehdrp = elf_elfheader (abfd); | |
10773 | ||
10774 | i_ehdrp->e_ident[EI_OSABI] = get_elf_backend_data (abfd)->elf_osabi; | |
d8045f23 NC |
10775 | |
10776 | /* To make things simpler for the loader on Linux systems we set the | |
9c55345c | 10777 | osabi field to ELFOSABI_GNU if the binary contains symbols of |
f64b2e8d | 10778 | the STT_GNU_IFUNC type or STB_GNU_UNIQUE binding. */ |
d8045f23 | 10779 | if (i_ehdrp->e_ident[EI_OSABI] == ELFOSABI_NONE |
f64b2e8d | 10780 | && elf_tdata (abfd)->has_gnu_symbols) |
9c55345c | 10781 | i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_GNU; |
d1036acb | 10782 | } |
fcb93ecf PB |
10783 | |
10784 | ||
10785 | /* Return TRUE for ELF symbol types that represent functions. | |
10786 | This is the default version of this function, which is sufficient for | |
d8045f23 | 10787 | most targets. It returns true if TYPE is STT_FUNC or STT_GNU_IFUNC. */ |
fcb93ecf PB |
10788 | |
10789 | bfd_boolean | |
10790 | _bfd_elf_is_function_type (unsigned int type) | |
10791 | { | |
d8045f23 NC |
10792 | return (type == STT_FUNC |
10793 | || type == STT_GNU_IFUNC); | |
fcb93ecf | 10794 | } |
9f296da3 | 10795 | |
aef36ac1 AM |
10796 | /* If the ELF symbol SYM might be a function in SEC, return the |
10797 | function size and set *CODE_OFF to the function's entry point, | |
10798 | otherwise return zero. */ | |
9f296da3 | 10799 | |
aef36ac1 AM |
10800 | bfd_size_type |
10801 | _bfd_elf_maybe_function_sym (const asymbol *sym, asection *sec, | |
10802 | bfd_vma *code_off) | |
9f296da3 | 10803 | { |
aef36ac1 AM |
10804 | bfd_size_type size; |
10805 | ||
ff9e0f5b | 10806 | if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT |
aef36ac1 AM |
10807 | | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0 |
10808 | || sym->section != sec) | |
10809 | return 0; | |
ff9e0f5b | 10810 | |
ff9e0f5b | 10811 | *code_off = sym->value; |
aef36ac1 AM |
10812 | size = 0; |
10813 | if (!(sym->flags & BSF_SYNTHETIC)) | |
10814 | size = ((elf_symbol_type *) sym)->internal_elf_sym.st_size; | |
10815 | if (size == 0) | |
10816 | size = 1; | |
10817 | return size; | |
9f296da3 | 10818 | } |