Commit | Line | Data |
---|---|---|
252b5132 | 1 | /* ELF executable support for BFD. |
340b6d91 | 2 | |
6f2750fe | 3 | Copyright (C) 1993-2016 Free Software Foundation, Inc. |
252b5132 | 4 | |
5e8d7549 | 5 | This file is part of BFD, the Binary File Descriptor library. |
252b5132 | 6 | |
5e8d7549 NC |
7 | This program is free software; you can redistribute it and/or modify |
8 | it under the terms of the GNU General Public License as published by | |
cd123cb7 | 9 | the Free Software Foundation; either version 3 of the License, or |
5e8d7549 | 10 | (at your option) any later version. |
252b5132 | 11 | |
5e8d7549 NC |
12 | This program is distributed in the hope that it will be useful, |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
252b5132 | 16 | |
5e8d7549 | 17 | You should have received a copy of the GNU General Public License |
b34976b6 | 18 | along with this program; if not, write to the Free Software |
cd123cb7 NC |
19 | Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, |
20 | MA 02110-1301, USA. */ | |
21 | ||
252b5132 | 22 | |
1b74d094 BW |
23 | /* |
24 | SECTION | |
252b5132 RH |
25 | ELF backends |
26 | ||
27 | BFD support for ELF formats is being worked on. | |
28 | Currently, the best supported back ends are for sparc and i386 | |
29 | (running svr4 or Solaris 2). | |
30 | ||
31 | Documentation of the internals of the support code still needs | |
32 | to be written. The code is changing quickly enough that we | |
661a3fd4 | 33 | haven't bothered yet. */ |
252b5132 | 34 | |
7ee38065 MS |
35 | /* For sparc64-cross-sparc32. */ |
36 | #define _SYSCALL32 | |
252b5132 | 37 | #include "sysdep.h" |
3db64b00 | 38 | #include "bfd.h" |
252b5132 RH |
39 | #include "bfdlink.h" |
40 | #include "libbfd.h" | |
41 | #define ARCH_SIZE 0 | |
42 | #include "elf-bfd.h" | |
e0e8c97f | 43 | #include "libiberty.h" |
ff59fc36 | 44 | #include "safe-ctype.h" |
de64ce13 | 45 | #include "elf-linux-core.h" |
252b5132 | 46 | |
8bc7f138 L |
47 | #ifdef CORE_HEADER |
48 | #include CORE_HEADER | |
49 | #endif | |
50 | ||
217aa764 | 51 | static int elf_sort_sections (const void *, const void *); |
c84fca4d | 52 | static bfd_boolean assign_file_positions_except_relocs (bfd *, struct bfd_link_info *); |
217aa764 | 53 | static bfd_boolean prep_headers (bfd *); |
ef10c3ac | 54 | static bfd_boolean swap_out_syms (bfd *, struct elf_strtab_hash **, int) ; |
718175fa JK |
55 | static bfd_boolean elf_read_notes (bfd *, file_ptr, bfd_size_type) ; |
56 | static bfd_boolean elf_parse_notes (bfd *abfd, char *buf, size_t size, | |
57 | file_ptr offset); | |
50b2bdb7 | 58 | |
252b5132 RH |
59 | /* Swap version information in and out. The version information is |
60 | currently size independent. If that ever changes, this code will | |
61 | need to move into elfcode.h. */ | |
62 | ||
63 | /* Swap in a Verdef structure. */ | |
64 | ||
65 | void | |
217aa764 AM |
66 | _bfd_elf_swap_verdef_in (bfd *abfd, |
67 | const Elf_External_Verdef *src, | |
68 | Elf_Internal_Verdef *dst) | |
252b5132 | 69 | { |
dc810e39 AM |
70 | dst->vd_version = H_GET_16 (abfd, src->vd_version); |
71 | dst->vd_flags = H_GET_16 (abfd, src->vd_flags); | |
72 | dst->vd_ndx = H_GET_16 (abfd, src->vd_ndx); | |
73 | dst->vd_cnt = H_GET_16 (abfd, src->vd_cnt); | |
74 | dst->vd_hash = H_GET_32 (abfd, src->vd_hash); | |
75 | dst->vd_aux = H_GET_32 (abfd, src->vd_aux); | |
76 | dst->vd_next = H_GET_32 (abfd, src->vd_next); | |
252b5132 RH |
77 | } |
78 | ||
79 | /* Swap out a Verdef structure. */ | |
80 | ||
81 | void | |
217aa764 AM |
82 | _bfd_elf_swap_verdef_out (bfd *abfd, |
83 | const Elf_Internal_Verdef *src, | |
84 | Elf_External_Verdef *dst) | |
252b5132 | 85 | { |
dc810e39 AM |
86 | H_PUT_16 (abfd, src->vd_version, dst->vd_version); |
87 | H_PUT_16 (abfd, src->vd_flags, dst->vd_flags); | |
88 | H_PUT_16 (abfd, src->vd_ndx, dst->vd_ndx); | |
89 | H_PUT_16 (abfd, src->vd_cnt, dst->vd_cnt); | |
90 | H_PUT_32 (abfd, src->vd_hash, dst->vd_hash); | |
91 | H_PUT_32 (abfd, src->vd_aux, dst->vd_aux); | |
92 | H_PUT_32 (abfd, src->vd_next, dst->vd_next); | |
252b5132 RH |
93 | } |
94 | ||
95 | /* Swap in a Verdaux structure. */ | |
96 | ||
97 | void | |
217aa764 AM |
98 | _bfd_elf_swap_verdaux_in (bfd *abfd, |
99 | const Elf_External_Verdaux *src, | |
100 | Elf_Internal_Verdaux *dst) | |
252b5132 | 101 | { |
dc810e39 AM |
102 | dst->vda_name = H_GET_32 (abfd, src->vda_name); |
103 | dst->vda_next = H_GET_32 (abfd, src->vda_next); | |
252b5132 RH |
104 | } |
105 | ||
106 | /* Swap out a Verdaux structure. */ | |
107 | ||
108 | void | |
217aa764 AM |
109 | _bfd_elf_swap_verdaux_out (bfd *abfd, |
110 | const Elf_Internal_Verdaux *src, | |
111 | Elf_External_Verdaux *dst) | |
252b5132 | 112 | { |
dc810e39 AM |
113 | H_PUT_32 (abfd, src->vda_name, dst->vda_name); |
114 | H_PUT_32 (abfd, src->vda_next, dst->vda_next); | |
252b5132 RH |
115 | } |
116 | ||
117 | /* Swap in a Verneed structure. */ | |
118 | ||
119 | void | |
217aa764 AM |
120 | _bfd_elf_swap_verneed_in (bfd *abfd, |
121 | const Elf_External_Verneed *src, | |
122 | Elf_Internal_Verneed *dst) | |
252b5132 | 123 | { |
dc810e39 AM |
124 | dst->vn_version = H_GET_16 (abfd, src->vn_version); |
125 | dst->vn_cnt = H_GET_16 (abfd, src->vn_cnt); | |
126 | dst->vn_file = H_GET_32 (abfd, src->vn_file); | |
127 | dst->vn_aux = H_GET_32 (abfd, src->vn_aux); | |
128 | dst->vn_next = H_GET_32 (abfd, src->vn_next); | |
252b5132 RH |
129 | } |
130 | ||
131 | /* Swap out a Verneed structure. */ | |
132 | ||
133 | void | |
217aa764 AM |
134 | _bfd_elf_swap_verneed_out (bfd *abfd, |
135 | const Elf_Internal_Verneed *src, | |
136 | Elf_External_Verneed *dst) | |
252b5132 | 137 | { |
dc810e39 AM |
138 | H_PUT_16 (abfd, src->vn_version, dst->vn_version); |
139 | H_PUT_16 (abfd, src->vn_cnt, dst->vn_cnt); | |
140 | H_PUT_32 (abfd, src->vn_file, dst->vn_file); | |
141 | H_PUT_32 (abfd, src->vn_aux, dst->vn_aux); | |
142 | H_PUT_32 (abfd, src->vn_next, dst->vn_next); | |
252b5132 RH |
143 | } |
144 | ||
145 | /* Swap in a Vernaux structure. */ | |
146 | ||
147 | void | |
217aa764 AM |
148 | _bfd_elf_swap_vernaux_in (bfd *abfd, |
149 | const Elf_External_Vernaux *src, | |
150 | Elf_Internal_Vernaux *dst) | |
252b5132 | 151 | { |
dc810e39 AM |
152 | dst->vna_hash = H_GET_32 (abfd, src->vna_hash); |
153 | dst->vna_flags = H_GET_16 (abfd, src->vna_flags); | |
154 | dst->vna_other = H_GET_16 (abfd, src->vna_other); | |
155 | dst->vna_name = H_GET_32 (abfd, src->vna_name); | |
156 | dst->vna_next = H_GET_32 (abfd, src->vna_next); | |
252b5132 RH |
157 | } |
158 | ||
159 | /* Swap out a Vernaux structure. */ | |
160 | ||
161 | void | |
217aa764 AM |
162 | _bfd_elf_swap_vernaux_out (bfd *abfd, |
163 | const Elf_Internal_Vernaux *src, | |
164 | Elf_External_Vernaux *dst) | |
252b5132 | 165 | { |
dc810e39 AM |
166 | H_PUT_32 (abfd, src->vna_hash, dst->vna_hash); |
167 | H_PUT_16 (abfd, src->vna_flags, dst->vna_flags); | |
168 | H_PUT_16 (abfd, src->vna_other, dst->vna_other); | |
169 | H_PUT_32 (abfd, src->vna_name, dst->vna_name); | |
170 | H_PUT_32 (abfd, src->vna_next, dst->vna_next); | |
252b5132 RH |
171 | } |
172 | ||
173 | /* Swap in a Versym structure. */ | |
174 | ||
175 | void | |
217aa764 AM |
176 | _bfd_elf_swap_versym_in (bfd *abfd, |
177 | const Elf_External_Versym *src, | |
178 | Elf_Internal_Versym *dst) | |
252b5132 | 179 | { |
dc810e39 | 180 | dst->vs_vers = H_GET_16 (abfd, src->vs_vers); |
252b5132 RH |
181 | } |
182 | ||
183 | /* Swap out a Versym structure. */ | |
184 | ||
185 | void | |
217aa764 AM |
186 | _bfd_elf_swap_versym_out (bfd *abfd, |
187 | const Elf_Internal_Versym *src, | |
188 | Elf_External_Versym *dst) | |
252b5132 | 189 | { |
dc810e39 | 190 | H_PUT_16 (abfd, src->vs_vers, dst->vs_vers); |
252b5132 RH |
191 | } |
192 | ||
193 | /* Standard ELF hash function. Do not change this function; you will | |
194 | cause invalid hash tables to be generated. */ | |
3a99b017 | 195 | |
252b5132 | 196 | unsigned long |
217aa764 | 197 | bfd_elf_hash (const char *namearg) |
252b5132 | 198 | { |
3a99b017 | 199 | const unsigned char *name = (const unsigned char *) namearg; |
252b5132 RH |
200 | unsigned long h = 0; |
201 | unsigned long g; | |
202 | int ch; | |
203 | ||
204 | while ((ch = *name++) != '\0') | |
205 | { | |
206 | h = (h << 4) + ch; | |
207 | if ((g = (h & 0xf0000000)) != 0) | |
208 | { | |
209 | h ^= g >> 24; | |
210 | /* The ELF ABI says `h &= ~g', but this is equivalent in | |
211 | this case and on some machines one insn instead of two. */ | |
212 | h ^= g; | |
213 | } | |
214 | } | |
32dfa85d | 215 | return h & 0xffffffff; |
252b5132 RH |
216 | } |
217 | ||
fdc90cb4 JJ |
218 | /* DT_GNU_HASH hash function. Do not change this function; you will |
219 | cause invalid hash tables to be generated. */ | |
220 | ||
221 | unsigned long | |
222 | bfd_elf_gnu_hash (const char *namearg) | |
223 | { | |
224 | const unsigned char *name = (const unsigned char *) namearg; | |
225 | unsigned long h = 5381; | |
226 | unsigned char ch; | |
227 | ||
228 | while ((ch = *name++) != '\0') | |
229 | h = (h << 5) + h + ch; | |
230 | return h & 0xffffffff; | |
231 | } | |
232 | ||
0c8d6e5c AM |
233 | /* Create a tdata field OBJECT_SIZE bytes in length, zeroed out and with |
234 | the object_id field of an elf_obj_tdata field set to OBJECT_ID. */ | |
b34976b6 | 235 | bfd_boolean |
0c8d6e5c | 236 | bfd_elf_allocate_object (bfd *abfd, |
0ffa91dd | 237 | size_t object_size, |
4dfe6ac6 | 238 | enum elf_target_id object_id) |
252b5132 | 239 | { |
0ffa91dd NC |
240 | BFD_ASSERT (object_size >= sizeof (struct elf_obj_tdata)); |
241 | abfd->tdata.any = bfd_zalloc (abfd, object_size); | |
242 | if (abfd->tdata.any == NULL) | |
243 | return FALSE; | |
252b5132 | 244 | |
0ffa91dd | 245 | elf_object_id (abfd) = object_id; |
c0355132 AM |
246 | if (abfd->direction != read_direction) |
247 | { | |
248 | struct output_elf_obj_tdata *o = bfd_zalloc (abfd, sizeof *o); | |
249 | if (o == NULL) | |
250 | return FALSE; | |
251 | elf_tdata (abfd)->o = o; | |
252 | elf_program_header_size (abfd) = (bfd_size_type) -1; | |
253 | } | |
b34976b6 | 254 | return TRUE; |
252b5132 RH |
255 | } |
256 | ||
0ffa91dd NC |
257 | |
258 | bfd_boolean | |
ae95ffa6 | 259 | bfd_elf_make_object (bfd *abfd) |
0ffa91dd | 260 | { |
ae95ffa6 | 261 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
0ffa91dd | 262 | return bfd_elf_allocate_object (abfd, sizeof (struct elf_obj_tdata), |
ae95ffa6 | 263 | bed->target_id); |
0ffa91dd NC |
264 | } |
265 | ||
b34976b6 | 266 | bfd_boolean |
217aa764 | 267 | bfd_elf_mkcorefile (bfd *abfd) |
252b5132 | 268 | { |
c044fabd | 269 | /* I think this can be done just like an object file. */ |
228e534f AM |
270 | if (!abfd->xvec->_bfd_set_format[(int) bfd_object] (abfd)) |
271 | return FALSE; | |
272 | elf_tdata (abfd)->core = bfd_zalloc (abfd, sizeof (*elf_tdata (abfd)->core)); | |
273 | return elf_tdata (abfd)->core != NULL; | |
252b5132 RH |
274 | } |
275 | ||
72a80a16 | 276 | static char * |
217aa764 | 277 | bfd_elf_get_str_section (bfd *abfd, unsigned int shindex) |
252b5132 RH |
278 | { |
279 | Elf_Internal_Shdr **i_shdrp; | |
f075ee0c | 280 | bfd_byte *shstrtab = NULL; |
dc810e39 AM |
281 | file_ptr offset; |
282 | bfd_size_type shstrtabsize; | |
252b5132 RH |
283 | |
284 | i_shdrp = elf_elfsections (abfd); | |
74f2e02b AM |
285 | if (i_shdrp == 0 |
286 | || shindex >= elf_numsections (abfd) | |
287 | || i_shdrp[shindex] == 0) | |
f075ee0c | 288 | return NULL; |
252b5132 | 289 | |
f075ee0c | 290 | shstrtab = i_shdrp[shindex]->contents; |
252b5132 RH |
291 | if (shstrtab == NULL) |
292 | { | |
c044fabd | 293 | /* No cached one, attempt to read, and cache what we read. */ |
252b5132 RH |
294 | offset = i_shdrp[shindex]->sh_offset; |
295 | shstrtabsize = i_shdrp[shindex]->sh_size; | |
c6c60d09 JJ |
296 | |
297 | /* Allocate and clear an extra byte at the end, to prevent crashes | |
298 | in case the string table is not terminated. */ | |
3471d59d | 299 | if (shstrtabsize + 1 <= 1 |
06614111 NC |
300 | || bfd_seek (abfd, offset, SEEK_SET) != 0 |
301 | || (shstrtab = (bfd_byte *) bfd_alloc (abfd, shstrtabsize + 1)) == NULL) | |
c6c60d09 JJ |
302 | shstrtab = NULL; |
303 | else if (bfd_bread (shstrtab, shstrtabsize, abfd) != shstrtabsize) | |
304 | { | |
305 | if (bfd_get_error () != bfd_error_system_call) | |
306 | bfd_set_error (bfd_error_file_truncated); | |
06614111 | 307 | bfd_release (abfd, shstrtab); |
c6c60d09 | 308 | shstrtab = NULL; |
3471d59d CC |
309 | /* Once we've failed to read it, make sure we don't keep |
310 | trying. Otherwise, we'll keep allocating space for | |
311 | the string table over and over. */ | |
312 | i_shdrp[shindex]->sh_size = 0; | |
c6c60d09 JJ |
313 | } |
314 | else | |
315 | shstrtab[shstrtabsize] = '\0'; | |
217aa764 | 316 | i_shdrp[shindex]->contents = shstrtab; |
252b5132 | 317 | } |
f075ee0c | 318 | return (char *) shstrtab; |
252b5132 RH |
319 | } |
320 | ||
321 | char * | |
217aa764 AM |
322 | bfd_elf_string_from_elf_section (bfd *abfd, |
323 | unsigned int shindex, | |
324 | unsigned int strindex) | |
252b5132 RH |
325 | { |
326 | Elf_Internal_Shdr *hdr; | |
327 | ||
328 | if (strindex == 0) | |
329 | return ""; | |
330 | ||
74f2e02b AM |
331 | if (elf_elfsections (abfd) == NULL || shindex >= elf_numsections (abfd)) |
332 | return NULL; | |
333 | ||
252b5132 RH |
334 | hdr = elf_elfsections (abfd)[shindex]; |
335 | ||
06614111 NC |
336 | if (hdr->contents == NULL) |
337 | { | |
338 | if (hdr->sh_type != SHT_STRTAB && hdr->sh_type < SHT_LOOS) | |
339 | { | |
340 | /* PR 17512: file: f057ec89. */ | |
341 | _bfd_error_handler (_("%B: attempt to load strings from a non-string section (number %d)"), | |
342 | abfd, shindex); | |
343 | return NULL; | |
344 | } | |
b1fa9dd6 | 345 | |
06614111 NC |
346 | if (bfd_elf_get_str_section (abfd, shindex) == NULL) |
347 | return NULL; | |
348 | } | |
252b5132 RH |
349 | |
350 | if (strindex >= hdr->sh_size) | |
351 | { | |
1b3a8575 | 352 | unsigned int shstrndx = elf_elfheader(abfd)->e_shstrndx; |
252b5132 | 353 | (*_bfd_error_handler) |
d003868e AM |
354 | (_("%B: invalid string offset %u >= %lu for section `%s'"), |
355 | abfd, strindex, (unsigned long) hdr->sh_size, | |
1b3a8575 | 356 | (shindex == shstrndx && strindex == hdr->sh_name |
252b5132 | 357 | ? ".shstrtab" |
1b3a8575 | 358 | : bfd_elf_string_from_elf_section (abfd, shstrndx, hdr->sh_name))); |
45b222d6 | 359 | return NULL; |
252b5132 RH |
360 | } |
361 | ||
362 | return ((char *) hdr->contents) + strindex; | |
363 | } | |
364 | ||
6cdc0ccc AM |
365 | /* Read and convert symbols to internal format. |
366 | SYMCOUNT specifies the number of symbols to read, starting from | |
367 | symbol SYMOFFSET. If any of INTSYM_BUF, EXTSYM_BUF or EXTSHNDX_BUF | |
368 | are non-NULL, they are used to store the internal symbols, external | |
b7c368d0 NC |
369 | symbols, and symbol section index extensions, respectively. |
370 | Returns a pointer to the internal symbol buffer (malloced if necessary) | |
371 | or NULL if there were no symbols or some kind of problem. */ | |
6cdc0ccc AM |
372 | |
373 | Elf_Internal_Sym * | |
217aa764 AM |
374 | bfd_elf_get_elf_syms (bfd *ibfd, |
375 | Elf_Internal_Shdr *symtab_hdr, | |
376 | size_t symcount, | |
377 | size_t symoffset, | |
378 | Elf_Internal_Sym *intsym_buf, | |
379 | void *extsym_buf, | |
380 | Elf_External_Sym_Shndx *extshndx_buf) | |
6cdc0ccc AM |
381 | { |
382 | Elf_Internal_Shdr *shndx_hdr; | |
217aa764 | 383 | void *alloc_ext; |
df622259 | 384 | const bfd_byte *esym; |
6cdc0ccc AM |
385 | Elf_External_Sym_Shndx *alloc_extshndx; |
386 | Elf_External_Sym_Shndx *shndx; | |
4dd07732 | 387 | Elf_Internal_Sym *alloc_intsym; |
6cdc0ccc AM |
388 | Elf_Internal_Sym *isym; |
389 | Elf_Internal_Sym *isymend; | |
9c5bfbb7 | 390 | const struct elf_backend_data *bed; |
6cdc0ccc AM |
391 | size_t extsym_size; |
392 | bfd_size_type amt; | |
393 | file_ptr pos; | |
394 | ||
e44a2c9c AM |
395 | if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour) |
396 | abort (); | |
397 | ||
6cdc0ccc AM |
398 | if (symcount == 0) |
399 | return intsym_buf; | |
400 | ||
401 | /* Normal syms might have section extension entries. */ | |
402 | shndx_hdr = NULL; | |
6a40cf0c NC |
403 | if (elf_symtab_shndx_list (ibfd) != NULL) |
404 | { | |
405 | elf_section_list * entry; | |
406 | Elf_Internal_Shdr **sections = elf_elfsections (ibfd); | |
407 | ||
408 | /* Find an index section that is linked to this symtab section. */ | |
409 | for (entry = elf_symtab_shndx_list (ibfd); entry != NULL; entry = entry->next) | |
315350be NC |
410 | { |
411 | /* PR 20063. */ | |
412 | if (entry->hdr.sh_link >= elf_numsections (ibfd)) | |
413 | continue; | |
414 | ||
415 | if (sections[entry->hdr.sh_link] == symtab_hdr) | |
416 | { | |
417 | shndx_hdr = & entry->hdr; | |
418 | break; | |
419 | }; | |
420 | } | |
6a40cf0c NC |
421 | |
422 | if (shndx_hdr == NULL) | |
423 | { | |
424 | if (symtab_hdr == & elf_symtab_hdr (ibfd)) | |
425 | /* Not really accurate, but this was how the old code used to work. */ | |
426 | shndx_hdr = & elf_symtab_shndx_list (ibfd)->hdr; | |
427 | /* Otherwise we do nothing. The assumption is that | |
428 | the index table will not be needed. */ | |
429 | } | |
430 | } | |
6cdc0ccc AM |
431 | |
432 | /* Read the symbols. */ | |
433 | alloc_ext = NULL; | |
434 | alloc_extshndx = NULL; | |
4dd07732 | 435 | alloc_intsym = NULL; |
6cdc0ccc AM |
436 | bed = get_elf_backend_data (ibfd); |
437 | extsym_size = bed->s->sizeof_sym; | |
438 | amt = symcount * extsym_size; | |
439 | pos = symtab_hdr->sh_offset + symoffset * extsym_size; | |
440 | if (extsym_buf == NULL) | |
441 | { | |
d0fb9a8d | 442 | alloc_ext = bfd_malloc2 (symcount, extsym_size); |
6cdc0ccc AM |
443 | extsym_buf = alloc_ext; |
444 | } | |
445 | if (extsym_buf == NULL | |
446 | || bfd_seek (ibfd, pos, SEEK_SET) != 0 | |
447 | || bfd_bread (extsym_buf, amt, ibfd) != amt) | |
448 | { | |
449 | intsym_buf = NULL; | |
450 | goto out; | |
451 | } | |
452 | ||
453 | if (shndx_hdr == NULL || shndx_hdr->sh_size == 0) | |
454 | extshndx_buf = NULL; | |
455 | else | |
456 | { | |
457 | amt = symcount * sizeof (Elf_External_Sym_Shndx); | |
458 | pos = shndx_hdr->sh_offset + symoffset * sizeof (Elf_External_Sym_Shndx); | |
459 | if (extshndx_buf == NULL) | |
460 | { | |
a50b1753 NC |
461 | alloc_extshndx = (Elf_External_Sym_Shndx *) |
462 | bfd_malloc2 (symcount, sizeof (Elf_External_Sym_Shndx)); | |
6cdc0ccc AM |
463 | extshndx_buf = alloc_extshndx; |
464 | } | |
465 | if (extshndx_buf == NULL | |
466 | || bfd_seek (ibfd, pos, SEEK_SET) != 0 | |
467 | || bfd_bread (extshndx_buf, amt, ibfd) != amt) | |
468 | { | |
469 | intsym_buf = NULL; | |
470 | goto out; | |
471 | } | |
472 | } | |
473 | ||
474 | if (intsym_buf == NULL) | |
475 | { | |
a50b1753 NC |
476 | alloc_intsym = (Elf_Internal_Sym *) |
477 | bfd_malloc2 (symcount, sizeof (Elf_Internal_Sym)); | |
4dd07732 | 478 | intsym_buf = alloc_intsym; |
6cdc0ccc AM |
479 | if (intsym_buf == NULL) |
480 | goto out; | |
481 | } | |
482 | ||
483 | /* Convert the symbols to internal form. */ | |
484 | isymend = intsym_buf + symcount; | |
a50b1753 NC |
485 | for (esym = (const bfd_byte *) extsym_buf, isym = intsym_buf, |
486 | shndx = extshndx_buf; | |
6cdc0ccc AM |
487 | isym < isymend; |
488 | esym += extsym_size, isym++, shndx = shndx != NULL ? shndx + 1 : NULL) | |
8384fb8f AM |
489 | if (!(*bed->s->swap_symbol_in) (ibfd, esym, shndx, isym)) |
490 | { | |
491 | symoffset += (esym - (bfd_byte *) extsym_buf) / extsym_size; | |
492 | (*_bfd_error_handler) (_("%B symbol number %lu references " | |
493 | "nonexistent SHT_SYMTAB_SHNDX section"), | |
494 | ibfd, (unsigned long) symoffset); | |
4dd07732 AM |
495 | if (alloc_intsym != NULL) |
496 | free (alloc_intsym); | |
8384fb8f AM |
497 | intsym_buf = NULL; |
498 | goto out; | |
499 | } | |
6cdc0ccc AM |
500 | |
501 | out: | |
502 | if (alloc_ext != NULL) | |
503 | free (alloc_ext); | |
504 | if (alloc_extshndx != NULL) | |
505 | free (alloc_extshndx); | |
506 | ||
507 | return intsym_buf; | |
508 | } | |
509 | ||
5cab59f6 AM |
510 | /* Look up a symbol name. */ |
511 | const char * | |
be8dd2ca AM |
512 | bfd_elf_sym_name (bfd *abfd, |
513 | Elf_Internal_Shdr *symtab_hdr, | |
26c61ae5 L |
514 | Elf_Internal_Sym *isym, |
515 | asection *sym_sec) | |
5cab59f6 | 516 | { |
26c61ae5 | 517 | const char *name; |
5cab59f6 | 518 | unsigned int iname = isym->st_name; |
be8dd2ca | 519 | unsigned int shindex = symtab_hdr->sh_link; |
26c61ae5 | 520 | |
138f35cc JJ |
521 | if (iname == 0 && ELF_ST_TYPE (isym->st_info) == STT_SECTION |
522 | /* Check for a bogus st_shndx to avoid crashing. */ | |
4fbb74a6 | 523 | && isym->st_shndx < elf_numsections (abfd)) |
5cab59f6 AM |
524 | { |
525 | iname = elf_elfsections (abfd)[isym->st_shndx]->sh_name; | |
526 | shindex = elf_elfheader (abfd)->e_shstrndx; | |
527 | } | |
528 | ||
26c61ae5 L |
529 | name = bfd_elf_string_from_elf_section (abfd, shindex, iname); |
530 | if (name == NULL) | |
531 | name = "(null)"; | |
532 | else if (sym_sec && *name == '\0') | |
533 | name = bfd_section_name (abfd, sym_sec); | |
534 | ||
535 | return name; | |
5cab59f6 AM |
536 | } |
537 | ||
dbb410c3 AM |
538 | /* Elf_Internal_Shdr->contents is an array of these for SHT_GROUP |
539 | sections. The first element is the flags, the rest are section | |
540 | pointers. */ | |
541 | ||
542 | typedef union elf_internal_group { | |
543 | Elf_Internal_Shdr *shdr; | |
544 | unsigned int flags; | |
545 | } Elf_Internal_Group; | |
546 | ||
b885599b AM |
547 | /* Return the name of the group signature symbol. Why isn't the |
548 | signature just a string? */ | |
549 | ||
550 | static const char * | |
217aa764 | 551 | group_signature (bfd *abfd, Elf_Internal_Shdr *ghdr) |
b885599b | 552 | { |
9dce4196 | 553 | Elf_Internal_Shdr *hdr; |
9dce4196 AM |
554 | unsigned char esym[sizeof (Elf64_External_Sym)]; |
555 | Elf_External_Sym_Shndx eshndx; | |
556 | Elf_Internal_Sym isym; | |
b885599b | 557 | |
13792e9d L |
558 | /* First we need to ensure the symbol table is available. Make sure |
559 | that it is a symbol table section. */ | |
4fbb74a6 AM |
560 | if (ghdr->sh_link >= elf_numsections (abfd)) |
561 | return NULL; | |
13792e9d L |
562 | hdr = elf_elfsections (abfd) [ghdr->sh_link]; |
563 | if (hdr->sh_type != SHT_SYMTAB | |
564 | || ! bfd_section_from_shdr (abfd, ghdr->sh_link)) | |
b885599b AM |
565 | return NULL; |
566 | ||
9dce4196 AM |
567 | /* Go read the symbol. */ |
568 | hdr = &elf_tdata (abfd)->symtab_hdr; | |
6cdc0ccc AM |
569 | if (bfd_elf_get_elf_syms (abfd, hdr, 1, ghdr->sh_info, |
570 | &isym, esym, &eshndx) == NULL) | |
b885599b | 571 | return NULL; |
9dce4196 | 572 | |
26c61ae5 | 573 | return bfd_elf_sym_name (abfd, hdr, &isym, NULL); |
b885599b AM |
574 | } |
575 | ||
dbb410c3 AM |
576 | /* Set next_in_group list pointer, and group name for NEWSECT. */ |
577 | ||
b34976b6 | 578 | static bfd_boolean |
217aa764 | 579 | setup_group (bfd *abfd, Elf_Internal_Shdr *hdr, asection *newsect) |
dbb410c3 AM |
580 | { |
581 | unsigned int num_group = elf_tdata (abfd)->num_group; | |
582 | ||
583 | /* If num_group is zero, read in all SHT_GROUP sections. The count | |
584 | is set to -1 if there are no SHT_GROUP sections. */ | |
585 | if (num_group == 0) | |
586 | { | |
587 | unsigned int i, shnum; | |
588 | ||
589 | /* First count the number of groups. If we have a SHT_GROUP | |
590 | section with just a flag word (ie. sh_size is 4), ignore it. */ | |
9ad5cbcf | 591 | shnum = elf_numsections (abfd); |
dbb410c3 | 592 | num_group = 0; |
08a40648 | 593 | |
44534af3 | 594 | #define IS_VALID_GROUP_SECTION_HEADER(shdr, minsize) \ |
1783205a | 595 | ( (shdr)->sh_type == SHT_GROUP \ |
44534af3 | 596 | && (shdr)->sh_size >= minsize \ |
1783205a NC |
597 | && (shdr)->sh_entsize == GRP_ENTRY_SIZE \ |
598 | && ((shdr)->sh_size % GRP_ENTRY_SIZE) == 0) | |
08a40648 | 599 | |
dbb410c3 AM |
600 | for (i = 0; i < shnum; i++) |
601 | { | |
602 | Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i]; | |
1783205a | 603 | |
44534af3 | 604 | if (IS_VALID_GROUP_SECTION_HEADER (shdr, 2 * GRP_ENTRY_SIZE)) |
dbb410c3 AM |
605 | num_group += 1; |
606 | } | |
607 | ||
608 | if (num_group == 0) | |
20dbb49d L |
609 | { |
610 | num_group = (unsigned) -1; | |
611 | elf_tdata (abfd)->num_group = num_group; | |
612 | } | |
613 | else | |
dbb410c3 AM |
614 | { |
615 | /* We keep a list of elf section headers for group sections, | |
616 | so we can find them quickly. */ | |
20dbb49d | 617 | bfd_size_type amt; |
d0fb9a8d | 618 | |
20dbb49d | 619 | elf_tdata (abfd)->num_group = num_group; |
a50b1753 NC |
620 | elf_tdata (abfd)->group_sect_ptr = (Elf_Internal_Shdr **) |
621 | bfd_alloc2 (abfd, num_group, sizeof (Elf_Internal_Shdr *)); | |
dbb410c3 | 622 | if (elf_tdata (abfd)->group_sect_ptr == NULL) |
b34976b6 | 623 | return FALSE; |
dbb410c3 AM |
624 | |
625 | num_group = 0; | |
626 | for (i = 0; i < shnum; i++) | |
627 | { | |
628 | Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i]; | |
1783205a | 629 | |
44534af3 | 630 | if (IS_VALID_GROUP_SECTION_HEADER (shdr, 2 * GRP_ENTRY_SIZE)) |
dbb410c3 | 631 | { |
973ffd63 | 632 | unsigned char *src; |
dbb410c3 AM |
633 | Elf_Internal_Group *dest; |
634 | ||
635 | /* Add to list of sections. */ | |
636 | elf_tdata (abfd)->group_sect_ptr[num_group] = shdr; | |
637 | num_group += 1; | |
638 | ||
639 | /* Read the raw contents. */ | |
640 | BFD_ASSERT (sizeof (*dest) >= 4); | |
641 | amt = shdr->sh_size * sizeof (*dest) / 4; | |
a50b1753 NC |
642 | shdr->contents = (unsigned char *) |
643 | bfd_alloc2 (abfd, shdr->sh_size, sizeof (*dest) / 4); | |
1783205a NC |
644 | /* PR binutils/4110: Handle corrupt group headers. */ |
645 | if (shdr->contents == NULL) | |
646 | { | |
647 | _bfd_error_handler | |
493a3386 | 648 | (_("%B: corrupt size field in group section header: 0x%lx"), abfd, shdr->sh_size); |
1783205a | 649 | bfd_set_error (bfd_error_bad_value); |
493a3386 NC |
650 | -- num_group; |
651 | continue; | |
1783205a NC |
652 | } |
653 | ||
654 | memset (shdr->contents, 0, amt); | |
655 | ||
656 | if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0 | |
dbb410c3 AM |
657 | || (bfd_bread (shdr->contents, shdr->sh_size, abfd) |
658 | != shdr->sh_size)) | |
493a3386 NC |
659 | { |
660 | _bfd_error_handler | |
661 | (_("%B: invalid size field in group section header: 0x%lx"), abfd, shdr->sh_size); | |
662 | bfd_set_error (bfd_error_bad_value); | |
663 | -- num_group; | |
664 | /* PR 17510: If the group contents are even partially | |
665 | corrupt, do not allow any of the contents to be used. */ | |
666 | memset (shdr->contents, 0, amt); | |
667 | continue; | |
668 | } | |
708d7d0d | 669 | |
dbb410c3 AM |
670 | /* Translate raw contents, a flag word followed by an |
671 | array of elf section indices all in target byte order, | |
672 | to the flag word followed by an array of elf section | |
673 | pointers. */ | |
674 | src = shdr->contents + shdr->sh_size; | |
675 | dest = (Elf_Internal_Group *) (shdr->contents + amt); | |
06614111 | 676 | |
dbb410c3 AM |
677 | while (1) |
678 | { | |
679 | unsigned int idx; | |
680 | ||
681 | src -= 4; | |
682 | --dest; | |
683 | idx = H_GET_32 (abfd, src); | |
684 | if (src == shdr->contents) | |
685 | { | |
686 | dest->flags = idx; | |
b885599b AM |
687 | if (shdr->bfd_section != NULL && (idx & GRP_COMDAT)) |
688 | shdr->bfd_section->flags | |
689 | |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD; | |
dbb410c3 AM |
690 | break; |
691 | } | |
692 | if (idx >= shnum) | |
693 | { | |
694 | ((*_bfd_error_handler) | |
d003868e | 695 | (_("%B: invalid SHT_GROUP entry"), abfd)); |
dbb410c3 AM |
696 | idx = 0; |
697 | } | |
698 | dest->shdr = elf_elfsections (abfd)[idx]; | |
699 | } | |
700 | } | |
701 | } | |
493a3386 NC |
702 | |
703 | /* PR 17510: Corrupt binaries might contain invalid groups. */ | |
704 | if (num_group != (unsigned) elf_tdata (abfd)->num_group) | |
705 | { | |
706 | elf_tdata (abfd)->num_group = num_group; | |
707 | ||
708 | /* If all groups are invalid then fail. */ | |
709 | if (num_group == 0) | |
710 | { | |
711 | elf_tdata (abfd)->group_sect_ptr = NULL; | |
712 | elf_tdata (abfd)->num_group = num_group = -1; | |
713 | (*_bfd_error_handler) (_("%B: no valid group sections found"), abfd); | |
714 | bfd_set_error (bfd_error_bad_value); | |
715 | } | |
716 | } | |
dbb410c3 AM |
717 | } |
718 | } | |
719 | ||
720 | if (num_group != (unsigned) -1) | |
721 | { | |
722 | unsigned int i; | |
723 | ||
724 | for (i = 0; i < num_group; i++) | |
725 | { | |
726 | Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i]; | |
727 | Elf_Internal_Group *idx = (Elf_Internal_Group *) shdr->contents; | |
728 | unsigned int n_elt = shdr->sh_size / 4; | |
729 | ||
730 | /* Look through this group's sections to see if current | |
731 | section is a member. */ | |
732 | while (--n_elt != 0) | |
733 | if ((++idx)->shdr == hdr) | |
734 | { | |
e0e8c97f | 735 | asection *s = NULL; |
dbb410c3 AM |
736 | |
737 | /* We are a member of this group. Go looking through | |
738 | other members to see if any others are linked via | |
739 | next_in_group. */ | |
740 | idx = (Elf_Internal_Group *) shdr->contents; | |
741 | n_elt = shdr->sh_size / 4; | |
742 | while (--n_elt != 0) | |
743 | if ((s = (++idx)->shdr->bfd_section) != NULL | |
945906ff | 744 | && elf_next_in_group (s) != NULL) |
dbb410c3 AM |
745 | break; |
746 | if (n_elt != 0) | |
747 | { | |
dbb410c3 AM |
748 | /* Snarf the group name from other member, and |
749 | insert current section in circular list. */ | |
945906ff AM |
750 | elf_group_name (newsect) = elf_group_name (s); |
751 | elf_next_in_group (newsect) = elf_next_in_group (s); | |
752 | elf_next_in_group (s) = newsect; | |
dbb410c3 AM |
753 | } |
754 | else | |
755 | { | |
dbb410c3 AM |
756 | const char *gname; |
757 | ||
b885599b AM |
758 | gname = group_signature (abfd, shdr); |
759 | if (gname == NULL) | |
b34976b6 | 760 | return FALSE; |
945906ff | 761 | elf_group_name (newsect) = gname; |
dbb410c3 AM |
762 | |
763 | /* Start a circular list with one element. */ | |
945906ff | 764 | elf_next_in_group (newsect) = newsect; |
dbb410c3 | 765 | } |
b885599b | 766 | |
9dce4196 AM |
767 | /* If the group section has been created, point to the |
768 | new member. */ | |
dbb410c3 | 769 | if (shdr->bfd_section != NULL) |
945906ff | 770 | elf_next_in_group (shdr->bfd_section) = newsect; |
b885599b | 771 | |
dbb410c3 AM |
772 | i = num_group - 1; |
773 | break; | |
774 | } | |
775 | } | |
776 | } | |
777 | ||
945906ff | 778 | if (elf_group_name (newsect) == NULL) |
dbb410c3 | 779 | { |
d003868e AM |
780 | (*_bfd_error_handler) (_("%B: no group info for section %A"), |
781 | abfd, newsect); | |
493a3386 | 782 | return FALSE; |
dbb410c3 | 783 | } |
b34976b6 | 784 | return TRUE; |
dbb410c3 AM |
785 | } |
786 | ||
3d7f7666 | 787 | bfd_boolean |
dd863624 | 788 | _bfd_elf_setup_sections (bfd *abfd) |
3d7f7666 L |
789 | { |
790 | unsigned int i; | |
791 | unsigned int num_group = elf_tdata (abfd)->num_group; | |
792 | bfd_boolean result = TRUE; | |
dd863624 L |
793 | asection *s; |
794 | ||
795 | /* Process SHF_LINK_ORDER. */ | |
796 | for (s = abfd->sections; s != NULL; s = s->next) | |
797 | { | |
798 | Elf_Internal_Shdr *this_hdr = &elf_section_data (s)->this_hdr; | |
799 | if ((this_hdr->sh_flags & SHF_LINK_ORDER) != 0) | |
800 | { | |
801 | unsigned int elfsec = this_hdr->sh_link; | |
802 | /* FIXME: The old Intel compiler and old strip/objcopy may | |
803 | not set the sh_link or sh_info fields. Hence we could | |
804 | get the situation where elfsec is 0. */ | |
805 | if (elfsec == 0) | |
806 | { | |
4fbb74a6 | 807 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
dd863624 L |
808 | if (bed->link_order_error_handler) |
809 | bed->link_order_error_handler | |
810 | (_("%B: warning: sh_link not set for section `%A'"), | |
811 | abfd, s); | |
812 | } | |
813 | else | |
814 | { | |
91d6fa6a | 815 | asection *linksec = NULL; |
25bbc984 | 816 | |
4fbb74a6 AM |
817 | if (elfsec < elf_numsections (abfd)) |
818 | { | |
819 | this_hdr = elf_elfsections (abfd)[elfsec]; | |
91d6fa6a | 820 | linksec = this_hdr->bfd_section; |
4fbb74a6 | 821 | } |
25bbc984 L |
822 | |
823 | /* PR 1991, 2008: | |
824 | Some strip/objcopy may leave an incorrect value in | |
825 | sh_link. We don't want to proceed. */ | |
91d6fa6a | 826 | if (linksec == NULL) |
25bbc984 L |
827 | { |
828 | (*_bfd_error_handler) | |
829 | (_("%B: sh_link [%d] in section `%A' is incorrect"), | |
830 | s->owner, s, elfsec); | |
831 | result = FALSE; | |
832 | } | |
833 | ||
91d6fa6a | 834 | elf_linked_to_section (s) = linksec; |
dd863624 L |
835 | } |
836 | } | |
837 | } | |
3d7f7666 | 838 | |
dd863624 | 839 | /* Process section groups. */ |
3d7f7666 L |
840 | if (num_group == (unsigned) -1) |
841 | return result; | |
842 | ||
843 | for (i = 0; i < num_group; i++) | |
844 | { | |
845 | Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i]; | |
4b0e8a5f NC |
846 | Elf_Internal_Group *idx; |
847 | unsigned int n_elt; | |
3d7f7666 | 848 | |
4b0e8a5f NC |
849 | /* PR binutils/18758: Beware of corrupt binaries with invalid group data. */ |
850 | if (shdr == NULL || shdr->bfd_section == NULL || shdr->contents == NULL) | |
851 | { | |
852 | (*_bfd_error_handler) | |
853 | (_("%B: section group entry number %u is corrupt"), | |
854 | abfd, i); | |
855 | result = FALSE; | |
856 | continue; | |
857 | } | |
858 | ||
859 | idx = (Elf_Internal_Group *) shdr->contents; | |
860 | n_elt = shdr->sh_size / 4; | |
1b786873 | 861 | |
3d7f7666 L |
862 | while (--n_elt != 0) |
863 | if ((++idx)->shdr->bfd_section) | |
864 | elf_sec_group (idx->shdr->bfd_section) = shdr->bfd_section; | |
865 | else if (idx->shdr->sh_type == SHT_RELA | |
866 | || idx->shdr->sh_type == SHT_REL) | |
867 | /* We won't include relocation sections in section groups in | |
868 | output object files. We adjust the group section size here | |
869 | so that relocatable link will work correctly when | |
870 | relocation sections are in section group in input object | |
871 | files. */ | |
872 | shdr->bfd_section->size -= 4; | |
873 | else | |
874 | { | |
875 | /* There are some unknown sections in the group. */ | |
876 | (*_bfd_error_handler) | |
d003868e AM |
877 | (_("%B: unknown [%d] section `%s' in group [%s]"), |
878 | abfd, | |
3d7f7666 | 879 | (unsigned int) idx->shdr->sh_type, |
1b3a8575 AM |
880 | bfd_elf_string_from_elf_section (abfd, |
881 | (elf_elfheader (abfd) | |
882 | ->e_shstrndx), | |
883 | idx->shdr->sh_name), | |
3d7f7666 L |
884 | shdr->bfd_section->name); |
885 | result = FALSE; | |
886 | } | |
887 | } | |
888 | return result; | |
889 | } | |
890 | ||
72adc230 AM |
891 | bfd_boolean |
892 | bfd_elf_is_group_section (bfd *abfd ATTRIBUTE_UNUSED, const asection *sec) | |
893 | { | |
894 | return elf_next_in_group (sec) != NULL; | |
895 | } | |
896 | ||
f6fe1ccd L |
897 | static char * |
898 | convert_debug_to_zdebug (bfd *abfd, const char *name) | |
899 | { | |
900 | unsigned int len = strlen (name); | |
901 | char *new_name = bfd_alloc (abfd, len + 2); | |
902 | if (new_name == NULL) | |
903 | return NULL; | |
904 | new_name[0] = '.'; | |
905 | new_name[1] = 'z'; | |
906 | memcpy (new_name + 2, name + 1, len); | |
907 | return new_name; | |
908 | } | |
909 | ||
910 | static char * | |
911 | convert_zdebug_to_debug (bfd *abfd, const char *name) | |
912 | { | |
913 | unsigned int len = strlen (name); | |
914 | char *new_name = bfd_alloc (abfd, len); | |
915 | if (new_name == NULL) | |
916 | return NULL; | |
917 | new_name[0] = '.'; | |
918 | memcpy (new_name + 1, name + 2, len - 1); | |
919 | return new_name; | |
920 | } | |
921 | ||
252b5132 RH |
922 | /* Make a BFD section from an ELF section. We store a pointer to the |
923 | BFD section in the bfd_section field of the header. */ | |
924 | ||
b34976b6 | 925 | bfd_boolean |
217aa764 AM |
926 | _bfd_elf_make_section_from_shdr (bfd *abfd, |
927 | Elf_Internal_Shdr *hdr, | |
6dc132d9 L |
928 | const char *name, |
929 | int shindex) | |
252b5132 RH |
930 | { |
931 | asection *newsect; | |
932 | flagword flags; | |
9c5bfbb7 | 933 | const struct elf_backend_data *bed; |
252b5132 RH |
934 | |
935 | if (hdr->bfd_section != NULL) | |
4e011fb5 | 936 | return TRUE; |
252b5132 RH |
937 | |
938 | newsect = bfd_make_section_anyway (abfd, name); | |
939 | if (newsect == NULL) | |
b34976b6 | 940 | return FALSE; |
252b5132 | 941 | |
1829f4b2 AM |
942 | hdr->bfd_section = newsect; |
943 | elf_section_data (newsect)->this_hdr = *hdr; | |
6dc132d9 | 944 | elf_section_data (newsect)->this_idx = shindex; |
1829f4b2 | 945 | |
2f89ff8d L |
946 | /* Always use the real type/flags. */ |
947 | elf_section_type (newsect) = hdr->sh_type; | |
948 | elf_section_flags (newsect) = hdr->sh_flags; | |
949 | ||
252b5132 RH |
950 | newsect->filepos = hdr->sh_offset; |
951 | ||
952 | if (! bfd_set_section_vma (abfd, newsect, hdr->sh_addr) | |
953 | || ! bfd_set_section_size (abfd, newsect, hdr->sh_size) | |
954 | || ! bfd_set_section_alignment (abfd, newsect, | |
72de5009 | 955 | bfd_log2 (hdr->sh_addralign))) |
b34976b6 | 956 | return FALSE; |
252b5132 RH |
957 | |
958 | flags = SEC_NO_FLAGS; | |
959 | if (hdr->sh_type != SHT_NOBITS) | |
960 | flags |= SEC_HAS_CONTENTS; | |
dbb410c3 | 961 | if (hdr->sh_type == SHT_GROUP) |
b3096250 | 962 | flags |= SEC_GROUP | SEC_EXCLUDE; |
252b5132 RH |
963 | if ((hdr->sh_flags & SHF_ALLOC) != 0) |
964 | { | |
965 | flags |= SEC_ALLOC; | |
966 | if (hdr->sh_type != SHT_NOBITS) | |
967 | flags |= SEC_LOAD; | |
968 | } | |
969 | if ((hdr->sh_flags & SHF_WRITE) == 0) | |
970 | flags |= SEC_READONLY; | |
971 | if ((hdr->sh_flags & SHF_EXECINSTR) != 0) | |
972 | flags |= SEC_CODE; | |
973 | else if ((flags & SEC_LOAD) != 0) | |
974 | flags |= SEC_DATA; | |
f5fa8ca2 JJ |
975 | if ((hdr->sh_flags & SHF_MERGE) != 0) |
976 | { | |
977 | flags |= SEC_MERGE; | |
978 | newsect->entsize = hdr->sh_entsize; | |
f5fa8ca2 | 979 | } |
84865015 NC |
980 | if ((hdr->sh_flags & SHF_STRINGS) != 0) |
981 | flags |= SEC_STRINGS; | |
dbb410c3 AM |
982 | if (hdr->sh_flags & SHF_GROUP) |
983 | if (!setup_group (abfd, hdr, newsect)) | |
b34976b6 | 984 | return FALSE; |
13ae64f3 JJ |
985 | if ((hdr->sh_flags & SHF_TLS) != 0) |
986 | flags |= SEC_THREAD_LOCAL; | |
18ae9cc1 L |
987 | if ((hdr->sh_flags & SHF_EXCLUDE) != 0) |
988 | flags |= SEC_EXCLUDE; | |
252b5132 | 989 | |
3d2b39cf | 990 | if ((flags & SEC_ALLOC) == 0) |
7a6cc5fb | 991 | { |
3d2b39cf L |
992 | /* The debugging sections appear to be recognized only by name, |
993 | not any sort of flag. Their SEC_ALLOC bits are cleared. */ | |
3d2b39cf L |
994 | if (name [0] == '.') |
995 | { | |
f073ced3 AM |
996 | const char *p; |
997 | int n; | |
998 | if (name[1] == 'd') | |
999 | p = ".debug", n = 6; | |
1000 | else if (name[1] == 'g' && name[2] == 'n') | |
1001 | p = ".gnu.linkonce.wi.", n = 17; | |
1002 | else if (name[1] == 'g' && name[2] == 'd') | |
1003 | p = ".gdb_index", n = 11; /* yes we really do mean 11. */ | |
1004 | else if (name[1] == 'l') | |
1005 | p = ".line", n = 5; | |
1006 | else if (name[1] == 's') | |
1007 | p = ".stab", n = 5; | |
1008 | else if (name[1] == 'z') | |
1009 | p = ".zdebug", n = 7; | |
1010 | else | |
1011 | p = NULL, n = 0; | |
1012 | if (p != NULL && strncmp (name, p, n) == 0) | |
3d2b39cf L |
1013 | flags |= SEC_DEBUGGING; |
1014 | } | |
1015 | } | |
252b5132 RH |
1016 | |
1017 | /* As a GNU extension, if the name begins with .gnu.linkonce, we | |
1018 | only link a single copy of the section. This is used to support | |
1019 | g++. g++ will emit each template expansion in its own section. | |
1020 | The symbols will be defined as weak, so that multiple definitions | |
1021 | are permitted. The GNU linker extension is to actually discard | |
1022 | all but one of the sections. */ | |
0112cd26 | 1023 | if (CONST_STRNEQ (name, ".gnu.linkonce") |
b885599b | 1024 | && elf_next_in_group (newsect) == NULL) |
252b5132 RH |
1025 | flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD; |
1026 | ||
fa152c49 JW |
1027 | bed = get_elf_backend_data (abfd); |
1028 | if (bed->elf_backend_section_flags) | |
1029 | if (! bed->elf_backend_section_flags (&flags, hdr)) | |
b34976b6 | 1030 | return FALSE; |
fa152c49 | 1031 | |
252b5132 | 1032 | if (! bfd_set_section_flags (abfd, newsect, flags)) |
b34976b6 | 1033 | return FALSE; |
252b5132 | 1034 | |
718175fa JK |
1035 | /* We do not parse the PT_NOTE segments as we are interested even in the |
1036 | separate debug info files which may have the segments offsets corrupted. | |
1037 | PT_NOTEs from the core files are currently not parsed using BFD. */ | |
1038 | if (hdr->sh_type == SHT_NOTE) | |
1039 | { | |
baea7ef1 | 1040 | bfd_byte *contents; |
718175fa | 1041 | |
baea7ef1 | 1042 | if (!bfd_malloc_and_get_section (abfd, newsect, &contents)) |
718175fa JK |
1043 | return FALSE; |
1044 | ||
baea7ef1 | 1045 | elf_parse_notes (abfd, (char *) contents, hdr->sh_size, -1); |
718175fa JK |
1046 | free (contents); |
1047 | } | |
1048 | ||
252b5132 RH |
1049 | if ((flags & SEC_ALLOC) != 0) |
1050 | { | |
1051 | Elf_Internal_Phdr *phdr; | |
6ffd7900 AM |
1052 | unsigned int i, nload; |
1053 | ||
1054 | /* Some ELF linkers produce binaries with all the program header | |
1055 | p_paddr fields zero. If we have such a binary with more than | |
1056 | one PT_LOAD header, then leave the section lma equal to vma | |
1057 | so that we don't create sections with overlapping lma. */ | |
1058 | phdr = elf_tdata (abfd)->phdr; | |
1059 | for (nload = 0, i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++) | |
1060 | if (phdr->p_paddr != 0) | |
1061 | break; | |
1062 | else if (phdr->p_type == PT_LOAD && phdr->p_memsz != 0) | |
1063 | ++nload; | |
1064 | if (i >= elf_elfheader (abfd)->e_phnum && nload > 1) | |
1065 | return TRUE; | |
252b5132 | 1066 | |
252b5132 RH |
1067 | phdr = elf_tdata (abfd)->phdr; |
1068 | for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++) | |
1069 | { | |
86b2281f AM |
1070 | if (((phdr->p_type == PT_LOAD |
1071 | && (hdr->sh_flags & SHF_TLS) == 0) | |
1072 | || phdr->p_type == PT_TLS) | |
9a83a553 | 1073 | && ELF_SECTION_IN_SEGMENT (hdr, phdr)) |
252b5132 | 1074 | { |
88967714 AM |
1075 | if ((flags & SEC_LOAD) == 0) |
1076 | newsect->lma = (phdr->p_paddr | |
1077 | + hdr->sh_addr - phdr->p_vaddr); | |
1078 | else | |
1079 | /* We used to use the same adjustment for SEC_LOAD | |
1080 | sections, but that doesn't work if the segment | |
1081 | is packed with code from multiple VMAs. | |
1082 | Instead we calculate the section LMA based on | |
1083 | the segment LMA. It is assumed that the | |
1084 | segment will contain sections with contiguous | |
1085 | LMAs, even if the VMAs are not. */ | |
1086 | newsect->lma = (phdr->p_paddr | |
1087 | + hdr->sh_offset - phdr->p_offset); | |
1088 | ||
1089 | /* With contiguous segments, we can't tell from file | |
1090 | offsets whether a section with zero size should | |
1091 | be placed at the end of one segment or the | |
1092 | beginning of the next. Decide based on vaddr. */ | |
1093 | if (hdr->sh_addr >= phdr->p_vaddr | |
1094 | && (hdr->sh_addr + hdr->sh_size | |
1095 | <= phdr->p_vaddr + phdr->p_memsz)) | |
1096 | break; | |
252b5132 RH |
1097 | } |
1098 | } | |
1099 | } | |
1100 | ||
4a114e3e L |
1101 | /* Compress/decompress DWARF debug sections with names: .debug_* and |
1102 | .zdebug_*, after the section flags is set. */ | |
1103 | if ((flags & SEC_DEBUGGING) | |
1104 | && ((name[1] == 'd' && name[6] == '_') | |
1105 | || (name[1] == 'z' && name[7] == '_'))) | |
1106 | { | |
1107 | enum { nothing, compress, decompress } action = nothing; | |
151411f8 | 1108 | int compression_header_size; |
dab394de | 1109 | bfd_size_type uncompressed_size; |
151411f8 L |
1110 | bfd_boolean compressed |
1111 | = bfd_is_section_compressed_with_header (abfd, newsect, | |
dab394de L |
1112 | &compression_header_size, |
1113 | &uncompressed_size); | |
4a114e3e | 1114 | |
151411f8 | 1115 | if (compressed) |
4a114e3e L |
1116 | { |
1117 | /* Compressed section. Check if we should decompress. */ | |
1118 | if ((abfd->flags & BFD_DECOMPRESS)) | |
1119 | action = decompress; | |
1120 | } | |
151411f8 L |
1121 | |
1122 | /* Compress the uncompressed section or convert from/to .zdebug* | |
1123 | section. Check if we should compress. */ | |
1124 | if (action == nothing) | |
4a114e3e | 1125 | { |
151411f8 L |
1126 | if (newsect->size != 0 |
1127 | && (abfd->flags & BFD_COMPRESS) | |
1128 | && compression_header_size >= 0 | |
dab394de | 1129 | && uncompressed_size > 0 |
151411f8 L |
1130 | && (!compressed |
1131 | || ((compression_header_size > 0) | |
1132 | != ((abfd->flags & BFD_COMPRESS_GABI) != 0)))) | |
4a114e3e | 1133 | action = compress; |
151411f8 L |
1134 | else |
1135 | return TRUE; | |
4a114e3e L |
1136 | } |
1137 | ||
151411f8 | 1138 | if (action == compress) |
4a114e3e | 1139 | { |
4a114e3e L |
1140 | if (!bfd_init_section_compress_status (abfd, newsect)) |
1141 | { | |
1142 | (*_bfd_error_handler) | |
bc823199 | 1143 | (_("%B: unable to initialize compress status for section %s"), |
4a114e3e L |
1144 | abfd, name); |
1145 | return FALSE; | |
1146 | } | |
151411f8 L |
1147 | } |
1148 | else | |
1149 | { | |
4a114e3e L |
1150 | if (!bfd_init_section_decompress_status (abfd, newsect)) |
1151 | { | |
1152 | (*_bfd_error_handler) | |
bc823199 | 1153 | (_("%B: unable to initialize decompress status for section %s"), |
4a114e3e L |
1154 | abfd, name); |
1155 | return FALSE; | |
1156 | } | |
151411f8 L |
1157 | } |
1158 | ||
f6fe1ccd | 1159 | if (abfd->is_linker_input) |
151411f8 | 1160 | { |
f6fe1ccd L |
1161 | if (name[1] == 'z' |
1162 | && (action == decompress | |
1163 | || (action == compress | |
1164 | && (abfd->flags & BFD_COMPRESS_GABI) != 0))) | |
4e011fb5 | 1165 | { |
f6fe1ccd L |
1166 | /* Convert section name from .zdebug_* to .debug_* so |
1167 | that linker will consider this section as a debug | |
1168 | section. */ | |
1169 | char *new_name = convert_zdebug_to_debug (abfd, name); | |
151411f8 L |
1170 | if (new_name == NULL) |
1171 | return FALSE; | |
f6fe1ccd | 1172 | bfd_rename_section (abfd, newsect, new_name); |
151411f8 | 1173 | } |
4a114e3e | 1174 | } |
f6fe1ccd L |
1175 | else |
1176 | /* For objdump, don't rename the section. For objcopy, delay | |
1177 | section rename to elf_fake_sections. */ | |
1178 | newsect->flags |= SEC_ELF_RENAME; | |
4a114e3e L |
1179 | } |
1180 | ||
b34976b6 | 1181 | return TRUE; |
252b5132 RH |
1182 | } |
1183 | ||
84865015 NC |
1184 | const char *const bfd_elf_section_type_names[] = |
1185 | { | |
252b5132 RH |
1186 | "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB", |
1187 | "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE", | |
1188 | "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM", | |
1189 | }; | |
1190 | ||
1049f94e | 1191 | /* ELF relocs are against symbols. If we are producing relocatable |
252b5132 RH |
1192 | output, and the reloc is against an external symbol, and nothing |
1193 | has given us any additional addend, the resulting reloc will also | |
1194 | be against the same symbol. In such a case, we don't want to | |
1195 | change anything about the way the reloc is handled, since it will | |
1196 | all be done at final link time. Rather than put special case code | |
1197 | into bfd_perform_relocation, all the reloc types use this howto | |
1198 | function. It just short circuits the reloc if producing | |
1049f94e | 1199 | relocatable output against an external symbol. */ |
252b5132 | 1200 | |
252b5132 | 1201 | bfd_reloc_status_type |
217aa764 AM |
1202 | bfd_elf_generic_reloc (bfd *abfd ATTRIBUTE_UNUSED, |
1203 | arelent *reloc_entry, | |
1204 | asymbol *symbol, | |
1205 | void *data ATTRIBUTE_UNUSED, | |
1206 | asection *input_section, | |
1207 | bfd *output_bfd, | |
1208 | char **error_message ATTRIBUTE_UNUSED) | |
1209 | { | |
1210 | if (output_bfd != NULL | |
252b5132 RH |
1211 | && (symbol->flags & BSF_SECTION_SYM) == 0 |
1212 | && (! reloc_entry->howto->partial_inplace | |
1213 | || reloc_entry->addend == 0)) | |
1214 | { | |
1215 | reloc_entry->address += input_section->output_offset; | |
1216 | return bfd_reloc_ok; | |
1217 | } | |
1218 | ||
1219 | return bfd_reloc_continue; | |
1220 | } | |
1221 | \f | |
84865015 NC |
1222 | /* Returns TRUE if section A matches section B. |
1223 | Names, addresses and links may be different, but everything else | |
1224 | should be the same. */ | |
1225 | ||
1226 | static bfd_boolean | |
5522f910 NC |
1227 | section_match (const Elf_Internal_Shdr * a, |
1228 | const Elf_Internal_Shdr * b) | |
84865015 NC |
1229 | { |
1230 | return | |
1231 | a->sh_type == b->sh_type | |
5522f910 NC |
1232 | && (a->sh_flags & ~ SHF_INFO_LINK) |
1233 | == (b->sh_flags & ~ SHF_INFO_LINK) | |
84865015 NC |
1234 | && a->sh_addralign == b->sh_addralign |
1235 | && a->sh_size == b->sh_size | |
1236 | && a->sh_entsize == b->sh_entsize | |
1237 | /* FIXME: Check sh_addr ? */ | |
1238 | ; | |
1239 | } | |
1240 | ||
1241 | /* Find a section in OBFD that has the same characteristics | |
1242 | as IHEADER. Return the index of this section or SHN_UNDEF if | |
1243 | none can be found. Check's section HINT first, as this is likely | |
1244 | to be the correct section. */ | |
1245 | ||
1246 | static unsigned int | |
5522f910 | 1247 | find_link (const bfd * obfd, const Elf_Internal_Shdr * iheader, const unsigned int hint) |
84865015 NC |
1248 | { |
1249 | Elf_Internal_Shdr ** oheaders = elf_elfsections (obfd); | |
1250 | unsigned int i; | |
1251 | ||
1252 | if (section_match (oheaders[hint], iheader)) | |
1253 | return hint; | |
1254 | ||
1255 | for (i = 1; i < elf_numsections (obfd); i++) | |
1256 | { | |
1257 | Elf_Internal_Shdr * oheader = oheaders[i]; | |
1258 | ||
1259 | if (section_match (oheader, iheader)) | |
1260 | /* FIXME: Do we care if there is a potential for | |
1261 | multiple matches ? */ | |
1262 | return i; | |
1263 | } | |
1264 | ||
1265 | return SHN_UNDEF; | |
1266 | } | |
1267 | ||
5522f910 NC |
1268 | /* PR 19938: Attempt to set the ELF section header fields of an OS or |
1269 | Processor specific section, based upon a matching input section. | |
1270 | Returns TRUE upon success, FALSE otherwise. */ | |
1271 | ||
1272 | static bfd_boolean | |
1273 | copy_special_section_fields (const bfd *ibfd, | |
1274 | bfd *obfd, | |
1275 | const Elf_Internal_Shdr *iheader, | |
1276 | Elf_Internal_Shdr *oheader, | |
1277 | const unsigned int secnum) | |
1278 | { | |
1279 | const struct elf_backend_data *bed = get_elf_backend_data (obfd); | |
1280 | const Elf_Internal_Shdr **iheaders = (const Elf_Internal_Shdr **) elf_elfsections (ibfd); | |
1281 | bfd_boolean changed = FALSE; | |
1282 | unsigned int sh_link; | |
1283 | ||
1284 | if (oheader->sh_type == SHT_NOBITS) | |
1285 | { | |
1286 | /* This is a feature for objcopy --only-keep-debug: | |
1287 | When a section's type is changed to NOBITS, we preserve | |
1288 | the sh_link and sh_info fields so that they can be | |
1289 | matched up with the original. | |
1290 | ||
1291 | Note: Strictly speaking these assignments are wrong. | |
1292 | The sh_link and sh_info fields should point to the | |
1293 | relevent sections in the output BFD, which may not be in | |
1294 | the same location as they were in the input BFD. But | |
1295 | the whole point of this action is to preserve the | |
1296 | original values of the sh_link and sh_info fields, so | |
1297 | that they can be matched up with the section headers in | |
1298 | the original file. So strictly speaking we may be | |
1299 | creating an invalid ELF file, but it is only for a file | |
1300 | that just contains debug info and only for sections | |
1301 | without any contents. */ | |
1302 | if (oheader->sh_link == 0) | |
1303 | oheader->sh_link = iheader->sh_link; | |
1304 | if (oheader->sh_info == 0) | |
1305 | oheader->sh_info = iheader->sh_info; | |
1306 | return TRUE; | |
1307 | } | |
1308 | ||
1309 | /* Allow the target a chance to decide how these fields should be set. */ | |
1310 | if (bed->elf_backend_copy_special_section_fields != NULL | |
1311 | && bed->elf_backend_copy_special_section_fields | |
1312 | (ibfd, obfd, iheader, oheader)) | |
1313 | return TRUE; | |
1314 | ||
1315 | /* We have an iheader which might match oheader, and which has non-zero | |
1316 | sh_info and/or sh_link fields. Attempt to follow those links and find | |
1317 | the section in the output bfd which corresponds to the linked section | |
1318 | in the input bfd. */ | |
1319 | if (iheader->sh_link != SHN_UNDEF) | |
1320 | { | |
1321 | sh_link = find_link (obfd, iheaders[iheader->sh_link], iheader->sh_link); | |
1322 | if (sh_link != SHN_UNDEF) | |
1323 | { | |
1324 | oheader->sh_link = sh_link; | |
1325 | changed = TRUE; | |
1326 | } | |
1327 | else | |
1328 | /* FIXME: Should we install iheader->sh_link | |
1329 | if we could not find a match ? */ | |
1330 | (* _bfd_error_handler) | |
1331 | (_("%B: Failed to find link section for section %d"), obfd, secnum); | |
1332 | } | |
1333 | ||
1334 | if (iheader->sh_info) | |
1335 | { | |
1336 | /* The sh_info field can hold arbitrary information, but if the | |
1337 | SHF_LINK_INFO flag is set then it should be interpreted as a | |
1338 | section index. */ | |
1339 | if (iheader->sh_flags & SHF_INFO_LINK) | |
1340 | { | |
1341 | sh_link = find_link (obfd, iheaders[iheader->sh_info], | |
1342 | iheader->sh_info); | |
1343 | if (sh_link != SHN_UNDEF) | |
1344 | oheader->sh_flags |= SHF_INFO_LINK; | |
1345 | } | |
1346 | else | |
1347 | /* No idea what it means - just copy it. */ | |
1348 | sh_link = iheader->sh_info; | |
1349 | ||
1350 | if (sh_link != SHN_UNDEF) | |
1351 | { | |
1352 | oheader->sh_info = sh_link; | |
1353 | changed = TRUE; | |
1354 | } | |
1355 | else | |
1356 | (* _bfd_error_handler) | |
1357 | (_("%B: Failed to find info section for section %d"), obfd, secnum); | |
1358 | } | |
1359 | ||
1360 | return changed; | |
1361 | } | |
1362 | ||
0ac4564e L |
1363 | /* Copy the program header and other data from one object module to |
1364 | another. */ | |
252b5132 | 1365 | |
b34976b6 | 1366 | bfd_boolean |
217aa764 | 1367 | _bfd_elf_copy_private_bfd_data (bfd *ibfd, bfd *obfd) |
2d502050 | 1368 | { |
5522f910 NC |
1369 | const Elf_Internal_Shdr **iheaders = (const Elf_Internal_Shdr **) elf_elfsections (ibfd); |
1370 | Elf_Internal_Shdr **oheaders = elf_elfsections (obfd); | |
1371 | const struct elf_backend_data *bed; | |
84865015 NC |
1372 | unsigned int i; |
1373 | ||
2d502050 | 1374 | if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour |
84865015 | 1375 | || bfd_get_flavour (obfd) != bfd_target_elf_flavour) |
b34976b6 | 1376 | return TRUE; |
2d502050 | 1377 | |
57b828ef L |
1378 | if (!elf_flags_init (obfd)) |
1379 | { | |
1380 | elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags; | |
1381 | elf_flags_init (obfd) = TRUE; | |
1382 | } | |
2d502050 | 1383 | |
0ac4564e | 1384 | elf_gp (obfd) = elf_gp (ibfd); |
57b828ef L |
1385 | |
1386 | /* Also copy the EI_OSABI field. */ | |
1387 | elf_elfheader (obfd)->e_ident[EI_OSABI] = | |
1388 | elf_elfheader (ibfd)->e_ident[EI_OSABI]; | |
104d59d1 | 1389 | |
5522f910 NC |
1390 | /* If set, copy the EI_ABIVERSION field. */ |
1391 | if (elf_elfheader (ibfd)->e_ident[EI_ABIVERSION]) | |
1392 | elf_elfheader (obfd)->e_ident[EI_ABIVERSION] | |
1393 | = elf_elfheader (ibfd)->e_ident[EI_ABIVERSION]; | |
1394 | ||
104d59d1 JM |
1395 | /* Copy object attributes. */ |
1396 | _bfd_elf_copy_obj_attributes (ibfd, obfd); | |
63b9bbb7 | 1397 | |
84865015 NC |
1398 | if (iheaders == NULL || oheaders == NULL) |
1399 | return TRUE; | |
63b9bbb7 | 1400 | |
5522f910 NC |
1401 | bed = get_elf_backend_data (obfd); |
1402 | ||
1403 | /* Possibly copy other fields in the section header. */ | |
84865015 | 1404 | for (i = 1; i < elf_numsections (obfd); i++) |
63b9bbb7 | 1405 | { |
84865015 NC |
1406 | unsigned int j; |
1407 | Elf_Internal_Shdr * oheader = oheaders[i]; | |
63b9bbb7 | 1408 | |
5522f910 NC |
1409 | /* Ignore ordinary sections. SHT_NOBITS sections are considered however |
1410 | because of a special case need for generating separate debug info | |
1411 | files. See below for more details. */ | |
84865015 NC |
1412 | if (oheader == NULL |
1413 | || (oheader->sh_type != SHT_NOBITS | |
5522f910 NC |
1414 | && oheader->sh_type < SHT_LOOS)) |
1415 | continue; | |
1416 | ||
1417 | /* Ignore empty sections, and sections whose | |
1418 | fields have already been initialised. */ | |
1419 | if (oheader->sh_size == 0 | |
84865015 NC |
1420 | || (oheader->sh_info != 0 && oheader->sh_link != 0)) |
1421 | continue; | |
63b9bbb7 | 1422 | |
84865015 | 1423 | /* Scan for the matching section in the input bfd. |
5522f910 NC |
1424 | First we try for a direct mapping between the input and output sections. */ |
1425 | for (j = 1; j < elf_numsections (ibfd); j++) | |
1426 | { | |
1427 | const Elf_Internal_Shdr * iheader = iheaders[j]; | |
1428 | ||
1429 | if (iheader == NULL) | |
1430 | continue; | |
1431 | ||
1432 | if (oheader->bfd_section != NULL | |
1433 | && iheader->bfd_section != NULL | |
1434 | && iheader->bfd_section->output_section != NULL | |
1435 | && iheader->bfd_section->output_section == oheader->bfd_section) | |
1436 | { | |
1437 | /* We have found a connection from the input section to the | |
1438 | output section. Attempt to copy the header fields. If | |
1439 | this fails then do not try any further sections - there | |
1440 | should only be a one-to-one mapping between input and output. */ | |
1441 | if (! copy_special_section_fields (ibfd, obfd, iheader, oheader, i)) | |
1442 | j = elf_numsections (ibfd); | |
1443 | break; | |
1444 | } | |
1445 | } | |
1446 | ||
1447 | if (j < elf_numsections (ibfd)) | |
1448 | continue; | |
1449 | ||
1450 | /* That failed. So try to deduce the corresponding input section. | |
84865015 NC |
1451 | Unfortunately we cannot compare names as the output string table |
1452 | is empty, so instead we check size, address and type. */ | |
1453 | for (j = 1; j < elf_numsections (ibfd); j++) | |
1454 | { | |
5522f910 | 1455 | const Elf_Internal_Shdr * iheader = iheaders[j]; |
84865015 | 1456 | |
5522f910 NC |
1457 | if (iheader == NULL) |
1458 | continue; | |
1459 | ||
1460 | /* Try matching fields in the input section's header. | |
1461 | Since --only-keep-debug turns all non-debug sections into | |
84865015 NC |
1462 | SHT_NOBITS sections, the output SHT_NOBITS type matches any |
1463 | input type. */ | |
1464 | if ((oheader->sh_type == SHT_NOBITS | |
1465 | || iheader->sh_type == oheader->sh_type) | |
5522f910 NC |
1466 | && (iheader->sh_flags & ~ SHF_INFO_LINK) |
1467 | == (oheader->sh_flags & ~ SHF_INFO_LINK) | |
84865015 NC |
1468 | && iheader->sh_addralign == oheader->sh_addralign |
1469 | && iheader->sh_entsize == oheader->sh_entsize | |
1470 | && iheader->sh_size == oheader->sh_size | |
1471 | && iheader->sh_addr == oheader->sh_addr | |
1472 | && (iheader->sh_info != oheader->sh_info | |
1473 | || iheader->sh_link != oheader->sh_link)) | |
63b9bbb7 | 1474 | { |
5522f910 NC |
1475 | if (copy_special_section_fields (ibfd, obfd, iheader, oheader, i)) |
1476 | break; | |
63b9bbb7 NC |
1477 | } |
1478 | } | |
5522f910 NC |
1479 | |
1480 | if (j == elf_numsections (ibfd) && oheader->sh_type >= SHT_LOOS) | |
1481 | { | |
1482 | /* Final attempt. Call the backend copy function | |
1483 | with a NULL input section. */ | |
1484 | if (bed->elf_backend_copy_special_section_fields != NULL) | |
1485 | bed->elf_backend_copy_special_section_fields (ibfd, obfd, NULL, oheader); | |
1486 | } | |
63b9bbb7 NC |
1487 | } |
1488 | ||
b34976b6 | 1489 | return TRUE; |
2d502050 L |
1490 | } |
1491 | ||
cedc298e L |
1492 | static const char * |
1493 | get_segment_type (unsigned int p_type) | |
1494 | { | |
1495 | const char *pt; | |
1496 | switch (p_type) | |
1497 | { | |
1498 | case PT_NULL: pt = "NULL"; break; | |
1499 | case PT_LOAD: pt = "LOAD"; break; | |
1500 | case PT_DYNAMIC: pt = "DYNAMIC"; break; | |
1501 | case PT_INTERP: pt = "INTERP"; break; | |
1502 | case PT_NOTE: pt = "NOTE"; break; | |
1503 | case PT_SHLIB: pt = "SHLIB"; break; | |
1504 | case PT_PHDR: pt = "PHDR"; break; | |
1505 | case PT_TLS: pt = "TLS"; break; | |
1506 | case PT_GNU_EH_FRAME: pt = "EH_FRAME"; break; | |
2b05f1b7 | 1507 | case PT_GNU_STACK: pt = "STACK"; break; |
cedc298e L |
1508 | case PT_GNU_RELRO: pt = "RELRO"; break; |
1509 | default: pt = NULL; break; | |
1510 | } | |
1511 | return pt; | |
1512 | } | |
1513 | ||
f0b79d91 L |
1514 | /* Print out the program headers. */ |
1515 | ||
b34976b6 | 1516 | bfd_boolean |
217aa764 | 1517 | _bfd_elf_print_private_bfd_data (bfd *abfd, void *farg) |
252b5132 | 1518 | { |
a50b1753 | 1519 | FILE *f = (FILE *) farg; |
252b5132 RH |
1520 | Elf_Internal_Phdr *p; |
1521 | asection *s; | |
1522 | bfd_byte *dynbuf = NULL; | |
1523 | ||
1524 | p = elf_tdata (abfd)->phdr; | |
1525 | if (p != NULL) | |
1526 | { | |
1527 | unsigned int i, c; | |
1528 | ||
1529 | fprintf (f, _("\nProgram Header:\n")); | |
1530 | c = elf_elfheader (abfd)->e_phnum; | |
1531 | for (i = 0; i < c; i++, p++) | |
1532 | { | |
cedc298e | 1533 | const char *pt = get_segment_type (p->p_type); |
252b5132 RH |
1534 | char buf[20]; |
1535 | ||
cedc298e | 1536 | if (pt == NULL) |
252b5132 | 1537 | { |
cedc298e L |
1538 | sprintf (buf, "0x%lx", p->p_type); |
1539 | pt = buf; | |
252b5132 | 1540 | } |
dc810e39 | 1541 | fprintf (f, "%8s off 0x", pt); |
60b89a18 | 1542 | bfd_fprintf_vma (abfd, f, p->p_offset); |
252b5132 | 1543 | fprintf (f, " vaddr 0x"); |
60b89a18 | 1544 | bfd_fprintf_vma (abfd, f, p->p_vaddr); |
252b5132 | 1545 | fprintf (f, " paddr 0x"); |
60b89a18 | 1546 | bfd_fprintf_vma (abfd, f, p->p_paddr); |
252b5132 RH |
1547 | fprintf (f, " align 2**%u\n", bfd_log2 (p->p_align)); |
1548 | fprintf (f, " filesz 0x"); | |
60b89a18 | 1549 | bfd_fprintf_vma (abfd, f, p->p_filesz); |
252b5132 | 1550 | fprintf (f, " memsz 0x"); |
60b89a18 | 1551 | bfd_fprintf_vma (abfd, f, p->p_memsz); |
252b5132 RH |
1552 | fprintf (f, " flags %c%c%c", |
1553 | (p->p_flags & PF_R) != 0 ? 'r' : '-', | |
1554 | (p->p_flags & PF_W) != 0 ? 'w' : '-', | |
1555 | (p->p_flags & PF_X) != 0 ? 'x' : '-'); | |
dc810e39 AM |
1556 | if ((p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X)) != 0) |
1557 | fprintf (f, " %lx", p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X)); | |
252b5132 RH |
1558 | fprintf (f, "\n"); |
1559 | } | |
1560 | } | |
1561 | ||
1562 | s = bfd_get_section_by_name (abfd, ".dynamic"); | |
1563 | if (s != NULL) | |
1564 | { | |
cb33740c | 1565 | unsigned int elfsec; |
dc810e39 | 1566 | unsigned long shlink; |
252b5132 RH |
1567 | bfd_byte *extdyn, *extdynend; |
1568 | size_t extdynsize; | |
217aa764 | 1569 | void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *); |
252b5132 RH |
1570 | |
1571 | fprintf (f, _("\nDynamic Section:\n")); | |
1572 | ||
eea6121a | 1573 | if (!bfd_malloc_and_get_section (abfd, s, &dynbuf)) |
252b5132 RH |
1574 | goto error_return; |
1575 | ||
1576 | elfsec = _bfd_elf_section_from_bfd_section (abfd, s); | |
cb33740c | 1577 | if (elfsec == SHN_BAD) |
252b5132 | 1578 | goto error_return; |
dc810e39 | 1579 | shlink = elf_elfsections (abfd)[elfsec]->sh_link; |
252b5132 RH |
1580 | |
1581 | extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn; | |
1582 | swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in; | |
1583 | ||
1584 | extdyn = dynbuf; | |
06614111 NC |
1585 | /* PR 17512: file: 6f427532. */ |
1586 | if (s->size < extdynsize) | |
1587 | goto error_return; | |
eea6121a | 1588 | extdynend = extdyn + s->size; |
1036838a NC |
1589 | /* PR 17512: file: id:000006,sig:06,src:000000,op:flip4,pos:5664. |
1590 | Fix range check. */ | |
1591 | for (; extdyn <= (extdynend - extdynsize); extdyn += extdynsize) | |
252b5132 RH |
1592 | { |
1593 | Elf_Internal_Dyn dyn; | |
ad9563d6 | 1594 | const char *name = ""; |
252b5132 | 1595 | char ab[20]; |
b34976b6 | 1596 | bfd_boolean stringp; |
ad9563d6 | 1597 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
252b5132 | 1598 | |
217aa764 | 1599 | (*swap_dyn_in) (abfd, extdyn, &dyn); |
252b5132 RH |
1600 | |
1601 | if (dyn.d_tag == DT_NULL) | |
1602 | break; | |
1603 | ||
b34976b6 | 1604 | stringp = FALSE; |
252b5132 RH |
1605 | switch (dyn.d_tag) |
1606 | { | |
1607 | default: | |
ad9563d6 CM |
1608 | if (bed->elf_backend_get_target_dtag) |
1609 | name = (*bed->elf_backend_get_target_dtag) (dyn.d_tag); | |
1610 | ||
1611 | if (!strcmp (name, "")) | |
1612 | { | |
1613 | sprintf (ab, "0x%lx", (unsigned long) dyn.d_tag); | |
1614 | name = ab; | |
1615 | } | |
252b5132 RH |
1616 | break; |
1617 | ||
b34976b6 | 1618 | case DT_NEEDED: name = "NEEDED"; stringp = TRUE; break; |
252b5132 RH |
1619 | case DT_PLTRELSZ: name = "PLTRELSZ"; break; |
1620 | case DT_PLTGOT: name = "PLTGOT"; break; | |
1621 | case DT_HASH: name = "HASH"; break; | |
1622 | case DT_STRTAB: name = "STRTAB"; break; | |
1623 | case DT_SYMTAB: name = "SYMTAB"; break; | |
1624 | case DT_RELA: name = "RELA"; break; | |
1625 | case DT_RELASZ: name = "RELASZ"; break; | |
1626 | case DT_RELAENT: name = "RELAENT"; break; | |
1627 | case DT_STRSZ: name = "STRSZ"; break; | |
1628 | case DT_SYMENT: name = "SYMENT"; break; | |
1629 | case DT_INIT: name = "INIT"; break; | |
1630 | case DT_FINI: name = "FINI"; break; | |
b34976b6 AM |
1631 | case DT_SONAME: name = "SONAME"; stringp = TRUE; break; |
1632 | case DT_RPATH: name = "RPATH"; stringp = TRUE; break; | |
252b5132 RH |
1633 | case DT_SYMBOLIC: name = "SYMBOLIC"; break; |
1634 | case DT_REL: name = "REL"; break; | |
1635 | case DT_RELSZ: name = "RELSZ"; break; | |
1636 | case DT_RELENT: name = "RELENT"; break; | |
1637 | case DT_PLTREL: name = "PLTREL"; break; | |
1638 | case DT_DEBUG: name = "DEBUG"; break; | |
1639 | case DT_TEXTREL: name = "TEXTREL"; break; | |
1640 | case DT_JMPREL: name = "JMPREL"; break; | |
94558834 L |
1641 | case DT_BIND_NOW: name = "BIND_NOW"; break; |
1642 | case DT_INIT_ARRAY: name = "INIT_ARRAY"; break; | |
1643 | case DT_FINI_ARRAY: name = "FINI_ARRAY"; break; | |
1644 | case DT_INIT_ARRAYSZ: name = "INIT_ARRAYSZ"; break; | |
1645 | case DT_FINI_ARRAYSZ: name = "FINI_ARRAYSZ"; break; | |
b34976b6 | 1646 | case DT_RUNPATH: name = "RUNPATH"; stringp = TRUE; break; |
94558834 L |
1647 | case DT_FLAGS: name = "FLAGS"; break; |
1648 | case DT_PREINIT_ARRAY: name = "PREINIT_ARRAY"; break; | |
1649 | case DT_PREINIT_ARRAYSZ: name = "PREINIT_ARRAYSZ"; break; | |
d48188b9 | 1650 | case DT_CHECKSUM: name = "CHECKSUM"; break; |
94558834 L |
1651 | case DT_PLTPADSZ: name = "PLTPADSZ"; break; |
1652 | case DT_MOVEENT: name = "MOVEENT"; break; | |
1653 | case DT_MOVESZ: name = "MOVESZ"; break; | |
1654 | case DT_FEATURE: name = "FEATURE"; break; | |
1655 | case DT_POSFLAG_1: name = "POSFLAG_1"; break; | |
1656 | case DT_SYMINSZ: name = "SYMINSZ"; break; | |
1657 | case DT_SYMINENT: name = "SYMINENT"; break; | |
b34976b6 AM |
1658 | case DT_CONFIG: name = "CONFIG"; stringp = TRUE; break; |
1659 | case DT_DEPAUDIT: name = "DEPAUDIT"; stringp = TRUE; break; | |
1660 | case DT_AUDIT: name = "AUDIT"; stringp = TRUE; break; | |
94558834 L |
1661 | case DT_PLTPAD: name = "PLTPAD"; break; |
1662 | case DT_MOVETAB: name = "MOVETAB"; break; | |
1663 | case DT_SYMINFO: name = "SYMINFO"; break; | |
1664 | case DT_RELACOUNT: name = "RELACOUNT"; break; | |
1665 | case DT_RELCOUNT: name = "RELCOUNT"; break; | |
1666 | case DT_FLAGS_1: name = "FLAGS_1"; break; | |
252b5132 RH |
1667 | case DT_VERSYM: name = "VERSYM"; break; |
1668 | case DT_VERDEF: name = "VERDEF"; break; | |
1669 | case DT_VERDEFNUM: name = "VERDEFNUM"; break; | |
1670 | case DT_VERNEED: name = "VERNEED"; break; | |
1671 | case DT_VERNEEDNUM: name = "VERNEEDNUM"; break; | |
b34976b6 | 1672 | case DT_AUXILIARY: name = "AUXILIARY"; stringp = TRUE; break; |
94558834 | 1673 | case DT_USED: name = "USED"; break; |
b34976b6 | 1674 | case DT_FILTER: name = "FILTER"; stringp = TRUE; break; |
fdc90cb4 | 1675 | case DT_GNU_HASH: name = "GNU_HASH"; break; |
252b5132 RH |
1676 | } |
1677 | ||
ad9563d6 | 1678 | fprintf (f, " %-20s ", name); |
252b5132 | 1679 | if (! stringp) |
a1f3c56e AN |
1680 | { |
1681 | fprintf (f, "0x"); | |
1682 | bfd_fprintf_vma (abfd, f, dyn.d_un.d_val); | |
1683 | } | |
252b5132 RH |
1684 | else |
1685 | { | |
1686 | const char *string; | |
dc810e39 | 1687 | unsigned int tagv = dyn.d_un.d_val; |
252b5132 | 1688 | |
dc810e39 | 1689 | string = bfd_elf_string_from_elf_section (abfd, shlink, tagv); |
252b5132 RH |
1690 | if (string == NULL) |
1691 | goto error_return; | |
1692 | fprintf (f, "%s", string); | |
1693 | } | |
1694 | fprintf (f, "\n"); | |
1695 | } | |
1696 | ||
1697 | free (dynbuf); | |
1698 | dynbuf = NULL; | |
1699 | } | |
1700 | ||
1701 | if ((elf_dynverdef (abfd) != 0 && elf_tdata (abfd)->verdef == NULL) | |
1702 | || (elf_dynverref (abfd) != 0 && elf_tdata (abfd)->verref == NULL)) | |
1703 | { | |
fc0e6df6 | 1704 | if (! _bfd_elf_slurp_version_tables (abfd, FALSE)) |
b34976b6 | 1705 | return FALSE; |
252b5132 RH |
1706 | } |
1707 | ||
1708 | if (elf_dynverdef (abfd) != 0) | |
1709 | { | |
1710 | Elf_Internal_Verdef *t; | |
1711 | ||
1712 | fprintf (f, _("\nVersion definitions:\n")); | |
1713 | for (t = elf_tdata (abfd)->verdef; t != NULL; t = t->vd_nextdef) | |
1714 | { | |
1715 | fprintf (f, "%d 0x%2.2x 0x%8.8lx %s\n", t->vd_ndx, | |
d0fb9a8d JJ |
1716 | t->vd_flags, t->vd_hash, |
1717 | t->vd_nodename ? t->vd_nodename : "<corrupt>"); | |
1718 | if (t->vd_auxptr != NULL && t->vd_auxptr->vda_nextptr != NULL) | |
252b5132 RH |
1719 | { |
1720 | Elf_Internal_Verdaux *a; | |
1721 | ||
1722 | fprintf (f, "\t"); | |
1723 | for (a = t->vd_auxptr->vda_nextptr; | |
1724 | a != NULL; | |
1725 | a = a->vda_nextptr) | |
d0fb9a8d JJ |
1726 | fprintf (f, "%s ", |
1727 | a->vda_nodename ? a->vda_nodename : "<corrupt>"); | |
252b5132 RH |
1728 | fprintf (f, "\n"); |
1729 | } | |
1730 | } | |
1731 | } | |
1732 | ||
1733 | if (elf_dynverref (abfd) != 0) | |
1734 | { | |
1735 | Elf_Internal_Verneed *t; | |
1736 | ||
1737 | fprintf (f, _("\nVersion References:\n")); | |
1738 | for (t = elf_tdata (abfd)->verref; t != NULL; t = t->vn_nextref) | |
1739 | { | |
1740 | Elf_Internal_Vernaux *a; | |
1741 | ||
d0fb9a8d JJ |
1742 | fprintf (f, _(" required from %s:\n"), |
1743 | t->vn_filename ? t->vn_filename : "<corrupt>"); | |
252b5132 RH |
1744 | for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) |
1745 | fprintf (f, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a->vna_hash, | |
d0fb9a8d JJ |
1746 | a->vna_flags, a->vna_other, |
1747 | a->vna_nodename ? a->vna_nodename : "<corrupt>"); | |
252b5132 RH |
1748 | } |
1749 | } | |
1750 | ||
b34976b6 | 1751 | return TRUE; |
252b5132 RH |
1752 | |
1753 | error_return: | |
1754 | if (dynbuf != NULL) | |
1755 | free (dynbuf); | |
b34976b6 | 1756 | return FALSE; |
252b5132 RH |
1757 | } |
1758 | ||
bb4d2ac2 L |
1759 | /* Get version string. */ |
1760 | ||
1761 | const char * | |
60bb06bc L |
1762 | _bfd_elf_get_symbol_version_string (bfd *abfd, asymbol *symbol, |
1763 | bfd_boolean *hidden) | |
bb4d2ac2 L |
1764 | { |
1765 | const char *version_string = NULL; | |
1766 | if (elf_dynversym (abfd) != 0 | |
1767 | && (elf_dynverdef (abfd) != 0 || elf_dynverref (abfd) != 0)) | |
1768 | { | |
1769 | unsigned int vernum = ((elf_symbol_type *) symbol)->version; | |
1770 | ||
1771 | *hidden = (vernum & VERSYM_HIDDEN) != 0; | |
1772 | vernum &= VERSYM_VERSION; | |
1773 | ||
1774 | if (vernum == 0) | |
1775 | version_string = ""; | |
1776 | else if (vernum == 1) | |
1777 | version_string = "Base"; | |
1778 | else if (vernum <= elf_tdata (abfd)->cverdefs) | |
1779 | version_string = | |
1780 | elf_tdata (abfd)->verdef[vernum - 1].vd_nodename; | |
1781 | else | |
1782 | { | |
1783 | Elf_Internal_Verneed *t; | |
1784 | ||
1785 | version_string = ""; | |
1786 | for (t = elf_tdata (abfd)->verref; | |
1787 | t != NULL; | |
1788 | t = t->vn_nextref) | |
1789 | { | |
1790 | Elf_Internal_Vernaux *a; | |
1791 | ||
1792 | for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) | |
1793 | { | |
1794 | if (a->vna_other == vernum) | |
1795 | { | |
1796 | version_string = a->vna_nodename; | |
1797 | break; | |
1798 | } | |
1799 | } | |
1800 | } | |
1801 | } | |
1802 | } | |
1803 | return version_string; | |
1804 | } | |
1805 | ||
252b5132 RH |
1806 | /* Display ELF-specific fields of a symbol. */ |
1807 | ||
1808 | void | |
217aa764 AM |
1809 | bfd_elf_print_symbol (bfd *abfd, |
1810 | void *filep, | |
1811 | asymbol *symbol, | |
1812 | bfd_print_symbol_type how) | |
252b5132 | 1813 | { |
a50b1753 | 1814 | FILE *file = (FILE *) filep; |
252b5132 RH |
1815 | switch (how) |
1816 | { | |
1817 | case bfd_print_symbol_name: | |
1818 | fprintf (file, "%s", symbol->name); | |
1819 | break; | |
1820 | case bfd_print_symbol_more: | |
1821 | fprintf (file, "elf "); | |
60b89a18 | 1822 | bfd_fprintf_vma (abfd, file, symbol->value); |
0af1713e | 1823 | fprintf (file, " %lx", (unsigned long) symbol->flags); |
252b5132 RH |
1824 | break; |
1825 | case bfd_print_symbol_all: | |
1826 | { | |
4e8a9624 AM |
1827 | const char *section_name; |
1828 | const char *name = NULL; | |
9c5bfbb7 | 1829 | const struct elf_backend_data *bed; |
7a13edea | 1830 | unsigned char st_other; |
dbb410c3 | 1831 | bfd_vma val; |
bb4d2ac2 L |
1832 | const char *version_string; |
1833 | bfd_boolean hidden; | |
c044fabd | 1834 | |
252b5132 | 1835 | section_name = symbol->section ? symbol->section->name : "(*none*)"; |
587ff49e RH |
1836 | |
1837 | bed = get_elf_backend_data (abfd); | |
1838 | if (bed->elf_backend_print_symbol_all) | |
c044fabd | 1839 | name = (*bed->elf_backend_print_symbol_all) (abfd, filep, symbol); |
587ff49e RH |
1840 | |
1841 | if (name == NULL) | |
1842 | { | |
7ee38065 | 1843 | name = symbol->name; |
217aa764 | 1844 | bfd_print_symbol_vandf (abfd, file, symbol); |
587ff49e RH |
1845 | } |
1846 | ||
252b5132 RH |
1847 | fprintf (file, " %s\t", section_name); |
1848 | /* Print the "other" value for a symbol. For common symbols, | |
1849 | we've already printed the size; now print the alignment. | |
1850 | For other symbols, we have no specified alignment, and | |
1851 | we've printed the address; now print the size. */ | |
dcf6c779 | 1852 | if (symbol->section && bfd_is_com_section (symbol->section)) |
dbb410c3 AM |
1853 | val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_value; |
1854 | else | |
1855 | val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_size; | |
1856 | bfd_fprintf_vma (abfd, file, val); | |
252b5132 RH |
1857 | |
1858 | /* If we have version information, print it. */ | |
60bb06bc L |
1859 | version_string = _bfd_elf_get_symbol_version_string (abfd, |
1860 | symbol, | |
1861 | &hidden); | |
bb4d2ac2 | 1862 | if (version_string) |
252b5132 | 1863 | { |
bb4d2ac2 | 1864 | if (!hidden) |
252b5132 RH |
1865 | fprintf (file, " %-11s", version_string); |
1866 | else | |
1867 | { | |
1868 | int i; | |
1869 | ||
1870 | fprintf (file, " (%s)", version_string); | |
1871 | for (i = 10 - strlen (version_string); i > 0; --i) | |
1872 | putc (' ', file); | |
1873 | } | |
1874 | } | |
1875 | ||
1876 | /* If the st_other field is not zero, print it. */ | |
7a13edea | 1877 | st_other = ((elf_symbol_type *) symbol)->internal_elf_sym.st_other; |
c044fabd | 1878 | |
7a13edea NC |
1879 | switch (st_other) |
1880 | { | |
1881 | case 0: break; | |
1882 | case STV_INTERNAL: fprintf (file, " .internal"); break; | |
1883 | case STV_HIDDEN: fprintf (file, " .hidden"); break; | |
1884 | case STV_PROTECTED: fprintf (file, " .protected"); break; | |
1885 | default: | |
1886 | /* Some other non-defined flags are also present, so print | |
1887 | everything hex. */ | |
1888 | fprintf (file, " 0x%02x", (unsigned int) st_other); | |
1889 | } | |
252b5132 | 1890 | |
587ff49e | 1891 | fprintf (file, " %s", name); |
252b5132 RH |
1892 | } |
1893 | break; | |
1894 | } | |
1895 | } | |
252b5132 RH |
1896 | \f |
1897 | /* ELF .o/exec file reading */ | |
1898 | ||
c044fabd | 1899 | /* Create a new bfd section from an ELF section header. */ |
252b5132 | 1900 | |
b34976b6 | 1901 | bfd_boolean |
217aa764 | 1902 | bfd_section_from_shdr (bfd *abfd, unsigned int shindex) |
252b5132 | 1903 | { |
4fbb74a6 AM |
1904 | Elf_Internal_Shdr *hdr; |
1905 | Elf_Internal_Ehdr *ehdr; | |
1906 | const struct elf_backend_data *bed; | |
90937f86 | 1907 | const char *name; |
bf67003b NC |
1908 | bfd_boolean ret = TRUE; |
1909 | static bfd_boolean * sections_being_created = NULL; | |
5a4b0ccc | 1910 | static bfd * sections_being_created_abfd = NULL; |
bf67003b | 1911 | static unsigned int nesting = 0; |
252b5132 | 1912 | |
4fbb74a6 AM |
1913 | if (shindex >= elf_numsections (abfd)) |
1914 | return FALSE; | |
1915 | ||
bf67003b NC |
1916 | if (++ nesting > 3) |
1917 | { | |
1918 | /* PR17512: A corrupt ELF binary might contain a recursive group of | |
06614111 | 1919 | sections, with each the string indicies pointing to the next in the |
bf67003b NC |
1920 | loop. Detect this here, by refusing to load a section that we are |
1921 | already in the process of loading. We only trigger this test if | |
1922 | we have nested at least three sections deep as normal ELF binaries | |
5a4b0ccc NC |
1923 | can expect to recurse at least once. |
1924 | ||
1925 | FIXME: It would be better if this array was attached to the bfd, | |
1926 | rather than being held in a static pointer. */ | |
1927 | ||
1928 | if (sections_being_created_abfd != abfd) | |
1929 | sections_being_created = NULL; | |
bf67003b NC |
1930 | if (sections_being_created == NULL) |
1931 | { | |
1932 | /* FIXME: It would be more efficient to attach this array to the bfd somehow. */ | |
1933 | sections_being_created = (bfd_boolean *) | |
1934 | bfd_zalloc (abfd, elf_numsections (abfd) * sizeof (bfd_boolean)); | |
5a4b0ccc | 1935 | sections_being_created_abfd = abfd; |
bf67003b NC |
1936 | } |
1937 | if (sections_being_created [shindex]) | |
1938 | { | |
1939 | (*_bfd_error_handler) | |
1940 | (_("%B: warning: loop in section dependencies detected"), abfd); | |
1941 | return FALSE; | |
1942 | } | |
1943 | sections_being_created [shindex] = TRUE; | |
1944 | } | |
1945 | ||
4fbb74a6 AM |
1946 | hdr = elf_elfsections (abfd)[shindex]; |
1947 | ehdr = elf_elfheader (abfd); | |
1948 | name = bfd_elf_string_from_elf_section (abfd, ehdr->e_shstrndx, | |
1b3a8575 | 1949 | hdr->sh_name); |
933d961a | 1950 | if (name == NULL) |
bf67003b | 1951 | goto fail; |
252b5132 | 1952 | |
4fbb74a6 | 1953 | bed = get_elf_backend_data (abfd); |
252b5132 RH |
1954 | switch (hdr->sh_type) |
1955 | { | |
1956 | case SHT_NULL: | |
1957 | /* Inactive section. Throw it away. */ | |
bf67003b | 1958 | goto success; |
252b5132 | 1959 | |
bf67003b NC |
1960 | case SHT_PROGBITS: /* Normal section with contents. */ |
1961 | case SHT_NOBITS: /* .bss section. */ | |
1962 | case SHT_HASH: /* .hash section. */ | |
1963 | case SHT_NOTE: /* .note section. */ | |
25e27870 L |
1964 | case SHT_INIT_ARRAY: /* .init_array section. */ |
1965 | case SHT_FINI_ARRAY: /* .fini_array section. */ | |
1966 | case SHT_PREINIT_ARRAY: /* .preinit_array section. */ | |
7f1204bb | 1967 | case SHT_GNU_LIBLIST: /* .gnu.liblist section. */ |
fdc90cb4 | 1968 | case SHT_GNU_HASH: /* .gnu.hash section. */ |
bf67003b NC |
1969 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); |
1970 | goto success; | |
252b5132 | 1971 | |
797fc050 | 1972 | case SHT_DYNAMIC: /* Dynamic linking information. */ |
6dc132d9 | 1973 | if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex)) |
bf67003b NC |
1974 | goto fail; |
1975 | ||
cfcac11d NC |
1976 | if (hdr->sh_link > elf_numsections (abfd)) |
1977 | { | |
caa83f8b | 1978 | /* PR 10478: Accept Solaris binaries with a sh_link |
cfcac11d NC |
1979 | field set to SHN_BEFORE or SHN_AFTER. */ |
1980 | switch (bfd_get_arch (abfd)) | |
1981 | { | |
caa83f8b | 1982 | case bfd_arch_i386: |
cfcac11d NC |
1983 | case bfd_arch_sparc: |
1984 | if (hdr->sh_link == (SHN_LORESERVE & 0xffff) /* SHN_BEFORE */ | |
1985 | || hdr->sh_link == ((SHN_LORESERVE + 1) & 0xffff) /* SHN_AFTER */) | |
1986 | break; | |
1987 | /* Otherwise fall through. */ | |
1988 | default: | |
bf67003b | 1989 | goto fail; |
cfcac11d NC |
1990 | } |
1991 | } | |
1992 | else if (elf_elfsections (abfd)[hdr->sh_link] == NULL) | |
bf67003b | 1993 | goto fail; |
cfcac11d | 1994 | else if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_STRTAB) |
797fc050 AM |
1995 | { |
1996 | Elf_Internal_Shdr *dynsymhdr; | |
1997 | ||
1998 | /* The shared libraries distributed with hpux11 have a bogus | |
1999 | sh_link field for the ".dynamic" section. Find the | |
2000 | string table for the ".dynsym" section instead. */ | |
2001 | if (elf_dynsymtab (abfd) != 0) | |
2002 | { | |
2003 | dynsymhdr = elf_elfsections (abfd)[elf_dynsymtab (abfd)]; | |
2004 | hdr->sh_link = dynsymhdr->sh_link; | |
2005 | } | |
2006 | else | |
2007 | { | |
2008 | unsigned int i, num_sec; | |
2009 | ||
2010 | num_sec = elf_numsections (abfd); | |
2011 | for (i = 1; i < num_sec; i++) | |
2012 | { | |
2013 | dynsymhdr = elf_elfsections (abfd)[i]; | |
2014 | if (dynsymhdr->sh_type == SHT_DYNSYM) | |
2015 | { | |
2016 | hdr->sh_link = dynsymhdr->sh_link; | |
2017 | break; | |
2018 | } | |
2019 | } | |
2020 | } | |
2021 | } | |
bf67003b | 2022 | goto success; |
797fc050 | 2023 | |
bf67003b | 2024 | case SHT_SYMTAB: /* A symbol table. */ |
252b5132 | 2025 | if (elf_onesymtab (abfd) == shindex) |
bf67003b | 2026 | goto success; |
252b5132 | 2027 | |
a50b2160 | 2028 | if (hdr->sh_entsize != bed->s->sizeof_sym) |
bf67003b NC |
2029 | goto fail; |
2030 | ||
3337c1e5 | 2031 | if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size) |
eee3b786 AM |
2032 | { |
2033 | if (hdr->sh_size != 0) | |
bf67003b | 2034 | goto fail; |
eee3b786 AM |
2035 | /* Some assemblers erroneously set sh_info to one with a |
2036 | zero sh_size. ld sees this as a global symbol count | |
2037 | of (unsigned) -1. Fix it here. */ | |
2038 | hdr->sh_info = 0; | |
bf67003b | 2039 | goto success; |
eee3b786 | 2040 | } |
bf67003b | 2041 | |
16ad13ec NC |
2042 | /* PR 18854: A binary might contain more than one symbol table. |
2043 | Unusual, but possible. Warn, but continue. */ | |
2044 | if (elf_onesymtab (abfd) != 0) | |
2045 | { | |
2046 | (*_bfd_error_handler) | |
2047 | (_("%B: warning: multiple symbol tables detected - ignoring the table in section %u"), | |
2048 | abfd, shindex); | |
2049 | goto success; | |
2050 | } | |
252b5132 | 2051 | elf_onesymtab (abfd) = shindex; |
6a40cf0c NC |
2052 | elf_symtab_hdr (abfd) = *hdr; |
2053 | elf_elfsections (abfd)[shindex] = hdr = & elf_symtab_hdr (abfd); | |
252b5132 RH |
2054 | abfd->flags |= HAS_SYMS; |
2055 | ||
2056 | /* Sometimes a shared object will map in the symbol table. If | |
08a40648 AM |
2057 | SHF_ALLOC is set, and this is a shared object, then we also |
2058 | treat this section as a BFD section. We can not base the | |
2059 | decision purely on SHF_ALLOC, because that flag is sometimes | |
2060 | set in a relocatable object file, which would confuse the | |
2061 | linker. */ | |
252b5132 RH |
2062 | if ((hdr->sh_flags & SHF_ALLOC) != 0 |
2063 | && (abfd->flags & DYNAMIC) != 0 | |
6dc132d9 L |
2064 | && ! _bfd_elf_make_section_from_shdr (abfd, hdr, name, |
2065 | shindex)) | |
bf67003b | 2066 | goto fail; |
252b5132 | 2067 | |
1b3a8575 AM |
2068 | /* Go looking for SHT_SYMTAB_SHNDX too, since if there is one we |
2069 | can't read symbols without that section loaded as well. It | |
2070 | is most likely specified by the next section header. */ | |
6a40cf0c NC |
2071 | { |
2072 | elf_section_list * entry; | |
2073 | unsigned int i, num_sec; | |
1b3a8575 | 2074 | |
6a40cf0c NC |
2075 | for (entry = elf_symtab_shndx_list (abfd); entry != NULL; entry = entry->next) |
2076 | if (entry->hdr.sh_link == shindex) | |
2077 | goto success; | |
2078 | ||
2079 | num_sec = elf_numsections (abfd); | |
2080 | for (i = shindex + 1; i < num_sec; i++) | |
2081 | { | |
2082 | Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i]; | |
2083 | ||
2084 | if (hdr2->sh_type == SHT_SYMTAB_SHNDX | |
2085 | && hdr2->sh_link == shindex) | |
2086 | break; | |
2087 | } | |
2088 | ||
2089 | if (i == num_sec) | |
2090 | for (i = 1; i < shindex; i++) | |
1b3a8575 AM |
2091 | { |
2092 | Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i]; | |
6a40cf0c | 2093 | |
1b3a8575 AM |
2094 | if (hdr2->sh_type == SHT_SYMTAB_SHNDX |
2095 | && hdr2->sh_link == shindex) | |
2096 | break; | |
2097 | } | |
6a40cf0c NC |
2098 | |
2099 | if (i != shindex) | |
2100 | ret = bfd_section_from_shdr (abfd, i); | |
2101 | /* else FIXME: we have failed to find the symbol table - should we issue an error ? */ | |
2102 | goto success; | |
2103 | } | |
252b5132 | 2104 | |
bf67003b | 2105 | case SHT_DYNSYM: /* A dynamic symbol table. */ |
252b5132 | 2106 | if (elf_dynsymtab (abfd) == shindex) |
bf67003b | 2107 | goto success; |
252b5132 | 2108 | |
a50b2160 | 2109 | if (hdr->sh_entsize != bed->s->sizeof_sym) |
bf67003b NC |
2110 | goto fail; |
2111 | ||
eee3b786 AM |
2112 | if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size) |
2113 | { | |
2114 | if (hdr->sh_size != 0) | |
bf67003b NC |
2115 | goto fail; |
2116 | ||
eee3b786 AM |
2117 | /* Some linkers erroneously set sh_info to one with a |
2118 | zero sh_size. ld sees this as a global symbol count | |
2119 | of (unsigned) -1. Fix it here. */ | |
2120 | hdr->sh_info = 0; | |
bf67003b | 2121 | goto success; |
eee3b786 | 2122 | } |
bf67003b | 2123 | |
16ad13ec NC |
2124 | /* PR 18854: A binary might contain more than one dynamic symbol table. |
2125 | Unusual, but possible. Warn, but continue. */ | |
2126 | if (elf_dynsymtab (abfd) != 0) | |
2127 | { | |
2128 | (*_bfd_error_handler) | |
2129 | (_("%B: warning: multiple dynamic symbol tables detected - ignoring the table in section %u"), | |
2130 | abfd, shindex); | |
2131 | goto success; | |
2132 | } | |
252b5132 RH |
2133 | elf_dynsymtab (abfd) = shindex; |
2134 | elf_tdata (abfd)->dynsymtab_hdr = *hdr; | |
2135 | elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->dynsymtab_hdr; | |
2136 | abfd->flags |= HAS_SYMS; | |
2137 | ||
2138 | /* Besides being a symbol table, we also treat this as a regular | |
2139 | section, so that objcopy can handle it. */ | |
bf67003b NC |
2140 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); |
2141 | goto success; | |
252b5132 | 2142 | |
bf67003b | 2143 | case SHT_SYMTAB_SHNDX: /* Symbol section indices when >64k sections. */ |
6a40cf0c NC |
2144 | { |
2145 | elf_section_list * entry; | |
9ad5cbcf | 2146 | |
6a40cf0c NC |
2147 | for (entry = elf_symtab_shndx_list (abfd); entry != NULL; entry = entry->next) |
2148 | if (entry->ndx == shindex) | |
2149 | goto success; | |
2150 | ||
2151 | entry = bfd_alloc (abfd, sizeof * entry); | |
2152 | if (entry == NULL) | |
2153 | goto fail; | |
2154 | entry->ndx = shindex; | |
2155 | entry->hdr = * hdr; | |
2156 | entry->next = elf_symtab_shndx_list (abfd); | |
2157 | elf_symtab_shndx_list (abfd) = entry; | |
2158 | elf_elfsections (abfd)[shindex] = & entry->hdr; | |
2159 | goto success; | |
2160 | } | |
9ad5cbcf | 2161 | |
bf67003b | 2162 | case SHT_STRTAB: /* A string table. */ |
252b5132 | 2163 | if (hdr->bfd_section != NULL) |
bf67003b NC |
2164 | goto success; |
2165 | ||
252b5132 RH |
2166 | if (ehdr->e_shstrndx == shindex) |
2167 | { | |
2168 | elf_tdata (abfd)->shstrtab_hdr = *hdr; | |
2169 | elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->shstrtab_hdr; | |
bf67003b | 2170 | goto success; |
252b5132 | 2171 | } |
bf67003b | 2172 | |
1b3a8575 AM |
2173 | if (elf_elfsections (abfd)[elf_onesymtab (abfd)]->sh_link == shindex) |
2174 | { | |
2175 | symtab_strtab: | |
2176 | elf_tdata (abfd)->strtab_hdr = *hdr; | |
2177 | elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->strtab_hdr; | |
bf67003b | 2178 | goto success; |
1b3a8575 | 2179 | } |
bf67003b | 2180 | |
1b3a8575 AM |
2181 | if (elf_elfsections (abfd)[elf_dynsymtab (abfd)]->sh_link == shindex) |
2182 | { | |
2183 | dynsymtab_strtab: | |
2184 | elf_tdata (abfd)->dynstrtab_hdr = *hdr; | |
2185 | hdr = &elf_tdata (abfd)->dynstrtab_hdr; | |
2186 | elf_elfsections (abfd)[shindex] = hdr; | |
2187 | /* We also treat this as a regular section, so that objcopy | |
2188 | can handle it. */ | |
bf67003b NC |
2189 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, |
2190 | shindex); | |
2191 | goto success; | |
1b3a8575 | 2192 | } |
252b5132 | 2193 | |
1b3a8575 AM |
2194 | /* If the string table isn't one of the above, then treat it as a |
2195 | regular section. We need to scan all the headers to be sure, | |
2196 | just in case this strtab section appeared before the above. */ | |
2197 | if (elf_onesymtab (abfd) == 0 || elf_dynsymtab (abfd) == 0) | |
2198 | { | |
2199 | unsigned int i, num_sec; | |
252b5132 | 2200 | |
1b3a8575 AM |
2201 | num_sec = elf_numsections (abfd); |
2202 | for (i = 1; i < num_sec; i++) | |
2203 | { | |
2204 | Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i]; | |
2205 | if (hdr2->sh_link == shindex) | |
2206 | { | |
933d961a JJ |
2207 | /* Prevent endless recursion on broken objects. */ |
2208 | if (i == shindex) | |
bf67003b | 2209 | goto fail; |
1b3a8575 | 2210 | if (! bfd_section_from_shdr (abfd, i)) |
bf67003b | 2211 | goto fail; |
1b3a8575 AM |
2212 | if (elf_onesymtab (abfd) == i) |
2213 | goto symtab_strtab; | |
2214 | if (elf_dynsymtab (abfd) == i) | |
2215 | goto dynsymtab_strtab; | |
2216 | } | |
2217 | } | |
2218 | } | |
bf67003b NC |
2219 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); |
2220 | goto success; | |
252b5132 RH |
2221 | |
2222 | case SHT_REL: | |
2223 | case SHT_RELA: | |
2224 | /* *These* do a lot of work -- but build no sections! */ | |
2225 | { | |
2226 | asection *target_sect; | |
d4730f92 | 2227 | Elf_Internal_Shdr *hdr2, **p_hdr; |
9ad5cbcf | 2228 | unsigned int num_sec = elf_numsections (abfd); |
d4730f92 BS |
2229 | struct bfd_elf_section_data *esdt; |
2230 | bfd_size_type amt; | |
252b5132 | 2231 | |
aa2ca951 JJ |
2232 | if (hdr->sh_entsize |
2233 | != (bfd_size_type) (hdr->sh_type == SHT_REL | |
a50b2160 | 2234 | ? bed->s->sizeof_rel : bed->s->sizeof_rela)) |
bf67003b | 2235 | goto fail; |
a50b2160 | 2236 | |
03ae5f59 | 2237 | /* Check for a bogus link to avoid crashing. */ |
4fbb74a6 | 2238 | if (hdr->sh_link >= num_sec) |
03ae5f59 ILT |
2239 | { |
2240 | ((*_bfd_error_handler) | |
d003868e AM |
2241 | (_("%B: invalid link %lu for reloc section %s (index %u)"), |
2242 | abfd, hdr->sh_link, name, shindex)); | |
bf67003b NC |
2243 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, |
2244 | shindex); | |
2245 | goto success; | |
03ae5f59 ILT |
2246 | } |
2247 | ||
252b5132 RH |
2248 | /* For some incomprehensible reason Oracle distributes |
2249 | libraries for Solaris in which some of the objects have | |
2250 | bogus sh_link fields. It would be nice if we could just | |
2251 | reject them, but, unfortunately, some people need to use | |
2252 | them. We scan through the section headers; if we find only | |
2253 | one suitable symbol table, we clobber the sh_link to point | |
83b89087 L |
2254 | to it. I hope this doesn't break anything. |
2255 | ||
2256 | Don't do it on executable nor shared library. */ | |
2257 | if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0 | |
2258 | && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_SYMTAB | |
252b5132 RH |
2259 | && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_DYNSYM) |
2260 | { | |
9ad5cbcf | 2261 | unsigned int scan; |
252b5132 RH |
2262 | int found; |
2263 | ||
2264 | found = 0; | |
9ad5cbcf | 2265 | for (scan = 1; scan < num_sec; scan++) |
252b5132 RH |
2266 | { |
2267 | if (elf_elfsections (abfd)[scan]->sh_type == SHT_SYMTAB | |
2268 | || elf_elfsections (abfd)[scan]->sh_type == SHT_DYNSYM) | |
2269 | { | |
2270 | if (found != 0) | |
2271 | { | |
2272 | found = 0; | |
2273 | break; | |
2274 | } | |
2275 | found = scan; | |
2276 | } | |
2277 | } | |
2278 | if (found != 0) | |
2279 | hdr->sh_link = found; | |
2280 | } | |
2281 | ||
2282 | /* Get the symbol table. */ | |
1b3a8575 AM |
2283 | if ((elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_SYMTAB |
2284 | || elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_DYNSYM) | |
252b5132 | 2285 | && ! bfd_section_from_shdr (abfd, hdr->sh_link)) |
bf67003b | 2286 | goto fail; |
252b5132 RH |
2287 | |
2288 | /* If this reloc section does not use the main symbol table we | |
2289 | don't treat it as a reloc section. BFD can't adequately | |
2290 | represent such a section, so at least for now, we don't | |
c044fabd | 2291 | try. We just present it as a normal section. We also |
60bcf0fa | 2292 | can't use it as a reloc section if it points to the null |
83b89087 L |
2293 | section, an invalid section, another reloc section, or its |
2294 | sh_link points to the null section. */ | |
185ef66d | 2295 | if (hdr->sh_link != elf_onesymtab (abfd) |
83b89087 | 2296 | || hdr->sh_link == SHN_UNDEF |
185ef66d | 2297 | || hdr->sh_info == SHN_UNDEF |
185ef66d AM |
2298 | || hdr->sh_info >= num_sec |
2299 | || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_REL | |
2300 | || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_RELA) | |
bf67003b NC |
2301 | { |
2302 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, | |
2303 | shindex); | |
2304 | goto success; | |
2305 | } | |
252b5132 RH |
2306 | |
2307 | if (! bfd_section_from_shdr (abfd, hdr->sh_info)) | |
bf67003b NC |
2308 | goto fail; |
2309 | ||
252b5132 RH |
2310 | target_sect = bfd_section_from_elf_index (abfd, hdr->sh_info); |
2311 | if (target_sect == NULL) | |
bf67003b | 2312 | goto fail; |
252b5132 | 2313 | |
d4730f92 BS |
2314 | esdt = elf_section_data (target_sect); |
2315 | if (hdr->sh_type == SHT_RELA) | |
2316 | p_hdr = &esdt->rela.hdr; | |
252b5132 | 2317 | else |
d4730f92 BS |
2318 | p_hdr = &esdt->rel.hdr; |
2319 | ||
06614111 NC |
2320 | /* PR 17512: file: 0b4f81b7. */ |
2321 | if (*p_hdr != NULL) | |
2322 | goto fail; | |
d4730f92 BS |
2323 | amt = sizeof (*hdr2); |
2324 | hdr2 = (Elf_Internal_Shdr *) bfd_alloc (abfd, amt); | |
2325 | if (hdr2 == NULL) | |
bf67003b | 2326 | goto fail; |
252b5132 | 2327 | *hdr2 = *hdr; |
d4730f92 | 2328 | *p_hdr = hdr2; |
252b5132 | 2329 | elf_elfsections (abfd)[shindex] = hdr2; |
d9bc7a44 | 2330 | target_sect->reloc_count += NUM_SHDR_ENTRIES (hdr); |
252b5132 RH |
2331 | target_sect->flags |= SEC_RELOC; |
2332 | target_sect->relocation = NULL; | |
2333 | target_sect->rel_filepos = hdr->sh_offset; | |
bf572ba0 MM |
2334 | /* In the section to which the relocations apply, mark whether |
2335 | its relocations are of the REL or RELA variety. */ | |
72730e0c | 2336 | if (hdr->sh_size != 0) |
d4730f92 BS |
2337 | { |
2338 | if (hdr->sh_type == SHT_RELA) | |
2339 | target_sect->use_rela_p = 1; | |
2340 | } | |
252b5132 | 2341 | abfd->flags |= HAS_RELOC; |
bf67003b | 2342 | goto success; |
252b5132 | 2343 | } |
252b5132 RH |
2344 | |
2345 | case SHT_GNU_verdef: | |
2346 | elf_dynverdef (abfd) = shindex; | |
2347 | elf_tdata (abfd)->dynverdef_hdr = *hdr; | |
bf67003b NC |
2348 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); |
2349 | goto success; | |
252b5132 RH |
2350 | |
2351 | case SHT_GNU_versym: | |
a50b2160 | 2352 | if (hdr->sh_entsize != sizeof (Elf_External_Versym)) |
bf67003b NC |
2353 | goto fail; |
2354 | ||
252b5132 RH |
2355 | elf_dynversym (abfd) = shindex; |
2356 | elf_tdata (abfd)->dynversym_hdr = *hdr; | |
bf67003b NC |
2357 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); |
2358 | goto success; | |
252b5132 RH |
2359 | |
2360 | case SHT_GNU_verneed: | |
2361 | elf_dynverref (abfd) = shindex; | |
2362 | elf_tdata (abfd)->dynverref_hdr = *hdr; | |
bf67003b NC |
2363 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); |
2364 | goto success; | |
252b5132 RH |
2365 | |
2366 | case SHT_SHLIB: | |
bf67003b | 2367 | goto success; |
252b5132 | 2368 | |
dbb410c3 | 2369 | case SHT_GROUP: |
44534af3 | 2370 | if (! IS_VALID_GROUP_SECTION_HEADER (hdr, GRP_ENTRY_SIZE)) |
bf67003b NC |
2371 | goto fail; |
2372 | ||
6dc132d9 | 2373 | if (!_bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex)) |
bf67003b NC |
2374 | goto fail; |
2375 | ||
dbb410c3 AM |
2376 | if (hdr->contents != NULL) |
2377 | { | |
2378 | Elf_Internal_Group *idx = (Elf_Internal_Group *) hdr->contents; | |
06614111 | 2379 | unsigned int n_elt = hdr->sh_size / sizeof (* idx); |
dbb410c3 AM |
2380 | asection *s; |
2381 | ||
06614111 NC |
2382 | if (n_elt == 0) |
2383 | goto fail; | |
b885599b AM |
2384 | if (idx->flags & GRP_COMDAT) |
2385 | hdr->bfd_section->flags | |
2386 | |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD; | |
2387 | ||
45c5e9ed L |
2388 | /* We try to keep the same section order as it comes in. */ |
2389 | idx += n_elt; | |
06614111 | 2390 | |
dbb410c3 | 2391 | while (--n_elt != 0) |
1783205a NC |
2392 | { |
2393 | --idx; | |
2394 | ||
2395 | if (idx->shdr != NULL | |
2396 | && (s = idx->shdr->bfd_section) != NULL | |
2397 | && elf_next_in_group (s) != NULL) | |
2398 | { | |
2399 | elf_next_in_group (hdr->bfd_section) = s; | |
2400 | break; | |
2401 | } | |
2402 | } | |
dbb410c3 | 2403 | } |
bf67003b | 2404 | goto success; |
dbb410c3 | 2405 | |
252b5132 | 2406 | default: |
104d59d1 JM |
2407 | /* Possibly an attributes section. */ |
2408 | if (hdr->sh_type == SHT_GNU_ATTRIBUTES | |
2409 | || hdr->sh_type == bed->obj_attrs_section_type) | |
2410 | { | |
2411 | if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex)) | |
bf67003b | 2412 | goto fail; |
104d59d1 | 2413 | _bfd_elf_parse_attributes (abfd, hdr); |
bf67003b | 2414 | goto success; |
104d59d1 JM |
2415 | } |
2416 | ||
252b5132 | 2417 | /* Check for any processor-specific section types. */ |
3eb70a79 | 2418 | if (bed->elf_backend_section_from_shdr (abfd, hdr, name, shindex)) |
bf67003b | 2419 | goto success; |
3eb70a79 L |
2420 | |
2421 | if (hdr->sh_type >= SHT_LOUSER && hdr->sh_type <= SHT_HIUSER) | |
2422 | { | |
2423 | if ((hdr->sh_flags & SHF_ALLOC) != 0) | |
2424 | /* FIXME: How to properly handle allocated section reserved | |
2425 | for applications? */ | |
2426 | (*_bfd_error_handler) | |
2427 | (_("%B: don't know how to handle allocated, application " | |
2428 | "specific section `%s' [0x%8x]"), | |
2429 | abfd, name, hdr->sh_type); | |
2430 | else | |
bf67003b NC |
2431 | { |
2432 | /* Allow sections reserved for applications. */ | |
2433 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, | |
2434 | shindex); | |
2435 | goto success; | |
2436 | } | |
3eb70a79 L |
2437 | } |
2438 | else if (hdr->sh_type >= SHT_LOPROC | |
2439 | && hdr->sh_type <= SHT_HIPROC) | |
2440 | /* FIXME: We should handle this section. */ | |
2441 | (*_bfd_error_handler) | |
2442 | (_("%B: don't know how to handle processor specific section " | |
2443 | "`%s' [0x%8x]"), | |
2444 | abfd, name, hdr->sh_type); | |
2445 | else if (hdr->sh_type >= SHT_LOOS && hdr->sh_type <= SHT_HIOS) | |
ff15b240 NC |
2446 | { |
2447 | /* Unrecognised OS-specific sections. */ | |
2448 | if ((hdr->sh_flags & SHF_OS_NONCONFORMING) != 0) | |
2449 | /* SHF_OS_NONCONFORMING indicates that special knowledge is | |
08a40648 | 2450 | required to correctly process the section and the file should |
ff15b240 NC |
2451 | be rejected with an error message. */ |
2452 | (*_bfd_error_handler) | |
2453 | (_("%B: don't know how to handle OS specific section " | |
2454 | "`%s' [0x%8x]"), | |
2455 | abfd, name, hdr->sh_type); | |
2456 | else | |
bf67003b NC |
2457 | { |
2458 | /* Otherwise it should be processed. */ | |
2459 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); | |
2460 | goto success; | |
2461 | } | |
ff15b240 | 2462 | } |
3eb70a79 L |
2463 | else |
2464 | /* FIXME: We should handle this section. */ | |
2465 | (*_bfd_error_handler) | |
2466 | (_("%B: don't know how to handle section `%s' [0x%8x]"), | |
2467 | abfd, name, hdr->sh_type); | |
2468 | ||
bf67003b | 2469 | goto fail; |
252b5132 RH |
2470 | } |
2471 | ||
bf67003b NC |
2472 | fail: |
2473 | ret = FALSE; | |
2474 | success: | |
e5b470e2 | 2475 | if (sections_being_created && sections_being_created_abfd == abfd) |
bf67003b NC |
2476 | sections_being_created [shindex] = FALSE; |
2477 | if (-- nesting == 0) | |
5a4b0ccc NC |
2478 | { |
2479 | sections_being_created = NULL; | |
2480 | sections_being_created_abfd = abfd; | |
2481 | } | |
bf67003b | 2482 | return ret; |
252b5132 RH |
2483 | } |
2484 | ||
87d72d41 | 2485 | /* Return the local symbol specified by ABFD, R_SYMNDX. */ |
ec338859 | 2486 | |
87d72d41 AM |
2487 | Elf_Internal_Sym * |
2488 | bfd_sym_from_r_symndx (struct sym_cache *cache, | |
2489 | bfd *abfd, | |
2490 | unsigned long r_symndx) | |
ec338859 | 2491 | { |
ec338859 AM |
2492 | unsigned int ent = r_symndx % LOCAL_SYM_CACHE_SIZE; |
2493 | ||
a5d1b3b5 AM |
2494 | if (cache->abfd != abfd || cache->indx[ent] != r_symndx) |
2495 | { | |
2496 | Elf_Internal_Shdr *symtab_hdr; | |
2497 | unsigned char esym[sizeof (Elf64_External_Sym)]; | |
2498 | Elf_External_Sym_Shndx eshndx; | |
ec338859 | 2499 | |
a5d1b3b5 AM |
2500 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; |
2501 | if (bfd_elf_get_elf_syms (abfd, symtab_hdr, 1, r_symndx, | |
87d72d41 | 2502 | &cache->sym[ent], esym, &eshndx) == NULL) |
a5d1b3b5 | 2503 | return NULL; |
9ad5cbcf | 2504 | |
a5d1b3b5 AM |
2505 | if (cache->abfd != abfd) |
2506 | { | |
2507 | memset (cache->indx, -1, sizeof (cache->indx)); | |
2508 | cache->abfd = abfd; | |
2509 | } | |
2510 | cache->indx[ent] = r_symndx; | |
ec338859 | 2511 | } |
a5d1b3b5 | 2512 | |
87d72d41 | 2513 | return &cache->sym[ent]; |
ec338859 AM |
2514 | } |
2515 | ||
252b5132 RH |
2516 | /* Given an ELF section number, retrieve the corresponding BFD |
2517 | section. */ | |
2518 | ||
2519 | asection * | |
91d6fa6a | 2520 | bfd_section_from_elf_index (bfd *abfd, unsigned int sec_index) |
252b5132 | 2521 | { |
91d6fa6a | 2522 | if (sec_index >= elf_numsections (abfd)) |
252b5132 | 2523 | return NULL; |
91d6fa6a | 2524 | return elf_elfsections (abfd)[sec_index]->bfd_section; |
252b5132 RH |
2525 | } |
2526 | ||
b35d266b | 2527 | static const struct bfd_elf_special_section special_sections_b[] = |
2f89ff8d | 2528 | { |
0112cd26 NC |
2529 | { STRING_COMMA_LEN (".bss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE }, |
2530 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2531 | }; |
2532 | ||
b35d266b | 2533 | static const struct bfd_elf_special_section special_sections_c[] = |
7f4d3958 | 2534 | { |
0112cd26 NC |
2535 | { STRING_COMMA_LEN (".comment"), 0, SHT_PROGBITS, 0 }, |
2536 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2537 | }; |
2538 | ||
b35d266b | 2539 | static const struct bfd_elf_special_section special_sections_d[] = |
7f4d3958 | 2540 | { |
0112cd26 NC |
2541 | { STRING_COMMA_LEN (".data"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE }, |
2542 | { STRING_COMMA_LEN (".data1"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE }, | |
a9a72a65 DE |
2543 | /* There are more DWARF sections than these, but they needn't be added here |
2544 | unless you have to cope with broken compilers that don't emit section | |
2545 | attributes or you want to help the user writing assembler. */ | |
0112cd26 NC |
2546 | { STRING_COMMA_LEN (".debug"), 0, SHT_PROGBITS, 0 }, |
2547 | { STRING_COMMA_LEN (".debug_line"), 0, SHT_PROGBITS, 0 }, | |
2548 | { STRING_COMMA_LEN (".debug_info"), 0, SHT_PROGBITS, 0 }, | |
2549 | { STRING_COMMA_LEN (".debug_abbrev"), 0, SHT_PROGBITS, 0 }, | |
2550 | { STRING_COMMA_LEN (".debug_aranges"), 0, SHT_PROGBITS, 0 }, | |
2551 | { STRING_COMMA_LEN (".dynamic"), 0, SHT_DYNAMIC, SHF_ALLOC }, | |
2552 | { STRING_COMMA_LEN (".dynstr"), 0, SHT_STRTAB, SHF_ALLOC }, | |
2553 | { STRING_COMMA_LEN (".dynsym"), 0, SHT_DYNSYM, SHF_ALLOC }, | |
2554 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2555 | }; |
2556 | ||
b35d266b | 2557 | static const struct bfd_elf_special_section special_sections_f[] = |
7f4d3958 | 2558 | { |
0112cd26 NC |
2559 | { STRING_COMMA_LEN (".fini"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR }, |
2560 | { STRING_COMMA_LEN (".fini_array"), 0, SHT_FINI_ARRAY, SHF_ALLOC + SHF_WRITE }, | |
2561 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2562 | }; |
2563 | ||
b35d266b | 2564 | static const struct bfd_elf_special_section special_sections_g[] = |
7f4d3958 | 2565 | { |
0112cd26 | 2566 | { STRING_COMMA_LEN (".gnu.linkonce.b"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE }, |
68efed41 | 2567 | { STRING_COMMA_LEN (".gnu.lto_"), -1, SHT_PROGBITS, SHF_EXCLUDE }, |
0112cd26 NC |
2568 | { STRING_COMMA_LEN (".got"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE }, |
2569 | { STRING_COMMA_LEN (".gnu.version"), 0, SHT_GNU_versym, 0 }, | |
2570 | { STRING_COMMA_LEN (".gnu.version_d"), 0, SHT_GNU_verdef, 0 }, | |
2571 | { STRING_COMMA_LEN (".gnu.version_r"), 0, SHT_GNU_verneed, 0 }, | |
2572 | { STRING_COMMA_LEN (".gnu.liblist"), 0, SHT_GNU_LIBLIST, SHF_ALLOC }, | |
2573 | { STRING_COMMA_LEN (".gnu.conflict"), 0, SHT_RELA, SHF_ALLOC }, | |
2574 | { STRING_COMMA_LEN (".gnu.hash"), 0, SHT_GNU_HASH, SHF_ALLOC }, | |
2575 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2576 | }; |
2577 | ||
b35d266b | 2578 | static const struct bfd_elf_special_section special_sections_h[] = |
7f4d3958 | 2579 | { |
0112cd26 NC |
2580 | { STRING_COMMA_LEN (".hash"), 0, SHT_HASH, SHF_ALLOC }, |
2581 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2582 | }; |
2583 | ||
b35d266b | 2584 | static const struct bfd_elf_special_section special_sections_i[] = |
7f4d3958 | 2585 | { |
0112cd26 NC |
2586 | { STRING_COMMA_LEN (".init"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR }, |
2587 | { STRING_COMMA_LEN (".init_array"), 0, SHT_INIT_ARRAY, SHF_ALLOC + SHF_WRITE }, | |
2588 | { STRING_COMMA_LEN (".interp"), 0, SHT_PROGBITS, 0 }, | |
2589 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2590 | }; |
2591 | ||
b35d266b | 2592 | static const struct bfd_elf_special_section special_sections_l[] = |
7f4d3958 | 2593 | { |
0112cd26 NC |
2594 | { STRING_COMMA_LEN (".line"), 0, SHT_PROGBITS, 0 }, |
2595 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2596 | }; |
2597 | ||
b35d266b | 2598 | static const struct bfd_elf_special_section special_sections_n[] = |
7f4d3958 | 2599 | { |
0112cd26 NC |
2600 | { STRING_COMMA_LEN (".note.GNU-stack"), 0, SHT_PROGBITS, 0 }, |
2601 | { STRING_COMMA_LEN (".note"), -1, SHT_NOTE, 0 }, | |
2602 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2603 | }; |
2604 | ||
b35d266b | 2605 | static const struct bfd_elf_special_section special_sections_p[] = |
7f4d3958 | 2606 | { |
0112cd26 NC |
2607 | { STRING_COMMA_LEN (".preinit_array"), 0, SHT_PREINIT_ARRAY, SHF_ALLOC + SHF_WRITE }, |
2608 | { STRING_COMMA_LEN (".plt"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR }, | |
2609 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2610 | }; |
2611 | ||
b35d266b | 2612 | static const struct bfd_elf_special_section special_sections_r[] = |
7f4d3958 | 2613 | { |
0112cd26 NC |
2614 | { STRING_COMMA_LEN (".rodata"), -2, SHT_PROGBITS, SHF_ALLOC }, |
2615 | { STRING_COMMA_LEN (".rodata1"), 0, SHT_PROGBITS, SHF_ALLOC }, | |
2616 | { STRING_COMMA_LEN (".rela"), -1, SHT_RELA, 0 }, | |
2617 | { STRING_COMMA_LEN (".rel"), -1, SHT_REL, 0 }, | |
2618 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2619 | }; |
2620 | ||
b35d266b | 2621 | static const struct bfd_elf_special_section special_sections_s[] = |
7f4d3958 | 2622 | { |
0112cd26 NC |
2623 | { STRING_COMMA_LEN (".shstrtab"), 0, SHT_STRTAB, 0 }, |
2624 | { STRING_COMMA_LEN (".strtab"), 0, SHT_STRTAB, 0 }, | |
2625 | { STRING_COMMA_LEN (".symtab"), 0, SHT_SYMTAB, 0 }, | |
60ff4dc4 HPN |
2626 | /* See struct bfd_elf_special_section declaration for the semantics of |
2627 | this special case where .prefix_length != strlen (.prefix). */ | |
2628 | { ".stabstr", 5, 3, SHT_STRTAB, 0 }, | |
0112cd26 | 2629 | { NULL, 0, 0, 0, 0 } |
2f89ff8d L |
2630 | }; |
2631 | ||
b35d266b | 2632 | static const struct bfd_elf_special_section special_sections_t[] = |
7f4d3958 | 2633 | { |
0112cd26 NC |
2634 | { STRING_COMMA_LEN (".text"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR }, |
2635 | { STRING_COMMA_LEN (".tbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS }, | |
2636 | { STRING_COMMA_LEN (".tdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS }, | |
2637 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2638 | }; |
2639 | ||
1b315056 CS |
2640 | static const struct bfd_elf_special_section special_sections_z[] = |
2641 | { | |
2642 | { STRING_COMMA_LEN (".zdebug_line"), 0, SHT_PROGBITS, 0 }, | |
2643 | { STRING_COMMA_LEN (".zdebug_info"), 0, SHT_PROGBITS, 0 }, | |
2644 | { STRING_COMMA_LEN (".zdebug_abbrev"), 0, SHT_PROGBITS, 0 }, | |
2645 | { STRING_COMMA_LEN (".zdebug_aranges"), 0, SHT_PROGBITS, 0 }, | |
2646 | { NULL, 0, 0, 0, 0 } | |
2647 | }; | |
2648 | ||
e4c93b56 | 2649 | static const struct bfd_elf_special_section * const special_sections[] = |
7f4d3958 | 2650 | { |
7f4d3958 | 2651 | special_sections_b, /* 'b' */ |
98ece1b3 | 2652 | special_sections_c, /* 'c' */ |
7f4d3958 L |
2653 | special_sections_d, /* 'd' */ |
2654 | NULL, /* 'e' */ | |
2655 | special_sections_f, /* 'f' */ | |
2656 | special_sections_g, /* 'g' */ | |
2657 | special_sections_h, /* 'h' */ | |
2658 | special_sections_i, /* 'i' */ | |
2659 | NULL, /* 'j' */ | |
2660 | NULL, /* 'k' */ | |
2661 | special_sections_l, /* 'l' */ | |
2662 | NULL, /* 'm' */ | |
2663 | special_sections_n, /* 'n' */ | |
2664 | NULL, /* 'o' */ | |
2665 | special_sections_p, /* 'p' */ | |
2666 | NULL, /* 'q' */ | |
2667 | special_sections_r, /* 'r' */ | |
2668 | special_sections_s, /* 's' */ | |
2669 | special_sections_t, /* 't' */ | |
1b315056 CS |
2670 | NULL, /* 'u' */ |
2671 | NULL, /* 'v' */ | |
2672 | NULL, /* 'w' */ | |
2673 | NULL, /* 'x' */ | |
2674 | NULL, /* 'y' */ | |
2675 | special_sections_z /* 'z' */ | |
7f4d3958 L |
2676 | }; |
2677 | ||
551b43fd AM |
2678 | const struct bfd_elf_special_section * |
2679 | _bfd_elf_get_special_section (const char *name, | |
2680 | const struct bfd_elf_special_section *spec, | |
2681 | unsigned int rela) | |
2f89ff8d L |
2682 | { |
2683 | int i; | |
7f4d3958 | 2684 | int len; |
7f4d3958 | 2685 | |
551b43fd | 2686 | len = strlen (name); |
7f4d3958 | 2687 | |
551b43fd | 2688 | for (i = 0; spec[i].prefix != NULL; i++) |
7dcb9820 AM |
2689 | { |
2690 | int suffix_len; | |
551b43fd | 2691 | int prefix_len = spec[i].prefix_length; |
7dcb9820 AM |
2692 | |
2693 | if (len < prefix_len) | |
2694 | continue; | |
551b43fd | 2695 | if (memcmp (name, spec[i].prefix, prefix_len) != 0) |
7dcb9820 AM |
2696 | continue; |
2697 | ||
551b43fd | 2698 | suffix_len = spec[i].suffix_length; |
7dcb9820 AM |
2699 | if (suffix_len <= 0) |
2700 | { | |
2701 | if (name[prefix_len] != 0) | |
2702 | { | |
2703 | if (suffix_len == 0) | |
2704 | continue; | |
2705 | if (name[prefix_len] != '.' | |
2706 | && (suffix_len == -2 | |
551b43fd | 2707 | || (rela && spec[i].type == SHT_REL))) |
7dcb9820 AM |
2708 | continue; |
2709 | } | |
2710 | } | |
2711 | else | |
2712 | { | |
2713 | if (len < prefix_len + suffix_len) | |
2714 | continue; | |
2715 | if (memcmp (name + len - suffix_len, | |
551b43fd | 2716 | spec[i].prefix + prefix_len, |
7dcb9820 AM |
2717 | suffix_len) != 0) |
2718 | continue; | |
2719 | } | |
551b43fd | 2720 | return &spec[i]; |
7dcb9820 | 2721 | } |
2f89ff8d L |
2722 | |
2723 | return NULL; | |
2724 | } | |
2725 | ||
7dcb9820 | 2726 | const struct bfd_elf_special_section * |
29ef7005 | 2727 | _bfd_elf_get_sec_type_attr (bfd *abfd, asection *sec) |
2f89ff8d | 2728 | { |
551b43fd AM |
2729 | int i; |
2730 | const struct bfd_elf_special_section *spec; | |
29ef7005 | 2731 | const struct elf_backend_data *bed; |
2f89ff8d L |
2732 | |
2733 | /* See if this is one of the special sections. */ | |
551b43fd AM |
2734 | if (sec->name == NULL) |
2735 | return NULL; | |
2f89ff8d | 2736 | |
29ef7005 L |
2737 | bed = get_elf_backend_data (abfd); |
2738 | spec = bed->special_sections; | |
2739 | if (spec) | |
2740 | { | |
2741 | spec = _bfd_elf_get_special_section (sec->name, | |
2742 | bed->special_sections, | |
2743 | sec->use_rela_p); | |
2744 | if (spec != NULL) | |
2745 | return spec; | |
2746 | } | |
2747 | ||
551b43fd AM |
2748 | if (sec->name[0] != '.') |
2749 | return NULL; | |
2f89ff8d | 2750 | |
551b43fd | 2751 | i = sec->name[1] - 'b'; |
1b315056 | 2752 | if (i < 0 || i > 'z' - 'b') |
551b43fd AM |
2753 | return NULL; |
2754 | ||
2755 | spec = special_sections[i]; | |
2f89ff8d | 2756 | |
551b43fd AM |
2757 | if (spec == NULL) |
2758 | return NULL; | |
2759 | ||
2760 | return _bfd_elf_get_special_section (sec->name, spec, sec->use_rela_p); | |
2f89ff8d L |
2761 | } |
2762 | ||
b34976b6 | 2763 | bfd_boolean |
217aa764 | 2764 | _bfd_elf_new_section_hook (bfd *abfd, asection *sec) |
252b5132 RH |
2765 | { |
2766 | struct bfd_elf_section_data *sdata; | |
551b43fd | 2767 | const struct elf_backend_data *bed; |
7dcb9820 | 2768 | const struct bfd_elf_special_section *ssect; |
252b5132 | 2769 | |
f0abc2a1 AM |
2770 | sdata = (struct bfd_elf_section_data *) sec->used_by_bfd; |
2771 | if (sdata == NULL) | |
2772 | { | |
a50b1753 NC |
2773 | sdata = (struct bfd_elf_section_data *) bfd_zalloc (abfd, |
2774 | sizeof (*sdata)); | |
f0abc2a1 AM |
2775 | if (sdata == NULL) |
2776 | return FALSE; | |
217aa764 | 2777 | sec->used_by_bfd = sdata; |
f0abc2a1 | 2778 | } |
bf572ba0 | 2779 | |
551b43fd AM |
2780 | /* Indicate whether or not this section should use RELA relocations. */ |
2781 | bed = get_elf_backend_data (abfd); | |
2782 | sec->use_rela_p = bed->default_use_rela_p; | |
2783 | ||
e843e0f8 L |
2784 | /* When we read a file, we don't need to set ELF section type and |
2785 | flags. They will be overridden in _bfd_elf_make_section_from_shdr | |
2786 | anyway. We will set ELF section type and flags for all linker | |
2787 | created sections. If user specifies BFD section flags, we will | |
2788 | set ELF section type and flags based on BFD section flags in | |
02ecc8e9 L |
2789 | elf_fake_sections. Special handling for .init_array/.fini_array |
2790 | output sections since they may contain .ctors/.dtors input | |
2791 | sections. We don't want _bfd_elf_init_private_section_data to | |
2792 | copy ELF section type from .ctors/.dtors input sections. */ | |
2793 | if (abfd->direction != read_direction | |
3496cb2a | 2794 | || (sec->flags & SEC_LINKER_CREATED) != 0) |
2f89ff8d | 2795 | { |
551b43fd | 2796 | ssect = (*bed->get_sec_type_attr) (abfd, sec); |
02ecc8e9 L |
2797 | if (ssect != NULL |
2798 | && (!sec->flags | |
2799 | || (sec->flags & SEC_LINKER_CREATED) != 0 | |
2800 | || ssect->type == SHT_INIT_ARRAY | |
2801 | || ssect->type == SHT_FINI_ARRAY)) | |
a31501e9 L |
2802 | { |
2803 | elf_section_type (sec) = ssect->type; | |
2804 | elf_section_flags (sec) = ssect->attr; | |
2805 | } | |
2f89ff8d L |
2806 | } |
2807 | ||
f592407e | 2808 | return _bfd_generic_new_section_hook (abfd, sec); |
252b5132 RH |
2809 | } |
2810 | ||
2811 | /* Create a new bfd section from an ELF program header. | |
2812 | ||
2813 | Since program segments have no names, we generate a synthetic name | |
2814 | of the form segment<NUM>, where NUM is generally the index in the | |
2815 | program header table. For segments that are split (see below) we | |
2816 | generate the names segment<NUM>a and segment<NUM>b. | |
2817 | ||
2818 | Note that some program segments may have a file size that is different than | |
2819 | (less than) the memory size. All this means is that at execution the | |
2820 | system must allocate the amount of memory specified by the memory size, | |
2821 | but only initialize it with the first "file size" bytes read from the | |
2822 | file. This would occur for example, with program segments consisting | |
2823 | of combined data+bss. | |
2824 | ||
2825 | To handle the above situation, this routine generates TWO bfd sections | |
2826 | for the single program segment. The first has the length specified by | |
2827 | the file size of the segment, and the second has the length specified | |
2828 | by the difference between the two sizes. In effect, the segment is split | |
d5191d0c | 2829 | into its initialized and uninitialized parts. |
252b5132 RH |
2830 | |
2831 | */ | |
2832 | ||
b34976b6 | 2833 | bfd_boolean |
217aa764 AM |
2834 | _bfd_elf_make_section_from_phdr (bfd *abfd, |
2835 | Elf_Internal_Phdr *hdr, | |
91d6fa6a | 2836 | int hdr_index, |
a50b1753 | 2837 | const char *type_name) |
252b5132 RH |
2838 | { |
2839 | asection *newsect; | |
2840 | char *name; | |
2841 | char namebuf[64]; | |
d4c88bbb | 2842 | size_t len; |
252b5132 RH |
2843 | int split; |
2844 | ||
2845 | split = ((hdr->p_memsz > 0) | |
2846 | && (hdr->p_filesz > 0) | |
2847 | && (hdr->p_memsz > hdr->p_filesz)); | |
d5191d0c AM |
2848 | |
2849 | if (hdr->p_filesz > 0) | |
252b5132 | 2850 | { |
91d6fa6a | 2851 | sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "a" : ""); |
d5191d0c | 2852 | len = strlen (namebuf) + 1; |
a50b1753 | 2853 | name = (char *) bfd_alloc (abfd, len); |
d5191d0c AM |
2854 | if (!name) |
2855 | return FALSE; | |
2856 | memcpy (name, namebuf, len); | |
2857 | newsect = bfd_make_section (abfd, name); | |
2858 | if (newsect == NULL) | |
2859 | return FALSE; | |
2860 | newsect->vma = hdr->p_vaddr; | |
2861 | newsect->lma = hdr->p_paddr; | |
2862 | newsect->size = hdr->p_filesz; | |
2863 | newsect->filepos = hdr->p_offset; | |
2864 | newsect->flags |= SEC_HAS_CONTENTS; | |
2865 | newsect->alignment_power = bfd_log2 (hdr->p_align); | |
2866 | if (hdr->p_type == PT_LOAD) | |
252b5132 | 2867 | { |
d5191d0c AM |
2868 | newsect->flags |= SEC_ALLOC; |
2869 | newsect->flags |= SEC_LOAD; | |
2870 | if (hdr->p_flags & PF_X) | |
2871 | { | |
2872 | /* FIXME: all we known is that it has execute PERMISSION, | |
2873 | may be data. */ | |
2874 | newsect->flags |= SEC_CODE; | |
2875 | } | |
2876 | } | |
2877 | if (!(hdr->p_flags & PF_W)) | |
2878 | { | |
2879 | newsect->flags |= SEC_READONLY; | |
252b5132 | 2880 | } |
252b5132 RH |
2881 | } |
2882 | ||
d5191d0c | 2883 | if (hdr->p_memsz > hdr->p_filesz) |
252b5132 | 2884 | { |
d5191d0c AM |
2885 | bfd_vma align; |
2886 | ||
91d6fa6a | 2887 | sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "b" : ""); |
d4c88bbb | 2888 | len = strlen (namebuf) + 1; |
a50b1753 | 2889 | name = (char *) bfd_alloc (abfd, len); |
252b5132 | 2890 | if (!name) |
b34976b6 | 2891 | return FALSE; |
d4c88bbb | 2892 | memcpy (name, namebuf, len); |
252b5132 RH |
2893 | newsect = bfd_make_section (abfd, name); |
2894 | if (newsect == NULL) | |
b34976b6 | 2895 | return FALSE; |
252b5132 RH |
2896 | newsect->vma = hdr->p_vaddr + hdr->p_filesz; |
2897 | newsect->lma = hdr->p_paddr + hdr->p_filesz; | |
eea6121a | 2898 | newsect->size = hdr->p_memsz - hdr->p_filesz; |
d5191d0c AM |
2899 | newsect->filepos = hdr->p_offset + hdr->p_filesz; |
2900 | align = newsect->vma & -newsect->vma; | |
2901 | if (align == 0 || align > hdr->p_align) | |
2902 | align = hdr->p_align; | |
2903 | newsect->alignment_power = bfd_log2 (align); | |
252b5132 RH |
2904 | if (hdr->p_type == PT_LOAD) |
2905 | { | |
d5191d0c AM |
2906 | /* Hack for gdb. Segments that have not been modified do |
2907 | not have their contents written to a core file, on the | |
2908 | assumption that a debugger can find the contents in the | |
2909 | executable. We flag this case by setting the fake | |
2910 | section size to zero. Note that "real" bss sections will | |
2911 | always have their contents dumped to the core file. */ | |
2912 | if (bfd_get_format (abfd) == bfd_core) | |
2913 | newsect->size = 0; | |
252b5132 RH |
2914 | newsect->flags |= SEC_ALLOC; |
2915 | if (hdr->p_flags & PF_X) | |
2916 | newsect->flags |= SEC_CODE; | |
2917 | } | |
2918 | if (!(hdr->p_flags & PF_W)) | |
2919 | newsect->flags |= SEC_READONLY; | |
2920 | } | |
2921 | ||
b34976b6 | 2922 | return TRUE; |
252b5132 RH |
2923 | } |
2924 | ||
b34976b6 | 2925 | bfd_boolean |
91d6fa6a | 2926 | bfd_section_from_phdr (bfd *abfd, Elf_Internal_Phdr *hdr, int hdr_index) |
20cfcaae | 2927 | { |
9c5bfbb7 | 2928 | const struct elf_backend_data *bed; |
20cfcaae NC |
2929 | |
2930 | switch (hdr->p_type) | |
2931 | { | |
2932 | case PT_NULL: | |
91d6fa6a | 2933 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "null"); |
20cfcaae NC |
2934 | |
2935 | case PT_LOAD: | |
91d6fa6a | 2936 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "load"); |
20cfcaae NC |
2937 | |
2938 | case PT_DYNAMIC: | |
91d6fa6a | 2939 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "dynamic"); |
20cfcaae NC |
2940 | |
2941 | case PT_INTERP: | |
91d6fa6a | 2942 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "interp"); |
20cfcaae NC |
2943 | |
2944 | case PT_NOTE: | |
91d6fa6a | 2945 | if (! _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "note")) |
b34976b6 | 2946 | return FALSE; |
718175fa | 2947 | if (! elf_read_notes (abfd, hdr->p_offset, hdr->p_filesz)) |
b34976b6 AM |
2948 | return FALSE; |
2949 | return TRUE; | |
20cfcaae NC |
2950 | |
2951 | case PT_SHLIB: | |
91d6fa6a | 2952 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "shlib"); |
20cfcaae NC |
2953 | |
2954 | case PT_PHDR: | |
91d6fa6a | 2955 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "phdr"); |
20cfcaae | 2956 | |
811072d8 | 2957 | case PT_GNU_EH_FRAME: |
91d6fa6a | 2958 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, |
811072d8 RM |
2959 | "eh_frame_hdr"); |
2960 | ||
2b05f1b7 | 2961 | case PT_GNU_STACK: |
91d6fa6a | 2962 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "stack"); |
9ee5e499 | 2963 | |
8c37241b | 2964 | case PT_GNU_RELRO: |
91d6fa6a | 2965 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "relro"); |
8c37241b | 2966 | |
20cfcaae | 2967 | default: |
8c1acd09 | 2968 | /* Check for any processor-specific program segment types. */ |
20cfcaae | 2969 | bed = get_elf_backend_data (abfd); |
91d6fa6a | 2970 | return bed->elf_backend_section_from_phdr (abfd, hdr, hdr_index, "proc"); |
20cfcaae NC |
2971 | } |
2972 | } | |
2973 | ||
d4730f92 BS |
2974 | /* Return the REL_HDR for SEC, assuming there is only a single one, either |
2975 | REL or RELA. */ | |
2976 | ||
2977 | Elf_Internal_Shdr * | |
2978 | _bfd_elf_single_rel_hdr (asection *sec) | |
2979 | { | |
2980 | if (elf_section_data (sec)->rel.hdr) | |
2981 | { | |
2982 | BFD_ASSERT (elf_section_data (sec)->rela.hdr == NULL); | |
2983 | return elf_section_data (sec)->rel.hdr; | |
2984 | } | |
2985 | else | |
2986 | return elf_section_data (sec)->rela.hdr; | |
2987 | } | |
2988 | ||
3e19fb8f L |
2989 | static bfd_boolean |
2990 | _bfd_elf_set_reloc_sh_name (bfd *abfd, | |
2991 | Elf_Internal_Shdr *rel_hdr, | |
2992 | const char *sec_name, | |
2993 | bfd_boolean use_rela_p) | |
2994 | { | |
2995 | char *name = (char *) bfd_alloc (abfd, | |
2996 | sizeof ".rela" + strlen (sec_name)); | |
2997 | if (name == NULL) | |
2998 | return FALSE; | |
2999 | ||
3000 | sprintf (name, "%s%s", use_rela_p ? ".rela" : ".rel", sec_name); | |
3001 | rel_hdr->sh_name = | |
3002 | (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), name, | |
3003 | FALSE); | |
3004 | if (rel_hdr->sh_name == (unsigned int) -1) | |
3005 | return FALSE; | |
3006 | ||
3007 | return TRUE; | |
3008 | } | |
3009 | ||
d4730f92 BS |
3010 | /* Allocate and initialize a section-header for a new reloc section, |
3011 | containing relocations against ASECT. It is stored in RELDATA. If | |
3012 | USE_RELA_P is TRUE, we use RELA relocations; otherwise, we use REL | |
3013 | relocations. */ | |
23bc299b | 3014 | |
5d13b3b3 | 3015 | static bfd_boolean |
217aa764 | 3016 | _bfd_elf_init_reloc_shdr (bfd *abfd, |
d4730f92 | 3017 | struct bfd_elf_section_reloc_data *reldata, |
f6fe1ccd | 3018 | const char *sec_name, |
3e19fb8f L |
3019 | bfd_boolean use_rela_p, |
3020 | bfd_boolean delay_st_name_p) | |
23bc299b | 3021 | { |
d4730f92 | 3022 | Elf_Internal_Shdr *rel_hdr; |
9c5bfbb7 | 3023 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
d4730f92 BS |
3024 | bfd_size_type amt; |
3025 | ||
3026 | amt = sizeof (Elf_Internal_Shdr); | |
3027 | BFD_ASSERT (reldata->hdr == NULL); | |
3028 | rel_hdr = bfd_zalloc (abfd, amt); | |
3029 | reldata->hdr = rel_hdr; | |
23bc299b | 3030 | |
3e19fb8f L |
3031 | if (delay_st_name_p) |
3032 | rel_hdr->sh_name = (unsigned int) -1; | |
3033 | else if (!_bfd_elf_set_reloc_sh_name (abfd, rel_hdr, sec_name, | |
3034 | use_rela_p)) | |
b34976b6 | 3035 | return FALSE; |
23bc299b MM |
3036 | rel_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL; |
3037 | rel_hdr->sh_entsize = (use_rela_p | |
3038 | ? bed->s->sizeof_rela | |
3039 | : bed->s->sizeof_rel); | |
72de5009 | 3040 | rel_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align; |
23bc299b MM |
3041 | rel_hdr->sh_flags = 0; |
3042 | rel_hdr->sh_addr = 0; | |
3043 | rel_hdr->sh_size = 0; | |
3044 | rel_hdr->sh_offset = 0; | |
3045 | ||
b34976b6 | 3046 | return TRUE; |
23bc299b MM |
3047 | } |
3048 | ||
94be91de JB |
3049 | /* Return the default section type based on the passed in section flags. */ |
3050 | ||
3051 | int | |
3052 | bfd_elf_get_default_section_type (flagword flags) | |
3053 | { | |
3054 | if ((flags & SEC_ALLOC) != 0 | |
2e76e85a | 3055 | && (flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0) |
94be91de JB |
3056 | return SHT_NOBITS; |
3057 | return SHT_PROGBITS; | |
3058 | } | |
3059 | ||
d4730f92 BS |
3060 | struct fake_section_arg |
3061 | { | |
3062 | struct bfd_link_info *link_info; | |
3063 | bfd_boolean failed; | |
3064 | }; | |
3065 | ||
252b5132 RH |
3066 | /* Set up an ELF internal section header for a section. */ |
3067 | ||
252b5132 | 3068 | static void |
d4730f92 | 3069 | elf_fake_sections (bfd *abfd, asection *asect, void *fsarg) |
252b5132 | 3070 | { |
d4730f92 | 3071 | struct fake_section_arg *arg = (struct fake_section_arg *)fsarg; |
9c5bfbb7 | 3072 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
d4730f92 | 3073 | struct bfd_elf_section_data *esd = elf_section_data (asect); |
252b5132 | 3074 | Elf_Internal_Shdr *this_hdr; |
0414f35b | 3075 | unsigned int sh_type; |
0ce398f1 | 3076 | const char *name = asect->name; |
3e19fb8f | 3077 | bfd_boolean delay_st_name_p = FALSE; |
252b5132 | 3078 | |
d4730f92 | 3079 | if (arg->failed) |
252b5132 RH |
3080 | { |
3081 | /* We already failed; just get out of the bfd_map_over_sections | |
08a40648 | 3082 | loop. */ |
252b5132 RH |
3083 | return; |
3084 | } | |
3085 | ||
d4730f92 | 3086 | this_hdr = &esd->this_hdr; |
252b5132 | 3087 | |
f6fe1ccd | 3088 | if (arg->link_info) |
0ce398f1 | 3089 | { |
f6fe1ccd L |
3090 | /* ld: compress DWARF debug sections with names: .debug_*. */ |
3091 | if ((arg->link_info->compress_debug & COMPRESS_DEBUG) | |
3092 | && (asect->flags & SEC_DEBUGGING) | |
3093 | && name[1] == 'd' | |
3094 | && name[6] == '_') | |
3095 | { | |
3096 | /* Set SEC_ELF_COMPRESS to indicate this section should be | |
3097 | compressed. */ | |
3098 | asect->flags |= SEC_ELF_COMPRESS; | |
0ce398f1 | 3099 | |
3e19fb8f L |
3100 | /* If this section will be compressed, delay adding setion |
3101 | name to section name section after it is compressed in | |
3102 | _bfd_elf_assign_file_positions_for_non_load. */ | |
3103 | delay_st_name_p = TRUE; | |
f6fe1ccd L |
3104 | } |
3105 | } | |
3106 | else if ((asect->flags & SEC_ELF_RENAME)) | |
3107 | { | |
3108 | /* objcopy: rename output DWARF debug section. */ | |
3109 | if ((abfd->flags & (BFD_DECOMPRESS | BFD_COMPRESS_GABI))) | |
3110 | { | |
3111 | /* When we decompress or compress with SHF_COMPRESSED, | |
3112 | convert section name from .zdebug_* to .debug_* if | |
3113 | needed. */ | |
3114 | if (name[1] == 'z') | |
3115 | { | |
3116 | char *new_name = convert_zdebug_to_debug (abfd, name); | |
3117 | if (new_name == NULL) | |
3118 | { | |
3119 | arg->failed = TRUE; | |
3120 | return; | |
3121 | } | |
3122 | name = new_name; | |
3123 | } | |
3124 | } | |
3125 | else if (asect->compress_status == COMPRESS_SECTION_DONE) | |
0ce398f1 | 3126 | { |
f6fe1ccd L |
3127 | /* PR binutils/18087: Compression does not always make a |
3128 | section smaller. So only rename the section when | |
3129 | compression has actually taken place. If input section | |
3130 | name is .zdebug_*, we should never compress it again. */ | |
3131 | char *new_name = convert_debug_to_zdebug (abfd, name); | |
0ce398f1 L |
3132 | if (new_name == NULL) |
3133 | { | |
3134 | arg->failed = TRUE; | |
3135 | return; | |
3136 | } | |
f6fe1ccd L |
3137 | BFD_ASSERT (name[1] != 'z'); |
3138 | name = new_name; | |
0ce398f1 L |
3139 | } |
3140 | } | |
3141 | ||
3e19fb8f L |
3142 | if (delay_st_name_p) |
3143 | this_hdr->sh_name = (unsigned int) -1; | |
3144 | else | |
252b5132 | 3145 | { |
3e19fb8f L |
3146 | this_hdr->sh_name |
3147 | = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), | |
3148 | name, FALSE); | |
3149 | if (this_hdr->sh_name == (unsigned int) -1) | |
3150 | { | |
3151 | arg->failed = TRUE; | |
3152 | return; | |
3153 | } | |
252b5132 RH |
3154 | } |
3155 | ||
a4d8e49b | 3156 | /* Don't clear sh_flags. Assembler may set additional bits. */ |
252b5132 RH |
3157 | |
3158 | if ((asect->flags & SEC_ALLOC) != 0 | |
3159 | || asect->user_set_vma) | |
3160 | this_hdr->sh_addr = asect->vma; | |
3161 | else | |
3162 | this_hdr->sh_addr = 0; | |
3163 | ||
3164 | this_hdr->sh_offset = 0; | |
eea6121a | 3165 | this_hdr->sh_size = asect->size; |
252b5132 | 3166 | this_hdr->sh_link = 0; |
c86934ce NC |
3167 | /* PR 17512: file: 0eb809fe, 8b0535ee. */ |
3168 | if (asect->alignment_power >= (sizeof (bfd_vma) * 8) - 1) | |
3169 | { | |
3170 | (*_bfd_error_handler) | |
3171 | (_("%B: error: Alignment power %d of section `%A' is too big"), | |
3172 | abfd, asect, asect->alignment_power); | |
3173 | arg->failed = TRUE; | |
3174 | return; | |
3175 | } | |
72de5009 | 3176 | this_hdr->sh_addralign = (bfd_vma) 1 << asect->alignment_power; |
252b5132 RH |
3177 | /* The sh_entsize and sh_info fields may have been set already by |
3178 | copy_private_section_data. */ | |
3179 | ||
3180 | this_hdr->bfd_section = asect; | |
3181 | this_hdr->contents = NULL; | |
3182 | ||
3cddba1e L |
3183 | /* If the section type is unspecified, we set it based on |
3184 | asect->flags. */ | |
98ece1b3 AM |
3185 | if ((asect->flags & SEC_GROUP) != 0) |
3186 | sh_type = SHT_GROUP; | |
98ece1b3 | 3187 | else |
94be91de | 3188 | sh_type = bfd_elf_get_default_section_type (asect->flags); |
98ece1b3 | 3189 | |
3cddba1e | 3190 | if (this_hdr->sh_type == SHT_NULL) |
98ece1b3 AM |
3191 | this_hdr->sh_type = sh_type; |
3192 | else if (this_hdr->sh_type == SHT_NOBITS | |
3193 | && sh_type == SHT_PROGBITS | |
3194 | && (asect->flags & SEC_ALLOC) != 0) | |
3cddba1e | 3195 | { |
98ece1b3 AM |
3196 | /* Warn if we are changing a NOBITS section to PROGBITS, but |
3197 | allow the link to proceed. This can happen when users link | |
3198 | non-bss input sections to bss output sections, or emit data | |
3199 | to a bss output section via a linker script. */ | |
3200 | (*_bfd_error_handler) | |
58f0869b | 3201 | (_("warning: section `%A' type changed to PROGBITS"), asect); |
98ece1b3 | 3202 | this_hdr->sh_type = sh_type; |
3cddba1e L |
3203 | } |
3204 | ||
2f89ff8d | 3205 | switch (this_hdr->sh_type) |
252b5132 | 3206 | { |
2f89ff8d | 3207 | default: |
2f89ff8d L |
3208 | break; |
3209 | ||
3210 | case SHT_STRTAB: | |
3211 | case SHT_INIT_ARRAY: | |
3212 | case SHT_FINI_ARRAY: | |
3213 | case SHT_PREINIT_ARRAY: | |
3214 | case SHT_NOTE: | |
3215 | case SHT_NOBITS: | |
3216 | case SHT_PROGBITS: | |
3217 | break; | |
3218 | ||
3219 | case SHT_HASH: | |
c7ac6ff8 | 3220 | this_hdr->sh_entsize = bed->s->sizeof_hash_entry; |
2f89ff8d | 3221 | break; |
5de3bf90 | 3222 | |
2f89ff8d | 3223 | case SHT_DYNSYM: |
252b5132 | 3224 | this_hdr->sh_entsize = bed->s->sizeof_sym; |
2f89ff8d L |
3225 | break; |
3226 | ||
3227 | case SHT_DYNAMIC: | |
252b5132 | 3228 | this_hdr->sh_entsize = bed->s->sizeof_dyn; |
2f89ff8d L |
3229 | break; |
3230 | ||
3231 | case SHT_RELA: | |
3232 | if (get_elf_backend_data (abfd)->may_use_rela_p) | |
3233 | this_hdr->sh_entsize = bed->s->sizeof_rela; | |
3234 | break; | |
3235 | ||
3236 | case SHT_REL: | |
3237 | if (get_elf_backend_data (abfd)->may_use_rel_p) | |
3238 | this_hdr->sh_entsize = bed->s->sizeof_rel; | |
3239 | break; | |
3240 | ||
3241 | case SHT_GNU_versym: | |
252b5132 | 3242 | this_hdr->sh_entsize = sizeof (Elf_External_Versym); |
2f89ff8d L |
3243 | break; |
3244 | ||
3245 | case SHT_GNU_verdef: | |
252b5132 RH |
3246 | this_hdr->sh_entsize = 0; |
3247 | /* objcopy or strip will copy over sh_info, but may not set | |
08a40648 AM |
3248 | cverdefs. The linker will set cverdefs, but sh_info will be |
3249 | zero. */ | |
252b5132 RH |
3250 | if (this_hdr->sh_info == 0) |
3251 | this_hdr->sh_info = elf_tdata (abfd)->cverdefs; | |
3252 | else | |
3253 | BFD_ASSERT (elf_tdata (abfd)->cverdefs == 0 | |
3254 | || this_hdr->sh_info == elf_tdata (abfd)->cverdefs); | |
2f89ff8d L |
3255 | break; |
3256 | ||
3257 | case SHT_GNU_verneed: | |
252b5132 RH |
3258 | this_hdr->sh_entsize = 0; |
3259 | /* objcopy or strip will copy over sh_info, but may not set | |
08a40648 AM |
3260 | cverrefs. The linker will set cverrefs, but sh_info will be |
3261 | zero. */ | |
252b5132 RH |
3262 | if (this_hdr->sh_info == 0) |
3263 | this_hdr->sh_info = elf_tdata (abfd)->cverrefs; | |
3264 | else | |
3265 | BFD_ASSERT (elf_tdata (abfd)->cverrefs == 0 | |
3266 | || this_hdr->sh_info == elf_tdata (abfd)->cverrefs); | |
2f89ff8d L |
3267 | break; |
3268 | ||
3269 | case SHT_GROUP: | |
1783205a | 3270 | this_hdr->sh_entsize = GRP_ENTRY_SIZE; |
2f89ff8d | 3271 | break; |
fdc90cb4 JJ |
3272 | |
3273 | case SHT_GNU_HASH: | |
3274 | this_hdr->sh_entsize = bed->s->arch_size == 64 ? 0 : 4; | |
3275 | break; | |
dbb410c3 | 3276 | } |
252b5132 RH |
3277 | |
3278 | if ((asect->flags & SEC_ALLOC) != 0) | |
3279 | this_hdr->sh_flags |= SHF_ALLOC; | |
3280 | if ((asect->flags & SEC_READONLY) == 0) | |
3281 | this_hdr->sh_flags |= SHF_WRITE; | |
3282 | if ((asect->flags & SEC_CODE) != 0) | |
3283 | this_hdr->sh_flags |= SHF_EXECINSTR; | |
f5fa8ca2 JJ |
3284 | if ((asect->flags & SEC_MERGE) != 0) |
3285 | { | |
3286 | this_hdr->sh_flags |= SHF_MERGE; | |
3287 | this_hdr->sh_entsize = asect->entsize; | |
f5fa8ca2 | 3288 | } |
84865015 NC |
3289 | if ((asect->flags & SEC_STRINGS) != 0) |
3290 | this_hdr->sh_flags |= SHF_STRINGS; | |
1126897b | 3291 | if ((asect->flags & SEC_GROUP) == 0 && elf_group_name (asect) != NULL) |
dbb410c3 | 3292 | this_hdr->sh_flags |= SHF_GROUP; |
13ae64f3 | 3293 | if ((asect->flags & SEC_THREAD_LOCAL) != 0) |
704afa60 JJ |
3294 | { |
3295 | this_hdr->sh_flags |= SHF_TLS; | |
3a800eb9 AM |
3296 | if (asect->size == 0 |
3297 | && (asect->flags & SEC_HAS_CONTENTS) == 0) | |
704afa60 | 3298 | { |
3a800eb9 | 3299 | struct bfd_link_order *o = asect->map_tail.link_order; |
b34976b6 | 3300 | |
704afa60 | 3301 | this_hdr->sh_size = 0; |
3a800eb9 AM |
3302 | if (o != NULL) |
3303 | { | |
704afa60 | 3304 | this_hdr->sh_size = o->offset + o->size; |
3a800eb9 AM |
3305 | if (this_hdr->sh_size != 0) |
3306 | this_hdr->sh_type = SHT_NOBITS; | |
3307 | } | |
704afa60 JJ |
3308 | } |
3309 | } | |
18ae9cc1 L |
3310 | if ((asect->flags & (SEC_GROUP | SEC_EXCLUDE)) == SEC_EXCLUDE) |
3311 | this_hdr->sh_flags |= SHF_EXCLUDE; | |
252b5132 | 3312 | |
d4730f92 BS |
3313 | /* If the section has relocs, set up a section header for the |
3314 | SHT_REL[A] section. If two relocation sections are required for | |
3315 | this section, it is up to the processor-specific back-end to | |
3316 | create the other. */ | |
3317 | if ((asect->flags & SEC_RELOC) != 0) | |
3318 | { | |
3319 | /* When doing a relocatable link, create both REL and RELA sections if | |
3320 | needed. */ | |
3321 | if (arg->link_info | |
3322 | /* Do the normal setup if we wouldn't create any sections here. */ | |
3323 | && esd->rel.count + esd->rela.count > 0 | |
0e1862bb L |
3324 | && (bfd_link_relocatable (arg->link_info) |
3325 | || arg->link_info->emitrelocations)) | |
d4730f92 BS |
3326 | { |
3327 | if (esd->rel.count && esd->rel.hdr == NULL | |
3e19fb8f L |
3328 | && !_bfd_elf_init_reloc_shdr (abfd, &esd->rel, name, FALSE, |
3329 | delay_st_name_p)) | |
d4730f92 BS |
3330 | { |
3331 | arg->failed = TRUE; | |
3332 | return; | |
3333 | } | |
3334 | if (esd->rela.count && esd->rela.hdr == NULL | |
3e19fb8f L |
3335 | && !_bfd_elf_init_reloc_shdr (abfd, &esd->rela, name, TRUE, |
3336 | delay_st_name_p)) | |
d4730f92 BS |
3337 | { |
3338 | arg->failed = TRUE; | |
3339 | return; | |
3340 | } | |
3341 | } | |
3342 | else if (!_bfd_elf_init_reloc_shdr (abfd, | |
3343 | (asect->use_rela_p | |
3344 | ? &esd->rela : &esd->rel), | |
f6fe1ccd | 3345 | name, |
3e19fb8f L |
3346 | asect->use_rela_p, |
3347 | delay_st_name_p)) | |
d4730f92 BS |
3348 | arg->failed = TRUE; |
3349 | } | |
3350 | ||
252b5132 | 3351 | /* Check for processor-specific section types. */ |
0414f35b | 3352 | sh_type = this_hdr->sh_type; |
e1fddb6b AO |
3353 | if (bed->elf_backend_fake_sections |
3354 | && !(*bed->elf_backend_fake_sections) (abfd, this_hdr, asect)) | |
d4730f92 | 3355 | arg->failed = TRUE; |
252b5132 | 3356 | |
42bb2e33 | 3357 | if (sh_type == SHT_NOBITS && asect->size != 0) |
0414f35b AM |
3358 | { |
3359 | /* Don't change the header type from NOBITS if we are being | |
42bb2e33 | 3360 | called for objcopy --only-keep-debug. */ |
0414f35b AM |
3361 | this_hdr->sh_type = sh_type; |
3362 | } | |
252b5132 RH |
3363 | } |
3364 | ||
bcacc0f5 AM |
3365 | /* Fill in the contents of a SHT_GROUP section. Called from |
3366 | _bfd_elf_compute_section_file_positions for gas, objcopy, and | |
3367 | when ELF targets use the generic linker, ld. Called for ld -r | |
3368 | from bfd_elf_final_link. */ | |
dbb410c3 | 3369 | |
1126897b | 3370 | void |
217aa764 | 3371 | bfd_elf_set_group_contents (bfd *abfd, asection *sec, void *failedptrarg) |
dbb410c3 | 3372 | { |
a50b1753 | 3373 | bfd_boolean *failedptr = (bfd_boolean *) failedptrarg; |
9dce4196 | 3374 | asection *elt, *first; |
dbb410c3 | 3375 | unsigned char *loc; |
b34976b6 | 3376 | bfd_boolean gas; |
dbb410c3 | 3377 | |
7e4111ad L |
3378 | /* Ignore linker created group section. See elfNN_ia64_object_p in |
3379 | elfxx-ia64.c. */ | |
3380 | if (((sec->flags & (SEC_GROUP | SEC_LINKER_CREATED)) != SEC_GROUP) | |
dbb410c3 AM |
3381 | || *failedptr) |
3382 | return; | |
3383 | ||
bcacc0f5 AM |
3384 | if (elf_section_data (sec)->this_hdr.sh_info == 0) |
3385 | { | |
3386 | unsigned long symindx = 0; | |
3387 | ||
3388 | /* elf_group_id will have been set up by objcopy and the | |
3389 | generic linker. */ | |
3390 | if (elf_group_id (sec) != NULL) | |
3391 | symindx = elf_group_id (sec)->udata.i; | |
1126897b | 3392 | |
bcacc0f5 AM |
3393 | if (symindx == 0) |
3394 | { | |
3395 | /* If called from the assembler, swap_out_syms will have set up | |
3396 | elf_section_syms. */ | |
3397 | BFD_ASSERT (elf_section_syms (abfd) != NULL); | |
3398 | symindx = elf_section_syms (abfd)[sec->index]->udata.i; | |
3399 | } | |
3400 | elf_section_data (sec)->this_hdr.sh_info = symindx; | |
3401 | } | |
3402 | else if (elf_section_data (sec)->this_hdr.sh_info == (unsigned int) -2) | |
1126897b | 3403 | { |
bcacc0f5 AM |
3404 | /* The ELF backend linker sets sh_info to -2 when the group |
3405 | signature symbol is global, and thus the index can't be | |
3406 | set until all local symbols are output. */ | |
3407 | asection *igroup = elf_sec_group (elf_next_in_group (sec)); | |
3408 | struct bfd_elf_section_data *sec_data = elf_section_data (igroup); | |
3409 | unsigned long symndx = sec_data->this_hdr.sh_info; | |
3410 | unsigned long extsymoff = 0; | |
3411 | struct elf_link_hash_entry *h; | |
3412 | ||
3413 | if (!elf_bad_symtab (igroup->owner)) | |
3414 | { | |
3415 | Elf_Internal_Shdr *symtab_hdr; | |
3416 | ||
3417 | symtab_hdr = &elf_tdata (igroup->owner)->symtab_hdr; | |
3418 | extsymoff = symtab_hdr->sh_info; | |
3419 | } | |
3420 | h = elf_sym_hashes (igroup->owner)[symndx - extsymoff]; | |
3421 | while (h->root.type == bfd_link_hash_indirect | |
3422 | || h->root.type == bfd_link_hash_warning) | |
3423 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
3424 | ||
3425 | elf_section_data (sec)->this_hdr.sh_info = h->indx; | |
1126897b | 3426 | } |
dbb410c3 | 3427 | |
1126897b | 3428 | /* The contents won't be allocated for "ld -r" or objcopy. */ |
b34976b6 | 3429 | gas = TRUE; |
dbb410c3 AM |
3430 | if (sec->contents == NULL) |
3431 | { | |
b34976b6 | 3432 | gas = FALSE; |
a50b1753 | 3433 | sec->contents = (unsigned char *) bfd_alloc (abfd, sec->size); |
9dce4196 AM |
3434 | |
3435 | /* Arrange for the section to be written out. */ | |
3436 | elf_section_data (sec)->this_hdr.contents = sec->contents; | |
dbb410c3 AM |
3437 | if (sec->contents == NULL) |
3438 | { | |
b34976b6 | 3439 | *failedptr = TRUE; |
dbb410c3 AM |
3440 | return; |
3441 | } | |
3442 | } | |
3443 | ||
eea6121a | 3444 | loc = sec->contents + sec->size; |
dbb410c3 | 3445 | |
9dce4196 AM |
3446 | /* Get the pointer to the first section in the group that gas |
3447 | squirreled away here. objcopy arranges for this to be set to the | |
3448 | start of the input section group. */ | |
3449 | first = elt = elf_next_in_group (sec); | |
dbb410c3 AM |
3450 | |
3451 | /* First element is a flag word. Rest of section is elf section | |
3452 | indices for all the sections of the group. Write them backwards | |
3453 | just to keep the group in the same order as given in .section | |
3454 | directives, not that it matters. */ | |
3455 | while (elt != NULL) | |
3456 | { | |
9dce4196 | 3457 | asection *s; |
9dce4196 | 3458 | |
9dce4196 | 3459 | s = elt; |
415f38a6 AM |
3460 | if (!gas) |
3461 | s = s->output_section; | |
3462 | if (s != NULL | |
3463 | && !bfd_is_abs_section (s)) | |
01e1a5bc | 3464 | { |
415f38a6 AM |
3465 | unsigned int idx = elf_section_data (s)->this_idx; |
3466 | ||
01e1a5bc | 3467 | loc -= 4; |
01e1a5bc NC |
3468 | H_PUT_32 (abfd, idx, loc); |
3469 | } | |
945906ff | 3470 | elt = elf_next_in_group (elt); |
9dce4196 AM |
3471 | if (elt == first) |
3472 | break; | |
dbb410c3 AM |
3473 | } |
3474 | ||
3d7f7666 | 3475 | if ((loc -= 4) != sec->contents) |
9dce4196 | 3476 | abort (); |
dbb410c3 | 3477 | |
9dce4196 | 3478 | H_PUT_32 (abfd, sec->flags & SEC_LINK_ONCE ? GRP_COMDAT : 0, loc); |
dbb410c3 AM |
3479 | } |
3480 | ||
bd53a53a L |
3481 | /* Return the section which RELOC_SEC applies to. */ |
3482 | ||
3483 | asection * | |
3484 | _bfd_elf_get_reloc_section (asection *reloc_sec) | |
3485 | { | |
3486 | const char *name; | |
3487 | unsigned int type; | |
3488 | bfd *abfd; | |
3489 | ||
3490 | if (reloc_sec == NULL) | |
3491 | return NULL; | |
3492 | ||
3493 | type = elf_section_data (reloc_sec)->this_hdr.sh_type; | |
3494 | if (type != SHT_REL && type != SHT_RELA) | |
3495 | return NULL; | |
3496 | ||
3497 | /* We look up the section the relocs apply to by name. */ | |
3498 | name = reloc_sec->name; | |
3499 | if (type == SHT_REL) | |
3500 | name += 4; | |
3501 | else | |
3502 | name += 5; | |
3503 | ||
3504 | /* If a target needs .got.plt section, relocations in rela.plt/rel.plt | |
3505 | section apply to .got.plt section. */ | |
3506 | abfd = reloc_sec->owner; | |
3507 | if (get_elf_backend_data (abfd)->want_got_plt | |
3508 | && strcmp (name, ".plt") == 0) | |
87070c08 AM |
3509 | { |
3510 | /* .got.plt is a linker created input section. It may be mapped | |
3511 | to some other output section. Try two likely sections. */ | |
3512 | name = ".got.plt"; | |
3513 | reloc_sec = bfd_get_section_by_name (abfd, name); | |
3514 | if (reloc_sec != NULL) | |
3515 | return reloc_sec; | |
3516 | name = ".got"; | |
3517 | } | |
bd53a53a L |
3518 | |
3519 | reloc_sec = bfd_get_section_by_name (abfd, name); | |
3520 | return reloc_sec; | |
3521 | } | |
3522 | ||
252b5132 RH |
3523 | /* Assign all ELF section numbers. The dummy first section is handled here |
3524 | too. The link/info pointers for the standard section types are filled | |
3525 | in here too, while we're at it. */ | |
3526 | ||
b34976b6 | 3527 | static bfd_boolean |
da9f89d4 | 3528 | assign_section_numbers (bfd *abfd, struct bfd_link_info *link_info) |
252b5132 RH |
3529 | { |
3530 | struct elf_obj_tdata *t = elf_tdata (abfd); | |
3531 | asection *sec; | |
3e19fb8f | 3532 | unsigned int section_number; |
252b5132 | 3533 | Elf_Internal_Shdr **i_shdrp; |
47cc2cf5 | 3534 | struct bfd_elf_section_data *d; |
3516e984 | 3535 | bfd_boolean need_symtab; |
252b5132 RH |
3536 | |
3537 | section_number = 1; | |
3538 | ||
2b0f7ef9 JJ |
3539 | _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd)); |
3540 | ||
da9f89d4 | 3541 | /* SHT_GROUP sections are in relocatable files only. */ |
0e1862bb | 3542 | if (link_info == NULL || bfd_link_relocatable (link_info)) |
252b5132 | 3543 | { |
14f2c699 L |
3544 | bfd_size_type reloc_count = 0; |
3545 | ||
da9f89d4 | 3546 | /* Put SHT_GROUP sections first. */ |
04dd1667 | 3547 | for (sec = abfd->sections; sec != NULL; sec = sec->next) |
47cc2cf5 | 3548 | { |
5daa8fe7 | 3549 | d = elf_section_data (sec); |
da9f89d4 L |
3550 | |
3551 | if (d->this_hdr.sh_type == SHT_GROUP) | |
08a40648 | 3552 | { |
5daa8fe7 | 3553 | if (sec->flags & SEC_LINKER_CREATED) |
da9f89d4 L |
3554 | { |
3555 | /* Remove the linker created SHT_GROUP sections. */ | |
5daa8fe7 | 3556 | bfd_section_list_remove (abfd, sec); |
da9f89d4 | 3557 | abfd->section_count--; |
da9f89d4 | 3558 | } |
08a40648 | 3559 | else |
4fbb74a6 | 3560 | d->this_idx = section_number++; |
da9f89d4 | 3561 | } |
14f2c699 L |
3562 | |
3563 | /* Count relocations. */ | |
3564 | reloc_count += sec->reloc_count; | |
47cc2cf5 | 3565 | } |
14f2c699 L |
3566 | |
3567 | /* Clear HAS_RELOC if there are no relocations. */ | |
3568 | if (reloc_count == 0) | |
3569 | abfd->flags &= ~HAS_RELOC; | |
47cc2cf5 PB |
3570 | } |
3571 | ||
3572 | for (sec = abfd->sections; sec; sec = sec->next) | |
3573 | { | |
3574 | d = elf_section_data (sec); | |
3575 | ||
3576 | if (d->this_hdr.sh_type != SHT_GROUP) | |
4fbb74a6 | 3577 | d->this_idx = section_number++; |
3e19fb8f L |
3578 | if (d->this_hdr.sh_name != (unsigned int) -1) |
3579 | _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->this_hdr.sh_name); | |
d4730f92 | 3580 | if (d->rel.hdr) |
2b0f7ef9 | 3581 | { |
d4730f92 | 3582 | d->rel.idx = section_number++; |
3e19fb8f L |
3583 | if (d->rel.hdr->sh_name != (unsigned int) -1) |
3584 | _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel.hdr->sh_name); | |
2b0f7ef9 | 3585 | } |
d4730f92 BS |
3586 | else |
3587 | d->rel.idx = 0; | |
23bc299b | 3588 | |
d4730f92 | 3589 | if (d->rela.hdr) |
2b0f7ef9 | 3590 | { |
d4730f92 | 3591 | d->rela.idx = section_number++; |
3e19fb8f L |
3592 | if (d->rela.hdr->sh_name != (unsigned int) -1) |
3593 | _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rela.hdr->sh_name); | |
2b0f7ef9 | 3594 | } |
23bc299b | 3595 | else |
d4730f92 | 3596 | d->rela.idx = 0; |
252b5132 RH |
3597 | } |
3598 | ||
12bd6957 | 3599 | elf_shstrtab_sec (abfd) = section_number++; |
2b0f7ef9 | 3600 | _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->shstrtab_hdr.sh_name); |
12bd6957 | 3601 | elf_elfheader (abfd)->e_shstrndx = elf_shstrtab_sec (abfd); |
252b5132 | 3602 | |
3516e984 L |
3603 | need_symtab = (bfd_get_symcount (abfd) > 0 |
3604 | || (link_info == NULL | |
3605 | && ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC)) | |
3606 | == HAS_RELOC))); | |
3607 | if (need_symtab) | |
252b5132 | 3608 | { |
12bd6957 | 3609 | elf_onesymtab (abfd) = section_number++; |
2b0f7ef9 | 3610 | _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->symtab_hdr.sh_name); |
4fbb74a6 | 3611 | if (section_number > ((SHN_LORESERVE - 2) & 0xFFFF)) |
9ad5cbcf | 3612 | { |
6a40cf0c NC |
3613 | elf_section_list * entry; |
3614 | ||
3615 | BFD_ASSERT (elf_symtab_shndx_list (abfd) == NULL); | |
3616 | ||
3617 | entry = bfd_zalloc (abfd, sizeof * entry); | |
3618 | entry->ndx = section_number++; | |
3619 | elf_symtab_shndx_list (abfd) = entry; | |
3620 | entry->hdr.sh_name | |
9ad5cbcf | 3621 | = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), |
b34976b6 | 3622 | ".symtab_shndx", FALSE); |
6a40cf0c | 3623 | if (entry->hdr.sh_name == (unsigned int) -1) |
b34976b6 | 3624 | return FALSE; |
9ad5cbcf | 3625 | } |
12bd6957 | 3626 | elf_strtab_sec (abfd) = section_number++; |
2b0f7ef9 | 3627 | _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->strtab_hdr.sh_name); |
252b5132 RH |
3628 | } |
3629 | ||
1c52a645 L |
3630 | if (section_number >= SHN_LORESERVE) |
3631 | { | |
3632 | _bfd_error_handler (_("%B: too many sections: %u"), | |
3633 | abfd, section_number); | |
3634 | return FALSE; | |
3635 | } | |
3636 | ||
9ad5cbcf | 3637 | elf_numsections (abfd) = section_number; |
252b5132 RH |
3638 | elf_elfheader (abfd)->e_shnum = section_number; |
3639 | ||
3640 | /* Set up the list of section header pointers, in agreement with the | |
3641 | indices. */ | |
a50b1753 NC |
3642 | i_shdrp = (Elf_Internal_Shdr **) bfd_zalloc2 (abfd, section_number, |
3643 | sizeof (Elf_Internal_Shdr *)); | |
252b5132 | 3644 | if (i_shdrp == NULL) |
b34976b6 | 3645 | return FALSE; |
252b5132 | 3646 | |
a50b1753 NC |
3647 | i_shdrp[0] = (Elf_Internal_Shdr *) bfd_zalloc (abfd, |
3648 | sizeof (Elf_Internal_Shdr)); | |
252b5132 RH |
3649 | if (i_shdrp[0] == NULL) |
3650 | { | |
3651 | bfd_release (abfd, i_shdrp); | |
b34976b6 | 3652 | return FALSE; |
252b5132 | 3653 | } |
252b5132 RH |
3654 | |
3655 | elf_elfsections (abfd) = i_shdrp; | |
3656 | ||
12bd6957 | 3657 | i_shdrp[elf_shstrtab_sec (abfd)] = &t->shstrtab_hdr; |
3516e984 | 3658 | if (need_symtab) |
252b5132 | 3659 | { |
12bd6957 | 3660 | i_shdrp[elf_onesymtab (abfd)] = &t->symtab_hdr; |
4fbb74a6 | 3661 | if (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF)) |
9ad5cbcf | 3662 | { |
6a40cf0c NC |
3663 | elf_section_list * entry = elf_symtab_shndx_list (abfd); |
3664 | BFD_ASSERT (entry != NULL); | |
3665 | i_shdrp[entry->ndx] = & entry->hdr; | |
3666 | entry->hdr.sh_link = elf_onesymtab (abfd); | |
9ad5cbcf | 3667 | } |
12bd6957 AM |
3668 | i_shdrp[elf_strtab_sec (abfd)] = &t->strtab_hdr; |
3669 | t->symtab_hdr.sh_link = elf_strtab_sec (abfd); | |
252b5132 | 3670 | } |
38ce5b11 | 3671 | |
252b5132 RH |
3672 | for (sec = abfd->sections; sec; sec = sec->next) |
3673 | { | |
252b5132 | 3674 | asection *s; |
252b5132 | 3675 | |
91d6fa6a NC |
3676 | d = elf_section_data (sec); |
3677 | ||
252b5132 | 3678 | i_shdrp[d->this_idx] = &d->this_hdr; |
d4730f92 BS |
3679 | if (d->rel.idx != 0) |
3680 | i_shdrp[d->rel.idx] = d->rel.hdr; | |
3681 | if (d->rela.idx != 0) | |
3682 | i_shdrp[d->rela.idx] = d->rela.hdr; | |
252b5132 RH |
3683 | |
3684 | /* Fill in the sh_link and sh_info fields while we're at it. */ | |
3685 | ||
3686 | /* sh_link of a reloc section is the section index of the symbol | |
3687 | table. sh_info is the section index of the section to which | |
3688 | the relocation entries apply. */ | |
d4730f92 | 3689 | if (d->rel.idx != 0) |
252b5132 | 3690 | { |
12bd6957 | 3691 | d->rel.hdr->sh_link = elf_onesymtab (abfd); |
d4730f92 | 3692 | d->rel.hdr->sh_info = d->this_idx; |
9ef5d938 | 3693 | d->rel.hdr->sh_flags |= SHF_INFO_LINK; |
252b5132 | 3694 | } |
d4730f92 | 3695 | if (d->rela.idx != 0) |
23bc299b | 3696 | { |
12bd6957 | 3697 | d->rela.hdr->sh_link = elf_onesymtab (abfd); |
d4730f92 | 3698 | d->rela.hdr->sh_info = d->this_idx; |
9ef5d938 | 3699 | d->rela.hdr->sh_flags |= SHF_INFO_LINK; |
23bc299b | 3700 | } |
252b5132 | 3701 | |
38ce5b11 L |
3702 | /* We need to set up sh_link for SHF_LINK_ORDER. */ |
3703 | if ((d->this_hdr.sh_flags & SHF_LINK_ORDER) != 0) | |
3704 | { | |
3705 | s = elf_linked_to_section (sec); | |
3706 | if (s) | |
38ce5b11 | 3707 | { |
f2876037 | 3708 | /* elf_linked_to_section points to the input section. */ |
ccd2ec6a | 3709 | if (link_info != NULL) |
38ce5b11 | 3710 | { |
f2876037 | 3711 | /* Check discarded linkonce section. */ |
dbaa2011 | 3712 | if (discarded_section (s)) |
38ce5b11 | 3713 | { |
ccd2ec6a L |
3714 | asection *kept; |
3715 | (*_bfd_error_handler) | |
3716 | (_("%B: sh_link of section `%A' points to discarded section `%A' of `%B'"), | |
3717 | abfd, d->this_hdr.bfd_section, | |
3718 | s, s->owner); | |
3719 | /* Point to the kept section if it has the same | |
3720 | size as the discarded one. */ | |
c0f00686 | 3721 | kept = _bfd_elf_check_kept_section (s, link_info); |
ccd2ec6a | 3722 | if (kept == NULL) |
185d09ad | 3723 | { |
ccd2ec6a L |
3724 | bfd_set_error (bfd_error_bad_value); |
3725 | return FALSE; | |
185d09ad | 3726 | } |
ccd2ec6a | 3727 | s = kept; |
38ce5b11 | 3728 | } |
e424ecc8 | 3729 | |
ccd2ec6a L |
3730 | s = s->output_section; |
3731 | BFD_ASSERT (s != NULL); | |
38ce5b11 | 3732 | } |
f2876037 L |
3733 | else |
3734 | { | |
3735 | /* Handle objcopy. */ | |
3736 | if (s->output_section == NULL) | |
3737 | { | |
3738 | (*_bfd_error_handler) | |
3739 | (_("%B: sh_link of section `%A' points to removed section `%A' of `%B'"), | |
3740 | abfd, d->this_hdr.bfd_section, s, s->owner); | |
3741 | bfd_set_error (bfd_error_bad_value); | |
3742 | return FALSE; | |
3743 | } | |
3744 | s = s->output_section; | |
3745 | } | |
ccd2ec6a L |
3746 | d->this_hdr.sh_link = elf_section_data (s)->this_idx; |
3747 | } | |
3748 | else | |
3749 | { | |
3750 | /* PR 290: | |
3751 | The Intel C compiler generates SHT_IA_64_UNWIND with | |
3752 | SHF_LINK_ORDER. But it doesn't set the sh_link or | |
3753 | sh_info fields. Hence we could get the situation | |
08a40648 | 3754 | where s is NULL. */ |
ccd2ec6a L |
3755 | const struct elf_backend_data *bed |
3756 | = get_elf_backend_data (abfd); | |
3757 | if (bed->link_order_error_handler) | |
3758 | bed->link_order_error_handler | |
3759 | (_("%B: warning: sh_link not set for section `%A'"), | |
3760 | abfd, sec); | |
38ce5b11 L |
3761 | } |
3762 | } | |
3763 | ||
252b5132 RH |
3764 | switch (d->this_hdr.sh_type) |
3765 | { | |
3766 | case SHT_REL: | |
3767 | case SHT_RELA: | |
3768 | /* A reloc section which we are treating as a normal BFD | |
3769 | section. sh_link is the section index of the symbol | |
3770 | table. sh_info is the section index of the section to | |
3771 | which the relocation entries apply. We assume that an | |
3772 | allocated reloc section uses the dynamic symbol table. | |
3773 | FIXME: How can we be sure? */ | |
3774 | s = bfd_get_section_by_name (abfd, ".dynsym"); | |
3775 | if (s != NULL) | |
3776 | d->this_hdr.sh_link = elf_section_data (s)->this_idx; | |
3777 | ||
bd53a53a | 3778 | s = get_elf_backend_data (abfd)->get_reloc_section (sec); |
252b5132 | 3779 | if (s != NULL) |
9ef5d938 L |
3780 | { |
3781 | d->this_hdr.sh_info = elf_section_data (s)->this_idx; | |
3782 | d->this_hdr.sh_flags |= SHF_INFO_LINK; | |
3783 | } | |
252b5132 RH |
3784 | break; |
3785 | ||
3786 | case SHT_STRTAB: | |
3787 | /* We assume that a section named .stab*str is a stabs | |
3788 | string section. We look for a section with the same name | |
3789 | but without the trailing ``str'', and set its sh_link | |
3790 | field to point to this section. */ | |
0112cd26 | 3791 | if (CONST_STRNEQ (sec->name, ".stab") |
252b5132 RH |
3792 | && strcmp (sec->name + strlen (sec->name) - 3, "str") == 0) |
3793 | { | |
3794 | size_t len; | |
3795 | char *alc; | |
3796 | ||
3797 | len = strlen (sec->name); | |
a50b1753 | 3798 | alc = (char *) bfd_malloc (len - 2); |
252b5132 | 3799 | if (alc == NULL) |
b34976b6 | 3800 | return FALSE; |
d4c88bbb | 3801 | memcpy (alc, sec->name, len - 3); |
252b5132 RH |
3802 | alc[len - 3] = '\0'; |
3803 | s = bfd_get_section_by_name (abfd, alc); | |
3804 | free (alc); | |
3805 | if (s != NULL) | |
3806 | { | |
3807 | elf_section_data (s)->this_hdr.sh_link = d->this_idx; | |
3808 | ||
3809 | /* This is a .stab section. */ | |
0594c12d AM |
3810 | if (elf_section_data (s)->this_hdr.sh_entsize == 0) |
3811 | elf_section_data (s)->this_hdr.sh_entsize | |
3812 | = 4 + 2 * bfd_get_arch_size (abfd) / 8; | |
252b5132 RH |
3813 | } |
3814 | } | |
3815 | break; | |
3816 | ||
3817 | case SHT_DYNAMIC: | |
3818 | case SHT_DYNSYM: | |
3819 | case SHT_GNU_verneed: | |
3820 | case SHT_GNU_verdef: | |
3821 | /* sh_link is the section header index of the string table | |
3822 | used for the dynamic entries, or the symbol table, or the | |
3823 | version strings. */ | |
3824 | s = bfd_get_section_by_name (abfd, ".dynstr"); | |
3825 | if (s != NULL) | |
3826 | d->this_hdr.sh_link = elf_section_data (s)->this_idx; | |
3827 | break; | |
3828 | ||
7f1204bb JJ |
3829 | case SHT_GNU_LIBLIST: |
3830 | /* sh_link is the section header index of the prelink library | |
08a40648 AM |
3831 | list used for the dynamic entries, or the symbol table, or |
3832 | the version strings. */ | |
7f1204bb JJ |
3833 | s = bfd_get_section_by_name (abfd, (sec->flags & SEC_ALLOC) |
3834 | ? ".dynstr" : ".gnu.libstr"); | |
3835 | if (s != NULL) | |
3836 | d->this_hdr.sh_link = elf_section_data (s)->this_idx; | |
3837 | break; | |
3838 | ||
252b5132 | 3839 | case SHT_HASH: |
fdc90cb4 | 3840 | case SHT_GNU_HASH: |
252b5132 RH |
3841 | case SHT_GNU_versym: |
3842 | /* sh_link is the section header index of the symbol table | |
3843 | this hash table or version table is for. */ | |
3844 | s = bfd_get_section_by_name (abfd, ".dynsym"); | |
3845 | if (s != NULL) | |
3846 | d->this_hdr.sh_link = elf_section_data (s)->this_idx; | |
3847 | break; | |
dbb410c3 AM |
3848 | |
3849 | case SHT_GROUP: | |
12bd6957 | 3850 | d->this_hdr.sh_link = elf_onesymtab (abfd); |
252b5132 RH |
3851 | } |
3852 | } | |
3853 | ||
3e19fb8f L |
3854 | /* Delay setting sh_name to _bfd_elf_write_object_contents so that |
3855 | _bfd_elf_assign_file_positions_for_non_load can convert DWARF | |
3856 | debug section name from .debug_* to .zdebug_* if needed. */ | |
3857 | ||
b34976b6 | 3858 | return TRUE; |
252b5132 RH |
3859 | } |
3860 | ||
5372391b | 3861 | static bfd_boolean |
217aa764 | 3862 | sym_is_global (bfd *abfd, asymbol *sym) |
252b5132 RH |
3863 | { |
3864 | /* If the backend has a special mapping, use it. */ | |
9c5bfbb7 | 3865 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
217aa764 AM |
3866 | if (bed->elf_backend_sym_is_global) |
3867 | return (*bed->elf_backend_sym_is_global) (abfd, sym); | |
252b5132 | 3868 | |
e47bf690 | 3869 | return ((sym->flags & (BSF_GLOBAL | BSF_WEAK | BSF_GNU_UNIQUE)) != 0 |
252b5132 RH |
3870 | || bfd_is_und_section (bfd_get_section (sym)) |
3871 | || bfd_is_com_section (bfd_get_section (sym))); | |
3872 | } | |
3873 | ||
5372391b | 3874 | /* Don't output section symbols for sections that are not going to be |
c6d8cab4 | 3875 | output, that are duplicates or there is no BFD section. */ |
5372391b AM |
3876 | |
3877 | static bfd_boolean | |
3878 | ignore_section_sym (bfd *abfd, asymbol *sym) | |
3879 | { | |
c6d8cab4 L |
3880 | elf_symbol_type *type_ptr; |
3881 | ||
3882 | if ((sym->flags & BSF_SECTION_SYM) == 0) | |
3883 | return FALSE; | |
3884 | ||
3885 | type_ptr = elf_symbol_from (abfd, sym); | |
3886 | return ((type_ptr != NULL | |
3887 | && type_ptr->internal_elf_sym.st_shndx != 0 | |
3888 | && bfd_is_abs_section (sym->section)) | |
3889 | || !(sym->section->owner == abfd | |
0f0a5e58 | 3890 | || (sym->section->output_section->owner == abfd |
2633a79c AM |
3891 | && sym->section->output_offset == 0) |
3892 | || bfd_is_abs_section (sym->section))); | |
5372391b AM |
3893 | } |
3894 | ||
2633a79c AM |
3895 | /* Map symbol from it's internal number to the external number, moving |
3896 | all local symbols to be at the head of the list. */ | |
3897 | ||
b34976b6 | 3898 | static bfd_boolean |
12bd6957 | 3899 | elf_map_symbols (bfd *abfd, unsigned int *pnum_locals) |
252b5132 | 3900 | { |
dc810e39 | 3901 | unsigned int symcount = bfd_get_symcount (abfd); |
252b5132 RH |
3902 | asymbol **syms = bfd_get_outsymbols (abfd); |
3903 | asymbol **sect_syms; | |
dc810e39 AM |
3904 | unsigned int num_locals = 0; |
3905 | unsigned int num_globals = 0; | |
3906 | unsigned int num_locals2 = 0; | |
3907 | unsigned int num_globals2 = 0; | |
7292b3ac | 3908 | unsigned int max_index = 0; |
dc810e39 | 3909 | unsigned int idx; |
252b5132 RH |
3910 | asection *asect; |
3911 | asymbol **new_syms; | |
252b5132 RH |
3912 | |
3913 | #ifdef DEBUG | |
3914 | fprintf (stderr, "elf_map_symbols\n"); | |
3915 | fflush (stderr); | |
3916 | #endif | |
3917 | ||
252b5132 RH |
3918 | for (asect = abfd->sections; asect; asect = asect->next) |
3919 | { | |
3920 | if (max_index < asect->index) | |
3921 | max_index = asect->index; | |
3922 | } | |
3923 | ||
3924 | max_index++; | |
a50b1753 | 3925 | sect_syms = (asymbol **) bfd_zalloc2 (abfd, max_index, sizeof (asymbol *)); |
252b5132 | 3926 | if (sect_syms == NULL) |
b34976b6 | 3927 | return FALSE; |
252b5132 | 3928 | elf_section_syms (abfd) = sect_syms; |
4e89ac30 | 3929 | elf_num_section_syms (abfd) = max_index; |
252b5132 | 3930 | |
079e9a2f AM |
3931 | /* Init sect_syms entries for any section symbols we have already |
3932 | decided to output. */ | |
252b5132 RH |
3933 | for (idx = 0; idx < symcount; idx++) |
3934 | { | |
dc810e39 | 3935 | asymbol *sym = syms[idx]; |
c044fabd | 3936 | |
252b5132 | 3937 | if ((sym->flags & BSF_SECTION_SYM) != 0 |
0f0a5e58 | 3938 | && sym->value == 0 |
2633a79c AM |
3939 | && !ignore_section_sym (abfd, sym) |
3940 | && !bfd_is_abs_section (sym->section)) | |
252b5132 | 3941 | { |
5372391b | 3942 | asection *sec = sym->section; |
252b5132 | 3943 | |
5372391b AM |
3944 | if (sec->owner != abfd) |
3945 | sec = sec->output_section; | |
252b5132 | 3946 | |
5372391b | 3947 | sect_syms[sec->index] = syms[idx]; |
252b5132 RH |
3948 | } |
3949 | } | |
3950 | ||
252b5132 RH |
3951 | /* Classify all of the symbols. */ |
3952 | for (idx = 0; idx < symcount; idx++) | |
3953 | { | |
2633a79c | 3954 | if (sym_is_global (abfd, syms[idx])) |
252b5132 | 3955 | num_globals++; |
2633a79c AM |
3956 | else if (!ignore_section_sym (abfd, syms[idx])) |
3957 | num_locals++; | |
252b5132 | 3958 | } |
079e9a2f | 3959 | |
5372391b | 3960 | /* We will be adding a section symbol for each normal BFD section. Most |
079e9a2f AM |
3961 | sections will already have a section symbol in outsymbols, but |
3962 | eg. SHT_GROUP sections will not, and we need the section symbol mapped | |
3963 | at least in that case. */ | |
252b5132 RH |
3964 | for (asect = abfd->sections; asect; asect = asect->next) |
3965 | { | |
079e9a2f | 3966 | if (sect_syms[asect->index] == NULL) |
252b5132 | 3967 | { |
079e9a2f | 3968 | if (!sym_is_global (abfd, asect->symbol)) |
252b5132 RH |
3969 | num_locals++; |
3970 | else | |
3971 | num_globals++; | |
252b5132 RH |
3972 | } |
3973 | } | |
3974 | ||
3975 | /* Now sort the symbols so the local symbols are first. */ | |
a50b1753 NC |
3976 | new_syms = (asymbol **) bfd_alloc2 (abfd, num_locals + num_globals, |
3977 | sizeof (asymbol *)); | |
dc810e39 | 3978 | |
252b5132 | 3979 | if (new_syms == NULL) |
b34976b6 | 3980 | return FALSE; |
252b5132 RH |
3981 | |
3982 | for (idx = 0; idx < symcount; idx++) | |
3983 | { | |
3984 | asymbol *sym = syms[idx]; | |
dc810e39 | 3985 | unsigned int i; |
252b5132 | 3986 | |
2633a79c AM |
3987 | if (sym_is_global (abfd, sym)) |
3988 | i = num_locals + num_globals2++; | |
3989 | else if (!ignore_section_sym (abfd, sym)) | |
252b5132 RH |
3990 | i = num_locals2++; |
3991 | else | |
2633a79c | 3992 | continue; |
252b5132 RH |
3993 | new_syms[i] = sym; |
3994 | sym->udata.i = i + 1; | |
3995 | } | |
3996 | for (asect = abfd->sections; asect; asect = asect->next) | |
3997 | { | |
079e9a2f | 3998 | if (sect_syms[asect->index] == NULL) |
252b5132 | 3999 | { |
079e9a2f | 4000 | asymbol *sym = asect->symbol; |
dc810e39 | 4001 | unsigned int i; |
252b5132 | 4002 | |
079e9a2f | 4003 | sect_syms[asect->index] = sym; |
252b5132 RH |
4004 | if (!sym_is_global (abfd, sym)) |
4005 | i = num_locals2++; | |
4006 | else | |
4007 | i = num_locals + num_globals2++; | |
4008 | new_syms[i] = sym; | |
4009 | sym->udata.i = i + 1; | |
4010 | } | |
4011 | } | |
4012 | ||
4013 | bfd_set_symtab (abfd, new_syms, num_locals + num_globals); | |
4014 | ||
12bd6957 | 4015 | *pnum_locals = num_locals; |
b34976b6 | 4016 | return TRUE; |
252b5132 RH |
4017 | } |
4018 | ||
4019 | /* Align to the maximum file alignment that could be required for any | |
4020 | ELF data structure. */ | |
4021 | ||
268b6b39 | 4022 | static inline file_ptr |
217aa764 | 4023 | align_file_position (file_ptr off, int align) |
252b5132 RH |
4024 | { |
4025 | return (off + align - 1) & ~(align - 1); | |
4026 | } | |
4027 | ||
4028 | /* Assign a file position to a section, optionally aligning to the | |
4029 | required section alignment. */ | |
4030 | ||
217aa764 AM |
4031 | file_ptr |
4032 | _bfd_elf_assign_file_position_for_section (Elf_Internal_Shdr *i_shdrp, | |
4033 | file_ptr offset, | |
4034 | bfd_boolean align) | |
252b5132 | 4035 | { |
72de5009 AM |
4036 | if (align && i_shdrp->sh_addralign > 1) |
4037 | offset = BFD_ALIGN (offset, i_shdrp->sh_addralign); | |
252b5132 RH |
4038 | i_shdrp->sh_offset = offset; |
4039 | if (i_shdrp->bfd_section != NULL) | |
4040 | i_shdrp->bfd_section->filepos = offset; | |
4041 | if (i_shdrp->sh_type != SHT_NOBITS) | |
4042 | offset += i_shdrp->sh_size; | |
4043 | return offset; | |
4044 | } | |
4045 | ||
4046 | /* Compute the file positions we are going to put the sections at, and | |
4047 | otherwise prepare to begin writing out the ELF file. If LINK_INFO | |
4048 | is not NULL, this is being called by the ELF backend linker. */ | |
4049 | ||
b34976b6 | 4050 | bfd_boolean |
217aa764 AM |
4051 | _bfd_elf_compute_section_file_positions (bfd *abfd, |
4052 | struct bfd_link_info *link_info) | |
252b5132 | 4053 | { |
9c5bfbb7 | 4054 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
d4730f92 | 4055 | struct fake_section_arg fsargs; |
b34976b6 | 4056 | bfd_boolean failed; |
ef10c3ac | 4057 | struct elf_strtab_hash *strtab = NULL; |
252b5132 | 4058 | Elf_Internal_Shdr *shstrtab_hdr; |
3516e984 | 4059 | bfd_boolean need_symtab; |
252b5132 RH |
4060 | |
4061 | if (abfd->output_has_begun) | |
b34976b6 | 4062 | return TRUE; |
252b5132 RH |
4063 | |
4064 | /* Do any elf backend specific processing first. */ | |
4065 | if (bed->elf_backend_begin_write_processing) | |
4066 | (*bed->elf_backend_begin_write_processing) (abfd, link_info); | |
4067 | ||
4068 | if (! prep_headers (abfd)) | |
b34976b6 | 4069 | return FALSE; |
252b5132 | 4070 | |
e6c51ed4 | 4071 | /* Post process the headers if necessary. */ |
78245035 | 4072 | (*bed->elf_backend_post_process_headers) (abfd, link_info); |
e6c51ed4 | 4073 | |
d4730f92 BS |
4074 | fsargs.failed = FALSE; |
4075 | fsargs.link_info = link_info; | |
4076 | bfd_map_over_sections (abfd, elf_fake_sections, &fsargs); | |
4077 | if (fsargs.failed) | |
b34976b6 | 4078 | return FALSE; |
252b5132 | 4079 | |
da9f89d4 | 4080 | if (!assign_section_numbers (abfd, link_info)) |
b34976b6 | 4081 | return FALSE; |
252b5132 RH |
4082 | |
4083 | /* The backend linker builds symbol table information itself. */ | |
3516e984 L |
4084 | need_symtab = (link_info == NULL |
4085 | && (bfd_get_symcount (abfd) > 0 | |
4086 | || ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC)) | |
4087 | == HAS_RELOC))); | |
4088 | if (need_symtab) | |
252b5132 RH |
4089 | { |
4090 | /* Non-zero if doing a relocatable link. */ | |
4091 | int relocatable_p = ! (abfd->flags & (EXEC_P | DYNAMIC)); | |
4092 | ||
4093 | if (! swap_out_syms (abfd, &strtab, relocatable_p)) | |
b34976b6 | 4094 | return FALSE; |
252b5132 RH |
4095 | } |
4096 | ||
d4730f92 | 4097 | failed = FALSE; |
1126897b | 4098 | if (link_info == NULL) |
dbb410c3 | 4099 | { |
1126897b | 4100 | bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed); |
dbb410c3 | 4101 | if (failed) |
b34976b6 | 4102 | return FALSE; |
dbb410c3 AM |
4103 | } |
4104 | ||
252b5132 RH |
4105 | shstrtab_hdr = &elf_tdata (abfd)->shstrtab_hdr; |
4106 | /* sh_name was set in prep_headers. */ | |
4107 | shstrtab_hdr->sh_type = SHT_STRTAB; | |
84865015 | 4108 | shstrtab_hdr->sh_flags = bed->elf_strtab_flags; |
252b5132 | 4109 | shstrtab_hdr->sh_addr = 0; |
946748d5 | 4110 | /* sh_size is set in _bfd_elf_assign_file_positions_for_non_load. */ |
252b5132 RH |
4111 | shstrtab_hdr->sh_entsize = 0; |
4112 | shstrtab_hdr->sh_link = 0; | |
4113 | shstrtab_hdr->sh_info = 0; | |
3e19fb8f | 4114 | /* sh_offset is set in _bfd_elf_assign_file_positions_for_non_load. */ |
252b5132 RH |
4115 | shstrtab_hdr->sh_addralign = 1; |
4116 | ||
c84fca4d | 4117 | if (!assign_file_positions_except_relocs (abfd, link_info)) |
b34976b6 | 4118 | return FALSE; |
252b5132 | 4119 | |
3516e984 | 4120 | if (need_symtab) |
252b5132 RH |
4121 | { |
4122 | file_ptr off; | |
4123 | Elf_Internal_Shdr *hdr; | |
4124 | ||
12bd6957 | 4125 | off = elf_next_file_pos (abfd); |
252b5132 | 4126 | |
6a40cf0c | 4127 | hdr = & elf_symtab_hdr (abfd); |
b34976b6 | 4128 | off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE); |
252b5132 | 4129 | |
6a40cf0c NC |
4130 | if (elf_symtab_shndx_list (abfd) != NULL) |
4131 | { | |
4132 | hdr = & elf_symtab_shndx_list (abfd)->hdr; | |
4133 | if (hdr->sh_size != 0) | |
4134 | off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE); | |
4135 | /* FIXME: What about other symtab_shndx sections in the list ? */ | |
4136 | } | |
9ad5cbcf | 4137 | |
252b5132 | 4138 | hdr = &elf_tdata (abfd)->strtab_hdr; |
b34976b6 | 4139 | off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE); |
252b5132 | 4140 | |
12bd6957 | 4141 | elf_next_file_pos (abfd) = off; |
252b5132 RH |
4142 | |
4143 | /* Now that we know where the .strtab section goes, write it | |
08a40648 | 4144 | out. */ |
252b5132 | 4145 | if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0 |
ef10c3ac | 4146 | || ! _bfd_elf_strtab_emit (abfd, strtab)) |
b34976b6 | 4147 | return FALSE; |
ef10c3ac | 4148 | _bfd_elf_strtab_free (strtab); |
252b5132 RH |
4149 | } |
4150 | ||
b34976b6 | 4151 | abfd->output_has_begun = TRUE; |
252b5132 | 4152 | |
b34976b6 | 4153 | return TRUE; |
252b5132 RH |
4154 | } |
4155 | ||
8ded5a0f AM |
4156 | /* Make an initial estimate of the size of the program header. If we |
4157 | get the number wrong here, we'll redo section placement. */ | |
4158 | ||
4159 | static bfd_size_type | |
4160 | get_program_header_size (bfd *abfd, struct bfd_link_info *info) | |
4161 | { | |
4162 | size_t segs; | |
4163 | asection *s; | |
2b05f1b7 | 4164 | const struct elf_backend_data *bed; |
8ded5a0f AM |
4165 | |
4166 | /* Assume we will need exactly two PT_LOAD segments: one for text | |
4167 | and one for data. */ | |
4168 | segs = 2; | |
4169 | ||
4170 | s = bfd_get_section_by_name (abfd, ".interp"); | |
4171 | if (s != NULL && (s->flags & SEC_LOAD) != 0) | |
4172 | { | |
4173 | /* If we have a loadable interpreter section, we need a | |
4174 | PT_INTERP segment. In this case, assume we also need a | |
4175 | PT_PHDR segment, although that may not be true for all | |
4176 | targets. */ | |
4177 | segs += 2; | |
4178 | } | |
4179 | ||
4180 | if (bfd_get_section_by_name (abfd, ".dynamic") != NULL) | |
4181 | { | |
4182 | /* We need a PT_DYNAMIC segment. */ | |
4183 | ++segs; | |
f210dcff | 4184 | } |
08a40648 | 4185 | |
ceae84aa | 4186 | if (info != NULL && info->relro) |
f210dcff L |
4187 | { |
4188 | /* We need a PT_GNU_RELRO segment. */ | |
4189 | ++segs; | |
8ded5a0f AM |
4190 | } |
4191 | ||
12bd6957 | 4192 | if (elf_eh_frame_hdr (abfd)) |
8ded5a0f AM |
4193 | { |
4194 | /* We need a PT_GNU_EH_FRAME segment. */ | |
4195 | ++segs; | |
4196 | } | |
4197 | ||
12bd6957 | 4198 | if (elf_stack_flags (abfd)) |
8ded5a0f | 4199 | { |
2b05f1b7 L |
4200 | /* We need a PT_GNU_STACK segment. */ |
4201 | ++segs; | |
4202 | } | |
94b11780 | 4203 | |
2b05f1b7 L |
4204 | for (s = abfd->sections; s != NULL; s = s->next) |
4205 | { | |
8ded5a0f | 4206 | if ((s->flags & SEC_LOAD) != 0 |
0112cd26 | 4207 | && CONST_STRNEQ (s->name, ".note")) |
8ded5a0f AM |
4208 | { |
4209 | /* We need a PT_NOTE segment. */ | |
4210 | ++segs; | |
1c5265b5 JJ |
4211 | /* Try to create just one PT_NOTE segment |
4212 | for all adjacent loadable .note* sections. | |
4213 | gABI requires that within a PT_NOTE segment | |
4214 | (and also inside of each SHT_NOTE section) | |
4215 | each note is padded to a multiple of 4 size, | |
4216 | so we check whether the sections are correctly | |
4217 | aligned. */ | |
4218 | if (s->alignment_power == 2) | |
4219 | while (s->next != NULL | |
4220 | && s->next->alignment_power == 2 | |
4221 | && (s->next->flags & SEC_LOAD) != 0 | |
4222 | && CONST_STRNEQ (s->next->name, ".note")) | |
4223 | s = s->next; | |
8ded5a0f AM |
4224 | } |
4225 | } | |
4226 | ||
4227 | for (s = abfd->sections; s != NULL; s = s->next) | |
4228 | { | |
4229 | if (s->flags & SEC_THREAD_LOCAL) | |
4230 | { | |
4231 | /* We need a PT_TLS segment. */ | |
4232 | ++segs; | |
4233 | break; | |
4234 | } | |
4235 | } | |
4236 | ||
4237 | /* Let the backend count up any program headers it might need. */ | |
2b05f1b7 | 4238 | bed = get_elf_backend_data (abfd); |
8ded5a0f AM |
4239 | if (bed->elf_backend_additional_program_headers) |
4240 | { | |
4241 | int a; | |
4242 | ||
4243 | a = (*bed->elf_backend_additional_program_headers) (abfd, info); | |
4244 | if (a == -1) | |
4245 | abort (); | |
4246 | segs += a; | |
4247 | } | |
4248 | ||
4249 | return segs * bed->s->sizeof_phdr; | |
4250 | } | |
4251 | ||
2ea37f1c NC |
4252 | /* Find the segment that contains the output_section of section. */ |
4253 | ||
4254 | Elf_Internal_Phdr * | |
4255 | _bfd_elf_find_segment_containing_section (bfd * abfd, asection * section) | |
4256 | { | |
4257 | struct elf_segment_map *m; | |
4258 | Elf_Internal_Phdr *p; | |
4259 | ||
12bd6957 | 4260 | for (m = elf_seg_map (abfd), p = elf_tdata (abfd)->phdr; |
2ea37f1c NC |
4261 | m != NULL; |
4262 | m = m->next, p++) | |
4263 | { | |
4264 | int i; | |
4265 | ||
4266 | for (i = m->count - 1; i >= 0; i--) | |
4267 | if (m->sections[i] == section) | |
4268 | return p; | |
4269 | } | |
4270 | ||
4271 | return NULL; | |
4272 | } | |
4273 | ||
252b5132 RH |
4274 | /* Create a mapping from a set of sections to a program segment. */ |
4275 | ||
217aa764 AM |
4276 | static struct elf_segment_map * |
4277 | make_mapping (bfd *abfd, | |
4278 | asection **sections, | |
4279 | unsigned int from, | |
4280 | unsigned int to, | |
4281 | bfd_boolean phdr) | |
252b5132 RH |
4282 | { |
4283 | struct elf_segment_map *m; | |
4284 | unsigned int i; | |
4285 | asection **hdrpp; | |
dc810e39 | 4286 | bfd_size_type amt; |
252b5132 | 4287 | |
dc810e39 AM |
4288 | amt = sizeof (struct elf_segment_map); |
4289 | amt += (to - from - 1) * sizeof (asection *); | |
a50b1753 | 4290 | m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); |
252b5132 RH |
4291 | if (m == NULL) |
4292 | return NULL; | |
4293 | m->next = NULL; | |
4294 | m->p_type = PT_LOAD; | |
4295 | for (i = from, hdrpp = sections + from; i < to; i++, hdrpp++) | |
4296 | m->sections[i - from] = *hdrpp; | |
4297 | m->count = to - from; | |
4298 | ||
4299 | if (from == 0 && phdr) | |
4300 | { | |
4301 | /* Include the headers in the first PT_LOAD segment. */ | |
4302 | m->includes_filehdr = 1; | |
4303 | m->includes_phdrs = 1; | |
4304 | } | |
4305 | ||
4306 | return m; | |
4307 | } | |
4308 | ||
229fcec5 MM |
4309 | /* Create the PT_DYNAMIC segment, which includes DYNSEC. Returns NULL |
4310 | on failure. */ | |
4311 | ||
4312 | struct elf_segment_map * | |
4313 | _bfd_elf_make_dynamic_segment (bfd *abfd, asection *dynsec) | |
4314 | { | |
4315 | struct elf_segment_map *m; | |
4316 | ||
a50b1753 NC |
4317 | m = (struct elf_segment_map *) bfd_zalloc (abfd, |
4318 | sizeof (struct elf_segment_map)); | |
229fcec5 MM |
4319 | if (m == NULL) |
4320 | return NULL; | |
4321 | m->next = NULL; | |
4322 | m->p_type = PT_DYNAMIC; | |
4323 | m->count = 1; | |
4324 | m->sections[0] = dynsec; | |
08a40648 | 4325 | |
229fcec5 MM |
4326 | return m; |
4327 | } | |
4328 | ||
8ded5a0f | 4329 | /* Possibly add or remove segments from the segment map. */ |
252b5132 | 4330 | |
b34976b6 | 4331 | static bfd_boolean |
3dea8fca AM |
4332 | elf_modify_segment_map (bfd *abfd, |
4333 | struct bfd_link_info *info, | |
4334 | bfd_boolean remove_empty_load) | |
252b5132 | 4335 | { |
252e386e | 4336 | struct elf_segment_map **m; |
8ded5a0f | 4337 | const struct elf_backend_data *bed; |
252b5132 | 4338 | |
8ded5a0f AM |
4339 | /* The placement algorithm assumes that non allocated sections are |
4340 | not in PT_LOAD segments. We ensure this here by removing such | |
4341 | sections from the segment map. We also remove excluded | |
252e386e AM |
4342 | sections. Finally, any PT_LOAD segment without sections is |
4343 | removed. */ | |
12bd6957 | 4344 | m = &elf_seg_map (abfd); |
252e386e | 4345 | while (*m) |
8ded5a0f AM |
4346 | { |
4347 | unsigned int i, new_count; | |
252b5132 | 4348 | |
252e386e | 4349 | for (new_count = 0, i = 0; i < (*m)->count; i++) |
8ded5a0f | 4350 | { |
252e386e AM |
4351 | if (((*m)->sections[i]->flags & SEC_EXCLUDE) == 0 |
4352 | && (((*m)->sections[i]->flags & SEC_ALLOC) != 0 | |
4353 | || (*m)->p_type != PT_LOAD)) | |
8ded5a0f | 4354 | { |
252e386e AM |
4355 | (*m)->sections[new_count] = (*m)->sections[i]; |
4356 | new_count++; | |
8ded5a0f AM |
4357 | } |
4358 | } | |
252e386e | 4359 | (*m)->count = new_count; |
252b5132 | 4360 | |
3dea8fca | 4361 | if (remove_empty_load && (*m)->p_type == PT_LOAD && (*m)->count == 0) |
252e386e AM |
4362 | *m = (*m)->next; |
4363 | else | |
4364 | m = &(*m)->next; | |
8ded5a0f | 4365 | } |
252b5132 | 4366 | |
8ded5a0f AM |
4367 | bed = get_elf_backend_data (abfd); |
4368 | if (bed->elf_backend_modify_segment_map != NULL) | |
252b5132 | 4369 | { |
252e386e | 4370 | if (!(*bed->elf_backend_modify_segment_map) (abfd, info)) |
8ded5a0f | 4371 | return FALSE; |
252b5132 | 4372 | } |
252b5132 | 4373 | |
8ded5a0f AM |
4374 | return TRUE; |
4375 | } | |
252b5132 | 4376 | |
8ded5a0f | 4377 | /* Set up a mapping from BFD sections to program segments. */ |
252b5132 | 4378 | |
8ded5a0f AM |
4379 | bfd_boolean |
4380 | _bfd_elf_map_sections_to_segments (bfd *abfd, struct bfd_link_info *info) | |
4381 | { | |
4382 | unsigned int count; | |
4383 | struct elf_segment_map *m; | |
4384 | asection **sections = NULL; | |
4385 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
3dea8fca | 4386 | bfd_boolean no_user_phdrs; |
252b5132 | 4387 | |
12bd6957 | 4388 | no_user_phdrs = elf_seg_map (abfd) == NULL; |
d324f6d6 RM |
4389 | |
4390 | if (info != NULL) | |
4391 | info->user_phdrs = !no_user_phdrs; | |
4392 | ||
3dea8fca | 4393 | if (no_user_phdrs && bfd_count_sections (abfd) != 0) |
252b5132 | 4394 | { |
8ded5a0f AM |
4395 | asection *s; |
4396 | unsigned int i; | |
4397 | struct elf_segment_map *mfirst; | |
4398 | struct elf_segment_map **pm; | |
4399 | asection *last_hdr; | |
4400 | bfd_vma last_size; | |
4401 | unsigned int phdr_index; | |
4402 | bfd_vma maxpagesize; | |
4403 | asection **hdrpp; | |
4404 | bfd_boolean phdr_in_segment = TRUE; | |
4405 | bfd_boolean writable; | |
4406 | int tls_count = 0; | |
4407 | asection *first_tls = NULL; | |
4408 | asection *dynsec, *eh_frame_hdr; | |
4409 | bfd_size_type amt; | |
8d06853e | 4410 | bfd_vma addr_mask, wrap_to = 0; |
252b5132 | 4411 | |
8ded5a0f | 4412 | /* Select the allocated sections, and sort them. */ |
252b5132 | 4413 | |
a50b1753 NC |
4414 | sections = (asection **) bfd_malloc2 (bfd_count_sections (abfd), |
4415 | sizeof (asection *)); | |
8ded5a0f | 4416 | if (sections == NULL) |
252b5132 | 4417 | goto error_return; |
252b5132 | 4418 | |
8d06853e AM |
4419 | /* Calculate top address, avoiding undefined behaviour of shift |
4420 | left operator when shift count is equal to size of type | |
4421 | being shifted. */ | |
4422 | addr_mask = ((bfd_vma) 1 << (bfd_arch_bits_per_address (abfd) - 1)) - 1; | |
4423 | addr_mask = (addr_mask << 1) + 1; | |
4424 | ||
8ded5a0f AM |
4425 | i = 0; |
4426 | for (s = abfd->sections; s != NULL; s = s->next) | |
4427 | { | |
4428 | if ((s->flags & SEC_ALLOC) != 0) | |
4429 | { | |
4430 | sections[i] = s; | |
4431 | ++i; | |
8d06853e AM |
4432 | /* A wrapping section potentially clashes with header. */ |
4433 | if (((s->lma + s->size) & addr_mask) < (s->lma & addr_mask)) | |
4434 | wrap_to = (s->lma + s->size) & addr_mask; | |
8ded5a0f AM |
4435 | } |
4436 | } | |
4437 | BFD_ASSERT (i <= bfd_count_sections (abfd)); | |
4438 | count = i; | |
252b5132 | 4439 | |
8ded5a0f | 4440 | qsort (sections, (size_t) count, sizeof (asection *), elf_sort_sections); |
252b5132 | 4441 | |
8ded5a0f | 4442 | /* Build the mapping. */ |
252b5132 | 4443 | |
8ded5a0f AM |
4444 | mfirst = NULL; |
4445 | pm = &mfirst; | |
252b5132 | 4446 | |
8ded5a0f AM |
4447 | /* If we have a .interp section, then create a PT_PHDR segment for |
4448 | the program headers and a PT_INTERP segment for the .interp | |
4449 | section. */ | |
4450 | s = bfd_get_section_by_name (abfd, ".interp"); | |
4451 | if (s != NULL && (s->flags & SEC_LOAD) != 0) | |
4452 | { | |
4453 | amt = sizeof (struct elf_segment_map); | |
a50b1753 | 4454 | m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); |
8ded5a0f AM |
4455 | if (m == NULL) |
4456 | goto error_return; | |
4457 | m->next = NULL; | |
4458 | m->p_type = PT_PHDR; | |
4459 | /* FIXME: UnixWare and Solaris set PF_X, Irix 5 does not. */ | |
4460 | m->p_flags = PF_R | PF_X; | |
4461 | m->p_flags_valid = 1; | |
4462 | m->includes_phdrs = 1; | |
252b5132 | 4463 | |
8ded5a0f AM |
4464 | *pm = m; |
4465 | pm = &m->next; | |
252b5132 | 4466 | |
8ded5a0f | 4467 | amt = sizeof (struct elf_segment_map); |
a50b1753 | 4468 | m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); |
8ded5a0f AM |
4469 | if (m == NULL) |
4470 | goto error_return; | |
4471 | m->next = NULL; | |
4472 | m->p_type = PT_INTERP; | |
4473 | m->count = 1; | |
4474 | m->sections[0] = s; | |
4475 | ||
4476 | *pm = m; | |
4477 | pm = &m->next; | |
252b5132 | 4478 | } |
8ded5a0f AM |
4479 | |
4480 | /* Look through the sections. We put sections in the same program | |
4481 | segment when the start of the second section can be placed within | |
4482 | a few bytes of the end of the first section. */ | |
4483 | last_hdr = NULL; | |
4484 | last_size = 0; | |
4485 | phdr_index = 0; | |
4486 | maxpagesize = bed->maxpagesize; | |
063bb025 NC |
4487 | /* PR 17512: file: c8455299. |
4488 | Avoid divide-by-zero errors later on. | |
4489 | FIXME: Should we abort if the maxpagesize is zero ? */ | |
4490 | if (maxpagesize == 0) | |
4491 | maxpagesize = 1; | |
8ded5a0f AM |
4492 | writable = FALSE; |
4493 | dynsec = bfd_get_section_by_name (abfd, ".dynamic"); | |
4494 | if (dynsec != NULL | |
4495 | && (dynsec->flags & SEC_LOAD) == 0) | |
4496 | dynsec = NULL; | |
4497 | ||
4498 | /* Deal with -Ttext or something similar such that the first section | |
4499 | is not adjacent to the program headers. This is an | |
4500 | approximation, since at this point we don't know exactly how many | |
4501 | program headers we will need. */ | |
4502 | if (count > 0) | |
252b5132 | 4503 | { |
12bd6957 | 4504 | bfd_size_type phdr_size = elf_program_header_size (abfd); |
8ded5a0f | 4505 | |
62d7a5f6 | 4506 | if (phdr_size == (bfd_size_type) -1) |
8ded5a0f | 4507 | phdr_size = get_program_header_size (abfd, info); |
d2bcb0d1 | 4508 | phdr_size += bed->s->sizeof_ehdr; |
8ded5a0f | 4509 | if ((abfd->flags & D_PAGED) == 0 |
8d06853e AM |
4510 | || (sections[0]->lma & addr_mask) < phdr_size |
4511 | || ((sections[0]->lma & addr_mask) % maxpagesize | |
4512 | < phdr_size % maxpagesize) | |
4513 | || (sections[0]->lma & addr_mask & -maxpagesize) < wrap_to) | |
8ded5a0f | 4514 | phdr_in_segment = FALSE; |
252b5132 RH |
4515 | } |
4516 | ||
8ded5a0f | 4517 | for (i = 0, hdrpp = sections; i < count; i++, hdrpp++) |
252b5132 | 4518 | { |
8ded5a0f AM |
4519 | asection *hdr; |
4520 | bfd_boolean new_segment; | |
4521 | ||
4522 | hdr = *hdrpp; | |
4523 | ||
4524 | /* See if this section and the last one will fit in the same | |
4525 | segment. */ | |
4526 | ||
4527 | if (last_hdr == NULL) | |
4528 | { | |
4529 | /* If we don't have a segment yet, then we don't need a new | |
4530 | one (we build the last one after this loop). */ | |
4531 | new_segment = FALSE; | |
4532 | } | |
4533 | else if (last_hdr->lma - last_hdr->vma != hdr->lma - hdr->vma) | |
4534 | { | |
4535 | /* If this section has a different relation between the | |
4536 | virtual address and the load address, then we need a new | |
4537 | segment. */ | |
4538 | new_segment = TRUE; | |
4539 | } | |
b5599592 AM |
4540 | else if (hdr->lma < last_hdr->lma + last_size |
4541 | || last_hdr->lma + last_size < last_hdr->lma) | |
4542 | { | |
4543 | /* If this section has a load address that makes it overlap | |
4544 | the previous section, then we need a new segment. */ | |
4545 | new_segment = TRUE; | |
4546 | } | |
39948a60 NC |
4547 | /* In the next test we have to be careful when last_hdr->lma is close |
4548 | to the end of the address space. If the aligned address wraps | |
4549 | around to the start of the address space, then there are no more | |
4550 | pages left in memory and it is OK to assume that the current | |
4551 | section can be included in the current segment. */ | |
4552 | else if ((BFD_ALIGN (last_hdr->lma + last_size, maxpagesize) + maxpagesize | |
4553 | > last_hdr->lma) | |
4554 | && (BFD_ALIGN (last_hdr->lma + last_size, maxpagesize) + maxpagesize | |
4ff73856 | 4555 | <= hdr->lma)) |
8ded5a0f AM |
4556 | { |
4557 | /* If putting this section in this segment would force us to | |
4558 | skip a page in the segment, then we need a new segment. */ | |
4559 | new_segment = TRUE; | |
4560 | } | |
4561 | else if ((last_hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0 | |
e5654c0f AM |
4562 | && (hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) != 0 |
4563 | && ((abfd->flags & D_PAGED) == 0 | |
4564 | || (((last_hdr->lma + last_size - 1) & -maxpagesize) | |
4565 | != (hdr->lma & -maxpagesize)))) | |
8ded5a0f | 4566 | { |
e5654c0f AM |
4567 | /* We don't want to put a loaded section after a |
4568 | nonloaded (ie. bss style) section in the same segment | |
4569 | as that will force the non-loaded section to be loaded. | |
4570 | Consider .tbss sections as loaded for this purpose. | |
4571 | However, like the writable/non-writable case below, | |
4572 | if they are on the same page then they must be put | |
4573 | in the same segment. */ | |
8ded5a0f AM |
4574 | new_segment = TRUE; |
4575 | } | |
4576 | else if ((abfd->flags & D_PAGED) == 0) | |
4577 | { | |
4578 | /* If the file is not demand paged, which means that we | |
4579 | don't require the sections to be correctly aligned in the | |
4580 | file, then there is no other reason for a new segment. */ | |
4581 | new_segment = FALSE; | |
4582 | } | |
4583 | else if (! writable | |
4584 | && (hdr->flags & SEC_READONLY) == 0 | |
8d06853e AM |
4585 | && (((last_hdr->lma + last_size - 1) & -maxpagesize) |
4586 | != (hdr->lma & -maxpagesize))) | |
8ded5a0f AM |
4587 | { |
4588 | /* We don't want to put a writable section in a read only | |
4589 | segment, unless they are on the same page in memory | |
4590 | anyhow. We already know that the last section does not | |
4591 | bring us past the current section on the page, so the | |
4592 | only case in which the new section is not on the same | |
4593 | page as the previous section is when the previous section | |
4594 | ends precisely on a page boundary. */ | |
4595 | new_segment = TRUE; | |
4596 | } | |
4597 | else | |
4598 | { | |
4599 | /* Otherwise, we can use the same segment. */ | |
4600 | new_segment = FALSE; | |
4601 | } | |
4602 | ||
2889e75b | 4603 | /* Allow interested parties a chance to override our decision. */ |
ceae84aa AM |
4604 | if (last_hdr != NULL |
4605 | && info != NULL | |
4606 | && info->callbacks->override_segment_assignment != NULL) | |
4607 | new_segment | |
4608 | = info->callbacks->override_segment_assignment (info, abfd, hdr, | |
4609 | last_hdr, | |
4610 | new_segment); | |
2889e75b | 4611 | |
8ded5a0f AM |
4612 | if (! new_segment) |
4613 | { | |
4614 | if ((hdr->flags & SEC_READONLY) == 0) | |
4615 | writable = TRUE; | |
4616 | last_hdr = hdr; | |
4617 | /* .tbss sections effectively have zero size. */ | |
4618 | if ((hdr->flags & (SEC_THREAD_LOCAL | SEC_LOAD)) | |
4619 | != SEC_THREAD_LOCAL) | |
4620 | last_size = hdr->size; | |
4621 | else | |
4622 | last_size = 0; | |
4623 | continue; | |
4624 | } | |
4625 | ||
4626 | /* We need a new program segment. We must create a new program | |
4627 | header holding all the sections from phdr_index until hdr. */ | |
4628 | ||
4629 | m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment); | |
4630 | if (m == NULL) | |
4631 | goto error_return; | |
4632 | ||
4633 | *pm = m; | |
4634 | pm = &m->next; | |
4635 | ||
252b5132 | 4636 | if ((hdr->flags & SEC_READONLY) == 0) |
b34976b6 | 4637 | writable = TRUE; |
8ded5a0f AM |
4638 | else |
4639 | writable = FALSE; | |
4640 | ||
baaff79e JJ |
4641 | last_hdr = hdr; |
4642 | /* .tbss sections effectively have zero size. */ | |
e5caec89 | 4643 | if ((hdr->flags & (SEC_THREAD_LOCAL | SEC_LOAD)) != SEC_THREAD_LOCAL) |
eea6121a | 4644 | last_size = hdr->size; |
baaff79e JJ |
4645 | else |
4646 | last_size = 0; | |
8ded5a0f AM |
4647 | phdr_index = i; |
4648 | phdr_in_segment = FALSE; | |
252b5132 RH |
4649 | } |
4650 | ||
86b2281f AM |
4651 | /* Create a final PT_LOAD program segment, but not if it's just |
4652 | for .tbss. */ | |
4653 | if (last_hdr != NULL | |
4654 | && (i - phdr_index != 1 | |
4655 | || ((last_hdr->flags & (SEC_THREAD_LOCAL | SEC_LOAD)) | |
4656 | != SEC_THREAD_LOCAL))) | |
8ded5a0f AM |
4657 | { |
4658 | m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment); | |
4659 | if (m == NULL) | |
4660 | goto error_return; | |
252b5132 | 4661 | |
8ded5a0f AM |
4662 | *pm = m; |
4663 | pm = &m->next; | |
4664 | } | |
252b5132 | 4665 | |
8ded5a0f AM |
4666 | /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */ |
4667 | if (dynsec != NULL) | |
4668 | { | |
4669 | m = _bfd_elf_make_dynamic_segment (abfd, dynsec); | |
4670 | if (m == NULL) | |
4671 | goto error_return; | |
4672 | *pm = m; | |
4673 | pm = &m->next; | |
4674 | } | |
252b5132 | 4675 | |
1c5265b5 JJ |
4676 | /* For each batch of consecutive loadable .note sections, |
4677 | add a PT_NOTE segment. We don't use bfd_get_section_by_name, | |
4678 | because if we link together nonloadable .note sections and | |
4679 | loadable .note sections, we will generate two .note sections | |
4680 | in the output file. FIXME: Using names for section types is | |
4681 | bogus anyhow. */ | |
8ded5a0f AM |
4682 | for (s = abfd->sections; s != NULL; s = s->next) |
4683 | { | |
4684 | if ((s->flags & SEC_LOAD) != 0 | |
0112cd26 | 4685 | && CONST_STRNEQ (s->name, ".note")) |
8ded5a0f | 4686 | { |
1c5265b5 | 4687 | asection *s2; |
91d6fa6a NC |
4688 | |
4689 | count = 1; | |
8ded5a0f | 4690 | amt = sizeof (struct elf_segment_map); |
1c5265b5 JJ |
4691 | if (s->alignment_power == 2) |
4692 | for (s2 = s; s2->next != NULL; s2 = s2->next) | |
55b581a6 JJ |
4693 | { |
4694 | if (s2->next->alignment_power == 2 | |
4695 | && (s2->next->flags & SEC_LOAD) != 0 | |
4696 | && CONST_STRNEQ (s2->next->name, ".note") | |
8d06853e AM |
4697 | && align_power (s2->lma + s2->size, 2) |
4698 | == s2->next->lma) | |
55b581a6 JJ |
4699 | count++; |
4700 | else | |
4701 | break; | |
4702 | } | |
1c5265b5 | 4703 | amt += (count - 1) * sizeof (asection *); |
a50b1753 | 4704 | m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); |
8ded5a0f AM |
4705 | if (m == NULL) |
4706 | goto error_return; | |
4707 | m->next = NULL; | |
4708 | m->p_type = PT_NOTE; | |
1c5265b5 JJ |
4709 | m->count = count; |
4710 | while (count > 1) | |
4711 | { | |
4712 | m->sections[m->count - count--] = s; | |
4713 | BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0); | |
4714 | s = s->next; | |
4715 | } | |
4716 | m->sections[m->count - 1] = s; | |
4717 | BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0); | |
8ded5a0f AM |
4718 | *pm = m; |
4719 | pm = &m->next; | |
4720 | } | |
4721 | if (s->flags & SEC_THREAD_LOCAL) | |
4722 | { | |
4723 | if (! tls_count) | |
4724 | first_tls = s; | |
4725 | tls_count++; | |
4726 | } | |
4727 | } | |
252b5132 | 4728 | |
8ded5a0f AM |
4729 | /* If there are any SHF_TLS output sections, add PT_TLS segment. */ |
4730 | if (tls_count > 0) | |
4731 | { | |
8ded5a0f AM |
4732 | amt = sizeof (struct elf_segment_map); |
4733 | amt += (tls_count - 1) * sizeof (asection *); | |
a50b1753 | 4734 | m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); |
8ded5a0f AM |
4735 | if (m == NULL) |
4736 | goto error_return; | |
4737 | m->next = NULL; | |
4738 | m->p_type = PT_TLS; | |
4739 | m->count = tls_count; | |
4740 | /* Mandated PF_R. */ | |
4741 | m->p_flags = PF_R; | |
4742 | m->p_flags_valid = 1; | |
d923cae0 | 4743 | s = first_tls; |
91d6fa6a | 4744 | for (i = 0; i < (unsigned int) tls_count; ++i) |
8ded5a0f | 4745 | { |
d923cae0 L |
4746 | if ((s->flags & SEC_THREAD_LOCAL) == 0) |
4747 | { | |
4748 | _bfd_error_handler | |
4749 | (_("%B: TLS sections are not adjacent:"), abfd); | |
4750 | s = first_tls; | |
4751 | i = 0; | |
4752 | while (i < (unsigned int) tls_count) | |
4753 | { | |
4754 | if ((s->flags & SEC_THREAD_LOCAL) != 0) | |
4755 | { | |
4756 | _bfd_error_handler (_(" TLS: %A"), s); | |
4757 | i++; | |
4758 | } | |
4759 | else | |
4760 | _bfd_error_handler (_(" non-TLS: %A"), s); | |
4761 | s = s->next; | |
4762 | } | |
4763 | bfd_set_error (bfd_error_bad_value); | |
4764 | goto error_return; | |
4765 | } | |
4766 | m->sections[i] = s; | |
4767 | s = s->next; | |
8ded5a0f | 4768 | } |
252b5132 | 4769 | |
8ded5a0f AM |
4770 | *pm = m; |
4771 | pm = &m->next; | |
4772 | } | |
252b5132 | 4773 | |
8ded5a0f AM |
4774 | /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME |
4775 | segment. */ | |
12bd6957 | 4776 | eh_frame_hdr = elf_eh_frame_hdr (abfd); |
8ded5a0f AM |
4777 | if (eh_frame_hdr != NULL |
4778 | && (eh_frame_hdr->output_section->flags & SEC_LOAD) != 0) | |
252b5132 | 4779 | { |
dc810e39 | 4780 | amt = sizeof (struct elf_segment_map); |
a50b1753 | 4781 | m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); |
252b5132 RH |
4782 | if (m == NULL) |
4783 | goto error_return; | |
4784 | m->next = NULL; | |
8ded5a0f | 4785 | m->p_type = PT_GNU_EH_FRAME; |
252b5132 | 4786 | m->count = 1; |
8ded5a0f | 4787 | m->sections[0] = eh_frame_hdr->output_section; |
252b5132 RH |
4788 | |
4789 | *pm = m; | |
4790 | pm = &m->next; | |
4791 | } | |
13ae64f3 | 4792 | |
12bd6957 | 4793 | if (elf_stack_flags (abfd)) |
13ae64f3 | 4794 | { |
8ded5a0f | 4795 | amt = sizeof (struct elf_segment_map); |
a50b1753 | 4796 | m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); |
8ded5a0f AM |
4797 | if (m == NULL) |
4798 | goto error_return; | |
4799 | m->next = NULL; | |
2b05f1b7 | 4800 | m->p_type = PT_GNU_STACK; |
12bd6957 | 4801 | m->p_flags = elf_stack_flags (abfd); |
04c3a755 | 4802 | m->p_align = bed->stack_align; |
8ded5a0f | 4803 | m->p_flags_valid = 1; |
04c3a755 NS |
4804 | m->p_align_valid = m->p_align != 0; |
4805 | if (info->stacksize > 0) | |
4806 | { | |
4807 | m->p_size = info->stacksize; | |
4808 | m->p_size_valid = 1; | |
4809 | } | |
252b5132 | 4810 | |
8ded5a0f AM |
4811 | *pm = m; |
4812 | pm = &m->next; | |
4813 | } | |
65765700 | 4814 | |
ceae84aa | 4815 | if (info != NULL && info->relro) |
8ded5a0f | 4816 | { |
f210dcff L |
4817 | for (m = mfirst; m != NULL; m = m->next) |
4818 | { | |
3832a4d8 AM |
4819 | if (m->p_type == PT_LOAD |
4820 | && m->count != 0 | |
4821 | && m->sections[0]->vma >= info->relro_start | |
4822 | && m->sections[0]->vma < info->relro_end) | |
f210dcff | 4823 | { |
3832a4d8 AM |
4824 | i = m->count; |
4825 | while (--i != (unsigned) -1) | |
4826 | if ((m->sections[i]->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) | |
4827 | == (SEC_LOAD | SEC_HAS_CONTENTS)) | |
4828 | break; | |
4829 | ||
43a8475c | 4830 | if (i != (unsigned) -1) |
f210dcff L |
4831 | break; |
4832 | } | |
be01b344 | 4833 | } |
f210dcff L |
4834 | |
4835 | /* Make a PT_GNU_RELRO segment only when it isn't empty. */ | |
4836 | if (m != NULL) | |
4837 | { | |
4838 | amt = sizeof (struct elf_segment_map); | |
a50b1753 | 4839 | m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); |
f210dcff L |
4840 | if (m == NULL) |
4841 | goto error_return; | |
4842 | m->next = NULL; | |
4843 | m->p_type = PT_GNU_RELRO; | |
f210dcff L |
4844 | *pm = m; |
4845 | pm = &m->next; | |
4846 | } | |
8ded5a0f | 4847 | } |
9ee5e499 | 4848 | |
8ded5a0f | 4849 | free (sections); |
12bd6957 | 4850 | elf_seg_map (abfd) = mfirst; |
9ee5e499 JJ |
4851 | } |
4852 | ||
3dea8fca | 4853 | if (!elf_modify_segment_map (abfd, info, no_user_phdrs)) |
8ded5a0f | 4854 | return FALSE; |
8c37241b | 4855 | |
12bd6957 | 4856 | for (count = 0, m = elf_seg_map (abfd); m != NULL; m = m->next) |
8ded5a0f | 4857 | ++count; |
12bd6957 | 4858 | elf_program_header_size (abfd) = count * bed->s->sizeof_phdr; |
252b5132 | 4859 | |
b34976b6 | 4860 | return TRUE; |
252b5132 RH |
4861 | |
4862 | error_return: | |
4863 | if (sections != NULL) | |
4864 | free (sections); | |
b34976b6 | 4865 | return FALSE; |
252b5132 RH |
4866 | } |
4867 | ||
4868 | /* Sort sections by address. */ | |
4869 | ||
4870 | static int | |
217aa764 | 4871 | elf_sort_sections (const void *arg1, const void *arg2) |
252b5132 RH |
4872 | { |
4873 | const asection *sec1 = *(const asection **) arg1; | |
4874 | const asection *sec2 = *(const asection **) arg2; | |
eecdbe52 | 4875 | bfd_size_type size1, size2; |
252b5132 RH |
4876 | |
4877 | /* Sort by LMA first, since this is the address used to | |
4878 | place the section into a segment. */ | |
4879 | if (sec1->lma < sec2->lma) | |
4880 | return -1; | |
4881 | else if (sec1->lma > sec2->lma) | |
4882 | return 1; | |
4883 | ||
4884 | /* Then sort by VMA. Normally the LMA and the VMA will be | |
4885 | the same, and this will do nothing. */ | |
4886 | if (sec1->vma < sec2->vma) | |
4887 | return -1; | |
4888 | else if (sec1->vma > sec2->vma) | |
4889 | return 1; | |
4890 | ||
4891 | /* Put !SEC_LOAD sections after SEC_LOAD ones. */ | |
4892 | ||
07c6e936 | 4893 | #define TOEND(x) (((x)->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0) |
252b5132 RH |
4894 | |
4895 | if (TOEND (sec1)) | |
4896 | { | |
4897 | if (TOEND (sec2)) | |
00a7cdc5 NC |
4898 | { |
4899 | /* If the indicies are the same, do not return 0 | |
4900 | here, but continue to try the next comparison. */ | |
4901 | if (sec1->target_index - sec2->target_index != 0) | |
4902 | return sec1->target_index - sec2->target_index; | |
4903 | } | |
252b5132 RH |
4904 | else |
4905 | return 1; | |
4906 | } | |
00a7cdc5 | 4907 | else if (TOEND (sec2)) |
252b5132 RH |
4908 | return -1; |
4909 | ||
4910 | #undef TOEND | |
4911 | ||
00a7cdc5 NC |
4912 | /* Sort by size, to put zero sized sections |
4913 | before others at the same address. */ | |
252b5132 | 4914 | |
eea6121a AM |
4915 | size1 = (sec1->flags & SEC_LOAD) ? sec1->size : 0; |
4916 | size2 = (sec2->flags & SEC_LOAD) ? sec2->size : 0; | |
eecdbe52 JJ |
4917 | |
4918 | if (size1 < size2) | |
252b5132 | 4919 | return -1; |
eecdbe52 | 4920 | if (size1 > size2) |
252b5132 RH |
4921 | return 1; |
4922 | ||
4923 | return sec1->target_index - sec2->target_index; | |
4924 | } | |
4925 | ||
340b6d91 AC |
4926 | /* Ian Lance Taylor writes: |
4927 | ||
4928 | We shouldn't be using % with a negative signed number. That's just | |
4929 | not good. We have to make sure either that the number is not | |
4930 | negative, or that the number has an unsigned type. When the types | |
4931 | are all the same size they wind up as unsigned. When file_ptr is a | |
4932 | larger signed type, the arithmetic winds up as signed long long, | |
4933 | which is wrong. | |
4934 | ||
4935 | What we're trying to say here is something like ``increase OFF by | |
4936 | the least amount that will cause it to be equal to the VMA modulo | |
4937 | the page size.'' */ | |
4938 | /* In other words, something like: | |
4939 | ||
4940 | vma_offset = m->sections[0]->vma % bed->maxpagesize; | |
4941 | off_offset = off % bed->maxpagesize; | |
4942 | if (vma_offset < off_offset) | |
4943 | adjustment = vma_offset + bed->maxpagesize - off_offset; | |
4944 | else | |
4945 | adjustment = vma_offset - off_offset; | |
08a40648 | 4946 | |
340b6d91 AC |
4947 | which can can be collapsed into the expression below. */ |
4948 | ||
4949 | static file_ptr | |
4950 | vma_page_aligned_bias (bfd_vma vma, ufile_ptr off, bfd_vma maxpagesize) | |
4951 | { | |
dc9155b2 NC |
4952 | /* PR binutils/16199: Handle an alignment of zero. */ |
4953 | if (maxpagesize == 0) | |
4954 | maxpagesize = 1; | |
340b6d91 AC |
4955 | return ((vma - off) % maxpagesize); |
4956 | } | |
4957 | ||
6d33f217 L |
4958 | static void |
4959 | print_segment_map (const struct elf_segment_map *m) | |
4960 | { | |
4961 | unsigned int j; | |
4962 | const char *pt = get_segment_type (m->p_type); | |
4963 | char buf[32]; | |
4964 | ||
4965 | if (pt == NULL) | |
4966 | { | |
4967 | if (m->p_type >= PT_LOPROC && m->p_type <= PT_HIPROC) | |
4968 | sprintf (buf, "LOPROC+%7.7x", | |
4969 | (unsigned int) (m->p_type - PT_LOPROC)); | |
4970 | else if (m->p_type >= PT_LOOS && m->p_type <= PT_HIOS) | |
4971 | sprintf (buf, "LOOS+%7.7x", | |
4972 | (unsigned int) (m->p_type - PT_LOOS)); | |
4973 | else | |
4974 | snprintf (buf, sizeof (buf), "%8.8x", | |
4975 | (unsigned int) m->p_type); | |
4976 | pt = buf; | |
4977 | } | |
4a97a0e5 | 4978 | fflush (stdout); |
6d33f217 L |
4979 | fprintf (stderr, "%s:", pt); |
4980 | for (j = 0; j < m->count; j++) | |
4981 | fprintf (stderr, " %s", m->sections [j]->name); | |
4982 | putc ('\n',stderr); | |
4a97a0e5 | 4983 | fflush (stderr); |
6d33f217 L |
4984 | } |
4985 | ||
32812159 AM |
4986 | static bfd_boolean |
4987 | write_zeros (bfd *abfd, file_ptr pos, bfd_size_type len) | |
4988 | { | |
4989 | void *buf; | |
4990 | bfd_boolean ret; | |
4991 | ||
4992 | if (bfd_seek (abfd, pos, SEEK_SET) != 0) | |
4993 | return FALSE; | |
4994 | buf = bfd_zmalloc (len); | |
4995 | if (buf == NULL) | |
4996 | return FALSE; | |
4997 | ret = bfd_bwrite (buf, len, abfd) == len; | |
4998 | free (buf); | |
4999 | return ret; | |
5000 | } | |
5001 | ||
252b5132 RH |
5002 | /* Assign file positions to the sections based on the mapping from |
5003 | sections to segments. This function also sets up some fields in | |
f3520d2f | 5004 | the file header. */ |
252b5132 | 5005 | |
b34976b6 | 5006 | static bfd_boolean |
f3520d2f AM |
5007 | assign_file_positions_for_load_sections (bfd *abfd, |
5008 | struct bfd_link_info *link_info) | |
252b5132 RH |
5009 | { |
5010 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
252b5132 | 5011 | struct elf_segment_map *m; |
252b5132 | 5012 | Elf_Internal_Phdr *phdrs; |
252b5132 | 5013 | Elf_Internal_Phdr *p; |
02bf8d82 | 5014 | file_ptr off; |
3f570048 | 5015 | bfd_size_type maxpagesize; |
f3520d2f | 5016 | unsigned int alloc; |
0920dee7 | 5017 | unsigned int i, j; |
2b0bc088 | 5018 | bfd_vma header_pad = 0; |
252b5132 | 5019 | |
e36284ab | 5020 | if (link_info == NULL |
ceae84aa | 5021 | && !_bfd_elf_map_sections_to_segments (abfd, link_info)) |
8ded5a0f | 5022 | return FALSE; |
252b5132 | 5023 | |
8ded5a0f | 5024 | alloc = 0; |
12bd6957 | 5025 | for (m = elf_seg_map (abfd); m != NULL; m = m->next) |
2b0bc088 NC |
5026 | { |
5027 | ++alloc; | |
5028 | if (m->header_size) | |
5029 | header_pad = m->header_size; | |
5030 | } | |
252b5132 | 5031 | |
82f2dbf7 NC |
5032 | if (alloc) |
5033 | { | |
5034 | elf_elfheader (abfd)->e_phoff = bed->s->sizeof_ehdr; | |
5035 | elf_elfheader (abfd)->e_phentsize = bed->s->sizeof_phdr; | |
5036 | } | |
5037 | else | |
5038 | { | |
5039 | /* PR binutils/12467. */ | |
5040 | elf_elfheader (abfd)->e_phoff = 0; | |
5041 | elf_elfheader (abfd)->e_phentsize = 0; | |
5042 | } | |
d324f6d6 | 5043 | |
8ded5a0f | 5044 | elf_elfheader (abfd)->e_phnum = alloc; |
252b5132 | 5045 | |
12bd6957 AM |
5046 | if (elf_program_header_size (abfd) == (bfd_size_type) -1) |
5047 | elf_program_header_size (abfd) = alloc * bed->s->sizeof_phdr; | |
8ded5a0f | 5048 | else |
12bd6957 | 5049 | BFD_ASSERT (elf_program_header_size (abfd) |
59e0647f | 5050 | >= alloc * bed->s->sizeof_phdr); |
252b5132 RH |
5051 | |
5052 | if (alloc == 0) | |
f3520d2f | 5053 | { |
12bd6957 | 5054 | elf_next_file_pos (abfd) = bed->s->sizeof_ehdr; |
8ded5a0f | 5055 | return TRUE; |
f3520d2f | 5056 | } |
252b5132 | 5057 | |
12bd6957 | 5058 | /* We're writing the size in elf_program_header_size (abfd), |
57268894 HPN |
5059 | see assign_file_positions_except_relocs, so make sure we have |
5060 | that amount allocated, with trailing space cleared. | |
12bd6957 AM |
5061 | The variable alloc contains the computed need, while |
5062 | elf_program_header_size (abfd) contains the size used for the | |
57268894 HPN |
5063 | layout. |
5064 | See ld/emultempl/elf-generic.em:gld${EMULATION_NAME}_map_segments | |
5065 | where the layout is forced to according to a larger size in the | |
5066 | last iterations for the testcase ld-elf/header. */ | |
12bd6957 | 5067 | BFD_ASSERT (elf_program_header_size (abfd) % bed->s->sizeof_phdr |
57268894 | 5068 | == 0); |
a50b1753 NC |
5069 | phdrs = (Elf_Internal_Phdr *) |
5070 | bfd_zalloc2 (abfd, | |
12bd6957 | 5071 | (elf_program_header_size (abfd) / bed->s->sizeof_phdr), |
a50b1753 | 5072 | sizeof (Elf_Internal_Phdr)); |
f3520d2f | 5073 | elf_tdata (abfd)->phdr = phdrs; |
252b5132 | 5074 | if (phdrs == NULL) |
b34976b6 | 5075 | return FALSE; |
252b5132 | 5076 | |
3f570048 AM |
5077 | maxpagesize = 1; |
5078 | if ((abfd->flags & D_PAGED) != 0) | |
5079 | maxpagesize = bed->maxpagesize; | |
5080 | ||
252b5132 RH |
5081 | off = bed->s->sizeof_ehdr; |
5082 | off += alloc * bed->s->sizeof_phdr; | |
2b0bc088 NC |
5083 | if (header_pad < (bfd_vma) off) |
5084 | header_pad = 0; | |
5085 | else | |
5086 | header_pad -= off; | |
5087 | off += header_pad; | |
252b5132 | 5088 | |
12bd6957 | 5089 | for (m = elf_seg_map (abfd), p = phdrs, j = 0; |
252b5132 | 5090 | m != NULL; |
0920dee7 | 5091 | m = m->next, p++, j++) |
252b5132 | 5092 | { |
252b5132 | 5093 | asection **secpp; |
bf988460 AM |
5094 | bfd_vma off_adjust; |
5095 | bfd_boolean no_contents; | |
252b5132 RH |
5096 | |
5097 | /* If elf_segment_map is not from map_sections_to_segments, the | |
08a40648 | 5098 | sections may not be correctly ordered. NOTE: sorting should |
52e9b619 MS |
5099 | not be done to the PT_NOTE section of a corefile, which may |
5100 | contain several pseudo-sections artificially created by bfd. | |
5101 | Sorting these pseudo-sections breaks things badly. */ | |
47d9a591 AM |
5102 | if (m->count > 1 |
5103 | && !(elf_elfheader (abfd)->e_type == ET_CORE | |
52e9b619 | 5104 | && m->p_type == PT_NOTE)) |
252b5132 RH |
5105 | qsort (m->sections, (size_t) m->count, sizeof (asection *), |
5106 | elf_sort_sections); | |
5107 | ||
b301b248 AM |
5108 | /* An ELF segment (described by Elf_Internal_Phdr) may contain a |
5109 | number of sections with contents contributing to both p_filesz | |
5110 | and p_memsz, followed by a number of sections with no contents | |
5111 | that just contribute to p_memsz. In this loop, OFF tracks next | |
02bf8d82 | 5112 | available file offset for PT_LOAD and PT_NOTE segments. */ |
252b5132 | 5113 | p->p_type = m->p_type; |
28a7f3e7 | 5114 | p->p_flags = m->p_flags; |
252b5132 | 5115 | |
3f570048 AM |
5116 | if (m->count == 0) |
5117 | p->p_vaddr = 0; | |
5118 | else | |
3271a814 | 5119 | p->p_vaddr = m->sections[0]->vma - m->p_vaddr_offset; |
3f570048 AM |
5120 | |
5121 | if (m->p_paddr_valid) | |
5122 | p->p_paddr = m->p_paddr; | |
5123 | else if (m->count == 0) | |
5124 | p->p_paddr = 0; | |
5125 | else | |
08a40648 | 5126 | p->p_paddr = m->sections[0]->lma - m->p_vaddr_offset; |
3f570048 AM |
5127 | |
5128 | if (p->p_type == PT_LOAD | |
5129 | && (abfd->flags & D_PAGED) != 0) | |
5130 | { | |
5131 | /* p_align in demand paged PT_LOAD segments effectively stores | |
5132 | the maximum page size. When copying an executable with | |
5133 | objcopy, we set m->p_align from the input file. Use this | |
5134 | value for maxpagesize rather than bed->maxpagesize, which | |
5135 | may be different. Note that we use maxpagesize for PT_TLS | |
5136 | segment alignment later in this function, so we are relying | |
5137 | on at least one PT_LOAD segment appearing before a PT_TLS | |
5138 | segment. */ | |
5139 | if (m->p_align_valid) | |
5140 | maxpagesize = m->p_align; | |
5141 | ||
5142 | p->p_align = maxpagesize; | |
5143 | } | |
3271a814 NS |
5144 | else if (m->p_align_valid) |
5145 | p->p_align = m->p_align; | |
e970b90a DJ |
5146 | else if (m->count == 0) |
5147 | p->p_align = 1 << bed->s->log_file_align; | |
3f570048 AM |
5148 | else |
5149 | p->p_align = 0; | |
5150 | ||
bf988460 AM |
5151 | no_contents = FALSE; |
5152 | off_adjust = 0; | |
252b5132 | 5153 | if (p->p_type == PT_LOAD |
b301b248 | 5154 | && m->count > 0) |
252b5132 | 5155 | { |
b301b248 | 5156 | bfd_size_type align; |
a49e53ed | 5157 | unsigned int align_power = 0; |
b301b248 | 5158 | |
3271a814 NS |
5159 | if (m->p_align_valid) |
5160 | align = p->p_align; | |
5161 | else | |
252b5132 | 5162 | { |
3271a814 NS |
5163 | for (i = 0, secpp = m->sections; i < m->count; i++, secpp++) |
5164 | { | |
5165 | unsigned int secalign; | |
08a40648 | 5166 | |
3271a814 NS |
5167 | secalign = bfd_get_section_alignment (abfd, *secpp); |
5168 | if (secalign > align_power) | |
5169 | align_power = secalign; | |
5170 | } | |
5171 | align = (bfd_size_type) 1 << align_power; | |
5172 | if (align < maxpagesize) | |
5173 | align = maxpagesize; | |
b301b248 | 5174 | } |
252b5132 | 5175 | |
02bf8d82 AM |
5176 | for (i = 0; i < m->count; i++) |
5177 | if ((m->sections[i]->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0) | |
5178 | /* If we aren't making room for this section, then | |
5179 | it must be SHT_NOBITS regardless of what we've | |
5180 | set via struct bfd_elf_special_section. */ | |
5181 | elf_section_type (m->sections[i]) = SHT_NOBITS; | |
5182 | ||
bf988460 | 5183 | /* Find out whether this segment contains any loadable |
aea274d3 AM |
5184 | sections. */ |
5185 | no_contents = TRUE; | |
5186 | for (i = 0; i < m->count; i++) | |
5187 | if (elf_section_type (m->sections[i]) != SHT_NOBITS) | |
5188 | { | |
5189 | no_contents = FALSE; | |
5190 | break; | |
5191 | } | |
bf988460 | 5192 | |
85cfcbfb | 5193 | off_adjust = vma_page_aligned_bias (p->p_vaddr, off, align); |
bf988460 AM |
5194 | off += off_adjust; |
5195 | if (no_contents) | |
5196 | { | |
5197 | /* We shouldn't need to align the segment on disk since | |
5198 | the segment doesn't need file space, but the gABI | |
5199 | arguably requires the alignment and glibc ld.so | |
5200 | checks it. So to comply with the alignment | |
5201 | requirement but not waste file space, we adjust | |
5202 | p_offset for just this segment. (OFF_ADJUST is | |
5203 | subtracted from OFF later.) This may put p_offset | |
5204 | past the end of file, but that shouldn't matter. */ | |
5205 | } | |
5206 | else | |
5207 | off_adjust = 0; | |
252b5132 | 5208 | } |
b1a6d0b1 NC |
5209 | /* Make sure the .dynamic section is the first section in the |
5210 | PT_DYNAMIC segment. */ | |
5211 | else if (p->p_type == PT_DYNAMIC | |
5212 | && m->count > 1 | |
5213 | && strcmp (m->sections[0]->name, ".dynamic") != 0) | |
5214 | { | |
5215 | _bfd_error_handler | |
b301b248 AM |
5216 | (_("%B: The first section in the PT_DYNAMIC segment is not the .dynamic section"), |
5217 | abfd); | |
b1a6d0b1 NC |
5218 | bfd_set_error (bfd_error_bad_value); |
5219 | return FALSE; | |
5220 | } | |
3f001e84 JK |
5221 | /* Set the note section type to SHT_NOTE. */ |
5222 | else if (p->p_type == PT_NOTE) | |
5223 | for (i = 0; i < m->count; i++) | |
5224 | elf_section_type (m->sections[i]) = SHT_NOTE; | |
252b5132 | 5225 | |
252b5132 RH |
5226 | p->p_offset = 0; |
5227 | p->p_filesz = 0; | |
5228 | p->p_memsz = 0; | |
5229 | ||
5230 | if (m->includes_filehdr) | |
5231 | { | |
bf988460 | 5232 | if (!m->p_flags_valid) |
252b5132 | 5233 | p->p_flags |= PF_R; |
252b5132 RH |
5234 | p->p_filesz = bed->s->sizeof_ehdr; |
5235 | p->p_memsz = bed->s->sizeof_ehdr; | |
5236 | if (m->count > 0) | |
5237 | { | |
252b5132 RH |
5238 | if (p->p_vaddr < (bfd_vma) off) |
5239 | { | |
caf47ea6 | 5240 | (*_bfd_error_handler) |
b301b248 AM |
5241 | (_("%B: Not enough room for program headers, try linking with -N"), |
5242 | abfd); | |
252b5132 | 5243 | bfd_set_error (bfd_error_bad_value); |
b34976b6 | 5244 | return FALSE; |
252b5132 RH |
5245 | } |
5246 | ||
5247 | p->p_vaddr -= off; | |
bf988460 | 5248 | if (!m->p_paddr_valid) |
252b5132 RH |
5249 | p->p_paddr -= off; |
5250 | } | |
252b5132 RH |
5251 | } |
5252 | ||
5253 | if (m->includes_phdrs) | |
5254 | { | |
bf988460 | 5255 | if (!m->p_flags_valid) |
252b5132 RH |
5256 | p->p_flags |= PF_R; |
5257 | ||
f3520d2f | 5258 | if (!m->includes_filehdr) |
252b5132 RH |
5259 | { |
5260 | p->p_offset = bed->s->sizeof_ehdr; | |
5261 | ||
5262 | if (m->count > 0) | |
5263 | { | |
252b5132 | 5264 | p->p_vaddr -= off - p->p_offset; |
bf988460 | 5265 | if (!m->p_paddr_valid) |
252b5132 RH |
5266 | p->p_paddr -= off - p->p_offset; |
5267 | } | |
252b5132 RH |
5268 | } |
5269 | ||
5270 | p->p_filesz += alloc * bed->s->sizeof_phdr; | |
5271 | p->p_memsz += alloc * bed->s->sizeof_phdr; | |
2b0bc088 NC |
5272 | if (m->count) |
5273 | { | |
5274 | p->p_filesz += header_pad; | |
5275 | p->p_memsz += header_pad; | |
5276 | } | |
252b5132 RH |
5277 | } |
5278 | ||
5279 | if (p->p_type == PT_LOAD | |
5280 | || (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core)) | |
5281 | { | |
bf988460 | 5282 | if (!m->includes_filehdr && !m->includes_phdrs) |
02bf8d82 | 5283 | p->p_offset = off; |
252b5132 RH |
5284 | else |
5285 | { | |
5286 | file_ptr adjust; | |
5287 | ||
5288 | adjust = off - (p->p_offset + p->p_filesz); | |
bf988460 AM |
5289 | if (!no_contents) |
5290 | p->p_filesz += adjust; | |
252b5132 RH |
5291 | p->p_memsz += adjust; |
5292 | } | |
5293 | } | |
5294 | ||
1ea63fd2 AM |
5295 | /* Set up p_filesz, p_memsz, p_align and p_flags from the section |
5296 | maps. Set filepos for sections in PT_LOAD segments, and in | |
5297 | core files, for sections in PT_NOTE segments. | |
5298 | assign_file_positions_for_non_load_sections will set filepos | |
5299 | for other sections and update p_filesz for other segments. */ | |
252b5132 RH |
5300 | for (i = 0, secpp = m->sections; i < m->count; i++, secpp++) |
5301 | { | |
5302 | asection *sec; | |
252b5132 | 5303 | bfd_size_type align; |
627b32bc | 5304 | Elf_Internal_Shdr *this_hdr; |
252b5132 RH |
5305 | |
5306 | sec = *secpp; | |
02bf8d82 | 5307 | this_hdr = &elf_section_data (sec)->this_hdr; |
3f570048 | 5308 | align = (bfd_size_type) 1 << bfd_get_section_alignment (abfd, sec); |
252b5132 | 5309 | |
88967714 AM |
5310 | if ((p->p_type == PT_LOAD |
5311 | || p->p_type == PT_TLS) | |
5312 | && (this_hdr->sh_type != SHT_NOBITS | |
5313 | || ((this_hdr->sh_flags & SHF_ALLOC) != 0 | |
5314 | && ((this_hdr->sh_flags & SHF_TLS) == 0 | |
5315 | || p->p_type == PT_TLS)))) | |
252b5132 | 5316 | { |
b5599592 AM |
5317 | bfd_vma p_start = p->p_paddr; |
5318 | bfd_vma p_end = p_start + p->p_memsz; | |
5319 | bfd_vma s_start = sec->lma; | |
5320 | bfd_vma adjust = s_start - p_end; | |
252b5132 | 5321 | |
a2d1e028 L |
5322 | if (adjust != 0 |
5323 | && (s_start < p_end | |
5324 | || p_end < p_start)) | |
252b5132 | 5325 | { |
88967714 | 5326 | (*_bfd_error_handler) |
b5599592 AM |
5327 | (_("%B: section %A lma %#lx adjusted to %#lx"), abfd, sec, |
5328 | (unsigned long) s_start, (unsigned long) p_end); | |
88967714 | 5329 | adjust = 0; |
b5599592 | 5330 | sec->lma = p_end; |
1cfb7d1e | 5331 | } |
3ac9b6c9 | 5332 | p->p_memsz += adjust; |
1cfb7d1e | 5333 | |
88967714 AM |
5334 | if (this_hdr->sh_type != SHT_NOBITS) |
5335 | { | |
32812159 AM |
5336 | if (p->p_filesz + adjust < p->p_memsz) |
5337 | { | |
5338 | /* We have a PROGBITS section following NOBITS ones. | |
5339 | Allocate file space for the NOBITS section(s) and | |
5340 | zero it. */ | |
5341 | adjust = p->p_memsz - p->p_filesz; | |
5342 | if (!write_zeros (abfd, off, adjust)) | |
5343 | return FALSE; | |
5344 | } | |
88967714 AM |
5345 | off += adjust; |
5346 | p->p_filesz += adjust; | |
252b5132 | 5347 | } |
252b5132 RH |
5348 | } |
5349 | ||
5350 | if (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core) | |
5351 | { | |
b301b248 AM |
5352 | /* The section at i == 0 is the one that actually contains |
5353 | everything. */ | |
4a938328 MS |
5354 | if (i == 0) |
5355 | { | |
627b32bc | 5356 | this_hdr->sh_offset = sec->filepos = off; |
6a3cd2b4 AM |
5357 | off += this_hdr->sh_size; |
5358 | p->p_filesz = this_hdr->sh_size; | |
b301b248 AM |
5359 | p->p_memsz = 0; |
5360 | p->p_align = 1; | |
252b5132 | 5361 | } |
4a938328 | 5362 | else |
252b5132 | 5363 | { |
b301b248 | 5364 | /* The rest are fake sections that shouldn't be written. */ |
252b5132 | 5365 | sec->filepos = 0; |
eea6121a | 5366 | sec->size = 0; |
b301b248 AM |
5367 | sec->flags = 0; |
5368 | continue; | |
252b5132 | 5369 | } |
252b5132 RH |
5370 | } |
5371 | else | |
5372 | { | |
1e951488 | 5373 | if (p->p_type == PT_LOAD) |
b301b248 | 5374 | { |
1e951488 AM |
5375 | this_hdr->sh_offset = sec->filepos = off; |
5376 | if (this_hdr->sh_type != SHT_NOBITS) | |
5377 | off += this_hdr->sh_size; | |
5378 | } | |
5379 | else if (this_hdr->sh_type == SHT_NOBITS | |
5380 | && (this_hdr->sh_flags & SHF_TLS) != 0 | |
5381 | && this_hdr->sh_offset == 0) | |
5382 | { | |
5383 | /* This is a .tbss section that didn't get a PT_LOAD. | |
5384 | (See _bfd_elf_map_sections_to_segments "Create a | |
5385 | final PT_LOAD".) Set sh_offset to the value it | |
5386 | would have if we had created a zero p_filesz and | |
5387 | p_memsz PT_LOAD header for the section. This | |
5388 | also makes the PT_TLS header have the same | |
5389 | p_offset value. */ | |
5390 | bfd_vma adjust = vma_page_aligned_bias (this_hdr->sh_addr, | |
5391 | off, align); | |
5392 | this_hdr->sh_offset = sec->filepos = off + adjust; | |
b301b248 | 5393 | } |
252b5132 | 5394 | |
02bf8d82 | 5395 | if (this_hdr->sh_type != SHT_NOBITS) |
b301b248 | 5396 | { |
6a3cd2b4 | 5397 | p->p_filesz += this_hdr->sh_size; |
02bf8d82 AM |
5398 | /* A load section without SHF_ALLOC is something like |
5399 | a note section in a PT_NOTE segment. These take | |
5400 | file space but are not loaded into memory. */ | |
5401 | if ((this_hdr->sh_flags & SHF_ALLOC) != 0) | |
6a3cd2b4 | 5402 | p->p_memsz += this_hdr->sh_size; |
b301b248 | 5403 | } |
6a3cd2b4 | 5404 | else if ((this_hdr->sh_flags & SHF_ALLOC) != 0) |
13ae64f3 | 5405 | { |
6a3cd2b4 AM |
5406 | if (p->p_type == PT_TLS) |
5407 | p->p_memsz += this_hdr->sh_size; | |
5408 | ||
5409 | /* .tbss is special. It doesn't contribute to p_memsz of | |
5410 | normal segments. */ | |
5411 | else if ((this_hdr->sh_flags & SHF_TLS) == 0) | |
5412 | p->p_memsz += this_hdr->sh_size; | |
13ae64f3 JJ |
5413 | } |
5414 | ||
b10a8ae0 L |
5415 | if (align > p->p_align |
5416 | && !m->p_align_valid | |
5417 | && (p->p_type != PT_LOAD | |
5418 | || (abfd->flags & D_PAGED) == 0)) | |
252b5132 RH |
5419 | p->p_align = align; |
5420 | } | |
5421 | ||
bf988460 | 5422 | if (!m->p_flags_valid) |
252b5132 RH |
5423 | { |
5424 | p->p_flags |= PF_R; | |
02bf8d82 | 5425 | if ((this_hdr->sh_flags & SHF_EXECINSTR) != 0) |
252b5132 | 5426 | p->p_flags |= PF_X; |
02bf8d82 | 5427 | if ((this_hdr->sh_flags & SHF_WRITE) != 0) |
252b5132 RH |
5428 | p->p_flags |= PF_W; |
5429 | } | |
5430 | } | |
43a8475c | 5431 | |
bf988460 | 5432 | off -= off_adjust; |
0920dee7 | 5433 | |
7c928300 AM |
5434 | /* Check that all sections are in a PT_LOAD segment. |
5435 | Don't check funky gdb generated core files. */ | |
5436 | if (p->p_type == PT_LOAD && bfd_get_format (abfd) != bfd_core) | |
9a83a553 AM |
5437 | { |
5438 | bfd_boolean check_vma = TRUE; | |
5439 | ||
5440 | for (i = 1; i < m->count; i++) | |
5441 | if (m->sections[i]->vma == m->sections[i - 1]->vma | |
5442 | && ELF_SECTION_SIZE (&(elf_section_data (m->sections[i]) | |
5443 | ->this_hdr), p) != 0 | |
5444 | && ELF_SECTION_SIZE (&(elf_section_data (m->sections[i - 1]) | |
5445 | ->this_hdr), p) != 0) | |
0920dee7 | 5446 | { |
9a83a553 AM |
5447 | /* Looks like we have overlays packed into the segment. */ |
5448 | check_vma = FALSE; | |
5449 | break; | |
0920dee7 | 5450 | } |
9a83a553 AM |
5451 | |
5452 | for (i = 0; i < m->count; i++) | |
5453 | { | |
5454 | Elf_Internal_Shdr *this_hdr; | |
5455 | asection *sec; | |
5456 | ||
5457 | sec = m->sections[i]; | |
5458 | this_hdr = &(elf_section_data(sec)->this_hdr); | |
86b2281f AM |
5459 | if (!ELF_SECTION_IN_SEGMENT_1 (this_hdr, p, check_vma, 0) |
5460 | && !ELF_TBSS_SPECIAL (this_hdr, p)) | |
9a83a553 AM |
5461 | { |
5462 | (*_bfd_error_handler) | |
5463 | (_("%B: section `%A' can't be allocated in segment %d"), | |
5464 | abfd, sec, j); | |
5465 | print_segment_map (m); | |
5466 | } | |
5467 | } | |
5468 | } | |
252b5132 RH |
5469 | } |
5470 | ||
12bd6957 | 5471 | elf_next_file_pos (abfd) = off; |
f3520d2f AM |
5472 | return TRUE; |
5473 | } | |
5474 | ||
5475 | /* Assign file positions for the other sections. */ | |
5476 | ||
5477 | static bfd_boolean | |
5478 | assign_file_positions_for_non_load_sections (bfd *abfd, | |
5479 | struct bfd_link_info *link_info) | |
5480 | { | |
5481 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
5482 | Elf_Internal_Shdr **i_shdrpp; | |
e06efbf1 | 5483 | Elf_Internal_Shdr **hdrpp, **end_hdrpp; |
f3520d2f AM |
5484 | Elf_Internal_Phdr *phdrs; |
5485 | Elf_Internal_Phdr *p; | |
5486 | struct elf_segment_map *m; | |
62655c7b | 5487 | struct elf_segment_map *hdrs_segment; |
f3520d2f AM |
5488 | bfd_vma filehdr_vaddr, filehdr_paddr; |
5489 | bfd_vma phdrs_vaddr, phdrs_paddr; | |
5490 | file_ptr off; | |
f3520d2f AM |
5491 | unsigned int count; |
5492 | ||
5c182d5f | 5493 | i_shdrpp = elf_elfsections (abfd); |
e06efbf1 | 5494 | end_hdrpp = i_shdrpp + elf_numsections (abfd); |
12bd6957 | 5495 | off = elf_next_file_pos (abfd); |
e06efbf1 | 5496 | for (hdrpp = i_shdrpp + 1; hdrpp < end_hdrpp; hdrpp++) |
5c182d5f | 5497 | { |
5c182d5f AM |
5498 | Elf_Internal_Shdr *hdr; |
5499 | ||
5500 | hdr = *hdrpp; | |
5501 | if (hdr->bfd_section != NULL | |
252e386e AM |
5502 | && (hdr->bfd_section->filepos != 0 |
5503 | || (hdr->sh_type == SHT_NOBITS | |
5504 | && hdr->contents == NULL))) | |
627b32bc | 5505 | BFD_ASSERT (hdr->sh_offset == hdr->bfd_section->filepos); |
5c182d5f AM |
5506 | else if ((hdr->sh_flags & SHF_ALLOC) != 0) |
5507 | { | |
e8d2ba53 AM |
5508 | if (hdr->sh_size != 0) |
5509 | (*_bfd_error_handler) | |
5510 | (_("%B: warning: allocated section `%s' not in segment"), | |
5511 | abfd, | |
5512 | (hdr->bfd_section == NULL | |
5513 | ? "*unknown*" | |
5514 | : hdr->bfd_section->name)); | |
3ba71138 L |
5515 | /* We don't need to page align empty sections. */ |
5516 | if ((abfd->flags & D_PAGED) != 0 && hdr->sh_size != 0) | |
5c182d5f AM |
5517 | off += vma_page_aligned_bias (hdr->sh_addr, off, |
5518 | bed->maxpagesize); | |
5519 | else | |
5520 | off += vma_page_aligned_bias (hdr->sh_addr, off, | |
5521 | hdr->sh_addralign); | |
5522 | off = _bfd_elf_assign_file_position_for_section (hdr, off, | |
5523 | FALSE); | |
5524 | } | |
5525 | else if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA) | |
5526 | && hdr->bfd_section == NULL) | |
0ce398f1 L |
5527 | || (hdr->bfd_section != NULL |
5528 | && (hdr->bfd_section->flags & SEC_ELF_COMPRESS)) | |
5529 | /* Compress DWARF debug sections. */ | |
12bd6957 | 5530 | || hdr == i_shdrpp[elf_onesymtab (abfd)] |
6a40cf0c NC |
5531 | || (elf_symtab_shndx_list (abfd) != NULL |
5532 | && hdr == i_shdrpp[elf_symtab_shndx_list (abfd)->ndx]) | |
3e19fb8f L |
5533 | || hdr == i_shdrpp[elf_strtab_sec (abfd)] |
5534 | || hdr == i_shdrpp[elf_shstrtab_sec (abfd)]) | |
5c182d5f AM |
5535 | hdr->sh_offset = -1; |
5536 | else | |
5537 | off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE); | |
5c182d5f AM |
5538 | } |
5539 | ||
252b5132 RH |
5540 | /* Now that we have set the section file positions, we can set up |
5541 | the file positions for the non PT_LOAD segments. */ | |
f3520d2f AM |
5542 | count = 0; |
5543 | filehdr_vaddr = 0; | |
5544 | filehdr_paddr = 0; | |
5545 | phdrs_vaddr = bed->maxpagesize + bed->s->sizeof_ehdr; | |
5546 | phdrs_paddr = 0; | |
62655c7b | 5547 | hdrs_segment = NULL; |
f3520d2f | 5548 | phdrs = elf_tdata (abfd)->phdr; |
12bd6957 | 5549 | for (m = elf_seg_map (abfd), p = phdrs; m != NULL; m = m->next, p++) |
f3520d2f AM |
5550 | { |
5551 | ++count; | |
5552 | if (p->p_type != PT_LOAD) | |
5553 | continue; | |
5554 | ||
5555 | if (m->includes_filehdr) | |
5556 | { | |
5557 | filehdr_vaddr = p->p_vaddr; | |
5558 | filehdr_paddr = p->p_paddr; | |
5559 | } | |
5560 | if (m->includes_phdrs) | |
5561 | { | |
5562 | phdrs_vaddr = p->p_vaddr; | |
5563 | phdrs_paddr = p->p_paddr; | |
5564 | if (m->includes_filehdr) | |
5565 | { | |
62655c7b | 5566 | hdrs_segment = m; |
f3520d2f AM |
5567 | phdrs_vaddr += bed->s->sizeof_ehdr; |
5568 | phdrs_paddr += bed->s->sizeof_ehdr; | |
5569 | } | |
5570 | } | |
5571 | } | |
5572 | ||
62655c7b RM |
5573 | if (hdrs_segment != NULL && link_info != NULL) |
5574 | { | |
5575 | /* There is a segment that contains both the file headers and the | |
5576 | program headers, so provide a symbol __ehdr_start pointing there. | |
5577 | A program can use this to examine itself robustly. */ | |
5578 | ||
5579 | struct elf_link_hash_entry *hash | |
5580 | = elf_link_hash_lookup (elf_hash_table (link_info), "__ehdr_start", | |
5581 | FALSE, FALSE, TRUE); | |
5582 | /* If the symbol was referenced and not defined, define it. */ | |
5583 | if (hash != NULL | |
5584 | && (hash->root.type == bfd_link_hash_new | |
5585 | || hash->root.type == bfd_link_hash_undefined | |
5586 | || hash->root.type == bfd_link_hash_undefweak | |
5587 | || hash->root.type == bfd_link_hash_common)) | |
5588 | { | |
5589 | asection *s = NULL; | |
5590 | if (hdrs_segment->count != 0) | |
5591 | /* The segment contains sections, so use the first one. */ | |
5592 | s = hdrs_segment->sections[0]; | |
5593 | else | |
5594 | /* Use the first (i.e. lowest-addressed) section in any segment. */ | |
12bd6957 | 5595 | for (m = elf_seg_map (abfd); m != NULL; m = m->next) |
62655c7b RM |
5596 | if (m->count != 0) |
5597 | { | |
5598 | s = m->sections[0]; | |
5599 | break; | |
5600 | } | |
5601 | ||
5602 | if (s != NULL) | |
5603 | { | |
5604 | hash->root.u.def.value = filehdr_vaddr - s->vma; | |
5605 | hash->root.u.def.section = s; | |
5606 | } | |
5607 | else | |
5608 | { | |
5609 | hash->root.u.def.value = filehdr_vaddr; | |
5610 | hash->root.u.def.section = bfd_abs_section_ptr; | |
5611 | } | |
5612 | ||
5613 | hash->root.type = bfd_link_hash_defined; | |
5614 | hash->def_regular = 1; | |
5615 | hash->non_elf = 0; | |
5616 | } | |
5617 | } | |
5618 | ||
12bd6957 | 5619 | for (m = elf_seg_map (abfd), p = phdrs; m != NULL; m = m->next, p++) |
252b5132 | 5620 | { |
129af99f | 5621 | if (p->p_type == PT_GNU_RELRO) |
252b5132 | 5622 | { |
b84a33b5 | 5623 | const Elf_Internal_Phdr *lp; |
3146fac4 | 5624 | struct elf_segment_map *lm; |
1ea63fd2 | 5625 | |
129af99f | 5626 | if (link_info != NULL) |
8c37241b | 5627 | { |
129af99f AS |
5628 | /* During linking the range of the RELRO segment is passed |
5629 | in link_info. */ | |
12bd6957 | 5630 | for (lm = elf_seg_map (abfd), lp = phdrs; |
3146fac4 AM |
5631 | lm != NULL; |
5632 | lm = lm->next, lp++) | |
8c37241b JJ |
5633 | { |
5634 | if (lp->p_type == PT_LOAD | |
b84a33b5 | 5635 | && lp->p_vaddr < link_info->relro_end |
3146fac4 AM |
5636 | && lm->count != 0 |
5637 | && lm->sections[0]->vma >= link_info->relro_start) | |
8c37241b JJ |
5638 | break; |
5639 | } | |
8981c88a | 5640 | |
3146fac4 | 5641 | BFD_ASSERT (lm != NULL); |
8c37241b | 5642 | } |
129af99f AS |
5643 | else |
5644 | { | |
5645 | /* Otherwise we are copying an executable or shared | |
b84a33b5 | 5646 | library, but we need to use the same linker logic. */ |
129af99f AS |
5647 | for (lp = phdrs; lp < phdrs + count; ++lp) |
5648 | { | |
5649 | if (lp->p_type == PT_LOAD | |
5650 | && lp->p_paddr == p->p_paddr) | |
5651 | break; | |
5652 | } | |
b84a33b5 AM |
5653 | } |
5654 | ||
5655 | if (lp < phdrs + count) | |
5656 | { | |
5657 | p->p_vaddr = lp->p_vaddr; | |
5658 | p->p_paddr = lp->p_paddr; | |
5659 | p->p_offset = lp->p_offset; | |
5660 | if (link_info != NULL) | |
5661 | p->p_filesz = link_info->relro_end - lp->p_vaddr; | |
5662 | else if (m->p_size_valid) | |
5663 | p->p_filesz = m->p_size; | |
129af99f AS |
5664 | else |
5665 | abort (); | |
b84a33b5 | 5666 | p->p_memsz = p->p_filesz; |
f3944f72 L |
5667 | /* Preserve the alignment and flags if they are valid. The |
5668 | gold linker generates RW/4 for the PT_GNU_RELRO section. | |
5669 | It is better for objcopy/strip to honor these attributes | |
5670 | otherwise gdb will choke when using separate debug files. | |
5671 | */ | |
5672 | if (!m->p_align_valid) | |
5673 | p->p_align = 1; | |
5674 | if (!m->p_flags_valid) | |
341b8ee7 | 5675 | p->p_flags = PF_R; |
129af99f | 5676 | } |
9433b9b1 | 5677 | else |
b84a33b5 AM |
5678 | { |
5679 | memset (p, 0, sizeof *p); | |
5680 | p->p_type = PT_NULL; | |
5681 | } | |
129af99f | 5682 | } |
04c3a755 NS |
5683 | else if (p->p_type == PT_GNU_STACK) |
5684 | { | |
5685 | if (m->p_size_valid) | |
5686 | p->p_memsz = m->p_size; | |
5687 | } | |
129af99f AS |
5688 | else if (m->count != 0) |
5689 | { | |
e06efbf1 | 5690 | unsigned int i; |
129af99f AS |
5691 | if (p->p_type != PT_LOAD |
5692 | && (p->p_type != PT_NOTE | |
5693 | || bfd_get_format (abfd) != bfd_core)) | |
5694 | { | |
c86934ce NC |
5695 | if (m->includes_filehdr || m->includes_phdrs) |
5696 | { | |
b1fa9dd6 | 5697 | /* PR 17512: file: 2195325e. */ |
c86934ce NC |
5698 | (*_bfd_error_handler) |
5699 | (_("%B: warning: non-load segment includes file header and/or program header"), | |
5700 | abfd); | |
5701 | return FALSE; | |
5702 | } | |
129af99f | 5703 | |
86b2281f | 5704 | p->p_filesz = 0; |
129af99f | 5705 | p->p_offset = m->sections[0]->filepos; |
86b2281f AM |
5706 | for (i = m->count; i-- != 0;) |
5707 | { | |
5708 | asection *sect = m->sections[i]; | |
5709 | Elf_Internal_Shdr *hdr = &elf_section_data (sect)->this_hdr; | |
5710 | if (hdr->sh_type != SHT_NOBITS) | |
5711 | { | |
5712 | p->p_filesz = (sect->filepos - m->sections[0]->filepos | |
5713 | + hdr->sh_size); | |
5714 | break; | |
5715 | } | |
5716 | } | |
129af99f AS |
5717 | } |
5718 | } | |
5719 | else if (m->includes_filehdr) | |
5720 | { | |
5721 | p->p_vaddr = filehdr_vaddr; | |
5722 | if (! m->p_paddr_valid) | |
5723 | p->p_paddr = filehdr_paddr; | |
5724 | } | |
5725 | else if (m->includes_phdrs) | |
5726 | { | |
5727 | p->p_vaddr = phdrs_vaddr; | |
5728 | if (! m->p_paddr_valid) | |
5729 | p->p_paddr = phdrs_paddr; | |
252b5132 RH |
5730 | } |
5731 | } | |
5732 | ||
12bd6957 | 5733 | elf_next_file_pos (abfd) = off; |
252b5132 | 5734 | |
b34976b6 | 5735 | return TRUE; |
252b5132 RH |
5736 | } |
5737 | ||
6a40cf0c NC |
5738 | static elf_section_list * |
5739 | find_section_in_list (unsigned int i, elf_section_list * list) | |
5740 | { | |
5741 | for (;list != NULL; list = list->next) | |
5742 | if (list->ndx == i) | |
5743 | break; | |
5744 | return list; | |
5745 | } | |
5746 | ||
252b5132 RH |
5747 | /* Work out the file positions of all the sections. This is called by |
5748 | _bfd_elf_compute_section_file_positions. All the section sizes and | |
5749 | VMAs must be known before this is called. | |
5750 | ||
e0638f70 AM |
5751 | Reloc sections come in two flavours: Those processed specially as |
5752 | "side-channel" data attached to a section to which they apply, and | |
5753 | those that bfd doesn't process as relocations. The latter sort are | |
5754 | stored in a normal bfd section by bfd_section_from_shdr. We don't | |
5755 | consider the former sort here, unless they form part of the loadable | |
5756 | image. Reloc sections not assigned here will be handled later by | |
5757 | assign_file_positions_for_relocs. | |
252b5132 RH |
5758 | |
5759 | We also don't set the positions of the .symtab and .strtab here. */ | |
5760 | ||
b34976b6 | 5761 | static bfd_boolean |
c84fca4d AO |
5762 | assign_file_positions_except_relocs (bfd *abfd, |
5763 | struct bfd_link_info *link_info) | |
252b5132 | 5764 | { |
5c182d5f AM |
5765 | struct elf_obj_tdata *tdata = elf_tdata (abfd); |
5766 | Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd); | |
9c5bfbb7 | 5767 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
252b5132 RH |
5768 | |
5769 | if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0 | |
5770 | && bfd_get_format (abfd) != bfd_core) | |
5771 | { | |
5c182d5f AM |
5772 | Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd); |
5773 | unsigned int num_sec = elf_numsections (abfd); | |
252b5132 RH |
5774 | Elf_Internal_Shdr **hdrpp; |
5775 | unsigned int i; | |
a485e98e | 5776 | file_ptr off; |
252b5132 RH |
5777 | |
5778 | /* Start after the ELF header. */ | |
5779 | off = i_ehdrp->e_ehsize; | |
5780 | ||
5781 | /* We are not creating an executable, which means that we are | |
5782 | not creating a program header, and that the actual order of | |
5783 | the sections in the file is unimportant. */ | |
9ad5cbcf | 5784 | for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++) |
252b5132 RH |
5785 | { |
5786 | Elf_Internal_Shdr *hdr; | |
5787 | ||
5788 | hdr = *hdrpp; | |
e0638f70 AM |
5789 | if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA) |
5790 | && hdr->bfd_section == NULL) | |
0ce398f1 L |
5791 | || (hdr->bfd_section != NULL |
5792 | && (hdr->bfd_section->flags & SEC_ELF_COMPRESS)) | |
5793 | /* Compress DWARF debug sections. */ | |
12bd6957 | 5794 | || i == elf_onesymtab (abfd) |
6a40cf0c NC |
5795 | || (elf_symtab_shndx_list (abfd) != NULL |
5796 | && hdr == i_shdrpp[elf_symtab_shndx_list (abfd)->ndx]) | |
3e19fb8f L |
5797 | || i == elf_strtab_sec (abfd) |
5798 | || i == elf_shstrtab_sec (abfd)) | |
252b5132 RH |
5799 | { |
5800 | hdr->sh_offset = -1; | |
252b5132 | 5801 | } |
9ad5cbcf | 5802 | else |
b34976b6 | 5803 | off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE); |
252b5132 | 5804 | } |
a485e98e AM |
5805 | |
5806 | elf_next_file_pos (abfd) = off; | |
252b5132 RH |
5807 | } |
5808 | else | |
5809 | { | |
f3520d2f AM |
5810 | unsigned int alloc; |
5811 | ||
252b5132 | 5812 | /* Assign file positions for the loaded sections based on the |
08a40648 | 5813 | assignment of sections to segments. */ |
f3520d2f AM |
5814 | if (!assign_file_positions_for_load_sections (abfd, link_info)) |
5815 | return FALSE; | |
5816 | ||
5817 | /* And for non-load sections. */ | |
5818 | if (!assign_file_positions_for_non_load_sections (abfd, link_info)) | |
5819 | return FALSE; | |
5820 | ||
e36284ab AM |
5821 | if (bed->elf_backend_modify_program_headers != NULL) |
5822 | { | |
5823 | if (!(*bed->elf_backend_modify_program_headers) (abfd, link_info)) | |
5824 | return FALSE; | |
5825 | } | |
5826 | ||
58e7ebac | 5827 | /* Set e_type in ELF header to ET_EXEC for -pie -Ttext-segment=. */ |
0e1862bb | 5828 | if (link_info != NULL && bfd_link_pie (link_info)) |
58e7ebac L |
5829 | { |
5830 | unsigned int num_segments = elf_elfheader (abfd)->e_phnum; | |
5831 | Elf_Internal_Phdr *segment = elf_tdata (abfd)->phdr; | |
5832 | Elf_Internal_Phdr *end_segment = &segment[num_segments]; | |
5833 | ||
5834 | /* Find the lowest p_vaddr in PT_LOAD segments. */ | |
5835 | bfd_vma p_vaddr = (bfd_vma) -1; | |
5836 | for (; segment < end_segment; segment++) | |
5837 | if (segment->p_type == PT_LOAD && p_vaddr > segment->p_vaddr) | |
5838 | p_vaddr = segment->p_vaddr; | |
5839 | ||
5840 | /* Set e_type to ET_EXEC if the lowest p_vaddr in PT_LOAD | |
5841 | segments is non-zero. */ | |
5842 | if (p_vaddr) | |
5843 | i_ehdrp->e_type = ET_EXEC; | |
5844 | } | |
5845 | ||
f3520d2f | 5846 | /* Write out the program headers. */ |
12bd6957 | 5847 | alloc = elf_program_header_size (abfd) / bed->s->sizeof_phdr; |
f3520d2f AM |
5848 | if (bfd_seek (abfd, (bfd_signed_vma) bed->s->sizeof_ehdr, SEEK_SET) != 0 |
5849 | || bed->s->write_out_phdrs (abfd, tdata->phdr, alloc) != 0) | |
b34976b6 | 5850 | return FALSE; |
252b5132 RH |
5851 | } |
5852 | ||
b34976b6 | 5853 | return TRUE; |
252b5132 RH |
5854 | } |
5855 | ||
b34976b6 | 5856 | static bfd_boolean |
217aa764 | 5857 | prep_headers (bfd *abfd) |
252b5132 | 5858 | { |
3d540e93 | 5859 | Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form. */ |
2b0f7ef9 | 5860 | struct elf_strtab_hash *shstrtab; |
9c5bfbb7 | 5861 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
252b5132 RH |
5862 | |
5863 | i_ehdrp = elf_elfheader (abfd); | |
252b5132 | 5864 | |
2b0f7ef9 | 5865 | shstrtab = _bfd_elf_strtab_init (); |
252b5132 | 5866 | if (shstrtab == NULL) |
b34976b6 | 5867 | return FALSE; |
252b5132 RH |
5868 | |
5869 | elf_shstrtab (abfd) = shstrtab; | |
5870 | ||
5871 | i_ehdrp->e_ident[EI_MAG0] = ELFMAG0; | |
5872 | i_ehdrp->e_ident[EI_MAG1] = ELFMAG1; | |
5873 | i_ehdrp->e_ident[EI_MAG2] = ELFMAG2; | |
5874 | i_ehdrp->e_ident[EI_MAG3] = ELFMAG3; | |
5875 | ||
5876 | i_ehdrp->e_ident[EI_CLASS] = bed->s->elfclass; | |
5877 | i_ehdrp->e_ident[EI_DATA] = | |
5878 | bfd_big_endian (abfd) ? ELFDATA2MSB : ELFDATA2LSB; | |
5879 | i_ehdrp->e_ident[EI_VERSION] = bed->s->ev_current; | |
5880 | ||
252b5132 RH |
5881 | if ((abfd->flags & DYNAMIC) != 0) |
5882 | i_ehdrp->e_type = ET_DYN; | |
5883 | else if ((abfd->flags & EXEC_P) != 0) | |
5884 | i_ehdrp->e_type = ET_EXEC; | |
5885 | else if (bfd_get_format (abfd) == bfd_core) | |
5886 | i_ehdrp->e_type = ET_CORE; | |
5887 | else | |
5888 | i_ehdrp->e_type = ET_REL; | |
5889 | ||
5890 | switch (bfd_get_arch (abfd)) | |
5891 | { | |
5892 | case bfd_arch_unknown: | |
5893 | i_ehdrp->e_machine = EM_NONE; | |
5894 | break; | |
aa4f99bb AO |
5895 | |
5896 | /* There used to be a long list of cases here, each one setting | |
5897 | e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE | |
5898 | in the corresponding bfd definition. To avoid duplication, | |
5899 | the switch was removed. Machines that need special handling | |
5900 | can generally do it in elf_backend_final_write_processing(), | |
5901 | unless they need the information earlier than the final write. | |
5902 | Such need can generally be supplied by replacing the tests for | |
5903 | e_machine with the conditions used to determine it. */ | |
252b5132 | 5904 | default: |
9c5bfbb7 AM |
5905 | i_ehdrp->e_machine = bed->elf_machine_code; |
5906 | } | |
aa4f99bb | 5907 | |
252b5132 RH |
5908 | i_ehdrp->e_version = bed->s->ev_current; |
5909 | i_ehdrp->e_ehsize = bed->s->sizeof_ehdr; | |
5910 | ||
c044fabd | 5911 | /* No program header, for now. */ |
252b5132 RH |
5912 | i_ehdrp->e_phoff = 0; |
5913 | i_ehdrp->e_phentsize = 0; | |
5914 | i_ehdrp->e_phnum = 0; | |
5915 | ||
c044fabd | 5916 | /* Each bfd section is section header entry. */ |
252b5132 RH |
5917 | i_ehdrp->e_entry = bfd_get_start_address (abfd); |
5918 | i_ehdrp->e_shentsize = bed->s->sizeof_shdr; | |
5919 | ||
c044fabd | 5920 | /* If we're building an executable, we'll need a program header table. */ |
252b5132 | 5921 | if (abfd->flags & EXEC_P) |
0e71e495 BE |
5922 | /* It all happens later. */ |
5923 | ; | |
252b5132 RH |
5924 | else |
5925 | { | |
5926 | i_ehdrp->e_phentsize = 0; | |
252b5132 RH |
5927 | i_ehdrp->e_phoff = 0; |
5928 | } | |
5929 | ||
5930 | elf_tdata (abfd)->symtab_hdr.sh_name = | |
b34976b6 | 5931 | (unsigned int) _bfd_elf_strtab_add (shstrtab, ".symtab", FALSE); |
252b5132 | 5932 | elf_tdata (abfd)->strtab_hdr.sh_name = |
b34976b6 | 5933 | (unsigned int) _bfd_elf_strtab_add (shstrtab, ".strtab", FALSE); |
252b5132 | 5934 | elf_tdata (abfd)->shstrtab_hdr.sh_name = |
b34976b6 | 5935 | (unsigned int) _bfd_elf_strtab_add (shstrtab, ".shstrtab", FALSE); |
252b5132 | 5936 | if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1 |
17ca87fc | 5937 | || elf_tdata (abfd)->strtab_hdr.sh_name == (unsigned int) -1 |
252b5132 | 5938 | || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1) |
b34976b6 | 5939 | return FALSE; |
252b5132 | 5940 | |
b34976b6 | 5941 | return TRUE; |
252b5132 RH |
5942 | } |
5943 | ||
5944 | /* Assign file positions for all the reloc sections which are not part | |
a485e98e | 5945 | of the loadable file image, and the file position of section headers. */ |
252b5132 | 5946 | |
0ce398f1 L |
5947 | static bfd_boolean |
5948 | _bfd_elf_assign_file_positions_for_non_load (bfd *abfd) | |
252b5132 RH |
5949 | { |
5950 | file_ptr off; | |
e06efbf1 | 5951 | Elf_Internal_Shdr **shdrpp, **end_shdrpp; |
3e19fb8f | 5952 | Elf_Internal_Shdr *shdrp; |
a485e98e AM |
5953 | Elf_Internal_Ehdr *i_ehdrp; |
5954 | const struct elf_backend_data *bed; | |
252b5132 | 5955 | |
12bd6957 | 5956 | off = elf_next_file_pos (abfd); |
252b5132 | 5957 | |
e06efbf1 L |
5958 | shdrpp = elf_elfsections (abfd); |
5959 | end_shdrpp = shdrpp + elf_numsections (abfd); | |
5960 | for (shdrpp++; shdrpp < end_shdrpp; shdrpp++) | |
252b5132 | 5961 | { |
252b5132 | 5962 | shdrp = *shdrpp; |
0ce398f1 L |
5963 | if (shdrp->sh_offset == -1) |
5964 | { | |
3e19fb8f | 5965 | asection *sec = shdrp->bfd_section; |
0ce398f1 L |
5966 | bfd_boolean is_rel = (shdrp->sh_type == SHT_REL |
5967 | || shdrp->sh_type == SHT_RELA); | |
5968 | if (is_rel | |
3e19fb8f | 5969 | || (sec != NULL && (sec->flags & SEC_ELF_COMPRESS))) |
0ce398f1 L |
5970 | { |
5971 | if (!is_rel) | |
5972 | { | |
3e19fb8f L |
5973 | const char *name = sec->name; |
5974 | struct bfd_elf_section_data *d; | |
5975 | ||
0ce398f1 | 5976 | /* Compress DWARF debug sections. */ |
3e19fb8f | 5977 | if (!bfd_compress_section (abfd, sec, |
0ce398f1 L |
5978 | shdrp->contents)) |
5979 | return FALSE; | |
3e19fb8f L |
5980 | |
5981 | if (sec->compress_status == COMPRESS_SECTION_DONE | |
5982 | && (abfd->flags & BFD_COMPRESS_GABI) == 0) | |
5983 | { | |
5984 | /* If section is compressed with zlib-gnu, convert | |
5985 | section name from .debug_* to .zdebug_*. */ | |
5986 | char *new_name | |
5987 | = convert_debug_to_zdebug (abfd, name); | |
5988 | if (new_name == NULL) | |
5989 | return FALSE; | |
5990 | name = new_name; | |
5991 | } | |
5992 | /* Add setion name to section name section. */ | |
5993 | if (shdrp->sh_name != (unsigned int) -1) | |
5994 | abort (); | |
5995 | shdrp->sh_name | |
5996 | = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), | |
5997 | name, FALSE); | |
5998 | d = elf_section_data (sec); | |
5999 | ||
6000 | /* Add reloc setion name to section name section. */ | |
6001 | if (d->rel.hdr | |
6002 | && !_bfd_elf_set_reloc_sh_name (abfd, | |
6003 | d->rel.hdr, | |
6004 | name, FALSE)) | |
6005 | return FALSE; | |
6006 | if (d->rela.hdr | |
6007 | && !_bfd_elf_set_reloc_sh_name (abfd, | |
6008 | d->rela.hdr, | |
91cb26da | 6009 | name, TRUE)) |
3e19fb8f L |
6010 | return FALSE; |
6011 | ||
0ce398f1 | 6012 | /* Update section size and contents. */ |
3e19fb8f L |
6013 | shdrp->sh_size = sec->size; |
6014 | shdrp->contents = sec->contents; | |
0ce398f1 L |
6015 | shdrp->bfd_section->contents = NULL; |
6016 | } | |
6017 | off = _bfd_elf_assign_file_position_for_section (shdrp, | |
6018 | off, | |
6019 | TRUE); | |
6020 | } | |
6021 | } | |
252b5132 RH |
6022 | } |
6023 | ||
3e19fb8f L |
6024 | /* Place section name section after DWARF debug sections have been |
6025 | compressed. */ | |
6026 | _bfd_elf_strtab_finalize (elf_shstrtab (abfd)); | |
6027 | shdrp = &elf_tdata (abfd)->shstrtab_hdr; | |
6028 | shdrp->sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd)); | |
6029 | off = _bfd_elf_assign_file_position_for_section (shdrp, off, TRUE); | |
6030 | ||
6031 | /* Place the section headers. */ | |
a485e98e AM |
6032 | i_ehdrp = elf_elfheader (abfd); |
6033 | bed = get_elf_backend_data (abfd); | |
6034 | off = align_file_position (off, 1 << bed->s->log_file_align); | |
6035 | i_ehdrp->e_shoff = off; | |
6036 | off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize; | |
12bd6957 | 6037 | elf_next_file_pos (abfd) = off; |
0ce398f1 L |
6038 | |
6039 | return TRUE; | |
252b5132 RH |
6040 | } |
6041 | ||
b34976b6 | 6042 | bfd_boolean |
217aa764 | 6043 | _bfd_elf_write_object_contents (bfd *abfd) |
252b5132 | 6044 | { |
9c5bfbb7 | 6045 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
252b5132 | 6046 | Elf_Internal_Shdr **i_shdrp; |
b34976b6 | 6047 | bfd_boolean failed; |
9ad5cbcf | 6048 | unsigned int count, num_sec; |
30e8ee25 | 6049 | struct elf_obj_tdata *t; |
252b5132 RH |
6050 | |
6051 | if (! abfd->output_has_begun | |
217aa764 | 6052 | && ! _bfd_elf_compute_section_file_positions (abfd, NULL)) |
b34976b6 | 6053 | return FALSE; |
252b5132 RH |
6054 | |
6055 | i_shdrp = elf_elfsections (abfd); | |
252b5132 | 6056 | |
b34976b6 | 6057 | failed = FALSE; |
252b5132 RH |
6058 | bfd_map_over_sections (abfd, bed->s->write_relocs, &failed); |
6059 | if (failed) | |
b34976b6 | 6060 | return FALSE; |
252b5132 | 6061 | |
0ce398f1 L |
6062 | if (!_bfd_elf_assign_file_positions_for_non_load (abfd)) |
6063 | return FALSE; | |
252b5132 | 6064 | |
c044fabd | 6065 | /* After writing the headers, we need to write the sections too... */ |
9ad5cbcf AM |
6066 | num_sec = elf_numsections (abfd); |
6067 | for (count = 1; count < num_sec; count++) | |
252b5132 | 6068 | { |
3e19fb8f L |
6069 | i_shdrp[count]->sh_name |
6070 | = _bfd_elf_strtab_offset (elf_shstrtab (abfd), | |
6071 | i_shdrp[count]->sh_name); | |
252b5132 RH |
6072 | if (bed->elf_backend_section_processing) |
6073 | (*bed->elf_backend_section_processing) (abfd, i_shdrp[count]); | |
6074 | if (i_shdrp[count]->contents) | |
6075 | { | |
dc810e39 AM |
6076 | bfd_size_type amt = i_shdrp[count]->sh_size; |
6077 | ||
252b5132 | 6078 | if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0 |
dc810e39 | 6079 | || bfd_bwrite (i_shdrp[count]->contents, amt, abfd) != amt) |
b34976b6 | 6080 | return FALSE; |
252b5132 RH |
6081 | } |
6082 | } | |
6083 | ||
6084 | /* Write out the section header names. */ | |
30e8ee25 | 6085 | t = elf_tdata (abfd); |
26ae6d5e | 6086 | if (elf_shstrtab (abfd) != NULL |
30e8ee25 | 6087 | && (bfd_seek (abfd, t->shstrtab_hdr.sh_offset, SEEK_SET) != 0 |
08a40648 | 6088 | || !_bfd_elf_strtab_emit (abfd, elf_shstrtab (abfd)))) |
b34976b6 | 6089 | return FALSE; |
252b5132 RH |
6090 | |
6091 | if (bed->elf_backend_final_write_processing) | |
12bd6957 | 6092 | (*bed->elf_backend_final_write_processing) (abfd, elf_linker (abfd)); |
252b5132 | 6093 | |
ff59fc36 RM |
6094 | if (!bed->s->write_shdrs_and_ehdr (abfd)) |
6095 | return FALSE; | |
6096 | ||
6097 | /* This is last since write_shdrs_and_ehdr can touch i_shdrp[0]. */ | |
c0355132 AM |
6098 | if (t->o->build_id.after_write_object_contents != NULL) |
6099 | return (*t->o->build_id.after_write_object_contents) (abfd); | |
ff59fc36 RM |
6100 | |
6101 | return TRUE; | |
252b5132 RH |
6102 | } |
6103 | ||
b34976b6 | 6104 | bfd_boolean |
217aa764 | 6105 | _bfd_elf_write_corefile_contents (bfd *abfd) |
252b5132 | 6106 | { |
c044fabd | 6107 | /* Hopefully this can be done just like an object file. */ |
252b5132 RH |
6108 | return _bfd_elf_write_object_contents (abfd); |
6109 | } | |
c044fabd KH |
6110 | |
6111 | /* Given a section, search the header to find them. */ | |
6112 | ||
cb33740c | 6113 | unsigned int |
198beae2 | 6114 | _bfd_elf_section_from_bfd_section (bfd *abfd, struct bfd_section *asect) |
252b5132 | 6115 | { |
9c5bfbb7 | 6116 | const struct elf_backend_data *bed; |
91d6fa6a | 6117 | unsigned int sec_index; |
252b5132 | 6118 | |
9ad5cbcf AM |
6119 | if (elf_section_data (asect) != NULL |
6120 | && elf_section_data (asect)->this_idx != 0) | |
6121 | return elf_section_data (asect)->this_idx; | |
6122 | ||
6123 | if (bfd_is_abs_section (asect)) | |
91d6fa6a | 6124 | sec_index = SHN_ABS; |
af746e92 | 6125 | else if (bfd_is_com_section (asect)) |
91d6fa6a | 6126 | sec_index = SHN_COMMON; |
af746e92 | 6127 | else if (bfd_is_und_section (asect)) |
91d6fa6a | 6128 | sec_index = SHN_UNDEF; |
af746e92 | 6129 | else |
91d6fa6a | 6130 | sec_index = SHN_BAD; |
252b5132 | 6131 | |
af746e92 | 6132 | bed = get_elf_backend_data (abfd); |
252b5132 RH |
6133 | if (bed->elf_backend_section_from_bfd_section) |
6134 | { | |
91d6fa6a | 6135 | int retval = sec_index; |
9ad5cbcf | 6136 | |
af746e92 AM |
6137 | if ((*bed->elf_backend_section_from_bfd_section) (abfd, asect, &retval)) |
6138 | return retval; | |
252b5132 RH |
6139 | } |
6140 | ||
91d6fa6a | 6141 | if (sec_index == SHN_BAD) |
af746e92 | 6142 | bfd_set_error (bfd_error_nonrepresentable_section); |
252b5132 | 6143 | |
91d6fa6a | 6144 | return sec_index; |
252b5132 RH |
6145 | } |
6146 | ||
6147 | /* Given a BFD symbol, return the index in the ELF symbol table, or -1 | |
6148 | on error. */ | |
6149 | ||
6150 | int | |
217aa764 | 6151 | _bfd_elf_symbol_from_bfd_symbol (bfd *abfd, asymbol **asym_ptr_ptr) |
252b5132 RH |
6152 | { |
6153 | asymbol *asym_ptr = *asym_ptr_ptr; | |
6154 | int idx; | |
6155 | flagword flags = asym_ptr->flags; | |
6156 | ||
6157 | /* When gas creates relocations against local labels, it creates its | |
6158 | own symbol for the section, but does put the symbol into the | |
6159 | symbol chain, so udata is 0. When the linker is generating | |
6160 | relocatable output, this section symbol may be for one of the | |
6161 | input sections rather than the output section. */ | |
6162 | if (asym_ptr->udata.i == 0 | |
6163 | && (flags & BSF_SECTION_SYM) | |
6164 | && asym_ptr->section) | |
6165 | { | |
5372391b | 6166 | asection *sec; |
252b5132 RH |
6167 | int indx; |
6168 | ||
5372391b AM |
6169 | sec = asym_ptr->section; |
6170 | if (sec->owner != abfd && sec->output_section != NULL) | |
6171 | sec = sec->output_section; | |
6172 | if (sec->owner == abfd | |
6173 | && (indx = sec->index) < elf_num_section_syms (abfd) | |
4e89ac30 | 6174 | && elf_section_syms (abfd)[indx] != NULL) |
252b5132 RH |
6175 | asym_ptr->udata.i = elf_section_syms (abfd)[indx]->udata.i; |
6176 | } | |
6177 | ||
6178 | idx = asym_ptr->udata.i; | |
6179 | ||
6180 | if (idx == 0) | |
6181 | { | |
6182 | /* This case can occur when using --strip-symbol on a symbol | |
08a40648 | 6183 | which is used in a relocation entry. */ |
252b5132 | 6184 | (*_bfd_error_handler) |
d003868e AM |
6185 | (_("%B: symbol `%s' required but not present"), |
6186 | abfd, bfd_asymbol_name (asym_ptr)); | |
252b5132 RH |
6187 | bfd_set_error (bfd_error_no_symbols); |
6188 | return -1; | |
6189 | } | |
6190 | ||
6191 | #if DEBUG & 4 | |
6192 | { | |
6193 | fprintf (stderr, | |
9ccb8af9 AM |
6194 | "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8lx\n", |
6195 | (long) asym_ptr, asym_ptr->name, idx, (long) flags); | |
252b5132 RH |
6196 | fflush (stderr); |
6197 | } | |
6198 | #endif | |
6199 | ||
6200 | return idx; | |
6201 | } | |
6202 | ||
84d1d650 | 6203 | /* Rewrite program header information. */ |
252b5132 | 6204 | |
b34976b6 | 6205 | static bfd_boolean |
84d1d650 | 6206 | rewrite_elf_program_header (bfd *ibfd, bfd *obfd) |
252b5132 | 6207 | { |
b34976b6 AM |
6208 | Elf_Internal_Ehdr *iehdr; |
6209 | struct elf_segment_map *map; | |
6210 | struct elf_segment_map *map_first; | |
6211 | struct elf_segment_map **pointer_to_map; | |
6212 | Elf_Internal_Phdr *segment; | |
6213 | asection *section; | |
6214 | unsigned int i; | |
6215 | unsigned int num_segments; | |
6216 | bfd_boolean phdr_included = FALSE; | |
5c44b38e | 6217 | bfd_boolean p_paddr_valid; |
b34976b6 AM |
6218 | bfd_vma maxpagesize; |
6219 | struct elf_segment_map *phdr_adjust_seg = NULL; | |
6220 | unsigned int phdr_adjust_num = 0; | |
9c5bfbb7 | 6221 | const struct elf_backend_data *bed; |
bc67d8a6 | 6222 | |
caf47ea6 | 6223 | bed = get_elf_backend_data (ibfd); |
252b5132 RH |
6224 | iehdr = elf_elfheader (ibfd); |
6225 | ||
bc67d8a6 | 6226 | map_first = NULL; |
c044fabd | 6227 | pointer_to_map = &map_first; |
252b5132 RH |
6228 | |
6229 | num_segments = elf_elfheader (ibfd)->e_phnum; | |
bc67d8a6 NC |
6230 | maxpagesize = get_elf_backend_data (obfd)->maxpagesize; |
6231 | ||
6232 | /* Returns the end address of the segment + 1. */ | |
aecc8f8a AM |
6233 | #define SEGMENT_END(segment, start) \ |
6234 | (start + (segment->p_memsz > segment->p_filesz \ | |
6235 | ? segment->p_memsz : segment->p_filesz)) | |
bc67d8a6 | 6236 | |
eecdbe52 JJ |
6237 | #define SECTION_SIZE(section, segment) \ |
6238 | (((section->flags & (SEC_HAS_CONTENTS | SEC_THREAD_LOCAL)) \ | |
6239 | != SEC_THREAD_LOCAL || segment->p_type == PT_TLS) \ | |
eea6121a | 6240 | ? section->size : 0) |
eecdbe52 | 6241 | |
b34976b6 | 6242 | /* Returns TRUE if the given section is contained within |
bc67d8a6 | 6243 | the given segment. VMA addresses are compared. */ |
aecc8f8a AM |
6244 | #define IS_CONTAINED_BY_VMA(section, segment) \ |
6245 | (section->vma >= segment->p_vaddr \ | |
eecdbe52 | 6246 | && (section->vma + SECTION_SIZE (section, segment) \ |
aecc8f8a | 6247 | <= (SEGMENT_END (segment, segment->p_vaddr)))) |
c044fabd | 6248 | |
b34976b6 | 6249 | /* Returns TRUE if the given section is contained within |
bc67d8a6 | 6250 | the given segment. LMA addresses are compared. */ |
aecc8f8a AM |
6251 | #define IS_CONTAINED_BY_LMA(section, segment, base) \ |
6252 | (section->lma >= base \ | |
eecdbe52 | 6253 | && (section->lma + SECTION_SIZE (section, segment) \ |
aecc8f8a | 6254 | <= SEGMENT_END (segment, base))) |
252b5132 | 6255 | |
0efc80c8 L |
6256 | /* Handle PT_NOTE segment. */ |
6257 | #define IS_NOTE(p, s) \ | |
aecc8f8a | 6258 | (p->p_type == PT_NOTE \ |
0efc80c8 | 6259 | && elf_section_type (s) == SHT_NOTE \ |
aecc8f8a | 6260 | && (bfd_vma) s->filepos >= p->p_offset \ |
cb3ff1e5 | 6261 | && ((bfd_vma) s->filepos + s->size \ |
aecc8f8a | 6262 | <= p->p_offset + p->p_filesz)) |
252b5132 | 6263 | |
0efc80c8 L |
6264 | /* Special case: corefile "NOTE" section containing regs, prpsinfo |
6265 | etc. */ | |
6266 | #define IS_COREFILE_NOTE(p, s) \ | |
6267 | (IS_NOTE (p, s) \ | |
6268 | && bfd_get_format (ibfd) == bfd_core \ | |
6269 | && s->vma == 0 \ | |
6270 | && s->lma == 0) | |
6271 | ||
252b5132 RH |
6272 | /* The complicated case when p_vaddr is 0 is to handle the Solaris |
6273 | linker, which generates a PT_INTERP section with p_vaddr and | |
6274 | p_memsz set to 0. */ | |
aecc8f8a AM |
6275 | #define IS_SOLARIS_PT_INTERP(p, s) \ |
6276 | (p->p_vaddr == 0 \ | |
6277 | && p->p_paddr == 0 \ | |
6278 | && p->p_memsz == 0 \ | |
6279 | && p->p_filesz > 0 \ | |
6280 | && (s->flags & SEC_HAS_CONTENTS) != 0 \ | |
eea6121a | 6281 | && s->size > 0 \ |
aecc8f8a | 6282 | && (bfd_vma) s->filepos >= p->p_offset \ |
cb3ff1e5 | 6283 | && ((bfd_vma) s->filepos + s->size \ |
aecc8f8a | 6284 | <= p->p_offset + p->p_filesz)) |
5c440b1e | 6285 | |
bc67d8a6 NC |
6286 | /* Decide if the given section should be included in the given segment. |
6287 | A section will be included if: | |
f5ffc919 | 6288 | 1. It is within the address space of the segment -- we use the LMA |
08a40648 | 6289 | if that is set for the segment and the VMA otherwise, |
0efc80c8 | 6290 | 2. It is an allocated section or a NOTE section in a PT_NOTE |
d324f6d6 | 6291 | segment. |
bc67d8a6 | 6292 | 3. There is an output section associated with it, |
eecdbe52 | 6293 | 4. The section has not already been allocated to a previous segment. |
2b05f1b7 | 6294 | 5. PT_GNU_STACK segments do not include any sections. |
03394ac9 | 6295 | 6. PT_TLS segment includes only SHF_TLS sections. |
6f79b219 JJ |
6296 | 7. SHF_TLS sections are only in PT_TLS or PT_LOAD segments. |
6297 | 8. PT_DYNAMIC should not contain empty sections at the beginning | |
08a40648 | 6298 | (with the possible exception of .dynamic). */ |
9f17e2a6 | 6299 | #define IS_SECTION_IN_INPUT_SEGMENT(section, segment, bed) \ |
2b05f1b7 L |
6300 | ((((segment->p_paddr \ |
6301 | ? IS_CONTAINED_BY_LMA (section, segment, segment->p_paddr) \ | |
6302 | : IS_CONTAINED_BY_VMA (section, segment)) \ | |
6303 | && (section->flags & SEC_ALLOC) != 0) \ | |
0efc80c8 | 6304 | || IS_NOTE (segment, section)) \ |
2b05f1b7 L |
6305 | && segment->p_type != PT_GNU_STACK \ |
6306 | && (segment->p_type != PT_TLS \ | |
6307 | || (section->flags & SEC_THREAD_LOCAL)) \ | |
6308 | && (segment->p_type == PT_LOAD \ | |
6309 | || segment->p_type == PT_TLS \ | |
6310 | || (section->flags & SEC_THREAD_LOCAL) == 0) \ | |
6311 | && (segment->p_type != PT_DYNAMIC \ | |
6312 | || SECTION_SIZE (section, segment) > 0 \ | |
6313 | || (segment->p_paddr \ | |
6314 | ? segment->p_paddr != section->lma \ | |
6315 | : segment->p_vaddr != section->vma) \ | |
6316 | || (strcmp (bfd_get_section_name (ibfd, section), ".dynamic") \ | |
6317 | == 0)) \ | |
0067a569 | 6318 | && !section->segment_mark) |
bc67d8a6 | 6319 | |
9f17e2a6 L |
6320 | /* If the output section of a section in the input segment is NULL, |
6321 | it is removed from the corresponding output segment. */ | |
6322 | #define INCLUDE_SECTION_IN_SEGMENT(section, segment, bed) \ | |
6323 | (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed) \ | |
6324 | && section->output_section != NULL) | |
6325 | ||
b34976b6 | 6326 | /* Returns TRUE iff seg1 starts after the end of seg2. */ |
b5f852ea NC |
6327 | #define SEGMENT_AFTER_SEGMENT(seg1, seg2, field) \ |
6328 | (seg1->field >= SEGMENT_END (seg2, seg2->field)) | |
6329 | ||
6330 | /* Returns TRUE iff seg1 and seg2 overlap. Segments overlap iff both | |
6331 | their VMA address ranges and their LMA address ranges overlap. | |
6332 | It is possible to have overlapping VMA ranges without overlapping LMA | |
6333 | ranges. RedBoot images for example can have both .data and .bss mapped | |
6334 | to the same VMA range, but with the .data section mapped to a different | |
6335 | LMA. */ | |
aecc8f8a | 6336 | #define SEGMENT_OVERLAPS(seg1, seg2) \ |
b5f852ea | 6337 | ( !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_vaddr) \ |
08a40648 | 6338 | || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_vaddr)) \ |
b5f852ea | 6339 | && !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_paddr) \ |
08a40648 | 6340 | || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_paddr))) |
bc67d8a6 NC |
6341 | |
6342 | /* Initialise the segment mark field. */ | |
6343 | for (section = ibfd->sections; section != NULL; section = section->next) | |
b34976b6 | 6344 | section->segment_mark = FALSE; |
bc67d8a6 | 6345 | |
5c44b38e AM |
6346 | /* The Solaris linker creates program headers in which all the |
6347 | p_paddr fields are zero. When we try to objcopy or strip such a | |
6348 | file, we get confused. Check for this case, and if we find it | |
6349 | don't set the p_paddr_valid fields. */ | |
6350 | p_paddr_valid = FALSE; | |
6351 | for (i = 0, segment = elf_tdata (ibfd)->phdr; | |
6352 | i < num_segments; | |
6353 | i++, segment++) | |
6354 | if (segment->p_paddr != 0) | |
6355 | { | |
6356 | p_paddr_valid = TRUE; | |
6357 | break; | |
6358 | } | |
6359 | ||
252b5132 | 6360 | /* Scan through the segments specified in the program header |
bc67d8a6 | 6361 | of the input BFD. For this first scan we look for overlaps |
9ad5cbcf | 6362 | in the loadable segments. These can be created by weird |
aecc8f8a | 6363 | parameters to objcopy. Also, fix some solaris weirdness. */ |
bc67d8a6 NC |
6364 | for (i = 0, segment = elf_tdata (ibfd)->phdr; |
6365 | i < num_segments; | |
c044fabd | 6366 | i++, segment++) |
252b5132 | 6367 | { |
252b5132 | 6368 | unsigned int j; |
c044fabd | 6369 | Elf_Internal_Phdr *segment2; |
252b5132 | 6370 | |
aecc8f8a AM |
6371 | if (segment->p_type == PT_INTERP) |
6372 | for (section = ibfd->sections; section; section = section->next) | |
6373 | if (IS_SOLARIS_PT_INTERP (segment, section)) | |
6374 | { | |
6375 | /* Mininal change so that the normal section to segment | |
4cc11e76 | 6376 | assignment code will work. */ |
aecc8f8a AM |
6377 | segment->p_vaddr = section->vma; |
6378 | break; | |
6379 | } | |
6380 | ||
bc67d8a6 | 6381 | if (segment->p_type != PT_LOAD) |
b10a8ae0 L |
6382 | { |
6383 | /* Remove PT_GNU_RELRO segment. */ | |
6384 | if (segment->p_type == PT_GNU_RELRO) | |
6385 | segment->p_type = PT_NULL; | |
6386 | continue; | |
6387 | } | |
c044fabd | 6388 | |
bc67d8a6 | 6389 | /* Determine if this segment overlaps any previous segments. */ |
0067a569 | 6390 | for (j = 0, segment2 = elf_tdata (ibfd)->phdr; j < i; j++, segment2++) |
bc67d8a6 NC |
6391 | { |
6392 | bfd_signed_vma extra_length; | |
c044fabd | 6393 | |
bc67d8a6 | 6394 | if (segment2->p_type != PT_LOAD |
0067a569 | 6395 | || !SEGMENT_OVERLAPS (segment, segment2)) |
bc67d8a6 | 6396 | continue; |
c044fabd | 6397 | |
bc67d8a6 NC |
6398 | /* Merge the two segments together. */ |
6399 | if (segment2->p_vaddr < segment->p_vaddr) | |
6400 | { | |
c044fabd | 6401 | /* Extend SEGMENT2 to include SEGMENT and then delete |
08a40648 | 6402 | SEGMENT. */ |
0067a569 AM |
6403 | extra_length = (SEGMENT_END (segment, segment->p_vaddr) |
6404 | - SEGMENT_END (segment2, segment2->p_vaddr)); | |
c044fabd | 6405 | |
bc67d8a6 NC |
6406 | if (extra_length > 0) |
6407 | { | |
0067a569 | 6408 | segment2->p_memsz += extra_length; |
bc67d8a6 NC |
6409 | segment2->p_filesz += extra_length; |
6410 | } | |
c044fabd | 6411 | |
bc67d8a6 | 6412 | segment->p_type = PT_NULL; |
c044fabd | 6413 | |
bc67d8a6 NC |
6414 | /* Since we have deleted P we must restart the outer loop. */ |
6415 | i = 0; | |
6416 | segment = elf_tdata (ibfd)->phdr; | |
6417 | break; | |
6418 | } | |
6419 | else | |
6420 | { | |
c044fabd | 6421 | /* Extend SEGMENT to include SEGMENT2 and then delete |
08a40648 | 6422 | SEGMENT2. */ |
0067a569 AM |
6423 | extra_length = (SEGMENT_END (segment2, segment2->p_vaddr) |
6424 | - SEGMENT_END (segment, segment->p_vaddr)); | |
c044fabd | 6425 | |
bc67d8a6 NC |
6426 | if (extra_length > 0) |
6427 | { | |
0067a569 | 6428 | segment->p_memsz += extra_length; |
bc67d8a6 NC |
6429 | segment->p_filesz += extra_length; |
6430 | } | |
c044fabd | 6431 | |
bc67d8a6 NC |
6432 | segment2->p_type = PT_NULL; |
6433 | } | |
6434 | } | |
6435 | } | |
c044fabd | 6436 | |
bc67d8a6 NC |
6437 | /* The second scan attempts to assign sections to segments. */ |
6438 | for (i = 0, segment = elf_tdata (ibfd)->phdr; | |
6439 | i < num_segments; | |
0067a569 | 6440 | i++, segment++) |
bc67d8a6 | 6441 | { |
0067a569 AM |
6442 | unsigned int section_count; |
6443 | asection **sections; | |
6444 | asection *output_section; | |
6445 | unsigned int isec; | |
6446 | bfd_vma matching_lma; | |
6447 | bfd_vma suggested_lma; | |
6448 | unsigned int j; | |
dc810e39 | 6449 | bfd_size_type amt; |
0067a569 AM |
6450 | asection *first_section; |
6451 | bfd_boolean first_matching_lma; | |
6452 | bfd_boolean first_suggested_lma; | |
bc67d8a6 NC |
6453 | |
6454 | if (segment->p_type == PT_NULL) | |
6455 | continue; | |
c044fabd | 6456 | |
9f17e2a6 | 6457 | first_section = NULL; |
bc67d8a6 | 6458 | /* Compute how many sections might be placed into this segment. */ |
b5f852ea NC |
6459 | for (section = ibfd->sections, section_count = 0; |
6460 | section != NULL; | |
6461 | section = section->next) | |
9f17e2a6 L |
6462 | { |
6463 | /* Find the first section in the input segment, which may be | |
6464 | removed from the corresponding output segment. */ | |
6465 | if (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed)) | |
6466 | { | |
6467 | if (first_section == NULL) | |
6468 | first_section = section; | |
6469 | if (section->output_section != NULL) | |
6470 | ++section_count; | |
6471 | } | |
6472 | } | |
811072d8 | 6473 | |
b5f852ea NC |
6474 | /* Allocate a segment map big enough to contain |
6475 | all of the sections we have selected. */ | |
dc810e39 AM |
6476 | amt = sizeof (struct elf_segment_map); |
6477 | amt += ((bfd_size_type) section_count - 1) * sizeof (asection *); | |
a50b1753 | 6478 | map = (struct elf_segment_map *) bfd_zalloc (obfd, amt); |
bc67d8a6 | 6479 | if (map == NULL) |
b34976b6 | 6480 | return FALSE; |
252b5132 RH |
6481 | |
6482 | /* Initialise the fields of the segment map. Default to | |
6483 | using the physical address of the segment in the input BFD. */ | |
0067a569 AM |
6484 | map->next = NULL; |
6485 | map->p_type = segment->p_type; | |
6486 | map->p_flags = segment->p_flags; | |
bc67d8a6 | 6487 | map->p_flags_valid = 1; |
55d55ac7 | 6488 | |
9f17e2a6 L |
6489 | /* If the first section in the input segment is removed, there is |
6490 | no need to preserve segment physical address in the corresponding | |
6491 | output segment. */ | |
945c025a | 6492 | if (!first_section || first_section->output_section != NULL) |
9f17e2a6 L |
6493 | { |
6494 | map->p_paddr = segment->p_paddr; | |
5c44b38e | 6495 | map->p_paddr_valid = p_paddr_valid; |
9f17e2a6 | 6496 | } |
252b5132 RH |
6497 | |
6498 | /* Determine if this segment contains the ELF file header | |
6499 | and if it contains the program headers themselves. */ | |
bc67d8a6 NC |
6500 | map->includes_filehdr = (segment->p_offset == 0 |
6501 | && segment->p_filesz >= iehdr->e_ehsize); | |
bc67d8a6 | 6502 | map->includes_phdrs = 0; |
252b5132 | 6503 | |
0067a569 | 6504 | if (!phdr_included || segment->p_type != PT_LOAD) |
252b5132 | 6505 | { |
bc67d8a6 NC |
6506 | map->includes_phdrs = |
6507 | (segment->p_offset <= (bfd_vma) iehdr->e_phoff | |
6508 | && (segment->p_offset + segment->p_filesz | |
252b5132 RH |
6509 | >= ((bfd_vma) iehdr->e_phoff |
6510 | + iehdr->e_phnum * iehdr->e_phentsize))); | |
c044fabd | 6511 | |
bc67d8a6 | 6512 | if (segment->p_type == PT_LOAD && map->includes_phdrs) |
b34976b6 | 6513 | phdr_included = TRUE; |
252b5132 RH |
6514 | } |
6515 | ||
bc67d8a6 | 6516 | if (section_count == 0) |
252b5132 RH |
6517 | { |
6518 | /* Special segments, such as the PT_PHDR segment, may contain | |
6519 | no sections, but ordinary, loadable segments should contain | |
1ed89aa9 NC |
6520 | something. They are allowed by the ELF spec however, so only |
6521 | a warning is produced. */ | |
bc67d8a6 | 6522 | if (segment->p_type == PT_LOAD) |
c86934ce NC |
6523 | (*_bfd_error_handler) (_("\ |
6524 | %B: warning: Empty loadable segment detected, is this intentional ?"), | |
0067a569 | 6525 | ibfd); |
252b5132 | 6526 | |
bc67d8a6 | 6527 | map->count = 0; |
c044fabd KH |
6528 | *pointer_to_map = map; |
6529 | pointer_to_map = &map->next; | |
252b5132 RH |
6530 | |
6531 | continue; | |
6532 | } | |
6533 | ||
6534 | /* Now scan the sections in the input BFD again and attempt | |
6535 | to add their corresponding output sections to the segment map. | |
6536 | The problem here is how to handle an output section which has | |
6537 | been moved (ie had its LMA changed). There are four possibilities: | |
6538 | ||
6539 | 1. None of the sections have been moved. | |
6540 | In this case we can continue to use the segment LMA from the | |
6541 | input BFD. | |
6542 | ||
6543 | 2. All of the sections have been moved by the same amount. | |
6544 | In this case we can change the segment's LMA to match the LMA | |
6545 | of the first section. | |
6546 | ||
6547 | 3. Some of the sections have been moved, others have not. | |
6548 | In this case those sections which have not been moved can be | |
6549 | placed in the current segment which will have to have its size, | |
6550 | and possibly its LMA changed, and a new segment or segments will | |
6551 | have to be created to contain the other sections. | |
6552 | ||
b5f852ea | 6553 | 4. The sections have been moved, but not by the same amount. |
252b5132 RH |
6554 | In this case we can change the segment's LMA to match the LMA |
6555 | of the first section and we will have to create a new segment | |
6556 | or segments to contain the other sections. | |
6557 | ||
6558 | In order to save time, we allocate an array to hold the section | |
6559 | pointers that we are interested in. As these sections get assigned | |
6560 | to a segment, they are removed from this array. */ | |
6561 | ||
a50b1753 | 6562 | sections = (asection **) bfd_malloc2 (section_count, sizeof (asection *)); |
252b5132 | 6563 | if (sections == NULL) |
b34976b6 | 6564 | return FALSE; |
252b5132 RH |
6565 | |
6566 | /* Step One: Scan for segment vs section LMA conflicts. | |
6567 | Also add the sections to the section array allocated above. | |
6568 | Also add the sections to the current segment. In the common | |
6569 | case, where the sections have not been moved, this means that | |
6570 | we have completely filled the segment, and there is nothing | |
6571 | more to do. */ | |
252b5132 | 6572 | isec = 0; |
72730e0c | 6573 | matching_lma = 0; |
252b5132 | 6574 | suggested_lma = 0; |
0067a569 AM |
6575 | first_matching_lma = TRUE; |
6576 | first_suggested_lma = TRUE; | |
252b5132 | 6577 | |
461c4b2e | 6578 | for (section = first_section, j = 0; |
bc67d8a6 NC |
6579 | section != NULL; |
6580 | section = section->next) | |
252b5132 | 6581 | { |
caf47ea6 | 6582 | if (INCLUDE_SECTION_IN_SEGMENT (section, segment, bed)) |
c0f7859b | 6583 | { |
bc67d8a6 NC |
6584 | output_section = section->output_section; |
6585 | ||
0067a569 | 6586 | sections[j++] = section; |
252b5132 RH |
6587 | |
6588 | /* The Solaris native linker always sets p_paddr to 0. | |
6589 | We try to catch that case here, and set it to the | |
5e8d7549 NC |
6590 | correct value. Note - some backends require that |
6591 | p_paddr be left as zero. */ | |
5c44b38e | 6592 | if (!p_paddr_valid |
4455705d | 6593 | && segment->p_vaddr != 0 |
0067a569 | 6594 | && !bed->want_p_paddr_set_to_zero |
252b5132 | 6595 | && isec == 0 |
bc67d8a6 | 6596 | && output_section->lma != 0 |
0067a569 AM |
6597 | && output_section->vma == (segment->p_vaddr |
6598 | + (map->includes_filehdr | |
6599 | ? iehdr->e_ehsize | |
6600 | : 0) | |
6601 | + (map->includes_phdrs | |
6602 | ? (iehdr->e_phnum | |
6603 | * iehdr->e_phentsize) | |
6604 | : 0))) | |
bc67d8a6 | 6605 | map->p_paddr = segment->p_vaddr; |
252b5132 RH |
6606 | |
6607 | /* Match up the physical address of the segment with the | |
6608 | LMA address of the output section. */ | |
bc67d8a6 | 6609 | if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr) |
5e8d7549 | 6610 | || IS_COREFILE_NOTE (segment, section) |
0067a569 AM |
6611 | || (bed->want_p_paddr_set_to_zero |
6612 | && IS_CONTAINED_BY_VMA (output_section, segment))) | |
252b5132 | 6613 | { |
0067a569 AM |
6614 | if (first_matching_lma || output_section->lma < matching_lma) |
6615 | { | |
6616 | matching_lma = output_section->lma; | |
6617 | first_matching_lma = FALSE; | |
6618 | } | |
252b5132 RH |
6619 | |
6620 | /* We assume that if the section fits within the segment | |
bc67d8a6 | 6621 | then it does not overlap any other section within that |
252b5132 | 6622 | segment. */ |
0067a569 AM |
6623 | map->sections[isec++] = output_section; |
6624 | } | |
6625 | else if (first_suggested_lma) | |
6626 | { | |
6627 | suggested_lma = output_section->lma; | |
6628 | first_suggested_lma = FALSE; | |
252b5132 | 6629 | } |
147d51c2 L |
6630 | |
6631 | if (j == section_count) | |
6632 | break; | |
252b5132 RH |
6633 | } |
6634 | } | |
6635 | ||
bc67d8a6 | 6636 | BFD_ASSERT (j == section_count); |
252b5132 RH |
6637 | |
6638 | /* Step Two: Adjust the physical address of the current segment, | |
6639 | if necessary. */ | |
bc67d8a6 | 6640 | if (isec == section_count) |
252b5132 RH |
6641 | { |
6642 | /* All of the sections fitted within the segment as currently | |
6643 | specified. This is the default case. Add the segment to | |
6644 | the list of built segments and carry on to process the next | |
6645 | program header in the input BFD. */ | |
bc67d8a6 | 6646 | map->count = section_count; |
c044fabd KH |
6647 | *pointer_to_map = map; |
6648 | pointer_to_map = &map->next; | |
08a40648 | 6649 | |
5c44b38e AM |
6650 | if (p_paddr_valid |
6651 | && !bed->want_p_paddr_set_to_zero | |
147d51c2 | 6652 | && matching_lma != map->p_paddr |
5c44b38e AM |
6653 | && !map->includes_filehdr |
6654 | && !map->includes_phdrs) | |
3271a814 NS |
6655 | /* There is some padding before the first section in the |
6656 | segment. So, we must account for that in the output | |
6657 | segment's vma. */ | |
6658 | map->p_vaddr_offset = matching_lma - map->p_paddr; | |
08a40648 | 6659 | |
252b5132 RH |
6660 | free (sections); |
6661 | continue; | |
6662 | } | |
252b5132 RH |
6663 | else |
6664 | { | |
0067a569 | 6665 | if (!first_matching_lma) |
72730e0c AM |
6666 | { |
6667 | /* At least one section fits inside the current segment. | |
6668 | Keep it, but modify its physical address to match the | |
6669 | LMA of the first section that fitted. */ | |
bc67d8a6 | 6670 | map->p_paddr = matching_lma; |
72730e0c AM |
6671 | } |
6672 | else | |
6673 | { | |
6674 | /* None of the sections fitted inside the current segment. | |
6675 | Change the current segment's physical address to match | |
6676 | the LMA of the first section. */ | |
bc67d8a6 | 6677 | map->p_paddr = suggested_lma; |
72730e0c AM |
6678 | } |
6679 | ||
bc67d8a6 NC |
6680 | /* Offset the segment physical address from the lma |
6681 | to allow for space taken up by elf headers. */ | |
6682 | if (map->includes_filehdr) | |
010c8431 AM |
6683 | { |
6684 | if (map->p_paddr >= iehdr->e_ehsize) | |
6685 | map->p_paddr -= iehdr->e_ehsize; | |
6686 | else | |
6687 | { | |
6688 | map->includes_filehdr = FALSE; | |
6689 | map->includes_phdrs = FALSE; | |
6690 | } | |
6691 | } | |
252b5132 | 6692 | |
bc67d8a6 NC |
6693 | if (map->includes_phdrs) |
6694 | { | |
010c8431 AM |
6695 | if (map->p_paddr >= iehdr->e_phnum * iehdr->e_phentsize) |
6696 | { | |
6697 | map->p_paddr -= iehdr->e_phnum * iehdr->e_phentsize; | |
6698 | ||
6699 | /* iehdr->e_phnum is just an estimate of the number | |
6700 | of program headers that we will need. Make a note | |
6701 | here of the number we used and the segment we chose | |
6702 | to hold these headers, so that we can adjust the | |
6703 | offset when we know the correct value. */ | |
6704 | phdr_adjust_num = iehdr->e_phnum; | |
6705 | phdr_adjust_seg = map; | |
6706 | } | |
6707 | else | |
6708 | map->includes_phdrs = FALSE; | |
bc67d8a6 | 6709 | } |
252b5132 RH |
6710 | } |
6711 | ||
6712 | /* Step Three: Loop over the sections again, this time assigning | |
caf47ea6 | 6713 | those that fit to the current segment and removing them from the |
252b5132 RH |
6714 | sections array; but making sure not to leave large gaps. Once all |
6715 | possible sections have been assigned to the current segment it is | |
6716 | added to the list of built segments and if sections still remain | |
6717 | to be assigned, a new segment is constructed before repeating | |
6718 | the loop. */ | |
6719 | isec = 0; | |
6720 | do | |
6721 | { | |
bc67d8a6 | 6722 | map->count = 0; |
252b5132 | 6723 | suggested_lma = 0; |
0067a569 | 6724 | first_suggested_lma = TRUE; |
252b5132 RH |
6725 | |
6726 | /* Fill the current segment with sections that fit. */ | |
bc67d8a6 | 6727 | for (j = 0; j < section_count; j++) |
252b5132 | 6728 | { |
bc67d8a6 | 6729 | section = sections[j]; |
252b5132 | 6730 | |
bc67d8a6 | 6731 | if (section == NULL) |
252b5132 RH |
6732 | continue; |
6733 | ||
bc67d8a6 | 6734 | output_section = section->output_section; |
252b5132 | 6735 | |
bc67d8a6 | 6736 | BFD_ASSERT (output_section != NULL); |
c044fabd | 6737 | |
bc67d8a6 NC |
6738 | if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr) |
6739 | || IS_COREFILE_NOTE (segment, section)) | |
252b5132 | 6740 | { |
bc67d8a6 | 6741 | if (map->count == 0) |
252b5132 RH |
6742 | { |
6743 | /* If the first section in a segment does not start at | |
bc67d8a6 NC |
6744 | the beginning of the segment, then something is |
6745 | wrong. */ | |
0067a569 AM |
6746 | if (output_section->lma |
6747 | != (map->p_paddr | |
6748 | + (map->includes_filehdr ? iehdr->e_ehsize : 0) | |
6749 | + (map->includes_phdrs | |
6750 | ? iehdr->e_phnum * iehdr->e_phentsize | |
6751 | : 0))) | |
252b5132 RH |
6752 | abort (); |
6753 | } | |
6754 | else | |
6755 | { | |
0067a569 | 6756 | asection *prev_sec; |
252b5132 | 6757 | |
bc67d8a6 | 6758 | prev_sec = map->sections[map->count - 1]; |
252b5132 RH |
6759 | |
6760 | /* If the gap between the end of the previous section | |
bc67d8a6 NC |
6761 | and the start of this section is more than |
6762 | maxpagesize then we need to start a new segment. */ | |
eea6121a | 6763 | if ((BFD_ALIGN (prev_sec->lma + prev_sec->size, |
079e9a2f | 6764 | maxpagesize) |
caf47ea6 | 6765 | < BFD_ALIGN (output_section->lma, maxpagesize)) |
0067a569 | 6766 | || (prev_sec->lma + prev_sec->size |
079e9a2f | 6767 | > output_section->lma)) |
252b5132 | 6768 | { |
0067a569 AM |
6769 | if (first_suggested_lma) |
6770 | { | |
6771 | suggested_lma = output_section->lma; | |
6772 | first_suggested_lma = FALSE; | |
6773 | } | |
252b5132 RH |
6774 | |
6775 | continue; | |
6776 | } | |
6777 | } | |
6778 | ||
bc67d8a6 | 6779 | map->sections[map->count++] = output_section; |
252b5132 RH |
6780 | ++isec; |
6781 | sections[j] = NULL; | |
b34976b6 | 6782 | section->segment_mark = TRUE; |
252b5132 | 6783 | } |
0067a569 AM |
6784 | else if (first_suggested_lma) |
6785 | { | |
6786 | suggested_lma = output_section->lma; | |
6787 | first_suggested_lma = FALSE; | |
6788 | } | |
252b5132 RH |
6789 | } |
6790 | ||
bc67d8a6 | 6791 | BFD_ASSERT (map->count > 0); |
252b5132 RH |
6792 | |
6793 | /* Add the current segment to the list of built segments. */ | |
c044fabd KH |
6794 | *pointer_to_map = map; |
6795 | pointer_to_map = &map->next; | |
252b5132 | 6796 | |
bc67d8a6 | 6797 | if (isec < section_count) |
252b5132 RH |
6798 | { |
6799 | /* We still have not allocated all of the sections to | |
6800 | segments. Create a new segment here, initialise it | |
6801 | and carry on looping. */ | |
dc810e39 AM |
6802 | amt = sizeof (struct elf_segment_map); |
6803 | amt += ((bfd_size_type) section_count - 1) * sizeof (asection *); | |
5964fc3a | 6804 | map = (struct elf_segment_map *) bfd_zalloc (obfd, amt); |
bc67d8a6 | 6805 | if (map == NULL) |
5ed6aba4 NC |
6806 | { |
6807 | free (sections); | |
6808 | return FALSE; | |
6809 | } | |
252b5132 RH |
6810 | |
6811 | /* Initialise the fields of the segment map. Set the physical | |
6812 | physical address to the LMA of the first section that has | |
6813 | not yet been assigned. */ | |
0067a569 AM |
6814 | map->next = NULL; |
6815 | map->p_type = segment->p_type; | |
6816 | map->p_flags = segment->p_flags; | |
6817 | map->p_flags_valid = 1; | |
6818 | map->p_paddr = suggested_lma; | |
5c44b38e | 6819 | map->p_paddr_valid = p_paddr_valid; |
bc67d8a6 | 6820 | map->includes_filehdr = 0; |
0067a569 | 6821 | map->includes_phdrs = 0; |
252b5132 RH |
6822 | } |
6823 | } | |
bc67d8a6 | 6824 | while (isec < section_count); |
252b5132 RH |
6825 | |
6826 | free (sections); | |
6827 | } | |
6828 | ||
12bd6957 | 6829 | elf_seg_map (obfd) = map_first; |
bc67d8a6 NC |
6830 | |
6831 | /* If we had to estimate the number of program headers that were | |
9ad5cbcf | 6832 | going to be needed, then check our estimate now and adjust |
bc67d8a6 NC |
6833 | the offset if necessary. */ |
6834 | if (phdr_adjust_seg != NULL) | |
6835 | { | |
6836 | unsigned int count; | |
c044fabd | 6837 | |
bc67d8a6 | 6838 | for (count = 0, map = map_first; map != NULL; map = map->next) |
c044fabd | 6839 | count++; |
252b5132 | 6840 | |
bc67d8a6 NC |
6841 | if (count > phdr_adjust_num) |
6842 | phdr_adjust_seg->p_paddr | |
6843 | -= (count - phdr_adjust_num) * iehdr->e_phentsize; | |
6844 | } | |
c044fabd | 6845 | |
bc67d8a6 | 6846 | #undef SEGMENT_END |
eecdbe52 | 6847 | #undef SECTION_SIZE |
bc67d8a6 NC |
6848 | #undef IS_CONTAINED_BY_VMA |
6849 | #undef IS_CONTAINED_BY_LMA | |
0efc80c8 | 6850 | #undef IS_NOTE |
252b5132 | 6851 | #undef IS_COREFILE_NOTE |
bc67d8a6 | 6852 | #undef IS_SOLARIS_PT_INTERP |
9f17e2a6 | 6853 | #undef IS_SECTION_IN_INPUT_SEGMENT |
bc67d8a6 NC |
6854 | #undef INCLUDE_SECTION_IN_SEGMENT |
6855 | #undef SEGMENT_AFTER_SEGMENT | |
6856 | #undef SEGMENT_OVERLAPS | |
b34976b6 | 6857 | return TRUE; |
252b5132 RH |
6858 | } |
6859 | ||
84d1d650 L |
6860 | /* Copy ELF program header information. */ |
6861 | ||
6862 | static bfd_boolean | |
6863 | copy_elf_program_header (bfd *ibfd, bfd *obfd) | |
6864 | { | |
6865 | Elf_Internal_Ehdr *iehdr; | |
6866 | struct elf_segment_map *map; | |
6867 | struct elf_segment_map *map_first; | |
6868 | struct elf_segment_map **pointer_to_map; | |
6869 | Elf_Internal_Phdr *segment; | |
6870 | unsigned int i; | |
6871 | unsigned int num_segments; | |
6872 | bfd_boolean phdr_included = FALSE; | |
88967714 | 6873 | bfd_boolean p_paddr_valid; |
84d1d650 L |
6874 | |
6875 | iehdr = elf_elfheader (ibfd); | |
6876 | ||
6877 | map_first = NULL; | |
6878 | pointer_to_map = &map_first; | |
6879 | ||
88967714 AM |
6880 | /* If all the segment p_paddr fields are zero, don't set |
6881 | map->p_paddr_valid. */ | |
6882 | p_paddr_valid = FALSE; | |
84d1d650 | 6883 | num_segments = elf_elfheader (ibfd)->e_phnum; |
88967714 AM |
6884 | for (i = 0, segment = elf_tdata (ibfd)->phdr; |
6885 | i < num_segments; | |
6886 | i++, segment++) | |
6887 | if (segment->p_paddr != 0) | |
6888 | { | |
6889 | p_paddr_valid = TRUE; | |
6890 | break; | |
6891 | } | |
6892 | ||
84d1d650 L |
6893 | for (i = 0, segment = elf_tdata (ibfd)->phdr; |
6894 | i < num_segments; | |
6895 | i++, segment++) | |
6896 | { | |
6897 | asection *section; | |
6898 | unsigned int section_count; | |
6899 | bfd_size_type amt; | |
6900 | Elf_Internal_Shdr *this_hdr; | |
53020534 | 6901 | asection *first_section = NULL; |
a76e6f2f | 6902 | asection *lowest_section; |
84d1d650 | 6903 | |
84d1d650 L |
6904 | /* Compute how many sections are in this segment. */ |
6905 | for (section = ibfd->sections, section_count = 0; | |
6906 | section != NULL; | |
6907 | section = section->next) | |
6908 | { | |
6909 | this_hdr = &(elf_section_data(section)->this_hdr); | |
f4638467 | 6910 | if (ELF_SECTION_IN_SEGMENT (this_hdr, segment)) |
3271a814 | 6911 | { |
a76e6f2f AM |
6912 | if (first_section == NULL) |
6913 | first_section = section; | |
3271a814 NS |
6914 | section_count++; |
6915 | } | |
84d1d650 L |
6916 | } |
6917 | ||
6918 | /* Allocate a segment map big enough to contain | |
6919 | all of the sections we have selected. */ | |
6920 | amt = sizeof (struct elf_segment_map); | |
6921 | if (section_count != 0) | |
6922 | amt += ((bfd_size_type) section_count - 1) * sizeof (asection *); | |
a50b1753 | 6923 | map = (struct elf_segment_map *) bfd_zalloc (obfd, amt); |
84d1d650 L |
6924 | if (map == NULL) |
6925 | return FALSE; | |
6926 | ||
6927 | /* Initialize the fields of the output segment map with the | |
6928 | input segment. */ | |
6929 | map->next = NULL; | |
6930 | map->p_type = segment->p_type; | |
6931 | map->p_flags = segment->p_flags; | |
6932 | map->p_flags_valid = 1; | |
6933 | map->p_paddr = segment->p_paddr; | |
88967714 | 6934 | map->p_paddr_valid = p_paddr_valid; |
3f570048 AM |
6935 | map->p_align = segment->p_align; |
6936 | map->p_align_valid = 1; | |
3271a814 | 6937 | map->p_vaddr_offset = 0; |
84d1d650 | 6938 | |
04c3a755 NS |
6939 | if (map->p_type == PT_GNU_RELRO |
6940 | || map->p_type == PT_GNU_STACK) | |
b10a8ae0 L |
6941 | { |
6942 | /* The PT_GNU_RELRO segment may contain the first a few | |
6943 | bytes in the .got.plt section even if the whole .got.plt | |
6944 | section isn't in the PT_GNU_RELRO segment. We won't | |
04c3a755 NS |
6945 | change the size of the PT_GNU_RELRO segment. |
6946 | Similarly, PT_GNU_STACK size is significant on uclinux | |
6947 | systems. */ | |
9433b9b1 | 6948 | map->p_size = segment->p_memsz; |
b10a8ae0 L |
6949 | map->p_size_valid = 1; |
6950 | } | |
6951 | ||
84d1d650 L |
6952 | /* Determine if this segment contains the ELF file header |
6953 | and if it contains the program headers themselves. */ | |
6954 | map->includes_filehdr = (segment->p_offset == 0 | |
6955 | && segment->p_filesz >= iehdr->e_ehsize); | |
6956 | ||
6957 | map->includes_phdrs = 0; | |
6958 | if (! phdr_included || segment->p_type != PT_LOAD) | |
6959 | { | |
6960 | map->includes_phdrs = | |
6961 | (segment->p_offset <= (bfd_vma) iehdr->e_phoff | |
6962 | && (segment->p_offset + segment->p_filesz | |
6963 | >= ((bfd_vma) iehdr->e_phoff | |
6964 | + iehdr->e_phnum * iehdr->e_phentsize))); | |
6965 | ||
6966 | if (segment->p_type == PT_LOAD && map->includes_phdrs) | |
6967 | phdr_included = TRUE; | |
6968 | } | |
6969 | ||
bbefd0a9 | 6970 | lowest_section = NULL; |
84d1d650 L |
6971 | if (section_count != 0) |
6972 | { | |
6973 | unsigned int isec = 0; | |
6974 | ||
53020534 | 6975 | for (section = first_section; |
84d1d650 L |
6976 | section != NULL; |
6977 | section = section->next) | |
6978 | { | |
6979 | this_hdr = &(elf_section_data(section)->this_hdr); | |
f4638467 | 6980 | if (ELF_SECTION_IN_SEGMENT (this_hdr, segment)) |
53020534 L |
6981 | { |
6982 | map->sections[isec++] = section->output_section; | |
a76e6f2f AM |
6983 | if ((section->flags & SEC_ALLOC) != 0) |
6984 | { | |
6985 | bfd_vma seg_off; | |
6986 | ||
bbefd0a9 AM |
6987 | if (lowest_section == NULL |
6988 | || section->lma < lowest_section->lma) | |
fb8a5684 AM |
6989 | lowest_section = section; |
6990 | ||
a76e6f2f AM |
6991 | /* Section lmas are set up from PT_LOAD header |
6992 | p_paddr in _bfd_elf_make_section_from_shdr. | |
6993 | If this header has a p_paddr that disagrees | |
6994 | with the section lma, flag the p_paddr as | |
6995 | invalid. */ | |
6996 | if ((section->flags & SEC_LOAD) != 0) | |
6997 | seg_off = this_hdr->sh_offset - segment->p_offset; | |
6998 | else | |
6999 | seg_off = this_hdr->sh_addr - segment->p_vaddr; | |
7000 | if (section->lma - segment->p_paddr != seg_off) | |
7001 | map->p_paddr_valid = FALSE; | |
7002 | } | |
53020534 L |
7003 | if (isec == section_count) |
7004 | break; | |
7005 | } | |
84d1d650 L |
7006 | } |
7007 | } | |
7008 | ||
a76e6f2f AM |
7009 | if (map->includes_filehdr && lowest_section != NULL) |
7010 | /* We need to keep the space used by the headers fixed. */ | |
7011 | map->header_size = lowest_section->vma - segment->p_vaddr; | |
d324f6d6 | 7012 | |
a76e6f2f AM |
7013 | if (!map->includes_phdrs |
7014 | && !map->includes_filehdr | |
7015 | && map->p_paddr_valid) | |
7016 | /* There is some other padding before the first section. */ | |
7017 | map->p_vaddr_offset = ((lowest_section ? lowest_section->lma : 0) | |
7018 | - segment->p_paddr); | |
7019 | ||
84d1d650 L |
7020 | map->count = section_count; |
7021 | *pointer_to_map = map; | |
7022 | pointer_to_map = &map->next; | |
7023 | } | |
7024 | ||
12bd6957 | 7025 | elf_seg_map (obfd) = map_first; |
84d1d650 L |
7026 | return TRUE; |
7027 | } | |
7028 | ||
7029 | /* Copy private BFD data. This copies or rewrites ELF program header | |
7030 | information. */ | |
7031 | ||
7032 | static bfd_boolean | |
7033 | copy_private_bfd_data (bfd *ibfd, bfd *obfd) | |
7034 | { | |
84d1d650 L |
7035 | if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour |
7036 | || bfd_get_flavour (obfd) != bfd_target_elf_flavour) | |
7037 | return TRUE; | |
7038 | ||
7039 | if (elf_tdata (ibfd)->phdr == NULL) | |
7040 | return TRUE; | |
7041 | ||
7042 | if (ibfd->xvec == obfd->xvec) | |
7043 | { | |
cb3ff1e5 NC |
7044 | /* Check to see if any sections in the input BFD |
7045 | covered by ELF program header have changed. */ | |
d55ce4e2 | 7046 | Elf_Internal_Phdr *segment; |
84d1d650 L |
7047 | asection *section, *osec; |
7048 | unsigned int i, num_segments; | |
7049 | Elf_Internal_Shdr *this_hdr; | |
147d51c2 L |
7050 | const struct elf_backend_data *bed; |
7051 | ||
7052 | bed = get_elf_backend_data (ibfd); | |
7053 | ||
7054 | /* Regenerate the segment map if p_paddr is set to 0. */ | |
7055 | if (bed->want_p_paddr_set_to_zero) | |
7056 | goto rewrite; | |
84d1d650 L |
7057 | |
7058 | /* Initialize the segment mark field. */ | |
7059 | for (section = obfd->sections; section != NULL; | |
7060 | section = section->next) | |
7061 | section->segment_mark = FALSE; | |
7062 | ||
7063 | num_segments = elf_elfheader (ibfd)->e_phnum; | |
7064 | for (i = 0, segment = elf_tdata (ibfd)->phdr; | |
7065 | i < num_segments; | |
7066 | i++, segment++) | |
7067 | { | |
5f6999aa NC |
7068 | /* PR binutils/3535. The Solaris linker always sets the p_paddr |
7069 | and p_memsz fields of special segments (DYNAMIC, INTERP) to 0 | |
7070 | which severly confuses things, so always regenerate the segment | |
7071 | map in this case. */ | |
7072 | if (segment->p_paddr == 0 | |
7073 | && segment->p_memsz == 0 | |
7074 | && (segment->p_type == PT_INTERP || segment->p_type == PT_DYNAMIC)) | |
cb3ff1e5 | 7075 | goto rewrite; |
5f6999aa | 7076 | |
84d1d650 L |
7077 | for (section = ibfd->sections; |
7078 | section != NULL; section = section->next) | |
7079 | { | |
7080 | /* We mark the output section so that we know it comes | |
7081 | from the input BFD. */ | |
7082 | osec = section->output_section; | |
7083 | if (osec) | |
7084 | osec->segment_mark = TRUE; | |
7085 | ||
7086 | /* Check if this section is covered by the segment. */ | |
7087 | this_hdr = &(elf_section_data(section)->this_hdr); | |
f4638467 | 7088 | if (ELF_SECTION_IN_SEGMENT (this_hdr, segment)) |
84d1d650 L |
7089 | { |
7090 | /* FIXME: Check if its output section is changed or | |
7091 | removed. What else do we need to check? */ | |
7092 | if (osec == NULL | |
7093 | || section->flags != osec->flags | |
7094 | || section->lma != osec->lma | |
7095 | || section->vma != osec->vma | |
7096 | || section->size != osec->size | |
7097 | || section->rawsize != osec->rawsize | |
7098 | || section->alignment_power != osec->alignment_power) | |
7099 | goto rewrite; | |
7100 | } | |
7101 | } | |
7102 | } | |
7103 | ||
cb3ff1e5 | 7104 | /* Check to see if any output section do not come from the |
84d1d650 L |
7105 | input BFD. */ |
7106 | for (section = obfd->sections; section != NULL; | |
7107 | section = section->next) | |
7108 | { | |
7109 | if (section->segment_mark == FALSE) | |
7110 | goto rewrite; | |
7111 | else | |
7112 | section->segment_mark = FALSE; | |
7113 | } | |
7114 | ||
7115 | return copy_elf_program_header (ibfd, obfd); | |
7116 | } | |
7117 | ||
7118 | rewrite: | |
f1d85785 L |
7119 | if (ibfd->xvec == obfd->xvec) |
7120 | { | |
7121 | /* When rewriting program header, set the output maxpagesize to | |
7122 | the maximum alignment of input PT_LOAD segments. */ | |
7123 | Elf_Internal_Phdr *segment; | |
7124 | unsigned int i; | |
7125 | unsigned int num_segments = elf_elfheader (ibfd)->e_phnum; | |
7126 | bfd_vma maxpagesize = 0; | |
7127 | ||
7128 | for (i = 0, segment = elf_tdata (ibfd)->phdr; | |
7129 | i < num_segments; | |
7130 | i++, segment++) | |
7131 | if (segment->p_type == PT_LOAD | |
7132 | && maxpagesize < segment->p_align) | |
c86934ce NC |
7133 | { |
7134 | /* PR 17512: file: f17299af. */ | |
7135 | if (segment->p_align > (bfd_vma) 1 << ((sizeof (bfd_vma) * 8) - 2)) | |
7136 | (*_bfd_error_handler) (_("\ | |
7137 | %B: warning: segment alignment of 0x%llx is too large"), | |
7138 | ibfd, (long long) segment->p_align); | |
7139 | else | |
7140 | maxpagesize = segment->p_align; | |
7141 | } | |
f1d85785 L |
7142 | |
7143 | if (maxpagesize != get_elf_backend_data (obfd)->maxpagesize) | |
7144 | bfd_emul_set_maxpagesize (bfd_get_target (obfd), maxpagesize); | |
7145 | } | |
7146 | ||
84d1d650 L |
7147 | return rewrite_elf_program_header (ibfd, obfd); |
7148 | } | |
7149 | ||
ccd2ec6a L |
7150 | /* Initialize private output section information from input section. */ |
7151 | ||
7152 | bfd_boolean | |
7153 | _bfd_elf_init_private_section_data (bfd *ibfd, | |
7154 | asection *isec, | |
7155 | bfd *obfd, | |
7156 | asection *osec, | |
7157 | struct bfd_link_info *link_info) | |
7158 | ||
7159 | { | |
7160 | Elf_Internal_Shdr *ihdr, *ohdr; | |
0e1862bb L |
7161 | bfd_boolean final_link = (link_info != NULL |
7162 | && !bfd_link_relocatable (link_info)); | |
ccd2ec6a L |
7163 | |
7164 | if (ibfd->xvec->flavour != bfd_target_elf_flavour | |
7165 | || obfd->xvec->flavour != bfd_target_elf_flavour) | |
7166 | return TRUE; | |
7167 | ||
ba85c43e NC |
7168 | BFD_ASSERT (elf_section_data (osec) != NULL); |
7169 | ||
dfa7b0b8 AM |
7170 | /* For objcopy and relocatable link, don't copy the output ELF |
7171 | section type from input if the output BFD section flags have been | |
7172 | set to something different. For a final link allow some flags | |
7173 | that the linker clears to differ. */ | |
42bb2e33 | 7174 | if (elf_section_type (osec) == SHT_NULL |
dfa7b0b8 AM |
7175 | && (osec->flags == isec->flags |
7176 | || (final_link | |
7177 | && ((osec->flags ^ isec->flags) | |
0814be7d | 7178 | & ~(SEC_LINK_ONCE | SEC_LINK_DUPLICATES | SEC_RELOC)) == 0))) |
42bb2e33 | 7179 | elf_section_type (osec) = elf_section_type (isec); |
d270463e L |
7180 | |
7181 | /* FIXME: Is this correct for all OS/PROC specific flags? */ | |
7182 | elf_section_flags (osec) |= (elf_section_flags (isec) | |
7183 | & (SHF_MASKOS | SHF_MASKPROC)); | |
ccd2ec6a L |
7184 | |
7185 | /* Set things up for objcopy and relocatable link. The output | |
7186 | SHT_GROUP section will have its elf_next_in_group pointing back | |
7187 | to the input group members. Ignore linker created group section. | |
7188 | See elfNN_ia64_object_p in elfxx-ia64.c. */ | |
dfa7b0b8 | 7189 | if (!final_link) |
ccd2ec6a L |
7190 | { |
7191 | if (elf_sec_group (isec) == NULL | |
7192 | || (elf_sec_group (isec)->flags & SEC_LINKER_CREATED) == 0) | |
7193 | { | |
7194 | if (elf_section_flags (isec) & SHF_GROUP) | |
7195 | elf_section_flags (osec) |= SHF_GROUP; | |
7196 | elf_next_in_group (osec) = elf_next_in_group (isec); | |
9659de1c | 7197 | elf_section_data (osec)->group = elf_section_data (isec)->group; |
ccd2ec6a | 7198 | } |
68f5ff89 L |
7199 | |
7200 | /* If not decompress, preserve SHF_COMPRESSED. */ | |
7201 | if ((ibfd->flags & BFD_DECOMPRESS) == 0) | |
7202 | elf_section_flags (osec) |= (elf_section_flags (isec) | |
7203 | & SHF_COMPRESSED); | |
ccd2ec6a L |
7204 | } |
7205 | ||
7206 | ihdr = &elf_section_data (isec)->this_hdr; | |
7207 | ||
7208 | /* We need to handle elf_linked_to_section for SHF_LINK_ORDER. We | |
7209 | don't use the output section of the linked-to section since it | |
7210 | may be NULL at this point. */ | |
7211 | if ((ihdr->sh_flags & SHF_LINK_ORDER) != 0) | |
7212 | { | |
7213 | ohdr = &elf_section_data (osec)->this_hdr; | |
7214 | ohdr->sh_flags |= SHF_LINK_ORDER; | |
7215 | elf_linked_to_section (osec) = elf_linked_to_section (isec); | |
7216 | } | |
7217 | ||
7218 | osec->use_rela_p = isec->use_rela_p; | |
7219 | ||
7220 | return TRUE; | |
7221 | } | |
7222 | ||
252b5132 RH |
7223 | /* Copy private section information. This copies over the entsize |
7224 | field, and sometimes the info field. */ | |
7225 | ||
b34976b6 | 7226 | bfd_boolean |
217aa764 AM |
7227 | _bfd_elf_copy_private_section_data (bfd *ibfd, |
7228 | asection *isec, | |
7229 | bfd *obfd, | |
7230 | asection *osec) | |
252b5132 RH |
7231 | { |
7232 | Elf_Internal_Shdr *ihdr, *ohdr; | |
7233 | ||
7234 | if (ibfd->xvec->flavour != bfd_target_elf_flavour | |
7235 | || obfd->xvec->flavour != bfd_target_elf_flavour) | |
b34976b6 | 7236 | return TRUE; |
252b5132 | 7237 | |
252b5132 RH |
7238 | ihdr = &elf_section_data (isec)->this_hdr; |
7239 | ohdr = &elf_section_data (osec)->this_hdr; | |
7240 | ||
7241 | ohdr->sh_entsize = ihdr->sh_entsize; | |
7242 | ||
7243 | if (ihdr->sh_type == SHT_SYMTAB | |
7244 | || ihdr->sh_type == SHT_DYNSYM | |
7245 | || ihdr->sh_type == SHT_GNU_verneed | |
7246 | || ihdr->sh_type == SHT_GNU_verdef) | |
7247 | ohdr->sh_info = ihdr->sh_info; | |
7248 | ||
ccd2ec6a L |
7249 | return _bfd_elf_init_private_section_data (ibfd, isec, obfd, osec, |
7250 | NULL); | |
252b5132 RH |
7251 | } |
7252 | ||
d0bf826b AM |
7253 | /* Look at all the SHT_GROUP sections in IBFD, making any adjustments |
7254 | necessary if we are removing either the SHT_GROUP section or any of | |
7255 | the group member sections. DISCARDED is the value that a section's | |
7256 | output_section has if the section will be discarded, NULL when this | |
7257 | function is called from objcopy, bfd_abs_section_ptr when called | |
7258 | from the linker. */ | |
80fccad2 BW |
7259 | |
7260 | bfd_boolean | |
d0bf826b | 7261 | _bfd_elf_fixup_group_sections (bfd *ibfd, asection *discarded) |
80fccad2 | 7262 | { |
30288845 AM |
7263 | asection *isec; |
7264 | ||
30288845 | 7265 | for (isec = ibfd->sections; isec != NULL; isec = isec->next) |
415f38a6 | 7266 | if (elf_section_type (isec) == SHT_GROUP) |
30288845 AM |
7267 | { |
7268 | asection *first = elf_next_in_group (isec); | |
7269 | asection *s = first; | |
d0bf826b AM |
7270 | bfd_size_type removed = 0; |
7271 | ||
30288845 AM |
7272 | while (s != NULL) |
7273 | { | |
415f38a6 AM |
7274 | /* If this member section is being output but the |
7275 | SHT_GROUP section is not, then clear the group info | |
7276 | set up by _bfd_elf_copy_private_section_data. */ | |
d0bf826b AM |
7277 | if (s->output_section != discarded |
7278 | && isec->output_section == discarded) | |
30288845 AM |
7279 | { |
7280 | elf_section_flags (s->output_section) &= ~SHF_GROUP; | |
7281 | elf_group_name (s->output_section) = NULL; | |
7282 | } | |
415f38a6 AM |
7283 | /* Conversely, if the member section is not being output |
7284 | but the SHT_GROUP section is, then adjust its size. */ | |
d0bf826b AM |
7285 | else if (s->output_section == discarded |
7286 | && isec->output_section != discarded) | |
7287 | removed += 4; | |
30288845 AM |
7288 | s = elf_next_in_group (s); |
7289 | if (s == first) | |
7290 | break; | |
7291 | } | |
d0bf826b AM |
7292 | if (removed != 0) |
7293 | { | |
7294 | if (discarded != NULL) | |
7295 | { | |
7296 | /* If we've been called for ld -r, then we need to | |
7297 | adjust the input section size. This function may | |
7298 | be called multiple times, so save the original | |
7299 | size. */ | |
7300 | if (isec->rawsize == 0) | |
7301 | isec->rawsize = isec->size; | |
7302 | isec->size = isec->rawsize - removed; | |
7303 | } | |
7304 | else | |
7305 | { | |
7306 | /* Adjust the output section size when called from | |
7307 | objcopy. */ | |
7308 | isec->output_section->size -= removed; | |
7309 | } | |
7310 | } | |
30288845 AM |
7311 | } |
7312 | ||
80fccad2 BW |
7313 | return TRUE; |
7314 | } | |
7315 | ||
d0bf826b AM |
7316 | /* Copy private header information. */ |
7317 | ||
7318 | bfd_boolean | |
7319 | _bfd_elf_copy_private_header_data (bfd *ibfd, bfd *obfd) | |
7320 | { | |
7321 | if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour | |
7322 | || bfd_get_flavour (obfd) != bfd_target_elf_flavour) | |
7323 | return TRUE; | |
7324 | ||
7325 | /* Copy over private BFD data if it has not already been copied. | |
7326 | This must be done here, rather than in the copy_private_bfd_data | |
7327 | entry point, because the latter is called after the section | |
7328 | contents have been set, which means that the program headers have | |
7329 | already been worked out. */ | |
12bd6957 | 7330 | if (elf_seg_map (obfd) == NULL && elf_tdata (ibfd)->phdr != NULL) |
d0bf826b AM |
7331 | { |
7332 | if (! copy_private_bfd_data (ibfd, obfd)) | |
7333 | return FALSE; | |
7334 | } | |
7335 | ||
7336 | return _bfd_elf_fixup_group_sections (ibfd, NULL); | |
7337 | } | |
7338 | ||
252b5132 RH |
7339 | /* Copy private symbol information. If this symbol is in a section |
7340 | which we did not map into a BFD section, try to map the section | |
7341 | index correctly. We use special macro definitions for the mapped | |
7342 | section indices; these definitions are interpreted by the | |
7343 | swap_out_syms function. */ | |
7344 | ||
9ad5cbcf AM |
7345 | #define MAP_ONESYMTAB (SHN_HIOS + 1) |
7346 | #define MAP_DYNSYMTAB (SHN_HIOS + 2) | |
7347 | #define MAP_STRTAB (SHN_HIOS + 3) | |
7348 | #define MAP_SHSTRTAB (SHN_HIOS + 4) | |
7349 | #define MAP_SYM_SHNDX (SHN_HIOS + 5) | |
252b5132 | 7350 | |
b34976b6 | 7351 | bfd_boolean |
217aa764 AM |
7352 | _bfd_elf_copy_private_symbol_data (bfd *ibfd, |
7353 | asymbol *isymarg, | |
7354 | bfd *obfd, | |
7355 | asymbol *osymarg) | |
252b5132 RH |
7356 | { |
7357 | elf_symbol_type *isym, *osym; | |
7358 | ||
7359 | if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour | |
7360 | || bfd_get_flavour (obfd) != bfd_target_elf_flavour) | |
b34976b6 | 7361 | return TRUE; |
252b5132 RH |
7362 | |
7363 | isym = elf_symbol_from (ibfd, isymarg); | |
7364 | osym = elf_symbol_from (obfd, osymarg); | |
7365 | ||
7366 | if (isym != NULL | |
8424d8f5 | 7367 | && isym->internal_elf_sym.st_shndx != 0 |
252b5132 RH |
7368 | && osym != NULL |
7369 | && bfd_is_abs_section (isym->symbol.section)) | |
7370 | { | |
7371 | unsigned int shndx; | |
7372 | ||
7373 | shndx = isym->internal_elf_sym.st_shndx; | |
7374 | if (shndx == elf_onesymtab (ibfd)) | |
7375 | shndx = MAP_ONESYMTAB; | |
7376 | else if (shndx == elf_dynsymtab (ibfd)) | |
7377 | shndx = MAP_DYNSYMTAB; | |
12bd6957 | 7378 | else if (shndx == elf_strtab_sec (ibfd)) |
252b5132 | 7379 | shndx = MAP_STRTAB; |
12bd6957 | 7380 | else if (shndx == elf_shstrtab_sec (ibfd)) |
252b5132 | 7381 | shndx = MAP_SHSTRTAB; |
6a40cf0c | 7382 | else if (find_section_in_list (shndx, elf_symtab_shndx_list (ibfd))) |
9ad5cbcf | 7383 | shndx = MAP_SYM_SHNDX; |
252b5132 RH |
7384 | osym->internal_elf_sym.st_shndx = shndx; |
7385 | } | |
7386 | ||
b34976b6 | 7387 | return TRUE; |
252b5132 RH |
7388 | } |
7389 | ||
7390 | /* Swap out the symbols. */ | |
7391 | ||
b34976b6 | 7392 | static bfd_boolean |
217aa764 | 7393 | swap_out_syms (bfd *abfd, |
ef10c3ac | 7394 | struct elf_strtab_hash **sttp, |
217aa764 | 7395 | int relocatable_p) |
252b5132 | 7396 | { |
9c5bfbb7 | 7397 | const struct elf_backend_data *bed; |
079e9a2f AM |
7398 | int symcount; |
7399 | asymbol **syms; | |
ef10c3ac | 7400 | struct elf_strtab_hash *stt; |
079e9a2f | 7401 | Elf_Internal_Shdr *symtab_hdr; |
9ad5cbcf | 7402 | Elf_Internal_Shdr *symtab_shndx_hdr; |
079e9a2f | 7403 | Elf_Internal_Shdr *symstrtab_hdr; |
ef10c3ac | 7404 | struct elf_sym_strtab *symstrtab; |
f075ee0c AM |
7405 | bfd_byte *outbound_syms; |
7406 | bfd_byte *outbound_shndx; | |
ef10c3ac L |
7407 | unsigned long outbound_syms_index; |
7408 | unsigned long outbound_shndx_index; | |
079e9a2f | 7409 | int idx; |
12bd6957 | 7410 | unsigned int num_locals; |
079e9a2f | 7411 | bfd_size_type amt; |
174fd7f9 | 7412 | bfd_boolean name_local_sections; |
252b5132 | 7413 | |
12bd6957 | 7414 | if (!elf_map_symbols (abfd, &num_locals)) |
b34976b6 | 7415 | return FALSE; |
252b5132 | 7416 | |
c044fabd | 7417 | /* Dump out the symtabs. */ |
ef10c3ac | 7418 | stt = _bfd_elf_strtab_init (); |
079e9a2f | 7419 | if (stt == NULL) |
b34976b6 | 7420 | return FALSE; |
252b5132 | 7421 | |
079e9a2f AM |
7422 | bed = get_elf_backend_data (abfd); |
7423 | symcount = bfd_get_symcount (abfd); | |
7424 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; | |
7425 | symtab_hdr->sh_type = SHT_SYMTAB; | |
7426 | symtab_hdr->sh_entsize = bed->s->sizeof_sym; | |
7427 | symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1); | |
12bd6957 | 7428 | symtab_hdr->sh_info = num_locals + 1; |
72de5009 | 7429 | symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align; |
079e9a2f AM |
7430 | |
7431 | symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr; | |
7432 | symstrtab_hdr->sh_type = SHT_STRTAB; | |
7433 | ||
ef10c3ac L |
7434 | /* Allocate buffer to swap out the .strtab section. */ |
7435 | symstrtab = (struct elf_sym_strtab *) bfd_malloc ((symcount + 1) | |
7436 | * sizeof (*symstrtab)); | |
7437 | if (symstrtab == NULL) | |
7438 | { | |
7439 | _bfd_elf_strtab_free (stt); | |
7440 | return FALSE; | |
7441 | } | |
7442 | ||
a50b1753 NC |
7443 | outbound_syms = (bfd_byte *) bfd_alloc2 (abfd, 1 + symcount, |
7444 | bed->s->sizeof_sym); | |
079e9a2f | 7445 | if (outbound_syms == NULL) |
5ed6aba4 | 7446 | { |
ef10c3ac L |
7447 | error_return: |
7448 | _bfd_elf_strtab_free (stt); | |
7449 | free (symstrtab); | |
5ed6aba4 NC |
7450 | return FALSE; |
7451 | } | |
217aa764 | 7452 | symtab_hdr->contents = outbound_syms; |
ef10c3ac | 7453 | outbound_syms_index = 0; |
252b5132 | 7454 | |
9ad5cbcf | 7455 | outbound_shndx = NULL; |
ef10c3ac | 7456 | outbound_shndx_index = 0; |
6a40cf0c NC |
7457 | |
7458 | if (elf_symtab_shndx_list (abfd)) | |
9ad5cbcf | 7459 | { |
6a40cf0c NC |
7460 | symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr; |
7461 | if (symtab_shndx_hdr->sh_name != 0) | |
7462 | { | |
7463 | amt = (bfd_size_type) (1 + symcount) * sizeof (Elf_External_Sym_Shndx); | |
7464 | outbound_shndx = (bfd_byte *) | |
7465 | bfd_zalloc2 (abfd, 1 + symcount, sizeof (Elf_External_Sym_Shndx)); | |
7466 | if (outbound_shndx == NULL) | |
7467 | goto error_return; | |
5ed6aba4 | 7468 | |
6a40cf0c NC |
7469 | symtab_shndx_hdr->contents = outbound_shndx; |
7470 | symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX; | |
7471 | symtab_shndx_hdr->sh_size = amt; | |
7472 | symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx); | |
7473 | symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx); | |
7474 | } | |
7475 | /* FIXME: What about any other headers in the list ? */ | |
9ad5cbcf AM |
7476 | } |
7477 | ||
589e6347 | 7478 | /* Now generate the data (for "contents"). */ |
079e9a2f AM |
7479 | { |
7480 | /* Fill in zeroth symbol and swap it out. */ | |
7481 | Elf_Internal_Sym sym; | |
7482 | sym.st_name = 0; | |
7483 | sym.st_value = 0; | |
7484 | sym.st_size = 0; | |
7485 | sym.st_info = 0; | |
7486 | sym.st_other = 0; | |
7487 | sym.st_shndx = SHN_UNDEF; | |
35fc36a8 | 7488 | sym.st_target_internal = 0; |
ef10c3ac L |
7489 | symstrtab[0].sym = sym; |
7490 | symstrtab[0].dest_index = outbound_syms_index; | |
7491 | symstrtab[0].destshndx_index = outbound_shndx_index; | |
7492 | outbound_syms_index++; | |
9ad5cbcf | 7493 | if (outbound_shndx != NULL) |
ef10c3ac | 7494 | outbound_shndx_index++; |
079e9a2f | 7495 | } |
252b5132 | 7496 | |
174fd7f9 RS |
7497 | name_local_sections |
7498 | = (bed->elf_backend_name_local_section_symbols | |
7499 | && bed->elf_backend_name_local_section_symbols (abfd)); | |
7500 | ||
079e9a2f | 7501 | syms = bfd_get_outsymbols (abfd); |
ef10c3ac | 7502 | for (idx = 0; idx < symcount;) |
252b5132 | 7503 | { |
252b5132 | 7504 | Elf_Internal_Sym sym; |
079e9a2f AM |
7505 | bfd_vma value = syms[idx]->value; |
7506 | elf_symbol_type *type_ptr; | |
7507 | flagword flags = syms[idx]->flags; | |
7508 | int type; | |
252b5132 | 7509 | |
174fd7f9 RS |
7510 | if (!name_local_sections |
7511 | && (flags & (BSF_SECTION_SYM | BSF_GLOBAL)) == BSF_SECTION_SYM) | |
079e9a2f AM |
7512 | { |
7513 | /* Local section symbols have no name. */ | |
ef10c3ac | 7514 | sym.st_name = (unsigned long) -1; |
079e9a2f AM |
7515 | } |
7516 | else | |
7517 | { | |
ef10c3ac L |
7518 | /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize |
7519 | to get the final offset for st_name. */ | |
7520 | sym.st_name | |
7521 | = (unsigned long) _bfd_elf_strtab_add (stt, syms[idx]->name, | |
7522 | FALSE); | |
079e9a2f | 7523 | if (sym.st_name == (unsigned long) -1) |
ef10c3ac | 7524 | goto error_return; |
079e9a2f | 7525 | } |
252b5132 | 7526 | |
079e9a2f | 7527 | type_ptr = elf_symbol_from (abfd, syms[idx]); |
252b5132 | 7528 | |
079e9a2f AM |
7529 | if ((flags & BSF_SECTION_SYM) == 0 |
7530 | && bfd_is_com_section (syms[idx]->section)) | |
7531 | { | |
7532 | /* ELF common symbols put the alignment into the `value' field, | |
7533 | and the size into the `size' field. This is backwards from | |
7534 | how BFD handles it, so reverse it here. */ | |
7535 | sym.st_size = value; | |
7536 | if (type_ptr == NULL | |
7537 | || type_ptr->internal_elf_sym.st_value == 0) | |
7538 | sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value)); | |
7539 | else | |
7540 | sym.st_value = type_ptr->internal_elf_sym.st_value; | |
7541 | sym.st_shndx = _bfd_elf_section_from_bfd_section | |
7542 | (abfd, syms[idx]->section); | |
7543 | } | |
7544 | else | |
7545 | { | |
7546 | asection *sec = syms[idx]->section; | |
cb33740c | 7547 | unsigned int shndx; |
252b5132 | 7548 | |
079e9a2f AM |
7549 | if (sec->output_section) |
7550 | { | |
7551 | value += sec->output_offset; | |
7552 | sec = sec->output_section; | |
7553 | } | |
589e6347 | 7554 | |
079e9a2f AM |
7555 | /* Don't add in the section vma for relocatable output. */ |
7556 | if (! relocatable_p) | |
7557 | value += sec->vma; | |
7558 | sym.st_value = value; | |
7559 | sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0; | |
7560 | ||
7561 | if (bfd_is_abs_section (sec) | |
7562 | && type_ptr != NULL | |
7563 | && type_ptr->internal_elf_sym.st_shndx != 0) | |
7564 | { | |
7565 | /* This symbol is in a real ELF section which we did | |
7566 | not create as a BFD section. Undo the mapping done | |
7567 | by copy_private_symbol_data. */ | |
7568 | shndx = type_ptr->internal_elf_sym.st_shndx; | |
7569 | switch (shndx) | |
7570 | { | |
7571 | case MAP_ONESYMTAB: | |
7572 | shndx = elf_onesymtab (abfd); | |
7573 | break; | |
7574 | case MAP_DYNSYMTAB: | |
7575 | shndx = elf_dynsymtab (abfd); | |
7576 | break; | |
7577 | case MAP_STRTAB: | |
12bd6957 | 7578 | shndx = elf_strtab_sec (abfd); |
079e9a2f AM |
7579 | break; |
7580 | case MAP_SHSTRTAB: | |
12bd6957 | 7581 | shndx = elf_shstrtab_sec (abfd); |
079e9a2f | 7582 | break; |
9ad5cbcf | 7583 | case MAP_SYM_SHNDX: |
6a40cf0c NC |
7584 | if (elf_symtab_shndx_list (abfd)) |
7585 | shndx = elf_symtab_shndx_list (abfd)->ndx; | |
9ad5cbcf | 7586 | break; |
079e9a2f | 7587 | default: |
15bc576a | 7588 | shndx = SHN_ABS; |
079e9a2f AM |
7589 | break; |
7590 | } | |
7591 | } | |
7592 | else | |
7593 | { | |
7594 | shndx = _bfd_elf_section_from_bfd_section (abfd, sec); | |
252b5132 | 7595 | |
cb33740c | 7596 | if (shndx == SHN_BAD) |
079e9a2f AM |
7597 | { |
7598 | asection *sec2; | |
7599 | ||
7600 | /* Writing this would be a hell of a lot easier if | |
7601 | we had some decent documentation on bfd, and | |
7602 | knew what to expect of the library, and what to | |
7603 | demand of applications. For example, it | |
7604 | appears that `objcopy' might not set the | |
7605 | section of a symbol to be a section that is | |
7606 | actually in the output file. */ | |
7607 | sec2 = bfd_get_section_by_name (abfd, sec->name); | |
589e6347 NC |
7608 | if (sec2 == NULL) |
7609 | { | |
7610 | _bfd_error_handler (_("\ | |
7611 | Unable to find equivalent output section for symbol '%s' from section '%s'"), | |
7612 | syms[idx]->name ? syms[idx]->name : "<Local sym>", | |
7613 | sec->name); | |
811072d8 | 7614 | bfd_set_error (bfd_error_invalid_operation); |
ef10c3ac | 7615 | goto error_return; |
589e6347 | 7616 | } |
811072d8 | 7617 | |
079e9a2f | 7618 | shndx = _bfd_elf_section_from_bfd_section (abfd, sec2); |
cb33740c | 7619 | BFD_ASSERT (shndx != SHN_BAD); |
079e9a2f AM |
7620 | } |
7621 | } | |
252b5132 | 7622 | |
079e9a2f AM |
7623 | sym.st_shndx = shndx; |
7624 | } | |
252b5132 | 7625 | |
13ae64f3 JJ |
7626 | if ((flags & BSF_THREAD_LOCAL) != 0) |
7627 | type = STT_TLS; | |
d8045f23 NC |
7628 | else if ((flags & BSF_GNU_INDIRECT_FUNCTION) != 0) |
7629 | type = STT_GNU_IFUNC; | |
13ae64f3 | 7630 | else if ((flags & BSF_FUNCTION) != 0) |
079e9a2f AM |
7631 | type = STT_FUNC; |
7632 | else if ((flags & BSF_OBJECT) != 0) | |
7633 | type = STT_OBJECT; | |
d9352518 DB |
7634 | else if ((flags & BSF_RELC) != 0) |
7635 | type = STT_RELC; | |
7636 | else if ((flags & BSF_SRELC) != 0) | |
7637 | type = STT_SRELC; | |
079e9a2f AM |
7638 | else |
7639 | type = STT_NOTYPE; | |
252b5132 | 7640 | |
13ae64f3 JJ |
7641 | if (syms[idx]->section->flags & SEC_THREAD_LOCAL) |
7642 | type = STT_TLS; | |
7643 | ||
589e6347 | 7644 | /* Processor-specific types. */ |
079e9a2f AM |
7645 | if (type_ptr != NULL |
7646 | && bed->elf_backend_get_symbol_type) | |
7647 | type = ((*bed->elf_backend_get_symbol_type) | |
7648 | (&type_ptr->internal_elf_sym, type)); | |
252b5132 | 7649 | |
079e9a2f AM |
7650 | if (flags & BSF_SECTION_SYM) |
7651 | { | |
7652 | if (flags & BSF_GLOBAL) | |
7653 | sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION); | |
7654 | else | |
7655 | sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION); | |
7656 | } | |
7657 | else if (bfd_is_com_section (syms[idx]->section)) | |
0a40daed | 7658 | { |
b8871f35 L |
7659 | if (type != STT_TLS) |
7660 | { | |
7661 | if ((abfd->flags & BFD_CONVERT_ELF_COMMON)) | |
7662 | type = ((abfd->flags & BFD_USE_ELF_STT_COMMON) | |
7663 | ? STT_COMMON : STT_OBJECT); | |
7664 | else | |
7665 | type = ((flags & BSF_ELF_COMMON) != 0 | |
7666 | ? STT_COMMON : STT_OBJECT); | |
7667 | } | |
7668 | sym.st_info = ELF_ST_INFO (STB_GLOBAL, type); | |
0a40daed | 7669 | } |
079e9a2f AM |
7670 | else if (bfd_is_und_section (syms[idx]->section)) |
7671 | sym.st_info = ELF_ST_INFO (((flags & BSF_WEAK) | |
7672 | ? STB_WEAK | |
7673 | : STB_GLOBAL), | |
7674 | type); | |
7675 | else if (flags & BSF_FILE) | |
7676 | sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE); | |
7677 | else | |
7678 | { | |
7679 | int bind = STB_LOCAL; | |
252b5132 | 7680 | |
079e9a2f AM |
7681 | if (flags & BSF_LOCAL) |
7682 | bind = STB_LOCAL; | |
3e7a7d11 NC |
7683 | else if (flags & BSF_GNU_UNIQUE) |
7684 | bind = STB_GNU_UNIQUE; | |
079e9a2f AM |
7685 | else if (flags & BSF_WEAK) |
7686 | bind = STB_WEAK; | |
7687 | else if (flags & BSF_GLOBAL) | |
7688 | bind = STB_GLOBAL; | |
252b5132 | 7689 | |
079e9a2f AM |
7690 | sym.st_info = ELF_ST_INFO (bind, type); |
7691 | } | |
252b5132 | 7692 | |
079e9a2f | 7693 | if (type_ptr != NULL) |
35fc36a8 RS |
7694 | { |
7695 | sym.st_other = type_ptr->internal_elf_sym.st_other; | |
7696 | sym.st_target_internal | |
7697 | = type_ptr->internal_elf_sym.st_target_internal; | |
7698 | } | |
079e9a2f | 7699 | else |
35fc36a8 RS |
7700 | { |
7701 | sym.st_other = 0; | |
7702 | sym.st_target_internal = 0; | |
7703 | } | |
252b5132 | 7704 | |
ef10c3ac L |
7705 | idx++; |
7706 | symstrtab[idx].sym = sym; | |
7707 | symstrtab[idx].dest_index = outbound_syms_index; | |
7708 | symstrtab[idx].destshndx_index = outbound_shndx_index; | |
7709 | ||
7710 | outbound_syms_index++; | |
9ad5cbcf | 7711 | if (outbound_shndx != NULL) |
ef10c3ac L |
7712 | outbound_shndx_index++; |
7713 | } | |
7714 | ||
7715 | /* Finalize the .strtab section. */ | |
7716 | _bfd_elf_strtab_finalize (stt); | |
7717 | ||
7718 | /* Swap out the .strtab section. */ | |
7719 | for (idx = 0; idx <= symcount; idx++) | |
7720 | { | |
7721 | struct elf_sym_strtab *elfsym = &symstrtab[idx]; | |
7722 | if (elfsym->sym.st_name == (unsigned long) -1) | |
7723 | elfsym->sym.st_name = 0; | |
7724 | else | |
7725 | elfsym->sym.st_name = _bfd_elf_strtab_offset (stt, | |
7726 | elfsym->sym.st_name); | |
7727 | bed->s->swap_symbol_out (abfd, &elfsym->sym, | |
7728 | (outbound_syms | |
7729 | + (elfsym->dest_index | |
7730 | * bed->s->sizeof_sym)), | |
7731 | (outbound_shndx | |
7732 | + (elfsym->destshndx_index | |
7733 | * sizeof (Elf_External_Sym_Shndx)))); | |
079e9a2f | 7734 | } |
ef10c3ac | 7735 | free (symstrtab); |
252b5132 | 7736 | |
079e9a2f | 7737 | *sttp = stt; |
ef10c3ac | 7738 | symstrtab_hdr->sh_size = _bfd_elf_strtab_size (stt); |
079e9a2f | 7739 | symstrtab_hdr->sh_type = SHT_STRTAB; |
84865015 | 7740 | symstrtab_hdr->sh_flags = bed->elf_strtab_flags; |
079e9a2f AM |
7741 | symstrtab_hdr->sh_addr = 0; |
7742 | symstrtab_hdr->sh_entsize = 0; | |
7743 | symstrtab_hdr->sh_link = 0; | |
7744 | symstrtab_hdr->sh_info = 0; | |
7745 | symstrtab_hdr->sh_addralign = 1; | |
252b5132 | 7746 | |
b34976b6 | 7747 | return TRUE; |
252b5132 RH |
7748 | } |
7749 | ||
7750 | /* Return the number of bytes required to hold the symtab vector. | |
7751 | ||
7752 | Note that we base it on the count plus 1, since we will null terminate | |
7753 | the vector allocated based on this size. However, the ELF symbol table | |
7754 | always has a dummy entry as symbol #0, so it ends up even. */ | |
7755 | ||
7756 | long | |
217aa764 | 7757 | _bfd_elf_get_symtab_upper_bound (bfd *abfd) |
252b5132 RH |
7758 | { |
7759 | long symcount; | |
7760 | long symtab_size; | |
7761 | Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr; | |
7762 | ||
7763 | symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym; | |
b99d1833 AM |
7764 | symtab_size = (symcount + 1) * (sizeof (asymbol *)); |
7765 | if (symcount > 0) | |
7766 | symtab_size -= sizeof (asymbol *); | |
252b5132 RH |
7767 | |
7768 | return symtab_size; | |
7769 | } | |
7770 | ||
7771 | long | |
217aa764 | 7772 | _bfd_elf_get_dynamic_symtab_upper_bound (bfd *abfd) |
252b5132 RH |
7773 | { |
7774 | long symcount; | |
7775 | long symtab_size; | |
7776 | Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr; | |
7777 | ||
7778 | if (elf_dynsymtab (abfd) == 0) | |
7779 | { | |
7780 | bfd_set_error (bfd_error_invalid_operation); | |
7781 | return -1; | |
7782 | } | |
7783 | ||
7784 | symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym; | |
b99d1833 AM |
7785 | symtab_size = (symcount + 1) * (sizeof (asymbol *)); |
7786 | if (symcount > 0) | |
7787 | symtab_size -= sizeof (asymbol *); | |
252b5132 RH |
7788 | |
7789 | return symtab_size; | |
7790 | } | |
7791 | ||
7792 | long | |
217aa764 AM |
7793 | _bfd_elf_get_reloc_upper_bound (bfd *abfd ATTRIBUTE_UNUSED, |
7794 | sec_ptr asect) | |
252b5132 RH |
7795 | { |
7796 | return (asect->reloc_count + 1) * sizeof (arelent *); | |
7797 | } | |
7798 | ||
7799 | /* Canonicalize the relocs. */ | |
7800 | ||
7801 | long | |
217aa764 AM |
7802 | _bfd_elf_canonicalize_reloc (bfd *abfd, |
7803 | sec_ptr section, | |
7804 | arelent **relptr, | |
7805 | asymbol **symbols) | |
252b5132 RH |
7806 | { |
7807 | arelent *tblptr; | |
7808 | unsigned int i; | |
9c5bfbb7 | 7809 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
252b5132 | 7810 | |
b34976b6 | 7811 | if (! bed->s->slurp_reloc_table (abfd, section, symbols, FALSE)) |
252b5132 RH |
7812 | return -1; |
7813 | ||
7814 | tblptr = section->relocation; | |
7815 | for (i = 0; i < section->reloc_count; i++) | |
7816 | *relptr++ = tblptr++; | |
7817 | ||
7818 | *relptr = NULL; | |
7819 | ||
7820 | return section->reloc_count; | |
7821 | } | |
7822 | ||
7823 | long | |
6cee3f79 | 7824 | _bfd_elf_canonicalize_symtab (bfd *abfd, asymbol **allocation) |
252b5132 | 7825 | { |
9c5bfbb7 | 7826 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
217aa764 | 7827 | long symcount = bed->s->slurp_symbol_table (abfd, allocation, FALSE); |
252b5132 RH |
7828 | |
7829 | if (symcount >= 0) | |
7830 | bfd_get_symcount (abfd) = symcount; | |
7831 | return symcount; | |
7832 | } | |
7833 | ||
7834 | long | |
217aa764 AM |
7835 | _bfd_elf_canonicalize_dynamic_symtab (bfd *abfd, |
7836 | asymbol **allocation) | |
252b5132 | 7837 | { |
9c5bfbb7 | 7838 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
217aa764 | 7839 | long symcount = bed->s->slurp_symbol_table (abfd, allocation, TRUE); |
1f70368c DJ |
7840 | |
7841 | if (symcount >= 0) | |
7842 | bfd_get_dynamic_symcount (abfd) = symcount; | |
7843 | return symcount; | |
252b5132 RH |
7844 | } |
7845 | ||
8615f3f2 AM |
7846 | /* Return the size required for the dynamic reloc entries. Any loadable |
7847 | section that was actually installed in the BFD, and has type SHT_REL | |
7848 | or SHT_RELA, and uses the dynamic symbol table, is considered to be a | |
7849 | dynamic reloc section. */ | |
252b5132 RH |
7850 | |
7851 | long | |
217aa764 | 7852 | _bfd_elf_get_dynamic_reloc_upper_bound (bfd *abfd) |
252b5132 RH |
7853 | { |
7854 | long ret; | |
7855 | asection *s; | |
7856 | ||
7857 | if (elf_dynsymtab (abfd) == 0) | |
7858 | { | |
7859 | bfd_set_error (bfd_error_invalid_operation); | |
7860 | return -1; | |
7861 | } | |
7862 | ||
7863 | ret = sizeof (arelent *); | |
7864 | for (s = abfd->sections; s != NULL; s = s->next) | |
266b05cf | 7865 | if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd) |
252b5132 RH |
7866 | && (elf_section_data (s)->this_hdr.sh_type == SHT_REL |
7867 | || elf_section_data (s)->this_hdr.sh_type == SHT_RELA)) | |
eea6121a | 7868 | ret += ((s->size / elf_section_data (s)->this_hdr.sh_entsize) |
252b5132 RH |
7869 | * sizeof (arelent *)); |
7870 | ||
7871 | return ret; | |
7872 | } | |
7873 | ||
8615f3f2 AM |
7874 | /* Canonicalize the dynamic relocation entries. Note that we return the |
7875 | dynamic relocations as a single block, although they are actually | |
7876 | associated with particular sections; the interface, which was | |
7877 | designed for SunOS style shared libraries, expects that there is only | |
7878 | one set of dynamic relocs. Any loadable section that was actually | |
7879 | installed in the BFD, and has type SHT_REL or SHT_RELA, and uses the | |
7880 | dynamic symbol table, is considered to be a dynamic reloc section. */ | |
252b5132 RH |
7881 | |
7882 | long | |
217aa764 AM |
7883 | _bfd_elf_canonicalize_dynamic_reloc (bfd *abfd, |
7884 | arelent **storage, | |
7885 | asymbol **syms) | |
252b5132 | 7886 | { |
217aa764 | 7887 | bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean); |
252b5132 RH |
7888 | asection *s; |
7889 | long ret; | |
7890 | ||
7891 | if (elf_dynsymtab (abfd) == 0) | |
7892 | { | |
7893 | bfd_set_error (bfd_error_invalid_operation); | |
7894 | return -1; | |
7895 | } | |
7896 | ||
7897 | slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table; | |
7898 | ret = 0; | |
7899 | for (s = abfd->sections; s != NULL; s = s->next) | |
7900 | { | |
266b05cf | 7901 | if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd) |
252b5132 RH |
7902 | && (elf_section_data (s)->this_hdr.sh_type == SHT_REL |
7903 | || elf_section_data (s)->this_hdr.sh_type == SHT_RELA)) | |
7904 | { | |
7905 | arelent *p; | |
7906 | long count, i; | |
7907 | ||
b34976b6 | 7908 | if (! (*slurp_relocs) (abfd, s, syms, TRUE)) |
252b5132 | 7909 | return -1; |
eea6121a | 7910 | count = s->size / elf_section_data (s)->this_hdr.sh_entsize; |
252b5132 RH |
7911 | p = s->relocation; |
7912 | for (i = 0; i < count; i++) | |
7913 | *storage++ = p++; | |
7914 | ret += count; | |
7915 | } | |
7916 | } | |
7917 | ||
7918 | *storage = NULL; | |
7919 | ||
7920 | return ret; | |
7921 | } | |
7922 | \f | |
7923 | /* Read in the version information. */ | |
7924 | ||
b34976b6 | 7925 | bfd_boolean |
fc0e6df6 | 7926 | _bfd_elf_slurp_version_tables (bfd *abfd, bfd_boolean default_imported_symver) |
252b5132 RH |
7927 | { |
7928 | bfd_byte *contents = NULL; | |
fc0e6df6 PB |
7929 | unsigned int freeidx = 0; |
7930 | ||
7931 | if (elf_dynverref (abfd) != 0) | |
7932 | { | |
7933 | Elf_Internal_Shdr *hdr; | |
7934 | Elf_External_Verneed *everneed; | |
7935 | Elf_Internal_Verneed *iverneed; | |
7936 | unsigned int i; | |
d0fb9a8d | 7937 | bfd_byte *contents_end; |
fc0e6df6 PB |
7938 | |
7939 | hdr = &elf_tdata (abfd)->dynverref_hdr; | |
7940 | ||
601a03ba | 7941 | if (hdr->sh_info == 0 || hdr->sh_size < sizeof (Elf_External_Verneed)) |
d0fb9a8d | 7942 | { |
601a03ba AM |
7943 | error_return_bad_verref: |
7944 | (*_bfd_error_handler) | |
7945 | (_("%B: .gnu.version_r invalid entry"), abfd); | |
7946 | bfd_set_error (bfd_error_bad_value); | |
d0fb9a8d JJ |
7947 | error_return_verref: |
7948 | elf_tdata (abfd)->verref = NULL; | |
7949 | elf_tdata (abfd)->cverrefs = 0; | |
7950 | goto error_return; | |
7951 | } | |
601a03ba AM |
7952 | |
7953 | contents = (bfd_byte *) bfd_malloc (hdr->sh_size); | |
7954 | if (contents == NULL) | |
7955 | goto error_return_verref; | |
7956 | ||
fc0e6df6 PB |
7957 | if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0 |
7958 | || bfd_bread (contents, hdr->sh_size, abfd) != hdr->sh_size) | |
d0fb9a8d | 7959 | goto error_return_verref; |
fc0e6df6 | 7960 | |
601a03ba AM |
7961 | elf_tdata (abfd)->verref = (Elf_Internal_Verneed *) |
7962 | bfd_zalloc2 (abfd, hdr->sh_info, sizeof (Elf_Internal_Verneed)); | |
7963 | ||
7964 | if (elf_tdata (abfd)->verref == NULL) | |
d0fb9a8d JJ |
7965 | goto error_return_verref; |
7966 | ||
7967 | BFD_ASSERT (sizeof (Elf_External_Verneed) | |
7968 | == sizeof (Elf_External_Vernaux)); | |
7969 | contents_end = contents + hdr->sh_size - sizeof (Elf_External_Verneed); | |
fc0e6df6 PB |
7970 | everneed = (Elf_External_Verneed *) contents; |
7971 | iverneed = elf_tdata (abfd)->verref; | |
7972 | for (i = 0; i < hdr->sh_info; i++, iverneed++) | |
7973 | { | |
7974 | Elf_External_Vernaux *evernaux; | |
7975 | Elf_Internal_Vernaux *ivernaux; | |
7976 | unsigned int j; | |
7977 | ||
7978 | _bfd_elf_swap_verneed_in (abfd, everneed, iverneed); | |
7979 | ||
7980 | iverneed->vn_bfd = abfd; | |
7981 | ||
7982 | iverneed->vn_filename = | |
7983 | bfd_elf_string_from_elf_section (abfd, hdr->sh_link, | |
7984 | iverneed->vn_file); | |
7985 | if (iverneed->vn_filename == NULL) | |
601a03ba | 7986 | goto error_return_bad_verref; |
fc0e6df6 | 7987 | |
d0fb9a8d JJ |
7988 | if (iverneed->vn_cnt == 0) |
7989 | iverneed->vn_auxptr = NULL; | |
7990 | else | |
7991 | { | |
a50b1753 NC |
7992 | iverneed->vn_auxptr = (struct elf_internal_vernaux *) |
7993 | bfd_alloc2 (abfd, iverneed->vn_cnt, | |
7994 | sizeof (Elf_Internal_Vernaux)); | |
d0fb9a8d JJ |
7995 | if (iverneed->vn_auxptr == NULL) |
7996 | goto error_return_verref; | |
7997 | } | |
7998 | ||
7999 | if (iverneed->vn_aux | |
8000 | > (size_t) (contents_end - (bfd_byte *) everneed)) | |
601a03ba | 8001 | goto error_return_bad_verref; |
fc0e6df6 PB |
8002 | |
8003 | evernaux = ((Elf_External_Vernaux *) | |
8004 | ((bfd_byte *) everneed + iverneed->vn_aux)); | |
8005 | ivernaux = iverneed->vn_auxptr; | |
8006 | for (j = 0; j < iverneed->vn_cnt; j++, ivernaux++) | |
8007 | { | |
8008 | _bfd_elf_swap_vernaux_in (abfd, evernaux, ivernaux); | |
8009 | ||
8010 | ivernaux->vna_nodename = | |
8011 | bfd_elf_string_from_elf_section (abfd, hdr->sh_link, | |
8012 | ivernaux->vna_name); | |
8013 | if (ivernaux->vna_nodename == NULL) | |
601a03ba | 8014 | goto error_return_bad_verref; |
fc0e6df6 | 8015 | |
25ff461f AM |
8016 | if (ivernaux->vna_other > freeidx) |
8017 | freeidx = ivernaux->vna_other; | |
8018 | ||
8019 | ivernaux->vna_nextptr = NULL; | |
8020 | if (ivernaux->vna_next == 0) | |
8021 | { | |
8022 | iverneed->vn_cnt = j + 1; | |
8023 | break; | |
8024 | } | |
fc0e6df6 PB |
8025 | if (j + 1 < iverneed->vn_cnt) |
8026 | ivernaux->vna_nextptr = ivernaux + 1; | |
fc0e6df6 | 8027 | |
d0fb9a8d JJ |
8028 | if (ivernaux->vna_next |
8029 | > (size_t) (contents_end - (bfd_byte *) evernaux)) | |
601a03ba | 8030 | goto error_return_bad_verref; |
d0fb9a8d | 8031 | |
fc0e6df6 PB |
8032 | evernaux = ((Elf_External_Vernaux *) |
8033 | ((bfd_byte *) evernaux + ivernaux->vna_next)); | |
fc0e6df6 PB |
8034 | } |
8035 | ||
25ff461f AM |
8036 | iverneed->vn_nextref = NULL; |
8037 | if (iverneed->vn_next == 0) | |
8038 | break; | |
fc0e6df6 PB |
8039 | if (i + 1 < hdr->sh_info) |
8040 | iverneed->vn_nextref = iverneed + 1; | |
fc0e6df6 | 8041 | |
d0fb9a8d JJ |
8042 | if (iverneed->vn_next |
8043 | > (size_t) (contents_end - (bfd_byte *) everneed)) | |
601a03ba | 8044 | goto error_return_bad_verref; |
d0fb9a8d | 8045 | |
fc0e6df6 PB |
8046 | everneed = ((Elf_External_Verneed *) |
8047 | ((bfd_byte *) everneed + iverneed->vn_next)); | |
8048 | } | |
25ff461f | 8049 | elf_tdata (abfd)->cverrefs = i; |
fc0e6df6 PB |
8050 | |
8051 | free (contents); | |
8052 | contents = NULL; | |
8053 | } | |
252b5132 RH |
8054 | |
8055 | if (elf_dynverdef (abfd) != 0) | |
8056 | { | |
8057 | Elf_Internal_Shdr *hdr; | |
8058 | Elf_External_Verdef *everdef; | |
8059 | Elf_Internal_Verdef *iverdef; | |
f631889e UD |
8060 | Elf_Internal_Verdef *iverdefarr; |
8061 | Elf_Internal_Verdef iverdefmem; | |
252b5132 | 8062 | unsigned int i; |
062e2358 | 8063 | unsigned int maxidx; |
d0fb9a8d | 8064 | bfd_byte *contents_end_def, *contents_end_aux; |
252b5132 RH |
8065 | |
8066 | hdr = &elf_tdata (abfd)->dynverdef_hdr; | |
8067 | ||
601a03ba AM |
8068 | if (hdr->sh_info == 0 || hdr->sh_size < sizeof (Elf_External_Verdef)) |
8069 | { | |
8070 | error_return_bad_verdef: | |
8071 | (*_bfd_error_handler) | |
8072 | (_("%B: .gnu.version_d invalid entry"), abfd); | |
8073 | bfd_set_error (bfd_error_bad_value); | |
8074 | error_return_verdef: | |
8075 | elf_tdata (abfd)->verdef = NULL; | |
8076 | elf_tdata (abfd)->cverdefs = 0; | |
8077 | goto error_return; | |
8078 | } | |
8079 | ||
a50b1753 | 8080 | contents = (bfd_byte *) bfd_malloc (hdr->sh_size); |
252b5132 | 8081 | if (contents == NULL) |
601a03ba | 8082 | goto error_return_verdef; |
252b5132 | 8083 | if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0 |
217aa764 | 8084 | || bfd_bread (contents, hdr->sh_size, abfd) != hdr->sh_size) |
601a03ba | 8085 | goto error_return_verdef; |
d0fb9a8d JJ |
8086 | |
8087 | BFD_ASSERT (sizeof (Elf_External_Verdef) | |
8088 | >= sizeof (Elf_External_Verdaux)); | |
8089 | contents_end_def = contents + hdr->sh_size | |
8090 | - sizeof (Elf_External_Verdef); | |
8091 | contents_end_aux = contents + hdr->sh_size | |
8092 | - sizeof (Elf_External_Verdaux); | |
8093 | ||
f631889e UD |
8094 | /* We know the number of entries in the section but not the maximum |
8095 | index. Therefore we have to run through all entries and find | |
8096 | the maximum. */ | |
252b5132 | 8097 | everdef = (Elf_External_Verdef *) contents; |
f631889e UD |
8098 | maxidx = 0; |
8099 | for (i = 0; i < hdr->sh_info; ++i) | |
8100 | { | |
8101 | _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem); | |
8102 | ||
601a03ba AM |
8103 | if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) == 0) |
8104 | goto error_return_bad_verdef; | |
062e2358 AM |
8105 | if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) > maxidx) |
8106 | maxidx = iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION); | |
f631889e | 8107 | |
25ff461f AM |
8108 | if (iverdefmem.vd_next == 0) |
8109 | break; | |
8110 | ||
d0fb9a8d JJ |
8111 | if (iverdefmem.vd_next |
8112 | > (size_t) (contents_end_def - (bfd_byte *) everdef)) | |
601a03ba | 8113 | goto error_return_bad_verdef; |
d0fb9a8d | 8114 | |
f631889e UD |
8115 | everdef = ((Elf_External_Verdef *) |
8116 | ((bfd_byte *) everdef + iverdefmem.vd_next)); | |
8117 | } | |
8118 | ||
fc0e6df6 PB |
8119 | if (default_imported_symver) |
8120 | { | |
8121 | if (freeidx > maxidx) | |
8122 | maxidx = ++freeidx; | |
8123 | else | |
8124 | freeidx = ++maxidx; | |
8125 | } | |
201159ec | 8126 | |
601a03ba AM |
8127 | elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *) |
8128 | bfd_zalloc2 (abfd, maxidx, sizeof (Elf_Internal_Verdef)); | |
f631889e | 8129 | if (elf_tdata (abfd)->verdef == NULL) |
601a03ba | 8130 | goto error_return_verdef; |
f631889e UD |
8131 | |
8132 | elf_tdata (abfd)->cverdefs = maxidx; | |
8133 | ||
8134 | everdef = (Elf_External_Verdef *) contents; | |
8135 | iverdefarr = elf_tdata (abfd)->verdef; | |
8136 | for (i = 0; i < hdr->sh_info; i++) | |
252b5132 RH |
8137 | { |
8138 | Elf_External_Verdaux *everdaux; | |
8139 | Elf_Internal_Verdaux *iverdaux; | |
8140 | unsigned int j; | |
8141 | ||
f631889e UD |
8142 | _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem); |
8143 | ||
d0fb9a8d | 8144 | if ((iverdefmem.vd_ndx & VERSYM_VERSION) == 0) |
601a03ba | 8145 | goto error_return_bad_verdef; |
d0fb9a8d | 8146 | |
f631889e | 8147 | iverdef = &iverdefarr[(iverdefmem.vd_ndx & VERSYM_VERSION) - 1]; |
595bce75 | 8148 | memcpy (iverdef, &iverdefmem, offsetof (Elf_Internal_Verdef, vd_bfd)); |
252b5132 RH |
8149 | |
8150 | iverdef->vd_bfd = abfd; | |
8151 | ||
d0fb9a8d JJ |
8152 | if (iverdef->vd_cnt == 0) |
8153 | iverdef->vd_auxptr = NULL; | |
8154 | else | |
8155 | { | |
a50b1753 NC |
8156 | iverdef->vd_auxptr = (struct elf_internal_verdaux *) |
8157 | bfd_alloc2 (abfd, iverdef->vd_cnt, | |
8158 | sizeof (Elf_Internal_Verdaux)); | |
d0fb9a8d JJ |
8159 | if (iverdef->vd_auxptr == NULL) |
8160 | goto error_return_verdef; | |
8161 | } | |
8162 | ||
8163 | if (iverdef->vd_aux | |
8164 | > (size_t) (contents_end_aux - (bfd_byte *) everdef)) | |
601a03ba | 8165 | goto error_return_bad_verdef; |
252b5132 RH |
8166 | |
8167 | everdaux = ((Elf_External_Verdaux *) | |
8168 | ((bfd_byte *) everdef + iverdef->vd_aux)); | |
8169 | iverdaux = iverdef->vd_auxptr; | |
8170 | for (j = 0; j < iverdef->vd_cnt; j++, iverdaux++) | |
8171 | { | |
8172 | _bfd_elf_swap_verdaux_in (abfd, everdaux, iverdaux); | |
8173 | ||
8174 | iverdaux->vda_nodename = | |
8175 | bfd_elf_string_from_elf_section (abfd, hdr->sh_link, | |
8176 | iverdaux->vda_name); | |
8177 | if (iverdaux->vda_nodename == NULL) | |
601a03ba | 8178 | goto error_return_bad_verdef; |
252b5132 | 8179 | |
25ff461f AM |
8180 | iverdaux->vda_nextptr = NULL; |
8181 | if (iverdaux->vda_next == 0) | |
8182 | { | |
8183 | iverdef->vd_cnt = j + 1; | |
8184 | break; | |
8185 | } | |
252b5132 RH |
8186 | if (j + 1 < iverdef->vd_cnt) |
8187 | iverdaux->vda_nextptr = iverdaux + 1; | |
252b5132 | 8188 | |
d0fb9a8d JJ |
8189 | if (iverdaux->vda_next |
8190 | > (size_t) (contents_end_aux - (bfd_byte *) everdaux)) | |
601a03ba | 8191 | goto error_return_bad_verdef; |
d0fb9a8d | 8192 | |
252b5132 RH |
8193 | everdaux = ((Elf_External_Verdaux *) |
8194 | ((bfd_byte *) everdaux + iverdaux->vda_next)); | |
8195 | } | |
8196 | ||
595bce75 | 8197 | iverdef->vd_nodename = NULL; |
d0fb9a8d JJ |
8198 | if (iverdef->vd_cnt) |
8199 | iverdef->vd_nodename = iverdef->vd_auxptr->vda_nodename; | |
252b5132 | 8200 | |
25ff461f AM |
8201 | iverdef->vd_nextdef = NULL; |
8202 | if (iverdef->vd_next == 0) | |
8203 | break; | |
d0fb9a8d | 8204 | if ((size_t) (iverdef - iverdefarr) + 1 < maxidx) |
252b5132 | 8205 | iverdef->vd_nextdef = iverdef + 1; |
252b5132 RH |
8206 | |
8207 | everdef = ((Elf_External_Verdef *) | |
8208 | ((bfd_byte *) everdef + iverdef->vd_next)); | |
8209 | } | |
8210 | ||
8211 | free (contents); | |
8212 | contents = NULL; | |
8213 | } | |
fc0e6df6 | 8214 | else if (default_imported_symver) |
252b5132 | 8215 | { |
fc0e6df6 PB |
8216 | if (freeidx < 3) |
8217 | freeidx = 3; | |
8218 | else | |
8219 | freeidx++; | |
252b5132 | 8220 | |
a50b1753 NC |
8221 | elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *) |
8222 | bfd_zalloc2 (abfd, freeidx, sizeof (Elf_Internal_Verdef)); | |
fc0e6df6 | 8223 | if (elf_tdata (abfd)->verdef == NULL) |
252b5132 RH |
8224 | goto error_return; |
8225 | ||
fc0e6df6 PB |
8226 | elf_tdata (abfd)->cverdefs = freeidx; |
8227 | } | |
252b5132 | 8228 | |
fc0e6df6 PB |
8229 | /* Create a default version based on the soname. */ |
8230 | if (default_imported_symver) | |
8231 | { | |
8232 | Elf_Internal_Verdef *iverdef; | |
8233 | Elf_Internal_Verdaux *iverdaux; | |
252b5132 | 8234 | |
5bb3703f | 8235 | iverdef = &elf_tdata (abfd)->verdef[freeidx - 1]; |
252b5132 | 8236 | |
fc0e6df6 PB |
8237 | iverdef->vd_version = VER_DEF_CURRENT; |
8238 | iverdef->vd_flags = 0; | |
8239 | iverdef->vd_ndx = freeidx; | |
8240 | iverdef->vd_cnt = 1; | |
252b5132 | 8241 | |
fc0e6df6 | 8242 | iverdef->vd_bfd = abfd; |
252b5132 | 8243 | |
fc0e6df6 PB |
8244 | iverdef->vd_nodename = bfd_elf_get_dt_soname (abfd); |
8245 | if (iverdef->vd_nodename == NULL) | |
d0fb9a8d | 8246 | goto error_return_verdef; |
fc0e6df6 | 8247 | iverdef->vd_nextdef = NULL; |
601a03ba AM |
8248 | iverdef->vd_auxptr = ((struct elf_internal_verdaux *) |
8249 | bfd_zalloc (abfd, sizeof (Elf_Internal_Verdaux))); | |
d0fb9a8d JJ |
8250 | if (iverdef->vd_auxptr == NULL) |
8251 | goto error_return_verdef; | |
252b5132 | 8252 | |
fc0e6df6 PB |
8253 | iverdaux = iverdef->vd_auxptr; |
8254 | iverdaux->vda_nodename = iverdef->vd_nodename; | |
252b5132 RH |
8255 | } |
8256 | ||
b34976b6 | 8257 | return TRUE; |
252b5132 RH |
8258 | |
8259 | error_return: | |
5ed6aba4 | 8260 | if (contents != NULL) |
252b5132 | 8261 | free (contents); |
b34976b6 | 8262 | return FALSE; |
252b5132 RH |
8263 | } |
8264 | \f | |
8265 | asymbol * | |
217aa764 | 8266 | _bfd_elf_make_empty_symbol (bfd *abfd) |
252b5132 RH |
8267 | { |
8268 | elf_symbol_type *newsym; | |
8269 | ||
201159ec | 8270 | newsym = (elf_symbol_type *) bfd_zalloc (abfd, sizeof * newsym); |
252b5132 RH |
8271 | if (!newsym) |
8272 | return NULL; | |
201159ec NC |
8273 | newsym->symbol.the_bfd = abfd; |
8274 | return &newsym->symbol; | |
252b5132 RH |
8275 | } |
8276 | ||
8277 | void | |
217aa764 AM |
8278 | _bfd_elf_get_symbol_info (bfd *abfd ATTRIBUTE_UNUSED, |
8279 | asymbol *symbol, | |
8280 | symbol_info *ret) | |
252b5132 RH |
8281 | { |
8282 | bfd_symbol_info (symbol, ret); | |
8283 | } | |
8284 | ||
8285 | /* Return whether a symbol name implies a local symbol. Most targets | |
8286 | use this function for the is_local_label_name entry point, but some | |
8287 | override it. */ | |
8288 | ||
b34976b6 | 8289 | bfd_boolean |
217aa764 AM |
8290 | _bfd_elf_is_local_label_name (bfd *abfd ATTRIBUTE_UNUSED, |
8291 | const char *name) | |
252b5132 RH |
8292 | { |
8293 | /* Normal local symbols start with ``.L''. */ | |
8294 | if (name[0] == '.' && name[1] == 'L') | |
b34976b6 | 8295 | return TRUE; |
252b5132 RH |
8296 | |
8297 | /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate | |
8298 | DWARF debugging symbols starting with ``..''. */ | |
8299 | if (name[0] == '.' && name[1] == '.') | |
b34976b6 | 8300 | return TRUE; |
252b5132 RH |
8301 | |
8302 | /* gcc will sometimes generate symbols beginning with ``_.L_'' when | |
8303 | emitting DWARF debugging output. I suspect this is actually a | |
8304 | small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call | |
8305 | ASM_GENERATE_INTERNAL_LABEL, and this causes the leading | |
8306 | underscore to be emitted on some ELF targets). For ease of use, | |
8307 | we treat such symbols as local. */ | |
8308 | if (name[0] == '_' && name[1] == '.' && name[2] == 'L' && name[3] == '_') | |
b34976b6 | 8309 | return TRUE; |
252b5132 | 8310 | |
b1fa9dd6 NC |
8311 | /* Treat assembler generated fake symbols, dollar local labels and |
8312 | forward-backward labels (aka local labels) as locals. | |
8313 | These labels have the form: | |
8314 | ||
8315 | L0^A.* (fake symbols) | |
8316 | ||
8317 | [.]?L[0123456789]+{^A|^B}[0123456789]* (local labels) | |
8318 | ||
8319 | Versions which start with .L will have already been matched above, | |
8320 | so we only need to match the rest. */ | |
8321 | if (name[0] == 'L' && ISDIGIT (name[1])) | |
8322 | { | |
8323 | bfd_boolean ret = FALSE; | |
8324 | const char * p; | |
8325 | char c; | |
8326 | ||
8327 | for (p = name + 2; (c = *p); p++) | |
8328 | { | |
8329 | if (c == 1 || c == 2) | |
8330 | { | |
8331 | if (c == 1 && p == name + 2) | |
8332 | /* A fake symbol. */ | |
8333 | return TRUE; | |
8334 | ||
8335 | /* FIXME: We are being paranoid here and treating symbols like | |
8336 | L0^Bfoo as if there were non-local, on the grounds that the | |
8337 | assembler will never generate them. But can any symbol | |
8338 | containing an ASCII value in the range 1-31 ever be anything | |
8339 | other than some kind of local ? */ | |
8340 | ret = TRUE; | |
8341 | } | |
8342 | ||
8343 | if (! ISDIGIT (c)) | |
8344 | { | |
8345 | ret = FALSE; | |
8346 | break; | |
8347 | } | |
8348 | } | |
8349 | return ret; | |
8350 | } | |
ffa54770 | 8351 | |
b34976b6 | 8352 | return FALSE; |
252b5132 RH |
8353 | } |
8354 | ||
8355 | alent * | |
217aa764 AM |
8356 | _bfd_elf_get_lineno (bfd *abfd ATTRIBUTE_UNUSED, |
8357 | asymbol *symbol ATTRIBUTE_UNUSED) | |
252b5132 RH |
8358 | { |
8359 | abort (); | |
8360 | return NULL; | |
8361 | } | |
8362 | ||
b34976b6 | 8363 | bfd_boolean |
217aa764 AM |
8364 | _bfd_elf_set_arch_mach (bfd *abfd, |
8365 | enum bfd_architecture arch, | |
8366 | unsigned long machine) | |
252b5132 RH |
8367 | { |
8368 | /* If this isn't the right architecture for this backend, and this | |
8369 | isn't the generic backend, fail. */ | |
8370 | if (arch != get_elf_backend_data (abfd)->arch | |
8371 | && arch != bfd_arch_unknown | |
8372 | && get_elf_backend_data (abfd)->arch != bfd_arch_unknown) | |
b34976b6 | 8373 | return FALSE; |
252b5132 RH |
8374 | |
8375 | return bfd_default_set_arch_mach (abfd, arch, machine); | |
8376 | } | |
8377 | ||
d1fad7c6 NC |
8378 | /* Find the nearest line to a particular section and offset, |
8379 | for error reporting. */ | |
8380 | ||
b34976b6 | 8381 | bfd_boolean |
217aa764 | 8382 | _bfd_elf_find_nearest_line (bfd *abfd, |
217aa764 | 8383 | asymbol **symbols, |
fb167eb2 | 8384 | asection *section, |
217aa764 AM |
8385 | bfd_vma offset, |
8386 | const char **filename_ptr, | |
8387 | const char **functionname_ptr, | |
fb167eb2 AM |
8388 | unsigned int *line_ptr, |
8389 | unsigned int *discriminator_ptr) | |
d1fad7c6 | 8390 | { |
b34976b6 | 8391 | bfd_boolean found; |
d1fad7c6 | 8392 | |
fb167eb2 | 8393 | if (_bfd_dwarf2_find_nearest_line (abfd, symbols, NULL, section, offset, |
4e8a9624 | 8394 | filename_ptr, functionname_ptr, |
fb167eb2 AM |
8395 | line_ptr, discriminator_ptr, |
8396 | dwarf_debug_sections, 0, | |
e00e8198 AM |
8397 | &elf_tdata (abfd)->dwarf2_find_line_info) |
8398 | || _bfd_dwarf1_find_nearest_line (abfd, symbols, section, offset, | |
8399 | filename_ptr, functionname_ptr, | |
8400 | line_ptr)) | |
d1fad7c6 NC |
8401 | { |
8402 | if (!*functionname_ptr) | |
e00e8198 AM |
8403 | _bfd_elf_find_function (abfd, symbols, section, offset, |
8404 | *filename_ptr ? NULL : filename_ptr, | |
8405 | functionname_ptr); | |
b34976b6 | 8406 | return TRUE; |
d1fad7c6 NC |
8407 | } |
8408 | ||
8409 | if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset, | |
4e8a9624 AM |
8410 | &found, filename_ptr, |
8411 | functionname_ptr, line_ptr, | |
8412 | &elf_tdata (abfd)->line_info)) | |
b34976b6 | 8413 | return FALSE; |
dc43ada5 | 8414 | if (found && (*functionname_ptr || *line_ptr)) |
b34976b6 | 8415 | return TRUE; |
d1fad7c6 NC |
8416 | |
8417 | if (symbols == NULL) | |
b34976b6 | 8418 | return FALSE; |
d1fad7c6 | 8419 | |
e00e8198 AM |
8420 | if (! _bfd_elf_find_function (abfd, symbols, section, offset, |
8421 | filename_ptr, functionname_ptr)) | |
b34976b6 | 8422 | return FALSE; |
d1fad7c6 | 8423 | |
252b5132 | 8424 | *line_ptr = 0; |
b34976b6 | 8425 | return TRUE; |
252b5132 RH |
8426 | } |
8427 | ||
5420f73d L |
8428 | /* Find the line for a symbol. */ |
8429 | ||
8430 | bfd_boolean | |
8431 | _bfd_elf_find_line (bfd *abfd, asymbol **symbols, asymbol *symbol, | |
8432 | const char **filename_ptr, unsigned int *line_ptr) | |
9b8d1a36 | 8433 | { |
fb167eb2 AM |
8434 | return _bfd_dwarf2_find_nearest_line (abfd, symbols, symbol, NULL, 0, |
8435 | filename_ptr, NULL, line_ptr, NULL, | |
8436 | dwarf_debug_sections, 0, | |
8437 | &elf_tdata (abfd)->dwarf2_find_line_info); | |
5420f73d L |
8438 | } |
8439 | ||
4ab527b0 FF |
8440 | /* After a call to bfd_find_nearest_line, successive calls to |
8441 | bfd_find_inliner_info can be used to get source information about | |
8442 | each level of function inlining that terminated at the address | |
8443 | passed to bfd_find_nearest_line. Currently this is only supported | |
8444 | for DWARF2 with appropriate DWARF3 extensions. */ | |
8445 | ||
8446 | bfd_boolean | |
8447 | _bfd_elf_find_inliner_info (bfd *abfd, | |
8448 | const char **filename_ptr, | |
8449 | const char **functionname_ptr, | |
8450 | unsigned int *line_ptr) | |
8451 | { | |
8452 | bfd_boolean found; | |
8453 | found = _bfd_dwarf2_find_inliner_info (abfd, filename_ptr, | |
8454 | functionname_ptr, line_ptr, | |
8455 | & elf_tdata (abfd)->dwarf2_find_line_info); | |
8456 | return found; | |
8457 | } | |
8458 | ||
252b5132 | 8459 | int |
a6b96beb | 8460 | _bfd_elf_sizeof_headers (bfd *abfd, struct bfd_link_info *info) |
252b5132 | 8461 | { |
8ded5a0f AM |
8462 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
8463 | int ret = bed->s->sizeof_ehdr; | |
252b5132 | 8464 | |
0e1862bb | 8465 | if (!bfd_link_relocatable (info)) |
8ded5a0f | 8466 | { |
12bd6957 | 8467 | bfd_size_type phdr_size = elf_program_header_size (abfd); |
8ded5a0f | 8468 | |
62d7a5f6 AM |
8469 | if (phdr_size == (bfd_size_type) -1) |
8470 | { | |
8471 | struct elf_segment_map *m; | |
8472 | ||
8473 | phdr_size = 0; | |
12bd6957 | 8474 | for (m = elf_seg_map (abfd); m != NULL; m = m->next) |
62d7a5f6 | 8475 | phdr_size += bed->s->sizeof_phdr; |
8ded5a0f | 8476 | |
62d7a5f6 AM |
8477 | if (phdr_size == 0) |
8478 | phdr_size = get_program_header_size (abfd, info); | |
8479 | } | |
8ded5a0f | 8480 | |
12bd6957 | 8481 | elf_program_header_size (abfd) = phdr_size; |
8ded5a0f AM |
8482 | ret += phdr_size; |
8483 | } | |
8484 | ||
252b5132 RH |
8485 | return ret; |
8486 | } | |
8487 | ||
b34976b6 | 8488 | bfd_boolean |
217aa764 AM |
8489 | _bfd_elf_set_section_contents (bfd *abfd, |
8490 | sec_ptr section, | |
0f867abe | 8491 | const void *location, |
217aa764 AM |
8492 | file_ptr offset, |
8493 | bfd_size_type count) | |
252b5132 RH |
8494 | { |
8495 | Elf_Internal_Shdr *hdr; | |
1b6aeedb | 8496 | file_ptr pos; |
252b5132 RH |
8497 | |
8498 | if (! abfd->output_has_begun | |
217aa764 | 8499 | && ! _bfd_elf_compute_section_file_positions (abfd, NULL)) |
b34976b6 | 8500 | return FALSE; |
252b5132 | 8501 | |
0ce398f1 L |
8502 | if (!count) |
8503 | return TRUE; | |
8504 | ||
252b5132 | 8505 | hdr = &elf_section_data (section)->this_hdr; |
0ce398f1 L |
8506 | if (hdr->sh_offset == (file_ptr) -1) |
8507 | { | |
8508 | /* We must compress this section. Write output to the buffer. */ | |
8509 | unsigned char *contents = hdr->contents; | |
8510 | if ((offset + count) > hdr->sh_size | |
8511 | || (section->flags & SEC_ELF_COMPRESS) == 0 | |
8512 | || contents == NULL) | |
8513 | abort (); | |
8514 | memcpy (contents + offset, location, count); | |
8515 | return TRUE; | |
8516 | } | |
dc810e39 AM |
8517 | pos = hdr->sh_offset + offset; |
8518 | if (bfd_seek (abfd, pos, SEEK_SET) != 0 | |
8519 | || bfd_bwrite (location, count, abfd) != count) | |
b34976b6 | 8520 | return FALSE; |
252b5132 | 8521 | |
b34976b6 | 8522 | return TRUE; |
252b5132 RH |
8523 | } |
8524 | ||
8525 | void | |
217aa764 AM |
8526 | _bfd_elf_no_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED, |
8527 | arelent *cache_ptr ATTRIBUTE_UNUSED, | |
8528 | Elf_Internal_Rela *dst ATTRIBUTE_UNUSED) | |
252b5132 RH |
8529 | { |
8530 | abort (); | |
8531 | } | |
8532 | ||
252b5132 RH |
8533 | /* Try to convert a non-ELF reloc into an ELF one. */ |
8534 | ||
b34976b6 | 8535 | bfd_boolean |
217aa764 | 8536 | _bfd_elf_validate_reloc (bfd *abfd, arelent *areloc) |
252b5132 | 8537 | { |
c044fabd | 8538 | /* Check whether we really have an ELF howto. */ |
252b5132 RH |
8539 | |
8540 | if ((*areloc->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec) | |
8541 | { | |
8542 | bfd_reloc_code_real_type code; | |
8543 | reloc_howto_type *howto; | |
8544 | ||
8545 | /* Alien reloc: Try to determine its type to replace it with an | |
c044fabd | 8546 | equivalent ELF reloc. */ |
252b5132 RH |
8547 | |
8548 | if (areloc->howto->pc_relative) | |
8549 | { | |
8550 | switch (areloc->howto->bitsize) | |
8551 | { | |
8552 | case 8: | |
8553 | code = BFD_RELOC_8_PCREL; | |
8554 | break; | |
8555 | case 12: | |
8556 | code = BFD_RELOC_12_PCREL; | |
8557 | break; | |
8558 | case 16: | |
8559 | code = BFD_RELOC_16_PCREL; | |
8560 | break; | |
8561 | case 24: | |
8562 | code = BFD_RELOC_24_PCREL; | |
8563 | break; | |
8564 | case 32: | |
8565 | code = BFD_RELOC_32_PCREL; | |
8566 | break; | |
8567 | case 64: | |
8568 | code = BFD_RELOC_64_PCREL; | |
8569 | break; | |
8570 | default: | |
8571 | goto fail; | |
8572 | } | |
8573 | ||
8574 | howto = bfd_reloc_type_lookup (abfd, code); | |
8575 | ||
8576 | if (areloc->howto->pcrel_offset != howto->pcrel_offset) | |
8577 | { | |
8578 | if (howto->pcrel_offset) | |
8579 | areloc->addend += areloc->address; | |
8580 | else | |
8581 | areloc->addend -= areloc->address; /* addend is unsigned!! */ | |
8582 | } | |
8583 | } | |
8584 | else | |
8585 | { | |
8586 | switch (areloc->howto->bitsize) | |
8587 | { | |
8588 | case 8: | |
8589 | code = BFD_RELOC_8; | |
8590 | break; | |
8591 | case 14: | |
8592 | code = BFD_RELOC_14; | |
8593 | break; | |
8594 | case 16: | |
8595 | code = BFD_RELOC_16; | |
8596 | break; | |
8597 | case 26: | |
8598 | code = BFD_RELOC_26; | |
8599 | break; | |
8600 | case 32: | |
8601 | code = BFD_RELOC_32; | |
8602 | break; | |
8603 | case 64: | |
8604 | code = BFD_RELOC_64; | |
8605 | break; | |
8606 | default: | |
8607 | goto fail; | |
8608 | } | |
8609 | ||
8610 | howto = bfd_reloc_type_lookup (abfd, code); | |
8611 | } | |
8612 | ||
8613 | if (howto) | |
8614 | areloc->howto = howto; | |
8615 | else | |
8616 | goto fail; | |
8617 | } | |
8618 | ||
b34976b6 | 8619 | return TRUE; |
252b5132 RH |
8620 | |
8621 | fail: | |
8622 | (*_bfd_error_handler) | |
d003868e AM |
8623 | (_("%B: unsupported relocation type %s"), |
8624 | abfd, areloc->howto->name); | |
252b5132 | 8625 | bfd_set_error (bfd_error_bad_value); |
b34976b6 | 8626 | return FALSE; |
252b5132 RH |
8627 | } |
8628 | ||
b34976b6 | 8629 | bfd_boolean |
217aa764 | 8630 | _bfd_elf_close_and_cleanup (bfd *abfd) |
252b5132 | 8631 | { |
d9071b0c TG |
8632 | struct elf_obj_tdata *tdata = elf_tdata (abfd); |
8633 | if (bfd_get_format (abfd) == bfd_object && tdata != NULL) | |
252b5132 | 8634 | { |
c0355132 | 8635 | if (elf_tdata (abfd)->o != NULL && elf_shstrtab (abfd) != NULL) |
2b0f7ef9 | 8636 | _bfd_elf_strtab_free (elf_shstrtab (abfd)); |
d9071b0c | 8637 | _bfd_dwarf2_cleanup_debug_info (abfd, &tdata->dwarf2_find_line_info); |
252b5132 RH |
8638 | } |
8639 | ||
8640 | return _bfd_generic_close_and_cleanup (abfd); | |
8641 | } | |
8642 | ||
8643 | /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY | |
8644 | in the relocation's offset. Thus we cannot allow any sort of sanity | |
8645 | range-checking to interfere. There is nothing else to do in processing | |
8646 | this reloc. */ | |
8647 | ||
8648 | bfd_reloc_status_type | |
217aa764 AM |
8649 | _bfd_elf_rel_vtable_reloc_fn |
8650 | (bfd *abfd ATTRIBUTE_UNUSED, arelent *re ATTRIBUTE_UNUSED, | |
fc0a2244 | 8651 | struct bfd_symbol *symbol ATTRIBUTE_UNUSED, |
217aa764 AM |
8652 | void *data ATTRIBUTE_UNUSED, asection *is ATTRIBUTE_UNUSED, |
8653 | bfd *obfd ATTRIBUTE_UNUSED, char **errmsg ATTRIBUTE_UNUSED) | |
252b5132 RH |
8654 | { |
8655 | return bfd_reloc_ok; | |
8656 | } | |
252b5132 RH |
8657 | \f |
8658 | /* Elf core file support. Much of this only works on native | |
8659 | toolchains, since we rely on knowing the | |
8660 | machine-dependent procfs structure in order to pick | |
c044fabd | 8661 | out details about the corefile. */ |
252b5132 RH |
8662 | |
8663 | #ifdef HAVE_SYS_PROCFS_H | |
16231b7b DG |
8664 | /* Needed for new procfs interface on sparc-solaris. */ |
8665 | # define _STRUCTURED_PROC 1 | |
252b5132 RH |
8666 | # include <sys/procfs.h> |
8667 | #endif | |
8668 | ||
261b8d08 PA |
8669 | /* Return a PID that identifies a "thread" for threaded cores, or the |
8670 | PID of the main process for non-threaded cores. */ | |
252b5132 RH |
8671 | |
8672 | static int | |
217aa764 | 8673 | elfcore_make_pid (bfd *abfd) |
252b5132 | 8674 | { |
261b8d08 PA |
8675 | int pid; |
8676 | ||
228e534f | 8677 | pid = elf_tdata (abfd)->core->lwpid; |
261b8d08 | 8678 | if (pid == 0) |
228e534f | 8679 | pid = elf_tdata (abfd)->core->pid; |
261b8d08 PA |
8680 | |
8681 | return pid; | |
252b5132 RH |
8682 | } |
8683 | ||
252b5132 RH |
8684 | /* If there isn't a section called NAME, make one, using |
8685 | data from SECT. Note, this function will generate a | |
8686 | reference to NAME, so you shouldn't deallocate or | |
c044fabd | 8687 | overwrite it. */ |
252b5132 | 8688 | |
b34976b6 | 8689 | static bfd_boolean |
217aa764 | 8690 | elfcore_maybe_make_sect (bfd *abfd, char *name, asection *sect) |
252b5132 | 8691 | { |
c044fabd | 8692 | asection *sect2; |
252b5132 RH |
8693 | |
8694 | if (bfd_get_section_by_name (abfd, name) != NULL) | |
b34976b6 | 8695 | return TRUE; |
252b5132 | 8696 | |
117ed4f8 | 8697 | sect2 = bfd_make_section_with_flags (abfd, name, sect->flags); |
252b5132 | 8698 | if (sect2 == NULL) |
b34976b6 | 8699 | return FALSE; |
252b5132 | 8700 | |
eea6121a | 8701 | sect2->size = sect->size; |
252b5132 | 8702 | sect2->filepos = sect->filepos; |
252b5132 | 8703 | sect2->alignment_power = sect->alignment_power; |
b34976b6 | 8704 | return TRUE; |
252b5132 RH |
8705 | } |
8706 | ||
bb0082d6 AM |
8707 | /* Create a pseudosection containing SIZE bytes at FILEPOS. This |
8708 | actually creates up to two pseudosections: | |
8709 | - For the single-threaded case, a section named NAME, unless | |
8710 | such a section already exists. | |
8711 | - For the multi-threaded case, a section named "NAME/PID", where | |
8712 | PID is elfcore_make_pid (abfd). | |
8713 | Both pseudosections have identical contents. */ | |
b34976b6 | 8714 | bfd_boolean |
217aa764 AM |
8715 | _bfd_elfcore_make_pseudosection (bfd *abfd, |
8716 | char *name, | |
8717 | size_t size, | |
8718 | ufile_ptr filepos) | |
bb0082d6 AM |
8719 | { |
8720 | char buf[100]; | |
8721 | char *threaded_name; | |
d4c88bbb | 8722 | size_t len; |
bb0082d6 AM |
8723 | asection *sect; |
8724 | ||
8725 | /* Build the section name. */ | |
8726 | ||
8727 | sprintf (buf, "%s/%d", name, elfcore_make_pid (abfd)); | |
d4c88bbb | 8728 | len = strlen (buf) + 1; |
a50b1753 | 8729 | threaded_name = (char *) bfd_alloc (abfd, len); |
bb0082d6 | 8730 | if (threaded_name == NULL) |
b34976b6 | 8731 | return FALSE; |
d4c88bbb | 8732 | memcpy (threaded_name, buf, len); |
bb0082d6 | 8733 | |
117ed4f8 AM |
8734 | sect = bfd_make_section_anyway_with_flags (abfd, threaded_name, |
8735 | SEC_HAS_CONTENTS); | |
bb0082d6 | 8736 | if (sect == NULL) |
b34976b6 | 8737 | return FALSE; |
eea6121a | 8738 | sect->size = size; |
bb0082d6 | 8739 | sect->filepos = filepos; |
bb0082d6 AM |
8740 | sect->alignment_power = 2; |
8741 | ||
936e320b | 8742 | return elfcore_maybe_make_sect (abfd, name, sect); |
bb0082d6 AM |
8743 | } |
8744 | ||
252b5132 | 8745 | /* prstatus_t exists on: |
4a938328 | 8746 | solaris 2.5+ |
252b5132 RH |
8747 | linux 2.[01] + glibc |
8748 | unixware 4.2 | |
8749 | */ | |
8750 | ||
8751 | #if defined (HAVE_PRSTATUS_T) | |
a7b97311 | 8752 | |
b34976b6 | 8753 | static bfd_boolean |
217aa764 | 8754 | elfcore_grok_prstatus (bfd *abfd, Elf_Internal_Note *note) |
252b5132 | 8755 | { |
eea6121a | 8756 | size_t size; |
7ee38065 | 8757 | int offset; |
252b5132 | 8758 | |
4a938328 MS |
8759 | if (note->descsz == sizeof (prstatus_t)) |
8760 | { | |
8761 | prstatus_t prstat; | |
252b5132 | 8762 | |
eea6121a | 8763 | size = sizeof (prstat.pr_reg); |
7ee38065 | 8764 | offset = offsetof (prstatus_t, pr_reg); |
4a938328 | 8765 | memcpy (&prstat, note->descdata, sizeof (prstat)); |
252b5132 | 8766 | |
fa49d224 NC |
8767 | /* Do not overwrite the core signal if it |
8768 | has already been set by another thread. */ | |
228e534f AM |
8769 | if (elf_tdata (abfd)->core->signal == 0) |
8770 | elf_tdata (abfd)->core->signal = prstat.pr_cursig; | |
8771 | if (elf_tdata (abfd)->core->pid == 0) | |
8772 | elf_tdata (abfd)->core->pid = prstat.pr_pid; | |
252b5132 | 8773 | |
4a938328 MS |
8774 | /* pr_who exists on: |
8775 | solaris 2.5+ | |
8776 | unixware 4.2 | |
8777 | pr_who doesn't exist on: | |
8778 | linux 2.[01] | |
8779 | */ | |
252b5132 | 8780 | #if defined (HAVE_PRSTATUS_T_PR_WHO) |
228e534f | 8781 | elf_tdata (abfd)->core->lwpid = prstat.pr_who; |
261b8d08 | 8782 | #else |
228e534f | 8783 | elf_tdata (abfd)->core->lwpid = prstat.pr_pid; |
252b5132 | 8784 | #endif |
4a938328 | 8785 | } |
7ee38065 | 8786 | #if defined (HAVE_PRSTATUS32_T) |
4a938328 MS |
8787 | else if (note->descsz == sizeof (prstatus32_t)) |
8788 | { | |
8789 | /* 64-bit host, 32-bit corefile */ | |
8790 | prstatus32_t prstat; | |
8791 | ||
eea6121a | 8792 | size = sizeof (prstat.pr_reg); |
7ee38065 | 8793 | offset = offsetof (prstatus32_t, pr_reg); |
4a938328 MS |
8794 | memcpy (&prstat, note->descdata, sizeof (prstat)); |
8795 | ||
fa49d224 NC |
8796 | /* Do not overwrite the core signal if it |
8797 | has already been set by another thread. */ | |
228e534f AM |
8798 | if (elf_tdata (abfd)->core->signal == 0) |
8799 | elf_tdata (abfd)->core->signal = prstat.pr_cursig; | |
8800 | if (elf_tdata (abfd)->core->pid == 0) | |
8801 | elf_tdata (abfd)->core->pid = prstat.pr_pid; | |
4a938328 MS |
8802 | |
8803 | /* pr_who exists on: | |
8804 | solaris 2.5+ | |
8805 | unixware 4.2 | |
8806 | pr_who doesn't exist on: | |
8807 | linux 2.[01] | |
8808 | */ | |
7ee38065 | 8809 | #if defined (HAVE_PRSTATUS32_T_PR_WHO) |
228e534f | 8810 | elf_tdata (abfd)->core->lwpid = prstat.pr_who; |
261b8d08 | 8811 | #else |
228e534f | 8812 | elf_tdata (abfd)->core->lwpid = prstat.pr_pid; |
4a938328 MS |
8813 | #endif |
8814 | } | |
7ee38065 | 8815 | #endif /* HAVE_PRSTATUS32_T */ |
4a938328 MS |
8816 | else |
8817 | { | |
8818 | /* Fail - we don't know how to handle any other | |
8819 | note size (ie. data object type). */ | |
b34976b6 | 8820 | return TRUE; |
4a938328 | 8821 | } |
252b5132 | 8822 | |
bb0082d6 | 8823 | /* Make a ".reg/999" section and a ".reg" section. */ |
936e320b | 8824 | return _bfd_elfcore_make_pseudosection (abfd, ".reg", |
eea6121a | 8825 | size, note->descpos + offset); |
252b5132 RH |
8826 | } |
8827 | #endif /* defined (HAVE_PRSTATUS_T) */ | |
8828 | ||
bb0082d6 | 8829 | /* Create a pseudosection containing the exact contents of NOTE. */ |
b34976b6 | 8830 | static bfd_boolean |
217aa764 AM |
8831 | elfcore_make_note_pseudosection (bfd *abfd, |
8832 | char *name, | |
8833 | Elf_Internal_Note *note) | |
252b5132 | 8834 | { |
936e320b AM |
8835 | return _bfd_elfcore_make_pseudosection (abfd, name, |
8836 | note->descsz, note->descpos); | |
252b5132 RH |
8837 | } |
8838 | ||
ff08c6bb JB |
8839 | /* There isn't a consistent prfpregset_t across platforms, |
8840 | but it doesn't matter, because we don't have to pick this | |
c044fabd KH |
8841 | data structure apart. */ |
8842 | ||
b34976b6 | 8843 | static bfd_boolean |
217aa764 | 8844 | elfcore_grok_prfpreg (bfd *abfd, Elf_Internal_Note *note) |
ff08c6bb JB |
8845 | { |
8846 | return elfcore_make_note_pseudosection (abfd, ".reg2", note); | |
8847 | } | |
8848 | ||
ff08c6bb | 8849 | /* Linux dumps the Intel SSE regs in a note named "LINUX" with a note |
971d4640 | 8850 | type of NT_PRXFPREG. Just include the whole note's contents |
ff08c6bb | 8851 | literally. */ |
c044fabd | 8852 | |
b34976b6 | 8853 | static bfd_boolean |
217aa764 | 8854 | elfcore_grok_prxfpreg (bfd *abfd, Elf_Internal_Note *note) |
ff08c6bb JB |
8855 | { |
8856 | return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note); | |
8857 | } | |
8858 | ||
4339cae0 L |
8859 | /* Linux dumps the Intel XSAVE extended state in a note named "LINUX" |
8860 | with a note type of NT_X86_XSTATE. Just include the whole note's | |
8861 | contents literally. */ | |
8862 | ||
8863 | static bfd_boolean | |
8864 | elfcore_grok_xstatereg (bfd *abfd, Elf_Internal_Note *note) | |
8865 | { | |
8866 | return elfcore_make_note_pseudosection (abfd, ".reg-xstate", note); | |
8867 | } | |
8868 | ||
97753bd5 AM |
8869 | static bfd_boolean |
8870 | elfcore_grok_ppc_vmx (bfd *abfd, Elf_Internal_Note *note) | |
8871 | { | |
8872 | return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vmx", note); | |
8873 | } | |
8874 | ||
89eeb0bc LM |
8875 | static bfd_boolean |
8876 | elfcore_grok_ppc_vsx (bfd *abfd, Elf_Internal_Note *note) | |
8877 | { | |
8878 | return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vsx", note); | |
8879 | } | |
97753bd5 | 8880 | |
0675e188 UW |
8881 | static bfd_boolean |
8882 | elfcore_grok_s390_high_gprs (bfd *abfd, Elf_Internal_Note *note) | |
8883 | { | |
8884 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-high-gprs", note); | |
8885 | } | |
8886 | ||
d7eeb400 MS |
8887 | static bfd_boolean |
8888 | elfcore_grok_s390_timer (bfd *abfd, Elf_Internal_Note *note) | |
8889 | { | |
8890 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-timer", note); | |
8891 | } | |
8892 | ||
8893 | static bfd_boolean | |
8894 | elfcore_grok_s390_todcmp (bfd *abfd, Elf_Internal_Note *note) | |
8895 | { | |
8896 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-todcmp", note); | |
8897 | } | |
8898 | ||
8899 | static bfd_boolean | |
8900 | elfcore_grok_s390_todpreg (bfd *abfd, Elf_Internal_Note *note) | |
8901 | { | |
8902 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-todpreg", note); | |
8903 | } | |
8904 | ||
8905 | static bfd_boolean | |
8906 | elfcore_grok_s390_ctrs (bfd *abfd, Elf_Internal_Note *note) | |
8907 | { | |
8908 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-ctrs", note); | |
8909 | } | |
8910 | ||
8911 | static bfd_boolean | |
8912 | elfcore_grok_s390_prefix (bfd *abfd, Elf_Internal_Note *note) | |
8913 | { | |
8914 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-prefix", note); | |
8915 | } | |
8916 | ||
355b81d9 UW |
8917 | static bfd_boolean |
8918 | elfcore_grok_s390_last_break (bfd *abfd, Elf_Internal_Note *note) | |
8919 | { | |
8920 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-last-break", note); | |
8921 | } | |
8922 | ||
8923 | static bfd_boolean | |
8924 | elfcore_grok_s390_system_call (bfd *abfd, Elf_Internal_Note *note) | |
8925 | { | |
8926 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-system-call", note); | |
8927 | } | |
8928 | ||
abb3f6cc NC |
8929 | static bfd_boolean |
8930 | elfcore_grok_s390_tdb (bfd *abfd, Elf_Internal_Note *note) | |
8931 | { | |
8932 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-tdb", note); | |
8933 | } | |
8934 | ||
4ef9f41a AA |
8935 | static bfd_boolean |
8936 | elfcore_grok_s390_vxrs_low (bfd *abfd, Elf_Internal_Note *note) | |
8937 | { | |
8938 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-vxrs-low", note); | |
8939 | } | |
8940 | ||
8941 | static bfd_boolean | |
8942 | elfcore_grok_s390_vxrs_high (bfd *abfd, Elf_Internal_Note *note) | |
8943 | { | |
8944 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-vxrs-high", note); | |
8945 | } | |
8946 | ||
faa9a424 UW |
8947 | static bfd_boolean |
8948 | elfcore_grok_arm_vfp (bfd *abfd, Elf_Internal_Note *note) | |
8949 | { | |
8950 | return elfcore_make_note_pseudosection (abfd, ".reg-arm-vfp", note); | |
8951 | } | |
8952 | ||
652451f8 YZ |
8953 | static bfd_boolean |
8954 | elfcore_grok_aarch_tls (bfd *abfd, Elf_Internal_Note *note) | |
8955 | { | |
8956 | return elfcore_make_note_pseudosection (abfd, ".reg-aarch-tls", note); | |
8957 | } | |
8958 | ||
8959 | static bfd_boolean | |
8960 | elfcore_grok_aarch_hw_break (bfd *abfd, Elf_Internal_Note *note) | |
8961 | { | |
8962 | return elfcore_make_note_pseudosection (abfd, ".reg-aarch-hw-break", note); | |
8963 | } | |
8964 | ||
8965 | static bfd_boolean | |
8966 | elfcore_grok_aarch_hw_watch (bfd *abfd, Elf_Internal_Note *note) | |
8967 | { | |
8968 | return elfcore_make_note_pseudosection (abfd, ".reg-aarch-hw-watch", note); | |
8969 | } | |
8970 | ||
252b5132 | 8971 | #if defined (HAVE_PRPSINFO_T) |
4a938328 | 8972 | typedef prpsinfo_t elfcore_psinfo_t; |
7ee38065 | 8973 | #if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */ |
4a938328 MS |
8974 | typedef prpsinfo32_t elfcore_psinfo32_t; |
8975 | #endif | |
252b5132 RH |
8976 | #endif |
8977 | ||
8978 | #if defined (HAVE_PSINFO_T) | |
4a938328 | 8979 | typedef psinfo_t elfcore_psinfo_t; |
7ee38065 | 8980 | #if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */ |
4a938328 MS |
8981 | typedef psinfo32_t elfcore_psinfo32_t; |
8982 | #endif | |
252b5132 RH |
8983 | #endif |
8984 | ||
252b5132 RH |
8985 | /* return a malloc'ed copy of a string at START which is at |
8986 | most MAX bytes long, possibly without a terminating '\0'. | |
c044fabd | 8987 | the copy will always have a terminating '\0'. */ |
252b5132 | 8988 | |
936e320b | 8989 | char * |
217aa764 | 8990 | _bfd_elfcore_strndup (bfd *abfd, char *start, size_t max) |
252b5132 | 8991 | { |
dc810e39 | 8992 | char *dups; |
a50b1753 | 8993 | char *end = (char *) memchr (start, '\0', max); |
dc810e39 | 8994 | size_t len; |
252b5132 RH |
8995 | |
8996 | if (end == NULL) | |
8997 | len = max; | |
8998 | else | |
8999 | len = end - start; | |
9000 | ||
a50b1753 | 9001 | dups = (char *) bfd_alloc (abfd, len + 1); |
dc810e39 | 9002 | if (dups == NULL) |
252b5132 RH |
9003 | return NULL; |
9004 | ||
dc810e39 AM |
9005 | memcpy (dups, start, len); |
9006 | dups[len] = '\0'; | |
252b5132 | 9007 | |
dc810e39 | 9008 | return dups; |
252b5132 RH |
9009 | } |
9010 | ||
bb0082d6 | 9011 | #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) |
b34976b6 | 9012 | static bfd_boolean |
217aa764 | 9013 | elfcore_grok_psinfo (bfd *abfd, Elf_Internal_Note *note) |
252b5132 | 9014 | { |
4a938328 MS |
9015 | if (note->descsz == sizeof (elfcore_psinfo_t)) |
9016 | { | |
9017 | elfcore_psinfo_t psinfo; | |
252b5132 | 9018 | |
7ee38065 | 9019 | memcpy (&psinfo, note->descdata, sizeof (psinfo)); |
252b5132 | 9020 | |
335e41d4 | 9021 | #if defined (HAVE_PSINFO_T_PR_PID) || defined (HAVE_PRPSINFO_T_PR_PID) |
228e534f | 9022 | elf_tdata (abfd)->core->pid = psinfo.pr_pid; |
335e41d4 | 9023 | #endif |
228e534f | 9024 | elf_tdata (abfd)->core->program |
936e320b AM |
9025 | = _bfd_elfcore_strndup (abfd, psinfo.pr_fname, |
9026 | sizeof (psinfo.pr_fname)); | |
252b5132 | 9027 | |
228e534f | 9028 | elf_tdata (abfd)->core->command |
936e320b AM |
9029 | = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs, |
9030 | sizeof (psinfo.pr_psargs)); | |
4a938328 | 9031 | } |
7ee38065 | 9032 | #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T) |
4a938328 MS |
9033 | else if (note->descsz == sizeof (elfcore_psinfo32_t)) |
9034 | { | |
9035 | /* 64-bit host, 32-bit corefile */ | |
9036 | elfcore_psinfo32_t psinfo; | |
9037 | ||
7ee38065 | 9038 | memcpy (&psinfo, note->descdata, sizeof (psinfo)); |
252b5132 | 9039 | |
335e41d4 | 9040 | #if defined (HAVE_PSINFO32_T_PR_PID) || defined (HAVE_PRPSINFO32_T_PR_PID) |
228e534f | 9041 | elf_tdata (abfd)->core->pid = psinfo.pr_pid; |
335e41d4 | 9042 | #endif |
228e534f | 9043 | elf_tdata (abfd)->core->program |
936e320b AM |
9044 | = _bfd_elfcore_strndup (abfd, psinfo.pr_fname, |
9045 | sizeof (psinfo.pr_fname)); | |
4a938328 | 9046 | |
228e534f | 9047 | elf_tdata (abfd)->core->command |
936e320b AM |
9048 | = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs, |
9049 | sizeof (psinfo.pr_psargs)); | |
4a938328 MS |
9050 | } |
9051 | #endif | |
9052 | ||
9053 | else | |
9054 | { | |
9055 | /* Fail - we don't know how to handle any other | |
9056 | note size (ie. data object type). */ | |
b34976b6 | 9057 | return TRUE; |
4a938328 | 9058 | } |
252b5132 RH |
9059 | |
9060 | /* Note that for some reason, a spurious space is tacked | |
9061 | onto the end of the args in some (at least one anyway) | |
c044fabd | 9062 | implementations, so strip it off if it exists. */ |
252b5132 RH |
9063 | |
9064 | { | |
228e534f | 9065 | char *command = elf_tdata (abfd)->core->command; |
252b5132 RH |
9066 | int n = strlen (command); |
9067 | ||
9068 | if (0 < n && command[n - 1] == ' ') | |
9069 | command[n - 1] = '\0'; | |
9070 | } | |
9071 | ||
b34976b6 | 9072 | return TRUE; |
252b5132 RH |
9073 | } |
9074 | #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */ | |
9075 | ||
252b5132 | 9076 | #if defined (HAVE_PSTATUS_T) |
b34976b6 | 9077 | static bfd_boolean |
217aa764 | 9078 | elfcore_grok_pstatus (bfd *abfd, Elf_Internal_Note *note) |
252b5132 | 9079 | { |
f572a39d AM |
9080 | if (note->descsz == sizeof (pstatus_t) |
9081 | #if defined (HAVE_PXSTATUS_T) | |
9082 | || note->descsz == sizeof (pxstatus_t) | |
9083 | #endif | |
9084 | ) | |
4a938328 MS |
9085 | { |
9086 | pstatus_t pstat; | |
252b5132 | 9087 | |
4a938328 | 9088 | memcpy (&pstat, note->descdata, sizeof (pstat)); |
252b5132 | 9089 | |
228e534f | 9090 | elf_tdata (abfd)->core->pid = pstat.pr_pid; |
4a938328 | 9091 | } |
7ee38065 | 9092 | #if defined (HAVE_PSTATUS32_T) |
4a938328 MS |
9093 | else if (note->descsz == sizeof (pstatus32_t)) |
9094 | { | |
9095 | /* 64-bit host, 32-bit corefile */ | |
9096 | pstatus32_t pstat; | |
252b5132 | 9097 | |
4a938328 | 9098 | memcpy (&pstat, note->descdata, sizeof (pstat)); |
252b5132 | 9099 | |
228e534f | 9100 | elf_tdata (abfd)->core->pid = pstat.pr_pid; |
4a938328 MS |
9101 | } |
9102 | #endif | |
252b5132 RH |
9103 | /* Could grab some more details from the "representative" |
9104 | lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an | |
c044fabd | 9105 | NT_LWPSTATUS note, presumably. */ |
252b5132 | 9106 | |
b34976b6 | 9107 | return TRUE; |
252b5132 RH |
9108 | } |
9109 | #endif /* defined (HAVE_PSTATUS_T) */ | |
9110 | ||
252b5132 | 9111 | #if defined (HAVE_LWPSTATUS_T) |
b34976b6 | 9112 | static bfd_boolean |
217aa764 | 9113 | elfcore_grok_lwpstatus (bfd *abfd, Elf_Internal_Note *note) |
252b5132 RH |
9114 | { |
9115 | lwpstatus_t lwpstat; | |
9116 | char buf[100]; | |
c044fabd | 9117 | char *name; |
d4c88bbb | 9118 | size_t len; |
c044fabd | 9119 | asection *sect; |
252b5132 | 9120 | |
f572a39d AM |
9121 | if (note->descsz != sizeof (lwpstat) |
9122 | #if defined (HAVE_LWPXSTATUS_T) | |
9123 | && note->descsz != sizeof (lwpxstatus_t) | |
9124 | #endif | |
9125 | ) | |
b34976b6 | 9126 | return TRUE; |
252b5132 RH |
9127 | |
9128 | memcpy (&lwpstat, note->descdata, sizeof (lwpstat)); | |
9129 | ||
228e534f | 9130 | elf_tdata (abfd)->core->lwpid = lwpstat.pr_lwpid; |
a1504221 JB |
9131 | /* Do not overwrite the core signal if it has already been set by |
9132 | another thread. */ | |
228e534f AM |
9133 | if (elf_tdata (abfd)->core->signal == 0) |
9134 | elf_tdata (abfd)->core->signal = lwpstat.pr_cursig; | |
252b5132 | 9135 | |
c044fabd | 9136 | /* Make a ".reg/999" section. */ |
252b5132 RH |
9137 | |
9138 | sprintf (buf, ".reg/%d", elfcore_make_pid (abfd)); | |
d4c88bbb | 9139 | len = strlen (buf) + 1; |
217aa764 | 9140 | name = bfd_alloc (abfd, len); |
252b5132 | 9141 | if (name == NULL) |
b34976b6 | 9142 | return FALSE; |
d4c88bbb | 9143 | memcpy (name, buf, len); |
252b5132 | 9144 | |
117ed4f8 | 9145 | sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); |
252b5132 | 9146 | if (sect == NULL) |
b34976b6 | 9147 | return FALSE; |
252b5132 RH |
9148 | |
9149 | #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT) | |
eea6121a | 9150 | sect->size = sizeof (lwpstat.pr_context.uc_mcontext.gregs); |
252b5132 RH |
9151 | sect->filepos = note->descpos |
9152 | + offsetof (lwpstatus_t, pr_context.uc_mcontext.gregs); | |
9153 | #endif | |
9154 | ||
9155 | #if defined (HAVE_LWPSTATUS_T_PR_REG) | |
eea6121a | 9156 | sect->size = sizeof (lwpstat.pr_reg); |
252b5132 RH |
9157 | sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_reg); |
9158 | #endif | |
9159 | ||
252b5132 RH |
9160 | sect->alignment_power = 2; |
9161 | ||
9162 | if (!elfcore_maybe_make_sect (abfd, ".reg", sect)) | |
b34976b6 | 9163 | return FALSE; |
252b5132 RH |
9164 | |
9165 | /* Make a ".reg2/999" section */ | |
9166 | ||
9167 | sprintf (buf, ".reg2/%d", elfcore_make_pid (abfd)); | |
d4c88bbb | 9168 | len = strlen (buf) + 1; |
217aa764 | 9169 | name = bfd_alloc (abfd, len); |
252b5132 | 9170 | if (name == NULL) |
b34976b6 | 9171 | return FALSE; |
d4c88bbb | 9172 | memcpy (name, buf, len); |
252b5132 | 9173 | |
117ed4f8 | 9174 | sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); |
252b5132 | 9175 | if (sect == NULL) |
b34976b6 | 9176 | return FALSE; |
252b5132 RH |
9177 | |
9178 | #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT) | |
eea6121a | 9179 | sect->size = sizeof (lwpstat.pr_context.uc_mcontext.fpregs); |
252b5132 RH |
9180 | sect->filepos = note->descpos |
9181 | + offsetof (lwpstatus_t, pr_context.uc_mcontext.fpregs); | |
9182 | #endif | |
9183 | ||
9184 | #if defined (HAVE_LWPSTATUS_T_PR_FPREG) | |
eea6121a | 9185 | sect->size = sizeof (lwpstat.pr_fpreg); |
252b5132 RH |
9186 | sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_fpreg); |
9187 | #endif | |
9188 | ||
252b5132 RH |
9189 | sect->alignment_power = 2; |
9190 | ||
936e320b | 9191 | return elfcore_maybe_make_sect (abfd, ".reg2", sect); |
252b5132 RH |
9192 | } |
9193 | #endif /* defined (HAVE_LWPSTATUS_T) */ | |
9194 | ||
b34976b6 | 9195 | static bfd_boolean |
217aa764 | 9196 | elfcore_grok_win32pstatus (bfd *abfd, Elf_Internal_Note *note) |
16e9c715 NC |
9197 | { |
9198 | char buf[30]; | |
c044fabd | 9199 | char *name; |
d4c88bbb | 9200 | size_t len; |
c044fabd | 9201 | asection *sect; |
4a6636fb PA |
9202 | int type; |
9203 | int is_active_thread; | |
9204 | bfd_vma base_addr; | |
16e9c715 | 9205 | |
4a6636fb | 9206 | if (note->descsz < 728) |
b34976b6 | 9207 | return TRUE; |
16e9c715 | 9208 | |
4a6636fb PA |
9209 | if (! CONST_STRNEQ (note->namedata, "win32")) |
9210 | return TRUE; | |
9211 | ||
9212 | type = bfd_get_32 (abfd, note->descdata); | |
c044fabd | 9213 | |
4a6636fb | 9214 | switch (type) |
16e9c715 | 9215 | { |
4a6636fb | 9216 | case 1 /* NOTE_INFO_PROCESS */: |
228e534f | 9217 | /* FIXME: need to add ->core->command. */ |
4a6636fb | 9218 | /* process_info.pid */ |
228e534f | 9219 | elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, note->descdata + 8); |
4a6636fb | 9220 | /* process_info.signal */ |
228e534f | 9221 | elf_tdata (abfd)->core->signal = bfd_get_32 (abfd, note->descdata + 12); |
c044fabd | 9222 | break; |
16e9c715 | 9223 | |
4a6636fb | 9224 | case 2 /* NOTE_INFO_THREAD */: |
16e9c715 | 9225 | /* Make a ".reg/999" section. */ |
4a6636fb PA |
9226 | /* thread_info.tid */ |
9227 | sprintf (buf, ".reg/%ld", (long) bfd_get_32 (abfd, note->descdata + 8)); | |
c044fabd | 9228 | |
d4c88bbb | 9229 | len = strlen (buf) + 1; |
a50b1753 | 9230 | name = (char *) bfd_alloc (abfd, len); |
16e9c715 | 9231 | if (name == NULL) |
b34976b6 | 9232 | return FALSE; |
c044fabd | 9233 | |
d4c88bbb | 9234 | memcpy (name, buf, len); |
16e9c715 | 9235 | |
117ed4f8 | 9236 | sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); |
16e9c715 | 9237 | if (sect == NULL) |
b34976b6 | 9238 | return FALSE; |
c044fabd | 9239 | |
4a6636fb PA |
9240 | /* sizeof (thread_info.thread_context) */ |
9241 | sect->size = 716; | |
9242 | /* offsetof (thread_info.thread_context) */ | |
9243 | sect->filepos = note->descpos + 12; | |
16e9c715 NC |
9244 | sect->alignment_power = 2; |
9245 | ||
4a6636fb PA |
9246 | /* thread_info.is_active_thread */ |
9247 | is_active_thread = bfd_get_32 (abfd, note->descdata + 8); | |
9248 | ||
9249 | if (is_active_thread) | |
16e9c715 | 9250 | if (! elfcore_maybe_make_sect (abfd, ".reg", sect)) |
b34976b6 | 9251 | return FALSE; |
16e9c715 NC |
9252 | break; |
9253 | ||
4a6636fb | 9254 | case 3 /* NOTE_INFO_MODULE */: |
16e9c715 | 9255 | /* Make a ".module/xxxxxxxx" section. */ |
4a6636fb PA |
9256 | /* module_info.base_address */ |
9257 | base_addr = bfd_get_32 (abfd, note->descdata + 4); | |
0af1713e | 9258 | sprintf (buf, ".module/%08lx", (unsigned long) base_addr); |
c044fabd | 9259 | |
d4c88bbb | 9260 | len = strlen (buf) + 1; |
a50b1753 | 9261 | name = (char *) bfd_alloc (abfd, len); |
16e9c715 | 9262 | if (name == NULL) |
b34976b6 | 9263 | return FALSE; |
c044fabd | 9264 | |
d4c88bbb | 9265 | memcpy (name, buf, len); |
252b5132 | 9266 | |
117ed4f8 | 9267 | sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); |
c044fabd | 9268 | |
16e9c715 | 9269 | if (sect == NULL) |
b34976b6 | 9270 | return FALSE; |
c044fabd | 9271 | |
eea6121a | 9272 | sect->size = note->descsz; |
16e9c715 | 9273 | sect->filepos = note->descpos; |
16e9c715 NC |
9274 | sect->alignment_power = 2; |
9275 | break; | |
9276 | ||
9277 | default: | |
b34976b6 | 9278 | return TRUE; |
16e9c715 NC |
9279 | } |
9280 | ||
b34976b6 | 9281 | return TRUE; |
16e9c715 | 9282 | } |
252b5132 | 9283 | |
b34976b6 | 9284 | static bfd_boolean |
217aa764 | 9285 | elfcore_grok_note (bfd *abfd, Elf_Internal_Note *note) |
252b5132 | 9286 | { |
9c5bfbb7 | 9287 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
bb0082d6 | 9288 | |
252b5132 RH |
9289 | switch (note->type) |
9290 | { | |
9291 | default: | |
b34976b6 | 9292 | return TRUE; |
252b5132 | 9293 | |
252b5132 | 9294 | case NT_PRSTATUS: |
bb0082d6 AM |
9295 | if (bed->elf_backend_grok_prstatus) |
9296 | if ((*bed->elf_backend_grok_prstatus) (abfd, note)) | |
b34976b6 | 9297 | return TRUE; |
bb0082d6 | 9298 | #if defined (HAVE_PRSTATUS_T) |
252b5132 | 9299 | return elfcore_grok_prstatus (abfd, note); |
bb0082d6 | 9300 | #else |
b34976b6 | 9301 | return TRUE; |
252b5132 RH |
9302 | #endif |
9303 | ||
9304 | #if defined (HAVE_PSTATUS_T) | |
9305 | case NT_PSTATUS: | |
9306 | return elfcore_grok_pstatus (abfd, note); | |
9307 | #endif | |
9308 | ||
9309 | #if defined (HAVE_LWPSTATUS_T) | |
9310 | case NT_LWPSTATUS: | |
9311 | return elfcore_grok_lwpstatus (abfd, note); | |
9312 | #endif | |
9313 | ||
9314 | case NT_FPREGSET: /* FIXME: rename to NT_PRFPREG */ | |
9315 | return elfcore_grok_prfpreg (abfd, note); | |
9316 | ||
c044fabd | 9317 | case NT_WIN32PSTATUS: |
16e9c715 | 9318 | return elfcore_grok_win32pstatus (abfd, note); |
16e9c715 | 9319 | |
c044fabd | 9320 | case NT_PRXFPREG: /* Linux SSE extension */ |
e377ab71 MK |
9321 | if (note->namesz == 6 |
9322 | && strcmp (note->namedata, "LINUX") == 0) | |
ff08c6bb JB |
9323 | return elfcore_grok_prxfpreg (abfd, note); |
9324 | else | |
b34976b6 | 9325 | return TRUE; |
ff08c6bb | 9326 | |
4339cae0 L |
9327 | case NT_X86_XSTATE: /* Linux XSAVE extension */ |
9328 | if (note->namesz == 6 | |
9329 | && strcmp (note->namedata, "LINUX") == 0) | |
9330 | return elfcore_grok_xstatereg (abfd, note); | |
97de3545 JB |
9331 | else if (note->namesz == 8 |
9332 | && strcmp (note->namedata, "FreeBSD") == 0) | |
9333 | return elfcore_grok_xstatereg (abfd, note); | |
4339cae0 L |
9334 | else |
9335 | return TRUE; | |
9336 | ||
97753bd5 AM |
9337 | case NT_PPC_VMX: |
9338 | if (note->namesz == 6 | |
9339 | && strcmp (note->namedata, "LINUX") == 0) | |
9340 | return elfcore_grok_ppc_vmx (abfd, note); | |
9341 | else | |
9342 | return TRUE; | |
9343 | ||
89eeb0bc LM |
9344 | case NT_PPC_VSX: |
9345 | if (note->namesz == 6 | |
9346 | && strcmp (note->namedata, "LINUX") == 0) | |
9347 | return elfcore_grok_ppc_vsx (abfd, note); | |
9348 | else | |
9349 | return TRUE; | |
9350 | ||
0675e188 UW |
9351 | case NT_S390_HIGH_GPRS: |
9352 | if (note->namesz == 6 | |
9353 | && strcmp (note->namedata, "LINUX") == 0) | |
9354 | return elfcore_grok_s390_high_gprs (abfd, note); | |
9355 | else | |
9356 | return TRUE; | |
9357 | ||
d7eeb400 MS |
9358 | case NT_S390_TIMER: |
9359 | if (note->namesz == 6 | |
9360 | && strcmp (note->namedata, "LINUX") == 0) | |
9361 | return elfcore_grok_s390_timer (abfd, note); | |
9362 | else | |
9363 | return TRUE; | |
9364 | ||
9365 | case NT_S390_TODCMP: | |
9366 | if (note->namesz == 6 | |
9367 | && strcmp (note->namedata, "LINUX") == 0) | |
9368 | return elfcore_grok_s390_todcmp (abfd, note); | |
9369 | else | |
9370 | return TRUE; | |
9371 | ||
9372 | case NT_S390_TODPREG: | |
9373 | if (note->namesz == 6 | |
9374 | && strcmp (note->namedata, "LINUX") == 0) | |
9375 | return elfcore_grok_s390_todpreg (abfd, note); | |
9376 | else | |
9377 | return TRUE; | |
9378 | ||
9379 | case NT_S390_CTRS: | |
9380 | if (note->namesz == 6 | |
9381 | && strcmp (note->namedata, "LINUX") == 0) | |
9382 | return elfcore_grok_s390_ctrs (abfd, note); | |
9383 | else | |
9384 | return TRUE; | |
9385 | ||
9386 | case NT_S390_PREFIX: | |
9387 | if (note->namesz == 6 | |
9388 | && strcmp (note->namedata, "LINUX") == 0) | |
9389 | return elfcore_grok_s390_prefix (abfd, note); | |
9390 | else | |
9391 | return TRUE; | |
9392 | ||
355b81d9 UW |
9393 | case NT_S390_LAST_BREAK: |
9394 | if (note->namesz == 6 | |
9395 | && strcmp (note->namedata, "LINUX") == 0) | |
9396 | return elfcore_grok_s390_last_break (abfd, note); | |
9397 | else | |
9398 | return TRUE; | |
9399 | ||
9400 | case NT_S390_SYSTEM_CALL: | |
9401 | if (note->namesz == 6 | |
9402 | && strcmp (note->namedata, "LINUX") == 0) | |
9403 | return elfcore_grok_s390_system_call (abfd, note); | |
9404 | else | |
9405 | return TRUE; | |
9406 | ||
abb3f6cc NC |
9407 | case NT_S390_TDB: |
9408 | if (note->namesz == 6 | |
9409 | && strcmp (note->namedata, "LINUX") == 0) | |
9410 | return elfcore_grok_s390_tdb (abfd, note); | |
9411 | else | |
9412 | return TRUE; | |
9413 | ||
4ef9f41a AA |
9414 | case NT_S390_VXRS_LOW: |
9415 | if (note->namesz == 6 | |
9416 | && strcmp (note->namedata, "LINUX") == 0) | |
9417 | return elfcore_grok_s390_vxrs_low (abfd, note); | |
9418 | else | |
9419 | return TRUE; | |
9420 | ||
9421 | case NT_S390_VXRS_HIGH: | |
9422 | if (note->namesz == 6 | |
9423 | && strcmp (note->namedata, "LINUX") == 0) | |
9424 | return elfcore_grok_s390_vxrs_high (abfd, note); | |
9425 | else | |
9426 | return TRUE; | |
9427 | ||
faa9a424 UW |
9428 | case NT_ARM_VFP: |
9429 | if (note->namesz == 6 | |
9430 | && strcmp (note->namedata, "LINUX") == 0) | |
9431 | return elfcore_grok_arm_vfp (abfd, note); | |
9432 | else | |
9433 | return TRUE; | |
9434 | ||
652451f8 YZ |
9435 | case NT_ARM_TLS: |
9436 | if (note->namesz == 6 | |
9437 | && strcmp (note->namedata, "LINUX") == 0) | |
9438 | return elfcore_grok_aarch_tls (abfd, note); | |
9439 | else | |
9440 | return TRUE; | |
9441 | ||
9442 | case NT_ARM_HW_BREAK: | |
9443 | if (note->namesz == 6 | |
9444 | && strcmp (note->namedata, "LINUX") == 0) | |
9445 | return elfcore_grok_aarch_hw_break (abfd, note); | |
9446 | else | |
9447 | return TRUE; | |
9448 | ||
9449 | case NT_ARM_HW_WATCH: | |
9450 | if (note->namesz == 6 | |
9451 | && strcmp (note->namedata, "LINUX") == 0) | |
9452 | return elfcore_grok_aarch_hw_watch (abfd, note); | |
9453 | else | |
9454 | return TRUE; | |
9455 | ||
252b5132 RH |
9456 | case NT_PRPSINFO: |
9457 | case NT_PSINFO: | |
bb0082d6 AM |
9458 | if (bed->elf_backend_grok_psinfo) |
9459 | if ((*bed->elf_backend_grok_psinfo) (abfd, note)) | |
b34976b6 | 9460 | return TRUE; |
bb0082d6 | 9461 | #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) |
252b5132 | 9462 | return elfcore_grok_psinfo (abfd, note); |
bb0082d6 | 9463 | #else |
b34976b6 | 9464 | return TRUE; |
252b5132 | 9465 | #endif |
3333a7c3 RM |
9466 | |
9467 | case NT_AUXV: | |
9468 | { | |
117ed4f8 AM |
9469 | asection *sect = bfd_make_section_anyway_with_flags (abfd, ".auxv", |
9470 | SEC_HAS_CONTENTS); | |
3333a7c3 RM |
9471 | |
9472 | if (sect == NULL) | |
9473 | return FALSE; | |
eea6121a | 9474 | sect->size = note->descsz; |
3333a7c3 | 9475 | sect->filepos = note->descpos; |
3333a7c3 RM |
9476 | sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32; |
9477 | ||
9478 | return TRUE; | |
9479 | } | |
9015683b | 9480 | |
451b7c33 TT |
9481 | case NT_FILE: |
9482 | return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.file", | |
9483 | note); | |
9484 | ||
9015683b TT |
9485 | case NT_SIGINFO: |
9486 | return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.siginfo", | |
9487 | note); | |
5b2c414d JB |
9488 | |
9489 | case NT_FREEBSD_THRMISC: | |
9490 | if (note->namesz == 8 | |
9491 | && strcmp (note->namedata, "FreeBSD") == 0) | |
9492 | return elfcore_make_note_pseudosection (abfd, ".thrmisc", note); | |
9493 | else | |
9494 | return TRUE; | |
252b5132 RH |
9495 | } |
9496 | } | |
9497 | ||
718175fa JK |
9498 | static bfd_boolean |
9499 | elfobj_grok_gnu_build_id (bfd *abfd, Elf_Internal_Note *note) | |
9500 | { | |
c74f7d1c | 9501 | struct bfd_build_id* build_id; |
30e8ee25 AM |
9502 | |
9503 | if (note->descsz == 0) | |
9504 | return FALSE; | |
9505 | ||
c74f7d1c JT |
9506 | build_id = bfd_alloc (abfd, sizeof (struct bfd_build_id) - 1 + note->descsz); |
9507 | if (build_id == NULL) | |
718175fa JK |
9508 | return FALSE; |
9509 | ||
c74f7d1c JT |
9510 | build_id->size = note->descsz; |
9511 | memcpy (build_id->data, note->descdata, note->descsz); | |
9512 | abfd->build_id = build_id; | |
718175fa JK |
9513 | |
9514 | return TRUE; | |
9515 | } | |
9516 | ||
9517 | static bfd_boolean | |
9518 | elfobj_grok_gnu_note (bfd *abfd, Elf_Internal_Note *note) | |
9519 | { | |
9520 | switch (note->type) | |
9521 | { | |
9522 | default: | |
9523 | return TRUE; | |
9524 | ||
9525 | case NT_GNU_BUILD_ID: | |
9526 | return elfobj_grok_gnu_build_id (abfd, note); | |
9527 | } | |
9528 | } | |
9529 | ||
e21e5835 NC |
9530 | static bfd_boolean |
9531 | elfobj_grok_stapsdt_note_1 (bfd *abfd, Elf_Internal_Note *note) | |
9532 | { | |
9533 | struct sdt_note *cur = | |
9534 | (struct sdt_note *) bfd_alloc (abfd, sizeof (struct sdt_note) | |
9535 | + note->descsz); | |
9536 | ||
9537 | cur->next = (struct sdt_note *) (elf_tdata (abfd))->sdt_note_head; | |
9538 | cur->size = (bfd_size_type) note->descsz; | |
9539 | memcpy (cur->data, note->descdata, note->descsz); | |
9540 | ||
9541 | elf_tdata (abfd)->sdt_note_head = cur; | |
9542 | ||
9543 | return TRUE; | |
9544 | } | |
9545 | ||
9546 | static bfd_boolean | |
9547 | elfobj_grok_stapsdt_note (bfd *abfd, Elf_Internal_Note *note) | |
9548 | { | |
9549 | switch (note->type) | |
9550 | { | |
9551 | case NT_STAPSDT: | |
9552 | return elfobj_grok_stapsdt_note_1 (abfd, note); | |
9553 | ||
9554 | default: | |
9555 | return TRUE; | |
9556 | } | |
9557 | } | |
9558 | ||
b34976b6 | 9559 | static bfd_boolean |
217aa764 | 9560 | elfcore_netbsd_get_lwpid (Elf_Internal_Note *note, int *lwpidp) |
50b2bdb7 AM |
9561 | { |
9562 | char *cp; | |
9563 | ||
9564 | cp = strchr (note->namedata, '@'); | |
9565 | if (cp != NULL) | |
9566 | { | |
d2b64500 | 9567 | *lwpidp = atoi(cp + 1); |
b34976b6 | 9568 | return TRUE; |
50b2bdb7 | 9569 | } |
b34976b6 | 9570 | return FALSE; |
50b2bdb7 AM |
9571 | } |
9572 | ||
b34976b6 | 9573 | static bfd_boolean |
217aa764 | 9574 | elfcore_grok_netbsd_procinfo (bfd *abfd, Elf_Internal_Note *note) |
50b2bdb7 | 9575 | { |
50b2bdb7 | 9576 | /* Signal number at offset 0x08. */ |
228e534f | 9577 | elf_tdata (abfd)->core->signal |
50b2bdb7 AM |
9578 | = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08); |
9579 | ||
9580 | /* Process ID at offset 0x50. */ | |
228e534f | 9581 | elf_tdata (abfd)->core->pid |
50b2bdb7 AM |
9582 | = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x50); |
9583 | ||
9584 | /* Command name at 0x7c (max 32 bytes, including nul). */ | |
228e534f | 9585 | elf_tdata (abfd)->core->command |
50b2bdb7 AM |
9586 | = _bfd_elfcore_strndup (abfd, note->descdata + 0x7c, 31); |
9587 | ||
7720ba9f MK |
9588 | return elfcore_make_note_pseudosection (abfd, ".note.netbsdcore.procinfo", |
9589 | note); | |
50b2bdb7 AM |
9590 | } |
9591 | ||
b34976b6 | 9592 | static bfd_boolean |
217aa764 | 9593 | elfcore_grok_netbsd_note (bfd *abfd, Elf_Internal_Note *note) |
50b2bdb7 AM |
9594 | { |
9595 | int lwp; | |
9596 | ||
9597 | if (elfcore_netbsd_get_lwpid (note, &lwp)) | |
228e534f | 9598 | elf_tdata (abfd)->core->lwpid = lwp; |
50b2bdb7 | 9599 | |
b4db1224 | 9600 | if (note->type == NT_NETBSDCORE_PROCINFO) |
50b2bdb7 AM |
9601 | { |
9602 | /* NetBSD-specific core "procinfo". Note that we expect to | |
08a40648 AM |
9603 | find this note before any of the others, which is fine, |
9604 | since the kernel writes this note out first when it | |
9605 | creates a core file. */ | |
47d9a591 | 9606 | |
50b2bdb7 AM |
9607 | return elfcore_grok_netbsd_procinfo (abfd, note); |
9608 | } | |
9609 | ||
b4db1224 JT |
9610 | /* As of Jan 2002 there are no other machine-independent notes |
9611 | defined for NetBSD core files. If the note type is less | |
9612 | than the start of the machine-dependent note types, we don't | |
9613 | understand it. */ | |
47d9a591 | 9614 | |
b4db1224 | 9615 | if (note->type < NT_NETBSDCORE_FIRSTMACH) |
b34976b6 | 9616 | return TRUE; |
50b2bdb7 AM |
9617 | |
9618 | ||
9619 | switch (bfd_get_arch (abfd)) | |
9620 | { | |
08a40648 AM |
9621 | /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and |
9622 | PT_GETFPREGS == mach+2. */ | |
50b2bdb7 AM |
9623 | |
9624 | case bfd_arch_alpha: | |
9625 | case bfd_arch_sparc: | |
9626 | switch (note->type) | |
08a40648 AM |
9627 | { |
9628 | case NT_NETBSDCORE_FIRSTMACH+0: | |
9629 | return elfcore_make_note_pseudosection (abfd, ".reg", note); | |
50b2bdb7 | 9630 | |
08a40648 AM |
9631 | case NT_NETBSDCORE_FIRSTMACH+2: |
9632 | return elfcore_make_note_pseudosection (abfd, ".reg2", note); | |
50b2bdb7 | 9633 | |
08a40648 AM |
9634 | default: |
9635 | return TRUE; | |
9636 | } | |
50b2bdb7 | 9637 | |
08a40648 AM |
9638 | /* On all other arch's, PT_GETREGS == mach+1 and |
9639 | PT_GETFPREGS == mach+3. */ | |
50b2bdb7 AM |
9640 | |
9641 | default: | |
9642 | switch (note->type) | |
08a40648 AM |
9643 | { |
9644 | case NT_NETBSDCORE_FIRSTMACH+1: | |
9645 | return elfcore_make_note_pseudosection (abfd, ".reg", note); | |
50b2bdb7 | 9646 | |
08a40648 AM |
9647 | case NT_NETBSDCORE_FIRSTMACH+3: |
9648 | return elfcore_make_note_pseudosection (abfd, ".reg2", note); | |
50b2bdb7 | 9649 | |
08a40648 AM |
9650 | default: |
9651 | return TRUE; | |
9652 | } | |
50b2bdb7 AM |
9653 | } |
9654 | /* NOTREACHED */ | |
9655 | } | |
9656 | ||
67cc5033 MK |
9657 | static bfd_boolean |
9658 | elfcore_grok_openbsd_procinfo (bfd *abfd, Elf_Internal_Note *note) | |
9659 | { | |
9660 | /* Signal number at offset 0x08. */ | |
228e534f | 9661 | elf_tdata (abfd)->core->signal |
67cc5033 MK |
9662 | = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08); |
9663 | ||
9664 | /* Process ID at offset 0x20. */ | |
228e534f | 9665 | elf_tdata (abfd)->core->pid |
67cc5033 MK |
9666 | = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x20); |
9667 | ||
9668 | /* Command name at 0x48 (max 32 bytes, including nul). */ | |
228e534f | 9669 | elf_tdata (abfd)->core->command |
67cc5033 MK |
9670 | = _bfd_elfcore_strndup (abfd, note->descdata + 0x48, 31); |
9671 | ||
9672 | return TRUE; | |
9673 | } | |
9674 | ||
9675 | static bfd_boolean | |
9676 | elfcore_grok_openbsd_note (bfd *abfd, Elf_Internal_Note *note) | |
9677 | { | |
9678 | if (note->type == NT_OPENBSD_PROCINFO) | |
9679 | return elfcore_grok_openbsd_procinfo (abfd, note); | |
9680 | ||
9681 | if (note->type == NT_OPENBSD_REGS) | |
9682 | return elfcore_make_note_pseudosection (abfd, ".reg", note); | |
9683 | ||
9684 | if (note->type == NT_OPENBSD_FPREGS) | |
9685 | return elfcore_make_note_pseudosection (abfd, ".reg2", note); | |
9686 | ||
9687 | if (note->type == NT_OPENBSD_XFPREGS) | |
9688 | return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note); | |
9689 | ||
9690 | if (note->type == NT_OPENBSD_AUXV) | |
9691 | { | |
9692 | asection *sect = bfd_make_section_anyway_with_flags (abfd, ".auxv", | |
9693 | SEC_HAS_CONTENTS); | |
9694 | ||
9695 | if (sect == NULL) | |
9696 | return FALSE; | |
9697 | sect->size = note->descsz; | |
9698 | sect->filepos = note->descpos; | |
9699 | sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32; | |
9700 | ||
9701 | return TRUE; | |
9702 | } | |
9703 | ||
9704 | if (note->type == NT_OPENBSD_WCOOKIE) | |
9705 | { | |
9706 | asection *sect = bfd_make_section_anyway_with_flags (abfd, ".wcookie", | |
9707 | SEC_HAS_CONTENTS); | |
9708 | ||
9709 | if (sect == NULL) | |
9710 | return FALSE; | |
9711 | sect->size = note->descsz; | |
9712 | sect->filepos = note->descpos; | |
9713 | sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32; | |
9714 | ||
9715 | return TRUE; | |
9716 | } | |
9717 | ||
9718 | return TRUE; | |
9719 | } | |
9720 | ||
07c6e936 | 9721 | static bfd_boolean |
d3fd4074 | 9722 | elfcore_grok_nto_status (bfd *abfd, Elf_Internal_Note *note, long *tid) |
07c6e936 NC |
9723 | { |
9724 | void *ddata = note->descdata; | |
9725 | char buf[100]; | |
9726 | char *name; | |
9727 | asection *sect; | |
f8843e87 AM |
9728 | short sig; |
9729 | unsigned flags; | |
07c6e936 NC |
9730 | |
9731 | /* nto_procfs_status 'pid' field is at offset 0. */ | |
228e534f | 9732 | elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, (bfd_byte *) ddata); |
07c6e936 | 9733 | |
f8843e87 AM |
9734 | /* nto_procfs_status 'tid' field is at offset 4. Pass it back. */ |
9735 | *tid = bfd_get_32 (abfd, (bfd_byte *) ddata + 4); | |
9736 | ||
9737 | /* nto_procfs_status 'flags' field is at offset 8. */ | |
9738 | flags = bfd_get_32 (abfd, (bfd_byte *) ddata + 8); | |
07c6e936 NC |
9739 | |
9740 | /* nto_procfs_status 'what' field is at offset 14. */ | |
f8843e87 AM |
9741 | if ((sig = bfd_get_16 (abfd, (bfd_byte *) ddata + 14)) > 0) |
9742 | { | |
228e534f AM |
9743 | elf_tdata (abfd)->core->signal = sig; |
9744 | elf_tdata (abfd)->core->lwpid = *tid; | |
f8843e87 | 9745 | } |
07c6e936 | 9746 | |
f8843e87 AM |
9747 | /* _DEBUG_FLAG_CURTID (current thread) is 0x80. Some cores |
9748 | do not come from signals so we make sure we set the current | |
9749 | thread just in case. */ | |
9750 | if (flags & 0x00000080) | |
228e534f | 9751 | elf_tdata (abfd)->core->lwpid = *tid; |
07c6e936 NC |
9752 | |
9753 | /* Make a ".qnx_core_status/%d" section. */ | |
d3fd4074 | 9754 | sprintf (buf, ".qnx_core_status/%ld", *tid); |
07c6e936 | 9755 | |
a50b1753 | 9756 | name = (char *) bfd_alloc (abfd, strlen (buf) + 1); |
07c6e936 NC |
9757 | if (name == NULL) |
9758 | return FALSE; | |
9759 | strcpy (name, buf); | |
9760 | ||
117ed4f8 | 9761 | sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); |
07c6e936 NC |
9762 | if (sect == NULL) |
9763 | return FALSE; | |
9764 | ||
eea6121a | 9765 | sect->size = note->descsz; |
07c6e936 | 9766 | sect->filepos = note->descpos; |
07c6e936 NC |
9767 | sect->alignment_power = 2; |
9768 | ||
9769 | return (elfcore_maybe_make_sect (abfd, ".qnx_core_status", sect)); | |
9770 | } | |
9771 | ||
9772 | static bfd_boolean | |
d69f560c KW |
9773 | elfcore_grok_nto_regs (bfd *abfd, |
9774 | Elf_Internal_Note *note, | |
d3fd4074 | 9775 | long tid, |
d69f560c | 9776 | char *base) |
07c6e936 NC |
9777 | { |
9778 | char buf[100]; | |
9779 | char *name; | |
9780 | asection *sect; | |
9781 | ||
d69f560c | 9782 | /* Make a "(base)/%d" section. */ |
d3fd4074 | 9783 | sprintf (buf, "%s/%ld", base, tid); |
07c6e936 | 9784 | |
a50b1753 | 9785 | name = (char *) bfd_alloc (abfd, strlen (buf) + 1); |
07c6e936 NC |
9786 | if (name == NULL) |
9787 | return FALSE; | |
9788 | strcpy (name, buf); | |
9789 | ||
117ed4f8 | 9790 | sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); |
07c6e936 NC |
9791 | if (sect == NULL) |
9792 | return FALSE; | |
9793 | ||
eea6121a | 9794 | sect->size = note->descsz; |
07c6e936 | 9795 | sect->filepos = note->descpos; |
07c6e936 NC |
9796 | sect->alignment_power = 2; |
9797 | ||
f8843e87 | 9798 | /* This is the current thread. */ |
228e534f | 9799 | if (elf_tdata (abfd)->core->lwpid == tid) |
d69f560c | 9800 | return elfcore_maybe_make_sect (abfd, base, sect); |
f8843e87 AM |
9801 | |
9802 | return TRUE; | |
07c6e936 NC |
9803 | } |
9804 | ||
9805 | #define BFD_QNT_CORE_INFO 7 | |
9806 | #define BFD_QNT_CORE_STATUS 8 | |
9807 | #define BFD_QNT_CORE_GREG 9 | |
9808 | #define BFD_QNT_CORE_FPREG 10 | |
9809 | ||
9810 | static bfd_boolean | |
217aa764 | 9811 | elfcore_grok_nto_note (bfd *abfd, Elf_Internal_Note *note) |
07c6e936 NC |
9812 | { |
9813 | /* Every GREG section has a STATUS section before it. Store the | |
811072d8 | 9814 | tid from the previous call to pass down to the next gregs |
07c6e936 | 9815 | function. */ |
d3fd4074 | 9816 | static long tid = 1; |
07c6e936 NC |
9817 | |
9818 | switch (note->type) | |
9819 | { | |
d69f560c KW |
9820 | case BFD_QNT_CORE_INFO: |
9821 | return elfcore_make_note_pseudosection (abfd, ".qnx_core_info", note); | |
9822 | case BFD_QNT_CORE_STATUS: | |
9823 | return elfcore_grok_nto_status (abfd, note, &tid); | |
9824 | case BFD_QNT_CORE_GREG: | |
9825 | return elfcore_grok_nto_regs (abfd, note, tid, ".reg"); | |
9826 | case BFD_QNT_CORE_FPREG: | |
9827 | return elfcore_grok_nto_regs (abfd, note, tid, ".reg2"); | |
9828 | default: | |
9829 | return TRUE; | |
07c6e936 NC |
9830 | } |
9831 | } | |
9832 | ||
b15fa79e AM |
9833 | static bfd_boolean |
9834 | elfcore_grok_spu_note (bfd *abfd, Elf_Internal_Note *note) | |
9835 | { | |
9836 | char *name; | |
9837 | asection *sect; | |
9838 | size_t len; | |
9839 | ||
9840 | /* Use note name as section name. */ | |
9841 | len = note->namesz; | |
a50b1753 | 9842 | name = (char *) bfd_alloc (abfd, len); |
b15fa79e AM |
9843 | if (name == NULL) |
9844 | return FALSE; | |
9845 | memcpy (name, note->namedata, len); | |
9846 | name[len - 1] = '\0'; | |
9847 | ||
9848 | sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); | |
9849 | if (sect == NULL) | |
9850 | return FALSE; | |
9851 | ||
9852 | sect->size = note->descsz; | |
9853 | sect->filepos = note->descpos; | |
9854 | sect->alignment_power = 1; | |
9855 | ||
9856 | return TRUE; | |
9857 | } | |
9858 | ||
7c76fa91 MS |
9859 | /* Function: elfcore_write_note |
9860 | ||
47d9a591 | 9861 | Inputs: |
a39f3346 | 9862 | buffer to hold note, and current size of buffer |
7c76fa91 MS |
9863 | name of note |
9864 | type of note | |
9865 | data for note | |
9866 | size of data for note | |
9867 | ||
a39f3346 AM |
9868 | Writes note to end of buffer. ELF64 notes are written exactly as |
9869 | for ELF32, despite the current (as of 2006) ELF gabi specifying | |
9870 | that they ought to have 8-byte namesz and descsz field, and have | |
9871 | 8-byte alignment. Other writers, eg. Linux kernel, do the same. | |
9872 | ||
7c76fa91 | 9873 | Return: |
a39f3346 | 9874 | Pointer to realloc'd buffer, *BUFSIZ updated. */ |
7c76fa91 MS |
9875 | |
9876 | char * | |
a39f3346 | 9877 | elfcore_write_note (bfd *abfd, |
217aa764 | 9878 | char *buf, |
a39f3346 | 9879 | int *bufsiz, |
217aa764 | 9880 | const char *name, |
a39f3346 | 9881 | int type, |
217aa764 | 9882 | const void *input, |
a39f3346 | 9883 | int size) |
7c76fa91 MS |
9884 | { |
9885 | Elf_External_Note *xnp; | |
d4c88bbb | 9886 | size_t namesz; |
d4c88bbb | 9887 | size_t newspace; |
a39f3346 | 9888 | char *dest; |
7c76fa91 | 9889 | |
d4c88bbb | 9890 | namesz = 0; |
d4c88bbb | 9891 | if (name != NULL) |
a39f3346 | 9892 | namesz = strlen (name) + 1; |
d4c88bbb | 9893 | |
a39f3346 | 9894 | newspace = 12 + ((namesz + 3) & -4) + ((size + 3) & -4); |
d4c88bbb | 9895 | |
a50b1753 | 9896 | buf = (char *) realloc (buf, *bufsiz + newspace); |
14b1c01e AM |
9897 | if (buf == NULL) |
9898 | return buf; | |
a39f3346 | 9899 | dest = buf + *bufsiz; |
7c76fa91 MS |
9900 | *bufsiz += newspace; |
9901 | xnp = (Elf_External_Note *) dest; | |
9902 | H_PUT_32 (abfd, namesz, xnp->namesz); | |
9903 | H_PUT_32 (abfd, size, xnp->descsz); | |
9904 | H_PUT_32 (abfd, type, xnp->type); | |
d4c88bbb AM |
9905 | dest = xnp->name; |
9906 | if (name != NULL) | |
9907 | { | |
9908 | memcpy (dest, name, namesz); | |
9909 | dest += namesz; | |
a39f3346 | 9910 | while (namesz & 3) |
d4c88bbb AM |
9911 | { |
9912 | *dest++ = '\0'; | |
a39f3346 | 9913 | ++namesz; |
d4c88bbb AM |
9914 | } |
9915 | } | |
9916 | memcpy (dest, input, size); | |
a39f3346 AM |
9917 | dest += size; |
9918 | while (size & 3) | |
9919 | { | |
9920 | *dest++ = '\0'; | |
9921 | ++size; | |
9922 | } | |
9923 | return buf; | |
7c76fa91 MS |
9924 | } |
9925 | ||
7c76fa91 | 9926 | char * |
217aa764 AM |
9927 | elfcore_write_prpsinfo (bfd *abfd, |
9928 | char *buf, | |
9929 | int *bufsiz, | |
9930 | const char *fname, | |
9931 | const char *psargs) | |
7c76fa91 | 9932 | { |
183e98be AM |
9933 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
9934 | ||
9935 | if (bed->elf_backend_write_core_note != NULL) | |
9936 | { | |
9937 | char *ret; | |
9938 | ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz, | |
9939 | NT_PRPSINFO, fname, psargs); | |
9940 | if (ret != NULL) | |
9941 | return ret; | |
9942 | } | |
7c76fa91 | 9943 | |
1f20dca5 | 9944 | #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) |
183e98be AM |
9945 | #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T) |
9946 | if (bed->s->elfclass == ELFCLASS32) | |
9947 | { | |
9948 | #if defined (HAVE_PSINFO32_T) | |
9949 | psinfo32_t data; | |
9950 | int note_type = NT_PSINFO; | |
9951 | #else | |
9952 | prpsinfo32_t data; | |
9953 | int note_type = NT_PRPSINFO; | |
9954 | #endif | |
9955 | ||
9956 | memset (&data, 0, sizeof (data)); | |
9957 | strncpy (data.pr_fname, fname, sizeof (data.pr_fname)); | |
9958 | strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs)); | |
9959 | return elfcore_write_note (abfd, buf, bufsiz, | |
1f20dca5 | 9960 | "CORE", note_type, &data, sizeof (data)); |
183e98be AM |
9961 | } |
9962 | else | |
9963 | #endif | |
9964 | { | |
7c76fa91 | 9965 | #if defined (HAVE_PSINFO_T) |
183e98be AM |
9966 | psinfo_t data; |
9967 | int note_type = NT_PSINFO; | |
7c76fa91 | 9968 | #else |
183e98be AM |
9969 | prpsinfo_t data; |
9970 | int note_type = NT_PRPSINFO; | |
7c76fa91 MS |
9971 | #endif |
9972 | ||
183e98be AM |
9973 | memset (&data, 0, sizeof (data)); |
9974 | strncpy (data.pr_fname, fname, sizeof (data.pr_fname)); | |
9975 | strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs)); | |
9976 | return elfcore_write_note (abfd, buf, bufsiz, | |
1f20dca5 | 9977 | "CORE", note_type, &data, sizeof (data)); |
183e98be | 9978 | } |
7c76fa91 MS |
9979 | #endif /* PSINFO_T or PRPSINFO_T */ |
9980 | ||
1f20dca5 UW |
9981 | free (buf); |
9982 | return NULL; | |
9983 | } | |
9984 | ||
70a38d42 SDJ |
9985 | char * |
9986 | elfcore_write_linux_prpsinfo32 | |
9987 | (bfd *abfd, char *buf, int *bufsiz, | |
9988 | const struct elf_internal_linux_prpsinfo *prpsinfo) | |
9989 | { | |
9990 | struct elf_external_linux_prpsinfo32 data; | |
9991 | ||
aeb70569 | 9992 | swap_linux_prpsinfo32_out (abfd, prpsinfo, &data); |
70a38d42 SDJ |
9993 | return elfcore_write_note (abfd, buf, bufsiz, "CORE", NT_PRPSINFO, |
9994 | &data, sizeof (data)); | |
9995 | } | |
9996 | ||
9997 | char * | |
9998 | elfcore_write_linux_prpsinfo64 | |
9999 | (bfd *abfd, char *buf, int *bufsiz, | |
10000 | const struct elf_internal_linux_prpsinfo *prpsinfo) | |
10001 | { | |
10002 | struct elf_external_linux_prpsinfo64 data; | |
10003 | ||
aeb70569 | 10004 | swap_linux_prpsinfo64_out (abfd, prpsinfo, &data); |
70a38d42 SDJ |
10005 | return elfcore_write_note (abfd, buf, bufsiz, |
10006 | "CORE", NT_PRPSINFO, &data, sizeof (data)); | |
10007 | } | |
10008 | ||
7c76fa91 | 10009 | char * |
217aa764 AM |
10010 | elfcore_write_prstatus (bfd *abfd, |
10011 | char *buf, | |
10012 | int *bufsiz, | |
10013 | long pid, | |
10014 | int cursig, | |
10015 | const void *gregs) | |
7c76fa91 | 10016 | { |
183e98be | 10017 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
7c76fa91 | 10018 | |
183e98be AM |
10019 | if (bed->elf_backend_write_core_note != NULL) |
10020 | { | |
10021 | char *ret; | |
10022 | ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz, | |
10023 | NT_PRSTATUS, | |
10024 | pid, cursig, gregs); | |
10025 | if (ret != NULL) | |
10026 | return ret; | |
10027 | } | |
10028 | ||
1f20dca5 | 10029 | #if defined (HAVE_PRSTATUS_T) |
183e98be AM |
10030 | #if defined (HAVE_PRSTATUS32_T) |
10031 | if (bed->s->elfclass == ELFCLASS32) | |
10032 | { | |
10033 | prstatus32_t prstat; | |
10034 | ||
10035 | memset (&prstat, 0, sizeof (prstat)); | |
10036 | prstat.pr_pid = pid; | |
10037 | prstat.pr_cursig = cursig; | |
10038 | memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg)); | |
1f20dca5 | 10039 | return elfcore_write_note (abfd, buf, bufsiz, "CORE", |
183e98be AM |
10040 | NT_PRSTATUS, &prstat, sizeof (prstat)); |
10041 | } | |
10042 | else | |
10043 | #endif | |
10044 | { | |
10045 | prstatus_t prstat; | |
10046 | ||
10047 | memset (&prstat, 0, sizeof (prstat)); | |
10048 | prstat.pr_pid = pid; | |
10049 | prstat.pr_cursig = cursig; | |
10050 | memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg)); | |
1f20dca5 | 10051 | return elfcore_write_note (abfd, buf, bufsiz, "CORE", |
183e98be AM |
10052 | NT_PRSTATUS, &prstat, sizeof (prstat)); |
10053 | } | |
7c76fa91 MS |
10054 | #endif /* HAVE_PRSTATUS_T */ |
10055 | ||
1f20dca5 UW |
10056 | free (buf); |
10057 | return NULL; | |
10058 | } | |
10059 | ||
51316059 MS |
10060 | #if defined (HAVE_LWPSTATUS_T) |
10061 | char * | |
217aa764 AM |
10062 | elfcore_write_lwpstatus (bfd *abfd, |
10063 | char *buf, | |
10064 | int *bufsiz, | |
10065 | long pid, | |
10066 | int cursig, | |
10067 | const void *gregs) | |
51316059 MS |
10068 | { |
10069 | lwpstatus_t lwpstat; | |
183e98be | 10070 | const char *note_name = "CORE"; |
51316059 MS |
10071 | |
10072 | memset (&lwpstat, 0, sizeof (lwpstat)); | |
10073 | lwpstat.pr_lwpid = pid >> 16; | |
10074 | lwpstat.pr_cursig = cursig; | |
10075 | #if defined (HAVE_LWPSTATUS_T_PR_REG) | |
d1e8523e | 10076 | memcpy (&lwpstat.pr_reg, gregs, sizeof (lwpstat.pr_reg)); |
51316059 MS |
10077 | #elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT) |
10078 | #if !defined(gregs) | |
10079 | memcpy (lwpstat.pr_context.uc_mcontext.gregs, | |
10080 | gregs, sizeof (lwpstat.pr_context.uc_mcontext.gregs)); | |
10081 | #else | |
10082 | memcpy (lwpstat.pr_context.uc_mcontext.__gregs, | |
10083 | gregs, sizeof (lwpstat.pr_context.uc_mcontext.__gregs)); | |
10084 | #endif | |
10085 | #endif | |
47d9a591 | 10086 | return elfcore_write_note (abfd, buf, bufsiz, note_name, |
51316059 MS |
10087 | NT_LWPSTATUS, &lwpstat, sizeof (lwpstat)); |
10088 | } | |
10089 | #endif /* HAVE_LWPSTATUS_T */ | |
10090 | ||
7c76fa91 MS |
10091 | #if defined (HAVE_PSTATUS_T) |
10092 | char * | |
217aa764 AM |
10093 | elfcore_write_pstatus (bfd *abfd, |
10094 | char *buf, | |
10095 | int *bufsiz, | |
10096 | long pid, | |
6c10990d NC |
10097 | int cursig ATTRIBUTE_UNUSED, |
10098 | const void *gregs ATTRIBUTE_UNUSED) | |
7c76fa91 | 10099 | { |
183e98be AM |
10100 | const char *note_name = "CORE"; |
10101 | #if defined (HAVE_PSTATUS32_T) | |
10102 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
7c76fa91 | 10103 | |
183e98be AM |
10104 | if (bed->s->elfclass == ELFCLASS32) |
10105 | { | |
10106 | pstatus32_t pstat; | |
10107 | ||
10108 | memset (&pstat, 0, sizeof (pstat)); | |
10109 | pstat.pr_pid = pid & 0xffff; | |
10110 | buf = elfcore_write_note (abfd, buf, bufsiz, note_name, | |
10111 | NT_PSTATUS, &pstat, sizeof (pstat)); | |
10112 | return buf; | |
10113 | } | |
10114 | else | |
10115 | #endif | |
10116 | { | |
10117 | pstatus_t pstat; | |
10118 | ||
10119 | memset (&pstat, 0, sizeof (pstat)); | |
10120 | pstat.pr_pid = pid & 0xffff; | |
10121 | buf = elfcore_write_note (abfd, buf, bufsiz, note_name, | |
10122 | NT_PSTATUS, &pstat, sizeof (pstat)); | |
10123 | return buf; | |
10124 | } | |
7c76fa91 MS |
10125 | } |
10126 | #endif /* HAVE_PSTATUS_T */ | |
10127 | ||
10128 | char * | |
217aa764 AM |
10129 | elfcore_write_prfpreg (bfd *abfd, |
10130 | char *buf, | |
10131 | int *bufsiz, | |
10132 | const void *fpregs, | |
10133 | int size) | |
7c76fa91 | 10134 | { |
183e98be | 10135 | const char *note_name = "CORE"; |
47d9a591 | 10136 | return elfcore_write_note (abfd, buf, bufsiz, |
7c76fa91 MS |
10137 | note_name, NT_FPREGSET, fpregs, size); |
10138 | } | |
10139 | ||
10140 | char * | |
217aa764 AM |
10141 | elfcore_write_prxfpreg (bfd *abfd, |
10142 | char *buf, | |
10143 | int *bufsiz, | |
10144 | const void *xfpregs, | |
10145 | int size) | |
7c76fa91 MS |
10146 | { |
10147 | char *note_name = "LINUX"; | |
47d9a591 | 10148 | return elfcore_write_note (abfd, buf, bufsiz, |
7c76fa91 MS |
10149 | note_name, NT_PRXFPREG, xfpregs, size); |
10150 | } | |
10151 | ||
4339cae0 L |
10152 | char * |
10153 | elfcore_write_xstatereg (bfd *abfd, char *buf, int *bufsiz, | |
10154 | const void *xfpregs, int size) | |
10155 | { | |
97de3545 JB |
10156 | char *note_name; |
10157 | if (get_elf_backend_data (abfd)->elf_osabi == ELFOSABI_FREEBSD) | |
10158 | note_name = "FreeBSD"; | |
10159 | else | |
10160 | note_name = "LINUX"; | |
4339cae0 L |
10161 | return elfcore_write_note (abfd, buf, bufsiz, |
10162 | note_name, NT_X86_XSTATE, xfpregs, size); | |
10163 | } | |
10164 | ||
97753bd5 AM |
10165 | char * |
10166 | elfcore_write_ppc_vmx (bfd *abfd, | |
10167 | char *buf, | |
10168 | int *bufsiz, | |
10169 | const void *ppc_vmx, | |
10170 | int size) | |
10171 | { | |
10172 | char *note_name = "LINUX"; | |
10173 | return elfcore_write_note (abfd, buf, bufsiz, | |
10174 | note_name, NT_PPC_VMX, ppc_vmx, size); | |
10175 | } | |
10176 | ||
89eeb0bc LM |
10177 | char * |
10178 | elfcore_write_ppc_vsx (bfd *abfd, | |
10179 | char *buf, | |
10180 | int *bufsiz, | |
10181 | const void *ppc_vsx, | |
10182 | int size) | |
10183 | { | |
10184 | char *note_name = "LINUX"; | |
10185 | return elfcore_write_note (abfd, buf, bufsiz, | |
10186 | note_name, NT_PPC_VSX, ppc_vsx, size); | |
10187 | } | |
10188 | ||
0675e188 UW |
10189 | static char * |
10190 | elfcore_write_s390_high_gprs (bfd *abfd, | |
10191 | char *buf, | |
10192 | int *bufsiz, | |
10193 | const void *s390_high_gprs, | |
10194 | int size) | |
10195 | { | |
10196 | char *note_name = "LINUX"; | |
10197 | return elfcore_write_note (abfd, buf, bufsiz, | |
10198 | note_name, NT_S390_HIGH_GPRS, | |
10199 | s390_high_gprs, size); | |
10200 | } | |
10201 | ||
d7eeb400 MS |
10202 | char * |
10203 | elfcore_write_s390_timer (bfd *abfd, | |
10204 | char *buf, | |
10205 | int *bufsiz, | |
10206 | const void *s390_timer, | |
10207 | int size) | |
10208 | { | |
10209 | char *note_name = "LINUX"; | |
10210 | return elfcore_write_note (abfd, buf, bufsiz, | |
10211 | note_name, NT_S390_TIMER, s390_timer, size); | |
10212 | } | |
10213 | ||
10214 | char * | |
10215 | elfcore_write_s390_todcmp (bfd *abfd, | |
10216 | char *buf, | |
10217 | int *bufsiz, | |
10218 | const void *s390_todcmp, | |
10219 | int size) | |
10220 | { | |
10221 | char *note_name = "LINUX"; | |
10222 | return elfcore_write_note (abfd, buf, bufsiz, | |
10223 | note_name, NT_S390_TODCMP, s390_todcmp, size); | |
10224 | } | |
10225 | ||
10226 | char * | |
10227 | elfcore_write_s390_todpreg (bfd *abfd, | |
10228 | char *buf, | |
10229 | int *bufsiz, | |
10230 | const void *s390_todpreg, | |
10231 | int size) | |
10232 | { | |
10233 | char *note_name = "LINUX"; | |
10234 | return elfcore_write_note (abfd, buf, bufsiz, | |
10235 | note_name, NT_S390_TODPREG, s390_todpreg, size); | |
10236 | } | |
10237 | ||
10238 | char * | |
10239 | elfcore_write_s390_ctrs (bfd *abfd, | |
10240 | char *buf, | |
10241 | int *bufsiz, | |
10242 | const void *s390_ctrs, | |
10243 | int size) | |
10244 | { | |
10245 | char *note_name = "LINUX"; | |
10246 | return elfcore_write_note (abfd, buf, bufsiz, | |
10247 | note_name, NT_S390_CTRS, s390_ctrs, size); | |
10248 | } | |
10249 | ||
10250 | char * | |
10251 | elfcore_write_s390_prefix (bfd *abfd, | |
10252 | char *buf, | |
10253 | int *bufsiz, | |
10254 | const void *s390_prefix, | |
10255 | int size) | |
10256 | { | |
10257 | char *note_name = "LINUX"; | |
10258 | return elfcore_write_note (abfd, buf, bufsiz, | |
10259 | note_name, NT_S390_PREFIX, s390_prefix, size); | |
10260 | } | |
10261 | ||
355b81d9 UW |
10262 | char * |
10263 | elfcore_write_s390_last_break (bfd *abfd, | |
10264 | char *buf, | |
10265 | int *bufsiz, | |
10266 | const void *s390_last_break, | |
10267 | int size) | |
10268 | { | |
10269 | char *note_name = "LINUX"; | |
10270 | return elfcore_write_note (abfd, buf, bufsiz, | |
10271 | note_name, NT_S390_LAST_BREAK, | |
10272 | s390_last_break, size); | |
10273 | } | |
10274 | ||
10275 | char * | |
10276 | elfcore_write_s390_system_call (bfd *abfd, | |
10277 | char *buf, | |
10278 | int *bufsiz, | |
10279 | const void *s390_system_call, | |
10280 | int size) | |
10281 | { | |
10282 | char *note_name = "LINUX"; | |
10283 | return elfcore_write_note (abfd, buf, bufsiz, | |
10284 | note_name, NT_S390_SYSTEM_CALL, | |
10285 | s390_system_call, size); | |
10286 | } | |
10287 | ||
abb3f6cc NC |
10288 | char * |
10289 | elfcore_write_s390_tdb (bfd *abfd, | |
10290 | char *buf, | |
10291 | int *bufsiz, | |
10292 | const void *s390_tdb, | |
10293 | int size) | |
10294 | { | |
10295 | char *note_name = "LINUX"; | |
10296 | return elfcore_write_note (abfd, buf, bufsiz, | |
10297 | note_name, NT_S390_TDB, s390_tdb, size); | |
10298 | } | |
10299 | ||
4ef9f41a AA |
10300 | char * |
10301 | elfcore_write_s390_vxrs_low (bfd *abfd, | |
10302 | char *buf, | |
10303 | int *bufsiz, | |
10304 | const void *s390_vxrs_low, | |
10305 | int size) | |
10306 | { | |
10307 | char *note_name = "LINUX"; | |
10308 | return elfcore_write_note (abfd, buf, bufsiz, | |
10309 | note_name, NT_S390_VXRS_LOW, s390_vxrs_low, size); | |
10310 | } | |
10311 | ||
10312 | char * | |
10313 | elfcore_write_s390_vxrs_high (bfd *abfd, | |
10314 | char *buf, | |
10315 | int *bufsiz, | |
10316 | const void *s390_vxrs_high, | |
10317 | int size) | |
10318 | { | |
10319 | char *note_name = "LINUX"; | |
10320 | return elfcore_write_note (abfd, buf, bufsiz, | |
10321 | note_name, NT_S390_VXRS_HIGH, | |
10322 | s390_vxrs_high, size); | |
10323 | } | |
10324 | ||
faa9a424 UW |
10325 | char * |
10326 | elfcore_write_arm_vfp (bfd *abfd, | |
10327 | char *buf, | |
10328 | int *bufsiz, | |
10329 | const void *arm_vfp, | |
10330 | int size) | |
10331 | { | |
10332 | char *note_name = "LINUX"; | |
10333 | return elfcore_write_note (abfd, buf, bufsiz, | |
10334 | note_name, NT_ARM_VFP, arm_vfp, size); | |
10335 | } | |
10336 | ||
652451f8 YZ |
10337 | char * |
10338 | elfcore_write_aarch_tls (bfd *abfd, | |
10339 | char *buf, | |
10340 | int *bufsiz, | |
10341 | const void *aarch_tls, | |
10342 | int size) | |
10343 | { | |
10344 | char *note_name = "LINUX"; | |
10345 | return elfcore_write_note (abfd, buf, bufsiz, | |
10346 | note_name, NT_ARM_TLS, aarch_tls, size); | |
10347 | } | |
10348 | ||
10349 | char * | |
10350 | elfcore_write_aarch_hw_break (bfd *abfd, | |
10351 | char *buf, | |
10352 | int *bufsiz, | |
10353 | const void *aarch_hw_break, | |
10354 | int size) | |
10355 | { | |
10356 | char *note_name = "LINUX"; | |
10357 | return elfcore_write_note (abfd, buf, bufsiz, | |
10358 | note_name, NT_ARM_HW_BREAK, aarch_hw_break, size); | |
10359 | } | |
10360 | ||
10361 | char * | |
10362 | elfcore_write_aarch_hw_watch (bfd *abfd, | |
10363 | char *buf, | |
10364 | int *bufsiz, | |
10365 | const void *aarch_hw_watch, | |
10366 | int size) | |
10367 | { | |
10368 | char *note_name = "LINUX"; | |
10369 | return elfcore_write_note (abfd, buf, bufsiz, | |
10370 | note_name, NT_ARM_HW_WATCH, aarch_hw_watch, size); | |
10371 | } | |
10372 | ||
bb864ac1 CES |
10373 | char * |
10374 | elfcore_write_register_note (bfd *abfd, | |
10375 | char *buf, | |
10376 | int *bufsiz, | |
10377 | const char *section, | |
10378 | const void *data, | |
10379 | int size) | |
10380 | { | |
10381 | if (strcmp (section, ".reg2") == 0) | |
10382 | return elfcore_write_prfpreg (abfd, buf, bufsiz, data, size); | |
10383 | if (strcmp (section, ".reg-xfp") == 0) | |
10384 | return elfcore_write_prxfpreg (abfd, buf, bufsiz, data, size); | |
4339cae0 L |
10385 | if (strcmp (section, ".reg-xstate") == 0) |
10386 | return elfcore_write_xstatereg (abfd, buf, bufsiz, data, size); | |
bb864ac1 CES |
10387 | if (strcmp (section, ".reg-ppc-vmx") == 0) |
10388 | return elfcore_write_ppc_vmx (abfd, buf, bufsiz, data, size); | |
89eeb0bc LM |
10389 | if (strcmp (section, ".reg-ppc-vsx") == 0) |
10390 | return elfcore_write_ppc_vsx (abfd, buf, bufsiz, data, size); | |
0675e188 UW |
10391 | if (strcmp (section, ".reg-s390-high-gprs") == 0) |
10392 | return elfcore_write_s390_high_gprs (abfd, buf, bufsiz, data, size); | |
d7eeb400 MS |
10393 | if (strcmp (section, ".reg-s390-timer") == 0) |
10394 | return elfcore_write_s390_timer (abfd, buf, bufsiz, data, size); | |
10395 | if (strcmp (section, ".reg-s390-todcmp") == 0) | |
10396 | return elfcore_write_s390_todcmp (abfd, buf, bufsiz, data, size); | |
10397 | if (strcmp (section, ".reg-s390-todpreg") == 0) | |
10398 | return elfcore_write_s390_todpreg (abfd, buf, bufsiz, data, size); | |
10399 | if (strcmp (section, ".reg-s390-ctrs") == 0) | |
10400 | return elfcore_write_s390_ctrs (abfd, buf, bufsiz, data, size); | |
10401 | if (strcmp (section, ".reg-s390-prefix") == 0) | |
10402 | return elfcore_write_s390_prefix (abfd, buf, bufsiz, data, size); | |
355b81d9 UW |
10403 | if (strcmp (section, ".reg-s390-last-break") == 0) |
10404 | return elfcore_write_s390_last_break (abfd, buf, bufsiz, data, size); | |
10405 | if (strcmp (section, ".reg-s390-system-call") == 0) | |
10406 | return elfcore_write_s390_system_call (abfd, buf, bufsiz, data, size); | |
abb3f6cc NC |
10407 | if (strcmp (section, ".reg-s390-tdb") == 0) |
10408 | return elfcore_write_s390_tdb (abfd, buf, bufsiz, data, size); | |
4ef9f41a AA |
10409 | if (strcmp (section, ".reg-s390-vxrs-low") == 0) |
10410 | return elfcore_write_s390_vxrs_low (abfd, buf, bufsiz, data, size); | |
10411 | if (strcmp (section, ".reg-s390-vxrs-high") == 0) | |
10412 | return elfcore_write_s390_vxrs_high (abfd, buf, bufsiz, data, size); | |
faa9a424 UW |
10413 | if (strcmp (section, ".reg-arm-vfp") == 0) |
10414 | return elfcore_write_arm_vfp (abfd, buf, bufsiz, data, size); | |
652451f8 YZ |
10415 | if (strcmp (section, ".reg-aarch-tls") == 0) |
10416 | return elfcore_write_aarch_tls (abfd, buf, bufsiz, data, size); | |
10417 | if (strcmp (section, ".reg-aarch-hw-break") == 0) | |
10418 | return elfcore_write_aarch_hw_break (abfd, buf, bufsiz, data, size); | |
10419 | if (strcmp (section, ".reg-aarch-hw-watch") == 0) | |
10420 | return elfcore_write_aarch_hw_watch (abfd, buf, bufsiz, data, size); | |
bb864ac1 CES |
10421 | return NULL; |
10422 | } | |
10423 | ||
b34976b6 | 10424 | static bfd_boolean |
718175fa | 10425 | elf_parse_notes (bfd *abfd, char *buf, size_t size, file_ptr offset) |
252b5132 | 10426 | { |
c044fabd | 10427 | char *p; |
252b5132 | 10428 | |
252b5132 RH |
10429 | p = buf; |
10430 | while (p < buf + size) | |
10431 | { | |
c044fabd KH |
10432 | /* FIXME: bad alignment assumption. */ |
10433 | Elf_External_Note *xnp = (Elf_External_Note *) p; | |
252b5132 RH |
10434 | Elf_Internal_Note in; |
10435 | ||
baea7ef1 AM |
10436 | if (offsetof (Elf_External_Note, name) > buf - p + size) |
10437 | return FALSE; | |
10438 | ||
dc810e39 | 10439 | in.type = H_GET_32 (abfd, xnp->type); |
252b5132 | 10440 | |
dc810e39 | 10441 | in.namesz = H_GET_32 (abfd, xnp->namesz); |
252b5132 | 10442 | in.namedata = xnp->name; |
baea7ef1 AM |
10443 | if (in.namesz > buf - in.namedata + size) |
10444 | return FALSE; | |
252b5132 | 10445 | |
dc810e39 | 10446 | in.descsz = H_GET_32 (abfd, xnp->descsz); |
252b5132 RH |
10447 | in.descdata = in.namedata + BFD_ALIGN (in.namesz, 4); |
10448 | in.descpos = offset + (in.descdata - buf); | |
baea7ef1 AM |
10449 | if (in.descsz != 0 |
10450 | && (in.descdata >= buf + size | |
10451 | || in.descsz > buf - in.descdata + size)) | |
10452 | return FALSE; | |
252b5132 | 10453 | |
718175fa JK |
10454 | switch (bfd_get_format (abfd)) |
10455 | { | |
10456 | default: | |
10457 | return TRUE; | |
10458 | ||
10459 | case bfd_core: | |
f64e188b | 10460 | { |
8acbedd6 | 10461 | #define GROKER_ELEMENT(S,F) {S, sizeof (S) - 1, F} |
f64e188b | 10462 | struct |
718175fa | 10463 | { |
f64e188b | 10464 | const char * string; |
8acbedd6 | 10465 | size_t len; |
f64e188b | 10466 | bfd_boolean (* func)(bfd *, Elf_Internal_Note *); |
718175fa | 10467 | } |
f64e188b | 10468 | grokers[] = |
b15fa79e | 10469 | { |
8acbedd6 KS |
10470 | GROKER_ELEMENT ("", elfcore_grok_note), |
10471 | GROKER_ELEMENT ("NetBSD-CORE", elfcore_grok_netbsd_note), | |
10472 | GROKER_ELEMENT ( "OpenBSD", elfcore_grok_openbsd_note), | |
10473 | GROKER_ELEMENT ("QNX", elfcore_grok_nto_note), | |
10474 | GROKER_ELEMENT ("SPU/", elfcore_grok_spu_note) | |
f64e188b | 10475 | }; |
8acbedd6 | 10476 | #undef GROKER_ELEMENT |
f64e188b NC |
10477 | int i; |
10478 | ||
10479 | for (i = ARRAY_SIZE (grokers); i--;) | |
8acbedd6 KS |
10480 | { |
10481 | if (in.namesz >= grokers[i].len | |
10482 | && strncmp (in.namedata, grokers[i].string, | |
10483 | grokers[i].len) == 0) | |
10484 | { | |
10485 | if (! grokers[i].func (abfd, & in)) | |
10486 | return FALSE; | |
10487 | break; | |
10488 | } | |
10489 | } | |
f64e188b NC |
10490 | break; |
10491 | } | |
718175fa JK |
10492 | |
10493 | case bfd_object: | |
10494 | if (in.namesz == sizeof "GNU" && strcmp (in.namedata, "GNU") == 0) | |
10495 | { | |
10496 | if (! elfobj_grok_gnu_note (abfd, &in)) | |
10497 | return FALSE; | |
10498 | } | |
e21e5835 NC |
10499 | else if (in.namesz == sizeof "stapsdt" |
10500 | && strcmp (in.namedata, "stapsdt") == 0) | |
10501 | { | |
10502 | if (! elfobj_grok_stapsdt_note (abfd, &in)) | |
10503 | return FALSE; | |
10504 | } | |
718175fa | 10505 | break; |
08a40648 | 10506 | } |
252b5132 RH |
10507 | |
10508 | p = in.descdata + BFD_ALIGN (in.descsz, 4); | |
10509 | } | |
10510 | ||
718175fa JK |
10511 | return TRUE; |
10512 | } | |
10513 | ||
10514 | static bfd_boolean | |
10515 | elf_read_notes (bfd *abfd, file_ptr offset, bfd_size_type size) | |
10516 | { | |
10517 | char *buf; | |
10518 | ||
10519 | if (size <= 0) | |
10520 | return TRUE; | |
10521 | ||
10522 | if (bfd_seek (abfd, offset, SEEK_SET) != 0) | |
10523 | return FALSE; | |
10524 | ||
f64e188b | 10525 | buf = (char *) bfd_malloc (size + 1); |
718175fa JK |
10526 | if (buf == NULL) |
10527 | return FALSE; | |
10528 | ||
f64e188b NC |
10529 | /* PR 17512: file: ec08f814 |
10530 | 0-termintate the buffer so that string searches will not overflow. */ | |
10531 | buf[size] = 0; | |
10532 | ||
718175fa JK |
10533 | if (bfd_bread (buf, size, abfd) != size |
10534 | || !elf_parse_notes (abfd, buf, size, offset)) | |
10535 | { | |
10536 | free (buf); | |
10537 | return FALSE; | |
10538 | } | |
10539 | ||
252b5132 | 10540 | free (buf); |
b34976b6 | 10541 | return TRUE; |
252b5132 | 10542 | } |
98d8431c JB |
10543 | \f |
10544 | /* Providing external access to the ELF program header table. */ | |
10545 | ||
10546 | /* Return an upper bound on the number of bytes required to store a | |
10547 | copy of ABFD's program header table entries. Return -1 if an error | |
10548 | occurs; bfd_get_error will return an appropriate code. */ | |
c044fabd | 10549 | |
98d8431c | 10550 | long |
217aa764 | 10551 | bfd_get_elf_phdr_upper_bound (bfd *abfd) |
98d8431c JB |
10552 | { |
10553 | if (abfd->xvec->flavour != bfd_target_elf_flavour) | |
10554 | { | |
10555 | bfd_set_error (bfd_error_wrong_format); | |
10556 | return -1; | |
10557 | } | |
10558 | ||
936e320b | 10559 | return elf_elfheader (abfd)->e_phnum * sizeof (Elf_Internal_Phdr); |
98d8431c JB |
10560 | } |
10561 | ||
98d8431c JB |
10562 | /* Copy ABFD's program header table entries to *PHDRS. The entries |
10563 | will be stored as an array of Elf_Internal_Phdr structures, as | |
10564 | defined in include/elf/internal.h. To find out how large the | |
10565 | buffer needs to be, call bfd_get_elf_phdr_upper_bound. | |
10566 | ||
10567 | Return the number of program header table entries read, or -1 if an | |
10568 | error occurs; bfd_get_error will return an appropriate code. */ | |
c044fabd | 10569 | |
98d8431c | 10570 | int |
217aa764 | 10571 | bfd_get_elf_phdrs (bfd *abfd, void *phdrs) |
98d8431c JB |
10572 | { |
10573 | int num_phdrs; | |
10574 | ||
10575 | if (abfd->xvec->flavour != bfd_target_elf_flavour) | |
10576 | { | |
10577 | bfd_set_error (bfd_error_wrong_format); | |
10578 | return -1; | |
10579 | } | |
10580 | ||
10581 | num_phdrs = elf_elfheader (abfd)->e_phnum; | |
c044fabd | 10582 | memcpy (phdrs, elf_tdata (abfd)->phdr, |
98d8431c JB |
10583 | num_phdrs * sizeof (Elf_Internal_Phdr)); |
10584 | ||
10585 | return num_phdrs; | |
10586 | } | |
ae4221d7 | 10587 | |
db6751f2 | 10588 | enum elf_reloc_type_class |
7e612e98 AM |
10589 | _bfd_elf_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED, |
10590 | const asection *rel_sec ATTRIBUTE_UNUSED, | |
10591 | const Elf_Internal_Rela *rela ATTRIBUTE_UNUSED) | |
db6751f2 JJ |
10592 | { |
10593 | return reloc_class_normal; | |
10594 | } | |
f8df10f4 | 10595 | |
47d9a591 | 10596 | /* For RELA architectures, return the relocation value for a |
f8df10f4 JJ |
10597 | relocation against a local symbol. */ |
10598 | ||
10599 | bfd_vma | |
217aa764 AM |
10600 | _bfd_elf_rela_local_sym (bfd *abfd, |
10601 | Elf_Internal_Sym *sym, | |
8517fae7 | 10602 | asection **psec, |
217aa764 | 10603 | Elf_Internal_Rela *rel) |
f8df10f4 | 10604 | { |
8517fae7 | 10605 | asection *sec = *psec; |
f8df10f4 JJ |
10606 | bfd_vma relocation; |
10607 | ||
10608 | relocation = (sec->output_section->vma | |
10609 | + sec->output_offset | |
10610 | + sym->st_value); | |
10611 | if ((sec->flags & SEC_MERGE) | |
c629eae0 | 10612 | && ELF_ST_TYPE (sym->st_info) == STT_SECTION |
dbaa2011 | 10613 | && sec->sec_info_type == SEC_INFO_TYPE_MERGE) |
f8df10f4 | 10614 | { |
f8df10f4 | 10615 | rel->r_addend = |
8517fae7 | 10616 | _bfd_merged_section_offset (abfd, psec, |
65765700 | 10617 | elf_section_data (sec)->sec_info, |
753731ee AM |
10618 | sym->st_value + rel->r_addend); |
10619 | if (sec != *psec) | |
10620 | { | |
10621 | /* If we have changed the section, and our original section is | |
10622 | marked with SEC_EXCLUDE, it means that the original | |
10623 | SEC_MERGE section has been completely subsumed in some | |
10624 | other SEC_MERGE section. In this case, we need to leave | |
10625 | some info around for --emit-relocs. */ | |
10626 | if ((sec->flags & SEC_EXCLUDE) != 0) | |
10627 | sec->kept_section = *psec; | |
10628 | sec = *psec; | |
10629 | } | |
8517fae7 AM |
10630 | rel->r_addend -= relocation; |
10631 | rel->r_addend += sec->output_section->vma + sec->output_offset; | |
f8df10f4 JJ |
10632 | } |
10633 | return relocation; | |
10634 | } | |
c629eae0 JJ |
10635 | |
10636 | bfd_vma | |
217aa764 AM |
10637 | _bfd_elf_rel_local_sym (bfd *abfd, |
10638 | Elf_Internal_Sym *sym, | |
10639 | asection **psec, | |
10640 | bfd_vma addend) | |
47d9a591 | 10641 | { |
c629eae0 JJ |
10642 | asection *sec = *psec; |
10643 | ||
dbaa2011 | 10644 | if (sec->sec_info_type != SEC_INFO_TYPE_MERGE) |
c629eae0 JJ |
10645 | return sym->st_value + addend; |
10646 | ||
10647 | return _bfd_merged_section_offset (abfd, psec, | |
65765700 | 10648 | elf_section_data (sec)->sec_info, |
753731ee | 10649 | sym->st_value + addend); |
c629eae0 JJ |
10650 | } |
10651 | ||
37b01f6a DG |
10652 | /* Adjust an address within a section. Given OFFSET within SEC, return |
10653 | the new offset within the section, based upon changes made to the | |
10654 | section. Returns -1 if the offset is now invalid. | |
10655 | The offset (in abnd out) is in target sized bytes, however big a | |
10656 | byte may be. */ | |
10657 | ||
c629eae0 | 10658 | bfd_vma |
217aa764 | 10659 | _bfd_elf_section_offset (bfd *abfd, |
92e4ec35 | 10660 | struct bfd_link_info *info, |
217aa764 AM |
10661 | asection *sec, |
10662 | bfd_vma offset) | |
c629eae0 | 10663 | { |
68bfbfcc | 10664 | switch (sec->sec_info_type) |
65765700 | 10665 | { |
dbaa2011 | 10666 | case SEC_INFO_TYPE_STABS: |
eea6121a AM |
10667 | return _bfd_stab_section_offset (sec, elf_section_data (sec)->sec_info, |
10668 | offset); | |
dbaa2011 | 10669 | case SEC_INFO_TYPE_EH_FRAME: |
92e4ec35 | 10670 | return _bfd_elf_eh_frame_section_offset (abfd, info, sec, offset); |
37b01f6a | 10671 | |
65765700 | 10672 | default: |
310fd250 L |
10673 | if ((sec->flags & SEC_ELF_REVERSE_COPY) != 0) |
10674 | { | |
37b01f6a | 10675 | /* Reverse the offset. */ |
310fd250 L |
10676 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
10677 | bfd_size_type address_size = bed->s->arch_size / 8; | |
37b01f6a DG |
10678 | |
10679 | /* address_size and sec->size are in octets. Convert | |
10680 | to bytes before subtracting the original offset. */ | |
10681 | offset = (sec->size - address_size) / bfd_octets_per_byte (abfd) - offset; | |
310fd250 | 10682 | } |
65765700 JJ |
10683 | return offset; |
10684 | } | |
c629eae0 | 10685 | } |
3333a7c3 RM |
10686 | \f |
10687 | /* Create a new BFD as if by bfd_openr. Rather than opening a file, | |
10688 | reconstruct an ELF file by reading the segments out of remote memory | |
10689 | based on the ELF file header at EHDR_VMA and the ELF program headers it | |
10690 | points to. If not null, *LOADBASEP is filled in with the difference | |
10691 | between the VMAs from which the segments were read, and the VMAs the | |
10692 | file headers (and hence BFD's idea of each section's VMA) put them at. | |
10693 | ||
10694 | The function TARGET_READ_MEMORY is called to copy LEN bytes from the | |
10695 | remote memory at target address VMA into the local buffer at MYADDR; it | |
10696 | should return zero on success or an `errno' code on failure. TEMPL must | |
10697 | be a BFD for an ELF target with the word size and byte order found in | |
10698 | the remote memory. */ | |
10699 | ||
10700 | bfd * | |
217aa764 AM |
10701 | bfd_elf_bfd_from_remote_memory |
10702 | (bfd *templ, | |
10703 | bfd_vma ehdr_vma, | |
f0a5d95a | 10704 | bfd_size_type size, |
217aa764 | 10705 | bfd_vma *loadbasep, |
fe78531d | 10706 | int (*target_read_memory) (bfd_vma, bfd_byte *, bfd_size_type)) |
3333a7c3 RM |
10707 | { |
10708 | return (*get_elf_backend_data (templ)->elf_backend_bfd_from_remote_memory) | |
5979d6b6 | 10709 | (templ, ehdr_vma, size, loadbasep, target_read_memory); |
3333a7c3 | 10710 | } |
4c45e5c9 JJ |
10711 | \f |
10712 | long | |
c9727e01 AM |
10713 | _bfd_elf_get_synthetic_symtab (bfd *abfd, |
10714 | long symcount ATTRIBUTE_UNUSED, | |
10715 | asymbol **syms ATTRIBUTE_UNUSED, | |
8615f3f2 | 10716 | long dynsymcount, |
c9727e01 AM |
10717 | asymbol **dynsyms, |
10718 | asymbol **ret) | |
4c45e5c9 JJ |
10719 | { |
10720 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
10721 | asection *relplt; | |
10722 | asymbol *s; | |
10723 | const char *relplt_name; | |
10724 | bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean); | |
10725 | arelent *p; | |
10726 | long count, i, n; | |
10727 | size_t size; | |
10728 | Elf_Internal_Shdr *hdr; | |
10729 | char *names; | |
10730 | asection *plt; | |
10731 | ||
8615f3f2 AM |
10732 | *ret = NULL; |
10733 | ||
90e3cdf2 JJ |
10734 | if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0) |
10735 | return 0; | |
10736 | ||
8615f3f2 AM |
10737 | if (dynsymcount <= 0) |
10738 | return 0; | |
10739 | ||
4c45e5c9 JJ |
10740 | if (!bed->plt_sym_val) |
10741 | return 0; | |
10742 | ||
10743 | relplt_name = bed->relplt_name; | |
10744 | if (relplt_name == NULL) | |
d35fd659 | 10745 | relplt_name = bed->rela_plts_and_copies_p ? ".rela.plt" : ".rel.plt"; |
4c45e5c9 JJ |
10746 | relplt = bfd_get_section_by_name (abfd, relplt_name); |
10747 | if (relplt == NULL) | |
10748 | return 0; | |
10749 | ||
10750 | hdr = &elf_section_data (relplt)->this_hdr; | |
10751 | if (hdr->sh_link != elf_dynsymtab (abfd) | |
10752 | || (hdr->sh_type != SHT_REL && hdr->sh_type != SHT_RELA)) | |
10753 | return 0; | |
10754 | ||
10755 | plt = bfd_get_section_by_name (abfd, ".plt"); | |
10756 | if (plt == NULL) | |
10757 | return 0; | |
10758 | ||
10759 | slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table; | |
c9727e01 | 10760 | if (! (*slurp_relocs) (abfd, relplt, dynsyms, TRUE)) |
4c45e5c9 JJ |
10761 | return -1; |
10762 | ||
eea6121a | 10763 | count = relplt->size / hdr->sh_entsize; |
4c45e5c9 JJ |
10764 | size = count * sizeof (asymbol); |
10765 | p = relplt->relocation; | |
cb53bf42 | 10766 | for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel) |
041de40d AM |
10767 | { |
10768 | size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt"); | |
10769 | if (p->addend != 0) | |
10770 | { | |
10771 | #ifdef BFD64 | |
10772 | size += sizeof ("+0x") - 1 + 8 + 8 * (bed->s->elfclass == ELFCLASS64); | |
10773 | #else | |
10774 | size += sizeof ("+0x") - 1 + 8; | |
10775 | #endif | |
10776 | } | |
10777 | } | |
4c45e5c9 | 10778 | |
a50b1753 | 10779 | s = *ret = (asymbol *) bfd_malloc (size); |
4c45e5c9 JJ |
10780 | if (s == NULL) |
10781 | return -1; | |
10782 | ||
10783 | names = (char *) (s + count); | |
10784 | p = relplt->relocation; | |
10785 | n = 0; | |
cb53bf42 | 10786 | for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel) |
4c45e5c9 JJ |
10787 | { |
10788 | size_t len; | |
10789 | bfd_vma addr; | |
10790 | ||
10791 | addr = bed->plt_sym_val (i, plt, p); | |
10792 | if (addr == (bfd_vma) -1) | |
10793 | continue; | |
10794 | ||
10795 | *s = **p->sym_ptr_ptr; | |
65a7a66f AM |
10796 | /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since |
10797 | we are defining a symbol, ensure one of them is set. */ | |
10798 | if ((s->flags & BSF_LOCAL) == 0) | |
10799 | s->flags |= BSF_GLOBAL; | |
6ba2a415 | 10800 | s->flags |= BSF_SYNTHETIC; |
4c45e5c9 JJ |
10801 | s->section = plt; |
10802 | s->value = addr - plt->vma; | |
10803 | s->name = names; | |
8f39ba8e | 10804 | s->udata.p = NULL; |
4c45e5c9 JJ |
10805 | len = strlen ((*p->sym_ptr_ptr)->name); |
10806 | memcpy (names, (*p->sym_ptr_ptr)->name, len); | |
10807 | names += len; | |
041de40d AM |
10808 | if (p->addend != 0) |
10809 | { | |
1d770845 | 10810 | char buf[30], *a; |
d324f6d6 | 10811 | |
041de40d AM |
10812 | memcpy (names, "+0x", sizeof ("+0x") - 1); |
10813 | names += sizeof ("+0x") - 1; | |
1d770845 L |
10814 | bfd_sprintf_vma (abfd, buf, p->addend); |
10815 | for (a = buf; *a == '0'; ++a) | |
10816 | ; | |
10817 | len = strlen (a); | |
10818 | memcpy (names, a, len); | |
10819 | names += len; | |
041de40d | 10820 | } |
4c45e5c9 JJ |
10821 | memcpy (names, "@plt", sizeof ("@plt")); |
10822 | names += sizeof ("@plt"); | |
8f39ba8e | 10823 | ++s, ++n; |
4c45e5c9 JJ |
10824 | } |
10825 | ||
10826 | return n; | |
10827 | } | |
3d7f7666 | 10828 | |
3b22753a L |
10829 | /* It is only used by x86-64 so far. */ |
10830 | asection _bfd_elf_large_com_section | |
10831 | = BFD_FAKE_SECTION (_bfd_elf_large_com_section, | |
f592407e | 10832 | SEC_IS_COMMON, NULL, "LARGE_COMMON", 0); |
ecca9871 | 10833 | |
d1036acb | 10834 | void |
78245035 L |
10835 | _bfd_elf_post_process_headers (bfd * abfd, |
10836 | struct bfd_link_info * link_info ATTRIBUTE_UNUSED) | |
d1036acb L |
10837 | { |
10838 | Elf_Internal_Ehdr * i_ehdrp; /* ELF file header, internal form. */ | |
10839 | ||
10840 | i_ehdrp = elf_elfheader (abfd); | |
10841 | ||
10842 | i_ehdrp->e_ident[EI_OSABI] = get_elf_backend_data (abfd)->elf_osabi; | |
d8045f23 NC |
10843 | |
10844 | /* To make things simpler for the loader on Linux systems we set the | |
9c55345c | 10845 | osabi field to ELFOSABI_GNU if the binary contains symbols of |
f64b2e8d | 10846 | the STT_GNU_IFUNC type or STB_GNU_UNIQUE binding. */ |
d8045f23 | 10847 | if (i_ehdrp->e_ident[EI_OSABI] == ELFOSABI_NONE |
f64b2e8d | 10848 | && elf_tdata (abfd)->has_gnu_symbols) |
9c55345c | 10849 | i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_GNU; |
d1036acb | 10850 | } |
fcb93ecf PB |
10851 | |
10852 | ||
10853 | /* Return TRUE for ELF symbol types that represent functions. | |
10854 | This is the default version of this function, which is sufficient for | |
d8045f23 | 10855 | most targets. It returns true if TYPE is STT_FUNC or STT_GNU_IFUNC. */ |
fcb93ecf PB |
10856 | |
10857 | bfd_boolean | |
10858 | _bfd_elf_is_function_type (unsigned int type) | |
10859 | { | |
d8045f23 NC |
10860 | return (type == STT_FUNC |
10861 | || type == STT_GNU_IFUNC); | |
fcb93ecf | 10862 | } |
9f296da3 | 10863 | |
aef36ac1 AM |
10864 | /* If the ELF symbol SYM might be a function in SEC, return the |
10865 | function size and set *CODE_OFF to the function's entry point, | |
10866 | otherwise return zero. */ | |
9f296da3 | 10867 | |
aef36ac1 AM |
10868 | bfd_size_type |
10869 | _bfd_elf_maybe_function_sym (const asymbol *sym, asection *sec, | |
10870 | bfd_vma *code_off) | |
9f296da3 | 10871 | { |
aef36ac1 AM |
10872 | bfd_size_type size; |
10873 | ||
ff9e0f5b | 10874 | if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT |
aef36ac1 AM |
10875 | | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0 |
10876 | || sym->section != sec) | |
10877 | return 0; | |
ff9e0f5b | 10878 | |
ff9e0f5b | 10879 | *code_off = sym->value; |
aef36ac1 AM |
10880 | size = 0; |
10881 | if (!(sym->flags & BSF_SYNTHETIC)) | |
10882 | size = ((elf_symbol_type *) sym)->internal_elf_sym.st_size; | |
10883 | if (size == 0) | |
10884 | size = 1; | |
10885 | return size; | |
9f296da3 | 10886 | } |