Commit | Line | Data |
---|---|---|
252b5132 | 1 | /* ELF executable support for BFD. |
340b6d91 | 2 | |
b90efa5b | 3 | Copyright (C) 1993-2015 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" |
70a38d42 | 45 | #include "elf-linux-psinfo.h" |
252b5132 | 46 | |
8bc7f138 L |
47 | #ifdef CORE_HEADER |
48 | #include CORE_HEADER | |
49 | #endif | |
50 | ||
217aa764 | 51 | static int elf_sort_sections (const void *, const void *); |
c84fca4d | 52 | static bfd_boolean assign_file_positions_except_relocs (bfd *, struct bfd_link_info *); |
217aa764 | 53 | static bfd_boolean prep_headers (bfd *); |
ef10c3ac | 54 | static bfd_boolean swap_out_syms (bfd *, struct elf_strtab_hash **, int) ; |
718175fa JK |
55 | static bfd_boolean elf_read_notes (bfd *, file_ptr, bfd_size_type) ; |
56 | static bfd_boolean elf_parse_notes (bfd *abfd, char *buf, size_t size, | |
57 | file_ptr offset); | |
50b2bdb7 | 58 | |
252b5132 RH |
59 | /* Swap version information in and out. The version information is |
60 | currently size independent. If that ever changes, this code will | |
61 | need to move into elfcode.h. */ | |
62 | ||
63 | /* Swap in a Verdef structure. */ | |
64 | ||
65 | void | |
217aa764 AM |
66 | _bfd_elf_swap_verdef_in (bfd *abfd, |
67 | const Elf_External_Verdef *src, | |
68 | Elf_Internal_Verdef *dst) | |
252b5132 | 69 | { |
dc810e39 AM |
70 | dst->vd_version = H_GET_16 (abfd, src->vd_version); |
71 | dst->vd_flags = H_GET_16 (abfd, src->vd_flags); | |
72 | dst->vd_ndx = H_GET_16 (abfd, src->vd_ndx); | |
73 | dst->vd_cnt = H_GET_16 (abfd, src->vd_cnt); | |
74 | dst->vd_hash = H_GET_32 (abfd, src->vd_hash); | |
75 | dst->vd_aux = H_GET_32 (abfd, src->vd_aux); | |
76 | dst->vd_next = H_GET_32 (abfd, src->vd_next); | |
252b5132 RH |
77 | } |
78 | ||
79 | /* Swap out a Verdef structure. */ | |
80 | ||
81 | void | |
217aa764 AM |
82 | _bfd_elf_swap_verdef_out (bfd *abfd, |
83 | const Elf_Internal_Verdef *src, | |
84 | Elf_External_Verdef *dst) | |
252b5132 | 85 | { |
dc810e39 AM |
86 | H_PUT_16 (abfd, src->vd_version, dst->vd_version); |
87 | H_PUT_16 (abfd, src->vd_flags, dst->vd_flags); | |
88 | H_PUT_16 (abfd, src->vd_ndx, dst->vd_ndx); | |
89 | H_PUT_16 (abfd, src->vd_cnt, dst->vd_cnt); | |
90 | H_PUT_32 (abfd, src->vd_hash, dst->vd_hash); | |
91 | H_PUT_32 (abfd, src->vd_aux, dst->vd_aux); | |
92 | H_PUT_32 (abfd, src->vd_next, dst->vd_next); | |
252b5132 RH |
93 | } |
94 | ||
95 | /* Swap in a Verdaux structure. */ | |
96 | ||
97 | void | |
217aa764 AM |
98 | _bfd_elf_swap_verdaux_in (bfd *abfd, |
99 | const Elf_External_Verdaux *src, | |
100 | Elf_Internal_Verdaux *dst) | |
252b5132 | 101 | { |
dc810e39 AM |
102 | dst->vda_name = H_GET_32 (abfd, src->vda_name); |
103 | dst->vda_next = H_GET_32 (abfd, src->vda_next); | |
252b5132 RH |
104 | } |
105 | ||
106 | /* Swap out a Verdaux structure. */ | |
107 | ||
108 | void | |
217aa764 AM |
109 | _bfd_elf_swap_verdaux_out (bfd *abfd, |
110 | const Elf_Internal_Verdaux *src, | |
111 | Elf_External_Verdaux *dst) | |
252b5132 | 112 | { |
dc810e39 AM |
113 | H_PUT_32 (abfd, src->vda_name, dst->vda_name); |
114 | H_PUT_32 (abfd, src->vda_next, dst->vda_next); | |
252b5132 RH |
115 | } |
116 | ||
117 | /* Swap in a Verneed structure. */ | |
118 | ||
119 | void | |
217aa764 AM |
120 | _bfd_elf_swap_verneed_in (bfd *abfd, |
121 | const Elf_External_Verneed *src, | |
122 | Elf_Internal_Verneed *dst) | |
252b5132 | 123 | { |
dc810e39 AM |
124 | dst->vn_version = H_GET_16 (abfd, src->vn_version); |
125 | dst->vn_cnt = H_GET_16 (abfd, src->vn_cnt); | |
126 | dst->vn_file = H_GET_32 (abfd, src->vn_file); | |
127 | dst->vn_aux = H_GET_32 (abfd, src->vn_aux); | |
128 | dst->vn_next = H_GET_32 (abfd, src->vn_next); | |
252b5132 RH |
129 | } |
130 | ||
131 | /* Swap out a Verneed structure. */ | |
132 | ||
133 | void | |
217aa764 AM |
134 | _bfd_elf_swap_verneed_out (bfd *abfd, |
135 | const Elf_Internal_Verneed *src, | |
136 | Elf_External_Verneed *dst) | |
252b5132 | 137 | { |
dc810e39 AM |
138 | H_PUT_16 (abfd, src->vn_version, dst->vn_version); |
139 | H_PUT_16 (abfd, src->vn_cnt, dst->vn_cnt); | |
140 | H_PUT_32 (abfd, src->vn_file, dst->vn_file); | |
141 | H_PUT_32 (abfd, src->vn_aux, dst->vn_aux); | |
142 | H_PUT_32 (abfd, src->vn_next, dst->vn_next); | |
252b5132 RH |
143 | } |
144 | ||
145 | /* Swap in a Vernaux structure. */ | |
146 | ||
147 | void | |
217aa764 AM |
148 | _bfd_elf_swap_vernaux_in (bfd *abfd, |
149 | const Elf_External_Vernaux *src, | |
150 | Elf_Internal_Vernaux *dst) | |
252b5132 | 151 | { |
dc810e39 AM |
152 | dst->vna_hash = H_GET_32 (abfd, src->vna_hash); |
153 | dst->vna_flags = H_GET_16 (abfd, src->vna_flags); | |
154 | dst->vna_other = H_GET_16 (abfd, src->vna_other); | |
155 | dst->vna_name = H_GET_32 (abfd, src->vna_name); | |
156 | dst->vna_next = H_GET_32 (abfd, src->vna_next); | |
252b5132 RH |
157 | } |
158 | ||
159 | /* Swap out a Vernaux structure. */ | |
160 | ||
161 | void | |
217aa764 AM |
162 | _bfd_elf_swap_vernaux_out (bfd *abfd, |
163 | const Elf_Internal_Vernaux *src, | |
164 | Elf_External_Vernaux *dst) | |
252b5132 | 165 | { |
dc810e39 AM |
166 | H_PUT_32 (abfd, src->vna_hash, dst->vna_hash); |
167 | H_PUT_16 (abfd, src->vna_flags, dst->vna_flags); | |
168 | H_PUT_16 (abfd, src->vna_other, dst->vna_other); | |
169 | H_PUT_32 (abfd, src->vna_name, dst->vna_name); | |
170 | H_PUT_32 (abfd, src->vna_next, dst->vna_next); | |
252b5132 RH |
171 | } |
172 | ||
173 | /* Swap in a Versym structure. */ | |
174 | ||
175 | void | |
217aa764 AM |
176 | _bfd_elf_swap_versym_in (bfd *abfd, |
177 | const Elf_External_Versym *src, | |
178 | Elf_Internal_Versym *dst) | |
252b5132 | 179 | { |
dc810e39 | 180 | dst->vs_vers = H_GET_16 (abfd, src->vs_vers); |
252b5132 RH |
181 | } |
182 | ||
183 | /* Swap out a Versym structure. */ | |
184 | ||
185 | void | |
217aa764 AM |
186 | _bfd_elf_swap_versym_out (bfd *abfd, |
187 | const Elf_Internal_Versym *src, | |
188 | Elf_External_Versym *dst) | |
252b5132 | 189 | { |
dc810e39 | 190 | H_PUT_16 (abfd, src->vs_vers, dst->vs_vers); |
252b5132 RH |
191 | } |
192 | ||
193 | /* Standard ELF hash function. Do not change this function; you will | |
194 | cause invalid hash tables to be generated. */ | |
3a99b017 | 195 | |
252b5132 | 196 | unsigned long |
217aa764 | 197 | bfd_elf_hash (const char *namearg) |
252b5132 | 198 | { |
3a99b017 | 199 | const unsigned char *name = (const unsigned char *) namearg; |
252b5132 RH |
200 | unsigned long h = 0; |
201 | unsigned long g; | |
202 | int ch; | |
203 | ||
204 | while ((ch = *name++) != '\0') | |
205 | { | |
206 | h = (h << 4) + ch; | |
207 | if ((g = (h & 0xf0000000)) != 0) | |
208 | { | |
209 | h ^= g >> 24; | |
210 | /* The ELF ABI says `h &= ~g', but this is equivalent in | |
211 | this case and on some machines one insn instead of two. */ | |
212 | h ^= g; | |
213 | } | |
214 | } | |
32dfa85d | 215 | return h & 0xffffffff; |
252b5132 RH |
216 | } |
217 | ||
fdc90cb4 JJ |
218 | /* DT_GNU_HASH hash function. Do not change this function; you will |
219 | cause invalid hash tables to be generated. */ | |
220 | ||
221 | unsigned long | |
222 | bfd_elf_gnu_hash (const char *namearg) | |
223 | { | |
224 | const unsigned char *name = (const unsigned char *) namearg; | |
225 | unsigned long h = 5381; | |
226 | unsigned char ch; | |
227 | ||
228 | while ((ch = *name++) != '\0') | |
229 | h = (h << 5) + h + ch; | |
230 | return h & 0xffffffff; | |
231 | } | |
232 | ||
0c8d6e5c AM |
233 | /* Create a tdata field OBJECT_SIZE bytes in length, zeroed out and with |
234 | the object_id field of an elf_obj_tdata field set to OBJECT_ID. */ | |
b34976b6 | 235 | bfd_boolean |
0c8d6e5c | 236 | bfd_elf_allocate_object (bfd *abfd, |
0ffa91dd | 237 | size_t object_size, |
4dfe6ac6 | 238 | enum elf_target_id object_id) |
252b5132 | 239 | { |
0ffa91dd NC |
240 | BFD_ASSERT (object_size >= sizeof (struct elf_obj_tdata)); |
241 | abfd->tdata.any = bfd_zalloc (abfd, object_size); | |
242 | if (abfd->tdata.any == NULL) | |
243 | return FALSE; | |
252b5132 | 244 | |
0ffa91dd | 245 | elf_object_id (abfd) = object_id; |
c0355132 AM |
246 | if (abfd->direction != read_direction) |
247 | { | |
248 | struct output_elf_obj_tdata *o = bfd_zalloc (abfd, sizeof *o); | |
249 | if (o == NULL) | |
250 | return FALSE; | |
251 | elf_tdata (abfd)->o = o; | |
252 | elf_program_header_size (abfd) = (bfd_size_type) -1; | |
253 | } | |
b34976b6 | 254 | return TRUE; |
252b5132 RH |
255 | } |
256 | ||
0ffa91dd NC |
257 | |
258 | bfd_boolean | |
ae95ffa6 | 259 | bfd_elf_make_object (bfd *abfd) |
0ffa91dd | 260 | { |
ae95ffa6 | 261 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
0ffa91dd | 262 | return bfd_elf_allocate_object (abfd, sizeof (struct elf_obj_tdata), |
ae95ffa6 | 263 | bed->target_id); |
0ffa91dd NC |
264 | } |
265 | ||
b34976b6 | 266 | bfd_boolean |
217aa764 | 267 | bfd_elf_mkcorefile (bfd *abfd) |
252b5132 | 268 | { |
c044fabd | 269 | /* I think this can be done just like an object file. */ |
228e534f AM |
270 | if (!abfd->xvec->_bfd_set_format[(int) bfd_object] (abfd)) |
271 | return FALSE; | |
272 | elf_tdata (abfd)->core = bfd_zalloc (abfd, sizeof (*elf_tdata (abfd)->core)); | |
273 | return elf_tdata (abfd)->core != NULL; | |
252b5132 RH |
274 | } |
275 | ||
72a80a16 | 276 | static char * |
217aa764 | 277 | bfd_elf_get_str_section (bfd *abfd, unsigned int shindex) |
252b5132 RH |
278 | { |
279 | Elf_Internal_Shdr **i_shdrp; | |
f075ee0c | 280 | bfd_byte *shstrtab = NULL; |
dc810e39 AM |
281 | file_ptr offset; |
282 | bfd_size_type shstrtabsize; | |
252b5132 RH |
283 | |
284 | i_shdrp = elf_elfsections (abfd); | |
74f2e02b AM |
285 | if (i_shdrp == 0 |
286 | || shindex >= elf_numsections (abfd) | |
287 | || i_shdrp[shindex] == 0) | |
f075ee0c | 288 | return NULL; |
252b5132 | 289 | |
f075ee0c | 290 | shstrtab = i_shdrp[shindex]->contents; |
252b5132 RH |
291 | if (shstrtab == NULL) |
292 | { | |
c044fabd | 293 | /* No cached one, attempt to read, and cache what we read. */ |
252b5132 RH |
294 | offset = i_shdrp[shindex]->sh_offset; |
295 | shstrtabsize = i_shdrp[shindex]->sh_size; | |
c6c60d09 JJ |
296 | |
297 | /* Allocate and clear an extra byte at the end, to prevent crashes | |
298 | in case the string table is not terminated. */ | |
3471d59d | 299 | if (shstrtabsize + 1 <= 1 |
06614111 NC |
300 | || bfd_seek (abfd, offset, SEEK_SET) != 0 |
301 | || (shstrtab = (bfd_byte *) bfd_alloc (abfd, shstrtabsize + 1)) == NULL) | |
c6c60d09 JJ |
302 | shstrtab = NULL; |
303 | else if (bfd_bread (shstrtab, shstrtabsize, abfd) != shstrtabsize) | |
304 | { | |
305 | if (bfd_get_error () != bfd_error_system_call) | |
306 | bfd_set_error (bfd_error_file_truncated); | |
06614111 | 307 | bfd_release (abfd, shstrtab); |
c6c60d09 | 308 | shstrtab = NULL; |
3471d59d CC |
309 | /* Once we've failed to read it, make sure we don't keep |
310 | trying. Otherwise, we'll keep allocating space for | |
311 | the string table over and over. */ | |
312 | i_shdrp[shindex]->sh_size = 0; | |
c6c60d09 JJ |
313 | } |
314 | else | |
315 | shstrtab[shstrtabsize] = '\0'; | |
217aa764 | 316 | i_shdrp[shindex]->contents = shstrtab; |
252b5132 | 317 | } |
f075ee0c | 318 | return (char *) shstrtab; |
252b5132 RH |
319 | } |
320 | ||
321 | char * | |
217aa764 AM |
322 | bfd_elf_string_from_elf_section (bfd *abfd, |
323 | unsigned int shindex, | |
324 | unsigned int strindex) | |
252b5132 RH |
325 | { |
326 | Elf_Internal_Shdr *hdr; | |
327 | ||
328 | if (strindex == 0) | |
329 | return ""; | |
330 | ||
74f2e02b AM |
331 | if (elf_elfsections (abfd) == NULL || shindex >= elf_numsections (abfd)) |
332 | return NULL; | |
333 | ||
252b5132 RH |
334 | hdr = elf_elfsections (abfd)[shindex]; |
335 | ||
06614111 NC |
336 | if (hdr->contents == NULL) |
337 | { | |
338 | if (hdr->sh_type != SHT_STRTAB && hdr->sh_type < SHT_LOOS) | |
339 | { | |
340 | /* PR 17512: file: f057ec89. */ | |
341 | _bfd_error_handler (_("%B: attempt to load strings from a non-string section (number %d)"), | |
342 | abfd, shindex); | |
343 | return NULL; | |
344 | } | |
b1fa9dd6 | 345 | |
06614111 NC |
346 | if (bfd_elf_get_str_section (abfd, shindex) == NULL) |
347 | return NULL; | |
348 | } | |
252b5132 RH |
349 | |
350 | if (strindex >= hdr->sh_size) | |
351 | { | |
1b3a8575 | 352 | unsigned int shstrndx = elf_elfheader(abfd)->e_shstrndx; |
252b5132 | 353 | (*_bfd_error_handler) |
d003868e AM |
354 | (_("%B: invalid string offset %u >= %lu for section `%s'"), |
355 | abfd, strindex, (unsigned long) hdr->sh_size, | |
1b3a8575 | 356 | (shindex == shstrndx && strindex == hdr->sh_name |
252b5132 | 357 | ? ".shstrtab" |
1b3a8575 | 358 | : bfd_elf_string_from_elf_section (abfd, shstrndx, hdr->sh_name))); |
45b222d6 | 359 | return NULL; |
252b5132 RH |
360 | } |
361 | ||
362 | return ((char *) hdr->contents) + strindex; | |
363 | } | |
364 | ||
6cdc0ccc AM |
365 | /* Read and convert symbols to internal format. |
366 | SYMCOUNT specifies the number of symbols to read, starting from | |
367 | symbol SYMOFFSET. If any of INTSYM_BUF, EXTSYM_BUF or EXTSHNDX_BUF | |
368 | are non-NULL, they are used to store the internal symbols, external | |
b7c368d0 NC |
369 | symbols, and symbol section index extensions, respectively. |
370 | Returns a pointer to the internal symbol buffer (malloced if necessary) | |
371 | or NULL if there were no symbols or some kind of problem. */ | |
6cdc0ccc AM |
372 | |
373 | Elf_Internal_Sym * | |
217aa764 AM |
374 | bfd_elf_get_elf_syms (bfd *ibfd, |
375 | Elf_Internal_Shdr *symtab_hdr, | |
376 | size_t symcount, | |
377 | size_t symoffset, | |
378 | Elf_Internal_Sym *intsym_buf, | |
379 | void *extsym_buf, | |
380 | Elf_External_Sym_Shndx *extshndx_buf) | |
6cdc0ccc AM |
381 | { |
382 | Elf_Internal_Shdr *shndx_hdr; | |
217aa764 | 383 | void *alloc_ext; |
df622259 | 384 | const bfd_byte *esym; |
6cdc0ccc AM |
385 | Elf_External_Sym_Shndx *alloc_extshndx; |
386 | Elf_External_Sym_Shndx *shndx; | |
4dd07732 | 387 | Elf_Internal_Sym *alloc_intsym; |
6cdc0ccc AM |
388 | Elf_Internal_Sym *isym; |
389 | Elf_Internal_Sym *isymend; | |
9c5bfbb7 | 390 | const struct elf_backend_data *bed; |
6cdc0ccc AM |
391 | size_t extsym_size; |
392 | bfd_size_type amt; | |
393 | file_ptr pos; | |
394 | ||
e44a2c9c AM |
395 | if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour) |
396 | abort (); | |
397 | ||
6cdc0ccc AM |
398 | if (symcount == 0) |
399 | return intsym_buf; | |
400 | ||
401 | /* Normal syms might have section extension entries. */ | |
402 | shndx_hdr = NULL; | |
6a40cf0c NC |
403 | if (elf_symtab_shndx_list (ibfd) != NULL) |
404 | { | |
405 | elf_section_list * entry; | |
406 | Elf_Internal_Shdr **sections = elf_elfsections (ibfd); | |
407 | ||
408 | /* Find an index section that is linked to this symtab section. */ | |
409 | for (entry = elf_symtab_shndx_list (ibfd); entry != NULL; entry = entry->next) | |
410 | if (sections[entry->hdr.sh_link] == symtab_hdr) | |
411 | { | |
412 | shndx_hdr = & entry->hdr; | |
413 | break; | |
414 | }; | |
415 | ||
416 | if (shndx_hdr == NULL) | |
417 | { | |
418 | if (symtab_hdr == & elf_symtab_hdr (ibfd)) | |
419 | /* Not really accurate, but this was how the old code used to work. */ | |
420 | shndx_hdr = & elf_symtab_shndx_list (ibfd)->hdr; | |
421 | /* Otherwise we do nothing. The assumption is that | |
422 | the index table will not be needed. */ | |
423 | } | |
424 | } | |
6cdc0ccc AM |
425 | |
426 | /* Read the symbols. */ | |
427 | alloc_ext = NULL; | |
428 | alloc_extshndx = NULL; | |
4dd07732 | 429 | alloc_intsym = NULL; |
6cdc0ccc AM |
430 | bed = get_elf_backend_data (ibfd); |
431 | extsym_size = bed->s->sizeof_sym; | |
432 | amt = symcount * extsym_size; | |
433 | pos = symtab_hdr->sh_offset + symoffset * extsym_size; | |
434 | if (extsym_buf == NULL) | |
435 | { | |
d0fb9a8d | 436 | alloc_ext = bfd_malloc2 (symcount, extsym_size); |
6cdc0ccc AM |
437 | extsym_buf = alloc_ext; |
438 | } | |
439 | if (extsym_buf == NULL | |
440 | || bfd_seek (ibfd, pos, SEEK_SET) != 0 | |
441 | || bfd_bread (extsym_buf, amt, ibfd) != amt) | |
442 | { | |
443 | intsym_buf = NULL; | |
444 | goto out; | |
445 | } | |
446 | ||
447 | if (shndx_hdr == NULL || shndx_hdr->sh_size == 0) | |
448 | extshndx_buf = NULL; | |
449 | else | |
450 | { | |
451 | amt = symcount * sizeof (Elf_External_Sym_Shndx); | |
452 | pos = shndx_hdr->sh_offset + symoffset * sizeof (Elf_External_Sym_Shndx); | |
453 | if (extshndx_buf == NULL) | |
454 | { | |
a50b1753 NC |
455 | alloc_extshndx = (Elf_External_Sym_Shndx *) |
456 | bfd_malloc2 (symcount, sizeof (Elf_External_Sym_Shndx)); | |
6cdc0ccc AM |
457 | extshndx_buf = alloc_extshndx; |
458 | } | |
459 | if (extshndx_buf == NULL | |
460 | || bfd_seek (ibfd, pos, SEEK_SET) != 0 | |
461 | || bfd_bread (extshndx_buf, amt, ibfd) != amt) | |
462 | { | |
463 | intsym_buf = NULL; | |
464 | goto out; | |
465 | } | |
466 | } | |
467 | ||
468 | if (intsym_buf == NULL) | |
469 | { | |
a50b1753 NC |
470 | alloc_intsym = (Elf_Internal_Sym *) |
471 | bfd_malloc2 (symcount, sizeof (Elf_Internal_Sym)); | |
4dd07732 | 472 | intsym_buf = alloc_intsym; |
6cdc0ccc AM |
473 | if (intsym_buf == NULL) |
474 | goto out; | |
475 | } | |
476 | ||
477 | /* Convert the symbols to internal form. */ | |
478 | isymend = intsym_buf + symcount; | |
a50b1753 NC |
479 | for (esym = (const bfd_byte *) extsym_buf, isym = intsym_buf, |
480 | shndx = extshndx_buf; | |
6cdc0ccc AM |
481 | isym < isymend; |
482 | esym += extsym_size, isym++, shndx = shndx != NULL ? shndx + 1 : NULL) | |
8384fb8f AM |
483 | if (!(*bed->s->swap_symbol_in) (ibfd, esym, shndx, isym)) |
484 | { | |
485 | symoffset += (esym - (bfd_byte *) extsym_buf) / extsym_size; | |
486 | (*_bfd_error_handler) (_("%B symbol number %lu references " | |
487 | "nonexistent SHT_SYMTAB_SHNDX section"), | |
488 | ibfd, (unsigned long) symoffset); | |
4dd07732 AM |
489 | if (alloc_intsym != NULL) |
490 | free (alloc_intsym); | |
8384fb8f AM |
491 | intsym_buf = NULL; |
492 | goto out; | |
493 | } | |
6cdc0ccc AM |
494 | |
495 | out: | |
496 | if (alloc_ext != NULL) | |
497 | free (alloc_ext); | |
498 | if (alloc_extshndx != NULL) | |
499 | free (alloc_extshndx); | |
500 | ||
501 | return intsym_buf; | |
502 | } | |
503 | ||
5cab59f6 AM |
504 | /* Look up a symbol name. */ |
505 | const char * | |
be8dd2ca AM |
506 | bfd_elf_sym_name (bfd *abfd, |
507 | Elf_Internal_Shdr *symtab_hdr, | |
26c61ae5 L |
508 | Elf_Internal_Sym *isym, |
509 | asection *sym_sec) | |
5cab59f6 | 510 | { |
26c61ae5 | 511 | const char *name; |
5cab59f6 | 512 | unsigned int iname = isym->st_name; |
be8dd2ca | 513 | unsigned int shindex = symtab_hdr->sh_link; |
26c61ae5 | 514 | |
138f35cc JJ |
515 | if (iname == 0 && ELF_ST_TYPE (isym->st_info) == STT_SECTION |
516 | /* Check for a bogus st_shndx to avoid crashing. */ | |
4fbb74a6 | 517 | && isym->st_shndx < elf_numsections (abfd)) |
5cab59f6 AM |
518 | { |
519 | iname = elf_elfsections (abfd)[isym->st_shndx]->sh_name; | |
520 | shindex = elf_elfheader (abfd)->e_shstrndx; | |
521 | } | |
522 | ||
26c61ae5 L |
523 | name = bfd_elf_string_from_elf_section (abfd, shindex, iname); |
524 | if (name == NULL) | |
525 | name = "(null)"; | |
526 | else if (sym_sec && *name == '\0') | |
527 | name = bfd_section_name (abfd, sym_sec); | |
528 | ||
529 | return name; | |
5cab59f6 AM |
530 | } |
531 | ||
dbb410c3 AM |
532 | /* Elf_Internal_Shdr->contents is an array of these for SHT_GROUP |
533 | sections. The first element is the flags, the rest are section | |
534 | pointers. */ | |
535 | ||
536 | typedef union elf_internal_group { | |
537 | Elf_Internal_Shdr *shdr; | |
538 | unsigned int flags; | |
539 | } Elf_Internal_Group; | |
540 | ||
b885599b AM |
541 | /* Return the name of the group signature symbol. Why isn't the |
542 | signature just a string? */ | |
543 | ||
544 | static const char * | |
217aa764 | 545 | group_signature (bfd *abfd, Elf_Internal_Shdr *ghdr) |
b885599b | 546 | { |
9dce4196 | 547 | Elf_Internal_Shdr *hdr; |
9dce4196 AM |
548 | unsigned char esym[sizeof (Elf64_External_Sym)]; |
549 | Elf_External_Sym_Shndx eshndx; | |
550 | Elf_Internal_Sym isym; | |
b885599b | 551 | |
13792e9d L |
552 | /* First we need to ensure the symbol table is available. Make sure |
553 | that it is a symbol table section. */ | |
4fbb74a6 AM |
554 | if (ghdr->sh_link >= elf_numsections (abfd)) |
555 | return NULL; | |
13792e9d L |
556 | hdr = elf_elfsections (abfd) [ghdr->sh_link]; |
557 | if (hdr->sh_type != SHT_SYMTAB | |
558 | || ! bfd_section_from_shdr (abfd, ghdr->sh_link)) | |
b885599b AM |
559 | return NULL; |
560 | ||
9dce4196 AM |
561 | /* Go read the symbol. */ |
562 | hdr = &elf_tdata (abfd)->symtab_hdr; | |
6cdc0ccc AM |
563 | if (bfd_elf_get_elf_syms (abfd, hdr, 1, ghdr->sh_info, |
564 | &isym, esym, &eshndx) == NULL) | |
b885599b | 565 | return NULL; |
9dce4196 | 566 | |
26c61ae5 | 567 | return bfd_elf_sym_name (abfd, hdr, &isym, NULL); |
b885599b AM |
568 | } |
569 | ||
dbb410c3 AM |
570 | /* Set next_in_group list pointer, and group name for NEWSECT. */ |
571 | ||
b34976b6 | 572 | static bfd_boolean |
217aa764 | 573 | setup_group (bfd *abfd, Elf_Internal_Shdr *hdr, asection *newsect) |
dbb410c3 AM |
574 | { |
575 | unsigned int num_group = elf_tdata (abfd)->num_group; | |
576 | ||
577 | /* If num_group is zero, read in all SHT_GROUP sections. The count | |
578 | is set to -1 if there are no SHT_GROUP sections. */ | |
579 | if (num_group == 0) | |
580 | { | |
581 | unsigned int i, shnum; | |
582 | ||
583 | /* First count the number of groups. If we have a SHT_GROUP | |
584 | section with just a flag word (ie. sh_size is 4), ignore it. */ | |
9ad5cbcf | 585 | shnum = elf_numsections (abfd); |
dbb410c3 | 586 | num_group = 0; |
08a40648 | 587 | |
44534af3 | 588 | #define IS_VALID_GROUP_SECTION_HEADER(shdr, minsize) \ |
1783205a | 589 | ( (shdr)->sh_type == SHT_GROUP \ |
44534af3 | 590 | && (shdr)->sh_size >= minsize \ |
1783205a NC |
591 | && (shdr)->sh_entsize == GRP_ENTRY_SIZE \ |
592 | && ((shdr)->sh_size % GRP_ENTRY_SIZE) == 0) | |
08a40648 | 593 | |
dbb410c3 AM |
594 | for (i = 0; i < shnum; i++) |
595 | { | |
596 | Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i]; | |
1783205a | 597 | |
44534af3 | 598 | if (IS_VALID_GROUP_SECTION_HEADER (shdr, 2 * GRP_ENTRY_SIZE)) |
dbb410c3 AM |
599 | num_group += 1; |
600 | } | |
601 | ||
602 | if (num_group == 0) | |
20dbb49d L |
603 | { |
604 | num_group = (unsigned) -1; | |
605 | elf_tdata (abfd)->num_group = num_group; | |
606 | } | |
607 | else | |
dbb410c3 AM |
608 | { |
609 | /* We keep a list of elf section headers for group sections, | |
610 | so we can find them quickly. */ | |
20dbb49d | 611 | bfd_size_type amt; |
d0fb9a8d | 612 | |
20dbb49d | 613 | elf_tdata (abfd)->num_group = num_group; |
a50b1753 NC |
614 | elf_tdata (abfd)->group_sect_ptr = (Elf_Internal_Shdr **) |
615 | bfd_alloc2 (abfd, num_group, sizeof (Elf_Internal_Shdr *)); | |
dbb410c3 | 616 | if (elf_tdata (abfd)->group_sect_ptr == NULL) |
b34976b6 | 617 | return FALSE; |
dbb410c3 AM |
618 | |
619 | num_group = 0; | |
620 | for (i = 0; i < shnum; i++) | |
621 | { | |
622 | Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i]; | |
1783205a | 623 | |
44534af3 | 624 | if (IS_VALID_GROUP_SECTION_HEADER (shdr, 2 * GRP_ENTRY_SIZE)) |
dbb410c3 | 625 | { |
973ffd63 | 626 | unsigned char *src; |
dbb410c3 AM |
627 | Elf_Internal_Group *dest; |
628 | ||
629 | /* Add to list of sections. */ | |
630 | elf_tdata (abfd)->group_sect_ptr[num_group] = shdr; | |
631 | num_group += 1; | |
632 | ||
633 | /* Read the raw contents. */ | |
634 | BFD_ASSERT (sizeof (*dest) >= 4); | |
635 | amt = shdr->sh_size * sizeof (*dest) / 4; | |
a50b1753 NC |
636 | shdr->contents = (unsigned char *) |
637 | bfd_alloc2 (abfd, shdr->sh_size, sizeof (*dest) / 4); | |
1783205a NC |
638 | /* PR binutils/4110: Handle corrupt group headers. */ |
639 | if (shdr->contents == NULL) | |
640 | { | |
641 | _bfd_error_handler | |
493a3386 | 642 | (_("%B: corrupt size field in group section header: 0x%lx"), abfd, shdr->sh_size); |
1783205a | 643 | bfd_set_error (bfd_error_bad_value); |
493a3386 NC |
644 | -- num_group; |
645 | continue; | |
1783205a NC |
646 | } |
647 | ||
648 | memset (shdr->contents, 0, amt); | |
649 | ||
650 | if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0 | |
dbb410c3 AM |
651 | || (bfd_bread (shdr->contents, shdr->sh_size, abfd) |
652 | != shdr->sh_size)) | |
493a3386 NC |
653 | { |
654 | _bfd_error_handler | |
655 | (_("%B: invalid size field in group section header: 0x%lx"), abfd, shdr->sh_size); | |
656 | bfd_set_error (bfd_error_bad_value); | |
657 | -- num_group; | |
658 | /* PR 17510: If the group contents are even partially | |
659 | corrupt, do not allow any of the contents to be used. */ | |
660 | memset (shdr->contents, 0, amt); | |
661 | continue; | |
662 | } | |
708d7d0d | 663 | |
dbb410c3 AM |
664 | /* Translate raw contents, a flag word followed by an |
665 | array of elf section indices all in target byte order, | |
666 | to the flag word followed by an array of elf section | |
667 | pointers. */ | |
668 | src = shdr->contents + shdr->sh_size; | |
669 | dest = (Elf_Internal_Group *) (shdr->contents + amt); | |
06614111 | 670 | |
dbb410c3 AM |
671 | while (1) |
672 | { | |
673 | unsigned int idx; | |
674 | ||
675 | src -= 4; | |
676 | --dest; | |
677 | idx = H_GET_32 (abfd, src); | |
678 | if (src == shdr->contents) | |
679 | { | |
680 | dest->flags = idx; | |
b885599b AM |
681 | if (shdr->bfd_section != NULL && (idx & GRP_COMDAT)) |
682 | shdr->bfd_section->flags | |
683 | |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD; | |
dbb410c3 AM |
684 | break; |
685 | } | |
686 | if (idx >= shnum) | |
687 | { | |
688 | ((*_bfd_error_handler) | |
d003868e | 689 | (_("%B: invalid SHT_GROUP entry"), abfd)); |
dbb410c3 AM |
690 | idx = 0; |
691 | } | |
692 | dest->shdr = elf_elfsections (abfd)[idx]; | |
693 | } | |
694 | } | |
695 | } | |
493a3386 NC |
696 | |
697 | /* PR 17510: Corrupt binaries might contain invalid groups. */ | |
698 | if (num_group != (unsigned) elf_tdata (abfd)->num_group) | |
699 | { | |
700 | elf_tdata (abfd)->num_group = num_group; | |
701 | ||
702 | /* If all groups are invalid then fail. */ | |
703 | if (num_group == 0) | |
704 | { | |
705 | elf_tdata (abfd)->group_sect_ptr = NULL; | |
706 | elf_tdata (abfd)->num_group = num_group = -1; | |
707 | (*_bfd_error_handler) (_("%B: no valid group sections found"), abfd); | |
708 | bfd_set_error (bfd_error_bad_value); | |
709 | } | |
710 | } | |
dbb410c3 AM |
711 | } |
712 | } | |
713 | ||
714 | if (num_group != (unsigned) -1) | |
715 | { | |
716 | unsigned int i; | |
717 | ||
718 | for (i = 0; i < num_group; i++) | |
719 | { | |
720 | Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i]; | |
721 | Elf_Internal_Group *idx = (Elf_Internal_Group *) shdr->contents; | |
722 | unsigned int n_elt = shdr->sh_size / 4; | |
723 | ||
724 | /* Look through this group's sections to see if current | |
725 | section is a member. */ | |
726 | while (--n_elt != 0) | |
727 | if ((++idx)->shdr == hdr) | |
728 | { | |
e0e8c97f | 729 | asection *s = NULL; |
dbb410c3 AM |
730 | |
731 | /* We are a member of this group. Go looking through | |
732 | other members to see if any others are linked via | |
733 | next_in_group. */ | |
734 | idx = (Elf_Internal_Group *) shdr->contents; | |
735 | n_elt = shdr->sh_size / 4; | |
736 | while (--n_elt != 0) | |
737 | if ((s = (++idx)->shdr->bfd_section) != NULL | |
945906ff | 738 | && elf_next_in_group (s) != NULL) |
dbb410c3 AM |
739 | break; |
740 | if (n_elt != 0) | |
741 | { | |
dbb410c3 AM |
742 | /* Snarf the group name from other member, and |
743 | insert current section in circular list. */ | |
945906ff AM |
744 | elf_group_name (newsect) = elf_group_name (s); |
745 | elf_next_in_group (newsect) = elf_next_in_group (s); | |
746 | elf_next_in_group (s) = newsect; | |
dbb410c3 AM |
747 | } |
748 | else | |
749 | { | |
dbb410c3 AM |
750 | const char *gname; |
751 | ||
b885599b AM |
752 | gname = group_signature (abfd, shdr); |
753 | if (gname == NULL) | |
b34976b6 | 754 | return FALSE; |
945906ff | 755 | elf_group_name (newsect) = gname; |
dbb410c3 AM |
756 | |
757 | /* Start a circular list with one element. */ | |
945906ff | 758 | elf_next_in_group (newsect) = newsect; |
dbb410c3 | 759 | } |
b885599b | 760 | |
9dce4196 AM |
761 | /* If the group section has been created, point to the |
762 | new member. */ | |
dbb410c3 | 763 | if (shdr->bfd_section != NULL) |
945906ff | 764 | elf_next_in_group (shdr->bfd_section) = newsect; |
b885599b | 765 | |
dbb410c3 AM |
766 | i = num_group - 1; |
767 | break; | |
768 | } | |
769 | } | |
770 | } | |
771 | ||
945906ff | 772 | if (elf_group_name (newsect) == NULL) |
dbb410c3 | 773 | { |
d003868e AM |
774 | (*_bfd_error_handler) (_("%B: no group info for section %A"), |
775 | abfd, newsect); | |
493a3386 | 776 | return FALSE; |
dbb410c3 | 777 | } |
b34976b6 | 778 | return TRUE; |
dbb410c3 AM |
779 | } |
780 | ||
3d7f7666 | 781 | bfd_boolean |
dd863624 | 782 | _bfd_elf_setup_sections (bfd *abfd) |
3d7f7666 L |
783 | { |
784 | unsigned int i; | |
785 | unsigned int num_group = elf_tdata (abfd)->num_group; | |
786 | bfd_boolean result = TRUE; | |
dd863624 L |
787 | asection *s; |
788 | ||
789 | /* Process SHF_LINK_ORDER. */ | |
790 | for (s = abfd->sections; s != NULL; s = s->next) | |
791 | { | |
792 | Elf_Internal_Shdr *this_hdr = &elf_section_data (s)->this_hdr; | |
793 | if ((this_hdr->sh_flags & SHF_LINK_ORDER) != 0) | |
794 | { | |
795 | unsigned int elfsec = this_hdr->sh_link; | |
796 | /* FIXME: The old Intel compiler and old strip/objcopy may | |
797 | not set the sh_link or sh_info fields. Hence we could | |
798 | get the situation where elfsec is 0. */ | |
799 | if (elfsec == 0) | |
800 | { | |
4fbb74a6 | 801 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
dd863624 L |
802 | if (bed->link_order_error_handler) |
803 | bed->link_order_error_handler | |
804 | (_("%B: warning: sh_link not set for section `%A'"), | |
805 | abfd, s); | |
806 | } | |
807 | else | |
808 | { | |
91d6fa6a | 809 | asection *linksec = NULL; |
25bbc984 | 810 | |
4fbb74a6 AM |
811 | if (elfsec < elf_numsections (abfd)) |
812 | { | |
813 | this_hdr = elf_elfsections (abfd)[elfsec]; | |
91d6fa6a | 814 | linksec = this_hdr->bfd_section; |
4fbb74a6 | 815 | } |
25bbc984 L |
816 | |
817 | /* PR 1991, 2008: | |
818 | Some strip/objcopy may leave an incorrect value in | |
819 | sh_link. We don't want to proceed. */ | |
91d6fa6a | 820 | if (linksec == NULL) |
25bbc984 L |
821 | { |
822 | (*_bfd_error_handler) | |
823 | (_("%B: sh_link [%d] in section `%A' is incorrect"), | |
824 | s->owner, s, elfsec); | |
825 | result = FALSE; | |
826 | } | |
827 | ||
91d6fa6a | 828 | elf_linked_to_section (s) = linksec; |
dd863624 L |
829 | } |
830 | } | |
831 | } | |
3d7f7666 | 832 | |
dd863624 | 833 | /* Process section groups. */ |
3d7f7666 L |
834 | if (num_group == (unsigned) -1) |
835 | return result; | |
836 | ||
837 | for (i = 0; i < num_group; i++) | |
838 | { | |
839 | Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i]; | |
4b0e8a5f NC |
840 | Elf_Internal_Group *idx; |
841 | unsigned int n_elt; | |
3d7f7666 | 842 | |
4b0e8a5f NC |
843 | /* PR binutils/18758: Beware of corrupt binaries with invalid group data. */ |
844 | if (shdr == NULL || shdr->bfd_section == NULL || shdr->contents == NULL) | |
845 | { | |
846 | (*_bfd_error_handler) | |
847 | (_("%B: section group entry number %u is corrupt"), | |
848 | abfd, i); | |
849 | result = FALSE; | |
850 | continue; | |
851 | } | |
852 | ||
853 | idx = (Elf_Internal_Group *) shdr->contents; | |
854 | n_elt = shdr->sh_size / 4; | |
1b786873 | 855 | |
3d7f7666 L |
856 | while (--n_elt != 0) |
857 | if ((++idx)->shdr->bfd_section) | |
858 | elf_sec_group (idx->shdr->bfd_section) = shdr->bfd_section; | |
859 | else if (idx->shdr->sh_type == SHT_RELA | |
860 | || idx->shdr->sh_type == SHT_REL) | |
861 | /* We won't include relocation sections in section groups in | |
862 | output object files. We adjust the group section size here | |
863 | so that relocatable link will work correctly when | |
864 | relocation sections are in section group in input object | |
865 | files. */ | |
866 | shdr->bfd_section->size -= 4; | |
867 | else | |
868 | { | |
869 | /* There are some unknown sections in the group. */ | |
870 | (*_bfd_error_handler) | |
d003868e AM |
871 | (_("%B: unknown [%d] section `%s' in group [%s]"), |
872 | abfd, | |
3d7f7666 | 873 | (unsigned int) idx->shdr->sh_type, |
1b3a8575 AM |
874 | bfd_elf_string_from_elf_section (abfd, |
875 | (elf_elfheader (abfd) | |
876 | ->e_shstrndx), | |
877 | idx->shdr->sh_name), | |
3d7f7666 L |
878 | shdr->bfd_section->name); |
879 | result = FALSE; | |
880 | } | |
881 | } | |
882 | return result; | |
883 | } | |
884 | ||
72adc230 AM |
885 | bfd_boolean |
886 | bfd_elf_is_group_section (bfd *abfd ATTRIBUTE_UNUSED, const asection *sec) | |
887 | { | |
888 | return elf_next_in_group (sec) != NULL; | |
889 | } | |
890 | ||
f6fe1ccd L |
891 | static char * |
892 | convert_debug_to_zdebug (bfd *abfd, const char *name) | |
893 | { | |
894 | unsigned int len = strlen (name); | |
895 | char *new_name = bfd_alloc (abfd, len + 2); | |
896 | if (new_name == NULL) | |
897 | return NULL; | |
898 | new_name[0] = '.'; | |
899 | new_name[1] = 'z'; | |
900 | memcpy (new_name + 2, name + 1, len); | |
901 | return new_name; | |
902 | } | |
903 | ||
904 | static char * | |
905 | convert_zdebug_to_debug (bfd *abfd, const char *name) | |
906 | { | |
907 | unsigned int len = strlen (name); | |
908 | char *new_name = bfd_alloc (abfd, len); | |
909 | if (new_name == NULL) | |
910 | return NULL; | |
911 | new_name[0] = '.'; | |
912 | memcpy (new_name + 1, name + 2, len - 1); | |
913 | return new_name; | |
914 | } | |
915 | ||
252b5132 RH |
916 | /* Make a BFD section from an ELF section. We store a pointer to the |
917 | BFD section in the bfd_section field of the header. */ | |
918 | ||
b34976b6 | 919 | bfd_boolean |
217aa764 AM |
920 | _bfd_elf_make_section_from_shdr (bfd *abfd, |
921 | Elf_Internal_Shdr *hdr, | |
6dc132d9 L |
922 | const char *name, |
923 | int shindex) | |
252b5132 RH |
924 | { |
925 | asection *newsect; | |
926 | flagword flags; | |
9c5bfbb7 | 927 | const struct elf_backend_data *bed; |
252b5132 RH |
928 | |
929 | if (hdr->bfd_section != NULL) | |
4e011fb5 | 930 | return TRUE; |
252b5132 RH |
931 | |
932 | newsect = bfd_make_section_anyway (abfd, name); | |
933 | if (newsect == NULL) | |
b34976b6 | 934 | return FALSE; |
252b5132 | 935 | |
1829f4b2 AM |
936 | hdr->bfd_section = newsect; |
937 | elf_section_data (newsect)->this_hdr = *hdr; | |
6dc132d9 | 938 | elf_section_data (newsect)->this_idx = shindex; |
1829f4b2 | 939 | |
2f89ff8d L |
940 | /* Always use the real type/flags. */ |
941 | elf_section_type (newsect) = hdr->sh_type; | |
942 | elf_section_flags (newsect) = hdr->sh_flags; | |
943 | ||
252b5132 RH |
944 | newsect->filepos = hdr->sh_offset; |
945 | ||
946 | if (! bfd_set_section_vma (abfd, newsect, hdr->sh_addr) | |
947 | || ! bfd_set_section_size (abfd, newsect, hdr->sh_size) | |
948 | || ! bfd_set_section_alignment (abfd, newsect, | |
72de5009 | 949 | bfd_log2 (hdr->sh_addralign))) |
b34976b6 | 950 | return FALSE; |
252b5132 RH |
951 | |
952 | flags = SEC_NO_FLAGS; | |
953 | if (hdr->sh_type != SHT_NOBITS) | |
954 | flags |= SEC_HAS_CONTENTS; | |
dbb410c3 | 955 | if (hdr->sh_type == SHT_GROUP) |
b3096250 | 956 | flags |= SEC_GROUP | SEC_EXCLUDE; |
252b5132 RH |
957 | if ((hdr->sh_flags & SHF_ALLOC) != 0) |
958 | { | |
959 | flags |= SEC_ALLOC; | |
960 | if (hdr->sh_type != SHT_NOBITS) | |
961 | flags |= SEC_LOAD; | |
962 | } | |
963 | if ((hdr->sh_flags & SHF_WRITE) == 0) | |
964 | flags |= SEC_READONLY; | |
965 | if ((hdr->sh_flags & SHF_EXECINSTR) != 0) | |
966 | flags |= SEC_CODE; | |
967 | else if ((flags & SEC_LOAD) != 0) | |
968 | flags |= SEC_DATA; | |
f5fa8ca2 JJ |
969 | if ((hdr->sh_flags & SHF_MERGE) != 0) |
970 | { | |
971 | flags |= SEC_MERGE; | |
972 | newsect->entsize = hdr->sh_entsize; | |
973 | if ((hdr->sh_flags & SHF_STRINGS) != 0) | |
974 | flags |= SEC_STRINGS; | |
975 | } | |
dbb410c3 AM |
976 | if (hdr->sh_flags & SHF_GROUP) |
977 | if (!setup_group (abfd, hdr, newsect)) | |
b34976b6 | 978 | return FALSE; |
13ae64f3 JJ |
979 | if ((hdr->sh_flags & SHF_TLS) != 0) |
980 | flags |= SEC_THREAD_LOCAL; | |
18ae9cc1 L |
981 | if ((hdr->sh_flags & SHF_EXCLUDE) != 0) |
982 | flags |= SEC_EXCLUDE; | |
252b5132 | 983 | |
3d2b39cf | 984 | if ((flags & SEC_ALLOC) == 0) |
7a6cc5fb | 985 | { |
3d2b39cf L |
986 | /* The debugging sections appear to be recognized only by name, |
987 | not any sort of flag. Their SEC_ALLOC bits are cleared. */ | |
3d2b39cf L |
988 | if (name [0] == '.') |
989 | { | |
f073ced3 AM |
990 | const char *p; |
991 | int n; | |
992 | if (name[1] == 'd') | |
993 | p = ".debug", n = 6; | |
994 | else if (name[1] == 'g' && name[2] == 'n') | |
995 | p = ".gnu.linkonce.wi.", n = 17; | |
996 | else if (name[1] == 'g' && name[2] == 'd') | |
997 | p = ".gdb_index", n = 11; /* yes we really do mean 11. */ | |
998 | else if (name[1] == 'l') | |
999 | p = ".line", n = 5; | |
1000 | else if (name[1] == 's') | |
1001 | p = ".stab", n = 5; | |
1002 | else if (name[1] == 'z') | |
1003 | p = ".zdebug", n = 7; | |
1004 | else | |
1005 | p = NULL, n = 0; | |
1006 | if (p != NULL && strncmp (name, p, n) == 0) | |
3d2b39cf L |
1007 | flags |= SEC_DEBUGGING; |
1008 | } | |
1009 | } | |
252b5132 RH |
1010 | |
1011 | /* As a GNU extension, if the name begins with .gnu.linkonce, we | |
1012 | only link a single copy of the section. This is used to support | |
1013 | g++. g++ will emit each template expansion in its own section. | |
1014 | The symbols will be defined as weak, so that multiple definitions | |
1015 | are permitted. The GNU linker extension is to actually discard | |
1016 | all but one of the sections. */ | |
0112cd26 | 1017 | if (CONST_STRNEQ (name, ".gnu.linkonce") |
b885599b | 1018 | && elf_next_in_group (newsect) == NULL) |
252b5132 RH |
1019 | flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD; |
1020 | ||
fa152c49 JW |
1021 | bed = get_elf_backend_data (abfd); |
1022 | if (bed->elf_backend_section_flags) | |
1023 | if (! bed->elf_backend_section_flags (&flags, hdr)) | |
b34976b6 | 1024 | return FALSE; |
fa152c49 | 1025 | |
252b5132 | 1026 | if (! bfd_set_section_flags (abfd, newsect, flags)) |
b34976b6 | 1027 | return FALSE; |
252b5132 | 1028 | |
718175fa JK |
1029 | /* We do not parse the PT_NOTE segments as we are interested even in the |
1030 | separate debug info files which may have the segments offsets corrupted. | |
1031 | PT_NOTEs from the core files are currently not parsed using BFD. */ | |
1032 | if (hdr->sh_type == SHT_NOTE) | |
1033 | { | |
baea7ef1 | 1034 | bfd_byte *contents; |
718175fa | 1035 | |
baea7ef1 | 1036 | if (!bfd_malloc_and_get_section (abfd, newsect, &contents)) |
718175fa JK |
1037 | return FALSE; |
1038 | ||
baea7ef1 | 1039 | elf_parse_notes (abfd, (char *) contents, hdr->sh_size, -1); |
718175fa JK |
1040 | free (contents); |
1041 | } | |
1042 | ||
252b5132 RH |
1043 | if ((flags & SEC_ALLOC) != 0) |
1044 | { | |
1045 | Elf_Internal_Phdr *phdr; | |
6ffd7900 AM |
1046 | unsigned int i, nload; |
1047 | ||
1048 | /* Some ELF linkers produce binaries with all the program header | |
1049 | p_paddr fields zero. If we have such a binary with more than | |
1050 | one PT_LOAD header, then leave the section lma equal to vma | |
1051 | so that we don't create sections with overlapping lma. */ | |
1052 | phdr = elf_tdata (abfd)->phdr; | |
1053 | for (nload = 0, i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++) | |
1054 | if (phdr->p_paddr != 0) | |
1055 | break; | |
1056 | else if (phdr->p_type == PT_LOAD && phdr->p_memsz != 0) | |
1057 | ++nload; | |
1058 | if (i >= elf_elfheader (abfd)->e_phnum && nload > 1) | |
1059 | return TRUE; | |
252b5132 | 1060 | |
252b5132 RH |
1061 | phdr = elf_tdata (abfd)->phdr; |
1062 | for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++) | |
1063 | { | |
86b2281f AM |
1064 | if (((phdr->p_type == PT_LOAD |
1065 | && (hdr->sh_flags & SHF_TLS) == 0) | |
1066 | || phdr->p_type == PT_TLS) | |
9a83a553 | 1067 | && ELF_SECTION_IN_SEGMENT (hdr, phdr)) |
252b5132 | 1068 | { |
88967714 AM |
1069 | if ((flags & SEC_LOAD) == 0) |
1070 | newsect->lma = (phdr->p_paddr | |
1071 | + hdr->sh_addr - phdr->p_vaddr); | |
1072 | else | |
1073 | /* We used to use the same adjustment for SEC_LOAD | |
1074 | sections, but that doesn't work if the segment | |
1075 | is packed with code from multiple VMAs. | |
1076 | Instead we calculate the section LMA based on | |
1077 | the segment LMA. It is assumed that the | |
1078 | segment will contain sections with contiguous | |
1079 | LMAs, even if the VMAs are not. */ | |
1080 | newsect->lma = (phdr->p_paddr | |
1081 | + hdr->sh_offset - phdr->p_offset); | |
1082 | ||
1083 | /* With contiguous segments, we can't tell from file | |
1084 | offsets whether a section with zero size should | |
1085 | be placed at the end of one segment or the | |
1086 | beginning of the next. Decide based on vaddr. */ | |
1087 | if (hdr->sh_addr >= phdr->p_vaddr | |
1088 | && (hdr->sh_addr + hdr->sh_size | |
1089 | <= phdr->p_vaddr + phdr->p_memsz)) | |
1090 | break; | |
252b5132 RH |
1091 | } |
1092 | } | |
1093 | } | |
1094 | ||
4a114e3e L |
1095 | /* Compress/decompress DWARF debug sections with names: .debug_* and |
1096 | .zdebug_*, after the section flags is set. */ | |
1097 | if ((flags & SEC_DEBUGGING) | |
1098 | && ((name[1] == 'd' && name[6] == '_') | |
1099 | || (name[1] == 'z' && name[7] == '_'))) | |
1100 | { | |
1101 | enum { nothing, compress, decompress } action = nothing; | |
151411f8 | 1102 | int compression_header_size; |
dab394de | 1103 | bfd_size_type uncompressed_size; |
151411f8 L |
1104 | bfd_boolean compressed |
1105 | = bfd_is_section_compressed_with_header (abfd, newsect, | |
dab394de L |
1106 | &compression_header_size, |
1107 | &uncompressed_size); | |
4a114e3e | 1108 | |
151411f8 | 1109 | if (compressed) |
4a114e3e L |
1110 | { |
1111 | /* Compressed section. Check if we should decompress. */ | |
1112 | if ((abfd->flags & BFD_DECOMPRESS)) | |
1113 | action = decompress; | |
1114 | } | |
151411f8 L |
1115 | |
1116 | /* Compress the uncompressed section or convert from/to .zdebug* | |
1117 | section. Check if we should compress. */ | |
1118 | if (action == nothing) | |
4a114e3e | 1119 | { |
151411f8 L |
1120 | if (newsect->size != 0 |
1121 | && (abfd->flags & BFD_COMPRESS) | |
1122 | && compression_header_size >= 0 | |
dab394de | 1123 | && uncompressed_size > 0 |
151411f8 L |
1124 | && (!compressed |
1125 | || ((compression_header_size > 0) | |
1126 | != ((abfd->flags & BFD_COMPRESS_GABI) != 0)))) | |
4a114e3e | 1127 | action = compress; |
151411f8 L |
1128 | else |
1129 | return TRUE; | |
4a114e3e L |
1130 | } |
1131 | ||
151411f8 | 1132 | if (action == compress) |
4a114e3e | 1133 | { |
4a114e3e L |
1134 | if (!bfd_init_section_compress_status (abfd, newsect)) |
1135 | { | |
1136 | (*_bfd_error_handler) | |
bc823199 | 1137 | (_("%B: unable to initialize compress status for section %s"), |
4a114e3e L |
1138 | abfd, name); |
1139 | return FALSE; | |
1140 | } | |
151411f8 L |
1141 | } |
1142 | else | |
1143 | { | |
4a114e3e L |
1144 | if (!bfd_init_section_decompress_status (abfd, newsect)) |
1145 | { | |
1146 | (*_bfd_error_handler) | |
bc823199 | 1147 | (_("%B: unable to initialize decompress status for section %s"), |
4a114e3e L |
1148 | abfd, name); |
1149 | return FALSE; | |
1150 | } | |
151411f8 L |
1151 | } |
1152 | ||
f6fe1ccd | 1153 | if (abfd->is_linker_input) |
151411f8 | 1154 | { |
f6fe1ccd L |
1155 | if (name[1] == 'z' |
1156 | && (action == decompress | |
1157 | || (action == compress | |
1158 | && (abfd->flags & BFD_COMPRESS_GABI) != 0))) | |
4e011fb5 | 1159 | { |
f6fe1ccd L |
1160 | /* Convert section name from .zdebug_* to .debug_* so |
1161 | that linker will consider this section as a debug | |
1162 | section. */ | |
1163 | char *new_name = convert_zdebug_to_debug (abfd, name); | |
151411f8 L |
1164 | if (new_name == NULL) |
1165 | return FALSE; | |
f6fe1ccd | 1166 | bfd_rename_section (abfd, newsect, new_name); |
151411f8 | 1167 | } |
4a114e3e | 1168 | } |
f6fe1ccd L |
1169 | else |
1170 | /* For objdump, don't rename the section. For objcopy, delay | |
1171 | section rename to elf_fake_sections. */ | |
1172 | newsect->flags |= SEC_ELF_RENAME; | |
4a114e3e L |
1173 | } |
1174 | ||
b34976b6 | 1175 | return TRUE; |
252b5132 RH |
1176 | } |
1177 | ||
252b5132 RH |
1178 | const char *const bfd_elf_section_type_names[] = { |
1179 | "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB", | |
1180 | "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE", | |
1181 | "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM", | |
1182 | }; | |
1183 | ||
1049f94e | 1184 | /* ELF relocs are against symbols. If we are producing relocatable |
252b5132 RH |
1185 | output, and the reloc is against an external symbol, and nothing |
1186 | has given us any additional addend, the resulting reloc will also | |
1187 | be against the same symbol. In such a case, we don't want to | |
1188 | change anything about the way the reloc is handled, since it will | |
1189 | all be done at final link time. Rather than put special case code | |
1190 | into bfd_perform_relocation, all the reloc types use this howto | |
1191 | function. It just short circuits the reloc if producing | |
1049f94e | 1192 | relocatable output against an external symbol. */ |
252b5132 | 1193 | |
252b5132 | 1194 | bfd_reloc_status_type |
217aa764 AM |
1195 | bfd_elf_generic_reloc (bfd *abfd ATTRIBUTE_UNUSED, |
1196 | arelent *reloc_entry, | |
1197 | asymbol *symbol, | |
1198 | void *data ATTRIBUTE_UNUSED, | |
1199 | asection *input_section, | |
1200 | bfd *output_bfd, | |
1201 | char **error_message ATTRIBUTE_UNUSED) | |
1202 | { | |
1203 | if (output_bfd != NULL | |
252b5132 RH |
1204 | && (symbol->flags & BSF_SECTION_SYM) == 0 |
1205 | && (! reloc_entry->howto->partial_inplace | |
1206 | || reloc_entry->addend == 0)) | |
1207 | { | |
1208 | reloc_entry->address += input_section->output_offset; | |
1209 | return bfd_reloc_ok; | |
1210 | } | |
1211 | ||
1212 | return bfd_reloc_continue; | |
1213 | } | |
1214 | \f | |
0ac4564e L |
1215 | /* Copy the program header and other data from one object module to |
1216 | another. */ | |
252b5132 | 1217 | |
b34976b6 | 1218 | bfd_boolean |
217aa764 | 1219 | _bfd_elf_copy_private_bfd_data (bfd *ibfd, bfd *obfd) |
2d502050 L |
1220 | { |
1221 | if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour | |
1222 | || bfd_get_flavour (obfd) != bfd_target_elf_flavour) | |
b34976b6 | 1223 | return TRUE; |
2d502050 | 1224 | |
57b828ef L |
1225 | if (!elf_flags_init (obfd)) |
1226 | { | |
1227 | elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags; | |
1228 | elf_flags_init (obfd) = TRUE; | |
1229 | } | |
2d502050 | 1230 | |
0ac4564e | 1231 | elf_gp (obfd) = elf_gp (ibfd); |
57b828ef L |
1232 | |
1233 | /* Also copy the EI_OSABI field. */ | |
1234 | elf_elfheader (obfd)->e_ident[EI_OSABI] = | |
1235 | elf_elfheader (ibfd)->e_ident[EI_OSABI]; | |
104d59d1 JM |
1236 | |
1237 | /* Copy object attributes. */ | |
1238 | _bfd_elf_copy_obj_attributes (ibfd, obfd); | |
63b9bbb7 NC |
1239 | |
1240 | /* This is an feature for objcopy --only-keep-debug: When a section's type | |
1241 | is changed to NOBITS, we preserve the sh_link and sh_info fields so that | |
1242 | they can be matched up with the original. */ | |
1243 | Elf_Internal_Shdr ** iheaders = elf_elfsections (ibfd); | |
1244 | Elf_Internal_Shdr ** oheaders = elf_elfsections (obfd); | |
1245 | ||
1246 | if (iheaders != NULL && oheaders != NULL) | |
1247 | { | |
1248 | unsigned int i; | |
1249 | ||
1250 | for (i = 0; i < elf_numsections (obfd); i++) | |
1251 | { | |
1252 | unsigned int j; | |
1253 | Elf_Internal_Shdr * oheader = oheaders[i]; | |
1254 | ||
1255 | if (oheader == NULL | |
1256 | || oheader->sh_type != SHT_NOBITS | |
1257 | || oheader->sh_size == 0 | |
1258 | || (oheader->sh_info != 0 && oheader->sh_link != 0)) | |
1259 | continue; | |
1260 | ||
1261 | /* Scan for the matching section in the input bfd. | |
1262 | FIXME: We could use something better than a linear scan here. | |
1263 | Unfortunately we cannot compare names as the output string table | |
1264 | is empty, so instead we check size, address and type. */ | |
1265 | for (j = 0; j < elf_numsections (ibfd); j++) | |
1266 | { | |
1267 | Elf_Internal_Shdr * iheader = iheaders[j]; | |
1b786873 | 1268 | |
d4ac1f87 L |
1269 | /* Since --only-keep-debug turns all non-debug sections |
1270 | into SHT_NOBITS sections, the output SHT_NOBITS type | |
1271 | matches any input type. */ | |
1272 | if ((oheader->sh_type == SHT_NOBITS | |
64140f86 | 1273 | || iheader->sh_type == oheader->sh_type) |
d4ac1f87 L |
1274 | && iheader->sh_flags == oheader->sh_flags |
1275 | && iheader->sh_addralign == oheader->sh_addralign | |
1276 | && iheader->sh_entsize == oheader->sh_entsize | |
63b9bbb7 NC |
1277 | && iheader->sh_size == oheader->sh_size |
1278 | && iheader->sh_addr == oheader->sh_addr | |
1279 | && (iheader->sh_info != oheader->sh_info | |
1280 | || iheader->sh_link != oheader->sh_link)) | |
1281 | { | |
1282 | /* Note: Strictly speaking these assignments are wrong. | |
1283 | The sh_link and sh_info fields should point to the | |
1284 | relevent sections in the output BFD, which may not be in | |
1285 | the same location as they were in the input BFD. But the | |
1286 | whole point of this action is to preserve the original | |
1287 | values of the sh_link and sh_info fields, so that they | |
1288 | can be matched up with the section headers in the | |
1289 | original file. So strictly speaking we may be creating | |
1290 | an invalid ELF file, but it is only for a file that just | |
1291 | contains debug info and only for sections without any | |
1292 | contents. */ | |
1293 | if (oheader->sh_link == 0) | |
1294 | oheader->sh_link = iheader->sh_link; | |
1295 | if (oheader->sh_info == 0) | |
1296 | oheader->sh_info = iheader->sh_info; | |
1297 | break; | |
1298 | } | |
1299 | } | |
1300 | } | |
1301 | } | |
1302 | ||
b34976b6 | 1303 | return TRUE; |
2d502050 L |
1304 | } |
1305 | ||
cedc298e L |
1306 | static const char * |
1307 | get_segment_type (unsigned int p_type) | |
1308 | { | |
1309 | const char *pt; | |
1310 | switch (p_type) | |
1311 | { | |
1312 | case PT_NULL: pt = "NULL"; break; | |
1313 | case PT_LOAD: pt = "LOAD"; break; | |
1314 | case PT_DYNAMIC: pt = "DYNAMIC"; break; | |
1315 | case PT_INTERP: pt = "INTERP"; break; | |
1316 | case PT_NOTE: pt = "NOTE"; break; | |
1317 | case PT_SHLIB: pt = "SHLIB"; break; | |
1318 | case PT_PHDR: pt = "PHDR"; break; | |
1319 | case PT_TLS: pt = "TLS"; break; | |
1320 | case PT_GNU_EH_FRAME: pt = "EH_FRAME"; break; | |
2b05f1b7 | 1321 | case PT_GNU_STACK: pt = "STACK"; break; |
cedc298e L |
1322 | case PT_GNU_RELRO: pt = "RELRO"; break; |
1323 | default: pt = NULL; break; | |
1324 | } | |
1325 | return pt; | |
1326 | } | |
1327 | ||
f0b79d91 L |
1328 | /* Print out the program headers. */ |
1329 | ||
b34976b6 | 1330 | bfd_boolean |
217aa764 | 1331 | _bfd_elf_print_private_bfd_data (bfd *abfd, void *farg) |
252b5132 | 1332 | { |
a50b1753 | 1333 | FILE *f = (FILE *) farg; |
252b5132 RH |
1334 | Elf_Internal_Phdr *p; |
1335 | asection *s; | |
1336 | bfd_byte *dynbuf = NULL; | |
1337 | ||
1338 | p = elf_tdata (abfd)->phdr; | |
1339 | if (p != NULL) | |
1340 | { | |
1341 | unsigned int i, c; | |
1342 | ||
1343 | fprintf (f, _("\nProgram Header:\n")); | |
1344 | c = elf_elfheader (abfd)->e_phnum; | |
1345 | for (i = 0; i < c; i++, p++) | |
1346 | { | |
cedc298e | 1347 | const char *pt = get_segment_type (p->p_type); |
252b5132 RH |
1348 | char buf[20]; |
1349 | ||
cedc298e | 1350 | if (pt == NULL) |
252b5132 | 1351 | { |
cedc298e L |
1352 | sprintf (buf, "0x%lx", p->p_type); |
1353 | pt = buf; | |
252b5132 | 1354 | } |
dc810e39 | 1355 | fprintf (f, "%8s off 0x", pt); |
60b89a18 | 1356 | bfd_fprintf_vma (abfd, f, p->p_offset); |
252b5132 | 1357 | fprintf (f, " vaddr 0x"); |
60b89a18 | 1358 | bfd_fprintf_vma (abfd, f, p->p_vaddr); |
252b5132 | 1359 | fprintf (f, " paddr 0x"); |
60b89a18 | 1360 | bfd_fprintf_vma (abfd, f, p->p_paddr); |
252b5132 RH |
1361 | fprintf (f, " align 2**%u\n", bfd_log2 (p->p_align)); |
1362 | fprintf (f, " filesz 0x"); | |
60b89a18 | 1363 | bfd_fprintf_vma (abfd, f, p->p_filesz); |
252b5132 | 1364 | fprintf (f, " memsz 0x"); |
60b89a18 | 1365 | bfd_fprintf_vma (abfd, f, p->p_memsz); |
252b5132 RH |
1366 | fprintf (f, " flags %c%c%c", |
1367 | (p->p_flags & PF_R) != 0 ? 'r' : '-', | |
1368 | (p->p_flags & PF_W) != 0 ? 'w' : '-', | |
1369 | (p->p_flags & PF_X) != 0 ? 'x' : '-'); | |
dc810e39 AM |
1370 | if ((p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X)) != 0) |
1371 | fprintf (f, " %lx", p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X)); | |
252b5132 RH |
1372 | fprintf (f, "\n"); |
1373 | } | |
1374 | } | |
1375 | ||
1376 | s = bfd_get_section_by_name (abfd, ".dynamic"); | |
1377 | if (s != NULL) | |
1378 | { | |
cb33740c | 1379 | unsigned int elfsec; |
dc810e39 | 1380 | unsigned long shlink; |
252b5132 RH |
1381 | bfd_byte *extdyn, *extdynend; |
1382 | size_t extdynsize; | |
217aa764 | 1383 | void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *); |
252b5132 RH |
1384 | |
1385 | fprintf (f, _("\nDynamic Section:\n")); | |
1386 | ||
eea6121a | 1387 | if (!bfd_malloc_and_get_section (abfd, s, &dynbuf)) |
252b5132 RH |
1388 | goto error_return; |
1389 | ||
1390 | elfsec = _bfd_elf_section_from_bfd_section (abfd, s); | |
cb33740c | 1391 | if (elfsec == SHN_BAD) |
252b5132 | 1392 | goto error_return; |
dc810e39 | 1393 | shlink = elf_elfsections (abfd)[elfsec]->sh_link; |
252b5132 RH |
1394 | |
1395 | extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn; | |
1396 | swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in; | |
1397 | ||
1398 | extdyn = dynbuf; | |
06614111 NC |
1399 | /* PR 17512: file: 6f427532. */ |
1400 | if (s->size < extdynsize) | |
1401 | goto error_return; | |
eea6121a | 1402 | extdynend = extdyn + s->size; |
1036838a NC |
1403 | /* PR 17512: file: id:000006,sig:06,src:000000,op:flip4,pos:5664. |
1404 | Fix range check. */ | |
1405 | for (; extdyn <= (extdynend - extdynsize); extdyn += extdynsize) | |
252b5132 RH |
1406 | { |
1407 | Elf_Internal_Dyn dyn; | |
ad9563d6 | 1408 | const char *name = ""; |
252b5132 | 1409 | char ab[20]; |
b34976b6 | 1410 | bfd_boolean stringp; |
ad9563d6 | 1411 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
252b5132 | 1412 | |
217aa764 | 1413 | (*swap_dyn_in) (abfd, extdyn, &dyn); |
252b5132 RH |
1414 | |
1415 | if (dyn.d_tag == DT_NULL) | |
1416 | break; | |
1417 | ||
b34976b6 | 1418 | stringp = FALSE; |
252b5132 RH |
1419 | switch (dyn.d_tag) |
1420 | { | |
1421 | default: | |
ad9563d6 CM |
1422 | if (bed->elf_backend_get_target_dtag) |
1423 | name = (*bed->elf_backend_get_target_dtag) (dyn.d_tag); | |
1424 | ||
1425 | if (!strcmp (name, "")) | |
1426 | { | |
1427 | sprintf (ab, "0x%lx", (unsigned long) dyn.d_tag); | |
1428 | name = ab; | |
1429 | } | |
252b5132 RH |
1430 | break; |
1431 | ||
b34976b6 | 1432 | case DT_NEEDED: name = "NEEDED"; stringp = TRUE; break; |
252b5132 RH |
1433 | case DT_PLTRELSZ: name = "PLTRELSZ"; break; |
1434 | case DT_PLTGOT: name = "PLTGOT"; break; | |
1435 | case DT_HASH: name = "HASH"; break; | |
1436 | case DT_STRTAB: name = "STRTAB"; break; | |
1437 | case DT_SYMTAB: name = "SYMTAB"; break; | |
1438 | case DT_RELA: name = "RELA"; break; | |
1439 | case DT_RELASZ: name = "RELASZ"; break; | |
1440 | case DT_RELAENT: name = "RELAENT"; break; | |
1441 | case DT_STRSZ: name = "STRSZ"; break; | |
1442 | case DT_SYMENT: name = "SYMENT"; break; | |
1443 | case DT_INIT: name = "INIT"; break; | |
1444 | case DT_FINI: name = "FINI"; break; | |
b34976b6 AM |
1445 | case DT_SONAME: name = "SONAME"; stringp = TRUE; break; |
1446 | case DT_RPATH: name = "RPATH"; stringp = TRUE; break; | |
252b5132 RH |
1447 | case DT_SYMBOLIC: name = "SYMBOLIC"; break; |
1448 | case DT_REL: name = "REL"; break; | |
1449 | case DT_RELSZ: name = "RELSZ"; break; | |
1450 | case DT_RELENT: name = "RELENT"; break; | |
1451 | case DT_PLTREL: name = "PLTREL"; break; | |
1452 | case DT_DEBUG: name = "DEBUG"; break; | |
1453 | case DT_TEXTREL: name = "TEXTREL"; break; | |
1454 | case DT_JMPREL: name = "JMPREL"; break; | |
94558834 L |
1455 | case DT_BIND_NOW: name = "BIND_NOW"; break; |
1456 | case DT_INIT_ARRAY: name = "INIT_ARRAY"; break; | |
1457 | case DT_FINI_ARRAY: name = "FINI_ARRAY"; break; | |
1458 | case DT_INIT_ARRAYSZ: name = "INIT_ARRAYSZ"; break; | |
1459 | case DT_FINI_ARRAYSZ: name = "FINI_ARRAYSZ"; break; | |
b34976b6 | 1460 | case DT_RUNPATH: name = "RUNPATH"; stringp = TRUE; break; |
94558834 L |
1461 | case DT_FLAGS: name = "FLAGS"; break; |
1462 | case DT_PREINIT_ARRAY: name = "PREINIT_ARRAY"; break; | |
1463 | case DT_PREINIT_ARRAYSZ: name = "PREINIT_ARRAYSZ"; break; | |
d48188b9 | 1464 | case DT_CHECKSUM: name = "CHECKSUM"; break; |
94558834 L |
1465 | case DT_PLTPADSZ: name = "PLTPADSZ"; break; |
1466 | case DT_MOVEENT: name = "MOVEENT"; break; | |
1467 | case DT_MOVESZ: name = "MOVESZ"; break; | |
1468 | case DT_FEATURE: name = "FEATURE"; break; | |
1469 | case DT_POSFLAG_1: name = "POSFLAG_1"; break; | |
1470 | case DT_SYMINSZ: name = "SYMINSZ"; break; | |
1471 | case DT_SYMINENT: name = "SYMINENT"; break; | |
b34976b6 AM |
1472 | case DT_CONFIG: name = "CONFIG"; stringp = TRUE; break; |
1473 | case DT_DEPAUDIT: name = "DEPAUDIT"; stringp = TRUE; break; | |
1474 | case DT_AUDIT: name = "AUDIT"; stringp = TRUE; break; | |
94558834 L |
1475 | case DT_PLTPAD: name = "PLTPAD"; break; |
1476 | case DT_MOVETAB: name = "MOVETAB"; break; | |
1477 | case DT_SYMINFO: name = "SYMINFO"; break; | |
1478 | case DT_RELACOUNT: name = "RELACOUNT"; break; | |
1479 | case DT_RELCOUNT: name = "RELCOUNT"; break; | |
1480 | case DT_FLAGS_1: name = "FLAGS_1"; break; | |
252b5132 RH |
1481 | case DT_VERSYM: name = "VERSYM"; break; |
1482 | case DT_VERDEF: name = "VERDEF"; break; | |
1483 | case DT_VERDEFNUM: name = "VERDEFNUM"; break; | |
1484 | case DT_VERNEED: name = "VERNEED"; break; | |
1485 | case DT_VERNEEDNUM: name = "VERNEEDNUM"; break; | |
b34976b6 | 1486 | case DT_AUXILIARY: name = "AUXILIARY"; stringp = TRUE; break; |
94558834 | 1487 | case DT_USED: name = "USED"; break; |
b34976b6 | 1488 | case DT_FILTER: name = "FILTER"; stringp = TRUE; break; |
fdc90cb4 | 1489 | case DT_GNU_HASH: name = "GNU_HASH"; break; |
252b5132 RH |
1490 | } |
1491 | ||
ad9563d6 | 1492 | fprintf (f, " %-20s ", name); |
252b5132 | 1493 | if (! stringp) |
a1f3c56e AN |
1494 | { |
1495 | fprintf (f, "0x"); | |
1496 | bfd_fprintf_vma (abfd, f, dyn.d_un.d_val); | |
1497 | } | |
252b5132 RH |
1498 | else |
1499 | { | |
1500 | const char *string; | |
dc810e39 | 1501 | unsigned int tagv = dyn.d_un.d_val; |
252b5132 | 1502 | |
dc810e39 | 1503 | string = bfd_elf_string_from_elf_section (abfd, shlink, tagv); |
252b5132 RH |
1504 | if (string == NULL) |
1505 | goto error_return; | |
1506 | fprintf (f, "%s", string); | |
1507 | } | |
1508 | fprintf (f, "\n"); | |
1509 | } | |
1510 | ||
1511 | free (dynbuf); | |
1512 | dynbuf = NULL; | |
1513 | } | |
1514 | ||
1515 | if ((elf_dynverdef (abfd) != 0 && elf_tdata (abfd)->verdef == NULL) | |
1516 | || (elf_dynverref (abfd) != 0 && elf_tdata (abfd)->verref == NULL)) | |
1517 | { | |
fc0e6df6 | 1518 | if (! _bfd_elf_slurp_version_tables (abfd, FALSE)) |
b34976b6 | 1519 | return FALSE; |
252b5132 RH |
1520 | } |
1521 | ||
1522 | if (elf_dynverdef (abfd) != 0) | |
1523 | { | |
1524 | Elf_Internal_Verdef *t; | |
1525 | ||
1526 | fprintf (f, _("\nVersion definitions:\n")); | |
1527 | for (t = elf_tdata (abfd)->verdef; t != NULL; t = t->vd_nextdef) | |
1528 | { | |
1529 | fprintf (f, "%d 0x%2.2x 0x%8.8lx %s\n", t->vd_ndx, | |
d0fb9a8d JJ |
1530 | t->vd_flags, t->vd_hash, |
1531 | t->vd_nodename ? t->vd_nodename : "<corrupt>"); | |
1532 | if (t->vd_auxptr != NULL && t->vd_auxptr->vda_nextptr != NULL) | |
252b5132 RH |
1533 | { |
1534 | Elf_Internal_Verdaux *a; | |
1535 | ||
1536 | fprintf (f, "\t"); | |
1537 | for (a = t->vd_auxptr->vda_nextptr; | |
1538 | a != NULL; | |
1539 | a = a->vda_nextptr) | |
d0fb9a8d JJ |
1540 | fprintf (f, "%s ", |
1541 | a->vda_nodename ? a->vda_nodename : "<corrupt>"); | |
252b5132 RH |
1542 | fprintf (f, "\n"); |
1543 | } | |
1544 | } | |
1545 | } | |
1546 | ||
1547 | if (elf_dynverref (abfd) != 0) | |
1548 | { | |
1549 | Elf_Internal_Verneed *t; | |
1550 | ||
1551 | fprintf (f, _("\nVersion References:\n")); | |
1552 | for (t = elf_tdata (abfd)->verref; t != NULL; t = t->vn_nextref) | |
1553 | { | |
1554 | Elf_Internal_Vernaux *a; | |
1555 | ||
d0fb9a8d JJ |
1556 | fprintf (f, _(" required from %s:\n"), |
1557 | t->vn_filename ? t->vn_filename : "<corrupt>"); | |
252b5132 RH |
1558 | for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) |
1559 | fprintf (f, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a->vna_hash, | |
d0fb9a8d JJ |
1560 | a->vna_flags, a->vna_other, |
1561 | a->vna_nodename ? a->vna_nodename : "<corrupt>"); | |
252b5132 RH |
1562 | } |
1563 | } | |
1564 | ||
b34976b6 | 1565 | return TRUE; |
252b5132 RH |
1566 | |
1567 | error_return: | |
1568 | if (dynbuf != NULL) | |
1569 | free (dynbuf); | |
b34976b6 | 1570 | return FALSE; |
252b5132 RH |
1571 | } |
1572 | ||
bb4d2ac2 L |
1573 | /* Get version string. */ |
1574 | ||
1575 | const char * | |
60bb06bc L |
1576 | _bfd_elf_get_symbol_version_string (bfd *abfd, asymbol *symbol, |
1577 | bfd_boolean *hidden) | |
bb4d2ac2 L |
1578 | { |
1579 | const char *version_string = NULL; | |
1580 | if (elf_dynversym (abfd) != 0 | |
1581 | && (elf_dynverdef (abfd) != 0 || elf_dynverref (abfd) != 0)) | |
1582 | { | |
1583 | unsigned int vernum = ((elf_symbol_type *) symbol)->version; | |
1584 | ||
1585 | *hidden = (vernum & VERSYM_HIDDEN) != 0; | |
1586 | vernum &= VERSYM_VERSION; | |
1587 | ||
1588 | if (vernum == 0) | |
1589 | version_string = ""; | |
1590 | else if (vernum == 1) | |
1591 | version_string = "Base"; | |
1592 | else if (vernum <= elf_tdata (abfd)->cverdefs) | |
1593 | version_string = | |
1594 | elf_tdata (abfd)->verdef[vernum - 1].vd_nodename; | |
1595 | else | |
1596 | { | |
1597 | Elf_Internal_Verneed *t; | |
1598 | ||
1599 | version_string = ""; | |
1600 | for (t = elf_tdata (abfd)->verref; | |
1601 | t != NULL; | |
1602 | t = t->vn_nextref) | |
1603 | { | |
1604 | Elf_Internal_Vernaux *a; | |
1605 | ||
1606 | for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) | |
1607 | { | |
1608 | if (a->vna_other == vernum) | |
1609 | { | |
1610 | version_string = a->vna_nodename; | |
1611 | break; | |
1612 | } | |
1613 | } | |
1614 | } | |
1615 | } | |
1616 | } | |
1617 | return version_string; | |
1618 | } | |
1619 | ||
252b5132 RH |
1620 | /* Display ELF-specific fields of a symbol. */ |
1621 | ||
1622 | void | |
217aa764 AM |
1623 | bfd_elf_print_symbol (bfd *abfd, |
1624 | void *filep, | |
1625 | asymbol *symbol, | |
1626 | bfd_print_symbol_type how) | |
252b5132 | 1627 | { |
a50b1753 | 1628 | FILE *file = (FILE *) filep; |
252b5132 RH |
1629 | switch (how) |
1630 | { | |
1631 | case bfd_print_symbol_name: | |
1632 | fprintf (file, "%s", symbol->name); | |
1633 | break; | |
1634 | case bfd_print_symbol_more: | |
1635 | fprintf (file, "elf "); | |
60b89a18 | 1636 | bfd_fprintf_vma (abfd, file, symbol->value); |
0af1713e | 1637 | fprintf (file, " %lx", (unsigned long) symbol->flags); |
252b5132 RH |
1638 | break; |
1639 | case bfd_print_symbol_all: | |
1640 | { | |
4e8a9624 AM |
1641 | const char *section_name; |
1642 | const char *name = NULL; | |
9c5bfbb7 | 1643 | const struct elf_backend_data *bed; |
7a13edea | 1644 | unsigned char st_other; |
dbb410c3 | 1645 | bfd_vma val; |
bb4d2ac2 L |
1646 | const char *version_string; |
1647 | bfd_boolean hidden; | |
c044fabd | 1648 | |
252b5132 | 1649 | section_name = symbol->section ? symbol->section->name : "(*none*)"; |
587ff49e RH |
1650 | |
1651 | bed = get_elf_backend_data (abfd); | |
1652 | if (bed->elf_backend_print_symbol_all) | |
c044fabd | 1653 | name = (*bed->elf_backend_print_symbol_all) (abfd, filep, symbol); |
587ff49e RH |
1654 | |
1655 | if (name == NULL) | |
1656 | { | |
7ee38065 | 1657 | name = symbol->name; |
217aa764 | 1658 | bfd_print_symbol_vandf (abfd, file, symbol); |
587ff49e RH |
1659 | } |
1660 | ||
252b5132 RH |
1661 | fprintf (file, " %s\t", section_name); |
1662 | /* Print the "other" value for a symbol. For common symbols, | |
1663 | we've already printed the size; now print the alignment. | |
1664 | For other symbols, we have no specified alignment, and | |
1665 | we've printed the address; now print the size. */ | |
dcf6c779 | 1666 | if (symbol->section && bfd_is_com_section (symbol->section)) |
dbb410c3 AM |
1667 | val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_value; |
1668 | else | |
1669 | val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_size; | |
1670 | bfd_fprintf_vma (abfd, file, val); | |
252b5132 RH |
1671 | |
1672 | /* If we have version information, print it. */ | |
60bb06bc L |
1673 | version_string = _bfd_elf_get_symbol_version_string (abfd, |
1674 | symbol, | |
1675 | &hidden); | |
bb4d2ac2 | 1676 | if (version_string) |
252b5132 | 1677 | { |
bb4d2ac2 | 1678 | if (!hidden) |
252b5132 RH |
1679 | fprintf (file, " %-11s", version_string); |
1680 | else | |
1681 | { | |
1682 | int i; | |
1683 | ||
1684 | fprintf (file, " (%s)", version_string); | |
1685 | for (i = 10 - strlen (version_string); i > 0; --i) | |
1686 | putc (' ', file); | |
1687 | } | |
1688 | } | |
1689 | ||
1690 | /* If the st_other field is not zero, print it. */ | |
7a13edea | 1691 | st_other = ((elf_symbol_type *) symbol)->internal_elf_sym.st_other; |
c044fabd | 1692 | |
7a13edea NC |
1693 | switch (st_other) |
1694 | { | |
1695 | case 0: break; | |
1696 | case STV_INTERNAL: fprintf (file, " .internal"); break; | |
1697 | case STV_HIDDEN: fprintf (file, " .hidden"); break; | |
1698 | case STV_PROTECTED: fprintf (file, " .protected"); break; | |
1699 | default: | |
1700 | /* Some other non-defined flags are also present, so print | |
1701 | everything hex. */ | |
1702 | fprintf (file, " 0x%02x", (unsigned int) st_other); | |
1703 | } | |
252b5132 | 1704 | |
587ff49e | 1705 | fprintf (file, " %s", name); |
252b5132 RH |
1706 | } |
1707 | break; | |
1708 | } | |
1709 | } | |
252b5132 RH |
1710 | \f |
1711 | /* ELF .o/exec file reading */ | |
1712 | ||
c044fabd | 1713 | /* Create a new bfd section from an ELF section header. */ |
252b5132 | 1714 | |
b34976b6 | 1715 | bfd_boolean |
217aa764 | 1716 | bfd_section_from_shdr (bfd *abfd, unsigned int shindex) |
252b5132 | 1717 | { |
4fbb74a6 AM |
1718 | Elf_Internal_Shdr *hdr; |
1719 | Elf_Internal_Ehdr *ehdr; | |
1720 | const struct elf_backend_data *bed; | |
90937f86 | 1721 | const char *name; |
bf67003b NC |
1722 | bfd_boolean ret = TRUE; |
1723 | static bfd_boolean * sections_being_created = NULL; | |
5a4b0ccc | 1724 | static bfd * sections_being_created_abfd = NULL; |
bf67003b | 1725 | static unsigned int nesting = 0; |
252b5132 | 1726 | |
4fbb74a6 AM |
1727 | if (shindex >= elf_numsections (abfd)) |
1728 | return FALSE; | |
1729 | ||
bf67003b NC |
1730 | if (++ nesting > 3) |
1731 | { | |
1732 | /* PR17512: A corrupt ELF binary might contain a recursive group of | |
06614111 | 1733 | sections, with each the string indicies pointing to the next in the |
bf67003b NC |
1734 | loop. Detect this here, by refusing to load a section that we are |
1735 | already in the process of loading. We only trigger this test if | |
1736 | we have nested at least three sections deep as normal ELF binaries | |
5a4b0ccc NC |
1737 | can expect to recurse at least once. |
1738 | ||
1739 | FIXME: It would be better if this array was attached to the bfd, | |
1740 | rather than being held in a static pointer. */ | |
1741 | ||
1742 | if (sections_being_created_abfd != abfd) | |
1743 | sections_being_created = NULL; | |
bf67003b NC |
1744 | if (sections_being_created == NULL) |
1745 | { | |
1746 | /* FIXME: It would be more efficient to attach this array to the bfd somehow. */ | |
1747 | sections_being_created = (bfd_boolean *) | |
1748 | bfd_zalloc (abfd, elf_numsections (abfd) * sizeof (bfd_boolean)); | |
5a4b0ccc | 1749 | sections_being_created_abfd = abfd; |
bf67003b NC |
1750 | } |
1751 | if (sections_being_created [shindex]) | |
1752 | { | |
1753 | (*_bfd_error_handler) | |
1754 | (_("%B: warning: loop in section dependencies detected"), abfd); | |
1755 | return FALSE; | |
1756 | } | |
1757 | sections_being_created [shindex] = TRUE; | |
1758 | } | |
1759 | ||
4fbb74a6 AM |
1760 | hdr = elf_elfsections (abfd)[shindex]; |
1761 | ehdr = elf_elfheader (abfd); | |
1762 | name = bfd_elf_string_from_elf_section (abfd, ehdr->e_shstrndx, | |
1b3a8575 | 1763 | hdr->sh_name); |
933d961a | 1764 | if (name == NULL) |
bf67003b | 1765 | goto fail; |
252b5132 | 1766 | |
4fbb74a6 | 1767 | bed = get_elf_backend_data (abfd); |
252b5132 RH |
1768 | switch (hdr->sh_type) |
1769 | { | |
1770 | case SHT_NULL: | |
1771 | /* Inactive section. Throw it away. */ | |
bf67003b | 1772 | goto success; |
252b5132 | 1773 | |
bf67003b NC |
1774 | case SHT_PROGBITS: /* Normal section with contents. */ |
1775 | case SHT_NOBITS: /* .bss section. */ | |
1776 | case SHT_HASH: /* .hash section. */ | |
1777 | case SHT_NOTE: /* .note section. */ | |
25e27870 L |
1778 | case SHT_INIT_ARRAY: /* .init_array section. */ |
1779 | case SHT_FINI_ARRAY: /* .fini_array section. */ | |
1780 | case SHT_PREINIT_ARRAY: /* .preinit_array section. */ | |
7f1204bb | 1781 | case SHT_GNU_LIBLIST: /* .gnu.liblist section. */ |
fdc90cb4 | 1782 | case SHT_GNU_HASH: /* .gnu.hash section. */ |
bf67003b NC |
1783 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); |
1784 | goto success; | |
252b5132 | 1785 | |
797fc050 | 1786 | case SHT_DYNAMIC: /* Dynamic linking information. */ |
6dc132d9 | 1787 | if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex)) |
bf67003b NC |
1788 | goto fail; |
1789 | ||
cfcac11d NC |
1790 | if (hdr->sh_link > elf_numsections (abfd)) |
1791 | { | |
caa83f8b | 1792 | /* PR 10478: Accept Solaris binaries with a sh_link |
cfcac11d NC |
1793 | field set to SHN_BEFORE or SHN_AFTER. */ |
1794 | switch (bfd_get_arch (abfd)) | |
1795 | { | |
caa83f8b | 1796 | case bfd_arch_i386: |
cfcac11d NC |
1797 | case bfd_arch_sparc: |
1798 | if (hdr->sh_link == (SHN_LORESERVE & 0xffff) /* SHN_BEFORE */ | |
1799 | || hdr->sh_link == ((SHN_LORESERVE + 1) & 0xffff) /* SHN_AFTER */) | |
1800 | break; | |
1801 | /* Otherwise fall through. */ | |
1802 | default: | |
bf67003b | 1803 | goto fail; |
cfcac11d NC |
1804 | } |
1805 | } | |
1806 | else if (elf_elfsections (abfd)[hdr->sh_link] == NULL) | |
bf67003b | 1807 | goto fail; |
cfcac11d | 1808 | else if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_STRTAB) |
797fc050 AM |
1809 | { |
1810 | Elf_Internal_Shdr *dynsymhdr; | |
1811 | ||
1812 | /* The shared libraries distributed with hpux11 have a bogus | |
1813 | sh_link field for the ".dynamic" section. Find the | |
1814 | string table for the ".dynsym" section instead. */ | |
1815 | if (elf_dynsymtab (abfd) != 0) | |
1816 | { | |
1817 | dynsymhdr = elf_elfsections (abfd)[elf_dynsymtab (abfd)]; | |
1818 | hdr->sh_link = dynsymhdr->sh_link; | |
1819 | } | |
1820 | else | |
1821 | { | |
1822 | unsigned int i, num_sec; | |
1823 | ||
1824 | num_sec = elf_numsections (abfd); | |
1825 | for (i = 1; i < num_sec; i++) | |
1826 | { | |
1827 | dynsymhdr = elf_elfsections (abfd)[i]; | |
1828 | if (dynsymhdr->sh_type == SHT_DYNSYM) | |
1829 | { | |
1830 | hdr->sh_link = dynsymhdr->sh_link; | |
1831 | break; | |
1832 | } | |
1833 | } | |
1834 | } | |
1835 | } | |
bf67003b | 1836 | goto success; |
797fc050 | 1837 | |
bf67003b | 1838 | case SHT_SYMTAB: /* A symbol table. */ |
252b5132 | 1839 | if (elf_onesymtab (abfd) == shindex) |
bf67003b | 1840 | goto success; |
252b5132 | 1841 | |
a50b2160 | 1842 | if (hdr->sh_entsize != bed->s->sizeof_sym) |
bf67003b NC |
1843 | goto fail; |
1844 | ||
3337c1e5 | 1845 | if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size) |
eee3b786 AM |
1846 | { |
1847 | if (hdr->sh_size != 0) | |
bf67003b | 1848 | goto fail; |
eee3b786 AM |
1849 | /* Some assemblers erroneously set sh_info to one with a |
1850 | zero sh_size. ld sees this as a global symbol count | |
1851 | of (unsigned) -1. Fix it here. */ | |
1852 | hdr->sh_info = 0; | |
bf67003b | 1853 | goto success; |
eee3b786 | 1854 | } |
bf67003b | 1855 | |
16ad13ec NC |
1856 | /* PR 18854: A binary might contain more than one symbol table. |
1857 | Unusual, but possible. Warn, but continue. */ | |
1858 | if (elf_onesymtab (abfd) != 0) | |
1859 | { | |
1860 | (*_bfd_error_handler) | |
1861 | (_("%B: warning: multiple symbol tables detected - ignoring the table in section %u"), | |
1862 | abfd, shindex); | |
1863 | goto success; | |
1864 | } | |
252b5132 | 1865 | elf_onesymtab (abfd) = shindex; |
6a40cf0c NC |
1866 | elf_symtab_hdr (abfd) = *hdr; |
1867 | elf_elfsections (abfd)[shindex] = hdr = & elf_symtab_hdr (abfd); | |
252b5132 RH |
1868 | abfd->flags |= HAS_SYMS; |
1869 | ||
1870 | /* Sometimes a shared object will map in the symbol table. If | |
08a40648 AM |
1871 | SHF_ALLOC is set, and this is a shared object, then we also |
1872 | treat this section as a BFD section. We can not base the | |
1873 | decision purely on SHF_ALLOC, because that flag is sometimes | |
1874 | set in a relocatable object file, which would confuse the | |
1875 | linker. */ | |
252b5132 RH |
1876 | if ((hdr->sh_flags & SHF_ALLOC) != 0 |
1877 | && (abfd->flags & DYNAMIC) != 0 | |
6dc132d9 L |
1878 | && ! _bfd_elf_make_section_from_shdr (abfd, hdr, name, |
1879 | shindex)) | |
bf67003b | 1880 | goto fail; |
252b5132 | 1881 | |
1b3a8575 AM |
1882 | /* Go looking for SHT_SYMTAB_SHNDX too, since if there is one we |
1883 | can't read symbols without that section loaded as well. It | |
1884 | is most likely specified by the next section header. */ | |
6a40cf0c NC |
1885 | { |
1886 | elf_section_list * entry; | |
1887 | unsigned int i, num_sec; | |
1b3a8575 | 1888 | |
6a40cf0c NC |
1889 | for (entry = elf_symtab_shndx_list (abfd); entry != NULL; entry = entry->next) |
1890 | if (entry->hdr.sh_link == shindex) | |
1891 | goto success; | |
1892 | ||
1893 | num_sec = elf_numsections (abfd); | |
1894 | for (i = shindex + 1; i < num_sec; i++) | |
1895 | { | |
1896 | Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i]; | |
1897 | ||
1898 | if (hdr2->sh_type == SHT_SYMTAB_SHNDX | |
1899 | && hdr2->sh_link == shindex) | |
1900 | break; | |
1901 | } | |
1902 | ||
1903 | if (i == num_sec) | |
1904 | for (i = 1; i < shindex; i++) | |
1b3a8575 AM |
1905 | { |
1906 | Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i]; | |
6a40cf0c | 1907 | |
1b3a8575 AM |
1908 | if (hdr2->sh_type == SHT_SYMTAB_SHNDX |
1909 | && hdr2->sh_link == shindex) | |
1910 | break; | |
1911 | } | |
6a40cf0c NC |
1912 | |
1913 | if (i != shindex) | |
1914 | ret = bfd_section_from_shdr (abfd, i); | |
1915 | /* else FIXME: we have failed to find the symbol table - should we issue an error ? */ | |
1916 | goto success; | |
1917 | } | |
252b5132 | 1918 | |
bf67003b | 1919 | case SHT_DYNSYM: /* A dynamic symbol table. */ |
252b5132 | 1920 | if (elf_dynsymtab (abfd) == shindex) |
bf67003b | 1921 | goto success; |
252b5132 | 1922 | |
a50b2160 | 1923 | if (hdr->sh_entsize != bed->s->sizeof_sym) |
bf67003b NC |
1924 | goto fail; |
1925 | ||
eee3b786 AM |
1926 | if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size) |
1927 | { | |
1928 | if (hdr->sh_size != 0) | |
bf67003b NC |
1929 | goto fail; |
1930 | ||
eee3b786 AM |
1931 | /* Some linkers erroneously set sh_info to one with a |
1932 | zero sh_size. ld sees this as a global symbol count | |
1933 | of (unsigned) -1. Fix it here. */ | |
1934 | hdr->sh_info = 0; | |
bf67003b | 1935 | goto success; |
eee3b786 | 1936 | } |
bf67003b | 1937 | |
16ad13ec NC |
1938 | /* PR 18854: A binary might contain more than one dynamic symbol table. |
1939 | Unusual, but possible. Warn, but continue. */ | |
1940 | if (elf_dynsymtab (abfd) != 0) | |
1941 | { | |
1942 | (*_bfd_error_handler) | |
1943 | (_("%B: warning: multiple dynamic symbol tables detected - ignoring the table in section %u"), | |
1944 | abfd, shindex); | |
1945 | goto success; | |
1946 | } | |
252b5132 RH |
1947 | elf_dynsymtab (abfd) = shindex; |
1948 | elf_tdata (abfd)->dynsymtab_hdr = *hdr; | |
1949 | elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->dynsymtab_hdr; | |
1950 | abfd->flags |= HAS_SYMS; | |
1951 | ||
1952 | /* Besides being a symbol table, we also treat this as a regular | |
1953 | section, so that objcopy can handle it. */ | |
bf67003b NC |
1954 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); |
1955 | goto success; | |
252b5132 | 1956 | |
bf67003b | 1957 | case SHT_SYMTAB_SHNDX: /* Symbol section indices when >64k sections. */ |
6a40cf0c NC |
1958 | { |
1959 | elf_section_list * entry; | |
9ad5cbcf | 1960 | |
6a40cf0c NC |
1961 | for (entry = elf_symtab_shndx_list (abfd); entry != NULL; entry = entry->next) |
1962 | if (entry->ndx == shindex) | |
1963 | goto success; | |
1964 | ||
1965 | entry = bfd_alloc (abfd, sizeof * entry); | |
1966 | if (entry == NULL) | |
1967 | goto fail; | |
1968 | entry->ndx = shindex; | |
1969 | entry->hdr = * hdr; | |
1970 | entry->next = elf_symtab_shndx_list (abfd); | |
1971 | elf_symtab_shndx_list (abfd) = entry; | |
1972 | elf_elfsections (abfd)[shindex] = & entry->hdr; | |
1973 | goto success; | |
1974 | } | |
9ad5cbcf | 1975 | |
bf67003b | 1976 | case SHT_STRTAB: /* A string table. */ |
252b5132 | 1977 | if (hdr->bfd_section != NULL) |
bf67003b NC |
1978 | goto success; |
1979 | ||
252b5132 RH |
1980 | if (ehdr->e_shstrndx == shindex) |
1981 | { | |
1982 | elf_tdata (abfd)->shstrtab_hdr = *hdr; | |
1983 | elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->shstrtab_hdr; | |
bf67003b | 1984 | goto success; |
252b5132 | 1985 | } |
bf67003b | 1986 | |
1b3a8575 AM |
1987 | if (elf_elfsections (abfd)[elf_onesymtab (abfd)]->sh_link == shindex) |
1988 | { | |
1989 | symtab_strtab: | |
1990 | elf_tdata (abfd)->strtab_hdr = *hdr; | |
1991 | elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->strtab_hdr; | |
bf67003b | 1992 | goto success; |
1b3a8575 | 1993 | } |
bf67003b | 1994 | |
1b3a8575 AM |
1995 | if (elf_elfsections (abfd)[elf_dynsymtab (abfd)]->sh_link == shindex) |
1996 | { | |
1997 | dynsymtab_strtab: | |
1998 | elf_tdata (abfd)->dynstrtab_hdr = *hdr; | |
1999 | hdr = &elf_tdata (abfd)->dynstrtab_hdr; | |
2000 | elf_elfsections (abfd)[shindex] = hdr; | |
2001 | /* We also treat this as a regular section, so that objcopy | |
2002 | can handle it. */ | |
bf67003b NC |
2003 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, |
2004 | shindex); | |
2005 | goto success; | |
1b3a8575 | 2006 | } |
252b5132 | 2007 | |
1b3a8575 AM |
2008 | /* If the string table isn't one of the above, then treat it as a |
2009 | regular section. We need to scan all the headers to be sure, | |
2010 | just in case this strtab section appeared before the above. */ | |
2011 | if (elf_onesymtab (abfd) == 0 || elf_dynsymtab (abfd) == 0) | |
2012 | { | |
2013 | unsigned int i, num_sec; | |
252b5132 | 2014 | |
1b3a8575 AM |
2015 | num_sec = elf_numsections (abfd); |
2016 | for (i = 1; i < num_sec; i++) | |
2017 | { | |
2018 | Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i]; | |
2019 | if (hdr2->sh_link == shindex) | |
2020 | { | |
933d961a JJ |
2021 | /* Prevent endless recursion on broken objects. */ |
2022 | if (i == shindex) | |
bf67003b | 2023 | goto fail; |
1b3a8575 | 2024 | if (! bfd_section_from_shdr (abfd, i)) |
bf67003b | 2025 | goto fail; |
1b3a8575 AM |
2026 | if (elf_onesymtab (abfd) == i) |
2027 | goto symtab_strtab; | |
2028 | if (elf_dynsymtab (abfd) == i) | |
2029 | goto dynsymtab_strtab; | |
2030 | } | |
2031 | } | |
2032 | } | |
bf67003b NC |
2033 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); |
2034 | goto success; | |
252b5132 RH |
2035 | |
2036 | case SHT_REL: | |
2037 | case SHT_RELA: | |
2038 | /* *These* do a lot of work -- but build no sections! */ | |
2039 | { | |
2040 | asection *target_sect; | |
d4730f92 | 2041 | Elf_Internal_Shdr *hdr2, **p_hdr; |
9ad5cbcf | 2042 | unsigned int num_sec = elf_numsections (abfd); |
d4730f92 BS |
2043 | struct bfd_elf_section_data *esdt; |
2044 | bfd_size_type amt; | |
252b5132 | 2045 | |
aa2ca951 JJ |
2046 | if (hdr->sh_entsize |
2047 | != (bfd_size_type) (hdr->sh_type == SHT_REL | |
a50b2160 | 2048 | ? bed->s->sizeof_rel : bed->s->sizeof_rela)) |
bf67003b | 2049 | goto fail; |
a50b2160 | 2050 | |
03ae5f59 | 2051 | /* Check for a bogus link to avoid crashing. */ |
4fbb74a6 | 2052 | if (hdr->sh_link >= num_sec) |
03ae5f59 ILT |
2053 | { |
2054 | ((*_bfd_error_handler) | |
d003868e AM |
2055 | (_("%B: invalid link %lu for reloc section %s (index %u)"), |
2056 | abfd, hdr->sh_link, name, shindex)); | |
bf67003b NC |
2057 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, |
2058 | shindex); | |
2059 | goto success; | |
03ae5f59 ILT |
2060 | } |
2061 | ||
252b5132 RH |
2062 | /* For some incomprehensible reason Oracle distributes |
2063 | libraries for Solaris in which some of the objects have | |
2064 | bogus sh_link fields. It would be nice if we could just | |
2065 | reject them, but, unfortunately, some people need to use | |
2066 | them. We scan through the section headers; if we find only | |
2067 | one suitable symbol table, we clobber the sh_link to point | |
83b89087 L |
2068 | to it. I hope this doesn't break anything. |
2069 | ||
2070 | Don't do it on executable nor shared library. */ | |
2071 | if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0 | |
2072 | && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_SYMTAB | |
252b5132 RH |
2073 | && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_DYNSYM) |
2074 | { | |
9ad5cbcf | 2075 | unsigned int scan; |
252b5132 RH |
2076 | int found; |
2077 | ||
2078 | found = 0; | |
9ad5cbcf | 2079 | for (scan = 1; scan < num_sec; scan++) |
252b5132 RH |
2080 | { |
2081 | if (elf_elfsections (abfd)[scan]->sh_type == SHT_SYMTAB | |
2082 | || elf_elfsections (abfd)[scan]->sh_type == SHT_DYNSYM) | |
2083 | { | |
2084 | if (found != 0) | |
2085 | { | |
2086 | found = 0; | |
2087 | break; | |
2088 | } | |
2089 | found = scan; | |
2090 | } | |
2091 | } | |
2092 | if (found != 0) | |
2093 | hdr->sh_link = found; | |
2094 | } | |
2095 | ||
2096 | /* Get the symbol table. */ | |
1b3a8575 AM |
2097 | if ((elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_SYMTAB |
2098 | || elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_DYNSYM) | |
252b5132 | 2099 | && ! bfd_section_from_shdr (abfd, hdr->sh_link)) |
bf67003b | 2100 | goto fail; |
252b5132 RH |
2101 | |
2102 | /* If this reloc section does not use the main symbol table we | |
2103 | don't treat it as a reloc section. BFD can't adequately | |
2104 | represent such a section, so at least for now, we don't | |
c044fabd | 2105 | try. We just present it as a normal section. We also |
60bcf0fa | 2106 | can't use it as a reloc section if it points to the null |
83b89087 L |
2107 | section, an invalid section, another reloc section, or its |
2108 | sh_link points to the null section. */ | |
185ef66d | 2109 | if (hdr->sh_link != elf_onesymtab (abfd) |
83b89087 | 2110 | || hdr->sh_link == SHN_UNDEF |
185ef66d | 2111 | || hdr->sh_info == SHN_UNDEF |
185ef66d AM |
2112 | || hdr->sh_info >= num_sec |
2113 | || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_REL | |
2114 | || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_RELA) | |
bf67003b NC |
2115 | { |
2116 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, | |
2117 | shindex); | |
2118 | goto success; | |
2119 | } | |
252b5132 RH |
2120 | |
2121 | if (! bfd_section_from_shdr (abfd, hdr->sh_info)) | |
bf67003b NC |
2122 | goto fail; |
2123 | ||
252b5132 RH |
2124 | target_sect = bfd_section_from_elf_index (abfd, hdr->sh_info); |
2125 | if (target_sect == NULL) | |
bf67003b | 2126 | goto fail; |
252b5132 | 2127 | |
d4730f92 BS |
2128 | esdt = elf_section_data (target_sect); |
2129 | if (hdr->sh_type == SHT_RELA) | |
2130 | p_hdr = &esdt->rela.hdr; | |
252b5132 | 2131 | else |
d4730f92 BS |
2132 | p_hdr = &esdt->rel.hdr; |
2133 | ||
06614111 NC |
2134 | /* PR 17512: file: 0b4f81b7. */ |
2135 | if (*p_hdr != NULL) | |
2136 | goto fail; | |
d4730f92 BS |
2137 | amt = sizeof (*hdr2); |
2138 | hdr2 = (Elf_Internal_Shdr *) bfd_alloc (abfd, amt); | |
2139 | if (hdr2 == NULL) | |
bf67003b | 2140 | goto fail; |
252b5132 | 2141 | *hdr2 = *hdr; |
d4730f92 | 2142 | *p_hdr = hdr2; |
252b5132 | 2143 | elf_elfsections (abfd)[shindex] = hdr2; |
d9bc7a44 | 2144 | target_sect->reloc_count += NUM_SHDR_ENTRIES (hdr); |
252b5132 RH |
2145 | target_sect->flags |= SEC_RELOC; |
2146 | target_sect->relocation = NULL; | |
2147 | target_sect->rel_filepos = hdr->sh_offset; | |
bf572ba0 MM |
2148 | /* In the section to which the relocations apply, mark whether |
2149 | its relocations are of the REL or RELA variety. */ | |
72730e0c | 2150 | if (hdr->sh_size != 0) |
d4730f92 BS |
2151 | { |
2152 | if (hdr->sh_type == SHT_RELA) | |
2153 | target_sect->use_rela_p = 1; | |
2154 | } | |
252b5132 | 2155 | abfd->flags |= HAS_RELOC; |
bf67003b | 2156 | goto success; |
252b5132 | 2157 | } |
252b5132 RH |
2158 | |
2159 | case SHT_GNU_verdef: | |
2160 | elf_dynverdef (abfd) = shindex; | |
2161 | elf_tdata (abfd)->dynverdef_hdr = *hdr; | |
bf67003b NC |
2162 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); |
2163 | goto success; | |
252b5132 RH |
2164 | |
2165 | case SHT_GNU_versym: | |
a50b2160 | 2166 | if (hdr->sh_entsize != sizeof (Elf_External_Versym)) |
bf67003b NC |
2167 | goto fail; |
2168 | ||
252b5132 RH |
2169 | elf_dynversym (abfd) = shindex; |
2170 | elf_tdata (abfd)->dynversym_hdr = *hdr; | |
bf67003b NC |
2171 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); |
2172 | goto success; | |
252b5132 RH |
2173 | |
2174 | case SHT_GNU_verneed: | |
2175 | elf_dynverref (abfd) = shindex; | |
2176 | elf_tdata (abfd)->dynverref_hdr = *hdr; | |
bf67003b NC |
2177 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); |
2178 | goto success; | |
252b5132 RH |
2179 | |
2180 | case SHT_SHLIB: | |
bf67003b | 2181 | goto success; |
252b5132 | 2182 | |
dbb410c3 | 2183 | case SHT_GROUP: |
44534af3 | 2184 | if (! IS_VALID_GROUP_SECTION_HEADER (hdr, GRP_ENTRY_SIZE)) |
bf67003b NC |
2185 | goto fail; |
2186 | ||
6dc132d9 | 2187 | if (!_bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex)) |
bf67003b NC |
2188 | goto fail; |
2189 | ||
dbb410c3 AM |
2190 | if (hdr->contents != NULL) |
2191 | { | |
2192 | Elf_Internal_Group *idx = (Elf_Internal_Group *) hdr->contents; | |
06614111 | 2193 | unsigned int n_elt = hdr->sh_size / sizeof (* idx); |
dbb410c3 AM |
2194 | asection *s; |
2195 | ||
06614111 NC |
2196 | if (n_elt == 0) |
2197 | goto fail; | |
b885599b AM |
2198 | if (idx->flags & GRP_COMDAT) |
2199 | hdr->bfd_section->flags | |
2200 | |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD; | |
2201 | ||
45c5e9ed L |
2202 | /* We try to keep the same section order as it comes in. */ |
2203 | idx += n_elt; | |
06614111 | 2204 | |
dbb410c3 | 2205 | while (--n_elt != 0) |
1783205a NC |
2206 | { |
2207 | --idx; | |
2208 | ||
2209 | if (idx->shdr != NULL | |
2210 | && (s = idx->shdr->bfd_section) != NULL | |
2211 | && elf_next_in_group (s) != NULL) | |
2212 | { | |
2213 | elf_next_in_group (hdr->bfd_section) = s; | |
2214 | break; | |
2215 | } | |
2216 | } | |
dbb410c3 | 2217 | } |
bf67003b | 2218 | goto success; |
dbb410c3 | 2219 | |
252b5132 | 2220 | default: |
104d59d1 JM |
2221 | /* Possibly an attributes section. */ |
2222 | if (hdr->sh_type == SHT_GNU_ATTRIBUTES | |
2223 | || hdr->sh_type == bed->obj_attrs_section_type) | |
2224 | { | |
2225 | if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex)) | |
bf67003b | 2226 | goto fail; |
104d59d1 | 2227 | _bfd_elf_parse_attributes (abfd, hdr); |
bf67003b | 2228 | goto success; |
104d59d1 JM |
2229 | } |
2230 | ||
252b5132 | 2231 | /* Check for any processor-specific section types. */ |
3eb70a79 | 2232 | if (bed->elf_backend_section_from_shdr (abfd, hdr, name, shindex)) |
bf67003b | 2233 | goto success; |
3eb70a79 L |
2234 | |
2235 | if (hdr->sh_type >= SHT_LOUSER && hdr->sh_type <= SHT_HIUSER) | |
2236 | { | |
2237 | if ((hdr->sh_flags & SHF_ALLOC) != 0) | |
2238 | /* FIXME: How to properly handle allocated section reserved | |
2239 | for applications? */ | |
2240 | (*_bfd_error_handler) | |
2241 | (_("%B: don't know how to handle allocated, application " | |
2242 | "specific section `%s' [0x%8x]"), | |
2243 | abfd, name, hdr->sh_type); | |
2244 | else | |
bf67003b NC |
2245 | { |
2246 | /* Allow sections reserved for applications. */ | |
2247 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, | |
2248 | shindex); | |
2249 | goto success; | |
2250 | } | |
3eb70a79 L |
2251 | } |
2252 | else if (hdr->sh_type >= SHT_LOPROC | |
2253 | && hdr->sh_type <= SHT_HIPROC) | |
2254 | /* FIXME: We should handle this section. */ | |
2255 | (*_bfd_error_handler) | |
2256 | (_("%B: don't know how to handle processor specific section " | |
2257 | "`%s' [0x%8x]"), | |
2258 | abfd, name, hdr->sh_type); | |
2259 | else if (hdr->sh_type >= SHT_LOOS && hdr->sh_type <= SHT_HIOS) | |
ff15b240 NC |
2260 | { |
2261 | /* Unrecognised OS-specific sections. */ | |
2262 | if ((hdr->sh_flags & SHF_OS_NONCONFORMING) != 0) | |
2263 | /* SHF_OS_NONCONFORMING indicates that special knowledge is | |
08a40648 | 2264 | required to correctly process the section and the file should |
ff15b240 NC |
2265 | be rejected with an error message. */ |
2266 | (*_bfd_error_handler) | |
2267 | (_("%B: don't know how to handle OS specific section " | |
2268 | "`%s' [0x%8x]"), | |
2269 | abfd, name, hdr->sh_type); | |
2270 | else | |
bf67003b NC |
2271 | { |
2272 | /* Otherwise it should be processed. */ | |
2273 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); | |
2274 | goto success; | |
2275 | } | |
ff15b240 | 2276 | } |
3eb70a79 L |
2277 | else |
2278 | /* FIXME: We should handle this section. */ | |
2279 | (*_bfd_error_handler) | |
2280 | (_("%B: don't know how to handle section `%s' [0x%8x]"), | |
2281 | abfd, name, hdr->sh_type); | |
2282 | ||
bf67003b | 2283 | goto fail; |
252b5132 RH |
2284 | } |
2285 | ||
bf67003b NC |
2286 | fail: |
2287 | ret = FALSE; | |
2288 | success: | |
e5b470e2 | 2289 | if (sections_being_created && sections_being_created_abfd == abfd) |
bf67003b NC |
2290 | sections_being_created [shindex] = FALSE; |
2291 | if (-- nesting == 0) | |
5a4b0ccc NC |
2292 | { |
2293 | sections_being_created = NULL; | |
2294 | sections_being_created_abfd = abfd; | |
2295 | } | |
bf67003b | 2296 | return ret; |
252b5132 RH |
2297 | } |
2298 | ||
87d72d41 | 2299 | /* Return the local symbol specified by ABFD, R_SYMNDX. */ |
ec338859 | 2300 | |
87d72d41 AM |
2301 | Elf_Internal_Sym * |
2302 | bfd_sym_from_r_symndx (struct sym_cache *cache, | |
2303 | bfd *abfd, | |
2304 | unsigned long r_symndx) | |
ec338859 | 2305 | { |
ec338859 AM |
2306 | unsigned int ent = r_symndx % LOCAL_SYM_CACHE_SIZE; |
2307 | ||
a5d1b3b5 AM |
2308 | if (cache->abfd != abfd || cache->indx[ent] != r_symndx) |
2309 | { | |
2310 | Elf_Internal_Shdr *symtab_hdr; | |
2311 | unsigned char esym[sizeof (Elf64_External_Sym)]; | |
2312 | Elf_External_Sym_Shndx eshndx; | |
ec338859 | 2313 | |
a5d1b3b5 AM |
2314 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; |
2315 | if (bfd_elf_get_elf_syms (abfd, symtab_hdr, 1, r_symndx, | |
87d72d41 | 2316 | &cache->sym[ent], esym, &eshndx) == NULL) |
a5d1b3b5 | 2317 | return NULL; |
9ad5cbcf | 2318 | |
a5d1b3b5 AM |
2319 | if (cache->abfd != abfd) |
2320 | { | |
2321 | memset (cache->indx, -1, sizeof (cache->indx)); | |
2322 | cache->abfd = abfd; | |
2323 | } | |
2324 | cache->indx[ent] = r_symndx; | |
ec338859 | 2325 | } |
a5d1b3b5 | 2326 | |
87d72d41 | 2327 | return &cache->sym[ent]; |
ec338859 AM |
2328 | } |
2329 | ||
252b5132 RH |
2330 | /* Given an ELF section number, retrieve the corresponding BFD |
2331 | section. */ | |
2332 | ||
2333 | asection * | |
91d6fa6a | 2334 | bfd_section_from_elf_index (bfd *abfd, unsigned int sec_index) |
252b5132 | 2335 | { |
91d6fa6a | 2336 | if (sec_index >= elf_numsections (abfd)) |
252b5132 | 2337 | return NULL; |
91d6fa6a | 2338 | return elf_elfsections (abfd)[sec_index]->bfd_section; |
252b5132 RH |
2339 | } |
2340 | ||
b35d266b | 2341 | static const struct bfd_elf_special_section special_sections_b[] = |
2f89ff8d | 2342 | { |
0112cd26 NC |
2343 | { STRING_COMMA_LEN (".bss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE }, |
2344 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2345 | }; |
2346 | ||
b35d266b | 2347 | static const struct bfd_elf_special_section special_sections_c[] = |
7f4d3958 | 2348 | { |
0112cd26 NC |
2349 | { STRING_COMMA_LEN (".comment"), 0, SHT_PROGBITS, 0 }, |
2350 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2351 | }; |
2352 | ||
b35d266b | 2353 | static const struct bfd_elf_special_section special_sections_d[] = |
7f4d3958 | 2354 | { |
0112cd26 NC |
2355 | { STRING_COMMA_LEN (".data"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE }, |
2356 | { STRING_COMMA_LEN (".data1"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE }, | |
a9a72a65 DE |
2357 | /* There are more DWARF sections than these, but they needn't be added here |
2358 | unless you have to cope with broken compilers that don't emit section | |
2359 | attributes or you want to help the user writing assembler. */ | |
0112cd26 NC |
2360 | { STRING_COMMA_LEN (".debug"), 0, SHT_PROGBITS, 0 }, |
2361 | { STRING_COMMA_LEN (".debug_line"), 0, SHT_PROGBITS, 0 }, | |
2362 | { STRING_COMMA_LEN (".debug_info"), 0, SHT_PROGBITS, 0 }, | |
2363 | { STRING_COMMA_LEN (".debug_abbrev"), 0, SHT_PROGBITS, 0 }, | |
2364 | { STRING_COMMA_LEN (".debug_aranges"), 0, SHT_PROGBITS, 0 }, | |
2365 | { STRING_COMMA_LEN (".dynamic"), 0, SHT_DYNAMIC, SHF_ALLOC }, | |
2366 | { STRING_COMMA_LEN (".dynstr"), 0, SHT_STRTAB, SHF_ALLOC }, | |
2367 | { STRING_COMMA_LEN (".dynsym"), 0, SHT_DYNSYM, SHF_ALLOC }, | |
2368 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2369 | }; |
2370 | ||
b35d266b | 2371 | static const struct bfd_elf_special_section special_sections_f[] = |
7f4d3958 | 2372 | { |
0112cd26 NC |
2373 | { STRING_COMMA_LEN (".fini"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR }, |
2374 | { STRING_COMMA_LEN (".fini_array"), 0, SHT_FINI_ARRAY, SHF_ALLOC + SHF_WRITE }, | |
2375 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2376 | }; |
2377 | ||
b35d266b | 2378 | static const struct bfd_elf_special_section special_sections_g[] = |
7f4d3958 | 2379 | { |
0112cd26 | 2380 | { STRING_COMMA_LEN (".gnu.linkonce.b"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE }, |
68efed41 | 2381 | { STRING_COMMA_LEN (".gnu.lto_"), -1, SHT_PROGBITS, SHF_EXCLUDE }, |
0112cd26 NC |
2382 | { STRING_COMMA_LEN (".got"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE }, |
2383 | { STRING_COMMA_LEN (".gnu.version"), 0, SHT_GNU_versym, 0 }, | |
2384 | { STRING_COMMA_LEN (".gnu.version_d"), 0, SHT_GNU_verdef, 0 }, | |
2385 | { STRING_COMMA_LEN (".gnu.version_r"), 0, SHT_GNU_verneed, 0 }, | |
2386 | { STRING_COMMA_LEN (".gnu.liblist"), 0, SHT_GNU_LIBLIST, SHF_ALLOC }, | |
2387 | { STRING_COMMA_LEN (".gnu.conflict"), 0, SHT_RELA, SHF_ALLOC }, | |
2388 | { STRING_COMMA_LEN (".gnu.hash"), 0, SHT_GNU_HASH, SHF_ALLOC }, | |
2389 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2390 | }; |
2391 | ||
b35d266b | 2392 | static const struct bfd_elf_special_section special_sections_h[] = |
7f4d3958 | 2393 | { |
0112cd26 NC |
2394 | { STRING_COMMA_LEN (".hash"), 0, SHT_HASH, SHF_ALLOC }, |
2395 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2396 | }; |
2397 | ||
b35d266b | 2398 | static const struct bfd_elf_special_section special_sections_i[] = |
7f4d3958 | 2399 | { |
0112cd26 NC |
2400 | { STRING_COMMA_LEN (".init"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR }, |
2401 | { STRING_COMMA_LEN (".init_array"), 0, SHT_INIT_ARRAY, SHF_ALLOC + SHF_WRITE }, | |
2402 | { STRING_COMMA_LEN (".interp"), 0, SHT_PROGBITS, 0 }, | |
2403 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2404 | }; |
2405 | ||
b35d266b | 2406 | static const struct bfd_elf_special_section special_sections_l[] = |
7f4d3958 | 2407 | { |
0112cd26 NC |
2408 | { STRING_COMMA_LEN (".line"), 0, SHT_PROGBITS, 0 }, |
2409 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2410 | }; |
2411 | ||
b35d266b | 2412 | static const struct bfd_elf_special_section special_sections_n[] = |
7f4d3958 | 2413 | { |
0112cd26 NC |
2414 | { STRING_COMMA_LEN (".note.GNU-stack"), 0, SHT_PROGBITS, 0 }, |
2415 | { STRING_COMMA_LEN (".note"), -1, SHT_NOTE, 0 }, | |
2416 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2417 | }; |
2418 | ||
b35d266b | 2419 | static const struct bfd_elf_special_section special_sections_p[] = |
7f4d3958 | 2420 | { |
0112cd26 NC |
2421 | { STRING_COMMA_LEN (".preinit_array"), 0, SHT_PREINIT_ARRAY, SHF_ALLOC + SHF_WRITE }, |
2422 | { STRING_COMMA_LEN (".plt"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR }, | |
2423 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2424 | }; |
2425 | ||
b35d266b | 2426 | static const struct bfd_elf_special_section special_sections_r[] = |
7f4d3958 | 2427 | { |
0112cd26 NC |
2428 | { STRING_COMMA_LEN (".rodata"), -2, SHT_PROGBITS, SHF_ALLOC }, |
2429 | { STRING_COMMA_LEN (".rodata1"), 0, SHT_PROGBITS, SHF_ALLOC }, | |
2430 | { STRING_COMMA_LEN (".rela"), -1, SHT_RELA, 0 }, | |
2431 | { STRING_COMMA_LEN (".rel"), -1, SHT_REL, 0 }, | |
2432 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2433 | }; |
2434 | ||
b35d266b | 2435 | static const struct bfd_elf_special_section special_sections_s[] = |
7f4d3958 | 2436 | { |
0112cd26 NC |
2437 | { STRING_COMMA_LEN (".shstrtab"), 0, SHT_STRTAB, 0 }, |
2438 | { STRING_COMMA_LEN (".strtab"), 0, SHT_STRTAB, 0 }, | |
2439 | { STRING_COMMA_LEN (".symtab"), 0, SHT_SYMTAB, 0 }, | |
60ff4dc4 HPN |
2440 | /* See struct bfd_elf_special_section declaration for the semantics of |
2441 | this special case where .prefix_length != strlen (.prefix). */ | |
2442 | { ".stabstr", 5, 3, SHT_STRTAB, 0 }, | |
0112cd26 | 2443 | { NULL, 0, 0, 0, 0 } |
2f89ff8d L |
2444 | }; |
2445 | ||
b35d266b | 2446 | static const struct bfd_elf_special_section special_sections_t[] = |
7f4d3958 | 2447 | { |
0112cd26 NC |
2448 | { STRING_COMMA_LEN (".text"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR }, |
2449 | { STRING_COMMA_LEN (".tbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS }, | |
2450 | { STRING_COMMA_LEN (".tdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS }, | |
2451 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2452 | }; |
2453 | ||
1b315056 CS |
2454 | static const struct bfd_elf_special_section special_sections_z[] = |
2455 | { | |
2456 | { STRING_COMMA_LEN (".zdebug_line"), 0, SHT_PROGBITS, 0 }, | |
2457 | { STRING_COMMA_LEN (".zdebug_info"), 0, SHT_PROGBITS, 0 }, | |
2458 | { STRING_COMMA_LEN (".zdebug_abbrev"), 0, SHT_PROGBITS, 0 }, | |
2459 | { STRING_COMMA_LEN (".zdebug_aranges"), 0, SHT_PROGBITS, 0 }, | |
2460 | { NULL, 0, 0, 0, 0 } | |
2461 | }; | |
2462 | ||
e4c93b56 | 2463 | static const struct bfd_elf_special_section * const special_sections[] = |
7f4d3958 | 2464 | { |
7f4d3958 | 2465 | special_sections_b, /* 'b' */ |
98ece1b3 | 2466 | special_sections_c, /* 'c' */ |
7f4d3958 L |
2467 | special_sections_d, /* 'd' */ |
2468 | NULL, /* 'e' */ | |
2469 | special_sections_f, /* 'f' */ | |
2470 | special_sections_g, /* 'g' */ | |
2471 | special_sections_h, /* 'h' */ | |
2472 | special_sections_i, /* 'i' */ | |
2473 | NULL, /* 'j' */ | |
2474 | NULL, /* 'k' */ | |
2475 | special_sections_l, /* 'l' */ | |
2476 | NULL, /* 'm' */ | |
2477 | special_sections_n, /* 'n' */ | |
2478 | NULL, /* 'o' */ | |
2479 | special_sections_p, /* 'p' */ | |
2480 | NULL, /* 'q' */ | |
2481 | special_sections_r, /* 'r' */ | |
2482 | special_sections_s, /* 's' */ | |
2483 | special_sections_t, /* 't' */ | |
1b315056 CS |
2484 | NULL, /* 'u' */ |
2485 | NULL, /* 'v' */ | |
2486 | NULL, /* 'w' */ | |
2487 | NULL, /* 'x' */ | |
2488 | NULL, /* 'y' */ | |
2489 | special_sections_z /* 'z' */ | |
7f4d3958 L |
2490 | }; |
2491 | ||
551b43fd AM |
2492 | const struct bfd_elf_special_section * |
2493 | _bfd_elf_get_special_section (const char *name, | |
2494 | const struct bfd_elf_special_section *spec, | |
2495 | unsigned int rela) | |
2f89ff8d L |
2496 | { |
2497 | int i; | |
7f4d3958 | 2498 | int len; |
7f4d3958 | 2499 | |
551b43fd | 2500 | len = strlen (name); |
7f4d3958 | 2501 | |
551b43fd | 2502 | for (i = 0; spec[i].prefix != NULL; i++) |
7dcb9820 AM |
2503 | { |
2504 | int suffix_len; | |
551b43fd | 2505 | int prefix_len = spec[i].prefix_length; |
7dcb9820 AM |
2506 | |
2507 | if (len < prefix_len) | |
2508 | continue; | |
551b43fd | 2509 | if (memcmp (name, spec[i].prefix, prefix_len) != 0) |
7dcb9820 AM |
2510 | continue; |
2511 | ||
551b43fd | 2512 | suffix_len = spec[i].suffix_length; |
7dcb9820 AM |
2513 | if (suffix_len <= 0) |
2514 | { | |
2515 | if (name[prefix_len] != 0) | |
2516 | { | |
2517 | if (suffix_len == 0) | |
2518 | continue; | |
2519 | if (name[prefix_len] != '.' | |
2520 | && (suffix_len == -2 | |
551b43fd | 2521 | || (rela && spec[i].type == SHT_REL))) |
7dcb9820 AM |
2522 | continue; |
2523 | } | |
2524 | } | |
2525 | else | |
2526 | { | |
2527 | if (len < prefix_len + suffix_len) | |
2528 | continue; | |
2529 | if (memcmp (name + len - suffix_len, | |
551b43fd | 2530 | spec[i].prefix + prefix_len, |
7dcb9820 AM |
2531 | suffix_len) != 0) |
2532 | continue; | |
2533 | } | |
551b43fd | 2534 | return &spec[i]; |
7dcb9820 | 2535 | } |
2f89ff8d L |
2536 | |
2537 | return NULL; | |
2538 | } | |
2539 | ||
7dcb9820 | 2540 | const struct bfd_elf_special_section * |
29ef7005 | 2541 | _bfd_elf_get_sec_type_attr (bfd *abfd, asection *sec) |
2f89ff8d | 2542 | { |
551b43fd AM |
2543 | int i; |
2544 | const struct bfd_elf_special_section *spec; | |
29ef7005 | 2545 | const struct elf_backend_data *bed; |
2f89ff8d L |
2546 | |
2547 | /* See if this is one of the special sections. */ | |
551b43fd AM |
2548 | if (sec->name == NULL) |
2549 | return NULL; | |
2f89ff8d | 2550 | |
29ef7005 L |
2551 | bed = get_elf_backend_data (abfd); |
2552 | spec = bed->special_sections; | |
2553 | if (spec) | |
2554 | { | |
2555 | spec = _bfd_elf_get_special_section (sec->name, | |
2556 | bed->special_sections, | |
2557 | sec->use_rela_p); | |
2558 | if (spec != NULL) | |
2559 | return spec; | |
2560 | } | |
2561 | ||
551b43fd AM |
2562 | if (sec->name[0] != '.') |
2563 | return NULL; | |
2f89ff8d | 2564 | |
551b43fd | 2565 | i = sec->name[1] - 'b'; |
1b315056 | 2566 | if (i < 0 || i > 'z' - 'b') |
551b43fd AM |
2567 | return NULL; |
2568 | ||
2569 | spec = special_sections[i]; | |
2f89ff8d | 2570 | |
551b43fd AM |
2571 | if (spec == NULL) |
2572 | return NULL; | |
2573 | ||
2574 | return _bfd_elf_get_special_section (sec->name, spec, sec->use_rela_p); | |
2f89ff8d L |
2575 | } |
2576 | ||
b34976b6 | 2577 | bfd_boolean |
217aa764 | 2578 | _bfd_elf_new_section_hook (bfd *abfd, asection *sec) |
252b5132 RH |
2579 | { |
2580 | struct bfd_elf_section_data *sdata; | |
551b43fd | 2581 | const struct elf_backend_data *bed; |
7dcb9820 | 2582 | const struct bfd_elf_special_section *ssect; |
252b5132 | 2583 | |
f0abc2a1 AM |
2584 | sdata = (struct bfd_elf_section_data *) sec->used_by_bfd; |
2585 | if (sdata == NULL) | |
2586 | { | |
a50b1753 NC |
2587 | sdata = (struct bfd_elf_section_data *) bfd_zalloc (abfd, |
2588 | sizeof (*sdata)); | |
f0abc2a1 AM |
2589 | if (sdata == NULL) |
2590 | return FALSE; | |
217aa764 | 2591 | sec->used_by_bfd = sdata; |
f0abc2a1 | 2592 | } |
bf572ba0 | 2593 | |
551b43fd AM |
2594 | /* Indicate whether or not this section should use RELA relocations. */ |
2595 | bed = get_elf_backend_data (abfd); | |
2596 | sec->use_rela_p = bed->default_use_rela_p; | |
2597 | ||
e843e0f8 L |
2598 | /* When we read a file, we don't need to set ELF section type and |
2599 | flags. They will be overridden in _bfd_elf_make_section_from_shdr | |
2600 | anyway. We will set ELF section type and flags for all linker | |
2601 | created sections. If user specifies BFD section flags, we will | |
2602 | set ELF section type and flags based on BFD section flags in | |
02ecc8e9 L |
2603 | elf_fake_sections. Special handling for .init_array/.fini_array |
2604 | output sections since they may contain .ctors/.dtors input | |
2605 | sections. We don't want _bfd_elf_init_private_section_data to | |
2606 | copy ELF section type from .ctors/.dtors input sections. */ | |
2607 | if (abfd->direction != read_direction | |
3496cb2a | 2608 | || (sec->flags & SEC_LINKER_CREATED) != 0) |
2f89ff8d | 2609 | { |
551b43fd | 2610 | ssect = (*bed->get_sec_type_attr) (abfd, sec); |
02ecc8e9 L |
2611 | if (ssect != NULL |
2612 | && (!sec->flags | |
2613 | || (sec->flags & SEC_LINKER_CREATED) != 0 | |
2614 | || ssect->type == SHT_INIT_ARRAY | |
2615 | || ssect->type == SHT_FINI_ARRAY)) | |
a31501e9 L |
2616 | { |
2617 | elf_section_type (sec) = ssect->type; | |
2618 | elf_section_flags (sec) = ssect->attr; | |
2619 | } | |
2f89ff8d L |
2620 | } |
2621 | ||
f592407e | 2622 | return _bfd_generic_new_section_hook (abfd, sec); |
252b5132 RH |
2623 | } |
2624 | ||
2625 | /* Create a new bfd section from an ELF program header. | |
2626 | ||
2627 | Since program segments have no names, we generate a synthetic name | |
2628 | of the form segment<NUM>, where NUM is generally the index in the | |
2629 | program header table. For segments that are split (see below) we | |
2630 | generate the names segment<NUM>a and segment<NUM>b. | |
2631 | ||
2632 | Note that some program segments may have a file size that is different than | |
2633 | (less than) the memory size. All this means is that at execution the | |
2634 | system must allocate the amount of memory specified by the memory size, | |
2635 | but only initialize it with the first "file size" bytes read from the | |
2636 | file. This would occur for example, with program segments consisting | |
2637 | of combined data+bss. | |
2638 | ||
2639 | To handle the above situation, this routine generates TWO bfd sections | |
2640 | for the single program segment. The first has the length specified by | |
2641 | the file size of the segment, and the second has the length specified | |
2642 | by the difference between the two sizes. In effect, the segment is split | |
d5191d0c | 2643 | into its initialized and uninitialized parts. |
252b5132 RH |
2644 | |
2645 | */ | |
2646 | ||
b34976b6 | 2647 | bfd_boolean |
217aa764 AM |
2648 | _bfd_elf_make_section_from_phdr (bfd *abfd, |
2649 | Elf_Internal_Phdr *hdr, | |
91d6fa6a | 2650 | int hdr_index, |
a50b1753 | 2651 | const char *type_name) |
252b5132 RH |
2652 | { |
2653 | asection *newsect; | |
2654 | char *name; | |
2655 | char namebuf[64]; | |
d4c88bbb | 2656 | size_t len; |
252b5132 RH |
2657 | int split; |
2658 | ||
2659 | split = ((hdr->p_memsz > 0) | |
2660 | && (hdr->p_filesz > 0) | |
2661 | && (hdr->p_memsz > hdr->p_filesz)); | |
d5191d0c AM |
2662 | |
2663 | if (hdr->p_filesz > 0) | |
252b5132 | 2664 | { |
91d6fa6a | 2665 | sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "a" : ""); |
d5191d0c | 2666 | len = strlen (namebuf) + 1; |
a50b1753 | 2667 | name = (char *) bfd_alloc (abfd, len); |
d5191d0c AM |
2668 | if (!name) |
2669 | return FALSE; | |
2670 | memcpy (name, namebuf, len); | |
2671 | newsect = bfd_make_section (abfd, name); | |
2672 | if (newsect == NULL) | |
2673 | return FALSE; | |
2674 | newsect->vma = hdr->p_vaddr; | |
2675 | newsect->lma = hdr->p_paddr; | |
2676 | newsect->size = hdr->p_filesz; | |
2677 | newsect->filepos = hdr->p_offset; | |
2678 | newsect->flags |= SEC_HAS_CONTENTS; | |
2679 | newsect->alignment_power = bfd_log2 (hdr->p_align); | |
2680 | if (hdr->p_type == PT_LOAD) | |
252b5132 | 2681 | { |
d5191d0c AM |
2682 | newsect->flags |= SEC_ALLOC; |
2683 | newsect->flags |= SEC_LOAD; | |
2684 | if (hdr->p_flags & PF_X) | |
2685 | { | |
2686 | /* FIXME: all we known is that it has execute PERMISSION, | |
2687 | may be data. */ | |
2688 | newsect->flags |= SEC_CODE; | |
2689 | } | |
2690 | } | |
2691 | if (!(hdr->p_flags & PF_W)) | |
2692 | { | |
2693 | newsect->flags |= SEC_READONLY; | |
252b5132 | 2694 | } |
252b5132 RH |
2695 | } |
2696 | ||
d5191d0c | 2697 | if (hdr->p_memsz > hdr->p_filesz) |
252b5132 | 2698 | { |
d5191d0c AM |
2699 | bfd_vma align; |
2700 | ||
91d6fa6a | 2701 | sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "b" : ""); |
d4c88bbb | 2702 | len = strlen (namebuf) + 1; |
a50b1753 | 2703 | name = (char *) bfd_alloc (abfd, len); |
252b5132 | 2704 | if (!name) |
b34976b6 | 2705 | return FALSE; |
d4c88bbb | 2706 | memcpy (name, namebuf, len); |
252b5132 RH |
2707 | newsect = bfd_make_section (abfd, name); |
2708 | if (newsect == NULL) | |
b34976b6 | 2709 | return FALSE; |
252b5132 RH |
2710 | newsect->vma = hdr->p_vaddr + hdr->p_filesz; |
2711 | newsect->lma = hdr->p_paddr + hdr->p_filesz; | |
eea6121a | 2712 | newsect->size = hdr->p_memsz - hdr->p_filesz; |
d5191d0c AM |
2713 | newsect->filepos = hdr->p_offset + hdr->p_filesz; |
2714 | align = newsect->vma & -newsect->vma; | |
2715 | if (align == 0 || align > hdr->p_align) | |
2716 | align = hdr->p_align; | |
2717 | newsect->alignment_power = bfd_log2 (align); | |
252b5132 RH |
2718 | if (hdr->p_type == PT_LOAD) |
2719 | { | |
d5191d0c AM |
2720 | /* Hack for gdb. Segments that have not been modified do |
2721 | not have their contents written to a core file, on the | |
2722 | assumption that a debugger can find the contents in the | |
2723 | executable. We flag this case by setting the fake | |
2724 | section size to zero. Note that "real" bss sections will | |
2725 | always have their contents dumped to the core file. */ | |
2726 | if (bfd_get_format (abfd) == bfd_core) | |
2727 | newsect->size = 0; | |
252b5132 RH |
2728 | newsect->flags |= SEC_ALLOC; |
2729 | if (hdr->p_flags & PF_X) | |
2730 | newsect->flags |= SEC_CODE; | |
2731 | } | |
2732 | if (!(hdr->p_flags & PF_W)) | |
2733 | newsect->flags |= SEC_READONLY; | |
2734 | } | |
2735 | ||
b34976b6 | 2736 | return TRUE; |
252b5132 RH |
2737 | } |
2738 | ||
b34976b6 | 2739 | bfd_boolean |
91d6fa6a | 2740 | bfd_section_from_phdr (bfd *abfd, Elf_Internal_Phdr *hdr, int hdr_index) |
20cfcaae | 2741 | { |
9c5bfbb7 | 2742 | const struct elf_backend_data *bed; |
20cfcaae NC |
2743 | |
2744 | switch (hdr->p_type) | |
2745 | { | |
2746 | case PT_NULL: | |
91d6fa6a | 2747 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "null"); |
20cfcaae NC |
2748 | |
2749 | case PT_LOAD: | |
91d6fa6a | 2750 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "load"); |
20cfcaae NC |
2751 | |
2752 | case PT_DYNAMIC: | |
91d6fa6a | 2753 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "dynamic"); |
20cfcaae NC |
2754 | |
2755 | case PT_INTERP: | |
91d6fa6a | 2756 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "interp"); |
20cfcaae NC |
2757 | |
2758 | case PT_NOTE: | |
91d6fa6a | 2759 | if (! _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "note")) |
b34976b6 | 2760 | return FALSE; |
718175fa | 2761 | if (! elf_read_notes (abfd, hdr->p_offset, hdr->p_filesz)) |
b34976b6 AM |
2762 | return FALSE; |
2763 | return TRUE; | |
20cfcaae NC |
2764 | |
2765 | case PT_SHLIB: | |
91d6fa6a | 2766 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "shlib"); |
20cfcaae NC |
2767 | |
2768 | case PT_PHDR: | |
91d6fa6a | 2769 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "phdr"); |
20cfcaae | 2770 | |
811072d8 | 2771 | case PT_GNU_EH_FRAME: |
91d6fa6a | 2772 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, |
811072d8 RM |
2773 | "eh_frame_hdr"); |
2774 | ||
2b05f1b7 | 2775 | case PT_GNU_STACK: |
91d6fa6a | 2776 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "stack"); |
9ee5e499 | 2777 | |
8c37241b | 2778 | case PT_GNU_RELRO: |
91d6fa6a | 2779 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "relro"); |
8c37241b | 2780 | |
20cfcaae | 2781 | default: |
8c1acd09 | 2782 | /* Check for any processor-specific program segment types. */ |
20cfcaae | 2783 | bed = get_elf_backend_data (abfd); |
91d6fa6a | 2784 | return bed->elf_backend_section_from_phdr (abfd, hdr, hdr_index, "proc"); |
20cfcaae NC |
2785 | } |
2786 | } | |
2787 | ||
d4730f92 BS |
2788 | /* Return the REL_HDR for SEC, assuming there is only a single one, either |
2789 | REL or RELA. */ | |
2790 | ||
2791 | Elf_Internal_Shdr * | |
2792 | _bfd_elf_single_rel_hdr (asection *sec) | |
2793 | { | |
2794 | if (elf_section_data (sec)->rel.hdr) | |
2795 | { | |
2796 | BFD_ASSERT (elf_section_data (sec)->rela.hdr == NULL); | |
2797 | return elf_section_data (sec)->rel.hdr; | |
2798 | } | |
2799 | else | |
2800 | return elf_section_data (sec)->rela.hdr; | |
2801 | } | |
2802 | ||
3e19fb8f L |
2803 | static bfd_boolean |
2804 | _bfd_elf_set_reloc_sh_name (bfd *abfd, | |
2805 | Elf_Internal_Shdr *rel_hdr, | |
2806 | const char *sec_name, | |
2807 | bfd_boolean use_rela_p) | |
2808 | { | |
2809 | char *name = (char *) bfd_alloc (abfd, | |
2810 | sizeof ".rela" + strlen (sec_name)); | |
2811 | if (name == NULL) | |
2812 | return FALSE; | |
2813 | ||
2814 | sprintf (name, "%s%s", use_rela_p ? ".rela" : ".rel", sec_name); | |
2815 | rel_hdr->sh_name = | |
2816 | (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), name, | |
2817 | FALSE); | |
2818 | if (rel_hdr->sh_name == (unsigned int) -1) | |
2819 | return FALSE; | |
2820 | ||
2821 | return TRUE; | |
2822 | } | |
2823 | ||
d4730f92 BS |
2824 | /* Allocate and initialize a section-header for a new reloc section, |
2825 | containing relocations against ASECT. It is stored in RELDATA. If | |
2826 | USE_RELA_P is TRUE, we use RELA relocations; otherwise, we use REL | |
2827 | relocations. */ | |
23bc299b | 2828 | |
5d13b3b3 | 2829 | static bfd_boolean |
217aa764 | 2830 | _bfd_elf_init_reloc_shdr (bfd *abfd, |
d4730f92 | 2831 | struct bfd_elf_section_reloc_data *reldata, |
f6fe1ccd | 2832 | const char *sec_name, |
3e19fb8f L |
2833 | bfd_boolean use_rela_p, |
2834 | bfd_boolean delay_st_name_p) | |
23bc299b | 2835 | { |
d4730f92 | 2836 | Elf_Internal_Shdr *rel_hdr; |
9c5bfbb7 | 2837 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
d4730f92 BS |
2838 | bfd_size_type amt; |
2839 | ||
2840 | amt = sizeof (Elf_Internal_Shdr); | |
2841 | BFD_ASSERT (reldata->hdr == NULL); | |
2842 | rel_hdr = bfd_zalloc (abfd, amt); | |
2843 | reldata->hdr = rel_hdr; | |
23bc299b | 2844 | |
3e19fb8f L |
2845 | if (delay_st_name_p) |
2846 | rel_hdr->sh_name = (unsigned int) -1; | |
2847 | else if (!_bfd_elf_set_reloc_sh_name (abfd, rel_hdr, sec_name, | |
2848 | use_rela_p)) | |
b34976b6 | 2849 | return FALSE; |
23bc299b MM |
2850 | rel_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL; |
2851 | rel_hdr->sh_entsize = (use_rela_p | |
2852 | ? bed->s->sizeof_rela | |
2853 | : bed->s->sizeof_rel); | |
72de5009 | 2854 | rel_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align; |
23bc299b MM |
2855 | rel_hdr->sh_flags = 0; |
2856 | rel_hdr->sh_addr = 0; | |
2857 | rel_hdr->sh_size = 0; | |
2858 | rel_hdr->sh_offset = 0; | |
2859 | ||
b34976b6 | 2860 | return TRUE; |
23bc299b MM |
2861 | } |
2862 | ||
94be91de JB |
2863 | /* Return the default section type based on the passed in section flags. */ |
2864 | ||
2865 | int | |
2866 | bfd_elf_get_default_section_type (flagword flags) | |
2867 | { | |
2868 | if ((flags & SEC_ALLOC) != 0 | |
2e76e85a | 2869 | && (flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0) |
94be91de JB |
2870 | return SHT_NOBITS; |
2871 | return SHT_PROGBITS; | |
2872 | } | |
2873 | ||
d4730f92 BS |
2874 | struct fake_section_arg |
2875 | { | |
2876 | struct bfd_link_info *link_info; | |
2877 | bfd_boolean failed; | |
2878 | }; | |
2879 | ||
252b5132 RH |
2880 | /* Set up an ELF internal section header for a section. */ |
2881 | ||
252b5132 | 2882 | static void |
d4730f92 | 2883 | elf_fake_sections (bfd *abfd, asection *asect, void *fsarg) |
252b5132 | 2884 | { |
d4730f92 | 2885 | struct fake_section_arg *arg = (struct fake_section_arg *)fsarg; |
9c5bfbb7 | 2886 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
d4730f92 | 2887 | struct bfd_elf_section_data *esd = elf_section_data (asect); |
252b5132 | 2888 | Elf_Internal_Shdr *this_hdr; |
0414f35b | 2889 | unsigned int sh_type; |
0ce398f1 | 2890 | const char *name = asect->name; |
3e19fb8f | 2891 | bfd_boolean delay_st_name_p = FALSE; |
252b5132 | 2892 | |
d4730f92 | 2893 | if (arg->failed) |
252b5132 RH |
2894 | { |
2895 | /* We already failed; just get out of the bfd_map_over_sections | |
08a40648 | 2896 | loop. */ |
252b5132 RH |
2897 | return; |
2898 | } | |
2899 | ||
d4730f92 | 2900 | this_hdr = &esd->this_hdr; |
252b5132 | 2901 | |
f6fe1ccd | 2902 | if (arg->link_info) |
0ce398f1 | 2903 | { |
f6fe1ccd L |
2904 | /* ld: compress DWARF debug sections with names: .debug_*. */ |
2905 | if ((arg->link_info->compress_debug & COMPRESS_DEBUG) | |
2906 | && (asect->flags & SEC_DEBUGGING) | |
2907 | && name[1] == 'd' | |
2908 | && name[6] == '_') | |
2909 | { | |
2910 | /* Set SEC_ELF_COMPRESS to indicate this section should be | |
2911 | compressed. */ | |
2912 | asect->flags |= SEC_ELF_COMPRESS; | |
0ce398f1 | 2913 | |
3e19fb8f L |
2914 | /* If this section will be compressed, delay adding setion |
2915 | name to section name section after it is compressed in | |
2916 | _bfd_elf_assign_file_positions_for_non_load. */ | |
2917 | delay_st_name_p = TRUE; | |
f6fe1ccd L |
2918 | } |
2919 | } | |
2920 | else if ((asect->flags & SEC_ELF_RENAME)) | |
2921 | { | |
2922 | /* objcopy: rename output DWARF debug section. */ | |
2923 | if ((abfd->flags & (BFD_DECOMPRESS | BFD_COMPRESS_GABI))) | |
2924 | { | |
2925 | /* When we decompress or compress with SHF_COMPRESSED, | |
2926 | convert section name from .zdebug_* to .debug_* if | |
2927 | needed. */ | |
2928 | if (name[1] == 'z') | |
2929 | { | |
2930 | char *new_name = convert_zdebug_to_debug (abfd, name); | |
2931 | if (new_name == NULL) | |
2932 | { | |
2933 | arg->failed = TRUE; | |
2934 | return; | |
2935 | } | |
2936 | name = new_name; | |
2937 | } | |
2938 | } | |
2939 | else if (asect->compress_status == COMPRESS_SECTION_DONE) | |
0ce398f1 | 2940 | { |
f6fe1ccd L |
2941 | /* PR binutils/18087: Compression does not always make a |
2942 | section smaller. So only rename the section when | |
2943 | compression has actually taken place. If input section | |
2944 | name is .zdebug_*, we should never compress it again. */ | |
2945 | char *new_name = convert_debug_to_zdebug (abfd, name); | |
0ce398f1 L |
2946 | if (new_name == NULL) |
2947 | { | |
2948 | arg->failed = TRUE; | |
2949 | return; | |
2950 | } | |
f6fe1ccd L |
2951 | BFD_ASSERT (name[1] != 'z'); |
2952 | name = new_name; | |
0ce398f1 L |
2953 | } |
2954 | } | |
2955 | ||
3e19fb8f L |
2956 | if (delay_st_name_p) |
2957 | this_hdr->sh_name = (unsigned int) -1; | |
2958 | else | |
252b5132 | 2959 | { |
3e19fb8f L |
2960 | this_hdr->sh_name |
2961 | = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), | |
2962 | name, FALSE); | |
2963 | if (this_hdr->sh_name == (unsigned int) -1) | |
2964 | { | |
2965 | arg->failed = TRUE; | |
2966 | return; | |
2967 | } | |
252b5132 RH |
2968 | } |
2969 | ||
a4d8e49b | 2970 | /* Don't clear sh_flags. Assembler may set additional bits. */ |
252b5132 RH |
2971 | |
2972 | if ((asect->flags & SEC_ALLOC) != 0 | |
2973 | || asect->user_set_vma) | |
2974 | this_hdr->sh_addr = asect->vma; | |
2975 | else | |
2976 | this_hdr->sh_addr = 0; | |
2977 | ||
2978 | this_hdr->sh_offset = 0; | |
eea6121a | 2979 | this_hdr->sh_size = asect->size; |
252b5132 | 2980 | this_hdr->sh_link = 0; |
c86934ce NC |
2981 | /* PR 17512: file: 0eb809fe, 8b0535ee. */ |
2982 | if (asect->alignment_power >= (sizeof (bfd_vma) * 8) - 1) | |
2983 | { | |
2984 | (*_bfd_error_handler) | |
2985 | (_("%B: error: Alignment power %d of section `%A' is too big"), | |
2986 | abfd, asect, asect->alignment_power); | |
2987 | arg->failed = TRUE; | |
2988 | return; | |
2989 | } | |
72de5009 | 2990 | this_hdr->sh_addralign = (bfd_vma) 1 << asect->alignment_power; |
252b5132 RH |
2991 | /* The sh_entsize and sh_info fields may have been set already by |
2992 | copy_private_section_data. */ | |
2993 | ||
2994 | this_hdr->bfd_section = asect; | |
2995 | this_hdr->contents = NULL; | |
2996 | ||
3cddba1e L |
2997 | /* If the section type is unspecified, we set it based on |
2998 | asect->flags. */ | |
98ece1b3 AM |
2999 | if ((asect->flags & SEC_GROUP) != 0) |
3000 | sh_type = SHT_GROUP; | |
98ece1b3 | 3001 | else |
94be91de | 3002 | sh_type = bfd_elf_get_default_section_type (asect->flags); |
98ece1b3 | 3003 | |
3cddba1e | 3004 | if (this_hdr->sh_type == SHT_NULL) |
98ece1b3 AM |
3005 | this_hdr->sh_type = sh_type; |
3006 | else if (this_hdr->sh_type == SHT_NOBITS | |
3007 | && sh_type == SHT_PROGBITS | |
3008 | && (asect->flags & SEC_ALLOC) != 0) | |
3cddba1e | 3009 | { |
98ece1b3 AM |
3010 | /* Warn if we are changing a NOBITS section to PROGBITS, but |
3011 | allow the link to proceed. This can happen when users link | |
3012 | non-bss input sections to bss output sections, or emit data | |
3013 | to a bss output section via a linker script. */ | |
3014 | (*_bfd_error_handler) | |
58f0869b | 3015 | (_("warning: section `%A' type changed to PROGBITS"), asect); |
98ece1b3 | 3016 | this_hdr->sh_type = sh_type; |
3cddba1e L |
3017 | } |
3018 | ||
2f89ff8d | 3019 | switch (this_hdr->sh_type) |
252b5132 | 3020 | { |
2f89ff8d | 3021 | default: |
2f89ff8d L |
3022 | break; |
3023 | ||
3024 | case SHT_STRTAB: | |
3025 | case SHT_INIT_ARRAY: | |
3026 | case SHT_FINI_ARRAY: | |
3027 | case SHT_PREINIT_ARRAY: | |
3028 | case SHT_NOTE: | |
3029 | case SHT_NOBITS: | |
3030 | case SHT_PROGBITS: | |
3031 | break; | |
3032 | ||
3033 | case SHT_HASH: | |
c7ac6ff8 | 3034 | this_hdr->sh_entsize = bed->s->sizeof_hash_entry; |
2f89ff8d | 3035 | break; |
5de3bf90 | 3036 | |
2f89ff8d | 3037 | case SHT_DYNSYM: |
252b5132 | 3038 | this_hdr->sh_entsize = bed->s->sizeof_sym; |
2f89ff8d L |
3039 | break; |
3040 | ||
3041 | case SHT_DYNAMIC: | |
252b5132 | 3042 | this_hdr->sh_entsize = bed->s->sizeof_dyn; |
2f89ff8d L |
3043 | break; |
3044 | ||
3045 | case SHT_RELA: | |
3046 | if (get_elf_backend_data (abfd)->may_use_rela_p) | |
3047 | this_hdr->sh_entsize = bed->s->sizeof_rela; | |
3048 | break; | |
3049 | ||
3050 | case SHT_REL: | |
3051 | if (get_elf_backend_data (abfd)->may_use_rel_p) | |
3052 | this_hdr->sh_entsize = bed->s->sizeof_rel; | |
3053 | break; | |
3054 | ||
3055 | case SHT_GNU_versym: | |
252b5132 | 3056 | this_hdr->sh_entsize = sizeof (Elf_External_Versym); |
2f89ff8d L |
3057 | break; |
3058 | ||
3059 | case SHT_GNU_verdef: | |
252b5132 RH |
3060 | this_hdr->sh_entsize = 0; |
3061 | /* objcopy or strip will copy over sh_info, but may not set | |
08a40648 AM |
3062 | cverdefs. The linker will set cverdefs, but sh_info will be |
3063 | zero. */ | |
252b5132 RH |
3064 | if (this_hdr->sh_info == 0) |
3065 | this_hdr->sh_info = elf_tdata (abfd)->cverdefs; | |
3066 | else | |
3067 | BFD_ASSERT (elf_tdata (abfd)->cverdefs == 0 | |
3068 | || this_hdr->sh_info == elf_tdata (abfd)->cverdefs); | |
2f89ff8d L |
3069 | break; |
3070 | ||
3071 | case SHT_GNU_verneed: | |
252b5132 RH |
3072 | this_hdr->sh_entsize = 0; |
3073 | /* objcopy or strip will copy over sh_info, but may not set | |
08a40648 AM |
3074 | cverrefs. The linker will set cverrefs, but sh_info will be |
3075 | zero. */ | |
252b5132 RH |
3076 | if (this_hdr->sh_info == 0) |
3077 | this_hdr->sh_info = elf_tdata (abfd)->cverrefs; | |
3078 | else | |
3079 | BFD_ASSERT (elf_tdata (abfd)->cverrefs == 0 | |
3080 | || this_hdr->sh_info == elf_tdata (abfd)->cverrefs); | |
2f89ff8d L |
3081 | break; |
3082 | ||
3083 | case SHT_GROUP: | |
1783205a | 3084 | this_hdr->sh_entsize = GRP_ENTRY_SIZE; |
2f89ff8d | 3085 | break; |
fdc90cb4 JJ |
3086 | |
3087 | case SHT_GNU_HASH: | |
3088 | this_hdr->sh_entsize = bed->s->arch_size == 64 ? 0 : 4; | |
3089 | break; | |
dbb410c3 | 3090 | } |
252b5132 RH |
3091 | |
3092 | if ((asect->flags & SEC_ALLOC) != 0) | |
3093 | this_hdr->sh_flags |= SHF_ALLOC; | |
3094 | if ((asect->flags & SEC_READONLY) == 0) | |
3095 | this_hdr->sh_flags |= SHF_WRITE; | |
3096 | if ((asect->flags & SEC_CODE) != 0) | |
3097 | this_hdr->sh_flags |= SHF_EXECINSTR; | |
f5fa8ca2 JJ |
3098 | if ((asect->flags & SEC_MERGE) != 0) |
3099 | { | |
3100 | this_hdr->sh_flags |= SHF_MERGE; | |
3101 | this_hdr->sh_entsize = asect->entsize; | |
3102 | if ((asect->flags & SEC_STRINGS) != 0) | |
3103 | this_hdr->sh_flags |= SHF_STRINGS; | |
3104 | } | |
1126897b | 3105 | if ((asect->flags & SEC_GROUP) == 0 && elf_group_name (asect) != NULL) |
dbb410c3 | 3106 | this_hdr->sh_flags |= SHF_GROUP; |
13ae64f3 | 3107 | if ((asect->flags & SEC_THREAD_LOCAL) != 0) |
704afa60 JJ |
3108 | { |
3109 | this_hdr->sh_flags |= SHF_TLS; | |
3a800eb9 AM |
3110 | if (asect->size == 0 |
3111 | && (asect->flags & SEC_HAS_CONTENTS) == 0) | |
704afa60 | 3112 | { |
3a800eb9 | 3113 | struct bfd_link_order *o = asect->map_tail.link_order; |
b34976b6 | 3114 | |
704afa60 | 3115 | this_hdr->sh_size = 0; |
3a800eb9 AM |
3116 | if (o != NULL) |
3117 | { | |
704afa60 | 3118 | this_hdr->sh_size = o->offset + o->size; |
3a800eb9 AM |
3119 | if (this_hdr->sh_size != 0) |
3120 | this_hdr->sh_type = SHT_NOBITS; | |
3121 | } | |
704afa60 JJ |
3122 | } |
3123 | } | |
18ae9cc1 L |
3124 | if ((asect->flags & (SEC_GROUP | SEC_EXCLUDE)) == SEC_EXCLUDE) |
3125 | this_hdr->sh_flags |= SHF_EXCLUDE; | |
252b5132 | 3126 | |
d4730f92 BS |
3127 | /* If the section has relocs, set up a section header for the |
3128 | SHT_REL[A] section. If two relocation sections are required for | |
3129 | this section, it is up to the processor-specific back-end to | |
3130 | create the other. */ | |
3131 | if ((asect->flags & SEC_RELOC) != 0) | |
3132 | { | |
3133 | /* When doing a relocatable link, create both REL and RELA sections if | |
3134 | needed. */ | |
3135 | if (arg->link_info | |
3136 | /* Do the normal setup if we wouldn't create any sections here. */ | |
3137 | && esd->rel.count + esd->rela.count > 0 | |
0e1862bb L |
3138 | && (bfd_link_relocatable (arg->link_info) |
3139 | || arg->link_info->emitrelocations)) | |
d4730f92 BS |
3140 | { |
3141 | if (esd->rel.count && esd->rel.hdr == NULL | |
3e19fb8f L |
3142 | && !_bfd_elf_init_reloc_shdr (abfd, &esd->rel, name, FALSE, |
3143 | delay_st_name_p)) | |
d4730f92 BS |
3144 | { |
3145 | arg->failed = TRUE; | |
3146 | return; | |
3147 | } | |
3148 | if (esd->rela.count && esd->rela.hdr == NULL | |
3e19fb8f L |
3149 | && !_bfd_elf_init_reloc_shdr (abfd, &esd->rela, name, TRUE, |
3150 | delay_st_name_p)) | |
d4730f92 BS |
3151 | { |
3152 | arg->failed = TRUE; | |
3153 | return; | |
3154 | } | |
3155 | } | |
3156 | else if (!_bfd_elf_init_reloc_shdr (abfd, | |
3157 | (asect->use_rela_p | |
3158 | ? &esd->rela : &esd->rel), | |
f6fe1ccd | 3159 | name, |
3e19fb8f L |
3160 | asect->use_rela_p, |
3161 | delay_st_name_p)) | |
d4730f92 BS |
3162 | arg->failed = TRUE; |
3163 | } | |
3164 | ||
252b5132 | 3165 | /* Check for processor-specific section types. */ |
0414f35b | 3166 | sh_type = this_hdr->sh_type; |
e1fddb6b AO |
3167 | if (bed->elf_backend_fake_sections |
3168 | && !(*bed->elf_backend_fake_sections) (abfd, this_hdr, asect)) | |
d4730f92 | 3169 | arg->failed = TRUE; |
252b5132 | 3170 | |
42bb2e33 | 3171 | if (sh_type == SHT_NOBITS && asect->size != 0) |
0414f35b AM |
3172 | { |
3173 | /* Don't change the header type from NOBITS if we are being | |
42bb2e33 | 3174 | called for objcopy --only-keep-debug. */ |
0414f35b AM |
3175 | this_hdr->sh_type = sh_type; |
3176 | } | |
252b5132 RH |
3177 | } |
3178 | ||
bcacc0f5 AM |
3179 | /* Fill in the contents of a SHT_GROUP section. Called from |
3180 | _bfd_elf_compute_section_file_positions for gas, objcopy, and | |
3181 | when ELF targets use the generic linker, ld. Called for ld -r | |
3182 | from bfd_elf_final_link. */ | |
dbb410c3 | 3183 | |
1126897b | 3184 | void |
217aa764 | 3185 | bfd_elf_set_group_contents (bfd *abfd, asection *sec, void *failedptrarg) |
dbb410c3 | 3186 | { |
a50b1753 | 3187 | bfd_boolean *failedptr = (bfd_boolean *) failedptrarg; |
9dce4196 | 3188 | asection *elt, *first; |
dbb410c3 | 3189 | unsigned char *loc; |
b34976b6 | 3190 | bfd_boolean gas; |
dbb410c3 | 3191 | |
7e4111ad L |
3192 | /* Ignore linker created group section. See elfNN_ia64_object_p in |
3193 | elfxx-ia64.c. */ | |
3194 | if (((sec->flags & (SEC_GROUP | SEC_LINKER_CREATED)) != SEC_GROUP) | |
dbb410c3 AM |
3195 | || *failedptr) |
3196 | return; | |
3197 | ||
bcacc0f5 AM |
3198 | if (elf_section_data (sec)->this_hdr.sh_info == 0) |
3199 | { | |
3200 | unsigned long symindx = 0; | |
3201 | ||
3202 | /* elf_group_id will have been set up by objcopy and the | |
3203 | generic linker. */ | |
3204 | if (elf_group_id (sec) != NULL) | |
3205 | symindx = elf_group_id (sec)->udata.i; | |
1126897b | 3206 | |
bcacc0f5 AM |
3207 | if (symindx == 0) |
3208 | { | |
3209 | /* If called from the assembler, swap_out_syms will have set up | |
3210 | elf_section_syms. */ | |
3211 | BFD_ASSERT (elf_section_syms (abfd) != NULL); | |
3212 | symindx = elf_section_syms (abfd)[sec->index]->udata.i; | |
3213 | } | |
3214 | elf_section_data (sec)->this_hdr.sh_info = symindx; | |
3215 | } | |
3216 | else if (elf_section_data (sec)->this_hdr.sh_info == (unsigned int) -2) | |
1126897b | 3217 | { |
bcacc0f5 AM |
3218 | /* The ELF backend linker sets sh_info to -2 when the group |
3219 | signature symbol is global, and thus the index can't be | |
3220 | set until all local symbols are output. */ | |
3221 | asection *igroup = elf_sec_group (elf_next_in_group (sec)); | |
3222 | struct bfd_elf_section_data *sec_data = elf_section_data (igroup); | |
3223 | unsigned long symndx = sec_data->this_hdr.sh_info; | |
3224 | unsigned long extsymoff = 0; | |
3225 | struct elf_link_hash_entry *h; | |
3226 | ||
3227 | if (!elf_bad_symtab (igroup->owner)) | |
3228 | { | |
3229 | Elf_Internal_Shdr *symtab_hdr; | |
3230 | ||
3231 | symtab_hdr = &elf_tdata (igroup->owner)->symtab_hdr; | |
3232 | extsymoff = symtab_hdr->sh_info; | |
3233 | } | |
3234 | h = elf_sym_hashes (igroup->owner)[symndx - extsymoff]; | |
3235 | while (h->root.type == bfd_link_hash_indirect | |
3236 | || h->root.type == bfd_link_hash_warning) | |
3237 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
3238 | ||
3239 | elf_section_data (sec)->this_hdr.sh_info = h->indx; | |
1126897b | 3240 | } |
dbb410c3 | 3241 | |
1126897b | 3242 | /* The contents won't be allocated for "ld -r" or objcopy. */ |
b34976b6 | 3243 | gas = TRUE; |
dbb410c3 AM |
3244 | if (sec->contents == NULL) |
3245 | { | |
b34976b6 | 3246 | gas = FALSE; |
a50b1753 | 3247 | sec->contents = (unsigned char *) bfd_alloc (abfd, sec->size); |
9dce4196 AM |
3248 | |
3249 | /* Arrange for the section to be written out. */ | |
3250 | elf_section_data (sec)->this_hdr.contents = sec->contents; | |
dbb410c3 AM |
3251 | if (sec->contents == NULL) |
3252 | { | |
b34976b6 | 3253 | *failedptr = TRUE; |
dbb410c3 AM |
3254 | return; |
3255 | } | |
3256 | } | |
3257 | ||
eea6121a | 3258 | loc = sec->contents + sec->size; |
dbb410c3 | 3259 | |
9dce4196 AM |
3260 | /* Get the pointer to the first section in the group that gas |
3261 | squirreled away here. objcopy arranges for this to be set to the | |
3262 | start of the input section group. */ | |
3263 | first = elt = elf_next_in_group (sec); | |
dbb410c3 AM |
3264 | |
3265 | /* First element is a flag word. Rest of section is elf section | |
3266 | indices for all the sections of the group. Write them backwards | |
3267 | just to keep the group in the same order as given in .section | |
3268 | directives, not that it matters. */ | |
3269 | while (elt != NULL) | |
3270 | { | |
9dce4196 | 3271 | asection *s; |
9dce4196 | 3272 | |
9dce4196 | 3273 | s = elt; |
415f38a6 AM |
3274 | if (!gas) |
3275 | s = s->output_section; | |
3276 | if (s != NULL | |
3277 | && !bfd_is_abs_section (s)) | |
01e1a5bc | 3278 | { |
415f38a6 AM |
3279 | unsigned int idx = elf_section_data (s)->this_idx; |
3280 | ||
01e1a5bc | 3281 | loc -= 4; |
01e1a5bc NC |
3282 | H_PUT_32 (abfd, idx, loc); |
3283 | } | |
945906ff | 3284 | elt = elf_next_in_group (elt); |
9dce4196 AM |
3285 | if (elt == first) |
3286 | break; | |
dbb410c3 AM |
3287 | } |
3288 | ||
3d7f7666 | 3289 | if ((loc -= 4) != sec->contents) |
9dce4196 | 3290 | abort (); |
dbb410c3 | 3291 | |
9dce4196 | 3292 | H_PUT_32 (abfd, sec->flags & SEC_LINK_ONCE ? GRP_COMDAT : 0, loc); |
dbb410c3 AM |
3293 | } |
3294 | ||
bd53a53a L |
3295 | /* Return the section which RELOC_SEC applies to. */ |
3296 | ||
3297 | asection * | |
3298 | _bfd_elf_get_reloc_section (asection *reloc_sec) | |
3299 | { | |
3300 | const char *name; | |
3301 | unsigned int type; | |
3302 | bfd *abfd; | |
3303 | ||
3304 | if (reloc_sec == NULL) | |
3305 | return NULL; | |
3306 | ||
3307 | type = elf_section_data (reloc_sec)->this_hdr.sh_type; | |
3308 | if (type != SHT_REL && type != SHT_RELA) | |
3309 | return NULL; | |
3310 | ||
3311 | /* We look up the section the relocs apply to by name. */ | |
3312 | name = reloc_sec->name; | |
3313 | if (type == SHT_REL) | |
3314 | name += 4; | |
3315 | else | |
3316 | name += 5; | |
3317 | ||
3318 | /* If a target needs .got.plt section, relocations in rela.plt/rel.plt | |
3319 | section apply to .got.plt section. */ | |
3320 | abfd = reloc_sec->owner; | |
3321 | if (get_elf_backend_data (abfd)->want_got_plt | |
3322 | && strcmp (name, ".plt") == 0) | |
87070c08 AM |
3323 | { |
3324 | /* .got.plt is a linker created input section. It may be mapped | |
3325 | to some other output section. Try two likely sections. */ | |
3326 | name = ".got.plt"; | |
3327 | reloc_sec = bfd_get_section_by_name (abfd, name); | |
3328 | if (reloc_sec != NULL) | |
3329 | return reloc_sec; | |
3330 | name = ".got"; | |
3331 | } | |
bd53a53a L |
3332 | |
3333 | reloc_sec = bfd_get_section_by_name (abfd, name); | |
3334 | return reloc_sec; | |
3335 | } | |
3336 | ||
252b5132 RH |
3337 | /* Assign all ELF section numbers. The dummy first section is handled here |
3338 | too. The link/info pointers for the standard section types are filled | |
3339 | in here too, while we're at it. */ | |
3340 | ||
b34976b6 | 3341 | static bfd_boolean |
da9f89d4 | 3342 | assign_section_numbers (bfd *abfd, struct bfd_link_info *link_info) |
252b5132 RH |
3343 | { |
3344 | struct elf_obj_tdata *t = elf_tdata (abfd); | |
3345 | asection *sec; | |
3e19fb8f | 3346 | unsigned int section_number; |
252b5132 | 3347 | Elf_Internal_Shdr **i_shdrp; |
47cc2cf5 | 3348 | struct bfd_elf_section_data *d; |
3516e984 | 3349 | bfd_boolean need_symtab; |
252b5132 RH |
3350 | |
3351 | section_number = 1; | |
3352 | ||
2b0f7ef9 JJ |
3353 | _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd)); |
3354 | ||
da9f89d4 | 3355 | /* SHT_GROUP sections are in relocatable files only. */ |
0e1862bb | 3356 | if (link_info == NULL || bfd_link_relocatable (link_info)) |
252b5132 | 3357 | { |
da9f89d4 | 3358 | /* Put SHT_GROUP sections first. */ |
04dd1667 | 3359 | for (sec = abfd->sections; sec != NULL; sec = sec->next) |
47cc2cf5 | 3360 | { |
5daa8fe7 | 3361 | d = elf_section_data (sec); |
da9f89d4 L |
3362 | |
3363 | if (d->this_hdr.sh_type == SHT_GROUP) | |
08a40648 | 3364 | { |
5daa8fe7 | 3365 | if (sec->flags & SEC_LINKER_CREATED) |
da9f89d4 L |
3366 | { |
3367 | /* Remove the linker created SHT_GROUP sections. */ | |
5daa8fe7 | 3368 | bfd_section_list_remove (abfd, sec); |
da9f89d4 | 3369 | abfd->section_count--; |
da9f89d4 | 3370 | } |
08a40648 | 3371 | else |
4fbb74a6 | 3372 | d->this_idx = section_number++; |
da9f89d4 | 3373 | } |
47cc2cf5 PB |
3374 | } |
3375 | } | |
3376 | ||
3377 | for (sec = abfd->sections; sec; sec = sec->next) | |
3378 | { | |
3379 | d = elf_section_data (sec); | |
3380 | ||
3381 | if (d->this_hdr.sh_type != SHT_GROUP) | |
4fbb74a6 | 3382 | d->this_idx = section_number++; |
3e19fb8f L |
3383 | if (d->this_hdr.sh_name != (unsigned int) -1) |
3384 | _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->this_hdr.sh_name); | |
d4730f92 | 3385 | if (d->rel.hdr) |
2b0f7ef9 | 3386 | { |
d4730f92 | 3387 | d->rel.idx = section_number++; |
3e19fb8f L |
3388 | if (d->rel.hdr->sh_name != (unsigned int) -1) |
3389 | _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel.hdr->sh_name); | |
2b0f7ef9 | 3390 | } |
d4730f92 BS |
3391 | else |
3392 | d->rel.idx = 0; | |
23bc299b | 3393 | |
d4730f92 | 3394 | if (d->rela.hdr) |
2b0f7ef9 | 3395 | { |
d4730f92 | 3396 | d->rela.idx = section_number++; |
3e19fb8f L |
3397 | if (d->rela.hdr->sh_name != (unsigned int) -1) |
3398 | _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rela.hdr->sh_name); | |
2b0f7ef9 | 3399 | } |
23bc299b | 3400 | else |
d4730f92 | 3401 | d->rela.idx = 0; |
252b5132 RH |
3402 | } |
3403 | ||
12bd6957 | 3404 | elf_shstrtab_sec (abfd) = section_number++; |
2b0f7ef9 | 3405 | _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->shstrtab_hdr.sh_name); |
12bd6957 | 3406 | elf_elfheader (abfd)->e_shstrndx = elf_shstrtab_sec (abfd); |
252b5132 | 3407 | |
3516e984 L |
3408 | need_symtab = (bfd_get_symcount (abfd) > 0 |
3409 | || (link_info == NULL | |
3410 | && ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC)) | |
3411 | == HAS_RELOC))); | |
3412 | if (need_symtab) | |
252b5132 | 3413 | { |
12bd6957 | 3414 | elf_onesymtab (abfd) = section_number++; |
2b0f7ef9 | 3415 | _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->symtab_hdr.sh_name); |
4fbb74a6 | 3416 | if (section_number > ((SHN_LORESERVE - 2) & 0xFFFF)) |
9ad5cbcf | 3417 | { |
6a40cf0c NC |
3418 | elf_section_list * entry; |
3419 | ||
3420 | BFD_ASSERT (elf_symtab_shndx_list (abfd) == NULL); | |
3421 | ||
3422 | entry = bfd_zalloc (abfd, sizeof * entry); | |
3423 | entry->ndx = section_number++; | |
3424 | elf_symtab_shndx_list (abfd) = entry; | |
3425 | entry->hdr.sh_name | |
9ad5cbcf | 3426 | = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), |
b34976b6 | 3427 | ".symtab_shndx", FALSE); |
6a40cf0c | 3428 | if (entry->hdr.sh_name == (unsigned int) -1) |
b34976b6 | 3429 | return FALSE; |
9ad5cbcf | 3430 | } |
12bd6957 | 3431 | elf_strtab_sec (abfd) = section_number++; |
2b0f7ef9 | 3432 | _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->strtab_hdr.sh_name); |
252b5132 RH |
3433 | } |
3434 | ||
1c52a645 L |
3435 | if (section_number >= SHN_LORESERVE) |
3436 | { | |
3437 | _bfd_error_handler (_("%B: too many sections: %u"), | |
3438 | abfd, section_number); | |
3439 | return FALSE; | |
3440 | } | |
3441 | ||
9ad5cbcf | 3442 | elf_numsections (abfd) = section_number; |
252b5132 RH |
3443 | elf_elfheader (abfd)->e_shnum = section_number; |
3444 | ||
3445 | /* Set up the list of section header pointers, in agreement with the | |
3446 | indices. */ | |
a50b1753 NC |
3447 | i_shdrp = (Elf_Internal_Shdr **) bfd_zalloc2 (abfd, section_number, |
3448 | sizeof (Elf_Internal_Shdr *)); | |
252b5132 | 3449 | if (i_shdrp == NULL) |
b34976b6 | 3450 | return FALSE; |
252b5132 | 3451 | |
a50b1753 NC |
3452 | i_shdrp[0] = (Elf_Internal_Shdr *) bfd_zalloc (abfd, |
3453 | sizeof (Elf_Internal_Shdr)); | |
252b5132 RH |
3454 | if (i_shdrp[0] == NULL) |
3455 | { | |
3456 | bfd_release (abfd, i_shdrp); | |
b34976b6 | 3457 | return FALSE; |
252b5132 | 3458 | } |
252b5132 RH |
3459 | |
3460 | elf_elfsections (abfd) = i_shdrp; | |
3461 | ||
12bd6957 | 3462 | i_shdrp[elf_shstrtab_sec (abfd)] = &t->shstrtab_hdr; |
3516e984 | 3463 | if (need_symtab) |
252b5132 | 3464 | { |
12bd6957 | 3465 | i_shdrp[elf_onesymtab (abfd)] = &t->symtab_hdr; |
4fbb74a6 | 3466 | if (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF)) |
9ad5cbcf | 3467 | { |
6a40cf0c NC |
3468 | elf_section_list * entry = elf_symtab_shndx_list (abfd); |
3469 | BFD_ASSERT (entry != NULL); | |
3470 | i_shdrp[entry->ndx] = & entry->hdr; | |
3471 | entry->hdr.sh_link = elf_onesymtab (abfd); | |
9ad5cbcf | 3472 | } |
12bd6957 AM |
3473 | i_shdrp[elf_strtab_sec (abfd)] = &t->strtab_hdr; |
3474 | t->symtab_hdr.sh_link = elf_strtab_sec (abfd); | |
252b5132 | 3475 | } |
38ce5b11 | 3476 | |
252b5132 RH |
3477 | for (sec = abfd->sections; sec; sec = sec->next) |
3478 | { | |
252b5132 | 3479 | asection *s; |
252b5132 | 3480 | |
91d6fa6a NC |
3481 | d = elf_section_data (sec); |
3482 | ||
252b5132 | 3483 | i_shdrp[d->this_idx] = &d->this_hdr; |
d4730f92 BS |
3484 | if (d->rel.idx != 0) |
3485 | i_shdrp[d->rel.idx] = d->rel.hdr; | |
3486 | if (d->rela.idx != 0) | |
3487 | i_shdrp[d->rela.idx] = d->rela.hdr; | |
252b5132 RH |
3488 | |
3489 | /* Fill in the sh_link and sh_info fields while we're at it. */ | |
3490 | ||
3491 | /* sh_link of a reloc section is the section index of the symbol | |
3492 | table. sh_info is the section index of the section to which | |
3493 | the relocation entries apply. */ | |
d4730f92 | 3494 | if (d->rel.idx != 0) |
252b5132 | 3495 | { |
12bd6957 | 3496 | d->rel.hdr->sh_link = elf_onesymtab (abfd); |
d4730f92 | 3497 | d->rel.hdr->sh_info = d->this_idx; |
9ef5d938 | 3498 | d->rel.hdr->sh_flags |= SHF_INFO_LINK; |
252b5132 | 3499 | } |
d4730f92 | 3500 | if (d->rela.idx != 0) |
23bc299b | 3501 | { |
12bd6957 | 3502 | d->rela.hdr->sh_link = elf_onesymtab (abfd); |
d4730f92 | 3503 | d->rela.hdr->sh_info = d->this_idx; |
9ef5d938 | 3504 | d->rela.hdr->sh_flags |= SHF_INFO_LINK; |
23bc299b | 3505 | } |
252b5132 | 3506 | |
38ce5b11 L |
3507 | /* We need to set up sh_link for SHF_LINK_ORDER. */ |
3508 | if ((d->this_hdr.sh_flags & SHF_LINK_ORDER) != 0) | |
3509 | { | |
3510 | s = elf_linked_to_section (sec); | |
3511 | if (s) | |
38ce5b11 | 3512 | { |
f2876037 | 3513 | /* elf_linked_to_section points to the input section. */ |
ccd2ec6a | 3514 | if (link_info != NULL) |
38ce5b11 | 3515 | { |
f2876037 | 3516 | /* Check discarded linkonce section. */ |
dbaa2011 | 3517 | if (discarded_section (s)) |
38ce5b11 | 3518 | { |
ccd2ec6a L |
3519 | asection *kept; |
3520 | (*_bfd_error_handler) | |
3521 | (_("%B: sh_link of section `%A' points to discarded section `%A' of `%B'"), | |
3522 | abfd, d->this_hdr.bfd_section, | |
3523 | s, s->owner); | |
3524 | /* Point to the kept section if it has the same | |
3525 | size as the discarded one. */ | |
c0f00686 | 3526 | kept = _bfd_elf_check_kept_section (s, link_info); |
ccd2ec6a | 3527 | if (kept == NULL) |
185d09ad | 3528 | { |
ccd2ec6a L |
3529 | bfd_set_error (bfd_error_bad_value); |
3530 | return FALSE; | |
185d09ad | 3531 | } |
ccd2ec6a | 3532 | s = kept; |
38ce5b11 | 3533 | } |
e424ecc8 | 3534 | |
ccd2ec6a L |
3535 | s = s->output_section; |
3536 | BFD_ASSERT (s != NULL); | |
38ce5b11 | 3537 | } |
f2876037 L |
3538 | else |
3539 | { | |
3540 | /* Handle objcopy. */ | |
3541 | if (s->output_section == NULL) | |
3542 | { | |
3543 | (*_bfd_error_handler) | |
3544 | (_("%B: sh_link of section `%A' points to removed section `%A' of `%B'"), | |
3545 | abfd, d->this_hdr.bfd_section, s, s->owner); | |
3546 | bfd_set_error (bfd_error_bad_value); | |
3547 | return FALSE; | |
3548 | } | |
3549 | s = s->output_section; | |
3550 | } | |
ccd2ec6a L |
3551 | d->this_hdr.sh_link = elf_section_data (s)->this_idx; |
3552 | } | |
3553 | else | |
3554 | { | |
3555 | /* PR 290: | |
3556 | The Intel C compiler generates SHT_IA_64_UNWIND with | |
3557 | SHF_LINK_ORDER. But it doesn't set the sh_link or | |
3558 | sh_info fields. Hence we could get the situation | |
08a40648 | 3559 | where s is NULL. */ |
ccd2ec6a L |
3560 | const struct elf_backend_data *bed |
3561 | = get_elf_backend_data (abfd); | |
3562 | if (bed->link_order_error_handler) | |
3563 | bed->link_order_error_handler | |
3564 | (_("%B: warning: sh_link not set for section `%A'"), | |
3565 | abfd, sec); | |
38ce5b11 L |
3566 | } |
3567 | } | |
3568 | ||
252b5132 RH |
3569 | switch (d->this_hdr.sh_type) |
3570 | { | |
3571 | case SHT_REL: | |
3572 | case SHT_RELA: | |
3573 | /* A reloc section which we are treating as a normal BFD | |
3574 | section. sh_link is the section index of the symbol | |
3575 | table. sh_info is the section index of the section to | |
3576 | which the relocation entries apply. We assume that an | |
3577 | allocated reloc section uses the dynamic symbol table. | |
3578 | FIXME: How can we be sure? */ | |
3579 | s = bfd_get_section_by_name (abfd, ".dynsym"); | |
3580 | if (s != NULL) | |
3581 | d->this_hdr.sh_link = elf_section_data (s)->this_idx; | |
3582 | ||
bd53a53a | 3583 | s = get_elf_backend_data (abfd)->get_reloc_section (sec); |
252b5132 | 3584 | if (s != NULL) |
9ef5d938 L |
3585 | { |
3586 | d->this_hdr.sh_info = elf_section_data (s)->this_idx; | |
3587 | d->this_hdr.sh_flags |= SHF_INFO_LINK; | |
3588 | } | |
252b5132 RH |
3589 | break; |
3590 | ||
3591 | case SHT_STRTAB: | |
3592 | /* We assume that a section named .stab*str is a stabs | |
3593 | string section. We look for a section with the same name | |
3594 | but without the trailing ``str'', and set its sh_link | |
3595 | field to point to this section. */ | |
0112cd26 | 3596 | if (CONST_STRNEQ (sec->name, ".stab") |
252b5132 RH |
3597 | && strcmp (sec->name + strlen (sec->name) - 3, "str") == 0) |
3598 | { | |
3599 | size_t len; | |
3600 | char *alc; | |
3601 | ||
3602 | len = strlen (sec->name); | |
a50b1753 | 3603 | alc = (char *) bfd_malloc (len - 2); |
252b5132 | 3604 | if (alc == NULL) |
b34976b6 | 3605 | return FALSE; |
d4c88bbb | 3606 | memcpy (alc, sec->name, len - 3); |
252b5132 RH |
3607 | alc[len - 3] = '\0'; |
3608 | s = bfd_get_section_by_name (abfd, alc); | |
3609 | free (alc); | |
3610 | if (s != NULL) | |
3611 | { | |
3612 | elf_section_data (s)->this_hdr.sh_link = d->this_idx; | |
3613 | ||
3614 | /* This is a .stab section. */ | |
0594c12d AM |
3615 | if (elf_section_data (s)->this_hdr.sh_entsize == 0) |
3616 | elf_section_data (s)->this_hdr.sh_entsize | |
3617 | = 4 + 2 * bfd_get_arch_size (abfd) / 8; | |
252b5132 RH |
3618 | } |
3619 | } | |
3620 | break; | |
3621 | ||
3622 | case SHT_DYNAMIC: | |
3623 | case SHT_DYNSYM: | |
3624 | case SHT_GNU_verneed: | |
3625 | case SHT_GNU_verdef: | |
3626 | /* sh_link is the section header index of the string table | |
3627 | used for the dynamic entries, or the symbol table, or the | |
3628 | version strings. */ | |
3629 | s = bfd_get_section_by_name (abfd, ".dynstr"); | |
3630 | if (s != NULL) | |
3631 | d->this_hdr.sh_link = elf_section_data (s)->this_idx; | |
3632 | break; | |
3633 | ||
7f1204bb JJ |
3634 | case SHT_GNU_LIBLIST: |
3635 | /* sh_link is the section header index of the prelink library | |
08a40648 AM |
3636 | list used for the dynamic entries, or the symbol table, or |
3637 | the version strings. */ | |
7f1204bb JJ |
3638 | s = bfd_get_section_by_name (abfd, (sec->flags & SEC_ALLOC) |
3639 | ? ".dynstr" : ".gnu.libstr"); | |
3640 | if (s != NULL) | |
3641 | d->this_hdr.sh_link = elf_section_data (s)->this_idx; | |
3642 | break; | |
3643 | ||
252b5132 | 3644 | case SHT_HASH: |
fdc90cb4 | 3645 | case SHT_GNU_HASH: |
252b5132 RH |
3646 | case SHT_GNU_versym: |
3647 | /* sh_link is the section header index of the symbol table | |
3648 | this hash table or version table is for. */ | |
3649 | s = bfd_get_section_by_name (abfd, ".dynsym"); | |
3650 | if (s != NULL) | |
3651 | d->this_hdr.sh_link = elf_section_data (s)->this_idx; | |
3652 | break; | |
dbb410c3 AM |
3653 | |
3654 | case SHT_GROUP: | |
12bd6957 | 3655 | d->this_hdr.sh_link = elf_onesymtab (abfd); |
252b5132 RH |
3656 | } |
3657 | } | |
3658 | ||
3e19fb8f L |
3659 | /* Delay setting sh_name to _bfd_elf_write_object_contents so that |
3660 | _bfd_elf_assign_file_positions_for_non_load can convert DWARF | |
3661 | debug section name from .debug_* to .zdebug_* if needed. */ | |
3662 | ||
b34976b6 | 3663 | return TRUE; |
252b5132 RH |
3664 | } |
3665 | ||
5372391b | 3666 | static bfd_boolean |
217aa764 | 3667 | sym_is_global (bfd *abfd, asymbol *sym) |
252b5132 RH |
3668 | { |
3669 | /* If the backend has a special mapping, use it. */ | |
9c5bfbb7 | 3670 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
217aa764 AM |
3671 | if (bed->elf_backend_sym_is_global) |
3672 | return (*bed->elf_backend_sym_is_global) (abfd, sym); | |
252b5132 | 3673 | |
e47bf690 | 3674 | return ((sym->flags & (BSF_GLOBAL | BSF_WEAK | BSF_GNU_UNIQUE)) != 0 |
252b5132 RH |
3675 | || bfd_is_und_section (bfd_get_section (sym)) |
3676 | || bfd_is_com_section (bfd_get_section (sym))); | |
3677 | } | |
3678 | ||
5372391b | 3679 | /* Don't output section symbols for sections that are not going to be |
c6d8cab4 | 3680 | output, that are duplicates or there is no BFD section. */ |
5372391b AM |
3681 | |
3682 | static bfd_boolean | |
3683 | ignore_section_sym (bfd *abfd, asymbol *sym) | |
3684 | { | |
c6d8cab4 L |
3685 | elf_symbol_type *type_ptr; |
3686 | ||
3687 | if ((sym->flags & BSF_SECTION_SYM) == 0) | |
3688 | return FALSE; | |
3689 | ||
3690 | type_ptr = elf_symbol_from (abfd, sym); | |
3691 | return ((type_ptr != NULL | |
3692 | && type_ptr->internal_elf_sym.st_shndx != 0 | |
3693 | && bfd_is_abs_section (sym->section)) | |
3694 | || !(sym->section->owner == abfd | |
0f0a5e58 | 3695 | || (sym->section->output_section->owner == abfd |
2633a79c AM |
3696 | && sym->section->output_offset == 0) |
3697 | || bfd_is_abs_section (sym->section))); | |
5372391b AM |
3698 | } |
3699 | ||
2633a79c AM |
3700 | /* Map symbol from it's internal number to the external number, moving |
3701 | all local symbols to be at the head of the list. */ | |
3702 | ||
b34976b6 | 3703 | static bfd_boolean |
12bd6957 | 3704 | elf_map_symbols (bfd *abfd, unsigned int *pnum_locals) |
252b5132 | 3705 | { |
dc810e39 | 3706 | unsigned int symcount = bfd_get_symcount (abfd); |
252b5132 RH |
3707 | asymbol **syms = bfd_get_outsymbols (abfd); |
3708 | asymbol **sect_syms; | |
dc810e39 AM |
3709 | unsigned int num_locals = 0; |
3710 | unsigned int num_globals = 0; | |
3711 | unsigned int num_locals2 = 0; | |
3712 | unsigned int num_globals2 = 0; | |
7292b3ac | 3713 | unsigned int max_index = 0; |
dc810e39 | 3714 | unsigned int idx; |
252b5132 RH |
3715 | asection *asect; |
3716 | asymbol **new_syms; | |
252b5132 RH |
3717 | |
3718 | #ifdef DEBUG | |
3719 | fprintf (stderr, "elf_map_symbols\n"); | |
3720 | fflush (stderr); | |
3721 | #endif | |
3722 | ||
252b5132 RH |
3723 | for (asect = abfd->sections; asect; asect = asect->next) |
3724 | { | |
3725 | if (max_index < asect->index) | |
3726 | max_index = asect->index; | |
3727 | } | |
3728 | ||
3729 | max_index++; | |
a50b1753 | 3730 | sect_syms = (asymbol **) bfd_zalloc2 (abfd, max_index, sizeof (asymbol *)); |
252b5132 | 3731 | if (sect_syms == NULL) |
b34976b6 | 3732 | return FALSE; |
252b5132 | 3733 | elf_section_syms (abfd) = sect_syms; |
4e89ac30 | 3734 | elf_num_section_syms (abfd) = max_index; |
252b5132 | 3735 | |
079e9a2f AM |
3736 | /* Init sect_syms entries for any section symbols we have already |
3737 | decided to output. */ | |
252b5132 RH |
3738 | for (idx = 0; idx < symcount; idx++) |
3739 | { | |
dc810e39 | 3740 | asymbol *sym = syms[idx]; |
c044fabd | 3741 | |
252b5132 | 3742 | if ((sym->flags & BSF_SECTION_SYM) != 0 |
0f0a5e58 | 3743 | && sym->value == 0 |
2633a79c AM |
3744 | && !ignore_section_sym (abfd, sym) |
3745 | && !bfd_is_abs_section (sym->section)) | |
252b5132 | 3746 | { |
5372391b | 3747 | asection *sec = sym->section; |
252b5132 | 3748 | |
5372391b AM |
3749 | if (sec->owner != abfd) |
3750 | sec = sec->output_section; | |
252b5132 | 3751 | |
5372391b | 3752 | sect_syms[sec->index] = syms[idx]; |
252b5132 RH |
3753 | } |
3754 | } | |
3755 | ||
252b5132 RH |
3756 | /* Classify all of the symbols. */ |
3757 | for (idx = 0; idx < symcount; idx++) | |
3758 | { | |
2633a79c | 3759 | if (sym_is_global (abfd, syms[idx])) |
252b5132 | 3760 | num_globals++; |
2633a79c AM |
3761 | else if (!ignore_section_sym (abfd, syms[idx])) |
3762 | num_locals++; | |
252b5132 | 3763 | } |
079e9a2f | 3764 | |
5372391b | 3765 | /* We will be adding a section symbol for each normal BFD section. Most |
079e9a2f AM |
3766 | sections will already have a section symbol in outsymbols, but |
3767 | eg. SHT_GROUP sections will not, and we need the section symbol mapped | |
3768 | at least in that case. */ | |
252b5132 RH |
3769 | for (asect = abfd->sections; asect; asect = asect->next) |
3770 | { | |
079e9a2f | 3771 | if (sect_syms[asect->index] == NULL) |
252b5132 | 3772 | { |
079e9a2f | 3773 | if (!sym_is_global (abfd, asect->symbol)) |
252b5132 RH |
3774 | num_locals++; |
3775 | else | |
3776 | num_globals++; | |
252b5132 RH |
3777 | } |
3778 | } | |
3779 | ||
3780 | /* Now sort the symbols so the local symbols are first. */ | |
a50b1753 NC |
3781 | new_syms = (asymbol **) bfd_alloc2 (abfd, num_locals + num_globals, |
3782 | sizeof (asymbol *)); | |
dc810e39 | 3783 | |
252b5132 | 3784 | if (new_syms == NULL) |
b34976b6 | 3785 | return FALSE; |
252b5132 RH |
3786 | |
3787 | for (idx = 0; idx < symcount; idx++) | |
3788 | { | |
3789 | asymbol *sym = syms[idx]; | |
dc810e39 | 3790 | unsigned int i; |
252b5132 | 3791 | |
2633a79c AM |
3792 | if (sym_is_global (abfd, sym)) |
3793 | i = num_locals + num_globals2++; | |
3794 | else if (!ignore_section_sym (abfd, sym)) | |
252b5132 RH |
3795 | i = num_locals2++; |
3796 | else | |
2633a79c | 3797 | continue; |
252b5132 RH |
3798 | new_syms[i] = sym; |
3799 | sym->udata.i = i + 1; | |
3800 | } | |
3801 | for (asect = abfd->sections; asect; asect = asect->next) | |
3802 | { | |
079e9a2f | 3803 | if (sect_syms[asect->index] == NULL) |
252b5132 | 3804 | { |
079e9a2f | 3805 | asymbol *sym = asect->symbol; |
dc810e39 | 3806 | unsigned int i; |
252b5132 | 3807 | |
079e9a2f | 3808 | sect_syms[asect->index] = sym; |
252b5132 RH |
3809 | if (!sym_is_global (abfd, sym)) |
3810 | i = num_locals2++; | |
3811 | else | |
3812 | i = num_locals + num_globals2++; | |
3813 | new_syms[i] = sym; | |
3814 | sym->udata.i = i + 1; | |
3815 | } | |
3816 | } | |
3817 | ||
3818 | bfd_set_symtab (abfd, new_syms, num_locals + num_globals); | |
3819 | ||
12bd6957 | 3820 | *pnum_locals = num_locals; |
b34976b6 | 3821 | return TRUE; |
252b5132 RH |
3822 | } |
3823 | ||
3824 | /* Align to the maximum file alignment that could be required for any | |
3825 | ELF data structure. */ | |
3826 | ||
268b6b39 | 3827 | static inline file_ptr |
217aa764 | 3828 | align_file_position (file_ptr off, int align) |
252b5132 RH |
3829 | { |
3830 | return (off + align - 1) & ~(align - 1); | |
3831 | } | |
3832 | ||
3833 | /* Assign a file position to a section, optionally aligning to the | |
3834 | required section alignment. */ | |
3835 | ||
217aa764 AM |
3836 | file_ptr |
3837 | _bfd_elf_assign_file_position_for_section (Elf_Internal_Shdr *i_shdrp, | |
3838 | file_ptr offset, | |
3839 | bfd_boolean align) | |
252b5132 | 3840 | { |
72de5009 AM |
3841 | if (align && i_shdrp->sh_addralign > 1) |
3842 | offset = BFD_ALIGN (offset, i_shdrp->sh_addralign); | |
252b5132 RH |
3843 | i_shdrp->sh_offset = offset; |
3844 | if (i_shdrp->bfd_section != NULL) | |
3845 | i_shdrp->bfd_section->filepos = offset; | |
3846 | if (i_shdrp->sh_type != SHT_NOBITS) | |
3847 | offset += i_shdrp->sh_size; | |
3848 | return offset; | |
3849 | } | |
3850 | ||
3851 | /* Compute the file positions we are going to put the sections at, and | |
3852 | otherwise prepare to begin writing out the ELF file. If LINK_INFO | |
3853 | is not NULL, this is being called by the ELF backend linker. */ | |
3854 | ||
b34976b6 | 3855 | bfd_boolean |
217aa764 AM |
3856 | _bfd_elf_compute_section_file_positions (bfd *abfd, |
3857 | struct bfd_link_info *link_info) | |
252b5132 | 3858 | { |
9c5bfbb7 | 3859 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
d4730f92 | 3860 | struct fake_section_arg fsargs; |
b34976b6 | 3861 | bfd_boolean failed; |
ef10c3ac | 3862 | struct elf_strtab_hash *strtab = NULL; |
252b5132 | 3863 | Elf_Internal_Shdr *shstrtab_hdr; |
3516e984 | 3864 | bfd_boolean need_symtab; |
252b5132 RH |
3865 | |
3866 | if (abfd->output_has_begun) | |
b34976b6 | 3867 | return TRUE; |
252b5132 RH |
3868 | |
3869 | /* Do any elf backend specific processing first. */ | |
3870 | if (bed->elf_backend_begin_write_processing) | |
3871 | (*bed->elf_backend_begin_write_processing) (abfd, link_info); | |
3872 | ||
3873 | if (! prep_headers (abfd)) | |
b34976b6 | 3874 | return FALSE; |
252b5132 | 3875 | |
e6c51ed4 | 3876 | /* Post process the headers if necessary. */ |
78245035 | 3877 | (*bed->elf_backend_post_process_headers) (abfd, link_info); |
e6c51ed4 | 3878 | |
d4730f92 BS |
3879 | fsargs.failed = FALSE; |
3880 | fsargs.link_info = link_info; | |
3881 | bfd_map_over_sections (abfd, elf_fake_sections, &fsargs); | |
3882 | if (fsargs.failed) | |
b34976b6 | 3883 | return FALSE; |
252b5132 | 3884 | |
da9f89d4 | 3885 | if (!assign_section_numbers (abfd, link_info)) |
b34976b6 | 3886 | return FALSE; |
252b5132 RH |
3887 | |
3888 | /* The backend linker builds symbol table information itself. */ | |
3516e984 L |
3889 | need_symtab = (link_info == NULL |
3890 | && (bfd_get_symcount (abfd) > 0 | |
3891 | || ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC)) | |
3892 | == HAS_RELOC))); | |
3893 | if (need_symtab) | |
252b5132 RH |
3894 | { |
3895 | /* Non-zero if doing a relocatable link. */ | |
3896 | int relocatable_p = ! (abfd->flags & (EXEC_P | DYNAMIC)); | |
3897 | ||
3898 | if (! swap_out_syms (abfd, &strtab, relocatable_p)) | |
b34976b6 | 3899 | return FALSE; |
252b5132 RH |
3900 | } |
3901 | ||
d4730f92 | 3902 | failed = FALSE; |
1126897b | 3903 | if (link_info == NULL) |
dbb410c3 | 3904 | { |
1126897b | 3905 | bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed); |
dbb410c3 | 3906 | if (failed) |
b34976b6 | 3907 | return FALSE; |
dbb410c3 AM |
3908 | } |
3909 | ||
252b5132 RH |
3910 | shstrtab_hdr = &elf_tdata (abfd)->shstrtab_hdr; |
3911 | /* sh_name was set in prep_headers. */ | |
3912 | shstrtab_hdr->sh_type = SHT_STRTAB; | |
3913 | shstrtab_hdr->sh_flags = 0; | |
3914 | shstrtab_hdr->sh_addr = 0; | |
946748d5 | 3915 | /* sh_size is set in _bfd_elf_assign_file_positions_for_non_load. */ |
252b5132 RH |
3916 | shstrtab_hdr->sh_entsize = 0; |
3917 | shstrtab_hdr->sh_link = 0; | |
3918 | shstrtab_hdr->sh_info = 0; | |
3e19fb8f | 3919 | /* sh_offset is set in _bfd_elf_assign_file_positions_for_non_load. */ |
252b5132 RH |
3920 | shstrtab_hdr->sh_addralign = 1; |
3921 | ||
c84fca4d | 3922 | if (!assign_file_positions_except_relocs (abfd, link_info)) |
b34976b6 | 3923 | return FALSE; |
252b5132 | 3924 | |
3516e984 | 3925 | if (need_symtab) |
252b5132 RH |
3926 | { |
3927 | file_ptr off; | |
3928 | Elf_Internal_Shdr *hdr; | |
3929 | ||
12bd6957 | 3930 | off = elf_next_file_pos (abfd); |
252b5132 | 3931 | |
6a40cf0c | 3932 | hdr = & elf_symtab_hdr (abfd); |
b34976b6 | 3933 | off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE); |
252b5132 | 3934 | |
6a40cf0c NC |
3935 | if (elf_symtab_shndx_list (abfd) != NULL) |
3936 | { | |
3937 | hdr = & elf_symtab_shndx_list (abfd)->hdr; | |
3938 | if (hdr->sh_size != 0) | |
3939 | off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE); | |
3940 | /* FIXME: What about other symtab_shndx sections in the list ? */ | |
3941 | } | |
9ad5cbcf | 3942 | |
252b5132 | 3943 | hdr = &elf_tdata (abfd)->strtab_hdr; |
b34976b6 | 3944 | off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE); |
252b5132 | 3945 | |
12bd6957 | 3946 | elf_next_file_pos (abfd) = off; |
252b5132 RH |
3947 | |
3948 | /* Now that we know where the .strtab section goes, write it | |
08a40648 | 3949 | out. */ |
252b5132 | 3950 | if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0 |
ef10c3ac | 3951 | || ! _bfd_elf_strtab_emit (abfd, strtab)) |
b34976b6 | 3952 | return FALSE; |
ef10c3ac | 3953 | _bfd_elf_strtab_free (strtab); |
252b5132 RH |
3954 | } |
3955 | ||
b34976b6 | 3956 | abfd->output_has_begun = TRUE; |
252b5132 | 3957 | |
b34976b6 | 3958 | return TRUE; |
252b5132 RH |
3959 | } |
3960 | ||
8ded5a0f AM |
3961 | /* Make an initial estimate of the size of the program header. If we |
3962 | get the number wrong here, we'll redo section placement. */ | |
3963 | ||
3964 | static bfd_size_type | |
3965 | get_program_header_size (bfd *abfd, struct bfd_link_info *info) | |
3966 | { | |
3967 | size_t segs; | |
3968 | asection *s; | |
2b05f1b7 | 3969 | const struct elf_backend_data *bed; |
8ded5a0f AM |
3970 | |
3971 | /* Assume we will need exactly two PT_LOAD segments: one for text | |
3972 | and one for data. */ | |
3973 | segs = 2; | |
3974 | ||
3975 | s = bfd_get_section_by_name (abfd, ".interp"); | |
3976 | if (s != NULL && (s->flags & SEC_LOAD) != 0) | |
3977 | { | |
3978 | /* If we have a loadable interpreter section, we need a | |
3979 | PT_INTERP segment. In this case, assume we also need a | |
3980 | PT_PHDR segment, although that may not be true for all | |
3981 | targets. */ | |
3982 | segs += 2; | |
3983 | } | |
3984 | ||
3985 | if (bfd_get_section_by_name (abfd, ".dynamic") != NULL) | |
3986 | { | |
3987 | /* We need a PT_DYNAMIC segment. */ | |
3988 | ++segs; | |
f210dcff | 3989 | } |
08a40648 | 3990 | |
ceae84aa | 3991 | if (info != NULL && info->relro) |
f210dcff L |
3992 | { |
3993 | /* We need a PT_GNU_RELRO segment. */ | |
3994 | ++segs; | |
8ded5a0f AM |
3995 | } |
3996 | ||
12bd6957 | 3997 | if (elf_eh_frame_hdr (abfd)) |
8ded5a0f AM |
3998 | { |
3999 | /* We need a PT_GNU_EH_FRAME segment. */ | |
4000 | ++segs; | |
4001 | } | |
4002 | ||
12bd6957 | 4003 | if (elf_stack_flags (abfd)) |
8ded5a0f | 4004 | { |
2b05f1b7 L |
4005 | /* We need a PT_GNU_STACK segment. */ |
4006 | ++segs; | |
4007 | } | |
94b11780 | 4008 | |
2b05f1b7 L |
4009 | for (s = abfd->sections; s != NULL; s = s->next) |
4010 | { | |
8ded5a0f | 4011 | if ((s->flags & SEC_LOAD) != 0 |
0112cd26 | 4012 | && CONST_STRNEQ (s->name, ".note")) |
8ded5a0f AM |
4013 | { |
4014 | /* We need a PT_NOTE segment. */ | |
4015 | ++segs; | |
1c5265b5 JJ |
4016 | /* Try to create just one PT_NOTE segment |
4017 | for all adjacent loadable .note* sections. | |
4018 | gABI requires that within a PT_NOTE segment | |
4019 | (and also inside of each SHT_NOTE section) | |
4020 | each note is padded to a multiple of 4 size, | |
4021 | so we check whether the sections are correctly | |
4022 | aligned. */ | |
4023 | if (s->alignment_power == 2) | |
4024 | while (s->next != NULL | |
4025 | && s->next->alignment_power == 2 | |
4026 | && (s->next->flags & SEC_LOAD) != 0 | |
4027 | && CONST_STRNEQ (s->next->name, ".note")) | |
4028 | s = s->next; | |
8ded5a0f AM |
4029 | } |
4030 | } | |
4031 | ||
4032 | for (s = abfd->sections; s != NULL; s = s->next) | |
4033 | { | |
4034 | if (s->flags & SEC_THREAD_LOCAL) | |
4035 | { | |
4036 | /* We need a PT_TLS segment. */ | |
4037 | ++segs; | |
4038 | break; | |
4039 | } | |
4040 | } | |
4041 | ||
4042 | /* Let the backend count up any program headers it might need. */ | |
2b05f1b7 | 4043 | bed = get_elf_backend_data (abfd); |
8ded5a0f AM |
4044 | if (bed->elf_backend_additional_program_headers) |
4045 | { | |
4046 | int a; | |
4047 | ||
4048 | a = (*bed->elf_backend_additional_program_headers) (abfd, info); | |
4049 | if (a == -1) | |
4050 | abort (); | |
4051 | segs += a; | |
4052 | } | |
4053 | ||
4054 | return segs * bed->s->sizeof_phdr; | |
4055 | } | |
4056 | ||
2ea37f1c NC |
4057 | /* Find the segment that contains the output_section of section. */ |
4058 | ||
4059 | Elf_Internal_Phdr * | |
4060 | _bfd_elf_find_segment_containing_section (bfd * abfd, asection * section) | |
4061 | { | |
4062 | struct elf_segment_map *m; | |
4063 | Elf_Internal_Phdr *p; | |
4064 | ||
12bd6957 | 4065 | for (m = elf_seg_map (abfd), p = elf_tdata (abfd)->phdr; |
2ea37f1c NC |
4066 | m != NULL; |
4067 | m = m->next, p++) | |
4068 | { | |
4069 | int i; | |
4070 | ||
4071 | for (i = m->count - 1; i >= 0; i--) | |
4072 | if (m->sections[i] == section) | |
4073 | return p; | |
4074 | } | |
4075 | ||
4076 | return NULL; | |
4077 | } | |
4078 | ||
252b5132 RH |
4079 | /* Create a mapping from a set of sections to a program segment. */ |
4080 | ||
217aa764 AM |
4081 | static struct elf_segment_map * |
4082 | make_mapping (bfd *abfd, | |
4083 | asection **sections, | |
4084 | unsigned int from, | |
4085 | unsigned int to, | |
4086 | bfd_boolean phdr) | |
252b5132 RH |
4087 | { |
4088 | struct elf_segment_map *m; | |
4089 | unsigned int i; | |
4090 | asection **hdrpp; | |
dc810e39 | 4091 | bfd_size_type amt; |
252b5132 | 4092 | |
dc810e39 AM |
4093 | amt = sizeof (struct elf_segment_map); |
4094 | amt += (to - from - 1) * sizeof (asection *); | |
a50b1753 | 4095 | m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); |
252b5132 RH |
4096 | if (m == NULL) |
4097 | return NULL; | |
4098 | m->next = NULL; | |
4099 | m->p_type = PT_LOAD; | |
4100 | for (i = from, hdrpp = sections + from; i < to; i++, hdrpp++) | |
4101 | m->sections[i - from] = *hdrpp; | |
4102 | m->count = to - from; | |
4103 | ||
4104 | if (from == 0 && phdr) | |
4105 | { | |
4106 | /* Include the headers in the first PT_LOAD segment. */ | |
4107 | m->includes_filehdr = 1; | |
4108 | m->includes_phdrs = 1; | |
4109 | } | |
4110 | ||
4111 | return m; | |
4112 | } | |
4113 | ||
229fcec5 MM |
4114 | /* Create the PT_DYNAMIC segment, which includes DYNSEC. Returns NULL |
4115 | on failure. */ | |
4116 | ||
4117 | struct elf_segment_map * | |
4118 | _bfd_elf_make_dynamic_segment (bfd *abfd, asection *dynsec) | |
4119 | { | |
4120 | struct elf_segment_map *m; | |
4121 | ||
a50b1753 NC |
4122 | m = (struct elf_segment_map *) bfd_zalloc (abfd, |
4123 | sizeof (struct elf_segment_map)); | |
229fcec5 MM |
4124 | if (m == NULL) |
4125 | return NULL; | |
4126 | m->next = NULL; | |
4127 | m->p_type = PT_DYNAMIC; | |
4128 | m->count = 1; | |
4129 | m->sections[0] = dynsec; | |
08a40648 | 4130 | |
229fcec5 MM |
4131 | return m; |
4132 | } | |
4133 | ||
8ded5a0f | 4134 | /* Possibly add or remove segments from the segment map. */ |
252b5132 | 4135 | |
b34976b6 | 4136 | static bfd_boolean |
3dea8fca AM |
4137 | elf_modify_segment_map (bfd *abfd, |
4138 | struct bfd_link_info *info, | |
4139 | bfd_boolean remove_empty_load) | |
252b5132 | 4140 | { |
252e386e | 4141 | struct elf_segment_map **m; |
8ded5a0f | 4142 | const struct elf_backend_data *bed; |
252b5132 | 4143 | |
8ded5a0f AM |
4144 | /* The placement algorithm assumes that non allocated sections are |
4145 | not in PT_LOAD segments. We ensure this here by removing such | |
4146 | sections from the segment map. We also remove excluded | |
252e386e AM |
4147 | sections. Finally, any PT_LOAD segment without sections is |
4148 | removed. */ | |
12bd6957 | 4149 | m = &elf_seg_map (abfd); |
252e386e | 4150 | while (*m) |
8ded5a0f AM |
4151 | { |
4152 | unsigned int i, new_count; | |
252b5132 | 4153 | |
252e386e | 4154 | for (new_count = 0, i = 0; i < (*m)->count; i++) |
8ded5a0f | 4155 | { |
252e386e AM |
4156 | if (((*m)->sections[i]->flags & SEC_EXCLUDE) == 0 |
4157 | && (((*m)->sections[i]->flags & SEC_ALLOC) != 0 | |
4158 | || (*m)->p_type != PT_LOAD)) | |
8ded5a0f | 4159 | { |
252e386e AM |
4160 | (*m)->sections[new_count] = (*m)->sections[i]; |
4161 | new_count++; | |
8ded5a0f AM |
4162 | } |
4163 | } | |
252e386e | 4164 | (*m)->count = new_count; |
252b5132 | 4165 | |
3dea8fca | 4166 | if (remove_empty_load && (*m)->p_type == PT_LOAD && (*m)->count == 0) |
252e386e AM |
4167 | *m = (*m)->next; |
4168 | else | |
4169 | m = &(*m)->next; | |
8ded5a0f | 4170 | } |
252b5132 | 4171 | |
8ded5a0f AM |
4172 | bed = get_elf_backend_data (abfd); |
4173 | if (bed->elf_backend_modify_segment_map != NULL) | |
252b5132 | 4174 | { |
252e386e | 4175 | if (!(*bed->elf_backend_modify_segment_map) (abfd, info)) |
8ded5a0f | 4176 | return FALSE; |
252b5132 | 4177 | } |
252b5132 | 4178 | |
8ded5a0f AM |
4179 | return TRUE; |
4180 | } | |
252b5132 | 4181 | |
8ded5a0f | 4182 | /* Set up a mapping from BFD sections to program segments. */ |
252b5132 | 4183 | |
8ded5a0f AM |
4184 | bfd_boolean |
4185 | _bfd_elf_map_sections_to_segments (bfd *abfd, struct bfd_link_info *info) | |
4186 | { | |
4187 | unsigned int count; | |
4188 | struct elf_segment_map *m; | |
4189 | asection **sections = NULL; | |
4190 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
3dea8fca | 4191 | bfd_boolean no_user_phdrs; |
252b5132 | 4192 | |
12bd6957 | 4193 | no_user_phdrs = elf_seg_map (abfd) == NULL; |
d324f6d6 RM |
4194 | |
4195 | if (info != NULL) | |
4196 | info->user_phdrs = !no_user_phdrs; | |
4197 | ||
3dea8fca | 4198 | if (no_user_phdrs && bfd_count_sections (abfd) != 0) |
252b5132 | 4199 | { |
8ded5a0f AM |
4200 | asection *s; |
4201 | unsigned int i; | |
4202 | struct elf_segment_map *mfirst; | |
4203 | struct elf_segment_map **pm; | |
4204 | asection *last_hdr; | |
4205 | bfd_vma last_size; | |
4206 | unsigned int phdr_index; | |
4207 | bfd_vma maxpagesize; | |
4208 | asection **hdrpp; | |
4209 | bfd_boolean phdr_in_segment = TRUE; | |
4210 | bfd_boolean writable; | |
4211 | int tls_count = 0; | |
4212 | asection *first_tls = NULL; | |
4213 | asection *dynsec, *eh_frame_hdr; | |
4214 | bfd_size_type amt; | |
8d06853e | 4215 | bfd_vma addr_mask, wrap_to = 0; |
252b5132 | 4216 | |
8ded5a0f | 4217 | /* Select the allocated sections, and sort them. */ |
252b5132 | 4218 | |
a50b1753 NC |
4219 | sections = (asection **) bfd_malloc2 (bfd_count_sections (abfd), |
4220 | sizeof (asection *)); | |
8ded5a0f | 4221 | if (sections == NULL) |
252b5132 | 4222 | goto error_return; |
252b5132 | 4223 | |
8d06853e AM |
4224 | /* Calculate top address, avoiding undefined behaviour of shift |
4225 | left operator when shift count is equal to size of type | |
4226 | being shifted. */ | |
4227 | addr_mask = ((bfd_vma) 1 << (bfd_arch_bits_per_address (abfd) - 1)) - 1; | |
4228 | addr_mask = (addr_mask << 1) + 1; | |
4229 | ||
8ded5a0f AM |
4230 | i = 0; |
4231 | for (s = abfd->sections; s != NULL; s = s->next) | |
4232 | { | |
4233 | if ((s->flags & SEC_ALLOC) != 0) | |
4234 | { | |
4235 | sections[i] = s; | |
4236 | ++i; | |
8d06853e AM |
4237 | /* A wrapping section potentially clashes with header. */ |
4238 | if (((s->lma + s->size) & addr_mask) < (s->lma & addr_mask)) | |
4239 | wrap_to = (s->lma + s->size) & addr_mask; | |
8ded5a0f AM |
4240 | } |
4241 | } | |
4242 | BFD_ASSERT (i <= bfd_count_sections (abfd)); | |
4243 | count = i; | |
252b5132 | 4244 | |
8ded5a0f | 4245 | qsort (sections, (size_t) count, sizeof (asection *), elf_sort_sections); |
252b5132 | 4246 | |
8ded5a0f | 4247 | /* Build the mapping. */ |
252b5132 | 4248 | |
8ded5a0f AM |
4249 | mfirst = NULL; |
4250 | pm = &mfirst; | |
252b5132 | 4251 | |
8ded5a0f AM |
4252 | /* If we have a .interp section, then create a PT_PHDR segment for |
4253 | the program headers and a PT_INTERP segment for the .interp | |
4254 | section. */ | |
4255 | s = bfd_get_section_by_name (abfd, ".interp"); | |
4256 | if (s != NULL && (s->flags & SEC_LOAD) != 0) | |
4257 | { | |
4258 | amt = sizeof (struct elf_segment_map); | |
a50b1753 | 4259 | m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); |
8ded5a0f AM |
4260 | if (m == NULL) |
4261 | goto error_return; | |
4262 | m->next = NULL; | |
4263 | m->p_type = PT_PHDR; | |
4264 | /* FIXME: UnixWare and Solaris set PF_X, Irix 5 does not. */ | |
4265 | m->p_flags = PF_R | PF_X; | |
4266 | m->p_flags_valid = 1; | |
4267 | m->includes_phdrs = 1; | |
252b5132 | 4268 | |
8ded5a0f AM |
4269 | *pm = m; |
4270 | pm = &m->next; | |
252b5132 | 4271 | |
8ded5a0f | 4272 | amt = sizeof (struct elf_segment_map); |
a50b1753 | 4273 | m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); |
8ded5a0f AM |
4274 | if (m == NULL) |
4275 | goto error_return; | |
4276 | m->next = NULL; | |
4277 | m->p_type = PT_INTERP; | |
4278 | m->count = 1; | |
4279 | m->sections[0] = s; | |
4280 | ||
4281 | *pm = m; | |
4282 | pm = &m->next; | |
252b5132 | 4283 | } |
8ded5a0f AM |
4284 | |
4285 | /* Look through the sections. We put sections in the same program | |
4286 | segment when the start of the second section can be placed within | |
4287 | a few bytes of the end of the first section. */ | |
4288 | last_hdr = NULL; | |
4289 | last_size = 0; | |
4290 | phdr_index = 0; | |
4291 | maxpagesize = bed->maxpagesize; | |
063bb025 NC |
4292 | /* PR 17512: file: c8455299. |
4293 | Avoid divide-by-zero errors later on. | |
4294 | FIXME: Should we abort if the maxpagesize is zero ? */ | |
4295 | if (maxpagesize == 0) | |
4296 | maxpagesize = 1; | |
8ded5a0f AM |
4297 | writable = FALSE; |
4298 | dynsec = bfd_get_section_by_name (abfd, ".dynamic"); | |
4299 | if (dynsec != NULL | |
4300 | && (dynsec->flags & SEC_LOAD) == 0) | |
4301 | dynsec = NULL; | |
4302 | ||
4303 | /* Deal with -Ttext or something similar such that the first section | |
4304 | is not adjacent to the program headers. This is an | |
4305 | approximation, since at this point we don't know exactly how many | |
4306 | program headers we will need. */ | |
4307 | if (count > 0) | |
252b5132 | 4308 | { |
12bd6957 | 4309 | bfd_size_type phdr_size = elf_program_header_size (abfd); |
8ded5a0f | 4310 | |
62d7a5f6 | 4311 | if (phdr_size == (bfd_size_type) -1) |
8ded5a0f | 4312 | phdr_size = get_program_header_size (abfd, info); |
d2bcb0d1 | 4313 | phdr_size += bed->s->sizeof_ehdr; |
8ded5a0f | 4314 | if ((abfd->flags & D_PAGED) == 0 |
8d06853e AM |
4315 | || (sections[0]->lma & addr_mask) < phdr_size |
4316 | || ((sections[0]->lma & addr_mask) % maxpagesize | |
4317 | < phdr_size % maxpagesize) | |
4318 | || (sections[0]->lma & addr_mask & -maxpagesize) < wrap_to) | |
8ded5a0f | 4319 | phdr_in_segment = FALSE; |
252b5132 RH |
4320 | } |
4321 | ||
8ded5a0f | 4322 | for (i = 0, hdrpp = sections; i < count; i++, hdrpp++) |
252b5132 | 4323 | { |
8ded5a0f AM |
4324 | asection *hdr; |
4325 | bfd_boolean new_segment; | |
4326 | ||
4327 | hdr = *hdrpp; | |
4328 | ||
4329 | /* See if this section and the last one will fit in the same | |
4330 | segment. */ | |
4331 | ||
4332 | if (last_hdr == NULL) | |
4333 | { | |
4334 | /* If we don't have a segment yet, then we don't need a new | |
4335 | one (we build the last one after this loop). */ | |
4336 | new_segment = FALSE; | |
4337 | } | |
4338 | else if (last_hdr->lma - last_hdr->vma != hdr->lma - hdr->vma) | |
4339 | { | |
4340 | /* If this section has a different relation between the | |
4341 | virtual address and the load address, then we need a new | |
4342 | segment. */ | |
4343 | new_segment = TRUE; | |
4344 | } | |
b5599592 AM |
4345 | else if (hdr->lma < last_hdr->lma + last_size |
4346 | || last_hdr->lma + last_size < last_hdr->lma) | |
4347 | { | |
4348 | /* If this section has a load address that makes it overlap | |
4349 | the previous section, then we need a new segment. */ | |
4350 | new_segment = TRUE; | |
4351 | } | |
39948a60 NC |
4352 | /* In the next test we have to be careful when last_hdr->lma is close |
4353 | to the end of the address space. If the aligned address wraps | |
4354 | around to the start of the address space, then there are no more | |
4355 | pages left in memory and it is OK to assume that the current | |
4356 | section can be included in the current segment. */ | |
4357 | else if ((BFD_ALIGN (last_hdr->lma + last_size, maxpagesize) + maxpagesize | |
4358 | > last_hdr->lma) | |
4359 | && (BFD_ALIGN (last_hdr->lma + last_size, maxpagesize) + maxpagesize | |
4ff73856 | 4360 | <= hdr->lma)) |
8ded5a0f AM |
4361 | { |
4362 | /* If putting this section in this segment would force us to | |
4363 | skip a page in the segment, then we need a new segment. */ | |
4364 | new_segment = TRUE; | |
4365 | } | |
4366 | else if ((last_hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0 | |
e5654c0f AM |
4367 | && (hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) != 0 |
4368 | && ((abfd->flags & D_PAGED) == 0 | |
4369 | || (((last_hdr->lma + last_size - 1) & -maxpagesize) | |
4370 | != (hdr->lma & -maxpagesize)))) | |
8ded5a0f | 4371 | { |
e5654c0f AM |
4372 | /* We don't want to put a loaded section after a |
4373 | nonloaded (ie. bss style) section in the same segment | |
4374 | as that will force the non-loaded section to be loaded. | |
4375 | Consider .tbss sections as loaded for this purpose. | |
4376 | However, like the writable/non-writable case below, | |
4377 | if they are on the same page then they must be put | |
4378 | in the same segment. */ | |
8ded5a0f AM |
4379 | new_segment = TRUE; |
4380 | } | |
4381 | else if ((abfd->flags & D_PAGED) == 0) | |
4382 | { | |
4383 | /* If the file is not demand paged, which means that we | |
4384 | don't require the sections to be correctly aligned in the | |
4385 | file, then there is no other reason for a new segment. */ | |
4386 | new_segment = FALSE; | |
4387 | } | |
4388 | else if (! writable | |
4389 | && (hdr->flags & SEC_READONLY) == 0 | |
8d06853e AM |
4390 | && (((last_hdr->lma + last_size - 1) & -maxpagesize) |
4391 | != (hdr->lma & -maxpagesize))) | |
8ded5a0f AM |
4392 | { |
4393 | /* We don't want to put a writable section in a read only | |
4394 | segment, unless they are on the same page in memory | |
4395 | anyhow. We already know that the last section does not | |
4396 | bring us past the current section on the page, so the | |
4397 | only case in which the new section is not on the same | |
4398 | page as the previous section is when the previous section | |
4399 | ends precisely on a page boundary. */ | |
4400 | new_segment = TRUE; | |
4401 | } | |
4402 | else | |
4403 | { | |
4404 | /* Otherwise, we can use the same segment. */ | |
4405 | new_segment = FALSE; | |
4406 | } | |
4407 | ||
2889e75b | 4408 | /* Allow interested parties a chance to override our decision. */ |
ceae84aa AM |
4409 | if (last_hdr != NULL |
4410 | && info != NULL | |
4411 | && info->callbacks->override_segment_assignment != NULL) | |
4412 | new_segment | |
4413 | = info->callbacks->override_segment_assignment (info, abfd, hdr, | |
4414 | last_hdr, | |
4415 | new_segment); | |
2889e75b | 4416 | |
8ded5a0f AM |
4417 | if (! new_segment) |
4418 | { | |
4419 | if ((hdr->flags & SEC_READONLY) == 0) | |
4420 | writable = TRUE; | |
4421 | last_hdr = hdr; | |
4422 | /* .tbss sections effectively have zero size. */ | |
4423 | if ((hdr->flags & (SEC_THREAD_LOCAL | SEC_LOAD)) | |
4424 | != SEC_THREAD_LOCAL) | |
4425 | last_size = hdr->size; | |
4426 | else | |
4427 | last_size = 0; | |
4428 | continue; | |
4429 | } | |
4430 | ||
4431 | /* We need a new program segment. We must create a new program | |
4432 | header holding all the sections from phdr_index until hdr. */ | |
4433 | ||
4434 | m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment); | |
4435 | if (m == NULL) | |
4436 | goto error_return; | |
4437 | ||
4438 | *pm = m; | |
4439 | pm = &m->next; | |
4440 | ||
252b5132 | 4441 | if ((hdr->flags & SEC_READONLY) == 0) |
b34976b6 | 4442 | writable = TRUE; |
8ded5a0f AM |
4443 | else |
4444 | writable = FALSE; | |
4445 | ||
baaff79e JJ |
4446 | last_hdr = hdr; |
4447 | /* .tbss sections effectively have zero size. */ | |
e5caec89 | 4448 | if ((hdr->flags & (SEC_THREAD_LOCAL | SEC_LOAD)) != SEC_THREAD_LOCAL) |
eea6121a | 4449 | last_size = hdr->size; |
baaff79e JJ |
4450 | else |
4451 | last_size = 0; | |
8ded5a0f AM |
4452 | phdr_index = i; |
4453 | phdr_in_segment = FALSE; | |
252b5132 RH |
4454 | } |
4455 | ||
86b2281f AM |
4456 | /* Create a final PT_LOAD program segment, but not if it's just |
4457 | for .tbss. */ | |
4458 | if (last_hdr != NULL | |
4459 | && (i - phdr_index != 1 | |
4460 | || ((last_hdr->flags & (SEC_THREAD_LOCAL | SEC_LOAD)) | |
4461 | != SEC_THREAD_LOCAL))) | |
8ded5a0f AM |
4462 | { |
4463 | m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment); | |
4464 | if (m == NULL) | |
4465 | goto error_return; | |
252b5132 | 4466 | |
8ded5a0f AM |
4467 | *pm = m; |
4468 | pm = &m->next; | |
4469 | } | |
252b5132 | 4470 | |
8ded5a0f AM |
4471 | /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */ |
4472 | if (dynsec != NULL) | |
4473 | { | |
4474 | m = _bfd_elf_make_dynamic_segment (abfd, dynsec); | |
4475 | if (m == NULL) | |
4476 | goto error_return; | |
4477 | *pm = m; | |
4478 | pm = &m->next; | |
4479 | } | |
252b5132 | 4480 | |
1c5265b5 JJ |
4481 | /* For each batch of consecutive loadable .note sections, |
4482 | add a PT_NOTE segment. We don't use bfd_get_section_by_name, | |
4483 | because if we link together nonloadable .note sections and | |
4484 | loadable .note sections, we will generate two .note sections | |
4485 | in the output file. FIXME: Using names for section types is | |
4486 | bogus anyhow. */ | |
8ded5a0f AM |
4487 | for (s = abfd->sections; s != NULL; s = s->next) |
4488 | { | |
4489 | if ((s->flags & SEC_LOAD) != 0 | |
0112cd26 | 4490 | && CONST_STRNEQ (s->name, ".note")) |
8ded5a0f | 4491 | { |
1c5265b5 | 4492 | asection *s2; |
91d6fa6a NC |
4493 | |
4494 | count = 1; | |
8ded5a0f | 4495 | amt = sizeof (struct elf_segment_map); |
1c5265b5 JJ |
4496 | if (s->alignment_power == 2) |
4497 | for (s2 = s; s2->next != NULL; s2 = s2->next) | |
55b581a6 JJ |
4498 | { |
4499 | if (s2->next->alignment_power == 2 | |
4500 | && (s2->next->flags & SEC_LOAD) != 0 | |
4501 | && CONST_STRNEQ (s2->next->name, ".note") | |
8d06853e AM |
4502 | && align_power (s2->lma + s2->size, 2) |
4503 | == s2->next->lma) | |
55b581a6 JJ |
4504 | count++; |
4505 | else | |
4506 | break; | |
4507 | } | |
1c5265b5 | 4508 | amt += (count - 1) * sizeof (asection *); |
a50b1753 | 4509 | m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); |
8ded5a0f AM |
4510 | if (m == NULL) |
4511 | goto error_return; | |
4512 | m->next = NULL; | |
4513 | m->p_type = PT_NOTE; | |
1c5265b5 JJ |
4514 | m->count = count; |
4515 | while (count > 1) | |
4516 | { | |
4517 | m->sections[m->count - count--] = s; | |
4518 | BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0); | |
4519 | s = s->next; | |
4520 | } | |
4521 | m->sections[m->count - 1] = s; | |
4522 | BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0); | |
8ded5a0f AM |
4523 | *pm = m; |
4524 | pm = &m->next; | |
4525 | } | |
4526 | if (s->flags & SEC_THREAD_LOCAL) | |
4527 | { | |
4528 | if (! tls_count) | |
4529 | first_tls = s; | |
4530 | tls_count++; | |
4531 | } | |
4532 | } | |
252b5132 | 4533 | |
8ded5a0f AM |
4534 | /* If there are any SHF_TLS output sections, add PT_TLS segment. */ |
4535 | if (tls_count > 0) | |
4536 | { | |
8ded5a0f AM |
4537 | amt = sizeof (struct elf_segment_map); |
4538 | amt += (tls_count - 1) * sizeof (asection *); | |
a50b1753 | 4539 | m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); |
8ded5a0f AM |
4540 | if (m == NULL) |
4541 | goto error_return; | |
4542 | m->next = NULL; | |
4543 | m->p_type = PT_TLS; | |
4544 | m->count = tls_count; | |
4545 | /* Mandated PF_R. */ | |
4546 | m->p_flags = PF_R; | |
4547 | m->p_flags_valid = 1; | |
d923cae0 | 4548 | s = first_tls; |
91d6fa6a | 4549 | for (i = 0; i < (unsigned int) tls_count; ++i) |
8ded5a0f | 4550 | { |
d923cae0 L |
4551 | if ((s->flags & SEC_THREAD_LOCAL) == 0) |
4552 | { | |
4553 | _bfd_error_handler | |
4554 | (_("%B: TLS sections are not adjacent:"), abfd); | |
4555 | s = first_tls; | |
4556 | i = 0; | |
4557 | while (i < (unsigned int) tls_count) | |
4558 | { | |
4559 | if ((s->flags & SEC_THREAD_LOCAL) != 0) | |
4560 | { | |
4561 | _bfd_error_handler (_(" TLS: %A"), s); | |
4562 | i++; | |
4563 | } | |
4564 | else | |
4565 | _bfd_error_handler (_(" non-TLS: %A"), s); | |
4566 | s = s->next; | |
4567 | } | |
4568 | bfd_set_error (bfd_error_bad_value); | |
4569 | goto error_return; | |
4570 | } | |
4571 | m->sections[i] = s; | |
4572 | s = s->next; | |
8ded5a0f | 4573 | } |
252b5132 | 4574 | |
8ded5a0f AM |
4575 | *pm = m; |
4576 | pm = &m->next; | |
4577 | } | |
252b5132 | 4578 | |
8ded5a0f AM |
4579 | /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME |
4580 | segment. */ | |
12bd6957 | 4581 | eh_frame_hdr = elf_eh_frame_hdr (abfd); |
8ded5a0f AM |
4582 | if (eh_frame_hdr != NULL |
4583 | && (eh_frame_hdr->output_section->flags & SEC_LOAD) != 0) | |
252b5132 | 4584 | { |
dc810e39 | 4585 | amt = sizeof (struct elf_segment_map); |
a50b1753 | 4586 | m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); |
252b5132 RH |
4587 | if (m == NULL) |
4588 | goto error_return; | |
4589 | m->next = NULL; | |
8ded5a0f | 4590 | m->p_type = PT_GNU_EH_FRAME; |
252b5132 | 4591 | m->count = 1; |
8ded5a0f | 4592 | m->sections[0] = eh_frame_hdr->output_section; |
252b5132 RH |
4593 | |
4594 | *pm = m; | |
4595 | pm = &m->next; | |
4596 | } | |
13ae64f3 | 4597 | |
12bd6957 | 4598 | if (elf_stack_flags (abfd)) |
13ae64f3 | 4599 | { |
8ded5a0f | 4600 | amt = sizeof (struct elf_segment_map); |
a50b1753 | 4601 | m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); |
8ded5a0f AM |
4602 | if (m == NULL) |
4603 | goto error_return; | |
4604 | m->next = NULL; | |
2b05f1b7 | 4605 | m->p_type = PT_GNU_STACK; |
12bd6957 | 4606 | m->p_flags = elf_stack_flags (abfd); |
04c3a755 | 4607 | m->p_align = bed->stack_align; |
8ded5a0f | 4608 | m->p_flags_valid = 1; |
04c3a755 NS |
4609 | m->p_align_valid = m->p_align != 0; |
4610 | if (info->stacksize > 0) | |
4611 | { | |
4612 | m->p_size = info->stacksize; | |
4613 | m->p_size_valid = 1; | |
4614 | } | |
252b5132 | 4615 | |
8ded5a0f AM |
4616 | *pm = m; |
4617 | pm = &m->next; | |
4618 | } | |
65765700 | 4619 | |
ceae84aa | 4620 | if (info != NULL && info->relro) |
8ded5a0f | 4621 | { |
f210dcff L |
4622 | for (m = mfirst; m != NULL; m = m->next) |
4623 | { | |
3832a4d8 AM |
4624 | if (m->p_type == PT_LOAD |
4625 | && m->count != 0 | |
4626 | && m->sections[0]->vma >= info->relro_start | |
4627 | && m->sections[0]->vma < info->relro_end) | |
f210dcff | 4628 | { |
3832a4d8 AM |
4629 | i = m->count; |
4630 | while (--i != (unsigned) -1) | |
4631 | if ((m->sections[i]->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) | |
4632 | == (SEC_LOAD | SEC_HAS_CONTENTS)) | |
4633 | break; | |
4634 | ||
43a8475c | 4635 | if (i != (unsigned) -1) |
f210dcff L |
4636 | break; |
4637 | } | |
be01b344 | 4638 | } |
f210dcff L |
4639 | |
4640 | /* Make a PT_GNU_RELRO segment only when it isn't empty. */ | |
4641 | if (m != NULL) | |
4642 | { | |
4643 | amt = sizeof (struct elf_segment_map); | |
a50b1753 | 4644 | m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); |
f210dcff L |
4645 | if (m == NULL) |
4646 | goto error_return; | |
4647 | m->next = NULL; | |
4648 | m->p_type = PT_GNU_RELRO; | |
f210dcff L |
4649 | *pm = m; |
4650 | pm = &m->next; | |
4651 | } | |
8ded5a0f | 4652 | } |
9ee5e499 | 4653 | |
8ded5a0f | 4654 | free (sections); |
12bd6957 | 4655 | elf_seg_map (abfd) = mfirst; |
9ee5e499 JJ |
4656 | } |
4657 | ||
3dea8fca | 4658 | if (!elf_modify_segment_map (abfd, info, no_user_phdrs)) |
8ded5a0f | 4659 | return FALSE; |
8c37241b | 4660 | |
12bd6957 | 4661 | for (count = 0, m = elf_seg_map (abfd); m != NULL; m = m->next) |
8ded5a0f | 4662 | ++count; |
12bd6957 | 4663 | elf_program_header_size (abfd) = count * bed->s->sizeof_phdr; |
252b5132 | 4664 | |
b34976b6 | 4665 | return TRUE; |
252b5132 RH |
4666 | |
4667 | error_return: | |
4668 | if (sections != NULL) | |
4669 | free (sections); | |
b34976b6 | 4670 | return FALSE; |
252b5132 RH |
4671 | } |
4672 | ||
4673 | /* Sort sections by address. */ | |
4674 | ||
4675 | static int | |
217aa764 | 4676 | elf_sort_sections (const void *arg1, const void *arg2) |
252b5132 RH |
4677 | { |
4678 | const asection *sec1 = *(const asection **) arg1; | |
4679 | const asection *sec2 = *(const asection **) arg2; | |
eecdbe52 | 4680 | bfd_size_type size1, size2; |
252b5132 RH |
4681 | |
4682 | /* Sort by LMA first, since this is the address used to | |
4683 | place the section into a segment. */ | |
4684 | if (sec1->lma < sec2->lma) | |
4685 | return -1; | |
4686 | else if (sec1->lma > sec2->lma) | |
4687 | return 1; | |
4688 | ||
4689 | /* Then sort by VMA. Normally the LMA and the VMA will be | |
4690 | the same, and this will do nothing. */ | |
4691 | if (sec1->vma < sec2->vma) | |
4692 | return -1; | |
4693 | else if (sec1->vma > sec2->vma) | |
4694 | return 1; | |
4695 | ||
4696 | /* Put !SEC_LOAD sections after SEC_LOAD ones. */ | |
4697 | ||
07c6e936 | 4698 | #define TOEND(x) (((x)->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0) |
252b5132 RH |
4699 | |
4700 | if (TOEND (sec1)) | |
4701 | { | |
4702 | if (TOEND (sec2)) | |
00a7cdc5 NC |
4703 | { |
4704 | /* If the indicies are the same, do not return 0 | |
4705 | here, but continue to try the next comparison. */ | |
4706 | if (sec1->target_index - sec2->target_index != 0) | |
4707 | return sec1->target_index - sec2->target_index; | |
4708 | } | |
252b5132 RH |
4709 | else |
4710 | return 1; | |
4711 | } | |
00a7cdc5 | 4712 | else if (TOEND (sec2)) |
252b5132 RH |
4713 | return -1; |
4714 | ||
4715 | #undef TOEND | |
4716 | ||
00a7cdc5 NC |
4717 | /* Sort by size, to put zero sized sections |
4718 | before others at the same address. */ | |
252b5132 | 4719 | |
eea6121a AM |
4720 | size1 = (sec1->flags & SEC_LOAD) ? sec1->size : 0; |
4721 | size2 = (sec2->flags & SEC_LOAD) ? sec2->size : 0; | |
eecdbe52 JJ |
4722 | |
4723 | if (size1 < size2) | |
252b5132 | 4724 | return -1; |
eecdbe52 | 4725 | if (size1 > size2) |
252b5132 RH |
4726 | return 1; |
4727 | ||
4728 | return sec1->target_index - sec2->target_index; | |
4729 | } | |
4730 | ||
340b6d91 AC |
4731 | /* Ian Lance Taylor writes: |
4732 | ||
4733 | We shouldn't be using % with a negative signed number. That's just | |
4734 | not good. We have to make sure either that the number is not | |
4735 | negative, or that the number has an unsigned type. When the types | |
4736 | are all the same size they wind up as unsigned. When file_ptr is a | |
4737 | larger signed type, the arithmetic winds up as signed long long, | |
4738 | which is wrong. | |
4739 | ||
4740 | What we're trying to say here is something like ``increase OFF by | |
4741 | the least amount that will cause it to be equal to the VMA modulo | |
4742 | the page size.'' */ | |
4743 | /* In other words, something like: | |
4744 | ||
4745 | vma_offset = m->sections[0]->vma % bed->maxpagesize; | |
4746 | off_offset = off % bed->maxpagesize; | |
4747 | if (vma_offset < off_offset) | |
4748 | adjustment = vma_offset + bed->maxpagesize - off_offset; | |
4749 | else | |
4750 | adjustment = vma_offset - off_offset; | |
08a40648 | 4751 | |
340b6d91 AC |
4752 | which can can be collapsed into the expression below. */ |
4753 | ||
4754 | static file_ptr | |
4755 | vma_page_aligned_bias (bfd_vma vma, ufile_ptr off, bfd_vma maxpagesize) | |
4756 | { | |
dc9155b2 NC |
4757 | /* PR binutils/16199: Handle an alignment of zero. */ |
4758 | if (maxpagesize == 0) | |
4759 | maxpagesize = 1; | |
340b6d91 AC |
4760 | return ((vma - off) % maxpagesize); |
4761 | } | |
4762 | ||
6d33f217 L |
4763 | static void |
4764 | print_segment_map (const struct elf_segment_map *m) | |
4765 | { | |
4766 | unsigned int j; | |
4767 | const char *pt = get_segment_type (m->p_type); | |
4768 | char buf[32]; | |
4769 | ||
4770 | if (pt == NULL) | |
4771 | { | |
4772 | if (m->p_type >= PT_LOPROC && m->p_type <= PT_HIPROC) | |
4773 | sprintf (buf, "LOPROC+%7.7x", | |
4774 | (unsigned int) (m->p_type - PT_LOPROC)); | |
4775 | else if (m->p_type >= PT_LOOS && m->p_type <= PT_HIOS) | |
4776 | sprintf (buf, "LOOS+%7.7x", | |
4777 | (unsigned int) (m->p_type - PT_LOOS)); | |
4778 | else | |
4779 | snprintf (buf, sizeof (buf), "%8.8x", | |
4780 | (unsigned int) m->p_type); | |
4781 | pt = buf; | |
4782 | } | |
4a97a0e5 | 4783 | fflush (stdout); |
6d33f217 L |
4784 | fprintf (stderr, "%s:", pt); |
4785 | for (j = 0; j < m->count; j++) | |
4786 | fprintf (stderr, " %s", m->sections [j]->name); | |
4787 | putc ('\n',stderr); | |
4a97a0e5 | 4788 | fflush (stderr); |
6d33f217 L |
4789 | } |
4790 | ||
32812159 AM |
4791 | static bfd_boolean |
4792 | write_zeros (bfd *abfd, file_ptr pos, bfd_size_type len) | |
4793 | { | |
4794 | void *buf; | |
4795 | bfd_boolean ret; | |
4796 | ||
4797 | if (bfd_seek (abfd, pos, SEEK_SET) != 0) | |
4798 | return FALSE; | |
4799 | buf = bfd_zmalloc (len); | |
4800 | if (buf == NULL) | |
4801 | return FALSE; | |
4802 | ret = bfd_bwrite (buf, len, abfd) == len; | |
4803 | free (buf); | |
4804 | return ret; | |
4805 | } | |
4806 | ||
252b5132 RH |
4807 | /* Assign file positions to the sections based on the mapping from |
4808 | sections to segments. This function also sets up some fields in | |
f3520d2f | 4809 | the file header. */ |
252b5132 | 4810 | |
b34976b6 | 4811 | static bfd_boolean |
f3520d2f AM |
4812 | assign_file_positions_for_load_sections (bfd *abfd, |
4813 | struct bfd_link_info *link_info) | |
252b5132 RH |
4814 | { |
4815 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
252b5132 | 4816 | struct elf_segment_map *m; |
252b5132 | 4817 | Elf_Internal_Phdr *phdrs; |
252b5132 | 4818 | Elf_Internal_Phdr *p; |
02bf8d82 | 4819 | file_ptr off; |
3f570048 | 4820 | bfd_size_type maxpagesize; |
f3520d2f | 4821 | unsigned int alloc; |
0920dee7 | 4822 | unsigned int i, j; |
2b0bc088 | 4823 | bfd_vma header_pad = 0; |
252b5132 | 4824 | |
e36284ab | 4825 | if (link_info == NULL |
ceae84aa | 4826 | && !_bfd_elf_map_sections_to_segments (abfd, link_info)) |
8ded5a0f | 4827 | return FALSE; |
252b5132 | 4828 | |
8ded5a0f | 4829 | alloc = 0; |
12bd6957 | 4830 | for (m = elf_seg_map (abfd); m != NULL; m = m->next) |
2b0bc088 NC |
4831 | { |
4832 | ++alloc; | |
4833 | if (m->header_size) | |
4834 | header_pad = m->header_size; | |
4835 | } | |
252b5132 | 4836 | |
82f2dbf7 NC |
4837 | if (alloc) |
4838 | { | |
4839 | elf_elfheader (abfd)->e_phoff = bed->s->sizeof_ehdr; | |
4840 | elf_elfheader (abfd)->e_phentsize = bed->s->sizeof_phdr; | |
4841 | } | |
4842 | else | |
4843 | { | |
4844 | /* PR binutils/12467. */ | |
4845 | elf_elfheader (abfd)->e_phoff = 0; | |
4846 | elf_elfheader (abfd)->e_phentsize = 0; | |
4847 | } | |
d324f6d6 | 4848 | |
8ded5a0f | 4849 | elf_elfheader (abfd)->e_phnum = alloc; |
252b5132 | 4850 | |
12bd6957 AM |
4851 | if (elf_program_header_size (abfd) == (bfd_size_type) -1) |
4852 | elf_program_header_size (abfd) = alloc * bed->s->sizeof_phdr; | |
8ded5a0f | 4853 | else |
12bd6957 | 4854 | BFD_ASSERT (elf_program_header_size (abfd) |
59e0647f | 4855 | >= alloc * bed->s->sizeof_phdr); |
252b5132 RH |
4856 | |
4857 | if (alloc == 0) | |
f3520d2f | 4858 | { |
12bd6957 | 4859 | elf_next_file_pos (abfd) = bed->s->sizeof_ehdr; |
8ded5a0f | 4860 | return TRUE; |
f3520d2f | 4861 | } |
252b5132 | 4862 | |
12bd6957 | 4863 | /* We're writing the size in elf_program_header_size (abfd), |
57268894 HPN |
4864 | see assign_file_positions_except_relocs, so make sure we have |
4865 | that amount allocated, with trailing space cleared. | |
12bd6957 AM |
4866 | The variable alloc contains the computed need, while |
4867 | elf_program_header_size (abfd) contains the size used for the | |
57268894 HPN |
4868 | layout. |
4869 | See ld/emultempl/elf-generic.em:gld${EMULATION_NAME}_map_segments | |
4870 | where the layout is forced to according to a larger size in the | |
4871 | last iterations for the testcase ld-elf/header. */ | |
12bd6957 | 4872 | BFD_ASSERT (elf_program_header_size (abfd) % bed->s->sizeof_phdr |
57268894 | 4873 | == 0); |
a50b1753 NC |
4874 | phdrs = (Elf_Internal_Phdr *) |
4875 | bfd_zalloc2 (abfd, | |
12bd6957 | 4876 | (elf_program_header_size (abfd) / bed->s->sizeof_phdr), |
a50b1753 | 4877 | sizeof (Elf_Internal_Phdr)); |
f3520d2f | 4878 | elf_tdata (abfd)->phdr = phdrs; |
252b5132 | 4879 | if (phdrs == NULL) |
b34976b6 | 4880 | return FALSE; |
252b5132 | 4881 | |
3f570048 AM |
4882 | maxpagesize = 1; |
4883 | if ((abfd->flags & D_PAGED) != 0) | |
4884 | maxpagesize = bed->maxpagesize; | |
4885 | ||
252b5132 RH |
4886 | off = bed->s->sizeof_ehdr; |
4887 | off += alloc * bed->s->sizeof_phdr; | |
2b0bc088 NC |
4888 | if (header_pad < (bfd_vma) off) |
4889 | header_pad = 0; | |
4890 | else | |
4891 | header_pad -= off; | |
4892 | off += header_pad; | |
252b5132 | 4893 | |
12bd6957 | 4894 | for (m = elf_seg_map (abfd), p = phdrs, j = 0; |
252b5132 | 4895 | m != NULL; |
0920dee7 | 4896 | m = m->next, p++, j++) |
252b5132 | 4897 | { |
252b5132 | 4898 | asection **secpp; |
bf988460 AM |
4899 | bfd_vma off_adjust; |
4900 | bfd_boolean no_contents; | |
252b5132 RH |
4901 | |
4902 | /* If elf_segment_map is not from map_sections_to_segments, the | |
08a40648 | 4903 | sections may not be correctly ordered. NOTE: sorting should |
52e9b619 MS |
4904 | not be done to the PT_NOTE section of a corefile, which may |
4905 | contain several pseudo-sections artificially created by bfd. | |
4906 | Sorting these pseudo-sections breaks things badly. */ | |
47d9a591 AM |
4907 | if (m->count > 1 |
4908 | && !(elf_elfheader (abfd)->e_type == ET_CORE | |
52e9b619 | 4909 | && m->p_type == PT_NOTE)) |
252b5132 RH |
4910 | qsort (m->sections, (size_t) m->count, sizeof (asection *), |
4911 | elf_sort_sections); | |
4912 | ||
b301b248 AM |
4913 | /* An ELF segment (described by Elf_Internal_Phdr) may contain a |
4914 | number of sections with contents contributing to both p_filesz | |
4915 | and p_memsz, followed by a number of sections with no contents | |
4916 | that just contribute to p_memsz. In this loop, OFF tracks next | |
02bf8d82 | 4917 | available file offset for PT_LOAD and PT_NOTE segments. */ |
252b5132 | 4918 | p->p_type = m->p_type; |
28a7f3e7 | 4919 | p->p_flags = m->p_flags; |
252b5132 | 4920 | |
3f570048 AM |
4921 | if (m->count == 0) |
4922 | p->p_vaddr = 0; | |
4923 | else | |
3271a814 | 4924 | p->p_vaddr = m->sections[0]->vma - m->p_vaddr_offset; |
3f570048 AM |
4925 | |
4926 | if (m->p_paddr_valid) | |
4927 | p->p_paddr = m->p_paddr; | |
4928 | else if (m->count == 0) | |
4929 | p->p_paddr = 0; | |
4930 | else | |
08a40648 | 4931 | p->p_paddr = m->sections[0]->lma - m->p_vaddr_offset; |
3f570048 AM |
4932 | |
4933 | if (p->p_type == PT_LOAD | |
4934 | && (abfd->flags & D_PAGED) != 0) | |
4935 | { | |
4936 | /* p_align in demand paged PT_LOAD segments effectively stores | |
4937 | the maximum page size. When copying an executable with | |
4938 | objcopy, we set m->p_align from the input file. Use this | |
4939 | value for maxpagesize rather than bed->maxpagesize, which | |
4940 | may be different. Note that we use maxpagesize for PT_TLS | |
4941 | segment alignment later in this function, so we are relying | |
4942 | on at least one PT_LOAD segment appearing before a PT_TLS | |
4943 | segment. */ | |
4944 | if (m->p_align_valid) | |
4945 | maxpagesize = m->p_align; | |
4946 | ||
4947 | p->p_align = maxpagesize; | |
4948 | } | |
3271a814 NS |
4949 | else if (m->p_align_valid) |
4950 | p->p_align = m->p_align; | |
e970b90a DJ |
4951 | else if (m->count == 0) |
4952 | p->p_align = 1 << bed->s->log_file_align; | |
3f570048 AM |
4953 | else |
4954 | p->p_align = 0; | |
4955 | ||
bf988460 AM |
4956 | no_contents = FALSE; |
4957 | off_adjust = 0; | |
252b5132 | 4958 | if (p->p_type == PT_LOAD |
b301b248 | 4959 | && m->count > 0) |
252b5132 | 4960 | { |
b301b248 | 4961 | bfd_size_type align; |
a49e53ed | 4962 | unsigned int align_power = 0; |
b301b248 | 4963 | |
3271a814 NS |
4964 | if (m->p_align_valid) |
4965 | align = p->p_align; | |
4966 | else | |
252b5132 | 4967 | { |
3271a814 NS |
4968 | for (i = 0, secpp = m->sections; i < m->count; i++, secpp++) |
4969 | { | |
4970 | unsigned int secalign; | |
08a40648 | 4971 | |
3271a814 NS |
4972 | secalign = bfd_get_section_alignment (abfd, *secpp); |
4973 | if (secalign > align_power) | |
4974 | align_power = secalign; | |
4975 | } | |
4976 | align = (bfd_size_type) 1 << align_power; | |
4977 | if (align < maxpagesize) | |
4978 | align = maxpagesize; | |
b301b248 | 4979 | } |
252b5132 | 4980 | |
02bf8d82 AM |
4981 | for (i = 0; i < m->count; i++) |
4982 | if ((m->sections[i]->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0) | |
4983 | /* If we aren't making room for this section, then | |
4984 | it must be SHT_NOBITS regardless of what we've | |
4985 | set via struct bfd_elf_special_section. */ | |
4986 | elf_section_type (m->sections[i]) = SHT_NOBITS; | |
4987 | ||
bf988460 | 4988 | /* Find out whether this segment contains any loadable |
aea274d3 AM |
4989 | sections. */ |
4990 | no_contents = TRUE; | |
4991 | for (i = 0; i < m->count; i++) | |
4992 | if (elf_section_type (m->sections[i]) != SHT_NOBITS) | |
4993 | { | |
4994 | no_contents = FALSE; | |
4995 | break; | |
4996 | } | |
bf988460 | 4997 | |
85cfcbfb | 4998 | off_adjust = vma_page_aligned_bias (p->p_vaddr, off, align); |
bf988460 AM |
4999 | off += off_adjust; |
5000 | if (no_contents) | |
5001 | { | |
5002 | /* We shouldn't need to align the segment on disk since | |
5003 | the segment doesn't need file space, but the gABI | |
5004 | arguably requires the alignment and glibc ld.so | |
5005 | checks it. So to comply with the alignment | |
5006 | requirement but not waste file space, we adjust | |
5007 | p_offset for just this segment. (OFF_ADJUST is | |
5008 | subtracted from OFF later.) This may put p_offset | |
5009 | past the end of file, but that shouldn't matter. */ | |
5010 | } | |
5011 | else | |
5012 | off_adjust = 0; | |
252b5132 | 5013 | } |
b1a6d0b1 NC |
5014 | /* Make sure the .dynamic section is the first section in the |
5015 | PT_DYNAMIC segment. */ | |
5016 | else if (p->p_type == PT_DYNAMIC | |
5017 | && m->count > 1 | |
5018 | && strcmp (m->sections[0]->name, ".dynamic") != 0) | |
5019 | { | |
5020 | _bfd_error_handler | |
b301b248 AM |
5021 | (_("%B: The first section in the PT_DYNAMIC segment is not the .dynamic section"), |
5022 | abfd); | |
b1a6d0b1 NC |
5023 | bfd_set_error (bfd_error_bad_value); |
5024 | return FALSE; | |
5025 | } | |
3f001e84 JK |
5026 | /* Set the note section type to SHT_NOTE. */ |
5027 | else if (p->p_type == PT_NOTE) | |
5028 | for (i = 0; i < m->count; i++) | |
5029 | elf_section_type (m->sections[i]) = SHT_NOTE; | |
252b5132 | 5030 | |
252b5132 RH |
5031 | p->p_offset = 0; |
5032 | p->p_filesz = 0; | |
5033 | p->p_memsz = 0; | |
5034 | ||
5035 | if (m->includes_filehdr) | |
5036 | { | |
bf988460 | 5037 | if (!m->p_flags_valid) |
252b5132 | 5038 | p->p_flags |= PF_R; |
252b5132 RH |
5039 | p->p_filesz = bed->s->sizeof_ehdr; |
5040 | p->p_memsz = bed->s->sizeof_ehdr; | |
5041 | if (m->count > 0) | |
5042 | { | |
252b5132 RH |
5043 | if (p->p_vaddr < (bfd_vma) off) |
5044 | { | |
caf47ea6 | 5045 | (*_bfd_error_handler) |
b301b248 AM |
5046 | (_("%B: Not enough room for program headers, try linking with -N"), |
5047 | abfd); | |
252b5132 | 5048 | bfd_set_error (bfd_error_bad_value); |
b34976b6 | 5049 | return FALSE; |
252b5132 RH |
5050 | } |
5051 | ||
5052 | p->p_vaddr -= off; | |
bf988460 | 5053 | if (!m->p_paddr_valid) |
252b5132 RH |
5054 | p->p_paddr -= off; |
5055 | } | |
252b5132 RH |
5056 | } |
5057 | ||
5058 | if (m->includes_phdrs) | |
5059 | { | |
bf988460 | 5060 | if (!m->p_flags_valid) |
252b5132 RH |
5061 | p->p_flags |= PF_R; |
5062 | ||
f3520d2f | 5063 | if (!m->includes_filehdr) |
252b5132 RH |
5064 | { |
5065 | p->p_offset = bed->s->sizeof_ehdr; | |
5066 | ||
5067 | if (m->count > 0) | |
5068 | { | |
252b5132 | 5069 | p->p_vaddr -= off - p->p_offset; |
bf988460 | 5070 | if (!m->p_paddr_valid) |
252b5132 RH |
5071 | p->p_paddr -= off - p->p_offset; |
5072 | } | |
252b5132 RH |
5073 | } |
5074 | ||
5075 | p->p_filesz += alloc * bed->s->sizeof_phdr; | |
5076 | p->p_memsz += alloc * bed->s->sizeof_phdr; | |
2b0bc088 NC |
5077 | if (m->count) |
5078 | { | |
5079 | p->p_filesz += header_pad; | |
5080 | p->p_memsz += header_pad; | |
5081 | } | |
252b5132 RH |
5082 | } |
5083 | ||
5084 | if (p->p_type == PT_LOAD | |
5085 | || (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core)) | |
5086 | { | |
bf988460 | 5087 | if (!m->includes_filehdr && !m->includes_phdrs) |
02bf8d82 | 5088 | p->p_offset = off; |
252b5132 RH |
5089 | else |
5090 | { | |
5091 | file_ptr adjust; | |
5092 | ||
5093 | adjust = off - (p->p_offset + p->p_filesz); | |
bf988460 AM |
5094 | if (!no_contents) |
5095 | p->p_filesz += adjust; | |
252b5132 RH |
5096 | p->p_memsz += adjust; |
5097 | } | |
5098 | } | |
5099 | ||
1ea63fd2 AM |
5100 | /* Set up p_filesz, p_memsz, p_align and p_flags from the section |
5101 | maps. Set filepos for sections in PT_LOAD segments, and in | |
5102 | core files, for sections in PT_NOTE segments. | |
5103 | assign_file_positions_for_non_load_sections will set filepos | |
5104 | for other sections and update p_filesz for other segments. */ | |
252b5132 RH |
5105 | for (i = 0, secpp = m->sections; i < m->count; i++, secpp++) |
5106 | { | |
5107 | asection *sec; | |
252b5132 | 5108 | bfd_size_type align; |
627b32bc | 5109 | Elf_Internal_Shdr *this_hdr; |
252b5132 RH |
5110 | |
5111 | sec = *secpp; | |
02bf8d82 | 5112 | this_hdr = &elf_section_data (sec)->this_hdr; |
3f570048 | 5113 | align = (bfd_size_type) 1 << bfd_get_section_alignment (abfd, sec); |
252b5132 | 5114 | |
88967714 AM |
5115 | if ((p->p_type == PT_LOAD |
5116 | || p->p_type == PT_TLS) | |
5117 | && (this_hdr->sh_type != SHT_NOBITS | |
5118 | || ((this_hdr->sh_flags & SHF_ALLOC) != 0 | |
5119 | && ((this_hdr->sh_flags & SHF_TLS) == 0 | |
5120 | || p->p_type == PT_TLS)))) | |
252b5132 | 5121 | { |
b5599592 AM |
5122 | bfd_vma p_start = p->p_paddr; |
5123 | bfd_vma p_end = p_start + p->p_memsz; | |
5124 | bfd_vma s_start = sec->lma; | |
5125 | bfd_vma adjust = s_start - p_end; | |
252b5132 | 5126 | |
a2d1e028 L |
5127 | if (adjust != 0 |
5128 | && (s_start < p_end | |
5129 | || p_end < p_start)) | |
252b5132 | 5130 | { |
88967714 | 5131 | (*_bfd_error_handler) |
b5599592 AM |
5132 | (_("%B: section %A lma %#lx adjusted to %#lx"), abfd, sec, |
5133 | (unsigned long) s_start, (unsigned long) p_end); | |
88967714 | 5134 | adjust = 0; |
b5599592 | 5135 | sec->lma = p_end; |
1cfb7d1e | 5136 | } |
3ac9b6c9 | 5137 | p->p_memsz += adjust; |
1cfb7d1e | 5138 | |
88967714 AM |
5139 | if (this_hdr->sh_type != SHT_NOBITS) |
5140 | { | |
32812159 AM |
5141 | if (p->p_filesz + adjust < p->p_memsz) |
5142 | { | |
5143 | /* We have a PROGBITS section following NOBITS ones. | |
5144 | Allocate file space for the NOBITS section(s) and | |
5145 | zero it. */ | |
5146 | adjust = p->p_memsz - p->p_filesz; | |
5147 | if (!write_zeros (abfd, off, adjust)) | |
5148 | return FALSE; | |
5149 | } | |
88967714 AM |
5150 | off += adjust; |
5151 | p->p_filesz += adjust; | |
252b5132 | 5152 | } |
252b5132 RH |
5153 | } |
5154 | ||
5155 | if (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core) | |
5156 | { | |
b301b248 AM |
5157 | /* The section at i == 0 is the one that actually contains |
5158 | everything. */ | |
4a938328 MS |
5159 | if (i == 0) |
5160 | { | |
627b32bc | 5161 | this_hdr->sh_offset = sec->filepos = off; |
6a3cd2b4 AM |
5162 | off += this_hdr->sh_size; |
5163 | p->p_filesz = this_hdr->sh_size; | |
b301b248 AM |
5164 | p->p_memsz = 0; |
5165 | p->p_align = 1; | |
252b5132 | 5166 | } |
4a938328 | 5167 | else |
252b5132 | 5168 | { |
b301b248 | 5169 | /* The rest are fake sections that shouldn't be written. */ |
252b5132 | 5170 | sec->filepos = 0; |
eea6121a | 5171 | sec->size = 0; |
b301b248 AM |
5172 | sec->flags = 0; |
5173 | continue; | |
252b5132 | 5174 | } |
252b5132 RH |
5175 | } |
5176 | else | |
5177 | { | |
1e951488 | 5178 | if (p->p_type == PT_LOAD) |
b301b248 | 5179 | { |
1e951488 AM |
5180 | this_hdr->sh_offset = sec->filepos = off; |
5181 | if (this_hdr->sh_type != SHT_NOBITS) | |
5182 | off += this_hdr->sh_size; | |
5183 | } | |
5184 | else if (this_hdr->sh_type == SHT_NOBITS | |
5185 | && (this_hdr->sh_flags & SHF_TLS) != 0 | |
5186 | && this_hdr->sh_offset == 0) | |
5187 | { | |
5188 | /* This is a .tbss section that didn't get a PT_LOAD. | |
5189 | (See _bfd_elf_map_sections_to_segments "Create a | |
5190 | final PT_LOAD".) Set sh_offset to the value it | |
5191 | would have if we had created a zero p_filesz and | |
5192 | p_memsz PT_LOAD header for the section. This | |
5193 | also makes the PT_TLS header have the same | |
5194 | p_offset value. */ | |
5195 | bfd_vma adjust = vma_page_aligned_bias (this_hdr->sh_addr, | |
5196 | off, align); | |
5197 | this_hdr->sh_offset = sec->filepos = off + adjust; | |
b301b248 | 5198 | } |
252b5132 | 5199 | |
02bf8d82 | 5200 | if (this_hdr->sh_type != SHT_NOBITS) |
b301b248 | 5201 | { |
6a3cd2b4 | 5202 | p->p_filesz += this_hdr->sh_size; |
02bf8d82 AM |
5203 | /* A load section without SHF_ALLOC is something like |
5204 | a note section in a PT_NOTE segment. These take | |
5205 | file space but are not loaded into memory. */ | |
5206 | if ((this_hdr->sh_flags & SHF_ALLOC) != 0) | |
6a3cd2b4 | 5207 | p->p_memsz += this_hdr->sh_size; |
b301b248 | 5208 | } |
6a3cd2b4 | 5209 | else if ((this_hdr->sh_flags & SHF_ALLOC) != 0) |
13ae64f3 | 5210 | { |
6a3cd2b4 AM |
5211 | if (p->p_type == PT_TLS) |
5212 | p->p_memsz += this_hdr->sh_size; | |
5213 | ||
5214 | /* .tbss is special. It doesn't contribute to p_memsz of | |
5215 | normal segments. */ | |
5216 | else if ((this_hdr->sh_flags & SHF_TLS) == 0) | |
5217 | p->p_memsz += this_hdr->sh_size; | |
13ae64f3 JJ |
5218 | } |
5219 | ||
b10a8ae0 L |
5220 | if (align > p->p_align |
5221 | && !m->p_align_valid | |
5222 | && (p->p_type != PT_LOAD | |
5223 | || (abfd->flags & D_PAGED) == 0)) | |
252b5132 RH |
5224 | p->p_align = align; |
5225 | } | |
5226 | ||
bf988460 | 5227 | if (!m->p_flags_valid) |
252b5132 RH |
5228 | { |
5229 | p->p_flags |= PF_R; | |
02bf8d82 | 5230 | if ((this_hdr->sh_flags & SHF_EXECINSTR) != 0) |
252b5132 | 5231 | p->p_flags |= PF_X; |
02bf8d82 | 5232 | if ((this_hdr->sh_flags & SHF_WRITE) != 0) |
252b5132 RH |
5233 | p->p_flags |= PF_W; |
5234 | } | |
5235 | } | |
43a8475c | 5236 | |
bf988460 | 5237 | off -= off_adjust; |
0920dee7 | 5238 | |
7c928300 AM |
5239 | /* Check that all sections are in a PT_LOAD segment. |
5240 | Don't check funky gdb generated core files. */ | |
5241 | if (p->p_type == PT_LOAD && bfd_get_format (abfd) != bfd_core) | |
9a83a553 AM |
5242 | { |
5243 | bfd_boolean check_vma = TRUE; | |
5244 | ||
5245 | for (i = 1; i < m->count; i++) | |
5246 | if (m->sections[i]->vma == m->sections[i - 1]->vma | |
5247 | && ELF_SECTION_SIZE (&(elf_section_data (m->sections[i]) | |
5248 | ->this_hdr), p) != 0 | |
5249 | && ELF_SECTION_SIZE (&(elf_section_data (m->sections[i - 1]) | |
5250 | ->this_hdr), p) != 0) | |
0920dee7 | 5251 | { |
9a83a553 AM |
5252 | /* Looks like we have overlays packed into the segment. */ |
5253 | check_vma = FALSE; | |
5254 | break; | |
0920dee7 | 5255 | } |
9a83a553 AM |
5256 | |
5257 | for (i = 0; i < m->count; i++) | |
5258 | { | |
5259 | Elf_Internal_Shdr *this_hdr; | |
5260 | asection *sec; | |
5261 | ||
5262 | sec = m->sections[i]; | |
5263 | this_hdr = &(elf_section_data(sec)->this_hdr); | |
86b2281f AM |
5264 | if (!ELF_SECTION_IN_SEGMENT_1 (this_hdr, p, check_vma, 0) |
5265 | && !ELF_TBSS_SPECIAL (this_hdr, p)) | |
9a83a553 AM |
5266 | { |
5267 | (*_bfd_error_handler) | |
5268 | (_("%B: section `%A' can't be allocated in segment %d"), | |
5269 | abfd, sec, j); | |
5270 | print_segment_map (m); | |
5271 | } | |
5272 | } | |
5273 | } | |
252b5132 RH |
5274 | } |
5275 | ||
12bd6957 | 5276 | elf_next_file_pos (abfd) = off; |
f3520d2f AM |
5277 | return TRUE; |
5278 | } | |
5279 | ||
5280 | /* Assign file positions for the other sections. */ | |
5281 | ||
5282 | static bfd_boolean | |
5283 | assign_file_positions_for_non_load_sections (bfd *abfd, | |
5284 | struct bfd_link_info *link_info) | |
5285 | { | |
5286 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
5287 | Elf_Internal_Shdr **i_shdrpp; | |
e06efbf1 | 5288 | Elf_Internal_Shdr **hdrpp, **end_hdrpp; |
f3520d2f AM |
5289 | Elf_Internal_Phdr *phdrs; |
5290 | Elf_Internal_Phdr *p; | |
5291 | struct elf_segment_map *m; | |
62655c7b | 5292 | struct elf_segment_map *hdrs_segment; |
f3520d2f AM |
5293 | bfd_vma filehdr_vaddr, filehdr_paddr; |
5294 | bfd_vma phdrs_vaddr, phdrs_paddr; | |
5295 | file_ptr off; | |
f3520d2f AM |
5296 | unsigned int count; |
5297 | ||
5c182d5f | 5298 | i_shdrpp = elf_elfsections (abfd); |
e06efbf1 | 5299 | end_hdrpp = i_shdrpp + elf_numsections (abfd); |
12bd6957 | 5300 | off = elf_next_file_pos (abfd); |
e06efbf1 | 5301 | for (hdrpp = i_shdrpp + 1; hdrpp < end_hdrpp; hdrpp++) |
5c182d5f | 5302 | { |
5c182d5f AM |
5303 | Elf_Internal_Shdr *hdr; |
5304 | ||
5305 | hdr = *hdrpp; | |
5306 | if (hdr->bfd_section != NULL | |
252e386e AM |
5307 | && (hdr->bfd_section->filepos != 0 |
5308 | || (hdr->sh_type == SHT_NOBITS | |
5309 | && hdr->contents == NULL))) | |
627b32bc | 5310 | BFD_ASSERT (hdr->sh_offset == hdr->bfd_section->filepos); |
5c182d5f AM |
5311 | else if ((hdr->sh_flags & SHF_ALLOC) != 0) |
5312 | { | |
e8d2ba53 AM |
5313 | if (hdr->sh_size != 0) |
5314 | (*_bfd_error_handler) | |
5315 | (_("%B: warning: allocated section `%s' not in segment"), | |
5316 | abfd, | |
5317 | (hdr->bfd_section == NULL | |
5318 | ? "*unknown*" | |
5319 | : hdr->bfd_section->name)); | |
3ba71138 L |
5320 | /* We don't need to page align empty sections. */ |
5321 | if ((abfd->flags & D_PAGED) != 0 && hdr->sh_size != 0) | |
5c182d5f AM |
5322 | off += vma_page_aligned_bias (hdr->sh_addr, off, |
5323 | bed->maxpagesize); | |
5324 | else | |
5325 | off += vma_page_aligned_bias (hdr->sh_addr, off, | |
5326 | hdr->sh_addralign); | |
5327 | off = _bfd_elf_assign_file_position_for_section (hdr, off, | |
5328 | FALSE); | |
5329 | } | |
5330 | else if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA) | |
5331 | && hdr->bfd_section == NULL) | |
0ce398f1 L |
5332 | || (hdr->bfd_section != NULL |
5333 | && (hdr->bfd_section->flags & SEC_ELF_COMPRESS)) | |
5334 | /* Compress DWARF debug sections. */ | |
12bd6957 | 5335 | || hdr == i_shdrpp[elf_onesymtab (abfd)] |
6a40cf0c NC |
5336 | || (elf_symtab_shndx_list (abfd) != NULL |
5337 | && hdr == i_shdrpp[elf_symtab_shndx_list (abfd)->ndx]) | |
3e19fb8f L |
5338 | || hdr == i_shdrpp[elf_strtab_sec (abfd)] |
5339 | || hdr == i_shdrpp[elf_shstrtab_sec (abfd)]) | |
5c182d5f AM |
5340 | hdr->sh_offset = -1; |
5341 | else | |
5342 | off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE); | |
5c182d5f AM |
5343 | } |
5344 | ||
252b5132 RH |
5345 | /* Now that we have set the section file positions, we can set up |
5346 | the file positions for the non PT_LOAD segments. */ | |
f3520d2f AM |
5347 | count = 0; |
5348 | filehdr_vaddr = 0; | |
5349 | filehdr_paddr = 0; | |
5350 | phdrs_vaddr = bed->maxpagesize + bed->s->sizeof_ehdr; | |
5351 | phdrs_paddr = 0; | |
62655c7b | 5352 | hdrs_segment = NULL; |
f3520d2f | 5353 | phdrs = elf_tdata (abfd)->phdr; |
12bd6957 | 5354 | for (m = elf_seg_map (abfd), p = phdrs; m != NULL; m = m->next, p++) |
f3520d2f AM |
5355 | { |
5356 | ++count; | |
5357 | if (p->p_type != PT_LOAD) | |
5358 | continue; | |
5359 | ||
5360 | if (m->includes_filehdr) | |
5361 | { | |
5362 | filehdr_vaddr = p->p_vaddr; | |
5363 | filehdr_paddr = p->p_paddr; | |
5364 | } | |
5365 | if (m->includes_phdrs) | |
5366 | { | |
5367 | phdrs_vaddr = p->p_vaddr; | |
5368 | phdrs_paddr = p->p_paddr; | |
5369 | if (m->includes_filehdr) | |
5370 | { | |
62655c7b | 5371 | hdrs_segment = m; |
f3520d2f AM |
5372 | phdrs_vaddr += bed->s->sizeof_ehdr; |
5373 | phdrs_paddr += bed->s->sizeof_ehdr; | |
5374 | } | |
5375 | } | |
5376 | } | |
5377 | ||
62655c7b RM |
5378 | if (hdrs_segment != NULL && link_info != NULL) |
5379 | { | |
5380 | /* There is a segment that contains both the file headers and the | |
5381 | program headers, so provide a symbol __ehdr_start pointing there. | |
5382 | A program can use this to examine itself robustly. */ | |
5383 | ||
5384 | struct elf_link_hash_entry *hash | |
5385 | = elf_link_hash_lookup (elf_hash_table (link_info), "__ehdr_start", | |
5386 | FALSE, FALSE, TRUE); | |
5387 | /* If the symbol was referenced and not defined, define it. */ | |
5388 | if (hash != NULL | |
5389 | && (hash->root.type == bfd_link_hash_new | |
5390 | || hash->root.type == bfd_link_hash_undefined | |
5391 | || hash->root.type == bfd_link_hash_undefweak | |
5392 | || hash->root.type == bfd_link_hash_common)) | |
5393 | { | |
5394 | asection *s = NULL; | |
5395 | if (hdrs_segment->count != 0) | |
5396 | /* The segment contains sections, so use the first one. */ | |
5397 | s = hdrs_segment->sections[0]; | |
5398 | else | |
5399 | /* Use the first (i.e. lowest-addressed) section in any segment. */ | |
12bd6957 | 5400 | for (m = elf_seg_map (abfd); m != NULL; m = m->next) |
62655c7b RM |
5401 | if (m->count != 0) |
5402 | { | |
5403 | s = m->sections[0]; | |
5404 | break; | |
5405 | } | |
5406 | ||
5407 | if (s != NULL) | |
5408 | { | |
5409 | hash->root.u.def.value = filehdr_vaddr - s->vma; | |
5410 | hash->root.u.def.section = s; | |
5411 | } | |
5412 | else | |
5413 | { | |
5414 | hash->root.u.def.value = filehdr_vaddr; | |
5415 | hash->root.u.def.section = bfd_abs_section_ptr; | |
5416 | } | |
5417 | ||
5418 | hash->root.type = bfd_link_hash_defined; | |
5419 | hash->def_regular = 1; | |
5420 | hash->non_elf = 0; | |
5421 | } | |
5422 | } | |
5423 | ||
12bd6957 | 5424 | for (m = elf_seg_map (abfd), p = phdrs; m != NULL; m = m->next, p++) |
252b5132 | 5425 | { |
129af99f | 5426 | if (p->p_type == PT_GNU_RELRO) |
252b5132 | 5427 | { |
b84a33b5 | 5428 | const Elf_Internal_Phdr *lp; |
3146fac4 | 5429 | struct elf_segment_map *lm; |
1ea63fd2 | 5430 | |
129af99f | 5431 | if (link_info != NULL) |
8c37241b | 5432 | { |
129af99f AS |
5433 | /* During linking the range of the RELRO segment is passed |
5434 | in link_info. */ | |
12bd6957 | 5435 | for (lm = elf_seg_map (abfd), lp = phdrs; |
3146fac4 AM |
5436 | lm != NULL; |
5437 | lm = lm->next, lp++) | |
8c37241b JJ |
5438 | { |
5439 | if (lp->p_type == PT_LOAD | |
b84a33b5 | 5440 | && lp->p_vaddr < link_info->relro_end |
3146fac4 AM |
5441 | && lm->count != 0 |
5442 | && lm->sections[0]->vma >= link_info->relro_start) | |
8c37241b JJ |
5443 | break; |
5444 | } | |
8981c88a | 5445 | |
3146fac4 | 5446 | BFD_ASSERT (lm != NULL); |
8c37241b | 5447 | } |
129af99f AS |
5448 | else |
5449 | { | |
5450 | /* Otherwise we are copying an executable or shared | |
b84a33b5 | 5451 | library, but we need to use the same linker logic. */ |
129af99f AS |
5452 | for (lp = phdrs; lp < phdrs + count; ++lp) |
5453 | { | |
5454 | if (lp->p_type == PT_LOAD | |
5455 | && lp->p_paddr == p->p_paddr) | |
5456 | break; | |
5457 | } | |
b84a33b5 AM |
5458 | } |
5459 | ||
5460 | if (lp < phdrs + count) | |
5461 | { | |
5462 | p->p_vaddr = lp->p_vaddr; | |
5463 | p->p_paddr = lp->p_paddr; | |
5464 | p->p_offset = lp->p_offset; | |
5465 | if (link_info != NULL) | |
5466 | p->p_filesz = link_info->relro_end - lp->p_vaddr; | |
5467 | else if (m->p_size_valid) | |
5468 | p->p_filesz = m->p_size; | |
129af99f AS |
5469 | else |
5470 | abort (); | |
b84a33b5 | 5471 | p->p_memsz = p->p_filesz; |
f3944f72 L |
5472 | /* Preserve the alignment and flags if they are valid. The |
5473 | gold linker generates RW/4 for the PT_GNU_RELRO section. | |
5474 | It is better for objcopy/strip to honor these attributes | |
5475 | otherwise gdb will choke when using separate debug files. | |
5476 | */ | |
5477 | if (!m->p_align_valid) | |
5478 | p->p_align = 1; | |
5479 | if (!m->p_flags_valid) | |
341b8ee7 | 5480 | p->p_flags = PF_R; |
129af99f | 5481 | } |
9433b9b1 | 5482 | else |
b84a33b5 AM |
5483 | { |
5484 | memset (p, 0, sizeof *p); | |
5485 | p->p_type = PT_NULL; | |
5486 | } | |
129af99f | 5487 | } |
04c3a755 NS |
5488 | else if (p->p_type == PT_GNU_STACK) |
5489 | { | |
5490 | if (m->p_size_valid) | |
5491 | p->p_memsz = m->p_size; | |
5492 | } | |
129af99f AS |
5493 | else if (m->count != 0) |
5494 | { | |
e06efbf1 | 5495 | unsigned int i; |
129af99f AS |
5496 | if (p->p_type != PT_LOAD |
5497 | && (p->p_type != PT_NOTE | |
5498 | || bfd_get_format (abfd) != bfd_core)) | |
5499 | { | |
c86934ce NC |
5500 | if (m->includes_filehdr || m->includes_phdrs) |
5501 | { | |
b1fa9dd6 | 5502 | /* PR 17512: file: 2195325e. */ |
c86934ce NC |
5503 | (*_bfd_error_handler) |
5504 | (_("%B: warning: non-load segment includes file header and/or program header"), | |
5505 | abfd); | |
5506 | return FALSE; | |
5507 | } | |
129af99f | 5508 | |
86b2281f | 5509 | p->p_filesz = 0; |
129af99f | 5510 | p->p_offset = m->sections[0]->filepos; |
86b2281f AM |
5511 | for (i = m->count; i-- != 0;) |
5512 | { | |
5513 | asection *sect = m->sections[i]; | |
5514 | Elf_Internal_Shdr *hdr = &elf_section_data (sect)->this_hdr; | |
5515 | if (hdr->sh_type != SHT_NOBITS) | |
5516 | { | |
5517 | p->p_filesz = (sect->filepos - m->sections[0]->filepos | |
5518 | + hdr->sh_size); | |
5519 | break; | |
5520 | } | |
5521 | } | |
129af99f AS |
5522 | } |
5523 | } | |
5524 | else if (m->includes_filehdr) | |
5525 | { | |
5526 | p->p_vaddr = filehdr_vaddr; | |
5527 | if (! m->p_paddr_valid) | |
5528 | p->p_paddr = filehdr_paddr; | |
5529 | } | |
5530 | else if (m->includes_phdrs) | |
5531 | { | |
5532 | p->p_vaddr = phdrs_vaddr; | |
5533 | if (! m->p_paddr_valid) | |
5534 | p->p_paddr = phdrs_paddr; | |
252b5132 RH |
5535 | } |
5536 | } | |
5537 | ||
12bd6957 | 5538 | elf_next_file_pos (abfd) = off; |
252b5132 | 5539 | |
b34976b6 | 5540 | return TRUE; |
252b5132 RH |
5541 | } |
5542 | ||
6a40cf0c NC |
5543 | static elf_section_list * |
5544 | find_section_in_list (unsigned int i, elf_section_list * list) | |
5545 | { | |
5546 | for (;list != NULL; list = list->next) | |
5547 | if (list->ndx == i) | |
5548 | break; | |
5549 | return list; | |
5550 | } | |
5551 | ||
252b5132 RH |
5552 | /* Work out the file positions of all the sections. This is called by |
5553 | _bfd_elf_compute_section_file_positions. All the section sizes and | |
5554 | VMAs must be known before this is called. | |
5555 | ||
e0638f70 AM |
5556 | Reloc sections come in two flavours: Those processed specially as |
5557 | "side-channel" data attached to a section to which they apply, and | |
5558 | those that bfd doesn't process as relocations. The latter sort are | |
5559 | stored in a normal bfd section by bfd_section_from_shdr. We don't | |
5560 | consider the former sort here, unless they form part of the loadable | |
5561 | image. Reloc sections not assigned here will be handled later by | |
5562 | assign_file_positions_for_relocs. | |
252b5132 RH |
5563 | |
5564 | We also don't set the positions of the .symtab and .strtab here. */ | |
5565 | ||
b34976b6 | 5566 | static bfd_boolean |
c84fca4d AO |
5567 | assign_file_positions_except_relocs (bfd *abfd, |
5568 | struct bfd_link_info *link_info) | |
252b5132 | 5569 | { |
5c182d5f AM |
5570 | struct elf_obj_tdata *tdata = elf_tdata (abfd); |
5571 | Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd); | |
9c5bfbb7 | 5572 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
252b5132 RH |
5573 | |
5574 | if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0 | |
5575 | && bfd_get_format (abfd) != bfd_core) | |
5576 | { | |
5c182d5f AM |
5577 | Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd); |
5578 | unsigned int num_sec = elf_numsections (abfd); | |
252b5132 RH |
5579 | Elf_Internal_Shdr **hdrpp; |
5580 | unsigned int i; | |
a485e98e | 5581 | file_ptr off; |
252b5132 RH |
5582 | |
5583 | /* Start after the ELF header. */ | |
5584 | off = i_ehdrp->e_ehsize; | |
5585 | ||
5586 | /* We are not creating an executable, which means that we are | |
5587 | not creating a program header, and that the actual order of | |
5588 | the sections in the file is unimportant. */ | |
9ad5cbcf | 5589 | for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++) |
252b5132 RH |
5590 | { |
5591 | Elf_Internal_Shdr *hdr; | |
5592 | ||
5593 | hdr = *hdrpp; | |
e0638f70 AM |
5594 | if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA) |
5595 | && hdr->bfd_section == NULL) | |
0ce398f1 L |
5596 | || (hdr->bfd_section != NULL |
5597 | && (hdr->bfd_section->flags & SEC_ELF_COMPRESS)) | |
5598 | /* Compress DWARF debug sections. */ | |
12bd6957 | 5599 | || i == elf_onesymtab (abfd) |
6a40cf0c NC |
5600 | || (elf_symtab_shndx_list (abfd) != NULL |
5601 | && hdr == i_shdrpp[elf_symtab_shndx_list (abfd)->ndx]) | |
3e19fb8f L |
5602 | || i == elf_strtab_sec (abfd) |
5603 | || i == elf_shstrtab_sec (abfd)) | |
252b5132 RH |
5604 | { |
5605 | hdr->sh_offset = -1; | |
252b5132 | 5606 | } |
9ad5cbcf | 5607 | else |
b34976b6 | 5608 | off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE); |
252b5132 | 5609 | } |
a485e98e AM |
5610 | |
5611 | elf_next_file_pos (abfd) = off; | |
252b5132 RH |
5612 | } |
5613 | else | |
5614 | { | |
f3520d2f AM |
5615 | unsigned int alloc; |
5616 | ||
252b5132 | 5617 | /* Assign file positions for the loaded sections based on the |
08a40648 | 5618 | assignment of sections to segments. */ |
f3520d2f AM |
5619 | if (!assign_file_positions_for_load_sections (abfd, link_info)) |
5620 | return FALSE; | |
5621 | ||
5622 | /* And for non-load sections. */ | |
5623 | if (!assign_file_positions_for_non_load_sections (abfd, link_info)) | |
5624 | return FALSE; | |
5625 | ||
e36284ab AM |
5626 | if (bed->elf_backend_modify_program_headers != NULL) |
5627 | { | |
5628 | if (!(*bed->elf_backend_modify_program_headers) (abfd, link_info)) | |
5629 | return FALSE; | |
5630 | } | |
5631 | ||
58e7ebac | 5632 | /* Set e_type in ELF header to ET_EXEC for -pie -Ttext-segment=. */ |
0e1862bb | 5633 | if (link_info != NULL && bfd_link_pie (link_info)) |
58e7ebac L |
5634 | { |
5635 | unsigned int num_segments = elf_elfheader (abfd)->e_phnum; | |
5636 | Elf_Internal_Phdr *segment = elf_tdata (abfd)->phdr; | |
5637 | Elf_Internal_Phdr *end_segment = &segment[num_segments]; | |
5638 | ||
5639 | /* Find the lowest p_vaddr in PT_LOAD segments. */ | |
5640 | bfd_vma p_vaddr = (bfd_vma) -1; | |
5641 | for (; segment < end_segment; segment++) | |
5642 | if (segment->p_type == PT_LOAD && p_vaddr > segment->p_vaddr) | |
5643 | p_vaddr = segment->p_vaddr; | |
5644 | ||
5645 | /* Set e_type to ET_EXEC if the lowest p_vaddr in PT_LOAD | |
5646 | segments is non-zero. */ | |
5647 | if (p_vaddr) | |
5648 | i_ehdrp->e_type = ET_EXEC; | |
5649 | } | |
5650 | ||
f3520d2f | 5651 | /* Write out the program headers. */ |
12bd6957 | 5652 | alloc = elf_program_header_size (abfd) / bed->s->sizeof_phdr; |
f3520d2f AM |
5653 | if (bfd_seek (abfd, (bfd_signed_vma) bed->s->sizeof_ehdr, SEEK_SET) != 0 |
5654 | || bed->s->write_out_phdrs (abfd, tdata->phdr, alloc) != 0) | |
b34976b6 | 5655 | return FALSE; |
252b5132 RH |
5656 | } |
5657 | ||
b34976b6 | 5658 | return TRUE; |
252b5132 RH |
5659 | } |
5660 | ||
b34976b6 | 5661 | static bfd_boolean |
217aa764 | 5662 | prep_headers (bfd *abfd) |
252b5132 | 5663 | { |
3d540e93 | 5664 | Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form. */ |
2b0f7ef9 | 5665 | struct elf_strtab_hash *shstrtab; |
9c5bfbb7 | 5666 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
252b5132 RH |
5667 | |
5668 | i_ehdrp = elf_elfheader (abfd); | |
252b5132 | 5669 | |
2b0f7ef9 | 5670 | shstrtab = _bfd_elf_strtab_init (); |
252b5132 | 5671 | if (shstrtab == NULL) |
b34976b6 | 5672 | return FALSE; |
252b5132 RH |
5673 | |
5674 | elf_shstrtab (abfd) = shstrtab; | |
5675 | ||
5676 | i_ehdrp->e_ident[EI_MAG0] = ELFMAG0; | |
5677 | i_ehdrp->e_ident[EI_MAG1] = ELFMAG1; | |
5678 | i_ehdrp->e_ident[EI_MAG2] = ELFMAG2; | |
5679 | i_ehdrp->e_ident[EI_MAG3] = ELFMAG3; | |
5680 | ||
5681 | i_ehdrp->e_ident[EI_CLASS] = bed->s->elfclass; | |
5682 | i_ehdrp->e_ident[EI_DATA] = | |
5683 | bfd_big_endian (abfd) ? ELFDATA2MSB : ELFDATA2LSB; | |
5684 | i_ehdrp->e_ident[EI_VERSION] = bed->s->ev_current; | |
5685 | ||
252b5132 RH |
5686 | if ((abfd->flags & DYNAMIC) != 0) |
5687 | i_ehdrp->e_type = ET_DYN; | |
5688 | else if ((abfd->flags & EXEC_P) != 0) | |
5689 | i_ehdrp->e_type = ET_EXEC; | |
5690 | else if (bfd_get_format (abfd) == bfd_core) | |
5691 | i_ehdrp->e_type = ET_CORE; | |
5692 | else | |
5693 | i_ehdrp->e_type = ET_REL; | |
5694 | ||
5695 | switch (bfd_get_arch (abfd)) | |
5696 | { | |
5697 | case bfd_arch_unknown: | |
5698 | i_ehdrp->e_machine = EM_NONE; | |
5699 | break; | |
aa4f99bb AO |
5700 | |
5701 | /* There used to be a long list of cases here, each one setting | |
5702 | e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE | |
5703 | in the corresponding bfd definition. To avoid duplication, | |
5704 | the switch was removed. Machines that need special handling | |
5705 | can generally do it in elf_backend_final_write_processing(), | |
5706 | unless they need the information earlier than the final write. | |
5707 | Such need can generally be supplied by replacing the tests for | |
5708 | e_machine with the conditions used to determine it. */ | |
252b5132 | 5709 | default: |
9c5bfbb7 AM |
5710 | i_ehdrp->e_machine = bed->elf_machine_code; |
5711 | } | |
aa4f99bb | 5712 | |
252b5132 RH |
5713 | i_ehdrp->e_version = bed->s->ev_current; |
5714 | i_ehdrp->e_ehsize = bed->s->sizeof_ehdr; | |
5715 | ||
c044fabd | 5716 | /* No program header, for now. */ |
252b5132 RH |
5717 | i_ehdrp->e_phoff = 0; |
5718 | i_ehdrp->e_phentsize = 0; | |
5719 | i_ehdrp->e_phnum = 0; | |
5720 | ||
c044fabd | 5721 | /* Each bfd section is section header entry. */ |
252b5132 RH |
5722 | i_ehdrp->e_entry = bfd_get_start_address (abfd); |
5723 | i_ehdrp->e_shentsize = bed->s->sizeof_shdr; | |
5724 | ||
c044fabd | 5725 | /* If we're building an executable, we'll need a program header table. */ |
252b5132 | 5726 | if (abfd->flags & EXEC_P) |
0e71e495 BE |
5727 | /* It all happens later. */ |
5728 | ; | |
252b5132 RH |
5729 | else |
5730 | { | |
5731 | i_ehdrp->e_phentsize = 0; | |
252b5132 RH |
5732 | i_ehdrp->e_phoff = 0; |
5733 | } | |
5734 | ||
5735 | elf_tdata (abfd)->symtab_hdr.sh_name = | |
b34976b6 | 5736 | (unsigned int) _bfd_elf_strtab_add (shstrtab, ".symtab", FALSE); |
252b5132 | 5737 | elf_tdata (abfd)->strtab_hdr.sh_name = |
b34976b6 | 5738 | (unsigned int) _bfd_elf_strtab_add (shstrtab, ".strtab", FALSE); |
252b5132 | 5739 | elf_tdata (abfd)->shstrtab_hdr.sh_name = |
b34976b6 | 5740 | (unsigned int) _bfd_elf_strtab_add (shstrtab, ".shstrtab", FALSE); |
252b5132 | 5741 | if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1 |
17ca87fc | 5742 | || elf_tdata (abfd)->strtab_hdr.sh_name == (unsigned int) -1 |
252b5132 | 5743 | || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1) |
b34976b6 | 5744 | return FALSE; |
252b5132 | 5745 | |
b34976b6 | 5746 | return TRUE; |
252b5132 RH |
5747 | } |
5748 | ||
5749 | /* Assign file positions for all the reloc sections which are not part | |
a485e98e | 5750 | of the loadable file image, and the file position of section headers. */ |
252b5132 | 5751 | |
0ce398f1 L |
5752 | static bfd_boolean |
5753 | _bfd_elf_assign_file_positions_for_non_load (bfd *abfd) | |
252b5132 RH |
5754 | { |
5755 | file_ptr off; | |
e06efbf1 | 5756 | Elf_Internal_Shdr **shdrpp, **end_shdrpp; |
3e19fb8f | 5757 | Elf_Internal_Shdr *shdrp; |
a485e98e AM |
5758 | Elf_Internal_Ehdr *i_ehdrp; |
5759 | const struct elf_backend_data *bed; | |
252b5132 | 5760 | |
12bd6957 | 5761 | off = elf_next_file_pos (abfd); |
252b5132 | 5762 | |
e06efbf1 L |
5763 | shdrpp = elf_elfsections (abfd); |
5764 | end_shdrpp = shdrpp + elf_numsections (abfd); | |
5765 | for (shdrpp++; shdrpp < end_shdrpp; shdrpp++) | |
252b5132 | 5766 | { |
252b5132 | 5767 | shdrp = *shdrpp; |
0ce398f1 L |
5768 | if (shdrp->sh_offset == -1) |
5769 | { | |
3e19fb8f | 5770 | asection *sec = shdrp->bfd_section; |
0ce398f1 L |
5771 | bfd_boolean is_rel = (shdrp->sh_type == SHT_REL |
5772 | || shdrp->sh_type == SHT_RELA); | |
5773 | if (is_rel | |
3e19fb8f | 5774 | || (sec != NULL && (sec->flags & SEC_ELF_COMPRESS))) |
0ce398f1 L |
5775 | { |
5776 | if (!is_rel) | |
5777 | { | |
3e19fb8f L |
5778 | const char *name = sec->name; |
5779 | struct bfd_elf_section_data *d; | |
5780 | ||
0ce398f1 | 5781 | /* Compress DWARF debug sections. */ |
3e19fb8f | 5782 | if (!bfd_compress_section (abfd, sec, |
0ce398f1 L |
5783 | shdrp->contents)) |
5784 | return FALSE; | |
3e19fb8f L |
5785 | |
5786 | if (sec->compress_status == COMPRESS_SECTION_DONE | |
5787 | && (abfd->flags & BFD_COMPRESS_GABI) == 0) | |
5788 | { | |
5789 | /* If section is compressed with zlib-gnu, convert | |
5790 | section name from .debug_* to .zdebug_*. */ | |
5791 | char *new_name | |
5792 | = convert_debug_to_zdebug (abfd, name); | |
5793 | if (new_name == NULL) | |
5794 | return FALSE; | |
5795 | name = new_name; | |
5796 | } | |
5797 | /* Add setion name to section name section. */ | |
5798 | if (shdrp->sh_name != (unsigned int) -1) | |
5799 | abort (); | |
5800 | shdrp->sh_name | |
5801 | = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), | |
5802 | name, FALSE); | |
5803 | d = elf_section_data (sec); | |
5804 | ||
5805 | /* Add reloc setion name to section name section. */ | |
5806 | if (d->rel.hdr | |
5807 | && !_bfd_elf_set_reloc_sh_name (abfd, | |
5808 | d->rel.hdr, | |
5809 | name, FALSE)) | |
5810 | return FALSE; | |
5811 | if (d->rela.hdr | |
5812 | && !_bfd_elf_set_reloc_sh_name (abfd, | |
5813 | d->rela.hdr, | |
91cb26da | 5814 | name, TRUE)) |
3e19fb8f L |
5815 | return FALSE; |
5816 | ||
0ce398f1 | 5817 | /* Update section size and contents. */ |
3e19fb8f L |
5818 | shdrp->sh_size = sec->size; |
5819 | shdrp->contents = sec->contents; | |
0ce398f1 L |
5820 | shdrp->bfd_section->contents = NULL; |
5821 | } | |
5822 | off = _bfd_elf_assign_file_position_for_section (shdrp, | |
5823 | off, | |
5824 | TRUE); | |
5825 | } | |
5826 | } | |
252b5132 RH |
5827 | } |
5828 | ||
3e19fb8f L |
5829 | /* Place section name section after DWARF debug sections have been |
5830 | compressed. */ | |
5831 | _bfd_elf_strtab_finalize (elf_shstrtab (abfd)); | |
5832 | shdrp = &elf_tdata (abfd)->shstrtab_hdr; | |
5833 | shdrp->sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd)); | |
5834 | off = _bfd_elf_assign_file_position_for_section (shdrp, off, TRUE); | |
5835 | ||
5836 | /* Place the section headers. */ | |
a485e98e AM |
5837 | i_ehdrp = elf_elfheader (abfd); |
5838 | bed = get_elf_backend_data (abfd); | |
5839 | off = align_file_position (off, 1 << bed->s->log_file_align); | |
5840 | i_ehdrp->e_shoff = off; | |
5841 | off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize; | |
12bd6957 | 5842 | elf_next_file_pos (abfd) = off; |
0ce398f1 L |
5843 | |
5844 | return TRUE; | |
252b5132 RH |
5845 | } |
5846 | ||
b34976b6 | 5847 | bfd_boolean |
217aa764 | 5848 | _bfd_elf_write_object_contents (bfd *abfd) |
252b5132 | 5849 | { |
9c5bfbb7 | 5850 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
252b5132 | 5851 | Elf_Internal_Shdr **i_shdrp; |
b34976b6 | 5852 | bfd_boolean failed; |
9ad5cbcf | 5853 | unsigned int count, num_sec; |
30e8ee25 | 5854 | struct elf_obj_tdata *t; |
252b5132 RH |
5855 | |
5856 | if (! abfd->output_has_begun | |
217aa764 | 5857 | && ! _bfd_elf_compute_section_file_positions (abfd, NULL)) |
b34976b6 | 5858 | return FALSE; |
252b5132 RH |
5859 | |
5860 | i_shdrp = elf_elfsections (abfd); | |
252b5132 | 5861 | |
b34976b6 | 5862 | failed = FALSE; |
252b5132 RH |
5863 | bfd_map_over_sections (abfd, bed->s->write_relocs, &failed); |
5864 | if (failed) | |
b34976b6 | 5865 | return FALSE; |
252b5132 | 5866 | |
0ce398f1 L |
5867 | if (!_bfd_elf_assign_file_positions_for_non_load (abfd)) |
5868 | return FALSE; | |
252b5132 | 5869 | |
c044fabd | 5870 | /* After writing the headers, we need to write the sections too... */ |
9ad5cbcf AM |
5871 | num_sec = elf_numsections (abfd); |
5872 | for (count = 1; count < num_sec; count++) | |
252b5132 | 5873 | { |
3e19fb8f L |
5874 | i_shdrp[count]->sh_name |
5875 | = _bfd_elf_strtab_offset (elf_shstrtab (abfd), | |
5876 | i_shdrp[count]->sh_name); | |
252b5132 RH |
5877 | if (bed->elf_backend_section_processing) |
5878 | (*bed->elf_backend_section_processing) (abfd, i_shdrp[count]); | |
5879 | if (i_shdrp[count]->contents) | |
5880 | { | |
dc810e39 AM |
5881 | bfd_size_type amt = i_shdrp[count]->sh_size; |
5882 | ||
252b5132 | 5883 | if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0 |
dc810e39 | 5884 | || bfd_bwrite (i_shdrp[count]->contents, amt, abfd) != amt) |
b34976b6 | 5885 | return FALSE; |
252b5132 RH |
5886 | } |
5887 | } | |
5888 | ||
5889 | /* Write out the section header names. */ | |
30e8ee25 | 5890 | t = elf_tdata (abfd); |
26ae6d5e | 5891 | if (elf_shstrtab (abfd) != NULL |
30e8ee25 | 5892 | && (bfd_seek (abfd, t->shstrtab_hdr.sh_offset, SEEK_SET) != 0 |
08a40648 | 5893 | || !_bfd_elf_strtab_emit (abfd, elf_shstrtab (abfd)))) |
b34976b6 | 5894 | return FALSE; |
252b5132 RH |
5895 | |
5896 | if (bed->elf_backend_final_write_processing) | |
12bd6957 | 5897 | (*bed->elf_backend_final_write_processing) (abfd, elf_linker (abfd)); |
252b5132 | 5898 | |
ff59fc36 RM |
5899 | if (!bed->s->write_shdrs_and_ehdr (abfd)) |
5900 | return FALSE; | |
5901 | ||
5902 | /* This is last since write_shdrs_and_ehdr can touch i_shdrp[0]. */ | |
c0355132 AM |
5903 | if (t->o->build_id.after_write_object_contents != NULL) |
5904 | return (*t->o->build_id.after_write_object_contents) (abfd); | |
ff59fc36 RM |
5905 | |
5906 | return TRUE; | |
252b5132 RH |
5907 | } |
5908 | ||
b34976b6 | 5909 | bfd_boolean |
217aa764 | 5910 | _bfd_elf_write_corefile_contents (bfd *abfd) |
252b5132 | 5911 | { |
c044fabd | 5912 | /* Hopefully this can be done just like an object file. */ |
252b5132 RH |
5913 | return _bfd_elf_write_object_contents (abfd); |
5914 | } | |
c044fabd KH |
5915 | |
5916 | /* Given a section, search the header to find them. */ | |
5917 | ||
cb33740c | 5918 | unsigned int |
198beae2 | 5919 | _bfd_elf_section_from_bfd_section (bfd *abfd, struct bfd_section *asect) |
252b5132 | 5920 | { |
9c5bfbb7 | 5921 | const struct elf_backend_data *bed; |
91d6fa6a | 5922 | unsigned int sec_index; |
252b5132 | 5923 | |
9ad5cbcf AM |
5924 | if (elf_section_data (asect) != NULL |
5925 | && elf_section_data (asect)->this_idx != 0) | |
5926 | return elf_section_data (asect)->this_idx; | |
5927 | ||
5928 | if (bfd_is_abs_section (asect)) | |
91d6fa6a | 5929 | sec_index = SHN_ABS; |
af746e92 | 5930 | else if (bfd_is_com_section (asect)) |
91d6fa6a | 5931 | sec_index = SHN_COMMON; |
af746e92 | 5932 | else if (bfd_is_und_section (asect)) |
91d6fa6a | 5933 | sec_index = SHN_UNDEF; |
af746e92 | 5934 | else |
91d6fa6a | 5935 | sec_index = SHN_BAD; |
252b5132 | 5936 | |
af746e92 | 5937 | bed = get_elf_backend_data (abfd); |
252b5132 RH |
5938 | if (bed->elf_backend_section_from_bfd_section) |
5939 | { | |
91d6fa6a | 5940 | int retval = sec_index; |
9ad5cbcf | 5941 | |
af746e92 AM |
5942 | if ((*bed->elf_backend_section_from_bfd_section) (abfd, asect, &retval)) |
5943 | return retval; | |
252b5132 RH |
5944 | } |
5945 | ||
91d6fa6a | 5946 | if (sec_index == SHN_BAD) |
af746e92 | 5947 | bfd_set_error (bfd_error_nonrepresentable_section); |
252b5132 | 5948 | |
91d6fa6a | 5949 | return sec_index; |
252b5132 RH |
5950 | } |
5951 | ||
5952 | /* Given a BFD symbol, return the index in the ELF symbol table, or -1 | |
5953 | on error. */ | |
5954 | ||
5955 | int | |
217aa764 | 5956 | _bfd_elf_symbol_from_bfd_symbol (bfd *abfd, asymbol **asym_ptr_ptr) |
252b5132 RH |
5957 | { |
5958 | asymbol *asym_ptr = *asym_ptr_ptr; | |
5959 | int idx; | |
5960 | flagword flags = asym_ptr->flags; | |
5961 | ||
5962 | /* When gas creates relocations against local labels, it creates its | |
5963 | own symbol for the section, but does put the symbol into the | |
5964 | symbol chain, so udata is 0. When the linker is generating | |
5965 | relocatable output, this section symbol may be for one of the | |
5966 | input sections rather than the output section. */ | |
5967 | if (asym_ptr->udata.i == 0 | |
5968 | && (flags & BSF_SECTION_SYM) | |
5969 | && asym_ptr->section) | |
5970 | { | |
5372391b | 5971 | asection *sec; |
252b5132 RH |
5972 | int indx; |
5973 | ||
5372391b AM |
5974 | sec = asym_ptr->section; |
5975 | if (sec->owner != abfd && sec->output_section != NULL) | |
5976 | sec = sec->output_section; | |
5977 | if (sec->owner == abfd | |
5978 | && (indx = sec->index) < elf_num_section_syms (abfd) | |
4e89ac30 | 5979 | && elf_section_syms (abfd)[indx] != NULL) |
252b5132 RH |
5980 | asym_ptr->udata.i = elf_section_syms (abfd)[indx]->udata.i; |
5981 | } | |
5982 | ||
5983 | idx = asym_ptr->udata.i; | |
5984 | ||
5985 | if (idx == 0) | |
5986 | { | |
5987 | /* This case can occur when using --strip-symbol on a symbol | |
08a40648 | 5988 | which is used in a relocation entry. */ |
252b5132 | 5989 | (*_bfd_error_handler) |
d003868e AM |
5990 | (_("%B: symbol `%s' required but not present"), |
5991 | abfd, bfd_asymbol_name (asym_ptr)); | |
252b5132 RH |
5992 | bfd_set_error (bfd_error_no_symbols); |
5993 | return -1; | |
5994 | } | |
5995 | ||
5996 | #if DEBUG & 4 | |
5997 | { | |
5998 | fprintf (stderr, | |
9ccb8af9 AM |
5999 | "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8lx\n", |
6000 | (long) asym_ptr, asym_ptr->name, idx, (long) flags); | |
252b5132 RH |
6001 | fflush (stderr); |
6002 | } | |
6003 | #endif | |
6004 | ||
6005 | return idx; | |
6006 | } | |
6007 | ||
84d1d650 | 6008 | /* Rewrite program header information. */ |
252b5132 | 6009 | |
b34976b6 | 6010 | static bfd_boolean |
84d1d650 | 6011 | rewrite_elf_program_header (bfd *ibfd, bfd *obfd) |
252b5132 | 6012 | { |
b34976b6 AM |
6013 | Elf_Internal_Ehdr *iehdr; |
6014 | struct elf_segment_map *map; | |
6015 | struct elf_segment_map *map_first; | |
6016 | struct elf_segment_map **pointer_to_map; | |
6017 | Elf_Internal_Phdr *segment; | |
6018 | asection *section; | |
6019 | unsigned int i; | |
6020 | unsigned int num_segments; | |
6021 | bfd_boolean phdr_included = FALSE; | |
5c44b38e | 6022 | bfd_boolean p_paddr_valid; |
b34976b6 AM |
6023 | bfd_vma maxpagesize; |
6024 | struct elf_segment_map *phdr_adjust_seg = NULL; | |
6025 | unsigned int phdr_adjust_num = 0; | |
9c5bfbb7 | 6026 | const struct elf_backend_data *bed; |
bc67d8a6 | 6027 | |
caf47ea6 | 6028 | bed = get_elf_backend_data (ibfd); |
252b5132 RH |
6029 | iehdr = elf_elfheader (ibfd); |
6030 | ||
bc67d8a6 | 6031 | map_first = NULL; |
c044fabd | 6032 | pointer_to_map = &map_first; |
252b5132 RH |
6033 | |
6034 | num_segments = elf_elfheader (ibfd)->e_phnum; | |
bc67d8a6 NC |
6035 | maxpagesize = get_elf_backend_data (obfd)->maxpagesize; |
6036 | ||
6037 | /* Returns the end address of the segment + 1. */ | |
aecc8f8a AM |
6038 | #define SEGMENT_END(segment, start) \ |
6039 | (start + (segment->p_memsz > segment->p_filesz \ | |
6040 | ? segment->p_memsz : segment->p_filesz)) | |
bc67d8a6 | 6041 | |
eecdbe52 JJ |
6042 | #define SECTION_SIZE(section, segment) \ |
6043 | (((section->flags & (SEC_HAS_CONTENTS | SEC_THREAD_LOCAL)) \ | |
6044 | != SEC_THREAD_LOCAL || segment->p_type == PT_TLS) \ | |
eea6121a | 6045 | ? section->size : 0) |
eecdbe52 | 6046 | |
b34976b6 | 6047 | /* Returns TRUE if the given section is contained within |
bc67d8a6 | 6048 | the given segment. VMA addresses are compared. */ |
aecc8f8a AM |
6049 | #define IS_CONTAINED_BY_VMA(section, segment) \ |
6050 | (section->vma >= segment->p_vaddr \ | |
eecdbe52 | 6051 | && (section->vma + SECTION_SIZE (section, segment) \ |
aecc8f8a | 6052 | <= (SEGMENT_END (segment, segment->p_vaddr)))) |
c044fabd | 6053 | |
b34976b6 | 6054 | /* Returns TRUE if the given section is contained within |
bc67d8a6 | 6055 | the given segment. LMA addresses are compared. */ |
aecc8f8a AM |
6056 | #define IS_CONTAINED_BY_LMA(section, segment, base) \ |
6057 | (section->lma >= base \ | |
eecdbe52 | 6058 | && (section->lma + SECTION_SIZE (section, segment) \ |
aecc8f8a | 6059 | <= SEGMENT_END (segment, base))) |
252b5132 | 6060 | |
0efc80c8 L |
6061 | /* Handle PT_NOTE segment. */ |
6062 | #define IS_NOTE(p, s) \ | |
aecc8f8a | 6063 | (p->p_type == PT_NOTE \ |
0efc80c8 | 6064 | && elf_section_type (s) == SHT_NOTE \ |
aecc8f8a | 6065 | && (bfd_vma) s->filepos >= p->p_offset \ |
cb3ff1e5 | 6066 | && ((bfd_vma) s->filepos + s->size \ |
aecc8f8a | 6067 | <= p->p_offset + p->p_filesz)) |
252b5132 | 6068 | |
0efc80c8 L |
6069 | /* Special case: corefile "NOTE" section containing regs, prpsinfo |
6070 | etc. */ | |
6071 | #define IS_COREFILE_NOTE(p, s) \ | |
6072 | (IS_NOTE (p, s) \ | |
6073 | && bfd_get_format (ibfd) == bfd_core \ | |
6074 | && s->vma == 0 \ | |
6075 | && s->lma == 0) | |
6076 | ||
252b5132 RH |
6077 | /* The complicated case when p_vaddr is 0 is to handle the Solaris |
6078 | linker, which generates a PT_INTERP section with p_vaddr and | |
6079 | p_memsz set to 0. */ | |
aecc8f8a AM |
6080 | #define IS_SOLARIS_PT_INTERP(p, s) \ |
6081 | (p->p_vaddr == 0 \ | |
6082 | && p->p_paddr == 0 \ | |
6083 | && p->p_memsz == 0 \ | |
6084 | && p->p_filesz > 0 \ | |
6085 | && (s->flags & SEC_HAS_CONTENTS) != 0 \ | |
eea6121a | 6086 | && s->size > 0 \ |
aecc8f8a | 6087 | && (bfd_vma) s->filepos >= p->p_offset \ |
cb3ff1e5 | 6088 | && ((bfd_vma) s->filepos + s->size \ |
aecc8f8a | 6089 | <= p->p_offset + p->p_filesz)) |
5c440b1e | 6090 | |
bc67d8a6 NC |
6091 | /* Decide if the given section should be included in the given segment. |
6092 | A section will be included if: | |
f5ffc919 | 6093 | 1. It is within the address space of the segment -- we use the LMA |
08a40648 | 6094 | if that is set for the segment and the VMA otherwise, |
0efc80c8 | 6095 | 2. It is an allocated section or a NOTE section in a PT_NOTE |
d324f6d6 | 6096 | segment. |
bc67d8a6 | 6097 | 3. There is an output section associated with it, |
eecdbe52 | 6098 | 4. The section has not already been allocated to a previous segment. |
2b05f1b7 | 6099 | 5. PT_GNU_STACK segments do not include any sections. |
03394ac9 | 6100 | 6. PT_TLS segment includes only SHF_TLS sections. |
6f79b219 JJ |
6101 | 7. SHF_TLS sections are only in PT_TLS or PT_LOAD segments. |
6102 | 8. PT_DYNAMIC should not contain empty sections at the beginning | |
08a40648 | 6103 | (with the possible exception of .dynamic). */ |
9f17e2a6 | 6104 | #define IS_SECTION_IN_INPUT_SEGMENT(section, segment, bed) \ |
2b05f1b7 L |
6105 | ((((segment->p_paddr \ |
6106 | ? IS_CONTAINED_BY_LMA (section, segment, segment->p_paddr) \ | |
6107 | : IS_CONTAINED_BY_VMA (section, segment)) \ | |
6108 | && (section->flags & SEC_ALLOC) != 0) \ | |
0efc80c8 | 6109 | || IS_NOTE (segment, section)) \ |
2b05f1b7 L |
6110 | && segment->p_type != PT_GNU_STACK \ |
6111 | && (segment->p_type != PT_TLS \ | |
6112 | || (section->flags & SEC_THREAD_LOCAL)) \ | |
6113 | && (segment->p_type == PT_LOAD \ | |
6114 | || segment->p_type == PT_TLS \ | |
6115 | || (section->flags & SEC_THREAD_LOCAL) == 0) \ | |
6116 | && (segment->p_type != PT_DYNAMIC \ | |
6117 | || SECTION_SIZE (section, segment) > 0 \ | |
6118 | || (segment->p_paddr \ | |
6119 | ? segment->p_paddr != section->lma \ | |
6120 | : segment->p_vaddr != section->vma) \ | |
6121 | || (strcmp (bfd_get_section_name (ibfd, section), ".dynamic") \ | |
6122 | == 0)) \ | |
0067a569 | 6123 | && !section->segment_mark) |
bc67d8a6 | 6124 | |
9f17e2a6 L |
6125 | /* If the output section of a section in the input segment is NULL, |
6126 | it is removed from the corresponding output segment. */ | |
6127 | #define INCLUDE_SECTION_IN_SEGMENT(section, segment, bed) \ | |
6128 | (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed) \ | |
6129 | && section->output_section != NULL) | |
6130 | ||
b34976b6 | 6131 | /* Returns TRUE iff seg1 starts after the end of seg2. */ |
b5f852ea NC |
6132 | #define SEGMENT_AFTER_SEGMENT(seg1, seg2, field) \ |
6133 | (seg1->field >= SEGMENT_END (seg2, seg2->field)) | |
6134 | ||
6135 | /* Returns TRUE iff seg1 and seg2 overlap. Segments overlap iff both | |
6136 | their VMA address ranges and their LMA address ranges overlap. | |
6137 | It is possible to have overlapping VMA ranges without overlapping LMA | |
6138 | ranges. RedBoot images for example can have both .data and .bss mapped | |
6139 | to the same VMA range, but with the .data section mapped to a different | |
6140 | LMA. */ | |
aecc8f8a | 6141 | #define SEGMENT_OVERLAPS(seg1, seg2) \ |
b5f852ea | 6142 | ( !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_vaddr) \ |
08a40648 | 6143 | || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_vaddr)) \ |
b5f852ea | 6144 | && !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_paddr) \ |
08a40648 | 6145 | || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_paddr))) |
bc67d8a6 NC |
6146 | |
6147 | /* Initialise the segment mark field. */ | |
6148 | for (section = ibfd->sections; section != NULL; section = section->next) | |
b34976b6 | 6149 | section->segment_mark = FALSE; |
bc67d8a6 | 6150 | |
5c44b38e AM |
6151 | /* The Solaris linker creates program headers in which all the |
6152 | p_paddr fields are zero. When we try to objcopy or strip such a | |
6153 | file, we get confused. Check for this case, and if we find it | |
6154 | don't set the p_paddr_valid fields. */ | |
6155 | p_paddr_valid = FALSE; | |
6156 | for (i = 0, segment = elf_tdata (ibfd)->phdr; | |
6157 | i < num_segments; | |
6158 | i++, segment++) | |
6159 | if (segment->p_paddr != 0) | |
6160 | { | |
6161 | p_paddr_valid = TRUE; | |
6162 | break; | |
6163 | } | |
6164 | ||
252b5132 | 6165 | /* Scan through the segments specified in the program header |
bc67d8a6 | 6166 | of the input BFD. For this first scan we look for overlaps |
9ad5cbcf | 6167 | in the loadable segments. These can be created by weird |
aecc8f8a | 6168 | parameters to objcopy. Also, fix some solaris weirdness. */ |
bc67d8a6 NC |
6169 | for (i = 0, segment = elf_tdata (ibfd)->phdr; |
6170 | i < num_segments; | |
c044fabd | 6171 | i++, segment++) |
252b5132 | 6172 | { |
252b5132 | 6173 | unsigned int j; |
c044fabd | 6174 | Elf_Internal_Phdr *segment2; |
252b5132 | 6175 | |
aecc8f8a AM |
6176 | if (segment->p_type == PT_INTERP) |
6177 | for (section = ibfd->sections; section; section = section->next) | |
6178 | if (IS_SOLARIS_PT_INTERP (segment, section)) | |
6179 | { | |
6180 | /* Mininal change so that the normal section to segment | |
4cc11e76 | 6181 | assignment code will work. */ |
aecc8f8a AM |
6182 | segment->p_vaddr = section->vma; |
6183 | break; | |
6184 | } | |
6185 | ||
bc67d8a6 | 6186 | if (segment->p_type != PT_LOAD) |
b10a8ae0 L |
6187 | { |
6188 | /* Remove PT_GNU_RELRO segment. */ | |
6189 | if (segment->p_type == PT_GNU_RELRO) | |
6190 | segment->p_type = PT_NULL; | |
6191 | continue; | |
6192 | } | |
c044fabd | 6193 | |
bc67d8a6 | 6194 | /* Determine if this segment overlaps any previous segments. */ |
0067a569 | 6195 | for (j = 0, segment2 = elf_tdata (ibfd)->phdr; j < i; j++, segment2++) |
bc67d8a6 NC |
6196 | { |
6197 | bfd_signed_vma extra_length; | |
c044fabd | 6198 | |
bc67d8a6 | 6199 | if (segment2->p_type != PT_LOAD |
0067a569 | 6200 | || !SEGMENT_OVERLAPS (segment, segment2)) |
bc67d8a6 | 6201 | continue; |
c044fabd | 6202 | |
bc67d8a6 NC |
6203 | /* Merge the two segments together. */ |
6204 | if (segment2->p_vaddr < segment->p_vaddr) | |
6205 | { | |
c044fabd | 6206 | /* Extend SEGMENT2 to include SEGMENT and then delete |
08a40648 | 6207 | SEGMENT. */ |
0067a569 AM |
6208 | extra_length = (SEGMENT_END (segment, segment->p_vaddr) |
6209 | - SEGMENT_END (segment2, segment2->p_vaddr)); | |
c044fabd | 6210 | |
bc67d8a6 NC |
6211 | if (extra_length > 0) |
6212 | { | |
0067a569 | 6213 | segment2->p_memsz += extra_length; |
bc67d8a6 NC |
6214 | segment2->p_filesz += extra_length; |
6215 | } | |
c044fabd | 6216 | |
bc67d8a6 | 6217 | segment->p_type = PT_NULL; |
c044fabd | 6218 | |
bc67d8a6 NC |
6219 | /* Since we have deleted P we must restart the outer loop. */ |
6220 | i = 0; | |
6221 | segment = elf_tdata (ibfd)->phdr; | |
6222 | break; | |
6223 | } | |
6224 | else | |
6225 | { | |
c044fabd | 6226 | /* Extend SEGMENT to include SEGMENT2 and then delete |
08a40648 | 6227 | SEGMENT2. */ |
0067a569 AM |
6228 | extra_length = (SEGMENT_END (segment2, segment2->p_vaddr) |
6229 | - SEGMENT_END (segment, segment->p_vaddr)); | |
c044fabd | 6230 | |
bc67d8a6 NC |
6231 | if (extra_length > 0) |
6232 | { | |
0067a569 | 6233 | segment->p_memsz += extra_length; |
bc67d8a6 NC |
6234 | segment->p_filesz += extra_length; |
6235 | } | |
c044fabd | 6236 | |
bc67d8a6 NC |
6237 | segment2->p_type = PT_NULL; |
6238 | } | |
6239 | } | |
6240 | } | |
c044fabd | 6241 | |
bc67d8a6 NC |
6242 | /* The second scan attempts to assign sections to segments. */ |
6243 | for (i = 0, segment = elf_tdata (ibfd)->phdr; | |
6244 | i < num_segments; | |
0067a569 | 6245 | i++, segment++) |
bc67d8a6 | 6246 | { |
0067a569 AM |
6247 | unsigned int section_count; |
6248 | asection **sections; | |
6249 | asection *output_section; | |
6250 | unsigned int isec; | |
6251 | bfd_vma matching_lma; | |
6252 | bfd_vma suggested_lma; | |
6253 | unsigned int j; | |
dc810e39 | 6254 | bfd_size_type amt; |
0067a569 AM |
6255 | asection *first_section; |
6256 | bfd_boolean first_matching_lma; | |
6257 | bfd_boolean first_suggested_lma; | |
bc67d8a6 NC |
6258 | |
6259 | if (segment->p_type == PT_NULL) | |
6260 | continue; | |
c044fabd | 6261 | |
9f17e2a6 | 6262 | first_section = NULL; |
bc67d8a6 | 6263 | /* Compute how many sections might be placed into this segment. */ |
b5f852ea NC |
6264 | for (section = ibfd->sections, section_count = 0; |
6265 | section != NULL; | |
6266 | section = section->next) | |
9f17e2a6 L |
6267 | { |
6268 | /* Find the first section in the input segment, which may be | |
6269 | removed from the corresponding output segment. */ | |
6270 | if (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed)) | |
6271 | { | |
6272 | if (first_section == NULL) | |
6273 | first_section = section; | |
6274 | if (section->output_section != NULL) | |
6275 | ++section_count; | |
6276 | } | |
6277 | } | |
811072d8 | 6278 | |
b5f852ea NC |
6279 | /* Allocate a segment map big enough to contain |
6280 | all of the sections we have selected. */ | |
dc810e39 AM |
6281 | amt = sizeof (struct elf_segment_map); |
6282 | amt += ((bfd_size_type) section_count - 1) * sizeof (asection *); | |
a50b1753 | 6283 | map = (struct elf_segment_map *) bfd_zalloc (obfd, amt); |
bc67d8a6 | 6284 | if (map == NULL) |
b34976b6 | 6285 | return FALSE; |
252b5132 RH |
6286 | |
6287 | /* Initialise the fields of the segment map. Default to | |
6288 | using the physical address of the segment in the input BFD. */ | |
0067a569 AM |
6289 | map->next = NULL; |
6290 | map->p_type = segment->p_type; | |
6291 | map->p_flags = segment->p_flags; | |
bc67d8a6 | 6292 | map->p_flags_valid = 1; |
55d55ac7 | 6293 | |
9f17e2a6 L |
6294 | /* If the first section in the input segment is removed, there is |
6295 | no need to preserve segment physical address in the corresponding | |
6296 | output segment. */ | |
945c025a | 6297 | if (!first_section || first_section->output_section != NULL) |
9f17e2a6 L |
6298 | { |
6299 | map->p_paddr = segment->p_paddr; | |
5c44b38e | 6300 | map->p_paddr_valid = p_paddr_valid; |
9f17e2a6 | 6301 | } |
252b5132 RH |
6302 | |
6303 | /* Determine if this segment contains the ELF file header | |
6304 | and if it contains the program headers themselves. */ | |
bc67d8a6 NC |
6305 | map->includes_filehdr = (segment->p_offset == 0 |
6306 | && segment->p_filesz >= iehdr->e_ehsize); | |
bc67d8a6 | 6307 | map->includes_phdrs = 0; |
252b5132 | 6308 | |
0067a569 | 6309 | if (!phdr_included || segment->p_type != PT_LOAD) |
252b5132 | 6310 | { |
bc67d8a6 NC |
6311 | map->includes_phdrs = |
6312 | (segment->p_offset <= (bfd_vma) iehdr->e_phoff | |
6313 | && (segment->p_offset + segment->p_filesz | |
252b5132 RH |
6314 | >= ((bfd_vma) iehdr->e_phoff |
6315 | + iehdr->e_phnum * iehdr->e_phentsize))); | |
c044fabd | 6316 | |
bc67d8a6 | 6317 | if (segment->p_type == PT_LOAD && map->includes_phdrs) |
b34976b6 | 6318 | phdr_included = TRUE; |
252b5132 RH |
6319 | } |
6320 | ||
bc67d8a6 | 6321 | if (section_count == 0) |
252b5132 RH |
6322 | { |
6323 | /* Special segments, such as the PT_PHDR segment, may contain | |
6324 | no sections, but ordinary, loadable segments should contain | |
1ed89aa9 NC |
6325 | something. They are allowed by the ELF spec however, so only |
6326 | a warning is produced. */ | |
bc67d8a6 | 6327 | if (segment->p_type == PT_LOAD) |
c86934ce NC |
6328 | (*_bfd_error_handler) (_("\ |
6329 | %B: warning: Empty loadable segment detected, is this intentional ?"), | |
0067a569 | 6330 | ibfd); |
252b5132 | 6331 | |
bc67d8a6 | 6332 | map->count = 0; |
c044fabd KH |
6333 | *pointer_to_map = map; |
6334 | pointer_to_map = &map->next; | |
252b5132 RH |
6335 | |
6336 | continue; | |
6337 | } | |
6338 | ||
6339 | /* Now scan the sections in the input BFD again and attempt | |
6340 | to add their corresponding output sections to the segment map. | |
6341 | The problem here is how to handle an output section which has | |
6342 | been moved (ie had its LMA changed). There are four possibilities: | |
6343 | ||
6344 | 1. None of the sections have been moved. | |
6345 | In this case we can continue to use the segment LMA from the | |
6346 | input BFD. | |
6347 | ||
6348 | 2. All of the sections have been moved by the same amount. | |
6349 | In this case we can change the segment's LMA to match the LMA | |
6350 | of the first section. | |
6351 | ||
6352 | 3. Some of the sections have been moved, others have not. | |
6353 | In this case those sections which have not been moved can be | |
6354 | placed in the current segment which will have to have its size, | |
6355 | and possibly its LMA changed, and a new segment or segments will | |
6356 | have to be created to contain the other sections. | |
6357 | ||
b5f852ea | 6358 | 4. The sections have been moved, but not by the same amount. |
252b5132 RH |
6359 | In this case we can change the segment's LMA to match the LMA |
6360 | of the first section and we will have to create a new segment | |
6361 | or segments to contain the other sections. | |
6362 | ||
6363 | In order to save time, we allocate an array to hold the section | |
6364 | pointers that we are interested in. As these sections get assigned | |
6365 | to a segment, they are removed from this array. */ | |
6366 | ||
a50b1753 | 6367 | sections = (asection **) bfd_malloc2 (section_count, sizeof (asection *)); |
252b5132 | 6368 | if (sections == NULL) |
b34976b6 | 6369 | return FALSE; |
252b5132 RH |
6370 | |
6371 | /* Step One: Scan for segment vs section LMA conflicts. | |
6372 | Also add the sections to the section array allocated above. | |
6373 | Also add the sections to the current segment. In the common | |
6374 | case, where the sections have not been moved, this means that | |
6375 | we have completely filled the segment, and there is nothing | |
6376 | more to do. */ | |
252b5132 | 6377 | isec = 0; |
72730e0c | 6378 | matching_lma = 0; |
252b5132 | 6379 | suggested_lma = 0; |
0067a569 AM |
6380 | first_matching_lma = TRUE; |
6381 | first_suggested_lma = TRUE; | |
252b5132 | 6382 | |
147d51c2 | 6383 | for (section = ibfd->sections; |
bc67d8a6 NC |
6384 | section != NULL; |
6385 | section = section->next) | |
147d51c2 L |
6386 | if (section == first_section) |
6387 | break; | |
6388 | ||
6389 | for (j = 0; section != NULL; section = section->next) | |
252b5132 | 6390 | { |
caf47ea6 | 6391 | if (INCLUDE_SECTION_IN_SEGMENT (section, segment, bed)) |
c0f7859b | 6392 | { |
bc67d8a6 NC |
6393 | output_section = section->output_section; |
6394 | ||
0067a569 | 6395 | sections[j++] = section; |
252b5132 RH |
6396 | |
6397 | /* The Solaris native linker always sets p_paddr to 0. | |
6398 | We try to catch that case here, and set it to the | |
5e8d7549 NC |
6399 | correct value. Note - some backends require that |
6400 | p_paddr be left as zero. */ | |
5c44b38e | 6401 | if (!p_paddr_valid |
4455705d | 6402 | && segment->p_vaddr != 0 |
0067a569 | 6403 | && !bed->want_p_paddr_set_to_zero |
252b5132 | 6404 | && isec == 0 |
bc67d8a6 | 6405 | && output_section->lma != 0 |
0067a569 AM |
6406 | && output_section->vma == (segment->p_vaddr |
6407 | + (map->includes_filehdr | |
6408 | ? iehdr->e_ehsize | |
6409 | : 0) | |
6410 | + (map->includes_phdrs | |
6411 | ? (iehdr->e_phnum | |
6412 | * iehdr->e_phentsize) | |
6413 | : 0))) | |
bc67d8a6 | 6414 | map->p_paddr = segment->p_vaddr; |
252b5132 RH |
6415 | |
6416 | /* Match up the physical address of the segment with the | |
6417 | LMA address of the output section. */ | |
bc67d8a6 | 6418 | if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr) |
5e8d7549 | 6419 | || IS_COREFILE_NOTE (segment, section) |
0067a569 AM |
6420 | || (bed->want_p_paddr_set_to_zero |
6421 | && IS_CONTAINED_BY_VMA (output_section, segment))) | |
252b5132 | 6422 | { |
0067a569 AM |
6423 | if (first_matching_lma || output_section->lma < matching_lma) |
6424 | { | |
6425 | matching_lma = output_section->lma; | |
6426 | first_matching_lma = FALSE; | |
6427 | } | |
252b5132 RH |
6428 | |
6429 | /* We assume that if the section fits within the segment | |
bc67d8a6 | 6430 | then it does not overlap any other section within that |
252b5132 | 6431 | segment. */ |
0067a569 AM |
6432 | map->sections[isec++] = output_section; |
6433 | } | |
6434 | else if (first_suggested_lma) | |
6435 | { | |
6436 | suggested_lma = output_section->lma; | |
6437 | first_suggested_lma = FALSE; | |
252b5132 | 6438 | } |
147d51c2 L |
6439 | |
6440 | if (j == section_count) | |
6441 | break; | |
252b5132 RH |
6442 | } |
6443 | } | |
6444 | ||
bc67d8a6 | 6445 | BFD_ASSERT (j == section_count); |
252b5132 RH |
6446 | |
6447 | /* Step Two: Adjust the physical address of the current segment, | |
6448 | if necessary. */ | |
bc67d8a6 | 6449 | if (isec == section_count) |
252b5132 RH |
6450 | { |
6451 | /* All of the sections fitted within the segment as currently | |
6452 | specified. This is the default case. Add the segment to | |
6453 | the list of built segments and carry on to process the next | |
6454 | program header in the input BFD. */ | |
bc67d8a6 | 6455 | map->count = section_count; |
c044fabd KH |
6456 | *pointer_to_map = map; |
6457 | pointer_to_map = &map->next; | |
08a40648 | 6458 | |
5c44b38e AM |
6459 | if (p_paddr_valid |
6460 | && !bed->want_p_paddr_set_to_zero | |
147d51c2 | 6461 | && matching_lma != map->p_paddr |
5c44b38e AM |
6462 | && !map->includes_filehdr |
6463 | && !map->includes_phdrs) | |
3271a814 NS |
6464 | /* There is some padding before the first section in the |
6465 | segment. So, we must account for that in the output | |
6466 | segment's vma. */ | |
6467 | map->p_vaddr_offset = matching_lma - map->p_paddr; | |
08a40648 | 6468 | |
252b5132 RH |
6469 | free (sections); |
6470 | continue; | |
6471 | } | |
252b5132 RH |
6472 | else |
6473 | { | |
0067a569 | 6474 | if (!first_matching_lma) |
72730e0c AM |
6475 | { |
6476 | /* At least one section fits inside the current segment. | |
6477 | Keep it, but modify its physical address to match the | |
6478 | LMA of the first section that fitted. */ | |
bc67d8a6 | 6479 | map->p_paddr = matching_lma; |
72730e0c AM |
6480 | } |
6481 | else | |
6482 | { | |
6483 | /* None of the sections fitted inside the current segment. | |
6484 | Change the current segment's physical address to match | |
6485 | the LMA of the first section. */ | |
bc67d8a6 | 6486 | map->p_paddr = suggested_lma; |
72730e0c AM |
6487 | } |
6488 | ||
bc67d8a6 NC |
6489 | /* Offset the segment physical address from the lma |
6490 | to allow for space taken up by elf headers. */ | |
6491 | if (map->includes_filehdr) | |
010c8431 AM |
6492 | { |
6493 | if (map->p_paddr >= iehdr->e_ehsize) | |
6494 | map->p_paddr -= iehdr->e_ehsize; | |
6495 | else | |
6496 | { | |
6497 | map->includes_filehdr = FALSE; | |
6498 | map->includes_phdrs = FALSE; | |
6499 | } | |
6500 | } | |
252b5132 | 6501 | |
bc67d8a6 NC |
6502 | if (map->includes_phdrs) |
6503 | { | |
010c8431 AM |
6504 | if (map->p_paddr >= iehdr->e_phnum * iehdr->e_phentsize) |
6505 | { | |
6506 | map->p_paddr -= iehdr->e_phnum * iehdr->e_phentsize; | |
6507 | ||
6508 | /* iehdr->e_phnum is just an estimate of the number | |
6509 | of program headers that we will need. Make a note | |
6510 | here of the number we used and the segment we chose | |
6511 | to hold these headers, so that we can adjust the | |
6512 | offset when we know the correct value. */ | |
6513 | phdr_adjust_num = iehdr->e_phnum; | |
6514 | phdr_adjust_seg = map; | |
6515 | } | |
6516 | else | |
6517 | map->includes_phdrs = FALSE; | |
bc67d8a6 | 6518 | } |
252b5132 RH |
6519 | } |
6520 | ||
6521 | /* Step Three: Loop over the sections again, this time assigning | |
caf47ea6 | 6522 | those that fit to the current segment and removing them from the |
252b5132 RH |
6523 | sections array; but making sure not to leave large gaps. Once all |
6524 | possible sections have been assigned to the current segment it is | |
6525 | added to the list of built segments and if sections still remain | |
6526 | to be assigned, a new segment is constructed before repeating | |
6527 | the loop. */ | |
6528 | isec = 0; | |
6529 | do | |
6530 | { | |
bc67d8a6 | 6531 | map->count = 0; |
252b5132 | 6532 | suggested_lma = 0; |
0067a569 | 6533 | first_suggested_lma = TRUE; |
252b5132 RH |
6534 | |
6535 | /* Fill the current segment with sections that fit. */ | |
bc67d8a6 | 6536 | for (j = 0; j < section_count; j++) |
252b5132 | 6537 | { |
bc67d8a6 | 6538 | section = sections[j]; |
252b5132 | 6539 | |
bc67d8a6 | 6540 | if (section == NULL) |
252b5132 RH |
6541 | continue; |
6542 | ||
bc67d8a6 | 6543 | output_section = section->output_section; |
252b5132 | 6544 | |
bc67d8a6 | 6545 | BFD_ASSERT (output_section != NULL); |
c044fabd | 6546 | |
bc67d8a6 NC |
6547 | if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr) |
6548 | || IS_COREFILE_NOTE (segment, section)) | |
252b5132 | 6549 | { |
bc67d8a6 | 6550 | if (map->count == 0) |
252b5132 RH |
6551 | { |
6552 | /* If the first section in a segment does not start at | |
bc67d8a6 NC |
6553 | the beginning of the segment, then something is |
6554 | wrong. */ | |
0067a569 AM |
6555 | if (output_section->lma |
6556 | != (map->p_paddr | |
6557 | + (map->includes_filehdr ? iehdr->e_ehsize : 0) | |
6558 | + (map->includes_phdrs | |
6559 | ? iehdr->e_phnum * iehdr->e_phentsize | |
6560 | : 0))) | |
252b5132 RH |
6561 | abort (); |
6562 | } | |
6563 | else | |
6564 | { | |
0067a569 | 6565 | asection *prev_sec; |
252b5132 | 6566 | |
bc67d8a6 | 6567 | prev_sec = map->sections[map->count - 1]; |
252b5132 RH |
6568 | |
6569 | /* If the gap between the end of the previous section | |
bc67d8a6 NC |
6570 | and the start of this section is more than |
6571 | maxpagesize then we need to start a new segment. */ | |
eea6121a | 6572 | if ((BFD_ALIGN (prev_sec->lma + prev_sec->size, |
079e9a2f | 6573 | maxpagesize) |
caf47ea6 | 6574 | < BFD_ALIGN (output_section->lma, maxpagesize)) |
0067a569 | 6575 | || (prev_sec->lma + prev_sec->size |
079e9a2f | 6576 | > output_section->lma)) |
252b5132 | 6577 | { |
0067a569 AM |
6578 | if (first_suggested_lma) |
6579 | { | |
6580 | suggested_lma = output_section->lma; | |
6581 | first_suggested_lma = FALSE; | |
6582 | } | |
252b5132 RH |
6583 | |
6584 | continue; | |
6585 | } | |
6586 | } | |
6587 | ||
bc67d8a6 | 6588 | map->sections[map->count++] = output_section; |
252b5132 RH |
6589 | ++isec; |
6590 | sections[j] = NULL; | |
b34976b6 | 6591 | section->segment_mark = TRUE; |
252b5132 | 6592 | } |
0067a569 AM |
6593 | else if (first_suggested_lma) |
6594 | { | |
6595 | suggested_lma = output_section->lma; | |
6596 | first_suggested_lma = FALSE; | |
6597 | } | |
252b5132 RH |
6598 | } |
6599 | ||
bc67d8a6 | 6600 | BFD_ASSERT (map->count > 0); |
252b5132 RH |
6601 | |
6602 | /* Add the current segment to the list of built segments. */ | |
c044fabd KH |
6603 | *pointer_to_map = map; |
6604 | pointer_to_map = &map->next; | |
252b5132 | 6605 | |
bc67d8a6 | 6606 | if (isec < section_count) |
252b5132 RH |
6607 | { |
6608 | /* We still have not allocated all of the sections to | |
6609 | segments. Create a new segment here, initialise it | |
6610 | and carry on looping. */ | |
dc810e39 AM |
6611 | amt = sizeof (struct elf_segment_map); |
6612 | amt += ((bfd_size_type) section_count - 1) * sizeof (asection *); | |
5964fc3a | 6613 | map = (struct elf_segment_map *) bfd_zalloc (obfd, amt); |
bc67d8a6 | 6614 | if (map == NULL) |
5ed6aba4 NC |
6615 | { |
6616 | free (sections); | |
6617 | return FALSE; | |
6618 | } | |
252b5132 RH |
6619 | |
6620 | /* Initialise the fields of the segment map. Set the physical | |
6621 | physical address to the LMA of the first section that has | |
6622 | not yet been assigned. */ | |
0067a569 AM |
6623 | map->next = NULL; |
6624 | map->p_type = segment->p_type; | |
6625 | map->p_flags = segment->p_flags; | |
6626 | map->p_flags_valid = 1; | |
6627 | map->p_paddr = suggested_lma; | |
5c44b38e | 6628 | map->p_paddr_valid = p_paddr_valid; |
bc67d8a6 | 6629 | map->includes_filehdr = 0; |
0067a569 | 6630 | map->includes_phdrs = 0; |
252b5132 RH |
6631 | } |
6632 | } | |
bc67d8a6 | 6633 | while (isec < section_count); |
252b5132 RH |
6634 | |
6635 | free (sections); | |
6636 | } | |
6637 | ||
12bd6957 | 6638 | elf_seg_map (obfd) = map_first; |
bc67d8a6 NC |
6639 | |
6640 | /* If we had to estimate the number of program headers that were | |
9ad5cbcf | 6641 | going to be needed, then check our estimate now and adjust |
bc67d8a6 NC |
6642 | the offset if necessary. */ |
6643 | if (phdr_adjust_seg != NULL) | |
6644 | { | |
6645 | unsigned int count; | |
c044fabd | 6646 | |
bc67d8a6 | 6647 | for (count = 0, map = map_first; map != NULL; map = map->next) |
c044fabd | 6648 | count++; |
252b5132 | 6649 | |
bc67d8a6 NC |
6650 | if (count > phdr_adjust_num) |
6651 | phdr_adjust_seg->p_paddr | |
6652 | -= (count - phdr_adjust_num) * iehdr->e_phentsize; | |
6653 | } | |
c044fabd | 6654 | |
bc67d8a6 | 6655 | #undef SEGMENT_END |
eecdbe52 | 6656 | #undef SECTION_SIZE |
bc67d8a6 NC |
6657 | #undef IS_CONTAINED_BY_VMA |
6658 | #undef IS_CONTAINED_BY_LMA | |
0efc80c8 | 6659 | #undef IS_NOTE |
252b5132 | 6660 | #undef IS_COREFILE_NOTE |
bc67d8a6 | 6661 | #undef IS_SOLARIS_PT_INTERP |
9f17e2a6 | 6662 | #undef IS_SECTION_IN_INPUT_SEGMENT |
bc67d8a6 NC |
6663 | #undef INCLUDE_SECTION_IN_SEGMENT |
6664 | #undef SEGMENT_AFTER_SEGMENT | |
6665 | #undef SEGMENT_OVERLAPS | |
b34976b6 | 6666 | return TRUE; |
252b5132 RH |
6667 | } |
6668 | ||
84d1d650 L |
6669 | /* Copy ELF program header information. */ |
6670 | ||
6671 | static bfd_boolean | |
6672 | copy_elf_program_header (bfd *ibfd, bfd *obfd) | |
6673 | { | |
6674 | Elf_Internal_Ehdr *iehdr; | |
6675 | struct elf_segment_map *map; | |
6676 | struct elf_segment_map *map_first; | |
6677 | struct elf_segment_map **pointer_to_map; | |
6678 | Elf_Internal_Phdr *segment; | |
6679 | unsigned int i; | |
6680 | unsigned int num_segments; | |
6681 | bfd_boolean phdr_included = FALSE; | |
88967714 | 6682 | bfd_boolean p_paddr_valid; |
84d1d650 L |
6683 | |
6684 | iehdr = elf_elfheader (ibfd); | |
6685 | ||
6686 | map_first = NULL; | |
6687 | pointer_to_map = &map_first; | |
6688 | ||
88967714 AM |
6689 | /* If all the segment p_paddr fields are zero, don't set |
6690 | map->p_paddr_valid. */ | |
6691 | p_paddr_valid = FALSE; | |
84d1d650 | 6692 | num_segments = elf_elfheader (ibfd)->e_phnum; |
88967714 AM |
6693 | for (i = 0, segment = elf_tdata (ibfd)->phdr; |
6694 | i < num_segments; | |
6695 | i++, segment++) | |
6696 | if (segment->p_paddr != 0) | |
6697 | { | |
6698 | p_paddr_valid = TRUE; | |
6699 | break; | |
6700 | } | |
6701 | ||
84d1d650 L |
6702 | for (i = 0, segment = elf_tdata (ibfd)->phdr; |
6703 | i < num_segments; | |
6704 | i++, segment++) | |
6705 | { | |
6706 | asection *section; | |
6707 | unsigned int section_count; | |
6708 | bfd_size_type amt; | |
6709 | Elf_Internal_Shdr *this_hdr; | |
53020534 | 6710 | asection *first_section = NULL; |
a76e6f2f | 6711 | asection *lowest_section; |
84d1d650 | 6712 | |
84d1d650 L |
6713 | /* Compute how many sections are in this segment. */ |
6714 | for (section = ibfd->sections, section_count = 0; | |
6715 | section != NULL; | |
6716 | section = section->next) | |
6717 | { | |
6718 | this_hdr = &(elf_section_data(section)->this_hdr); | |
f4638467 | 6719 | if (ELF_SECTION_IN_SEGMENT (this_hdr, segment)) |
3271a814 | 6720 | { |
a76e6f2f AM |
6721 | if (first_section == NULL) |
6722 | first_section = section; | |
3271a814 NS |
6723 | section_count++; |
6724 | } | |
84d1d650 L |
6725 | } |
6726 | ||
6727 | /* Allocate a segment map big enough to contain | |
6728 | all of the sections we have selected. */ | |
6729 | amt = sizeof (struct elf_segment_map); | |
6730 | if (section_count != 0) | |
6731 | amt += ((bfd_size_type) section_count - 1) * sizeof (asection *); | |
a50b1753 | 6732 | map = (struct elf_segment_map *) bfd_zalloc (obfd, amt); |
84d1d650 L |
6733 | if (map == NULL) |
6734 | return FALSE; | |
6735 | ||
6736 | /* Initialize the fields of the output segment map with the | |
6737 | input segment. */ | |
6738 | map->next = NULL; | |
6739 | map->p_type = segment->p_type; | |
6740 | map->p_flags = segment->p_flags; | |
6741 | map->p_flags_valid = 1; | |
6742 | map->p_paddr = segment->p_paddr; | |
88967714 | 6743 | map->p_paddr_valid = p_paddr_valid; |
3f570048 AM |
6744 | map->p_align = segment->p_align; |
6745 | map->p_align_valid = 1; | |
3271a814 | 6746 | map->p_vaddr_offset = 0; |
84d1d650 | 6747 | |
04c3a755 NS |
6748 | if (map->p_type == PT_GNU_RELRO |
6749 | || map->p_type == PT_GNU_STACK) | |
b10a8ae0 L |
6750 | { |
6751 | /* The PT_GNU_RELRO segment may contain the first a few | |
6752 | bytes in the .got.plt section even if the whole .got.plt | |
6753 | section isn't in the PT_GNU_RELRO segment. We won't | |
04c3a755 NS |
6754 | change the size of the PT_GNU_RELRO segment. |
6755 | Similarly, PT_GNU_STACK size is significant on uclinux | |
6756 | systems. */ | |
9433b9b1 | 6757 | map->p_size = segment->p_memsz; |
b10a8ae0 L |
6758 | map->p_size_valid = 1; |
6759 | } | |
6760 | ||
84d1d650 L |
6761 | /* Determine if this segment contains the ELF file header |
6762 | and if it contains the program headers themselves. */ | |
6763 | map->includes_filehdr = (segment->p_offset == 0 | |
6764 | && segment->p_filesz >= iehdr->e_ehsize); | |
6765 | ||
6766 | map->includes_phdrs = 0; | |
6767 | if (! phdr_included || segment->p_type != PT_LOAD) | |
6768 | { | |
6769 | map->includes_phdrs = | |
6770 | (segment->p_offset <= (bfd_vma) iehdr->e_phoff | |
6771 | && (segment->p_offset + segment->p_filesz | |
6772 | >= ((bfd_vma) iehdr->e_phoff | |
6773 | + iehdr->e_phnum * iehdr->e_phentsize))); | |
6774 | ||
6775 | if (segment->p_type == PT_LOAD && map->includes_phdrs) | |
6776 | phdr_included = TRUE; | |
6777 | } | |
6778 | ||
bbefd0a9 | 6779 | lowest_section = NULL; |
84d1d650 L |
6780 | if (section_count != 0) |
6781 | { | |
6782 | unsigned int isec = 0; | |
6783 | ||
53020534 | 6784 | for (section = first_section; |
84d1d650 L |
6785 | section != NULL; |
6786 | section = section->next) | |
6787 | { | |
6788 | this_hdr = &(elf_section_data(section)->this_hdr); | |
f4638467 | 6789 | if (ELF_SECTION_IN_SEGMENT (this_hdr, segment)) |
53020534 L |
6790 | { |
6791 | map->sections[isec++] = section->output_section; | |
a76e6f2f AM |
6792 | if ((section->flags & SEC_ALLOC) != 0) |
6793 | { | |
6794 | bfd_vma seg_off; | |
6795 | ||
bbefd0a9 AM |
6796 | if (lowest_section == NULL |
6797 | || section->lma < lowest_section->lma) | |
fb8a5684 AM |
6798 | lowest_section = section; |
6799 | ||
a76e6f2f AM |
6800 | /* Section lmas are set up from PT_LOAD header |
6801 | p_paddr in _bfd_elf_make_section_from_shdr. | |
6802 | If this header has a p_paddr that disagrees | |
6803 | with the section lma, flag the p_paddr as | |
6804 | invalid. */ | |
6805 | if ((section->flags & SEC_LOAD) != 0) | |
6806 | seg_off = this_hdr->sh_offset - segment->p_offset; | |
6807 | else | |
6808 | seg_off = this_hdr->sh_addr - segment->p_vaddr; | |
6809 | if (section->lma - segment->p_paddr != seg_off) | |
6810 | map->p_paddr_valid = FALSE; | |
6811 | } | |
53020534 L |
6812 | if (isec == section_count) |
6813 | break; | |
6814 | } | |
84d1d650 L |
6815 | } |
6816 | } | |
6817 | ||
a76e6f2f AM |
6818 | if (map->includes_filehdr && lowest_section != NULL) |
6819 | /* We need to keep the space used by the headers fixed. */ | |
6820 | map->header_size = lowest_section->vma - segment->p_vaddr; | |
d324f6d6 | 6821 | |
a76e6f2f AM |
6822 | if (!map->includes_phdrs |
6823 | && !map->includes_filehdr | |
6824 | && map->p_paddr_valid) | |
6825 | /* There is some other padding before the first section. */ | |
6826 | map->p_vaddr_offset = ((lowest_section ? lowest_section->lma : 0) | |
6827 | - segment->p_paddr); | |
6828 | ||
84d1d650 L |
6829 | map->count = section_count; |
6830 | *pointer_to_map = map; | |
6831 | pointer_to_map = &map->next; | |
6832 | } | |
6833 | ||
12bd6957 | 6834 | elf_seg_map (obfd) = map_first; |
84d1d650 L |
6835 | return TRUE; |
6836 | } | |
6837 | ||
6838 | /* Copy private BFD data. This copies or rewrites ELF program header | |
6839 | information. */ | |
6840 | ||
6841 | static bfd_boolean | |
6842 | copy_private_bfd_data (bfd *ibfd, bfd *obfd) | |
6843 | { | |
84d1d650 L |
6844 | if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour |
6845 | || bfd_get_flavour (obfd) != bfd_target_elf_flavour) | |
6846 | return TRUE; | |
6847 | ||
6848 | if (elf_tdata (ibfd)->phdr == NULL) | |
6849 | return TRUE; | |
6850 | ||
6851 | if (ibfd->xvec == obfd->xvec) | |
6852 | { | |
cb3ff1e5 NC |
6853 | /* Check to see if any sections in the input BFD |
6854 | covered by ELF program header have changed. */ | |
d55ce4e2 | 6855 | Elf_Internal_Phdr *segment; |
84d1d650 L |
6856 | asection *section, *osec; |
6857 | unsigned int i, num_segments; | |
6858 | Elf_Internal_Shdr *this_hdr; | |
147d51c2 L |
6859 | const struct elf_backend_data *bed; |
6860 | ||
6861 | bed = get_elf_backend_data (ibfd); | |
6862 | ||
6863 | /* Regenerate the segment map if p_paddr is set to 0. */ | |
6864 | if (bed->want_p_paddr_set_to_zero) | |
6865 | goto rewrite; | |
84d1d650 L |
6866 | |
6867 | /* Initialize the segment mark field. */ | |
6868 | for (section = obfd->sections; section != NULL; | |
6869 | section = section->next) | |
6870 | section->segment_mark = FALSE; | |
6871 | ||
6872 | num_segments = elf_elfheader (ibfd)->e_phnum; | |
6873 | for (i = 0, segment = elf_tdata (ibfd)->phdr; | |
6874 | i < num_segments; | |
6875 | i++, segment++) | |
6876 | { | |
5f6999aa NC |
6877 | /* PR binutils/3535. The Solaris linker always sets the p_paddr |
6878 | and p_memsz fields of special segments (DYNAMIC, INTERP) to 0 | |
6879 | which severly confuses things, so always regenerate the segment | |
6880 | map in this case. */ | |
6881 | if (segment->p_paddr == 0 | |
6882 | && segment->p_memsz == 0 | |
6883 | && (segment->p_type == PT_INTERP || segment->p_type == PT_DYNAMIC)) | |
cb3ff1e5 | 6884 | goto rewrite; |
5f6999aa | 6885 | |
84d1d650 L |
6886 | for (section = ibfd->sections; |
6887 | section != NULL; section = section->next) | |
6888 | { | |
6889 | /* We mark the output section so that we know it comes | |
6890 | from the input BFD. */ | |
6891 | osec = section->output_section; | |
6892 | if (osec) | |
6893 | osec->segment_mark = TRUE; | |
6894 | ||
6895 | /* Check if this section is covered by the segment. */ | |
6896 | this_hdr = &(elf_section_data(section)->this_hdr); | |
f4638467 | 6897 | if (ELF_SECTION_IN_SEGMENT (this_hdr, segment)) |
84d1d650 L |
6898 | { |
6899 | /* FIXME: Check if its output section is changed or | |
6900 | removed. What else do we need to check? */ | |
6901 | if (osec == NULL | |
6902 | || section->flags != osec->flags | |
6903 | || section->lma != osec->lma | |
6904 | || section->vma != osec->vma | |
6905 | || section->size != osec->size | |
6906 | || section->rawsize != osec->rawsize | |
6907 | || section->alignment_power != osec->alignment_power) | |
6908 | goto rewrite; | |
6909 | } | |
6910 | } | |
6911 | } | |
6912 | ||
cb3ff1e5 | 6913 | /* Check to see if any output section do not come from the |
84d1d650 L |
6914 | input BFD. */ |
6915 | for (section = obfd->sections; section != NULL; | |
6916 | section = section->next) | |
6917 | { | |
6918 | if (section->segment_mark == FALSE) | |
6919 | goto rewrite; | |
6920 | else | |
6921 | section->segment_mark = FALSE; | |
6922 | } | |
6923 | ||
6924 | return copy_elf_program_header (ibfd, obfd); | |
6925 | } | |
6926 | ||
6927 | rewrite: | |
f1d85785 L |
6928 | if (ibfd->xvec == obfd->xvec) |
6929 | { | |
6930 | /* When rewriting program header, set the output maxpagesize to | |
6931 | the maximum alignment of input PT_LOAD segments. */ | |
6932 | Elf_Internal_Phdr *segment; | |
6933 | unsigned int i; | |
6934 | unsigned int num_segments = elf_elfheader (ibfd)->e_phnum; | |
6935 | bfd_vma maxpagesize = 0; | |
6936 | ||
6937 | for (i = 0, segment = elf_tdata (ibfd)->phdr; | |
6938 | i < num_segments; | |
6939 | i++, segment++) | |
6940 | if (segment->p_type == PT_LOAD | |
6941 | && maxpagesize < segment->p_align) | |
c86934ce NC |
6942 | { |
6943 | /* PR 17512: file: f17299af. */ | |
6944 | if (segment->p_align > (bfd_vma) 1 << ((sizeof (bfd_vma) * 8) - 2)) | |
6945 | (*_bfd_error_handler) (_("\ | |
6946 | %B: warning: segment alignment of 0x%llx is too large"), | |
6947 | ibfd, (long long) segment->p_align); | |
6948 | else | |
6949 | maxpagesize = segment->p_align; | |
6950 | } | |
f1d85785 L |
6951 | |
6952 | if (maxpagesize != get_elf_backend_data (obfd)->maxpagesize) | |
6953 | bfd_emul_set_maxpagesize (bfd_get_target (obfd), maxpagesize); | |
6954 | } | |
6955 | ||
84d1d650 L |
6956 | return rewrite_elf_program_header (ibfd, obfd); |
6957 | } | |
6958 | ||
ccd2ec6a L |
6959 | /* Initialize private output section information from input section. */ |
6960 | ||
6961 | bfd_boolean | |
6962 | _bfd_elf_init_private_section_data (bfd *ibfd, | |
6963 | asection *isec, | |
6964 | bfd *obfd, | |
6965 | asection *osec, | |
6966 | struct bfd_link_info *link_info) | |
6967 | ||
6968 | { | |
6969 | Elf_Internal_Shdr *ihdr, *ohdr; | |
0e1862bb L |
6970 | bfd_boolean final_link = (link_info != NULL |
6971 | && !bfd_link_relocatable (link_info)); | |
ccd2ec6a L |
6972 | |
6973 | if (ibfd->xvec->flavour != bfd_target_elf_flavour | |
6974 | || obfd->xvec->flavour != bfd_target_elf_flavour) | |
6975 | return TRUE; | |
6976 | ||
ba85c43e NC |
6977 | BFD_ASSERT (elf_section_data (osec) != NULL); |
6978 | ||
dfa7b0b8 AM |
6979 | /* For objcopy and relocatable link, don't copy the output ELF |
6980 | section type from input if the output BFD section flags have been | |
6981 | set to something different. For a final link allow some flags | |
6982 | that the linker clears to differ. */ | |
42bb2e33 | 6983 | if (elf_section_type (osec) == SHT_NULL |
dfa7b0b8 AM |
6984 | && (osec->flags == isec->flags |
6985 | || (final_link | |
6986 | && ((osec->flags ^ isec->flags) | |
0814be7d | 6987 | & ~(SEC_LINK_ONCE | SEC_LINK_DUPLICATES | SEC_RELOC)) == 0))) |
42bb2e33 | 6988 | elf_section_type (osec) = elf_section_type (isec); |
d270463e L |
6989 | |
6990 | /* FIXME: Is this correct for all OS/PROC specific flags? */ | |
6991 | elf_section_flags (osec) |= (elf_section_flags (isec) | |
6992 | & (SHF_MASKOS | SHF_MASKPROC)); | |
ccd2ec6a L |
6993 | |
6994 | /* Set things up for objcopy and relocatable link. The output | |
6995 | SHT_GROUP section will have its elf_next_in_group pointing back | |
6996 | to the input group members. Ignore linker created group section. | |
6997 | See elfNN_ia64_object_p in elfxx-ia64.c. */ | |
dfa7b0b8 | 6998 | if (!final_link) |
ccd2ec6a L |
6999 | { |
7000 | if (elf_sec_group (isec) == NULL | |
7001 | || (elf_sec_group (isec)->flags & SEC_LINKER_CREATED) == 0) | |
7002 | { | |
7003 | if (elf_section_flags (isec) & SHF_GROUP) | |
7004 | elf_section_flags (osec) |= SHF_GROUP; | |
7005 | elf_next_in_group (osec) = elf_next_in_group (isec); | |
9659de1c | 7006 | elf_section_data (osec)->group = elf_section_data (isec)->group; |
ccd2ec6a | 7007 | } |
68f5ff89 L |
7008 | |
7009 | /* If not decompress, preserve SHF_COMPRESSED. */ | |
7010 | if ((ibfd->flags & BFD_DECOMPRESS) == 0) | |
7011 | elf_section_flags (osec) |= (elf_section_flags (isec) | |
7012 | & SHF_COMPRESSED); | |
ccd2ec6a L |
7013 | } |
7014 | ||
7015 | ihdr = &elf_section_data (isec)->this_hdr; | |
7016 | ||
7017 | /* We need to handle elf_linked_to_section for SHF_LINK_ORDER. We | |
7018 | don't use the output section of the linked-to section since it | |
7019 | may be NULL at this point. */ | |
7020 | if ((ihdr->sh_flags & SHF_LINK_ORDER) != 0) | |
7021 | { | |
7022 | ohdr = &elf_section_data (osec)->this_hdr; | |
7023 | ohdr->sh_flags |= SHF_LINK_ORDER; | |
7024 | elf_linked_to_section (osec) = elf_linked_to_section (isec); | |
7025 | } | |
7026 | ||
7027 | osec->use_rela_p = isec->use_rela_p; | |
7028 | ||
7029 | return TRUE; | |
7030 | } | |
7031 | ||
252b5132 RH |
7032 | /* Copy private section information. This copies over the entsize |
7033 | field, and sometimes the info field. */ | |
7034 | ||
b34976b6 | 7035 | bfd_boolean |
217aa764 AM |
7036 | _bfd_elf_copy_private_section_data (bfd *ibfd, |
7037 | asection *isec, | |
7038 | bfd *obfd, | |
7039 | asection *osec) | |
252b5132 RH |
7040 | { |
7041 | Elf_Internal_Shdr *ihdr, *ohdr; | |
7042 | ||
7043 | if (ibfd->xvec->flavour != bfd_target_elf_flavour | |
7044 | || obfd->xvec->flavour != bfd_target_elf_flavour) | |
b34976b6 | 7045 | return TRUE; |
252b5132 | 7046 | |
252b5132 RH |
7047 | ihdr = &elf_section_data (isec)->this_hdr; |
7048 | ohdr = &elf_section_data (osec)->this_hdr; | |
7049 | ||
7050 | ohdr->sh_entsize = ihdr->sh_entsize; | |
7051 | ||
7052 | if (ihdr->sh_type == SHT_SYMTAB | |
7053 | || ihdr->sh_type == SHT_DYNSYM | |
7054 | || ihdr->sh_type == SHT_GNU_verneed | |
7055 | || ihdr->sh_type == SHT_GNU_verdef) | |
7056 | ohdr->sh_info = ihdr->sh_info; | |
7057 | ||
ccd2ec6a L |
7058 | return _bfd_elf_init_private_section_data (ibfd, isec, obfd, osec, |
7059 | NULL); | |
252b5132 RH |
7060 | } |
7061 | ||
d0bf826b AM |
7062 | /* Look at all the SHT_GROUP sections in IBFD, making any adjustments |
7063 | necessary if we are removing either the SHT_GROUP section or any of | |
7064 | the group member sections. DISCARDED is the value that a section's | |
7065 | output_section has if the section will be discarded, NULL when this | |
7066 | function is called from objcopy, bfd_abs_section_ptr when called | |
7067 | from the linker. */ | |
80fccad2 BW |
7068 | |
7069 | bfd_boolean | |
d0bf826b | 7070 | _bfd_elf_fixup_group_sections (bfd *ibfd, asection *discarded) |
80fccad2 | 7071 | { |
30288845 AM |
7072 | asection *isec; |
7073 | ||
30288845 | 7074 | for (isec = ibfd->sections; isec != NULL; isec = isec->next) |
415f38a6 | 7075 | if (elf_section_type (isec) == SHT_GROUP) |
30288845 AM |
7076 | { |
7077 | asection *first = elf_next_in_group (isec); | |
7078 | asection *s = first; | |
d0bf826b AM |
7079 | bfd_size_type removed = 0; |
7080 | ||
30288845 AM |
7081 | while (s != NULL) |
7082 | { | |
415f38a6 AM |
7083 | /* If this member section is being output but the |
7084 | SHT_GROUP section is not, then clear the group info | |
7085 | set up by _bfd_elf_copy_private_section_data. */ | |
d0bf826b AM |
7086 | if (s->output_section != discarded |
7087 | && isec->output_section == discarded) | |
30288845 AM |
7088 | { |
7089 | elf_section_flags (s->output_section) &= ~SHF_GROUP; | |
7090 | elf_group_name (s->output_section) = NULL; | |
7091 | } | |
415f38a6 AM |
7092 | /* Conversely, if the member section is not being output |
7093 | but the SHT_GROUP section is, then adjust its size. */ | |
d0bf826b AM |
7094 | else if (s->output_section == discarded |
7095 | && isec->output_section != discarded) | |
7096 | removed += 4; | |
30288845 AM |
7097 | s = elf_next_in_group (s); |
7098 | if (s == first) | |
7099 | break; | |
7100 | } | |
d0bf826b AM |
7101 | if (removed != 0) |
7102 | { | |
7103 | if (discarded != NULL) | |
7104 | { | |
7105 | /* If we've been called for ld -r, then we need to | |
7106 | adjust the input section size. This function may | |
7107 | be called multiple times, so save the original | |
7108 | size. */ | |
7109 | if (isec->rawsize == 0) | |
7110 | isec->rawsize = isec->size; | |
7111 | isec->size = isec->rawsize - removed; | |
7112 | } | |
7113 | else | |
7114 | { | |
7115 | /* Adjust the output section size when called from | |
7116 | objcopy. */ | |
7117 | isec->output_section->size -= removed; | |
7118 | } | |
7119 | } | |
30288845 AM |
7120 | } |
7121 | ||
80fccad2 BW |
7122 | return TRUE; |
7123 | } | |
7124 | ||
d0bf826b AM |
7125 | /* Copy private header information. */ |
7126 | ||
7127 | bfd_boolean | |
7128 | _bfd_elf_copy_private_header_data (bfd *ibfd, bfd *obfd) | |
7129 | { | |
7130 | if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour | |
7131 | || bfd_get_flavour (obfd) != bfd_target_elf_flavour) | |
7132 | return TRUE; | |
7133 | ||
7134 | /* Copy over private BFD data if it has not already been copied. | |
7135 | This must be done here, rather than in the copy_private_bfd_data | |
7136 | entry point, because the latter is called after the section | |
7137 | contents have been set, which means that the program headers have | |
7138 | already been worked out. */ | |
12bd6957 | 7139 | if (elf_seg_map (obfd) == NULL && elf_tdata (ibfd)->phdr != NULL) |
d0bf826b AM |
7140 | { |
7141 | if (! copy_private_bfd_data (ibfd, obfd)) | |
7142 | return FALSE; | |
7143 | } | |
7144 | ||
7145 | return _bfd_elf_fixup_group_sections (ibfd, NULL); | |
7146 | } | |
7147 | ||
252b5132 RH |
7148 | /* Copy private symbol information. If this symbol is in a section |
7149 | which we did not map into a BFD section, try to map the section | |
7150 | index correctly. We use special macro definitions for the mapped | |
7151 | section indices; these definitions are interpreted by the | |
7152 | swap_out_syms function. */ | |
7153 | ||
9ad5cbcf AM |
7154 | #define MAP_ONESYMTAB (SHN_HIOS + 1) |
7155 | #define MAP_DYNSYMTAB (SHN_HIOS + 2) | |
7156 | #define MAP_STRTAB (SHN_HIOS + 3) | |
7157 | #define MAP_SHSTRTAB (SHN_HIOS + 4) | |
7158 | #define MAP_SYM_SHNDX (SHN_HIOS + 5) | |
252b5132 | 7159 | |
b34976b6 | 7160 | bfd_boolean |
217aa764 AM |
7161 | _bfd_elf_copy_private_symbol_data (bfd *ibfd, |
7162 | asymbol *isymarg, | |
7163 | bfd *obfd, | |
7164 | asymbol *osymarg) | |
252b5132 RH |
7165 | { |
7166 | elf_symbol_type *isym, *osym; | |
7167 | ||
7168 | if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour | |
7169 | || bfd_get_flavour (obfd) != bfd_target_elf_flavour) | |
b34976b6 | 7170 | return TRUE; |
252b5132 RH |
7171 | |
7172 | isym = elf_symbol_from (ibfd, isymarg); | |
7173 | osym = elf_symbol_from (obfd, osymarg); | |
7174 | ||
7175 | if (isym != NULL | |
8424d8f5 | 7176 | && isym->internal_elf_sym.st_shndx != 0 |
252b5132 RH |
7177 | && osym != NULL |
7178 | && bfd_is_abs_section (isym->symbol.section)) | |
7179 | { | |
7180 | unsigned int shndx; | |
7181 | ||
7182 | shndx = isym->internal_elf_sym.st_shndx; | |
7183 | if (shndx == elf_onesymtab (ibfd)) | |
7184 | shndx = MAP_ONESYMTAB; | |
7185 | else if (shndx == elf_dynsymtab (ibfd)) | |
7186 | shndx = MAP_DYNSYMTAB; | |
12bd6957 | 7187 | else if (shndx == elf_strtab_sec (ibfd)) |
252b5132 | 7188 | shndx = MAP_STRTAB; |
12bd6957 | 7189 | else if (shndx == elf_shstrtab_sec (ibfd)) |
252b5132 | 7190 | shndx = MAP_SHSTRTAB; |
6a40cf0c | 7191 | else if (find_section_in_list (shndx, elf_symtab_shndx_list (ibfd))) |
9ad5cbcf | 7192 | shndx = MAP_SYM_SHNDX; |
252b5132 RH |
7193 | osym->internal_elf_sym.st_shndx = shndx; |
7194 | } | |
7195 | ||
b34976b6 | 7196 | return TRUE; |
252b5132 RH |
7197 | } |
7198 | ||
7199 | /* Swap out the symbols. */ | |
7200 | ||
b34976b6 | 7201 | static bfd_boolean |
217aa764 | 7202 | swap_out_syms (bfd *abfd, |
ef10c3ac | 7203 | struct elf_strtab_hash **sttp, |
217aa764 | 7204 | int relocatable_p) |
252b5132 | 7205 | { |
9c5bfbb7 | 7206 | const struct elf_backend_data *bed; |
079e9a2f AM |
7207 | int symcount; |
7208 | asymbol **syms; | |
ef10c3ac | 7209 | struct elf_strtab_hash *stt; |
079e9a2f | 7210 | Elf_Internal_Shdr *symtab_hdr; |
9ad5cbcf | 7211 | Elf_Internal_Shdr *symtab_shndx_hdr; |
079e9a2f | 7212 | Elf_Internal_Shdr *symstrtab_hdr; |
ef10c3ac | 7213 | struct elf_sym_strtab *symstrtab; |
f075ee0c AM |
7214 | bfd_byte *outbound_syms; |
7215 | bfd_byte *outbound_shndx; | |
ef10c3ac L |
7216 | unsigned long outbound_syms_index; |
7217 | unsigned long outbound_shndx_index; | |
079e9a2f | 7218 | int idx; |
12bd6957 | 7219 | unsigned int num_locals; |
079e9a2f | 7220 | bfd_size_type amt; |
174fd7f9 | 7221 | bfd_boolean name_local_sections; |
252b5132 | 7222 | |
12bd6957 | 7223 | if (!elf_map_symbols (abfd, &num_locals)) |
b34976b6 | 7224 | return FALSE; |
252b5132 | 7225 | |
c044fabd | 7226 | /* Dump out the symtabs. */ |
ef10c3ac | 7227 | stt = _bfd_elf_strtab_init (); |
079e9a2f | 7228 | if (stt == NULL) |
b34976b6 | 7229 | return FALSE; |
252b5132 | 7230 | |
079e9a2f AM |
7231 | bed = get_elf_backend_data (abfd); |
7232 | symcount = bfd_get_symcount (abfd); | |
7233 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; | |
7234 | symtab_hdr->sh_type = SHT_SYMTAB; | |
7235 | symtab_hdr->sh_entsize = bed->s->sizeof_sym; | |
7236 | symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1); | |
12bd6957 | 7237 | symtab_hdr->sh_info = num_locals + 1; |
72de5009 | 7238 | symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align; |
079e9a2f AM |
7239 | |
7240 | symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr; | |
7241 | symstrtab_hdr->sh_type = SHT_STRTAB; | |
7242 | ||
ef10c3ac L |
7243 | /* Allocate buffer to swap out the .strtab section. */ |
7244 | symstrtab = (struct elf_sym_strtab *) bfd_malloc ((symcount + 1) | |
7245 | * sizeof (*symstrtab)); | |
7246 | if (symstrtab == NULL) | |
7247 | { | |
7248 | _bfd_elf_strtab_free (stt); | |
7249 | return FALSE; | |
7250 | } | |
7251 | ||
a50b1753 NC |
7252 | outbound_syms = (bfd_byte *) bfd_alloc2 (abfd, 1 + symcount, |
7253 | bed->s->sizeof_sym); | |
079e9a2f | 7254 | if (outbound_syms == NULL) |
5ed6aba4 | 7255 | { |
ef10c3ac L |
7256 | error_return: |
7257 | _bfd_elf_strtab_free (stt); | |
7258 | free (symstrtab); | |
5ed6aba4 NC |
7259 | return FALSE; |
7260 | } | |
217aa764 | 7261 | symtab_hdr->contents = outbound_syms; |
ef10c3ac | 7262 | outbound_syms_index = 0; |
252b5132 | 7263 | |
9ad5cbcf | 7264 | outbound_shndx = NULL; |
ef10c3ac | 7265 | outbound_shndx_index = 0; |
6a40cf0c NC |
7266 | |
7267 | if (elf_symtab_shndx_list (abfd)) | |
9ad5cbcf | 7268 | { |
6a40cf0c NC |
7269 | symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr; |
7270 | if (symtab_shndx_hdr->sh_name != 0) | |
7271 | { | |
7272 | amt = (bfd_size_type) (1 + symcount) * sizeof (Elf_External_Sym_Shndx); | |
7273 | outbound_shndx = (bfd_byte *) | |
7274 | bfd_zalloc2 (abfd, 1 + symcount, sizeof (Elf_External_Sym_Shndx)); | |
7275 | if (outbound_shndx == NULL) | |
7276 | goto error_return; | |
5ed6aba4 | 7277 | |
6a40cf0c NC |
7278 | symtab_shndx_hdr->contents = outbound_shndx; |
7279 | symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX; | |
7280 | symtab_shndx_hdr->sh_size = amt; | |
7281 | symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx); | |
7282 | symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx); | |
7283 | } | |
7284 | /* FIXME: What about any other headers in the list ? */ | |
9ad5cbcf AM |
7285 | } |
7286 | ||
589e6347 | 7287 | /* Now generate the data (for "contents"). */ |
079e9a2f AM |
7288 | { |
7289 | /* Fill in zeroth symbol and swap it out. */ | |
7290 | Elf_Internal_Sym sym; | |
7291 | sym.st_name = 0; | |
7292 | sym.st_value = 0; | |
7293 | sym.st_size = 0; | |
7294 | sym.st_info = 0; | |
7295 | sym.st_other = 0; | |
7296 | sym.st_shndx = SHN_UNDEF; | |
35fc36a8 | 7297 | sym.st_target_internal = 0; |
ef10c3ac L |
7298 | symstrtab[0].sym = sym; |
7299 | symstrtab[0].dest_index = outbound_syms_index; | |
7300 | symstrtab[0].destshndx_index = outbound_shndx_index; | |
7301 | outbound_syms_index++; | |
9ad5cbcf | 7302 | if (outbound_shndx != NULL) |
ef10c3ac | 7303 | outbound_shndx_index++; |
079e9a2f | 7304 | } |
252b5132 | 7305 | |
174fd7f9 RS |
7306 | name_local_sections |
7307 | = (bed->elf_backend_name_local_section_symbols | |
7308 | && bed->elf_backend_name_local_section_symbols (abfd)); | |
7309 | ||
079e9a2f | 7310 | syms = bfd_get_outsymbols (abfd); |
ef10c3ac | 7311 | for (idx = 0; idx < symcount;) |
252b5132 | 7312 | { |
252b5132 | 7313 | Elf_Internal_Sym sym; |
079e9a2f AM |
7314 | bfd_vma value = syms[idx]->value; |
7315 | elf_symbol_type *type_ptr; | |
7316 | flagword flags = syms[idx]->flags; | |
7317 | int type; | |
252b5132 | 7318 | |
174fd7f9 RS |
7319 | if (!name_local_sections |
7320 | && (flags & (BSF_SECTION_SYM | BSF_GLOBAL)) == BSF_SECTION_SYM) | |
079e9a2f AM |
7321 | { |
7322 | /* Local section symbols have no name. */ | |
ef10c3ac | 7323 | sym.st_name = (unsigned long) -1; |
079e9a2f AM |
7324 | } |
7325 | else | |
7326 | { | |
ef10c3ac L |
7327 | /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize |
7328 | to get the final offset for st_name. */ | |
7329 | sym.st_name | |
7330 | = (unsigned long) _bfd_elf_strtab_add (stt, syms[idx]->name, | |
7331 | FALSE); | |
079e9a2f | 7332 | if (sym.st_name == (unsigned long) -1) |
ef10c3ac | 7333 | goto error_return; |
079e9a2f | 7334 | } |
252b5132 | 7335 | |
079e9a2f | 7336 | type_ptr = elf_symbol_from (abfd, syms[idx]); |
252b5132 | 7337 | |
079e9a2f AM |
7338 | if ((flags & BSF_SECTION_SYM) == 0 |
7339 | && bfd_is_com_section (syms[idx]->section)) | |
7340 | { | |
7341 | /* ELF common symbols put the alignment into the `value' field, | |
7342 | and the size into the `size' field. This is backwards from | |
7343 | how BFD handles it, so reverse it here. */ | |
7344 | sym.st_size = value; | |
7345 | if (type_ptr == NULL | |
7346 | || type_ptr->internal_elf_sym.st_value == 0) | |
7347 | sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value)); | |
7348 | else | |
7349 | sym.st_value = type_ptr->internal_elf_sym.st_value; | |
7350 | sym.st_shndx = _bfd_elf_section_from_bfd_section | |
7351 | (abfd, syms[idx]->section); | |
7352 | } | |
7353 | else | |
7354 | { | |
7355 | asection *sec = syms[idx]->section; | |
cb33740c | 7356 | unsigned int shndx; |
252b5132 | 7357 | |
079e9a2f AM |
7358 | if (sec->output_section) |
7359 | { | |
7360 | value += sec->output_offset; | |
7361 | sec = sec->output_section; | |
7362 | } | |
589e6347 | 7363 | |
079e9a2f AM |
7364 | /* Don't add in the section vma for relocatable output. */ |
7365 | if (! relocatable_p) | |
7366 | value += sec->vma; | |
7367 | sym.st_value = value; | |
7368 | sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0; | |
7369 | ||
7370 | if (bfd_is_abs_section (sec) | |
7371 | && type_ptr != NULL | |
7372 | && type_ptr->internal_elf_sym.st_shndx != 0) | |
7373 | { | |
7374 | /* This symbol is in a real ELF section which we did | |
7375 | not create as a BFD section. Undo the mapping done | |
7376 | by copy_private_symbol_data. */ | |
7377 | shndx = type_ptr->internal_elf_sym.st_shndx; | |
7378 | switch (shndx) | |
7379 | { | |
7380 | case MAP_ONESYMTAB: | |
7381 | shndx = elf_onesymtab (abfd); | |
7382 | break; | |
7383 | case MAP_DYNSYMTAB: | |
7384 | shndx = elf_dynsymtab (abfd); | |
7385 | break; | |
7386 | case MAP_STRTAB: | |
12bd6957 | 7387 | shndx = elf_strtab_sec (abfd); |
079e9a2f AM |
7388 | break; |
7389 | case MAP_SHSTRTAB: | |
12bd6957 | 7390 | shndx = elf_shstrtab_sec (abfd); |
079e9a2f | 7391 | break; |
9ad5cbcf | 7392 | case MAP_SYM_SHNDX: |
6a40cf0c NC |
7393 | if (elf_symtab_shndx_list (abfd)) |
7394 | shndx = elf_symtab_shndx_list (abfd)->ndx; | |
9ad5cbcf | 7395 | break; |
079e9a2f | 7396 | default: |
15bc576a | 7397 | shndx = SHN_ABS; |
079e9a2f AM |
7398 | break; |
7399 | } | |
7400 | } | |
7401 | else | |
7402 | { | |
7403 | shndx = _bfd_elf_section_from_bfd_section (abfd, sec); | |
252b5132 | 7404 | |
cb33740c | 7405 | if (shndx == SHN_BAD) |
079e9a2f AM |
7406 | { |
7407 | asection *sec2; | |
7408 | ||
7409 | /* Writing this would be a hell of a lot easier if | |
7410 | we had some decent documentation on bfd, and | |
7411 | knew what to expect of the library, and what to | |
7412 | demand of applications. For example, it | |
7413 | appears that `objcopy' might not set the | |
7414 | section of a symbol to be a section that is | |
7415 | actually in the output file. */ | |
7416 | sec2 = bfd_get_section_by_name (abfd, sec->name); | |
589e6347 NC |
7417 | if (sec2 == NULL) |
7418 | { | |
7419 | _bfd_error_handler (_("\ | |
7420 | Unable to find equivalent output section for symbol '%s' from section '%s'"), | |
7421 | syms[idx]->name ? syms[idx]->name : "<Local sym>", | |
7422 | sec->name); | |
811072d8 | 7423 | bfd_set_error (bfd_error_invalid_operation); |
ef10c3ac | 7424 | goto error_return; |
589e6347 | 7425 | } |
811072d8 | 7426 | |
079e9a2f | 7427 | shndx = _bfd_elf_section_from_bfd_section (abfd, sec2); |
cb33740c | 7428 | BFD_ASSERT (shndx != SHN_BAD); |
079e9a2f AM |
7429 | } |
7430 | } | |
252b5132 | 7431 | |
079e9a2f AM |
7432 | sym.st_shndx = shndx; |
7433 | } | |
252b5132 | 7434 | |
13ae64f3 JJ |
7435 | if ((flags & BSF_THREAD_LOCAL) != 0) |
7436 | type = STT_TLS; | |
d8045f23 NC |
7437 | else if ((flags & BSF_GNU_INDIRECT_FUNCTION) != 0) |
7438 | type = STT_GNU_IFUNC; | |
13ae64f3 | 7439 | else if ((flags & BSF_FUNCTION) != 0) |
079e9a2f AM |
7440 | type = STT_FUNC; |
7441 | else if ((flags & BSF_OBJECT) != 0) | |
7442 | type = STT_OBJECT; | |
d9352518 DB |
7443 | else if ((flags & BSF_RELC) != 0) |
7444 | type = STT_RELC; | |
7445 | else if ((flags & BSF_SRELC) != 0) | |
7446 | type = STT_SRELC; | |
079e9a2f AM |
7447 | else |
7448 | type = STT_NOTYPE; | |
252b5132 | 7449 | |
13ae64f3 JJ |
7450 | if (syms[idx]->section->flags & SEC_THREAD_LOCAL) |
7451 | type = STT_TLS; | |
7452 | ||
589e6347 | 7453 | /* Processor-specific types. */ |
079e9a2f AM |
7454 | if (type_ptr != NULL |
7455 | && bed->elf_backend_get_symbol_type) | |
7456 | type = ((*bed->elf_backend_get_symbol_type) | |
7457 | (&type_ptr->internal_elf_sym, type)); | |
252b5132 | 7458 | |
079e9a2f AM |
7459 | if (flags & BSF_SECTION_SYM) |
7460 | { | |
7461 | if (flags & BSF_GLOBAL) | |
7462 | sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION); | |
7463 | else | |
7464 | sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION); | |
7465 | } | |
7466 | else if (bfd_is_com_section (syms[idx]->section)) | |
0a40daed | 7467 | { |
504b7d20 | 7468 | #ifdef USE_STT_COMMON |
0a40daed MK |
7469 | if (type == STT_OBJECT) |
7470 | sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_COMMON); | |
7471 | else | |
504b7d20 | 7472 | #endif |
c91e322a | 7473 | sym.st_info = ELF_ST_INFO (STB_GLOBAL, type); |
0a40daed | 7474 | } |
079e9a2f AM |
7475 | else if (bfd_is_und_section (syms[idx]->section)) |
7476 | sym.st_info = ELF_ST_INFO (((flags & BSF_WEAK) | |
7477 | ? STB_WEAK | |
7478 | : STB_GLOBAL), | |
7479 | type); | |
7480 | else if (flags & BSF_FILE) | |
7481 | sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE); | |
7482 | else | |
7483 | { | |
7484 | int bind = STB_LOCAL; | |
252b5132 | 7485 | |
079e9a2f AM |
7486 | if (flags & BSF_LOCAL) |
7487 | bind = STB_LOCAL; | |
3e7a7d11 NC |
7488 | else if (flags & BSF_GNU_UNIQUE) |
7489 | bind = STB_GNU_UNIQUE; | |
079e9a2f AM |
7490 | else if (flags & BSF_WEAK) |
7491 | bind = STB_WEAK; | |
7492 | else if (flags & BSF_GLOBAL) | |
7493 | bind = STB_GLOBAL; | |
252b5132 | 7494 | |
079e9a2f AM |
7495 | sym.st_info = ELF_ST_INFO (bind, type); |
7496 | } | |
252b5132 | 7497 | |
079e9a2f | 7498 | if (type_ptr != NULL) |
35fc36a8 RS |
7499 | { |
7500 | sym.st_other = type_ptr->internal_elf_sym.st_other; | |
7501 | sym.st_target_internal | |
7502 | = type_ptr->internal_elf_sym.st_target_internal; | |
7503 | } | |
079e9a2f | 7504 | else |
35fc36a8 RS |
7505 | { |
7506 | sym.st_other = 0; | |
7507 | sym.st_target_internal = 0; | |
7508 | } | |
252b5132 | 7509 | |
ef10c3ac L |
7510 | idx++; |
7511 | symstrtab[idx].sym = sym; | |
7512 | symstrtab[idx].dest_index = outbound_syms_index; | |
7513 | symstrtab[idx].destshndx_index = outbound_shndx_index; | |
7514 | ||
7515 | outbound_syms_index++; | |
9ad5cbcf | 7516 | if (outbound_shndx != NULL) |
ef10c3ac L |
7517 | outbound_shndx_index++; |
7518 | } | |
7519 | ||
7520 | /* Finalize the .strtab section. */ | |
7521 | _bfd_elf_strtab_finalize (stt); | |
7522 | ||
7523 | /* Swap out the .strtab section. */ | |
7524 | for (idx = 0; idx <= symcount; idx++) | |
7525 | { | |
7526 | struct elf_sym_strtab *elfsym = &symstrtab[idx]; | |
7527 | if (elfsym->sym.st_name == (unsigned long) -1) | |
7528 | elfsym->sym.st_name = 0; | |
7529 | else | |
7530 | elfsym->sym.st_name = _bfd_elf_strtab_offset (stt, | |
7531 | elfsym->sym.st_name); | |
7532 | bed->s->swap_symbol_out (abfd, &elfsym->sym, | |
7533 | (outbound_syms | |
7534 | + (elfsym->dest_index | |
7535 | * bed->s->sizeof_sym)), | |
7536 | (outbound_shndx | |
7537 | + (elfsym->destshndx_index | |
7538 | * sizeof (Elf_External_Sym_Shndx)))); | |
079e9a2f | 7539 | } |
ef10c3ac | 7540 | free (symstrtab); |
252b5132 | 7541 | |
079e9a2f | 7542 | *sttp = stt; |
ef10c3ac | 7543 | symstrtab_hdr->sh_size = _bfd_elf_strtab_size (stt); |
079e9a2f | 7544 | symstrtab_hdr->sh_type = SHT_STRTAB; |
252b5132 | 7545 | |
079e9a2f AM |
7546 | symstrtab_hdr->sh_flags = 0; |
7547 | symstrtab_hdr->sh_addr = 0; | |
7548 | symstrtab_hdr->sh_entsize = 0; | |
7549 | symstrtab_hdr->sh_link = 0; | |
7550 | symstrtab_hdr->sh_info = 0; | |
7551 | symstrtab_hdr->sh_addralign = 1; | |
252b5132 | 7552 | |
b34976b6 | 7553 | return TRUE; |
252b5132 RH |
7554 | } |
7555 | ||
7556 | /* Return the number of bytes required to hold the symtab vector. | |
7557 | ||
7558 | Note that we base it on the count plus 1, since we will null terminate | |
7559 | the vector allocated based on this size. However, the ELF symbol table | |
7560 | always has a dummy entry as symbol #0, so it ends up even. */ | |
7561 | ||
7562 | long | |
217aa764 | 7563 | _bfd_elf_get_symtab_upper_bound (bfd *abfd) |
252b5132 RH |
7564 | { |
7565 | long symcount; | |
7566 | long symtab_size; | |
7567 | Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr; | |
7568 | ||
7569 | symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym; | |
b99d1833 AM |
7570 | symtab_size = (symcount + 1) * (sizeof (asymbol *)); |
7571 | if (symcount > 0) | |
7572 | symtab_size -= sizeof (asymbol *); | |
252b5132 RH |
7573 | |
7574 | return symtab_size; | |
7575 | } | |
7576 | ||
7577 | long | |
217aa764 | 7578 | _bfd_elf_get_dynamic_symtab_upper_bound (bfd *abfd) |
252b5132 RH |
7579 | { |
7580 | long symcount; | |
7581 | long symtab_size; | |
7582 | Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr; | |
7583 | ||
7584 | if (elf_dynsymtab (abfd) == 0) | |
7585 | { | |
7586 | bfd_set_error (bfd_error_invalid_operation); | |
7587 | return -1; | |
7588 | } | |
7589 | ||
7590 | symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym; | |
b99d1833 AM |
7591 | symtab_size = (symcount + 1) * (sizeof (asymbol *)); |
7592 | if (symcount > 0) | |
7593 | symtab_size -= sizeof (asymbol *); | |
252b5132 RH |
7594 | |
7595 | return symtab_size; | |
7596 | } | |
7597 | ||
7598 | long | |
217aa764 AM |
7599 | _bfd_elf_get_reloc_upper_bound (bfd *abfd ATTRIBUTE_UNUSED, |
7600 | sec_ptr asect) | |
252b5132 RH |
7601 | { |
7602 | return (asect->reloc_count + 1) * sizeof (arelent *); | |
7603 | } | |
7604 | ||
7605 | /* Canonicalize the relocs. */ | |
7606 | ||
7607 | long | |
217aa764 AM |
7608 | _bfd_elf_canonicalize_reloc (bfd *abfd, |
7609 | sec_ptr section, | |
7610 | arelent **relptr, | |
7611 | asymbol **symbols) | |
252b5132 RH |
7612 | { |
7613 | arelent *tblptr; | |
7614 | unsigned int i; | |
9c5bfbb7 | 7615 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
252b5132 | 7616 | |
b34976b6 | 7617 | if (! bed->s->slurp_reloc_table (abfd, section, symbols, FALSE)) |
252b5132 RH |
7618 | return -1; |
7619 | ||
7620 | tblptr = section->relocation; | |
7621 | for (i = 0; i < section->reloc_count; i++) | |
7622 | *relptr++ = tblptr++; | |
7623 | ||
7624 | *relptr = NULL; | |
7625 | ||
7626 | return section->reloc_count; | |
7627 | } | |
7628 | ||
7629 | long | |
6cee3f79 | 7630 | _bfd_elf_canonicalize_symtab (bfd *abfd, asymbol **allocation) |
252b5132 | 7631 | { |
9c5bfbb7 | 7632 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
217aa764 | 7633 | long symcount = bed->s->slurp_symbol_table (abfd, allocation, FALSE); |
252b5132 RH |
7634 | |
7635 | if (symcount >= 0) | |
7636 | bfd_get_symcount (abfd) = symcount; | |
7637 | return symcount; | |
7638 | } | |
7639 | ||
7640 | long | |
217aa764 AM |
7641 | _bfd_elf_canonicalize_dynamic_symtab (bfd *abfd, |
7642 | asymbol **allocation) | |
252b5132 | 7643 | { |
9c5bfbb7 | 7644 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
217aa764 | 7645 | long symcount = bed->s->slurp_symbol_table (abfd, allocation, TRUE); |
1f70368c DJ |
7646 | |
7647 | if (symcount >= 0) | |
7648 | bfd_get_dynamic_symcount (abfd) = symcount; | |
7649 | return symcount; | |
252b5132 RH |
7650 | } |
7651 | ||
8615f3f2 AM |
7652 | /* Return the size required for the dynamic reloc entries. Any loadable |
7653 | section that was actually installed in the BFD, and has type SHT_REL | |
7654 | or SHT_RELA, and uses the dynamic symbol table, is considered to be a | |
7655 | dynamic reloc section. */ | |
252b5132 RH |
7656 | |
7657 | long | |
217aa764 | 7658 | _bfd_elf_get_dynamic_reloc_upper_bound (bfd *abfd) |
252b5132 RH |
7659 | { |
7660 | long ret; | |
7661 | asection *s; | |
7662 | ||
7663 | if (elf_dynsymtab (abfd) == 0) | |
7664 | { | |
7665 | bfd_set_error (bfd_error_invalid_operation); | |
7666 | return -1; | |
7667 | } | |
7668 | ||
7669 | ret = sizeof (arelent *); | |
7670 | for (s = abfd->sections; s != NULL; s = s->next) | |
266b05cf | 7671 | if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd) |
252b5132 RH |
7672 | && (elf_section_data (s)->this_hdr.sh_type == SHT_REL |
7673 | || elf_section_data (s)->this_hdr.sh_type == SHT_RELA)) | |
eea6121a | 7674 | ret += ((s->size / elf_section_data (s)->this_hdr.sh_entsize) |
252b5132 RH |
7675 | * sizeof (arelent *)); |
7676 | ||
7677 | return ret; | |
7678 | } | |
7679 | ||
8615f3f2 AM |
7680 | /* Canonicalize the dynamic relocation entries. Note that we return the |
7681 | dynamic relocations as a single block, although they are actually | |
7682 | associated with particular sections; the interface, which was | |
7683 | designed for SunOS style shared libraries, expects that there is only | |
7684 | one set of dynamic relocs. Any loadable section that was actually | |
7685 | installed in the BFD, and has type SHT_REL or SHT_RELA, and uses the | |
7686 | dynamic symbol table, is considered to be a dynamic reloc section. */ | |
252b5132 RH |
7687 | |
7688 | long | |
217aa764 AM |
7689 | _bfd_elf_canonicalize_dynamic_reloc (bfd *abfd, |
7690 | arelent **storage, | |
7691 | asymbol **syms) | |
252b5132 | 7692 | { |
217aa764 | 7693 | bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean); |
252b5132 RH |
7694 | asection *s; |
7695 | long ret; | |
7696 | ||
7697 | if (elf_dynsymtab (abfd) == 0) | |
7698 | { | |
7699 | bfd_set_error (bfd_error_invalid_operation); | |
7700 | return -1; | |
7701 | } | |
7702 | ||
7703 | slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table; | |
7704 | ret = 0; | |
7705 | for (s = abfd->sections; s != NULL; s = s->next) | |
7706 | { | |
266b05cf | 7707 | if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd) |
252b5132 RH |
7708 | && (elf_section_data (s)->this_hdr.sh_type == SHT_REL |
7709 | || elf_section_data (s)->this_hdr.sh_type == SHT_RELA)) | |
7710 | { | |
7711 | arelent *p; | |
7712 | long count, i; | |
7713 | ||
b34976b6 | 7714 | if (! (*slurp_relocs) (abfd, s, syms, TRUE)) |
252b5132 | 7715 | return -1; |
eea6121a | 7716 | count = s->size / elf_section_data (s)->this_hdr.sh_entsize; |
252b5132 RH |
7717 | p = s->relocation; |
7718 | for (i = 0; i < count; i++) | |
7719 | *storage++ = p++; | |
7720 | ret += count; | |
7721 | } | |
7722 | } | |
7723 | ||
7724 | *storage = NULL; | |
7725 | ||
7726 | return ret; | |
7727 | } | |
7728 | \f | |
7729 | /* Read in the version information. */ | |
7730 | ||
b34976b6 | 7731 | bfd_boolean |
fc0e6df6 | 7732 | _bfd_elf_slurp_version_tables (bfd *abfd, bfd_boolean default_imported_symver) |
252b5132 RH |
7733 | { |
7734 | bfd_byte *contents = NULL; | |
fc0e6df6 PB |
7735 | unsigned int freeidx = 0; |
7736 | ||
7737 | if (elf_dynverref (abfd) != 0) | |
7738 | { | |
7739 | Elf_Internal_Shdr *hdr; | |
7740 | Elf_External_Verneed *everneed; | |
7741 | Elf_Internal_Verneed *iverneed; | |
7742 | unsigned int i; | |
d0fb9a8d | 7743 | bfd_byte *contents_end; |
fc0e6df6 PB |
7744 | |
7745 | hdr = &elf_tdata (abfd)->dynverref_hdr; | |
7746 | ||
601a03ba | 7747 | if (hdr->sh_info == 0 || hdr->sh_size < sizeof (Elf_External_Verneed)) |
d0fb9a8d | 7748 | { |
601a03ba AM |
7749 | error_return_bad_verref: |
7750 | (*_bfd_error_handler) | |
7751 | (_("%B: .gnu.version_r invalid entry"), abfd); | |
7752 | bfd_set_error (bfd_error_bad_value); | |
d0fb9a8d JJ |
7753 | error_return_verref: |
7754 | elf_tdata (abfd)->verref = NULL; | |
7755 | elf_tdata (abfd)->cverrefs = 0; | |
7756 | goto error_return; | |
7757 | } | |
601a03ba AM |
7758 | |
7759 | contents = (bfd_byte *) bfd_malloc (hdr->sh_size); | |
7760 | if (contents == NULL) | |
7761 | goto error_return_verref; | |
7762 | ||
fc0e6df6 PB |
7763 | if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0 |
7764 | || bfd_bread (contents, hdr->sh_size, abfd) != hdr->sh_size) | |
d0fb9a8d | 7765 | goto error_return_verref; |
fc0e6df6 | 7766 | |
601a03ba AM |
7767 | elf_tdata (abfd)->verref = (Elf_Internal_Verneed *) |
7768 | bfd_zalloc2 (abfd, hdr->sh_info, sizeof (Elf_Internal_Verneed)); | |
7769 | ||
7770 | if (elf_tdata (abfd)->verref == NULL) | |
d0fb9a8d JJ |
7771 | goto error_return_verref; |
7772 | ||
7773 | BFD_ASSERT (sizeof (Elf_External_Verneed) | |
7774 | == sizeof (Elf_External_Vernaux)); | |
7775 | contents_end = contents + hdr->sh_size - sizeof (Elf_External_Verneed); | |
fc0e6df6 PB |
7776 | everneed = (Elf_External_Verneed *) contents; |
7777 | iverneed = elf_tdata (abfd)->verref; | |
7778 | for (i = 0; i < hdr->sh_info; i++, iverneed++) | |
7779 | { | |
7780 | Elf_External_Vernaux *evernaux; | |
7781 | Elf_Internal_Vernaux *ivernaux; | |
7782 | unsigned int j; | |
7783 | ||
7784 | _bfd_elf_swap_verneed_in (abfd, everneed, iverneed); | |
7785 | ||
7786 | iverneed->vn_bfd = abfd; | |
7787 | ||
7788 | iverneed->vn_filename = | |
7789 | bfd_elf_string_from_elf_section (abfd, hdr->sh_link, | |
7790 | iverneed->vn_file); | |
7791 | if (iverneed->vn_filename == NULL) | |
601a03ba | 7792 | goto error_return_bad_verref; |
fc0e6df6 | 7793 | |
d0fb9a8d JJ |
7794 | if (iverneed->vn_cnt == 0) |
7795 | iverneed->vn_auxptr = NULL; | |
7796 | else | |
7797 | { | |
a50b1753 NC |
7798 | iverneed->vn_auxptr = (struct elf_internal_vernaux *) |
7799 | bfd_alloc2 (abfd, iverneed->vn_cnt, | |
7800 | sizeof (Elf_Internal_Vernaux)); | |
d0fb9a8d JJ |
7801 | if (iverneed->vn_auxptr == NULL) |
7802 | goto error_return_verref; | |
7803 | } | |
7804 | ||
7805 | if (iverneed->vn_aux | |
7806 | > (size_t) (contents_end - (bfd_byte *) everneed)) | |
601a03ba | 7807 | goto error_return_bad_verref; |
fc0e6df6 PB |
7808 | |
7809 | evernaux = ((Elf_External_Vernaux *) | |
7810 | ((bfd_byte *) everneed + iverneed->vn_aux)); | |
7811 | ivernaux = iverneed->vn_auxptr; | |
7812 | for (j = 0; j < iverneed->vn_cnt; j++, ivernaux++) | |
7813 | { | |
7814 | _bfd_elf_swap_vernaux_in (abfd, evernaux, ivernaux); | |
7815 | ||
7816 | ivernaux->vna_nodename = | |
7817 | bfd_elf_string_from_elf_section (abfd, hdr->sh_link, | |
7818 | ivernaux->vna_name); | |
7819 | if (ivernaux->vna_nodename == NULL) | |
601a03ba | 7820 | goto error_return_bad_verref; |
fc0e6df6 | 7821 | |
25ff461f AM |
7822 | if (ivernaux->vna_other > freeidx) |
7823 | freeidx = ivernaux->vna_other; | |
7824 | ||
7825 | ivernaux->vna_nextptr = NULL; | |
7826 | if (ivernaux->vna_next == 0) | |
7827 | { | |
7828 | iverneed->vn_cnt = j + 1; | |
7829 | break; | |
7830 | } | |
fc0e6df6 PB |
7831 | if (j + 1 < iverneed->vn_cnt) |
7832 | ivernaux->vna_nextptr = ivernaux + 1; | |
fc0e6df6 | 7833 | |
d0fb9a8d JJ |
7834 | if (ivernaux->vna_next |
7835 | > (size_t) (contents_end - (bfd_byte *) evernaux)) | |
601a03ba | 7836 | goto error_return_bad_verref; |
d0fb9a8d | 7837 | |
fc0e6df6 PB |
7838 | evernaux = ((Elf_External_Vernaux *) |
7839 | ((bfd_byte *) evernaux + ivernaux->vna_next)); | |
fc0e6df6 PB |
7840 | } |
7841 | ||
25ff461f AM |
7842 | iverneed->vn_nextref = NULL; |
7843 | if (iverneed->vn_next == 0) | |
7844 | break; | |
fc0e6df6 PB |
7845 | if (i + 1 < hdr->sh_info) |
7846 | iverneed->vn_nextref = iverneed + 1; | |
fc0e6df6 | 7847 | |
d0fb9a8d JJ |
7848 | if (iverneed->vn_next |
7849 | > (size_t) (contents_end - (bfd_byte *) everneed)) | |
601a03ba | 7850 | goto error_return_bad_verref; |
d0fb9a8d | 7851 | |
fc0e6df6 PB |
7852 | everneed = ((Elf_External_Verneed *) |
7853 | ((bfd_byte *) everneed + iverneed->vn_next)); | |
7854 | } | |
25ff461f | 7855 | elf_tdata (abfd)->cverrefs = i; |
fc0e6df6 PB |
7856 | |
7857 | free (contents); | |
7858 | contents = NULL; | |
7859 | } | |
252b5132 RH |
7860 | |
7861 | if (elf_dynverdef (abfd) != 0) | |
7862 | { | |
7863 | Elf_Internal_Shdr *hdr; | |
7864 | Elf_External_Verdef *everdef; | |
7865 | Elf_Internal_Verdef *iverdef; | |
f631889e UD |
7866 | Elf_Internal_Verdef *iverdefarr; |
7867 | Elf_Internal_Verdef iverdefmem; | |
252b5132 | 7868 | unsigned int i; |
062e2358 | 7869 | unsigned int maxidx; |
d0fb9a8d | 7870 | bfd_byte *contents_end_def, *contents_end_aux; |
252b5132 RH |
7871 | |
7872 | hdr = &elf_tdata (abfd)->dynverdef_hdr; | |
7873 | ||
601a03ba AM |
7874 | if (hdr->sh_info == 0 || hdr->sh_size < sizeof (Elf_External_Verdef)) |
7875 | { | |
7876 | error_return_bad_verdef: | |
7877 | (*_bfd_error_handler) | |
7878 | (_("%B: .gnu.version_d invalid entry"), abfd); | |
7879 | bfd_set_error (bfd_error_bad_value); | |
7880 | error_return_verdef: | |
7881 | elf_tdata (abfd)->verdef = NULL; | |
7882 | elf_tdata (abfd)->cverdefs = 0; | |
7883 | goto error_return; | |
7884 | } | |
7885 | ||
a50b1753 | 7886 | contents = (bfd_byte *) bfd_malloc (hdr->sh_size); |
252b5132 | 7887 | if (contents == NULL) |
601a03ba | 7888 | goto error_return_verdef; |
252b5132 | 7889 | if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0 |
217aa764 | 7890 | || bfd_bread (contents, hdr->sh_size, abfd) != hdr->sh_size) |
601a03ba | 7891 | goto error_return_verdef; |
d0fb9a8d JJ |
7892 | |
7893 | BFD_ASSERT (sizeof (Elf_External_Verdef) | |
7894 | >= sizeof (Elf_External_Verdaux)); | |
7895 | contents_end_def = contents + hdr->sh_size | |
7896 | - sizeof (Elf_External_Verdef); | |
7897 | contents_end_aux = contents + hdr->sh_size | |
7898 | - sizeof (Elf_External_Verdaux); | |
7899 | ||
f631889e UD |
7900 | /* We know the number of entries in the section but not the maximum |
7901 | index. Therefore we have to run through all entries and find | |
7902 | the maximum. */ | |
252b5132 | 7903 | everdef = (Elf_External_Verdef *) contents; |
f631889e UD |
7904 | maxidx = 0; |
7905 | for (i = 0; i < hdr->sh_info; ++i) | |
7906 | { | |
7907 | _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem); | |
7908 | ||
601a03ba AM |
7909 | if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) == 0) |
7910 | goto error_return_bad_verdef; | |
062e2358 AM |
7911 | if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) > maxidx) |
7912 | maxidx = iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION); | |
f631889e | 7913 | |
25ff461f AM |
7914 | if (iverdefmem.vd_next == 0) |
7915 | break; | |
7916 | ||
d0fb9a8d JJ |
7917 | if (iverdefmem.vd_next |
7918 | > (size_t) (contents_end_def - (bfd_byte *) everdef)) | |
601a03ba | 7919 | goto error_return_bad_verdef; |
d0fb9a8d | 7920 | |
f631889e UD |
7921 | everdef = ((Elf_External_Verdef *) |
7922 | ((bfd_byte *) everdef + iverdefmem.vd_next)); | |
7923 | } | |
7924 | ||
fc0e6df6 PB |
7925 | if (default_imported_symver) |
7926 | { | |
7927 | if (freeidx > maxidx) | |
7928 | maxidx = ++freeidx; | |
7929 | else | |
7930 | freeidx = ++maxidx; | |
7931 | } | |
201159ec | 7932 | |
601a03ba AM |
7933 | elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *) |
7934 | bfd_zalloc2 (abfd, maxidx, sizeof (Elf_Internal_Verdef)); | |
f631889e | 7935 | if (elf_tdata (abfd)->verdef == NULL) |
601a03ba | 7936 | goto error_return_verdef; |
f631889e UD |
7937 | |
7938 | elf_tdata (abfd)->cverdefs = maxidx; | |
7939 | ||
7940 | everdef = (Elf_External_Verdef *) contents; | |
7941 | iverdefarr = elf_tdata (abfd)->verdef; | |
7942 | for (i = 0; i < hdr->sh_info; i++) | |
252b5132 RH |
7943 | { |
7944 | Elf_External_Verdaux *everdaux; | |
7945 | Elf_Internal_Verdaux *iverdaux; | |
7946 | unsigned int j; | |
7947 | ||
f631889e UD |
7948 | _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem); |
7949 | ||
d0fb9a8d | 7950 | if ((iverdefmem.vd_ndx & VERSYM_VERSION) == 0) |
601a03ba | 7951 | goto error_return_bad_verdef; |
d0fb9a8d | 7952 | |
f631889e | 7953 | iverdef = &iverdefarr[(iverdefmem.vd_ndx & VERSYM_VERSION) - 1]; |
595bce75 | 7954 | memcpy (iverdef, &iverdefmem, offsetof (Elf_Internal_Verdef, vd_bfd)); |
252b5132 RH |
7955 | |
7956 | iverdef->vd_bfd = abfd; | |
7957 | ||
d0fb9a8d JJ |
7958 | if (iverdef->vd_cnt == 0) |
7959 | iverdef->vd_auxptr = NULL; | |
7960 | else | |
7961 | { | |
a50b1753 NC |
7962 | iverdef->vd_auxptr = (struct elf_internal_verdaux *) |
7963 | bfd_alloc2 (abfd, iverdef->vd_cnt, | |
7964 | sizeof (Elf_Internal_Verdaux)); | |
d0fb9a8d JJ |
7965 | if (iverdef->vd_auxptr == NULL) |
7966 | goto error_return_verdef; | |
7967 | } | |
7968 | ||
7969 | if (iverdef->vd_aux | |
7970 | > (size_t) (contents_end_aux - (bfd_byte *) everdef)) | |
601a03ba | 7971 | goto error_return_bad_verdef; |
252b5132 RH |
7972 | |
7973 | everdaux = ((Elf_External_Verdaux *) | |
7974 | ((bfd_byte *) everdef + iverdef->vd_aux)); | |
7975 | iverdaux = iverdef->vd_auxptr; | |
7976 | for (j = 0; j < iverdef->vd_cnt; j++, iverdaux++) | |
7977 | { | |
7978 | _bfd_elf_swap_verdaux_in (abfd, everdaux, iverdaux); | |
7979 | ||
7980 | iverdaux->vda_nodename = | |
7981 | bfd_elf_string_from_elf_section (abfd, hdr->sh_link, | |
7982 | iverdaux->vda_name); | |
7983 | if (iverdaux->vda_nodename == NULL) | |
601a03ba | 7984 | goto error_return_bad_verdef; |
252b5132 | 7985 | |
25ff461f AM |
7986 | iverdaux->vda_nextptr = NULL; |
7987 | if (iverdaux->vda_next == 0) | |
7988 | { | |
7989 | iverdef->vd_cnt = j + 1; | |
7990 | break; | |
7991 | } | |
252b5132 RH |
7992 | if (j + 1 < iverdef->vd_cnt) |
7993 | iverdaux->vda_nextptr = iverdaux + 1; | |
252b5132 | 7994 | |
d0fb9a8d JJ |
7995 | if (iverdaux->vda_next |
7996 | > (size_t) (contents_end_aux - (bfd_byte *) everdaux)) | |
601a03ba | 7997 | goto error_return_bad_verdef; |
d0fb9a8d | 7998 | |
252b5132 RH |
7999 | everdaux = ((Elf_External_Verdaux *) |
8000 | ((bfd_byte *) everdaux + iverdaux->vda_next)); | |
8001 | } | |
8002 | ||
595bce75 | 8003 | iverdef->vd_nodename = NULL; |
d0fb9a8d JJ |
8004 | if (iverdef->vd_cnt) |
8005 | iverdef->vd_nodename = iverdef->vd_auxptr->vda_nodename; | |
252b5132 | 8006 | |
25ff461f AM |
8007 | iverdef->vd_nextdef = NULL; |
8008 | if (iverdef->vd_next == 0) | |
8009 | break; | |
d0fb9a8d | 8010 | if ((size_t) (iverdef - iverdefarr) + 1 < maxidx) |
252b5132 | 8011 | iverdef->vd_nextdef = iverdef + 1; |
252b5132 RH |
8012 | |
8013 | everdef = ((Elf_External_Verdef *) | |
8014 | ((bfd_byte *) everdef + iverdef->vd_next)); | |
8015 | } | |
8016 | ||
8017 | free (contents); | |
8018 | contents = NULL; | |
8019 | } | |
fc0e6df6 | 8020 | else if (default_imported_symver) |
252b5132 | 8021 | { |
fc0e6df6 PB |
8022 | if (freeidx < 3) |
8023 | freeidx = 3; | |
8024 | else | |
8025 | freeidx++; | |
252b5132 | 8026 | |
a50b1753 NC |
8027 | elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *) |
8028 | bfd_zalloc2 (abfd, freeidx, sizeof (Elf_Internal_Verdef)); | |
fc0e6df6 | 8029 | if (elf_tdata (abfd)->verdef == NULL) |
252b5132 RH |
8030 | goto error_return; |
8031 | ||
fc0e6df6 PB |
8032 | elf_tdata (abfd)->cverdefs = freeidx; |
8033 | } | |
252b5132 | 8034 | |
fc0e6df6 PB |
8035 | /* Create a default version based on the soname. */ |
8036 | if (default_imported_symver) | |
8037 | { | |
8038 | Elf_Internal_Verdef *iverdef; | |
8039 | Elf_Internal_Verdaux *iverdaux; | |
252b5132 | 8040 | |
5bb3703f | 8041 | iverdef = &elf_tdata (abfd)->verdef[freeidx - 1]; |
252b5132 | 8042 | |
fc0e6df6 PB |
8043 | iverdef->vd_version = VER_DEF_CURRENT; |
8044 | iverdef->vd_flags = 0; | |
8045 | iverdef->vd_ndx = freeidx; | |
8046 | iverdef->vd_cnt = 1; | |
252b5132 | 8047 | |
fc0e6df6 | 8048 | iverdef->vd_bfd = abfd; |
252b5132 | 8049 | |
fc0e6df6 PB |
8050 | iverdef->vd_nodename = bfd_elf_get_dt_soname (abfd); |
8051 | if (iverdef->vd_nodename == NULL) | |
d0fb9a8d | 8052 | goto error_return_verdef; |
fc0e6df6 | 8053 | iverdef->vd_nextdef = NULL; |
601a03ba AM |
8054 | iverdef->vd_auxptr = ((struct elf_internal_verdaux *) |
8055 | bfd_zalloc (abfd, sizeof (Elf_Internal_Verdaux))); | |
d0fb9a8d JJ |
8056 | if (iverdef->vd_auxptr == NULL) |
8057 | goto error_return_verdef; | |
252b5132 | 8058 | |
fc0e6df6 PB |
8059 | iverdaux = iverdef->vd_auxptr; |
8060 | iverdaux->vda_nodename = iverdef->vd_nodename; | |
252b5132 RH |
8061 | } |
8062 | ||
b34976b6 | 8063 | return TRUE; |
252b5132 RH |
8064 | |
8065 | error_return: | |
5ed6aba4 | 8066 | if (contents != NULL) |
252b5132 | 8067 | free (contents); |
b34976b6 | 8068 | return FALSE; |
252b5132 RH |
8069 | } |
8070 | \f | |
8071 | asymbol * | |
217aa764 | 8072 | _bfd_elf_make_empty_symbol (bfd *abfd) |
252b5132 RH |
8073 | { |
8074 | elf_symbol_type *newsym; | |
8075 | ||
201159ec | 8076 | newsym = (elf_symbol_type *) bfd_zalloc (abfd, sizeof * newsym); |
252b5132 RH |
8077 | if (!newsym) |
8078 | return NULL; | |
201159ec NC |
8079 | newsym->symbol.the_bfd = abfd; |
8080 | return &newsym->symbol; | |
252b5132 RH |
8081 | } |
8082 | ||
8083 | void | |
217aa764 AM |
8084 | _bfd_elf_get_symbol_info (bfd *abfd ATTRIBUTE_UNUSED, |
8085 | asymbol *symbol, | |
8086 | symbol_info *ret) | |
252b5132 RH |
8087 | { |
8088 | bfd_symbol_info (symbol, ret); | |
8089 | } | |
8090 | ||
8091 | /* Return whether a symbol name implies a local symbol. Most targets | |
8092 | use this function for the is_local_label_name entry point, but some | |
8093 | override it. */ | |
8094 | ||
b34976b6 | 8095 | bfd_boolean |
217aa764 AM |
8096 | _bfd_elf_is_local_label_name (bfd *abfd ATTRIBUTE_UNUSED, |
8097 | const char *name) | |
252b5132 RH |
8098 | { |
8099 | /* Normal local symbols start with ``.L''. */ | |
8100 | if (name[0] == '.' && name[1] == 'L') | |
b34976b6 | 8101 | return TRUE; |
252b5132 RH |
8102 | |
8103 | /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate | |
8104 | DWARF debugging symbols starting with ``..''. */ | |
8105 | if (name[0] == '.' && name[1] == '.') | |
b34976b6 | 8106 | return TRUE; |
252b5132 RH |
8107 | |
8108 | /* gcc will sometimes generate symbols beginning with ``_.L_'' when | |
8109 | emitting DWARF debugging output. I suspect this is actually a | |
8110 | small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call | |
8111 | ASM_GENERATE_INTERNAL_LABEL, and this causes the leading | |
8112 | underscore to be emitted on some ELF targets). For ease of use, | |
8113 | we treat such symbols as local. */ | |
8114 | if (name[0] == '_' && name[1] == '.' && name[2] == 'L' && name[3] == '_') | |
b34976b6 | 8115 | return TRUE; |
252b5132 | 8116 | |
b1fa9dd6 NC |
8117 | /* Treat assembler generated fake symbols, dollar local labels and |
8118 | forward-backward labels (aka local labels) as locals. | |
8119 | These labels have the form: | |
8120 | ||
8121 | L0^A.* (fake symbols) | |
8122 | ||
8123 | [.]?L[0123456789]+{^A|^B}[0123456789]* (local labels) | |
8124 | ||
8125 | Versions which start with .L will have already been matched above, | |
8126 | so we only need to match the rest. */ | |
8127 | if (name[0] == 'L' && ISDIGIT (name[1])) | |
8128 | { | |
8129 | bfd_boolean ret = FALSE; | |
8130 | const char * p; | |
8131 | char c; | |
8132 | ||
8133 | for (p = name + 2; (c = *p); p++) | |
8134 | { | |
8135 | if (c == 1 || c == 2) | |
8136 | { | |
8137 | if (c == 1 && p == name + 2) | |
8138 | /* A fake symbol. */ | |
8139 | return TRUE; | |
8140 | ||
8141 | /* FIXME: We are being paranoid here and treating symbols like | |
8142 | L0^Bfoo as if there were non-local, on the grounds that the | |
8143 | assembler will never generate them. But can any symbol | |
8144 | containing an ASCII value in the range 1-31 ever be anything | |
8145 | other than some kind of local ? */ | |
8146 | ret = TRUE; | |
8147 | } | |
8148 | ||
8149 | if (! ISDIGIT (c)) | |
8150 | { | |
8151 | ret = FALSE; | |
8152 | break; | |
8153 | } | |
8154 | } | |
8155 | return ret; | |
8156 | } | |
ffa54770 | 8157 | |
b34976b6 | 8158 | return FALSE; |
252b5132 RH |
8159 | } |
8160 | ||
8161 | alent * | |
217aa764 AM |
8162 | _bfd_elf_get_lineno (bfd *abfd ATTRIBUTE_UNUSED, |
8163 | asymbol *symbol ATTRIBUTE_UNUSED) | |
252b5132 RH |
8164 | { |
8165 | abort (); | |
8166 | return NULL; | |
8167 | } | |
8168 | ||
b34976b6 | 8169 | bfd_boolean |
217aa764 AM |
8170 | _bfd_elf_set_arch_mach (bfd *abfd, |
8171 | enum bfd_architecture arch, | |
8172 | unsigned long machine) | |
252b5132 RH |
8173 | { |
8174 | /* If this isn't the right architecture for this backend, and this | |
8175 | isn't the generic backend, fail. */ | |
8176 | if (arch != get_elf_backend_data (abfd)->arch | |
8177 | && arch != bfd_arch_unknown | |
8178 | && get_elf_backend_data (abfd)->arch != bfd_arch_unknown) | |
b34976b6 | 8179 | return FALSE; |
252b5132 RH |
8180 | |
8181 | return bfd_default_set_arch_mach (abfd, arch, machine); | |
8182 | } | |
8183 | ||
d1fad7c6 NC |
8184 | /* Find the nearest line to a particular section and offset, |
8185 | for error reporting. */ | |
8186 | ||
b34976b6 | 8187 | bfd_boolean |
217aa764 | 8188 | _bfd_elf_find_nearest_line (bfd *abfd, |
217aa764 | 8189 | asymbol **symbols, |
fb167eb2 | 8190 | asection *section, |
217aa764 AM |
8191 | bfd_vma offset, |
8192 | const char **filename_ptr, | |
8193 | const char **functionname_ptr, | |
fb167eb2 AM |
8194 | unsigned int *line_ptr, |
8195 | unsigned int *discriminator_ptr) | |
d1fad7c6 | 8196 | { |
b34976b6 | 8197 | bfd_boolean found; |
d1fad7c6 | 8198 | |
fb167eb2 | 8199 | if (_bfd_dwarf2_find_nearest_line (abfd, symbols, NULL, section, offset, |
4e8a9624 | 8200 | filename_ptr, functionname_ptr, |
fb167eb2 AM |
8201 | line_ptr, discriminator_ptr, |
8202 | dwarf_debug_sections, 0, | |
e00e8198 AM |
8203 | &elf_tdata (abfd)->dwarf2_find_line_info) |
8204 | || _bfd_dwarf1_find_nearest_line (abfd, symbols, section, offset, | |
8205 | filename_ptr, functionname_ptr, | |
8206 | line_ptr)) | |
d1fad7c6 NC |
8207 | { |
8208 | if (!*functionname_ptr) | |
e00e8198 AM |
8209 | _bfd_elf_find_function (abfd, symbols, section, offset, |
8210 | *filename_ptr ? NULL : filename_ptr, | |
8211 | functionname_ptr); | |
b34976b6 | 8212 | return TRUE; |
d1fad7c6 NC |
8213 | } |
8214 | ||
8215 | if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset, | |
4e8a9624 AM |
8216 | &found, filename_ptr, |
8217 | functionname_ptr, line_ptr, | |
8218 | &elf_tdata (abfd)->line_info)) | |
b34976b6 | 8219 | return FALSE; |
dc43ada5 | 8220 | if (found && (*functionname_ptr || *line_ptr)) |
b34976b6 | 8221 | return TRUE; |
d1fad7c6 NC |
8222 | |
8223 | if (symbols == NULL) | |
b34976b6 | 8224 | return FALSE; |
d1fad7c6 | 8225 | |
e00e8198 AM |
8226 | if (! _bfd_elf_find_function (abfd, symbols, section, offset, |
8227 | filename_ptr, functionname_ptr)) | |
b34976b6 | 8228 | return FALSE; |
d1fad7c6 | 8229 | |
252b5132 | 8230 | *line_ptr = 0; |
b34976b6 | 8231 | return TRUE; |
252b5132 RH |
8232 | } |
8233 | ||
5420f73d L |
8234 | /* Find the line for a symbol. */ |
8235 | ||
8236 | bfd_boolean | |
8237 | _bfd_elf_find_line (bfd *abfd, asymbol **symbols, asymbol *symbol, | |
8238 | const char **filename_ptr, unsigned int *line_ptr) | |
9b8d1a36 | 8239 | { |
fb167eb2 AM |
8240 | return _bfd_dwarf2_find_nearest_line (abfd, symbols, symbol, NULL, 0, |
8241 | filename_ptr, NULL, line_ptr, NULL, | |
8242 | dwarf_debug_sections, 0, | |
8243 | &elf_tdata (abfd)->dwarf2_find_line_info); | |
5420f73d L |
8244 | } |
8245 | ||
4ab527b0 FF |
8246 | /* After a call to bfd_find_nearest_line, successive calls to |
8247 | bfd_find_inliner_info can be used to get source information about | |
8248 | each level of function inlining that terminated at the address | |
8249 | passed to bfd_find_nearest_line. Currently this is only supported | |
8250 | for DWARF2 with appropriate DWARF3 extensions. */ | |
8251 | ||
8252 | bfd_boolean | |
8253 | _bfd_elf_find_inliner_info (bfd *abfd, | |
8254 | const char **filename_ptr, | |
8255 | const char **functionname_ptr, | |
8256 | unsigned int *line_ptr) | |
8257 | { | |
8258 | bfd_boolean found; | |
8259 | found = _bfd_dwarf2_find_inliner_info (abfd, filename_ptr, | |
8260 | functionname_ptr, line_ptr, | |
8261 | & elf_tdata (abfd)->dwarf2_find_line_info); | |
8262 | return found; | |
8263 | } | |
8264 | ||
252b5132 | 8265 | int |
a6b96beb | 8266 | _bfd_elf_sizeof_headers (bfd *abfd, struct bfd_link_info *info) |
252b5132 | 8267 | { |
8ded5a0f AM |
8268 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
8269 | int ret = bed->s->sizeof_ehdr; | |
252b5132 | 8270 | |
0e1862bb | 8271 | if (!bfd_link_relocatable (info)) |
8ded5a0f | 8272 | { |
12bd6957 | 8273 | bfd_size_type phdr_size = elf_program_header_size (abfd); |
8ded5a0f | 8274 | |
62d7a5f6 AM |
8275 | if (phdr_size == (bfd_size_type) -1) |
8276 | { | |
8277 | struct elf_segment_map *m; | |
8278 | ||
8279 | phdr_size = 0; | |
12bd6957 | 8280 | for (m = elf_seg_map (abfd); m != NULL; m = m->next) |
62d7a5f6 | 8281 | phdr_size += bed->s->sizeof_phdr; |
8ded5a0f | 8282 | |
62d7a5f6 AM |
8283 | if (phdr_size == 0) |
8284 | phdr_size = get_program_header_size (abfd, info); | |
8285 | } | |
8ded5a0f | 8286 | |
12bd6957 | 8287 | elf_program_header_size (abfd) = phdr_size; |
8ded5a0f AM |
8288 | ret += phdr_size; |
8289 | } | |
8290 | ||
252b5132 RH |
8291 | return ret; |
8292 | } | |
8293 | ||
b34976b6 | 8294 | bfd_boolean |
217aa764 AM |
8295 | _bfd_elf_set_section_contents (bfd *abfd, |
8296 | sec_ptr section, | |
0f867abe | 8297 | const void *location, |
217aa764 AM |
8298 | file_ptr offset, |
8299 | bfd_size_type count) | |
252b5132 RH |
8300 | { |
8301 | Elf_Internal_Shdr *hdr; | |
1b6aeedb | 8302 | file_ptr pos; |
252b5132 RH |
8303 | |
8304 | if (! abfd->output_has_begun | |
217aa764 | 8305 | && ! _bfd_elf_compute_section_file_positions (abfd, NULL)) |
b34976b6 | 8306 | return FALSE; |
252b5132 | 8307 | |
0ce398f1 L |
8308 | if (!count) |
8309 | return TRUE; | |
8310 | ||
252b5132 | 8311 | hdr = &elf_section_data (section)->this_hdr; |
0ce398f1 L |
8312 | if (hdr->sh_offset == (file_ptr) -1) |
8313 | { | |
8314 | /* We must compress this section. Write output to the buffer. */ | |
8315 | unsigned char *contents = hdr->contents; | |
8316 | if ((offset + count) > hdr->sh_size | |
8317 | || (section->flags & SEC_ELF_COMPRESS) == 0 | |
8318 | || contents == NULL) | |
8319 | abort (); | |
8320 | memcpy (contents + offset, location, count); | |
8321 | return TRUE; | |
8322 | } | |
dc810e39 AM |
8323 | pos = hdr->sh_offset + offset; |
8324 | if (bfd_seek (abfd, pos, SEEK_SET) != 0 | |
8325 | || bfd_bwrite (location, count, abfd) != count) | |
b34976b6 | 8326 | return FALSE; |
252b5132 | 8327 | |
b34976b6 | 8328 | return TRUE; |
252b5132 RH |
8329 | } |
8330 | ||
8331 | void | |
217aa764 AM |
8332 | _bfd_elf_no_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED, |
8333 | arelent *cache_ptr ATTRIBUTE_UNUSED, | |
8334 | Elf_Internal_Rela *dst ATTRIBUTE_UNUSED) | |
252b5132 RH |
8335 | { |
8336 | abort (); | |
8337 | } | |
8338 | ||
252b5132 RH |
8339 | /* Try to convert a non-ELF reloc into an ELF one. */ |
8340 | ||
b34976b6 | 8341 | bfd_boolean |
217aa764 | 8342 | _bfd_elf_validate_reloc (bfd *abfd, arelent *areloc) |
252b5132 | 8343 | { |
c044fabd | 8344 | /* Check whether we really have an ELF howto. */ |
252b5132 RH |
8345 | |
8346 | if ((*areloc->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec) | |
8347 | { | |
8348 | bfd_reloc_code_real_type code; | |
8349 | reloc_howto_type *howto; | |
8350 | ||
8351 | /* Alien reloc: Try to determine its type to replace it with an | |
c044fabd | 8352 | equivalent ELF reloc. */ |
252b5132 RH |
8353 | |
8354 | if (areloc->howto->pc_relative) | |
8355 | { | |
8356 | switch (areloc->howto->bitsize) | |
8357 | { | |
8358 | case 8: | |
8359 | code = BFD_RELOC_8_PCREL; | |
8360 | break; | |
8361 | case 12: | |
8362 | code = BFD_RELOC_12_PCREL; | |
8363 | break; | |
8364 | case 16: | |
8365 | code = BFD_RELOC_16_PCREL; | |
8366 | break; | |
8367 | case 24: | |
8368 | code = BFD_RELOC_24_PCREL; | |
8369 | break; | |
8370 | case 32: | |
8371 | code = BFD_RELOC_32_PCREL; | |
8372 | break; | |
8373 | case 64: | |
8374 | code = BFD_RELOC_64_PCREL; | |
8375 | break; | |
8376 | default: | |
8377 | goto fail; | |
8378 | } | |
8379 | ||
8380 | howto = bfd_reloc_type_lookup (abfd, code); | |
8381 | ||
8382 | if (areloc->howto->pcrel_offset != howto->pcrel_offset) | |
8383 | { | |
8384 | if (howto->pcrel_offset) | |
8385 | areloc->addend += areloc->address; | |
8386 | else | |
8387 | areloc->addend -= areloc->address; /* addend is unsigned!! */ | |
8388 | } | |
8389 | } | |
8390 | else | |
8391 | { | |
8392 | switch (areloc->howto->bitsize) | |
8393 | { | |
8394 | case 8: | |
8395 | code = BFD_RELOC_8; | |
8396 | break; | |
8397 | case 14: | |
8398 | code = BFD_RELOC_14; | |
8399 | break; | |
8400 | case 16: | |
8401 | code = BFD_RELOC_16; | |
8402 | break; | |
8403 | case 26: | |
8404 | code = BFD_RELOC_26; | |
8405 | break; | |
8406 | case 32: | |
8407 | code = BFD_RELOC_32; | |
8408 | break; | |
8409 | case 64: | |
8410 | code = BFD_RELOC_64; | |
8411 | break; | |
8412 | default: | |
8413 | goto fail; | |
8414 | } | |
8415 | ||
8416 | howto = bfd_reloc_type_lookup (abfd, code); | |
8417 | } | |
8418 | ||
8419 | if (howto) | |
8420 | areloc->howto = howto; | |
8421 | else | |
8422 | goto fail; | |
8423 | } | |
8424 | ||
b34976b6 | 8425 | return TRUE; |
252b5132 RH |
8426 | |
8427 | fail: | |
8428 | (*_bfd_error_handler) | |
d003868e AM |
8429 | (_("%B: unsupported relocation type %s"), |
8430 | abfd, areloc->howto->name); | |
252b5132 | 8431 | bfd_set_error (bfd_error_bad_value); |
b34976b6 | 8432 | return FALSE; |
252b5132 RH |
8433 | } |
8434 | ||
b34976b6 | 8435 | bfd_boolean |
217aa764 | 8436 | _bfd_elf_close_and_cleanup (bfd *abfd) |
252b5132 | 8437 | { |
d9071b0c TG |
8438 | struct elf_obj_tdata *tdata = elf_tdata (abfd); |
8439 | if (bfd_get_format (abfd) == bfd_object && tdata != NULL) | |
252b5132 | 8440 | { |
c0355132 | 8441 | if (elf_tdata (abfd)->o != NULL && elf_shstrtab (abfd) != NULL) |
2b0f7ef9 | 8442 | _bfd_elf_strtab_free (elf_shstrtab (abfd)); |
d9071b0c | 8443 | _bfd_dwarf2_cleanup_debug_info (abfd, &tdata->dwarf2_find_line_info); |
252b5132 RH |
8444 | } |
8445 | ||
8446 | return _bfd_generic_close_and_cleanup (abfd); | |
8447 | } | |
8448 | ||
8449 | /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY | |
8450 | in the relocation's offset. Thus we cannot allow any sort of sanity | |
8451 | range-checking to interfere. There is nothing else to do in processing | |
8452 | this reloc. */ | |
8453 | ||
8454 | bfd_reloc_status_type | |
217aa764 AM |
8455 | _bfd_elf_rel_vtable_reloc_fn |
8456 | (bfd *abfd ATTRIBUTE_UNUSED, arelent *re ATTRIBUTE_UNUSED, | |
fc0a2244 | 8457 | struct bfd_symbol *symbol ATTRIBUTE_UNUSED, |
217aa764 AM |
8458 | void *data ATTRIBUTE_UNUSED, asection *is ATTRIBUTE_UNUSED, |
8459 | bfd *obfd ATTRIBUTE_UNUSED, char **errmsg ATTRIBUTE_UNUSED) | |
252b5132 RH |
8460 | { |
8461 | return bfd_reloc_ok; | |
8462 | } | |
252b5132 RH |
8463 | \f |
8464 | /* Elf core file support. Much of this only works on native | |
8465 | toolchains, since we rely on knowing the | |
8466 | machine-dependent procfs structure in order to pick | |
c044fabd | 8467 | out details about the corefile. */ |
252b5132 RH |
8468 | |
8469 | #ifdef HAVE_SYS_PROCFS_H | |
16231b7b DG |
8470 | /* Needed for new procfs interface on sparc-solaris. */ |
8471 | # define _STRUCTURED_PROC 1 | |
252b5132 RH |
8472 | # include <sys/procfs.h> |
8473 | #endif | |
8474 | ||
261b8d08 PA |
8475 | /* Return a PID that identifies a "thread" for threaded cores, or the |
8476 | PID of the main process for non-threaded cores. */ | |
252b5132 RH |
8477 | |
8478 | static int | |
217aa764 | 8479 | elfcore_make_pid (bfd *abfd) |
252b5132 | 8480 | { |
261b8d08 PA |
8481 | int pid; |
8482 | ||
228e534f | 8483 | pid = elf_tdata (abfd)->core->lwpid; |
261b8d08 | 8484 | if (pid == 0) |
228e534f | 8485 | pid = elf_tdata (abfd)->core->pid; |
261b8d08 PA |
8486 | |
8487 | return pid; | |
252b5132 RH |
8488 | } |
8489 | ||
252b5132 RH |
8490 | /* If there isn't a section called NAME, make one, using |
8491 | data from SECT. Note, this function will generate a | |
8492 | reference to NAME, so you shouldn't deallocate or | |
c044fabd | 8493 | overwrite it. */ |
252b5132 | 8494 | |
b34976b6 | 8495 | static bfd_boolean |
217aa764 | 8496 | elfcore_maybe_make_sect (bfd *abfd, char *name, asection *sect) |
252b5132 | 8497 | { |
c044fabd | 8498 | asection *sect2; |
252b5132 RH |
8499 | |
8500 | if (bfd_get_section_by_name (abfd, name) != NULL) | |
b34976b6 | 8501 | return TRUE; |
252b5132 | 8502 | |
117ed4f8 | 8503 | sect2 = bfd_make_section_with_flags (abfd, name, sect->flags); |
252b5132 | 8504 | if (sect2 == NULL) |
b34976b6 | 8505 | return FALSE; |
252b5132 | 8506 | |
eea6121a | 8507 | sect2->size = sect->size; |
252b5132 | 8508 | sect2->filepos = sect->filepos; |
252b5132 | 8509 | sect2->alignment_power = sect->alignment_power; |
b34976b6 | 8510 | return TRUE; |
252b5132 RH |
8511 | } |
8512 | ||
bb0082d6 AM |
8513 | /* Create a pseudosection containing SIZE bytes at FILEPOS. This |
8514 | actually creates up to two pseudosections: | |
8515 | - For the single-threaded case, a section named NAME, unless | |
8516 | such a section already exists. | |
8517 | - For the multi-threaded case, a section named "NAME/PID", where | |
8518 | PID is elfcore_make_pid (abfd). | |
8519 | Both pseudosections have identical contents. */ | |
b34976b6 | 8520 | bfd_boolean |
217aa764 AM |
8521 | _bfd_elfcore_make_pseudosection (bfd *abfd, |
8522 | char *name, | |
8523 | size_t size, | |
8524 | ufile_ptr filepos) | |
bb0082d6 AM |
8525 | { |
8526 | char buf[100]; | |
8527 | char *threaded_name; | |
d4c88bbb | 8528 | size_t len; |
bb0082d6 AM |
8529 | asection *sect; |
8530 | ||
8531 | /* Build the section name. */ | |
8532 | ||
8533 | sprintf (buf, "%s/%d", name, elfcore_make_pid (abfd)); | |
d4c88bbb | 8534 | len = strlen (buf) + 1; |
a50b1753 | 8535 | threaded_name = (char *) bfd_alloc (abfd, len); |
bb0082d6 | 8536 | if (threaded_name == NULL) |
b34976b6 | 8537 | return FALSE; |
d4c88bbb | 8538 | memcpy (threaded_name, buf, len); |
bb0082d6 | 8539 | |
117ed4f8 AM |
8540 | sect = bfd_make_section_anyway_with_flags (abfd, threaded_name, |
8541 | SEC_HAS_CONTENTS); | |
bb0082d6 | 8542 | if (sect == NULL) |
b34976b6 | 8543 | return FALSE; |
eea6121a | 8544 | sect->size = size; |
bb0082d6 | 8545 | sect->filepos = filepos; |
bb0082d6 AM |
8546 | sect->alignment_power = 2; |
8547 | ||
936e320b | 8548 | return elfcore_maybe_make_sect (abfd, name, sect); |
bb0082d6 AM |
8549 | } |
8550 | ||
252b5132 | 8551 | /* prstatus_t exists on: |
4a938328 | 8552 | solaris 2.5+ |
252b5132 RH |
8553 | linux 2.[01] + glibc |
8554 | unixware 4.2 | |
8555 | */ | |
8556 | ||
8557 | #if defined (HAVE_PRSTATUS_T) | |
a7b97311 | 8558 | |
b34976b6 | 8559 | static bfd_boolean |
217aa764 | 8560 | elfcore_grok_prstatus (bfd *abfd, Elf_Internal_Note *note) |
252b5132 | 8561 | { |
eea6121a | 8562 | size_t size; |
7ee38065 | 8563 | int offset; |
252b5132 | 8564 | |
4a938328 MS |
8565 | if (note->descsz == sizeof (prstatus_t)) |
8566 | { | |
8567 | prstatus_t prstat; | |
252b5132 | 8568 | |
eea6121a | 8569 | size = sizeof (prstat.pr_reg); |
7ee38065 | 8570 | offset = offsetof (prstatus_t, pr_reg); |
4a938328 | 8571 | memcpy (&prstat, note->descdata, sizeof (prstat)); |
252b5132 | 8572 | |
fa49d224 NC |
8573 | /* Do not overwrite the core signal if it |
8574 | has already been set by another thread. */ | |
228e534f AM |
8575 | if (elf_tdata (abfd)->core->signal == 0) |
8576 | elf_tdata (abfd)->core->signal = prstat.pr_cursig; | |
8577 | if (elf_tdata (abfd)->core->pid == 0) | |
8578 | elf_tdata (abfd)->core->pid = prstat.pr_pid; | |
252b5132 | 8579 | |
4a938328 MS |
8580 | /* pr_who exists on: |
8581 | solaris 2.5+ | |
8582 | unixware 4.2 | |
8583 | pr_who doesn't exist on: | |
8584 | linux 2.[01] | |
8585 | */ | |
252b5132 | 8586 | #if defined (HAVE_PRSTATUS_T_PR_WHO) |
228e534f | 8587 | elf_tdata (abfd)->core->lwpid = prstat.pr_who; |
261b8d08 | 8588 | #else |
228e534f | 8589 | elf_tdata (abfd)->core->lwpid = prstat.pr_pid; |
252b5132 | 8590 | #endif |
4a938328 | 8591 | } |
7ee38065 | 8592 | #if defined (HAVE_PRSTATUS32_T) |
4a938328 MS |
8593 | else if (note->descsz == sizeof (prstatus32_t)) |
8594 | { | |
8595 | /* 64-bit host, 32-bit corefile */ | |
8596 | prstatus32_t prstat; | |
8597 | ||
eea6121a | 8598 | size = sizeof (prstat.pr_reg); |
7ee38065 | 8599 | offset = offsetof (prstatus32_t, pr_reg); |
4a938328 MS |
8600 | memcpy (&prstat, note->descdata, sizeof (prstat)); |
8601 | ||
fa49d224 NC |
8602 | /* Do not overwrite the core signal if it |
8603 | has already been set by another thread. */ | |
228e534f AM |
8604 | if (elf_tdata (abfd)->core->signal == 0) |
8605 | elf_tdata (abfd)->core->signal = prstat.pr_cursig; | |
8606 | if (elf_tdata (abfd)->core->pid == 0) | |
8607 | elf_tdata (abfd)->core->pid = prstat.pr_pid; | |
4a938328 MS |
8608 | |
8609 | /* pr_who exists on: | |
8610 | solaris 2.5+ | |
8611 | unixware 4.2 | |
8612 | pr_who doesn't exist on: | |
8613 | linux 2.[01] | |
8614 | */ | |
7ee38065 | 8615 | #if defined (HAVE_PRSTATUS32_T_PR_WHO) |
228e534f | 8616 | elf_tdata (abfd)->core->lwpid = prstat.pr_who; |
261b8d08 | 8617 | #else |
228e534f | 8618 | elf_tdata (abfd)->core->lwpid = prstat.pr_pid; |
4a938328 MS |
8619 | #endif |
8620 | } | |
7ee38065 | 8621 | #endif /* HAVE_PRSTATUS32_T */ |
4a938328 MS |
8622 | else |
8623 | { | |
8624 | /* Fail - we don't know how to handle any other | |
8625 | note size (ie. data object type). */ | |
b34976b6 | 8626 | return TRUE; |
4a938328 | 8627 | } |
252b5132 | 8628 | |
bb0082d6 | 8629 | /* Make a ".reg/999" section and a ".reg" section. */ |
936e320b | 8630 | return _bfd_elfcore_make_pseudosection (abfd, ".reg", |
eea6121a | 8631 | size, note->descpos + offset); |
252b5132 RH |
8632 | } |
8633 | #endif /* defined (HAVE_PRSTATUS_T) */ | |
8634 | ||
bb0082d6 | 8635 | /* Create a pseudosection containing the exact contents of NOTE. */ |
b34976b6 | 8636 | static bfd_boolean |
217aa764 AM |
8637 | elfcore_make_note_pseudosection (bfd *abfd, |
8638 | char *name, | |
8639 | Elf_Internal_Note *note) | |
252b5132 | 8640 | { |
936e320b AM |
8641 | return _bfd_elfcore_make_pseudosection (abfd, name, |
8642 | note->descsz, note->descpos); | |
252b5132 RH |
8643 | } |
8644 | ||
ff08c6bb JB |
8645 | /* There isn't a consistent prfpregset_t across platforms, |
8646 | but it doesn't matter, because we don't have to pick this | |
c044fabd KH |
8647 | data structure apart. */ |
8648 | ||
b34976b6 | 8649 | static bfd_boolean |
217aa764 | 8650 | elfcore_grok_prfpreg (bfd *abfd, Elf_Internal_Note *note) |
ff08c6bb JB |
8651 | { |
8652 | return elfcore_make_note_pseudosection (abfd, ".reg2", note); | |
8653 | } | |
8654 | ||
ff08c6bb | 8655 | /* Linux dumps the Intel SSE regs in a note named "LINUX" with a note |
971d4640 | 8656 | type of NT_PRXFPREG. Just include the whole note's contents |
ff08c6bb | 8657 | literally. */ |
c044fabd | 8658 | |
b34976b6 | 8659 | static bfd_boolean |
217aa764 | 8660 | elfcore_grok_prxfpreg (bfd *abfd, Elf_Internal_Note *note) |
ff08c6bb JB |
8661 | { |
8662 | return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note); | |
8663 | } | |
8664 | ||
4339cae0 L |
8665 | /* Linux dumps the Intel XSAVE extended state in a note named "LINUX" |
8666 | with a note type of NT_X86_XSTATE. Just include the whole note's | |
8667 | contents literally. */ | |
8668 | ||
8669 | static bfd_boolean | |
8670 | elfcore_grok_xstatereg (bfd *abfd, Elf_Internal_Note *note) | |
8671 | { | |
8672 | return elfcore_make_note_pseudosection (abfd, ".reg-xstate", note); | |
8673 | } | |
8674 | ||
97753bd5 AM |
8675 | static bfd_boolean |
8676 | elfcore_grok_ppc_vmx (bfd *abfd, Elf_Internal_Note *note) | |
8677 | { | |
8678 | return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vmx", note); | |
8679 | } | |
8680 | ||
89eeb0bc LM |
8681 | static bfd_boolean |
8682 | elfcore_grok_ppc_vsx (bfd *abfd, Elf_Internal_Note *note) | |
8683 | { | |
8684 | return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vsx", note); | |
8685 | } | |
97753bd5 | 8686 | |
0675e188 UW |
8687 | static bfd_boolean |
8688 | elfcore_grok_s390_high_gprs (bfd *abfd, Elf_Internal_Note *note) | |
8689 | { | |
8690 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-high-gprs", note); | |
8691 | } | |
8692 | ||
d7eeb400 MS |
8693 | static bfd_boolean |
8694 | elfcore_grok_s390_timer (bfd *abfd, Elf_Internal_Note *note) | |
8695 | { | |
8696 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-timer", note); | |
8697 | } | |
8698 | ||
8699 | static bfd_boolean | |
8700 | elfcore_grok_s390_todcmp (bfd *abfd, Elf_Internal_Note *note) | |
8701 | { | |
8702 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-todcmp", note); | |
8703 | } | |
8704 | ||
8705 | static bfd_boolean | |
8706 | elfcore_grok_s390_todpreg (bfd *abfd, Elf_Internal_Note *note) | |
8707 | { | |
8708 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-todpreg", note); | |
8709 | } | |
8710 | ||
8711 | static bfd_boolean | |
8712 | elfcore_grok_s390_ctrs (bfd *abfd, Elf_Internal_Note *note) | |
8713 | { | |
8714 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-ctrs", note); | |
8715 | } | |
8716 | ||
8717 | static bfd_boolean | |
8718 | elfcore_grok_s390_prefix (bfd *abfd, Elf_Internal_Note *note) | |
8719 | { | |
8720 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-prefix", note); | |
8721 | } | |
8722 | ||
355b81d9 UW |
8723 | static bfd_boolean |
8724 | elfcore_grok_s390_last_break (bfd *abfd, Elf_Internal_Note *note) | |
8725 | { | |
8726 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-last-break", note); | |
8727 | } | |
8728 | ||
8729 | static bfd_boolean | |
8730 | elfcore_grok_s390_system_call (bfd *abfd, Elf_Internal_Note *note) | |
8731 | { | |
8732 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-system-call", note); | |
8733 | } | |
8734 | ||
abb3f6cc NC |
8735 | static bfd_boolean |
8736 | elfcore_grok_s390_tdb (bfd *abfd, Elf_Internal_Note *note) | |
8737 | { | |
8738 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-tdb", note); | |
8739 | } | |
8740 | ||
4ef9f41a AA |
8741 | static bfd_boolean |
8742 | elfcore_grok_s390_vxrs_low (bfd *abfd, Elf_Internal_Note *note) | |
8743 | { | |
8744 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-vxrs-low", note); | |
8745 | } | |
8746 | ||
8747 | static bfd_boolean | |
8748 | elfcore_grok_s390_vxrs_high (bfd *abfd, Elf_Internal_Note *note) | |
8749 | { | |
8750 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-vxrs-high", note); | |
8751 | } | |
8752 | ||
faa9a424 UW |
8753 | static bfd_boolean |
8754 | elfcore_grok_arm_vfp (bfd *abfd, Elf_Internal_Note *note) | |
8755 | { | |
8756 | return elfcore_make_note_pseudosection (abfd, ".reg-arm-vfp", note); | |
8757 | } | |
8758 | ||
652451f8 YZ |
8759 | static bfd_boolean |
8760 | elfcore_grok_aarch_tls (bfd *abfd, Elf_Internal_Note *note) | |
8761 | { | |
8762 | return elfcore_make_note_pseudosection (abfd, ".reg-aarch-tls", note); | |
8763 | } | |
8764 | ||
8765 | static bfd_boolean | |
8766 | elfcore_grok_aarch_hw_break (bfd *abfd, Elf_Internal_Note *note) | |
8767 | { | |
8768 | return elfcore_make_note_pseudosection (abfd, ".reg-aarch-hw-break", note); | |
8769 | } | |
8770 | ||
8771 | static bfd_boolean | |
8772 | elfcore_grok_aarch_hw_watch (bfd *abfd, Elf_Internal_Note *note) | |
8773 | { | |
8774 | return elfcore_make_note_pseudosection (abfd, ".reg-aarch-hw-watch", note); | |
8775 | } | |
8776 | ||
252b5132 | 8777 | #if defined (HAVE_PRPSINFO_T) |
4a938328 | 8778 | typedef prpsinfo_t elfcore_psinfo_t; |
7ee38065 | 8779 | #if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */ |
4a938328 MS |
8780 | typedef prpsinfo32_t elfcore_psinfo32_t; |
8781 | #endif | |
252b5132 RH |
8782 | #endif |
8783 | ||
8784 | #if defined (HAVE_PSINFO_T) | |
4a938328 | 8785 | typedef psinfo_t elfcore_psinfo_t; |
7ee38065 | 8786 | #if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */ |
4a938328 MS |
8787 | typedef psinfo32_t elfcore_psinfo32_t; |
8788 | #endif | |
252b5132 RH |
8789 | #endif |
8790 | ||
252b5132 RH |
8791 | /* return a malloc'ed copy of a string at START which is at |
8792 | most MAX bytes long, possibly without a terminating '\0'. | |
c044fabd | 8793 | the copy will always have a terminating '\0'. */ |
252b5132 | 8794 | |
936e320b | 8795 | char * |
217aa764 | 8796 | _bfd_elfcore_strndup (bfd *abfd, char *start, size_t max) |
252b5132 | 8797 | { |
dc810e39 | 8798 | char *dups; |
a50b1753 | 8799 | char *end = (char *) memchr (start, '\0', max); |
dc810e39 | 8800 | size_t len; |
252b5132 RH |
8801 | |
8802 | if (end == NULL) | |
8803 | len = max; | |
8804 | else | |
8805 | len = end - start; | |
8806 | ||
a50b1753 | 8807 | dups = (char *) bfd_alloc (abfd, len + 1); |
dc810e39 | 8808 | if (dups == NULL) |
252b5132 RH |
8809 | return NULL; |
8810 | ||
dc810e39 AM |
8811 | memcpy (dups, start, len); |
8812 | dups[len] = '\0'; | |
252b5132 | 8813 | |
dc810e39 | 8814 | return dups; |
252b5132 RH |
8815 | } |
8816 | ||
bb0082d6 | 8817 | #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) |
b34976b6 | 8818 | static bfd_boolean |
217aa764 | 8819 | elfcore_grok_psinfo (bfd *abfd, Elf_Internal_Note *note) |
252b5132 | 8820 | { |
4a938328 MS |
8821 | if (note->descsz == sizeof (elfcore_psinfo_t)) |
8822 | { | |
8823 | elfcore_psinfo_t psinfo; | |
252b5132 | 8824 | |
7ee38065 | 8825 | memcpy (&psinfo, note->descdata, sizeof (psinfo)); |
252b5132 | 8826 | |
335e41d4 | 8827 | #if defined (HAVE_PSINFO_T_PR_PID) || defined (HAVE_PRPSINFO_T_PR_PID) |
228e534f | 8828 | elf_tdata (abfd)->core->pid = psinfo.pr_pid; |
335e41d4 | 8829 | #endif |
228e534f | 8830 | elf_tdata (abfd)->core->program |
936e320b AM |
8831 | = _bfd_elfcore_strndup (abfd, psinfo.pr_fname, |
8832 | sizeof (psinfo.pr_fname)); | |
252b5132 | 8833 | |
228e534f | 8834 | elf_tdata (abfd)->core->command |
936e320b AM |
8835 | = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs, |
8836 | sizeof (psinfo.pr_psargs)); | |
4a938328 | 8837 | } |
7ee38065 | 8838 | #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T) |
4a938328 MS |
8839 | else if (note->descsz == sizeof (elfcore_psinfo32_t)) |
8840 | { | |
8841 | /* 64-bit host, 32-bit corefile */ | |
8842 | elfcore_psinfo32_t psinfo; | |
8843 | ||
7ee38065 | 8844 | memcpy (&psinfo, note->descdata, sizeof (psinfo)); |
252b5132 | 8845 | |
335e41d4 | 8846 | #if defined (HAVE_PSINFO32_T_PR_PID) || defined (HAVE_PRPSINFO32_T_PR_PID) |
228e534f | 8847 | elf_tdata (abfd)->core->pid = psinfo.pr_pid; |
335e41d4 | 8848 | #endif |
228e534f | 8849 | elf_tdata (abfd)->core->program |
936e320b AM |
8850 | = _bfd_elfcore_strndup (abfd, psinfo.pr_fname, |
8851 | sizeof (psinfo.pr_fname)); | |
4a938328 | 8852 | |
228e534f | 8853 | elf_tdata (abfd)->core->command |
936e320b AM |
8854 | = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs, |
8855 | sizeof (psinfo.pr_psargs)); | |
4a938328 MS |
8856 | } |
8857 | #endif | |
8858 | ||
8859 | else | |
8860 | { | |
8861 | /* Fail - we don't know how to handle any other | |
8862 | note size (ie. data object type). */ | |
b34976b6 | 8863 | return TRUE; |
4a938328 | 8864 | } |
252b5132 RH |
8865 | |
8866 | /* Note that for some reason, a spurious space is tacked | |
8867 | onto the end of the args in some (at least one anyway) | |
c044fabd | 8868 | implementations, so strip it off if it exists. */ |
252b5132 RH |
8869 | |
8870 | { | |
228e534f | 8871 | char *command = elf_tdata (abfd)->core->command; |
252b5132 RH |
8872 | int n = strlen (command); |
8873 | ||
8874 | if (0 < n && command[n - 1] == ' ') | |
8875 | command[n - 1] = '\0'; | |
8876 | } | |
8877 | ||
b34976b6 | 8878 | return TRUE; |
252b5132 RH |
8879 | } |
8880 | #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */ | |
8881 | ||
252b5132 | 8882 | #if defined (HAVE_PSTATUS_T) |
b34976b6 | 8883 | static bfd_boolean |
217aa764 | 8884 | elfcore_grok_pstatus (bfd *abfd, Elf_Internal_Note *note) |
252b5132 | 8885 | { |
f572a39d AM |
8886 | if (note->descsz == sizeof (pstatus_t) |
8887 | #if defined (HAVE_PXSTATUS_T) | |
8888 | || note->descsz == sizeof (pxstatus_t) | |
8889 | #endif | |
8890 | ) | |
4a938328 MS |
8891 | { |
8892 | pstatus_t pstat; | |
252b5132 | 8893 | |
4a938328 | 8894 | memcpy (&pstat, note->descdata, sizeof (pstat)); |
252b5132 | 8895 | |
228e534f | 8896 | elf_tdata (abfd)->core->pid = pstat.pr_pid; |
4a938328 | 8897 | } |
7ee38065 | 8898 | #if defined (HAVE_PSTATUS32_T) |
4a938328 MS |
8899 | else if (note->descsz == sizeof (pstatus32_t)) |
8900 | { | |
8901 | /* 64-bit host, 32-bit corefile */ | |
8902 | pstatus32_t pstat; | |
252b5132 | 8903 | |
4a938328 | 8904 | memcpy (&pstat, note->descdata, sizeof (pstat)); |
252b5132 | 8905 | |
228e534f | 8906 | elf_tdata (abfd)->core->pid = pstat.pr_pid; |
4a938328 MS |
8907 | } |
8908 | #endif | |
252b5132 RH |
8909 | /* Could grab some more details from the "representative" |
8910 | lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an | |
c044fabd | 8911 | NT_LWPSTATUS note, presumably. */ |
252b5132 | 8912 | |
b34976b6 | 8913 | return TRUE; |
252b5132 RH |
8914 | } |
8915 | #endif /* defined (HAVE_PSTATUS_T) */ | |
8916 | ||
252b5132 | 8917 | #if defined (HAVE_LWPSTATUS_T) |
b34976b6 | 8918 | static bfd_boolean |
217aa764 | 8919 | elfcore_grok_lwpstatus (bfd *abfd, Elf_Internal_Note *note) |
252b5132 RH |
8920 | { |
8921 | lwpstatus_t lwpstat; | |
8922 | char buf[100]; | |
c044fabd | 8923 | char *name; |
d4c88bbb | 8924 | size_t len; |
c044fabd | 8925 | asection *sect; |
252b5132 | 8926 | |
f572a39d AM |
8927 | if (note->descsz != sizeof (lwpstat) |
8928 | #if defined (HAVE_LWPXSTATUS_T) | |
8929 | && note->descsz != sizeof (lwpxstatus_t) | |
8930 | #endif | |
8931 | ) | |
b34976b6 | 8932 | return TRUE; |
252b5132 RH |
8933 | |
8934 | memcpy (&lwpstat, note->descdata, sizeof (lwpstat)); | |
8935 | ||
228e534f | 8936 | elf_tdata (abfd)->core->lwpid = lwpstat.pr_lwpid; |
a1504221 JB |
8937 | /* Do not overwrite the core signal if it has already been set by |
8938 | another thread. */ | |
228e534f AM |
8939 | if (elf_tdata (abfd)->core->signal == 0) |
8940 | elf_tdata (abfd)->core->signal = lwpstat.pr_cursig; | |
252b5132 | 8941 | |
c044fabd | 8942 | /* Make a ".reg/999" section. */ |
252b5132 RH |
8943 | |
8944 | sprintf (buf, ".reg/%d", elfcore_make_pid (abfd)); | |
d4c88bbb | 8945 | len = strlen (buf) + 1; |
217aa764 | 8946 | name = bfd_alloc (abfd, len); |
252b5132 | 8947 | if (name == NULL) |
b34976b6 | 8948 | return FALSE; |
d4c88bbb | 8949 | memcpy (name, buf, len); |
252b5132 | 8950 | |
117ed4f8 | 8951 | sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); |
252b5132 | 8952 | if (sect == NULL) |
b34976b6 | 8953 | return FALSE; |
252b5132 RH |
8954 | |
8955 | #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT) | |
eea6121a | 8956 | sect->size = sizeof (lwpstat.pr_context.uc_mcontext.gregs); |
252b5132 RH |
8957 | sect->filepos = note->descpos |
8958 | + offsetof (lwpstatus_t, pr_context.uc_mcontext.gregs); | |
8959 | #endif | |
8960 | ||
8961 | #if defined (HAVE_LWPSTATUS_T_PR_REG) | |
eea6121a | 8962 | sect->size = sizeof (lwpstat.pr_reg); |
252b5132 RH |
8963 | sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_reg); |
8964 | #endif | |
8965 | ||
252b5132 RH |
8966 | sect->alignment_power = 2; |
8967 | ||
8968 | if (!elfcore_maybe_make_sect (abfd, ".reg", sect)) | |
b34976b6 | 8969 | return FALSE; |
252b5132 RH |
8970 | |
8971 | /* Make a ".reg2/999" section */ | |
8972 | ||
8973 | sprintf (buf, ".reg2/%d", elfcore_make_pid (abfd)); | |
d4c88bbb | 8974 | len = strlen (buf) + 1; |
217aa764 | 8975 | name = bfd_alloc (abfd, len); |
252b5132 | 8976 | if (name == NULL) |
b34976b6 | 8977 | return FALSE; |
d4c88bbb | 8978 | memcpy (name, buf, len); |
252b5132 | 8979 | |
117ed4f8 | 8980 | sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); |
252b5132 | 8981 | if (sect == NULL) |
b34976b6 | 8982 | return FALSE; |
252b5132 RH |
8983 | |
8984 | #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT) | |
eea6121a | 8985 | sect->size = sizeof (lwpstat.pr_context.uc_mcontext.fpregs); |
252b5132 RH |
8986 | sect->filepos = note->descpos |
8987 | + offsetof (lwpstatus_t, pr_context.uc_mcontext.fpregs); | |
8988 | #endif | |
8989 | ||
8990 | #if defined (HAVE_LWPSTATUS_T_PR_FPREG) | |
eea6121a | 8991 | sect->size = sizeof (lwpstat.pr_fpreg); |
252b5132 RH |
8992 | sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_fpreg); |
8993 | #endif | |
8994 | ||
252b5132 RH |
8995 | sect->alignment_power = 2; |
8996 | ||
936e320b | 8997 | return elfcore_maybe_make_sect (abfd, ".reg2", sect); |
252b5132 RH |
8998 | } |
8999 | #endif /* defined (HAVE_LWPSTATUS_T) */ | |
9000 | ||
b34976b6 | 9001 | static bfd_boolean |
217aa764 | 9002 | elfcore_grok_win32pstatus (bfd *abfd, Elf_Internal_Note *note) |
16e9c715 NC |
9003 | { |
9004 | char buf[30]; | |
c044fabd | 9005 | char *name; |
d4c88bbb | 9006 | size_t len; |
c044fabd | 9007 | asection *sect; |
4a6636fb PA |
9008 | int type; |
9009 | int is_active_thread; | |
9010 | bfd_vma base_addr; | |
16e9c715 | 9011 | |
4a6636fb | 9012 | if (note->descsz < 728) |
b34976b6 | 9013 | return TRUE; |
16e9c715 | 9014 | |
4a6636fb PA |
9015 | if (! CONST_STRNEQ (note->namedata, "win32")) |
9016 | return TRUE; | |
9017 | ||
9018 | type = bfd_get_32 (abfd, note->descdata); | |
c044fabd | 9019 | |
4a6636fb | 9020 | switch (type) |
16e9c715 | 9021 | { |
4a6636fb | 9022 | case 1 /* NOTE_INFO_PROCESS */: |
228e534f | 9023 | /* FIXME: need to add ->core->command. */ |
4a6636fb | 9024 | /* process_info.pid */ |
228e534f | 9025 | elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, note->descdata + 8); |
4a6636fb | 9026 | /* process_info.signal */ |
228e534f | 9027 | elf_tdata (abfd)->core->signal = bfd_get_32 (abfd, note->descdata + 12); |
c044fabd | 9028 | break; |
16e9c715 | 9029 | |
4a6636fb | 9030 | case 2 /* NOTE_INFO_THREAD */: |
16e9c715 | 9031 | /* Make a ".reg/999" section. */ |
4a6636fb PA |
9032 | /* thread_info.tid */ |
9033 | sprintf (buf, ".reg/%ld", (long) bfd_get_32 (abfd, note->descdata + 8)); | |
c044fabd | 9034 | |
d4c88bbb | 9035 | len = strlen (buf) + 1; |
a50b1753 | 9036 | name = (char *) bfd_alloc (abfd, len); |
16e9c715 | 9037 | if (name == NULL) |
b34976b6 | 9038 | return FALSE; |
c044fabd | 9039 | |
d4c88bbb | 9040 | memcpy (name, buf, len); |
16e9c715 | 9041 | |
117ed4f8 | 9042 | sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); |
16e9c715 | 9043 | if (sect == NULL) |
b34976b6 | 9044 | return FALSE; |
c044fabd | 9045 | |
4a6636fb PA |
9046 | /* sizeof (thread_info.thread_context) */ |
9047 | sect->size = 716; | |
9048 | /* offsetof (thread_info.thread_context) */ | |
9049 | sect->filepos = note->descpos + 12; | |
16e9c715 NC |
9050 | sect->alignment_power = 2; |
9051 | ||
4a6636fb PA |
9052 | /* thread_info.is_active_thread */ |
9053 | is_active_thread = bfd_get_32 (abfd, note->descdata + 8); | |
9054 | ||
9055 | if (is_active_thread) | |
16e9c715 | 9056 | if (! elfcore_maybe_make_sect (abfd, ".reg", sect)) |
b34976b6 | 9057 | return FALSE; |
16e9c715 NC |
9058 | break; |
9059 | ||
4a6636fb | 9060 | case 3 /* NOTE_INFO_MODULE */: |
16e9c715 | 9061 | /* Make a ".module/xxxxxxxx" section. */ |
4a6636fb PA |
9062 | /* module_info.base_address */ |
9063 | base_addr = bfd_get_32 (abfd, note->descdata + 4); | |
0af1713e | 9064 | sprintf (buf, ".module/%08lx", (unsigned long) base_addr); |
c044fabd | 9065 | |
d4c88bbb | 9066 | len = strlen (buf) + 1; |
a50b1753 | 9067 | name = (char *) bfd_alloc (abfd, len); |
16e9c715 | 9068 | if (name == NULL) |
b34976b6 | 9069 | return FALSE; |
c044fabd | 9070 | |
d4c88bbb | 9071 | memcpy (name, buf, len); |
252b5132 | 9072 | |
117ed4f8 | 9073 | sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); |
c044fabd | 9074 | |
16e9c715 | 9075 | if (sect == NULL) |
b34976b6 | 9076 | return FALSE; |
c044fabd | 9077 | |
eea6121a | 9078 | sect->size = note->descsz; |
16e9c715 | 9079 | sect->filepos = note->descpos; |
16e9c715 NC |
9080 | sect->alignment_power = 2; |
9081 | break; | |
9082 | ||
9083 | default: | |
b34976b6 | 9084 | return TRUE; |
16e9c715 NC |
9085 | } |
9086 | ||
b34976b6 | 9087 | return TRUE; |
16e9c715 | 9088 | } |
252b5132 | 9089 | |
b34976b6 | 9090 | static bfd_boolean |
217aa764 | 9091 | elfcore_grok_note (bfd *abfd, Elf_Internal_Note *note) |
252b5132 | 9092 | { |
9c5bfbb7 | 9093 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
bb0082d6 | 9094 | |
252b5132 RH |
9095 | switch (note->type) |
9096 | { | |
9097 | default: | |
b34976b6 | 9098 | return TRUE; |
252b5132 | 9099 | |
252b5132 | 9100 | case NT_PRSTATUS: |
bb0082d6 AM |
9101 | if (bed->elf_backend_grok_prstatus) |
9102 | if ((*bed->elf_backend_grok_prstatus) (abfd, note)) | |
b34976b6 | 9103 | return TRUE; |
bb0082d6 | 9104 | #if defined (HAVE_PRSTATUS_T) |
252b5132 | 9105 | return elfcore_grok_prstatus (abfd, note); |
bb0082d6 | 9106 | #else |
b34976b6 | 9107 | return TRUE; |
252b5132 RH |
9108 | #endif |
9109 | ||
9110 | #if defined (HAVE_PSTATUS_T) | |
9111 | case NT_PSTATUS: | |
9112 | return elfcore_grok_pstatus (abfd, note); | |
9113 | #endif | |
9114 | ||
9115 | #if defined (HAVE_LWPSTATUS_T) | |
9116 | case NT_LWPSTATUS: | |
9117 | return elfcore_grok_lwpstatus (abfd, note); | |
9118 | #endif | |
9119 | ||
9120 | case NT_FPREGSET: /* FIXME: rename to NT_PRFPREG */ | |
9121 | return elfcore_grok_prfpreg (abfd, note); | |
9122 | ||
c044fabd | 9123 | case NT_WIN32PSTATUS: |
16e9c715 | 9124 | return elfcore_grok_win32pstatus (abfd, note); |
16e9c715 | 9125 | |
c044fabd | 9126 | case NT_PRXFPREG: /* Linux SSE extension */ |
e377ab71 MK |
9127 | if (note->namesz == 6 |
9128 | && strcmp (note->namedata, "LINUX") == 0) | |
ff08c6bb JB |
9129 | return elfcore_grok_prxfpreg (abfd, note); |
9130 | else | |
b34976b6 | 9131 | return TRUE; |
ff08c6bb | 9132 | |
4339cae0 L |
9133 | case NT_X86_XSTATE: /* Linux XSAVE extension */ |
9134 | if (note->namesz == 6 | |
9135 | && strcmp (note->namedata, "LINUX") == 0) | |
9136 | return elfcore_grok_xstatereg (abfd, note); | |
97de3545 JB |
9137 | else if (note->namesz == 8 |
9138 | && strcmp (note->namedata, "FreeBSD") == 0) | |
9139 | return elfcore_grok_xstatereg (abfd, note); | |
4339cae0 L |
9140 | else |
9141 | return TRUE; | |
9142 | ||
97753bd5 AM |
9143 | case NT_PPC_VMX: |
9144 | if (note->namesz == 6 | |
9145 | && strcmp (note->namedata, "LINUX") == 0) | |
9146 | return elfcore_grok_ppc_vmx (abfd, note); | |
9147 | else | |
9148 | return TRUE; | |
9149 | ||
89eeb0bc LM |
9150 | case NT_PPC_VSX: |
9151 | if (note->namesz == 6 | |
9152 | && strcmp (note->namedata, "LINUX") == 0) | |
9153 | return elfcore_grok_ppc_vsx (abfd, note); | |
9154 | else | |
9155 | return TRUE; | |
9156 | ||
0675e188 UW |
9157 | case NT_S390_HIGH_GPRS: |
9158 | if (note->namesz == 6 | |
9159 | && strcmp (note->namedata, "LINUX") == 0) | |
9160 | return elfcore_grok_s390_high_gprs (abfd, note); | |
9161 | else | |
9162 | return TRUE; | |
9163 | ||
d7eeb400 MS |
9164 | case NT_S390_TIMER: |
9165 | if (note->namesz == 6 | |
9166 | && strcmp (note->namedata, "LINUX") == 0) | |
9167 | return elfcore_grok_s390_timer (abfd, note); | |
9168 | else | |
9169 | return TRUE; | |
9170 | ||
9171 | case NT_S390_TODCMP: | |
9172 | if (note->namesz == 6 | |
9173 | && strcmp (note->namedata, "LINUX") == 0) | |
9174 | return elfcore_grok_s390_todcmp (abfd, note); | |
9175 | else | |
9176 | return TRUE; | |
9177 | ||
9178 | case NT_S390_TODPREG: | |
9179 | if (note->namesz == 6 | |
9180 | && strcmp (note->namedata, "LINUX") == 0) | |
9181 | return elfcore_grok_s390_todpreg (abfd, note); | |
9182 | else | |
9183 | return TRUE; | |
9184 | ||
9185 | case NT_S390_CTRS: | |
9186 | if (note->namesz == 6 | |
9187 | && strcmp (note->namedata, "LINUX") == 0) | |
9188 | return elfcore_grok_s390_ctrs (abfd, note); | |
9189 | else | |
9190 | return TRUE; | |
9191 | ||
9192 | case NT_S390_PREFIX: | |
9193 | if (note->namesz == 6 | |
9194 | && strcmp (note->namedata, "LINUX") == 0) | |
9195 | return elfcore_grok_s390_prefix (abfd, note); | |
9196 | else | |
9197 | return TRUE; | |
9198 | ||
355b81d9 UW |
9199 | case NT_S390_LAST_BREAK: |
9200 | if (note->namesz == 6 | |
9201 | && strcmp (note->namedata, "LINUX") == 0) | |
9202 | return elfcore_grok_s390_last_break (abfd, note); | |
9203 | else | |
9204 | return TRUE; | |
9205 | ||
9206 | case NT_S390_SYSTEM_CALL: | |
9207 | if (note->namesz == 6 | |
9208 | && strcmp (note->namedata, "LINUX") == 0) | |
9209 | return elfcore_grok_s390_system_call (abfd, note); | |
9210 | else | |
9211 | return TRUE; | |
9212 | ||
abb3f6cc NC |
9213 | case NT_S390_TDB: |
9214 | if (note->namesz == 6 | |
9215 | && strcmp (note->namedata, "LINUX") == 0) | |
9216 | return elfcore_grok_s390_tdb (abfd, note); | |
9217 | else | |
9218 | return TRUE; | |
9219 | ||
4ef9f41a AA |
9220 | case NT_S390_VXRS_LOW: |
9221 | if (note->namesz == 6 | |
9222 | && strcmp (note->namedata, "LINUX") == 0) | |
9223 | return elfcore_grok_s390_vxrs_low (abfd, note); | |
9224 | else | |
9225 | return TRUE; | |
9226 | ||
9227 | case NT_S390_VXRS_HIGH: | |
9228 | if (note->namesz == 6 | |
9229 | && strcmp (note->namedata, "LINUX") == 0) | |
9230 | return elfcore_grok_s390_vxrs_high (abfd, note); | |
9231 | else | |
9232 | return TRUE; | |
9233 | ||
faa9a424 UW |
9234 | case NT_ARM_VFP: |
9235 | if (note->namesz == 6 | |
9236 | && strcmp (note->namedata, "LINUX") == 0) | |
9237 | return elfcore_grok_arm_vfp (abfd, note); | |
9238 | else | |
9239 | return TRUE; | |
9240 | ||
652451f8 YZ |
9241 | case NT_ARM_TLS: |
9242 | if (note->namesz == 6 | |
9243 | && strcmp (note->namedata, "LINUX") == 0) | |
9244 | return elfcore_grok_aarch_tls (abfd, note); | |
9245 | else | |
9246 | return TRUE; | |
9247 | ||
9248 | case NT_ARM_HW_BREAK: | |
9249 | if (note->namesz == 6 | |
9250 | && strcmp (note->namedata, "LINUX") == 0) | |
9251 | return elfcore_grok_aarch_hw_break (abfd, note); | |
9252 | else | |
9253 | return TRUE; | |
9254 | ||
9255 | case NT_ARM_HW_WATCH: | |
9256 | if (note->namesz == 6 | |
9257 | && strcmp (note->namedata, "LINUX") == 0) | |
9258 | return elfcore_grok_aarch_hw_watch (abfd, note); | |
9259 | else | |
9260 | return TRUE; | |
9261 | ||
252b5132 RH |
9262 | case NT_PRPSINFO: |
9263 | case NT_PSINFO: | |
bb0082d6 AM |
9264 | if (bed->elf_backend_grok_psinfo) |
9265 | if ((*bed->elf_backend_grok_psinfo) (abfd, note)) | |
b34976b6 | 9266 | return TRUE; |
bb0082d6 | 9267 | #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) |
252b5132 | 9268 | return elfcore_grok_psinfo (abfd, note); |
bb0082d6 | 9269 | #else |
b34976b6 | 9270 | return TRUE; |
252b5132 | 9271 | #endif |
3333a7c3 RM |
9272 | |
9273 | case NT_AUXV: | |
9274 | { | |
117ed4f8 AM |
9275 | asection *sect = bfd_make_section_anyway_with_flags (abfd, ".auxv", |
9276 | SEC_HAS_CONTENTS); | |
3333a7c3 RM |
9277 | |
9278 | if (sect == NULL) | |
9279 | return FALSE; | |
eea6121a | 9280 | sect->size = note->descsz; |
3333a7c3 | 9281 | sect->filepos = note->descpos; |
3333a7c3 RM |
9282 | sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32; |
9283 | ||
9284 | return TRUE; | |
9285 | } | |
9015683b | 9286 | |
451b7c33 TT |
9287 | case NT_FILE: |
9288 | return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.file", | |
9289 | note); | |
9290 | ||
9015683b TT |
9291 | case NT_SIGINFO: |
9292 | return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.siginfo", | |
9293 | note); | |
252b5132 RH |
9294 | } |
9295 | } | |
9296 | ||
718175fa JK |
9297 | static bfd_boolean |
9298 | elfobj_grok_gnu_build_id (bfd *abfd, Elf_Internal_Note *note) | |
9299 | { | |
c74f7d1c | 9300 | struct bfd_build_id* build_id; |
30e8ee25 AM |
9301 | |
9302 | if (note->descsz == 0) | |
9303 | return FALSE; | |
9304 | ||
c74f7d1c JT |
9305 | build_id = bfd_alloc (abfd, sizeof (struct bfd_build_id) - 1 + note->descsz); |
9306 | if (build_id == NULL) | |
718175fa JK |
9307 | return FALSE; |
9308 | ||
c74f7d1c JT |
9309 | build_id->size = note->descsz; |
9310 | memcpy (build_id->data, note->descdata, note->descsz); | |
9311 | abfd->build_id = build_id; | |
718175fa JK |
9312 | |
9313 | return TRUE; | |
9314 | } | |
9315 | ||
9316 | static bfd_boolean | |
9317 | elfobj_grok_gnu_note (bfd *abfd, Elf_Internal_Note *note) | |
9318 | { | |
9319 | switch (note->type) | |
9320 | { | |
9321 | default: | |
9322 | return TRUE; | |
9323 | ||
9324 | case NT_GNU_BUILD_ID: | |
9325 | return elfobj_grok_gnu_build_id (abfd, note); | |
9326 | } | |
9327 | } | |
9328 | ||
e21e5835 NC |
9329 | static bfd_boolean |
9330 | elfobj_grok_stapsdt_note_1 (bfd *abfd, Elf_Internal_Note *note) | |
9331 | { | |
9332 | struct sdt_note *cur = | |
9333 | (struct sdt_note *) bfd_alloc (abfd, sizeof (struct sdt_note) | |
9334 | + note->descsz); | |
9335 | ||
9336 | cur->next = (struct sdt_note *) (elf_tdata (abfd))->sdt_note_head; | |
9337 | cur->size = (bfd_size_type) note->descsz; | |
9338 | memcpy (cur->data, note->descdata, note->descsz); | |
9339 | ||
9340 | elf_tdata (abfd)->sdt_note_head = cur; | |
9341 | ||
9342 | return TRUE; | |
9343 | } | |
9344 | ||
9345 | static bfd_boolean | |
9346 | elfobj_grok_stapsdt_note (bfd *abfd, Elf_Internal_Note *note) | |
9347 | { | |
9348 | switch (note->type) | |
9349 | { | |
9350 | case NT_STAPSDT: | |
9351 | return elfobj_grok_stapsdt_note_1 (abfd, note); | |
9352 | ||
9353 | default: | |
9354 | return TRUE; | |
9355 | } | |
9356 | } | |
9357 | ||
b34976b6 | 9358 | static bfd_boolean |
217aa764 | 9359 | elfcore_netbsd_get_lwpid (Elf_Internal_Note *note, int *lwpidp) |
50b2bdb7 AM |
9360 | { |
9361 | char *cp; | |
9362 | ||
9363 | cp = strchr (note->namedata, '@'); | |
9364 | if (cp != NULL) | |
9365 | { | |
d2b64500 | 9366 | *lwpidp = atoi(cp + 1); |
b34976b6 | 9367 | return TRUE; |
50b2bdb7 | 9368 | } |
b34976b6 | 9369 | return FALSE; |
50b2bdb7 AM |
9370 | } |
9371 | ||
b34976b6 | 9372 | static bfd_boolean |
217aa764 | 9373 | elfcore_grok_netbsd_procinfo (bfd *abfd, Elf_Internal_Note *note) |
50b2bdb7 | 9374 | { |
50b2bdb7 | 9375 | /* Signal number at offset 0x08. */ |
228e534f | 9376 | elf_tdata (abfd)->core->signal |
50b2bdb7 AM |
9377 | = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08); |
9378 | ||
9379 | /* Process ID at offset 0x50. */ | |
228e534f | 9380 | elf_tdata (abfd)->core->pid |
50b2bdb7 AM |
9381 | = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x50); |
9382 | ||
9383 | /* Command name at 0x7c (max 32 bytes, including nul). */ | |
228e534f | 9384 | elf_tdata (abfd)->core->command |
50b2bdb7 AM |
9385 | = _bfd_elfcore_strndup (abfd, note->descdata + 0x7c, 31); |
9386 | ||
7720ba9f MK |
9387 | return elfcore_make_note_pseudosection (abfd, ".note.netbsdcore.procinfo", |
9388 | note); | |
50b2bdb7 AM |
9389 | } |
9390 | ||
b34976b6 | 9391 | static bfd_boolean |
217aa764 | 9392 | elfcore_grok_netbsd_note (bfd *abfd, Elf_Internal_Note *note) |
50b2bdb7 AM |
9393 | { |
9394 | int lwp; | |
9395 | ||
9396 | if (elfcore_netbsd_get_lwpid (note, &lwp)) | |
228e534f | 9397 | elf_tdata (abfd)->core->lwpid = lwp; |
50b2bdb7 | 9398 | |
b4db1224 | 9399 | if (note->type == NT_NETBSDCORE_PROCINFO) |
50b2bdb7 AM |
9400 | { |
9401 | /* NetBSD-specific core "procinfo". Note that we expect to | |
08a40648 AM |
9402 | find this note before any of the others, which is fine, |
9403 | since the kernel writes this note out first when it | |
9404 | creates a core file. */ | |
47d9a591 | 9405 | |
50b2bdb7 AM |
9406 | return elfcore_grok_netbsd_procinfo (abfd, note); |
9407 | } | |
9408 | ||
b4db1224 JT |
9409 | /* As of Jan 2002 there are no other machine-independent notes |
9410 | defined for NetBSD core files. If the note type is less | |
9411 | than the start of the machine-dependent note types, we don't | |
9412 | understand it. */ | |
47d9a591 | 9413 | |
b4db1224 | 9414 | if (note->type < NT_NETBSDCORE_FIRSTMACH) |
b34976b6 | 9415 | return TRUE; |
50b2bdb7 AM |
9416 | |
9417 | ||
9418 | switch (bfd_get_arch (abfd)) | |
9419 | { | |
08a40648 AM |
9420 | /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and |
9421 | PT_GETFPREGS == mach+2. */ | |
50b2bdb7 AM |
9422 | |
9423 | case bfd_arch_alpha: | |
9424 | case bfd_arch_sparc: | |
9425 | switch (note->type) | |
08a40648 AM |
9426 | { |
9427 | case NT_NETBSDCORE_FIRSTMACH+0: | |
9428 | return elfcore_make_note_pseudosection (abfd, ".reg", note); | |
50b2bdb7 | 9429 | |
08a40648 AM |
9430 | case NT_NETBSDCORE_FIRSTMACH+2: |
9431 | return elfcore_make_note_pseudosection (abfd, ".reg2", note); | |
50b2bdb7 | 9432 | |
08a40648 AM |
9433 | default: |
9434 | return TRUE; | |
9435 | } | |
50b2bdb7 | 9436 | |
08a40648 AM |
9437 | /* On all other arch's, PT_GETREGS == mach+1 and |
9438 | PT_GETFPREGS == mach+3. */ | |
50b2bdb7 AM |
9439 | |
9440 | default: | |
9441 | switch (note->type) | |
08a40648 AM |
9442 | { |
9443 | case NT_NETBSDCORE_FIRSTMACH+1: | |
9444 | return elfcore_make_note_pseudosection (abfd, ".reg", note); | |
50b2bdb7 | 9445 | |
08a40648 AM |
9446 | case NT_NETBSDCORE_FIRSTMACH+3: |
9447 | return elfcore_make_note_pseudosection (abfd, ".reg2", note); | |
50b2bdb7 | 9448 | |
08a40648 AM |
9449 | default: |
9450 | return TRUE; | |
9451 | } | |
50b2bdb7 AM |
9452 | } |
9453 | /* NOTREACHED */ | |
9454 | } | |
9455 | ||
67cc5033 MK |
9456 | static bfd_boolean |
9457 | elfcore_grok_openbsd_procinfo (bfd *abfd, Elf_Internal_Note *note) | |
9458 | { | |
9459 | /* Signal number at offset 0x08. */ | |
228e534f | 9460 | elf_tdata (abfd)->core->signal |
67cc5033 MK |
9461 | = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08); |
9462 | ||
9463 | /* Process ID at offset 0x20. */ | |
228e534f | 9464 | elf_tdata (abfd)->core->pid |
67cc5033 MK |
9465 | = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x20); |
9466 | ||
9467 | /* Command name at 0x48 (max 32 bytes, including nul). */ | |
228e534f | 9468 | elf_tdata (abfd)->core->command |
67cc5033 MK |
9469 | = _bfd_elfcore_strndup (abfd, note->descdata + 0x48, 31); |
9470 | ||
9471 | return TRUE; | |
9472 | } | |
9473 | ||
9474 | static bfd_boolean | |
9475 | elfcore_grok_openbsd_note (bfd *abfd, Elf_Internal_Note *note) | |
9476 | { | |
9477 | if (note->type == NT_OPENBSD_PROCINFO) | |
9478 | return elfcore_grok_openbsd_procinfo (abfd, note); | |
9479 | ||
9480 | if (note->type == NT_OPENBSD_REGS) | |
9481 | return elfcore_make_note_pseudosection (abfd, ".reg", note); | |
9482 | ||
9483 | if (note->type == NT_OPENBSD_FPREGS) | |
9484 | return elfcore_make_note_pseudosection (abfd, ".reg2", note); | |
9485 | ||
9486 | if (note->type == NT_OPENBSD_XFPREGS) | |
9487 | return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note); | |
9488 | ||
9489 | if (note->type == NT_OPENBSD_AUXV) | |
9490 | { | |
9491 | asection *sect = bfd_make_section_anyway_with_flags (abfd, ".auxv", | |
9492 | SEC_HAS_CONTENTS); | |
9493 | ||
9494 | if (sect == NULL) | |
9495 | return FALSE; | |
9496 | sect->size = note->descsz; | |
9497 | sect->filepos = note->descpos; | |
9498 | sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32; | |
9499 | ||
9500 | return TRUE; | |
9501 | } | |
9502 | ||
9503 | if (note->type == NT_OPENBSD_WCOOKIE) | |
9504 | { | |
9505 | asection *sect = bfd_make_section_anyway_with_flags (abfd, ".wcookie", | |
9506 | SEC_HAS_CONTENTS); | |
9507 | ||
9508 | if (sect == NULL) | |
9509 | return FALSE; | |
9510 | sect->size = note->descsz; | |
9511 | sect->filepos = note->descpos; | |
9512 | sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32; | |
9513 | ||
9514 | return TRUE; | |
9515 | } | |
9516 | ||
9517 | return TRUE; | |
9518 | } | |
9519 | ||
07c6e936 | 9520 | static bfd_boolean |
d3fd4074 | 9521 | elfcore_grok_nto_status (bfd *abfd, Elf_Internal_Note *note, long *tid) |
07c6e936 NC |
9522 | { |
9523 | void *ddata = note->descdata; | |
9524 | char buf[100]; | |
9525 | char *name; | |
9526 | asection *sect; | |
f8843e87 AM |
9527 | short sig; |
9528 | unsigned flags; | |
07c6e936 NC |
9529 | |
9530 | /* nto_procfs_status 'pid' field is at offset 0. */ | |
228e534f | 9531 | elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, (bfd_byte *) ddata); |
07c6e936 | 9532 | |
f8843e87 AM |
9533 | /* nto_procfs_status 'tid' field is at offset 4. Pass it back. */ |
9534 | *tid = bfd_get_32 (abfd, (bfd_byte *) ddata + 4); | |
9535 | ||
9536 | /* nto_procfs_status 'flags' field is at offset 8. */ | |
9537 | flags = bfd_get_32 (abfd, (bfd_byte *) ddata + 8); | |
07c6e936 NC |
9538 | |
9539 | /* nto_procfs_status 'what' field is at offset 14. */ | |
f8843e87 AM |
9540 | if ((sig = bfd_get_16 (abfd, (bfd_byte *) ddata + 14)) > 0) |
9541 | { | |
228e534f AM |
9542 | elf_tdata (abfd)->core->signal = sig; |
9543 | elf_tdata (abfd)->core->lwpid = *tid; | |
f8843e87 | 9544 | } |
07c6e936 | 9545 | |
f8843e87 AM |
9546 | /* _DEBUG_FLAG_CURTID (current thread) is 0x80. Some cores |
9547 | do not come from signals so we make sure we set the current | |
9548 | thread just in case. */ | |
9549 | if (flags & 0x00000080) | |
228e534f | 9550 | elf_tdata (abfd)->core->lwpid = *tid; |
07c6e936 NC |
9551 | |
9552 | /* Make a ".qnx_core_status/%d" section. */ | |
d3fd4074 | 9553 | sprintf (buf, ".qnx_core_status/%ld", *tid); |
07c6e936 | 9554 | |
a50b1753 | 9555 | name = (char *) bfd_alloc (abfd, strlen (buf) + 1); |
07c6e936 NC |
9556 | if (name == NULL) |
9557 | return FALSE; | |
9558 | strcpy (name, buf); | |
9559 | ||
117ed4f8 | 9560 | sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); |
07c6e936 NC |
9561 | if (sect == NULL) |
9562 | return FALSE; | |
9563 | ||
eea6121a | 9564 | sect->size = note->descsz; |
07c6e936 | 9565 | sect->filepos = note->descpos; |
07c6e936 NC |
9566 | sect->alignment_power = 2; |
9567 | ||
9568 | return (elfcore_maybe_make_sect (abfd, ".qnx_core_status", sect)); | |
9569 | } | |
9570 | ||
9571 | static bfd_boolean | |
d69f560c KW |
9572 | elfcore_grok_nto_regs (bfd *abfd, |
9573 | Elf_Internal_Note *note, | |
d3fd4074 | 9574 | long tid, |
d69f560c | 9575 | char *base) |
07c6e936 NC |
9576 | { |
9577 | char buf[100]; | |
9578 | char *name; | |
9579 | asection *sect; | |
9580 | ||
d69f560c | 9581 | /* Make a "(base)/%d" section. */ |
d3fd4074 | 9582 | sprintf (buf, "%s/%ld", base, tid); |
07c6e936 | 9583 | |
a50b1753 | 9584 | name = (char *) bfd_alloc (abfd, strlen (buf) + 1); |
07c6e936 NC |
9585 | if (name == NULL) |
9586 | return FALSE; | |
9587 | strcpy (name, buf); | |
9588 | ||
117ed4f8 | 9589 | sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); |
07c6e936 NC |
9590 | if (sect == NULL) |
9591 | return FALSE; | |
9592 | ||
eea6121a | 9593 | sect->size = note->descsz; |
07c6e936 | 9594 | sect->filepos = note->descpos; |
07c6e936 NC |
9595 | sect->alignment_power = 2; |
9596 | ||
f8843e87 | 9597 | /* This is the current thread. */ |
228e534f | 9598 | if (elf_tdata (abfd)->core->lwpid == tid) |
d69f560c | 9599 | return elfcore_maybe_make_sect (abfd, base, sect); |
f8843e87 AM |
9600 | |
9601 | return TRUE; | |
07c6e936 NC |
9602 | } |
9603 | ||
9604 | #define BFD_QNT_CORE_INFO 7 | |
9605 | #define BFD_QNT_CORE_STATUS 8 | |
9606 | #define BFD_QNT_CORE_GREG 9 | |
9607 | #define BFD_QNT_CORE_FPREG 10 | |
9608 | ||
9609 | static bfd_boolean | |
217aa764 | 9610 | elfcore_grok_nto_note (bfd *abfd, Elf_Internal_Note *note) |
07c6e936 NC |
9611 | { |
9612 | /* Every GREG section has a STATUS section before it. Store the | |
811072d8 | 9613 | tid from the previous call to pass down to the next gregs |
07c6e936 | 9614 | function. */ |
d3fd4074 | 9615 | static long tid = 1; |
07c6e936 NC |
9616 | |
9617 | switch (note->type) | |
9618 | { | |
d69f560c KW |
9619 | case BFD_QNT_CORE_INFO: |
9620 | return elfcore_make_note_pseudosection (abfd, ".qnx_core_info", note); | |
9621 | case BFD_QNT_CORE_STATUS: | |
9622 | return elfcore_grok_nto_status (abfd, note, &tid); | |
9623 | case BFD_QNT_CORE_GREG: | |
9624 | return elfcore_grok_nto_regs (abfd, note, tid, ".reg"); | |
9625 | case BFD_QNT_CORE_FPREG: | |
9626 | return elfcore_grok_nto_regs (abfd, note, tid, ".reg2"); | |
9627 | default: | |
9628 | return TRUE; | |
07c6e936 NC |
9629 | } |
9630 | } | |
9631 | ||
b15fa79e AM |
9632 | static bfd_boolean |
9633 | elfcore_grok_spu_note (bfd *abfd, Elf_Internal_Note *note) | |
9634 | { | |
9635 | char *name; | |
9636 | asection *sect; | |
9637 | size_t len; | |
9638 | ||
9639 | /* Use note name as section name. */ | |
9640 | len = note->namesz; | |
a50b1753 | 9641 | name = (char *) bfd_alloc (abfd, len); |
b15fa79e AM |
9642 | if (name == NULL) |
9643 | return FALSE; | |
9644 | memcpy (name, note->namedata, len); | |
9645 | name[len - 1] = '\0'; | |
9646 | ||
9647 | sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); | |
9648 | if (sect == NULL) | |
9649 | return FALSE; | |
9650 | ||
9651 | sect->size = note->descsz; | |
9652 | sect->filepos = note->descpos; | |
9653 | sect->alignment_power = 1; | |
9654 | ||
9655 | return TRUE; | |
9656 | } | |
9657 | ||
7c76fa91 MS |
9658 | /* Function: elfcore_write_note |
9659 | ||
47d9a591 | 9660 | Inputs: |
a39f3346 | 9661 | buffer to hold note, and current size of buffer |
7c76fa91 MS |
9662 | name of note |
9663 | type of note | |
9664 | data for note | |
9665 | size of data for note | |
9666 | ||
a39f3346 AM |
9667 | Writes note to end of buffer. ELF64 notes are written exactly as |
9668 | for ELF32, despite the current (as of 2006) ELF gabi specifying | |
9669 | that they ought to have 8-byte namesz and descsz field, and have | |
9670 | 8-byte alignment. Other writers, eg. Linux kernel, do the same. | |
9671 | ||
7c76fa91 | 9672 | Return: |
a39f3346 | 9673 | Pointer to realloc'd buffer, *BUFSIZ updated. */ |
7c76fa91 MS |
9674 | |
9675 | char * | |
a39f3346 | 9676 | elfcore_write_note (bfd *abfd, |
217aa764 | 9677 | char *buf, |
a39f3346 | 9678 | int *bufsiz, |
217aa764 | 9679 | const char *name, |
a39f3346 | 9680 | int type, |
217aa764 | 9681 | const void *input, |
a39f3346 | 9682 | int size) |
7c76fa91 MS |
9683 | { |
9684 | Elf_External_Note *xnp; | |
d4c88bbb | 9685 | size_t namesz; |
d4c88bbb | 9686 | size_t newspace; |
a39f3346 | 9687 | char *dest; |
7c76fa91 | 9688 | |
d4c88bbb | 9689 | namesz = 0; |
d4c88bbb | 9690 | if (name != NULL) |
a39f3346 | 9691 | namesz = strlen (name) + 1; |
d4c88bbb | 9692 | |
a39f3346 | 9693 | newspace = 12 + ((namesz + 3) & -4) + ((size + 3) & -4); |
d4c88bbb | 9694 | |
a50b1753 | 9695 | buf = (char *) realloc (buf, *bufsiz + newspace); |
14b1c01e AM |
9696 | if (buf == NULL) |
9697 | return buf; | |
a39f3346 | 9698 | dest = buf + *bufsiz; |
7c76fa91 MS |
9699 | *bufsiz += newspace; |
9700 | xnp = (Elf_External_Note *) dest; | |
9701 | H_PUT_32 (abfd, namesz, xnp->namesz); | |
9702 | H_PUT_32 (abfd, size, xnp->descsz); | |
9703 | H_PUT_32 (abfd, type, xnp->type); | |
d4c88bbb AM |
9704 | dest = xnp->name; |
9705 | if (name != NULL) | |
9706 | { | |
9707 | memcpy (dest, name, namesz); | |
9708 | dest += namesz; | |
a39f3346 | 9709 | while (namesz & 3) |
d4c88bbb AM |
9710 | { |
9711 | *dest++ = '\0'; | |
a39f3346 | 9712 | ++namesz; |
d4c88bbb AM |
9713 | } |
9714 | } | |
9715 | memcpy (dest, input, size); | |
a39f3346 AM |
9716 | dest += size; |
9717 | while (size & 3) | |
9718 | { | |
9719 | *dest++ = '\0'; | |
9720 | ++size; | |
9721 | } | |
9722 | return buf; | |
7c76fa91 MS |
9723 | } |
9724 | ||
7c76fa91 | 9725 | char * |
217aa764 AM |
9726 | elfcore_write_prpsinfo (bfd *abfd, |
9727 | char *buf, | |
9728 | int *bufsiz, | |
9729 | const char *fname, | |
9730 | const char *psargs) | |
7c76fa91 | 9731 | { |
183e98be AM |
9732 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
9733 | ||
9734 | if (bed->elf_backend_write_core_note != NULL) | |
9735 | { | |
9736 | char *ret; | |
9737 | ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz, | |
9738 | NT_PRPSINFO, fname, psargs); | |
9739 | if (ret != NULL) | |
9740 | return ret; | |
9741 | } | |
7c76fa91 | 9742 | |
1f20dca5 | 9743 | #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) |
183e98be AM |
9744 | #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T) |
9745 | if (bed->s->elfclass == ELFCLASS32) | |
9746 | { | |
9747 | #if defined (HAVE_PSINFO32_T) | |
9748 | psinfo32_t data; | |
9749 | int note_type = NT_PSINFO; | |
9750 | #else | |
9751 | prpsinfo32_t data; | |
9752 | int note_type = NT_PRPSINFO; | |
9753 | #endif | |
9754 | ||
9755 | memset (&data, 0, sizeof (data)); | |
9756 | strncpy (data.pr_fname, fname, sizeof (data.pr_fname)); | |
9757 | strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs)); | |
9758 | return elfcore_write_note (abfd, buf, bufsiz, | |
1f20dca5 | 9759 | "CORE", note_type, &data, sizeof (data)); |
183e98be AM |
9760 | } |
9761 | else | |
9762 | #endif | |
9763 | { | |
7c76fa91 | 9764 | #if defined (HAVE_PSINFO_T) |
183e98be AM |
9765 | psinfo_t data; |
9766 | int note_type = NT_PSINFO; | |
7c76fa91 | 9767 | #else |
183e98be AM |
9768 | prpsinfo_t data; |
9769 | int note_type = NT_PRPSINFO; | |
7c76fa91 MS |
9770 | #endif |
9771 | ||
183e98be AM |
9772 | memset (&data, 0, sizeof (data)); |
9773 | strncpy (data.pr_fname, fname, sizeof (data.pr_fname)); | |
9774 | strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs)); | |
9775 | return elfcore_write_note (abfd, buf, bufsiz, | |
1f20dca5 | 9776 | "CORE", note_type, &data, sizeof (data)); |
183e98be | 9777 | } |
7c76fa91 MS |
9778 | #endif /* PSINFO_T or PRPSINFO_T */ |
9779 | ||
1f20dca5 UW |
9780 | free (buf); |
9781 | return NULL; | |
9782 | } | |
9783 | ||
70a38d42 SDJ |
9784 | char * |
9785 | elfcore_write_linux_prpsinfo32 | |
9786 | (bfd *abfd, char *buf, int *bufsiz, | |
9787 | const struct elf_internal_linux_prpsinfo *prpsinfo) | |
9788 | { | |
9789 | struct elf_external_linux_prpsinfo32 data; | |
9790 | ||
9791 | memset (&data, 0, sizeof (data)); | |
9792 | LINUX_PRPSINFO32_SWAP_FIELDS (abfd, prpsinfo, data); | |
9793 | ||
9794 | return elfcore_write_note (abfd, buf, bufsiz, "CORE", NT_PRPSINFO, | |
9795 | &data, sizeof (data)); | |
9796 | } | |
9797 | ||
9798 | char * | |
9799 | elfcore_write_linux_prpsinfo64 | |
9800 | (bfd *abfd, char *buf, int *bufsiz, | |
9801 | const struct elf_internal_linux_prpsinfo *prpsinfo) | |
9802 | { | |
9803 | struct elf_external_linux_prpsinfo64 data; | |
9804 | ||
9805 | memset (&data, 0, sizeof (data)); | |
9806 | LINUX_PRPSINFO64_SWAP_FIELDS (abfd, prpsinfo, data); | |
9807 | ||
9808 | return elfcore_write_note (abfd, buf, bufsiz, | |
9809 | "CORE", NT_PRPSINFO, &data, sizeof (data)); | |
9810 | } | |
9811 | ||
7c76fa91 | 9812 | char * |
217aa764 AM |
9813 | elfcore_write_prstatus (bfd *abfd, |
9814 | char *buf, | |
9815 | int *bufsiz, | |
9816 | long pid, | |
9817 | int cursig, | |
9818 | const void *gregs) | |
7c76fa91 | 9819 | { |
183e98be | 9820 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
7c76fa91 | 9821 | |
183e98be AM |
9822 | if (bed->elf_backend_write_core_note != NULL) |
9823 | { | |
9824 | char *ret; | |
9825 | ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz, | |
9826 | NT_PRSTATUS, | |
9827 | pid, cursig, gregs); | |
9828 | if (ret != NULL) | |
9829 | return ret; | |
9830 | } | |
9831 | ||
1f20dca5 | 9832 | #if defined (HAVE_PRSTATUS_T) |
183e98be AM |
9833 | #if defined (HAVE_PRSTATUS32_T) |
9834 | if (bed->s->elfclass == ELFCLASS32) | |
9835 | { | |
9836 | prstatus32_t prstat; | |
9837 | ||
9838 | memset (&prstat, 0, sizeof (prstat)); | |
9839 | prstat.pr_pid = pid; | |
9840 | prstat.pr_cursig = cursig; | |
9841 | memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg)); | |
1f20dca5 | 9842 | return elfcore_write_note (abfd, buf, bufsiz, "CORE", |
183e98be AM |
9843 | NT_PRSTATUS, &prstat, sizeof (prstat)); |
9844 | } | |
9845 | else | |
9846 | #endif | |
9847 | { | |
9848 | prstatus_t prstat; | |
9849 | ||
9850 | memset (&prstat, 0, sizeof (prstat)); | |
9851 | prstat.pr_pid = pid; | |
9852 | prstat.pr_cursig = cursig; | |
9853 | memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg)); | |
1f20dca5 | 9854 | return elfcore_write_note (abfd, buf, bufsiz, "CORE", |
183e98be AM |
9855 | NT_PRSTATUS, &prstat, sizeof (prstat)); |
9856 | } | |
7c76fa91 MS |
9857 | #endif /* HAVE_PRSTATUS_T */ |
9858 | ||
1f20dca5 UW |
9859 | free (buf); |
9860 | return NULL; | |
9861 | } | |
9862 | ||
51316059 MS |
9863 | #if defined (HAVE_LWPSTATUS_T) |
9864 | char * | |
217aa764 AM |
9865 | elfcore_write_lwpstatus (bfd *abfd, |
9866 | char *buf, | |
9867 | int *bufsiz, | |
9868 | long pid, | |
9869 | int cursig, | |
9870 | const void *gregs) | |
51316059 MS |
9871 | { |
9872 | lwpstatus_t lwpstat; | |
183e98be | 9873 | const char *note_name = "CORE"; |
51316059 MS |
9874 | |
9875 | memset (&lwpstat, 0, sizeof (lwpstat)); | |
9876 | lwpstat.pr_lwpid = pid >> 16; | |
9877 | lwpstat.pr_cursig = cursig; | |
9878 | #if defined (HAVE_LWPSTATUS_T_PR_REG) | |
d1e8523e | 9879 | memcpy (&lwpstat.pr_reg, gregs, sizeof (lwpstat.pr_reg)); |
51316059 MS |
9880 | #elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT) |
9881 | #if !defined(gregs) | |
9882 | memcpy (lwpstat.pr_context.uc_mcontext.gregs, | |
9883 | gregs, sizeof (lwpstat.pr_context.uc_mcontext.gregs)); | |
9884 | #else | |
9885 | memcpy (lwpstat.pr_context.uc_mcontext.__gregs, | |
9886 | gregs, sizeof (lwpstat.pr_context.uc_mcontext.__gregs)); | |
9887 | #endif | |
9888 | #endif | |
47d9a591 | 9889 | return elfcore_write_note (abfd, buf, bufsiz, note_name, |
51316059 MS |
9890 | NT_LWPSTATUS, &lwpstat, sizeof (lwpstat)); |
9891 | } | |
9892 | #endif /* HAVE_LWPSTATUS_T */ | |
9893 | ||
7c76fa91 MS |
9894 | #if defined (HAVE_PSTATUS_T) |
9895 | char * | |
217aa764 AM |
9896 | elfcore_write_pstatus (bfd *abfd, |
9897 | char *buf, | |
9898 | int *bufsiz, | |
9899 | long pid, | |
6c10990d NC |
9900 | int cursig ATTRIBUTE_UNUSED, |
9901 | const void *gregs ATTRIBUTE_UNUSED) | |
7c76fa91 | 9902 | { |
183e98be AM |
9903 | const char *note_name = "CORE"; |
9904 | #if defined (HAVE_PSTATUS32_T) | |
9905 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
7c76fa91 | 9906 | |
183e98be AM |
9907 | if (bed->s->elfclass == ELFCLASS32) |
9908 | { | |
9909 | pstatus32_t pstat; | |
9910 | ||
9911 | memset (&pstat, 0, sizeof (pstat)); | |
9912 | pstat.pr_pid = pid & 0xffff; | |
9913 | buf = elfcore_write_note (abfd, buf, bufsiz, note_name, | |
9914 | NT_PSTATUS, &pstat, sizeof (pstat)); | |
9915 | return buf; | |
9916 | } | |
9917 | else | |
9918 | #endif | |
9919 | { | |
9920 | pstatus_t pstat; | |
9921 | ||
9922 | memset (&pstat, 0, sizeof (pstat)); | |
9923 | pstat.pr_pid = pid & 0xffff; | |
9924 | buf = elfcore_write_note (abfd, buf, bufsiz, note_name, | |
9925 | NT_PSTATUS, &pstat, sizeof (pstat)); | |
9926 | return buf; | |
9927 | } | |
7c76fa91 MS |
9928 | } |
9929 | #endif /* HAVE_PSTATUS_T */ | |
9930 | ||
9931 | char * | |
217aa764 AM |
9932 | elfcore_write_prfpreg (bfd *abfd, |
9933 | char *buf, | |
9934 | int *bufsiz, | |
9935 | const void *fpregs, | |
9936 | int size) | |
7c76fa91 | 9937 | { |
183e98be | 9938 | const char *note_name = "CORE"; |
47d9a591 | 9939 | return elfcore_write_note (abfd, buf, bufsiz, |
7c76fa91 MS |
9940 | note_name, NT_FPREGSET, fpregs, size); |
9941 | } | |
9942 | ||
9943 | char * | |
217aa764 AM |
9944 | elfcore_write_prxfpreg (bfd *abfd, |
9945 | char *buf, | |
9946 | int *bufsiz, | |
9947 | const void *xfpregs, | |
9948 | int size) | |
7c76fa91 MS |
9949 | { |
9950 | char *note_name = "LINUX"; | |
47d9a591 | 9951 | return elfcore_write_note (abfd, buf, bufsiz, |
7c76fa91 MS |
9952 | note_name, NT_PRXFPREG, xfpregs, size); |
9953 | } | |
9954 | ||
4339cae0 L |
9955 | char * |
9956 | elfcore_write_xstatereg (bfd *abfd, char *buf, int *bufsiz, | |
9957 | const void *xfpregs, int size) | |
9958 | { | |
97de3545 JB |
9959 | char *note_name; |
9960 | if (get_elf_backend_data (abfd)->elf_osabi == ELFOSABI_FREEBSD) | |
9961 | note_name = "FreeBSD"; | |
9962 | else | |
9963 | note_name = "LINUX"; | |
4339cae0 L |
9964 | return elfcore_write_note (abfd, buf, bufsiz, |
9965 | note_name, NT_X86_XSTATE, xfpregs, size); | |
9966 | } | |
9967 | ||
97753bd5 AM |
9968 | char * |
9969 | elfcore_write_ppc_vmx (bfd *abfd, | |
9970 | char *buf, | |
9971 | int *bufsiz, | |
9972 | const void *ppc_vmx, | |
9973 | int size) | |
9974 | { | |
9975 | char *note_name = "LINUX"; | |
9976 | return elfcore_write_note (abfd, buf, bufsiz, | |
9977 | note_name, NT_PPC_VMX, ppc_vmx, size); | |
9978 | } | |
9979 | ||
89eeb0bc LM |
9980 | char * |
9981 | elfcore_write_ppc_vsx (bfd *abfd, | |
9982 | char *buf, | |
9983 | int *bufsiz, | |
9984 | const void *ppc_vsx, | |
9985 | int size) | |
9986 | { | |
9987 | char *note_name = "LINUX"; | |
9988 | return elfcore_write_note (abfd, buf, bufsiz, | |
9989 | note_name, NT_PPC_VSX, ppc_vsx, size); | |
9990 | } | |
9991 | ||
0675e188 UW |
9992 | static char * |
9993 | elfcore_write_s390_high_gprs (bfd *abfd, | |
9994 | char *buf, | |
9995 | int *bufsiz, | |
9996 | const void *s390_high_gprs, | |
9997 | int size) | |
9998 | { | |
9999 | char *note_name = "LINUX"; | |
10000 | return elfcore_write_note (abfd, buf, bufsiz, | |
10001 | note_name, NT_S390_HIGH_GPRS, | |
10002 | s390_high_gprs, size); | |
10003 | } | |
10004 | ||
d7eeb400 MS |
10005 | char * |
10006 | elfcore_write_s390_timer (bfd *abfd, | |
10007 | char *buf, | |
10008 | int *bufsiz, | |
10009 | const void *s390_timer, | |
10010 | int size) | |
10011 | { | |
10012 | char *note_name = "LINUX"; | |
10013 | return elfcore_write_note (abfd, buf, bufsiz, | |
10014 | note_name, NT_S390_TIMER, s390_timer, size); | |
10015 | } | |
10016 | ||
10017 | char * | |
10018 | elfcore_write_s390_todcmp (bfd *abfd, | |
10019 | char *buf, | |
10020 | int *bufsiz, | |
10021 | const void *s390_todcmp, | |
10022 | int size) | |
10023 | { | |
10024 | char *note_name = "LINUX"; | |
10025 | return elfcore_write_note (abfd, buf, bufsiz, | |
10026 | note_name, NT_S390_TODCMP, s390_todcmp, size); | |
10027 | } | |
10028 | ||
10029 | char * | |
10030 | elfcore_write_s390_todpreg (bfd *abfd, | |
10031 | char *buf, | |
10032 | int *bufsiz, | |
10033 | const void *s390_todpreg, | |
10034 | int size) | |
10035 | { | |
10036 | char *note_name = "LINUX"; | |
10037 | return elfcore_write_note (abfd, buf, bufsiz, | |
10038 | note_name, NT_S390_TODPREG, s390_todpreg, size); | |
10039 | } | |
10040 | ||
10041 | char * | |
10042 | elfcore_write_s390_ctrs (bfd *abfd, | |
10043 | char *buf, | |
10044 | int *bufsiz, | |
10045 | const void *s390_ctrs, | |
10046 | int size) | |
10047 | { | |
10048 | char *note_name = "LINUX"; | |
10049 | return elfcore_write_note (abfd, buf, bufsiz, | |
10050 | note_name, NT_S390_CTRS, s390_ctrs, size); | |
10051 | } | |
10052 | ||
10053 | char * | |
10054 | elfcore_write_s390_prefix (bfd *abfd, | |
10055 | char *buf, | |
10056 | int *bufsiz, | |
10057 | const void *s390_prefix, | |
10058 | int size) | |
10059 | { | |
10060 | char *note_name = "LINUX"; | |
10061 | return elfcore_write_note (abfd, buf, bufsiz, | |
10062 | note_name, NT_S390_PREFIX, s390_prefix, size); | |
10063 | } | |
10064 | ||
355b81d9 UW |
10065 | char * |
10066 | elfcore_write_s390_last_break (bfd *abfd, | |
10067 | char *buf, | |
10068 | int *bufsiz, | |
10069 | const void *s390_last_break, | |
10070 | int size) | |
10071 | { | |
10072 | char *note_name = "LINUX"; | |
10073 | return elfcore_write_note (abfd, buf, bufsiz, | |
10074 | note_name, NT_S390_LAST_BREAK, | |
10075 | s390_last_break, size); | |
10076 | } | |
10077 | ||
10078 | char * | |
10079 | elfcore_write_s390_system_call (bfd *abfd, | |
10080 | char *buf, | |
10081 | int *bufsiz, | |
10082 | const void *s390_system_call, | |
10083 | int size) | |
10084 | { | |
10085 | char *note_name = "LINUX"; | |
10086 | return elfcore_write_note (abfd, buf, bufsiz, | |
10087 | note_name, NT_S390_SYSTEM_CALL, | |
10088 | s390_system_call, size); | |
10089 | } | |
10090 | ||
abb3f6cc NC |
10091 | char * |
10092 | elfcore_write_s390_tdb (bfd *abfd, | |
10093 | char *buf, | |
10094 | int *bufsiz, | |
10095 | const void *s390_tdb, | |
10096 | int size) | |
10097 | { | |
10098 | char *note_name = "LINUX"; | |
10099 | return elfcore_write_note (abfd, buf, bufsiz, | |
10100 | note_name, NT_S390_TDB, s390_tdb, size); | |
10101 | } | |
10102 | ||
4ef9f41a AA |
10103 | char * |
10104 | elfcore_write_s390_vxrs_low (bfd *abfd, | |
10105 | char *buf, | |
10106 | int *bufsiz, | |
10107 | const void *s390_vxrs_low, | |
10108 | int size) | |
10109 | { | |
10110 | char *note_name = "LINUX"; | |
10111 | return elfcore_write_note (abfd, buf, bufsiz, | |
10112 | note_name, NT_S390_VXRS_LOW, s390_vxrs_low, size); | |
10113 | } | |
10114 | ||
10115 | char * | |
10116 | elfcore_write_s390_vxrs_high (bfd *abfd, | |
10117 | char *buf, | |
10118 | int *bufsiz, | |
10119 | const void *s390_vxrs_high, | |
10120 | int size) | |
10121 | { | |
10122 | char *note_name = "LINUX"; | |
10123 | return elfcore_write_note (abfd, buf, bufsiz, | |
10124 | note_name, NT_S390_VXRS_HIGH, | |
10125 | s390_vxrs_high, size); | |
10126 | } | |
10127 | ||
faa9a424 UW |
10128 | char * |
10129 | elfcore_write_arm_vfp (bfd *abfd, | |
10130 | char *buf, | |
10131 | int *bufsiz, | |
10132 | const void *arm_vfp, | |
10133 | int size) | |
10134 | { | |
10135 | char *note_name = "LINUX"; | |
10136 | return elfcore_write_note (abfd, buf, bufsiz, | |
10137 | note_name, NT_ARM_VFP, arm_vfp, size); | |
10138 | } | |
10139 | ||
652451f8 YZ |
10140 | char * |
10141 | elfcore_write_aarch_tls (bfd *abfd, | |
10142 | char *buf, | |
10143 | int *bufsiz, | |
10144 | const void *aarch_tls, | |
10145 | int size) | |
10146 | { | |
10147 | char *note_name = "LINUX"; | |
10148 | return elfcore_write_note (abfd, buf, bufsiz, | |
10149 | note_name, NT_ARM_TLS, aarch_tls, size); | |
10150 | } | |
10151 | ||
10152 | char * | |
10153 | elfcore_write_aarch_hw_break (bfd *abfd, | |
10154 | char *buf, | |
10155 | int *bufsiz, | |
10156 | const void *aarch_hw_break, | |
10157 | int size) | |
10158 | { | |
10159 | char *note_name = "LINUX"; | |
10160 | return elfcore_write_note (abfd, buf, bufsiz, | |
10161 | note_name, NT_ARM_HW_BREAK, aarch_hw_break, size); | |
10162 | } | |
10163 | ||
10164 | char * | |
10165 | elfcore_write_aarch_hw_watch (bfd *abfd, | |
10166 | char *buf, | |
10167 | int *bufsiz, | |
10168 | const void *aarch_hw_watch, | |
10169 | int size) | |
10170 | { | |
10171 | char *note_name = "LINUX"; | |
10172 | return elfcore_write_note (abfd, buf, bufsiz, | |
10173 | note_name, NT_ARM_HW_WATCH, aarch_hw_watch, size); | |
10174 | } | |
10175 | ||
bb864ac1 CES |
10176 | char * |
10177 | elfcore_write_register_note (bfd *abfd, | |
10178 | char *buf, | |
10179 | int *bufsiz, | |
10180 | const char *section, | |
10181 | const void *data, | |
10182 | int size) | |
10183 | { | |
10184 | if (strcmp (section, ".reg2") == 0) | |
10185 | return elfcore_write_prfpreg (abfd, buf, bufsiz, data, size); | |
10186 | if (strcmp (section, ".reg-xfp") == 0) | |
10187 | return elfcore_write_prxfpreg (abfd, buf, bufsiz, data, size); | |
4339cae0 L |
10188 | if (strcmp (section, ".reg-xstate") == 0) |
10189 | return elfcore_write_xstatereg (abfd, buf, bufsiz, data, size); | |
bb864ac1 CES |
10190 | if (strcmp (section, ".reg-ppc-vmx") == 0) |
10191 | return elfcore_write_ppc_vmx (abfd, buf, bufsiz, data, size); | |
89eeb0bc LM |
10192 | if (strcmp (section, ".reg-ppc-vsx") == 0) |
10193 | return elfcore_write_ppc_vsx (abfd, buf, bufsiz, data, size); | |
0675e188 UW |
10194 | if (strcmp (section, ".reg-s390-high-gprs") == 0) |
10195 | return elfcore_write_s390_high_gprs (abfd, buf, bufsiz, data, size); | |
d7eeb400 MS |
10196 | if (strcmp (section, ".reg-s390-timer") == 0) |
10197 | return elfcore_write_s390_timer (abfd, buf, bufsiz, data, size); | |
10198 | if (strcmp (section, ".reg-s390-todcmp") == 0) | |
10199 | return elfcore_write_s390_todcmp (abfd, buf, bufsiz, data, size); | |
10200 | if (strcmp (section, ".reg-s390-todpreg") == 0) | |
10201 | return elfcore_write_s390_todpreg (abfd, buf, bufsiz, data, size); | |
10202 | if (strcmp (section, ".reg-s390-ctrs") == 0) | |
10203 | return elfcore_write_s390_ctrs (abfd, buf, bufsiz, data, size); | |
10204 | if (strcmp (section, ".reg-s390-prefix") == 0) | |
10205 | return elfcore_write_s390_prefix (abfd, buf, bufsiz, data, size); | |
355b81d9 UW |
10206 | if (strcmp (section, ".reg-s390-last-break") == 0) |
10207 | return elfcore_write_s390_last_break (abfd, buf, bufsiz, data, size); | |
10208 | if (strcmp (section, ".reg-s390-system-call") == 0) | |
10209 | return elfcore_write_s390_system_call (abfd, buf, bufsiz, data, size); | |
abb3f6cc NC |
10210 | if (strcmp (section, ".reg-s390-tdb") == 0) |
10211 | return elfcore_write_s390_tdb (abfd, buf, bufsiz, data, size); | |
4ef9f41a AA |
10212 | if (strcmp (section, ".reg-s390-vxrs-low") == 0) |
10213 | return elfcore_write_s390_vxrs_low (abfd, buf, bufsiz, data, size); | |
10214 | if (strcmp (section, ".reg-s390-vxrs-high") == 0) | |
10215 | return elfcore_write_s390_vxrs_high (abfd, buf, bufsiz, data, size); | |
faa9a424 UW |
10216 | if (strcmp (section, ".reg-arm-vfp") == 0) |
10217 | return elfcore_write_arm_vfp (abfd, buf, bufsiz, data, size); | |
652451f8 YZ |
10218 | if (strcmp (section, ".reg-aarch-tls") == 0) |
10219 | return elfcore_write_aarch_tls (abfd, buf, bufsiz, data, size); | |
10220 | if (strcmp (section, ".reg-aarch-hw-break") == 0) | |
10221 | return elfcore_write_aarch_hw_break (abfd, buf, bufsiz, data, size); | |
10222 | if (strcmp (section, ".reg-aarch-hw-watch") == 0) | |
10223 | return elfcore_write_aarch_hw_watch (abfd, buf, bufsiz, data, size); | |
bb864ac1 CES |
10224 | return NULL; |
10225 | } | |
10226 | ||
b34976b6 | 10227 | static bfd_boolean |
718175fa | 10228 | elf_parse_notes (bfd *abfd, char *buf, size_t size, file_ptr offset) |
252b5132 | 10229 | { |
c044fabd | 10230 | char *p; |
252b5132 | 10231 | |
252b5132 RH |
10232 | p = buf; |
10233 | while (p < buf + size) | |
10234 | { | |
c044fabd KH |
10235 | /* FIXME: bad alignment assumption. */ |
10236 | Elf_External_Note *xnp = (Elf_External_Note *) p; | |
252b5132 RH |
10237 | Elf_Internal_Note in; |
10238 | ||
baea7ef1 AM |
10239 | if (offsetof (Elf_External_Note, name) > buf - p + size) |
10240 | return FALSE; | |
10241 | ||
dc810e39 | 10242 | in.type = H_GET_32 (abfd, xnp->type); |
252b5132 | 10243 | |
dc810e39 | 10244 | in.namesz = H_GET_32 (abfd, xnp->namesz); |
252b5132 | 10245 | in.namedata = xnp->name; |
baea7ef1 AM |
10246 | if (in.namesz > buf - in.namedata + size) |
10247 | return FALSE; | |
252b5132 | 10248 | |
dc810e39 | 10249 | in.descsz = H_GET_32 (abfd, xnp->descsz); |
252b5132 RH |
10250 | in.descdata = in.namedata + BFD_ALIGN (in.namesz, 4); |
10251 | in.descpos = offset + (in.descdata - buf); | |
baea7ef1 AM |
10252 | if (in.descsz != 0 |
10253 | && (in.descdata >= buf + size | |
10254 | || in.descsz > buf - in.descdata + size)) | |
10255 | return FALSE; | |
252b5132 | 10256 | |
718175fa JK |
10257 | switch (bfd_get_format (abfd)) |
10258 | { | |
10259 | default: | |
10260 | return TRUE; | |
10261 | ||
10262 | case bfd_core: | |
f64e188b | 10263 | { |
8acbedd6 | 10264 | #define GROKER_ELEMENT(S,F) {S, sizeof (S) - 1, F} |
f64e188b | 10265 | struct |
718175fa | 10266 | { |
f64e188b | 10267 | const char * string; |
8acbedd6 | 10268 | size_t len; |
f64e188b | 10269 | bfd_boolean (* func)(bfd *, Elf_Internal_Note *); |
718175fa | 10270 | } |
f64e188b | 10271 | grokers[] = |
b15fa79e | 10272 | { |
8acbedd6 KS |
10273 | GROKER_ELEMENT ("", elfcore_grok_note), |
10274 | GROKER_ELEMENT ("NetBSD-CORE", elfcore_grok_netbsd_note), | |
10275 | GROKER_ELEMENT ( "OpenBSD", elfcore_grok_openbsd_note), | |
10276 | GROKER_ELEMENT ("QNX", elfcore_grok_nto_note), | |
10277 | GROKER_ELEMENT ("SPU/", elfcore_grok_spu_note) | |
f64e188b | 10278 | }; |
8acbedd6 | 10279 | #undef GROKER_ELEMENT |
f64e188b NC |
10280 | int i; |
10281 | ||
10282 | for (i = ARRAY_SIZE (grokers); i--;) | |
8acbedd6 KS |
10283 | { |
10284 | if (in.namesz >= grokers[i].len | |
10285 | && strncmp (in.namedata, grokers[i].string, | |
10286 | grokers[i].len) == 0) | |
10287 | { | |
10288 | if (! grokers[i].func (abfd, & in)) | |
10289 | return FALSE; | |
10290 | break; | |
10291 | } | |
10292 | } | |
f64e188b NC |
10293 | break; |
10294 | } | |
718175fa JK |
10295 | |
10296 | case bfd_object: | |
10297 | if (in.namesz == sizeof "GNU" && strcmp (in.namedata, "GNU") == 0) | |
10298 | { | |
10299 | if (! elfobj_grok_gnu_note (abfd, &in)) | |
10300 | return FALSE; | |
10301 | } | |
e21e5835 NC |
10302 | else if (in.namesz == sizeof "stapsdt" |
10303 | && strcmp (in.namedata, "stapsdt") == 0) | |
10304 | { | |
10305 | if (! elfobj_grok_stapsdt_note (abfd, &in)) | |
10306 | return FALSE; | |
10307 | } | |
718175fa | 10308 | break; |
08a40648 | 10309 | } |
252b5132 RH |
10310 | |
10311 | p = in.descdata + BFD_ALIGN (in.descsz, 4); | |
10312 | } | |
10313 | ||
718175fa JK |
10314 | return TRUE; |
10315 | } | |
10316 | ||
10317 | static bfd_boolean | |
10318 | elf_read_notes (bfd *abfd, file_ptr offset, bfd_size_type size) | |
10319 | { | |
10320 | char *buf; | |
10321 | ||
10322 | if (size <= 0) | |
10323 | return TRUE; | |
10324 | ||
10325 | if (bfd_seek (abfd, offset, SEEK_SET) != 0) | |
10326 | return FALSE; | |
10327 | ||
f64e188b | 10328 | buf = (char *) bfd_malloc (size + 1); |
718175fa JK |
10329 | if (buf == NULL) |
10330 | return FALSE; | |
10331 | ||
f64e188b NC |
10332 | /* PR 17512: file: ec08f814 |
10333 | 0-termintate the buffer so that string searches will not overflow. */ | |
10334 | buf[size] = 0; | |
10335 | ||
718175fa JK |
10336 | if (bfd_bread (buf, size, abfd) != size |
10337 | || !elf_parse_notes (abfd, buf, size, offset)) | |
10338 | { | |
10339 | free (buf); | |
10340 | return FALSE; | |
10341 | } | |
10342 | ||
252b5132 | 10343 | free (buf); |
b34976b6 | 10344 | return TRUE; |
252b5132 | 10345 | } |
98d8431c JB |
10346 | \f |
10347 | /* Providing external access to the ELF program header table. */ | |
10348 | ||
10349 | /* Return an upper bound on the number of bytes required to store a | |
10350 | copy of ABFD's program header table entries. Return -1 if an error | |
10351 | occurs; bfd_get_error will return an appropriate code. */ | |
c044fabd | 10352 | |
98d8431c | 10353 | long |
217aa764 | 10354 | bfd_get_elf_phdr_upper_bound (bfd *abfd) |
98d8431c JB |
10355 | { |
10356 | if (abfd->xvec->flavour != bfd_target_elf_flavour) | |
10357 | { | |
10358 | bfd_set_error (bfd_error_wrong_format); | |
10359 | return -1; | |
10360 | } | |
10361 | ||
936e320b | 10362 | return elf_elfheader (abfd)->e_phnum * sizeof (Elf_Internal_Phdr); |
98d8431c JB |
10363 | } |
10364 | ||
98d8431c JB |
10365 | /* Copy ABFD's program header table entries to *PHDRS. The entries |
10366 | will be stored as an array of Elf_Internal_Phdr structures, as | |
10367 | defined in include/elf/internal.h. To find out how large the | |
10368 | buffer needs to be, call bfd_get_elf_phdr_upper_bound. | |
10369 | ||
10370 | Return the number of program header table entries read, or -1 if an | |
10371 | error occurs; bfd_get_error will return an appropriate code. */ | |
c044fabd | 10372 | |
98d8431c | 10373 | int |
217aa764 | 10374 | bfd_get_elf_phdrs (bfd *abfd, void *phdrs) |
98d8431c JB |
10375 | { |
10376 | int num_phdrs; | |
10377 | ||
10378 | if (abfd->xvec->flavour != bfd_target_elf_flavour) | |
10379 | { | |
10380 | bfd_set_error (bfd_error_wrong_format); | |
10381 | return -1; | |
10382 | } | |
10383 | ||
10384 | num_phdrs = elf_elfheader (abfd)->e_phnum; | |
c044fabd | 10385 | memcpy (phdrs, elf_tdata (abfd)->phdr, |
98d8431c JB |
10386 | num_phdrs * sizeof (Elf_Internal_Phdr)); |
10387 | ||
10388 | return num_phdrs; | |
10389 | } | |
ae4221d7 | 10390 | |
db6751f2 | 10391 | enum elf_reloc_type_class |
7e612e98 AM |
10392 | _bfd_elf_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED, |
10393 | const asection *rel_sec ATTRIBUTE_UNUSED, | |
10394 | const Elf_Internal_Rela *rela ATTRIBUTE_UNUSED) | |
db6751f2 JJ |
10395 | { |
10396 | return reloc_class_normal; | |
10397 | } | |
f8df10f4 | 10398 | |
47d9a591 | 10399 | /* For RELA architectures, return the relocation value for a |
f8df10f4 JJ |
10400 | relocation against a local symbol. */ |
10401 | ||
10402 | bfd_vma | |
217aa764 AM |
10403 | _bfd_elf_rela_local_sym (bfd *abfd, |
10404 | Elf_Internal_Sym *sym, | |
8517fae7 | 10405 | asection **psec, |
217aa764 | 10406 | Elf_Internal_Rela *rel) |
f8df10f4 | 10407 | { |
8517fae7 | 10408 | asection *sec = *psec; |
f8df10f4 JJ |
10409 | bfd_vma relocation; |
10410 | ||
10411 | relocation = (sec->output_section->vma | |
10412 | + sec->output_offset | |
10413 | + sym->st_value); | |
10414 | if ((sec->flags & SEC_MERGE) | |
c629eae0 | 10415 | && ELF_ST_TYPE (sym->st_info) == STT_SECTION |
dbaa2011 | 10416 | && sec->sec_info_type == SEC_INFO_TYPE_MERGE) |
f8df10f4 | 10417 | { |
f8df10f4 | 10418 | rel->r_addend = |
8517fae7 | 10419 | _bfd_merged_section_offset (abfd, psec, |
65765700 | 10420 | elf_section_data (sec)->sec_info, |
753731ee AM |
10421 | sym->st_value + rel->r_addend); |
10422 | if (sec != *psec) | |
10423 | { | |
10424 | /* If we have changed the section, and our original section is | |
10425 | marked with SEC_EXCLUDE, it means that the original | |
10426 | SEC_MERGE section has been completely subsumed in some | |
10427 | other SEC_MERGE section. In this case, we need to leave | |
10428 | some info around for --emit-relocs. */ | |
10429 | if ((sec->flags & SEC_EXCLUDE) != 0) | |
10430 | sec->kept_section = *psec; | |
10431 | sec = *psec; | |
10432 | } | |
8517fae7 AM |
10433 | rel->r_addend -= relocation; |
10434 | rel->r_addend += sec->output_section->vma + sec->output_offset; | |
f8df10f4 JJ |
10435 | } |
10436 | return relocation; | |
10437 | } | |
c629eae0 JJ |
10438 | |
10439 | bfd_vma | |
217aa764 AM |
10440 | _bfd_elf_rel_local_sym (bfd *abfd, |
10441 | Elf_Internal_Sym *sym, | |
10442 | asection **psec, | |
10443 | bfd_vma addend) | |
47d9a591 | 10444 | { |
c629eae0 JJ |
10445 | asection *sec = *psec; |
10446 | ||
dbaa2011 | 10447 | if (sec->sec_info_type != SEC_INFO_TYPE_MERGE) |
c629eae0 JJ |
10448 | return sym->st_value + addend; |
10449 | ||
10450 | return _bfd_merged_section_offset (abfd, psec, | |
65765700 | 10451 | elf_section_data (sec)->sec_info, |
753731ee | 10452 | sym->st_value + addend); |
c629eae0 JJ |
10453 | } |
10454 | ||
10455 | bfd_vma | |
217aa764 | 10456 | _bfd_elf_section_offset (bfd *abfd, |
92e4ec35 | 10457 | struct bfd_link_info *info, |
217aa764 AM |
10458 | asection *sec, |
10459 | bfd_vma offset) | |
c629eae0 | 10460 | { |
68bfbfcc | 10461 | switch (sec->sec_info_type) |
65765700 | 10462 | { |
dbaa2011 | 10463 | case SEC_INFO_TYPE_STABS: |
eea6121a AM |
10464 | return _bfd_stab_section_offset (sec, elf_section_data (sec)->sec_info, |
10465 | offset); | |
dbaa2011 | 10466 | case SEC_INFO_TYPE_EH_FRAME: |
92e4ec35 | 10467 | return _bfd_elf_eh_frame_section_offset (abfd, info, sec, offset); |
65765700 | 10468 | default: |
310fd250 L |
10469 | if ((sec->flags & SEC_ELF_REVERSE_COPY) != 0) |
10470 | { | |
10471 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
10472 | bfd_size_type address_size = bed->s->arch_size / 8; | |
10473 | offset = sec->size - offset - address_size; | |
10474 | } | |
65765700 JJ |
10475 | return offset; |
10476 | } | |
c629eae0 | 10477 | } |
3333a7c3 RM |
10478 | \f |
10479 | /* Create a new BFD as if by bfd_openr. Rather than opening a file, | |
10480 | reconstruct an ELF file by reading the segments out of remote memory | |
10481 | based on the ELF file header at EHDR_VMA and the ELF program headers it | |
10482 | points to. If not null, *LOADBASEP is filled in with the difference | |
10483 | between the VMAs from which the segments were read, and the VMAs the | |
10484 | file headers (and hence BFD's idea of each section's VMA) put them at. | |
10485 | ||
10486 | The function TARGET_READ_MEMORY is called to copy LEN bytes from the | |
10487 | remote memory at target address VMA into the local buffer at MYADDR; it | |
10488 | should return zero on success or an `errno' code on failure. TEMPL must | |
10489 | be a BFD for an ELF target with the word size and byte order found in | |
10490 | the remote memory. */ | |
10491 | ||
10492 | bfd * | |
217aa764 AM |
10493 | bfd_elf_bfd_from_remote_memory |
10494 | (bfd *templ, | |
10495 | bfd_vma ehdr_vma, | |
f0a5d95a | 10496 | bfd_size_type size, |
217aa764 | 10497 | bfd_vma *loadbasep, |
fe78531d | 10498 | int (*target_read_memory) (bfd_vma, bfd_byte *, bfd_size_type)) |
3333a7c3 RM |
10499 | { |
10500 | return (*get_elf_backend_data (templ)->elf_backend_bfd_from_remote_memory) | |
5979d6b6 | 10501 | (templ, ehdr_vma, size, loadbasep, target_read_memory); |
3333a7c3 | 10502 | } |
4c45e5c9 JJ |
10503 | \f |
10504 | long | |
c9727e01 AM |
10505 | _bfd_elf_get_synthetic_symtab (bfd *abfd, |
10506 | long symcount ATTRIBUTE_UNUSED, | |
10507 | asymbol **syms ATTRIBUTE_UNUSED, | |
8615f3f2 | 10508 | long dynsymcount, |
c9727e01 AM |
10509 | asymbol **dynsyms, |
10510 | asymbol **ret) | |
4c45e5c9 JJ |
10511 | { |
10512 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
10513 | asection *relplt; | |
10514 | asymbol *s; | |
10515 | const char *relplt_name; | |
10516 | bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean); | |
10517 | arelent *p; | |
10518 | long count, i, n; | |
10519 | size_t size; | |
10520 | Elf_Internal_Shdr *hdr; | |
10521 | char *names; | |
10522 | asection *plt; | |
10523 | ||
8615f3f2 AM |
10524 | *ret = NULL; |
10525 | ||
90e3cdf2 JJ |
10526 | if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0) |
10527 | return 0; | |
10528 | ||
8615f3f2 AM |
10529 | if (dynsymcount <= 0) |
10530 | return 0; | |
10531 | ||
4c45e5c9 JJ |
10532 | if (!bed->plt_sym_val) |
10533 | return 0; | |
10534 | ||
10535 | relplt_name = bed->relplt_name; | |
10536 | if (relplt_name == NULL) | |
d35fd659 | 10537 | relplt_name = bed->rela_plts_and_copies_p ? ".rela.plt" : ".rel.plt"; |
4c45e5c9 JJ |
10538 | relplt = bfd_get_section_by_name (abfd, relplt_name); |
10539 | if (relplt == NULL) | |
10540 | return 0; | |
10541 | ||
10542 | hdr = &elf_section_data (relplt)->this_hdr; | |
10543 | if (hdr->sh_link != elf_dynsymtab (abfd) | |
10544 | || (hdr->sh_type != SHT_REL && hdr->sh_type != SHT_RELA)) | |
10545 | return 0; | |
10546 | ||
10547 | plt = bfd_get_section_by_name (abfd, ".plt"); | |
10548 | if (plt == NULL) | |
10549 | return 0; | |
10550 | ||
10551 | slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table; | |
c9727e01 | 10552 | if (! (*slurp_relocs) (abfd, relplt, dynsyms, TRUE)) |
4c45e5c9 JJ |
10553 | return -1; |
10554 | ||
eea6121a | 10555 | count = relplt->size / hdr->sh_entsize; |
4c45e5c9 JJ |
10556 | size = count * sizeof (asymbol); |
10557 | p = relplt->relocation; | |
cb53bf42 | 10558 | for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel) |
041de40d AM |
10559 | { |
10560 | size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt"); | |
10561 | if (p->addend != 0) | |
10562 | { | |
10563 | #ifdef BFD64 | |
10564 | size += sizeof ("+0x") - 1 + 8 + 8 * (bed->s->elfclass == ELFCLASS64); | |
10565 | #else | |
10566 | size += sizeof ("+0x") - 1 + 8; | |
10567 | #endif | |
10568 | } | |
10569 | } | |
4c45e5c9 | 10570 | |
a50b1753 | 10571 | s = *ret = (asymbol *) bfd_malloc (size); |
4c45e5c9 JJ |
10572 | if (s == NULL) |
10573 | return -1; | |
10574 | ||
10575 | names = (char *) (s + count); | |
10576 | p = relplt->relocation; | |
10577 | n = 0; | |
cb53bf42 | 10578 | for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel) |
4c45e5c9 JJ |
10579 | { |
10580 | size_t len; | |
10581 | bfd_vma addr; | |
10582 | ||
10583 | addr = bed->plt_sym_val (i, plt, p); | |
10584 | if (addr == (bfd_vma) -1) | |
10585 | continue; | |
10586 | ||
10587 | *s = **p->sym_ptr_ptr; | |
65a7a66f AM |
10588 | /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since |
10589 | we are defining a symbol, ensure one of them is set. */ | |
10590 | if ((s->flags & BSF_LOCAL) == 0) | |
10591 | s->flags |= BSF_GLOBAL; | |
6ba2a415 | 10592 | s->flags |= BSF_SYNTHETIC; |
4c45e5c9 JJ |
10593 | s->section = plt; |
10594 | s->value = addr - plt->vma; | |
10595 | s->name = names; | |
8f39ba8e | 10596 | s->udata.p = NULL; |
4c45e5c9 JJ |
10597 | len = strlen ((*p->sym_ptr_ptr)->name); |
10598 | memcpy (names, (*p->sym_ptr_ptr)->name, len); | |
10599 | names += len; | |
041de40d AM |
10600 | if (p->addend != 0) |
10601 | { | |
1d770845 | 10602 | char buf[30], *a; |
d324f6d6 | 10603 | |
041de40d AM |
10604 | memcpy (names, "+0x", sizeof ("+0x") - 1); |
10605 | names += sizeof ("+0x") - 1; | |
1d770845 L |
10606 | bfd_sprintf_vma (abfd, buf, p->addend); |
10607 | for (a = buf; *a == '0'; ++a) | |
10608 | ; | |
10609 | len = strlen (a); | |
10610 | memcpy (names, a, len); | |
10611 | names += len; | |
041de40d | 10612 | } |
4c45e5c9 JJ |
10613 | memcpy (names, "@plt", sizeof ("@plt")); |
10614 | names += sizeof ("@plt"); | |
8f39ba8e | 10615 | ++s, ++n; |
4c45e5c9 JJ |
10616 | } |
10617 | ||
10618 | return n; | |
10619 | } | |
3d7f7666 | 10620 | |
3b22753a L |
10621 | /* It is only used by x86-64 so far. */ |
10622 | asection _bfd_elf_large_com_section | |
10623 | = BFD_FAKE_SECTION (_bfd_elf_large_com_section, | |
f592407e | 10624 | SEC_IS_COMMON, NULL, "LARGE_COMMON", 0); |
ecca9871 | 10625 | |
d1036acb | 10626 | void |
78245035 L |
10627 | _bfd_elf_post_process_headers (bfd * abfd, |
10628 | struct bfd_link_info * link_info ATTRIBUTE_UNUSED) | |
d1036acb L |
10629 | { |
10630 | Elf_Internal_Ehdr * i_ehdrp; /* ELF file header, internal form. */ | |
10631 | ||
10632 | i_ehdrp = elf_elfheader (abfd); | |
10633 | ||
10634 | i_ehdrp->e_ident[EI_OSABI] = get_elf_backend_data (abfd)->elf_osabi; | |
d8045f23 NC |
10635 | |
10636 | /* To make things simpler for the loader on Linux systems we set the | |
9c55345c | 10637 | osabi field to ELFOSABI_GNU if the binary contains symbols of |
f64b2e8d | 10638 | the STT_GNU_IFUNC type or STB_GNU_UNIQUE binding. */ |
d8045f23 | 10639 | if (i_ehdrp->e_ident[EI_OSABI] == ELFOSABI_NONE |
f64b2e8d | 10640 | && elf_tdata (abfd)->has_gnu_symbols) |
9c55345c | 10641 | i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_GNU; |
d1036acb | 10642 | } |
fcb93ecf PB |
10643 | |
10644 | ||
10645 | /* Return TRUE for ELF symbol types that represent functions. | |
10646 | This is the default version of this function, which is sufficient for | |
d8045f23 | 10647 | most targets. It returns true if TYPE is STT_FUNC or STT_GNU_IFUNC. */ |
fcb93ecf PB |
10648 | |
10649 | bfd_boolean | |
10650 | _bfd_elf_is_function_type (unsigned int type) | |
10651 | { | |
d8045f23 NC |
10652 | return (type == STT_FUNC |
10653 | || type == STT_GNU_IFUNC); | |
fcb93ecf | 10654 | } |
9f296da3 | 10655 | |
aef36ac1 AM |
10656 | /* If the ELF symbol SYM might be a function in SEC, return the |
10657 | function size and set *CODE_OFF to the function's entry point, | |
10658 | otherwise return zero. */ | |
9f296da3 | 10659 | |
aef36ac1 AM |
10660 | bfd_size_type |
10661 | _bfd_elf_maybe_function_sym (const asymbol *sym, asection *sec, | |
10662 | bfd_vma *code_off) | |
9f296da3 | 10663 | { |
aef36ac1 AM |
10664 | bfd_size_type size; |
10665 | ||
ff9e0f5b | 10666 | if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT |
aef36ac1 AM |
10667 | | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0 |
10668 | || sym->section != sec) | |
10669 | return 0; | |
ff9e0f5b | 10670 | |
ff9e0f5b | 10671 | *code_off = sym->value; |
aef36ac1 AM |
10672 | size = 0; |
10673 | if (!(sym->flags & BSF_SYNTHETIC)) | |
10674 | size = ((elf_symbol_type *) sym)->internal_elf_sym.st_size; | |
10675 | if (size == 0) | |
10676 | size = 1; | |
10677 | return size; | |
9f296da3 | 10678 | } |