Commit | Line | Data |
---|---|---|
252b5132 | 1 | /* ELF executable support for BFD. |
340b6d91 | 2 | |
2571583a | 3 | Copyright (C) 1993-2017 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. */ | |
695344c0 | 341 | /* xgettext:c-format */ |
06614111 NC |
342 | _bfd_error_handler (_("%B: attempt to load strings from a non-string section (number %d)"), |
343 | abfd, shindex); | |
344 | return NULL; | |
345 | } | |
b1fa9dd6 | 346 | |
06614111 NC |
347 | if (bfd_elf_get_str_section (abfd, shindex) == NULL) |
348 | return NULL; | |
349 | } | |
252b5132 RH |
350 | |
351 | if (strindex >= hdr->sh_size) | |
352 | { | |
1b3a8575 | 353 | unsigned int shstrndx = elf_elfheader(abfd)->e_shstrndx; |
4eca0228 | 354 | _bfd_error_handler |
695344c0 | 355 | /* xgettext:c-format */ |
d003868e AM |
356 | (_("%B: invalid string offset %u >= %lu for section `%s'"), |
357 | abfd, strindex, (unsigned long) hdr->sh_size, | |
1b3a8575 | 358 | (shindex == shstrndx && strindex == hdr->sh_name |
252b5132 | 359 | ? ".shstrtab" |
1b3a8575 | 360 | : bfd_elf_string_from_elf_section (abfd, shstrndx, hdr->sh_name))); |
45b222d6 | 361 | return NULL; |
252b5132 RH |
362 | } |
363 | ||
364 | return ((char *) hdr->contents) + strindex; | |
365 | } | |
366 | ||
6cdc0ccc AM |
367 | /* Read and convert symbols to internal format. |
368 | SYMCOUNT specifies the number of symbols to read, starting from | |
369 | symbol SYMOFFSET. If any of INTSYM_BUF, EXTSYM_BUF or EXTSHNDX_BUF | |
370 | are non-NULL, they are used to store the internal symbols, external | |
b7c368d0 NC |
371 | symbols, and symbol section index extensions, respectively. |
372 | Returns a pointer to the internal symbol buffer (malloced if necessary) | |
373 | or NULL if there were no symbols or some kind of problem. */ | |
6cdc0ccc AM |
374 | |
375 | Elf_Internal_Sym * | |
217aa764 AM |
376 | bfd_elf_get_elf_syms (bfd *ibfd, |
377 | Elf_Internal_Shdr *symtab_hdr, | |
378 | size_t symcount, | |
379 | size_t symoffset, | |
380 | Elf_Internal_Sym *intsym_buf, | |
381 | void *extsym_buf, | |
382 | Elf_External_Sym_Shndx *extshndx_buf) | |
6cdc0ccc AM |
383 | { |
384 | Elf_Internal_Shdr *shndx_hdr; | |
217aa764 | 385 | void *alloc_ext; |
df622259 | 386 | const bfd_byte *esym; |
6cdc0ccc AM |
387 | Elf_External_Sym_Shndx *alloc_extshndx; |
388 | Elf_External_Sym_Shndx *shndx; | |
4dd07732 | 389 | Elf_Internal_Sym *alloc_intsym; |
6cdc0ccc AM |
390 | Elf_Internal_Sym *isym; |
391 | Elf_Internal_Sym *isymend; | |
9c5bfbb7 | 392 | const struct elf_backend_data *bed; |
6cdc0ccc AM |
393 | size_t extsym_size; |
394 | bfd_size_type amt; | |
395 | file_ptr pos; | |
396 | ||
e44a2c9c AM |
397 | if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour) |
398 | abort (); | |
399 | ||
6cdc0ccc AM |
400 | if (symcount == 0) |
401 | return intsym_buf; | |
402 | ||
403 | /* Normal syms might have section extension entries. */ | |
404 | shndx_hdr = NULL; | |
6a40cf0c NC |
405 | if (elf_symtab_shndx_list (ibfd) != NULL) |
406 | { | |
407 | elf_section_list * entry; | |
408 | Elf_Internal_Shdr **sections = elf_elfsections (ibfd); | |
409 | ||
410 | /* Find an index section that is linked to this symtab section. */ | |
411 | for (entry = elf_symtab_shndx_list (ibfd); entry != NULL; entry = entry->next) | |
315350be NC |
412 | { |
413 | /* PR 20063. */ | |
414 | if (entry->hdr.sh_link >= elf_numsections (ibfd)) | |
415 | continue; | |
416 | ||
417 | if (sections[entry->hdr.sh_link] == symtab_hdr) | |
418 | { | |
419 | shndx_hdr = & entry->hdr; | |
420 | break; | |
421 | }; | |
422 | } | |
6a40cf0c NC |
423 | |
424 | if (shndx_hdr == NULL) | |
425 | { | |
426 | if (symtab_hdr == & elf_symtab_hdr (ibfd)) | |
427 | /* Not really accurate, but this was how the old code used to work. */ | |
428 | shndx_hdr = & elf_symtab_shndx_list (ibfd)->hdr; | |
429 | /* Otherwise we do nothing. The assumption is that | |
430 | the index table will not be needed. */ | |
431 | } | |
432 | } | |
6cdc0ccc AM |
433 | |
434 | /* Read the symbols. */ | |
435 | alloc_ext = NULL; | |
436 | alloc_extshndx = NULL; | |
4dd07732 | 437 | alloc_intsym = NULL; |
6cdc0ccc AM |
438 | bed = get_elf_backend_data (ibfd); |
439 | extsym_size = bed->s->sizeof_sym; | |
ef53be89 | 440 | amt = (bfd_size_type) symcount * extsym_size; |
6cdc0ccc AM |
441 | pos = symtab_hdr->sh_offset + symoffset * extsym_size; |
442 | if (extsym_buf == NULL) | |
443 | { | |
d0fb9a8d | 444 | alloc_ext = bfd_malloc2 (symcount, extsym_size); |
6cdc0ccc AM |
445 | extsym_buf = alloc_ext; |
446 | } | |
447 | if (extsym_buf == NULL | |
448 | || bfd_seek (ibfd, pos, SEEK_SET) != 0 | |
449 | || bfd_bread (extsym_buf, amt, ibfd) != amt) | |
450 | { | |
451 | intsym_buf = NULL; | |
452 | goto out; | |
453 | } | |
454 | ||
455 | if (shndx_hdr == NULL || shndx_hdr->sh_size == 0) | |
456 | extshndx_buf = NULL; | |
457 | else | |
458 | { | |
ef53be89 | 459 | amt = (bfd_size_type) symcount * sizeof (Elf_External_Sym_Shndx); |
6cdc0ccc AM |
460 | pos = shndx_hdr->sh_offset + symoffset * sizeof (Elf_External_Sym_Shndx); |
461 | if (extshndx_buf == NULL) | |
462 | { | |
a50b1753 NC |
463 | alloc_extshndx = (Elf_External_Sym_Shndx *) |
464 | bfd_malloc2 (symcount, sizeof (Elf_External_Sym_Shndx)); | |
6cdc0ccc AM |
465 | extshndx_buf = alloc_extshndx; |
466 | } | |
467 | if (extshndx_buf == NULL | |
468 | || bfd_seek (ibfd, pos, SEEK_SET) != 0 | |
469 | || bfd_bread (extshndx_buf, amt, ibfd) != amt) | |
470 | { | |
471 | intsym_buf = NULL; | |
472 | goto out; | |
473 | } | |
474 | } | |
475 | ||
476 | if (intsym_buf == NULL) | |
477 | { | |
a50b1753 NC |
478 | alloc_intsym = (Elf_Internal_Sym *) |
479 | bfd_malloc2 (symcount, sizeof (Elf_Internal_Sym)); | |
4dd07732 | 480 | intsym_buf = alloc_intsym; |
6cdc0ccc AM |
481 | if (intsym_buf == NULL) |
482 | goto out; | |
483 | } | |
484 | ||
485 | /* Convert the symbols to internal form. */ | |
486 | isymend = intsym_buf + symcount; | |
a50b1753 NC |
487 | for (esym = (const bfd_byte *) extsym_buf, isym = intsym_buf, |
488 | shndx = extshndx_buf; | |
6cdc0ccc AM |
489 | isym < isymend; |
490 | esym += extsym_size, isym++, shndx = shndx != NULL ? shndx + 1 : NULL) | |
8384fb8f AM |
491 | if (!(*bed->s->swap_symbol_in) (ibfd, esym, shndx, isym)) |
492 | { | |
493 | symoffset += (esym - (bfd_byte *) extsym_buf) / extsym_size; | |
695344c0 | 494 | /* xgettext:c-format */ |
4eca0228 AM |
495 | _bfd_error_handler (_("%B symbol number %lu references " |
496 | "nonexistent SHT_SYMTAB_SHNDX section"), | |
497 | ibfd, (unsigned long) symoffset); | |
4dd07732 AM |
498 | if (alloc_intsym != NULL) |
499 | free (alloc_intsym); | |
8384fb8f AM |
500 | intsym_buf = NULL; |
501 | goto out; | |
502 | } | |
6cdc0ccc AM |
503 | |
504 | out: | |
505 | if (alloc_ext != NULL) | |
506 | free (alloc_ext); | |
507 | if (alloc_extshndx != NULL) | |
508 | free (alloc_extshndx); | |
509 | ||
510 | return intsym_buf; | |
511 | } | |
512 | ||
5cab59f6 AM |
513 | /* Look up a symbol name. */ |
514 | const char * | |
be8dd2ca AM |
515 | bfd_elf_sym_name (bfd *abfd, |
516 | Elf_Internal_Shdr *symtab_hdr, | |
26c61ae5 L |
517 | Elf_Internal_Sym *isym, |
518 | asection *sym_sec) | |
5cab59f6 | 519 | { |
26c61ae5 | 520 | const char *name; |
5cab59f6 | 521 | unsigned int iname = isym->st_name; |
be8dd2ca | 522 | unsigned int shindex = symtab_hdr->sh_link; |
26c61ae5 | 523 | |
138f35cc JJ |
524 | if (iname == 0 && ELF_ST_TYPE (isym->st_info) == STT_SECTION |
525 | /* Check for a bogus st_shndx to avoid crashing. */ | |
4fbb74a6 | 526 | && isym->st_shndx < elf_numsections (abfd)) |
5cab59f6 AM |
527 | { |
528 | iname = elf_elfsections (abfd)[isym->st_shndx]->sh_name; | |
529 | shindex = elf_elfheader (abfd)->e_shstrndx; | |
530 | } | |
531 | ||
26c61ae5 L |
532 | name = bfd_elf_string_from_elf_section (abfd, shindex, iname); |
533 | if (name == NULL) | |
534 | name = "(null)"; | |
535 | else if (sym_sec && *name == '\0') | |
536 | name = bfd_section_name (abfd, sym_sec); | |
537 | ||
538 | return name; | |
5cab59f6 AM |
539 | } |
540 | ||
dbb410c3 AM |
541 | /* Elf_Internal_Shdr->contents is an array of these for SHT_GROUP |
542 | sections. The first element is the flags, the rest are section | |
543 | pointers. */ | |
544 | ||
545 | typedef union elf_internal_group { | |
546 | Elf_Internal_Shdr *shdr; | |
547 | unsigned int flags; | |
548 | } Elf_Internal_Group; | |
549 | ||
b885599b AM |
550 | /* Return the name of the group signature symbol. Why isn't the |
551 | signature just a string? */ | |
552 | ||
553 | static const char * | |
217aa764 | 554 | group_signature (bfd *abfd, Elf_Internal_Shdr *ghdr) |
b885599b | 555 | { |
9dce4196 | 556 | Elf_Internal_Shdr *hdr; |
9dce4196 AM |
557 | unsigned char esym[sizeof (Elf64_External_Sym)]; |
558 | Elf_External_Sym_Shndx eshndx; | |
559 | Elf_Internal_Sym isym; | |
b885599b | 560 | |
13792e9d L |
561 | /* First we need to ensure the symbol table is available. Make sure |
562 | that it is a symbol table section. */ | |
4fbb74a6 AM |
563 | if (ghdr->sh_link >= elf_numsections (abfd)) |
564 | return NULL; | |
13792e9d L |
565 | hdr = elf_elfsections (abfd) [ghdr->sh_link]; |
566 | if (hdr->sh_type != SHT_SYMTAB | |
567 | || ! bfd_section_from_shdr (abfd, ghdr->sh_link)) | |
b885599b AM |
568 | return NULL; |
569 | ||
9dce4196 AM |
570 | /* Go read the symbol. */ |
571 | hdr = &elf_tdata (abfd)->symtab_hdr; | |
6cdc0ccc AM |
572 | if (bfd_elf_get_elf_syms (abfd, hdr, 1, ghdr->sh_info, |
573 | &isym, esym, &eshndx) == NULL) | |
b885599b | 574 | return NULL; |
9dce4196 | 575 | |
26c61ae5 | 576 | return bfd_elf_sym_name (abfd, hdr, &isym, NULL); |
b885599b AM |
577 | } |
578 | ||
dbb410c3 AM |
579 | /* Set next_in_group list pointer, and group name for NEWSECT. */ |
580 | ||
b34976b6 | 581 | static bfd_boolean |
217aa764 | 582 | setup_group (bfd *abfd, Elf_Internal_Shdr *hdr, asection *newsect) |
dbb410c3 AM |
583 | { |
584 | unsigned int num_group = elf_tdata (abfd)->num_group; | |
585 | ||
586 | /* If num_group is zero, read in all SHT_GROUP sections. The count | |
587 | is set to -1 if there are no SHT_GROUP sections. */ | |
588 | if (num_group == 0) | |
589 | { | |
590 | unsigned int i, shnum; | |
591 | ||
592 | /* First count the number of groups. If we have a SHT_GROUP | |
593 | section with just a flag word (ie. sh_size is 4), ignore it. */ | |
9ad5cbcf | 594 | shnum = elf_numsections (abfd); |
dbb410c3 | 595 | num_group = 0; |
08a40648 | 596 | |
44534af3 | 597 | #define IS_VALID_GROUP_SECTION_HEADER(shdr, minsize) \ |
1783205a | 598 | ( (shdr)->sh_type == SHT_GROUP \ |
44534af3 | 599 | && (shdr)->sh_size >= minsize \ |
1783205a NC |
600 | && (shdr)->sh_entsize == GRP_ENTRY_SIZE \ |
601 | && ((shdr)->sh_size % GRP_ENTRY_SIZE) == 0) | |
08a40648 | 602 | |
dbb410c3 AM |
603 | for (i = 0; i < shnum; i++) |
604 | { | |
605 | Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i]; | |
1783205a | 606 | |
44534af3 | 607 | if (IS_VALID_GROUP_SECTION_HEADER (shdr, 2 * GRP_ENTRY_SIZE)) |
dbb410c3 AM |
608 | num_group += 1; |
609 | } | |
610 | ||
611 | if (num_group == 0) | |
20dbb49d L |
612 | { |
613 | num_group = (unsigned) -1; | |
614 | elf_tdata (abfd)->num_group = num_group; | |
615 | } | |
616 | else | |
dbb410c3 AM |
617 | { |
618 | /* We keep a list of elf section headers for group sections, | |
619 | so we can find them quickly. */ | |
20dbb49d | 620 | bfd_size_type amt; |
d0fb9a8d | 621 | |
20dbb49d | 622 | elf_tdata (abfd)->num_group = num_group; |
a50b1753 NC |
623 | elf_tdata (abfd)->group_sect_ptr = (Elf_Internal_Shdr **) |
624 | bfd_alloc2 (abfd, num_group, sizeof (Elf_Internal_Shdr *)); | |
dbb410c3 | 625 | if (elf_tdata (abfd)->group_sect_ptr == NULL) |
b34976b6 | 626 | return FALSE; |
dbb410c3 AM |
627 | |
628 | num_group = 0; | |
629 | for (i = 0; i < shnum; i++) | |
630 | { | |
631 | Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i]; | |
1783205a | 632 | |
44534af3 | 633 | if (IS_VALID_GROUP_SECTION_HEADER (shdr, 2 * GRP_ENTRY_SIZE)) |
dbb410c3 | 634 | { |
973ffd63 | 635 | unsigned char *src; |
dbb410c3 AM |
636 | Elf_Internal_Group *dest; |
637 | ||
638 | /* Add to list of sections. */ | |
639 | elf_tdata (abfd)->group_sect_ptr[num_group] = shdr; | |
640 | num_group += 1; | |
641 | ||
642 | /* Read the raw contents. */ | |
643 | BFD_ASSERT (sizeof (*dest) >= 4); | |
644 | amt = shdr->sh_size * sizeof (*dest) / 4; | |
a50b1753 NC |
645 | shdr->contents = (unsigned char *) |
646 | bfd_alloc2 (abfd, shdr->sh_size, sizeof (*dest) / 4); | |
1783205a NC |
647 | /* PR binutils/4110: Handle corrupt group headers. */ |
648 | if (shdr->contents == NULL) | |
649 | { | |
650 | _bfd_error_handler | |
695344c0 | 651 | /* xgettext:c-format */ |
493a3386 | 652 | (_("%B: corrupt size field in group section header: 0x%lx"), abfd, shdr->sh_size); |
1783205a | 653 | bfd_set_error (bfd_error_bad_value); |
493a3386 NC |
654 | -- num_group; |
655 | continue; | |
1783205a NC |
656 | } |
657 | ||
658 | memset (shdr->contents, 0, amt); | |
659 | ||
660 | if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0 | |
dbb410c3 AM |
661 | || (bfd_bread (shdr->contents, shdr->sh_size, abfd) |
662 | != shdr->sh_size)) | |
493a3386 NC |
663 | { |
664 | _bfd_error_handler | |
695344c0 | 665 | /* xgettext:c-format */ |
493a3386 NC |
666 | (_("%B: invalid size field in group section header: 0x%lx"), abfd, shdr->sh_size); |
667 | bfd_set_error (bfd_error_bad_value); | |
668 | -- num_group; | |
669 | /* PR 17510: If the group contents are even partially | |
670 | corrupt, do not allow any of the contents to be used. */ | |
671 | memset (shdr->contents, 0, amt); | |
672 | continue; | |
673 | } | |
708d7d0d | 674 | |
dbb410c3 AM |
675 | /* Translate raw contents, a flag word followed by an |
676 | array of elf section indices all in target byte order, | |
677 | to the flag word followed by an array of elf section | |
678 | pointers. */ | |
679 | src = shdr->contents + shdr->sh_size; | |
680 | dest = (Elf_Internal_Group *) (shdr->contents + amt); | |
06614111 | 681 | |
dbb410c3 AM |
682 | while (1) |
683 | { | |
684 | unsigned int idx; | |
685 | ||
686 | src -= 4; | |
687 | --dest; | |
688 | idx = H_GET_32 (abfd, src); | |
689 | if (src == shdr->contents) | |
690 | { | |
691 | dest->flags = idx; | |
b885599b AM |
692 | if (shdr->bfd_section != NULL && (idx & GRP_COMDAT)) |
693 | shdr->bfd_section->flags | |
694 | |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD; | |
dbb410c3 AM |
695 | break; |
696 | } | |
697 | if (idx >= shnum) | |
698 | { | |
4eca0228 AM |
699 | _bfd_error_handler |
700 | (_("%B: invalid SHT_GROUP entry"), abfd); | |
dbb410c3 AM |
701 | idx = 0; |
702 | } | |
703 | dest->shdr = elf_elfsections (abfd)[idx]; | |
704 | } | |
705 | } | |
706 | } | |
493a3386 NC |
707 | |
708 | /* PR 17510: Corrupt binaries might contain invalid groups. */ | |
709 | if (num_group != (unsigned) elf_tdata (abfd)->num_group) | |
710 | { | |
711 | elf_tdata (abfd)->num_group = num_group; | |
712 | ||
713 | /* If all groups are invalid then fail. */ | |
714 | if (num_group == 0) | |
715 | { | |
716 | elf_tdata (abfd)->group_sect_ptr = NULL; | |
717 | elf_tdata (abfd)->num_group = num_group = -1; | |
4eca0228 AM |
718 | _bfd_error_handler |
719 | (_("%B: no valid group sections found"), abfd); | |
493a3386 NC |
720 | bfd_set_error (bfd_error_bad_value); |
721 | } | |
722 | } | |
dbb410c3 AM |
723 | } |
724 | } | |
725 | ||
726 | if (num_group != (unsigned) -1) | |
727 | { | |
728 | unsigned int i; | |
729 | ||
730 | for (i = 0; i < num_group; i++) | |
731 | { | |
732 | Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i]; | |
733 | Elf_Internal_Group *idx = (Elf_Internal_Group *) shdr->contents; | |
734 | unsigned int n_elt = shdr->sh_size / 4; | |
735 | ||
736 | /* Look through this group's sections to see if current | |
737 | section is a member. */ | |
738 | while (--n_elt != 0) | |
739 | if ((++idx)->shdr == hdr) | |
740 | { | |
e0e8c97f | 741 | asection *s = NULL; |
dbb410c3 AM |
742 | |
743 | /* We are a member of this group. Go looking through | |
744 | other members to see if any others are linked via | |
745 | next_in_group. */ | |
746 | idx = (Elf_Internal_Group *) shdr->contents; | |
747 | n_elt = shdr->sh_size / 4; | |
748 | while (--n_elt != 0) | |
749 | if ((s = (++idx)->shdr->bfd_section) != NULL | |
945906ff | 750 | && elf_next_in_group (s) != NULL) |
dbb410c3 AM |
751 | break; |
752 | if (n_elt != 0) | |
753 | { | |
dbb410c3 AM |
754 | /* Snarf the group name from other member, and |
755 | insert current section in circular list. */ | |
945906ff AM |
756 | elf_group_name (newsect) = elf_group_name (s); |
757 | elf_next_in_group (newsect) = elf_next_in_group (s); | |
758 | elf_next_in_group (s) = newsect; | |
dbb410c3 AM |
759 | } |
760 | else | |
761 | { | |
dbb410c3 AM |
762 | const char *gname; |
763 | ||
b885599b AM |
764 | gname = group_signature (abfd, shdr); |
765 | if (gname == NULL) | |
b34976b6 | 766 | return FALSE; |
945906ff | 767 | elf_group_name (newsect) = gname; |
dbb410c3 AM |
768 | |
769 | /* Start a circular list with one element. */ | |
945906ff | 770 | elf_next_in_group (newsect) = newsect; |
dbb410c3 | 771 | } |
b885599b | 772 | |
9dce4196 AM |
773 | /* If the group section has been created, point to the |
774 | new member. */ | |
dbb410c3 | 775 | if (shdr->bfd_section != NULL) |
945906ff | 776 | elf_next_in_group (shdr->bfd_section) = newsect; |
b885599b | 777 | |
dbb410c3 AM |
778 | i = num_group - 1; |
779 | break; | |
780 | } | |
781 | } | |
782 | } | |
783 | ||
945906ff | 784 | if (elf_group_name (newsect) == NULL) |
dbb410c3 | 785 | { |
695344c0 | 786 | /* xgettext:c-format */ |
4eca0228 AM |
787 | _bfd_error_handler (_("%B: no group info for section %A"), |
788 | abfd, newsect); | |
493a3386 | 789 | return FALSE; |
dbb410c3 | 790 | } |
b34976b6 | 791 | return TRUE; |
dbb410c3 AM |
792 | } |
793 | ||
3d7f7666 | 794 | bfd_boolean |
dd863624 | 795 | _bfd_elf_setup_sections (bfd *abfd) |
3d7f7666 L |
796 | { |
797 | unsigned int i; | |
798 | unsigned int num_group = elf_tdata (abfd)->num_group; | |
799 | bfd_boolean result = TRUE; | |
dd863624 L |
800 | asection *s; |
801 | ||
802 | /* Process SHF_LINK_ORDER. */ | |
803 | for (s = abfd->sections; s != NULL; s = s->next) | |
804 | { | |
805 | Elf_Internal_Shdr *this_hdr = &elf_section_data (s)->this_hdr; | |
806 | if ((this_hdr->sh_flags & SHF_LINK_ORDER) != 0) | |
807 | { | |
808 | unsigned int elfsec = this_hdr->sh_link; | |
809 | /* FIXME: The old Intel compiler and old strip/objcopy may | |
810 | not set the sh_link or sh_info fields. Hence we could | |
811 | get the situation where elfsec is 0. */ | |
812 | if (elfsec == 0) | |
813 | { | |
4fbb74a6 | 814 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
dd863624 L |
815 | if (bed->link_order_error_handler) |
816 | bed->link_order_error_handler | |
695344c0 | 817 | /* xgettext:c-format */ |
dd863624 L |
818 | (_("%B: warning: sh_link not set for section `%A'"), |
819 | abfd, s); | |
820 | } | |
821 | else | |
822 | { | |
91d6fa6a | 823 | asection *linksec = NULL; |
25bbc984 | 824 | |
4fbb74a6 AM |
825 | if (elfsec < elf_numsections (abfd)) |
826 | { | |
827 | this_hdr = elf_elfsections (abfd)[elfsec]; | |
91d6fa6a | 828 | linksec = this_hdr->bfd_section; |
4fbb74a6 | 829 | } |
25bbc984 L |
830 | |
831 | /* PR 1991, 2008: | |
832 | Some strip/objcopy may leave an incorrect value in | |
833 | sh_link. We don't want to proceed. */ | |
91d6fa6a | 834 | if (linksec == NULL) |
25bbc984 | 835 | { |
4eca0228 | 836 | _bfd_error_handler |
695344c0 | 837 | /* xgettext:c-format */ |
25bbc984 L |
838 | (_("%B: sh_link [%d] in section `%A' is incorrect"), |
839 | s->owner, s, elfsec); | |
840 | result = FALSE; | |
841 | } | |
842 | ||
91d6fa6a | 843 | elf_linked_to_section (s) = linksec; |
dd863624 L |
844 | } |
845 | } | |
53720c49 AM |
846 | else if (this_hdr->sh_type == SHT_GROUP |
847 | && elf_next_in_group (s) == NULL) | |
848 | { | |
4eca0228 | 849 | _bfd_error_handler |
695344c0 | 850 | /* xgettext:c-format */ |
53720c49 AM |
851 | (_("%B: SHT_GROUP section [index %d] has no SHF_GROUP sections"), |
852 | abfd, elf_section_data (s)->this_idx); | |
853 | result = FALSE; | |
854 | } | |
dd863624 | 855 | } |
3d7f7666 | 856 | |
dd863624 | 857 | /* Process section groups. */ |
3d7f7666 L |
858 | if (num_group == (unsigned) -1) |
859 | return result; | |
860 | ||
861 | for (i = 0; i < num_group; i++) | |
862 | { | |
863 | Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i]; | |
4b0e8a5f NC |
864 | Elf_Internal_Group *idx; |
865 | unsigned int n_elt; | |
3d7f7666 | 866 | |
4b0e8a5f NC |
867 | /* PR binutils/18758: Beware of corrupt binaries with invalid group data. */ |
868 | if (shdr == NULL || shdr->bfd_section == NULL || shdr->contents == NULL) | |
869 | { | |
4eca0228 | 870 | _bfd_error_handler |
695344c0 | 871 | /* xgettext:c-format */ |
4b0e8a5f NC |
872 | (_("%B: section group entry number %u is corrupt"), |
873 | abfd, i); | |
874 | result = FALSE; | |
875 | continue; | |
876 | } | |
877 | ||
878 | idx = (Elf_Internal_Group *) shdr->contents; | |
879 | n_elt = shdr->sh_size / 4; | |
1b786873 | 880 | |
3d7f7666 L |
881 | while (--n_elt != 0) |
882 | if ((++idx)->shdr->bfd_section) | |
883 | elf_sec_group (idx->shdr->bfd_section) = shdr->bfd_section; | |
884 | else if (idx->shdr->sh_type == SHT_RELA | |
885 | || idx->shdr->sh_type == SHT_REL) | |
886 | /* We won't include relocation sections in section groups in | |
887 | output object files. We adjust the group section size here | |
888 | so that relocatable link will work correctly when | |
889 | relocation sections are in section group in input object | |
890 | files. */ | |
891 | shdr->bfd_section->size -= 4; | |
892 | else | |
893 | { | |
894 | /* There are some unknown sections in the group. */ | |
4eca0228 | 895 | _bfd_error_handler |
695344c0 | 896 | /* xgettext:c-format */ |
d003868e AM |
897 | (_("%B: unknown [%d] section `%s' in group [%s]"), |
898 | abfd, | |
3d7f7666 | 899 | (unsigned int) idx->shdr->sh_type, |
1b3a8575 AM |
900 | bfd_elf_string_from_elf_section (abfd, |
901 | (elf_elfheader (abfd) | |
902 | ->e_shstrndx), | |
903 | idx->shdr->sh_name), | |
3d7f7666 L |
904 | shdr->bfd_section->name); |
905 | result = FALSE; | |
906 | } | |
907 | } | |
908 | return result; | |
909 | } | |
910 | ||
72adc230 AM |
911 | bfd_boolean |
912 | bfd_elf_is_group_section (bfd *abfd ATTRIBUTE_UNUSED, const asection *sec) | |
913 | { | |
914 | return elf_next_in_group (sec) != NULL; | |
915 | } | |
916 | ||
f6fe1ccd L |
917 | static char * |
918 | convert_debug_to_zdebug (bfd *abfd, const char *name) | |
919 | { | |
920 | unsigned int len = strlen (name); | |
921 | char *new_name = bfd_alloc (abfd, len + 2); | |
922 | if (new_name == NULL) | |
923 | return NULL; | |
924 | new_name[0] = '.'; | |
925 | new_name[1] = 'z'; | |
926 | memcpy (new_name + 2, name + 1, len); | |
927 | return new_name; | |
928 | } | |
929 | ||
930 | static char * | |
931 | convert_zdebug_to_debug (bfd *abfd, const char *name) | |
932 | { | |
933 | unsigned int len = strlen (name); | |
934 | char *new_name = bfd_alloc (abfd, len); | |
935 | if (new_name == NULL) | |
936 | return NULL; | |
937 | new_name[0] = '.'; | |
938 | memcpy (new_name + 1, name + 2, len - 1); | |
939 | return new_name; | |
940 | } | |
941 | ||
252b5132 RH |
942 | /* Make a BFD section from an ELF section. We store a pointer to the |
943 | BFD section in the bfd_section field of the header. */ | |
944 | ||
b34976b6 | 945 | bfd_boolean |
217aa764 AM |
946 | _bfd_elf_make_section_from_shdr (bfd *abfd, |
947 | Elf_Internal_Shdr *hdr, | |
6dc132d9 L |
948 | const char *name, |
949 | int shindex) | |
252b5132 RH |
950 | { |
951 | asection *newsect; | |
952 | flagword flags; | |
9c5bfbb7 | 953 | const struct elf_backend_data *bed; |
252b5132 RH |
954 | |
955 | if (hdr->bfd_section != NULL) | |
4e011fb5 | 956 | return TRUE; |
252b5132 RH |
957 | |
958 | newsect = bfd_make_section_anyway (abfd, name); | |
959 | if (newsect == NULL) | |
b34976b6 | 960 | return FALSE; |
252b5132 | 961 | |
1829f4b2 AM |
962 | hdr->bfd_section = newsect; |
963 | elf_section_data (newsect)->this_hdr = *hdr; | |
6dc132d9 | 964 | elf_section_data (newsect)->this_idx = shindex; |
1829f4b2 | 965 | |
2f89ff8d L |
966 | /* Always use the real type/flags. */ |
967 | elf_section_type (newsect) = hdr->sh_type; | |
968 | elf_section_flags (newsect) = hdr->sh_flags; | |
969 | ||
252b5132 RH |
970 | newsect->filepos = hdr->sh_offset; |
971 | ||
972 | if (! bfd_set_section_vma (abfd, newsect, hdr->sh_addr) | |
973 | || ! bfd_set_section_size (abfd, newsect, hdr->sh_size) | |
974 | || ! bfd_set_section_alignment (abfd, newsect, | |
72de5009 | 975 | bfd_log2 (hdr->sh_addralign))) |
b34976b6 | 976 | return FALSE; |
252b5132 RH |
977 | |
978 | flags = SEC_NO_FLAGS; | |
979 | if (hdr->sh_type != SHT_NOBITS) | |
980 | flags |= SEC_HAS_CONTENTS; | |
dbb410c3 | 981 | if (hdr->sh_type == SHT_GROUP) |
b3096250 | 982 | flags |= SEC_GROUP | SEC_EXCLUDE; |
252b5132 RH |
983 | if ((hdr->sh_flags & SHF_ALLOC) != 0) |
984 | { | |
985 | flags |= SEC_ALLOC; | |
986 | if (hdr->sh_type != SHT_NOBITS) | |
987 | flags |= SEC_LOAD; | |
988 | } | |
989 | if ((hdr->sh_flags & SHF_WRITE) == 0) | |
990 | flags |= SEC_READONLY; | |
991 | if ((hdr->sh_flags & SHF_EXECINSTR) != 0) | |
992 | flags |= SEC_CODE; | |
993 | else if ((flags & SEC_LOAD) != 0) | |
994 | flags |= SEC_DATA; | |
f5fa8ca2 JJ |
995 | if ((hdr->sh_flags & SHF_MERGE) != 0) |
996 | { | |
997 | flags |= SEC_MERGE; | |
998 | newsect->entsize = hdr->sh_entsize; | |
f5fa8ca2 | 999 | } |
84865015 NC |
1000 | if ((hdr->sh_flags & SHF_STRINGS) != 0) |
1001 | flags |= SEC_STRINGS; | |
dbb410c3 AM |
1002 | if (hdr->sh_flags & SHF_GROUP) |
1003 | if (!setup_group (abfd, hdr, newsect)) | |
b34976b6 | 1004 | return FALSE; |
13ae64f3 JJ |
1005 | if ((hdr->sh_flags & SHF_TLS) != 0) |
1006 | flags |= SEC_THREAD_LOCAL; | |
18ae9cc1 L |
1007 | if ((hdr->sh_flags & SHF_EXCLUDE) != 0) |
1008 | flags |= SEC_EXCLUDE; | |
252b5132 | 1009 | |
3d2b39cf | 1010 | if ((flags & SEC_ALLOC) == 0) |
7a6cc5fb | 1011 | { |
3d2b39cf L |
1012 | /* The debugging sections appear to be recognized only by name, |
1013 | not any sort of flag. Their SEC_ALLOC bits are cleared. */ | |
3d2b39cf L |
1014 | if (name [0] == '.') |
1015 | { | |
f073ced3 AM |
1016 | const char *p; |
1017 | int n; | |
1018 | if (name[1] == 'd') | |
1019 | p = ".debug", n = 6; | |
1020 | else if (name[1] == 'g' && name[2] == 'n') | |
1021 | p = ".gnu.linkonce.wi.", n = 17; | |
1022 | else if (name[1] == 'g' && name[2] == 'd') | |
1023 | p = ".gdb_index", n = 11; /* yes we really do mean 11. */ | |
1024 | else if (name[1] == 'l') | |
1025 | p = ".line", n = 5; | |
1026 | else if (name[1] == 's') | |
1027 | p = ".stab", n = 5; | |
1028 | else if (name[1] == 'z') | |
1029 | p = ".zdebug", n = 7; | |
1030 | else | |
1031 | p = NULL, n = 0; | |
1032 | if (p != NULL && strncmp (name, p, n) == 0) | |
3d2b39cf L |
1033 | flags |= SEC_DEBUGGING; |
1034 | } | |
1035 | } | |
252b5132 RH |
1036 | |
1037 | /* As a GNU extension, if the name begins with .gnu.linkonce, we | |
1038 | only link a single copy of the section. This is used to support | |
1039 | g++. g++ will emit each template expansion in its own section. | |
1040 | The symbols will be defined as weak, so that multiple definitions | |
1041 | are permitted. The GNU linker extension is to actually discard | |
1042 | all but one of the sections. */ | |
0112cd26 | 1043 | if (CONST_STRNEQ (name, ".gnu.linkonce") |
b885599b | 1044 | && elf_next_in_group (newsect) == NULL) |
252b5132 RH |
1045 | flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD; |
1046 | ||
fa152c49 JW |
1047 | bed = get_elf_backend_data (abfd); |
1048 | if (bed->elf_backend_section_flags) | |
1049 | if (! bed->elf_backend_section_flags (&flags, hdr)) | |
b34976b6 | 1050 | return FALSE; |
fa152c49 | 1051 | |
252b5132 | 1052 | if (! bfd_set_section_flags (abfd, newsect, flags)) |
b34976b6 | 1053 | return FALSE; |
252b5132 | 1054 | |
718175fa JK |
1055 | /* We do not parse the PT_NOTE segments as we are interested even in the |
1056 | separate debug info files which may have the segments offsets corrupted. | |
1057 | PT_NOTEs from the core files are currently not parsed using BFD. */ | |
1058 | if (hdr->sh_type == SHT_NOTE) | |
1059 | { | |
baea7ef1 | 1060 | bfd_byte *contents; |
718175fa | 1061 | |
baea7ef1 | 1062 | if (!bfd_malloc_and_get_section (abfd, newsect, &contents)) |
718175fa JK |
1063 | return FALSE; |
1064 | ||
08698b7b | 1065 | elf_parse_notes (abfd, (char *) contents, hdr->sh_size, hdr->sh_offset); |
718175fa JK |
1066 | free (contents); |
1067 | } | |
1068 | ||
252b5132 RH |
1069 | if ((flags & SEC_ALLOC) != 0) |
1070 | { | |
1071 | Elf_Internal_Phdr *phdr; | |
6ffd7900 AM |
1072 | unsigned int i, nload; |
1073 | ||
1074 | /* Some ELF linkers produce binaries with all the program header | |
1075 | p_paddr fields zero. If we have such a binary with more than | |
1076 | one PT_LOAD header, then leave the section lma equal to vma | |
1077 | so that we don't create sections with overlapping lma. */ | |
1078 | phdr = elf_tdata (abfd)->phdr; | |
1079 | for (nload = 0, i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++) | |
1080 | if (phdr->p_paddr != 0) | |
1081 | break; | |
1082 | else if (phdr->p_type == PT_LOAD && phdr->p_memsz != 0) | |
1083 | ++nload; | |
1084 | if (i >= elf_elfheader (abfd)->e_phnum && nload > 1) | |
1085 | return TRUE; | |
252b5132 | 1086 | |
252b5132 RH |
1087 | phdr = elf_tdata (abfd)->phdr; |
1088 | for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++) | |
1089 | { | |
86b2281f AM |
1090 | if (((phdr->p_type == PT_LOAD |
1091 | && (hdr->sh_flags & SHF_TLS) == 0) | |
1092 | || phdr->p_type == PT_TLS) | |
9a83a553 | 1093 | && ELF_SECTION_IN_SEGMENT (hdr, phdr)) |
252b5132 | 1094 | { |
88967714 AM |
1095 | if ((flags & SEC_LOAD) == 0) |
1096 | newsect->lma = (phdr->p_paddr | |
1097 | + hdr->sh_addr - phdr->p_vaddr); | |
1098 | else | |
1099 | /* We used to use the same adjustment for SEC_LOAD | |
1100 | sections, but that doesn't work if the segment | |
1101 | is packed with code from multiple VMAs. | |
1102 | Instead we calculate the section LMA based on | |
1103 | the segment LMA. It is assumed that the | |
1104 | segment will contain sections with contiguous | |
1105 | LMAs, even if the VMAs are not. */ | |
1106 | newsect->lma = (phdr->p_paddr | |
1107 | + hdr->sh_offset - phdr->p_offset); | |
1108 | ||
1109 | /* With contiguous segments, we can't tell from file | |
1110 | offsets whether a section with zero size should | |
1111 | be placed at the end of one segment or the | |
1112 | beginning of the next. Decide based on vaddr. */ | |
1113 | if (hdr->sh_addr >= phdr->p_vaddr | |
1114 | && (hdr->sh_addr + hdr->sh_size | |
1115 | <= phdr->p_vaddr + phdr->p_memsz)) | |
1116 | break; | |
252b5132 RH |
1117 | } |
1118 | } | |
1119 | } | |
1120 | ||
4a114e3e L |
1121 | /* Compress/decompress DWARF debug sections with names: .debug_* and |
1122 | .zdebug_*, after the section flags is set. */ | |
1123 | if ((flags & SEC_DEBUGGING) | |
1124 | && ((name[1] == 'd' && name[6] == '_') | |
1125 | || (name[1] == 'z' && name[7] == '_'))) | |
1126 | { | |
1127 | enum { nothing, compress, decompress } action = nothing; | |
151411f8 | 1128 | int compression_header_size; |
dab394de | 1129 | bfd_size_type uncompressed_size; |
151411f8 L |
1130 | bfd_boolean compressed |
1131 | = bfd_is_section_compressed_with_header (abfd, newsect, | |
dab394de L |
1132 | &compression_header_size, |
1133 | &uncompressed_size); | |
4a114e3e | 1134 | |
151411f8 | 1135 | if (compressed) |
4a114e3e L |
1136 | { |
1137 | /* Compressed section. Check if we should decompress. */ | |
1138 | if ((abfd->flags & BFD_DECOMPRESS)) | |
1139 | action = decompress; | |
1140 | } | |
151411f8 L |
1141 | |
1142 | /* Compress the uncompressed section or convert from/to .zdebug* | |
1143 | section. Check if we should compress. */ | |
1144 | if (action == nothing) | |
4a114e3e | 1145 | { |
151411f8 L |
1146 | if (newsect->size != 0 |
1147 | && (abfd->flags & BFD_COMPRESS) | |
1148 | && compression_header_size >= 0 | |
dab394de | 1149 | && uncompressed_size > 0 |
151411f8 L |
1150 | && (!compressed |
1151 | || ((compression_header_size > 0) | |
1152 | != ((abfd->flags & BFD_COMPRESS_GABI) != 0)))) | |
4a114e3e | 1153 | action = compress; |
151411f8 L |
1154 | else |
1155 | return TRUE; | |
4a114e3e L |
1156 | } |
1157 | ||
151411f8 | 1158 | if (action == compress) |
4a114e3e | 1159 | { |
4a114e3e L |
1160 | if (!bfd_init_section_compress_status (abfd, newsect)) |
1161 | { | |
4eca0228 | 1162 | _bfd_error_handler |
695344c0 | 1163 | /* xgettext:c-format */ |
bc823199 | 1164 | (_("%B: unable to initialize compress status for section %s"), |
4a114e3e L |
1165 | abfd, name); |
1166 | return FALSE; | |
1167 | } | |
151411f8 L |
1168 | } |
1169 | else | |
1170 | { | |
4a114e3e L |
1171 | if (!bfd_init_section_decompress_status (abfd, newsect)) |
1172 | { | |
4eca0228 | 1173 | _bfd_error_handler |
695344c0 | 1174 | /* xgettext:c-format */ |
bc823199 | 1175 | (_("%B: unable to initialize decompress status for section %s"), |
4a114e3e L |
1176 | abfd, name); |
1177 | return FALSE; | |
1178 | } | |
151411f8 L |
1179 | } |
1180 | ||
f6fe1ccd | 1181 | if (abfd->is_linker_input) |
151411f8 | 1182 | { |
f6fe1ccd L |
1183 | if (name[1] == 'z' |
1184 | && (action == decompress | |
1185 | || (action == compress | |
1186 | && (abfd->flags & BFD_COMPRESS_GABI) != 0))) | |
4e011fb5 | 1187 | { |
f6fe1ccd L |
1188 | /* Convert section name from .zdebug_* to .debug_* so |
1189 | that linker will consider this section as a debug | |
1190 | section. */ | |
1191 | char *new_name = convert_zdebug_to_debug (abfd, name); | |
151411f8 L |
1192 | if (new_name == NULL) |
1193 | return FALSE; | |
f6fe1ccd | 1194 | bfd_rename_section (abfd, newsect, new_name); |
151411f8 | 1195 | } |
4a114e3e | 1196 | } |
f6fe1ccd L |
1197 | else |
1198 | /* For objdump, don't rename the section. For objcopy, delay | |
1199 | section rename to elf_fake_sections. */ | |
1200 | newsect->flags |= SEC_ELF_RENAME; | |
4a114e3e L |
1201 | } |
1202 | ||
b34976b6 | 1203 | return TRUE; |
252b5132 RH |
1204 | } |
1205 | ||
84865015 NC |
1206 | const char *const bfd_elf_section_type_names[] = |
1207 | { | |
252b5132 RH |
1208 | "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB", |
1209 | "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE", | |
1210 | "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM", | |
1211 | }; | |
1212 | ||
1049f94e | 1213 | /* ELF relocs are against symbols. If we are producing relocatable |
252b5132 RH |
1214 | output, and the reloc is against an external symbol, and nothing |
1215 | has given us any additional addend, the resulting reloc will also | |
1216 | be against the same symbol. In such a case, we don't want to | |
1217 | change anything about the way the reloc is handled, since it will | |
1218 | all be done at final link time. Rather than put special case code | |
1219 | into bfd_perform_relocation, all the reloc types use this howto | |
1220 | function. It just short circuits the reloc if producing | |
1049f94e | 1221 | relocatable output against an external symbol. */ |
252b5132 | 1222 | |
252b5132 | 1223 | bfd_reloc_status_type |
217aa764 AM |
1224 | bfd_elf_generic_reloc (bfd *abfd ATTRIBUTE_UNUSED, |
1225 | arelent *reloc_entry, | |
1226 | asymbol *symbol, | |
1227 | void *data ATTRIBUTE_UNUSED, | |
1228 | asection *input_section, | |
1229 | bfd *output_bfd, | |
1230 | char **error_message ATTRIBUTE_UNUSED) | |
1231 | { | |
1232 | if (output_bfd != NULL | |
252b5132 RH |
1233 | && (symbol->flags & BSF_SECTION_SYM) == 0 |
1234 | && (! reloc_entry->howto->partial_inplace | |
1235 | || reloc_entry->addend == 0)) | |
1236 | { | |
1237 | reloc_entry->address += input_section->output_offset; | |
1238 | return bfd_reloc_ok; | |
1239 | } | |
1240 | ||
1241 | return bfd_reloc_continue; | |
1242 | } | |
1243 | \f | |
84865015 NC |
1244 | /* Returns TRUE if section A matches section B. |
1245 | Names, addresses and links may be different, but everything else | |
1246 | should be the same. */ | |
1247 | ||
1248 | static bfd_boolean | |
5522f910 NC |
1249 | section_match (const Elf_Internal_Shdr * a, |
1250 | const Elf_Internal_Shdr * b) | |
84865015 NC |
1251 | { |
1252 | return | |
1253 | a->sh_type == b->sh_type | |
5522f910 NC |
1254 | && (a->sh_flags & ~ SHF_INFO_LINK) |
1255 | == (b->sh_flags & ~ SHF_INFO_LINK) | |
84865015 NC |
1256 | && a->sh_addralign == b->sh_addralign |
1257 | && a->sh_size == b->sh_size | |
1258 | && a->sh_entsize == b->sh_entsize | |
1259 | /* FIXME: Check sh_addr ? */ | |
1260 | ; | |
1261 | } | |
1262 | ||
1263 | /* Find a section in OBFD that has the same characteristics | |
1264 | as IHEADER. Return the index of this section or SHN_UNDEF if | |
1265 | none can be found. Check's section HINT first, as this is likely | |
1266 | to be the correct section. */ | |
1267 | ||
1268 | static unsigned int | |
5522f910 | 1269 | find_link (const bfd * obfd, const Elf_Internal_Shdr * iheader, const unsigned int hint) |
84865015 NC |
1270 | { |
1271 | Elf_Internal_Shdr ** oheaders = elf_elfsections (obfd); | |
1272 | unsigned int i; | |
1273 | ||
a55c9876 NC |
1274 | BFD_ASSERT (iheader != NULL); |
1275 | ||
1276 | /* See PR 20922 for a reproducer of the NULL test. */ | |
1277 | if (oheaders[hint] != NULL | |
1278 | && section_match (oheaders[hint], iheader)) | |
84865015 NC |
1279 | return hint; |
1280 | ||
1281 | for (i = 1; i < elf_numsections (obfd); i++) | |
1282 | { | |
1283 | Elf_Internal_Shdr * oheader = oheaders[i]; | |
1284 | ||
a55c9876 NC |
1285 | if (oheader == NULL) |
1286 | continue; | |
84865015 NC |
1287 | if (section_match (oheader, iheader)) |
1288 | /* FIXME: Do we care if there is a potential for | |
1289 | multiple matches ? */ | |
1290 | return i; | |
1291 | } | |
1292 | ||
1293 | return SHN_UNDEF; | |
1294 | } | |
1295 | ||
5522f910 NC |
1296 | /* PR 19938: Attempt to set the ELF section header fields of an OS or |
1297 | Processor specific section, based upon a matching input section. | |
1298 | Returns TRUE upon success, FALSE otherwise. */ | |
1299 | ||
1300 | static bfd_boolean | |
1301 | copy_special_section_fields (const bfd *ibfd, | |
1302 | bfd *obfd, | |
1303 | const Elf_Internal_Shdr *iheader, | |
1304 | Elf_Internal_Shdr *oheader, | |
1305 | const unsigned int secnum) | |
1306 | { | |
1307 | const struct elf_backend_data *bed = get_elf_backend_data (obfd); | |
1308 | const Elf_Internal_Shdr **iheaders = (const Elf_Internal_Shdr **) elf_elfsections (ibfd); | |
1309 | bfd_boolean changed = FALSE; | |
1310 | unsigned int sh_link; | |
1311 | ||
1312 | if (oheader->sh_type == SHT_NOBITS) | |
1313 | { | |
1314 | /* This is a feature for objcopy --only-keep-debug: | |
1315 | When a section's type is changed to NOBITS, we preserve | |
1316 | the sh_link and sh_info fields so that they can be | |
1317 | matched up with the original. | |
1318 | ||
1319 | Note: Strictly speaking these assignments are wrong. | |
1320 | The sh_link and sh_info fields should point to the | |
1321 | relevent sections in the output BFD, which may not be in | |
1322 | the same location as they were in the input BFD. But | |
1323 | the whole point of this action is to preserve the | |
1324 | original values of the sh_link and sh_info fields, so | |
1325 | that they can be matched up with the section headers in | |
1326 | the original file. So strictly speaking we may be | |
1327 | creating an invalid ELF file, but it is only for a file | |
1328 | that just contains debug info and only for sections | |
1329 | without any contents. */ | |
1330 | if (oheader->sh_link == 0) | |
1331 | oheader->sh_link = iheader->sh_link; | |
1332 | if (oheader->sh_info == 0) | |
1333 | oheader->sh_info = iheader->sh_info; | |
1334 | return TRUE; | |
1335 | } | |
1336 | ||
1337 | /* Allow the target a chance to decide how these fields should be set. */ | |
1338 | if (bed->elf_backend_copy_special_section_fields != NULL | |
1339 | && bed->elf_backend_copy_special_section_fields | |
1340 | (ibfd, obfd, iheader, oheader)) | |
1341 | return TRUE; | |
1342 | ||
1343 | /* We have an iheader which might match oheader, and which has non-zero | |
1344 | sh_info and/or sh_link fields. Attempt to follow those links and find | |
1345 | the section in the output bfd which corresponds to the linked section | |
1346 | in the input bfd. */ | |
1347 | if (iheader->sh_link != SHN_UNDEF) | |
1348 | { | |
4f3ca05b NC |
1349 | /* See PR 20931 for a reproducer. */ |
1350 | if (iheader->sh_link >= elf_numsections (ibfd)) | |
1351 | { | |
1352 | (* _bfd_error_handler) | |
1353 | /* xgettext:c-format */ | |
1354 | (_("%B: Invalid sh_link field (%d) in section number %d"), | |
1355 | ibfd, iheader->sh_link, secnum); | |
1356 | return FALSE; | |
1357 | } | |
1358 | ||
5522f910 NC |
1359 | sh_link = find_link (obfd, iheaders[iheader->sh_link], iheader->sh_link); |
1360 | if (sh_link != SHN_UNDEF) | |
1361 | { | |
1362 | oheader->sh_link = sh_link; | |
1363 | changed = TRUE; | |
1364 | } | |
1365 | else | |
1366 | /* FIXME: Should we install iheader->sh_link | |
1367 | if we could not find a match ? */ | |
1368 | (* _bfd_error_handler) | |
695344c0 | 1369 | /* xgettext:c-format */ |
5522f910 NC |
1370 | (_("%B: Failed to find link section for section %d"), obfd, secnum); |
1371 | } | |
1372 | ||
1373 | if (iheader->sh_info) | |
1374 | { | |
1375 | /* The sh_info field can hold arbitrary information, but if the | |
1376 | SHF_LINK_INFO flag is set then it should be interpreted as a | |
1377 | section index. */ | |
1378 | if (iheader->sh_flags & SHF_INFO_LINK) | |
1379 | { | |
1380 | sh_link = find_link (obfd, iheaders[iheader->sh_info], | |
1381 | iheader->sh_info); | |
1382 | if (sh_link != SHN_UNDEF) | |
1383 | oheader->sh_flags |= SHF_INFO_LINK; | |
1384 | } | |
1385 | else | |
1386 | /* No idea what it means - just copy it. */ | |
1387 | sh_link = iheader->sh_info; | |
1388 | ||
1389 | if (sh_link != SHN_UNDEF) | |
1390 | { | |
1391 | oheader->sh_info = sh_link; | |
1392 | changed = TRUE; | |
1393 | } | |
1394 | else | |
1395 | (* _bfd_error_handler) | |
695344c0 | 1396 | /* xgettext:c-format */ |
5522f910 NC |
1397 | (_("%B: Failed to find info section for section %d"), obfd, secnum); |
1398 | } | |
1399 | ||
1400 | return changed; | |
1401 | } | |
1402 | ||
0ac4564e L |
1403 | /* Copy the program header and other data from one object module to |
1404 | another. */ | |
252b5132 | 1405 | |
b34976b6 | 1406 | bfd_boolean |
217aa764 | 1407 | _bfd_elf_copy_private_bfd_data (bfd *ibfd, bfd *obfd) |
2d502050 | 1408 | { |
5522f910 NC |
1409 | const Elf_Internal_Shdr **iheaders = (const Elf_Internal_Shdr **) elf_elfsections (ibfd); |
1410 | Elf_Internal_Shdr **oheaders = elf_elfsections (obfd); | |
1411 | const struct elf_backend_data *bed; | |
84865015 NC |
1412 | unsigned int i; |
1413 | ||
2d502050 | 1414 | if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour |
84865015 | 1415 | || bfd_get_flavour (obfd) != bfd_target_elf_flavour) |
b34976b6 | 1416 | return TRUE; |
2d502050 | 1417 | |
57b828ef L |
1418 | if (!elf_flags_init (obfd)) |
1419 | { | |
1420 | elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags; | |
1421 | elf_flags_init (obfd) = TRUE; | |
1422 | } | |
2d502050 | 1423 | |
0ac4564e | 1424 | elf_gp (obfd) = elf_gp (ibfd); |
57b828ef L |
1425 | |
1426 | /* Also copy the EI_OSABI field. */ | |
1427 | elf_elfheader (obfd)->e_ident[EI_OSABI] = | |
1428 | elf_elfheader (ibfd)->e_ident[EI_OSABI]; | |
104d59d1 | 1429 | |
5522f910 NC |
1430 | /* If set, copy the EI_ABIVERSION field. */ |
1431 | if (elf_elfheader (ibfd)->e_ident[EI_ABIVERSION]) | |
1432 | elf_elfheader (obfd)->e_ident[EI_ABIVERSION] | |
1433 | = elf_elfheader (ibfd)->e_ident[EI_ABIVERSION]; | |
1434 | ||
104d59d1 JM |
1435 | /* Copy object attributes. */ |
1436 | _bfd_elf_copy_obj_attributes (ibfd, obfd); | |
63b9bbb7 | 1437 | |
84865015 NC |
1438 | if (iheaders == NULL || oheaders == NULL) |
1439 | return TRUE; | |
63b9bbb7 | 1440 | |
5522f910 NC |
1441 | bed = get_elf_backend_data (obfd); |
1442 | ||
1443 | /* Possibly copy other fields in the section header. */ | |
84865015 | 1444 | for (i = 1; i < elf_numsections (obfd); i++) |
63b9bbb7 | 1445 | { |
84865015 NC |
1446 | unsigned int j; |
1447 | Elf_Internal_Shdr * oheader = oheaders[i]; | |
63b9bbb7 | 1448 | |
5522f910 NC |
1449 | /* Ignore ordinary sections. SHT_NOBITS sections are considered however |
1450 | because of a special case need for generating separate debug info | |
1451 | files. See below for more details. */ | |
84865015 NC |
1452 | if (oheader == NULL |
1453 | || (oheader->sh_type != SHT_NOBITS | |
5522f910 NC |
1454 | && oheader->sh_type < SHT_LOOS)) |
1455 | continue; | |
1456 | ||
1457 | /* Ignore empty sections, and sections whose | |
1458 | fields have already been initialised. */ | |
1459 | if (oheader->sh_size == 0 | |
84865015 NC |
1460 | || (oheader->sh_info != 0 && oheader->sh_link != 0)) |
1461 | continue; | |
63b9bbb7 | 1462 | |
84865015 | 1463 | /* Scan for the matching section in the input bfd. |
5522f910 NC |
1464 | First we try for a direct mapping between the input and output sections. */ |
1465 | for (j = 1; j < elf_numsections (ibfd); j++) | |
1466 | { | |
1467 | const Elf_Internal_Shdr * iheader = iheaders[j]; | |
1468 | ||
1469 | if (iheader == NULL) | |
1470 | continue; | |
1471 | ||
1472 | if (oheader->bfd_section != NULL | |
1473 | && iheader->bfd_section != NULL | |
1474 | && iheader->bfd_section->output_section != NULL | |
1475 | && iheader->bfd_section->output_section == oheader->bfd_section) | |
1476 | { | |
1477 | /* We have found a connection from the input section to the | |
1478 | output section. Attempt to copy the header fields. If | |
1479 | this fails then do not try any further sections - there | |
1480 | should only be a one-to-one mapping between input and output. */ | |
1481 | if (! copy_special_section_fields (ibfd, obfd, iheader, oheader, i)) | |
1482 | j = elf_numsections (ibfd); | |
1483 | break; | |
1484 | } | |
1485 | } | |
1486 | ||
1487 | if (j < elf_numsections (ibfd)) | |
1488 | continue; | |
1489 | ||
1490 | /* That failed. So try to deduce the corresponding input section. | |
84865015 NC |
1491 | Unfortunately we cannot compare names as the output string table |
1492 | is empty, so instead we check size, address and type. */ | |
1493 | for (j = 1; j < elf_numsections (ibfd); j++) | |
1494 | { | |
5522f910 | 1495 | const Elf_Internal_Shdr * iheader = iheaders[j]; |
84865015 | 1496 | |
5522f910 NC |
1497 | if (iheader == NULL) |
1498 | continue; | |
1499 | ||
1500 | /* Try matching fields in the input section's header. | |
1501 | Since --only-keep-debug turns all non-debug sections into | |
84865015 NC |
1502 | SHT_NOBITS sections, the output SHT_NOBITS type matches any |
1503 | input type. */ | |
1504 | if ((oheader->sh_type == SHT_NOBITS | |
1505 | || iheader->sh_type == oheader->sh_type) | |
5522f910 NC |
1506 | && (iheader->sh_flags & ~ SHF_INFO_LINK) |
1507 | == (oheader->sh_flags & ~ SHF_INFO_LINK) | |
84865015 NC |
1508 | && iheader->sh_addralign == oheader->sh_addralign |
1509 | && iheader->sh_entsize == oheader->sh_entsize | |
1510 | && iheader->sh_size == oheader->sh_size | |
1511 | && iheader->sh_addr == oheader->sh_addr | |
1512 | && (iheader->sh_info != oheader->sh_info | |
1513 | || iheader->sh_link != oheader->sh_link)) | |
63b9bbb7 | 1514 | { |
5522f910 NC |
1515 | if (copy_special_section_fields (ibfd, obfd, iheader, oheader, i)) |
1516 | break; | |
63b9bbb7 NC |
1517 | } |
1518 | } | |
5522f910 NC |
1519 | |
1520 | if (j == elf_numsections (ibfd) && oheader->sh_type >= SHT_LOOS) | |
1521 | { | |
1522 | /* Final attempt. Call the backend copy function | |
1523 | with a NULL input section. */ | |
1524 | if (bed->elf_backend_copy_special_section_fields != NULL) | |
1525 | bed->elf_backend_copy_special_section_fields (ibfd, obfd, NULL, oheader); | |
1526 | } | |
63b9bbb7 NC |
1527 | } |
1528 | ||
b34976b6 | 1529 | return TRUE; |
2d502050 L |
1530 | } |
1531 | ||
cedc298e L |
1532 | static const char * |
1533 | get_segment_type (unsigned int p_type) | |
1534 | { | |
1535 | const char *pt; | |
1536 | switch (p_type) | |
1537 | { | |
1538 | case PT_NULL: pt = "NULL"; break; | |
1539 | case PT_LOAD: pt = "LOAD"; break; | |
1540 | case PT_DYNAMIC: pt = "DYNAMIC"; break; | |
1541 | case PT_INTERP: pt = "INTERP"; break; | |
1542 | case PT_NOTE: pt = "NOTE"; break; | |
1543 | case PT_SHLIB: pt = "SHLIB"; break; | |
1544 | case PT_PHDR: pt = "PHDR"; break; | |
1545 | case PT_TLS: pt = "TLS"; break; | |
1546 | case PT_GNU_EH_FRAME: pt = "EH_FRAME"; break; | |
2b05f1b7 | 1547 | case PT_GNU_STACK: pt = "STACK"; break; |
cedc298e L |
1548 | case PT_GNU_RELRO: pt = "RELRO"; break; |
1549 | default: pt = NULL; break; | |
1550 | } | |
1551 | return pt; | |
1552 | } | |
1553 | ||
f0b79d91 L |
1554 | /* Print out the program headers. */ |
1555 | ||
b34976b6 | 1556 | bfd_boolean |
217aa764 | 1557 | _bfd_elf_print_private_bfd_data (bfd *abfd, void *farg) |
252b5132 | 1558 | { |
a50b1753 | 1559 | FILE *f = (FILE *) farg; |
252b5132 RH |
1560 | Elf_Internal_Phdr *p; |
1561 | asection *s; | |
1562 | bfd_byte *dynbuf = NULL; | |
1563 | ||
1564 | p = elf_tdata (abfd)->phdr; | |
1565 | if (p != NULL) | |
1566 | { | |
1567 | unsigned int i, c; | |
1568 | ||
1569 | fprintf (f, _("\nProgram Header:\n")); | |
1570 | c = elf_elfheader (abfd)->e_phnum; | |
1571 | for (i = 0; i < c; i++, p++) | |
1572 | { | |
cedc298e | 1573 | const char *pt = get_segment_type (p->p_type); |
252b5132 RH |
1574 | char buf[20]; |
1575 | ||
cedc298e | 1576 | if (pt == NULL) |
252b5132 | 1577 | { |
cedc298e L |
1578 | sprintf (buf, "0x%lx", p->p_type); |
1579 | pt = buf; | |
252b5132 | 1580 | } |
dc810e39 | 1581 | fprintf (f, "%8s off 0x", pt); |
60b89a18 | 1582 | bfd_fprintf_vma (abfd, f, p->p_offset); |
252b5132 | 1583 | fprintf (f, " vaddr 0x"); |
60b89a18 | 1584 | bfd_fprintf_vma (abfd, f, p->p_vaddr); |
252b5132 | 1585 | fprintf (f, " paddr 0x"); |
60b89a18 | 1586 | bfd_fprintf_vma (abfd, f, p->p_paddr); |
252b5132 RH |
1587 | fprintf (f, " align 2**%u\n", bfd_log2 (p->p_align)); |
1588 | fprintf (f, " filesz 0x"); | |
60b89a18 | 1589 | bfd_fprintf_vma (abfd, f, p->p_filesz); |
252b5132 | 1590 | fprintf (f, " memsz 0x"); |
60b89a18 | 1591 | bfd_fprintf_vma (abfd, f, p->p_memsz); |
252b5132 RH |
1592 | fprintf (f, " flags %c%c%c", |
1593 | (p->p_flags & PF_R) != 0 ? 'r' : '-', | |
1594 | (p->p_flags & PF_W) != 0 ? 'w' : '-', | |
1595 | (p->p_flags & PF_X) != 0 ? 'x' : '-'); | |
dc810e39 AM |
1596 | if ((p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X)) != 0) |
1597 | fprintf (f, " %lx", p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X)); | |
252b5132 RH |
1598 | fprintf (f, "\n"); |
1599 | } | |
1600 | } | |
1601 | ||
1602 | s = bfd_get_section_by_name (abfd, ".dynamic"); | |
1603 | if (s != NULL) | |
1604 | { | |
cb33740c | 1605 | unsigned int elfsec; |
dc810e39 | 1606 | unsigned long shlink; |
252b5132 RH |
1607 | bfd_byte *extdyn, *extdynend; |
1608 | size_t extdynsize; | |
217aa764 | 1609 | void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *); |
252b5132 RH |
1610 | |
1611 | fprintf (f, _("\nDynamic Section:\n")); | |
1612 | ||
eea6121a | 1613 | if (!bfd_malloc_and_get_section (abfd, s, &dynbuf)) |
252b5132 RH |
1614 | goto error_return; |
1615 | ||
1616 | elfsec = _bfd_elf_section_from_bfd_section (abfd, s); | |
cb33740c | 1617 | if (elfsec == SHN_BAD) |
252b5132 | 1618 | goto error_return; |
dc810e39 | 1619 | shlink = elf_elfsections (abfd)[elfsec]->sh_link; |
252b5132 RH |
1620 | |
1621 | extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn; | |
1622 | swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in; | |
1623 | ||
1624 | extdyn = dynbuf; | |
06614111 NC |
1625 | /* PR 17512: file: 6f427532. */ |
1626 | if (s->size < extdynsize) | |
1627 | goto error_return; | |
eea6121a | 1628 | extdynend = extdyn + s->size; |
1036838a NC |
1629 | /* PR 17512: file: id:000006,sig:06,src:000000,op:flip4,pos:5664. |
1630 | Fix range check. */ | |
1631 | for (; extdyn <= (extdynend - extdynsize); extdyn += extdynsize) | |
252b5132 RH |
1632 | { |
1633 | Elf_Internal_Dyn dyn; | |
ad9563d6 | 1634 | const char *name = ""; |
252b5132 | 1635 | char ab[20]; |
b34976b6 | 1636 | bfd_boolean stringp; |
ad9563d6 | 1637 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
252b5132 | 1638 | |
217aa764 | 1639 | (*swap_dyn_in) (abfd, extdyn, &dyn); |
252b5132 RH |
1640 | |
1641 | if (dyn.d_tag == DT_NULL) | |
1642 | break; | |
1643 | ||
b34976b6 | 1644 | stringp = FALSE; |
252b5132 RH |
1645 | switch (dyn.d_tag) |
1646 | { | |
1647 | default: | |
ad9563d6 CM |
1648 | if (bed->elf_backend_get_target_dtag) |
1649 | name = (*bed->elf_backend_get_target_dtag) (dyn.d_tag); | |
1650 | ||
1651 | if (!strcmp (name, "")) | |
1652 | { | |
1653 | sprintf (ab, "0x%lx", (unsigned long) dyn.d_tag); | |
1654 | name = ab; | |
1655 | } | |
252b5132 RH |
1656 | break; |
1657 | ||
b34976b6 | 1658 | case DT_NEEDED: name = "NEEDED"; stringp = TRUE; break; |
252b5132 RH |
1659 | case DT_PLTRELSZ: name = "PLTRELSZ"; break; |
1660 | case DT_PLTGOT: name = "PLTGOT"; break; | |
1661 | case DT_HASH: name = "HASH"; break; | |
1662 | case DT_STRTAB: name = "STRTAB"; break; | |
1663 | case DT_SYMTAB: name = "SYMTAB"; break; | |
1664 | case DT_RELA: name = "RELA"; break; | |
1665 | case DT_RELASZ: name = "RELASZ"; break; | |
1666 | case DT_RELAENT: name = "RELAENT"; break; | |
1667 | case DT_STRSZ: name = "STRSZ"; break; | |
1668 | case DT_SYMENT: name = "SYMENT"; break; | |
1669 | case DT_INIT: name = "INIT"; break; | |
1670 | case DT_FINI: name = "FINI"; break; | |
b34976b6 AM |
1671 | case DT_SONAME: name = "SONAME"; stringp = TRUE; break; |
1672 | case DT_RPATH: name = "RPATH"; stringp = TRUE; break; | |
252b5132 RH |
1673 | case DT_SYMBOLIC: name = "SYMBOLIC"; break; |
1674 | case DT_REL: name = "REL"; break; | |
1675 | case DT_RELSZ: name = "RELSZ"; break; | |
1676 | case DT_RELENT: name = "RELENT"; break; | |
1677 | case DT_PLTREL: name = "PLTREL"; break; | |
1678 | case DT_DEBUG: name = "DEBUG"; break; | |
1679 | case DT_TEXTREL: name = "TEXTREL"; break; | |
1680 | case DT_JMPREL: name = "JMPREL"; break; | |
94558834 L |
1681 | case DT_BIND_NOW: name = "BIND_NOW"; break; |
1682 | case DT_INIT_ARRAY: name = "INIT_ARRAY"; break; | |
1683 | case DT_FINI_ARRAY: name = "FINI_ARRAY"; break; | |
1684 | case DT_INIT_ARRAYSZ: name = "INIT_ARRAYSZ"; break; | |
1685 | case DT_FINI_ARRAYSZ: name = "FINI_ARRAYSZ"; break; | |
b34976b6 | 1686 | case DT_RUNPATH: name = "RUNPATH"; stringp = TRUE; break; |
94558834 L |
1687 | case DT_FLAGS: name = "FLAGS"; break; |
1688 | case DT_PREINIT_ARRAY: name = "PREINIT_ARRAY"; break; | |
1689 | case DT_PREINIT_ARRAYSZ: name = "PREINIT_ARRAYSZ"; break; | |
d48188b9 | 1690 | case DT_CHECKSUM: name = "CHECKSUM"; break; |
94558834 L |
1691 | case DT_PLTPADSZ: name = "PLTPADSZ"; break; |
1692 | case DT_MOVEENT: name = "MOVEENT"; break; | |
1693 | case DT_MOVESZ: name = "MOVESZ"; break; | |
1694 | case DT_FEATURE: name = "FEATURE"; break; | |
1695 | case DT_POSFLAG_1: name = "POSFLAG_1"; break; | |
1696 | case DT_SYMINSZ: name = "SYMINSZ"; break; | |
1697 | case DT_SYMINENT: name = "SYMINENT"; break; | |
b34976b6 AM |
1698 | case DT_CONFIG: name = "CONFIG"; stringp = TRUE; break; |
1699 | case DT_DEPAUDIT: name = "DEPAUDIT"; stringp = TRUE; break; | |
1700 | case DT_AUDIT: name = "AUDIT"; stringp = TRUE; break; | |
94558834 L |
1701 | case DT_PLTPAD: name = "PLTPAD"; break; |
1702 | case DT_MOVETAB: name = "MOVETAB"; break; | |
1703 | case DT_SYMINFO: name = "SYMINFO"; break; | |
1704 | case DT_RELACOUNT: name = "RELACOUNT"; break; | |
1705 | case DT_RELCOUNT: name = "RELCOUNT"; break; | |
1706 | case DT_FLAGS_1: name = "FLAGS_1"; break; | |
252b5132 RH |
1707 | case DT_VERSYM: name = "VERSYM"; break; |
1708 | case DT_VERDEF: name = "VERDEF"; break; | |
1709 | case DT_VERDEFNUM: name = "VERDEFNUM"; break; | |
1710 | case DT_VERNEED: name = "VERNEED"; break; | |
1711 | case DT_VERNEEDNUM: name = "VERNEEDNUM"; break; | |
b34976b6 | 1712 | case DT_AUXILIARY: name = "AUXILIARY"; stringp = TRUE; break; |
94558834 | 1713 | case DT_USED: name = "USED"; break; |
b34976b6 | 1714 | case DT_FILTER: name = "FILTER"; stringp = TRUE; break; |
fdc90cb4 | 1715 | case DT_GNU_HASH: name = "GNU_HASH"; break; |
252b5132 RH |
1716 | } |
1717 | ||
ad9563d6 | 1718 | fprintf (f, " %-20s ", name); |
252b5132 | 1719 | if (! stringp) |
a1f3c56e AN |
1720 | { |
1721 | fprintf (f, "0x"); | |
1722 | bfd_fprintf_vma (abfd, f, dyn.d_un.d_val); | |
1723 | } | |
252b5132 RH |
1724 | else |
1725 | { | |
1726 | const char *string; | |
dc810e39 | 1727 | unsigned int tagv = dyn.d_un.d_val; |
252b5132 | 1728 | |
dc810e39 | 1729 | string = bfd_elf_string_from_elf_section (abfd, shlink, tagv); |
252b5132 RH |
1730 | if (string == NULL) |
1731 | goto error_return; | |
1732 | fprintf (f, "%s", string); | |
1733 | } | |
1734 | fprintf (f, "\n"); | |
1735 | } | |
1736 | ||
1737 | free (dynbuf); | |
1738 | dynbuf = NULL; | |
1739 | } | |
1740 | ||
1741 | if ((elf_dynverdef (abfd) != 0 && elf_tdata (abfd)->verdef == NULL) | |
1742 | || (elf_dynverref (abfd) != 0 && elf_tdata (abfd)->verref == NULL)) | |
1743 | { | |
fc0e6df6 | 1744 | if (! _bfd_elf_slurp_version_tables (abfd, FALSE)) |
b34976b6 | 1745 | return FALSE; |
252b5132 RH |
1746 | } |
1747 | ||
1748 | if (elf_dynverdef (abfd) != 0) | |
1749 | { | |
1750 | Elf_Internal_Verdef *t; | |
1751 | ||
1752 | fprintf (f, _("\nVersion definitions:\n")); | |
1753 | for (t = elf_tdata (abfd)->verdef; t != NULL; t = t->vd_nextdef) | |
1754 | { | |
1755 | fprintf (f, "%d 0x%2.2x 0x%8.8lx %s\n", t->vd_ndx, | |
d0fb9a8d JJ |
1756 | t->vd_flags, t->vd_hash, |
1757 | t->vd_nodename ? t->vd_nodename : "<corrupt>"); | |
1758 | if (t->vd_auxptr != NULL && t->vd_auxptr->vda_nextptr != NULL) | |
252b5132 RH |
1759 | { |
1760 | Elf_Internal_Verdaux *a; | |
1761 | ||
1762 | fprintf (f, "\t"); | |
1763 | for (a = t->vd_auxptr->vda_nextptr; | |
1764 | a != NULL; | |
1765 | a = a->vda_nextptr) | |
d0fb9a8d JJ |
1766 | fprintf (f, "%s ", |
1767 | a->vda_nodename ? a->vda_nodename : "<corrupt>"); | |
252b5132 RH |
1768 | fprintf (f, "\n"); |
1769 | } | |
1770 | } | |
1771 | } | |
1772 | ||
1773 | if (elf_dynverref (abfd) != 0) | |
1774 | { | |
1775 | Elf_Internal_Verneed *t; | |
1776 | ||
1777 | fprintf (f, _("\nVersion References:\n")); | |
1778 | for (t = elf_tdata (abfd)->verref; t != NULL; t = t->vn_nextref) | |
1779 | { | |
1780 | Elf_Internal_Vernaux *a; | |
1781 | ||
d0fb9a8d JJ |
1782 | fprintf (f, _(" required from %s:\n"), |
1783 | t->vn_filename ? t->vn_filename : "<corrupt>"); | |
252b5132 RH |
1784 | for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) |
1785 | fprintf (f, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a->vna_hash, | |
d0fb9a8d JJ |
1786 | a->vna_flags, a->vna_other, |
1787 | a->vna_nodename ? a->vna_nodename : "<corrupt>"); | |
252b5132 RH |
1788 | } |
1789 | } | |
1790 | ||
b34976b6 | 1791 | return TRUE; |
252b5132 RH |
1792 | |
1793 | error_return: | |
1794 | if (dynbuf != NULL) | |
1795 | free (dynbuf); | |
b34976b6 | 1796 | return FALSE; |
252b5132 RH |
1797 | } |
1798 | ||
bb4d2ac2 L |
1799 | /* Get version string. */ |
1800 | ||
1801 | const char * | |
60bb06bc L |
1802 | _bfd_elf_get_symbol_version_string (bfd *abfd, asymbol *symbol, |
1803 | bfd_boolean *hidden) | |
bb4d2ac2 L |
1804 | { |
1805 | const char *version_string = NULL; | |
1806 | if (elf_dynversym (abfd) != 0 | |
1807 | && (elf_dynverdef (abfd) != 0 || elf_dynverref (abfd) != 0)) | |
1808 | { | |
1809 | unsigned int vernum = ((elf_symbol_type *) symbol)->version; | |
1810 | ||
1811 | *hidden = (vernum & VERSYM_HIDDEN) != 0; | |
1812 | vernum &= VERSYM_VERSION; | |
1813 | ||
1814 | if (vernum == 0) | |
1815 | version_string = ""; | |
1816 | else if (vernum == 1) | |
1817 | version_string = "Base"; | |
1818 | else if (vernum <= elf_tdata (abfd)->cverdefs) | |
1819 | version_string = | |
1820 | elf_tdata (abfd)->verdef[vernum - 1].vd_nodename; | |
1821 | else | |
1822 | { | |
1823 | Elf_Internal_Verneed *t; | |
1824 | ||
1825 | version_string = ""; | |
1826 | for (t = elf_tdata (abfd)->verref; | |
1827 | t != NULL; | |
1828 | t = t->vn_nextref) | |
1829 | { | |
1830 | Elf_Internal_Vernaux *a; | |
1831 | ||
1832 | for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) | |
1833 | { | |
1834 | if (a->vna_other == vernum) | |
1835 | { | |
1836 | version_string = a->vna_nodename; | |
1837 | break; | |
1838 | } | |
1839 | } | |
1840 | } | |
1841 | } | |
1842 | } | |
1843 | return version_string; | |
1844 | } | |
1845 | ||
252b5132 RH |
1846 | /* Display ELF-specific fields of a symbol. */ |
1847 | ||
1848 | void | |
217aa764 AM |
1849 | bfd_elf_print_symbol (bfd *abfd, |
1850 | void *filep, | |
1851 | asymbol *symbol, | |
1852 | bfd_print_symbol_type how) | |
252b5132 | 1853 | { |
a50b1753 | 1854 | FILE *file = (FILE *) filep; |
252b5132 RH |
1855 | switch (how) |
1856 | { | |
1857 | case bfd_print_symbol_name: | |
1858 | fprintf (file, "%s", symbol->name); | |
1859 | break; | |
1860 | case bfd_print_symbol_more: | |
1861 | fprintf (file, "elf "); | |
60b89a18 | 1862 | bfd_fprintf_vma (abfd, file, symbol->value); |
0af1713e | 1863 | fprintf (file, " %lx", (unsigned long) symbol->flags); |
252b5132 RH |
1864 | break; |
1865 | case bfd_print_symbol_all: | |
1866 | { | |
4e8a9624 AM |
1867 | const char *section_name; |
1868 | const char *name = NULL; | |
9c5bfbb7 | 1869 | const struct elf_backend_data *bed; |
7a13edea | 1870 | unsigned char st_other; |
dbb410c3 | 1871 | bfd_vma val; |
bb4d2ac2 L |
1872 | const char *version_string; |
1873 | bfd_boolean hidden; | |
c044fabd | 1874 | |
252b5132 | 1875 | section_name = symbol->section ? symbol->section->name : "(*none*)"; |
587ff49e RH |
1876 | |
1877 | bed = get_elf_backend_data (abfd); | |
1878 | if (bed->elf_backend_print_symbol_all) | |
c044fabd | 1879 | name = (*bed->elf_backend_print_symbol_all) (abfd, filep, symbol); |
587ff49e RH |
1880 | |
1881 | if (name == NULL) | |
1882 | { | |
7ee38065 | 1883 | name = symbol->name; |
217aa764 | 1884 | bfd_print_symbol_vandf (abfd, file, symbol); |
587ff49e RH |
1885 | } |
1886 | ||
252b5132 RH |
1887 | fprintf (file, " %s\t", section_name); |
1888 | /* Print the "other" value for a symbol. For common symbols, | |
1889 | we've already printed the size; now print the alignment. | |
1890 | For other symbols, we have no specified alignment, and | |
1891 | we've printed the address; now print the size. */ | |
dcf6c779 | 1892 | if (symbol->section && bfd_is_com_section (symbol->section)) |
dbb410c3 AM |
1893 | val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_value; |
1894 | else | |
1895 | val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_size; | |
1896 | bfd_fprintf_vma (abfd, file, val); | |
252b5132 RH |
1897 | |
1898 | /* If we have version information, print it. */ | |
60bb06bc L |
1899 | version_string = _bfd_elf_get_symbol_version_string (abfd, |
1900 | symbol, | |
1901 | &hidden); | |
bb4d2ac2 | 1902 | if (version_string) |
252b5132 | 1903 | { |
bb4d2ac2 | 1904 | if (!hidden) |
252b5132 RH |
1905 | fprintf (file, " %-11s", version_string); |
1906 | else | |
1907 | { | |
1908 | int i; | |
1909 | ||
1910 | fprintf (file, " (%s)", version_string); | |
1911 | for (i = 10 - strlen (version_string); i > 0; --i) | |
1912 | putc (' ', file); | |
1913 | } | |
1914 | } | |
1915 | ||
1916 | /* If the st_other field is not zero, print it. */ | |
7a13edea | 1917 | st_other = ((elf_symbol_type *) symbol)->internal_elf_sym.st_other; |
c044fabd | 1918 | |
7a13edea NC |
1919 | switch (st_other) |
1920 | { | |
1921 | case 0: break; | |
1922 | case STV_INTERNAL: fprintf (file, " .internal"); break; | |
1923 | case STV_HIDDEN: fprintf (file, " .hidden"); break; | |
1924 | case STV_PROTECTED: fprintf (file, " .protected"); break; | |
1925 | default: | |
1926 | /* Some other non-defined flags are also present, so print | |
1927 | everything hex. */ | |
1928 | fprintf (file, " 0x%02x", (unsigned int) st_other); | |
1929 | } | |
252b5132 | 1930 | |
587ff49e | 1931 | fprintf (file, " %s", name); |
252b5132 RH |
1932 | } |
1933 | break; | |
1934 | } | |
1935 | } | |
252b5132 RH |
1936 | \f |
1937 | /* ELF .o/exec file reading */ | |
1938 | ||
c044fabd | 1939 | /* Create a new bfd section from an ELF section header. */ |
252b5132 | 1940 | |
b34976b6 | 1941 | bfd_boolean |
217aa764 | 1942 | bfd_section_from_shdr (bfd *abfd, unsigned int shindex) |
252b5132 | 1943 | { |
4fbb74a6 AM |
1944 | Elf_Internal_Shdr *hdr; |
1945 | Elf_Internal_Ehdr *ehdr; | |
1946 | const struct elf_backend_data *bed; | |
90937f86 | 1947 | const char *name; |
bf67003b NC |
1948 | bfd_boolean ret = TRUE; |
1949 | static bfd_boolean * sections_being_created = NULL; | |
5a4b0ccc | 1950 | static bfd * sections_being_created_abfd = NULL; |
bf67003b | 1951 | static unsigned int nesting = 0; |
252b5132 | 1952 | |
4fbb74a6 AM |
1953 | if (shindex >= elf_numsections (abfd)) |
1954 | return FALSE; | |
1955 | ||
bf67003b NC |
1956 | if (++ nesting > 3) |
1957 | { | |
1958 | /* PR17512: A corrupt ELF binary might contain a recursive group of | |
06614111 | 1959 | sections, with each the string indicies pointing to the next in the |
bf67003b NC |
1960 | loop. Detect this here, by refusing to load a section that we are |
1961 | already in the process of loading. We only trigger this test if | |
1962 | we have nested at least three sections deep as normal ELF binaries | |
5a4b0ccc NC |
1963 | can expect to recurse at least once. |
1964 | ||
1965 | FIXME: It would be better if this array was attached to the bfd, | |
1966 | rather than being held in a static pointer. */ | |
1967 | ||
1968 | if (sections_being_created_abfd != abfd) | |
1969 | sections_being_created = NULL; | |
bf67003b NC |
1970 | if (sections_being_created == NULL) |
1971 | { | |
1972 | /* FIXME: It would be more efficient to attach this array to the bfd somehow. */ | |
1973 | sections_being_created = (bfd_boolean *) | |
1974 | bfd_zalloc (abfd, elf_numsections (abfd) * sizeof (bfd_boolean)); | |
5a4b0ccc | 1975 | sections_being_created_abfd = abfd; |
bf67003b NC |
1976 | } |
1977 | if (sections_being_created [shindex]) | |
1978 | { | |
4eca0228 | 1979 | _bfd_error_handler |
bf67003b NC |
1980 | (_("%B: warning: loop in section dependencies detected"), abfd); |
1981 | return FALSE; | |
1982 | } | |
1983 | sections_being_created [shindex] = TRUE; | |
1984 | } | |
1985 | ||
4fbb74a6 AM |
1986 | hdr = elf_elfsections (abfd)[shindex]; |
1987 | ehdr = elf_elfheader (abfd); | |
1988 | name = bfd_elf_string_from_elf_section (abfd, ehdr->e_shstrndx, | |
1b3a8575 | 1989 | hdr->sh_name); |
933d961a | 1990 | if (name == NULL) |
bf67003b | 1991 | goto fail; |
252b5132 | 1992 | |
4fbb74a6 | 1993 | bed = get_elf_backend_data (abfd); |
252b5132 RH |
1994 | switch (hdr->sh_type) |
1995 | { | |
1996 | case SHT_NULL: | |
1997 | /* Inactive section. Throw it away. */ | |
bf67003b | 1998 | goto success; |
252b5132 | 1999 | |
bf67003b NC |
2000 | case SHT_PROGBITS: /* Normal section with contents. */ |
2001 | case SHT_NOBITS: /* .bss section. */ | |
2002 | case SHT_HASH: /* .hash section. */ | |
2003 | case SHT_NOTE: /* .note section. */ | |
25e27870 L |
2004 | case SHT_INIT_ARRAY: /* .init_array section. */ |
2005 | case SHT_FINI_ARRAY: /* .fini_array section. */ | |
2006 | case SHT_PREINIT_ARRAY: /* .preinit_array section. */ | |
7f1204bb | 2007 | case SHT_GNU_LIBLIST: /* .gnu.liblist section. */ |
fdc90cb4 | 2008 | case SHT_GNU_HASH: /* .gnu.hash section. */ |
bf67003b NC |
2009 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); |
2010 | goto success; | |
252b5132 | 2011 | |
797fc050 | 2012 | case SHT_DYNAMIC: /* Dynamic linking information. */ |
6dc132d9 | 2013 | if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex)) |
bf67003b NC |
2014 | goto fail; |
2015 | ||
cfcac11d NC |
2016 | if (hdr->sh_link > elf_numsections (abfd)) |
2017 | { | |
caa83f8b | 2018 | /* PR 10478: Accept Solaris binaries with a sh_link |
cfcac11d NC |
2019 | field set to SHN_BEFORE or SHN_AFTER. */ |
2020 | switch (bfd_get_arch (abfd)) | |
2021 | { | |
caa83f8b | 2022 | case bfd_arch_i386: |
cfcac11d NC |
2023 | case bfd_arch_sparc: |
2024 | if (hdr->sh_link == (SHN_LORESERVE & 0xffff) /* SHN_BEFORE */ | |
2025 | || hdr->sh_link == ((SHN_LORESERVE + 1) & 0xffff) /* SHN_AFTER */) | |
2026 | break; | |
2027 | /* Otherwise fall through. */ | |
2028 | default: | |
bf67003b | 2029 | goto fail; |
cfcac11d NC |
2030 | } |
2031 | } | |
2032 | else if (elf_elfsections (abfd)[hdr->sh_link] == NULL) | |
bf67003b | 2033 | goto fail; |
cfcac11d | 2034 | else if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_STRTAB) |
797fc050 AM |
2035 | { |
2036 | Elf_Internal_Shdr *dynsymhdr; | |
2037 | ||
2038 | /* The shared libraries distributed with hpux11 have a bogus | |
2039 | sh_link field for the ".dynamic" section. Find the | |
2040 | string table for the ".dynsym" section instead. */ | |
2041 | if (elf_dynsymtab (abfd) != 0) | |
2042 | { | |
2043 | dynsymhdr = elf_elfsections (abfd)[elf_dynsymtab (abfd)]; | |
2044 | hdr->sh_link = dynsymhdr->sh_link; | |
2045 | } | |
2046 | else | |
2047 | { | |
2048 | unsigned int i, num_sec; | |
2049 | ||
2050 | num_sec = elf_numsections (abfd); | |
2051 | for (i = 1; i < num_sec; i++) | |
2052 | { | |
2053 | dynsymhdr = elf_elfsections (abfd)[i]; | |
2054 | if (dynsymhdr->sh_type == SHT_DYNSYM) | |
2055 | { | |
2056 | hdr->sh_link = dynsymhdr->sh_link; | |
2057 | break; | |
2058 | } | |
2059 | } | |
2060 | } | |
2061 | } | |
bf67003b | 2062 | goto success; |
797fc050 | 2063 | |
bf67003b | 2064 | case SHT_SYMTAB: /* A symbol table. */ |
252b5132 | 2065 | if (elf_onesymtab (abfd) == shindex) |
bf67003b | 2066 | goto success; |
252b5132 | 2067 | |
a50b2160 | 2068 | if (hdr->sh_entsize != bed->s->sizeof_sym) |
bf67003b NC |
2069 | goto fail; |
2070 | ||
3337c1e5 | 2071 | if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size) |
eee3b786 AM |
2072 | { |
2073 | if (hdr->sh_size != 0) | |
bf67003b | 2074 | goto fail; |
eee3b786 AM |
2075 | /* Some assemblers erroneously set sh_info to one with a |
2076 | zero sh_size. ld sees this as a global symbol count | |
2077 | of (unsigned) -1. Fix it here. */ | |
2078 | hdr->sh_info = 0; | |
bf67003b | 2079 | goto success; |
eee3b786 | 2080 | } |
bf67003b | 2081 | |
16ad13ec NC |
2082 | /* PR 18854: A binary might contain more than one symbol table. |
2083 | Unusual, but possible. Warn, but continue. */ | |
2084 | if (elf_onesymtab (abfd) != 0) | |
2085 | { | |
4eca0228 | 2086 | _bfd_error_handler |
695344c0 | 2087 | /* xgettext:c-format */ |
16ad13ec NC |
2088 | (_("%B: warning: multiple symbol tables detected - ignoring the table in section %u"), |
2089 | abfd, shindex); | |
2090 | goto success; | |
2091 | } | |
252b5132 | 2092 | elf_onesymtab (abfd) = shindex; |
6a40cf0c NC |
2093 | elf_symtab_hdr (abfd) = *hdr; |
2094 | elf_elfsections (abfd)[shindex] = hdr = & elf_symtab_hdr (abfd); | |
252b5132 RH |
2095 | abfd->flags |= HAS_SYMS; |
2096 | ||
2097 | /* Sometimes a shared object will map in the symbol table. If | |
08a40648 AM |
2098 | SHF_ALLOC is set, and this is a shared object, then we also |
2099 | treat this section as a BFD section. We can not base the | |
2100 | decision purely on SHF_ALLOC, because that flag is sometimes | |
2101 | set in a relocatable object file, which would confuse the | |
2102 | linker. */ | |
252b5132 RH |
2103 | if ((hdr->sh_flags & SHF_ALLOC) != 0 |
2104 | && (abfd->flags & DYNAMIC) != 0 | |
6dc132d9 L |
2105 | && ! _bfd_elf_make_section_from_shdr (abfd, hdr, name, |
2106 | shindex)) | |
bf67003b | 2107 | goto fail; |
252b5132 | 2108 | |
1b3a8575 AM |
2109 | /* Go looking for SHT_SYMTAB_SHNDX too, since if there is one we |
2110 | can't read symbols without that section loaded as well. It | |
2111 | is most likely specified by the next section header. */ | |
6a40cf0c NC |
2112 | { |
2113 | elf_section_list * entry; | |
2114 | unsigned int i, num_sec; | |
1b3a8575 | 2115 | |
6a40cf0c NC |
2116 | for (entry = elf_symtab_shndx_list (abfd); entry != NULL; entry = entry->next) |
2117 | if (entry->hdr.sh_link == shindex) | |
2118 | goto success; | |
2119 | ||
2120 | num_sec = elf_numsections (abfd); | |
2121 | for (i = shindex + 1; i < num_sec; i++) | |
2122 | { | |
2123 | Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i]; | |
2124 | ||
2125 | if (hdr2->sh_type == SHT_SYMTAB_SHNDX | |
2126 | && hdr2->sh_link == shindex) | |
2127 | break; | |
2128 | } | |
2129 | ||
2130 | if (i == num_sec) | |
2131 | for (i = 1; i < shindex; i++) | |
1b3a8575 AM |
2132 | { |
2133 | Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i]; | |
6a40cf0c | 2134 | |
1b3a8575 AM |
2135 | if (hdr2->sh_type == SHT_SYMTAB_SHNDX |
2136 | && hdr2->sh_link == shindex) | |
2137 | break; | |
2138 | } | |
6a40cf0c NC |
2139 | |
2140 | if (i != shindex) | |
2141 | ret = bfd_section_from_shdr (abfd, i); | |
2142 | /* else FIXME: we have failed to find the symbol table - should we issue an error ? */ | |
2143 | goto success; | |
2144 | } | |
252b5132 | 2145 | |
bf67003b | 2146 | case SHT_DYNSYM: /* A dynamic symbol table. */ |
252b5132 | 2147 | if (elf_dynsymtab (abfd) == shindex) |
bf67003b | 2148 | goto success; |
252b5132 | 2149 | |
a50b2160 | 2150 | if (hdr->sh_entsize != bed->s->sizeof_sym) |
bf67003b NC |
2151 | goto fail; |
2152 | ||
eee3b786 AM |
2153 | if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size) |
2154 | { | |
2155 | if (hdr->sh_size != 0) | |
bf67003b NC |
2156 | goto fail; |
2157 | ||
eee3b786 AM |
2158 | /* Some linkers erroneously set sh_info to one with a |
2159 | zero sh_size. ld sees this as a global symbol count | |
2160 | of (unsigned) -1. Fix it here. */ | |
2161 | hdr->sh_info = 0; | |
bf67003b | 2162 | goto success; |
eee3b786 | 2163 | } |
bf67003b | 2164 | |
16ad13ec NC |
2165 | /* PR 18854: A binary might contain more than one dynamic symbol table. |
2166 | Unusual, but possible. Warn, but continue. */ | |
2167 | if (elf_dynsymtab (abfd) != 0) | |
2168 | { | |
4eca0228 | 2169 | _bfd_error_handler |
695344c0 | 2170 | /* xgettext:c-format */ |
16ad13ec NC |
2171 | (_("%B: warning: multiple dynamic symbol tables detected - ignoring the table in section %u"), |
2172 | abfd, shindex); | |
2173 | goto success; | |
2174 | } | |
252b5132 RH |
2175 | elf_dynsymtab (abfd) = shindex; |
2176 | elf_tdata (abfd)->dynsymtab_hdr = *hdr; | |
2177 | elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->dynsymtab_hdr; | |
2178 | abfd->flags |= HAS_SYMS; | |
2179 | ||
2180 | /* Besides being a symbol table, we also treat this as a regular | |
2181 | section, so that objcopy can handle it. */ | |
bf67003b NC |
2182 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); |
2183 | goto success; | |
252b5132 | 2184 | |
bf67003b | 2185 | case SHT_SYMTAB_SHNDX: /* Symbol section indices when >64k sections. */ |
6a40cf0c NC |
2186 | { |
2187 | elf_section_list * entry; | |
9ad5cbcf | 2188 | |
6a40cf0c NC |
2189 | for (entry = elf_symtab_shndx_list (abfd); entry != NULL; entry = entry->next) |
2190 | if (entry->ndx == shindex) | |
2191 | goto success; | |
2192 | ||
2193 | entry = bfd_alloc (abfd, sizeof * entry); | |
2194 | if (entry == NULL) | |
2195 | goto fail; | |
2196 | entry->ndx = shindex; | |
2197 | entry->hdr = * hdr; | |
2198 | entry->next = elf_symtab_shndx_list (abfd); | |
2199 | elf_symtab_shndx_list (abfd) = entry; | |
2200 | elf_elfsections (abfd)[shindex] = & entry->hdr; | |
2201 | goto success; | |
2202 | } | |
9ad5cbcf | 2203 | |
bf67003b | 2204 | case SHT_STRTAB: /* A string table. */ |
252b5132 | 2205 | if (hdr->bfd_section != NULL) |
bf67003b NC |
2206 | goto success; |
2207 | ||
252b5132 RH |
2208 | if (ehdr->e_shstrndx == shindex) |
2209 | { | |
2210 | elf_tdata (abfd)->shstrtab_hdr = *hdr; | |
2211 | elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->shstrtab_hdr; | |
bf67003b | 2212 | goto success; |
252b5132 | 2213 | } |
bf67003b | 2214 | |
1b3a8575 AM |
2215 | if (elf_elfsections (abfd)[elf_onesymtab (abfd)]->sh_link == shindex) |
2216 | { | |
2217 | symtab_strtab: | |
2218 | elf_tdata (abfd)->strtab_hdr = *hdr; | |
2219 | elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->strtab_hdr; | |
bf67003b | 2220 | goto success; |
1b3a8575 | 2221 | } |
bf67003b | 2222 | |
1b3a8575 AM |
2223 | if (elf_elfsections (abfd)[elf_dynsymtab (abfd)]->sh_link == shindex) |
2224 | { | |
2225 | dynsymtab_strtab: | |
2226 | elf_tdata (abfd)->dynstrtab_hdr = *hdr; | |
2227 | hdr = &elf_tdata (abfd)->dynstrtab_hdr; | |
2228 | elf_elfsections (abfd)[shindex] = hdr; | |
2229 | /* We also treat this as a regular section, so that objcopy | |
2230 | can handle it. */ | |
bf67003b NC |
2231 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, |
2232 | shindex); | |
2233 | goto success; | |
1b3a8575 | 2234 | } |
252b5132 | 2235 | |
1b3a8575 AM |
2236 | /* If the string table isn't one of the above, then treat it as a |
2237 | regular section. We need to scan all the headers to be sure, | |
2238 | just in case this strtab section appeared before the above. */ | |
2239 | if (elf_onesymtab (abfd) == 0 || elf_dynsymtab (abfd) == 0) | |
2240 | { | |
2241 | unsigned int i, num_sec; | |
252b5132 | 2242 | |
1b3a8575 AM |
2243 | num_sec = elf_numsections (abfd); |
2244 | for (i = 1; i < num_sec; i++) | |
2245 | { | |
2246 | Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i]; | |
2247 | if (hdr2->sh_link == shindex) | |
2248 | { | |
933d961a JJ |
2249 | /* Prevent endless recursion on broken objects. */ |
2250 | if (i == shindex) | |
bf67003b | 2251 | goto fail; |
1b3a8575 | 2252 | if (! bfd_section_from_shdr (abfd, i)) |
bf67003b | 2253 | goto fail; |
1b3a8575 AM |
2254 | if (elf_onesymtab (abfd) == i) |
2255 | goto symtab_strtab; | |
2256 | if (elf_dynsymtab (abfd) == i) | |
2257 | goto dynsymtab_strtab; | |
2258 | } | |
2259 | } | |
2260 | } | |
bf67003b NC |
2261 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); |
2262 | goto success; | |
252b5132 RH |
2263 | |
2264 | case SHT_REL: | |
2265 | case SHT_RELA: | |
2266 | /* *These* do a lot of work -- but build no sections! */ | |
2267 | { | |
2268 | asection *target_sect; | |
d4730f92 | 2269 | Elf_Internal_Shdr *hdr2, **p_hdr; |
9ad5cbcf | 2270 | unsigned int num_sec = elf_numsections (abfd); |
d4730f92 | 2271 | struct bfd_elf_section_data *esdt; |
252b5132 | 2272 | |
aa2ca951 JJ |
2273 | if (hdr->sh_entsize |
2274 | != (bfd_size_type) (hdr->sh_type == SHT_REL | |
a50b2160 | 2275 | ? bed->s->sizeof_rel : bed->s->sizeof_rela)) |
bf67003b | 2276 | goto fail; |
a50b2160 | 2277 | |
03ae5f59 | 2278 | /* Check for a bogus link to avoid crashing. */ |
4fbb74a6 | 2279 | if (hdr->sh_link >= num_sec) |
03ae5f59 | 2280 | { |
4eca0228 | 2281 | _bfd_error_handler |
695344c0 | 2282 | /* xgettext:c-format */ |
4eca0228 AM |
2283 | (_("%B: invalid link %lu for reloc section %s (index %u)"), |
2284 | abfd, hdr->sh_link, name, shindex); | |
bf67003b NC |
2285 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, |
2286 | shindex); | |
2287 | goto success; | |
03ae5f59 ILT |
2288 | } |
2289 | ||
252b5132 RH |
2290 | /* For some incomprehensible reason Oracle distributes |
2291 | libraries for Solaris in which some of the objects have | |
2292 | bogus sh_link fields. It would be nice if we could just | |
2293 | reject them, but, unfortunately, some people need to use | |
2294 | them. We scan through the section headers; if we find only | |
2295 | one suitable symbol table, we clobber the sh_link to point | |
83b89087 L |
2296 | to it. I hope this doesn't break anything. |
2297 | ||
2298 | Don't do it on executable nor shared library. */ | |
2299 | if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0 | |
2300 | && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_SYMTAB | |
252b5132 RH |
2301 | && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_DYNSYM) |
2302 | { | |
9ad5cbcf | 2303 | unsigned int scan; |
252b5132 RH |
2304 | int found; |
2305 | ||
2306 | found = 0; | |
9ad5cbcf | 2307 | for (scan = 1; scan < num_sec; scan++) |
252b5132 RH |
2308 | { |
2309 | if (elf_elfsections (abfd)[scan]->sh_type == SHT_SYMTAB | |
2310 | || elf_elfsections (abfd)[scan]->sh_type == SHT_DYNSYM) | |
2311 | { | |
2312 | if (found != 0) | |
2313 | { | |
2314 | found = 0; | |
2315 | break; | |
2316 | } | |
2317 | found = scan; | |
2318 | } | |
2319 | } | |
2320 | if (found != 0) | |
2321 | hdr->sh_link = found; | |
2322 | } | |
2323 | ||
2324 | /* Get the symbol table. */ | |
1b3a8575 AM |
2325 | if ((elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_SYMTAB |
2326 | || elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_DYNSYM) | |
252b5132 | 2327 | && ! bfd_section_from_shdr (abfd, hdr->sh_link)) |
bf67003b | 2328 | goto fail; |
252b5132 RH |
2329 | |
2330 | /* If this reloc section does not use the main symbol table we | |
2331 | don't treat it as a reloc section. BFD can't adequately | |
2332 | represent such a section, so at least for now, we don't | |
c044fabd | 2333 | try. We just present it as a normal section. We also |
60bcf0fa | 2334 | can't use it as a reloc section if it points to the null |
83b89087 L |
2335 | section, an invalid section, another reloc section, or its |
2336 | sh_link points to the null section. */ | |
185ef66d | 2337 | if (hdr->sh_link != elf_onesymtab (abfd) |
83b89087 | 2338 | || hdr->sh_link == SHN_UNDEF |
185ef66d | 2339 | || hdr->sh_info == SHN_UNDEF |
185ef66d AM |
2340 | || hdr->sh_info >= num_sec |
2341 | || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_REL | |
2342 | || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_RELA) | |
bf67003b NC |
2343 | { |
2344 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, | |
2345 | shindex); | |
2346 | goto success; | |
2347 | } | |
252b5132 RH |
2348 | |
2349 | if (! bfd_section_from_shdr (abfd, hdr->sh_info)) | |
bf67003b NC |
2350 | goto fail; |
2351 | ||
252b5132 RH |
2352 | target_sect = bfd_section_from_elf_index (abfd, hdr->sh_info); |
2353 | if (target_sect == NULL) | |
bf67003b | 2354 | goto fail; |
252b5132 | 2355 | |
d4730f92 BS |
2356 | esdt = elf_section_data (target_sect); |
2357 | if (hdr->sh_type == SHT_RELA) | |
2358 | p_hdr = &esdt->rela.hdr; | |
252b5132 | 2359 | else |
d4730f92 BS |
2360 | p_hdr = &esdt->rel.hdr; |
2361 | ||
06614111 NC |
2362 | /* PR 17512: file: 0b4f81b7. */ |
2363 | if (*p_hdr != NULL) | |
2364 | goto fail; | |
ef53be89 | 2365 | hdr2 = (Elf_Internal_Shdr *) bfd_alloc (abfd, sizeof (*hdr2)); |
d4730f92 | 2366 | if (hdr2 == NULL) |
bf67003b | 2367 | goto fail; |
252b5132 | 2368 | *hdr2 = *hdr; |
d4730f92 | 2369 | *p_hdr = hdr2; |
252b5132 | 2370 | elf_elfsections (abfd)[shindex] = hdr2; |
d9bc7a44 | 2371 | target_sect->reloc_count += NUM_SHDR_ENTRIES (hdr); |
252b5132 RH |
2372 | target_sect->flags |= SEC_RELOC; |
2373 | target_sect->relocation = NULL; | |
2374 | target_sect->rel_filepos = hdr->sh_offset; | |
bf572ba0 MM |
2375 | /* In the section to which the relocations apply, mark whether |
2376 | its relocations are of the REL or RELA variety. */ | |
72730e0c | 2377 | if (hdr->sh_size != 0) |
d4730f92 BS |
2378 | { |
2379 | if (hdr->sh_type == SHT_RELA) | |
2380 | target_sect->use_rela_p = 1; | |
2381 | } | |
252b5132 | 2382 | abfd->flags |= HAS_RELOC; |
bf67003b | 2383 | goto success; |
252b5132 | 2384 | } |
252b5132 RH |
2385 | |
2386 | case SHT_GNU_verdef: | |
2387 | elf_dynverdef (abfd) = shindex; | |
2388 | elf_tdata (abfd)->dynverdef_hdr = *hdr; | |
bf67003b NC |
2389 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); |
2390 | goto success; | |
252b5132 RH |
2391 | |
2392 | case SHT_GNU_versym: | |
a50b2160 | 2393 | if (hdr->sh_entsize != sizeof (Elf_External_Versym)) |
bf67003b NC |
2394 | goto fail; |
2395 | ||
252b5132 RH |
2396 | elf_dynversym (abfd) = shindex; |
2397 | elf_tdata (abfd)->dynversym_hdr = *hdr; | |
bf67003b NC |
2398 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); |
2399 | goto success; | |
252b5132 RH |
2400 | |
2401 | case SHT_GNU_verneed: | |
2402 | elf_dynverref (abfd) = shindex; | |
2403 | elf_tdata (abfd)->dynverref_hdr = *hdr; | |
bf67003b NC |
2404 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); |
2405 | goto success; | |
252b5132 RH |
2406 | |
2407 | case SHT_SHLIB: | |
bf67003b | 2408 | goto success; |
252b5132 | 2409 | |
dbb410c3 | 2410 | case SHT_GROUP: |
44534af3 | 2411 | if (! IS_VALID_GROUP_SECTION_HEADER (hdr, GRP_ENTRY_SIZE)) |
bf67003b NC |
2412 | goto fail; |
2413 | ||
6dc132d9 | 2414 | if (!_bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex)) |
bf67003b NC |
2415 | goto fail; |
2416 | ||
dbb410c3 AM |
2417 | if (hdr->contents != NULL) |
2418 | { | |
2419 | Elf_Internal_Group *idx = (Elf_Internal_Group *) hdr->contents; | |
06614111 | 2420 | unsigned int n_elt = hdr->sh_size / sizeof (* idx); |
dbb410c3 AM |
2421 | asection *s; |
2422 | ||
06614111 NC |
2423 | if (n_elt == 0) |
2424 | goto fail; | |
b885599b AM |
2425 | if (idx->flags & GRP_COMDAT) |
2426 | hdr->bfd_section->flags | |
2427 | |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD; | |
2428 | ||
45c5e9ed L |
2429 | /* We try to keep the same section order as it comes in. */ |
2430 | idx += n_elt; | |
06614111 | 2431 | |
dbb410c3 | 2432 | while (--n_elt != 0) |
1783205a NC |
2433 | { |
2434 | --idx; | |
2435 | ||
2436 | if (idx->shdr != NULL | |
2437 | && (s = idx->shdr->bfd_section) != NULL | |
2438 | && elf_next_in_group (s) != NULL) | |
2439 | { | |
2440 | elf_next_in_group (hdr->bfd_section) = s; | |
2441 | break; | |
2442 | } | |
2443 | } | |
dbb410c3 | 2444 | } |
bf67003b | 2445 | goto success; |
dbb410c3 | 2446 | |
252b5132 | 2447 | default: |
104d59d1 JM |
2448 | /* Possibly an attributes section. */ |
2449 | if (hdr->sh_type == SHT_GNU_ATTRIBUTES | |
2450 | || hdr->sh_type == bed->obj_attrs_section_type) | |
2451 | { | |
2452 | if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex)) | |
bf67003b | 2453 | goto fail; |
104d59d1 | 2454 | _bfd_elf_parse_attributes (abfd, hdr); |
bf67003b | 2455 | goto success; |
104d59d1 JM |
2456 | } |
2457 | ||
252b5132 | 2458 | /* Check for any processor-specific section types. */ |
3eb70a79 | 2459 | if (bed->elf_backend_section_from_shdr (abfd, hdr, name, shindex)) |
bf67003b | 2460 | goto success; |
3eb70a79 L |
2461 | |
2462 | if (hdr->sh_type >= SHT_LOUSER && hdr->sh_type <= SHT_HIUSER) | |
2463 | { | |
2464 | if ((hdr->sh_flags & SHF_ALLOC) != 0) | |
2465 | /* FIXME: How to properly handle allocated section reserved | |
2466 | for applications? */ | |
4eca0228 | 2467 | _bfd_error_handler |
695344c0 | 2468 | /* xgettext:c-format */ |
3eb70a79 L |
2469 | (_("%B: don't know how to handle allocated, application " |
2470 | "specific section `%s' [0x%8x]"), | |
2471 | abfd, name, hdr->sh_type); | |
2472 | else | |
bf67003b NC |
2473 | { |
2474 | /* Allow sections reserved for applications. */ | |
2475 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, | |
2476 | shindex); | |
2477 | goto success; | |
2478 | } | |
3eb70a79 L |
2479 | } |
2480 | else if (hdr->sh_type >= SHT_LOPROC | |
2481 | && hdr->sh_type <= SHT_HIPROC) | |
2482 | /* FIXME: We should handle this section. */ | |
4eca0228 | 2483 | _bfd_error_handler |
695344c0 | 2484 | /* xgettext:c-format */ |
3eb70a79 L |
2485 | (_("%B: don't know how to handle processor specific section " |
2486 | "`%s' [0x%8x]"), | |
2487 | abfd, name, hdr->sh_type); | |
2488 | else if (hdr->sh_type >= SHT_LOOS && hdr->sh_type <= SHT_HIOS) | |
ff15b240 NC |
2489 | { |
2490 | /* Unrecognised OS-specific sections. */ | |
2491 | if ((hdr->sh_flags & SHF_OS_NONCONFORMING) != 0) | |
2492 | /* SHF_OS_NONCONFORMING indicates that special knowledge is | |
08a40648 | 2493 | required to correctly process the section and the file should |
ff15b240 | 2494 | be rejected with an error message. */ |
4eca0228 | 2495 | _bfd_error_handler |
695344c0 | 2496 | /* xgettext:c-format */ |
ff15b240 NC |
2497 | (_("%B: don't know how to handle OS specific section " |
2498 | "`%s' [0x%8x]"), | |
2499 | abfd, name, hdr->sh_type); | |
2500 | else | |
bf67003b NC |
2501 | { |
2502 | /* Otherwise it should be processed. */ | |
2503 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); | |
2504 | goto success; | |
2505 | } | |
ff15b240 | 2506 | } |
3eb70a79 L |
2507 | else |
2508 | /* FIXME: We should handle this section. */ | |
4eca0228 | 2509 | _bfd_error_handler |
695344c0 | 2510 | /* xgettext:c-format */ |
3eb70a79 L |
2511 | (_("%B: don't know how to handle section `%s' [0x%8x]"), |
2512 | abfd, name, hdr->sh_type); | |
2513 | ||
bf67003b | 2514 | goto fail; |
252b5132 RH |
2515 | } |
2516 | ||
bf67003b NC |
2517 | fail: |
2518 | ret = FALSE; | |
2519 | success: | |
e5b470e2 | 2520 | if (sections_being_created && sections_being_created_abfd == abfd) |
bf67003b NC |
2521 | sections_being_created [shindex] = FALSE; |
2522 | if (-- nesting == 0) | |
5a4b0ccc NC |
2523 | { |
2524 | sections_being_created = NULL; | |
2525 | sections_being_created_abfd = abfd; | |
2526 | } | |
bf67003b | 2527 | return ret; |
252b5132 RH |
2528 | } |
2529 | ||
87d72d41 | 2530 | /* Return the local symbol specified by ABFD, R_SYMNDX. */ |
ec338859 | 2531 | |
87d72d41 AM |
2532 | Elf_Internal_Sym * |
2533 | bfd_sym_from_r_symndx (struct sym_cache *cache, | |
2534 | bfd *abfd, | |
2535 | unsigned long r_symndx) | |
ec338859 | 2536 | { |
ec338859 AM |
2537 | unsigned int ent = r_symndx % LOCAL_SYM_CACHE_SIZE; |
2538 | ||
a5d1b3b5 AM |
2539 | if (cache->abfd != abfd || cache->indx[ent] != r_symndx) |
2540 | { | |
2541 | Elf_Internal_Shdr *symtab_hdr; | |
2542 | unsigned char esym[sizeof (Elf64_External_Sym)]; | |
2543 | Elf_External_Sym_Shndx eshndx; | |
ec338859 | 2544 | |
a5d1b3b5 AM |
2545 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; |
2546 | if (bfd_elf_get_elf_syms (abfd, symtab_hdr, 1, r_symndx, | |
87d72d41 | 2547 | &cache->sym[ent], esym, &eshndx) == NULL) |
a5d1b3b5 | 2548 | return NULL; |
9ad5cbcf | 2549 | |
a5d1b3b5 AM |
2550 | if (cache->abfd != abfd) |
2551 | { | |
2552 | memset (cache->indx, -1, sizeof (cache->indx)); | |
2553 | cache->abfd = abfd; | |
2554 | } | |
2555 | cache->indx[ent] = r_symndx; | |
ec338859 | 2556 | } |
a5d1b3b5 | 2557 | |
87d72d41 | 2558 | return &cache->sym[ent]; |
ec338859 AM |
2559 | } |
2560 | ||
252b5132 RH |
2561 | /* Given an ELF section number, retrieve the corresponding BFD |
2562 | section. */ | |
2563 | ||
2564 | asection * | |
91d6fa6a | 2565 | bfd_section_from_elf_index (bfd *abfd, unsigned int sec_index) |
252b5132 | 2566 | { |
91d6fa6a | 2567 | if (sec_index >= elf_numsections (abfd)) |
252b5132 | 2568 | return NULL; |
91d6fa6a | 2569 | return elf_elfsections (abfd)[sec_index]->bfd_section; |
252b5132 RH |
2570 | } |
2571 | ||
b35d266b | 2572 | static const struct bfd_elf_special_section special_sections_b[] = |
2f89ff8d | 2573 | { |
0112cd26 NC |
2574 | { STRING_COMMA_LEN (".bss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE }, |
2575 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2576 | }; |
2577 | ||
b35d266b | 2578 | static const struct bfd_elf_special_section special_sections_c[] = |
7f4d3958 | 2579 | { |
0112cd26 NC |
2580 | { STRING_COMMA_LEN (".comment"), 0, SHT_PROGBITS, 0 }, |
2581 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2582 | }; |
2583 | ||
b35d266b | 2584 | static const struct bfd_elf_special_section special_sections_d[] = |
7f4d3958 | 2585 | { |
0112cd26 NC |
2586 | { STRING_COMMA_LEN (".data"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE }, |
2587 | { STRING_COMMA_LEN (".data1"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE }, | |
a9a72a65 DE |
2588 | /* There are more DWARF sections than these, but they needn't be added here |
2589 | unless you have to cope with broken compilers that don't emit section | |
2590 | attributes or you want to help the user writing assembler. */ | |
0112cd26 NC |
2591 | { STRING_COMMA_LEN (".debug"), 0, SHT_PROGBITS, 0 }, |
2592 | { STRING_COMMA_LEN (".debug_line"), 0, SHT_PROGBITS, 0 }, | |
2593 | { STRING_COMMA_LEN (".debug_info"), 0, SHT_PROGBITS, 0 }, | |
2594 | { STRING_COMMA_LEN (".debug_abbrev"), 0, SHT_PROGBITS, 0 }, | |
2595 | { STRING_COMMA_LEN (".debug_aranges"), 0, SHT_PROGBITS, 0 }, | |
2596 | { STRING_COMMA_LEN (".dynamic"), 0, SHT_DYNAMIC, SHF_ALLOC }, | |
2597 | { STRING_COMMA_LEN (".dynstr"), 0, SHT_STRTAB, SHF_ALLOC }, | |
2598 | { STRING_COMMA_LEN (".dynsym"), 0, SHT_DYNSYM, SHF_ALLOC }, | |
2599 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2600 | }; |
2601 | ||
b35d266b | 2602 | static const struct bfd_elf_special_section special_sections_f[] = |
7f4d3958 | 2603 | { |
0112cd26 NC |
2604 | { STRING_COMMA_LEN (".fini"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR }, |
2605 | { STRING_COMMA_LEN (".fini_array"), 0, SHT_FINI_ARRAY, SHF_ALLOC + SHF_WRITE }, | |
2606 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2607 | }; |
2608 | ||
b35d266b | 2609 | static const struct bfd_elf_special_section special_sections_g[] = |
7f4d3958 | 2610 | { |
0112cd26 | 2611 | { STRING_COMMA_LEN (".gnu.linkonce.b"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE }, |
68efed41 | 2612 | { STRING_COMMA_LEN (".gnu.lto_"), -1, SHT_PROGBITS, SHF_EXCLUDE }, |
0112cd26 NC |
2613 | { STRING_COMMA_LEN (".got"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE }, |
2614 | { STRING_COMMA_LEN (".gnu.version"), 0, SHT_GNU_versym, 0 }, | |
2615 | { STRING_COMMA_LEN (".gnu.version_d"), 0, SHT_GNU_verdef, 0 }, | |
2616 | { STRING_COMMA_LEN (".gnu.version_r"), 0, SHT_GNU_verneed, 0 }, | |
2617 | { STRING_COMMA_LEN (".gnu.liblist"), 0, SHT_GNU_LIBLIST, SHF_ALLOC }, | |
2618 | { STRING_COMMA_LEN (".gnu.conflict"), 0, SHT_RELA, SHF_ALLOC }, | |
2619 | { STRING_COMMA_LEN (".gnu.hash"), 0, SHT_GNU_HASH, SHF_ALLOC }, | |
2620 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2621 | }; |
2622 | ||
b35d266b | 2623 | static const struct bfd_elf_special_section special_sections_h[] = |
7f4d3958 | 2624 | { |
0112cd26 NC |
2625 | { STRING_COMMA_LEN (".hash"), 0, SHT_HASH, SHF_ALLOC }, |
2626 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2627 | }; |
2628 | ||
b35d266b | 2629 | static const struct bfd_elf_special_section special_sections_i[] = |
7f4d3958 | 2630 | { |
0112cd26 NC |
2631 | { STRING_COMMA_LEN (".init"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR }, |
2632 | { STRING_COMMA_LEN (".init_array"), 0, SHT_INIT_ARRAY, SHF_ALLOC + SHF_WRITE }, | |
2633 | { STRING_COMMA_LEN (".interp"), 0, SHT_PROGBITS, 0 }, | |
2634 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2635 | }; |
2636 | ||
b35d266b | 2637 | static const struct bfd_elf_special_section special_sections_l[] = |
7f4d3958 | 2638 | { |
0112cd26 NC |
2639 | { STRING_COMMA_LEN (".line"), 0, SHT_PROGBITS, 0 }, |
2640 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2641 | }; |
2642 | ||
b35d266b | 2643 | static const struct bfd_elf_special_section special_sections_n[] = |
7f4d3958 | 2644 | { |
0112cd26 NC |
2645 | { STRING_COMMA_LEN (".note.GNU-stack"), 0, SHT_PROGBITS, 0 }, |
2646 | { STRING_COMMA_LEN (".note"), -1, SHT_NOTE, 0 }, | |
2647 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2648 | }; |
2649 | ||
b35d266b | 2650 | static const struct bfd_elf_special_section special_sections_p[] = |
7f4d3958 | 2651 | { |
0112cd26 NC |
2652 | { STRING_COMMA_LEN (".preinit_array"), 0, SHT_PREINIT_ARRAY, SHF_ALLOC + SHF_WRITE }, |
2653 | { STRING_COMMA_LEN (".plt"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR }, | |
2654 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2655 | }; |
2656 | ||
b35d266b | 2657 | static const struct bfd_elf_special_section special_sections_r[] = |
7f4d3958 | 2658 | { |
0112cd26 NC |
2659 | { STRING_COMMA_LEN (".rodata"), -2, SHT_PROGBITS, SHF_ALLOC }, |
2660 | { STRING_COMMA_LEN (".rodata1"), 0, SHT_PROGBITS, SHF_ALLOC }, | |
2661 | { STRING_COMMA_LEN (".rela"), -1, SHT_RELA, 0 }, | |
2662 | { STRING_COMMA_LEN (".rel"), -1, SHT_REL, 0 }, | |
2663 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2664 | }; |
2665 | ||
b35d266b | 2666 | static const struct bfd_elf_special_section special_sections_s[] = |
7f4d3958 | 2667 | { |
0112cd26 NC |
2668 | { STRING_COMMA_LEN (".shstrtab"), 0, SHT_STRTAB, 0 }, |
2669 | { STRING_COMMA_LEN (".strtab"), 0, SHT_STRTAB, 0 }, | |
2670 | { STRING_COMMA_LEN (".symtab"), 0, SHT_SYMTAB, 0 }, | |
60ff4dc4 HPN |
2671 | /* See struct bfd_elf_special_section declaration for the semantics of |
2672 | this special case where .prefix_length != strlen (.prefix). */ | |
2673 | { ".stabstr", 5, 3, SHT_STRTAB, 0 }, | |
0112cd26 | 2674 | { NULL, 0, 0, 0, 0 } |
2f89ff8d L |
2675 | }; |
2676 | ||
b35d266b | 2677 | static const struct bfd_elf_special_section special_sections_t[] = |
7f4d3958 | 2678 | { |
0112cd26 NC |
2679 | { STRING_COMMA_LEN (".text"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR }, |
2680 | { STRING_COMMA_LEN (".tbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS }, | |
2681 | { STRING_COMMA_LEN (".tdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS }, | |
2682 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2683 | }; |
2684 | ||
1b315056 CS |
2685 | static const struct bfd_elf_special_section special_sections_z[] = |
2686 | { | |
2687 | { STRING_COMMA_LEN (".zdebug_line"), 0, SHT_PROGBITS, 0 }, | |
2688 | { STRING_COMMA_LEN (".zdebug_info"), 0, SHT_PROGBITS, 0 }, | |
2689 | { STRING_COMMA_LEN (".zdebug_abbrev"), 0, SHT_PROGBITS, 0 }, | |
2690 | { STRING_COMMA_LEN (".zdebug_aranges"), 0, SHT_PROGBITS, 0 }, | |
2691 | { NULL, 0, 0, 0, 0 } | |
2692 | }; | |
2693 | ||
e4c93b56 | 2694 | static const struct bfd_elf_special_section * const special_sections[] = |
7f4d3958 | 2695 | { |
7f4d3958 | 2696 | special_sections_b, /* 'b' */ |
98ece1b3 | 2697 | special_sections_c, /* 'c' */ |
7f4d3958 L |
2698 | special_sections_d, /* 'd' */ |
2699 | NULL, /* 'e' */ | |
2700 | special_sections_f, /* 'f' */ | |
2701 | special_sections_g, /* 'g' */ | |
2702 | special_sections_h, /* 'h' */ | |
2703 | special_sections_i, /* 'i' */ | |
2704 | NULL, /* 'j' */ | |
2705 | NULL, /* 'k' */ | |
2706 | special_sections_l, /* 'l' */ | |
2707 | NULL, /* 'm' */ | |
2708 | special_sections_n, /* 'n' */ | |
2709 | NULL, /* 'o' */ | |
2710 | special_sections_p, /* 'p' */ | |
2711 | NULL, /* 'q' */ | |
2712 | special_sections_r, /* 'r' */ | |
2713 | special_sections_s, /* 's' */ | |
2714 | special_sections_t, /* 't' */ | |
1b315056 CS |
2715 | NULL, /* 'u' */ |
2716 | NULL, /* 'v' */ | |
2717 | NULL, /* 'w' */ | |
2718 | NULL, /* 'x' */ | |
2719 | NULL, /* 'y' */ | |
2720 | special_sections_z /* 'z' */ | |
7f4d3958 L |
2721 | }; |
2722 | ||
551b43fd AM |
2723 | const struct bfd_elf_special_section * |
2724 | _bfd_elf_get_special_section (const char *name, | |
2725 | const struct bfd_elf_special_section *spec, | |
2726 | unsigned int rela) | |
2f89ff8d L |
2727 | { |
2728 | int i; | |
7f4d3958 | 2729 | int len; |
7f4d3958 | 2730 | |
551b43fd | 2731 | len = strlen (name); |
7f4d3958 | 2732 | |
551b43fd | 2733 | for (i = 0; spec[i].prefix != NULL; i++) |
7dcb9820 AM |
2734 | { |
2735 | int suffix_len; | |
551b43fd | 2736 | int prefix_len = spec[i].prefix_length; |
7dcb9820 AM |
2737 | |
2738 | if (len < prefix_len) | |
2739 | continue; | |
551b43fd | 2740 | if (memcmp (name, spec[i].prefix, prefix_len) != 0) |
7dcb9820 AM |
2741 | continue; |
2742 | ||
551b43fd | 2743 | suffix_len = spec[i].suffix_length; |
7dcb9820 AM |
2744 | if (suffix_len <= 0) |
2745 | { | |
2746 | if (name[prefix_len] != 0) | |
2747 | { | |
2748 | if (suffix_len == 0) | |
2749 | continue; | |
2750 | if (name[prefix_len] != '.' | |
2751 | && (suffix_len == -2 | |
551b43fd | 2752 | || (rela && spec[i].type == SHT_REL))) |
7dcb9820 AM |
2753 | continue; |
2754 | } | |
2755 | } | |
2756 | else | |
2757 | { | |
2758 | if (len < prefix_len + suffix_len) | |
2759 | continue; | |
2760 | if (memcmp (name + len - suffix_len, | |
551b43fd | 2761 | spec[i].prefix + prefix_len, |
7dcb9820 AM |
2762 | suffix_len) != 0) |
2763 | continue; | |
2764 | } | |
551b43fd | 2765 | return &spec[i]; |
7dcb9820 | 2766 | } |
2f89ff8d L |
2767 | |
2768 | return NULL; | |
2769 | } | |
2770 | ||
7dcb9820 | 2771 | const struct bfd_elf_special_section * |
29ef7005 | 2772 | _bfd_elf_get_sec_type_attr (bfd *abfd, asection *sec) |
2f89ff8d | 2773 | { |
551b43fd AM |
2774 | int i; |
2775 | const struct bfd_elf_special_section *spec; | |
29ef7005 | 2776 | const struct elf_backend_data *bed; |
2f89ff8d L |
2777 | |
2778 | /* See if this is one of the special sections. */ | |
551b43fd AM |
2779 | if (sec->name == NULL) |
2780 | return NULL; | |
2f89ff8d | 2781 | |
29ef7005 L |
2782 | bed = get_elf_backend_data (abfd); |
2783 | spec = bed->special_sections; | |
2784 | if (spec) | |
2785 | { | |
2786 | spec = _bfd_elf_get_special_section (sec->name, | |
2787 | bed->special_sections, | |
2788 | sec->use_rela_p); | |
2789 | if (spec != NULL) | |
2790 | return spec; | |
2791 | } | |
2792 | ||
551b43fd AM |
2793 | if (sec->name[0] != '.') |
2794 | return NULL; | |
2f89ff8d | 2795 | |
551b43fd | 2796 | i = sec->name[1] - 'b'; |
1b315056 | 2797 | if (i < 0 || i > 'z' - 'b') |
551b43fd AM |
2798 | return NULL; |
2799 | ||
2800 | spec = special_sections[i]; | |
2f89ff8d | 2801 | |
551b43fd AM |
2802 | if (spec == NULL) |
2803 | return NULL; | |
2804 | ||
2805 | return _bfd_elf_get_special_section (sec->name, spec, sec->use_rela_p); | |
2f89ff8d L |
2806 | } |
2807 | ||
b34976b6 | 2808 | bfd_boolean |
217aa764 | 2809 | _bfd_elf_new_section_hook (bfd *abfd, asection *sec) |
252b5132 RH |
2810 | { |
2811 | struct bfd_elf_section_data *sdata; | |
551b43fd | 2812 | const struct elf_backend_data *bed; |
7dcb9820 | 2813 | const struct bfd_elf_special_section *ssect; |
252b5132 | 2814 | |
f0abc2a1 AM |
2815 | sdata = (struct bfd_elf_section_data *) sec->used_by_bfd; |
2816 | if (sdata == NULL) | |
2817 | { | |
a50b1753 NC |
2818 | sdata = (struct bfd_elf_section_data *) bfd_zalloc (abfd, |
2819 | sizeof (*sdata)); | |
f0abc2a1 AM |
2820 | if (sdata == NULL) |
2821 | return FALSE; | |
217aa764 | 2822 | sec->used_by_bfd = sdata; |
f0abc2a1 | 2823 | } |
bf572ba0 | 2824 | |
551b43fd AM |
2825 | /* Indicate whether or not this section should use RELA relocations. */ |
2826 | bed = get_elf_backend_data (abfd); | |
2827 | sec->use_rela_p = bed->default_use_rela_p; | |
2828 | ||
e843e0f8 L |
2829 | /* When we read a file, we don't need to set ELF section type and |
2830 | flags. They will be overridden in _bfd_elf_make_section_from_shdr | |
2831 | anyway. We will set ELF section type and flags for all linker | |
2832 | created sections. If user specifies BFD section flags, we will | |
2833 | set ELF section type and flags based on BFD section flags in | |
02ecc8e9 L |
2834 | elf_fake_sections. Special handling for .init_array/.fini_array |
2835 | output sections since they may contain .ctors/.dtors input | |
2836 | sections. We don't want _bfd_elf_init_private_section_data to | |
2837 | copy ELF section type from .ctors/.dtors input sections. */ | |
2838 | if (abfd->direction != read_direction | |
3496cb2a | 2839 | || (sec->flags & SEC_LINKER_CREATED) != 0) |
2f89ff8d | 2840 | { |
551b43fd | 2841 | ssect = (*bed->get_sec_type_attr) (abfd, sec); |
02ecc8e9 L |
2842 | if (ssect != NULL |
2843 | && (!sec->flags | |
2844 | || (sec->flags & SEC_LINKER_CREATED) != 0 | |
2845 | || ssect->type == SHT_INIT_ARRAY | |
2846 | || ssect->type == SHT_FINI_ARRAY)) | |
a31501e9 L |
2847 | { |
2848 | elf_section_type (sec) = ssect->type; | |
2849 | elf_section_flags (sec) = ssect->attr; | |
2850 | } | |
2f89ff8d L |
2851 | } |
2852 | ||
f592407e | 2853 | return _bfd_generic_new_section_hook (abfd, sec); |
252b5132 RH |
2854 | } |
2855 | ||
2856 | /* Create a new bfd section from an ELF program header. | |
2857 | ||
2858 | Since program segments have no names, we generate a synthetic name | |
2859 | of the form segment<NUM>, where NUM is generally the index in the | |
2860 | program header table. For segments that are split (see below) we | |
2861 | generate the names segment<NUM>a and segment<NUM>b. | |
2862 | ||
2863 | Note that some program segments may have a file size that is different than | |
2864 | (less than) the memory size. All this means is that at execution the | |
2865 | system must allocate the amount of memory specified by the memory size, | |
2866 | but only initialize it with the first "file size" bytes read from the | |
2867 | file. This would occur for example, with program segments consisting | |
2868 | of combined data+bss. | |
2869 | ||
2870 | To handle the above situation, this routine generates TWO bfd sections | |
2871 | for the single program segment. The first has the length specified by | |
2872 | the file size of the segment, and the second has the length specified | |
2873 | by the difference between the two sizes. In effect, the segment is split | |
d5191d0c | 2874 | into its initialized and uninitialized parts. |
252b5132 RH |
2875 | |
2876 | */ | |
2877 | ||
b34976b6 | 2878 | bfd_boolean |
217aa764 AM |
2879 | _bfd_elf_make_section_from_phdr (bfd *abfd, |
2880 | Elf_Internal_Phdr *hdr, | |
91d6fa6a | 2881 | int hdr_index, |
a50b1753 | 2882 | const char *type_name) |
252b5132 RH |
2883 | { |
2884 | asection *newsect; | |
2885 | char *name; | |
2886 | char namebuf[64]; | |
d4c88bbb | 2887 | size_t len; |
252b5132 RH |
2888 | int split; |
2889 | ||
2890 | split = ((hdr->p_memsz > 0) | |
2891 | && (hdr->p_filesz > 0) | |
2892 | && (hdr->p_memsz > hdr->p_filesz)); | |
d5191d0c AM |
2893 | |
2894 | if (hdr->p_filesz > 0) | |
252b5132 | 2895 | { |
91d6fa6a | 2896 | sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "a" : ""); |
d5191d0c | 2897 | len = strlen (namebuf) + 1; |
a50b1753 | 2898 | name = (char *) bfd_alloc (abfd, len); |
d5191d0c AM |
2899 | if (!name) |
2900 | return FALSE; | |
2901 | memcpy (name, namebuf, len); | |
2902 | newsect = bfd_make_section (abfd, name); | |
2903 | if (newsect == NULL) | |
2904 | return FALSE; | |
2905 | newsect->vma = hdr->p_vaddr; | |
2906 | newsect->lma = hdr->p_paddr; | |
2907 | newsect->size = hdr->p_filesz; | |
2908 | newsect->filepos = hdr->p_offset; | |
2909 | newsect->flags |= SEC_HAS_CONTENTS; | |
2910 | newsect->alignment_power = bfd_log2 (hdr->p_align); | |
2911 | if (hdr->p_type == PT_LOAD) | |
252b5132 | 2912 | { |
d5191d0c AM |
2913 | newsect->flags |= SEC_ALLOC; |
2914 | newsect->flags |= SEC_LOAD; | |
2915 | if (hdr->p_flags & PF_X) | |
2916 | { | |
2917 | /* FIXME: all we known is that it has execute PERMISSION, | |
2918 | may be data. */ | |
2919 | newsect->flags |= SEC_CODE; | |
2920 | } | |
2921 | } | |
2922 | if (!(hdr->p_flags & PF_W)) | |
2923 | { | |
2924 | newsect->flags |= SEC_READONLY; | |
252b5132 | 2925 | } |
252b5132 RH |
2926 | } |
2927 | ||
d5191d0c | 2928 | if (hdr->p_memsz > hdr->p_filesz) |
252b5132 | 2929 | { |
d5191d0c AM |
2930 | bfd_vma align; |
2931 | ||
91d6fa6a | 2932 | sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "b" : ""); |
d4c88bbb | 2933 | len = strlen (namebuf) + 1; |
a50b1753 | 2934 | name = (char *) bfd_alloc (abfd, len); |
252b5132 | 2935 | if (!name) |
b34976b6 | 2936 | return FALSE; |
d4c88bbb | 2937 | memcpy (name, namebuf, len); |
252b5132 RH |
2938 | newsect = bfd_make_section (abfd, name); |
2939 | if (newsect == NULL) | |
b34976b6 | 2940 | return FALSE; |
252b5132 RH |
2941 | newsect->vma = hdr->p_vaddr + hdr->p_filesz; |
2942 | newsect->lma = hdr->p_paddr + hdr->p_filesz; | |
eea6121a | 2943 | newsect->size = hdr->p_memsz - hdr->p_filesz; |
d5191d0c AM |
2944 | newsect->filepos = hdr->p_offset + hdr->p_filesz; |
2945 | align = newsect->vma & -newsect->vma; | |
2946 | if (align == 0 || align > hdr->p_align) | |
2947 | align = hdr->p_align; | |
2948 | newsect->alignment_power = bfd_log2 (align); | |
252b5132 RH |
2949 | if (hdr->p_type == PT_LOAD) |
2950 | { | |
d5191d0c AM |
2951 | /* Hack for gdb. Segments that have not been modified do |
2952 | not have their contents written to a core file, on the | |
2953 | assumption that a debugger can find the contents in the | |
2954 | executable. We flag this case by setting the fake | |
2955 | section size to zero. Note that "real" bss sections will | |
2956 | always have their contents dumped to the core file. */ | |
2957 | if (bfd_get_format (abfd) == bfd_core) | |
2958 | newsect->size = 0; | |
252b5132 RH |
2959 | newsect->flags |= SEC_ALLOC; |
2960 | if (hdr->p_flags & PF_X) | |
2961 | newsect->flags |= SEC_CODE; | |
2962 | } | |
2963 | if (!(hdr->p_flags & PF_W)) | |
2964 | newsect->flags |= SEC_READONLY; | |
2965 | } | |
2966 | ||
b34976b6 | 2967 | return TRUE; |
252b5132 RH |
2968 | } |
2969 | ||
b34976b6 | 2970 | bfd_boolean |
91d6fa6a | 2971 | bfd_section_from_phdr (bfd *abfd, Elf_Internal_Phdr *hdr, int hdr_index) |
20cfcaae | 2972 | { |
9c5bfbb7 | 2973 | const struct elf_backend_data *bed; |
20cfcaae NC |
2974 | |
2975 | switch (hdr->p_type) | |
2976 | { | |
2977 | case PT_NULL: | |
91d6fa6a | 2978 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "null"); |
20cfcaae NC |
2979 | |
2980 | case PT_LOAD: | |
91d6fa6a | 2981 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "load"); |
20cfcaae NC |
2982 | |
2983 | case PT_DYNAMIC: | |
91d6fa6a | 2984 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "dynamic"); |
20cfcaae NC |
2985 | |
2986 | case PT_INTERP: | |
91d6fa6a | 2987 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "interp"); |
20cfcaae NC |
2988 | |
2989 | case PT_NOTE: | |
91d6fa6a | 2990 | if (! _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "note")) |
b34976b6 | 2991 | return FALSE; |
718175fa | 2992 | if (! elf_read_notes (abfd, hdr->p_offset, hdr->p_filesz)) |
b34976b6 AM |
2993 | return FALSE; |
2994 | return TRUE; | |
20cfcaae NC |
2995 | |
2996 | case PT_SHLIB: | |
91d6fa6a | 2997 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "shlib"); |
20cfcaae NC |
2998 | |
2999 | case PT_PHDR: | |
91d6fa6a | 3000 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "phdr"); |
20cfcaae | 3001 | |
811072d8 | 3002 | case PT_GNU_EH_FRAME: |
91d6fa6a | 3003 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, |
811072d8 RM |
3004 | "eh_frame_hdr"); |
3005 | ||
2b05f1b7 | 3006 | case PT_GNU_STACK: |
91d6fa6a | 3007 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "stack"); |
9ee5e499 | 3008 | |
8c37241b | 3009 | case PT_GNU_RELRO: |
91d6fa6a | 3010 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "relro"); |
8c37241b | 3011 | |
20cfcaae | 3012 | default: |
8c1acd09 | 3013 | /* Check for any processor-specific program segment types. */ |
20cfcaae | 3014 | bed = get_elf_backend_data (abfd); |
91d6fa6a | 3015 | return bed->elf_backend_section_from_phdr (abfd, hdr, hdr_index, "proc"); |
20cfcaae NC |
3016 | } |
3017 | } | |
3018 | ||
d4730f92 BS |
3019 | /* Return the REL_HDR for SEC, assuming there is only a single one, either |
3020 | REL or RELA. */ | |
3021 | ||
3022 | Elf_Internal_Shdr * | |
3023 | _bfd_elf_single_rel_hdr (asection *sec) | |
3024 | { | |
3025 | if (elf_section_data (sec)->rel.hdr) | |
3026 | { | |
3027 | BFD_ASSERT (elf_section_data (sec)->rela.hdr == NULL); | |
3028 | return elf_section_data (sec)->rel.hdr; | |
3029 | } | |
3030 | else | |
3031 | return elf_section_data (sec)->rela.hdr; | |
3032 | } | |
3033 | ||
3e19fb8f L |
3034 | static bfd_boolean |
3035 | _bfd_elf_set_reloc_sh_name (bfd *abfd, | |
3036 | Elf_Internal_Shdr *rel_hdr, | |
3037 | const char *sec_name, | |
3038 | bfd_boolean use_rela_p) | |
3039 | { | |
3040 | char *name = (char *) bfd_alloc (abfd, | |
3041 | sizeof ".rela" + strlen (sec_name)); | |
3042 | if (name == NULL) | |
3043 | return FALSE; | |
3044 | ||
3045 | sprintf (name, "%s%s", use_rela_p ? ".rela" : ".rel", sec_name); | |
3046 | rel_hdr->sh_name = | |
3047 | (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), name, | |
3048 | FALSE); | |
3049 | if (rel_hdr->sh_name == (unsigned int) -1) | |
3050 | return FALSE; | |
3051 | ||
3052 | return TRUE; | |
3053 | } | |
3054 | ||
d4730f92 BS |
3055 | /* Allocate and initialize a section-header for a new reloc section, |
3056 | containing relocations against ASECT. It is stored in RELDATA. If | |
3057 | USE_RELA_P is TRUE, we use RELA relocations; otherwise, we use REL | |
3058 | relocations. */ | |
23bc299b | 3059 | |
5d13b3b3 | 3060 | static bfd_boolean |
217aa764 | 3061 | _bfd_elf_init_reloc_shdr (bfd *abfd, |
d4730f92 | 3062 | struct bfd_elf_section_reloc_data *reldata, |
f6fe1ccd | 3063 | const char *sec_name, |
3e19fb8f L |
3064 | bfd_boolean use_rela_p, |
3065 | bfd_boolean delay_st_name_p) | |
23bc299b | 3066 | { |
d4730f92 | 3067 | Elf_Internal_Shdr *rel_hdr; |
9c5bfbb7 | 3068 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
d4730f92 | 3069 | |
d4730f92 | 3070 | BFD_ASSERT (reldata->hdr == NULL); |
ef53be89 | 3071 | rel_hdr = bfd_zalloc (abfd, sizeof (*rel_hdr)); |
d4730f92 | 3072 | reldata->hdr = rel_hdr; |
23bc299b | 3073 | |
3e19fb8f L |
3074 | if (delay_st_name_p) |
3075 | rel_hdr->sh_name = (unsigned int) -1; | |
3076 | else if (!_bfd_elf_set_reloc_sh_name (abfd, rel_hdr, sec_name, | |
3077 | use_rela_p)) | |
b34976b6 | 3078 | return FALSE; |
23bc299b MM |
3079 | rel_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL; |
3080 | rel_hdr->sh_entsize = (use_rela_p | |
3081 | ? bed->s->sizeof_rela | |
3082 | : bed->s->sizeof_rel); | |
72de5009 | 3083 | rel_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align; |
23bc299b MM |
3084 | rel_hdr->sh_flags = 0; |
3085 | rel_hdr->sh_addr = 0; | |
3086 | rel_hdr->sh_size = 0; | |
3087 | rel_hdr->sh_offset = 0; | |
3088 | ||
b34976b6 | 3089 | return TRUE; |
23bc299b MM |
3090 | } |
3091 | ||
94be91de JB |
3092 | /* Return the default section type based on the passed in section flags. */ |
3093 | ||
3094 | int | |
3095 | bfd_elf_get_default_section_type (flagword flags) | |
3096 | { | |
3097 | if ((flags & SEC_ALLOC) != 0 | |
2e76e85a | 3098 | && (flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0) |
94be91de JB |
3099 | return SHT_NOBITS; |
3100 | return SHT_PROGBITS; | |
3101 | } | |
3102 | ||
d4730f92 BS |
3103 | struct fake_section_arg |
3104 | { | |
3105 | struct bfd_link_info *link_info; | |
3106 | bfd_boolean failed; | |
3107 | }; | |
3108 | ||
252b5132 RH |
3109 | /* Set up an ELF internal section header for a section. */ |
3110 | ||
252b5132 | 3111 | static void |
d4730f92 | 3112 | elf_fake_sections (bfd *abfd, asection *asect, void *fsarg) |
252b5132 | 3113 | { |
d4730f92 | 3114 | struct fake_section_arg *arg = (struct fake_section_arg *)fsarg; |
9c5bfbb7 | 3115 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
d4730f92 | 3116 | struct bfd_elf_section_data *esd = elf_section_data (asect); |
252b5132 | 3117 | Elf_Internal_Shdr *this_hdr; |
0414f35b | 3118 | unsigned int sh_type; |
0ce398f1 | 3119 | const char *name = asect->name; |
3e19fb8f | 3120 | bfd_boolean delay_st_name_p = FALSE; |
252b5132 | 3121 | |
d4730f92 | 3122 | if (arg->failed) |
252b5132 RH |
3123 | { |
3124 | /* We already failed; just get out of the bfd_map_over_sections | |
08a40648 | 3125 | loop. */ |
252b5132 RH |
3126 | return; |
3127 | } | |
3128 | ||
d4730f92 | 3129 | this_hdr = &esd->this_hdr; |
252b5132 | 3130 | |
f6fe1ccd | 3131 | if (arg->link_info) |
0ce398f1 | 3132 | { |
f6fe1ccd L |
3133 | /* ld: compress DWARF debug sections with names: .debug_*. */ |
3134 | if ((arg->link_info->compress_debug & COMPRESS_DEBUG) | |
3135 | && (asect->flags & SEC_DEBUGGING) | |
3136 | && name[1] == 'd' | |
3137 | && name[6] == '_') | |
3138 | { | |
3139 | /* Set SEC_ELF_COMPRESS to indicate this section should be | |
3140 | compressed. */ | |
3141 | asect->flags |= SEC_ELF_COMPRESS; | |
0ce398f1 | 3142 | |
dd905818 | 3143 | /* If this section will be compressed, delay adding section |
3e19fb8f L |
3144 | name to section name section after it is compressed in |
3145 | _bfd_elf_assign_file_positions_for_non_load. */ | |
3146 | delay_st_name_p = TRUE; | |
f6fe1ccd L |
3147 | } |
3148 | } | |
3149 | else if ((asect->flags & SEC_ELF_RENAME)) | |
3150 | { | |
3151 | /* objcopy: rename output DWARF debug section. */ | |
3152 | if ((abfd->flags & (BFD_DECOMPRESS | BFD_COMPRESS_GABI))) | |
3153 | { | |
3154 | /* When we decompress or compress with SHF_COMPRESSED, | |
3155 | convert section name from .zdebug_* to .debug_* if | |
3156 | needed. */ | |
3157 | if (name[1] == 'z') | |
3158 | { | |
3159 | char *new_name = convert_zdebug_to_debug (abfd, name); | |
3160 | if (new_name == NULL) | |
3161 | { | |
3162 | arg->failed = TRUE; | |
3163 | return; | |
3164 | } | |
3165 | name = new_name; | |
3166 | } | |
3167 | } | |
3168 | else if (asect->compress_status == COMPRESS_SECTION_DONE) | |
0ce398f1 | 3169 | { |
f6fe1ccd L |
3170 | /* PR binutils/18087: Compression does not always make a |
3171 | section smaller. So only rename the section when | |
3172 | compression has actually taken place. If input section | |
3173 | name is .zdebug_*, we should never compress it again. */ | |
3174 | char *new_name = convert_debug_to_zdebug (abfd, name); | |
0ce398f1 L |
3175 | if (new_name == NULL) |
3176 | { | |
3177 | arg->failed = TRUE; | |
3178 | return; | |
3179 | } | |
f6fe1ccd L |
3180 | BFD_ASSERT (name[1] != 'z'); |
3181 | name = new_name; | |
0ce398f1 L |
3182 | } |
3183 | } | |
3184 | ||
3e19fb8f L |
3185 | if (delay_st_name_p) |
3186 | this_hdr->sh_name = (unsigned int) -1; | |
3187 | else | |
252b5132 | 3188 | { |
3e19fb8f L |
3189 | this_hdr->sh_name |
3190 | = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), | |
3191 | name, FALSE); | |
3192 | if (this_hdr->sh_name == (unsigned int) -1) | |
3193 | { | |
3194 | arg->failed = TRUE; | |
3195 | return; | |
3196 | } | |
252b5132 RH |
3197 | } |
3198 | ||
a4d8e49b | 3199 | /* Don't clear sh_flags. Assembler may set additional bits. */ |
252b5132 RH |
3200 | |
3201 | if ((asect->flags & SEC_ALLOC) != 0 | |
3202 | || asect->user_set_vma) | |
3203 | this_hdr->sh_addr = asect->vma; | |
3204 | else | |
3205 | this_hdr->sh_addr = 0; | |
3206 | ||
3207 | this_hdr->sh_offset = 0; | |
eea6121a | 3208 | this_hdr->sh_size = asect->size; |
252b5132 | 3209 | this_hdr->sh_link = 0; |
c86934ce NC |
3210 | /* PR 17512: file: 0eb809fe, 8b0535ee. */ |
3211 | if (asect->alignment_power >= (sizeof (bfd_vma) * 8) - 1) | |
3212 | { | |
4eca0228 | 3213 | _bfd_error_handler |
695344c0 | 3214 | /* xgettext:c-format */ |
c86934ce NC |
3215 | (_("%B: error: Alignment power %d of section `%A' is too big"), |
3216 | abfd, asect, asect->alignment_power); | |
3217 | arg->failed = TRUE; | |
3218 | return; | |
3219 | } | |
72de5009 | 3220 | this_hdr->sh_addralign = (bfd_vma) 1 << asect->alignment_power; |
252b5132 RH |
3221 | /* The sh_entsize and sh_info fields may have been set already by |
3222 | copy_private_section_data. */ | |
3223 | ||
3224 | this_hdr->bfd_section = asect; | |
3225 | this_hdr->contents = NULL; | |
3226 | ||
3cddba1e L |
3227 | /* If the section type is unspecified, we set it based on |
3228 | asect->flags. */ | |
98ece1b3 AM |
3229 | if ((asect->flags & SEC_GROUP) != 0) |
3230 | sh_type = SHT_GROUP; | |
98ece1b3 | 3231 | else |
94be91de | 3232 | sh_type = bfd_elf_get_default_section_type (asect->flags); |
98ece1b3 | 3233 | |
3cddba1e | 3234 | if (this_hdr->sh_type == SHT_NULL) |
98ece1b3 AM |
3235 | this_hdr->sh_type = sh_type; |
3236 | else if (this_hdr->sh_type == SHT_NOBITS | |
3237 | && sh_type == SHT_PROGBITS | |
3238 | && (asect->flags & SEC_ALLOC) != 0) | |
3cddba1e | 3239 | { |
98ece1b3 AM |
3240 | /* Warn if we are changing a NOBITS section to PROGBITS, but |
3241 | allow the link to proceed. This can happen when users link | |
3242 | non-bss input sections to bss output sections, or emit data | |
3243 | to a bss output section via a linker script. */ | |
4eca0228 | 3244 | _bfd_error_handler |
58f0869b | 3245 | (_("warning: section `%A' type changed to PROGBITS"), asect); |
98ece1b3 | 3246 | this_hdr->sh_type = sh_type; |
3cddba1e L |
3247 | } |
3248 | ||
2f89ff8d | 3249 | switch (this_hdr->sh_type) |
252b5132 | 3250 | { |
2f89ff8d | 3251 | default: |
2f89ff8d L |
3252 | break; |
3253 | ||
3254 | case SHT_STRTAB: | |
2f89ff8d L |
3255 | case SHT_NOTE: |
3256 | case SHT_NOBITS: | |
3257 | case SHT_PROGBITS: | |
3258 | break; | |
606851fb AM |
3259 | |
3260 | case SHT_INIT_ARRAY: | |
3261 | case SHT_FINI_ARRAY: | |
3262 | case SHT_PREINIT_ARRAY: | |
3263 | this_hdr->sh_entsize = bed->s->arch_size / 8; | |
3264 | break; | |
2f89ff8d L |
3265 | |
3266 | case SHT_HASH: | |
c7ac6ff8 | 3267 | this_hdr->sh_entsize = bed->s->sizeof_hash_entry; |
2f89ff8d | 3268 | break; |
5de3bf90 | 3269 | |
2f89ff8d | 3270 | case SHT_DYNSYM: |
252b5132 | 3271 | this_hdr->sh_entsize = bed->s->sizeof_sym; |
2f89ff8d L |
3272 | break; |
3273 | ||
3274 | case SHT_DYNAMIC: | |
252b5132 | 3275 | this_hdr->sh_entsize = bed->s->sizeof_dyn; |
2f89ff8d L |
3276 | break; |
3277 | ||
3278 | case SHT_RELA: | |
3279 | if (get_elf_backend_data (abfd)->may_use_rela_p) | |
3280 | this_hdr->sh_entsize = bed->s->sizeof_rela; | |
3281 | break; | |
3282 | ||
3283 | case SHT_REL: | |
3284 | if (get_elf_backend_data (abfd)->may_use_rel_p) | |
3285 | this_hdr->sh_entsize = bed->s->sizeof_rel; | |
3286 | break; | |
3287 | ||
3288 | case SHT_GNU_versym: | |
252b5132 | 3289 | this_hdr->sh_entsize = sizeof (Elf_External_Versym); |
2f89ff8d L |
3290 | break; |
3291 | ||
3292 | case SHT_GNU_verdef: | |
252b5132 RH |
3293 | this_hdr->sh_entsize = 0; |
3294 | /* objcopy or strip will copy over sh_info, but may not set | |
08a40648 AM |
3295 | cverdefs. The linker will set cverdefs, but sh_info will be |
3296 | zero. */ | |
252b5132 RH |
3297 | if (this_hdr->sh_info == 0) |
3298 | this_hdr->sh_info = elf_tdata (abfd)->cverdefs; | |
3299 | else | |
3300 | BFD_ASSERT (elf_tdata (abfd)->cverdefs == 0 | |
3301 | || this_hdr->sh_info == elf_tdata (abfd)->cverdefs); | |
2f89ff8d L |
3302 | break; |
3303 | ||
3304 | case SHT_GNU_verneed: | |
252b5132 RH |
3305 | this_hdr->sh_entsize = 0; |
3306 | /* objcopy or strip will copy over sh_info, but may not set | |
08a40648 AM |
3307 | cverrefs. The linker will set cverrefs, but sh_info will be |
3308 | zero. */ | |
252b5132 RH |
3309 | if (this_hdr->sh_info == 0) |
3310 | this_hdr->sh_info = elf_tdata (abfd)->cverrefs; | |
3311 | else | |
3312 | BFD_ASSERT (elf_tdata (abfd)->cverrefs == 0 | |
3313 | || this_hdr->sh_info == elf_tdata (abfd)->cverrefs); | |
2f89ff8d L |
3314 | break; |
3315 | ||
3316 | case SHT_GROUP: | |
1783205a | 3317 | this_hdr->sh_entsize = GRP_ENTRY_SIZE; |
2f89ff8d | 3318 | break; |
fdc90cb4 JJ |
3319 | |
3320 | case SHT_GNU_HASH: | |
3321 | this_hdr->sh_entsize = bed->s->arch_size == 64 ? 0 : 4; | |
3322 | break; | |
dbb410c3 | 3323 | } |
252b5132 RH |
3324 | |
3325 | if ((asect->flags & SEC_ALLOC) != 0) | |
3326 | this_hdr->sh_flags |= SHF_ALLOC; | |
3327 | if ((asect->flags & SEC_READONLY) == 0) | |
3328 | this_hdr->sh_flags |= SHF_WRITE; | |
3329 | if ((asect->flags & SEC_CODE) != 0) | |
3330 | this_hdr->sh_flags |= SHF_EXECINSTR; | |
f5fa8ca2 JJ |
3331 | if ((asect->flags & SEC_MERGE) != 0) |
3332 | { | |
3333 | this_hdr->sh_flags |= SHF_MERGE; | |
3334 | this_hdr->sh_entsize = asect->entsize; | |
f5fa8ca2 | 3335 | } |
84865015 NC |
3336 | if ((asect->flags & SEC_STRINGS) != 0) |
3337 | this_hdr->sh_flags |= SHF_STRINGS; | |
1126897b | 3338 | if ((asect->flags & SEC_GROUP) == 0 && elf_group_name (asect) != NULL) |
dbb410c3 | 3339 | this_hdr->sh_flags |= SHF_GROUP; |
13ae64f3 | 3340 | if ((asect->flags & SEC_THREAD_LOCAL) != 0) |
704afa60 JJ |
3341 | { |
3342 | this_hdr->sh_flags |= SHF_TLS; | |
3a800eb9 AM |
3343 | if (asect->size == 0 |
3344 | && (asect->flags & SEC_HAS_CONTENTS) == 0) | |
704afa60 | 3345 | { |
3a800eb9 | 3346 | struct bfd_link_order *o = asect->map_tail.link_order; |
b34976b6 | 3347 | |
704afa60 | 3348 | this_hdr->sh_size = 0; |
3a800eb9 AM |
3349 | if (o != NULL) |
3350 | { | |
704afa60 | 3351 | this_hdr->sh_size = o->offset + o->size; |
3a800eb9 AM |
3352 | if (this_hdr->sh_size != 0) |
3353 | this_hdr->sh_type = SHT_NOBITS; | |
3354 | } | |
704afa60 JJ |
3355 | } |
3356 | } | |
18ae9cc1 L |
3357 | if ((asect->flags & (SEC_GROUP | SEC_EXCLUDE)) == SEC_EXCLUDE) |
3358 | this_hdr->sh_flags |= SHF_EXCLUDE; | |
252b5132 | 3359 | |
d4730f92 BS |
3360 | /* If the section has relocs, set up a section header for the |
3361 | SHT_REL[A] section. If two relocation sections are required for | |
3362 | this section, it is up to the processor-specific back-end to | |
3363 | create the other. */ | |
3364 | if ((asect->flags & SEC_RELOC) != 0) | |
3365 | { | |
3366 | /* When doing a relocatable link, create both REL and RELA sections if | |
3367 | needed. */ | |
3368 | if (arg->link_info | |
3369 | /* Do the normal setup if we wouldn't create any sections here. */ | |
3370 | && esd->rel.count + esd->rela.count > 0 | |
0e1862bb L |
3371 | && (bfd_link_relocatable (arg->link_info) |
3372 | || arg->link_info->emitrelocations)) | |
d4730f92 BS |
3373 | { |
3374 | if (esd->rel.count && esd->rel.hdr == NULL | |
3e19fb8f L |
3375 | && !_bfd_elf_init_reloc_shdr (abfd, &esd->rel, name, FALSE, |
3376 | delay_st_name_p)) | |
d4730f92 BS |
3377 | { |
3378 | arg->failed = TRUE; | |
3379 | return; | |
3380 | } | |
3381 | if (esd->rela.count && esd->rela.hdr == NULL | |
3e19fb8f L |
3382 | && !_bfd_elf_init_reloc_shdr (abfd, &esd->rela, name, TRUE, |
3383 | delay_st_name_p)) | |
d4730f92 BS |
3384 | { |
3385 | arg->failed = TRUE; | |
3386 | return; | |
3387 | } | |
3388 | } | |
3389 | else if (!_bfd_elf_init_reloc_shdr (abfd, | |
3390 | (asect->use_rela_p | |
3391 | ? &esd->rela : &esd->rel), | |
f6fe1ccd | 3392 | name, |
3e19fb8f L |
3393 | asect->use_rela_p, |
3394 | delay_st_name_p)) | |
d4730f92 BS |
3395 | arg->failed = TRUE; |
3396 | } | |
3397 | ||
252b5132 | 3398 | /* Check for processor-specific section types. */ |
0414f35b | 3399 | sh_type = this_hdr->sh_type; |
e1fddb6b AO |
3400 | if (bed->elf_backend_fake_sections |
3401 | && !(*bed->elf_backend_fake_sections) (abfd, this_hdr, asect)) | |
d4730f92 | 3402 | arg->failed = TRUE; |
252b5132 | 3403 | |
42bb2e33 | 3404 | if (sh_type == SHT_NOBITS && asect->size != 0) |
0414f35b AM |
3405 | { |
3406 | /* Don't change the header type from NOBITS if we are being | |
42bb2e33 | 3407 | called for objcopy --only-keep-debug. */ |
0414f35b AM |
3408 | this_hdr->sh_type = sh_type; |
3409 | } | |
252b5132 RH |
3410 | } |
3411 | ||
bcacc0f5 AM |
3412 | /* Fill in the contents of a SHT_GROUP section. Called from |
3413 | _bfd_elf_compute_section_file_positions for gas, objcopy, and | |
3414 | when ELF targets use the generic linker, ld. Called for ld -r | |
3415 | from bfd_elf_final_link. */ | |
dbb410c3 | 3416 | |
1126897b | 3417 | void |
217aa764 | 3418 | bfd_elf_set_group_contents (bfd *abfd, asection *sec, void *failedptrarg) |
dbb410c3 | 3419 | { |
a50b1753 | 3420 | bfd_boolean *failedptr = (bfd_boolean *) failedptrarg; |
9dce4196 | 3421 | asection *elt, *first; |
dbb410c3 | 3422 | unsigned char *loc; |
b34976b6 | 3423 | bfd_boolean gas; |
dbb410c3 | 3424 | |
7e4111ad L |
3425 | /* Ignore linker created group section. See elfNN_ia64_object_p in |
3426 | elfxx-ia64.c. */ | |
3427 | if (((sec->flags & (SEC_GROUP | SEC_LINKER_CREATED)) != SEC_GROUP) | |
dbb410c3 AM |
3428 | || *failedptr) |
3429 | return; | |
3430 | ||
bcacc0f5 AM |
3431 | if (elf_section_data (sec)->this_hdr.sh_info == 0) |
3432 | { | |
3433 | unsigned long symindx = 0; | |
3434 | ||
3435 | /* elf_group_id will have been set up by objcopy and the | |
3436 | generic linker. */ | |
3437 | if (elf_group_id (sec) != NULL) | |
3438 | symindx = elf_group_id (sec)->udata.i; | |
1126897b | 3439 | |
bcacc0f5 AM |
3440 | if (symindx == 0) |
3441 | { | |
3442 | /* If called from the assembler, swap_out_syms will have set up | |
3443 | elf_section_syms. */ | |
3444 | BFD_ASSERT (elf_section_syms (abfd) != NULL); | |
3445 | symindx = elf_section_syms (abfd)[sec->index]->udata.i; | |
3446 | } | |
3447 | elf_section_data (sec)->this_hdr.sh_info = symindx; | |
3448 | } | |
3449 | else if (elf_section_data (sec)->this_hdr.sh_info == (unsigned int) -2) | |
1126897b | 3450 | { |
bcacc0f5 AM |
3451 | /* The ELF backend linker sets sh_info to -2 when the group |
3452 | signature symbol is global, and thus the index can't be | |
3453 | set until all local symbols are output. */ | |
53720c49 AM |
3454 | asection *igroup; |
3455 | struct bfd_elf_section_data *sec_data; | |
3456 | unsigned long symndx; | |
3457 | unsigned long extsymoff; | |
bcacc0f5 AM |
3458 | struct elf_link_hash_entry *h; |
3459 | ||
53720c49 AM |
3460 | /* The point of this little dance to the first SHF_GROUP section |
3461 | then back to the SHT_GROUP section is that this gets us to | |
3462 | the SHT_GROUP in the input object. */ | |
3463 | igroup = elf_sec_group (elf_next_in_group (sec)); | |
3464 | sec_data = elf_section_data (igroup); | |
3465 | symndx = sec_data->this_hdr.sh_info; | |
3466 | extsymoff = 0; | |
bcacc0f5 AM |
3467 | if (!elf_bad_symtab (igroup->owner)) |
3468 | { | |
3469 | Elf_Internal_Shdr *symtab_hdr; | |
3470 | ||
3471 | symtab_hdr = &elf_tdata (igroup->owner)->symtab_hdr; | |
3472 | extsymoff = symtab_hdr->sh_info; | |
3473 | } | |
3474 | h = elf_sym_hashes (igroup->owner)[symndx - extsymoff]; | |
3475 | while (h->root.type == bfd_link_hash_indirect | |
3476 | || h->root.type == bfd_link_hash_warning) | |
3477 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
3478 | ||
3479 | elf_section_data (sec)->this_hdr.sh_info = h->indx; | |
1126897b | 3480 | } |
dbb410c3 | 3481 | |
1126897b | 3482 | /* The contents won't be allocated for "ld -r" or objcopy. */ |
b34976b6 | 3483 | gas = TRUE; |
dbb410c3 AM |
3484 | if (sec->contents == NULL) |
3485 | { | |
b34976b6 | 3486 | gas = FALSE; |
a50b1753 | 3487 | sec->contents = (unsigned char *) bfd_alloc (abfd, sec->size); |
9dce4196 AM |
3488 | |
3489 | /* Arrange for the section to be written out. */ | |
3490 | elf_section_data (sec)->this_hdr.contents = sec->contents; | |
dbb410c3 AM |
3491 | if (sec->contents == NULL) |
3492 | { | |
b34976b6 | 3493 | *failedptr = TRUE; |
dbb410c3 AM |
3494 | return; |
3495 | } | |
3496 | } | |
3497 | ||
eea6121a | 3498 | loc = sec->contents + sec->size; |
dbb410c3 | 3499 | |
9dce4196 AM |
3500 | /* Get the pointer to the first section in the group that gas |
3501 | squirreled away here. objcopy arranges for this to be set to the | |
3502 | start of the input section group. */ | |
3503 | first = elt = elf_next_in_group (sec); | |
dbb410c3 AM |
3504 | |
3505 | /* First element is a flag word. Rest of section is elf section | |
3506 | indices for all the sections of the group. Write them backwards | |
3507 | just to keep the group in the same order as given in .section | |
3508 | directives, not that it matters. */ | |
3509 | while (elt != NULL) | |
3510 | { | |
9dce4196 | 3511 | asection *s; |
9dce4196 | 3512 | |
9dce4196 | 3513 | s = elt; |
415f38a6 AM |
3514 | if (!gas) |
3515 | s = s->output_section; | |
3516 | if (s != NULL | |
3517 | && !bfd_is_abs_section (s)) | |
01e1a5bc | 3518 | { |
415f38a6 AM |
3519 | unsigned int idx = elf_section_data (s)->this_idx; |
3520 | ||
01e1a5bc | 3521 | loc -= 4; |
01e1a5bc NC |
3522 | H_PUT_32 (abfd, idx, loc); |
3523 | } | |
945906ff | 3524 | elt = elf_next_in_group (elt); |
9dce4196 AM |
3525 | if (elt == first) |
3526 | break; | |
dbb410c3 AM |
3527 | } |
3528 | ||
3d7f7666 | 3529 | if ((loc -= 4) != sec->contents) |
9dce4196 | 3530 | abort (); |
dbb410c3 | 3531 | |
9dce4196 | 3532 | H_PUT_32 (abfd, sec->flags & SEC_LINK_ONCE ? GRP_COMDAT : 0, loc); |
dbb410c3 AM |
3533 | } |
3534 | ||
bd53a53a L |
3535 | /* Return the section which RELOC_SEC applies to. */ |
3536 | ||
3537 | asection * | |
3538 | _bfd_elf_get_reloc_section (asection *reloc_sec) | |
3539 | { | |
3540 | const char *name; | |
3541 | unsigned int type; | |
3542 | bfd *abfd; | |
3543 | ||
3544 | if (reloc_sec == NULL) | |
3545 | return NULL; | |
3546 | ||
3547 | type = elf_section_data (reloc_sec)->this_hdr.sh_type; | |
3548 | if (type != SHT_REL && type != SHT_RELA) | |
3549 | return NULL; | |
3550 | ||
3551 | /* We look up the section the relocs apply to by name. */ | |
3552 | name = reloc_sec->name; | |
3553 | if (type == SHT_REL) | |
3554 | name += 4; | |
3555 | else | |
3556 | name += 5; | |
3557 | ||
3558 | /* If a target needs .got.plt section, relocations in rela.plt/rel.plt | |
3559 | section apply to .got.plt section. */ | |
3560 | abfd = reloc_sec->owner; | |
3561 | if (get_elf_backend_data (abfd)->want_got_plt | |
3562 | && strcmp (name, ".plt") == 0) | |
87070c08 AM |
3563 | { |
3564 | /* .got.plt is a linker created input section. It may be mapped | |
3565 | to some other output section. Try two likely sections. */ | |
3566 | name = ".got.plt"; | |
3567 | reloc_sec = bfd_get_section_by_name (abfd, name); | |
3568 | if (reloc_sec != NULL) | |
3569 | return reloc_sec; | |
3570 | name = ".got"; | |
3571 | } | |
bd53a53a L |
3572 | |
3573 | reloc_sec = bfd_get_section_by_name (abfd, name); | |
3574 | return reloc_sec; | |
3575 | } | |
3576 | ||
252b5132 RH |
3577 | /* Assign all ELF section numbers. The dummy first section is handled here |
3578 | too. The link/info pointers for the standard section types are filled | |
3579 | in here too, while we're at it. */ | |
3580 | ||
b34976b6 | 3581 | static bfd_boolean |
da9f89d4 | 3582 | assign_section_numbers (bfd *abfd, struct bfd_link_info *link_info) |
252b5132 RH |
3583 | { |
3584 | struct elf_obj_tdata *t = elf_tdata (abfd); | |
3585 | asection *sec; | |
3e19fb8f | 3586 | unsigned int section_number; |
252b5132 | 3587 | Elf_Internal_Shdr **i_shdrp; |
47cc2cf5 | 3588 | struct bfd_elf_section_data *d; |
3516e984 | 3589 | bfd_boolean need_symtab; |
252b5132 RH |
3590 | |
3591 | section_number = 1; | |
3592 | ||
2b0f7ef9 JJ |
3593 | _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd)); |
3594 | ||
da9f89d4 | 3595 | /* SHT_GROUP sections are in relocatable files only. */ |
0e1862bb | 3596 | if (link_info == NULL || bfd_link_relocatable (link_info)) |
252b5132 | 3597 | { |
ef53be89 | 3598 | size_t reloc_count = 0; |
14f2c699 | 3599 | |
da9f89d4 | 3600 | /* Put SHT_GROUP sections first. */ |
04dd1667 | 3601 | for (sec = abfd->sections; sec != NULL; sec = sec->next) |
47cc2cf5 | 3602 | { |
5daa8fe7 | 3603 | d = elf_section_data (sec); |
da9f89d4 L |
3604 | |
3605 | if (d->this_hdr.sh_type == SHT_GROUP) | |
08a40648 | 3606 | { |
5daa8fe7 | 3607 | if (sec->flags & SEC_LINKER_CREATED) |
da9f89d4 L |
3608 | { |
3609 | /* Remove the linker created SHT_GROUP sections. */ | |
5daa8fe7 | 3610 | bfd_section_list_remove (abfd, sec); |
da9f89d4 | 3611 | abfd->section_count--; |
da9f89d4 | 3612 | } |
08a40648 | 3613 | else |
4fbb74a6 | 3614 | d->this_idx = section_number++; |
da9f89d4 | 3615 | } |
14f2c699 L |
3616 | |
3617 | /* Count relocations. */ | |
3618 | reloc_count += sec->reloc_count; | |
47cc2cf5 | 3619 | } |
14f2c699 L |
3620 | |
3621 | /* Clear HAS_RELOC if there are no relocations. */ | |
3622 | if (reloc_count == 0) | |
3623 | abfd->flags &= ~HAS_RELOC; | |
47cc2cf5 PB |
3624 | } |
3625 | ||
3626 | for (sec = abfd->sections; sec; sec = sec->next) | |
3627 | { | |
3628 | d = elf_section_data (sec); | |
3629 | ||
3630 | if (d->this_hdr.sh_type != SHT_GROUP) | |
4fbb74a6 | 3631 | d->this_idx = section_number++; |
3e19fb8f L |
3632 | if (d->this_hdr.sh_name != (unsigned int) -1) |
3633 | _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->this_hdr.sh_name); | |
d4730f92 | 3634 | if (d->rel.hdr) |
2b0f7ef9 | 3635 | { |
d4730f92 | 3636 | d->rel.idx = section_number++; |
3e19fb8f L |
3637 | if (d->rel.hdr->sh_name != (unsigned int) -1) |
3638 | _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel.hdr->sh_name); | |
2b0f7ef9 | 3639 | } |
d4730f92 BS |
3640 | else |
3641 | d->rel.idx = 0; | |
23bc299b | 3642 | |
d4730f92 | 3643 | if (d->rela.hdr) |
2b0f7ef9 | 3644 | { |
d4730f92 | 3645 | d->rela.idx = section_number++; |
3e19fb8f L |
3646 | if (d->rela.hdr->sh_name != (unsigned int) -1) |
3647 | _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rela.hdr->sh_name); | |
2b0f7ef9 | 3648 | } |
23bc299b | 3649 | else |
d4730f92 | 3650 | d->rela.idx = 0; |
252b5132 RH |
3651 | } |
3652 | ||
3516e984 L |
3653 | need_symtab = (bfd_get_symcount (abfd) > 0 |
3654 | || (link_info == NULL | |
3655 | && ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC)) | |
3656 | == HAS_RELOC))); | |
3657 | if (need_symtab) | |
252b5132 | 3658 | { |
12bd6957 | 3659 | elf_onesymtab (abfd) = section_number++; |
2b0f7ef9 | 3660 | _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->symtab_hdr.sh_name); |
4fbb74a6 | 3661 | if (section_number > ((SHN_LORESERVE - 2) & 0xFFFF)) |
9ad5cbcf | 3662 | { |
6a40cf0c NC |
3663 | elf_section_list * entry; |
3664 | ||
3665 | BFD_ASSERT (elf_symtab_shndx_list (abfd) == NULL); | |
3666 | ||
3667 | entry = bfd_zalloc (abfd, sizeof * entry); | |
3668 | entry->ndx = section_number++; | |
3669 | elf_symtab_shndx_list (abfd) = entry; | |
3670 | entry->hdr.sh_name | |
9ad5cbcf | 3671 | = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), |
b34976b6 | 3672 | ".symtab_shndx", FALSE); |
6a40cf0c | 3673 | if (entry->hdr.sh_name == (unsigned int) -1) |
b34976b6 | 3674 | return FALSE; |
9ad5cbcf | 3675 | } |
12bd6957 | 3676 | elf_strtab_sec (abfd) = section_number++; |
2b0f7ef9 | 3677 | _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->strtab_hdr.sh_name); |
252b5132 RH |
3678 | } |
3679 | ||
dd905818 NC |
3680 | elf_shstrtab_sec (abfd) = section_number++; |
3681 | _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->shstrtab_hdr.sh_name); | |
3682 | elf_elfheader (abfd)->e_shstrndx = elf_shstrtab_sec (abfd); | |
3683 | ||
1c52a645 L |
3684 | if (section_number >= SHN_LORESERVE) |
3685 | { | |
695344c0 | 3686 | /* xgettext:c-format */ |
1c52a645 L |
3687 | _bfd_error_handler (_("%B: too many sections: %u"), |
3688 | abfd, section_number); | |
3689 | return FALSE; | |
3690 | } | |
3691 | ||
9ad5cbcf | 3692 | elf_numsections (abfd) = section_number; |
252b5132 RH |
3693 | elf_elfheader (abfd)->e_shnum = section_number; |
3694 | ||
3695 | /* Set up the list of section header pointers, in agreement with the | |
3696 | indices. */ | |
a50b1753 NC |
3697 | i_shdrp = (Elf_Internal_Shdr **) bfd_zalloc2 (abfd, section_number, |
3698 | sizeof (Elf_Internal_Shdr *)); | |
252b5132 | 3699 | if (i_shdrp == NULL) |
b34976b6 | 3700 | return FALSE; |
252b5132 | 3701 | |
a50b1753 NC |
3702 | i_shdrp[0] = (Elf_Internal_Shdr *) bfd_zalloc (abfd, |
3703 | sizeof (Elf_Internal_Shdr)); | |
252b5132 RH |
3704 | if (i_shdrp[0] == NULL) |
3705 | { | |
3706 | bfd_release (abfd, i_shdrp); | |
b34976b6 | 3707 | return FALSE; |
252b5132 | 3708 | } |
252b5132 RH |
3709 | |
3710 | elf_elfsections (abfd) = i_shdrp; | |
3711 | ||
12bd6957 | 3712 | i_shdrp[elf_shstrtab_sec (abfd)] = &t->shstrtab_hdr; |
3516e984 | 3713 | if (need_symtab) |
252b5132 | 3714 | { |
12bd6957 | 3715 | i_shdrp[elf_onesymtab (abfd)] = &t->symtab_hdr; |
4fbb74a6 | 3716 | if (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF)) |
9ad5cbcf | 3717 | { |
6a40cf0c NC |
3718 | elf_section_list * entry = elf_symtab_shndx_list (abfd); |
3719 | BFD_ASSERT (entry != NULL); | |
3720 | i_shdrp[entry->ndx] = & entry->hdr; | |
3721 | entry->hdr.sh_link = elf_onesymtab (abfd); | |
9ad5cbcf | 3722 | } |
12bd6957 AM |
3723 | i_shdrp[elf_strtab_sec (abfd)] = &t->strtab_hdr; |
3724 | t->symtab_hdr.sh_link = elf_strtab_sec (abfd); | |
252b5132 | 3725 | } |
38ce5b11 | 3726 | |
252b5132 RH |
3727 | for (sec = abfd->sections; sec; sec = sec->next) |
3728 | { | |
252b5132 | 3729 | asection *s; |
252b5132 | 3730 | |
91d6fa6a NC |
3731 | d = elf_section_data (sec); |
3732 | ||
252b5132 | 3733 | i_shdrp[d->this_idx] = &d->this_hdr; |
d4730f92 BS |
3734 | if (d->rel.idx != 0) |
3735 | i_shdrp[d->rel.idx] = d->rel.hdr; | |
3736 | if (d->rela.idx != 0) | |
3737 | i_shdrp[d->rela.idx] = d->rela.hdr; | |
252b5132 RH |
3738 | |
3739 | /* Fill in the sh_link and sh_info fields while we're at it. */ | |
3740 | ||
3741 | /* sh_link of a reloc section is the section index of the symbol | |
3742 | table. sh_info is the section index of the section to which | |
3743 | the relocation entries apply. */ | |
d4730f92 | 3744 | if (d->rel.idx != 0) |
252b5132 | 3745 | { |
12bd6957 | 3746 | d->rel.hdr->sh_link = elf_onesymtab (abfd); |
d4730f92 | 3747 | d->rel.hdr->sh_info = d->this_idx; |
9ef5d938 | 3748 | d->rel.hdr->sh_flags |= SHF_INFO_LINK; |
252b5132 | 3749 | } |
d4730f92 | 3750 | if (d->rela.idx != 0) |
23bc299b | 3751 | { |
12bd6957 | 3752 | d->rela.hdr->sh_link = elf_onesymtab (abfd); |
d4730f92 | 3753 | d->rela.hdr->sh_info = d->this_idx; |
9ef5d938 | 3754 | d->rela.hdr->sh_flags |= SHF_INFO_LINK; |
23bc299b | 3755 | } |
252b5132 | 3756 | |
38ce5b11 L |
3757 | /* We need to set up sh_link for SHF_LINK_ORDER. */ |
3758 | if ((d->this_hdr.sh_flags & SHF_LINK_ORDER) != 0) | |
3759 | { | |
3760 | s = elf_linked_to_section (sec); | |
3761 | if (s) | |
38ce5b11 | 3762 | { |
f2876037 | 3763 | /* elf_linked_to_section points to the input section. */ |
ccd2ec6a | 3764 | if (link_info != NULL) |
38ce5b11 | 3765 | { |
f2876037 | 3766 | /* Check discarded linkonce section. */ |
dbaa2011 | 3767 | if (discarded_section (s)) |
38ce5b11 | 3768 | { |
ccd2ec6a | 3769 | asection *kept; |
4eca0228 | 3770 | _bfd_error_handler |
695344c0 | 3771 | /* xgettext:c-format */ |
ccd2ec6a L |
3772 | (_("%B: sh_link of section `%A' points to discarded section `%A' of `%B'"), |
3773 | abfd, d->this_hdr.bfd_section, | |
3774 | s, s->owner); | |
3775 | /* Point to the kept section if it has the same | |
3776 | size as the discarded one. */ | |
c0f00686 | 3777 | kept = _bfd_elf_check_kept_section (s, link_info); |
ccd2ec6a | 3778 | if (kept == NULL) |
185d09ad | 3779 | { |
ccd2ec6a L |
3780 | bfd_set_error (bfd_error_bad_value); |
3781 | return FALSE; | |
185d09ad | 3782 | } |
ccd2ec6a | 3783 | s = kept; |
38ce5b11 | 3784 | } |
e424ecc8 | 3785 | |
ccd2ec6a L |
3786 | s = s->output_section; |
3787 | BFD_ASSERT (s != NULL); | |
38ce5b11 | 3788 | } |
f2876037 L |
3789 | else |
3790 | { | |
3791 | /* Handle objcopy. */ | |
3792 | if (s->output_section == NULL) | |
3793 | { | |
4eca0228 | 3794 | _bfd_error_handler |
695344c0 | 3795 | /* xgettext:c-format */ |
f2876037 L |
3796 | (_("%B: sh_link of section `%A' points to removed section `%A' of `%B'"), |
3797 | abfd, d->this_hdr.bfd_section, s, s->owner); | |
3798 | bfd_set_error (bfd_error_bad_value); | |
3799 | return FALSE; | |
3800 | } | |
3801 | s = s->output_section; | |
3802 | } | |
ccd2ec6a L |
3803 | d->this_hdr.sh_link = elf_section_data (s)->this_idx; |
3804 | } | |
3805 | else | |
3806 | { | |
3807 | /* PR 290: | |
3808 | The Intel C compiler generates SHT_IA_64_UNWIND with | |
3809 | SHF_LINK_ORDER. But it doesn't set the sh_link or | |
3810 | sh_info fields. Hence we could get the situation | |
08a40648 | 3811 | where s is NULL. */ |
ccd2ec6a L |
3812 | const struct elf_backend_data *bed |
3813 | = get_elf_backend_data (abfd); | |
3814 | if (bed->link_order_error_handler) | |
3815 | bed->link_order_error_handler | |
695344c0 | 3816 | /* xgettext:c-format */ |
ccd2ec6a L |
3817 | (_("%B: warning: sh_link not set for section `%A'"), |
3818 | abfd, sec); | |
38ce5b11 L |
3819 | } |
3820 | } | |
3821 | ||
252b5132 RH |
3822 | switch (d->this_hdr.sh_type) |
3823 | { | |
3824 | case SHT_REL: | |
3825 | case SHT_RELA: | |
3826 | /* A reloc section which we are treating as a normal BFD | |
3827 | section. sh_link is the section index of the symbol | |
3828 | table. sh_info is the section index of the section to | |
3829 | which the relocation entries apply. We assume that an | |
3830 | allocated reloc section uses the dynamic symbol table. | |
3831 | FIXME: How can we be sure? */ | |
3832 | s = bfd_get_section_by_name (abfd, ".dynsym"); | |
3833 | if (s != NULL) | |
3834 | d->this_hdr.sh_link = elf_section_data (s)->this_idx; | |
3835 | ||
bd53a53a | 3836 | s = get_elf_backend_data (abfd)->get_reloc_section (sec); |
252b5132 | 3837 | if (s != NULL) |
9ef5d938 L |
3838 | { |
3839 | d->this_hdr.sh_info = elf_section_data (s)->this_idx; | |
3840 | d->this_hdr.sh_flags |= SHF_INFO_LINK; | |
3841 | } | |
252b5132 RH |
3842 | break; |
3843 | ||
3844 | case SHT_STRTAB: | |
3845 | /* We assume that a section named .stab*str is a stabs | |
3846 | string section. We look for a section with the same name | |
3847 | but without the trailing ``str'', and set its sh_link | |
3848 | field to point to this section. */ | |
0112cd26 | 3849 | if (CONST_STRNEQ (sec->name, ".stab") |
252b5132 RH |
3850 | && strcmp (sec->name + strlen (sec->name) - 3, "str") == 0) |
3851 | { | |
3852 | size_t len; | |
3853 | char *alc; | |
3854 | ||
3855 | len = strlen (sec->name); | |
a50b1753 | 3856 | alc = (char *) bfd_malloc (len - 2); |
252b5132 | 3857 | if (alc == NULL) |
b34976b6 | 3858 | return FALSE; |
d4c88bbb | 3859 | memcpy (alc, sec->name, len - 3); |
252b5132 RH |
3860 | alc[len - 3] = '\0'; |
3861 | s = bfd_get_section_by_name (abfd, alc); | |
3862 | free (alc); | |
3863 | if (s != NULL) | |
3864 | { | |
3865 | elf_section_data (s)->this_hdr.sh_link = d->this_idx; | |
3866 | ||
3867 | /* This is a .stab section. */ | |
0594c12d AM |
3868 | if (elf_section_data (s)->this_hdr.sh_entsize == 0) |
3869 | elf_section_data (s)->this_hdr.sh_entsize | |
3870 | = 4 + 2 * bfd_get_arch_size (abfd) / 8; | |
252b5132 RH |
3871 | } |
3872 | } | |
3873 | break; | |
3874 | ||
3875 | case SHT_DYNAMIC: | |
3876 | case SHT_DYNSYM: | |
3877 | case SHT_GNU_verneed: | |
3878 | case SHT_GNU_verdef: | |
3879 | /* sh_link is the section header index of the string table | |
3880 | used for the dynamic entries, or the symbol table, or the | |
3881 | version strings. */ | |
3882 | s = bfd_get_section_by_name (abfd, ".dynstr"); | |
3883 | if (s != NULL) | |
3884 | d->this_hdr.sh_link = elf_section_data (s)->this_idx; | |
3885 | break; | |
3886 | ||
7f1204bb JJ |
3887 | case SHT_GNU_LIBLIST: |
3888 | /* sh_link is the section header index of the prelink library | |
08a40648 AM |
3889 | list used for the dynamic entries, or the symbol table, or |
3890 | the version strings. */ | |
7f1204bb JJ |
3891 | s = bfd_get_section_by_name (abfd, (sec->flags & SEC_ALLOC) |
3892 | ? ".dynstr" : ".gnu.libstr"); | |
3893 | if (s != NULL) | |
3894 | d->this_hdr.sh_link = elf_section_data (s)->this_idx; | |
3895 | break; | |
3896 | ||
252b5132 | 3897 | case SHT_HASH: |
fdc90cb4 | 3898 | case SHT_GNU_HASH: |
252b5132 RH |
3899 | case SHT_GNU_versym: |
3900 | /* sh_link is the section header index of the symbol table | |
3901 | this hash table or version table is for. */ | |
3902 | s = bfd_get_section_by_name (abfd, ".dynsym"); | |
3903 | if (s != NULL) | |
3904 | d->this_hdr.sh_link = elf_section_data (s)->this_idx; | |
3905 | break; | |
dbb410c3 AM |
3906 | |
3907 | case SHT_GROUP: | |
12bd6957 | 3908 | d->this_hdr.sh_link = elf_onesymtab (abfd); |
252b5132 RH |
3909 | } |
3910 | } | |
3911 | ||
3e19fb8f L |
3912 | /* Delay setting sh_name to _bfd_elf_write_object_contents so that |
3913 | _bfd_elf_assign_file_positions_for_non_load can convert DWARF | |
3914 | debug section name from .debug_* to .zdebug_* if needed. */ | |
3915 | ||
b34976b6 | 3916 | return TRUE; |
252b5132 RH |
3917 | } |
3918 | ||
5372391b | 3919 | static bfd_boolean |
217aa764 | 3920 | sym_is_global (bfd *abfd, asymbol *sym) |
252b5132 RH |
3921 | { |
3922 | /* If the backend has a special mapping, use it. */ | |
9c5bfbb7 | 3923 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
217aa764 AM |
3924 | if (bed->elf_backend_sym_is_global) |
3925 | return (*bed->elf_backend_sym_is_global) (abfd, sym); | |
252b5132 | 3926 | |
e47bf690 | 3927 | return ((sym->flags & (BSF_GLOBAL | BSF_WEAK | BSF_GNU_UNIQUE)) != 0 |
252b5132 RH |
3928 | || bfd_is_und_section (bfd_get_section (sym)) |
3929 | || bfd_is_com_section (bfd_get_section (sym))); | |
3930 | } | |
3931 | ||
76359541 TP |
3932 | /* Filter global symbols of ABFD to include in the import library. All |
3933 | SYMCOUNT symbols of ABFD can be examined from their pointers in | |
3934 | SYMS. Pointers of symbols to keep should be stored contiguously at | |
3935 | the beginning of that array. | |
3936 | ||
3937 | Returns the number of symbols to keep. */ | |
3938 | ||
3939 | unsigned int | |
3940 | _bfd_elf_filter_global_symbols (bfd *abfd, struct bfd_link_info *info, | |
3941 | asymbol **syms, long symcount) | |
3942 | { | |
3943 | long src_count, dst_count = 0; | |
3944 | ||
3945 | for (src_count = 0; src_count < symcount; src_count++) | |
3946 | { | |
3947 | asymbol *sym = syms[src_count]; | |
3948 | char *name = (char *) bfd_asymbol_name (sym); | |
3949 | struct bfd_link_hash_entry *h; | |
3950 | ||
3951 | if (!sym_is_global (abfd, sym)) | |
3952 | continue; | |
3953 | ||
3954 | h = bfd_link_hash_lookup (info->hash, name, FALSE, FALSE, FALSE); | |
5df1bc57 AM |
3955 | if (h == NULL) |
3956 | continue; | |
76359541 TP |
3957 | if (h->type != bfd_link_hash_defined && h->type != bfd_link_hash_defweak) |
3958 | continue; | |
76359541 TP |
3959 | if (h->linker_def || h->ldscript_def) |
3960 | continue; | |
3961 | ||
3962 | syms[dst_count++] = sym; | |
3963 | } | |
3964 | ||
3965 | syms[dst_count] = NULL; | |
3966 | ||
3967 | return dst_count; | |
3968 | } | |
3969 | ||
5372391b | 3970 | /* Don't output section symbols for sections that are not going to be |
c6d8cab4 | 3971 | output, that are duplicates or there is no BFD section. */ |
5372391b AM |
3972 | |
3973 | static bfd_boolean | |
3974 | ignore_section_sym (bfd *abfd, asymbol *sym) | |
3975 | { | |
c6d8cab4 L |
3976 | elf_symbol_type *type_ptr; |
3977 | ||
3978 | if ((sym->flags & BSF_SECTION_SYM) == 0) | |
3979 | return FALSE; | |
3980 | ||
3981 | type_ptr = elf_symbol_from (abfd, sym); | |
3982 | return ((type_ptr != NULL | |
3983 | && type_ptr->internal_elf_sym.st_shndx != 0 | |
3984 | && bfd_is_abs_section (sym->section)) | |
3985 | || !(sym->section->owner == abfd | |
0f0a5e58 | 3986 | || (sym->section->output_section->owner == abfd |
2633a79c AM |
3987 | && sym->section->output_offset == 0) |
3988 | || bfd_is_abs_section (sym->section))); | |
5372391b AM |
3989 | } |
3990 | ||
2633a79c AM |
3991 | /* Map symbol from it's internal number to the external number, moving |
3992 | all local symbols to be at the head of the list. */ | |
3993 | ||
b34976b6 | 3994 | static bfd_boolean |
12bd6957 | 3995 | elf_map_symbols (bfd *abfd, unsigned int *pnum_locals) |
252b5132 | 3996 | { |
dc810e39 | 3997 | unsigned int symcount = bfd_get_symcount (abfd); |
252b5132 RH |
3998 | asymbol **syms = bfd_get_outsymbols (abfd); |
3999 | asymbol **sect_syms; | |
dc810e39 AM |
4000 | unsigned int num_locals = 0; |
4001 | unsigned int num_globals = 0; | |
4002 | unsigned int num_locals2 = 0; | |
4003 | unsigned int num_globals2 = 0; | |
7292b3ac | 4004 | unsigned int max_index = 0; |
dc810e39 | 4005 | unsigned int idx; |
252b5132 RH |
4006 | asection *asect; |
4007 | asymbol **new_syms; | |
252b5132 RH |
4008 | |
4009 | #ifdef DEBUG | |
4010 | fprintf (stderr, "elf_map_symbols\n"); | |
4011 | fflush (stderr); | |
4012 | #endif | |
4013 | ||
252b5132 RH |
4014 | for (asect = abfd->sections; asect; asect = asect->next) |
4015 | { | |
4016 | if (max_index < asect->index) | |
4017 | max_index = asect->index; | |
4018 | } | |
4019 | ||
4020 | max_index++; | |
a50b1753 | 4021 | sect_syms = (asymbol **) bfd_zalloc2 (abfd, max_index, sizeof (asymbol *)); |
252b5132 | 4022 | if (sect_syms == NULL) |
b34976b6 | 4023 | return FALSE; |
252b5132 | 4024 | elf_section_syms (abfd) = sect_syms; |
4e89ac30 | 4025 | elf_num_section_syms (abfd) = max_index; |
252b5132 | 4026 | |
079e9a2f AM |
4027 | /* Init sect_syms entries for any section symbols we have already |
4028 | decided to output. */ | |
252b5132 RH |
4029 | for (idx = 0; idx < symcount; idx++) |
4030 | { | |
dc810e39 | 4031 | asymbol *sym = syms[idx]; |
c044fabd | 4032 | |
252b5132 | 4033 | if ((sym->flags & BSF_SECTION_SYM) != 0 |
0f0a5e58 | 4034 | && sym->value == 0 |
2633a79c AM |
4035 | && !ignore_section_sym (abfd, sym) |
4036 | && !bfd_is_abs_section (sym->section)) | |
252b5132 | 4037 | { |
5372391b | 4038 | asection *sec = sym->section; |
252b5132 | 4039 | |
5372391b AM |
4040 | if (sec->owner != abfd) |
4041 | sec = sec->output_section; | |
252b5132 | 4042 | |
5372391b | 4043 | sect_syms[sec->index] = syms[idx]; |
252b5132 RH |
4044 | } |
4045 | } | |
4046 | ||
252b5132 RH |
4047 | /* Classify all of the symbols. */ |
4048 | for (idx = 0; idx < symcount; idx++) | |
4049 | { | |
2633a79c | 4050 | if (sym_is_global (abfd, syms[idx])) |
252b5132 | 4051 | num_globals++; |
2633a79c AM |
4052 | else if (!ignore_section_sym (abfd, syms[idx])) |
4053 | num_locals++; | |
252b5132 | 4054 | } |
079e9a2f | 4055 | |
5372391b | 4056 | /* We will be adding a section symbol for each normal BFD section. Most |
079e9a2f AM |
4057 | sections will already have a section symbol in outsymbols, but |
4058 | eg. SHT_GROUP sections will not, and we need the section symbol mapped | |
4059 | at least in that case. */ | |
252b5132 RH |
4060 | for (asect = abfd->sections; asect; asect = asect->next) |
4061 | { | |
079e9a2f | 4062 | if (sect_syms[asect->index] == NULL) |
252b5132 | 4063 | { |
079e9a2f | 4064 | if (!sym_is_global (abfd, asect->symbol)) |
252b5132 RH |
4065 | num_locals++; |
4066 | else | |
4067 | num_globals++; | |
252b5132 RH |
4068 | } |
4069 | } | |
4070 | ||
4071 | /* Now sort the symbols so the local symbols are first. */ | |
a50b1753 NC |
4072 | new_syms = (asymbol **) bfd_alloc2 (abfd, num_locals + num_globals, |
4073 | sizeof (asymbol *)); | |
dc810e39 | 4074 | |
252b5132 | 4075 | if (new_syms == NULL) |
b34976b6 | 4076 | return FALSE; |
252b5132 RH |
4077 | |
4078 | for (idx = 0; idx < symcount; idx++) | |
4079 | { | |
4080 | asymbol *sym = syms[idx]; | |
dc810e39 | 4081 | unsigned int i; |
252b5132 | 4082 | |
2633a79c AM |
4083 | if (sym_is_global (abfd, sym)) |
4084 | i = num_locals + num_globals2++; | |
4085 | else if (!ignore_section_sym (abfd, sym)) | |
252b5132 RH |
4086 | i = num_locals2++; |
4087 | else | |
2633a79c | 4088 | continue; |
252b5132 RH |
4089 | new_syms[i] = sym; |
4090 | sym->udata.i = i + 1; | |
4091 | } | |
4092 | for (asect = abfd->sections; asect; asect = asect->next) | |
4093 | { | |
079e9a2f | 4094 | if (sect_syms[asect->index] == NULL) |
252b5132 | 4095 | { |
079e9a2f | 4096 | asymbol *sym = asect->symbol; |
dc810e39 | 4097 | unsigned int i; |
252b5132 | 4098 | |
079e9a2f | 4099 | sect_syms[asect->index] = sym; |
252b5132 RH |
4100 | if (!sym_is_global (abfd, sym)) |
4101 | i = num_locals2++; | |
4102 | else | |
4103 | i = num_locals + num_globals2++; | |
4104 | new_syms[i] = sym; | |
4105 | sym->udata.i = i + 1; | |
4106 | } | |
4107 | } | |
4108 | ||
4109 | bfd_set_symtab (abfd, new_syms, num_locals + num_globals); | |
4110 | ||
12bd6957 | 4111 | *pnum_locals = num_locals; |
b34976b6 | 4112 | return TRUE; |
252b5132 RH |
4113 | } |
4114 | ||
4115 | /* Align to the maximum file alignment that could be required for any | |
4116 | ELF data structure. */ | |
4117 | ||
268b6b39 | 4118 | static inline file_ptr |
217aa764 | 4119 | align_file_position (file_ptr off, int align) |
252b5132 RH |
4120 | { |
4121 | return (off + align - 1) & ~(align - 1); | |
4122 | } | |
4123 | ||
4124 | /* Assign a file position to a section, optionally aligning to the | |
4125 | required section alignment. */ | |
4126 | ||
217aa764 AM |
4127 | file_ptr |
4128 | _bfd_elf_assign_file_position_for_section (Elf_Internal_Shdr *i_shdrp, | |
4129 | file_ptr offset, | |
4130 | bfd_boolean align) | |
252b5132 | 4131 | { |
72de5009 AM |
4132 | if (align && i_shdrp->sh_addralign > 1) |
4133 | offset = BFD_ALIGN (offset, i_shdrp->sh_addralign); | |
252b5132 RH |
4134 | i_shdrp->sh_offset = offset; |
4135 | if (i_shdrp->bfd_section != NULL) | |
4136 | i_shdrp->bfd_section->filepos = offset; | |
4137 | if (i_shdrp->sh_type != SHT_NOBITS) | |
4138 | offset += i_shdrp->sh_size; | |
4139 | return offset; | |
4140 | } | |
4141 | ||
4142 | /* Compute the file positions we are going to put the sections at, and | |
4143 | otherwise prepare to begin writing out the ELF file. If LINK_INFO | |
4144 | is not NULL, this is being called by the ELF backend linker. */ | |
4145 | ||
b34976b6 | 4146 | bfd_boolean |
217aa764 AM |
4147 | _bfd_elf_compute_section_file_positions (bfd *abfd, |
4148 | struct bfd_link_info *link_info) | |
252b5132 | 4149 | { |
9c5bfbb7 | 4150 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
d4730f92 | 4151 | struct fake_section_arg fsargs; |
b34976b6 | 4152 | bfd_boolean failed; |
ef10c3ac | 4153 | struct elf_strtab_hash *strtab = NULL; |
252b5132 | 4154 | Elf_Internal_Shdr *shstrtab_hdr; |
3516e984 | 4155 | bfd_boolean need_symtab; |
252b5132 RH |
4156 | |
4157 | if (abfd->output_has_begun) | |
b34976b6 | 4158 | return TRUE; |
252b5132 RH |
4159 | |
4160 | /* Do any elf backend specific processing first. */ | |
4161 | if (bed->elf_backend_begin_write_processing) | |
4162 | (*bed->elf_backend_begin_write_processing) (abfd, link_info); | |
4163 | ||
4164 | if (! prep_headers (abfd)) | |
b34976b6 | 4165 | return FALSE; |
252b5132 | 4166 | |
e6c51ed4 | 4167 | /* Post process the headers if necessary. */ |
78245035 | 4168 | (*bed->elf_backend_post_process_headers) (abfd, link_info); |
e6c51ed4 | 4169 | |
d4730f92 BS |
4170 | fsargs.failed = FALSE; |
4171 | fsargs.link_info = link_info; | |
4172 | bfd_map_over_sections (abfd, elf_fake_sections, &fsargs); | |
4173 | if (fsargs.failed) | |
b34976b6 | 4174 | return FALSE; |
252b5132 | 4175 | |
da9f89d4 | 4176 | if (!assign_section_numbers (abfd, link_info)) |
b34976b6 | 4177 | return FALSE; |
252b5132 RH |
4178 | |
4179 | /* The backend linker builds symbol table information itself. */ | |
3516e984 L |
4180 | need_symtab = (link_info == NULL |
4181 | && (bfd_get_symcount (abfd) > 0 | |
4182 | || ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC)) | |
4183 | == HAS_RELOC))); | |
4184 | if (need_symtab) | |
252b5132 RH |
4185 | { |
4186 | /* Non-zero if doing a relocatable link. */ | |
4187 | int relocatable_p = ! (abfd->flags & (EXEC_P | DYNAMIC)); | |
4188 | ||
4189 | if (! swap_out_syms (abfd, &strtab, relocatable_p)) | |
b34976b6 | 4190 | return FALSE; |
252b5132 RH |
4191 | } |
4192 | ||
d4730f92 | 4193 | failed = FALSE; |
1126897b | 4194 | if (link_info == NULL) |
dbb410c3 | 4195 | { |
1126897b | 4196 | bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed); |
dbb410c3 | 4197 | if (failed) |
b34976b6 | 4198 | return FALSE; |
dbb410c3 AM |
4199 | } |
4200 | ||
252b5132 RH |
4201 | shstrtab_hdr = &elf_tdata (abfd)->shstrtab_hdr; |
4202 | /* sh_name was set in prep_headers. */ | |
4203 | shstrtab_hdr->sh_type = SHT_STRTAB; | |
84865015 | 4204 | shstrtab_hdr->sh_flags = bed->elf_strtab_flags; |
252b5132 | 4205 | shstrtab_hdr->sh_addr = 0; |
946748d5 | 4206 | /* sh_size is set in _bfd_elf_assign_file_positions_for_non_load. */ |
252b5132 RH |
4207 | shstrtab_hdr->sh_entsize = 0; |
4208 | shstrtab_hdr->sh_link = 0; | |
4209 | shstrtab_hdr->sh_info = 0; | |
3e19fb8f | 4210 | /* sh_offset is set in _bfd_elf_assign_file_positions_for_non_load. */ |
252b5132 RH |
4211 | shstrtab_hdr->sh_addralign = 1; |
4212 | ||
c84fca4d | 4213 | if (!assign_file_positions_except_relocs (abfd, link_info)) |
b34976b6 | 4214 | return FALSE; |
252b5132 | 4215 | |
3516e984 | 4216 | if (need_symtab) |
252b5132 RH |
4217 | { |
4218 | file_ptr off; | |
4219 | Elf_Internal_Shdr *hdr; | |
4220 | ||
12bd6957 | 4221 | off = elf_next_file_pos (abfd); |
252b5132 | 4222 | |
6a40cf0c | 4223 | hdr = & elf_symtab_hdr (abfd); |
b34976b6 | 4224 | off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE); |
252b5132 | 4225 | |
6a40cf0c NC |
4226 | if (elf_symtab_shndx_list (abfd) != NULL) |
4227 | { | |
4228 | hdr = & elf_symtab_shndx_list (abfd)->hdr; | |
4229 | if (hdr->sh_size != 0) | |
4230 | off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE); | |
4231 | /* FIXME: What about other symtab_shndx sections in the list ? */ | |
4232 | } | |
9ad5cbcf | 4233 | |
252b5132 | 4234 | hdr = &elf_tdata (abfd)->strtab_hdr; |
b34976b6 | 4235 | off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE); |
252b5132 | 4236 | |
12bd6957 | 4237 | elf_next_file_pos (abfd) = off; |
252b5132 RH |
4238 | |
4239 | /* Now that we know where the .strtab section goes, write it | |
08a40648 | 4240 | out. */ |
252b5132 | 4241 | if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0 |
ef10c3ac | 4242 | || ! _bfd_elf_strtab_emit (abfd, strtab)) |
b34976b6 | 4243 | return FALSE; |
ef10c3ac | 4244 | _bfd_elf_strtab_free (strtab); |
252b5132 RH |
4245 | } |
4246 | ||
b34976b6 | 4247 | abfd->output_has_begun = TRUE; |
252b5132 | 4248 | |
b34976b6 | 4249 | return TRUE; |
252b5132 RH |
4250 | } |
4251 | ||
8ded5a0f AM |
4252 | /* Make an initial estimate of the size of the program header. If we |
4253 | get the number wrong here, we'll redo section placement. */ | |
4254 | ||
4255 | static bfd_size_type | |
4256 | get_program_header_size (bfd *abfd, struct bfd_link_info *info) | |
4257 | { | |
4258 | size_t segs; | |
4259 | asection *s; | |
2b05f1b7 | 4260 | const struct elf_backend_data *bed; |
8ded5a0f AM |
4261 | |
4262 | /* Assume we will need exactly two PT_LOAD segments: one for text | |
4263 | and one for data. */ | |
4264 | segs = 2; | |
4265 | ||
4266 | s = bfd_get_section_by_name (abfd, ".interp"); | |
4267 | if (s != NULL && (s->flags & SEC_LOAD) != 0) | |
4268 | { | |
4269 | /* If we have a loadable interpreter section, we need a | |
4270 | PT_INTERP segment. In this case, assume we also need a | |
4271 | PT_PHDR segment, although that may not be true for all | |
4272 | targets. */ | |
e9a38e0f | 4273 | segs += 2; |
8ded5a0f AM |
4274 | } |
4275 | ||
4276 | if (bfd_get_section_by_name (abfd, ".dynamic") != NULL) | |
4277 | { | |
4278 | /* We need a PT_DYNAMIC segment. */ | |
4279 | ++segs; | |
f210dcff | 4280 | } |
08a40648 | 4281 | |
ceae84aa | 4282 | if (info != NULL && info->relro) |
f210dcff L |
4283 | { |
4284 | /* We need a PT_GNU_RELRO segment. */ | |
4285 | ++segs; | |
8ded5a0f AM |
4286 | } |
4287 | ||
12bd6957 | 4288 | if (elf_eh_frame_hdr (abfd)) |
8ded5a0f AM |
4289 | { |
4290 | /* We need a PT_GNU_EH_FRAME segment. */ | |
4291 | ++segs; | |
4292 | } | |
4293 | ||
12bd6957 | 4294 | if (elf_stack_flags (abfd)) |
8ded5a0f | 4295 | { |
2b05f1b7 L |
4296 | /* We need a PT_GNU_STACK segment. */ |
4297 | ++segs; | |
4298 | } | |
94b11780 | 4299 | |
2b05f1b7 L |
4300 | for (s = abfd->sections; s != NULL; s = s->next) |
4301 | { | |
8ded5a0f | 4302 | if ((s->flags & SEC_LOAD) != 0 |
0112cd26 | 4303 | && CONST_STRNEQ (s->name, ".note")) |
8ded5a0f AM |
4304 | { |
4305 | /* We need a PT_NOTE segment. */ | |
4306 | ++segs; | |
1c5265b5 JJ |
4307 | /* Try to create just one PT_NOTE segment |
4308 | for all adjacent loadable .note* sections. | |
4309 | gABI requires that within a PT_NOTE segment | |
4310 | (and also inside of each SHT_NOTE section) | |
4311 | each note is padded to a multiple of 4 size, | |
4312 | so we check whether the sections are correctly | |
4313 | aligned. */ | |
4314 | if (s->alignment_power == 2) | |
4315 | while (s->next != NULL | |
4316 | && s->next->alignment_power == 2 | |
4317 | && (s->next->flags & SEC_LOAD) != 0 | |
4318 | && CONST_STRNEQ (s->next->name, ".note")) | |
4319 | s = s->next; | |
8ded5a0f AM |
4320 | } |
4321 | } | |
4322 | ||
4323 | for (s = abfd->sections; s != NULL; s = s->next) | |
4324 | { | |
4325 | if (s->flags & SEC_THREAD_LOCAL) | |
4326 | { | |
4327 | /* We need a PT_TLS segment. */ | |
4328 | ++segs; | |
4329 | break; | |
4330 | } | |
4331 | } | |
4332 | ||
2b05f1b7 | 4333 | bed = get_elf_backend_data (abfd); |
a91e1603 L |
4334 | |
4335 | if ((abfd->flags & D_PAGED) != 0) | |
4336 | { | |
4337 | /* Add a PT_GNU_MBIND segment for each mbind section. */ | |
4338 | unsigned int page_align_power = bfd_log2 (bed->commonpagesize); | |
4339 | for (s = abfd->sections; s != NULL; s = s->next) | |
4340 | if (elf_section_flags (s) & SHF_GNU_MBIND) | |
4341 | { | |
4342 | if (elf_section_data (s)->this_hdr.sh_info | |
4343 | > PT_GNU_MBIND_NUM) | |
4344 | { | |
4345 | _bfd_error_handler | |
4346 | /* xgettext:c-format */ | |
4347 | (_("%B: GNU_MBIN section `%A' has invalid sh_info field: %d"), | |
4348 | abfd, s, elf_section_data (s)->this_hdr.sh_info); | |
4349 | continue; | |
4350 | } | |
4351 | /* Align mbind section to page size. */ | |
4352 | if (s->alignment_power < page_align_power) | |
4353 | s->alignment_power = page_align_power; | |
4354 | segs ++; | |
4355 | } | |
4356 | } | |
4357 | ||
4358 | /* Let the backend count up any program headers it might need. */ | |
4359 | if (bed->elf_backend_additional_program_headers) | |
8ded5a0f AM |
4360 | { |
4361 | int a; | |
4362 | ||
4363 | a = (*bed->elf_backend_additional_program_headers) (abfd, info); | |
4364 | if (a == -1) | |
4365 | abort (); | |
4366 | segs += a; | |
4367 | } | |
4368 | ||
4369 | return segs * bed->s->sizeof_phdr; | |
4370 | } | |
4371 | ||
2ea37f1c NC |
4372 | /* Find the segment that contains the output_section of section. */ |
4373 | ||
4374 | Elf_Internal_Phdr * | |
4375 | _bfd_elf_find_segment_containing_section (bfd * abfd, asection * section) | |
4376 | { | |
4377 | struct elf_segment_map *m; | |
4378 | Elf_Internal_Phdr *p; | |
4379 | ||
12bd6957 | 4380 | for (m = elf_seg_map (abfd), p = elf_tdata (abfd)->phdr; |
2ea37f1c NC |
4381 | m != NULL; |
4382 | m = m->next, p++) | |
4383 | { | |
4384 | int i; | |
4385 | ||
4386 | for (i = m->count - 1; i >= 0; i--) | |
4387 | if (m->sections[i] == section) | |
4388 | return p; | |
4389 | } | |
4390 | ||
4391 | return NULL; | |
4392 | } | |
4393 | ||
252b5132 RH |
4394 | /* Create a mapping from a set of sections to a program segment. */ |
4395 | ||
217aa764 AM |
4396 | static struct elf_segment_map * |
4397 | make_mapping (bfd *abfd, | |
4398 | asection **sections, | |
4399 | unsigned int from, | |
4400 | unsigned int to, | |
4401 | bfd_boolean phdr) | |
252b5132 RH |
4402 | { |
4403 | struct elf_segment_map *m; | |
4404 | unsigned int i; | |
4405 | asection **hdrpp; | |
dc810e39 | 4406 | bfd_size_type amt; |
252b5132 | 4407 | |
dc810e39 AM |
4408 | amt = sizeof (struct elf_segment_map); |
4409 | amt += (to - from - 1) * sizeof (asection *); | |
a50b1753 | 4410 | m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); |
252b5132 RH |
4411 | if (m == NULL) |
4412 | return NULL; | |
4413 | m->next = NULL; | |
4414 | m->p_type = PT_LOAD; | |
4415 | for (i = from, hdrpp = sections + from; i < to; i++, hdrpp++) | |
4416 | m->sections[i - from] = *hdrpp; | |
4417 | m->count = to - from; | |
4418 | ||
4419 | if (from == 0 && phdr) | |
4420 | { | |
4421 | /* Include the headers in the first PT_LOAD segment. */ | |
4422 | m->includes_filehdr = 1; | |
4423 | m->includes_phdrs = 1; | |
4424 | } | |
4425 | ||
4426 | return m; | |
4427 | } | |
4428 | ||
229fcec5 MM |
4429 | /* Create the PT_DYNAMIC segment, which includes DYNSEC. Returns NULL |
4430 | on failure. */ | |
4431 | ||
4432 | struct elf_segment_map * | |
4433 | _bfd_elf_make_dynamic_segment (bfd *abfd, asection *dynsec) | |
4434 | { | |
4435 | struct elf_segment_map *m; | |
4436 | ||
a50b1753 NC |
4437 | m = (struct elf_segment_map *) bfd_zalloc (abfd, |
4438 | sizeof (struct elf_segment_map)); | |
229fcec5 MM |
4439 | if (m == NULL) |
4440 | return NULL; | |
4441 | m->next = NULL; | |
4442 | m->p_type = PT_DYNAMIC; | |
4443 | m->count = 1; | |
4444 | m->sections[0] = dynsec; | |
08a40648 | 4445 | |
229fcec5 MM |
4446 | return m; |
4447 | } | |
4448 | ||
8ded5a0f | 4449 | /* Possibly add or remove segments from the segment map. */ |
252b5132 | 4450 | |
b34976b6 | 4451 | static bfd_boolean |
3dea8fca AM |
4452 | elf_modify_segment_map (bfd *abfd, |
4453 | struct bfd_link_info *info, | |
4454 | bfd_boolean remove_empty_load) | |
252b5132 | 4455 | { |
252e386e | 4456 | struct elf_segment_map **m; |
8ded5a0f | 4457 | const struct elf_backend_data *bed; |
252b5132 | 4458 | |
8ded5a0f AM |
4459 | /* The placement algorithm assumes that non allocated sections are |
4460 | not in PT_LOAD segments. We ensure this here by removing such | |
4461 | sections from the segment map. We also remove excluded | |
252e386e AM |
4462 | sections. Finally, any PT_LOAD segment without sections is |
4463 | removed. */ | |
12bd6957 | 4464 | m = &elf_seg_map (abfd); |
252e386e | 4465 | while (*m) |
8ded5a0f AM |
4466 | { |
4467 | unsigned int i, new_count; | |
252b5132 | 4468 | |
252e386e | 4469 | for (new_count = 0, i = 0; i < (*m)->count; i++) |
8ded5a0f | 4470 | { |
252e386e AM |
4471 | if (((*m)->sections[i]->flags & SEC_EXCLUDE) == 0 |
4472 | && (((*m)->sections[i]->flags & SEC_ALLOC) != 0 | |
4473 | || (*m)->p_type != PT_LOAD)) | |
8ded5a0f | 4474 | { |
252e386e AM |
4475 | (*m)->sections[new_count] = (*m)->sections[i]; |
4476 | new_count++; | |
8ded5a0f AM |
4477 | } |
4478 | } | |
252e386e | 4479 | (*m)->count = new_count; |
252b5132 | 4480 | |
1a9ccd70 NC |
4481 | if (remove_empty_load |
4482 | && (*m)->p_type == PT_LOAD | |
4483 | && (*m)->count == 0 | |
4484 | && !(*m)->includes_phdrs) | |
252e386e AM |
4485 | *m = (*m)->next; |
4486 | else | |
4487 | m = &(*m)->next; | |
8ded5a0f | 4488 | } |
252b5132 | 4489 | |
8ded5a0f AM |
4490 | bed = get_elf_backend_data (abfd); |
4491 | if (bed->elf_backend_modify_segment_map != NULL) | |
252b5132 | 4492 | { |
252e386e | 4493 | if (!(*bed->elf_backend_modify_segment_map) (abfd, info)) |
8ded5a0f | 4494 | return FALSE; |
252b5132 | 4495 | } |
252b5132 | 4496 | |
8ded5a0f AM |
4497 | return TRUE; |
4498 | } | |
252b5132 | 4499 | |
8ded5a0f | 4500 | /* Set up a mapping from BFD sections to program segments. */ |
252b5132 | 4501 | |
8ded5a0f AM |
4502 | bfd_boolean |
4503 | _bfd_elf_map_sections_to_segments (bfd *abfd, struct bfd_link_info *info) | |
4504 | { | |
4505 | unsigned int count; | |
4506 | struct elf_segment_map *m; | |
4507 | asection **sections = NULL; | |
4508 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
3dea8fca | 4509 | bfd_boolean no_user_phdrs; |
252b5132 | 4510 | |
12bd6957 | 4511 | no_user_phdrs = elf_seg_map (abfd) == NULL; |
d324f6d6 RM |
4512 | |
4513 | if (info != NULL) | |
4514 | info->user_phdrs = !no_user_phdrs; | |
4515 | ||
3dea8fca | 4516 | if (no_user_phdrs && bfd_count_sections (abfd) != 0) |
252b5132 | 4517 | { |
8ded5a0f AM |
4518 | asection *s; |
4519 | unsigned int i; | |
4520 | struct elf_segment_map *mfirst; | |
4521 | struct elf_segment_map **pm; | |
4522 | asection *last_hdr; | |
4523 | bfd_vma last_size; | |
4524 | unsigned int phdr_index; | |
4525 | bfd_vma maxpagesize; | |
4526 | asection **hdrpp; | |
4527 | bfd_boolean phdr_in_segment = TRUE; | |
4528 | bfd_boolean writable; | |
4529 | int tls_count = 0; | |
4530 | asection *first_tls = NULL; | |
a91e1603 | 4531 | asection *first_mbind = NULL; |
8ded5a0f AM |
4532 | asection *dynsec, *eh_frame_hdr; |
4533 | bfd_size_type amt; | |
8d06853e | 4534 | bfd_vma addr_mask, wrap_to = 0; |
1a9ccd70 | 4535 | bfd_boolean linker_created_pt_phdr_segment = FALSE; |
252b5132 | 4536 | |
8ded5a0f | 4537 | /* Select the allocated sections, and sort them. */ |
252b5132 | 4538 | |
a50b1753 NC |
4539 | sections = (asection **) bfd_malloc2 (bfd_count_sections (abfd), |
4540 | sizeof (asection *)); | |
8ded5a0f | 4541 | if (sections == NULL) |
252b5132 | 4542 | goto error_return; |
252b5132 | 4543 | |
8d06853e AM |
4544 | /* Calculate top address, avoiding undefined behaviour of shift |
4545 | left operator when shift count is equal to size of type | |
4546 | being shifted. */ | |
4547 | addr_mask = ((bfd_vma) 1 << (bfd_arch_bits_per_address (abfd) - 1)) - 1; | |
4548 | addr_mask = (addr_mask << 1) + 1; | |
4549 | ||
8ded5a0f AM |
4550 | i = 0; |
4551 | for (s = abfd->sections; s != NULL; s = s->next) | |
4552 | { | |
4553 | if ((s->flags & SEC_ALLOC) != 0) | |
4554 | { | |
4555 | sections[i] = s; | |
4556 | ++i; | |
8d06853e AM |
4557 | /* A wrapping section potentially clashes with header. */ |
4558 | if (((s->lma + s->size) & addr_mask) < (s->lma & addr_mask)) | |
4559 | wrap_to = (s->lma + s->size) & addr_mask; | |
8ded5a0f AM |
4560 | } |
4561 | } | |
4562 | BFD_ASSERT (i <= bfd_count_sections (abfd)); | |
4563 | count = i; | |
252b5132 | 4564 | |
8ded5a0f | 4565 | qsort (sections, (size_t) count, sizeof (asection *), elf_sort_sections); |
252b5132 | 4566 | |
8ded5a0f | 4567 | /* Build the mapping. */ |
252b5132 | 4568 | |
8ded5a0f AM |
4569 | mfirst = NULL; |
4570 | pm = &mfirst; | |
252b5132 | 4571 | |
8ded5a0f AM |
4572 | /* If we have a .interp section, then create a PT_PHDR segment for |
4573 | the program headers and a PT_INTERP segment for the .interp | |
4574 | section. */ | |
4575 | s = bfd_get_section_by_name (abfd, ".interp"); | |
4576 | if (s != NULL && (s->flags & SEC_LOAD) != 0) | |
4577 | { | |
4578 | amt = sizeof (struct elf_segment_map); | |
a50b1753 | 4579 | m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); |
8ded5a0f AM |
4580 | if (m == NULL) |
4581 | goto error_return; | |
4582 | m->next = NULL; | |
4583 | m->p_type = PT_PHDR; | |
4584 | /* FIXME: UnixWare and Solaris set PF_X, Irix 5 does not. */ | |
4585 | m->p_flags = PF_R | PF_X; | |
4586 | m->p_flags_valid = 1; | |
4587 | m->includes_phdrs = 1; | |
1a9ccd70 | 4588 | linker_created_pt_phdr_segment = TRUE; |
8ded5a0f AM |
4589 | *pm = m; |
4590 | pm = &m->next; | |
252b5132 | 4591 | |
8ded5a0f | 4592 | amt = sizeof (struct elf_segment_map); |
a50b1753 | 4593 | m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); |
8ded5a0f AM |
4594 | if (m == NULL) |
4595 | goto error_return; | |
4596 | m->next = NULL; | |
4597 | m->p_type = PT_INTERP; | |
4598 | m->count = 1; | |
4599 | m->sections[0] = s; | |
4600 | ||
4601 | *pm = m; | |
4602 | pm = &m->next; | |
252b5132 | 4603 | } |
8ded5a0f AM |
4604 | |
4605 | /* Look through the sections. We put sections in the same program | |
4606 | segment when the start of the second section can be placed within | |
4607 | a few bytes of the end of the first section. */ | |
4608 | last_hdr = NULL; | |
4609 | last_size = 0; | |
4610 | phdr_index = 0; | |
4611 | maxpagesize = bed->maxpagesize; | |
063bb025 NC |
4612 | /* PR 17512: file: c8455299. |
4613 | Avoid divide-by-zero errors later on. | |
4614 | FIXME: Should we abort if the maxpagesize is zero ? */ | |
4615 | if (maxpagesize == 0) | |
4616 | maxpagesize = 1; | |
8ded5a0f AM |
4617 | writable = FALSE; |
4618 | dynsec = bfd_get_section_by_name (abfd, ".dynamic"); | |
4619 | if (dynsec != NULL | |
4620 | && (dynsec->flags & SEC_LOAD) == 0) | |
4621 | dynsec = NULL; | |
4622 | ||
4623 | /* Deal with -Ttext or something similar such that the first section | |
4624 | is not adjacent to the program headers. This is an | |
4625 | approximation, since at this point we don't know exactly how many | |
4626 | program headers we will need. */ | |
4627 | if (count > 0) | |
252b5132 | 4628 | { |
12bd6957 | 4629 | bfd_size_type phdr_size = elf_program_header_size (abfd); |
8ded5a0f | 4630 | |
62d7a5f6 | 4631 | if (phdr_size == (bfd_size_type) -1) |
8ded5a0f | 4632 | phdr_size = get_program_header_size (abfd, info); |
d2bcb0d1 | 4633 | phdr_size += bed->s->sizeof_ehdr; |
8ded5a0f | 4634 | if ((abfd->flags & D_PAGED) == 0 |
8d06853e AM |
4635 | || (sections[0]->lma & addr_mask) < phdr_size |
4636 | || ((sections[0]->lma & addr_mask) % maxpagesize | |
4637 | < phdr_size % maxpagesize) | |
4638 | || (sections[0]->lma & addr_mask & -maxpagesize) < wrap_to) | |
1a9ccd70 NC |
4639 | { |
4640 | /* PR 20815: The ELF standard says that a PT_PHDR segment, if | |
4641 | present, must be included as part of the memory image of the | |
4642 | program. Ie it must be part of a PT_LOAD segment as well. | |
4643 | If we have had to create our own PT_PHDR segment, but it is | |
4644 | not going to be covered by the first PT_LOAD segment, then | |
4645 | force the inclusion if we can... */ | |
4646 | if ((abfd->flags & D_PAGED) != 0 | |
4647 | && linker_created_pt_phdr_segment) | |
4648 | phdr_in_segment = TRUE; | |
4649 | else | |
4650 | phdr_in_segment = FALSE; | |
4651 | } | |
252b5132 RH |
4652 | } |
4653 | ||
8ded5a0f | 4654 | for (i = 0, hdrpp = sections; i < count; i++, hdrpp++) |
252b5132 | 4655 | { |
8ded5a0f AM |
4656 | asection *hdr; |
4657 | bfd_boolean new_segment; | |
4658 | ||
4659 | hdr = *hdrpp; | |
4660 | ||
4661 | /* See if this section and the last one will fit in the same | |
4662 | segment. */ | |
4663 | ||
4664 | if (last_hdr == NULL) | |
4665 | { | |
4666 | /* If we don't have a segment yet, then we don't need a new | |
4667 | one (we build the last one after this loop). */ | |
4668 | new_segment = FALSE; | |
4669 | } | |
4670 | else if (last_hdr->lma - last_hdr->vma != hdr->lma - hdr->vma) | |
4671 | { | |
4672 | /* If this section has a different relation between the | |
4673 | virtual address and the load address, then we need a new | |
4674 | segment. */ | |
4675 | new_segment = TRUE; | |
4676 | } | |
b5599592 AM |
4677 | else if (hdr->lma < last_hdr->lma + last_size |
4678 | || last_hdr->lma + last_size < last_hdr->lma) | |
4679 | { | |
4680 | /* If this section has a load address that makes it overlap | |
4681 | the previous section, then we need a new segment. */ | |
4682 | new_segment = TRUE; | |
4683 | } | |
39948a60 NC |
4684 | /* In the next test we have to be careful when last_hdr->lma is close |
4685 | to the end of the address space. If the aligned address wraps | |
4686 | around to the start of the address space, then there are no more | |
4687 | pages left in memory and it is OK to assume that the current | |
4688 | section can be included in the current segment. */ | |
4689 | else if ((BFD_ALIGN (last_hdr->lma + last_size, maxpagesize) + maxpagesize | |
4690 | > last_hdr->lma) | |
4691 | && (BFD_ALIGN (last_hdr->lma + last_size, maxpagesize) + maxpagesize | |
4ff73856 | 4692 | <= hdr->lma)) |
8ded5a0f AM |
4693 | { |
4694 | /* If putting this section in this segment would force us to | |
4695 | skip a page in the segment, then we need a new segment. */ | |
4696 | new_segment = TRUE; | |
4697 | } | |
4698 | else if ((last_hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0 | |
e5654c0f AM |
4699 | && (hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) != 0 |
4700 | && ((abfd->flags & D_PAGED) == 0 | |
4701 | || (((last_hdr->lma + last_size - 1) & -maxpagesize) | |
4702 | != (hdr->lma & -maxpagesize)))) | |
8ded5a0f | 4703 | { |
e5654c0f AM |
4704 | /* We don't want to put a loaded section after a |
4705 | nonloaded (ie. bss style) section in the same segment | |
4706 | as that will force the non-loaded section to be loaded. | |
4707 | Consider .tbss sections as loaded for this purpose. | |
4708 | However, like the writable/non-writable case below, | |
4709 | if they are on the same page then they must be put | |
4710 | in the same segment. */ | |
8ded5a0f AM |
4711 | new_segment = TRUE; |
4712 | } | |
4713 | else if ((abfd->flags & D_PAGED) == 0) | |
4714 | { | |
4715 | /* If the file is not demand paged, which means that we | |
4716 | don't require the sections to be correctly aligned in the | |
4717 | file, then there is no other reason for a new segment. */ | |
4718 | new_segment = FALSE; | |
4719 | } | |
4720 | else if (! writable | |
4721 | && (hdr->flags & SEC_READONLY) == 0 | |
8d06853e AM |
4722 | && (((last_hdr->lma + last_size - 1) & -maxpagesize) |
4723 | != (hdr->lma & -maxpagesize))) | |
8ded5a0f AM |
4724 | { |
4725 | /* We don't want to put a writable section in a read only | |
4726 | segment, unless they are on the same page in memory | |
4727 | anyhow. We already know that the last section does not | |
4728 | bring us past the current section on the page, so the | |
4729 | only case in which the new section is not on the same | |
4730 | page as the previous section is when the previous section | |
4731 | ends precisely on a page boundary. */ | |
4732 | new_segment = TRUE; | |
4733 | } | |
4734 | else | |
4735 | { | |
4736 | /* Otherwise, we can use the same segment. */ | |
4737 | new_segment = FALSE; | |
4738 | } | |
4739 | ||
2889e75b | 4740 | /* Allow interested parties a chance to override our decision. */ |
ceae84aa AM |
4741 | if (last_hdr != NULL |
4742 | && info != NULL | |
4743 | && info->callbacks->override_segment_assignment != NULL) | |
4744 | new_segment | |
4745 | = info->callbacks->override_segment_assignment (info, abfd, hdr, | |
4746 | last_hdr, | |
4747 | new_segment); | |
2889e75b | 4748 | |
8ded5a0f AM |
4749 | if (! new_segment) |
4750 | { | |
4751 | if ((hdr->flags & SEC_READONLY) == 0) | |
4752 | writable = TRUE; | |
4753 | last_hdr = hdr; | |
4754 | /* .tbss sections effectively have zero size. */ | |
4755 | if ((hdr->flags & (SEC_THREAD_LOCAL | SEC_LOAD)) | |
4756 | != SEC_THREAD_LOCAL) | |
4757 | last_size = hdr->size; | |
4758 | else | |
4759 | last_size = 0; | |
4760 | continue; | |
4761 | } | |
4762 | ||
4763 | /* We need a new program segment. We must create a new program | |
4764 | header holding all the sections from phdr_index until hdr. */ | |
4765 | ||
4766 | m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment); | |
4767 | if (m == NULL) | |
4768 | goto error_return; | |
4769 | ||
4770 | *pm = m; | |
4771 | pm = &m->next; | |
4772 | ||
252b5132 | 4773 | if ((hdr->flags & SEC_READONLY) == 0) |
b34976b6 | 4774 | writable = TRUE; |
8ded5a0f AM |
4775 | else |
4776 | writable = FALSE; | |
4777 | ||
baaff79e JJ |
4778 | last_hdr = hdr; |
4779 | /* .tbss sections effectively have zero size. */ | |
e5caec89 | 4780 | if ((hdr->flags & (SEC_THREAD_LOCAL | SEC_LOAD)) != SEC_THREAD_LOCAL) |
eea6121a | 4781 | last_size = hdr->size; |
baaff79e JJ |
4782 | else |
4783 | last_size = 0; | |
8ded5a0f AM |
4784 | phdr_index = i; |
4785 | phdr_in_segment = FALSE; | |
252b5132 RH |
4786 | } |
4787 | ||
86b2281f AM |
4788 | /* Create a final PT_LOAD program segment, but not if it's just |
4789 | for .tbss. */ | |
4790 | if (last_hdr != NULL | |
4791 | && (i - phdr_index != 1 | |
4792 | || ((last_hdr->flags & (SEC_THREAD_LOCAL | SEC_LOAD)) | |
4793 | != SEC_THREAD_LOCAL))) | |
8ded5a0f AM |
4794 | { |
4795 | m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment); | |
4796 | if (m == NULL) | |
4797 | goto error_return; | |
252b5132 | 4798 | |
8ded5a0f AM |
4799 | *pm = m; |
4800 | pm = &m->next; | |
4801 | } | |
252b5132 | 4802 | |
8ded5a0f AM |
4803 | /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */ |
4804 | if (dynsec != NULL) | |
4805 | { | |
4806 | m = _bfd_elf_make_dynamic_segment (abfd, dynsec); | |
4807 | if (m == NULL) | |
4808 | goto error_return; | |
4809 | *pm = m; | |
4810 | pm = &m->next; | |
4811 | } | |
252b5132 | 4812 | |
1c5265b5 JJ |
4813 | /* For each batch of consecutive loadable .note sections, |
4814 | add a PT_NOTE segment. We don't use bfd_get_section_by_name, | |
4815 | because if we link together nonloadable .note sections and | |
4816 | loadable .note sections, we will generate two .note sections | |
4817 | in the output file. FIXME: Using names for section types is | |
4818 | bogus anyhow. */ | |
8ded5a0f AM |
4819 | for (s = abfd->sections; s != NULL; s = s->next) |
4820 | { | |
4821 | if ((s->flags & SEC_LOAD) != 0 | |
0112cd26 | 4822 | && CONST_STRNEQ (s->name, ".note")) |
8ded5a0f | 4823 | { |
1c5265b5 | 4824 | asection *s2; |
91d6fa6a NC |
4825 | |
4826 | count = 1; | |
8ded5a0f | 4827 | amt = sizeof (struct elf_segment_map); |
1c5265b5 JJ |
4828 | if (s->alignment_power == 2) |
4829 | for (s2 = s; s2->next != NULL; s2 = s2->next) | |
55b581a6 JJ |
4830 | { |
4831 | if (s2->next->alignment_power == 2 | |
4832 | && (s2->next->flags & SEC_LOAD) != 0 | |
4833 | && CONST_STRNEQ (s2->next->name, ".note") | |
8d06853e AM |
4834 | && align_power (s2->lma + s2->size, 2) |
4835 | == s2->next->lma) | |
55b581a6 JJ |
4836 | count++; |
4837 | else | |
4838 | break; | |
4839 | } | |
1c5265b5 | 4840 | amt += (count - 1) * sizeof (asection *); |
a50b1753 | 4841 | m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); |
8ded5a0f AM |
4842 | if (m == NULL) |
4843 | goto error_return; | |
4844 | m->next = NULL; | |
4845 | m->p_type = PT_NOTE; | |
1c5265b5 JJ |
4846 | m->count = count; |
4847 | while (count > 1) | |
4848 | { | |
4849 | m->sections[m->count - count--] = s; | |
4850 | BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0); | |
4851 | s = s->next; | |
4852 | } | |
4853 | m->sections[m->count - 1] = s; | |
4854 | BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0); | |
8ded5a0f AM |
4855 | *pm = m; |
4856 | pm = &m->next; | |
4857 | } | |
4858 | if (s->flags & SEC_THREAD_LOCAL) | |
4859 | { | |
4860 | if (! tls_count) | |
4861 | first_tls = s; | |
4862 | tls_count++; | |
4863 | } | |
a91e1603 L |
4864 | if (first_mbind == NULL |
4865 | && (elf_section_flags (s) & SHF_GNU_MBIND) != 0) | |
4866 | first_mbind = s; | |
8ded5a0f | 4867 | } |
252b5132 | 4868 | |
8ded5a0f AM |
4869 | /* If there are any SHF_TLS output sections, add PT_TLS segment. */ |
4870 | if (tls_count > 0) | |
4871 | { | |
8ded5a0f AM |
4872 | amt = sizeof (struct elf_segment_map); |
4873 | amt += (tls_count - 1) * sizeof (asection *); | |
a50b1753 | 4874 | m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); |
8ded5a0f AM |
4875 | if (m == NULL) |
4876 | goto error_return; | |
4877 | m->next = NULL; | |
4878 | m->p_type = PT_TLS; | |
4879 | m->count = tls_count; | |
4880 | /* Mandated PF_R. */ | |
4881 | m->p_flags = PF_R; | |
4882 | m->p_flags_valid = 1; | |
d923cae0 | 4883 | s = first_tls; |
91d6fa6a | 4884 | for (i = 0; i < (unsigned int) tls_count; ++i) |
8ded5a0f | 4885 | { |
d923cae0 L |
4886 | if ((s->flags & SEC_THREAD_LOCAL) == 0) |
4887 | { | |
4888 | _bfd_error_handler | |
4889 | (_("%B: TLS sections are not adjacent:"), abfd); | |
4890 | s = first_tls; | |
4891 | i = 0; | |
4892 | while (i < (unsigned int) tls_count) | |
4893 | { | |
4894 | if ((s->flags & SEC_THREAD_LOCAL) != 0) | |
4895 | { | |
4896 | _bfd_error_handler (_(" TLS: %A"), s); | |
4897 | i++; | |
4898 | } | |
4899 | else | |
4900 | _bfd_error_handler (_(" non-TLS: %A"), s); | |
4901 | s = s->next; | |
4902 | } | |
4903 | bfd_set_error (bfd_error_bad_value); | |
4904 | goto error_return; | |
4905 | } | |
4906 | m->sections[i] = s; | |
4907 | s = s->next; | |
8ded5a0f | 4908 | } |
252b5132 | 4909 | |
8ded5a0f AM |
4910 | *pm = m; |
4911 | pm = &m->next; | |
4912 | } | |
252b5132 | 4913 | |
a91e1603 L |
4914 | if (first_mbind && (abfd->flags & D_PAGED) != 0) |
4915 | for (s = first_mbind; s != NULL; s = s->next) | |
4916 | if ((elf_section_flags (s) & SHF_GNU_MBIND) != 0 | |
4917 | && (elf_section_data (s)->this_hdr.sh_info | |
4918 | <= PT_GNU_MBIND_NUM)) | |
4919 | { | |
4920 | /* Mandated PF_R. */ | |
4921 | unsigned long p_flags = PF_R; | |
4922 | if ((s->flags & SEC_READONLY) == 0) | |
4923 | p_flags |= PF_W; | |
4924 | if ((s->flags & SEC_CODE) != 0) | |
4925 | p_flags |= PF_X; | |
4926 | ||
4927 | amt = sizeof (struct elf_segment_map) + sizeof (asection *); | |
4928 | m = bfd_zalloc (abfd, amt); | |
4929 | if (m == NULL) | |
4930 | goto error_return; | |
4931 | m->next = NULL; | |
4932 | m->p_type = (PT_GNU_MBIND_LO | |
4933 | + elf_section_data (s)->this_hdr.sh_info); | |
4934 | m->count = 1; | |
4935 | m->p_flags_valid = 1; | |
4936 | m->sections[0] = s; | |
4937 | m->p_flags = p_flags; | |
4938 | ||
4939 | *pm = m; | |
4940 | pm = &m->next; | |
4941 | } | |
4942 | ||
8ded5a0f AM |
4943 | /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME |
4944 | segment. */ | |
12bd6957 | 4945 | eh_frame_hdr = elf_eh_frame_hdr (abfd); |
8ded5a0f AM |
4946 | if (eh_frame_hdr != NULL |
4947 | && (eh_frame_hdr->output_section->flags & SEC_LOAD) != 0) | |
252b5132 | 4948 | { |
dc810e39 | 4949 | amt = sizeof (struct elf_segment_map); |
a50b1753 | 4950 | m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); |
252b5132 RH |
4951 | if (m == NULL) |
4952 | goto error_return; | |
4953 | m->next = NULL; | |
8ded5a0f | 4954 | m->p_type = PT_GNU_EH_FRAME; |
252b5132 | 4955 | m->count = 1; |
8ded5a0f | 4956 | m->sections[0] = eh_frame_hdr->output_section; |
252b5132 RH |
4957 | |
4958 | *pm = m; | |
4959 | pm = &m->next; | |
4960 | } | |
13ae64f3 | 4961 | |
12bd6957 | 4962 | if (elf_stack_flags (abfd)) |
13ae64f3 | 4963 | { |
8ded5a0f | 4964 | amt = sizeof (struct elf_segment_map); |
a50b1753 | 4965 | m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); |
8ded5a0f AM |
4966 | if (m == NULL) |
4967 | goto error_return; | |
4968 | m->next = NULL; | |
2b05f1b7 | 4969 | m->p_type = PT_GNU_STACK; |
12bd6957 | 4970 | m->p_flags = elf_stack_flags (abfd); |
04c3a755 | 4971 | m->p_align = bed->stack_align; |
8ded5a0f | 4972 | m->p_flags_valid = 1; |
04c3a755 NS |
4973 | m->p_align_valid = m->p_align != 0; |
4974 | if (info->stacksize > 0) | |
4975 | { | |
4976 | m->p_size = info->stacksize; | |
4977 | m->p_size_valid = 1; | |
4978 | } | |
252b5132 | 4979 | |
8ded5a0f AM |
4980 | *pm = m; |
4981 | pm = &m->next; | |
4982 | } | |
65765700 | 4983 | |
ceae84aa | 4984 | if (info != NULL && info->relro) |
8ded5a0f | 4985 | { |
f210dcff L |
4986 | for (m = mfirst; m != NULL; m = m->next) |
4987 | { | |
3832a4d8 AM |
4988 | if (m->p_type == PT_LOAD |
4989 | && m->count != 0 | |
4990 | && m->sections[0]->vma >= info->relro_start | |
4991 | && m->sections[0]->vma < info->relro_end) | |
f210dcff | 4992 | { |
3832a4d8 AM |
4993 | i = m->count; |
4994 | while (--i != (unsigned) -1) | |
4995 | if ((m->sections[i]->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) | |
4996 | == (SEC_LOAD | SEC_HAS_CONTENTS)) | |
4997 | break; | |
4998 | ||
43a8475c | 4999 | if (i != (unsigned) -1) |
f210dcff L |
5000 | break; |
5001 | } | |
be01b344 | 5002 | } |
f210dcff L |
5003 | |
5004 | /* Make a PT_GNU_RELRO segment only when it isn't empty. */ | |
5005 | if (m != NULL) | |
5006 | { | |
5007 | amt = sizeof (struct elf_segment_map); | |
a50b1753 | 5008 | m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); |
f210dcff L |
5009 | if (m == NULL) |
5010 | goto error_return; | |
5011 | m->next = NULL; | |
5012 | m->p_type = PT_GNU_RELRO; | |
f210dcff L |
5013 | *pm = m; |
5014 | pm = &m->next; | |
5015 | } | |
8ded5a0f | 5016 | } |
9ee5e499 | 5017 | |
8ded5a0f | 5018 | free (sections); |
12bd6957 | 5019 | elf_seg_map (abfd) = mfirst; |
9ee5e499 JJ |
5020 | } |
5021 | ||
3dea8fca | 5022 | if (!elf_modify_segment_map (abfd, info, no_user_phdrs)) |
8ded5a0f | 5023 | return FALSE; |
8c37241b | 5024 | |
12bd6957 | 5025 | for (count = 0, m = elf_seg_map (abfd); m != NULL; m = m->next) |
8ded5a0f | 5026 | ++count; |
12bd6957 | 5027 | elf_program_header_size (abfd) = count * bed->s->sizeof_phdr; |
252b5132 | 5028 | |
b34976b6 | 5029 | return TRUE; |
252b5132 RH |
5030 | |
5031 | error_return: | |
5032 | if (sections != NULL) | |
5033 | free (sections); | |
b34976b6 | 5034 | return FALSE; |
252b5132 RH |
5035 | } |
5036 | ||
5037 | /* Sort sections by address. */ | |
5038 | ||
5039 | static int | |
217aa764 | 5040 | elf_sort_sections (const void *arg1, const void *arg2) |
252b5132 RH |
5041 | { |
5042 | const asection *sec1 = *(const asection **) arg1; | |
5043 | const asection *sec2 = *(const asection **) arg2; | |
eecdbe52 | 5044 | bfd_size_type size1, size2; |
252b5132 RH |
5045 | |
5046 | /* Sort by LMA first, since this is the address used to | |
5047 | place the section into a segment. */ | |
5048 | if (sec1->lma < sec2->lma) | |
5049 | return -1; | |
5050 | else if (sec1->lma > sec2->lma) | |
5051 | return 1; | |
5052 | ||
5053 | /* Then sort by VMA. Normally the LMA and the VMA will be | |
5054 | the same, and this will do nothing. */ | |
5055 | if (sec1->vma < sec2->vma) | |
5056 | return -1; | |
5057 | else if (sec1->vma > sec2->vma) | |
5058 | return 1; | |
5059 | ||
5060 | /* Put !SEC_LOAD sections after SEC_LOAD ones. */ | |
5061 | ||
07c6e936 | 5062 | #define TOEND(x) (((x)->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0) |
252b5132 RH |
5063 | |
5064 | if (TOEND (sec1)) | |
5065 | { | |
5066 | if (TOEND (sec2)) | |
00a7cdc5 NC |
5067 | { |
5068 | /* If the indicies are the same, do not return 0 | |
5069 | here, but continue to try the next comparison. */ | |
5070 | if (sec1->target_index - sec2->target_index != 0) | |
5071 | return sec1->target_index - sec2->target_index; | |
5072 | } | |
252b5132 RH |
5073 | else |
5074 | return 1; | |
5075 | } | |
00a7cdc5 | 5076 | else if (TOEND (sec2)) |
252b5132 RH |
5077 | return -1; |
5078 | ||
5079 | #undef TOEND | |
5080 | ||
00a7cdc5 NC |
5081 | /* Sort by size, to put zero sized sections |
5082 | before others at the same address. */ | |
252b5132 | 5083 | |
eea6121a AM |
5084 | size1 = (sec1->flags & SEC_LOAD) ? sec1->size : 0; |
5085 | size2 = (sec2->flags & SEC_LOAD) ? sec2->size : 0; | |
eecdbe52 JJ |
5086 | |
5087 | if (size1 < size2) | |
252b5132 | 5088 | return -1; |
eecdbe52 | 5089 | if (size1 > size2) |
252b5132 RH |
5090 | return 1; |
5091 | ||
5092 | return sec1->target_index - sec2->target_index; | |
5093 | } | |
5094 | ||
340b6d91 AC |
5095 | /* Ian Lance Taylor writes: |
5096 | ||
5097 | We shouldn't be using % with a negative signed number. That's just | |
5098 | not good. We have to make sure either that the number is not | |
5099 | negative, or that the number has an unsigned type. When the types | |
5100 | are all the same size they wind up as unsigned. When file_ptr is a | |
5101 | larger signed type, the arithmetic winds up as signed long long, | |
5102 | which is wrong. | |
5103 | ||
5104 | What we're trying to say here is something like ``increase OFF by | |
5105 | the least amount that will cause it to be equal to the VMA modulo | |
5106 | the page size.'' */ | |
5107 | /* In other words, something like: | |
5108 | ||
5109 | vma_offset = m->sections[0]->vma % bed->maxpagesize; | |
5110 | off_offset = off % bed->maxpagesize; | |
5111 | if (vma_offset < off_offset) | |
5112 | adjustment = vma_offset + bed->maxpagesize - off_offset; | |
5113 | else | |
5114 | adjustment = vma_offset - off_offset; | |
08a40648 | 5115 | |
340b6d91 AC |
5116 | which can can be collapsed into the expression below. */ |
5117 | ||
5118 | static file_ptr | |
5119 | vma_page_aligned_bias (bfd_vma vma, ufile_ptr off, bfd_vma maxpagesize) | |
5120 | { | |
dc9155b2 NC |
5121 | /* PR binutils/16199: Handle an alignment of zero. */ |
5122 | if (maxpagesize == 0) | |
5123 | maxpagesize = 1; | |
340b6d91 AC |
5124 | return ((vma - off) % maxpagesize); |
5125 | } | |
5126 | ||
6d33f217 L |
5127 | static void |
5128 | print_segment_map (const struct elf_segment_map *m) | |
5129 | { | |
5130 | unsigned int j; | |
5131 | const char *pt = get_segment_type (m->p_type); | |
5132 | char buf[32]; | |
5133 | ||
5134 | if (pt == NULL) | |
5135 | { | |
5136 | if (m->p_type >= PT_LOPROC && m->p_type <= PT_HIPROC) | |
5137 | sprintf (buf, "LOPROC+%7.7x", | |
5138 | (unsigned int) (m->p_type - PT_LOPROC)); | |
5139 | else if (m->p_type >= PT_LOOS && m->p_type <= PT_HIOS) | |
5140 | sprintf (buf, "LOOS+%7.7x", | |
5141 | (unsigned int) (m->p_type - PT_LOOS)); | |
5142 | else | |
5143 | snprintf (buf, sizeof (buf), "%8.8x", | |
5144 | (unsigned int) m->p_type); | |
5145 | pt = buf; | |
5146 | } | |
4a97a0e5 | 5147 | fflush (stdout); |
6d33f217 L |
5148 | fprintf (stderr, "%s:", pt); |
5149 | for (j = 0; j < m->count; j++) | |
5150 | fprintf (stderr, " %s", m->sections [j]->name); | |
5151 | putc ('\n',stderr); | |
4a97a0e5 | 5152 | fflush (stderr); |
6d33f217 L |
5153 | } |
5154 | ||
32812159 AM |
5155 | static bfd_boolean |
5156 | write_zeros (bfd *abfd, file_ptr pos, bfd_size_type len) | |
5157 | { | |
5158 | void *buf; | |
5159 | bfd_boolean ret; | |
5160 | ||
5161 | if (bfd_seek (abfd, pos, SEEK_SET) != 0) | |
5162 | return FALSE; | |
5163 | buf = bfd_zmalloc (len); | |
5164 | if (buf == NULL) | |
5165 | return FALSE; | |
5166 | ret = bfd_bwrite (buf, len, abfd) == len; | |
5167 | free (buf); | |
5168 | return ret; | |
5169 | } | |
5170 | ||
252b5132 RH |
5171 | /* Assign file positions to the sections based on the mapping from |
5172 | sections to segments. This function also sets up some fields in | |
f3520d2f | 5173 | the file header. */ |
252b5132 | 5174 | |
b34976b6 | 5175 | static bfd_boolean |
f3520d2f AM |
5176 | assign_file_positions_for_load_sections (bfd *abfd, |
5177 | struct bfd_link_info *link_info) | |
252b5132 RH |
5178 | { |
5179 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
252b5132 | 5180 | struct elf_segment_map *m; |
252b5132 | 5181 | Elf_Internal_Phdr *phdrs; |
252b5132 | 5182 | Elf_Internal_Phdr *p; |
02bf8d82 | 5183 | file_ptr off; |
3f570048 | 5184 | bfd_size_type maxpagesize; |
a8c75b76 | 5185 | unsigned int pt_load_count = 0; |
f3520d2f | 5186 | unsigned int alloc; |
0920dee7 | 5187 | unsigned int i, j; |
2b0bc088 | 5188 | bfd_vma header_pad = 0; |
252b5132 | 5189 | |
e36284ab | 5190 | if (link_info == NULL |
ceae84aa | 5191 | && !_bfd_elf_map_sections_to_segments (abfd, link_info)) |
8ded5a0f | 5192 | return FALSE; |
252b5132 | 5193 | |
8ded5a0f | 5194 | alloc = 0; |
12bd6957 | 5195 | for (m = elf_seg_map (abfd); m != NULL; m = m->next) |
2b0bc088 NC |
5196 | { |
5197 | ++alloc; | |
5198 | if (m->header_size) | |
5199 | header_pad = m->header_size; | |
5200 | } | |
252b5132 | 5201 | |
82f2dbf7 NC |
5202 | if (alloc) |
5203 | { | |
5204 | elf_elfheader (abfd)->e_phoff = bed->s->sizeof_ehdr; | |
5205 | elf_elfheader (abfd)->e_phentsize = bed->s->sizeof_phdr; | |
5206 | } | |
5207 | else | |
5208 | { | |
5209 | /* PR binutils/12467. */ | |
5210 | elf_elfheader (abfd)->e_phoff = 0; | |
5211 | elf_elfheader (abfd)->e_phentsize = 0; | |
5212 | } | |
d324f6d6 | 5213 | |
8ded5a0f | 5214 | elf_elfheader (abfd)->e_phnum = alloc; |
252b5132 | 5215 | |
12bd6957 AM |
5216 | if (elf_program_header_size (abfd) == (bfd_size_type) -1) |
5217 | elf_program_header_size (abfd) = alloc * bed->s->sizeof_phdr; | |
8ded5a0f | 5218 | else |
12bd6957 | 5219 | BFD_ASSERT (elf_program_header_size (abfd) |
59e0647f | 5220 | >= alloc * bed->s->sizeof_phdr); |
252b5132 RH |
5221 | |
5222 | if (alloc == 0) | |
f3520d2f | 5223 | { |
12bd6957 | 5224 | elf_next_file_pos (abfd) = bed->s->sizeof_ehdr; |
8ded5a0f | 5225 | return TRUE; |
f3520d2f | 5226 | } |
252b5132 | 5227 | |
12bd6957 | 5228 | /* We're writing the size in elf_program_header_size (abfd), |
57268894 HPN |
5229 | see assign_file_positions_except_relocs, so make sure we have |
5230 | that amount allocated, with trailing space cleared. | |
12bd6957 AM |
5231 | The variable alloc contains the computed need, while |
5232 | elf_program_header_size (abfd) contains the size used for the | |
57268894 HPN |
5233 | layout. |
5234 | See ld/emultempl/elf-generic.em:gld${EMULATION_NAME}_map_segments | |
5235 | where the layout is forced to according to a larger size in the | |
5236 | last iterations for the testcase ld-elf/header. */ | |
12bd6957 | 5237 | BFD_ASSERT (elf_program_header_size (abfd) % bed->s->sizeof_phdr |
57268894 | 5238 | == 0); |
a50b1753 NC |
5239 | phdrs = (Elf_Internal_Phdr *) |
5240 | bfd_zalloc2 (abfd, | |
12bd6957 | 5241 | (elf_program_header_size (abfd) / bed->s->sizeof_phdr), |
a50b1753 | 5242 | sizeof (Elf_Internal_Phdr)); |
f3520d2f | 5243 | elf_tdata (abfd)->phdr = phdrs; |
252b5132 | 5244 | if (phdrs == NULL) |
b34976b6 | 5245 | return FALSE; |
252b5132 | 5246 | |
3f570048 AM |
5247 | maxpagesize = 1; |
5248 | if ((abfd->flags & D_PAGED) != 0) | |
5249 | maxpagesize = bed->maxpagesize; | |
5250 | ||
252b5132 RH |
5251 | off = bed->s->sizeof_ehdr; |
5252 | off += alloc * bed->s->sizeof_phdr; | |
2b0bc088 NC |
5253 | if (header_pad < (bfd_vma) off) |
5254 | header_pad = 0; | |
5255 | else | |
5256 | header_pad -= off; | |
5257 | off += header_pad; | |
252b5132 | 5258 | |
12bd6957 | 5259 | for (m = elf_seg_map (abfd), p = phdrs, j = 0; |
252b5132 | 5260 | m != NULL; |
0920dee7 | 5261 | m = m->next, p++, j++) |
252b5132 | 5262 | { |
252b5132 | 5263 | asection **secpp; |
bf988460 AM |
5264 | bfd_vma off_adjust; |
5265 | bfd_boolean no_contents; | |
252b5132 RH |
5266 | |
5267 | /* If elf_segment_map is not from map_sections_to_segments, the | |
08a40648 | 5268 | sections may not be correctly ordered. NOTE: sorting should |
52e9b619 MS |
5269 | not be done to the PT_NOTE section of a corefile, which may |
5270 | contain several pseudo-sections artificially created by bfd. | |
5271 | Sorting these pseudo-sections breaks things badly. */ | |
47d9a591 AM |
5272 | if (m->count > 1 |
5273 | && !(elf_elfheader (abfd)->e_type == ET_CORE | |
52e9b619 | 5274 | && m->p_type == PT_NOTE)) |
252b5132 RH |
5275 | qsort (m->sections, (size_t) m->count, sizeof (asection *), |
5276 | elf_sort_sections); | |
5277 | ||
b301b248 AM |
5278 | /* An ELF segment (described by Elf_Internal_Phdr) may contain a |
5279 | number of sections with contents contributing to both p_filesz | |
5280 | and p_memsz, followed by a number of sections with no contents | |
5281 | that just contribute to p_memsz. In this loop, OFF tracks next | |
02bf8d82 | 5282 | available file offset for PT_LOAD and PT_NOTE segments. */ |
252b5132 | 5283 | p->p_type = m->p_type; |
28a7f3e7 | 5284 | p->p_flags = m->p_flags; |
252b5132 | 5285 | |
3f570048 AM |
5286 | if (m->count == 0) |
5287 | p->p_vaddr = 0; | |
5288 | else | |
3271a814 | 5289 | p->p_vaddr = m->sections[0]->vma - m->p_vaddr_offset; |
3f570048 AM |
5290 | |
5291 | if (m->p_paddr_valid) | |
5292 | p->p_paddr = m->p_paddr; | |
5293 | else if (m->count == 0) | |
5294 | p->p_paddr = 0; | |
5295 | else | |
08a40648 | 5296 | p->p_paddr = m->sections[0]->lma - m->p_vaddr_offset; |
3f570048 AM |
5297 | |
5298 | if (p->p_type == PT_LOAD | |
5299 | && (abfd->flags & D_PAGED) != 0) | |
5300 | { | |
5301 | /* p_align in demand paged PT_LOAD segments effectively stores | |
5302 | the maximum page size. When copying an executable with | |
5303 | objcopy, we set m->p_align from the input file. Use this | |
5304 | value for maxpagesize rather than bed->maxpagesize, which | |
5305 | may be different. Note that we use maxpagesize for PT_TLS | |
5306 | segment alignment later in this function, so we are relying | |
5307 | on at least one PT_LOAD segment appearing before a PT_TLS | |
5308 | segment. */ | |
5309 | if (m->p_align_valid) | |
5310 | maxpagesize = m->p_align; | |
5311 | ||
5312 | p->p_align = maxpagesize; | |
a8c75b76 | 5313 | pt_load_count += 1; |
3f570048 | 5314 | } |
3271a814 NS |
5315 | else if (m->p_align_valid) |
5316 | p->p_align = m->p_align; | |
e970b90a DJ |
5317 | else if (m->count == 0) |
5318 | p->p_align = 1 << bed->s->log_file_align; | |
3f570048 AM |
5319 | else |
5320 | p->p_align = 0; | |
5321 | ||
bf988460 AM |
5322 | no_contents = FALSE; |
5323 | off_adjust = 0; | |
252b5132 | 5324 | if (p->p_type == PT_LOAD |
b301b248 | 5325 | && m->count > 0) |
252b5132 | 5326 | { |
b301b248 | 5327 | bfd_size_type align; |
a49e53ed | 5328 | unsigned int align_power = 0; |
b301b248 | 5329 | |
3271a814 NS |
5330 | if (m->p_align_valid) |
5331 | align = p->p_align; | |
5332 | else | |
252b5132 | 5333 | { |
3271a814 NS |
5334 | for (i = 0, secpp = m->sections; i < m->count; i++, secpp++) |
5335 | { | |
5336 | unsigned int secalign; | |
08a40648 | 5337 | |
3271a814 NS |
5338 | secalign = bfd_get_section_alignment (abfd, *secpp); |
5339 | if (secalign > align_power) | |
5340 | align_power = secalign; | |
5341 | } | |
5342 | align = (bfd_size_type) 1 << align_power; | |
5343 | if (align < maxpagesize) | |
5344 | align = maxpagesize; | |
b301b248 | 5345 | } |
252b5132 | 5346 | |
02bf8d82 AM |
5347 | for (i = 0; i < m->count; i++) |
5348 | if ((m->sections[i]->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0) | |
5349 | /* If we aren't making room for this section, then | |
5350 | it must be SHT_NOBITS regardless of what we've | |
5351 | set via struct bfd_elf_special_section. */ | |
5352 | elf_section_type (m->sections[i]) = SHT_NOBITS; | |
5353 | ||
bf988460 | 5354 | /* Find out whether this segment contains any loadable |
aea274d3 AM |
5355 | sections. */ |
5356 | no_contents = TRUE; | |
5357 | for (i = 0; i < m->count; i++) | |
5358 | if (elf_section_type (m->sections[i]) != SHT_NOBITS) | |
5359 | { | |
5360 | no_contents = FALSE; | |
5361 | break; | |
5362 | } | |
bf988460 | 5363 | |
85cfcbfb | 5364 | off_adjust = vma_page_aligned_bias (p->p_vaddr, off, align); |
a8c75b76 AM |
5365 | |
5366 | /* Broken hardware and/or kernel require that files do not | |
5367 | map the same page with different permissions on some hppa | |
5368 | processors. */ | |
5369 | if (pt_load_count > 1 | |
5370 | && bed->no_page_alias | |
5371 | && (off & (maxpagesize - 1)) != 0 | |
5372 | && (off & -maxpagesize) == ((off + off_adjust) & -maxpagesize)) | |
5373 | off_adjust += maxpagesize; | |
bf988460 AM |
5374 | off += off_adjust; |
5375 | if (no_contents) | |
5376 | { | |
5377 | /* We shouldn't need to align the segment on disk since | |
5378 | the segment doesn't need file space, but the gABI | |
5379 | arguably requires the alignment and glibc ld.so | |
5380 | checks it. So to comply with the alignment | |
5381 | requirement but not waste file space, we adjust | |
5382 | p_offset for just this segment. (OFF_ADJUST is | |
5383 | subtracted from OFF later.) This may put p_offset | |
5384 | past the end of file, but that shouldn't matter. */ | |
5385 | } | |
5386 | else | |
5387 | off_adjust = 0; | |
252b5132 | 5388 | } |
b1a6d0b1 NC |
5389 | /* Make sure the .dynamic section is the first section in the |
5390 | PT_DYNAMIC segment. */ | |
5391 | else if (p->p_type == PT_DYNAMIC | |
5392 | && m->count > 1 | |
5393 | && strcmp (m->sections[0]->name, ".dynamic") != 0) | |
5394 | { | |
5395 | _bfd_error_handler | |
b301b248 AM |
5396 | (_("%B: The first section in the PT_DYNAMIC segment is not the .dynamic section"), |
5397 | abfd); | |
b1a6d0b1 NC |
5398 | bfd_set_error (bfd_error_bad_value); |
5399 | return FALSE; | |
5400 | } | |
3f001e84 JK |
5401 | /* Set the note section type to SHT_NOTE. */ |
5402 | else if (p->p_type == PT_NOTE) | |
5403 | for (i = 0; i < m->count; i++) | |
5404 | elf_section_type (m->sections[i]) = SHT_NOTE; | |
252b5132 | 5405 | |
252b5132 RH |
5406 | p->p_offset = 0; |
5407 | p->p_filesz = 0; | |
5408 | p->p_memsz = 0; | |
5409 | ||
5410 | if (m->includes_filehdr) | |
5411 | { | |
bf988460 | 5412 | if (!m->p_flags_valid) |
252b5132 | 5413 | p->p_flags |= PF_R; |
252b5132 RH |
5414 | p->p_filesz = bed->s->sizeof_ehdr; |
5415 | p->p_memsz = bed->s->sizeof_ehdr; | |
5416 | if (m->count > 0) | |
5417 | { | |
9ab82472 L |
5418 | if (p->p_vaddr < (bfd_vma) off |
5419 | || (!m->p_paddr_valid | |
5420 | && p->p_paddr < (bfd_vma) off)) | |
252b5132 | 5421 | { |
4eca0228 | 5422 | _bfd_error_handler |
b301b248 AM |
5423 | (_("%B: Not enough room for program headers, try linking with -N"), |
5424 | abfd); | |
252b5132 | 5425 | bfd_set_error (bfd_error_bad_value); |
b34976b6 | 5426 | return FALSE; |
252b5132 RH |
5427 | } |
5428 | ||
5429 | p->p_vaddr -= off; | |
bf988460 | 5430 | if (!m->p_paddr_valid) |
252b5132 RH |
5431 | p->p_paddr -= off; |
5432 | } | |
252b5132 RH |
5433 | } |
5434 | ||
5435 | if (m->includes_phdrs) | |
5436 | { | |
bf988460 | 5437 | if (!m->p_flags_valid) |
252b5132 RH |
5438 | p->p_flags |= PF_R; |
5439 | ||
f3520d2f | 5440 | if (!m->includes_filehdr) |
252b5132 RH |
5441 | { |
5442 | p->p_offset = bed->s->sizeof_ehdr; | |
5443 | ||
5444 | if (m->count > 0) | |
5445 | { | |
252b5132 | 5446 | p->p_vaddr -= off - p->p_offset; |
bf988460 | 5447 | if (!m->p_paddr_valid) |
252b5132 RH |
5448 | p->p_paddr -= off - p->p_offset; |
5449 | } | |
252b5132 RH |
5450 | } |
5451 | ||
5452 | p->p_filesz += alloc * bed->s->sizeof_phdr; | |
5453 | p->p_memsz += alloc * bed->s->sizeof_phdr; | |
2b0bc088 NC |
5454 | if (m->count) |
5455 | { | |
5456 | p->p_filesz += header_pad; | |
5457 | p->p_memsz += header_pad; | |
5458 | } | |
252b5132 RH |
5459 | } |
5460 | ||
5461 | if (p->p_type == PT_LOAD | |
5462 | || (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core)) | |
5463 | { | |
bf988460 | 5464 | if (!m->includes_filehdr && !m->includes_phdrs) |
02bf8d82 | 5465 | p->p_offset = off; |
252b5132 RH |
5466 | else |
5467 | { | |
5468 | file_ptr adjust; | |
5469 | ||
5470 | adjust = off - (p->p_offset + p->p_filesz); | |
bf988460 AM |
5471 | if (!no_contents) |
5472 | p->p_filesz += adjust; | |
252b5132 RH |
5473 | p->p_memsz += adjust; |
5474 | } | |
5475 | } | |
5476 | ||
1ea63fd2 AM |
5477 | /* Set up p_filesz, p_memsz, p_align and p_flags from the section |
5478 | maps. Set filepos for sections in PT_LOAD segments, and in | |
5479 | core files, for sections in PT_NOTE segments. | |
5480 | assign_file_positions_for_non_load_sections will set filepos | |
5481 | for other sections and update p_filesz for other segments. */ | |
252b5132 RH |
5482 | for (i = 0, secpp = m->sections; i < m->count; i++, secpp++) |
5483 | { | |
5484 | asection *sec; | |
252b5132 | 5485 | bfd_size_type align; |
627b32bc | 5486 | Elf_Internal_Shdr *this_hdr; |
252b5132 RH |
5487 | |
5488 | sec = *secpp; | |
02bf8d82 | 5489 | this_hdr = &elf_section_data (sec)->this_hdr; |
3f570048 | 5490 | align = (bfd_size_type) 1 << bfd_get_section_alignment (abfd, sec); |
252b5132 | 5491 | |
88967714 AM |
5492 | if ((p->p_type == PT_LOAD |
5493 | || p->p_type == PT_TLS) | |
5494 | && (this_hdr->sh_type != SHT_NOBITS | |
5495 | || ((this_hdr->sh_flags & SHF_ALLOC) != 0 | |
5496 | && ((this_hdr->sh_flags & SHF_TLS) == 0 | |
5497 | || p->p_type == PT_TLS)))) | |
252b5132 | 5498 | { |
b5599592 AM |
5499 | bfd_vma p_start = p->p_paddr; |
5500 | bfd_vma p_end = p_start + p->p_memsz; | |
5501 | bfd_vma s_start = sec->lma; | |
5502 | bfd_vma adjust = s_start - p_end; | |
252b5132 | 5503 | |
a2d1e028 L |
5504 | if (adjust != 0 |
5505 | && (s_start < p_end | |
5506 | || p_end < p_start)) | |
252b5132 | 5507 | { |
4eca0228 | 5508 | _bfd_error_handler |
695344c0 | 5509 | /* xgettext:c-format */ |
b5599592 AM |
5510 | (_("%B: section %A lma %#lx adjusted to %#lx"), abfd, sec, |
5511 | (unsigned long) s_start, (unsigned long) p_end); | |
88967714 | 5512 | adjust = 0; |
b5599592 | 5513 | sec->lma = p_end; |
1cfb7d1e | 5514 | } |
3ac9b6c9 | 5515 | p->p_memsz += adjust; |
1cfb7d1e | 5516 | |
88967714 AM |
5517 | if (this_hdr->sh_type != SHT_NOBITS) |
5518 | { | |
32812159 AM |
5519 | if (p->p_filesz + adjust < p->p_memsz) |
5520 | { | |
5521 | /* We have a PROGBITS section following NOBITS ones. | |
5522 | Allocate file space for the NOBITS section(s) and | |
5523 | zero it. */ | |
5524 | adjust = p->p_memsz - p->p_filesz; | |
5525 | if (!write_zeros (abfd, off, adjust)) | |
5526 | return FALSE; | |
5527 | } | |
88967714 AM |
5528 | off += adjust; |
5529 | p->p_filesz += adjust; | |
252b5132 | 5530 | } |
252b5132 RH |
5531 | } |
5532 | ||
5533 | if (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core) | |
5534 | { | |
b301b248 AM |
5535 | /* The section at i == 0 is the one that actually contains |
5536 | everything. */ | |
4a938328 MS |
5537 | if (i == 0) |
5538 | { | |
627b32bc | 5539 | this_hdr->sh_offset = sec->filepos = off; |
6a3cd2b4 AM |
5540 | off += this_hdr->sh_size; |
5541 | p->p_filesz = this_hdr->sh_size; | |
b301b248 AM |
5542 | p->p_memsz = 0; |
5543 | p->p_align = 1; | |
252b5132 | 5544 | } |
4a938328 | 5545 | else |
252b5132 | 5546 | { |
b301b248 | 5547 | /* The rest are fake sections that shouldn't be written. */ |
252b5132 | 5548 | sec->filepos = 0; |
eea6121a | 5549 | sec->size = 0; |
b301b248 AM |
5550 | sec->flags = 0; |
5551 | continue; | |
252b5132 | 5552 | } |
252b5132 RH |
5553 | } |
5554 | else | |
5555 | { | |
1e951488 | 5556 | if (p->p_type == PT_LOAD) |
b301b248 | 5557 | { |
1e951488 AM |
5558 | this_hdr->sh_offset = sec->filepos = off; |
5559 | if (this_hdr->sh_type != SHT_NOBITS) | |
5560 | off += this_hdr->sh_size; | |
5561 | } | |
5562 | else if (this_hdr->sh_type == SHT_NOBITS | |
5563 | && (this_hdr->sh_flags & SHF_TLS) != 0 | |
5564 | && this_hdr->sh_offset == 0) | |
5565 | { | |
5566 | /* This is a .tbss section that didn't get a PT_LOAD. | |
5567 | (See _bfd_elf_map_sections_to_segments "Create a | |
5568 | final PT_LOAD".) Set sh_offset to the value it | |
5569 | would have if we had created a zero p_filesz and | |
5570 | p_memsz PT_LOAD header for the section. This | |
5571 | also makes the PT_TLS header have the same | |
5572 | p_offset value. */ | |
5573 | bfd_vma adjust = vma_page_aligned_bias (this_hdr->sh_addr, | |
5574 | off, align); | |
5575 | this_hdr->sh_offset = sec->filepos = off + adjust; | |
b301b248 | 5576 | } |
252b5132 | 5577 | |
02bf8d82 | 5578 | if (this_hdr->sh_type != SHT_NOBITS) |
b301b248 | 5579 | { |
6a3cd2b4 | 5580 | p->p_filesz += this_hdr->sh_size; |
02bf8d82 AM |
5581 | /* A load section without SHF_ALLOC is something like |
5582 | a note section in a PT_NOTE segment. These take | |
5583 | file space but are not loaded into memory. */ | |
5584 | if ((this_hdr->sh_flags & SHF_ALLOC) != 0) | |
6a3cd2b4 | 5585 | p->p_memsz += this_hdr->sh_size; |
b301b248 | 5586 | } |
6a3cd2b4 | 5587 | else if ((this_hdr->sh_flags & SHF_ALLOC) != 0) |
13ae64f3 | 5588 | { |
6a3cd2b4 AM |
5589 | if (p->p_type == PT_TLS) |
5590 | p->p_memsz += this_hdr->sh_size; | |
5591 | ||
5592 | /* .tbss is special. It doesn't contribute to p_memsz of | |
5593 | normal segments. */ | |
5594 | else if ((this_hdr->sh_flags & SHF_TLS) == 0) | |
5595 | p->p_memsz += this_hdr->sh_size; | |
13ae64f3 JJ |
5596 | } |
5597 | ||
b10a8ae0 L |
5598 | if (align > p->p_align |
5599 | && !m->p_align_valid | |
5600 | && (p->p_type != PT_LOAD | |
5601 | || (abfd->flags & D_PAGED) == 0)) | |
252b5132 RH |
5602 | p->p_align = align; |
5603 | } | |
5604 | ||
bf988460 | 5605 | if (!m->p_flags_valid) |
252b5132 RH |
5606 | { |
5607 | p->p_flags |= PF_R; | |
02bf8d82 | 5608 | if ((this_hdr->sh_flags & SHF_EXECINSTR) != 0) |
252b5132 | 5609 | p->p_flags |= PF_X; |
02bf8d82 | 5610 | if ((this_hdr->sh_flags & SHF_WRITE) != 0) |
252b5132 RH |
5611 | p->p_flags |= PF_W; |
5612 | } | |
5613 | } | |
43a8475c | 5614 | |
bf988460 | 5615 | off -= off_adjust; |
0920dee7 | 5616 | |
7c928300 AM |
5617 | /* Check that all sections are in a PT_LOAD segment. |
5618 | Don't check funky gdb generated core files. */ | |
5619 | if (p->p_type == PT_LOAD && bfd_get_format (abfd) != bfd_core) | |
9a83a553 AM |
5620 | { |
5621 | bfd_boolean check_vma = TRUE; | |
5622 | ||
5623 | for (i = 1; i < m->count; i++) | |
5624 | if (m->sections[i]->vma == m->sections[i - 1]->vma | |
5625 | && ELF_SECTION_SIZE (&(elf_section_data (m->sections[i]) | |
5626 | ->this_hdr), p) != 0 | |
5627 | && ELF_SECTION_SIZE (&(elf_section_data (m->sections[i - 1]) | |
5628 | ->this_hdr), p) != 0) | |
0920dee7 | 5629 | { |
9a83a553 AM |
5630 | /* Looks like we have overlays packed into the segment. */ |
5631 | check_vma = FALSE; | |
5632 | break; | |
0920dee7 | 5633 | } |
9a83a553 AM |
5634 | |
5635 | for (i = 0; i < m->count; i++) | |
5636 | { | |
5637 | Elf_Internal_Shdr *this_hdr; | |
5638 | asection *sec; | |
5639 | ||
5640 | sec = m->sections[i]; | |
5641 | this_hdr = &(elf_section_data(sec)->this_hdr); | |
86b2281f AM |
5642 | if (!ELF_SECTION_IN_SEGMENT_1 (this_hdr, p, check_vma, 0) |
5643 | && !ELF_TBSS_SPECIAL (this_hdr, p)) | |
9a83a553 | 5644 | { |
4eca0228 | 5645 | _bfd_error_handler |
695344c0 | 5646 | /* xgettext:c-format */ |
9a83a553 AM |
5647 | (_("%B: section `%A' can't be allocated in segment %d"), |
5648 | abfd, sec, j); | |
5649 | print_segment_map (m); | |
5650 | } | |
5651 | } | |
5652 | } | |
252b5132 RH |
5653 | } |
5654 | ||
12bd6957 | 5655 | elf_next_file_pos (abfd) = off; |
f3520d2f AM |
5656 | return TRUE; |
5657 | } | |
5658 | ||
5659 | /* Assign file positions for the other sections. */ | |
5660 | ||
5661 | static bfd_boolean | |
5662 | assign_file_positions_for_non_load_sections (bfd *abfd, | |
5663 | struct bfd_link_info *link_info) | |
5664 | { | |
5665 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
5666 | Elf_Internal_Shdr **i_shdrpp; | |
e06efbf1 | 5667 | Elf_Internal_Shdr **hdrpp, **end_hdrpp; |
f3520d2f AM |
5668 | Elf_Internal_Phdr *phdrs; |
5669 | Elf_Internal_Phdr *p; | |
5670 | struct elf_segment_map *m; | |
62655c7b | 5671 | struct elf_segment_map *hdrs_segment; |
f3520d2f AM |
5672 | bfd_vma filehdr_vaddr, filehdr_paddr; |
5673 | bfd_vma phdrs_vaddr, phdrs_paddr; | |
5674 | file_ptr off; | |
f3520d2f AM |
5675 | unsigned int count; |
5676 | ||
5c182d5f | 5677 | i_shdrpp = elf_elfsections (abfd); |
e06efbf1 | 5678 | end_hdrpp = i_shdrpp + elf_numsections (abfd); |
12bd6957 | 5679 | off = elf_next_file_pos (abfd); |
e06efbf1 | 5680 | for (hdrpp = i_shdrpp + 1; hdrpp < end_hdrpp; hdrpp++) |
5c182d5f | 5681 | { |
5c182d5f AM |
5682 | Elf_Internal_Shdr *hdr; |
5683 | ||
5684 | hdr = *hdrpp; | |
5685 | if (hdr->bfd_section != NULL | |
252e386e AM |
5686 | && (hdr->bfd_section->filepos != 0 |
5687 | || (hdr->sh_type == SHT_NOBITS | |
5688 | && hdr->contents == NULL))) | |
627b32bc | 5689 | BFD_ASSERT (hdr->sh_offset == hdr->bfd_section->filepos); |
5c182d5f AM |
5690 | else if ((hdr->sh_flags & SHF_ALLOC) != 0) |
5691 | { | |
e8d2ba53 | 5692 | if (hdr->sh_size != 0) |
4eca0228 | 5693 | _bfd_error_handler |
695344c0 | 5694 | /* xgettext:c-format */ |
e8d2ba53 AM |
5695 | (_("%B: warning: allocated section `%s' not in segment"), |
5696 | abfd, | |
5697 | (hdr->bfd_section == NULL | |
5698 | ? "*unknown*" | |
5699 | : hdr->bfd_section->name)); | |
3ba71138 L |
5700 | /* We don't need to page align empty sections. */ |
5701 | if ((abfd->flags & D_PAGED) != 0 && hdr->sh_size != 0) | |
5c182d5f AM |
5702 | off += vma_page_aligned_bias (hdr->sh_addr, off, |
5703 | bed->maxpagesize); | |
5704 | else | |
5705 | off += vma_page_aligned_bias (hdr->sh_addr, off, | |
5706 | hdr->sh_addralign); | |
5707 | off = _bfd_elf_assign_file_position_for_section (hdr, off, | |
5708 | FALSE); | |
5709 | } | |
5710 | else if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA) | |
5711 | && hdr->bfd_section == NULL) | |
0ce398f1 L |
5712 | || (hdr->bfd_section != NULL |
5713 | && (hdr->bfd_section->flags & SEC_ELF_COMPRESS)) | |
5714 | /* Compress DWARF debug sections. */ | |
12bd6957 | 5715 | || hdr == i_shdrpp[elf_onesymtab (abfd)] |
6a40cf0c NC |
5716 | || (elf_symtab_shndx_list (abfd) != NULL |
5717 | && hdr == i_shdrpp[elf_symtab_shndx_list (abfd)->ndx]) | |
3e19fb8f L |
5718 | || hdr == i_shdrpp[elf_strtab_sec (abfd)] |
5719 | || hdr == i_shdrpp[elf_shstrtab_sec (abfd)]) | |
5c182d5f AM |
5720 | hdr->sh_offset = -1; |
5721 | else | |
5722 | off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE); | |
5c182d5f AM |
5723 | } |
5724 | ||
252b5132 RH |
5725 | /* Now that we have set the section file positions, we can set up |
5726 | the file positions for the non PT_LOAD segments. */ | |
f3520d2f AM |
5727 | count = 0; |
5728 | filehdr_vaddr = 0; | |
5729 | filehdr_paddr = 0; | |
5730 | phdrs_vaddr = bed->maxpagesize + bed->s->sizeof_ehdr; | |
5731 | phdrs_paddr = 0; | |
62655c7b | 5732 | hdrs_segment = NULL; |
f3520d2f | 5733 | phdrs = elf_tdata (abfd)->phdr; |
12bd6957 | 5734 | for (m = elf_seg_map (abfd), p = phdrs; m != NULL; m = m->next, p++) |
f3520d2f AM |
5735 | { |
5736 | ++count; | |
5737 | if (p->p_type != PT_LOAD) | |
5738 | continue; | |
5739 | ||
5740 | if (m->includes_filehdr) | |
5741 | { | |
5742 | filehdr_vaddr = p->p_vaddr; | |
5743 | filehdr_paddr = p->p_paddr; | |
5744 | } | |
5745 | if (m->includes_phdrs) | |
5746 | { | |
5747 | phdrs_vaddr = p->p_vaddr; | |
5748 | phdrs_paddr = p->p_paddr; | |
5749 | if (m->includes_filehdr) | |
5750 | { | |
62655c7b | 5751 | hdrs_segment = m; |
f3520d2f AM |
5752 | phdrs_vaddr += bed->s->sizeof_ehdr; |
5753 | phdrs_paddr += bed->s->sizeof_ehdr; | |
5754 | } | |
5755 | } | |
5756 | } | |
5757 | ||
62655c7b RM |
5758 | if (hdrs_segment != NULL && link_info != NULL) |
5759 | { | |
5760 | /* There is a segment that contains both the file headers and the | |
5761 | program headers, so provide a symbol __ehdr_start pointing there. | |
5762 | A program can use this to examine itself robustly. */ | |
5763 | ||
5764 | struct elf_link_hash_entry *hash | |
5765 | = elf_link_hash_lookup (elf_hash_table (link_info), "__ehdr_start", | |
5766 | FALSE, FALSE, TRUE); | |
5767 | /* If the symbol was referenced and not defined, define it. */ | |
5768 | if (hash != NULL | |
5769 | && (hash->root.type == bfd_link_hash_new | |
5770 | || hash->root.type == bfd_link_hash_undefined | |
5771 | || hash->root.type == bfd_link_hash_undefweak | |
5772 | || hash->root.type == bfd_link_hash_common)) | |
5773 | { | |
5774 | asection *s = NULL; | |
5775 | if (hdrs_segment->count != 0) | |
5776 | /* The segment contains sections, so use the first one. */ | |
5777 | s = hdrs_segment->sections[0]; | |
5778 | else | |
5779 | /* Use the first (i.e. lowest-addressed) section in any segment. */ | |
12bd6957 | 5780 | for (m = elf_seg_map (abfd); m != NULL; m = m->next) |
62655c7b RM |
5781 | if (m->count != 0) |
5782 | { | |
5783 | s = m->sections[0]; | |
5784 | break; | |
5785 | } | |
5786 | ||
5787 | if (s != NULL) | |
5788 | { | |
5789 | hash->root.u.def.value = filehdr_vaddr - s->vma; | |
5790 | hash->root.u.def.section = s; | |
5791 | } | |
5792 | else | |
5793 | { | |
5794 | hash->root.u.def.value = filehdr_vaddr; | |
5795 | hash->root.u.def.section = bfd_abs_section_ptr; | |
5796 | } | |
5797 | ||
5798 | hash->root.type = bfd_link_hash_defined; | |
5799 | hash->def_regular = 1; | |
5800 | hash->non_elf = 0; | |
5801 | } | |
5802 | } | |
5803 | ||
12bd6957 | 5804 | for (m = elf_seg_map (abfd), p = phdrs; m != NULL; m = m->next, p++) |
252b5132 | 5805 | { |
129af99f | 5806 | if (p->p_type == PT_GNU_RELRO) |
252b5132 | 5807 | { |
b84a33b5 | 5808 | const Elf_Internal_Phdr *lp; |
3146fac4 | 5809 | struct elf_segment_map *lm; |
1ea63fd2 | 5810 | |
129af99f | 5811 | if (link_info != NULL) |
8c37241b | 5812 | { |
129af99f AS |
5813 | /* During linking the range of the RELRO segment is passed |
5814 | in link_info. */ | |
12bd6957 | 5815 | for (lm = elf_seg_map (abfd), lp = phdrs; |
3146fac4 AM |
5816 | lm != NULL; |
5817 | lm = lm->next, lp++) | |
8c37241b JJ |
5818 | { |
5819 | if (lp->p_type == PT_LOAD | |
b84a33b5 | 5820 | && lp->p_vaddr < link_info->relro_end |
3146fac4 AM |
5821 | && lm->count != 0 |
5822 | && lm->sections[0]->vma >= link_info->relro_start) | |
8c37241b JJ |
5823 | break; |
5824 | } | |
8981c88a | 5825 | |
3146fac4 | 5826 | BFD_ASSERT (lm != NULL); |
8c37241b | 5827 | } |
129af99f AS |
5828 | else |
5829 | { | |
5830 | /* Otherwise we are copying an executable or shared | |
b84a33b5 | 5831 | library, but we need to use the same linker logic. */ |
129af99f AS |
5832 | for (lp = phdrs; lp < phdrs + count; ++lp) |
5833 | { | |
5834 | if (lp->p_type == PT_LOAD | |
5835 | && lp->p_paddr == p->p_paddr) | |
5836 | break; | |
5837 | } | |
b84a33b5 AM |
5838 | } |
5839 | ||
5840 | if (lp < phdrs + count) | |
5841 | { | |
5842 | p->p_vaddr = lp->p_vaddr; | |
5843 | p->p_paddr = lp->p_paddr; | |
5844 | p->p_offset = lp->p_offset; | |
5845 | if (link_info != NULL) | |
5846 | p->p_filesz = link_info->relro_end - lp->p_vaddr; | |
5847 | else if (m->p_size_valid) | |
5848 | p->p_filesz = m->p_size; | |
129af99f AS |
5849 | else |
5850 | abort (); | |
b84a33b5 | 5851 | p->p_memsz = p->p_filesz; |
f3944f72 L |
5852 | /* Preserve the alignment and flags if they are valid. The |
5853 | gold linker generates RW/4 for the PT_GNU_RELRO section. | |
5854 | It is better for objcopy/strip to honor these attributes | |
5855 | otherwise gdb will choke when using separate debug files. | |
5856 | */ | |
5857 | if (!m->p_align_valid) | |
5858 | p->p_align = 1; | |
5859 | if (!m->p_flags_valid) | |
341b8ee7 | 5860 | p->p_flags = PF_R; |
129af99f | 5861 | } |
9433b9b1 | 5862 | else |
b84a33b5 AM |
5863 | { |
5864 | memset (p, 0, sizeof *p); | |
5865 | p->p_type = PT_NULL; | |
5866 | } | |
129af99f | 5867 | } |
04c3a755 NS |
5868 | else if (p->p_type == PT_GNU_STACK) |
5869 | { | |
5870 | if (m->p_size_valid) | |
5871 | p->p_memsz = m->p_size; | |
5872 | } | |
129af99f AS |
5873 | else if (m->count != 0) |
5874 | { | |
e06efbf1 | 5875 | unsigned int i; |
1a9ccd70 | 5876 | |
129af99f AS |
5877 | if (p->p_type != PT_LOAD |
5878 | && (p->p_type != PT_NOTE | |
5879 | || bfd_get_format (abfd) != bfd_core)) | |
5880 | { | |
1a9ccd70 NC |
5881 | /* A user specified segment layout may include a PHDR |
5882 | segment that overlaps with a LOAD segment... */ | |
5883 | if (p->p_type == PT_PHDR) | |
5884 | { | |
5885 | m->count = 0; | |
5886 | continue; | |
5887 | } | |
5888 | ||
c86934ce NC |
5889 | if (m->includes_filehdr || m->includes_phdrs) |
5890 | { | |
b1fa9dd6 | 5891 | /* PR 17512: file: 2195325e. */ |
4eca0228 | 5892 | _bfd_error_handler |
1a9ccd70 NC |
5893 | (_("%B: error: non-load segment %d includes file header and/or program header"), |
5894 | abfd, (int)(p - phdrs)); | |
c86934ce NC |
5895 | return FALSE; |
5896 | } | |
129af99f | 5897 | |
86b2281f | 5898 | p->p_filesz = 0; |
129af99f | 5899 | p->p_offset = m->sections[0]->filepos; |
86b2281f AM |
5900 | for (i = m->count; i-- != 0;) |
5901 | { | |
5902 | asection *sect = m->sections[i]; | |
5903 | Elf_Internal_Shdr *hdr = &elf_section_data (sect)->this_hdr; | |
5904 | if (hdr->sh_type != SHT_NOBITS) | |
5905 | { | |
5906 | p->p_filesz = (sect->filepos - m->sections[0]->filepos | |
5907 | + hdr->sh_size); | |
5908 | break; | |
5909 | } | |
5910 | } | |
129af99f AS |
5911 | } |
5912 | } | |
5913 | else if (m->includes_filehdr) | |
5914 | { | |
5915 | p->p_vaddr = filehdr_vaddr; | |
5916 | if (! m->p_paddr_valid) | |
5917 | p->p_paddr = filehdr_paddr; | |
5918 | } | |
5919 | else if (m->includes_phdrs) | |
5920 | { | |
5921 | p->p_vaddr = phdrs_vaddr; | |
5922 | if (! m->p_paddr_valid) | |
5923 | p->p_paddr = phdrs_paddr; | |
252b5132 RH |
5924 | } |
5925 | } | |
5926 | ||
12bd6957 | 5927 | elf_next_file_pos (abfd) = off; |
252b5132 | 5928 | |
b34976b6 | 5929 | return TRUE; |
252b5132 RH |
5930 | } |
5931 | ||
6a40cf0c NC |
5932 | static elf_section_list * |
5933 | find_section_in_list (unsigned int i, elf_section_list * list) | |
5934 | { | |
5935 | for (;list != NULL; list = list->next) | |
5936 | if (list->ndx == i) | |
5937 | break; | |
5938 | return list; | |
5939 | } | |
5940 | ||
252b5132 RH |
5941 | /* Work out the file positions of all the sections. This is called by |
5942 | _bfd_elf_compute_section_file_positions. All the section sizes and | |
5943 | VMAs must be known before this is called. | |
5944 | ||
e0638f70 AM |
5945 | Reloc sections come in two flavours: Those processed specially as |
5946 | "side-channel" data attached to a section to which they apply, and | |
5947 | those that bfd doesn't process as relocations. The latter sort are | |
5948 | stored in a normal bfd section by bfd_section_from_shdr. We don't | |
5949 | consider the former sort here, unless they form part of the loadable | |
5950 | image. Reloc sections not assigned here will be handled later by | |
5951 | assign_file_positions_for_relocs. | |
252b5132 RH |
5952 | |
5953 | We also don't set the positions of the .symtab and .strtab here. */ | |
5954 | ||
b34976b6 | 5955 | static bfd_boolean |
c84fca4d AO |
5956 | assign_file_positions_except_relocs (bfd *abfd, |
5957 | struct bfd_link_info *link_info) | |
252b5132 | 5958 | { |
5c182d5f AM |
5959 | struct elf_obj_tdata *tdata = elf_tdata (abfd); |
5960 | Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd); | |
9c5bfbb7 | 5961 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
252b5132 RH |
5962 | |
5963 | if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0 | |
5964 | && bfd_get_format (abfd) != bfd_core) | |
5965 | { | |
5c182d5f AM |
5966 | Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd); |
5967 | unsigned int num_sec = elf_numsections (abfd); | |
252b5132 RH |
5968 | Elf_Internal_Shdr **hdrpp; |
5969 | unsigned int i; | |
a485e98e | 5970 | file_ptr off; |
252b5132 RH |
5971 | |
5972 | /* Start after the ELF header. */ | |
5973 | off = i_ehdrp->e_ehsize; | |
5974 | ||
5975 | /* We are not creating an executable, which means that we are | |
5976 | not creating a program header, and that the actual order of | |
5977 | the sections in the file is unimportant. */ | |
9ad5cbcf | 5978 | for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++) |
252b5132 RH |
5979 | { |
5980 | Elf_Internal_Shdr *hdr; | |
5981 | ||
5982 | hdr = *hdrpp; | |
e0638f70 AM |
5983 | if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA) |
5984 | && hdr->bfd_section == NULL) | |
0ce398f1 L |
5985 | || (hdr->bfd_section != NULL |
5986 | && (hdr->bfd_section->flags & SEC_ELF_COMPRESS)) | |
5987 | /* Compress DWARF debug sections. */ | |
12bd6957 | 5988 | || i == elf_onesymtab (abfd) |
6a40cf0c NC |
5989 | || (elf_symtab_shndx_list (abfd) != NULL |
5990 | && hdr == i_shdrpp[elf_symtab_shndx_list (abfd)->ndx]) | |
3e19fb8f L |
5991 | || i == elf_strtab_sec (abfd) |
5992 | || i == elf_shstrtab_sec (abfd)) | |
252b5132 RH |
5993 | { |
5994 | hdr->sh_offset = -1; | |
252b5132 | 5995 | } |
9ad5cbcf | 5996 | else |
b34976b6 | 5997 | off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE); |
252b5132 | 5998 | } |
a485e98e AM |
5999 | |
6000 | elf_next_file_pos (abfd) = off; | |
252b5132 RH |
6001 | } |
6002 | else | |
6003 | { | |
f3520d2f AM |
6004 | unsigned int alloc; |
6005 | ||
252b5132 | 6006 | /* Assign file positions for the loaded sections based on the |
08a40648 | 6007 | assignment of sections to segments. */ |
f3520d2f AM |
6008 | if (!assign_file_positions_for_load_sections (abfd, link_info)) |
6009 | return FALSE; | |
6010 | ||
6011 | /* And for non-load sections. */ | |
6012 | if (!assign_file_positions_for_non_load_sections (abfd, link_info)) | |
6013 | return FALSE; | |
6014 | ||
e36284ab AM |
6015 | if (bed->elf_backend_modify_program_headers != NULL) |
6016 | { | |
6017 | if (!(*bed->elf_backend_modify_program_headers) (abfd, link_info)) | |
6018 | return FALSE; | |
6019 | } | |
6020 | ||
58e7ebac | 6021 | /* Set e_type in ELF header to ET_EXEC for -pie -Ttext-segment=. */ |
0e1862bb | 6022 | if (link_info != NULL && bfd_link_pie (link_info)) |
58e7ebac L |
6023 | { |
6024 | unsigned int num_segments = elf_elfheader (abfd)->e_phnum; | |
6025 | Elf_Internal_Phdr *segment = elf_tdata (abfd)->phdr; | |
6026 | Elf_Internal_Phdr *end_segment = &segment[num_segments]; | |
6027 | ||
6028 | /* Find the lowest p_vaddr in PT_LOAD segments. */ | |
6029 | bfd_vma p_vaddr = (bfd_vma) -1; | |
6030 | for (; segment < end_segment; segment++) | |
6031 | if (segment->p_type == PT_LOAD && p_vaddr > segment->p_vaddr) | |
6032 | p_vaddr = segment->p_vaddr; | |
6033 | ||
6034 | /* Set e_type to ET_EXEC if the lowest p_vaddr in PT_LOAD | |
6035 | segments is non-zero. */ | |
6036 | if (p_vaddr) | |
6037 | i_ehdrp->e_type = ET_EXEC; | |
6038 | } | |
6039 | ||
f3520d2f | 6040 | /* Write out the program headers. */ |
12bd6957 | 6041 | alloc = elf_program_header_size (abfd) / bed->s->sizeof_phdr; |
1a9ccd70 NC |
6042 | |
6043 | /* Sort the program headers into the ordering required by the ELF standard. */ | |
6044 | if (alloc == 0) | |
6045 | return TRUE; | |
6046 | ||
1a9ccd70 NC |
6047 | /* PR ld/20815 - Check that the program header segment, if present, will |
6048 | be loaded into memory. FIXME: The check below is not sufficient as | |
6049 | really all PT_LOAD segments should be checked before issuing an error | |
6050 | message. Plus the PHDR segment does not have to be the first segment | |
6051 | in the program header table. But this version of the check should | |
cd584857 NC |
6052 | catch all real world use cases. |
6053 | ||
6054 | FIXME: We used to have code here to sort the PT_LOAD segments into | |
6055 | ascending order, as per the ELF spec. But this breaks some programs, | |
6056 | including the Linux kernel. But really either the spec should be | |
6057 | changed or the programs updated. */ | |
1a9ccd70 | 6058 | if (alloc > 1 |
cd584857 NC |
6059 | && tdata->phdr[0].p_type == PT_PHDR |
6060 | && ! bed->elf_backend_allow_non_load_phdr (abfd, tdata->phdr, alloc) | |
6061 | && tdata->phdr[1].p_type == PT_LOAD | |
6062 | && (tdata->phdr[1].p_vaddr > tdata->phdr[0].p_vaddr | |
6063 | || (tdata->phdr[1].p_vaddr + tdata->phdr[1].p_memsz) | |
6064 | < (tdata->phdr[0].p_vaddr + tdata->phdr[0].p_memsz))) | |
1a9ccd70 NC |
6065 | { |
6066 | /* The fix for this error is usually to edit the linker script being | |
6067 | used and set up the program headers manually. Either that or | |
6068 | leave room for the headers at the start of the SECTIONS. */ | |
6069 | _bfd_error_handler (_("\ | |
6070 | %B: error: PHDR segment not covered by LOAD segment"), | |
6071 | abfd); | |
1a9ccd70 NC |
6072 | return FALSE; |
6073 | } | |
6074 | ||
f3520d2f | 6075 | if (bfd_seek (abfd, (bfd_signed_vma) bed->s->sizeof_ehdr, SEEK_SET) != 0 |
cd584857 NC |
6076 | || bed->s->write_out_phdrs (abfd, tdata->phdr, alloc) != 0) |
6077 | return FALSE; | |
252b5132 RH |
6078 | } |
6079 | ||
b34976b6 | 6080 | return TRUE; |
252b5132 RH |
6081 | } |
6082 | ||
b34976b6 | 6083 | static bfd_boolean |
217aa764 | 6084 | prep_headers (bfd *abfd) |
252b5132 | 6085 | { |
3d540e93 | 6086 | Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form. */ |
2b0f7ef9 | 6087 | struct elf_strtab_hash *shstrtab; |
9c5bfbb7 | 6088 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
252b5132 RH |
6089 | |
6090 | i_ehdrp = elf_elfheader (abfd); | |
252b5132 | 6091 | |
2b0f7ef9 | 6092 | shstrtab = _bfd_elf_strtab_init (); |
252b5132 | 6093 | if (shstrtab == NULL) |
b34976b6 | 6094 | return FALSE; |
252b5132 RH |
6095 | |
6096 | elf_shstrtab (abfd) = shstrtab; | |
6097 | ||
6098 | i_ehdrp->e_ident[EI_MAG0] = ELFMAG0; | |
6099 | i_ehdrp->e_ident[EI_MAG1] = ELFMAG1; | |
6100 | i_ehdrp->e_ident[EI_MAG2] = ELFMAG2; | |
6101 | i_ehdrp->e_ident[EI_MAG3] = ELFMAG3; | |
6102 | ||
6103 | i_ehdrp->e_ident[EI_CLASS] = bed->s->elfclass; | |
6104 | i_ehdrp->e_ident[EI_DATA] = | |
6105 | bfd_big_endian (abfd) ? ELFDATA2MSB : ELFDATA2LSB; | |
6106 | i_ehdrp->e_ident[EI_VERSION] = bed->s->ev_current; | |
6107 | ||
252b5132 RH |
6108 | if ((abfd->flags & DYNAMIC) != 0) |
6109 | i_ehdrp->e_type = ET_DYN; | |
6110 | else if ((abfd->flags & EXEC_P) != 0) | |
6111 | i_ehdrp->e_type = ET_EXEC; | |
6112 | else if (bfd_get_format (abfd) == bfd_core) | |
6113 | i_ehdrp->e_type = ET_CORE; | |
6114 | else | |
6115 | i_ehdrp->e_type = ET_REL; | |
6116 | ||
6117 | switch (bfd_get_arch (abfd)) | |
6118 | { | |
6119 | case bfd_arch_unknown: | |
6120 | i_ehdrp->e_machine = EM_NONE; | |
6121 | break; | |
aa4f99bb AO |
6122 | |
6123 | /* There used to be a long list of cases here, each one setting | |
6124 | e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE | |
6125 | in the corresponding bfd definition. To avoid duplication, | |
6126 | the switch was removed. Machines that need special handling | |
6127 | can generally do it in elf_backend_final_write_processing(), | |
6128 | unless they need the information earlier than the final write. | |
6129 | Such need can generally be supplied by replacing the tests for | |
6130 | e_machine with the conditions used to determine it. */ | |
252b5132 | 6131 | default: |
9c5bfbb7 AM |
6132 | i_ehdrp->e_machine = bed->elf_machine_code; |
6133 | } | |
aa4f99bb | 6134 | |
252b5132 RH |
6135 | i_ehdrp->e_version = bed->s->ev_current; |
6136 | i_ehdrp->e_ehsize = bed->s->sizeof_ehdr; | |
6137 | ||
c044fabd | 6138 | /* No program header, for now. */ |
252b5132 RH |
6139 | i_ehdrp->e_phoff = 0; |
6140 | i_ehdrp->e_phentsize = 0; | |
6141 | i_ehdrp->e_phnum = 0; | |
6142 | ||
c044fabd | 6143 | /* Each bfd section is section header entry. */ |
252b5132 RH |
6144 | i_ehdrp->e_entry = bfd_get_start_address (abfd); |
6145 | i_ehdrp->e_shentsize = bed->s->sizeof_shdr; | |
6146 | ||
c044fabd | 6147 | /* If we're building an executable, we'll need a program header table. */ |
252b5132 | 6148 | if (abfd->flags & EXEC_P) |
0e71e495 BE |
6149 | /* It all happens later. */ |
6150 | ; | |
252b5132 RH |
6151 | else |
6152 | { | |
6153 | i_ehdrp->e_phentsize = 0; | |
252b5132 RH |
6154 | i_ehdrp->e_phoff = 0; |
6155 | } | |
6156 | ||
6157 | elf_tdata (abfd)->symtab_hdr.sh_name = | |
b34976b6 | 6158 | (unsigned int) _bfd_elf_strtab_add (shstrtab, ".symtab", FALSE); |
252b5132 | 6159 | elf_tdata (abfd)->strtab_hdr.sh_name = |
b34976b6 | 6160 | (unsigned int) _bfd_elf_strtab_add (shstrtab, ".strtab", FALSE); |
252b5132 | 6161 | elf_tdata (abfd)->shstrtab_hdr.sh_name = |
b34976b6 | 6162 | (unsigned int) _bfd_elf_strtab_add (shstrtab, ".shstrtab", FALSE); |
252b5132 | 6163 | if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1 |
17ca87fc | 6164 | || elf_tdata (abfd)->strtab_hdr.sh_name == (unsigned int) -1 |
252b5132 | 6165 | || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1) |
b34976b6 | 6166 | return FALSE; |
252b5132 | 6167 | |
b34976b6 | 6168 | return TRUE; |
252b5132 RH |
6169 | } |
6170 | ||
6171 | /* Assign file positions for all the reloc sections which are not part | |
a485e98e | 6172 | of the loadable file image, and the file position of section headers. */ |
252b5132 | 6173 | |
0ce398f1 L |
6174 | static bfd_boolean |
6175 | _bfd_elf_assign_file_positions_for_non_load (bfd *abfd) | |
252b5132 RH |
6176 | { |
6177 | file_ptr off; | |
e06efbf1 | 6178 | Elf_Internal_Shdr **shdrpp, **end_shdrpp; |
3e19fb8f | 6179 | Elf_Internal_Shdr *shdrp; |
a485e98e AM |
6180 | Elf_Internal_Ehdr *i_ehdrp; |
6181 | const struct elf_backend_data *bed; | |
252b5132 | 6182 | |
12bd6957 | 6183 | off = elf_next_file_pos (abfd); |
252b5132 | 6184 | |
e06efbf1 L |
6185 | shdrpp = elf_elfsections (abfd); |
6186 | end_shdrpp = shdrpp + elf_numsections (abfd); | |
6187 | for (shdrpp++; shdrpp < end_shdrpp; shdrpp++) | |
252b5132 | 6188 | { |
252b5132 | 6189 | shdrp = *shdrpp; |
0ce398f1 L |
6190 | if (shdrp->sh_offset == -1) |
6191 | { | |
3e19fb8f | 6192 | asection *sec = shdrp->bfd_section; |
0ce398f1 L |
6193 | bfd_boolean is_rel = (shdrp->sh_type == SHT_REL |
6194 | || shdrp->sh_type == SHT_RELA); | |
6195 | if (is_rel | |
3e19fb8f | 6196 | || (sec != NULL && (sec->flags & SEC_ELF_COMPRESS))) |
0ce398f1 L |
6197 | { |
6198 | if (!is_rel) | |
6199 | { | |
3e19fb8f L |
6200 | const char *name = sec->name; |
6201 | struct bfd_elf_section_data *d; | |
6202 | ||
0ce398f1 | 6203 | /* Compress DWARF debug sections. */ |
3e19fb8f | 6204 | if (!bfd_compress_section (abfd, sec, |
0ce398f1 L |
6205 | shdrp->contents)) |
6206 | return FALSE; | |
3e19fb8f L |
6207 | |
6208 | if (sec->compress_status == COMPRESS_SECTION_DONE | |
6209 | && (abfd->flags & BFD_COMPRESS_GABI) == 0) | |
6210 | { | |
6211 | /* If section is compressed with zlib-gnu, convert | |
6212 | section name from .debug_* to .zdebug_*. */ | |
6213 | char *new_name | |
6214 | = convert_debug_to_zdebug (abfd, name); | |
6215 | if (new_name == NULL) | |
6216 | return FALSE; | |
6217 | name = new_name; | |
6218 | } | |
dd905818 | 6219 | /* Add section name to section name section. */ |
3e19fb8f L |
6220 | if (shdrp->sh_name != (unsigned int) -1) |
6221 | abort (); | |
6222 | shdrp->sh_name | |
6223 | = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), | |
6224 | name, FALSE); | |
6225 | d = elf_section_data (sec); | |
6226 | ||
dd905818 | 6227 | /* Add reloc section name to section name section. */ |
3e19fb8f L |
6228 | if (d->rel.hdr |
6229 | && !_bfd_elf_set_reloc_sh_name (abfd, | |
6230 | d->rel.hdr, | |
6231 | name, FALSE)) | |
6232 | return FALSE; | |
6233 | if (d->rela.hdr | |
6234 | && !_bfd_elf_set_reloc_sh_name (abfd, | |
6235 | d->rela.hdr, | |
91cb26da | 6236 | name, TRUE)) |
3e19fb8f L |
6237 | return FALSE; |
6238 | ||
0ce398f1 | 6239 | /* Update section size and contents. */ |
3e19fb8f L |
6240 | shdrp->sh_size = sec->size; |
6241 | shdrp->contents = sec->contents; | |
0ce398f1 L |
6242 | shdrp->bfd_section->contents = NULL; |
6243 | } | |
6244 | off = _bfd_elf_assign_file_position_for_section (shdrp, | |
6245 | off, | |
6246 | TRUE); | |
6247 | } | |
6248 | } | |
252b5132 RH |
6249 | } |
6250 | ||
3e19fb8f L |
6251 | /* Place section name section after DWARF debug sections have been |
6252 | compressed. */ | |
6253 | _bfd_elf_strtab_finalize (elf_shstrtab (abfd)); | |
6254 | shdrp = &elf_tdata (abfd)->shstrtab_hdr; | |
6255 | shdrp->sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd)); | |
6256 | off = _bfd_elf_assign_file_position_for_section (shdrp, off, TRUE); | |
6257 | ||
6258 | /* Place the section headers. */ | |
a485e98e AM |
6259 | i_ehdrp = elf_elfheader (abfd); |
6260 | bed = get_elf_backend_data (abfd); | |
6261 | off = align_file_position (off, 1 << bed->s->log_file_align); | |
6262 | i_ehdrp->e_shoff = off; | |
6263 | off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize; | |
12bd6957 | 6264 | elf_next_file_pos (abfd) = off; |
0ce398f1 L |
6265 | |
6266 | return TRUE; | |
252b5132 RH |
6267 | } |
6268 | ||
b34976b6 | 6269 | bfd_boolean |
217aa764 | 6270 | _bfd_elf_write_object_contents (bfd *abfd) |
252b5132 | 6271 | { |
9c5bfbb7 | 6272 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
252b5132 | 6273 | Elf_Internal_Shdr **i_shdrp; |
b34976b6 | 6274 | bfd_boolean failed; |
9ad5cbcf | 6275 | unsigned int count, num_sec; |
30e8ee25 | 6276 | struct elf_obj_tdata *t; |
252b5132 RH |
6277 | |
6278 | if (! abfd->output_has_begun | |
217aa764 | 6279 | && ! _bfd_elf_compute_section_file_positions (abfd, NULL)) |
b34976b6 | 6280 | return FALSE; |
252b5132 RH |
6281 | |
6282 | i_shdrp = elf_elfsections (abfd); | |
252b5132 | 6283 | |
b34976b6 | 6284 | failed = FALSE; |
252b5132 RH |
6285 | bfd_map_over_sections (abfd, bed->s->write_relocs, &failed); |
6286 | if (failed) | |
b34976b6 | 6287 | return FALSE; |
252b5132 | 6288 | |
0ce398f1 L |
6289 | if (!_bfd_elf_assign_file_positions_for_non_load (abfd)) |
6290 | return FALSE; | |
252b5132 | 6291 | |
c044fabd | 6292 | /* After writing the headers, we need to write the sections too... */ |
9ad5cbcf AM |
6293 | num_sec = elf_numsections (abfd); |
6294 | for (count = 1; count < num_sec; count++) | |
252b5132 | 6295 | { |
3e19fb8f L |
6296 | i_shdrp[count]->sh_name |
6297 | = _bfd_elf_strtab_offset (elf_shstrtab (abfd), | |
6298 | i_shdrp[count]->sh_name); | |
252b5132 RH |
6299 | if (bed->elf_backend_section_processing) |
6300 | (*bed->elf_backend_section_processing) (abfd, i_shdrp[count]); | |
6301 | if (i_shdrp[count]->contents) | |
6302 | { | |
dc810e39 AM |
6303 | bfd_size_type amt = i_shdrp[count]->sh_size; |
6304 | ||
252b5132 | 6305 | if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0 |
dc810e39 | 6306 | || bfd_bwrite (i_shdrp[count]->contents, amt, abfd) != amt) |
b34976b6 | 6307 | return FALSE; |
252b5132 RH |
6308 | } |
6309 | } | |
6310 | ||
6311 | /* Write out the section header names. */ | |
30e8ee25 | 6312 | t = elf_tdata (abfd); |
26ae6d5e | 6313 | if (elf_shstrtab (abfd) != NULL |
30e8ee25 | 6314 | && (bfd_seek (abfd, t->shstrtab_hdr.sh_offset, SEEK_SET) != 0 |
08a40648 | 6315 | || !_bfd_elf_strtab_emit (abfd, elf_shstrtab (abfd)))) |
b34976b6 | 6316 | return FALSE; |
252b5132 RH |
6317 | |
6318 | if (bed->elf_backend_final_write_processing) | |
12bd6957 | 6319 | (*bed->elf_backend_final_write_processing) (abfd, elf_linker (abfd)); |
252b5132 | 6320 | |
ff59fc36 RM |
6321 | if (!bed->s->write_shdrs_and_ehdr (abfd)) |
6322 | return FALSE; | |
6323 | ||
6324 | /* This is last since write_shdrs_and_ehdr can touch i_shdrp[0]. */ | |
c0355132 AM |
6325 | if (t->o->build_id.after_write_object_contents != NULL) |
6326 | return (*t->o->build_id.after_write_object_contents) (abfd); | |
ff59fc36 RM |
6327 | |
6328 | return TRUE; | |
252b5132 RH |
6329 | } |
6330 | ||
b34976b6 | 6331 | bfd_boolean |
217aa764 | 6332 | _bfd_elf_write_corefile_contents (bfd *abfd) |
252b5132 | 6333 | { |
c044fabd | 6334 | /* Hopefully this can be done just like an object file. */ |
252b5132 RH |
6335 | return _bfd_elf_write_object_contents (abfd); |
6336 | } | |
c044fabd KH |
6337 | |
6338 | /* Given a section, search the header to find them. */ | |
6339 | ||
cb33740c | 6340 | unsigned int |
198beae2 | 6341 | _bfd_elf_section_from_bfd_section (bfd *abfd, struct bfd_section *asect) |
252b5132 | 6342 | { |
9c5bfbb7 | 6343 | const struct elf_backend_data *bed; |
91d6fa6a | 6344 | unsigned int sec_index; |
252b5132 | 6345 | |
9ad5cbcf AM |
6346 | if (elf_section_data (asect) != NULL |
6347 | && elf_section_data (asect)->this_idx != 0) | |
6348 | return elf_section_data (asect)->this_idx; | |
6349 | ||
6350 | if (bfd_is_abs_section (asect)) | |
91d6fa6a | 6351 | sec_index = SHN_ABS; |
af746e92 | 6352 | else if (bfd_is_com_section (asect)) |
91d6fa6a | 6353 | sec_index = SHN_COMMON; |
af746e92 | 6354 | else if (bfd_is_und_section (asect)) |
91d6fa6a | 6355 | sec_index = SHN_UNDEF; |
af746e92 | 6356 | else |
91d6fa6a | 6357 | sec_index = SHN_BAD; |
252b5132 | 6358 | |
af746e92 | 6359 | bed = get_elf_backend_data (abfd); |
252b5132 RH |
6360 | if (bed->elf_backend_section_from_bfd_section) |
6361 | { | |
91d6fa6a | 6362 | int retval = sec_index; |
9ad5cbcf | 6363 | |
af746e92 AM |
6364 | if ((*bed->elf_backend_section_from_bfd_section) (abfd, asect, &retval)) |
6365 | return retval; | |
252b5132 RH |
6366 | } |
6367 | ||
91d6fa6a | 6368 | if (sec_index == SHN_BAD) |
af746e92 | 6369 | bfd_set_error (bfd_error_nonrepresentable_section); |
252b5132 | 6370 | |
91d6fa6a | 6371 | return sec_index; |
252b5132 RH |
6372 | } |
6373 | ||
6374 | /* Given a BFD symbol, return the index in the ELF symbol table, or -1 | |
6375 | on error. */ | |
6376 | ||
6377 | int | |
217aa764 | 6378 | _bfd_elf_symbol_from_bfd_symbol (bfd *abfd, asymbol **asym_ptr_ptr) |
252b5132 RH |
6379 | { |
6380 | asymbol *asym_ptr = *asym_ptr_ptr; | |
6381 | int idx; | |
6382 | flagword flags = asym_ptr->flags; | |
6383 | ||
6384 | /* When gas creates relocations against local labels, it creates its | |
6385 | own symbol for the section, but does put the symbol into the | |
6386 | symbol chain, so udata is 0. When the linker is generating | |
6387 | relocatable output, this section symbol may be for one of the | |
6388 | input sections rather than the output section. */ | |
6389 | if (asym_ptr->udata.i == 0 | |
6390 | && (flags & BSF_SECTION_SYM) | |
6391 | && asym_ptr->section) | |
6392 | { | |
5372391b | 6393 | asection *sec; |
252b5132 RH |
6394 | int indx; |
6395 | ||
5372391b AM |
6396 | sec = asym_ptr->section; |
6397 | if (sec->owner != abfd && sec->output_section != NULL) | |
6398 | sec = sec->output_section; | |
6399 | if (sec->owner == abfd | |
6400 | && (indx = sec->index) < elf_num_section_syms (abfd) | |
4e89ac30 | 6401 | && elf_section_syms (abfd)[indx] != NULL) |
252b5132 RH |
6402 | asym_ptr->udata.i = elf_section_syms (abfd)[indx]->udata.i; |
6403 | } | |
6404 | ||
6405 | idx = asym_ptr->udata.i; | |
6406 | ||
6407 | if (idx == 0) | |
6408 | { | |
6409 | /* This case can occur when using --strip-symbol on a symbol | |
08a40648 | 6410 | which is used in a relocation entry. */ |
4eca0228 | 6411 | _bfd_error_handler |
695344c0 | 6412 | /* xgettext:c-format */ |
d003868e AM |
6413 | (_("%B: symbol `%s' required but not present"), |
6414 | abfd, bfd_asymbol_name (asym_ptr)); | |
252b5132 RH |
6415 | bfd_set_error (bfd_error_no_symbols); |
6416 | return -1; | |
6417 | } | |
6418 | ||
6419 | #if DEBUG & 4 | |
6420 | { | |
6421 | fprintf (stderr, | |
9ccb8af9 AM |
6422 | "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8lx\n", |
6423 | (long) asym_ptr, asym_ptr->name, idx, (long) flags); | |
252b5132 RH |
6424 | fflush (stderr); |
6425 | } | |
6426 | #endif | |
6427 | ||
6428 | return idx; | |
6429 | } | |
6430 | ||
84d1d650 | 6431 | /* Rewrite program header information. */ |
252b5132 | 6432 | |
b34976b6 | 6433 | static bfd_boolean |
84d1d650 | 6434 | rewrite_elf_program_header (bfd *ibfd, bfd *obfd) |
252b5132 | 6435 | { |
b34976b6 AM |
6436 | Elf_Internal_Ehdr *iehdr; |
6437 | struct elf_segment_map *map; | |
6438 | struct elf_segment_map *map_first; | |
6439 | struct elf_segment_map **pointer_to_map; | |
6440 | Elf_Internal_Phdr *segment; | |
6441 | asection *section; | |
6442 | unsigned int i; | |
6443 | unsigned int num_segments; | |
6444 | bfd_boolean phdr_included = FALSE; | |
5c44b38e | 6445 | bfd_boolean p_paddr_valid; |
b34976b6 AM |
6446 | bfd_vma maxpagesize; |
6447 | struct elf_segment_map *phdr_adjust_seg = NULL; | |
6448 | unsigned int phdr_adjust_num = 0; | |
9c5bfbb7 | 6449 | const struct elf_backend_data *bed; |
bc67d8a6 | 6450 | |
caf47ea6 | 6451 | bed = get_elf_backend_data (ibfd); |
252b5132 RH |
6452 | iehdr = elf_elfheader (ibfd); |
6453 | ||
bc67d8a6 | 6454 | map_first = NULL; |
c044fabd | 6455 | pointer_to_map = &map_first; |
252b5132 RH |
6456 | |
6457 | num_segments = elf_elfheader (ibfd)->e_phnum; | |
bc67d8a6 NC |
6458 | maxpagesize = get_elf_backend_data (obfd)->maxpagesize; |
6459 | ||
6460 | /* Returns the end address of the segment + 1. */ | |
aecc8f8a AM |
6461 | #define SEGMENT_END(segment, start) \ |
6462 | (start + (segment->p_memsz > segment->p_filesz \ | |
6463 | ? segment->p_memsz : segment->p_filesz)) | |
bc67d8a6 | 6464 | |
eecdbe52 JJ |
6465 | #define SECTION_SIZE(section, segment) \ |
6466 | (((section->flags & (SEC_HAS_CONTENTS | SEC_THREAD_LOCAL)) \ | |
6467 | != SEC_THREAD_LOCAL || segment->p_type == PT_TLS) \ | |
eea6121a | 6468 | ? section->size : 0) |
eecdbe52 | 6469 | |
b34976b6 | 6470 | /* Returns TRUE if the given section is contained within |
bc67d8a6 | 6471 | the given segment. VMA addresses are compared. */ |
aecc8f8a AM |
6472 | #define IS_CONTAINED_BY_VMA(section, segment) \ |
6473 | (section->vma >= segment->p_vaddr \ | |
eecdbe52 | 6474 | && (section->vma + SECTION_SIZE (section, segment) \ |
aecc8f8a | 6475 | <= (SEGMENT_END (segment, segment->p_vaddr)))) |
c044fabd | 6476 | |
b34976b6 | 6477 | /* Returns TRUE if the given section is contained within |
bc67d8a6 | 6478 | the given segment. LMA addresses are compared. */ |
aecc8f8a AM |
6479 | #define IS_CONTAINED_BY_LMA(section, segment, base) \ |
6480 | (section->lma >= base \ | |
eecdbe52 | 6481 | && (section->lma + SECTION_SIZE (section, segment) \ |
aecc8f8a | 6482 | <= SEGMENT_END (segment, base))) |
252b5132 | 6483 | |
0efc80c8 L |
6484 | /* Handle PT_NOTE segment. */ |
6485 | #define IS_NOTE(p, s) \ | |
aecc8f8a | 6486 | (p->p_type == PT_NOTE \ |
0efc80c8 | 6487 | && elf_section_type (s) == SHT_NOTE \ |
aecc8f8a | 6488 | && (bfd_vma) s->filepos >= p->p_offset \ |
cb3ff1e5 | 6489 | && ((bfd_vma) s->filepos + s->size \ |
aecc8f8a | 6490 | <= p->p_offset + p->p_filesz)) |
252b5132 | 6491 | |
0efc80c8 L |
6492 | /* Special case: corefile "NOTE" section containing regs, prpsinfo |
6493 | etc. */ | |
6494 | #define IS_COREFILE_NOTE(p, s) \ | |
6495 | (IS_NOTE (p, s) \ | |
6496 | && bfd_get_format (ibfd) == bfd_core \ | |
6497 | && s->vma == 0 \ | |
6498 | && s->lma == 0) | |
6499 | ||
252b5132 RH |
6500 | /* The complicated case when p_vaddr is 0 is to handle the Solaris |
6501 | linker, which generates a PT_INTERP section with p_vaddr and | |
6502 | p_memsz set to 0. */ | |
aecc8f8a AM |
6503 | #define IS_SOLARIS_PT_INTERP(p, s) \ |
6504 | (p->p_vaddr == 0 \ | |
6505 | && p->p_paddr == 0 \ | |
6506 | && p->p_memsz == 0 \ | |
6507 | && p->p_filesz > 0 \ | |
6508 | && (s->flags & SEC_HAS_CONTENTS) != 0 \ | |
eea6121a | 6509 | && s->size > 0 \ |
aecc8f8a | 6510 | && (bfd_vma) s->filepos >= p->p_offset \ |
cb3ff1e5 | 6511 | && ((bfd_vma) s->filepos + s->size \ |
aecc8f8a | 6512 | <= p->p_offset + p->p_filesz)) |
5c440b1e | 6513 | |
bc67d8a6 NC |
6514 | /* Decide if the given section should be included in the given segment. |
6515 | A section will be included if: | |
f5ffc919 | 6516 | 1. It is within the address space of the segment -- we use the LMA |
08a40648 | 6517 | if that is set for the segment and the VMA otherwise, |
0efc80c8 | 6518 | 2. It is an allocated section or a NOTE section in a PT_NOTE |
d324f6d6 | 6519 | segment. |
bc67d8a6 | 6520 | 3. There is an output section associated with it, |
eecdbe52 | 6521 | 4. The section has not already been allocated to a previous segment. |
2b05f1b7 | 6522 | 5. PT_GNU_STACK segments do not include any sections. |
03394ac9 | 6523 | 6. PT_TLS segment includes only SHF_TLS sections. |
6f79b219 JJ |
6524 | 7. SHF_TLS sections are only in PT_TLS or PT_LOAD segments. |
6525 | 8. PT_DYNAMIC should not contain empty sections at the beginning | |
08a40648 | 6526 | (with the possible exception of .dynamic). */ |
9f17e2a6 | 6527 | #define IS_SECTION_IN_INPUT_SEGMENT(section, segment, bed) \ |
2b05f1b7 L |
6528 | ((((segment->p_paddr \ |
6529 | ? IS_CONTAINED_BY_LMA (section, segment, segment->p_paddr) \ | |
6530 | : IS_CONTAINED_BY_VMA (section, segment)) \ | |
6531 | && (section->flags & SEC_ALLOC) != 0) \ | |
0efc80c8 | 6532 | || IS_NOTE (segment, section)) \ |
2b05f1b7 L |
6533 | && segment->p_type != PT_GNU_STACK \ |
6534 | && (segment->p_type != PT_TLS \ | |
6535 | || (section->flags & SEC_THREAD_LOCAL)) \ | |
6536 | && (segment->p_type == PT_LOAD \ | |
6537 | || segment->p_type == PT_TLS \ | |
6538 | || (section->flags & SEC_THREAD_LOCAL) == 0) \ | |
6539 | && (segment->p_type != PT_DYNAMIC \ | |
6540 | || SECTION_SIZE (section, segment) > 0 \ | |
6541 | || (segment->p_paddr \ | |
6542 | ? segment->p_paddr != section->lma \ | |
6543 | : segment->p_vaddr != section->vma) \ | |
6544 | || (strcmp (bfd_get_section_name (ibfd, section), ".dynamic") \ | |
6545 | == 0)) \ | |
0067a569 | 6546 | && !section->segment_mark) |
bc67d8a6 | 6547 | |
9f17e2a6 L |
6548 | /* If the output section of a section in the input segment is NULL, |
6549 | it is removed from the corresponding output segment. */ | |
6550 | #define INCLUDE_SECTION_IN_SEGMENT(section, segment, bed) \ | |
6551 | (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed) \ | |
6552 | && section->output_section != NULL) | |
6553 | ||
b34976b6 | 6554 | /* Returns TRUE iff seg1 starts after the end of seg2. */ |
b5f852ea NC |
6555 | #define SEGMENT_AFTER_SEGMENT(seg1, seg2, field) \ |
6556 | (seg1->field >= SEGMENT_END (seg2, seg2->field)) | |
6557 | ||
6558 | /* Returns TRUE iff seg1 and seg2 overlap. Segments overlap iff both | |
6559 | their VMA address ranges and their LMA address ranges overlap. | |
6560 | It is possible to have overlapping VMA ranges without overlapping LMA | |
6561 | ranges. RedBoot images for example can have both .data and .bss mapped | |
6562 | to the same VMA range, but with the .data section mapped to a different | |
6563 | LMA. */ | |
aecc8f8a | 6564 | #define SEGMENT_OVERLAPS(seg1, seg2) \ |
b5f852ea | 6565 | ( !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_vaddr) \ |
08a40648 | 6566 | || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_vaddr)) \ |
b5f852ea | 6567 | && !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_paddr) \ |
08a40648 | 6568 | || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_paddr))) |
bc67d8a6 NC |
6569 | |
6570 | /* Initialise the segment mark field. */ | |
6571 | for (section = ibfd->sections; section != NULL; section = section->next) | |
b34976b6 | 6572 | section->segment_mark = FALSE; |
bc67d8a6 | 6573 | |
5c44b38e AM |
6574 | /* The Solaris linker creates program headers in which all the |
6575 | p_paddr fields are zero. When we try to objcopy or strip such a | |
6576 | file, we get confused. Check for this case, and if we find it | |
6577 | don't set the p_paddr_valid fields. */ | |
6578 | p_paddr_valid = FALSE; | |
6579 | for (i = 0, segment = elf_tdata (ibfd)->phdr; | |
6580 | i < num_segments; | |
6581 | i++, segment++) | |
6582 | if (segment->p_paddr != 0) | |
6583 | { | |
6584 | p_paddr_valid = TRUE; | |
6585 | break; | |
6586 | } | |
6587 | ||
252b5132 | 6588 | /* Scan through the segments specified in the program header |
bc67d8a6 | 6589 | of the input BFD. For this first scan we look for overlaps |
9ad5cbcf | 6590 | in the loadable segments. These can be created by weird |
aecc8f8a | 6591 | parameters to objcopy. Also, fix some solaris weirdness. */ |
bc67d8a6 NC |
6592 | for (i = 0, segment = elf_tdata (ibfd)->phdr; |
6593 | i < num_segments; | |
c044fabd | 6594 | i++, segment++) |
252b5132 | 6595 | { |
252b5132 | 6596 | unsigned int j; |
c044fabd | 6597 | Elf_Internal_Phdr *segment2; |
252b5132 | 6598 | |
aecc8f8a AM |
6599 | if (segment->p_type == PT_INTERP) |
6600 | for (section = ibfd->sections; section; section = section->next) | |
6601 | if (IS_SOLARIS_PT_INTERP (segment, section)) | |
6602 | { | |
6603 | /* Mininal change so that the normal section to segment | |
4cc11e76 | 6604 | assignment code will work. */ |
aecc8f8a AM |
6605 | segment->p_vaddr = section->vma; |
6606 | break; | |
6607 | } | |
6608 | ||
bc67d8a6 | 6609 | if (segment->p_type != PT_LOAD) |
b10a8ae0 L |
6610 | { |
6611 | /* Remove PT_GNU_RELRO segment. */ | |
6612 | if (segment->p_type == PT_GNU_RELRO) | |
6613 | segment->p_type = PT_NULL; | |
6614 | continue; | |
6615 | } | |
c044fabd | 6616 | |
bc67d8a6 | 6617 | /* Determine if this segment overlaps any previous segments. */ |
0067a569 | 6618 | for (j = 0, segment2 = elf_tdata (ibfd)->phdr; j < i; j++, segment2++) |
bc67d8a6 NC |
6619 | { |
6620 | bfd_signed_vma extra_length; | |
c044fabd | 6621 | |
bc67d8a6 | 6622 | if (segment2->p_type != PT_LOAD |
0067a569 | 6623 | || !SEGMENT_OVERLAPS (segment, segment2)) |
bc67d8a6 | 6624 | continue; |
c044fabd | 6625 | |
bc67d8a6 NC |
6626 | /* Merge the two segments together. */ |
6627 | if (segment2->p_vaddr < segment->p_vaddr) | |
6628 | { | |
c044fabd | 6629 | /* Extend SEGMENT2 to include SEGMENT and then delete |
08a40648 | 6630 | SEGMENT. */ |
0067a569 AM |
6631 | extra_length = (SEGMENT_END (segment, segment->p_vaddr) |
6632 | - SEGMENT_END (segment2, segment2->p_vaddr)); | |
c044fabd | 6633 | |
bc67d8a6 NC |
6634 | if (extra_length > 0) |
6635 | { | |
0067a569 | 6636 | segment2->p_memsz += extra_length; |
bc67d8a6 NC |
6637 | segment2->p_filesz += extra_length; |
6638 | } | |
c044fabd | 6639 | |
bc67d8a6 | 6640 | segment->p_type = PT_NULL; |
c044fabd | 6641 | |
bc67d8a6 NC |
6642 | /* Since we have deleted P we must restart the outer loop. */ |
6643 | i = 0; | |
6644 | segment = elf_tdata (ibfd)->phdr; | |
6645 | break; | |
6646 | } | |
6647 | else | |
6648 | { | |
c044fabd | 6649 | /* Extend SEGMENT to include SEGMENT2 and then delete |
08a40648 | 6650 | SEGMENT2. */ |
0067a569 AM |
6651 | extra_length = (SEGMENT_END (segment2, segment2->p_vaddr) |
6652 | - SEGMENT_END (segment, segment->p_vaddr)); | |
c044fabd | 6653 | |
bc67d8a6 NC |
6654 | if (extra_length > 0) |
6655 | { | |
0067a569 | 6656 | segment->p_memsz += extra_length; |
bc67d8a6 NC |
6657 | segment->p_filesz += extra_length; |
6658 | } | |
c044fabd | 6659 | |
bc67d8a6 NC |
6660 | segment2->p_type = PT_NULL; |
6661 | } | |
6662 | } | |
6663 | } | |
c044fabd | 6664 | |
bc67d8a6 NC |
6665 | /* The second scan attempts to assign sections to segments. */ |
6666 | for (i = 0, segment = elf_tdata (ibfd)->phdr; | |
6667 | i < num_segments; | |
0067a569 | 6668 | i++, segment++) |
bc67d8a6 | 6669 | { |
0067a569 AM |
6670 | unsigned int section_count; |
6671 | asection **sections; | |
6672 | asection *output_section; | |
6673 | unsigned int isec; | |
6674 | bfd_vma matching_lma; | |
6675 | bfd_vma suggested_lma; | |
6676 | unsigned int j; | |
dc810e39 | 6677 | bfd_size_type amt; |
0067a569 AM |
6678 | asection *first_section; |
6679 | bfd_boolean first_matching_lma; | |
6680 | bfd_boolean first_suggested_lma; | |
bc67d8a6 NC |
6681 | |
6682 | if (segment->p_type == PT_NULL) | |
6683 | continue; | |
c044fabd | 6684 | |
9f17e2a6 | 6685 | first_section = NULL; |
bc67d8a6 | 6686 | /* Compute how many sections might be placed into this segment. */ |
b5f852ea NC |
6687 | for (section = ibfd->sections, section_count = 0; |
6688 | section != NULL; | |
6689 | section = section->next) | |
9f17e2a6 L |
6690 | { |
6691 | /* Find the first section in the input segment, which may be | |
6692 | removed from the corresponding output segment. */ | |
6693 | if (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed)) | |
6694 | { | |
6695 | if (first_section == NULL) | |
6696 | first_section = section; | |
6697 | if (section->output_section != NULL) | |
6698 | ++section_count; | |
6699 | } | |
6700 | } | |
811072d8 | 6701 | |
b5f852ea NC |
6702 | /* Allocate a segment map big enough to contain |
6703 | all of the sections we have selected. */ | |
dc810e39 AM |
6704 | amt = sizeof (struct elf_segment_map); |
6705 | amt += ((bfd_size_type) section_count - 1) * sizeof (asection *); | |
a50b1753 | 6706 | map = (struct elf_segment_map *) bfd_zalloc (obfd, amt); |
bc67d8a6 | 6707 | if (map == NULL) |
b34976b6 | 6708 | return FALSE; |
252b5132 RH |
6709 | |
6710 | /* Initialise the fields of the segment map. Default to | |
6711 | using the physical address of the segment in the input BFD. */ | |
0067a569 AM |
6712 | map->next = NULL; |
6713 | map->p_type = segment->p_type; | |
6714 | map->p_flags = segment->p_flags; | |
bc67d8a6 | 6715 | map->p_flags_valid = 1; |
55d55ac7 | 6716 | |
9f17e2a6 L |
6717 | /* If the first section in the input segment is removed, there is |
6718 | no need to preserve segment physical address in the corresponding | |
6719 | output segment. */ | |
945c025a | 6720 | if (!first_section || first_section->output_section != NULL) |
9f17e2a6 L |
6721 | { |
6722 | map->p_paddr = segment->p_paddr; | |
5c44b38e | 6723 | map->p_paddr_valid = p_paddr_valid; |
9f17e2a6 | 6724 | } |
252b5132 RH |
6725 | |
6726 | /* Determine if this segment contains the ELF file header | |
6727 | and if it contains the program headers themselves. */ | |
bc67d8a6 NC |
6728 | map->includes_filehdr = (segment->p_offset == 0 |
6729 | && segment->p_filesz >= iehdr->e_ehsize); | |
bc67d8a6 | 6730 | map->includes_phdrs = 0; |
252b5132 | 6731 | |
0067a569 | 6732 | if (!phdr_included || segment->p_type != PT_LOAD) |
252b5132 | 6733 | { |
bc67d8a6 NC |
6734 | map->includes_phdrs = |
6735 | (segment->p_offset <= (bfd_vma) iehdr->e_phoff | |
6736 | && (segment->p_offset + segment->p_filesz | |
252b5132 RH |
6737 | >= ((bfd_vma) iehdr->e_phoff |
6738 | + iehdr->e_phnum * iehdr->e_phentsize))); | |
c044fabd | 6739 | |
bc67d8a6 | 6740 | if (segment->p_type == PT_LOAD && map->includes_phdrs) |
b34976b6 | 6741 | phdr_included = TRUE; |
252b5132 RH |
6742 | } |
6743 | ||
bc67d8a6 | 6744 | if (section_count == 0) |
252b5132 RH |
6745 | { |
6746 | /* Special segments, such as the PT_PHDR segment, may contain | |
6747 | no sections, but ordinary, loadable segments should contain | |
1ed89aa9 | 6748 | something. They are allowed by the ELF spec however, so only |
f98450c6 NC |
6749 | a warning is produced. |
6750 | There is however the valid use case of embedded systems which | |
6751 | have segments with p_filesz of 0 and a p_memsz > 0 to initialize | |
6752 | flash memory with zeros. No warning is shown for that case. */ | |
6753 | if (segment->p_type == PT_LOAD | |
6754 | && (segment->p_filesz > 0 || segment->p_memsz == 0)) | |
6755 | /* xgettext:c-format */ | |
4eca0228 | 6756 | _bfd_error_handler (_("\ |
f98450c6 NC |
6757 | %B: warning: Empty loadable segment detected at vaddr=0x%.8x, is this intentional ?"), |
6758 | ibfd, segment->p_vaddr); | |
252b5132 | 6759 | |
bc67d8a6 | 6760 | map->count = 0; |
c044fabd KH |
6761 | *pointer_to_map = map; |
6762 | pointer_to_map = &map->next; | |
252b5132 RH |
6763 | |
6764 | continue; | |
6765 | } | |
6766 | ||
6767 | /* Now scan the sections in the input BFD again and attempt | |
6768 | to add their corresponding output sections to the segment map. | |
6769 | The problem here is how to handle an output section which has | |
6770 | been moved (ie had its LMA changed). There are four possibilities: | |
6771 | ||
6772 | 1. None of the sections have been moved. | |
6773 | In this case we can continue to use the segment LMA from the | |
6774 | input BFD. | |
6775 | ||
6776 | 2. All of the sections have been moved by the same amount. | |
6777 | In this case we can change the segment's LMA to match the LMA | |
6778 | of the first section. | |
6779 | ||
6780 | 3. Some of the sections have been moved, others have not. | |
6781 | In this case those sections which have not been moved can be | |
6782 | placed in the current segment which will have to have its size, | |
6783 | and possibly its LMA changed, and a new segment or segments will | |
6784 | have to be created to contain the other sections. | |
6785 | ||
b5f852ea | 6786 | 4. The sections have been moved, but not by the same amount. |
252b5132 RH |
6787 | In this case we can change the segment's LMA to match the LMA |
6788 | of the first section and we will have to create a new segment | |
6789 | or segments to contain the other sections. | |
6790 | ||
6791 | In order to save time, we allocate an array to hold the section | |
6792 | pointers that we are interested in. As these sections get assigned | |
6793 | to a segment, they are removed from this array. */ | |
6794 | ||
a50b1753 | 6795 | sections = (asection **) bfd_malloc2 (section_count, sizeof (asection *)); |
252b5132 | 6796 | if (sections == NULL) |
b34976b6 | 6797 | return FALSE; |
252b5132 RH |
6798 | |
6799 | /* Step One: Scan for segment vs section LMA conflicts. | |
6800 | Also add the sections to the section array allocated above. | |
6801 | Also add the sections to the current segment. In the common | |
6802 | case, where the sections have not been moved, this means that | |
6803 | we have completely filled the segment, and there is nothing | |
6804 | more to do. */ | |
252b5132 | 6805 | isec = 0; |
72730e0c | 6806 | matching_lma = 0; |
252b5132 | 6807 | suggested_lma = 0; |
0067a569 AM |
6808 | first_matching_lma = TRUE; |
6809 | first_suggested_lma = TRUE; | |
252b5132 | 6810 | |
461c4b2e | 6811 | for (section = first_section, j = 0; |
bc67d8a6 NC |
6812 | section != NULL; |
6813 | section = section->next) | |
252b5132 | 6814 | { |
caf47ea6 | 6815 | if (INCLUDE_SECTION_IN_SEGMENT (section, segment, bed)) |
c0f7859b | 6816 | { |
bc67d8a6 NC |
6817 | output_section = section->output_section; |
6818 | ||
0067a569 | 6819 | sections[j++] = section; |
252b5132 RH |
6820 | |
6821 | /* The Solaris native linker always sets p_paddr to 0. | |
6822 | We try to catch that case here, and set it to the | |
5e8d7549 NC |
6823 | correct value. Note - some backends require that |
6824 | p_paddr be left as zero. */ | |
5c44b38e | 6825 | if (!p_paddr_valid |
4455705d | 6826 | && segment->p_vaddr != 0 |
0067a569 | 6827 | && !bed->want_p_paddr_set_to_zero |
252b5132 | 6828 | && isec == 0 |
bc67d8a6 | 6829 | && output_section->lma != 0 |
0067a569 AM |
6830 | && output_section->vma == (segment->p_vaddr |
6831 | + (map->includes_filehdr | |
6832 | ? iehdr->e_ehsize | |
6833 | : 0) | |
6834 | + (map->includes_phdrs | |
6835 | ? (iehdr->e_phnum | |
6836 | * iehdr->e_phentsize) | |
6837 | : 0))) | |
bc67d8a6 | 6838 | map->p_paddr = segment->p_vaddr; |
252b5132 RH |
6839 | |
6840 | /* Match up the physical address of the segment with the | |
6841 | LMA address of the output section. */ | |
bc67d8a6 | 6842 | if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr) |
5e8d7549 | 6843 | || IS_COREFILE_NOTE (segment, section) |
0067a569 AM |
6844 | || (bed->want_p_paddr_set_to_zero |
6845 | && IS_CONTAINED_BY_VMA (output_section, segment))) | |
252b5132 | 6846 | { |
0067a569 AM |
6847 | if (first_matching_lma || output_section->lma < matching_lma) |
6848 | { | |
6849 | matching_lma = output_section->lma; | |
6850 | first_matching_lma = FALSE; | |
6851 | } | |
252b5132 RH |
6852 | |
6853 | /* We assume that if the section fits within the segment | |
bc67d8a6 | 6854 | then it does not overlap any other section within that |
252b5132 | 6855 | segment. */ |
0067a569 AM |
6856 | map->sections[isec++] = output_section; |
6857 | } | |
6858 | else if (first_suggested_lma) | |
6859 | { | |
6860 | suggested_lma = output_section->lma; | |
6861 | first_suggested_lma = FALSE; | |
252b5132 | 6862 | } |
147d51c2 L |
6863 | |
6864 | if (j == section_count) | |
6865 | break; | |
252b5132 RH |
6866 | } |
6867 | } | |
6868 | ||
bc67d8a6 | 6869 | BFD_ASSERT (j == section_count); |
252b5132 RH |
6870 | |
6871 | /* Step Two: Adjust the physical address of the current segment, | |
6872 | if necessary. */ | |
bc67d8a6 | 6873 | if (isec == section_count) |
252b5132 RH |
6874 | { |
6875 | /* All of the sections fitted within the segment as currently | |
6876 | specified. This is the default case. Add the segment to | |
6877 | the list of built segments and carry on to process the next | |
6878 | program header in the input BFD. */ | |
bc67d8a6 | 6879 | map->count = section_count; |
c044fabd KH |
6880 | *pointer_to_map = map; |
6881 | pointer_to_map = &map->next; | |
08a40648 | 6882 | |
5c44b38e AM |
6883 | if (p_paddr_valid |
6884 | && !bed->want_p_paddr_set_to_zero | |
147d51c2 | 6885 | && matching_lma != map->p_paddr |
5c44b38e AM |
6886 | && !map->includes_filehdr |
6887 | && !map->includes_phdrs) | |
3271a814 NS |
6888 | /* There is some padding before the first section in the |
6889 | segment. So, we must account for that in the output | |
6890 | segment's vma. */ | |
6891 | map->p_vaddr_offset = matching_lma - map->p_paddr; | |
08a40648 | 6892 | |
252b5132 RH |
6893 | free (sections); |
6894 | continue; | |
6895 | } | |
252b5132 RH |
6896 | else |
6897 | { | |
0067a569 | 6898 | if (!first_matching_lma) |
72730e0c AM |
6899 | { |
6900 | /* At least one section fits inside the current segment. | |
6901 | Keep it, but modify its physical address to match the | |
6902 | LMA of the first section that fitted. */ | |
bc67d8a6 | 6903 | map->p_paddr = matching_lma; |
72730e0c AM |
6904 | } |
6905 | else | |
6906 | { | |
6907 | /* None of the sections fitted inside the current segment. | |
6908 | Change the current segment's physical address to match | |
6909 | the LMA of the first section. */ | |
bc67d8a6 | 6910 | map->p_paddr = suggested_lma; |
72730e0c AM |
6911 | } |
6912 | ||
bc67d8a6 NC |
6913 | /* Offset the segment physical address from the lma |
6914 | to allow for space taken up by elf headers. */ | |
6915 | if (map->includes_filehdr) | |
010c8431 AM |
6916 | { |
6917 | if (map->p_paddr >= iehdr->e_ehsize) | |
6918 | map->p_paddr -= iehdr->e_ehsize; | |
6919 | else | |
6920 | { | |
6921 | map->includes_filehdr = FALSE; | |
6922 | map->includes_phdrs = FALSE; | |
6923 | } | |
6924 | } | |
252b5132 | 6925 | |
bc67d8a6 NC |
6926 | if (map->includes_phdrs) |
6927 | { | |
010c8431 AM |
6928 | if (map->p_paddr >= iehdr->e_phnum * iehdr->e_phentsize) |
6929 | { | |
6930 | map->p_paddr -= iehdr->e_phnum * iehdr->e_phentsize; | |
6931 | ||
6932 | /* iehdr->e_phnum is just an estimate of the number | |
6933 | of program headers that we will need. Make a note | |
6934 | here of the number we used and the segment we chose | |
6935 | to hold these headers, so that we can adjust the | |
6936 | offset when we know the correct value. */ | |
6937 | phdr_adjust_num = iehdr->e_phnum; | |
6938 | phdr_adjust_seg = map; | |
6939 | } | |
6940 | else | |
6941 | map->includes_phdrs = FALSE; | |
bc67d8a6 | 6942 | } |
252b5132 RH |
6943 | } |
6944 | ||
6945 | /* Step Three: Loop over the sections again, this time assigning | |
caf47ea6 | 6946 | those that fit to the current segment and removing them from the |
252b5132 RH |
6947 | sections array; but making sure not to leave large gaps. Once all |
6948 | possible sections have been assigned to the current segment it is | |
6949 | added to the list of built segments and if sections still remain | |
6950 | to be assigned, a new segment is constructed before repeating | |
6951 | the loop. */ | |
6952 | isec = 0; | |
6953 | do | |
6954 | { | |
bc67d8a6 | 6955 | map->count = 0; |
252b5132 | 6956 | suggested_lma = 0; |
0067a569 | 6957 | first_suggested_lma = TRUE; |
252b5132 RH |
6958 | |
6959 | /* Fill the current segment with sections that fit. */ | |
bc67d8a6 | 6960 | for (j = 0; j < section_count; j++) |
252b5132 | 6961 | { |
bc67d8a6 | 6962 | section = sections[j]; |
252b5132 | 6963 | |
bc67d8a6 | 6964 | if (section == NULL) |
252b5132 RH |
6965 | continue; |
6966 | ||
bc67d8a6 | 6967 | output_section = section->output_section; |
252b5132 | 6968 | |
bc67d8a6 | 6969 | BFD_ASSERT (output_section != NULL); |
c044fabd | 6970 | |
bc67d8a6 NC |
6971 | if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr) |
6972 | || IS_COREFILE_NOTE (segment, section)) | |
252b5132 | 6973 | { |
bc67d8a6 | 6974 | if (map->count == 0) |
252b5132 RH |
6975 | { |
6976 | /* If the first section in a segment does not start at | |
bc67d8a6 NC |
6977 | the beginning of the segment, then something is |
6978 | wrong. */ | |
0067a569 AM |
6979 | if (output_section->lma |
6980 | != (map->p_paddr | |
6981 | + (map->includes_filehdr ? iehdr->e_ehsize : 0) | |
6982 | + (map->includes_phdrs | |
6983 | ? iehdr->e_phnum * iehdr->e_phentsize | |
6984 | : 0))) | |
252b5132 RH |
6985 | abort (); |
6986 | } | |
6987 | else | |
6988 | { | |
0067a569 | 6989 | asection *prev_sec; |
252b5132 | 6990 | |
bc67d8a6 | 6991 | prev_sec = map->sections[map->count - 1]; |
252b5132 RH |
6992 | |
6993 | /* If the gap between the end of the previous section | |
bc67d8a6 NC |
6994 | and the start of this section is more than |
6995 | maxpagesize then we need to start a new segment. */ | |
eea6121a | 6996 | if ((BFD_ALIGN (prev_sec->lma + prev_sec->size, |
079e9a2f | 6997 | maxpagesize) |
caf47ea6 | 6998 | < BFD_ALIGN (output_section->lma, maxpagesize)) |
0067a569 | 6999 | || (prev_sec->lma + prev_sec->size |
079e9a2f | 7000 | > output_section->lma)) |
252b5132 | 7001 | { |
0067a569 AM |
7002 | if (first_suggested_lma) |
7003 | { | |
7004 | suggested_lma = output_section->lma; | |
7005 | first_suggested_lma = FALSE; | |
7006 | } | |
252b5132 RH |
7007 | |
7008 | continue; | |
7009 | } | |
7010 | } | |
7011 | ||
bc67d8a6 | 7012 | map->sections[map->count++] = output_section; |
252b5132 RH |
7013 | ++isec; |
7014 | sections[j] = NULL; | |
b34976b6 | 7015 | section->segment_mark = TRUE; |
252b5132 | 7016 | } |
0067a569 AM |
7017 | else if (first_suggested_lma) |
7018 | { | |
7019 | suggested_lma = output_section->lma; | |
7020 | first_suggested_lma = FALSE; | |
7021 | } | |
252b5132 RH |
7022 | } |
7023 | ||
bc67d8a6 | 7024 | BFD_ASSERT (map->count > 0); |
252b5132 RH |
7025 | |
7026 | /* Add the current segment to the list of built segments. */ | |
c044fabd KH |
7027 | *pointer_to_map = map; |
7028 | pointer_to_map = &map->next; | |
252b5132 | 7029 | |
bc67d8a6 | 7030 | if (isec < section_count) |
252b5132 RH |
7031 | { |
7032 | /* We still have not allocated all of the sections to | |
7033 | segments. Create a new segment here, initialise it | |
7034 | and carry on looping. */ | |
dc810e39 AM |
7035 | amt = sizeof (struct elf_segment_map); |
7036 | amt += ((bfd_size_type) section_count - 1) * sizeof (asection *); | |
5964fc3a | 7037 | map = (struct elf_segment_map *) bfd_zalloc (obfd, amt); |
bc67d8a6 | 7038 | if (map == NULL) |
5ed6aba4 NC |
7039 | { |
7040 | free (sections); | |
7041 | return FALSE; | |
7042 | } | |
252b5132 RH |
7043 | |
7044 | /* Initialise the fields of the segment map. Set the physical | |
7045 | physical address to the LMA of the first section that has | |
7046 | not yet been assigned. */ | |
0067a569 AM |
7047 | map->next = NULL; |
7048 | map->p_type = segment->p_type; | |
7049 | map->p_flags = segment->p_flags; | |
7050 | map->p_flags_valid = 1; | |
7051 | map->p_paddr = suggested_lma; | |
5c44b38e | 7052 | map->p_paddr_valid = p_paddr_valid; |
bc67d8a6 | 7053 | map->includes_filehdr = 0; |
0067a569 | 7054 | map->includes_phdrs = 0; |
252b5132 RH |
7055 | } |
7056 | } | |
bc67d8a6 | 7057 | while (isec < section_count); |
252b5132 RH |
7058 | |
7059 | free (sections); | |
7060 | } | |
7061 | ||
12bd6957 | 7062 | elf_seg_map (obfd) = map_first; |
bc67d8a6 NC |
7063 | |
7064 | /* If we had to estimate the number of program headers that were | |
9ad5cbcf | 7065 | going to be needed, then check our estimate now and adjust |
bc67d8a6 NC |
7066 | the offset if necessary. */ |
7067 | if (phdr_adjust_seg != NULL) | |
7068 | { | |
7069 | unsigned int count; | |
c044fabd | 7070 | |
bc67d8a6 | 7071 | for (count = 0, map = map_first; map != NULL; map = map->next) |
c044fabd | 7072 | count++; |
252b5132 | 7073 | |
bc67d8a6 NC |
7074 | if (count > phdr_adjust_num) |
7075 | phdr_adjust_seg->p_paddr | |
7076 | -= (count - phdr_adjust_num) * iehdr->e_phentsize; | |
7077 | } | |
c044fabd | 7078 | |
bc67d8a6 | 7079 | #undef SEGMENT_END |
eecdbe52 | 7080 | #undef SECTION_SIZE |
bc67d8a6 NC |
7081 | #undef IS_CONTAINED_BY_VMA |
7082 | #undef IS_CONTAINED_BY_LMA | |
0efc80c8 | 7083 | #undef IS_NOTE |
252b5132 | 7084 | #undef IS_COREFILE_NOTE |
bc67d8a6 | 7085 | #undef IS_SOLARIS_PT_INTERP |
9f17e2a6 | 7086 | #undef IS_SECTION_IN_INPUT_SEGMENT |
bc67d8a6 NC |
7087 | #undef INCLUDE_SECTION_IN_SEGMENT |
7088 | #undef SEGMENT_AFTER_SEGMENT | |
7089 | #undef SEGMENT_OVERLAPS | |
b34976b6 | 7090 | return TRUE; |
252b5132 RH |
7091 | } |
7092 | ||
84d1d650 L |
7093 | /* Copy ELF program header information. */ |
7094 | ||
7095 | static bfd_boolean | |
7096 | copy_elf_program_header (bfd *ibfd, bfd *obfd) | |
7097 | { | |
7098 | Elf_Internal_Ehdr *iehdr; | |
7099 | struct elf_segment_map *map; | |
7100 | struct elf_segment_map *map_first; | |
7101 | struct elf_segment_map **pointer_to_map; | |
7102 | Elf_Internal_Phdr *segment; | |
7103 | unsigned int i; | |
7104 | unsigned int num_segments; | |
7105 | bfd_boolean phdr_included = FALSE; | |
88967714 | 7106 | bfd_boolean p_paddr_valid; |
84d1d650 L |
7107 | |
7108 | iehdr = elf_elfheader (ibfd); | |
7109 | ||
7110 | map_first = NULL; | |
7111 | pointer_to_map = &map_first; | |
7112 | ||
88967714 AM |
7113 | /* If all the segment p_paddr fields are zero, don't set |
7114 | map->p_paddr_valid. */ | |
7115 | p_paddr_valid = FALSE; | |
84d1d650 | 7116 | num_segments = elf_elfheader (ibfd)->e_phnum; |
88967714 AM |
7117 | for (i = 0, segment = elf_tdata (ibfd)->phdr; |
7118 | i < num_segments; | |
7119 | i++, segment++) | |
7120 | if (segment->p_paddr != 0) | |
7121 | { | |
7122 | p_paddr_valid = TRUE; | |
7123 | break; | |
7124 | } | |
7125 | ||
84d1d650 L |
7126 | for (i = 0, segment = elf_tdata (ibfd)->phdr; |
7127 | i < num_segments; | |
7128 | i++, segment++) | |
7129 | { | |
7130 | asection *section; | |
7131 | unsigned int section_count; | |
7132 | bfd_size_type amt; | |
7133 | Elf_Internal_Shdr *this_hdr; | |
53020534 | 7134 | asection *first_section = NULL; |
a76e6f2f | 7135 | asection *lowest_section; |
84d1d650 | 7136 | |
84d1d650 L |
7137 | /* Compute how many sections are in this segment. */ |
7138 | for (section = ibfd->sections, section_count = 0; | |
7139 | section != NULL; | |
7140 | section = section->next) | |
7141 | { | |
7142 | this_hdr = &(elf_section_data(section)->this_hdr); | |
f4638467 | 7143 | if (ELF_SECTION_IN_SEGMENT (this_hdr, segment)) |
3271a814 | 7144 | { |
a76e6f2f AM |
7145 | if (first_section == NULL) |
7146 | first_section = section; | |
3271a814 NS |
7147 | section_count++; |
7148 | } | |
84d1d650 L |
7149 | } |
7150 | ||
7151 | /* Allocate a segment map big enough to contain | |
7152 | all of the sections we have selected. */ | |
7153 | amt = sizeof (struct elf_segment_map); | |
7154 | if (section_count != 0) | |
7155 | amt += ((bfd_size_type) section_count - 1) * sizeof (asection *); | |
a50b1753 | 7156 | map = (struct elf_segment_map *) bfd_zalloc (obfd, amt); |
84d1d650 L |
7157 | if (map == NULL) |
7158 | return FALSE; | |
7159 | ||
7160 | /* Initialize the fields of the output segment map with the | |
7161 | input segment. */ | |
7162 | map->next = NULL; | |
7163 | map->p_type = segment->p_type; | |
7164 | map->p_flags = segment->p_flags; | |
7165 | map->p_flags_valid = 1; | |
7166 | map->p_paddr = segment->p_paddr; | |
88967714 | 7167 | map->p_paddr_valid = p_paddr_valid; |
3f570048 AM |
7168 | map->p_align = segment->p_align; |
7169 | map->p_align_valid = 1; | |
3271a814 | 7170 | map->p_vaddr_offset = 0; |
84d1d650 | 7171 | |
04c3a755 NS |
7172 | if (map->p_type == PT_GNU_RELRO |
7173 | || map->p_type == PT_GNU_STACK) | |
b10a8ae0 L |
7174 | { |
7175 | /* The PT_GNU_RELRO segment may contain the first a few | |
7176 | bytes in the .got.plt section even if the whole .got.plt | |
7177 | section isn't in the PT_GNU_RELRO segment. We won't | |
04c3a755 NS |
7178 | change the size of the PT_GNU_RELRO segment. |
7179 | Similarly, PT_GNU_STACK size is significant on uclinux | |
7180 | systems. */ | |
9433b9b1 | 7181 | map->p_size = segment->p_memsz; |
b10a8ae0 L |
7182 | map->p_size_valid = 1; |
7183 | } | |
7184 | ||
84d1d650 L |
7185 | /* Determine if this segment contains the ELF file header |
7186 | and if it contains the program headers themselves. */ | |
7187 | map->includes_filehdr = (segment->p_offset == 0 | |
7188 | && segment->p_filesz >= iehdr->e_ehsize); | |
7189 | ||
7190 | map->includes_phdrs = 0; | |
7191 | if (! phdr_included || segment->p_type != PT_LOAD) | |
7192 | { | |
7193 | map->includes_phdrs = | |
7194 | (segment->p_offset <= (bfd_vma) iehdr->e_phoff | |
7195 | && (segment->p_offset + segment->p_filesz | |
7196 | >= ((bfd_vma) iehdr->e_phoff | |
7197 | + iehdr->e_phnum * iehdr->e_phentsize))); | |
7198 | ||
7199 | if (segment->p_type == PT_LOAD && map->includes_phdrs) | |
7200 | phdr_included = TRUE; | |
7201 | } | |
7202 | ||
bbefd0a9 | 7203 | lowest_section = NULL; |
84d1d650 L |
7204 | if (section_count != 0) |
7205 | { | |
7206 | unsigned int isec = 0; | |
7207 | ||
53020534 | 7208 | for (section = first_section; |
84d1d650 L |
7209 | section != NULL; |
7210 | section = section->next) | |
7211 | { | |
7212 | this_hdr = &(elf_section_data(section)->this_hdr); | |
f4638467 | 7213 | if (ELF_SECTION_IN_SEGMENT (this_hdr, segment)) |
53020534 L |
7214 | { |
7215 | map->sections[isec++] = section->output_section; | |
a76e6f2f AM |
7216 | if ((section->flags & SEC_ALLOC) != 0) |
7217 | { | |
7218 | bfd_vma seg_off; | |
7219 | ||
bbefd0a9 AM |
7220 | if (lowest_section == NULL |
7221 | || section->lma < lowest_section->lma) | |
fb8a5684 AM |
7222 | lowest_section = section; |
7223 | ||
a76e6f2f AM |
7224 | /* Section lmas are set up from PT_LOAD header |
7225 | p_paddr in _bfd_elf_make_section_from_shdr. | |
7226 | If this header has a p_paddr that disagrees | |
7227 | with the section lma, flag the p_paddr as | |
7228 | invalid. */ | |
7229 | if ((section->flags & SEC_LOAD) != 0) | |
7230 | seg_off = this_hdr->sh_offset - segment->p_offset; | |
7231 | else | |
7232 | seg_off = this_hdr->sh_addr - segment->p_vaddr; | |
7233 | if (section->lma - segment->p_paddr != seg_off) | |
7234 | map->p_paddr_valid = FALSE; | |
7235 | } | |
53020534 L |
7236 | if (isec == section_count) |
7237 | break; | |
7238 | } | |
84d1d650 L |
7239 | } |
7240 | } | |
7241 | ||
a76e6f2f AM |
7242 | if (map->includes_filehdr && lowest_section != NULL) |
7243 | /* We need to keep the space used by the headers fixed. */ | |
7244 | map->header_size = lowest_section->vma - segment->p_vaddr; | |
d324f6d6 | 7245 | |
a76e6f2f AM |
7246 | if (!map->includes_phdrs |
7247 | && !map->includes_filehdr | |
7248 | && map->p_paddr_valid) | |
7249 | /* There is some other padding before the first section. */ | |
7250 | map->p_vaddr_offset = ((lowest_section ? lowest_section->lma : 0) | |
7251 | - segment->p_paddr); | |
7252 | ||
84d1d650 L |
7253 | map->count = section_count; |
7254 | *pointer_to_map = map; | |
7255 | pointer_to_map = &map->next; | |
7256 | } | |
7257 | ||
12bd6957 | 7258 | elf_seg_map (obfd) = map_first; |
84d1d650 L |
7259 | return TRUE; |
7260 | } | |
7261 | ||
7262 | /* Copy private BFD data. This copies or rewrites ELF program header | |
7263 | information. */ | |
7264 | ||
7265 | static bfd_boolean | |
7266 | copy_private_bfd_data (bfd *ibfd, bfd *obfd) | |
7267 | { | |
84d1d650 L |
7268 | if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour |
7269 | || bfd_get_flavour (obfd) != bfd_target_elf_flavour) | |
7270 | return TRUE; | |
7271 | ||
7272 | if (elf_tdata (ibfd)->phdr == NULL) | |
7273 | return TRUE; | |
7274 | ||
7275 | if (ibfd->xvec == obfd->xvec) | |
7276 | { | |
cb3ff1e5 NC |
7277 | /* Check to see if any sections in the input BFD |
7278 | covered by ELF program header have changed. */ | |
d55ce4e2 | 7279 | Elf_Internal_Phdr *segment; |
84d1d650 L |
7280 | asection *section, *osec; |
7281 | unsigned int i, num_segments; | |
7282 | Elf_Internal_Shdr *this_hdr; | |
147d51c2 L |
7283 | const struct elf_backend_data *bed; |
7284 | ||
7285 | bed = get_elf_backend_data (ibfd); | |
7286 | ||
7287 | /* Regenerate the segment map if p_paddr is set to 0. */ | |
7288 | if (bed->want_p_paddr_set_to_zero) | |
7289 | goto rewrite; | |
84d1d650 L |
7290 | |
7291 | /* Initialize the segment mark field. */ | |
7292 | for (section = obfd->sections; section != NULL; | |
7293 | section = section->next) | |
7294 | section->segment_mark = FALSE; | |
7295 | ||
7296 | num_segments = elf_elfheader (ibfd)->e_phnum; | |
7297 | for (i = 0, segment = elf_tdata (ibfd)->phdr; | |
7298 | i < num_segments; | |
7299 | i++, segment++) | |
7300 | { | |
5f6999aa NC |
7301 | /* PR binutils/3535. The Solaris linker always sets the p_paddr |
7302 | and p_memsz fields of special segments (DYNAMIC, INTERP) to 0 | |
7303 | which severly confuses things, so always regenerate the segment | |
7304 | map in this case. */ | |
7305 | if (segment->p_paddr == 0 | |
7306 | && segment->p_memsz == 0 | |
7307 | && (segment->p_type == PT_INTERP || segment->p_type == PT_DYNAMIC)) | |
cb3ff1e5 | 7308 | goto rewrite; |
5f6999aa | 7309 | |
84d1d650 L |
7310 | for (section = ibfd->sections; |
7311 | section != NULL; section = section->next) | |
7312 | { | |
7313 | /* We mark the output section so that we know it comes | |
7314 | from the input BFD. */ | |
7315 | osec = section->output_section; | |
7316 | if (osec) | |
7317 | osec->segment_mark = TRUE; | |
7318 | ||
7319 | /* Check if this section is covered by the segment. */ | |
7320 | this_hdr = &(elf_section_data(section)->this_hdr); | |
f4638467 | 7321 | if (ELF_SECTION_IN_SEGMENT (this_hdr, segment)) |
84d1d650 L |
7322 | { |
7323 | /* FIXME: Check if its output section is changed or | |
7324 | removed. What else do we need to check? */ | |
7325 | if (osec == NULL | |
7326 | || section->flags != osec->flags | |
7327 | || section->lma != osec->lma | |
7328 | || section->vma != osec->vma | |
7329 | || section->size != osec->size | |
7330 | || section->rawsize != osec->rawsize | |
7331 | || section->alignment_power != osec->alignment_power) | |
7332 | goto rewrite; | |
7333 | } | |
7334 | } | |
7335 | } | |
7336 | ||
cb3ff1e5 | 7337 | /* Check to see if any output section do not come from the |
84d1d650 L |
7338 | input BFD. */ |
7339 | for (section = obfd->sections; section != NULL; | |
7340 | section = section->next) | |
7341 | { | |
7342 | if (section->segment_mark == FALSE) | |
7343 | goto rewrite; | |
7344 | else | |
7345 | section->segment_mark = FALSE; | |
7346 | } | |
7347 | ||
7348 | return copy_elf_program_header (ibfd, obfd); | |
7349 | } | |
7350 | ||
7351 | rewrite: | |
f1d85785 L |
7352 | if (ibfd->xvec == obfd->xvec) |
7353 | { | |
7354 | /* When rewriting program header, set the output maxpagesize to | |
7355 | the maximum alignment of input PT_LOAD segments. */ | |
7356 | Elf_Internal_Phdr *segment; | |
7357 | unsigned int i; | |
7358 | unsigned int num_segments = elf_elfheader (ibfd)->e_phnum; | |
7359 | bfd_vma maxpagesize = 0; | |
7360 | ||
7361 | for (i = 0, segment = elf_tdata (ibfd)->phdr; | |
7362 | i < num_segments; | |
7363 | i++, segment++) | |
7364 | if (segment->p_type == PT_LOAD | |
7365 | && maxpagesize < segment->p_align) | |
c86934ce NC |
7366 | { |
7367 | /* PR 17512: file: f17299af. */ | |
7368 | if (segment->p_align > (bfd_vma) 1 << ((sizeof (bfd_vma) * 8) - 2)) | |
695344c0 | 7369 | /* xgettext:c-format */ |
4eca0228 | 7370 | _bfd_error_handler (_("\ |
c86934ce | 7371 | %B: warning: segment alignment of 0x%llx is too large"), |
4eca0228 | 7372 | ibfd, (long long) segment->p_align); |
c86934ce NC |
7373 | else |
7374 | maxpagesize = segment->p_align; | |
7375 | } | |
f1d85785 L |
7376 | |
7377 | if (maxpagesize != get_elf_backend_data (obfd)->maxpagesize) | |
7378 | bfd_emul_set_maxpagesize (bfd_get_target (obfd), maxpagesize); | |
7379 | } | |
7380 | ||
84d1d650 L |
7381 | return rewrite_elf_program_header (ibfd, obfd); |
7382 | } | |
7383 | ||
ccd2ec6a L |
7384 | /* Initialize private output section information from input section. */ |
7385 | ||
7386 | bfd_boolean | |
7387 | _bfd_elf_init_private_section_data (bfd *ibfd, | |
7388 | asection *isec, | |
7389 | bfd *obfd, | |
7390 | asection *osec, | |
7391 | struct bfd_link_info *link_info) | |
7392 | ||
7393 | { | |
7394 | Elf_Internal_Shdr *ihdr, *ohdr; | |
0e1862bb L |
7395 | bfd_boolean final_link = (link_info != NULL |
7396 | && !bfd_link_relocatable (link_info)); | |
ccd2ec6a L |
7397 | |
7398 | if (ibfd->xvec->flavour != bfd_target_elf_flavour | |
7399 | || obfd->xvec->flavour != bfd_target_elf_flavour) | |
7400 | return TRUE; | |
7401 | ||
ba85c43e NC |
7402 | BFD_ASSERT (elf_section_data (osec) != NULL); |
7403 | ||
dfa7b0b8 AM |
7404 | /* For objcopy and relocatable link, don't copy the output ELF |
7405 | section type from input if the output BFD section flags have been | |
7406 | set to something different. For a final link allow some flags | |
7407 | that the linker clears to differ. */ | |
42bb2e33 | 7408 | if (elf_section_type (osec) == SHT_NULL |
dfa7b0b8 AM |
7409 | && (osec->flags == isec->flags |
7410 | || (final_link | |
7411 | && ((osec->flags ^ isec->flags) | |
0814be7d | 7412 | & ~(SEC_LINK_ONCE | SEC_LINK_DUPLICATES | SEC_RELOC)) == 0))) |
42bb2e33 | 7413 | elf_section_type (osec) = elf_section_type (isec); |
d270463e L |
7414 | |
7415 | /* FIXME: Is this correct for all OS/PROC specific flags? */ | |
7416 | elf_section_flags (osec) |= (elf_section_flags (isec) | |
7417 | & (SHF_MASKOS | SHF_MASKPROC)); | |
ccd2ec6a | 7418 | |
a91e1603 L |
7419 | /* Copy sh_info from input for mbind section. */ |
7420 | if (elf_section_flags (isec) & SHF_GNU_MBIND) | |
7421 | elf_section_data (osec)->this_hdr.sh_info | |
7422 | = elf_section_data (isec)->this_hdr.sh_info; | |
7423 | ||
ccd2ec6a L |
7424 | /* Set things up for objcopy and relocatable link. The output |
7425 | SHT_GROUP section will have its elf_next_in_group pointing back | |
7426 | to the input group members. Ignore linker created group section. | |
7427 | See elfNN_ia64_object_p in elfxx-ia64.c. */ | |
dfa7b0b8 | 7428 | if (!final_link) |
ccd2ec6a L |
7429 | { |
7430 | if (elf_sec_group (isec) == NULL | |
7431 | || (elf_sec_group (isec)->flags & SEC_LINKER_CREATED) == 0) | |
7432 | { | |
7433 | if (elf_section_flags (isec) & SHF_GROUP) | |
7434 | elf_section_flags (osec) |= SHF_GROUP; | |
7435 | elf_next_in_group (osec) = elf_next_in_group (isec); | |
9659de1c | 7436 | elf_section_data (osec)->group = elf_section_data (isec)->group; |
ccd2ec6a | 7437 | } |
68f5ff89 L |
7438 | |
7439 | /* If not decompress, preserve SHF_COMPRESSED. */ | |
7440 | if ((ibfd->flags & BFD_DECOMPRESS) == 0) | |
7441 | elf_section_flags (osec) |= (elf_section_flags (isec) | |
7442 | & SHF_COMPRESSED); | |
ccd2ec6a L |
7443 | } |
7444 | ||
7445 | ihdr = &elf_section_data (isec)->this_hdr; | |
7446 | ||
7447 | /* We need to handle elf_linked_to_section for SHF_LINK_ORDER. We | |
7448 | don't use the output section of the linked-to section since it | |
7449 | may be NULL at this point. */ | |
7450 | if ((ihdr->sh_flags & SHF_LINK_ORDER) != 0) | |
7451 | { | |
7452 | ohdr = &elf_section_data (osec)->this_hdr; | |
7453 | ohdr->sh_flags |= SHF_LINK_ORDER; | |
7454 | elf_linked_to_section (osec) = elf_linked_to_section (isec); | |
7455 | } | |
7456 | ||
7457 | osec->use_rela_p = isec->use_rela_p; | |
7458 | ||
7459 | return TRUE; | |
7460 | } | |
7461 | ||
252b5132 RH |
7462 | /* Copy private section information. This copies over the entsize |
7463 | field, and sometimes the info field. */ | |
7464 | ||
b34976b6 | 7465 | bfd_boolean |
217aa764 AM |
7466 | _bfd_elf_copy_private_section_data (bfd *ibfd, |
7467 | asection *isec, | |
7468 | bfd *obfd, | |
7469 | asection *osec) | |
252b5132 RH |
7470 | { |
7471 | Elf_Internal_Shdr *ihdr, *ohdr; | |
7472 | ||
7473 | if (ibfd->xvec->flavour != bfd_target_elf_flavour | |
7474 | || obfd->xvec->flavour != bfd_target_elf_flavour) | |
b34976b6 | 7475 | return TRUE; |
252b5132 | 7476 | |
252b5132 RH |
7477 | ihdr = &elf_section_data (isec)->this_hdr; |
7478 | ohdr = &elf_section_data (osec)->this_hdr; | |
7479 | ||
7480 | ohdr->sh_entsize = ihdr->sh_entsize; | |
7481 | ||
7482 | if (ihdr->sh_type == SHT_SYMTAB | |
7483 | || ihdr->sh_type == SHT_DYNSYM | |
7484 | || ihdr->sh_type == SHT_GNU_verneed | |
7485 | || ihdr->sh_type == SHT_GNU_verdef) | |
7486 | ohdr->sh_info = ihdr->sh_info; | |
7487 | ||
ccd2ec6a L |
7488 | return _bfd_elf_init_private_section_data (ibfd, isec, obfd, osec, |
7489 | NULL); | |
252b5132 RH |
7490 | } |
7491 | ||
d0bf826b AM |
7492 | /* Look at all the SHT_GROUP sections in IBFD, making any adjustments |
7493 | necessary if we are removing either the SHT_GROUP section or any of | |
7494 | the group member sections. DISCARDED is the value that a section's | |
7495 | output_section has if the section will be discarded, NULL when this | |
7496 | function is called from objcopy, bfd_abs_section_ptr when called | |
7497 | from the linker. */ | |
80fccad2 BW |
7498 | |
7499 | bfd_boolean | |
d0bf826b | 7500 | _bfd_elf_fixup_group_sections (bfd *ibfd, asection *discarded) |
80fccad2 | 7501 | { |
30288845 AM |
7502 | asection *isec; |
7503 | ||
30288845 | 7504 | for (isec = ibfd->sections; isec != NULL; isec = isec->next) |
415f38a6 | 7505 | if (elf_section_type (isec) == SHT_GROUP) |
30288845 AM |
7506 | { |
7507 | asection *first = elf_next_in_group (isec); | |
7508 | asection *s = first; | |
d0bf826b AM |
7509 | bfd_size_type removed = 0; |
7510 | ||
30288845 AM |
7511 | while (s != NULL) |
7512 | { | |
415f38a6 AM |
7513 | /* If this member section is being output but the |
7514 | SHT_GROUP section is not, then clear the group info | |
7515 | set up by _bfd_elf_copy_private_section_data. */ | |
d0bf826b AM |
7516 | if (s->output_section != discarded |
7517 | && isec->output_section == discarded) | |
30288845 AM |
7518 | { |
7519 | elf_section_flags (s->output_section) &= ~SHF_GROUP; | |
7520 | elf_group_name (s->output_section) = NULL; | |
7521 | } | |
415f38a6 AM |
7522 | /* Conversely, if the member section is not being output |
7523 | but the SHT_GROUP section is, then adjust its size. */ | |
d0bf826b AM |
7524 | else if (s->output_section == discarded |
7525 | && isec->output_section != discarded) | |
7526 | removed += 4; | |
30288845 AM |
7527 | s = elf_next_in_group (s); |
7528 | if (s == first) | |
7529 | break; | |
7530 | } | |
d0bf826b AM |
7531 | if (removed != 0) |
7532 | { | |
7533 | if (discarded != NULL) | |
7534 | { | |
7535 | /* If we've been called for ld -r, then we need to | |
7536 | adjust the input section size. This function may | |
7537 | be called multiple times, so save the original | |
7538 | size. */ | |
7539 | if (isec->rawsize == 0) | |
7540 | isec->rawsize = isec->size; | |
7541 | isec->size = isec->rawsize - removed; | |
7542 | } | |
7543 | else | |
7544 | { | |
7545 | /* Adjust the output section size when called from | |
7546 | objcopy. */ | |
7547 | isec->output_section->size -= removed; | |
7548 | } | |
7549 | } | |
30288845 AM |
7550 | } |
7551 | ||
80fccad2 BW |
7552 | return TRUE; |
7553 | } | |
7554 | ||
d0bf826b AM |
7555 | /* Copy private header information. */ |
7556 | ||
7557 | bfd_boolean | |
7558 | _bfd_elf_copy_private_header_data (bfd *ibfd, bfd *obfd) | |
7559 | { | |
7560 | if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour | |
7561 | || bfd_get_flavour (obfd) != bfd_target_elf_flavour) | |
7562 | return TRUE; | |
7563 | ||
7564 | /* Copy over private BFD data if it has not already been copied. | |
7565 | This must be done here, rather than in the copy_private_bfd_data | |
7566 | entry point, because the latter is called after the section | |
7567 | contents have been set, which means that the program headers have | |
7568 | already been worked out. */ | |
12bd6957 | 7569 | if (elf_seg_map (obfd) == NULL && elf_tdata (ibfd)->phdr != NULL) |
d0bf826b AM |
7570 | { |
7571 | if (! copy_private_bfd_data (ibfd, obfd)) | |
7572 | return FALSE; | |
7573 | } | |
7574 | ||
7575 | return _bfd_elf_fixup_group_sections (ibfd, NULL); | |
7576 | } | |
7577 | ||
252b5132 RH |
7578 | /* Copy private symbol information. If this symbol is in a section |
7579 | which we did not map into a BFD section, try to map the section | |
7580 | index correctly. We use special macro definitions for the mapped | |
7581 | section indices; these definitions are interpreted by the | |
7582 | swap_out_syms function. */ | |
7583 | ||
9ad5cbcf AM |
7584 | #define MAP_ONESYMTAB (SHN_HIOS + 1) |
7585 | #define MAP_DYNSYMTAB (SHN_HIOS + 2) | |
7586 | #define MAP_STRTAB (SHN_HIOS + 3) | |
7587 | #define MAP_SHSTRTAB (SHN_HIOS + 4) | |
7588 | #define MAP_SYM_SHNDX (SHN_HIOS + 5) | |
252b5132 | 7589 | |
b34976b6 | 7590 | bfd_boolean |
217aa764 AM |
7591 | _bfd_elf_copy_private_symbol_data (bfd *ibfd, |
7592 | asymbol *isymarg, | |
7593 | bfd *obfd, | |
7594 | asymbol *osymarg) | |
252b5132 RH |
7595 | { |
7596 | elf_symbol_type *isym, *osym; | |
7597 | ||
7598 | if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour | |
7599 | || bfd_get_flavour (obfd) != bfd_target_elf_flavour) | |
b34976b6 | 7600 | return TRUE; |
252b5132 RH |
7601 | |
7602 | isym = elf_symbol_from (ibfd, isymarg); | |
7603 | osym = elf_symbol_from (obfd, osymarg); | |
7604 | ||
7605 | if (isym != NULL | |
8424d8f5 | 7606 | && isym->internal_elf_sym.st_shndx != 0 |
252b5132 RH |
7607 | && osym != NULL |
7608 | && bfd_is_abs_section (isym->symbol.section)) | |
7609 | { | |
7610 | unsigned int shndx; | |
7611 | ||
7612 | shndx = isym->internal_elf_sym.st_shndx; | |
7613 | if (shndx == elf_onesymtab (ibfd)) | |
7614 | shndx = MAP_ONESYMTAB; | |
7615 | else if (shndx == elf_dynsymtab (ibfd)) | |
7616 | shndx = MAP_DYNSYMTAB; | |
12bd6957 | 7617 | else if (shndx == elf_strtab_sec (ibfd)) |
252b5132 | 7618 | shndx = MAP_STRTAB; |
12bd6957 | 7619 | else if (shndx == elf_shstrtab_sec (ibfd)) |
252b5132 | 7620 | shndx = MAP_SHSTRTAB; |
6a40cf0c | 7621 | else if (find_section_in_list (shndx, elf_symtab_shndx_list (ibfd))) |
9ad5cbcf | 7622 | shndx = MAP_SYM_SHNDX; |
252b5132 RH |
7623 | osym->internal_elf_sym.st_shndx = shndx; |
7624 | } | |
7625 | ||
b34976b6 | 7626 | return TRUE; |
252b5132 RH |
7627 | } |
7628 | ||
7629 | /* Swap out the symbols. */ | |
7630 | ||
b34976b6 | 7631 | static bfd_boolean |
217aa764 | 7632 | swap_out_syms (bfd *abfd, |
ef10c3ac | 7633 | struct elf_strtab_hash **sttp, |
217aa764 | 7634 | int relocatable_p) |
252b5132 | 7635 | { |
9c5bfbb7 | 7636 | const struct elf_backend_data *bed; |
079e9a2f AM |
7637 | int symcount; |
7638 | asymbol **syms; | |
ef10c3ac | 7639 | struct elf_strtab_hash *stt; |
079e9a2f | 7640 | Elf_Internal_Shdr *symtab_hdr; |
9ad5cbcf | 7641 | Elf_Internal_Shdr *symtab_shndx_hdr; |
079e9a2f | 7642 | Elf_Internal_Shdr *symstrtab_hdr; |
ef10c3ac | 7643 | struct elf_sym_strtab *symstrtab; |
f075ee0c AM |
7644 | bfd_byte *outbound_syms; |
7645 | bfd_byte *outbound_shndx; | |
ef10c3ac L |
7646 | unsigned long outbound_syms_index; |
7647 | unsigned long outbound_shndx_index; | |
079e9a2f | 7648 | int idx; |
12bd6957 | 7649 | unsigned int num_locals; |
079e9a2f | 7650 | bfd_size_type amt; |
174fd7f9 | 7651 | bfd_boolean name_local_sections; |
252b5132 | 7652 | |
12bd6957 | 7653 | if (!elf_map_symbols (abfd, &num_locals)) |
b34976b6 | 7654 | return FALSE; |
252b5132 | 7655 | |
c044fabd | 7656 | /* Dump out the symtabs. */ |
ef10c3ac | 7657 | stt = _bfd_elf_strtab_init (); |
079e9a2f | 7658 | if (stt == NULL) |
b34976b6 | 7659 | return FALSE; |
252b5132 | 7660 | |
079e9a2f AM |
7661 | bed = get_elf_backend_data (abfd); |
7662 | symcount = bfd_get_symcount (abfd); | |
7663 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; | |
7664 | symtab_hdr->sh_type = SHT_SYMTAB; | |
7665 | symtab_hdr->sh_entsize = bed->s->sizeof_sym; | |
7666 | symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1); | |
12bd6957 | 7667 | symtab_hdr->sh_info = num_locals + 1; |
72de5009 | 7668 | symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align; |
079e9a2f AM |
7669 | |
7670 | symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr; | |
7671 | symstrtab_hdr->sh_type = SHT_STRTAB; | |
7672 | ||
ef10c3ac L |
7673 | /* Allocate buffer to swap out the .strtab section. */ |
7674 | symstrtab = (struct elf_sym_strtab *) bfd_malloc ((symcount + 1) | |
7675 | * sizeof (*symstrtab)); | |
7676 | if (symstrtab == NULL) | |
7677 | { | |
7678 | _bfd_elf_strtab_free (stt); | |
7679 | return FALSE; | |
7680 | } | |
7681 | ||
a50b1753 NC |
7682 | outbound_syms = (bfd_byte *) bfd_alloc2 (abfd, 1 + symcount, |
7683 | bed->s->sizeof_sym); | |
079e9a2f | 7684 | if (outbound_syms == NULL) |
5ed6aba4 | 7685 | { |
ef10c3ac L |
7686 | error_return: |
7687 | _bfd_elf_strtab_free (stt); | |
7688 | free (symstrtab); | |
5ed6aba4 NC |
7689 | return FALSE; |
7690 | } | |
217aa764 | 7691 | symtab_hdr->contents = outbound_syms; |
ef10c3ac | 7692 | outbound_syms_index = 0; |
252b5132 | 7693 | |
9ad5cbcf | 7694 | outbound_shndx = NULL; |
ef10c3ac | 7695 | outbound_shndx_index = 0; |
6a40cf0c NC |
7696 | |
7697 | if (elf_symtab_shndx_list (abfd)) | |
9ad5cbcf | 7698 | { |
6a40cf0c NC |
7699 | symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr; |
7700 | if (symtab_shndx_hdr->sh_name != 0) | |
7701 | { | |
7702 | amt = (bfd_size_type) (1 + symcount) * sizeof (Elf_External_Sym_Shndx); | |
7703 | outbound_shndx = (bfd_byte *) | |
7704 | bfd_zalloc2 (abfd, 1 + symcount, sizeof (Elf_External_Sym_Shndx)); | |
7705 | if (outbound_shndx == NULL) | |
7706 | goto error_return; | |
5ed6aba4 | 7707 | |
6a40cf0c NC |
7708 | symtab_shndx_hdr->contents = outbound_shndx; |
7709 | symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX; | |
7710 | symtab_shndx_hdr->sh_size = amt; | |
7711 | symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx); | |
7712 | symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx); | |
7713 | } | |
7714 | /* FIXME: What about any other headers in the list ? */ | |
9ad5cbcf AM |
7715 | } |
7716 | ||
589e6347 | 7717 | /* Now generate the data (for "contents"). */ |
079e9a2f AM |
7718 | { |
7719 | /* Fill in zeroth symbol and swap it out. */ | |
7720 | Elf_Internal_Sym sym; | |
7721 | sym.st_name = 0; | |
7722 | sym.st_value = 0; | |
7723 | sym.st_size = 0; | |
7724 | sym.st_info = 0; | |
7725 | sym.st_other = 0; | |
7726 | sym.st_shndx = SHN_UNDEF; | |
35fc36a8 | 7727 | sym.st_target_internal = 0; |
ef10c3ac L |
7728 | symstrtab[0].sym = sym; |
7729 | symstrtab[0].dest_index = outbound_syms_index; | |
7730 | symstrtab[0].destshndx_index = outbound_shndx_index; | |
7731 | outbound_syms_index++; | |
9ad5cbcf | 7732 | if (outbound_shndx != NULL) |
ef10c3ac | 7733 | outbound_shndx_index++; |
079e9a2f | 7734 | } |
252b5132 | 7735 | |
174fd7f9 RS |
7736 | name_local_sections |
7737 | = (bed->elf_backend_name_local_section_symbols | |
7738 | && bed->elf_backend_name_local_section_symbols (abfd)); | |
7739 | ||
079e9a2f | 7740 | syms = bfd_get_outsymbols (abfd); |
ef10c3ac | 7741 | for (idx = 0; idx < symcount;) |
252b5132 | 7742 | { |
252b5132 | 7743 | Elf_Internal_Sym sym; |
079e9a2f AM |
7744 | bfd_vma value = syms[idx]->value; |
7745 | elf_symbol_type *type_ptr; | |
7746 | flagword flags = syms[idx]->flags; | |
7747 | int type; | |
252b5132 | 7748 | |
174fd7f9 RS |
7749 | if (!name_local_sections |
7750 | && (flags & (BSF_SECTION_SYM | BSF_GLOBAL)) == BSF_SECTION_SYM) | |
079e9a2f AM |
7751 | { |
7752 | /* Local section symbols have no name. */ | |
ef10c3ac | 7753 | sym.st_name = (unsigned long) -1; |
079e9a2f AM |
7754 | } |
7755 | else | |
7756 | { | |
ef10c3ac L |
7757 | /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize |
7758 | to get the final offset for st_name. */ | |
7759 | sym.st_name | |
7760 | = (unsigned long) _bfd_elf_strtab_add (stt, syms[idx]->name, | |
7761 | FALSE); | |
079e9a2f | 7762 | if (sym.st_name == (unsigned long) -1) |
ef10c3ac | 7763 | goto error_return; |
079e9a2f | 7764 | } |
252b5132 | 7765 | |
079e9a2f | 7766 | type_ptr = elf_symbol_from (abfd, syms[idx]); |
252b5132 | 7767 | |
079e9a2f AM |
7768 | if ((flags & BSF_SECTION_SYM) == 0 |
7769 | && bfd_is_com_section (syms[idx]->section)) | |
7770 | { | |
7771 | /* ELF common symbols put the alignment into the `value' field, | |
7772 | and the size into the `size' field. This is backwards from | |
7773 | how BFD handles it, so reverse it here. */ | |
7774 | sym.st_size = value; | |
7775 | if (type_ptr == NULL | |
7776 | || type_ptr->internal_elf_sym.st_value == 0) | |
7777 | sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value)); | |
7778 | else | |
7779 | sym.st_value = type_ptr->internal_elf_sym.st_value; | |
7780 | sym.st_shndx = _bfd_elf_section_from_bfd_section | |
7781 | (abfd, syms[idx]->section); | |
7782 | } | |
7783 | else | |
7784 | { | |
7785 | asection *sec = syms[idx]->section; | |
cb33740c | 7786 | unsigned int shndx; |
252b5132 | 7787 | |
079e9a2f AM |
7788 | if (sec->output_section) |
7789 | { | |
7790 | value += sec->output_offset; | |
7791 | sec = sec->output_section; | |
7792 | } | |
589e6347 | 7793 | |
079e9a2f AM |
7794 | /* Don't add in the section vma for relocatable output. */ |
7795 | if (! relocatable_p) | |
7796 | value += sec->vma; | |
7797 | sym.st_value = value; | |
7798 | sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0; | |
7799 | ||
7800 | if (bfd_is_abs_section (sec) | |
7801 | && type_ptr != NULL | |
7802 | && type_ptr->internal_elf_sym.st_shndx != 0) | |
7803 | { | |
7804 | /* This symbol is in a real ELF section which we did | |
7805 | not create as a BFD section. Undo the mapping done | |
7806 | by copy_private_symbol_data. */ | |
7807 | shndx = type_ptr->internal_elf_sym.st_shndx; | |
7808 | switch (shndx) | |
7809 | { | |
7810 | case MAP_ONESYMTAB: | |
7811 | shndx = elf_onesymtab (abfd); | |
7812 | break; | |
7813 | case MAP_DYNSYMTAB: | |
7814 | shndx = elf_dynsymtab (abfd); | |
7815 | break; | |
7816 | case MAP_STRTAB: | |
12bd6957 | 7817 | shndx = elf_strtab_sec (abfd); |
079e9a2f AM |
7818 | break; |
7819 | case MAP_SHSTRTAB: | |
12bd6957 | 7820 | shndx = elf_shstrtab_sec (abfd); |
079e9a2f | 7821 | break; |
9ad5cbcf | 7822 | case MAP_SYM_SHNDX: |
6a40cf0c NC |
7823 | if (elf_symtab_shndx_list (abfd)) |
7824 | shndx = elf_symtab_shndx_list (abfd)->ndx; | |
9ad5cbcf | 7825 | break; |
079e9a2f | 7826 | default: |
15bc576a | 7827 | shndx = SHN_ABS; |
079e9a2f AM |
7828 | break; |
7829 | } | |
7830 | } | |
7831 | else | |
7832 | { | |
7833 | shndx = _bfd_elf_section_from_bfd_section (abfd, sec); | |
252b5132 | 7834 | |
cb33740c | 7835 | if (shndx == SHN_BAD) |
079e9a2f AM |
7836 | { |
7837 | asection *sec2; | |
7838 | ||
7839 | /* Writing this would be a hell of a lot easier if | |
7840 | we had some decent documentation on bfd, and | |
7841 | knew what to expect of the library, and what to | |
7842 | demand of applications. For example, it | |
7843 | appears that `objcopy' might not set the | |
7844 | section of a symbol to be a section that is | |
7845 | actually in the output file. */ | |
7846 | sec2 = bfd_get_section_by_name (abfd, sec->name); | |
5df1bc57 AM |
7847 | if (sec2 != NULL) |
7848 | shndx = _bfd_elf_section_from_bfd_section (abfd, sec2); | |
7849 | if (shndx == SHN_BAD) | |
589e6347 | 7850 | { |
695344c0 | 7851 | /* xgettext:c-format */ |
589e6347 NC |
7852 | _bfd_error_handler (_("\ |
7853 | Unable to find equivalent output section for symbol '%s' from section '%s'"), | |
7854 | syms[idx]->name ? syms[idx]->name : "<Local sym>", | |
7855 | sec->name); | |
811072d8 | 7856 | bfd_set_error (bfd_error_invalid_operation); |
ef10c3ac | 7857 | goto error_return; |
589e6347 | 7858 | } |
079e9a2f AM |
7859 | } |
7860 | } | |
252b5132 | 7861 | |
079e9a2f AM |
7862 | sym.st_shndx = shndx; |
7863 | } | |
252b5132 | 7864 | |
13ae64f3 JJ |
7865 | if ((flags & BSF_THREAD_LOCAL) != 0) |
7866 | type = STT_TLS; | |
d8045f23 NC |
7867 | else if ((flags & BSF_GNU_INDIRECT_FUNCTION) != 0) |
7868 | type = STT_GNU_IFUNC; | |
13ae64f3 | 7869 | else if ((flags & BSF_FUNCTION) != 0) |
079e9a2f AM |
7870 | type = STT_FUNC; |
7871 | else if ((flags & BSF_OBJECT) != 0) | |
7872 | type = STT_OBJECT; | |
d9352518 DB |
7873 | else if ((flags & BSF_RELC) != 0) |
7874 | type = STT_RELC; | |
7875 | else if ((flags & BSF_SRELC) != 0) | |
7876 | type = STT_SRELC; | |
079e9a2f AM |
7877 | else |
7878 | type = STT_NOTYPE; | |
252b5132 | 7879 | |
13ae64f3 JJ |
7880 | if (syms[idx]->section->flags & SEC_THREAD_LOCAL) |
7881 | type = STT_TLS; | |
7882 | ||
589e6347 | 7883 | /* Processor-specific types. */ |
079e9a2f AM |
7884 | if (type_ptr != NULL |
7885 | && bed->elf_backend_get_symbol_type) | |
7886 | type = ((*bed->elf_backend_get_symbol_type) | |
7887 | (&type_ptr->internal_elf_sym, type)); | |
252b5132 | 7888 | |
079e9a2f AM |
7889 | if (flags & BSF_SECTION_SYM) |
7890 | { | |
7891 | if (flags & BSF_GLOBAL) | |
7892 | sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION); | |
7893 | else | |
7894 | sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION); | |
7895 | } | |
7896 | else if (bfd_is_com_section (syms[idx]->section)) | |
0a40daed | 7897 | { |
b8871f35 L |
7898 | if (type != STT_TLS) |
7899 | { | |
7900 | if ((abfd->flags & BFD_CONVERT_ELF_COMMON)) | |
7901 | type = ((abfd->flags & BFD_USE_ELF_STT_COMMON) | |
7902 | ? STT_COMMON : STT_OBJECT); | |
7903 | else | |
7904 | type = ((flags & BSF_ELF_COMMON) != 0 | |
7905 | ? STT_COMMON : STT_OBJECT); | |
7906 | } | |
7907 | sym.st_info = ELF_ST_INFO (STB_GLOBAL, type); | |
0a40daed | 7908 | } |
079e9a2f AM |
7909 | else if (bfd_is_und_section (syms[idx]->section)) |
7910 | sym.st_info = ELF_ST_INFO (((flags & BSF_WEAK) | |
7911 | ? STB_WEAK | |
7912 | : STB_GLOBAL), | |
7913 | type); | |
7914 | else if (flags & BSF_FILE) | |
7915 | sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE); | |
7916 | else | |
7917 | { | |
7918 | int bind = STB_LOCAL; | |
252b5132 | 7919 | |
079e9a2f AM |
7920 | if (flags & BSF_LOCAL) |
7921 | bind = STB_LOCAL; | |
3e7a7d11 NC |
7922 | else if (flags & BSF_GNU_UNIQUE) |
7923 | bind = STB_GNU_UNIQUE; | |
079e9a2f AM |
7924 | else if (flags & BSF_WEAK) |
7925 | bind = STB_WEAK; | |
7926 | else if (flags & BSF_GLOBAL) | |
7927 | bind = STB_GLOBAL; | |
252b5132 | 7928 | |
079e9a2f AM |
7929 | sym.st_info = ELF_ST_INFO (bind, type); |
7930 | } | |
252b5132 | 7931 | |
079e9a2f | 7932 | if (type_ptr != NULL) |
35fc36a8 RS |
7933 | { |
7934 | sym.st_other = type_ptr->internal_elf_sym.st_other; | |
7935 | sym.st_target_internal | |
7936 | = type_ptr->internal_elf_sym.st_target_internal; | |
7937 | } | |
079e9a2f | 7938 | else |
35fc36a8 RS |
7939 | { |
7940 | sym.st_other = 0; | |
7941 | sym.st_target_internal = 0; | |
7942 | } | |
252b5132 | 7943 | |
ef10c3ac L |
7944 | idx++; |
7945 | symstrtab[idx].sym = sym; | |
7946 | symstrtab[idx].dest_index = outbound_syms_index; | |
7947 | symstrtab[idx].destshndx_index = outbound_shndx_index; | |
7948 | ||
7949 | outbound_syms_index++; | |
9ad5cbcf | 7950 | if (outbound_shndx != NULL) |
ef10c3ac L |
7951 | outbound_shndx_index++; |
7952 | } | |
7953 | ||
7954 | /* Finalize the .strtab section. */ | |
7955 | _bfd_elf_strtab_finalize (stt); | |
7956 | ||
7957 | /* Swap out the .strtab section. */ | |
7958 | for (idx = 0; idx <= symcount; idx++) | |
7959 | { | |
7960 | struct elf_sym_strtab *elfsym = &symstrtab[idx]; | |
7961 | if (elfsym->sym.st_name == (unsigned long) -1) | |
7962 | elfsym->sym.st_name = 0; | |
7963 | else | |
7964 | elfsym->sym.st_name = _bfd_elf_strtab_offset (stt, | |
7965 | elfsym->sym.st_name); | |
7966 | bed->s->swap_symbol_out (abfd, &elfsym->sym, | |
7967 | (outbound_syms | |
7968 | + (elfsym->dest_index | |
7969 | * bed->s->sizeof_sym)), | |
7970 | (outbound_shndx | |
7971 | + (elfsym->destshndx_index | |
7972 | * sizeof (Elf_External_Sym_Shndx)))); | |
079e9a2f | 7973 | } |
ef10c3ac | 7974 | free (symstrtab); |
252b5132 | 7975 | |
079e9a2f | 7976 | *sttp = stt; |
ef10c3ac | 7977 | symstrtab_hdr->sh_size = _bfd_elf_strtab_size (stt); |
079e9a2f | 7978 | symstrtab_hdr->sh_type = SHT_STRTAB; |
84865015 | 7979 | symstrtab_hdr->sh_flags = bed->elf_strtab_flags; |
079e9a2f AM |
7980 | symstrtab_hdr->sh_addr = 0; |
7981 | symstrtab_hdr->sh_entsize = 0; | |
7982 | symstrtab_hdr->sh_link = 0; | |
7983 | symstrtab_hdr->sh_info = 0; | |
7984 | symstrtab_hdr->sh_addralign = 1; | |
252b5132 | 7985 | |
b34976b6 | 7986 | return TRUE; |
252b5132 RH |
7987 | } |
7988 | ||
7989 | /* Return the number of bytes required to hold the symtab vector. | |
7990 | ||
7991 | Note that we base it on the count plus 1, since we will null terminate | |
7992 | the vector allocated based on this size. However, the ELF symbol table | |
7993 | always has a dummy entry as symbol #0, so it ends up even. */ | |
7994 | ||
7995 | long | |
217aa764 | 7996 | _bfd_elf_get_symtab_upper_bound (bfd *abfd) |
252b5132 RH |
7997 | { |
7998 | long symcount; | |
7999 | long symtab_size; | |
8000 | Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr; | |
8001 | ||
8002 | symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym; | |
b99d1833 AM |
8003 | symtab_size = (symcount + 1) * (sizeof (asymbol *)); |
8004 | if (symcount > 0) | |
8005 | symtab_size -= sizeof (asymbol *); | |
252b5132 RH |
8006 | |
8007 | return symtab_size; | |
8008 | } | |
8009 | ||
8010 | long | |
217aa764 | 8011 | _bfd_elf_get_dynamic_symtab_upper_bound (bfd *abfd) |
252b5132 RH |
8012 | { |
8013 | long symcount; | |
8014 | long symtab_size; | |
8015 | Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr; | |
8016 | ||
8017 | if (elf_dynsymtab (abfd) == 0) | |
8018 | { | |
8019 | bfd_set_error (bfd_error_invalid_operation); | |
8020 | return -1; | |
8021 | } | |
8022 | ||
8023 | symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym; | |
b99d1833 AM |
8024 | symtab_size = (symcount + 1) * (sizeof (asymbol *)); |
8025 | if (symcount > 0) | |
8026 | symtab_size -= sizeof (asymbol *); | |
252b5132 RH |
8027 | |
8028 | return symtab_size; | |
8029 | } | |
8030 | ||
8031 | long | |
217aa764 AM |
8032 | _bfd_elf_get_reloc_upper_bound (bfd *abfd ATTRIBUTE_UNUSED, |
8033 | sec_ptr asect) | |
252b5132 RH |
8034 | { |
8035 | return (asect->reloc_count + 1) * sizeof (arelent *); | |
8036 | } | |
8037 | ||
8038 | /* Canonicalize the relocs. */ | |
8039 | ||
8040 | long | |
217aa764 AM |
8041 | _bfd_elf_canonicalize_reloc (bfd *abfd, |
8042 | sec_ptr section, | |
8043 | arelent **relptr, | |
8044 | asymbol **symbols) | |
252b5132 RH |
8045 | { |
8046 | arelent *tblptr; | |
8047 | unsigned int i; | |
9c5bfbb7 | 8048 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
252b5132 | 8049 | |
b34976b6 | 8050 | if (! bed->s->slurp_reloc_table (abfd, section, symbols, FALSE)) |
252b5132 RH |
8051 | return -1; |
8052 | ||
8053 | tblptr = section->relocation; | |
8054 | for (i = 0; i < section->reloc_count; i++) | |
8055 | *relptr++ = tblptr++; | |
8056 | ||
8057 | *relptr = NULL; | |
8058 | ||
8059 | return section->reloc_count; | |
8060 | } | |
8061 | ||
8062 | long | |
6cee3f79 | 8063 | _bfd_elf_canonicalize_symtab (bfd *abfd, asymbol **allocation) |
252b5132 | 8064 | { |
9c5bfbb7 | 8065 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
217aa764 | 8066 | long symcount = bed->s->slurp_symbol_table (abfd, allocation, FALSE); |
252b5132 RH |
8067 | |
8068 | if (symcount >= 0) | |
8069 | bfd_get_symcount (abfd) = symcount; | |
8070 | return symcount; | |
8071 | } | |
8072 | ||
8073 | long | |
217aa764 AM |
8074 | _bfd_elf_canonicalize_dynamic_symtab (bfd *abfd, |
8075 | asymbol **allocation) | |
252b5132 | 8076 | { |
9c5bfbb7 | 8077 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
217aa764 | 8078 | long symcount = bed->s->slurp_symbol_table (abfd, allocation, TRUE); |
1f70368c DJ |
8079 | |
8080 | if (symcount >= 0) | |
8081 | bfd_get_dynamic_symcount (abfd) = symcount; | |
8082 | return symcount; | |
252b5132 RH |
8083 | } |
8084 | ||
8615f3f2 AM |
8085 | /* Return the size required for the dynamic reloc entries. Any loadable |
8086 | section that was actually installed in the BFD, and has type SHT_REL | |
8087 | or SHT_RELA, and uses the dynamic symbol table, is considered to be a | |
8088 | dynamic reloc section. */ | |
252b5132 RH |
8089 | |
8090 | long | |
217aa764 | 8091 | _bfd_elf_get_dynamic_reloc_upper_bound (bfd *abfd) |
252b5132 RH |
8092 | { |
8093 | long ret; | |
8094 | asection *s; | |
8095 | ||
8096 | if (elf_dynsymtab (abfd) == 0) | |
8097 | { | |
8098 | bfd_set_error (bfd_error_invalid_operation); | |
8099 | return -1; | |
8100 | } | |
8101 | ||
8102 | ret = sizeof (arelent *); | |
8103 | for (s = abfd->sections; s != NULL; s = s->next) | |
266b05cf | 8104 | if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd) |
252b5132 RH |
8105 | && (elf_section_data (s)->this_hdr.sh_type == SHT_REL |
8106 | || elf_section_data (s)->this_hdr.sh_type == SHT_RELA)) | |
eea6121a | 8107 | ret += ((s->size / elf_section_data (s)->this_hdr.sh_entsize) |
252b5132 RH |
8108 | * sizeof (arelent *)); |
8109 | ||
8110 | return ret; | |
8111 | } | |
8112 | ||
8615f3f2 AM |
8113 | /* Canonicalize the dynamic relocation entries. Note that we return the |
8114 | dynamic relocations as a single block, although they are actually | |
8115 | associated with particular sections; the interface, which was | |
8116 | designed for SunOS style shared libraries, expects that there is only | |
8117 | one set of dynamic relocs. Any loadable section that was actually | |
8118 | installed in the BFD, and has type SHT_REL or SHT_RELA, and uses the | |
8119 | dynamic symbol table, is considered to be a dynamic reloc section. */ | |
252b5132 RH |
8120 | |
8121 | long | |
217aa764 AM |
8122 | _bfd_elf_canonicalize_dynamic_reloc (bfd *abfd, |
8123 | arelent **storage, | |
8124 | asymbol **syms) | |
252b5132 | 8125 | { |
217aa764 | 8126 | bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean); |
252b5132 RH |
8127 | asection *s; |
8128 | long ret; | |
8129 | ||
8130 | if (elf_dynsymtab (abfd) == 0) | |
8131 | { | |
8132 | bfd_set_error (bfd_error_invalid_operation); | |
8133 | return -1; | |
8134 | } | |
8135 | ||
8136 | slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table; | |
8137 | ret = 0; | |
8138 | for (s = abfd->sections; s != NULL; s = s->next) | |
8139 | { | |
266b05cf | 8140 | if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd) |
252b5132 RH |
8141 | && (elf_section_data (s)->this_hdr.sh_type == SHT_REL |
8142 | || elf_section_data (s)->this_hdr.sh_type == SHT_RELA)) | |
8143 | { | |
8144 | arelent *p; | |
8145 | long count, i; | |
8146 | ||
b34976b6 | 8147 | if (! (*slurp_relocs) (abfd, s, syms, TRUE)) |
252b5132 | 8148 | return -1; |
eea6121a | 8149 | count = s->size / elf_section_data (s)->this_hdr.sh_entsize; |
252b5132 RH |
8150 | p = s->relocation; |
8151 | for (i = 0; i < count; i++) | |
8152 | *storage++ = p++; | |
8153 | ret += count; | |
8154 | } | |
8155 | } | |
8156 | ||
8157 | *storage = NULL; | |
8158 | ||
8159 | return ret; | |
8160 | } | |
8161 | \f | |
8162 | /* Read in the version information. */ | |
8163 | ||
b34976b6 | 8164 | bfd_boolean |
fc0e6df6 | 8165 | _bfd_elf_slurp_version_tables (bfd *abfd, bfd_boolean default_imported_symver) |
252b5132 RH |
8166 | { |
8167 | bfd_byte *contents = NULL; | |
fc0e6df6 PB |
8168 | unsigned int freeidx = 0; |
8169 | ||
8170 | if (elf_dynverref (abfd) != 0) | |
8171 | { | |
8172 | Elf_Internal_Shdr *hdr; | |
8173 | Elf_External_Verneed *everneed; | |
8174 | Elf_Internal_Verneed *iverneed; | |
8175 | unsigned int i; | |
d0fb9a8d | 8176 | bfd_byte *contents_end; |
fc0e6df6 PB |
8177 | |
8178 | hdr = &elf_tdata (abfd)->dynverref_hdr; | |
8179 | ||
601a03ba | 8180 | if (hdr->sh_info == 0 || hdr->sh_size < sizeof (Elf_External_Verneed)) |
d0fb9a8d | 8181 | { |
601a03ba | 8182 | error_return_bad_verref: |
4eca0228 | 8183 | _bfd_error_handler |
601a03ba AM |
8184 | (_("%B: .gnu.version_r invalid entry"), abfd); |
8185 | bfd_set_error (bfd_error_bad_value); | |
d0fb9a8d JJ |
8186 | error_return_verref: |
8187 | elf_tdata (abfd)->verref = NULL; | |
8188 | elf_tdata (abfd)->cverrefs = 0; | |
8189 | goto error_return; | |
8190 | } | |
601a03ba AM |
8191 | |
8192 | contents = (bfd_byte *) bfd_malloc (hdr->sh_size); | |
8193 | if (contents == NULL) | |
8194 | goto error_return_verref; | |
8195 | ||
fc0e6df6 PB |
8196 | if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0 |
8197 | || bfd_bread (contents, hdr->sh_size, abfd) != hdr->sh_size) | |
d0fb9a8d | 8198 | goto error_return_verref; |
fc0e6df6 | 8199 | |
601a03ba AM |
8200 | elf_tdata (abfd)->verref = (Elf_Internal_Verneed *) |
8201 | bfd_zalloc2 (abfd, hdr->sh_info, sizeof (Elf_Internal_Verneed)); | |
8202 | ||
8203 | if (elf_tdata (abfd)->verref == NULL) | |
d0fb9a8d JJ |
8204 | goto error_return_verref; |
8205 | ||
8206 | BFD_ASSERT (sizeof (Elf_External_Verneed) | |
8207 | == sizeof (Elf_External_Vernaux)); | |
8208 | contents_end = contents + hdr->sh_size - sizeof (Elf_External_Verneed); | |
fc0e6df6 PB |
8209 | everneed = (Elf_External_Verneed *) contents; |
8210 | iverneed = elf_tdata (abfd)->verref; | |
8211 | for (i = 0; i < hdr->sh_info; i++, iverneed++) | |
8212 | { | |
8213 | Elf_External_Vernaux *evernaux; | |
8214 | Elf_Internal_Vernaux *ivernaux; | |
8215 | unsigned int j; | |
8216 | ||
8217 | _bfd_elf_swap_verneed_in (abfd, everneed, iverneed); | |
8218 | ||
8219 | iverneed->vn_bfd = abfd; | |
8220 | ||
8221 | iverneed->vn_filename = | |
8222 | bfd_elf_string_from_elf_section (abfd, hdr->sh_link, | |
8223 | iverneed->vn_file); | |
8224 | if (iverneed->vn_filename == NULL) | |
601a03ba | 8225 | goto error_return_bad_verref; |
fc0e6df6 | 8226 | |
d0fb9a8d JJ |
8227 | if (iverneed->vn_cnt == 0) |
8228 | iverneed->vn_auxptr = NULL; | |
8229 | else | |
8230 | { | |
a50b1753 NC |
8231 | iverneed->vn_auxptr = (struct elf_internal_vernaux *) |
8232 | bfd_alloc2 (abfd, iverneed->vn_cnt, | |
8233 | sizeof (Elf_Internal_Vernaux)); | |
d0fb9a8d JJ |
8234 | if (iverneed->vn_auxptr == NULL) |
8235 | goto error_return_verref; | |
8236 | } | |
8237 | ||
8238 | if (iverneed->vn_aux | |
8239 | > (size_t) (contents_end - (bfd_byte *) everneed)) | |
601a03ba | 8240 | goto error_return_bad_verref; |
fc0e6df6 PB |
8241 | |
8242 | evernaux = ((Elf_External_Vernaux *) | |
8243 | ((bfd_byte *) everneed + iverneed->vn_aux)); | |
8244 | ivernaux = iverneed->vn_auxptr; | |
8245 | for (j = 0; j < iverneed->vn_cnt; j++, ivernaux++) | |
8246 | { | |
8247 | _bfd_elf_swap_vernaux_in (abfd, evernaux, ivernaux); | |
8248 | ||
8249 | ivernaux->vna_nodename = | |
8250 | bfd_elf_string_from_elf_section (abfd, hdr->sh_link, | |
8251 | ivernaux->vna_name); | |
8252 | if (ivernaux->vna_nodename == NULL) | |
601a03ba | 8253 | goto error_return_bad_verref; |
fc0e6df6 | 8254 | |
25ff461f AM |
8255 | if (ivernaux->vna_other > freeidx) |
8256 | freeidx = ivernaux->vna_other; | |
8257 | ||
8258 | ivernaux->vna_nextptr = NULL; | |
8259 | if (ivernaux->vna_next == 0) | |
8260 | { | |
8261 | iverneed->vn_cnt = j + 1; | |
8262 | break; | |
8263 | } | |
fc0e6df6 PB |
8264 | if (j + 1 < iverneed->vn_cnt) |
8265 | ivernaux->vna_nextptr = ivernaux + 1; | |
fc0e6df6 | 8266 | |
d0fb9a8d JJ |
8267 | if (ivernaux->vna_next |
8268 | > (size_t) (contents_end - (bfd_byte *) evernaux)) | |
601a03ba | 8269 | goto error_return_bad_verref; |
d0fb9a8d | 8270 | |
fc0e6df6 PB |
8271 | evernaux = ((Elf_External_Vernaux *) |
8272 | ((bfd_byte *) evernaux + ivernaux->vna_next)); | |
fc0e6df6 PB |
8273 | } |
8274 | ||
25ff461f AM |
8275 | iverneed->vn_nextref = NULL; |
8276 | if (iverneed->vn_next == 0) | |
8277 | break; | |
fc0e6df6 PB |
8278 | if (i + 1 < hdr->sh_info) |
8279 | iverneed->vn_nextref = iverneed + 1; | |
fc0e6df6 | 8280 | |
d0fb9a8d JJ |
8281 | if (iverneed->vn_next |
8282 | > (size_t) (contents_end - (bfd_byte *) everneed)) | |
601a03ba | 8283 | goto error_return_bad_verref; |
d0fb9a8d | 8284 | |
fc0e6df6 PB |
8285 | everneed = ((Elf_External_Verneed *) |
8286 | ((bfd_byte *) everneed + iverneed->vn_next)); | |
8287 | } | |
25ff461f | 8288 | elf_tdata (abfd)->cverrefs = i; |
fc0e6df6 PB |
8289 | |
8290 | free (contents); | |
8291 | contents = NULL; | |
8292 | } | |
252b5132 RH |
8293 | |
8294 | if (elf_dynverdef (abfd) != 0) | |
8295 | { | |
8296 | Elf_Internal_Shdr *hdr; | |
8297 | Elf_External_Verdef *everdef; | |
8298 | Elf_Internal_Verdef *iverdef; | |
f631889e UD |
8299 | Elf_Internal_Verdef *iverdefarr; |
8300 | Elf_Internal_Verdef iverdefmem; | |
252b5132 | 8301 | unsigned int i; |
062e2358 | 8302 | unsigned int maxidx; |
d0fb9a8d | 8303 | bfd_byte *contents_end_def, *contents_end_aux; |
252b5132 RH |
8304 | |
8305 | hdr = &elf_tdata (abfd)->dynverdef_hdr; | |
8306 | ||
601a03ba AM |
8307 | if (hdr->sh_info == 0 || hdr->sh_size < sizeof (Elf_External_Verdef)) |
8308 | { | |
8309 | error_return_bad_verdef: | |
4eca0228 | 8310 | _bfd_error_handler |
601a03ba AM |
8311 | (_("%B: .gnu.version_d invalid entry"), abfd); |
8312 | bfd_set_error (bfd_error_bad_value); | |
8313 | error_return_verdef: | |
8314 | elf_tdata (abfd)->verdef = NULL; | |
8315 | elf_tdata (abfd)->cverdefs = 0; | |
8316 | goto error_return; | |
8317 | } | |
8318 | ||
a50b1753 | 8319 | contents = (bfd_byte *) bfd_malloc (hdr->sh_size); |
252b5132 | 8320 | if (contents == NULL) |
601a03ba | 8321 | goto error_return_verdef; |
252b5132 | 8322 | if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0 |
217aa764 | 8323 | || bfd_bread (contents, hdr->sh_size, abfd) != hdr->sh_size) |
601a03ba | 8324 | goto error_return_verdef; |
d0fb9a8d JJ |
8325 | |
8326 | BFD_ASSERT (sizeof (Elf_External_Verdef) | |
8327 | >= sizeof (Elf_External_Verdaux)); | |
8328 | contents_end_def = contents + hdr->sh_size | |
8329 | - sizeof (Elf_External_Verdef); | |
8330 | contents_end_aux = contents + hdr->sh_size | |
8331 | - sizeof (Elf_External_Verdaux); | |
8332 | ||
f631889e UD |
8333 | /* We know the number of entries in the section but not the maximum |
8334 | index. Therefore we have to run through all entries and find | |
8335 | the maximum. */ | |
252b5132 | 8336 | everdef = (Elf_External_Verdef *) contents; |
f631889e UD |
8337 | maxidx = 0; |
8338 | for (i = 0; i < hdr->sh_info; ++i) | |
8339 | { | |
8340 | _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem); | |
8341 | ||
601a03ba AM |
8342 | if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) == 0) |
8343 | goto error_return_bad_verdef; | |
062e2358 AM |
8344 | if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) > maxidx) |
8345 | maxidx = iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION); | |
f631889e | 8346 | |
25ff461f AM |
8347 | if (iverdefmem.vd_next == 0) |
8348 | break; | |
8349 | ||
d0fb9a8d JJ |
8350 | if (iverdefmem.vd_next |
8351 | > (size_t) (contents_end_def - (bfd_byte *) everdef)) | |
601a03ba | 8352 | goto error_return_bad_verdef; |
d0fb9a8d | 8353 | |
f631889e UD |
8354 | everdef = ((Elf_External_Verdef *) |
8355 | ((bfd_byte *) everdef + iverdefmem.vd_next)); | |
8356 | } | |
8357 | ||
fc0e6df6 PB |
8358 | if (default_imported_symver) |
8359 | { | |
8360 | if (freeidx > maxidx) | |
8361 | maxidx = ++freeidx; | |
8362 | else | |
8363 | freeidx = ++maxidx; | |
8364 | } | |
201159ec | 8365 | |
601a03ba AM |
8366 | elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *) |
8367 | bfd_zalloc2 (abfd, maxidx, sizeof (Elf_Internal_Verdef)); | |
f631889e | 8368 | if (elf_tdata (abfd)->verdef == NULL) |
601a03ba | 8369 | goto error_return_verdef; |
f631889e UD |
8370 | |
8371 | elf_tdata (abfd)->cverdefs = maxidx; | |
8372 | ||
8373 | everdef = (Elf_External_Verdef *) contents; | |
8374 | iverdefarr = elf_tdata (abfd)->verdef; | |
8375 | for (i = 0; i < hdr->sh_info; i++) | |
252b5132 RH |
8376 | { |
8377 | Elf_External_Verdaux *everdaux; | |
8378 | Elf_Internal_Verdaux *iverdaux; | |
8379 | unsigned int j; | |
8380 | ||
f631889e UD |
8381 | _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem); |
8382 | ||
d0fb9a8d | 8383 | if ((iverdefmem.vd_ndx & VERSYM_VERSION) == 0) |
601a03ba | 8384 | goto error_return_bad_verdef; |
d0fb9a8d | 8385 | |
f631889e | 8386 | iverdef = &iverdefarr[(iverdefmem.vd_ndx & VERSYM_VERSION) - 1]; |
595bce75 | 8387 | memcpy (iverdef, &iverdefmem, offsetof (Elf_Internal_Verdef, vd_bfd)); |
252b5132 RH |
8388 | |
8389 | iverdef->vd_bfd = abfd; | |
8390 | ||
d0fb9a8d JJ |
8391 | if (iverdef->vd_cnt == 0) |
8392 | iverdef->vd_auxptr = NULL; | |
8393 | else | |
8394 | { | |
a50b1753 NC |
8395 | iverdef->vd_auxptr = (struct elf_internal_verdaux *) |
8396 | bfd_alloc2 (abfd, iverdef->vd_cnt, | |
8397 | sizeof (Elf_Internal_Verdaux)); | |
d0fb9a8d JJ |
8398 | if (iverdef->vd_auxptr == NULL) |
8399 | goto error_return_verdef; | |
8400 | } | |
8401 | ||
8402 | if (iverdef->vd_aux | |
8403 | > (size_t) (contents_end_aux - (bfd_byte *) everdef)) | |
601a03ba | 8404 | goto error_return_bad_verdef; |
252b5132 RH |
8405 | |
8406 | everdaux = ((Elf_External_Verdaux *) | |
8407 | ((bfd_byte *) everdef + iverdef->vd_aux)); | |
8408 | iverdaux = iverdef->vd_auxptr; | |
8409 | for (j = 0; j < iverdef->vd_cnt; j++, iverdaux++) | |
8410 | { | |
8411 | _bfd_elf_swap_verdaux_in (abfd, everdaux, iverdaux); | |
8412 | ||
8413 | iverdaux->vda_nodename = | |
8414 | bfd_elf_string_from_elf_section (abfd, hdr->sh_link, | |
8415 | iverdaux->vda_name); | |
8416 | if (iverdaux->vda_nodename == NULL) | |
601a03ba | 8417 | goto error_return_bad_verdef; |
252b5132 | 8418 | |
25ff461f AM |
8419 | iverdaux->vda_nextptr = NULL; |
8420 | if (iverdaux->vda_next == 0) | |
8421 | { | |
8422 | iverdef->vd_cnt = j + 1; | |
8423 | break; | |
8424 | } | |
252b5132 RH |
8425 | if (j + 1 < iverdef->vd_cnt) |
8426 | iverdaux->vda_nextptr = iverdaux + 1; | |
252b5132 | 8427 | |
d0fb9a8d JJ |
8428 | if (iverdaux->vda_next |
8429 | > (size_t) (contents_end_aux - (bfd_byte *) everdaux)) | |
601a03ba | 8430 | goto error_return_bad_verdef; |
d0fb9a8d | 8431 | |
252b5132 RH |
8432 | everdaux = ((Elf_External_Verdaux *) |
8433 | ((bfd_byte *) everdaux + iverdaux->vda_next)); | |
8434 | } | |
8435 | ||
595bce75 | 8436 | iverdef->vd_nodename = NULL; |
d0fb9a8d JJ |
8437 | if (iverdef->vd_cnt) |
8438 | iverdef->vd_nodename = iverdef->vd_auxptr->vda_nodename; | |
252b5132 | 8439 | |
25ff461f AM |
8440 | iverdef->vd_nextdef = NULL; |
8441 | if (iverdef->vd_next == 0) | |
8442 | break; | |
d0fb9a8d | 8443 | if ((size_t) (iverdef - iverdefarr) + 1 < maxidx) |
252b5132 | 8444 | iverdef->vd_nextdef = iverdef + 1; |
252b5132 RH |
8445 | |
8446 | everdef = ((Elf_External_Verdef *) | |
8447 | ((bfd_byte *) everdef + iverdef->vd_next)); | |
8448 | } | |
8449 | ||
8450 | free (contents); | |
8451 | contents = NULL; | |
8452 | } | |
fc0e6df6 | 8453 | else if (default_imported_symver) |
252b5132 | 8454 | { |
fc0e6df6 PB |
8455 | if (freeidx < 3) |
8456 | freeidx = 3; | |
8457 | else | |
8458 | freeidx++; | |
252b5132 | 8459 | |
a50b1753 NC |
8460 | elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *) |
8461 | bfd_zalloc2 (abfd, freeidx, sizeof (Elf_Internal_Verdef)); | |
fc0e6df6 | 8462 | if (elf_tdata (abfd)->verdef == NULL) |
252b5132 RH |
8463 | goto error_return; |
8464 | ||
fc0e6df6 PB |
8465 | elf_tdata (abfd)->cverdefs = freeidx; |
8466 | } | |
252b5132 | 8467 | |
fc0e6df6 PB |
8468 | /* Create a default version based on the soname. */ |
8469 | if (default_imported_symver) | |
8470 | { | |
8471 | Elf_Internal_Verdef *iverdef; | |
8472 | Elf_Internal_Verdaux *iverdaux; | |
252b5132 | 8473 | |
5bb3703f | 8474 | iverdef = &elf_tdata (abfd)->verdef[freeidx - 1]; |
252b5132 | 8475 | |
fc0e6df6 PB |
8476 | iverdef->vd_version = VER_DEF_CURRENT; |
8477 | iverdef->vd_flags = 0; | |
8478 | iverdef->vd_ndx = freeidx; | |
8479 | iverdef->vd_cnt = 1; | |
252b5132 | 8480 | |
fc0e6df6 | 8481 | iverdef->vd_bfd = abfd; |
252b5132 | 8482 | |
fc0e6df6 PB |
8483 | iverdef->vd_nodename = bfd_elf_get_dt_soname (abfd); |
8484 | if (iverdef->vd_nodename == NULL) | |
d0fb9a8d | 8485 | goto error_return_verdef; |
fc0e6df6 | 8486 | iverdef->vd_nextdef = NULL; |
601a03ba AM |
8487 | iverdef->vd_auxptr = ((struct elf_internal_verdaux *) |
8488 | bfd_zalloc (abfd, sizeof (Elf_Internal_Verdaux))); | |
d0fb9a8d JJ |
8489 | if (iverdef->vd_auxptr == NULL) |
8490 | goto error_return_verdef; | |
252b5132 | 8491 | |
fc0e6df6 PB |
8492 | iverdaux = iverdef->vd_auxptr; |
8493 | iverdaux->vda_nodename = iverdef->vd_nodename; | |
252b5132 RH |
8494 | } |
8495 | ||
b34976b6 | 8496 | return TRUE; |
252b5132 RH |
8497 | |
8498 | error_return: | |
5ed6aba4 | 8499 | if (contents != NULL) |
252b5132 | 8500 | free (contents); |
b34976b6 | 8501 | return FALSE; |
252b5132 RH |
8502 | } |
8503 | \f | |
8504 | asymbol * | |
217aa764 | 8505 | _bfd_elf_make_empty_symbol (bfd *abfd) |
252b5132 RH |
8506 | { |
8507 | elf_symbol_type *newsym; | |
8508 | ||
201159ec | 8509 | newsym = (elf_symbol_type *) bfd_zalloc (abfd, sizeof * newsym); |
252b5132 RH |
8510 | if (!newsym) |
8511 | return NULL; | |
201159ec NC |
8512 | newsym->symbol.the_bfd = abfd; |
8513 | return &newsym->symbol; | |
252b5132 RH |
8514 | } |
8515 | ||
8516 | void | |
217aa764 AM |
8517 | _bfd_elf_get_symbol_info (bfd *abfd ATTRIBUTE_UNUSED, |
8518 | asymbol *symbol, | |
8519 | symbol_info *ret) | |
252b5132 RH |
8520 | { |
8521 | bfd_symbol_info (symbol, ret); | |
8522 | } | |
8523 | ||
8524 | /* Return whether a symbol name implies a local symbol. Most targets | |
8525 | use this function for the is_local_label_name entry point, but some | |
8526 | override it. */ | |
8527 | ||
b34976b6 | 8528 | bfd_boolean |
217aa764 AM |
8529 | _bfd_elf_is_local_label_name (bfd *abfd ATTRIBUTE_UNUSED, |
8530 | const char *name) | |
252b5132 RH |
8531 | { |
8532 | /* Normal local symbols start with ``.L''. */ | |
8533 | if (name[0] == '.' && name[1] == 'L') | |
b34976b6 | 8534 | return TRUE; |
252b5132 RH |
8535 | |
8536 | /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate | |
8537 | DWARF debugging symbols starting with ``..''. */ | |
8538 | if (name[0] == '.' && name[1] == '.') | |
b34976b6 | 8539 | return TRUE; |
252b5132 RH |
8540 | |
8541 | /* gcc will sometimes generate symbols beginning with ``_.L_'' when | |
8542 | emitting DWARF debugging output. I suspect this is actually a | |
8543 | small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call | |
8544 | ASM_GENERATE_INTERNAL_LABEL, and this causes the leading | |
8545 | underscore to be emitted on some ELF targets). For ease of use, | |
8546 | we treat such symbols as local. */ | |
8547 | if (name[0] == '_' && name[1] == '.' && name[2] == 'L' && name[3] == '_') | |
b34976b6 | 8548 | return TRUE; |
252b5132 | 8549 | |
b1fa9dd6 NC |
8550 | /* Treat assembler generated fake symbols, dollar local labels and |
8551 | forward-backward labels (aka local labels) as locals. | |
8552 | These labels have the form: | |
8553 | ||
8554 | L0^A.* (fake symbols) | |
8555 | ||
8556 | [.]?L[0123456789]+{^A|^B}[0123456789]* (local labels) | |
8557 | ||
8558 | Versions which start with .L will have already been matched above, | |
8559 | so we only need to match the rest. */ | |
8560 | if (name[0] == 'L' && ISDIGIT (name[1])) | |
8561 | { | |
8562 | bfd_boolean ret = FALSE; | |
8563 | const char * p; | |
8564 | char c; | |
8565 | ||
8566 | for (p = name + 2; (c = *p); p++) | |
8567 | { | |
8568 | if (c == 1 || c == 2) | |
8569 | { | |
8570 | if (c == 1 && p == name + 2) | |
8571 | /* A fake symbol. */ | |
8572 | return TRUE; | |
8573 | ||
8574 | /* FIXME: We are being paranoid here and treating symbols like | |
8575 | L0^Bfoo as if there were non-local, on the grounds that the | |
8576 | assembler will never generate them. But can any symbol | |
8577 | containing an ASCII value in the range 1-31 ever be anything | |
8578 | other than some kind of local ? */ | |
8579 | ret = TRUE; | |
8580 | } | |
8581 | ||
8582 | if (! ISDIGIT (c)) | |
8583 | { | |
8584 | ret = FALSE; | |
8585 | break; | |
8586 | } | |
8587 | } | |
8588 | return ret; | |
8589 | } | |
ffa54770 | 8590 | |
b34976b6 | 8591 | return FALSE; |
252b5132 RH |
8592 | } |
8593 | ||
8594 | alent * | |
217aa764 AM |
8595 | _bfd_elf_get_lineno (bfd *abfd ATTRIBUTE_UNUSED, |
8596 | asymbol *symbol ATTRIBUTE_UNUSED) | |
252b5132 RH |
8597 | { |
8598 | abort (); | |
8599 | return NULL; | |
8600 | } | |
8601 | ||
b34976b6 | 8602 | bfd_boolean |
217aa764 AM |
8603 | _bfd_elf_set_arch_mach (bfd *abfd, |
8604 | enum bfd_architecture arch, | |
8605 | unsigned long machine) | |
252b5132 RH |
8606 | { |
8607 | /* If this isn't the right architecture for this backend, and this | |
8608 | isn't the generic backend, fail. */ | |
8609 | if (arch != get_elf_backend_data (abfd)->arch | |
8610 | && arch != bfd_arch_unknown | |
8611 | && get_elf_backend_data (abfd)->arch != bfd_arch_unknown) | |
b34976b6 | 8612 | return FALSE; |
252b5132 RH |
8613 | |
8614 | return bfd_default_set_arch_mach (abfd, arch, machine); | |
8615 | } | |
8616 | ||
d1fad7c6 NC |
8617 | /* Find the nearest line to a particular section and offset, |
8618 | for error reporting. */ | |
8619 | ||
b34976b6 | 8620 | bfd_boolean |
217aa764 | 8621 | _bfd_elf_find_nearest_line (bfd *abfd, |
217aa764 | 8622 | asymbol **symbols, |
fb167eb2 | 8623 | asection *section, |
217aa764 AM |
8624 | bfd_vma offset, |
8625 | const char **filename_ptr, | |
8626 | const char **functionname_ptr, | |
fb167eb2 AM |
8627 | unsigned int *line_ptr, |
8628 | unsigned int *discriminator_ptr) | |
d1fad7c6 | 8629 | { |
b34976b6 | 8630 | bfd_boolean found; |
d1fad7c6 | 8631 | |
fb167eb2 | 8632 | if (_bfd_dwarf2_find_nearest_line (abfd, symbols, NULL, section, offset, |
4e8a9624 | 8633 | filename_ptr, functionname_ptr, |
fb167eb2 AM |
8634 | line_ptr, discriminator_ptr, |
8635 | dwarf_debug_sections, 0, | |
e00e8198 AM |
8636 | &elf_tdata (abfd)->dwarf2_find_line_info) |
8637 | || _bfd_dwarf1_find_nearest_line (abfd, symbols, section, offset, | |
8638 | filename_ptr, functionname_ptr, | |
8639 | line_ptr)) | |
d1fad7c6 NC |
8640 | { |
8641 | if (!*functionname_ptr) | |
e00e8198 AM |
8642 | _bfd_elf_find_function (abfd, symbols, section, offset, |
8643 | *filename_ptr ? NULL : filename_ptr, | |
8644 | functionname_ptr); | |
b34976b6 | 8645 | return TRUE; |
d1fad7c6 NC |
8646 | } |
8647 | ||
8648 | if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset, | |
4e8a9624 AM |
8649 | &found, filename_ptr, |
8650 | functionname_ptr, line_ptr, | |
8651 | &elf_tdata (abfd)->line_info)) | |
b34976b6 | 8652 | return FALSE; |
dc43ada5 | 8653 | if (found && (*functionname_ptr || *line_ptr)) |
b34976b6 | 8654 | return TRUE; |
d1fad7c6 NC |
8655 | |
8656 | if (symbols == NULL) | |
b34976b6 | 8657 | return FALSE; |
d1fad7c6 | 8658 | |
e00e8198 AM |
8659 | if (! _bfd_elf_find_function (abfd, symbols, section, offset, |
8660 | filename_ptr, functionname_ptr)) | |
b34976b6 | 8661 | return FALSE; |
d1fad7c6 | 8662 | |
252b5132 | 8663 | *line_ptr = 0; |
b34976b6 | 8664 | return TRUE; |
252b5132 RH |
8665 | } |
8666 | ||
5420f73d L |
8667 | /* Find the line for a symbol. */ |
8668 | ||
8669 | bfd_boolean | |
8670 | _bfd_elf_find_line (bfd *abfd, asymbol **symbols, asymbol *symbol, | |
8671 | const char **filename_ptr, unsigned int *line_ptr) | |
9b8d1a36 | 8672 | { |
fb167eb2 AM |
8673 | return _bfd_dwarf2_find_nearest_line (abfd, symbols, symbol, NULL, 0, |
8674 | filename_ptr, NULL, line_ptr, NULL, | |
8675 | dwarf_debug_sections, 0, | |
8676 | &elf_tdata (abfd)->dwarf2_find_line_info); | |
5420f73d L |
8677 | } |
8678 | ||
4ab527b0 FF |
8679 | /* After a call to bfd_find_nearest_line, successive calls to |
8680 | bfd_find_inliner_info can be used to get source information about | |
8681 | each level of function inlining that terminated at the address | |
8682 | passed to bfd_find_nearest_line. Currently this is only supported | |
8683 | for DWARF2 with appropriate DWARF3 extensions. */ | |
8684 | ||
8685 | bfd_boolean | |
8686 | _bfd_elf_find_inliner_info (bfd *abfd, | |
8687 | const char **filename_ptr, | |
8688 | const char **functionname_ptr, | |
8689 | unsigned int *line_ptr) | |
8690 | { | |
8691 | bfd_boolean found; | |
8692 | found = _bfd_dwarf2_find_inliner_info (abfd, filename_ptr, | |
8693 | functionname_ptr, line_ptr, | |
8694 | & elf_tdata (abfd)->dwarf2_find_line_info); | |
8695 | return found; | |
8696 | } | |
8697 | ||
252b5132 | 8698 | int |
a6b96beb | 8699 | _bfd_elf_sizeof_headers (bfd *abfd, struct bfd_link_info *info) |
252b5132 | 8700 | { |
8ded5a0f AM |
8701 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
8702 | int ret = bed->s->sizeof_ehdr; | |
252b5132 | 8703 | |
0e1862bb | 8704 | if (!bfd_link_relocatable (info)) |
8ded5a0f | 8705 | { |
12bd6957 | 8706 | bfd_size_type phdr_size = elf_program_header_size (abfd); |
8ded5a0f | 8707 | |
62d7a5f6 AM |
8708 | if (phdr_size == (bfd_size_type) -1) |
8709 | { | |
8710 | struct elf_segment_map *m; | |
8711 | ||
8712 | phdr_size = 0; | |
12bd6957 | 8713 | for (m = elf_seg_map (abfd); m != NULL; m = m->next) |
62d7a5f6 | 8714 | phdr_size += bed->s->sizeof_phdr; |
8ded5a0f | 8715 | |
62d7a5f6 AM |
8716 | if (phdr_size == 0) |
8717 | phdr_size = get_program_header_size (abfd, info); | |
8718 | } | |
8ded5a0f | 8719 | |
12bd6957 | 8720 | elf_program_header_size (abfd) = phdr_size; |
8ded5a0f AM |
8721 | ret += phdr_size; |
8722 | } | |
8723 | ||
252b5132 RH |
8724 | return ret; |
8725 | } | |
8726 | ||
b34976b6 | 8727 | bfd_boolean |
217aa764 AM |
8728 | _bfd_elf_set_section_contents (bfd *abfd, |
8729 | sec_ptr section, | |
0f867abe | 8730 | const void *location, |
217aa764 AM |
8731 | file_ptr offset, |
8732 | bfd_size_type count) | |
252b5132 RH |
8733 | { |
8734 | Elf_Internal_Shdr *hdr; | |
1b6aeedb | 8735 | file_ptr pos; |
252b5132 RH |
8736 | |
8737 | if (! abfd->output_has_begun | |
217aa764 | 8738 | && ! _bfd_elf_compute_section_file_positions (abfd, NULL)) |
b34976b6 | 8739 | return FALSE; |
252b5132 | 8740 | |
0ce398f1 L |
8741 | if (!count) |
8742 | return TRUE; | |
8743 | ||
252b5132 | 8744 | hdr = &elf_section_data (section)->this_hdr; |
0ce398f1 L |
8745 | if (hdr->sh_offset == (file_ptr) -1) |
8746 | { | |
8747 | /* We must compress this section. Write output to the buffer. */ | |
8748 | unsigned char *contents = hdr->contents; | |
8749 | if ((offset + count) > hdr->sh_size | |
8750 | || (section->flags & SEC_ELF_COMPRESS) == 0 | |
8751 | || contents == NULL) | |
8752 | abort (); | |
8753 | memcpy (contents + offset, location, count); | |
8754 | return TRUE; | |
8755 | } | |
dc810e39 AM |
8756 | pos = hdr->sh_offset + offset; |
8757 | if (bfd_seek (abfd, pos, SEEK_SET) != 0 | |
8758 | || bfd_bwrite (location, count, abfd) != count) | |
b34976b6 | 8759 | return FALSE; |
252b5132 | 8760 | |
b34976b6 | 8761 | return TRUE; |
252b5132 RH |
8762 | } |
8763 | ||
8764 | void | |
217aa764 AM |
8765 | _bfd_elf_no_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED, |
8766 | arelent *cache_ptr ATTRIBUTE_UNUSED, | |
8767 | Elf_Internal_Rela *dst ATTRIBUTE_UNUSED) | |
252b5132 RH |
8768 | { |
8769 | abort (); | |
8770 | } | |
8771 | ||
252b5132 RH |
8772 | /* Try to convert a non-ELF reloc into an ELF one. */ |
8773 | ||
b34976b6 | 8774 | bfd_boolean |
217aa764 | 8775 | _bfd_elf_validate_reloc (bfd *abfd, arelent *areloc) |
252b5132 | 8776 | { |
c044fabd | 8777 | /* Check whether we really have an ELF howto. */ |
252b5132 RH |
8778 | |
8779 | if ((*areloc->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec) | |
8780 | { | |
8781 | bfd_reloc_code_real_type code; | |
8782 | reloc_howto_type *howto; | |
8783 | ||
8784 | /* Alien reloc: Try to determine its type to replace it with an | |
c044fabd | 8785 | equivalent ELF reloc. */ |
252b5132 RH |
8786 | |
8787 | if (areloc->howto->pc_relative) | |
8788 | { | |
8789 | switch (areloc->howto->bitsize) | |
8790 | { | |
8791 | case 8: | |
8792 | code = BFD_RELOC_8_PCREL; | |
8793 | break; | |
8794 | case 12: | |
8795 | code = BFD_RELOC_12_PCREL; | |
8796 | break; | |
8797 | case 16: | |
8798 | code = BFD_RELOC_16_PCREL; | |
8799 | break; | |
8800 | case 24: | |
8801 | code = BFD_RELOC_24_PCREL; | |
8802 | break; | |
8803 | case 32: | |
8804 | code = BFD_RELOC_32_PCREL; | |
8805 | break; | |
8806 | case 64: | |
8807 | code = BFD_RELOC_64_PCREL; | |
8808 | break; | |
8809 | default: | |
8810 | goto fail; | |
8811 | } | |
8812 | ||
8813 | howto = bfd_reloc_type_lookup (abfd, code); | |
8814 | ||
8815 | if (areloc->howto->pcrel_offset != howto->pcrel_offset) | |
8816 | { | |
8817 | if (howto->pcrel_offset) | |
8818 | areloc->addend += areloc->address; | |
8819 | else | |
8820 | areloc->addend -= areloc->address; /* addend is unsigned!! */ | |
8821 | } | |
8822 | } | |
8823 | else | |
8824 | { | |
8825 | switch (areloc->howto->bitsize) | |
8826 | { | |
8827 | case 8: | |
8828 | code = BFD_RELOC_8; | |
8829 | break; | |
8830 | case 14: | |
8831 | code = BFD_RELOC_14; | |
8832 | break; | |
8833 | case 16: | |
8834 | code = BFD_RELOC_16; | |
8835 | break; | |
8836 | case 26: | |
8837 | code = BFD_RELOC_26; | |
8838 | break; | |
8839 | case 32: | |
8840 | code = BFD_RELOC_32; | |
8841 | break; | |
8842 | case 64: | |
8843 | code = BFD_RELOC_64; | |
8844 | break; | |
8845 | default: | |
8846 | goto fail; | |
8847 | } | |
8848 | ||
8849 | howto = bfd_reloc_type_lookup (abfd, code); | |
8850 | } | |
8851 | ||
8852 | if (howto) | |
8853 | areloc->howto = howto; | |
8854 | else | |
8855 | goto fail; | |
8856 | } | |
8857 | ||
b34976b6 | 8858 | return TRUE; |
252b5132 RH |
8859 | |
8860 | fail: | |
4eca0228 | 8861 | _bfd_error_handler |
695344c0 | 8862 | /* xgettext:c-format */ |
d003868e AM |
8863 | (_("%B: unsupported relocation type %s"), |
8864 | abfd, areloc->howto->name); | |
252b5132 | 8865 | bfd_set_error (bfd_error_bad_value); |
b34976b6 | 8866 | return FALSE; |
252b5132 RH |
8867 | } |
8868 | ||
b34976b6 | 8869 | bfd_boolean |
217aa764 | 8870 | _bfd_elf_close_and_cleanup (bfd *abfd) |
252b5132 | 8871 | { |
d9071b0c TG |
8872 | struct elf_obj_tdata *tdata = elf_tdata (abfd); |
8873 | if (bfd_get_format (abfd) == bfd_object && tdata != NULL) | |
252b5132 | 8874 | { |
c0355132 | 8875 | if (elf_tdata (abfd)->o != NULL && elf_shstrtab (abfd) != NULL) |
2b0f7ef9 | 8876 | _bfd_elf_strtab_free (elf_shstrtab (abfd)); |
d9071b0c | 8877 | _bfd_dwarf2_cleanup_debug_info (abfd, &tdata->dwarf2_find_line_info); |
252b5132 RH |
8878 | } |
8879 | ||
8880 | return _bfd_generic_close_and_cleanup (abfd); | |
8881 | } | |
8882 | ||
8883 | /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY | |
8884 | in the relocation's offset. Thus we cannot allow any sort of sanity | |
8885 | range-checking to interfere. There is nothing else to do in processing | |
8886 | this reloc. */ | |
8887 | ||
8888 | bfd_reloc_status_type | |
217aa764 AM |
8889 | _bfd_elf_rel_vtable_reloc_fn |
8890 | (bfd *abfd ATTRIBUTE_UNUSED, arelent *re ATTRIBUTE_UNUSED, | |
fc0a2244 | 8891 | struct bfd_symbol *symbol ATTRIBUTE_UNUSED, |
217aa764 AM |
8892 | void *data ATTRIBUTE_UNUSED, asection *is ATTRIBUTE_UNUSED, |
8893 | bfd *obfd ATTRIBUTE_UNUSED, char **errmsg ATTRIBUTE_UNUSED) | |
252b5132 RH |
8894 | { |
8895 | return bfd_reloc_ok; | |
8896 | } | |
252b5132 RH |
8897 | \f |
8898 | /* Elf core file support. Much of this only works on native | |
8899 | toolchains, since we rely on knowing the | |
8900 | machine-dependent procfs structure in order to pick | |
c044fabd | 8901 | out details about the corefile. */ |
252b5132 RH |
8902 | |
8903 | #ifdef HAVE_SYS_PROCFS_H | |
16231b7b DG |
8904 | /* Needed for new procfs interface on sparc-solaris. */ |
8905 | # define _STRUCTURED_PROC 1 | |
252b5132 RH |
8906 | # include <sys/procfs.h> |
8907 | #endif | |
8908 | ||
261b8d08 PA |
8909 | /* Return a PID that identifies a "thread" for threaded cores, or the |
8910 | PID of the main process for non-threaded cores. */ | |
252b5132 RH |
8911 | |
8912 | static int | |
217aa764 | 8913 | elfcore_make_pid (bfd *abfd) |
252b5132 | 8914 | { |
261b8d08 PA |
8915 | int pid; |
8916 | ||
228e534f | 8917 | pid = elf_tdata (abfd)->core->lwpid; |
261b8d08 | 8918 | if (pid == 0) |
228e534f | 8919 | pid = elf_tdata (abfd)->core->pid; |
261b8d08 PA |
8920 | |
8921 | return pid; | |
252b5132 RH |
8922 | } |
8923 | ||
252b5132 RH |
8924 | /* If there isn't a section called NAME, make one, using |
8925 | data from SECT. Note, this function will generate a | |
8926 | reference to NAME, so you shouldn't deallocate or | |
c044fabd | 8927 | overwrite it. */ |
252b5132 | 8928 | |
b34976b6 | 8929 | static bfd_boolean |
217aa764 | 8930 | elfcore_maybe_make_sect (bfd *abfd, char *name, asection *sect) |
252b5132 | 8931 | { |
c044fabd | 8932 | asection *sect2; |
252b5132 RH |
8933 | |
8934 | if (bfd_get_section_by_name (abfd, name) != NULL) | |
b34976b6 | 8935 | return TRUE; |
252b5132 | 8936 | |
117ed4f8 | 8937 | sect2 = bfd_make_section_with_flags (abfd, name, sect->flags); |
252b5132 | 8938 | if (sect2 == NULL) |
b34976b6 | 8939 | return FALSE; |
252b5132 | 8940 | |
eea6121a | 8941 | sect2->size = sect->size; |
252b5132 | 8942 | sect2->filepos = sect->filepos; |
252b5132 | 8943 | sect2->alignment_power = sect->alignment_power; |
b34976b6 | 8944 | return TRUE; |
252b5132 RH |
8945 | } |
8946 | ||
bb0082d6 AM |
8947 | /* Create a pseudosection containing SIZE bytes at FILEPOS. This |
8948 | actually creates up to two pseudosections: | |
8949 | - For the single-threaded case, a section named NAME, unless | |
8950 | such a section already exists. | |
8951 | - For the multi-threaded case, a section named "NAME/PID", where | |
8952 | PID is elfcore_make_pid (abfd). | |
8953 | Both pseudosections have identical contents. */ | |
b34976b6 | 8954 | bfd_boolean |
217aa764 AM |
8955 | _bfd_elfcore_make_pseudosection (bfd *abfd, |
8956 | char *name, | |
8957 | size_t size, | |
8958 | ufile_ptr filepos) | |
bb0082d6 AM |
8959 | { |
8960 | char buf[100]; | |
8961 | char *threaded_name; | |
d4c88bbb | 8962 | size_t len; |
bb0082d6 AM |
8963 | asection *sect; |
8964 | ||
8965 | /* Build the section name. */ | |
8966 | ||
8967 | sprintf (buf, "%s/%d", name, elfcore_make_pid (abfd)); | |
d4c88bbb | 8968 | len = strlen (buf) + 1; |
a50b1753 | 8969 | threaded_name = (char *) bfd_alloc (abfd, len); |
bb0082d6 | 8970 | if (threaded_name == NULL) |
b34976b6 | 8971 | return FALSE; |
d4c88bbb | 8972 | memcpy (threaded_name, buf, len); |
bb0082d6 | 8973 | |
117ed4f8 AM |
8974 | sect = bfd_make_section_anyway_with_flags (abfd, threaded_name, |
8975 | SEC_HAS_CONTENTS); | |
bb0082d6 | 8976 | if (sect == NULL) |
b34976b6 | 8977 | return FALSE; |
eea6121a | 8978 | sect->size = size; |
bb0082d6 | 8979 | sect->filepos = filepos; |
bb0082d6 AM |
8980 | sect->alignment_power = 2; |
8981 | ||
936e320b | 8982 | return elfcore_maybe_make_sect (abfd, name, sect); |
bb0082d6 AM |
8983 | } |
8984 | ||
252b5132 | 8985 | /* prstatus_t exists on: |
4a938328 | 8986 | solaris 2.5+ |
252b5132 RH |
8987 | linux 2.[01] + glibc |
8988 | unixware 4.2 | |
8989 | */ | |
8990 | ||
8991 | #if defined (HAVE_PRSTATUS_T) | |
a7b97311 | 8992 | |
b34976b6 | 8993 | static bfd_boolean |
217aa764 | 8994 | elfcore_grok_prstatus (bfd *abfd, Elf_Internal_Note *note) |
252b5132 | 8995 | { |
eea6121a | 8996 | size_t size; |
7ee38065 | 8997 | int offset; |
252b5132 | 8998 | |
4a938328 MS |
8999 | if (note->descsz == sizeof (prstatus_t)) |
9000 | { | |
9001 | prstatus_t prstat; | |
252b5132 | 9002 | |
eea6121a | 9003 | size = sizeof (prstat.pr_reg); |
7ee38065 | 9004 | offset = offsetof (prstatus_t, pr_reg); |
4a938328 | 9005 | memcpy (&prstat, note->descdata, sizeof (prstat)); |
252b5132 | 9006 | |
fa49d224 NC |
9007 | /* Do not overwrite the core signal if it |
9008 | has already been set by another thread. */ | |
228e534f AM |
9009 | if (elf_tdata (abfd)->core->signal == 0) |
9010 | elf_tdata (abfd)->core->signal = prstat.pr_cursig; | |
9011 | if (elf_tdata (abfd)->core->pid == 0) | |
9012 | elf_tdata (abfd)->core->pid = prstat.pr_pid; | |
252b5132 | 9013 | |
4a938328 MS |
9014 | /* pr_who exists on: |
9015 | solaris 2.5+ | |
9016 | unixware 4.2 | |
9017 | pr_who doesn't exist on: | |
9018 | linux 2.[01] | |
9019 | */ | |
252b5132 | 9020 | #if defined (HAVE_PRSTATUS_T_PR_WHO) |
228e534f | 9021 | elf_tdata (abfd)->core->lwpid = prstat.pr_who; |
261b8d08 | 9022 | #else |
228e534f | 9023 | elf_tdata (abfd)->core->lwpid = prstat.pr_pid; |
252b5132 | 9024 | #endif |
4a938328 | 9025 | } |
7ee38065 | 9026 | #if defined (HAVE_PRSTATUS32_T) |
4a938328 MS |
9027 | else if (note->descsz == sizeof (prstatus32_t)) |
9028 | { | |
9029 | /* 64-bit host, 32-bit corefile */ | |
9030 | prstatus32_t prstat; | |
9031 | ||
eea6121a | 9032 | size = sizeof (prstat.pr_reg); |
7ee38065 | 9033 | offset = offsetof (prstatus32_t, pr_reg); |
4a938328 MS |
9034 | memcpy (&prstat, note->descdata, sizeof (prstat)); |
9035 | ||
fa49d224 NC |
9036 | /* Do not overwrite the core signal if it |
9037 | has already been set by another thread. */ | |
228e534f AM |
9038 | if (elf_tdata (abfd)->core->signal == 0) |
9039 | elf_tdata (abfd)->core->signal = prstat.pr_cursig; | |
9040 | if (elf_tdata (abfd)->core->pid == 0) | |
9041 | elf_tdata (abfd)->core->pid = prstat.pr_pid; | |
4a938328 MS |
9042 | |
9043 | /* pr_who exists on: | |
9044 | solaris 2.5+ | |
9045 | unixware 4.2 | |
9046 | pr_who doesn't exist on: | |
9047 | linux 2.[01] | |
9048 | */ | |
7ee38065 | 9049 | #if defined (HAVE_PRSTATUS32_T_PR_WHO) |
228e534f | 9050 | elf_tdata (abfd)->core->lwpid = prstat.pr_who; |
261b8d08 | 9051 | #else |
228e534f | 9052 | elf_tdata (abfd)->core->lwpid = prstat.pr_pid; |
4a938328 MS |
9053 | #endif |
9054 | } | |
7ee38065 | 9055 | #endif /* HAVE_PRSTATUS32_T */ |
4a938328 MS |
9056 | else |
9057 | { | |
9058 | /* Fail - we don't know how to handle any other | |
9059 | note size (ie. data object type). */ | |
b34976b6 | 9060 | return TRUE; |
4a938328 | 9061 | } |
252b5132 | 9062 | |
bb0082d6 | 9063 | /* Make a ".reg/999" section and a ".reg" section. */ |
936e320b | 9064 | return _bfd_elfcore_make_pseudosection (abfd, ".reg", |
eea6121a | 9065 | size, note->descpos + offset); |
252b5132 RH |
9066 | } |
9067 | #endif /* defined (HAVE_PRSTATUS_T) */ | |
9068 | ||
bb0082d6 | 9069 | /* Create a pseudosection containing the exact contents of NOTE. */ |
b34976b6 | 9070 | static bfd_boolean |
217aa764 AM |
9071 | elfcore_make_note_pseudosection (bfd *abfd, |
9072 | char *name, | |
9073 | Elf_Internal_Note *note) | |
252b5132 | 9074 | { |
936e320b AM |
9075 | return _bfd_elfcore_make_pseudosection (abfd, name, |
9076 | note->descsz, note->descpos); | |
252b5132 RH |
9077 | } |
9078 | ||
ff08c6bb JB |
9079 | /* There isn't a consistent prfpregset_t across platforms, |
9080 | but it doesn't matter, because we don't have to pick this | |
c044fabd KH |
9081 | data structure apart. */ |
9082 | ||
b34976b6 | 9083 | static bfd_boolean |
217aa764 | 9084 | elfcore_grok_prfpreg (bfd *abfd, Elf_Internal_Note *note) |
ff08c6bb JB |
9085 | { |
9086 | return elfcore_make_note_pseudosection (abfd, ".reg2", note); | |
9087 | } | |
9088 | ||
ff08c6bb | 9089 | /* Linux dumps the Intel SSE regs in a note named "LINUX" with a note |
971d4640 | 9090 | type of NT_PRXFPREG. Just include the whole note's contents |
ff08c6bb | 9091 | literally. */ |
c044fabd | 9092 | |
b34976b6 | 9093 | static bfd_boolean |
217aa764 | 9094 | elfcore_grok_prxfpreg (bfd *abfd, Elf_Internal_Note *note) |
ff08c6bb JB |
9095 | { |
9096 | return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note); | |
9097 | } | |
9098 | ||
4339cae0 L |
9099 | /* Linux dumps the Intel XSAVE extended state in a note named "LINUX" |
9100 | with a note type of NT_X86_XSTATE. Just include the whole note's | |
9101 | contents literally. */ | |
9102 | ||
9103 | static bfd_boolean | |
9104 | elfcore_grok_xstatereg (bfd *abfd, Elf_Internal_Note *note) | |
9105 | { | |
9106 | return elfcore_make_note_pseudosection (abfd, ".reg-xstate", note); | |
9107 | } | |
9108 | ||
97753bd5 AM |
9109 | static bfd_boolean |
9110 | elfcore_grok_ppc_vmx (bfd *abfd, Elf_Internal_Note *note) | |
9111 | { | |
9112 | return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vmx", note); | |
9113 | } | |
9114 | ||
89eeb0bc LM |
9115 | static bfd_boolean |
9116 | elfcore_grok_ppc_vsx (bfd *abfd, Elf_Internal_Note *note) | |
9117 | { | |
9118 | return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vsx", note); | |
9119 | } | |
97753bd5 | 9120 | |
0675e188 UW |
9121 | static bfd_boolean |
9122 | elfcore_grok_s390_high_gprs (bfd *abfd, Elf_Internal_Note *note) | |
9123 | { | |
9124 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-high-gprs", note); | |
9125 | } | |
9126 | ||
d7eeb400 MS |
9127 | static bfd_boolean |
9128 | elfcore_grok_s390_timer (bfd *abfd, Elf_Internal_Note *note) | |
9129 | { | |
9130 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-timer", note); | |
9131 | } | |
9132 | ||
9133 | static bfd_boolean | |
9134 | elfcore_grok_s390_todcmp (bfd *abfd, Elf_Internal_Note *note) | |
9135 | { | |
9136 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-todcmp", note); | |
9137 | } | |
9138 | ||
9139 | static bfd_boolean | |
9140 | elfcore_grok_s390_todpreg (bfd *abfd, Elf_Internal_Note *note) | |
9141 | { | |
9142 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-todpreg", note); | |
9143 | } | |
9144 | ||
9145 | static bfd_boolean | |
9146 | elfcore_grok_s390_ctrs (bfd *abfd, Elf_Internal_Note *note) | |
9147 | { | |
9148 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-ctrs", note); | |
9149 | } | |
9150 | ||
9151 | static bfd_boolean | |
9152 | elfcore_grok_s390_prefix (bfd *abfd, Elf_Internal_Note *note) | |
9153 | { | |
9154 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-prefix", note); | |
9155 | } | |
9156 | ||
355b81d9 UW |
9157 | static bfd_boolean |
9158 | elfcore_grok_s390_last_break (bfd *abfd, Elf_Internal_Note *note) | |
9159 | { | |
9160 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-last-break", note); | |
9161 | } | |
9162 | ||
9163 | static bfd_boolean | |
9164 | elfcore_grok_s390_system_call (bfd *abfd, Elf_Internal_Note *note) | |
9165 | { | |
9166 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-system-call", note); | |
9167 | } | |
9168 | ||
abb3f6cc NC |
9169 | static bfd_boolean |
9170 | elfcore_grok_s390_tdb (bfd *abfd, Elf_Internal_Note *note) | |
9171 | { | |
9172 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-tdb", note); | |
9173 | } | |
9174 | ||
4ef9f41a AA |
9175 | static bfd_boolean |
9176 | elfcore_grok_s390_vxrs_low (bfd *abfd, Elf_Internal_Note *note) | |
9177 | { | |
9178 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-vxrs-low", note); | |
9179 | } | |
9180 | ||
9181 | static bfd_boolean | |
9182 | elfcore_grok_s390_vxrs_high (bfd *abfd, Elf_Internal_Note *note) | |
9183 | { | |
9184 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-vxrs-high", note); | |
9185 | } | |
9186 | ||
faa9a424 UW |
9187 | static bfd_boolean |
9188 | elfcore_grok_arm_vfp (bfd *abfd, Elf_Internal_Note *note) | |
9189 | { | |
9190 | return elfcore_make_note_pseudosection (abfd, ".reg-arm-vfp", note); | |
9191 | } | |
9192 | ||
652451f8 YZ |
9193 | static bfd_boolean |
9194 | elfcore_grok_aarch_tls (bfd *abfd, Elf_Internal_Note *note) | |
9195 | { | |
9196 | return elfcore_make_note_pseudosection (abfd, ".reg-aarch-tls", note); | |
9197 | } | |
9198 | ||
9199 | static bfd_boolean | |
9200 | elfcore_grok_aarch_hw_break (bfd *abfd, Elf_Internal_Note *note) | |
9201 | { | |
9202 | return elfcore_make_note_pseudosection (abfd, ".reg-aarch-hw-break", note); | |
9203 | } | |
9204 | ||
9205 | static bfd_boolean | |
9206 | elfcore_grok_aarch_hw_watch (bfd *abfd, Elf_Internal_Note *note) | |
9207 | { | |
9208 | return elfcore_make_note_pseudosection (abfd, ".reg-aarch-hw-watch", note); | |
9209 | } | |
9210 | ||
252b5132 | 9211 | #if defined (HAVE_PRPSINFO_T) |
4a938328 | 9212 | typedef prpsinfo_t elfcore_psinfo_t; |
7ee38065 | 9213 | #if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */ |
4a938328 MS |
9214 | typedef prpsinfo32_t elfcore_psinfo32_t; |
9215 | #endif | |
252b5132 RH |
9216 | #endif |
9217 | ||
9218 | #if defined (HAVE_PSINFO_T) | |
4a938328 | 9219 | typedef psinfo_t elfcore_psinfo_t; |
7ee38065 | 9220 | #if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */ |
4a938328 MS |
9221 | typedef psinfo32_t elfcore_psinfo32_t; |
9222 | #endif | |
252b5132 RH |
9223 | #endif |
9224 | ||
252b5132 RH |
9225 | /* return a malloc'ed copy of a string at START which is at |
9226 | most MAX bytes long, possibly without a terminating '\0'. | |
c044fabd | 9227 | the copy will always have a terminating '\0'. */ |
252b5132 | 9228 | |
936e320b | 9229 | char * |
217aa764 | 9230 | _bfd_elfcore_strndup (bfd *abfd, char *start, size_t max) |
252b5132 | 9231 | { |
dc810e39 | 9232 | char *dups; |
a50b1753 | 9233 | char *end = (char *) memchr (start, '\0', max); |
dc810e39 | 9234 | size_t len; |
252b5132 RH |
9235 | |
9236 | if (end == NULL) | |
9237 | len = max; | |
9238 | else | |
9239 | len = end - start; | |
9240 | ||
a50b1753 | 9241 | dups = (char *) bfd_alloc (abfd, len + 1); |
dc810e39 | 9242 | if (dups == NULL) |
252b5132 RH |
9243 | return NULL; |
9244 | ||
dc810e39 AM |
9245 | memcpy (dups, start, len); |
9246 | dups[len] = '\0'; | |
252b5132 | 9247 | |
dc810e39 | 9248 | return dups; |
252b5132 RH |
9249 | } |
9250 | ||
bb0082d6 | 9251 | #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) |
b34976b6 | 9252 | static bfd_boolean |
217aa764 | 9253 | elfcore_grok_psinfo (bfd *abfd, Elf_Internal_Note *note) |
252b5132 | 9254 | { |
4a938328 MS |
9255 | if (note->descsz == sizeof (elfcore_psinfo_t)) |
9256 | { | |
9257 | elfcore_psinfo_t psinfo; | |
252b5132 | 9258 | |
7ee38065 | 9259 | memcpy (&psinfo, note->descdata, sizeof (psinfo)); |
252b5132 | 9260 | |
335e41d4 | 9261 | #if defined (HAVE_PSINFO_T_PR_PID) || defined (HAVE_PRPSINFO_T_PR_PID) |
228e534f | 9262 | elf_tdata (abfd)->core->pid = psinfo.pr_pid; |
335e41d4 | 9263 | #endif |
228e534f | 9264 | elf_tdata (abfd)->core->program |
936e320b AM |
9265 | = _bfd_elfcore_strndup (abfd, psinfo.pr_fname, |
9266 | sizeof (psinfo.pr_fname)); | |
252b5132 | 9267 | |
228e534f | 9268 | elf_tdata (abfd)->core->command |
936e320b AM |
9269 | = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs, |
9270 | sizeof (psinfo.pr_psargs)); | |
4a938328 | 9271 | } |
7ee38065 | 9272 | #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T) |
4a938328 MS |
9273 | else if (note->descsz == sizeof (elfcore_psinfo32_t)) |
9274 | { | |
9275 | /* 64-bit host, 32-bit corefile */ | |
9276 | elfcore_psinfo32_t psinfo; | |
9277 | ||
7ee38065 | 9278 | memcpy (&psinfo, note->descdata, sizeof (psinfo)); |
252b5132 | 9279 | |
335e41d4 | 9280 | #if defined (HAVE_PSINFO32_T_PR_PID) || defined (HAVE_PRPSINFO32_T_PR_PID) |
228e534f | 9281 | elf_tdata (abfd)->core->pid = psinfo.pr_pid; |
335e41d4 | 9282 | #endif |
228e534f | 9283 | elf_tdata (abfd)->core->program |
936e320b AM |
9284 | = _bfd_elfcore_strndup (abfd, psinfo.pr_fname, |
9285 | sizeof (psinfo.pr_fname)); | |
4a938328 | 9286 | |
228e534f | 9287 | elf_tdata (abfd)->core->command |
936e320b AM |
9288 | = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs, |
9289 | sizeof (psinfo.pr_psargs)); | |
4a938328 MS |
9290 | } |
9291 | #endif | |
9292 | ||
9293 | else | |
9294 | { | |
9295 | /* Fail - we don't know how to handle any other | |
9296 | note size (ie. data object type). */ | |
b34976b6 | 9297 | return TRUE; |
4a938328 | 9298 | } |
252b5132 RH |
9299 | |
9300 | /* Note that for some reason, a spurious space is tacked | |
9301 | onto the end of the args in some (at least one anyway) | |
c044fabd | 9302 | implementations, so strip it off if it exists. */ |
252b5132 RH |
9303 | |
9304 | { | |
228e534f | 9305 | char *command = elf_tdata (abfd)->core->command; |
252b5132 RH |
9306 | int n = strlen (command); |
9307 | ||
9308 | if (0 < n && command[n - 1] == ' ') | |
9309 | command[n - 1] = '\0'; | |
9310 | } | |
9311 | ||
b34976b6 | 9312 | return TRUE; |
252b5132 RH |
9313 | } |
9314 | #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */ | |
9315 | ||
252b5132 | 9316 | #if defined (HAVE_PSTATUS_T) |
b34976b6 | 9317 | static bfd_boolean |
217aa764 | 9318 | elfcore_grok_pstatus (bfd *abfd, Elf_Internal_Note *note) |
252b5132 | 9319 | { |
f572a39d AM |
9320 | if (note->descsz == sizeof (pstatus_t) |
9321 | #if defined (HAVE_PXSTATUS_T) | |
9322 | || note->descsz == sizeof (pxstatus_t) | |
9323 | #endif | |
9324 | ) | |
4a938328 MS |
9325 | { |
9326 | pstatus_t pstat; | |
252b5132 | 9327 | |
4a938328 | 9328 | memcpy (&pstat, note->descdata, sizeof (pstat)); |
252b5132 | 9329 | |
228e534f | 9330 | elf_tdata (abfd)->core->pid = pstat.pr_pid; |
4a938328 | 9331 | } |
7ee38065 | 9332 | #if defined (HAVE_PSTATUS32_T) |
4a938328 MS |
9333 | else if (note->descsz == sizeof (pstatus32_t)) |
9334 | { | |
9335 | /* 64-bit host, 32-bit corefile */ | |
9336 | pstatus32_t pstat; | |
252b5132 | 9337 | |
4a938328 | 9338 | memcpy (&pstat, note->descdata, sizeof (pstat)); |
252b5132 | 9339 | |
228e534f | 9340 | elf_tdata (abfd)->core->pid = pstat.pr_pid; |
4a938328 MS |
9341 | } |
9342 | #endif | |
252b5132 RH |
9343 | /* Could grab some more details from the "representative" |
9344 | lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an | |
c044fabd | 9345 | NT_LWPSTATUS note, presumably. */ |
252b5132 | 9346 | |
b34976b6 | 9347 | return TRUE; |
252b5132 RH |
9348 | } |
9349 | #endif /* defined (HAVE_PSTATUS_T) */ | |
9350 | ||
252b5132 | 9351 | #if defined (HAVE_LWPSTATUS_T) |
b34976b6 | 9352 | static bfd_boolean |
217aa764 | 9353 | elfcore_grok_lwpstatus (bfd *abfd, Elf_Internal_Note *note) |
252b5132 RH |
9354 | { |
9355 | lwpstatus_t lwpstat; | |
9356 | char buf[100]; | |
c044fabd | 9357 | char *name; |
d4c88bbb | 9358 | size_t len; |
c044fabd | 9359 | asection *sect; |
252b5132 | 9360 | |
f572a39d AM |
9361 | if (note->descsz != sizeof (lwpstat) |
9362 | #if defined (HAVE_LWPXSTATUS_T) | |
9363 | && note->descsz != sizeof (lwpxstatus_t) | |
9364 | #endif | |
9365 | ) | |
b34976b6 | 9366 | return TRUE; |
252b5132 RH |
9367 | |
9368 | memcpy (&lwpstat, note->descdata, sizeof (lwpstat)); | |
9369 | ||
228e534f | 9370 | elf_tdata (abfd)->core->lwpid = lwpstat.pr_lwpid; |
a1504221 JB |
9371 | /* Do not overwrite the core signal if it has already been set by |
9372 | another thread. */ | |
228e534f AM |
9373 | if (elf_tdata (abfd)->core->signal == 0) |
9374 | elf_tdata (abfd)->core->signal = lwpstat.pr_cursig; | |
252b5132 | 9375 | |
c044fabd | 9376 | /* Make a ".reg/999" section. */ |
252b5132 RH |
9377 | |
9378 | sprintf (buf, ".reg/%d", elfcore_make_pid (abfd)); | |
d4c88bbb | 9379 | len = strlen (buf) + 1; |
217aa764 | 9380 | name = bfd_alloc (abfd, len); |
252b5132 | 9381 | if (name == NULL) |
b34976b6 | 9382 | return FALSE; |
d4c88bbb | 9383 | memcpy (name, buf, len); |
252b5132 | 9384 | |
117ed4f8 | 9385 | sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); |
252b5132 | 9386 | if (sect == NULL) |
b34976b6 | 9387 | return FALSE; |
252b5132 RH |
9388 | |
9389 | #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT) | |
eea6121a | 9390 | sect->size = sizeof (lwpstat.pr_context.uc_mcontext.gregs); |
252b5132 RH |
9391 | sect->filepos = note->descpos |
9392 | + offsetof (lwpstatus_t, pr_context.uc_mcontext.gregs); | |
9393 | #endif | |
9394 | ||
9395 | #if defined (HAVE_LWPSTATUS_T_PR_REG) | |
eea6121a | 9396 | sect->size = sizeof (lwpstat.pr_reg); |
252b5132 RH |
9397 | sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_reg); |
9398 | #endif | |
9399 | ||
252b5132 RH |
9400 | sect->alignment_power = 2; |
9401 | ||
9402 | if (!elfcore_maybe_make_sect (abfd, ".reg", sect)) | |
b34976b6 | 9403 | return FALSE; |
252b5132 RH |
9404 | |
9405 | /* Make a ".reg2/999" section */ | |
9406 | ||
9407 | sprintf (buf, ".reg2/%d", elfcore_make_pid (abfd)); | |
d4c88bbb | 9408 | len = strlen (buf) + 1; |
217aa764 | 9409 | name = bfd_alloc (abfd, len); |
252b5132 | 9410 | if (name == NULL) |
b34976b6 | 9411 | return FALSE; |
d4c88bbb | 9412 | memcpy (name, buf, len); |
252b5132 | 9413 | |
117ed4f8 | 9414 | sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); |
252b5132 | 9415 | if (sect == NULL) |
b34976b6 | 9416 | return FALSE; |
252b5132 RH |
9417 | |
9418 | #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT) | |
eea6121a | 9419 | sect->size = sizeof (lwpstat.pr_context.uc_mcontext.fpregs); |
252b5132 RH |
9420 | sect->filepos = note->descpos |
9421 | + offsetof (lwpstatus_t, pr_context.uc_mcontext.fpregs); | |
9422 | #endif | |
9423 | ||
9424 | #if defined (HAVE_LWPSTATUS_T_PR_FPREG) | |
eea6121a | 9425 | sect->size = sizeof (lwpstat.pr_fpreg); |
252b5132 RH |
9426 | sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_fpreg); |
9427 | #endif | |
9428 | ||
252b5132 RH |
9429 | sect->alignment_power = 2; |
9430 | ||
936e320b | 9431 | return elfcore_maybe_make_sect (abfd, ".reg2", sect); |
252b5132 RH |
9432 | } |
9433 | #endif /* defined (HAVE_LWPSTATUS_T) */ | |
9434 | ||
b34976b6 | 9435 | static bfd_boolean |
217aa764 | 9436 | elfcore_grok_win32pstatus (bfd *abfd, Elf_Internal_Note *note) |
16e9c715 NC |
9437 | { |
9438 | char buf[30]; | |
c044fabd | 9439 | char *name; |
d4c88bbb | 9440 | size_t len; |
c044fabd | 9441 | asection *sect; |
4a6636fb PA |
9442 | int type; |
9443 | int is_active_thread; | |
9444 | bfd_vma base_addr; | |
16e9c715 | 9445 | |
4a6636fb | 9446 | if (note->descsz < 728) |
b34976b6 | 9447 | return TRUE; |
16e9c715 | 9448 | |
4a6636fb PA |
9449 | if (! CONST_STRNEQ (note->namedata, "win32")) |
9450 | return TRUE; | |
9451 | ||
9452 | type = bfd_get_32 (abfd, note->descdata); | |
c044fabd | 9453 | |
4a6636fb | 9454 | switch (type) |
16e9c715 | 9455 | { |
4a6636fb | 9456 | case 1 /* NOTE_INFO_PROCESS */: |
228e534f | 9457 | /* FIXME: need to add ->core->command. */ |
4a6636fb | 9458 | /* process_info.pid */ |
228e534f | 9459 | elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, note->descdata + 8); |
4a6636fb | 9460 | /* process_info.signal */ |
228e534f | 9461 | elf_tdata (abfd)->core->signal = bfd_get_32 (abfd, note->descdata + 12); |
c044fabd | 9462 | break; |
16e9c715 | 9463 | |
4a6636fb | 9464 | case 2 /* NOTE_INFO_THREAD */: |
16e9c715 | 9465 | /* Make a ".reg/999" section. */ |
4a6636fb PA |
9466 | /* thread_info.tid */ |
9467 | sprintf (buf, ".reg/%ld", (long) bfd_get_32 (abfd, note->descdata + 8)); | |
c044fabd | 9468 | |
d4c88bbb | 9469 | len = strlen (buf) + 1; |
a50b1753 | 9470 | name = (char *) bfd_alloc (abfd, len); |
16e9c715 | 9471 | if (name == NULL) |
b34976b6 | 9472 | return FALSE; |
c044fabd | 9473 | |
d4c88bbb | 9474 | memcpy (name, buf, len); |
16e9c715 | 9475 | |
117ed4f8 | 9476 | sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); |
16e9c715 | 9477 | if (sect == NULL) |
b34976b6 | 9478 | return FALSE; |
c044fabd | 9479 | |
4a6636fb PA |
9480 | /* sizeof (thread_info.thread_context) */ |
9481 | sect->size = 716; | |
9482 | /* offsetof (thread_info.thread_context) */ | |
9483 | sect->filepos = note->descpos + 12; | |
16e9c715 NC |
9484 | sect->alignment_power = 2; |
9485 | ||
4a6636fb PA |
9486 | /* thread_info.is_active_thread */ |
9487 | is_active_thread = bfd_get_32 (abfd, note->descdata + 8); | |
9488 | ||
9489 | if (is_active_thread) | |
16e9c715 | 9490 | if (! elfcore_maybe_make_sect (abfd, ".reg", sect)) |
b34976b6 | 9491 | return FALSE; |
16e9c715 NC |
9492 | break; |
9493 | ||
4a6636fb | 9494 | case 3 /* NOTE_INFO_MODULE */: |
16e9c715 | 9495 | /* Make a ".module/xxxxxxxx" section. */ |
4a6636fb PA |
9496 | /* module_info.base_address */ |
9497 | base_addr = bfd_get_32 (abfd, note->descdata + 4); | |
0af1713e | 9498 | sprintf (buf, ".module/%08lx", (unsigned long) base_addr); |
c044fabd | 9499 | |
d4c88bbb | 9500 | len = strlen (buf) + 1; |
a50b1753 | 9501 | name = (char *) bfd_alloc (abfd, len); |
16e9c715 | 9502 | if (name == NULL) |
b34976b6 | 9503 | return FALSE; |
c044fabd | 9504 | |
d4c88bbb | 9505 | memcpy (name, buf, len); |
252b5132 | 9506 | |
117ed4f8 | 9507 | sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); |
c044fabd | 9508 | |
16e9c715 | 9509 | if (sect == NULL) |
b34976b6 | 9510 | return FALSE; |
c044fabd | 9511 | |
eea6121a | 9512 | sect->size = note->descsz; |
16e9c715 | 9513 | sect->filepos = note->descpos; |
16e9c715 NC |
9514 | sect->alignment_power = 2; |
9515 | break; | |
9516 | ||
9517 | default: | |
b34976b6 | 9518 | return TRUE; |
16e9c715 NC |
9519 | } |
9520 | ||
b34976b6 | 9521 | return TRUE; |
16e9c715 | 9522 | } |
252b5132 | 9523 | |
b34976b6 | 9524 | static bfd_boolean |
217aa764 | 9525 | elfcore_grok_note (bfd *abfd, Elf_Internal_Note *note) |
252b5132 | 9526 | { |
9c5bfbb7 | 9527 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
bb0082d6 | 9528 | |
252b5132 RH |
9529 | switch (note->type) |
9530 | { | |
9531 | default: | |
b34976b6 | 9532 | return TRUE; |
252b5132 | 9533 | |
252b5132 | 9534 | case NT_PRSTATUS: |
bb0082d6 AM |
9535 | if (bed->elf_backend_grok_prstatus) |
9536 | if ((*bed->elf_backend_grok_prstatus) (abfd, note)) | |
b34976b6 | 9537 | return TRUE; |
bb0082d6 | 9538 | #if defined (HAVE_PRSTATUS_T) |
252b5132 | 9539 | return elfcore_grok_prstatus (abfd, note); |
bb0082d6 | 9540 | #else |
b34976b6 | 9541 | return TRUE; |
252b5132 RH |
9542 | #endif |
9543 | ||
9544 | #if defined (HAVE_PSTATUS_T) | |
9545 | case NT_PSTATUS: | |
9546 | return elfcore_grok_pstatus (abfd, note); | |
9547 | #endif | |
9548 | ||
9549 | #if defined (HAVE_LWPSTATUS_T) | |
9550 | case NT_LWPSTATUS: | |
9551 | return elfcore_grok_lwpstatus (abfd, note); | |
9552 | #endif | |
9553 | ||
9554 | case NT_FPREGSET: /* FIXME: rename to NT_PRFPREG */ | |
9555 | return elfcore_grok_prfpreg (abfd, note); | |
9556 | ||
c044fabd | 9557 | case NT_WIN32PSTATUS: |
16e9c715 | 9558 | return elfcore_grok_win32pstatus (abfd, note); |
16e9c715 | 9559 | |
c044fabd | 9560 | case NT_PRXFPREG: /* Linux SSE extension */ |
e377ab71 MK |
9561 | if (note->namesz == 6 |
9562 | && strcmp (note->namedata, "LINUX") == 0) | |
ff08c6bb JB |
9563 | return elfcore_grok_prxfpreg (abfd, note); |
9564 | else | |
b34976b6 | 9565 | return TRUE; |
ff08c6bb | 9566 | |
4339cae0 L |
9567 | case NT_X86_XSTATE: /* Linux XSAVE extension */ |
9568 | if (note->namesz == 6 | |
9569 | && strcmp (note->namedata, "LINUX") == 0) | |
9570 | return elfcore_grok_xstatereg (abfd, note); | |
9571 | else | |
9572 | return TRUE; | |
9573 | ||
97753bd5 AM |
9574 | case NT_PPC_VMX: |
9575 | if (note->namesz == 6 | |
9576 | && strcmp (note->namedata, "LINUX") == 0) | |
9577 | return elfcore_grok_ppc_vmx (abfd, note); | |
9578 | else | |
9579 | return TRUE; | |
9580 | ||
89eeb0bc LM |
9581 | case NT_PPC_VSX: |
9582 | if (note->namesz == 6 | |
9583 | && strcmp (note->namedata, "LINUX") == 0) | |
9584 | return elfcore_grok_ppc_vsx (abfd, note); | |
9585 | else | |
9586 | return TRUE; | |
9587 | ||
0675e188 UW |
9588 | case NT_S390_HIGH_GPRS: |
9589 | if (note->namesz == 6 | |
9590 | && strcmp (note->namedata, "LINUX") == 0) | |
9591 | return elfcore_grok_s390_high_gprs (abfd, note); | |
9592 | else | |
9593 | return TRUE; | |
9594 | ||
d7eeb400 MS |
9595 | case NT_S390_TIMER: |
9596 | if (note->namesz == 6 | |
9597 | && strcmp (note->namedata, "LINUX") == 0) | |
9598 | return elfcore_grok_s390_timer (abfd, note); | |
9599 | else | |
9600 | return TRUE; | |
9601 | ||
9602 | case NT_S390_TODCMP: | |
9603 | if (note->namesz == 6 | |
9604 | && strcmp (note->namedata, "LINUX") == 0) | |
9605 | return elfcore_grok_s390_todcmp (abfd, note); | |
9606 | else | |
9607 | return TRUE; | |
9608 | ||
9609 | case NT_S390_TODPREG: | |
9610 | if (note->namesz == 6 | |
9611 | && strcmp (note->namedata, "LINUX") == 0) | |
9612 | return elfcore_grok_s390_todpreg (abfd, note); | |
9613 | else | |
9614 | return TRUE; | |
9615 | ||
9616 | case NT_S390_CTRS: | |
9617 | if (note->namesz == 6 | |
9618 | && strcmp (note->namedata, "LINUX") == 0) | |
9619 | return elfcore_grok_s390_ctrs (abfd, note); | |
9620 | else | |
9621 | return TRUE; | |
9622 | ||
9623 | case NT_S390_PREFIX: | |
9624 | if (note->namesz == 6 | |
9625 | && strcmp (note->namedata, "LINUX") == 0) | |
9626 | return elfcore_grok_s390_prefix (abfd, note); | |
9627 | else | |
9628 | return TRUE; | |
9629 | ||
355b81d9 UW |
9630 | case NT_S390_LAST_BREAK: |
9631 | if (note->namesz == 6 | |
9632 | && strcmp (note->namedata, "LINUX") == 0) | |
9633 | return elfcore_grok_s390_last_break (abfd, note); | |
9634 | else | |
9635 | return TRUE; | |
9636 | ||
9637 | case NT_S390_SYSTEM_CALL: | |
9638 | if (note->namesz == 6 | |
9639 | && strcmp (note->namedata, "LINUX") == 0) | |
9640 | return elfcore_grok_s390_system_call (abfd, note); | |
9641 | else | |
9642 | return TRUE; | |
9643 | ||
abb3f6cc NC |
9644 | case NT_S390_TDB: |
9645 | if (note->namesz == 6 | |
9646 | && strcmp (note->namedata, "LINUX") == 0) | |
9647 | return elfcore_grok_s390_tdb (abfd, note); | |
9648 | else | |
9649 | return TRUE; | |
9650 | ||
4ef9f41a AA |
9651 | case NT_S390_VXRS_LOW: |
9652 | if (note->namesz == 6 | |
9653 | && strcmp (note->namedata, "LINUX") == 0) | |
9654 | return elfcore_grok_s390_vxrs_low (abfd, note); | |
9655 | else | |
9656 | return TRUE; | |
9657 | ||
9658 | case NT_S390_VXRS_HIGH: | |
9659 | if (note->namesz == 6 | |
9660 | && strcmp (note->namedata, "LINUX") == 0) | |
9661 | return elfcore_grok_s390_vxrs_high (abfd, note); | |
9662 | else | |
9663 | return TRUE; | |
9664 | ||
faa9a424 UW |
9665 | case NT_ARM_VFP: |
9666 | if (note->namesz == 6 | |
9667 | && strcmp (note->namedata, "LINUX") == 0) | |
9668 | return elfcore_grok_arm_vfp (abfd, note); | |
9669 | else | |
9670 | return TRUE; | |
9671 | ||
652451f8 YZ |
9672 | case NT_ARM_TLS: |
9673 | if (note->namesz == 6 | |
9674 | && strcmp (note->namedata, "LINUX") == 0) | |
9675 | return elfcore_grok_aarch_tls (abfd, note); | |
9676 | else | |
9677 | return TRUE; | |
9678 | ||
9679 | case NT_ARM_HW_BREAK: | |
9680 | if (note->namesz == 6 | |
9681 | && strcmp (note->namedata, "LINUX") == 0) | |
9682 | return elfcore_grok_aarch_hw_break (abfd, note); | |
9683 | else | |
9684 | return TRUE; | |
9685 | ||
9686 | case NT_ARM_HW_WATCH: | |
9687 | if (note->namesz == 6 | |
9688 | && strcmp (note->namedata, "LINUX") == 0) | |
9689 | return elfcore_grok_aarch_hw_watch (abfd, note); | |
9690 | else | |
9691 | return TRUE; | |
9692 | ||
252b5132 RH |
9693 | case NT_PRPSINFO: |
9694 | case NT_PSINFO: | |
bb0082d6 AM |
9695 | if (bed->elf_backend_grok_psinfo) |
9696 | if ((*bed->elf_backend_grok_psinfo) (abfd, note)) | |
b34976b6 | 9697 | return TRUE; |
bb0082d6 | 9698 | #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) |
252b5132 | 9699 | return elfcore_grok_psinfo (abfd, note); |
bb0082d6 | 9700 | #else |
b34976b6 | 9701 | return TRUE; |
252b5132 | 9702 | #endif |
3333a7c3 RM |
9703 | |
9704 | case NT_AUXV: | |
9705 | { | |
117ed4f8 AM |
9706 | asection *sect = bfd_make_section_anyway_with_flags (abfd, ".auxv", |
9707 | SEC_HAS_CONTENTS); | |
3333a7c3 RM |
9708 | |
9709 | if (sect == NULL) | |
9710 | return FALSE; | |
eea6121a | 9711 | sect->size = note->descsz; |
3333a7c3 | 9712 | sect->filepos = note->descpos; |
3333a7c3 RM |
9713 | sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32; |
9714 | ||
9715 | return TRUE; | |
9716 | } | |
9015683b | 9717 | |
451b7c33 TT |
9718 | case NT_FILE: |
9719 | return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.file", | |
9720 | note); | |
9721 | ||
9015683b TT |
9722 | case NT_SIGINFO: |
9723 | return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.siginfo", | |
9724 | note); | |
5b2c414d | 9725 | |
252b5132 RH |
9726 | } |
9727 | } | |
9728 | ||
718175fa JK |
9729 | static bfd_boolean |
9730 | elfobj_grok_gnu_build_id (bfd *abfd, Elf_Internal_Note *note) | |
9731 | { | |
c74f7d1c | 9732 | struct bfd_build_id* build_id; |
30e8ee25 AM |
9733 | |
9734 | if (note->descsz == 0) | |
9735 | return FALSE; | |
9736 | ||
c74f7d1c JT |
9737 | build_id = bfd_alloc (abfd, sizeof (struct bfd_build_id) - 1 + note->descsz); |
9738 | if (build_id == NULL) | |
718175fa JK |
9739 | return FALSE; |
9740 | ||
c74f7d1c JT |
9741 | build_id->size = note->descsz; |
9742 | memcpy (build_id->data, note->descdata, note->descsz); | |
9743 | abfd->build_id = build_id; | |
718175fa JK |
9744 | |
9745 | return TRUE; | |
9746 | } | |
9747 | ||
9748 | static bfd_boolean | |
9749 | elfobj_grok_gnu_note (bfd *abfd, Elf_Internal_Note *note) | |
9750 | { | |
9751 | switch (note->type) | |
9752 | { | |
9753 | default: | |
9754 | return TRUE; | |
9755 | ||
46bed679 L |
9756 | case NT_GNU_PROPERTY_TYPE_0: |
9757 | return _bfd_elf_parse_gnu_properties (abfd, note); | |
9758 | ||
718175fa JK |
9759 | case NT_GNU_BUILD_ID: |
9760 | return elfobj_grok_gnu_build_id (abfd, note); | |
9761 | } | |
9762 | } | |
9763 | ||
e21e5835 NC |
9764 | static bfd_boolean |
9765 | elfobj_grok_stapsdt_note_1 (bfd *abfd, Elf_Internal_Note *note) | |
9766 | { | |
9767 | struct sdt_note *cur = | |
9768 | (struct sdt_note *) bfd_alloc (abfd, sizeof (struct sdt_note) | |
9769 | + note->descsz); | |
9770 | ||
9771 | cur->next = (struct sdt_note *) (elf_tdata (abfd))->sdt_note_head; | |
9772 | cur->size = (bfd_size_type) note->descsz; | |
9773 | memcpy (cur->data, note->descdata, note->descsz); | |
9774 | ||
9775 | elf_tdata (abfd)->sdt_note_head = cur; | |
9776 | ||
9777 | return TRUE; | |
9778 | } | |
9779 | ||
9780 | static bfd_boolean | |
9781 | elfobj_grok_stapsdt_note (bfd *abfd, Elf_Internal_Note *note) | |
9782 | { | |
9783 | switch (note->type) | |
9784 | { | |
9785 | case NT_STAPSDT: | |
9786 | return elfobj_grok_stapsdt_note_1 (abfd, note); | |
9787 | ||
9788 | default: | |
9789 | return TRUE; | |
9790 | } | |
9791 | } | |
9792 | ||
aa1ed4a9 JB |
9793 | static bfd_boolean |
9794 | elfcore_grok_freebsd_psinfo (bfd *abfd, Elf_Internal_Note *note) | |
9795 | { | |
9796 | size_t offset; | |
9797 | ||
aa1ed4a9 JB |
9798 | switch (abfd->arch_info->bits_per_word) |
9799 | { | |
9800 | case 32: | |
0064d223 JB |
9801 | if (note->descsz < 108) |
9802 | return FALSE; | |
aa1ed4a9 JB |
9803 | break; |
9804 | ||
9805 | case 64: | |
0064d223 JB |
9806 | if (note->descsz < 120) |
9807 | return FALSE; | |
aa1ed4a9 JB |
9808 | break; |
9809 | ||
9810 | default: | |
9811 | return FALSE; | |
9812 | } | |
9813 | ||
0064d223 JB |
9814 | /* Check for version 1 in pr_version. */ |
9815 | if (bfd_h_get_32 (abfd, (bfd_byte *) note->descdata) != 1) | |
9816 | return FALSE; | |
9817 | offset = 4; | |
9818 | ||
9819 | /* Skip over pr_psinfosz. */ | |
9820 | if (abfd->arch_info->bits_per_word == 32) | |
9821 | offset += 4; | |
9822 | else | |
9823 | { | |
9824 | offset += 4; /* Padding before pr_psinfosz. */ | |
9825 | offset += 8; | |
9826 | } | |
9827 | ||
aa1ed4a9 JB |
9828 | /* pr_fname is PRFNAMESZ (16) + 1 bytes in size. */ |
9829 | elf_tdata (abfd)->core->program | |
9830 | = _bfd_elfcore_strndup (abfd, note->descdata + offset, 17); | |
9831 | offset += 17; | |
9832 | ||
9833 | /* pr_psargs is PRARGSZ (80) + 1 bytes in size. */ | |
9834 | elf_tdata (abfd)->core->command | |
9835 | = _bfd_elfcore_strndup (abfd, note->descdata + offset, 81); | |
0064d223 JB |
9836 | offset += 81; |
9837 | ||
9838 | /* Padding before pr_pid. */ | |
9839 | offset += 2; | |
9840 | ||
9841 | /* The pr_pid field was added in version "1a". */ | |
9842 | if (note->descsz < offset + 4) | |
9843 | return TRUE; | |
9844 | ||
9845 | elf_tdata (abfd)->core->pid | |
9846 | = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset); | |
aa1ed4a9 JB |
9847 | |
9848 | return TRUE; | |
9849 | } | |
9850 | ||
9851 | static bfd_boolean | |
9852 | elfcore_grok_freebsd_prstatus (bfd *abfd, Elf_Internal_Note *note) | |
9853 | { | |
9854 | size_t offset; | |
9855 | size_t size; | |
9856 | ||
9857 | /* Check for version 1 in pr_version. */ | |
9858 | if (bfd_h_get_32 (abfd, (bfd_byte *) note->descdata) != 1) | |
9859 | return FALSE; | |
9860 | offset = 4; | |
9861 | ||
9862 | /* Skip over pr_statussz. */ | |
9863 | switch (abfd->arch_info->bits_per_word) | |
9864 | { | |
9865 | case 32: | |
9866 | offset += 4; | |
9867 | break; | |
9868 | ||
9869 | case 64: | |
9870 | offset += 4; /* Padding before pr_statussz. */ | |
9871 | offset += 8; | |
9872 | break; | |
9873 | ||
9874 | default: | |
9875 | return FALSE; | |
9876 | } | |
9877 | ||
9878 | /* Extract size of pr_reg from pr_gregsetsz. */ | |
9879 | if (abfd->arch_info->bits_per_word == 32) | |
9880 | size = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset); | |
9881 | else | |
9882 | size = bfd_h_get_64 (abfd, (bfd_byte *) note->descdata + offset); | |
9883 | ||
9884 | /* Skip over pr_gregsetsz and pr_fpregsetsz. */ | |
9885 | offset += (abfd->arch_info->bits_per_word / 8) * 2; | |
9886 | ||
9887 | /* Skip over pr_osreldate. */ | |
9888 | offset += 4; | |
9889 | ||
9890 | /* Read signal from pr_cursig. */ | |
9891 | if (elf_tdata (abfd)->core->signal == 0) | |
9892 | elf_tdata (abfd)->core->signal | |
9893 | = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset); | |
9894 | offset += 4; | |
9895 | ||
9896 | /* Read TID from pr_pid. */ | |
9897 | elf_tdata (abfd)->core->lwpid | |
9898 | = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset); | |
9899 | offset += 4; | |
9900 | ||
9901 | /* Padding before pr_reg. */ | |
9902 | if (abfd->arch_info->bits_per_word == 64) | |
9903 | offset += 4; | |
9904 | ||
9905 | /* Make a ".reg/999" section and a ".reg" section. */ | |
9906 | return _bfd_elfcore_make_pseudosection (abfd, ".reg", | |
9907 | size, note->descpos + offset); | |
9908 | } | |
9909 | ||
9910 | static bfd_boolean | |
9911 | elfcore_grok_freebsd_note (bfd *abfd, Elf_Internal_Note *note) | |
9912 | { | |
9913 | switch (note->type) | |
9914 | { | |
9915 | case NT_PRSTATUS: | |
9916 | return elfcore_grok_freebsd_prstatus (abfd, note); | |
9917 | ||
9918 | case NT_FPREGSET: | |
9919 | return elfcore_grok_prfpreg (abfd, note); | |
9920 | ||
9921 | case NT_PRPSINFO: | |
9922 | return elfcore_grok_freebsd_psinfo (abfd, note); | |
9923 | ||
9924 | case NT_FREEBSD_THRMISC: | |
9925 | if (note->namesz == 8) | |
9926 | return elfcore_make_note_pseudosection (abfd, ".thrmisc", note); | |
9927 | else | |
9928 | return TRUE; | |
9929 | ||
3350c5f5 JB |
9930 | case NT_FREEBSD_PROCSTAT_AUXV: |
9931 | { | |
9932 | asection *sect = bfd_make_section_anyway_with_flags (abfd, ".auxv", | |
9933 | SEC_HAS_CONTENTS); | |
9934 | ||
9935 | if (sect == NULL) | |
9936 | return FALSE; | |
9937 | sect->size = note->descsz - 4; | |
9938 | sect->filepos = note->descpos + 4; | |
9939 | sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32; | |
9940 | ||
9941 | return TRUE; | |
9942 | } | |
9943 | ||
aa1ed4a9 JB |
9944 | case NT_X86_XSTATE: |
9945 | if (note->namesz == 8) | |
9946 | return elfcore_grok_xstatereg (abfd, note); | |
9947 | else | |
9948 | return TRUE; | |
9949 | ||
9950 | default: | |
9951 | return TRUE; | |
9952 | } | |
9953 | } | |
9954 | ||
b34976b6 | 9955 | static bfd_boolean |
217aa764 | 9956 | elfcore_netbsd_get_lwpid (Elf_Internal_Note *note, int *lwpidp) |
50b2bdb7 AM |
9957 | { |
9958 | char *cp; | |
9959 | ||
9960 | cp = strchr (note->namedata, '@'); | |
9961 | if (cp != NULL) | |
9962 | { | |
d2b64500 | 9963 | *lwpidp = atoi(cp + 1); |
b34976b6 | 9964 | return TRUE; |
50b2bdb7 | 9965 | } |
b34976b6 | 9966 | return FALSE; |
50b2bdb7 AM |
9967 | } |
9968 | ||
b34976b6 | 9969 | static bfd_boolean |
217aa764 | 9970 | elfcore_grok_netbsd_procinfo (bfd *abfd, Elf_Internal_Note *note) |
50b2bdb7 | 9971 | { |
50b2bdb7 | 9972 | /* Signal number at offset 0x08. */ |
228e534f | 9973 | elf_tdata (abfd)->core->signal |
50b2bdb7 AM |
9974 | = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08); |
9975 | ||
9976 | /* Process ID at offset 0x50. */ | |
228e534f | 9977 | elf_tdata (abfd)->core->pid |
50b2bdb7 AM |
9978 | = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x50); |
9979 | ||
9980 | /* Command name at 0x7c (max 32 bytes, including nul). */ | |
228e534f | 9981 | elf_tdata (abfd)->core->command |
50b2bdb7 AM |
9982 | = _bfd_elfcore_strndup (abfd, note->descdata + 0x7c, 31); |
9983 | ||
7720ba9f MK |
9984 | return elfcore_make_note_pseudosection (abfd, ".note.netbsdcore.procinfo", |
9985 | note); | |
50b2bdb7 AM |
9986 | } |
9987 | ||
b34976b6 | 9988 | static bfd_boolean |
217aa764 | 9989 | elfcore_grok_netbsd_note (bfd *abfd, Elf_Internal_Note *note) |
50b2bdb7 AM |
9990 | { |
9991 | int lwp; | |
9992 | ||
9993 | if (elfcore_netbsd_get_lwpid (note, &lwp)) | |
228e534f | 9994 | elf_tdata (abfd)->core->lwpid = lwp; |
50b2bdb7 | 9995 | |
b4db1224 | 9996 | if (note->type == NT_NETBSDCORE_PROCINFO) |
50b2bdb7 AM |
9997 | { |
9998 | /* NetBSD-specific core "procinfo". Note that we expect to | |
08a40648 AM |
9999 | find this note before any of the others, which is fine, |
10000 | since the kernel writes this note out first when it | |
10001 | creates a core file. */ | |
47d9a591 | 10002 | |
50b2bdb7 AM |
10003 | return elfcore_grok_netbsd_procinfo (abfd, note); |
10004 | } | |
10005 | ||
b4db1224 JT |
10006 | /* As of Jan 2002 there are no other machine-independent notes |
10007 | defined for NetBSD core files. If the note type is less | |
10008 | than the start of the machine-dependent note types, we don't | |
10009 | understand it. */ | |
47d9a591 | 10010 | |
b4db1224 | 10011 | if (note->type < NT_NETBSDCORE_FIRSTMACH) |
b34976b6 | 10012 | return TRUE; |
50b2bdb7 AM |
10013 | |
10014 | ||
10015 | switch (bfd_get_arch (abfd)) | |
10016 | { | |
08a40648 AM |
10017 | /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and |
10018 | PT_GETFPREGS == mach+2. */ | |
50b2bdb7 AM |
10019 | |
10020 | case bfd_arch_alpha: | |
10021 | case bfd_arch_sparc: | |
10022 | switch (note->type) | |
08a40648 AM |
10023 | { |
10024 | case NT_NETBSDCORE_FIRSTMACH+0: | |
10025 | return elfcore_make_note_pseudosection (abfd, ".reg", note); | |
50b2bdb7 | 10026 | |
08a40648 AM |
10027 | case NT_NETBSDCORE_FIRSTMACH+2: |
10028 | return elfcore_make_note_pseudosection (abfd, ".reg2", note); | |
50b2bdb7 | 10029 | |
08a40648 AM |
10030 | default: |
10031 | return TRUE; | |
10032 | } | |
50b2bdb7 | 10033 | |
08a40648 AM |
10034 | /* On all other arch's, PT_GETREGS == mach+1 and |
10035 | PT_GETFPREGS == mach+3. */ | |
50b2bdb7 AM |
10036 | |
10037 | default: | |
10038 | switch (note->type) | |
08a40648 AM |
10039 | { |
10040 | case NT_NETBSDCORE_FIRSTMACH+1: | |
10041 | return elfcore_make_note_pseudosection (abfd, ".reg", note); | |
50b2bdb7 | 10042 | |
08a40648 AM |
10043 | case NT_NETBSDCORE_FIRSTMACH+3: |
10044 | return elfcore_make_note_pseudosection (abfd, ".reg2", note); | |
50b2bdb7 | 10045 | |
08a40648 AM |
10046 | default: |
10047 | return TRUE; | |
10048 | } | |
50b2bdb7 AM |
10049 | } |
10050 | /* NOTREACHED */ | |
10051 | } | |
10052 | ||
67cc5033 MK |
10053 | static bfd_boolean |
10054 | elfcore_grok_openbsd_procinfo (bfd *abfd, Elf_Internal_Note *note) | |
10055 | { | |
10056 | /* Signal number at offset 0x08. */ | |
228e534f | 10057 | elf_tdata (abfd)->core->signal |
67cc5033 MK |
10058 | = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08); |
10059 | ||
10060 | /* Process ID at offset 0x20. */ | |
228e534f | 10061 | elf_tdata (abfd)->core->pid |
67cc5033 MK |
10062 | = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x20); |
10063 | ||
10064 | /* Command name at 0x48 (max 32 bytes, including nul). */ | |
228e534f | 10065 | elf_tdata (abfd)->core->command |
67cc5033 MK |
10066 | = _bfd_elfcore_strndup (abfd, note->descdata + 0x48, 31); |
10067 | ||
10068 | return TRUE; | |
10069 | } | |
10070 | ||
10071 | static bfd_boolean | |
10072 | elfcore_grok_openbsd_note (bfd *abfd, Elf_Internal_Note *note) | |
10073 | { | |
10074 | if (note->type == NT_OPENBSD_PROCINFO) | |
10075 | return elfcore_grok_openbsd_procinfo (abfd, note); | |
10076 | ||
10077 | if (note->type == NT_OPENBSD_REGS) | |
10078 | return elfcore_make_note_pseudosection (abfd, ".reg", note); | |
10079 | ||
10080 | if (note->type == NT_OPENBSD_FPREGS) | |
10081 | return elfcore_make_note_pseudosection (abfd, ".reg2", note); | |
10082 | ||
10083 | if (note->type == NT_OPENBSD_XFPREGS) | |
10084 | return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note); | |
10085 | ||
10086 | if (note->type == NT_OPENBSD_AUXV) | |
10087 | { | |
10088 | asection *sect = bfd_make_section_anyway_with_flags (abfd, ".auxv", | |
10089 | SEC_HAS_CONTENTS); | |
10090 | ||
10091 | if (sect == NULL) | |
10092 | return FALSE; | |
10093 | sect->size = note->descsz; | |
10094 | sect->filepos = note->descpos; | |
10095 | sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32; | |
10096 | ||
10097 | return TRUE; | |
10098 | } | |
10099 | ||
10100 | if (note->type == NT_OPENBSD_WCOOKIE) | |
10101 | { | |
10102 | asection *sect = bfd_make_section_anyway_with_flags (abfd, ".wcookie", | |
10103 | SEC_HAS_CONTENTS); | |
10104 | ||
10105 | if (sect == NULL) | |
10106 | return FALSE; | |
10107 | sect->size = note->descsz; | |
10108 | sect->filepos = note->descpos; | |
10109 | sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32; | |
10110 | ||
10111 | return TRUE; | |
10112 | } | |
10113 | ||
10114 | return TRUE; | |
10115 | } | |
10116 | ||
07c6e936 | 10117 | static bfd_boolean |
d3fd4074 | 10118 | elfcore_grok_nto_status (bfd *abfd, Elf_Internal_Note *note, long *tid) |
07c6e936 NC |
10119 | { |
10120 | void *ddata = note->descdata; | |
10121 | char buf[100]; | |
10122 | char *name; | |
10123 | asection *sect; | |
f8843e87 AM |
10124 | short sig; |
10125 | unsigned flags; | |
07c6e936 NC |
10126 | |
10127 | /* nto_procfs_status 'pid' field is at offset 0. */ | |
228e534f | 10128 | elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, (bfd_byte *) ddata); |
07c6e936 | 10129 | |
f8843e87 AM |
10130 | /* nto_procfs_status 'tid' field is at offset 4. Pass it back. */ |
10131 | *tid = bfd_get_32 (abfd, (bfd_byte *) ddata + 4); | |
10132 | ||
10133 | /* nto_procfs_status 'flags' field is at offset 8. */ | |
10134 | flags = bfd_get_32 (abfd, (bfd_byte *) ddata + 8); | |
07c6e936 NC |
10135 | |
10136 | /* nto_procfs_status 'what' field is at offset 14. */ | |
f8843e87 AM |
10137 | if ((sig = bfd_get_16 (abfd, (bfd_byte *) ddata + 14)) > 0) |
10138 | { | |
228e534f AM |
10139 | elf_tdata (abfd)->core->signal = sig; |
10140 | elf_tdata (abfd)->core->lwpid = *tid; | |
f8843e87 | 10141 | } |
07c6e936 | 10142 | |
f8843e87 AM |
10143 | /* _DEBUG_FLAG_CURTID (current thread) is 0x80. Some cores |
10144 | do not come from signals so we make sure we set the current | |
10145 | thread just in case. */ | |
10146 | if (flags & 0x00000080) | |
228e534f | 10147 | elf_tdata (abfd)->core->lwpid = *tid; |
07c6e936 NC |
10148 | |
10149 | /* Make a ".qnx_core_status/%d" section. */ | |
d3fd4074 | 10150 | sprintf (buf, ".qnx_core_status/%ld", *tid); |
07c6e936 | 10151 | |
a50b1753 | 10152 | name = (char *) bfd_alloc (abfd, strlen (buf) + 1); |
07c6e936 NC |
10153 | if (name == NULL) |
10154 | return FALSE; | |
10155 | strcpy (name, buf); | |
10156 | ||
117ed4f8 | 10157 | sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); |
07c6e936 NC |
10158 | if (sect == NULL) |
10159 | return FALSE; | |
10160 | ||
eea6121a | 10161 | sect->size = note->descsz; |
07c6e936 | 10162 | sect->filepos = note->descpos; |
07c6e936 NC |
10163 | sect->alignment_power = 2; |
10164 | ||
10165 | return (elfcore_maybe_make_sect (abfd, ".qnx_core_status", sect)); | |
10166 | } | |
10167 | ||
10168 | static bfd_boolean | |
d69f560c KW |
10169 | elfcore_grok_nto_regs (bfd *abfd, |
10170 | Elf_Internal_Note *note, | |
d3fd4074 | 10171 | long tid, |
d69f560c | 10172 | char *base) |
07c6e936 NC |
10173 | { |
10174 | char buf[100]; | |
10175 | char *name; | |
10176 | asection *sect; | |
10177 | ||
d69f560c | 10178 | /* Make a "(base)/%d" section. */ |
d3fd4074 | 10179 | sprintf (buf, "%s/%ld", base, tid); |
07c6e936 | 10180 | |
a50b1753 | 10181 | name = (char *) bfd_alloc (abfd, strlen (buf) + 1); |
07c6e936 NC |
10182 | if (name == NULL) |
10183 | return FALSE; | |
10184 | strcpy (name, buf); | |
10185 | ||
117ed4f8 | 10186 | sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); |
07c6e936 NC |
10187 | if (sect == NULL) |
10188 | return FALSE; | |
10189 | ||
eea6121a | 10190 | sect->size = note->descsz; |
07c6e936 | 10191 | sect->filepos = note->descpos; |
07c6e936 NC |
10192 | sect->alignment_power = 2; |
10193 | ||
f8843e87 | 10194 | /* This is the current thread. */ |
228e534f | 10195 | if (elf_tdata (abfd)->core->lwpid == tid) |
d69f560c | 10196 | return elfcore_maybe_make_sect (abfd, base, sect); |
f8843e87 AM |
10197 | |
10198 | return TRUE; | |
07c6e936 NC |
10199 | } |
10200 | ||
10201 | #define BFD_QNT_CORE_INFO 7 | |
10202 | #define BFD_QNT_CORE_STATUS 8 | |
10203 | #define BFD_QNT_CORE_GREG 9 | |
10204 | #define BFD_QNT_CORE_FPREG 10 | |
10205 | ||
10206 | static bfd_boolean | |
217aa764 | 10207 | elfcore_grok_nto_note (bfd *abfd, Elf_Internal_Note *note) |
07c6e936 NC |
10208 | { |
10209 | /* Every GREG section has a STATUS section before it. Store the | |
811072d8 | 10210 | tid from the previous call to pass down to the next gregs |
07c6e936 | 10211 | function. */ |
d3fd4074 | 10212 | static long tid = 1; |
07c6e936 NC |
10213 | |
10214 | switch (note->type) | |
10215 | { | |
d69f560c KW |
10216 | case BFD_QNT_CORE_INFO: |
10217 | return elfcore_make_note_pseudosection (abfd, ".qnx_core_info", note); | |
10218 | case BFD_QNT_CORE_STATUS: | |
10219 | return elfcore_grok_nto_status (abfd, note, &tid); | |
10220 | case BFD_QNT_CORE_GREG: | |
10221 | return elfcore_grok_nto_regs (abfd, note, tid, ".reg"); | |
10222 | case BFD_QNT_CORE_FPREG: | |
10223 | return elfcore_grok_nto_regs (abfd, note, tid, ".reg2"); | |
10224 | default: | |
10225 | return TRUE; | |
07c6e936 NC |
10226 | } |
10227 | } | |
10228 | ||
b15fa79e AM |
10229 | static bfd_boolean |
10230 | elfcore_grok_spu_note (bfd *abfd, Elf_Internal_Note *note) | |
10231 | { | |
10232 | char *name; | |
10233 | asection *sect; | |
10234 | size_t len; | |
10235 | ||
10236 | /* Use note name as section name. */ | |
10237 | len = note->namesz; | |
a50b1753 | 10238 | name = (char *) bfd_alloc (abfd, len); |
b15fa79e AM |
10239 | if (name == NULL) |
10240 | return FALSE; | |
10241 | memcpy (name, note->namedata, len); | |
10242 | name[len - 1] = '\0'; | |
10243 | ||
10244 | sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); | |
10245 | if (sect == NULL) | |
10246 | return FALSE; | |
10247 | ||
10248 | sect->size = note->descsz; | |
10249 | sect->filepos = note->descpos; | |
10250 | sect->alignment_power = 1; | |
10251 | ||
10252 | return TRUE; | |
10253 | } | |
10254 | ||
7c76fa91 MS |
10255 | /* Function: elfcore_write_note |
10256 | ||
47d9a591 | 10257 | Inputs: |
a39f3346 | 10258 | buffer to hold note, and current size of buffer |
7c76fa91 MS |
10259 | name of note |
10260 | type of note | |
10261 | data for note | |
10262 | size of data for note | |
10263 | ||
a39f3346 AM |
10264 | Writes note to end of buffer. ELF64 notes are written exactly as |
10265 | for ELF32, despite the current (as of 2006) ELF gabi specifying | |
10266 | that they ought to have 8-byte namesz and descsz field, and have | |
10267 | 8-byte alignment. Other writers, eg. Linux kernel, do the same. | |
10268 | ||
7c76fa91 | 10269 | Return: |
a39f3346 | 10270 | Pointer to realloc'd buffer, *BUFSIZ updated. */ |
7c76fa91 MS |
10271 | |
10272 | char * | |
a39f3346 | 10273 | elfcore_write_note (bfd *abfd, |
217aa764 | 10274 | char *buf, |
a39f3346 | 10275 | int *bufsiz, |
217aa764 | 10276 | const char *name, |
a39f3346 | 10277 | int type, |
217aa764 | 10278 | const void *input, |
a39f3346 | 10279 | int size) |
7c76fa91 MS |
10280 | { |
10281 | Elf_External_Note *xnp; | |
d4c88bbb | 10282 | size_t namesz; |
d4c88bbb | 10283 | size_t newspace; |
a39f3346 | 10284 | char *dest; |
7c76fa91 | 10285 | |
d4c88bbb | 10286 | namesz = 0; |
d4c88bbb | 10287 | if (name != NULL) |
a39f3346 | 10288 | namesz = strlen (name) + 1; |
d4c88bbb | 10289 | |
a39f3346 | 10290 | newspace = 12 + ((namesz + 3) & -4) + ((size + 3) & -4); |
d4c88bbb | 10291 | |
a50b1753 | 10292 | buf = (char *) realloc (buf, *bufsiz + newspace); |
14b1c01e AM |
10293 | if (buf == NULL) |
10294 | return buf; | |
a39f3346 | 10295 | dest = buf + *bufsiz; |
7c76fa91 MS |
10296 | *bufsiz += newspace; |
10297 | xnp = (Elf_External_Note *) dest; | |
10298 | H_PUT_32 (abfd, namesz, xnp->namesz); | |
10299 | H_PUT_32 (abfd, size, xnp->descsz); | |
10300 | H_PUT_32 (abfd, type, xnp->type); | |
d4c88bbb AM |
10301 | dest = xnp->name; |
10302 | if (name != NULL) | |
10303 | { | |
10304 | memcpy (dest, name, namesz); | |
10305 | dest += namesz; | |
a39f3346 | 10306 | while (namesz & 3) |
d4c88bbb AM |
10307 | { |
10308 | *dest++ = '\0'; | |
a39f3346 | 10309 | ++namesz; |
d4c88bbb AM |
10310 | } |
10311 | } | |
10312 | memcpy (dest, input, size); | |
a39f3346 AM |
10313 | dest += size; |
10314 | while (size & 3) | |
10315 | { | |
10316 | *dest++ = '\0'; | |
10317 | ++size; | |
10318 | } | |
10319 | return buf; | |
7c76fa91 MS |
10320 | } |
10321 | ||
7c76fa91 | 10322 | char * |
217aa764 AM |
10323 | elfcore_write_prpsinfo (bfd *abfd, |
10324 | char *buf, | |
10325 | int *bufsiz, | |
10326 | const char *fname, | |
10327 | const char *psargs) | |
7c76fa91 | 10328 | { |
183e98be AM |
10329 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
10330 | ||
10331 | if (bed->elf_backend_write_core_note != NULL) | |
10332 | { | |
10333 | char *ret; | |
10334 | ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz, | |
10335 | NT_PRPSINFO, fname, psargs); | |
10336 | if (ret != NULL) | |
10337 | return ret; | |
10338 | } | |
7c76fa91 | 10339 | |
1f20dca5 | 10340 | #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) |
183e98be AM |
10341 | #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T) |
10342 | if (bed->s->elfclass == ELFCLASS32) | |
10343 | { | |
10344 | #if defined (HAVE_PSINFO32_T) | |
10345 | psinfo32_t data; | |
10346 | int note_type = NT_PSINFO; | |
10347 | #else | |
10348 | prpsinfo32_t data; | |
10349 | int note_type = NT_PRPSINFO; | |
10350 | #endif | |
10351 | ||
10352 | memset (&data, 0, sizeof (data)); | |
10353 | strncpy (data.pr_fname, fname, sizeof (data.pr_fname)); | |
10354 | strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs)); | |
10355 | return elfcore_write_note (abfd, buf, bufsiz, | |
1f20dca5 | 10356 | "CORE", note_type, &data, sizeof (data)); |
183e98be AM |
10357 | } |
10358 | else | |
10359 | #endif | |
10360 | { | |
7c76fa91 | 10361 | #if defined (HAVE_PSINFO_T) |
183e98be AM |
10362 | psinfo_t data; |
10363 | int note_type = NT_PSINFO; | |
7c76fa91 | 10364 | #else |
183e98be AM |
10365 | prpsinfo_t data; |
10366 | int note_type = NT_PRPSINFO; | |
7c76fa91 MS |
10367 | #endif |
10368 | ||
183e98be AM |
10369 | memset (&data, 0, sizeof (data)); |
10370 | strncpy (data.pr_fname, fname, sizeof (data.pr_fname)); | |
10371 | strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs)); | |
10372 | return elfcore_write_note (abfd, buf, bufsiz, | |
1f20dca5 | 10373 | "CORE", note_type, &data, sizeof (data)); |
183e98be | 10374 | } |
7c76fa91 MS |
10375 | #endif /* PSINFO_T or PRPSINFO_T */ |
10376 | ||
1f20dca5 UW |
10377 | free (buf); |
10378 | return NULL; | |
10379 | } | |
10380 | ||
70a38d42 SDJ |
10381 | char * |
10382 | elfcore_write_linux_prpsinfo32 | |
10383 | (bfd *abfd, char *buf, int *bufsiz, | |
10384 | const struct elf_internal_linux_prpsinfo *prpsinfo) | |
10385 | { | |
10386 | struct elf_external_linux_prpsinfo32 data; | |
10387 | ||
aeb70569 | 10388 | swap_linux_prpsinfo32_out (abfd, prpsinfo, &data); |
70a38d42 SDJ |
10389 | return elfcore_write_note (abfd, buf, bufsiz, "CORE", NT_PRPSINFO, |
10390 | &data, sizeof (data)); | |
10391 | } | |
10392 | ||
10393 | char * | |
10394 | elfcore_write_linux_prpsinfo64 | |
10395 | (bfd *abfd, char *buf, int *bufsiz, | |
10396 | const struct elf_internal_linux_prpsinfo *prpsinfo) | |
10397 | { | |
10398 | struct elf_external_linux_prpsinfo64 data; | |
10399 | ||
aeb70569 | 10400 | swap_linux_prpsinfo64_out (abfd, prpsinfo, &data); |
70a38d42 SDJ |
10401 | return elfcore_write_note (abfd, buf, bufsiz, |
10402 | "CORE", NT_PRPSINFO, &data, sizeof (data)); | |
10403 | } | |
10404 | ||
7c76fa91 | 10405 | char * |
217aa764 AM |
10406 | elfcore_write_prstatus (bfd *abfd, |
10407 | char *buf, | |
10408 | int *bufsiz, | |
10409 | long pid, | |
10410 | int cursig, | |
10411 | const void *gregs) | |
7c76fa91 | 10412 | { |
183e98be | 10413 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
7c76fa91 | 10414 | |
183e98be AM |
10415 | if (bed->elf_backend_write_core_note != NULL) |
10416 | { | |
10417 | char *ret; | |
10418 | ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz, | |
10419 | NT_PRSTATUS, | |
10420 | pid, cursig, gregs); | |
10421 | if (ret != NULL) | |
10422 | return ret; | |
10423 | } | |
10424 | ||
1f20dca5 | 10425 | #if defined (HAVE_PRSTATUS_T) |
183e98be AM |
10426 | #if defined (HAVE_PRSTATUS32_T) |
10427 | if (bed->s->elfclass == ELFCLASS32) | |
10428 | { | |
10429 | prstatus32_t prstat; | |
10430 | ||
10431 | memset (&prstat, 0, sizeof (prstat)); | |
10432 | prstat.pr_pid = pid; | |
10433 | prstat.pr_cursig = cursig; | |
10434 | memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg)); | |
1f20dca5 | 10435 | return elfcore_write_note (abfd, buf, bufsiz, "CORE", |
183e98be AM |
10436 | NT_PRSTATUS, &prstat, sizeof (prstat)); |
10437 | } | |
10438 | else | |
10439 | #endif | |
10440 | { | |
10441 | prstatus_t prstat; | |
10442 | ||
10443 | memset (&prstat, 0, sizeof (prstat)); | |
10444 | prstat.pr_pid = pid; | |
10445 | prstat.pr_cursig = cursig; | |
10446 | memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg)); | |
1f20dca5 | 10447 | return elfcore_write_note (abfd, buf, bufsiz, "CORE", |
183e98be AM |
10448 | NT_PRSTATUS, &prstat, sizeof (prstat)); |
10449 | } | |
7c76fa91 MS |
10450 | #endif /* HAVE_PRSTATUS_T */ |
10451 | ||
1f20dca5 UW |
10452 | free (buf); |
10453 | return NULL; | |
10454 | } | |
10455 | ||
51316059 MS |
10456 | #if defined (HAVE_LWPSTATUS_T) |
10457 | char * | |
217aa764 AM |
10458 | elfcore_write_lwpstatus (bfd *abfd, |
10459 | char *buf, | |
10460 | int *bufsiz, | |
10461 | long pid, | |
10462 | int cursig, | |
10463 | const void *gregs) | |
51316059 MS |
10464 | { |
10465 | lwpstatus_t lwpstat; | |
183e98be | 10466 | const char *note_name = "CORE"; |
51316059 MS |
10467 | |
10468 | memset (&lwpstat, 0, sizeof (lwpstat)); | |
10469 | lwpstat.pr_lwpid = pid >> 16; | |
10470 | lwpstat.pr_cursig = cursig; | |
10471 | #if defined (HAVE_LWPSTATUS_T_PR_REG) | |
d1e8523e | 10472 | memcpy (&lwpstat.pr_reg, gregs, sizeof (lwpstat.pr_reg)); |
51316059 MS |
10473 | #elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT) |
10474 | #if !defined(gregs) | |
10475 | memcpy (lwpstat.pr_context.uc_mcontext.gregs, | |
10476 | gregs, sizeof (lwpstat.pr_context.uc_mcontext.gregs)); | |
10477 | #else | |
10478 | memcpy (lwpstat.pr_context.uc_mcontext.__gregs, | |
10479 | gregs, sizeof (lwpstat.pr_context.uc_mcontext.__gregs)); | |
10480 | #endif | |
10481 | #endif | |
47d9a591 | 10482 | return elfcore_write_note (abfd, buf, bufsiz, note_name, |
51316059 MS |
10483 | NT_LWPSTATUS, &lwpstat, sizeof (lwpstat)); |
10484 | } | |
10485 | #endif /* HAVE_LWPSTATUS_T */ | |
10486 | ||
7c76fa91 MS |
10487 | #if defined (HAVE_PSTATUS_T) |
10488 | char * | |
217aa764 AM |
10489 | elfcore_write_pstatus (bfd *abfd, |
10490 | char *buf, | |
10491 | int *bufsiz, | |
10492 | long pid, | |
6c10990d NC |
10493 | int cursig ATTRIBUTE_UNUSED, |
10494 | const void *gregs ATTRIBUTE_UNUSED) | |
7c76fa91 | 10495 | { |
183e98be AM |
10496 | const char *note_name = "CORE"; |
10497 | #if defined (HAVE_PSTATUS32_T) | |
10498 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
7c76fa91 | 10499 | |
183e98be AM |
10500 | if (bed->s->elfclass == ELFCLASS32) |
10501 | { | |
10502 | pstatus32_t pstat; | |
10503 | ||
10504 | memset (&pstat, 0, sizeof (pstat)); | |
10505 | pstat.pr_pid = pid & 0xffff; | |
10506 | buf = elfcore_write_note (abfd, buf, bufsiz, note_name, | |
10507 | NT_PSTATUS, &pstat, sizeof (pstat)); | |
10508 | return buf; | |
10509 | } | |
10510 | else | |
10511 | #endif | |
10512 | { | |
10513 | pstatus_t pstat; | |
10514 | ||
10515 | memset (&pstat, 0, sizeof (pstat)); | |
10516 | pstat.pr_pid = pid & 0xffff; | |
10517 | buf = elfcore_write_note (abfd, buf, bufsiz, note_name, | |
10518 | NT_PSTATUS, &pstat, sizeof (pstat)); | |
10519 | return buf; | |
10520 | } | |
7c76fa91 MS |
10521 | } |
10522 | #endif /* HAVE_PSTATUS_T */ | |
10523 | ||
10524 | char * | |
217aa764 AM |
10525 | elfcore_write_prfpreg (bfd *abfd, |
10526 | char *buf, | |
10527 | int *bufsiz, | |
10528 | const void *fpregs, | |
10529 | int size) | |
7c76fa91 | 10530 | { |
183e98be | 10531 | const char *note_name = "CORE"; |
47d9a591 | 10532 | return elfcore_write_note (abfd, buf, bufsiz, |
7c76fa91 MS |
10533 | note_name, NT_FPREGSET, fpregs, size); |
10534 | } | |
10535 | ||
10536 | char * | |
217aa764 AM |
10537 | elfcore_write_prxfpreg (bfd *abfd, |
10538 | char *buf, | |
10539 | int *bufsiz, | |
10540 | const void *xfpregs, | |
10541 | int size) | |
7c76fa91 MS |
10542 | { |
10543 | char *note_name = "LINUX"; | |
47d9a591 | 10544 | return elfcore_write_note (abfd, buf, bufsiz, |
7c76fa91 MS |
10545 | note_name, NT_PRXFPREG, xfpregs, size); |
10546 | } | |
10547 | ||
4339cae0 L |
10548 | char * |
10549 | elfcore_write_xstatereg (bfd *abfd, char *buf, int *bufsiz, | |
10550 | const void *xfpregs, int size) | |
10551 | { | |
97de3545 JB |
10552 | char *note_name; |
10553 | if (get_elf_backend_data (abfd)->elf_osabi == ELFOSABI_FREEBSD) | |
10554 | note_name = "FreeBSD"; | |
10555 | else | |
10556 | note_name = "LINUX"; | |
4339cae0 L |
10557 | return elfcore_write_note (abfd, buf, bufsiz, |
10558 | note_name, NT_X86_XSTATE, xfpregs, size); | |
10559 | } | |
10560 | ||
97753bd5 AM |
10561 | char * |
10562 | elfcore_write_ppc_vmx (bfd *abfd, | |
10563 | char *buf, | |
10564 | int *bufsiz, | |
10565 | const void *ppc_vmx, | |
10566 | int size) | |
10567 | { | |
10568 | char *note_name = "LINUX"; | |
10569 | return elfcore_write_note (abfd, buf, bufsiz, | |
10570 | note_name, NT_PPC_VMX, ppc_vmx, size); | |
10571 | } | |
10572 | ||
89eeb0bc LM |
10573 | char * |
10574 | elfcore_write_ppc_vsx (bfd *abfd, | |
10575 | char *buf, | |
10576 | int *bufsiz, | |
10577 | const void *ppc_vsx, | |
10578 | int size) | |
10579 | { | |
10580 | char *note_name = "LINUX"; | |
10581 | return elfcore_write_note (abfd, buf, bufsiz, | |
10582 | note_name, NT_PPC_VSX, ppc_vsx, size); | |
10583 | } | |
10584 | ||
0675e188 UW |
10585 | static char * |
10586 | elfcore_write_s390_high_gprs (bfd *abfd, | |
10587 | char *buf, | |
10588 | int *bufsiz, | |
10589 | const void *s390_high_gprs, | |
10590 | int size) | |
10591 | { | |
10592 | char *note_name = "LINUX"; | |
10593 | return elfcore_write_note (abfd, buf, bufsiz, | |
10594 | note_name, NT_S390_HIGH_GPRS, | |
10595 | s390_high_gprs, size); | |
10596 | } | |
10597 | ||
d7eeb400 MS |
10598 | char * |
10599 | elfcore_write_s390_timer (bfd *abfd, | |
10600 | char *buf, | |
10601 | int *bufsiz, | |
10602 | const void *s390_timer, | |
10603 | int size) | |
10604 | { | |
10605 | char *note_name = "LINUX"; | |
10606 | return elfcore_write_note (abfd, buf, bufsiz, | |
10607 | note_name, NT_S390_TIMER, s390_timer, size); | |
10608 | } | |
10609 | ||
10610 | char * | |
10611 | elfcore_write_s390_todcmp (bfd *abfd, | |
10612 | char *buf, | |
10613 | int *bufsiz, | |
10614 | const void *s390_todcmp, | |
10615 | int size) | |
10616 | { | |
10617 | char *note_name = "LINUX"; | |
10618 | return elfcore_write_note (abfd, buf, bufsiz, | |
10619 | note_name, NT_S390_TODCMP, s390_todcmp, size); | |
10620 | } | |
10621 | ||
10622 | char * | |
10623 | elfcore_write_s390_todpreg (bfd *abfd, | |
10624 | char *buf, | |
10625 | int *bufsiz, | |
10626 | const void *s390_todpreg, | |
10627 | int size) | |
10628 | { | |
10629 | char *note_name = "LINUX"; | |
10630 | return elfcore_write_note (abfd, buf, bufsiz, | |
10631 | note_name, NT_S390_TODPREG, s390_todpreg, size); | |
10632 | } | |
10633 | ||
10634 | char * | |
10635 | elfcore_write_s390_ctrs (bfd *abfd, | |
10636 | char *buf, | |
10637 | int *bufsiz, | |
10638 | const void *s390_ctrs, | |
10639 | int size) | |
10640 | { | |
10641 | char *note_name = "LINUX"; | |
10642 | return elfcore_write_note (abfd, buf, bufsiz, | |
10643 | note_name, NT_S390_CTRS, s390_ctrs, size); | |
10644 | } | |
10645 | ||
10646 | char * | |
10647 | elfcore_write_s390_prefix (bfd *abfd, | |
10648 | char *buf, | |
10649 | int *bufsiz, | |
10650 | const void *s390_prefix, | |
10651 | int size) | |
10652 | { | |
10653 | char *note_name = "LINUX"; | |
10654 | return elfcore_write_note (abfd, buf, bufsiz, | |
10655 | note_name, NT_S390_PREFIX, s390_prefix, size); | |
10656 | } | |
10657 | ||
355b81d9 UW |
10658 | char * |
10659 | elfcore_write_s390_last_break (bfd *abfd, | |
10660 | char *buf, | |
10661 | int *bufsiz, | |
10662 | const void *s390_last_break, | |
10663 | int size) | |
10664 | { | |
10665 | char *note_name = "LINUX"; | |
10666 | return elfcore_write_note (abfd, buf, bufsiz, | |
10667 | note_name, NT_S390_LAST_BREAK, | |
10668 | s390_last_break, size); | |
10669 | } | |
10670 | ||
10671 | char * | |
10672 | elfcore_write_s390_system_call (bfd *abfd, | |
10673 | char *buf, | |
10674 | int *bufsiz, | |
10675 | const void *s390_system_call, | |
10676 | int size) | |
10677 | { | |
10678 | char *note_name = "LINUX"; | |
10679 | return elfcore_write_note (abfd, buf, bufsiz, | |
10680 | note_name, NT_S390_SYSTEM_CALL, | |
10681 | s390_system_call, size); | |
10682 | } | |
10683 | ||
abb3f6cc NC |
10684 | char * |
10685 | elfcore_write_s390_tdb (bfd *abfd, | |
10686 | char *buf, | |
10687 | int *bufsiz, | |
10688 | const void *s390_tdb, | |
10689 | int size) | |
10690 | { | |
10691 | char *note_name = "LINUX"; | |
10692 | return elfcore_write_note (abfd, buf, bufsiz, | |
10693 | note_name, NT_S390_TDB, s390_tdb, size); | |
10694 | } | |
10695 | ||
4ef9f41a AA |
10696 | char * |
10697 | elfcore_write_s390_vxrs_low (bfd *abfd, | |
10698 | char *buf, | |
10699 | int *bufsiz, | |
10700 | const void *s390_vxrs_low, | |
10701 | int size) | |
10702 | { | |
10703 | char *note_name = "LINUX"; | |
10704 | return elfcore_write_note (abfd, buf, bufsiz, | |
10705 | note_name, NT_S390_VXRS_LOW, s390_vxrs_low, size); | |
10706 | } | |
10707 | ||
10708 | char * | |
10709 | elfcore_write_s390_vxrs_high (bfd *abfd, | |
10710 | char *buf, | |
10711 | int *bufsiz, | |
10712 | const void *s390_vxrs_high, | |
10713 | int size) | |
10714 | { | |
10715 | char *note_name = "LINUX"; | |
10716 | return elfcore_write_note (abfd, buf, bufsiz, | |
10717 | note_name, NT_S390_VXRS_HIGH, | |
10718 | s390_vxrs_high, size); | |
10719 | } | |
10720 | ||
faa9a424 UW |
10721 | char * |
10722 | elfcore_write_arm_vfp (bfd *abfd, | |
10723 | char *buf, | |
10724 | int *bufsiz, | |
10725 | const void *arm_vfp, | |
10726 | int size) | |
10727 | { | |
10728 | char *note_name = "LINUX"; | |
10729 | return elfcore_write_note (abfd, buf, bufsiz, | |
10730 | note_name, NT_ARM_VFP, arm_vfp, size); | |
10731 | } | |
10732 | ||
652451f8 YZ |
10733 | char * |
10734 | elfcore_write_aarch_tls (bfd *abfd, | |
10735 | char *buf, | |
10736 | int *bufsiz, | |
10737 | const void *aarch_tls, | |
10738 | int size) | |
10739 | { | |
10740 | char *note_name = "LINUX"; | |
10741 | return elfcore_write_note (abfd, buf, bufsiz, | |
10742 | note_name, NT_ARM_TLS, aarch_tls, size); | |
10743 | } | |
10744 | ||
10745 | char * | |
10746 | elfcore_write_aarch_hw_break (bfd *abfd, | |
10747 | char *buf, | |
10748 | int *bufsiz, | |
10749 | const void *aarch_hw_break, | |
10750 | int size) | |
10751 | { | |
10752 | char *note_name = "LINUX"; | |
10753 | return elfcore_write_note (abfd, buf, bufsiz, | |
10754 | note_name, NT_ARM_HW_BREAK, aarch_hw_break, size); | |
10755 | } | |
10756 | ||
10757 | char * | |
10758 | elfcore_write_aarch_hw_watch (bfd *abfd, | |
10759 | char *buf, | |
10760 | int *bufsiz, | |
10761 | const void *aarch_hw_watch, | |
10762 | int size) | |
10763 | { | |
10764 | char *note_name = "LINUX"; | |
10765 | return elfcore_write_note (abfd, buf, bufsiz, | |
10766 | note_name, NT_ARM_HW_WATCH, aarch_hw_watch, size); | |
10767 | } | |
10768 | ||
bb864ac1 CES |
10769 | char * |
10770 | elfcore_write_register_note (bfd *abfd, | |
10771 | char *buf, | |
10772 | int *bufsiz, | |
10773 | const char *section, | |
10774 | const void *data, | |
10775 | int size) | |
10776 | { | |
10777 | if (strcmp (section, ".reg2") == 0) | |
10778 | return elfcore_write_prfpreg (abfd, buf, bufsiz, data, size); | |
10779 | if (strcmp (section, ".reg-xfp") == 0) | |
10780 | return elfcore_write_prxfpreg (abfd, buf, bufsiz, data, size); | |
4339cae0 L |
10781 | if (strcmp (section, ".reg-xstate") == 0) |
10782 | return elfcore_write_xstatereg (abfd, buf, bufsiz, data, size); | |
bb864ac1 CES |
10783 | if (strcmp (section, ".reg-ppc-vmx") == 0) |
10784 | return elfcore_write_ppc_vmx (abfd, buf, bufsiz, data, size); | |
89eeb0bc LM |
10785 | if (strcmp (section, ".reg-ppc-vsx") == 0) |
10786 | return elfcore_write_ppc_vsx (abfd, buf, bufsiz, data, size); | |
0675e188 UW |
10787 | if (strcmp (section, ".reg-s390-high-gprs") == 0) |
10788 | return elfcore_write_s390_high_gprs (abfd, buf, bufsiz, data, size); | |
d7eeb400 MS |
10789 | if (strcmp (section, ".reg-s390-timer") == 0) |
10790 | return elfcore_write_s390_timer (abfd, buf, bufsiz, data, size); | |
10791 | if (strcmp (section, ".reg-s390-todcmp") == 0) | |
10792 | return elfcore_write_s390_todcmp (abfd, buf, bufsiz, data, size); | |
10793 | if (strcmp (section, ".reg-s390-todpreg") == 0) | |
10794 | return elfcore_write_s390_todpreg (abfd, buf, bufsiz, data, size); | |
10795 | if (strcmp (section, ".reg-s390-ctrs") == 0) | |
10796 | return elfcore_write_s390_ctrs (abfd, buf, bufsiz, data, size); | |
10797 | if (strcmp (section, ".reg-s390-prefix") == 0) | |
10798 | return elfcore_write_s390_prefix (abfd, buf, bufsiz, data, size); | |
355b81d9 UW |
10799 | if (strcmp (section, ".reg-s390-last-break") == 0) |
10800 | return elfcore_write_s390_last_break (abfd, buf, bufsiz, data, size); | |
10801 | if (strcmp (section, ".reg-s390-system-call") == 0) | |
10802 | return elfcore_write_s390_system_call (abfd, buf, bufsiz, data, size); | |
abb3f6cc NC |
10803 | if (strcmp (section, ".reg-s390-tdb") == 0) |
10804 | return elfcore_write_s390_tdb (abfd, buf, bufsiz, data, size); | |
4ef9f41a AA |
10805 | if (strcmp (section, ".reg-s390-vxrs-low") == 0) |
10806 | return elfcore_write_s390_vxrs_low (abfd, buf, bufsiz, data, size); | |
10807 | if (strcmp (section, ".reg-s390-vxrs-high") == 0) | |
10808 | return elfcore_write_s390_vxrs_high (abfd, buf, bufsiz, data, size); | |
faa9a424 UW |
10809 | if (strcmp (section, ".reg-arm-vfp") == 0) |
10810 | return elfcore_write_arm_vfp (abfd, buf, bufsiz, data, size); | |
652451f8 YZ |
10811 | if (strcmp (section, ".reg-aarch-tls") == 0) |
10812 | return elfcore_write_aarch_tls (abfd, buf, bufsiz, data, size); | |
10813 | if (strcmp (section, ".reg-aarch-hw-break") == 0) | |
10814 | return elfcore_write_aarch_hw_break (abfd, buf, bufsiz, data, size); | |
10815 | if (strcmp (section, ".reg-aarch-hw-watch") == 0) | |
10816 | return elfcore_write_aarch_hw_watch (abfd, buf, bufsiz, data, size); | |
bb864ac1 CES |
10817 | return NULL; |
10818 | } | |
10819 | ||
b34976b6 | 10820 | static bfd_boolean |
718175fa | 10821 | elf_parse_notes (bfd *abfd, char *buf, size_t size, file_ptr offset) |
252b5132 | 10822 | { |
c044fabd | 10823 | char *p; |
252b5132 | 10824 | |
252b5132 RH |
10825 | p = buf; |
10826 | while (p < buf + size) | |
10827 | { | |
c044fabd KH |
10828 | /* FIXME: bad alignment assumption. */ |
10829 | Elf_External_Note *xnp = (Elf_External_Note *) p; | |
252b5132 RH |
10830 | Elf_Internal_Note in; |
10831 | ||
baea7ef1 AM |
10832 | if (offsetof (Elf_External_Note, name) > buf - p + size) |
10833 | return FALSE; | |
10834 | ||
dc810e39 | 10835 | in.type = H_GET_32 (abfd, xnp->type); |
252b5132 | 10836 | |
dc810e39 | 10837 | in.namesz = H_GET_32 (abfd, xnp->namesz); |
252b5132 | 10838 | in.namedata = xnp->name; |
baea7ef1 AM |
10839 | if (in.namesz > buf - in.namedata + size) |
10840 | return FALSE; | |
252b5132 | 10841 | |
dc810e39 | 10842 | in.descsz = H_GET_32 (abfd, xnp->descsz); |
252b5132 RH |
10843 | in.descdata = in.namedata + BFD_ALIGN (in.namesz, 4); |
10844 | in.descpos = offset + (in.descdata - buf); | |
baea7ef1 AM |
10845 | if (in.descsz != 0 |
10846 | && (in.descdata >= buf + size | |
10847 | || in.descsz > buf - in.descdata + size)) | |
10848 | return FALSE; | |
252b5132 | 10849 | |
718175fa JK |
10850 | switch (bfd_get_format (abfd)) |
10851 | { | |
10852 | default: | |
10853 | return TRUE; | |
10854 | ||
10855 | case bfd_core: | |
f64e188b | 10856 | { |
8acbedd6 | 10857 | #define GROKER_ELEMENT(S,F) {S, sizeof (S) - 1, F} |
f64e188b | 10858 | struct |
718175fa | 10859 | { |
f64e188b | 10860 | const char * string; |
8acbedd6 | 10861 | size_t len; |
f64e188b | 10862 | bfd_boolean (* func)(bfd *, Elf_Internal_Note *); |
718175fa | 10863 | } |
f64e188b | 10864 | grokers[] = |
b15fa79e | 10865 | { |
8acbedd6 | 10866 | GROKER_ELEMENT ("", elfcore_grok_note), |
aa1ed4a9 | 10867 | GROKER_ELEMENT ("FreeBSD", elfcore_grok_freebsd_note), |
8acbedd6 KS |
10868 | GROKER_ELEMENT ("NetBSD-CORE", elfcore_grok_netbsd_note), |
10869 | GROKER_ELEMENT ( "OpenBSD", elfcore_grok_openbsd_note), | |
10870 | GROKER_ELEMENT ("QNX", elfcore_grok_nto_note), | |
10871 | GROKER_ELEMENT ("SPU/", elfcore_grok_spu_note) | |
f64e188b | 10872 | }; |
8acbedd6 | 10873 | #undef GROKER_ELEMENT |
f64e188b NC |
10874 | int i; |
10875 | ||
10876 | for (i = ARRAY_SIZE (grokers); i--;) | |
8acbedd6 KS |
10877 | { |
10878 | if (in.namesz >= grokers[i].len | |
10879 | && strncmp (in.namedata, grokers[i].string, | |
10880 | grokers[i].len) == 0) | |
10881 | { | |
10882 | if (! grokers[i].func (abfd, & in)) | |
10883 | return FALSE; | |
10884 | break; | |
10885 | } | |
10886 | } | |
f64e188b NC |
10887 | break; |
10888 | } | |
718175fa JK |
10889 | |
10890 | case bfd_object: | |
10891 | if (in.namesz == sizeof "GNU" && strcmp (in.namedata, "GNU") == 0) | |
10892 | { | |
10893 | if (! elfobj_grok_gnu_note (abfd, &in)) | |
10894 | return FALSE; | |
10895 | } | |
e21e5835 NC |
10896 | else if (in.namesz == sizeof "stapsdt" |
10897 | && strcmp (in.namedata, "stapsdt") == 0) | |
10898 | { | |
10899 | if (! elfobj_grok_stapsdt_note (abfd, &in)) | |
10900 | return FALSE; | |
10901 | } | |
718175fa | 10902 | break; |
08a40648 | 10903 | } |
252b5132 RH |
10904 | |
10905 | p = in.descdata + BFD_ALIGN (in.descsz, 4); | |
10906 | } | |
10907 | ||
718175fa JK |
10908 | return TRUE; |
10909 | } | |
10910 | ||
10911 | static bfd_boolean | |
10912 | elf_read_notes (bfd *abfd, file_ptr offset, bfd_size_type size) | |
10913 | { | |
10914 | char *buf; | |
10915 | ||
10916 | if (size <= 0) | |
10917 | return TRUE; | |
10918 | ||
10919 | if (bfd_seek (abfd, offset, SEEK_SET) != 0) | |
10920 | return FALSE; | |
10921 | ||
f64e188b | 10922 | buf = (char *) bfd_malloc (size + 1); |
718175fa JK |
10923 | if (buf == NULL) |
10924 | return FALSE; | |
10925 | ||
f64e188b NC |
10926 | /* PR 17512: file: ec08f814 |
10927 | 0-termintate the buffer so that string searches will not overflow. */ | |
10928 | buf[size] = 0; | |
10929 | ||
718175fa JK |
10930 | if (bfd_bread (buf, size, abfd) != size |
10931 | || !elf_parse_notes (abfd, buf, size, offset)) | |
10932 | { | |
10933 | free (buf); | |
10934 | return FALSE; | |
10935 | } | |
10936 | ||
252b5132 | 10937 | free (buf); |
b34976b6 | 10938 | return TRUE; |
252b5132 | 10939 | } |
98d8431c JB |
10940 | \f |
10941 | /* Providing external access to the ELF program header table. */ | |
10942 | ||
10943 | /* Return an upper bound on the number of bytes required to store a | |
10944 | copy of ABFD's program header table entries. Return -1 if an error | |
10945 | occurs; bfd_get_error will return an appropriate code. */ | |
c044fabd | 10946 | |
98d8431c | 10947 | long |
217aa764 | 10948 | bfd_get_elf_phdr_upper_bound (bfd *abfd) |
98d8431c JB |
10949 | { |
10950 | if (abfd->xvec->flavour != bfd_target_elf_flavour) | |
10951 | { | |
10952 | bfd_set_error (bfd_error_wrong_format); | |
10953 | return -1; | |
10954 | } | |
10955 | ||
936e320b | 10956 | return elf_elfheader (abfd)->e_phnum * sizeof (Elf_Internal_Phdr); |
98d8431c JB |
10957 | } |
10958 | ||
98d8431c JB |
10959 | /* Copy ABFD's program header table entries to *PHDRS. The entries |
10960 | will be stored as an array of Elf_Internal_Phdr structures, as | |
10961 | defined in include/elf/internal.h. To find out how large the | |
10962 | buffer needs to be, call bfd_get_elf_phdr_upper_bound. | |
10963 | ||
10964 | Return the number of program header table entries read, or -1 if an | |
10965 | error occurs; bfd_get_error will return an appropriate code. */ | |
c044fabd | 10966 | |
98d8431c | 10967 | int |
217aa764 | 10968 | bfd_get_elf_phdrs (bfd *abfd, void *phdrs) |
98d8431c JB |
10969 | { |
10970 | int num_phdrs; | |
10971 | ||
10972 | if (abfd->xvec->flavour != bfd_target_elf_flavour) | |
10973 | { | |
10974 | bfd_set_error (bfd_error_wrong_format); | |
10975 | return -1; | |
10976 | } | |
10977 | ||
10978 | num_phdrs = elf_elfheader (abfd)->e_phnum; | |
c044fabd | 10979 | memcpy (phdrs, elf_tdata (abfd)->phdr, |
98d8431c JB |
10980 | num_phdrs * sizeof (Elf_Internal_Phdr)); |
10981 | ||
10982 | return num_phdrs; | |
10983 | } | |
ae4221d7 | 10984 | |
db6751f2 | 10985 | enum elf_reloc_type_class |
7e612e98 AM |
10986 | _bfd_elf_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED, |
10987 | const asection *rel_sec ATTRIBUTE_UNUSED, | |
10988 | const Elf_Internal_Rela *rela ATTRIBUTE_UNUSED) | |
db6751f2 JJ |
10989 | { |
10990 | return reloc_class_normal; | |
10991 | } | |
f8df10f4 | 10992 | |
47d9a591 | 10993 | /* For RELA architectures, return the relocation value for a |
f8df10f4 JJ |
10994 | relocation against a local symbol. */ |
10995 | ||
10996 | bfd_vma | |
217aa764 AM |
10997 | _bfd_elf_rela_local_sym (bfd *abfd, |
10998 | Elf_Internal_Sym *sym, | |
8517fae7 | 10999 | asection **psec, |
217aa764 | 11000 | Elf_Internal_Rela *rel) |
f8df10f4 | 11001 | { |
8517fae7 | 11002 | asection *sec = *psec; |
f8df10f4 JJ |
11003 | bfd_vma relocation; |
11004 | ||
11005 | relocation = (sec->output_section->vma | |
11006 | + sec->output_offset | |
11007 | + sym->st_value); | |
11008 | if ((sec->flags & SEC_MERGE) | |
c629eae0 | 11009 | && ELF_ST_TYPE (sym->st_info) == STT_SECTION |
dbaa2011 | 11010 | && sec->sec_info_type == SEC_INFO_TYPE_MERGE) |
f8df10f4 | 11011 | { |
f8df10f4 | 11012 | rel->r_addend = |
8517fae7 | 11013 | _bfd_merged_section_offset (abfd, psec, |
65765700 | 11014 | elf_section_data (sec)->sec_info, |
753731ee AM |
11015 | sym->st_value + rel->r_addend); |
11016 | if (sec != *psec) | |
11017 | { | |
11018 | /* If we have changed the section, and our original section is | |
11019 | marked with SEC_EXCLUDE, it means that the original | |
11020 | SEC_MERGE section has been completely subsumed in some | |
11021 | other SEC_MERGE section. In this case, we need to leave | |
11022 | some info around for --emit-relocs. */ | |
11023 | if ((sec->flags & SEC_EXCLUDE) != 0) | |
11024 | sec->kept_section = *psec; | |
11025 | sec = *psec; | |
11026 | } | |
8517fae7 AM |
11027 | rel->r_addend -= relocation; |
11028 | rel->r_addend += sec->output_section->vma + sec->output_offset; | |
f8df10f4 JJ |
11029 | } |
11030 | return relocation; | |
11031 | } | |
c629eae0 JJ |
11032 | |
11033 | bfd_vma | |
217aa764 AM |
11034 | _bfd_elf_rel_local_sym (bfd *abfd, |
11035 | Elf_Internal_Sym *sym, | |
11036 | asection **psec, | |
11037 | bfd_vma addend) | |
47d9a591 | 11038 | { |
c629eae0 JJ |
11039 | asection *sec = *psec; |
11040 | ||
dbaa2011 | 11041 | if (sec->sec_info_type != SEC_INFO_TYPE_MERGE) |
c629eae0 JJ |
11042 | return sym->st_value + addend; |
11043 | ||
11044 | return _bfd_merged_section_offset (abfd, psec, | |
65765700 | 11045 | elf_section_data (sec)->sec_info, |
753731ee | 11046 | sym->st_value + addend); |
c629eae0 JJ |
11047 | } |
11048 | ||
37b01f6a DG |
11049 | /* Adjust an address within a section. Given OFFSET within SEC, return |
11050 | the new offset within the section, based upon changes made to the | |
11051 | section. Returns -1 if the offset is now invalid. | |
11052 | The offset (in abnd out) is in target sized bytes, however big a | |
11053 | byte may be. */ | |
11054 | ||
c629eae0 | 11055 | bfd_vma |
217aa764 | 11056 | _bfd_elf_section_offset (bfd *abfd, |
92e4ec35 | 11057 | struct bfd_link_info *info, |
217aa764 AM |
11058 | asection *sec, |
11059 | bfd_vma offset) | |
c629eae0 | 11060 | { |
68bfbfcc | 11061 | switch (sec->sec_info_type) |
65765700 | 11062 | { |
dbaa2011 | 11063 | case SEC_INFO_TYPE_STABS: |
eea6121a AM |
11064 | return _bfd_stab_section_offset (sec, elf_section_data (sec)->sec_info, |
11065 | offset); | |
dbaa2011 | 11066 | case SEC_INFO_TYPE_EH_FRAME: |
92e4ec35 | 11067 | return _bfd_elf_eh_frame_section_offset (abfd, info, sec, offset); |
37b01f6a | 11068 | |
65765700 | 11069 | default: |
310fd250 L |
11070 | if ((sec->flags & SEC_ELF_REVERSE_COPY) != 0) |
11071 | { | |
37b01f6a | 11072 | /* Reverse the offset. */ |
310fd250 L |
11073 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
11074 | bfd_size_type address_size = bed->s->arch_size / 8; | |
37b01f6a DG |
11075 | |
11076 | /* address_size and sec->size are in octets. Convert | |
11077 | to bytes before subtracting the original offset. */ | |
11078 | offset = (sec->size - address_size) / bfd_octets_per_byte (abfd) - offset; | |
310fd250 | 11079 | } |
65765700 JJ |
11080 | return offset; |
11081 | } | |
c629eae0 | 11082 | } |
3333a7c3 RM |
11083 | \f |
11084 | /* Create a new BFD as if by bfd_openr. Rather than opening a file, | |
11085 | reconstruct an ELF file by reading the segments out of remote memory | |
11086 | based on the ELF file header at EHDR_VMA and the ELF program headers it | |
11087 | points to. If not null, *LOADBASEP is filled in with the difference | |
11088 | between the VMAs from which the segments were read, and the VMAs the | |
11089 | file headers (and hence BFD's idea of each section's VMA) put them at. | |
11090 | ||
11091 | The function TARGET_READ_MEMORY is called to copy LEN bytes from the | |
11092 | remote memory at target address VMA into the local buffer at MYADDR; it | |
11093 | should return zero on success or an `errno' code on failure. TEMPL must | |
11094 | be a BFD for an ELF target with the word size and byte order found in | |
11095 | the remote memory. */ | |
11096 | ||
11097 | bfd * | |
217aa764 AM |
11098 | bfd_elf_bfd_from_remote_memory |
11099 | (bfd *templ, | |
11100 | bfd_vma ehdr_vma, | |
f0a5d95a | 11101 | bfd_size_type size, |
217aa764 | 11102 | bfd_vma *loadbasep, |
fe78531d | 11103 | int (*target_read_memory) (bfd_vma, bfd_byte *, bfd_size_type)) |
3333a7c3 RM |
11104 | { |
11105 | return (*get_elf_backend_data (templ)->elf_backend_bfd_from_remote_memory) | |
5979d6b6 | 11106 | (templ, ehdr_vma, size, loadbasep, target_read_memory); |
3333a7c3 | 11107 | } |
4c45e5c9 JJ |
11108 | \f |
11109 | long | |
c9727e01 AM |
11110 | _bfd_elf_get_synthetic_symtab (bfd *abfd, |
11111 | long symcount ATTRIBUTE_UNUSED, | |
11112 | asymbol **syms ATTRIBUTE_UNUSED, | |
8615f3f2 | 11113 | long dynsymcount, |
c9727e01 AM |
11114 | asymbol **dynsyms, |
11115 | asymbol **ret) | |
4c45e5c9 JJ |
11116 | { |
11117 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
11118 | asection *relplt; | |
11119 | asymbol *s; | |
11120 | const char *relplt_name; | |
11121 | bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean); | |
11122 | arelent *p; | |
11123 | long count, i, n; | |
11124 | size_t size; | |
11125 | Elf_Internal_Shdr *hdr; | |
11126 | char *names; | |
11127 | asection *plt; | |
11128 | ||
8615f3f2 AM |
11129 | *ret = NULL; |
11130 | ||
90e3cdf2 JJ |
11131 | if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0) |
11132 | return 0; | |
11133 | ||
8615f3f2 AM |
11134 | if (dynsymcount <= 0) |
11135 | return 0; | |
11136 | ||
4c45e5c9 JJ |
11137 | if (!bed->plt_sym_val) |
11138 | return 0; | |
11139 | ||
11140 | relplt_name = bed->relplt_name; | |
11141 | if (relplt_name == NULL) | |
d35fd659 | 11142 | relplt_name = bed->rela_plts_and_copies_p ? ".rela.plt" : ".rel.plt"; |
4c45e5c9 JJ |
11143 | relplt = bfd_get_section_by_name (abfd, relplt_name); |
11144 | if (relplt == NULL) | |
11145 | return 0; | |
11146 | ||
11147 | hdr = &elf_section_data (relplt)->this_hdr; | |
11148 | if (hdr->sh_link != elf_dynsymtab (abfd) | |
11149 | || (hdr->sh_type != SHT_REL && hdr->sh_type != SHT_RELA)) | |
11150 | return 0; | |
11151 | ||
11152 | plt = bfd_get_section_by_name (abfd, ".plt"); | |
11153 | if (plt == NULL) | |
11154 | return 0; | |
11155 | ||
11156 | slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table; | |
c9727e01 | 11157 | if (! (*slurp_relocs) (abfd, relplt, dynsyms, TRUE)) |
4c45e5c9 JJ |
11158 | return -1; |
11159 | ||
eea6121a | 11160 | count = relplt->size / hdr->sh_entsize; |
4c45e5c9 JJ |
11161 | size = count * sizeof (asymbol); |
11162 | p = relplt->relocation; | |
cb53bf42 | 11163 | for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel) |
041de40d AM |
11164 | { |
11165 | size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt"); | |
11166 | if (p->addend != 0) | |
11167 | { | |
11168 | #ifdef BFD64 | |
11169 | size += sizeof ("+0x") - 1 + 8 + 8 * (bed->s->elfclass == ELFCLASS64); | |
11170 | #else | |
11171 | size += sizeof ("+0x") - 1 + 8; | |
11172 | #endif | |
11173 | } | |
11174 | } | |
4c45e5c9 | 11175 | |
a50b1753 | 11176 | s = *ret = (asymbol *) bfd_malloc (size); |
4c45e5c9 JJ |
11177 | if (s == NULL) |
11178 | return -1; | |
11179 | ||
11180 | names = (char *) (s + count); | |
11181 | p = relplt->relocation; | |
11182 | n = 0; | |
cb53bf42 | 11183 | for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel) |
4c45e5c9 JJ |
11184 | { |
11185 | size_t len; | |
11186 | bfd_vma addr; | |
11187 | ||
11188 | addr = bed->plt_sym_val (i, plt, p); | |
11189 | if (addr == (bfd_vma) -1) | |
11190 | continue; | |
11191 | ||
11192 | *s = **p->sym_ptr_ptr; | |
65a7a66f AM |
11193 | /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since |
11194 | we are defining a symbol, ensure one of them is set. */ | |
11195 | if ((s->flags & BSF_LOCAL) == 0) | |
11196 | s->flags |= BSF_GLOBAL; | |
6ba2a415 | 11197 | s->flags |= BSF_SYNTHETIC; |
4c45e5c9 JJ |
11198 | s->section = plt; |
11199 | s->value = addr - plt->vma; | |
11200 | s->name = names; | |
8f39ba8e | 11201 | s->udata.p = NULL; |
4c45e5c9 JJ |
11202 | len = strlen ((*p->sym_ptr_ptr)->name); |
11203 | memcpy (names, (*p->sym_ptr_ptr)->name, len); | |
11204 | names += len; | |
041de40d AM |
11205 | if (p->addend != 0) |
11206 | { | |
1d770845 | 11207 | char buf[30], *a; |
d324f6d6 | 11208 | |
041de40d AM |
11209 | memcpy (names, "+0x", sizeof ("+0x") - 1); |
11210 | names += sizeof ("+0x") - 1; | |
1d770845 L |
11211 | bfd_sprintf_vma (abfd, buf, p->addend); |
11212 | for (a = buf; *a == '0'; ++a) | |
11213 | ; | |
11214 | len = strlen (a); | |
11215 | memcpy (names, a, len); | |
11216 | names += len; | |
041de40d | 11217 | } |
4c45e5c9 JJ |
11218 | memcpy (names, "@plt", sizeof ("@plt")); |
11219 | names += sizeof ("@plt"); | |
8f39ba8e | 11220 | ++s, ++n; |
4c45e5c9 JJ |
11221 | } |
11222 | ||
11223 | return n; | |
11224 | } | |
3d7f7666 | 11225 | |
821e6ff6 AM |
11226 | /* It is only used by x86-64 so far. |
11227 | ??? This repeats *COM* id of zero. sec->id is supposed to be unique, | |
11228 | but current usage would allow all of _bfd_std_section to be zero. t*/ | |
3b22753a | 11229 | asection _bfd_elf_large_com_section |
821e6ff6 AM |
11230 | = BFD_FAKE_SECTION (_bfd_elf_large_com_section, NULL, |
11231 | "LARGE_COMMON", 0, SEC_IS_COMMON); | |
ecca9871 | 11232 | |
d1036acb | 11233 | void |
78245035 L |
11234 | _bfd_elf_post_process_headers (bfd * abfd, |
11235 | struct bfd_link_info * link_info ATTRIBUTE_UNUSED) | |
d1036acb L |
11236 | { |
11237 | Elf_Internal_Ehdr * i_ehdrp; /* ELF file header, internal form. */ | |
11238 | ||
11239 | i_ehdrp = elf_elfheader (abfd); | |
11240 | ||
11241 | i_ehdrp->e_ident[EI_OSABI] = get_elf_backend_data (abfd)->elf_osabi; | |
d8045f23 NC |
11242 | |
11243 | /* To make things simpler for the loader on Linux systems we set the | |
9c55345c | 11244 | osabi field to ELFOSABI_GNU if the binary contains symbols of |
f64b2e8d | 11245 | the STT_GNU_IFUNC type or STB_GNU_UNIQUE binding. */ |
d8045f23 | 11246 | if (i_ehdrp->e_ident[EI_OSABI] == ELFOSABI_NONE |
f64b2e8d | 11247 | && elf_tdata (abfd)->has_gnu_symbols) |
9c55345c | 11248 | i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_GNU; |
d1036acb | 11249 | } |
fcb93ecf PB |
11250 | |
11251 | ||
11252 | /* Return TRUE for ELF symbol types that represent functions. | |
11253 | This is the default version of this function, which is sufficient for | |
d8045f23 | 11254 | most targets. It returns true if TYPE is STT_FUNC or STT_GNU_IFUNC. */ |
fcb93ecf PB |
11255 | |
11256 | bfd_boolean | |
11257 | _bfd_elf_is_function_type (unsigned int type) | |
11258 | { | |
d8045f23 NC |
11259 | return (type == STT_FUNC |
11260 | || type == STT_GNU_IFUNC); | |
fcb93ecf | 11261 | } |
9f296da3 | 11262 | |
aef36ac1 AM |
11263 | /* If the ELF symbol SYM might be a function in SEC, return the |
11264 | function size and set *CODE_OFF to the function's entry point, | |
11265 | otherwise return zero. */ | |
9f296da3 | 11266 | |
aef36ac1 AM |
11267 | bfd_size_type |
11268 | _bfd_elf_maybe_function_sym (const asymbol *sym, asection *sec, | |
11269 | bfd_vma *code_off) | |
9f296da3 | 11270 | { |
aef36ac1 AM |
11271 | bfd_size_type size; |
11272 | ||
ff9e0f5b | 11273 | if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT |
aef36ac1 AM |
11274 | | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0 |
11275 | || sym->section != sec) | |
11276 | return 0; | |
ff9e0f5b | 11277 | |
ff9e0f5b | 11278 | *code_off = sym->value; |
aef36ac1 AM |
11279 | size = 0; |
11280 | if (!(sym->flags & BSF_SYNTHETIC)) | |
11281 | size = ((elf_symbol_type *) sym)->internal_elf_sym.st_size; | |
11282 | if (size == 0) | |
11283 | size = 1; | |
11284 | return size; | |
9f296da3 | 11285 | } |