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 | ||
baea7ef1 | 1065 | elf_parse_notes (abfd, (char *) contents, hdr->sh_size, -1); |
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 | ||
1274 | if (section_match (oheaders[hint], iheader)) | |
1275 | return hint; | |
1276 | ||
1277 | for (i = 1; i < elf_numsections (obfd); i++) | |
1278 | { | |
1279 | Elf_Internal_Shdr * oheader = oheaders[i]; | |
1280 | ||
1281 | if (section_match (oheader, iheader)) | |
1282 | /* FIXME: Do we care if there is a potential for | |
1283 | multiple matches ? */ | |
1284 | return i; | |
1285 | } | |
1286 | ||
1287 | return SHN_UNDEF; | |
1288 | } | |
1289 | ||
5522f910 NC |
1290 | /* PR 19938: Attempt to set the ELF section header fields of an OS or |
1291 | Processor specific section, based upon a matching input section. | |
1292 | Returns TRUE upon success, FALSE otherwise. */ | |
1293 | ||
1294 | static bfd_boolean | |
1295 | copy_special_section_fields (const bfd *ibfd, | |
1296 | bfd *obfd, | |
1297 | const Elf_Internal_Shdr *iheader, | |
1298 | Elf_Internal_Shdr *oheader, | |
1299 | const unsigned int secnum) | |
1300 | { | |
1301 | const struct elf_backend_data *bed = get_elf_backend_data (obfd); | |
1302 | const Elf_Internal_Shdr **iheaders = (const Elf_Internal_Shdr **) elf_elfsections (ibfd); | |
1303 | bfd_boolean changed = FALSE; | |
1304 | unsigned int sh_link; | |
1305 | ||
1306 | if (oheader->sh_type == SHT_NOBITS) | |
1307 | { | |
1308 | /* This is a feature for objcopy --only-keep-debug: | |
1309 | When a section's type is changed to NOBITS, we preserve | |
1310 | the sh_link and sh_info fields so that they can be | |
1311 | matched up with the original. | |
1312 | ||
1313 | Note: Strictly speaking these assignments are wrong. | |
1314 | The sh_link and sh_info fields should point to the | |
1315 | relevent sections in the output BFD, which may not be in | |
1316 | the same location as they were in the input BFD. But | |
1317 | the whole point of this action is to preserve the | |
1318 | original values of the sh_link and sh_info fields, so | |
1319 | that they can be matched up with the section headers in | |
1320 | the original file. So strictly speaking we may be | |
1321 | creating an invalid ELF file, but it is only for a file | |
1322 | that just contains debug info and only for sections | |
1323 | without any contents. */ | |
1324 | if (oheader->sh_link == 0) | |
1325 | oheader->sh_link = iheader->sh_link; | |
1326 | if (oheader->sh_info == 0) | |
1327 | oheader->sh_info = iheader->sh_info; | |
1328 | return TRUE; | |
1329 | } | |
1330 | ||
1331 | /* Allow the target a chance to decide how these fields should be set. */ | |
1332 | if (bed->elf_backend_copy_special_section_fields != NULL | |
1333 | && bed->elf_backend_copy_special_section_fields | |
1334 | (ibfd, obfd, iheader, oheader)) | |
1335 | return TRUE; | |
1336 | ||
1337 | /* We have an iheader which might match oheader, and which has non-zero | |
1338 | sh_info and/or sh_link fields. Attempt to follow those links and find | |
1339 | the section in the output bfd which corresponds to the linked section | |
1340 | in the input bfd. */ | |
1341 | if (iheader->sh_link != SHN_UNDEF) | |
1342 | { | |
1343 | sh_link = find_link (obfd, iheaders[iheader->sh_link], iheader->sh_link); | |
1344 | if (sh_link != SHN_UNDEF) | |
1345 | { | |
1346 | oheader->sh_link = sh_link; | |
1347 | changed = TRUE; | |
1348 | } | |
1349 | else | |
1350 | /* FIXME: Should we install iheader->sh_link | |
1351 | if we could not find a match ? */ | |
1352 | (* _bfd_error_handler) | |
695344c0 | 1353 | /* xgettext:c-format */ |
5522f910 NC |
1354 | (_("%B: Failed to find link section for section %d"), obfd, secnum); |
1355 | } | |
1356 | ||
1357 | if (iheader->sh_info) | |
1358 | { | |
1359 | /* The sh_info field can hold arbitrary information, but if the | |
1360 | SHF_LINK_INFO flag is set then it should be interpreted as a | |
1361 | section index. */ | |
1362 | if (iheader->sh_flags & SHF_INFO_LINK) | |
1363 | { | |
1364 | sh_link = find_link (obfd, iheaders[iheader->sh_info], | |
1365 | iheader->sh_info); | |
1366 | if (sh_link != SHN_UNDEF) | |
1367 | oheader->sh_flags |= SHF_INFO_LINK; | |
1368 | } | |
1369 | else | |
1370 | /* No idea what it means - just copy it. */ | |
1371 | sh_link = iheader->sh_info; | |
1372 | ||
1373 | if (sh_link != SHN_UNDEF) | |
1374 | { | |
1375 | oheader->sh_info = sh_link; | |
1376 | changed = TRUE; | |
1377 | } | |
1378 | else | |
1379 | (* _bfd_error_handler) | |
695344c0 | 1380 | /* xgettext:c-format */ |
5522f910 NC |
1381 | (_("%B: Failed to find info section for section %d"), obfd, secnum); |
1382 | } | |
1383 | ||
1384 | return changed; | |
1385 | } | |
1386 | ||
0ac4564e L |
1387 | /* Copy the program header and other data from one object module to |
1388 | another. */ | |
252b5132 | 1389 | |
b34976b6 | 1390 | bfd_boolean |
217aa764 | 1391 | _bfd_elf_copy_private_bfd_data (bfd *ibfd, bfd *obfd) |
2d502050 | 1392 | { |
5522f910 NC |
1393 | const Elf_Internal_Shdr **iheaders = (const Elf_Internal_Shdr **) elf_elfsections (ibfd); |
1394 | Elf_Internal_Shdr **oheaders = elf_elfsections (obfd); | |
1395 | const struct elf_backend_data *bed; | |
84865015 NC |
1396 | unsigned int i; |
1397 | ||
2d502050 | 1398 | if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour |
84865015 | 1399 | || bfd_get_flavour (obfd) != bfd_target_elf_flavour) |
b34976b6 | 1400 | return TRUE; |
2d502050 | 1401 | |
57b828ef L |
1402 | if (!elf_flags_init (obfd)) |
1403 | { | |
1404 | elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags; | |
1405 | elf_flags_init (obfd) = TRUE; | |
1406 | } | |
2d502050 | 1407 | |
0ac4564e | 1408 | elf_gp (obfd) = elf_gp (ibfd); |
57b828ef L |
1409 | |
1410 | /* Also copy the EI_OSABI field. */ | |
1411 | elf_elfheader (obfd)->e_ident[EI_OSABI] = | |
1412 | elf_elfheader (ibfd)->e_ident[EI_OSABI]; | |
104d59d1 | 1413 | |
5522f910 NC |
1414 | /* If set, copy the EI_ABIVERSION field. */ |
1415 | if (elf_elfheader (ibfd)->e_ident[EI_ABIVERSION]) | |
1416 | elf_elfheader (obfd)->e_ident[EI_ABIVERSION] | |
1417 | = elf_elfheader (ibfd)->e_ident[EI_ABIVERSION]; | |
1418 | ||
104d59d1 JM |
1419 | /* Copy object attributes. */ |
1420 | _bfd_elf_copy_obj_attributes (ibfd, obfd); | |
63b9bbb7 | 1421 | |
84865015 NC |
1422 | if (iheaders == NULL || oheaders == NULL) |
1423 | return TRUE; | |
63b9bbb7 | 1424 | |
5522f910 NC |
1425 | bed = get_elf_backend_data (obfd); |
1426 | ||
1427 | /* Possibly copy other fields in the section header. */ | |
84865015 | 1428 | for (i = 1; i < elf_numsections (obfd); i++) |
63b9bbb7 | 1429 | { |
84865015 NC |
1430 | unsigned int j; |
1431 | Elf_Internal_Shdr * oheader = oheaders[i]; | |
63b9bbb7 | 1432 | |
5522f910 NC |
1433 | /* Ignore ordinary sections. SHT_NOBITS sections are considered however |
1434 | because of a special case need for generating separate debug info | |
1435 | files. See below for more details. */ | |
84865015 NC |
1436 | if (oheader == NULL |
1437 | || (oheader->sh_type != SHT_NOBITS | |
5522f910 NC |
1438 | && oheader->sh_type < SHT_LOOS)) |
1439 | continue; | |
1440 | ||
1441 | /* Ignore empty sections, and sections whose | |
1442 | fields have already been initialised. */ | |
1443 | if (oheader->sh_size == 0 | |
84865015 NC |
1444 | || (oheader->sh_info != 0 && oheader->sh_link != 0)) |
1445 | continue; | |
63b9bbb7 | 1446 | |
84865015 | 1447 | /* Scan for the matching section in the input bfd. |
5522f910 NC |
1448 | First we try for a direct mapping between the input and output sections. */ |
1449 | for (j = 1; j < elf_numsections (ibfd); j++) | |
1450 | { | |
1451 | const Elf_Internal_Shdr * iheader = iheaders[j]; | |
1452 | ||
1453 | if (iheader == NULL) | |
1454 | continue; | |
1455 | ||
1456 | if (oheader->bfd_section != NULL | |
1457 | && iheader->bfd_section != NULL | |
1458 | && iheader->bfd_section->output_section != NULL | |
1459 | && iheader->bfd_section->output_section == oheader->bfd_section) | |
1460 | { | |
1461 | /* We have found a connection from the input section to the | |
1462 | output section. Attempt to copy the header fields. If | |
1463 | this fails then do not try any further sections - there | |
1464 | should only be a one-to-one mapping between input and output. */ | |
1465 | if (! copy_special_section_fields (ibfd, obfd, iheader, oheader, i)) | |
1466 | j = elf_numsections (ibfd); | |
1467 | break; | |
1468 | } | |
1469 | } | |
1470 | ||
1471 | if (j < elf_numsections (ibfd)) | |
1472 | continue; | |
1473 | ||
1474 | /* That failed. So try to deduce the corresponding input section. | |
84865015 NC |
1475 | Unfortunately we cannot compare names as the output string table |
1476 | is empty, so instead we check size, address and type. */ | |
1477 | for (j = 1; j < elf_numsections (ibfd); j++) | |
1478 | { | |
5522f910 | 1479 | const Elf_Internal_Shdr * iheader = iheaders[j]; |
84865015 | 1480 | |
5522f910 NC |
1481 | if (iheader == NULL) |
1482 | continue; | |
1483 | ||
1484 | /* Try matching fields in the input section's header. | |
1485 | Since --only-keep-debug turns all non-debug sections into | |
84865015 NC |
1486 | SHT_NOBITS sections, the output SHT_NOBITS type matches any |
1487 | input type. */ | |
1488 | if ((oheader->sh_type == SHT_NOBITS | |
1489 | || iheader->sh_type == oheader->sh_type) | |
5522f910 NC |
1490 | && (iheader->sh_flags & ~ SHF_INFO_LINK) |
1491 | == (oheader->sh_flags & ~ SHF_INFO_LINK) | |
84865015 NC |
1492 | && iheader->sh_addralign == oheader->sh_addralign |
1493 | && iheader->sh_entsize == oheader->sh_entsize | |
1494 | && iheader->sh_size == oheader->sh_size | |
1495 | && iheader->sh_addr == oheader->sh_addr | |
1496 | && (iheader->sh_info != oheader->sh_info | |
1497 | || iheader->sh_link != oheader->sh_link)) | |
63b9bbb7 | 1498 | { |
5522f910 NC |
1499 | if (copy_special_section_fields (ibfd, obfd, iheader, oheader, i)) |
1500 | break; | |
63b9bbb7 NC |
1501 | } |
1502 | } | |
5522f910 NC |
1503 | |
1504 | if (j == elf_numsections (ibfd) && oheader->sh_type >= SHT_LOOS) | |
1505 | { | |
1506 | /* Final attempt. Call the backend copy function | |
1507 | with a NULL input section. */ | |
1508 | if (bed->elf_backend_copy_special_section_fields != NULL) | |
1509 | bed->elf_backend_copy_special_section_fields (ibfd, obfd, NULL, oheader); | |
1510 | } | |
63b9bbb7 NC |
1511 | } |
1512 | ||
b34976b6 | 1513 | return TRUE; |
2d502050 L |
1514 | } |
1515 | ||
cedc298e L |
1516 | static const char * |
1517 | get_segment_type (unsigned int p_type) | |
1518 | { | |
1519 | const char *pt; | |
1520 | switch (p_type) | |
1521 | { | |
1522 | case PT_NULL: pt = "NULL"; break; | |
1523 | case PT_LOAD: pt = "LOAD"; break; | |
1524 | case PT_DYNAMIC: pt = "DYNAMIC"; break; | |
1525 | case PT_INTERP: pt = "INTERP"; break; | |
1526 | case PT_NOTE: pt = "NOTE"; break; | |
1527 | case PT_SHLIB: pt = "SHLIB"; break; | |
1528 | case PT_PHDR: pt = "PHDR"; break; | |
1529 | case PT_TLS: pt = "TLS"; break; | |
1530 | case PT_GNU_EH_FRAME: pt = "EH_FRAME"; break; | |
2b05f1b7 | 1531 | case PT_GNU_STACK: pt = "STACK"; break; |
cedc298e L |
1532 | case PT_GNU_RELRO: pt = "RELRO"; break; |
1533 | default: pt = NULL; break; | |
1534 | } | |
1535 | return pt; | |
1536 | } | |
1537 | ||
f0b79d91 L |
1538 | /* Print out the program headers. */ |
1539 | ||
b34976b6 | 1540 | bfd_boolean |
217aa764 | 1541 | _bfd_elf_print_private_bfd_data (bfd *abfd, void *farg) |
252b5132 | 1542 | { |
a50b1753 | 1543 | FILE *f = (FILE *) farg; |
252b5132 RH |
1544 | Elf_Internal_Phdr *p; |
1545 | asection *s; | |
1546 | bfd_byte *dynbuf = NULL; | |
1547 | ||
1548 | p = elf_tdata (abfd)->phdr; | |
1549 | if (p != NULL) | |
1550 | { | |
1551 | unsigned int i, c; | |
1552 | ||
1553 | fprintf (f, _("\nProgram Header:\n")); | |
1554 | c = elf_elfheader (abfd)->e_phnum; | |
1555 | for (i = 0; i < c; i++, p++) | |
1556 | { | |
cedc298e | 1557 | const char *pt = get_segment_type (p->p_type); |
252b5132 RH |
1558 | char buf[20]; |
1559 | ||
cedc298e | 1560 | if (pt == NULL) |
252b5132 | 1561 | { |
cedc298e L |
1562 | sprintf (buf, "0x%lx", p->p_type); |
1563 | pt = buf; | |
252b5132 | 1564 | } |
dc810e39 | 1565 | fprintf (f, "%8s off 0x", pt); |
60b89a18 | 1566 | bfd_fprintf_vma (abfd, f, p->p_offset); |
252b5132 | 1567 | fprintf (f, " vaddr 0x"); |
60b89a18 | 1568 | bfd_fprintf_vma (abfd, f, p->p_vaddr); |
252b5132 | 1569 | fprintf (f, " paddr 0x"); |
60b89a18 | 1570 | bfd_fprintf_vma (abfd, f, p->p_paddr); |
252b5132 RH |
1571 | fprintf (f, " align 2**%u\n", bfd_log2 (p->p_align)); |
1572 | fprintf (f, " filesz 0x"); | |
60b89a18 | 1573 | bfd_fprintf_vma (abfd, f, p->p_filesz); |
252b5132 | 1574 | fprintf (f, " memsz 0x"); |
60b89a18 | 1575 | bfd_fprintf_vma (abfd, f, p->p_memsz); |
252b5132 RH |
1576 | fprintf (f, " flags %c%c%c", |
1577 | (p->p_flags & PF_R) != 0 ? 'r' : '-', | |
1578 | (p->p_flags & PF_W) != 0 ? 'w' : '-', | |
1579 | (p->p_flags & PF_X) != 0 ? 'x' : '-'); | |
dc810e39 AM |
1580 | if ((p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X)) != 0) |
1581 | fprintf (f, " %lx", p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X)); | |
252b5132 RH |
1582 | fprintf (f, "\n"); |
1583 | } | |
1584 | } | |
1585 | ||
1586 | s = bfd_get_section_by_name (abfd, ".dynamic"); | |
1587 | if (s != NULL) | |
1588 | { | |
cb33740c | 1589 | unsigned int elfsec; |
dc810e39 | 1590 | unsigned long shlink; |
252b5132 RH |
1591 | bfd_byte *extdyn, *extdynend; |
1592 | size_t extdynsize; | |
217aa764 | 1593 | void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *); |
252b5132 RH |
1594 | |
1595 | fprintf (f, _("\nDynamic Section:\n")); | |
1596 | ||
eea6121a | 1597 | if (!bfd_malloc_and_get_section (abfd, s, &dynbuf)) |
252b5132 RH |
1598 | goto error_return; |
1599 | ||
1600 | elfsec = _bfd_elf_section_from_bfd_section (abfd, s); | |
cb33740c | 1601 | if (elfsec == SHN_BAD) |
252b5132 | 1602 | goto error_return; |
dc810e39 | 1603 | shlink = elf_elfsections (abfd)[elfsec]->sh_link; |
252b5132 RH |
1604 | |
1605 | extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn; | |
1606 | swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in; | |
1607 | ||
1608 | extdyn = dynbuf; | |
06614111 NC |
1609 | /* PR 17512: file: 6f427532. */ |
1610 | if (s->size < extdynsize) | |
1611 | goto error_return; | |
eea6121a | 1612 | extdynend = extdyn + s->size; |
1036838a NC |
1613 | /* PR 17512: file: id:000006,sig:06,src:000000,op:flip4,pos:5664. |
1614 | Fix range check. */ | |
1615 | for (; extdyn <= (extdynend - extdynsize); extdyn += extdynsize) | |
252b5132 RH |
1616 | { |
1617 | Elf_Internal_Dyn dyn; | |
ad9563d6 | 1618 | const char *name = ""; |
252b5132 | 1619 | char ab[20]; |
b34976b6 | 1620 | bfd_boolean stringp; |
ad9563d6 | 1621 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
252b5132 | 1622 | |
217aa764 | 1623 | (*swap_dyn_in) (abfd, extdyn, &dyn); |
252b5132 RH |
1624 | |
1625 | if (dyn.d_tag == DT_NULL) | |
1626 | break; | |
1627 | ||
b34976b6 | 1628 | stringp = FALSE; |
252b5132 RH |
1629 | switch (dyn.d_tag) |
1630 | { | |
1631 | default: | |
ad9563d6 CM |
1632 | if (bed->elf_backend_get_target_dtag) |
1633 | name = (*bed->elf_backend_get_target_dtag) (dyn.d_tag); | |
1634 | ||
1635 | if (!strcmp (name, "")) | |
1636 | { | |
1637 | sprintf (ab, "0x%lx", (unsigned long) dyn.d_tag); | |
1638 | name = ab; | |
1639 | } | |
252b5132 RH |
1640 | break; |
1641 | ||
b34976b6 | 1642 | case DT_NEEDED: name = "NEEDED"; stringp = TRUE; break; |
252b5132 RH |
1643 | case DT_PLTRELSZ: name = "PLTRELSZ"; break; |
1644 | case DT_PLTGOT: name = "PLTGOT"; break; | |
1645 | case DT_HASH: name = "HASH"; break; | |
1646 | case DT_STRTAB: name = "STRTAB"; break; | |
1647 | case DT_SYMTAB: name = "SYMTAB"; break; | |
1648 | case DT_RELA: name = "RELA"; break; | |
1649 | case DT_RELASZ: name = "RELASZ"; break; | |
1650 | case DT_RELAENT: name = "RELAENT"; break; | |
1651 | case DT_STRSZ: name = "STRSZ"; break; | |
1652 | case DT_SYMENT: name = "SYMENT"; break; | |
1653 | case DT_INIT: name = "INIT"; break; | |
1654 | case DT_FINI: name = "FINI"; break; | |
b34976b6 AM |
1655 | case DT_SONAME: name = "SONAME"; stringp = TRUE; break; |
1656 | case DT_RPATH: name = "RPATH"; stringp = TRUE; break; | |
252b5132 RH |
1657 | case DT_SYMBOLIC: name = "SYMBOLIC"; break; |
1658 | case DT_REL: name = "REL"; break; | |
1659 | case DT_RELSZ: name = "RELSZ"; break; | |
1660 | case DT_RELENT: name = "RELENT"; break; | |
1661 | case DT_PLTREL: name = "PLTREL"; break; | |
1662 | case DT_DEBUG: name = "DEBUG"; break; | |
1663 | case DT_TEXTREL: name = "TEXTREL"; break; | |
1664 | case DT_JMPREL: name = "JMPREL"; break; | |
94558834 L |
1665 | case DT_BIND_NOW: name = "BIND_NOW"; break; |
1666 | case DT_INIT_ARRAY: name = "INIT_ARRAY"; break; | |
1667 | case DT_FINI_ARRAY: name = "FINI_ARRAY"; break; | |
1668 | case DT_INIT_ARRAYSZ: name = "INIT_ARRAYSZ"; break; | |
1669 | case DT_FINI_ARRAYSZ: name = "FINI_ARRAYSZ"; break; | |
b34976b6 | 1670 | case DT_RUNPATH: name = "RUNPATH"; stringp = TRUE; break; |
94558834 L |
1671 | case DT_FLAGS: name = "FLAGS"; break; |
1672 | case DT_PREINIT_ARRAY: name = "PREINIT_ARRAY"; break; | |
1673 | case DT_PREINIT_ARRAYSZ: name = "PREINIT_ARRAYSZ"; break; | |
d48188b9 | 1674 | case DT_CHECKSUM: name = "CHECKSUM"; break; |
94558834 L |
1675 | case DT_PLTPADSZ: name = "PLTPADSZ"; break; |
1676 | case DT_MOVEENT: name = "MOVEENT"; break; | |
1677 | case DT_MOVESZ: name = "MOVESZ"; break; | |
1678 | case DT_FEATURE: name = "FEATURE"; break; | |
1679 | case DT_POSFLAG_1: name = "POSFLAG_1"; break; | |
1680 | case DT_SYMINSZ: name = "SYMINSZ"; break; | |
1681 | case DT_SYMINENT: name = "SYMINENT"; break; | |
b34976b6 AM |
1682 | case DT_CONFIG: name = "CONFIG"; stringp = TRUE; break; |
1683 | case DT_DEPAUDIT: name = "DEPAUDIT"; stringp = TRUE; break; | |
1684 | case DT_AUDIT: name = "AUDIT"; stringp = TRUE; break; | |
94558834 L |
1685 | case DT_PLTPAD: name = "PLTPAD"; break; |
1686 | case DT_MOVETAB: name = "MOVETAB"; break; | |
1687 | case DT_SYMINFO: name = "SYMINFO"; break; | |
1688 | case DT_RELACOUNT: name = "RELACOUNT"; break; | |
1689 | case DT_RELCOUNT: name = "RELCOUNT"; break; | |
1690 | case DT_FLAGS_1: name = "FLAGS_1"; break; | |
252b5132 RH |
1691 | case DT_VERSYM: name = "VERSYM"; break; |
1692 | case DT_VERDEF: name = "VERDEF"; break; | |
1693 | case DT_VERDEFNUM: name = "VERDEFNUM"; break; | |
1694 | case DT_VERNEED: name = "VERNEED"; break; | |
1695 | case DT_VERNEEDNUM: name = "VERNEEDNUM"; break; | |
b34976b6 | 1696 | case DT_AUXILIARY: name = "AUXILIARY"; stringp = TRUE; break; |
94558834 | 1697 | case DT_USED: name = "USED"; break; |
b34976b6 | 1698 | case DT_FILTER: name = "FILTER"; stringp = TRUE; break; |
fdc90cb4 | 1699 | case DT_GNU_HASH: name = "GNU_HASH"; break; |
252b5132 RH |
1700 | } |
1701 | ||
ad9563d6 | 1702 | fprintf (f, " %-20s ", name); |
252b5132 | 1703 | if (! stringp) |
a1f3c56e AN |
1704 | { |
1705 | fprintf (f, "0x"); | |
1706 | bfd_fprintf_vma (abfd, f, dyn.d_un.d_val); | |
1707 | } | |
252b5132 RH |
1708 | else |
1709 | { | |
1710 | const char *string; | |
dc810e39 | 1711 | unsigned int tagv = dyn.d_un.d_val; |
252b5132 | 1712 | |
dc810e39 | 1713 | string = bfd_elf_string_from_elf_section (abfd, shlink, tagv); |
252b5132 RH |
1714 | if (string == NULL) |
1715 | goto error_return; | |
1716 | fprintf (f, "%s", string); | |
1717 | } | |
1718 | fprintf (f, "\n"); | |
1719 | } | |
1720 | ||
1721 | free (dynbuf); | |
1722 | dynbuf = NULL; | |
1723 | } | |
1724 | ||
1725 | if ((elf_dynverdef (abfd) != 0 && elf_tdata (abfd)->verdef == NULL) | |
1726 | || (elf_dynverref (abfd) != 0 && elf_tdata (abfd)->verref == NULL)) | |
1727 | { | |
fc0e6df6 | 1728 | if (! _bfd_elf_slurp_version_tables (abfd, FALSE)) |
b34976b6 | 1729 | return FALSE; |
252b5132 RH |
1730 | } |
1731 | ||
1732 | if (elf_dynverdef (abfd) != 0) | |
1733 | { | |
1734 | Elf_Internal_Verdef *t; | |
1735 | ||
1736 | fprintf (f, _("\nVersion definitions:\n")); | |
1737 | for (t = elf_tdata (abfd)->verdef; t != NULL; t = t->vd_nextdef) | |
1738 | { | |
1739 | fprintf (f, "%d 0x%2.2x 0x%8.8lx %s\n", t->vd_ndx, | |
d0fb9a8d JJ |
1740 | t->vd_flags, t->vd_hash, |
1741 | t->vd_nodename ? t->vd_nodename : "<corrupt>"); | |
1742 | if (t->vd_auxptr != NULL && t->vd_auxptr->vda_nextptr != NULL) | |
252b5132 RH |
1743 | { |
1744 | Elf_Internal_Verdaux *a; | |
1745 | ||
1746 | fprintf (f, "\t"); | |
1747 | for (a = t->vd_auxptr->vda_nextptr; | |
1748 | a != NULL; | |
1749 | a = a->vda_nextptr) | |
d0fb9a8d JJ |
1750 | fprintf (f, "%s ", |
1751 | a->vda_nodename ? a->vda_nodename : "<corrupt>"); | |
252b5132 RH |
1752 | fprintf (f, "\n"); |
1753 | } | |
1754 | } | |
1755 | } | |
1756 | ||
1757 | if (elf_dynverref (abfd) != 0) | |
1758 | { | |
1759 | Elf_Internal_Verneed *t; | |
1760 | ||
1761 | fprintf (f, _("\nVersion References:\n")); | |
1762 | for (t = elf_tdata (abfd)->verref; t != NULL; t = t->vn_nextref) | |
1763 | { | |
1764 | Elf_Internal_Vernaux *a; | |
1765 | ||
d0fb9a8d JJ |
1766 | fprintf (f, _(" required from %s:\n"), |
1767 | t->vn_filename ? t->vn_filename : "<corrupt>"); | |
252b5132 RH |
1768 | for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) |
1769 | fprintf (f, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a->vna_hash, | |
d0fb9a8d JJ |
1770 | a->vna_flags, a->vna_other, |
1771 | a->vna_nodename ? a->vna_nodename : "<corrupt>"); | |
252b5132 RH |
1772 | } |
1773 | } | |
1774 | ||
b34976b6 | 1775 | return TRUE; |
252b5132 RH |
1776 | |
1777 | error_return: | |
1778 | if (dynbuf != NULL) | |
1779 | free (dynbuf); | |
b34976b6 | 1780 | return FALSE; |
252b5132 RH |
1781 | } |
1782 | ||
bb4d2ac2 L |
1783 | /* Get version string. */ |
1784 | ||
1785 | const char * | |
60bb06bc L |
1786 | _bfd_elf_get_symbol_version_string (bfd *abfd, asymbol *symbol, |
1787 | bfd_boolean *hidden) | |
bb4d2ac2 L |
1788 | { |
1789 | const char *version_string = NULL; | |
1790 | if (elf_dynversym (abfd) != 0 | |
1791 | && (elf_dynverdef (abfd) != 0 || elf_dynverref (abfd) != 0)) | |
1792 | { | |
1793 | unsigned int vernum = ((elf_symbol_type *) symbol)->version; | |
1794 | ||
1795 | *hidden = (vernum & VERSYM_HIDDEN) != 0; | |
1796 | vernum &= VERSYM_VERSION; | |
1797 | ||
1798 | if (vernum == 0) | |
1799 | version_string = ""; | |
1800 | else if (vernum == 1) | |
1801 | version_string = "Base"; | |
1802 | else if (vernum <= elf_tdata (abfd)->cverdefs) | |
1803 | version_string = | |
1804 | elf_tdata (abfd)->verdef[vernum - 1].vd_nodename; | |
1805 | else | |
1806 | { | |
1807 | Elf_Internal_Verneed *t; | |
1808 | ||
1809 | version_string = ""; | |
1810 | for (t = elf_tdata (abfd)->verref; | |
1811 | t != NULL; | |
1812 | t = t->vn_nextref) | |
1813 | { | |
1814 | Elf_Internal_Vernaux *a; | |
1815 | ||
1816 | for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) | |
1817 | { | |
1818 | if (a->vna_other == vernum) | |
1819 | { | |
1820 | version_string = a->vna_nodename; | |
1821 | break; | |
1822 | } | |
1823 | } | |
1824 | } | |
1825 | } | |
1826 | } | |
1827 | return version_string; | |
1828 | } | |
1829 | ||
252b5132 RH |
1830 | /* Display ELF-specific fields of a symbol. */ |
1831 | ||
1832 | void | |
217aa764 AM |
1833 | bfd_elf_print_symbol (bfd *abfd, |
1834 | void *filep, | |
1835 | asymbol *symbol, | |
1836 | bfd_print_symbol_type how) | |
252b5132 | 1837 | { |
a50b1753 | 1838 | FILE *file = (FILE *) filep; |
252b5132 RH |
1839 | switch (how) |
1840 | { | |
1841 | case bfd_print_symbol_name: | |
1842 | fprintf (file, "%s", symbol->name); | |
1843 | break; | |
1844 | case bfd_print_symbol_more: | |
1845 | fprintf (file, "elf "); | |
60b89a18 | 1846 | bfd_fprintf_vma (abfd, file, symbol->value); |
0af1713e | 1847 | fprintf (file, " %lx", (unsigned long) symbol->flags); |
252b5132 RH |
1848 | break; |
1849 | case bfd_print_symbol_all: | |
1850 | { | |
4e8a9624 AM |
1851 | const char *section_name; |
1852 | const char *name = NULL; | |
9c5bfbb7 | 1853 | const struct elf_backend_data *bed; |
7a13edea | 1854 | unsigned char st_other; |
dbb410c3 | 1855 | bfd_vma val; |
bb4d2ac2 L |
1856 | const char *version_string; |
1857 | bfd_boolean hidden; | |
c044fabd | 1858 | |
252b5132 | 1859 | section_name = symbol->section ? symbol->section->name : "(*none*)"; |
587ff49e RH |
1860 | |
1861 | bed = get_elf_backend_data (abfd); | |
1862 | if (bed->elf_backend_print_symbol_all) | |
c044fabd | 1863 | name = (*bed->elf_backend_print_symbol_all) (abfd, filep, symbol); |
587ff49e RH |
1864 | |
1865 | if (name == NULL) | |
1866 | { | |
7ee38065 | 1867 | name = symbol->name; |
217aa764 | 1868 | bfd_print_symbol_vandf (abfd, file, symbol); |
587ff49e RH |
1869 | } |
1870 | ||
252b5132 RH |
1871 | fprintf (file, " %s\t", section_name); |
1872 | /* Print the "other" value for a symbol. For common symbols, | |
1873 | we've already printed the size; now print the alignment. | |
1874 | For other symbols, we have no specified alignment, and | |
1875 | we've printed the address; now print the size. */ | |
dcf6c779 | 1876 | if (symbol->section && bfd_is_com_section (symbol->section)) |
dbb410c3 AM |
1877 | val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_value; |
1878 | else | |
1879 | val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_size; | |
1880 | bfd_fprintf_vma (abfd, file, val); | |
252b5132 RH |
1881 | |
1882 | /* If we have version information, print it. */ | |
60bb06bc L |
1883 | version_string = _bfd_elf_get_symbol_version_string (abfd, |
1884 | symbol, | |
1885 | &hidden); | |
bb4d2ac2 | 1886 | if (version_string) |
252b5132 | 1887 | { |
bb4d2ac2 | 1888 | if (!hidden) |
252b5132 RH |
1889 | fprintf (file, " %-11s", version_string); |
1890 | else | |
1891 | { | |
1892 | int i; | |
1893 | ||
1894 | fprintf (file, " (%s)", version_string); | |
1895 | for (i = 10 - strlen (version_string); i > 0; --i) | |
1896 | putc (' ', file); | |
1897 | } | |
1898 | } | |
1899 | ||
1900 | /* If the st_other field is not zero, print it. */ | |
7a13edea | 1901 | st_other = ((elf_symbol_type *) symbol)->internal_elf_sym.st_other; |
c044fabd | 1902 | |
7a13edea NC |
1903 | switch (st_other) |
1904 | { | |
1905 | case 0: break; | |
1906 | case STV_INTERNAL: fprintf (file, " .internal"); break; | |
1907 | case STV_HIDDEN: fprintf (file, " .hidden"); break; | |
1908 | case STV_PROTECTED: fprintf (file, " .protected"); break; | |
1909 | default: | |
1910 | /* Some other non-defined flags are also present, so print | |
1911 | everything hex. */ | |
1912 | fprintf (file, " 0x%02x", (unsigned int) st_other); | |
1913 | } | |
252b5132 | 1914 | |
587ff49e | 1915 | fprintf (file, " %s", name); |
252b5132 RH |
1916 | } |
1917 | break; | |
1918 | } | |
1919 | } | |
252b5132 RH |
1920 | \f |
1921 | /* ELF .o/exec file reading */ | |
1922 | ||
c044fabd | 1923 | /* Create a new bfd section from an ELF section header. */ |
252b5132 | 1924 | |
b34976b6 | 1925 | bfd_boolean |
217aa764 | 1926 | bfd_section_from_shdr (bfd *abfd, unsigned int shindex) |
252b5132 | 1927 | { |
4fbb74a6 AM |
1928 | Elf_Internal_Shdr *hdr; |
1929 | Elf_Internal_Ehdr *ehdr; | |
1930 | const struct elf_backend_data *bed; | |
90937f86 | 1931 | const char *name; |
bf67003b NC |
1932 | bfd_boolean ret = TRUE; |
1933 | static bfd_boolean * sections_being_created = NULL; | |
5a4b0ccc | 1934 | static bfd * sections_being_created_abfd = NULL; |
bf67003b | 1935 | static unsigned int nesting = 0; |
252b5132 | 1936 | |
4fbb74a6 AM |
1937 | if (shindex >= elf_numsections (abfd)) |
1938 | return FALSE; | |
1939 | ||
bf67003b NC |
1940 | if (++ nesting > 3) |
1941 | { | |
1942 | /* PR17512: A corrupt ELF binary might contain a recursive group of | |
06614111 | 1943 | sections, with each the string indicies pointing to the next in the |
bf67003b NC |
1944 | loop. Detect this here, by refusing to load a section that we are |
1945 | already in the process of loading. We only trigger this test if | |
1946 | we have nested at least three sections deep as normal ELF binaries | |
5a4b0ccc NC |
1947 | can expect to recurse at least once. |
1948 | ||
1949 | FIXME: It would be better if this array was attached to the bfd, | |
1950 | rather than being held in a static pointer. */ | |
1951 | ||
1952 | if (sections_being_created_abfd != abfd) | |
1953 | sections_being_created = NULL; | |
bf67003b NC |
1954 | if (sections_being_created == NULL) |
1955 | { | |
1956 | /* FIXME: It would be more efficient to attach this array to the bfd somehow. */ | |
1957 | sections_being_created = (bfd_boolean *) | |
1958 | bfd_zalloc (abfd, elf_numsections (abfd) * sizeof (bfd_boolean)); | |
5a4b0ccc | 1959 | sections_being_created_abfd = abfd; |
bf67003b NC |
1960 | } |
1961 | if (sections_being_created [shindex]) | |
1962 | { | |
4eca0228 | 1963 | _bfd_error_handler |
bf67003b NC |
1964 | (_("%B: warning: loop in section dependencies detected"), abfd); |
1965 | return FALSE; | |
1966 | } | |
1967 | sections_being_created [shindex] = TRUE; | |
1968 | } | |
1969 | ||
4fbb74a6 AM |
1970 | hdr = elf_elfsections (abfd)[shindex]; |
1971 | ehdr = elf_elfheader (abfd); | |
1972 | name = bfd_elf_string_from_elf_section (abfd, ehdr->e_shstrndx, | |
1b3a8575 | 1973 | hdr->sh_name); |
933d961a | 1974 | if (name == NULL) |
bf67003b | 1975 | goto fail; |
252b5132 | 1976 | |
4fbb74a6 | 1977 | bed = get_elf_backend_data (abfd); |
252b5132 RH |
1978 | switch (hdr->sh_type) |
1979 | { | |
1980 | case SHT_NULL: | |
1981 | /* Inactive section. Throw it away. */ | |
bf67003b | 1982 | goto success; |
252b5132 | 1983 | |
bf67003b NC |
1984 | case SHT_PROGBITS: /* Normal section with contents. */ |
1985 | case SHT_NOBITS: /* .bss section. */ | |
1986 | case SHT_HASH: /* .hash section. */ | |
1987 | case SHT_NOTE: /* .note section. */ | |
25e27870 L |
1988 | case SHT_INIT_ARRAY: /* .init_array section. */ |
1989 | case SHT_FINI_ARRAY: /* .fini_array section. */ | |
1990 | case SHT_PREINIT_ARRAY: /* .preinit_array section. */ | |
7f1204bb | 1991 | case SHT_GNU_LIBLIST: /* .gnu.liblist section. */ |
fdc90cb4 | 1992 | case SHT_GNU_HASH: /* .gnu.hash section. */ |
bf67003b NC |
1993 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); |
1994 | goto success; | |
252b5132 | 1995 | |
797fc050 | 1996 | case SHT_DYNAMIC: /* Dynamic linking information. */ |
6dc132d9 | 1997 | if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex)) |
bf67003b NC |
1998 | goto fail; |
1999 | ||
cfcac11d NC |
2000 | if (hdr->sh_link > elf_numsections (abfd)) |
2001 | { | |
caa83f8b | 2002 | /* PR 10478: Accept Solaris binaries with a sh_link |
cfcac11d NC |
2003 | field set to SHN_BEFORE or SHN_AFTER. */ |
2004 | switch (bfd_get_arch (abfd)) | |
2005 | { | |
caa83f8b | 2006 | case bfd_arch_i386: |
cfcac11d NC |
2007 | case bfd_arch_sparc: |
2008 | if (hdr->sh_link == (SHN_LORESERVE & 0xffff) /* SHN_BEFORE */ | |
2009 | || hdr->sh_link == ((SHN_LORESERVE + 1) & 0xffff) /* SHN_AFTER */) | |
2010 | break; | |
2011 | /* Otherwise fall through. */ | |
2012 | default: | |
bf67003b | 2013 | goto fail; |
cfcac11d NC |
2014 | } |
2015 | } | |
2016 | else if (elf_elfsections (abfd)[hdr->sh_link] == NULL) | |
bf67003b | 2017 | goto fail; |
cfcac11d | 2018 | else if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_STRTAB) |
797fc050 AM |
2019 | { |
2020 | Elf_Internal_Shdr *dynsymhdr; | |
2021 | ||
2022 | /* The shared libraries distributed with hpux11 have a bogus | |
2023 | sh_link field for the ".dynamic" section. Find the | |
2024 | string table for the ".dynsym" section instead. */ | |
2025 | if (elf_dynsymtab (abfd) != 0) | |
2026 | { | |
2027 | dynsymhdr = elf_elfsections (abfd)[elf_dynsymtab (abfd)]; | |
2028 | hdr->sh_link = dynsymhdr->sh_link; | |
2029 | } | |
2030 | else | |
2031 | { | |
2032 | unsigned int i, num_sec; | |
2033 | ||
2034 | num_sec = elf_numsections (abfd); | |
2035 | for (i = 1; i < num_sec; i++) | |
2036 | { | |
2037 | dynsymhdr = elf_elfsections (abfd)[i]; | |
2038 | if (dynsymhdr->sh_type == SHT_DYNSYM) | |
2039 | { | |
2040 | hdr->sh_link = dynsymhdr->sh_link; | |
2041 | break; | |
2042 | } | |
2043 | } | |
2044 | } | |
2045 | } | |
bf67003b | 2046 | goto success; |
797fc050 | 2047 | |
bf67003b | 2048 | case SHT_SYMTAB: /* A symbol table. */ |
252b5132 | 2049 | if (elf_onesymtab (abfd) == shindex) |
bf67003b | 2050 | goto success; |
252b5132 | 2051 | |
a50b2160 | 2052 | if (hdr->sh_entsize != bed->s->sizeof_sym) |
bf67003b NC |
2053 | goto fail; |
2054 | ||
3337c1e5 | 2055 | if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size) |
eee3b786 AM |
2056 | { |
2057 | if (hdr->sh_size != 0) | |
bf67003b | 2058 | goto fail; |
eee3b786 AM |
2059 | /* Some assemblers erroneously set sh_info to one with a |
2060 | zero sh_size. ld sees this as a global symbol count | |
2061 | of (unsigned) -1. Fix it here. */ | |
2062 | hdr->sh_info = 0; | |
bf67003b | 2063 | goto success; |
eee3b786 | 2064 | } |
bf67003b | 2065 | |
16ad13ec NC |
2066 | /* PR 18854: A binary might contain more than one symbol table. |
2067 | Unusual, but possible. Warn, but continue. */ | |
2068 | if (elf_onesymtab (abfd) != 0) | |
2069 | { | |
4eca0228 | 2070 | _bfd_error_handler |
695344c0 | 2071 | /* xgettext:c-format */ |
16ad13ec NC |
2072 | (_("%B: warning: multiple symbol tables detected - ignoring the table in section %u"), |
2073 | abfd, shindex); | |
2074 | goto success; | |
2075 | } | |
252b5132 | 2076 | elf_onesymtab (abfd) = shindex; |
6a40cf0c NC |
2077 | elf_symtab_hdr (abfd) = *hdr; |
2078 | elf_elfsections (abfd)[shindex] = hdr = & elf_symtab_hdr (abfd); | |
252b5132 RH |
2079 | abfd->flags |= HAS_SYMS; |
2080 | ||
2081 | /* Sometimes a shared object will map in the symbol table. If | |
08a40648 AM |
2082 | SHF_ALLOC is set, and this is a shared object, then we also |
2083 | treat this section as a BFD section. We can not base the | |
2084 | decision purely on SHF_ALLOC, because that flag is sometimes | |
2085 | set in a relocatable object file, which would confuse the | |
2086 | linker. */ | |
252b5132 RH |
2087 | if ((hdr->sh_flags & SHF_ALLOC) != 0 |
2088 | && (abfd->flags & DYNAMIC) != 0 | |
6dc132d9 L |
2089 | && ! _bfd_elf_make_section_from_shdr (abfd, hdr, name, |
2090 | shindex)) | |
bf67003b | 2091 | goto fail; |
252b5132 | 2092 | |
1b3a8575 AM |
2093 | /* Go looking for SHT_SYMTAB_SHNDX too, since if there is one we |
2094 | can't read symbols without that section loaded as well. It | |
2095 | is most likely specified by the next section header. */ | |
6a40cf0c NC |
2096 | { |
2097 | elf_section_list * entry; | |
2098 | unsigned int i, num_sec; | |
1b3a8575 | 2099 | |
6a40cf0c NC |
2100 | for (entry = elf_symtab_shndx_list (abfd); entry != NULL; entry = entry->next) |
2101 | if (entry->hdr.sh_link == shindex) | |
2102 | goto success; | |
2103 | ||
2104 | num_sec = elf_numsections (abfd); | |
2105 | for (i = shindex + 1; i < num_sec; i++) | |
2106 | { | |
2107 | Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i]; | |
2108 | ||
2109 | if (hdr2->sh_type == SHT_SYMTAB_SHNDX | |
2110 | && hdr2->sh_link == shindex) | |
2111 | break; | |
2112 | } | |
2113 | ||
2114 | if (i == num_sec) | |
2115 | for (i = 1; i < shindex; i++) | |
1b3a8575 AM |
2116 | { |
2117 | Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i]; | |
6a40cf0c | 2118 | |
1b3a8575 AM |
2119 | if (hdr2->sh_type == SHT_SYMTAB_SHNDX |
2120 | && hdr2->sh_link == shindex) | |
2121 | break; | |
2122 | } | |
6a40cf0c NC |
2123 | |
2124 | if (i != shindex) | |
2125 | ret = bfd_section_from_shdr (abfd, i); | |
2126 | /* else FIXME: we have failed to find the symbol table - should we issue an error ? */ | |
2127 | goto success; | |
2128 | } | |
252b5132 | 2129 | |
bf67003b | 2130 | case SHT_DYNSYM: /* A dynamic symbol table. */ |
252b5132 | 2131 | if (elf_dynsymtab (abfd) == shindex) |
bf67003b | 2132 | goto success; |
252b5132 | 2133 | |
a50b2160 | 2134 | if (hdr->sh_entsize != bed->s->sizeof_sym) |
bf67003b NC |
2135 | goto fail; |
2136 | ||
eee3b786 AM |
2137 | if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size) |
2138 | { | |
2139 | if (hdr->sh_size != 0) | |
bf67003b NC |
2140 | goto fail; |
2141 | ||
eee3b786 AM |
2142 | /* Some linkers erroneously set sh_info to one with a |
2143 | zero sh_size. ld sees this as a global symbol count | |
2144 | of (unsigned) -1. Fix it here. */ | |
2145 | hdr->sh_info = 0; | |
bf67003b | 2146 | goto success; |
eee3b786 | 2147 | } |
bf67003b | 2148 | |
16ad13ec NC |
2149 | /* PR 18854: A binary might contain more than one dynamic symbol table. |
2150 | Unusual, but possible. Warn, but continue. */ | |
2151 | if (elf_dynsymtab (abfd) != 0) | |
2152 | { | |
4eca0228 | 2153 | _bfd_error_handler |
695344c0 | 2154 | /* xgettext:c-format */ |
16ad13ec NC |
2155 | (_("%B: warning: multiple dynamic symbol tables detected - ignoring the table in section %u"), |
2156 | abfd, shindex); | |
2157 | goto success; | |
2158 | } | |
252b5132 RH |
2159 | elf_dynsymtab (abfd) = shindex; |
2160 | elf_tdata (abfd)->dynsymtab_hdr = *hdr; | |
2161 | elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->dynsymtab_hdr; | |
2162 | abfd->flags |= HAS_SYMS; | |
2163 | ||
2164 | /* Besides being a symbol table, we also treat this as a regular | |
2165 | section, so that objcopy can handle it. */ | |
bf67003b NC |
2166 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); |
2167 | goto success; | |
252b5132 | 2168 | |
bf67003b | 2169 | case SHT_SYMTAB_SHNDX: /* Symbol section indices when >64k sections. */ |
6a40cf0c NC |
2170 | { |
2171 | elf_section_list * entry; | |
9ad5cbcf | 2172 | |
6a40cf0c NC |
2173 | for (entry = elf_symtab_shndx_list (abfd); entry != NULL; entry = entry->next) |
2174 | if (entry->ndx == shindex) | |
2175 | goto success; | |
2176 | ||
2177 | entry = bfd_alloc (abfd, sizeof * entry); | |
2178 | if (entry == NULL) | |
2179 | goto fail; | |
2180 | entry->ndx = shindex; | |
2181 | entry->hdr = * hdr; | |
2182 | entry->next = elf_symtab_shndx_list (abfd); | |
2183 | elf_symtab_shndx_list (abfd) = entry; | |
2184 | elf_elfsections (abfd)[shindex] = & entry->hdr; | |
2185 | goto success; | |
2186 | } | |
9ad5cbcf | 2187 | |
bf67003b | 2188 | case SHT_STRTAB: /* A string table. */ |
252b5132 | 2189 | if (hdr->bfd_section != NULL) |
bf67003b NC |
2190 | goto success; |
2191 | ||
252b5132 RH |
2192 | if (ehdr->e_shstrndx == shindex) |
2193 | { | |
2194 | elf_tdata (abfd)->shstrtab_hdr = *hdr; | |
2195 | elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->shstrtab_hdr; | |
bf67003b | 2196 | goto success; |
252b5132 | 2197 | } |
bf67003b | 2198 | |
1b3a8575 AM |
2199 | if (elf_elfsections (abfd)[elf_onesymtab (abfd)]->sh_link == shindex) |
2200 | { | |
2201 | symtab_strtab: | |
2202 | elf_tdata (abfd)->strtab_hdr = *hdr; | |
2203 | elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->strtab_hdr; | |
bf67003b | 2204 | goto success; |
1b3a8575 | 2205 | } |
bf67003b | 2206 | |
1b3a8575 AM |
2207 | if (elf_elfsections (abfd)[elf_dynsymtab (abfd)]->sh_link == shindex) |
2208 | { | |
2209 | dynsymtab_strtab: | |
2210 | elf_tdata (abfd)->dynstrtab_hdr = *hdr; | |
2211 | hdr = &elf_tdata (abfd)->dynstrtab_hdr; | |
2212 | elf_elfsections (abfd)[shindex] = hdr; | |
2213 | /* We also treat this as a regular section, so that objcopy | |
2214 | can handle it. */ | |
bf67003b NC |
2215 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, |
2216 | shindex); | |
2217 | goto success; | |
1b3a8575 | 2218 | } |
252b5132 | 2219 | |
1b3a8575 AM |
2220 | /* If the string table isn't one of the above, then treat it as a |
2221 | regular section. We need to scan all the headers to be sure, | |
2222 | just in case this strtab section appeared before the above. */ | |
2223 | if (elf_onesymtab (abfd) == 0 || elf_dynsymtab (abfd) == 0) | |
2224 | { | |
2225 | unsigned int i, num_sec; | |
252b5132 | 2226 | |
1b3a8575 AM |
2227 | num_sec = elf_numsections (abfd); |
2228 | for (i = 1; i < num_sec; i++) | |
2229 | { | |
2230 | Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i]; | |
2231 | if (hdr2->sh_link == shindex) | |
2232 | { | |
933d961a JJ |
2233 | /* Prevent endless recursion on broken objects. */ |
2234 | if (i == shindex) | |
bf67003b | 2235 | goto fail; |
1b3a8575 | 2236 | if (! bfd_section_from_shdr (abfd, i)) |
bf67003b | 2237 | goto fail; |
1b3a8575 AM |
2238 | if (elf_onesymtab (abfd) == i) |
2239 | goto symtab_strtab; | |
2240 | if (elf_dynsymtab (abfd) == i) | |
2241 | goto dynsymtab_strtab; | |
2242 | } | |
2243 | } | |
2244 | } | |
bf67003b NC |
2245 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); |
2246 | goto success; | |
252b5132 RH |
2247 | |
2248 | case SHT_REL: | |
2249 | case SHT_RELA: | |
2250 | /* *These* do a lot of work -- but build no sections! */ | |
2251 | { | |
2252 | asection *target_sect; | |
d4730f92 | 2253 | Elf_Internal_Shdr *hdr2, **p_hdr; |
9ad5cbcf | 2254 | unsigned int num_sec = elf_numsections (abfd); |
d4730f92 | 2255 | struct bfd_elf_section_data *esdt; |
252b5132 | 2256 | |
aa2ca951 JJ |
2257 | if (hdr->sh_entsize |
2258 | != (bfd_size_type) (hdr->sh_type == SHT_REL | |
a50b2160 | 2259 | ? bed->s->sizeof_rel : bed->s->sizeof_rela)) |
bf67003b | 2260 | goto fail; |
a50b2160 | 2261 | |
03ae5f59 | 2262 | /* Check for a bogus link to avoid crashing. */ |
4fbb74a6 | 2263 | if (hdr->sh_link >= num_sec) |
03ae5f59 | 2264 | { |
4eca0228 | 2265 | _bfd_error_handler |
695344c0 | 2266 | /* xgettext:c-format */ |
4eca0228 AM |
2267 | (_("%B: invalid link %lu for reloc section %s (index %u)"), |
2268 | abfd, hdr->sh_link, name, shindex); | |
bf67003b NC |
2269 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, |
2270 | shindex); | |
2271 | goto success; | |
03ae5f59 ILT |
2272 | } |
2273 | ||
252b5132 RH |
2274 | /* For some incomprehensible reason Oracle distributes |
2275 | libraries for Solaris in which some of the objects have | |
2276 | bogus sh_link fields. It would be nice if we could just | |
2277 | reject them, but, unfortunately, some people need to use | |
2278 | them. We scan through the section headers; if we find only | |
2279 | one suitable symbol table, we clobber the sh_link to point | |
83b89087 L |
2280 | to it. I hope this doesn't break anything. |
2281 | ||
2282 | Don't do it on executable nor shared library. */ | |
2283 | if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0 | |
2284 | && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_SYMTAB | |
252b5132 RH |
2285 | && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_DYNSYM) |
2286 | { | |
9ad5cbcf | 2287 | unsigned int scan; |
252b5132 RH |
2288 | int found; |
2289 | ||
2290 | found = 0; | |
9ad5cbcf | 2291 | for (scan = 1; scan < num_sec; scan++) |
252b5132 RH |
2292 | { |
2293 | if (elf_elfsections (abfd)[scan]->sh_type == SHT_SYMTAB | |
2294 | || elf_elfsections (abfd)[scan]->sh_type == SHT_DYNSYM) | |
2295 | { | |
2296 | if (found != 0) | |
2297 | { | |
2298 | found = 0; | |
2299 | break; | |
2300 | } | |
2301 | found = scan; | |
2302 | } | |
2303 | } | |
2304 | if (found != 0) | |
2305 | hdr->sh_link = found; | |
2306 | } | |
2307 | ||
2308 | /* Get the symbol table. */ | |
1b3a8575 AM |
2309 | if ((elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_SYMTAB |
2310 | || elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_DYNSYM) | |
252b5132 | 2311 | && ! bfd_section_from_shdr (abfd, hdr->sh_link)) |
bf67003b | 2312 | goto fail; |
252b5132 RH |
2313 | |
2314 | /* If this reloc section does not use the main symbol table we | |
2315 | don't treat it as a reloc section. BFD can't adequately | |
2316 | represent such a section, so at least for now, we don't | |
c044fabd | 2317 | try. We just present it as a normal section. We also |
60bcf0fa | 2318 | can't use it as a reloc section if it points to the null |
83b89087 L |
2319 | section, an invalid section, another reloc section, or its |
2320 | sh_link points to the null section. */ | |
185ef66d | 2321 | if (hdr->sh_link != elf_onesymtab (abfd) |
83b89087 | 2322 | || hdr->sh_link == SHN_UNDEF |
185ef66d | 2323 | || hdr->sh_info == SHN_UNDEF |
185ef66d AM |
2324 | || hdr->sh_info >= num_sec |
2325 | || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_REL | |
2326 | || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_RELA) | |
bf67003b NC |
2327 | { |
2328 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, | |
2329 | shindex); | |
2330 | goto success; | |
2331 | } | |
252b5132 RH |
2332 | |
2333 | if (! bfd_section_from_shdr (abfd, hdr->sh_info)) | |
bf67003b NC |
2334 | goto fail; |
2335 | ||
252b5132 RH |
2336 | target_sect = bfd_section_from_elf_index (abfd, hdr->sh_info); |
2337 | if (target_sect == NULL) | |
bf67003b | 2338 | goto fail; |
252b5132 | 2339 | |
d4730f92 BS |
2340 | esdt = elf_section_data (target_sect); |
2341 | if (hdr->sh_type == SHT_RELA) | |
2342 | p_hdr = &esdt->rela.hdr; | |
252b5132 | 2343 | else |
d4730f92 BS |
2344 | p_hdr = &esdt->rel.hdr; |
2345 | ||
06614111 NC |
2346 | /* PR 17512: file: 0b4f81b7. */ |
2347 | if (*p_hdr != NULL) | |
2348 | goto fail; | |
ef53be89 | 2349 | hdr2 = (Elf_Internal_Shdr *) bfd_alloc (abfd, sizeof (*hdr2)); |
d4730f92 | 2350 | if (hdr2 == NULL) |
bf67003b | 2351 | goto fail; |
252b5132 | 2352 | *hdr2 = *hdr; |
d4730f92 | 2353 | *p_hdr = hdr2; |
252b5132 | 2354 | elf_elfsections (abfd)[shindex] = hdr2; |
d9bc7a44 | 2355 | target_sect->reloc_count += NUM_SHDR_ENTRIES (hdr); |
252b5132 RH |
2356 | target_sect->flags |= SEC_RELOC; |
2357 | target_sect->relocation = NULL; | |
2358 | target_sect->rel_filepos = hdr->sh_offset; | |
bf572ba0 MM |
2359 | /* In the section to which the relocations apply, mark whether |
2360 | its relocations are of the REL or RELA variety. */ | |
72730e0c | 2361 | if (hdr->sh_size != 0) |
d4730f92 BS |
2362 | { |
2363 | if (hdr->sh_type == SHT_RELA) | |
2364 | target_sect->use_rela_p = 1; | |
2365 | } | |
252b5132 | 2366 | abfd->flags |= HAS_RELOC; |
bf67003b | 2367 | goto success; |
252b5132 | 2368 | } |
252b5132 RH |
2369 | |
2370 | case SHT_GNU_verdef: | |
2371 | elf_dynverdef (abfd) = shindex; | |
2372 | elf_tdata (abfd)->dynverdef_hdr = *hdr; | |
bf67003b NC |
2373 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); |
2374 | goto success; | |
252b5132 RH |
2375 | |
2376 | case SHT_GNU_versym: | |
a50b2160 | 2377 | if (hdr->sh_entsize != sizeof (Elf_External_Versym)) |
bf67003b NC |
2378 | goto fail; |
2379 | ||
252b5132 RH |
2380 | elf_dynversym (abfd) = shindex; |
2381 | elf_tdata (abfd)->dynversym_hdr = *hdr; | |
bf67003b NC |
2382 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); |
2383 | goto success; | |
252b5132 RH |
2384 | |
2385 | case SHT_GNU_verneed: | |
2386 | elf_dynverref (abfd) = shindex; | |
2387 | elf_tdata (abfd)->dynverref_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_SHLIB: | |
bf67003b | 2392 | goto success; |
252b5132 | 2393 | |
dbb410c3 | 2394 | case SHT_GROUP: |
44534af3 | 2395 | if (! IS_VALID_GROUP_SECTION_HEADER (hdr, GRP_ENTRY_SIZE)) |
bf67003b NC |
2396 | goto fail; |
2397 | ||
6dc132d9 | 2398 | if (!_bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex)) |
bf67003b NC |
2399 | goto fail; |
2400 | ||
dbb410c3 AM |
2401 | if (hdr->contents != NULL) |
2402 | { | |
2403 | Elf_Internal_Group *idx = (Elf_Internal_Group *) hdr->contents; | |
06614111 | 2404 | unsigned int n_elt = hdr->sh_size / sizeof (* idx); |
dbb410c3 AM |
2405 | asection *s; |
2406 | ||
06614111 NC |
2407 | if (n_elt == 0) |
2408 | goto fail; | |
b885599b AM |
2409 | if (idx->flags & GRP_COMDAT) |
2410 | hdr->bfd_section->flags | |
2411 | |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD; | |
2412 | ||
45c5e9ed L |
2413 | /* We try to keep the same section order as it comes in. */ |
2414 | idx += n_elt; | |
06614111 | 2415 | |
dbb410c3 | 2416 | while (--n_elt != 0) |
1783205a NC |
2417 | { |
2418 | --idx; | |
2419 | ||
2420 | if (idx->shdr != NULL | |
2421 | && (s = idx->shdr->bfd_section) != NULL | |
2422 | && elf_next_in_group (s) != NULL) | |
2423 | { | |
2424 | elf_next_in_group (hdr->bfd_section) = s; | |
2425 | break; | |
2426 | } | |
2427 | } | |
dbb410c3 | 2428 | } |
bf67003b | 2429 | goto success; |
dbb410c3 | 2430 | |
252b5132 | 2431 | default: |
104d59d1 JM |
2432 | /* Possibly an attributes section. */ |
2433 | if (hdr->sh_type == SHT_GNU_ATTRIBUTES | |
2434 | || hdr->sh_type == bed->obj_attrs_section_type) | |
2435 | { | |
2436 | if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex)) | |
bf67003b | 2437 | goto fail; |
104d59d1 | 2438 | _bfd_elf_parse_attributes (abfd, hdr); |
bf67003b | 2439 | goto success; |
104d59d1 JM |
2440 | } |
2441 | ||
252b5132 | 2442 | /* Check for any processor-specific section types. */ |
3eb70a79 | 2443 | if (bed->elf_backend_section_from_shdr (abfd, hdr, name, shindex)) |
bf67003b | 2444 | goto success; |
3eb70a79 L |
2445 | |
2446 | if (hdr->sh_type >= SHT_LOUSER && hdr->sh_type <= SHT_HIUSER) | |
2447 | { | |
2448 | if ((hdr->sh_flags & SHF_ALLOC) != 0) | |
2449 | /* FIXME: How to properly handle allocated section reserved | |
2450 | for applications? */ | |
4eca0228 | 2451 | _bfd_error_handler |
695344c0 | 2452 | /* xgettext:c-format */ |
3eb70a79 L |
2453 | (_("%B: don't know how to handle allocated, application " |
2454 | "specific section `%s' [0x%8x]"), | |
2455 | abfd, name, hdr->sh_type); | |
2456 | else | |
bf67003b NC |
2457 | { |
2458 | /* Allow sections reserved for applications. */ | |
2459 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, | |
2460 | shindex); | |
2461 | goto success; | |
2462 | } | |
3eb70a79 L |
2463 | } |
2464 | else if (hdr->sh_type >= SHT_LOPROC | |
2465 | && hdr->sh_type <= SHT_HIPROC) | |
2466 | /* FIXME: We should handle this section. */ | |
4eca0228 | 2467 | _bfd_error_handler |
695344c0 | 2468 | /* xgettext:c-format */ |
3eb70a79 L |
2469 | (_("%B: don't know how to handle processor specific section " |
2470 | "`%s' [0x%8x]"), | |
2471 | abfd, name, hdr->sh_type); | |
2472 | else if (hdr->sh_type >= SHT_LOOS && hdr->sh_type <= SHT_HIOS) | |
ff15b240 NC |
2473 | { |
2474 | /* Unrecognised OS-specific sections. */ | |
2475 | if ((hdr->sh_flags & SHF_OS_NONCONFORMING) != 0) | |
2476 | /* SHF_OS_NONCONFORMING indicates that special knowledge is | |
08a40648 | 2477 | required to correctly process the section and the file should |
ff15b240 | 2478 | be rejected with an error message. */ |
4eca0228 | 2479 | _bfd_error_handler |
695344c0 | 2480 | /* xgettext:c-format */ |
ff15b240 NC |
2481 | (_("%B: don't know how to handle OS specific section " |
2482 | "`%s' [0x%8x]"), | |
2483 | abfd, name, hdr->sh_type); | |
2484 | else | |
bf67003b NC |
2485 | { |
2486 | /* Otherwise it should be processed. */ | |
2487 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); | |
2488 | goto success; | |
2489 | } | |
ff15b240 | 2490 | } |
3eb70a79 L |
2491 | else |
2492 | /* FIXME: We should handle this section. */ | |
4eca0228 | 2493 | _bfd_error_handler |
695344c0 | 2494 | /* xgettext:c-format */ |
3eb70a79 L |
2495 | (_("%B: don't know how to handle section `%s' [0x%8x]"), |
2496 | abfd, name, hdr->sh_type); | |
2497 | ||
bf67003b | 2498 | goto fail; |
252b5132 RH |
2499 | } |
2500 | ||
bf67003b NC |
2501 | fail: |
2502 | ret = FALSE; | |
2503 | success: | |
e5b470e2 | 2504 | if (sections_being_created && sections_being_created_abfd == abfd) |
bf67003b NC |
2505 | sections_being_created [shindex] = FALSE; |
2506 | if (-- nesting == 0) | |
5a4b0ccc NC |
2507 | { |
2508 | sections_being_created = NULL; | |
2509 | sections_being_created_abfd = abfd; | |
2510 | } | |
bf67003b | 2511 | return ret; |
252b5132 RH |
2512 | } |
2513 | ||
87d72d41 | 2514 | /* Return the local symbol specified by ABFD, R_SYMNDX. */ |
ec338859 | 2515 | |
87d72d41 AM |
2516 | Elf_Internal_Sym * |
2517 | bfd_sym_from_r_symndx (struct sym_cache *cache, | |
2518 | bfd *abfd, | |
2519 | unsigned long r_symndx) | |
ec338859 | 2520 | { |
ec338859 AM |
2521 | unsigned int ent = r_symndx % LOCAL_SYM_CACHE_SIZE; |
2522 | ||
a5d1b3b5 AM |
2523 | if (cache->abfd != abfd || cache->indx[ent] != r_symndx) |
2524 | { | |
2525 | Elf_Internal_Shdr *symtab_hdr; | |
2526 | unsigned char esym[sizeof (Elf64_External_Sym)]; | |
2527 | Elf_External_Sym_Shndx eshndx; | |
ec338859 | 2528 | |
a5d1b3b5 AM |
2529 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; |
2530 | if (bfd_elf_get_elf_syms (abfd, symtab_hdr, 1, r_symndx, | |
87d72d41 | 2531 | &cache->sym[ent], esym, &eshndx) == NULL) |
a5d1b3b5 | 2532 | return NULL; |
9ad5cbcf | 2533 | |
a5d1b3b5 AM |
2534 | if (cache->abfd != abfd) |
2535 | { | |
2536 | memset (cache->indx, -1, sizeof (cache->indx)); | |
2537 | cache->abfd = abfd; | |
2538 | } | |
2539 | cache->indx[ent] = r_symndx; | |
ec338859 | 2540 | } |
a5d1b3b5 | 2541 | |
87d72d41 | 2542 | return &cache->sym[ent]; |
ec338859 AM |
2543 | } |
2544 | ||
252b5132 RH |
2545 | /* Given an ELF section number, retrieve the corresponding BFD |
2546 | section. */ | |
2547 | ||
2548 | asection * | |
91d6fa6a | 2549 | bfd_section_from_elf_index (bfd *abfd, unsigned int sec_index) |
252b5132 | 2550 | { |
91d6fa6a | 2551 | if (sec_index >= elf_numsections (abfd)) |
252b5132 | 2552 | return NULL; |
91d6fa6a | 2553 | return elf_elfsections (abfd)[sec_index]->bfd_section; |
252b5132 RH |
2554 | } |
2555 | ||
b35d266b | 2556 | static const struct bfd_elf_special_section special_sections_b[] = |
2f89ff8d | 2557 | { |
0112cd26 NC |
2558 | { STRING_COMMA_LEN (".bss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE }, |
2559 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2560 | }; |
2561 | ||
b35d266b | 2562 | static const struct bfd_elf_special_section special_sections_c[] = |
7f4d3958 | 2563 | { |
0112cd26 NC |
2564 | { STRING_COMMA_LEN (".comment"), 0, SHT_PROGBITS, 0 }, |
2565 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2566 | }; |
2567 | ||
b35d266b | 2568 | static const struct bfd_elf_special_section special_sections_d[] = |
7f4d3958 | 2569 | { |
0112cd26 NC |
2570 | { STRING_COMMA_LEN (".data"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE }, |
2571 | { STRING_COMMA_LEN (".data1"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE }, | |
a9a72a65 DE |
2572 | /* There are more DWARF sections than these, but they needn't be added here |
2573 | unless you have to cope with broken compilers that don't emit section | |
2574 | attributes or you want to help the user writing assembler. */ | |
0112cd26 NC |
2575 | { STRING_COMMA_LEN (".debug"), 0, SHT_PROGBITS, 0 }, |
2576 | { STRING_COMMA_LEN (".debug_line"), 0, SHT_PROGBITS, 0 }, | |
2577 | { STRING_COMMA_LEN (".debug_info"), 0, SHT_PROGBITS, 0 }, | |
2578 | { STRING_COMMA_LEN (".debug_abbrev"), 0, SHT_PROGBITS, 0 }, | |
2579 | { STRING_COMMA_LEN (".debug_aranges"), 0, SHT_PROGBITS, 0 }, | |
2580 | { STRING_COMMA_LEN (".dynamic"), 0, SHT_DYNAMIC, SHF_ALLOC }, | |
2581 | { STRING_COMMA_LEN (".dynstr"), 0, SHT_STRTAB, SHF_ALLOC }, | |
2582 | { STRING_COMMA_LEN (".dynsym"), 0, SHT_DYNSYM, SHF_ALLOC }, | |
2583 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2584 | }; |
2585 | ||
b35d266b | 2586 | static const struct bfd_elf_special_section special_sections_f[] = |
7f4d3958 | 2587 | { |
0112cd26 NC |
2588 | { STRING_COMMA_LEN (".fini"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR }, |
2589 | { STRING_COMMA_LEN (".fini_array"), 0, SHT_FINI_ARRAY, SHF_ALLOC + SHF_WRITE }, | |
2590 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2591 | }; |
2592 | ||
b35d266b | 2593 | static const struct bfd_elf_special_section special_sections_g[] = |
7f4d3958 | 2594 | { |
0112cd26 | 2595 | { STRING_COMMA_LEN (".gnu.linkonce.b"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE }, |
68efed41 | 2596 | { STRING_COMMA_LEN (".gnu.lto_"), -1, SHT_PROGBITS, SHF_EXCLUDE }, |
0112cd26 NC |
2597 | { STRING_COMMA_LEN (".got"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE }, |
2598 | { STRING_COMMA_LEN (".gnu.version"), 0, SHT_GNU_versym, 0 }, | |
2599 | { STRING_COMMA_LEN (".gnu.version_d"), 0, SHT_GNU_verdef, 0 }, | |
2600 | { STRING_COMMA_LEN (".gnu.version_r"), 0, SHT_GNU_verneed, 0 }, | |
2601 | { STRING_COMMA_LEN (".gnu.liblist"), 0, SHT_GNU_LIBLIST, SHF_ALLOC }, | |
2602 | { STRING_COMMA_LEN (".gnu.conflict"), 0, SHT_RELA, SHF_ALLOC }, | |
2603 | { STRING_COMMA_LEN (".gnu.hash"), 0, SHT_GNU_HASH, SHF_ALLOC }, | |
2604 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2605 | }; |
2606 | ||
b35d266b | 2607 | static const struct bfd_elf_special_section special_sections_h[] = |
7f4d3958 | 2608 | { |
0112cd26 NC |
2609 | { STRING_COMMA_LEN (".hash"), 0, SHT_HASH, SHF_ALLOC }, |
2610 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2611 | }; |
2612 | ||
b35d266b | 2613 | static const struct bfd_elf_special_section special_sections_i[] = |
7f4d3958 | 2614 | { |
0112cd26 NC |
2615 | { STRING_COMMA_LEN (".init"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR }, |
2616 | { STRING_COMMA_LEN (".init_array"), 0, SHT_INIT_ARRAY, SHF_ALLOC + SHF_WRITE }, | |
2617 | { STRING_COMMA_LEN (".interp"), 0, SHT_PROGBITS, 0 }, | |
2618 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2619 | }; |
2620 | ||
b35d266b | 2621 | static const struct bfd_elf_special_section special_sections_l[] = |
7f4d3958 | 2622 | { |
0112cd26 NC |
2623 | { STRING_COMMA_LEN (".line"), 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_n[] = |
7f4d3958 | 2628 | { |
0112cd26 NC |
2629 | { STRING_COMMA_LEN (".note.GNU-stack"), 0, SHT_PROGBITS, 0 }, |
2630 | { STRING_COMMA_LEN (".note"), -1, SHT_NOTE, 0 }, | |
2631 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2632 | }; |
2633 | ||
b35d266b | 2634 | static const struct bfd_elf_special_section special_sections_p[] = |
7f4d3958 | 2635 | { |
0112cd26 NC |
2636 | { STRING_COMMA_LEN (".preinit_array"), 0, SHT_PREINIT_ARRAY, SHF_ALLOC + SHF_WRITE }, |
2637 | { STRING_COMMA_LEN (".plt"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR }, | |
2638 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2639 | }; |
2640 | ||
b35d266b | 2641 | static const struct bfd_elf_special_section special_sections_r[] = |
7f4d3958 | 2642 | { |
0112cd26 NC |
2643 | { STRING_COMMA_LEN (".rodata"), -2, SHT_PROGBITS, SHF_ALLOC }, |
2644 | { STRING_COMMA_LEN (".rodata1"), 0, SHT_PROGBITS, SHF_ALLOC }, | |
2645 | { STRING_COMMA_LEN (".rela"), -1, SHT_RELA, 0 }, | |
2646 | { STRING_COMMA_LEN (".rel"), -1, SHT_REL, 0 }, | |
2647 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2648 | }; |
2649 | ||
b35d266b | 2650 | static const struct bfd_elf_special_section special_sections_s[] = |
7f4d3958 | 2651 | { |
0112cd26 NC |
2652 | { STRING_COMMA_LEN (".shstrtab"), 0, SHT_STRTAB, 0 }, |
2653 | { STRING_COMMA_LEN (".strtab"), 0, SHT_STRTAB, 0 }, | |
2654 | { STRING_COMMA_LEN (".symtab"), 0, SHT_SYMTAB, 0 }, | |
60ff4dc4 HPN |
2655 | /* See struct bfd_elf_special_section declaration for the semantics of |
2656 | this special case where .prefix_length != strlen (.prefix). */ | |
2657 | { ".stabstr", 5, 3, SHT_STRTAB, 0 }, | |
0112cd26 | 2658 | { NULL, 0, 0, 0, 0 } |
2f89ff8d L |
2659 | }; |
2660 | ||
b35d266b | 2661 | static const struct bfd_elf_special_section special_sections_t[] = |
7f4d3958 | 2662 | { |
0112cd26 NC |
2663 | { STRING_COMMA_LEN (".text"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR }, |
2664 | { STRING_COMMA_LEN (".tbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS }, | |
2665 | { STRING_COMMA_LEN (".tdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS }, | |
2666 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2667 | }; |
2668 | ||
1b315056 CS |
2669 | static const struct bfd_elf_special_section special_sections_z[] = |
2670 | { | |
2671 | { STRING_COMMA_LEN (".zdebug_line"), 0, SHT_PROGBITS, 0 }, | |
2672 | { STRING_COMMA_LEN (".zdebug_info"), 0, SHT_PROGBITS, 0 }, | |
2673 | { STRING_COMMA_LEN (".zdebug_abbrev"), 0, SHT_PROGBITS, 0 }, | |
2674 | { STRING_COMMA_LEN (".zdebug_aranges"), 0, SHT_PROGBITS, 0 }, | |
2675 | { NULL, 0, 0, 0, 0 } | |
2676 | }; | |
2677 | ||
e4c93b56 | 2678 | static const struct bfd_elf_special_section * const special_sections[] = |
7f4d3958 | 2679 | { |
7f4d3958 | 2680 | special_sections_b, /* 'b' */ |
98ece1b3 | 2681 | special_sections_c, /* 'c' */ |
7f4d3958 L |
2682 | special_sections_d, /* 'd' */ |
2683 | NULL, /* 'e' */ | |
2684 | special_sections_f, /* 'f' */ | |
2685 | special_sections_g, /* 'g' */ | |
2686 | special_sections_h, /* 'h' */ | |
2687 | special_sections_i, /* 'i' */ | |
2688 | NULL, /* 'j' */ | |
2689 | NULL, /* 'k' */ | |
2690 | special_sections_l, /* 'l' */ | |
2691 | NULL, /* 'm' */ | |
2692 | special_sections_n, /* 'n' */ | |
2693 | NULL, /* 'o' */ | |
2694 | special_sections_p, /* 'p' */ | |
2695 | NULL, /* 'q' */ | |
2696 | special_sections_r, /* 'r' */ | |
2697 | special_sections_s, /* 's' */ | |
2698 | special_sections_t, /* 't' */ | |
1b315056 CS |
2699 | NULL, /* 'u' */ |
2700 | NULL, /* 'v' */ | |
2701 | NULL, /* 'w' */ | |
2702 | NULL, /* 'x' */ | |
2703 | NULL, /* 'y' */ | |
2704 | special_sections_z /* 'z' */ | |
7f4d3958 L |
2705 | }; |
2706 | ||
551b43fd AM |
2707 | const struct bfd_elf_special_section * |
2708 | _bfd_elf_get_special_section (const char *name, | |
2709 | const struct bfd_elf_special_section *spec, | |
2710 | unsigned int rela) | |
2f89ff8d L |
2711 | { |
2712 | int i; | |
7f4d3958 | 2713 | int len; |
7f4d3958 | 2714 | |
551b43fd | 2715 | len = strlen (name); |
7f4d3958 | 2716 | |
551b43fd | 2717 | for (i = 0; spec[i].prefix != NULL; i++) |
7dcb9820 AM |
2718 | { |
2719 | int suffix_len; | |
551b43fd | 2720 | int prefix_len = spec[i].prefix_length; |
7dcb9820 AM |
2721 | |
2722 | if (len < prefix_len) | |
2723 | continue; | |
551b43fd | 2724 | if (memcmp (name, spec[i].prefix, prefix_len) != 0) |
7dcb9820 AM |
2725 | continue; |
2726 | ||
551b43fd | 2727 | suffix_len = spec[i].suffix_length; |
7dcb9820 AM |
2728 | if (suffix_len <= 0) |
2729 | { | |
2730 | if (name[prefix_len] != 0) | |
2731 | { | |
2732 | if (suffix_len == 0) | |
2733 | continue; | |
2734 | if (name[prefix_len] != '.' | |
2735 | && (suffix_len == -2 | |
551b43fd | 2736 | || (rela && spec[i].type == SHT_REL))) |
7dcb9820 AM |
2737 | continue; |
2738 | } | |
2739 | } | |
2740 | else | |
2741 | { | |
2742 | if (len < prefix_len + suffix_len) | |
2743 | continue; | |
2744 | if (memcmp (name + len - suffix_len, | |
551b43fd | 2745 | spec[i].prefix + prefix_len, |
7dcb9820 AM |
2746 | suffix_len) != 0) |
2747 | continue; | |
2748 | } | |
551b43fd | 2749 | return &spec[i]; |
7dcb9820 | 2750 | } |
2f89ff8d L |
2751 | |
2752 | return NULL; | |
2753 | } | |
2754 | ||
7dcb9820 | 2755 | const struct bfd_elf_special_section * |
29ef7005 | 2756 | _bfd_elf_get_sec_type_attr (bfd *abfd, asection *sec) |
2f89ff8d | 2757 | { |
551b43fd AM |
2758 | int i; |
2759 | const struct bfd_elf_special_section *spec; | |
29ef7005 | 2760 | const struct elf_backend_data *bed; |
2f89ff8d L |
2761 | |
2762 | /* See if this is one of the special sections. */ | |
551b43fd AM |
2763 | if (sec->name == NULL) |
2764 | return NULL; | |
2f89ff8d | 2765 | |
29ef7005 L |
2766 | bed = get_elf_backend_data (abfd); |
2767 | spec = bed->special_sections; | |
2768 | if (spec) | |
2769 | { | |
2770 | spec = _bfd_elf_get_special_section (sec->name, | |
2771 | bed->special_sections, | |
2772 | sec->use_rela_p); | |
2773 | if (spec != NULL) | |
2774 | return spec; | |
2775 | } | |
2776 | ||
551b43fd AM |
2777 | if (sec->name[0] != '.') |
2778 | return NULL; | |
2f89ff8d | 2779 | |
551b43fd | 2780 | i = sec->name[1] - 'b'; |
1b315056 | 2781 | if (i < 0 || i > 'z' - 'b') |
551b43fd AM |
2782 | return NULL; |
2783 | ||
2784 | spec = special_sections[i]; | |
2f89ff8d | 2785 | |
551b43fd AM |
2786 | if (spec == NULL) |
2787 | return NULL; | |
2788 | ||
2789 | return _bfd_elf_get_special_section (sec->name, spec, sec->use_rela_p); | |
2f89ff8d L |
2790 | } |
2791 | ||
b34976b6 | 2792 | bfd_boolean |
217aa764 | 2793 | _bfd_elf_new_section_hook (bfd *abfd, asection *sec) |
252b5132 RH |
2794 | { |
2795 | struct bfd_elf_section_data *sdata; | |
551b43fd | 2796 | const struct elf_backend_data *bed; |
7dcb9820 | 2797 | const struct bfd_elf_special_section *ssect; |
252b5132 | 2798 | |
f0abc2a1 AM |
2799 | sdata = (struct bfd_elf_section_data *) sec->used_by_bfd; |
2800 | if (sdata == NULL) | |
2801 | { | |
a50b1753 NC |
2802 | sdata = (struct bfd_elf_section_data *) bfd_zalloc (abfd, |
2803 | sizeof (*sdata)); | |
f0abc2a1 AM |
2804 | if (sdata == NULL) |
2805 | return FALSE; | |
217aa764 | 2806 | sec->used_by_bfd = sdata; |
f0abc2a1 | 2807 | } |
bf572ba0 | 2808 | |
551b43fd AM |
2809 | /* Indicate whether or not this section should use RELA relocations. */ |
2810 | bed = get_elf_backend_data (abfd); | |
2811 | sec->use_rela_p = bed->default_use_rela_p; | |
2812 | ||
e843e0f8 L |
2813 | /* When we read a file, we don't need to set ELF section type and |
2814 | flags. They will be overridden in _bfd_elf_make_section_from_shdr | |
2815 | anyway. We will set ELF section type and flags for all linker | |
2816 | created sections. If user specifies BFD section flags, we will | |
2817 | set ELF section type and flags based on BFD section flags in | |
02ecc8e9 L |
2818 | elf_fake_sections. Special handling for .init_array/.fini_array |
2819 | output sections since they may contain .ctors/.dtors input | |
2820 | sections. We don't want _bfd_elf_init_private_section_data to | |
2821 | copy ELF section type from .ctors/.dtors input sections. */ | |
2822 | if (abfd->direction != read_direction | |
3496cb2a | 2823 | || (sec->flags & SEC_LINKER_CREATED) != 0) |
2f89ff8d | 2824 | { |
551b43fd | 2825 | ssect = (*bed->get_sec_type_attr) (abfd, sec); |
02ecc8e9 L |
2826 | if (ssect != NULL |
2827 | && (!sec->flags | |
2828 | || (sec->flags & SEC_LINKER_CREATED) != 0 | |
2829 | || ssect->type == SHT_INIT_ARRAY | |
2830 | || ssect->type == SHT_FINI_ARRAY)) | |
a31501e9 L |
2831 | { |
2832 | elf_section_type (sec) = ssect->type; | |
2833 | elf_section_flags (sec) = ssect->attr; | |
2834 | } | |
2f89ff8d L |
2835 | } |
2836 | ||
f592407e | 2837 | return _bfd_generic_new_section_hook (abfd, sec); |
252b5132 RH |
2838 | } |
2839 | ||
2840 | /* Create a new bfd section from an ELF program header. | |
2841 | ||
2842 | Since program segments have no names, we generate a synthetic name | |
2843 | of the form segment<NUM>, where NUM is generally the index in the | |
2844 | program header table. For segments that are split (see below) we | |
2845 | generate the names segment<NUM>a and segment<NUM>b. | |
2846 | ||
2847 | Note that some program segments may have a file size that is different than | |
2848 | (less than) the memory size. All this means is that at execution the | |
2849 | system must allocate the amount of memory specified by the memory size, | |
2850 | but only initialize it with the first "file size" bytes read from the | |
2851 | file. This would occur for example, with program segments consisting | |
2852 | of combined data+bss. | |
2853 | ||
2854 | To handle the above situation, this routine generates TWO bfd sections | |
2855 | for the single program segment. The first has the length specified by | |
2856 | the file size of the segment, and the second has the length specified | |
2857 | by the difference between the two sizes. In effect, the segment is split | |
d5191d0c | 2858 | into its initialized and uninitialized parts. |
252b5132 RH |
2859 | |
2860 | */ | |
2861 | ||
b34976b6 | 2862 | bfd_boolean |
217aa764 AM |
2863 | _bfd_elf_make_section_from_phdr (bfd *abfd, |
2864 | Elf_Internal_Phdr *hdr, | |
91d6fa6a | 2865 | int hdr_index, |
a50b1753 | 2866 | const char *type_name) |
252b5132 RH |
2867 | { |
2868 | asection *newsect; | |
2869 | char *name; | |
2870 | char namebuf[64]; | |
d4c88bbb | 2871 | size_t len; |
252b5132 RH |
2872 | int split; |
2873 | ||
2874 | split = ((hdr->p_memsz > 0) | |
2875 | && (hdr->p_filesz > 0) | |
2876 | && (hdr->p_memsz > hdr->p_filesz)); | |
d5191d0c AM |
2877 | |
2878 | if (hdr->p_filesz > 0) | |
252b5132 | 2879 | { |
91d6fa6a | 2880 | sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "a" : ""); |
d5191d0c | 2881 | len = strlen (namebuf) + 1; |
a50b1753 | 2882 | name = (char *) bfd_alloc (abfd, len); |
d5191d0c AM |
2883 | if (!name) |
2884 | return FALSE; | |
2885 | memcpy (name, namebuf, len); | |
2886 | newsect = bfd_make_section (abfd, name); | |
2887 | if (newsect == NULL) | |
2888 | return FALSE; | |
2889 | newsect->vma = hdr->p_vaddr; | |
2890 | newsect->lma = hdr->p_paddr; | |
2891 | newsect->size = hdr->p_filesz; | |
2892 | newsect->filepos = hdr->p_offset; | |
2893 | newsect->flags |= SEC_HAS_CONTENTS; | |
2894 | newsect->alignment_power = bfd_log2 (hdr->p_align); | |
2895 | if (hdr->p_type == PT_LOAD) | |
252b5132 | 2896 | { |
d5191d0c AM |
2897 | newsect->flags |= SEC_ALLOC; |
2898 | newsect->flags |= SEC_LOAD; | |
2899 | if (hdr->p_flags & PF_X) | |
2900 | { | |
2901 | /* FIXME: all we known is that it has execute PERMISSION, | |
2902 | may be data. */ | |
2903 | newsect->flags |= SEC_CODE; | |
2904 | } | |
2905 | } | |
2906 | if (!(hdr->p_flags & PF_W)) | |
2907 | { | |
2908 | newsect->flags |= SEC_READONLY; | |
252b5132 | 2909 | } |
252b5132 RH |
2910 | } |
2911 | ||
d5191d0c | 2912 | if (hdr->p_memsz > hdr->p_filesz) |
252b5132 | 2913 | { |
d5191d0c AM |
2914 | bfd_vma align; |
2915 | ||
91d6fa6a | 2916 | sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "b" : ""); |
d4c88bbb | 2917 | len = strlen (namebuf) + 1; |
a50b1753 | 2918 | name = (char *) bfd_alloc (abfd, len); |
252b5132 | 2919 | if (!name) |
b34976b6 | 2920 | return FALSE; |
d4c88bbb | 2921 | memcpy (name, namebuf, len); |
252b5132 RH |
2922 | newsect = bfd_make_section (abfd, name); |
2923 | if (newsect == NULL) | |
b34976b6 | 2924 | return FALSE; |
252b5132 RH |
2925 | newsect->vma = hdr->p_vaddr + hdr->p_filesz; |
2926 | newsect->lma = hdr->p_paddr + hdr->p_filesz; | |
eea6121a | 2927 | newsect->size = hdr->p_memsz - hdr->p_filesz; |
d5191d0c AM |
2928 | newsect->filepos = hdr->p_offset + hdr->p_filesz; |
2929 | align = newsect->vma & -newsect->vma; | |
2930 | if (align == 0 || align > hdr->p_align) | |
2931 | align = hdr->p_align; | |
2932 | newsect->alignment_power = bfd_log2 (align); | |
252b5132 RH |
2933 | if (hdr->p_type == PT_LOAD) |
2934 | { | |
d5191d0c AM |
2935 | /* Hack for gdb. Segments that have not been modified do |
2936 | not have their contents written to a core file, on the | |
2937 | assumption that a debugger can find the contents in the | |
2938 | executable. We flag this case by setting the fake | |
2939 | section size to zero. Note that "real" bss sections will | |
2940 | always have their contents dumped to the core file. */ | |
2941 | if (bfd_get_format (abfd) == bfd_core) | |
2942 | newsect->size = 0; | |
252b5132 RH |
2943 | newsect->flags |= SEC_ALLOC; |
2944 | if (hdr->p_flags & PF_X) | |
2945 | newsect->flags |= SEC_CODE; | |
2946 | } | |
2947 | if (!(hdr->p_flags & PF_W)) | |
2948 | newsect->flags |= SEC_READONLY; | |
2949 | } | |
2950 | ||
b34976b6 | 2951 | return TRUE; |
252b5132 RH |
2952 | } |
2953 | ||
b34976b6 | 2954 | bfd_boolean |
91d6fa6a | 2955 | bfd_section_from_phdr (bfd *abfd, Elf_Internal_Phdr *hdr, int hdr_index) |
20cfcaae | 2956 | { |
9c5bfbb7 | 2957 | const struct elf_backend_data *bed; |
20cfcaae NC |
2958 | |
2959 | switch (hdr->p_type) | |
2960 | { | |
2961 | case PT_NULL: | |
91d6fa6a | 2962 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "null"); |
20cfcaae NC |
2963 | |
2964 | case PT_LOAD: | |
91d6fa6a | 2965 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "load"); |
20cfcaae NC |
2966 | |
2967 | case PT_DYNAMIC: | |
91d6fa6a | 2968 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "dynamic"); |
20cfcaae NC |
2969 | |
2970 | case PT_INTERP: | |
91d6fa6a | 2971 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "interp"); |
20cfcaae NC |
2972 | |
2973 | case PT_NOTE: | |
91d6fa6a | 2974 | if (! _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "note")) |
b34976b6 | 2975 | return FALSE; |
718175fa | 2976 | if (! elf_read_notes (abfd, hdr->p_offset, hdr->p_filesz)) |
b34976b6 AM |
2977 | return FALSE; |
2978 | return TRUE; | |
20cfcaae NC |
2979 | |
2980 | case PT_SHLIB: | |
91d6fa6a | 2981 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "shlib"); |
20cfcaae NC |
2982 | |
2983 | case PT_PHDR: | |
91d6fa6a | 2984 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "phdr"); |
20cfcaae | 2985 | |
811072d8 | 2986 | case PT_GNU_EH_FRAME: |
91d6fa6a | 2987 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, |
811072d8 RM |
2988 | "eh_frame_hdr"); |
2989 | ||
2b05f1b7 | 2990 | case PT_GNU_STACK: |
91d6fa6a | 2991 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "stack"); |
9ee5e499 | 2992 | |
8c37241b | 2993 | case PT_GNU_RELRO: |
91d6fa6a | 2994 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "relro"); |
8c37241b | 2995 | |
20cfcaae | 2996 | default: |
8c1acd09 | 2997 | /* Check for any processor-specific program segment types. */ |
20cfcaae | 2998 | bed = get_elf_backend_data (abfd); |
91d6fa6a | 2999 | return bed->elf_backend_section_from_phdr (abfd, hdr, hdr_index, "proc"); |
20cfcaae NC |
3000 | } |
3001 | } | |
3002 | ||
d4730f92 BS |
3003 | /* Return the REL_HDR for SEC, assuming there is only a single one, either |
3004 | REL or RELA. */ | |
3005 | ||
3006 | Elf_Internal_Shdr * | |
3007 | _bfd_elf_single_rel_hdr (asection *sec) | |
3008 | { | |
3009 | if (elf_section_data (sec)->rel.hdr) | |
3010 | { | |
3011 | BFD_ASSERT (elf_section_data (sec)->rela.hdr == NULL); | |
3012 | return elf_section_data (sec)->rel.hdr; | |
3013 | } | |
3014 | else | |
3015 | return elf_section_data (sec)->rela.hdr; | |
3016 | } | |
3017 | ||
3e19fb8f L |
3018 | static bfd_boolean |
3019 | _bfd_elf_set_reloc_sh_name (bfd *abfd, | |
3020 | Elf_Internal_Shdr *rel_hdr, | |
3021 | const char *sec_name, | |
3022 | bfd_boolean use_rela_p) | |
3023 | { | |
3024 | char *name = (char *) bfd_alloc (abfd, | |
3025 | sizeof ".rela" + strlen (sec_name)); | |
3026 | if (name == NULL) | |
3027 | return FALSE; | |
3028 | ||
3029 | sprintf (name, "%s%s", use_rela_p ? ".rela" : ".rel", sec_name); | |
3030 | rel_hdr->sh_name = | |
3031 | (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), name, | |
3032 | FALSE); | |
3033 | if (rel_hdr->sh_name == (unsigned int) -1) | |
3034 | return FALSE; | |
3035 | ||
3036 | return TRUE; | |
3037 | } | |
3038 | ||
d4730f92 BS |
3039 | /* Allocate and initialize a section-header for a new reloc section, |
3040 | containing relocations against ASECT. It is stored in RELDATA. If | |
3041 | USE_RELA_P is TRUE, we use RELA relocations; otherwise, we use REL | |
3042 | relocations. */ | |
23bc299b | 3043 | |
5d13b3b3 | 3044 | static bfd_boolean |
217aa764 | 3045 | _bfd_elf_init_reloc_shdr (bfd *abfd, |
d4730f92 | 3046 | struct bfd_elf_section_reloc_data *reldata, |
f6fe1ccd | 3047 | const char *sec_name, |
3e19fb8f L |
3048 | bfd_boolean use_rela_p, |
3049 | bfd_boolean delay_st_name_p) | |
23bc299b | 3050 | { |
d4730f92 | 3051 | Elf_Internal_Shdr *rel_hdr; |
9c5bfbb7 | 3052 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
d4730f92 | 3053 | |
d4730f92 | 3054 | BFD_ASSERT (reldata->hdr == NULL); |
ef53be89 | 3055 | rel_hdr = bfd_zalloc (abfd, sizeof (*rel_hdr)); |
d4730f92 | 3056 | reldata->hdr = rel_hdr; |
23bc299b | 3057 | |
3e19fb8f L |
3058 | if (delay_st_name_p) |
3059 | rel_hdr->sh_name = (unsigned int) -1; | |
3060 | else if (!_bfd_elf_set_reloc_sh_name (abfd, rel_hdr, sec_name, | |
3061 | use_rela_p)) | |
b34976b6 | 3062 | return FALSE; |
23bc299b MM |
3063 | rel_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL; |
3064 | rel_hdr->sh_entsize = (use_rela_p | |
3065 | ? bed->s->sizeof_rela | |
3066 | : bed->s->sizeof_rel); | |
72de5009 | 3067 | rel_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align; |
23bc299b MM |
3068 | rel_hdr->sh_flags = 0; |
3069 | rel_hdr->sh_addr = 0; | |
3070 | rel_hdr->sh_size = 0; | |
3071 | rel_hdr->sh_offset = 0; | |
3072 | ||
b34976b6 | 3073 | return TRUE; |
23bc299b MM |
3074 | } |
3075 | ||
94be91de JB |
3076 | /* Return the default section type based on the passed in section flags. */ |
3077 | ||
3078 | int | |
3079 | bfd_elf_get_default_section_type (flagword flags) | |
3080 | { | |
3081 | if ((flags & SEC_ALLOC) != 0 | |
2e76e85a | 3082 | && (flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0) |
94be91de JB |
3083 | return SHT_NOBITS; |
3084 | return SHT_PROGBITS; | |
3085 | } | |
3086 | ||
d4730f92 BS |
3087 | struct fake_section_arg |
3088 | { | |
3089 | struct bfd_link_info *link_info; | |
3090 | bfd_boolean failed; | |
3091 | }; | |
3092 | ||
252b5132 RH |
3093 | /* Set up an ELF internal section header for a section. */ |
3094 | ||
252b5132 | 3095 | static void |
d4730f92 | 3096 | elf_fake_sections (bfd *abfd, asection *asect, void *fsarg) |
252b5132 | 3097 | { |
d4730f92 | 3098 | struct fake_section_arg *arg = (struct fake_section_arg *)fsarg; |
9c5bfbb7 | 3099 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
d4730f92 | 3100 | struct bfd_elf_section_data *esd = elf_section_data (asect); |
252b5132 | 3101 | Elf_Internal_Shdr *this_hdr; |
0414f35b | 3102 | unsigned int sh_type; |
0ce398f1 | 3103 | const char *name = asect->name; |
3e19fb8f | 3104 | bfd_boolean delay_st_name_p = FALSE; |
252b5132 | 3105 | |
d4730f92 | 3106 | if (arg->failed) |
252b5132 RH |
3107 | { |
3108 | /* We already failed; just get out of the bfd_map_over_sections | |
08a40648 | 3109 | loop. */ |
252b5132 RH |
3110 | return; |
3111 | } | |
3112 | ||
d4730f92 | 3113 | this_hdr = &esd->this_hdr; |
252b5132 | 3114 | |
f6fe1ccd | 3115 | if (arg->link_info) |
0ce398f1 | 3116 | { |
f6fe1ccd L |
3117 | /* ld: compress DWARF debug sections with names: .debug_*. */ |
3118 | if ((arg->link_info->compress_debug & COMPRESS_DEBUG) | |
3119 | && (asect->flags & SEC_DEBUGGING) | |
3120 | && name[1] == 'd' | |
3121 | && name[6] == '_') | |
3122 | { | |
3123 | /* Set SEC_ELF_COMPRESS to indicate this section should be | |
3124 | compressed. */ | |
3125 | asect->flags |= SEC_ELF_COMPRESS; | |
0ce398f1 | 3126 | |
dd905818 | 3127 | /* If this section will be compressed, delay adding section |
3e19fb8f L |
3128 | name to section name section after it is compressed in |
3129 | _bfd_elf_assign_file_positions_for_non_load. */ | |
3130 | delay_st_name_p = TRUE; | |
f6fe1ccd L |
3131 | } |
3132 | } | |
3133 | else if ((asect->flags & SEC_ELF_RENAME)) | |
3134 | { | |
3135 | /* objcopy: rename output DWARF debug section. */ | |
3136 | if ((abfd->flags & (BFD_DECOMPRESS | BFD_COMPRESS_GABI))) | |
3137 | { | |
3138 | /* When we decompress or compress with SHF_COMPRESSED, | |
3139 | convert section name from .zdebug_* to .debug_* if | |
3140 | needed. */ | |
3141 | if (name[1] == 'z') | |
3142 | { | |
3143 | char *new_name = convert_zdebug_to_debug (abfd, name); | |
3144 | if (new_name == NULL) | |
3145 | { | |
3146 | arg->failed = TRUE; | |
3147 | return; | |
3148 | } | |
3149 | name = new_name; | |
3150 | } | |
3151 | } | |
3152 | else if (asect->compress_status == COMPRESS_SECTION_DONE) | |
0ce398f1 | 3153 | { |
f6fe1ccd L |
3154 | /* PR binutils/18087: Compression does not always make a |
3155 | section smaller. So only rename the section when | |
3156 | compression has actually taken place. If input section | |
3157 | name is .zdebug_*, we should never compress it again. */ | |
3158 | char *new_name = convert_debug_to_zdebug (abfd, name); | |
0ce398f1 L |
3159 | if (new_name == NULL) |
3160 | { | |
3161 | arg->failed = TRUE; | |
3162 | return; | |
3163 | } | |
f6fe1ccd L |
3164 | BFD_ASSERT (name[1] != 'z'); |
3165 | name = new_name; | |
0ce398f1 L |
3166 | } |
3167 | } | |
3168 | ||
3e19fb8f L |
3169 | if (delay_st_name_p) |
3170 | this_hdr->sh_name = (unsigned int) -1; | |
3171 | else | |
252b5132 | 3172 | { |
3e19fb8f L |
3173 | this_hdr->sh_name |
3174 | = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), | |
3175 | name, FALSE); | |
3176 | if (this_hdr->sh_name == (unsigned int) -1) | |
3177 | { | |
3178 | arg->failed = TRUE; | |
3179 | return; | |
3180 | } | |
252b5132 RH |
3181 | } |
3182 | ||
a4d8e49b | 3183 | /* Don't clear sh_flags. Assembler may set additional bits. */ |
252b5132 RH |
3184 | |
3185 | if ((asect->flags & SEC_ALLOC) != 0 | |
3186 | || asect->user_set_vma) | |
3187 | this_hdr->sh_addr = asect->vma; | |
3188 | else | |
3189 | this_hdr->sh_addr = 0; | |
3190 | ||
3191 | this_hdr->sh_offset = 0; | |
eea6121a | 3192 | this_hdr->sh_size = asect->size; |
252b5132 | 3193 | this_hdr->sh_link = 0; |
c86934ce NC |
3194 | /* PR 17512: file: 0eb809fe, 8b0535ee. */ |
3195 | if (asect->alignment_power >= (sizeof (bfd_vma) * 8) - 1) | |
3196 | { | |
4eca0228 | 3197 | _bfd_error_handler |
695344c0 | 3198 | /* xgettext:c-format */ |
c86934ce NC |
3199 | (_("%B: error: Alignment power %d of section `%A' is too big"), |
3200 | abfd, asect, asect->alignment_power); | |
3201 | arg->failed = TRUE; | |
3202 | return; | |
3203 | } | |
72de5009 | 3204 | this_hdr->sh_addralign = (bfd_vma) 1 << asect->alignment_power; |
252b5132 RH |
3205 | /* The sh_entsize and sh_info fields may have been set already by |
3206 | copy_private_section_data. */ | |
3207 | ||
3208 | this_hdr->bfd_section = asect; | |
3209 | this_hdr->contents = NULL; | |
3210 | ||
3cddba1e L |
3211 | /* If the section type is unspecified, we set it based on |
3212 | asect->flags. */ | |
98ece1b3 AM |
3213 | if ((asect->flags & SEC_GROUP) != 0) |
3214 | sh_type = SHT_GROUP; | |
98ece1b3 | 3215 | else |
94be91de | 3216 | sh_type = bfd_elf_get_default_section_type (asect->flags); |
98ece1b3 | 3217 | |
3cddba1e | 3218 | if (this_hdr->sh_type == SHT_NULL) |
98ece1b3 AM |
3219 | this_hdr->sh_type = sh_type; |
3220 | else if (this_hdr->sh_type == SHT_NOBITS | |
3221 | && sh_type == SHT_PROGBITS | |
3222 | && (asect->flags & SEC_ALLOC) != 0) | |
3cddba1e | 3223 | { |
98ece1b3 AM |
3224 | /* Warn if we are changing a NOBITS section to PROGBITS, but |
3225 | allow the link to proceed. This can happen when users link | |
3226 | non-bss input sections to bss output sections, or emit data | |
3227 | to a bss output section via a linker script. */ | |
4eca0228 | 3228 | _bfd_error_handler |
58f0869b | 3229 | (_("warning: section `%A' type changed to PROGBITS"), asect); |
98ece1b3 | 3230 | this_hdr->sh_type = sh_type; |
3cddba1e L |
3231 | } |
3232 | ||
2f89ff8d | 3233 | switch (this_hdr->sh_type) |
252b5132 | 3234 | { |
2f89ff8d | 3235 | default: |
2f89ff8d L |
3236 | break; |
3237 | ||
3238 | case SHT_STRTAB: | |
2f89ff8d L |
3239 | case SHT_NOTE: |
3240 | case SHT_NOBITS: | |
3241 | case SHT_PROGBITS: | |
3242 | break; | |
606851fb AM |
3243 | |
3244 | case SHT_INIT_ARRAY: | |
3245 | case SHT_FINI_ARRAY: | |
3246 | case SHT_PREINIT_ARRAY: | |
3247 | this_hdr->sh_entsize = bed->s->arch_size / 8; | |
3248 | break; | |
2f89ff8d L |
3249 | |
3250 | case SHT_HASH: | |
c7ac6ff8 | 3251 | this_hdr->sh_entsize = bed->s->sizeof_hash_entry; |
2f89ff8d | 3252 | break; |
5de3bf90 | 3253 | |
2f89ff8d | 3254 | case SHT_DYNSYM: |
252b5132 | 3255 | this_hdr->sh_entsize = bed->s->sizeof_sym; |
2f89ff8d L |
3256 | break; |
3257 | ||
3258 | case SHT_DYNAMIC: | |
252b5132 | 3259 | this_hdr->sh_entsize = bed->s->sizeof_dyn; |
2f89ff8d L |
3260 | break; |
3261 | ||
3262 | case SHT_RELA: | |
3263 | if (get_elf_backend_data (abfd)->may_use_rela_p) | |
3264 | this_hdr->sh_entsize = bed->s->sizeof_rela; | |
3265 | break; | |
3266 | ||
3267 | case SHT_REL: | |
3268 | if (get_elf_backend_data (abfd)->may_use_rel_p) | |
3269 | this_hdr->sh_entsize = bed->s->sizeof_rel; | |
3270 | break; | |
3271 | ||
3272 | case SHT_GNU_versym: | |
252b5132 | 3273 | this_hdr->sh_entsize = sizeof (Elf_External_Versym); |
2f89ff8d L |
3274 | break; |
3275 | ||
3276 | case SHT_GNU_verdef: | |
252b5132 RH |
3277 | this_hdr->sh_entsize = 0; |
3278 | /* objcopy or strip will copy over sh_info, but may not set | |
08a40648 AM |
3279 | cverdefs. The linker will set cverdefs, but sh_info will be |
3280 | zero. */ | |
252b5132 RH |
3281 | if (this_hdr->sh_info == 0) |
3282 | this_hdr->sh_info = elf_tdata (abfd)->cverdefs; | |
3283 | else | |
3284 | BFD_ASSERT (elf_tdata (abfd)->cverdefs == 0 | |
3285 | || this_hdr->sh_info == elf_tdata (abfd)->cverdefs); | |
2f89ff8d L |
3286 | break; |
3287 | ||
3288 | case SHT_GNU_verneed: | |
252b5132 RH |
3289 | this_hdr->sh_entsize = 0; |
3290 | /* objcopy or strip will copy over sh_info, but may not set | |
08a40648 AM |
3291 | cverrefs. The linker will set cverrefs, but sh_info will be |
3292 | zero. */ | |
252b5132 RH |
3293 | if (this_hdr->sh_info == 0) |
3294 | this_hdr->sh_info = elf_tdata (abfd)->cverrefs; | |
3295 | else | |
3296 | BFD_ASSERT (elf_tdata (abfd)->cverrefs == 0 | |
3297 | || this_hdr->sh_info == elf_tdata (abfd)->cverrefs); | |
2f89ff8d L |
3298 | break; |
3299 | ||
3300 | case SHT_GROUP: | |
1783205a | 3301 | this_hdr->sh_entsize = GRP_ENTRY_SIZE; |
2f89ff8d | 3302 | break; |
fdc90cb4 JJ |
3303 | |
3304 | case SHT_GNU_HASH: | |
3305 | this_hdr->sh_entsize = bed->s->arch_size == 64 ? 0 : 4; | |
3306 | break; | |
dbb410c3 | 3307 | } |
252b5132 RH |
3308 | |
3309 | if ((asect->flags & SEC_ALLOC) != 0) | |
3310 | this_hdr->sh_flags |= SHF_ALLOC; | |
3311 | if ((asect->flags & SEC_READONLY) == 0) | |
3312 | this_hdr->sh_flags |= SHF_WRITE; | |
3313 | if ((asect->flags & SEC_CODE) != 0) | |
3314 | this_hdr->sh_flags |= SHF_EXECINSTR; | |
f5fa8ca2 JJ |
3315 | if ((asect->flags & SEC_MERGE) != 0) |
3316 | { | |
3317 | this_hdr->sh_flags |= SHF_MERGE; | |
3318 | this_hdr->sh_entsize = asect->entsize; | |
f5fa8ca2 | 3319 | } |
84865015 NC |
3320 | if ((asect->flags & SEC_STRINGS) != 0) |
3321 | this_hdr->sh_flags |= SHF_STRINGS; | |
1126897b | 3322 | if ((asect->flags & SEC_GROUP) == 0 && elf_group_name (asect) != NULL) |
dbb410c3 | 3323 | this_hdr->sh_flags |= SHF_GROUP; |
13ae64f3 | 3324 | if ((asect->flags & SEC_THREAD_LOCAL) != 0) |
704afa60 JJ |
3325 | { |
3326 | this_hdr->sh_flags |= SHF_TLS; | |
3a800eb9 AM |
3327 | if (asect->size == 0 |
3328 | && (asect->flags & SEC_HAS_CONTENTS) == 0) | |
704afa60 | 3329 | { |
3a800eb9 | 3330 | struct bfd_link_order *o = asect->map_tail.link_order; |
b34976b6 | 3331 | |
704afa60 | 3332 | this_hdr->sh_size = 0; |
3a800eb9 AM |
3333 | if (o != NULL) |
3334 | { | |
704afa60 | 3335 | this_hdr->sh_size = o->offset + o->size; |
3a800eb9 AM |
3336 | if (this_hdr->sh_size != 0) |
3337 | this_hdr->sh_type = SHT_NOBITS; | |
3338 | } | |
704afa60 JJ |
3339 | } |
3340 | } | |
18ae9cc1 L |
3341 | if ((asect->flags & (SEC_GROUP | SEC_EXCLUDE)) == SEC_EXCLUDE) |
3342 | this_hdr->sh_flags |= SHF_EXCLUDE; | |
252b5132 | 3343 | |
d4730f92 BS |
3344 | /* If the section has relocs, set up a section header for the |
3345 | SHT_REL[A] section. If two relocation sections are required for | |
3346 | this section, it is up to the processor-specific back-end to | |
3347 | create the other. */ | |
3348 | if ((asect->flags & SEC_RELOC) != 0) | |
3349 | { | |
3350 | /* When doing a relocatable link, create both REL and RELA sections if | |
3351 | needed. */ | |
3352 | if (arg->link_info | |
3353 | /* Do the normal setup if we wouldn't create any sections here. */ | |
3354 | && esd->rel.count + esd->rela.count > 0 | |
0e1862bb L |
3355 | && (bfd_link_relocatable (arg->link_info) |
3356 | || arg->link_info->emitrelocations)) | |
d4730f92 BS |
3357 | { |
3358 | if (esd->rel.count && esd->rel.hdr == NULL | |
3e19fb8f L |
3359 | && !_bfd_elf_init_reloc_shdr (abfd, &esd->rel, name, FALSE, |
3360 | delay_st_name_p)) | |
d4730f92 BS |
3361 | { |
3362 | arg->failed = TRUE; | |
3363 | return; | |
3364 | } | |
3365 | if (esd->rela.count && esd->rela.hdr == NULL | |
3e19fb8f L |
3366 | && !_bfd_elf_init_reloc_shdr (abfd, &esd->rela, name, TRUE, |
3367 | delay_st_name_p)) | |
d4730f92 BS |
3368 | { |
3369 | arg->failed = TRUE; | |
3370 | return; | |
3371 | } | |
3372 | } | |
3373 | else if (!_bfd_elf_init_reloc_shdr (abfd, | |
3374 | (asect->use_rela_p | |
3375 | ? &esd->rela : &esd->rel), | |
f6fe1ccd | 3376 | name, |
3e19fb8f L |
3377 | asect->use_rela_p, |
3378 | delay_st_name_p)) | |
d4730f92 BS |
3379 | arg->failed = TRUE; |
3380 | } | |
3381 | ||
252b5132 | 3382 | /* Check for processor-specific section types. */ |
0414f35b | 3383 | sh_type = this_hdr->sh_type; |
e1fddb6b AO |
3384 | if (bed->elf_backend_fake_sections |
3385 | && !(*bed->elf_backend_fake_sections) (abfd, this_hdr, asect)) | |
d4730f92 | 3386 | arg->failed = TRUE; |
252b5132 | 3387 | |
42bb2e33 | 3388 | if (sh_type == SHT_NOBITS && asect->size != 0) |
0414f35b AM |
3389 | { |
3390 | /* Don't change the header type from NOBITS if we are being | |
42bb2e33 | 3391 | called for objcopy --only-keep-debug. */ |
0414f35b AM |
3392 | this_hdr->sh_type = sh_type; |
3393 | } | |
252b5132 RH |
3394 | } |
3395 | ||
bcacc0f5 AM |
3396 | /* Fill in the contents of a SHT_GROUP section. Called from |
3397 | _bfd_elf_compute_section_file_positions for gas, objcopy, and | |
3398 | when ELF targets use the generic linker, ld. Called for ld -r | |
3399 | from bfd_elf_final_link. */ | |
dbb410c3 | 3400 | |
1126897b | 3401 | void |
217aa764 | 3402 | bfd_elf_set_group_contents (bfd *abfd, asection *sec, void *failedptrarg) |
dbb410c3 | 3403 | { |
a50b1753 | 3404 | bfd_boolean *failedptr = (bfd_boolean *) failedptrarg; |
9dce4196 | 3405 | asection *elt, *first; |
dbb410c3 | 3406 | unsigned char *loc; |
b34976b6 | 3407 | bfd_boolean gas; |
dbb410c3 | 3408 | |
7e4111ad L |
3409 | /* Ignore linker created group section. See elfNN_ia64_object_p in |
3410 | elfxx-ia64.c. */ | |
3411 | if (((sec->flags & (SEC_GROUP | SEC_LINKER_CREATED)) != SEC_GROUP) | |
dbb410c3 AM |
3412 | || *failedptr) |
3413 | return; | |
3414 | ||
bcacc0f5 AM |
3415 | if (elf_section_data (sec)->this_hdr.sh_info == 0) |
3416 | { | |
3417 | unsigned long symindx = 0; | |
3418 | ||
3419 | /* elf_group_id will have been set up by objcopy and the | |
3420 | generic linker. */ | |
3421 | if (elf_group_id (sec) != NULL) | |
3422 | symindx = elf_group_id (sec)->udata.i; | |
1126897b | 3423 | |
bcacc0f5 AM |
3424 | if (symindx == 0) |
3425 | { | |
3426 | /* If called from the assembler, swap_out_syms will have set up | |
3427 | elf_section_syms. */ | |
3428 | BFD_ASSERT (elf_section_syms (abfd) != NULL); | |
3429 | symindx = elf_section_syms (abfd)[sec->index]->udata.i; | |
3430 | } | |
3431 | elf_section_data (sec)->this_hdr.sh_info = symindx; | |
3432 | } | |
3433 | else if (elf_section_data (sec)->this_hdr.sh_info == (unsigned int) -2) | |
1126897b | 3434 | { |
bcacc0f5 AM |
3435 | /* The ELF backend linker sets sh_info to -2 when the group |
3436 | signature symbol is global, and thus the index can't be | |
3437 | set until all local symbols are output. */ | |
53720c49 AM |
3438 | asection *igroup; |
3439 | struct bfd_elf_section_data *sec_data; | |
3440 | unsigned long symndx; | |
3441 | unsigned long extsymoff; | |
bcacc0f5 AM |
3442 | struct elf_link_hash_entry *h; |
3443 | ||
53720c49 AM |
3444 | /* The point of this little dance to the first SHF_GROUP section |
3445 | then back to the SHT_GROUP section is that this gets us to | |
3446 | the SHT_GROUP in the input object. */ | |
3447 | igroup = elf_sec_group (elf_next_in_group (sec)); | |
3448 | sec_data = elf_section_data (igroup); | |
3449 | symndx = sec_data->this_hdr.sh_info; | |
3450 | extsymoff = 0; | |
bcacc0f5 AM |
3451 | if (!elf_bad_symtab (igroup->owner)) |
3452 | { | |
3453 | Elf_Internal_Shdr *symtab_hdr; | |
3454 | ||
3455 | symtab_hdr = &elf_tdata (igroup->owner)->symtab_hdr; | |
3456 | extsymoff = symtab_hdr->sh_info; | |
3457 | } | |
3458 | h = elf_sym_hashes (igroup->owner)[symndx - extsymoff]; | |
3459 | while (h->root.type == bfd_link_hash_indirect | |
3460 | || h->root.type == bfd_link_hash_warning) | |
3461 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
3462 | ||
3463 | elf_section_data (sec)->this_hdr.sh_info = h->indx; | |
1126897b | 3464 | } |
dbb410c3 | 3465 | |
1126897b | 3466 | /* The contents won't be allocated for "ld -r" or objcopy. */ |
b34976b6 | 3467 | gas = TRUE; |
dbb410c3 AM |
3468 | if (sec->contents == NULL) |
3469 | { | |
b34976b6 | 3470 | gas = FALSE; |
a50b1753 | 3471 | sec->contents = (unsigned char *) bfd_alloc (abfd, sec->size); |
9dce4196 AM |
3472 | |
3473 | /* Arrange for the section to be written out. */ | |
3474 | elf_section_data (sec)->this_hdr.contents = sec->contents; | |
dbb410c3 AM |
3475 | if (sec->contents == NULL) |
3476 | { | |
b34976b6 | 3477 | *failedptr = TRUE; |
dbb410c3 AM |
3478 | return; |
3479 | } | |
3480 | } | |
3481 | ||
eea6121a | 3482 | loc = sec->contents + sec->size; |
dbb410c3 | 3483 | |
9dce4196 AM |
3484 | /* Get the pointer to the first section in the group that gas |
3485 | squirreled away here. objcopy arranges for this to be set to the | |
3486 | start of the input section group. */ | |
3487 | first = elt = elf_next_in_group (sec); | |
dbb410c3 AM |
3488 | |
3489 | /* First element is a flag word. Rest of section is elf section | |
3490 | indices for all the sections of the group. Write them backwards | |
3491 | just to keep the group in the same order as given in .section | |
3492 | directives, not that it matters. */ | |
3493 | while (elt != NULL) | |
3494 | { | |
9dce4196 | 3495 | asection *s; |
9dce4196 | 3496 | |
9dce4196 | 3497 | s = elt; |
415f38a6 AM |
3498 | if (!gas) |
3499 | s = s->output_section; | |
3500 | if (s != NULL | |
3501 | && !bfd_is_abs_section (s)) | |
01e1a5bc | 3502 | { |
415f38a6 AM |
3503 | unsigned int idx = elf_section_data (s)->this_idx; |
3504 | ||
01e1a5bc | 3505 | loc -= 4; |
01e1a5bc NC |
3506 | H_PUT_32 (abfd, idx, loc); |
3507 | } | |
945906ff | 3508 | elt = elf_next_in_group (elt); |
9dce4196 AM |
3509 | if (elt == first) |
3510 | break; | |
dbb410c3 AM |
3511 | } |
3512 | ||
3d7f7666 | 3513 | if ((loc -= 4) != sec->contents) |
9dce4196 | 3514 | abort (); |
dbb410c3 | 3515 | |
9dce4196 | 3516 | H_PUT_32 (abfd, sec->flags & SEC_LINK_ONCE ? GRP_COMDAT : 0, loc); |
dbb410c3 AM |
3517 | } |
3518 | ||
bd53a53a L |
3519 | /* Return the section which RELOC_SEC applies to. */ |
3520 | ||
3521 | asection * | |
3522 | _bfd_elf_get_reloc_section (asection *reloc_sec) | |
3523 | { | |
3524 | const char *name; | |
3525 | unsigned int type; | |
3526 | bfd *abfd; | |
3527 | ||
3528 | if (reloc_sec == NULL) | |
3529 | return NULL; | |
3530 | ||
3531 | type = elf_section_data (reloc_sec)->this_hdr.sh_type; | |
3532 | if (type != SHT_REL && type != SHT_RELA) | |
3533 | return NULL; | |
3534 | ||
3535 | /* We look up the section the relocs apply to by name. */ | |
3536 | name = reloc_sec->name; | |
3537 | if (type == SHT_REL) | |
3538 | name += 4; | |
3539 | else | |
3540 | name += 5; | |
3541 | ||
3542 | /* If a target needs .got.plt section, relocations in rela.plt/rel.plt | |
3543 | section apply to .got.plt section. */ | |
3544 | abfd = reloc_sec->owner; | |
3545 | if (get_elf_backend_data (abfd)->want_got_plt | |
3546 | && strcmp (name, ".plt") == 0) | |
87070c08 AM |
3547 | { |
3548 | /* .got.plt is a linker created input section. It may be mapped | |
3549 | to some other output section. Try two likely sections. */ | |
3550 | name = ".got.plt"; | |
3551 | reloc_sec = bfd_get_section_by_name (abfd, name); | |
3552 | if (reloc_sec != NULL) | |
3553 | return reloc_sec; | |
3554 | name = ".got"; | |
3555 | } | |
bd53a53a L |
3556 | |
3557 | reloc_sec = bfd_get_section_by_name (abfd, name); | |
3558 | return reloc_sec; | |
3559 | } | |
3560 | ||
252b5132 RH |
3561 | /* Assign all ELF section numbers. The dummy first section is handled here |
3562 | too. The link/info pointers for the standard section types are filled | |
3563 | in here too, while we're at it. */ | |
3564 | ||
b34976b6 | 3565 | static bfd_boolean |
da9f89d4 | 3566 | assign_section_numbers (bfd *abfd, struct bfd_link_info *link_info) |
252b5132 RH |
3567 | { |
3568 | struct elf_obj_tdata *t = elf_tdata (abfd); | |
3569 | asection *sec; | |
3e19fb8f | 3570 | unsigned int section_number; |
252b5132 | 3571 | Elf_Internal_Shdr **i_shdrp; |
47cc2cf5 | 3572 | struct bfd_elf_section_data *d; |
3516e984 | 3573 | bfd_boolean need_symtab; |
252b5132 RH |
3574 | |
3575 | section_number = 1; | |
3576 | ||
2b0f7ef9 JJ |
3577 | _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd)); |
3578 | ||
da9f89d4 | 3579 | /* SHT_GROUP sections are in relocatable files only. */ |
0e1862bb | 3580 | if (link_info == NULL || bfd_link_relocatable (link_info)) |
252b5132 | 3581 | { |
ef53be89 | 3582 | size_t reloc_count = 0; |
14f2c699 | 3583 | |
da9f89d4 | 3584 | /* Put SHT_GROUP sections first. */ |
04dd1667 | 3585 | for (sec = abfd->sections; sec != NULL; sec = sec->next) |
47cc2cf5 | 3586 | { |
5daa8fe7 | 3587 | d = elf_section_data (sec); |
da9f89d4 L |
3588 | |
3589 | if (d->this_hdr.sh_type == SHT_GROUP) | |
08a40648 | 3590 | { |
5daa8fe7 | 3591 | if (sec->flags & SEC_LINKER_CREATED) |
da9f89d4 L |
3592 | { |
3593 | /* Remove the linker created SHT_GROUP sections. */ | |
5daa8fe7 | 3594 | bfd_section_list_remove (abfd, sec); |
da9f89d4 | 3595 | abfd->section_count--; |
da9f89d4 | 3596 | } |
08a40648 | 3597 | else |
4fbb74a6 | 3598 | d->this_idx = section_number++; |
da9f89d4 | 3599 | } |
14f2c699 L |
3600 | |
3601 | /* Count relocations. */ | |
3602 | reloc_count += sec->reloc_count; | |
47cc2cf5 | 3603 | } |
14f2c699 L |
3604 | |
3605 | /* Clear HAS_RELOC if there are no relocations. */ | |
3606 | if (reloc_count == 0) | |
3607 | abfd->flags &= ~HAS_RELOC; | |
47cc2cf5 PB |
3608 | } |
3609 | ||
3610 | for (sec = abfd->sections; sec; sec = sec->next) | |
3611 | { | |
3612 | d = elf_section_data (sec); | |
3613 | ||
3614 | if (d->this_hdr.sh_type != SHT_GROUP) | |
4fbb74a6 | 3615 | d->this_idx = section_number++; |
3e19fb8f L |
3616 | if (d->this_hdr.sh_name != (unsigned int) -1) |
3617 | _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->this_hdr.sh_name); | |
d4730f92 | 3618 | if (d->rel.hdr) |
2b0f7ef9 | 3619 | { |
d4730f92 | 3620 | d->rel.idx = section_number++; |
3e19fb8f L |
3621 | if (d->rel.hdr->sh_name != (unsigned int) -1) |
3622 | _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel.hdr->sh_name); | |
2b0f7ef9 | 3623 | } |
d4730f92 BS |
3624 | else |
3625 | d->rel.idx = 0; | |
23bc299b | 3626 | |
d4730f92 | 3627 | if (d->rela.hdr) |
2b0f7ef9 | 3628 | { |
d4730f92 | 3629 | d->rela.idx = section_number++; |
3e19fb8f L |
3630 | if (d->rela.hdr->sh_name != (unsigned int) -1) |
3631 | _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rela.hdr->sh_name); | |
2b0f7ef9 | 3632 | } |
23bc299b | 3633 | else |
d4730f92 | 3634 | d->rela.idx = 0; |
252b5132 RH |
3635 | } |
3636 | ||
3516e984 L |
3637 | need_symtab = (bfd_get_symcount (abfd) > 0 |
3638 | || (link_info == NULL | |
3639 | && ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC)) | |
3640 | == HAS_RELOC))); | |
3641 | if (need_symtab) | |
252b5132 | 3642 | { |
12bd6957 | 3643 | elf_onesymtab (abfd) = section_number++; |
2b0f7ef9 | 3644 | _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->symtab_hdr.sh_name); |
4fbb74a6 | 3645 | if (section_number > ((SHN_LORESERVE - 2) & 0xFFFF)) |
9ad5cbcf | 3646 | { |
6a40cf0c NC |
3647 | elf_section_list * entry; |
3648 | ||
3649 | BFD_ASSERT (elf_symtab_shndx_list (abfd) == NULL); | |
3650 | ||
3651 | entry = bfd_zalloc (abfd, sizeof * entry); | |
3652 | entry->ndx = section_number++; | |
3653 | elf_symtab_shndx_list (abfd) = entry; | |
3654 | entry->hdr.sh_name | |
9ad5cbcf | 3655 | = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), |
b34976b6 | 3656 | ".symtab_shndx", FALSE); |
6a40cf0c | 3657 | if (entry->hdr.sh_name == (unsigned int) -1) |
b34976b6 | 3658 | return FALSE; |
9ad5cbcf | 3659 | } |
12bd6957 | 3660 | elf_strtab_sec (abfd) = section_number++; |
2b0f7ef9 | 3661 | _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->strtab_hdr.sh_name); |
252b5132 RH |
3662 | } |
3663 | ||
dd905818 NC |
3664 | elf_shstrtab_sec (abfd) = section_number++; |
3665 | _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->shstrtab_hdr.sh_name); | |
3666 | elf_elfheader (abfd)->e_shstrndx = elf_shstrtab_sec (abfd); | |
3667 | ||
1c52a645 L |
3668 | if (section_number >= SHN_LORESERVE) |
3669 | { | |
695344c0 | 3670 | /* xgettext:c-format */ |
1c52a645 L |
3671 | _bfd_error_handler (_("%B: too many sections: %u"), |
3672 | abfd, section_number); | |
3673 | return FALSE; | |
3674 | } | |
3675 | ||
9ad5cbcf | 3676 | elf_numsections (abfd) = section_number; |
252b5132 RH |
3677 | elf_elfheader (abfd)->e_shnum = section_number; |
3678 | ||
3679 | /* Set up the list of section header pointers, in agreement with the | |
3680 | indices. */ | |
a50b1753 NC |
3681 | i_shdrp = (Elf_Internal_Shdr **) bfd_zalloc2 (abfd, section_number, |
3682 | sizeof (Elf_Internal_Shdr *)); | |
252b5132 | 3683 | if (i_shdrp == NULL) |
b34976b6 | 3684 | return FALSE; |
252b5132 | 3685 | |
a50b1753 NC |
3686 | i_shdrp[0] = (Elf_Internal_Shdr *) bfd_zalloc (abfd, |
3687 | sizeof (Elf_Internal_Shdr)); | |
252b5132 RH |
3688 | if (i_shdrp[0] == NULL) |
3689 | { | |
3690 | bfd_release (abfd, i_shdrp); | |
b34976b6 | 3691 | return FALSE; |
252b5132 | 3692 | } |
252b5132 RH |
3693 | |
3694 | elf_elfsections (abfd) = i_shdrp; | |
3695 | ||
12bd6957 | 3696 | i_shdrp[elf_shstrtab_sec (abfd)] = &t->shstrtab_hdr; |
3516e984 | 3697 | if (need_symtab) |
252b5132 | 3698 | { |
12bd6957 | 3699 | i_shdrp[elf_onesymtab (abfd)] = &t->symtab_hdr; |
4fbb74a6 | 3700 | if (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF)) |
9ad5cbcf | 3701 | { |
6a40cf0c NC |
3702 | elf_section_list * entry = elf_symtab_shndx_list (abfd); |
3703 | BFD_ASSERT (entry != NULL); | |
3704 | i_shdrp[entry->ndx] = & entry->hdr; | |
3705 | entry->hdr.sh_link = elf_onesymtab (abfd); | |
9ad5cbcf | 3706 | } |
12bd6957 AM |
3707 | i_shdrp[elf_strtab_sec (abfd)] = &t->strtab_hdr; |
3708 | t->symtab_hdr.sh_link = elf_strtab_sec (abfd); | |
252b5132 | 3709 | } |
38ce5b11 | 3710 | |
252b5132 RH |
3711 | for (sec = abfd->sections; sec; sec = sec->next) |
3712 | { | |
252b5132 | 3713 | asection *s; |
252b5132 | 3714 | |
91d6fa6a NC |
3715 | d = elf_section_data (sec); |
3716 | ||
252b5132 | 3717 | i_shdrp[d->this_idx] = &d->this_hdr; |
d4730f92 BS |
3718 | if (d->rel.idx != 0) |
3719 | i_shdrp[d->rel.idx] = d->rel.hdr; | |
3720 | if (d->rela.idx != 0) | |
3721 | i_shdrp[d->rela.idx] = d->rela.hdr; | |
252b5132 RH |
3722 | |
3723 | /* Fill in the sh_link and sh_info fields while we're at it. */ | |
3724 | ||
3725 | /* sh_link of a reloc section is the section index of the symbol | |
3726 | table. sh_info is the section index of the section to which | |
3727 | the relocation entries apply. */ | |
d4730f92 | 3728 | if (d->rel.idx != 0) |
252b5132 | 3729 | { |
12bd6957 | 3730 | d->rel.hdr->sh_link = elf_onesymtab (abfd); |
d4730f92 | 3731 | d->rel.hdr->sh_info = d->this_idx; |
9ef5d938 | 3732 | d->rel.hdr->sh_flags |= SHF_INFO_LINK; |
252b5132 | 3733 | } |
d4730f92 | 3734 | if (d->rela.idx != 0) |
23bc299b | 3735 | { |
12bd6957 | 3736 | d->rela.hdr->sh_link = elf_onesymtab (abfd); |
d4730f92 | 3737 | d->rela.hdr->sh_info = d->this_idx; |
9ef5d938 | 3738 | d->rela.hdr->sh_flags |= SHF_INFO_LINK; |
23bc299b | 3739 | } |
252b5132 | 3740 | |
38ce5b11 L |
3741 | /* We need to set up sh_link for SHF_LINK_ORDER. */ |
3742 | if ((d->this_hdr.sh_flags & SHF_LINK_ORDER) != 0) | |
3743 | { | |
3744 | s = elf_linked_to_section (sec); | |
3745 | if (s) | |
38ce5b11 | 3746 | { |
f2876037 | 3747 | /* elf_linked_to_section points to the input section. */ |
ccd2ec6a | 3748 | if (link_info != NULL) |
38ce5b11 | 3749 | { |
f2876037 | 3750 | /* Check discarded linkonce section. */ |
dbaa2011 | 3751 | if (discarded_section (s)) |
38ce5b11 | 3752 | { |
ccd2ec6a | 3753 | asection *kept; |
4eca0228 | 3754 | _bfd_error_handler |
695344c0 | 3755 | /* xgettext:c-format */ |
ccd2ec6a L |
3756 | (_("%B: sh_link of section `%A' points to discarded section `%A' of `%B'"), |
3757 | abfd, d->this_hdr.bfd_section, | |
3758 | s, s->owner); | |
3759 | /* Point to the kept section if it has the same | |
3760 | size as the discarded one. */ | |
c0f00686 | 3761 | kept = _bfd_elf_check_kept_section (s, link_info); |
ccd2ec6a | 3762 | if (kept == NULL) |
185d09ad | 3763 | { |
ccd2ec6a L |
3764 | bfd_set_error (bfd_error_bad_value); |
3765 | return FALSE; | |
185d09ad | 3766 | } |
ccd2ec6a | 3767 | s = kept; |
38ce5b11 | 3768 | } |
e424ecc8 | 3769 | |
ccd2ec6a L |
3770 | s = s->output_section; |
3771 | BFD_ASSERT (s != NULL); | |
38ce5b11 | 3772 | } |
f2876037 L |
3773 | else |
3774 | { | |
3775 | /* Handle objcopy. */ | |
3776 | if (s->output_section == NULL) | |
3777 | { | |
4eca0228 | 3778 | _bfd_error_handler |
695344c0 | 3779 | /* xgettext:c-format */ |
f2876037 L |
3780 | (_("%B: sh_link of section `%A' points to removed section `%A' of `%B'"), |
3781 | abfd, d->this_hdr.bfd_section, s, s->owner); | |
3782 | bfd_set_error (bfd_error_bad_value); | |
3783 | return FALSE; | |
3784 | } | |
3785 | s = s->output_section; | |
3786 | } | |
ccd2ec6a L |
3787 | d->this_hdr.sh_link = elf_section_data (s)->this_idx; |
3788 | } | |
3789 | else | |
3790 | { | |
3791 | /* PR 290: | |
3792 | The Intel C compiler generates SHT_IA_64_UNWIND with | |
3793 | SHF_LINK_ORDER. But it doesn't set the sh_link or | |
3794 | sh_info fields. Hence we could get the situation | |
08a40648 | 3795 | where s is NULL. */ |
ccd2ec6a L |
3796 | const struct elf_backend_data *bed |
3797 | = get_elf_backend_data (abfd); | |
3798 | if (bed->link_order_error_handler) | |
3799 | bed->link_order_error_handler | |
695344c0 | 3800 | /* xgettext:c-format */ |
ccd2ec6a L |
3801 | (_("%B: warning: sh_link not set for section `%A'"), |
3802 | abfd, sec); | |
38ce5b11 L |
3803 | } |
3804 | } | |
3805 | ||
252b5132 RH |
3806 | switch (d->this_hdr.sh_type) |
3807 | { | |
3808 | case SHT_REL: | |
3809 | case SHT_RELA: | |
3810 | /* A reloc section which we are treating as a normal BFD | |
3811 | section. sh_link is the section index of the symbol | |
3812 | table. sh_info is the section index of the section to | |
3813 | which the relocation entries apply. We assume that an | |
3814 | allocated reloc section uses the dynamic symbol table. | |
3815 | FIXME: How can we be sure? */ | |
3816 | s = bfd_get_section_by_name (abfd, ".dynsym"); | |
3817 | if (s != NULL) | |
3818 | d->this_hdr.sh_link = elf_section_data (s)->this_idx; | |
3819 | ||
bd53a53a | 3820 | s = get_elf_backend_data (abfd)->get_reloc_section (sec); |
252b5132 | 3821 | if (s != NULL) |
9ef5d938 L |
3822 | { |
3823 | d->this_hdr.sh_info = elf_section_data (s)->this_idx; | |
3824 | d->this_hdr.sh_flags |= SHF_INFO_LINK; | |
3825 | } | |
252b5132 RH |
3826 | break; |
3827 | ||
3828 | case SHT_STRTAB: | |
3829 | /* We assume that a section named .stab*str is a stabs | |
3830 | string section. We look for a section with the same name | |
3831 | but without the trailing ``str'', and set its sh_link | |
3832 | field to point to this section. */ | |
0112cd26 | 3833 | if (CONST_STRNEQ (sec->name, ".stab") |
252b5132 RH |
3834 | && strcmp (sec->name + strlen (sec->name) - 3, "str") == 0) |
3835 | { | |
3836 | size_t len; | |
3837 | char *alc; | |
3838 | ||
3839 | len = strlen (sec->name); | |
a50b1753 | 3840 | alc = (char *) bfd_malloc (len - 2); |
252b5132 | 3841 | if (alc == NULL) |
b34976b6 | 3842 | return FALSE; |
d4c88bbb | 3843 | memcpy (alc, sec->name, len - 3); |
252b5132 RH |
3844 | alc[len - 3] = '\0'; |
3845 | s = bfd_get_section_by_name (abfd, alc); | |
3846 | free (alc); | |
3847 | if (s != NULL) | |
3848 | { | |
3849 | elf_section_data (s)->this_hdr.sh_link = d->this_idx; | |
3850 | ||
3851 | /* This is a .stab section. */ | |
0594c12d AM |
3852 | if (elf_section_data (s)->this_hdr.sh_entsize == 0) |
3853 | elf_section_data (s)->this_hdr.sh_entsize | |
3854 | = 4 + 2 * bfd_get_arch_size (abfd) / 8; | |
252b5132 RH |
3855 | } |
3856 | } | |
3857 | break; | |
3858 | ||
3859 | case SHT_DYNAMIC: | |
3860 | case SHT_DYNSYM: | |
3861 | case SHT_GNU_verneed: | |
3862 | case SHT_GNU_verdef: | |
3863 | /* sh_link is the section header index of the string table | |
3864 | used for the dynamic entries, or the symbol table, or the | |
3865 | version strings. */ | |
3866 | s = bfd_get_section_by_name (abfd, ".dynstr"); | |
3867 | if (s != NULL) | |
3868 | d->this_hdr.sh_link = elf_section_data (s)->this_idx; | |
3869 | break; | |
3870 | ||
7f1204bb JJ |
3871 | case SHT_GNU_LIBLIST: |
3872 | /* sh_link is the section header index of the prelink library | |
08a40648 AM |
3873 | list used for the dynamic entries, or the symbol table, or |
3874 | the version strings. */ | |
7f1204bb JJ |
3875 | s = bfd_get_section_by_name (abfd, (sec->flags & SEC_ALLOC) |
3876 | ? ".dynstr" : ".gnu.libstr"); | |
3877 | if (s != NULL) | |
3878 | d->this_hdr.sh_link = elf_section_data (s)->this_idx; | |
3879 | break; | |
3880 | ||
252b5132 | 3881 | case SHT_HASH: |
fdc90cb4 | 3882 | case SHT_GNU_HASH: |
252b5132 RH |
3883 | case SHT_GNU_versym: |
3884 | /* sh_link is the section header index of the symbol table | |
3885 | this hash table or version table is for. */ | |
3886 | s = bfd_get_section_by_name (abfd, ".dynsym"); | |
3887 | if (s != NULL) | |
3888 | d->this_hdr.sh_link = elf_section_data (s)->this_idx; | |
3889 | break; | |
dbb410c3 AM |
3890 | |
3891 | case SHT_GROUP: | |
12bd6957 | 3892 | d->this_hdr.sh_link = elf_onesymtab (abfd); |
252b5132 RH |
3893 | } |
3894 | } | |
3895 | ||
3e19fb8f L |
3896 | /* Delay setting sh_name to _bfd_elf_write_object_contents so that |
3897 | _bfd_elf_assign_file_positions_for_non_load can convert DWARF | |
3898 | debug section name from .debug_* to .zdebug_* if needed. */ | |
3899 | ||
b34976b6 | 3900 | return TRUE; |
252b5132 RH |
3901 | } |
3902 | ||
5372391b | 3903 | static bfd_boolean |
217aa764 | 3904 | sym_is_global (bfd *abfd, asymbol *sym) |
252b5132 RH |
3905 | { |
3906 | /* If the backend has a special mapping, use it. */ | |
9c5bfbb7 | 3907 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
217aa764 AM |
3908 | if (bed->elf_backend_sym_is_global) |
3909 | return (*bed->elf_backend_sym_is_global) (abfd, sym); | |
252b5132 | 3910 | |
e47bf690 | 3911 | return ((sym->flags & (BSF_GLOBAL | BSF_WEAK | BSF_GNU_UNIQUE)) != 0 |
252b5132 RH |
3912 | || bfd_is_und_section (bfd_get_section (sym)) |
3913 | || bfd_is_com_section (bfd_get_section (sym))); | |
3914 | } | |
3915 | ||
76359541 TP |
3916 | /* Filter global symbols of ABFD to include in the import library. All |
3917 | SYMCOUNT symbols of ABFD can be examined from their pointers in | |
3918 | SYMS. Pointers of symbols to keep should be stored contiguously at | |
3919 | the beginning of that array. | |
3920 | ||
3921 | Returns the number of symbols to keep. */ | |
3922 | ||
3923 | unsigned int | |
3924 | _bfd_elf_filter_global_symbols (bfd *abfd, struct bfd_link_info *info, | |
3925 | asymbol **syms, long symcount) | |
3926 | { | |
3927 | long src_count, dst_count = 0; | |
3928 | ||
3929 | for (src_count = 0; src_count < symcount; src_count++) | |
3930 | { | |
3931 | asymbol *sym = syms[src_count]; | |
3932 | char *name = (char *) bfd_asymbol_name (sym); | |
3933 | struct bfd_link_hash_entry *h; | |
3934 | ||
3935 | if (!sym_is_global (abfd, sym)) | |
3936 | continue; | |
3937 | ||
3938 | h = bfd_link_hash_lookup (info->hash, name, FALSE, FALSE, FALSE); | |
5df1bc57 AM |
3939 | if (h == NULL) |
3940 | continue; | |
76359541 TP |
3941 | if (h->type != bfd_link_hash_defined && h->type != bfd_link_hash_defweak) |
3942 | continue; | |
76359541 TP |
3943 | if (h->linker_def || h->ldscript_def) |
3944 | continue; | |
3945 | ||
3946 | syms[dst_count++] = sym; | |
3947 | } | |
3948 | ||
3949 | syms[dst_count] = NULL; | |
3950 | ||
3951 | return dst_count; | |
3952 | } | |
3953 | ||
5372391b | 3954 | /* Don't output section symbols for sections that are not going to be |
c6d8cab4 | 3955 | output, that are duplicates or there is no BFD section. */ |
5372391b AM |
3956 | |
3957 | static bfd_boolean | |
3958 | ignore_section_sym (bfd *abfd, asymbol *sym) | |
3959 | { | |
c6d8cab4 L |
3960 | elf_symbol_type *type_ptr; |
3961 | ||
3962 | if ((sym->flags & BSF_SECTION_SYM) == 0) | |
3963 | return FALSE; | |
3964 | ||
3965 | type_ptr = elf_symbol_from (abfd, sym); | |
3966 | return ((type_ptr != NULL | |
3967 | && type_ptr->internal_elf_sym.st_shndx != 0 | |
3968 | && bfd_is_abs_section (sym->section)) | |
3969 | || !(sym->section->owner == abfd | |
0f0a5e58 | 3970 | || (sym->section->output_section->owner == abfd |
2633a79c AM |
3971 | && sym->section->output_offset == 0) |
3972 | || bfd_is_abs_section (sym->section))); | |
5372391b AM |
3973 | } |
3974 | ||
2633a79c AM |
3975 | /* Map symbol from it's internal number to the external number, moving |
3976 | all local symbols to be at the head of the list. */ | |
3977 | ||
b34976b6 | 3978 | static bfd_boolean |
12bd6957 | 3979 | elf_map_symbols (bfd *abfd, unsigned int *pnum_locals) |
252b5132 | 3980 | { |
dc810e39 | 3981 | unsigned int symcount = bfd_get_symcount (abfd); |
252b5132 RH |
3982 | asymbol **syms = bfd_get_outsymbols (abfd); |
3983 | asymbol **sect_syms; | |
dc810e39 AM |
3984 | unsigned int num_locals = 0; |
3985 | unsigned int num_globals = 0; | |
3986 | unsigned int num_locals2 = 0; | |
3987 | unsigned int num_globals2 = 0; | |
7292b3ac | 3988 | unsigned int max_index = 0; |
dc810e39 | 3989 | unsigned int idx; |
252b5132 RH |
3990 | asection *asect; |
3991 | asymbol **new_syms; | |
252b5132 RH |
3992 | |
3993 | #ifdef DEBUG | |
3994 | fprintf (stderr, "elf_map_symbols\n"); | |
3995 | fflush (stderr); | |
3996 | #endif | |
3997 | ||
252b5132 RH |
3998 | for (asect = abfd->sections; asect; asect = asect->next) |
3999 | { | |
4000 | if (max_index < asect->index) | |
4001 | max_index = asect->index; | |
4002 | } | |
4003 | ||
4004 | max_index++; | |
a50b1753 | 4005 | sect_syms = (asymbol **) bfd_zalloc2 (abfd, max_index, sizeof (asymbol *)); |
252b5132 | 4006 | if (sect_syms == NULL) |
b34976b6 | 4007 | return FALSE; |
252b5132 | 4008 | elf_section_syms (abfd) = sect_syms; |
4e89ac30 | 4009 | elf_num_section_syms (abfd) = max_index; |
252b5132 | 4010 | |
079e9a2f AM |
4011 | /* Init sect_syms entries for any section symbols we have already |
4012 | decided to output. */ | |
252b5132 RH |
4013 | for (idx = 0; idx < symcount; idx++) |
4014 | { | |
dc810e39 | 4015 | asymbol *sym = syms[idx]; |
c044fabd | 4016 | |
252b5132 | 4017 | if ((sym->flags & BSF_SECTION_SYM) != 0 |
0f0a5e58 | 4018 | && sym->value == 0 |
2633a79c AM |
4019 | && !ignore_section_sym (abfd, sym) |
4020 | && !bfd_is_abs_section (sym->section)) | |
252b5132 | 4021 | { |
5372391b | 4022 | asection *sec = sym->section; |
252b5132 | 4023 | |
5372391b AM |
4024 | if (sec->owner != abfd) |
4025 | sec = sec->output_section; | |
252b5132 | 4026 | |
5372391b | 4027 | sect_syms[sec->index] = syms[idx]; |
252b5132 RH |
4028 | } |
4029 | } | |
4030 | ||
252b5132 RH |
4031 | /* Classify all of the symbols. */ |
4032 | for (idx = 0; idx < symcount; idx++) | |
4033 | { | |
2633a79c | 4034 | if (sym_is_global (abfd, syms[idx])) |
252b5132 | 4035 | num_globals++; |
2633a79c AM |
4036 | else if (!ignore_section_sym (abfd, syms[idx])) |
4037 | num_locals++; | |
252b5132 | 4038 | } |
079e9a2f | 4039 | |
5372391b | 4040 | /* We will be adding a section symbol for each normal BFD section. Most |
079e9a2f AM |
4041 | sections will already have a section symbol in outsymbols, but |
4042 | eg. SHT_GROUP sections will not, and we need the section symbol mapped | |
4043 | at least in that case. */ | |
252b5132 RH |
4044 | for (asect = abfd->sections; asect; asect = asect->next) |
4045 | { | |
079e9a2f | 4046 | if (sect_syms[asect->index] == NULL) |
252b5132 | 4047 | { |
079e9a2f | 4048 | if (!sym_is_global (abfd, asect->symbol)) |
252b5132 RH |
4049 | num_locals++; |
4050 | else | |
4051 | num_globals++; | |
252b5132 RH |
4052 | } |
4053 | } | |
4054 | ||
4055 | /* Now sort the symbols so the local symbols are first. */ | |
a50b1753 NC |
4056 | new_syms = (asymbol **) bfd_alloc2 (abfd, num_locals + num_globals, |
4057 | sizeof (asymbol *)); | |
dc810e39 | 4058 | |
252b5132 | 4059 | if (new_syms == NULL) |
b34976b6 | 4060 | return FALSE; |
252b5132 RH |
4061 | |
4062 | for (idx = 0; idx < symcount; idx++) | |
4063 | { | |
4064 | asymbol *sym = syms[idx]; | |
dc810e39 | 4065 | unsigned int i; |
252b5132 | 4066 | |
2633a79c AM |
4067 | if (sym_is_global (abfd, sym)) |
4068 | i = num_locals + num_globals2++; | |
4069 | else if (!ignore_section_sym (abfd, sym)) | |
252b5132 RH |
4070 | i = num_locals2++; |
4071 | else | |
2633a79c | 4072 | continue; |
252b5132 RH |
4073 | new_syms[i] = sym; |
4074 | sym->udata.i = i + 1; | |
4075 | } | |
4076 | for (asect = abfd->sections; asect; asect = asect->next) | |
4077 | { | |
079e9a2f | 4078 | if (sect_syms[asect->index] == NULL) |
252b5132 | 4079 | { |
079e9a2f | 4080 | asymbol *sym = asect->symbol; |
dc810e39 | 4081 | unsigned int i; |
252b5132 | 4082 | |
079e9a2f | 4083 | sect_syms[asect->index] = sym; |
252b5132 RH |
4084 | if (!sym_is_global (abfd, sym)) |
4085 | i = num_locals2++; | |
4086 | else | |
4087 | i = num_locals + num_globals2++; | |
4088 | new_syms[i] = sym; | |
4089 | sym->udata.i = i + 1; | |
4090 | } | |
4091 | } | |
4092 | ||
4093 | bfd_set_symtab (abfd, new_syms, num_locals + num_globals); | |
4094 | ||
12bd6957 | 4095 | *pnum_locals = num_locals; |
b34976b6 | 4096 | return TRUE; |
252b5132 RH |
4097 | } |
4098 | ||
4099 | /* Align to the maximum file alignment that could be required for any | |
4100 | ELF data structure. */ | |
4101 | ||
268b6b39 | 4102 | static inline file_ptr |
217aa764 | 4103 | align_file_position (file_ptr off, int align) |
252b5132 RH |
4104 | { |
4105 | return (off + align - 1) & ~(align - 1); | |
4106 | } | |
4107 | ||
4108 | /* Assign a file position to a section, optionally aligning to the | |
4109 | required section alignment. */ | |
4110 | ||
217aa764 AM |
4111 | file_ptr |
4112 | _bfd_elf_assign_file_position_for_section (Elf_Internal_Shdr *i_shdrp, | |
4113 | file_ptr offset, | |
4114 | bfd_boolean align) | |
252b5132 | 4115 | { |
72de5009 AM |
4116 | if (align && i_shdrp->sh_addralign > 1) |
4117 | offset = BFD_ALIGN (offset, i_shdrp->sh_addralign); | |
252b5132 RH |
4118 | i_shdrp->sh_offset = offset; |
4119 | if (i_shdrp->bfd_section != NULL) | |
4120 | i_shdrp->bfd_section->filepos = offset; | |
4121 | if (i_shdrp->sh_type != SHT_NOBITS) | |
4122 | offset += i_shdrp->sh_size; | |
4123 | return offset; | |
4124 | } | |
4125 | ||
4126 | /* Compute the file positions we are going to put the sections at, and | |
4127 | otherwise prepare to begin writing out the ELF file. If LINK_INFO | |
4128 | is not NULL, this is being called by the ELF backend linker. */ | |
4129 | ||
b34976b6 | 4130 | bfd_boolean |
217aa764 AM |
4131 | _bfd_elf_compute_section_file_positions (bfd *abfd, |
4132 | struct bfd_link_info *link_info) | |
252b5132 | 4133 | { |
9c5bfbb7 | 4134 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
d4730f92 | 4135 | struct fake_section_arg fsargs; |
b34976b6 | 4136 | bfd_boolean failed; |
ef10c3ac | 4137 | struct elf_strtab_hash *strtab = NULL; |
252b5132 | 4138 | Elf_Internal_Shdr *shstrtab_hdr; |
3516e984 | 4139 | bfd_boolean need_symtab; |
252b5132 RH |
4140 | |
4141 | if (abfd->output_has_begun) | |
b34976b6 | 4142 | return TRUE; |
252b5132 RH |
4143 | |
4144 | /* Do any elf backend specific processing first. */ | |
4145 | if (bed->elf_backend_begin_write_processing) | |
4146 | (*bed->elf_backend_begin_write_processing) (abfd, link_info); | |
4147 | ||
4148 | if (! prep_headers (abfd)) | |
b34976b6 | 4149 | return FALSE; |
252b5132 | 4150 | |
e6c51ed4 | 4151 | /* Post process the headers if necessary. */ |
78245035 | 4152 | (*bed->elf_backend_post_process_headers) (abfd, link_info); |
e6c51ed4 | 4153 | |
d4730f92 BS |
4154 | fsargs.failed = FALSE; |
4155 | fsargs.link_info = link_info; | |
4156 | bfd_map_over_sections (abfd, elf_fake_sections, &fsargs); | |
4157 | if (fsargs.failed) | |
b34976b6 | 4158 | return FALSE; |
252b5132 | 4159 | |
da9f89d4 | 4160 | if (!assign_section_numbers (abfd, link_info)) |
b34976b6 | 4161 | return FALSE; |
252b5132 RH |
4162 | |
4163 | /* The backend linker builds symbol table information itself. */ | |
3516e984 L |
4164 | need_symtab = (link_info == NULL |
4165 | && (bfd_get_symcount (abfd) > 0 | |
4166 | || ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC)) | |
4167 | == HAS_RELOC))); | |
4168 | if (need_symtab) | |
252b5132 RH |
4169 | { |
4170 | /* Non-zero if doing a relocatable link. */ | |
4171 | int relocatable_p = ! (abfd->flags & (EXEC_P | DYNAMIC)); | |
4172 | ||
4173 | if (! swap_out_syms (abfd, &strtab, relocatable_p)) | |
b34976b6 | 4174 | return FALSE; |
252b5132 RH |
4175 | } |
4176 | ||
d4730f92 | 4177 | failed = FALSE; |
1126897b | 4178 | if (link_info == NULL) |
dbb410c3 | 4179 | { |
1126897b | 4180 | bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed); |
dbb410c3 | 4181 | if (failed) |
b34976b6 | 4182 | return FALSE; |
dbb410c3 AM |
4183 | } |
4184 | ||
252b5132 RH |
4185 | shstrtab_hdr = &elf_tdata (abfd)->shstrtab_hdr; |
4186 | /* sh_name was set in prep_headers. */ | |
4187 | shstrtab_hdr->sh_type = SHT_STRTAB; | |
84865015 | 4188 | shstrtab_hdr->sh_flags = bed->elf_strtab_flags; |
252b5132 | 4189 | shstrtab_hdr->sh_addr = 0; |
946748d5 | 4190 | /* sh_size is set in _bfd_elf_assign_file_positions_for_non_load. */ |
252b5132 RH |
4191 | shstrtab_hdr->sh_entsize = 0; |
4192 | shstrtab_hdr->sh_link = 0; | |
4193 | shstrtab_hdr->sh_info = 0; | |
3e19fb8f | 4194 | /* sh_offset is set in _bfd_elf_assign_file_positions_for_non_load. */ |
252b5132 RH |
4195 | shstrtab_hdr->sh_addralign = 1; |
4196 | ||
c84fca4d | 4197 | if (!assign_file_positions_except_relocs (abfd, link_info)) |
b34976b6 | 4198 | return FALSE; |
252b5132 | 4199 | |
3516e984 | 4200 | if (need_symtab) |
252b5132 RH |
4201 | { |
4202 | file_ptr off; | |
4203 | Elf_Internal_Shdr *hdr; | |
4204 | ||
12bd6957 | 4205 | off = elf_next_file_pos (abfd); |
252b5132 | 4206 | |
6a40cf0c | 4207 | hdr = & elf_symtab_hdr (abfd); |
b34976b6 | 4208 | off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE); |
252b5132 | 4209 | |
6a40cf0c NC |
4210 | if (elf_symtab_shndx_list (abfd) != NULL) |
4211 | { | |
4212 | hdr = & elf_symtab_shndx_list (abfd)->hdr; | |
4213 | if (hdr->sh_size != 0) | |
4214 | off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE); | |
4215 | /* FIXME: What about other symtab_shndx sections in the list ? */ | |
4216 | } | |
9ad5cbcf | 4217 | |
252b5132 | 4218 | hdr = &elf_tdata (abfd)->strtab_hdr; |
b34976b6 | 4219 | off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE); |
252b5132 | 4220 | |
12bd6957 | 4221 | elf_next_file_pos (abfd) = off; |
252b5132 RH |
4222 | |
4223 | /* Now that we know where the .strtab section goes, write it | |
08a40648 | 4224 | out. */ |
252b5132 | 4225 | if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0 |
ef10c3ac | 4226 | || ! _bfd_elf_strtab_emit (abfd, strtab)) |
b34976b6 | 4227 | return FALSE; |
ef10c3ac | 4228 | _bfd_elf_strtab_free (strtab); |
252b5132 RH |
4229 | } |
4230 | ||
b34976b6 | 4231 | abfd->output_has_begun = TRUE; |
252b5132 | 4232 | |
b34976b6 | 4233 | return TRUE; |
252b5132 RH |
4234 | } |
4235 | ||
8ded5a0f AM |
4236 | /* Make an initial estimate of the size of the program header. If we |
4237 | get the number wrong here, we'll redo section placement. */ | |
4238 | ||
4239 | static bfd_size_type | |
4240 | get_program_header_size (bfd *abfd, struct bfd_link_info *info) | |
4241 | { | |
4242 | size_t segs; | |
4243 | asection *s; | |
2b05f1b7 | 4244 | const struct elf_backend_data *bed; |
8ded5a0f AM |
4245 | |
4246 | /* Assume we will need exactly two PT_LOAD segments: one for text | |
4247 | and one for data. */ | |
4248 | segs = 2; | |
4249 | ||
4250 | s = bfd_get_section_by_name (abfd, ".interp"); | |
4251 | if (s != NULL && (s->flags & SEC_LOAD) != 0) | |
4252 | { | |
4253 | /* If we have a loadable interpreter section, we need a | |
4254 | PT_INTERP segment. In this case, assume we also need a | |
4255 | PT_PHDR segment, although that may not be true for all | |
4256 | targets. */ | |
1a9ccd70 | 4257 | segs += 3; |
8ded5a0f AM |
4258 | } |
4259 | ||
4260 | if (bfd_get_section_by_name (abfd, ".dynamic") != NULL) | |
4261 | { | |
4262 | /* We need a PT_DYNAMIC segment. */ | |
4263 | ++segs; | |
f210dcff | 4264 | } |
08a40648 | 4265 | |
ceae84aa | 4266 | if (info != NULL && info->relro) |
f210dcff L |
4267 | { |
4268 | /* We need a PT_GNU_RELRO segment. */ | |
4269 | ++segs; | |
8ded5a0f AM |
4270 | } |
4271 | ||
12bd6957 | 4272 | if (elf_eh_frame_hdr (abfd)) |
8ded5a0f AM |
4273 | { |
4274 | /* We need a PT_GNU_EH_FRAME segment. */ | |
4275 | ++segs; | |
4276 | } | |
4277 | ||
12bd6957 | 4278 | if (elf_stack_flags (abfd)) |
8ded5a0f | 4279 | { |
2b05f1b7 L |
4280 | /* We need a PT_GNU_STACK segment. */ |
4281 | ++segs; | |
4282 | } | |
94b11780 | 4283 | |
2b05f1b7 L |
4284 | for (s = abfd->sections; s != NULL; s = s->next) |
4285 | { | |
8ded5a0f | 4286 | if ((s->flags & SEC_LOAD) != 0 |
0112cd26 | 4287 | && CONST_STRNEQ (s->name, ".note")) |
8ded5a0f AM |
4288 | { |
4289 | /* We need a PT_NOTE segment. */ | |
4290 | ++segs; | |
1c5265b5 JJ |
4291 | /* Try to create just one PT_NOTE segment |
4292 | for all adjacent loadable .note* sections. | |
4293 | gABI requires that within a PT_NOTE segment | |
4294 | (and also inside of each SHT_NOTE section) | |
4295 | each note is padded to a multiple of 4 size, | |
4296 | so we check whether the sections are correctly | |
4297 | aligned. */ | |
4298 | if (s->alignment_power == 2) | |
4299 | while (s->next != NULL | |
4300 | && s->next->alignment_power == 2 | |
4301 | && (s->next->flags & SEC_LOAD) != 0 | |
4302 | && CONST_STRNEQ (s->next->name, ".note")) | |
4303 | s = s->next; | |
8ded5a0f AM |
4304 | } |
4305 | } | |
4306 | ||
4307 | for (s = abfd->sections; s != NULL; s = s->next) | |
4308 | { | |
4309 | if (s->flags & SEC_THREAD_LOCAL) | |
4310 | { | |
4311 | /* We need a PT_TLS segment. */ | |
4312 | ++segs; | |
4313 | break; | |
4314 | } | |
4315 | } | |
4316 | ||
4317 | /* Let the backend count up any program headers it might need. */ | |
2b05f1b7 | 4318 | bed = get_elf_backend_data (abfd); |
8ded5a0f AM |
4319 | if (bed->elf_backend_additional_program_headers) |
4320 | { | |
4321 | int a; | |
4322 | ||
4323 | a = (*bed->elf_backend_additional_program_headers) (abfd, info); | |
4324 | if (a == -1) | |
4325 | abort (); | |
4326 | segs += a; | |
4327 | } | |
4328 | ||
4329 | return segs * bed->s->sizeof_phdr; | |
4330 | } | |
4331 | ||
2ea37f1c NC |
4332 | /* Find the segment that contains the output_section of section. */ |
4333 | ||
4334 | Elf_Internal_Phdr * | |
4335 | _bfd_elf_find_segment_containing_section (bfd * abfd, asection * section) | |
4336 | { | |
4337 | struct elf_segment_map *m; | |
4338 | Elf_Internal_Phdr *p; | |
4339 | ||
12bd6957 | 4340 | for (m = elf_seg_map (abfd), p = elf_tdata (abfd)->phdr; |
2ea37f1c NC |
4341 | m != NULL; |
4342 | m = m->next, p++) | |
4343 | { | |
4344 | int i; | |
4345 | ||
4346 | for (i = m->count - 1; i >= 0; i--) | |
4347 | if (m->sections[i] == section) | |
4348 | return p; | |
4349 | } | |
4350 | ||
4351 | return NULL; | |
4352 | } | |
4353 | ||
252b5132 RH |
4354 | /* Create a mapping from a set of sections to a program segment. */ |
4355 | ||
217aa764 AM |
4356 | static struct elf_segment_map * |
4357 | make_mapping (bfd *abfd, | |
4358 | asection **sections, | |
4359 | unsigned int from, | |
4360 | unsigned int to, | |
4361 | bfd_boolean phdr) | |
252b5132 RH |
4362 | { |
4363 | struct elf_segment_map *m; | |
4364 | unsigned int i; | |
4365 | asection **hdrpp; | |
dc810e39 | 4366 | bfd_size_type amt; |
252b5132 | 4367 | |
dc810e39 AM |
4368 | amt = sizeof (struct elf_segment_map); |
4369 | amt += (to - from - 1) * sizeof (asection *); | |
a50b1753 | 4370 | m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); |
252b5132 RH |
4371 | if (m == NULL) |
4372 | return NULL; | |
4373 | m->next = NULL; | |
4374 | m->p_type = PT_LOAD; | |
4375 | for (i = from, hdrpp = sections + from; i < to; i++, hdrpp++) | |
4376 | m->sections[i - from] = *hdrpp; | |
4377 | m->count = to - from; | |
4378 | ||
4379 | if (from == 0 && phdr) | |
4380 | { | |
4381 | /* Include the headers in the first PT_LOAD segment. */ | |
4382 | m->includes_filehdr = 1; | |
4383 | m->includes_phdrs = 1; | |
4384 | } | |
4385 | ||
4386 | return m; | |
4387 | } | |
4388 | ||
229fcec5 MM |
4389 | /* Create the PT_DYNAMIC segment, which includes DYNSEC. Returns NULL |
4390 | on failure. */ | |
4391 | ||
4392 | struct elf_segment_map * | |
4393 | _bfd_elf_make_dynamic_segment (bfd *abfd, asection *dynsec) | |
4394 | { | |
4395 | struct elf_segment_map *m; | |
4396 | ||
a50b1753 NC |
4397 | m = (struct elf_segment_map *) bfd_zalloc (abfd, |
4398 | sizeof (struct elf_segment_map)); | |
229fcec5 MM |
4399 | if (m == NULL) |
4400 | return NULL; | |
4401 | m->next = NULL; | |
4402 | m->p_type = PT_DYNAMIC; | |
4403 | m->count = 1; | |
4404 | m->sections[0] = dynsec; | |
08a40648 | 4405 | |
229fcec5 MM |
4406 | return m; |
4407 | } | |
4408 | ||
8ded5a0f | 4409 | /* Possibly add or remove segments from the segment map. */ |
252b5132 | 4410 | |
b34976b6 | 4411 | static bfd_boolean |
3dea8fca AM |
4412 | elf_modify_segment_map (bfd *abfd, |
4413 | struct bfd_link_info *info, | |
4414 | bfd_boolean remove_empty_load) | |
252b5132 | 4415 | { |
252e386e | 4416 | struct elf_segment_map **m; |
8ded5a0f | 4417 | const struct elf_backend_data *bed; |
252b5132 | 4418 | |
8ded5a0f AM |
4419 | /* The placement algorithm assumes that non allocated sections are |
4420 | not in PT_LOAD segments. We ensure this here by removing such | |
4421 | sections from the segment map. We also remove excluded | |
252e386e AM |
4422 | sections. Finally, any PT_LOAD segment without sections is |
4423 | removed. */ | |
12bd6957 | 4424 | m = &elf_seg_map (abfd); |
252e386e | 4425 | while (*m) |
8ded5a0f AM |
4426 | { |
4427 | unsigned int i, new_count; | |
252b5132 | 4428 | |
252e386e | 4429 | for (new_count = 0, i = 0; i < (*m)->count; i++) |
8ded5a0f | 4430 | { |
252e386e AM |
4431 | if (((*m)->sections[i]->flags & SEC_EXCLUDE) == 0 |
4432 | && (((*m)->sections[i]->flags & SEC_ALLOC) != 0 | |
4433 | || (*m)->p_type != PT_LOAD)) | |
8ded5a0f | 4434 | { |
252e386e AM |
4435 | (*m)->sections[new_count] = (*m)->sections[i]; |
4436 | new_count++; | |
8ded5a0f AM |
4437 | } |
4438 | } | |
252e386e | 4439 | (*m)->count = new_count; |
252b5132 | 4440 | |
1a9ccd70 NC |
4441 | if (remove_empty_load |
4442 | && (*m)->p_type == PT_LOAD | |
4443 | && (*m)->count == 0 | |
4444 | && !(*m)->includes_phdrs) | |
252e386e AM |
4445 | *m = (*m)->next; |
4446 | else | |
4447 | m = &(*m)->next; | |
8ded5a0f | 4448 | } |
252b5132 | 4449 | |
8ded5a0f AM |
4450 | bed = get_elf_backend_data (abfd); |
4451 | if (bed->elf_backend_modify_segment_map != NULL) | |
252b5132 | 4452 | { |
252e386e | 4453 | if (!(*bed->elf_backend_modify_segment_map) (abfd, info)) |
8ded5a0f | 4454 | return FALSE; |
252b5132 | 4455 | } |
252b5132 | 4456 | |
8ded5a0f AM |
4457 | return TRUE; |
4458 | } | |
252b5132 | 4459 | |
8ded5a0f | 4460 | /* Set up a mapping from BFD sections to program segments. */ |
252b5132 | 4461 | |
8ded5a0f AM |
4462 | bfd_boolean |
4463 | _bfd_elf_map_sections_to_segments (bfd *abfd, struct bfd_link_info *info) | |
4464 | { | |
4465 | unsigned int count; | |
4466 | struct elf_segment_map *m; | |
4467 | asection **sections = NULL; | |
4468 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
3dea8fca | 4469 | bfd_boolean no_user_phdrs; |
252b5132 | 4470 | |
12bd6957 | 4471 | no_user_phdrs = elf_seg_map (abfd) == NULL; |
d324f6d6 RM |
4472 | |
4473 | if (info != NULL) | |
4474 | info->user_phdrs = !no_user_phdrs; | |
4475 | ||
3dea8fca | 4476 | if (no_user_phdrs && bfd_count_sections (abfd) != 0) |
252b5132 | 4477 | { |
8ded5a0f AM |
4478 | asection *s; |
4479 | unsigned int i; | |
4480 | struct elf_segment_map *mfirst; | |
4481 | struct elf_segment_map **pm; | |
4482 | asection *last_hdr; | |
4483 | bfd_vma last_size; | |
4484 | unsigned int phdr_index; | |
4485 | bfd_vma maxpagesize; | |
4486 | asection **hdrpp; | |
4487 | bfd_boolean phdr_in_segment = TRUE; | |
4488 | bfd_boolean writable; | |
4489 | int tls_count = 0; | |
4490 | asection *first_tls = NULL; | |
4491 | asection *dynsec, *eh_frame_hdr; | |
4492 | bfd_size_type amt; | |
8d06853e | 4493 | bfd_vma addr_mask, wrap_to = 0; |
1a9ccd70 | 4494 | bfd_boolean linker_created_pt_phdr_segment = FALSE; |
252b5132 | 4495 | |
8ded5a0f | 4496 | /* Select the allocated sections, and sort them. */ |
252b5132 | 4497 | |
a50b1753 NC |
4498 | sections = (asection **) bfd_malloc2 (bfd_count_sections (abfd), |
4499 | sizeof (asection *)); | |
8ded5a0f | 4500 | if (sections == NULL) |
252b5132 | 4501 | goto error_return; |
252b5132 | 4502 | |
8d06853e AM |
4503 | /* Calculate top address, avoiding undefined behaviour of shift |
4504 | left operator when shift count is equal to size of type | |
4505 | being shifted. */ | |
4506 | addr_mask = ((bfd_vma) 1 << (bfd_arch_bits_per_address (abfd) - 1)) - 1; | |
4507 | addr_mask = (addr_mask << 1) + 1; | |
4508 | ||
8ded5a0f AM |
4509 | i = 0; |
4510 | for (s = abfd->sections; s != NULL; s = s->next) | |
4511 | { | |
4512 | if ((s->flags & SEC_ALLOC) != 0) | |
4513 | { | |
4514 | sections[i] = s; | |
4515 | ++i; | |
8d06853e AM |
4516 | /* A wrapping section potentially clashes with header. */ |
4517 | if (((s->lma + s->size) & addr_mask) < (s->lma & addr_mask)) | |
4518 | wrap_to = (s->lma + s->size) & addr_mask; | |
8ded5a0f AM |
4519 | } |
4520 | } | |
4521 | BFD_ASSERT (i <= bfd_count_sections (abfd)); | |
4522 | count = i; | |
252b5132 | 4523 | |
8ded5a0f | 4524 | qsort (sections, (size_t) count, sizeof (asection *), elf_sort_sections); |
252b5132 | 4525 | |
8ded5a0f | 4526 | /* Build the mapping. */ |
252b5132 | 4527 | |
8ded5a0f AM |
4528 | mfirst = NULL; |
4529 | pm = &mfirst; | |
252b5132 | 4530 | |
8ded5a0f AM |
4531 | /* If we have a .interp section, then create a PT_PHDR segment for |
4532 | the program headers and a PT_INTERP segment for the .interp | |
4533 | section. */ | |
4534 | s = bfd_get_section_by_name (abfd, ".interp"); | |
4535 | if (s != NULL && (s->flags & SEC_LOAD) != 0) | |
4536 | { | |
4537 | amt = sizeof (struct elf_segment_map); | |
a50b1753 | 4538 | m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); |
8ded5a0f AM |
4539 | if (m == NULL) |
4540 | goto error_return; | |
4541 | m->next = NULL; | |
4542 | m->p_type = PT_PHDR; | |
4543 | /* FIXME: UnixWare and Solaris set PF_X, Irix 5 does not. */ | |
4544 | m->p_flags = PF_R | PF_X; | |
4545 | m->p_flags_valid = 1; | |
4546 | m->includes_phdrs = 1; | |
1a9ccd70 | 4547 | linker_created_pt_phdr_segment = TRUE; |
8ded5a0f AM |
4548 | *pm = m; |
4549 | pm = &m->next; | |
252b5132 | 4550 | |
8ded5a0f | 4551 | amt = sizeof (struct elf_segment_map); |
a50b1753 | 4552 | m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); |
8ded5a0f AM |
4553 | if (m == NULL) |
4554 | goto error_return; | |
4555 | m->next = NULL; | |
4556 | m->p_type = PT_INTERP; | |
4557 | m->count = 1; | |
4558 | m->sections[0] = s; | |
4559 | ||
4560 | *pm = m; | |
4561 | pm = &m->next; | |
252b5132 | 4562 | } |
8ded5a0f AM |
4563 | |
4564 | /* Look through the sections. We put sections in the same program | |
4565 | segment when the start of the second section can be placed within | |
4566 | a few bytes of the end of the first section. */ | |
4567 | last_hdr = NULL; | |
4568 | last_size = 0; | |
4569 | phdr_index = 0; | |
4570 | maxpagesize = bed->maxpagesize; | |
063bb025 NC |
4571 | /* PR 17512: file: c8455299. |
4572 | Avoid divide-by-zero errors later on. | |
4573 | FIXME: Should we abort if the maxpagesize is zero ? */ | |
4574 | if (maxpagesize == 0) | |
4575 | maxpagesize = 1; | |
8ded5a0f AM |
4576 | writable = FALSE; |
4577 | dynsec = bfd_get_section_by_name (abfd, ".dynamic"); | |
4578 | if (dynsec != NULL | |
4579 | && (dynsec->flags & SEC_LOAD) == 0) | |
4580 | dynsec = NULL; | |
4581 | ||
4582 | /* Deal with -Ttext or something similar such that the first section | |
4583 | is not adjacent to the program headers. This is an | |
4584 | approximation, since at this point we don't know exactly how many | |
4585 | program headers we will need. */ | |
4586 | if (count > 0) | |
252b5132 | 4587 | { |
12bd6957 | 4588 | bfd_size_type phdr_size = elf_program_header_size (abfd); |
8ded5a0f | 4589 | |
62d7a5f6 | 4590 | if (phdr_size == (bfd_size_type) -1) |
8ded5a0f | 4591 | phdr_size = get_program_header_size (abfd, info); |
d2bcb0d1 | 4592 | phdr_size += bed->s->sizeof_ehdr; |
8ded5a0f | 4593 | if ((abfd->flags & D_PAGED) == 0 |
8d06853e AM |
4594 | || (sections[0]->lma & addr_mask) < phdr_size |
4595 | || ((sections[0]->lma & addr_mask) % maxpagesize | |
4596 | < phdr_size % maxpagesize) | |
4597 | || (sections[0]->lma & addr_mask & -maxpagesize) < wrap_to) | |
1a9ccd70 NC |
4598 | { |
4599 | /* PR 20815: The ELF standard says that a PT_PHDR segment, if | |
4600 | present, must be included as part of the memory image of the | |
4601 | program. Ie it must be part of a PT_LOAD segment as well. | |
4602 | If we have had to create our own PT_PHDR segment, but it is | |
4603 | not going to be covered by the first PT_LOAD segment, then | |
4604 | force the inclusion if we can... */ | |
4605 | if ((abfd->flags & D_PAGED) != 0 | |
4606 | && linker_created_pt_phdr_segment) | |
4607 | phdr_in_segment = TRUE; | |
4608 | else | |
4609 | phdr_in_segment = FALSE; | |
4610 | } | |
252b5132 RH |
4611 | } |
4612 | ||
8ded5a0f | 4613 | for (i = 0, hdrpp = sections; i < count; i++, hdrpp++) |
252b5132 | 4614 | { |
8ded5a0f AM |
4615 | asection *hdr; |
4616 | bfd_boolean new_segment; | |
4617 | ||
4618 | hdr = *hdrpp; | |
4619 | ||
4620 | /* See if this section and the last one will fit in the same | |
4621 | segment. */ | |
4622 | ||
4623 | if (last_hdr == NULL) | |
4624 | { | |
4625 | /* If we don't have a segment yet, then we don't need a new | |
4626 | one (we build the last one after this loop). */ | |
4627 | new_segment = FALSE; | |
4628 | } | |
4629 | else if (last_hdr->lma - last_hdr->vma != hdr->lma - hdr->vma) | |
4630 | { | |
4631 | /* If this section has a different relation between the | |
4632 | virtual address and the load address, then we need a new | |
4633 | segment. */ | |
4634 | new_segment = TRUE; | |
4635 | } | |
b5599592 AM |
4636 | else if (hdr->lma < last_hdr->lma + last_size |
4637 | || last_hdr->lma + last_size < last_hdr->lma) | |
4638 | { | |
4639 | /* If this section has a load address that makes it overlap | |
4640 | the previous section, then we need a new segment. */ | |
4641 | new_segment = TRUE; | |
4642 | } | |
39948a60 NC |
4643 | /* In the next test we have to be careful when last_hdr->lma is close |
4644 | to the end of the address space. If the aligned address wraps | |
4645 | around to the start of the address space, then there are no more | |
4646 | pages left in memory and it is OK to assume that the current | |
4647 | section can be included in the current segment. */ | |
4648 | else if ((BFD_ALIGN (last_hdr->lma + last_size, maxpagesize) + maxpagesize | |
4649 | > last_hdr->lma) | |
4650 | && (BFD_ALIGN (last_hdr->lma + last_size, maxpagesize) + maxpagesize | |
4ff73856 | 4651 | <= hdr->lma)) |
8ded5a0f AM |
4652 | { |
4653 | /* If putting this section in this segment would force us to | |
4654 | skip a page in the segment, then we need a new segment. */ | |
4655 | new_segment = TRUE; | |
4656 | } | |
4657 | else if ((last_hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0 | |
e5654c0f AM |
4658 | && (hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) != 0 |
4659 | && ((abfd->flags & D_PAGED) == 0 | |
4660 | || (((last_hdr->lma + last_size - 1) & -maxpagesize) | |
4661 | != (hdr->lma & -maxpagesize)))) | |
8ded5a0f | 4662 | { |
e5654c0f AM |
4663 | /* We don't want to put a loaded section after a |
4664 | nonloaded (ie. bss style) section in the same segment | |
4665 | as that will force the non-loaded section to be loaded. | |
4666 | Consider .tbss sections as loaded for this purpose. | |
4667 | However, like the writable/non-writable case below, | |
4668 | if they are on the same page then they must be put | |
4669 | in the same segment. */ | |
8ded5a0f AM |
4670 | new_segment = TRUE; |
4671 | } | |
4672 | else if ((abfd->flags & D_PAGED) == 0) | |
4673 | { | |
4674 | /* If the file is not demand paged, which means that we | |
4675 | don't require the sections to be correctly aligned in the | |
4676 | file, then there is no other reason for a new segment. */ | |
4677 | new_segment = FALSE; | |
4678 | } | |
4679 | else if (! writable | |
4680 | && (hdr->flags & SEC_READONLY) == 0 | |
8d06853e AM |
4681 | && (((last_hdr->lma + last_size - 1) & -maxpagesize) |
4682 | != (hdr->lma & -maxpagesize))) | |
8ded5a0f AM |
4683 | { |
4684 | /* We don't want to put a writable section in a read only | |
4685 | segment, unless they are on the same page in memory | |
4686 | anyhow. We already know that the last section does not | |
4687 | bring us past the current section on the page, so the | |
4688 | only case in which the new section is not on the same | |
4689 | page as the previous section is when the previous section | |
4690 | ends precisely on a page boundary. */ | |
4691 | new_segment = TRUE; | |
4692 | } | |
4693 | else | |
4694 | { | |
4695 | /* Otherwise, we can use the same segment. */ | |
4696 | new_segment = FALSE; | |
4697 | } | |
4698 | ||
2889e75b | 4699 | /* Allow interested parties a chance to override our decision. */ |
ceae84aa AM |
4700 | if (last_hdr != NULL |
4701 | && info != NULL | |
4702 | && info->callbacks->override_segment_assignment != NULL) | |
4703 | new_segment | |
4704 | = info->callbacks->override_segment_assignment (info, abfd, hdr, | |
4705 | last_hdr, | |
4706 | new_segment); | |
2889e75b | 4707 | |
8ded5a0f AM |
4708 | if (! new_segment) |
4709 | { | |
4710 | if ((hdr->flags & SEC_READONLY) == 0) | |
4711 | writable = TRUE; | |
4712 | last_hdr = hdr; | |
4713 | /* .tbss sections effectively have zero size. */ | |
4714 | if ((hdr->flags & (SEC_THREAD_LOCAL | SEC_LOAD)) | |
4715 | != SEC_THREAD_LOCAL) | |
4716 | last_size = hdr->size; | |
4717 | else | |
4718 | last_size = 0; | |
4719 | continue; | |
4720 | } | |
4721 | ||
4722 | /* We need a new program segment. We must create a new program | |
4723 | header holding all the sections from phdr_index until hdr. */ | |
4724 | ||
4725 | m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment); | |
4726 | if (m == NULL) | |
4727 | goto error_return; | |
4728 | ||
4729 | *pm = m; | |
4730 | pm = &m->next; | |
4731 | ||
252b5132 | 4732 | if ((hdr->flags & SEC_READONLY) == 0) |
b34976b6 | 4733 | writable = TRUE; |
8ded5a0f AM |
4734 | else |
4735 | writable = FALSE; | |
4736 | ||
baaff79e JJ |
4737 | last_hdr = hdr; |
4738 | /* .tbss sections effectively have zero size. */ | |
e5caec89 | 4739 | if ((hdr->flags & (SEC_THREAD_LOCAL | SEC_LOAD)) != SEC_THREAD_LOCAL) |
eea6121a | 4740 | last_size = hdr->size; |
baaff79e JJ |
4741 | else |
4742 | last_size = 0; | |
8ded5a0f AM |
4743 | phdr_index = i; |
4744 | phdr_in_segment = FALSE; | |
252b5132 RH |
4745 | } |
4746 | ||
86b2281f AM |
4747 | /* Create a final PT_LOAD program segment, but not if it's just |
4748 | for .tbss. */ | |
4749 | if (last_hdr != NULL | |
4750 | && (i - phdr_index != 1 | |
4751 | || ((last_hdr->flags & (SEC_THREAD_LOCAL | SEC_LOAD)) | |
4752 | != SEC_THREAD_LOCAL))) | |
8ded5a0f AM |
4753 | { |
4754 | m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment); | |
4755 | if (m == NULL) | |
4756 | goto error_return; | |
252b5132 | 4757 | |
8ded5a0f AM |
4758 | *pm = m; |
4759 | pm = &m->next; | |
4760 | } | |
252b5132 | 4761 | |
8ded5a0f AM |
4762 | /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */ |
4763 | if (dynsec != NULL) | |
4764 | { | |
4765 | m = _bfd_elf_make_dynamic_segment (abfd, dynsec); | |
4766 | if (m == NULL) | |
4767 | goto error_return; | |
4768 | *pm = m; | |
4769 | pm = &m->next; | |
4770 | } | |
252b5132 | 4771 | |
1c5265b5 JJ |
4772 | /* For each batch of consecutive loadable .note sections, |
4773 | add a PT_NOTE segment. We don't use bfd_get_section_by_name, | |
4774 | because if we link together nonloadable .note sections and | |
4775 | loadable .note sections, we will generate two .note sections | |
4776 | in the output file. FIXME: Using names for section types is | |
4777 | bogus anyhow. */ | |
8ded5a0f AM |
4778 | for (s = abfd->sections; s != NULL; s = s->next) |
4779 | { | |
4780 | if ((s->flags & SEC_LOAD) != 0 | |
0112cd26 | 4781 | && CONST_STRNEQ (s->name, ".note")) |
8ded5a0f | 4782 | { |
1c5265b5 | 4783 | asection *s2; |
91d6fa6a NC |
4784 | |
4785 | count = 1; | |
8ded5a0f | 4786 | amt = sizeof (struct elf_segment_map); |
1c5265b5 JJ |
4787 | if (s->alignment_power == 2) |
4788 | for (s2 = s; s2->next != NULL; s2 = s2->next) | |
55b581a6 JJ |
4789 | { |
4790 | if (s2->next->alignment_power == 2 | |
4791 | && (s2->next->flags & SEC_LOAD) != 0 | |
4792 | && CONST_STRNEQ (s2->next->name, ".note") | |
8d06853e AM |
4793 | && align_power (s2->lma + s2->size, 2) |
4794 | == s2->next->lma) | |
55b581a6 JJ |
4795 | count++; |
4796 | else | |
4797 | break; | |
4798 | } | |
1c5265b5 | 4799 | amt += (count - 1) * sizeof (asection *); |
a50b1753 | 4800 | m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); |
8ded5a0f AM |
4801 | if (m == NULL) |
4802 | goto error_return; | |
4803 | m->next = NULL; | |
4804 | m->p_type = PT_NOTE; | |
1c5265b5 JJ |
4805 | m->count = count; |
4806 | while (count > 1) | |
4807 | { | |
4808 | m->sections[m->count - count--] = s; | |
4809 | BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0); | |
4810 | s = s->next; | |
4811 | } | |
4812 | m->sections[m->count - 1] = s; | |
4813 | BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0); | |
8ded5a0f AM |
4814 | *pm = m; |
4815 | pm = &m->next; | |
4816 | } | |
4817 | if (s->flags & SEC_THREAD_LOCAL) | |
4818 | { | |
4819 | if (! tls_count) | |
4820 | first_tls = s; | |
4821 | tls_count++; | |
4822 | } | |
4823 | } | |
252b5132 | 4824 | |
8ded5a0f AM |
4825 | /* If there are any SHF_TLS output sections, add PT_TLS segment. */ |
4826 | if (tls_count > 0) | |
4827 | { | |
8ded5a0f AM |
4828 | amt = sizeof (struct elf_segment_map); |
4829 | amt += (tls_count - 1) * sizeof (asection *); | |
a50b1753 | 4830 | m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); |
8ded5a0f AM |
4831 | if (m == NULL) |
4832 | goto error_return; | |
4833 | m->next = NULL; | |
4834 | m->p_type = PT_TLS; | |
4835 | m->count = tls_count; | |
4836 | /* Mandated PF_R. */ | |
4837 | m->p_flags = PF_R; | |
4838 | m->p_flags_valid = 1; | |
d923cae0 | 4839 | s = first_tls; |
91d6fa6a | 4840 | for (i = 0; i < (unsigned int) tls_count; ++i) |
8ded5a0f | 4841 | { |
d923cae0 L |
4842 | if ((s->flags & SEC_THREAD_LOCAL) == 0) |
4843 | { | |
4844 | _bfd_error_handler | |
4845 | (_("%B: TLS sections are not adjacent:"), abfd); | |
4846 | s = first_tls; | |
4847 | i = 0; | |
4848 | while (i < (unsigned int) tls_count) | |
4849 | { | |
4850 | if ((s->flags & SEC_THREAD_LOCAL) != 0) | |
4851 | { | |
4852 | _bfd_error_handler (_(" TLS: %A"), s); | |
4853 | i++; | |
4854 | } | |
4855 | else | |
4856 | _bfd_error_handler (_(" non-TLS: %A"), s); | |
4857 | s = s->next; | |
4858 | } | |
4859 | bfd_set_error (bfd_error_bad_value); | |
4860 | goto error_return; | |
4861 | } | |
4862 | m->sections[i] = s; | |
4863 | s = s->next; | |
8ded5a0f | 4864 | } |
252b5132 | 4865 | |
8ded5a0f AM |
4866 | *pm = m; |
4867 | pm = &m->next; | |
4868 | } | |
252b5132 | 4869 | |
8ded5a0f AM |
4870 | /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME |
4871 | segment. */ | |
12bd6957 | 4872 | eh_frame_hdr = elf_eh_frame_hdr (abfd); |
8ded5a0f AM |
4873 | if (eh_frame_hdr != NULL |
4874 | && (eh_frame_hdr->output_section->flags & SEC_LOAD) != 0) | |
252b5132 | 4875 | { |
dc810e39 | 4876 | amt = sizeof (struct elf_segment_map); |
a50b1753 | 4877 | m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); |
252b5132 RH |
4878 | if (m == NULL) |
4879 | goto error_return; | |
4880 | m->next = NULL; | |
8ded5a0f | 4881 | m->p_type = PT_GNU_EH_FRAME; |
252b5132 | 4882 | m->count = 1; |
8ded5a0f | 4883 | m->sections[0] = eh_frame_hdr->output_section; |
252b5132 RH |
4884 | |
4885 | *pm = m; | |
4886 | pm = &m->next; | |
4887 | } | |
13ae64f3 | 4888 | |
12bd6957 | 4889 | if (elf_stack_flags (abfd)) |
13ae64f3 | 4890 | { |
8ded5a0f | 4891 | amt = sizeof (struct elf_segment_map); |
a50b1753 | 4892 | m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); |
8ded5a0f AM |
4893 | if (m == NULL) |
4894 | goto error_return; | |
4895 | m->next = NULL; | |
2b05f1b7 | 4896 | m->p_type = PT_GNU_STACK; |
12bd6957 | 4897 | m->p_flags = elf_stack_flags (abfd); |
04c3a755 | 4898 | m->p_align = bed->stack_align; |
8ded5a0f | 4899 | m->p_flags_valid = 1; |
04c3a755 NS |
4900 | m->p_align_valid = m->p_align != 0; |
4901 | if (info->stacksize > 0) | |
4902 | { | |
4903 | m->p_size = info->stacksize; | |
4904 | m->p_size_valid = 1; | |
4905 | } | |
252b5132 | 4906 | |
8ded5a0f AM |
4907 | *pm = m; |
4908 | pm = &m->next; | |
4909 | } | |
65765700 | 4910 | |
ceae84aa | 4911 | if (info != NULL && info->relro) |
8ded5a0f | 4912 | { |
f210dcff L |
4913 | for (m = mfirst; m != NULL; m = m->next) |
4914 | { | |
3832a4d8 AM |
4915 | if (m->p_type == PT_LOAD |
4916 | && m->count != 0 | |
4917 | && m->sections[0]->vma >= info->relro_start | |
4918 | && m->sections[0]->vma < info->relro_end) | |
f210dcff | 4919 | { |
3832a4d8 AM |
4920 | i = m->count; |
4921 | while (--i != (unsigned) -1) | |
4922 | if ((m->sections[i]->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) | |
4923 | == (SEC_LOAD | SEC_HAS_CONTENTS)) | |
4924 | break; | |
4925 | ||
43a8475c | 4926 | if (i != (unsigned) -1) |
f210dcff L |
4927 | break; |
4928 | } | |
be01b344 | 4929 | } |
f210dcff L |
4930 | |
4931 | /* Make a PT_GNU_RELRO segment only when it isn't empty. */ | |
4932 | if (m != NULL) | |
4933 | { | |
4934 | amt = sizeof (struct elf_segment_map); | |
a50b1753 | 4935 | m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); |
f210dcff L |
4936 | if (m == NULL) |
4937 | goto error_return; | |
4938 | m->next = NULL; | |
4939 | m->p_type = PT_GNU_RELRO; | |
f210dcff L |
4940 | *pm = m; |
4941 | pm = &m->next; | |
4942 | } | |
8ded5a0f | 4943 | } |
9ee5e499 | 4944 | |
8ded5a0f | 4945 | free (sections); |
12bd6957 | 4946 | elf_seg_map (abfd) = mfirst; |
9ee5e499 JJ |
4947 | } |
4948 | ||
3dea8fca | 4949 | if (!elf_modify_segment_map (abfd, info, no_user_phdrs)) |
8ded5a0f | 4950 | return FALSE; |
8c37241b | 4951 | |
12bd6957 | 4952 | for (count = 0, m = elf_seg_map (abfd); m != NULL; m = m->next) |
8ded5a0f | 4953 | ++count; |
12bd6957 | 4954 | elf_program_header_size (abfd) = count * bed->s->sizeof_phdr; |
252b5132 | 4955 | |
b34976b6 | 4956 | return TRUE; |
252b5132 RH |
4957 | |
4958 | error_return: | |
4959 | if (sections != NULL) | |
4960 | free (sections); | |
b34976b6 | 4961 | return FALSE; |
252b5132 RH |
4962 | } |
4963 | ||
4964 | /* Sort sections by address. */ | |
4965 | ||
4966 | static int | |
217aa764 | 4967 | elf_sort_sections (const void *arg1, const void *arg2) |
252b5132 RH |
4968 | { |
4969 | const asection *sec1 = *(const asection **) arg1; | |
4970 | const asection *sec2 = *(const asection **) arg2; | |
eecdbe52 | 4971 | bfd_size_type size1, size2; |
252b5132 RH |
4972 | |
4973 | /* Sort by LMA first, since this is the address used to | |
4974 | place the section into a segment. */ | |
4975 | if (sec1->lma < sec2->lma) | |
4976 | return -1; | |
4977 | else if (sec1->lma > sec2->lma) | |
4978 | return 1; | |
4979 | ||
4980 | /* Then sort by VMA. Normally the LMA and the VMA will be | |
4981 | the same, and this will do nothing. */ | |
4982 | if (sec1->vma < sec2->vma) | |
4983 | return -1; | |
4984 | else if (sec1->vma > sec2->vma) | |
4985 | return 1; | |
4986 | ||
4987 | /* Put !SEC_LOAD sections after SEC_LOAD ones. */ | |
4988 | ||
07c6e936 | 4989 | #define TOEND(x) (((x)->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0) |
252b5132 RH |
4990 | |
4991 | if (TOEND (sec1)) | |
4992 | { | |
4993 | if (TOEND (sec2)) | |
00a7cdc5 NC |
4994 | { |
4995 | /* If the indicies are the same, do not return 0 | |
4996 | here, but continue to try the next comparison. */ | |
4997 | if (sec1->target_index - sec2->target_index != 0) | |
4998 | return sec1->target_index - sec2->target_index; | |
4999 | } | |
252b5132 RH |
5000 | else |
5001 | return 1; | |
5002 | } | |
00a7cdc5 | 5003 | else if (TOEND (sec2)) |
252b5132 RH |
5004 | return -1; |
5005 | ||
5006 | #undef TOEND | |
5007 | ||
00a7cdc5 NC |
5008 | /* Sort by size, to put zero sized sections |
5009 | before others at the same address. */ | |
252b5132 | 5010 | |
eea6121a AM |
5011 | size1 = (sec1->flags & SEC_LOAD) ? sec1->size : 0; |
5012 | size2 = (sec2->flags & SEC_LOAD) ? sec2->size : 0; | |
eecdbe52 JJ |
5013 | |
5014 | if (size1 < size2) | |
252b5132 | 5015 | return -1; |
eecdbe52 | 5016 | if (size1 > size2) |
252b5132 RH |
5017 | return 1; |
5018 | ||
5019 | return sec1->target_index - sec2->target_index; | |
5020 | } | |
5021 | ||
340b6d91 AC |
5022 | /* Ian Lance Taylor writes: |
5023 | ||
5024 | We shouldn't be using % with a negative signed number. That's just | |
5025 | not good. We have to make sure either that the number is not | |
5026 | negative, or that the number has an unsigned type. When the types | |
5027 | are all the same size they wind up as unsigned. When file_ptr is a | |
5028 | larger signed type, the arithmetic winds up as signed long long, | |
5029 | which is wrong. | |
5030 | ||
5031 | What we're trying to say here is something like ``increase OFF by | |
5032 | the least amount that will cause it to be equal to the VMA modulo | |
5033 | the page size.'' */ | |
5034 | /* In other words, something like: | |
5035 | ||
5036 | vma_offset = m->sections[0]->vma % bed->maxpagesize; | |
5037 | off_offset = off % bed->maxpagesize; | |
5038 | if (vma_offset < off_offset) | |
5039 | adjustment = vma_offset + bed->maxpagesize - off_offset; | |
5040 | else | |
5041 | adjustment = vma_offset - off_offset; | |
08a40648 | 5042 | |
340b6d91 AC |
5043 | which can can be collapsed into the expression below. */ |
5044 | ||
5045 | static file_ptr | |
5046 | vma_page_aligned_bias (bfd_vma vma, ufile_ptr off, bfd_vma maxpagesize) | |
5047 | { | |
dc9155b2 NC |
5048 | /* PR binutils/16199: Handle an alignment of zero. */ |
5049 | if (maxpagesize == 0) | |
5050 | maxpagesize = 1; | |
340b6d91 AC |
5051 | return ((vma - off) % maxpagesize); |
5052 | } | |
5053 | ||
6d33f217 L |
5054 | static void |
5055 | print_segment_map (const struct elf_segment_map *m) | |
5056 | { | |
5057 | unsigned int j; | |
5058 | const char *pt = get_segment_type (m->p_type); | |
5059 | char buf[32]; | |
5060 | ||
5061 | if (pt == NULL) | |
5062 | { | |
5063 | if (m->p_type >= PT_LOPROC && m->p_type <= PT_HIPROC) | |
5064 | sprintf (buf, "LOPROC+%7.7x", | |
5065 | (unsigned int) (m->p_type - PT_LOPROC)); | |
5066 | else if (m->p_type >= PT_LOOS && m->p_type <= PT_HIOS) | |
5067 | sprintf (buf, "LOOS+%7.7x", | |
5068 | (unsigned int) (m->p_type - PT_LOOS)); | |
5069 | else | |
5070 | snprintf (buf, sizeof (buf), "%8.8x", | |
5071 | (unsigned int) m->p_type); | |
5072 | pt = buf; | |
5073 | } | |
4a97a0e5 | 5074 | fflush (stdout); |
6d33f217 L |
5075 | fprintf (stderr, "%s:", pt); |
5076 | for (j = 0; j < m->count; j++) | |
5077 | fprintf (stderr, " %s", m->sections [j]->name); | |
5078 | putc ('\n',stderr); | |
4a97a0e5 | 5079 | fflush (stderr); |
6d33f217 L |
5080 | } |
5081 | ||
32812159 AM |
5082 | static bfd_boolean |
5083 | write_zeros (bfd *abfd, file_ptr pos, bfd_size_type len) | |
5084 | { | |
5085 | void *buf; | |
5086 | bfd_boolean ret; | |
5087 | ||
5088 | if (bfd_seek (abfd, pos, SEEK_SET) != 0) | |
5089 | return FALSE; | |
5090 | buf = bfd_zmalloc (len); | |
5091 | if (buf == NULL) | |
5092 | return FALSE; | |
5093 | ret = bfd_bwrite (buf, len, abfd) == len; | |
5094 | free (buf); | |
5095 | return ret; | |
5096 | } | |
5097 | ||
252b5132 RH |
5098 | /* Assign file positions to the sections based on the mapping from |
5099 | sections to segments. This function also sets up some fields in | |
f3520d2f | 5100 | the file header. */ |
252b5132 | 5101 | |
b34976b6 | 5102 | static bfd_boolean |
f3520d2f AM |
5103 | assign_file_positions_for_load_sections (bfd *abfd, |
5104 | struct bfd_link_info *link_info) | |
252b5132 RH |
5105 | { |
5106 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
252b5132 | 5107 | struct elf_segment_map *m; |
252b5132 | 5108 | Elf_Internal_Phdr *phdrs; |
252b5132 | 5109 | Elf_Internal_Phdr *p; |
02bf8d82 | 5110 | file_ptr off; |
3f570048 | 5111 | bfd_size_type maxpagesize; |
f3520d2f | 5112 | unsigned int alloc; |
0920dee7 | 5113 | unsigned int i, j; |
2b0bc088 | 5114 | bfd_vma header_pad = 0; |
252b5132 | 5115 | |
e36284ab | 5116 | if (link_info == NULL |
ceae84aa | 5117 | && !_bfd_elf_map_sections_to_segments (abfd, link_info)) |
8ded5a0f | 5118 | return FALSE; |
252b5132 | 5119 | |
8ded5a0f | 5120 | alloc = 0; |
12bd6957 | 5121 | for (m = elf_seg_map (abfd); m != NULL; m = m->next) |
2b0bc088 NC |
5122 | { |
5123 | ++alloc; | |
5124 | if (m->header_size) | |
5125 | header_pad = m->header_size; | |
5126 | } | |
252b5132 | 5127 | |
82f2dbf7 NC |
5128 | if (alloc) |
5129 | { | |
5130 | elf_elfheader (abfd)->e_phoff = bed->s->sizeof_ehdr; | |
5131 | elf_elfheader (abfd)->e_phentsize = bed->s->sizeof_phdr; | |
5132 | } | |
5133 | else | |
5134 | { | |
5135 | /* PR binutils/12467. */ | |
5136 | elf_elfheader (abfd)->e_phoff = 0; | |
5137 | elf_elfheader (abfd)->e_phentsize = 0; | |
5138 | } | |
d324f6d6 | 5139 | |
8ded5a0f | 5140 | elf_elfheader (abfd)->e_phnum = alloc; |
252b5132 | 5141 | |
12bd6957 AM |
5142 | if (elf_program_header_size (abfd) == (bfd_size_type) -1) |
5143 | elf_program_header_size (abfd) = alloc * bed->s->sizeof_phdr; | |
8ded5a0f | 5144 | else |
12bd6957 | 5145 | BFD_ASSERT (elf_program_header_size (abfd) |
59e0647f | 5146 | >= alloc * bed->s->sizeof_phdr); |
252b5132 RH |
5147 | |
5148 | if (alloc == 0) | |
f3520d2f | 5149 | { |
12bd6957 | 5150 | elf_next_file_pos (abfd) = bed->s->sizeof_ehdr; |
8ded5a0f | 5151 | return TRUE; |
f3520d2f | 5152 | } |
252b5132 | 5153 | |
12bd6957 | 5154 | /* We're writing the size in elf_program_header_size (abfd), |
57268894 HPN |
5155 | see assign_file_positions_except_relocs, so make sure we have |
5156 | that amount allocated, with trailing space cleared. | |
12bd6957 AM |
5157 | The variable alloc contains the computed need, while |
5158 | elf_program_header_size (abfd) contains the size used for the | |
57268894 HPN |
5159 | layout. |
5160 | See ld/emultempl/elf-generic.em:gld${EMULATION_NAME}_map_segments | |
5161 | where the layout is forced to according to a larger size in the | |
5162 | last iterations for the testcase ld-elf/header. */ | |
12bd6957 | 5163 | BFD_ASSERT (elf_program_header_size (abfd) % bed->s->sizeof_phdr |
57268894 | 5164 | == 0); |
a50b1753 NC |
5165 | phdrs = (Elf_Internal_Phdr *) |
5166 | bfd_zalloc2 (abfd, | |
12bd6957 | 5167 | (elf_program_header_size (abfd) / bed->s->sizeof_phdr), |
a50b1753 | 5168 | sizeof (Elf_Internal_Phdr)); |
f3520d2f | 5169 | elf_tdata (abfd)->phdr = phdrs; |
252b5132 | 5170 | if (phdrs == NULL) |
b34976b6 | 5171 | return FALSE; |
252b5132 | 5172 | |
3f570048 AM |
5173 | maxpagesize = 1; |
5174 | if ((abfd->flags & D_PAGED) != 0) | |
5175 | maxpagesize = bed->maxpagesize; | |
5176 | ||
252b5132 RH |
5177 | off = bed->s->sizeof_ehdr; |
5178 | off += alloc * bed->s->sizeof_phdr; | |
2b0bc088 NC |
5179 | if (header_pad < (bfd_vma) off) |
5180 | header_pad = 0; | |
5181 | else | |
5182 | header_pad -= off; | |
5183 | off += header_pad; | |
252b5132 | 5184 | |
12bd6957 | 5185 | for (m = elf_seg_map (abfd), p = phdrs, j = 0; |
252b5132 | 5186 | m != NULL; |
0920dee7 | 5187 | m = m->next, p++, j++) |
252b5132 | 5188 | { |
252b5132 | 5189 | asection **secpp; |
bf988460 AM |
5190 | bfd_vma off_adjust; |
5191 | bfd_boolean no_contents; | |
252b5132 RH |
5192 | |
5193 | /* If elf_segment_map is not from map_sections_to_segments, the | |
08a40648 | 5194 | sections may not be correctly ordered. NOTE: sorting should |
52e9b619 MS |
5195 | not be done to the PT_NOTE section of a corefile, which may |
5196 | contain several pseudo-sections artificially created by bfd. | |
5197 | Sorting these pseudo-sections breaks things badly. */ | |
47d9a591 AM |
5198 | if (m->count > 1 |
5199 | && !(elf_elfheader (abfd)->e_type == ET_CORE | |
52e9b619 | 5200 | && m->p_type == PT_NOTE)) |
252b5132 RH |
5201 | qsort (m->sections, (size_t) m->count, sizeof (asection *), |
5202 | elf_sort_sections); | |
5203 | ||
b301b248 AM |
5204 | /* An ELF segment (described by Elf_Internal_Phdr) may contain a |
5205 | number of sections with contents contributing to both p_filesz | |
5206 | and p_memsz, followed by a number of sections with no contents | |
5207 | that just contribute to p_memsz. In this loop, OFF tracks next | |
02bf8d82 | 5208 | available file offset for PT_LOAD and PT_NOTE segments. */ |
252b5132 | 5209 | p->p_type = m->p_type; |
28a7f3e7 | 5210 | p->p_flags = m->p_flags; |
252b5132 | 5211 | |
3f570048 AM |
5212 | if (m->count == 0) |
5213 | p->p_vaddr = 0; | |
5214 | else | |
3271a814 | 5215 | p->p_vaddr = m->sections[0]->vma - m->p_vaddr_offset; |
3f570048 AM |
5216 | |
5217 | if (m->p_paddr_valid) | |
5218 | p->p_paddr = m->p_paddr; | |
5219 | else if (m->count == 0) | |
5220 | p->p_paddr = 0; | |
5221 | else | |
08a40648 | 5222 | p->p_paddr = m->sections[0]->lma - m->p_vaddr_offset; |
3f570048 AM |
5223 | |
5224 | if (p->p_type == PT_LOAD | |
5225 | && (abfd->flags & D_PAGED) != 0) | |
5226 | { | |
5227 | /* p_align in demand paged PT_LOAD segments effectively stores | |
5228 | the maximum page size. When copying an executable with | |
5229 | objcopy, we set m->p_align from the input file. Use this | |
5230 | value for maxpagesize rather than bed->maxpagesize, which | |
5231 | may be different. Note that we use maxpagesize for PT_TLS | |
5232 | segment alignment later in this function, so we are relying | |
5233 | on at least one PT_LOAD segment appearing before a PT_TLS | |
5234 | segment. */ | |
5235 | if (m->p_align_valid) | |
5236 | maxpagesize = m->p_align; | |
5237 | ||
5238 | p->p_align = maxpagesize; | |
5239 | } | |
3271a814 NS |
5240 | else if (m->p_align_valid) |
5241 | p->p_align = m->p_align; | |
e970b90a DJ |
5242 | else if (m->count == 0) |
5243 | p->p_align = 1 << bed->s->log_file_align; | |
3f570048 AM |
5244 | else |
5245 | p->p_align = 0; | |
5246 | ||
bf988460 AM |
5247 | no_contents = FALSE; |
5248 | off_adjust = 0; | |
252b5132 | 5249 | if (p->p_type == PT_LOAD |
b301b248 | 5250 | && m->count > 0) |
252b5132 | 5251 | { |
b301b248 | 5252 | bfd_size_type align; |
a49e53ed | 5253 | unsigned int align_power = 0; |
b301b248 | 5254 | |
3271a814 NS |
5255 | if (m->p_align_valid) |
5256 | align = p->p_align; | |
5257 | else | |
252b5132 | 5258 | { |
3271a814 NS |
5259 | for (i = 0, secpp = m->sections; i < m->count; i++, secpp++) |
5260 | { | |
5261 | unsigned int secalign; | |
08a40648 | 5262 | |
3271a814 NS |
5263 | secalign = bfd_get_section_alignment (abfd, *secpp); |
5264 | if (secalign > align_power) | |
5265 | align_power = secalign; | |
5266 | } | |
5267 | align = (bfd_size_type) 1 << align_power; | |
5268 | if (align < maxpagesize) | |
5269 | align = maxpagesize; | |
b301b248 | 5270 | } |
252b5132 | 5271 | |
02bf8d82 AM |
5272 | for (i = 0; i < m->count; i++) |
5273 | if ((m->sections[i]->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0) | |
5274 | /* If we aren't making room for this section, then | |
5275 | it must be SHT_NOBITS regardless of what we've | |
5276 | set via struct bfd_elf_special_section. */ | |
5277 | elf_section_type (m->sections[i]) = SHT_NOBITS; | |
5278 | ||
bf988460 | 5279 | /* Find out whether this segment contains any loadable |
aea274d3 AM |
5280 | sections. */ |
5281 | no_contents = TRUE; | |
5282 | for (i = 0; i < m->count; i++) | |
5283 | if (elf_section_type (m->sections[i]) != SHT_NOBITS) | |
5284 | { | |
5285 | no_contents = FALSE; | |
5286 | break; | |
5287 | } | |
bf988460 | 5288 | |
85cfcbfb | 5289 | off_adjust = vma_page_aligned_bias (p->p_vaddr, off, align); |
bf988460 AM |
5290 | off += off_adjust; |
5291 | if (no_contents) | |
5292 | { | |
5293 | /* We shouldn't need to align the segment on disk since | |
5294 | the segment doesn't need file space, but the gABI | |
5295 | arguably requires the alignment and glibc ld.so | |
5296 | checks it. So to comply with the alignment | |
5297 | requirement but not waste file space, we adjust | |
5298 | p_offset for just this segment. (OFF_ADJUST is | |
5299 | subtracted from OFF later.) This may put p_offset | |
5300 | past the end of file, but that shouldn't matter. */ | |
5301 | } | |
5302 | else | |
5303 | off_adjust = 0; | |
252b5132 | 5304 | } |
b1a6d0b1 NC |
5305 | /* Make sure the .dynamic section is the first section in the |
5306 | PT_DYNAMIC segment. */ | |
5307 | else if (p->p_type == PT_DYNAMIC | |
5308 | && m->count > 1 | |
5309 | && strcmp (m->sections[0]->name, ".dynamic") != 0) | |
5310 | { | |
5311 | _bfd_error_handler | |
b301b248 AM |
5312 | (_("%B: The first section in the PT_DYNAMIC segment is not the .dynamic section"), |
5313 | abfd); | |
b1a6d0b1 NC |
5314 | bfd_set_error (bfd_error_bad_value); |
5315 | return FALSE; | |
5316 | } | |
3f001e84 JK |
5317 | /* Set the note section type to SHT_NOTE. */ |
5318 | else if (p->p_type == PT_NOTE) | |
5319 | for (i = 0; i < m->count; i++) | |
5320 | elf_section_type (m->sections[i]) = SHT_NOTE; | |
252b5132 | 5321 | |
252b5132 RH |
5322 | p->p_offset = 0; |
5323 | p->p_filesz = 0; | |
5324 | p->p_memsz = 0; | |
5325 | ||
5326 | if (m->includes_filehdr) | |
5327 | { | |
bf988460 | 5328 | if (!m->p_flags_valid) |
252b5132 | 5329 | p->p_flags |= PF_R; |
252b5132 RH |
5330 | p->p_filesz = bed->s->sizeof_ehdr; |
5331 | p->p_memsz = bed->s->sizeof_ehdr; | |
5332 | if (m->count > 0) | |
5333 | { | |
9ab82472 L |
5334 | if (p->p_vaddr < (bfd_vma) off |
5335 | || (!m->p_paddr_valid | |
5336 | && p->p_paddr < (bfd_vma) off)) | |
252b5132 | 5337 | { |
4eca0228 | 5338 | _bfd_error_handler |
b301b248 AM |
5339 | (_("%B: Not enough room for program headers, try linking with -N"), |
5340 | abfd); | |
252b5132 | 5341 | bfd_set_error (bfd_error_bad_value); |
b34976b6 | 5342 | return FALSE; |
252b5132 RH |
5343 | } |
5344 | ||
5345 | p->p_vaddr -= off; | |
bf988460 | 5346 | if (!m->p_paddr_valid) |
252b5132 RH |
5347 | p->p_paddr -= off; |
5348 | } | |
252b5132 RH |
5349 | } |
5350 | ||
5351 | if (m->includes_phdrs) | |
5352 | { | |
bf988460 | 5353 | if (!m->p_flags_valid) |
252b5132 RH |
5354 | p->p_flags |= PF_R; |
5355 | ||
f3520d2f | 5356 | if (!m->includes_filehdr) |
252b5132 RH |
5357 | { |
5358 | p->p_offset = bed->s->sizeof_ehdr; | |
5359 | ||
5360 | if (m->count > 0) | |
5361 | { | |
252b5132 | 5362 | p->p_vaddr -= off - p->p_offset; |
bf988460 | 5363 | if (!m->p_paddr_valid) |
252b5132 RH |
5364 | p->p_paddr -= off - p->p_offset; |
5365 | } | |
252b5132 RH |
5366 | } |
5367 | ||
5368 | p->p_filesz += alloc * bed->s->sizeof_phdr; | |
5369 | p->p_memsz += alloc * bed->s->sizeof_phdr; | |
2b0bc088 NC |
5370 | if (m->count) |
5371 | { | |
5372 | p->p_filesz += header_pad; | |
5373 | p->p_memsz += header_pad; | |
5374 | } | |
252b5132 RH |
5375 | } |
5376 | ||
5377 | if (p->p_type == PT_LOAD | |
5378 | || (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core)) | |
5379 | { | |
bf988460 | 5380 | if (!m->includes_filehdr && !m->includes_phdrs) |
02bf8d82 | 5381 | p->p_offset = off; |
252b5132 RH |
5382 | else |
5383 | { | |
5384 | file_ptr adjust; | |
5385 | ||
5386 | adjust = off - (p->p_offset + p->p_filesz); | |
bf988460 AM |
5387 | if (!no_contents) |
5388 | p->p_filesz += adjust; | |
252b5132 RH |
5389 | p->p_memsz += adjust; |
5390 | } | |
5391 | } | |
5392 | ||
1ea63fd2 AM |
5393 | /* Set up p_filesz, p_memsz, p_align and p_flags from the section |
5394 | maps. Set filepos for sections in PT_LOAD segments, and in | |
5395 | core files, for sections in PT_NOTE segments. | |
5396 | assign_file_positions_for_non_load_sections will set filepos | |
5397 | for other sections and update p_filesz for other segments. */ | |
252b5132 RH |
5398 | for (i = 0, secpp = m->sections; i < m->count; i++, secpp++) |
5399 | { | |
5400 | asection *sec; | |
252b5132 | 5401 | bfd_size_type align; |
627b32bc | 5402 | Elf_Internal_Shdr *this_hdr; |
252b5132 RH |
5403 | |
5404 | sec = *secpp; | |
02bf8d82 | 5405 | this_hdr = &elf_section_data (sec)->this_hdr; |
3f570048 | 5406 | align = (bfd_size_type) 1 << bfd_get_section_alignment (abfd, sec); |
252b5132 | 5407 | |
88967714 AM |
5408 | if ((p->p_type == PT_LOAD |
5409 | || p->p_type == PT_TLS) | |
5410 | && (this_hdr->sh_type != SHT_NOBITS | |
5411 | || ((this_hdr->sh_flags & SHF_ALLOC) != 0 | |
5412 | && ((this_hdr->sh_flags & SHF_TLS) == 0 | |
5413 | || p->p_type == PT_TLS)))) | |
252b5132 | 5414 | { |
b5599592 AM |
5415 | bfd_vma p_start = p->p_paddr; |
5416 | bfd_vma p_end = p_start + p->p_memsz; | |
5417 | bfd_vma s_start = sec->lma; | |
5418 | bfd_vma adjust = s_start - p_end; | |
252b5132 | 5419 | |
a2d1e028 L |
5420 | if (adjust != 0 |
5421 | && (s_start < p_end | |
5422 | || p_end < p_start)) | |
252b5132 | 5423 | { |
4eca0228 | 5424 | _bfd_error_handler |
695344c0 | 5425 | /* xgettext:c-format */ |
b5599592 AM |
5426 | (_("%B: section %A lma %#lx adjusted to %#lx"), abfd, sec, |
5427 | (unsigned long) s_start, (unsigned long) p_end); | |
88967714 | 5428 | adjust = 0; |
b5599592 | 5429 | sec->lma = p_end; |
1cfb7d1e | 5430 | } |
3ac9b6c9 | 5431 | p->p_memsz += adjust; |
1cfb7d1e | 5432 | |
88967714 AM |
5433 | if (this_hdr->sh_type != SHT_NOBITS) |
5434 | { | |
32812159 AM |
5435 | if (p->p_filesz + adjust < p->p_memsz) |
5436 | { | |
5437 | /* We have a PROGBITS section following NOBITS ones. | |
5438 | Allocate file space for the NOBITS section(s) and | |
5439 | zero it. */ | |
5440 | adjust = p->p_memsz - p->p_filesz; | |
5441 | if (!write_zeros (abfd, off, adjust)) | |
5442 | return FALSE; | |
5443 | } | |
88967714 AM |
5444 | off += adjust; |
5445 | p->p_filesz += adjust; | |
252b5132 | 5446 | } |
252b5132 RH |
5447 | } |
5448 | ||
5449 | if (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core) | |
5450 | { | |
b301b248 AM |
5451 | /* The section at i == 0 is the one that actually contains |
5452 | everything. */ | |
4a938328 MS |
5453 | if (i == 0) |
5454 | { | |
627b32bc | 5455 | this_hdr->sh_offset = sec->filepos = off; |
6a3cd2b4 AM |
5456 | off += this_hdr->sh_size; |
5457 | p->p_filesz = this_hdr->sh_size; | |
b301b248 AM |
5458 | p->p_memsz = 0; |
5459 | p->p_align = 1; | |
252b5132 | 5460 | } |
4a938328 | 5461 | else |
252b5132 | 5462 | { |
b301b248 | 5463 | /* The rest are fake sections that shouldn't be written. */ |
252b5132 | 5464 | sec->filepos = 0; |
eea6121a | 5465 | sec->size = 0; |
b301b248 AM |
5466 | sec->flags = 0; |
5467 | continue; | |
252b5132 | 5468 | } |
252b5132 RH |
5469 | } |
5470 | else | |
5471 | { | |
1e951488 | 5472 | if (p->p_type == PT_LOAD) |
b301b248 | 5473 | { |
1e951488 AM |
5474 | this_hdr->sh_offset = sec->filepos = off; |
5475 | if (this_hdr->sh_type != SHT_NOBITS) | |
5476 | off += this_hdr->sh_size; | |
5477 | } | |
5478 | else if (this_hdr->sh_type == SHT_NOBITS | |
5479 | && (this_hdr->sh_flags & SHF_TLS) != 0 | |
5480 | && this_hdr->sh_offset == 0) | |
5481 | { | |
5482 | /* This is a .tbss section that didn't get a PT_LOAD. | |
5483 | (See _bfd_elf_map_sections_to_segments "Create a | |
5484 | final PT_LOAD".) Set sh_offset to the value it | |
5485 | would have if we had created a zero p_filesz and | |
5486 | p_memsz PT_LOAD header for the section. This | |
5487 | also makes the PT_TLS header have the same | |
5488 | p_offset value. */ | |
5489 | bfd_vma adjust = vma_page_aligned_bias (this_hdr->sh_addr, | |
5490 | off, align); | |
5491 | this_hdr->sh_offset = sec->filepos = off + adjust; | |
b301b248 | 5492 | } |
252b5132 | 5493 | |
02bf8d82 | 5494 | if (this_hdr->sh_type != SHT_NOBITS) |
b301b248 | 5495 | { |
6a3cd2b4 | 5496 | p->p_filesz += this_hdr->sh_size; |
02bf8d82 AM |
5497 | /* A load section without SHF_ALLOC is something like |
5498 | a note section in a PT_NOTE segment. These take | |
5499 | file space but are not loaded into memory. */ | |
5500 | if ((this_hdr->sh_flags & SHF_ALLOC) != 0) | |
6a3cd2b4 | 5501 | p->p_memsz += this_hdr->sh_size; |
b301b248 | 5502 | } |
6a3cd2b4 | 5503 | else if ((this_hdr->sh_flags & SHF_ALLOC) != 0) |
13ae64f3 | 5504 | { |
6a3cd2b4 AM |
5505 | if (p->p_type == PT_TLS) |
5506 | p->p_memsz += this_hdr->sh_size; | |
5507 | ||
5508 | /* .tbss is special. It doesn't contribute to p_memsz of | |
5509 | normal segments. */ | |
5510 | else if ((this_hdr->sh_flags & SHF_TLS) == 0) | |
5511 | p->p_memsz += this_hdr->sh_size; | |
13ae64f3 JJ |
5512 | } |
5513 | ||
b10a8ae0 L |
5514 | if (align > p->p_align |
5515 | && !m->p_align_valid | |
5516 | && (p->p_type != PT_LOAD | |
5517 | || (abfd->flags & D_PAGED) == 0)) | |
252b5132 RH |
5518 | p->p_align = align; |
5519 | } | |
5520 | ||
bf988460 | 5521 | if (!m->p_flags_valid) |
252b5132 RH |
5522 | { |
5523 | p->p_flags |= PF_R; | |
02bf8d82 | 5524 | if ((this_hdr->sh_flags & SHF_EXECINSTR) != 0) |
252b5132 | 5525 | p->p_flags |= PF_X; |
02bf8d82 | 5526 | if ((this_hdr->sh_flags & SHF_WRITE) != 0) |
252b5132 RH |
5527 | p->p_flags |= PF_W; |
5528 | } | |
5529 | } | |
43a8475c | 5530 | |
bf988460 | 5531 | off -= off_adjust; |
0920dee7 | 5532 | |
7c928300 AM |
5533 | /* Check that all sections are in a PT_LOAD segment. |
5534 | Don't check funky gdb generated core files. */ | |
5535 | if (p->p_type == PT_LOAD && bfd_get_format (abfd) != bfd_core) | |
9a83a553 AM |
5536 | { |
5537 | bfd_boolean check_vma = TRUE; | |
5538 | ||
5539 | for (i = 1; i < m->count; i++) | |
5540 | if (m->sections[i]->vma == m->sections[i - 1]->vma | |
5541 | && ELF_SECTION_SIZE (&(elf_section_data (m->sections[i]) | |
5542 | ->this_hdr), p) != 0 | |
5543 | && ELF_SECTION_SIZE (&(elf_section_data (m->sections[i - 1]) | |
5544 | ->this_hdr), p) != 0) | |
0920dee7 | 5545 | { |
9a83a553 AM |
5546 | /* Looks like we have overlays packed into the segment. */ |
5547 | check_vma = FALSE; | |
5548 | break; | |
0920dee7 | 5549 | } |
9a83a553 AM |
5550 | |
5551 | for (i = 0; i < m->count; i++) | |
5552 | { | |
5553 | Elf_Internal_Shdr *this_hdr; | |
5554 | asection *sec; | |
5555 | ||
5556 | sec = m->sections[i]; | |
5557 | this_hdr = &(elf_section_data(sec)->this_hdr); | |
86b2281f AM |
5558 | if (!ELF_SECTION_IN_SEGMENT_1 (this_hdr, p, check_vma, 0) |
5559 | && !ELF_TBSS_SPECIAL (this_hdr, p)) | |
9a83a553 | 5560 | { |
4eca0228 | 5561 | _bfd_error_handler |
695344c0 | 5562 | /* xgettext:c-format */ |
9a83a553 AM |
5563 | (_("%B: section `%A' can't be allocated in segment %d"), |
5564 | abfd, sec, j); | |
5565 | print_segment_map (m); | |
5566 | } | |
5567 | } | |
5568 | } | |
252b5132 RH |
5569 | } |
5570 | ||
12bd6957 | 5571 | elf_next_file_pos (abfd) = off; |
f3520d2f AM |
5572 | return TRUE; |
5573 | } | |
5574 | ||
5575 | /* Assign file positions for the other sections. */ | |
5576 | ||
5577 | static bfd_boolean | |
5578 | assign_file_positions_for_non_load_sections (bfd *abfd, | |
5579 | struct bfd_link_info *link_info) | |
5580 | { | |
5581 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
5582 | Elf_Internal_Shdr **i_shdrpp; | |
e06efbf1 | 5583 | Elf_Internal_Shdr **hdrpp, **end_hdrpp; |
f3520d2f AM |
5584 | Elf_Internal_Phdr *phdrs; |
5585 | Elf_Internal_Phdr *p; | |
5586 | struct elf_segment_map *m; | |
62655c7b | 5587 | struct elf_segment_map *hdrs_segment; |
f3520d2f AM |
5588 | bfd_vma filehdr_vaddr, filehdr_paddr; |
5589 | bfd_vma phdrs_vaddr, phdrs_paddr; | |
5590 | file_ptr off; | |
f3520d2f AM |
5591 | unsigned int count; |
5592 | ||
5c182d5f | 5593 | i_shdrpp = elf_elfsections (abfd); |
e06efbf1 | 5594 | end_hdrpp = i_shdrpp + elf_numsections (abfd); |
12bd6957 | 5595 | off = elf_next_file_pos (abfd); |
e06efbf1 | 5596 | for (hdrpp = i_shdrpp + 1; hdrpp < end_hdrpp; hdrpp++) |
5c182d5f | 5597 | { |
5c182d5f AM |
5598 | Elf_Internal_Shdr *hdr; |
5599 | ||
5600 | hdr = *hdrpp; | |
5601 | if (hdr->bfd_section != NULL | |
252e386e AM |
5602 | && (hdr->bfd_section->filepos != 0 |
5603 | || (hdr->sh_type == SHT_NOBITS | |
5604 | && hdr->contents == NULL))) | |
627b32bc | 5605 | BFD_ASSERT (hdr->sh_offset == hdr->bfd_section->filepos); |
5c182d5f AM |
5606 | else if ((hdr->sh_flags & SHF_ALLOC) != 0) |
5607 | { | |
e8d2ba53 | 5608 | if (hdr->sh_size != 0) |
4eca0228 | 5609 | _bfd_error_handler |
695344c0 | 5610 | /* xgettext:c-format */ |
e8d2ba53 AM |
5611 | (_("%B: warning: allocated section `%s' not in segment"), |
5612 | abfd, | |
5613 | (hdr->bfd_section == NULL | |
5614 | ? "*unknown*" | |
5615 | : hdr->bfd_section->name)); | |
3ba71138 L |
5616 | /* We don't need to page align empty sections. */ |
5617 | if ((abfd->flags & D_PAGED) != 0 && hdr->sh_size != 0) | |
5c182d5f AM |
5618 | off += vma_page_aligned_bias (hdr->sh_addr, off, |
5619 | bed->maxpagesize); | |
5620 | else | |
5621 | off += vma_page_aligned_bias (hdr->sh_addr, off, | |
5622 | hdr->sh_addralign); | |
5623 | off = _bfd_elf_assign_file_position_for_section (hdr, off, | |
5624 | FALSE); | |
5625 | } | |
5626 | else if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA) | |
5627 | && hdr->bfd_section == NULL) | |
0ce398f1 L |
5628 | || (hdr->bfd_section != NULL |
5629 | && (hdr->bfd_section->flags & SEC_ELF_COMPRESS)) | |
5630 | /* Compress DWARF debug sections. */ | |
12bd6957 | 5631 | || hdr == i_shdrpp[elf_onesymtab (abfd)] |
6a40cf0c NC |
5632 | || (elf_symtab_shndx_list (abfd) != NULL |
5633 | && hdr == i_shdrpp[elf_symtab_shndx_list (abfd)->ndx]) | |
3e19fb8f L |
5634 | || hdr == i_shdrpp[elf_strtab_sec (abfd)] |
5635 | || hdr == i_shdrpp[elf_shstrtab_sec (abfd)]) | |
5c182d5f AM |
5636 | hdr->sh_offset = -1; |
5637 | else | |
5638 | off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE); | |
5c182d5f AM |
5639 | } |
5640 | ||
252b5132 RH |
5641 | /* Now that we have set the section file positions, we can set up |
5642 | the file positions for the non PT_LOAD segments. */ | |
f3520d2f AM |
5643 | count = 0; |
5644 | filehdr_vaddr = 0; | |
5645 | filehdr_paddr = 0; | |
5646 | phdrs_vaddr = bed->maxpagesize + bed->s->sizeof_ehdr; | |
5647 | phdrs_paddr = 0; | |
62655c7b | 5648 | hdrs_segment = NULL; |
f3520d2f | 5649 | phdrs = elf_tdata (abfd)->phdr; |
12bd6957 | 5650 | for (m = elf_seg_map (abfd), p = phdrs; m != NULL; m = m->next, p++) |
f3520d2f AM |
5651 | { |
5652 | ++count; | |
5653 | if (p->p_type != PT_LOAD) | |
5654 | continue; | |
5655 | ||
5656 | if (m->includes_filehdr) | |
5657 | { | |
5658 | filehdr_vaddr = p->p_vaddr; | |
5659 | filehdr_paddr = p->p_paddr; | |
5660 | } | |
5661 | if (m->includes_phdrs) | |
5662 | { | |
5663 | phdrs_vaddr = p->p_vaddr; | |
5664 | phdrs_paddr = p->p_paddr; | |
5665 | if (m->includes_filehdr) | |
5666 | { | |
62655c7b | 5667 | hdrs_segment = m; |
f3520d2f AM |
5668 | phdrs_vaddr += bed->s->sizeof_ehdr; |
5669 | phdrs_paddr += bed->s->sizeof_ehdr; | |
5670 | } | |
5671 | } | |
5672 | } | |
5673 | ||
62655c7b RM |
5674 | if (hdrs_segment != NULL && link_info != NULL) |
5675 | { | |
5676 | /* There is a segment that contains both the file headers and the | |
5677 | program headers, so provide a symbol __ehdr_start pointing there. | |
5678 | A program can use this to examine itself robustly. */ | |
5679 | ||
5680 | struct elf_link_hash_entry *hash | |
5681 | = elf_link_hash_lookup (elf_hash_table (link_info), "__ehdr_start", | |
5682 | FALSE, FALSE, TRUE); | |
5683 | /* If the symbol was referenced and not defined, define it. */ | |
5684 | if (hash != NULL | |
5685 | && (hash->root.type == bfd_link_hash_new | |
5686 | || hash->root.type == bfd_link_hash_undefined | |
5687 | || hash->root.type == bfd_link_hash_undefweak | |
5688 | || hash->root.type == bfd_link_hash_common)) | |
5689 | { | |
5690 | asection *s = NULL; | |
5691 | if (hdrs_segment->count != 0) | |
5692 | /* The segment contains sections, so use the first one. */ | |
5693 | s = hdrs_segment->sections[0]; | |
5694 | else | |
5695 | /* Use the first (i.e. lowest-addressed) section in any segment. */ | |
12bd6957 | 5696 | for (m = elf_seg_map (abfd); m != NULL; m = m->next) |
62655c7b RM |
5697 | if (m->count != 0) |
5698 | { | |
5699 | s = m->sections[0]; | |
5700 | break; | |
5701 | } | |
5702 | ||
5703 | if (s != NULL) | |
5704 | { | |
5705 | hash->root.u.def.value = filehdr_vaddr - s->vma; | |
5706 | hash->root.u.def.section = s; | |
5707 | } | |
5708 | else | |
5709 | { | |
5710 | hash->root.u.def.value = filehdr_vaddr; | |
5711 | hash->root.u.def.section = bfd_abs_section_ptr; | |
5712 | } | |
5713 | ||
5714 | hash->root.type = bfd_link_hash_defined; | |
5715 | hash->def_regular = 1; | |
5716 | hash->non_elf = 0; | |
5717 | } | |
5718 | } | |
5719 | ||
12bd6957 | 5720 | for (m = elf_seg_map (abfd), p = phdrs; m != NULL; m = m->next, p++) |
252b5132 | 5721 | { |
129af99f | 5722 | if (p->p_type == PT_GNU_RELRO) |
252b5132 | 5723 | { |
b84a33b5 | 5724 | const Elf_Internal_Phdr *lp; |
3146fac4 | 5725 | struct elf_segment_map *lm; |
1ea63fd2 | 5726 | |
129af99f | 5727 | if (link_info != NULL) |
8c37241b | 5728 | { |
129af99f AS |
5729 | /* During linking the range of the RELRO segment is passed |
5730 | in link_info. */ | |
12bd6957 | 5731 | for (lm = elf_seg_map (abfd), lp = phdrs; |
3146fac4 AM |
5732 | lm != NULL; |
5733 | lm = lm->next, lp++) | |
8c37241b JJ |
5734 | { |
5735 | if (lp->p_type == PT_LOAD | |
b84a33b5 | 5736 | && lp->p_vaddr < link_info->relro_end |
3146fac4 AM |
5737 | && lm->count != 0 |
5738 | && lm->sections[0]->vma >= link_info->relro_start) | |
8c37241b JJ |
5739 | break; |
5740 | } | |
8981c88a | 5741 | |
3146fac4 | 5742 | BFD_ASSERT (lm != NULL); |
8c37241b | 5743 | } |
129af99f AS |
5744 | else |
5745 | { | |
5746 | /* Otherwise we are copying an executable or shared | |
b84a33b5 | 5747 | library, but we need to use the same linker logic. */ |
129af99f AS |
5748 | for (lp = phdrs; lp < phdrs + count; ++lp) |
5749 | { | |
5750 | if (lp->p_type == PT_LOAD | |
5751 | && lp->p_paddr == p->p_paddr) | |
5752 | break; | |
5753 | } | |
b84a33b5 AM |
5754 | } |
5755 | ||
5756 | if (lp < phdrs + count) | |
5757 | { | |
5758 | p->p_vaddr = lp->p_vaddr; | |
5759 | p->p_paddr = lp->p_paddr; | |
5760 | p->p_offset = lp->p_offset; | |
5761 | if (link_info != NULL) | |
5762 | p->p_filesz = link_info->relro_end - lp->p_vaddr; | |
5763 | else if (m->p_size_valid) | |
5764 | p->p_filesz = m->p_size; | |
129af99f AS |
5765 | else |
5766 | abort (); | |
b84a33b5 | 5767 | p->p_memsz = p->p_filesz; |
f3944f72 L |
5768 | /* Preserve the alignment and flags if they are valid. The |
5769 | gold linker generates RW/4 for the PT_GNU_RELRO section. | |
5770 | It is better for objcopy/strip to honor these attributes | |
5771 | otherwise gdb will choke when using separate debug files. | |
5772 | */ | |
5773 | if (!m->p_align_valid) | |
5774 | p->p_align = 1; | |
5775 | if (!m->p_flags_valid) | |
341b8ee7 | 5776 | p->p_flags = PF_R; |
129af99f | 5777 | } |
9433b9b1 | 5778 | else |
b84a33b5 AM |
5779 | { |
5780 | memset (p, 0, sizeof *p); | |
5781 | p->p_type = PT_NULL; | |
5782 | } | |
129af99f | 5783 | } |
04c3a755 NS |
5784 | else if (p->p_type == PT_GNU_STACK) |
5785 | { | |
5786 | if (m->p_size_valid) | |
5787 | p->p_memsz = m->p_size; | |
5788 | } | |
129af99f AS |
5789 | else if (m->count != 0) |
5790 | { | |
e06efbf1 | 5791 | unsigned int i; |
1a9ccd70 | 5792 | |
129af99f AS |
5793 | if (p->p_type != PT_LOAD |
5794 | && (p->p_type != PT_NOTE | |
5795 | || bfd_get_format (abfd) != bfd_core)) | |
5796 | { | |
1a9ccd70 NC |
5797 | /* A user specified segment layout may include a PHDR |
5798 | segment that overlaps with a LOAD segment... */ | |
5799 | if (p->p_type == PT_PHDR) | |
5800 | { | |
5801 | m->count = 0; | |
5802 | continue; | |
5803 | } | |
5804 | ||
c86934ce NC |
5805 | if (m->includes_filehdr || m->includes_phdrs) |
5806 | { | |
b1fa9dd6 | 5807 | /* PR 17512: file: 2195325e. */ |
4eca0228 | 5808 | _bfd_error_handler |
1a9ccd70 NC |
5809 | (_("%B: error: non-load segment %d includes file header and/or program header"), |
5810 | abfd, (int)(p - phdrs)); | |
c86934ce NC |
5811 | return FALSE; |
5812 | } | |
129af99f | 5813 | |
86b2281f | 5814 | p->p_filesz = 0; |
129af99f | 5815 | p->p_offset = m->sections[0]->filepos; |
86b2281f AM |
5816 | for (i = m->count; i-- != 0;) |
5817 | { | |
5818 | asection *sect = m->sections[i]; | |
5819 | Elf_Internal_Shdr *hdr = &elf_section_data (sect)->this_hdr; | |
5820 | if (hdr->sh_type != SHT_NOBITS) | |
5821 | { | |
5822 | p->p_filesz = (sect->filepos - m->sections[0]->filepos | |
5823 | + hdr->sh_size); | |
5824 | break; | |
5825 | } | |
5826 | } | |
129af99f AS |
5827 | } |
5828 | } | |
5829 | else if (m->includes_filehdr) | |
5830 | { | |
5831 | p->p_vaddr = filehdr_vaddr; | |
5832 | if (! m->p_paddr_valid) | |
5833 | p->p_paddr = filehdr_paddr; | |
5834 | } | |
5835 | else if (m->includes_phdrs) | |
5836 | { | |
5837 | p->p_vaddr = phdrs_vaddr; | |
5838 | if (! m->p_paddr_valid) | |
5839 | p->p_paddr = phdrs_paddr; | |
252b5132 RH |
5840 | } |
5841 | } | |
5842 | ||
12bd6957 | 5843 | elf_next_file_pos (abfd) = off; |
252b5132 | 5844 | |
b34976b6 | 5845 | return TRUE; |
252b5132 RH |
5846 | } |
5847 | ||
6a40cf0c NC |
5848 | static elf_section_list * |
5849 | find_section_in_list (unsigned int i, elf_section_list * list) | |
5850 | { | |
5851 | for (;list != NULL; list = list->next) | |
5852 | if (list->ndx == i) | |
5853 | break; | |
5854 | return list; | |
5855 | } | |
5856 | ||
1a9ccd70 NC |
5857 | /* Compare function used when sorting the program header table. |
5858 | The ELF standard requires that a PT_PHDR segment, if present, | |
5859 | must appear before any PT_LOAD segments. It also requires | |
5860 | that all PT_LOAD segments are sorted into order of increasing | |
5861 | p_vaddr. */ | |
5862 | ||
5863 | static signed int | |
5864 | phdr_sorter (const void * a, const void * b) | |
5865 | { | |
5866 | Elf_Internal_Phdr * ahdr = (Elf_Internal_Phdr *) a; | |
5867 | Elf_Internal_Phdr * bhdr = (Elf_Internal_Phdr *) b; | |
5868 | ||
5869 | switch (ahdr->p_type) | |
5870 | { | |
5871 | case PT_LOAD: | |
5872 | switch (bhdr->p_type) | |
5873 | { | |
5874 | case PT_PHDR: | |
5875 | return 1; | |
5876 | case PT_LOAD: | |
5877 | if (ahdr->p_vaddr < bhdr->p_vaddr) | |
5878 | return -1; | |
5879 | if (ahdr->p_vaddr > bhdr->p_vaddr) | |
5880 | return 1; | |
5881 | return 0; | |
5882 | default: | |
5883 | return 0; | |
5884 | } | |
5885 | break; | |
5886 | case PT_PHDR: | |
5887 | switch (bhdr->p_type) | |
5888 | { | |
5889 | case PT_PHDR: | |
5890 | _bfd_error_handler (_("error: multiple PHDR segments detecetd")); | |
5891 | return 0; | |
5892 | case PT_LOAD: | |
5893 | return -1; | |
5894 | default: | |
5895 | return 0; | |
5896 | } | |
5897 | break; | |
5898 | default: | |
5899 | return 0; | |
5900 | } | |
5901 | } | |
5902 | ||
252b5132 RH |
5903 | /* Work out the file positions of all the sections. This is called by |
5904 | _bfd_elf_compute_section_file_positions. All the section sizes and | |
5905 | VMAs must be known before this is called. | |
5906 | ||
e0638f70 AM |
5907 | Reloc sections come in two flavours: Those processed specially as |
5908 | "side-channel" data attached to a section to which they apply, and | |
5909 | those that bfd doesn't process as relocations. The latter sort are | |
5910 | stored in a normal bfd section by bfd_section_from_shdr. We don't | |
5911 | consider the former sort here, unless they form part of the loadable | |
5912 | image. Reloc sections not assigned here will be handled later by | |
5913 | assign_file_positions_for_relocs. | |
252b5132 RH |
5914 | |
5915 | We also don't set the positions of the .symtab and .strtab here. */ | |
5916 | ||
b34976b6 | 5917 | static bfd_boolean |
c84fca4d AO |
5918 | assign_file_positions_except_relocs (bfd *abfd, |
5919 | struct bfd_link_info *link_info) | |
252b5132 | 5920 | { |
5c182d5f AM |
5921 | struct elf_obj_tdata *tdata = elf_tdata (abfd); |
5922 | Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd); | |
9c5bfbb7 | 5923 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
252b5132 RH |
5924 | |
5925 | if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0 | |
5926 | && bfd_get_format (abfd) != bfd_core) | |
5927 | { | |
5c182d5f AM |
5928 | Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd); |
5929 | unsigned int num_sec = elf_numsections (abfd); | |
252b5132 RH |
5930 | Elf_Internal_Shdr **hdrpp; |
5931 | unsigned int i; | |
a485e98e | 5932 | file_ptr off; |
252b5132 RH |
5933 | |
5934 | /* Start after the ELF header. */ | |
5935 | off = i_ehdrp->e_ehsize; | |
5936 | ||
5937 | /* We are not creating an executable, which means that we are | |
5938 | not creating a program header, and that the actual order of | |
5939 | the sections in the file is unimportant. */ | |
9ad5cbcf | 5940 | for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++) |
252b5132 RH |
5941 | { |
5942 | Elf_Internal_Shdr *hdr; | |
5943 | ||
5944 | hdr = *hdrpp; | |
e0638f70 AM |
5945 | if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA) |
5946 | && hdr->bfd_section == NULL) | |
0ce398f1 L |
5947 | || (hdr->bfd_section != NULL |
5948 | && (hdr->bfd_section->flags & SEC_ELF_COMPRESS)) | |
5949 | /* Compress DWARF debug sections. */ | |
12bd6957 | 5950 | || i == elf_onesymtab (abfd) |
6a40cf0c NC |
5951 | || (elf_symtab_shndx_list (abfd) != NULL |
5952 | && hdr == i_shdrpp[elf_symtab_shndx_list (abfd)->ndx]) | |
3e19fb8f L |
5953 | || i == elf_strtab_sec (abfd) |
5954 | || i == elf_shstrtab_sec (abfd)) | |
252b5132 RH |
5955 | { |
5956 | hdr->sh_offset = -1; | |
252b5132 | 5957 | } |
9ad5cbcf | 5958 | else |
b34976b6 | 5959 | off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE); |
252b5132 | 5960 | } |
a485e98e AM |
5961 | |
5962 | elf_next_file_pos (abfd) = off; | |
252b5132 RH |
5963 | } |
5964 | else | |
5965 | { | |
1a9ccd70 | 5966 | Elf_Internal_Phdr * map; |
f3520d2f AM |
5967 | unsigned int alloc; |
5968 | ||
252b5132 | 5969 | /* Assign file positions for the loaded sections based on the |
08a40648 | 5970 | assignment of sections to segments. */ |
f3520d2f AM |
5971 | if (!assign_file_positions_for_load_sections (abfd, link_info)) |
5972 | return FALSE; | |
5973 | ||
5974 | /* And for non-load sections. */ | |
5975 | if (!assign_file_positions_for_non_load_sections (abfd, link_info)) | |
5976 | return FALSE; | |
5977 | ||
e36284ab AM |
5978 | if (bed->elf_backend_modify_program_headers != NULL) |
5979 | { | |
5980 | if (!(*bed->elf_backend_modify_program_headers) (abfd, link_info)) | |
5981 | return FALSE; | |
5982 | } | |
5983 | ||
58e7ebac | 5984 | /* Set e_type in ELF header to ET_EXEC for -pie -Ttext-segment=. */ |
0e1862bb | 5985 | if (link_info != NULL && bfd_link_pie (link_info)) |
58e7ebac L |
5986 | { |
5987 | unsigned int num_segments = elf_elfheader (abfd)->e_phnum; | |
5988 | Elf_Internal_Phdr *segment = elf_tdata (abfd)->phdr; | |
5989 | Elf_Internal_Phdr *end_segment = &segment[num_segments]; | |
5990 | ||
5991 | /* Find the lowest p_vaddr in PT_LOAD segments. */ | |
5992 | bfd_vma p_vaddr = (bfd_vma) -1; | |
5993 | for (; segment < end_segment; segment++) | |
5994 | if (segment->p_type == PT_LOAD && p_vaddr > segment->p_vaddr) | |
5995 | p_vaddr = segment->p_vaddr; | |
5996 | ||
5997 | /* Set e_type to ET_EXEC if the lowest p_vaddr in PT_LOAD | |
5998 | segments is non-zero. */ | |
5999 | if (p_vaddr) | |
6000 | i_ehdrp->e_type = ET_EXEC; | |
6001 | } | |
6002 | ||
f3520d2f | 6003 | /* Write out the program headers. */ |
12bd6957 | 6004 | alloc = elf_program_header_size (abfd) / bed->s->sizeof_phdr; |
1a9ccd70 NC |
6005 | |
6006 | /* Sort the program headers into the ordering required by the ELF standard. */ | |
6007 | if (alloc == 0) | |
6008 | return TRUE; | |
6009 | ||
6010 | map = (Elf_Internal_Phdr *) xmalloc (alloc * sizeof (* tdata->phdr)); | |
6011 | memcpy (map, tdata->phdr, alloc * sizeof (* tdata->phdr)); | |
6012 | qsort (map, alloc, sizeof (* tdata->phdr), phdr_sorter); | |
6013 | ||
6014 | /* PR ld/20815 - Check that the program header segment, if present, will | |
6015 | be loaded into memory. FIXME: The check below is not sufficient as | |
6016 | really all PT_LOAD segments should be checked before issuing an error | |
6017 | message. Plus the PHDR segment does not have to be the first segment | |
6018 | in the program header table. But this version of the check should | |
6019 | catch all real world use cases. */ | |
6020 | if (alloc > 1 | |
6021 | && map[0].p_type == PT_PHDR | |
6022 | && ! bed->elf_backend_allow_non_load_phdr (abfd, map, alloc) | |
6023 | && map[1].p_type == PT_LOAD | |
6024 | && (map[1].p_vaddr > map[0].p_vaddr | |
6025 | || (map[1].p_vaddr + map[1].p_memsz) < (map[0].p_vaddr + map[0].p_memsz))) | |
6026 | { | |
6027 | /* The fix for this error is usually to edit the linker script being | |
6028 | used and set up the program headers manually. Either that or | |
6029 | leave room for the headers at the start of the SECTIONS. */ | |
6030 | _bfd_error_handler (_("\ | |
6031 | %B: error: PHDR segment not covered by LOAD segment"), | |
6032 | abfd); | |
6033 | free (map); | |
6034 | return FALSE; | |
6035 | } | |
6036 | ||
f3520d2f | 6037 | if (bfd_seek (abfd, (bfd_signed_vma) bed->s->sizeof_ehdr, SEEK_SET) != 0 |
1a9ccd70 NC |
6038 | || bed->s->write_out_phdrs (abfd, map, alloc) != 0) |
6039 | { | |
6040 | free (map); | |
6041 | return FALSE; | |
6042 | } | |
6043 | ||
6044 | free (map); | |
252b5132 RH |
6045 | } |
6046 | ||
b34976b6 | 6047 | return TRUE; |
252b5132 RH |
6048 | } |
6049 | ||
b34976b6 | 6050 | static bfd_boolean |
217aa764 | 6051 | prep_headers (bfd *abfd) |
252b5132 | 6052 | { |
3d540e93 | 6053 | Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form. */ |
2b0f7ef9 | 6054 | struct elf_strtab_hash *shstrtab; |
9c5bfbb7 | 6055 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
252b5132 RH |
6056 | |
6057 | i_ehdrp = elf_elfheader (abfd); | |
252b5132 | 6058 | |
2b0f7ef9 | 6059 | shstrtab = _bfd_elf_strtab_init (); |
252b5132 | 6060 | if (shstrtab == NULL) |
b34976b6 | 6061 | return FALSE; |
252b5132 RH |
6062 | |
6063 | elf_shstrtab (abfd) = shstrtab; | |
6064 | ||
6065 | i_ehdrp->e_ident[EI_MAG0] = ELFMAG0; | |
6066 | i_ehdrp->e_ident[EI_MAG1] = ELFMAG1; | |
6067 | i_ehdrp->e_ident[EI_MAG2] = ELFMAG2; | |
6068 | i_ehdrp->e_ident[EI_MAG3] = ELFMAG3; | |
6069 | ||
6070 | i_ehdrp->e_ident[EI_CLASS] = bed->s->elfclass; | |
6071 | i_ehdrp->e_ident[EI_DATA] = | |
6072 | bfd_big_endian (abfd) ? ELFDATA2MSB : ELFDATA2LSB; | |
6073 | i_ehdrp->e_ident[EI_VERSION] = bed->s->ev_current; | |
6074 | ||
252b5132 RH |
6075 | if ((abfd->flags & DYNAMIC) != 0) |
6076 | i_ehdrp->e_type = ET_DYN; | |
6077 | else if ((abfd->flags & EXEC_P) != 0) | |
6078 | i_ehdrp->e_type = ET_EXEC; | |
6079 | else if (bfd_get_format (abfd) == bfd_core) | |
6080 | i_ehdrp->e_type = ET_CORE; | |
6081 | else | |
6082 | i_ehdrp->e_type = ET_REL; | |
6083 | ||
6084 | switch (bfd_get_arch (abfd)) | |
6085 | { | |
6086 | case bfd_arch_unknown: | |
6087 | i_ehdrp->e_machine = EM_NONE; | |
6088 | break; | |
aa4f99bb AO |
6089 | |
6090 | /* There used to be a long list of cases here, each one setting | |
6091 | e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE | |
6092 | in the corresponding bfd definition. To avoid duplication, | |
6093 | the switch was removed. Machines that need special handling | |
6094 | can generally do it in elf_backend_final_write_processing(), | |
6095 | unless they need the information earlier than the final write. | |
6096 | Such need can generally be supplied by replacing the tests for | |
6097 | e_machine with the conditions used to determine it. */ | |
252b5132 | 6098 | default: |
9c5bfbb7 AM |
6099 | i_ehdrp->e_machine = bed->elf_machine_code; |
6100 | } | |
aa4f99bb | 6101 | |
252b5132 RH |
6102 | i_ehdrp->e_version = bed->s->ev_current; |
6103 | i_ehdrp->e_ehsize = bed->s->sizeof_ehdr; | |
6104 | ||
c044fabd | 6105 | /* No program header, for now. */ |
252b5132 RH |
6106 | i_ehdrp->e_phoff = 0; |
6107 | i_ehdrp->e_phentsize = 0; | |
6108 | i_ehdrp->e_phnum = 0; | |
6109 | ||
c044fabd | 6110 | /* Each bfd section is section header entry. */ |
252b5132 RH |
6111 | i_ehdrp->e_entry = bfd_get_start_address (abfd); |
6112 | i_ehdrp->e_shentsize = bed->s->sizeof_shdr; | |
6113 | ||
c044fabd | 6114 | /* If we're building an executable, we'll need a program header table. */ |
252b5132 | 6115 | if (abfd->flags & EXEC_P) |
0e71e495 BE |
6116 | /* It all happens later. */ |
6117 | ; | |
252b5132 RH |
6118 | else |
6119 | { | |
6120 | i_ehdrp->e_phentsize = 0; | |
252b5132 RH |
6121 | i_ehdrp->e_phoff = 0; |
6122 | } | |
6123 | ||
6124 | elf_tdata (abfd)->symtab_hdr.sh_name = | |
b34976b6 | 6125 | (unsigned int) _bfd_elf_strtab_add (shstrtab, ".symtab", FALSE); |
252b5132 | 6126 | elf_tdata (abfd)->strtab_hdr.sh_name = |
b34976b6 | 6127 | (unsigned int) _bfd_elf_strtab_add (shstrtab, ".strtab", FALSE); |
252b5132 | 6128 | elf_tdata (abfd)->shstrtab_hdr.sh_name = |
b34976b6 | 6129 | (unsigned int) _bfd_elf_strtab_add (shstrtab, ".shstrtab", FALSE); |
252b5132 | 6130 | if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1 |
17ca87fc | 6131 | || elf_tdata (abfd)->strtab_hdr.sh_name == (unsigned int) -1 |
252b5132 | 6132 | || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1) |
b34976b6 | 6133 | return FALSE; |
252b5132 | 6134 | |
b34976b6 | 6135 | return TRUE; |
252b5132 RH |
6136 | } |
6137 | ||
6138 | /* Assign file positions for all the reloc sections which are not part | |
a485e98e | 6139 | of the loadable file image, and the file position of section headers. */ |
252b5132 | 6140 | |
0ce398f1 L |
6141 | static bfd_boolean |
6142 | _bfd_elf_assign_file_positions_for_non_load (bfd *abfd) | |
252b5132 RH |
6143 | { |
6144 | file_ptr off; | |
e06efbf1 | 6145 | Elf_Internal_Shdr **shdrpp, **end_shdrpp; |
3e19fb8f | 6146 | Elf_Internal_Shdr *shdrp; |
a485e98e AM |
6147 | Elf_Internal_Ehdr *i_ehdrp; |
6148 | const struct elf_backend_data *bed; | |
252b5132 | 6149 | |
12bd6957 | 6150 | off = elf_next_file_pos (abfd); |
252b5132 | 6151 | |
e06efbf1 L |
6152 | shdrpp = elf_elfsections (abfd); |
6153 | end_shdrpp = shdrpp + elf_numsections (abfd); | |
6154 | for (shdrpp++; shdrpp < end_shdrpp; shdrpp++) | |
252b5132 | 6155 | { |
252b5132 | 6156 | shdrp = *shdrpp; |
0ce398f1 L |
6157 | if (shdrp->sh_offset == -1) |
6158 | { | |
3e19fb8f | 6159 | asection *sec = shdrp->bfd_section; |
0ce398f1 L |
6160 | bfd_boolean is_rel = (shdrp->sh_type == SHT_REL |
6161 | || shdrp->sh_type == SHT_RELA); | |
6162 | if (is_rel | |
3e19fb8f | 6163 | || (sec != NULL && (sec->flags & SEC_ELF_COMPRESS))) |
0ce398f1 L |
6164 | { |
6165 | if (!is_rel) | |
6166 | { | |
3e19fb8f L |
6167 | const char *name = sec->name; |
6168 | struct bfd_elf_section_data *d; | |
6169 | ||
0ce398f1 | 6170 | /* Compress DWARF debug sections. */ |
3e19fb8f | 6171 | if (!bfd_compress_section (abfd, sec, |
0ce398f1 L |
6172 | shdrp->contents)) |
6173 | return FALSE; | |
3e19fb8f L |
6174 | |
6175 | if (sec->compress_status == COMPRESS_SECTION_DONE | |
6176 | && (abfd->flags & BFD_COMPRESS_GABI) == 0) | |
6177 | { | |
6178 | /* If section is compressed with zlib-gnu, convert | |
6179 | section name from .debug_* to .zdebug_*. */ | |
6180 | char *new_name | |
6181 | = convert_debug_to_zdebug (abfd, name); | |
6182 | if (new_name == NULL) | |
6183 | return FALSE; | |
6184 | name = new_name; | |
6185 | } | |
dd905818 | 6186 | /* Add section name to section name section. */ |
3e19fb8f L |
6187 | if (shdrp->sh_name != (unsigned int) -1) |
6188 | abort (); | |
6189 | shdrp->sh_name | |
6190 | = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), | |
6191 | name, FALSE); | |
6192 | d = elf_section_data (sec); | |
6193 | ||
dd905818 | 6194 | /* Add reloc section name to section name section. */ |
3e19fb8f L |
6195 | if (d->rel.hdr |
6196 | && !_bfd_elf_set_reloc_sh_name (abfd, | |
6197 | d->rel.hdr, | |
6198 | name, FALSE)) | |
6199 | return FALSE; | |
6200 | if (d->rela.hdr | |
6201 | && !_bfd_elf_set_reloc_sh_name (abfd, | |
6202 | d->rela.hdr, | |
91cb26da | 6203 | name, TRUE)) |
3e19fb8f L |
6204 | return FALSE; |
6205 | ||
0ce398f1 | 6206 | /* Update section size and contents. */ |
3e19fb8f L |
6207 | shdrp->sh_size = sec->size; |
6208 | shdrp->contents = sec->contents; | |
0ce398f1 L |
6209 | shdrp->bfd_section->contents = NULL; |
6210 | } | |
6211 | off = _bfd_elf_assign_file_position_for_section (shdrp, | |
6212 | off, | |
6213 | TRUE); | |
6214 | } | |
6215 | } | |
252b5132 RH |
6216 | } |
6217 | ||
3e19fb8f L |
6218 | /* Place section name section after DWARF debug sections have been |
6219 | compressed. */ | |
6220 | _bfd_elf_strtab_finalize (elf_shstrtab (abfd)); | |
6221 | shdrp = &elf_tdata (abfd)->shstrtab_hdr; | |
6222 | shdrp->sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd)); | |
6223 | off = _bfd_elf_assign_file_position_for_section (shdrp, off, TRUE); | |
6224 | ||
6225 | /* Place the section headers. */ | |
a485e98e AM |
6226 | i_ehdrp = elf_elfheader (abfd); |
6227 | bed = get_elf_backend_data (abfd); | |
6228 | off = align_file_position (off, 1 << bed->s->log_file_align); | |
6229 | i_ehdrp->e_shoff = off; | |
6230 | off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize; | |
12bd6957 | 6231 | elf_next_file_pos (abfd) = off; |
0ce398f1 L |
6232 | |
6233 | return TRUE; | |
252b5132 RH |
6234 | } |
6235 | ||
b34976b6 | 6236 | bfd_boolean |
217aa764 | 6237 | _bfd_elf_write_object_contents (bfd *abfd) |
252b5132 | 6238 | { |
9c5bfbb7 | 6239 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
252b5132 | 6240 | Elf_Internal_Shdr **i_shdrp; |
b34976b6 | 6241 | bfd_boolean failed; |
9ad5cbcf | 6242 | unsigned int count, num_sec; |
30e8ee25 | 6243 | struct elf_obj_tdata *t; |
252b5132 RH |
6244 | |
6245 | if (! abfd->output_has_begun | |
217aa764 | 6246 | && ! _bfd_elf_compute_section_file_positions (abfd, NULL)) |
b34976b6 | 6247 | return FALSE; |
252b5132 RH |
6248 | |
6249 | i_shdrp = elf_elfsections (abfd); | |
252b5132 | 6250 | |
b34976b6 | 6251 | failed = FALSE; |
252b5132 RH |
6252 | bfd_map_over_sections (abfd, bed->s->write_relocs, &failed); |
6253 | if (failed) | |
b34976b6 | 6254 | return FALSE; |
252b5132 | 6255 | |
0ce398f1 L |
6256 | if (!_bfd_elf_assign_file_positions_for_non_load (abfd)) |
6257 | return FALSE; | |
252b5132 | 6258 | |
c044fabd | 6259 | /* After writing the headers, we need to write the sections too... */ |
9ad5cbcf AM |
6260 | num_sec = elf_numsections (abfd); |
6261 | for (count = 1; count < num_sec; count++) | |
252b5132 | 6262 | { |
3e19fb8f L |
6263 | i_shdrp[count]->sh_name |
6264 | = _bfd_elf_strtab_offset (elf_shstrtab (abfd), | |
6265 | i_shdrp[count]->sh_name); | |
252b5132 RH |
6266 | if (bed->elf_backend_section_processing) |
6267 | (*bed->elf_backend_section_processing) (abfd, i_shdrp[count]); | |
6268 | if (i_shdrp[count]->contents) | |
6269 | { | |
dc810e39 AM |
6270 | bfd_size_type amt = i_shdrp[count]->sh_size; |
6271 | ||
252b5132 | 6272 | if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0 |
dc810e39 | 6273 | || bfd_bwrite (i_shdrp[count]->contents, amt, abfd) != amt) |
b34976b6 | 6274 | return FALSE; |
252b5132 RH |
6275 | } |
6276 | } | |
6277 | ||
6278 | /* Write out the section header names. */ | |
30e8ee25 | 6279 | t = elf_tdata (abfd); |
26ae6d5e | 6280 | if (elf_shstrtab (abfd) != NULL |
30e8ee25 | 6281 | && (bfd_seek (abfd, t->shstrtab_hdr.sh_offset, SEEK_SET) != 0 |
08a40648 | 6282 | || !_bfd_elf_strtab_emit (abfd, elf_shstrtab (abfd)))) |
b34976b6 | 6283 | return FALSE; |
252b5132 RH |
6284 | |
6285 | if (bed->elf_backend_final_write_processing) | |
12bd6957 | 6286 | (*bed->elf_backend_final_write_processing) (abfd, elf_linker (abfd)); |
252b5132 | 6287 | |
ff59fc36 RM |
6288 | if (!bed->s->write_shdrs_and_ehdr (abfd)) |
6289 | return FALSE; | |
6290 | ||
6291 | /* This is last since write_shdrs_and_ehdr can touch i_shdrp[0]. */ | |
c0355132 AM |
6292 | if (t->o->build_id.after_write_object_contents != NULL) |
6293 | return (*t->o->build_id.after_write_object_contents) (abfd); | |
ff59fc36 RM |
6294 | |
6295 | return TRUE; | |
252b5132 RH |
6296 | } |
6297 | ||
b34976b6 | 6298 | bfd_boolean |
217aa764 | 6299 | _bfd_elf_write_corefile_contents (bfd *abfd) |
252b5132 | 6300 | { |
c044fabd | 6301 | /* Hopefully this can be done just like an object file. */ |
252b5132 RH |
6302 | return _bfd_elf_write_object_contents (abfd); |
6303 | } | |
c044fabd KH |
6304 | |
6305 | /* Given a section, search the header to find them. */ | |
6306 | ||
cb33740c | 6307 | unsigned int |
198beae2 | 6308 | _bfd_elf_section_from_bfd_section (bfd *abfd, struct bfd_section *asect) |
252b5132 | 6309 | { |
9c5bfbb7 | 6310 | const struct elf_backend_data *bed; |
91d6fa6a | 6311 | unsigned int sec_index; |
252b5132 | 6312 | |
9ad5cbcf AM |
6313 | if (elf_section_data (asect) != NULL |
6314 | && elf_section_data (asect)->this_idx != 0) | |
6315 | return elf_section_data (asect)->this_idx; | |
6316 | ||
6317 | if (bfd_is_abs_section (asect)) | |
91d6fa6a | 6318 | sec_index = SHN_ABS; |
af746e92 | 6319 | else if (bfd_is_com_section (asect)) |
91d6fa6a | 6320 | sec_index = SHN_COMMON; |
af746e92 | 6321 | else if (bfd_is_und_section (asect)) |
91d6fa6a | 6322 | sec_index = SHN_UNDEF; |
af746e92 | 6323 | else |
91d6fa6a | 6324 | sec_index = SHN_BAD; |
252b5132 | 6325 | |
af746e92 | 6326 | bed = get_elf_backend_data (abfd); |
252b5132 RH |
6327 | if (bed->elf_backend_section_from_bfd_section) |
6328 | { | |
91d6fa6a | 6329 | int retval = sec_index; |
9ad5cbcf | 6330 | |
af746e92 AM |
6331 | if ((*bed->elf_backend_section_from_bfd_section) (abfd, asect, &retval)) |
6332 | return retval; | |
252b5132 RH |
6333 | } |
6334 | ||
91d6fa6a | 6335 | if (sec_index == SHN_BAD) |
af746e92 | 6336 | bfd_set_error (bfd_error_nonrepresentable_section); |
252b5132 | 6337 | |
91d6fa6a | 6338 | return sec_index; |
252b5132 RH |
6339 | } |
6340 | ||
6341 | /* Given a BFD symbol, return the index in the ELF symbol table, or -1 | |
6342 | on error. */ | |
6343 | ||
6344 | int | |
217aa764 | 6345 | _bfd_elf_symbol_from_bfd_symbol (bfd *abfd, asymbol **asym_ptr_ptr) |
252b5132 RH |
6346 | { |
6347 | asymbol *asym_ptr = *asym_ptr_ptr; | |
6348 | int idx; | |
6349 | flagword flags = asym_ptr->flags; | |
6350 | ||
6351 | /* When gas creates relocations against local labels, it creates its | |
6352 | own symbol for the section, but does put the symbol into the | |
6353 | symbol chain, so udata is 0. When the linker is generating | |
6354 | relocatable output, this section symbol may be for one of the | |
6355 | input sections rather than the output section. */ | |
6356 | if (asym_ptr->udata.i == 0 | |
6357 | && (flags & BSF_SECTION_SYM) | |
6358 | && asym_ptr->section) | |
6359 | { | |
5372391b | 6360 | asection *sec; |
252b5132 RH |
6361 | int indx; |
6362 | ||
5372391b AM |
6363 | sec = asym_ptr->section; |
6364 | if (sec->owner != abfd && sec->output_section != NULL) | |
6365 | sec = sec->output_section; | |
6366 | if (sec->owner == abfd | |
6367 | && (indx = sec->index) < elf_num_section_syms (abfd) | |
4e89ac30 | 6368 | && elf_section_syms (abfd)[indx] != NULL) |
252b5132 RH |
6369 | asym_ptr->udata.i = elf_section_syms (abfd)[indx]->udata.i; |
6370 | } | |
6371 | ||
6372 | idx = asym_ptr->udata.i; | |
6373 | ||
6374 | if (idx == 0) | |
6375 | { | |
6376 | /* This case can occur when using --strip-symbol on a symbol | |
08a40648 | 6377 | which is used in a relocation entry. */ |
4eca0228 | 6378 | _bfd_error_handler |
695344c0 | 6379 | /* xgettext:c-format */ |
d003868e AM |
6380 | (_("%B: symbol `%s' required but not present"), |
6381 | abfd, bfd_asymbol_name (asym_ptr)); | |
252b5132 RH |
6382 | bfd_set_error (bfd_error_no_symbols); |
6383 | return -1; | |
6384 | } | |
6385 | ||
6386 | #if DEBUG & 4 | |
6387 | { | |
6388 | fprintf (stderr, | |
9ccb8af9 AM |
6389 | "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8lx\n", |
6390 | (long) asym_ptr, asym_ptr->name, idx, (long) flags); | |
252b5132 RH |
6391 | fflush (stderr); |
6392 | } | |
6393 | #endif | |
6394 | ||
6395 | return idx; | |
6396 | } | |
6397 | ||
84d1d650 | 6398 | /* Rewrite program header information. */ |
252b5132 | 6399 | |
b34976b6 | 6400 | static bfd_boolean |
84d1d650 | 6401 | rewrite_elf_program_header (bfd *ibfd, bfd *obfd) |
252b5132 | 6402 | { |
b34976b6 AM |
6403 | Elf_Internal_Ehdr *iehdr; |
6404 | struct elf_segment_map *map; | |
6405 | struct elf_segment_map *map_first; | |
6406 | struct elf_segment_map **pointer_to_map; | |
6407 | Elf_Internal_Phdr *segment; | |
6408 | asection *section; | |
6409 | unsigned int i; | |
6410 | unsigned int num_segments; | |
6411 | bfd_boolean phdr_included = FALSE; | |
5c44b38e | 6412 | bfd_boolean p_paddr_valid; |
b34976b6 AM |
6413 | bfd_vma maxpagesize; |
6414 | struct elf_segment_map *phdr_adjust_seg = NULL; | |
6415 | unsigned int phdr_adjust_num = 0; | |
9c5bfbb7 | 6416 | const struct elf_backend_data *bed; |
bc67d8a6 | 6417 | |
caf47ea6 | 6418 | bed = get_elf_backend_data (ibfd); |
252b5132 RH |
6419 | iehdr = elf_elfheader (ibfd); |
6420 | ||
bc67d8a6 | 6421 | map_first = NULL; |
c044fabd | 6422 | pointer_to_map = &map_first; |
252b5132 RH |
6423 | |
6424 | num_segments = elf_elfheader (ibfd)->e_phnum; | |
bc67d8a6 NC |
6425 | maxpagesize = get_elf_backend_data (obfd)->maxpagesize; |
6426 | ||
6427 | /* Returns the end address of the segment + 1. */ | |
aecc8f8a AM |
6428 | #define SEGMENT_END(segment, start) \ |
6429 | (start + (segment->p_memsz > segment->p_filesz \ | |
6430 | ? segment->p_memsz : segment->p_filesz)) | |
bc67d8a6 | 6431 | |
eecdbe52 JJ |
6432 | #define SECTION_SIZE(section, segment) \ |
6433 | (((section->flags & (SEC_HAS_CONTENTS | SEC_THREAD_LOCAL)) \ | |
6434 | != SEC_THREAD_LOCAL || segment->p_type == PT_TLS) \ | |
eea6121a | 6435 | ? section->size : 0) |
eecdbe52 | 6436 | |
b34976b6 | 6437 | /* Returns TRUE if the given section is contained within |
bc67d8a6 | 6438 | the given segment. VMA addresses are compared. */ |
aecc8f8a AM |
6439 | #define IS_CONTAINED_BY_VMA(section, segment) \ |
6440 | (section->vma >= segment->p_vaddr \ | |
eecdbe52 | 6441 | && (section->vma + SECTION_SIZE (section, segment) \ |
aecc8f8a | 6442 | <= (SEGMENT_END (segment, segment->p_vaddr)))) |
c044fabd | 6443 | |
b34976b6 | 6444 | /* Returns TRUE if the given section is contained within |
bc67d8a6 | 6445 | the given segment. LMA addresses are compared. */ |
aecc8f8a AM |
6446 | #define IS_CONTAINED_BY_LMA(section, segment, base) \ |
6447 | (section->lma >= base \ | |
eecdbe52 | 6448 | && (section->lma + SECTION_SIZE (section, segment) \ |
aecc8f8a | 6449 | <= SEGMENT_END (segment, base))) |
252b5132 | 6450 | |
0efc80c8 L |
6451 | /* Handle PT_NOTE segment. */ |
6452 | #define IS_NOTE(p, s) \ | |
aecc8f8a | 6453 | (p->p_type == PT_NOTE \ |
0efc80c8 | 6454 | && elf_section_type (s) == SHT_NOTE \ |
aecc8f8a | 6455 | && (bfd_vma) s->filepos >= p->p_offset \ |
cb3ff1e5 | 6456 | && ((bfd_vma) s->filepos + s->size \ |
aecc8f8a | 6457 | <= p->p_offset + p->p_filesz)) |
252b5132 | 6458 | |
0efc80c8 L |
6459 | /* Special case: corefile "NOTE" section containing regs, prpsinfo |
6460 | etc. */ | |
6461 | #define IS_COREFILE_NOTE(p, s) \ | |
6462 | (IS_NOTE (p, s) \ | |
6463 | && bfd_get_format (ibfd) == bfd_core \ | |
6464 | && s->vma == 0 \ | |
6465 | && s->lma == 0) | |
6466 | ||
252b5132 RH |
6467 | /* The complicated case when p_vaddr is 0 is to handle the Solaris |
6468 | linker, which generates a PT_INTERP section with p_vaddr and | |
6469 | p_memsz set to 0. */ | |
aecc8f8a AM |
6470 | #define IS_SOLARIS_PT_INTERP(p, s) \ |
6471 | (p->p_vaddr == 0 \ | |
6472 | && p->p_paddr == 0 \ | |
6473 | && p->p_memsz == 0 \ | |
6474 | && p->p_filesz > 0 \ | |
6475 | && (s->flags & SEC_HAS_CONTENTS) != 0 \ | |
eea6121a | 6476 | && s->size > 0 \ |
aecc8f8a | 6477 | && (bfd_vma) s->filepos >= p->p_offset \ |
cb3ff1e5 | 6478 | && ((bfd_vma) s->filepos + s->size \ |
aecc8f8a | 6479 | <= p->p_offset + p->p_filesz)) |
5c440b1e | 6480 | |
bc67d8a6 NC |
6481 | /* Decide if the given section should be included in the given segment. |
6482 | A section will be included if: | |
f5ffc919 | 6483 | 1. It is within the address space of the segment -- we use the LMA |
08a40648 | 6484 | if that is set for the segment and the VMA otherwise, |
0efc80c8 | 6485 | 2. It is an allocated section or a NOTE section in a PT_NOTE |
d324f6d6 | 6486 | segment. |
bc67d8a6 | 6487 | 3. There is an output section associated with it, |
eecdbe52 | 6488 | 4. The section has not already been allocated to a previous segment. |
2b05f1b7 | 6489 | 5. PT_GNU_STACK segments do not include any sections. |
03394ac9 | 6490 | 6. PT_TLS segment includes only SHF_TLS sections. |
6f79b219 JJ |
6491 | 7. SHF_TLS sections are only in PT_TLS or PT_LOAD segments. |
6492 | 8. PT_DYNAMIC should not contain empty sections at the beginning | |
08a40648 | 6493 | (with the possible exception of .dynamic). */ |
9f17e2a6 | 6494 | #define IS_SECTION_IN_INPUT_SEGMENT(section, segment, bed) \ |
2b05f1b7 L |
6495 | ((((segment->p_paddr \ |
6496 | ? IS_CONTAINED_BY_LMA (section, segment, segment->p_paddr) \ | |
6497 | : IS_CONTAINED_BY_VMA (section, segment)) \ | |
6498 | && (section->flags & SEC_ALLOC) != 0) \ | |
0efc80c8 | 6499 | || IS_NOTE (segment, section)) \ |
2b05f1b7 L |
6500 | && segment->p_type != PT_GNU_STACK \ |
6501 | && (segment->p_type != PT_TLS \ | |
6502 | || (section->flags & SEC_THREAD_LOCAL)) \ | |
6503 | && (segment->p_type == PT_LOAD \ | |
6504 | || segment->p_type == PT_TLS \ | |
6505 | || (section->flags & SEC_THREAD_LOCAL) == 0) \ | |
6506 | && (segment->p_type != PT_DYNAMIC \ | |
6507 | || SECTION_SIZE (section, segment) > 0 \ | |
6508 | || (segment->p_paddr \ | |
6509 | ? segment->p_paddr != section->lma \ | |
6510 | : segment->p_vaddr != section->vma) \ | |
6511 | || (strcmp (bfd_get_section_name (ibfd, section), ".dynamic") \ | |
6512 | == 0)) \ | |
0067a569 | 6513 | && !section->segment_mark) |
bc67d8a6 | 6514 | |
9f17e2a6 L |
6515 | /* If the output section of a section in the input segment is NULL, |
6516 | it is removed from the corresponding output segment. */ | |
6517 | #define INCLUDE_SECTION_IN_SEGMENT(section, segment, bed) \ | |
6518 | (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed) \ | |
6519 | && section->output_section != NULL) | |
6520 | ||
b34976b6 | 6521 | /* Returns TRUE iff seg1 starts after the end of seg2. */ |
b5f852ea NC |
6522 | #define SEGMENT_AFTER_SEGMENT(seg1, seg2, field) \ |
6523 | (seg1->field >= SEGMENT_END (seg2, seg2->field)) | |
6524 | ||
6525 | /* Returns TRUE iff seg1 and seg2 overlap. Segments overlap iff both | |
6526 | their VMA address ranges and their LMA address ranges overlap. | |
6527 | It is possible to have overlapping VMA ranges without overlapping LMA | |
6528 | ranges. RedBoot images for example can have both .data and .bss mapped | |
6529 | to the same VMA range, but with the .data section mapped to a different | |
6530 | LMA. */ | |
aecc8f8a | 6531 | #define SEGMENT_OVERLAPS(seg1, seg2) \ |
b5f852ea | 6532 | ( !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_vaddr) \ |
08a40648 | 6533 | || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_vaddr)) \ |
b5f852ea | 6534 | && !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_paddr) \ |
08a40648 | 6535 | || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_paddr))) |
bc67d8a6 NC |
6536 | |
6537 | /* Initialise the segment mark field. */ | |
6538 | for (section = ibfd->sections; section != NULL; section = section->next) | |
b34976b6 | 6539 | section->segment_mark = FALSE; |
bc67d8a6 | 6540 | |
5c44b38e AM |
6541 | /* The Solaris linker creates program headers in which all the |
6542 | p_paddr fields are zero. When we try to objcopy or strip such a | |
6543 | file, we get confused. Check for this case, and if we find it | |
6544 | don't set the p_paddr_valid fields. */ | |
6545 | p_paddr_valid = FALSE; | |
6546 | for (i = 0, segment = elf_tdata (ibfd)->phdr; | |
6547 | i < num_segments; | |
6548 | i++, segment++) | |
6549 | if (segment->p_paddr != 0) | |
6550 | { | |
6551 | p_paddr_valid = TRUE; | |
6552 | break; | |
6553 | } | |
6554 | ||
252b5132 | 6555 | /* Scan through the segments specified in the program header |
bc67d8a6 | 6556 | of the input BFD. For this first scan we look for overlaps |
9ad5cbcf | 6557 | in the loadable segments. These can be created by weird |
aecc8f8a | 6558 | parameters to objcopy. Also, fix some solaris weirdness. */ |
bc67d8a6 NC |
6559 | for (i = 0, segment = elf_tdata (ibfd)->phdr; |
6560 | i < num_segments; | |
c044fabd | 6561 | i++, segment++) |
252b5132 | 6562 | { |
252b5132 | 6563 | unsigned int j; |
c044fabd | 6564 | Elf_Internal_Phdr *segment2; |
252b5132 | 6565 | |
aecc8f8a AM |
6566 | if (segment->p_type == PT_INTERP) |
6567 | for (section = ibfd->sections; section; section = section->next) | |
6568 | if (IS_SOLARIS_PT_INTERP (segment, section)) | |
6569 | { | |
6570 | /* Mininal change so that the normal section to segment | |
4cc11e76 | 6571 | assignment code will work. */ |
aecc8f8a AM |
6572 | segment->p_vaddr = section->vma; |
6573 | break; | |
6574 | } | |
6575 | ||
bc67d8a6 | 6576 | if (segment->p_type != PT_LOAD) |
b10a8ae0 L |
6577 | { |
6578 | /* Remove PT_GNU_RELRO segment. */ | |
6579 | if (segment->p_type == PT_GNU_RELRO) | |
6580 | segment->p_type = PT_NULL; | |
6581 | continue; | |
6582 | } | |
c044fabd | 6583 | |
bc67d8a6 | 6584 | /* Determine if this segment overlaps any previous segments. */ |
0067a569 | 6585 | for (j = 0, segment2 = elf_tdata (ibfd)->phdr; j < i; j++, segment2++) |
bc67d8a6 NC |
6586 | { |
6587 | bfd_signed_vma extra_length; | |
c044fabd | 6588 | |
bc67d8a6 | 6589 | if (segment2->p_type != PT_LOAD |
0067a569 | 6590 | || !SEGMENT_OVERLAPS (segment, segment2)) |
bc67d8a6 | 6591 | continue; |
c044fabd | 6592 | |
bc67d8a6 NC |
6593 | /* Merge the two segments together. */ |
6594 | if (segment2->p_vaddr < segment->p_vaddr) | |
6595 | { | |
c044fabd | 6596 | /* Extend SEGMENT2 to include SEGMENT and then delete |
08a40648 | 6597 | SEGMENT. */ |
0067a569 AM |
6598 | extra_length = (SEGMENT_END (segment, segment->p_vaddr) |
6599 | - SEGMENT_END (segment2, segment2->p_vaddr)); | |
c044fabd | 6600 | |
bc67d8a6 NC |
6601 | if (extra_length > 0) |
6602 | { | |
0067a569 | 6603 | segment2->p_memsz += extra_length; |
bc67d8a6 NC |
6604 | segment2->p_filesz += extra_length; |
6605 | } | |
c044fabd | 6606 | |
bc67d8a6 | 6607 | segment->p_type = PT_NULL; |
c044fabd | 6608 | |
bc67d8a6 NC |
6609 | /* Since we have deleted P we must restart the outer loop. */ |
6610 | i = 0; | |
6611 | segment = elf_tdata (ibfd)->phdr; | |
6612 | break; | |
6613 | } | |
6614 | else | |
6615 | { | |
c044fabd | 6616 | /* Extend SEGMENT to include SEGMENT2 and then delete |
08a40648 | 6617 | SEGMENT2. */ |
0067a569 AM |
6618 | extra_length = (SEGMENT_END (segment2, segment2->p_vaddr) |
6619 | - SEGMENT_END (segment, segment->p_vaddr)); | |
c044fabd | 6620 | |
bc67d8a6 NC |
6621 | if (extra_length > 0) |
6622 | { | |
0067a569 | 6623 | segment->p_memsz += extra_length; |
bc67d8a6 NC |
6624 | segment->p_filesz += extra_length; |
6625 | } | |
c044fabd | 6626 | |
bc67d8a6 NC |
6627 | segment2->p_type = PT_NULL; |
6628 | } | |
6629 | } | |
6630 | } | |
c044fabd | 6631 | |
bc67d8a6 NC |
6632 | /* The second scan attempts to assign sections to segments. */ |
6633 | for (i = 0, segment = elf_tdata (ibfd)->phdr; | |
6634 | i < num_segments; | |
0067a569 | 6635 | i++, segment++) |
bc67d8a6 | 6636 | { |
0067a569 AM |
6637 | unsigned int section_count; |
6638 | asection **sections; | |
6639 | asection *output_section; | |
6640 | unsigned int isec; | |
6641 | bfd_vma matching_lma; | |
6642 | bfd_vma suggested_lma; | |
6643 | unsigned int j; | |
dc810e39 | 6644 | bfd_size_type amt; |
0067a569 AM |
6645 | asection *first_section; |
6646 | bfd_boolean first_matching_lma; | |
6647 | bfd_boolean first_suggested_lma; | |
bc67d8a6 NC |
6648 | |
6649 | if (segment->p_type == PT_NULL) | |
6650 | continue; | |
c044fabd | 6651 | |
9f17e2a6 | 6652 | first_section = NULL; |
bc67d8a6 | 6653 | /* Compute how many sections might be placed into this segment. */ |
b5f852ea NC |
6654 | for (section = ibfd->sections, section_count = 0; |
6655 | section != NULL; | |
6656 | section = section->next) | |
9f17e2a6 L |
6657 | { |
6658 | /* Find the first section in the input segment, which may be | |
6659 | removed from the corresponding output segment. */ | |
6660 | if (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed)) | |
6661 | { | |
6662 | if (first_section == NULL) | |
6663 | first_section = section; | |
6664 | if (section->output_section != NULL) | |
6665 | ++section_count; | |
6666 | } | |
6667 | } | |
811072d8 | 6668 | |
b5f852ea NC |
6669 | /* Allocate a segment map big enough to contain |
6670 | all of the sections we have selected. */ | |
dc810e39 AM |
6671 | amt = sizeof (struct elf_segment_map); |
6672 | amt += ((bfd_size_type) section_count - 1) * sizeof (asection *); | |
a50b1753 | 6673 | map = (struct elf_segment_map *) bfd_zalloc (obfd, amt); |
bc67d8a6 | 6674 | if (map == NULL) |
b34976b6 | 6675 | return FALSE; |
252b5132 RH |
6676 | |
6677 | /* Initialise the fields of the segment map. Default to | |
6678 | using the physical address of the segment in the input BFD. */ | |
0067a569 AM |
6679 | map->next = NULL; |
6680 | map->p_type = segment->p_type; | |
6681 | map->p_flags = segment->p_flags; | |
bc67d8a6 | 6682 | map->p_flags_valid = 1; |
55d55ac7 | 6683 | |
9f17e2a6 L |
6684 | /* If the first section in the input segment is removed, there is |
6685 | no need to preserve segment physical address in the corresponding | |
6686 | output segment. */ | |
945c025a | 6687 | if (!first_section || first_section->output_section != NULL) |
9f17e2a6 L |
6688 | { |
6689 | map->p_paddr = segment->p_paddr; | |
5c44b38e | 6690 | map->p_paddr_valid = p_paddr_valid; |
9f17e2a6 | 6691 | } |
252b5132 RH |
6692 | |
6693 | /* Determine if this segment contains the ELF file header | |
6694 | and if it contains the program headers themselves. */ | |
bc67d8a6 NC |
6695 | map->includes_filehdr = (segment->p_offset == 0 |
6696 | && segment->p_filesz >= iehdr->e_ehsize); | |
bc67d8a6 | 6697 | map->includes_phdrs = 0; |
252b5132 | 6698 | |
0067a569 | 6699 | if (!phdr_included || segment->p_type != PT_LOAD) |
252b5132 | 6700 | { |
bc67d8a6 NC |
6701 | map->includes_phdrs = |
6702 | (segment->p_offset <= (bfd_vma) iehdr->e_phoff | |
6703 | && (segment->p_offset + segment->p_filesz | |
252b5132 RH |
6704 | >= ((bfd_vma) iehdr->e_phoff |
6705 | + iehdr->e_phnum * iehdr->e_phentsize))); | |
c044fabd | 6706 | |
bc67d8a6 | 6707 | if (segment->p_type == PT_LOAD && map->includes_phdrs) |
b34976b6 | 6708 | phdr_included = TRUE; |
252b5132 RH |
6709 | } |
6710 | ||
bc67d8a6 | 6711 | if (section_count == 0) |
252b5132 RH |
6712 | { |
6713 | /* Special segments, such as the PT_PHDR segment, may contain | |
6714 | no sections, but ordinary, loadable segments should contain | |
1ed89aa9 NC |
6715 | something. They are allowed by the ELF spec however, so only |
6716 | a warning is produced. */ | |
bc67d8a6 | 6717 | if (segment->p_type == PT_LOAD) |
4eca0228 | 6718 | _bfd_error_handler (_("\ |
c86934ce | 6719 | %B: warning: Empty loadable segment detected, is this intentional ?"), |
4eca0228 | 6720 | ibfd); |
252b5132 | 6721 | |
bc67d8a6 | 6722 | map->count = 0; |
c044fabd KH |
6723 | *pointer_to_map = map; |
6724 | pointer_to_map = &map->next; | |
252b5132 RH |
6725 | |
6726 | continue; | |
6727 | } | |
6728 | ||
6729 | /* Now scan the sections in the input BFD again and attempt | |
6730 | to add their corresponding output sections to the segment map. | |
6731 | The problem here is how to handle an output section which has | |
6732 | been moved (ie had its LMA changed). There are four possibilities: | |
6733 | ||
6734 | 1. None of the sections have been moved. | |
6735 | In this case we can continue to use the segment LMA from the | |
6736 | input BFD. | |
6737 | ||
6738 | 2. All of the sections have been moved by the same amount. | |
6739 | In this case we can change the segment's LMA to match the LMA | |
6740 | of the first section. | |
6741 | ||
6742 | 3. Some of the sections have been moved, others have not. | |
6743 | In this case those sections which have not been moved can be | |
6744 | placed in the current segment which will have to have its size, | |
6745 | and possibly its LMA changed, and a new segment or segments will | |
6746 | have to be created to contain the other sections. | |
6747 | ||
b5f852ea | 6748 | 4. The sections have been moved, but not by the same amount. |
252b5132 RH |
6749 | In this case we can change the segment's LMA to match the LMA |
6750 | of the first section and we will have to create a new segment | |
6751 | or segments to contain the other sections. | |
6752 | ||
6753 | In order to save time, we allocate an array to hold the section | |
6754 | pointers that we are interested in. As these sections get assigned | |
6755 | to a segment, they are removed from this array. */ | |
6756 | ||
a50b1753 | 6757 | sections = (asection **) bfd_malloc2 (section_count, sizeof (asection *)); |
252b5132 | 6758 | if (sections == NULL) |
b34976b6 | 6759 | return FALSE; |
252b5132 RH |
6760 | |
6761 | /* Step One: Scan for segment vs section LMA conflicts. | |
6762 | Also add the sections to the section array allocated above. | |
6763 | Also add the sections to the current segment. In the common | |
6764 | case, where the sections have not been moved, this means that | |
6765 | we have completely filled the segment, and there is nothing | |
6766 | more to do. */ | |
252b5132 | 6767 | isec = 0; |
72730e0c | 6768 | matching_lma = 0; |
252b5132 | 6769 | suggested_lma = 0; |
0067a569 AM |
6770 | first_matching_lma = TRUE; |
6771 | first_suggested_lma = TRUE; | |
252b5132 | 6772 | |
461c4b2e | 6773 | for (section = first_section, j = 0; |
bc67d8a6 NC |
6774 | section != NULL; |
6775 | section = section->next) | |
252b5132 | 6776 | { |
caf47ea6 | 6777 | if (INCLUDE_SECTION_IN_SEGMENT (section, segment, bed)) |
c0f7859b | 6778 | { |
bc67d8a6 NC |
6779 | output_section = section->output_section; |
6780 | ||
0067a569 | 6781 | sections[j++] = section; |
252b5132 RH |
6782 | |
6783 | /* The Solaris native linker always sets p_paddr to 0. | |
6784 | We try to catch that case here, and set it to the | |
5e8d7549 NC |
6785 | correct value. Note - some backends require that |
6786 | p_paddr be left as zero. */ | |
5c44b38e | 6787 | if (!p_paddr_valid |
4455705d | 6788 | && segment->p_vaddr != 0 |
0067a569 | 6789 | && !bed->want_p_paddr_set_to_zero |
252b5132 | 6790 | && isec == 0 |
bc67d8a6 | 6791 | && output_section->lma != 0 |
0067a569 AM |
6792 | && output_section->vma == (segment->p_vaddr |
6793 | + (map->includes_filehdr | |
6794 | ? iehdr->e_ehsize | |
6795 | : 0) | |
6796 | + (map->includes_phdrs | |
6797 | ? (iehdr->e_phnum | |
6798 | * iehdr->e_phentsize) | |
6799 | : 0))) | |
bc67d8a6 | 6800 | map->p_paddr = segment->p_vaddr; |
252b5132 RH |
6801 | |
6802 | /* Match up the physical address of the segment with the | |
6803 | LMA address of the output section. */ | |
bc67d8a6 | 6804 | if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr) |
5e8d7549 | 6805 | || IS_COREFILE_NOTE (segment, section) |
0067a569 AM |
6806 | || (bed->want_p_paddr_set_to_zero |
6807 | && IS_CONTAINED_BY_VMA (output_section, segment))) | |
252b5132 | 6808 | { |
0067a569 AM |
6809 | if (first_matching_lma || output_section->lma < matching_lma) |
6810 | { | |
6811 | matching_lma = output_section->lma; | |
6812 | first_matching_lma = FALSE; | |
6813 | } | |
252b5132 RH |
6814 | |
6815 | /* We assume that if the section fits within the segment | |
bc67d8a6 | 6816 | then it does not overlap any other section within that |
252b5132 | 6817 | segment. */ |
0067a569 AM |
6818 | map->sections[isec++] = output_section; |
6819 | } | |
6820 | else if (first_suggested_lma) | |
6821 | { | |
6822 | suggested_lma = output_section->lma; | |
6823 | first_suggested_lma = FALSE; | |
252b5132 | 6824 | } |
147d51c2 L |
6825 | |
6826 | if (j == section_count) | |
6827 | break; | |
252b5132 RH |
6828 | } |
6829 | } | |
6830 | ||
bc67d8a6 | 6831 | BFD_ASSERT (j == section_count); |
252b5132 RH |
6832 | |
6833 | /* Step Two: Adjust the physical address of the current segment, | |
6834 | if necessary. */ | |
bc67d8a6 | 6835 | if (isec == section_count) |
252b5132 RH |
6836 | { |
6837 | /* All of the sections fitted within the segment as currently | |
6838 | specified. This is the default case. Add the segment to | |
6839 | the list of built segments and carry on to process the next | |
6840 | program header in the input BFD. */ | |
bc67d8a6 | 6841 | map->count = section_count; |
c044fabd KH |
6842 | *pointer_to_map = map; |
6843 | pointer_to_map = &map->next; | |
08a40648 | 6844 | |
5c44b38e AM |
6845 | if (p_paddr_valid |
6846 | && !bed->want_p_paddr_set_to_zero | |
147d51c2 | 6847 | && matching_lma != map->p_paddr |
5c44b38e AM |
6848 | && !map->includes_filehdr |
6849 | && !map->includes_phdrs) | |
3271a814 NS |
6850 | /* There is some padding before the first section in the |
6851 | segment. So, we must account for that in the output | |
6852 | segment's vma. */ | |
6853 | map->p_vaddr_offset = matching_lma - map->p_paddr; | |
08a40648 | 6854 | |
252b5132 RH |
6855 | free (sections); |
6856 | continue; | |
6857 | } | |
252b5132 RH |
6858 | else |
6859 | { | |
0067a569 | 6860 | if (!first_matching_lma) |
72730e0c AM |
6861 | { |
6862 | /* At least one section fits inside the current segment. | |
6863 | Keep it, but modify its physical address to match the | |
6864 | LMA of the first section that fitted. */ | |
bc67d8a6 | 6865 | map->p_paddr = matching_lma; |
72730e0c AM |
6866 | } |
6867 | else | |
6868 | { | |
6869 | /* None of the sections fitted inside the current segment. | |
6870 | Change the current segment's physical address to match | |
6871 | the LMA of the first section. */ | |
bc67d8a6 | 6872 | map->p_paddr = suggested_lma; |
72730e0c AM |
6873 | } |
6874 | ||
bc67d8a6 NC |
6875 | /* Offset the segment physical address from the lma |
6876 | to allow for space taken up by elf headers. */ | |
6877 | if (map->includes_filehdr) | |
010c8431 AM |
6878 | { |
6879 | if (map->p_paddr >= iehdr->e_ehsize) | |
6880 | map->p_paddr -= iehdr->e_ehsize; | |
6881 | else | |
6882 | { | |
6883 | map->includes_filehdr = FALSE; | |
6884 | map->includes_phdrs = FALSE; | |
6885 | } | |
6886 | } | |
252b5132 | 6887 | |
bc67d8a6 NC |
6888 | if (map->includes_phdrs) |
6889 | { | |
010c8431 AM |
6890 | if (map->p_paddr >= iehdr->e_phnum * iehdr->e_phentsize) |
6891 | { | |
6892 | map->p_paddr -= iehdr->e_phnum * iehdr->e_phentsize; | |
6893 | ||
6894 | /* iehdr->e_phnum is just an estimate of the number | |
6895 | of program headers that we will need. Make a note | |
6896 | here of the number we used and the segment we chose | |
6897 | to hold these headers, so that we can adjust the | |
6898 | offset when we know the correct value. */ | |
6899 | phdr_adjust_num = iehdr->e_phnum; | |
6900 | phdr_adjust_seg = map; | |
6901 | } | |
6902 | else | |
6903 | map->includes_phdrs = FALSE; | |
bc67d8a6 | 6904 | } |
252b5132 RH |
6905 | } |
6906 | ||
6907 | /* Step Three: Loop over the sections again, this time assigning | |
caf47ea6 | 6908 | those that fit to the current segment and removing them from the |
252b5132 RH |
6909 | sections array; but making sure not to leave large gaps. Once all |
6910 | possible sections have been assigned to the current segment it is | |
6911 | added to the list of built segments and if sections still remain | |
6912 | to be assigned, a new segment is constructed before repeating | |
6913 | the loop. */ | |
6914 | isec = 0; | |
6915 | do | |
6916 | { | |
bc67d8a6 | 6917 | map->count = 0; |
252b5132 | 6918 | suggested_lma = 0; |
0067a569 | 6919 | first_suggested_lma = TRUE; |
252b5132 RH |
6920 | |
6921 | /* Fill the current segment with sections that fit. */ | |
bc67d8a6 | 6922 | for (j = 0; j < section_count; j++) |
252b5132 | 6923 | { |
bc67d8a6 | 6924 | section = sections[j]; |
252b5132 | 6925 | |
bc67d8a6 | 6926 | if (section == NULL) |
252b5132 RH |
6927 | continue; |
6928 | ||
bc67d8a6 | 6929 | output_section = section->output_section; |
252b5132 | 6930 | |
bc67d8a6 | 6931 | BFD_ASSERT (output_section != NULL); |
c044fabd | 6932 | |
bc67d8a6 NC |
6933 | if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr) |
6934 | || IS_COREFILE_NOTE (segment, section)) | |
252b5132 | 6935 | { |
bc67d8a6 | 6936 | if (map->count == 0) |
252b5132 RH |
6937 | { |
6938 | /* If the first section in a segment does not start at | |
bc67d8a6 NC |
6939 | the beginning of the segment, then something is |
6940 | wrong. */ | |
0067a569 AM |
6941 | if (output_section->lma |
6942 | != (map->p_paddr | |
6943 | + (map->includes_filehdr ? iehdr->e_ehsize : 0) | |
6944 | + (map->includes_phdrs | |
6945 | ? iehdr->e_phnum * iehdr->e_phentsize | |
6946 | : 0))) | |
252b5132 RH |
6947 | abort (); |
6948 | } | |
6949 | else | |
6950 | { | |
0067a569 | 6951 | asection *prev_sec; |
252b5132 | 6952 | |
bc67d8a6 | 6953 | prev_sec = map->sections[map->count - 1]; |
252b5132 RH |
6954 | |
6955 | /* If the gap between the end of the previous section | |
bc67d8a6 NC |
6956 | and the start of this section is more than |
6957 | maxpagesize then we need to start a new segment. */ | |
eea6121a | 6958 | if ((BFD_ALIGN (prev_sec->lma + prev_sec->size, |
079e9a2f | 6959 | maxpagesize) |
caf47ea6 | 6960 | < BFD_ALIGN (output_section->lma, maxpagesize)) |
0067a569 | 6961 | || (prev_sec->lma + prev_sec->size |
079e9a2f | 6962 | > output_section->lma)) |
252b5132 | 6963 | { |
0067a569 AM |
6964 | if (first_suggested_lma) |
6965 | { | |
6966 | suggested_lma = output_section->lma; | |
6967 | first_suggested_lma = FALSE; | |
6968 | } | |
252b5132 RH |
6969 | |
6970 | continue; | |
6971 | } | |
6972 | } | |
6973 | ||
bc67d8a6 | 6974 | map->sections[map->count++] = output_section; |
252b5132 RH |
6975 | ++isec; |
6976 | sections[j] = NULL; | |
b34976b6 | 6977 | section->segment_mark = TRUE; |
252b5132 | 6978 | } |
0067a569 AM |
6979 | else if (first_suggested_lma) |
6980 | { | |
6981 | suggested_lma = output_section->lma; | |
6982 | first_suggested_lma = FALSE; | |
6983 | } | |
252b5132 RH |
6984 | } |
6985 | ||
bc67d8a6 | 6986 | BFD_ASSERT (map->count > 0); |
252b5132 RH |
6987 | |
6988 | /* Add the current segment to the list of built segments. */ | |
c044fabd KH |
6989 | *pointer_to_map = map; |
6990 | pointer_to_map = &map->next; | |
252b5132 | 6991 | |
bc67d8a6 | 6992 | if (isec < section_count) |
252b5132 RH |
6993 | { |
6994 | /* We still have not allocated all of the sections to | |
6995 | segments. Create a new segment here, initialise it | |
6996 | and carry on looping. */ | |
dc810e39 AM |
6997 | amt = sizeof (struct elf_segment_map); |
6998 | amt += ((bfd_size_type) section_count - 1) * sizeof (asection *); | |
5964fc3a | 6999 | map = (struct elf_segment_map *) bfd_zalloc (obfd, amt); |
bc67d8a6 | 7000 | if (map == NULL) |
5ed6aba4 NC |
7001 | { |
7002 | free (sections); | |
7003 | return FALSE; | |
7004 | } | |
252b5132 RH |
7005 | |
7006 | /* Initialise the fields of the segment map. Set the physical | |
7007 | physical address to the LMA of the first section that has | |
7008 | not yet been assigned. */ | |
0067a569 AM |
7009 | map->next = NULL; |
7010 | map->p_type = segment->p_type; | |
7011 | map->p_flags = segment->p_flags; | |
7012 | map->p_flags_valid = 1; | |
7013 | map->p_paddr = suggested_lma; | |
5c44b38e | 7014 | map->p_paddr_valid = p_paddr_valid; |
bc67d8a6 | 7015 | map->includes_filehdr = 0; |
0067a569 | 7016 | map->includes_phdrs = 0; |
252b5132 RH |
7017 | } |
7018 | } | |
bc67d8a6 | 7019 | while (isec < section_count); |
252b5132 RH |
7020 | |
7021 | free (sections); | |
7022 | } | |
7023 | ||
12bd6957 | 7024 | elf_seg_map (obfd) = map_first; |
bc67d8a6 NC |
7025 | |
7026 | /* If we had to estimate the number of program headers that were | |
9ad5cbcf | 7027 | going to be needed, then check our estimate now and adjust |
bc67d8a6 NC |
7028 | the offset if necessary. */ |
7029 | if (phdr_adjust_seg != NULL) | |
7030 | { | |
7031 | unsigned int count; | |
c044fabd | 7032 | |
bc67d8a6 | 7033 | for (count = 0, map = map_first; map != NULL; map = map->next) |
c044fabd | 7034 | count++; |
252b5132 | 7035 | |
bc67d8a6 NC |
7036 | if (count > phdr_adjust_num) |
7037 | phdr_adjust_seg->p_paddr | |
7038 | -= (count - phdr_adjust_num) * iehdr->e_phentsize; | |
7039 | } | |
c044fabd | 7040 | |
bc67d8a6 | 7041 | #undef SEGMENT_END |
eecdbe52 | 7042 | #undef SECTION_SIZE |
bc67d8a6 NC |
7043 | #undef IS_CONTAINED_BY_VMA |
7044 | #undef IS_CONTAINED_BY_LMA | |
0efc80c8 | 7045 | #undef IS_NOTE |
252b5132 | 7046 | #undef IS_COREFILE_NOTE |
bc67d8a6 | 7047 | #undef IS_SOLARIS_PT_INTERP |
9f17e2a6 | 7048 | #undef IS_SECTION_IN_INPUT_SEGMENT |
bc67d8a6 NC |
7049 | #undef INCLUDE_SECTION_IN_SEGMENT |
7050 | #undef SEGMENT_AFTER_SEGMENT | |
7051 | #undef SEGMENT_OVERLAPS | |
b34976b6 | 7052 | return TRUE; |
252b5132 RH |
7053 | } |
7054 | ||
84d1d650 L |
7055 | /* Copy ELF program header information. */ |
7056 | ||
7057 | static bfd_boolean | |
7058 | copy_elf_program_header (bfd *ibfd, bfd *obfd) | |
7059 | { | |
7060 | Elf_Internal_Ehdr *iehdr; | |
7061 | struct elf_segment_map *map; | |
7062 | struct elf_segment_map *map_first; | |
7063 | struct elf_segment_map **pointer_to_map; | |
7064 | Elf_Internal_Phdr *segment; | |
7065 | unsigned int i; | |
7066 | unsigned int num_segments; | |
7067 | bfd_boolean phdr_included = FALSE; | |
88967714 | 7068 | bfd_boolean p_paddr_valid; |
84d1d650 L |
7069 | |
7070 | iehdr = elf_elfheader (ibfd); | |
7071 | ||
7072 | map_first = NULL; | |
7073 | pointer_to_map = &map_first; | |
7074 | ||
88967714 AM |
7075 | /* If all the segment p_paddr fields are zero, don't set |
7076 | map->p_paddr_valid. */ | |
7077 | p_paddr_valid = FALSE; | |
84d1d650 | 7078 | num_segments = elf_elfheader (ibfd)->e_phnum; |
88967714 AM |
7079 | for (i = 0, segment = elf_tdata (ibfd)->phdr; |
7080 | i < num_segments; | |
7081 | i++, segment++) | |
7082 | if (segment->p_paddr != 0) | |
7083 | { | |
7084 | p_paddr_valid = TRUE; | |
7085 | break; | |
7086 | } | |
7087 | ||
84d1d650 L |
7088 | for (i = 0, segment = elf_tdata (ibfd)->phdr; |
7089 | i < num_segments; | |
7090 | i++, segment++) | |
7091 | { | |
7092 | asection *section; | |
7093 | unsigned int section_count; | |
7094 | bfd_size_type amt; | |
7095 | Elf_Internal_Shdr *this_hdr; | |
53020534 | 7096 | asection *first_section = NULL; |
a76e6f2f | 7097 | asection *lowest_section; |
84d1d650 | 7098 | |
84d1d650 L |
7099 | /* Compute how many sections are in this segment. */ |
7100 | for (section = ibfd->sections, section_count = 0; | |
7101 | section != NULL; | |
7102 | section = section->next) | |
7103 | { | |
7104 | this_hdr = &(elf_section_data(section)->this_hdr); | |
f4638467 | 7105 | if (ELF_SECTION_IN_SEGMENT (this_hdr, segment)) |
3271a814 | 7106 | { |
a76e6f2f AM |
7107 | if (first_section == NULL) |
7108 | first_section = section; | |
3271a814 NS |
7109 | section_count++; |
7110 | } | |
84d1d650 L |
7111 | } |
7112 | ||
7113 | /* Allocate a segment map big enough to contain | |
7114 | all of the sections we have selected. */ | |
7115 | amt = sizeof (struct elf_segment_map); | |
7116 | if (section_count != 0) | |
7117 | amt += ((bfd_size_type) section_count - 1) * sizeof (asection *); | |
a50b1753 | 7118 | map = (struct elf_segment_map *) bfd_zalloc (obfd, amt); |
84d1d650 L |
7119 | if (map == NULL) |
7120 | return FALSE; | |
7121 | ||
7122 | /* Initialize the fields of the output segment map with the | |
7123 | input segment. */ | |
7124 | map->next = NULL; | |
7125 | map->p_type = segment->p_type; | |
7126 | map->p_flags = segment->p_flags; | |
7127 | map->p_flags_valid = 1; | |
7128 | map->p_paddr = segment->p_paddr; | |
88967714 | 7129 | map->p_paddr_valid = p_paddr_valid; |
3f570048 AM |
7130 | map->p_align = segment->p_align; |
7131 | map->p_align_valid = 1; | |
3271a814 | 7132 | map->p_vaddr_offset = 0; |
84d1d650 | 7133 | |
04c3a755 NS |
7134 | if (map->p_type == PT_GNU_RELRO |
7135 | || map->p_type == PT_GNU_STACK) | |
b10a8ae0 L |
7136 | { |
7137 | /* The PT_GNU_RELRO segment may contain the first a few | |
7138 | bytes in the .got.plt section even if the whole .got.plt | |
7139 | section isn't in the PT_GNU_RELRO segment. We won't | |
04c3a755 NS |
7140 | change the size of the PT_GNU_RELRO segment. |
7141 | Similarly, PT_GNU_STACK size is significant on uclinux | |
7142 | systems. */ | |
9433b9b1 | 7143 | map->p_size = segment->p_memsz; |
b10a8ae0 L |
7144 | map->p_size_valid = 1; |
7145 | } | |
7146 | ||
84d1d650 L |
7147 | /* Determine if this segment contains the ELF file header |
7148 | and if it contains the program headers themselves. */ | |
7149 | map->includes_filehdr = (segment->p_offset == 0 | |
7150 | && segment->p_filesz >= iehdr->e_ehsize); | |
7151 | ||
7152 | map->includes_phdrs = 0; | |
7153 | if (! phdr_included || segment->p_type != PT_LOAD) | |
7154 | { | |
7155 | map->includes_phdrs = | |
7156 | (segment->p_offset <= (bfd_vma) iehdr->e_phoff | |
7157 | && (segment->p_offset + segment->p_filesz | |
7158 | >= ((bfd_vma) iehdr->e_phoff | |
7159 | + iehdr->e_phnum * iehdr->e_phentsize))); | |
7160 | ||
7161 | if (segment->p_type == PT_LOAD && map->includes_phdrs) | |
7162 | phdr_included = TRUE; | |
7163 | } | |
7164 | ||
bbefd0a9 | 7165 | lowest_section = NULL; |
84d1d650 L |
7166 | if (section_count != 0) |
7167 | { | |
7168 | unsigned int isec = 0; | |
7169 | ||
53020534 | 7170 | for (section = first_section; |
84d1d650 L |
7171 | section != NULL; |
7172 | section = section->next) | |
7173 | { | |
7174 | this_hdr = &(elf_section_data(section)->this_hdr); | |
f4638467 | 7175 | if (ELF_SECTION_IN_SEGMENT (this_hdr, segment)) |
53020534 L |
7176 | { |
7177 | map->sections[isec++] = section->output_section; | |
a76e6f2f AM |
7178 | if ((section->flags & SEC_ALLOC) != 0) |
7179 | { | |
7180 | bfd_vma seg_off; | |
7181 | ||
bbefd0a9 AM |
7182 | if (lowest_section == NULL |
7183 | || section->lma < lowest_section->lma) | |
fb8a5684 AM |
7184 | lowest_section = section; |
7185 | ||
a76e6f2f AM |
7186 | /* Section lmas are set up from PT_LOAD header |
7187 | p_paddr in _bfd_elf_make_section_from_shdr. | |
7188 | If this header has a p_paddr that disagrees | |
7189 | with the section lma, flag the p_paddr as | |
7190 | invalid. */ | |
7191 | if ((section->flags & SEC_LOAD) != 0) | |
7192 | seg_off = this_hdr->sh_offset - segment->p_offset; | |
7193 | else | |
7194 | seg_off = this_hdr->sh_addr - segment->p_vaddr; | |
7195 | if (section->lma - segment->p_paddr != seg_off) | |
7196 | map->p_paddr_valid = FALSE; | |
7197 | } | |
53020534 L |
7198 | if (isec == section_count) |
7199 | break; | |
7200 | } | |
84d1d650 L |
7201 | } |
7202 | } | |
7203 | ||
a76e6f2f AM |
7204 | if (map->includes_filehdr && lowest_section != NULL) |
7205 | /* We need to keep the space used by the headers fixed. */ | |
7206 | map->header_size = lowest_section->vma - segment->p_vaddr; | |
d324f6d6 | 7207 | |
a76e6f2f AM |
7208 | if (!map->includes_phdrs |
7209 | && !map->includes_filehdr | |
7210 | && map->p_paddr_valid) | |
7211 | /* There is some other padding before the first section. */ | |
7212 | map->p_vaddr_offset = ((lowest_section ? lowest_section->lma : 0) | |
7213 | - segment->p_paddr); | |
7214 | ||
84d1d650 L |
7215 | map->count = section_count; |
7216 | *pointer_to_map = map; | |
7217 | pointer_to_map = &map->next; | |
7218 | } | |
7219 | ||
12bd6957 | 7220 | elf_seg_map (obfd) = map_first; |
84d1d650 L |
7221 | return TRUE; |
7222 | } | |
7223 | ||
7224 | /* Copy private BFD data. This copies or rewrites ELF program header | |
7225 | information. */ | |
7226 | ||
7227 | static bfd_boolean | |
7228 | copy_private_bfd_data (bfd *ibfd, bfd *obfd) | |
7229 | { | |
84d1d650 L |
7230 | if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour |
7231 | || bfd_get_flavour (obfd) != bfd_target_elf_flavour) | |
7232 | return TRUE; | |
7233 | ||
7234 | if (elf_tdata (ibfd)->phdr == NULL) | |
7235 | return TRUE; | |
7236 | ||
7237 | if (ibfd->xvec == obfd->xvec) | |
7238 | { | |
cb3ff1e5 NC |
7239 | /* Check to see if any sections in the input BFD |
7240 | covered by ELF program header have changed. */ | |
d55ce4e2 | 7241 | Elf_Internal_Phdr *segment; |
84d1d650 L |
7242 | asection *section, *osec; |
7243 | unsigned int i, num_segments; | |
7244 | Elf_Internal_Shdr *this_hdr; | |
147d51c2 L |
7245 | const struct elf_backend_data *bed; |
7246 | ||
7247 | bed = get_elf_backend_data (ibfd); | |
7248 | ||
7249 | /* Regenerate the segment map if p_paddr is set to 0. */ | |
7250 | if (bed->want_p_paddr_set_to_zero) | |
7251 | goto rewrite; | |
84d1d650 L |
7252 | |
7253 | /* Initialize the segment mark field. */ | |
7254 | for (section = obfd->sections; section != NULL; | |
7255 | section = section->next) | |
7256 | section->segment_mark = FALSE; | |
7257 | ||
7258 | num_segments = elf_elfheader (ibfd)->e_phnum; | |
7259 | for (i = 0, segment = elf_tdata (ibfd)->phdr; | |
7260 | i < num_segments; | |
7261 | i++, segment++) | |
7262 | { | |
5f6999aa NC |
7263 | /* PR binutils/3535. The Solaris linker always sets the p_paddr |
7264 | and p_memsz fields of special segments (DYNAMIC, INTERP) to 0 | |
7265 | which severly confuses things, so always regenerate the segment | |
7266 | map in this case. */ | |
7267 | if (segment->p_paddr == 0 | |
7268 | && segment->p_memsz == 0 | |
7269 | && (segment->p_type == PT_INTERP || segment->p_type == PT_DYNAMIC)) | |
cb3ff1e5 | 7270 | goto rewrite; |
5f6999aa | 7271 | |
84d1d650 L |
7272 | for (section = ibfd->sections; |
7273 | section != NULL; section = section->next) | |
7274 | { | |
7275 | /* We mark the output section so that we know it comes | |
7276 | from the input BFD. */ | |
7277 | osec = section->output_section; | |
7278 | if (osec) | |
7279 | osec->segment_mark = TRUE; | |
7280 | ||
7281 | /* Check if this section is covered by the segment. */ | |
7282 | this_hdr = &(elf_section_data(section)->this_hdr); | |
f4638467 | 7283 | if (ELF_SECTION_IN_SEGMENT (this_hdr, segment)) |
84d1d650 L |
7284 | { |
7285 | /* FIXME: Check if its output section is changed or | |
7286 | removed. What else do we need to check? */ | |
7287 | if (osec == NULL | |
7288 | || section->flags != osec->flags | |
7289 | || section->lma != osec->lma | |
7290 | || section->vma != osec->vma | |
7291 | || section->size != osec->size | |
7292 | || section->rawsize != osec->rawsize | |
7293 | || section->alignment_power != osec->alignment_power) | |
7294 | goto rewrite; | |
7295 | } | |
7296 | } | |
7297 | } | |
7298 | ||
cb3ff1e5 | 7299 | /* Check to see if any output section do not come from the |
84d1d650 L |
7300 | input BFD. */ |
7301 | for (section = obfd->sections; section != NULL; | |
7302 | section = section->next) | |
7303 | { | |
7304 | if (section->segment_mark == FALSE) | |
7305 | goto rewrite; | |
7306 | else | |
7307 | section->segment_mark = FALSE; | |
7308 | } | |
7309 | ||
7310 | return copy_elf_program_header (ibfd, obfd); | |
7311 | } | |
7312 | ||
7313 | rewrite: | |
f1d85785 L |
7314 | if (ibfd->xvec == obfd->xvec) |
7315 | { | |
7316 | /* When rewriting program header, set the output maxpagesize to | |
7317 | the maximum alignment of input PT_LOAD segments. */ | |
7318 | Elf_Internal_Phdr *segment; | |
7319 | unsigned int i; | |
7320 | unsigned int num_segments = elf_elfheader (ibfd)->e_phnum; | |
7321 | bfd_vma maxpagesize = 0; | |
7322 | ||
7323 | for (i = 0, segment = elf_tdata (ibfd)->phdr; | |
7324 | i < num_segments; | |
7325 | i++, segment++) | |
7326 | if (segment->p_type == PT_LOAD | |
7327 | && maxpagesize < segment->p_align) | |
c86934ce NC |
7328 | { |
7329 | /* PR 17512: file: f17299af. */ | |
7330 | if (segment->p_align > (bfd_vma) 1 << ((sizeof (bfd_vma) * 8) - 2)) | |
695344c0 | 7331 | /* xgettext:c-format */ |
4eca0228 | 7332 | _bfd_error_handler (_("\ |
c86934ce | 7333 | %B: warning: segment alignment of 0x%llx is too large"), |
4eca0228 | 7334 | ibfd, (long long) segment->p_align); |
c86934ce NC |
7335 | else |
7336 | maxpagesize = segment->p_align; | |
7337 | } | |
f1d85785 L |
7338 | |
7339 | if (maxpagesize != get_elf_backend_data (obfd)->maxpagesize) | |
7340 | bfd_emul_set_maxpagesize (bfd_get_target (obfd), maxpagesize); | |
7341 | } | |
7342 | ||
84d1d650 L |
7343 | return rewrite_elf_program_header (ibfd, obfd); |
7344 | } | |
7345 | ||
ccd2ec6a L |
7346 | /* Initialize private output section information from input section. */ |
7347 | ||
7348 | bfd_boolean | |
7349 | _bfd_elf_init_private_section_data (bfd *ibfd, | |
7350 | asection *isec, | |
7351 | bfd *obfd, | |
7352 | asection *osec, | |
7353 | struct bfd_link_info *link_info) | |
7354 | ||
7355 | { | |
7356 | Elf_Internal_Shdr *ihdr, *ohdr; | |
0e1862bb L |
7357 | bfd_boolean final_link = (link_info != NULL |
7358 | && !bfd_link_relocatable (link_info)); | |
ccd2ec6a L |
7359 | |
7360 | if (ibfd->xvec->flavour != bfd_target_elf_flavour | |
7361 | || obfd->xvec->flavour != bfd_target_elf_flavour) | |
7362 | return TRUE; | |
7363 | ||
ba85c43e NC |
7364 | BFD_ASSERT (elf_section_data (osec) != NULL); |
7365 | ||
dfa7b0b8 AM |
7366 | /* For objcopy and relocatable link, don't copy the output ELF |
7367 | section type from input if the output BFD section flags have been | |
7368 | set to something different. For a final link allow some flags | |
7369 | that the linker clears to differ. */ | |
42bb2e33 | 7370 | if (elf_section_type (osec) == SHT_NULL |
dfa7b0b8 AM |
7371 | && (osec->flags == isec->flags |
7372 | || (final_link | |
7373 | && ((osec->flags ^ isec->flags) | |
0814be7d | 7374 | & ~(SEC_LINK_ONCE | SEC_LINK_DUPLICATES | SEC_RELOC)) == 0))) |
42bb2e33 | 7375 | elf_section_type (osec) = elf_section_type (isec); |
d270463e L |
7376 | |
7377 | /* FIXME: Is this correct for all OS/PROC specific flags? */ | |
7378 | elf_section_flags (osec) |= (elf_section_flags (isec) | |
7379 | & (SHF_MASKOS | SHF_MASKPROC)); | |
ccd2ec6a L |
7380 | |
7381 | /* Set things up for objcopy and relocatable link. The output | |
7382 | SHT_GROUP section will have its elf_next_in_group pointing back | |
7383 | to the input group members. Ignore linker created group section. | |
7384 | See elfNN_ia64_object_p in elfxx-ia64.c. */ | |
dfa7b0b8 | 7385 | if (!final_link) |
ccd2ec6a L |
7386 | { |
7387 | if (elf_sec_group (isec) == NULL | |
7388 | || (elf_sec_group (isec)->flags & SEC_LINKER_CREATED) == 0) | |
7389 | { | |
7390 | if (elf_section_flags (isec) & SHF_GROUP) | |
7391 | elf_section_flags (osec) |= SHF_GROUP; | |
7392 | elf_next_in_group (osec) = elf_next_in_group (isec); | |
9659de1c | 7393 | elf_section_data (osec)->group = elf_section_data (isec)->group; |
ccd2ec6a | 7394 | } |
68f5ff89 L |
7395 | |
7396 | /* If not decompress, preserve SHF_COMPRESSED. */ | |
7397 | if ((ibfd->flags & BFD_DECOMPRESS) == 0) | |
7398 | elf_section_flags (osec) |= (elf_section_flags (isec) | |
7399 | & SHF_COMPRESSED); | |
ccd2ec6a L |
7400 | } |
7401 | ||
7402 | ihdr = &elf_section_data (isec)->this_hdr; | |
7403 | ||
7404 | /* We need to handle elf_linked_to_section for SHF_LINK_ORDER. We | |
7405 | don't use the output section of the linked-to section since it | |
7406 | may be NULL at this point. */ | |
7407 | if ((ihdr->sh_flags & SHF_LINK_ORDER) != 0) | |
7408 | { | |
7409 | ohdr = &elf_section_data (osec)->this_hdr; | |
7410 | ohdr->sh_flags |= SHF_LINK_ORDER; | |
7411 | elf_linked_to_section (osec) = elf_linked_to_section (isec); | |
7412 | } | |
7413 | ||
7414 | osec->use_rela_p = isec->use_rela_p; | |
7415 | ||
7416 | return TRUE; | |
7417 | } | |
7418 | ||
252b5132 RH |
7419 | /* Copy private section information. This copies over the entsize |
7420 | field, and sometimes the info field. */ | |
7421 | ||
b34976b6 | 7422 | bfd_boolean |
217aa764 AM |
7423 | _bfd_elf_copy_private_section_data (bfd *ibfd, |
7424 | asection *isec, | |
7425 | bfd *obfd, | |
7426 | asection *osec) | |
252b5132 RH |
7427 | { |
7428 | Elf_Internal_Shdr *ihdr, *ohdr; | |
7429 | ||
7430 | if (ibfd->xvec->flavour != bfd_target_elf_flavour | |
7431 | || obfd->xvec->flavour != bfd_target_elf_flavour) | |
b34976b6 | 7432 | return TRUE; |
252b5132 | 7433 | |
252b5132 RH |
7434 | ihdr = &elf_section_data (isec)->this_hdr; |
7435 | ohdr = &elf_section_data (osec)->this_hdr; | |
7436 | ||
7437 | ohdr->sh_entsize = ihdr->sh_entsize; | |
7438 | ||
7439 | if (ihdr->sh_type == SHT_SYMTAB | |
7440 | || ihdr->sh_type == SHT_DYNSYM | |
7441 | || ihdr->sh_type == SHT_GNU_verneed | |
7442 | || ihdr->sh_type == SHT_GNU_verdef) | |
7443 | ohdr->sh_info = ihdr->sh_info; | |
7444 | ||
ccd2ec6a L |
7445 | return _bfd_elf_init_private_section_data (ibfd, isec, obfd, osec, |
7446 | NULL); | |
252b5132 RH |
7447 | } |
7448 | ||
d0bf826b AM |
7449 | /* Look at all the SHT_GROUP sections in IBFD, making any adjustments |
7450 | necessary if we are removing either the SHT_GROUP section or any of | |
7451 | the group member sections. DISCARDED is the value that a section's | |
7452 | output_section has if the section will be discarded, NULL when this | |
7453 | function is called from objcopy, bfd_abs_section_ptr when called | |
7454 | from the linker. */ | |
80fccad2 BW |
7455 | |
7456 | bfd_boolean | |
d0bf826b | 7457 | _bfd_elf_fixup_group_sections (bfd *ibfd, asection *discarded) |
80fccad2 | 7458 | { |
30288845 AM |
7459 | asection *isec; |
7460 | ||
30288845 | 7461 | for (isec = ibfd->sections; isec != NULL; isec = isec->next) |
415f38a6 | 7462 | if (elf_section_type (isec) == SHT_GROUP) |
30288845 AM |
7463 | { |
7464 | asection *first = elf_next_in_group (isec); | |
7465 | asection *s = first; | |
d0bf826b AM |
7466 | bfd_size_type removed = 0; |
7467 | ||
30288845 AM |
7468 | while (s != NULL) |
7469 | { | |
415f38a6 AM |
7470 | /* If this member section is being output but the |
7471 | SHT_GROUP section is not, then clear the group info | |
7472 | set up by _bfd_elf_copy_private_section_data. */ | |
d0bf826b AM |
7473 | if (s->output_section != discarded |
7474 | && isec->output_section == discarded) | |
30288845 AM |
7475 | { |
7476 | elf_section_flags (s->output_section) &= ~SHF_GROUP; | |
7477 | elf_group_name (s->output_section) = NULL; | |
7478 | } | |
415f38a6 AM |
7479 | /* Conversely, if the member section is not being output |
7480 | but the SHT_GROUP section is, then adjust its size. */ | |
d0bf826b AM |
7481 | else if (s->output_section == discarded |
7482 | && isec->output_section != discarded) | |
7483 | removed += 4; | |
30288845 AM |
7484 | s = elf_next_in_group (s); |
7485 | if (s == first) | |
7486 | break; | |
7487 | } | |
d0bf826b AM |
7488 | if (removed != 0) |
7489 | { | |
7490 | if (discarded != NULL) | |
7491 | { | |
7492 | /* If we've been called for ld -r, then we need to | |
7493 | adjust the input section size. This function may | |
7494 | be called multiple times, so save the original | |
7495 | size. */ | |
7496 | if (isec->rawsize == 0) | |
7497 | isec->rawsize = isec->size; | |
7498 | isec->size = isec->rawsize - removed; | |
7499 | } | |
7500 | else | |
7501 | { | |
7502 | /* Adjust the output section size when called from | |
7503 | objcopy. */ | |
7504 | isec->output_section->size -= removed; | |
7505 | } | |
7506 | } | |
30288845 AM |
7507 | } |
7508 | ||
80fccad2 BW |
7509 | return TRUE; |
7510 | } | |
7511 | ||
d0bf826b AM |
7512 | /* Copy private header information. */ |
7513 | ||
7514 | bfd_boolean | |
7515 | _bfd_elf_copy_private_header_data (bfd *ibfd, bfd *obfd) | |
7516 | { | |
7517 | if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour | |
7518 | || bfd_get_flavour (obfd) != bfd_target_elf_flavour) | |
7519 | return TRUE; | |
7520 | ||
7521 | /* Copy over private BFD data if it has not already been copied. | |
7522 | This must be done here, rather than in the copy_private_bfd_data | |
7523 | entry point, because the latter is called after the section | |
7524 | contents have been set, which means that the program headers have | |
7525 | already been worked out. */ | |
12bd6957 | 7526 | if (elf_seg_map (obfd) == NULL && elf_tdata (ibfd)->phdr != NULL) |
d0bf826b AM |
7527 | { |
7528 | if (! copy_private_bfd_data (ibfd, obfd)) | |
7529 | return FALSE; | |
7530 | } | |
7531 | ||
7532 | return _bfd_elf_fixup_group_sections (ibfd, NULL); | |
7533 | } | |
7534 | ||
252b5132 RH |
7535 | /* Copy private symbol information. If this symbol is in a section |
7536 | which we did not map into a BFD section, try to map the section | |
7537 | index correctly. We use special macro definitions for the mapped | |
7538 | section indices; these definitions are interpreted by the | |
7539 | swap_out_syms function. */ | |
7540 | ||
9ad5cbcf AM |
7541 | #define MAP_ONESYMTAB (SHN_HIOS + 1) |
7542 | #define MAP_DYNSYMTAB (SHN_HIOS + 2) | |
7543 | #define MAP_STRTAB (SHN_HIOS + 3) | |
7544 | #define MAP_SHSTRTAB (SHN_HIOS + 4) | |
7545 | #define MAP_SYM_SHNDX (SHN_HIOS + 5) | |
252b5132 | 7546 | |
b34976b6 | 7547 | bfd_boolean |
217aa764 AM |
7548 | _bfd_elf_copy_private_symbol_data (bfd *ibfd, |
7549 | asymbol *isymarg, | |
7550 | bfd *obfd, | |
7551 | asymbol *osymarg) | |
252b5132 RH |
7552 | { |
7553 | elf_symbol_type *isym, *osym; | |
7554 | ||
7555 | if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour | |
7556 | || bfd_get_flavour (obfd) != bfd_target_elf_flavour) | |
b34976b6 | 7557 | return TRUE; |
252b5132 RH |
7558 | |
7559 | isym = elf_symbol_from (ibfd, isymarg); | |
7560 | osym = elf_symbol_from (obfd, osymarg); | |
7561 | ||
7562 | if (isym != NULL | |
8424d8f5 | 7563 | && isym->internal_elf_sym.st_shndx != 0 |
252b5132 RH |
7564 | && osym != NULL |
7565 | && bfd_is_abs_section (isym->symbol.section)) | |
7566 | { | |
7567 | unsigned int shndx; | |
7568 | ||
7569 | shndx = isym->internal_elf_sym.st_shndx; | |
7570 | if (shndx == elf_onesymtab (ibfd)) | |
7571 | shndx = MAP_ONESYMTAB; | |
7572 | else if (shndx == elf_dynsymtab (ibfd)) | |
7573 | shndx = MAP_DYNSYMTAB; | |
12bd6957 | 7574 | else if (shndx == elf_strtab_sec (ibfd)) |
252b5132 | 7575 | shndx = MAP_STRTAB; |
12bd6957 | 7576 | else if (shndx == elf_shstrtab_sec (ibfd)) |
252b5132 | 7577 | shndx = MAP_SHSTRTAB; |
6a40cf0c | 7578 | else if (find_section_in_list (shndx, elf_symtab_shndx_list (ibfd))) |
9ad5cbcf | 7579 | shndx = MAP_SYM_SHNDX; |
252b5132 RH |
7580 | osym->internal_elf_sym.st_shndx = shndx; |
7581 | } | |
7582 | ||
b34976b6 | 7583 | return TRUE; |
252b5132 RH |
7584 | } |
7585 | ||
7586 | /* Swap out the symbols. */ | |
7587 | ||
b34976b6 | 7588 | static bfd_boolean |
217aa764 | 7589 | swap_out_syms (bfd *abfd, |
ef10c3ac | 7590 | struct elf_strtab_hash **sttp, |
217aa764 | 7591 | int relocatable_p) |
252b5132 | 7592 | { |
9c5bfbb7 | 7593 | const struct elf_backend_data *bed; |
079e9a2f AM |
7594 | int symcount; |
7595 | asymbol **syms; | |
ef10c3ac | 7596 | struct elf_strtab_hash *stt; |
079e9a2f | 7597 | Elf_Internal_Shdr *symtab_hdr; |
9ad5cbcf | 7598 | Elf_Internal_Shdr *symtab_shndx_hdr; |
079e9a2f | 7599 | Elf_Internal_Shdr *symstrtab_hdr; |
ef10c3ac | 7600 | struct elf_sym_strtab *symstrtab; |
f075ee0c AM |
7601 | bfd_byte *outbound_syms; |
7602 | bfd_byte *outbound_shndx; | |
ef10c3ac L |
7603 | unsigned long outbound_syms_index; |
7604 | unsigned long outbound_shndx_index; | |
079e9a2f | 7605 | int idx; |
12bd6957 | 7606 | unsigned int num_locals; |
079e9a2f | 7607 | bfd_size_type amt; |
174fd7f9 | 7608 | bfd_boolean name_local_sections; |
252b5132 | 7609 | |
12bd6957 | 7610 | if (!elf_map_symbols (abfd, &num_locals)) |
b34976b6 | 7611 | return FALSE; |
252b5132 | 7612 | |
c044fabd | 7613 | /* Dump out the symtabs. */ |
ef10c3ac | 7614 | stt = _bfd_elf_strtab_init (); |
079e9a2f | 7615 | if (stt == NULL) |
b34976b6 | 7616 | return FALSE; |
252b5132 | 7617 | |
079e9a2f AM |
7618 | bed = get_elf_backend_data (abfd); |
7619 | symcount = bfd_get_symcount (abfd); | |
7620 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; | |
7621 | symtab_hdr->sh_type = SHT_SYMTAB; | |
7622 | symtab_hdr->sh_entsize = bed->s->sizeof_sym; | |
7623 | symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1); | |
12bd6957 | 7624 | symtab_hdr->sh_info = num_locals + 1; |
72de5009 | 7625 | symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align; |
079e9a2f AM |
7626 | |
7627 | symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr; | |
7628 | symstrtab_hdr->sh_type = SHT_STRTAB; | |
7629 | ||
ef10c3ac L |
7630 | /* Allocate buffer to swap out the .strtab section. */ |
7631 | symstrtab = (struct elf_sym_strtab *) bfd_malloc ((symcount + 1) | |
7632 | * sizeof (*symstrtab)); | |
7633 | if (symstrtab == NULL) | |
7634 | { | |
7635 | _bfd_elf_strtab_free (stt); | |
7636 | return FALSE; | |
7637 | } | |
7638 | ||
a50b1753 NC |
7639 | outbound_syms = (bfd_byte *) bfd_alloc2 (abfd, 1 + symcount, |
7640 | bed->s->sizeof_sym); | |
079e9a2f | 7641 | if (outbound_syms == NULL) |
5ed6aba4 | 7642 | { |
ef10c3ac L |
7643 | error_return: |
7644 | _bfd_elf_strtab_free (stt); | |
7645 | free (symstrtab); | |
5ed6aba4 NC |
7646 | return FALSE; |
7647 | } | |
217aa764 | 7648 | symtab_hdr->contents = outbound_syms; |
ef10c3ac | 7649 | outbound_syms_index = 0; |
252b5132 | 7650 | |
9ad5cbcf | 7651 | outbound_shndx = NULL; |
ef10c3ac | 7652 | outbound_shndx_index = 0; |
6a40cf0c NC |
7653 | |
7654 | if (elf_symtab_shndx_list (abfd)) | |
9ad5cbcf | 7655 | { |
6a40cf0c NC |
7656 | symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr; |
7657 | if (symtab_shndx_hdr->sh_name != 0) | |
7658 | { | |
7659 | amt = (bfd_size_type) (1 + symcount) * sizeof (Elf_External_Sym_Shndx); | |
7660 | outbound_shndx = (bfd_byte *) | |
7661 | bfd_zalloc2 (abfd, 1 + symcount, sizeof (Elf_External_Sym_Shndx)); | |
7662 | if (outbound_shndx == NULL) | |
7663 | goto error_return; | |
5ed6aba4 | 7664 | |
6a40cf0c NC |
7665 | symtab_shndx_hdr->contents = outbound_shndx; |
7666 | symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX; | |
7667 | symtab_shndx_hdr->sh_size = amt; | |
7668 | symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx); | |
7669 | symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx); | |
7670 | } | |
7671 | /* FIXME: What about any other headers in the list ? */ | |
9ad5cbcf AM |
7672 | } |
7673 | ||
589e6347 | 7674 | /* Now generate the data (for "contents"). */ |
079e9a2f AM |
7675 | { |
7676 | /* Fill in zeroth symbol and swap it out. */ | |
7677 | Elf_Internal_Sym sym; | |
7678 | sym.st_name = 0; | |
7679 | sym.st_value = 0; | |
7680 | sym.st_size = 0; | |
7681 | sym.st_info = 0; | |
7682 | sym.st_other = 0; | |
7683 | sym.st_shndx = SHN_UNDEF; | |
35fc36a8 | 7684 | sym.st_target_internal = 0; |
ef10c3ac L |
7685 | symstrtab[0].sym = sym; |
7686 | symstrtab[0].dest_index = outbound_syms_index; | |
7687 | symstrtab[0].destshndx_index = outbound_shndx_index; | |
7688 | outbound_syms_index++; | |
9ad5cbcf | 7689 | if (outbound_shndx != NULL) |
ef10c3ac | 7690 | outbound_shndx_index++; |
079e9a2f | 7691 | } |
252b5132 | 7692 | |
174fd7f9 RS |
7693 | name_local_sections |
7694 | = (bed->elf_backend_name_local_section_symbols | |
7695 | && bed->elf_backend_name_local_section_symbols (abfd)); | |
7696 | ||
079e9a2f | 7697 | syms = bfd_get_outsymbols (abfd); |
ef10c3ac | 7698 | for (idx = 0; idx < symcount;) |
252b5132 | 7699 | { |
252b5132 | 7700 | Elf_Internal_Sym sym; |
079e9a2f AM |
7701 | bfd_vma value = syms[idx]->value; |
7702 | elf_symbol_type *type_ptr; | |
7703 | flagword flags = syms[idx]->flags; | |
7704 | int type; | |
252b5132 | 7705 | |
174fd7f9 RS |
7706 | if (!name_local_sections |
7707 | && (flags & (BSF_SECTION_SYM | BSF_GLOBAL)) == BSF_SECTION_SYM) | |
079e9a2f AM |
7708 | { |
7709 | /* Local section symbols have no name. */ | |
ef10c3ac | 7710 | sym.st_name = (unsigned long) -1; |
079e9a2f AM |
7711 | } |
7712 | else | |
7713 | { | |
ef10c3ac L |
7714 | /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize |
7715 | to get the final offset for st_name. */ | |
7716 | sym.st_name | |
7717 | = (unsigned long) _bfd_elf_strtab_add (stt, syms[idx]->name, | |
7718 | FALSE); | |
079e9a2f | 7719 | if (sym.st_name == (unsigned long) -1) |
ef10c3ac | 7720 | goto error_return; |
079e9a2f | 7721 | } |
252b5132 | 7722 | |
079e9a2f | 7723 | type_ptr = elf_symbol_from (abfd, syms[idx]); |
252b5132 | 7724 | |
079e9a2f AM |
7725 | if ((flags & BSF_SECTION_SYM) == 0 |
7726 | && bfd_is_com_section (syms[idx]->section)) | |
7727 | { | |
7728 | /* ELF common symbols put the alignment into the `value' field, | |
7729 | and the size into the `size' field. This is backwards from | |
7730 | how BFD handles it, so reverse it here. */ | |
7731 | sym.st_size = value; | |
7732 | if (type_ptr == NULL | |
7733 | || type_ptr->internal_elf_sym.st_value == 0) | |
7734 | sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value)); | |
7735 | else | |
7736 | sym.st_value = type_ptr->internal_elf_sym.st_value; | |
7737 | sym.st_shndx = _bfd_elf_section_from_bfd_section | |
7738 | (abfd, syms[idx]->section); | |
7739 | } | |
7740 | else | |
7741 | { | |
7742 | asection *sec = syms[idx]->section; | |
cb33740c | 7743 | unsigned int shndx; |
252b5132 | 7744 | |
079e9a2f AM |
7745 | if (sec->output_section) |
7746 | { | |
7747 | value += sec->output_offset; | |
7748 | sec = sec->output_section; | |
7749 | } | |
589e6347 | 7750 | |
079e9a2f AM |
7751 | /* Don't add in the section vma for relocatable output. */ |
7752 | if (! relocatable_p) | |
7753 | value += sec->vma; | |
7754 | sym.st_value = value; | |
7755 | sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0; | |
7756 | ||
7757 | if (bfd_is_abs_section (sec) | |
7758 | && type_ptr != NULL | |
7759 | && type_ptr->internal_elf_sym.st_shndx != 0) | |
7760 | { | |
7761 | /* This symbol is in a real ELF section which we did | |
7762 | not create as a BFD section. Undo the mapping done | |
7763 | by copy_private_symbol_data. */ | |
7764 | shndx = type_ptr->internal_elf_sym.st_shndx; | |
7765 | switch (shndx) | |
7766 | { | |
7767 | case MAP_ONESYMTAB: | |
7768 | shndx = elf_onesymtab (abfd); | |
7769 | break; | |
7770 | case MAP_DYNSYMTAB: | |
7771 | shndx = elf_dynsymtab (abfd); | |
7772 | break; | |
7773 | case MAP_STRTAB: | |
12bd6957 | 7774 | shndx = elf_strtab_sec (abfd); |
079e9a2f AM |
7775 | break; |
7776 | case MAP_SHSTRTAB: | |
12bd6957 | 7777 | shndx = elf_shstrtab_sec (abfd); |
079e9a2f | 7778 | break; |
9ad5cbcf | 7779 | case MAP_SYM_SHNDX: |
6a40cf0c NC |
7780 | if (elf_symtab_shndx_list (abfd)) |
7781 | shndx = elf_symtab_shndx_list (abfd)->ndx; | |
9ad5cbcf | 7782 | break; |
079e9a2f | 7783 | default: |
15bc576a | 7784 | shndx = SHN_ABS; |
079e9a2f AM |
7785 | break; |
7786 | } | |
7787 | } | |
7788 | else | |
7789 | { | |
7790 | shndx = _bfd_elf_section_from_bfd_section (abfd, sec); | |
252b5132 | 7791 | |
cb33740c | 7792 | if (shndx == SHN_BAD) |
079e9a2f AM |
7793 | { |
7794 | asection *sec2; | |
7795 | ||
7796 | /* Writing this would be a hell of a lot easier if | |
7797 | we had some decent documentation on bfd, and | |
7798 | knew what to expect of the library, and what to | |
7799 | demand of applications. For example, it | |
7800 | appears that `objcopy' might not set the | |
7801 | section of a symbol to be a section that is | |
7802 | actually in the output file. */ | |
7803 | sec2 = bfd_get_section_by_name (abfd, sec->name); | |
5df1bc57 AM |
7804 | if (sec2 != NULL) |
7805 | shndx = _bfd_elf_section_from_bfd_section (abfd, sec2); | |
7806 | if (shndx == SHN_BAD) | |
589e6347 | 7807 | { |
695344c0 | 7808 | /* xgettext:c-format */ |
589e6347 NC |
7809 | _bfd_error_handler (_("\ |
7810 | Unable to find equivalent output section for symbol '%s' from section '%s'"), | |
7811 | syms[idx]->name ? syms[idx]->name : "<Local sym>", | |
7812 | sec->name); | |
811072d8 | 7813 | bfd_set_error (bfd_error_invalid_operation); |
ef10c3ac | 7814 | goto error_return; |
589e6347 | 7815 | } |
079e9a2f AM |
7816 | } |
7817 | } | |
252b5132 | 7818 | |
079e9a2f AM |
7819 | sym.st_shndx = shndx; |
7820 | } | |
252b5132 | 7821 | |
13ae64f3 JJ |
7822 | if ((flags & BSF_THREAD_LOCAL) != 0) |
7823 | type = STT_TLS; | |
d8045f23 NC |
7824 | else if ((flags & BSF_GNU_INDIRECT_FUNCTION) != 0) |
7825 | type = STT_GNU_IFUNC; | |
13ae64f3 | 7826 | else if ((flags & BSF_FUNCTION) != 0) |
079e9a2f AM |
7827 | type = STT_FUNC; |
7828 | else if ((flags & BSF_OBJECT) != 0) | |
7829 | type = STT_OBJECT; | |
d9352518 DB |
7830 | else if ((flags & BSF_RELC) != 0) |
7831 | type = STT_RELC; | |
7832 | else if ((flags & BSF_SRELC) != 0) | |
7833 | type = STT_SRELC; | |
079e9a2f AM |
7834 | else |
7835 | type = STT_NOTYPE; | |
252b5132 | 7836 | |
13ae64f3 JJ |
7837 | if (syms[idx]->section->flags & SEC_THREAD_LOCAL) |
7838 | type = STT_TLS; | |
7839 | ||
589e6347 | 7840 | /* Processor-specific types. */ |
079e9a2f AM |
7841 | if (type_ptr != NULL |
7842 | && bed->elf_backend_get_symbol_type) | |
7843 | type = ((*bed->elf_backend_get_symbol_type) | |
7844 | (&type_ptr->internal_elf_sym, type)); | |
252b5132 | 7845 | |
079e9a2f AM |
7846 | if (flags & BSF_SECTION_SYM) |
7847 | { | |
7848 | if (flags & BSF_GLOBAL) | |
7849 | sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION); | |
7850 | else | |
7851 | sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION); | |
7852 | } | |
7853 | else if (bfd_is_com_section (syms[idx]->section)) | |
0a40daed | 7854 | { |
b8871f35 L |
7855 | if (type != STT_TLS) |
7856 | { | |
7857 | if ((abfd->flags & BFD_CONVERT_ELF_COMMON)) | |
7858 | type = ((abfd->flags & BFD_USE_ELF_STT_COMMON) | |
7859 | ? STT_COMMON : STT_OBJECT); | |
7860 | else | |
7861 | type = ((flags & BSF_ELF_COMMON) != 0 | |
7862 | ? STT_COMMON : STT_OBJECT); | |
7863 | } | |
7864 | sym.st_info = ELF_ST_INFO (STB_GLOBAL, type); | |
0a40daed | 7865 | } |
079e9a2f AM |
7866 | else if (bfd_is_und_section (syms[idx]->section)) |
7867 | sym.st_info = ELF_ST_INFO (((flags & BSF_WEAK) | |
7868 | ? STB_WEAK | |
7869 | : STB_GLOBAL), | |
7870 | type); | |
7871 | else if (flags & BSF_FILE) | |
7872 | sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE); | |
7873 | else | |
7874 | { | |
7875 | int bind = STB_LOCAL; | |
252b5132 | 7876 | |
079e9a2f AM |
7877 | if (flags & BSF_LOCAL) |
7878 | bind = STB_LOCAL; | |
3e7a7d11 NC |
7879 | else if (flags & BSF_GNU_UNIQUE) |
7880 | bind = STB_GNU_UNIQUE; | |
079e9a2f AM |
7881 | else if (flags & BSF_WEAK) |
7882 | bind = STB_WEAK; | |
7883 | else if (flags & BSF_GLOBAL) | |
7884 | bind = STB_GLOBAL; | |
252b5132 | 7885 | |
079e9a2f AM |
7886 | sym.st_info = ELF_ST_INFO (bind, type); |
7887 | } | |
252b5132 | 7888 | |
079e9a2f | 7889 | if (type_ptr != NULL) |
35fc36a8 RS |
7890 | { |
7891 | sym.st_other = type_ptr->internal_elf_sym.st_other; | |
7892 | sym.st_target_internal | |
7893 | = type_ptr->internal_elf_sym.st_target_internal; | |
7894 | } | |
079e9a2f | 7895 | else |
35fc36a8 RS |
7896 | { |
7897 | sym.st_other = 0; | |
7898 | sym.st_target_internal = 0; | |
7899 | } | |
252b5132 | 7900 | |
ef10c3ac L |
7901 | idx++; |
7902 | symstrtab[idx].sym = sym; | |
7903 | symstrtab[idx].dest_index = outbound_syms_index; | |
7904 | symstrtab[idx].destshndx_index = outbound_shndx_index; | |
7905 | ||
7906 | outbound_syms_index++; | |
9ad5cbcf | 7907 | if (outbound_shndx != NULL) |
ef10c3ac L |
7908 | outbound_shndx_index++; |
7909 | } | |
7910 | ||
7911 | /* Finalize the .strtab section. */ | |
7912 | _bfd_elf_strtab_finalize (stt); | |
7913 | ||
7914 | /* Swap out the .strtab section. */ | |
7915 | for (idx = 0; idx <= symcount; idx++) | |
7916 | { | |
7917 | struct elf_sym_strtab *elfsym = &symstrtab[idx]; | |
7918 | if (elfsym->sym.st_name == (unsigned long) -1) | |
7919 | elfsym->sym.st_name = 0; | |
7920 | else | |
7921 | elfsym->sym.st_name = _bfd_elf_strtab_offset (stt, | |
7922 | elfsym->sym.st_name); | |
7923 | bed->s->swap_symbol_out (abfd, &elfsym->sym, | |
7924 | (outbound_syms | |
7925 | + (elfsym->dest_index | |
7926 | * bed->s->sizeof_sym)), | |
7927 | (outbound_shndx | |
7928 | + (elfsym->destshndx_index | |
7929 | * sizeof (Elf_External_Sym_Shndx)))); | |
079e9a2f | 7930 | } |
ef10c3ac | 7931 | free (symstrtab); |
252b5132 | 7932 | |
079e9a2f | 7933 | *sttp = stt; |
ef10c3ac | 7934 | symstrtab_hdr->sh_size = _bfd_elf_strtab_size (stt); |
079e9a2f | 7935 | symstrtab_hdr->sh_type = SHT_STRTAB; |
84865015 | 7936 | symstrtab_hdr->sh_flags = bed->elf_strtab_flags; |
079e9a2f AM |
7937 | symstrtab_hdr->sh_addr = 0; |
7938 | symstrtab_hdr->sh_entsize = 0; | |
7939 | symstrtab_hdr->sh_link = 0; | |
7940 | symstrtab_hdr->sh_info = 0; | |
7941 | symstrtab_hdr->sh_addralign = 1; | |
252b5132 | 7942 | |
b34976b6 | 7943 | return TRUE; |
252b5132 RH |
7944 | } |
7945 | ||
7946 | /* Return the number of bytes required to hold the symtab vector. | |
7947 | ||
7948 | Note that we base it on the count plus 1, since we will null terminate | |
7949 | the vector allocated based on this size. However, the ELF symbol table | |
7950 | always has a dummy entry as symbol #0, so it ends up even. */ | |
7951 | ||
7952 | long | |
217aa764 | 7953 | _bfd_elf_get_symtab_upper_bound (bfd *abfd) |
252b5132 RH |
7954 | { |
7955 | long symcount; | |
7956 | long symtab_size; | |
7957 | Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr; | |
7958 | ||
7959 | symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym; | |
b99d1833 AM |
7960 | symtab_size = (symcount + 1) * (sizeof (asymbol *)); |
7961 | if (symcount > 0) | |
7962 | symtab_size -= sizeof (asymbol *); | |
252b5132 RH |
7963 | |
7964 | return symtab_size; | |
7965 | } | |
7966 | ||
7967 | long | |
217aa764 | 7968 | _bfd_elf_get_dynamic_symtab_upper_bound (bfd *abfd) |
252b5132 RH |
7969 | { |
7970 | long symcount; | |
7971 | long symtab_size; | |
7972 | Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr; | |
7973 | ||
7974 | if (elf_dynsymtab (abfd) == 0) | |
7975 | { | |
7976 | bfd_set_error (bfd_error_invalid_operation); | |
7977 | return -1; | |
7978 | } | |
7979 | ||
7980 | symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym; | |
b99d1833 AM |
7981 | symtab_size = (symcount + 1) * (sizeof (asymbol *)); |
7982 | if (symcount > 0) | |
7983 | symtab_size -= sizeof (asymbol *); | |
252b5132 RH |
7984 | |
7985 | return symtab_size; | |
7986 | } | |
7987 | ||
7988 | long | |
217aa764 AM |
7989 | _bfd_elf_get_reloc_upper_bound (bfd *abfd ATTRIBUTE_UNUSED, |
7990 | sec_ptr asect) | |
252b5132 RH |
7991 | { |
7992 | return (asect->reloc_count + 1) * sizeof (arelent *); | |
7993 | } | |
7994 | ||
7995 | /* Canonicalize the relocs. */ | |
7996 | ||
7997 | long | |
217aa764 AM |
7998 | _bfd_elf_canonicalize_reloc (bfd *abfd, |
7999 | sec_ptr section, | |
8000 | arelent **relptr, | |
8001 | asymbol **symbols) | |
252b5132 RH |
8002 | { |
8003 | arelent *tblptr; | |
8004 | unsigned int i; | |
9c5bfbb7 | 8005 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
252b5132 | 8006 | |
b34976b6 | 8007 | if (! bed->s->slurp_reloc_table (abfd, section, symbols, FALSE)) |
252b5132 RH |
8008 | return -1; |
8009 | ||
8010 | tblptr = section->relocation; | |
8011 | for (i = 0; i < section->reloc_count; i++) | |
8012 | *relptr++ = tblptr++; | |
8013 | ||
8014 | *relptr = NULL; | |
8015 | ||
8016 | return section->reloc_count; | |
8017 | } | |
8018 | ||
8019 | long | |
6cee3f79 | 8020 | _bfd_elf_canonicalize_symtab (bfd *abfd, asymbol **allocation) |
252b5132 | 8021 | { |
9c5bfbb7 | 8022 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
217aa764 | 8023 | long symcount = bed->s->slurp_symbol_table (abfd, allocation, FALSE); |
252b5132 RH |
8024 | |
8025 | if (symcount >= 0) | |
8026 | bfd_get_symcount (abfd) = symcount; | |
8027 | return symcount; | |
8028 | } | |
8029 | ||
8030 | long | |
217aa764 AM |
8031 | _bfd_elf_canonicalize_dynamic_symtab (bfd *abfd, |
8032 | asymbol **allocation) | |
252b5132 | 8033 | { |
9c5bfbb7 | 8034 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
217aa764 | 8035 | long symcount = bed->s->slurp_symbol_table (abfd, allocation, TRUE); |
1f70368c DJ |
8036 | |
8037 | if (symcount >= 0) | |
8038 | bfd_get_dynamic_symcount (abfd) = symcount; | |
8039 | return symcount; | |
252b5132 RH |
8040 | } |
8041 | ||
8615f3f2 AM |
8042 | /* Return the size required for the dynamic reloc entries. Any loadable |
8043 | section that was actually installed in the BFD, and has type SHT_REL | |
8044 | or SHT_RELA, and uses the dynamic symbol table, is considered to be a | |
8045 | dynamic reloc section. */ | |
252b5132 RH |
8046 | |
8047 | long | |
217aa764 | 8048 | _bfd_elf_get_dynamic_reloc_upper_bound (bfd *abfd) |
252b5132 RH |
8049 | { |
8050 | long ret; | |
8051 | asection *s; | |
8052 | ||
8053 | if (elf_dynsymtab (abfd) == 0) | |
8054 | { | |
8055 | bfd_set_error (bfd_error_invalid_operation); | |
8056 | return -1; | |
8057 | } | |
8058 | ||
8059 | ret = sizeof (arelent *); | |
8060 | for (s = abfd->sections; s != NULL; s = s->next) | |
266b05cf | 8061 | if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd) |
252b5132 RH |
8062 | && (elf_section_data (s)->this_hdr.sh_type == SHT_REL |
8063 | || elf_section_data (s)->this_hdr.sh_type == SHT_RELA)) | |
eea6121a | 8064 | ret += ((s->size / elf_section_data (s)->this_hdr.sh_entsize) |
252b5132 RH |
8065 | * sizeof (arelent *)); |
8066 | ||
8067 | return ret; | |
8068 | } | |
8069 | ||
8615f3f2 AM |
8070 | /* Canonicalize the dynamic relocation entries. Note that we return the |
8071 | dynamic relocations as a single block, although they are actually | |
8072 | associated with particular sections; the interface, which was | |
8073 | designed for SunOS style shared libraries, expects that there is only | |
8074 | one set of dynamic relocs. Any loadable section that was actually | |
8075 | installed in the BFD, and has type SHT_REL or SHT_RELA, and uses the | |
8076 | dynamic symbol table, is considered to be a dynamic reloc section. */ | |
252b5132 RH |
8077 | |
8078 | long | |
217aa764 AM |
8079 | _bfd_elf_canonicalize_dynamic_reloc (bfd *abfd, |
8080 | arelent **storage, | |
8081 | asymbol **syms) | |
252b5132 | 8082 | { |
217aa764 | 8083 | bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean); |
252b5132 RH |
8084 | asection *s; |
8085 | long ret; | |
8086 | ||
8087 | if (elf_dynsymtab (abfd) == 0) | |
8088 | { | |
8089 | bfd_set_error (bfd_error_invalid_operation); | |
8090 | return -1; | |
8091 | } | |
8092 | ||
8093 | slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table; | |
8094 | ret = 0; | |
8095 | for (s = abfd->sections; s != NULL; s = s->next) | |
8096 | { | |
266b05cf | 8097 | if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd) |
252b5132 RH |
8098 | && (elf_section_data (s)->this_hdr.sh_type == SHT_REL |
8099 | || elf_section_data (s)->this_hdr.sh_type == SHT_RELA)) | |
8100 | { | |
8101 | arelent *p; | |
8102 | long count, i; | |
8103 | ||
b34976b6 | 8104 | if (! (*slurp_relocs) (abfd, s, syms, TRUE)) |
252b5132 | 8105 | return -1; |
eea6121a | 8106 | count = s->size / elf_section_data (s)->this_hdr.sh_entsize; |
252b5132 RH |
8107 | p = s->relocation; |
8108 | for (i = 0; i < count; i++) | |
8109 | *storage++ = p++; | |
8110 | ret += count; | |
8111 | } | |
8112 | } | |
8113 | ||
8114 | *storage = NULL; | |
8115 | ||
8116 | return ret; | |
8117 | } | |
8118 | \f | |
8119 | /* Read in the version information. */ | |
8120 | ||
b34976b6 | 8121 | bfd_boolean |
fc0e6df6 | 8122 | _bfd_elf_slurp_version_tables (bfd *abfd, bfd_boolean default_imported_symver) |
252b5132 RH |
8123 | { |
8124 | bfd_byte *contents = NULL; | |
fc0e6df6 PB |
8125 | unsigned int freeidx = 0; |
8126 | ||
8127 | if (elf_dynverref (abfd) != 0) | |
8128 | { | |
8129 | Elf_Internal_Shdr *hdr; | |
8130 | Elf_External_Verneed *everneed; | |
8131 | Elf_Internal_Verneed *iverneed; | |
8132 | unsigned int i; | |
d0fb9a8d | 8133 | bfd_byte *contents_end; |
fc0e6df6 PB |
8134 | |
8135 | hdr = &elf_tdata (abfd)->dynverref_hdr; | |
8136 | ||
601a03ba | 8137 | if (hdr->sh_info == 0 || hdr->sh_size < sizeof (Elf_External_Verneed)) |
d0fb9a8d | 8138 | { |
601a03ba | 8139 | error_return_bad_verref: |
4eca0228 | 8140 | _bfd_error_handler |
601a03ba AM |
8141 | (_("%B: .gnu.version_r invalid entry"), abfd); |
8142 | bfd_set_error (bfd_error_bad_value); | |
d0fb9a8d JJ |
8143 | error_return_verref: |
8144 | elf_tdata (abfd)->verref = NULL; | |
8145 | elf_tdata (abfd)->cverrefs = 0; | |
8146 | goto error_return; | |
8147 | } | |
601a03ba AM |
8148 | |
8149 | contents = (bfd_byte *) bfd_malloc (hdr->sh_size); | |
8150 | if (contents == NULL) | |
8151 | goto error_return_verref; | |
8152 | ||
fc0e6df6 PB |
8153 | if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0 |
8154 | || bfd_bread (contents, hdr->sh_size, abfd) != hdr->sh_size) | |
d0fb9a8d | 8155 | goto error_return_verref; |
fc0e6df6 | 8156 | |
601a03ba AM |
8157 | elf_tdata (abfd)->verref = (Elf_Internal_Verneed *) |
8158 | bfd_zalloc2 (abfd, hdr->sh_info, sizeof (Elf_Internal_Verneed)); | |
8159 | ||
8160 | if (elf_tdata (abfd)->verref == NULL) | |
d0fb9a8d JJ |
8161 | goto error_return_verref; |
8162 | ||
8163 | BFD_ASSERT (sizeof (Elf_External_Verneed) | |
8164 | == sizeof (Elf_External_Vernaux)); | |
8165 | contents_end = contents + hdr->sh_size - sizeof (Elf_External_Verneed); | |
fc0e6df6 PB |
8166 | everneed = (Elf_External_Verneed *) contents; |
8167 | iverneed = elf_tdata (abfd)->verref; | |
8168 | for (i = 0; i < hdr->sh_info; i++, iverneed++) | |
8169 | { | |
8170 | Elf_External_Vernaux *evernaux; | |
8171 | Elf_Internal_Vernaux *ivernaux; | |
8172 | unsigned int j; | |
8173 | ||
8174 | _bfd_elf_swap_verneed_in (abfd, everneed, iverneed); | |
8175 | ||
8176 | iverneed->vn_bfd = abfd; | |
8177 | ||
8178 | iverneed->vn_filename = | |
8179 | bfd_elf_string_from_elf_section (abfd, hdr->sh_link, | |
8180 | iverneed->vn_file); | |
8181 | if (iverneed->vn_filename == NULL) | |
601a03ba | 8182 | goto error_return_bad_verref; |
fc0e6df6 | 8183 | |
d0fb9a8d JJ |
8184 | if (iverneed->vn_cnt == 0) |
8185 | iverneed->vn_auxptr = NULL; | |
8186 | else | |
8187 | { | |
a50b1753 NC |
8188 | iverneed->vn_auxptr = (struct elf_internal_vernaux *) |
8189 | bfd_alloc2 (abfd, iverneed->vn_cnt, | |
8190 | sizeof (Elf_Internal_Vernaux)); | |
d0fb9a8d JJ |
8191 | if (iverneed->vn_auxptr == NULL) |
8192 | goto error_return_verref; | |
8193 | } | |
8194 | ||
8195 | if (iverneed->vn_aux | |
8196 | > (size_t) (contents_end - (bfd_byte *) everneed)) | |
601a03ba | 8197 | goto error_return_bad_verref; |
fc0e6df6 PB |
8198 | |
8199 | evernaux = ((Elf_External_Vernaux *) | |
8200 | ((bfd_byte *) everneed + iverneed->vn_aux)); | |
8201 | ivernaux = iverneed->vn_auxptr; | |
8202 | for (j = 0; j < iverneed->vn_cnt; j++, ivernaux++) | |
8203 | { | |
8204 | _bfd_elf_swap_vernaux_in (abfd, evernaux, ivernaux); | |
8205 | ||
8206 | ivernaux->vna_nodename = | |
8207 | bfd_elf_string_from_elf_section (abfd, hdr->sh_link, | |
8208 | ivernaux->vna_name); | |
8209 | if (ivernaux->vna_nodename == NULL) | |
601a03ba | 8210 | goto error_return_bad_verref; |
fc0e6df6 | 8211 | |
25ff461f AM |
8212 | if (ivernaux->vna_other > freeidx) |
8213 | freeidx = ivernaux->vna_other; | |
8214 | ||
8215 | ivernaux->vna_nextptr = NULL; | |
8216 | if (ivernaux->vna_next == 0) | |
8217 | { | |
8218 | iverneed->vn_cnt = j + 1; | |
8219 | break; | |
8220 | } | |
fc0e6df6 PB |
8221 | if (j + 1 < iverneed->vn_cnt) |
8222 | ivernaux->vna_nextptr = ivernaux + 1; | |
fc0e6df6 | 8223 | |
d0fb9a8d JJ |
8224 | if (ivernaux->vna_next |
8225 | > (size_t) (contents_end - (bfd_byte *) evernaux)) | |
601a03ba | 8226 | goto error_return_bad_verref; |
d0fb9a8d | 8227 | |
fc0e6df6 PB |
8228 | evernaux = ((Elf_External_Vernaux *) |
8229 | ((bfd_byte *) evernaux + ivernaux->vna_next)); | |
fc0e6df6 PB |
8230 | } |
8231 | ||
25ff461f AM |
8232 | iverneed->vn_nextref = NULL; |
8233 | if (iverneed->vn_next == 0) | |
8234 | break; | |
fc0e6df6 PB |
8235 | if (i + 1 < hdr->sh_info) |
8236 | iverneed->vn_nextref = iverneed + 1; | |
fc0e6df6 | 8237 | |
d0fb9a8d JJ |
8238 | if (iverneed->vn_next |
8239 | > (size_t) (contents_end - (bfd_byte *) everneed)) | |
601a03ba | 8240 | goto error_return_bad_verref; |
d0fb9a8d | 8241 | |
fc0e6df6 PB |
8242 | everneed = ((Elf_External_Verneed *) |
8243 | ((bfd_byte *) everneed + iverneed->vn_next)); | |
8244 | } | |
25ff461f | 8245 | elf_tdata (abfd)->cverrefs = i; |
fc0e6df6 PB |
8246 | |
8247 | free (contents); | |
8248 | contents = NULL; | |
8249 | } | |
252b5132 RH |
8250 | |
8251 | if (elf_dynverdef (abfd) != 0) | |
8252 | { | |
8253 | Elf_Internal_Shdr *hdr; | |
8254 | Elf_External_Verdef *everdef; | |
8255 | Elf_Internal_Verdef *iverdef; | |
f631889e UD |
8256 | Elf_Internal_Verdef *iverdefarr; |
8257 | Elf_Internal_Verdef iverdefmem; | |
252b5132 | 8258 | unsigned int i; |
062e2358 | 8259 | unsigned int maxidx; |
d0fb9a8d | 8260 | bfd_byte *contents_end_def, *contents_end_aux; |
252b5132 RH |
8261 | |
8262 | hdr = &elf_tdata (abfd)->dynverdef_hdr; | |
8263 | ||
601a03ba AM |
8264 | if (hdr->sh_info == 0 || hdr->sh_size < sizeof (Elf_External_Verdef)) |
8265 | { | |
8266 | error_return_bad_verdef: | |
4eca0228 | 8267 | _bfd_error_handler |
601a03ba AM |
8268 | (_("%B: .gnu.version_d invalid entry"), abfd); |
8269 | bfd_set_error (bfd_error_bad_value); | |
8270 | error_return_verdef: | |
8271 | elf_tdata (abfd)->verdef = NULL; | |
8272 | elf_tdata (abfd)->cverdefs = 0; | |
8273 | goto error_return; | |
8274 | } | |
8275 | ||
a50b1753 | 8276 | contents = (bfd_byte *) bfd_malloc (hdr->sh_size); |
252b5132 | 8277 | if (contents == NULL) |
601a03ba | 8278 | goto error_return_verdef; |
252b5132 | 8279 | if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0 |
217aa764 | 8280 | || bfd_bread (contents, hdr->sh_size, abfd) != hdr->sh_size) |
601a03ba | 8281 | goto error_return_verdef; |
d0fb9a8d JJ |
8282 | |
8283 | BFD_ASSERT (sizeof (Elf_External_Verdef) | |
8284 | >= sizeof (Elf_External_Verdaux)); | |
8285 | contents_end_def = contents + hdr->sh_size | |
8286 | - sizeof (Elf_External_Verdef); | |
8287 | contents_end_aux = contents + hdr->sh_size | |
8288 | - sizeof (Elf_External_Verdaux); | |
8289 | ||
f631889e UD |
8290 | /* We know the number of entries in the section but not the maximum |
8291 | index. Therefore we have to run through all entries and find | |
8292 | the maximum. */ | |
252b5132 | 8293 | everdef = (Elf_External_Verdef *) contents; |
f631889e UD |
8294 | maxidx = 0; |
8295 | for (i = 0; i < hdr->sh_info; ++i) | |
8296 | { | |
8297 | _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem); | |
8298 | ||
601a03ba AM |
8299 | if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) == 0) |
8300 | goto error_return_bad_verdef; | |
062e2358 AM |
8301 | if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) > maxidx) |
8302 | maxidx = iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION); | |
f631889e | 8303 | |
25ff461f AM |
8304 | if (iverdefmem.vd_next == 0) |
8305 | break; | |
8306 | ||
d0fb9a8d JJ |
8307 | if (iverdefmem.vd_next |
8308 | > (size_t) (contents_end_def - (bfd_byte *) everdef)) | |
601a03ba | 8309 | goto error_return_bad_verdef; |
d0fb9a8d | 8310 | |
f631889e UD |
8311 | everdef = ((Elf_External_Verdef *) |
8312 | ((bfd_byte *) everdef + iverdefmem.vd_next)); | |
8313 | } | |
8314 | ||
fc0e6df6 PB |
8315 | if (default_imported_symver) |
8316 | { | |
8317 | if (freeidx > maxidx) | |
8318 | maxidx = ++freeidx; | |
8319 | else | |
8320 | freeidx = ++maxidx; | |
8321 | } | |
201159ec | 8322 | |
601a03ba AM |
8323 | elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *) |
8324 | bfd_zalloc2 (abfd, maxidx, sizeof (Elf_Internal_Verdef)); | |
f631889e | 8325 | if (elf_tdata (abfd)->verdef == NULL) |
601a03ba | 8326 | goto error_return_verdef; |
f631889e UD |
8327 | |
8328 | elf_tdata (abfd)->cverdefs = maxidx; | |
8329 | ||
8330 | everdef = (Elf_External_Verdef *) contents; | |
8331 | iverdefarr = elf_tdata (abfd)->verdef; | |
8332 | for (i = 0; i < hdr->sh_info; i++) | |
252b5132 RH |
8333 | { |
8334 | Elf_External_Verdaux *everdaux; | |
8335 | Elf_Internal_Verdaux *iverdaux; | |
8336 | unsigned int j; | |
8337 | ||
f631889e UD |
8338 | _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem); |
8339 | ||
d0fb9a8d | 8340 | if ((iverdefmem.vd_ndx & VERSYM_VERSION) == 0) |
601a03ba | 8341 | goto error_return_bad_verdef; |
d0fb9a8d | 8342 | |
f631889e | 8343 | iverdef = &iverdefarr[(iverdefmem.vd_ndx & VERSYM_VERSION) - 1]; |
595bce75 | 8344 | memcpy (iverdef, &iverdefmem, offsetof (Elf_Internal_Verdef, vd_bfd)); |
252b5132 RH |
8345 | |
8346 | iverdef->vd_bfd = abfd; | |
8347 | ||
d0fb9a8d JJ |
8348 | if (iverdef->vd_cnt == 0) |
8349 | iverdef->vd_auxptr = NULL; | |
8350 | else | |
8351 | { | |
a50b1753 NC |
8352 | iverdef->vd_auxptr = (struct elf_internal_verdaux *) |
8353 | bfd_alloc2 (abfd, iverdef->vd_cnt, | |
8354 | sizeof (Elf_Internal_Verdaux)); | |
d0fb9a8d JJ |
8355 | if (iverdef->vd_auxptr == NULL) |
8356 | goto error_return_verdef; | |
8357 | } | |
8358 | ||
8359 | if (iverdef->vd_aux | |
8360 | > (size_t) (contents_end_aux - (bfd_byte *) everdef)) | |
601a03ba | 8361 | goto error_return_bad_verdef; |
252b5132 RH |
8362 | |
8363 | everdaux = ((Elf_External_Verdaux *) | |
8364 | ((bfd_byte *) everdef + iverdef->vd_aux)); | |
8365 | iverdaux = iverdef->vd_auxptr; | |
8366 | for (j = 0; j < iverdef->vd_cnt; j++, iverdaux++) | |
8367 | { | |
8368 | _bfd_elf_swap_verdaux_in (abfd, everdaux, iverdaux); | |
8369 | ||
8370 | iverdaux->vda_nodename = | |
8371 | bfd_elf_string_from_elf_section (abfd, hdr->sh_link, | |
8372 | iverdaux->vda_name); | |
8373 | if (iverdaux->vda_nodename == NULL) | |
601a03ba | 8374 | goto error_return_bad_verdef; |
252b5132 | 8375 | |
25ff461f AM |
8376 | iverdaux->vda_nextptr = NULL; |
8377 | if (iverdaux->vda_next == 0) | |
8378 | { | |
8379 | iverdef->vd_cnt = j + 1; | |
8380 | break; | |
8381 | } | |
252b5132 RH |
8382 | if (j + 1 < iverdef->vd_cnt) |
8383 | iverdaux->vda_nextptr = iverdaux + 1; | |
252b5132 | 8384 | |
d0fb9a8d JJ |
8385 | if (iverdaux->vda_next |
8386 | > (size_t) (contents_end_aux - (bfd_byte *) everdaux)) | |
601a03ba | 8387 | goto error_return_bad_verdef; |
d0fb9a8d | 8388 | |
252b5132 RH |
8389 | everdaux = ((Elf_External_Verdaux *) |
8390 | ((bfd_byte *) everdaux + iverdaux->vda_next)); | |
8391 | } | |
8392 | ||
595bce75 | 8393 | iverdef->vd_nodename = NULL; |
d0fb9a8d JJ |
8394 | if (iverdef->vd_cnt) |
8395 | iverdef->vd_nodename = iverdef->vd_auxptr->vda_nodename; | |
252b5132 | 8396 | |
25ff461f AM |
8397 | iverdef->vd_nextdef = NULL; |
8398 | if (iverdef->vd_next == 0) | |
8399 | break; | |
d0fb9a8d | 8400 | if ((size_t) (iverdef - iverdefarr) + 1 < maxidx) |
252b5132 | 8401 | iverdef->vd_nextdef = iverdef + 1; |
252b5132 RH |
8402 | |
8403 | everdef = ((Elf_External_Verdef *) | |
8404 | ((bfd_byte *) everdef + iverdef->vd_next)); | |
8405 | } | |
8406 | ||
8407 | free (contents); | |
8408 | contents = NULL; | |
8409 | } | |
fc0e6df6 | 8410 | else if (default_imported_symver) |
252b5132 | 8411 | { |
fc0e6df6 PB |
8412 | if (freeidx < 3) |
8413 | freeidx = 3; | |
8414 | else | |
8415 | freeidx++; | |
252b5132 | 8416 | |
a50b1753 NC |
8417 | elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *) |
8418 | bfd_zalloc2 (abfd, freeidx, sizeof (Elf_Internal_Verdef)); | |
fc0e6df6 | 8419 | if (elf_tdata (abfd)->verdef == NULL) |
252b5132 RH |
8420 | goto error_return; |
8421 | ||
fc0e6df6 PB |
8422 | elf_tdata (abfd)->cverdefs = freeidx; |
8423 | } | |
252b5132 | 8424 | |
fc0e6df6 PB |
8425 | /* Create a default version based on the soname. */ |
8426 | if (default_imported_symver) | |
8427 | { | |
8428 | Elf_Internal_Verdef *iverdef; | |
8429 | Elf_Internal_Verdaux *iverdaux; | |
252b5132 | 8430 | |
5bb3703f | 8431 | iverdef = &elf_tdata (abfd)->verdef[freeidx - 1]; |
252b5132 | 8432 | |
fc0e6df6 PB |
8433 | iverdef->vd_version = VER_DEF_CURRENT; |
8434 | iverdef->vd_flags = 0; | |
8435 | iverdef->vd_ndx = freeidx; | |
8436 | iverdef->vd_cnt = 1; | |
252b5132 | 8437 | |
fc0e6df6 | 8438 | iverdef->vd_bfd = abfd; |
252b5132 | 8439 | |
fc0e6df6 PB |
8440 | iverdef->vd_nodename = bfd_elf_get_dt_soname (abfd); |
8441 | if (iverdef->vd_nodename == NULL) | |
d0fb9a8d | 8442 | goto error_return_verdef; |
fc0e6df6 | 8443 | iverdef->vd_nextdef = NULL; |
601a03ba AM |
8444 | iverdef->vd_auxptr = ((struct elf_internal_verdaux *) |
8445 | bfd_zalloc (abfd, sizeof (Elf_Internal_Verdaux))); | |
d0fb9a8d JJ |
8446 | if (iverdef->vd_auxptr == NULL) |
8447 | goto error_return_verdef; | |
252b5132 | 8448 | |
fc0e6df6 PB |
8449 | iverdaux = iverdef->vd_auxptr; |
8450 | iverdaux->vda_nodename = iverdef->vd_nodename; | |
252b5132 RH |
8451 | } |
8452 | ||
b34976b6 | 8453 | return TRUE; |
252b5132 RH |
8454 | |
8455 | error_return: | |
5ed6aba4 | 8456 | if (contents != NULL) |
252b5132 | 8457 | free (contents); |
b34976b6 | 8458 | return FALSE; |
252b5132 RH |
8459 | } |
8460 | \f | |
8461 | asymbol * | |
217aa764 | 8462 | _bfd_elf_make_empty_symbol (bfd *abfd) |
252b5132 RH |
8463 | { |
8464 | elf_symbol_type *newsym; | |
8465 | ||
201159ec | 8466 | newsym = (elf_symbol_type *) bfd_zalloc (abfd, sizeof * newsym); |
252b5132 RH |
8467 | if (!newsym) |
8468 | return NULL; | |
201159ec NC |
8469 | newsym->symbol.the_bfd = abfd; |
8470 | return &newsym->symbol; | |
252b5132 RH |
8471 | } |
8472 | ||
8473 | void | |
217aa764 AM |
8474 | _bfd_elf_get_symbol_info (bfd *abfd ATTRIBUTE_UNUSED, |
8475 | asymbol *symbol, | |
8476 | symbol_info *ret) | |
252b5132 RH |
8477 | { |
8478 | bfd_symbol_info (symbol, ret); | |
8479 | } | |
8480 | ||
8481 | /* Return whether a symbol name implies a local symbol. Most targets | |
8482 | use this function for the is_local_label_name entry point, but some | |
8483 | override it. */ | |
8484 | ||
b34976b6 | 8485 | bfd_boolean |
217aa764 AM |
8486 | _bfd_elf_is_local_label_name (bfd *abfd ATTRIBUTE_UNUSED, |
8487 | const char *name) | |
252b5132 RH |
8488 | { |
8489 | /* Normal local symbols start with ``.L''. */ | |
8490 | if (name[0] == '.' && name[1] == 'L') | |
b34976b6 | 8491 | return TRUE; |
252b5132 RH |
8492 | |
8493 | /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate | |
8494 | DWARF debugging symbols starting with ``..''. */ | |
8495 | if (name[0] == '.' && name[1] == '.') | |
b34976b6 | 8496 | return TRUE; |
252b5132 RH |
8497 | |
8498 | /* gcc will sometimes generate symbols beginning with ``_.L_'' when | |
8499 | emitting DWARF debugging output. I suspect this is actually a | |
8500 | small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call | |
8501 | ASM_GENERATE_INTERNAL_LABEL, and this causes the leading | |
8502 | underscore to be emitted on some ELF targets). For ease of use, | |
8503 | we treat such symbols as local. */ | |
8504 | if (name[0] == '_' && name[1] == '.' && name[2] == 'L' && name[3] == '_') | |
b34976b6 | 8505 | return TRUE; |
252b5132 | 8506 | |
b1fa9dd6 NC |
8507 | /* Treat assembler generated fake symbols, dollar local labels and |
8508 | forward-backward labels (aka local labels) as locals. | |
8509 | These labels have the form: | |
8510 | ||
8511 | L0^A.* (fake symbols) | |
8512 | ||
8513 | [.]?L[0123456789]+{^A|^B}[0123456789]* (local labels) | |
8514 | ||
8515 | Versions which start with .L will have already been matched above, | |
8516 | so we only need to match the rest. */ | |
8517 | if (name[0] == 'L' && ISDIGIT (name[1])) | |
8518 | { | |
8519 | bfd_boolean ret = FALSE; | |
8520 | const char * p; | |
8521 | char c; | |
8522 | ||
8523 | for (p = name + 2; (c = *p); p++) | |
8524 | { | |
8525 | if (c == 1 || c == 2) | |
8526 | { | |
8527 | if (c == 1 && p == name + 2) | |
8528 | /* A fake symbol. */ | |
8529 | return TRUE; | |
8530 | ||
8531 | /* FIXME: We are being paranoid here and treating symbols like | |
8532 | L0^Bfoo as if there were non-local, on the grounds that the | |
8533 | assembler will never generate them. But can any symbol | |
8534 | containing an ASCII value in the range 1-31 ever be anything | |
8535 | other than some kind of local ? */ | |
8536 | ret = TRUE; | |
8537 | } | |
8538 | ||
8539 | if (! ISDIGIT (c)) | |
8540 | { | |
8541 | ret = FALSE; | |
8542 | break; | |
8543 | } | |
8544 | } | |
8545 | return ret; | |
8546 | } | |
ffa54770 | 8547 | |
b34976b6 | 8548 | return FALSE; |
252b5132 RH |
8549 | } |
8550 | ||
8551 | alent * | |
217aa764 AM |
8552 | _bfd_elf_get_lineno (bfd *abfd ATTRIBUTE_UNUSED, |
8553 | asymbol *symbol ATTRIBUTE_UNUSED) | |
252b5132 RH |
8554 | { |
8555 | abort (); | |
8556 | return NULL; | |
8557 | } | |
8558 | ||
b34976b6 | 8559 | bfd_boolean |
217aa764 AM |
8560 | _bfd_elf_set_arch_mach (bfd *abfd, |
8561 | enum bfd_architecture arch, | |
8562 | unsigned long machine) | |
252b5132 RH |
8563 | { |
8564 | /* If this isn't the right architecture for this backend, and this | |
8565 | isn't the generic backend, fail. */ | |
8566 | if (arch != get_elf_backend_data (abfd)->arch | |
8567 | && arch != bfd_arch_unknown | |
8568 | && get_elf_backend_data (abfd)->arch != bfd_arch_unknown) | |
b34976b6 | 8569 | return FALSE; |
252b5132 RH |
8570 | |
8571 | return bfd_default_set_arch_mach (abfd, arch, machine); | |
8572 | } | |
8573 | ||
d1fad7c6 NC |
8574 | /* Find the nearest line to a particular section and offset, |
8575 | for error reporting. */ | |
8576 | ||
b34976b6 | 8577 | bfd_boolean |
217aa764 | 8578 | _bfd_elf_find_nearest_line (bfd *abfd, |
217aa764 | 8579 | asymbol **symbols, |
fb167eb2 | 8580 | asection *section, |
217aa764 AM |
8581 | bfd_vma offset, |
8582 | const char **filename_ptr, | |
8583 | const char **functionname_ptr, | |
fb167eb2 AM |
8584 | unsigned int *line_ptr, |
8585 | unsigned int *discriminator_ptr) | |
d1fad7c6 | 8586 | { |
b34976b6 | 8587 | bfd_boolean found; |
d1fad7c6 | 8588 | |
fb167eb2 | 8589 | if (_bfd_dwarf2_find_nearest_line (abfd, symbols, NULL, section, offset, |
4e8a9624 | 8590 | filename_ptr, functionname_ptr, |
fb167eb2 AM |
8591 | line_ptr, discriminator_ptr, |
8592 | dwarf_debug_sections, 0, | |
e00e8198 AM |
8593 | &elf_tdata (abfd)->dwarf2_find_line_info) |
8594 | || _bfd_dwarf1_find_nearest_line (abfd, symbols, section, offset, | |
8595 | filename_ptr, functionname_ptr, | |
8596 | line_ptr)) | |
d1fad7c6 NC |
8597 | { |
8598 | if (!*functionname_ptr) | |
e00e8198 AM |
8599 | _bfd_elf_find_function (abfd, symbols, section, offset, |
8600 | *filename_ptr ? NULL : filename_ptr, | |
8601 | functionname_ptr); | |
b34976b6 | 8602 | return TRUE; |
d1fad7c6 NC |
8603 | } |
8604 | ||
8605 | if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset, | |
4e8a9624 AM |
8606 | &found, filename_ptr, |
8607 | functionname_ptr, line_ptr, | |
8608 | &elf_tdata (abfd)->line_info)) | |
b34976b6 | 8609 | return FALSE; |
dc43ada5 | 8610 | if (found && (*functionname_ptr || *line_ptr)) |
b34976b6 | 8611 | return TRUE; |
d1fad7c6 NC |
8612 | |
8613 | if (symbols == NULL) | |
b34976b6 | 8614 | return FALSE; |
d1fad7c6 | 8615 | |
e00e8198 AM |
8616 | if (! _bfd_elf_find_function (abfd, symbols, section, offset, |
8617 | filename_ptr, functionname_ptr)) | |
b34976b6 | 8618 | return FALSE; |
d1fad7c6 | 8619 | |
252b5132 | 8620 | *line_ptr = 0; |
b34976b6 | 8621 | return TRUE; |
252b5132 RH |
8622 | } |
8623 | ||
5420f73d L |
8624 | /* Find the line for a symbol. */ |
8625 | ||
8626 | bfd_boolean | |
8627 | _bfd_elf_find_line (bfd *abfd, asymbol **symbols, asymbol *symbol, | |
8628 | const char **filename_ptr, unsigned int *line_ptr) | |
9b8d1a36 | 8629 | { |
fb167eb2 AM |
8630 | return _bfd_dwarf2_find_nearest_line (abfd, symbols, symbol, NULL, 0, |
8631 | filename_ptr, NULL, line_ptr, NULL, | |
8632 | dwarf_debug_sections, 0, | |
8633 | &elf_tdata (abfd)->dwarf2_find_line_info); | |
5420f73d L |
8634 | } |
8635 | ||
4ab527b0 FF |
8636 | /* After a call to bfd_find_nearest_line, successive calls to |
8637 | bfd_find_inliner_info can be used to get source information about | |
8638 | each level of function inlining that terminated at the address | |
8639 | passed to bfd_find_nearest_line. Currently this is only supported | |
8640 | for DWARF2 with appropriate DWARF3 extensions. */ | |
8641 | ||
8642 | bfd_boolean | |
8643 | _bfd_elf_find_inliner_info (bfd *abfd, | |
8644 | const char **filename_ptr, | |
8645 | const char **functionname_ptr, | |
8646 | unsigned int *line_ptr) | |
8647 | { | |
8648 | bfd_boolean found; | |
8649 | found = _bfd_dwarf2_find_inliner_info (abfd, filename_ptr, | |
8650 | functionname_ptr, line_ptr, | |
8651 | & elf_tdata (abfd)->dwarf2_find_line_info); | |
8652 | return found; | |
8653 | } | |
8654 | ||
252b5132 | 8655 | int |
a6b96beb | 8656 | _bfd_elf_sizeof_headers (bfd *abfd, struct bfd_link_info *info) |
252b5132 | 8657 | { |
8ded5a0f AM |
8658 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
8659 | int ret = bed->s->sizeof_ehdr; | |
252b5132 | 8660 | |
0e1862bb | 8661 | if (!bfd_link_relocatable (info)) |
8ded5a0f | 8662 | { |
12bd6957 | 8663 | bfd_size_type phdr_size = elf_program_header_size (abfd); |
8ded5a0f | 8664 | |
62d7a5f6 AM |
8665 | if (phdr_size == (bfd_size_type) -1) |
8666 | { | |
8667 | struct elf_segment_map *m; | |
8668 | ||
8669 | phdr_size = 0; | |
12bd6957 | 8670 | for (m = elf_seg_map (abfd); m != NULL; m = m->next) |
62d7a5f6 | 8671 | phdr_size += bed->s->sizeof_phdr; |
8ded5a0f | 8672 | |
62d7a5f6 AM |
8673 | if (phdr_size == 0) |
8674 | phdr_size = get_program_header_size (abfd, info); | |
8675 | } | |
8ded5a0f | 8676 | |
12bd6957 | 8677 | elf_program_header_size (abfd) = phdr_size; |
8ded5a0f AM |
8678 | ret += phdr_size; |
8679 | } | |
8680 | ||
252b5132 RH |
8681 | return ret; |
8682 | } | |
8683 | ||
b34976b6 | 8684 | bfd_boolean |
217aa764 AM |
8685 | _bfd_elf_set_section_contents (bfd *abfd, |
8686 | sec_ptr section, | |
0f867abe | 8687 | const void *location, |
217aa764 AM |
8688 | file_ptr offset, |
8689 | bfd_size_type count) | |
252b5132 RH |
8690 | { |
8691 | Elf_Internal_Shdr *hdr; | |
1b6aeedb | 8692 | file_ptr pos; |
252b5132 RH |
8693 | |
8694 | if (! abfd->output_has_begun | |
217aa764 | 8695 | && ! _bfd_elf_compute_section_file_positions (abfd, NULL)) |
b34976b6 | 8696 | return FALSE; |
252b5132 | 8697 | |
0ce398f1 L |
8698 | if (!count) |
8699 | return TRUE; | |
8700 | ||
252b5132 | 8701 | hdr = &elf_section_data (section)->this_hdr; |
0ce398f1 L |
8702 | if (hdr->sh_offset == (file_ptr) -1) |
8703 | { | |
8704 | /* We must compress this section. Write output to the buffer. */ | |
8705 | unsigned char *contents = hdr->contents; | |
8706 | if ((offset + count) > hdr->sh_size | |
8707 | || (section->flags & SEC_ELF_COMPRESS) == 0 | |
8708 | || contents == NULL) | |
8709 | abort (); | |
8710 | memcpy (contents + offset, location, count); | |
8711 | return TRUE; | |
8712 | } | |
dc810e39 AM |
8713 | pos = hdr->sh_offset + offset; |
8714 | if (bfd_seek (abfd, pos, SEEK_SET) != 0 | |
8715 | || bfd_bwrite (location, count, abfd) != count) | |
b34976b6 | 8716 | return FALSE; |
252b5132 | 8717 | |
b34976b6 | 8718 | return TRUE; |
252b5132 RH |
8719 | } |
8720 | ||
8721 | void | |
217aa764 AM |
8722 | _bfd_elf_no_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED, |
8723 | arelent *cache_ptr ATTRIBUTE_UNUSED, | |
8724 | Elf_Internal_Rela *dst ATTRIBUTE_UNUSED) | |
252b5132 RH |
8725 | { |
8726 | abort (); | |
8727 | } | |
8728 | ||
252b5132 RH |
8729 | /* Try to convert a non-ELF reloc into an ELF one. */ |
8730 | ||
b34976b6 | 8731 | bfd_boolean |
217aa764 | 8732 | _bfd_elf_validate_reloc (bfd *abfd, arelent *areloc) |
252b5132 | 8733 | { |
c044fabd | 8734 | /* Check whether we really have an ELF howto. */ |
252b5132 RH |
8735 | |
8736 | if ((*areloc->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec) | |
8737 | { | |
8738 | bfd_reloc_code_real_type code; | |
8739 | reloc_howto_type *howto; | |
8740 | ||
8741 | /* Alien reloc: Try to determine its type to replace it with an | |
c044fabd | 8742 | equivalent ELF reloc. */ |
252b5132 RH |
8743 | |
8744 | if (areloc->howto->pc_relative) | |
8745 | { | |
8746 | switch (areloc->howto->bitsize) | |
8747 | { | |
8748 | case 8: | |
8749 | code = BFD_RELOC_8_PCREL; | |
8750 | break; | |
8751 | case 12: | |
8752 | code = BFD_RELOC_12_PCREL; | |
8753 | break; | |
8754 | case 16: | |
8755 | code = BFD_RELOC_16_PCREL; | |
8756 | break; | |
8757 | case 24: | |
8758 | code = BFD_RELOC_24_PCREL; | |
8759 | break; | |
8760 | case 32: | |
8761 | code = BFD_RELOC_32_PCREL; | |
8762 | break; | |
8763 | case 64: | |
8764 | code = BFD_RELOC_64_PCREL; | |
8765 | break; | |
8766 | default: | |
8767 | goto fail; | |
8768 | } | |
8769 | ||
8770 | howto = bfd_reloc_type_lookup (abfd, code); | |
8771 | ||
8772 | if (areloc->howto->pcrel_offset != howto->pcrel_offset) | |
8773 | { | |
8774 | if (howto->pcrel_offset) | |
8775 | areloc->addend += areloc->address; | |
8776 | else | |
8777 | areloc->addend -= areloc->address; /* addend is unsigned!! */ | |
8778 | } | |
8779 | } | |
8780 | else | |
8781 | { | |
8782 | switch (areloc->howto->bitsize) | |
8783 | { | |
8784 | case 8: | |
8785 | code = BFD_RELOC_8; | |
8786 | break; | |
8787 | case 14: | |
8788 | code = BFD_RELOC_14; | |
8789 | break; | |
8790 | case 16: | |
8791 | code = BFD_RELOC_16; | |
8792 | break; | |
8793 | case 26: | |
8794 | code = BFD_RELOC_26; | |
8795 | break; | |
8796 | case 32: | |
8797 | code = BFD_RELOC_32; | |
8798 | break; | |
8799 | case 64: | |
8800 | code = BFD_RELOC_64; | |
8801 | break; | |
8802 | default: | |
8803 | goto fail; | |
8804 | } | |
8805 | ||
8806 | howto = bfd_reloc_type_lookup (abfd, code); | |
8807 | } | |
8808 | ||
8809 | if (howto) | |
8810 | areloc->howto = howto; | |
8811 | else | |
8812 | goto fail; | |
8813 | } | |
8814 | ||
b34976b6 | 8815 | return TRUE; |
252b5132 RH |
8816 | |
8817 | fail: | |
4eca0228 | 8818 | _bfd_error_handler |
695344c0 | 8819 | /* xgettext:c-format */ |
d003868e AM |
8820 | (_("%B: unsupported relocation type %s"), |
8821 | abfd, areloc->howto->name); | |
252b5132 | 8822 | bfd_set_error (bfd_error_bad_value); |
b34976b6 | 8823 | return FALSE; |
252b5132 RH |
8824 | } |
8825 | ||
b34976b6 | 8826 | bfd_boolean |
217aa764 | 8827 | _bfd_elf_close_and_cleanup (bfd *abfd) |
252b5132 | 8828 | { |
d9071b0c TG |
8829 | struct elf_obj_tdata *tdata = elf_tdata (abfd); |
8830 | if (bfd_get_format (abfd) == bfd_object && tdata != NULL) | |
252b5132 | 8831 | { |
c0355132 | 8832 | if (elf_tdata (abfd)->o != NULL && elf_shstrtab (abfd) != NULL) |
2b0f7ef9 | 8833 | _bfd_elf_strtab_free (elf_shstrtab (abfd)); |
d9071b0c | 8834 | _bfd_dwarf2_cleanup_debug_info (abfd, &tdata->dwarf2_find_line_info); |
252b5132 RH |
8835 | } |
8836 | ||
8837 | return _bfd_generic_close_and_cleanup (abfd); | |
8838 | } | |
8839 | ||
8840 | /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY | |
8841 | in the relocation's offset. Thus we cannot allow any sort of sanity | |
8842 | range-checking to interfere. There is nothing else to do in processing | |
8843 | this reloc. */ | |
8844 | ||
8845 | bfd_reloc_status_type | |
217aa764 AM |
8846 | _bfd_elf_rel_vtable_reloc_fn |
8847 | (bfd *abfd ATTRIBUTE_UNUSED, arelent *re ATTRIBUTE_UNUSED, | |
fc0a2244 | 8848 | struct bfd_symbol *symbol ATTRIBUTE_UNUSED, |
217aa764 AM |
8849 | void *data ATTRIBUTE_UNUSED, asection *is ATTRIBUTE_UNUSED, |
8850 | bfd *obfd ATTRIBUTE_UNUSED, char **errmsg ATTRIBUTE_UNUSED) | |
252b5132 RH |
8851 | { |
8852 | return bfd_reloc_ok; | |
8853 | } | |
252b5132 RH |
8854 | \f |
8855 | /* Elf core file support. Much of this only works on native | |
8856 | toolchains, since we rely on knowing the | |
8857 | machine-dependent procfs structure in order to pick | |
c044fabd | 8858 | out details about the corefile. */ |
252b5132 RH |
8859 | |
8860 | #ifdef HAVE_SYS_PROCFS_H | |
16231b7b DG |
8861 | /* Needed for new procfs interface on sparc-solaris. */ |
8862 | # define _STRUCTURED_PROC 1 | |
252b5132 RH |
8863 | # include <sys/procfs.h> |
8864 | #endif | |
8865 | ||
261b8d08 PA |
8866 | /* Return a PID that identifies a "thread" for threaded cores, or the |
8867 | PID of the main process for non-threaded cores. */ | |
252b5132 RH |
8868 | |
8869 | static int | |
217aa764 | 8870 | elfcore_make_pid (bfd *abfd) |
252b5132 | 8871 | { |
261b8d08 PA |
8872 | int pid; |
8873 | ||
228e534f | 8874 | pid = elf_tdata (abfd)->core->lwpid; |
261b8d08 | 8875 | if (pid == 0) |
228e534f | 8876 | pid = elf_tdata (abfd)->core->pid; |
261b8d08 PA |
8877 | |
8878 | return pid; | |
252b5132 RH |
8879 | } |
8880 | ||
252b5132 RH |
8881 | /* If there isn't a section called NAME, make one, using |
8882 | data from SECT. Note, this function will generate a | |
8883 | reference to NAME, so you shouldn't deallocate or | |
c044fabd | 8884 | overwrite it. */ |
252b5132 | 8885 | |
b34976b6 | 8886 | static bfd_boolean |
217aa764 | 8887 | elfcore_maybe_make_sect (bfd *abfd, char *name, asection *sect) |
252b5132 | 8888 | { |
c044fabd | 8889 | asection *sect2; |
252b5132 RH |
8890 | |
8891 | if (bfd_get_section_by_name (abfd, name) != NULL) | |
b34976b6 | 8892 | return TRUE; |
252b5132 | 8893 | |
117ed4f8 | 8894 | sect2 = bfd_make_section_with_flags (abfd, name, sect->flags); |
252b5132 | 8895 | if (sect2 == NULL) |
b34976b6 | 8896 | return FALSE; |
252b5132 | 8897 | |
eea6121a | 8898 | sect2->size = sect->size; |
252b5132 | 8899 | sect2->filepos = sect->filepos; |
252b5132 | 8900 | sect2->alignment_power = sect->alignment_power; |
b34976b6 | 8901 | return TRUE; |
252b5132 RH |
8902 | } |
8903 | ||
bb0082d6 AM |
8904 | /* Create a pseudosection containing SIZE bytes at FILEPOS. This |
8905 | actually creates up to two pseudosections: | |
8906 | - For the single-threaded case, a section named NAME, unless | |
8907 | such a section already exists. | |
8908 | - For the multi-threaded case, a section named "NAME/PID", where | |
8909 | PID is elfcore_make_pid (abfd). | |
8910 | Both pseudosections have identical contents. */ | |
b34976b6 | 8911 | bfd_boolean |
217aa764 AM |
8912 | _bfd_elfcore_make_pseudosection (bfd *abfd, |
8913 | char *name, | |
8914 | size_t size, | |
8915 | ufile_ptr filepos) | |
bb0082d6 AM |
8916 | { |
8917 | char buf[100]; | |
8918 | char *threaded_name; | |
d4c88bbb | 8919 | size_t len; |
bb0082d6 AM |
8920 | asection *sect; |
8921 | ||
8922 | /* Build the section name. */ | |
8923 | ||
8924 | sprintf (buf, "%s/%d", name, elfcore_make_pid (abfd)); | |
d4c88bbb | 8925 | len = strlen (buf) + 1; |
a50b1753 | 8926 | threaded_name = (char *) bfd_alloc (abfd, len); |
bb0082d6 | 8927 | if (threaded_name == NULL) |
b34976b6 | 8928 | return FALSE; |
d4c88bbb | 8929 | memcpy (threaded_name, buf, len); |
bb0082d6 | 8930 | |
117ed4f8 AM |
8931 | sect = bfd_make_section_anyway_with_flags (abfd, threaded_name, |
8932 | SEC_HAS_CONTENTS); | |
bb0082d6 | 8933 | if (sect == NULL) |
b34976b6 | 8934 | return FALSE; |
eea6121a | 8935 | sect->size = size; |
bb0082d6 | 8936 | sect->filepos = filepos; |
bb0082d6 AM |
8937 | sect->alignment_power = 2; |
8938 | ||
936e320b | 8939 | return elfcore_maybe_make_sect (abfd, name, sect); |
bb0082d6 AM |
8940 | } |
8941 | ||
252b5132 | 8942 | /* prstatus_t exists on: |
4a938328 | 8943 | solaris 2.5+ |
252b5132 RH |
8944 | linux 2.[01] + glibc |
8945 | unixware 4.2 | |
8946 | */ | |
8947 | ||
8948 | #if defined (HAVE_PRSTATUS_T) | |
a7b97311 | 8949 | |
b34976b6 | 8950 | static bfd_boolean |
217aa764 | 8951 | elfcore_grok_prstatus (bfd *abfd, Elf_Internal_Note *note) |
252b5132 | 8952 | { |
eea6121a | 8953 | size_t size; |
7ee38065 | 8954 | int offset; |
252b5132 | 8955 | |
4a938328 MS |
8956 | if (note->descsz == sizeof (prstatus_t)) |
8957 | { | |
8958 | prstatus_t prstat; | |
252b5132 | 8959 | |
eea6121a | 8960 | size = sizeof (prstat.pr_reg); |
7ee38065 | 8961 | offset = offsetof (prstatus_t, pr_reg); |
4a938328 | 8962 | memcpy (&prstat, note->descdata, sizeof (prstat)); |
252b5132 | 8963 | |
fa49d224 NC |
8964 | /* Do not overwrite the core signal if it |
8965 | has already been set by another thread. */ | |
228e534f AM |
8966 | if (elf_tdata (abfd)->core->signal == 0) |
8967 | elf_tdata (abfd)->core->signal = prstat.pr_cursig; | |
8968 | if (elf_tdata (abfd)->core->pid == 0) | |
8969 | elf_tdata (abfd)->core->pid = prstat.pr_pid; | |
252b5132 | 8970 | |
4a938328 MS |
8971 | /* pr_who exists on: |
8972 | solaris 2.5+ | |
8973 | unixware 4.2 | |
8974 | pr_who doesn't exist on: | |
8975 | linux 2.[01] | |
8976 | */ | |
252b5132 | 8977 | #if defined (HAVE_PRSTATUS_T_PR_WHO) |
228e534f | 8978 | elf_tdata (abfd)->core->lwpid = prstat.pr_who; |
261b8d08 | 8979 | #else |
228e534f | 8980 | elf_tdata (abfd)->core->lwpid = prstat.pr_pid; |
252b5132 | 8981 | #endif |
4a938328 | 8982 | } |
7ee38065 | 8983 | #if defined (HAVE_PRSTATUS32_T) |
4a938328 MS |
8984 | else if (note->descsz == sizeof (prstatus32_t)) |
8985 | { | |
8986 | /* 64-bit host, 32-bit corefile */ | |
8987 | prstatus32_t prstat; | |
8988 | ||
eea6121a | 8989 | size = sizeof (prstat.pr_reg); |
7ee38065 | 8990 | offset = offsetof (prstatus32_t, pr_reg); |
4a938328 MS |
8991 | memcpy (&prstat, note->descdata, sizeof (prstat)); |
8992 | ||
fa49d224 NC |
8993 | /* Do not overwrite the core signal if it |
8994 | has already been set by another thread. */ | |
228e534f AM |
8995 | if (elf_tdata (abfd)->core->signal == 0) |
8996 | elf_tdata (abfd)->core->signal = prstat.pr_cursig; | |
8997 | if (elf_tdata (abfd)->core->pid == 0) | |
8998 | elf_tdata (abfd)->core->pid = prstat.pr_pid; | |
4a938328 MS |
8999 | |
9000 | /* pr_who exists on: | |
9001 | solaris 2.5+ | |
9002 | unixware 4.2 | |
9003 | pr_who doesn't exist on: | |
9004 | linux 2.[01] | |
9005 | */ | |
7ee38065 | 9006 | #if defined (HAVE_PRSTATUS32_T_PR_WHO) |
228e534f | 9007 | elf_tdata (abfd)->core->lwpid = prstat.pr_who; |
261b8d08 | 9008 | #else |
228e534f | 9009 | elf_tdata (abfd)->core->lwpid = prstat.pr_pid; |
4a938328 MS |
9010 | #endif |
9011 | } | |
7ee38065 | 9012 | #endif /* HAVE_PRSTATUS32_T */ |
4a938328 MS |
9013 | else |
9014 | { | |
9015 | /* Fail - we don't know how to handle any other | |
9016 | note size (ie. data object type). */ | |
b34976b6 | 9017 | return TRUE; |
4a938328 | 9018 | } |
252b5132 | 9019 | |
bb0082d6 | 9020 | /* Make a ".reg/999" section and a ".reg" section. */ |
936e320b | 9021 | return _bfd_elfcore_make_pseudosection (abfd, ".reg", |
eea6121a | 9022 | size, note->descpos + offset); |
252b5132 RH |
9023 | } |
9024 | #endif /* defined (HAVE_PRSTATUS_T) */ | |
9025 | ||
bb0082d6 | 9026 | /* Create a pseudosection containing the exact contents of NOTE. */ |
b34976b6 | 9027 | static bfd_boolean |
217aa764 AM |
9028 | elfcore_make_note_pseudosection (bfd *abfd, |
9029 | char *name, | |
9030 | Elf_Internal_Note *note) | |
252b5132 | 9031 | { |
936e320b AM |
9032 | return _bfd_elfcore_make_pseudosection (abfd, name, |
9033 | note->descsz, note->descpos); | |
252b5132 RH |
9034 | } |
9035 | ||
ff08c6bb JB |
9036 | /* There isn't a consistent prfpregset_t across platforms, |
9037 | but it doesn't matter, because we don't have to pick this | |
c044fabd KH |
9038 | data structure apart. */ |
9039 | ||
b34976b6 | 9040 | static bfd_boolean |
217aa764 | 9041 | elfcore_grok_prfpreg (bfd *abfd, Elf_Internal_Note *note) |
ff08c6bb JB |
9042 | { |
9043 | return elfcore_make_note_pseudosection (abfd, ".reg2", note); | |
9044 | } | |
9045 | ||
ff08c6bb | 9046 | /* Linux dumps the Intel SSE regs in a note named "LINUX" with a note |
971d4640 | 9047 | type of NT_PRXFPREG. Just include the whole note's contents |
ff08c6bb | 9048 | literally. */ |
c044fabd | 9049 | |
b34976b6 | 9050 | static bfd_boolean |
217aa764 | 9051 | elfcore_grok_prxfpreg (bfd *abfd, Elf_Internal_Note *note) |
ff08c6bb JB |
9052 | { |
9053 | return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note); | |
9054 | } | |
9055 | ||
4339cae0 L |
9056 | /* Linux dumps the Intel XSAVE extended state in a note named "LINUX" |
9057 | with a note type of NT_X86_XSTATE. Just include the whole note's | |
9058 | contents literally. */ | |
9059 | ||
9060 | static bfd_boolean | |
9061 | elfcore_grok_xstatereg (bfd *abfd, Elf_Internal_Note *note) | |
9062 | { | |
9063 | return elfcore_make_note_pseudosection (abfd, ".reg-xstate", note); | |
9064 | } | |
9065 | ||
97753bd5 AM |
9066 | static bfd_boolean |
9067 | elfcore_grok_ppc_vmx (bfd *abfd, Elf_Internal_Note *note) | |
9068 | { | |
9069 | return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vmx", note); | |
9070 | } | |
9071 | ||
89eeb0bc LM |
9072 | static bfd_boolean |
9073 | elfcore_grok_ppc_vsx (bfd *abfd, Elf_Internal_Note *note) | |
9074 | { | |
9075 | return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vsx", note); | |
9076 | } | |
97753bd5 | 9077 | |
0675e188 UW |
9078 | static bfd_boolean |
9079 | elfcore_grok_s390_high_gprs (bfd *abfd, Elf_Internal_Note *note) | |
9080 | { | |
9081 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-high-gprs", note); | |
9082 | } | |
9083 | ||
d7eeb400 MS |
9084 | static bfd_boolean |
9085 | elfcore_grok_s390_timer (bfd *abfd, Elf_Internal_Note *note) | |
9086 | { | |
9087 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-timer", note); | |
9088 | } | |
9089 | ||
9090 | static bfd_boolean | |
9091 | elfcore_grok_s390_todcmp (bfd *abfd, Elf_Internal_Note *note) | |
9092 | { | |
9093 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-todcmp", note); | |
9094 | } | |
9095 | ||
9096 | static bfd_boolean | |
9097 | elfcore_grok_s390_todpreg (bfd *abfd, Elf_Internal_Note *note) | |
9098 | { | |
9099 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-todpreg", note); | |
9100 | } | |
9101 | ||
9102 | static bfd_boolean | |
9103 | elfcore_grok_s390_ctrs (bfd *abfd, Elf_Internal_Note *note) | |
9104 | { | |
9105 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-ctrs", note); | |
9106 | } | |
9107 | ||
9108 | static bfd_boolean | |
9109 | elfcore_grok_s390_prefix (bfd *abfd, Elf_Internal_Note *note) | |
9110 | { | |
9111 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-prefix", note); | |
9112 | } | |
9113 | ||
355b81d9 UW |
9114 | static bfd_boolean |
9115 | elfcore_grok_s390_last_break (bfd *abfd, Elf_Internal_Note *note) | |
9116 | { | |
9117 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-last-break", note); | |
9118 | } | |
9119 | ||
9120 | static bfd_boolean | |
9121 | elfcore_grok_s390_system_call (bfd *abfd, Elf_Internal_Note *note) | |
9122 | { | |
9123 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-system-call", note); | |
9124 | } | |
9125 | ||
abb3f6cc NC |
9126 | static bfd_boolean |
9127 | elfcore_grok_s390_tdb (bfd *abfd, Elf_Internal_Note *note) | |
9128 | { | |
9129 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-tdb", note); | |
9130 | } | |
9131 | ||
4ef9f41a AA |
9132 | static bfd_boolean |
9133 | elfcore_grok_s390_vxrs_low (bfd *abfd, Elf_Internal_Note *note) | |
9134 | { | |
9135 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-vxrs-low", note); | |
9136 | } | |
9137 | ||
9138 | static bfd_boolean | |
9139 | elfcore_grok_s390_vxrs_high (bfd *abfd, Elf_Internal_Note *note) | |
9140 | { | |
9141 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-vxrs-high", note); | |
9142 | } | |
9143 | ||
faa9a424 UW |
9144 | static bfd_boolean |
9145 | elfcore_grok_arm_vfp (bfd *abfd, Elf_Internal_Note *note) | |
9146 | { | |
9147 | return elfcore_make_note_pseudosection (abfd, ".reg-arm-vfp", note); | |
9148 | } | |
9149 | ||
652451f8 YZ |
9150 | static bfd_boolean |
9151 | elfcore_grok_aarch_tls (bfd *abfd, Elf_Internal_Note *note) | |
9152 | { | |
9153 | return elfcore_make_note_pseudosection (abfd, ".reg-aarch-tls", note); | |
9154 | } | |
9155 | ||
9156 | static bfd_boolean | |
9157 | elfcore_grok_aarch_hw_break (bfd *abfd, Elf_Internal_Note *note) | |
9158 | { | |
9159 | return elfcore_make_note_pseudosection (abfd, ".reg-aarch-hw-break", note); | |
9160 | } | |
9161 | ||
9162 | static bfd_boolean | |
9163 | elfcore_grok_aarch_hw_watch (bfd *abfd, Elf_Internal_Note *note) | |
9164 | { | |
9165 | return elfcore_make_note_pseudosection (abfd, ".reg-aarch-hw-watch", note); | |
9166 | } | |
9167 | ||
252b5132 | 9168 | #if defined (HAVE_PRPSINFO_T) |
4a938328 | 9169 | typedef prpsinfo_t elfcore_psinfo_t; |
7ee38065 | 9170 | #if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */ |
4a938328 MS |
9171 | typedef prpsinfo32_t elfcore_psinfo32_t; |
9172 | #endif | |
252b5132 RH |
9173 | #endif |
9174 | ||
9175 | #if defined (HAVE_PSINFO_T) | |
4a938328 | 9176 | typedef psinfo_t elfcore_psinfo_t; |
7ee38065 | 9177 | #if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */ |
4a938328 MS |
9178 | typedef psinfo32_t elfcore_psinfo32_t; |
9179 | #endif | |
252b5132 RH |
9180 | #endif |
9181 | ||
252b5132 RH |
9182 | /* return a malloc'ed copy of a string at START which is at |
9183 | most MAX bytes long, possibly without a terminating '\0'. | |
c044fabd | 9184 | the copy will always have a terminating '\0'. */ |
252b5132 | 9185 | |
936e320b | 9186 | char * |
217aa764 | 9187 | _bfd_elfcore_strndup (bfd *abfd, char *start, size_t max) |
252b5132 | 9188 | { |
dc810e39 | 9189 | char *dups; |
a50b1753 | 9190 | char *end = (char *) memchr (start, '\0', max); |
dc810e39 | 9191 | size_t len; |
252b5132 RH |
9192 | |
9193 | if (end == NULL) | |
9194 | len = max; | |
9195 | else | |
9196 | len = end - start; | |
9197 | ||
a50b1753 | 9198 | dups = (char *) bfd_alloc (abfd, len + 1); |
dc810e39 | 9199 | if (dups == NULL) |
252b5132 RH |
9200 | return NULL; |
9201 | ||
dc810e39 AM |
9202 | memcpy (dups, start, len); |
9203 | dups[len] = '\0'; | |
252b5132 | 9204 | |
dc810e39 | 9205 | return dups; |
252b5132 RH |
9206 | } |
9207 | ||
bb0082d6 | 9208 | #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) |
b34976b6 | 9209 | static bfd_boolean |
217aa764 | 9210 | elfcore_grok_psinfo (bfd *abfd, Elf_Internal_Note *note) |
252b5132 | 9211 | { |
4a938328 MS |
9212 | if (note->descsz == sizeof (elfcore_psinfo_t)) |
9213 | { | |
9214 | elfcore_psinfo_t psinfo; | |
252b5132 | 9215 | |
7ee38065 | 9216 | memcpy (&psinfo, note->descdata, sizeof (psinfo)); |
252b5132 | 9217 | |
335e41d4 | 9218 | #if defined (HAVE_PSINFO_T_PR_PID) || defined (HAVE_PRPSINFO_T_PR_PID) |
228e534f | 9219 | elf_tdata (abfd)->core->pid = psinfo.pr_pid; |
335e41d4 | 9220 | #endif |
228e534f | 9221 | elf_tdata (abfd)->core->program |
936e320b AM |
9222 | = _bfd_elfcore_strndup (abfd, psinfo.pr_fname, |
9223 | sizeof (psinfo.pr_fname)); | |
252b5132 | 9224 | |
228e534f | 9225 | elf_tdata (abfd)->core->command |
936e320b AM |
9226 | = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs, |
9227 | sizeof (psinfo.pr_psargs)); | |
4a938328 | 9228 | } |
7ee38065 | 9229 | #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T) |
4a938328 MS |
9230 | else if (note->descsz == sizeof (elfcore_psinfo32_t)) |
9231 | { | |
9232 | /* 64-bit host, 32-bit corefile */ | |
9233 | elfcore_psinfo32_t psinfo; | |
9234 | ||
7ee38065 | 9235 | memcpy (&psinfo, note->descdata, sizeof (psinfo)); |
252b5132 | 9236 | |
335e41d4 | 9237 | #if defined (HAVE_PSINFO32_T_PR_PID) || defined (HAVE_PRPSINFO32_T_PR_PID) |
228e534f | 9238 | elf_tdata (abfd)->core->pid = psinfo.pr_pid; |
335e41d4 | 9239 | #endif |
228e534f | 9240 | elf_tdata (abfd)->core->program |
936e320b AM |
9241 | = _bfd_elfcore_strndup (abfd, psinfo.pr_fname, |
9242 | sizeof (psinfo.pr_fname)); | |
4a938328 | 9243 | |
228e534f | 9244 | elf_tdata (abfd)->core->command |
936e320b AM |
9245 | = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs, |
9246 | sizeof (psinfo.pr_psargs)); | |
4a938328 MS |
9247 | } |
9248 | #endif | |
9249 | ||
9250 | else | |
9251 | { | |
9252 | /* Fail - we don't know how to handle any other | |
9253 | note size (ie. data object type). */ | |
b34976b6 | 9254 | return TRUE; |
4a938328 | 9255 | } |
252b5132 RH |
9256 | |
9257 | /* Note that for some reason, a spurious space is tacked | |
9258 | onto the end of the args in some (at least one anyway) | |
c044fabd | 9259 | implementations, so strip it off if it exists. */ |
252b5132 RH |
9260 | |
9261 | { | |
228e534f | 9262 | char *command = elf_tdata (abfd)->core->command; |
252b5132 RH |
9263 | int n = strlen (command); |
9264 | ||
9265 | if (0 < n && command[n - 1] == ' ') | |
9266 | command[n - 1] = '\0'; | |
9267 | } | |
9268 | ||
b34976b6 | 9269 | return TRUE; |
252b5132 RH |
9270 | } |
9271 | #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */ | |
9272 | ||
252b5132 | 9273 | #if defined (HAVE_PSTATUS_T) |
b34976b6 | 9274 | static bfd_boolean |
217aa764 | 9275 | elfcore_grok_pstatus (bfd *abfd, Elf_Internal_Note *note) |
252b5132 | 9276 | { |
f572a39d AM |
9277 | if (note->descsz == sizeof (pstatus_t) |
9278 | #if defined (HAVE_PXSTATUS_T) | |
9279 | || note->descsz == sizeof (pxstatus_t) | |
9280 | #endif | |
9281 | ) | |
4a938328 MS |
9282 | { |
9283 | pstatus_t pstat; | |
252b5132 | 9284 | |
4a938328 | 9285 | memcpy (&pstat, note->descdata, sizeof (pstat)); |
252b5132 | 9286 | |
228e534f | 9287 | elf_tdata (abfd)->core->pid = pstat.pr_pid; |
4a938328 | 9288 | } |
7ee38065 | 9289 | #if defined (HAVE_PSTATUS32_T) |
4a938328 MS |
9290 | else if (note->descsz == sizeof (pstatus32_t)) |
9291 | { | |
9292 | /* 64-bit host, 32-bit corefile */ | |
9293 | pstatus32_t pstat; | |
252b5132 | 9294 | |
4a938328 | 9295 | memcpy (&pstat, note->descdata, sizeof (pstat)); |
252b5132 | 9296 | |
228e534f | 9297 | elf_tdata (abfd)->core->pid = pstat.pr_pid; |
4a938328 MS |
9298 | } |
9299 | #endif | |
252b5132 RH |
9300 | /* Could grab some more details from the "representative" |
9301 | lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an | |
c044fabd | 9302 | NT_LWPSTATUS note, presumably. */ |
252b5132 | 9303 | |
b34976b6 | 9304 | return TRUE; |
252b5132 RH |
9305 | } |
9306 | #endif /* defined (HAVE_PSTATUS_T) */ | |
9307 | ||
252b5132 | 9308 | #if defined (HAVE_LWPSTATUS_T) |
b34976b6 | 9309 | static bfd_boolean |
217aa764 | 9310 | elfcore_grok_lwpstatus (bfd *abfd, Elf_Internal_Note *note) |
252b5132 RH |
9311 | { |
9312 | lwpstatus_t lwpstat; | |
9313 | char buf[100]; | |
c044fabd | 9314 | char *name; |
d4c88bbb | 9315 | size_t len; |
c044fabd | 9316 | asection *sect; |
252b5132 | 9317 | |
f572a39d AM |
9318 | if (note->descsz != sizeof (lwpstat) |
9319 | #if defined (HAVE_LWPXSTATUS_T) | |
9320 | && note->descsz != sizeof (lwpxstatus_t) | |
9321 | #endif | |
9322 | ) | |
b34976b6 | 9323 | return TRUE; |
252b5132 RH |
9324 | |
9325 | memcpy (&lwpstat, note->descdata, sizeof (lwpstat)); | |
9326 | ||
228e534f | 9327 | elf_tdata (abfd)->core->lwpid = lwpstat.pr_lwpid; |
a1504221 JB |
9328 | /* Do not overwrite the core signal if it has already been set by |
9329 | another thread. */ | |
228e534f AM |
9330 | if (elf_tdata (abfd)->core->signal == 0) |
9331 | elf_tdata (abfd)->core->signal = lwpstat.pr_cursig; | |
252b5132 | 9332 | |
c044fabd | 9333 | /* Make a ".reg/999" section. */ |
252b5132 RH |
9334 | |
9335 | sprintf (buf, ".reg/%d", elfcore_make_pid (abfd)); | |
d4c88bbb | 9336 | len = strlen (buf) + 1; |
217aa764 | 9337 | name = bfd_alloc (abfd, len); |
252b5132 | 9338 | if (name == NULL) |
b34976b6 | 9339 | return FALSE; |
d4c88bbb | 9340 | memcpy (name, buf, len); |
252b5132 | 9341 | |
117ed4f8 | 9342 | sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); |
252b5132 | 9343 | if (sect == NULL) |
b34976b6 | 9344 | return FALSE; |
252b5132 RH |
9345 | |
9346 | #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT) | |
eea6121a | 9347 | sect->size = sizeof (lwpstat.pr_context.uc_mcontext.gregs); |
252b5132 RH |
9348 | sect->filepos = note->descpos |
9349 | + offsetof (lwpstatus_t, pr_context.uc_mcontext.gregs); | |
9350 | #endif | |
9351 | ||
9352 | #if defined (HAVE_LWPSTATUS_T_PR_REG) | |
eea6121a | 9353 | sect->size = sizeof (lwpstat.pr_reg); |
252b5132 RH |
9354 | sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_reg); |
9355 | #endif | |
9356 | ||
252b5132 RH |
9357 | sect->alignment_power = 2; |
9358 | ||
9359 | if (!elfcore_maybe_make_sect (abfd, ".reg", sect)) | |
b34976b6 | 9360 | return FALSE; |
252b5132 RH |
9361 | |
9362 | /* Make a ".reg2/999" section */ | |
9363 | ||
9364 | sprintf (buf, ".reg2/%d", elfcore_make_pid (abfd)); | |
d4c88bbb | 9365 | len = strlen (buf) + 1; |
217aa764 | 9366 | name = bfd_alloc (abfd, len); |
252b5132 | 9367 | if (name == NULL) |
b34976b6 | 9368 | return FALSE; |
d4c88bbb | 9369 | memcpy (name, buf, len); |
252b5132 | 9370 | |
117ed4f8 | 9371 | sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); |
252b5132 | 9372 | if (sect == NULL) |
b34976b6 | 9373 | return FALSE; |
252b5132 RH |
9374 | |
9375 | #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT) | |
eea6121a | 9376 | sect->size = sizeof (lwpstat.pr_context.uc_mcontext.fpregs); |
252b5132 RH |
9377 | sect->filepos = note->descpos |
9378 | + offsetof (lwpstatus_t, pr_context.uc_mcontext.fpregs); | |
9379 | #endif | |
9380 | ||
9381 | #if defined (HAVE_LWPSTATUS_T_PR_FPREG) | |
eea6121a | 9382 | sect->size = sizeof (lwpstat.pr_fpreg); |
252b5132 RH |
9383 | sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_fpreg); |
9384 | #endif | |
9385 | ||
252b5132 RH |
9386 | sect->alignment_power = 2; |
9387 | ||
936e320b | 9388 | return elfcore_maybe_make_sect (abfd, ".reg2", sect); |
252b5132 RH |
9389 | } |
9390 | #endif /* defined (HAVE_LWPSTATUS_T) */ | |
9391 | ||
b34976b6 | 9392 | static bfd_boolean |
217aa764 | 9393 | elfcore_grok_win32pstatus (bfd *abfd, Elf_Internal_Note *note) |
16e9c715 NC |
9394 | { |
9395 | char buf[30]; | |
c044fabd | 9396 | char *name; |
d4c88bbb | 9397 | size_t len; |
c044fabd | 9398 | asection *sect; |
4a6636fb PA |
9399 | int type; |
9400 | int is_active_thread; | |
9401 | bfd_vma base_addr; | |
16e9c715 | 9402 | |
4a6636fb | 9403 | if (note->descsz < 728) |
b34976b6 | 9404 | return TRUE; |
16e9c715 | 9405 | |
4a6636fb PA |
9406 | if (! CONST_STRNEQ (note->namedata, "win32")) |
9407 | return TRUE; | |
9408 | ||
9409 | type = bfd_get_32 (abfd, note->descdata); | |
c044fabd | 9410 | |
4a6636fb | 9411 | switch (type) |
16e9c715 | 9412 | { |
4a6636fb | 9413 | case 1 /* NOTE_INFO_PROCESS */: |
228e534f | 9414 | /* FIXME: need to add ->core->command. */ |
4a6636fb | 9415 | /* process_info.pid */ |
228e534f | 9416 | elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, note->descdata + 8); |
4a6636fb | 9417 | /* process_info.signal */ |
228e534f | 9418 | elf_tdata (abfd)->core->signal = bfd_get_32 (abfd, note->descdata + 12); |
c044fabd | 9419 | break; |
16e9c715 | 9420 | |
4a6636fb | 9421 | case 2 /* NOTE_INFO_THREAD */: |
16e9c715 | 9422 | /* Make a ".reg/999" section. */ |
4a6636fb PA |
9423 | /* thread_info.tid */ |
9424 | sprintf (buf, ".reg/%ld", (long) bfd_get_32 (abfd, note->descdata + 8)); | |
c044fabd | 9425 | |
d4c88bbb | 9426 | len = strlen (buf) + 1; |
a50b1753 | 9427 | name = (char *) bfd_alloc (abfd, len); |
16e9c715 | 9428 | if (name == NULL) |
b34976b6 | 9429 | return FALSE; |
c044fabd | 9430 | |
d4c88bbb | 9431 | memcpy (name, buf, len); |
16e9c715 | 9432 | |
117ed4f8 | 9433 | sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); |
16e9c715 | 9434 | if (sect == NULL) |
b34976b6 | 9435 | return FALSE; |
c044fabd | 9436 | |
4a6636fb PA |
9437 | /* sizeof (thread_info.thread_context) */ |
9438 | sect->size = 716; | |
9439 | /* offsetof (thread_info.thread_context) */ | |
9440 | sect->filepos = note->descpos + 12; | |
16e9c715 NC |
9441 | sect->alignment_power = 2; |
9442 | ||
4a6636fb PA |
9443 | /* thread_info.is_active_thread */ |
9444 | is_active_thread = bfd_get_32 (abfd, note->descdata + 8); | |
9445 | ||
9446 | if (is_active_thread) | |
16e9c715 | 9447 | if (! elfcore_maybe_make_sect (abfd, ".reg", sect)) |
b34976b6 | 9448 | return FALSE; |
16e9c715 NC |
9449 | break; |
9450 | ||
4a6636fb | 9451 | case 3 /* NOTE_INFO_MODULE */: |
16e9c715 | 9452 | /* Make a ".module/xxxxxxxx" section. */ |
4a6636fb PA |
9453 | /* module_info.base_address */ |
9454 | base_addr = bfd_get_32 (abfd, note->descdata + 4); | |
0af1713e | 9455 | sprintf (buf, ".module/%08lx", (unsigned long) base_addr); |
c044fabd | 9456 | |
d4c88bbb | 9457 | len = strlen (buf) + 1; |
a50b1753 | 9458 | name = (char *) bfd_alloc (abfd, len); |
16e9c715 | 9459 | if (name == NULL) |
b34976b6 | 9460 | return FALSE; |
c044fabd | 9461 | |
d4c88bbb | 9462 | memcpy (name, buf, len); |
252b5132 | 9463 | |
117ed4f8 | 9464 | sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); |
c044fabd | 9465 | |
16e9c715 | 9466 | if (sect == NULL) |
b34976b6 | 9467 | return FALSE; |
c044fabd | 9468 | |
eea6121a | 9469 | sect->size = note->descsz; |
16e9c715 | 9470 | sect->filepos = note->descpos; |
16e9c715 NC |
9471 | sect->alignment_power = 2; |
9472 | break; | |
9473 | ||
9474 | default: | |
b34976b6 | 9475 | return TRUE; |
16e9c715 NC |
9476 | } |
9477 | ||
b34976b6 | 9478 | return TRUE; |
16e9c715 | 9479 | } |
252b5132 | 9480 | |
b34976b6 | 9481 | static bfd_boolean |
217aa764 | 9482 | elfcore_grok_note (bfd *abfd, Elf_Internal_Note *note) |
252b5132 | 9483 | { |
9c5bfbb7 | 9484 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
bb0082d6 | 9485 | |
252b5132 RH |
9486 | switch (note->type) |
9487 | { | |
9488 | default: | |
b34976b6 | 9489 | return TRUE; |
252b5132 | 9490 | |
252b5132 | 9491 | case NT_PRSTATUS: |
bb0082d6 AM |
9492 | if (bed->elf_backend_grok_prstatus) |
9493 | if ((*bed->elf_backend_grok_prstatus) (abfd, note)) | |
b34976b6 | 9494 | return TRUE; |
bb0082d6 | 9495 | #if defined (HAVE_PRSTATUS_T) |
252b5132 | 9496 | return elfcore_grok_prstatus (abfd, note); |
bb0082d6 | 9497 | #else |
b34976b6 | 9498 | return TRUE; |
252b5132 RH |
9499 | #endif |
9500 | ||
9501 | #if defined (HAVE_PSTATUS_T) | |
9502 | case NT_PSTATUS: | |
9503 | return elfcore_grok_pstatus (abfd, note); | |
9504 | #endif | |
9505 | ||
9506 | #if defined (HAVE_LWPSTATUS_T) | |
9507 | case NT_LWPSTATUS: | |
9508 | return elfcore_grok_lwpstatus (abfd, note); | |
9509 | #endif | |
9510 | ||
9511 | case NT_FPREGSET: /* FIXME: rename to NT_PRFPREG */ | |
9512 | return elfcore_grok_prfpreg (abfd, note); | |
9513 | ||
c044fabd | 9514 | case NT_WIN32PSTATUS: |
16e9c715 | 9515 | return elfcore_grok_win32pstatus (abfd, note); |
16e9c715 | 9516 | |
c044fabd | 9517 | case NT_PRXFPREG: /* Linux SSE extension */ |
e377ab71 MK |
9518 | if (note->namesz == 6 |
9519 | && strcmp (note->namedata, "LINUX") == 0) | |
ff08c6bb JB |
9520 | return elfcore_grok_prxfpreg (abfd, note); |
9521 | else | |
b34976b6 | 9522 | return TRUE; |
ff08c6bb | 9523 | |
4339cae0 L |
9524 | case NT_X86_XSTATE: /* Linux XSAVE extension */ |
9525 | if (note->namesz == 6 | |
9526 | && strcmp (note->namedata, "LINUX") == 0) | |
9527 | return elfcore_grok_xstatereg (abfd, note); | |
9528 | else | |
9529 | return TRUE; | |
9530 | ||
97753bd5 AM |
9531 | case NT_PPC_VMX: |
9532 | if (note->namesz == 6 | |
9533 | && strcmp (note->namedata, "LINUX") == 0) | |
9534 | return elfcore_grok_ppc_vmx (abfd, note); | |
9535 | else | |
9536 | return TRUE; | |
9537 | ||
89eeb0bc LM |
9538 | case NT_PPC_VSX: |
9539 | if (note->namesz == 6 | |
9540 | && strcmp (note->namedata, "LINUX") == 0) | |
9541 | return elfcore_grok_ppc_vsx (abfd, note); | |
9542 | else | |
9543 | return TRUE; | |
9544 | ||
0675e188 UW |
9545 | case NT_S390_HIGH_GPRS: |
9546 | if (note->namesz == 6 | |
9547 | && strcmp (note->namedata, "LINUX") == 0) | |
9548 | return elfcore_grok_s390_high_gprs (abfd, note); | |
9549 | else | |
9550 | return TRUE; | |
9551 | ||
d7eeb400 MS |
9552 | case NT_S390_TIMER: |
9553 | if (note->namesz == 6 | |
9554 | && strcmp (note->namedata, "LINUX") == 0) | |
9555 | return elfcore_grok_s390_timer (abfd, note); | |
9556 | else | |
9557 | return TRUE; | |
9558 | ||
9559 | case NT_S390_TODCMP: | |
9560 | if (note->namesz == 6 | |
9561 | && strcmp (note->namedata, "LINUX") == 0) | |
9562 | return elfcore_grok_s390_todcmp (abfd, note); | |
9563 | else | |
9564 | return TRUE; | |
9565 | ||
9566 | case NT_S390_TODPREG: | |
9567 | if (note->namesz == 6 | |
9568 | && strcmp (note->namedata, "LINUX") == 0) | |
9569 | return elfcore_grok_s390_todpreg (abfd, note); | |
9570 | else | |
9571 | return TRUE; | |
9572 | ||
9573 | case NT_S390_CTRS: | |
9574 | if (note->namesz == 6 | |
9575 | && strcmp (note->namedata, "LINUX") == 0) | |
9576 | return elfcore_grok_s390_ctrs (abfd, note); | |
9577 | else | |
9578 | return TRUE; | |
9579 | ||
9580 | case NT_S390_PREFIX: | |
9581 | if (note->namesz == 6 | |
9582 | && strcmp (note->namedata, "LINUX") == 0) | |
9583 | return elfcore_grok_s390_prefix (abfd, note); | |
9584 | else | |
9585 | return TRUE; | |
9586 | ||
355b81d9 UW |
9587 | case NT_S390_LAST_BREAK: |
9588 | if (note->namesz == 6 | |
9589 | && strcmp (note->namedata, "LINUX") == 0) | |
9590 | return elfcore_grok_s390_last_break (abfd, note); | |
9591 | else | |
9592 | return TRUE; | |
9593 | ||
9594 | case NT_S390_SYSTEM_CALL: | |
9595 | if (note->namesz == 6 | |
9596 | && strcmp (note->namedata, "LINUX") == 0) | |
9597 | return elfcore_grok_s390_system_call (abfd, note); | |
9598 | else | |
9599 | return TRUE; | |
9600 | ||
abb3f6cc NC |
9601 | case NT_S390_TDB: |
9602 | if (note->namesz == 6 | |
9603 | && strcmp (note->namedata, "LINUX") == 0) | |
9604 | return elfcore_grok_s390_tdb (abfd, note); | |
9605 | else | |
9606 | return TRUE; | |
9607 | ||
4ef9f41a AA |
9608 | case NT_S390_VXRS_LOW: |
9609 | if (note->namesz == 6 | |
9610 | && strcmp (note->namedata, "LINUX") == 0) | |
9611 | return elfcore_grok_s390_vxrs_low (abfd, note); | |
9612 | else | |
9613 | return TRUE; | |
9614 | ||
9615 | case NT_S390_VXRS_HIGH: | |
9616 | if (note->namesz == 6 | |
9617 | && strcmp (note->namedata, "LINUX") == 0) | |
9618 | return elfcore_grok_s390_vxrs_high (abfd, note); | |
9619 | else | |
9620 | return TRUE; | |
9621 | ||
faa9a424 UW |
9622 | case NT_ARM_VFP: |
9623 | if (note->namesz == 6 | |
9624 | && strcmp (note->namedata, "LINUX") == 0) | |
9625 | return elfcore_grok_arm_vfp (abfd, note); | |
9626 | else | |
9627 | return TRUE; | |
9628 | ||
652451f8 YZ |
9629 | case NT_ARM_TLS: |
9630 | if (note->namesz == 6 | |
9631 | && strcmp (note->namedata, "LINUX") == 0) | |
9632 | return elfcore_grok_aarch_tls (abfd, note); | |
9633 | else | |
9634 | return TRUE; | |
9635 | ||
9636 | case NT_ARM_HW_BREAK: | |
9637 | if (note->namesz == 6 | |
9638 | && strcmp (note->namedata, "LINUX") == 0) | |
9639 | return elfcore_grok_aarch_hw_break (abfd, note); | |
9640 | else | |
9641 | return TRUE; | |
9642 | ||
9643 | case NT_ARM_HW_WATCH: | |
9644 | if (note->namesz == 6 | |
9645 | && strcmp (note->namedata, "LINUX") == 0) | |
9646 | return elfcore_grok_aarch_hw_watch (abfd, note); | |
9647 | else | |
9648 | return TRUE; | |
9649 | ||
252b5132 RH |
9650 | case NT_PRPSINFO: |
9651 | case NT_PSINFO: | |
bb0082d6 AM |
9652 | if (bed->elf_backend_grok_psinfo) |
9653 | if ((*bed->elf_backend_grok_psinfo) (abfd, note)) | |
b34976b6 | 9654 | return TRUE; |
bb0082d6 | 9655 | #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) |
252b5132 | 9656 | return elfcore_grok_psinfo (abfd, note); |
bb0082d6 | 9657 | #else |
b34976b6 | 9658 | return TRUE; |
252b5132 | 9659 | #endif |
3333a7c3 RM |
9660 | |
9661 | case NT_AUXV: | |
9662 | { | |
117ed4f8 AM |
9663 | asection *sect = bfd_make_section_anyway_with_flags (abfd, ".auxv", |
9664 | SEC_HAS_CONTENTS); | |
3333a7c3 RM |
9665 | |
9666 | if (sect == NULL) | |
9667 | return FALSE; | |
eea6121a | 9668 | sect->size = note->descsz; |
3333a7c3 | 9669 | sect->filepos = note->descpos; |
3333a7c3 RM |
9670 | sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32; |
9671 | ||
9672 | return TRUE; | |
9673 | } | |
9015683b | 9674 | |
451b7c33 TT |
9675 | case NT_FILE: |
9676 | return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.file", | |
9677 | note); | |
9678 | ||
9015683b TT |
9679 | case NT_SIGINFO: |
9680 | return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.siginfo", | |
9681 | note); | |
5b2c414d | 9682 | |
252b5132 RH |
9683 | } |
9684 | } | |
9685 | ||
718175fa JK |
9686 | static bfd_boolean |
9687 | elfobj_grok_gnu_build_id (bfd *abfd, Elf_Internal_Note *note) | |
9688 | { | |
c74f7d1c | 9689 | struct bfd_build_id* build_id; |
30e8ee25 AM |
9690 | |
9691 | if (note->descsz == 0) | |
9692 | return FALSE; | |
9693 | ||
c74f7d1c JT |
9694 | build_id = bfd_alloc (abfd, sizeof (struct bfd_build_id) - 1 + note->descsz); |
9695 | if (build_id == NULL) | |
718175fa JK |
9696 | return FALSE; |
9697 | ||
c74f7d1c JT |
9698 | build_id->size = note->descsz; |
9699 | memcpy (build_id->data, note->descdata, note->descsz); | |
9700 | abfd->build_id = build_id; | |
718175fa JK |
9701 | |
9702 | return TRUE; | |
9703 | } | |
9704 | ||
9705 | static bfd_boolean | |
9706 | elfobj_grok_gnu_note (bfd *abfd, Elf_Internal_Note *note) | |
9707 | { | |
9708 | switch (note->type) | |
9709 | { | |
9710 | default: | |
9711 | return TRUE; | |
9712 | ||
9713 | case NT_GNU_BUILD_ID: | |
9714 | return elfobj_grok_gnu_build_id (abfd, note); | |
9715 | } | |
9716 | } | |
9717 | ||
e21e5835 NC |
9718 | static bfd_boolean |
9719 | elfobj_grok_stapsdt_note_1 (bfd *abfd, Elf_Internal_Note *note) | |
9720 | { | |
9721 | struct sdt_note *cur = | |
9722 | (struct sdt_note *) bfd_alloc (abfd, sizeof (struct sdt_note) | |
9723 | + note->descsz); | |
9724 | ||
9725 | cur->next = (struct sdt_note *) (elf_tdata (abfd))->sdt_note_head; | |
9726 | cur->size = (bfd_size_type) note->descsz; | |
9727 | memcpy (cur->data, note->descdata, note->descsz); | |
9728 | ||
9729 | elf_tdata (abfd)->sdt_note_head = cur; | |
9730 | ||
9731 | return TRUE; | |
9732 | } | |
9733 | ||
9734 | static bfd_boolean | |
9735 | elfobj_grok_stapsdt_note (bfd *abfd, Elf_Internal_Note *note) | |
9736 | { | |
9737 | switch (note->type) | |
9738 | { | |
9739 | case NT_STAPSDT: | |
9740 | return elfobj_grok_stapsdt_note_1 (abfd, note); | |
9741 | ||
9742 | default: | |
9743 | return TRUE; | |
9744 | } | |
9745 | } | |
9746 | ||
aa1ed4a9 JB |
9747 | static bfd_boolean |
9748 | elfcore_grok_freebsd_psinfo (bfd *abfd, Elf_Internal_Note *note) | |
9749 | { | |
9750 | size_t offset; | |
9751 | ||
aa1ed4a9 JB |
9752 | switch (abfd->arch_info->bits_per_word) |
9753 | { | |
9754 | case 32: | |
0064d223 JB |
9755 | if (note->descsz < 108) |
9756 | return FALSE; | |
aa1ed4a9 JB |
9757 | break; |
9758 | ||
9759 | case 64: | |
0064d223 JB |
9760 | if (note->descsz < 120) |
9761 | return FALSE; | |
aa1ed4a9 JB |
9762 | break; |
9763 | ||
9764 | default: | |
9765 | return FALSE; | |
9766 | } | |
9767 | ||
0064d223 JB |
9768 | /* Check for version 1 in pr_version. */ |
9769 | if (bfd_h_get_32 (abfd, (bfd_byte *) note->descdata) != 1) | |
9770 | return FALSE; | |
9771 | offset = 4; | |
9772 | ||
9773 | /* Skip over pr_psinfosz. */ | |
9774 | if (abfd->arch_info->bits_per_word == 32) | |
9775 | offset += 4; | |
9776 | else | |
9777 | { | |
9778 | offset += 4; /* Padding before pr_psinfosz. */ | |
9779 | offset += 8; | |
9780 | } | |
9781 | ||
aa1ed4a9 JB |
9782 | /* pr_fname is PRFNAMESZ (16) + 1 bytes in size. */ |
9783 | elf_tdata (abfd)->core->program | |
9784 | = _bfd_elfcore_strndup (abfd, note->descdata + offset, 17); | |
9785 | offset += 17; | |
9786 | ||
9787 | /* pr_psargs is PRARGSZ (80) + 1 bytes in size. */ | |
9788 | elf_tdata (abfd)->core->command | |
9789 | = _bfd_elfcore_strndup (abfd, note->descdata + offset, 81); | |
0064d223 JB |
9790 | offset += 81; |
9791 | ||
9792 | /* Padding before pr_pid. */ | |
9793 | offset += 2; | |
9794 | ||
9795 | /* The pr_pid field was added in version "1a". */ | |
9796 | if (note->descsz < offset + 4) | |
9797 | return TRUE; | |
9798 | ||
9799 | elf_tdata (abfd)->core->pid | |
9800 | = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset); | |
aa1ed4a9 JB |
9801 | |
9802 | return TRUE; | |
9803 | } | |
9804 | ||
9805 | static bfd_boolean | |
9806 | elfcore_grok_freebsd_prstatus (bfd *abfd, Elf_Internal_Note *note) | |
9807 | { | |
9808 | size_t offset; | |
9809 | size_t size; | |
9810 | ||
9811 | /* Check for version 1 in pr_version. */ | |
9812 | if (bfd_h_get_32 (abfd, (bfd_byte *) note->descdata) != 1) | |
9813 | return FALSE; | |
9814 | offset = 4; | |
9815 | ||
9816 | /* Skip over pr_statussz. */ | |
9817 | switch (abfd->arch_info->bits_per_word) | |
9818 | { | |
9819 | case 32: | |
9820 | offset += 4; | |
9821 | break; | |
9822 | ||
9823 | case 64: | |
9824 | offset += 4; /* Padding before pr_statussz. */ | |
9825 | offset += 8; | |
9826 | break; | |
9827 | ||
9828 | default: | |
9829 | return FALSE; | |
9830 | } | |
9831 | ||
9832 | /* Extract size of pr_reg from pr_gregsetsz. */ | |
9833 | if (abfd->arch_info->bits_per_word == 32) | |
9834 | size = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset); | |
9835 | else | |
9836 | size = bfd_h_get_64 (abfd, (bfd_byte *) note->descdata + offset); | |
9837 | ||
9838 | /* Skip over pr_gregsetsz and pr_fpregsetsz. */ | |
9839 | offset += (abfd->arch_info->bits_per_word / 8) * 2; | |
9840 | ||
9841 | /* Skip over pr_osreldate. */ | |
9842 | offset += 4; | |
9843 | ||
9844 | /* Read signal from pr_cursig. */ | |
9845 | if (elf_tdata (abfd)->core->signal == 0) | |
9846 | elf_tdata (abfd)->core->signal | |
9847 | = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset); | |
9848 | offset += 4; | |
9849 | ||
9850 | /* Read TID from pr_pid. */ | |
9851 | elf_tdata (abfd)->core->lwpid | |
9852 | = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset); | |
9853 | offset += 4; | |
9854 | ||
9855 | /* Padding before pr_reg. */ | |
9856 | if (abfd->arch_info->bits_per_word == 64) | |
9857 | offset += 4; | |
9858 | ||
9859 | /* Make a ".reg/999" section and a ".reg" section. */ | |
9860 | return _bfd_elfcore_make_pseudosection (abfd, ".reg", | |
9861 | size, note->descpos + offset); | |
9862 | } | |
9863 | ||
9864 | static bfd_boolean | |
9865 | elfcore_grok_freebsd_note (bfd *abfd, Elf_Internal_Note *note) | |
9866 | { | |
9867 | switch (note->type) | |
9868 | { | |
9869 | case NT_PRSTATUS: | |
9870 | return elfcore_grok_freebsd_prstatus (abfd, note); | |
9871 | ||
9872 | case NT_FPREGSET: | |
9873 | return elfcore_grok_prfpreg (abfd, note); | |
9874 | ||
9875 | case NT_PRPSINFO: | |
9876 | return elfcore_grok_freebsd_psinfo (abfd, note); | |
9877 | ||
9878 | case NT_FREEBSD_THRMISC: | |
9879 | if (note->namesz == 8) | |
9880 | return elfcore_make_note_pseudosection (abfd, ".thrmisc", note); | |
9881 | else | |
9882 | return TRUE; | |
9883 | ||
3350c5f5 JB |
9884 | case NT_FREEBSD_PROCSTAT_AUXV: |
9885 | { | |
9886 | asection *sect = bfd_make_section_anyway_with_flags (abfd, ".auxv", | |
9887 | SEC_HAS_CONTENTS); | |
9888 | ||
9889 | if (sect == NULL) | |
9890 | return FALSE; | |
9891 | sect->size = note->descsz - 4; | |
9892 | sect->filepos = note->descpos + 4; | |
9893 | sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32; | |
9894 | ||
9895 | return TRUE; | |
9896 | } | |
9897 | ||
aa1ed4a9 JB |
9898 | case NT_X86_XSTATE: |
9899 | if (note->namesz == 8) | |
9900 | return elfcore_grok_xstatereg (abfd, note); | |
9901 | else | |
9902 | return TRUE; | |
9903 | ||
9904 | default: | |
9905 | return TRUE; | |
9906 | } | |
9907 | } | |
9908 | ||
b34976b6 | 9909 | static bfd_boolean |
217aa764 | 9910 | elfcore_netbsd_get_lwpid (Elf_Internal_Note *note, int *lwpidp) |
50b2bdb7 AM |
9911 | { |
9912 | char *cp; | |
9913 | ||
9914 | cp = strchr (note->namedata, '@'); | |
9915 | if (cp != NULL) | |
9916 | { | |
d2b64500 | 9917 | *lwpidp = atoi(cp + 1); |
b34976b6 | 9918 | return TRUE; |
50b2bdb7 | 9919 | } |
b34976b6 | 9920 | return FALSE; |
50b2bdb7 AM |
9921 | } |
9922 | ||
b34976b6 | 9923 | static bfd_boolean |
217aa764 | 9924 | elfcore_grok_netbsd_procinfo (bfd *abfd, Elf_Internal_Note *note) |
50b2bdb7 | 9925 | { |
50b2bdb7 | 9926 | /* Signal number at offset 0x08. */ |
228e534f | 9927 | elf_tdata (abfd)->core->signal |
50b2bdb7 AM |
9928 | = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08); |
9929 | ||
9930 | /* Process ID at offset 0x50. */ | |
228e534f | 9931 | elf_tdata (abfd)->core->pid |
50b2bdb7 AM |
9932 | = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x50); |
9933 | ||
9934 | /* Command name at 0x7c (max 32 bytes, including nul). */ | |
228e534f | 9935 | elf_tdata (abfd)->core->command |
50b2bdb7 AM |
9936 | = _bfd_elfcore_strndup (abfd, note->descdata + 0x7c, 31); |
9937 | ||
7720ba9f MK |
9938 | return elfcore_make_note_pseudosection (abfd, ".note.netbsdcore.procinfo", |
9939 | note); | |
50b2bdb7 AM |
9940 | } |
9941 | ||
b34976b6 | 9942 | static bfd_boolean |
217aa764 | 9943 | elfcore_grok_netbsd_note (bfd *abfd, Elf_Internal_Note *note) |
50b2bdb7 AM |
9944 | { |
9945 | int lwp; | |
9946 | ||
9947 | if (elfcore_netbsd_get_lwpid (note, &lwp)) | |
228e534f | 9948 | elf_tdata (abfd)->core->lwpid = lwp; |
50b2bdb7 | 9949 | |
b4db1224 | 9950 | if (note->type == NT_NETBSDCORE_PROCINFO) |
50b2bdb7 AM |
9951 | { |
9952 | /* NetBSD-specific core "procinfo". Note that we expect to | |
08a40648 AM |
9953 | find this note before any of the others, which is fine, |
9954 | since the kernel writes this note out first when it | |
9955 | creates a core file. */ | |
47d9a591 | 9956 | |
50b2bdb7 AM |
9957 | return elfcore_grok_netbsd_procinfo (abfd, note); |
9958 | } | |
9959 | ||
b4db1224 JT |
9960 | /* As of Jan 2002 there are no other machine-independent notes |
9961 | defined for NetBSD core files. If the note type is less | |
9962 | than the start of the machine-dependent note types, we don't | |
9963 | understand it. */ | |
47d9a591 | 9964 | |
b4db1224 | 9965 | if (note->type < NT_NETBSDCORE_FIRSTMACH) |
b34976b6 | 9966 | return TRUE; |
50b2bdb7 AM |
9967 | |
9968 | ||
9969 | switch (bfd_get_arch (abfd)) | |
9970 | { | |
08a40648 AM |
9971 | /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and |
9972 | PT_GETFPREGS == mach+2. */ | |
50b2bdb7 AM |
9973 | |
9974 | case bfd_arch_alpha: | |
9975 | case bfd_arch_sparc: | |
9976 | switch (note->type) | |
08a40648 AM |
9977 | { |
9978 | case NT_NETBSDCORE_FIRSTMACH+0: | |
9979 | return elfcore_make_note_pseudosection (abfd, ".reg", note); | |
50b2bdb7 | 9980 | |
08a40648 AM |
9981 | case NT_NETBSDCORE_FIRSTMACH+2: |
9982 | return elfcore_make_note_pseudosection (abfd, ".reg2", note); | |
50b2bdb7 | 9983 | |
08a40648 AM |
9984 | default: |
9985 | return TRUE; | |
9986 | } | |
50b2bdb7 | 9987 | |
08a40648 AM |
9988 | /* On all other arch's, PT_GETREGS == mach+1 and |
9989 | PT_GETFPREGS == mach+3. */ | |
50b2bdb7 AM |
9990 | |
9991 | default: | |
9992 | switch (note->type) | |
08a40648 AM |
9993 | { |
9994 | case NT_NETBSDCORE_FIRSTMACH+1: | |
9995 | return elfcore_make_note_pseudosection (abfd, ".reg", note); | |
50b2bdb7 | 9996 | |
08a40648 AM |
9997 | case NT_NETBSDCORE_FIRSTMACH+3: |
9998 | return elfcore_make_note_pseudosection (abfd, ".reg2", note); | |
50b2bdb7 | 9999 | |
08a40648 AM |
10000 | default: |
10001 | return TRUE; | |
10002 | } | |
50b2bdb7 AM |
10003 | } |
10004 | /* NOTREACHED */ | |
10005 | } | |
10006 | ||
67cc5033 MK |
10007 | static bfd_boolean |
10008 | elfcore_grok_openbsd_procinfo (bfd *abfd, Elf_Internal_Note *note) | |
10009 | { | |
10010 | /* Signal number at offset 0x08. */ | |
228e534f | 10011 | elf_tdata (abfd)->core->signal |
67cc5033 MK |
10012 | = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08); |
10013 | ||
10014 | /* Process ID at offset 0x20. */ | |
228e534f | 10015 | elf_tdata (abfd)->core->pid |
67cc5033 MK |
10016 | = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x20); |
10017 | ||
10018 | /* Command name at 0x48 (max 32 bytes, including nul). */ | |
228e534f | 10019 | elf_tdata (abfd)->core->command |
67cc5033 MK |
10020 | = _bfd_elfcore_strndup (abfd, note->descdata + 0x48, 31); |
10021 | ||
10022 | return TRUE; | |
10023 | } | |
10024 | ||
10025 | static bfd_boolean | |
10026 | elfcore_grok_openbsd_note (bfd *abfd, Elf_Internal_Note *note) | |
10027 | { | |
10028 | if (note->type == NT_OPENBSD_PROCINFO) | |
10029 | return elfcore_grok_openbsd_procinfo (abfd, note); | |
10030 | ||
10031 | if (note->type == NT_OPENBSD_REGS) | |
10032 | return elfcore_make_note_pseudosection (abfd, ".reg", note); | |
10033 | ||
10034 | if (note->type == NT_OPENBSD_FPREGS) | |
10035 | return elfcore_make_note_pseudosection (abfd, ".reg2", note); | |
10036 | ||
10037 | if (note->type == NT_OPENBSD_XFPREGS) | |
10038 | return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note); | |
10039 | ||
10040 | if (note->type == NT_OPENBSD_AUXV) | |
10041 | { | |
10042 | asection *sect = bfd_make_section_anyway_with_flags (abfd, ".auxv", | |
10043 | SEC_HAS_CONTENTS); | |
10044 | ||
10045 | if (sect == NULL) | |
10046 | return FALSE; | |
10047 | sect->size = note->descsz; | |
10048 | sect->filepos = note->descpos; | |
10049 | sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32; | |
10050 | ||
10051 | return TRUE; | |
10052 | } | |
10053 | ||
10054 | if (note->type == NT_OPENBSD_WCOOKIE) | |
10055 | { | |
10056 | asection *sect = bfd_make_section_anyway_with_flags (abfd, ".wcookie", | |
10057 | SEC_HAS_CONTENTS); | |
10058 | ||
10059 | if (sect == NULL) | |
10060 | return FALSE; | |
10061 | sect->size = note->descsz; | |
10062 | sect->filepos = note->descpos; | |
10063 | sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32; | |
10064 | ||
10065 | return TRUE; | |
10066 | } | |
10067 | ||
10068 | return TRUE; | |
10069 | } | |
10070 | ||
07c6e936 | 10071 | static bfd_boolean |
d3fd4074 | 10072 | elfcore_grok_nto_status (bfd *abfd, Elf_Internal_Note *note, long *tid) |
07c6e936 NC |
10073 | { |
10074 | void *ddata = note->descdata; | |
10075 | char buf[100]; | |
10076 | char *name; | |
10077 | asection *sect; | |
f8843e87 AM |
10078 | short sig; |
10079 | unsigned flags; | |
07c6e936 NC |
10080 | |
10081 | /* nto_procfs_status 'pid' field is at offset 0. */ | |
228e534f | 10082 | elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, (bfd_byte *) ddata); |
07c6e936 | 10083 | |
f8843e87 AM |
10084 | /* nto_procfs_status 'tid' field is at offset 4. Pass it back. */ |
10085 | *tid = bfd_get_32 (abfd, (bfd_byte *) ddata + 4); | |
10086 | ||
10087 | /* nto_procfs_status 'flags' field is at offset 8. */ | |
10088 | flags = bfd_get_32 (abfd, (bfd_byte *) ddata + 8); | |
07c6e936 NC |
10089 | |
10090 | /* nto_procfs_status 'what' field is at offset 14. */ | |
f8843e87 AM |
10091 | if ((sig = bfd_get_16 (abfd, (bfd_byte *) ddata + 14)) > 0) |
10092 | { | |
228e534f AM |
10093 | elf_tdata (abfd)->core->signal = sig; |
10094 | elf_tdata (abfd)->core->lwpid = *tid; | |
f8843e87 | 10095 | } |
07c6e936 | 10096 | |
f8843e87 AM |
10097 | /* _DEBUG_FLAG_CURTID (current thread) is 0x80. Some cores |
10098 | do not come from signals so we make sure we set the current | |
10099 | thread just in case. */ | |
10100 | if (flags & 0x00000080) | |
228e534f | 10101 | elf_tdata (abfd)->core->lwpid = *tid; |
07c6e936 NC |
10102 | |
10103 | /* Make a ".qnx_core_status/%d" section. */ | |
d3fd4074 | 10104 | sprintf (buf, ".qnx_core_status/%ld", *tid); |
07c6e936 | 10105 | |
a50b1753 | 10106 | name = (char *) bfd_alloc (abfd, strlen (buf) + 1); |
07c6e936 NC |
10107 | if (name == NULL) |
10108 | return FALSE; | |
10109 | strcpy (name, buf); | |
10110 | ||
117ed4f8 | 10111 | sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); |
07c6e936 NC |
10112 | if (sect == NULL) |
10113 | return FALSE; | |
10114 | ||
eea6121a | 10115 | sect->size = note->descsz; |
07c6e936 | 10116 | sect->filepos = note->descpos; |
07c6e936 NC |
10117 | sect->alignment_power = 2; |
10118 | ||
10119 | return (elfcore_maybe_make_sect (abfd, ".qnx_core_status", sect)); | |
10120 | } | |
10121 | ||
10122 | static bfd_boolean | |
d69f560c KW |
10123 | elfcore_grok_nto_regs (bfd *abfd, |
10124 | Elf_Internal_Note *note, | |
d3fd4074 | 10125 | long tid, |
d69f560c | 10126 | char *base) |
07c6e936 NC |
10127 | { |
10128 | char buf[100]; | |
10129 | char *name; | |
10130 | asection *sect; | |
10131 | ||
d69f560c | 10132 | /* Make a "(base)/%d" section. */ |
d3fd4074 | 10133 | sprintf (buf, "%s/%ld", base, tid); |
07c6e936 | 10134 | |
a50b1753 | 10135 | name = (char *) bfd_alloc (abfd, strlen (buf) + 1); |
07c6e936 NC |
10136 | if (name == NULL) |
10137 | return FALSE; | |
10138 | strcpy (name, buf); | |
10139 | ||
117ed4f8 | 10140 | sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); |
07c6e936 NC |
10141 | if (sect == NULL) |
10142 | return FALSE; | |
10143 | ||
eea6121a | 10144 | sect->size = note->descsz; |
07c6e936 | 10145 | sect->filepos = note->descpos; |
07c6e936 NC |
10146 | sect->alignment_power = 2; |
10147 | ||
f8843e87 | 10148 | /* This is the current thread. */ |
228e534f | 10149 | if (elf_tdata (abfd)->core->lwpid == tid) |
d69f560c | 10150 | return elfcore_maybe_make_sect (abfd, base, sect); |
f8843e87 AM |
10151 | |
10152 | return TRUE; | |
07c6e936 NC |
10153 | } |
10154 | ||
10155 | #define BFD_QNT_CORE_INFO 7 | |
10156 | #define BFD_QNT_CORE_STATUS 8 | |
10157 | #define BFD_QNT_CORE_GREG 9 | |
10158 | #define BFD_QNT_CORE_FPREG 10 | |
10159 | ||
10160 | static bfd_boolean | |
217aa764 | 10161 | elfcore_grok_nto_note (bfd *abfd, Elf_Internal_Note *note) |
07c6e936 NC |
10162 | { |
10163 | /* Every GREG section has a STATUS section before it. Store the | |
811072d8 | 10164 | tid from the previous call to pass down to the next gregs |
07c6e936 | 10165 | function. */ |
d3fd4074 | 10166 | static long tid = 1; |
07c6e936 NC |
10167 | |
10168 | switch (note->type) | |
10169 | { | |
d69f560c KW |
10170 | case BFD_QNT_CORE_INFO: |
10171 | return elfcore_make_note_pseudosection (abfd, ".qnx_core_info", note); | |
10172 | case BFD_QNT_CORE_STATUS: | |
10173 | return elfcore_grok_nto_status (abfd, note, &tid); | |
10174 | case BFD_QNT_CORE_GREG: | |
10175 | return elfcore_grok_nto_regs (abfd, note, tid, ".reg"); | |
10176 | case BFD_QNT_CORE_FPREG: | |
10177 | return elfcore_grok_nto_regs (abfd, note, tid, ".reg2"); | |
10178 | default: | |
10179 | return TRUE; | |
07c6e936 NC |
10180 | } |
10181 | } | |
10182 | ||
b15fa79e AM |
10183 | static bfd_boolean |
10184 | elfcore_grok_spu_note (bfd *abfd, Elf_Internal_Note *note) | |
10185 | { | |
10186 | char *name; | |
10187 | asection *sect; | |
10188 | size_t len; | |
10189 | ||
10190 | /* Use note name as section name. */ | |
10191 | len = note->namesz; | |
a50b1753 | 10192 | name = (char *) bfd_alloc (abfd, len); |
b15fa79e AM |
10193 | if (name == NULL) |
10194 | return FALSE; | |
10195 | memcpy (name, note->namedata, len); | |
10196 | name[len - 1] = '\0'; | |
10197 | ||
10198 | sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); | |
10199 | if (sect == NULL) | |
10200 | return FALSE; | |
10201 | ||
10202 | sect->size = note->descsz; | |
10203 | sect->filepos = note->descpos; | |
10204 | sect->alignment_power = 1; | |
10205 | ||
10206 | return TRUE; | |
10207 | } | |
10208 | ||
7c76fa91 MS |
10209 | /* Function: elfcore_write_note |
10210 | ||
47d9a591 | 10211 | Inputs: |
a39f3346 | 10212 | buffer to hold note, and current size of buffer |
7c76fa91 MS |
10213 | name of note |
10214 | type of note | |
10215 | data for note | |
10216 | size of data for note | |
10217 | ||
a39f3346 AM |
10218 | Writes note to end of buffer. ELF64 notes are written exactly as |
10219 | for ELF32, despite the current (as of 2006) ELF gabi specifying | |
10220 | that they ought to have 8-byte namesz and descsz field, and have | |
10221 | 8-byte alignment. Other writers, eg. Linux kernel, do the same. | |
10222 | ||
7c76fa91 | 10223 | Return: |
a39f3346 | 10224 | Pointer to realloc'd buffer, *BUFSIZ updated. */ |
7c76fa91 MS |
10225 | |
10226 | char * | |
a39f3346 | 10227 | elfcore_write_note (bfd *abfd, |
217aa764 | 10228 | char *buf, |
a39f3346 | 10229 | int *bufsiz, |
217aa764 | 10230 | const char *name, |
a39f3346 | 10231 | int type, |
217aa764 | 10232 | const void *input, |
a39f3346 | 10233 | int size) |
7c76fa91 MS |
10234 | { |
10235 | Elf_External_Note *xnp; | |
d4c88bbb | 10236 | size_t namesz; |
d4c88bbb | 10237 | size_t newspace; |
a39f3346 | 10238 | char *dest; |
7c76fa91 | 10239 | |
d4c88bbb | 10240 | namesz = 0; |
d4c88bbb | 10241 | if (name != NULL) |
a39f3346 | 10242 | namesz = strlen (name) + 1; |
d4c88bbb | 10243 | |
a39f3346 | 10244 | newspace = 12 + ((namesz + 3) & -4) + ((size + 3) & -4); |
d4c88bbb | 10245 | |
a50b1753 | 10246 | buf = (char *) realloc (buf, *bufsiz + newspace); |
14b1c01e AM |
10247 | if (buf == NULL) |
10248 | return buf; | |
a39f3346 | 10249 | dest = buf + *bufsiz; |
7c76fa91 MS |
10250 | *bufsiz += newspace; |
10251 | xnp = (Elf_External_Note *) dest; | |
10252 | H_PUT_32 (abfd, namesz, xnp->namesz); | |
10253 | H_PUT_32 (abfd, size, xnp->descsz); | |
10254 | H_PUT_32 (abfd, type, xnp->type); | |
d4c88bbb AM |
10255 | dest = xnp->name; |
10256 | if (name != NULL) | |
10257 | { | |
10258 | memcpy (dest, name, namesz); | |
10259 | dest += namesz; | |
a39f3346 | 10260 | while (namesz & 3) |
d4c88bbb AM |
10261 | { |
10262 | *dest++ = '\0'; | |
a39f3346 | 10263 | ++namesz; |
d4c88bbb AM |
10264 | } |
10265 | } | |
10266 | memcpy (dest, input, size); | |
a39f3346 AM |
10267 | dest += size; |
10268 | while (size & 3) | |
10269 | { | |
10270 | *dest++ = '\0'; | |
10271 | ++size; | |
10272 | } | |
10273 | return buf; | |
7c76fa91 MS |
10274 | } |
10275 | ||
7c76fa91 | 10276 | char * |
217aa764 AM |
10277 | elfcore_write_prpsinfo (bfd *abfd, |
10278 | char *buf, | |
10279 | int *bufsiz, | |
10280 | const char *fname, | |
10281 | const char *psargs) | |
7c76fa91 | 10282 | { |
183e98be AM |
10283 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
10284 | ||
10285 | if (bed->elf_backend_write_core_note != NULL) | |
10286 | { | |
10287 | char *ret; | |
10288 | ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz, | |
10289 | NT_PRPSINFO, fname, psargs); | |
10290 | if (ret != NULL) | |
10291 | return ret; | |
10292 | } | |
7c76fa91 | 10293 | |
1f20dca5 | 10294 | #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) |
183e98be AM |
10295 | #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T) |
10296 | if (bed->s->elfclass == ELFCLASS32) | |
10297 | { | |
10298 | #if defined (HAVE_PSINFO32_T) | |
10299 | psinfo32_t data; | |
10300 | int note_type = NT_PSINFO; | |
10301 | #else | |
10302 | prpsinfo32_t data; | |
10303 | int note_type = NT_PRPSINFO; | |
10304 | #endif | |
10305 | ||
10306 | memset (&data, 0, sizeof (data)); | |
10307 | strncpy (data.pr_fname, fname, sizeof (data.pr_fname)); | |
10308 | strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs)); | |
10309 | return elfcore_write_note (abfd, buf, bufsiz, | |
1f20dca5 | 10310 | "CORE", note_type, &data, sizeof (data)); |
183e98be AM |
10311 | } |
10312 | else | |
10313 | #endif | |
10314 | { | |
7c76fa91 | 10315 | #if defined (HAVE_PSINFO_T) |
183e98be AM |
10316 | psinfo_t data; |
10317 | int note_type = NT_PSINFO; | |
7c76fa91 | 10318 | #else |
183e98be AM |
10319 | prpsinfo_t data; |
10320 | int note_type = NT_PRPSINFO; | |
7c76fa91 MS |
10321 | #endif |
10322 | ||
183e98be AM |
10323 | memset (&data, 0, sizeof (data)); |
10324 | strncpy (data.pr_fname, fname, sizeof (data.pr_fname)); | |
10325 | strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs)); | |
10326 | return elfcore_write_note (abfd, buf, bufsiz, | |
1f20dca5 | 10327 | "CORE", note_type, &data, sizeof (data)); |
183e98be | 10328 | } |
7c76fa91 MS |
10329 | #endif /* PSINFO_T or PRPSINFO_T */ |
10330 | ||
1f20dca5 UW |
10331 | free (buf); |
10332 | return NULL; | |
10333 | } | |
10334 | ||
70a38d42 SDJ |
10335 | char * |
10336 | elfcore_write_linux_prpsinfo32 | |
10337 | (bfd *abfd, char *buf, int *bufsiz, | |
10338 | const struct elf_internal_linux_prpsinfo *prpsinfo) | |
10339 | { | |
10340 | struct elf_external_linux_prpsinfo32 data; | |
10341 | ||
aeb70569 | 10342 | swap_linux_prpsinfo32_out (abfd, prpsinfo, &data); |
70a38d42 SDJ |
10343 | return elfcore_write_note (abfd, buf, bufsiz, "CORE", NT_PRPSINFO, |
10344 | &data, sizeof (data)); | |
10345 | } | |
10346 | ||
10347 | char * | |
10348 | elfcore_write_linux_prpsinfo64 | |
10349 | (bfd *abfd, char *buf, int *bufsiz, | |
10350 | const struct elf_internal_linux_prpsinfo *prpsinfo) | |
10351 | { | |
10352 | struct elf_external_linux_prpsinfo64 data; | |
10353 | ||
aeb70569 | 10354 | swap_linux_prpsinfo64_out (abfd, prpsinfo, &data); |
70a38d42 SDJ |
10355 | return elfcore_write_note (abfd, buf, bufsiz, |
10356 | "CORE", NT_PRPSINFO, &data, sizeof (data)); | |
10357 | } | |
10358 | ||
7c76fa91 | 10359 | char * |
217aa764 AM |
10360 | elfcore_write_prstatus (bfd *abfd, |
10361 | char *buf, | |
10362 | int *bufsiz, | |
10363 | long pid, | |
10364 | int cursig, | |
10365 | const void *gregs) | |
7c76fa91 | 10366 | { |
183e98be | 10367 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
7c76fa91 | 10368 | |
183e98be AM |
10369 | if (bed->elf_backend_write_core_note != NULL) |
10370 | { | |
10371 | char *ret; | |
10372 | ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz, | |
10373 | NT_PRSTATUS, | |
10374 | pid, cursig, gregs); | |
10375 | if (ret != NULL) | |
10376 | return ret; | |
10377 | } | |
10378 | ||
1f20dca5 | 10379 | #if defined (HAVE_PRSTATUS_T) |
183e98be AM |
10380 | #if defined (HAVE_PRSTATUS32_T) |
10381 | if (bed->s->elfclass == ELFCLASS32) | |
10382 | { | |
10383 | prstatus32_t prstat; | |
10384 | ||
10385 | memset (&prstat, 0, sizeof (prstat)); | |
10386 | prstat.pr_pid = pid; | |
10387 | prstat.pr_cursig = cursig; | |
10388 | memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg)); | |
1f20dca5 | 10389 | return elfcore_write_note (abfd, buf, bufsiz, "CORE", |
183e98be AM |
10390 | NT_PRSTATUS, &prstat, sizeof (prstat)); |
10391 | } | |
10392 | else | |
10393 | #endif | |
10394 | { | |
10395 | prstatus_t prstat; | |
10396 | ||
10397 | memset (&prstat, 0, sizeof (prstat)); | |
10398 | prstat.pr_pid = pid; | |
10399 | prstat.pr_cursig = cursig; | |
10400 | memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg)); | |
1f20dca5 | 10401 | return elfcore_write_note (abfd, buf, bufsiz, "CORE", |
183e98be AM |
10402 | NT_PRSTATUS, &prstat, sizeof (prstat)); |
10403 | } | |
7c76fa91 MS |
10404 | #endif /* HAVE_PRSTATUS_T */ |
10405 | ||
1f20dca5 UW |
10406 | free (buf); |
10407 | return NULL; | |
10408 | } | |
10409 | ||
51316059 MS |
10410 | #if defined (HAVE_LWPSTATUS_T) |
10411 | char * | |
217aa764 AM |
10412 | elfcore_write_lwpstatus (bfd *abfd, |
10413 | char *buf, | |
10414 | int *bufsiz, | |
10415 | long pid, | |
10416 | int cursig, | |
10417 | const void *gregs) | |
51316059 MS |
10418 | { |
10419 | lwpstatus_t lwpstat; | |
183e98be | 10420 | const char *note_name = "CORE"; |
51316059 MS |
10421 | |
10422 | memset (&lwpstat, 0, sizeof (lwpstat)); | |
10423 | lwpstat.pr_lwpid = pid >> 16; | |
10424 | lwpstat.pr_cursig = cursig; | |
10425 | #if defined (HAVE_LWPSTATUS_T_PR_REG) | |
d1e8523e | 10426 | memcpy (&lwpstat.pr_reg, gregs, sizeof (lwpstat.pr_reg)); |
51316059 MS |
10427 | #elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT) |
10428 | #if !defined(gregs) | |
10429 | memcpy (lwpstat.pr_context.uc_mcontext.gregs, | |
10430 | gregs, sizeof (lwpstat.pr_context.uc_mcontext.gregs)); | |
10431 | #else | |
10432 | memcpy (lwpstat.pr_context.uc_mcontext.__gregs, | |
10433 | gregs, sizeof (lwpstat.pr_context.uc_mcontext.__gregs)); | |
10434 | #endif | |
10435 | #endif | |
47d9a591 | 10436 | return elfcore_write_note (abfd, buf, bufsiz, note_name, |
51316059 MS |
10437 | NT_LWPSTATUS, &lwpstat, sizeof (lwpstat)); |
10438 | } | |
10439 | #endif /* HAVE_LWPSTATUS_T */ | |
10440 | ||
7c76fa91 MS |
10441 | #if defined (HAVE_PSTATUS_T) |
10442 | char * | |
217aa764 AM |
10443 | elfcore_write_pstatus (bfd *abfd, |
10444 | char *buf, | |
10445 | int *bufsiz, | |
10446 | long pid, | |
6c10990d NC |
10447 | int cursig ATTRIBUTE_UNUSED, |
10448 | const void *gregs ATTRIBUTE_UNUSED) | |
7c76fa91 | 10449 | { |
183e98be AM |
10450 | const char *note_name = "CORE"; |
10451 | #if defined (HAVE_PSTATUS32_T) | |
10452 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
7c76fa91 | 10453 | |
183e98be AM |
10454 | if (bed->s->elfclass == ELFCLASS32) |
10455 | { | |
10456 | pstatus32_t pstat; | |
10457 | ||
10458 | memset (&pstat, 0, sizeof (pstat)); | |
10459 | pstat.pr_pid = pid & 0xffff; | |
10460 | buf = elfcore_write_note (abfd, buf, bufsiz, note_name, | |
10461 | NT_PSTATUS, &pstat, sizeof (pstat)); | |
10462 | return buf; | |
10463 | } | |
10464 | else | |
10465 | #endif | |
10466 | { | |
10467 | pstatus_t pstat; | |
10468 | ||
10469 | memset (&pstat, 0, sizeof (pstat)); | |
10470 | pstat.pr_pid = pid & 0xffff; | |
10471 | buf = elfcore_write_note (abfd, buf, bufsiz, note_name, | |
10472 | NT_PSTATUS, &pstat, sizeof (pstat)); | |
10473 | return buf; | |
10474 | } | |
7c76fa91 MS |
10475 | } |
10476 | #endif /* HAVE_PSTATUS_T */ | |
10477 | ||
10478 | char * | |
217aa764 AM |
10479 | elfcore_write_prfpreg (bfd *abfd, |
10480 | char *buf, | |
10481 | int *bufsiz, | |
10482 | const void *fpregs, | |
10483 | int size) | |
7c76fa91 | 10484 | { |
183e98be | 10485 | const char *note_name = "CORE"; |
47d9a591 | 10486 | return elfcore_write_note (abfd, buf, bufsiz, |
7c76fa91 MS |
10487 | note_name, NT_FPREGSET, fpregs, size); |
10488 | } | |
10489 | ||
10490 | char * | |
217aa764 AM |
10491 | elfcore_write_prxfpreg (bfd *abfd, |
10492 | char *buf, | |
10493 | int *bufsiz, | |
10494 | const void *xfpregs, | |
10495 | int size) | |
7c76fa91 MS |
10496 | { |
10497 | char *note_name = "LINUX"; | |
47d9a591 | 10498 | return elfcore_write_note (abfd, buf, bufsiz, |
7c76fa91 MS |
10499 | note_name, NT_PRXFPREG, xfpregs, size); |
10500 | } | |
10501 | ||
4339cae0 L |
10502 | char * |
10503 | elfcore_write_xstatereg (bfd *abfd, char *buf, int *bufsiz, | |
10504 | const void *xfpregs, int size) | |
10505 | { | |
97de3545 JB |
10506 | char *note_name; |
10507 | if (get_elf_backend_data (abfd)->elf_osabi == ELFOSABI_FREEBSD) | |
10508 | note_name = "FreeBSD"; | |
10509 | else | |
10510 | note_name = "LINUX"; | |
4339cae0 L |
10511 | return elfcore_write_note (abfd, buf, bufsiz, |
10512 | note_name, NT_X86_XSTATE, xfpregs, size); | |
10513 | } | |
10514 | ||
97753bd5 AM |
10515 | char * |
10516 | elfcore_write_ppc_vmx (bfd *abfd, | |
10517 | char *buf, | |
10518 | int *bufsiz, | |
10519 | const void *ppc_vmx, | |
10520 | int size) | |
10521 | { | |
10522 | char *note_name = "LINUX"; | |
10523 | return elfcore_write_note (abfd, buf, bufsiz, | |
10524 | note_name, NT_PPC_VMX, ppc_vmx, size); | |
10525 | } | |
10526 | ||
89eeb0bc LM |
10527 | char * |
10528 | elfcore_write_ppc_vsx (bfd *abfd, | |
10529 | char *buf, | |
10530 | int *bufsiz, | |
10531 | const void *ppc_vsx, | |
10532 | int size) | |
10533 | { | |
10534 | char *note_name = "LINUX"; | |
10535 | return elfcore_write_note (abfd, buf, bufsiz, | |
10536 | note_name, NT_PPC_VSX, ppc_vsx, size); | |
10537 | } | |
10538 | ||
0675e188 UW |
10539 | static char * |
10540 | elfcore_write_s390_high_gprs (bfd *abfd, | |
10541 | char *buf, | |
10542 | int *bufsiz, | |
10543 | const void *s390_high_gprs, | |
10544 | int size) | |
10545 | { | |
10546 | char *note_name = "LINUX"; | |
10547 | return elfcore_write_note (abfd, buf, bufsiz, | |
10548 | note_name, NT_S390_HIGH_GPRS, | |
10549 | s390_high_gprs, size); | |
10550 | } | |
10551 | ||
d7eeb400 MS |
10552 | char * |
10553 | elfcore_write_s390_timer (bfd *abfd, | |
10554 | char *buf, | |
10555 | int *bufsiz, | |
10556 | const void *s390_timer, | |
10557 | int size) | |
10558 | { | |
10559 | char *note_name = "LINUX"; | |
10560 | return elfcore_write_note (abfd, buf, bufsiz, | |
10561 | note_name, NT_S390_TIMER, s390_timer, size); | |
10562 | } | |
10563 | ||
10564 | char * | |
10565 | elfcore_write_s390_todcmp (bfd *abfd, | |
10566 | char *buf, | |
10567 | int *bufsiz, | |
10568 | const void *s390_todcmp, | |
10569 | int size) | |
10570 | { | |
10571 | char *note_name = "LINUX"; | |
10572 | return elfcore_write_note (abfd, buf, bufsiz, | |
10573 | note_name, NT_S390_TODCMP, s390_todcmp, size); | |
10574 | } | |
10575 | ||
10576 | char * | |
10577 | elfcore_write_s390_todpreg (bfd *abfd, | |
10578 | char *buf, | |
10579 | int *bufsiz, | |
10580 | const void *s390_todpreg, | |
10581 | int size) | |
10582 | { | |
10583 | char *note_name = "LINUX"; | |
10584 | return elfcore_write_note (abfd, buf, bufsiz, | |
10585 | note_name, NT_S390_TODPREG, s390_todpreg, size); | |
10586 | } | |
10587 | ||
10588 | char * | |
10589 | elfcore_write_s390_ctrs (bfd *abfd, | |
10590 | char *buf, | |
10591 | int *bufsiz, | |
10592 | const void *s390_ctrs, | |
10593 | int size) | |
10594 | { | |
10595 | char *note_name = "LINUX"; | |
10596 | return elfcore_write_note (abfd, buf, bufsiz, | |
10597 | note_name, NT_S390_CTRS, s390_ctrs, size); | |
10598 | } | |
10599 | ||
10600 | char * | |
10601 | elfcore_write_s390_prefix (bfd *abfd, | |
10602 | char *buf, | |
10603 | int *bufsiz, | |
10604 | const void *s390_prefix, | |
10605 | int size) | |
10606 | { | |
10607 | char *note_name = "LINUX"; | |
10608 | return elfcore_write_note (abfd, buf, bufsiz, | |
10609 | note_name, NT_S390_PREFIX, s390_prefix, size); | |
10610 | } | |
10611 | ||
355b81d9 UW |
10612 | char * |
10613 | elfcore_write_s390_last_break (bfd *abfd, | |
10614 | char *buf, | |
10615 | int *bufsiz, | |
10616 | const void *s390_last_break, | |
10617 | int size) | |
10618 | { | |
10619 | char *note_name = "LINUX"; | |
10620 | return elfcore_write_note (abfd, buf, bufsiz, | |
10621 | note_name, NT_S390_LAST_BREAK, | |
10622 | s390_last_break, size); | |
10623 | } | |
10624 | ||
10625 | char * | |
10626 | elfcore_write_s390_system_call (bfd *abfd, | |
10627 | char *buf, | |
10628 | int *bufsiz, | |
10629 | const void *s390_system_call, | |
10630 | int size) | |
10631 | { | |
10632 | char *note_name = "LINUX"; | |
10633 | return elfcore_write_note (abfd, buf, bufsiz, | |
10634 | note_name, NT_S390_SYSTEM_CALL, | |
10635 | s390_system_call, size); | |
10636 | } | |
10637 | ||
abb3f6cc NC |
10638 | char * |
10639 | elfcore_write_s390_tdb (bfd *abfd, | |
10640 | char *buf, | |
10641 | int *bufsiz, | |
10642 | const void *s390_tdb, | |
10643 | int size) | |
10644 | { | |
10645 | char *note_name = "LINUX"; | |
10646 | return elfcore_write_note (abfd, buf, bufsiz, | |
10647 | note_name, NT_S390_TDB, s390_tdb, size); | |
10648 | } | |
10649 | ||
4ef9f41a AA |
10650 | char * |
10651 | elfcore_write_s390_vxrs_low (bfd *abfd, | |
10652 | char *buf, | |
10653 | int *bufsiz, | |
10654 | const void *s390_vxrs_low, | |
10655 | int size) | |
10656 | { | |
10657 | char *note_name = "LINUX"; | |
10658 | return elfcore_write_note (abfd, buf, bufsiz, | |
10659 | note_name, NT_S390_VXRS_LOW, s390_vxrs_low, size); | |
10660 | } | |
10661 | ||
10662 | char * | |
10663 | elfcore_write_s390_vxrs_high (bfd *abfd, | |
10664 | char *buf, | |
10665 | int *bufsiz, | |
10666 | const void *s390_vxrs_high, | |
10667 | int size) | |
10668 | { | |
10669 | char *note_name = "LINUX"; | |
10670 | return elfcore_write_note (abfd, buf, bufsiz, | |
10671 | note_name, NT_S390_VXRS_HIGH, | |
10672 | s390_vxrs_high, size); | |
10673 | } | |
10674 | ||
faa9a424 UW |
10675 | char * |
10676 | elfcore_write_arm_vfp (bfd *abfd, | |
10677 | char *buf, | |
10678 | int *bufsiz, | |
10679 | const void *arm_vfp, | |
10680 | int size) | |
10681 | { | |
10682 | char *note_name = "LINUX"; | |
10683 | return elfcore_write_note (abfd, buf, bufsiz, | |
10684 | note_name, NT_ARM_VFP, arm_vfp, size); | |
10685 | } | |
10686 | ||
652451f8 YZ |
10687 | char * |
10688 | elfcore_write_aarch_tls (bfd *abfd, | |
10689 | char *buf, | |
10690 | int *bufsiz, | |
10691 | const void *aarch_tls, | |
10692 | int size) | |
10693 | { | |
10694 | char *note_name = "LINUX"; | |
10695 | return elfcore_write_note (abfd, buf, bufsiz, | |
10696 | note_name, NT_ARM_TLS, aarch_tls, size); | |
10697 | } | |
10698 | ||
10699 | char * | |
10700 | elfcore_write_aarch_hw_break (bfd *abfd, | |
10701 | char *buf, | |
10702 | int *bufsiz, | |
10703 | const void *aarch_hw_break, | |
10704 | int size) | |
10705 | { | |
10706 | char *note_name = "LINUX"; | |
10707 | return elfcore_write_note (abfd, buf, bufsiz, | |
10708 | note_name, NT_ARM_HW_BREAK, aarch_hw_break, size); | |
10709 | } | |
10710 | ||
10711 | char * | |
10712 | elfcore_write_aarch_hw_watch (bfd *abfd, | |
10713 | char *buf, | |
10714 | int *bufsiz, | |
10715 | const void *aarch_hw_watch, | |
10716 | int size) | |
10717 | { | |
10718 | char *note_name = "LINUX"; | |
10719 | return elfcore_write_note (abfd, buf, bufsiz, | |
10720 | note_name, NT_ARM_HW_WATCH, aarch_hw_watch, size); | |
10721 | } | |
10722 | ||
bb864ac1 CES |
10723 | char * |
10724 | elfcore_write_register_note (bfd *abfd, | |
10725 | char *buf, | |
10726 | int *bufsiz, | |
10727 | const char *section, | |
10728 | const void *data, | |
10729 | int size) | |
10730 | { | |
10731 | if (strcmp (section, ".reg2") == 0) | |
10732 | return elfcore_write_prfpreg (abfd, buf, bufsiz, data, size); | |
10733 | if (strcmp (section, ".reg-xfp") == 0) | |
10734 | return elfcore_write_prxfpreg (abfd, buf, bufsiz, data, size); | |
4339cae0 L |
10735 | if (strcmp (section, ".reg-xstate") == 0) |
10736 | return elfcore_write_xstatereg (abfd, buf, bufsiz, data, size); | |
bb864ac1 CES |
10737 | if (strcmp (section, ".reg-ppc-vmx") == 0) |
10738 | return elfcore_write_ppc_vmx (abfd, buf, bufsiz, data, size); | |
89eeb0bc LM |
10739 | if (strcmp (section, ".reg-ppc-vsx") == 0) |
10740 | return elfcore_write_ppc_vsx (abfd, buf, bufsiz, data, size); | |
0675e188 UW |
10741 | if (strcmp (section, ".reg-s390-high-gprs") == 0) |
10742 | return elfcore_write_s390_high_gprs (abfd, buf, bufsiz, data, size); | |
d7eeb400 MS |
10743 | if (strcmp (section, ".reg-s390-timer") == 0) |
10744 | return elfcore_write_s390_timer (abfd, buf, bufsiz, data, size); | |
10745 | if (strcmp (section, ".reg-s390-todcmp") == 0) | |
10746 | return elfcore_write_s390_todcmp (abfd, buf, bufsiz, data, size); | |
10747 | if (strcmp (section, ".reg-s390-todpreg") == 0) | |
10748 | return elfcore_write_s390_todpreg (abfd, buf, bufsiz, data, size); | |
10749 | if (strcmp (section, ".reg-s390-ctrs") == 0) | |
10750 | return elfcore_write_s390_ctrs (abfd, buf, bufsiz, data, size); | |
10751 | if (strcmp (section, ".reg-s390-prefix") == 0) | |
10752 | return elfcore_write_s390_prefix (abfd, buf, bufsiz, data, size); | |
355b81d9 UW |
10753 | if (strcmp (section, ".reg-s390-last-break") == 0) |
10754 | return elfcore_write_s390_last_break (abfd, buf, bufsiz, data, size); | |
10755 | if (strcmp (section, ".reg-s390-system-call") == 0) | |
10756 | return elfcore_write_s390_system_call (abfd, buf, bufsiz, data, size); | |
abb3f6cc NC |
10757 | if (strcmp (section, ".reg-s390-tdb") == 0) |
10758 | return elfcore_write_s390_tdb (abfd, buf, bufsiz, data, size); | |
4ef9f41a AA |
10759 | if (strcmp (section, ".reg-s390-vxrs-low") == 0) |
10760 | return elfcore_write_s390_vxrs_low (abfd, buf, bufsiz, data, size); | |
10761 | if (strcmp (section, ".reg-s390-vxrs-high") == 0) | |
10762 | return elfcore_write_s390_vxrs_high (abfd, buf, bufsiz, data, size); | |
faa9a424 UW |
10763 | if (strcmp (section, ".reg-arm-vfp") == 0) |
10764 | return elfcore_write_arm_vfp (abfd, buf, bufsiz, data, size); | |
652451f8 YZ |
10765 | if (strcmp (section, ".reg-aarch-tls") == 0) |
10766 | return elfcore_write_aarch_tls (abfd, buf, bufsiz, data, size); | |
10767 | if (strcmp (section, ".reg-aarch-hw-break") == 0) | |
10768 | return elfcore_write_aarch_hw_break (abfd, buf, bufsiz, data, size); | |
10769 | if (strcmp (section, ".reg-aarch-hw-watch") == 0) | |
10770 | return elfcore_write_aarch_hw_watch (abfd, buf, bufsiz, data, size); | |
bb864ac1 CES |
10771 | return NULL; |
10772 | } | |
10773 | ||
b34976b6 | 10774 | static bfd_boolean |
718175fa | 10775 | elf_parse_notes (bfd *abfd, char *buf, size_t size, file_ptr offset) |
252b5132 | 10776 | { |
c044fabd | 10777 | char *p; |
252b5132 | 10778 | |
252b5132 RH |
10779 | p = buf; |
10780 | while (p < buf + size) | |
10781 | { | |
c044fabd KH |
10782 | /* FIXME: bad alignment assumption. */ |
10783 | Elf_External_Note *xnp = (Elf_External_Note *) p; | |
252b5132 RH |
10784 | Elf_Internal_Note in; |
10785 | ||
baea7ef1 AM |
10786 | if (offsetof (Elf_External_Note, name) > buf - p + size) |
10787 | return FALSE; | |
10788 | ||
dc810e39 | 10789 | in.type = H_GET_32 (abfd, xnp->type); |
252b5132 | 10790 | |
dc810e39 | 10791 | in.namesz = H_GET_32 (abfd, xnp->namesz); |
252b5132 | 10792 | in.namedata = xnp->name; |
baea7ef1 AM |
10793 | if (in.namesz > buf - in.namedata + size) |
10794 | return FALSE; | |
252b5132 | 10795 | |
dc810e39 | 10796 | in.descsz = H_GET_32 (abfd, xnp->descsz); |
252b5132 RH |
10797 | in.descdata = in.namedata + BFD_ALIGN (in.namesz, 4); |
10798 | in.descpos = offset + (in.descdata - buf); | |
baea7ef1 AM |
10799 | if (in.descsz != 0 |
10800 | && (in.descdata >= buf + size | |
10801 | || in.descsz > buf - in.descdata + size)) | |
10802 | return FALSE; | |
252b5132 | 10803 | |
718175fa JK |
10804 | switch (bfd_get_format (abfd)) |
10805 | { | |
10806 | default: | |
10807 | return TRUE; | |
10808 | ||
10809 | case bfd_core: | |
f64e188b | 10810 | { |
8acbedd6 | 10811 | #define GROKER_ELEMENT(S,F) {S, sizeof (S) - 1, F} |
f64e188b | 10812 | struct |
718175fa | 10813 | { |
f64e188b | 10814 | const char * string; |
8acbedd6 | 10815 | size_t len; |
f64e188b | 10816 | bfd_boolean (* func)(bfd *, Elf_Internal_Note *); |
718175fa | 10817 | } |
f64e188b | 10818 | grokers[] = |
b15fa79e | 10819 | { |
8acbedd6 | 10820 | GROKER_ELEMENT ("", elfcore_grok_note), |
aa1ed4a9 | 10821 | GROKER_ELEMENT ("FreeBSD", elfcore_grok_freebsd_note), |
8acbedd6 KS |
10822 | GROKER_ELEMENT ("NetBSD-CORE", elfcore_grok_netbsd_note), |
10823 | GROKER_ELEMENT ( "OpenBSD", elfcore_grok_openbsd_note), | |
10824 | GROKER_ELEMENT ("QNX", elfcore_grok_nto_note), | |
10825 | GROKER_ELEMENT ("SPU/", elfcore_grok_spu_note) | |
f64e188b | 10826 | }; |
8acbedd6 | 10827 | #undef GROKER_ELEMENT |
f64e188b NC |
10828 | int i; |
10829 | ||
10830 | for (i = ARRAY_SIZE (grokers); i--;) | |
8acbedd6 KS |
10831 | { |
10832 | if (in.namesz >= grokers[i].len | |
10833 | && strncmp (in.namedata, grokers[i].string, | |
10834 | grokers[i].len) == 0) | |
10835 | { | |
10836 | if (! grokers[i].func (abfd, & in)) | |
10837 | return FALSE; | |
10838 | break; | |
10839 | } | |
10840 | } | |
f64e188b NC |
10841 | break; |
10842 | } | |
718175fa JK |
10843 | |
10844 | case bfd_object: | |
10845 | if (in.namesz == sizeof "GNU" && strcmp (in.namedata, "GNU") == 0) | |
10846 | { | |
10847 | if (! elfobj_grok_gnu_note (abfd, &in)) | |
10848 | return FALSE; | |
10849 | } | |
e21e5835 NC |
10850 | else if (in.namesz == sizeof "stapsdt" |
10851 | && strcmp (in.namedata, "stapsdt") == 0) | |
10852 | { | |
10853 | if (! elfobj_grok_stapsdt_note (abfd, &in)) | |
10854 | return FALSE; | |
10855 | } | |
718175fa | 10856 | break; |
08a40648 | 10857 | } |
252b5132 RH |
10858 | |
10859 | p = in.descdata + BFD_ALIGN (in.descsz, 4); | |
10860 | } | |
10861 | ||
718175fa JK |
10862 | return TRUE; |
10863 | } | |
10864 | ||
10865 | static bfd_boolean | |
10866 | elf_read_notes (bfd *abfd, file_ptr offset, bfd_size_type size) | |
10867 | { | |
10868 | char *buf; | |
10869 | ||
10870 | if (size <= 0) | |
10871 | return TRUE; | |
10872 | ||
10873 | if (bfd_seek (abfd, offset, SEEK_SET) != 0) | |
10874 | return FALSE; | |
10875 | ||
f64e188b | 10876 | buf = (char *) bfd_malloc (size + 1); |
718175fa JK |
10877 | if (buf == NULL) |
10878 | return FALSE; | |
10879 | ||
f64e188b NC |
10880 | /* PR 17512: file: ec08f814 |
10881 | 0-termintate the buffer so that string searches will not overflow. */ | |
10882 | buf[size] = 0; | |
10883 | ||
718175fa JK |
10884 | if (bfd_bread (buf, size, abfd) != size |
10885 | || !elf_parse_notes (abfd, buf, size, offset)) | |
10886 | { | |
10887 | free (buf); | |
10888 | return FALSE; | |
10889 | } | |
10890 | ||
252b5132 | 10891 | free (buf); |
b34976b6 | 10892 | return TRUE; |
252b5132 | 10893 | } |
98d8431c JB |
10894 | \f |
10895 | /* Providing external access to the ELF program header table. */ | |
10896 | ||
10897 | /* Return an upper bound on the number of bytes required to store a | |
10898 | copy of ABFD's program header table entries. Return -1 if an error | |
10899 | occurs; bfd_get_error will return an appropriate code. */ | |
c044fabd | 10900 | |
98d8431c | 10901 | long |
217aa764 | 10902 | bfd_get_elf_phdr_upper_bound (bfd *abfd) |
98d8431c JB |
10903 | { |
10904 | if (abfd->xvec->flavour != bfd_target_elf_flavour) | |
10905 | { | |
10906 | bfd_set_error (bfd_error_wrong_format); | |
10907 | return -1; | |
10908 | } | |
10909 | ||
936e320b | 10910 | return elf_elfheader (abfd)->e_phnum * sizeof (Elf_Internal_Phdr); |
98d8431c JB |
10911 | } |
10912 | ||
98d8431c JB |
10913 | /* Copy ABFD's program header table entries to *PHDRS. The entries |
10914 | will be stored as an array of Elf_Internal_Phdr structures, as | |
10915 | defined in include/elf/internal.h. To find out how large the | |
10916 | buffer needs to be, call bfd_get_elf_phdr_upper_bound. | |
10917 | ||
10918 | Return the number of program header table entries read, or -1 if an | |
10919 | error occurs; bfd_get_error will return an appropriate code. */ | |
c044fabd | 10920 | |
98d8431c | 10921 | int |
217aa764 | 10922 | bfd_get_elf_phdrs (bfd *abfd, void *phdrs) |
98d8431c JB |
10923 | { |
10924 | int num_phdrs; | |
10925 | ||
10926 | if (abfd->xvec->flavour != bfd_target_elf_flavour) | |
10927 | { | |
10928 | bfd_set_error (bfd_error_wrong_format); | |
10929 | return -1; | |
10930 | } | |
10931 | ||
10932 | num_phdrs = elf_elfheader (abfd)->e_phnum; | |
c044fabd | 10933 | memcpy (phdrs, elf_tdata (abfd)->phdr, |
98d8431c JB |
10934 | num_phdrs * sizeof (Elf_Internal_Phdr)); |
10935 | ||
10936 | return num_phdrs; | |
10937 | } | |
ae4221d7 | 10938 | |
db6751f2 | 10939 | enum elf_reloc_type_class |
7e612e98 AM |
10940 | _bfd_elf_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED, |
10941 | const asection *rel_sec ATTRIBUTE_UNUSED, | |
10942 | const Elf_Internal_Rela *rela ATTRIBUTE_UNUSED) | |
db6751f2 JJ |
10943 | { |
10944 | return reloc_class_normal; | |
10945 | } | |
f8df10f4 | 10946 | |
47d9a591 | 10947 | /* For RELA architectures, return the relocation value for a |
f8df10f4 JJ |
10948 | relocation against a local symbol. */ |
10949 | ||
10950 | bfd_vma | |
217aa764 AM |
10951 | _bfd_elf_rela_local_sym (bfd *abfd, |
10952 | Elf_Internal_Sym *sym, | |
8517fae7 | 10953 | asection **psec, |
217aa764 | 10954 | Elf_Internal_Rela *rel) |
f8df10f4 | 10955 | { |
8517fae7 | 10956 | asection *sec = *psec; |
f8df10f4 JJ |
10957 | bfd_vma relocation; |
10958 | ||
10959 | relocation = (sec->output_section->vma | |
10960 | + sec->output_offset | |
10961 | + sym->st_value); | |
10962 | if ((sec->flags & SEC_MERGE) | |
c629eae0 | 10963 | && ELF_ST_TYPE (sym->st_info) == STT_SECTION |
dbaa2011 | 10964 | && sec->sec_info_type == SEC_INFO_TYPE_MERGE) |
f8df10f4 | 10965 | { |
f8df10f4 | 10966 | rel->r_addend = |
8517fae7 | 10967 | _bfd_merged_section_offset (abfd, psec, |
65765700 | 10968 | elf_section_data (sec)->sec_info, |
753731ee AM |
10969 | sym->st_value + rel->r_addend); |
10970 | if (sec != *psec) | |
10971 | { | |
10972 | /* If we have changed the section, and our original section is | |
10973 | marked with SEC_EXCLUDE, it means that the original | |
10974 | SEC_MERGE section has been completely subsumed in some | |
10975 | other SEC_MERGE section. In this case, we need to leave | |
10976 | some info around for --emit-relocs. */ | |
10977 | if ((sec->flags & SEC_EXCLUDE) != 0) | |
10978 | sec->kept_section = *psec; | |
10979 | sec = *psec; | |
10980 | } | |
8517fae7 AM |
10981 | rel->r_addend -= relocation; |
10982 | rel->r_addend += sec->output_section->vma + sec->output_offset; | |
f8df10f4 JJ |
10983 | } |
10984 | return relocation; | |
10985 | } | |
c629eae0 JJ |
10986 | |
10987 | bfd_vma | |
217aa764 AM |
10988 | _bfd_elf_rel_local_sym (bfd *abfd, |
10989 | Elf_Internal_Sym *sym, | |
10990 | asection **psec, | |
10991 | bfd_vma addend) | |
47d9a591 | 10992 | { |
c629eae0 JJ |
10993 | asection *sec = *psec; |
10994 | ||
dbaa2011 | 10995 | if (sec->sec_info_type != SEC_INFO_TYPE_MERGE) |
c629eae0 JJ |
10996 | return sym->st_value + addend; |
10997 | ||
10998 | return _bfd_merged_section_offset (abfd, psec, | |
65765700 | 10999 | elf_section_data (sec)->sec_info, |
753731ee | 11000 | sym->st_value + addend); |
c629eae0 JJ |
11001 | } |
11002 | ||
37b01f6a DG |
11003 | /* Adjust an address within a section. Given OFFSET within SEC, return |
11004 | the new offset within the section, based upon changes made to the | |
11005 | section. Returns -1 if the offset is now invalid. | |
11006 | The offset (in abnd out) is in target sized bytes, however big a | |
11007 | byte may be. */ | |
11008 | ||
c629eae0 | 11009 | bfd_vma |
217aa764 | 11010 | _bfd_elf_section_offset (bfd *abfd, |
92e4ec35 | 11011 | struct bfd_link_info *info, |
217aa764 AM |
11012 | asection *sec, |
11013 | bfd_vma offset) | |
c629eae0 | 11014 | { |
68bfbfcc | 11015 | switch (sec->sec_info_type) |
65765700 | 11016 | { |
dbaa2011 | 11017 | case SEC_INFO_TYPE_STABS: |
eea6121a AM |
11018 | return _bfd_stab_section_offset (sec, elf_section_data (sec)->sec_info, |
11019 | offset); | |
dbaa2011 | 11020 | case SEC_INFO_TYPE_EH_FRAME: |
92e4ec35 | 11021 | return _bfd_elf_eh_frame_section_offset (abfd, info, sec, offset); |
37b01f6a | 11022 | |
65765700 | 11023 | default: |
310fd250 L |
11024 | if ((sec->flags & SEC_ELF_REVERSE_COPY) != 0) |
11025 | { | |
37b01f6a | 11026 | /* Reverse the offset. */ |
310fd250 L |
11027 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
11028 | bfd_size_type address_size = bed->s->arch_size / 8; | |
37b01f6a DG |
11029 | |
11030 | /* address_size and sec->size are in octets. Convert | |
11031 | to bytes before subtracting the original offset. */ | |
11032 | offset = (sec->size - address_size) / bfd_octets_per_byte (abfd) - offset; | |
310fd250 | 11033 | } |
65765700 JJ |
11034 | return offset; |
11035 | } | |
c629eae0 | 11036 | } |
3333a7c3 RM |
11037 | \f |
11038 | /* Create a new BFD as if by bfd_openr. Rather than opening a file, | |
11039 | reconstruct an ELF file by reading the segments out of remote memory | |
11040 | based on the ELF file header at EHDR_VMA and the ELF program headers it | |
11041 | points to. If not null, *LOADBASEP is filled in with the difference | |
11042 | between the VMAs from which the segments were read, and the VMAs the | |
11043 | file headers (and hence BFD's idea of each section's VMA) put them at. | |
11044 | ||
11045 | The function TARGET_READ_MEMORY is called to copy LEN bytes from the | |
11046 | remote memory at target address VMA into the local buffer at MYADDR; it | |
11047 | should return zero on success or an `errno' code on failure. TEMPL must | |
11048 | be a BFD for an ELF target with the word size and byte order found in | |
11049 | the remote memory. */ | |
11050 | ||
11051 | bfd * | |
217aa764 AM |
11052 | bfd_elf_bfd_from_remote_memory |
11053 | (bfd *templ, | |
11054 | bfd_vma ehdr_vma, | |
f0a5d95a | 11055 | bfd_size_type size, |
217aa764 | 11056 | bfd_vma *loadbasep, |
fe78531d | 11057 | int (*target_read_memory) (bfd_vma, bfd_byte *, bfd_size_type)) |
3333a7c3 RM |
11058 | { |
11059 | return (*get_elf_backend_data (templ)->elf_backend_bfd_from_remote_memory) | |
5979d6b6 | 11060 | (templ, ehdr_vma, size, loadbasep, target_read_memory); |
3333a7c3 | 11061 | } |
4c45e5c9 JJ |
11062 | \f |
11063 | long | |
c9727e01 AM |
11064 | _bfd_elf_get_synthetic_symtab (bfd *abfd, |
11065 | long symcount ATTRIBUTE_UNUSED, | |
11066 | asymbol **syms ATTRIBUTE_UNUSED, | |
8615f3f2 | 11067 | long dynsymcount, |
c9727e01 AM |
11068 | asymbol **dynsyms, |
11069 | asymbol **ret) | |
4c45e5c9 JJ |
11070 | { |
11071 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
11072 | asection *relplt; | |
11073 | asymbol *s; | |
11074 | const char *relplt_name; | |
11075 | bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean); | |
11076 | arelent *p; | |
11077 | long count, i, n; | |
11078 | size_t size; | |
11079 | Elf_Internal_Shdr *hdr; | |
11080 | char *names; | |
11081 | asection *plt; | |
11082 | ||
8615f3f2 AM |
11083 | *ret = NULL; |
11084 | ||
90e3cdf2 JJ |
11085 | if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0) |
11086 | return 0; | |
11087 | ||
8615f3f2 AM |
11088 | if (dynsymcount <= 0) |
11089 | return 0; | |
11090 | ||
4c45e5c9 JJ |
11091 | if (!bed->plt_sym_val) |
11092 | return 0; | |
11093 | ||
11094 | relplt_name = bed->relplt_name; | |
11095 | if (relplt_name == NULL) | |
d35fd659 | 11096 | relplt_name = bed->rela_plts_and_copies_p ? ".rela.plt" : ".rel.plt"; |
4c45e5c9 JJ |
11097 | relplt = bfd_get_section_by_name (abfd, relplt_name); |
11098 | if (relplt == NULL) | |
11099 | return 0; | |
11100 | ||
11101 | hdr = &elf_section_data (relplt)->this_hdr; | |
11102 | if (hdr->sh_link != elf_dynsymtab (abfd) | |
11103 | || (hdr->sh_type != SHT_REL && hdr->sh_type != SHT_RELA)) | |
11104 | return 0; | |
11105 | ||
11106 | plt = bfd_get_section_by_name (abfd, ".plt"); | |
11107 | if (plt == NULL) | |
11108 | return 0; | |
11109 | ||
11110 | slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table; | |
c9727e01 | 11111 | if (! (*slurp_relocs) (abfd, relplt, dynsyms, TRUE)) |
4c45e5c9 JJ |
11112 | return -1; |
11113 | ||
eea6121a | 11114 | count = relplt->size / hdr->sh_entsize; |
4c45e5c9 JJ |
11115 | size = count * sizeof (asymbol); |
11116 | p = relplt->relocation; | |
cb53bf42 | 11117 | for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel) |
041de40d AM |
11118 | { |
11119 | size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt"); | |
11120 | if (p->addend != 0) | |
11121 | { | |
11122 | #ifdef BFD64 | |
11123 | size += sizeof ("+0x") - 1 + 8 + 8 * (bed->s->elfclass == ELFCLASS64); | |
11124 | #else | |
11125 | size += sizeof ("+0x") - 1 + 8; | |
11126 | #endif | |
11127 | } | |
11128 | } | |
4c45e5c9 | 11129 | |
a50b1753 | 11130 | s = *ret = (asymbol *) bfd_malloc (size); |
4c45e5c9 JJ |
11131 | if (s == NULL) |
11132 | return -1; | |
11133 | ||
11134 | names = (char *) (s + count); | |
11135 | p = relplt->relocation; | |
11136 | n = 0; | |
cb53bf42 | 11137 | for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel) |
4c45e5c9 JJ |
11138 | { |
11139 | size_t len; | |
11140 | bfd_vma addr; | |
11141 | ||
11142 | addr = bed->plt_sym_val (i, plt, p); | |
11143 | if (addr == (bfd_vma) -1) | |
11144 | continue; | |
11145 | ||
11146 | *s = **p->sym_ptr_ptr; | |
65a7a66f AM |
11147 | /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since |
11148 | we are defining a symbol, ensure one of them is set. */ | |
11149 | if ((s->flags & BSF_LOCAL) == 0) | |
11150 | s->flags |= BSF_GLOBAL; | |
6ba2a415 | 11151 | s->flags |= BSF_SYNTHETIC; |
4c45e5c9 JJ |
11152 | s->section = plt; |
11153 | s->value = addr - plt->vma; | |
11154 | s->name = names; | |
8f39ba8e | 11155 | s->udata.p = NULL; |
4c45e5c9 JJ |
11156 | len = strlen ((*p->sym_ptr_ptr)->name); |
11157 | memcpy (names, (*p->sym_ptr_ptr)->name, len); | |
11158 | names += len; | |
041de40d AM |
11159 | if (p->addend != 0) |
11160 | { | |
1d770845 | 11161 | char buf[30], *a; |
d324f6d6 | 11162 | |
041de40d AM |
11163 | memcpy (names, "+0x", sizeof ("+0x") - 1); |
11164 | names += sizeof ("+0x") - 1; | |
1d770845 L |
11165 | bfd_sprintf_vma (abfd, buf, p->addend); |
11166 | for (a = buf; *a == '0'; ++a) | |
11167 | ; | |
11168 | len = strlen (a); | |
11169 | memcpy (names, a, len); | |
11170 | names += len; | |
041de40d | 11171 | } |
4c45e5c9 JJ |
11172 | memcpy (names, "@plt", sizeof ("@plt")); |
11173 | names += sizeof ("@plt"); | |
8f39ba8e | 11174 | ++s, ++n; |
4c45e5c9 JJ |
11175 | } |
11176 | ||
11177 | return n; | |
11178 | } | |
3d7f7666 | 11179 | |
821e6ff6 AM |
11180 | /* It is only used by x86-64 so far. |
11181 | ??? This repeats *COM* id of zero. sec->id is supposed to be unique, | |
11182 | but current usage would allow all of _bfd_std_section to be zero. t*/ | |
3b22753a | 11183 | asection _bfd_elf_large_com_section |
821e6ff6 AM |
11184 | = BFD_FAKE_SECTION (_bfd_elf_large_com_section, NULL, |
11185 | "LARGE_COMMON", 0, SEC_IS_COMMON); | |
ecca9871 | 11186 | |
d1036acb | 11187 | void |
78245035 L |
11188 | _bfd_elf_post_process_headers (bfd * abfd, |
11189 | struct bfd_link_info * link_info ATTRIBUTE_UNUSED) | |
d1036acb L |
11190 | { |
11191 | Elf_Internal_Ehdr * i_ehdrp; /* ELF file header, internal form. */ | |
11192 | ||
11193 | i_ehdrp = elf_elfheader (abfd); | |
11194 | ||
11195 | i_ehdrp->e_ident[EI_OSABI] = get_elf_backend_data (abfd)->elf_osabi; | |
d8045f23 NC |
11196 | |
11197 | /* To make things simpler for the loader on Linux systems we set the | |
9c55345c | 11198 | osabi field to ELFOSABI_GNU if the binary contains symbols of |
f64b2e8d | 11199 | the STT_GNU_IFUNC type or STB_GNU_UNIQUE binding. */ |
d8045f23 | 11200 | if (i_ehdrp->e_ident[EI_OSABI] == ELFOSABI_NONE |
f64b2e8d | 11201 | && elf_tdata (abfd)->has_gnu_symbols) |
9c55345c | 11202 | i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_GNU; |
d1036acb | 11203 | } |
fcb93ecf PB |
11204 | |
11205 | ||
11206 | /* Return TRUE for ELF symbol types that represent functions. | |
11207 | This is the default version of this function, which is sufficient for | |
d8045f23 | 11208 | most targets. It returns true if TYPE is STT_FUNC or STT_GNU_IFUNC. */ |
fcb93ecf PB |
11209 | |
11210 | bfd_boolean | |
11211 | _bfd_elf_is_function_type (unsigned int type) | |
11212 | { | |
d8045f23 NC |
11213 | return (type == STT_FUNC |
11214 | || type == STT_GNU_IFUNC); | |
fcb93ecf | 11215 | } |
9f296da3 | 11216 | |
aef36ac1 AM |
11217 | /* If the ELF symbol SYM might be a function in SEC, return the |
11218 | function size and set *CODE_OFF to the function's entry point, | |
11219 | otherwise return zero. */ | |
9f296da3 | 11220 | |
aef36ac1 AM |
11221 | bfd_size_type |
11222 | _bfd_elf_maybe_function_sym (const asymbol *sym, asection *sec, | |
11223 | bfd_vma *code_off) | |
9f296da3 | 11224 | { |
aef36ac1 AM |
11225 | bfd_size_type size; |
11226 | ||
ff9e0f5b | 11227 | if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT |
aef36ac1 AM |
11228 | | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0 |
11229 | || sym->section != sec) | |
11230 | return 0; | |
ff9e0f5b | 11231 | |
ff9e0f5b | 11232 | *code_off = sym->value; |
aef36ac1 AM |
11233 | size = 0; |
11234 | if (!(sym->flags & BSF_SYNTHETIC)) | |
11235 | size = ((elf_symbol_type *) sym)->internal_elf_sym.st_size; | |
11236 | if (size == 0) | |
11237 | size = 1; | |
11238 | return size; | |
9f296da3 | 11239 | } |