]>
Commit | Line | Data |
---|---|---|
252b5132 | 1 | /* ELF executable support for BFD. |
340b6d91 | 2 | |
6f2750fe | 3 | Copyright (C) 1993-2016 Free Software Foundation, Inc. |
252b5132 | 4 | |
5e8d7549 | 5 | This file is part of BFD, the Binary File Descriptor library. |
252b5132 | 6 | |
5e8d7549 NC |
7 | This program is free software; you can redistribute it and/or modify |
8 | it under the terms of the GNU General Public License as published by | |
cd123cb7 | 9 | the Free Software Foundation; either version 3 of the License, or |
5e8d7549 | 10 | (at your option) any later version. |
252b5132 | 11 | |
5e8d7549 NC |
12 | This program is distributed in the hope that it will be useful, |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
252b5132 | 16 | |
5e8d7549 | 17 | You should have received a copy of the GNU General Public License |
b34976b6 | 18 | along with this program; if not, write to the Free Software |
cd123cb7 NC |
19 | Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, |
20 | MA 02110-1301, USA. */ | |
21 | ||
252b5132 | 22 | |
1b74d094 BW |
23 | /* |
24 | SECTION | |
252b5132 RH |
25 | ELF backends |
26 | ||
27 | BFD support for ELF formats is being worked on. | |
28 | Currently, the best supported back ends are for sparc and i386 | |
29 | (running svr4 or Solaris 2). | |
30 | ||
31 | Documentation of the internals of the support code still needs | |
32 | to be written. The code is changing quickly enough that we | |
661a3fd4 | 33 | haven't bothered yet. */ |
252b5132 | 34 | |
7ee38065 MS |
35 | /* For sparc64-cross-sparc32. */ |
36 | #define _SYSCALL32 | |
252b5132 | 37 | #include "sysdep.h" |
3db64b00 | 38 | #include "bfd.h" |
252b5132 RH |
39 | #include "bfdlink.h" |
40 | #include "libbfd.h" | |
41 | #define ARCH_SIZE 0 | |
42 | #include "elf-bfd.h" | |
e0e8c97f | 43 | #include "libiberty.h" |
ff59fc36 | 44 | #include "safe-ctype.h" |
de64ce13 | 45 | #include "elf-linux-core.h" |
252b5132 | 46 | |
8bc7f138 L |
47 | #ifdef CORE_HEADER |
48 | #include CORE_HEADER | |
49 | #endif | |
50 | ||
217aa764 | 51 | static int elf_sort_sections (const void *, const void *); |
c84fca4d | 52 | static bfd_boolean assign_file_positions_except_relocs (bfd *, struct bfd_link_info *); |
217aa764 | 53 | static bfd_boolean prep_headers (bfd *); |
ef10c3ac | 54 | static bfd_boolean swap_out_syms (bfd *, struct elf_strtab_hash **, int) ; |
718175fa JK |
55 | static bfd_boolean elf_read_notes (bfd *, file_ptr, bfd_size_type) ; |
56 | static bfd_boolean elf_parse_notes (bfd *abfd, char *buf, size_t size, | |
57 | file_ptr offset); | |
50b2bdb7 | 58 | |
252b5132 RH |
59 | /* Swap version information in and out. The version information is |
60 | currently size independent. If that ever changes, this code will | |
61 | need to move into elfcode.h. */ | |
62 | ||
63 | /* Swap in a Verdef structure. */ | |
64 | ||
65 | void | |
217aa764 AM |
66 | _bfd_elf_swap_verdef_in (bfd *abfd, |
67 | const Elf_External_Verdef *src, | |
68 | Elf_Internal_Verdef *dst) | |
252b5132 | 69 | { |
dc810e39 AM |
70 | dst->vd_version = H_GET_16 (abfd, src->vd_version); |
71 | dst->vd_flags = H_GET_16 (abfd, src->vd_flags); | |
72 | dst->vd_ndx = H_GET_16 (abfd, src->vd_ndx); | |
73 | dst->vd_cnt = H_GET_16 (abfd, src->vd_cnt); | |
74 | dst->vd_hash = H_GET_32 (abfd, src->vd_hash); | |
75 | dst->vd_aux = H_GET_32 (abfd, src->vd_aux); | |
76 | dst->vd_next = H_GET_32 (abfd, src->vd_next); | |
252b5132 RH |
77 | } |
78 | ||
79 | /* Swap out a Verdef structure. */ | |
80 | ||
81 | void | |
217aa764 AM |
82 | _bfd_elf_swap_verdef_out (bfd *abfd, |
83 | const Elf_Internal_Verdef *src, | |
84 | Elf_External_Verdef *dst) | |
252b5132 | 85 | { |
dc810e39 AM |
86 | H_PUT_16 (abfd, src->vd_version, dst->vd_version); |
87 | H_PUT_16 (abfd, src->vd_flags, dst->vd_flags); | |
88 | H_PUT_16 (abfd, src->vd_ndx, dst->vd_ndx); | |
89 | H_PUT_16 (abfd, src->vd_cnt, dst->vd_cnt); | |
90 | H_PUT_32 (abfd, src->vd_hash, dst->vd_hash); | |
91 | H_PUT_32 (abfd, src->vd_aux, dst->vd_aux); | |
92 | H_PUT_32 (abfd, src->vd_next, dst->vd_next); | |
252b5132 RH |
93 | } |
94 | ||
95 | /* Swap in a Verdaux structure. */ | |
96 | ||
97 | void | |
217aa764 AM |
98 | _bfd_elf_swap_verdaux_in (bfd *abfd, |
99 | const Elf_External_Verdaux *src, | |
100 | Elf_Internal_Verdaux *dst) | |
252b5132 | 101 | { |
dc810e39 AM |
102 | dst->vda_name = H_GET_32 (abfd, src->vda_name); |
103 | dst->vda_next = H_GET_32 (abfd, src->vda_next); | |
252b5132 RH |
104 | } |
105 | ||
106 | /* Swap out a Verdaux structure. */ | |
107 | ||
108 | void | |
217aa764 AM |
109 | _bfd_elf_swap_verdaux_out (bfd *abfd, |
110 | const Elf_Internal_Verdaux *src, | |
111 | Elf_External_Verdaux *dst) | |
252b5132 | 112 | { |
dc810e39 AM |
113 | H_PUT_32 (abfd, src->vda_name, dst->vda_name); |
114 | H_PUT_32 (abfd, src->vda_next, dst->vda_next); | |
252b5132 RH |
115 | } |
116 | ||
117 | /* Swap in a Verneed structure. */ | |
118 | ||
119 | void | |
217aa764 AM |
120 | _bfd_elf_swap_verneed_in (bfd *abfd, |
121 | const Elf_External_Verneed *src, | |
122 | Elf_Internal_Verneed *dst) | |
252b5132 | 123 | { |
dc810e39 AM |
124 | dst->vn_version = H_GET_16 (abfd, src->vn_version); |
125 | dst->vn_cnt = H_GET_16 (abfd, src->vn_cnt); | |
126 | dst->vn_file = H_GET_32 (abfd, src->vn_file); | |
127 | dst->vn_aux = H_GET_32 (abfd, src->vn_aux); | |
128 | dst->vn_next = H_GET_32 (abfd, src->vn_next); | |
252b5132 RH |
129 | } |
130 | ||
131 | /* Swap out a Verneed structure. */ | |
132 | ||
133 | void | |
217aa764 AM |
134 | _bfd_elf_swap_verneed_out (bfd *abfd, |
135 | const Elf_Internal_Verneed *src, | |
136 | Elf_External_Verneed *dst) | |
252b5132 | 137 | { |
dc810e39 AM |
138 | H_PUT_16 (abfd, src->vn_version, dst->vn_version); |
139 | H_PUT_16 (abfd, src->vn_cnt, dst->vn_cnt); | |
140 | H_PUT_32 (abfd, src->vn_file, dst->vn_file); | |
141 | H_PUT_32 (abfd, src->vn_aux, dst->vn_aux); | |
142 | H_PUT_32 (abfd, src->vn_next, dst->vn_next); | |
252b5132 RH |
143 | } |
144 | ||
145 | /* Swap in a Vernaux structure. */ | |
146 | ||
147 | void | |
217aa764 AM |
148 | _bfd_elf_swap_vernaux_in (bfd *abfd, |
149 | const Elf_External_Vernaux *src, | |
150 | Elf_Internal_Vernaux *dst) | |
252b5132 | 151 | { |
dc810e39 AM |
152 | dst->vna_hash = H_GET_32 (abfd, src->vna_hash); |
153 | dst->vna_flags = H_GET_16 (abfd, src->vna_flags); | |
154 | dst->vna_other = H_GET_16 (abfd, src->vna_other); | |
155 | dst->vna_name = H_GET_32 (abfd, src->vna_name); | |
156 | dst->vna_next = H_GET_32 (abfd, src->vna_next); | |
252b5132 RH |
157 | } |
158 | ||
159 | /* Swap out a Vernaux structure. */ | |
160 | ||
161 | void | |
217aa764 AM |
162 | _bfd_elf_swap_vernaux_out (bfd *abfd, |
163 | const Elf_Internal_Vernaux *src, | |
164 | Elf_External_Vernaux *dst) | |
252b5132 | 165 | { |
dc810e39 AM |
166 | H_PUT_32 (abfd, src->vna_hash, dst->vna_hash); |
167 | H_PUT_16 (abfd, src->vna_flags, dst->vna_flags); | |
168 | H_PUT_16 (abfd, src->vna_other, dst->vna_other); | |
169 | H_PUT_32 (abfd, src->vna_name, dst->vna_name); | |
170 | H_PUT_32 (abfd, src->vna_next, dst->vna_next); | |
252b5132 RH |
171 | } |
172 | ||
173 | /* Swap in a Versym structure. */ | |
174 | ||
175 | void | |
217aa764 AM |
176 | _bfd_elf_swap_versym_in (bfd *abfd, |
177 | const Elf_External_Versym *src, | |
178 | Elf_Internal_Versym *dst) | |
252b5132 | 179 | { |
dc810e39 | 180 | dst->vs_vers = H_GET_16 (abfd, src->vs_vers); |
252b5132 RH |
181 | } |
182 | ||
183 | /* Swap out a Versym structure. */ | |
184 | ||
185 | void | |
217aa764 AM |
186 | _bfd_elf_swap_versym_out (bfd *abfd, |
187 | const Elf_Internal_Versym *src, | |
188 | Elf_External_Versym *dst) | |
252b5132 | 189 | { |
dc810e39 | 190 | H_PUT_16 (abfd, src->vs_vers, dst->vs_vers); |
252b5132 RH |
191 | } |
192 | ||
193 | /* Standard ELF hash function. Do not change this function; you will | |
194 | cause invalid hash tables to be generated. */ | |
3a99b017 | 195 | |
252b5132 | 196 | unsigned long |
217aa764 | 197 | bfd_elf_hash (const char *namearg) |
252b5132 | 198 | { |
3a99b017 | 199 | const unsigned char *name = (const unsigned char *) namearg; |
252b5132 RH |
200 | unsigned long h = 0; |
201 | unsigned long g; | |
202 | int ch; | |
203 | ||
204 | while ((ch = *name++) != '\0') | |
205 | { | |
206 | h = (h << 4) + ch; | |
207 | if ((g = (h & 0xf0000000)) != 0) | |
208 | { | |
209 | h ^= g >> 24; | |
210 | /* The ELF ABI says `h &= ~g', but this is equivalent in | |
211 | this case and on some machines one insn instead of two. */ | |
212 | h ^= g; | |
213 | } | |
214 | } | |
32dfa85d | 215 | return h & 0xffffffff; |
252b5132 RH |
216 | } |
217 | ||
fdc90cb4 JJ |
218 | /* DT_GNU_HASH hash function. Do not change this function; you will |
219 | cause invalid hash tables to be generated. */ | |
220 | ||
221 | unsigned long | |
222 | bfd_elf_gnu_hash (const char *namearg) | |
223 | { | |
224 | const unsigned char *name = (const unsigned char *) namearg; | |
225 | unsigned long h = 5381; | |
226 | unsigned char ch; | |
227 | ||
228 | while ((ch = *name++) != '\0') | |
229 | h = (h << 5) + h + ch; | |
230 | return h & 0xffffffff; | |
231 | } | |
232 | ||
0c8d6e5c AM |
233 | /* Create a tdata field OBJECT_SIZE bytes in length, zeroed out and with |
234 | the object_id field of an elf_obj_tdata field set to OBJECT_ID. */ | |
b34976b6 | 235 | bfd_boolean |
0c8d6e5c | 236 | bfd_elf_allocate_object (bfd *abfd, |
0ffa91dd | 237 | size_t object_size, |
4dfe6ac6 | 238 | enum elf_target_id object_id) |
252b5132 | 239 | { |
0ffa91dd NC |
240 | BFD_ASSERT (object_size >= sizeof (struct elf_obj_tdata)); |
241 | abfd->tdata.any = bfd_zalloc (abfd, object_size); | |
242 | if (abfd->tdata.any == NULL) | |
243 | return FALSE; | |
252b5132 | 244 | |
0ffa91dd | 245 | elf_object_id (abfd) = object_id; |
c0355132 AM |
246 | if (abfd->direction != read_direction) |
247 | { | |
248 | struct output_elf_obj_tdata *o = bfd_zalloc (abfd, sizeof *o); | |
249 | if (o == NULL) | |
250 | return FALSE; | |
251 | elf_tdata (abfd)->o = o; | |
252 | elf_program_header_size (abfd) = (bfd_size_type) -1; | |
253 | } | |
b34976b6 | 254 | return TRUE; |
252b5132 RH |
255 | } |
256 | ||
0ffa91dd NC |
257 | |
258 | bfd_boolean | |
ae95ffa6 | 259 | bfd_elf_make_object (bfd *abfd) |
0ffa91dd | 260 | { |
ae95ffa6 | 261 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
0ffa91dd | 262 | return bfd_elf_allocate_object (abfd, sizeof (struct elf_obj_tdata), |
ae95ffa6 | 263 | bed->target_id); |
0ffa91dd NC |
264 | } |
265 | ||
b34976b6 | 266 | bfd_boolean |
217aa764 | 267 | bfd_elf_mkcorefile (bfd *abfd) |
252b5132 | 268 | { |
c044fabd | 269 | /* I think this can be done just like an object file. */ |
228e534f AM |
270 | if (!abfd->xvec->_bfd_set_format[(int) bfd_object] (abfd)) |
271 | return FALSE; | |
272 | elf_tdata (abfd)->core = bfd_zalloc (abfd, sizeof (*elf_tdata (abfd)->core)); | |
273 | return elf_tdata (abfd)->core != NULL; | |
252b5132 RH |
274 | } |
275 | ||
72a80a16 | 276 | static char * |
217aa764 | 277 | bfd_elf_get_str_section (bfd *abfd, unsigned int shindex) |
252b5132 RH |
278 | { |
279 | Elf_Internal_Shdr **i_shdrp; | |
f075ee0c | 280 | bfd_byte *shstrtab = NULL; |
dc810e39 AM |
281 | file_ptr offset; |
282 | bfd_size_type shstrtabsize; | |
252b5132 RH |
283 | |
284 | i_shdrp = elf_elfsections (abfd); | |
74f2e02b AM |
285 | if (i_shdrp == 0 |
286 | || shindex >= elf_numsections (abfd) | |
287 | || i_shdrp[shindex] == 0) | |
f075ee0c | 288 | return NULL; |
252b5132 | 289 | |
f075ee0c | 290 | shstrtab = i_shdrp[shindex]->contents; |
252b5132 RH |
291 | if (shstrtab == NULL) |
292 | { | |
c044fabd | 293 | /* No cached one, attempt to read, and cache what we read. */ |
252b5132 RH |
294 | offset = i_shdrp[shindex]->sh_offset; |
295 | shstrtabsize = i_shdrp[shindex]->sh_size; | |
c6c60d09 JJ |
296 | |
297 | /* Allocate and clear an extra byte at the end, to prevent crashes | |
298 | in case the string table is not terminated. */ | |
3471d59d | 299 | if (shstrtabsize + 1 <= 1 |
06614111 NC |
300 | || bfd_seek (abfd, offset, SEEK_SET) != 0 |
301 | || (shstrtab = (bfd_byte *) bfd_alloc (abfd, shstrtabsize + 1)) == NULL) | |
c6c60d09 JJ |
302 | shstrtab = NULL; |
303 | else if (bfd_bread (shstrtab, shstrtabsize, abfd) != shstrtabsize) | |
304 | { | |
305 | if (bfd_get_error () != bfd_error_system_call) | |
306 | bfd_set_error (bfd_error_file_truncated); | |
06614111 | 307 | bfd_release (abfd, shstrtab); |
c6c60d09 | 308 | shstrtab = NULL; |
3471d59d CC |
309 | /* Once we've failed to read it, make sure we don't keep |
310 | trying. Otherwise, we'll keep allocating space for | |
311 | the string table over and over. */ | |
312 | i_shdrp[shindex]->sh_size = 0; | |
c6c60d09 JJ |
313 | } |
314 | else | |
315 | shstrtab[shstrtabsize] = '\0'; | |
217aa764 | 316 | i_shdrp[shindex]->contents = shstrtab; |
252b5132 | 317 | } |
f075ee0c | 318 | return (char *) shstrtab; |
252b5132 RH |
319 | } |
320 | ||
321 | char * | |
217aa764 AM |
322 | bfd_elf_string_from_elf_section (bfd *abfd, |
323 | unsigned int shindex, | |
324 | unsigned int strindex) | |
252b5132 RH |
325 | { |
326 | Elf_Internal_Shdr *hdr; | |
327 | ||
328 | if (strindex == 0) | |
329 | return ""; | |
330 | ||
74f2e02b AM |
331 | if (elf_elfsections (abfd) == NULL || shindex >= elf_numsections (abfd)) |
332 | return NULL; | |
333 | ||
252b5132 RH |
334 | hdr = elf_elfsections (abfd)[shindex]; |
335 | ||
06614111 NC |
336 | if (hdr->contents == NULL) |
337 | { | |
338 | if (hdr->sh_type != SHT_STRTAB && hdr->sh_type < SHT_LOOS) | |
339 | { | |
340 | /* PR 17512: file: f057ec89. */ | |
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 | { | |
1349 | sh_link = find_link (obfd, iheaders[iheader->sh_link], iheader->sh_link); | |
1350 | if (sh_link != SHN_UNDEF) | |
1351 | { | |
1352 | oheader->sh_link = sh_link; | |
1353 | changed = TRUE; | |
1354 | } | |
1355 | else | |
1356 | /* FIXME: Should we install iheader->sh_link | |
1357 | if we could not find a match ? */ | |
1358 | (* _bfd_error_handler) | |
695344c0 | 1359 | /* xgettext:c-format */ |
5522f910 NC |
1360 | (_("%B: Failed to find link section for section %d"), obfd, secnum); |
1361 | } | |
1362 | ||
1363 | if (iheader->sh_info) | |
1364 | { | |
1365 | /* The sh_info field can hold arbitrary information, but if the | |
1366 | SHF_LINK_INFO flag is set then it should be interpreted as a | |
1367 | section index. */ | |
1368 | if (iheader->sh_flags & SHF_INFO_LINK) | |
1369 | { | |
1370 | sh_link = find_link (obfd, iheaders[iheader->sh_info], | |
1371 | iheader->sh_info); | |
1372 | if (sh_link != SHN_UNDEF) | |
1373 | oheader->sh_flags |= SHF_INFO_LINK; | |
1374 | } | |
1375 | else | |
1376 | /* No idea what it means - just copy it. */ | |
1377 | sh_link = iheader->sh_info; | |
1378 | ||
1379 | if (sh_link != SHN_UNDEF) | |
1380 | { | |
1381 | oheader->sh_info = sh_link; | |
1382 | changed = TRUE; | |
1383 | } | |
1384 | else | |
1385 | (* _bfd_error_handler) | |
695344c0 | 1386 | /* xgettext:c-format */ |
5522f910 NC |
1387 | (_("%B: Failed to find info section for section %d"), obfd, secnum); |
1388 | } | |
1389 | ||
1390 | return changed; | |
1391 | } | |
1392 | ||
0ac4564e L |
1393 | /* Copy the program header and other data from one object module to |
1394 | another. */ | |
252b5132 | 1395 | |
b34976b6 | 1396 | bfd_boolean |
217aa764 | 1397 | _bfd_elf_copy_private_bfd_data (bfd *ibfd, bfd *obfd) |
2d502050 | 1398 | { |
5522f910 NC |
1399 | const Elf_Internal_Shdr **iheaders = (const Elf_Internal_Shdr **) elf_elfsections (ibfd); |
1400 | Elf_Internal_Shdr **oheaders = elf_elfsections (obfd); | |
1401 | const struct elf_backend_data *bed; | |
84865015 NC |
1402 | unsigned int i; |
1403 | ||
2d502050 | 1404 | if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour |
84865015 | 1405 | || bfd_get_flavour (obfd) != bfd_target_elf_flavour) |
b34976b6 | 1406 | return TRUE; |
2d502050 | 1407 | |
57b828ef L |
1408 | if (!elf_flags_init (obfd)) |
1409 | { | |
1410 | elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags; | |
1411 | elf_flags_init (obfd) = TRUE; | |
1412 | } | |
2d502050 | 1413 | |
0ac4564e | 1414 | elf_gp (obfd) = elf_gp (ibfd); |
57b828ef L |
1415 | |
1416 | /* Also copy the EI_OSABI field. */ | |
1417 | elf_elfheader (obfd)->e_ident[EI_OSABI] = | |
1418 | elf_elfheader (ibfd)->e_ident[EI_OSABI]; | |
104d59d1 | 1419 | |
5522f910 NC |
1420 | /* If set, copy the EI_ABIVERSION field. */ |
1421 | if (elf_elfheader (ibfd)->e_ident[EI_ABIVERSION]) | |
1422 | elf_elfheader (obfd)->e_ident[EI_ABIVERSION] | |
1423 | = elf_elfheader (ibfd)->e_ident[EI_ABIVERSION]; | |
1424 | ||
104d59d1 JM |
1425 | /* Copy object attributes. */ |
1426 | _bfd_elf_copy_obj_attributes (ibfd, obfd); | |
63b9bbb7 | 1427 | |
84865015 NC |
1428 | if (iheaders == NULL || oheaders == NULL) |
1429 | return TRUE; | |
63b9bbb7 | 1430 | |
5522f910 NC |
1431 | bed = get_elf_backend_data (obfd); |
1432 | ||
1433 | /* Possibly copy other fields in the section header. */ | |
84865015 | 1434 | for (i = 1; i < elf_numsections (obfd); i++) |
63b9bbb7 | 1435 | { |
84865015 NC |
1436 | unsigned int j; |
1437 | Elf_Internal_Shdr * oheader = oheaders[i]; | |
63b9bbb7 | 1438 | |
5522f910 NC |
1439 | /* Ignore ordinary sections. SHT_NOBITS sections are considered however |
1440 | because of a special case need for generating separate debug info | |
1441 | files. See below for more details. */ | |
84865015 NC |
1442 | if (oheader == NULL |
1443 | || (oheader->sh_type != SHT_NOBITS | |
5522f910 NC |
1444 | && oheader->sh_type < SHT_LOOS)) |
1445 | continue; | |
1446 | ||
1447 | /* Ignore empty sections, and sections whose | |
1448 | fields have already been initialised. */ | |
1449 | if (oheader->sh_size == 0 | |
84865015 NC |
1450 | || (oheader->sh_info != 0 && oheader->sh_link != 0)) |
1451 | continue; | |
63b9bbb7 | 1452 | |
84865015 | 1453 | /* Scan for the matching section in the input bfd. |
5522f910 NC |
1454 | First we try for a direct mapping between the input and output sections. */ |
1455 | for (j = 1; j < elf_numsections (ibfd); j++) | |
1456 | { | |
1457 | const Elf_Internal_Shdr * iheader = iheaders[j]; | |
1458 | ||
1459 | if (iheader == NULL) | |
1460 | continue; | |
1461 | ||
1462 | if (oheader->bfd_section != NULL | |
1463 | && iheader->bfd_section != NULL | |
1464 | && iheader->bfd_section->output_section != NULL | |
1465 | && iheader->bfd_section->output_section == oheader->bfd_section) | |
1466 | { | |
1467 | /* We have found a connection from the input section to the | |
1468 | output section. Attempt to copy the header fields. If | |
1469 | this fails then do not try any further sections - there | |
1470 | should only be a one-to-one mapping between input and output. */ | |
1471 | if (! copy_special_section_fields (ibfd, obfd, iheader, oheader, i)) | |
1472 | j = elf_numsections (ibfd); | |
1473 | break; | |
1474 | } | |
1475 | } | |
1476 | ||
1477 | if (j < elf_numsections (ibfd)) | |
1478 | continue; | |
1479 | ||
1480 | /* That failed. So try to deduce the corresponding input section. | |
84865015 NC |
1481 | Unfortunately we cannot compare names as the output string table |
1482 | is empty, so instead we check size, address and type. */ | |
1483 | for (j = 1; j < elf_numsections (ibfd); j++) | |
1484 | { | |
5522f910 | 1485 | const Elf_Internal_Shdr * iheader = iheaders[j]; |
84865015 | 1486 | |
5522f910 NC |
1487 | if (iheader == NULL) |
1488 | continue; | |
1489 | ||
1490 | /* Try matching fields in the input section's header. | |
1491 | Since --only-keep-debug turns all non-debug sections into | |
84865015 NC |
1492 | SHT_NOBITS sections, the output SHT_NOBITS type matches any |
1493 | input type. */ | |
1494 | if ((oheader->sh_type == SHT_NOBITS | |
1495 | || iheader->sh_type == oheader->sh_type) | |
5522f910 NC |
1496 | && (iheader->sh_flags & ~ SHF_INFO_LINK) |
1497 | == (oheader->sh_flags & ~ SHF_INFO_LINK) | |
84865015 NC |
1498 | && iheader->sh_addralign == oheader->sh_addralign |
1499 | && iheader->sh_entsize == oheader->sh_entsize | |
1500 | && iheader->sh_size == oheader->sh_size | |
1501 | && iheader->sh_addr == oheader->sh_addr | |
1502 | && (iheader->sh_info != oheader->sh_info | |
1503 | || iheader->sh_link != oheader->sh_link)) | |
63b9bbb7 | 1504 | { |
5522f910 NC |
1505 | if (copy_special_section_fields (ibfd, obfd, iheader, oheader, i)) |
1506 | break; | |
63b9bbb7 NC |
1507 | } |
1508 | } | |
5522f910 NC |
1509 | |
1510 | if (j == elf_numsections (ibfd) && oheader->sh_type >= SHT_LOOS) | |
1511 | { | |
1512 | /* Final attempt. Call the backend copy function | |
1513 | with a NULL input section. */ | |
1514 | if (bed->elf_backend_copy_special_section_fields != NULL) | |
1515 | bed->elf_backend_copy_special_section_fields (ibfd, obfd, NULL, oheader); | |
1516 | } | |
63b9bbb7 NC |
1517 | } |
1518 | ||
b34976b6 | 1519 | return TRUE; |
2d502050 L |
1520 | } |
1521 | ||
cedc298e L |
1522 | static const char * |
1523 | get_segment_type (unsigned int p_type) | |
1524 | { | |
1525 | const char *pt; | |
1526 | switch (p_type) | |
1527 | { | |
1528 | case PT_NULL: pt = "NULL"; break; | |
1529 | case PT_LOAD: pt = "LOAD"; break; | |
1530 | case PT_DYNAMIC: pt = "DYNAMIC"; break; | |
1531 | case PT_INTERP: pt = "INTERP"; break; | |
1532 | case PT_NOTE: pt = "NOTE"; break; | |
1533 | case PT_SHLIB: pt = "SHLIB"; break; | |
1534 | case PT_PHDR: pt = "PHDR"; break; | |
1535 | case PT_TLS: pt = "TLS"; break; | |
1536 | case PT_GNU_EH_FRAME: pt = "EH_FRAME"; break; | |
2b05f1b7 | 1537 | case PT_GNU_STACK: pt = "STACK"; break; |
cedc298e L |
1538 | case PT_GNU_RELRO: pt = "RELRO"; break; |
1539 | default: pt = NULL; break; | |
1540 | } | |
1541 | return pt; | |
1542 | } | |
1543 | ||
f0b79d91 L |
1544 | /* Print out the program headers. */ |
1545 | ||
b34976b6 | 1546 | bfd_boolean |
217aa764 | 1547 | _bfd_elf_print_private_bfd_data (bfd *abfd, void *farg) |
252b5132 | 1548 | { |
a50b1753 | 1549 | FILE *f = (FILE *) farg; |
252b5132 RH |
1550 | Elf_Internal_Phdr *p; |
1551 | asection *s; | |
1552 | bfd_byte *dynbuf = NULL; | |
1553 | ||
1554 | p = elf_tdata (abfd)->phdr; | |
1555 | if (p != NULL) | |
1556 | { | |
1557 | unsigned int i, c; | |
1558 | ||
1559 | fprintf (f, _("\nProgram Header:\n")); | |
1560 | c = elf_elfheader (abfd)->e_phnum; | |
1561 | for (i = 0; i < c; i++, p++) | |
1562 | { | |
cedc298e | 1563 | const char *pt = get_segment_type (p->p_type); |
252b5132 RH |
1564 | char buf[20]; |
1565 | ||
cedc298e | 1566 | if (pt == NULL) |
252b5132 | 1567 | { |
cedc298e L |
1568 | sprintf (buf, "0x%lx", p->p_type); |
1569 | pt = buf; | |
252b5132 | 1570 | } |
dc810e39 | 1571 | fprintf (f, "%8s off 0x", pt); |
60b89a18 | 1572 | bfd_fprintf_vma (abfd, f, p->p_offset); |
252b5132 | 1573 | fprintf (f, " vaddr 0x"); |
60b89a18 | 1574 | bfd_fprintf_vma (abfd, f, p->p_vaddr); |
252b5132 | 1575 | fprintf (f, " paddr 0x"); |
60b89a18 | 1576 | bfd_fprintf_vma (abfd, f, p->p_paddr); |
252b5132 RH |
1577 | fprintf (f, " align 2**%u\n", bfd_log2 (p->p_align)); |
1578 | fprintf (f, " filesz 0x"); | |
60b89a18 | 1579 | bfd_fprintf_vma (abfd, f, p->p_filesz); |
252b5132 | 1580 | fprintf (f, " memsz 0x"); |
60b89a18 | 1581 | bfd_fprintf_vma (abfd, f, p->p_memsz); |
252b5132 RH |
1582 | fprintf (f, " flags %c%c%c", |
1583 | (p->p_flags & PF_R) != 0 ? 'r' : '-', | |
1584 | (p->p_flags & PF_W) != 0 ? 'w' : '-', | |
1585 | (p->p_flags & PF_X) != 0 ? 'x' : '-'); | |
dc810e39 AM |
1586 | if ((p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X)) != 0) |
1587 | fprintf (f, " %lx", p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X)); | |
252b5132 RH |
1588 | fprintf (f, "\n"); |
1589 | } | |
1590 | } | |
1591 | ||
1592 | s = bfd_get_section_by_name (abfd, ".dynamic"); | |
1593 | if (s != NULL) | |
1594 | { | |
cb33740c | 1595 | unsigned int elfsec; |
dc810e39 | 1596 | unsigned long shlink; |
252b5132 RH |
1597 | bfd_byte *extdyn, *extdynend; |
1598 | size_t extdynsize; | |
217aa764 | 1599 | void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *); |
252b5132 RH |
1600 | |
1601 | fprintf (f, _("\nDynamic Section:\n")); | |
1602 | ||
eea6121a | 1603 | if (!bfd_malloc_and_get_section (abfd, s, &dynbuf)) |
252b5132 RH |
1604 | goto error_return; |
1605 | ||
1606 | elfsec = _bfd_elf_section_from_bfd_section (abfd, s); | |
cb33740c | 1607 | if (elfsec == SHN_BAD) |
252b5132 | 1608 | goto error_return; |
dc810e39 | 1609 | shlink = elf_elfsections (abfd)[elfsec]->sh_link; |
252b5132 RH |
1610 | |
1611 | extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn; | |
1612 | swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in; | |
1613 | ||
1614 | extdyn = dynbuf; | |
06614111 NC |
1615 | /* PR 17512: file: 6f427532. */ |
1616 | if (s->size < extdynsize) | |
1617 | goto error_return; | |
eea6121a | 1618 | extdynend = extdyn + s->size; |
1036838a NC |
1619 | /* PR 17512: file: id:000006,sig:06,src:000000,op:flip4,pos:5664. |
1620 | Fix range check. */ | |
1621 | for (; extdyn <= (extdynend - extdynsize); extdyn += extdynsize) | |
252b5132 RH |
1622 | { |
1623 | Elf_Internal_Dyn dyn; | |
ad9563d6 | 1624 | const char *name = ""; |
252b5132 | 1625 | char ab[20]; |
b34976b6 | 1626 | bfd_boolean stringp; |
ad9563d6 | 1627 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
252b5132 | 1628 | |
217aa764 | 1629 | (*swap_dyn_in) (abfd, extdyn, &dyn); |
252b5132 RH |
1630 | |
1631 | if (dyn.d_tag == DT_NULL) | |
1632 | break; | |
1633 | ||
b34976b6 | 1634 | stringp = FALSE; |
252b5132 RH |
1635 | switch (dyn.d_tag) |
1636 | { | |
1637 | default: | |
ad9563d6 CM |
1638 | if (bed->elf_backend_get_target_dtag) |
1639 | name = (*bed->elf_backend_get_target_dtag) (dyn.d_tag); | |
1640 | ||
1641 | if (!strcmp (name, "")) | |
1642 | { | |
1643 | sprintf (ab, "0x%lx", (unsigned long) dyn.d_tag); | |
1644 | name = ab; | |
1645 | } | |
252b5132 RH |
1646 | break; |
1647 | ||
b34976b6 | 1648 | case DT_NEEDED: name = "NEEDED"; stringp = TRUE; break; |
252b5132 RH |
1649 | case DT_PLTRELSZ: name = "PLTRELSZ"; break; |
1650 | case DT_PLTGOT: name = "PLTGOT"; break; | |
1651 | case DT_HASH: name = "HASH"; break; | |
1652 | case DT_STRTAB: name = "STRTAB"; break; | |
1653 | case DT_SYMTAB: name = "SYMTAB"; break; | |
1654 | case DT_RELA: name = "RELA"; break; | |
1655 | case DT_RELASZ: name = "RELASZ"; break; | |
1656 | case DT_RELAENT: name = "RELAENT"; break; | |
1657 | case DT_STRSZ: name = "STRSZ"; break; | |
1658 | case DT_SYMENT: name = "SYMENT"; break; | |
1659 | case DT_INIT: name = "INIT"; break; | |
1660 | case DT_FINI: name = "FINI"; break; | |
b34976b6 AM |
1661 | case DT_SONAME: name = "SONAME"; stringp = TRUE; break; |
1662 | case DT_RPATH: name = "RPATH"; stringp = TRUE; break; | |
252b5132 RH |
1663 | case DT_SYMBOLIC: name = "SYMBOLIC"; break; |
1664 | case DT_REL: name = "REL"; break; | |
1665 | case DT_RELSZ: name = "RELSZ"; break; | |
1666 | case DT_RELENT: name = "RELENT"; break; | |
1667 | case DT_PLTREL: name = "PLTREL"; break; | |
1668 | case DT_DEBUG: name = "DEBUG"; break; | |
1669 | case DT_TEXTREL: name = "TEXTREL"; break; | |
1670 | case DT_JMPREL: name = "JMPREL"; break; | |
94558834 L |
1671 | case DT_BIND_NOW: name = "BIND_NOW"; break; |
1672 | case DT_INIT_ARRAY: name = "INIT_ARRAY"; break; | |
1673 | case DT_FINI_ARRAY: name = "FINI_ARRAY"; break; | |
1674 | case DT_INIT_ARRAYSZ: name = "INIT_ARRAYSZ"; break; | |
1675 | case DT_FINI_ARRAYSZ: name = "FINI_ARRAYSZ"; break; | |
b34976b6 | 1676 | case DT_RUNPATH: name = "RUNPATH"; stringp = TRUE; break; |
94558834 L |
1677 | case DT_FLAGS: name = "FLAGS"; break; |
1678 | case DT_PREINIT_ARRAY: name = "PREINIT_ARRAY"; break; | |
1679 | case DT_PREINIT_ARRAYSZ: name = "PREINIT_ARRAYSZ"; break; | |
d48188b9 | 1680 | case DT_CHECKSUM: name = "CHECKSUM"; break; |
94558834 L |
1681 | case DT_PLTPADSZ: name = "PLTPADSZ"; break; |
1682 | case DT_MOVEENT: name = "MOVEENT"; break; | |
1683 | case DT_MOVESZ: name = "MOVESZ"; break; | |
1684 | case DT_FEATURE: name = "FEATURE"; break; | |
1685 | case DT_POSFLAG_1: name = "POSFLAG_1"; break; | |
1686 | case DT_SYMINSZ: name = "SYMINSZ"; break; | |
1687 | case DT_SYMINENT: name = "SYMINENT"; break; | |
b34976b6 AM |
1688 | case DT_CONFIG: name = "CONFIG"; stringp = TRUE; break; |
1689 | case DT_DEPAUDIT: name = "DEPAUDIT"; stringp = TRUE; break; | |
1690 | case DT_AUDIT: name = "AUDIT"; stringp = TRUE; break; | |
94558834 L |
1691 | case DT_PLTPAD: name = "PLTPAD"; break; |
1692 | case DT_MOVETAB: name = "MOVETAB"; break; | |
1693 | case DT_SYMINFO: name = "SYMINFO"; break; | |
1694 | case DT_RELACOUNT: name = "RELACOUNT"; break; | |
1695 | case DT_RELCOUNT: name = "RELCOUNT"; break; | |
1696 | case DT_FLAGS_1: name = "FLAGS_1"; break; | |
252b5132 RH |
1697 | case DT_VERSYM: name = "VERSYM"; break; |
1698 | case DT_VERDEF: name = "VERDEF"; break; | |
1699 | case DT_VERDEFNUM: name = "VERDEFNUM"; break; | |
1700 | case DT_VERNEED: name = "VERNEED"; break; | |
1701 | case DT_VERNEEDNUM: name = "VERNEEDNUM"; break; | |
b34976b6 | 1702 | case DT_AUXILIARY: name = "AUXILIARY"; stringp = TRUE; break; |
94558834 | 1703 | case DT_USED: name = "USED"; break; |
b34976b6 | 1704 | case DT_FILTER: name = "FILTER"; stringp = TRUE; break; |
fdc90cb4 | 1705 | case DT_GNU_HASH: name = "GNU_HASH"; break; |
252b5132 RH |
1706 | } |
1707 | ||
ad9563d6 | 1708 | fprintf (f, " %-20s ", name); |
252b5132 | 1709 | if (! stringp) |
a1f3c56e AN |
1710 | { |
1711 | fprintf (f, "0x"); | |
1712 | bfd_fprintf_vma (abfd, f, dyn.d_un.d_val); | |
1713 | } | |
252b5132 RH |
1714 | else |
1715 | { | |
1716 | const char *string; | |
dc810e39 | 1717 | unsigned int tagv = dyn.d_un.d_val; |
252b5132 | 1718 | |
dc810e39 | 1719 | string = bfd_elf_string_from_elf_section (abfd, shlink, tagv); |
252b5132 RH |
1720 | if (string == NULL) |
1721 | goto error_return; | |
1722 | fprintf (f, "%s", string); | |
1723 | } | |
1724 | fprintf (f, "\n"); | |
1725 | } | |
1726 | ||
1727 | free (dynbuf); | |
1728 | dynbuf = NULL; | |
1729 | } | |
1730 | ||
1731 | if ((elf_dynverdef (abfd) != 0 && elf_tdata (abfd)->verdef == NULL) | |
1732 | || (elf_dynverref (abfd) != 0 && elf_tdata (abfd)->verref == NULL)) | |
1733 | { | |
fc0e6df6 | 1734 | if (! _bfd_elf_slurp_version_tables (abfd, FALSE)) |
b34976b6 | 1735 | return FALSE; |
252b5132 RH |
1736 | } |
1737 | ||
1738 | if (elf_dynverdef (abfd) != 0) | |
1739 | { | |
1740 | Elf_Internal_Verdef *t; | |
1741 | ||
1742 | fprintf (f, _("\nVersion definitions:\n")); | |
1743 | for (t = elf_tdata (abfd)->verdef; t != NULL; t = t->vd_nextdef) | |
1744 | { | |
1745 | fprintf (f, "%d 0x%2.2x 0x%8.8lx %s\n", t->vd_ndx, | |
d0fb9a8d JJ |
1746 | t->vd_flags, t->vd_hash, |
1747 | t->vd_nodename ? t->vd_nodename : "<corrupt>"); | |
1748 | if (t->vd_auxptr != NULL && t->vd_auxptr->vda_nextptr != NULL) | |
252b5132 RH |
1749 | { |
1750 | Elf_Internal_Verdaux *a; | |
1751 | ||
1752 | fprintf (f, "\t"); | |
1753 | for (a = t->vd_auxptr->vda_nextptr; | |
1754 | a != NULL; | |
1755 | a = a->vda_nextptr) | |
d0fb9a8d JJ |
1756 | fprintf (f, "%s ", |
1757 | a->vda_nodename ? a->vda_nodename : "<corrupt>"); | |
252b5132 RH |
1758 | fprintf (f, "\n"); |
1759 | } | |
1760 | } | |
1761 | } | |
1762 | ||
1763 | if (elf_dynverref (abfd) != 0) | |
1764 | { | |
1765 | Elf_Internal_Verneed *t; | |
1766 | ||
1767 | fprintf (f, _("\nVersion References:\n")); | |
1768 | for (t = elf_tdata (abfd)->verref; t != NULL; t = t->vn_nextref) | |
1769 | { | |
1770 | Elf_Internal_Vernaux *a; | |
1771 | ||
d0fb9a8d JJ |
1772 | fprintf (f, _(" required from %s:\n"), |
1773 | t->vn_filename ? t->vn_filename : "<corrupt>"); | |
252b5132 RH |
1774 | for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) |
1775 | fprintf (f, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a->vna_hash, | |
d0fb9a8d JJ |
1776 | a->vna_flags, a->vna_other, |
1777 | a->vna_nodename ? a->vna_nodename : "<corrupt>"); | |
252b5132 RH |
1778 | } |
1779 | } | |
1780 | ||
b34976b6 | 1781 | return TRUE; |
252b5132 RH |
1782 | |
1783 | error_return: | |
1784 | if (dynbuf != NULL) | |
1785 | free (dynbuf); | |
b34976b6 | 1786 | return FALSE; |
252b5132 RH |
1787 | } |
1788 | ||
bb4d2ac2 L |
1789 | /* Get version string. */ |
1790 | ||
1791 | const char * | |
60bb06bc L |
1792 | _bfd_elf_get_symbol_version_string (bfd *abfd, asymbol *symbol, |
1793 | bfd_boolean *hidden) | |
bb4d2ac2 L |
1794 | { |
1795 | const char *version_string = NULL; | |
1796 | if (elf_dynversym (abfd) != 0 | |
1797 | && (elf_dynverdef (abfd) != 0 || elf_dynverref (abfd) != 0)) | |
1798 | { | |
1799 | unsigned int vernum = ((elf_symbol_type *) symbol)->version; | |
1800 | ||
1801 | *hidden = (vernum & VERSYM_HIDDEN) != 0; | |
1802 | vernum &= VERSYM_VERSION; | |
1803 | ||
1804 | if (vernum == 0) | |
1805 | version_string = ""; | |
1806 | else if (vernum == 1) | |
1807 | version_string = "Base"; | |
1808 | else if (vernum <= elf_tdata (abfd)->cverdefs) | |
1809 | version_string = | |
1810 | elf_tdata (abfd)->verdef[vernum - 1].vd_nodename; | |
1811 | else | |
1812 | { | |
1813 | Elf_Internal_Verneed *t; | |
1814 | ||
1815 | version_string = ""; | |
1816 | for (t = elf_tdata (abfd)->verref; | |
1817 | t != NULL; | |
1818 | t = t->vn_nextref) | |
1819 | { | |
1820 | Elf_Internal_Vernaux *a; | |
1821 | ||
1822 | for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) | |
1823 | { | |
1824 | if (a->vna_other == vernum) | |
1825 | { | |
1826 | version_string = a->vna_nodename; | |
1827 | break; | |
1828 | } | |
1829 | } | |
1830 | } | |
1831 | } | |
1832 | } | |
1833 | return version_string; | |
1834 | } | |
1835 | ||
252b5132 RH |
1836 | /* Display ELF-specific fields of a symbol. */ |
1837 | ||
1838 | void | |
217aa764 AM |
1839 | bfd_elf_print_symbol (bfd *abfd, |
1840 | void *filep, | |
1841 | asymbol *symbol, | |
1842 | bfd_print_symbol_type how) | |
252b5132 | 1843 | { |
a50b1753 | 1844 | FILE *file = (FILE *) filep; |
252b5132 RH |
1845 | switch (how) |
1846 | { | |
1847 | case bfd_print_symbol_name: | |
1848 | fprintf (file, "%s", symbol->name); | |
1849 | break; | |
1850 | case bfd_print_symbol_more: | |
1851 | fprintf (file, "elf "); | |
60b89a18 | 1852 | bfd_fprintf_vma (abfd, file, symbol->value); |
0af1713e | 1853 | fprintf (file, " %lx", (unsigned long) symbol->flags); |
252b5132 RH |
1854 | break; |
1855 | case bfd_print_symbol_all: | |
1856 | { | |
4e8a9624 AM |
1857 | const char *section_name; |
1858 | const char *name = NULL; | |
9c5bfbb7 | 1859 | const struct elf_backend_data *bed; |
7a13edea | 1860 | unsigned char st_other; |
dbb410c3 | 1861 | bfd_vma val; |
bb4d2ac2 L |
1862 | const char *version_string; |
1863 | bfd_boolean hidden; | |
c044fabd | 1864 | |
252b5132 | 1865 | section_name = symbol->section ? symbol->section->name : "(*none*)"; |
587ff49e RH |
1866 | |
1867 | bed = get_elf_backend_data (abfd); | |
1868 | if (bed->elf_backend_print_symbol_all) | |
c044fabd | 1869 | name = (*bed->elf_backend_print_symbol_all) (abfd, filep, symbol); |
587ff49e RH |
1870 | |
1871 | if (name == NULL) | |
1872 | { | |
7ee38065 | 1873 | name = symbol->name; |
217aa764 | 1874 | bfd_print_symbol_vandf (abfd, file, symbol); |
587ff49e RH |
1875 | } |
1876 | ||
252b5132 RH |
1877 | fprintf (file, " %s\t", section_name); |
1878 | /* Print the "other" value for a symbol. For common symbols, | |
1879 | we've already printed the size; now print the alignment. | |
1880 | For other symbols, we have no specified alignment, and | |
1881 | we've printed the address; now print the size. */ | |
dcf6c779 | 1882 | if (symbol->section && bfd_is_com_section (symbol->section)) |
dbb410c3 AM |
1883 | val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_value; |
1884 | else | |
1885 | val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_size; | |
1886 | bfd_fprintf_vma (abfd, file, val); | |
252b5132 RH |
1887 | |
1888 | /* If we have version information, print it. */ | |
60bb06bc L |
1889 | version_string = _bfd_elf_get_symbol_version_string (abfd, |
1890 | symbol, | |
1891 | &hidden); | |
bb4d2ac2 | 1892 | if (version_string) |
252b5132 | 1893 | { |
bb4d2ac2 | 1894 | if (!hidden) |
252b5132 RH |
1895 | fprintf (file, " %-11s", version_string); |
1896 | else | |
1897 | { | |
1898 | int i; | |
1899 | ||
1900 | fprintf (file, " (%s)", version_string); | |
1901 | for (i = 10 - strlen (version_string); i > 0; --i) | |
1902 | putc (' ', file); | |
1903 | } | |
1904 | } | |
1905 | ||
1906 | /* If the st_other field is not zero, print it. */ | |
7a13edea | 1907 | st_other = ((elf_symbol_type *) symbol)->internal_elf_sym.st_other; |
c044fabd | 1908 | |
7a13edea NC |
1909 | switch (st_other) |
1910 | { | |
1911 | case 0: break; | |
1912 | case STV_INTERNAL: fprintf (file, " .internal"); break; | |
1913 | case STV_HIDDEN: fprintf (file, " .hidden"); break; | |
1914 | case STV_PROTECTED: fprintf (file, " .protected"); break; | |
1915 | default: | |
1916 | /* Some other non-defined flags are also present, so print | |
1917 | everything hex. */ | |
1918 | fprintf (file, " 0x%02x", (unsigned int) st_other); | |
1919 | } | |
252b5132 | 1920 | |
587ff49e | 1921 | fprintf (file, " %s", name); |
252b5132 RH |
1922 | } |
1923 | break; | |
1924 | } | |
1925 | } | |
252b5132 RH |
1926 | \f |
1927 | /* ELF .o/exec file reading */ | |
1928 | ||
c044fabd | 1929 | /* Create a new bfd section from an ELF section header. */ |
252b5132 | 1930 | |
b34976b6 | 1931 | bfd_boolean |
217aa764 | 1932 | bfd_section_from_shdr (bfd *abfd, unsigned int shindex) |
252b5132 | 1933 | { |
4fbb74a6 AM |
1934 | Elf_Internal_Shdr *hdr; |
1935 | Elf_Internal_Ehdr *ehdr; | |
1936 | const struct elf_backend_data *bed; | |
90937f86 | 1937 | const char *name; |
bf67003b NC |
1938 | bfd_boolean ret = TRUE; |
1939 | static bfd_boolean * sections_being_created = NULL; | |
5a4b0ccc | 1940 | static bfd * sections_being_created_abfd = NULL; |
bf67003b | 1941 | static unsigned int nesting = 0; |
252b5132 | 1942 | |
4fbb74a6 AM |
1943 | if (shindex >= elf_numsections (abfd)) |
1944 | return FALSE; | |
1945 | ||
bf67003b NC |
1946 | if (++ nesting > 3) |
1947 | { | |
1948 | /* PR17512: A corrupt ELF binary might contain a recursive group of | |
06614111 | 1949 | sections, with each the string indicies pointing to the next in the |
bf67003b NC |
1950 | loop. Detect this here, by refusing to load a section that we are |
1951 | already in the process of loading. We only trigger this test if | |
1952 | we have nested at least three sections deep as normal ELF binaries | |
5a4b0ccc NC |
1953 | can expect to recurse at least once. |
1954 | ||
1955 | FIXME: It would be better if this array was attached to the bfd, | |
1956 | rather than being held in a static pointer. */ | |
1957 | ||
1958 | if (sections_being_created_abfd != abfd) | |
1959 | sections_being_created = NULL; | |
bf67003b NC |
1960 | if (sections_being_created == NULL) |
1961 | { | |
1962 | /* FIXME: It would be more efficient to attach this array to the bfd somehow. */ | |
1963 | sections_being_created = (bfd_boolean *) | |
1964 | bfd_zalloc (abfd, elf_numsections (abfd) * sizeof (bfd_boolean)); | |
5a4b0ccc | 1965 | sections_being_created_abfd = abfd; |
bf67003b NC |
1966 | } |
1967 | if (sections_being_created [shindex]) | |
1968 | { | |
4eca0228 | 1969 | _bfd_error_handler |
bf67003b NC |
1970 | (_("%B: warning: loop in section dependencies detected"), abfd); |
1971 | return FALSE; | |
1972 | } | |
1973 | sections_being_created [shindex] = TRUE; | |
1974 | } | |
1975 | ||
4fbb74a6 AM |
1976 | hdr = elf_elfsections (abfd)[shindex]; |
1977 | ehdr = elf_elfheader (abfd); | |
1978 | name = bfd_elf_string_from_elf_section (abfd, ehdr->e_shstrndx, | |
1b3a8575 | 1979 | hdr->sh_name); |
933d961a | 1980 | if (name == NULL) |
bf67003b | 1981 | goto fail; |
252b5132 | 1982 | |
4fbb74a6 | 1983 | bed = get_elf_backend_data (abfd); |
252b5132 RH |
1984 | switch (hdr->sh_type) |
1985 | { | |
1986 | case SHT_NULL: | |
1987 | /* Inactive section. Throw it away. */ | |
bf67003b | 1988 | goto success; |
252b5132 | 1989 | |
bf67003b NC |
1990 | case SHT_PROGBITS: /* Normal section with contents. */ |
1991 | case SHT_NOBITS: /* .bss section. */ | |
1992 | case SHT_HASH: /* .hash section. */ | |
1993 | case SHT_NOTE: /* .note section. */ | |
25e27870 L |
1994 | case SHT_INIT_ARRAY: /* .init_array section. */ |
1995 | case SHT_FINI_ARRAY: /* .fini_array section. */ | |
1996 | case SHT_PREINIT_ARRAY: /* .preinit_array section. */ | |
7f1204bb | 1997 | case SHT_GNU_LIBLIST: /* .gnu.liblist section. */ |
fdc90cb4 | 1998 | case SHT_GNU_HASH: /* .gnu.hash section. */ |
bf67003b NC |
1999 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); |
2000 | goto success; | |
252b5132 | 2001 | |
797fc050 | 2002 | case SHT_DYNAMIC: /* Dynamic linking information. */ |
6dc132d9 | 2003 | if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex)) |
bf67003b NC |
2004 | goto fail; |
2005 | ||
cfcac11d NC |
2006 | if (hdr->sh_link > elf_numsections (abfd)) |
2007 | { | |
caa83f8b | 2008 | /* PR 10478: Accept Solaris binaries with a sh_link |
cfcac11d NC |
2009 | field set to SHN_BEFORE or SHN_AFTER. */ |
2010 | switch (bfd_get_arch (abfd)) | |
2011 | { | |
caa83f8b | 2012 | case bfd_arch_i386: |
cfcac11d NC |
2013 | case bfd_arch_sparc: |
2014 | if (hdr->sh_link == (SHN_LORESERVE & 0xffff) /* SHN_BEFORE */ | |
2015 | || hdr->sh_link == ((SHN_LORESERVE + 1) & 0xffff) /* SHN_AFTER */) | |
2016 | break; | |
2017 | /* Otherwise fall through. */ | |
2018 | default: | |
bf67003b | 2019 | goto fail; |
cfcac11d NC |
2020 | } |
2021 | } | |
2022 | else if (elf_elfsections (abfd)[hdr->sh_link] == NULL) | |
bf67003b | 2023 | goto fail; |
cfcac11d | 2024 | else if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_STRTAB) |
797fc050 AM |
2025 | { |
2026 | Elf_Internal_Shdr *dynsymhdr; | |
2027 | ||
2028 | /* The shared libraries distributed with hpux11 have a bogus | |
2029 | sh_link field for the ".dynamic" section. Find the | |
2030 | string table for the ".dynsym" section instead. */ | |
2031 | if (elf_dynsymtab (abfd) != 0) | |
2032 | { | |
2033 | dynsymhdr = elf_elfsections (abfd)[elf_dynsymtab (abfd)]; | |
2034 | hdr->sh_link = dynsymhdr->sh_link; | |
2035 | } | |
2036 | else | |
2037 | { | |
2038 | unsigned int i, num_sec; | |
2039 | ||
2040 | num_sec = elf_numsections (abfd); | |
2041 | for (i = 1; i < num_sec; i++) | |
2042 | { | |
2043 | dynsymhdr = elf_elfsections (abfd)[i]; | |
2044 | if (dynsymhdr->sh_type == SHT_DYNSYM) | |
2045 | { | |
2046 | hdr->sh_link = dynsymhdr->sh_link; | |
2047 | break; | |
2048 | } | |
2049 | } | |
2050 | } | |
2051 | } | |
bf67003b | 2052 | goto success; |
797fc050 | 2053 | |
bf67003b | 2054 | case SHT_SYMTAB: /* A symbol table. */ |
252b5132 | 2055 | if (elf_onesymtab (abfd) == shindex) |
bf67003b | 2056 | goto success; |
252b5132 | 2057 | |
a50b2160 | 2058 | if (hdr->sh_entsize != bed->s->sizeof_sym) |
bf67003b NC |
2059 | goto fail; |
2060 | ||
3337c1e5 | 2061 | if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size) |
eee3b786 AM |
2062 | { |
2063 | if (hdr->sh_size != 0) | |
bf67003b | 2064 | goto fail; |
eee3b786 AM |
2065 | /* Some assemblers erroneously set sh_info to one with a |
2066 | zero sh_size. ld sees this as a global symbol count | |
2067 | of (unsigned) -1. Fix it here. */ | |
2068 | hdr->sh_info = 0; | |
bf67003b | 2069 | goto success; |
eee3b786 | 2070 | } |
bf67003b | 2071 | |
16ad13ec NC |
2072 | /* PR 18854: A binary might contain more than one symbol table. |
2073 | Unusual, but possible. Warn, but continue. */ | |
2074 | if (elf_onesymtab (abfd) != 0) | |
2075 | { | |
4eca0228 | 2076 | _bfd_error_handler |
695344c0 | 2077 | /* xgettext:c-format */ |
16ad13ec NC |
2078 | (_("%B: warning: multiple symbol tables detected - ignoring the table in section %u"), |
2079 | abfd, shindex); | |
2080 | goto success; | |
2081 | } | |
252b5132 | 2082 | elf_onesymtab (abfd) = shindex; |
6a40cf0c NC |
2083 | elf_symtab_hdr (abfd) = *hdr; |
2084 | elf_elfsections (abfd)[shindex] = hdr = & elf_symtab_hdr (abfd); | |
252b5132 RH |
2085 | abfd->flags |= HAS_SYMS; |
2086 | ||
2087 | /* Sometimes a shared object will map in the symbol table. If | |
08a40648 AM |
2088 | SHF_ALLOC is set, and this is a shared object, then we also |
2089 | treat this section as a BFD section. We can not base the | |
2090 | decision purely on SHF_ALLOC, because that flag is sometimes | |
2091 | set in a relocatable object file, which would confuse the | |
2092 | linker. */ | |
252b5132 RH |
2093 | if ((hdr->sh_flags & SHF_ALLOC) != 0 |
2094 | && (abfd->flags & DYNAMIC) != 0 | |
6dc132d9 L |
2095 | && ! _bfd_elf_make_section_from_shdr (abfd, hdr, name, |
2096 | shindex)) | |
bf67003b | 2097 | goto fail; |
252b5132 | 2098 | |
1b3a8575 AM |
2099 | /* Go looking for SHT_SYMTAB_SHNDX too, since if there is one we |
2100 | can't read symbols without that section loaded as well. It | |
2101 | is most likely specified by the next section header. */ | |
6a40cf0c NC |
2102 | { |
2103 | elf_section_list * entry; | |
2104 | unsigned int i, num_sec; | |
1b3a8575 | 2105 | |
6a40cf0c NC |
2106 | for (entry = elf_symtab_shndx_list (abfd); entry != NULL; entry = entry->next) |
2107 | if (entry->hdr.sh_link == shindex) | |
2108 | goto success; | |
2109 | ||
2110 | num_sec = elf_numsections (abfd); | |
2111 | for (i = shindex + 1; i < num_sec; i++) | |
2112 | { | |
2113 | Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i]; | |
2114 | ||
2115 | if (hdr2->sh_type == SHT_SYMTAB_SHNDX | |
2116 | && hdr2->sh_link == shindex) | |
2117 | break; | |
2118 | } | |
2119 | ||
2120 | if (i == num_sec) | |
2121 | for (i = 1; i < shindex; i++) | |
1b3a8575 AM |
2122 | { |
2123 | Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i]; | |
6a40cf0c | 2124 | |
1b3a8575 AM |
2125 | if (hdr2->sh_type == SHT_SYMTAB_SHNDX |
2126 | && hdr2->sh_link == shindex) | |
2127 | break; | |
2128 | } | |
6a40cf0c NC |
2129 | |
2130 | if (i != shindex) | |
2131 | ret = bfd_section_from_shdr (abfd, i); | |
2132 | /* else FIXME: we have failed to find the symbol table - should we issue an error ? */ | |
2133 | goto success; | |
2134 | } | |
252b5132 | 2135 | |
bf67003b | 2136 | case SHT_DYNSYM: /* A dynamic symbol table. */ |
252b5132 | 2137 | if (elf_dynsymtab (abfd) == shindex) |
bf67003b | 2138 | goto success; |
252b5132 | 2139 | |
a50b2160 | 2140 | if (hdr->sh_entsize != bed->s->sizeof_sym) |
bf67003b NC |
2141 | goto fail; |
2142 | ||
eee3b786 AM |
2143 | if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size) |
2144 | { | |
2145 | if (hdr->sh_size != 0) | |
bf67003b NC |
2146 | goto fail; |
2147 | ||
eee3b786 AM |
2148 | /* Some linkers erroneously set sh_info to one with a |
2149 | zero sh_size. ld sees this as a global symbol count | |
2150 | of (unsigned) -1. Fix it here. */ | |
2151 | hdr->sh_info = 0; | |
bf67003b | 2152 | goto success; |
eee3b786 | 2153 | } |
bf67003b | 2154 | |
16ad13ec NC |
2155 | /* PR 18854: A binary might contain more than one dynamic symbol table. |
2156 | Unusual, but possible. Warn, but continue. */ | |
2157 | if (elf_dynsymtab (abfd) != 0) | |
2158 | { | |
4eca0228 | 2159 | _bfd_error_handler |
695344c0 | 2160 | /* xgettext:c-format */ |
16ad13ec NC |
2161 | (_("%B: warning: multiple dynamic symbol tables detected - ignoring the table in section %u"), |
2162 | abfd, shindex); | |
2163 | goto success; | |
2164 | } | |
252b5132 RH |
2165 | elf_dynsymtab (abfd) = shindex; |
2166 | elf_tdata (abfd)->dynsymtab_hdr = *hdr; | |
2167 | elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->dynsymtab_hdr; | |
2168 | abfd->flags |= HAS_SYMS; | |
2169 | ||
2170 | /* Besides being a symbol table, we also treat this as a regular | |
2171 | section, so that objcopy can handle it. */ | |
bf67003b NC |
2172 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); |
2173 | goto success; | |
252b5132 | 2174 | |
bf67003b | 2175 | case SHT_SYMTAB_SHNDX: /* Symbol section indices when >64k sections. */ |
6a40cf0c NC |
2176 | { |
2177 | elf_section_list * entry; | |
9ad5cbcf | 2178 | |
6a40cf0c NC |
2179 | for (entry = elf_symtab_shndx_list (abfd); entry != NULL; entry = entry->next) |
2180 | if (entry->ndx == shindex) | |
2181 | goto success; | |
2182 | ||
2183 | entry = bfd_alloc (abfd, sizeof * entry); | |
2184 | if (entry == NULL) | |
2185 | goto fail; | |
2186 | entry->ndx = shindex; | |
2187 | entry->hdr = * hdr; | |
2188 | entry->next = elf_symtab_shndx_list (abfd); | |
2189 | elf_symtab_shndx_list (abfd) = entry; | |
2190 | elf_elfsections (abfd)[shindex] = & entry->hdr; | |
2191 | goto success; | |
2192 | } | |
9ad5cbcf | 2193 | |
bf67003b | 2194 | case SHT_STRTAB: /* A string table. */ |
252b5132 | 2195 | if (hdr->bfd_section != NULL) |
bf67003b NC |
2196 | goto success; |
2197 | ||
252b5132 RH |
2198 | if (ehdr->e_shstrndx == shindex) |
2199 | { | |
2200 | elf_tdata (abfd)->shstrtab_hdr = *hdr; | |
2201 | elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->shstrtab_hdr; | |
bf67003b | 2202 | goto success; |
252b5132 | 2203 | } |
bf67003b | 2204 | |
1b3a8575 AM |
2205 | if (elf_elfsections (abfd)[elf_onesymtab (abfd)]->sh_link == shindex) |
2206 | { | |
2207 | symtab_strtab: | |
2208 | elf_tdata (abfd)->strtab_hdr = *hdr; | |
2209 | elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->strtab_hdr; | |
bf67003b | 2210 | goto success; |
1b3a8575 | 2211 | } |
bf67003b | 2212 | |
1b3a8575 AM |
2213 | if (elf_elfsections (abfd)[elf_dynsymtab (abfd)]->sh_link == shindex) |
2214 | { | |
2215 | dynsymtab_strtab: | |
2216 | elf_tdata (abfd)->dynstrtab_hdr = *hdr; | |
2217 | hdr = &elf_tdata (abfd)->dynstrtab_hdr; | |
2218 | elf_elfsections (abfd)[shindex] = hdr; | |
2219 | /* We also treat this as a regular section, so that objcopy | |
2220 | can handle it. */ | |
bf67003b NC |
2221 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, |
2222 | shindex); | |
2223 | goto success; | |
1b3a8575 | 2224 | } |
252b5132 | 2225 | |
1b3a8575 AM |
2226 | /* If the string table isn't one of the above, then treat it as a |
2227 | regular section. We need to scan all the headers to be sure, | |
2228 | just in case this strtab section appeared before the above. */ | |
2229 | if (elf_onesymtab (abfd) == 0 || elf_dynsymtab (abfd) == 0) | |
2230 | { | |
2231 | unsigned int i, num_sec; | |
252b5132 | 2232 | |
1b3a8575 AM |
2233 | num_sec = elf_numsections (abfd); |
2234 | for (i = 1; i < num_sec; i++) | |
2235 | { | |
2236 | Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i]; | |
2237 | if (hdr2->sh_link == shindex) | |
2238 | { | |
933d961a JJ |
2239 | /* Prevent endless recursion on broken objects. */ |
2240 | if (i == shindex) | |
bf67003b | 2241 | goto fail; |
1b3a8575 | 2242 | if (! bfd_section_from_shdr (abfd, i)) |
bf67003b | 2243 | goto fail; |
1b3a8575 AM |
2244 | if (elf_onesymtab (abfd) == i) |
2245 | goto symtab_strtab; | |
2246 | if (elf_dynsymtab (abfd) == i) | |
2247 | goto dynsymtab_strtab; | |
2248 | } | |
2249 | } | |
2250 | } | |
bf67003b NC |
2251 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); |
2252 | goto success; | |
252b5132 RH |
2253 | |
2254 | case SHT_REL: | |
2255 | case SHT_RELA: | |
2256 | /* *These* do a lot of work -- but build no sections! */ | |
2257 | { | |
2258 | asection *target_sect; | |
d4730f92 | 2259 | Elf_Internal_Shdr *hdr2, **p_hdr; |
9ad5cbcf | 2260 | unsigned int num_sec = elf_numsections (abfd); |
d4730f92 | 2261 | struct bfd_elf_section_data *esdt; |
252b5132 | 2262 | |
aa2ca951 JJ |
2263 | if (hdr->sh_entsize |
2264 | != (bfd_size_type) (hdr->sh_type == SHT_REL | |
a50b2160 | 2265 | ? bed->s->sizeof_rel : bed->s->sizeof_rela)) |
bf67003b | 2266 | goto fail; |
a50b2160 | 2267 | |
03ae5f59 | 2268 | /* Check for a bogus link to avoid crashing. */ |
4fbb74a6 | 2269 | if (hdr->sh_link >= num_sec) |
03ae5f59 | 2270 | { |
4eca0228 | 2271 | _bfd_error_handler |
695344c0 | 2272 | /* xgettext:c-format */ |
4eca0228 AM |
2273 | (_("%B: invalid link %lu for reloc section %s (index %u)"), |
2274 | abfd, hdr->sh_link, name, shindex); | |
bf67003b NC |
2275 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, |
2276 | shindex); | |
2277 | goto success; | |
03ae5f59 ILT |
2278 | } |
2279 | ||
252b5132 RH |
2280 | /* For some incomprehensible reason Oracle distributes |
2281 | libraries for Solaris in which some of the objects have | |
2282 | bogus sh_link fields. It would be nice if we could just | |
2283 | reject them, but, unfortunately, some people need to use | |
2284 | them. We scan through the section headers; if we find only | |
2285 | one suitable symbol table, we clobber the sh_link to point | |
83b89087 L |
2286 | to it. I hope this doesn't break anything. |
2287 | ||
2288 | Don't do it on executable nor shared library. */ | |
2289 | if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0 | |
2290 | && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_SYMTAB | |
252b5132 RH |
2291 | && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_DYNSYM) |
2292 | { | |
9ad5cbcf | 2293 | unsigned int scan; |
252b5132 RH |
2294 | int found; |
2295 | ||
2296 | found = 0; | |
9ad5cbcf | 2297 | for (scan = 1; scan < num_sec; scan++) |
252b5132 RH |
2298 | { |
2299 | if (elf_elfsections (abfd)[scan]->sh_type == SHT_SYMTAB | |
2300 | || elf_elfsections (abfd)[scan]->sh_type == SHT_DYNSYM) | |
2301 | { | |
2302 | if (found != 0) | |
2303 | { | |
2304 | found = 0; | |
2305 | break; | |
2306 | } | |
2307 | found = scan; | |
2308 | } | |
2309 | } | |
2310 | if (found != 0) | |
2311 | hdr->sh_link = found; | |
2312 | } | |
2313 | ||
2314 | /* Get the symbol table. */ | |
1b3a8575 AM |
2315 | if ((elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_SYMTAB |
2316 | || elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_DYNSYM) | |
252b5132 | 2317 | && ! bfd_section_from_shdr (abfd, hdr->sh_link)) |
bf67003b | 2318 | goto fail; |
252b5132 RH |
2319 | |
2320 | /* If this reloc section does not use the main symbol table we | |
2321 | don't treat it as a reloc section. BFD can't adequately | |
2322 | represent such a section, so at least for now, we don't | |
c044fabd | 2323 | try. We just present it as a normal section. We also |
60bcf0fa | 2324 | can't use it as a reloc section if it points to the null |
83b89087 L |
2325 | section, an invalid section, another reloc section, or its |
2326 | sh_link points to the null section. */ | |
185ef66d | 2327 | if (hdr->sh_link != elf_onesymtab (abfd) |
83b89087 | 2328 | || hdr->sh_link == SHN_UNDEF |
185ef66d | 2329 | || hdr->sh_info == SHN_UNDEF |
185ef66d AM |
2330 | || hdr->sh_info >= num_sec |
2331 | || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_REL | |
2332 | || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_RELA) | |
bf67003b NC |
2333 | { |
2334 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, | |
2335 | shindex); | |
2336 | goto success; | |
2337 | } | |
252b5132 RH |
2338 | |
2339 | if (! bfd_section_from_shdr (abfd, hdr->sh_info)) | |
bf67003b NC |
2340 | goto fail; |
2341 | ||
252b5132 RH |
2342 | target_sect = bfd_section_from_elf_index (abfd, hdr->sh_info); |
2343 | if (target_sect == NULL) | |
bf67003b | 2344 | goto fail; |
252b5132 | 2345 | |
d4730f92 BS |
2346 | esdt = elf_section_data (target_sect); |
2347 | if (hdr->sh_type == SHT_RELA) | |
2348 | p_hdr = &esdt->rela.hdr; | |
252b5132 | 2349 | else |
d4730f92 BS |
2350 | p_hdr = &esdt->rel.hdr; |
2351 | ||
06614111 NC |
2352 | /* PR 17512: file: 0b4f81b7. */ |
2353 | if (*p_hdr != NULL) | |
2354 | goto fail; | |
ef53be89 | 2355 | hdr2 = (Elf_Internal_Shdr *) bfd_alloc (abfd, sizeof (*hdr2)); |
d4730f92 | 2356 | if (hdr2 == NULL) |
bf67003b | 2357 | goto fail; |
252b5132 | 2358 | *hdr2 = *hdr; |
d4730f92 | 2359 | *p_hdr = hdr2; |
252b5132 | 2360 | elf_elfsections (abfd)[shindex] = hdr2; |
d9bc7a44 | 2361 | target_sect->reloc_count += NUM_SHDR_ENTRIES (hdr); |
252b5132 RH |
2362 | target_sect->flags |= SEC_RELOC; |
2363 | target_sect->relocation = NULL; | |
2364 | target_sect->rel_filepos = hdr->sh_offset; | |
bf572ba0 MM |
2365 | /* In the section to which the relocations apply, mark whether |
2366 | its relocations are of the REL or RELA variety. */ | |
72730e0c | 2367 | if (hdr->sh_size != 0) |
d4730f92 BS |
2368 | { |
2369 | if (hdr->sh_type == SHT_RELA) | |
2370 | target_sect->use_rela_p = 1; | |
2371 | } | |
252b5132 | 2372 | abfd->flags |= HAS_RELOC; |
bf67003b | 2373 | goto success; |
252b5132 | 2374 | } |
252b5132 RH |
2375 | |
2376 | case SHT_GNU_verdef: | |
2377 | elf_dynverdef (abfd) = shindex; | |
2378 | elf_tdata (abfd)->dynverdef_hdr = *hdr; | |
bf67003b NC |
2379 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); |
2380 | goto success; | |
252b5132 RH |
2381 | |
2382 | case SHT_GNU_versym: | |
a50b2160 | 2383 | if (hdr->sh_entsize != sizeof (Elf_External_Versym)) |
bf67003b NC |
2384 | goto fail; |
2385 | ||
252b5132 RH |
2386 | elf_dynversym (abfd) = shindex; |
2387 | elf_tdata (abfd)->dynversym_hdr = *hdr; | |
bf67003b NC |
2388 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); |
2389 | goto success; | |
252b5132 RH |
2390 | |
2391 | case SHT_GNU_verneed: | |
2392 | elf_dynverref (abfd) = shindex; | |
2393 | elf_tdata (abfd)->dynverref_hdr = *hdr; | |
bf67003b NC |
2394 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); |
2395 | goto success; | |
252b5132 RH |
2396 | |
2397 | case SHT_SHLIB: | |
bf67003b | 2398 | goto success; |
252b5132 | 2399 | |
dbb410c3 | 2400 | case SHT_GROUP: |
44534af3 | 2401 | if (! IS_VALID_GROUP_SECTION_HEADER (hdr, GRP_ENTRY_SIZE)) |
bf67003b NC |
2402 | goto fail; |
2403 | ||
6dc132d9 | 2404 | if (!_bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex)) |
bf67003b NC |
2405 | goto fail; |
2406 | ||
dbb410c3 AM |
2407 | if (hdr->contents != NULL) |
2408 | { | |
2409 | Elf_Internal_Group *idx = (Elf_Internal_Group *) hdr->contents; | |
06614111 | 2410 | unsigned int n_elt = hdr->sh_size / sizeof (* idx); |
dbb410c3 AM |
2411 | asection *s; |
2412 | ||
06614111 NC |
2413 | if (n_elt == 0) |
2414 | goto fail; | |
b885599b AM |
2415 | if (idx->flags & GRP_COMDAT) |
2416 | hdr->bfd_section->flags | |
2417 | |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD; | |
2418 | ||
45c5e9ed L |
2419 | /* We try to keep the same section order as it comes in. */ |
2420 | idx += n_elt; | |
06614111 | 2421 | |
dbb410c3 | 2422 | while (--n_elt != 0) |
1783205a NC |
2423 | { |
2424 | --idx; | |
2425 | ||
2426 | if (idx->shdr != NULL | |
2427 | && (s = idx->shdr->bfd_section) != NULL | |
2428 | && elf_next_in_group (s) != NULL) | |
2429 | { | |
2430 | elf_next_in_group (hdr->bfd_section) = s; | |
2431 | break; | |
2432 | } | |
2433 | } | |
dbb410c3 | 2434 | } |
bf67003b | 2435 | goto success; |
dbb410c3 | 2436 | |
252b5132 | 2437 | default: |
104d59d1 JM |
2438 | /* Possibly an attributes section. */ |
2439 | if (hdr->sh_type == SHT_GNU_ATTRIBUTES | |
2440 | || hdr->sh_type == bed->obj_attrs_section_type) | |
2441 | { | |
2442 | if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex)) | |
bf67003b | 2443 | goto fail; |
104d59d1 | 2444 | _bfd_elf_parse_attributes (abfd, hdr); |
bf67003b | 2445 | goto success; |
104d59d1 JM |
2446 | } |
2447 | ||
252b5132 | 2448 | /* Check for any processor-specific section types. */ |
3eb70a79 | 2449 | if (bed->elf_backend_section_from_shdr (abfd, hdr, name, shindex)) |
bf67003b | 2450 | goto success; |
3eb70a79 L |
2451 | |
2452 | if (hdr->sh_type >= SHT_LOUSER && hdr->sh_type <= SHT_HIUSER) | |
2453 | { | |
2454 | if ((hdr->sh_flags & SHF_ALLOC) != 0) | |
2455 | /* FIXME: How to properly handle allocated section reserved | |
2456 | for applications? */ | |
4eca0228 | 2457 | _bfd_error_handler |
695344c0 | 2458 | /* xgettext:c-format */ |
3eb70a79 L |
2459 | (_("%B: don't know how to handle allocated, application " |
2460 | "specific section `%s' [0x%8x]"), | |
2461 | abfd, name, hdr->sh_type); | |
2462 | else | |
bf67003b NC |
2463 | { |
2464 | /* Allow sections reserved for applications. */ | |
2465 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, | |
2466 | shindex); | |
2467 | goto success; | |
2468 | } | |
3eb70a79 L |
2469 | } |
2470 | else if (hdr->sh_type >= SHT_LOPROC | |
2471 | && hdr->sh_type <= SHT_HIPROC) | |
2472 | /* FIXME: We should handle this section. */ | |
4eca0228 | 2473 | _bfd_error_handler |
695344c0 | 2474 | /* xgettext:c-format */ |
3eb70a79 L |
2475 | (_("%B: don't know how to handle processor specific section " |
2476 | "`%s' [0x%8x]"), | |
2477 | abfd, name, hdr->sh_type); | |
2478 | else if (hdr->sh_type >= SHT_LOOS && hdr->sh_type <= SHT_HIOS) | |
ff15b240 NC |
2479 | { |
2480 | /* Unrecognised OS-specific sections. */ | |
2481 | if ((hdr->sh_flags & SHF_OS_NONCONFORMING) != 0) | |
2482 | /* SHF_OS_NONCONFORMING indicates that special knowledge is | |
08a40648 | 2483 | required to correctly process the section and the file should |
ff15b240 | 2484 | be rejected with an error message. */ |
4eca0228 | 2485 | _bfd_error_handler |
695344c0 | 2486 | /* xgettext:c-format */ |
ff15b240 NC |
2487 | (_("%B: don't know how to handle OS specific section " |
2488 | "`%s' [0x%8x]"), | |
2489 | abfd, name, hdr->sh_type); | |
2490 | else | |
bf67003b NC |
2491 | { |
2492 | /* Otherwise it should be processed. */ | |
2493 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); | |
2494 | goto success; | |
2495 | } | |
ff15b240 | 2496 | } |
3eb70a79 L |
2497 | else |
2498 | /* FIXME: We should handle this section. */ | |
4eca0228 | 2499 | _bfd_error_handler |
695344c0 | 2500 | /* xgettext:c-format */ |
3eb70a79 L |
2501 | (_("%B: don't know how to handle section `%s' [0x%8x]"), |
2502 | abfd, name, hdr->sh_type); | |
2503 | ||
bf67003b | 2504 | goto fail; |
252b5132 RH |
2505 | } |
2506 | ||
bf67003b NC |
2507 | fail: |
2508 | ret = FALSE; | |
2509 | success: | |
e5b470e2 | 2510 | if (sections_being_created && sections_being_created_abfd == abfd) |
bf67003b NC |
2511 | sections_being_created [shindex] = FALSE; |
2512 | if (-- nesting == 0) | |
5a4b0ccc NC |
2513 | { |
2514 | sections_being_created = NULL; | |
2515 | sections_being_created_abfd = abfd; | |
2516 | } | |
bf67003b | 2517 | return ret; |
252b5132 RH |
2518 | } |
2519 | ||
87d72d41 | 2520 | /* Return the local symbol specified by ABFD, R_SYMNDX. */ |
ec338859 | 2521 | |
87d72d41 AM |
2522 | Elf_Internal_Sym * |
2523 | bfd_sym_from_r_symndx (struct sym_cache *cache, | |
2524 | bfd *abfd, | |
2525 | unsigned long r_symndx) | |
ec338859 | 2526 | { |
ec338859 AM |
2527 | unsigned int ent = r_symndx % LOCAL_SYM_CACHE_SIZE; |
2528 | ||
a5d1b3b5 AM |
2529 | if (cache->abfd != abfd || cache->indx[ent] != r_symndx) |
2530 | { | |
2531 | Elf_Internal_Shdr *symtab_hdr; | |
2532 | unsigned char esym[sizeof (Elf64_External_Sym)]; | |
2533 | Elf_External_Sym_Shndx eshndx; | |
ec338859 | 2534 | |
a5d1b3b5 AM |
2535 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; |
2536 | if (bfd_elf_get_elf_syms (abfd, symtab_hdr, 1, r_symndx, | |
87d72d41 | 2537 | &cache->sym[ent], esym, &eshndx) == NULL) |
a5d1b3b5 | 2538 | return NULL; |
9ad5cbcf | 2539 | |
a5d1b3b5 AM |
2540 | if (cache->abfd != abfd) |
2541 | { | |
2542 | memset (cache->indx, -1, sizeof (cache->indx)); | |
2543 | cache->abfd = abfd; | |
2544 | } | |
2545 | cache->indx[ent] = r_symndx; | |
ec338859 | 2546 | } |
a5d1b3b5 | 2547 | |
87d72d41 | 2548 | return &cache->sym[ent]; |
ec338859 AM |
2549 | } |
2550 | ||
252b5132 RH |
2551 | /* Given an ELF section number, retrieve the corresponding BFD |
2552 | section. */ | |
2553 | ||
2554 | asection * | |
91d6fa6a | 2555 | bfd_section_from_elf_index (bfd *abfd, unsigned int sec_index) |
252b5132 | 2556 | { |
91d6fa6a | 2557 | if (sec_index >= elf_numsections (abfd)) |
252b5132 | 2558 | return NULL; |
91d6fa6a | 2559 | return elf_elfsections (abfd)[sec_index]->bfd_section; |
252b5132 RH |
2560 | } |
2561 | ||
b35d266b | 2562 | static const struct bfd_elf_special_section special_sections_b[] = |
2f89ff8d | 2563 | { |
0112cd26 NC |
2564 | { STRING_COMMA_LEN (".bss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE }, |
2565 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2566 | }; |
2567 | ||
b35d266b | 2568 | static const struct bfd_elf_special_section special_sections_c[] = |
7f4d3958 | 2569 | { |
0112cd26 NC |
2570 | { STRING_COMMA_LEN (".comment"), 0, SHT_PROGBITS, 0 }, |
2571 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2572 | }; |
2573 | ||
b35d266b | 2574 | static const struct bfd_elf_special_section special_sections_d[] = |
7f4d3958 | 2575 | { |
0112cd26 NC |
2576 | { STRING_COMMA_LEN (".data"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE }, |
2577 | { STRING_COMMA_LEN (".data1"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE }, | |
a9a72a65 DE |
2578 | /* There are more DWARF sections than these, but they needn't be added here |
2579 | unless you have to cope with broken compilers that don't emit section | |
2580 | attributes or you want to help the user writing assembler. */ | |
0112cd26 NC |
2581 | { STRING_COMMA_LEN (".debug"), 0, SHT_PROGBITS, 0 }, |
2582 | { STRING_COMMA_LEN (".debug_line"), 0, SHT_PROGBITS, 0 }, | |
2583 | { STRING_COMMA_LEN (".debug_info"), 0, SHT_PROGBITS, 0 }, | |
2584 | { STRING_COMMA_LEN (".debug_abbrev"), 0, SHT_PROGBITS, 0 }, | |
2585 | { STRING_COMMA_LEN (".debug_aranges"), 0, SHT_PROGBITS, 0 }, | |
2586 | { STRING_COMMA_LEN (".dynamic"), 0, SHT_DYNAMIC, SHF_ALLOC }, | |
2587 | { STRING_COMMA_LEN (".dynstr"), 0, SHT_STRTAB, SHF_ALLOC }, | |
2588 | { STRING_COMMA_LEN (".dynsym"), 0, SHT_DYNSYM, SHF_ALLOC }, | |
2589 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2590 | }; |
2591 | ||
b35d266b | 2592 | static const struct bfd_elf_special_section special_sections_f[] = |
7f4d3958 | 2593 | { |
0112cd26 NC |
2594 | { STRING_COMMA_LEN (".fini"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR }, |
2595 | { STRING_COMMA_LEN (".fini_array"), 0, SHT_FINI_ARRAY, SHF_ALLOC + SHF_WRITE }, | |
2596 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2597 | }; |
2598 | ||
b35d266b | 2599 | static const struct bfd_elf_special_section special_sections_g[] = |
7f4d3958 | 2600 | { |
0112cd26 | 2601 | { STRING_COMMA_LEN (".gnu.linkonce.b"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE }, |
68efed41 | 2602 | { STRING_COMMA_LEN (".gnu.lto_"), -1, SHT_PROGBITS, SHF_EXCLUDE }, |
0112cd26 NC |
2603 | { STRING_COMMA_LEN (".got"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE }, |
2604 | { STRING_COMMA_LEN (".gnu.version"), 0, SHT_GNU_versym, 0 }, | |
2605 | { STRING_COMMA_LEN (".gnu.version_d"), 0, SHT_GNU_verdef, 0 }, | |
2606 | { STRING_COMMA_LEN (".gnu.version_r"), 0, SHT_GNU_verneed, 0 }, | |
2607 | { STRING_COMMA_LEN (".gnu.liblist"), 0, SHT_GNU_LIBLIST, SHF_ALLOC }, | |
2608 | { STRING_COMMA_LEN (".gnu.conflict"), 0, SHT_RELA, SHF_ALLOC }, | |
2609 | { STRING_COMMA_LEN (".gnu.hash"), 0, SHT_GNU_HASH, SHF_ALLOC }, | |
2610 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2611 | }; |
2612 | ||
b35d266b | 2613 | static const struct bfd_elf_special_section special_sections_h[] = |
7f4d3958 | 2614 | { |
0112cd26 NC |
2615 | { STRING_COMMA_LEN (".hash"), 0, SHT_HASH, SHF_ALLOC }, |
2616 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2617 | }; |
2618 | ||
b35d266b | 2619 | static const struct bfd_elf_special_section special_sections_i[] = |
7f4d3958 | 2620 | { |
0112cd26 NC |
2621 | { STRING_COMMA_LEN (".init"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR }, |
2622 | { STRING_COMMA_LEN (".init_array"), 0, SHT_INIT_ARRAY, SHF_ALLOC + SHF_WRITE }, | |
2623 | { STRING_COMMA_LEN (".interp"), 0, SHT_PROGBITS, 0 }, | |
2624 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2625 | }; |
2626 | ||
b35d266b | 2627 | static const struct bfd_elf_special_section special_sections_l[] = |
7f4d3958 | 2628 | { |
0112cd26 NC |
2629 | { STRING_COMMA_LEN (".line"), 0, SHT_PROGBITS, 0 }, |
2630 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2631 | }; |
2632 | ||
b35d266b | 2633 | static const struct bfd_elf_special_section special_sections_n[] = |
7f4d3958 | 2634 | { |
0112cd26 NC |
2635 | { STRING_COMMA_LEN (".note.GNU-stack"), 0, SHT_PROGBITS, 0 }, |
2636 | { STRING_COMMA_LEN (".note"), -1, SHT_NOTE, 0 }, | |
2637 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2638 | }; |
2639 | ||
b35d266b | 2640 | static const struct bfd_elf_special_section special_sections_p[] = |
7f4d3958 | 2641 | { |
0112cd26 NC |
2642 | { STRING_COMMA_LEN (".preinit_array"), 0, SHT_PREINIT_ARRAY, SHF_ALLOC + SHF_WRITE }, |
2643 | { STRING_COMMA_LEN (".plt"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR }, | |
2644 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2645 | }; |
2646 | ||
b35d266b | 2647 | static const struct bfd_elf_special_section special_sections_r[] = |
7f4d3958 | 2648 | { |
0112cd26 NC |
2649 | { STRING_COMMA_LEN (".rodata"), -2, SHT_PROGBITS, SHF_ALLOC }, |
2650 | { STRING_COMMA_LEN (".rodata1"), 0, SHT_PROGBITS, SHF_ALLOC }, | |
2651 | { STRING_COMMA_LEN (".rela"), -1, SHT_RELA, 0 }, | |
2652 | { STRING_COMMA_LEN (".rel"), -1, SHT_REL, 0 }, | |
2653 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2654 | }; |
2655 | ||
b35d266b | 2656 | static const struct bfd_elf_special_section special_sections_s[] = |
7f4d3958 | 2657 | { |
0112cd26 NC |
2658 | { STRING_COMMA_LEN (".shstrtab"), 0, SHT_STRTAB, 0 }, |
2659 | { STRING_COMMA_LEN (".strtab"), 0, SHT_STRTAB, 0 }, | |
2660 | { STRING_COMMA_LEN (".symtab"), 0, SHT_SYMTAB, 0 }, | |
60ff4dc4 HPN |
2661 | /* See struct bfd_elf_special_section declaration for the semantics of |
2662 | this special case where .prefix_length != strlen (.prefix). */ | |
2663 | { ".stabstr", 5, 3, SHT_STRTAB, 0 }, | |
0112cd26 | 2664 | { NULL, 0, 0, 0, 0 } |
2f89ff8d L |
2665 | }; |
2666 | ||
b35d266b | 2667 | static const struct bfd_elf_special_section special_sections_t[] = |
7f4d3958 | 2668 | { |
0112cd26 NC |
2669 | { STRING_COMMA_LEN (".text"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR }, |
2670 | { STRING_COMMA_LEN (".tbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS }, | |
2671 | { STRING_COMMA_LEN (".tdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS }, | |
2672 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2673 | }; |
2674 | ||
1b315056 CS |
2675 | static const struct bfd_elf_special_section special_sections_z[] = |
2676 | { | |
2677 | { STRING_COMMA_LEN (".zdebug_line"), 0, SHT_PROGBITS, 0 }, | |
2678 | { STRING_COMMA_LEN (".zdebug_info"), 0, SHT_PROGBITS, 0 }, | |
2679 | { STRING_COMMA_LEN (".zdebug_abbrev"), 0, SHT_PROGBITS, 0 }, | |
2680 | { STRING_COMMA_LEN (".zdebug_aranges"), 0, SHT_PROGBITS, 0 }, | |
2681 | { NULL, 0, 0, 0, 0 } | |
2682 | }; | |
2683 | ||
e4c93b56 | 2684 | static const struct bfd_elf_special_section * const special_sections[] = |
7f4d3958 | 2685 | { |
7f4d3958 | 2686 | special_sections_b, /* 'b' */ |
98ece1b3 | 2687 | special_sections_c, /* 'c' */ |
7f4d3958 L |
2688 | special_sections_d, /* 'd' */ |
2689 | NULL, /* 'e' */ | |
2690 | special_sections_f, /* 'f' */ | |
2691 | special_sections_g, /* 'g' */ | |
2692 | special_sections_h, /* 'h' */ | |
2693 | special_sections_i, /* 'i' */ | |
2694 | NULL, /* 'j' */ | |
2695 | NULL, /* 'k' */ | |
2696 | special_sections_l, /* 'l' */ | |
2697 | NULL, /* 'm' */ | |
2698 | special_sections_n, /* 'n' */ | |
2699 | NULL, /* 'o' */ | |
2700 | special_sections_p, /* 'p' */ | |
2701 | NULL, /* 'q' */ | |
2702 | special_sections_r, /* 'r' */ | |
2703 | special_sections_s, /* 's' */ | |
2704 | special_sections_t, /* 't' */ | |
1b315056 CS |
2705 | NULL, /* 'u' */ |
2706 | NULL, /* 'v' */ | |
2707 | NULL, /* 'w' */ | |
2708 | NULL, /* 'x' */ | |
2709 | NULL, /* 'y' */ | |
2710 | special_sections_z /* 'z' */ | |
7f4d3958 L |
2711 | }; |
2712 | ||
551b43fd AM |
2713 | const struct bfd_elf_special_section * |
2714 | _bfd_elf_get_special_section (const char *name, | |
2715 | const struct bfd_elf_special_section *spec, | |
2716 | unsigned int rela) | |
2f89ff8d L |
2717 | { |
2718 | int i; | |
7f4d3958 | 2719 | int len; |
7f4d3958 | 2720 | |
551b43fd | 2721 | len = strlen (name); |
7f4d3958 | 2722 | |
551b43fd | 2723 | for (i = 0; spec[i].prefix != NULL; i++) |
7dcb9820 AM |
2724 | { |
2725 | int suffix_len; | |
551b43fd | 2726 | int prefix_len = spec[i].prefix_length; |
7dcb9820 AM |
2727 | |
2728 | if (len < prefix_len) | |
2729 | continue; | |
551b43fd | 2730 | if (memcmp (name, spec[i].prefix, prefix_len) != 0) |
7dcb9820 AM |
2731 | continue; |
2732 | ||
551b43fd | 2733 | suffix_len = spec[i].suffix_length; |
7dcb9820 AM |
2734 | if (suffix_len <= 0) |
2735 | { | |
2736 | if (name[prefix_len] != 0) | |
2737 | { | |
2738 | if (suffix_len == 0) | |
2739 | continue; | |
2740 | if (name[prefix_len] != '.' | |
2741 | && (suffix_len == -2 | |
551b43fd | 2742 | || (rela && spec[i].type == SHT_REL))) |
7dcb9820 AM |
2743 | continue; |
2744 | } | |
2745 | } | |
2746 | else | |
2747 | { | |
2748 | if (len < prefix_len + suffix_len) | |
2749 | continue; | |
2750 | if (memcmp (name + len - suffix_len, | |
551b43fd | 2751 | spec[i].prefix + prefix_len, |
7dcb9820 AM |
2752 | suffix_len) != 0) |
2753 | continue; | |
2754 | } | |
551b43fd | 2755 | return &spec[i]; |
7dcb9820 | 2756 | } |
2f89ff8d L |
2757 | |
2758 | return NULL; | |
2759 | } | |
2760 | ||
7dcb9820 | 2761 | const struct bfd_elf_special_section * |
29ef7005 | 2762 | _bfd_elf_get_sec_type_attr (bfd *abfd, asection *sec) |
2f89ff8d | 2763 | { |
551b43fd AM |
2764 | int i; |
2765 | const struct bfd_elf_special_section *spec; | |
29ef7005 | 2766 | const struct elf_backend_data *bed; |
2f89ff8d L |
2767 | |
2768 | /* See if this is one of the special sections. */ | |
551b43fd AM |
2769 | if (sec->name == NULL) |
2770 | return NULL; | |
2f89ff8d | 2771 | |
29ef7005 L |
2772 | bed = get_elf_backend_data (abfd); |
2773 | spec = bed->special_sections; | |
2774 | if (spec) | |
2775 | { | |
2776 | spec = _bfd_elf_get_special_section (sec->name, | |
2777 | bed->special_sections, | |
2778 | sec->use_rela_p); | |
2779 | if (spec != NULL) | |
2780 | return spec; | |
2781 | } | |
2782 | ||
551b43fd AM |
2783 | if (sec->name[0] != '.') |
2784 | return NULL; | |
2f89ff8d | 2785 | |
551b43fd | 2786 | i = sec->name[1] - 'b'; |
1b315056 | 2787 | if (i < 0 || i > 'z' - 'b') |
551b43fd AM |
2788 | return NULL; |
2789 | ||
2790 | spec = special_sections[i]; | |
2f89ff8d | 2791 | |
551b43fd AM |
2792 | if (spec == NULL) |
2793 | return NULL; | |
2794 | ||
2795 | return _bfd_elf_get_special_section (sec->name, spec, sec->use_rela_p); | |
2f89ff8d L |
2796 | } |
2797 | ||
b34976b6 | 2798 | bfd_boolean |
217aa764 | 2799 | _bfd_elf_new_section_hook (bfd *abfd, asection *sec) |
252b5132 RH |
2800 | { |
2801 | struct bfd_elf_section_data *sdata; | |
551b43fd | 2802 | const struct elf_backend_data *bed; |
7dcb9820 | 2803 | const struct bfd_elf_special_section *ssect; |
252b5132 | 2804 | |
f0abc2a1 AM |
2805 | sdata = (struct bfd_elf_section_data *) sec->used_by_bfd; |
2806 | if (sdata == NULL) | |
2807 | { | |
a50b1753 NC |
2808 | sdata = (struct bfd_elf_section_data *) bfd_zalloc (abfd, |
2809 | sizeof (*sdata)); | |
f0abc2a1 AM |
2810 | if (sdata == NULL) |
2811 | return FALSE; | |
217aa764 | 2812 | sec->used_by_bfd = sdata; |
f0abc2a1 | 2813 | } |
bf572ba0 | 2814 | |
551b43fd AM |
2815 | /* Indicate whether or not this section should use RELA relocations. */ |
2816 | bed = get_elf_backend_data (abfd); | |
2817 | sec->use_rela_p = bed->default_use_rela_p; | |
2818 | ||
e843e0f8 L |
2819 | /* When we read a file, we don't need to set ELF section type and |
2820 | flags. They will be overridden in _bfd_elf_make_section_from_shdr | |
2821 | anyway. We will set ELF section type and flags for all linker | |
2822 | created sections. If user specifies BFD section flags, we will | |
2823 | set ELF section type and flags based on BFD section flags in | |
02ecc8e9 L |
2824 | elf_fake_sections. Special handling for .init_array/.fini_array |
2825 | output sections since they may contain .ctors/.dtors input | |
2826 | sections. We don't want _bfd_elf_init_private_section_data to | |
2827 | copy ELF section type from .ctors/.dtors input sections. */ | |
2828 | if (abfd->direction != read_direction | |
3496cb2a | 2829 | || (sec->flags & SEC_LINKER_CREATED) != 0) |
2f89ff8d | 2830 | { |
551b43fd | 2831 | ssect = (*bed->get_sec_type_attr) (abfd, sec); |
02ecc8e9 L |
2832 | if (ssect != NULL |
2833 | && (!sec->flags | |
2834 | || (sec->flags & SEC_LINKER_CREATED) != 0 | |
2835 | || ssect->type == SHT_INIT_ARRAY | |
2836 | || ssect->type == SHT_FINI_ARRAY)) | |
a31501e9 L |
2837 | { |
2838 | elf_section_type (sec) = ssect->type; | |
2839 | elf_section_flags (sec) = ssect->attr; | |
2840 | } | |
2f89ff8d L |
2841 | } |
2842 | ||
f592407e | 2843 | return _bfd_generic_new_section_hook (abfd, sec); |
252b5132 RH |
2844 | } |
2845 | ||
2846 | /* Create a new bfd section from an ELF program header. | |
2847 | ||
2848 | Since program segments have no names, we generate a synthetic name | |
2849 | of the form segment<NUM>, where NUM is generally the index in the | |
2850 | program header table. For segments that are split (see below) we | |
2851 | generate the names segment<NUM>a and segment<NUM>b. | |
2852 | ||
2853 | Note that some program segments may have a file size that is different than | |
2854 | (less than) the memory size. All this means is that at execution the | |
2855 | system must allocate the amount of memory specified by the memory size, | |
2856 | but only initialize it with the first "file size" bytes read from the | |
2857 | file. This would occur for example, with program segments consisting | |
2858 | of combined data+bss. | |
2859 | ||
2860 | To handle the above situation, this routine generates TWO bfd sections | |
2861 | for the single program segment. The first has the length specified by | |
2862 | the file size of the segment, and the second has the length specified | |
2863 | by the difference between the two sizes. In effect, the segment is split | |
d5191d0c | 2864 | into its initialized and uninitialized parts. |
252b5132 RH |
2865 | |
2866 | */ | |
2867 | ||
b34976b6 | 2868 | bfd_boolean |
217aa764 AM |
2869 | _bfd_elf_make_section_from_phdr (bfd *abfd, |
2870 | Elf_Internal_Phdr *hdr, | |
91d6fa6a | 2871 | int hdr_index, |
a50b1753 | 2872 | const char *type_name) |
252b5132 RH |
2873 | { |
2874 | asection *newsect; | |
2875 | char *name; | |
2876 | char namebuf[64]; | |
d4c88bbb | 2877 | size_t len; |
252b5132 RH |
2878 | int split; |
2879 | ||
2880 | split = ((hdr->p_memsz > 0) | |
2881 | && (hdr->p_filesz > 0) | |
2882 | && (hdr->p_memsz > hdr->p_filesz)); | |
d5191d0c AM |
2883 | |
2884 | if (hdr->p_filesz > 0) | |
252b5132 | 2885 | { |
91d6fa6a | 2886 | sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "a" : ""); |
d5191d0c | 2887 | len = strlen (namebuf) + 1; |
a50b1753 | 2888 | name = (char *) bfd_alloc (abfd, len); |
d5191d0c AM |
2889 | if (!name) |
2890 | return FALSE; | |
2891 | memcpy (name, namebuf, len); | |
2892 | newsect = bfd_make_section (abfd, name); | |
2893 | if (newsect == NULL) | |
2894 | return FALSE; | |
2895 | newsect->vma = hdr->p_vaddr; | |
2896 | newsect->lma = hdr->p_paddr; | |
2897 | newsect->size = hdr->p_filesz; | |
2898 | newsect->filepos = hdr->p_offset; | |
2899 | newsect->flags |= SEC_HAS_CONTENTS; | |
2900 | newsect->alignment_power = bfd_log2 (hdr->p_align); | |
2901 | if (hdr->p_type == PT_LOAD) | |
252b5132 | 2902 | { |
d5191d0c AM |
2903 | newsect->flags |= SEC_ALLOC; |
2904 | newsect->flags |= SEC_LOAD; | |
2905 | if (hdr->p_flags & PF_X) | |
2906 | { | |
2907 | /* FIXME: all we known is that it has execute PERMISSION, | |
2908 | may be data. */ | |
2909 | newsect->flags |= SEC_CODE; | |
2910 | } | |
2911 | } | |
2912 | if (!(hdr->p_flags & PF_W)) | |
2913 | { | |
2914 | newsect->flags |= SEC_READONLY; | |
252b5132 | 2915 | } |
252b5132 RH |
2916 | } |
2917 | ||
d5191d0c | 2918 | if (hdr->p_memsz > hdr->p_filesz) |
252b5132 | 2919 | { |
d5191d0c AM |
2920 | bfd_vma align; |
2921 | ||
91d6fa6a | 2922 | sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "b" : ""); |
d4c88bbb | 2923 | len = strlen (namebuf) + 1; |
a50b1753 | 2924 | name = (char *) bfd_alloc (abfd, len); |
252b5132 | 2925 | if (!name) |
b34976b6 | 2926 | return FALSE; |
d4c88bbb | 2927 | memcpy (name, namebuf, len); |
252b5132 RH |
2928 | newsect = bfd_make_section (abfd, name); |
2929 | if (newsect == NULL) | |
b34976b6 | 2930 | return FALSE; |
252b5132 RH |
2931 | newsect->vma = hdr->p_vaddr + hdr->p_filesz; |
2932 | newsect->lma = hdr->p_paddr + hdr->p_filesz; | |
eea6121a | 2933 | newsect->size = hdr->p_memsz - hdr->p_filesz; |
d5191d0c AM |
2934 | newsect->filepos = hdr->p_offset + hdr->p_filesz; |
2935 | align = newsect->vma & -newsect->vma; | |
2936 | if (align == 0 || align > hdr->p_align) | |
2937 | align = hdr->p_align; | |
2938 | newsect->alignment_power = bfd_log2 (align); | |
252b5132 RH |
2939 | if (hdr->p_type == PT_LOAD) |
2940 | { | |
d5191d0c AM |
2941 | /* Hack for gdb. Segments that have not been modified do |
2942 | not have their contents written to a core file, on the | |
2943 | assumption that a debugger can find the contents in the | |
2944 | executable. We flag this case by setting the fake | |
2945 | section size to zero. Note that "real" bss sections will | |
2946 | always have their contents dumped to the core file. */ | |
2947 | if (bfd_get_format (abfd) == bfd_core) | |
2948 | newsect->size = 0; | |
252b5132 RH |
2949 | newsect->flags |= SEC_ALLOC; |
2950 | if (hdr->p_flags & PF_X) | |
2951 | newsect->flags |= SEC_CODE; | |
2952 | } | |
2953 | if (!(hdr->p_flags & PF_W)) | |
2954 | newsect->flags |= SEC_READONLY; | |
2955 | } | |
2956 | ||
b34976b6 | 2957 | return TRUE; |
252b5132 RH |
2958 | } |
2959 | ||
b34976b6 | 2960 | bfd_boolean |
91d6fa6a | 2961 | bfd_section_from_phdr (bfd *abfd, Elf_Internal_Phdr *hdr, int hdr_index) |
20cfcaae | 2962 | { |
9c5bfbb7 | 2963 | const struct elf_backend_data *bed; |
20cfcaae NC |
2964 | |
2965 | switch (hdr->p_type) | |
2966 | { | |
2967 | case PT_NULL: | |
91d6fa6a | 2968 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "null"); |
20cfcaae NC |
2969 | |
2970 | case PT_LOAD: | |
91d6fa6a | 2971 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "load"); |
20cfcaae NC |
2972 | |
2973 | case PT_DYNAMIC: | |
91d6fa6a | 2974 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "dynamic"); |
20cfcaae NC |
2975 | |
2976 | case PT_INTERP: | |
91d6fa6a | 2977 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "interp"); |
20cfcaae NC |
2978 | |
2979 | case PT_NOTE: | |
91d6fa6a | 2980 | if (! _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "note")) |
b34976b6 | 2981 | return FALSE; |
718175fa | 2982 | if (! elf_read_notes (abfd, hdr->p_offset, hdr->p_filesz)) |
b34976b6 AM |
2983 | return FALSE; |
2984 | return TRUE; | |
20cfcaae NC |
2985 | |
2986 | case PT_SHLIB: | |
91d6fa6a | 2987 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "shlib"); |
20cfcaae NC |
2988 | |
2989 | case PT_PHDR: | |
91d6fa6a | 2990 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "phdr"); |
20cfcaae | 2991 | |
811072d8 | 2992 | case PT_GNU_EH_FRAME: |
91d6fa6a | 2993 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, |
811072d8 RM |
2994 | "eh_frame_hdr"); |
2995 | ||
2b05f1b7 | 2996 | case PT_GNU_STACK: |
91d6fa6a | 2997 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "stack"); |
9ee5e499 | 2998 | |
8c37241b | 2999 | case PT_GNU_RELRO: |
91d6fa6a | 3000 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "relro"); |
8c37241b | 3001 | |
20cfcaae | 3002 | default: |
8c1acd09 | 3003 | /* Check for any processor-specific program segment types. */ |
20cfcaae | 3004 | bed = get_elf_backend_data (abfd); |
91d6fa6a | 3005 | return bed->elf_backend_section_from_phdr (abfd, hdr, hdr_index, "proc"); |
20cfcaae NC |
3006 | } |
3007 | } | |
3008 | ||
d4730f92 BS |
3009 | /* Return the REL_HDR for SEC, assuming there is only a single one, either |
3010 | REL or RELA. */ | |
3011 | ||
3012 | Elf_Internal_Shdr * | |
3013 | _bfd_elf_single_rel_hdr (asection *sec) | |
3014 | { | |
3015 | if (elf_section_data (sec)->rel.hdr) | |
3016 | { | |
3017 | BFD_ASSERT (elf_section_data (sec)->rela.hdr == NULL); | |
3018 | return elf_section_data (sec)->rel.hdr; | |
3019 | } | |
3020 | else | |
3021 | return elf_section_data (sec)->rela.hdr; | |
3022 | } | |
3023 | ||
3e19fb8f L |
3024 | static bfd_boolean |
3025 | _bfd_elf_set_reloc_sh_name (bfd *abfd, | |
3026 | Elf_Internal_Shdr *rel_hdr, | |
3027 | const char *sec_name, | |
3028 | bfd_boolean use_rela_p) | |
3029 | { | |
3030 | char *name = (char *) bfd_alloc (abfd, | |
3031 | sizeof ".rela" + strlen (sec_name)); | |
3032 | if (name == NULL) | |
3033 | return FALSE; | |
3034 | ||
3035 | sprintf (name, "%s%s", use_rela_p ? ".rela" : ".rel", sec_name); | |
3036 | rel_hdr->sh_name = | |
3037 | (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), name, | |
3038 | FALSE); | |
3039 | if (rel_hdr->sh_name == (unsigned int) -1) | |
3040 | return FALSE; | |
3041 | ||
3042 | return TRUE; | |
3043 | } | |
3044 | ||
d4730f92 BS |
3045 | /* Allocate and initialize a section-header for a new reloc section, |
3046 | containing relocations against ASECT. It is stored in RELDATA. If | |
3047 | USE_RELA_P is TRUE, we use RELA relocations; otherwise, we use REL | |
3048 | relocations. */ | |
23bc299b | 3049 | |
5d13b3b3 | 3050 | static bfd_boolean |
217aa764 | 3051 | _bfd_elf_init_reloc_shdr (bfd *abfd, |
d4730f92 | 3052 | struct bfd_elf_section_reloc_data *reldata, |
f6fe1ccd | 3053 | const char *sec_name, |
3e19fb8f L |
3054 | bfd_boolean use_rela_p, |
3055 | bfd_boolean delay_st_name_p) | |
23bc299b | 3056 | { |
d4730f92 | 3057 | Elf_Internal_Shdr *rel_hdr; |
9c5bfbb7 | 3058 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
d4730f92 | 3059 | |
d4730f92 | 3060 | BFD_ASSERT (reldata->hdr == NULL); |
ef53be89 | 3061 | rel_hdr = bfd_zalloc (abfd, sizeof (*rel_hdr)); |
d4730f92 | 3062 | reldata->hdr = rel_hdr; |
23bc299b | 3063 | |
3e19fb8f L |
3064 | if (delay_st_name_p) |
3065 | rel_hdr->sh_name = (unsigned int) -1; | |
3066 | else if (!_bfd_elf_set_reloc_sh_name (abfd, rel_hdr, sec_name, | |
3067 | use_rela_p)) | |
b34976b6 | 3068 | return FALSE; |
23bc299b MM |
3069 | rel_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL; |
3070 | rel_hdr->sh_entsize = (use_rela_p | |
3071 | ? bed->s->sizeof_rela | |
3072 | : bed->s->sizeof_rel); | |
72de5009 | 3073 | rel_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align; |
23bc299b MM |
3074 | rel_hdr->sh_flags = 0; |
3075 | rel_hdr->sh_addr = 0; | |
3076 | rel_hdr->sh_size = 0; | |
3077 | rel_hdr->sh_offset = 0; | |
3078 | ||
b34976b6 | 3079 | return TRUE; |
23bc299b MM |
3080 | } |
3081 | ||
94be91de JB |
3082 | /* Return the default section type based on the passed in section flags. */ |
3083 | ||
3084 | int | |
3085 | bfd_elf_get_default_section_type (flagword flags) | |
3086 | { | |
3087 | if ((flags & SEC_ALLOC) != 0 | |
2e76e85a | 3088 | && (flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0) |
94be91de JB |
3089 | return SHT_NOBITS; |
3090 | return SHT_PROGBITS; | |
3091 | } | |
3092 | ||
d4730f92 BS |
3093 | struct fake_section_arg |
3094 | { | |
3095 | struct bfd_link_info *link_info; | |
3096 | bfd_boolean failed; | |
3097 | }; | |
3098 | ||
252b5132 RH |
3099 | /* Set up an ELF internal section header for a section. */ |
3100 | ||
252b5132 | 3101 | static void |
d4730f92 | 3102 | elf_fake_sections (bfd *abfd, asection *asect, void *fsarg) |
252b5132 | 3103 | { |
d4730f92 | 3104 | struct fake_section_arg *arg = (struct fake_section_arg *)fsarg; |
9c5bfbb7 | 3105 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
d4730f92 | 3106 | struct bfd_elf_section_data *esd = elf_section_data (asect); |
252b5132 | 3107 | Elf_Internal_Shdr *this_hdr; |
0414f35b | 3108 | unsigned int sh_type; |
0ce398f1 | 3109 | const char *name = asect->name; |
3e19fb8f | 3110 | bfd_boolean delay_st_name_p = FALSE; |
252b5132 | 3111 | |
d4730f92 | 3112 | if (arg->failed) |
252b5132 RH |
3113 | { |
3114 | /* We already failed; just get out of the bfd_map_over_sections | |
08a40648 | 3115 | loop. */ |
252b5132 RH |
3116 | return; |
3117 | } | |
3118 | ||
d4730f92 | 3119 | this_hdr = &esd->this_hdr; |
252b5132 | 3120 | |
f6fe1ccd | 3121 | if (arg->link_info) |
0ce398f1 | 3122 | { |
f6fe1ccd L |
3123 | /* ld: compress DWARF debug sections with names: .debug_*. */ |
3124 | if ((arg->link_info->compress_debug & COMPRESS_DEBUG) | |
3125 | && (asect->flags & SEC_DEBUGGING) | |
3126 | && name[1] == 'd' | |
3127 | && name[6] == '_') | |
3128 | { | |
3129 | /* Set SEC_ELF_COMPRESS to indicate this section should be | |
3130 | compressed. */ | |
3131 | asect->flags |= SEC_ELF_COMPRESS; | |
0ce398f1 | 3132 | |
dd905818 | 3133 | /* If this section will be compressed, delay adding section |
3e19fb8f L |
3134 | name to section name section after it is compressed in |
3135 | _bfd_elf_assign_file_positions_for_non_load. */ | |
3136 | delay_st_name_p = TRUE; | |
f6fe1ccd L |
3137 | } |
3138 | } | |
3139 | else if ((asect->flags & SEC_ELF_RENAME)) | |
3140 | { | |
3141 | /* objcopy: rename output DWARF debug section. */ | |
3142 | if ((abfd->flags & (BFD_DECOMPRESS | BFD_COMPRESS_GABI))) | |
3143 | { | |
3144 | /* When we decompress or compress with SHF_COMPRESSED, | |
3145 | convert section name from .zdebug_* to .debug_* if | |
3146 | needed. */ | |
3147 | if (name[1] == 'z') | |
3148 | { | |
3149 | char *new_name = convert_zdebug_to_debug (abfd, name); | |
3150 | if (new_name == NULL) | |
3151 | { | |
3152 | arg->failed = TRUE; | |
3153 | return; | |
3154 | } | |
3155 | name = new_name; | |
3156 | } | |
3157 | } | |
3158 | else if (asect->compress_status == COMPRESS_SECTION_DONE) | |
0ce398f1 | 3159 | { |
f6fe1ccd L |
3160 | /* PR binutils/18087: Compression does not always make a |
3161 | section smaller. So only rename the section when | |
3162 | compression has actually taken place. If input section | |
3163 | name is .zdebug_*, we should never compress it again. */ | |
3164 | char *new_name = convert_debug_to_zdebug (abfd, name); | |
0ce398f1 L |
3165 | if (new_name == NULL) |
3166 | { | |
3167 | arg->failed = TRUE; | |
3168 | return; | |
3169 | } | |
f6fe1ccd L |
3170 | BFD_ASSERT (name[1] != 'z'); |
3171 | name = new_name; | |
0ce398f1 L |
3172 | } |
3173 | } | |
3174 | ||
3e19fb8f L |
3175 | if (delay_st_name_p) |
3176 | this_hdr->sh_name = (unsigned int) -1; | |
3177 | else | |
252b5132 | 3178 | { |
3e19fb8f L |
3179 | this_hdr->sh_name |
3180 | = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), | |
3181 | name, FALSE); | |
3182 | if (this_hdr->sh_name == (unsigned int) -1) | |
3183 | { | |
3184 | arg->failed = TRUE; | |
3185 | return; | |
3186 | } | |
252b5132 RH |
3187 | } |
3188 | ||
a4d8e49b | 3189 | /* Don't clear sh_flags. Assembler may set additional bits. */ |
252b5132 RH |
3190 | |
3191 | if ((asect->flags & SEC_ALLOC) != 0 | |
3192 | || asect->user_set_vma) | |
3193 | this_hdr->sh_addr = asect->vma; | |
3194 | else | |
3195 | this_hdr->sh_addr = 0; | |
3196 | ||
3197 | this_hdr->sh_offset = 0; | |
eea6121a | 3198 | this_hdr->sh_size = asect->size; |
252b5132 | 3199 | this_hdr->sh_link = 0; |
c86934ce NC |
3200 | /* PR 17512: file: 0eb809fe, 8b0535ee. */ |
3201 | if (asect->alignment_power >= (sizeof (bfd_vma) * 8) - 1) | |
3202 | { | |
4eca0228 | 3203 | _bfd_error_handler |
695344c0 | 3204 | /* xgettext:c-format */ |
c86934ce NC |
3205 | (_("%B: error: Alignment power %d of section `%A' is too big"), |
3206 | abfd, asect, asect->alignment_power); | |
3207 | arg->failed = TRUE; | |
3208 | return; | |
3209 | } | |
72de5009 | 3210 | this_hdr->sh_addralign = (bfd_vma) 1 << asect->alignment_power; |
252b5132 RH |
3211 | /* The sh_entsize and sh_info fields may have been set already by |
3212 | copy_private_section_data. */ | |
3213 | ||
3214 | this_hdr->bfd_section = asect; | |
3215 | this_hdr->contents = NULL; | |
3216 | ||
3cddba1e L |
3217 | /* If the section type is unspecified, we set it based on |
3218 | asect->flags. */ | |
98ece1b3 AM |
3219 | if ((asect->flags & SEC_GROUP) != 0) |
3220 | sh_type = SHT_GROUP; | |
98ece1b3 | 3221 | else |
94be91de | 3222 | sh_type = bfd_elf_get_default_section_type (asect->flags); |
98ece1b3 | 3223 | |
3cddba1e | 3224 | if (this_hdr->sh_type == SHT_NULL) |
98ece1b3 AM |
3225 | this_hdr->sh_type = sh_type; |
3226 | else if (this_hdr->sh_type == SHT_NOBITS | |
3227 | && sh_type == SHT_PROGBITS | |
3228 | && (asect->flags & SEC_ALLOC) != 0) | |
3cddba1e | 3229 | { |
98ece1b3 AM |
3230 | /* Warn if we are changing a NOBITS section to PROGBITS, but |
3231 | allow the link to proceed. This can happen when users link | |
3232 | non-bss input sections to bss output sections, or emit data | |
3233 | to a bss output section via a linker script. */ | |
4eca0228 | 3234 | _bfd_error_handler |
58f0869b | 3235 | (_("warning: section `%A' type changed to PROGBITS"), asect); |
98ece1b3 | 3236 | this_hdr->sh_type = sh_type; |
3cddba1e L |
3237 | } |
3238 | ||
2f89ff8d | 3239 | switch (this_hdr->sh_type) |
252b5132 | 3240 | { |
2f89ff8d | 3241 | default: |
2f89ff8d L |
3242 | break; |
3243 | ||
3244 | case SHT_STRTAB: | |
2f89ff8d L |
3245 | case SHT_NOTE: |
3246 | case SHT_NOBITS: | |
3247 | case SHT_PROGBITS: | |
3248 | break; | |
606851fb AM |
3249 | |
3250 | case SHT_INIT_ARRAY: | |
3251 | case SHT_FINI_ARRAY: | |
3252 | case SHT_PREINIT_ARRAY: | |
3253 | this_hdr->sh_entsize = bed->s->arch_size / 8; | |
3254 | break; | |
2f89ff8d L |
3255 | |
3256 | case SHT_HASH: | |
c7ac6ff8 | 3257 | this_hdr->sh_entsize = bed->s->sizeof_hash_entry; |
2f89ff8d | 3258 | break; |
5de3bf90 | 3259 | |
2f89ff8d | 3260 | case SHT_DYNSYM: |
252b5132 | 3261 | this_hdr->sh_entsize = bed->s->sizeof_sym; |
2f89ff8d L |
3262 | break; |
3263 | ||
3264 | case SHT_DYNAMIC: | |
252b5132 | 3265 | this_hdr->sh_entsize = bed->s->sizeof_dyn; |
2f89ff8d L |
3266 | break; |
3267 | ||
3268 | case SHT_RELA: | |
3269 | if (get_elf_backend_data (abfd)->may_use_rela_p) | |
3270 | this_hdr->sh_entsize = bed->s->sizeof_rela; | |
3271 | break; | |
3272 | ||
3273 | case SHT_REL: | |
3274 | if (get_elf_backend_data (abfd)->may_use_rel_p) | |
3275 | this_hdr->sh_entsize = bed->s->sizeof_rel; | |
3276 | break; | |
3277 | ||
3278 | case SHT_GNU_versym: | |
252b5132 | 3279 | this_hdr->sh_entsize = sizeof (Elf_External_Versym); |
2f89ff8d L |
3280 | break; |
3281 | ||
3282 | case SHT_GNU_verdef: | |
252b5132 RH |
3283 | this_hdr->sh_entsize = 0; |
3284 | /* objcopy or strip will copy over sh_info, but may not set | |
08a40648 AM |
3285 | cverdefs. The linker will set cverdefs, but sh_info will be |
3286 | zero. */ | |
252b5132 RH |
3287 | if (this_hdr->sh_info == 0) |
3288 | this_hdr->sh_info = elf_tdata (abfd)->cverdefs; | |
3289 | else | |
3290 | BFD_ASSERT (elf_tdata (abfd)->cverdefs == 0 | |
3291 | || this_hdr->sh_info == elf_tdata (abfd)->cverdefs); | |
2f89ff8d L |
3292 | break; |
3293 | ||
3294 | case SHT_GNU_verneed: | |
252b5132 RH |
3295 | this_hdr->sh_entsize = 0; |
3296 | /* objcopy or strip will copy over sh_info, but may not set | |
08a40648 AM |
3297 | cverrefs. The linker will set cverrefs, but sh_info will be |
3298 | zero. */ | |
252b5132 RH |
3299 | if (this_hdr->sh_info == 0) |
3300 | this_hdr->sh_info = elf_tdata (abfd)->cverrefs; | |
3301 | else | |
3302 | BFD_ASSERT (elf_tdata (abfd)->cverrefs == 0 | |
3303 | || this_hdr->sh_info == elf_tdata (abfd)->cverrefs); | |
2f89ff8d L |
3304 | break; |
3305 | ||
3306 | case SHT_GROUP: | |
1783205a | 3307 | this_hdr->sh_entsize = GRP_ENTRY_SIZE; |
2f89ff8d | 3308 | break; |
fdc90cb4 JJ |
3309 | |
3310 | case SHT_GNU_HASH: | |
3311 | this_hdr->sh_entsize = bed->s->arch_size == 64 ? 0 : 4; | |
3312 | break; | |
dbb410c3 | 3313 | } |
252b5132 RH |
3314 | |
3315 | if ((asect->flags & SEC_ALLOC) != 0) | |
3316 | this_hdr->sh_flags |= SHF_ALLOC; | |
3317 | if ((asect->flags & SEC_READONLY) == 0) | |
3318 | this_hdr->sh_flags |= SHF_WRITE; | |
3319 | if ((asect->flags & SEC_CODE) != 0) | |
3320 | this_hdr->sh_flags |= SHF_EXECINSTR; | |
f5fa8ca2 JJ |
3321 | if ((asect->flags & SEC_MERGE) != 0) |
3322 | { | |
3323 | this_hdr->sh_flags |= SHF_MERGE; | |
3324 | this_hdr->sh_entsize = asect->entsize; | |
f5fa8ca2 | 3325 | } |
84865015 NC |
3326 | if ((asect->flags & SEC_STRINGS) != 0) |
3327 | this_hdr->sh_flags |= SHF_STRINGS; | |
1126897b | 3328 | if ((asect->flags & SEC_GROUP) == 0 && elf_group_name (asect) != NULL) |
dbb410c3 | 3329 | this_hdr->sh_flags |= SHF_GROUP; |
13ae64f3 | 3330 | if ((asect->flags & SEC_THREAD_LOCAL) != 0) |
704afa60 JJ |
3331 | { |
3332 | this_hdr->sh_flags |= SHF_TLS; | |
3a800eb9 AM |
3333 | if (asect->size == 0 |
3334 | && (asect->flags & SEC_HAS_CONTENTS) == 0) | |
704afa60 | 3335 | { |
3a800eb9 | 3336 | struct bfd_link_order *o = asect->map_tail.link_order; |
b34976b6 | 3337 | |
704afa60 | 3338 | this_hdr->sh_size = 0; |
3a800eb9 AM |
3339 | if (o != NULL) |
3340 | { | |
704afa60 | 3341 | this_hdr->sh_size = o->offset + o->size; |
3a800eb9 AM |
3342 | if (this_hdr->sh_size != 0) |
3343 | this_hdr->sh_type = SHT_NOBITS; | |
3344 | } | |
704afa60 JJ |
3345 | } |
3346 | } | |
18ae9cc1 L |
3347 | if ((asect->flags & (SEC_GROUP | SEC_EXCLUDE)) == SEC_EXCLUDE) |
3348 | this_hdr->sh_flags |= SHF_EXCLUDE; | |
252b5132 | 3349 | |
d4730f92 BS |
3350 | /* If the section has relocs, set up a section header for the |
3351 | SHT_REL[A] section. If two relocation sections are required for | |
3352 | this section, it is up to the processor-specific back-end to | |
3353 | create the other. */ | |
3354 | if ((asect->flags & SEC_RELOC) != 0) | |
3355 | { | |
3356 | /* When doing a relocatable link, create both REL and RELA sections if | |
3357 | needed. */ | |
3358 | if (arg->link_info | |
3359 | /* Do the normal setup if we wouldn't create any sections here. */ | |
3360 | && esd->rel.count + esd->rela.count > 0 | |
0e1862bb L |
3361 | && (bfd_link_relocatable (arg->link_info) |
3362 | || arg->link_info->emitrelocations)) | |
d4730f92 BS |
3363 | { |
3364 | if (esd->rel.count && esd->rel.hdr == NULL | |
3e19fb8f L |
3365 | && !_bfd_elf_init_reloc_shdr (abfd, &esd->rel, name, FALSE, |
3366 | delay_st_name_p)) | |
d4730f92 BS |
3367 | { |
3368 | arg->failed = TRUE; | |
3369 | return; | |
3370 | } | |
3371 | if (esd->rela.count && esd->rela.hdr == NULL | |
3e19fb8f L |
3372 | && !_bfd_elf_init_reloc_shdr (abfd, &esd->rela, name, TRUE, |
3373 | delay_st_name_p)) | |
d4730f92 BS |
3374 | { |
3375 | arg->failed = TRUE; | |
3376 | return; | |
3377 | } | |
3378 | } | |
3379 | else if (!_bfd_elf_init_reloc_shdr (abfd, | |
3380 | (asect->use_rela_p | |
3381 | ? &esd->rela : &esd->rel), | |
f6fe1ccd | 3382 | name, |
3e19fb8f L |
3383 | asect->use_rela_p, |
3384 | delay_st_name_p)) | |
d4730f92 BS |
3385 | arg->failed = TRUE; |
3386 | } | |
3387 | ||
252b5132 | 3388 | /* Check for processor-specific section types. */ |
0414f35b | 3389 | sh_type = this_hdr->sh_type; |
e1fddb6b AO |
3390 | if (bed->elf_backend_fake_sections |
3391 | && !(*bed->elf_backend_fake_sections) (abfd, this_hdr, asect)) | |
d4730f92 | 3392 | arg->failed = TRUE; |
252b5132 | 3393 | |
42bb2e33 | 3394 | if (sh_type == SHT_NOBITS && asect->size != 0) |
0414f35b AM |
3395 | { |
3396 | /* Don't change the header type from NOBITS if we are being | |
42bb2e33 | 3397 | called for objcopy --only-keep-debug. */ |
0414f35b AM |
3398 | this_hdr->sh_type = sh_type; |
3399 | } | |
252b5132 RH |
3400 | } |
3401 | ||
bcacc0f5 AM |
3402 | /* Fill in the contents of a SHT_GROUP section. Called from |
3403 | _bfd_elf_compute_section_file_positions for gas, objcopy, and | |
3404 | when ELF targets use the generic linker, ld. Called for ld -r | |
3405 | from bfd_elf_final_link. */ | |
dbb410c3 | 3406 | |
1126897b | 3407 | void |
217aa764 | 3408 | bfd_elf_set_group_contents (bfd *abfd, asection *sec, void *failedptrarg) |
dbb410c3 | 3409 | { |
a50b1753 | 3410 | bfd_boolean *failedptr = (bfd_boolean *) failedptrarg; |
9dce4196 | 3411 | asection *elt, *first; |
dbb410c3 | 3412 | unsigned char *loc; |
b34976b6 | 3413 | bfd_boolean gas; |
dbb410c3 | 3414 | |
7e4111ad L |
3415 | /* Ignore linker created group section. See elfNN_ia64_object_p in |
3416 | elfxx-ia64.c. */ | |
3417 | if (((sec->flags & (SEC_GROUP | SEC_LINKER_CREATED)) != SEC_GROUP) | |
dbb410c3 AM |
3418 | || *failedptr) |
3419 | return; | |
3420 | ||
bcacc0f5 AM |
3421 | if (elf_section_data (sec)->this_hdr.sh_info == 0) |
3422 | { | |
3423 | unsigned long symindx = 0; | |
3424 | ||
3425 | /* elf_group_id will have been set up by objcopy and the | |
3426 | generic linker. */ | |
3427 | if (elf_group_id (sec) != NULL) | |
3428 | symindx = elf_group_id (sec)->udata.i; | |
1126897b | 3429 | |
bcacc0f5 AM |
3430 | if (symindx == 0) |
3431 | { | |
3432 | /* If called from the assembler, swap_out_syms will have set up | |
3433 | elf_section_syms. */ | |
3434 | BFD_ASSERT (elf_section_syms (abfd) != NULL); | |
3435 | symindx = elf_section_syms (abfd)[sec->index]->udata.i; | |
3436 | } | |
3437 | elf_section_data (sec)->this_hdr.sh_info = symindx; | |
3438 | } | |
3439 | else if (elf_section_data (sec)->this_hdr.sh_info == (unsigned int) -2) | |
1126897b | 3440 | { |
bcacc0f5 AM |
3441 | /* The ELF backend linker sets sh_info to -2 when the group |
3442 | signature symbol is global, and thus the index can't be | |
3443 | set until all local symbols are output. */ | |
53720c49 AM |
3444 | asection *igroup; |
3445 | struct bfd_elf_section_data *sec_data; | |
3446 | unsigned long symndx; | |
3447 | unsigned long extsymoff; | |
bcacc0f5 AM |
3448 | struct elf_link_hash_entry *h; |
3449 | ||
53720c49 AM |
3450 | /* The point of this little dance to the first SHF_GROUP section |
3451 | then back to the SHT_GROUP section is that this gets us to | |
3452 | the SHT_GROUP in the input object. */ | |
3453 | igroup = elf_sec_group (elf_next_in_group (sec)); | |
3454 | sec_data = elf_section_data (igroup); | |
3455 | symndx = sec_data->this_hdr.sh_info; | |
3456 | extsymoff = 0; | |
bcacc0f5 AM |
3457 | if (!elf_bad_symtab (igroup->owner)) |
3458 | { | |
3459 | Elf_Internal_Shdr *symtab_hdr; | |
3460 | ||
3461 | symtab_hdr = &elf_tdata (igroup->owner)->symtab_hdr; | |
3462 | extsymoff = symtab_hdr->sh_info; | |
3463 | } | |
3464 | h = elf_sym_hashes (igroup->owner)[symndx - extsymoff]; | |
3465 | while (h->root.type == bfd_link_hash_indirect | |
3466 | || h->root.type == bfd_link_hash_warning) | |
3467 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
3468 | ||
3469 | elf_section_data (sec)->this_hdr.sh_info = h->indx; | |
1126897b | 3470 | } |
dbb410c3 | 3471 | |
1126897b | 3472 | /* The contents won't be allocated for "ld -r" or objcopy. */ |
b34976b6 | 3473 | gas = TRUE; |
dbb410c3 AM |
3474 | if (sec->contents == NULL) |
3475 | { | |
b34976b6 | 3476 | gas = FALSE; |
a50b1753 | 3477 | sec->contents = (unsigned char *) bfd_alloc (abfd, sec->size); |
9dce4196 AM |
3478 | |
3479 | /* Arrange for the section to be written out. */ | |
3480 | elf_section_data (sec)->this_hdr.contents = sec->contents; | |
dbb410c3 AM |
3481 | if (sec->contents == NULL) |
3482 | { | |
b34976b6 | 3483 | *failedptr = TRUE; |
dbb410c3 AM |
3484 | return; |
3485 | } | |
3486 | } | |
3487 | ||
eea6121a | 3488 | loc = sec->contents + sec->size; |
dbb410c3 | 3489 | |
9dce4196 AM |
3490 | /* Get the pointer to the first section in the group that gas |
3491 | squirreled away here. objcopy arranges for this to be set to the | |
3492 | start of the input section group. */ | |
3493 | first = elt = elf_next_in_group (sec); | |
dbb410c3 AM |
3494 | |
3495 | /* First element is a flag word. Rest of section is elf section | |
3496 | indices for all the sections of the group. Write them backwards | |
3497 | just to keep the group in the same order as given in .section | |
3498 | directives, not that it matters. */ | |
3499 | while (elt != NULL) | |
3500 | { | |
9dce4196 | 3501 | asection *s; |
9dce4196 | 3502 | |
9dce4196 | 3503 | s = elt; |
415f38a6 AM |
3504 | if (!gas) |
3505 | s = s->output_section; | |
3506 | if (s != NULL | |
3507 | && !bfd_is_abs_section (s)) | |
01e1a5bc | 3508 | { |
415f38a6 AM |
3509 | unsigned int idx = elf_section_data (s)->this_idx; |
3510 | ||
01e1a5bc | 3511 | loc -= 4; |
01e1a5bc NC |
3512 | H_PUT_32 (abfd, idx, loc); |
3513 | } | |
945906ff | 3514 | elt = elf_next_in_group (elt); |
9dce4196 AM |
3515 | if (elt == first) |
3516 | break; | |
dbb410c3 AM |
3517 | } |
3518 | ||
3d7f7666 | 3519 | if ((loc -= 4) != sec->contents) |
9dce4196 | 3520 | abort (); |
dbb410c3 | 3521 | |
9dce4196 | 3522 | H_PUT_32 (abfd, sec->flags & SEC_LINK_ONCE ? GRP_COMDAT : 0, loc); |
dbb410c3 AM |
3523 | } |
3524 | ||
bd53a53a L |
3525 | /* Return the section which RELOC_SEC applies to. */ |
3526 | ||
3527 | asection * | |
3528 | _bfd_elf_get_reloc_section (asection *reloc_sec) | |
3529 | { | |
3530 | const char *name; | |
3531 | unsigned int type; | |
3532 | bfd *abfd; | |
3533 | ||
3534 | if (reloc_sec == NULL) | |
3535 | return NULL; | |
3536 | ||
3537 | type = elf_section_data (reloc_sec)->this_hdr.sh_type; | |
3538 | if (type != SHT_REL && type != SHT_RELA) | |
3539 | return NULL; | |
3540 | ||
3541 | /* We look up the section the relocs apply to by name. */ | |
3542 | name = reloc_sec->name; | |
3543 | if (type == SHT_REL) | |
3544 | name += 4; | |
3545 | else | |
3546 | name += 5; | |
3547 | ||
3548 | /* If a target needs .got.plt section, relocations in rela.plt/rel.plt | |
3549 | section apply to .got.plt section. */ | |
3550 | abfd = reloc_sec->owner; | |
3551 | if (get_elf_backend_data (abfd)->want_got_plt | |
3552 | && strcmp (name, ".plt") == 0) | |
87070c08 AM |
3553 | { |
3554 | /* .got.plt is a linker created input section. It may be mapped | |
3555 | to some other output section. Try two likely sections. */ | |
3556 | name = ".got.plt"; | |
3557 | reloc_sec = bfd_get_section_by_name (abfd, name); | |
3558 | if (reloc_sec != NULL) | |
3559 | return reloc_sec; | |
3560 | name = ".got"; | |
3561 | } | |
bd53a53a L |
3562 | |
3563 | reloc_sec = bfd_get_section_by_name (abfd, name); | |
3564 | return reloc_sec; | |
3565 | } | |
3566 | ||
252b5132 RH |
3567 | /* Assign all ELF section numbers. The dummy first section is handled here |
3568 | too. The link/info pointers for the standard section types are filled | |
3569 | in here too, while we're at it. */ | |
3570 | ||
b34976b6 | 3571 | static bfd_boolean |
da9f89d4 | 3572 | assign_section_numbers (bfd *abfd, struct bfd_link_info *link_info) |
252b5132 RH |
3573 | { |
3574 | struct elf_obj_tdata *t = elf_tdata (abfd); | |
3575 | asection *sec; | |
3e19fb8f | 3576 | unsigned int section_number; |
252b5132 | 3577 | Elf_Internal_Shdr **i_shdrp; |
47cc2cf5 | 3578 | struct bfd_elf_section_data *d; |
3516e984 | 3579 | bfd_boolean need_symtab; |
252b5132 RH |
3580 | |
3581 | section_number = 1; | |
3582 | ||
2b0f7ef9 JJ |
3583 | _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd)); |
3584 | ||
da9f89d4 | 3585 | /* SHT_GROUP sections are in relocatable files only. */ |
0e1862bb | 3586 | if (link_info == NULL || bfd_link_relocatable (link_info)) |
252b5132 | 3587 | { |
ef53be89 | 3588 | size_t reloc_count = 0; |
14f2c699 | 3589 | |
da9f89d4 | 3590 | /* Put SHT_GROUP sections first. */ |
04dd1667 | 3591 | for (sec = abfd->sections; sec != NULL; sec = sec->next) |
47cc2cf5 | 3592 | { |
5daa8fe7 | 3593 | d = elf_section_data (sec); |
da9f89d4 L |
3594 | |
3595 | if (d->this_hdr.sh_type == SHT_GROUP) | |
08a40648 | 3596 | { |
5daa8fe7 | 3597 | if (sec->flags & SEC_LINKER_CREATED) |
da9f89d4 L |
3598 | { |
3599 | /* Remove the linker created SHT_GROUP sections. */ | |
5daa8fe7 | 3600 | bfd_section_list_remove (abfd, sec); |
da9f89d4 | 3601 | abfd->section_count--; |
da9f89d4 | 3602 | } |
08a40648 | 3603 | else |
4fbb74a6 | 3604 | d->this_idx = section_number++; |
da9f89d4 | 3605 | } |
14f2c699 L |
3606 | |
3607 | /* Count relocations. */ | |
3608 | reloc_count += sec->reloc_count; | |
47cc2cf5 | 3609 | } |
14f2c699 L |
3610 | |
3611 | /* Clear HAS_RELOC if there are no relocations. */ | |
3612 | if (reloc_count == 0) | |
3613 | abfd->flags &= ~HAS_RELOC; | |
47cc2cf5 PB |
3614 | } |
3615 | ||
3616 | for (sec = abfd->sections; sec; sec = sec->next) | |
3617 | { | |
3618 | d = elf_section_data (sec); | |
3619 | ||
3620 | if (d->this_hdr.sh_type != SHT_GROUP) | |
4fbb74a6 | 3621 | d->this_idx = section_number++; |
3e19fb8f L |
3622 | if (d->this_hdr.sh_name != (unsigned int) -1) |
3623 | _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->this_hdr.sh_name); | |
d4730f92 | 3624 | if (d->rel.hdr) |
2b0f7ef9 | 3625 | { |
d4730f92 | 3626 | d->rel.idx = section_number++; |
3e19fb8f L |
3627 | if (d->rel.hdr->sh_name != (unsigned int) -1) |
3628 | _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel.hdr->sh_name); | |
2b0f7ef9 | 3629 | } |
d4730f92 BS |
3630 | else |
3631 | d->rel.idx = 0; | |
23bc299b | 3632 | |
d4730f92 | 3633 | if (d->rela.hdr) |
2b0f7ef9 | 3634 | { |
d4730f92 | 3635 | d->rela.idx = section_number++; |
3e19fb8f L |
3636 | if (d->rela.hdr->sh_name != (unsigned int) -1) |
3637 | _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rela.hdr->sh_name); | |
2b0f7ef9 | 3638 | } |
23bc299b | 3639 | else |
d4730f92 | 3640 | d->rela.idx = 0; |
252b5132 RH |
3641 | } |
3642 | ||
3516e984 L |
3643 | need_symtab = (bfd_get_symcount (abfd) > 0 |
3644 | || (link_info == NULL | |
3645 | && ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC)) | |
3646 | == HAS_RELOC))); | |
3647 | if (need_symtab) | |
252b5132 | 3648 | { |
12bd6957 | 3649 | elf_onesymtab (abfd) = section_number++; |
2b0f7ef9 | 3650 | _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->symtab_hdr.sh_name); |
4fbb74a6 | 3651 | if (section_number > ((SHN_LORESERVE - 2) & 0xFFFF)) |
9ad5cbcf | 3652 | { |
6a40cf0c NC |
3653 | elf_section_list * entry; |
3654 | ||
3655 | BFD_ASSERT (elf_symtab_shndx_list (abfd) == NULL); | |
3656 | ||
3657 | entry = bfd_zalloc (abfd, sizeof * entry); | |
3658 | entry->ndx = section_number++; | |
3659 | elf_symtab_shndx_list (abfd) = entry; | |
3660 | entry->hdr.sh_name | |
9ad5cbcf | 3661 | = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), |
b34976b6 | 3662 | ".symtab_shndx", FALSE); |
6a40cf0c | 3663 | if (entry->hdr.sh_name == (unsigned int) -1) |
b34976b6 | 3664 | return FALSE; |
9ad5cbcf | 3665 | } |
12bd6957 | 3666 | elf_strtab_sec (abfd) = section_number++; |
2b0f7ef9 | 3667 | _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->strtab_hdr.sh_name); |
252b5132 RH |
3668 | } |
3669 | ||
dd905818 NC |
3670 | elf_shstrtab_sec (abfd) = section_number++; |
3671 | _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->shstrtab_hdr.sh_name); | |
3672 | elf_elfheader (abfd)->e_shstrndx = elf_shstrtab_sec (abfd); | |
3673 | ||
1c52a645 L |
3674 | if (section_number >= SHN_LORESERVE) |
3675 | { | |
695344c0 | 3676 | /* xgettext:c-format */ |
1c52a645 L |
3677 | _bfd_error_handler (_("%B: too many sections: %u"), |
3678 | abfd, section_number); | |
3679 | return FALSE; | |
3680 | } | |
3681 | ||
9ad5cbcf | 3682 | elf_numsections (abfd) = section_number; |
252b5132 RH |
3683 | elf_elfheader (abfd)->e_shnum = section_number; |
3684 | ||
3685 | /* Set up the list of section header pointers, in agreement with the | |
3686 | indices. */ | |
a50b1753 NC |
3687 | i_shdrp = (Elf_Internal_Shdr **) bfd_zalloc2 (abfd, section_number, |
3688 | sizeof (Elf_Internal_Shdr *)); | |
252b5132 | 3689 | if (i_shdrp == NULL) |
b34976b6 | 3690 | return FALSE; |
252b5132 | 3691 | |
a50b1753 NC |
3692 | i_shdrp[0] = (Elf_Internal_Shdr *) bfd_zalloc (abfd, |
3693 | sizeof (Elf_Internal_Shdr)); | |
252b5132 RH |
3694 | if (i_shdrp[0] == NULL) |
3695 | { | |
3696 | bfd_release (abfd, i_shdrp); | |
b34976b6 | 3697 | return FALSE; |
252b5132 | 3698 | } |
252b5132 RH |
3699 | |
3700 | elf_elfsections (abfd) = i_shdrp; | |
3701 | ||
12bd6957 | 3702 | i_shdrp[elf_shstrtab_sec (abfd)] = &t->shstrtab_hdr; |
3516e984 | 3703 | if (need_symtab) |
252b5132 | 3704 | { |
12bd6957 | 3705 | i_shdrp[elf_onesymtab (abfd)] = &t->symtab_hdr; |
4fbb74a6 | 3706 | if (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF)) |
9ad5cbcf | 3707 | { |
6a40cf0c NC |
3708 | elf_section_list * entry = elf_symtab_shndx_list (abfd); |
3709 | BFD_ASSERT (entry != NULL); | |
3710 | i_shdrp[entry->ndx] = & entry->hdr; | |
3711 | entry->hdr.sh_link = elf_onesymtab (abfd); | |
9ad5cbcf | 3712 | } |
12bd6957 AM |
3713 | i_shdrp[elf_strtab_sec (abfd)] = &t->strtab_hdr; |
3714 | t->symtab_hdr.sh_link = elf_strtab_sec (abfd); | |
252b5132 | 3715 | } |
38ce5b11 | 3716 | |
252b5132 RH |
3717 | for (sec = abfd->sections; sec; sec = sec->next) |
3718 | { | |
252b5132 | 3719 | asection *s; |
252b5132 | 3720 | |
91d6fa6a NC |
3721 | d = elf_section_data (sec); |
3722 | ||
252b5132 | 3723 | i_shdrp[d->this_idx] = &d->this_hdr; |
d4730f92 BS |
3724 | if (d->rel.idx != 0) |
3725 | i_shdrp[d->rel.idx] = d->rel.hdr; | |
3726 | if (d->rela.idx != 0) | |
3727 | i_shdrp[d->rela.idx] = d->rela.hdr; | |
252b5132 RH |
3728 | |
3729 | /* Fill in the sh_link and sh_info fields while we're at it. */ | |
3730 | ||
3731 | /* sh_link of a reloc section is the section index of the symbol | |
3732 | table. sh_info is the section index of the section to which | |
3733 | the relocation entries apply. */ | |
d4730f92 | 3734 | if (d->rel.idx != 0) |
252b5132 | 3735 | { |
12bd6957 | 3736 | d->rel.hdr->sh_link = elf_onesymtab (abfd); |
d4730f92 | 3737 | d->rel.hdr->sh_info = d->this_idx; |
9ef5d938 | 3738 | d->rel.hdr->sh_flags |= SHF_INFO_LINK; |
252b5132 | 3739 | } |
d4730f92 | 3740 | if (d->rela.idx != 0) |
23bc299b | 3741 | { |
12bd6957 | 3742 | d->rela.hdr->sh_link = elf_onesymtab (abfd); |
d4730f92 | 3743 | d->rela.hdr->sh_info = d->this_idx; |
9ef5d938 | 3744 | d->rela.hdr->sh_flags |= SHF_INFO_LINK; |
23bc299b | 3745 | } |
252b5132 | 3746 | |
38ce5b11 L |
3747 | /* We need to set up sh_link for SHF_LINK_ORDER. */ |
3748 | if ((d->this_hdr.sh_flags & SHF_LINK_ORDER) != 0) | |
3749 | { | |
3750 | s = elf_linked_to_section (sec); | |
3751 | if (s) | |
38ce5b11 | 3752 | { |
f2876037 | 3753 | /* elf_linked_to_section points to the input section. */ |
ccd2ec6a | 3754 | if (link_info != NULL) |
38ce5b11 | 3755 | { |
f2876037 | 3756 | /* Check discarded linkonce section. */ |
dbaa2011 | 3757 | if (discarded_section (s)) |
38ce5b11 | 3758 | { |
ccd2ec6a | 3759 | asection *kept; |
4eca0228 | 3760 | _bfd_error_handler |
695344c0 | 3761 | /* xgettext:c-format */ |
ccd2ec6a L |
3762 | (_("%B: sh_link of section `%A' points to discarded section `%A' of `%B'"), |
3763 | abfd, d->this_hdr.bfd_section, | |
3764 | s, s->owner); | |
3765 | /* Point to the kept section if it has the same | |
3766 | size as the discarded one. */ | |
c0f00686 | 3767 | kept = _bfd_elf_check_kept_section (s, link_info); |
ccd2ec6a | 3768 | if (kept == NULL) |
185d09ad | 3769 | { |
ccd2ec6a L |
3770 | bfd_set_error (bfd_error_bad_value); |
3771 | return FALSE; | |
185d09ad | 3772 | } |
ccd2ec6a | 3773 | s = kept; |
38ce5b11 | 3774 | } |
e424ecc8 | 3775 | |
ccd2ec6a L |
3776 | s = s->output_section; |
3777 | BFD_ASSERT (s != NULL); | |
38ce5b11 | 3778 | } |
f2876037 L |
3779 | else |
3780 | { | |
3781 | /* Handle objcopy. */ | |
3782 | if (s->output_section == NULL) | |
3783 | { | |
4eca0228 | 3784 | _bfd_error_handler |
695344c0 | 3785 | /* xgettext:c-format */ |
f2876037 L |
3786 | (_("%B: sh_link of section `%A' points to removed section `%A' of `%B'"), |
3787 | abfd, d->this_hdr.bfd_section, s, s->owner); | |
3788 | bfd_set_error (bfd_error_bad_value); | |
3789 | return FALSE; | |
3790 | } | |
3791 | s = s->output_section; | |
3792 | } | |
ccd2ec6a L |
3793 | d->this_hdr.sh_link = elf_section_data (s)->this_idx; |
3794 | } | |
3795 | else | |
3796 | { | |
3797 | /* PR 290: | |
3798 | The Intel C compiler generates SHT_IA_64_UNWIND with | |
3799 | SHF_LINK_ORDER. But it doesn't set the sh_link or | |
3800 | sh_info fields. Hence we could get the situation | |
08a40648 | 3801 | where s is NULL. */ |
ccd2ec6a L |
3802 | const struct elf_backend_data *bed |
3803 | = get_elf_backend_data (abfd); | |
3804 | if (bed->link_order_error_handler) | |
3805 | bed->link_order_error_handler | |
695344c0 | 3806 | /* xgettext:c-format */ |
ccd2ec6a L |
3807 | (_("%B: warning: sh_link not set for section `%A'"), |
3808 | abfd, sec); | |
38ce5b11 L |
3809 | } |
3810 | } | |
3811 | ||
252b5132 RH |
3812 | switch (d->this_hdr.sh_type) |
3813 | { | |
3814 | case SHT_REL: | |
3815 | case SHT_RELA: | |
3816 | /* A reloc section which we are treating as a normal BFD | |
3817 | section. sh_link is the section index of the symbol | |
3818 | table. sh_info is the section index of the section to | |
3819 | which the relocation entries apply. We assume that an | |
3820 | allocated reloc section uses the dynamic symbol table. | |
3821 | FIXME: How can we be sure? */ | |
3822 | s = bfd_get_section_by_name (abfd, ".dynsym"); | |
3823 | if (s != NULL) | |
3824 | d->this_hdr.sh_link = elf_section_data (s)->this_idx; | |
3825 | ||
bd53a53a | 3826 | s = get_elf_backend_data (abfd)->get_reloc_section (sec); |
252b5132 | 3827 | if (s != NULL) |
9ef5d938 L |
3828 | { |
3829 | d->this_hdr.sh_info = elf_section_data (s)->this_idx; | |
3830 | d->this_hdr.sh_flags |= SHF_INFO_LINK; | |
3831 | } | |
252b5132 RH |
3832 | break; |
3833 | ||
3834 | case SHT_STRTAB: | |
3835 | /* We assume that a section named .stab*str is a stabs | |
3836 | string section. We look for a section with the same name | |
3837 | but without the trailing ``str'', and set its sh_link | |
3838 | field to point to this section. */ | |
0112cd26 | 3839 | if (CONST_STRNEQ (sec->name, ".stab") |
252b5132 RH |
3840 | && strcmp (sec->name + strlen (sec->name) - 3, "str") == 0) |
3841 | { | |
3842 | size_t len; | |
3843 | char *alc; | |
3844 | ||
3845 | len = strlen (sec->name); | |
a50b1753 | 3846 | alc = (char *) bfd_malloc (len - 2); |
252b5132 | 3847 | if (alc == NULL) |
b34976b6 | 3848 | return FALSE; |
d4c88bbb | 3849 | memcpy (alc, sec->name, len - 3); |
252b5132 RH |
3850 | alc[len - 3] = '\0'; |
3851 | s = bfd_get_section_by_name (abfd, alc); | |
3852 | free (alc); | |
3853 | if (s != NULL) | |
3854 | { | |
3855 | elf_section_data (s)->this_hdr.sh_link = d->this_idx; | |
3856 | ||
3857 | /* This is a .stab section. */ | |
0594c12d AM |
3858 | if (elf_section_data (s)->this_hdr.sh_entsize == 0) |
3859 | elf_section_data (s)->this_hdr.sh_entsize | |
3860 | = 4 + 2 * bfd_get_arch_size (abfd) / 8; | |
252b5132 RH |
3861 | } |
3862 | } | |
3863 | break; | |
3864 | ||
3865 | case SHT_DYNAMIC: | |
3866 | case SHT_DYNSYM: | |
3867 | case SHT_GNU_verneed: | |
3868 | case SHT_GNU_verdef: | |
3869 | /* sh_link is the section header index of the string table | |
3870 | used for the dynamic entries, or the symbol table, or the | |
3871 | version strings. */ | |
3872 | s = bfd_get_section_by_name (abfd, ".dynstr"); | |
3873 | if (s != NULL) | |
3874 | d->this_hdr.sh_link = elf_section_data (s)->this_idx; | |
3875 | break; | |
3876 | ||
7f1204bb JJ |
3877 | case SHT_GNU_LIBLIST: |
3878 | /* sh_link is the section header index of the prelink library | |
08a40648 AM |
3879 | list used for the dynamic entries, or the symbol table, or |
3880 | the version strings. */ | |
7f1204bb JJ |
3881 | s = bfd_get_section_by_name (abfd, (sec->flags & SEC_ALLOC) |
3882 | ? ".dynstr" : ".gnu.libstr"); | |
3883 | if (s != NULL) | |
3884 | d->this_hdr.sh_link = elf_section_data (s)->this_idx; | |
3885 | break; | |
3886 | ||
252b5132 | 3887 | case SHT_HASH: |
fdc90cb4 | 3888 | case SHT_GNU_HASH: |
252b5132 RH |
3889 | case SHT_GNU_versym: |
3890 | /* sh_link is the section header index of the symbol table | |
3891 | this hash table or version table is for. */ | |
3892 | s = bfd_get_section_by_name (abfd, ".dynsym"); | |
3893 | if (s != NULL) | |
3894 | d->this_hdr.sh_link = elf_section_data (s)->this_idx; | |
3895 | break; | |
dbb410c3 AM |
3896 | |
3897 | case SHT_GROUP: | |
12bd6957 | 3898 | d->this_hdr.sh_link = elf_onesymtab (abfd); |
252b5132 RH |
3899 | } |
3900 | } | |
3901 | ||
3e19fb8f L |
3902 | /* Delay setting sh_name to _bfd_elf_write_object_contents so that |
3903 | _bfd_elf_assign_file_positions_for_non_load can convert DWARF | |
3904 | debug section name from .debug_* to .zdebug_* if needed. */ | |
3905 | ||
b34976b6 | 3906 | return TRUE; |
252b5132 RH |
3907 | } |
3908 | ||
5372391b | 3909 | static bfd_boolean |
217aa764 | 3910 | sym_is_global (bfd *abfd, asymbol *sym) |
252b5132 RH |
3911 | { |
3912 | /* If the backend has a special mapping, use it. */ | |
9c5bfbb7 | 3913 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
217aa764 AM |
3914 | if (bed->elf_backend_sym_is_global) |
3915 | return (*bed->elf_backend_sym_is_global) (abfd, sym); | |
252b5132 | 3916 | |
e47bf690 | 3917 | return ((sym->flags & (BSF_GLOBAL | BSF_WEAK | BSF_GNU_UNIQUE)) != 0 |
252b5132 RH |
3918 | || bfd_is_und_section (bfd_get_section (sym)) |
3919 | || bfd_is_com_section (bfd_get_section (sym))); | |
3920 | } | |
3921 | ||
76359541 TP |
3922 | /* Filter global symbols of ABFD to include in the import library. All |
3923 | SYMCOUNT symbols of ABFD can be examined from their pointers in | |
3924 | SYMS. Pointers of symbols to keep should be stored contiguously at | |
3925 | the beginning of that array. | |
3926 | ||
3927 | Returns the number of symbols to keep. */ | |
3928 | ||
3929 | unsigned int | |
3930 | _bfd_elf_filter_global_symbols (bfd *abfd, struct bfd_link_info *info, | |
3931 | asymbol **syms, long symcount) | |
3932 | { | |
3933 | long src_count, dst_count = 0; | |
3934 | ||
3935 | for (src_count = 0; src_count < symcount; src_count++) | |
3936 | { | |
3937 | asymbol *sym = syms[src_count]; | |
3938 | char *name = (char *) bfd_asymbol_name (sym); | |
3939 | struct bfd_link_hash_entry *h; | |
3940 | ||
3941 | if (!sym_is_global (abfd, sym)) | |
3942 | continue; | |
3943 | ||
3944 | h = bfd_link_hash_lookup (info->hash, name, FALSE, FALSE, FALSE); | |
5df1bc57 AM |
3945 | if (h == NULL) |
3946 | continue; | |
76359541 TP |
3947 | if (h->type != bfd_link_hash_defined && h->type != bfd_link_hash_defweak) |
3948 | continue; | |
76359541 TP |
3949 | if (h->linker_def || h->ldscript_def) |
3950 | continue; | |
3951 | ||
3952 | syms[dst_count++] = sym; | |
3953 | } | |
3954 | ||
3955 | syms[dst_count] = NULL; | |
3956 | ||
3957 | return dst_count; | |
3958 | } | |
3959 | ||
5372391b | 3960 | /* Don't output section symbols for sections that are not going to be |
c6d8cab4 | 3961 | output, that are duplicates or there is no BFD section. */ |
5372391b AM |
3962 | |
3963 | static bfd_boolean | |
3964 | ignore_section_sym (bfd *abfd, asymbol *sym) | |
3965 | { | |
c6d8cab4 L |
3966 | elf_symbol_type *type_ptr; |
3967 | ||
3968 | if ((sym->flags & BSF_SECTION_SYM) == 0) | |
3969 | return FALSE; | |
3970 | ||
3971 | type_ptr = elf_symbol_from (abfd, sym); | |
3972 | return ((type_ptr != NULL | |
3973 | && type_ptr->internal_elf_sym.st_shndx != 0 | |
3974 | && bfd_is_abs_section (sym->section)) | |
3975 | || !(sym->section->owner == abfd | |
0f0a5e58 | 3976 | || (sym->section->output_section->owner == abfd |
2633a79c AM |
3977 | && sym->section->output_offset == 0) |
3978 | || bfd_is_abs_section (sym->section))); | |
5372391b AM |
3979 | } |
3980 | ||
2633a79c AM |
3981 | /* Map symbol from it's internal number to the external number, moving |
3982 | all local symbols to be at the head of the list. */ | |
3983 | ||
b34976b6 | 3984 | static bfd_boolean |
12bd6957 | 3985 | elf_map_symbols (bfd *abfd, unsigned int *pnum_locals) |
252b5132 | 3986 | { |
dc810e39 | 3987 | unsigned int symcount = bfd_get_symcount (abfd); |
252b5132 RH |
3988 | asymbol **syms = bfd_get_outsymbols (abfd); |
3989 | asymbol **sect_syms; | |
dc810e39 AM |
3990 | unsigned int num_locals = 0; |
3991 | unsigned int num_globals = 0; | |
3992 | unsigned int num_locals2 = 0; | |
3993 | unsigned int num_globals2 = 0; | |
7292b3ac | 3994 | unsigned int max_index = 0; |
dc810e39 | 3995 | unsigned int idx; |
252b5132 RH |
3996 | asection *asect; |
3997 | asymbol **new_syms; | |
252b5132 RH |
3998 | |
3999 | #ifdef DEBUG | |
4000 | fprintf (stderr, "elf_map_symbols\n"); | |
4001 | fflush (stderr); | |
4002 | #endif | |
4003 | ||
252b5132 RH |
4004 | for (asect = abfd->sections; asect; asect = asect->next) |
4005 | { | |
4006 | if (max_index < asect->index) | |
4007 | max_index = asect->index; | |
4008 | } | |
4009 | ||
4010 | max_index++; | |
a50b1753 | 4011 | sect_syms = (asymbol **) bfd_zalloc2 (abfd, max_index, sizeof (asymbol *)); |
252b5132 | 4012 | if (sect_syms == NULL) |
b34976b6 | 4013 | return FALSE; |
252b5132 | 4014 | elf_section_syms (abfd) = sect_syms; |
4e89ac30 | 4015 | elf_num_section_syms (abfd) = max_index; |
252b5132 | 4016 | |
079e9a2f AM |
4017 | /* Init sect_syms entries for any section symbols we have already |
4018 | decided to output. */ | |
252b5132 RH |
4019 | for (idx = 0; idx < symcount; idx++) |
4020 | { | |
dc810e39 | 4021 | asymbol *sym = syms[idx]; |
c044fabd | 4022 | |
252b5132 | 4023 | if ((sym->flags & BSF_SECTION_SYM) != 0 |
0f0a5e58 | 4024 | && sym->value == 0 |
2633a79c AM |
4025 | && !ignore_section_sym (abfd, sym) |
4026 | && !bfd_is_abs_section (sym->section)) | |
252b5132 | 4027 | { |
5372391b | 4028 | asection *sec = sym->section; |
252b5132 | 4029 | |
5372391b AM |
4030 | if (sec->owner != abfd) |
4031 | sec = sec->output_section; | |
252b5132 | 4032 | |
5372391b | 4033 | sect_syms[sec->index] = syms[idx]; |
252b5132 RH |
4034 | } |
4035 | } | |
4036 | ||
252b5132 RH |
4037 | /* Classify all of the symbols. */ |
4038 | for (idx = 0; idx < symcount; idx++) | |
4039 | { | |
2633a79c | 4040 | if (sym_is_global (abfd, syms[idx])) |
252b5132 | 4041 | num_globals++; |
2633a79c AM |
4042 | else if (!ignore_section_sym (abfd, syms[idx])) |
4043 | num_locals++; | |
252b5132 | 4044 | } |
079e9a2f | 4045 | |
5372391b | 4046 | /* We will be adding a section symbol for each normal BFD section. Most |
079e9a2f AM |
4047 | sections will already have a section symbol in outsymbols, but |
4048 | eg. SHT_GROUP sections will not, and we need the section symbol mapped | |
4049 | at least in that case. */ | |
252b5132 RH |
4050 | for (asect = abfd->sections; asect; asect = asect->next) |
4051 | { | |
079e9a2f | 4052 | if (sect_syms[asect->index] == NULL) |
252b5132 | 4053 | { |
079e9a2f | 4054 | if (!sym_is_global (abfd, asect->symbol)) |
252b5132 RH |
4055 | num_locals++; |
4056 | else | |
4057 | num_globals++; | |
252b5132 RH |
4058 | } |
4059 | } | |
4060 | ||
4061 | /* Now sort the symbols so the local symbols are first. */ | |
a50b1753 NC |
4062 | new_syms = (asymbol **) bfd_alloc2 (abfd, num_locals + num_globals, |
4063 | sizeof (asymbol *)); | |
dc810e39 | 4064 | |
252b5132 | 4065 | if (new_syms == NULL) |
b34976b6 | 4066 | return FALSE; |
252b5132 RH |
4067 | |
4068 | for (idx = 0; idx < symcount; idx++) | |
4069 | { | |
4070 | asymbol *sym = syms[idx]; | |
dc810e39 | 4071 | unsigned int i; |
252b5132 | 4072 | |
2633a79c AM |
4073 | if (sym_is_global (abfd, sym)) |
4074 | i = num_locals + num_globals2++; | |
4075 | else if (!ignore_section_sym (abfd, sym)) | |
252b5132 RH |
4076 | i = num_locals2++; |
4077 | else | |
2633a79c | 4078 | continue; |
252b5132 RH |
4079 | new_syms[i] = sym; |
4080 | sym->udata.i = i + 1; | |
4081 | } | |
4082 | for (asect = abfd->sections; asect; asect = asect->next) | |
4083 | { | |
079e9a2f | 4084 | if (sect_syms[asect->index] == NULL) |
252b5132 | 4085 | { |
079e9a2f | 4086 | asymbol *sym = asect->symbol; |
dc810e39 | 4087 | unsigned int i; |
252b5132 | 4088 | |
079e9a2f | 4089 | sect_syms[asect->index] = sym; |
252b5132 RH |
4090 | if (!sym_is_global (abfd, sym)) |
4091 | i = num_locals2++; | |
4092 | else | |
4093 | i = num_locals + num_globals2++; | |
4094 | new_syms[i] = sym; | |
4095 | sym->udata.i = i + 1; | |
4096 | } | |
4097 | } | |
4098 | ||
4099 | bfd_set_symtab (abfd, new_syms, num_locals + num_globals); | |
4100 | ||
12bd6957 | 4101 | *pnum_locals = num_locals; |
b34976b6 | 4102 | return TRUE; |
252b5132 RH |
4103 | } |
4104 | ||
4105 | /* Align to the maximum file alignment that could be required for any | |
4106 | ELF data structure. */ | |
4107 | ||
268b6b39 | 4108 | static inline file_ptr |
217aa764 | 4109 | align_file_position (file_ptr off, int align) |
252b5132 RH |
4110 | { |
4111 | return (off + align - 1) & ~(align - 1); | |
4112 | } | |
4113 | ||
4114 | /* Assign a file position to a section, optionally aligning to the | |
4115 | required section alignment. */ | |
4116 | ||
217aa764 AM |
4117 | file_ptr |
4118 | _bfd_elf_assign_file_position_for_section (Elf_Internal_Shdr *i_shdrp, | |
4119 | file_ptr offset, | |
4120 | bfd_boolean align) | |
252b5132 | 4121 | { |
72de5009 AM |
4122 | if (align && i_shdrp->sh_addralign > 1) |
4123 | offset = BFD_ALIGN (offset, i_shdrp->sh_addralign); | |
252b5132 RH |
4124 | i_shdrp->sh_offset = offset; |
4125 | if (i_shdrp->bfd_section != NULL) | |
4126 | i_shdrp->bfd_section->filepos = offset; | |
4127 | if (i_shdrp->sh_type != SHT_NOBITS) | |
4128 | offset += i_shdrp->sh_size; | |
4129 | return offset; | |
4130 | } | |
4131 | ||
4132 | /* Compute the file positions we are going to put the sections at, and | |
4133 | otherwise prepare to begin writing out the ELF file. If LINK_INFO | |
4134 | is not NULL, this is being called by the ELF backend linker. */ | |
4135 | ||
b34976b6 | 4136 | bfd_boolean |
217aa764 AM |
4137 | _bfd_elf_compute_section_file_positions (bfd *abfd, |
4138 | struct bfd_link_info *link_info) | |
252b5132 | 4139 | { |
9c5bfbb7 | 4140 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
d4730f92 | 4141 | struct fake_section_arg fsargs; |
b34976b6 | 4142 | bfd_boolean failed; |
ef10c3ac | 4143 | struct elf_strtab_hash *strtab = NULL; |
252b5132 | 4144 | Elf_Internal_Shdr *shstrtab_hdr; |
3516e984 | 4145 | bfd_boolean need_symtab; |
252b5132 RH |
4146 | |
4147 | if (abfd->output_has_begun) | |
b34976b6 | 4148 | return TRUE; |
252b5132 RH |
4149 | |
4150 | /* Do any elf backend specific processing first. */ | |
4151 | if (bed->elf_backend_begin_write_processing) | |
4152 | (*bed->elf_backend_begin_write_processing) (abfd, link_info); | |
4153 | ||
4154 | if (! prep_headers (abfd)) | |
b34976b6 | 4155 | return FALSE; |
252b5132 | 4156 | |
e6c51ed4 | 4157 | /* Post process the headers if necessary. */ |
78245035 | 4158 | (*bed->elf_backend_post_process_headers) (abfd, link_info); |
e6c51ed4 | 4159 | |
d4730f92 BS |
4160 | fsargs.failed = FALSE; |
4161 | fsargs.link_info = link_info; | |
4162 | bfd_map_over_sections (abfd, elf_fake_sections, &fsargs); | |
4163 | if (fsargs.failed) | |
b34976b6 | 4164 | return FALSE; |
252b5132 | 4165 | |
da9f89d4 | 4166 | if (!assign_section_numbers (abfd, link_info)) |
b34976b6 | 4167 | return FALSE; |
252b5132 RH |
4168 | |
4169 | /* The backend linker builds symbol table information itself. */ | |
3516e984 L |
4170 | need_symtab = (link_info == NULL |
4171 | && (bfd_get_symcount (abfd) > 0 | |
4172 | || ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC)) | |
4173 | == HAS_RELOC))); | |
4174 | if (need_symtab) | |
252b5132 RH |
4175 | { |
4176 | /* Non-zero if doing a relocatable link. */ | |
4177 | int relocatable_p = ! (abfd->flags & (EXEC_P | DYNAMIC)); | |
4178 | ||
4179 | if (! swap_out_syms (abfd, &strtab, relocatable_p)) | |
b34976b6 | 4180 | return FALSE; |
252b5132 RH |
4181 | } |
4182 | ||
d4730f92 | 4183 | failed = FALSE; |
1126897b | 4184 | if (link_info == NULL) |
dbb410c3 | 4185 | { |
1126897b | 4186 | bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed); |
dbb410c3 | 4187 | if (failed) |
b34976b6 | 4188 | return FALSE; |
dbb410c3 AM |
4189 | } |
4190 | ||
252b5132 RH |
4191 | shstrtab_hdr = &elf_tdata (abfd)->shstrtab_hdr; |
4192 | /* sh_name was set in prep_headers. */ | |
4193 | shstrtab_hdr->sh_type = SHT_STRTAB; | |
84865015 | 4194 | shstrtab_hdr->sh_flags = bed->elf_strtab_flags; |
252b5132 | 4195 | shstrtab_hdr->sh_addr = 0; |
946748d5 | 4196 | /* sh_size is set in _bfd_elf_assign_file_positions_for_non_load. */ |
252b5132 RH |
4197 | shstrtab_hdr->sh_entsize = 0; |
4198 | shstrtab_hdr->sh_link = 0; | |
4199 | shstrtab_hdr->sh_info = 0; | |
3e19fb8f | 4200 | /* sh_offset is set in _bfd_elf_assign_file_positions_for_non_load. */ |
252b5132 RH |
4201 | shstrtab_hdr->sh_addralign = 1; |
4202 | ||
c84fca4d | 4203 | if (!assign_file_positions_except_relocs (abfd, link_info)) |
b34976b6 | 4204 | return FALSE; |
252b5132 | 4205 | |
3516e984 | 4206 | if (need_symtab) |
252b5132 RH |
4207 | { |
4208 | file_ptr off; | |
4209 | Elf_Internal_Shdr *hdr; | |
4210 | ||
12bd6957 | 4211 | off = elf_next_file_pos (abfd); |
252b5132 | 4212 | |
6a40cf0c | 4213 | hdr = & elf_symtab_hdr (abfd); |
b34976b6 | 4214 | off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE); |
252b5132 | 4215 | |
6a40cf0c NC |
4216 | if (elf_symtab_shndx_list (abfd) != NULL) |
4217 | { | |
4218 | hdr = & elf_symtab_shndx_list (abfd)->hdr; | |
4219 | if (hdr->sh_size != 0) | |
4220 | off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE); | |
4221 | /* FIXME: What about other symtab_shndx sections in the list ? */ | |
4222 | } | |
9ad5cbcf | 4223 | |
252b5132 | 4224 | hdr = &elf_tdata (abfd)->strtab_hdr; |
b34976b6 | 4225 | off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE); |
252b5132 | 4226 | |
12bd6957 | 4227 | elf_next_file_pos (abfd) = off; |
252b5132 RH |
4228 | |
4229 | /* Now that we know where the .strtab section goes, write it | |
08a40648 | 4230 | out. */ |
252b5132 | 4231 | if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0 |
ef10c3ac | 4232 | || ! _bfd_elf_strtab_emit (abfd, strtab)) |
b34976b6 | 4233 | return FALSE; |
ef10c3ac | 4234 | _bfd_elf_strtab_free (strtab); |
252b5132 RH |
4235 | } |
4236 | ||
b34976b6 | 4237 | abfd->output_has_begun = TRUE; |
252b5132 | 4238 | |
b34976b6 | 4239 | return TRUE; |
252b5132 RH |
4240 | } |
4241 | ||
8ded5a0f AM |
4242 | /* Make an initial estimate of the size of the program header. If we |
4243 | get the number wrong here, we'll redo section placement. */ | |
4244 | ||
4245 | static bfd_size_type | |
4246 | get_program_header_size (bfd *abfd, struct bfd_link_info *info) | |
4247 | { | |
4248 | size_t segs; | |
4249 | asection *s; | |
2b05f1b7 | 4250 | const struct elf_backend_data *bed; |
8ded5a0f AM |
4251 | |
4252 | /* Assume we will need exactly two PT_LOAD segments: one for text | |
4253 | and one for data. */ | |
4254 | segs = 2; | |
4255 | ||
4256 | s = bfd_get_section_by_name (abfd, ".interp"); | |
4257 | if (s != NULL && (s->flags & SEC_LOAD) != 0) | |
4258 | { | |
4259 | /* If we have a loadable interpreter section, we need a | |
4260 | PT_INTERP segment. In this case, assume we also need a | |
4261 | PT_PHDR segment, although that may not be true for all | |
4262 | targets. */ | |
e9a38e0f | 4263 | segs += 2; |
8ded5a0f AM |
4264 | } |
4265 | ||
4266 | if (bfd_get_section_by_name (abfd, ".dynamic") != NULL) | |
4267 | { | |
4268 | /* We need a PT_DYNAMIC segment. */ | |
4269 | ++segs; | |
f210dcff | 4270 | } |
08a40648 | 4271 | |
ceae84aa | 4272 | if (info != NULL && info->relro) |
f210dcff L |
4273 | { |
4274 | /* We need a PT_GNU_RELRO segment. */ | |
4275 | ++segs; | |
8ded5a0f AM |
4276 | } |
4277 | ||
12bd6957 | 4278 | if (elf_eh_frame_hdr (abfd)) |
8ded5a0f AM |
4279 | { |
4280 | /* We need a PT_GNU_EH_FRAME segment. */ | |
4281 | ++segs; | |
4282 | } | |
4283 | ||
12bd6957 | 4284 | if (elf_stack_flags (abfd)) |
8ded5a0f | 4285 | { |
2b05f1b7 L |
4286 | /* We need a PT_GNU_STACK segment. */ |
4287 | ++segs; | |
4288 | } | |
94b11780 | 4289 | |
2b05f1b7 L |
4290 | for (s = abfd->sections; s != NULL; s = s->next) |
4291 | { | |
8ded5a0f | 4292 | if ((s->flags & SEC_LOAD) != 0 |
0112cd26 | 4293 | && CONST_STRNEQ (s->name, ".note")) |
8ded5a0f AM |
4294 | { |
4295 | /* We need a PT_NOTE segment. */ | |
4296 | ++segs; | |
1c5265b5 JJ |
4297 | /* Try to create just one PT_NOTE segment |
4298 | for all adjacent loadable .note* sections. | |
4299 | gABI requires that within a PT_NOTE segment | |
4300 | (and also inside of each SHT_NOTE section) | |
4301 | each note is padded to a multiple of 4 size, | |
4302 | so we check whether the sections are correctly | |
4303 | aligned. */ | |
4304 | if (s->alignment_power == 2) | |
4305 | while (s->next != NULL | |
4306 | && s->next->alignment_power == 2 | |
4307 | && (s->next->flags & SEC_LOAD) != 0 | |
4308 | && CONST_STRNEQ (s->next->name, ".note")) | |
4309 | s = s->next; | |
8ded5a0f AM |
4310 | } |
4311 | } | |
4312 | ||
4313 | for (s = abfd->sections; s != NULL; s = s->next) | |
4314 | { | |
4315 | if (s->flags & SEC_THREAD_LOCAL) | |
4316 | { | |
4317 | /* We need a PT_TLS segment. */ | |
4318 | ++segs; | |
4319 | break; | |
4320 | } | |
4321 | } | |
4322 | ||
4323 | /* Let the backend count up any program headers it might need. */ | |
2b05f1b7 | 4324 | bed = get_elf_backend_data (abfd); |
8ded5a0f AM |
4325 | if (bed->elf_backend_additional_program_headers) |
4326 | { | |
4327 | int a; | |
4328 | ||
4329 | a = (*bed->elf_backend_additional_program_headers) (abfd, info); | |
4330 | if (a == -1) | |
4331 | abort (); | |
4332 | segs += a; | |
4333 | } | |
4334 | ||
4335 | return segs * bed->s->sizeof_phdr; | |
4336 | } | |
4337 | ||
2ea37f1c NC |
4338 | /* Find the segment that contains the output_section of section. */ |
4339 | ||
4340 | Elf_Internal_Phdr * | |
4341 | _bfd_elf_find_segment_containing_section (bfd * abfd, asection * section) | |
4342 | { | |
4343 | struct elf_segment_map *m; | |
4344 | Elf_Internal_Phdr *p; | |
4345 | ||
12bd6957 | 4346 | for (m = elf_seg_map (abfd), p = elf_tdata (abfd)->phdr; |
2ea37f1c NC |
4347 | m != NULL; |
4348 | m = m->next, p++) | |
4349 | { | |
4350 | int i; | |
4351 | ||
4352 | for (i = m->count - 1; i >= 0; i--) | |
4353 | if (m->sections[i] == section) | |
4354 | return p; | |
4355 | } | |
4356 | ||
4357 | return NULL; | |
4358 | } | |
4359 | ||
252b5132 RH |
4360 | /* Create a mapping from a set of sections to a program segment. */ |
4361 | ||
217aa764 AM |
4362 | static struct elf_segment_map * |
4363 | make_mapping (bfd *abfd, | |
4364 | asection **sections, | |
4365 | unsigned int from, | |
4366 | unsigned int to, | |
4367 | bfd_boolean phdr) | |
252b5132 RH |
4368 | { |
4369 | struct elf_segment_map *m; | |
4370 | unsigned int i; | |
4371 | asection **hdrpp; | |
dc810e39 | 4372 | bfd_size_type amt; |
252b5132 | 4373 | |
dc810e39 AM |
4374 | amt = sizeof (struct elf_segment_map); |
4375 | amt += (to - from - 1) * sizeof (asection *); | |
a50b1753 | 4376 | m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); |
252b5132 RH |
4377 | if (m == NULL) |
4378 | return NULL; | |
4379 | m->next = NULL; | |
4380 | m->p_type = PT_LOAD; | |
4381 | for (i = from, hdrpp = sections + from; i < to; i++, hdrpp++) | |
4382 | m->sections[i - from] = *hdrpp; | |
4383 | m->count = to - from; | |
4384 | ||
4385 | if (from == 0 && phdr) | |
4386 | { | |
4387 | /* Include the headers in the first PT_LOAD segment. */ | |
4388 | m->includes_filehdr = 1; | |
4389 | m->includes_phdrs = 1; | |
4390 | } | |
4391 | ||
4392 | return m; | |
4393 | } | |
4394 | ||
229fcec5 MM |
4395 | /* Create the PT_DYNAMIC segment, which includes DYNSEC. Returns NULL |
4396 | on failure. */ | |
4397 | ||
4398 | struct elf_segment_map * | |
4399 | _bfd_elf_make_dynamic_segment (bfd *abfd, asection *dynsec) | |
4400 | { | |
4401 | struct elf_segment_map *m; | |
4402 | ||
a50b1753 NC |
4403 | m = (struct elf_segment_map *) bfd_zalloc (abfd, |
4404 | sizeof (struct elf_segment_map)); | |
229fcec5 MM |
4405 | if (m == NULL) |
4406 | return NULL; | |
4407 | m->next = NULL; | |
4408 | m->p_type = PT_DYNAMIC; | |
4409 | m->count = 1; | |
4410 | m->sections[0] = dynsec; | |
08a40648 | 4411 | |
229fcec5 MM |
4412 | return m; |
4413 | } | |
4414 | ||
8ded5a0f | 4415 | /* Possibly add or remove segments from the segment map. */ |
252b5132 | 4416 | |
b34976b6 | 4417 | static bfd_boolean |
3dea8fca AM |
4418 | elf_modify_segment_map (bfd *abfd, |
4419 | struct bfd_link_info *info, | |
4420 | bfd_boolean remove_empty_load) | |
252b5132 | 4421 | { |
252e386e | 4422 | struct elf_segment_map **m; |
8ded5a0f | 4423 | const struct elf_backend_data *bed; |
252b5132 | 4424 | |
8ded5a0f AM |
4425 | /* The placement algorithm assumes that non allocated sections are |
4426 | not in PT_LOAD segments. We ensure this here by removing such | |
4427 | sections from the segment map. We also remove excluded | |
252e386e AM |
4428 | sections. Finally, any PT_LOAD segment without sections is |
4429 | removed. */ | |
12bd6957 | 4430 | m = &elf_seg_map (abfd); |
252e386e | 4431 | while (*m) |
8ded5a0f AM |
4432 | { |
4433 | unsigned int i, new_count; | |
252b5132 | 4434 | |
252e386e | 4435 | for (new_count = 0, i = 0; i < (*m)->count; i++) |
8ded5a0f | 4436 | { |
252e386e AM |
4437 | if (((*m)->sections[i]->flags & SEC_EXCLUDE) == 0 |
4438 | && (((*m)->sections[i]->flags & SEC_ALLOC) != 0 | |
4439 | || (*m)->p_type != PT_LOAD)) | |
8ded5a0f | 4440 | { |
252e386e AM |
4441 | (*m)->sections[new_count] = (*m)->sections[i]; |
4442 | new_count++; | |
8ded5a0f AM |
4443 | } |
4444 | } | |
252e386e | 4445 | (*m)->count = new_count; |
252b5132 | 4446 | |
1a9ccd70 NC |
4447 | if (remove_empty_load |
4448 | && (*m)->p_type == PT_LOAD | |
4449 | && (*m)->count == 0 | |
4450 | && !(*m)->includes_phdrs) | |
252e386e AM |
4451 | *m = (*m)->next; |
4452 | else | |
4453 | m = &(*m)->next; | |
8ded5a0f | 4454 | } |
252b5132 | 4455 | |
8ded5a0f AM |
4456 | bed = get_elf_backend_data (abfd); |
4457 | if (bed->elf_backend_modify_segment_map != NULL) | |
252b5132 | 4458 | { |
252e386e | 4459 | if (!(*bed->elf_backend_modify_segment_map) (abfd, info)) |
8ded5a0f | 4460 | return FALSE; |
252b5132 | 4461 | } |
252b5132 | 4462 | |
8ded5a0f AM |
4463 | return TRUE; |
4464 | } | |
252b5132 | 4465 | |
8ded5a0f | 4466 | /* Set up a mapping from BFD sections to program segments. */ |
252b5132 | 4467 | |
8ded5a0f AM |
4468 | bfd_boolean |
4469 | _bfd_elf_map_sections_to_segments (bfd *abfd, struct bfd_link_info *info) | |
4470 | { | |
4471 | unsigned int count; | |
4472 | struct elf_segment_map *m; | |
4473 | asection **sections = NULL; | |
4474 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
3dea8fca | 4475 | bfd_boolean no_user_phdrs; |
252b5132 | 4476 | |
12bd6957 | 4477 | no_user_phdrs = elf_seg_map (abfd) == NULL; |
d324f6d6 RM |
4478 | |
4479 | if (info != NULL) | |
4480 | info->user_phdrs = !no_user_phdrs; | |
4481 | ||
3dea8fca | 4482 | if (no_user_phdrs && bfd_count_sections (abfd) != 0) |
252b5132 | 4483 | { |
8ded5a0f AM |
4484 | asection *s; |
4485 | unsigned int i; | |
4486 | struct elf_segment_map *mfirst; | |
4487 | struct elf_segment_map **pm; | |
4488 | asection *last_hdr; | |
4489 | bfd_vma last_size; | |
4490 | unsigned int phdr_index; | |
4491 | bfd_vma maxpagesize; | |
4492 | asection **hdrpp; | |
4493 | bfd_boolean phdr_in_segment = TRUE; | |
4494 | bfd_boolean writable; | |
4495 | int tls_count = 0; | |
4496 | asection *first_tls = NULL; | |
4497 | asection *dynsec, *eh_frame_hdr; | |
4498 | bfd_size_type amt; | |
8d06853e | 4499 | bfd_vma addr_mask, wrap_to = 0; |
1a9ccd70 | 4500 | bfd_boolean linker_created_pt_phdr_segment = FALSE; |
252b5132 | 4501 | |
8ded5a0f | 4502 | /* Select the allocated sections, and sort them. */ |
252b5132 | 4503 | |
a50b1753 NC |
4504 | sections = (asection **) bfd_malloc2 (bfd_count_sections (abfd), |
4505 | sizeof (asection *)); | |
8ded5a0f | 4506 | if (sections == NULL) |
252b5132 | 4507 | goto error_return; |
252b5132 | 4508 | |
8d06853e AM |
4509 | /* Calculate top address, avoiding undefined behaviour of shift |
4510 | left operator when shift count is equal to size of type | |
4511 | being shifted. */ | |
4512 | addr_mask = ((bfd_vma) 1 << (bfd_arch_bits_per_address (abfd) - 1)) - 1; | |
4513 | addr_mask = (addr_mask << 1) + 1; | |
4514 | ||
8ded5a0f AM |
4515 | i = 0; |
4516 | for (s = abfd->sections; s != NULL; s = s->next) | |
4517 | { | |
4518 | if ((s->flags & SEC_ALLOC) != 0) | |
4519 | { | |
4520 | sections[i] = s; | |
4521 | ++i; | |
8d06853e AM |
4522 | /* A wrapping section potentially clashes with header. */ |
4523 | if (((s->lma + s->size) & addr_mask) < (s->lma & addr_mask)) | |
4524 | wrap_to = (s->lma + s->size) & addr_mask; | |
8ded5a0f AM |
4525 | } |
4526 | } | |
4527 | BFD_ASSERT (i <= bfd_count_sections (abfd)); | |
4528 | count = i; | |
252b5132 | 4529 | |
8ded5a0f | 4530 | qsort (sections, (size_t) count, sizeof (asection *), elf_sort_sections); |
252b5132 | 4531 | |
8ded5a0f | 4532 | /* Build the mapping. */ |
252b5132 | 4533 | |
8ded5a0f AM |
4534 | mfirst = NULL; |
4535 | pm = &mfirst; | |
252b5132 | 4536 | |
8ded5a0f AM |
4537 | /* If we have a .interp section, then create a PT_PHDR segment for |
4538 | the program headers and a PT_INTERP segment for the .interp | |
4539 | section. */ | |
4540 | s = bfd_get_section_by_name (abfd, ".interp"); | |
4541 | if (s != NULL && (s->flags & SEC_LOAD) != 0) | |
4542 | { | |
4543 | amt = sizeof (struct elf_segment_map); | |
a50b1753 | 4544 | m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); |
8ded5a0f AM |
4545 | if (m == NULL) |
4546 | goto error_return; | |
4547 | m->next = NULL; | |
4548 | m->p_type = PT_PHDR; | |
4549 | /* FIXME: UnixWare and Solaris set PF_X, Irix 5 does not. */ | |
4550 | m->p_flags = PF_R | PF_X; | |
4551 | m->p_flags_valid = 1; | |
4552 | m->includes_phdrs = 1; | |
1a9ccd70 | 4553 | linker_created_pt_phdr_segment = TRUE; |
8ded5a0f AM |
4554 | *pm = m; |
4555 | pm = &m->next; | |
252b5132 | 4556 | |
8ded5a0f | 4557 | amt = sizeof (struct elf_segment_map); |
a50b1753 | 4558 | m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); |
8ded5a0f AM |
4559 | if (m == NULL) |
4560 | goto error_return; | |
4561 | m->next = NULL; | |
4562 | m->p_type = PT_INTERP; | |
4563 | m->count = 1; | |
4564 | m->sections[0] = s; | |
4565 | ||
4566 | *pm = m; | |
4567 | pm = &m->next; | |
252b5132 | 4568 | } |
8ded5a0f AM |
4569 | |
4570 | /* Look through the sections. We put sections in the same program | |
4571 | segment when the start of the second section can be placed within | |
4572 | a few bytes of the end of the first section. */ | |
4573 | last_hdr = NULL; | |
4574 | last_size = 0; | |
4575 | phdr_index = 0; | |
4576 | maxpagesize = bed->maxpagesize; | |
063bb025 NC |
4577 | /* PR 17512: file: c8455299. |
4578 | Avoid divide-by-zero errors later on. | |
4579 | FIXME: Should we abort if the maxpagesize is zero ? */ | |
4580 | if (maxpagesize == 0) | |
4581 | maxpagesize = 1; | |
8ded5a0f AM |
4582 | writable = FALSE; |
4583 | dynsec = bfd_get_section_by_name (abfd, ".dynamic"); | |
4584 | if (dynsec != NULL | |
4585 | && (dynsec->flags & SEC_LOAD) == 0) | |
4586 | dynsec = NULL; | |
4587 | ||
4588 | /* Deal with -Ttext or something similar such that the first section | |
4589 | is not adjacent to the program headers. This is an | |
4590 | approximation, since at this point we don't know exactly how many | |
4591 | program headers we will need. */ | |
4592 | if (count > 0) | |
252b5132 | 4593 | { |
12bd6957 | 4594 | bfd_size_type phdr_size = elf_program_header_size (abfd); |
8ded5a0f | 4595 | |
62d7a5f6 | 4596 | if (phdr_size == (bfd_size_type) -1) |
8ded5a0f | 4597 | phdr_size = get_program_header_size (abfd, info); |
d2bcb0d1 | 4598 | phdr_size += bed->s->sizeof_ehdr; |
8ded5a0f | 4599 | if ((abfd->flags & D_PAGED) == 0 |
8d06853e AM |
4600 | || (sections[0]->lma & addr_mask) < phdr_size |
4601 | || ((sections[0]->lma & addr_mask) % maxpagesize | |
4602 | < phdr_size % maxpagesize) | |
4603 | || (sections[0]->lma & addr_mask & -maxpagesize) < wrap_to) | |
1a9ccd70 NC |
4604 | { |
4605 | /* PR 20815: The ELF standard says that a PT_PHDR segment, if | |
4606 | present, must be included as part of the memory image of the | |
4607 | program. Ie it must be part of a PT_LOAD segment as well. | |
4608 | If we have had to create our own PT_PHDR segment, but it is | |
4609 | not going to be covered by the first PT_LOAD segment, then | |
4610 | force the inclusion if we can... */ | |
4611 | if ((abfd->flags & D_PAGED) != 0 | |
4612 | && linker_created_pt_phdr_segment) | |
4613 | phdr_in_segment = TRUE; | |
4614 | else | |
4615 | phdr_in_segment = FALSE; | |
4616 | } | |
252b5132 RH |
4617 | } |
4618 | ||
8ded5a0f | 4619 | for (i = 0, hdrpp = sections; i < count; i++, hdrpp++) |
252b5132 | 4620 | { |
8ded5a0f AM |
4621 | asection *hdr; |
4622 | bfd_boolean new_segment; | |
4623 | ||
4624 | hdr = *hdrpp; | |
4625 | ||
4626 | /* See if this section and the last one will fit in the same | |
4627 | segment. */ | |
4628 | ||
4629 | if (last_hdr == NULL) | |
4630 | { | |
4631 | /* If we don't have a segment yet, then we don't need a new | |
4632 | one (we build the last one after this loop). */ | |
4633 | new_segment = FALSE; | |
4634 | } | |
4635 | else if (last_hdr->lma - last_hdr->vma != hdr->lma - hdr->vma) | |
4636 | { | |
4637 | /* If this section has a different relation between the | |
4638 | virtual address and the load address, then we need a new | |
4639 | segment. */ | |
4640 | new_segment = TRUE; | |
4641 | } | |
b5599592 AM |
4642 | else if (hdr->lma < last_hdr->lma + last_size |
4643 | || last_hdr->lma + last_size < last_hdr->lma) | |
4644 | { | |
4645 | /* If this section has a load address that makes it overlap | |
4646 | the previous section, then we need a new segment. */ | |
4647 | new_segment = TRUE; | |
4648 | } | |
39948a60 NC |
4649 | /* In the next test we have to be careful when last_hdr->lma is close |
4650 | to the end of the address space. If the aligned address wraps | |
4651 | around to the start of the address space, then there are no more | |
4652 | pages left in memory and it is OK to assume that the current | |
4653 | section can be included in the current segment. */ | |
4654 | else if ((BFD_ALIGN (last_hdr->lma + last_size, maxpagesize) + maxpagesize | |
4655 | > last_hdr->lma) | |
4656 | && (BFD_ALIGN (last_hdr->lma + last_size, maxpagesize) + maxpagesize | |
4ff73856 | 4657 | <= hdr->lma)) |
8ded5a0f AM |
4658 | { |
4659 | /* If putting this section in this segment would force us to | |
4660 | skip a page in the segment, then we need a new segment. */ | |
4661 | new_segment = TRUE; | |
4662 | } | |
4663 | else if ((last_hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0 | |
e5654c0f AM |
4664 | && (hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) != 0 |
4665 | && ((abfd->flags & D_PAGED) == 0 | |
4666 | || (((last_hdr->lma + last_size - 1) & -maxpagesize) | |
4667 | != (hdr->lma & -maxpagesize)))) | |
8ded5a0f | 4668 | { |
e5654c0f AM |
4669 | /* We don't want to put a loaded section after a |
4670 | nonloaded (ie. bss style) section in the same segment | |
4671 | as that will force the non-loaded section to be loaded. | |
4672 | Consider .tbss sections as loaded for this purpose. | |
4673 | However, like the writable/non-writable case below, | |
4674 | if they are on the same page then they must be put | |
4675 | in the same segment. */ | |
8ded5a0f AM |
4676 | new_segment = TRUE; |
4677 | } | |
4678 | else if ((abfd->flags & D_PAGED) == 0) | |
4679 | { | |
4680 | /* If the file is not demand paged, which means that we | |
4681 | don't require the sections to be correctly aligned in the | |
4682 | file, then there is no other reason for a new segment. */ | |
4683 | new_segment = FALSE; | |
4684 | } | |
4685 | else if (! writable | |
4686 | && (hdr->flags & SEC_READONLY) == 0 | |
8d06853e AM |
4687 | && (((last_hdr->lma + last_size - 1) & -maxpagesize) |
4688 | != (hdr->lma & -maxpagesize))) | |
8ded5a0f AM |
4689 | { |
4690 | /* We don't want to put a writable section in a read only | |
4691 | segment, unless they are on the same page in memory | |
4692 | anyhow. We already know that the last section does not | |
4693 | bring us past the current section on the page, so the | |
4694 | only case in which the new section is not on the same | |
4695 | page as the previous section is when the previous section | |
4696 | ends precisely on a page boundary. */ | |
4697 | new_segment = TRUE; | |
4698 | } | |
4699 | else | |
4700 | { | |
4701 | /* Otherwise, we can use the same segment. */ | |
4702 | new_segment = FALSE; | |
4703 | } | |
4704 | ||
2889e75b | 4705 | /* Allow interested parties a chance to override our decision. */ |
ceae84aa AM |
4706 | if (last_hdr != NULL |
4707 | && info != NULL | |
4708 | && info->callbacks->override_segment_assignment != NULL) | |
4709 | new_segment | |
4710 | = info->callbacks->override_segment_assignment (info, abfd, hdr, | |
4711 | last_hdr, | |
4712 | new_segment); | |
2889e75b | 4713 | |
8ded5a0f AM |
4714 | if (! new_segment) |
4715 | { | |
4716 | if ((hdr->flags & SEC_READONLY) == 0) | |
4717 | writable = TRUE; | |
4718 | last_hdr = hdr; | |
4719 | /* .tbss sections effectively have zero size. */ | |
4720 | if ((hdr->flags & (SEC_THREAD_LOCAL | SEC_LOAD)) | |
4721 | != SEC_THREAD_LOCAL) | |
4722 | last_size = hdr->size; | |
4723 | else | |
4724 | last_size = 0; | |
4725 | continue; | |
4726 | } | |
4727 | ||
4728 | /* We need a new program segment. We must create a new program | |
4729 | header holding all the sections from phdr_index until hdr. */ | |
4730 | ||
4731 | m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment); | |
4732 | if (m == NULL) | |
4733 | goto error_return; | |
4734 | ||
4735 | *pm = m; | |
4736 | pm = &m->next; | |
4737 | ||
252b5132 | 4738 | if ((hdr->flags & SEC_READONLY) == 0) |
b34976b6 | 4739 | writable = TRUE; |
8ded5a0f AM |
4740 | else |
4741 | writable = FALSE; | |
4742 | ||
baaff79e JJ |
4743 | last_hdr = hdr; |
4744 | /* .tbss sections effectively have zero size. */ | |
e5caec89 | 4745 | if ((hdr->flags & (SEC_THREAD_LOCAL | SEC_LOAD)) != SEC_THREAD_LOCAL) |
eea6121a | 4746 | last_size = hdr->size; |
baaff79e JJ |
4747 | else |
4748 | last_size = 0; | |
8ded5a0f AM |
4749 | phdr_index = i; |
4750 | phdr_in_segment = FALSE; | |
252b5132 RH |
4751 | } |
4752 | ||
86b2281f AM |
4753 | /* Create a final PT_LOAD program segment, but not if it's just |
4754 | for .tbss. */ | |
4755 | if (last_hdr != NULL | |
4756 | && (i - phdr_index != 1 | |
4757 | || ((last_hdr->flags & (SEC_THREAD_LOCAL | SEC_LOAD)) | |
4758 | != SEC_THREAD_LOCAL))) | |
8ded5a0f AM |
4759 | { |
4760 | m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment); | |
4761 | if (m == NULL) | |
4762 | goto error_return; | |
252b5132 | 4763 | |
8ded5a0f AM |
4764 | *pm = m; |
4765 | pm = &m->next; | |
4766 | } | |
252b5132 | 4767 | |
8ded5a0f AM |
4768 | /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */ |
4769 | if (dynsec != NULL) | |
4770 | { | |
4771 | m = _bfd_elf_make_dynamic_segment (abfd, dynsec); | |
4772 | if (m == NULL) | |
4773 | goto error_return; | |
4774 | *pm = m; | |
4775 | pm = &m->next; | |
4776 | } | |
252b5132 | 4777 | |
1c5265b5 JJ |
4778 | /* For each batch of consecutive loadable .note sections, |
4779 | add a PT_NOTE segment. We don't use bfd_get_section_by_name, | |
4780 | because if we link together nonloadable .note sections and | |
4781 | loadable .note sections, we will generate two .note sections | |
4782 | in the output file. FIXME: Using names for section types is | |
4783 | bogus anyhow. */ | |
8ded5a0f AM |
4784 | for (s = abfd->sections; s != NULL; s = s->next) |
4785 | { | |
4786 | if ((s->flags & SEC_LOAD) != 0 | |
0112cd26 | 4787 | && CONST_STRNEQ (s->name, ".note")) |
8ded5a0f | 4788 | { |
1c5265b5 | 4789 | asection *s2; |
91d6fa6a NC |
4790 | |
4791 | count = 1; | |
8ded5a0f | 4792 | amt = sizeof (struct elf_segment_map); |
1c5265b5 JJ |
4793 | if (s->alignment_power == 2) |
4794 | for (s2 = s; s2->next != NULL; s2 = s2->next) | |
55b581a6 JJ |
4795 | { |
4796 | if (s2->next->alignment_power == 2 | |
4797 | && (s2->next->flags & SEC_LOAD) != 0 | |
4798 | && CONST_STRNEQ (s2->next->name, ".note") | |
8d06853e AM |
4799 | && align_power (s2->lma + s2->size, 2) |
4800 | == s2->next->lma) | |
55b581a6 JJ |
4801 | count++; |
4802 | else | |
4803 | break; | |
4804 | } | |
1c5265b5 | 4805 | amt += (count - 1) * sizeof (asection *); |
a50b1753 | 4806 | m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); |
8ded5a0f AM |
4807 | if (m == NULL) |
4808 | goto error_return; | |
4809 | m->next = NULL; | |
4810 | m->p_type = PT_NOTE; | |
1c5265b5 JJ |
4811 | m->count = count; |
4812 | while (count > 1) | |
4813 | { | |
4814 | m->sections[m->count - count--] = s; | |
4815 | BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0); | |
4816 | s = s->next; | |
4817 | } | |
4818 | m->sections[m->count - 1] = s; | |
4819 | BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0); | |
8ded5a0f AM |
4820 | *pm = m; |
4821 | pm = &m->next; | |
4822 | } | |
4823 | if (s->flags & SEC_THREAD_LOCAL) | |
4824 | { | |
4825 | if (! tls_count) | |
4826 | first_tls = s; | |
4827 | tls_count++; | |
4828 | } | |
4829 | } | |
252b5132 | 4830 | |
8ded5a0f AM |
4831 | /* If there are any SHF_TLS output sections, add PT_TLS segment. */ |
4832 | if (tls_count > 0) | |
4833 | { | |
8ded5a0f AM |
4834 | amt = sizeof (struct elf_segment_map); |
4835 | amt += (tls_count - 1) * sizeof (asection *); | |
a50b1753 | 4836 | m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); |
8ded5a0f AM |
4837 | if (m == NULL) |
4838 | goto error_return; | |
4839 | m->next = NULL; | |
4840 | m->p_type = PT_TLS; | |
4841 | m->count = tls_count; | |
4842 | /* Mandated PF_R. */ | |
4843 | m->p_flags = PF_R; | |
4844 | m->p_flags_valid = 1; | |
d923cae0 | 4845 | s = first_tls; |
91d6fa6a | 4846 | for (i = 0; i < (unsigned int) tls_count; ++i) |
8ded5a0f | 4847 | { |
d923cae0 L |
4848 | if ((s->flags & SEC_THREAD_LOCAL) == 0) |
4849 | { | |
4850 | _bfd_error_handler | |
4851 | (_("%B: TLS sections are not adjacent:"), abfd); | |
4852 | s = first_tls; | |
4853 | i = 0; | |
4854 | while (i < (unsigned int) tls_count) | |
4855 | { | |
4856 | if ((s->flags & SEC_THREAD_LOCAL) != 0) | |
4857 | { | |
4858 | _bfd_error_handler (_(" TLS: %A"), s); | |
4859 | i++; | |
4860 | } | |
4861 | else | |
4862 | _bfd_error_handler (_(" non-TLS: %A"), s); | |
4863 | s = s->next; | |
4864 | } | |
4865 | bfd_set_error (bfd_error_bad_value); | |
4866 | goto error_return; | |
4867 | } | |
4868 | m->sections[i] = s; | |
4869 | s = s->next; | |
8ded5a0f | 4870 | } |
252b5132 | 4871 | |
8ded5a0f AM |
4872 | *pm = m; |
4873 | pm = &m->next; | |
4874 | } | |
252b5132 | 4875 | |
8ded5a0f AM |
4876 | /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME |
4877 | segment. */ | |
12bd6957 | 4878 | eh_frame_hdr = elf_eh_frame_hdr (abfd); |
8ded5a0f AM |
4879 | if (eh_frame_hdr != NULL |
4880 | && (eh_frame_hdr->output_section->flags & SEC_LOAD) != 0) | |
252b5132 | 4881 | { |
dc810e39 | 4882 | amt = sizeof (struct elf_segment_map); |
a50b1753 | 4883 | m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); |
252b5132 RH |
4884 | if (m == NULL) |
4885 | goto error_return; | |
4886 | m->next = NULL; | |
8ded5a0f | 4887 | m->p_type = PT_GNU_EH_FRAME; |
252b5132 | 4888 | m->count = 1; |
8ded5a0f | 4889 | m->sections[0] = eh_frame_hdr->output_section; |
252b5132 RH |
4890 | |
4891 | *pm = m; | |
4892 | pm = &m->next; | |
4893 | } | |
13ae64f3 | 4894 | |
12bd6957 | 4895 | if (elf_stack_flags (abfd)) |
13ae64f3 | 4896 | { |
8ded5a0f | 4897 | amt = sizeof (struct elf_segment_map); |
a50b1753 | 4898 | m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); |
8ded5a0f AM |
4899 | if (m == NULL) |
4900 | goto error_return; | |
4901 | m->next = NULL; | |
2b05f1b7 | 4902 | m->p_type = PT_GNU_STACK; |
12bd6957 | 4903 | m->p_flags = elf_stack_flags (abfd); |
04c3a755 | 4904 | m->p_align = bed->stack_align; |
8ded5a0f | 4905 | m->p_flags_valid = 1; |
04c3a755 NS |
4906 | m->p_align_valid = m->p_align != 0; |
4907 | if (info->stacksize > 0) | |
4908 | { | |
4909 | m->p_size = info->stacksize; | |
4910 | m->p_size_valid = 1; | |
4911 | } | |
252b5132 | 4912 | |
8ded5a0f AM |
4913 | *pm = m; |
4914 | pm = &m->next; | |
4915 | } | |
65765700 | 4916 | |
ceae84aa | 4917 | if (info != NULL && info->relro) |
8ded5a0f | 4918 | { |
f210dcff L |
4919 | for (m = mfirst; m != NULL; m = m->next) |
4920 | { | |
3832a4d8 AM |
4921 | if (m->p_type == PT_LOAD |
4922 | && m->count != 0 | |
4923 | && m->sections[0]->vma >= info->relro_start | |
4924 | && m->sections[0]->vma < info->relro_end) | |
f210dcff | 4925 | { |
3832a4d8 AM |
4926 | i = m->count; |
4927 | while (--i != (unsigned) -1) | |
4928 | if ((m->sections[i]->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) | |
4929 | == (SEC_LOAD | SEC_HAS_CONTENTS)) | |
4930 | break; | |
4931 | ||
43a8475c | 4932 | if (i != (unsigned) -1) |
f210dcff L |
4933 | break; |
4934 | } | |
be01b344 | 4935 | } |
f210dcff L |
4936 | |
4937 | /* Make a PT_GNU_RELRO segment only when it isn't empty. */ | |
4938 | if (m != NULL) | |
4939 | { | |
4940 | amt = sizeof (struct elf_segment_map); | |
a50b1753 | 4941 | m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); |
f210dcff L |
4942 | if (m == NULL) |
4943 | goto error_return; | |
4944 | m->next = NULL; | |
4945 | m->p_type = PT_GNU_RELRO; | |
f210dcff L |
4946 | *pm = m; |
4947 | pm = &m->next; | |
4948 | } | |
8ded5a0f | 4949 | } |
9ee5e499 | 4950 | |
8ded5a0f | 4951 | free (sections); |
12bd6957 | 4952 | elf_seg_map (abfd) = mfirst; |
9ee5e499 JJ |
4953 | } |
4954 | ||
3dea8fca | 4955 | if (!elf_modify_segment_map (abfd, info, no_user_phdrs)) |
8ded5a0f | 4956 | return FALSE; |
8c37241b | 4957 | |
12bd6957 | 4958 | for (count = 0, m = elf_seg_map (abfd); m != NULL; m = m->next) |
8ded5a0f | 4959 | ++count; |
12bd6957 | 4960 | elf_program_header_size (abfd) = count * bed->s->sizeof_phdr; |
252b5132 | 4961 | |
b34976b6 | 4962 | return TRUE; |
252b5132 RH |
4963 | |
4964 | error_return: | |
4965 | if (sections != NULL) | |
4966 | free (sections); | |
b34976b6 | 4967 | return FALSE; |
252b5132 RH |
4968 | } |
4969 | ||
4970 | /* Sort sections by address. */ | |
4971 | ||
4972 | static int | |
217aa764 | 4973 | elf_sort_sections (const void *arg1, const void *arg2) |
252b5132 RH |
4974 | { |
4975 | const asection *sec1 = *(const asection **) arg1; | |
4976 | const asection *sec2 = *(const asection **) arg2; | |
eecdbe52 | 4977 | bfd_size_type size1, size2; |
252b5132 RH |
4978 | |
4979 | /* Sort by LMA first, since this is the address used to | |
4980 | place the section into a segment. */ | |
4981 | if (sec1->lma < sec2->lma) | |
4982 | return -1; | |
4983 | else if (sec1->lma > sec2->lma) | |
4984 | return 1; | |
4985 | ||
4986 | /* Then sort by VMA. Normally the LMA and the VMA will be | |
4987 | the same, and this will do nothing. */ | |
4988 | if (sec1->vma < sec2->vma) | |
4989 | return -1; | |
4990 | else if (sec1->vma > sec2->vma) | |
4991 | return 1; | |
4992 | ||
4993 | /* Put !SEC_LOAD sections after SEC_LOAD ones. */ | |
4994 | ||
07c6e936 | 4995 | #define TOEND(x) (((x)->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0) |
252b5132 RH |
4996 | |
4997 | if (TOEND (sec1)) | |
4998 | { | |
4999 | if (TOEND (sec2)) | |
00a7cdc5 NC |
5000 | { |
5001 | /* If the indicies are the same, do not return 0 | |
5002 | here, but continue to try the next comparison. */ | |
5003 | if (sec1->target_index - sec2->target_index != 0) | |
5004 | return sec1->target_index - sec2->target_index; | |
5005 | } | |
252b5132 RH |
5006 | else |
5007 | return 1; | |
5008 | } | |
00a7cdc5 | 5009 | else if (TOEND (sec2)) |
252b5132 RH |
5010 | return -1; |
5011 | ||
5012 | #undef TOEND | |
5013 | ||
00a7cdc5 NC |
5014 | /* Sort by size, to put zero sized sections |
5015 | before others at the same address. */ | |
252b5132 | 5016 | |
eea6121a AM |
5017 | size1 = (sec1->flags & SEC_LOAD) ? sec1->size : 0; |
5018 | size2 = (sec2->flags & SEC_LOAD) ? sec2->size : 0; | |
eecdbe52 JJ |
5019 | |
5020 | if (size1 < size2) | |
252b5132 | 5021 | return -1; |
eecdbe52 | 5022 | if (size1 > size2) |
252b5132 RH |
5023 | return 1; |
5024 | ||
5025 | return sec1->target_index - sec2->target_index; | |
5026 | } | |
5027 | ||
340b6d91 AC |
5028 | /* Ian Lance Taylor writes: |
5029 | ||
5030 | We shouldn't be using % with a negative signed number. That's just | |
5031 | not good. We have to make sure either that the number is not | |
5032 | negative, or that the number has an unsigned type. When the types | |
5033 | are all the same size they wind up as unsigned. When file_ptr is a | |
5034 | larger signed type, the arithmetic winds up as signed long long, | |
5035 | which is wrong. | |
5036 | ||
5037 | What we're trying to say here is something like ``increase OFF by | |
5038 | the least amount that will cause it to be equal to the VMA modulo | |
5039 | the page size.'' */ | |
5040 | /* In other words, something like: | |
5041 | ||
5042 | vma_offset = m->sections[0]->vma % bed->maxpagesize; | |
5043 | off_offset = off % bed->maxpagesize; | |
5044 | if (vma_offset < off_offset) | |
5045 | adjustment = vma_offset + bed->maxpagesize - off_offset; | |
5046 | else | |
5047 | adjustment = vma_offset - off_offset; | |
08a40648 | 5048 | |
340b6d91 AC |
5049 | which can can be collapsed into the expression below. */ |
5050 | ||
5051 | static file_ptr | |
5052 | vma_page_aligned_bias (bfd_vma vma, ufile_ptr off, bfd_vma maxpagesize) | |
5053 | { | |
dc9155b2 NC |
5054 | /* PR binutils/16199: Handle an alignment of zero. */ |
5055 | if (maxpagesize == 0) | |
5056 | maxpagesize = 1; | |
340b6d91 AC |
5057 | return ((vma - off) % maxpagesize); |
5058 | } | |
5059 | ||
6d33f217 L |
5060 | static void |
5061 | print_segment_map (const struct elf_segment_map *m) | |
5062 | { | |
5063 | unsigned int j; | |
5064 | const char *pt = get_segment_type (m->p_type); | |
5065 | char buf[32]; | |
5066 | ||
5067 | if (pt == NULL) | |
5068 | { | |
5069 | if (m->p_type >= PT_LOPROC && m->p_type <= PT_HIPROC) | |
5070 | sprintf (buf, "LOPROC+%7.7x", | |
5071 | (unsigned int) (m->p_type - PT_LOPROC)); | |
5072 | else if (m->p_type >= PT_LOOS && m->p_type <= PT_HIOS) | |
5073 | sprintf (buf, "LOOS+%7.7x", | |
5074 | (unsigned int) (m->p_type - PT_LOOS)); | |
5075 | else | |
5076 | snprintf (buf, sizeof (buf), "%8.8x", | |
5077 | (unsigned int) m->p_type); | |
5078 | pt = buf; | |
5079 | } | |
4a97a0e5 | 5080 | fflush (stdout); |
6d33f217 L |
5081 | fprintf (stderr, "%s:", pt); |
5082 | for (j = 0; j < m->count; j++) | |
5083 | fprintf (stderr, " %s", m->sections [j]->name); | |
5084 | putc ('\n',stderr); | |
4a97a0e5 | 5085 | fflush (stderr); |
6d33f217 L |
5086 | } |
5087 | ||
32812159 AM |
5088 | static bfd_boolean |
5089 | write_zeros (bfd *abfd, file_ptr pos, bfd_size_type len) | |
5090 | { | |
5091 | void *buf; | |
5092 | bfd_boolean ret; | |
5093 | ||
5094 | if (bfd_seek (abfd, pos, SEEK_SET) != 0) | |
5095 | return FALSE; | |
5096 | buf = bfd_zmalloc (len); | |
5097 | if (buf == NULL) | |
5098 | return FALSE; | |
5099 | ret = bfd_bwrite (buf, len, abfd) == len; | |
5100 | free (buf); | |
5101 | return ret; | |
5102 | } | |
5103 | ||
252b5132 RH |
5104 | /* Assign file positions to the sections based on the mapping from |
5105 | sections to segments. This function also sets up some fields in | |
f3520d2f | 5106 | the file header. */ |
252b5132 | 5107 | |
b34976b6 | 5108 | static bfd_boolean |
f3520d2f AM |
5109 | assign_file_positions_for_load_sections (bfd *abfd, |
5110 | struct bfd_link_info *link_info) | |
252b5132 RH |
5111 | { |
5112 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
252b5132 | 5113 | struct elf_segment_map *m; |
252b5132 | 5114 | Elf_Internal_Phdr *phdrs; |
252b5132 | 5115 | Elf_Internal_Phdr *p; |
02bf8d82 | 5116 | file_ptr off; |
3f570048 | 5117 | bfd_size_type maxpagesize; |
f3520d2f | 5118 | unsigned int alloc; |
0920dee7 | 5119 | unsigned int i, j; |
2b0bc088 | 5120 | bfd_vma header_pad = 0; |
252b5132 | 5121 | |
e36284ab | 5122 | if (link_info == NULL |
ceae84aa | 5123 | && !_bfd_elf_map_sections_to_segments (abfd, link_info)) |
8ded5a0f | 5124 | return FALSE; |
252b5132 | 5125 | |
8ded5a0f | 5126 | alloc = 0; |
12bd6957 | 5127 | for (m = elf_seg_map (abfd); m != NULL; m = m->next) |
2b0bc088 NC |
5128 | { |
5129 | ++alloc; | |
5130 | if (m->header_size) | |
5131 | header_pad = m->header_size; | |
5132 | } | |
252b5132 | 5133 | |
82f2dbf7 NC |
5134 | if (alloc) |
5135 | { | |
5136 | elf_elfheader (abfd)->e_phoff = bed->s->sizeof_ehdr; | |
5137 | elf_elfheader (abfd)->e_phentsize = bed->s->sizeof_phdr; | |
5138 | } | |
5139 | else | |
5140 | { | |
5141 | /* PR binutils/12467. */ | |
5142 | elf_elfheader (abfd)->e_phoff = 0; | |
5143 | elf_elfheader (abfd)->e_phentsize = 0; | |
5144 | } | |
d324f6d6 | 5145 | |
8ded5a0f | 5146 | elf_elfheader (abfd)->e_phnum = alloc; |
252b5132 | 5147 | |
12bd6957 AM |
5148 | if (elf_program_header_size (abfd) == (bfd_size_type) -1) |
5149 | elf_program_header_size (abfd) = alloc * bed->s->sizeof_phdr; | |
8ded5a0f | 5150 | else |
12bd6957 | 5151 | BFD_ASSERT (elf_program_header_size (abfd) |
59e0647f | 5152 | >= alloc * bed->s->sizeof_phdr); |
252b5132 RH |
5153 | |
5154 | if (alloc == 0) | |
f3520d2f | 5155 | { |
12bd6957 | 5156 | elf_next_file_pos (abfd) = bed->s->sizeof_ehdr; |
8ded5a0f | 5157 | return TRUE; |
f3520d2f | 5158 | } |
252b5132 | 5159 | |
12bd6957 | 5160 | /* We're writing the size in elf_program_header_size (abfd), |
57268894 HPN |
5161 | see assign_file_positions_except_relocs, so make sure we have |
5162 | that amount allocated, with trailing space cleared. | |
12bd6957 AM |
5163 | The variable alloc contains the computed need, while |
5164 | elf_program_header_size (abfd) contains the size used for the | |
57268894 HPN |
5165 | layout. |
5166 | See ld/emultempl/elf-generic.em:gld${EMULATION_NAME}_map_segments | |
5167 | where the layout is forced to according to a larger size in the | |
5168 | last iterations for the testcase ld-elf/header. */ | |
12bd6957 | 5169 | BFD_ASSERT (elf_program_header_size (abfd) % bed->s->sizeof_phdr |
57268894 | 5170 | == 0); |
a50b1753 NC |
5171 | phdrs = (Elf_Internal_Phdr *) |
5172 | bfd_zalloc2 (abfd, | |
12bd6957 | 5173 | (elf_program_header_size (abfd) / bed->s->sizeof_phdr), |
a50b1753 | 5174 | sizeof (Elf_Internal_Phdr)); |
f3520d2f | 5175 | elf_tdata (abfd)->phdr = phdrs; |
252b5132 | 5176 | if (phdrs == NULL) |
b34976b6 | 5177 | return FALSE; |
252b5132 | 5178 | |
3f570048 AM |
5179 | maxpagesize = 1; |
5180 | if ((abfd->flags & D_PAGED) != 0) | |
5181 | maxpagesize = bed->maxpagesize; | |
5182 | ||
252b5132 RH |
5183 | off = bed->s->sizeof_ehdr; |
5184 | off += alloc * bed->s->sizeof_phdr; | |
2b0bc088 NC |
5185 | if (header_pad < (bfd_vma) off) |
5186 | header_pad = 0; | |
5187 | else | |
5188 | header_pad -= off; | |
5189 | off += header_pad; | |
252b5132 | 5190 | |
12bd6957 | 5191 | for (m = elf_seg_map (abfd), p = phdrs, j = 0; |
252b5132 | 5192 | m != NULL; |
0920dee7 | 5193 | m = m->next, p++, j++) |
252b5132 | 5194 | { |
252b5132 | 5195 | asection **secpp; |
bf988460 AM |
5196 | bfd_vma off_adjust; |
5197 | bfd_boolean no_contents; | |
252b5132 RH |
5198 | |
5199 | /* If elf_segment_map is not from map_sections_to_segments, the | |
08a40648 | 5200 | sections may not be correctly ordered. NOTE: sorting should |
52e9b619 MS |
5201 | not be done to the PT_NOTE section of a corefile, which may |
5202 | contain several pseudo-sections artificially created by bfd. | |
5203 | Sorting these pseudo-sections breaks things badly. */ | |
47d9a591 AM |
5204 | if (m->count > 1 |
5205 | && !(elf_elfheader (abfd)->e_type == ET_CORE | |
52e9b619 | 5206 | && m->p_type == PT_NOTE)) |
252b5132 RH |
5207 | qsort (m->sections, (size_t) m->count, sizeof (asection *), |
5208 | elf_sort_sections); | |
5209 | ||
b301b248 AM |
5210 | /* An ELF segment (described by Elf_Internal_Phdr) may contain a |
5211 | number of sections with contents contributing to both p_filesz | |
5212 | and p_memsz, followed by a number of sections with no contents | |
5213 | that just contribute to p_memsz. In this loop, OFF tracks next | |
02bf8d82 | 5214 | available file offset for PT_LOAD and PT_NOTE segments. */ |
252b5132 | 5215 | p->p_type = m->p_type; |
28a7f3e7 | 5216 | p->p_flags = m->p_flags; |
252b5132 | 5217 | |
3f570048 AM |
5218 | if (m->count == 0) |
5219 | p->p_vaddr = 0; | |
5220 | else | |
3271a814 | 5221 | p->p_vaddr = m->sections[0]->vma - m->p_vaddr_offset; |
3f570048 AM |
5222 | |
5223 | if (m->p_paddr_valid) | |
5224 | p->p_paddr = m->p_paddr; | |
5225 | else if (m->count == 0) | |
5226 | p->p_paddr = 0; | |
5227 | else | |
08a40648 | 5228 | p->p_paddr = m->sections[0]->lma - m->p_vaddr_offset; |
3f570048 AM |
5229 | |
5230 | if (p->p_type == PT_LOAD | |
5231 | && (abfd->flags & D_PAGED) != 0) | |
5232 | { | |
5233 | /* p_align in demand paged PT_LOAD segments effectively stores | |
5234 | the maximum page size. When copying an executable with | |
5235 | objcopy, we set m->p_align from the input file. Use this | |
5236 | value for maxpagesize rather than bed->maxpagesize, which | |
5237 | may be different. Note that we use maxpagesize for PT_TLS | |
5238 | segment alignment later in this function, so we are relying | |
5239 | on at least one PT_LOAD segment appearing before a PT_TLS | |
5240 | segment. */ | |
5241 | if (m->p_align_valid) | |
5242 | maxpagesize = m->p_align; | |
5243 | ||
5244 | p->p_align = maxpagesize; | |
5245 | } | |
3271a814 NS |
5246 | else if (m->p_align_valid) |
5247 | p->p_align = m->p_align; | |
e970b90a DJ |
5248 | else if (m->count == 0) |
5249 | p->p_align = 1 << bed->s->log_file_align; | |
3f570048 AM |
5250 | else |
5251 | p->p_align = 0; | |
5252 | ||
bf988460 AM |
5253 | no_contents = FALSE; |
5254 | off_adjust = 0; | |
252b5132 | 5255 | if (p->p_type == PT_LOAD |
b301b248 | 5256 | && m->count > 0) |
252b5132 | 5257 | { |
b301b248 | 5258 | bfd_size_type align; |
a49e53ed | 5259 | unsigned int align_power = 0; |
b301b248 | 5260 | |
3271a814 NS |
5261 | if (m->p_align_valid) |
5262 | align = p->p_align; | |
5263 | else | |
252b5132 | 5264 | { |
3271a814 NS |
5265 | for (i = 0, secpp = m->sections; i < m->count; i++, secpp++) |
5266 | { | |
5267 | unsigned int secalign; | |
08a40648 | 5268 | |
3271a814 NS |
5269 | secalign = bfd_get_section_alignment (abfd, *secpp); |
5270 | if (secalign > align_power) | |
5271 | align_power = secalign; | |
5272 | } | |
5273 | align = (bfd_size_type) 1 << align_power; | |
5274 | if (align < maxpagesize) | |
5275 | align = maxpagesize; | |
b301b248 | 5276 | } |
252b5132 | 5277 | |
02bf8d82 AM |
5278 | for (i = 0; i < m->count; i++) |
5279 | if ((m->sections[i]->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0) | |
5280 | /* If we aren't making room for this section, then | |
5281 | it must be SHT_NOBITS regardless of what we've | |
5282 | set via struct bfd_elf_special_section. */ | |
5283 | elf_section_type (m->sections[i]) = SHT_NOBITS; | |
5284 | ||
bf988460 | 5285 | /* Find out whether this segment contains any loadable |
aea274d3 AM |
5286 | sections. */ |
5287 | no_contents = TRUE; | |
5288 | for (i = 0; i < m->count; i++) | |
5289 | if (elf_section_type (m->sections[i]) != SHT_NOBITS) | |
5290 | { | |
5291 | no_contents = FALSE; | |
5292 | break; | |
5293 | } | |
bf988460 | 5294 | |
85cfcbfb | 5295 | off_adjust = vma_page_aligned_bias (p->p_vaddr, off, align); |
bf988460 AM |
5296 | off += off_adjust; |
5297 | if (no_contents) | |
5298 | { | |
5299 | /* We shouldn't need to align the segment on disk since | |
5300 | the segment doesn't need file space, but the gABI | |
5301 | arguably requires the alignment and glibc ld.so | |
5302 | checks it. So to comply with the alignment | |
5303 | requirement but not waste file space, we adjust | |
5304 | p_offset for just this segment. (OFF_ADJUST is | |
5305 | subtracted from OFF later.) This may put p_offset | |
5306 | past the end of file, but that shouldn't matter. */ | |
5307 | } | |
5308 | else | |
5309 | off_adjust = 0; | |
252b5132 | 5310 | } |
b1a6d0b1 NC |
5311 | /* Make sure the .dynamic section is the first section in the |
5312 | PT_DYNAMIC segment. */ | |
5313 | else if (p->p_type == PT_DYNAMIC | |
5314 | && m->count > 1 | |
5315 | && strcmp (m->sections[0]->name, ".dynamic") != 0) | |
5316 | { | |
5317 | _bfd_error_handler | |
b301b248 AM |
5318 | (_("%B: The first section in the PT_DYNAMIC segment is not the .dynamic section"), |
5319 | abfd); | |
b1a6d0b1 NC |
5320 | bfd_set_error (bfd_error_bad_value); |
5321 | return FALSE; | |
5322 | } | |
3f001e84 JK |
5323 | /* Set the note section type to SHT_NOTE. */ |
5324 | else if (p->p_type == PT_NOTE) | |
5325 | for (i = 0; i < m->count; i++) | |
5326 | elf_section_type (m->sections[i]) = SHT_NOTE; | |
252b5132 | 5327 | |
252b5132 RH |
5328 | p->p_offset = 0; |
5329 | p->p_filesz = 0; | |
5330 | p->p_memsz = 0; | |
5331 | ||
5332 | if (m->includes_filehdr) | |
5333 | { | |
bf988460 | 5334 | if (!m->p_flags_valid) |
252b5132 | 5335 | p->p_flags |= PF_R; |
252b5132 RH |
5336 | p->p_filesz = bed->s->sizeof_ehdr; |
5337 | p->p_memsz = bed->s->sizeof_ehdr; | |
5338 | if (m->count > 0) | |
5339 | { | |
9ab82472 L |
5340 | if (p->p_vaddr < (bfd_vma) off |
5341 | || (!m->p_paddr_valid | |
5342 | && p->p_paddr < (bfd_vma) off)) | |
252b5132 | 5343 | { |
4eca0228 | 5344 | _bfd_error_handler |
b301b248 AM |
5345 | (_("%B: Not enough room for program headers, try linking with -N"), |
5346 | abfd); | |
252b5132 | 5347 | bfd_set_error (bfd_error_bad_value); |
b34976b6 | 5348 | return FALSE; |
252b5132 RH |
5349 | } |
5350 | ||
5351 | p->p_vaddr -= off; | |
bf988460 | 5352 | if (!m->p_paddr_valid) |
252b5132 RH |
5353 | p->p_paddr -= off; |
5354 | } | |
252b5132 RH |
5355 | } |
5356 | ||
5357 | if (m->includes_phdrs) | |
5358 | { | |
bf988460 | 5359 | if (!m->p_flags_valid) |
252b5132 RH |
5360 | p->p_flags |= PF_R; |
5361 | ||
f3520d2f | 5362 | if (!m->includes_filehdr) |
252b5132 RH |
5363 | { |
5364 | p->p_offset = bed->s->sizeof_ehdr; | |
5365 | ||
5366 | if (m->count > 0) | |
5367 | { | |
252b5132 | 5368 | p->p_vaddr -= off - p->p_offset; |
bf988460 | 5369 | if (!m->p_paddr_valid) |
252b5132 RH |
5370 | p->p_paddr -= off - p->p_offset; |
5371 | } | |
252b5132 RH |
5372 | } |
5373 | ||
5374 | p->p_filesz += alloc * bed->s->sizeof_phdr; | |
5375 | p->p_memsz += alloc * bed->s->sizeof_phdr; | |
2b0bc088 NC |
5376 | if (m->count) |
5377 | { | |
5378 | p->p_filesz += header_pad; | |
5379 | p->p_memsz += header_pad; | |
5380 | } | |
252b5132 RH |
5381 | } |
5382 | ||
5383 | if (p->p_type == PT_LOAD | |
5384 | || (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core)) | |
5385 | { | |
bf988460 | 5386 | if (!m->includes_filehdr && !m->includes_phdrs) |
02bf8d82 | 5387 | p->p_offset = off; |
252b5132 RH |
5388 | else |
5389 | { | |
5390 | file_ptr adjust; | |
5391 | ||
5392 | adjust = off - (p->p_offset + p->p_filesz); | |
bf988460 AM |
5393 | if (!no_contents) |
5394 | p->p_filesz += adjust; | |
252b5132 RH |
5395 | p->p_memsz += adjust; |
5396 | } | |
5397 | } | |
5398 | ||
1ea63fd2 AM |
5399 | /* Set up p_filesz, p_memsz, p_align and p_flags from the section |
5400 | maps. Set filepos for sections in PT_LOAD segments, and in | |
5401 | core files, for sections in PT_NOTE segments. | |
5402 | assign_file_positions_for_non_load_sections will set filepos | |
5403 | for other sections and update p_filesz for other segments. */ | |
252b5132 RH |
5404 | for (i = 0, secpp = m->sections; i < m->count; i++, secpp++) |
5405 | { | |
5406 | asection *sec; | |
252b5132 | 5407 | bfd_size_type align; |
627b32bc | 5408 | Elf_Internal_Shdr *this_hdr; |
252b5132 RH |
5409 | |
5410 | sec = *secpp; | |
02bf8d82 | 5411 | this_hdr = &elf_section_data (sec)->this_hdr; |
3f570048 | 5412 | align = (bfd_size_type) 1 << bfd_get_section_alignment (abfd, sec); |
252b5132 | 5413 | |
88967714 AM |
5414 | if ((p->p_type == PT_LOAD |
5415 | || p->p_type == PT_TLS) | |
5416 | && (this_hdr->sh_type != SHT_NOBITS | |
5417 | || ((this_hdr->sh_flags & SHF_ALLOC) != 0 | |
5418 | && ((this_hdr->sh_flags & SHF_TLS) == 0 | |
5419 | || p->p_type == PT_TLS)))) | |
252b5132 | 5420 | { |
b5599592 AM |
5421 | bfd_vma p_start = p->p_paddr; |
5422 | bfd_vma p_end = p_start + p->p_memsz; | |
5423 | bfd_vma s_start = sec->lma; | |
5424 | bfd_vma adjust = s_start - p_end; | |
252b5132 | 5425 | |
a2d1e028 L |
5426 | if (adjust != 0 |
5427 | && (s_start < p_end | |
5428 | || p_end < p_start)) | |
252b5132 | 5429 | { |
4eca0228 | 5430 | _bfd_error_handler |
695344c0 | 5431 | /* xgettext:c-format */ |
b5599592 AM |
5432 | (_("%B: section %A lma %#lx adjusted to %#lx"), abfd, sec, |
5433 | (unsigned long) s_start, (unsigned long) p_end); | |
88967714 | 5434 | adjust = 0; |
b5599592 | 5435 | sec->lma = p_end; |
1cfb7d1e | 5436 | } |
3ac9b6c9 | 5437 | p->p_memsz += adjust; |
1cfb7d1e | 5438 | |
88967714 AM |
5439 | if (this_hdr->sh_type != SHT_NOBITS) |
5440 | { | |
32812159 AM |
5441 | if (p->p_filesz + adjust < p->p_memsz) |
5442 | { | |
5443 | /* We have a PROGBITS section following NOBITS ones. | |
5444 | Allocate file space for the NOBITS section(s) and | |
5445 | zero it. */ | |
5446 | adjust = p->p_memsz - p->p_filesz; | |
5447 | if (!write_zeros (abfd, off, adjust)) | |
5448 | return FALSE; | |
5449 | } | |
88967714 AM |
5450 | off += adjust; |
5451 | p->p_filesz += adjust; | |
252b5132 | 5452 | } |
252b5132 RH |
5453 | } |
5454 | ||
5455 | if (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core) | |
5456 | { | |
b301b248 AM |
5457 | /* The section at i == 0 is the one that actually contains |
5458 | everything. */ | |
4a938328 MS |
5459 | if (i == 0) |
5460 | { | |
627b32bc | 5461 | this_hdr->sh_offset = sec->filepos = off; |
6a3cd2b4 AM |
5462 | off += this_hdr->sh_size; |
5463 | p->p_filesz = this_hdr->sh_size; | |
b301b248 AM |
5464 | p->p_memsz = 0; |
5465 | p->p_align = 1; | |
252b5132 | 5466 | } |
4a938328 | 5467 | else |
252b5132 | 5468 | { |
b301b248 | 5469 | /* The rest are fake sections that shouldn't be written. */ |
252b5132 | 5470 | sec->filepos = 0; |
eea6121a | 5471 | sec->size = 0; |
b301b248 AM |
5472 | sec->flags = 0; |
5473 | continue; | |
252b5132 | 5474 | } |
252b5132 RH |
5475 | } |
5476 | else | |
5477 | { | |
1e951488 | 5478 | if (p->p_type == PT_LOAD) |
b301b248 | 5479 | { |
1e951488 AM |
5480 | this_hdr->sh_offset = sec->filepos = off; |
5481 | if (this_hdr->sh_type != SHT_NOBITS) | |
5482 | off += this_hdr->sh_size; | |
5483 | } | |
5484 | else if (this_hdr->sh_type == SHT_NOBITS | |
5485 | && (this_hdr->sh_flags & SHF_TLS) != 0 | |
5486 | && this_hdr->sh_offset == 0) | |
5487 | { | |
5488 | /* This is a .tbss section that didn't get a PT_LOAD. | |
5489 | (See _bfd_elf_map_sections_to_segments "Create a | |
5490 | final PT_LOAD".) Set sh_offset to the value it | |
5491 | would have if we had created a zero p_filesz and | |
5492 | p_memsz PT_LOAD header for the section. This | |
5493 | also makes the PT_TLS header have the same | |
5494 | p_offset value. */ | |
5495 | bfd_vma adjust = vma_page_aligned_bias (this_hdr->sh_addr, | |
5496 | off, align); | |
5497 | this_hdr->sh_offset = sec->filepos = off + adjust; | |
b301b248 | 5498 | } |
252b5132 | 5499 | |
02bf8d82 | 5500 | if (this_hdr->sh_type != SHT_NOBITS) |
b301b248 | 5501 | { |
6a3cd2b4 | 5502 | p->p_filesz += this_hdr->sh_size; |
02bf8d82 AM |
5503 | /* A load section without SHF_ALLOC is something like |
5504 | a note section in a PT_NOTE segment. These take | |
5505 | file space but are not loaded into memory. */ | |
5506 | if ((this_hdr->sh_flags & SHF_ALLOC) != 0) | |
6a3cd2b4 | 5507 | p->p_memsz += this_hdr->sh_size; |
b301b248 | 5508 | } |
6a3cd2b4 | 5509 | else if ((this_hdr->sh_flags & SHF_ALLOC) != 0) |
13ae64f3 | 5510 | { |
6a3cd2b4 AM |
5511 | if (p->p_type == PT_TLS) |
5512 | p->p_memsz += this_hdr->sh_size; | |
5513 | ||
5514 | /* .tbss is special. It doesn't contribute to p_memsz of | |
5515 | normal segments. */ | |
5516 | else if ((this_hdr->sh_flags & SHF_TLS) == 0) | |
5517 | p->p_memsz += this_hdr->sh_size; | |
13ae64f3 JJ |
5518 | } |
5519 | ||
b10a8ae0 L |
5520 | if (align > p->p_align |
5521 | && !m->p_align_valid | |
5522 | && (p->p_type != PT_LOAD | |
5523 | || (abfd->flags & D_PAGED) == 0)) | |
252b5132 RH |
5524 | p->p_align = align; |
5525 | } | |
5526 | ||
bf988460 | 5527 | if (!m->p_flags_valid) |
252b5132 RH |
5528 | { |
5529 | p->p_flags |= PF_R; | |
02bf8d82 | 5530 | if ((this_hdr->sh_flags & SHF_EXECINSTR) != 0) |
252b5132 | 5531 | p->p_flags |= PF_X; |
02bf8d82 | 5532 | if ((this_hdr->sh_flags & SHF_WRITE) != 0) |
252b5132 RH |
5533 | p->p_flags |= PF_W; |
5534 | } | |
5535 | } | |
43a8475c | 5536 | |
bf988460 | 5537 | off -= off_adjust; |
0920dee7 | 5538 | |
7c928300 AM |
5539 | /* Check that all sections are in a PT_LOAD segment. |
5540 | Don't check funky gdb generated core files. */ | |
5541 | if (p->p_type == PT_LOAD && bfd_get_format (abfd) != bfd_core) | |
9a83a553 AM |
5542 | { |
5543 | bfd_boolean check_vma = TRUE; | |
5544 | ||
5545 | for (i = 1; i < m->count; i++) | |
5546 | if (m->sections[i]->vma == m->sections[i - 1]->vma | |
5547 | && ELF_SECTION_SIZE (&(elf_section_data (m->sections[i]) | |
5548 | ->this_hdr), p) != 0 | |
5549 | && ELF_SECTION_SIZE (&(elf_section_data (m->sections[i - 1]) | |
5550 | ->this_hdr), p) != 0) | |
0920dee7 | 5551 | { |
9a83a553 AM |
5552 | /* Looks like we have overlays packed into the segment. */ |
5553 | check_vma = FALSE; | |
5554 | break; | |
0920dee7 | 5555 | } |
9a83a553 AM |
5556 | |
5557 | for (i = 0; i < m->count; i++) | |
5558 | { | |
5559 | Elf_Internal_Shdr *this_hdr; | |
5560 | asection *sec; | |
5561 | ||
5562 | sec = m->sections[i]; | |
5563 | this_hdr = &(elf_section_data(sec)->this_hdr); | |
86b2281f AM |
5564 | if (!ELF_SECTION_IN_SEGMENT_1 (this_hdr, p, check_vma, 0) |
5565 | && !ELF_TBSS_SPECIAL (this_hdr, p)) | |
9a83a553 | 5566 | { |
4eca0228 | 5567 | _bfd_error_handler |
695344c0 | 5568 | /* xgettext:c-format */ |
9a83a553 AM |
5569 | (_("%B: section `%A' can't be allocated in segment %d"), |
5570 | abfd, sec, j); | |
5571 | print_segment_map (m); | |
5572 | } | |
5573 | } | |
5574 | } | |
252b5132 RH |
5575 | } |
5576 | ||
12bd6957 | 5577 | elf_next_file_pos (abfd) = off; |
f3520d2f AM |
5578 | return TRUE; |
5579 | } | |
5580 | ||
5581 | /* Assign file positions for the other sections. */ | |
5582 | ||
5583 | static bfd_boolean | |
5584 | assign_file_positions_for_non_load_sections (bfd *abfd, | |
5585 | struct bfd_link_info *link_info) | |
5586 | { | |
5587 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
5588 | Elf_Internal_Shdr **i_shdrpp; | |
e06efbf1 | 5589 | Elf_Internal_Shdr **hdrpp, **end_hdrpp; |
f3520d2f AM |
5590 | Elf_Internal_Phdr *phdrs; |
5591 | Elf_Internal_Phdr *p; | |
5592 | struct elf_segment_map *m; | |
62655c7b | 5593 | struct elf_segment_map *hdrs_segment; |
f3520d2f AM |
5594 | bfd_vma filehdr_vaddr, filehdr_paddr; |
5595 | bfd_vma phdrs_vaddr, phdrs_paddr; | |
5596 | file_ptr off; | |
f3520d2f AM |
5597 | unsigned int count; |
5598 | ||
5c182d5f | 5599 | i_shdrpp = elf_elfsections (abfd); |
e06efbf1 | 5600 | end_hdrpp = i_shdrpp + elf_numsections (abfd); |
12bd6957 | 5601 | off = elf_next_file_pos (abfd); |
e06efbf1 | 5602 | for (hdrpp = i_shdrpp + 1; hdrpp < end_hdrpp; hdrpp++) |
5c182d5f | 5603 | { |
5c182d5f AM |
5604 | Elf_Internal_Shdr *hdr; |
5605 | ||
5606 | hdr = *hdrpp; | |
5607 | if (hdr->bfd_section != NULL | |
252e386e AM |
5608 | && (hdr->bfd_section->filepos != 0 |
5609 | || (hdr->sh_type == SHT_NOBITS | |
5610 | && hdr->contents == NULL))) | |
627b32bc | 5611 | BFD_ASSERT (hdr->sh_offset == hdr->bfd_section->filepos); |
5c182d5f AM |
5612 | else if ((hdr->sh_flags & SHF_ALLOC) != 0) |
5613 | { | |
e8d2ba53 | 5614 | if (hdr->sh_size != 0) |
4eca0228 | 5615 | _bfd_error_handler |
695344c0 | 5616 | /* xgettext:c-format */ |
e8d2ba53 AM |
5617 | (_("%B: warning: allocated section `%s' not in segment"), |
5618 | abfd, | |
5619 | (hdr->bfd_section == NULL | |
5620 | ? "*unknown*" | |
5621 | : hdr->bfd_section->name)); | |
3ba71138 L |
5622 | /* We don't need to page align empty sections. */ |
5623 | if ((abfd->flags & D_PAGED) != 0 && hdr->sh_size != 0) | |
5c182d5f AM |
5624 | off += vma_page_aligned_bias (hdr->sh_addr, off, |
5625 | bed->maxpagesize); | |
5626 | else | |
5627 | off += vma_page_aligned_bias (hdr->sh_addr, off, | |
5628 | hdr->sh_addralign); | |
5629 | off = _bfd_elf_assign_file_position_for_section (hdr, off, | |
5630 | FALSE); | |
5631 | } | |
5632 | else if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA) | |
5633 | && hdr->bfd_section == NULL) | |
0ce398f1 L |
5634 | || (hdr->bfd_section != NULL |
5635 | && (hdr->bfd_section->flags & SEC_ELF_COMPRESS)) | |
5636 | /* Compress DWARF debug sections. */ | |
12bd6957 | 5637 | || hdr == i_shdrpp[elf_onesymtab (abfd)] |
6a40cf0c NC |
5638 | || (elf_symtab_shndx_list (abfd) != NULL |
5639 | && hdr == i_shdrpp[elf_symtab_shndx_list (abfd)->ndx]) | |
3e19fb8f L |
5640 | || hdr == i_shdrpp[elf_strtab_sec (abfd)] |
5641 | || hdr == i_shdrpp[elf_shstrtab_sec (abfd)]) | |
5c182d5f AM |
5642 | hdr->sh_offset = -1; |
5643 | else | |
5644 | off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE); | |
5c182d5f AM |
5645 | } |
5646 | ||
252b5132 RH |
5647 | /* Now that we have set the section file positions, we can set up |
5648 | the file positions for the non PT_LOAD segments. */ | |
f3520d2f AM |
5649 | count = 0; |
5650 | filehdr_vaddr = 0; | |
5651 | filehdr_paddr = 0; | |
5652 | phdrs_vaddr = bed->maxpagesize + bed->s->sizeof_ehdr; | |
5653 | phdrs_paddr = 0; | |
62655c7b | 5654 | hdrs_segment = NULL; |
f3520d2f | 5655 | phdrs = elf_tdata (abfd)->phdr; |
12bd6957 | 5656 | for (m = elf_seg_map (abfd), p = phdrs; m != NULL; m = m->next, p++) |
f3520d2f AM |
5657 | { |
5658 | ++count; | |
5659 | if (p->p_type != PT_LOAD) | |
5660 | continue; | |
5661 | ||
5662 | if (m->includes_filehdr) | |
5663 | { | |
5664 | filehdr_vaddr = p->p_vaddr; | |
5665 | filehdr_paddr = p->p_paddr; | |
5666 | } | |
5667 | if (m->includes_phdrs) | |
5668 | { | |
5669 | phdrs_vaddr = p->p_vaddr; | |
5670 | phdrs_paddr = p->p_paddr; | |
5671 | if (m->includes_filehdr) | |
5672 | { | |
62655c7b | 5673 | hdrs_segment = m; |
f3520d2f AM |
5674 | phdrs_vaddr += bed->s->sizeof_ehdr; |
5675 | phdrs_paddr += bed->s->sizeof_ehdr; | |
5676 | } | |
5677 | } | |
5678 | } | |
5679 | ||
62655c7b RM |
5680 | if (hdrs_segment != NULL && link_info != NULL) |
5681 | { | |
5682 | /* There is a segment that contains both the file headers and the | |
5683 | program headers, so provide a symbol __ehdr_start pointing there. | |
5684 | A program can use this to examine itself robustly. */ | |
5685 | ||
5686 | struct elf_link_hash_entry *hash | |
5687 | = elf_link_hash_lookup (elf_hash_table (link_info), "__ehdr_start", | |
5688 | FALSE, FALSE, TRUE); | |
5689 | /* If the symbol was referenced and not defined, define it. */ | |
5690 | if (hash != NULL | |
5691 | && (hash->root.type == bfd_link_hash_new | |
5692 | || hash->root.type == bfd_link_hash_undefined | |
5693 | || hash->root.type == bfd_link_hash_undefweak | |
5694 | || hash->root.type == bfd_link_hash_common)) | |
5695 | { | |
5696 | asection *s = NULL; | |
5697 | if (hdrs_segment->count != 0) | |
5698 | /* The segment contains sections, so use the first one. */ | |
5699 | s = hdrs_segment->sections[0]; | |
5700 | else | |
5701 | /* Use the first (i.e. lowest-addressed) section in any segment. */ | |
12bd6957 | 5702 | for (m = elf_seg_map (abfd); m != NULL; m = m->next) |
62655c7b RM |
5703 | if (m->count != 0) |
5704 | { | |
5705 | s = m->sections[0]; | |
5706 | break; | |
5707 | } | |
5708 | ||
5709 | if (s != NULL) | |
5710 | { | |
5711 | hash->root.u.def.value = filehdr_vaddr - s->vma; | |
5712 | hash->root.u.def.section = s; | |
5713 | } | |
5714 | else | |
5715 | { | |
5716 | hash->root.u.def.value = filehdr_vaddr; | |
5717 | hash->root.u.def.section = bfd_abs_section_ptr; | |
5718 | } | |
5719 | ||
5720 | hash->root.type = bfd_link_hash_defined; | |
5721 | hash->def_regular = 1; | |
5722 | hash->non_elf = 0; | |
5723 | } | |
5724 | } | |
5725 | ||
12bd6957 | 5726 | for (m = elf_seg_map (abfd), p = phdrs; m != NULL; m = m->next, p++) |
252b5132 | 5727 | { |
129af99f | 5728 | if (p->p_type == PT_GNU_RELRO) |
252b5132 | 5729 | { |
b84a33b5 | 5730 | const Elf_Internal_Phdr *lp; |
3146fac4 | 5731 | struct elf_segment_map *lm; |
1ea63fd2 | 5732 | |
129af99f | 5733 | if (link_info != NULL) |
8c37241b | 5734 | { |
129af99f AS |
5735 | /* During linking the range of the RELRO segment is passed |
5736 | in link_info. */ | |
12bd6957 | 5737 | for (lm = elf_seg_map (abfd), lp = phdrs; |
3146fac4 AM |
5738 | lm != NULL; |
5739 | lm = lm->next, lp++) | |
8c37241b JJ |
5740 | { |
5741 | if (lp->p_type == PT_LOAD | |
b84a33b5 | 5742 | && lp->p_vaddr < link_info->relro_end |
3146fac4 AM |
5743 | && lm->count != 0 |
5744 | && lm->sections[0]->vma >= link_info->relro_start) | |
8c37241b JJ |
5745 | break; |
5746 | } | |
8981c88a | 5747 | |
3146fac4 | 5748 | BFD_ASSERT (lm != NULL); |
8c37241b | 5749 | } |
129af99f AS |
5750 | else |
5751 | { | |
5752 | /* Otherwise we are copying an executable or shared | |
b84a33b5 | 5753 | library, but we need to use the same linker logic. */ |
129af99f AS |
5754 | for (lp = phdrs; lp < phdrs + count; ++lp) |
5755 | { | |
5756 | if (lp->p_type == PT_LOAD | |
5757 | && lp->p_paddr == p->p_paddr) | |
5758 | break; | |
5759 | } | |
b84a33b5 AM |
5760 | } |
5761 | ||
5762 | if (lp < phdrs + count) | |
5763 | { | |
5764 | p->p_vaddr = lp->p_vaddr; | |
5765 | p->p_paddr = lp->p_paddr; | |
5766 | p->p_offset = lp->p_offset; | |
5767 | if (link_info != NULL) | |
5768 | p->p_filesz = link_info->relro_end - lp->p_vaddr; | |
5769 | else if (m->p_size_valid) | |
5770 | p->p_filesz = m->p_size; | |
129af99f AS |
5771 | else |
5772 | abort (); | |
b84a33b5 | 5773 | p->p_memsz = p->p_filesz; |
f3944f72 L |
5774 | /* Preserve the alignment and flags if they are valid. The |
5775 | gold linker generates RW/4 for the PT_GNU_RELRO section. | |
5776 | It is better for objcopy/strip to honor these attributes | |
5777 | otherwise gdb will choke when using separate debug files. | |
5778 | */ | |
5779 | if (!m->p_align_valid) | |
5780 | p->p_align = 1; | |
5781 | if (!m->p_flags_valid) | |
341b8ee7 | 5782 | p->p_flags = PF_R; |
129af99f | 5783 | } |
9433b9b1 | 5784 | else |
b84a33b5 AM |
5785 | { |
5786 | memset (p, 0, sizeof *p); | |
5787 | p->p_type = PT_NULL; | |
5788 | } | |
129af99f | 5789 | } |
04c3a755 NS |
5790 | else if (p->p_type == PT_GNU_STACK) |
5791 | { | |
5792 | if (m->p_size_valid) | |
5793 | p->p_memsz = m->p_size; | |
5794 | } | |
129af99f AS |
5795 | else if (m->count != 0) |
5796 | { | |
e06efbf1 | 5797 | unsigned int i; |
1a9ccd70 | 5798 | |
129af99f AS |
5799 | if (p->p_type != PT_LOAD |
5800 | && (p->p_type != PT_NOTE | |
5801 | || bfd_get_format (abfd) != bfd_core)) | |
5802 | { | |
1a9ccd70 NC |
5803 | /* A user specified segment layout may include a PHDR |
5804 | segment that overlaps with a LOAD segment... */ | |
5805 | if (p->p_type == PT_PHDR) | |
5806 | { | |
5807 | m->count = 0; | |
5808 | continue; | |
5809 | } | |
5810 | ||
c86934ce NC |
5811 | if (m->includes_filehdr || m->includes_phdrs) |
5812 | { | |
b1fa9dd6 | 5813 | /* PR 17512: file: 2195325e. */ |
4eca0228 | 5814 | _bfd_error_handler |
1a9ccd70 NC |
5815 | (_("%B: error: non-load segment %d includes file header and/or program header"), |
5816 | abfd, (int)(p - phdrs)); | |
c86934ce NC |
5817 | return FALSE; |
5818 | } | |
129af99f | 5819 | |
86b2281f | 5820 | p->p_filesz = 0; |
129af99f | 5821 | p->p_offset = m->sections[0]->filepos; |
86b2281f AM |
5822 | for (i = m->count; i-- != 0;) |
5823 | { | |
5824 | asection *sect = m->sections[i]; | |
5825 | Elf_Internal_Shdr *hdr = &elf_section_data (sect)->this_hdr; | |
5826 | if (hdr->sh_type != SHT_NOBITS) | |
5827 | { | |
5828 | p->p_filesz = (sect->filepos - m->sections[0]->filepos | |
5829 | + hdr->sh_size); | |
5830 | break; | |
5831 | } | |
5832 | } | |
129af99f AS |
5833 | } |
5834 | } | |
5835 | else if (m->includes_filehdr) | |
5836 | { | |
5837 | p->p_vaddr = filehdr_vaddr; | |
5838 | if (! m->p_paddr_valid) | |
5839 | p->p_paddr = filehdr_paddr; | |
5840 | } | |
5841 | else if (m->includes_phdrs) | |
5842 | { | |
5843 | p->p_vaddr = phdrs_vaddr; | |
5844 | if (! m->p_paddr_valid) | |
5845 | p->p_paddr = phdrs_paddr; | |
252b5132 RH |
5846 | } |
5847 | } | |
5848 | ||
12bd6957 | 5849 | elf_next_file_pos (abfd) = off; |
252b5132 | 5850 | |
b34976b6 | 5851 | return TRUE; |
252b5132 RH |
5852 | } |
5853 | ||
6a40cf0c NC |
5854 | static elf_section_list * |
5855 | find_section_in_list (unsigned int i, elf_section_list * list) | |
5856 | { | |
5857 | for (;list != NULL; list = list->next) | |
5858 | if (list->ndx == i) | |
5859 | break; | |
5860 | return list; | |
5861 | } | |
5862 | ||
252b5132 RH |
5863 | /* Work out the file positions of all the sections. This is called by |
5864 | _bfd_elf_compute_section_file_positions. All the section sizes and | |
5865 | VMAs must be known before this is called. | |
5866 | ||
e0638f70 AM |
5867 | Reloc sections come in two flavours: Those processed specially as |
5868 | "side-channel" data attached to a section to which they apply, and | |
5869 | those that bfd doesn't process as relocations. The latter sort are | |
5870 | stored in a normal bfd section by bfd_section_from_shdr. We don't | |
5871 | consider the former sort here, unless they form part of the loadable | |
5872 | image. Reloc sections not assigned here will be handled later by | |
5873 | assign_file_positions_for_relocs. | |
252b5132 RH |
5874 | |
5875 | We also don't set the positions of the .symtab and .strtab here. */ | |
5876 | ||
b34976b6 | 5877 | static bfd_boolean |
c84fca4d AO |
5878 | assign_file_positions_except_relocs (bfd *abfd, |
5879 | struct bfd_link_info *link_info) | |
252b5132 | 5880 | { |
5c182d5f AM |
5881 | struct elf_obj_tdata *tdata = elf_tdata (abfd); |
5882 | Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd); | |
9c5bfbb7 | 5883 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
252b5132 RH |
5884 | |
5885 | if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0 | |
5886 | && bfd_get_format (abfd) != bfd_core) | |
5887 | { | |
5c182d5f AM |
5888 | Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd); |
5889 | unsigned int num_sec = elf_numsections (abfd); | |
252b5132 RH |
5890 | Elf_Internal_Shdr **hdrpp; |
5891 | unsigned int i; | |
a485e98e | 5892 | file_ptr off; |
252b5132 RH |
5893 | |
5894 | /* Start after the ELF header. */ | |
5895 | off = i_ehdrp->e_ehsize; | |
5896 | ||
5897 | /* We are not creating an executable, which means that we are | |
5898 | not creating a program header, and that the actual order of | |
5899 | the sections in the file is unimportant. */ | |
9ad5cbcf | 5900 | for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++) |
252b5132 RH |
5901 | { |
5902 | Elf_Internal_Shdr *hdr; | |
5903 | ||
5904 | hdr = *hdrpp; | |
e0638f70 AM |
5905 | if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA) |
5906 | && hdr->bfd_section == NULL) | |
0ce398f1 L |
5907 | || (hdr->bfd_section != NULL |
5908 | && (hdr->bfd_section->flags & SEC_ELF_COMPRESS)) | |
5909 | /* Compress DWARF debug sections. */ | |
12bd6957 | 5910 | || i == elf_onesymtab (abfd) |
6a40cf0c NC |
5911 | || (elf_symtab_shndx_list (abfd) != NULL |
5912 | && hdr == i_shdrpp[elf_symtab_shndx_list (abfd)->ndx]) | |
3e19fb8f L |
5913 | || i == elf_strtab_sec (abfd) |
5914 | || i == elf_shstrtab_sec (abfd)) | |
252b5132 RH |
5915 | { |
5916 | hdr->sh_offset = -1; | |
252b5132 | 5917 | } |
9ad5cbcf | 5918 | else |
b34976b6 | 5919 | off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE); |
252b5132 | 5920 | } |
a485e98e AM |
5921 | |
5922 | elf_next_file_pos (abfd) = off; | |
252b5132 RH |
5923 | } |
5924 | else | |
5925 | { | |
f3520d2f AM |
5926 | unsigned int alloc; |
5927 | ||
252b5132 | 5928 | /* Assign file positions for the loaded sections based on the |
08a40648 | 5929 | assignment of sections to segments. */ |
f3520d2f AM |
5930 | if (!assign_file_positions_for_load_sections (abfd, link_info)) |
5931 | return FALSE; | |
5932 | ||
5933 | /* And for non-load sections. */ | |
5934 | if (!assign_file_positions_for_non_load_sections (abfd, link_info)) | |
5935 | return FALSE; | |
5936 | ||
e36284ab AM |
5937 | if (bed->elf_backend_modify_program_headers != NULL) |
5938 | { | |
5939 | if (!(*bed->elf_backend_modify_program_headers) (abfd, link_info)) | |
5940 | return FALSE; | |
5941 | } | |
5942 | ||
58e7ebac | 5943 | /* Set e_type in ELF header to ET_EXEC for -pie -Ttext-segment=. */ |
0e1862bb | 5944 | if (link_info != NULL && bfd_link_pie (link_info)) |
58e7ebac L |
5945 | { |
5946 | unsigned int num_segments = elf_elfheader (abfd)->e_phnum; | |
5947 | Elf_Internal_Phdr *segment = elf_tdata (abfd)->phdr; | |
5948 | Elf_Internal_Phdr *end_segment = &segment[num_segments]; | |
5949 | ||
5950 | /* Find the lowest p_vaddr in PT_LOAD segments. */ | |
5951 | bfd_vma p_vaddr = (bfd_vma) -1; | |
5952 | for (; segment < end_segment; segment++) | |
5953 | if (segment->p_type == PT_LOAD && p_vaddr > segment->p_vaddr) | |
5954 | p_vaddr = segment->p_vaddr; | |
5955 | ||
5956 | /* Set e_type to ET_EXEC if the lowest p_vaddr in PT_LOAD | |
5957 | segments is non-zero. */ | |
5958 | if (p_vaddr) | |
5959 | i_ehdrp->e_type = ET_EXEC; | |
5960 | } | |
5961 | ||
f3520d2f | 5962 | /* Write out the program headers. */ |
12bd6957 | 5963 | alloc = elf_program_header_size (abfd) / bed->s->sizeof_phdr; |
1a9ccd70 NC |
5964 | |
5965 | /* Sort the program headers into the ordering required by the ELF standard. */ | |
5966 | if (alloc == 0) | |
5967 | return TRUE; | |
5968 | ||
1a9ccd70 NC |
5969 | /* PR ld/20815 - Check that the program header segment, if present, will |
5970 | be loaded into memory. FIXME: The check below is not sufficient as | |
5971 | really all PT_LOAD segments should be checked before issuing an error | |
5972 | message. Plus the PHDR segment does not have to be the first segment | |
5973 | in the program header table. But this version of the check should | |
cd584857 NC |
5974 | catch all real world use cases. |
5975 | ||
5976 | FIXME: We used to have code here to sort the PT_LOAD segments into | |
5977 | ascending order, as per the ELF spec. But this breaks some programs, | |
5978 | including the Linux kernel. But really either the spec should be | |
5979 | changed or the programs updated. */ | |
1a9ccd70 | 5980 | if (alloc > 1 |
cd584857 NC |
5981 | && tdata->phdr[0].p_type == PT_PHDR |
5982 | && ! bed->elf_backend_allow_non_load_phdr (abfd, tdata->phdr, alloc) | |
5983 | && tdata->phdr[1].p_type == PT_LOAD | |
5984 | && (tdata->phdr[1].p_vaddr > tdata->phdr[0].p_vaddr | |
5985 | || (tdata->phdr[1].p_vaddr + tdata->phdr[1].p_memsz) | |
5986 | < (tdata->phdr[0].p_vaddr + tdata->phdr[0].p_memsz))) | |
1a9ccd70 NC |
5987 | { |
5988 | /* The fix for this error is usually to edit the linker script being | |
5989 | used and set up the program headers manually. Either that or | |
5990 | leave room for the headers at the start of the SECTIONS. */ | |
5991 | _bfd_error_handler (_("\ | |
5992 | %B: error: PHDR segment not covered by LOAD segment"), | |
5993 | abfd); | |
1a9ccd70 NC |
5994 | return FALSE; |
5995 | } | |
5996 | ||
f3520d2f | 5997 | if (bfd_seek (abfd, (bfd_signed_vma) bed->s->sizeof_ehdr, SEEK_SET) != 0 |
cd584857 NC |
5998 | || bed->s->write_out_phdrs (abfd, tdata->phdr, alloc) != 0) |
5999 | return FALSE; | |
252b5132 RH |
6000 | } |
6001 | ||
b34976b6 | 6002 | return TRUE; |
252b5132 RH |
6003 | } |
6004 | ||
b34976b6 | 6005 | static bfd_boolean |
217aa764 | 6006 | prep_headers (bfd *abfd) |
252b5132 | 6007 | { |
3d540e93 | 6008 | Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form. */ |
2b0f7ef9 | 6009 | struct elf_strtab_hash *shstrtab; |
9c5bfbb7 | 6010 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
252b5132 RH |
6011 | |
6012 | i_ehdrp = elf_elfheader (abfd); | |
252b5132 | 6013 | |
2b0f7ef9 | 6014 | shstrtab = _bfd_elf_strtab_init (); |
252b5132 | 6015 | if (shstrtab == NULL) |
b34976b6 | 6016 | return FALSE; |
252b5132 RH |
6017 | |
6018 | elf_shstrtab (abfd) = shstrtab; | |
6019 | ||
6020 | i_ehdrp->e_ident[EI_MAG0] = ELFMAG0; | |
6021 | i_ehdrp->e_ident[EI_MAG1] = ELFMAG1; | |
6022 | i_ehdrp->e_ident[EI_MAG2] = ELFMAG2; | |
6023 | i_ehdrp->e_ident[EI_MAG3] = ELFMAG3; | |
6024 | ||
6025 | i_ehdrp->e_ident[EI_CLASS] = bed->s->elfclass; | |
6026 | i_ehdrp->e_ident[EI_DATA] = | |
6027 | bfd_big_endian (abfd) ? ELFDATA2MSB : ELFDATA2LSB; | |
6028 | i_ehdrp->e_ident[EI_VERSION] = bed->s->ev_current; | |
6029 | ||
252b5132 RH |
6030 | if ((abfd->flags & DYNAMIC) != 0) |
6031 | i_ehdrp->e_type = ET_DYN; | |
6032 | else if ((abfd->flags & EXEC_P) != 0) | |
6033 | i_ehdrp->e_type = ET_EXEC; | |
6034 | else if (bfd_get_format (abfd) == bfd_core) | |
6035 | i_ehdrp->e_type = ET_CORE; | |
6036 | else | |
6037 | i_ehdrp->e_type = ET_REL; | |
6038 | ||
6039 | switch (bfd_get_arch (abfd)) | |
6040 | { | |
6041 | case bfd_arch_unknown: | |
6042 | i_ehdrp->e_machine = EM_NONE; | |
6043 | break; | |
aa4f99bb AO |
6044 | |
6045 | /* There used to be a long list of cases here, each one setting | |
6046 | e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE | |
6047 | in the corresponding bfd definition. To avoid duplication, | |
6048 | the switch was removed. Machines that need special handling | |
6049 | can generally do it in elf_backend_final_write_processing(), | |
6050 | unless they need the information earlier than the final write. | |
6051 | Such need can generally be supplied by replacing the tests for | |
6052 | e_machine with the conditions used to determine it. */ | |
252b5132 | 6053 | default: |
9c5bfbb7 AM |
6054 | i_ehdrp->e_machine = bed->elf_machine_code; |
6055 | } | |
aa4f99bb | 6056 | |
252b5132 RH |
6057 | i_ehdrp->e_version = bed->s->ev_current; |
6058 | i_ehdrp->e_ehsize = bed->s->sizeof_ehdr; | |
6059 | ||
c044fabd | 6060 | /* No program header, for now. */ |
252b5132 RH |
6061 | i_ehdrp->e_phoff = 0; |
6062 | i_ehdrp->e_phentsize = 0; | |
6063 | i_ehdrp->e_phnum = 0; | |
6064 | ||
c044fabd | 6065 | /* Each bfd section is section header entry. */ |
252b5132 RH |
6066 | i_ehdrp->e_entry = bfd_get_start_address (abfd); |
6067 | i_ehdrp->e_shentsize = bed->s->sizeof_shdr; | |
6068 | ||
c044fabd | 6069 | /* If we're building an executable, we'll need a program header table. */ |
252b5132 | 6070 | if (abfd->flags & EXEC_P) |
0e71e495 BE |
6071 | /* It all happens later. */ |
6072 | ; | |
252b5132 RH |
6073 | else |
6074 | { | |
6075 | i_ehdrp->e_phentsize = 0; | |
252b5132 RH |
6076 | i_ehdrp->e_phoff = 0; |
6077 | } | |
6078 | ||
6079 | elf_tdata (abfd)->symtab_hdr.sh_name = | |
b34976b6 | 6080 | (unsigned int) _bfd_elf_strtab_add (shstrtab, ".symtab", FALSE); |
252b5132 | 6081 | elf_tdata (abfd)->strtab_hdr.sh_name = |
b34976b6 | 6082 | (unsigned int) _bfd_elf_strtab_add (shstrtab, ".strtab", FALSE); |
252b5132 | 6083 | elf_tdata (abfd)->shstrtab_hdr.sh_name = |
b34976b6 | 6084 | (unsigned int) _bfd_elf_strtab_add (shstrtab, ".shstrtab", FALSE); |
252b5132 | 6085 | if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1 |
17ca87fc | 6086 | || elf_tdata (abfd)->strtab_hdr.sh_name == (unsigned int) -1 |
252b5132 | 6087 | || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1) |
b34976b6 | 6088 | return FALSE; |
252b5132 | 6089 | |
b34976b6 | 6090 | return TRUE; |
252b5132 RH |
6091 | } |
6092 | ||
6093 | /* Assign file positions for all the reloc sections which are not part | |
a485e98e | 6094 | of the loadable file image, and the file position of section headers. */ |
252b5132 | 6095 | |
0ce398f1 L |
6096 | static bfd_boolean |
6097 | _bfd_elf_assign_file_positions_for_non_load (bfd *abfd) | |
252b5132 RH |
6098 | { |
6099 | file_ptr off; | |
e06efbf1 | 6100 | Elf_Internal_Shdr **shdrpp, **end_shdrpp; |
3e19fb8f | 6101 | Elf_Internal_Shdr *shdrp; |
a485e98e AM |
6102 | Elf_Internal_Ehdr *i_ehdrp; |
6103 | const struct elf_backend_data *bed; | |
252b5132 | 6104 | |
12bd6957 | 6105 | off = elf_next_file_pos (abfd); |
252b5132 | 6106 | |
e06efbf1 L |
6107 | shdrpp = elf_elfsections (abfd); |
6108 | end_shdrpp = shdrpp + elf_numsections (abfd); | |
6109 | for (shdrpp++; shdrpp < end_shdrpp; shdrpp++) | |
252b5132 | 6110 | { |
252b5132 | 6111 | shdrp = *shdrpp; |
0ce398f1 L |
6112 | if (shdrp->sh_offset == -1) |
6113 | { | |
3e19fb8f | 6114 | asection *sec = shdrp->bfd_section; |
0ce398f1 L |
6115 | bfd_boolean is_rel = (shdrp->sh_type == SHT_REL |
6116 | || shdrp->sh_type == SHT_RELA); | |
6117 | if (is_rel | |
3e19fb8f | 6118 | || (sec != NULL && (sec->flags & SEC_ELF_COMPRESS))) |
0ce398f1 L |
6119 | { |
6120 | if (!is_rel) | |
6121 | { | |
3e19fb8f L |
6122 | const char *name = sec->name; |
6123 | struct bfd_elf_section_data *d; | |
6124 | ||
0ce398f1 | 6125 | /* Compress DWARF debug sections. */ |
3e19fb8f | 6126 | if (!bfd_compress_section (abfd, sec, |
0ce398f1 L |
6127 | shdrp->contents)) |
6128 | return FALSE; | |
3e19fb8f L |
6129 | |
6130 | if (sec->compress_status == COMPRESS_SECTION_DONE | |
6131 | && (abfd->flags & BFD_COMPRESS_GABI) == 0) | |
6132 | { | |
6133 | /* If section is compressed with zlib-gnu, convert | |
6134 | section name from .debug_* to .zdebug_*. */ | |
6135 | char *new_name | |
6136 | = convert_debug_to_zdebug (abfd, name); | |
6137 | if (new_name == NULL) | |
6138 | return FALSE; | |
6139 | name = new_name; | |
6140 | } | |
dd905818 | 6141 | /* Add section name to section name section. */ |
3e19fb8f L |
6142 | if (shdrp->sh_name != (unsigned int) -1) |
6143 | abort (); | |
6144 | shdrp->sh_name | |
6145 | = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), | |
6146 | name, FALSE); | |
6147 | d = elf_section_data (sec); | |
6148 | ||
dd905818 | 6149 | /* Add reloc section name to section name section. */ |
3e19fb8f L |
6150 | if (d->rel.hdr |
6151 | && !_bfd_elf_set_reloc_sh_name (abfd, | |
6152 | d->rel.hdr, | |
6153 | name, FALSE)) | |
6154 | return FALSE; | |
6155 | if (d->rela.hdr | |
6156 | && !_bfd_elf_set_reloc_sh_name (abfd, | |
6157 | d->rela.hdr, | |
91cb26da | 6158 | name, TRUE)) |
3e19fb8f L |
6159 | return FALSE; |
6160 | ||
0ce398f1 | 6161 | /* Update section size and contents. */ |
3e19fb8f L |
6162 | shdrp->sh_size = sec->size; |
6163 | shdrp->contents = sec->contents; | |
0ce398f1 L |
6164 | shdrp->bfd_section->contents = NULL; |
6165 | } | |
6166 | off = _bfd_elf_assign_file_position_for_section (shdrp, | |
6167 | off, | |
6168 | TRUE); | |
6169 | } | |
6170 | } | |
252b5132 RH |
6171 | } |
6172 | ||
3e19fb8f L |
6173 | /* Place section name section after DWARF debug sections have been |
6174 | compressed. */ | |
6175 | _bfd_elf_strtab_finalize (elf_shstrtab (abfd)); | |
6176 | shdrp = &elf_tdata (abfd)->shstrtab_hdr; | |
6177 | shdrp->sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd)); | |
6178 | off = _bfd_elf_assign_file_position_for_section (shdrp, off, TRUE); | |
6179 | ||
6180 | /* Place the section headers. */ | |
a485e98e AM |
6181 | i_ehdrp = elf_elfheader (abfd); |
6182 | bed = get_elf_backend_data (abfd); | |
6183 | off = align_file_position (off, 1 << bed->s->log_file_align); | |
6184 | i_ehdrp->e_shoff = off; | |
6185 | off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize; | |
12bd6957 | 6186 | elf_next_file_pos (abfd) = off; |
0ce398f1 L |
6187 | |
6188 | return TRUE; | |
252b5132 RH |
6189 | } |
6190 | ||
b34976b6 | 6191 | bfd_boolean |
217aa764 | 6192 | _bfd_elf_write_object_contents (bfd *abfd) |
252b5132 | 6193 | { |
9c5bfbb7 | 6194 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
252b5132 | 6195 | Elf_Internal_Shdr **i_shdrp; |
b34976b6 | 6196 | bfd_boolean failed; |
9ad5cbcf | 6197 | unsigned int count, num_sec; |
30e8ee25 | 6198 | struct elf_obj_tdata *t; |
252b5132 RH |
6199 | |
6200 | if (! abfd->output_has_begun | |
217aa764 | 6201 | && ! _bfd_elf_compute_section_file_positions (abfd, NULL)) |
b34976b6 | 6202 | return FALSE; |
252b5132 RH |
6203 | |
6204 | i_shdrp = elf_elfsections (abfd); | |
252b5132 | 6205 | |
b34976b6 | 6206 | failed = FALSE; |
252b5132 RH |
6207 | bfd_map_over_sections (abfd, bed->s->write_relocs, &failed); |
6208 | if (failed) | |
b34976b6 | 6209 | return FALSE; |
252b5132 | 6210 | |
0ce398f1 L |
6211 | if (!_bfd_elf_assign_file_positions_for_non_load (abfd)) |
6212 | return FALSE; | |
252b5132 | 6213 | |
c044fabd | 6214 | /* After writing the headers, we need to write the sections too... */ |
9ad5cbcf AM |
6215 | num_sec = elf_numsections (abfd); |
6216 | for (count = 1; count < num_sec; count++) | |
252b5132 | 6217 | { |
3e19fb8f L |
6218 | i_shdrp[count]->sh_name |
6219 | = _bfd_elf_strtab_offset (elf_shstrtab (abfd), | |
6220 | i_shdrp[count]->sh_name); | |
252b5132 RH |
6221 | if (bed->elf_backend_section_processing) |
6222 | (*bed->elf_backend_section_processing) (abfd, i_shdrp[count]); | |
6223 | if (i_shdrp[count]->contents) | |
6224 | { | |
dc810e39 AM |
6225 | bfd_size_type amt = i_shdrp[count]->sh_size; |
6226 | ||
252b5132 | 6227 | if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0 |
dc810e39 | 6228 | || bfd_bwrite (i_shdrp[count]->contents, amt, abfd) != amt) |
b34976b6 | 6229 | return FALSE; |
252b5132 RH |
6230 | } |
6231 | } | |
6232 | ||
6233 | /* Write out the section header names. */ | |
30e8ee25 | 6234 | t = elf_tdata (abfd); |
26ae6d5e | 6235 | if (elf_shstrtab (abfd) != NULL |
30e8ee25 | 6236 | && (bfd_seek (abfd, t->shstrtab_hdr.sh_offset, SEEK_SET) != 0 |
08a40648 | 6237 | || !_bfd_elf_strtab_emit (abfd, elf_shstrtab (abfd)))) |
b34976b6 | 6238 | return FALSE; |
252b5132 RH |
6239 | |
6240 | if (bed->elf_backend_final_write_processing) | |
12bd6957 | 6241 | (*bed->elf_backend_final_write_processing) (abfd, elf_linker (abfd)); |
252b5132 | 6242 | |
ff59fc36 RM |
6243 | if (!bed->s->write_shdrs_and_ehdr (abfd)) |
6244 | return FALSE; | |
6245 | ||
6246 | /* This is last since write_shdrs_and_ehdr can touch i_shdrp[0]. */ | |
c0355132 AM |
6247 | if (t->o->build_id.after_write_object_contents != NULL) |
6248 | return (*t->o->build_id.after_write_object_contents) (abfd); | |
ff59fc36 RM |
6249 | |
6250 | return TRUE; | |
252b5132 RH |
6251 | } |
6252 | ||
b34976b6 | 6253 | bfd_boolean |
217aa764 | 6254 | _bfd_elf_write_corefile_contents (bfd *abfd) |
252b5132 | 6255 | { |
c044fabd | 6256 | /* Hopefully this can be done just like an object file. */ |
252b5132 RH |
6257 | return _bfd_elf_write_object_contents (abfd); |
6258 | } | |
c044fabd KH |
6259 | |
6260 | /* Given a section, search the header to find them. */ | |
6261 | ||
cb33740c | 6262 | unsigned int |
198beae2 | 6263 | _bfd_elf_section_from_bfd_section (bfd *abfd, struct bfd_section *asect) |
252b5132 | 6264 | { |
9c5bfbb7 | 6265 | const struct elf_backend_data *bed; |
91d6fa6a | 6266 | unsigned int sec_index; |
252b5132 | 6267 | |
9ad5cbcf AM |
6268 | if (elf_section_data (asect) != NULL |
6269 | && elf_section_data (asect)->this_idx != 0) | |
6270 | return elf_section_data (asect)->this_idx; | |
6271 | ||
6272 | if (bfd_is_abs_section (asect)) | |
91d6fa6a | 6273 | sec_index = SHN_ABS; |
af746e92 | 6274 | else if (bfd_is_com_section (asect)) |
91d6fa6a | 6275 | sec_index = SHN_COMMON; |
af746e92 | 6276 | else if (bfd_is_und_section (asect)) |
91d6fa6a | 6277 | sec_index = SHN_UNDEF; |
af746e92 | 6278 | else |
91d6fa6a | 6279 | sec_index = SHN_BAD; |
252b5132 | 6280 | |
af746e92 | 6281 | bed = get_elf_backend_data (abfd); |
252b5132 RH |
6282 | if (bed->elf_backend_section_from_bfd_section) |
6283 | { | |
91d6fa6a | 6284 | int retval = sec_index; |
9ad5cbcf | 6285 | |
af746e92 AM |
6286 | if ((*bed->elf_backend_section_from_bfd_section) (abfd, asect, &retval)) |
6287 | return retval; | |
252b5132 RH |
6288 | } |
6289 | ||
91d6fa6a | 6290 | if (sec_index == SHN_BAD) |
af746e92 | 6291 | bfd_set_error (bfd_error_nonrepresentable_section); |
252b5132 | 6292 | |
91d6fa6a | 6293 | return sec_index; |
252b5132 RH |
6294 | } |
6295 | ||
6296 | /* Given a BFD symbol, return the index in the ELF symbol table, or -1 | |
6297 | on error. */ | |
6298 | ||
6299 | int | |
217aa764 | 6300 | _bfd_elf_symbol_from_bfd_symbol (bfd *abfd, asymbol **asym_ptr_ptr) |
252b5132 RH |
6301 | { |
6302 | asymbol *asym_ptr = *asym_ptr_ptr; | |
6303 | int idx; | |
6304 | flagword flags = asym_ptr->flags; | |
6305 | ||
6306 | /* When gas creates relocations against local labels, it creates its | |
6307 | own symbol for the section, but does put the symbol into the | |
6308 | symbol chain, so udata is 0. When the linker is generating | |
6309 | relocatable output, this section symbol may be for one of the | |
6310 | input sections rather than the output section. */ | |
6311 | if (asym_ptr->udata.i == 0 | |
6312 | && (flags & BSF_SECTION_SYM) | |
6313 | && asym_ptr->section) | |
6314 | { | |
5372391b | 6315 | asection *sec; |
252b5132 RH |
6316 | int indx; |
6317 | ||
5372391b AM |
6318 | sec = asym_ptr->section; |
6319 | if (sec->owner != abfd && sec->output_section != NULL) | |
6320 | sec = sec->output_section; | |
6321 | if (sec->owner == abfd | |
6322 | && (indx = sec->index) < elf_num_section_syms (abfd) | |
4e89ac30 | 6323 | && elf_section_syms (abfd)[indx] != NULL) |
252b5132 RH |
6324 | asym_ptr->udata.i = elf_section_syms (abfd)[indx]->udata.i; |
6325 | } | |
6326 | ||
6327 | idx = asym_ptr->udata.i; | |
6328 | ||
6329 | if (idx == 0) | |
6330 | { | |
6331 | /* This case can occur when using --strip-symbol on a symbol | |
08a40648 | 6332 | which is used in a relocation entry. */ |
4eca0228 | 6333 | _bfd_error_handler |
695344c0 | 6334 | /* xgettext:c-format */ |
d003868e AM |
6335 | (_("%B: symbol `%s' required but not present"), |
6336 | abfd, bfd_asymbol_name (asym_ptr)); | |
252b5132 RH |
6337 | bfd_set_error (bfd_error_no_symbols); |
6338 | return -1; | |
6339 | } | |
6340 | ||
6341 | #if DEBUG & 4 | |
6342 | { | |
6343 | fprintf (stderr, | |
9ccb8af9 AM |
6344 | "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8lx\n", |
6345 | (long) asym_ptr, asym_ptr->name, idx, (long) flags); | |
252b5132 RH |
6346 | fflush (stderr); |
6347 | } | |
6348 | #endif | |
6349 | ||
6350 | return idx; | |
6351 | } | |
6352 | ||
84d1d650 | 6353 | /* Rewrite program header information. */ |
252b5132 | 6354 | |
b34976b6 | 6355 | static bfd_boolean |
84d1d650 | 6356 | rewrite_elf_program_header (bfd *ibfd, bfd *obfd) |
252b5132 | 6357 | { |
b34976b6 AM |
6358 | Elf_Internal_Ehdr *iehdr; |
6359 | struct elf_segment_map *map; | |
6360 | struct elf_segment_map *map_first; | |
6361 | struct elf_segment_map **pointer_to_map; | |
6362 | Elf_Internal_Phdr *segment; | |
6363 | asection *section; | |
6364 | unsigned int i; | |
6365 | unsigned int num_segments; | |
6366 | bfd_boolean phdr_included = FALSE; | |
5c44b38e | 6367 | bfd_boolean p_paddr_valid; |
b34976b6 AM |
6368 | bfd_vma maxpagesize; |
6369 | struct elf_segment_map *phdr_adjust_seg = NULL; | |
6370 | unsigned int phdr_adjust_num = 0; | |
9c5bfbb7 | 6371 | const struct elf_backend_data *bed; |
bc67d8a6 | 6372 | |
caf47ea6 | 6373 | bed = get_elf_backend_data (ibfd); |
252b5132 RH |
6374 | iehdr = elf_elfheader (ibfd); |
6375 | ||
bc67d8a6 | 6376 | map_first = NULL; |
c044fabd | 6377 | pointer_to_map = &map_first; |
252b5132 RH |
6378 | |
6379 | num_segments = elf_elfheader (ibfd)->e_phnum; | |
bc67d8a6 NC |
6380 | maxpagesize = get_elf_backend_data (obfd)->maxpagesize; |
6381 | ||
6382 | /* Returns the end address of the segment + 1. */ | |
aecc8f8a AM |
6383 | #define SEGMENT_END(segment, start) \ |
6384 | (start + (segment->p_memsz > segment->p_filesz \ | |
6385 | ? segment->p_memsz : segment->p_filesz)) | |
bc67d8a6 | 6386 | |
eecdbe52 JJ |
6387 | #define SECTION_SIZE(section, segment) \ |
6388 | (((section->flags & (SEC_HAS_CONTENTS | SEC_THREAD_LOCAL)) \ | |
6389 | != SEC_THREAD_LOCAL || segment->p_type == PT_TLS) \ | |
eea6121a | 6390 | ? section->size : 0) |
eecdbe52 | 6391 | |
b34976b6 | 6392 | /* Returns TRUE if the given section is contained within |
bc67d8a6 | 6393 | the given segment. VMA addresses are compared. */ |
aecc8f8a AM |
6394 | #define IS_CONTAINED_BY_VMA(section, segment) \ |
6395 | (section->vma >= segment->p_vaddr \ | |
eecdbe52 | 6396 | && (section->vma + SECTION_SIZE (section, segment) \ |
aecc8f8a | 6397 | <= (SEGMENT_END (segment, segment->p_vaddr)))) |
c044fabd | 6398 | |
b34976b6 | 6399 | /* Returns TRUE if the given section is contained within |
bc67d8a6 | 6400 | the given segment. LMA addresses are compared. */ |
aecc8f8a AM |
6401 | #define IS_CONTAINED_BY_LMA(section, segment, base) \ |
6402 | (section->lma >= base \ | |
eecdbe52 | 6403 | && (section->lma + SECTION_SIZE (section, segment) \ |
aecc8f8a | 6404 | <= SEGMENT_END (segment, base))) |
252b5132 | 6405 | |
0efc80c8 L |
6406 | /* Handle PT_NOTE segment. */ |
6407 | #define IS_NOTE(p, s) \ | |
aecc8f8a | 6408 | (p->p_type == PT_NOTE \ |
0efc80c8 | 6409 | && elf_section_type (s) == SHT_NOTE \ |
aecc8f8a | 6410 | && (bfd_vma) s->filepos >= p->p_offset \ |
cb3ff1e5 | 6411 | && ((bfd_vma) s->filepos + s->size \ |
aecc8f8a | 6412 | <= p->p_offset + p->p_filesz)) |
252b5132 | 6413 | |
0efc80c8 L |
6414 | /* Special case: corefile "NOTE" section containing regs, prpsinfo |
6415 | etc. */ | |
6416 | #define IS_COREFILE_NOTE(p, s) \ | |
6417 | (IS_NOTE (p, s) \ | |
6418 | && bfd_get_format (ibfd) == bfd_core \ | |
6419 | && s->vma == 0 \ | |
6420 | && s->lma == 0) | |
6421 | ||
252b5132 RH |
6422 | /* The complicated case when p_vaddr is 0 is to handle the Solaris |
6423 | linker, which generates a PT_INTERP section with p_vaddr and | |
6424 | p_memsz set to 0. */ | |
aecc8f8a AM |
6425 | #define IS_SOLARIS_PT_INTERP(p, s) \ |
6426 | (p->p_vaddr == 0 \ | |
6427 | && p->p_paddr == 0 \ | |
6428 | && p->p_memsz == 0 \ | |
6429 | && p->p_filesz > 0 \ | |
6430 | && (s->flags & SEC_HAS_CONTENTS) != 0 \ | |
eea6121a | 6431 | && s->size > 0 \ |
aecc8f8a | 6432 | && (bfd_vma) s->filepos >= p->p_offset \ |
cb3ff1e5 | 6433 | && ((bfd_vma) s->filepos + s->size \ |
aecc8f8a | 6434 | <= p->p_offset + p->p_filesz)) |
5c440b1e | 6435 | |
bc67d8a6 NC |
6436 | /* Decide if the given section should be included in the given segment. |
6437 | A section will be included if: | |
f5ffc919 | 6438 | 1. It is within the address space of the segment -- we use the LMA |
08a40648 | 6439 | if that is set for the segment and the VMA otherwise, |
0efc80c8 | 6440 | 2. It is an allocated section or a NOTE section in a PT_NOTE |
d324f6d6 | 6441 | segment. |
bc67d8a6 | 6442 | 3. There is an output section associated with it, |
eecdbe52 | 6443 | 4. The section has not already been allocated to a previous segment. |
2b05f1b7 | 6444 | 5. PT_GNU_STACK segments do not include any sections. |
03394ac9 | 6445 | 6. PT_TLS segment includes only SHF_TLS sections. |
6f79b219 JJ |
6446 | 7. SHF_TLS sections are only in PT_TLS or PT_LOAD segments. |
6447 | 8. PT_DYNAMIC should not contain empty sections at the beginning | |
08a40648 | 6448 | (with the possible exception of .dynamic). */ |
9f17e2a6 | 6449 | #define IS_SECTION_IN_INPUT_SEGMENT(section, segment, bed) \ |
2b05f1b7 L |
6450 | ((((segment->p_paddr \ |
6451 | ? IS_CONTAINED_BY_LMA (section, segment, segment->p_paddr) \ | |
6452 | : IS_CONTAINED_BY_VMA (section, segment)) \ | |
6453 | && (section->flags & SEC_ALLOC) != 0) \ | |
0efc80c8 | 6454 | || IS_NOTE (segment, section)) \ |
2b05f1b7 L |
6455 | && segment->p_type != PT_GNU_STACK \ |
6456 | && (segment->p_type != PT_TLS \ | |
6457 | || (section->flags & SEC_THREAD_LOCAL)) \ | |
6458 | && (segment->p_type == PT_LOAD \ | |
6459 | || segment->p_type == PT_TLS \ | |
6460 | || (section->flags & SEC_THREAD_LOCAL) == 0) \ | |
6461 | && (segment->p_type != PT_DYNAMIC \ | |
6462 | || SECTION_SIZE (section, segment) > 0 \ | |
6463 | || (segment->p_paddr \ | |
6464 | ? segment->p_paddr != section->lma \ | |
6465 | : segment->p_vaddr != section->vma) \ | |
6466 | || (strcmp (bfd_get_section_name (ibfd, section), ".dynamic") \ | |
6467 | == 0)) \ | |
0067a569 | 6468 | && !section->segment_mark) |
bc67d8a6 | 6469 | |
9f17e2a6 L |
6470 | /* If the output section of a section in the input segment is NULL, |
6471 | it is removed from the corresponding output segment. */ | |
6472 | #define INCLUDE_SECTION_IN_SEGMENT(section, segment, bed) \ | |
6473 | (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed) \ | |
6474 | && section->output_section != NULL) | |
6475 | ||
b34976b6 | 6476 | /* Returns TRUE iff seg1 starts after the end of seg2. */ |
b5f852ea NC |
6477 | #define SEGMENT_AFTER_SEGMENT(seg1, seg2, field) \ |
6478 | (seg1->field >= SEGMENT_END (seg2, seg2->field)) | |
6479 | ||
6480 | /* Returns TRUE iff seg1 and seg2 overlap. Segments overlap iff both | |
6481 | their VMA address ranges and their LMA address ranges overlap. | |
6482 | It is possible to have overlapping VMA ranges without overlapping LMA | |
6483 | ranges. RedBoot images for example can have both .data and .bss mapped | |
6484 | to the same VMA range, but with the .data section mapped to a different | |
6485 | LMA. */ | |
aecc8f8a | 6486 | #define SEGMENT_OVERLAPS(seg1, seg2) \ |
b5f852ea | 6487 | ( !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_vaddr) \ |
08a40648 | 6488 | || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_vaddr)) \ |
b5f852ea | 6489 | && !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_paddr) \ |
08a40648 | 6490 | || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_paddr))) |
bc67d8a6 NC |
6491 | |
6492 | /* Initialise the segment mark field. */ | |
6493 | for (section = ibfd->sections; section != NULL; section = section->next) | |
b34976b6 | 6494 | section->segment_mark = FALSE; |
bc67d8a6 | 6495 | |
5c44b38e AM |
6496 | /* The Solaris linker creates program headers in which all the |
6497 | p_paddr fields are zero. When we try to objcopy or strip such a | |
6498 | file, we get confused. Check for this case, and if we find it | |
6499 | don't set the p_paddr_valid fields. */ | |
6500 | p_paddr_valid = FALSE; | |
6501 | for (i = 0, segment = elf_tdata (ibfd)->phdr; | |
6502 | i < num_segments; | |
6503 | i++, segment++) | |
6504 | if (segment->p_paddr != 0) | |
6505 | { | |
6506 | p_paddr_valid = TRUE; | |
6507 | break; | |
6508 | } | |
6509 | ||
252b5132 | 6510 | /* Scan through the segments specified in the program header |
bc67d8a6 | 6511 | of the input BFD. For this first scan we look for overlaps |
9ad5cbcf | 6512 | in the loadable segments. These can be created by weird |
aecc8f8a | 6513 | parameters to objcopy. Also, fix some solaris weirdness. */ |
bc67d8a6 NC |
6514 | for (i = 0, segment = elf_tdata (ibfd)->phdr; |
6515 | i < num_segments; | |
c044fabd | 6516 | i++, segment++) |
252b5132 | 6517 | { |
252b5132 | 6518 | unsigned int j; |
c044fabd | 6519 | Elf_Internal_Phdr *segment2; |
252b5132 | 6520 | |
aecc8f8a AM |
6521 | if (segment->p_type == PT_INTERP) |
6522 | for (section = ibfd->sections; section; section = section->next) | |
6523 | if (IS_SOLARIS_PT_INTERP (segment, section)) | |
6524 | { | |
6525 | /* Mininal change so that the normal section to segment | |
4cc11e76 | 6526 | assignment code will work. */ |
aecc8f8a AM |
6527 | segment->p_vaddr = section->vma; |
6528 | break; | |
6529 | } | |
6530 | ||
bc67d8a6 | 6531 | if (segment->p_type != PT_LOAD) |
b10a8ae0 L |
6532 | { |
6533 | /* Remove PT_GNU_RELRO segment. */ | |
6534 | if (segment->p_type == PT_GNU_RELRO) | |
6535 | segment->p_type = PT_NULL; | |
6536 | continue; | |
6537 | } | |
c044fabd | 6538 | |
bc67d8a6 | 6539 | /* Determine if this segment overlaps any previous segments. */ |
0067a569 | 6540 | for (j = 0, segment2 = elf_tdata (ibfd)->phdr; j < i; j++, segment2++) |
bc67d8a6 NC |
6541 | { |
6542 | bfd_signed_vma extra_length; | |
c044fabd | 6543 | |
bc67d8a6 | 6544 | if (segment2->p_type != PT_LOAD |
0067a569 | 6545 | || !SEGMENT_OVERLAPS (segment, segment2)) |
bc67d8a6 | 6546 | continue; |
c044fabd | 6547 | |
bc67d8a6 NC |
6548 | /* Merge the two segments together. */ |
6549 | if (segment2->p_vaddr < segment->p_vaddr) | |
6550 | { | |
c044fabd | 6551 | /* Extend SEGMENT2 to include SEGMENT and then delete |
08a40648 | 6552 | SEGMENT. */ |
0067a569 AM |
6553 | extra_length = (SEGMENT_END (segment, segment->p_vaddr) |
6554 | - SEGMENT_END (segment2, segment2->p_vaddr)); | |
c044fabd | 6555 | |
bc67d8a6 NC |
6556 | if (extra_length > 0) |
6557 | { | |
0067a569 | 6558 | segment2->p_memsz += extra_length; |
bc67d8a6 NC |
6559 | segment2->p_filesz += extra_length; |
6560 | } | |
c044fabd | 6561 | |
bc67d8a6 | 6562 | segment->p_type = PT_NULL; |
c044fabd | 6563 | |
bc67d8a6 NC |
6564 | /* Since we have deleted P we must restart the outer loop. */ |
6565 | i = 0; | |
6566 | segment = elf_tdata (ibfd)->phdr; | |
6567 | break; | |
6568 | } | |
6569 | else | |
6570 | { | |
c044fabd | 6571 | /* Extend SEGMENT to include SEGMENT2 and then delete |
08a40648 | 6572 | SEGMENT2. */ |
0067a569 AM |
6573 | extra_length = (SEGMENT_END (segment2, segment2->p_vaddr) |
6574 | - SEGMENT_END (segment, segment->p_vaddr)); | |
c044fabd | 6575 | |
bc67d8a6 NC |
6576 | if (extra_length > 0) |
6577 | { | |
0067a569 | 6578 | segment->p_memsz += extra_length; |
bc67d8a6 NC |
6579 | segment->p_filesz += extra_length; |
6580 | } | |
c044fabd | 6581 | |
bc67d8a6 NC |
6582 | segment2->p_type = PT_NULL; |
6583 | } | |
6584 | } | |
6585 | } | |
c044fabd | 6586 | |
bc67d8a6 NC |
6587 | /* The second scan attempts to assign sections to segments. */ |
6588 | for (i = 0, segment = elf_tdata (ibfd)->phdr; | |
6589 | i < num_segments; | |
0067a569 | 6590 | i++, segment++) |
bc67d8a6 | 6591 | { |
0067a569 AM |
6592 | unsigned int section_count; |
6593 | asection **sections; | |
6594 | asection *output_section; | |
6595 | unsigned int isec; | |
6596 | bfd_vma matching_lma; | |
6597 | bfd_vma suggested_lma; | |
6598 | unsigned int j; | |
dc810e39 | 6599 | bfd_size_type amt; |
0067a569 AM |
6600 | asection *first_section; |
6601 | bfd_boolean first_matching_lma; | |
6602 | bfd_boolean first_suggested_lma; | |
bc67d8a6 NC |
6603 | |
6604 | if (segment->p_type == PT_NULL) | |
6605 | continue; | |
c044fabd | 6606 | |
9f17e2a6 | 6607 | first_section = NULL; |
bc67d8a6 | 6608 | /* Compute how many sections might be placed into this segment. */ |
b5f852ea NC |
6609 | for (section = ibfd->sections, section_count = 0; |
6610 | section != NULL; | |
6611 | section = section->next) | |
9f17e2a6 L |
6612 | { |
6613 | /* Find the first section in the input segment, which may be | |
6614 | removed from the corresponding output segment. */ | |
6615 | if (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed)) | |
6616 | { | |
6617 | if (first_section == NULL) | |
6618 | first_section = section; | |
6619 | if (section->output_section != NULL) | |
6620 | ++section_count; | |
6621 | } | |
6622 | } | |
811072d8 | 6623 | |
b5f852ea NC |
6624 | /* Allocate a segment map big enough to contain |
6625 | all of the sections we have selected. */ | |
dc810e39 AM |
6626 | amt = sizeof (struct elf_segment_map); |
6627 | amt += ((bfd_size_type) section_count - 1) * sizeof (asection *); | |
a50b1753 | 6628 | map = (struct elf_segment_map *) bfd_zalloc (obfd, amt); |
bc67d8a6 | 6629 | if (map == NULL) |
b34976b6 | 6630 | return FALSE; |
252b5132 RH |
6631 | |
6632 | /* Initialise the fields of the segment map. Default to | |
6633 | using the physical address of the segment in the input BFD. */ | |
0067a569 AM |
6634 | map->next = NULL; |
6635 | map->p_type = segment->p_type; | |
6636 | map->p_flags = segment->p_flags; | |
bc67d8a6 | 6637 | map->p_flags_valid = 1; |
55d55ac7 | 6638 | |
9f17e2a6 L |
6639 | /* If the first section in the input segment is removed, there is |
6640 | no need to preserve segment physical address in the corresponding | |
6641 | output segment. */ | |
945c025a | 6642 | if (!first_section || first_section->output_section != NULL) |
9f17e2a6 L |
6643 | { |
6644 | map->p_paddr = segment->p_paddr; | |
5c44b38e | 6645 | map->p_paddr_valid = p_paddr_valid; |
9f17e2a6 | 6646 | } |
252b5132 RH |
6647 | |
6648 | /* Determine if this segment contains the ELF file header | |
6649 | and if it contains the program headers themselves. */ | |
bc67d8a6 NC |
6650 | map->includes_filehdr = (segment->p_offset == 0 |
6651 | && segment->p_filesz >= iehdr->e_ehsize); | |
bc67d8a6 | 6652 | map->includes_phdrs = 0; |
252b5132 | 6653 | |
0067a569 | 6654 | if (!phdr_included || segment->p_type != PT_LOAD) |
252b5132 | 6655 | { |
bc67d8a6 NC |
6656 | map->includes_phdrs = |
6657 | (segment->p_offset <= (bfd_vma) iehdr->e_phoff | |
6658 | && (segment->p_offset + segment->p_filesz | |
252b5132 RH |
6659 | >= ((bfd_vma) iehdr->e_phoff |
6660 | + iehdr->e_phnum * iehdr->e_phentsize))); | |
c044fabd | 6661 | |
bc67d8a6 | 6662 | if (segment->p_type == PT_LOAD && map->includes_phdrs) |
b34976b6 | 6663 | phdr_included = TRUE; |
252b5132 RH |
6664 | } |
6665 | ||
bc67d8a6 | 6666 | if (section_count == 0) |
252b5132 RH |
6667 | { |
6668 | /* Special segments, such as the PT_PHDR segment, may contain | |
6669 | no sections, but ordinary, loadable segments should contain | |
1ed89aa9 NC |
6670 | something. They are allowed by the ELF spec however, so only |
6671 | a warning is produced. */ | |
bc67d8a6 | 6672 | if (segment->p_type == PT_LOAD) |
4eca0228 | 6673 | _bfd_error_handler (_("\ |
c86934ce | 6674 | %B: warning: Empty loadable segment detected, is this intentional ?"), |
4eca0228 | 6675 | ibfd); |
252b5132 | 6676 | |
bc67d8a6 | 6677 | map->count = 0; |
c044fabd KH |
6678 | *pointer_to_map = map; |
6679 | pointer_to_map = &map->next; | |
252b5132 RH |
6680 | |
6681 | continue; | |
6682 | } | |
6683 | ||
6684 | /* Now scan the sections in the input BFD again and attempt | |
6685 | to add their corresponding output sections to the segment map. | |
6686 | The problem here is how to handle an output section which has | |
6687 | been moved (ie had its LMA changed). There are four possibilities: | |
6688 | ||
6689 | 1. None of the sections have been moved. | |
6690 | In this case we can continue to use the segment LMA from the | |
6691 | input BFD. | |
6692 | ||
6693 | 2. All of the sections have been moved by the same amount. | |
6694 | In this case we can change the segment's LMA to match the LMA | |
6695 | of the first section. | |
6696 | ||
6697 | 3. Some of the sections have been moved, others have not. | |
6698 | In this case those sections which have not been moved can be | |
6699 | placed in the current segment which will have to have its size, | |
6700 | and possibly its LMA changed, and a new segment or segments will | |
6701 | have to be created to contain the other sections. | |
6702 | ||
b5f852ea | 6703 | 4. The sections have been moved, but not by the same amount. |
252b5132 RH |
6704 | In this case we can change the segment's LMA to match the LMA |
6705 | of the first section and we will have to create a new segment | |
6706 | or segments to contain the other sections. | |
6707 | ||
6708 | In order to save time, we allocate an array to hold the section | |
6709 | pointers that we are interested in. As these sections get assigned | |
6710 | to a segment, they are removed from this array. */ | |
6711 | ||
a50b1753 | 6712 | sections = (asection **) bfd_malloc2 (section_count, sizeof (asection *)); |
252b5132 | 6713 | if (sections == NULL) |
b34976b6 | 6714 | return FALSE; |
252b5132 RH |
6715 | |
6716 | /* Step One: Scan for segment vs section LMA conflicts. | |
6717 | Also add the sections to the section array allocated above. | |
6718 | Also add the sections to the current segment. In the common | |
6719 | case, where the sections have not been moved, this means that | |
6720 | we have completely filled the segment, and there is nothing | |
6721 | more to do. */ | |
252b5132 | 6722 | isec = 0; |
72730e0c | 6723 | matching_lma = 0; |
252b5132 | 6724 | suggested_lma = 0; |
0067a569 AM |
6725 | first_matching_lma = TRUE; |
6726 | first_suggested_lma = TRUE; | |
252b5132 | 6727 | |
461c4b2e | 6728 | for (section = first_section, j = 0; |
bc67d8a6 NC |
6729 | section != NULL; |
6730 | section = section->next) | |
252b5132 | 6731 | { |
caf47ea6 | 6732 | if (INCLUDE_SECTION_IN_SEGMENT (section, segment, bed)) |
c0f7859b | 6733 | { |
bc67d8a6 NC |
6734 | output_section = section->output_section; |
6735 | ||
0067a569 | 6736 | sections[j++] = section; |
252b5132 RH |
6737 | |
6738 | /* The Solaris native linker always sets p_paddr to 0. | |
6739 | We try to catch that case here, and set it to the | |
5e8d7549 NC |
6740 | correct value. Note - some backends require that |
6741 | p_paddr be left as zero. */ | |
5c44b38e | 6742 | if (!p_paddr_valid |
4455705d | 6743 | && segment->p_vaddr != 0 |
0067a569 | 6744 | && !bed->want_p_paddr_set_to_zero |
252b5132 | 6745 | && isec == 0 |
bc67d8a6 | 6746 | && output_section->lma != 0 |
0067a569 AM |
6747 | && output_section->vma == (segment->p_vaddr |
6748 | + (map->includes_filehdr | |
6749 | ? iehdr->e_ehsize | |
6750 | : 0) | |
6751 | + (map->includes_phdrs | |
6752 | ? (iehdr->e_phnum | |
6753 | * iehdr->e_phentsize) | |
6754 | : 0))) | |
bc67d8a6 | 6755 | map->p_paddr = segment->p_vaddr; |
252b5132 RH |
6756 | |
6757 | /* Match up the physical address of the segment with the | |
6758 | LMA address of the output section. */ | |
bc67d8a6 | 6759 | if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr) |
5e8d7549 | 6760 | || IS_COREFILE_NOTE (segment, section) |
0067a569 AM |
6761 | || (bed->want_p_paddr_set_to_zero |
6762 | && IS_CONTAINED_BY_VMA (output_section, segment))) | |
252b5132 | 6763 | { |
0067a569 AM |
6764 | if (first_matching_lma || output_section->lma < matching_lma) |
6765 | { | |
6766 | matching_lma = output_section->lma; | |
6767 | first_matching_lma = FALSE; | |
6768 | } | |
252b5132 RH |
6769 | |
6770 | /* We assume that if the section fits within the segment | |
bc67d8a6 | 6771 | then it does not overlap any other section within that |
252b5132 | 6772 | segment. */ |
0067a569 AM |
6773 | map->sections[isec++] = output_section; |
6774 | } | |
6775 | else if (first_suggested_lma) | |
6776 | { | |
6777 | suggested_lma = output_section->lma; | |
6778 | first_suggested_lma = FALSE; | |
252b5132 | 6779 | } |
147d51c2 L |
6780 | |
6781 | if (j == section_count) | |
6782 | break; | |
252b5132 RH |
6783 | } |
6784 | } | |
6785 | ||
bc67d8a6 | 6786 | BFD_ASSERT (j == section_count); |
252b5132 RH |
6787 | |
6788 | /* Step Two: Adjust the physical address of the current segment, | |
6789 | if necessary. */ | |
bc67d8a6 | 6790 | if (isec == section_count) |
252b5132 RH |
6791 | { |
6792 | /* All of the sections fitted within the segment as currently | |
6793 | specified. This is the default case. Add the segment to | |
6794 | the list of built segments and carry on to process the next | |
6795 | program header in the input BFD. */ | |
bc67d8a6 | 6796 | map->count = section_count; |
c044fabd KH |
6797 | *pointer_to_map = map; |
6798 | pointer_to_map = &map->next; | |
08a40648 | 6799 | |
5c44b38e AM |
6800 | if (p_paddr_valid |
6801 | && !bed->want_p_paddr_set_to_zero | |
147d51c2 | 6802 | && matching_lma != map->p_paddr |
5c44b38e AM |
6803 | && !map->includes_filehdr |
6804 | && !map->includes_phdrs) | |
3271a814 NS |
6805 | /* There is some padding before the first section in the |
6806 | segment. So, we must account for that in the output | |
6807 | segment's vma. */ | |
6808 | map->p_vaddr_offset = matching_lma - map->p_paddr; | |
08a40648 | 6809 | |
252b5132 RH |
6810 | free (sections); |
6811 | continue; | |
6812 | } | |
252b5132 RH |
6813 | else |
6814 | { | |
0067a569 | 6815 | if (!first_matching_lma) |
72730e0c AM |
6816 | { |
6817 | /* At least one section fits inside the current segment. | |
6818 | Keep it, but modify its physical address to match the | |
6819 | LMA of the first section that fitted. */ | |
bc67d8a6 | 6820 | map->p_paddr = matching_lma; |
72730e0c AM |
6821 | } |
6822 | else | |
6823 | { | |
6824 | /* None of the sections fitted inside the current segment. | |
6825 | Change the current segment's physical address to match | |
6826 | the LMA of the first section. */ | |
bc67d8a6 | 6827 | map->p_paddr = suggested_lma; |
72730e0c AM |
6828 | } |
6829 | ||
bc67d8a6 NC |
6830 | /* Offset the segment physical address from the lma |
6831 | to allow for space taken up by elf headers. */ | |
6832 | if (map->includes_filehdr) | |
010c8431 AM |
6833 | { |
6834 | if (map->p_paddr >= iehdr->e_ehsize) | |
6835 | map->p_paddr -= iehdr->e_ehsize; | |
6836 | else | |
6837 | { | |
6838 | map->includes_filehdr = FALSE; | |
6839 | map->includes_phdrs = FALSE; | |
6840 | } | |
6841 | } | |
252b5132 | 6842 | |
bc67d8a6 NC |
6843 | if (map->includes_phdrs) |
6844 | { | |
010c8431 AM |
6845 | if (map->p_paddr >= iehdr->e_phnum * iehdr->e_phentsize) |
6846 | { | |
6847 | map->p_paddr -= iehdr->e_phnum * iehdr->e_phentsize; | |
6848 | ||
6849 | /* iehdr->e_phnum is just an estimate of the number | |
6850 | of program headers that we will need. Make a note | |
6851 | here of the number we used and the segment we chose | |
6852 | to hold these headers, so that we can adjust the | |
6853 | offset when we know the correct value. */ | |
6854 | phdr_adjust_num = iehdr->e_phnum; | |
6855 | phdr_adjust_seg = map; | |
6856 | } | |
6857 | else | |
6858 | map->includes_phdrs = FALSE; | |
bc67d8a6 | 6859 | } |
252b5132 RH |
6860 | } |
6861 | ||
6862 | /* Step Three: Loop over the sections again, this time assigning | |
caf47ea6 | 6863 | those that fit to the current segment and removing them from the |
252b5132 RH |
6864 | sections array; but making sure not to leave large gaps. Once all |
6865 | possible sections have been assigned to the current segment it is | |
6866 | added to the list of built segments and if sections still remain | |
6867 | to be assigned, a new segment is constructed before repeating | |
6868 | the loop. */ | |
6869 | isec = 0; | |
6870 | do | |
6871 | { | |
bc67d8a6 | 6872 | map->count = 0; |
252b5132 | 6873 | suggested_lma = 0; |
0067a569 | 6874 | first_suggested_lma = TRUE; |
252b5132 RH |
6875 | |
6876 | /* Fill the current segment with sections that fit. */ | |
bc67d8a6 | 6877 | for (j = 0; j < section_count; j++) |
252b5132 | 6878 | { |
bc67d8a6 | 6879 | section = sections[j]; |
252b5132 | 6880 | |
bc67d8a6 | 6881 | if (section == NULL) |
252b5132 RH |
6882 | continue; |
6883 | ||
bc67d8a6 | 6884 | output_section = section->output_section; |
252b5132 | 6885 | |
bc67d8a6 | 6886 | BFD_ASSERT (output_section != NULL); |
c044fabd | 6887 | |
bc67d8a6 NC |
6888 | if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr) |
6889 | || IS_COREFILE_NOTE (segment, section)) | |
252b5132 | 6890 | { |
bc67d8a6 | 6891 | if (map->count == 0) |
252b5132 RH |
6892 | { |
6893 | /* If the first section in a segment does not start at | |
bc67d8a6 NC |
6894 | the beginning of the segment, then something is |
6895 | wrong. */ | |
0067a569 AM |
6896 | if (output_section->lma |
6897 | != (map->p_paddr | |
6898 | + (map->includes_filehdr ? iehdr->e_ehsize : 0) | |
6899 | + (map->includes_phdrs | |
6900 | ? iehdr->e_phnum * iehdr->e_phentsize | |
6901 | : 0))) | |
252b5132 RH |
6902 | abort (); |
6903 | } | |
6904 | else | |
6905 | { | |
0067a569 | 6906 | asection *prev_sec; |
252b5132 | 6907 | |
bc67d8a6 | 6908 | prev_sec = map->sections[map->count - 1]; |
252b5132 RH |
6909 | |
6910 | /* If the gap between the end of the previous section | |
bc67d8a6 NC |
6911 | and the start of this section is more than |
6912 | maxpagesize then we need to start a new segment. */ | |
eea6121a | 6913 | if ((BFD_ALIGN (prev_sec->lma + prev_sec->size, |
079e9a2f | 6914 | maxpagesize) |
caf47ea6 | 6915 | < BFD_ALIGN (output_section->lma, maxpagesize)) |
0067a569 | 6916 | || (prev_sec->lma + prev_sec->size |
079e9a2f | 6917 | > output_section->lma)) |
252b5132 | 6918 | { |
0067a569 AM |
6919 | if (first_suggested_lma) |
6920 | { | |
6921 | suggested_lma = output_section->lma; | |
6922 | first_suggested_lma = FALSE; | |
6923 | } | |
252b5132 RH |
6924 | |
6925 | continue; | |
6926 | } | |
6927 | } | |
6928 | ||
bc67d8a6 | 6929 | map->sections[map->count++] = output_section; |
252b5132 RH |
6930 | ++isec; |
6931 | sections[j] = NULL; | |
b34976b6 | 6932 | section->segment_mark = TRUE; |
252b5132 | 6933 | } |
0067a569 AM |
6934 | else if (first_suggested_lma) |
6935 | { | |
6936 | suggested_lma = output_section->lma; | |
6937 | first_suggested_lma = FALSE; | |
6938 | } | |
252b5132 RH |
6939 | } |
6940 | ||
bc67d8a6 | 6941 | BFD_ASSERT (map->count > 0); |
252b5132 RH |
6942 | |
6943 | /* Add the current segment to the list of built segments. */ | |
c044fabd KH |
6944 | *pointer_to_map = map; |
6945 | pointer_to_map = &map->next; | |
252b5132 | 6946 | |
bc67d8a6 | 6947 | if (isec < section_count) |
252b5132 RH |
6948 | { |
6949 | /* We still have not allocated all of the sections to | |
6950 | segments. Create a new segment here, initialise it | |
6951 | and carry on looping. */ | |
dc810e39 AM |
6952 | amt = sizeof (struct elf_segment_map); |
6953 | amt += ((bfd_size_type) section_count - 1) * sizeof (asection *); | |
5964fc3a | 6954 | map = (struct elf_segment_map *) bfd_zalloc (obfd, amt); |
bc67d8a6 | 6955 | if (map == NULL) |
5ed6aba4 NC |
6956 | { |
6957 | free (sections); | |
6958 | return FALSE; | |
6959 | } | |
252b5132 RH |
6960 | |
6961 | /* Initialise the fields of the segment map. Set the physical | |
6962 | physical address to the LMA of the first section that has | |
6963 | not yet been assigned. */ | |
0067a569 AM |
6964 | map->next = NULL; |
6965 | map->p_type = segment->p_type; | |
6966 | map->p_flags = segment->p_flags; | |
6967 | map->p_flags_valid = 1; | |
6968 | map->p_paddr = suggested_lma; | |
5c44b38e | 6969 | map->p_paddr_valid = p_paddr_valid; |
bc67d8a6 | 6970 | map->includes_filehdr = 0; |
0067a569 | 6971 | map->includes_phdrs = 0; |
252b5132 RH |
6972 | } |
6973 | } | |
bc67d8a6 | 6974 | while (isec < section_count); |
252b5132 RH |
6975 | |
6976 | free (sections); | |
6977 | } | |
6978 | ||
12bd6957 | 6979 | elf_seg_map (obfd) = map_first; |
bc67d8a6 NC |
6980 | |
6981 | /* If we had to estimate the number of program headers that were | |
9ad5cbcf | 6982 | going to be needed, then check our estimate now and adjust |
bc67d8a6 NC |
6983 | the offset if necessary. */ |
6984 | if (phdr_adjust_seg != NULL) | |
6985 | { | |
6986 | unsigned int count; | |
c044fabd | 6987 | |
bc67d8a6 | 6988 | for (count = 0, map = map_first; map != NULL; map = map->next) |
c044fabd | 6989 | count++; |
252b5132 | 6990 | |
bc67d8a6 NC |
6991 | if (count > phdr_adjust_num) |
6992 | phdr_adjust_seg->p_paddr | |
6993 | -= (count - phdr_adjust_num) * iehdr->e_phentsize; | |
6994 | } | |
c044fabd | 6995 | |
bc67d8a6 | 6996 | #undef SEGMENT_END |
eecdbe52 | 6997 | #undef SECTION_SIZE |
bc67d8a6 NC |
6998 | #undef IS_CONTAINED_BY_VMA |
6999 | #undef IS_CONTAINED_BY_LMA | |
0efc80c8 | 7000 | #undef IS_NOTE |
252b5132 | 7001 | #undef IS_COREFILE_NOTE |
bc67d8a6 | 7002 | #undef IS_SOLARIS_PT_INTERP |
9f17e2a6 | 7003 | #undef IS_SECTION_IN_INPUT_SEGMENT |
bc67d8a6 NC |
7004 | #undef INCLUDE_SECTION_IN_SEGMENT |
7005 | #undef SEGMENT_AFTER_SEGMENT | |
7006 | #undef SEGMENT_OVERLAPS | |
b34976b6 | 7007 | return TRUE; |
252b5132 RH |
7008 | } |
7009 | ||
84d1d650 L |
7010 | /* Copy ELF program header information. */ |
7011 | ||
7012 | static bfd_boolean | |
7013 | copy_elf_program_header (bfd *ibfd, bfd *obfd) | |
7014 | { | |
7015 | Elf_Internal_Ehdr *iehdr; | |
7016 | struct elf_segment_map *map; | |
7017 | struct elf_segment_map *map_first; | |
7018 | struct elf_segment_map **pointer_to_map; | |
7019 | Elf_Internal_Phdr *segment; | |
7020 | unsigned int i; | |
7021 | unsigned int num_segments; | |
7022 | bfd_boolean phdr_included = FALSE; | |
88967714 | 7023 | bfd_boolean p_paddr_valid; |
84d1d650 L |
7024 | |
7025 | iehdr = elf_elfheader (ibfd); | |
7026 | ||
7027 | map_first = NULL; | |
7028 | pointer_to_map = &map_first; | |
7029 | ||
88967714 AM |
7030 | /* If all the segment p_paddr fields are zero, don't set |
7031 | map->p_paddr_valid. */ | |
7032 | p_paddr_valid = FALSE; | |
84d1d650 | 7033 | num_segments = elf_elfheader (ibfd)->e_phnum; |
88967714 AM |
7034 | for (i = 0, segment = elf_tdata (ibfd)->phdr; |
7035 | i < num_segments; | |
7036 | i++, segment++) | |
7037 | if (segment->p_paddr != 0) | |
7038 | { | |
7039 | p_paddr_valid = TRUE; | |
7040 | break; | |
7041 | } | |
7042 | ||
84d1d650 L |
7043 | for (i = 0, segment = elf_tdata (ibfd)->phdr; |
7044 | i < num_segments; | |
7045 | i++, segment++) | |
7046 | { | |
7047 | asection *section; | |
7048 | unsigned int section_count; | |
7049 | bfd_size_type amt; | |
7050 | Elf_Internal_Shdr *this_hdr; | |
53020534 | 7051 | asection *first_section = NULL; |
a76e6f2f | 7052 | asection *lowest_section; |
84d1d650 | 7053 | |
84d1d650 L |
7054 | /* Compute how many sections are in this segment. */ |
7055 | for (section = ibfd->sections, section_count = 0; | |
7056 | section != NULL; | |
7057 | section = section->next) | |
7058 | { | |
7059 | this_hdr = &(elf_section_data(section)->this_hdr); | |
f4638467 | 7060 | if (ELF_SECTION_IN_SEGMENT (this_hdr, segment)) |
3271a814 | 7061 | { |
a76e6f2f AM |
7062 | if (first_section == NULL) |
7063 | first_section = section; | |
3271a814 NS |
7064 | section_count++; |
7065 | } | |
84d1d650 L |
7066 | } |
7067 | ||
7068 | /* Allocate a segment map big enough to contain | |
7069 | all of the sections we have selected. */ | |
7070 | amt = sizeof (struct elf_segment_map); | |
7071 | if (section_count != 0) | |
7072 | amt += ((bfd_size_type) section_count - 1) * sizeof (asection *); | |
a50b1753 | 7073 | map = (struct elf_segment_map *) bfd_zalloc (obfd, amt); |
84d1d650 L |
7074 | if (map == NULL) |
7075 | return FALSE; | |
7076 | ||
7077 | /* Initialize the fields of the output segment map with the | |
7078 | input segment. */ | |
7079 | map->next = NULL; | |
7080 | map->p_type = segment->p_type; | |
7081 | map->p_flags = segment->p_flags; | |
7082 | map->p_flags_valid = 1; | |
7083 | map->p_paddr = segment->p_paddr; | |
88967714 | 7084 | map->p_paddr_valid = p_paddr_valid; |
3f570048 AM |
7085 | map->p_align = segment->p_align; |
7086 | map->p_align_valid = 1; | |
3271a814 | 7087 | map->p_vaddr_offset = 0; |
84d1d650 | 7088 | |
04c3a755 NS |
7089 | if (map->p_type == PT_GNU_RELRO |
7090 | || map->p_type == PT_GNU_STACK) | |
b10a8ae0 L |
7091 | { |
7092 | /* The PT_GNU_RELRO segment may contain the first a few | |
7093 | bytes in the .got.plt section even if the whole .got.plt | |
7094 | section isn't in the PT_GNU_RELRO segment. We won't | |
04c3a755 NS |
7095 | change the size of the PT_GNU_RELRO segment. |
7096 | Similarly, PT_GNU_STACK size is significant on uclinux | |
7097 | systems. */ | |
9433b9b1 | 7098 | map->p_size = segment->p_memsz; |
b10a8ae0 L |
7099 | map->p_size_valid = 1; |
7100 | } | |
7101 | ||
84d1d650 L |
7102 | /* Determine if this segment contains the ELF file header |
7103 | and if it contains the program headers themselves. */ | |
7104 | map->includes_filehdr = (segment->p_offset == 0 | |
7105 | && segment->p_filesz >= iehdr->e_ehsize); | |
7106 | ||
7107 | map->includes_phdrs = 0; | |
7108 | if (! phdr_included || segment->p_type != PT_LOAD) | |
7109 | { | |
7110 | map->includes_phdrs = | |
7111 | (segment->p_offset <= (bfd_vma) iehdr->e_phoff | |
7112 | && (segment->p_offset + segment->p_filesz | |
7113 | >= ((bfd_vma) iehdr->e_phoff | |
7114 | + iehdr->e_phnum * iehdr->e_phentsize))); | |
7115 | ||
7116 | if (segment->p_type == PT_LOAD && map->includes_phdrs) | |
7117 | phdr_included = TRUE; | |
7118 | } | |
7119 | ||
bbefd0a9 | 7120 | lowest_section = NULL; |
84d1d650 L |
7121 | if (section_count != 0) |
7122 | { | |
7123 | unsigned int isec = 0; | |
7124 | ||
53020534 | 7125 | for (section = first_section; |
84d1d650 L |
7126 | section != NULL; |
7127 | section = section->next) | |
7128 | { | |
7129 | this_hdr = &(elf_section_data(section)->this_hdr); | |
f4638467 | 7130 | if (ELF_SECTION_IN_SEGMENT (this_hdr, segment)) |
53020534 L |
7131 | { |
7132 | map->sections[isec++] = section->output_section; | |
a76e6f2f AM |
7133 | if ((section->flags & SEC_ALLOC) != 0) |
7134 | { | |
7135 | bfd_vma seg_off; | |
7136 | ||
bbefd0a9 AM |
7137 | if (lowest_section == NULL |
7138 | || section->lma < lowest_section->lma) | |
fb8a5684 AM |
7139 | lowest_section = section; |
7140 | ||
a76e6f2f AM |
7141 | /* Section lmas are set up from PT_LOAD header |
7142 | p_paddr in _bfd_elf_make_section_from_shdr. | |
7143 | If this header has a p_paddr that disagrees | |
7144 | with the section lma, flag the p_paddr as | |
7145 | invalid. */ | |
7146 | if ((section->flags & SEC_LOAD) != 0) | |
7147 | seg_off = this_hdr->sh_offset - segment->p_offset; | |
7148 | else | |
7149 | seg_off = this_hdr->sh_addr - segment->p_vaddr; | |
7150 | if (section->lma - segment->p_paddr != seg_off) | |
7151 | map->p_paddr_valid = FALSE; | |
7152 | } | |
53020534 L |
7153 | if (isec == section_count) |
7154 | break; | |
7155 | } | |
84d1d650 L |
7156 | } |
7157 | } | |
7158 | ||
a76e6f2f AM |
7159 | if (map->includes_filehdr && lowest_section != NULL) |
7160 | /* We need to keep the space used by the headers fixed. */ | |
7161 | map->header_size = lowest_section->vma - segment->p_vaddr; | |
d324f6d6 | 7162 | |
a76e6f2f AM |
7163 | if (!map->includes_phdrs |
7164 | && !map->includes_filehdr | |
7165 | && map->p_paddr_valid) | |
7166 | /* There is some other padding before the first section. */ | |
7167 | map->p_vaddr_offset = ((lowest_section ? lowest_section->lma : 0) | |
7168 | - segment->p_paddr); | |
7169 | ||
84d1d650 L |
7170 | map->count = section_count; |
7171 | *pointer_to_map = map; | |
7172 | pointer_to_map = &map->next; | |
7173 | } | |
7174 | ||
12bd6957 | 7175 | elf_seg_map (obfd) = map_first; |
84d1d650 L |
7176 | return TRUE; |
7177 | } | |
7178 | ||
7179 | /* Copy private BFD data. This copies or rewrites ELF program header | |
7180 | information. */ | |
7181 | ||
7182 | static bfd_boolean | |
7183 | copy_private_bfd_data (bfd *ibfd, bfd *obfd) | |
7184 | { | |
84d1d650 L |
7185 | if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour |
7186 | || bfd_get_flavour (obfd) != bfd_target_elf_flavour) | |
7187 | return TRUE; | |
7188 | ||
7189 | if (elf_tdata (ibfd)->phdr == NULL) | |
7190 | return TRUE; | |
7191 | ||
7192 | if (ibfd->xvec == obfd->xvec) | |
7193 | { | |
cb3ff1e5 NC |
7194 | /* Check to see if any sections in the input BFD |
7195 | covered by ELF program header have changed. */ | |
d55ce4e2 | 7196 | Elf_Internal_Phdr *segment; |
84d1d650 L |
7197 | asection *section, *osec; |
7198 | unsigned int i, num_segments; | |
7199 | Elf_Internal_Shdr *this_hdr; | |
147d51c2 L |
7200 | const struct elf_backend_data *bed; |
7201 | ||
7202 | bed = get_elf_backend_data (ibfd); | |
7203 | ||
7204 | /* Regenerate the segment map if p_paddr is set to 0. */ | |
7205 | if (bed->want_p_paddr_set_to_zero) | |
7206 | goto rewrite; | |
84d1d650 L |
7207 | |
7208 | /* Initialize the segment mark field. */ | |
7209 | for (section = obfd->sections; section != NULL; | |
7210 | section = section->next) | |
7211 | section->segment_mark = FALSE; | |
7212 | ||
7213 | num_segments = elf_elfheader (ibfd)->e_phnum; | |
7214 | for (i = 0, segment = elf_tdata (ibfd)->phdr; | |
7215 | i < num_segments; | |
7216 | i++, segment++) | |
7217 | { | |
5f6999aa NC |
7218 | /* PR binutils/3535. The Solaris linker always sets the p_paddr |
7219 | and p_memsz fields of special segments (DYNAMIC, INTERP) to 0 | |
7220 | which severly confuses things, so always regenerate the segment | |
7221 | map in this case. */ | |
7222 | if (segment->p_paddr == 0 | |
7223 | && segment->p_memsz == 0 | |
7224 | && (segment->p_type == PT_INTERP || segment->p_type == PT_DYNAMIC)) | |
cb3ff1e5 | 7225 | goto rewrite; |
5f6999aa | 7226 | |
84d1d650 L |
7227 | for (section = ibfd->sections; |
7228 | section != NULL; section = section->next) | |
7229 | { | |
7230 | /* We mark the output section so that we know it comes | |
7231 | from the input BFD. */ | |
7232 | osec = section->output_section; | |
7233 | if (osec) | |
7234 | osec->segment_mark = TRUE; | |
7235 | ||
7236 | /* Check if this section is covered by the segment. */ | |
7237 | this_hdr = &(elf_section_data(section)->this_hdr); | |
f4638467 | 7238 | if (ELF_SECTION_IN_SEGMENT (this_hdr, segment)) |
84d1d650 L |
7239 | { |
7240 | /* FIXME: Check if its output section is changed or | |
7241 | removed. What else do we need to check? */ | |
7242 | if (osec == NULL | |
7243 | || section->flags != osec->flags | |
7244 | || section->lma != osec->lma | |
7245 | || section->vma != osec->vma | |
7246 | || section->size != osec->size | |
7247 | || section->rawsize != osec->rawsize | |
7248 | || section->alignment_power != osec->alignment_power) | |
7249 | goto rewrite; | |
7250 | } | |
7251 | } | |
7252 | } | |
7253 | ||
cb3ff1e5 | 7254 | /* Check to see if any output section do not come from the |
84d1d650 L |
7255 | input BFD. */ |
7256 | for (section = obfd->sections; section != NULL; | |
7257 | section = section->next) | |
7258 | { | |
7259 | if (section->segment_mark == FALSE) | |
7260 | goto rewrite; | |
7261 | else | |
7262 | section->segment_mark = FALSE; | |
7263 | } | |
7264 | ||
7265 | return copy_elf_program_header (ibfd, obfd); | |
7266 | } | |
7267 | ||
7268 | rewrite: | |
f1d85785 L |
7269 | if (ibfd->xvec == obfd->xvec) |
7270 | { | |
7271 | /* When rewriting program header, set the output maxpagesize to | |
7272 | the maximum alignment of input PT_LOAD segments. */ | |
7273 | Elf_Internal_Phdr *segment; | |
7274 | unsigned int i; | |
7275 | unsigned int num_segments = elf_elfheader (ibfd)->e_phnum; | |
7276 | bfd_vma maxpagesize = 0; | |
7277 | ||
7278 | for (i = 0, segment = elf_tdata (ibfd)->phdr; | |
7279 | i < num_segments; | |
7280 | i++, segment++) | |
7281 | if (segment->p_type == PT_LOAD | |
7282 | && maxpagesize < segment->p_align) | |
c86934ce NC |
7283 | { |
7284 | /* PR 17512: file: f17299af. */ | |
7285 | if (segment->p_align > (bfd_vma) 1 << ((sizeof (bfd_vma) * 8) - 2)) | |
695344c0 | 7286 | /* xgettext:c-format */ |
4eca0228 | 7287 | _bfd_error_handler (_("\ |
c86934ce | 7288 | %B: warning: segment alignment of 0x%llx is too large"), |
4eca0228 | 7289 | ibfd, (long long) segment->p_align); |
c86934ce NC |
7290 | else |
7291 | maxpagesize = segment->p_align; | |
7292 | } | |
f1d85785 L |
7293 | |
7294 | if (maxpagesize != get_elf_backend_data (obfd)->maxpagesize) | |
7295 | bfd_emul_set_maxpagesize (bfd_get_target (obfd), maxpagesize); | |
7296 | } | |
7297 | ||
84d1d650 L |
7298 | return rewrite_elf_program_header (ibfd, obfd); |
7299 | } | |
7300 | ||
ccd2ec6a L |
7301 | /* Initialize private output section information from input section. */ |
7302 | ||
7303 | bfd_boolean | |
7304 | _bfd_elf_init_private_section_data (bfd *ibfd, | |
7305 | asection *isec, | |
7306 | bfd *obfd, | |
7307 | asection *osec, | |
7308 | struct bfd_link_info *link_info) | |
7309 | ||
7310 | { | |
7311 | Elf_Internal_Shdr *ihdr, *ohdr; | |
0e1862bb L |
7312 | bfd_boolean final_link = (link_info != NULL |
7313 | && !bfd_link_relocatable (link_info)); | |
ccd2ec6a L |
7314 | |
7315 | if (ibfd->xvec->flavour != bfd_target_elf_flavour | |
7316 | || obfd->xvec->flavour != bfd_target_elf_flavour) | |
7317 | return TRUE; | |
7318 | ||
ba85c43e NC |
7319 | BFD_ASSERT (elf_section_data (osec) != NULL); |
7320 | ||
dfa7b0b8 AM |
7321 | /* For objcopy and relocatable link, don't copy the output ELF |
7322 | section type from input if the output BFD section flags have been | |
7323 | set to something different. For a final link allow some flags | |
7324 | that the linker clears to differ. */ | |
42bb2e33 | 7325 | if (elf_section_type (osec) == SHT_NULL |
dfa7b0b8 AM |
7326 | && (osec->flags == isec->flags |
7327 | || (final_link | |
7328 | && ((osec->flags ^ isec->flags) | |
0814be7d | 7329 | & ~(SEC_LINK_ONCE | SEC_LINK_DUPLICATES | SEC_RELOC)) == 0))) |
42bb2e33 | 7330 | elf_section_type (osec) = elf_section_type (isec); |
d270463e L |
7331 | |
7332 | /* FIXME: Is this correct for all OS/PROC specific flags? */ | |
7333 | elf_section_flags (osec) |= (elf_section_flags (isec) | |
7334 | & (SHF_MASKOS | SHF_MASKPROC)); | |
ccd2ec6a L |
7335 | |
7336 | /* Set things up for objcopy and relocatable link. The output | |
7337 | SHT_GROUP section will have its elf_next_in_group pointing back | |
7338 | to the input group members. Ignore linker created group section. | |
7339 | See elfNN_ia64_object_p in elfxx-ia64.c. */ | |
dfa7b0b8 | 7340 | if (!final_link) |
ccd2ec6a L |
7341 | { |
7342 | if (elf_sec_group (isec) == NULL | |
7343 | || (elf_sec_group (isec)->flags & SEC_LINKER_CREATED) == 0) | |
7344 | { | |
7345 | if (elf_section_flags (isec) & SHF_GROUP) | |
7346 | elf_section_flags (osec) |= SHF_GROUP; | |
7347 | elf_next_in_group (osec) = elf_next_in_group (isec); | |
9659de1c | 7348 | elf_section_data (osec)->group = elf_section_data (isec)->group; |
ccd2ec6a | 7349 | } |
68f5ff89 L |
7350 | |
7351 | /* If not decompress, preserve SHF_COMPRESSED. */ | |
7352 | if ((ibfd->flags & BFD_DECOMPRESS) == 0) | |
7353 | elf_section_flags (osec) |= (elf_section_flags (isec) | |
7354 | & SHF_COMPRESSED); | |
ccd2ec6a L |
7355 | } |
7356 | ||
7357 | ihdr = &elf_section_data (isec)->this_hdr; | |
7358 | ||
7359 | /* We need to handle elf_linked_to_section for SHF_LINK_ORDER. We | |
7360 | don't use the output section of the linked-to section since it | |
7361 | may be NULL at this point. */ | |
7362 | if ((ihdr->sh_flags & SHF_LINK_ORDER) != 0) | |
7363 | { | |
7364 | ohdr = &elf_section_data (osec)->this_hdr; | |
7365 | ohdr->sh_flags |= SHF_LINK_ORDER; | |
7366 | elf_linked_to_section (osec) = elf_linked_to_section (isec); | |
7367 | } | |
7368 | ||
7369 | osec->use_rela_p = isec->use_rela_p; | |
7370 | ||
7371 | return TRUE; | |
7372 | } | |
7373 | ||
252b5132 RH |
7374 | /* Copy private section information. This copies over the entsize |
7375 | field, and sometimes the info field. */ | |
7376 | ||
b34976b6 | 7377 | bfd_boolean |
217aa764 AM |
7378 | _bfd_elf_copy_private_section_data (bfd *ibfd, |
7379 | asection *isec, | |
7380 | bfd *obfd, | |
7381 | asection *osec) | |
252b5132 RH |
7382 | { |
7383 | Elf_Internal_Shdr *ihdr, *ohdr; | |
7384 | ||
7385 | if (ibfd->xvec->flavour != bfd_target_elf_flavour | |
7386 | || obfd->xvec->flavour != bfd_target_elf_flavour) | |
b34976b6 | 7387 | return TRUE; |
252b5132 | 7388 | |
252b5132 RH |
7389 | ihdr = &elf_section_data (isec)->this_hdr; |
7390 | ohdr = &elf_section_data (osec)->this_hdr; | |
7391 | ||
7392 | ohdr->sh_entsize = ihdr->sh_entsize; | |
7393 | ||
7394 | if (ihdr->sh_type == SHT_SYMTAB | |
7395 | || ihdr->sh_type == SHT_DYNSYM | |
7396 | || ihdr->sh_type == SHT_GNU_verneed | |
7397 | || ihdr->sh_type == SHT_GNU_verdef) | |
7398 | ohdr->sh_info = ihdr->sh_info; | |
7399 | ||
ccd2ec6a L |
7400 | return _bfd_elf_init_private_section_data (ibfd, isec, obfd, osec, |
7401 | NULL); | |
252b5132 RH |
7402 | } |
7403 | ||
d0bf826b AM |
7404 | /* Look at all the SHT_GROUP sections in IBFD, making any adjustments |
7405 | necessary if we are removing either the SHT_GROUP section or any of | |
7406 | the group member sections. DISCARDED is the value that a section's | |
7407 | output_section has if the section will be discarded, NULL when this | |
7408 | function is called from objcopy, bfd_abs_section_ptr when called | |
7409 | from the linker. */ | |
80fccad2 BW |
7410 | |
7411 | bfd_boolean | |
d0bf826b | 7412 | _bfd_elf_fixup_group_sections (bfd *ibfd, asection *discarded) |
80fccad2 | 7413 | { |
30288845 AM |
7414 | asection *isec; |
7415 | ||
30288845 | 7416 | for (isec = ibfd->sections; isec != NULL; isec = isec->next) |
415f38a6 | 7417 | if (elf_section_type (isec) == SHT_GROUP) |
30288845 AM |
7418 | { |
7419 | asection *first = elf_next_in_group (isec); | |
7420 | asection *s = first; | |
d0bf826b AM |
7421 | bfd_size_type removed = 0; |
7422 | ||
30288845 AM |
7423 | while (s != NULL) |
7424 | { | |
415f38a6 AM |
7425 | /* If this member section is being output but the |
7426 | SHT_GROUP section is not, then clear the group info | |
7427 | set up by _bfd_elf_copy_private_section_data. */ | |
d0bf826b AM |
7428 | if (s->output_section != discarded |
7429 | && isec->output_section == discarded) | |
30288845 AM |
7430 | { |
7431 | elf_section_flags (s->output_section) &= ~SHF_GROUP; | |
7432 | elf_group_name (s->output_section) = NULL; | |
7433 | } | |
415f38a6 AM |
7434 | /* Conversely, if the member section is not being output |
7435 | but the SHT_GROUP section is, then adjust its size. */ | |
d0bf826b AM |
7436 | else if (s->output_section == discarded |
7437 | && isec->output_section != discarded) | |
7438 | removed += 4; | |
30288845 AM |
7439 | s = elf_next_in_group (s); |
7440 | if (s == first) | |
7441 | break; | |
7442 | } | |
d0bf826b AM |
7443 | if (removed != 0) |
7444 | { | |
7445 | if (discarded != NULL) | |
7446 | { | |
7447 | /* If we've been called for ld -r, then we need to | |
7448 | adjust the input section size. This function may | |
7449 | be called multiple times, so save the original | |
7450 | size. */ | |
7451 | if (isec->rawsize == 0) | |
7452 | isec->rawsize = isec->size; | |
7453 | isec->size = isec->rawsize - removed; | |
7454 | } | |
7455 | else | |
7456 | { | |
7457 | /* Adjust the output section size when called from | |
7458 | objcopy. */ | |
7459 | isec->output_section->size -= removed; | |
7460 | } | |
7461 | } | |
30288845 AM |
7462 | } |
7463 | ||
80fccad2 BW |
7464 | return TRUE; |
7465 | } | |
7466 | ||
d0bf826b AM |
7467 | /* Copy private header information. */ |
7468 | ||
7469 | bfd_boolean | |
7470 | _bfd_elf_copy_private_header_data (bfd *ibfd, bfd *obfd) | |
7471 | { | |
7472 | if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour | |
7473 | || bfd_get_flavour (obfd) != bfd_target_elf_flavour) | |
7474 | return TRUE; | |
7475 | ||
7476 | /* Copy over private BFD data if it has not already been copied. | |
7477 | This must be done here, rather than in the copy_private_bfd_data | |
7478 | entry point, because the latter is called after the section | |
7479 | contents have been set, which means that the program headers have | |
7480 | already been worked out. */ | |
12bd6957 | 7481 | if (elf_seg_map (obfd) == NULL && elf_tdata (ibfd)->phdr != NULL) |
d0bf826b AM |
7482 | { |
7483 | if (! copy_private_bfd_data (ibfd, obfd)) | |
7484 | return FALSE; | |
7485 | } | |
7486 | ||
7487 | return _bfd_elf_fixup_group_sections (ibfd, NULL); | |
7488 | } | |
7489 | ||
252b5132 RH |
7490 | /* Copy private symbol information. If this symbol is in a section |
7491 | which we did not map into a BFD section, try to map the section | |
7492 | index correctly. We use special macro definitions for the mapped | |
7493 | section indices; these definitions are interpreted by the | |
7494 | swap_out_syms function. */ | |
7495 | ||
9ad5cbcf AM |
7496 | #define MAP_ONESYMTAB (SHN_HIOS + 1) |
7497 | #define MAP_DYNSYMTAB (SHN_HIOS + 2) | |
7498 | #define MAP_STRTAB (SHN_HIOS + 3) | |
7499 | #define MAP_SHSTRTAB (SHN_HIOS + 4) | |
7500 | #define MAP_SYM_SHNDX (SHN_HIOS + 5) | |
252b5132 | 7501 | |
b34976b6 | 7502 | bfd_boolean |
217aa764 AM |
7503 | _bfd_elf_copy_private_symbol_data (bfd *ibfd, |
7504 | asymbol *isymarg, | |
7505 | bfd *obfd, | |
7506 | asymbol *osymarg) | |
252b5132 RH |
7507 | { |
7508 | elf_symbol_type *isym, *osym; | |
7509 | ||
7510 | if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour | |
7511 | || bfd_get_flavour (obfd) != bfd_target_elf_flavour) | |
b34976b6 | 7512 | return TRUE; |
252b5132 RH |
7513 | |
7514 | isym = elf_symbol_from (ibfd, isymarg); | |
7515 | osym = elf_symbol_from (obfd, osymarg); | |
7516 | ||
7517 | if (isym != NULL | |
8424d8f5 | 7518 | && isym->internal_elf_sym.st_shndx != 0 |
252b5132 RH |
7519 | && osym != NULL |
7520 | && bfd_is_abs_section (isym->symbol.section)) | |
7521 | { | |
7522 | unsigned int shndx; | |
7523 | ||
7524 | shndx = isym->internal_elf_sym.st_shndx; | |
7525 | if (shndx == elf_onesymtab (ibfd)) | |
7526 | shndx = MAP_ONESYMTAB; | |
7527 | else if (shndx == elf_dynsymtab (ibfd)) | |
7528 | shndx = MAP_DYNSYMTAB; | |
12bd6957 | 7529 | else if (shndx == elf_strtab_sec (ibfd)) |
252b5132 | 7530 | shndx = MAP_STRTAB; |
12bd6957 | 7531 | else if (shndx == elf_shstrtab_sec (ibfd)) |
252b5132 | 7532 | shndx = MAP_SHSTRTAB; |
6a40cf0c | 7533 | else if (find_section_in_list (shndx, elf_symtab_shndx_list (ibfd))) |
9ad5cbcf | 7534 | shndx = MAP_SYM_SHNDX; |
252b5132 RH |
7535 | osym->internal_elf_sym.st_shndx = shndx; |
7536 | } | |
7537 | ||
b34976b6 | 7538 | return TRUE; |
252b5132 RH |
7539 | } |
7540 | ||
7541 | /* Swap out the symbols. */ | |
7542 | ||
b34976b6 | 7543 | static bfd_boolean |
217aa764 | 7544 | swap_out_syms (bfd *abfd, |
ef10c3ac | 7545 | struct elf_strtab_hash **sttp, |
217aa764 | 7546 | int relocatable_p) |
252b5132 | 7547 | { |
9c5bfbb7 | 7548 | const struct elf_backend_data *bed; |
079e9a2f AM |
7549 | int symcount; |
7550 | asymbol **syms; | |
ef10c3ac | 7551 | struct elf_strtab_hash *stt; |
079e9a2f | 7552 | Elf_Internal_Shdr *symtab_hdr; |
9ad5cbcf | 7553 | Elf_Internal_Shdr *symtab_shndx_hdr; |
079e9a2f | 7554 | Elf_Internal_Shdr *symstrtab_hdr; |
ef10c3ac | 7555 | struct elf_sym_strtab *symstrtab; |
f075ee0c AM |
7556 | bfd_byte *outbound_syms; |
7557 | bfd_byte *outbound_shndx; | |
ef10c3ac L |
7558 | unsigned long outbound_syms_index; |
7559 | unsigned long outbound_shndx_index; | |
079e9a2f | 7560 | int idx; |
12bd6957 | 7561 | unsigned int num_locals; |
079e9a2f | 7562 | bfd_size_type amt; |
174fd7f9 | 7563 | bfd_boolean name_local_sections; |
252b5132 | 7564 | |
12bd6957 | 7565 | if (!elf_map_symbols (abfd, &num_locals)) |
b34976b6 | 7566 | return FALSE; |
252b5132 | 7567 | |
c044fabd | 7568 | /* Dump out the symtabs. */ |
ef10c3ac | 7569 | stt = _bfd_elf_strtab_init (); |
079e9a2f | 7570 | if (stt == NULL) |
b34976b6 | 7571 | return FALSE; |
252b5132 | 7572 | |
079e9a2f AM |
7573 | bed = get_elf_backend_data (abfd); |
7574 | symcount = bfd_get_symcount (abfd); | |
7575 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; | |
7576 | symtab_hdr->sh_type = SHT_SYMTAB; | |
7577 | symtab_hdr->sh_entsize = bed->s->sizeof_sym; | |
7578 | symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1); | |
12bd6957 | 7579 | symtab_hdr->sh_info = num_locals + 1; |
72de5009 | 7580 | symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align; |
079e9a2f AM |
7581 | |
7582 | symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr; | |
7583 | symstrtab_hdr->sh_type = SHT_STRTAB; | |
7584 | ||
ef10c3ac L |
7585 | /* Allocate buffer to swap out the .strtab section. */ |
7586 | symstrtab = (struct elf_sym_strtab *) bfd_malloc ((symcount + 1) | |
7587 | * sizeof (*symstrtab)); | |
7588 | if (symstrtab == NULL) | |
7589 | { | |
7590 | _bfd_elf_strtab_free (stt); | |
7591 | return FALSE; | |
7592 | } | |
7593 | ||
a50b1753 NC |
7594 | outbound_syms = (bfd_byte *) bfd_alloc2 (abfd, 1 + symcount, |
7595 | bed->s->sizeof_sym); | |
079e9a2f | 7596 | if (outbound_syms == NULL) |
5ed6aba4 | 7597 | { |
ef10c3ac L |
7598 | error_return: |
7599 | _bfd_elf_strtab_free (stt); | |
7600 | free (symstrtab); | |
5ed6aba4 NC |
7601 | return FALSE; |
7602 | } | |
217aa764 | 7603 | symtab_hdr->contents = outbound_syms; |
ef10c3ac | 7604 | outbound_syms_index = 0; |
252b5132 | 7605 | |
9ad5cbcf | 7606 | outbound_shndx = NULL; |
ef10c3ac | 7607 | outbound_shndx_index = 0; |
6a40cf0c NC |
7608 | |
7609 | if (elf_symtab_shndx_list (abfd)) | |
9ad5cbcf | 7610 | { |
6a40cf0c NC |
7611 | symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr; |
7612 | if (symtab_shndx_hdr->sh_name != 0) | |
7613 | { | |
7614 | amt = (bfd_size_type) (1 + symcount) * sizeof (Elf_External_Sym_Shndx); | |
7615 | outbound_shndx = (bfd_byte *) | |
7616 | bfd_zalloc2 (abfd, 1 + symcount, sizeof (Elf_External_Sym_Shndx)); | |
7617 | if (outbound_shndx == NULL) | |
7618 | goto error_return; | |
5ed6aba4 | 7619 | |
6a40cf0c NC |
7620 | symtab_shndx_hdr->contents = outbound_shndx; |
7621 | symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX; | |
7622 | symtab_shndx_hdr->sh_size = amt; | |
7623 | symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx); | |
7624 | symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx); | |
7625 | } | |
7626 | /* FIXME: What about any other headers in the list ? */ | |
9ad5cbcf AM |
7627 | } |
7628 | ||
589e6347 | 7629 | /* Now generate the data (for "contents"). */ |
079e9a2f AM |
7630 | { |
7631 | /* Fill in zeroth symbol and swap it out. */ | |
7632 | Elf_Internal_Sym sym; | |
7633 | sym.st_name = 0; | |
7634 | sym.st_value = 0; | |
7635 | sym.st_size = 0; | |
7636 | sym.st_info = 0; | |
7637 | sym.st_other = 0; | |
7638 | sym.st_shndx = SHN_UNDEF; | |
35fc36a8 | 7639 | sym.st_target_internal = 0; |
ef10c3ac L |
7640 | symstrtab[0].sym = sym; |
7641 | symstrtab[0].dest_index = outbound_syms_index; | |
7642 | symstrtab[0].destshndx_index = outbound_shndx_index; | |
7643 | outbound_syms_index++; | |
9ad5cbcf | 7644 | if (outbound_shndx != NULL) |
ef10c3ac | 7645 | outbound_shndx_index++; |
079e9a2f | 7646 | } |
252b5132 | 7647 | |
174fd7f9 RS |
7648 | name_local_sections |
7649 | = (bed->elf_backend_name_local_section_symbols | |
7650 | && bed->elf_backend_name_local_section_symbols (abfd)); | |
7651 | ||
079e9a2f | 7652 | syms = bfd_get_outsymbols (abfd); |
ef10c3ac | 7653 | for (idx = 0; idx < symcount;) |
252b5132 | 7654 | { |
252b5132 | 7655 | Elf_Internal_Sym sym; |
079e9a2f AM |
7656 | bfd_vma value = syms[idx]->value; |
7657 | elf_symbol_type *type_ptr; | |
7658 | flagword flags = syms[idx]->flags; | |
7659 | int type; | |
252b5132 | 7660 | |
174fd7f9 RS |
7661 | if (!name_local_sections |
7662 | && (flags & (BSF_SECTION_SYM | BSF_GLOBAL)) == BSF_SECTION_SYM) | |
079e9a2f AM |
7663 | { |
7664 | /* Local section symbols have no name. */ | |
ef10c3ac | 7665 | sym.st_name = (unsigned long) -1; |
079e9a2f AM |
7666 | } |
7667 | else | |
7668 | { | |
ef10c3ac L |
7669 | /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize |
7670 | to get the final offset for st_name. */ | |
7671 | sym.st_name | |
7672 | = (unsigned long) _bfd_elf_strtab_add (stt, syms[idx]->name, | |
7673 | FALSE); | |
079e9a2f | 7674 | if (sym.st_name == (unsigned long) -1) |
ef10c3ac | 7675 | goto error_return; |
079e9a2f | 7676 | } |
252b5132 | 7677 | |
079e9a2f | 7678 | type_ptr = elf_symbol_from (abfd, syms[idx]); |
252b5132 | 7679 | |
079e9a2f AM |
7680 | if ((flags & BSF_SECTION_SYM) == 0 |
7681 | && bfd_is_com_section (syms[idx]->section)) | |
7682 | { | |
7683 | /* ELF common symbols put the alignment into the `value' field, | |
7684 | and the size into the `size' field. This is backwards from | |
7685 | how BFD handles it, so reverse it here. */ | |
7686 | sym.st_size = value; | |
7687 | if (type_ptr == NULL | |
7688 | || type_ptr->internal_elf_sym.st_value == 0) | |
7689 | sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value)); | |
7690 | else | |
7691 | sym.st_value = type_ptr->internal_elf_sym.st_value; | |
7692 | sym.st_shndx = _bfd_elf_section_from_bfd_section | |
7693 | (abfd, syms[idx]->section); | |
7694 | } | |
7695 | else | |
7696 | { | |
7697 | asection *sec = syms[idx]->section; | |
cb33740c | 7698 | unsigned int shndx; |
252b5132 | 7699 | |
079e9a2f AM |
7700 | if (sec->output_section) |
7701 | { | |
7702 | value += sec->output_offset; | |
7703 | sec = sec->output_section; | |
7704 | } | |
589e6347 | 7705 | |
079e9a2f AM |
7706 | /* Don't add in the section vma for relocatable output. */ |
7707 | if (! relocatable_p) | |
7708 | value += sec->vma; | |
7709 | sym.st_value = value; | |
7710 | sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0; | |
7711 | ||
7712 | if (bfd_is_abs_section (sec) | |
7713 | && type_ptr != NULL | |
7714 | && type_ptr->internal_elf_sym.st_shndx != 0) | |
7715 | { | |
7716 | /* This symbol is in a real ELF section which we did | |
7717 | not create as a BFD section. Undo the mapping done | |
7718 | by copy_private_symbol_data. */ | |
7719 | shndx = type_ptr->internal_elf_sym.st_shndx; | |
7720 | switch (shndx) | |
7721 | { | |
7722 | case MAP_ONESYMTAB: | |
7723 | shndx = elf_onesymtab (abfd); | |
7724 | break; | |
7725 | case MAP_DYNSYMTAB: | |
7726 | shndx = elf_dynsymtab (abfd); | |
7727 | break; | |
7728 | case MAP_STRTAB: | |
12bd6957 | 7729 | shndx = elf_strtab_sec (abfd); |
079e9a2f AM |
7730 | break; |
7731 | case MAP_SHSTRTAB: | |
12bd6957 | 7732 | shndx = elf_shstrtab_sec (abfd); |
079e9a2f | 7733 | break; |
9ad5cbcf | 7734 | case MAP_SYM_SHNDX: |
6a40cf0c NC |
7735 | if (elf_symtab_shndx_list (abfd)) |
7736 | shndx = elf_symtab_shndx_list (abfd)->ndx; | |
9ad5cbcf | 7737 | break; |
079e9a2f | 7738 | default: |
15bc576a | 7739 | shndx = SHN_ABS; |
079e9a2f AM |
7740 | break; |
7741 | } | |
7742 | } | |
7743 | else | |
7744 | { | |
7745 | shndx = _bfd_elf_section_from_bfd_section (abfd, sec); | |
252b5132 | 7746 | |
cb33740c | 7747 | if (shndx == SHN_BAD) |
079e9a2f AM |
7748 | { |
7749 | asection *sec2; | |
7750 | ||
7751 | /* Writing this would be a hell of a lot easier if | |
7752 | we had some decent documentation on bfd, and | |
7753 | knew what to expect of the library, and what to | |
7754 | demand of applications. For example, it | |
7755 | appears that `objcopy' might not set the | |
7756 | section of a symbol to be a section that is | |
7757 | actually in the output file. */ | |
7758 | sec2 = bfd_get_section_by_name (abfd, sec->name); | |
5df1bc57 AM |
7759 | if (sec2 != NULL) |
7760 | shndx = _bfd_elf_section_from_bfd_section (abfd, sec2); | |
7761 | if (shndx == SHN_BAD) | |
589e6347 | 7762 | { |
695344c0 | 7763 | /* xgettext:c-format */ |
589e6347 NC |
7764 | _bfd_error_handler (_("\ |
7765 | Unable to find equivalent output section for symbol '%s' from section '%s'"), | |
7766 | syms[idx]->name ? syms[idx]->name : "<Local sym>", | |
7767 | sec->name); | |
811072d8 | 7768 | bfd_set_error (bfd_error_invalid_operation); |
ef10c3ac | 7769 | goto error_return; |
589e6347 | 7770 | } |
079e9a2f AM |
7771 | } |
7772 | } | |
252b5132 | 7773 | |
079e9a2f AM |
7774 | sym.st_shndx = shndx; |
7775 | } | |
252b5132 | 7776 | |
13ae64f3 JJ |
7777 | if ((flags & BSF_THREAD_LOCAL) != 0) |
7778 | type = STT_TLS; | |
d8045f23 NC |
7779 | else if ((flags & BSF_GNU_INDIRECT_FUNCTION) != 0) |
7780 | type = STT_GNU_IFUNC; | |
13ae64f3 | 7781 | else if ((flags & BSF_FUNCTION) != 0) |
079e9a2f AM |
7782 | type = STT_FUNC; |
7783 | else if ((flags & BSF_OBJECT) != 0) | |
7784 | type = STT_OBJECT; | |
d9352518 DB |
7785 | else if ((flags & BSF_RELC) != 0) |
7786 | type = STT_RELC; | |
7787 | else if ((flags & BSF_SRELC) != 0) | |
7788 | type = STT_SRELC; | |
079e9a2f AM |
7789 | else |
7790 | type = STT_NOTYPE; | |
252b5132 | 7791 | |
13ae64f3 JJ |
7792 | if (syms[idx]->section->flags & SEC_THREAD_LOCAL) |
7793 | type = STT_TLS; | |
7794 | ||
589e6347 | 7795 | /* Processor-specific types. */ |
079e9a2f AM |
7796 | if (type_ptr != NULL |
7797 | && bed->elf_backend_get_symbol_type) | |
7798 | type = ((*bed->elf_backend_get_symbol_type) | |
7799 | (&type_ptr->internal_elf_sym, type)); | |
252b5132 | 7800 | |
079e9a2f AM |
7801 | if (flags & BSF_SECTION_SYM) |
7802 | { | |
7803 | if (flags & BSF_GLOBAL) | |
7804 | sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION); | |
7805 | else | |
7806 | sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION); | |
7807 | } | |
7808 | else if (bfd_is_com_section (syms[idx]->section)) | |
0a40daed | 7809 | { |
b8871f35 L |
7810 | if (type != STT_TLS) |
7811 | { | |
7812 | if ((abfd->flags & BFD_CONVERT_ELF_COMMON)) | |
7813 | type = ((abfd->flags & BFD_USE_ELF_STT_COMMON) | |
7814 | ? STT_COMMON : STT_OBJECT); | |
7815 | else | |
7816 | type = ((flags & BSF_ELF_COMMON) != 0 | |
7817 | ? STT_COMMON : STT_OBJECT); | |
7818 | } | |
7819 | sym.st_info = ELF_ST_INFO (STB_GLOBAL, type); | |
0a40daed | 7820 | } |
079e9a2f AM |
7821 | else if (bfd_is_und_section (syms[idx]->section)) |
7822 | sym.st_info = ELF_ST_INFO (((flags & BSF_WEAK) | |
7823 | ? STB_WEAK | |
7824 | : STB_GLOBAL), | |
7825 | type); | |
7826 | else if (flags & BSF_FILE) | |
7827 | sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE); | |
7828 | else | |
7829 | { | |
7830 | int bind = STB_LOCAL; | |
252b5132 | 7831 | |
079e9a2f AM |
7832 | if (flags & BSF_LOCAL) |
7833 | bind = STB_LOCAL; | |
3e7a7d11 NC |
7834 | else if (flags & BSF_GNU_UNIQUE) |
7835 | bind = STB_GNU_UNIQUE; | |
079e9a2f AM |
7836 | else if (flags & BSF_WEAK) |
7837 | bind = STB_WEAK; | |
7838 | else if (flags & BSF_GLOBAL) | |
7839 | bind = STB_GLOBAL; | |
252b5132 | 7840 | |
079e9a2f AM |
7841 | sym.st_info = ELF_ST_INFO (bind, type); |
7842 | } | |
252b5132 | 7843 | |
079e9a2f | 7844 | if (type_ptr != NULL) |
35fc36a8 RS |
7845 | { |
7846 | sym.st_other = type_ptr->internal_elf_sym.st_other; | |
7847 | sym.st_target_internal | |
7848 | = type_ptr->internal_elf_sym.st_target_internal; | |
7849 | } | |
079e9a2f | 7850 | else |
35fc36a8 RS |
7851 | { |
7852 | sym.st_other = 0; | |
7853 | sym.st_target_internal = 0; | |
7854 | } | |
252b5132 | 7855 | |
ef10c3ac L |
7856 | idx++; |
7857 | symstrtab[idx].sym = sym; | |
7858 | symstrtab[idx].dest_index = outbound_syms_index; | |
7859 | symstrtab[idx].destshndx_index = outbound_shndx_index; | |
7860 | ||
7861 | outbound_syms_index++; | |
9ad5cbcf | 7862 | if (outbound_shndx != NULL) |
ef10c3ac L |
7863 | outbound_shndx_index++; |
7864 | } | |
7865 | ||
7866 | /* Finalize the .strtab section. */ | |
7867 | _bfd_elf_strtab_finalize (stt); | |
7868 | ||
7869 | /* Swap out the .strtab section. */ | |
7870 | for (idx = 0; idx <= symcount; idx++) | |
7871 | { | |
7872 | struct elf_sym_strtab *elfsym = &symstrtab[idx]; | |
7873 | if (elfsym->sym.st_name == (unsigned long) -1) | |
7874 | elfsym->sym.st_name = 0; | |
7875 | else | |
7876 | elfsym->sym.st_name = _bfd_elf_strtab_offset (stt, | |
7877 | elfsym->sym.st_name); | |
7878 | bed->s->swap_symbol_out (abfd, &elfsym->sym, | |
7879 | (outbound_syms | |
7880 | + (elfsym->dest_index | |
7881 | * bed->s->sizeof_sym)), | |
7882 | (outbound_shndx | |
7883 | + (elfsym->destshndx_index | |
7884 | * sizeof (Elf_External_Sym_Shndx)))); | |
079e9a2f | 7885 | } |
ef10c3ac | 7886 | free (symstrtab); |
252b5132 | 7887 | |
079e9a2f | 7888 | *sttp = stt; |
ef10c3ac | 7889 | symstrtab_hdr->sh_size = _bfd_elf_strtab_size (stt); |
079e9a2f | 7890 | symstrtab_hdr->sh_type = SHT_STRTAB; |
84865015 | 7891 | symstrtab_hdr->sh_flags = bed->elf_strtab_flags; |
079e9a2f AM |
7892 | symstrtab_hdr->sh_addr = 0; |
7893 | symstrtab_hdr->sh_entsize = 0; | |
7894 | symstrtab_hdr->sh_link = 0; | |
7895 | symstrtab_hdr->sh_info = 0; | |
7896 | symstrtab_hdr->sh_addralign = 1; | |
252b5132 | 7897 | |
b34976b6 | 7898 | return TRUE; |
252b5132 RH |
7899 | } |
7900 | ||
7901 | /* Return the number of bytes required to hold the symtab vector. | |
7902 | ||
7903 | Note that we base it on the count plus 1, since we will null terminate | |
7904 | the vector allocated based on this size. However, the ELF symbol table | |
7905 | always has a dummy entry as symbol #0, so it ends up even. */ | |
7906 | ||
7907 | long | |
217aa764 | 7908 | _bfd_elf_get_symtab_upper_bound (bfd *abfd) |
252b5132 RH |
7909 | { |
7910 | long symcount; | |
7911 | long symtab_size; | |
7912 | Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr; | |
7913 | ||
7914 | symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym; | |
b99d1833 AM |
7915 | symtab_size = (symcount + 1) * (sizeof (asymbol *)); |
7916 | if (symcount > 0) | |
7917 | symtab_size -= sizeof (asymbol *); | |
252b5132 RH |
7918 | |
7919 | return symtab_size; | |
7920 | } | |
7921 | ||
7922 | long | |
217aa764 | 7923 | _bfd_elf_get_dynamic_symtab_upper_bound (bfd *abfd) |
252b5132 RH |
7924 | { |
7925 | long symcount; | |
7926 | long symtab_size; | |
7927 | Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr; | |
7928 | ||
7929 | if (elf_dynsymtab (abfd) == 0) | |
7930 | { | |
7931 | bfd_set_error (bfd_error_invalid_operation); | |
7932 | return -1; | |
7933 | } | |
7934 | ||
7935 | symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym; | |
b99d1833 AM |
7936 | symtab_size = (symcount + 1) * (sizeof (asymbol *)); |
7937 | if (symcount > 0) | |
7938 | symtab_size -= sizeof (asymbol *); | |
252b5132 RH |
7939 | |
7940 | return symtab_size; | |
7941 | } | |
7942 | ||
7943 | long | |
217aa764 AM |
7944 | _bfd_elf_get_reloc_upper_bound (bfd *abfd ATTRIBUTE_UNUSED, |
7945 | sec_ptr asect) | |
252b5132 RH |
7946 | { |
7947 | return (asect->reloc_count + 1) * sizeof (arelent *); | |
7948 | } | |
7949 | ||
7950 | /* Canonicalize the relocs. */ | |
7951 | ||
7952 | long | |
217aa764 AM |
7953 | _bfd_elf_canonicalize_reloc (bfd *abfd, |
7954 | sec_ptr section, | |
7955 | arelent **relptr, | |
7956 | asymbol **symbols) | |
252b5132 RH |
7957 | { |
7958 | arelent *tblptr; | |
7959 | unsigned int i; | |
9c5bfbb7 | 7960 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
252b5132 | 7961 | |
b34976b6 | 7962 | if (! bed->s->slurp_reloc_table (abfd, section, symbols, FALSE)) |
252b5132 RH |
7963 | return -1; |
7964 | ||
7965 | tblptr = section->relocation; | |
7966 | for (i = 0; i < section->reloc_count; i++) | |
7967 | *relptr++ = tblptr++; | |
7968 | ||
7969 | *relptr = NULL; | |
7970 | ||
7971 | return section->reloc_count; | |
7972 | } | |
7973 | ||
7974 | long | |
6cee3f79 | 7975 | _bfd_elf_canonicalize_symtab (bfd *abfd, asymbol **allocation) |
252b5132 | 7976 | { |
9c5bfbb7 | 7977 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
217aa764 | 7978 | long symcount = bed->s->slurp_symbol_table (abfd, allocation, FALSE); |
252b5132 RH |
7979 | |
7980 | if (symcount >= 0) | |
7981 | bfd_get_symcount (abfd) = symcount; | |
7982 | return symcount; | |
7983 | } | |
7984 | ||
7985 | long | |
217aa764 AM |
7986 | _bfd_elf_canonicalize_dynamic_symtab (bfd *abfd, |
7987 | asymbol **allocation) | |
252b5132 | 7988 | { |
9c5bfbb7 | 7989 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
217aa764 | 7990 | long symcount = bed->s->slurp_symbol_table (abfd, allocation, TRUE); |
1f70368c DJ |
7991 | |
7992 | if (symcount >= 0) | |
7993 | bfd_get_dynamic_symcount (abfd) = symcount; | |
7994 | return symcount; | |
252b5132 RH |
7995 | } |
7996 | ||
8615f3f2 AM |
7997 | /* Return the size required for the dynamic reloc entries. Any loadable |
7998 | section that was actually installed in the BFD, and has type SHT_REL | |
7999 | or SHT_RELA, and uses the dynamic symbol table, is considered to be a | |
8000 | dynamic reloc section. */ | |
252b5132 RH |
8001 | |
8002 | long | |
217aa764 | 8003 | _bfd_elf_get_dynamic_reloc_upper_bound (bfd *abfd) |
252b5132 RH |
8004 | { |
8005 | long ret; | |
8006 | asection *s; | |
8007 | ||
8008 | if (elf_dynsymtab (abfd) == 0) | |
8009 | { | |
8010 | bfd_set_error (bfd_error_invalid_operation); | |
8011 | return -1; | |
8012 | } | |
8013 | ||
8014 | ret = sizeof (arelent *); | |
8015 | for (s = abfd->sections; s != NULL; s = s->next) | |
266b05cf | 8016 | if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd) |
252b5132 RH |
8017 | && (elf_section_data (s)->this_hdr.sh_type == SHT_REL |
8018 | || elf_section_data (s)->this_hdr.sh_type == SHT_RELA)) | |
eea6121a | 8019 | ret += ((s->size / elf_section_data (s)->this_hdr.sh_entsize) |
252b5132 RH |
8020 | * sizeof (arelent *)); |
8021 | ||
8022 | return ret; | |
8023 | } | |
8024 | ||
8615f3f2 AM |
8025 | /* Canonicalize the dynamic relocation entries. Note that we return the |
8026 | dynamic relocations as a single block, although they are actually | |
8027 | associated with particular sections; the interface, which was | |
8028 | designed for SunOS style shared libraries, expects that there is only | |
8029 | one set of dynamic relocs. Any loadable section that was actually | |
8030 | installed in the BFD, and has type SHT_REL or SHT_RELA, and uses the | |
8031 | dynamic symbol table, is considered to be a dynamic reloc section. */ | |
252b5132 RH |
8032 | |
8033 | long | |
217aa764 AM |
8034 | _bfd_elf_canonicalize_dynamic_reloc (bfd *abfd, |
8035 | arelent **storage, | |
8036 | asymbol **syms) | |
252b5132 | 8037 | { |
217aa764 | 8038 | bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean); |
252b5132 RH |
8039 | asection *s; |
8040 | long ret; | |
8041 | ||
8042 | if (elf_dynsymtab (abfd) == 0) | |
8043 | { | |
8044 | bfd_set_error (bfd_error_invalid_operation); | |
8045 | return -1; | |
8046 | } | |
8047 | ||
8048 | slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table; | |
8049 | ret = 0; | |
8050 | for (s = abfd->sections; s != NULL; s = s->next) | |
8051 | { | |
266b05cf | 8052 | if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd) |
252b5132 RH |
8053 | && (elf_section_data (s)->this_hdr.sh_type == SHT_REL |
8054 | || elf_section_data (s)->this_hdr.sh_type == SHT_RELA)) | |
8055 | { | |
8056 | arelent *p; | |
8057 | long count, i; | |
8058 | ||
b34976b6 | 8059 | if (! (*slurp_relocs) (abfd, s, syms, TRUE)) |
252b5132 | 8060 | return -1; |
eea6121a | 8061 | count = s->size / elf_section_data (s)->this_hdr.sh_entsize; |
252b5132 RH |
8062 | p = s->relocation; |
8063 | for (i = 0; i < count; i++) | |
8064 | *storage++ = p++; | |
8065 | ret += count; | |
8066 | } | |
8067 | } | |
8068 | ||
8069 | *storage = NULL; | |
8070 | ||
8071 | return ret; | |
8072 | } | |
8073 | \f | |
8074 | /* Read in the version information. */ | |
8075 | ||
b34976b6 | 8076 | bfd_boolean |
fc0e6df6 | 8077 | _bfd_elf_slurp_version_tables (bfd *abfd, bfd_boolean default_imported_symver) |
252b5132 RH |
8078 | { |
8079 | bfd_byte *contents = NULL; | |
fc0e6df6 PB |
8080 | unsigned int freeidx = 0; |
8081 | ||
8082 | if (elf_dynverref (abfd) != 0) | |
8083 | { | |
8084 | Elf_Internal_Shdr *hdr; | |
8085 | Elf_External_Verneed *everneed; | |
8086 | Elf_Internal_Verneed *iverneed; | |
8087 | unsigned int i; | |
d0fb9a8d | 8088 | bfd_byte *contents_end; |
fc0e6df6 PB |
8089 | |
8090 | hdr = &elf_tdata (abfd)->dynverref_hdr; | |
8091 | ||
601a03ba | 8092 | if (hdr->sh_info == 0 || hdr->sh_size < sizeof (Elf_External_Verneed)) |
d0fb9a8d | 8093 | { |
601a03ba | 8094 | error_return_bad_verref: |
4eca0228 | 8095 | _bfd_error_handler |
601a03ba AM |
8096 | (_("%B: .gnu.version_r invalid entry"), abfd); |
8097 | bfd_set_error (bfd_error_bad_value); | |
d0fb9a8d JJ |
8098 | error_return_verref: |
8099 | elf_tdata (abfd)->verref = NULL; | |
8100 | elf_tdata (abfd)->cverrefs = 0; | |
8101 | goto error_return; | |
8102 | } | |
601a03ba AM |
8103 | |
8104 | contents = (bfd_byte *) bfd_malloc (hdr->sh_size); | |
8105 | if (contents == NULL) | |
8106 | goto error_return_verref; | |
8107 | ||
fc0e6df6 PB |
8108 | if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0 |
8109 | || bfd_bread (contents, hdr->sh_size, abfd) != hdr->sh_size) | |
d0fb9a8d | 8110 | goto error_return_verref; |
fc0e6df6 | 8111 | |
601a03ba AM |
8112 | elf_tdata (abfd)->verref = (Elf_Internal_Verneed *) |
8113 | bfd_zalloc2 (abfd, hdr->sh_info, sizeof (Elf_Internal_Verneed)); | |
8114 | ||
8115 | if (elf_tdata (abfd)->verref == NULL) | |
d0fb9a8d JJ |
8116 | goto error_return_verref; |
8117 | ||
8118 | BFD_ASSERT (sizeof (Elf_External_Verneed) | |
8119 | == sizeof (Elf_External_Vernaux)); | |
8120 | contents_end = contents + hdr->sh_size - sizeof (Elf_External_Verneed); | |
fc0e6df6 PB |
8121 | everneed = (Elf_External_Verneed *) contents; |
8122 | iverneed = elf_tdata (abfd)->verref; | |
8123 | for (i = 0; i < hdr->sh_info; i++, iverneed++) | |
8124 | { | |
8125 | Elf_External_Vernaux *evernaux; | |
8126 | Elf_Internal_Vernaux *ivernaux; | |
8127 | unsigned int j; | |
8128 | ||
8129 | _bfd_elf_swap_verneed_in (abfd, everneed, iverneed); | |
8130 | ||
8131 | iverneed->vn_bfd = abfd; | |
8132 | ||
8133 | iverneed->vn_filename = | |
8134 | bfd_elf_string_from_elf_section (abfd, hdr->sh_link, | |
8135 | iverneed->vn_file); | |
8136 | if (iverneed->vn_filename == NULL) | |
601a03ba | 8137 | goto error_return_bad_verref; |
fc0e6df6 | 8138 | |
d0fb9a8d JJ |
8139 | if (iverneed->vn_cnt == 0) |
8140 | iverneed->vn_auxptr = NULL; | |
8141 | else | |
8142 | { | |
a50b1753 NC |
8143 | iverneed->vn_auxptr = (struct elf_internal_vernaux *) |
8144 | bfd_alloc2 (abfd, iverneed->vn_cnt, | |
8145 | sizeof (Elf_Internal_Vernaux)); | |
d0fb9a8d JJ |
8146 | if (iverneed->vn_auxptr == NULL) |
8147 | goto error_return_verref; | |
8148 | } | |
8149 | ||
8150 | if (iverneed->vn_aux | |
8151 | > (size_t) (contents_end - (bfd_byte *) everneed)) | |
601a03ba | 8152 | goto error_return_bad_verref; |
fc0e6df6 PB |
8153 | |
8154 | evernaux = ((Elf_External_Vernaux *) | |
8155 | ((bfd_byte *) everneed + iverneed->vn_aux)); | |
8156 | ivernaux = iverneed->vn_auxptr; | |
8157 | for (j = 0; j < iverneed->vn_cnt; j++, ivernaux++) | |
8158 | { | |
8159 | _bfd_elf_swap_vernaux_in (abfd, evernaux, ivernaux); | |
8160 | ||
8161 | ivernaux->vna_nodename = | |
8162 | bfd_elf_string_from_elf_section (abfd, hdr->sh_link, | |
8163 | ivernaux->vna_name); | |
8164 | if (ivernaux->vna_nodename == NULL) | |
601a03ba | 8165 | goto error_return_bad_verref; |
fc0e6df6 | 8166 | |
25ff461f AM |
8167 | if (ivernaux->vna_other > freeidx) |
8168 | freeidx = ivernaux->vna_other; | |
8169 | ||
8170 | ivernaux->vna_nextptr = NULL; | |
8171 | if (ivernaux->vna_next == 0) | |
8172 | { | |
8173 | iverneed->vn_cnt = j + 1; | |
8174 | break; | |
8175 | } | |
fc0e6df6 PB |
8176 | if (j + 1 < iverneed->vn_cnt) |
8177 | ivernaux->vna_nextptr = ivernaux + 1; | |
fc0e6df6 | 8178 | |
d0fb9a8d JJ |
8179 | if (ivernaux->vna_next |
8180 | > (size_t) (contents_end - (bfd_byte *) evernaux)) | |
601a03ba | 8181 | goto error_return_bad_verref; |
d0fb9a8d | 8182 | |
fc0e6df6 PB |
8183 | evernaux = ((Elf_External_Vernaux *) |
8184 | ((bfd_byte *) evernaux + ivernaux->vna_next)); | |
fc0e6df6 PB |
8185 | } |
8186 | ||
25ff461f AM |
8187 | iverneed->vn_nextref = NULL; |
8188 | if (iverneed->vn_next == 0) | |
8189 | break; | |
fc0e6df6 PB |
8190 | if (i + 1 < hdr->sh_info) |
8191 | iverneed->vn_nextref = iverneed + 1; | |
fc0e6df6 | 8192 | |
d0fb9a8d JJ |
8193 | if (iverneed->vn_next |
8194 | > (size_t) (contents_end - (bfd_byte *) everneed)) | |
601a03ba | 8195 | goto error_return_bad_verref; |
d0fb9a8d | 8196 | |
fc0e6df6 PB |
8197 | everneed = ((Elf_External_Verneed *) |
8198 | ((bfd_byte *) everneed + iverneed->vn_next)); | |
8199 | } | |
25ff461f | 8200 | elf_tdata (abfd)->cverrefs = i; |
fc0e6df6 PB |
8201 | |
8202 | free (contents); | |
8203 | contents = NULL; | |
8204 | } | |
252b5132 RH |
8205 | |
8206 | if (elf_dynverdef (abfd) != 0) | |
8207 | { | |
8208 | Elf_Internal_Shdr *hdr; | |
8209 | Elf_External_Verdef *everdef; | |
8210 | Elf_Internal_Verdef *iverdef; | |
f631889e UD |
8211 | Elf_Internal_Verdef *iverdefarr; |
8212 | Elf_Internal_Verdef iverdefmem; | |
252b5132 | 8213 | unsigned int i; |
062e2358 | 8214 | unsigned int maxidx; |
d0fb9a8d | 8215 | bfd_byte *contents_end_def, *contents_end_aux; |
252b5132 RH |
8216 | |
8217 | hdr = &elf_tdata (abfd)->dynverdef_hdr; | |
8218 | ||
601a03ba AM |
8219 | if (hdr->sh_info == 0 || hdr->sh_size < sizeof (Elf_External_Verdef)) |
8220 | { | |
8221 | error_return_bad_verdef: | |
4eca0228 | 8222 | _bfd_error_handler |
601a03ba AM |
8223 | (_("%B: .gnu.version_d invalid entry"), abfd); |
8224 | bfd_set_error (bfd_error_bad_value); | |
8225 | error_return_verdef: | |
8226 | elf_tdata (abfd)->verdef = NULL; | |
8227 | elf_tdata (abfd)->cverdefs = 0; | |
8228 | goto error_return; | |
8229 | } | |
8230 | ||
a50b1753 | 8231 | contents = (bfd_byte *) bfd_malloc (hdr->sh_size); |
252b5132 | 8232 | if (contents == NULL) |
601a03ba | 8233 | goto error_return_verdef; |
252b5132 | 8234 | if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0 |
217aa764 | 8235 | || bfd_bread (contents, hdr->sh_size, abfd) != hdr->sh_size) |
601a03ba | 8236 | goto error_return_verdef; |
d0fb9a8d JJ |
8237 | |
8238 | BFD_ASSERT (sizeof (Elf_External_Verdef) | |
8239 | >= sizeof (Elf_External_Verdaux)); | |
8240 | contents_end_def = contents + hdr->sh_size | |
8241 | - sizeof (Elf_External_Verdef); | |
8242 | contents_end_aux = contents + hdr->sh_size | |
8243 | - sizeof (Elf_External_Verdaux); | |
8244 | ||
f631889e UD |
8245 | /* We know the number of entries in the section but not the maximum |
8246 | index. Therefore we have to run through all entries and find | |
8247 | the maximum. */ | |
252b5132 | 8248 | everdef = (Elf_External_Verdef *) contents; |
f631889e UD |
8249 | maxidx = 0; |
8250 | for (i = 0; i < hdr->sh_info; ++i) | |
8251 | { | |
8252 | _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem); | |
8253 | ||
601a03ba AM |
8254 | if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) == 0) |
8255 | goto error_return_bad_verdef; | |
062e2358 AM |
8256 | if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) > maxidx) |
8257 | maxidx = iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION); | |
f631889e | 8258 | |
25ff461f AM |
8259 | if (iverdefmem.vd_next == 0) |
8260 | break; | |
8261 | ||
d0fb9a8d JJ |
8262 | if (iverdefmem.vd_next |
8263 | > (size_t) (contents_end_def - (bfd_byte *) everdef)) | |
601a03ba | 8264 | goto error_return_bad_verdef; |
d0fb9a8d | 8265 | |
f631889e UD |
8266 | everdef = ((Elf_External_Verdef *) |
8267 | ((bfd_byte *) everdef + iverdefmem.vd_next)); | |
8268 | } | |
8269 | ||
fc0e6df6 PB |
8270 | if (default_imported_symver) |
8271 | { | |
8272 | if (freeidx > maxidx) | |
8273 | maxidx = ++freeidx; | |
8274 | else | |
8275 | freeidx = ++maxidx; | |
8276 | } | |
201159ec | 8277 | |
601a03ba AM |
8278 | elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *) |
8279 | bfd_zalloc2 (abfd, maxidx, sizeof (Elf_Internal_Verdef)); | |
f631889e | 8280 | if (elf_tdata (abfd)->verdef == NULL) |
601a03ba | 8281 | goto error_return_verdef; |
f631889e UD |
8282 | |
8283 | elf_tdata (abfd)->cverdefs = maxidx; | |
8284 | ||
8285 | everdef = (Elf_External_Verdef *) contents; | |
8286 | iverdefarr = elf_tdata (abfd)->verdef; | |
8287 | for (i = 0; i < hdr->sh_info; i++) | |
252b5132 RH |
8288 | { |
8289 | Elf_External_Verdaux *everdaux; | |
8290 | Elf_Internal_Verdaux *iverdaux; | |
8291 | unsigned int j; | |
8292 | ||
f631889e UD |
8293 | _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem); |
8294 | ||
d0fb9a8d | 8295 | if ((iverdefmem.vd_ndx & VERSYM_VERSION) == 0) |
601a03ba | 8296 | goto error_return_bad_verdef; |
d0fb9a8d | 8297 | |
f631889e | 8298 | iverdef = &iverdefarr[(iverdefmem.vd_ndx & VERSYM_VERSION) - 1]; |
595bce75 | 8299 | memcpy (iverdef, &iverdefmem, offsetof (Elf_Internal_Verdef, vd_bfd)); |
252b5132 RH |
8300 | |
8301 | iverdef->vd_bfd = abfd; | |
8302 | ||
d0fb9a8d JJ |
8303 | if (iverdef->vd_cnt == 0) |
8304 | iverdef->vd_auxptr = NULL; | |
8305 | else | |
8306 | { | |
a50b1753 NC |
8307 | iverdef->vd_auxptr = (struct elf_internal_verdaux *) |
8308 | bfd_alloc2 (abfd, iverdef->vd_cnt, | |
8309 | sizeof (Elf_Internal_Verdaux)); | |
d0fb9a8d JJ |
8310 | if (iverdef->vd_auxptr == NULL) |
8311 | goto error_return_verdef; | |
8312 | } | |
8313 | ||
8314 | if (iverdef->vd_aux | |
8315 | > (size_t) (contents_end_aux - (bfd_byte *) everdef)) | |
601a03ba | 8316 | goto error_return_bad_verdef; |
252b5132 RH |
8317 | |
8318 | everdaux = ((Elf_External_Verdaux *) | |
8319 | ((bfd_byte *) everdef + iverdef->vd_aux)); | |
8320 | iverdaux = iverdef->vd_auxptr; | |
8321 | for (j = 0; j < iverdef->vd_cnt; j++, iverdaux++) | |
8322 | { | |
8323 | _bfd_elf_swap_verdaux_in (abfd, everdaux, iverdaux); | |
8324 | ||
8325 | iverdaux->vda_nodename = | |
8326 | bfd_elf_string_from_elf_section (abfd, hdr->sh_link, | |
8327 | iverdaux->vda_name); | |
8328 | if (iverdaux->vda_nodename == NULL) | |
601a03ba | 8329 | goto error_return_bad_verdef; |
252b5132 | 8330 | |
25ff461f AM |
8331 | iverdaux->vda_nextptr = NULL; |
8332 | if (iverdaux->vda_next == 0) | |
8333 | { | |
8334 | iverdef->vd_cnt = j + 1; | |
8335 | break; | |
8336 | } | |
252b5132 RH |
8337 | if (j + 1 < iverdef->vd_cnt) |
8338 | iverdaux->vda_nextptr = iverdaux + 1; | |
252b5132 | 8339 | |
d0fb9a8d JJ |
8340 | if (iverdaux->vda_next |
8341 | > (size_t) (contents_end_aux - (bfd_byte *) everdaux)) | |
601a03ba | 8342 | goto error_return_bad_verdef; |
d0fb9a8d | 8343 | |
252b5132 RH |
8344 | everdaux = ((Elf_External_Verdaux *) |
8345 | ((bfd_byte *) everdaux + iverdaux->vda_next)); | |
8346 | } | |
8347 | ||
595bce75 | 8348 | iverdef->vd_nodename = NULL; |
d0fb9a8d JJ |
8349 | if (iverdef->vd_cnt) |
8350 | iverdef->vd_nodename = iverdef->vd_auxptr->vda_nodename; | |
252b5132 | 8351 | |
25ff461f AM |
8352 | iverdef->vd_nextdef = NULL; |
8353 | if (iverdef->vd_next == 0) | |
8354 | break; | |
d0fb9a8d | 8355 | if ((size_t) (iverdef - iverdefarr) + 1 < maxidx) |
252b5132 | 8356 | iverdef->vd_nextdef = iverdef + 1; |
252b5132 RH |
8357 | |
8358 | everdef = ((Elf_External_Verdef *) | |
8359 | ((bfd_byte *) everdef + iverdef->vd_next)); | |
8360 | } | |
8361 | ||
8362 | free (contents); | |
8363 | contents = NULL; | |
8364 | } | |
fc0e6df6 | 8365 | else if (default_imported_symver) |
252b5132 | 8366 | { |
fc0e6df6 PB |
8367 | if (freeidx < 3) |
8368 | freeidx = 3; | |
8369 | else | |
8370 | freeidx++; | |
252b5132 | 8371 | |
a50b1753 NC |
8372 | elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *) |
8373 | bfd_zalloc2 (abfd, freeidx, sizeof (Elf_Internal_Verdef)); | |
fc0e6df6 | 8374 | if (elf_tdata (abfd)->verdef == NULL) |
252b5132 RH |
8375 | goto error_return; |
8376 | ||
fc0e6df6 PB |
8377 | elf_tdata (abfd)->cverdefs = freeidx; |
8378 | } | |
252b5132 | 8379 | |
fc0e6df6 PB |
8380 | /* Create a default version based on the soname. */ |
8381 | if (default_imported_symver) | |
8382 | { | |
8383 | Elf_Internal_Verdef *iverdef; | |
8384 | Elf_Internal_Verdaux *iverdaux; | |
252b5132 | 8385 | |
5bb3703f | 8386 | iverdef = &elf_tdata (abfd)->verdef[freeidx - 1]; |
252b5132 | 8387 | |
fc0e6df6 PB |
8388 | iverdef->vd_version = VER_DEF_CURRENT; |
8389 | iverdef->vd_flags = 0; | |
8390 | iverdef->vd_ndx = freeidx; | |
8391 | iverdef->vd_cnt = 1; | |
252b5132 | 8392 | |
fc0e6df6 | 8393 | iverdef->vd_bfd = abfd; |
252b5132 | 8394 | |
fc0e6df6 PB |
8395 | iverdef->vd_nodename = bfd_elf_get_dt_soname (abfd); |
8396 | if (iverdef->vd_nodename == NULL) | |
d0fb9a8d | 8397 | goto error_return_verdef; |
fc0e6df6 | 8398 | iverdef->vd_nextdef = NULL; |
601a03ba AM |
8399 | iverdef->vd_auxptr = ((struct elf_internal_verdaux *) |
8400 | bfd_zalloc (abfd, sizeof (Elf_Internal_Verdaux))); | |
d0fb9a8d JJ |
8401 | if (iverdef->vd_auxptr == NULL) |
8402 | goto error_return_verdef; | |
252b5132 | 8403 | |
fc0e6df6 PB |
8404 | iverdaux = iverdef->vd_auxptr; |
8405 | iverdaux->vda_nodename = iverdef->vd_nodename; | |
252b5132 RH |
8406 | } |
8407 | ||
b34976b6 | 8408 | return TRUE; |
252b5132 RH |
8409 | |
8410 | error_return: | |
5ed6aba4 | 8411 | if (contents != NULL) |
252b5132 | 8412 | free (contents); |
b34976b6 | 8413 | return FALSE; |
252b5132 RH |
8414 | } |
8415 | \f | |
8416 | asymbol * | |
217aa764 | 8417 | _bfd_elf_make_empty_symbol (bfd *abfd) |
252b5132 RH |
8418 | { |
8419 | elf_symbol_type *newsym; | |
8420 | ||
201159ec | 8421 | newsym = (elf_symbol_type *) bfd_zalloc (abfd, sizeof * newsym); |
252b5132 RH |
8422 | if (!newsym) |
8423 | return NULL; | |
201159ec NC |
8424 | newsym->symbol.the_bfd = abfd; |
8425 | return &newsym->symbol; | |
252b5132 RH |
8426 | } |
8427 | ||
8428 | void | |
217aa764 AM |
8429 | _bfd_elf_get_symbol_info (bfd *abfd ATTRIBUTE_UNUSED, |
8430 | asymbol *symbol, | |
8431 | symbol_info *ret) | |
252b5132 RH |
8432 | { |
8433 | bfd_symbol_info (symbol, ret); | |
8434 | } | |
8435 | ||
8436 | /* Return whether a symbol name implies a local symbol. Most targets | |
8437 | use this function for the is_local_label_name entry point, but some | |
8438 | override it. */ | |
8439 | ||
b34976b6 | 8440 | bfd_boolean |
217aa764 AM |
8441 | _bfd_elf_is_local_label_name (bfd *abfd ATTRIBUTE_UNUSED, |
8442 | const char *name) | |
252b5132 RH |
8443 | { |
8444 | /* Normal local symbols start with ``.L''. */ | |
8445 | if (name[0] == '.' && name[1] == 'L') | |
b34976b6 | 8446 | return TRUE; |
252b5132 RH |
8447 | |
8448 | /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate | |
8449 | DWARF debugging symbols starting with ``..''. */ | |
8450 | if (name[0] == '.' && name[1] == '.') | |
b34976b6 | 8451 | return TRUE; |
252b5132 RH |
8452 | |
8453 | /* gcc will sometimes generate symbols beginning with ``_.L_'' when | |
8454 | emitting DWARF debugging output. I suspect this is actually a | |
8455 | small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call | |
8456 | ASM_GENERATE_INTERNAL_LABEL, and this causes the leading | |
8457 | underscore to be emitted on some ELF targets). For ease of use, | |
8458 | we treat such symbols as local. */ | |
8459 | if (name[0] == '_' && name[1] == '.' && name[2] == 'L' && name[3] == '_') | |
b34976b6 | 8460 | return TRUE; |
252b5132 | 8461 | |
b1fa9dd6 NC |
8462 | /* Treat assembler generated fake symbols, dollar local labels and |
8463 | forward-backward labels (aka local labels) as locals. | |
8464 | These labels have the form: | |
8465 | ||
8466 | L0^A.* (fake symbols) | |
8467 | ||
8468 | [.]?L[0123456789]+{^A|^B}[0123456789]* (local labels) | |
8469 | ||
8470 | Versions which start with .L will have already been matched above, | |
8471 | so we only need to match the rest. */ | |
8472 | if (name[0] == 'L' && ISDIGIT (name[1])) | |
8473 | { | |
8474 | bfd_boolean ret = FALSE; | |
8475 | const char * p; | |
8476 | char c; | |
8477 | ||
8478 | for (p = name + 2; (c = *p); p++) | |
8479 | { | |
8480 | if (c == 1 || c == 2) | |
8481 | { | |
8482 | if (c == 1 && p == name + 2) | |
8483 | /* A fake symbol. */ | |
8484 | return TRUE; | |
8485 | ||
8486 | /* FIXME: We are being paranoid here and treating symbols like | |
8487 | L0^Bfoo as if there were non-local, on the grounds that the | |
8488 | assembler will never generate them. But can any symbol | |
8489 | containing an ASCII value in the range 1-31 ever be anything | |
8490 | other than some kind of local ? */ | |
8491 | ret = TRUE; | |
8492 | } | |
8493 | ||
8494 | if (! ISDIGIT (c)) | |
8495 | { | |
8496 | ret = FALSE; | |
8497 | break; | |
8498 | } | |
8499 | } | |
8500 | return ret; | |
8501 | } | |
ffa54770 | 8502 | |
b34976b6 | 8503 | return FALSE; |
252b5132 RH |
8504 | } |
8505 | ||
8506 | alent * | |
217aa764 AM |
8507 | _bfd_elf_get_lineno (bfd *abfd ATTRIBUTE_UNUSED, |
8508 | asymbol *symbol ATTRIBUTE_UNUSED) | |
252b5132 RH |
8509 | { |
8510 | abort (); | |
8511 | return NULL; | |
8512 | } | |
8513 | ||
b34976b6 | 8514 | bfd_boolean |
217aa764 AM |
8515 | _bfd_elf_set_arch_mach (bfd *abfd, |
8516 | enum bfd_architecture arch, | |
8517 | unsigned long machine) | |
252b5132 RH |
8518 | { |
8519 | /* If this isn't the right architecture for this backend, and this | |
8520 | isn't the generic backend, fail. */ | |
8521 | if (arch != get_elf_backend_data (abfd)->arch | |
8522 | && arch != bfd_arch_unknown | |
8523 | && get_elf_backend_data (abfd)->arch != bfd_arch_unknown) | |
b34976b6 | 8524 | return FALSE; |
252b5132 RH |
8525 | |
8526 | return bfd_default_set_arch_mach (abfd, arch, machine); | |
8527 | } | |
8528 | ||
d1fad7c6 NC |
8529 | /* Find the nearest line to a particular section and offset, |
8530 | for error reporting. */ | |
8531 | ||
b34976b6 | 8532 | bfd_boolean |
217aa764 | 8533 | _bfd_elf_find_nearest_line (bfd *abfd, |
217aa764 | 8534 | asymbol **symbols, |
fb167eb2 | 8535 | asection *section, |
217aa764 AM |
8536 | bfd_vma offset, |
8537 | const char **filename_ptr, | |
8538 | const char **functionname_ptr, | |
fb167eb2 AM |
8539 | unsigned int *line_ptr, |
8540 | unsigned int *discriminator_ptr) | |
d1fad7c6 | 8541 | { |
b34976b6 | 8542 | bfd_boolean found; |
d1fad7c6 | 8543 | |
fb167eb2 | 8544 | if (_bfd_dwarf2_find_nearest_line (abfd, symbols, NULL, section, offset, |
4e8a9624 | 8545 | filename_ptr, functionname_ptr, |
fb167eb2 AM |
8546 | line_ptr, discriminator_ptr, |
8547 | dwarf_debug_sections, 0, | |
e00e8198 AM |
8548 | &elf_tdata (abfd)->dwarf2_find_line_info) |
8549 | || _bfd_dwarf1_find_nearest_line (abfd, symbols, section, offset, | |
8550 | filename_ptr, functionname_ptr, | |
8551 | line_ptr)) | |
d1fad7c6 NC |
8552 | { |
8553 | if (!*functionname_ptr) | |
e00e8198 AM |
8554 | _bfd_elf_find_function (abfd, symbols, section, offset, |
8555 | *filename_ptr ? NULL : filename_ptr, | |
8556 | functionname_ptr); | |
b34976b6 | 8557 | return TRUE; |
d1fad7c6 NC |
8558 | } |
8559 | ||
8560 | if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset, | |
4e8a9624 AM |
8561 | &found, filename_ptr, |
8562 | functionname_ptr, line_ptr, | |
8563 | &elf_tdata (abfd)->line_info)) | |
b34976b6 | 8564 | return FALSE; |
dc43ada5 | 8565 | if (found && (*functionname_ptr || *line_ptr)) |
b34976b6 | 8566 | return TRUE; |
d1fad7c6 NC |
8567 | |
8568 | if (symbols == NULL) | |
b34976b6 | 8569 | return FALSE; |
d1fad7c6 | 8570 | |
e00e8198 AM |
8571 | if (! _bfd_elf_find_function (abfd, symbols, section, offset, |
8572 | filename_ptr, functionname_ptr)) | |
b34976b6 | 8573 | return FALSE; |
d1fad7c6 | 8574 | |
252b5132 | 8575 | *line_ptr = 0; |
b34976b6 | 8576 | return TRUE; |
252b5132 RH |
8577 | } |
8578 | ||
5420f73d L |
8579 | /* Find the line for a symbol. */ |
8580 | ||
8581 | bfd_boolean | |
8582 | _bfd_elf_find_line (bfd *abfd, asymbol **symbols, asymbol *symbol, | |
8583 | const char **filename_ptr, unsigned int *line_ptr) | |
9b8d1a36 | 8584 | { |
fb167eb2 AM |
8585 | return _bfd_dwarf2_find_nearest_line (abfd, symbols, symbol, NULL, 0, |
8586 | filename_ptr, NULL, line_ptr, NULL, | |
8587 | dwarf_debug_sections, 0, | |
8588 | &elf_tdata (abfd)->dwarf2_find_line_info); | |
5420f73d L |
8589 | } |
8590 | ||
4ab527b0 FF |
8591 | /* After a call to bfd_find_nearest_line, successive calls to |
8592 | bfd_find_inliner_info can be used to get source information about | |
8593 | each level of function inlining that terminated at the address | |
8594 | passed to bfd_find_nearest_line. Currently this is only supported | |
8595 | for DWARF2 with appropriate DWARF3 extensions. */ | |
8596 | ||
8597 | bfd_boolean | |
8598 | _bfd_elf_find_inliner_info (bfd *abfd, | |
8599 | const char **filename_ptr, | |
8600 | const char **functionname_ptr, | |
8601 | unsigned int *line_ptr) | |
8602 | { | |
8603 | bfd_boolean found; | |
8604 | found = _bfd_dwarf2_find_inliner_info (abfd, filename_ptr, | |
8605 | functionname_ptr, line_ptr, | |
8606 | & elf_tdata (abfd)->dwarf2_find_line_info); | |
8607 | return found; | |
8608 | } | |
8609 | ||
252b5132 | 8610 | int |
a6b96beb | 8611 | _bfd_elf_sizeof_headers (bfd *abfd, struct bfd_link_info *info) |
252b5132 | 8612 | { |
8ded5a0f AM |
8613 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
8614 | int ret = bed->s->sizeof_ehdr; | |
252b5132 | 8615 | |
0e1862bb | 8616 | if (!bfd_link_relocatable (info)) |
8ded5a0f | 8617 | { |
12bd6957 | 8618 | bfd_size_type phdr_size = elf_program_header_size (abfd); |
8ded5a0f | 8619 | |
62d7a5f6 AM |
8620 | if (phdr_size == (bfd_size_type) -1) |
8621 | { | |
8622 | struct elf_segment_map *m; | |
8623 | ||
8624 | phdr_size = 0; | |
12bd6957 | 8625 | for (m = elf_seg_map (abfd); m != NULL; m = m->next) |
62d7a5f6 | 8626 | phdr_size += bed->s->sizeof_phdr; |
8ded5a0f | 8627 | |
62d7a5f6 AM |
8628 | if (phdr_size == 0) |
8629 | phdr_size = get_program_header_size (abfd, info); | |
8630 | } | |
8ded5a0f | 8631 | |
12bd6957 | 8632 | elf_program_header_size (abfd) = phdr_size; |
8ded5a0f AM |
8633 | ret += phdr_size; |
8634 | } | |
8635 | ||
252b5132 RH |
8636 | return ret; |
8637 | } | |
8638 | ||
b34976b6 | 8639 | bfd_boolean |
217aa764 AM |
8640 | _bfd_elf_set_section_contents (bfd *abfd, |
8641 | sec_ptr section, | |
0f867abe | 8642 | const void *location, |
217aa764 AM |
8643 | file_ptr offset, |
8644 | bfd_size_type count) | |
252b5132 RH |
8645 | { |
8646 | Elf_Internal_Shdr *hdr; | |
1b6aeedb | 8647 | file_ptr pos; |
252b5132 RH |
8648 | |
8649 | if (! abfd->output_has_begun | |
217aa764 | 8650 | && ! _bfd_elf_compute_section_file_positions (abfd, NULL)) |
b34976b6 | 8651 | return FALSE; |
252b5132 | 8652 | |
0ce398f1 L |
8653 | if (!count) |
8654 | return TRUE; | |
8655 | ||
252b5132 | 8656 | hdr = &elf_section_data (section)->this_hdr; |
0ce398f1 L |
8657 | if (hdr->sh_offset == (file_ptr) -1) |
8658 | { | |
8659 | /* We must compress this section. Write output to the buffer. */ | |
8660 | unsigned char *contents = hdr->contents; | |
8661 | if ((offset + count) > hdr->sh_size | |
8662 | || (section->flags & SEC_ELF_COMPRESS) == 0 | |
8663 | || contents == NULL) | |
8664 | abort (); | |
8665 | memcpy (contents + offset, location, count); | |
8666 | return TRUE; | |
8667 | } | |
dc810e39 AM |
8668 | pos = hdr->sh_offset + offset; |
8669 | if (bfd_seek (abfd, pos, SEEK_SET) != 0 | |
8670 | || bfd_bwrite (location, count, abfd) != count) | |
b34976b6 | 8671 | return FALSE; |
252b5132 | 8672 | |
b34976b6 | 8673 | return TRUE; |
252b5132 RH |
8674 | } |
8675 | ||
8676 | void | |
217aa764 AM |
8677 | _bfd_elf_no_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED, |
8678 | arelent *cache_ptr ATTRIBUTE_UNUSED, | |
8679 | Elf_Internal_Rela *dst ATTRIBUTE_UNUSED) | |
252b5132 RH |
8680 | { |
8681 | abort (); | |
8682 | } | |
8683 | ||
252b5132 RH |
8684 | /* Try to convert a non-ELF reloc into an ELF one. */ |
8685 | ||
b34976b6 | 8686 | bfd_boolean |
217aa764 | 8687 | _bfd_elf_validate_reloc (bfd *abfd, arelent *areloc) |
252b5132 | 8688 | { |
c044fabd | 8689 | /* Check whether we really have an ELF howto. */ |
252b5132 RH |
8690 | |
8691 | if ((*areloc->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec) | |
8692 | { | |
8693 | bfd_reloc_code_real_type code; | |
8694 | reloc_howto_type *howto; | |
8695 | ||
8696 | /* Alien reloc: Try to determine its type to replace it with an | |
c044fabd | 8697 | equivalent ELF reloc. */ |
252b5132 RH |
8698 | |
8699 | if (areloc->howto->pc_relative) | |
8700 | { | |
8701 | switch (areloc->howto->bitsize) | |
8702 | { | |
8703 | case 8: | |
8704 | code = BFD_RELOC_8_PCREL; | |
8705 | break; | |
8706 | case 12: | |
8707 | code = BFD_RELOC_12_PCREL; | |
8708 | break; | |
8709 | case 16: | |
8710 | code = BFD_RELOC_16_PCREL; | |
8711 | break; | |
8712 | case 24: | |
8713 | code = BFD_RELOC_24_PCREL; | |
8714 | break; | |
8715 | case 32: | |
8716 | code = BFD_RELOC_32_PCREL; | |
8717 | break; | |
8718 | case 64: | |
8719 | code = BFD_RELOC_64_PCREL; | |
8720 | break; | |
8721 | default: | |
8722 | goto fail; | |
8723 | } | |
8724 | ||
8725 | howto = bfd_reloc_type_lookup (abfd, code); | |
8726 | ||
8727 | if (areloc->howto->pcrel_offset != howto->pcrel_offset) | |
8728 | { | |
8729 | if (howto->pcrel_offset) | |
8730 | areloc->addend += areloc->address; | |
8731 | else | |
8732 | areloc->addend -= areloc->address; /* addend is unsigned!! */ | |
8733 | } | |
8734 | } | |
8735 | else | |
8736 | { | |
8737 | switch (areloc->howto->bitsize) | |
8738 | { | |
8739 | case 8: | |
8740 | code = BFD_RELOC_8; | |
8741 | break; | |
8742 | case 14: | |
8743 | code = BFD_RELOC_14; | |
8744 | break; | |
8745 | case 16: | |
8746 | code = BFD_RELOC_16; | |
8747 | break; | |
8748 | case 26: | |
8749 | code = BFD_RELOC_26; | |
8750 | break; | |
8751 | case 32: | |
8752 | code = BFD_RELOC_32; | |
8753 | break; | |
8754 | case 64: | |
8755 | code = BFD_RELOC_64; | |
8756 | break; | |
8757 | default: | |
8758 | goto fail; | |
8759 | } | |
8760 | ||
8761 | howto = bfd_reloc_type_lookup (abfd, code); | |
8762 | } | |
8763 | ||
8764 | if (howto) | |
8765 | areloc->howto = howto; | |
8766 | else | |
8767 | goto fail; | |
8768 | } | |
8769 | ||
b34976b6 | 8770 | return TRUE; |
252b5132 RH |
8771 | |
8772 | fail: | |
4eca0228 | 8773 | _bfd_error_handler |
695344c0 | 8774 | /* xgettext:c-format */ |
d003868e AM |
8775 | (_("%B: unsupported relocation type %s"), |
8776 | abfd, areloc->howto->name); | |
252b5132 | 8777 | bfd_set_error (bfd_error_bad_value); |
b34976b6 | 8778 | return FALSE; |
252b5132 RH |
8779 | } |
8780 | ||
b34976b6 | 8781 | bfd_boolean |
217aa764 | 8782 | _bfd_elf_close_and_cleanup (bfd *abfd) |
252b5132 | 8783 | { |
d9071b0c TG |
8784 | struct elf_obj_tdata *tdata = elf_tdata (abfd); |
8785 | if (bfd_get_format (abfd) == bfd_object && tdata != NULL) | |
252b5132 | 8786 | { |
c0355132 | 8787 | if (elf_tdata (abfd)->o != NULL && elf_shstrtab (abfd) != NULL) |
2b0f7ef9 | 8788 | _bfd_elf_strtab_free (elf_shstrtab (abfd)); |
d9071b0c | 8789 | _bfd_dwarf2_cleanup_debug_info (abfd, &tdata->dwarf2_find_line_info); |
252b5132 RH |
8790 | } |
8791 | ||
8792 | return _bfd_generic_close_and_cleanup (abfd); | |
8793 | } | |
8794 | ||
8795 | /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY | |
8796 | in the relocation's offset. Thus we cannot allow any sort of sanity | |
8797 | range-checking to interfere. There is nothing else to do in processing | |
8798 | this reloc. */ | |
8799 | ||
8800 | bfd_reloc_status_type | |
217aa764 AM |
8801 | _bfd_elf_rel_vtable_reloc_fn |
8802 | (bfd *abfd ATTRIBUTE_UNUSED, arelent *re ATTRIBUTE_UNUSED, | |
fc0a2244 | 8803 | struct bfd_symbol *symbol ATTRIBUTE_UNUSED, |
217aa764 AM |
8804 | void *data ATTRIBUTE_UNUSED, asection *is ATTRIBUTE_UNUSED, |
8805 | bfd *obfd ATTRIBUTE_UNUSED, char **errmsg ATTRIBUTE_UNUSED) | |
252b5132 RH |
8806 | { |
8807 | return bfd_reloc_ok; | |
8808 | } | |
252b5132 RH |
8809 | \f |
8810 | /* Elf core file support. Much of this only works on native | |
8811 | toolchains, since we rely on knowing the | |
8812 | machine-dependent procfs structure in order to pick | |
c044fabd | 8813 | out details about the corefile. */ |
252b5132 RH |
8814 | |
8815 | #ifdef HAVE_SYS_PROCFS_H | |
16231b7b DG |
8816 | /* Needed for new procfs interface on sparc-solaris. */ |
8817 | # define _STRUCTURED_PROC 1 | |
252b5132 RH |
8818 | # include <sys/procfs.h> |
8819 | #endif | |
8820 | ||
261b8d08 PA |
8821 | /* Return a PID that identifies a "thread" for threaded cores, or the |
8822 | PID of the main process for non-threaded cores. */ | |
252b5132 RH |
8823 | |
8824 | static int | |
217aa764 | 8825 | elfcore_make_pid (bfd *abfd) |
252b5132 | 8826 | { |
261b8d08 PA |
8827 | int pid; |
8828 | ||
228e534f | 8829 | pid = elf_tdata (abfd)->core->lwpid; |
261b8d08 | 8830 | if (pid == 0) |
228e534f | 8831 | pid = elf_tdata (abfd)->core->pid; |
261b8d08 PA |
8832 | |
8833 | return pid; | |
252b5132 RH |
8834 | } |
8835 | ||
252b5132 RH |
8836 | /* If there isn't a section called NAME, make one, using |
8837 | data from SECT. Note, this function will generate a | |
8838 | reference to NAME, so you shouldn't deallocate or | |
c044fabd | 8839 | overwrite it. */ |
252b5132 | 8840 | |
b34976b6 | 8841 | static bfd_boolean |
217aa764 | 8842 | elfcore_maybe_make_sect (bfd *abfd, char *name, asection *sect) |
252b5132 | 8843 | { |
c044fabd | 8844 | asection *sect2; |
252b5132 RH |
8845 | |
8846 | if (bfd_get_section_by_name (abfd, name) != NULL) | |
b34976b6 | 8847 | return TRUE; |
252b5132 | 8848 | |
117ed4f8 | 8849 | sect2 = bfd_make_section_with_flags (abfd, name, sect->flags); |
252b5132 | 8850 | if (sect2 == NULL) |
b34976b6 | 8851 | return FALSE; |
252b5132 | 8852 | |
eea6121a | 8853 | sect2->size = sect->size; |
252b5132 | 8854 | sect2->filepos = sect->filepos; |
252b5132 | 8855 | sect2->alignment_power = sect->alignment_power; |
b34976b6 | 8856 | return TRUE; |
252b5132 RH |
8857 | } |
8858 | ||
bb0082d6 AM |
8859 | /* Create a pseudosection containing SIZE bytes at FILEPOS. This |
8860 | actually creates up to two pseudosections: | |
8861 | - For the single-threaded case, a section named NAME, unless | |
8862 | such a section already exists. | |
8863 | - For the multi-threaded case, a section named "NAME/PID", where | |
8864 | PID is elfcore_make_pid (abfd). | |
8865 | Both pseudosections have identical contents. */ | |
b34976b6 | 8866 | bfd_boolean |
217aa764 AM |
8867 | _bfd_elfcore_make_pseudosection (bfd *abfd, |
8868 | char *name, | |
8869 | size_t size, | |
8870 | ufile_ptr filepos) | |
bb0082d6 AM |
8871 | { |
8872 | char buf[100]; | |
8873 | char *threaded_name; | |
d4c88bbb | 8874 | size_t len; |
bb0082d6 AM |
8875 | asection *sect; |
8876 | ||
8877 | /* Build the section name. */ | |
8878 | ||
8879 | sprintf (buf, "%s/%d", name, elfcore_make_pid (abfd)); | |
d4c88bbb | 8880 | len = strlen (buf) + 1; |
a50b1753 | 8881 | threaded_name = (char *) bfd_alloc (abfd, len); |
bb0082d6 | 8882 | if (threaded_name == NULL) |
b34976b6 | 8883 | return FALSE; |
d4c88bbb | 8884 | memcpy (threaded_name, buf, len); |
bb0082d6 | 8885 | |
117ed4f8 AM |
8886 | sect = bfd_make_section_anyway_with_flags (abfd, threaded_name, |
8887 | SEC_HAS_CONTENTS); | |
bb0082d6 | 8888 | if (sect == NULL) |
b34976b6 | 8889 | return FALSE; |
eea6121a | 8890 | sect->size = size; |
bb0082d6 | 8891 | sect->filepos = filepos; |
bb0082d6 AM |
8892 | sect->alignment_power = 2; |
8893 | ||
936e320b | 8894 | return elfcore_maybe_make_sect (abfd, name, sect); |
bb0082d6 AM |
8895 | } |
8896 | ||
252b5132 | 8897 | /* prstatus_t exists on: |
4a938328 | 8898 | solaris 2.5+ |
252b5132 RH |
8899 | linux 2.[01] + glibc |
8900 | unixware 4.2 | |
8901 | */ | |
8902 | ||
8903 | #if defined (HAVE_PRSTATUS_T) | |
a7b97311 | 8904 | |
b34976b6 | 8905 | static bfd_boolean |
217aa764 | 8906 | elfcore_grok_prstatus (bfd *abfd, Elf_Internal_Note *note) |
252b5132 | 8907 | { |
eea6121a | 8908 | size_t size; |
7ee38065 | 8909 | int offset; |
252b5132 | 8910 | |
4a938328 MS |
8911 | if (note->descsz == sizeof (prstatus_t)) |
8912 | { | |
8913 | prstatus_t prstat; | |
252b5132 | 8914 | |
eea6121a | 8915 | size = sizeof (prstat.pr_reg); |
7ee38065 | 8916 | offset = offsetof (prstatus_t, pr_reg); |
4a938328 | 8917 | memcpy (&prstat, note->descdata, sizeof (prstat)); |
252b5132 | 8918 | |
fa49d224 NC |
8919 | /* Do not overwrite the core signal if it |
8920 | has already been set by another thread. */ | |
228e534f AM |
8921 | if (elf_tdata (abfd)->core->signal == 0) |
8922 | elf_tdata (abfd)->core->signal = prstat.pr_cursig; | |
8923 | if (elf_tdata (abfd)->core->pid == 0) | |
8924 | elf_tdata (abfd)->core->pid = prstat.pr_pid; | |
252b5132 | 8925 | |
4a938328 MS |
8926 | /* pr_who exists on: |
8927 | solaris 2.5+ | |
8928 | unixware 4.2 | |
8929 | pr_who doesn't exist on: | |
8930 | linux 2.[01] | |
8931 | */ | |
252b5132 | 8932 | #if defined (HAVE_PRSTATUS_T_PR_WHO) |
228e534f | 8933 | elf_tdata (abfd)->core->lwpid = prstat.pr_who; |
261b8d08 | 8934 | #else |
228e534f | 8935 | elf_tdata (abfd)->core->lwpid = prstat.pr_pid; |
252b5132 | 8936 | #endif |
4a938328 | 8937 | } |
7ee38065 | 8938 | #if defined (HAVE_PRSTATUS32_T) |
4a938328 MS |
8939 | else if (note->descsz == sizeof (prstatus32_t)) |
8940 | { | |
8941 | /* 64-bit host, 32-bit corefile */ | |
8942 | prstatus32_t prstat; | |
8943 | ||
eea6121a | 8944 | size = sizeof (prstat.pr_reg); |
7ee38065 | 8945 | offset = offsetof (prstatus32_t, pr_reg); |
4a938328 MS |
8946 | memcpy (&prstat, note->descdata, sizeof (prstat)); |
8947 | ||
fa49d224 NC |
8948 | /* Do not overwrite the core signal if it |
8949 | has already been set by another thread. */ | |
228e534f AM |
8950 | if (elf_tdata (abfd)->core->signal == 0) |
8951 | elf_tdata (abfd)->core->signal = prstat.pr_cursig; | |
8952 | if (elf_tdata (abfd)->core->pid == 0) | |
8953 | elf_tdata (abfd)->core->pid = prstat.pr_pid; | |
4a938328 MS |
8954 | |
8955 | /* pr_who exists on: | |
8956 | solaris 2.5+ | |
8957 | unixware 4.2 | |
8958 | pr_who doesn't exist on: | |
8959 | linux 2.[01] | |
8960 | */ | |
7ee38065 | 8961 | #if defined (HAVE_PRSTATUS32_T_PR_WHO) |
228e534f | 8962 | elf_tdata (abfd)->core->lwpid = prstat.pr_who; |
261b8d08 | 8963 | #else |
228e534f | 8964 | elf_tdata (abfd)->core->lwpid = prstat.pr_pid; |
4a938328 MS |
8965 | #endif |
8966 | } | |
7ee38065 | 8967 | #endif /* HAVE_PRSTATUS32_T */ |
4a938328 MS |
8968 | else |
8969 | { | |
8970 | /* Fail - we don't know how to handle any other | |
8971 | note size (ie. data object type). */ | |
b34976b6 | 8972 | return TRUE; |
4a938328 | 8973 | } |
252b5132 | 8974 | |
bb0082d6 | 8975 | /* Make a ".reg/999" section and a ".reg" section. */ |
936e320b | 8976 | return _bfd_elfcore_make_pseudosection (abfd, ".reg", |
eea6121a | 8977 | size, note->descpos + offset); |
252b5132 RH |
8978 | } |
8979 | #endif /* defined (HAVE_PRSTATUS_T) */ | |
8980 | ||
bb0082d6 | 8981 | /* Create a pseudosection containing the exact contents of NOTE. */ |
b34976b6 | 8982 | static bfd_boolean |
217aa764 AM |
8983 | elfcore_make_note_pseudosection (bfd *abfd, |
8984 | char *name, | |
8985 | Elf_Internal_Note *note) | |
252b5132 | 8986 | { |
936e320b AM |
8987 | return _bfd_elfcore_make_pseudosection (abfd, name, |
8988 | note->descsz, note->descpos); | |
252b5132 RH |
8989 | } |
8990 | ||
ff08c6bb JB |
8991 | /* There isn't a consistent prfpregset_t across platforms, |
8992 | but it doesn't matter, because we don't have to pick this | |
c044fabd KH |
8993 | data structure apart. */ |
8994 | ||
b34976b6 | 8995 | static bfd_boolean |
217aa764 | 8996 | elfcore_grok_prfpreg (bfd *abfd, Elf_Internal_Note *note) |
ff08c6bb JB |
8997 | { |
8998 | return elfcore_make_note_pseudosection (abfd, ".reg2", note); | |
8999 | } | |
9000 | ||
ff08c6bb | 9001 | /* Linux dumps the Intel SSE regs in a note named "LINUX" with a note |
971d4640 | 9002 | type of NT_PRXFPREG. Just include the whole note's contents |
ff08c6bb | 9003 | literally. */ |
c044fabd | 9004 | |
b34976b6 | 9005 | static bfd_boolean |
217aa764 | 9006 | elfcore_grok_prxfpreg (bfd *abfd, Elf_Internal_Note *note) |
ff08c6bb JB |
9007 | { |
9008 | return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note); | |
9009 | } | |
9010 | ||
4339cae0 L |
9011 | /* Linux dumps the Intel XSAVE extended state in a note named "LINUX" |
9012 | with a note type of NT_X86_XSTATE. Just include the whole note's | |
9013 | contents literally. */ | |
9014 | ||
9015 | static bfd_boolean | |
9016 | elfcore_grok_xstatereg (bfd *abfd, Elf_Internal_Note *note) | |
9017 | { | |
9018 | return elfcore_make_note_pseudosection (abfd, ".reg-xstate", note); | |
9019 | } | |
9020 | ||
97753bd5 AM |
9021 | static bfd_boolean |
9022 | elfcore_grok_ppc_vmx (bfd *abfd, Elf_Internal_Note *note) | |
9023 | { | |
9024 | return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vmx", note); | |
9025 | } | |
9026 | ||
89eeb0bc LM |
9027 | static bfd_boolean |
9028 | elfcore_grok_ppc_vsx (bfd *abfd, Elf_Internal_Note *note) | |
9029 | { | |
9030 | return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vsx", note); | |
9031 | } | |
97753bd5 | 9032 | |
0675e188 UW |
9033 | static bfd_boolean |
9034 | elfcore_grok_s390_high_gprs (bfd *abfd, Elf_Internal_Note *note) | |
9035 | { | |
9036 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-high-gprs", note); | |
9037 | } | |
9038 | ||
d7eeb400 MS |
9039 | static bfd_boolean |
9040 | elfcore_grok_s390_timer (bfd *abfd, Elf_Internal_Note *note) | |
9041 | { | |
9042 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-timer", note); | |
9043 | } | |
9044 | ||
9045 | static bfd_boolean | |
9046 | elfcore_grok_s390_todcmp (bfd *abfd, Elf_Internal_Note *note) | |
9047 | { | |
9048 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-todcmp", note); | |
9049 | } | |
9050 | ||
9051 | static bfd_boolean | |
9052 | elfcore_grok_s390_todpreg (bfd *abfd, Elf_Internal_Note *note) | |
9053 | { | |
9054 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-todpreg", note); | |
9055 | } | |
9056 | ||
9057 | static bfd_boolean | |
9058 | elfcore_grok_s390_ctrs (bfd *abfd, Elf_Internal_Note *note) | |
9059 | { | |
9060 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-ctrs", note); | |
9061 | } | |
9062 | ||
9063 | static bfd_boolean | |
9064 | elfcore_grok_s390_prefix (bfd *abfd, Elf_Internal_Note *note) | |
9065 | { | |
9066 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-prefix", note); | |
9067 | } | |
9068 | ||
355b81d9 UW |
9069 | static bfd_boolean |
9070 | elfcore_grok_s390_last_break (bfd *abfd, Elf_Internal_Note *note) | |
9071 | { | |
9072 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-last-break", note); | |
9073 | } | |
9074 | ||
9075 | static bfd_boolean | |
9076 | elfcore_grok_s390_system_call (bfd *abfd, Elf_Internal_Note *note) | |
9077 | { | |
9078 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-system-call", note); | |
9079 | } | |
9080 | ||
abb3f6cc NC |
9081 | static bfd_boolean |
9082 | elfcore_grok_s390_tdb (bfd *abfd, Elf_Internal_Note *note) | |
9083 | { | |
9084 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-tdb", note); | |
9085 | } | |
9086 | ||
4ef9f41a AA |
9087 | static bfd_boolean |
9088 | elfcore_grok_s390_vxrs_low (bfd *abfd, Elf_Internal_Note *note) | |
9089 | { | |
9090 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-vxrs-low", note); | |
9091 | } | |
9092 | ||
9093 | static bfd_boolean | |
9094 | elfcore_grok_s390_vxrs_high (bfd *abfd, Elf_Internal_Note *note) | |
9095 | { | |
9096 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-vxrs-high", note); | |
9097 | } | |
9098 | ||
faa9a424 UW |
9099 | static bfd_boolean |
9100 | elfcore_grok_arm_vfp (bfd *abfd, Elf_Internal_Note *note) | |
9101 | { | |
9102 | return elfcore_make_note_pseudosection (abfd, ".reg-arm-vfp", note); | |
9103 | } | |
9104 | ||
652451f8 YZ |
9105 | static bfd_boolean |
9106 | elfcore_grok_aarch_tls (bfd *abfd, Elf_Internal_Note *note) | |
9107 | { | |
9108 | return elfcore_make_note_pseudosection (abfd, ".reg-aarch-tls", note); | |
9109 | } | |
9110 | ||
9111 | static bfd_boolean | |
9112 | elfcore_grok_aarch_hw_break (bfd *abfd, Elf_Internal_Note *note) | |
9113 | { | |
9114 | return elfcore_make_note_pseudosection (abfd, ".reg-aarch-hw-break", note); | |
9115 | } | |
9116 | ||
9117 | static bfd_boolean | |
9118 | elfcore_grok_aarch_hw_watch (bfd *abfd, Elf_Internal_Note *note) | |
9119 | { | |
9120 | return elfcore_make_note_pseudosection (abfd, ".reg-aarch-hw-watch", note); | |
9121 | } | |
9122 | ||
252b5132 | 9123 | #if defined (HAVE_PRPSINFO_T) |
4a938328 | 9124 | typedef prpsinfo_t elfcore_psinfo_t; |
7ee38065 | 9125 | #if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */ |
4a938328 MS |
9126 | typedef prpsinfo32_t elfcore_psinfo32_t; |
9127 | #endif | |
252b5132 RH |
9128 | #endif |
9129 | ||
9130 | #if defined (HAVE_PSINFO_T) | |
4a938328 | 9131 | typedef psinfo_t elfcore_psinfo_t; |
7ee38065 | 9132 | #if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */ |
4a938328 MS |
9133 | typedef psinfo32_t elfcore_psinfo32_t; |
9134 | #endif | |
252b5132 RH |
9135 | #endif |
9136 | ||
252b5132 RH |
9137 | /* return a malloc'ed copy of a string at START which is at |
9138 | most MAX bytes long, possibly without a terminating '\0'. | |
c044fabd | 9139 | the copy will always have a terminating '\0'. */ |
252b5132 | 9140 | |
936e320b | 9141 | char * |
217aa764 | 9142 | _bfd_elfcore_strndup (bfd *abfd, char *start, size_t max) |
252b5132 | 9143 | { |
dc810e39 | 9144 | char *dups; |
a50b1753 | 9145 | char *end = (char *) memchr (start, '\0', max); |
dc810e39 | 9146 | size_t len; |
252b5132 RH |
9147 | |
9148 | if (end == NULL) | |
9149 | len = max; | |
9150 | else | |
9151 | len = end - start; | |
9152 | ||
a50b1753 | 9153 | dups = (char *) bfd_alloc (abfd, len + 1); |
dc810e39 | 9154 | if (dups == NULL) |
252b5132 RH |
9155 | return NULL; |
9156 | ||
dc810e39 AM |
9157 | memcpy (dups, start, len); |
9158 | dups[len] = '\0'; | |
252b5132 | 9159 | |
dc810e39 | 9160 | return dups; |
252b5132 RH |
9161 | } |
9162 | ||
bb0082d6 | 9163 | #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) |
b34976b6 | 9164 | static bfd_boolean |
217aa764 | 9165 | elfcore_grok_psinfo (bfd *abfd, Elf_Internal_Note *note) |
252b5132 | 9166 | { |
4a938328 MS |
9167 | if (note->descsz == sizeof (elfcore_psinfo_t)) |
9168 | { | |
9169 | elfcore_psinfo_t psinfo; | |
252b5132 | 9170 | |
7ee38065 | 9171 | memcpy (&psinfo, note->descdata, sizeof (psinfo)); |
252b5132 | 9172 | |
335e41d4 | 9173 | #if defined (HAVE_PSINFO_T_PR_PID) || defined (HAVE_PRPSINFO_T_PR_PID) |
228e534f | 9174 | elf_tdata (abfd)->core->pid = psinfo.pr_pid; |
335e41d4 | 9175 | #endif |
228e534f | 9176 | elf_tdata (abfd)->core->program |
936e320b AM |
9177 | = _bfd_elfcore_strndup (abfd, psinfo.pr_fname, |
9178 | sizeof (psinfo.pr_fname)); | |
252b5132 | 9179 | |
228e534f | 9180 | elf_tdata (abfd)->core->command |
936e320b AM |
9181 | = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs, |
9182 | sizeof (psinfo.pr_psargs)); | |
4a938328 | 9183 | } |
7ee38065 | 9184 | #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T) |
4a938328 MS |
9185 | else if (note->descsz == sizeof (elfcore_psinfo32_t)) |
9186 | { | |
9187 | /* 64-bit host, 32-bit corefile */ | |
9188 | elfcore_psinfo32_t psinfo; | |
9189 | ||
7ee38065 | 9190 | memcpy (&psinfo, note->descdata, sizeof (psinfo)); |
252b5132 | 9191 | |
335e41d4 | 9192 | #if defined (HAVE_PSINFO32_T_PR_PID) || defined (HAVE_PRPSINFO32_T_PR_PID) |
228e534f | 9193 | elf_tdata (abfd)->core->pid = psinfo.pr_pid; |
335e41d4 | 9194 | #endif |
228e534f | 9195 | elf_tdata (abfd)->core->program |
936e320b AM |
9196 | = _bfd_elfcore_strndup (abfd, psinfo.pr_fname, |
9197 | sizeof (psinfo.pr_fname)); | |
4a938328 | 9198 | |
228e534f | 9199 | elf_tdata (abfd)->core->command |
936e320b AM |
9200 | = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs, |
9201 | sizeof (psinfo.pr_psargs)); | |
4a938328 MS |
9202 | } |
9203 | #endif | |
9204 | ||
9205 | else | |
9206 | { | |
9207 | /* Fail - we don't know how to handle any other | |
9208 | note size (ie. data object type). */ | |
b34976b6 | 9209 | return TRUE; |
4a938328 | 9210 | } |
252b5132 RH |
9211 | |
9212 | /* Note that for some reason, a spurious space is tacked | |
9213 | onto the end of the args in some (at least one anyway) | |
c044fabd | 9214 | implementations, so strip it off if it exists. */ |
252b5132 RH |
9215 | |
9216 | { | |
228e534f | 9217 | char *command = elf_tdata (abfd)->core->command; |
252b5132 RH |
9218 | int n = strlen (command); |
9219 | ||
9220 | if (0 < n && command[n - 1] == ' ') | |
9221 | command[n - 1] = '\0'; | |
9222 | } | |
9223 | ||
b34976b6 | 9224 | return TRUE; |
252b5132 RH |
9225 | } |
9226 | #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */ | |
9227 | ||
252b5132 | 9228 | #if defined (HAVE_PSTATUS_T) |
b34976b6 | 9229 | static bfd_boolean |
217aa764 | 9230 | elfcore_grok_pstatus (bfd *abfd, Elf_Internal_Note *note) |
252b5132 | 9231 | { |
f572a39d AM |
9232 | if (note->descsz == sizeof (pstatus_t) |
9233 | #if defined (HAVE_PXSTATUS_T) | |
9234 | || note->descsz == sizeof (pxstatus_t) | |
9235 | #endif | |
9236 | ) | |
4a938328 MS |
9237 | { |
9238 | pstatus_t pstat; | |
252b5132 | 9239 | |
4a938328 | 9240 | memcpy (&pstat, note->descdata, sizeof (pstat)); |
252b5132 | 9241 | |
228e534f | 9242 | elf_tdata (abfd)->core->pid = pstat.pr_pid; |
4a938328 | 9243 | } |
7ee38065 | 9244 | #if defined (HAVE_PSTATUS32_T) |
4a938328 MS |
9245 | else if (note->descsz == sizeof (pstatus32_t)) |
9246 | { | |
9247 | /* 64-bit host, 32-bit corefile */ | |
9248 | pstatus32_t pstat; | |
252b5132 | 9249 | |
4a938328 | 9250 | memcpy (&pstat, note->descdata, sizeof (pstat)); |
252b5132 | 9251 | |
228e534f | 9252 | elf_tdata (abfd)->core->pid = pstat.pr_pid; |
4a938328 MS |
9253 | } |
9254 | #endif | |
252b5132 RH |
9255 | /* Could grab some more details from the "representative" |
9256 | lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an | |
c044fabd | 9257 | NT_LWPSTATUS note, presumably. */ |
252b5132 | 9258 | |
b34976b6 | 9259 | return TRUE; |
252b5132 RH |
9260 | } |
9261 | #endif /* defined (HAVE_PSTATUS_T) */ | |
9262 | ||
252b5132 | 9263 | #if defined (HAVE_LWPSTATUS_T) |
b34976b6 | 9264 | static bfd_boolean |
217aa764 | 9265 | elfcore_grok_lwpstatus (bfd *abfd, Elf_Internal_Note *note) |
252b5132 RH |
9266 | { |
9267 | lwpstatus_t lwpstat; | |
9268 | char buf[100]; | |
c044fabd | 9269 | char *name; |
d4c88bbb | 9270 | size_t len; |
c044fabd | 9271 | asection *sect; |
252b5132 | 9272 | |
f572a39d AM |
9273 | if (note->descsz != sizeof (lwpstat) |
9274 | #if defined (HAVE_LWPXSTATUS_T) | |
9275 | && note->descsz != sizeof (lwpxstatus_t) | |
9276 | #endif | |
9277 | ) | |
b34976b6 | 9278 | return TRUE; |
252b5132 RH |
9279 | |
9280 | memcpy (&lwpstat, note->descdata, sizeof (lwpstat)); | |
9281 | ||
228e534f | 9282 | elf_tdata (abfd)->core->lwpid = lwpstat.pr_lwpid; |
a1504221 JB |
9283 | /* Do not overwrite the core signal if it has already been set by |
9284 | another thread. */ | |
228e534f AM |
9285 | if (elf_tdata (abfd)->core->signal == 0) |
9286 | elf_tdata (abfd)->core->signal = lwpstat.pr_cursig; | |
252b5132 | 9287 | |
c044fabd | 9288 | /* Make a ".reg/999" section. */ |
252b5132 RH |
9289 | |
9290 | sprintf (buf, ".reg/%d", elfcore_make_pid (abfd)); | |
d4c88bbb | 9291 | len = strlen (buf) + 1; |
217aa764 | 9292 | name = bfd_alloc (abfd, len); |
252b5132 | 9293 | if (name == NULL) |
b34976b6 | 9294 | return FALSE; |
d4c88bbb | 9295 | memcpy (name, buf, len); |
252b5132 | 9296 | |
117ed4f8 | 9297 | sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); |
252b5132 | 9298 | if (sect == NULL) |
b34976b6 | 9299 | return FALSE; |
252b5132 RH |
9300 | |
9301 | #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT) | |
eea6121a | 9302 | sect->size = sizeof (lwpstat.pr_context.uc_mcontext.gregs); |
252b5132 RH |
9303 | sect->filepos = note->descpos |
9304 | + offsetof (lwpstatus_t, pr_context.uc_mcontext.gregs); | |
9305 | #endif | |
9306 | ||
9307 | #if defined (HAVE_LWPSTATUS_T_PR_REG) | |
eea6121a | 9308 | sect->size = sizeof (lwpstat.pr_reg); |
252b5132 RH |
9309 | sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_reg); |
9310 | #endif | |
9311 | ||
252b5132 RH |
9312 | sect->alignment_power = 2; |
9313 | ||
9314 | if (!elfcore_maybe_make_sect (abfd, ".reg", sect)) | |
b34976b6 | 9315 | return FALSE; |
252b5132 RH |
9316 | |
9317 | /* Make a ".reg2/999" section */ | |
9318 | ||
9319 | sprintf (buf, ".reg2/%d", elfcore_make_pid (abfd)); | |
d4c88bbb | 9320 | len = strlen (buf) + 1; |
217aa764 | 9321 | name = bfd_alloc (abfd, len); |
252b5132 | 9322 | if (name == NULL) |
b34976b6 | 9323 | return FALSE; |
d4c88bbb | 9324 | memcpy (name, buf, len); |
252b5132 | 9325 | |
117ed4f8 | 9326 | sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); |
252b5132 | 9327 | if (sect == NULL) |
b34976b6 | 9328 | return FALSE; |
252b5132 RH |
9329 | |
9330 | #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT) | |
eea6121a | 9331 | sect->size = sizeof (lwpstat.pr_context.uc_mcontext.fpregs); |
252b5132 RH |
9332 | sect->filepos = note->descpos |
9333 | + offsetof (lwpstatus_t, pr_context.uc_mcontext.fpregs); | |
9334 | #endif | |
9335 | ||
9336 | #if defined (HAVE_LWPSTATUS_T_PR_FPREG) | |
eea6121a | 9337 | sect->size = sizeof (lwpstat.pr_fpreg); |
252b5132 RH |
9338 | sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_fpreg); |
9339 | #endif | |
9340 | ||
252b5132 RH |
9341 | sect->alignment_power = 2; |
9342 | ||
936e320b | 9343 | return elfcore_maybe_make_sect (abfd, ".reg2", sect); |
252b5132 RH |
9344 | } |
9345 | #endif /* defined (HAVE_LWPSTATUS_T) */ | |
9346 | ||
b34976b6 | 9347 | static bfd_boolean |
217aa764 | 9348 | elfcore_grok_win32pstatus (bfd *abfd, Elf_Internal_Note *note) |
16e9c715 NC |
9349 | { |
9350 | char buf[30]; | |
c044fabd | 9351 | char *name; |
d4c88bbb | 9352 | size_t len; |
c044fabd | 9353 | asection *sect; |
4a6636fb PA |
9354 | int type; |
9355 | int is_active_thread; | |
9356 | bfd_vma base_addr; | |
16e9c715 | 9357 | |
4a6636fb | 9358 | if (note->descsz < 728) |
b34976b6 | 9359 | return TRUE; |
16e9c715 | 9360 | |
4a6636fb PA |
9361 | if (! CONST_STRNEQ (note->namedata, "win32")) |
9362 | return TRUE; | |
9363 | ||
9364 | type = bfd_get_32 (abfd, note->descdata); | |
c044fabd | 9365 | |
4a6636fb | 9366 | switch (type) |
16e9c715 | 9367 | { |
4a6636fb | 9368 | case 1 /* NOTE_INFO_PROCESS */: |
228e534f | 9369 | /* FIXME: need to add ->core->command. */ |
4a6636fb | 9370 | /* process_info.pid */ |
228e534f | 9371 | elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, note->descdata + 8); |
4a6636fb | 9372 | /* process_info.signal */ |
228e534f | 9373 | elf_tdata (abfd)->core->signal = bfd_get_32 (abfd, note->descdata + 12); |
c044fabd | 9374 | break; |
16e9c715 | 9375 | |
4a6636fb | 9376 | case 2 /* NOTE_INFO_THREAD */: |
16e9c715 | 9377 | /* Make a ".reg/999" section. */ |
4a6636fb PA |
9378 | /* thread_info.tid */ |
9379 | sprintf (buf, ".reg/%ld", (long) bfd_get_32 (abfd, note->descdata + 8)); | |
c044fabd | 9380 | |
d4c88bbb | 9381 | len = strlen (buf) + 1; |
a50b1753 | 9382 | name = (char *) bfd_alloc (abfd, len); |
16e9c715 | 9383 | if (name == NULL) |
b34976b6 | 9384 | return FALSE; |
c044fabd | 9385 | |
d4c88bbb | 9386 | memcpy (name, buf, len); |
16e9c715 | 9387 | |
117ed4f8 | 9388 | sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); |
16e9c715 | 9389 | if (sect == NULL) |
b34976b6 | 9390 | return FALSE; |
c044fabd | 9391 | |
4a6636fb PA |
9392 | /* sizeof (thread_info.thread_context) */ |
9393 | sect->size = 716; | |
9394 | /* offsetof (thread_info.thread_context) */ | |
9395 | sect->filepos = note->descpos + 12; | |
16e9c715 NC |
9396 | sect->alignment_power = 2; |
9397 | ||
4a6636fb PA |
9398 | /* thread_info.is_active_thread */ |
9399 | is_active_thread = bfd_get_32 (abfd, note->descdata + 8); | |
9400 | ||
9401 | if (is_active_thread) | |
16e9c715 | 9402 | if (! elfcore_maybe_make_sect (abfd, ".reg", sect)) |
b34976b6 | 9403 | return FALSE; |
16e9c715 NC |
9404 | break; |
9405 | ||
4a6636fb | 9406 | case 3 /* NOTE_INFO_MODULE */: |
16e9c715 | 9407 | /* Make a ".module/xxxxxxxx" section. */ |
4a6636fb PA |
9408 | /* module_info.base_address */ |
9409 | base_addr = bfd_get_32 (abfd, note->descdata + 4); | |
0af1713e | 9410 | sprintf (buf, ".module/%08lx", (unsigned long) base_addr); |
c044fabd | 9411 | |
d4c88bbb | 9412 | len = strlen (buf) + 1; |
a50b1753 | 9413 | name = (char *) bfd_alloc (abfd, len); |
16e9c715 | 9414 | if (name == NULL) |
b34976b6 | 9415 | return FALSE; |
c044fabd | 9416 | |
d4c88bbb | 9417 | memcpy (name, buf, len); |
252b5132 | 9418 | |
117ed4f8 | 9419 | sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); |
c044fabd | 9420 | |
16e9c715 | 9421 | if (sect == NULL) |
b34976b6 | 9422 | return FALSE; |
c044fabd | 9423 | |
eea6121a | 9424 | sect->size = note->descsz; |
16e9c715 | 9425 | sect->filepos = note->descpos; |
16e9c715 NC |
9426 | sect->alignment_power = 2; |
9427 | break; | |
9428 | ||
9429 | default: | |
b34976b6 | 9430 | return TRUE; |
16e9c715 NC |
9431 | } |
9432 | ||
b34976b6 | 9433 | return TRUE; |
16e9c715 | 9434 | } |
252b5132 | 9435 | |
b34976b6 | 9436 | static bfd_boolean |
217aa764 | 9437 | elfcore_grok_note (bfd *abfd, Elf_Internal_Note *note) |
252b5132 | 9438 | { |
9c5bfbb7 | 9439 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
bb0082d6 | 9440 | |
252b5132 RH |
9441 | switch (note->type) |
9442 | { | |
9443 | default: | |
b34976b6 | 9444 | return TRUE; |
252b5132 | 9445 | |
252b5132 | 9446 | case NT_PRSTATUS: |
bb0082d6 AM |
9447 | if (bed->elf_backend_grok_prstatus) |
9448 | if ((*bed->elf_backend_grok_prstatus) (abfd, note)) | |
b34976b6 | 9449 | return TRUE; |
bb0082d6 | 9450 | #if defined (HAVE_PRSTATUS_T) |
252b5132 | 9451 | return elfcore_grok_prstatus (abfd, note); |
bb0082d6 | 9452 | #else |
b34976b6 | 9453 | return TRUE; |
252b5132 RH |
9454 | #endif |
9455 | ||
9456 | #if defined (HAVE_PSTATUS_T) | |
9457 | case NT_PSTATUS: | |
9458 | return elfcore_grok_pstatus (abfd, note); | |
9459 | #endif | |
9460 | ||
9461 | #if defined (HAVE_LWPSTATUS_T) | |
9462 | case NT_LWPSTATUS: | |
9463 | return elfcore_grok_lwpstatus (abfd, note); | |
9464 | #endif | |
9465 | ||
9466 | case NT_FPREGSET: /* FIXME: rename to NT_PRFPREG */ | |
9467 | return elfcore_grok_prfpreg (abfd, note); | |
9468 | ||
c044fabd | 9469 | case NT_WIN32PSTATUS: |
16e9c715 | 9470 | return elfcore_grok_win32pstatus (abfd, note); |
16e9c715 | 9471 | |
c044fabd | 9472 | case NT_PRXFPREG: /* Linux SSE extension */ |
e377ab71 MK |
9473 | if (note->namesz == 6 |
9474 | && strcmp (note->namedata, "LINUX") == 0) | |
ff08c6bb JB |
9475 | return elfcore_grok_prxfpreg (abfd, note); |
9476 | else | |
b34976b6 | 9477 | return TRUE; |
ff08c6bb | 9478 | |
4339cae0 L |
9479 | case NT_X86_XSTATE: /* Linux XSAVE extension */ |
9480 | if (note->namesz == 6 | |
9481 | && strcmp (note->namedata, "LINUX") == 0) | |
9482 | return elfcore_grok_xstatereg (abfd, note); | |
9483 | else | |
9484 | return TRUE; | |
9485 | ||
97753bd5 AM |
9486 | case NT_PPC_VMX: |
9487 | if (note->namesz == 6 | |
9488 | && strcmp (note->namedata, "LINUX") == 0) | |
9489 | return elfcore_grok_ppc_vmx (abfd, note); | |
9490 | else | |
9491 | return TRUE; | |
9492 | ||
89eeb0bc LM |
9493 | case NT_PPC_VSX: |
9494 | if (note->namesz == 6 | |
9495 | && strcmp (note->namedata, "LINUX") == 0) | |
9496 | return elfcore_grok_ppc_vsx (abfd, note); | |
9497 | else | |
9498 | return TRUE; | |
9499 | ||
0675e188 UW |
9500 | case NT_S390_HIGH_GPRS: |
9501 | if (note->namesz == 6 | |
9502 | && strcmp (note->namedata, "LINUX") == 0) | |
9503 | return elfcore_grok_s390_high_gprs (abfd, note); | |
9504 | else | |
9505 | return TRUE; | |
9506 | ||
d7eeb400 MS |
9507 | case NT_S390_TIMER: |
9508 | if (note->namesz == 6 | |
9509 | && strcmp (note->namedata, "LINUX") == 0) | |
9510 | return elfcore_grok_s390_timer (abfd, note); | |
9511 | else | |
9512 | return TRUE; | |
9513 | ||
9514 | case NT_S390_TODCMP: | |
9515 | if (note->namesz == 6 | |
9516 | && strcmp (note->namedata, "LINUX") == 0) | |
9517 | return elfcore_grok_s390_todcmp (abfd, note); | |
9518 | else | |
9519 | return TRUE; | |
9520 | ||
9521 | case NT_S390_TODPREG: | |
9522 | if (note->namesz == 6 | |
9523 | && strcmp (note->namedata, "LINUX") == 0) | |
9524 | return elfcore_grok_s390_todpreg (abfd, note); | |
9525 | else | |
9526 | return TRUE; | |
9527 | ||
9528 | case NT_S390_CTRS: | |
9529 | if (note->namesz == 6 | |
9530 | && strcmp (note->namedata, "LINUX") == 0) | |
9531 | return elfcore_grok_s390_ctrs (abfd, note); | |
9532 | else | |
9533 | return TRUE; | |
9534 | ||
9535 | case NT_S390_PREFIX: | |
9536 | if (note->namesz == 6 | |
9537 | && strcmp (note->namedata, "LINUX") == 0) | |
9538 | return elfcore_grok_s390_prefix (abfd, note); | |
9539 | else | |
9540 | return TRUE; | |
9541 | ||
355b81d9 UW |
9542 | case NT_S390_LAST_BREAK: |
9543 | if (note->namesz == 6 | |
9544 | && strcmp (note->namedata, "LINUX") == 0) | |
9545 | return elfcore_grok_s390_last_break (abfd, note); | |
9546 | else | |
9547 | return TRUE; | |
9548 | ||
9549 | case NT_S390_SYSTEM_CALL: | |
9550 | if (note->namesz == 6 | |
9551 | && strcmp (note->namedata, "LINUX") == 0) | |
9552 | return elfcore_grok_s390_system_call (abfd, note); | |
9553 | else | |
9554 | return TRUE; | |
9555 | ||
abb3f6cc NC |
9556 | case NT_S390_TDB: |
9557 | if (note->namesz == 6 | |
9558 | && strcmp (note->namedata, "LINUX") == 0) | |
9559 | return elfcore_grok_s390_tdb (abfd, note); | |
9560 | else | |
9561 | return TRUE; | |
9562 | ||
4ef9f41a AA |
9563 | case NT_S390_VXRS_LOW: |
9564 | if (note->namesz == 6 | |
9565 | && strcmp (note->namedata, "LINUX") == 0) | |
9566 | return elfcore_grok_s390_vxrs_low (abfd, note); | |
9567 | else | |
9568 | return TRUE; | |
9569 | ||
9570 | case NT_S390_VXRS_HIGH: | |
9571 | if (note->namesz == 6 | |
9572 | && strcmp (note->namedata, "LINUX") == 0) | |
9573 | return elfcore_grok_s390_vxrs_high (abfd, note); | |
9574 | else | |
9575 | return TRUE; | |
9576 | ||
faa9a424 UW |
9577 | case NT_ARM_VFP: |
9578 | if (note->namesz == 6 | |
9579 | && strcmp (note->namedata, "LINUX") == 0) | |
9580 | return elfcore_grok_arm_vfp (abfd, note); | |
9581 | else | |
9582 | return TRUE; | |
9583 | ||
652451f8 YZ |
9584 | case NT_ARM_TLS: |
9585 | if (note->namesz == 6 | |
9586 | && strcmp (note->namedata, "LINUX") == 0) | |
9587 | return elfcore_grok_aarch_tls (abfd, note); | |
9588 | else | |
9589 | return TRUE; | |
9590 | ||
9591 | case NT_ARM_HW_BREAK: | |
9592 | if (note->namesz == 6 | |
9593 | && strcmp (note->namedata, "LINUX") == 0) | |
9594 | return elfcore_grok_aarch_hw_break (abfd, note); | |
9595 | else | |
9596 | return TRUE; | |
9597 | ||
9598 | case NT_ARM_HW_WATCH: | |
9599 | if (note->namesz == 6 | |
9600 | && strcmp (note->namedata, "LINUX") == 0) | |
9601 | return elfcore_grok_aarch_hw_watch (abfd, note); | |
9602 | else | |
9603 | return TRUE; | |
9604 | ||
252b5132 RH |
9605 | case NT_PRPSINFO: |
9606 | case NT_PSINFO: | |
bb0082d6 AM |
9607 | if (bed->elf_backend_grok_psinfo) |
9608 | if ((*bed->elf_backend_grok_psinfo) (abfd, note)) | |
b34976b6 | 9609 | return TRUE; |
bb0082d6 | 9610 | #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) |
252b5132 | 9611 | return elfcore_grok_psinfo (abfd, note); |
bb0082d6 | 9612 | #else |
b34976b6 | 9613 | return TRUE; |
252b5132 | 9614 | #endif |
3333a7c3 RM |
9615 | |
9616 | case NT_AUXV: | |
9617 | { | |
117ed4f8 AM |
9618 | asection *sect = bfd_make_section_anyway_with_flags (abfd, ".auxv", |
9619 | SEC_HAS_CONTENTS); | |
3333a7c3 RM |
9620 | |
9621 | if (sect == NULL) | |
9622 | return FALSE; | |
eea6121a | 9623 | sect->size = note->descsz; |
3333a7c3 | 9624 | sect->filepos = note->descpos; |
3333a7c3 RM |
9625 | sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32; |
9626 | ||
9627 | return TRUE; | |
9628 | } | |
9015683b | 9629 | |
451b7c33 TT |
9630 | case NT_FILE: |
9631 | return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.file", | |
9632 | note); | |
9633 | ||
9015683b TT |
9634 | case NT_SIGINFO: |
9635 | return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.siginfo", | |
9636 | note); | |
5b2c414d | 9637 | |
252b5132 RH |
9638 | } |
9639 | } | |
9640 | ||
718175fa JK |
9641 | static bfd_boolean |
9642 | elfobj_grok_gnu_build_id (bfd *abfd, Elf_Internal_Note *note) | |
9643 | { | |
c74f7d1c | 9644 | struct bfd_build_id* build_id; |
30e8ee25 AM |
9645 | |
9646 | if (note->descsz == 0) | |
9647 | return FALSE; | |
9648 | ||
c74f7d1c JT |
9649 | build_id = bfd_alloc (abfd, sizeof (struct bfd_build_id) - 1 + note->descsz); |
9650 | if (build_id == NULL) | |
718175fa JK |
9651 | return FALSE; |
9652 | ||
c74f7d1c JT |
9653 | build_id->size = note->descsz; |
9654 | memcpy (build_id->data, note->descdata, note->descsz); | |
9655 | abfd->build_id = build_id; | |
718175fa JK |
9656 | |
9657 | return TRUE; | |
9658 | } | |
9659 | ||
9660 | static bfd_boolean | |
9661 | elfobj_grok_gnu_note (bfd *abfd, Elf_Internal_Note *note) | |
9662 | { | |
9663 | switch (note->type) | |
9664 | { | |
9665 | default: | |
9666 | return TRUE; | |
9667 | ||
9668 | case NT_GNU_BUILD_ID: | |
9669 | return elfobj_grok_gnu_build_id (abfd, note); | |
9670 | } | |
9671 | } | |
9672 | ||
e21e5835 NC |
9673 | static bfd_boolean |
9674 | elfobj_grok_stapsdt_note_1 (bfd *abfd, Elf_Internal_Note *note) | |
9675 | { | |
9676 | struct sdt_note *cur = | |
9677 | (struct sdt_note *) bfd_alloc (abfd, sizeof (struct sdt_note) | |
9678 | + note->descsz); | |
9679 | ||
9680 | cur->next = (struct sdt_note *) (elf_tdata (abfd))->sdt_note_head; | |
9681 | cur->size = (bfd_size_type) note->descsz; | |
9682 | memcpy (cur->data, note->descdata, note->descsz); | |
9683 | ||
9684 | elf_tdata (abfd)->sdt_note_head = cur; | |
9685 | ||
9686 | return TRUE; | |
9687 | } | |
9688 | ||
9689 | static bfd_boolean | |
9690 | elfobj_grok_stapsdt_note (bfd *abfd, Elf_Internal_Note *note) | |
9691 | { | |
9692 | switch (note->type) | |
9693 | { | |
9694 | case NT_STAPSDT: | |
9695 | return elfobj_grok_stapsdt_note_1 (abfd, note); | |
9696 | ||
9697 | default: | |
9698 | return TRUE; | |
9699 | } | |
9700 | } | |
9701 | ||
aa1ed4a9 JB |
9702 | static bfd_boolean |
9703 | elfcore_grok_freebsd_psinfo (bfd *abfd, Elf_Internal_Note *note) | |
9704 | { | |
9705 | size_t offset; | |
9706 | ||
aa1ed4a9 JB |
9707 | switch (abfd->arch_info->bits_per_word) |
9708 | { | |
9709 | case 32: | |
0064d223 JB |
9710 | if (note->descsz < 108) |
9711 | return FALSE; | |
aa1ed4a9 JB |
9712 | break; |
9713 | ||
9714 | case 64: | |
0064d223 JB |
9715 | if (note->descsz < 120) |
9716 | return FALSE; | |
aa1ed4a9 JB |
9717 | break; |
9718 | ||
9719 | default: | |
9720 | return FALSE; | |
9721 | } | |
9722 | ||
0064d223 JB |
9723 | /* Check for version 1 in pr_version. */ |
9724 | if (bfd_h_get_32 (abfd, (bfd_byte *) note->descdata) != 1) | |
9725 | return FALSE; | |
9726 | offset = 4; | |
9727 | ||
9728 | /* Skip over pr_psinfosz. */ | |
9729 | if (abfd->arch_info->bits_per_word == 32) | |
9730 | offset += 4; | |
9731 | else | |
9732 | { | |
9733 | offset += 4; /* Padding before pr_psinfosz. */ | |
9734 | offset += 8; | |
9735 | } | |
9736 | ||
aa1ed4a9 JB |
9737 | /* pr_fname is PRFNAMESZ (16) + 1 bytes in size. */ |
9738 | elf_tdata (abfd)->core->program | |
9739 | = _bfd_elfcore_strndup (abfd, note->descdata + offset, 17); | |
9740 | offset += 17; | |
9741 | ||
9742 | /* pr_psargs is PRARGSZ (80) + 1 bytes in size. */ | |
9743 | elf_tdata (abfd)->core->command | |
9744 | = _bfd_elfcore_strndup (abfd, note->descdata + offset, 81); | |
0064d223 JB |
9745 | offset += 81; |
9746 | ||
9747 | /* Padding before pr_pid. */ | |
9748 | offset += 2; | |
9749 | ||
9750 | /* The pr_pid field was added in version "1a". */ | |
9751 | if (note->descsz < offset + 4) | |
9752 | return TRUE; | |
9753 | ||
9754 | elf_tdata (abfd)->core->pid | |
9755 | = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset); | |
aa1ed4a9 JB |
9756 | |
9757 | return TRUE; | |
9758 | } | |
9759 | ||
9760 | static bfd_boolean | |
9761 | elfcore_grok_freebsd_prstatus (bfd *abfd, Elf_Internal_Note *note) | |
9762 | { | |
9763 | size_t offset; | |
9764 | size_t size; | |
9765 | ||
9766 | /* Check for version 1 in pr_version. */ | |
9767 | if (bfd_h_get_32 (abfd, (bfd_byte *) note->descdata) != 1) | |
9768 | return FALSE; | |
9769 | offset = 4; | |
9770 | ||
9771 | /* Skip over pr_statussz. */ | |
9772 | switch (abfd->arch_info->bits_per_word) | |
9773 | { | |
9774 | case 32: | |
9775 | offset += 4; | |
9776 | break; | |
9777 | ||
9778 | case 64: | |
9779 | offset += 4; /* Padding before pr_statussz. */ | |
9780 | offset += 8; | |
9781 | break; | |
9782 | ||
9783 | default: | |
9784 | return FALSE; | |
9785 | } | |
9786 | ||
9787 | /* Extract size of pr_reg from pr_gregsetsz. */ | |
9788 | if (abfd->arch_info->bits_per_word == 32) | |
9789 | size = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset); | |
9790 | else | |
9791 | size = bfd_h_get_64 (abfd, (bfd_byte *) note->descdata + offset); | |
9792 | ||
9793 | /* Skip over pr_gregsetsz and pr_fpregsetsz. */ | |
9794 | offset += (abfd->arch_info->bits_per_word / 8) * 2; | |
9795 | ||
9796 | /* Skip over pr_osreldate. */ | |
9797 | offset += 4; | |
9798 | ||
9799 | /* Read signal from pr_cursig. */ | |
9800 | if (elf_tdata (abfd)->core->signal == 0) | |
9801 | elf_tdata (abfd)->core->signal | |
9802 | = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset); | |
9803 | offset += 4; | |
9804 | ||
9805 | /* Read TID from pr_pid. */ | |
9806 | elf_tdata (abfd)->core->lwpid | |
9807 | = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset); | |
9808 | offset += 4; | |
9809 | ||
9810 | /* Padding before pr_reg. */ | |
9811 | if (abfd->arch_info->bits_per_word == 64) | |
9812 | offset += 4; | |
9813 | ||
9814 | /* Make a ".reg/999" section and a ".reg" section. */ | |
9815 | return _bfd_elfcore_make_pseudosection (abfd, ".reg", | |
9816 | size, note->descpos + offset); | |
9817 | } | |
9818 | ||
9819 | static bfd_boolean | |
9820 | elfcore_grok_freebsd_note (bfd *abfd, Elf_Internal_Note *note) | |
9821 | { | |
9822 | switch (note->type) | |
9823 | { | |
9824 | case NT_PRSTATUS: | |
9825 | return elfcore_grok_freebsd_prstatus (abfd, note); | |
9826 | ||
9827 | case NT_FPREGSET: | |
9828 | return elfcore_grok_prfpreg (abfd, note); | |
9829 | ||
9830 | case NT_PRPSINFO: | |
9831 | return elfcore_grok_freebsd_psinfo (abfd, note); | |
9832 | ||
9833 | case NT_FREEBSD_THRMISC: | |
9834 | if (note->namesz == 8) | |
9835 | return elfcore_make_note_pseudosection (abfd, ".thrmisc", note); | |
9836 | else | |
9837 | return TRUE; | |
9838 | ||
3350c5f5 JB |
9839 | case NT_FREEBSD_PROCSTAT_AUXV: |
9840 | { | |
9841 | asection *sect = bfd_make_section_anyway_with_flags (abfd, ".auxv", | |
9842 | SEC_HAS_CONTENTS); | |
9843 | ||
9844 | if (sect == NULL) | |
9845 | return FALSE; | |
9846 | sect->size = note->descsz - 4; | |
9847 | sect->filepos = note->descpos + 4; | |
9848 | sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32; | |
9849 | ||
9850 | return TRUE; | |
9851 | } | |
9852 | ||
aa1ed4a9 JB |
9853 | case NT_X86_XSTATE: |
9854 | if (note->namesz == 8) | |
9855 | return elfcore_grok_xstatereg (abfd, note); | |
9856 | else | |
9857 | return TRUE; | |
9858 | ||
9859 | default: | |
9860 | return TRUE; | |
9861 | } | |
9862 | } | |
9863 | ||
b34976b6 | 9864 | static bfd_boolean |
217aa764 | 9865 | elfcore_netbsd_get_lwpid (Elf_Internal_Note *note, int *lwpidp) |
50b2bdb7 AM |
9866 | { |
9867 | char *cp; | |
9868 | ||
9869 | cp = strchr (note->namedata, '@'); | |
9870 | if (cp != NULL) | |
9871 | { | |
d2b64500 | 9872 | *lwpidp = atoi(cp + 1); |
b34976b6 | 9873 | return TRUE; |
50b2bdb7 | 9874 | } |
b34976b6 | 9875 | return FALSE; |
50b2bdb7 AM |
9876 | } |
9877 | ||
b34976b6 | 9878 | static bfd_boolean |
217aa764 | 9879 | elfcore_grok_netbsd_procinfo (bfd *abfd, Elf_Internal_Note *note) |
50b2bdb7 | 9880 | { |
50b2bdb7 | 9881 | /* Signal number at offset 0x08. */ |
228e534f | 9882 | elf_tdata (abfd)->core->signal |
50b2bdb7 AM |
9883 | = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08); |
9884 | ||
9885 | /* Process ID at offset 0x50. */ | |
228e534f | 9886 | elf_tdata (abfd)->core->pid |
50b2bdb7 AM |
9887 | = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x50); |
9888 | ||
9889 | /* Command name at 0x7c (max 32 bytes, including nul). */ | |
228e534f | 9890 | elf_tdata (abfd)->core->command |
50b2bdb7 AM |
9891 | = _bfd_elfcore_strndup (abfd, note->descdata + 0x7c, 31); |
9892 | ||
7720ba9f MK |
9893 | return elfcore_make_note_pseudosection (abfd, ".note.netbsdcore.procinfo", |
9894 | note); | |
50b2bdb7 AM |
9895 | } |
9896 | ||
b34976b6 | 9897 | static bfd_boolean |
217aa764 | 9898 | elfcore_grok_netbsd_note (bfd *abfd, Elf_Internal_Note *note) |
50b2bdb7 AM |
9899 | { |
9900 | int lwp; | |
9901 | ||
9902 | if (elfcore_netbsd_get_lwpid (note, &lwp)) | |
228e534f | 9903 | elf_tdata (abfd)->core->lwpid = lwp; |
50b2bdb7 | 9904 | |
b4db1224 | 9905 | if (note->type == NT_NETBSDCORE_PROCINFO) |
50b2bdb7 AM |
9906 | { |
9907 | /* NetBSD-specific core "procinfo". Note that we expect to | |
08a40648 AM |
9908 | find this note before any of the others, which is fine, |
9909 | since the kernel writes this note out first when it | |
9910 | creates a core file. */ | |
47d9a591 | 9911 | |
50b2bdb7 AM |
9912 | return elfcore_grok_netbsd_procinfo (abfd, note); |
9913 | } | |
9914 | ||
b4db1224 JT |
9915 | /* As of Jan 2002 there are no other machine-independent notes |
9916 | defined for NetBSD core files. If the note type is less | |
9917 | than the start of the machine-dependent note types, we don't | |
9918 | understand it. */ | |
47d9a591 | 9919 | |
b4db1224 | 9920 | if (note->type < NT_NETBSDCORE_FIRSTMACH) |
b34976b6 | 9921 | return TRUE; |
50b2bdb7 AM |
9922 | |
9923 | ||
9924 | switch (bfd_get_arch (abfd)) | |
9925 | { | |
08a40648 AM |
9926 | /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and |
9927 | PT_GETFPREGS == mach+2. */ | |
50b2bdb7 AM |
9928 | |
9929 | case bfd_arch_alpha: | |
9930 | case bfd_arch_sparc: | |
9931 | switch (note->type) | |
08a40648 AM |
9932 | { |
9933 | case NT_NETBSDCORE_FIRSTMACH+0: | |
9934 | return elfcore_make_note_pseudosection (abfd, ".reg", note); | |
50b2bdb7 | 9935 | |
08a40648 AM |
9936 | case NT_NETBSDCORE_FIRSTMACH+2: |
9937 | return elfcore_make_note_pseudosection (abfd, ".reg2", note); | |
50b2bdb7 | 9938 | |
08a40648 AM |
9939 | default: |
9940 | return TRUE; | |
9941 | } | |
50b2bdb7 | 9942 | |
08a40648 AM |
9943 | /* On all other arch's, PT_GETREGS == mach+1 and |
9944 | PT_GETFPREGS == mach+3. */ | |
50b2bdb7 AM |
9945 | |
9946 | default: | |
9947 | switch (note->type) | |
08a40648 AM |
9948 | { |
9949 | case NT_NETBSDCORE_FIRSTMACH+1: | |
9950 | return elfcore_make_note_pseudosection (abfd, ".reg", note); | |
50b2bdb7 | 9951 | |
08a40648 AM |
9952 | case NT_NETBSDCORE_FIRSTMACH+3: |
9953 | return elfcore_make_note_pseudosection (abfd, ".reg2", note); | |
50b2bdb7 | 9954 | |
08a40648 AM |
9955 | default: |
9956 | return TRUE; | |
9957 | } | |
50b2bdb7 AM |
9958 | } |
9959 | /* NOTREACHED */ | |
9960 | } | |
9961 | ||
67cc5033 MK |
9962 | static bfd_boolean |
9963 | elfcore_grok_openbsd_procinfo (bfd *abfd, Elf_Internal_Note *note) | |
9964 | { | |
9965 | /* Signal number at offset 0x08. */ | |
228e534f | 9966 | elf_tdata (abfd)->core->signal |
67cc5033 MK |
9967 | = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08); |
9968 | ||
9969 | /* Process ID at offset 0x20. */ | |
228e534f | 9970 | elf_tdata (abfd)->core->pid |
67cc5033 MK |
9971 | = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x20); |
9972 | ||
9973 | /* Command name at 0x48 (max 32 bytes, including nul). */ | |
228e534f | 9974 | elf_tdata (abfd)->core->command |
67cc5033 MK |
9975 | = _bfd_elfcore_strndup (abfd, note->descdata + 0x48, 31); |
9976 | ||
9977 | return TRUE; | |
9978 | } | |
9979 | ||
9980 | static bfd_boolean | |
9981 | elfcore_grok_openbsd_note (bfd *abfd, Elf_Internal_Note *note) | |
9982 | { | |
9983 | if (note->type == NT_OPENBSD_PROCINFO) | |
9984 | return elfcore_grok_openbsd_procinfo (abfd, note); | |
9985 | ||
9986 | if (note->type == NT_OPENBSD_REGS) | |
9987 | return elfcore_make_note_pseudosection (abfd, ".reg", note); | |
9988 | ||
9989 | if (note->type == NT_OPENBSD_FPREGS) | |
9990 | return elfcore_make_note_pseudosection (abfd, ".reg2", note); | |
9991 | ||
9992 | if (note->type == NT_OPENBSD_XFPREGS) | |
9993 | return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note); | |
9994 | ||
9995 | if (note->type == NT_OPENBSD_AUXV) | |
9996 | { | |
9997 | asection *sect = bfd_make_section_anyway_with_flags (abfd, ".auxv", | |
9998 | SEC_HAS_CONTENTS); | |
9999 | ||
10000 | if (sect == NULL) | |
10001 | return FALSE; | |
10002 | sect->size = note->descsz; | |
10003 | sect->filepos = note->descpos; | |
10004 | sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32; | |
10005 | ||
10006 | return TRUE; | |
10007 | } | |
10008 | ||
10009 | if (note->type == NT_OPENBSD_WCOOKIE) | |
10010 | { | |
10011 | asection *sect = bfd_make_section_anyway_with_flags (abfd, ".wcookie", | |
10012 | SEC_HAS_CONTENTS); | |
10013 | ||
10014 | if (sect == NULL) | |
10015 | return FALSE; | |
10016 | sect->size = note->descsz; | |
10017 | sect->filepos = note->descpos; | |
10018 | sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32; | |
10019 | ||
10020 | return TRUE; | |
10021 | } | |
10022 | ||
10023 | return TRUE; | |
10024 | } | |
10025 | ||
07c6e936 | 10026 | static bfd_boolean |
d3fd4074 | 10027 | elfcore_grok_nto_status (bfd *abfd, Elf_Internal_Note *note, long *tid) |
07c6e936 NC |
10028 | { |
10029 | void *ddata = note->descdata; | |
10030 | char buf[100]; | |
10031 | char *name; | |
10032 | asection *sect; | |
f8843e87 AM |
10033 | short sig; |
10034 | unsigned flags; | |
07c6e936 NC |
10035 | |
10036 | /* nto_procfs_status 'pid' field is at offset 0. */ | |
228e534f | 10037 | elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, (bfd_byte *) ddata); |
07c6e936 | 10038 | |
f8843e87 AM |
10039 | /* nto_procfs_status 'tid' field is at offset 4. Pass it back. */ |
10040 | *tid = bfd_get_32 (abfd, (bfd_byte *) ddata + 4); | |
10041 | ||
10042 | /* nto_procfs_status 'flags' field is at offset 8. */ | |
10043 | flags = bfd_get_32 (abfd, (bfd_byte *) ddata + 8); | |
07c6e936 NC |
10044 | |
10045 | /* nto_procfs_status 'what' field is at offset 14. */ | |
f8843e87 AM |
10046 | if ((sig = bfd_get_16 (abfd, (bfd_byte *) ddata + 14)) > 0) |
10047 | { | |
228e534f AM |
10048 | elf_tdata (abfd)->core->signal = sig; |
10049 | elf_tdata (abfd)->core->lwpid = *tid; | |
f8843e87 | 10050 | } |
07c6e936 | 10051 | |
f8843e87 AM |
10052 | /* _DEBUG_FLAG_CURTID (current thread) is 0x80. Some cores |
10053 | do not come from signals so we make sure we set the current | |
10054 | thread just in case. */ | |
10055 | if (flags & 0x00000080) | |
228e534f | 10056 | elf_tdata (abfd)->core->lwpid = *tid; |
07c6e936 NC |
10057 | |
10058 | /* Make a ".qnx_core_status/%d" section. */ | |
d3fd4074 | 10059 | sprintf (buf, ".qnx_core_status/%ld", *tid); |
07c6e936 | 10060 | |
a50b1753 | 10061 | name = (char *) bfd_alloc (abfd, strlen (buf) + 1); |
07c6e936 NC |
10062 | if (name == NULL) |
10063 | return FALSE; | |
10064 | strcpy (name, buf); | |
10065 | ||
117ed4f8 | 10066 | sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); |
07c6e936 NC |
10067 | if (sect == NULL) |
10068 | return FALSE; | |
10069 | ||
eea6121a | 10070 | sect->size = note->descsz; |
07c6e936 | 10071 | sect->filepos = note->descpos; |
07c6e936 NC |
10072 | sect->alignment_power = 2; |
10073 | ||
10074 | return (elfcore_maybe_make_sect (abfd, ".qnx_core_status", sect)); | |
10075 | } | |
10076 | ||
10077 | static bfd_boolean | |
d69f560c KW |
10078 | elfcore_grok_nto_regs (bfd *abfd, |
10079 | Elf_Internal_Note *note, | |
d3fd4074 | 10080 | long tid, |
d69f560c | 10081 | char *base) |
07c6e936 NC |
10082 | { |
10083 | char buf[100]; | |
10084 | char *name; | |
10085 | asection *sect; | |
10086 | ||
d69f560c | 10087 | /* Make a "(base)/%d" section. */ |
d3fd4074 | 10088 | sprintf (buf, "%s/%ld", base, tid); |
07c6e936 | 10089 | |
a50b1753 | 10090 | name = (char *) bfd_alloc (abfd, strlen (buf) + 1); |
07c6e936 NC |
10091 | if (name == NULL) |
10092 | return FALSE; | |
10093 | strcpy (name, buf); | |
10094 | ||
117ed4f8 | 10095 | sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); |
07c6e936 NC |
10096 | if (sect == NULL) |
10097 | return FALSE; | |
10098 | ||
eea6121a | 10099 | sect->size = note->descsz; |
07c6e936 | 10100 | sect->filepos = note->descpos; |
07c6e936 NC |
10101 | sect->alignment_power = 2; |
10102 | ||
f8843e87 | 10103 | /* This is the current thread. */ |
228e534f | 10104 | if (elf_tdata (abfd)->core->lwpid == tid) |
d69f560c | 10105 | return elfcore_maybe_make_sect (abfd, base, sect); |
f8843e87 AM |
10106 | |
10107 | return TRUE; | |
07c6e936 NC |
10108 | } |
10109 | ||
10110 | #define BFD_QNT_CORE_INFO 7 | |
10111 | #define BFD_QNT_CORE_STATUS 8 | |
10112 | #define BFD_QNT_CORE_GREG 9 | |
10113 | #define BFD_QNT_CORE_FPREG 10 | |
10114 | ||
10115 | static bfd_boolean | |
217aa764 | 10116 | elfcore_grok_nto_note (bfd *abfd, Elf_Internal_Note *note) |
07c6e936 NC |
10117 | { |
10118 | /* Every GREG section has a STATUS section before it. Store the | |
811072d8 | 10119 | tid from the previous call to pass down to the next gregs |
07c6e936 | 10120 | function. */ |
d3fd4074 | 10121 | static long tid = 1; |
07c6e936 NC |
10122 | |
10123 | switch (note->type) | |
10124 | { | |
d69f560c KW |
10125 | case BFD_QNT_CORE_INFO: |
10126 | return elfcore_make_note_pseudosection (abfd, ".qnx_core_info", note); | |
10127 | case BFD_QNT_CORE_STATUS: | |
10128 | return elfcore_grok_nto_status (abfd, note, &tid); | |
10129 | case BFD_QNT_CORE_GREG: | |
10130 | return elfcore_grok_nto_regs (abfd, note, tid, ".reg"); | |
10131 | case BFD_QNT_CORE_FPREG: | |
10132 | return elfcore_grok_nto_regs (abfd, note, tid, ".reg2"); | |
10133 | default: | |
10134 | return TRUE; | |
07c6e936 NC |
10135 | } |
10136 | } | |
10137 | ||
b15fa79e AM |
10138 | static bfd_boolean |
10139 | elfcore_grok_spu_note (bfd *abfd, Elf_Internal_Note *note) | |
10140 | { | |
10141 | char *name; | |
10142 | asection *sect; | |
10143 | size_t len; | |
10144 | ||
10145 | /* Use note name as section name. */ | |
10146 | len = note->namesz; | |
a50b1753 | 10147 | name = (char *) bfd_alloc (abfd, len); |
b15fa79e AM |
10148 | if (name == NULL) |
10149 | return FALSE; | |
10150 | memcpy (name, note->namedata, len); | |
10151 | name[len - 1] = '\0'; | |
10152 | ||
10153 | sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); | |
10154 | if (sect == NULL) | |
10155 | return FALSE; | |
10156 | ||
10157 | sect->size = note->descsz; | |
10158 | sect->filepos = note->descpos; | |
10159 | sect->alignment_power = 1; | |
10160 | ||
10161 | return TRUE; | |
10162 | } | |
10163 | ||
7c76fa91 MS |
10164 | /* Function: elfcore_write_note |
10165 | ||
47d9a591 | 10166 | Inputs: |
a39f3346 | 10167 | buffer to hold note, and current size of buffer |
7c76fa91 MS |
10168 | name of note |
10169 | type of note | |
10170 | data for note | |
10171 | size of data for note | |
10172 | ||
a39f3346 AM |
10173 | Writes note to end of buffer. ELF64 notes are written exactly as |
10174 | for ELF32, despite the current (as of 2006) ELF gabi specifying | |
10175 | that they ought to have 8-byte namesz and descsz field, and have | |
10176 | 8-byte alignment. Other writers, eg. Linux kernel, do the same. | |
10177 | ||
7c76fa91 | 10178 | Return: |
a39f3346 | 10179 | Pointer to realloc'd buffer, *BUFSIZ updated. */ |
7c76fa91 MS |
10180 | |
10181 | char * | |
a39f3346 | 10182 | elfcore_write_note (bfd *abfd, |
217aa764 | 10183 | char *buf, |
a39f3346 | 10184 | int *bufsiz, |
217aa764 | 10185 | const char *name, |
a39f3346 | 10186 | int type, |
217aa764 | 10187 | const void *input, |
a39f3346 | 10188 | int size) |
7c76fa91 MS |
10189 | { |
10190 | Elf_External_Note *xnp; | |
d4c88bbb | 10191 | size_t namesz; |
d4c88bbb | 10192 | size_t newspace; |
a39f3346 | 10193 | char *dest; |
7c76fa91 | 10194 | |
d4c88bbb | 10195 | namesz = 0; |
d4c88bbb | 10196 | if (name != NULL) |
a39f3346 | 10197 | namesz = strlen (name) + 1; |
d4c88bbb | 10198 | |
a39f3346 | 10199 | newspace = 12 + ((namesz + 3) & -4) + ((size + 3) & -4); |
d4c88bbb | 10200 | |
a50b1753 | 10201 | buf = (char *) realloc (buf, *bufsiz + newspace); |
14b1c01e AM |
10202 | if (buf == NULL) |
10203 | return buf; | |
a39f3346 | 10204 | dest = buf + *bufsiz; |
7c76fa91 MS |
10205 | *bufsiz += newspace; |
10206 | xnp = (Elf_External_Note *) dest; | |
10207 | H_PUT_32 (abfd, namesz, xnp->namesz); | |
10208 | H_PUT_32 (abfd, size, xnp->descsz); | |
10209 | H_PUT_32 (abfd, type, xnp->type); | |
d4c88bbb AM |
10210 | dest = xnp->name; |
10211 | if (name != NULL) | |
10212 | { | |
10213 | memcpy (dest, name, namesz); | |
10214 | dest += namesz; | |
a39f3346 | 10215 | while (namesz & 3) |
d4c88bbb AM |
10216 | { |
10217 | *dest++ = '\0'; | |
a39f3346 | 10218 | ++namesz; |
d4c88bbb AM |
10219 | } |
10220 | } | |
10221 | memcpy (dest, input, size); | |
a39f3346 AM |
10222 | dest += size; |
10223 | while (size & 3) | |
10224 | { | |
10225 | *dest++ = '\0'; | |
10226 | ++size; | |
10227 | } | |
10228 | return buf; | |
7c76fa91 MS |
10229 | } |
10230 | ||
7c76fa91 | 10231 | char * |
217aa764 AM |
10232 | elfcore_write_prpsinfo (bfd *abfd, |
10233 | char *buf, | |
10234 | int *bufsiz, | |
10235 | const char *fname, | |
10236 | const char *psargs) | |
7c76fa91 | 10237 | { |
183e98be AM |
10238 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
10239 | ||
10240 | if (bed->elf_backend_write_core_note != NULL) | |
10241 | { | |
10242 | char *ret; | |
10243 | ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz, | |
10244 | NT_PRPSINFO, fname, psargs); | |
10245 | if (ret != NULL) | |
10246 | return ret; | |
10247 | } | |
7c76fa91 | 10248 | |
1f20dca5 | 10249 | #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) |
183e98be AM |
10250 | #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T) |
10251 | if (bed->s->elfclass == ELFCLASS32) | |
10252 | { | |
10253 | #if defined (HAVE_PSINFO32_T) | |
10254 | psinfo32_t data; | |
10255 | int note_type = NT_PSINFO; | |
10256 | #else | |
10257 | prpsinfo32_t data; | |
10258 | int note_type = NT_PRPSINFO; | |
10259 | #endif | |
10260 | ||
10261 | memset (&data, 0, sizeof (data)); | |
10262 | strncpy (data.pr_fname, fname, sizeof (data.pr_fname)); | |
10263 | strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs)); | |
10264 | return elfcore_write_note (abfd, buf, bufsiz, | |
1f20dca5 | 10265 | "CORE", note_type, &data, sizeof (data)); |
183e98be AM |
10266 | } |
10267 | else | |
10268 | #endif | |
10269 | { | |
7c76fa91 | 10270 | #if defined (HAVE_PSINFO_T) |
183e98be AM |
10271 | psinfo_t data; |
10272 | int note_type = NT_PSINFO; | |
7c76fa91 | 10273 | #else |
183e98be AM |
10274 | prpsinfo_t data; |
10275 | int note_type = NT_PRPSINFO; | |
7c76fa91 MS |
10276 | #endif |
10277 | ||
183e98be AM |
10278 | memset (&data, 0, sizeof (data)); |
10279 | strncpy (data.pr_fname, fname, sizeof (data.pr_fname)); | |
10280 | strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs)); | |
10281 | return elfcore_write_note (abfd, buf, bufsiz, | |
1f20dca5 | 10282 | "CORE", note_type, &data, sizeof (data)); |
183e98be | 10283 | } |
7c76fa91 MS |
10284 | #endif /* PSINFO_T or PRPSINFO_T */ |
10285 | ||
1f20dca5 UW |
10286 | free (buf); |
10287 | return NULL; | |
10288 | } | |
10289 | ||
70a38d42 SDJ |
10290 | char * |
10291 | elfcore_write_linux_prpsinfo32 | |
10292 | (bfd *abfd, char *buf, int *bufsiz, | |
10293 | const struct elf_internal_linux_prpsinfo *prpsinfo) | |
10294 | { | |
10295 | struct elf_external_linux_prpsinfo32 data; | |
10296 | ||
aeb70569 | 10297 | swap_linux_prpsinfo32_out (abfd, prpsinfo, &data); |
70a38d42 SDJ |
10298 | return elfcore_write_note (abfd, buf, bufsiz, "CORE", NT_PRPSINFO, |
10299 | &data, sizeof (data)); | |
10300 | } | |
10301 | ||
10302 | char * | |
10303 | elfcore_write_linux_prpsinfo64 | |
10304 | (bfd *abfd, char *buf, int *bufsiz, | |
10305 | const struct elf_internal_linux_prpsinfo *prpsinfo) | |
10306 | { | |
10307 | struct elf_external_linux_prpsinfo64 data; | |
10308 | ||
aeb70569 | 10309 | swap_linux_prpsinfo64_out (abfd, prpsinfo, &data); |
70a38d42 SDJ |
10310 | return elfcore_write_note (abfd, buf, bufsiz, |
10311 | "CORE", NT_PRPSINFO, &data, sizeof (data)); | |
10312 | } | |
10313 | ||
7c76fa91 | 10314 | char * |
217aa764 AM |
10315 | elfcore_write_prstatus (bfd *abfd, |
10316 | char *buf, | |
10317 | int *bufsiz, | |
10318 | long pid, | |
10319 | int cursig, | |
10320 | const void *gregs) | |
7c76fa91 | 10321 | { |
183e98be | 10322 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
7c76fa91 | 10323 | |
183e98be AM |
10324 | if (bed->elf_backend_write_core_note != NULL) |
10325 | { | |
10326 | char *ret; | |
10327 | ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz, | |
10328 | NT_PRSTATUS, | |
10329 | pid, cursig, gregs); | |
10330 | if (ret != NULL) | |
10331 | return ret; | |
10332 | } | |
10333 | ||
1f20dca5 | 10334 | #if defined (HAVE_PRSTATUS_T) |
183e98be AM |
10335 | #if defined (HAVE_PRSTATUS32_T) |
10336 | if (bed->s->elfclass == ELFCLASS32) | |
10337 | { | |
10338 | prstatus32_t prstat; | |
10339 | ||
10340 | memset (&prstat, 0, sizeof (prstat)); | |
10341 | prstat.pr_pid = pid; | |
10342 | prstat.pr_cursig = cursig; | |
10343 | memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg)); | |
1f20dca5 | 10344 | return elfcore_write_note (abfd, buf, bufsiz, "CORE", |
183e98be AM |
10345 | NT_PRSTATUS, &prstat, sizeof (prstat)); |
10346 | } | |
10347 | else | |
10348 | #endif | |
10349 | { | |
10350 | prstatus_t prstat; | |
10351 | ||
10352 | memset (&prstat, 0, sizeof (prstat)); | |
10353 | prstat.pr_pid = pid; | |
10354 | prstat.pr_cursig = cursig; | |
10355 | memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg)); | |
1f20dca5 | 10356 | return elfcore_write_note (abfd, buf, bufsiz, "CORE", |
183e98be AM |
10357 | NT_PRSTATUS, &prstat, sizeof (prstat)); |
10358 | } | |
7c76fa91 MS |
10359 | #endif /* HAVE_PRSTATUS_T */ |
10360 | ||
1f20dca5 UW |
10361 | free (buf); |
10362 | return NULL; | |
10363 | } | |
10364 | ||
51316059 MS |
10365 | #if defined (HAVE_LWPSTATUS_T) |
10366 | char * | |
217aa764 AM |
10367 | elfcore_write_lwpstatus (bfd *abfd, |
10368 | char *buf, | |
10369 | int *bufsiz, | |
10370 | long pid, | |
10371 | int cursig, | |
10372 | const void *gregs) | |
51316059 MS |
10373 | { |
10374 | lwpstatus_t lwpstat; | |
183e98be | 10375 | const char *note_name = "CORE"; |
51316059 MS |
10376 | |
10377 | memset (&lwpstat, 0, sizeof (lwpstat)); | |
10378 | lwpstat.pr_lwpid = pid >> 16; | |
10379 | lwpstat.pr_cursig = cursig; | |
10380 | #if defined (HAVE_LWPSTATUS_T_PR_REG) | |
d1e8523e | 10381 | memcpy (&lwpstat.pr_reg, gregs, sizeof (lwpstat.pr_reg)); |
51316059 MS |
10382 | #elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT) |
10383 | #if !defined(gregs) | |
10384 | memcpy (lwpstat.pr_context.uc_mcontext.gregs, | |
10385 | gregs, sizeof (lwpstat.pr_context.uc_mcontext.gregs)); | |
10386 | #else | |
10387 | memcpy (lwpstat.pr_context.uc_mcontext.__gregs, | |
10388 | gregs, sizeof (lwpstat.pr_context.uc_mcontext.__gregs)); | |
10389 | #endif | |
10390 | #endif | |
47d9a591 | 10391 | return elfcore_write_note (abfd, buf, bufsiz, note_name, |
51316059 MS |
10392 | NT_LWPSTATUS, &lwpstat, sizeof (lwpstat)); |
10393 | } | |
10394 | #endif /* HAVE_LWPSTATUS_T */ | |
10395 | ||
7c76fa91 MS |
10396 | #if defined (HAVE_PSTATUS_T) |
10397 | char * | |
217aa764 AM |
10398 | elfcore_write_pstatus (bfd *abfd, |
10399 | char *buf, | |
10400 | int *bufsiz, | |
10401 | long pid, | |
6c10990d NC |
10402 | int cursig ATTRIBUTE_UNUSED, |
10403 | const void *gregs ATTRIBUTE_UNUSED) | |
7c76fa91 | 10404 | { |
183e98be AM |
10405 | const char *note_name = "CORE"; |
10406 | #if defined (HAVE_PSTATUS32_T) | |
10407 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
7c76fa91 | 10408 | |
183e98be AM |
10409 | if (bed->s->elfclass == ELFCLASS32) |
10410 | { | |
10411 | pstatus32_t pstat; | |
10412 | ||
10413 | memset (&pstat, 0, sizeof (pstat)); | |
10414 | pstat.pr_pid = pid & 0xffff; | |
10415 | buf = elfcore_write_note (abfd, buf, bufsiz, note_name, | |
10416 | NT_PSTATUS, &pstat, sizeof (pstat)); | |
10417 | return buf; | |
10418 | } | |
10419 | else | |
10420 | #endif | |
10421 | { | |
10422 | pstatus_t pstat; | |
10423 | ||
10424 | memset (&pstat, 0, sizeof (pstat)); | |
10425 | pstat.pr_pid = pid & 0xffff; | |
10426 | buf = elfcore_write_note (abfd, buf, bufsiz, note_name, | |
10427 | NT_PSTATUS, &pstat, sizeof (pstat)); | |
10428 | return buf; | |
10429 | } | |
7c76fa91 MS |
10430 | } |
10431 | #endif /* HAVE_PSTATUS_T */ | |
10432 | ||
10433 | char * | |
217aa764 AM |
10434 | elfcore_write_prfpreg (bfd *abfd, |
10435 | char *buf, | |
10436 | int *bufsiz, | |
10437 | const void *fpregs, | |
10438 | int size) | |
7c76fa91 | 10439 | { |
183e98be | 10440 | const char *note_name = "CORE"; |
47d9a591 | 10441 | return elfcore_write_note (abfd, buf, bufsiz, |
7c76fa91 MS |
10442 | note_name, NT_FPREGSET, fpregs, size); |
10443 | } | |
10444 | ||
10445 | char * | |
217aa764 AM |
10446 | elfcore_write_prxfpreg (bfd *abfd, |
10447 | char *buf, | |
10448 | int *bufsiz, | |
10449 | const void *xfpregs, | |
10450 | int size) | |
7c76fa91 MS |
10451 | { |
10452 | char *note_name = "LINUX"; | |
47d9a591 | 10453 | return elfcore_write_note (abfd, buf, bufsiz, |
7c76fa91 MS |
10454 | note_name, NT_PRXFPREG, xfpregs, size); |
10455 | } | |
10456 | ||
4339cae0 L |
10457 | char * |
10458 | elfcore_write_xstatereg (bfd *abfd, char *buf, int *bufsiz, | |
10459 | const void *xfpregs, int size) | |
10460 | { | |
97de3545 JB |
10461 | char *note_name; |
10462 | if (get_elf_backend_data (abfd)->elf_osabi == ELFOSABI_FREEBSD) | |
10463 | note_name = "FreeBSD"; | |
10464 | else | |
10465 | note_name = "LINUX"; | |
4339cae0 L |
10466 | return elfcore_write_note (abfd, buf, bufsiz, |
10467 | note_name, NT_X86_XSTATE, xfpregs, size); | |
10468 | } | |
10469 | ||
97753bd5 AM |
10470 | char * |
10471 | elfcore_write_ppc_vmx (bfd *abfd, | |
10472 | char *buf, | |
10473 | int *bufsiz, | |
10474 | const void *ppc_vmx, | |
10475 | int size) | |
10476 | { | |
10477 | char *note_name = "LINUX"; | |
10478 | return elfcore_write_note (abfd, buf, bufsiz, | |
10479 | note_name, NT_PPC_VMX, ppc_vmx, size); | |
10480 | } | |
10481 | ||
89eeb0bc LM |
10482 | char * |
10483 | elfcore_write_ppc_vsx (bfd *abfd, | |
10484 | char *buf, | |
10485 | int *bufsiz, | |
10486 | const void *ppc_vsx, | |
10487 | int size) | |
10488 | { | |
10489 | char *note_name = "LINUX"; | |
10490 | return elfcore_write_note (abfd, buf, bufsiz, | |
10491 | note_name, NT_PPC_VSX, ppc_vsx, size); | |
10492 | } | |
10493 | ||
0675e188 UW |
10494 | static char * |
10495 | elfcore_write_s390_high_gprs (bfd *abfd, | |
10496 | char *buf, | |
10497 | int *bufsiz, | |
10498 | const void *s390_high_gprs, | |
10499 | int size) | |
10500 | { | |
10501 | char *note_name = "LINUX"; | |
10502 | return elfcore_write_note (abfd, buf, bufsiz, | |
10503 | note_name, NT_S390_HIGH_GPRS, | |
10504 | s390_high_gprs, size); | |
10505 | } | |
10506 | ||
d7eeb400 MS |
10507 | char * |
10508 | elfcore_write_s390_timer (bfd *abfd, | |
10509 | char *buf, | |
10510 | int *bufsiz, | |
10511 | const void *s390_timer, | |
10512 | int size) | |
10513 | { | |
10514 | char *note_name = "LINUX"; | |
10515 | return elfcore_write_note (abfd, buf, bufsiz, | |
10516 | note_name, NT_S390_TIMER, s390_timer, size); | |
10517 | } | |
10518 | ||
10519 | char * | |
10520 | elfcore_write_s390_todcmp (bfd *abfd, | |
10521 | char *buf, | |
10522 | int *bufsiz, | |
10523 | const void *s390_todcmp, | |
10524 | int size) | |
10525 | { | |
10526 | char *note_name = "LINUX"; | |
10527 | return elfcore_write_note (abfd, buf, bufsiz, | |
10528 | note_name, NT_S390_TODCMP, s390_todcmp, size); | |
10529 | } | |
10530 | ||
10531 | char * | |
10532 | elfcore_write_s390_todpreg (bfd *abfd, | |
10533 | char *buf, | |
10534 | int *bufsiz, | |
10535 | const void *s390_todpreg, | |
10536 | int size) | |
10537 | { | |
10538 | char *note_name = "LINUX"; | |
10539 | return elfcore_write_note (abfd, buf, bufsiz, | |
10540 | note_name, NT_S390_TODPREG, s390_todpreg, size); | |
10541 | } | |
10542 | ||
10543 | char * | |
10544 | elfcore_write_s390_ctrs (bfd *abfd, | |
10545 | char *buf, | |
10546 | int *bufsiz, | |
10547 | const void *s390_ctrs, | |
10548 | int size) | |
10549 | { | |
10550 | char *note_name = "LINUX"; | |
10551 | return elfcore_write_note (abfd, buf, bufsiz, | |
10552 | note_name, NT_S390_CTRS, s390_ctrs, size); | |
10553 | } | |
10554 | ||
10555 | char * | |
10556 | elfcore_write_s390_prefix (bfd *abfd, | |
10557 | char *buf, | |
10558 | int *bufsiz, | |
10559 | const void *s390_prefix, | |
10560 | int size) | |
10561 | { | |
10562 | char *note_name = "LINUX"; | |
10563 | return elfcore_write_note (abfd, buf, bufsiz, | |
10564 | note_name, NT_S390_PREFIX, s390_prefix, size); | |
10565 | } | |
10566 | ||
355b81d9 UW |
10567 | char * |
10568 | elfcore_write_s390_last_break (bfd *abfd, | |
10569 | char *buf, | |
10570 | int *bufsiz, | |
10571 | const void *s390_last_break, | |
10572 | int size) | |
10573 | { | |
10574 | char *note_name = "LINUX"; | |
10575 | return elfcore_write_note (abfd, buf, bufsiz, | |
10576 | note_name, NT_S390_LAST_BREAK, | |
10577 | s390_last_break, size); | |
10578 | } | |
10579 | ||
10580 | char * | |
10581 | elfcore_write_s390_system_call (bfd *abfd, | |
10582 | char *buf, | |
10583 | int *bufsiz, | |
10584 | const void *s390_system_call, | |
10585 | int size) | |
10586 | { | |
10587 | char *note_name = "LINUX"; | |
10588 | return elfcore_write_note (abfd, buf, bufsiz, | |
10589 | note_name, NT_S390_SYSTEM_CALL, | |
10590 | s390_system_call, size); | |
10591 | } | |
10592 | ||
abb3f6cc NC |
10593 | char * |
10594 | elfcore_write_s390_tdb (bfd *abfd, | |
10595 | char *buf, | |
10596 | int *bufsiz, | |
10597 | const void *s390_tdb, | |
10598 | int size) | |
10599 | { | |
10600 | char *note_name = "LINUX"; | |
10601 | return elfcore_write_note (abfd, buf, bufsiz, | |
10602 | note_name, NT_S390_TDB, s390_tdb, size); | |
10603 | } | |
10604 | ||
4ef9f41a AA |
10605 | char * |
10606 | elfcore_write_s390_vxrs_low (bfd *abfd, | |
10607 | char *buf, | |
10608 | int *bufsiz, | |
10609 | const void *s390_vxrs_low, | |
10610 | int size) | |
10611 | { | |
10612 | char *note_name = "LINUX"; | |
10613 | return elfcore_write_note (abfd, buf, bufsiz, | |
10614 | note_name, NT_S390_VXRS_LOW, s390_vxrs_low, size); | |
10615 | } | |
10616 | ||
10617 | char * | |
10618 | elfcore_write_s390_vxrs_high (bfd *abfd, | |
10619 | char *buf, | |
10620 | int *bufsiz, | |
10621 | const void *s390_vxrs_high, | |
10622 | int size) | |
10623 | { | |
10624 | char *note_name = "LINUX"; | |
10625 | return elfcore_write_note (abfd, buf, bufsiz, | |
10626 | note_name, NT_S390_VXRS_HIGH, | |
10627 | s390_vxrs_high, size); | |
10628 | } | |
10629 | ||
faa9a424 UW |
10630 | char * |
10631 | elfcore_write_arm_vfp (bfd *abfd, | |
10632 | char *buf, | |
10633 | int *bufsiz, | |
10634 | const void *arm_vfp, | |
10635 | int size) | |
10636 | { | |
10637 | char *note_name = "LINUX"; | |
10638 | return elfcore_write_note (abfd, buf, bufsiz, | |
10639 | note_name, NT_ARM_VFP, arm_vfp, size); | |
10640 | } | |
10641 | ||
652451f8 YZ |
10642 | char * |
10643 | elfcore_write_aarch_tls (bfd *abfd, | |
10644 | char *buf, | |
10645 | int *bufsiz, | |
10646 | const void *aarch_tls, | |
10647 | int size) | |
10648 | { | |
10649 | char *note_name = "LINUX"; | |
10650 | return elfcore_write_note (abfd, buf, bufsiz, | |
10651 | note_name, NT_ARM_TLS, aarch_tls, size); | |
10652 | } | |
10653 | ||
10654 | char * | |
10655 | elfcore_write_aarch_hw_break (bfd *abfd, | |
10656 | char *buf, | |
10657 | int *bufsiz, | |
10658 | const void *aarch_hw_break, | |
10659 | int size) | |
10660 | { | |
10661 | char *note_name = "LINUX"; | |
10662 | return elfcore_write_note (abfd, buf, bufsiz, | |
10663 | note_name, NT_ARM_HW_BREAK, aarch_hw_break, size); | |
10664 | } | |
10665 | ||
10666 | char * | |
10667 | elfcore_write_aarch_hw_watch (bfd *abfd, | |
10668 | char *buf, | |
10669 | int *bufsiz, | |
10670 | const void *aarch_hw_watch, | |
10671 | int size) | |
10672 | { | |
10673 | char *note_name = "LINUX"; | |
10674 | return elfcore_write_note (abfd, buf, bufsiz, | |
10675 | note_name, NT_ARM_HW_WATCH, aarch_hw_watch, size); | |
10676 | } | |
10677 | ||
bb864ac1 CES |
10678 | char * |
10679 | elfcore_write_register_note (bfd *abfd, | |
10680 | char *buf, | |
10681 | int *bufsiz, | |
10682 | const char *section, | |
10683 | const void *data, | |
10684 | int size) | |
10685 | { | |
10686 | if (strcmp (section, ".reg2") == 0) | |
10687 | return elfcore_write_prfpreg (abfd, buf, bufsiz, data, size); | |
10688 | if (strcmp (section, ".reg-xfp") == 0) | |
10689 | return elfcore_write_prxfpreg (abfd, buf, bufsiz, data, size); | |
4339cae0 L |
10690 | if (strcmp (section, ".reg-xstate") == 0) |
10691 | return elfcore_write_xstatereg (abfd, buf, bufsiz, data, size); | |
bb864ac1 CES |
10692 | if (strcmp (section, ".reg-ppc-vmx") == 0) |
10693 | return elfcore_write_ppc_vmx (abfd, buf, bufsiz, data, size); | |
89eeb0bc LM |
10694 | if (strcmp (section, ".reg-ppc-vsx") == 0) |
10695 | return elfcore_write_ppc_vsx (abfd, buf, bufsiz, data, size); | |
0675e188 UW |
10696 | if (strcmp (section, ".reg-s390-high-gprs") == 0) |
10697 | return elfcore_write_s390_high_gprs (abfd, buf, bufsiz, data, size); | |
d7eeb400 MS |
10698 | if (strcmp (section, ".reg-s390-timer") == 0) |
10699 | return elfcore_write_s390_timer (abfd, buf, bufsiz, data, size); | |
10700 | if (strcmp (section, ".reg-s390-todcmp") == 0) | |
10701 | return elfcore_write_s390_todcmp (abfd, buf, bufsiz, data, size); | |
10702 | if (strcmp (section, ".reg-s390-todpreg") == 0) | |
10703 | return elfcore_write_s390_todpreg (abfd, buf, bufsiz, data, size); | |
10704 | if (strcmp (section, ".reg-s390-ctrs") == 0) | |
10705 | return elfcore_write_s390_ctrs (abfd, buf, bufsiz, data, size); | |
10706 | if (strcmp (section, ".reg-s390-prefix") == 0) | |
10707 | return elfcore_write_s390_prefix (abfd, buf, bufsiz, data, size); | |
355b81d9 UW |
10708 | if (strcmp (section, ".reg-s390-last-break") == 0) |
10709 | return elfcore_write_s390_last_break (abfd, buf, bufsiz, data, size); | |
10710 | if (strcmp (section, ".reg-s390-system-call") == 0) | |
10711 | return elfcore_write_s390_system_call (abfd, buf, bufsiz, data, size); | |
abb3f6cc NC |
10712 | if (strcmp (section, ".reg-s390-tdb") == 0) |
10713 | return elfcore_write_s390_tdb (abfd, buf, bufsiz, data, size); | |
4ef9f41a AA |
10714 | if (strcmp (section, ".reg-s390-vxrs-low") == 0) |
10715 | return elfcore_write_s390_vxrs_low (abfd, buf, bufsiz, data, size); | |
10716 | if (strcmp (section, ".reg-s390-vxrs-high") == 0) | |
10717 | return elfcore_write_s390_vxrs_high (abfd, buf, bufsiz, data, size); | |
faa9a424 UW |
10718 | if (strcmp (section, ".reg-arm-vfp") == 0) |
10719 | return elfcore_write_arm_vfp (abfd, buf, bufsiz, data, size); | |
652451f8 YZ |
10720 | if (strcmp (section, ".reg-aarch-tls") == 0) |
10721 | return elfcore_write_aarch_tls (abfd, buf, bufsiz, data, size); | |
10722 | if (strcmp (section, ".reg-aarch-hw-break") == 0) | |
10723 | return elfcore_write_aarch_hw_break (abfd, buf, bufsiz, data, size); | |
10724 | if (strcmp (section, ".reg-aarch-hw-watch") == 0) | |
10725 | return elfcore_write_aarch_hw_watch (abfd, buf, bufsiz, data, size); | |
bb864ac1 CES |
10726 | return NULL; |
10727 | } | |
10728 | ||
b34976b6 | 10729 | static bfd_boolean |
718175fa | 10730 | elf_parse_notes (bfd *abfd, char *buf, size_t size, file_ptr offset) |
252b5132 | 10731 | { |
c044fabd | 10732 | char *p; |
252b5132 | 10733 | |
252b5132 RH |
10734 | p = buf; |
10735 | while (p < buf + size) | |
10736 | { | |
c044fabd KH |
10737 | /* FIXME: bad alignment assumption. */ |
10738 | Elf_External_Note *xnp = (Elf_External_Note *) p; | |
252b5132 RH |
10739 | Elf_Internal_Note in; |
10740 | ||
baea7ef1 AM |
10741 | if (offsetof (Elf_External_Note, name) > buf - p + size) |
10742 | return FALSE; | |
10743 | ||
dc810e39 | 10744 | in.type = H_GET_32 (abfd, xnp->type); |
252b5132 | 10745 | |
dc810e39 | 10746 | in.namesz = H_GET_32 (abfd, xnp->namesz); |
252b5132 | 10747 | in.namedata = xnp->name; |
baea7ef1 AM |
10748 | if (in.namesz > buf - in.namedata + size) |
10749 | return FALSE; | |
252b5132 | 10750 | |
dc810e39 | 10751 | in.descsz = H_GET_32 (abfd, xnp->descsz); |
252b5132 RH |
10752 | in.descdata = in.namedata + BFD_ALIGN (in.namesz, 4); |
10753 | in.descpos = offset + (in.descdata - buf); | |
baea7ef1 AM |
10754 | if (in.descsz != 0 |
10755 | && (in.descdata >= buf + size | |
10756 | || in.descsz > buf - in.descdata + size)) | |
10757 | return FALSE; | |
252b5132 | 10758 | |
718175fa JK |
10759 | switch (bfd_get_format (abfd)) |
10760 | { | |
10761 | default: | |
10762 | return TRUE; | |
10763 | ||
10764 | case bfd_core: | |
f64e188b | 10765 | { |
8acbedd6 | 10766 | #define GROKER_ELEMENT(S,F) {S, sizeof (S) - 1, F} |
f64e188b | 10767 | struct |
718175fa | 10768 | { |
f64e188b | 10769 | const char * string; |
8acbedd6 | 10770 | size_t len; |
f64e188b | 10771 | bfd_boolean (* func)(bfd *, Elf_Internal_Note *); |
718175fa | 10772 | } |
f64e188b | 10773 | grokers[] = |
b15fa79e | 10774 | { |
8acbedd6 | 10775 | GROKER_ELEMENT ("", elfcore_grok_note), |
aa1ed4a9 | 10776 | GROKER_ELEMENT ("FreeBSD", elfcore_grok_freebsd_note), |
8acbedd6 KS |
10777 | GROKER_ELEMENT ("NetBSD-CORE", elfcore_grok_netbsd_note), |
10778 | GROKER_ELEMENT ( "OpenBSD", elfcore_grok_openbsd_note), | |
10779 | GROKER_ELEMENT ("QNX", elfcore_grok_nto_note), | |
10780 | GROKER_ELEMENT ("SPU/", elfcore_grok_spu_note) | |
f64e188b | 10781 | }; |
8acbedd6 | 10782 | #undef GROKER_ELEMENT |
f64e188b NC |
10783 | int i; |
10784 | ||
10785 | for (i = ARRAY_SIZE (grokers); i--;) | |
8acbedd6 KS |
10786 | { |
10787 | if (in.namesz >= grokers[i].len | |
10788 | && strncmp (in.namedata, grokers[i].string, | |
10789 | grokers[i].len) == 0) | |
10790 | { | |
10791 | if (! grokers[i].func (abfd, & in)) | |
10792 | return FALSE; | |
10793 | break; | |
10794 | } | |
10795 | } | |
f64e188b NC |
10796 | break; |
10797 | } | |
718175fa JK |
10798 | |
10799 | case bfd_object: | |
10800 | if (in.namesz == sizeof "GNU" && strcmp (in.namedata, "GNU") == 0) | |
10801 | { | |
10802 | if (! elfobj_grok_gnu_note (abfd, &in)) | |
10803 | return FALSE; | |
10804 | } | |
e21e5835 NC |
10805 | else if (in.namesz == sizeof "stapsdt" |
10806 | && strcmp (in.namedata, "stapsdt") == 0) | |
10807 | { | |
10808 | if (! elfobj_grok_stapsdt_note (abfd, &in)) | |
10809 | return FALSE; | |
10810 | } | |
718175fa | 10811 | break; |
08a40648 | 10812 | } |
252b5132 RH |
10813 | |
10814 | p = in.descdata + BFD_ALIGN (in.descsz, 4); | |
10815 | } | |
10816 | ||
718175fa JK |
10817 | return TRUE; |
10818 | } | |
10819 | ||
10820 | static bfd_boolean | |
10821 | elf_read_notes (bfd *abfd, file_ptr offset, bfd_size_type size) | |
10822 | { | |
10823 | char *buf; | |
10824 | ||
10825 | if (size <= 0) | |
10826 | return TRUE; | |
10827 | ||
10828 | if (bfd_seek (abfd, offset, SEEK_SET) != 0) | |
10829 | return FALSE; | |
10830 | ||
f64e188b | 10831 | buf = (char *) bfd_malloc (size + 1); |
718175fa JK |
10832 | if (buf == NULL) |
10833 | return FALSE; | |
10834 | ||
f64e188b NC |
10835 | /* PR 17512: file: ec08f814 |
10836 | 0-termintate the buffer so that string searches will not overflow. */ | |
10837 | buf[size] = 0; | |
10838 | ||
718175fa JK |
10839 | if (bfd_bread (buf, size, abfd) != size |
10840 | || !elf_parse_notes (abfd, buf, size, offset)) | |
10841 | { | |
10842 | free (buf); | |
10843 | return FALSE; | |
10844 | } | |
10845 | ||
252b5132 | 10846 | free (buf); |
b34976b6 | 10847 | return TRUE; |
252b5132 | 10848 | } |
98d8431c JB |
10849 | \f |
10850 | /* Providing external access to the ELF program header table. */ | |
10851 | ||
10852 | /* Return an upper bound on the number of bytes required to store a | |
10853 | copy of ABFD's program header table entries. Return -1 if an error | |
10854 | occurs; bfd_get_error will return an appropriate code. */ | |
c044fabd | 10855 | |
98d8431c | 10856 | long |
217aa764 | 10857 | bfd_get_elf_phdr_upper_bound (bfd *abfd) |
98d8431c JB |
10858 | { |
10859 | if (abfd->xvec->flavour != bfd_target_elf_flavour) | |
10860 | { | |
10861 | bfd_set_error (bfd_error_wrong_format); | |
10862 | return -1; | |
10863 | } | |
10864 | ||
936e320b | 10865 | return elf_elfheader (abfd)->e_phnum * sizeof (Elf_Internal_Phdr); |
98d8431c JB |
10866 | } |
10867 | ||
98d8431c JB |
10868 | /* Copy ABFD's program header table entries to *PHDRS. The entries |
10869 | will be stored as an array of Elf_Internal_Phdr structures, as | |
10870 | defined in include/elf/internal.h. To find out how large the | |
10871 | buffer needs to be, call bfd_get_elf_phdr_upper_bound. | |
10872 | ||
10873 | Return the number of program header table entries read, or -1 if an | |
10874 | error occurs; bfd_get_error will return an appropriate code. */ | |
c044fabd | 10875 | |
98d8431c | 10876 | int |
217aa764 | 10877 | bfd_get_elf_phdrs (bfd *abfd, void *phdrs) |
98d8431c JB |
10878 | { |
10879 | int num_phdrs; | |
10880 | ||
10881 | if (abfd->xvec->flavour != bfd_target_elf_flavour) | |
10882 | { | |
10883 | bfd_set_error (bfd_error_wrong_format); | |
10884 | return -1; | |
10885 | } | |
10886 | ||
10887 | num_phdrs = elf_elfheader (abfd)->e_phnum; | |
c044fabd | 10888 | memcpy (phdrs, elf_tdata (abfd)->phdr, |
98d8431c JB |
10889 | num_phdrs * sizeof (Elf_Internal_Phdr)); |
10890 | ||
10891 | return num_phdrs; | |
10892 | } | |
ae4221d7 | 10893 | |
db6751f2 | 10894 | enum elf_reloc_type_class |
7e612e98 AM |
10895 | _bfd_elf_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED, |
10896 | const asection *rel_sec ATTRIBUTE_UNUSED, | |
10897 | const Elf_Internal_Rela *rela ATTRIBUTE_UNUSED) | |
db6751f2 JJ |
10898 | { |
10899 | return reloc_class_normal; | |
10900 | } | |
f8df10f4 | 10901 | |
47d9a591 | 10902 | /* For RELA architectures, return the relocation value for a |
f8df10f4 JJ |
10903 | relocation against a local symbol. */ |
10904 | ||
10905 | bfd_vma | |
217aa764 AM |
10906 | _bfd_elf_rela_local_sym (bfd *abfd, |
10907 | Elf_Internal_Sym *sym, | |
8517fae7 | 10908 | asection **psec, |
217aa764 | 10909 | Elf_Internal_Rela *rel) |
f8df10f4 | 10910 | { |
8517fae7 | 10911 | asection *sec = *psec; |
f8df10f4 JJ |
10912 | bfd_vma relocation; |
10913 | ||
10914 | relocation = (sec->output_section->vma | |
10915 | + sec->output_offset | |
10916 | + sym->st_value); | |
10917 | if ((sec->flags & SEC_MERGE) | |
c629eae0 | 10918 | && ELF_ST_TYPE (sym->st_info) == STT_SECTION |
dbaa2011 | 10919 | && sec->sec_info_type == SEC_INFO_TYPE_MERGE) |
f8df10f4 | 10920 | { |
f8df10f4 | 10921 | rel->r_addend = |
8517fae7 | 10922 | _bfd_merged_section_offset (abfd, psec, |
65765700 | 10923 | elf_section_data (sec)->sec_info, |
753731ee AM |
10924 | sym->st_value + rel->r_addend); |
10925 | if (sec != *psec) | |
10926 | { | |
10927 | /* If we have changed the section, and our original section is | |
10928 | marked with SEC_EXCLUDE, it means that the original | |
10929 | SEC_MERGE section has been completely subsumed in some | |
10930 | other SEC_MERGE section. In this case, we need to leave | |
10931 | some info around for --emit-relocs. */ | |
10932 | if ((sec->flags & SEC_EXCLUDE) != 0) | |
10933 | sec->kept_section = *psec; | |
10934 | sec = *psec; | |
10935 | } | |
8517fae7 AM |
10936 | rel->r_addend -= relocation; |
10937 | rel->r_addend += sec->output_section->vma + sec->output_offset; | |
f8df10f4 JJ |
10938 | } |
10939 | return relocation; | |
10940 | } | |
c629eae0 JJ |
10941 | |
10942 | bfd_vma | |
217aa764 AM |
10943 | _bfd_elf_rel_local_sym (bfd *abfd, |
10944 | Elf_Internal_Sym *sym, | |
10945 | asection **psec, | |
10946 | bfd_vma addend) | |
47d9a591 | 10947 | { |
c629eae0 JJ |
10948 | asection *sec = *psec; |
10949 | ||
dbaa2011 | 10950 | if (sec->sec_info_type != SEC_INFO_TYPE_MERGE) |
c629eae0 JJ |
10951 | return sym->st_value + addend; |
10952 | ||
10953 | return _bfd_merged_section_offset (abfd, psec, | |
65765700 | 10954 | elf_section_data (sec)->sec_info, |
753731ee | 10955 | sym->st_value + addend); |
c629eae0 JJ |
10956 | } |
10957 | ||
37b01f6a DG |
10958 | /* Adjust an address within a section. Given OFFSET within SEC, return |
10959 | the new offset within the section, based upon changes made to the | |
10960 | section. Returns -1 if the offset is now invalid. | |
10961 | The offset (in abnd out) is in target sized bytes, however big a | |
10962 | byte may be. */ | |
10963 | ||
c629eae0 | 10964 | bfd_vma |
217aa764 | 10965 | _bfd_elf_section_offset (bfd *abfd, |
92e4ec35 | 10966 | struct bfd_link_info *info, |
217aa764 AM |
10967 | asection *sec, |
10968 | bfd_vma offset) | |
c629eae0 | 10969 | { |
68bfbfcc | 10970 | switch (sec->sec_info_type) |
65765700 | 10971 | { |
dbaa2011 | 10972 | case SEC_INFO_TYPE_STABS: |
eea6121a AM |
10973 | return _bfd_stab_section_offset (sec, elf_section_data (sec)->sec_info, |
10974 | offset); | |
dbaa2011 | 10975 | case SEC_INFO_TYPE_EH_FRAME: |
92e4ec35 | 10976 | return _bfd_elf_eh_frame_section_offset (abfd, info, sec, offset); |
37b01f6a | 10977 | |
65765700 | 10978 | default: |
310fd250 L |
10979 | if ((sec->flags & SEC_ELF_REVERSE_COPY) != 0) |
10980 | { | |
37b01f6a | 10981 | /* Reverse the offset. */ |
310fd250 L |
10982 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
10983 | bfd_size_type address_size = bed->s->arch_size / 8; | |
37b01f6a DG |
10984 | |
10985 | /* address_size and sec->size are in octets. Convert | |
10986 | to bytes before subtracting the original offset. */ | |
10987 | offset = (sec->size - address_size) / bfd_octets_per_byte (abfd) - offset; | |
310fd250 | 10988 | } |
65765700 JJ |
10989 | return offset; |
10990 | } | |
c629eae0 | 10991 | } |
3333a7c3 RM |
10992 | \f |
10993 | /* Create a new BFD as if by bfd_openr. Rather than opening a file, | |
10994 | reconstruct an ELF file by reading the segments out of remote memory | |
10995 | based on the ELF file header at EHDR_VMA and the ELF program headers it | |
10996 | points to. If not null, *LOADBASEP is filled in with the difference | |
10997 | between the VMAs from which the segments were read, and the VMAs the | |
10998 | file headers (and hence BFD's idea of each section's VMA) put them at. | |
10999 | ||
11000 | The function TARGET_READ_MEMORY is called to copy LEN bytes from the | |
11001 | remote memory at target address VMA into the local buffer at MYADDR; it | |
11002 | should return zero on success or an `errno' code on failure. TEMPL must | |
11003 | be a BFD for an ELF target with the word size and byte order found in | |
11004 | the remote memory. */ | |
11005 | ||
11006 | bfd * | |
217aa764 AM |
11007 | bfd_elf_bfd_from_remote_memory |
11008 | (bfd *templ, | |
11009 | bfd_vma ehdr_vma, | |
f0a5d95a | 11010 | bfd_size_type size, |
217aa764 | 11011 | bfd_vma *loadbasep, |
fe78531d | 11012 | int (*target_read_memory) (bfd_vma, bfd_byte *, bfd_size_type)) |
3333a7c3 RM |
11013 | { |
11014 | return (*get_elf_backend_data (templ)->elf_backend_bfd_from_remote_memory) | |
5979d6b6 | 11015 | (templ, ehdr_vma, size, loadbasep, target_read_memory); |
3333a7c3 | 11016 | } |
4c45e5c9 JJ |
11017 | \f |
11018 | long | |
c9727e01 AM |
11019 | _bfd_elf_get_synthetic_symtab (bfd *abfd, |
11020 | long symcount ATTRIBUTE_UNUSED, | |
11021 | asymbol **syms ATTRIBUTE_UNUSED, | |
8615f3f2 | 11022 | long dynsymcount, |
c9727e01 AM |
11023 | asymbol **dynsyms, |
11024 | asymbol **ret) | |
4c45e5c9 JJ |
11025 | { |
11026 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
11027 | asection *relplt; | |
11028 | asymbol *s; | |
11029 | const char *relplt_name; | |
11030 | bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean); | |
11031 | arelent *p; | |
11032 | long count, i, n; | |
11033 | size_t size; | |
11034 | Elf_Internal_Shdr *hdr; | |
11035 | char *names; | |
11036 | asection *plt; | |
11037 | ||
8615f3f2 AM |
11038 | *ret = NULL; |
11039 | ||
90e3cdf2 JJ |
11040 | if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0) |
11041 | return 0; | |
11042 | ||
8615f3f2 AM |
11043 | if (dynsymcount <= 0) |
11044 | return 0; | |
11045 | ||
4c45e5c9 JJ |
11046 | if (!bed->plt_sym_val) |
11047 | return 0; | |
11048 | ||
11049 | relplt_name = bed->relplt_name; | |
11050 | if (relplt_name == NULL) | |
d35fd659 | 11051 | relplt_name = bed->rela_plts_and_copies_p ? ".rela.plt" : ".rel.plt"; |
4c45e5c9 JJ |
11052 | relplt = bfd_get_section_by_name (abfd, relplt_name); |
11053 | if (relplt == NULL) | |
11054 | return 0; | |
11055 | ||
11056 | hdr = &elf_section_data (relplt)->this_hdr; | |
11057 | if (hdr->sh_link != elf_dynsymtab (abfd) | |
11058 | || (hdr->sh_type != SHT_REL && hdr->sh_type != SHT_RELA)) | |
11059 | return 0; | |
11060 | ||
11061 | plt = bfd_get_section_by_name (abfd, ".plt"); | |
11062 | if (plt == NULL) | |
11063 | return 0; | |
11064 | ||
11065 | slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table; | |
c9727e01 | 11066 | if (! (*slurp_relocs) (abfd, relplt, dynsyms, TRUE)) |
4c45e5c9 JJ |
11067 | return -1; |
11068 | ||
eea6121a | 11069 | count = relplt->size / hdr->sh_entsize; |
4c45e5c9 JJ |
11070 | size = count * sizeof (asymbol); |
11071 | p = relplt->relocation; | |
cb53bf42 | 11072 | for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel) |
041de40d AM |
11073 | { |
11074 | size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt"); | |
11075 | if (p->addend != 0) | |
11076 | { | |
11077 | #ifdef BFD64 | |
11078 | size += sizeof ("+0x") - 1 + 8 + 8 * (bed->s->elfclass == ELFCLASS64); | |
11079 | #else | |
11080 | size += sizeof ("+0x") - 1 + 8; | |
11081 | #endif | |
11082 | } | |
11083 | } | |
4c45e5c9 | 11084 | |
a50b1753 | 11085 | s = *ret = (asymbol *) bfd_malloc (size); |
4c45e5c9 JJ |
11086 | if (s == NULL) |
11087 | return -1; | |
11088 | ||
11089 | names = (char *) (s + count); | |
11090 | p = relplt->relocation; | |
11091 | n = 0; | |
cb53bf42 | 11092 | for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel) |
4c45e5c9 JJ |
11093 | { |
11094 | size_t len; | |
11095 | bfd_vma addr; | |
11096 | ||
11097 | addr = bed->plt_sym_val (i, plt, p); | |
11098 | if (addr == (bfd_vma) -1) | |
11099 | continue; | |
11100 | ||
11101 | *s = **p->sym_ptr_ptr; | |
65a7a66f AM |
11102 | /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since |
11103 | we are defining a symbol, ensure one of them is set. */ | |
11104 | if ((s->flags & BSF_LOCAL) == 0) | |
11105 | s->flags |= BSF_GLOBAL; | |
6ba2a415 | 11106 | s->flags |= BSF_SYNTHETIC; |
4c45e5c9 JJ |
11107 | s->section = plt; |
11108 | s->value = addr - plt->vma; | |
11109 | s->name = names; | |
8f39ba8e | 11110 | s->udata.p = NULL; |
4c45e5c9 JJ |
11111 | len = strlen ((*p->sym_ptr_ptr)->name); |
11112 | memcpy (names, (*p->sym_ptr_ptr)->name, len); | |
11113 | names += len; | |
041de40d AM |
11114 | if (p->addend != 0) |
11115 | { | |
1d770845 | 11116 | char buf[30], *a; |
d324f6d6 | 11117 | |
041de40d AM |
11118 | memcpy (names, "+0x", sizeof ("+0x") - 1); |
11119 | names += sizeof ("+0x") - 1; | |
1d770845 L |
11120 | bfd_sprintf_vma (abfd, buf, p->addend); |
11121 | for (a = buf; *a == '0'; ++a) | |
11122 | ; | |
11123 | len = strlen (a); | |
11124 | memcpy (names, a, len); | |
11125 | names += len; | |
041de40d | 11126 | } |
4c45e5c9 JJ |
11127 | memcpy (names, "@plt", sizeof ("@plt")); |
11128 | names += sizeof ("@plt"); | |
8f39ba8e | 11129 | ++s, ++n; |
4c45e5c9 JJ |
11130 | } |
11131 | ||
11132 | return n; | |
11133 | } | |
3d7f7666 | 11134 | |
821e6ff6 AM |
11135 | /* It is only used by x86-64 so far. |
11136 | ??? This repeats *COM* id of zero. sec->id is supposed to be unique, | |
11137 | but current usage would allow all of _bfd_std_section to be zero. t*/ | |
3b22753a | 11138 | asection _bfd_elf_large_com_section |
821e6ff6 AM |
11139 | = BFD_FAKE_SECTION (_bfd_elf_large_com_section, NULL, |
11140 | "LARGE_COMMON", 0, SEC_IS_COMMON); | |
ecca9871 | 11141 | |
d1036acb | 11142 | void |
78245035 L |
11143 | _bfd_elf_post_process_headers (bfd * abfd, |
11144 | struct bfd_link_info * link_info ATTRIBUTE_UNUSED) | |
d1036acb L |
11145 | { |
11146 | Elf_Internal_Ehdr * i_ehdrp; /* ELF file header, internal form. */ | |
11147 | ||
11148 | i_ehdrp = elf_elfheader (abfd); | |
11149 | ||
11150 | i_ehdrp->e_ident[EI_OSABI] = get_elf_backend_data (abfd)->elf_osabi; | |
d8045f23 NC |
11151 | |
11152 | /* To make things simpler for the loader on Linux systems we set the | |
9c55345c | 11153 | osabi field to ELFOSABI_GNU if the binary contains symbols of |
f64b2e8d | 11154 | the STT_GNU_IFUNC type or STB_GNU_UNIQUE binding. */ |
d8045f23 | 11155 | if (i_ehdrp->e_ident[EI_OSABI] == ELFOSABI_NONE |
f64b2e8d | 11156 | && elf_tdata (abfd)->has_gnu_symbols) |
9c55345c | 11157 | i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_GNU; |
d1036acb | 11158 | } |
fcb93ecf PB |
11159 | |
11160 | ||
11161 | /* Return TRUE for ELF symbol types that represent functions. | |
11162 | This is the default version of this function, which is sufficient for | |
d8045f23 | 11163 | most targets. It returns true if TYPE is STT_FUNC or STT_GNU_IFUNC. */ |
fcb93ecf PB |
11164 | |
11165 | bfd_boolean | |
11166 | _bfd_elf_is_function_type (unsigned int type) | |
11167 | { | |
d8045f23 NC |
11168 | return (type == STT_FUNC |
11169 | || type == STT_GNU_IFUNC); | |
fcb93ecf | 11170 | } |
9f296da3 | 11171 | |
aef36ac1 AM |
11172 | /* If the ELF symbol SYM might be a function in SEC, return the |
11173 | function size and set *CODE_OFF to the function's entry point, | |
11174 | otherwise return zero. */ | |
9f296da3 | 11175 | |
aef36ac1 AM |
11176 | bfd_size_type |
11177 | _bfd_elf_maybe_function_sym (const asymbol *sym, asection *sec, | |
11178 | bfd_vma *code_off) | |
9f296da3 | 11179 | { |
aef36ac1 AM |
11180 | bfd_size_type size; |
11181 | ||
ff9e0f5b | 11182 | if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT |
aef36ac1 AM |
11183 | | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0 |
11184 | || sym->section != sec) | |
11185 | return 0; | |
ff9e0f5b | 11186 | |
ff9e0f5b | 11187 | *code_off = sym->value; |
aef36ac1 AM |
11188 | size = 0; |
11189 | if (!(sym->flags & BSF_SYNTHETIC)) | |
11190 | size = ((elf_symbol_type *) sym)->internal_elf_sym.st_size; | |
11191 | if (size == 0) | |
11192 | size = 1; | |
11193 | return size; | |
9f296da3 | 11194 | } |