Commit | Line | Data |
---|---|---|
252b5132 | 1 | /* ELF executable support for BFD. |
340b6d91 AC |
2 | |
3 | Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, | |
415f38a6 | 4 | 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010 |
72a80a16 | 5 | Free Software Foundation, Inc. |
252b5132 | 6 | |
5e8d7549 | 7 | This file is part of BFD, the Binary File Descriptor library. |
252b5132 | 8 | |
5e8d7549 NC |
9 | This program is free software; you can redistribute it and/or modify |
10 | it under the terms of the GNU General Public License as published by | |
cd123cb7 | 11 | the Free Software Foundation; either version 3 of the License, or |
5e8d7549 | 12 | (at your option) any later version. |
252b5132 | 13 | |
5e8d7549 NC |
14 | This program is distributed in the hope that it will be useful, |
15 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 | GNU General Public License for more details. | |
252b5132 | 18 | |
5e8d7549 | 19 | You should have received a copy of the GNU General Public License |
b34976b6 | 20 | along with this program; if not, write to the Free Software |
cd123cb7 NC |
21 | Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, |
22 | MA 02110-1301, USA. */ | |
23 | ||
252b5132 | 24 | |
1b74d094 BW |
25 | /* |
26 | SECTION | |
252b5132 RH |
27 | ELF backends |
28 | ||
29 | BFD support for ELF formats is being worked on. | |
30 | Currently, the best supported back ends are for sparc and i386 | |
31 | (running svr4 or Solaris 2). | |
32 | ||
33 | Documentation of the internals of the support code still needs | |
34 | to be written. The code is changing quickly enough that we | |
661a3fd4 | 35 | haven't bothered yet. */ |
252b5132 | 36 | |
7ee38065 MS |
37 | /* For sparc64-cross-sparc32. */ |
38 | #define _SYSCALL32 | |
252b5132 | 39 | #include "sysdep.h" |
3db64b00 | 40 | #include "bfd.h" |
252b5132 RH |
41 | #include "bfdlink.h" |
42 | #include "libbfd.h" | |
43 | #define ARCH_SIZE 0 | |
44 | #include "elf-bfd.h" | |
e0e8c97f | 45 | #include "libiberty.h" |
ff59fc36 | 46 | #include "safe-ctype.h" |
252b5132 | 47 | |
8bc7f138 L |
48 | #ifdef CORE_HEADER |
49 | #include CORE_HEADER | |
50 | #endif | |
51 | ||
217aa764 | 52 | static int elf_sort_sections (const void *, const void *); |
c84fca4d | 53 | static bfd_boolean assign_file_positions_except_relocs (bfd *, struct bfd_link_info *); |
217aa764 AM |
54 | static bfd_boolean prep_headers (bfd *); |
55 | static bfd_boolean swap_out_syms (bfd *, struct bfd_strtab_hash **, int) ; | |
718175fa JK |
56 | static bfd_boolean elf_read_notes (bfd *, file_ptr, bfd_size_type) ; |
57 | static bfd_boolean elf_parse_notes (bfd *abfd, char *buf, size_t size, | |
58 | file_ptr offset); | |
50b2bdb7 | 59 | |
252b5132 RH |
60 | /* Swap version information in and out. The version information is |
61 | currently size independent. If that ever changes, this code will | |
62 | need to move into elfcode.h. */ | |
63 | ||
64 | /* Swap in a Verdef structure. */ | |
65 | ||
66 | void | |
217aa764 AM |
67 | _bfd_elf_swap_verdef_in (bfd *abfd, |
68 | const Elf_External_Verdef *src, | |
69 | Elf_Internal_Verdef *dst) | |
252b5132 | 70 | { |
dc810e39 AM |
71 | dst->vd_version = H_GET_16 (abfd, src->vd_version); |
72 | dst->vd_flags = H_GET_16 (abfd, src->vd_flags); | |
73 | dst->vd_ndx = H_GET_16 (abfd, src->vd_ndx); | |
74 | dst->vd_cnt = H_GET_16 (abfd, src->vd_cnt); | |
75 | dst->vd_hash = H_GET_32 (abfd, src->vd_hash); | |
76 | dst->vd_aux = H_GET_32 (abfd, src->vd_aux); | |
77 | dst->vd_next = H_GET_32 (abfd, src->vd_next); | |
252b5132 RH |
78 | } |
79 | ||
80 | /* Swap out a Verdef structure. */ | |
81 | ||
82 | void | |
217aa764 AM |
83 | _bfd_elf_swap_verdef_out (bfd *abfd, |
84 | const Elf_Internal_Verdef *src, | |
85 | Elf_External_Verdef *dst) | |
252b5132 | 86 | { |
dc810e39 AM |
87 | H_PUT_16 (abfd, src->vd_version, dst->vd_version); |
88 | H_PUT_16 (abfd, src->vd_flags, dst->vd_flags); | |
89 | H_PUT_16 (abfd, src->vd_ndx, dst->vd_ndx); | |
90 | H_PUT_16 (abfd, src->vd_cnt, dst->vd_cnt); | |
91 | H_PUT_32 (abfd, src->vd_hash, dst->vd_hash); | |
92 | H_PUT_32 (abfd, src->vd_aux, dst->vd_aux); | |
93 | H_PUT_32 (abfd, src->vd_next, dst->vd_next); | |
252b5132 RH |
94 | } |
95 | ||
96 | /* Swap in a Verdaux structure. */ | |
97 | ||
98 | void | |
217aa764 AM |
99 | _bfd_elf_swap_verdaux_in (bfd *abfd, |
100 | const Elf_External_Verdaux *src, | |
101 | Elf_Internal_Verdaux *dst) | |
252b5132 | 102 | { |
dc810e39 AM |
103 | dst->vda_name = H_GET_32 (abfd, src->vda_name); |
104 | dst->vda_next = H_GET_32 (abfd, src->vda_next); | |
252b5132 RH |
105 | } |
106 | ||
107 | /* Swap out a Verdaux structure. */ | |
108 | ||
109 | void | |
217aa764 AM |
110 | _bfd_elf_swap_verdaux_out (bfd *abfd, |
111 | const Elf_Internal_Verdaux *src, | |
112 | Elf_External_Verdaux *dst) | |
252b5132 | 113 | { |
dc810e39 AM |
114 | H_PUT_32 (abfd, src->vda_name, dst->vda_name); |
115 | H_PUT_32 (abfd, src->vda_next, dst->vda_next); | |
252b5132 RH |
116 | } |
117 | ||
118 | /* Swap in a Verneed structure. */ | |
119 | ||
120 | void | |
217aa764 AM |
121 | _bfd_elf_swap_verneed_in (bfd *abfd, |
122 | const Elf_External_Verneed *src, | |
123 | Elf_Internal_Verneed *dst) | |
252b5132 | 124 | { |
dc810e39 AM |
125 | dst->vn_version = H_GET_16 (abfd, src->vn_version); |
126 | dst->vn_cnt = H_GET_16 (abfd, src->vn_cnt); | |
127 | dst->vn_file = H_GET_32 (abfd, src->vn_file); | |
128 | dst->vn_aux = H_GET_32 (abfd, src->vn_aux); | |
129 | dst->vn_next = H_GET_32 (abfd, src->vn_next); | |
252b5132 RH |
130 | } |
131 | ||
132 | /* Swap out a Verneed structure. */ | |
133 | ||
134 | void | |
217aa764 AM |
135 | _bfd_elf_swap_verneed_out (bfd *abfd, |
136 | const Elf_Internal_Verneed *src, | |
137 | Elf_External_Verneed *dst) | |
252b5132 | 138 | { |
dc810e39 AM |
139 | H_PUT_16 (abfd, src->vn_version, dst->vn_version); |
140 | H_PUT_16 (abfd, src->vn_cnt, dst->vn_cnt); | |
141 | H_PUT_32 (abfd, src->vn_file, dst->vn_file); | |
142 | H_PUT_32 (abfd, src->vn_aux, dst->vn_aux); | |
143 | H_PUT_32 (abfd, src->vn_next, dst->vn_next); | |
252b5132 RH |
144 | } |
145 | ||
146 | /* Swap in a Vernaux structure. */ | |
147 | ||
148 | void | |
217aa764 AM |
149 | _bfd_elf_swap_vernaux_in (bfd *abfd, |
150 | const Elf_External_Vernaux *src, | |
151 | Elf_Internal_Vernaux *dst) | |
252b5132 | 152 | { |
dc810e39 AM |
153 | dst->vna_hash = H_GET_32 (abfd, src->vna_hash); |
154 | dst->vna_flags = H_GET_16 (abfd, src->vna_flags); | |
155 | dst->vna_other = H_GET_16 (abfd, src->vna_other); | |
156 | dst->vna_name = H_GET_32 (abfd, src->vna_name); | |
157 | dst->vna_next = H_GET_32 (abfd, src->vna_next); | |
252b5132 RH |
158 | } |
159 | ||
160 | /* Swap out a Vernaux structure. */ | |
161 | ||
162 | void | |
217aa764 AM |
163 | _bfd_elf_swap_vernaux_out (bfd *abfd, |
164 | const Elf_Internal_Vernaux *src, | |
165 | Elf_External_Vernaux *dst) | |
252b5132 | 166 | { |
dc810e39 AM |
167 | H_PUT_32 (abfd, src->vna_hash, dst->vna_hash); |
168 | H_PUT_16 (abfd, src->vna_flags, dst->vna_flags); | |
169 | H_PUT_16 (abfd, src->vna_other, dst->vna_other); | |
170 | H_PUT_32 (abfd, src->vna_name, dst->vna_name); | |
171 | H_PUT_32 (abfd, src->vna_next, dst->vna_next); | |
252b5132 RH |
172 | } |
173 | ||
174 | /* Swap in a Versym structure. */ | |
175 | ||
176 | void | |
217aa764 AM |
177 | _bfd_elf_swap_versym_in (bfd *abfd, |
178 | const Elf_External_Versym *src, | |
179 | Elf_Internal_Versym *dst) | |
252b5132 | 180 | { |
dc810e39 | 181 | dst->vs_vers = H_GET_16 (abfd, src->vs_vers); |
252b5132 RH |
182 | } |
183 | ||
184 | /* Swap out a Versym structure. */ | |
185 | ||
186 | void | |
217aa764 AM |
187 | _bfd_elf_swap_versym_out (bfd *abfd, |
188 | const Elf_Internal_Versym *src, | |
189 | Elf_External_Versym *dst) | |
252b5132 | 190 | { |
dc810e39 | 191 | H_PUT_16 (abfd, src->vs_vers, dst->vs_vers); |
252b5132 RH |
192 | } |
193 | ||
194 | /* Standard ELF hash function. Do not change this function; you will | |
195 | cause invalid hash tables to be generated. */ | |
3a99b017 | 196 | |
252b5132 | 197 | unsigned long |
217aa764 | 198 | bfd_elf_hash (const char *namearg) |
252b5132 | 199 | { |
3a99b017 | 200 | const unsigned char *name = (const unsigned char *) namearg; |
252b5132 RH |
201 | unsigned long h = 0; |
202 | unsigned long g; | |
203 | int ch; | |
204 | ||
205 | while ((ch = *name++) != '\0') | |
206 | { | |
207 | h = (h << 4) + ch; | |
208 | if ((g = (h & 0xf0000000)) != 0) | |
209 | { | |
210 | h ^= g >> 24; | |
211 | /* The ELF ABI says `h &= ~g', but this is equivalent in | |
212 | this case and on some machines one insn instead of two. */ | |
213 | h ^= g; | |
214 | } | |
215 | } | |
32dfa85d | 216 | return h & 0xffffffff; |
252b5132 RH |
217 | } |
218 | ||
fdc90cb4 JJ |
219 | /* DT_GNU_HASH hash function. Do not change this function; you will |
220 | cause invalid hash tables to be generated. */ | |
221 | ||
222 | unsigned long | |
223 | bfd_elf_gnu_hash (const char *namearg) | |
224 | { | |
225 | const unsigned char *name = (const unsigned char *) namearg; | |
226 | unsigned long h = 5381; | |
227 | unsigned char ch; | |
228 | ||
229 | while ((ch = *name++) != '\0') | |
230 | h = (h << 5) + h + ch; | |
231 | return h & 0xffffffff; | |
232 | } | |
233 | ||
0c8d6e5c AM |
234 | /* Create a tdata field OBJECT_SIZE bytes in length, zeroed out and with |
235 | the object_id field of an elf_obj_tdata field set to OBJECT_ID. */ | |
b34976b6 | 236 | bfd_boolean |
0c8d6e5c | 237 | bfd_elf_allocate_object (bfd *abfd, |
0ffa91dd | 238 | size_t object_size, |
4dfe6ac6 | 239 | enum elf_target_id object_id) |
252b5132 | 240 | { |
0ffa91dd NC |
241 | BFD_ASSERT (object_size >= sizeof (struct elf_obj_tdata)); |
242 | abfd->tdata.any = bfd_zalloc (abfd, object_size); | |
243 | if (abfd->tdata.any == NULL) | |
244 | return FALSE; | |
252b5132 | 245 | |
0ffa91dd NC |
246 | elf_object_id (abfd) = object_id; |
247 | elf_program_header_size (abfd) = (bfd_size_type) -1; | |
b34976b6 | 248 | return TRUE; |
252b5132 RH |
249 | } |
250 | ||
0ffa91dd NC |
251 | |
252 | bfd_boolean | |
ae95ffa6 | 253 | bfd_elf_make_object (bfd *abfd) |
0ffa91dd | 254 | { |
ae95ffa6 | 255 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
0ffa91dd | 256 | return bfd_elf_allocate_object (abfd, sizeof (struct elf_obj_tdata), |
ae95ffa6 | 257 | bed->target_id); |
0ffa91dd NC |
258 | } |
259 | ||
b34976b6 | 260 | bfd_boolean |
217aa764 | 261 | bfd_elf_mkcorefile (bfd *abfd) |
252b5132 | 262 | { |
c044fabd | 263 | /* I think this can be done just like an object file. */ |
ae95ffa6 | 264 | return abfd->xvec->_bfd_set_format[(int) bfd_object] (abfd); |
252b5132 RH |
265 | } |
266 | ||
72a80a16 | 267 | static char * |
217aa764 | 268 | bfd_elf_get_str_section (bfd *abfd, unsigned int shindex) |
252b5132 RH |
269 | { |
270 | Elf_Internal_Shdr **i_shdrp; | |
f075ee0c | 271 | bfd_byte *shstrtab = NULL; |
dc810e39 AM |
272 | file_ptr offset; |
273 | bfd_size_type shstrtabsize; | |
252b5132 RH |
274 | |
275 | i_shdrp = elf_elfsections (abfd); | |
74f2e02b AM |
276 | if (i_shdrp == 0 |
277 | || shindex >= elf_numsections (abfd) | |
278 | || i_shdrp[shindex] == 0) | |
f075ee0c | 279 | return NULL; |
252b5132 | 280 | |
f075ee0c | 281 | shstrtab = i_shdrp[shindex]->contents; |
252b5132 RH |
282 | if (shstrtab == NULL) |
283 | { | |
c044fabd | 284 | /* No cached one, attempt to read, and cache what we read. */ |
252b5132 RH |
285 | offset = i_shdrp[shindex]->sh_offset; |
286 | shstrtabsize = i_shdrp[shindex]->sh_size; | |
c6c60d09 JJ |
287 | |
288 | /* Allocate and clear an extra byte at the end, to prevent crashes | |
289 | in case the string table is not terminated. */ | |
3471d59d | 290 | if (shstrtabsize + 1 <= 1 |
a50b1753 | 291 | || (shstrtab = (bfd_byte *) bfd_alloc (abfd, shstrtabsize + 1)) == NULL |
c6c60d09 JJ |
292 | || bfd_seek (abfd, offset, SEEK_SET) != 0) |
293 | shstrtab = NULL; | |
294 | else if (bfd_bread (shstrtab, shstrtabsize, abfd) != shstrtabsize) | |
295 | { | |
296 | if (bfd_get_error () != bfd_error_system_call) | |
297 | bfd_set_error (bfd_error_file_truncated); | |
298 | shstrtab = NULL; | |
3471d59d CC |
299 | /* Once we've failed to read it, make sure we don't keep |
300 | trying. Otherwise, we'll keep allocating space for | |
301 | the string table over and over. */ | |
302 | i_shdrp[shindex]->sh_size = 0; | |
c6c60d09 JJ |
303 | } |
304 | else | |
305 | shstrtab[shstrtabsize] = '\0'; | |
217aa764 | 306 | i_shdrp[shindex]->contents = shstrtab; |
252b5132 | 307 | } |
f075ee0c | 308 | return (char *) shstrtab; |
252b5132 RH |
309 | } |
310 | ||
311 | char * | |
217aa764 AM |
312 | bfd_elf_string_from_elf_section (bfd *abfd, |
313 | unsigned int shindex, | |
314 | unsigned int strindex) | |
252b5132 RH |
315 | { |
316 | Elf_Internal_Shdr *hdr; | |
317 | ||
318 | if (strindex == 0) | |
319 | return ""; | |
320 | ||
74f2e02b AM |
321 | if (elf_elfsections (abfd) == NULL || shindex >= elf_numsections (abfd)) |
322 | return NULL; | |
323 | ||
252b5132 RH |
324 | hdr = elf_elfsections (abfd)[shindex]; |
325 | ||
326 | if (hdr->contents == NULL | |
327 | && bfd_elf_get_str_section (abfd, shindex) == NULL) | |
328 | return NULL; | |
329 | ||
330 | if (strindex >= hdr->sh_size) | |
331 | { | |
1b3a8575 | 332 | unsigned int shstrndx = elf_elfheader(abfd)->e_shstrndx; |
252b5132 | 333 | (*_bfd_error_handler) |
d003868e AM |
334 | (_("%B: invalid string offset %u >= %lu for section `%s'"), |
335 | abfd, strindex, (unsigned long) hdr->sh_size, | |
1b3a8575 | 336 | (shindex == shstrndx && strindex == hdr->sh_name |
252b5132 | 337 | ? ".shstrtab" |
1b3a8575 | 338 | : bfd_elf_string_from_elf_section (abfd, shstrndx, hdr->sh_name))); |
45b222d6 | 339 | return NULL; |
252b5132 RH |
340 | } |
341 | ||
342 | return ((char *) hdr->contents) + strindex; | |
343 | } | |
344 | ||
6cdc0ccc AM |
345 | /* Read and convert symbols to internal format. |
346 | SYMCOUNT specifies the number of symbols to read, starting from | |
347 | symbol SYMOFFSET. If any of INTSYM_BUF, EXTSYM_BUF or EXTSHNDX_BUF | |
348 | are non-NULL, they are used to store the internal symbols, external | |
b7c368d0 NC |
349 | symbols, and symbol section index extensions, respectively. |
350 | Returns a pointer to the internal symbol buffer (malloced if necessary) | |
351 | or NULL if there were no symbols or some kind of problem. */ | |
6cdc0ccc AM |
352 | |
353 | Elf_Internal_Sym * | |
217aa764 AM |
354 | bfd_elf_get_elf_syms (bfd *ibfd, |
355 | Elf_Internal_Shdr *symtab_hdr, | |
356 | size_t symcount, | |
357 | size_t symoffset, | |
358 | Elf_Internal_Sym *intsym_buf, | |
359 | void *extsym_buf, | |
360 | Elf_External_Sym_Shndx *extshndx_buf) | |
6cdc0ccc AM |
361 | { |
362 | Elf_Internal_Shdr *shndx_hdr; | |
217aa764 | 363 | void *alloc_ext; |
df622259 | 364 | const bfd_byte *esym; |
6cdc0ccc AM |
365 | Elf_External_Sym_Shndx *alloc_extshndx; |
366 | Elf_External_Sym_Shndx *shndx; | |
4dd07732 | 367 | Elf_Internal_Sym *alloc_intsym; |
6cdc0ccc AM |
368 | Elf_Internal_Sym *isym; |
369 | Elf_Internal_Sym *isymend; | |
9c5bfbb7 | 370 | const struct elf_backend_data *bed; |
6cdc0ccc AM |
371 | size_t extsym_size; |
372 | bfd_size_type amt; | |
373 | file_ptr pos; | |
374 | ||
e44a2c9c AM |
375 | if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour) |
376 | abort (); | |
377 | ||
6cdc0ccc AM |
378 | if (symcount == 0) |
379 | return intsym_buf; | |
380 | ||
381 | /* Normal syms might have section extension entries. */ | |
382 | shndx_hdr = NULL; | |
383 | if (symtab_hdr == &elf_tdata (ibfd)->symtab_hdr) | |
384 | shndx_hdr = &elf_tdata (ibfd)->symtab_shndx_hdr; | |
385 | ||
386 | /* Read the symbols. */ | |
387 | alloc_ext = NULL; | |
388 | alloc_extshndx = NULL; | |
4dd07732 | 389 | alloc_intsym = NULL; |
6cdc0ccc AM |
390 | bed = get_elf_backend_data (ibfd); |
391 | extsym_size = bed->s->sizeof_sym; | |
392 | amt = symcount * extsym_size; | |
393 | pos = symtab_hdr->sh_offset + symoffset * extsym_size; | |
394 | if (extsym_buf == NULL) | |
395 | { | |
d0fb9a8d | 396 | alloc_ext = bfd_malloc2 (symcount, extsym_size); |
6cdc0ccc AM |
397 | extsym_buf = alloc_ext; |
398 | } | |
399 | if (extsym_buf == NULL | |
400 | || bfd_seek (ibfd, pos, SEEK_SET) != 0 | |
401 | || bfd_bread (extsym_buf, amt, ibfd) != amt) | |
402 | { | |
403 | intsym_buf = NULL; | |
404 | goto out; | |
405 | } | |
406 | ||
407 | if (shndx_hdr == NULL || shndx_hdr->sh_size == 0) | |
408 | extshndx_buf = NULL; | |
409 | else | |
410 | { | |
411 | amt = symcount * sizeof (Elf_External_Sym_Shndx); | |
412 | pos = shndx_hdr->sh_offset + symoffset * sizeof (Elf_External_Sym_Shndx); | |
413 | if (extshndx_buf == NULL) | |
414 | { | |
a50b1753 NC |
415 | alloc_extshndx = (Elf_External_Sym_Shndx *) |
416 | bfd_malloc2 (symcount, sizeof (Elf_External_Sym_Shndx)); | |
6cdc0ccc AM |
417 | extshndx_buf = alloc_extshndx; |
418 | } | |
419 | if (extshndx_buf == NULL | |
420 | || bfd_seek (ibfd, pos, SEEK_SET) != 0 | |
421 | || bfd_bread (extshndx_buf, amt, ibfd) != amt) | |
422 | { | |
423 | intsym_buf = NULL; | |
424 | goto out; | |
425 | } | |
426 | } | |
427 | ||
428 | if (intsym_buf == NULL) | |
429 | { | |
a50b1753 NC |
430 | alloc_intsym = (Elf_Internal_Sym *) |
431 | bfd_malloc2 (symcount, sizeof (Elf_Internal_Sym)); | |
4dd07732 | 432 | intsym_buf = alloc_intsym; |
6cdc0ccc AM |
433 | if (intsym_buf == NULL) |
434 | goto out; | |
435 | } | |
436 | ||
437 | /* Convert the symbols to internal form. */ | |
438 | isymend = intsym_buf + symcount; | |
a50b1753 NC |
439 | for (esym = (const bfd_byte *) extsym_buf, isym = intsym_buf, |
440 | shndx = extshndx_buf; | |
6cdc0ccc AM |
441 | isym < isymend; |
442 | esym += extsym_size, isym++, shndx = shndx != NULL ? shndx + 1 : NULL) | |
8384fb8f AM |
443 | if (!(*bed->s->swap_symbol_in) (ibfd, esym, shndx, isym)) |
444 | { | |
445 | symoffset += (esym - (bfd_byte *) extsym_buf) / extsym_size; | |
446 | (*_bfd_error_handler) (_("%B symbol number %lu references " | |
447 | "nonexistent SHT_SYMTAB_SHNDX section"), | |
448 | ibfd, (unsigned long) symoffset); | |
4dd07732 AM |
449 | if (alloc_intsym != NULL) |
450 | free (alloc_intsym); | |
8384fb8f AM |
451 | intsym_buf = NULL; |
452 | goto out; | |
453 | } | |
6cdc0ccc AM |
454 | |
455 | out: | |
456 | if (alloc_ext != NULL) | |
457 | free (alloc_ext); | |
458 | if (alloc_extshndx != NULL) | |
459 | free (alloc_extshndx); | |
460 | ||
461 | return intsym_buf; | |
462 | } | |
463 | ||
5cab59f6 AM |
464 | /* Look up a symbol name. */ |
465 | const char * | |
be8dd2ca AM |
466 | bfd_elf_sym_name (bfd *abfd, |
467 | Elf_Internal_Shdr *symtab_hdr, | |
26c61ae5 L |
468 | Elf_Internal_Sym *isym, |
469 | asection *sym_sec) | |
5cab59f6 | 470 | { |
26c61ae5 | 471 | const char *name; |
5cab59f6 | 472 | unsigned int iname = isym->st_name; |
be8dd2ca | 473 | unsigned int shindex = symtab_hdr->sh_link; |
26c61ae5 | 474 | |
138f35cc JJ |
475 | if (iname == 0 && ELF_ST_TYPE (isym->st_info) == STT_SECTION |
476 | /* Check for a bogus st_shndx to avoid crashing. */ | |
4fbb74a6 | 477 | && isym->st_shndx < elf_numsections (abfd)) |
5cab59f6 AM |
478 | { |
479 | iname = elf_elfsections (abfd)[isym->st_shndx]->sh_name; | |
480 | shindex = elf_elfheader (abfd)->e_shstrndx; | |
481 | } | |
482 | ||
26c61ae5 L |
483 | name = bfd_elf_string_from_elf_section (abfd, shindex, iname); |
484 | if (name == NULL) | |
485 | name = "(null)"; | |
486 | else if (sym_sec && *name == '\0') | |
487 | name = bfd_section_name (abfd, sym_sec); | |
488 | ||
489 | return name; | |
5cab59f6 AM |
490 | } |
491 | ||
dbb410c3 AM |
492 | /* Elf_Internal_Shdr->contents is an array of these for SHT_GROUP |
493 | sections. The first element is the flags, the rest are section | |
494 | pointers. */ | |
495 | ||
496 | typedef union elf_internal_group { | |
497 | Elf_Internal_Shdr *shdr; | |
498 | unsigned int flags; | |
499 | } Elf_Internal_Group; | |
500 | ||
b885599b AM |
501 | /* Return the name of the group signature symbol. Why isn't the |
502 | signature just a string? */ | |
503 | ||
504 | static const char * | |
217aa764 | 505 | group_signature (bfd *abfd, Elf_Internal_Shdr *ghdr) |
b885599b | 506 | { |
9dce4196 | 507 | Elf_Internal_Shdr *hdr; |
9dce4196 AM |
508 | unsigned char esym[sizeof (Elf64_External_Sym)]; |
509 | Elf_External_Sym_Shndx eshndx; | |
510 | Elf_Internal_Sym isym; | |
b885599b | 511 | |
13792e9d L |
512 | /* First we need to ensure the symbol table is available. Make sure |
513 | that it is a symbol table section. */ | |
4fbb74a6 AM |
514 | if (ghdr->sh_link >= elf_numsections (abfd)) |
515 | return NULL; | |
13792e9d L |
516 | hdr = elf_elfsections (abfd) [ghdr->sh_link]; |
517 | if (hdr->sh_type != SHT_SYMTAB | |
518 | || ! bfd_section_from_shdr (abfd, ghdr->sh_link)) | |
b885599b AM |
519 | return NULL; |
520 | ||
9dce4196 AM |
521 | /* Go read the symbol. */ |
522 | hdr = &elf_tdata (abfd)->symtab_hdr; | |
6cdc0ccc AM |
523 | if (bfd_elf_get_elf_syms (abfd, hdr, 1, ghdr->sh_info, |
524 | &isym, esym, &eshndx) == NULL) | |
b885599b | 525 | return NULL; |
9dce4196 | 526 | |
26c61ae5 | 527 | return bfd_elf_sym_name (abfd, hdr, &isym, NULL); |
b885599b AM |
528 | } |
529 | ||
dbb410c3 AM |
530 | /* Set next_in_group list pointer, and group name for NEWSECT. */ |
531 | ||
b34976b6 | 532 | static bfd_boolean |
217aa764 | 533 | setup_group (bfd *abfd, Elf_Internal_Shdr *hdr, asection *newsect) |
dbb410c3 AM |
534 | { |
535 | unsigned int num_group = elf_tdata (abfd)->num_group; | |
536 | ||
537 | /* If num_group is zero, read in all SHT_GROUP sections. The count | |
538 | is set to -1 if there are no SHT_GROUP sections. */ | |
539 | if (num_group == 0) | |
540 | { | |
541 | unsigned int i, shnum; | |
542 | ||
543 | /* First count the number of groups. If we have a SHT_GROUP | |
544 | section with just a flag word (ie. sh_size is 4), ignore it. */ | |
9ad5cbcf | 545 | shnum = elf_numsections (abfd); |
dbb410c3 | 546 | num_group = 0; |
08a40648 | 547 | |
1783205a NC |
548 | #define IS_VALID_GROUP_SECTION_HEADER(shdr) \ |
549 | ( (shdr)->sh_type == SHT_GROUP \ | |
550 | && (shdr)->sh_size >= (2 * GRP_ENTRY_SIZE) \ | |
551 | && (shdr)->sh_entsize == GRP_ENTRY_SIZE \ | |
552 | && ((shdr)->sh_size % GRP_ENTRY_SIZE) == 0) | |
08a40648 | 553 | |
dbb410c3 AM |
554 | for (i = 0; i < shnum; i++) |
555 | { | |
556 | Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i]; | |
1783205a NC |
557 | |
558 | if (IS_VALID_GROUP_SECTION_HEADER (shdr)) | |
dbb410c3 AM |
559 | num_group += 1; |
560 | } | |
561 | ||
562 | if (num_group == 0) | |
20dbb49d L |
563 | { |
564 | num_group = (unsigned) -1; | |
565 | elf_tdata (abfd)->num_group = num_group; | |
566 | } | |
567 | else | |
dbb410c3 AM |
568 | { |
569 | /* We keep a list of elf section headers for group sections, | |
570 | so we can find them quickly. */ | |
20dbb49d | 571 | bfd_size_type amt; |
d0fb9a8d | 572 | |
20dbb49d | 573 | elf_tdata (abfd)->num_group = num_group; |
a50b1753 NC |
574 | elf_tdata (abfd)->group_sect_ptr = (Elf_Internal_Shdr **) |
575 | bfd_alloc2 (abfd, num_group, sizeof (Elf_Internal_Shdr *)); | |
dbb410c3 | 576 | if (elf_tdata (abfd)->group_sect_ptr == NULL) |
b34976b6 | 577 | return FALSE; |
dbb410c3 AM |
578 | |
579 | num_group = 0; | |
580 | for (i = 0; i < shnum; i++) | |
581 | { | |
582 | Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i]; | |
1783205a NC |
583 | |
584 | if (IS_VALID_GROUP_SECTION_HEADER (shdr)) | |
dbb410c3 | 585 | { |
973ffd63 | 586 | unsigned char *src; |
dbb410c3 AM |
587 | Elf_Internal_Group *dest; |
588 | ||
589 | /* Add to list of sections. */ | |
590 | elf_tdata (abfd)->group_sect_ptr[num_group] = shdr; | |
591 | num_group += 1; | |
592 | ||
593 | /* Read the raw contents. */ | |
594 | BFD_ASSERT (sizeof (*dest) >= 4); | |
595 | amt = shdr->sh_size * sizeof (*dest) / 4; | |
a50b1753 NC |
596 | shdr->contents = (unsigned char *) |
597 | bfd_alloc2 (abfd, shdr->sh_size, sizeof (*dest) / 4); | |
1783205a NC |
598 | /* PR binutils/4110: Handle corrupt group headers. */ |
599 | if (shdr->contents == NULL) | |
600 | { | |
601 | _bfd_error_handler | |
602 | (_("%B: Corrupt size field in group section header: 0x%lx"), abfd, shdr->sh_size); | |
603 | bfd_set_error (bfd_error_bad_value); | |
604 | return FALSE; | |
605 | } | |
606 | ||
607 | memset (shdr->contents, 0, amt); | |
608 | ||
609 | if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0 | |
dbb410c3 AM |
610 | || (bfd_bread (shdr->contents, shdr->sh_size, abfd) |
611 | != shdr->sh_size)) | |
b34976b6 | 612 | return FALSE; |
dbb410c3 AM |
613 | |
614 | /* Translate raw contents, a flag word followed by an | |
615 | array of elf section indices all in target byte order, | |
616 | to the flag word followed by an array of elf section | |
617 | pointers. */ | |
618 | src = shdr->contents + shdr->sh_size; | |
619 | dest = (Elf_Internal_Group *) (shdr->contents + amt); | |
620 | while (1) | |
621 | { | |
622 | unsigned int idx; | |
623 | ||
624 | src -= 4; | |
625 | --dest; | |
626 | idx = H_GET_32 (abfd, src); | |
627 | if (src == shdr->contents) | |
628 | { | |
629 | dest->flags = idx; | |
b885599b AM |
630 | if (shdr->bfd_section != NULL && (idx & GRP_COMDAT)) |
631 | shdr->bfd_section->flags | |
632 | |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD; | |
dbb410c3 AM |
633 | break; |
634 | } | |
635 | if (idx >= shnum) | |
636 | { | |
637 | ((*_bfd_error_handler) | |
d003868e | 638 | (_("%B: invalid SHT_GROUP entry"), abfd)); |
dbb410c3 AM |
639 | idx = 0; |
640 | } | |
641 | dest->shdr = elf_elfsections (abfd)[idx]; | |
642 | } | |
643 | } | |
644 | } | |
645 | } | |
646 | } | |
647 | ||
648 | if (num_group != (unsigned) -1) | |
649 | { | |
650 | unsigned int i; | |
651 | ||
652 | for (i = 0; i < num_group; i++) | |
653 | { | |
654 | Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i]; | |
655 | Elf_Internal_Group *idx = (Elf_Internal_Group *) shdr->contents; | |
656 | unsigned int n_elt = shdr->sh_size / 4; | |
657 | ||
658 | /* Look through this group's sections to see if current | |
659 | section is a member. */ | |
660 | while (--n_elt != 0) | |
661 | if ((++idx)->shdr == hdr) | |
662 | { | |
e0e8c97f | 663 | asection *s = NULL; |
dbb410c3 AM |
664 | |
665 | /* We are a member of this group. Go looking through | |
666 | other members to see if any others are linked via | |
667 | next_in_group. */ | |
668 | idx = (Elf_Internal_Group *) shdr->contents; | |
669 | n_elt = shdr->sh_size / 4; | |
670 | while (--n_elt != 0) | |
671 | if ((s = (++idx)->shdr->bfd_section) != NULL | |
945906ff | 672 | && elf_next_in_group (s) != NULL) |
dbb410c3 AM |
673 | break; |
674 | if (n_elt != 0) | |
675 | { | |
dbb410c3 AM |
676 | /* Snarf the group name from other member, and |
677 | insert current section in circular list. */ | |
945906ff AM |
678 | elf_group_name (newsect) = elf_group_name (s); |
679 | elf_next_in_group (newsect) = elf_next_in_group (s); | |
680 | elf_next_in_group (s) = newsect; | |
dbb410c3 AM |
681 | } |
682 | else | |
683 | { | |
dbb410c3 AM |
684 | const char *gname; |
685 | ||
b885599b AM |
686 | gname = group_signature (abfd, shdr); |
687 | if (gname == NULL) | |
b34976b6 | 688 | return FALSE; |
945906ff | 689 | elf_group_name (newsect) = gname; |
dbb410c3 AM |
690 | |
691 | /* Start a circular list with one element. */ | |
945906ff | 692 | elf_next_in_group (newsect) = newsect; |
dbb410c3 | 693 | } |
b885599b | 694 | |
9dce4196 AM |
695 | /* If the group section has been created, point to the |
696 | new member. */ | |
dbb410c3 | 697 | if (shdr->bfd_section != NULL) |
945906ff | 698 | elf_next_in_group (shdr->bfd_section) = newsect; |
b885599b | 699 | |
dbb410c3 AM |
700 | i = num_group - 1; |
701 | break; | |
702 | } | |
703 | } | |
704 | } | |
705 | ||
945906ff | 706 | if (elf_group_name (newsect) == NULL) |
dbb410c3 | 707 | { |
d003868e AM |
708 | (*_bfd_error_handler) (_("%B: no group info for section %A"), |
709 | abfd, newsect); | |
dbb410c3 | 710 | } |
b34976b6 | 711 | return TRUE; |
dbb410c3 AM |
712 | } |
713 | ||
3d7f7666 | 714 | bfd_boolean |
dd863624 | 715 | _bfd_elf_setup_sections (bfd *abfd) |
3d7f7666 L |
716 | { |
717 | unsigned int i; | |
718 | unsigned int num_group = elf_tdata (abfd)->num_group; | |
719 | bfd_boolean result = TRUE; | |
dd863624 L |
720 | asection *s; |
721 | ||
722 | /* Process SHF_LINK_ORDER. */ | |
723 | for (s = abfd->sections; s != NULL; s = s->next) | |
724 | { | |
725 | Elf_Internal_Shdr *this_hdr = &elf_section_data (s)->this_hdr; | |
726 | if ((this_hdr->sh_flags & SHF_LINK_ORDER) != 0) | |
727 | { | |
728 | unsigned int elfsec = this_hdr->sh_link; | |
729 | /* FIXME: The old Intel compiler and old strip/objcopy may | |
730 | not set the sh_link or sh_info fields. Hence we could | |
731 | get the situation where elfsec is 0. */ | |
732 | if (elfsec == 0) | |
733 | { | |
4fbb74a6 | 734 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
dd863624 L |
735 | if (bed->link_order_error_handler) |
736 | bed->link_order_error_handler | |
737 | (_("%B: warning: sh_link not set for section `%A'"), | |
738 | abfd, s); | |
739 | } | |
740 | else | |
741 | { | |
91d6fa6a | 742 | asection *linksec = NULL; |
25bbc984 | 743 | |
4fbb74a6 AM |
744 | if (elfsec < elf_numsections (abfd)) |
745 | { | |
746 | this_hdr = elf_elfsections (abfd)[elfsec]; | |
91d6fa6a | 747 | linksec = this_hdr->bfd_section; |
4fbb74a6 | 748 | } |
25bbc984 L |
749 | |
750 | /* PR 1991, 2008: | |
751 | Some strip/objcopy may leave an incorrect value in | |
752 | sh_link. We don't want to proceed. */ | |
91d6fa6a | 753 | if (linksec == NULL) |
25bbc984 L |
754 | { |
755 | (*_bfd_error_handler) | |
756 | (_("%B: sh_link [%d] in section `%A' is incorrect"), | |
757 | s->owner, s, elfsec); | |
758 | result = FALSE; | |
759 | } | |
760 | ||
91d6fa6a | 761 | elf_linked_to_section (s) = linksec; |
dd863624 L |
762 | } |
763 | } | |
764 | } | |
3d7f7666 | 765 | |
dd863624 | 766 | /* Process section groups. */ |
3d7f7666 L |
767 | if (num_group == (unsigned) -1) |
768 | return result; | |
769 | ||
770 | for (i = 0; i < num_group; i++) | |
771 | { | |
772 | Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i]; | |
773 | Elf_Internal_Group *idx = (Elf_Internal_Group *) shdr->contents; | |
774 | unsigned int n_elt = shdr->sh_size / 4; | |
775 | ||
776 | while (--n_elt != 0) | |
777 | if ((++idx)->shdr->bfd_section) | |
778 | elf_sec_group (idx->shdr->bfd_section) = shdr->bfd_section; | |
779 | else if (idx->shdr->sh_type == SHT_RELA | |
780 | || idx->shdr->sh_type == SHT_REL) | |
781 | /* We won't include relocation sections in section groups in | |
782 | output object files. We adjust the group section size here | |
783 | so that relocatable link will work correctly when | |
784 | relocation sections are in section group in input object | |
785 | files. */ | |
786 | shdr->bfd_section->size -= 4; | |
787 | else | |
788 | { | |
789 | /* There are some unknown sections in the group. */ | |
790 | (*_bfd_error_handler) | |
d003868e AM |
791 | (_("%B: unknown [%d] section `%s' in group [%s]"), |
792 | abfd, | |
3d7f7666 | 793 | (unsigned int) idx->shdr->sh_type, |
1b3a8575 AM |
794 | bfd_elf_string_from_elf_section (abfd, |
795 | (elf_elfheader (abfd) | |
796 | ->e_shstrndx), | |
797 | idx->shdr->sh_name), | |
3d7f7666 L |
798 | shdr->bfd_section->name); |
799 | result = FALSE; | |
800 | } | |
801 | } | |
802 | return result; | |
803 | } | |
804 | ||
72adc230 AM |
805 | bfd_boolean |
806 | bfd_elf_is_group_section (bfd *abfd ATTRIBUTE_UNUSED, const asection *sec) | |
807 | { | |
808 | return elf_next_in_group (sec) != NULL; | |
809 | } | |
810 | ||
252b5132 RH |
811 | /* Make a BFD section from an ELF section. We store a pointer to the |
812 | BFD section in the bfd_section field of the header. */ | |
813 | ||
b34976b6 | 814 | bfd_boolean |
217aa764 AM |
815 | _bfd_elf_make_section_from_shdr (bfd *abfd, |
816 | Elf_Internal_Shdr *hdr, | |
6dc132d9 L |
817 | const char *name, |
818 | int shindex) | |
252b5132 RH |
819 | { |
820 | asection *newsect; | |
821 | flagword flags; | |
9c5bfbb7 | 822 | const struct elf_backend_data *bed; |
252b5132 RH |
823 | |
824 | if (hdr->bfd_section != NULL) | |
825 | { | |
826 | BFD_ASSERT (strcmp (name, | |
827 | bfd_get_section_name (abfd, hdr->bfd_section)) == 0); | |
b34976b6 | 828 | return TRUE; |
252b5132 RH |
829 | } |
830 | ||
831 | newsect = bfd_make_section_anyway (abfd, name); | |
832 | if (newsect == NULL) | |
b34976b6 | 833 | return FALSE; |
252b5132 | 834 | |
1829f4b2 AM |
835 | hdr->bfd_section = newsect; |
836 | elf_section_data (newsect)->this_hdr = *hdr; | |
6dc132d9 | 837 | elf_section_data (newsect)->this_idx = shindex; |
1829f4b2 | 838 | |
2f89ff8d L |
839 | /* Always use the real type/flags. */ |
840 | elf_section_type (newsect) = hdr->sh_type; | |
841 | elf_section_flags (newsect) = hdr->sh_flags; | |
842 | ||
252b5132 RH |
843 | newsect->filepos = hdr->sh_offset; |
844 | ||
845 | if (! bfd_set_section_vma (abfd, newsect, hdr->sh_addr) | |
846 | || ! bfd_set_section_size (abfd, newsect, hdr->sh_size) | |
847 | || ! bfd_set_section_alignment (abfd, newsect, | |
72de5009 | 848 | bfd_log2 (hdr->sh_addralign))) |
b34976b6 | 849 | return FALSE; |
252b5132 RH |
850 | |
851 | flags = SEC_NO_FLAGS; | |
852 | if (hdr->sh_type != SHT_NOBITS) | |
853 | flags |= SEC_HAS_CONTENTS; | |
dbb410c3 | 854 | if (hdr->sh_type == SHT_GROUP) |
b3096250 | 855 | flags |= SEC_GROUP | SEC_EXCLUDE; |
252b5132 RH |
856 | if ((hdr->sh_flags & SHF_ALLOC) != 0) |
857 | { | |
858 | flags |= SEC_ALLOC; | |
859 | if (hdr->sh_type != SHT_NOBITS) | |
860 | flags |= SEC_LOAD; | |
861 | } | |
862 | if ((hdr->sh_flags & SHF_WRITE) == 0) | |
863 | flags |= SEC_READONLY; | |
864 | if ((hdr->sh_flags & SHF_EXECINSTR) != 0) | |
865 | flags |= SEC_CODE; | |
866 | else if ((flags & SEC_LOAD) != 0) | |
867 | flags |= SEC_DATA; | |
f5fa8ca2 JJ |
868 | if ((hdr->sh_flags & SHF_MERGE) != 0) |
869 | { | |
870 | flags |= SEC_MERGE; | |
871 | newsect->entsize = hdr->sh_entsize; | |
872 | if ((hdr->sh_flags & SHF_STRINGS) != 0) | |
873 | flags |= SEC_STRINGS; | |
874 | } | |
dbb410c3 AM |
875 | if (hdr->sh_flags & SHF_GROUP) |
876 | if (!setup_group (abfd, hdr, newsect)) | |
b34976b6 | 877 | return FALSE; |
13ae64f3 JJ |
878 | if ((hdr->sh_flags & SHF_TLS) != 0) |
879 | flags |= SEC_THREAD_LOCAL; | |
18ae9cc1 L |
880 | if ((hdr->sh_flags & SHF_EXCLUDE) != 0) |
881 | flags |= SEC_EXCLUDE; | |
252b5132 | 882 | |
3d2b39cf | 883 | if ((flags & SEC_ALLOC) == 0) |
7a6cc5fb | 884 | { |
3d2b39cf L |
885 | /* The debugging sections appear to be recognized only by name, |
886 | not any sort of flag. Their SEC_ALLOC bits are cleared. */ | |
887 | static const struct | |
888 | { | |
889 | const char *name; | |
890 | int len; | |
891 | } debug_sections [] = | |
892 | { | |
0112cd26 | 893 | { STRING_COMMA_LEN ("debug") }, /* 'd' */ |
3d2b39cf L |
894 | { NULL, 0 }, /* 'e' */ |
895 | { NULL, 0 }, /* 'f' */ | |
0112cd26 | 896 | { STRING_COMMA_LEN ("gnu.linkonce.wi.") }, /* 'g' */ |
3d2b39cf L |
897 | { NULL, 0 }, /* 'h' */ |
898 | { NULL, 0 }, /* 'i' */ | |
899 | { NULL, 0 }, /* 'j' */ | |
900 | { NULL, 0 }, /* 'k' */ | |
0112cd26 | 901 | { STRING_COMMA_LEN ("line") }, /* 'l' */ |
3d2b39cf L |
902 | { NULL, 0 }, /* 'm' */ |
903 | { NULL, 0 }, /* 'n' */ | |
904 | { NULL, 0 }, /* 'o' */ | |
905 | { NULL, 0 }, /* 'p' */ | |
906 | { NULL, 0 }, /* 'q' */ | |
907 | { NULL, 0 }, /* 'r' */ | |
1b315056 CS |
908 | { STRING_COMMA_LEN ("stab") }, /* 's' */ |
909 | { NULL, 0 }, /* 't' */ | |
910 | { NULL, 0 }, /* 'u' */ | |
911 | { NULL, 0 }, /* 'v' */ | |
912 | { NULL, 0 }, /* 'w' */ | |
913 | { NULL, 0 }, /* 'x' */ | |
914 | { NULL, 0 }, /* 'y' */ | |
915 | { STRING_COMMA_LEN ("zdebug") } /* 'z' */ | |
3d2b39cf | 916 | }; |
08a40648 | 917 | |
3d2b39cf L |
918 | if (name [0] == '.') |
919 | { | |
920 | int i = name [1] - 'd'; | |
921 | if (i >= 0 | |
922 | && i < (int) ARRAY_SIZE (debug_sections) | |
923 | && debug_sections [i].name != NULL | |
924 | && strncmp (&name [1], debug_sections [i].name, | |
925 | debug_sections [i].len) == 0) | |
926 | flags |= SEC_DEBUGGING; | |
927 | } | |
928 | } | |
252b5132 RH |
929 | |
930 | /* As a GNU extension, if the name begins with .gnu.linkonce, we | |
931 | only link a single copy of the section. This is used to support | |
932 | g++. g++ will emit each template expansion in its own section. | |
933 | The symbols will be defined as weak, so that multiple definitions | |
934 | are permitted. The GNU linker extension is to actually discard | |
935 | all but one of the sections. */ | |
0112cd26 | 936 | if (CONST_STRNEQ (name, ".gnu.linkonce") |
b885599b | 937 | && elf_next_in_group (newsect) == NULL) |
252b5132 RH |
938 | flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD; |
939 | ||
fa152c49 JW |
940 | bed = get_elf_backend_data (abfd); |
941 | if (bed->elf_backend_section_flags) | |
942 | if (! bed->elf_backend_section_flags (&flags, hdr)) | |
b34976b6 | 943 | return FALSE; |
fa152c49 | 944 | |
252b5132 | 945 | if (! bfd_set_section_flags (abfd, newsect, flags)) |
b34976b6 | 946 | return FALSE; |
252b5132 | 947 | |
718175fa JK |
948 | /* We do not parse the PT_NOTE segments as we are interested even in the |
949 | separate debug info files which may have the segments offsets corrupted. | |
950 | PT_NOTEs from the core files are currently not parsed using BFD. */ | |
951 | if (hdr->sh_type == SHT_NOTE) | |
952 | { | |
baea7ef1 | 953 | bfd_byte *contents; |
718175fa | 954 | |
baea7ef1 | 955 | if (!bfd_malloc_and_get_section (abfd, newsect, &contents)) |
718175fa JK |
956 | return FALSE; |
957 | ||
baea7ef1 | 958 | elf_parse_notes (abfd, (char *) contents, hdr->sh_size, -1); |
718175fa JK |
959 | free (contents); |
960 | } | |
961 | ||
252b5132 RH |
962 | if ((flags & SEC_ALLOC) != 0) |
963 | { | |
964 | Elf_Internal_Phdr *phdr; | |
6ffd7900 AM |
965 | unsigned int i, nload; |
966 | ||
967 | /* Some ELF linkers produce binaries with all the program header | |
968 | p_paddr fields zero. If we have such a binary with more than | |
969 | one PT_LOAD header, then leave the section lma equal to vma | |
970 | so that we don't create sections with overlapping lma. */ | |
971 | phdr = elf_tdata (abfd)->phdr; | |
972 | for (nload = 0, i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++) | |
973 | if (phdr->p_paddr != 0) | |
974 | break; | |
975 | else if (phdr->p_type == PT_LOAD && phdr->p_memsz != 0) | |
976 | ++nload; | |
977 | if (i >= elf_elfheader (abfd)->e_phnum && nload > 1) | |
978 | return TRUE; | |
252b5132 | 979 | |
252b5132 RH |
980 | phdr = elf_tdata (abfd)->phdr; |
981 | for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++) | |
982 | { | |
88967714 | 983 | if (phdr->p_type == PT_LOAD |
9a83a553 | 984 | && ELF_SECTION_IN_SEGMENT (hdr, phdr)) |
252b5132 | 985 | { |
88967714 AM |
986 | if ((flags & SEC_LOAD) == 0) |
987 | newsect->lma = (phdr->p_paddr | |
988 | + hdr->sh_addr - phdr->p_vaddr); | |
989 | else | |
990 | /* We used to use the same adjustment for SEC_LOAD | |
991 | sections, but that doesn't work if the segment | |
992 | is packed with code from multiple VMAs. | |
993 | Instead we calculate the section LMA based on | |
994 | the segment LMA. It is assumed that the | |
995 | segment will contain sections with contiguous | |
996 | LMAs, even if the VMAs are not. */ | |
997 | newsect->lma = (phdr->p_paddr | |
998 | + hdr->sh_offset - phdr->p_offset); | |
999 | ||
1000 | /* With contiguous segments, we can't tell from file | |
1001 | offsets whether a section with zero size should | |
1002 | be placed at the end of one segment or the | |
1003 | beginning of the next. Decide based on vaddr. */ | |
1004 | if (hdr->sh_addr >= phdr->p_vaddr | |
1005 | && (hdr->sh_addr + hdr->sh_size | |
1006 | <= phdr->p_vaddr + phdr->p_memsz)) | |
1007 | break; | |
252b5132 RH |
1008 | } |
1009 | } | |
1010 | } | |
1011 | ||
b34976b6 | 1012 | return TRUE; |
252b5132 RH |
1013 | } |
1014 | ||
252b5132 RH |
1015 | const char *const bfd_elf_section_type_names[] = { |
1016 | "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB", | |
1017 | "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE", | |
1018 | "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM", | |
1019 | }; | |
1020 | ||
1049f94e | 1021 | /* ELF relocs are against symbols. If we are producing relocatable |
252b5132 RH |
1022 | output, and the reloc is against an external symbol, and nothing |
1023 | has given us any additional addend, the resulting reloc will also | |
1024 | be against the same symbol. In such a case, we don't want to | |
1025 | change anything about the way the reloc is handled, since it will | |
1026 | all be done at final link time. Rather than put special case code | |
1027 | into bfd_perform_relocation, all the reloc types use this howto | |
1028 | function. It just short circuits the reloc if producing | |
1049f94e | 1029 | relocatable output against an external symbol. */ |
252b5132 | 1030 | |
252b5132 | 1031 | bfd_reloc_status_type |
217aa764 AM |
1032 | bfd_elf_generic_reloc (bfd *abfd ATTRIBUTE_UNUSED, |
1033 | arelent *reloc_entry, | |
1034 | asymbol *symbol, | |
1035 | void *data ATTRIBUTE_UNUSED, | |
1036 | asection *input_section, | |
1037 | bfd *output_bfd, | |
1038 | char **error_message ATTRIBUTE_UNUSED) | |
1039 | { | |
1040 | if (output_bfd != NULL | |
252b5132 RH |
1041 | && (symbol->flags & BSF_SECTION_SYM) == 0 |
1042 | && (! reloc_entry->howto->partial_inplace | |
1043 | || reloc_entry->addend == 0)) | |
1044 | { | |
1045 | reloc_entry->address += input_section->output_offset; | |
1046 | return bfd_reloc_ok; | |
1047 | } | |
1048 | ||
1049 | return bfd_reloc_continue; | |
1050 | } | |
1051 | \f | |
0ac4564e L |
1052 | /* Copy the program header and other data from one object module to |
1053 | another. */ | |
252b5132 | 1054 | |
b34976b6 | 1055 | bfd_boolean |
217aa764 | 1056 | _bfd_elf_copy_private_bfd_data (bfd *ibfd, bfd *obfd) |
2d502050 L |
1057 | { |
1058 | if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour | |
1059 | || bfd_get_flavour (obfd) != bfd_target_elf_flavour) | |
b34976b6 | 1060 | return TRUE; |
2d502050 L |
1061 | |
1062 | BFD_ASSERT (!elf_flags_init (obfd) | |
1063 | || (elf_elfheader (obfd)->e_flags | |
1064 | == elf_elfheader (ibfd)->e_flags)); | |
1065 | ||
0ac4564e | 1066 | elf_gp (obfd) = elf_gp (ibfd); |
2d502050 | 1067 | elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags; |
b34976b6 | 1068 | elf_flags_init (obfd) = TRUE; |
104d59d1 JM |
1069 | |
1070 | /* Copy object attributes. */ | |
1071 | _bfd_elf_copy_obj_attributes (ibfd, obfd); | |
b34976b6 | 1072 | return TRUE; |
2d502050 L |
1073 | } |
1074 | ||
cedc298e L |
1075 | static const char * |
1076 | get_segment_type (unsigned int p_type) | |
1077 | { | |
1078 | const char *pt; | |
1079 | switch (p_type) | |
1080 | { | |
1081 | case PT_NULL: pt = "NULL"; break; | |
1082 | case PT_LOAD: pt = "LOAD"; break; | |
1083 | case PT_DYNAMIC: pt = "DYNAMIC"; break; | |
1084 | case PT_INTERP: pt = "INTERP"; break; | |
1085 | case PT_NOTE: pt = "NOTE"; break; | |
1086 | case PT_SHLIB: pt = "SHLIB"; break; | |
1087 | case PT_PHDR: pt = "PHDR"; break; | |
1088 | case PT_TLS: pt = "TLS"; break; | |
1089 | case PT_GNU_EH_FRAME: pt = "EH_FRAME"; break; | |
2b05f1b7 | 1090 | case PT_GNU_STACK: pt = "STACK"; break; |
cedc298e L |
1091 | case PT_GNU_RELRO: pt = "RELRO"; break; |
1092 | default: pt = NULL; break; | |
1093 | } | |
1094 | return pt; | |
1095 | } | |
1096 | ||
f0b79d91 L |
1097 | /* Print out the program headers. */ |
1098 | ||
b34976b6 | 1099 | bfd_boolean |
217aa764 | 1100 | _bfd_elf_print_private_bfd_data (bfd *abfd, void *farg) |
252b5132 | 1101 | { |
a50b1753 | 1102 | FILE *f = (FILE *) farg; |
252b5132 RH |
1103 | Elf_Internal_Phdr *p; |
1104 | asection *s; | |
1105 | bfd_byte *dynbuf = NULL; | |
1106 | ||
1107 | p = elf_tdata (abfd)->phdr; | |
1108 | if (p != NULL) | |
1109 | { | |
1110 | unsigned int i, c; | |
1111 | ||
1112 | fprintf (f, _("\nProgram Header:\n")); | |
1113 | c = elf_elfheader (abfd)->e_phnum; | |
1114 | for (i = 0; i < c; i++, p++) | |
1115 | { | |
cedc298e | 1116 | const char *pt = get_segment_type (p->p_type); |
252b5132 RH |
1117 | char buf[20]; |
1118 | ||
cedc298e | 1119 | if (pt == NULL) |
252b5132 | 1120 | { |
cedc298e L |
1121 | sprintf (buf, "0x%lx", p->p_type); |
1122 | pt = buf; | |
252b5132 | 1123 | } |
dc810e39 | 1124 | fprintf (f, "%8s off 0x", pt); |
60b89a18 | 1125 | bfd_fprintf_vma (abfd, f, p->p_offset); |
252b5132 | 1126 | fprintf (f, " vaddr 0x"); |
60b89a18 | 1127 | bfd_fprintf_vma (abfd, f, p->p_vaddr); |
252b5132 | 1128 | fprintf (f, " paddr 0x"); |
60b89a18 | 1129 | bfd_fprintf_vma (abfd, f, p->p_paddr); |
252b5132 RH |
1130 | fprintf (f, " align 2**%u\n", bfd_log2 (p->p_align)); |
1131 | fprintf (f, " filesz 0x"); | |
60b89a18 | 1132 | bfd_fprintf_vma (abfd, f, p->p_filesz); |
252b5132 | 1133 | fprintf (f, " memsz 0x"); |
60b89a18 | 1134 | bfd_fprintf_vma (abfd, f, p->p_memsz); |
252b5132 RH |
1135 | fprintf (f, " flags %c%c%c", |
1136 | (p->p_flags & PF_R) != 0 ? 'r' : '-', | |
1137 | (p->p_flags & PF_W) != 0 ? 'w' : '-', | |
1138 | (p->p_flags & PF_X) != 0 ? 'x' : '-'); | |
dc810e39 AM |
1139 | if ((p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X)) != 0) |
1140 | fprintf (f, " %lx", p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X)); | |
252b5132 RH |
1141 | fprintf (f, "\n"); |
1142 | } | |
1143 | } | |
1144 | ||
1145 | s = bfd_get_section_by_name (abfd, ".dynamic"); | |
1146 | if (s != NULL) | |
1147 | { | |
cb33740c | 1148 | unsigned int elfsec; |
dc810e39 | 1149 | unsigned long shlink; |
252b5132 RH |
1150 | bfd_byte *extdyn, *extdynend; |
1151 | size_t extdynsize; | |
217aa764 | 1152 | void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *); |
252b5132 RH |
1153 | |
1154 | fprintf (f, _("\nDynamic Section:\n")); | |
1155 | ||
eea6121a | 1156 | if (!bfd_malloc_and_get_section (abfd, s, &dynbuf)) |
252b5132 RH |
1157 | goto error_return; |
1158 | ||
1159 | elfsec = _bfd_elf_section_from_bfd_section (abfd, s); | |
cb33740c | 1160 | if (elfsec == SHN_BAD) |
252b5132 | 1161 | goto error_return; |
dc810e39 | 1162 | shlink = elf_elfsections (abfd)[elfsec]->sh_link; |
252b5132 RH |
1163 | |
1164 | extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn; | |
1165 | swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in; | |
1166 | ||
1167 | extdyn = dynbuf; | |
eea6121a | 1168 | extdynend = extdyn + s->size; |
252b5132 RH |
1169 | for (; extdyn < extdynend; extdyn += extdynsize) |
1170 | { | |
1171 | Elf_Internal_Dyn dyn; | |
ad9563d6 | 1172 | const char *name = ""; |
252b5132 | 1173 | char ab[20]; |
b34976b6 | 1174 | bfd_boolean stringp; |
ad9563d6 | 1175 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
252b5132 | 1176 | |
217aa764 | 1177 | (*swap_dyn_in) (abfd, extdyn, &dyn); |
252b5132 RH |
1178 | |
1179 | if (dyn.d_tag == DT_NULL) | |
1180 | break; | |
1181 | ||
b34976b6 | 1182 | stringp = FALSE; |
252b5132 RH |
1183 | switch (dyn.d_tag) |
1184 | { | |
1185 | default: | |
ad9563d6 CM |
1186 | if (bed->elf_backend_get_target_dtag) |
1187 | name = (*bed->elf_backend_get_target_dtag) (dyn.d_tag); | |
1188 | ||
1189 | if (!strcmp (name, "")) | |
1190 | { | |
1191 | sprintf (ab, "0x%lx", (unsigned long) dyn.d_tag); | |
1192 | name = ab; | |
1193 | } | |
252b5132 RH |
1194 | break; |
1195 | ||
b34976b6 | 1196 | case DT_NEEDED: name = "NEEDED"; stringp = TRUE; break; |
252b5132 RH |
1197 | case DT_PLTRELSZ: name = "PLTRELSZ"; break; |
1198 | case DT_PLTGOT: name = "PLTGOT"; break; | |
1199 | case DT_HASH: name = "HASH"; break; | |
1200 | case DT_STRTAB: name = "STRTAB"; break; | |
1201 | case DT_SYMTAB: name = "SYMTAB"; break; | |
1202 | case DT_RELA: name = "RELA"; break; | |
1203 | case DT_RELASZ: name = "RELASZ"; break; | |
1204 | case DT_RELAENT: name = "RELAENT"; break; | |
1205 | case DT_STRSZ: name = "STRSZ"; break; | |
1206 | case DT_SYMENT: name = "SYMENT"; break; | |
1207 | case DT_INIT: name = "INIT"; break; | |
1208 | case DT_FINI: name = "FINI"; break; | |
b34976b6 AM |
1209 | case DT_SONAME: name = "SONAME"; stringp = TRUE; break; |
1210 | case DT_RPATH: name = "RPATH"; stringp = TRUE; break; | |
252b5132 RH |
1211 | case DT_SYMBOLIC: name = "SYMBOLIC"; break; |
1212 | case DT_REL: name = "REL"; break; | |
1213 | case DT_RELSZ: name = "RELSZ"; break; | |
1214 | case DT_RELENT: name = "RELENT"; break; | |
1215 | case DT_PLTREL: name = "PLTREL"; break; | |
1216 | case DT_DEBUG: name = "DEBUG"; break; | |
1217 | case DT_TEXTREL: name = "TEXTREL"; break; | |
1218 | case DT_JMPREL: name = "JMPREL"; break; | |
94558834 L |
1219 | case DT_BIND_NOW: name = "BIND_NOW"; break; |
1220 | case DT_INIT_ARRAY: name = "INIT_ARRAY"; break; | |
1221 | case DT_FINI_ARRAY: name = "FINI_ARRAY"; break; | |
1222 | case DT_INIT_ARRAYSZ: name = "INIT_ARRAYSZ"; break; | |
1223 | case DT_FINI_ARRAYSZ: name = "FINI_ARRAYSZ"; break; | |
b34976b6 | 1224 | case DT_RUNPATH: name = "RUNPATH"; stringp = TRUE; break; |
94558834 L |
1225 | case DT_FLAGS: name = "FLAGS"; break; |
1226 | case DT_PREINIT_ARRAY: name = "PREINIT_ARRAY"; break; | |
1227 | case DT_PREINIT_ARRAYSZ: name = "PREINIT_ARRAYSZ"; break; | |
d48188b9 | 1228 | case DT_CHECKSUM: name = "CHECKSUM"; break; |
94558834 L |
1229 | case DT_PLTPADSZ: name = "PLTPADSZ"; break; |
1230 | case DT_MOVEENT: name = "MOVEENT"; break; | |
1231 | case DT_MOVESZ: name = "MOVESZ"; break; | |
1232 | case DT_FEATURE: name = "FEATURE"; break; | |
1233 | case DT_POSFLAG_1: name = "POSFLAG_1"; break; | |
1234 | case DT_SYMINSZ: name = "SYMINSZ"; break; | |
1235 | case DT_SYMINENT: name = "SYMINENT"; break; | |
b34976b6 AM |
1236 | case DT_CONFIG: name = "CONFIG"; stringp = TRUE; break; |
1237 | case DT_DEPAUDIT: name = "DEPAUDIT"; stringp = TRUE; break; | |
1238 | case DT_AUDIT: name = "AUDIT"; stringp = TRUE; break; | |
94558834 L |
1239 | case DT_PLTPAD: name = "PLTPAD"; break; |
1240 | case DT_MOVETAB: name = "MOVETAB"; break; | |
1241 | case DT_SYMINFO: name = "SYMINFO"; break; | |
1242 | case DT_RELACOUNT: name = "RELACOUNT"; break; | |
1243 | case DT_RELCOUNT: name = "RELCOUNT"; break; | |
1244 | case DT_FLAGS_1: name = "FLAGS_1"; break; | |
252b5132 RH |
1245 | case DT_VERSYM: name = "VERSYM"; break; |
1246 | case DT_VERDEF: name = "VERDEF"; break; | |
1247 | case DT_VERDEFNUM: name = "VERDEFNUM"; break; | |
1248 | case DT_VERNEED: name = "VERNEED"; break; | |
1249 | case DT_VERNEEDNUM: name = "VERNEEDNUM"; break; | |
b34976b6 | 1250 | case DT_AUXILIARY: name = "AUXILIARY"; stringp = TRUE; break; |
94558834 | 1251 | case DT_USED: name = "USED"; break; |
b34976b6 | 1252 | case DT_FILTER: name = "FILTER"; stringp = TRUE; break; |
fdc90cb4 | 1253 | case DT_GNU_HASH: name = "GNU_HASH"; break; |
252b5132 RH |
1254 | } |
1255 | ||
ad9563d6 | 1256 | fprintf (f, " %-20s ", name); |
252b5132 | 1257 | if (! stringp) |
a1f3c56e AN |
1258 | { |
1259 | fprintf (f, "0x"); | |
1260 | bfd_fprintf_vma (abfd, f, dyn.d_un.d_val); | |
1261 | } | |
252b5132 RH |
1262 | else |
1263 | { | |
1264 | const char *string; | |
dc810e39 | 1265 | unsigned int tagv = dyn.d_un.d_val; |
252b5132 | 1266 | |
dc810e39 | 1267 | string = bfd_elf_string_from_elf_section (abfd, shlink, tagv); |
252b5132 RH |
1268 | if (string == NULL) |
1269 | goto error_return; | |
1270 | fprintf (f, "%s", string); | |
1271 | } | |
1272 | fprintf (f, "\n"); | |
1273 | } | |
1274 | ||
1275 | free (dynbuf); | |
1276 | dynbuf = NULL; | |
1277 | } | |
1278 | ||
1279 | if ((elf_dynverdef (abfd) != 0 && elf_tdata (abfd)->verdef == NULL) | |
1280 | || (elf_dynverref (abfd) != 0 && elf_tdata (abfd)->verref == NULL)) | |
1281 | { | |
fc0e6df6 | 1282 | if (! _bfd_elf_slurp_version_tables (abfd, FALSE)) |
b34976b6 | 1283 | return FALSE; |
252b5132 RH |
1284 | } |
1285 | ||
1286 | if (elf_dynverdef (abfd) != 0) | |
1287 | { | |
1288 | Elf_Internal_Verdef *t; | |
1289 | ||
1290 | fprintf (f, _("\nVersion definitions:\n")); | |
1291 | for (t = elf_tdata (abfd)->verdef; t != NULL; t = t->vd_nextdef) | |
1292 | { | |
1293 | fprintf (f, "%d 0x%2.2x 0x%8.8lx %s\n", t->vd_ndx, | |
d0fb9a8d JJ |
1294 | t->vd_flags, t->vd_hash, |
1295 | t->vd_nodename ? t->vd_nodename : "<corrupt>"); | |
1296 | if (t->vd_auxptr != NULL && t->vd_auxptr->vda_nextptr != NULL) | |
252b5132 RH |
1297 | { |
1298 | Elf_Internal_Verdaux *a; | |
1299 | ||
1300 | fprintf (f, "\t"); | |
1301 | for (a = t->vd_auxptr->vda_nextptr; | |
1302 | a != NULL; | |
1303 | a = a->vda_nextptr) | |
d0fb9a8d JJ |
1304 | fprintf (f, "%s ", |
1305 | a->vda_nodename ? a->vda_nodename : "<corrupt>"); | |
252b5132 RH |
1306 | fprintf (f, "\n"); |
1307 | } | |
1308 | } | |
1309 | } | |
1310 | ||
1311 | if (elf_dynverref (abfd) != 0) | |
1312 | { | |
1313 | Elf_Internal_Verneed *t; | |
1314 | ||
1315 | fprintf (f, _("\nVersion References:\n")); | |
1316 | for (t = elf_tdata (abfd)->verref; t != NULL; t = t->vn_nextref) | |
1317 | { | |
1318 | Elf_Internal_Vernaux *a; | |
1319 | ||
d0fb9a8d JJ |
1320 | fprintf (f, _(" required from %s:\n"), |
1321 | t->vn_filename ? t->vn_filename : "<corrupt>"); | |
252b5132 RH |
1322 | for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) |
1323 | fprintf (f, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a->vna_hash, | |
d0fb9a8d JJ |
1324 | a->vna_flags, a->vna_other, |
1325 | a->vna_nodename ? a->vna_nodename : "<corrupt>"); | |
252b5132 RH |
1326 | } |
1327 | } | |
1328 | ||
b34976b6 | 1329 | return TRUE; |
252b5132 RH |
1330 | |
1331 | error_return: | |
1332 | if (dynbuf != NULL) | |
1333 | free (dynbuf); | |
b34976b6 | 1334 | return FALSE; |
252b5132 RH |
1335 | } |
1336 | ||
1337 | /* Display ELF-specific fields of a symbol. */ | |
1338 | ||
1339 | void | |
217aa764 AM |
1340 | bfd_elf_print_symbol (bfd *abfd, |
1341 | void *filep, | |
1342 | asymbol *symbol, | |
1343 | bfd_print_symbol_type how) | |
252b5132 | 1344 | { |
a50b1753 | 1345 | FILE *file = (FILE *) filep; |
252b5132 RH |
1346 | switch (how) |
1347 | { | |
1348 | case bfd_print_symbol_name: | |
1349 | fprintf (file, "%s", symbol->name); | |
1350 | break; | |
1351 | case bfd_print_symbol_more: | |
1352 | fprintf (file, "elf "); | |
60b89a18 | 1353 | bfd_fprintf_vma (abfd, file, symbol->value); |
0af1713e | 1354 | fprintf (file, " %lx", (unsigned long) symbol->flags); |
252b5132 RH |
1355 | break; |
1356 | case bfd_print_symbol_all: | |
1357 | { | |
4e8a9624 AM |
1358 | const char *section_name; |
1359 | const char *name = NULL; | |
9c5bfbb7 | 1360 | const struct elf_backend_data *bed; |
7a13edea | 1361 | unsigned char st_other; |
dbb410c3 | 1362 | bfd_vma val; |
c044fabd | 1363 | |
252b5132 | 1364 | section_name = symbol->section ? symbol->section->name : "(*none*)"; |
587ff49e RH |
1365 | |
1366 | bed = get_elf_backend_data (abfd); | |
1367 | if (bed->elf_backend_print_symbol_all) | |
c044fabd | 1368 | name = (*bed->elf_backend_print_symbol_all) (abfd, filep, symbol); |
587ff49e RH |
1369 | |
1370 | if (name == NULL) | |
1371 | { | |
7ee38065 | 1372 | name = symbol->name; |
217aa764 | 1373 | bfd_print_symbol_vandf (abfd, file, symbol); |
587ff49e RH |
1374 | } |
1375 | ||
252b5132 RH |
1376 | fprintf (file, " %s\t", section_name); |
1377 | /* Print the "other" value for a symbol. For common symbols, | |
1378 | we've already printed the size; now print the alignment. | |
1379 | For other symbols, we have no specified alignment, and | |
1380 | we've printed the address; now print the size. */ | |
dcf6c779 | 1381 | if (symbol->section && bfd_is_com_section (symbol->section)) |
dbb410c3 AM |
1382 | val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_value; |
1383 | else | |
1384 | val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_size; | |
1385 | bfd_fprintf_vma (abfd, file, val); | |
252b5132 RH |
1386 | |
1387 | /* If we have version information, print it. */ | |
1388 | if (elf_tdata (abfd)->dynversym_section != 0 | |
1389 | && (elf_tdata (abfd)->dynverdef_section != 0 | |
1390 | || elf_tdata (abfd)->dynverref_section != 0)) | |
1391 | { | |
1392 | unsigned int vernum; | |
1393 | const char *version_string; | |
1394 | ||
1395 | vernum = ((elf_symbol_type *) symbol)->version & VERSYM_VERSION; | |
1396 | ||
1397 | if (vernum == 0) | |
1398 | version_string = ""; | |
1399 | else if (vernum == 1) | |
1400 | version_string = "Base"; | |
1401 | else if (vernum <= elf_tdata (abfd)->cverdefs) | |
1402 | version_string = | |
1403 | elf_tdata (abfd)->verdef[vernum - 1].vd_nodename; | |
1404 | else | |
1405 | { | |
1406 | Elf_Internal_Verneed *t; | |
1407 | ||
1408 | version_string = ""; | |
1409 | for (t = elf_tdata (abfd)->verref; | |
1410 | t != NULL; | |
1411 | t = t->vn_nextref) | |
1412 | { | |
1413 | Elf_Internal_Vernaux *a; | |
1414 | ||
1415 | for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) | |
1416 | { | |
1417 | if (a->vna_other == vernum) | |
1418 | { | |
1419 | version_string = a->vna_nodename; | |
1420 | break; | |
1421 | } | |
1422 | } | |
1423 | } | |
1424 | } | |
1425 | ||
1426 | if ((((elf_symbol_type *) symbol)->version & VERSYM_HIDDEN) == 0) | |
1427 | fprintf (file, " %-11s", version_string); | |
1428 | else | |
1429 | { | |
1430 | int i; | |
1431 | ||
1432 | fprintf (file, " (%s)", version_string); | |
1433 | for (i = 10 - strlen (version_string); i > 0; --i) | |
1434 | putc (' ', file); | |
1435 | } | |
1436 | } | |
1437 | ||
1438 | /* If the st_other field is not zero, print it. */ | |
7a13edea | 1439 | st_other = ((elf_symbol_type *) symbol)->internal_elf_sym.st_other; |
c044fabd | 1440 | |
7a13edea NC |
1441 | switch (st_other) |
1442 | { | |
1443 | case 0: break; | |
1444 | case STV_INTERNAL: fprintf (file, " .internal"); break; | |
1445 | case STV_HIDDEN: fprintf (file, " .hidden"); break; | |
1446 | case STV_PROTECTED: fprintf (file, " .protected"); break; | |
1447 | default: | |
1448 | /* Some other non-defined flags are also present, so print | |
1449 | everything hex. */ | |
1450 | fprintf (file, " 0x%02x", (unsigned int) st_other); | |
1451 | } | |
252b5132 | 1452 | |
587ff49e | 1453 | fprintf (file, " %s", name); |
252b5132 RH |
1454 | } |
1455 | break; | |
1456 | } | |
1457 | } | |
252b5132 | 1458 | |
252b5132 RH |
1459 | /* Allocate an ELF string table--force the first byte to be zero. */ |
1460 | ||
1461 | struct bfd_strtab_hash * | |
217aa764 | 1462 | _bfd_elf_stringtab_init (void) |
252b5132 RH |
1463 | { |
1464 | struct bfd_strtab_hash *ret; | |
1465 | ||
1466 | ret = _bfd_stringtab_init (); | |
1467 | if (ret != NULL) | |
1468 | { | |
1469 | bfd_size_type loc; | |
1470 | ||
b34976b6 | 1471 | loc = _bfd_stringtab_add (ret, "", TRUE, FALSE); |
252b5132 RH |
1472 | BFD_ASSERT (loc == 0 || loc == (bfd_size_type) -1); |
1473 | if (loc == (bfd_size_type) -1) | |
1474 | { | |
1475 | _bfd_stringtab_free (ret); | |
1476 | ret = NULL; | |
1477 | } | |
1478 | } | |
1479 | return ret; | |
1480 | } | |
1481 | \f | |
1482 | /* ELF .o/exec file reading */ | |
1483 | ||
c044fabd | 1484 | /* Create a new bfd section from an ELF section header. */ |
252b5132 | 1485 | |
b34976b6 | 1486 | bfd_boolean |
217aa764 | 1487 | bfd_section_from_shdr (bfd *abfd, unsigned int shindex) |
252b5132 | 1488 | { |
4fbb74a6 AM |
1489 | Elf_Internal_Shdr *hdr; |
1490 | Elf_Internal_Ehdr *ehdr; | |
1491 | const struct elf_backend_data *bed; | |
90937f86 | 1492 | const char *name; |
252b5132 | 1493 | |
4fbb74a6 AM |
1494 | if (shindex >= elf_numsections (abfd)) |
1495 | return FALSE; | |
1496 | ||
1497 | hdr = elf_elfsections (abfd)[shindex]; | |
1498 | ehdr = elf_elfheader (abfd); | |
1499 | name = bfd_elf_string_from_elf_section (abfd, ehdr->e_shstrndx, | |
1b3a8575 | 1500 | hdr->sh_name); |
933d961a JJ |
1501 | if (name == NULL) |
1502 | return FALSE; | |
252b5132 | 1503 | |
4fbb74a6 | 1504 | bed = get_elf_backend_data (abfd); |
252b5132 RH |
1505 | switch (hdr->sh_type) |
1506 | { | |
1507 | case SHT_NULL: | |
1508 | /* Inactive section. Throw it away. */ | |
b34976b6 | 1509 | return TRUE; |
252b5132 RH |
1510 | |
1511 | case SHT_PROGBITS: /* Normal section with contents. */ | |
252b5132 RH |
1512 | case SHT_NOBITS: /* .bss section. */ |
1513 | case SHT_HASH: /* .hash section. */ | |
1514 | case SHT_NOTE: /* .note section. */ | |
25e27870 L |
1515 | case SHT_INIT_ARRAY: /* .init_array section. */ |
1516 | case SHT_FINI_ARRAY: /* .fini_array section. */ | |
1517 | case SHT_PREINIT_ARRAY: /* .preinit_array section. */ | |
7f1204bb | 1518 | case SHT_GNU_LIBLIST: /* .gnu.liblist section. */ |
fdc90cb4 | 1519 | case SHT_GNU_HASH: /* .gnu.hash section. */ |
6dc132d9 | 1520 | return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); |
252b5132 | 1521 | |
797fc050 | 1522 | case SHT_DYNAMIC: /* Dynamic linking information. */ |
6dc132d9 | 1523 | if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex)) |
b34976b6 | 1524 | return FALSE; |
cfcac11d NC |
1525 | if (hdr->sh_link > elf_numsections (abfd)) |
1526 | { | |
caa83f8b | 1527 | /* PR 10478: Accept Solaris binaries with a sh_link |
cfcac11d NC |
1528 | field set to SHN_BEFORE or SHN_AFTER. */ |
1529 | switch (bfd_get_arch (abfd)) | |
1530 | { | |
caa83f8b | 1531 | case bfd_arch_i386: |
cfcac11d NC |
1532 | case bfd_arch_sparc: |
1533 | if (hdr->sh_link == (SHN_LORESERVE & 0xffff) /* SHN_BEFORE */ | |
1534 | || hdr->sh_link == ((SHN_LORESERVE + 1) & 0xffff) /* SHN_AFTER */) | |
1535 | break; | |
1536 | /* Otherwise fall through. */ | |
1537 | default: | |
1538 | return FALSE; | |
1539 | } | |
1540 | } | |
1541 | else if (elf_elfsections (abfd)[hdr->sh_link] == NULL) | |
8e0ed13f | 1542 | return FALSE; |
cfcac11d | 1543 | else if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_STRTAB) |
797fc050 AM |
1544 | { |
1545 | Elf_Internal_Shdr *dynsymhdr; | |
1546 | ||
1547 | /* The shared libraries distributed with hpux11 have a bogus | |
1548 | sh_link field for the ".dynamic" section. Find the | |
1549 | string table for the ".dynsym" section instead. */ | |
1550 | if (elf_dynsymtab (abfd) != 0) | |
1551 | { | |
1552 | dynsymhdr = elf_elfsections (abfd)[elf_dynsymtab (abfd)]; | |
1553 | hdr->sh_link = dynsymhdr->sh_link; | |
1554 | } | |
1555 | else | |
1556 | { | |
1557 | unsigned int i, num_sec; | |
1558 | ||
1559 | num_sec = elf_numsections (abfd); | |
1560 | for (i = 1; i < num_sec; i++) | |
1561 | { | |
1562 | dynsymhdr = elf_elfsections (abfd)[i]; | |
1563 | if (dynsymhdr->sh_type == SHT_DYNSYM) | |
1564 | { | |
1565 | hdr->sh_link = dynsymhdr->sh_link; | |
1566 | break; | |
1567 | } | |
1568 | } | |
1569 | } | |
1570 | } | |
1571 | break; | |
1572 | ||
252b5132 RH |
1573 | case SHT_SYMTAB: /* A symbol table */ |
1574 | if (elf_onesymtab (abfd) == shindex) | |
b34976b6 | 1575 | return TRUE; |
252b5132 | 1576 | |
a50b2160 JJ |
1577 | if (hdr->sh_entsize != bed->s->sizeof_sym) |
1578 | return FALSE; | |
3337c1e5 AM |
1579 | if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size) |
1580 | return FALSE; | |
252b5132 RH |
1581 | BFD_ASSERT (elf_onesymtab (abfd) == 0); |
1582 | elf_onesymtab (abfd) = shindex; | |
1583 | elf_tdata (abfd)->symtab_hdr = *hdr; | |
1584 | elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->symtab_hdr; | |
1585 | abfd->flags |= HAS_SYMS; | |
1586 | ||
1587 | /* Sometimes a shared object will map in the symbol table. If | |
08a40648 AM |
1588 | SHF_ALLOC is set, and this is a shared object, then we also |
1589 | treat this section as a BFD section. We can not base the | |
1590 | decision purely on SHF_ALLOC, because that flag is sometimes | |
1591 | set in a relocatable object file, which would confuse the | |
1592 | linker. */ | |
252b5132 RH |
1593 | if ((hdr->sh_flags & SHF_ALLOC) != 0 |
1594 | && (abfd->flags & DYNAMIC) != 0 | |
6dc132d9 L |
1595 | && ! _bfd_elf_make_section_from_shdr (abfd, hdr, name, |
1596 | shindex)) | |
b34976b6 | 1597 | return FALSE; |
252b5132 | 1598 | |
1b3a8575 AM |
1599 | /* Go looking for SHT_SYMTAB_SHNDX too, since if there is one we |
1600 | can't read symbols without that section loaded as well. It | |
1601 | is most likely specified by the next section header. */ | |
1602 | if (elf_elfsections (abfd)[elf_symtab_shndx (abfd)]->sh_link != shindex) | |
1603 | { | |
1604 | unsigned int i, num_sec; | |
1605 | ||
1606 | num_sec = elf_numsections (abfd); | |
1607 | for (i = shindex + 1; i < num_sec; i++) | |
1608 | { | |
1609 | Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i]; | |
1610 | if (hdr2->sh_type == SHT_SYMTAB_SHNDX | |
1611 | && hdr2->sh_link == shindex) | |
1612 | break; | |
1613 | } | |
1614 | if (i == num_sec) | |
1615 | for (i = 1; i < shindex; i++) | |
1616 | { | |
1617 | Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i]; | |
1618 | if (hdr2->sh_type == SHT_SYMTAB_SHNDX | |
1619 | && hdr2->sh_link == shindex) | |
1620 | break; | |
1621 | } | |
1622 | if (i != shindex) | |
1623 | return bfd_section_from_shdr (abfd, i); | |
1624 | } | |
b34976b6 | 1625 | return TRUE; |
252b5132 RH |
1626 | |
1627 | case SHT_DYNSYM: /* A dynamic symbol table */ | |
1628 | if (elf_dynsymtab (abfd) == shindex) | |
b34976b6 | 1629 | return TRUE; |
252b5132 | 1630 | |
a50b2160 JJ |
1631 | if (hdr->sh_entsize != bed->s->sizeof_sym) |
1632 | return FALSE; | |
252b5132 RH |
1633 | BFD_ASSERT (elf_dynsymtab (abfd) == 0); |
1634 | elf_dynsymtab (abfd) = shindex; | |
1635 | elf_tdata (abfd)->dynsymtab_hdr = *hdr; | |
1636 | elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->dynsymtab_hdr; | |
1637 | abfd->flags |= HAS_SYMS; | |
1638 | ||
1639 | /* Besides being a symbol table, we also treat this as a regular | |
1640 | section, so that objcopy can handle it. */ | |
6dc132d9 | 1641 | return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); |
252b5132 | 1642 | |
9ad5cbcf AM |
1643 | case SHT_SYMTAB_SHNDX: /* Symbol section indices when >64k sections */ |
1644 | if (elf_symtab_shndx (abfd) == shindex) | |
b34976b6 | 1645 | return TRUE; |
9ad5cbcf | 1646 | |
1b3a8575 | 1647 | BFD_ASSERT (elf_symtab_shndx (abfd) == 0); |
9ad5cbcf AM |
1648 | elf_symtab_shndx (abfd) = shindex; |
1649 | elf_tdata (abfd)->symtab_shndx_hdr = *hdr; | |
1650 | elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->symtab_shndx_hdr; | |
b34976b6 | 1651 | return TRUE; |
9ad5cbcf | 1652 | |
252b5132 RH |
1653 | case SHT_STRTAB: /* A string table */ |
1654 | if (hdr->bfd_section != NULL) | |
b34976b6 | 1655 | return TRUE; |
252b5132 RH |
1656 | if (ehdr->e_shstrndx == shindex) |
1657 | { | |
1658 | elf_tdata (abfd)->shstrtab_hdr = *hdr; | |
1659 | elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->shstrtab_hdr; | |
b34976b6 | 1660 | return TRUE; |
252b5132 | 1661 | } |
1b3a8575 AM |
1662 | if (elf_elfsections (abfd)[elf_onesymtab (abfd)]->sh_link == shindex) |
1663 | { | |
1664 | symtab_strtab: | |
1665 | elf_tdata (abfd)->strtab_hdr = *hdr; | |
1666 | elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->strtab_hdr; | |
1667 | return TRUE; | |
1668 | } | |
1669 | if (elf_elfsections (abfd)[elf_dynsymtab (abfd)]->sh_link == shindex) | |
1670 | { | |
1671 | dynsymtab_strtab: | |
1672 | elf_tdata (abfd)->dynstrtab_hdr = *hdr; | |
1673 | hdr = &elf_tdata (abfd)->dynstrtab_hdr; | |
1674 | elf_elfsections (abfd)[shindex] = hdr; | |
1675 | /* We also treat this as a regular section, so that objcopy | |
1676 | can handle it. */ | |
6dc132d9 L |
1677 | return _bfd_elf_make_section_from_shdr (abfd, hdr, name, |
1678 | shindex); | |
1b3a8575 | 1679 | } |
252b5132 | 1680 | |
1b3a8575 AM |
1681 | /* If the string table isn't one of the above, then treat it as a |
1682 | regular section. We need to scan all the headers to be sure, | |
1683 | just in case this strtab section appeared before the above. */ | |
1684 | if (elf_onesymtab (abfd) == 0 || elf_dynsymtab (abfd) == 0) | |
1685 | { | |
1686 | unsigned int i, num_sec; | |
252b5132 | 1687 | |
1b3a8575 AM |
1688 | num_sec = elf_numsections (abfd); |
1689 | for (i = 1; i < num_sec; i++) | |
1690 | { | |
1691 | Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i]; | |
1692 | if (hdr2->sh_link == shindex) | |
1693 | { | |
933d961a JJ |
1694 | /* Prevent endless recursion on broken objects. */ |
1695 | if (i == shindex) | |
1696 | return FALSE; | |
1b3a8575 AM |
1697 | if (! bfd_section_from_shdr (abfd, i)) |
1698 | return FALSE; | |
1699 | if (elf_onesymtab (abfd) == i) | |
1700 | goto symtab_strtab; | |
1701 | if (elf_dynsymtab (abfd) == i) | |
1702 | goto dynsymtab_strtab; | |
1703 | } | |
1704 | } | |
1705 | } | |
6dc132d9 | 1706 | return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); |
252b5132 RH |
1707 | |
1708 | case SHT_REL: | |
1709 | case SHT_RELA: | |
1710 | /* *These* do a lot of work -- but build no sections! */ | |
1711 | { | |
1712 | asection *target_sect; | |
d4730f92 | 1713 | Elf_Internal_Shdr *hdr2, **p_hdr; |
9ad5cbcf | 1714 | unsigned int num_sec = elf_numsections (abfd); |
d4730f92 BS |
1715 | struct bfd_elf_section_data *esdt; |
1716 | bfd_size_type amt; | |
252b5132 | 1717 | |
aa2ca951 JJ |
1718 | if (hdr->sh_entsize |
1719 | != (bfd_size_type) (hdr->sh_type == SHT_REL | |
a50b2160 JJ |
1720 | ? bed->s->sizeof_rel : bed->s->sizeof_rela)) |
1721 | return FALSE; | |
1722 | ||
03ae5f59 | 1723 | /* Check for a bogus link to avoid crashing. */ |
4fbb74a6 | 1724 | if (hdr->sh_link >= num_sec) |
03ae5f59 ILT |
1725 | { |
1726 | ((*_bfd_error_handler) | |
d003868e AM |
1727 | (_("%B: invalid link %lu for reloc section %s (index %u)"), |
1728 | abfd, hdr->sh_link, name, shindex)); | |
6dc132d9 L |
1729 | return _bfd_elf_make_section_from_shdr (abfd, hdr, name, |
1730 | shindex); | |
03ae5f59 ILT |
1731 | } |
1732 | ||
252b5132 RH |
1733 | /* For some incomprehensible reason Oracle distributes |
1734 | libraries for Solaris in which some of the objects have | |
1735 | bogus sh_link fields. It would be nice if we could just | |
1736 | reject them, but, unfortunately, some people need to use | |
1737 | them. We scan through the section headers; if we find only | |
1738 | one suitable symbol table, we clobber the sh_link to point | |
83b89087 L |
1739 | to it. I hope this doesn't break anything. |
1740 | ||
1741 | Don't do it on executable nor shared library. */ | |
1742 | if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0 | |
1743 | && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_SYMTAB | |
252b5132 RH |
1744 | && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_DYNSYM) |
1745 | { | |
9ad5cbcf | 1746 | unsigned int scan; |
252b5132 RH |
1747 | int found; |
1748 | ||
1749 | found = 0; | |
9ad5cbcf | 1750 | for (scan = 1; scan < num_sec; scan++) |
252b5132 RH |
1751 | { |
1752 | if (elf_elfsections (abfd)[scan]->sh_type == SHT_SYMTAB | |
1753 | || elf_elfsections (abfd)[scan]->sh_type == SHT_DYNSYM) | |
1754 | { | |
1755 | if (found != 0) | |
1756 | { | |
1757 | found = 0; | |
1758 | break; | |
1759 | } | |
1760 | found = scan; | |
1761 | } | |
1762 | } | |
1763 | if (found != 0) | |
1764 | hdr->sh_link = found; | |
1765 | } | |
1766 | ||
1767 | /* Get the symbol table. */ | |
1b3a8575 AM |
1768 | if ((elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_SYMTAB |
1769 | || elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_DYNSYM) | |
252b5132 | 1770 | && ! bfd_section_from_shdr (abfd, hdr->sh_link)) |
b34976b6 | 1771 | return FALSE; |
252b5132 RH |
1772 | |
1773 | /* If this reloc section does not use the main symbol table we | |
1774 | don't treat it as a reloc section. BFD can't adequately | |
1775 | represent such a section, so at least for now, we don't | |
c044fabd | 1776 | try. We just present it as a normal section. We also |
60bcf0fa | 1777 | can't use it as a reloc section if it points to the null |
83b89087 L |
1778 | section, an invalid section, another reloc section, or its |
1779 | sh_link points to the null section. */ | |
185ef66d | 1780 | if (hdr->sh_link != elf_onesymtab (abfd) |
83b89087 | 1781 | || hdr->sh_link == SHN_UNDEF |
185ef66d | 1782 | || hdr->sh_info == SHN_UNDEF |
185ef66d AM |
1783 | || hdr->sh_info >= num_sec |
1784 | || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_REL | |
1785 | || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_RELA) | |
6dc132d9 L |
1786 | return _bfd_elf_make_section_from_shdr (abfd, hdr, name, |
1787 | shindex); | |
252b5132 RH |
1788 | |
1789 | if (! bfd_section_from_shdr (abfd, hdr->sh_info)) | |
b34976b6 | 1790 | return FALSE; |
252b5132 RH |
1791 | target_sect = bfd_section_from_elf_index (abfd, hdr->sh_info); |
1792 | if (target_sect == NULL) | |
b34976b6 | 1793 | return FALSE; |
252b5132 | 1794 | |
d4730f92 BS |
1795 | esdt = elf_section_data (target_sect); |
1796 | if (hdr->sh_type == SHT_RELA) | |
1797 | p_hdr = &esdt->rela.hdr; | |
252b5132 | 1798 | else |
d4730f92 BS |
1799 | p_hdr = &esdt->rel.hdr; |
1800 | ||
1801 | BFD_ASSERT (*p_hdr == NULL); | |
1802 | amt = sizeof (*hdr2); | |
1803 | hdr2 = (Elf_Internal_Shdr *) bfd_alloc (abfd, amt); | |
1804 | if (hdr2 == NULL) | |
1805 | return FALSE; | |
252b5132 | 1806 | *hdr2 = *hdr; |
d4730f92 | 1807 | *p_hdr = hdr2; |
252b5132 | 1808 | elf_elfsections (abfd)[shindex] = hdr2; |
d9bc7a44 | 1809 | target_sect->reloc_count += NUM_SHDR_ENTRIES (hdr); |
252b5132 RH |
1810 | target_sect->flags |= SEC_RELOC; |
1811 | target_sect->relocation = NULL; | |
1812 | target_sect->rel_filepos = hdr->sh_offset; | |
bf572ba0 MM |
1813 | /* In the section to which the relocations apply, mark whether |
1814 | its relocations are of the REL or RELA variety. */ | |
72730e0c | 1815 | if (hdr->sh_size != 0) |
d4730f92 BS |
1816 | { |
1817 | if (hdr->sh_type == SHT_RELA) | |
1818 | target_sect->use_rela_p = 1; | |
1819 | } | |
252b5132 | 1820 | abfd->flags |= HAS_RELOC; |
b34976b6 | 1821 | return TRUE; |
252b5132 | 1822 | } |
252b5132 RH |
1823 | |
1824 | case SHT_GNU_verdef: | |
1825 | elf_dynverdef (abfd) = shindex; | |
1826 | elf_tdata (abfd)->dynverdef_hdr = *hdr; | |
6dc132d9 | 1827 | return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); |
252b5132 RH |
1828 | |
1829 | case SHT_GNU_versym: | |
a50b2160 JJ |
1830 | if (hdr->sh_entsize != sizeof (Elf_External_Versym)) |
1831 | return FALSE; | |
252b5132 RH |
1832 | elf_dynversym (abfd) = shindex; |
1833 | elf_tdata (abfd)->dynversym_hdr = *hdr; | |
6dc132d9 | 1834 | return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); |
252b5132 RH |
1835 | |
1836 | case SHT_GNU_verneed: | |
1837 | elf_dynverref (abfd) = shindex; | |
1838 | elf_tdata (abfd)->dynverref_hdr = *hdr; | |
6dc132d9 | 1839 | return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); |
252b5132 RH |
1840 | |
1841 | case SHT_SHLIB: | |
b34976b6 | 1842 | return TRUE; |
252b5132 | 1843 | |
dbb410c3 | 1844 | case SHT_GROUP: |
1783205a | 1845 | if (! IS_VALID_GROUP_SECTION_HEADER (hdr)) |
a50b2160 | 1846 | return FALSE; |
6dc132d9 | 1847 | if (!_bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex)) |
b34976b6 | 1848 | return FALSE; |
dbb410c3 AM |
1849 | if (hdr->contents != NULL) |
1850 | { | |
1851 | Elf_Internal_Group *idx = (Elf_Internal_Group *) hdr->contents; | |
1783205a | 1852 | unsigned int n_elt = hdr->sh_size / GRP_ENTRY_SIZE; |
dbb410c3 AM |
1853 | asection *s; |
1854 | ||
b885599b AM |
1855 | if (idx->flags & GRP_COMDAT) |
1856 | hdr->bfd_section->flags | |
1857 | |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD; | |
1858 | ||
45c5e9ed L |
1859 | /* We try to keep the same section order as it comes in. */ |
1860 | idx += n_elt; | |
dbb410c3 | 1861 | while (--n_elt != 0) |
1783205a NC |
1862 | { |
1863 | --idx; | |
1864 | ||
1865 | if (idx->shdr != NULL | |
1866 | && (s = idx->shdr->bfd_section) != NULL | |
1867 | && elf_next_in_group (s) != NULL) | |
1868 | { | |
1869 | elf_next_in_group (hdr->bfd_section) = s; | |
1870 | break; | |
1871 | } | |
1872 | } | |
dbb410c3 AM |
1873 | } |
1874 | break; | |
1875 | ||
252b5132 | 1876 | default: |
104d59d1 JM |
1877 | /* Possibly an attributes section. */ |
1878 | if (hdr->sh_type == SHT_GNU_ATTRIBUTES | |
1879 | || hdr->sh_type == bed->obj_attrs_section_type) | |
1880 | { | |
1881 | if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex)) | |
1882 | return FALSE; | |
1883 | _bfd_elf_parse_attributes (abfd, hdr); | |
1884 | return TRUE; | |
1885 | } | |
1886 | ||
252b5132 | 1887 | /* Check for any processor-specific section types. */ |
3eb70a79 L |
1888 | if (bed->elf_backend_section_from_shdr (abfd, hdr, name, shindex)) |
1889 | return TRUE; | |
1890 | ||
1891 | if (hdr->sh_type >= SHT_LOUSER && hdr->sh_type <= SHT_HIUSER) | |
1892 | { | |
1893 | if ((hdr->sh_flags & SHF_ALLOC) != 0) | |
1894 | /* FIXME: How to properly handle allocated section reserved | |
1895 | for applications? */ | |
1896 | (*_bfd_error_handler) | |
1897 | (_("%B: don't know how to handle allocated, application " | |
1898 | "specific section `%s' [0x%8x]"), | |
1899 | abfd, name, hdr->sh_type); | |
1900 | else | |
1901 | /* Allow sections reserved for applications. */ | |
1902 | return _bfd_elf_make_section_from_shdr (abfd, hdr, name, | |
1903 | shindex); | |
1904 | } | |
1905 | else if (hdr->sh_type >= SHT_LOPROC | |
1906 | && hdr->sh_type <= SHT_HIPROC) | |
1907 | /* FIXME: We should handle this section. */ | |
1908 | (*_bfd_error_handler) | |
1909 | (_("%B: don't know how to handle processor specific section " | |
1910 | "`%s' [0x%8x]"), | |
1911 | abfd, name, hdr->sh_type); | |
1912 | else if (hdr->sh_type >= SHT_LOOS && hdr->sh_type <= SHT_HIOS) | |
ff15b240 NC |
1913 | { |
1914 | /* Unrecognised OS-specific sections. */ | |
1915 | if ((hdr->sh_flags & SHF_OS_NONCONFORMING) != 0) | |
1916 | /* SHF_OS_NONCONFORMING indicates that special knowledge is | |
08a40648 | 1917 | required to correctly process the section and the file should |
ff15b240 NC |
1918 | be rejected with an error message. */ |
1919 | (*_bfd_error_handler) | |
1920 | (_("%B: don't know how to handle OS specific section " | |
1921 | "`%s' [0x%8x]"), | |
1922 | abfd, name, hdr->sh_type); | |
1923 | else | |
1924 | /* Otherwise it should be processed. */ | |
1925 | return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); | |
1926 | } | |
3eb70a79 L |
1927 | else |
1928 | /* FIXME: We should handle this section. */ | |
1929 | (*_bfd_error_handler) | |
1930 | (_("%B: don't know how to handle section `%s' [0x%8x]"), | |
1931 | abfd, name, hdr->sh_type); | |
1932 | ||
1933 | return FALSE; | |
252b5132 RH |
1934 | } |
1935 | ||
b34976b6 | 1936 | return TRUE; |
252b5132 RH |
1937 | } |
1938 | ||
87d72d41 | 1939 | /* Return the local symbol specified by ABFD, R_SYMNDX. */ |
ec338859 | 1940 | |
87d72d41 AM |
1941 | Elf_Internal_Sym * |
1942 | bfd_sym_from_r_symndx (struct sym_cache *cache, | |
1943 | bfd *abfd, | |
1944 | unsigned long r_symndx) | |
ec338859 | 1945 | { |
ec338859 AM |
1946 | unsigned int ent = r_symndx % LOCAL_SYM_CACHE_SIZE; |
1947 | ||
a5d1b3b5 AM |
1948 | if (cache->abfd != abfd || cache->indx[ent] != r_symndx) |
1949 | { | |
1950 | Elf_Internal_Shdr *symtab_hdr; | |
1951 | unsigned char esym[sizeof (Elf64_External_Sym)]; | |
1952 | Elf_External_Sym_Shndx eshndx; | |
ec338859 | 1953 | |
a5d1b3b5 AM |
1954 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; |
1955 | if (bfd_elf_get_elf_syms (abfd, symtab_hdr, 1, r_symndx, | |
87d72d41 | 1956 | &cache->sym[ent], esym, &eshndx) == NULL) |
a5d1b3b5 | 1957 | return NULL; |
9ad5cbcf | 1958 | |
a5d1b3b5 AM |
1959 | if (cache->abfd != abfd) |
1960 | { | |
1961 | memset (cache->indx, -1, sizeof (cache->indx)); | |
1962 | cache->abfd = abfd; | |
1963 | } | |
1964 | cache->indx[ent] = r_symndx; | |
ec338859 | 1965 | } |
a5d1b3b5 | 1966 | |
87d72d41 | 1967 | return &cache->sym[ent]; |
ec338859 AM |
1968 | } |
1969 | ||
252b5132 RH |
1970 | /* Given an ELF section number, retrieve the corresponding BFD |
1971 | section. */ | |
1972 | ||
1973 | asection * | |
91d6fa6a | 1974 | bfd_section_from_elf_index (bfd *abfd, unsigned int sec_index) |
252b5132 | 1975 | { |
91d6fa6a | 1976 | if (sec_index >= elf_numsections (abfd)) |
252b5132 | 1977 | return NULL; |
91d6fa6a | 1978 | return elf_elfsections (abfd)[sec_index]->bfd_section; |
252b5132 RH |
1979 | } |
1980 | ||
b35d266b | 1981 | static const struct bfd_elf_special_section special_sections_b[] = |
2f89ff8d | 1982 | { |
0112cd26 NC |
1983 | { STRING_COMMA_LEN (".bss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE }, |
1984 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
1985 | }; |
1986 | ||
b35d266b | 1987 | static const struct bfd_elf_special_section special_sections_c[] = |
7f4d3958 | 1988 | { |
0112cd26 NC |
1989 | { STRING_COMMA_LEN (".comment"), 0, SHT_PROGBITS, 0 }, |
1990 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
1991 | }; |
1992 | ||
b35d266b | 1993 | static const struct bfd_elf_special_section special_sections_d[] = |
7f4d3958 | 1994 | { |
0112cd26 NC |
1995 | { STRING_COMMA_LEN (".data"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE }, |
1996 | { STRING_COMMA_LEN (".data1"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE }, | |
1997 | { STRING_COMMA_LEN (".debug"), 0, SHT_PROGBITS, 0 }, | |
1998 | { STRING_COMMA_LEN (".debug_line"), 0, SHT_PROGBITS, 0 }, | |
1999 | { STRING_COMMA_LEN (".debug_info"), 0, SHT_PROGBITS, 0 }, | |
2000 | { STRING_COMMA_LEN (".debug_abbrev"), 0, SHT_PROGBITS, 0 }, | |
2001 | { STRING_COMMA_LEN (".debug_aranges"), 0, SHT_PROGBITS, 0 }, | |
2002 | { STRING_COMMA_LEN (".dynamic"), 0, SHT_DYNAMIC, SHF_ALLOC }, | |
2003 | { STRING_COMMA_LEN (".dynstr"), 0, SHT_STRTAB, SHF_ALLOC }, | |
2004 | { STRING_COMMA_LEN (".dynsym"), 0, SHT_DYNSYM, SHF_ALLOC }, | |
2005 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2006 | }; |
2007 | ||
b35d266b | 2008 | static const struct bfd_elf_special_section special_sections_f[] = |
7f4d3958 | 2009 | { |
0112cd26 NC |
2010 | { STRING_COMMA_LEN (".fini"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR }, |
2011 | { STRING_COMMA_LEN (".fini_array"), 0, SHT_FINI_ARRAY, SHF_ALLOC + SHF_WRITE }, | |
2012 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2013 | }; |
2014 | ||
b35d266b | 2015 | static const struct bfd_elf_special_section special_sections_g[] = |
7f4d3958 | 2016 | { |
0112cd26 NC |
2017 | { STRING_COMMA_LEN (".gnu.linkonce.b"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE }, |
2018 | { STRING_COMMA_LEN (".got"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE }, | |
2019 | { STRING_COMMA_LEN (".gnu.version"), 0, SHT_GNU_versym, 0 }, | |
2020 | { STRING_COMMA_LEN (".gnu.version_d"), 0, SHT_GNU_verdef, 0 }, | |
2021 | { STRING_COMMA_LEN (".gnu.version_r"), 0, SHT_GNU_verneed, 0 }, | |
2022 | { STRING_COMMA_LEN (".gnu.liblist"), 0, SHT_GNU_LIBLIST, SHF_ALLOC }, | |
2023 | { STRING_COMMA_LEN (".gnu.conflict"), 0, SHT_RELA, SHF_ALLOC }, | |
2024 | { STRING_COMMA_LEN (".gnu.hash"), 0, SHT_GNU_HASH, SHF_ALLOC }, | |
2025 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2026 | }; |
2027 | ||
b35d266b | 2028 | static const struct bfd_elf_special_section special_sections_h[] = |
7f4d3958 | 2029 | { |
0112cd26 NC |
2030 | { STRING_COMMA_LEN (".hash"), 0, SHT_HASH, SHF_ALLOC }, |
2031 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2032 | }; |
2033 | ||
b35d266b | 2034 | static const struct bfd_elf_special_section special_sections_i[] = |
7f4d3958 | 2035 | { |
0112cd26 NC |
2036 | { STRING_COMMA_LEN (".init"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR }, |
2037 | { STRING_COMMA_LEN (".init_array"), 0, SHT_INIT_ARRAY, SHF_ALLOC + SHF_WRITE }, | |
2038 | { STRING_COMMA_LEN (".interp"), 0, SHT_PROGBITS, 0 }, | |
2039 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2040 | }; |
2041 | ||
b35d266b | 2042 | static const struct bfd_elf_special_section special_sections_l[] = |
7f4d3958 | 2043 | { |
0112cd26 NC |
2044 | { STRING_COMMA_LEN (".line"), 0, SHT_PROGBITS, 0 }, |
2045 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2046 | }; |
2047 | ||
b35d266b | 2048 | static const struct bfd_elf_special_section special_sections_n[] = |
7f4d3958 | 2049 | { |
0112cd26 NC |
2050 | { STRING_COMMA_LEN (".note.GNU-stack"), 0, SHT_PROGBITS, 0 }, |
2051 | { STRING_COMMA_LEN (".note"), -1, SHT_NOTE, 0 }, | |
2052 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2053 | }; |
2054 | ||
b35d266b | 2055 | static const struct bfd_elf_special_section special_sections_p[] = |
7f4d3958 | 2056 | { |
0112cd26 NC |
2057 | { STRING_COMMA_LEN (".preinit_array"), 0, SHT_PREINIT_ARRAY, SHF_ALLOC + SHF_WRITE }, |
2058 | { STRING_COMMA_LEN (".plt"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR }, | |
2059 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2060 | }; |
2061 | ||
b35d266b | 2062 | static const struct bfd_elf_special_section special_sections_r[] = |
7f4d3958 | 2063 | { |
0112cd26 NC |
2064 | { STRING_COMMA_LEN (".rodata"), -2, SHT_PROGBITS, SHF_ALLOC }, |
2065 | { STRING_COMMA_LEN (".rodata1"), 0, SHT_PROGBITS, SHF_ALLOC }, | |
2066 | { STRING_COMMA_LEN (".rela"), -1, SHT_RELA, 0 }, | |
2067 | { STRING_COMMA_LEN (".rel"), -1, SHT_REL, 0 }, | |
2068 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2069 | }; |
2070 | ||
b35d266b | 2071 | static const struct bfd_elf_special_section special_sections_s[] = |
7f4d3958 | 2072 | { |
0112cd26 NC |
2073 | { STRING_COMMA_LEN (".shstrtab"), 0, SHT_STRTAB, 0 }, |
2074 | { STRING_COMMA_LEN (".strtab"), 0, SHT_STRTAB, 0 }, | |
2075 | { STRING_COMMA_LEN (".symtab"), 0, SHT_SYMTAB, 0 }, | |
60ff4dc4 HPN |
2076 | /* See struct bfd_elf_special_section declaration for the semantics of |
2077 | this special case where .prefix_length != strlen (.prefix). */ | |
2078 | { ".stabstr", 5, 3, SHT_STRTAB, 0 }, | |
0112cd26 | 2079 | { NULL, 0, 0, 0, 0 } |
2f89ff8d L |
2080 | }; |
2081 | ||
b35d266b | 2082 | static const struct bfd_elf_special_section special_sections_t[] = |
7f4d3958 | 2083 | { |
0112cd26 NC |
2084 | { STRING_COMMA_LEN (".text"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR }, |
2085 | { STRING_COMMA_LEN (".tbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS }, | |
2086 | { STRING_COMMA_LEN (".tdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS }, | |
2087 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2088 | }; |
2089 | ||
1b315056 CS |
2090 | static const struct bfd_elf_special_section special_sections_z[] = |
2091 | { | |
2092 | { STRING_COMMA_LEN (".zdebug_line"), 0, SHT_PROGBITS, 0 }, | |
2093 | { STRING_COMMA_LEN (".zdebug_info"), 0, SHT_PROGBITS, 0 }, | |
2094 | { STRING_COMMA_LEN (".zdebug_abbrev"), 0, SHT_PROGBITS, 0 }, | |
2095 | { STRING_COMMA_LEN (".zdebug_aranges"), 0, SHT_PROGBITS, 0 }, | |
2096 | { NULL, 0, 0, 0, 0 } | |
2097 | }; | |
2098 | ||
b35d266b | 2099 | static const struct bfd_elf_special_section *special_sections[] = |
7f4d3958 | 2100 | { |
7f4d3958 | 2101 | special_sections_b, /* 'b' */ |
98ece1b3 | 2102 | special_sections_c, /* 'c' */ |
7f4d3958 L |
2103 | special_sections_d, /* 'd' */ |
2104 | NULL, /* 'e' */ | |
2105 | special_sections_f, /* 'f' */ | |
2106 | special_sections_g, /* 'g' */ | |
2107 | special_sections_h, /* 'h' */ | |
2108 | special_sections_i, /* 'i' */ | |
2109 | NULL, /* 'j' */ | |
2110 | NULL, /* 'k' */ | |
2111 | special_sections_l, /* 'l' */ | |
2112 | NULL, /* 'm' */ | |
2113 | special_sections_n, /* 'n' */ | |
2114 | NULL, /* 'o' */ | |
2115 | special_sections_p, /* 'p' */ | |
2116 | NULL, /* 'q' */ | |
2117 | special_sections_r, /* 'r' */ | |
2118 | special_sections_s, /* 's' */ | |
2119 | special_sections_t, /* 't' */ | |
1b315056 CS |
2120 | NULL, /* 'u' */ |
2121 | NULL, /* 'v' */ | |
2122 | NULL, /* 'w' */ | |
2123 | NULL, /* 'x' */ | |
2124 | NULL, /* 'y' */ | |
2125 | special_sections_z /* 'z' */ | |
7f4d3958 L |
2126 | }; |
2127 | ||
551b43fd AM |
2128 | const struct bfd_elf_special_section * |
2129 | _bfd_elf_get_special_section (const char *name, | |
2130 | const struct bfd_elf_special_section *spec, | |
2131 | unsigned int rela) | |
2f89ff8d L |
2132 | { |
2133 | int i; | |
7f4d3958 | 2134 | int len; |
7f4d3958 | 2135 | |
551b43fd | 2136 | len = strlen (name); |
7f4d3958 | 2137 | |
551b43fd | 2138 | for (i = 0; spec[i].prefix != NULL; i++) |
7dcb9820 AM |
2139 | { |
2140 | int suffix_len; | |
551b43fd | 2141 | int prefix_len = spec[i].prefix_length; |
7dcb9820 AM |
2142 | |
2143 | if (len < prefix_len) | |
2144 | continue; | |
551b43fd | 2145 | if (memcmp (name, spec[i].prefix, prefix_len) != 0) |
7dcb9820 AM |
2146 | continue; |
2147 | ||
551b43fd | 2148 | suffix_len = spec[i].suffix_length; |
7dcb9820 AM |
2149 | if (suffix_len <= 0) |
2150 | { | |
2151 | if (name[prefix_len] != 0) | |
2152 | { | |
2153 | if (suffix_len == 0) | |
2154 | continue; | |
2155 | if (name[prefix_len] != '.' | |
2156 | && (suffix_len == -2 | |
551b43fd | 2157 | || (rela && spec[i].type == SHT_REL))) |
7dcb9820 AM |
2158 | continue; |
2159 | } | |
2160 | } | |
2161 | else | |
2162 | { | |
2163 | if (len < prefix_len + suffix_len) | |
2164 | continue; | |
2165 | if (memcmp (name + len - suffix_len, | |
551b43fd | 2166 | spec[i].prefix + prefix_len, |
7dcb9820 AM |
2167 | suffix_len) != 0) |
2168 | continue; | |
2169 | } | |
551b43fd | 2170 | return &spec[i]; |
7dcb9820 | 2171 | } |
2f89ff8d L |
2172 | |
2173 | return NULL; | |
2174 | } | |
2175 | ||
7dcb9820 | 2176 | const struct bfd_elf_special_section * |
29ef7005 | 2177 | _bfd_elf_get_sec_type_attr (bfd *abfd, asection *sec) |
2f89ff8d | 2178 | { |
551b43fd AM |
2179 | int i; |
2180 | const struct bfd_elf_special_section *spec; | |
29ef7005 | 2181 | const struct elf_backend_data *bed; |
2f89ff8d L |
2182 | |
2183 | /* See if this is one of the special sections. */ | |
551b43fd AM |
2184 | if (sec->name == NULL) |
2185 | return NULL; | |
2f89ff8d | 2186 | |
29ef7005 L |
2187 | bed = get_elf_backend_data (abfd); |
2188 | spec = bed->special_sections; | |
2189 | if (spec) | |
2190 | { | |
2191 | spec = _bfd_elf_get_special_section (sec->name, | |
2192 | bed->special_sections, | |
2193 | sec->use_rela_p); | |
2194 | if (spec != NULL) | |
2195 | return spec; | |
2196 | } | |
2197 | ||
551b43fd AM |
2198 | if (sec->name[0] != '.') |
2199 | return NULL; | |
2f89ff8d | 2200 | |
551b43fd | 2201 | i = sec->name[1] - 'b'; |
1b315056 | 2202 | if (i < 0 || i > 'z' - 'b') |
551b43fd AM |
2203 | return NULL; |
2204 | ||
2205 | spec = special_sections[i]; | |
2f89ff8d | 2206 | |
551b43fd AM |
2207 | if (spec == NULL) |
2208 | return NULL; | |
2209 | ||
2210 | return _bfd_elf_get_special_section (sec->name, spec, sec->use_rela_p); | |
2f89ff8d L |
2211 | } |
2212 | ||
b34976b6 | 2213 | bfd_boolean |
217aa764 | 2214 | _bfd_elf_new_section_hook (bfd *abfd, asection *sec) |
252b5132 RH |
2215 | { |
2216 | struct bfd_elf_section_data *sdata; | |
551b43fd | 2217 | const struct elf_backend_data *bed; |
7dcb9820 | 2218 | const struct bfd_elf_special_section *ssect; |
252b5132 | 2219 | |
f0abc2a1 AM |
2220 | sdata = (struct bfd_elf_section_data *) sec->used_by_bfd; |
2221 | if (sdata == NULL) | |
2222 | { | |
a50b1753 NC |
2223 | sdata = (struct bfd_elf_section_data *) bfd_zalloc (abfd, |
2224 | sizeof (*sdata)); | |
f0abc2a1 AM |
2225 | if (sdata == NULL) |
2226 | return FALSE; | |
217aa764 | 2227 | sec->used_by_bfd = sdata; |
f0abc2a1 | 2228 | } |
bf572ba0 | 2229 | |
551b43fd AM |
2230 | /* Indicate whether or not this section should use RELA relocations. */ |
2231 | bed = get_elf_backend_data (abfd); | |
2232 | sec->use_rela_p = bed->default_use_rela_p; | |
2233 | ||
e843e0f8 L |
2234 | /* When we read a file, we don't need to set ELF section type and |
2235 | flags. They will be overridden in _bfd_elf_make_section_from_shdr | |
2236 | anyway. We will set ELF section type and flags for all linker | |
2237 | created sections. If user specifies BFD section flags, we will | |
2238 | set ELF section type and flags based on BFD section flags in | |
2239 | elf_fake_sections. */ | |
2240 | if ((!sec->flags && abfd->direction != read_direction) | |
3496cb2a | 2241 | || (sec->flags & SEC_LINKER_CREATED) != 0) |
2f89ff8d | 2242 | { |
551b43fd | 2243 | ssect = (*bed->get_sec_type_attr) (abfd, sec); |
a31501e9 L |
2244 | if (ssect != NULL) |
2245 | { | |
2246 | elf_section_type (sec) = ssect->type; | |
2247 | elf_section_flags (sec) = ssect->attr; | |
2248 | } | |
2f89ff8d L |
2249 | } |
2250 | ||
f592407e | 2251 | return _bfd_generic_new_section_hook (abfd, sec); |
252b5132 RH |
2252 | } |
2253 | ||
2254 | /* Create a new bfd section from an ELF program header. | |
2255 | ||
2256 | Since program segments have no names, we generate a synthetic name | |
2257 | of the form segment<NUM>, where NUM is generally the index in the | |
2258 | program header table. For segments that are split (see below) we | |
2259 | generate the names segment<NUM>a and segment<NUM>b. | |
2260 | ||
2261 | Note that some program segments may have a file size that is different than | |
2262 | (less than) the memory size. All this means is that at execution the | |
2263 | system must allocate the amount of memory specified by the memory size, | |
2264 | but only initialize it with the first "file size" bytes read from the | |
2265 | file. This would occur for example, with program segments consisting | |
2266 | of combined data+bss. | |
2267 | ||
2268 | To handle the above situation, this routine generates TWO bfd sections | |
2269 | for the single program segment. The first has the length specified by | |
2270 | the file size of the segment, and the second has the length specified | |
2271 | by the difference between the two sizes. In effect, the segment is split | |
d5191d0c | 2272 | into its initialized and uninitialized parts. |
252b5132 RH |
2273 | |
2274 | */ | |
2275 | ||
b34976b6 | 2276 | bfd_boolean |
217aa764 AM |
2277 | _bfd_elf_make_section_from_phdr (bfd *abfd, |
2278 | Elf_Internal_Phdr *hdr, | |
91d6fa6a | 2279 | int hdr_index, |
a50b1753 | 2280 | const char *type_name) |
252b5132 RH |
2281 | { |
2282 | asection *newsect; | |
2283 | char *name; | |
2284 | char namebuf[64]; | |
d4c88bbb | 2285 | size_t len; |
252b5132 RH |
2286 | int split; |
2287 | ||
2288 | split = ((hdr->p_memsz > 0) | |
2289 | && (hdr->p_filesz > 0) | |
2290 | && (hdr->p_memsz > hdr->p_filesz)); | |
d5191d0c AM |
2291 | |
2292 | if (hdr->p_filesz > 0) | |
252b5132 | 2293 | { |
91d6fa6a | 2294 | sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "a" : ""); |
d5191d0c | 2295 | len = strlen (namebuf) + 1; |
a50b1753 | 2296 | name = (char *) bfd_alloc (abfd, len); |
d5191d0c AM |
2297 | if (!name) |
2298 | return FALSE; | |
2299 | memcpy (name, namebuf, len); | |
2300 | newsect = bfd_make_section (abfd, name); | |
2301 | if (newsect == NULL) | |
2302 | return FALSE; | |
2303 | newsect->vma = hdr->p_vaddr; | |
2304 | newsect->lma = hdr->p_paddr; | |
2305 | newsect->size = hdr->p_filesz; | |
2306 | newsect->filepos = hdr->p_offset; | |
2307 | newsect->flags |= SEC_HAS_CONTENTS; | |
2308 | newsect->alignment_power = bfd_log2 (hdr->p_align); | |
2309 | if (hdr->p_type == PT_LOAD) | |
252b5132 | 2310 | { |
d5191d0c AM |
2311 | newsect->flags |= SEC_ALLOC; |
2312 | newsect->flags |= SEC_LOAD; | |
2313 | if (hdr->p_flags & PF_X) | |
2314 | { | |
2315 | /* FIXME: all we known is that it has execute PERMISSION, | |
2316 | may be data. */ | |
2317 | newsect->flags |= SEC_CODE; | |
2318 | } | |
2319 | } | |
2320 | if (!(hdr->p_flags & PF_W)) | |
2321 | { | |
2322 | newsect->flags |= SEC_READONLY; | |
252b5132 | 2323 | } |
252b5132 RH |
2324 | } |
2325 | ||
d5191d0c | 2326 | if (hdr->p_memsz > hdr->p_filesz) |
252b5132 | 2327 | { |
d5191d0c AM |
2328 | bfd_vma align; |
2329 | ||
91d6fa6a | 2330 | sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "b" : ""); |
d4c88bbb | 2331 | len = strlen (namebuf) + 1; |
a50b1753 | 2332 | name = (char *) bfd_alloc (abfd, len); |
252b5132 | 2333 | if (!name) |
b34976b6 | 2334 | return FALSE; |
d4c88bbb | 2335 | memcpy (name, namebuf, len); |
252b5132 RH |
2336 | newsect = bfd_make_section (abfd, name); |
2337 | if (newsect == NULL) | |
b34976b6 | 2338 | return FALSE; |
252b5132 RH |
2339 | newsect->vma = hdr->p_vaddr + hdr->p_filesz; |
2340 | newsect->lma = hdr->p_paddr + hdr->p_filesz; | |
eea6121a | 2341 | newsect->size = hdr->p_memsz - hdr->p_filesz; |
d5191d0c AM |
2342 | newsect->filepos = hdr->p_offset + hdr->p_filesz; |
2343 | align = newsect->vma & -newsect->vma; | |
2344 | if (align == 0 || align > hdr->p_align) | |
2345 | align = hdr->p_align; | |
2346 | newsect->alignment_power = bfd_log2 (align); | |
252b5132 RH |
2347 | if (hdr->p_type == PT_LOAD) |
2348 | { | |
d5191d0c AM |
2349 | /* Hack for gdb. Segments that have not been modified do |
2350 | not have their contents written to a core file, on the | |
2351 | assumption that a debugger can find the contents in the | |
2352 | executable. We flag this case by setting the fake | |
2353 | section size to zero. Note that "real" bss sections will | |
2354 | always have their contents dumped to the core file. */ | |
2355 | if (bfd_get_format (abfd) == bfd_core) | |
2356 | newsect->size = 0; | |
252b5132 RH |
2357 | newsect->flags |= SEC_ALLOC; |
2358 | if (hdr->p_flags & PF_X) | |
2359 | newsect->flags |= SEC_CODE; | |
2360 | } | |
2361 | if (!(hdr->p_flags & PF_W)) | |
2362 | newsect->flags |= SEC_READONLY; | |
2363 | } | |
2364 | ||
b34976b6 | 2365 | return TRUE; |
252b5132 RH |
2366 | } |
2367 | ||
b34976b6 | 2368 | bfd_boolean |
91d6fa6a | 2369 | bfd_section_from_phdr (bfd *abfd, Elf_Internal_Phdr *hdr, int hdr_index) |
20cfcaae | 2370 | { |
9c5bfbb7 | 2371 | const struct elf_backend_data *bed; |
20cfcaae NC |
2372 | |
2373 | switch (hdr->p_type) | |
2374 | { | |
2375 | case PT_NULL: | |
91d6fa6a | 2376 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "null"); |
20cfcaae NC |
2377 | |
2378 | case PT_LOAD: | |
91d6fa6a | 2379 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "load"); |
20cfcaae NC |
2380 | |
2381 | case PT_DYNAMIC: | |
91d6fa6a | 2382 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "dynamic"); |
20cfcaae NC |
2383 | |
2384 | case PT_INTERP: | |
91d6fa6a | 2385 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "interp"); |
20cfcaae NC |
2386 | |
2387 | case PT_NOTE: | |
91d6fa6a | 2388 | if (! _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "note")) |
b34976b6 | 2389 | return FALSE; |
718175fa | 2390 | if (! elf_read_notes (abfd, hdr->p_offset, hdr->p_filesz)) |
b34976b6 AM |
2391 | return FALSE; |
2392 | return TRUE; | |
20cfcaae NC |
2393 | |
2394 | case PT_SHLIB: | |
91d6fa6a | 2395 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "shlib"); |
20cfcaae NC |
2396 | |
2397 | case PT_PHDR: | |
91d6fa6a | 2398 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "phdr"); |
20cfcaae | 2399 | |
811072d8 | 2400 | case PT_GNU_EH_FRAME: |
91d6fa6a | 2401 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, |
811072d8 RM |
2402 | "eh_frame_hdr"); |
2403 | ||
2b05f1b7 | 2404 | case PT_GNU_STACK: |
91d6fa6a | 2405 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "stack"); |
9ee5e499 | 2406 | |
8c37241b | 2407 | case PT_GNU_RELRO: |
91d6fa6a | 2408 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "relro"); |
8c37241b | 2409 | |
20cfcaae | 2410 | default: |
8c1acd09 | 2411 | /* Check for any processor-specific program segment types. */ |
20cfcaae | 2412 | bed = get_elf_backend_data (abfd); |
91d6fa6a | 2413 | return bed->elf_backend_section_from_phdr (abfd, hdr, hdr_index, "proc"); |
20cfcaae NC |
2414 | } |
2415 | } | |
2416 | ||
d4730f92 BS |
2417 | /* Return the REL_HDR for SEC, assuming there is only a single one, either |
2418 | REL or RELA. */ | |
2419 | ||
2420 | Elf_Internal_Shdr * | |
2421 | _bfd_elf_single_rel_hdr (asection *sec) | |
2422 | { | |
2423 | if (elf_section_data (sec)->rel.hdr) | |
2424 | { | |
2425 | BFD_ASSERT (elf_section_data (sec)->rela.hdr == NULL); | |
2426 | return elf_section_data (sec)->rel.hdr; | |
2427 | } | |
2428 | else | |
2429 | return elf_section_data (sec)->rela.hdr; | |
2430 | } | |
2431 | ||
2432 | /* Allocate and initialize a section-header for a new reloc section, | |
2433 | containing relocations against ASECT. It is stored in RELDATA. If | |
2434 | USE_RELA_P is TRUE, we use RELA relocations; otherwise, we use REL | |
2435 | relocations. */ | |
23bc299b | 2436 | |
b34976b6 | 2437 | bfd_boolean |
217aa764 | 2438 | _bfd_elf_init_reloc_shdr (bfd *abfd, |
d4730f92 | 2439 | struct bfd_elf_section_reloc_data *reldata, |
217aa764 AM |
2440 | asection *asect, |
2441 | bfd_boolean use_rela_p) | |
23bc299b | 2442 | { |
d4730f92 | 2443 | Elf_Internal_Shdr *rel_hdr; |
23bc299b | 2444 | char *name; |
9c5bfbb7 | 2445 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
d4730f92 BS |
2446 | bfd_size_type amt; |
2447 | ||
2448 | amt = sizeof (Elf_Internal_Shdr); | |
2449 | BFD_ASSERT (reldata->hdr == NULL); | |
2450 | rel_hdr = bfd_zalloc (abfd, amt); | |
2451 | reldata->hdr = rel_hdr; | |
23bc299b | 2452 | |
d4730f92 | 2453 | amt = sizeof ".rela" + strlen (asect->name); |
a50b1753 | 2454 | name = (char *) bfd_alloc (abfd, amt); |
23bc299b | 2455 | if (name == NULL) |
b34976b6 | 2456 | return FALSE; |
23bc299b MM |
2457 | sprintf (name, "%s%s", use_rela_p ? ".rela" : ".rel", asect->name); |
2458 | rel_hdr->sh_name = | |
2b0f7ef9 | 2459 | (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), name, |
b34976b6 | 2460 | FALSE); |
23bc299b | 2461 | if (rel_hdr->sh_name == (unsigned int) -1) |
b34976b6 | 2462 | return FALSE; |
23bc299b MM |
2463 | rel_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL; |
2464 | rel_hdr->sh_entsize = (use_rela_p | |
2465 | ? bed->s->sizeof_rela | |
2466 | : bed->s->sizeof_rel); | |
72de5009 | 2467 | rel_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align; |
23bc299b MM |
2468 | rel_hdr->sh_flags = 0; |
2469 | rel_hdr->sh_addr = 0; | |
2470 | rel_hdr->sh_size = 0; | |
2471 | rel_hdr->sh_offset = 0; | |
2472 | ||
b34976b6 | 2473 | return TRUE; |
23bc299b MM |
2474 | } |
2475 | ||
94be91de JB |
2476 | /* Return the default section type based on the passed in section flags. */ |
2477 | ||
2478 | int | |
2479 | bfd_elf_get_default_section_type (flagword flags) | |
2480 | { | |
2481 | if ((flags & SEC_ALLOC) != 0 | |
2e76e85a | 2482 | && (flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0) |
94be91de JB |
2483 | return SHT_NOBITS; |
2484 | return SHT_PROGBITS; | |
2485 | } | |
2486 | ||
d4730f92 BS |
2487 | struct fake_section_arg |
2488 | { | |
2489 | struct bfd_link_info *link_info; | |
2490 | bfd_boolean failed; | |
2491 | }; | |
2492 | ||
252b5132 RH |
2493 | /* Set up an ELF internal section header for a section. */ |
2494 | ||
252b5132 | 2495 | static void |
d4730f92 | 2496 | elf_fake_sections (bfd *abfd, asection *asect, void *fsarg) |
252b5132 | 2497 | { |
d4730f92 | 2498 | struct fake_section_arg *arg = (struct fake_section_arg *)fsarg; |
9c5bfbb7 | 2499 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
d4730f92 | 2500 | struct bfd_elf_section_data *esd = elf_section_data (asect); |
252b5132 | 2501 | Elf_Internal_Shdr *this_hdr; |
0414f35b | 2502 | unsigned int sh_type; |
252b5132 | 2503 | |
d4730f92 | 2504 | if (arg->failed) |
252b5132 RH |
2505 | { |
2506 | /* We already failed; just get out of the bfd_map_over_sections | |
08a40648 | 2507 | loop. */ |
252b5132 RH |
2508 | return; |
2509 | } | |
2510 | ||
d4730f92 | 2511 | this_hdr = &esd->this_hdr; |
252b5132 | 2512 | |
e57b5356 AM |
2513 | this_hdr->sh_name = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), |
2514 | asect->name, FALSE); | |
2515 | if (this_hdr->sh_name == (unsigned int) -1) | |
252b5132 | 2516 | { |
d4730f92 | 2517 | arg->failed = TRUE; |
252b5132 RH |
2518 | return; |
2519 | } | |
2520 | ||
a4d8e49b | 2521 | /* Don't clear sh_flags. Assembler may set additional bits. */ |
252b5132 RH |
2522 | |
2523 | if ((asect->flags & SEC_ALLOC) != 0 | |
2524 | || asect->user_set_vma) | |
2525 | this_hdr->sh_addr = asect->vma; | |
2526 | else | |
2527 | this_hdr->sh_addr = 0; | |
2528 | ||
2529 | this_hdr->sh_offset = 0; | |
eea6121a | 2530 | this_hdr->sh_size = asect->size; |
252b5132 | 2531 | this_hdr->sh_link = 0; |
72de5009 | 2532 | this_hdr->sh_addralign = (bfd_vma) 1 << asect->alignment_power; |
252b5132 RH |
2533 | /* The sh_entsize and sh_info fields may have been set already by |
2534 | copy_private_section_data. */ | |
2535 | ||
2536 | this_hdr->bfd_section = asect; | |
2537 | this_hdr->contents = NULL; | |
2538 | ||
3cddba1e L |
2539 | /* If the section type is unspecified, we set it based on |
2540 | asect->flags. */ | |
98ece1b3 AM |
2541 | if ((asect->flags & SEC_GROUP) != 0) |
2542 | sh_type = SHT_GROUP; | |
98ece1b3 | 2543 | else |
94be91de | 2544 | sh_type = bfd_elf_get_default_section_type (asect->flags); |
98ece1b3 | 2545 | |
3cddba1e | 2546 | if (this_hdr->sh_type == SHT_NULL) |
98ece1b3 AM |
2547 | this_hdr->sh_type = sh_type; |
2548 | else if (this_hdr->sh_type == SHT_NOBITS | |
2549 | && sh_type == SHT_PROGBITS | |
2550 | && (asect->flags & SEC_ALLOC) != 0) | |
3cddba1e | 2551 | { |
98ece1b3 AM |
2552 | /* Warn if we are changing a NOBITS section to PROGBITS, but |
2553 | allow the link to proceed. This can happen when users link | |
2554 | non-bss input sections to bss output sections, or emit data | |
2555 | to a bss output section via a linker script. */ | |
2556 | (*_bfd_error_handler) | |
58f0869b | 2557 | (_("warning: section `%A' type changed to PROGBITS"), asect); |
98ece1b3 | 2558 | this_hdr->sh_type = sh_type; |
3cddba1e L |
2559 | } |
2560 | ||
2f89ff8d | 2561 | switch (this_hdr->sh_type) |
252b5132 | 2562 | { |
2f89ff8d | 2563 | default: |
2f89ff8d L |
2564 | break; |
2565 | ||
2566 | case SHT_STRTAB: | |
2567 | case SHT_INIT_ARRAY: | |
2568 | case SHT_FINI_ARRAY: | |
2569 | case SHT_PREINIT_ARRAY: | |
2570 | case SHT_NOTE: | |
2571 | case SHT_NOBITS: | |
2572 | case SHT_PROGBITS: | |
2573 | break; | |
2574 | ||
2575 | case SHT_HASH: | |
c7ac6ff8 | 2576 | this_hdr->sh_entsize = bed->s->sizeof_hash_entry; |
2f89ff8d | 2577 | break; |
5de3bf90 | 2578 | |
2f89ff8d | 2579 | case SHT_DYNSYM: |
252b5132 | 2580 | this_hdr->sh_entsize = bed->s->sizeof_sym; |
2f89ff8d L |
2581 | break; |
2582 | ||
2583 | case SHT_DYNAMIC: | |
252b5132 | 2584 | this_hdr->sh_entsize = bed->s->sizeof_dyn; |
2f89ff8d L |
2585 | break; |
2586 | ||
2587 | case SHT_RELA: | |
2588 | if (get_elf_backend_data (abfd)->may_use_rela_p) | |
2589 | this_hdr->sh_entsize = bed->s->sizeof_rela; | |
2590 | break; | |
2591 | ||
2592 | case SHT_REL: | |
2593 | if (get_elf_backend_data (abfd)->may_use_rel_p) | |
2594 | this_hdr->sh_entsize = bed->s->sizeof_rel; | |
2595 | break; | |
2596 | ||
2597 | case SHT_GNU_versym: | |
252b5132 | 2598 | this_hdr->sh_entsize = sizeof (Elf_External_Versym); |
2f89ff8d L |
2599 | break; |
2600 | ||
2601 | case SHT_GNU_verdef: | |
252b5132 RH |
2602 | this_hdr->sh_entsize = 0; |
2603 | /* objcopy or strip will copy over sh_info, but may not set | |
08a40648 AM |
2604 | cverdefs. The linker will set cverdefs, but sh_info will be |
2605 | zero. */ | |
252b5132 RH |
2606 | if (this_hdr->sh_info == 0) |
2607 | this_hdr->sh_info = elf_tdata (abfd)->cverdefs; | |
2608 | else | |
2609 | BFD_ASSERT (elf_tdata (abfd)->cverdefs == 0 | |
2610 | || this_hdr->sh_info == elf_tdata (abfd)->cverdefs); | |
2f89ff8d L |
2611 | break; |
2612 | ||
2613 | case SHT_GNU_verneed: | |
252b5132 RH |
2614 | this_hdr->sh_entsize = 0; |
2615 | /* objcopy or strip will copy over sh_info, but may not set | |
08a40648 AM |
2616 | cverrefs. The linker will set cverrefs, but sh_info will be |
2617 | zero. */ | |
252b5132 RH |
2618 | if (this_hdr->sh_info == 0) |
2619 | this_hdr->sh_info = elf_tdata (abfd)->cverrefs; | |
2620 | else | |
2621 | BFD_ASSERT (elf_tdata (abfd)->cverrefs == 0 | |
2622 | || this_hdr->sh_info == elf_tdata (abfd)->cverrefs); | |
2f89ff8d L |
2623 | break; |
2624 | ||
2625 | case SHT_GROUP: | |
1783205a | 2626 | this_hdr->sh_entsize = GRP_ENTRY_SIZE; |
2f89ff8d | 2627 | break; |
fdc90cb4 JJ |
2628 | |
2629 | case SHT_GNU_HASH: | |
2630 | this_hdr->sh_entsize = bed->s->arch_size == 64 ? 0 : 4; | |
2631 | break; | |
dbb410c3 | 2632 | } |
252b5132 RH |
2633 | |
2634 | if ((asect->flags & SEC_ALLOC) != 0) | |
2635 | this_hdr->sh_flags |= SHF_ALLOC; | |
2636 | if ((asect->flags & SEC_READONLY) == 0) | |
2637 | this_hdr->sh_flags |= SHF_WRITE; | |
2638 | if ((asect->flags & SEC_CODE) != 0) | |
2639 | this_hdr->sh_flags |= SHF_EXECINSTR; | |
f5fa8ca2 JJ |
2640 | if ((asect->flags & SEC_MERGE) != 0) |
2641 | { | |
2642 | this_hdr->sh_flags |= SHF_MERGE; | |
2643 | this_hdr->sh_entsize = asect->entsize; | |
2644 | if ((asect->flags & SEC_STRINGS) != 0) | |
2645 | this_hdr->sh_flags |= SHF_STRINGS; | |
2646 | } | |
1126897b | 2647 | if ((asect->flags & SEC_GROUP) == 0 && elf_group_name (asect) != NULL) |
dbb410c3 | 2648 | this_hdr->sh_flags |= SHF_GROUP; |
13ae64f3 | 2649 | if ((asect->flags & SEC_THREAD_LOCAL) != 0) |
704afa60 JJ |
2650 | { |
2651 | this_hdr->sh_flags |= SHF_TLS; | |
3a800eb9 AM |
2652 | if (asect->size == 0 |
2653 | && (asect->flags & SEC_HAS_CONTENTS) == 0) | |
704afa60 | 2654 | { |
3a800eb9 | 2655 | struct bfd_link_order *o = asect->map_tail.link_order; |
b34976b6 | 2656 | |
704afa60 | 2657 | this_hdr->sh_size = 0; |
3a800eb9 AM |
2658 | if (o != NULL) |
2659 | { | |
704afa60 | 2660 | this_hdr->sh_size = o->offset + o->size; |
3a800eb9 AM |
2661 | if (this_hdr->sh_size != 0) |
2662 | this_hdr->sh_type = SHT_NOBITS; | |
2663 | } | |
704afa60 JJ |
2664 | } |
2665 | } | |
18ae9cc1 L |
2666 | if ((asect->flags & (SEC_GROUP | SEC_EXCLUDE)) == SEC_EXCLUDE) |
2667 | this_hdr->sh_flags |= SHF_EXCLUDE; | |
252b5132 | 2668 | |
d4730f92 BS |
2669 | /* If the section has relocs, set up a section header for the |
2670 | SHT_REL[A] section. If two relocation sections are required for | |
2671 | this section, it is up to the processor-specific back-end to | |
2672 | create the other. */ | |
2673 | if ((asect->flags & SEC_RELOC) != 0) | |
2674 | { | |
2675 | /* When doing a relocatable link, create both REL and RELA sections if | |
2676 | needed. */ | |
2677 | if (arg->link_info | |
2678 | /* Do the normal setup if we wouldn't create any sections here. */ | |
2679 | && esd->rel.count + esd->rela.count > 0 | |
2680 | && (arg->link_info->relocatable || arg->link_info->emitrelocations)) | |
2681 | { | |
2682 | if (esd->rel.count && esd->rel.hdr == NULL | |
2683 | && !_bfd_elf_init_reloc_shdr (abfd, &esd->rel, asect, FALSE)) | |
2684 | { | |
2685 | arg->failed = TRUE; | |
2686 | return; | |
2687 | } | |
2688 | if (esd->rela.count && esd->rela.hdr == NULL | |
2689 | && !_bfd_elf_init_reloc_shdr (abfd, &esd->rela, asect, TRUE)) | |
2690 | { | |
2691 | arg->failed = TRUE; | |
2692 | return; | |
2693 | } | |
2694 | } | |
2695 | else if (!_bfd_elf_init_reloc_shdr (abfd, | |
2696 | (asect->use_rela_p | |
2697 | ? &esd->rela : &esd->rel), | |
2698 | asect, | |
2699 | asect->use_rela_p)) | |
2700 | arg->failed = TRUE; | |
2701 | } | |
2702 | ||
252b5132 | 2703 | /* Check for processor-specific section types. */ |
0414f35b | 2704 | sh_type = this_hdr->sh_type; |
e1fddb6b AO |
2705 | if (bed->elf_backend_fake_sections |
2706 | && !(*bed->elf_backend_fake_sections) (abfd, this_hdr, asect)) | |
d4730f92 | 2707 | arg->failed = TRUE; |
252b5132 | 2708 | |
42bb2e33 | 2709 | if (sh_type == SHT_NOBITS && asect->size != 0) |
0414f35b AM |
2710 | { |
2711 | /* Don't change the header type from NOBITS if we are being | |
42bb2e33 | 2712 | called for objcopy --only-keep-debug. */ |
0414f35b AM |
2713 | this_hdr->sh_type = sh_type; |
2714 | } | |
252b5132 RH |
2715 | } |
2716 | ||
bcacc0f5 AM |
2717 | /* Fill in the contents of a SHT_GROUP section. Called from |
2718 | _bfd_elf_compute_section_file_positions for gas, objcopy, and | |
2719 | when ELF targets use the generic linker, ld. Called for ld -r | |
2720 | from bfd_elf_final_link. */ | |
dbb410c3 | 2721 | |
1126897b | 2722 | void |
217aa764 | 2723 | bfd_elf_set_group_contents (bfd *abfd, asection *sec, void *failedptrarg) |
dbb410c3 | 2724 | { |
a50b1753 | 2725 | bfd_boolean *failedptr = (bfd_boolean *) failedptrarg; |
9dce4196 | 2726 | asection *elt, *first; |
dbb410c3 | 2727 | unsigned char *loc; |
b34976b6 | 2728 | bfd_boolean gas; |
dbb410c3 | 2729 | |
7e4111ad L |
2730 | /* Ignore linker created group section. See elfNN_ia64_object_p in |
2731 | elfxx-ia64.c. */ | |
2732 | if (((sec->flags & (SEC_GROUP | SEC_LINKER_CREATED)) != SEC_GROUP) | |
dbb410c3 AM |
2733 | || *failedptr) |
2734 | return; | |
2735 | ||
bcacc0f5 AM |
2736 | if (elf_section_data (sec)->this_hdr.sh_info == 0) |
2737 | { | |
2738 | unsigned long symindx = 0; | |
2739 | ||
2740 | /* elf_group_id will have been set up by objcopy and the | |
2741 | generic linker. */ | |
2742 | if (elf_group_id (sec) != NULL) | |
2743 | symindx = elf_group_id (sec)->udata.i; | |
1126897b | 2744 | |
bcacc0f5 AM |
2745 | if (symindx == 0) |
2746 | { | |
2747 | /* If called from the assembler, swap_out_syms will have set up | |
2748 | elf_section_syms. */ | |
2749 | BFD_ASSERT (elf_section_syms (abfd) != NULL); | |
2750 | symindx = elf_section_syms (abfd)[sec->index]->udata.i; | |
2751 | } | |
2752 | elf_section_data (sec)->this_hdr.sh_info = symindx; | |
2753 | } | |
2754 | else if (elf_section_data (sec)->this_hdr.sh_info == (unsigned int) -2) | |
1126897b | 2755 | { |
bcacc0f5 AM |
2756 | /* The ELF backend linker sets sh_info to -2 when the group |
2757 | signature symbol is global, and thus the index can't be | |
2758 | set until all local symbols are output. */ | |
2759 | asection *igroup = elf_sec_group (elf_next_in_group (sec)); | |
2760 | struct bfd_elf_section_data *sec_data = elf_section_data (igroup); | |
2761 | unsigned long symndx = sec_data->this_hdr.sh_info; | |
2762 | unsigned long extsymoff = 0; | |
2763 | struct elf_link_hash_entry *h; | |
2764 | ||
2765 | if (!elf_bad_symtab (igroup->owner)) | |
2766 | { | |
2767 | Elf_Internal_Shdr *symtab_hdr; | |
2768 | ||
2769 | symtab_hdr = &elf_tdata (igroup->owner)->symtab_hdr; | |
2770 | extsymoff = symtab_hdr->sh_info; | |
2771 | } | |
2772 | h = elf_sym_hashes (igroup->owner)[symndx - extsymoff]; | |
2773 | while (h->root.type == bfd_link_hash_indirect | |
2774 | || h->root.type == bfd_link_hash_warning) | |
2775 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
2776 | ||
2777 | elf_section_data (sec)->this_hdr.sh_info = h->indx; | |
1126897b | 2778 | } |
dbb410c3 | 2779 | |
1126897b | 2780 | /* The contents won't be allocated for "ld -r" or objcopy. */ |
b34976b6 | 2781 | gas = TRUE; |
dbb410c3 AM |
2782 | if (sec->contents == NULL) |
2783 | { | |
b34976b6 | 2784 | gas = FALSE; |
a50b1753 | 2785 | sec->contents = (unsigned char *) bfd_alloc (abfd, sec->size); |
9dce4196 AM |
2786 | |
2787 | /* Arrange for the section to be written out. */ | |
2788 | elf_section_data (sec)->this_hdr.contents = sec->contents; | |
dbb410c3 AM |
2789 | if (sec->contents == NULL) |
2790 | { | |
b34976b6 | 2791 | *failedptr = TRUE; |
dbb410c3 AM |
2792 | return; |
2793 | } | |
2794 | } | |
2795 | ||
eea6121a | 2796 | loc = sec->contents + sec->size; |
dbb410c3 | 2797 | |
9dce4196 AM |
2798 | /* Get the pointer to the first section in the group that gas |
2799 | squirreled away here. objcopy arranges for this to be set to the | |
2800 | start of the input section group. */ | |
2801 | first = elt = elf_next_in_group (sec); | |
dbb410c3 AM |
2802 | |
2803 | /* First element is a flag word. Rest of section is elf section | |
2804 | indices for all the sections of the group. Write them backwards | |
2805 | just to keep the group in the same order as given in .section | |
2806 | directives, not that it matters. */ | |
2807 | while (elt != NULL) | |
2808 | { | |
9dce4196 | 2809 | asection *s; |
9dce4196 | 2810 | |
9dce4196 | 2811 | s = elt; |
415f38a6 AM |
2812 | if (!gas) |
2813 | s = s->output_section; | |
2814 | if (s != NULL | |
2815 | && !bfd_is_abs_section (s)) | |
01e1a5bc | 2816 | { |
415f38a6 AM |
2817 | unsigned int idx = elf_section_data (s)->this_idx; |
2818 | ||
01e1a5bc | 2819 | loc -= 4; |
01e1a5bc NC |
2820 | H_PUT_32 (abfd, idx, loc); |
2821 | } | |
945906ff | 2822 | elt = elf_next_in_group (elt); |
9dce4196 AM |
2823 | if (elt == first) |
2824 | break; | |
dbb410c3 AM |
2825 | } |
2826 | ||
3d7f7666 | 2827 | if ((loc -= 4) != sec->contents) |
9dce4196 | 2828 | abort (); |
dbb410c3 | 2829 | |
9dce4196 | 2830 | H_PUT_32 (abfd, sec->flags & SEC_LINK_ONCE ? GRP_COMDAT : 0, loc); |
dbb410c3 AM |
2831 | } |
2832 | ||
252b5132 RH |
2833 | /* Assign all ELF section numbers. The dummy first section is handled here |
2834 | too. The link/info pointers for the standard section types are filled | |
2835 | in here too, while we're at it. */ | |
2836 | ||
b34976b6 | 2837 | static bfd_boolean |
da9f89d4 | 2838 | assign_section_numbers (bfd *abfd, struct bfd_link_info *link_info) |
252b5132 RH |
2839 | { |
2840 | struct elf_obj_tdata *t = elf_tdata (abfd); | |
2841 | asection *sec; | |
2b0f7ef9 | 2842 | unsigned int section_number, secn; |
252b5132 | 2843 | Elf_Internal_Shdr **i_shdrp; |
47cc2cf5 | 2844 | struct bfd_elf_section_data *d; |
3516e984 | 2845 | bfd_boolean need_symtab; |
252b5132 RH |
2846 | |
2847 | section_number = 1; | |
2848 | ||
2b0f7ef9 JJ |
2849 | _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd)); |
2850 | ||
da9f89d4 L |
2851 | /* SHT_GROUP sections are in relocatable files only. */ |
2852 | if (link_info == NULL || link_info->relocatable) | |
252b5132 | 2853 | { |
da9f89d4 | 2854 | /* Put SHT_GROUP sections first. */ |
04dd1667 | 2855 | for (sec = abfd->sections; sec != NULL; sec = sec->next) |
47cc2cf5 | 2856 | { |
5daa8fe7 | 2857 | d = elf_section_data (sec); |
da9f89d4 L |
2858 | |
2859 | if (d->this_hdr.sh_type == SHT_GROUP) | |
08a40648 | 2860 | { |
5daa8fe7 | 2861 | if (sec->flags & SEC_LINKER_CREATED) |
da9f89d4 L |
2862 | { |
2863 | /* Remove the linker created SHT_GROUP sections. */ | |
5daa8fe7 | 2864 | bfd_section_list_remove (abfd, sec); |
da9f89d4 | 2865 | abfd->section_count--; |
da9f89d4 | 2866 | } |
08a40648 | 2867 | else |
4fbb74a6 | 2868 | d->this_idx = section_number++; |
da9f89d4 | 2869 | } |
47cc2cf5 PB |
2870 | } |
2871 | } | |
2872 | ||
2873 | for (sec = abfd->sections; sec; sec = sec->next) | |
2874 | { | |
2875 | d = elf_section_data (sec); | |
2876 | ||
2877 | if (d->this_hdr.sh_type != SHT_GROUP) | |
4fbb74a6 | 2878 | d->this_idx = section_number++; |
2b0f7ef9 | 2879 | _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->this_hdr.sh_name); |
d4730f92 | 2880 | if (d->rel.hdr) |
2b0f7ef9 | 2881 | { |
d4730f92 BS |
2882 | d->rel.idx = section_number++; |
2883 | _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel.hdr->sh_name); | |
2b0f7ef9 | 2884 | } |
d4730f92 BS |
2885 | else |
2886 | d->rel.idx = 0; | |
23bc299b | 2887 | |
d4730f92 | 2888 | if (d->rela.hdr) |
2b0f7ef9 | 2889 | { |
d4730f92 BS |
2890 | d->rela.idx = section_number++; |
2891 | _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rela.hdr->sh_name); | |
2b0f7ef9 | 2892 | } |
23bc299b | 2893 | else |
d4730f92 | 2894 | d->rela.idx = 0; |
252b5132 RH |
2895 | } |
2896 | ||
2897 | t->shstrtab_section = section_number++; | |
2b0f7ef9 | 2898 | _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->shstrtab_hdr.sh_name); |
252b5132 | 2899 | elf_elfheader (abfd)->e_shstrndx = t->shstrtab_section; |
252b5132 | 2900 | |
3516e984 L |
2901 | need_symtab = (bfd_get_symcount (abfd) > 0 |
2902 | || (link_info == NULL | |
2903 | && ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC)) | |
2904 | == HAS_RELOC))); | |
2905 | if (need_symtab) | |
252b5132 RH |
2906 | { |
2907 | t->symtab_section = section_number++; | |
2b0f7ef9 | 2908 | _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->symtab_hdr.sh_name); |
4fbb74a6 | 2909 | if (section_number > ((SHN_LORESERVE - 2) & 0xFFFF)) |
9ad5cbcf | 2910 | { |
9ad5cbcf AM |
2911 | t->symtab_shndx_section = section_number++; |
2912 | t->symtab_shndx_hdr.sh_name | |
2913 | = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), | |
b34976b6 | 2914 | ".symtab_shndx", FALSE); |
9ad5cbcf | 2915 | if (t->symtab_shndx_hdr.sh_name == (unsigned int) -1) |
b34976b6 | 2916 | return FALSE; |
9ad5cbcf | 2917 | } |
252b5132 | 2918 | t->strtab_section = section_number++; |
2b0f7ef9 | 2919 | _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->strtab_hdr.sh_name); |
252b5132 RH |
2920 | } |
2921 | ||
2b0f7ef9 JJ |
2922 | _bfd_elf_strtab_finalize (elf_shstrtab (abfd)); |
2923 | t->shstrtab_hdr.sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd)); | |
9ad5cbcf AM |
2924 | |
2925 | elf_numsections (abfd) = section_number; | |
252b5132 RH |
2926 | elf_elfheader (abfd)->e_shnum = section_number; |
2927 | ||
2928 | /* Set up the list of section header pointers, in agreement with the | |
2929 | indices. */ | |
a50b1753 NC |
2930 | i_shdrp = (Elf_Internal_Shdr **) bfd_zalloc2 (abfd, section_number, |
2931 | sizeof (Elf_Internal_Shdr *)); | |
252b5132 | 2932 | if (i_shdrp == NULL) |
b34976b6 | 2933 | return FALSE; |
252b5132 | 2934 | |
a50b1753 NC |
2935 | i_shdrp[0] = (Elf_Internal_Shdr *) bfd_zalloc (abfd, |
2936 | sizeof (Elf_Internal_Shdr)); | |
252b5132 RH |
2937 | if (i_shdrp[0] == NULL) |
2938 | { | |
2939 | bfd_release (abfd, i_shdrp); | |
b34976b6 | 2940 | return FALSE; |
252b5132 | 2941 | } |
252b5132 RH |
2942 | |
2943 | elf_elfsections (abfd) = i_shdrp; | |
2944 | ||
2945 | i_shdrp[t->shstrtab_section] = &t->shstrtab_hdr; | |
3516e984 | 2946 | if (need_symtab) |
252b5132 RH |
2947 | { |
2948 | i_shdrp[t->symtab_section] = &t->symtab_hdr; | |
4fbb74a6 | 2949 | if (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF)) |
9ad5cbcf AM |
2950 | { |
2951 | i_shdrp[t->symtab_shndx_section] = &t->symtab_shndx_hdr; | |
2952 | t->symtab_shndx_hdr.sh_link = t->symtab_section; | |
2953 | } | |
252b5132 RH |
2954 | i_shdrp[t->strtab_section] = &t->strtab_hdr; |
2955 | t->symtab_hdr.sh_link = t->strtab_section; | |
2956 | } | |
38ce5b11 | 2957 | |
252b5132 RH |
2958 | for (sec = abfd->sections; sec; sec = sec->next) |
2959 | { | |
252b5132 RH |
2960 | asection *s; |
2961 | const char *name; | |
2962 | ||
91d6fa6a NC |
2963 | d = elf_section_data (sec); |
2964 | ||
252b5132 | 2965 | i_shdrp[d->this_idx] = &d->this_hdr; |
d4730f92 BS |
2966 | if (d->rel.idx != 0) |
2967 | i_shdrp[d->rel.idx] = d->rel.hdr; | |
2968 | if (d->rela.idx != 0) | |
2969 | i_shdrp[d->rela.idx] = d->rela.hdr; | |
252b5132 RH |
2970 | |
2971 | /* Fill in the sh_link and sh_info fields while we're at it. */ | |
2972 | ||
2973 | /* sh_link of a reloc section is the section index of the symbol | |
2974 | table. sh_info is the section index of the section to which | |
2975 | the relocation entries apply. */ | |
d4730f92 | 2976 | if (d->rel.idx != 0) |
252b5132 | 2977 | { |
d4730f92 BS |
2978 | d->rel.hdr->sh_link = t->symtab_section; |
2979 | d->rel.hdr->sh_info = d->this_idx; | |
252b5132 | 2980 | } |
d4730f92 | 2981 | if (d->rela.idx != 0) |
23bc299b | 2982 | { |
d4730f92 BS |
2983 | d->rela.hdr->sh_link = t->symtab_section; |
2984 | d->rela.hdr->sh_info = d->this_idx; | |
23bc299b | 2985 | } |
252b5132 | 2986 | |
38ce5b11 L |
2987 | /* We need to set up sh_link for SHF_LINK_ORDER. */ |
2988 | if ((d->this_hdr.sh_flags & SHF_LINK_ORDER) != 0) | |
2989 | { | |
2990 | s = elf_linked_to_section (sec); | |
2991 | if (s) | |
38ce5b11 | 2992 | { |
f2876037 | 2993 | /* elf_linked_to_section points to the input section. */ |
ccd2ec6a | 2994 | if (link_info != NULL) |
38ce5b11 | 2995 | { |
f2876037 | 2996 | /* Check discarded linkonce section. */ |
ccd2ec6a | 2997 | if (elf_discarded_section (s)) |
38ce5b11 | 2998 | { |
ccd2ec6a L |
2999 | asection *kept; |
3000 | (*_bfd_error_handler) | |
3001 | (_("%B: sh_link of section `%A' points to discarded section `%A' of `%B'"), | |
3002 | abfd, d->this_hdr.bfd_section, | |
3003 | s, s->owner); | |
3004 | /* Point to the kept section if it has the same | |
3005 | size as the discarded one. */ | |
c0f00686 | 3006 | kept = _bfd_elf_check_kept_section (s, link_info); |
ccd2ec6a | 3007 | if (kept == NULL) |
185d09ad | 3008 | { |
ccd2ec6a L |
3009 | bfd_set_error (bfd_error_bad_value); |
3010 | return FALSE; | |
185d09ad | 3011 | } |
ccd2ec6a | 3012 | s = kept; |
38ce5b11 | 3013 | } |
e424ecc8 | 3014 | |
ccd2ec6a L |
3015 | s = s->output_section; |
3016 | BFD_ASSERT (s != NULL); | |
38ce5b11 | 3017 | } |
f2876037 L |
3018 | else |
3019 | { | |
3020 | /* Handle objcopy. */ | |
3021 | if (s->output_section == NULL) | |
3022 | { | |
3023 | (*_bfd_error_handler) | |
3024 | (_("%B: sh_link of section `%A' points to removed section `%A' of `%B'"), | |
3025 | abfd, d->this_hdr.bfd_section, s, s->owner); | |
3026 | bfd_set_error (bfd_error_bad_value); | |
3027 | return FALSE; | |
3028 | } | |
3029 | s = s->output_section; | |
3030 | } | |
ccd2ec6a L |
3031 | d->this_hdr.sh_link = elf_section_data (s)->this_idx; |
3032 | } | |
3033 | else | |
3034 | { | |
3035 | /* PR 290: | |
3036 | The Intel C compiler generates SHT_IA_64_UNWIND with | |
3037 | SHF_LINK_ORDER. But it doesn't set the sh_link or | |
3038 | sh_info fields. Hence we could get the situation | |
08a40648 | 3039 | where s is NULL. */ |
ccd2ec6a L |
3040 | const struct elf_backend_data *bed |
3041 | = get_elf_backend_data (abfd); | |
3042 | if (bed->link_order_error_handler) | |
3043 | bed->link_order_error_handler | |
3044 | (_("%B: warning: sh_link not set for section `%A'"), | |
3045 | abfd, sec); | |
38ce5b11 L |
3046 | } |
3047 | } | |
3048 | ||
252b5132 RH |
3049 | switch (d->this_hdr.sh_type) |
3050 | { | |
3051 | case SHT_REL: | |
3052 | case SHT_RELA: | |
3053 | /* A reloc section which we are treating as a normal BFD | |
3054 | section. sh_link is the section index of the symbol | |
3055 | table. sh_info is the section index of the section to | |
3056 | which the relocation entries apply. We assume that an | |
3057 | allocated reloc section uses the dynamic symbol table. | |
3058 | FIXME: How can we be sure? */ | |
3059 | s = bfd_get_section_by_name (abfd, ".dynsym"); | |
3060 | if (s != NULL) | |
3061 | d->this_hdr.sh_link = elf_section_data (s)->this_idx; | |
3062 | ||
3063 | /* We look up the section the relocs apply to by name. */ | |
3064 | name = sec->name; | |
3065 | if (d->this_hdr.sh_type == SHT_REL) | |
3066 | name += 4; | |
3067 | else | |
3068 | name += 5; | |
3069 | s = bfd_get_section_by_name (abfd, name); | |
3070 | if (s != NULL) | |
3071 | d->this_hdr.sh_info = elf_section_data (s)->this_idx; | |
3072 | break; | |
3073 | ||
3074 | case SHT_STRTAB: | |
3075 | /* We assume that a section named .stab*str is a stabs | |
3076 | string section. We look for a section with the same name | |
3077 | but without the trailing ``str'', and set its sh_link | |
3078 | field to point to this section. */ | |
0112cd26 | 3079 | if (CONST_STRNEQ (sec->name, ".stab") |
252b5132 RH |
3080 | && strcmp (sec->name + strlen (sec->name) - 3, "str") == 0) |
3081 | { | |
3082 | size_t len; | |
3083 | char *alc; | |
3084 | ||
3085 | len = strlen (sec->name); | |
a50b1753 | 3086 | alc = (char *) bfd_malloc (len - 2); |
252b5132 | 3087 | if (alc == NULL) |
b34976b6 | 3088 | return FALSE; |
d4c88bbb | 3089 | memcpy (alc, sec->name, len - 3); |
252b5132 RH |
3090 | alc[len - 3] = '\0'; |
3091 | s = bfd_get_section_by_name (abfd, alc); | |
3092 | free (alc); | |
3093 | if (s != NULL) | |
3094 | { | |
3095 | elf_section_data (s)->this_hdr.sh_link = d->this_idx; | |
3096 | ||
3097 | /* This is a .stab section. */ | |
0594c12d AM |
3098 | if (elf_section_data (s)->this_hdr.sh_entsize == 0) |
3099 | elf_section_data (s)->this_hdr.sh_entsize | |
3100 | = 4 + 2 * bfd_get_arch_size (abfd) / 8; | |
252b5132 RH |
3101 | } |
3102 | } | |
3103 | break; | |
3104 | ||
3105 | case SHT_DYNAMIC: | |
3106 | case SHT_DYNSYM: | |
3107 | case SHT_GNU_verneed: | |
3108 | case SHT_GNU_verdef: | |
3109 | /* sh_link is the section header index of the string table | |
3110 | used for the dynamic entries, or the symbol table, or the | |
3111 | version strings. */ | |
3112 | s = bfd_get_section_by_name (abfd, ".dynstr"); | |
3113 | if (s != NULL) | |
3114 | d->this_hdr.sh_link = elf_section_data (s)->this_idx; | |
3115 | break; | |
3116 | ||
7f1204bb JJ |
3117 | case SHT_GNU_LIBLIST: |
3118 | /* sh_link is the section header index of the prelink library | |
08a40648 AM |
3119 | list used for the dynamic entries, or the symbol table, or |
3120 | the version strings. */ | |
7f1204bb JJ |
3121 | s = bfd_get_section_by_name (abfd, (sec->flags & SEC_ALLOC) |
3122 | ? ".dynstr" : ".gnu.libstr"); | |
3123 | if (s != NULL) | |
3124 | d->this_hdr.sh_link = elf_section_data (s)->this_idx; | |
3125 | break; | |
3126 | ||
252b5132 | 3127 | case SHT_HASH: |
fdc90cb4 | 3128 | case SHT_GNU_HASH: |
252b5132 RH |
3129 | case SHT_GNU_versym: |
3130 | /* sh_link is the section header index of the symbol table | |
3131 | this hash table or version table is for. */ | |
3132 | s = bfd_get_section_by_name (abfd, ".dynsym"); | |
3133 | if (s != NULL) | |
3134 | d->this_hdr.sh_link = elf_section_data (s)->this_idx; | |
3135 | break; | |
dbb410c3 AM |
3136 | |
3137 | case SHT_GROUP: | |
3138 | d->this_hdr.sh_link = t->symtab_section; | |
252b5132 RH |
3139 | } |
3140 | } | |
3141 | ||
2b0f7ef9 | 3142 | for (secn = 1; secn < section_number; ++secn) |
9ad5cbcf AM |
3143 | if (i_shdrp[secn] == NULL) |
3144 | i_shdrp[secn] = i_shdrp[0]; | |
3145 | else | |
3146 | i_shdrp[secn]->sh_name = _bfd_elf_strtab_offset (elf_shstrtab (abfd), | |
3147 | i_shdrp[secn]->sh_name); | |
b34976b6 | 3148 | return TRUE; |
252b5132 RH |
3149 | } |
3150 | ||
3151 | /* Map symbol from it's internal number to the external number, moving | |
3152 | all local symbols to be at the head of the list. */ | |
3153 | ||
5372391b | 3154 | static bfd_boolean |
217aa764 | 3155 | sym_is_global (bfd *abfd, asymbol *sym) |
252b5132 RH |
3156 | { |
3157 | /* If the backend has a special mapping, use it. */ | |
9c5bfbb7 | 3158 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
217aa764 AM |
3159 | if (bed->elf_backend_sym_is_global) |
3160 | return (*bed->elf_backend_sym_is_global) (abfd, sym); | |
252b5132 | 3161 | |
e47bf690 | 3162 | return ((sym->flags & (BSF_GLOBAL | BSF_WEAK | BSF_GNU_UNIQUE)) != 0 |
252b5132 RH |
3163 | || bfd_is_und_section (bfd_get_section (sym)) |
3164 | || bfd_is_com_section (bfd_get_section (sym))); | |
3165 | } | |
3166 | ||
5372391b | 3167 | /* Don't output section symbols for sections that are not going to be |
0f0a5e58 | 3168 | output. */ |
5372391b AM |
3169 | |
3170 | static bfd_boolean | |
3171 | ignore_section_sym (bfd *abfd, asymbol *sym) | |
3172 | { | |
3173 | return ((sym->flags & BSF_SECTION_SYM) != 0 | |
0f0a5e58 AM |
3174 | && !(sym->section->owner == abfd |
3175 | || (sym->section->output_section->owner == abfd | |
3176 | && sym->section->output_offset == 0))); | |
5372391b AM |
3177 | } |
3178 | ||
b34976b6 | 3179 | static bfd_boolean |
217aa764 | 3180 | elf_map_symbols (bfd *abfd) |
252b5132 | 3181 | { |
dc810e39 | 3182 | unsigned int symcount = bfd_get_symcount (abfd); |
252b5132 RH |
3183 | asymbol **syms = bfd_get_outsymbols (abfd); |
3184 | asymbol **sect_syms; | |
dc810e39 AM |
3185 | unsigned int num_locals = 0; |
3186 | unsigned int num_globals = 0; | |
3187 | unsigned int num_locals2 = 0; | |
3188 | unsigned int num_globals2 = 0; | |
252b5132 | 3189 | int max_index = 0; |
dc810e39 | 3190 | unsigned int idx; |
252b5132 RH |
3191 | asection *asect; |
3192 | asymbol **new_syms; | |
252b5132 RH |
3193 | |
3194 | #ifdef DEBUG | |
3195 | fprintf (stderr, "elf_map_symbols\n"); | |
3196 | fflush (stderr); | |
3197 | #endif | |
3198 | ||
252b5132 RH |
3199 | for (asect = abfd->sections; asect; asect = asect->next) |
3200 | { | |
3201 | if (max_index < asect->index) | |
3202 | max_index = asect->index; | |
3203 | } | |
3204 | ||
3205 | max_index++; | |
a50b1753 | 3206 | sect_syms = (asymbol **) bfd_zalloc2 (abfd, max_index, sizeof (asymbol *)); |
252b5132 | 3207 | if (sect_syms == NULL) |
b34976b6 | 3208 | return FALSE; |
252b5132 | 3209 | elf_section_syms (abfd) = sect_syms; |
4e89ac30 | 3210 | elf_num_section_syms (abfd) = max_index; |
252b5132 | 3211 | |
079e9a2f AM |
3212 | /* Init sect_syms entries for any section symbols we have already |
3213 | decided to output. */ | |
252b5132 RH |
3214 | for (idx = 0; idx < symcount; idx++) |
3215 | { | |
dc810e39 | 3216 | asymbol *sym = syms[idx]; |
c044fabd | 3217 | |
252b5132 | 3218 | if ((sym->flags & BSF_SECTION_SYM) != 0 |
0f0a5e58 | 3219 | && sym->value == 0 |
5372391b | 3220 | && !ignore_section_sym (abfd, sym)) |
252b5132 | 3221 | { |
5372391b | 3222 | asection *sec = sym->section; |
252b5132 | 3223 | |
5372391b AM |
3224 | if (sec->owner != abfd) |
3225 | sec = sec->output_section; | |
252b5132 | 3226 | |
5372391b | 3227 | sect_syms[sec->index] = syms[idx]; |
252b5132 RH |
3228 | } |
3229 | } | |
3230 | ||
252b5132 RH |
3231 | /* Classify all of the symbols. */ |
3232 | for (idx = 0; idx < symcount; idx++) | |
3233 | { | |
5372391b AM |
3234 | if (ignore_section_sym (abfd, syms[idx])) |
3235 | continue; | |
252b5132 RH |
3236 | if (!sym_is_global (abfd, syms[idx])) |
3237 | num_locals++; | |
3238 | else | |
3239 | num_globals++; | |
3240 | } | |
079e9a2f | 3241 | |
5372391b | 3242 | /* We will be adding a section symbol for each normal BFD section. Most |
079e9a2f AM |
3243 | sections will already have a section symbol in outsymbols, but |
3244 | eg. SHT_GROUP sections will not, and we need the section symbol mapped | |
3245 | at least in that case. */ | |
252b5132 RH |
3246 | for (asect = abfd->sections; asect; asect = asect->next) |
3247 | { | |
079e9a2f | 3248 | if (sect_syms[asect->index] == NULL) |
252b5132 | 3249 | { |
079e9a2f | 3250 | if (!sym_is_global (abfd, asect->symbol)) |
252b5132 RH |
3251 | num_locals++; |
3252 | else | |
3253 | num_globals++; | |
252b5132 RH |
3254 | } |
3255 | } | |
3256 | ||
3257 | /* Now sort the symbols so the local symbols are first. */ | |
a50b1753 NC |
3258 | new_syms = (asymbol **) bfd_alloc2 (abfd, num_locals + num_globals, |
3259 | sizeof (asymbol *)); | |
dc810e39 | 3260 | |
252b5132 | 3261 | if (new_syms == NULL) |
b34976b6 | 3262 | return FALSE; |
252b5132 RH |
3263 | |
3264 | for (idx = 0; idx < symcount; idx++) | |
3265 | { | |
3266 | asymbol *sym = syms[idx]; | |
dc810e39 | 3267 | unsigned int i; |
252b5132 | 3268 | |
5372391b AM |
3269 | if (ignore_section_sym (abfd, sym)) |
3270 | continue; | |
252b5132 RH |
3271 | if (!sym_is_global (abfd, sym)) |
3272 | i = num_locals2++; | |
3273 | else | |
3274 | i = num_locals + num_globals2++; | |
3275 | new_syms[i] = sym; | |
3276 | sym->udata.i = i + 1; | |
3277 | } | |
3278 | for (asect = abfd->sections; asect; asect = asect->next) | |
3279 | { | |
079e9a2f | 3280 | if (sect_syms[asect->index] == NULL) |
252b5132 | 3281 | { |
079e9a2f | 3282 | asymbol *sym = asect->symbol; |
dc810e39 | 3283 | unsigned int i; |
252b5132 | 3284 | |
079e9a2f | 3285 | sect_syms[asect->index] = sym; |
252b5132 RH |
3286 | if (!sym_is_global (abfd, sym)) |
3287 | i = num_locals2++; | |
3288 | else | |
3289 | i = num_locals + num_globals2++; | |
3290 | new_syms[i] = sym; | |
3291 | sym->udata.i = i + 1; | |
3292 | } | |
3293 | } | |
3294 | ||
3295 | bfd_set_symtab (abfd, new_syms, num_locals + num_globals); | |
3296 | ||
3297 | elf_num_locals (abfd) = num_locals; | |
3298 | elf_num_globals (abfd) = num_globals; | |
b34976b6 | 3299 | return TRUE; |
252b5132 RH |
3300 | } |
3301 | ||
3302 | /* Align to the maximum file alignment that could be required for any | |
3303 | ELF data structure. */ | |
3304 | ||
268b6b39 | 3305 | static inline file_ptr |
217aa764 | 3306 | align_file_position (file_ptr off, int align) |
252b5132 RH |
3307 | { |
3308 | return (off + align - 1) & ~(align - 1); | |
3309 | } | |
3310 | ||
3311 | /* Assign a file position to a section, optionally aligning to the | |
3312 | required section alignment. */ | |
3313 | ||
217aa764 AM |
3314 | file_ptr |
3315 | _bfd_elf_assign_file_position_for_section (Elf_Internal_Shdr *i_shdrp, | |
3316 | file_ptr offset, | |
3317 | bfd_boolean align) | |
252b5132 | 3318 | { |
72de5009 AM |
3319 | if (align && i_shdrp->sh_addralign > 1) |
3320 | offset = BFD_ALIGN (offset, i_shdrp->sh_addralign); | |
252b5132 RH |
3321 | i_shdrp->sh_offset = offset; |
3322 | if (i_shdrp->bfd_section != NULL) | |
3323 | i_shdrp->bfd_section->filepos = offset; | |
3324 | if (i_shdrp->sh_type != SHT_NOBITS) | |
3325 | offset += i_shdrp->sh_size; | |
3326 | return offset; | |
3327 | } | |
3328 | ||
3329 | /* Compute the file positions we are going to put the sections at, and | |
3330 | otherwise prepare to begin writing out the ELF file. If LINK_INFO | |
3331 | is not NULL, this is being called by the ELF backend linker. */ | |
3332 | ||
b34976b6 | 3333 | bfd_boolean |
217aa764 AM |
3334 | _bfd_elf_compute_section_file_positions (bfd *abfd, |
3335 | struct bfd_link_info *link_info) | |
252b5132 | 3336 | { |
9c5bfbb7 | 3337 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
d4730f92 | 3338 | struct fake_section_arg fsargs; |
b34976b6 | 3339 | bfd_boolean failed; |
4b6c0f2f | 3340 | struct bfd_strtab_hash *strtab = NULL; |
252b5132 | 3341 | Elf_Internal_Shdr *shstrtab_hdr; |
3516e984 | 3342 | bfd_boolean need_symtab; |
252b5132 RH |
3343 | |
3344 | if (abfd->output_has_begun) | |
b34976b6 | 3345 | return TRUE; |
252b5132 RH |
3346 | |
3347 | /* Do any elf backend specific processing first. */ | |
3348 | if (bed->elf_backend_begin_write_processing) | |
3349 | (*bed->elf_backend_begin_write_processing) (abfd, link_info); | |
3350 | ||
3351 | if (! prep_headers (abfd)) | |
b34976b6 | 3352 | return FALSE; |
252b5132 | 3353 | |
e6c51ed4 NC |
3354 | /* Post process the headers if necessary. */ |
3355 | if (bed->elf_backend_post_process_headers) | |
3356 | (*bed->elf_backend_post_process_headers) (abfd, link_info); | |
3357 | ||
d4730f92 BS |
3358 | fsargs.failed = FALSE; |
3359 | fsargs.link_info = link_info; | |
3360 | bfd_map_over_sections (abfd, elf_fake_sections, &fsargs); | |
3361 | if (fsargs.failed) | |
b34976b6 | 3362 | return FALSE; |
252b5132 | 3363 | |
da9f89d4 | 3364 | if (!assign_section_numbers (abfd, link_info)) |
b34976b6 | 3365 | return FALSE; |
252b5132 RH |
3366 | |
3367 | /* The backend linker builds symbol table information itself. */ | |
3516e984 L |
3368 | need_symtab = (link_info == NULL |
3369 | && (bfd_get_symcount (abfd) > 0 | |
3370 | || ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC)) | |
3371 | == HAS_RELOC))); | |
3372 | if (need_symtab) | |
252b5132 RH |
3373 | { |
3374 | /* Non-zero if doing a relocatable link. */ | |
3375 | int relocatable_p = ! (abfd->flags & (EXEC_P | DYNAMIC)); | |
3376 | ||
3377 | if (! swap_out_syms (abfd, &strtab, relocatable_p)) | |
b34976b6 | 3378 | return FALSE; |
252b5132 RH |
3379 | } |
3380 | ||
d4730f92 | 3381 | failed = FALSE; |
1126897b | 3382 | if (link_info == NULL) |
dbb410c3 | 3383 | { |
1126897b | 3384 | bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed); |
dbb410c3 | 3385 | if (failed) |
b34976b6 | 3386 | return FALSE; |
dbb410c3 AM |
3387 | } |
3388 | ||
252b5132 RH |
3389 | shstrtab_hdr = &elf_tdata (abfd)->shstrtab_hdr; |
3390 | /* sh_name was set in prep_headers. */ | |
3391 | shstrtab_hdr->sh_type = SHT_STRTAB; | |
3392 | shstrtab_hdr->sh_flags = 0; | |
3393 | shstrtab_hdr->sh_addr = 0; | |
2b0f7ef9 | 3394 | shstrtab_hdr->sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd)); |
252b5132 RH |
3395 | shstrtab_hdr->sh_entsize = 0; |
3396 | shstrtab_hdr->sh_link = 0; | |
3397 | shstrtab_hdr->sh_info = 0; | |
3398 | /* sh_offset is set in assign_file_positions_except_relocs. */ | |
3399 | shstrtab_hdr->sh_addralign = 1; | |
3400 | ||
c84fca4d | 3401 | if (!assign_file_positions_except_relocs (abfd, link_info)) |
b34976b6 | 3402 | return FALSE; |
252b5132 | 3403 | |
3516e984 | 3404 | if (need_symtab) |
252b5132 RH |
3405 | { |
3406 | file_ptr off; | |
3407 | Elf_Internal_Shdr *hdr; | |
3408 | ||
3409 | off = elf_tdata (abfd)->next_file_pos; | |
3410 | ||
3411 | hdr = &elf_tdata (abfd)->symtab_hdr; | |
b34976b6 | 3412 | off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE); |
252b5132 | 3413 | |
9ad5cbcf AM |
3414 | hdr = &elf_tdata (abfd)->symtab_shndx_hdr; |
3415 | if (hdr->sh_size != 0) | |
b34976b6 | 3416 | off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE); |
9ad5cbcf | 3417 | |
252b5132 | 3418 | hdr = &elf_tdata (abfd)->strtab_hdr; |
b34976b6 | 3419 | off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE); |
252b5132 RH |
3420 | |
3421 | elf_tdata (abfd)->next_file_pos = off; | |
3422 | ||
3423 | /* Now that we know where the .strtab section goes, write it | |
08a40648 | 3424 | out. */ |
252b5132 RH |
3425 | if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0 |
3426 | || ! _bfd_stringtab_emit (abfd, strtab)) | |
b34976b6 | 3427 | return FALSE; |
252b5132 RH |
3428 | _bfd_stringtab_free (strtab); |
3429 | } | |
3430 | ||
b34976b6 | 3431 | abfd->output_has_begun = TRUE; |
252b5132 | 3432 | |
b34976b6 | 3433 | return TRUE; |
252b5132 RH |
3434 | } |
3435 | ||
8ded5a0f AM |
3436 | /* Make an initial estimate of the size of the program header. If we |
3437 | get the number wrong here, we'll redo section placement. */ | |
3438 | ||
3439 | static bfd_size_type | |
3440 | get_program_header_size (bfd *abfd, struct bfd_link_info *info) | |
3441 | { | |
3442 | size_t segs; | |
3443 | asection *s; | |
2b05f1b7 | 3444 | const struct elf_backend_data *bed; |
8ded5a0f AM |
3445 | |
3446 | /* Assume we will need exactly two PT_LOAD segments: one for text | |
3447 | and one for data. */ | |
3448 | segs = 2; | |
3449 | ||
3450 | s = bfd_get_section_by_name (abfd, ".interp"); | |
3451 | if (s != NULL && (s->flags & SEC_LOAD) != 0) | |
3452 | { | |
3453 | /* If we have a loadable interpreter section, we need a | |
3454 | PT_INTERP segment. In this case, assume we also need a | |
3455 | PT_PHDR segment, although that may not be true for all | |
3456 | targets. */ | |
3457 | segs += 2; | |
3458 | } | |
3459 | ||
3460 | if (bfd_get_section_by_name (abfd, ".dynamic") != NULL) | |
3461 | { | |
3462 | /* We need a PT_DYNAMIC segment. */ | |
3463 | ++segs; | |
f210dcff | 3464 | } |
08a40648 | 3465 | |
ceae84aa | 3466 | if (info != NULL && info->relro) |
f210dcff L |
3467 | { |
3468 | /* We need a PT_GNU_RELRO segment. */ | |
3469 | ++segs; | |
8ded5a0f AM |
3470 | } |
3471 | ||
3472 | if (elf_tdata (abfd)->eh_frame_hdr) | |
3473 | { | |
3474 | /* We need a PT_GNU_EH_FRAME segment. */ | |
3475 | ++segs; | |
3476 | } | |
3477 | ||
2b05f1b7 | 3478 | if (elf_tdata (abfd)->stack_flags) |
8ded5a0f | 3479 | { |
2b05f1b7 L |
3480 | /* We need a PT_GNU_STACK segment. */ |
3481 | ++segs; | |
3482 | } | |
94b11780 | 3483 | |
2b05f1b7 L |
3484 | for (s = abfd->sections; s != NULL; s = s->next) |
3485 | { | |
8ded5a0f | 3486 | if ((s->flags & SEC_LOAD) != 0 |
0112cd26 | 3487 | && CONST_STRNEQ (s->name, ".note")) |
8ded5a0f AM |
3488 | { |
3489 | /* We need a PT_NOTE segment. */ | |
3490 | ++segs; | |
1c5265b5 JJ |
3491 | /* Try to create just one PT_NOTE segment |
3492 | for all adjacent loadable .note* sections. | |
3493 | gABI requires that within a PT_NOTE segment | |
3494 | (and also inside of each SHT_NOTE section) | |
3495 | each note is padded to a multiple of 4 size, | |
3496 | so we check whether the sections are correctly | |
3497 | aligned. */ | |
3498 | if (s->alignment_power == 2) | |
3499 | while (s->next != NULL | |
3500 | && s->next->alignment_power == 2 | |
3501 | && (s->next->flags & SEC_LOAD) != 0 | |
3502 | && CONST_STRNEQ (s->next->name, ".note")) | |
3503 | s = s->next; | |
8ded5a0f AM |
3504 | } |
3505 | } | |
3506 | ||
3507 | for (s = abfd->sections; s != NULL; s = s->next) | |
3508 | { | |
3509 | if (s->flags & SEC_THREAD_LOCAL) | |
3510 | { | |
3511 | /* We need a PT_TLS segment. */ | |
3512 | ++segs; | |
3513 | break; | |
3514 | } | |
3515 | } | |
3516 | ||
3517 | /* Let the backend count up any program headers it might need. */ | |
2b05f1b7 | 3518 | bed = get_elf_backend_data (abfd); |
8ded5a0f AM |
3519 | if (bed->elf_backend_additional_program_headers) |
3520 | { | |
3521 | int a; | |
3522 | ||
3523 | a = (*bed->elf_backend_additional_program_headers) (abfd, info); | |
3524 | if (a == -1) | |
3525 | abort (); | |
3526 | segs += a; | |
3527 | } | |
3528 | ||
3529 | return segs * bed->s->sizeof_phdr; | |
3530 | } | |
3531 | ||
2ea37f1c NC |
3532 | /* Find the segment that contains the output_section of section. */ |
3533 | ||
3534 | Elf_Internal_Phdr * | |
3535 | _bfd_elf_find_segment_containing_section (bfd * abfd, asection * section) | |
3536 | { | |
3537 | struct elf_segment_map *m; | |
3538 | Elf_Internal_Phdr *p; | |
3539 | ||
3540 | for (m = elf_tdata (abfd)->segment_map, | |
3541 | p = elf_tdata (abfd)->phdr; | |
3542 | m != NULL; | |
3543 | m = m->next, p++) | |
3544 | { | |
3545 | int i; | |
3546 | ||
3547 | for (i = m->count - 1; i >= 0; i--) | |
3548 | if (m->sections[i] == section) | |
3549 | return p; | |
3550 | } | |
3551 | ||
3552 | return NULL; | |
3553 | } | |
3554 | ||
252b5132 RH |
3555 | /* Create a mapping from a set of sections to a program segment. */ |
3556 | ||
217aa764 AM |
3557 | static struct elf_segment_map * |
3558 | make_mapping (bfd *abfd, | |
3559 | asection **sections, | |
3560 | unsigned int from, | |
3561 | unsigned int to, | |
3562 | bfd_boolean phdr) | |
252b5132 RH |
3563 | { |
3564 | struct elf_segment_map *m; | |
3565 | unsigned int i; | |
3566 | asection **hdrpp; | |
dc810e39 | 3567 | bfd_size_type amt; |
252b5132 | 3568 | |
dc810e39 AM |
3569 | amt = sizeof (struct elf_segment_map); |
3570 | amt += (to - from - 1) * sizeof (asection *); | |
a50b1753 | 3571 | m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); |
252b5132 RH |
3572 | if (m == NULL) |
3573 | return NULL; | |
3574 | m->next = NULL; | |
3575 | m->p_type = PT_LOAD; | |
3576 | for (i = from, hdrpp = sections + from; i < to; i++, hdrpp++) | |
3577 | m->sections[i - from] = *hdrpp; | |
3578 | m->count = to - from; | |
3579 | ||
3580 | if (from == 0 && phdr) | |
3581 | { | |
3582 | /* Include the headers in the first PT_LOAD segment. */ | |
3583 | m->includes_filehdr = 1; | |
3584 | m->includes_phdrs = 1; | |
3585 | } | |
3586 | ||
3587 | return m; | |
3588 | } | |
3589 | ||
229fcec5 MM |
3590 | /* Create the PT_DYNAMIC segment, which includes DYNSEC. Returns NULL |
3591 | on failure. */ | |
3592 | ||
3593 | struct elf_segment_map * | |
3594 | _bfd_elf_make_dynamic_segment (bfd *abfd, asection *dynsec) | |
3595 | { | |
3596 | struct elf_segment_map *m; | |
3597 | ||
a50b1753 NC |
3598 | m = (struct elf_segment_map *) bfd_zalloc (abfd, |
3599 | sizeof (struct elf_segment_map)); | |
229fcec5 MM |
3600 | if (m == NULL) |
3601 | return NULL; | |
3602 | m->next = NULL; | |
3603 | m->p_type = PT_DYNAMIC; | |
3604 | m->count = 1; | |
3605 | m->sections[0] = dynsec; | |
08a40648 | 3606 | |
229fcec5 MM |
3607 | return m; |
3608 | } | |
3609 | ||
8ded5a0f | 3610 | /* Possibly add or remove segments from the segment map. */ |
252b5132 | 3611 | |
b34976b6 | 3612 | static bfd_boolean |
3dea8fca AM |
3613 | elf_modify_segment_map (bfd *abfd, |
3614 | struct bfd_link_info *info, | |
3615 | bfd_boolean remove_empty_load) | |
252b5132 | 3616 | { |
252e386e | 3617 | struct elf_segment_map **m; |
8ded5a0f | 3618 | const struct elf_backend_data *bed; |
252b5132 | 3619 | |
8ded5a0f AM |
3620 | /* The placement algorithm assumes that non allocated sections are |
3621 | not in PT_LOAD segments. We ensure this here by removing such | |
3622 | sections from the segment map. We also remove excluded | |
252e386e AM |
3623 | sections. Finally, any PT_LOAD segment without sections is |
3624 | removed. */ | |
3625 | m = &elf_tdata (abfd)->segment_map; | |
3626 | while (*m) | |
8ded5a0f AM |
3627 | { |
3628 | unsigned int i, new_count; | |
252b5132 | 3629 | |
252e386e | 3630 | for (new_count = 0, i = 0; i < (*m)->count; i++) |
8ded5a0f | 3631 | { |
252e386e AM |
3632 | if (((*m)->sections[i]->flags & SEC_EXCLUDE) == 0 |
3633 | && (((*m)->sections[i]->flags & SEC_ALLOC) != 0 | |
3634 | || (*m)->p_type != PT_LOAD)) | |
8ded5a0f | 3635 | { |
252e386e AM |
3636 | (*m)->sections[new_count] = (*m)->sections[i]; |
3637 | new_count++; | |
8ded5a0f AM |
3638 | } |
3639 | } | |
252e386e | 3640 | (*m)->count = new_count; |
252b5132 | 3641 | |
3dea8fca | 3642 | if (remove_empty_load && (*m)->p_type == PT_LOAD && (*m)->count == 0) |
252e386e AM |
3643 | *m = (*m)->next; |
3644 | else | |
3645 | m = &(*m)->next; | |
8ded5a0f | 3646 | } |
252b5132 | 3647 | |
8ded5a0f AM |
3648 | bed = get_elf_backend_data (abfd); |
3649 | if (bed->elf_backend_modify_segment_map != NULL) | |
252b5132 | 3650 | { |
252e386e | 3651 | if (!(*bed->elf_backend_modify_segment_map) (abfd, info)) |
8ded5a0f | 3652 | return FALSE; |
252b5132 | 3653 | } |
252b5132 | 3654 | |
8ded5a0f AM |
3655 | return TRUE; |
3656 | } | |
252b5132 | 3657 | |
8ded5a0f | 3658 | /* Set up a mapping from BFD sections to program segments. */ |
252b5132 | 3659 | |
8ded5a0f AM |
3660 | bfd_boolean |
3661 | _bfd_elf_map_sections_to_segments (bfd *abfd, struct bfd_link_info *info) | |
3662 | { | |
3663 | unsigned int count; | |
3664 | struct elf_segment_map *m; | |
3665 | asection **sections = NULL; | |
3666 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
3dea8fca | 3667 | bfd_boolean no_user_phdrs; |
252b5132 | 3668 | |
3dea8fca AM |
3669 | no_user_phdrs = elf_tdata (abfd)->segment_map == NULL; |
3670 | if (no_user_phdrs && bfd_count_sections (abfd) != 0) | |
252b5132 | 3671 | { |
8ded5a0f AM |
3672 | asection *s; |
3673 | unsigned int i; | |
3674 | struct elf_segment_map *mfirst; | |
3675 | struct elf_segment_map **pm; | |
3676 | asection *last_hdr; | |
3677 | bfd_vma last_size; | |
3678 | unsigned int phdr_index; | |
3679 | bfd_vma maxpagesize; | |
3680 | asection **hdrpp; | |
3681 | bfd_boolean phdr_in_segment = TRUE; | |
3682 | bfd_boolean writable; | |
3683 | int tls_count = 0; | |
3684 | asection *first_tls = NULL; | |
3685 | asection *dynsec, *eh_frame_hdr; | |
3686 | bfd_size_type amt; | |
8d06853e | 3687 | bfd_vma addr_mask, wrap_to = 0; |
252b5132 | 3688 | |
8ded5a0f | 3689 | /* Select the allocated sections, and sort them. */ |
252b5132 | 3690 | |
a50b1753 NC |
3691 | sections = (asection **) bfd_malloc2 (bfd_count_sections (abfd), |
3692 | sizeof (asection *)); | |
8ded5a0f | 3693 | if (sections == NULL) |
252b5132 | 3694 | goto error_return; |
252b5132 | 3695 | |
8d06853e AM |
3696 | /* Calculate top address, avoiding undefined behaviour of shift |
3697 | left operator when shift count is equal to size of type | |
3698 | being shifted. */ | |
3699 | addr_mask = ((bfd_vma) 1 << (bfd_arch_bits_per_address (abfd) - 1)) - 1; | |
3700 | addr_mask = (addr_mask << 1) + 1; | |
3701 | ||
8ded5a0f AM |
3702 | i = 0; |
3703 | for (s = abfd->sections; s != NULL; s = s->next) | |
3704 | { | |
3705 | if ((s->flags & SEC_ALLOC) != 0) | |
3706 | { | |
3707 | sections[i] = s; | |
3708 | ++i; | |
8d06853e AM |
3709 | /* A wrapping section potentially clashes with header. */ |
3710 | if (((s->lma + s->size) & addr_mask) < (s->lma & addr_mask)) | |
3711 | wrap_to = (s->lma + s->size) & addr_mask; | |
8ded5a0f AM |
3712 | } |
3713 | } | |
3714 | BFD_ASSERT (i <= bfd_count_sections (abfd)); | |
3715 | count = i; | |
252b5132 | 3716 | |
8ded5a0f | 3717 | qsort (sections, (size_t) count, sizeof (asection *), elf_sort_sections); |
252b5132 | 3718 | |
8ded5a0f | 3719 | /* Build the mapping. */ |
252b5132 | 3720 | |
8ded5a0f AM |
3721 | mfirst = NULL; |
3722 | pm = &mfirst; | |
252b5132 | 3723 | |
8ded5a0f AM |
3724 | /* If we have a .interp section, then create a PT_PHDR segment for |
3725 | the program headers and a PT_INTERP segment for the .interp | |
3726 | section. */ | |
3727 | s = bfd_get_section_by_name (abfd, ".interp"); | |
3728 | if (s != NULL && (s->flags & SEC_LOAD) != 0) | |
3729 | { | |
3730 | amt = sizeof (struct elf_segment_map); | |
a50b1753 | 3731 | m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); |
8ded5a0f AM |
3732 | if (m == NULL) |
3733 | goto error_return; | |
3734 | m->next = NULL; | |
3735 | m->p_type = PT_PHDR; | |
3736 | /* FIXME: UnixWare and Solaris set PF_X, Irix 5 does not. */ | |
3737 | m->p_flags = PF_R | PF_X; | |
3738 | m->p_flags_valid = 1; | |
3739 | m->includes_phdrs = 1; | |
252b5132 | 3740 | |
8ded5a0f AM |
3741 | *pm = m; |
3742 | pm = &m->next; | |
252b5132 | 3743 | |
8ded5a0f | 3744 | amt = sizeof (struct elf_segment_map); |
a50b1753 | 3745 | m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); |
8ded5a0f AM |
3746 | if (m == NULL) |
3747 | goto error_return; | |
3748 | m->next = NULL; | |
3749 | m->p_type = PT_INTERP; | |
3750 | m->count = 1; | |
3751 | m->sections[0] = s; | |
3752 | ||
3753 | *pm = m; | |
3754 | pm = &m->next; | |
252b5132 | 3755 | } |
8ded5a0f AM |
3756 | |
3757 | /* Look through the sections. We put sections in the same program | |
3758 | segment when the start of the second section can be placed within | |
3759 | a few bytes of the end of the first section. */ | |
3760 | last_hdr = NULL; | |
3761 | last_size = 0; | |
3762 | phdr_index = 0; | |
3763 | maxpagesize = bed->maxpagesize; | |
3764 | writable = FALSE; | |
3765 | dynsec = bfd_get_section_by_name (abfd, ".dynamic"); | |
3766 | if (dynsec != NULL | |
3767 | && (dynsec->flags & SEC_LOAD) == 0) | |
3768 | dynsec = NULL; | |
3769 | ||
3770 | /* Deal with -Ttext or something similar such that the first section | |
3771 | is not adjacent to the program headers. This is an | |
3772 | approximation, since at this point we don't know exactly how many | |
3773 | program headers we will need. */ | |
3774 | if (count > 0) | |
252b5132 | 3775 | { |
8ded5a0f AM |
3776 | bfd_size_type phdr_size = elf_tdata (abfd)->program_header_size; |
3777 | ||
62d7a5f6 | 3778 | if (phdr_size == (bfd_size_type) -1) |
8ded5a0f AM |
3779 | phdr_size = get_program_header_size (abfd, info); |
3780 | if ((abfd->flags & D_PAGED) == 0 | |
8d06853e AM |
3781 | || (sections[0]->lma & addr_mask) < phdr_size |
3782 | || ((sections[0]->lma & addr_mask) % maxpagesize | |
3783 | < phdr_size % maxpagesize) | |
3784 | || (sections[0]->lma & addr_mask & -maxpagesize) < wrap_to) | |
8ded5a0f | 3785 | phdr_in_segment = FALSE; |
252b5132 RH |
3786 | } |
3787 | ||
8ded5a0f | 3788 | for (i = 0, hdrpp = sections; i < count; i++, hdrpp++) |
252b5132 | 3789 | { |
8ded5a0f AM |
3790 | asection *hdr; |
3791 | bfd_boolean new_segment; | |
3792 | ||
3793 | hdr = *hdrpp; | |
3794 | ||
3795 | /* See if this section and the last one will fit in the same | |
3796 | segment. */ | |
3797 | ||
3798 | if (last_hdr == NULL) | |
3799 | { | |
3800 | /* If we don't have a segment yet, then we don't need a new | |
3801 | one (we build the last one after this loop). */ | |
3802 | new_segment = FALSE; | |
3803 | } | |
3804 | else if (last_hdr->lma - last_hdr->vma != hdr->lma - hdr->vma) | |
3805 | { | |
3806 | /* If this section has a different relation between the | |
3807 | virtual address and the load address, then we need a new | |
3808 | segment. */ | |
3809 | new_segment = TRUE; | |
3810 | } | |
b5599592 AM |
3811 | else if (hdr->lma < last_hdr->lma + last_size |
3812 | || last_hdr->lma + last_size < last_hdr->lma) | |
3813 | { | |
3814 | /* If this section has a load address that makes it overlap | |
3815 | the previous section, then we need a new segment. */ | |
3816 | new_segment = TRUE; | |
3817 | } | |
39948a60 NC |
3818 | /* In the next test we have to be careful when last_hdr->lma is close |
3819 | to the end of the address space. If the aligned address wraps | |
3820 | around to the start of the address space, then there are no more | |
3821 | pages left in memory and it is OK to assume that the current | |
3822 | section can be included in the current segment. */ | |
3823 | else if ((BFD_ALIGN (last_hdr->lma + last_size, maxpagesize) + maxpagesize | |
3824 | > last_hdr->lma) | |
3825 | && (BFD_ALIGN (last_hdr->lma + last_size, maxpagesize) + maxpagesize | |
4ff73856 | 3826 | <= hdr->lma)) |
8ded5a0f AM |
3827 | { |
3828 | /* If putting this section in this segment would force us to | |
3829 | skip a page in the segment, then we need a new segment. */ | |
3830 | new_segment = TRUE; | |
3831 | } | |
3832 | else if ((last_hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0 | |
3833 | && (hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) != 0) | |
3834 | { | |
3835 | /* We don't want to put a loadable section after a | |
3836 | nonloadable section in the same segment. | |
3837 | Consider .tbss sections as loadable for this purpose. */ | |
3838 | new_segment = TRUE; | |
3839 | } | |
3840 | else if ((abfd->flags & D_PAGED) == 0) | |
3841 | { | |
3842 | /* If the file is not demand paged, which means that we | |
3843 | don't require the sections to be correctly aligned in the | |
3844 | file, then there is no other reason for a new segment. */ | |
3845 | new_segment = FALSE; | |
3846 | } | |
3847 | else if (! writable | |
3848 | && (hdr->flags & SEC_READONLY) == 0 | |
8d06853e AM |
3849 | && (((last_hdr->lma + last_size - 1) & -maxpagesize) |
3850 | != (hdr->lma & -maxpagesize))) | |
8ded5a0f AM |
3851 | { |
3852 | /* We don't want to put a writable section in a read only | |
3853 | segment, unless they are on the same page in memory | |
3854 | anyhow. We already know that the last section does not | |
3855 | bring us past the current section on the page, so the | |
3856 | only case in which the new section is not on the same | |
3857 | page as the previous section is when the previous section | |
3858 | ends precisely on a page boundary. */ | |
3859 | new_segment = TRUE; | |
3860 | } | |
3861 | else | |
3862 | { | |
3863 | /* Otherwise, we can use the same segment. */ | |
3864 | new_segment = FALSE; | |
3865 | } | |
3866 | ||
2889e75b | 3867 | /* Allow interested parties a chance to override our decision. */ |
ceae84aa AM |
3868 | if (last_hdr != NULL |
3869 | && info != NULL | |
3870 | && info->callbacks->override_segment_assignment != NULL) | |
3871 | new_segment | |
3872 | = info->callbacks->override_segment_assignment (info, abfd, hdr, | |
3873 | last_hdr, | |
3874 | new_segment); | |
2889e75b | 3875 | |
8ded5a0f AM |
3876 | if (! new_segment) |
3877 | { | |
3878 | if ((hdr->flags & SEC_READONLY) == 0) | |
3879 | writable = TRUE; | |
3880 | last_hdr = hdr; | |
3881 | /* .tbss sections effectively have zero size. */ | |
3882 | if ((hdr->flags & (SEC_THREAD_LOCAL | SEC_LOAD)) | |
3883 | != SEC_THREAD_LOCAL) | |
3884 | last_size = hdr->size; | |
3885 | else | |
3886 | last_size = 0; | |
3887 | continue; | |
3888 | } | |
3889 | ||
3890 | /* We need a new program segment. We must create a new program | |
3891 | header holding all the sections from phdr_index until hdr. */ | |
3892 | ||
3893 | m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment); | |
3894 | if (m == NULL) | |
3895 | goto error_return; | |
3896 | ||
3897 | *pm = m; | |
3898 | pm = &m->next; | |
3899 | ||
252b5132 | 3900 | if ((hdr->flags & SEC_READONLY) == 0) |
b34976b6 | 3901 | writable = TRUE; |
8ded5a0f AM |
3902 | else |
3903 | writable = FALSE; | |
3904 | ||
baaff79e JJ |
3905 | last_hdr = hdr; |
3906 | /* .tbss sections effectively have zero size. */ | |
e5caec89 | 3907 | if ((hdr->flags & (SEC_THREAD_LOCAL | SEC_LOAD)) != SEC_THREAD_LOCAL) |
eea6121a | 3908 | last_size = hdr->size; |
baaff79e JJ |
3909 | else |
3910 | last_size = 0; | |
8ded5a0f AM |
3911 | phdr_index = i; |
3912 | phdr_in_segment = FALSE; | |
252b5132 RH |
3913 | } |
3914 | ||
8ded5a0f AM |
3915 | /* Create a final PT_LOAD program segment. */ |
3916 | if (last_hdr != NULL) | |
3917 | { | |
3918 | m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment); | |
3919 | if (m == NULL) | |
3920 | goto error_return; | |
252b5132 | 3921 | |
8ded5a0f AM |
3922 | *pm = m; |
3923 | pm = &m->next; | |
3924 | } | |
252b5132 | 3925 | |
8ded5a0f AM |
3926 | /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */ |
3927 | if (dynsec != NULL) | |
3928 | { | |
3929 | m = _bfd_elf_make_dynamic_segment (abfd, dynsec); | |
3930 | if (m == NULL) | |
3931 | goto error_return; | |
3932 | *pm = m; | |
3933 | pm = &m->next; | |
3934 | } | |
252b5132 | 3935 | |
1c5265b5 JJ |
3936 | /* For each batch of consecutive loadable .note sections, |
3937 | add a PT_NOTE segment. We don't use bfd_get_section_by_name, | |
3938 | because if we link together nonloadable .note sections and | |
3939 | loadable .note sections, we will generate two .note sections | |
3940 | in the output file. FIXME: Using names for section types is | |
3941 | bogus anyhow. */ | |
8ded5a0f AM |
3942 | for (s = abfd->sections; s != NULL; s = s->next) |
3943 | { | |
3944 | if ((s->flags & SEC_LOAD) != 0 | |
0112cd26 | 3945 | && CONST_STRNEQ (s->name, ".note")) |
8ded5a0f | 3946 | { |
1c5265b5 | 3947 | asection *s2; |
91d6fa6a NC |
3948 | |
3949 | count = 1; | |
8ded5a0f | 3950 | amt = sizeof (struct elf_segment_map); |
1c5265b5 JJ |
3951 | if (s->alignment_power == 2) |
3952 | for (s2 = s; s2->next != NULL; s2 = s2->next) | |
55b581a6 JJ |
3953 | { |
3954 | if (s2->next->alignment_power == 2 | |
3955 | && (s2->next->flags & SEC_LOAD) != 0 | |
3956 | && CONST_STRNEQ (s2->next->name, ".note") | |
8d06853e AM |
3957 | && align_power (s2->lma + s2->size, 2) |
3958 | == s2->next->lma) | |
55b581a6 JJ |
3959 | count++; |
3960 | else | |
3961 | break; | |
3962 | } | |
1c5265b5 | 3963 | amt += (count - 1) * sizeof (asection *); |
a50b1753 | 3964 | m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); |
8ded5a0f AM |
3965 | if (m == NULL) |
3966 | goto error_return; | |
3967 | m->next = NULL; | |
3968 | m->p_type = PT_NOTE; | |
1c5265b5 JJ |
3969 | m->count = count; |
3970 | while (count > 1) | |
3971 | { | |
3972 | m->sections[m->count - count--] = s; | |
3973 | BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0); | |
3974 | s = s->next; | |
3975 | } | |
3976 | m->sections[m->count - 1] = s; | |
3977 | BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0); | |
8ded5a0f AM |
3978 | *pm = m; |
3979 | pm = &m->next; | |
3980 | } | |
3981 | if (s->flags & SEC_THREAD_LOCAL) | |
3982 | { | |
3983 | if (! tls_count) | |
3984 | first_tls = s; | |
3985 | tls_count++; | |
3986 | } | |
3987 | } | |
252b5132 | 3988 | |
8ded5a0f AM |
3989 | /* If there are any SHF_TLS output sections, add PT_TLS segment. */ |
3990 | if (tls_count > 0) | |
3991 | { | |
8ded5a0f AM |
3992 | amt = sizeof (struct elf_segment_map); |
3993 | amt += (tls_count - 1) * sizeof (asection *); | |
a50b1753 | 3994 | m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); |
8ded5a0f AM |
3995 | if (m == NULL) |
3996 | goto error_return; | |
3997 | m->next = NULL; | |
3998 | m->p_type = PT_TLS; | |
3999 | m->count = tls_count; | |
4000 | /* Mandated PF_R. */ | |
4001 | m->p_flags = PF_R; | |
4002 | m->p_flags_valid = 1; | |
91d6fa6a | 4003 | for (i = 0; i < (unsigned int) tls_count; ++i) |
8ded5a0f AM |
4004 | { |
4005 | BFD_ASSERT (first_tls->flags & SEC_THREAD_LOCAL); | |
4006 | m->sections[i] = first_tls; | |
4007 | first_tls = first_tls->next; | |
4008 | } | |
252b5132 | 4009 | |
8ded5a0f AM |
4010 | *pm = m; |
4011 | pm = &m->next; | |
4012 | } | |
252b5132 | 4013 | |
8ded5a0f AM |
4014 | /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME |
4015 | segment. */ | |
4016 | eh_frame_hdr = elf_tdata (abfd)->eh_frame_hdr; | |
4017 | if (eh_frame_hdr != NULL | |
4018 | && (eh_frame_hdr->output_section->flags & SEC_LOAD) != 0) | |
252b5132 | 4019 | { |
dc810e39 | 4020 | amt = sizeof (struct elf_segment_map); |
a50b1753 | 4021 | m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); |
252b5132 RH |
4022 | if (m == NULL) |
4023 | goto error_return; | |
4024 | m->next = NULL; | |
8ded5a0f | 4025 | m->p_type = PT_GNU_EH_FRAME; |
252b5132 | 4026 | m->count = 1; |
8ded5a0f | 4027 | m->sections[0] = eh_frame_hdr->output_section; |
252b5132 RH |
4028 | |
4029 | *pm = m; | |
4030 | pm = &m->next; | |
4031 | } | |
13ae64f3 | 4032 | |
8ded5a0f | 4033 | if (elf_tdata (abfd)->stack_flags) |
13ae64f3 | 4034 | { |
8ded5a0f | 4035 | amt = sizeof (struct elf_segment_map); |
a50b1753 | 4036 | m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); |
8ded5a0f AM |
4037 | if (m == NULL) |
4038 | goto error_return; | |
4039 | m->next = NULL; | |
2b05f1b7 | 4040 | m->p_type = PT_GNU_STACK; |
8ded5a0f AM |
4041 | m->p_flags = elf_tdata (abfd)->stack_flags; |
4042 | m->p_flags_valid = 1; | |
252b5132 | 4043 | |
8ded5a0f AM |
4044 | *pm = m; |
4045 | pm = &m->next; | |
4046 | } | |
65765700 | 4047 | |
ceae84aa | 4048 | if (info != NULL && info->relro) |
8ded5a0f | 4049 | { |
f210dcff L |
4050 | for (m = mfirst; m != NULL; m = m->next) |
4051 | { | |
4052 | if (m->p_type == PT_LOAD) | |
4053 | { | |
4054 | asection *last = m->sections[m->count - 1]; | |
4055 | bfd_vma vaddr = m->sections[0]->vma; | |
4056 | bfd_vma filesz = last->vma - vaddr + last->size; | |
65765700 | 4057 | |
f210dcff L |
4058 | if (vaddr < info->relro_end |
4059 | && vaddr >= info->relro_start | |
4060 | && (vaddr + filesz) >= info->relro_end) | |
4061 | break; | |
4062 | } | |
4063 | } | |
4064 | ||
4065 | /* Make a PT_GNU_RELRO segment only when it isn't empty. */ | |
4066 | if (m != NULL) | |
4067 | { | |
4068 | amt = sizeof (struct elf_segment_map); | |
a50b1753 | 4069 | m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); |
f210dcff L |
4070 | if (m == NULL) |
4071 | goto error_return; | |
4072 | m->next = NULL; | |
4073 | m->p_type = PT_GNU_RELRO; | |
4074 | m->p_flags = PF_R; | |
4075 | m->p_flags_valid = 1; | |
4076 | ||
4077 | *pm = m; | |
4078 | pm = &m->next; | |
4079 | } | |
8ded5a0f | 4080 | } |
9ee5e499 | 4081 | |
8ded5a0f AM |
4082 | free (sections); |
4083 | elf_tdata (abfd)->segment_map = mfirst; | |
9ee5e499 JJ |
4084 | } |
4085 | ||
3dea8fca | 4086 | if (!elf_modify_segment_map (abfd, info, no_user_phdrs)) |
8ded5a0f | 4087 | return FALSE; |
8c37241b | 4088 | |
8ded5a0f AM |
4089 | for (count = 0, m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next) |
4090 | ++count; | |
4091 | elf_tdata (abfd)->program_header_size = count * bed->s->sizeof_phdr; | |
252b5132 | 4092 | |
b34976b6 | 4093 | return TRUE; |
252b5132 RH |
4094 | |
4095 | error_return: | |
4096 | if (sections != NULL) | |
4097 | free (sections); | |
b34976b6 | 4098 | return FALSE; |
252b5132 RH |
4099 | } |
4100 | ||
4101 | /* Sort sections by address. */ | |
4102 | ||
4103 | static int | |
217aa764 | 4104 | elf_sort_sections (const void *arg1, const void *arg2) |
252b5132 RH |
4105 | { |
4106 | const asection *sec1 = *(const asection **) arg1; | |
4107 | const asection *sec2 = *(const asection **) arg2; | |
eecdbe52 | 4108 | bfd_size_type size1, size2; |
252b5132 RH |
4109 | |
4110 | /* Sort by LMA first, since this is the address used to | |
4111 | place the section into a segment. */ | |
4112 | if (sec1->lma < sec2->lma) | |
4113 | return -1; | |
4114 | else if (sec1->lma > sec2->lma) | |
4115 | return 1; | |
4116 | ||
4117 | /* Then sort by VMA. Normally the LMA and the VMA will be | |
4118 | the same, and this will do nothing. */ | |
4119 | if (sec1->vma < sec2->vma) | |
4120 | return -1; | |
4121 | else if (sec1->vma > sec2->vma) | |
4122 | return 1; | |
4123 | ||
4124 | /* Put !SEC_LOAD sections after SEC_LOAD ones. */ | |
4125 | ||
07c6e936 | 4126 | #define TOEND(x) (((x)->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0) |
252b5132 RH |
4127 | |
4128 | if (TOEND (sec1)) | |
4129 | { | |
4130 | if (TOEND (sec2)) | |
00a7cdc5 NC |
4131 | { |
4132 | /* If the indicies are the same, do not return 0 | |
4133 | here, but continue to try the next comparison. */ | |
4134 | if (sec1->target_index - sec2->target_index != 0) | |
4135 | return sec1->target_index - sec2->target_index; | |
4136 | } | |
252b5132 RH |
4137 | else |
4138 | return 1; | |
4139 | } | |
00a7cdc5 | 4140 | else if (TOEND (sec2)) |
252b5132 RH |
4141 | return -1; |
4142 | ||
4143 | #undef TOEND | |
4144 | ||
00a7cdc5 NC |
4145 | /* Sort by size, to put zero sized sections |
4146 | before others at the same address. */ | |
252b5132 | 4147 | |
eea6121a AM |
4148 | size1 = (sec1->flags & SEC_LOAD) ? sec1->size : 0; |
4149 | size2 = (sec2->flags & SEC_LOAD) ? sec2->size : 0; | |
eecdbe52 JJ |
4150 | |
4151 | if (size1 < size2) | |
252b5132 | 4152 | return -1; |
eecdbe52 | 4153 | if (size1 > size2) |
252b5132 RH |
4154 | return 1; |
4155 | ||
4156 | return sec1->target_index - sec2->target_index; | |
4157 | } | |
4158 | ||
340b6d91 AC |
4159 | /* Ian Lance Taylor writes: |
4160 | ||
4161 | We shouldn't be using % with a negative signed number. That's just | |
4162 | not good. We have to make sure either that the number is not | |
4163 | negative, or that the number has an unsigned type. When the types | |
4164 | are all the same size they wind up as unsigned. When file_ptr is a | |
4165 | larger signed type, the arithmetic winds up as signed long long, | |
4166 | which is wrong. | |
4167 | ||
4168 | What we're trying to say here is something like ``increase OFF by | |
4169 | the least amount that will cause it to be equal to the VMA modulo | |
4170 | the page size.'' */ | |
4171 | /* In other words, something like: | |
4172 | ||
4173 | vma_offset = m->sections[0]->vma % bed->maxpagesize; | |
4174 | off_offset = off % bed->maxpagesize; | |
4175 | if (vma_offset < off_offset) | |
4176 | adjustment = vma_offset + bed->maxpagesize - off_offset; | |
4177 | else | |
4178 | adjustment = vma_offset - off_offset; | |
08a40648 | 4179 | |
340b6d91 AC |
4180 | which can can be collapsed into the expression below. */ |
4181 | ||
4182 | static file_ptr | |
4183 | vma_page_aligned_bias (bfd_vma vma, ufile_ptr off, bfd_vma maxpagesize) | |
4184 | { | |
4185 | return ((vma - off) % maxpagesize); | |
4186 | } | |
4187 | ||
6d33f217 L |
4188 | static void |
4189 | print_segment_map (const struct elf_segment_map *m) | |
4190 | { | |
4191 | unsigned int j; | |
4192 | const char *pt = get_segment_type (m->p_type); | |
4193 | char buf[32]; | |
4194 | ||
4195 | if (pt == NULL) | |
4196 | { | |
4197 | if (m->p_type >= PT_LOPROC && m->p_type <= PT_HIPROC) | |
4198 | sprintf (buf, "LOPROC+%7.7x", | |
4199 | (unsigned int) (m->p_type - PT_LOPROC)); | |
4200 | else if (m->p_type >= PT_LOOS && m->p_type <= PT_HIOS) | |
4201 | sprintf (buf, "LOOS+%7.7x", | |
4202 | (unsigned int) (m->p_type - PT_LOOS)); | |
4203 | else | |
4204 | snprintf (buf, sizeof (buf), "%8.8x", | |
4205 | (unsigned int) m->p_type); | |
4206 | pt = buf; | |
4207 | } | |
4208 | fprintf (stderr, "%s:", pt); | |
4209 | for (j = 0; j < m->count; j++) | |
4210 | fprintf (stderr, " %s", m->sections [j]->name); | |
4211 | putc ('\n',stderr); | |
4212 | } | |
4213 | ||
32812159 AM |
4214 | static bfd_boolean |
4215 | write_zeros (bfd *abfd, file_ptr pos, bfd_size_type len) | |
4216 | { | |
4217 | void *buf; | |
4218 | bfd_boolean ret; | |
4219 | ||
4220 | if (bfd_seek (abfd, pos, SEEK_SET) != 0) | |
4221 | return FALSE; | |
4222 | buf = bfd_zmalloc (len); | |
4223 | if (buf == NULL) | |
4224 | return FALSE; | |
4225 | ret = bfd_bwrite (buf, len, abfd) == len; | |
4226 | free (buf); | |
4227 | return ret; | |
4228 | } | |
4229 | ||
252b5132 RH |
4230 | /* Assign file positions to the sections based on the mapping from |
4231 | sections to segments. This function also sets up some fields in | |
f3520d2f | 4232 | the file header. */ |
252b5132 | 4233 | |
b34976b6 | 4234 | static bfd_boolean |
f3520d2f AM |
4235 | assign_file_positions_for_load_sections (bfd *abfd, |
4236 | struct bfd_link_info *link_info) | |
252b5132 RH |
4237 | { |
4238 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
252b5132 | 4239 | struct elf_segment_map *m; |
252b5132 | 4240 | Elf_Internal_Phdr *phdrs; |
252b5132 | 4241 | Elf_Internal_Phdr *p; |
02bf8d82 | 4242 | file_ptr off; |
3f570048 | 4243 | bfd_size_type maxpagesize; |
f3520d2f | 4244 | unsigned int alloc; |
0920dee7 | 4245 | unsigned int i, j; |
2b0bc088 | 4246 | bfd_vma header_pad = 0; |
252b5132 | 4247 | |
e36284ab | 4248 | if (link_info == NULL |
ceae84aa | 4249 | && !_bfd_elf_map_sections_to_segments (abfd, link_info)) |
8ded5a0f | 4250 | return FALSE; |
252b5132 | 4251 | |
8ded5a0f | 4252 | alloc = 0; |
252b5132 | 4253 | for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next) |
2b0bc088 NC |
4254 | { |
4255 | ++alloc; | |
4256 | if (m->header_size) | |
4257 | header_pad = m->header_size; | |
4258 | } | |
252b5132 RH |
4259 | |
4260 | elf_elfheader (abfd)->e_phoff = bed->s->sizeof_ehdr; | |
4261 | elf_elfheader (abfd)->e_phentsize = bed->s->sizeof_phdr; | |
8ded5a0f | 4262 | elf_elfheader (abfd)->e_phnum = alloc; |
252b5132 | 4263 | |
62d7a5f6 | 4264 | if (elf_tdata (abfd)->program_header_size == (bfd_size_type) -1) |
8ded5a0f AM |
4265 | elf_tdata (abfd)->program_header_size = alloc * bed->s->sizeof_phdr; |
4266 | else | |
4267 | BFD_ASSERT (elf_tdata (abfd)->program_header_size | |
59e0647f | 4268 | >= alloc * bed->s->sizeof_phdr); |
252b5132 RH |
4269 | |
4270 | if (alloc == 0) | |
f3520d2f | 4271 | { |
8ded5a0f AM |
4272 | elf_tdata (abfd)->next_file_pos = bed->s->sizeof_ehdr; |
4273 | return TRUE; | |
f3520d2f | 4274 | } |
252b5132 | 4275 | |
57268894 HPN |
4276 | /* We're writing the size in elf_tdata (abfd)->program_header_size, |
4277 | see assign_file_positions_except_relocs, so make sure we have | |
4278 | that amount allocated, with trailing space cleared. | |
4279 | The variable alloc contains the computed need, while elf_tdata | |
4280 | (abfd)->program_header_size contains the size used for the | |
4281 | layout. | |
4282 | See ld/emultempl/elf-generic.em:gld${EMULATION_NAME}_map_segments | |
4283 | where the layout is forced to according to a larger size in the | |
4284 | last iterations for the testcase ld-elf/header. */ | |
4285 | BFD_ASSERT (elf_tdata (abfd)->program_header_size % bed->s->sizeof_phdr | |
4286 | == 0); | |
a50b1753 NC |
4287 | phdrs = (Elf_Internal_Phdr *) |
4288 | bfd_zalloc2 (abfd, | |
4289 | (elf_tdata (abfd)->program_header_size / bed->s->sizeof_phdr), | |
4290 | sizeof (Elf_Internal_Phdr)); | |
f3520d2f | 4291 | elf_tdata (abfd)->phdr = phdrs; |
252b5132 | 4292 | if (phdrs == NULL) |
b34976b6 | 4293 | return FALSE; |
252b5132 | 4294 | |
3f570048 AM |
4295 | maxpagesize = 1; |
4296 | if ((abfd->flags & D_PAGED) != 0) | |
4297 | maxpagesize = bed->maxpagesize; | |
4298 | ||
252b5132 RH |
4299 | off = bed->s->sizeof_ehdr; |
4300 | off += alloc * bed->s->sizeof_phdr; | |
2b0bc088 NC |
4301 | if (header_pad < (bfd_vma) off) |
4302 | header_pad = 0; | |
4303 | else | |
4304 | header_pad -= off; | |
4305 | off += header_pad; | |
252b5132 | 4306 | |
0920dee7 | 4307 | for (m = elf_tdata (abfd)->segment_map, p = phdrs, j = 0; |
252b5132 | 4308 | m != NULL; |
0920dee7 | 4309 | m = m->next, p++, j++) |
252b5132 | 4310 | { |
252b5132 | 4311 | asection **secpp; |
bf988460 AM |
4312 | bfd_vma off_adjust; |
4313 | bfd_boolean no_contents; | |
252b5132 RH |
4314 | |
4315 | /* If elf_segment_map is not from map_sections_to_segments, the | |
08a40648 | 4316 | sections may not be correctly ordered. NOTE: sorting should |
52e9b619 MS |
4317 | not be done to the PT_NOTE section of a corefile, which may |
4318 | contain several pseudo-sections artificially created by bfd. | |
4319 | Sorting these pseudo-sections breaks things badly. */ | |
47d9a591 AM |
4320 | if (m->count > 1 |
4321 | && !(elf_elfheader (abfd)->e_type == ET_CORE | |
52e9b619 | 4322 | && m->p_type == PT_NOTE)) |
252b5132 RH |
4323 | qsort (m->sections, (size_t) m->count, sizeof (asection *), |
4324 | elf_sort_sections); | |
4325 | ||
b301b248 AM |
4326 | /* An ELF segment (described by Elf_Internal_Phdr) may contain a |
4327 | number of sections with contents contributing to both p_filesz | |
4328 | and p_memsz, followed by a number of sections with no contents | |
4329 | that just contribute to p_memsz. In this loop, OFF tracks next | |
02bf8d82 | 4330 | available file offset for PT_LOAD and PT_NOTE segments. */ |
252b5132 | 4331 | p->p_type = m->p_type; |
28a7f3e7 | 4332 | p->p_flags = m->p_flags; |
252b5132 | 4333 | |
3f570048 AM |
4334 | if (m->count == 0) |
4335 | p->p_vaddr = 0; | |
4336 | else | |
3271a814 | 4337 | p->p_vaddr = m->sections[0]->vma - m->p_vaddr_offset; |
3f570048 AM |
4338 | |
4339 | if (m->p_paddr_valid) | |
4340 | p->p_paddr = m->p_paddr; | |
4341 | else if (m->count == 0) | |
4342 | p->p_paddr = 0; | |
4343 | else | |
08a40648 | 4344 | p->p_paddr = m->sections[0]->lma - m->p_vaddr_offset; |
3f570048 AM |
4345 | |
4346 | if (p->p_type == PT_LOAD | |
4347 | && (abfd->flags & D_PAGED) != 0) | |
4348 | { | |
4349 | /* p_align in demand paged PT_LOAD segments effectively stores | |
4350 | the maximum page size. When copying an executable with | |
4351 | objcopy, we set m->p_align from the input file. Use this | |
4352 | value for maxpagesize rather than bed->maxpagesize, which | |
4353 | may be different. Note that we use maxpagesize for PT_TLS | |
4354 | segment alignment later in this function, so we are relying | |
4355 | on at least one PT_LOAD segment appearing before a PT_TLS | |
4356 | segment. */ | |
4357 | if (m->p_align_valid) | |
4358 | maxpagesize = m->p_align; | |
4359 | ||
4360 | p->p_align = maxpagesize; | |
4361 | } | |
3271a814 NS |
4362 | else if (m->p_align_valid) |
4363 | p->p_align = m->p_align; | |
e970b90a DJ |
4364 | else if (m->count == 0) |
4365 | p->p_align = 1 << bed->s->log_file_align; | |
3f570048 AM |
4366 | else |
4367 | p->p_align = 0; | |
4368 | ||
bf988460 AM |
4369 | no_contents = FALSE; |
4370 | off_adjust = 0; | |
252b5132 | 4371 | if (p->p_type == PT_LOAD |
b301b248 | 4372 | && m->count > 0) |
252b5132 | 4373 | { |
b301b248 | 4374 | bfd_size_type align; |
a49e53ed | 4375 | unsigned int align_power = 0; |
b301b248 | 4376 | |
3271a814 NS |
4377 | if (m->p_align_valid) |
4378 | align = p->p_align; | |
4379 | else | |
252b5132 | 4380 | { |
3271a814 NS |
4381 | for (i = 0, secpp = m->sections; i < m->count; i++, secpp++) |
4382 | { | |
4383 | unsigned int secalign; | |
08a40648 | 4384 | |
3271a814 NS |
4385 | secalign = bfd_get_section_alignment (abfd, *secpp); |
4386 | if (secalign > align_power) | |
4387 | align_power = secalign; | |
4388 | } | |
4389 | align = (bfd_size_type) 1 << align_power; | |
4390 | if (align < maxpagesize) | |
4391 | align = maxpagesize; | |
b301b248 | 4392 | } |
252b5132 | 4393 | |
02bf8d82 AM |
4394 | for (i = 0; i < m->count; i++) |
4395 | if ((m->sections[i]->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0) | |
4396 | /* If we aren't making room for this section, then | |
4397 | it must be SHT_NOBITS regardless of what we've | |
4398 | set via struct bfd_elf_special_section. */ | |
4399 | elf_section_type (m->sections[i]) = SHT_NOBITS; | |
4400 | ||
bf988460 | 4401 | /* Find out whether this segment contains any loadable |
aea274d3 AM |
4402 | sections. */ |
4403 | no_contents = TRUE; | |
4404 | for (i = 0; i < m->count; i++) | |
4405 | if (elf_section_type (m->sections[i]) != SHT_NOBITS) | |
4406 | { | |
4407 | no_contents = FALSE; | |
4408 | break; | |
4409 | } | |
bf988460 | 4410 | |
85cfcbfb | 4411 | off_adjust = vma_page_aligned_bias (p->p_vaddr, off, align); |
bf988460 AM |
4412 | off += off_adjust; |
4413 | if (no_contents) | |
4414 | { | |
4415 | /* We shouldn't need to align the segment on disk since | |
4416 | the segment doesn't need file space, but the gABI | |
4417 | arguably requires the alignment and glibc ld.so | |
4418 | checks it. So to comply with the alignment | |
4419 | requirement but not waste file space, we adjust | |
4420 | p_offset for just this segment. (OFF_ADJUST is | |
4421 | subtracted from OFF later.) This may put p_offset | |
4422 | past the end of file, but that shouldn't matter. */ | |
4423 | } | |
4424 | else | |
4425 | off_adjust = 0; | |
252b5132 | 4426 | } |
b1a6d0b1 NC |
4427 | /* Make sure the .dynamic section is the first section in the |
4428 | PT_DYNAMIC segment. */ | |
4429 | else if (p->p_type == PT_DYNAMIC | |
4430 | && m->count > 1 | |
4431 | && strcmp (m->sections[0]->name, ".dynamic") != 0) | |
4432 | { | |
4433 | _bfd_error_handler | |
b301b248 AM |
4434 | (_("%B: The first section in the PT_DYNAMIC segment is not the .dynamic section"), |
4435 | abfd); | |
b1a6d0b1 NC |
4436 | bfd_set_error (bfd_error_bad_value); |
4437 | return FALSE; | |
4438 | } | |
3f001e84 JK |
4439 | /* Set the note section type to SHT_NOTE. */ |
4440 | else if (p->p_type == PT_NOTE) | |
4441 | for (i = 0; i < m->count; i++) | |
4442 | elf_section_type (m->sections[i]) = SHT_NOTE; | |
252b5132 | 4443 | |
252b5132 RH |
4444 | p->p_offset = 0; |
4445 | p->p_filesz = 0; | |
4446 | p->p_memsz = 0; | |
4447 | ||
4448 | if (m->includes_filehdr) | |
4449 | { | |
bf988460 | 4450 | if (!m->p_flags_valid) |
252b5132 | 4451 | p->p_flags |= PF_R; |
252b5132 RH |
4452 | p->p_filesz = bed->s->sizeof_ehdr; |
4453 | p->p_memsz = bed->s->sizeof_ehdr; | |
4454 | if (m->count > 0) | |
4455 | { | |
4456 | BFD_ASSERT (p->p_type == PT_LOAD); | |
4457 | ||
4458 | if (p->p_vaddr < (bfd_vma) off) | |
4459 | { | |
caf47ea6 | 4460 | (*_bfd_error_handler) |
b301b248 AM |
4461 | (_("%B: Not enough room for program headers, try linking with -N"), |
4462 | abfd); | |
252b5132 | 4463 | bfd_set_error (bfd_error_bad_value); |
b34976b6 | 4464 | return FALSE; |
252b5132 RH |
4465 | } |
4466 | ||
4467 | p->p_vaddr -= off; | |
bf988460 | 4468 | if (!m->p_paddr_valid) |
252b5132 RH |
4469 | p->p_paddr -= off; |
4470 | } | |
252b5132 RH |
4471 | } |
4472 | ||
4473 | if (m->includes_phdrs) | |
4474 | { | |
bf988460 | 4475 | if (!m->p_flags_valid) |
252b5132 RH |
4476 | p->p_flags |= PF_R; |
4477 | ||
f3520d2f | 4478 | if (!m->includes_filehdr) |
252b5132 RH |
4479 | { |
4480 | p->p_offset = bed->s->sizeof_ehdr; | |
4481 | ||
4482 | if (m->count > 0) | |
4483 | { | |
4484 | BFD_ASSERT (p->p_type == PT_LOAD); | |
4485 | p->p_vaddr -= off - p->p_offset; | |
bf988460 | 4486 | if (!m->p_paddr_valid) |
252b5132 RH |
4487 | p->p_paddr -= off - p->p_offset; |
4488 | } | |
252b5132 RH |
4489 | } |
4490 | ||
4491 | p->p_filesz += alloc * bed->s->sizeof_phdr; | |
4492 | p->p_memsz += alloc * bed->s->sizeof_phdr; | |
2b0bc088 NC |
4493 | if (m->count) |
4494 | { | |
4495 | p->p_filesz += header_pad; | |
4496 | p->p_memsz += header_pad; | |
4497 | } | |
252b5132 RH |
4498 | } |
4499 | ||
4500 | if (p->p_type == PT_LOAD | |
4501 | || (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core)) | |
4502 | { | |
bf988460 | 4503 | if (!m->includes_filehdr && !m->includes_phdrs) |
02bf8d82 | 4504 | p->p_offset = off; |
252b5132 RH |
4505 | else |
4506 | { | |
4507 | file_ptr adjust; | |
4508 | ||
4509 | adjust = off - (p->p_offset + p->p_filesz); | |
bf988460 AM |
4510 | if (!no_contents) |
4511 | p->p_filesz += adjust; | |
252b5132 RH |
4512 | p->p_memsz += adjust; |
4513 | } | |
4514 | } | |
4515 | ||
1ea63fd2 AM |
4516 | /* Set up p_filesz, p_memsz, p_align and p_flags from the section |
4517 | maps. Set filepos for sections in PT_LOAD segments, and in | |
4518 | core files, for sections in PT_NOTE segments. | |
4519 | assign_file_positions_for_non_load_sections will set filepos | |
4520 | for other sections and update p_filesz for other segments. */ | |
252b5132 RH |
4521 | for (i = 0, secpp = m->sections; i < m->count; i++, secpp++) |
4522 | { | |
4523 | asection *sec; | |
252b5132 | 4524 | bfd_size_type align; |
627b32bc | 4525 | Elf_Internal_Shdr *this_hdr; |
252b5132 RH |
4526 | |
4527 | sec = *secpp; | |
02bf8d82 | 4528 | this_hdr = &elf_section_data (sec)->this_hdr; |
3f570048 | 4529 | align = (bfd_size_type) 1 << bfd_get_section_alignment (abfd, sec); |
252b5132 | 4530 | |
88967714 AM |
4531 | if ((p->p_type == PT_LOAD |
4532 | || p->p_type == PT_TLS) | |
4533 | && (this_hdr->sh_type != SHT_NOBITS | |
4534 | || ((this_hdr->sh_flags & SHF_ALLOC) != 0 | |
4535 | && ((this_hdr->sh_flags & SHF_TLS) == 0 | |
4536 | || p->p_type == PT_TLS)))) | |
252b5132 | 4537 | { |
b5599592 AM |
4538 | bfd_vma p_start = p->p_paddr; |
4539 | bfd_vma p_end = p_start + p->p_memsz; | |
4540 | bfd_vma s_start = sec->lma; | |
4541 | bfd_vma adjust = s_start - p_end; | |
252b5132 | 4542 | |
a2d1e028 L |
4543 | if (adjust != 0 |
4544 | && (s_start < p_end | |
4545 | || p_end < p_start)) | |
252b5132 | 4546 | { |
88967714 | 4547 | (*_bfd_error_handler) |
b5599592 AM |
4548 | (_("%B: section %A lma %#lx adjusted to %#lx"), abfd, sec, |
4549 | (unsigned long) s_start, (unsigned long) p_end); | |
88967714 | 4550 | adjust = 0; |
b5599592 | 4551 | sec->lma = p_end; |
1cfb7d1e | 4552 | } |
3ac9b6c9 | 4553 | p->p_memsz += adjust; |
1cfb7d1e | 4554 | |
88967714 AM |
4555 | if (this_hdr->sh_type != SHT_NOBITS) |
4556 | { | |
32812159 AM |
4557 | if (p->p_filesz + adjust < p->p_memsz) |
4558 | { | |
4559 | /* We have a PROGBITS section following NOBITS ones. | |
4560 | Allocate file space for the NOBITS section(s) and | |
4561 | zero it. */ | |
4562 | adjust = p->p_memsz - p->p_filesz; | |
4563 | if (!write_zeros (abfd, off, adjust)) | |
4564 | return FALSE; | |
4565 | } | |
88967714 AM |
4566 | off += adjust; |
4567 | p->p_filesz += adjust; | |
252b5132 | 4568 | } |
252b5132 RH |
4569 | } |
4570 | ||
4571 | if (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core) | |
4572 | { | |
b301b248 AM |
4573 | /* The section at i == 0 is the one that actually contains |
4574 | everything. */ | |
4a938328 MS |
4575 | if (i == 0) |
4576 | { | |
627b32bc | 4577 | this_hdr->sh_offset = sec->filepos = off; |
6a3cd2b4 AM |
4578 | off += this_hdr->sh_size; |
4579 | p->p_filesz = this_hdr->sh_size; | |
b301b248 AM |
4580 | p->p_memsz = 0; |
4581 | p->p_align = 1; | |
252b5132 | 4582 | } |
4a938328 | 4583 | else |
252b5132 | 4584 | { |
b301b248 | 4585 | /* The rest are fake sections that shouldn't be written. */ |
252b5132 | 4586 | sec->filepos = 0; |
eea6121a | 4587 | sec->size = 0; |
b301b248 AM |
4588 | sec->flags = 0; |
4589 | continue; | |
252b5132 | 4590 | } |
252b5132 RH |
4591 | } |
4592 | else | |
4593 | { | |
b301b248 AM |
4594 | if (p->p_type == PT_LOAD) |
4595 | { | |
02bf8d82 AM |
4596 | this_hdr->sh_offset = sec->filepos = off; |
4597 | if (this_hdr->sh_type != SHT_NOBITS) | |
6a3cd2b4 | 4598 | off += this_hdr->sh_size; |
b301b248 | 4599 | } |
252b5132 | 4600 | |
02bf8d82 | 4601 | if (this_hdr->sh_type != SHT_NOBITS) |
b301b248 | 4602 | { |
6a3cd2b4 | 4603 | p->p_filesz += this_hdr->sh_size; |
02bf8d82 AM |
4604 | /* A load section without SHF_ALLOC is something like |
4605 | a note section in a PT_NOTE segment. These take | |
4606 | file space but are not loaded into memory. */ | |
4607 | if ((this_hdr->sh_flags & SHF_ALLOC) != 0) | |
6a3cd2b4 | 4608 | p->p_memsz += this_hdr->sh_size; |
b301b248 | 4609 | } |
6a3cd2b4 | 4610 | else if ((this_hdr->sh_flags & SHF_ALLOC) != 0) |
13ae64f3 | 4611 | { |
6a3cd2b4 AM |
4612 | if (p->p_type == PT_TLS) |
4613 | p->p_memsz += this_hdr->sh_size; | |
4614 | ||
4615 | /* .tbss is special. It doesn't contribute to p_memsz of | |
4616 | normal segments. */ | |
4617 | else if ((this_hdr->sh_flags & SHF_TLS) == 0) | |
4618 | p->p_memsz += this_hdr->sh_size; | |
13ae64f3 JJ |
4619 | } |
4620 | ||
b10a8ae0 L |
4621 | if (align > p->p_align |
4622 | && !m->p_align_valid | |
4623 | && (p->p_type != PT_LOAD | |
4624 | || (abfd->flags & D_PAGED) == 0)) | |
252b5132 RH |
4625 | p->p_align = align; |
4626 | } | |
4627 | ||
bf988460 | 4628 | if (!m->p_flags_valid) |
252b5132 RH |
4629 | { |
4630 | p->p_flags |= PF_R; | |
02bf8d82 | 4631 | if ((this_hdr->sh_flags & SHF_EXECINSTR) != 0) |
252b5132 | 4632 | p->p_flags |= PF_X; |
02bf8d82 | 4633 | if ((this_hdr->sh_flags & SHF_WRITE) != 0) |
252b5132 RH |
4634 | p->p_flags |= PF_W; |
4635 | } | |
4636 | } | |
bf988460 | 4637 | off -= off_adjust; |
0920dee7 | 4638 | |
7c928300 AM |
4639 | /* Check that all sections are in a PT_LOAD segment. |
4640 | Don't check funky gdb generated core files. */ | |
4641 | if (p->p_type == PT_LOAD && bfd_get_format (abfd) != bfd_core) | |
9a83a553 AM |
4642 | { |
4643 | bfd_boolean check_vma = TRUE; | |
4644 | ||
4645 | for (i = 1; i < m->count; i++) | |
4646 | if (m->sections[i]->vma == m->sections[i - 1]->vma | |
4647 | && ELF_SECTION_SIZE (&(elf_section_data (m->sections[i]) | |
4648 | ->this_hdr), p) != 0 | |
4649 | && ELF_SECTION_SIZE (&(elf_section_data (m->sections[i - 1]) | |
4650 | ->this_hdr), p) != 0) | |
0920dee7 | 4651 | { |
9a83a553 AM |
4652 | /* Looks like we have overlays packed into the segment. */ |
4653 | check_vma = FALSE; | |
4654 | break; | |
0920dee7 | 4655 | } |
9a83a553 AM |
4656 | |
4657 | for (i = 0; i < m->count; i++) | |
4658 | { | |
4659 | Elf_Internal_Shdr *this_hdr; | |
4660 | asection *sec; | |
4661 | ||
4662 | sec = m->sections[i]; | |
4663 | this_hdr = &(elf_section_data(sec)->this_hdr); | |
f4638467 | 4664 | if (!ELF_SECTION_IN_SEGMENT_1 (this_hdr, p, check_vma, 0)) |
9a83a553 AM |
4665 | { |
4666 | (*_bfd_error_handler) | |
4667 | (_("%B: section `%A' can't be allocated in segment %d"), | |
4668 | abfd, sec, j); | |
4669 | print_segment_map (m); | |
4670 | } | |
4671 | } | |
4672 | } | |
252b5132 RH |
4673 | } |
4674 | ||
f3520d2f AM |
4675 | elf_tdata (abfd)->next_file_pos = off; |
4676 | return TRUE; | |
4677 | } | |
4678 | ||
4679 | /* Assign file positions for the other sections. */ | |
4680 | ||
4681 | static bfd_boolean | |
4682 | assign_file_positions_for_non_load_sections (bfd *abfd, | |
4683 | struct bfd_link_info *link_info) | |
4684 | { | |
4685 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
4686 | Elf_Internal_Shdr **i_shdrpp; | |
4687 | Elf_Internal_Shdr **hdrpp; | |
4688 | Elf_Internal_Phdr *phdrs; | |
4689 | Elf_Internal_Phdr *p; | |
4690 | struct elf_segment_map *m; | |
4691 | bfd_vma filehdr_vaddr, filehdr_paddr; | |
4692 | bfd_vma phdrs_vaddr, phdrs_paddr; | |
4693 | file_ptr off; | |
4694 | unsigned int num_sec; | |
4695 | unsigned int i; | |
4696 | unsigned int count; | |
4697 | ||
5c182d5f AM |
4698 | i_shdrpp = elf_elfsections (abfd); |
4699 | num_sec = elf_numsections (abfd); | |
f3520d2f | 4700 | off = elf_tdata (abfd)->next_file_pos; |
5c182d5f AM |
4701 | for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++) |
4702 | { | |
4703 | struct elf_obj_tdata *tdata = elf_tdata (abfd); | |
4704 | Elf_Internal_Shdr *hdr; | |
4705 | ||
4706 | hdr = *hdrpp; | |
4707 | if (hdr->bfd_section != NULL | |
252e386e AM |
4708 | && (hdr->bfd_section->filepos != 0 |
4709 | || (hdr->sh_type == SHT_NOBITS | |
4710 | && hdr->contents == NULL))) | |
627b32bc | 4711 | BFD_ASSERT (hdr->sh_offset == hdr->bfd_section->filepos); |
5c182d5f AM |
4712 | else if ((hdr->sh_flags & SHF_ALLOC) != 0) |
4713 | { | |
f4638467 AM |
4714 | (*_bfd_error_handler) |
4715 | (_("%B: warning: allocated section `%s' not in segment"), | |
4716 | abfd, | |
4717 | (hdr->bfd_section == NULL | |
4718 | ? "*unknown*" | |
4719 | : hdr->bfd_section->name)); | |
3ba71138 L |
4720 | /* We don't need to page align empty sections. */ |
4721 | if ((abfd->flags & D_PAGED) != 0 && hdr->sh_size != 0) | |
5c182d5f AM |
4722 | off += vma_page_aligned_bias (hdr->sh_addr, off, |
4723 | bed->maxpagesize); | |
4724 | else | |
4725 | off += vma_page_aligned_bias (hdr->sh_addr, off, | |
4726 | hdr->sh_addralign); | |
4727 | off = _bfd_elf_assign_file_position_for_section (hdr, off, | |
4728 | FALSE); | |
4729 | } | |
4730 | else if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA) | |
4731 | && hdr->bfd_section == NULL) | |
4732 | || hdr == i_shdrpp[tdata->symtab_section] | |
4733 | || hdr == i_shdrpp[tdata->symtab_shndx_section] | |
4734 | || hdr == i_shdrpp[tdata->strtab_section]) | |
4735 | hdr->sh_offset = -1; | |
4736 | else | |
4737 | off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE); | |
5c182d5f AM |
4738 | } |
4739 | ||
252b5132 RH |
4740 | /* Now that we have set the section file positions, we can set up |
4741 | the file positions for the non PT_LOAD segments. */ | |
f3520d2f AM |
4742 | count = 0; |
4743 | filehdr_vaddr = 0; | |
4744 | filehdr_paddr = 0; | |
4745 | phdrs_vaddr = bed->maxpagesize + bed->s->sizeof_ehdr; | |
4746 | phdrs_paddr = 0; | |
4747 | phdrs = elf_tdata (abfd)->phdr; | |
4748 | for (m = elf_tdata (abfd)->segment_map, p = phdrs; | |
4749 | m != NULL; | |
4750 | m = m->next, p++) | |
4751 | { | |
4752 | ++count; | |
4753 | if (p->p_type != PT_LOAD) | |
4754 | continue; | |
4755 | ||
4756 | if (m->includes_filehdr) | |
4757 | { | |
4758 | filehdr_vaddr = p->p_vaddr; | |
4759 | filehdr_paddr = p->p_paddr; | |
4760 | } | |
4761 | if (m->includes_phdrs) | |
4762 | { | |
4763 | phdrs_vaddr = p->p_vaddr; | |
4764 | phdrs_paddr = p->p_paddr; | |
4765 | if (m->includes_filehdr) | |
4766 | { | |
4767 | phdrs_vaddr += bed->s->sizeof_ehdr; | |
4768 | phdrs_paddr += bed->s->sizeof_ehdr; | |
4769 | } | |
4770 | } | |
4771 | } | |
4772 | ||
252b5132 RH |
4773 | for (m = elf_tdata (abfd)->segment_map, p = phdrs; |
4774 | m != NULL; | |
4775 | m = m->next, p++) | |
4776 | { | |
129af99f | 4777 | if (p->p_type == PT_GNU_RELRO) |
252b5132 | 4778 | { |
b84a33b5 AM |
4779 | const Elf_Internal_Phdr *lp; |
4780 | ||
129af99f | 4781 | BFD_ASSERT (!m->includes_filehdr && !m->includes_phdrs); |
1ea63fd2 | 4782 | |
129af99f | 4783 | if (link_info != NULL) |
8c37241b | 4784 | { |
129af99f AS |
4785 | /* During linking the range of the RELRO segment is passed |
4786 | in link_info. */ | |
8c37241b JJ |
4787 | for (lp = phdrs; lp < phdrs + count; ++lp) |
4788 | { | |
4789 | if (lp->p_type == PT_LOAD | |
8c37241b | 4790 | && lp->p_vaddr >= link_info->relro_start |
b84a33b5 AM |
4791 | && lp->p_vaddr < link_info->relro_end |
4792 | && lp->p_vaddr + lp->p_filesz >= link_info->relro_end) | |
8c37241b JJ |
4793 | break; |
4794 | } | |
8c37241b | 4795 | } |
129af99f AS |
4796 | else |
4797 | { | |
4798 | /* Otherwise we are copying an executable or shared | |
b84a33b5 | 4799 | library, but we need to use the same linker logic. */ |
129af99f AS |
4800 | for (lp = phdrs; lp < phdrs + count; ++lp) |
4801 | { | |
4802 | if (lp->p_type == PT_LOAD | |
4803 | && lp->p_paddr == p->p_paddr) | |
4804 | break; | |
4805 | } | |
b84a33b5 AM |
4806 | } |
4807 | ||
4808 | if (lp < phdrs + count) | |
4809 | { | |
4810 | p->p_vaddr = lp->p_vaddr; | |
4811 | p->p_paddr = lp->p_paddr; | |
4812 | p->p_offset = lp->p_offset; | |
4813 | if (link_info != NULL) | |
4814 | p->p_filesz = link_info->relro_end - lp->p_vaddr; | |
4815 | else if (m->p_size_valid) | |
4816 | p->p_filesz = m->p_size; | |
129af99f AS |
4817 | else |
4818 | abort (); | |
b84a33b5 AM |
4819 | p->p_memsz = p->p_filesz; |
4820 | p->p_align = 1; | |
4821 | p->p_flags = (lp->p_flags & ~PF_W); | |
129af99f | 4822 | } |
9433b9b1 | 4823 | else |
b84a33b5 AM |
4824 | { |
4825 | memset (p, 0, sizeof *p); | |
4826 | p->p_type = PT_NULL; | |
4827 | } | |
129af99f AS |
4828 | } |
4829 | else if (m->count != 0) | |
4830 | { | |
4831 | if (p->p_type != PT_LOAD | |
4832 | && (p->p_type != PT_NOTE | |
4833 | || bfd_get_format (abfd) != bfd_core)) | |
4834 | { | |
4835 | Elf_Internal_Shdr *hdr; | |
4836 | asection *sect; | |
4837 | ||
4838 | BFD_ASSERT (!m->includes_filehdr && !m->includes_phdrs); | |
4839 | ||
4840 | sect = m->sections[m->count - 1]; | |
4841 | hdr = &elf_section_data (sect)->this_hdr; | |
4842 | p->p_filesz = sect->filepos - m->sections[0]->filepos; | |
4843 | if (hdr->sh_type != SHT_NOBITS) | |
4844 | p->p_filesz += hdr->sh_size; | |
4845 | p->p_offset = m->sections[0]->filepos; | |
4846 | } | |
4847 | } | |
4848 | else if (m->includes_filehdr) | |
4849 | { | |
4850 | p->p_vaddr = filehdr_vaddr; | |
4851 | if (! m->p_paddr_valid) | |
4852 | p->p_paddr = filehdr_paddr; | |
4853 | } | |
4854 | else if (m->includes_phdrs) | |
4855 | { | |
4856 | p->p_vaddr = phdrs_vaddr; | |
4857 | if (! m->p_paddr_valid) | |
4858 | p->p_paddr = phdrs_paddr; | |
252b5132 RH |
4859 | } |
4860 | } | |
4861 | ||
252b5132 RH |
4862 | elf_tdata (abfd)->next_file_pos = off; |
4863 | ||
b34976b6 | 4864 | return TRUE; |
252b5132 RH |
4865 | } |
4866 | ||
252b5132 RH |
4867 | /* Work out the file positions of all the sections. This is called by |
4868 | _bfd_elf_compute_section_file_positions. All the section sizes and | |
4869 | VMAs must be known before this is called. | |
4870 | ||
e0638f70 AM |
4871 | Reloc sections come in two flavours: Those processed specially as |
4872 | "side-channel" data attached to a section to which they apply, and | |
4873 | those that bfd doesn't process as relocations. The latter sort are | |
4874 | stored in a normal bfd section by bfd_section_from_shdr. We don't | |
4875 | consider the former sort here, unless they form part of the loadable | |
4876 | image. Reloc sections not assigned here will be handled later by | |
4877 | assign_file_positions_for_relocs. | |
252b5132 RH |
4878 | |
4879 | We also don't set the positions of the .symtab and .strtab here. */ | |
4880 | ||
b34976b6 | 4881 | static bfd_boolean |
c84fca4d AO |
4882 | assign_file_positions_except_relocs (bfd *abfd, |
4883 | struct bfd_link_info *link_info) | |
252b5132 | 4884 | { |
5c182d5f AM |
4885 | struct elf_obj_tdata *tdata = elf_tdata (abfd); |
4886 | Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd); | |
252b5132 | 4887 | file_ptr off; |
9c5bfbb7 | 4888 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
252b5132 RH |
4889 | |
4890 | if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0 | |
4891 | && bfd_get_format (abfd) != bfd_core) | |
4892 | { | |
5c182d5f AM |
4893 | Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd); |
4894 | unsigned int num_sec = elf_numsections (abfd); | |
252b5132 RH |
4895 | Elf_Internal_Shdr **hdrpp; |
4896 | unsigned int i; | |
4897 | ||
4898 | /* Start after the ELF header. */ | |
4899 | off = i_ehdrp->e_ehsize; | |
4900 | ||
4901 | /* We are not creating an executable, which means that we are | |
4902 | not creating a program header, and that the actual order of | |
4903 | the sections in the file is unimportant. */ | |
9ad5cbcf | 4904 | for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++) |
252b5132 RH |
4905 | { |
4906 | Elf_Internal_Shdr *hdr; | |
4907 | ||
4908 | hdr = *hdrpp; | |
e0638f70 AM |
4909 | if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA) |
4910 | && hdr->bfd_section == NULL) | |
9ad5cbcf AM |
4911 | || i == tdata->symtab_section |
4912 | || i == tdata->symtab_shndx_section | |
252b5132 RH |
4913 | || i == tdata->strtab_section) |
4914 | { | |
4915 | hdr->sh_offset = -1; | |
252b5132 | 4916 | } |
9ad5cbcf | 4917 | else |
b34976b6 | 4918 | off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE); |
252b5132 RH |
4919 | } |
4920 | } | |
4921 | else | |
4922 | { | |
f3520d2f AM |
4923 | unsigned int alloc; |
4924 | ||
252b5132 | 4925 | /* Assign file positions for the loaded sections based on the |
08a40648 | 4926 | assignment of sections to segments. */ |
f3520d2f AM |
4927 | if (!assign_file_positions_for_load_sections (abfd, link_info)) |
4928 | return FALSE; | |
4929 | ||
4930 | /* And for non-load sections. */ | |
4931 | if (!assign_file_positions_for_non_load_sections (abfd, link_info)) | |
4932 | return FALSE; | |
4933 | ||
e36284ab AM |
4934 | if (bed->elf_backend_modify_program_headers != NULL) |
4935 | { | |
4936 | if (!(*bed->elf_backend_modify_program_headers) (abfd, link_info)) | |
4937 | return FALSE; | |
4938 | } | |
4939 | ||
f3520d2f AM |
4940 | /* Write out the program headers. */ |
4941 | alloc = tdata->program_header_size / bed->s->sizeof_phdr; | |
4942 | if (bfd_seek (abfd, (bfd_signed_vma) bed->s->sizeof_ehdr, SEEK_SET) != 0 | |
4943 | || bed->s->write_out_phdrs (abfd, tdata->phdr, alloc) != 0) | |
b34976b6 | 4944 | return FALSE; |
252b5132 | 4945 | |
5c182d5f | 4946 | off = tdata->next_file_pos; |
252b5132 RH |
4947 | } |
4948 | ||
4949 | /* Place the section headers. */ | |
45d6a902 | 4950 | off = align_file_position (off, 1 << bed->s->log_file_align); |
252b5132 RH |
4951 | i_ehdrp->e_shoff = off; |
4952 | off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize; | |
4953 | ||
5c182d5f | 4954 | tdata->next_file_pos = off; |
252b5132 | 4955 | |
b34976b6 | 4956 | return TRUE; |
252b5132 RH |
4957 | } |
4958 | ||
b34976b6 | 4959 | static bfd_boolean |
217aa764 | 4960 | prep_headers (bfd *abfd) |
252b5132 | 4961 | { |
3d540e93 | 4962 | Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form. */ |
2b0f7ef9 | 4963 | struct elf_strtab_hash *shstrtab; |
9c5bfbb7 | 4964 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
252b5132 RH |
4965 | |
4966 | i_ehdrp = elf_elfheader (abfd); | |
252b5132 | 4967 | |
2b0f7ef9 | 4968 | shstrtab = _bfd_elf_strtab_init (); |
252b5132 | 4969 | if (shstrtab == NULL) |
b34976b6 | 4970 | return FALSE; |
252b5132 RH |
4971 | |
4972 | elf_shstrtab (abfd) = shstrtab; | |
4973 | ||
4974 | i_ehdrp->e_ident[EI_MAG0] = ELFMAG0; | |
4975 | i_ehdrp->e_ident[EI_MAG1] = ELFMAG1; | |
4976 | i_ehdrp->e_ident[EI_MAG2] = ELFMAG2; | |
4977 | i_ehdrp->e_ident[EI_MAG3] = ELFMAG3; | |
4978 | ||
4979 | i_ehdrp->e_ident[EI_CLASS] = bed->s->elfclass; | |
4980 | i_ehdrp->e_ident[EI_DATA] = | |
4981 | bfd_big_endian (abfd) ? ELFDATA2MSB : ELFDATA2LSB; | |
4982 | i_ehdrp->e_ident[EI_VERSION] = bed->s->ev_current; | |
4983 | ||
252b5132 RH |
4984 | if ((abfd->flags & DYNAMIC) != 0) |
4985 | i_ehdrp->e_type = ET_DYN; | |
4986 | else if ((abfd->flags & EXEC_P) != 0) | |
4987 | i_ehdrp->e_type = ET_EXEC; | |
4988 | else if (bfd_get_format (abfd) == bfd_core) | |
4989 | i_ehdrp->e_type = ET_CORE; | |
4990 | else | |
4991 | i_ehdrp->e_type = ET_REL; | |
4992 | ||
4993 | switch (bfd_get_arch (abfd)) | |
4994 | { | |
4995 | case bfd_arch_unknown: | |
4996 | i_ehdrp->e_machine = EM_NONE; | |
4997 | break; | |
aa4f99bb AO |
4998 | |
4999 | /* There used to be a long list of cases here, each one setting | |
5000 | e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE | |
5001 | in the corresponding bfd definition. To avoid duplication, | |
5002 | the switch was removed. Machines that need special handling | |
5003 | can generally do it in elf_backend_final_write_processing(), | |
5004 | unless they need the information earlier than the final write. | |
5005 | Such need can generally be supplied by replacing the tests for | |
5006 | e_machine with the conditions used to determine it. */ | |
252b5132 | 5007 | default: |
9c5bfbb7 AM |
5008 | i_ehdrp->e_machine = bed->elf_machine_code; |
5009 | } | |
aa4f99bb | 5010 | |
252b5132 RH |
5011 | i_ehdrp->e_version = bed->s->ev_current; |
5012 | i_ehdrp->e_ehsize = bed->s->sizeof_ehdr; | |
5013 | ||
c044fabd | 5014 | /* No program header, for now. */ |
252b5132 RH |
5015 | i_ehdrp->e_phoff = 0; |
5016 | i_ehdrp->e_phentsize = 0; | |
5017 | i_ehdrp->e_phnum = 0; | |
5018 | ||
c044fabd | 5019 | /* Each bfd section is section header entry. */ |
252b5132 RH |
5020 | i_ehdrp->e_entry = bfd_get_start_address (abfd); |
5021 | i_ehdrp->e_shentsize = bed->s->sizeof_shdr; | |
5022 | ||
c044fabd | 5023 | /* If we're building an executable, we'll need a program header table. */ |
252b5132 | 5024 | if (abfd->flags & EXEC_P) |
0e71e495 BE |
5025 | /* It all happens later. */ |
5026 | ; | |
252b5132 RH |
5027 | else |
5028 | { | |
5029 | i_ehdrp->e_phentsize = 0; | |
252b5132 RH |
5030 | i_ehdrp->e_phoff = 0; |
5031 | } | |
5032 | ||
5033 | elf_tdata (abfd)->symtab_hdr.sh_name = | |
b34976b6 | 5034 | (unsigned int) _bfd_elf_strtab_add (shstrtab, ".symtab", FALSE); |
252b5132 | 5035 | elf_tdata (abfd)->strtab_hdr.sh_name = |
b34976b6 | 5036 | (unsigned int) _bfd_elf_strtab_add (shstrtab, ".strtab", FALSE); |
252b5132 | 5037 | elf_tdata (abfd)->shstrtab_hdr.sh_name = |
b34976b6 | 5038 | (unsigned int) _bfd_elf_strtab_add (shstrtab, ".shstrtab", FALSE); |
252b5132 RH |
5039 | if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1 |
5040 | || elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1 | |
5041 | || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1) | |
b34976b6 | 5042 | return FALSE; |
252b5132 | 5043 | |
b34976b6 | 5044 | return TRUE; |
252b5132 RH |
5045 | } |
5046 | ||
5047 | /* Assign file positions for all the reloc sections which are not part | |
5048 | of the loadable file image. */ | |
5049 | ||
5050 | void | |
217aa764 | 5051 | _bfd_elf_assign_file_positions_for_relocs (bfd *abfd) |
252b5132 RH |
5052 | { |
5053 | file_ptr off; | |
9ad5cbcf | 5054 | unsigned int i, num_sec; |
252b5132 RH |
5055 | Elf_Internal_Shdr **shdrpp; |
5056 | ||
5057 | off = elf_tdata (abfd)->next_file_pos; | |
5058 | ||
9ad5cbcf AM |
5059 | num_sec = elf_numsections (abfd); |
5060 | for (i = 1, shdrpp = elf_elfsections (abfd) + 1; i < num_sec; i++, shdrpp++) | |
252b5132 RH |
5061 | { |
5062 | Elf_Internal_Shdr *shdrp; | |
5063 | ||
5064 | shdrp = *shdrpp; | |
5065 | if ((shdrp->sh_type == SHT_REL || shdrp->sh_type == SHT_RELA) | |
5066 | && shdrp->sh_offset == -1) | |
b34976b6 | 5067 | off = _bfd_elf_assign_file_position_for_section (shdrp, off, TRUE); |
252b5132 RH |
5068 | } |
5069 | ||
5070 | elf_tdata (abfd)->next_file_pos = off; | |
5071 | } | |
5072 | ||
b34976b6 | 5073 | bfd_boolean |
217aa764 | 5074 | _bfd_elf_write_object_contents (bfd *abfd) |
252b5132 | 5075 | { |
9c5bfbb7 | 5076 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
252b5132 | 5077 | Elf_Internal_Shdr **i_shdrp; |
b34976b6 | 5078 | bfd_boolean failed; |
9ad5cbcf | 5079 | unsigned int count, num_sec; |
252b5132 RH |
5080 | |
5081 | if (! abfd->output_has_begun | |
217aa764 | 5082 | && ! _bfd_elf_compute_section_file_positions (abfd, NULL)) |
b34976b6 | 5083 | return FALSE; |
252b5132 RH |
5084 | |
5085 | i_shdrp = elf_elfsections (abfd); | |
252b5132 | 5086 | |
b34976b6 | 5087 | failed = FALSE; |
252b5132 RH |
5088 | bfd_map_over_sections (abfd, bed->s->write_relocs, &failed); |
5089 | if (failed) | |
b34976b6 | 5090 | return FALSE; |
252b5132 RH |
5091 | |
5092 | _bfd_elf_assign_file_positions_for_relocs (abfd); | |
5093 | ||
c044fabd | 5094 | /* After writing the headers, we need to write the sections too... */ |
9ad5cbcf AM |
5095 | num_sec = elf_numsections (abfd); |
5096 | for (count = 1; count < num_sec; count++) | |
252b5132 RH |
5097 | { |
5098 | if (bed->elf_backend_section_processing) | |
5099 | (*bed->elf_backend_section_processing) (abfd, i_shdrp[count]); | |
5100 | if (i_shdrp[count]->contents) | |
5101 | { | |
dc810e39 AM |
5102 | bfd_size_type amt = i_shdrp[count]->sh_size; |
5103 | ||
252b5132 | 5104 | if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0 |
dc810e39 | 5105 | || bfd_bwrite (i_shdrp[count]->contents, amt, abfd) != amt) |
b34976b6 | 5106 | return FALSE; |
252b5132 RH |
5107 | } |
5108 | } | |
5109 | ||
5110 | /* Write out the section header names. */ | |
26ae6d5e DJ |
5111 | if (elf_shstrtab (abfd) != NULL |
5112 | && (bfd_seek (abfd, elf_tdata (abfd)->shstrtab_hdr.sh_offset, SEEK_SET) != 0 | |
08a40648 | 5113 | || !_bfd_elf_strtab_emit (abfd, elf_shstrtab (abfd)))) |
b34976b6 | 5114 | return FALSE; |
252b5132 RH |
5115 | |
5116 | if (bed->elf_backend_final_write_processing) | |
5117 | (*bed->elf_backend_final_write_processing) (abfd, | |
5118 | elf_tdata (abfd)->linker); | |
5119 | ||
ff59fc36 RM |
5120 | if (!bed->s->write_shdrs_and_ehdr (abfd)) |
5121 | return FALSE; | |
5122 | ||
5123 | /* This is last since write_shdrs_and_ehdr can touch i_shdrp[0]. */ | |
bfb53a4f RM |
5124 | if (elf_tdata (abfd)->after_write_object_contents) |
5125 | return (*elf_tdata (abfd)->after_write_object_contents) (abfd); | |
ff59fc36 RM |
5126 | |
5127 | return TRUE; | |
252b5132 RH |
5128 | } |
5129 | ||
b34976b6 | 5130 | bfd_boolean |
217aa764 | 5131 | _bfd_elf_write_corefile_contents (bfd *abfd) |
252b5132 | 5132 | { |
c044fabd | 5133 | /* Hopefully this can be done just like an object file. */ |
252b5132 RH |
5134 | return _bfd_elf_write_object_contents (abfd); |
5135 | } | |
c044fabd KH |
5136 | |
5137 | /* Given a section, search the header to find them. */ | |
5138 | ||
cb33740c | 5139 | unsigned int |
198beae2 | 5140 | _bfd_elf_section_from_bfd_section (bfd *abfd, struct bfd_section *asect) |
252b5132 | 5141 | { |
9c5bfbb7 | 5142 | const struct elf_backend_data *bed; |
91d6fa6a | 5143 | unsigned int sec_index; |
252b5132 | 5144 | |
9ad5cbcf AM |
5145 | if (elf_section_data (asect) != NULL |
5146 | && elf_section_data (asect)->this_idx != 0) | |
5147 | return elf_section_data (asect)->this_idx; | |
5148 | ||
5149 | if (bfd_is_abs_section (asect)) | |
91d6fa6a | 5150 | sec_index = SHN_ABS; |
af746e92 | 5151 | else if (bfd_is_com_section (asect)) |
91d6fa6a | 5152 | sec_index = SHN_COMMON; |
af746e92 | 5153 | else if (bfd_is_und_section (asect)) |
91d6fa6a | 5154 | sec_index = SHN_UNDEF; |
af746e92 | 5155 | else |
91d6fa6a | 5156 | sec_index = SHN_BAD; |
252b5132 | 5157 | |
af746e92 | 5158 | bed = get_elf_backend_data (abfd); |
252b5132 RH |
5159 | if (bed->elf_backend_section_from_bfd_section) |
5160 | { | |
91d6fa6a | 5161 | int retval = sec_index; |
9ad5cbcf | 5162 | |
af746e92 AM |
5163 | if ((*bed->elf_backend_section_from_bfd_section) (abfd, asect, &retval)) |
5164 | return retval; | |
252b5132 RH |
5165 | } |
5166 | ||
91d6fa6a | 5167 | if (sec_index == SHN_BAD) |
af746e92 | 5168 | bfd_set_error (bfd_error_nonrepresentable_section); |
252b5132 | 5169 | |
91d6fa6a | 5170 | return sec_index; |
252b5132 RH |
5171 | } |
5172 | ||
5173 | /* Given a BFD symbol, return the index in the ELF symbol table, or -1 | |
5174 | on error. */ | |
5175 | ||
5176 | int | |
217aa764 | 5177 | _bfd_elf_symbol_from_bfd_symbol (bfd *abfd, asymbol **asym_ptr_ptr) |
252b5132 RH |
5178 | { |
5179 | asymbol *asym_ptr = *asym_ptr_ptr; | |
5180 | int idx; | |
5181 | flagword flags = asym_ptr->flags; | |
5182 | ||
5183 | /* When gas creates relocations against local labels, it creates its | |
5184 | own symbol for the section, but does put the symbol into the | |
5185 | symbol chain, so udata is 0. When the linker is generating | |
5186 | relocatable output, this section symbol may be for one of the | |
5187 | input sections rather than the output section. */ | |
5188 | if (asym_ptr->udata.i == 0 | |
5189 | && (flags & BSF_SECTION_SYM) | |
5190 | && asym_ptr->section) | |
5191 | { | |
5372391b | 5192 | asection *sec; |
252b5132 RH |
5193 | int indx; |
5194 | ||
5372391b AM |
5195 | sec = asym_ptr->section; |
5196 | if (sec->owner != abfd && sec->output_section != NULL) | |
5197 | sec = sec->output_section; | |
5198 | if (sec->owner == abfd | |
5199 | && (indx = sec->index) < elf_num_section_syms (abfd) | |
4e89ac30 | 5200 | && elf_section_syms (abfd)[indx] != NULL) |
252b5132 RH |
5201 | asym_ptr->udata.i = elf_section_syms (abfd)[indx]->udata.i; |
5202 | } | |
5203 | ||
5204 | idx = asym_ptr->udata.i; | |
5205 | ||
5206 | if (idx == 0) | |
5207 | { | |
5208 | /* This case can occur when using --strip-symbol on a symbol | |
08a40648 | 5209 | which is used in a relocation entry. */ |
252b5132 | 5210 | (*_bfd_error_handler) |
d003868e AM |
5211 | (_("%B: symbol `%s' required but not present"), |
5212 | abfd, bfd_asymbol_name (asym_ptr)); | |
252b5132 RH |
5213 | bfd_set_error (bfd_error_no_symbols); |
5214 | return -1; | |
5215 | } | |
5216 | ||
5217 | #if DEBUG & 4 | |
5218 | { | |
5219 | fprintf (stderr, | |
9ccb8af9 AM |
5220 | "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8lx\n", |
5221 | (long) asym_ptr, asym_ptr->name, idx, (long) flags); | |
252b5132 RH |
5222 | fflush (stderr); |
5223 | } | |
5224 | #endif | |
5225 | ||
5226 | return idx; | |
5227 | } | |
5228 | ||
84d1d650 | 5229 | /* Rewrite program header information. */ |
252b5132 | 5230 | |
b34976b6 | 5231 | static bfd_boolean |
84d1d650 | 5232 | rewrite_elf_program_header (bfd *ibfd, bfd *obfd) |
252b5132 | 5233 | { |
b34976b6 AM |
5234 | Elf_Internal_Ehdr *iehdr; |
5235 | struct elf_segment_map *map; | |
5236 | struct elf_segment_map *map_first; | |
5237 | struct elf_segment_map **pointer_to_map; | |
5238 | Elf_Internal_Phdr *segment; | |
5239 | asection *section; | |
5240 | unsigned int i; | |
5241 | unsigned int num_segments; | |
5242 | bfd_boolean phdr_included = FALSE; | |
5c44b38e | 5243 | bfd_boolean p_paddr_valid; |
b34976b6 AM |
5244 | bfd_vma maxpagesize; |
5245 | struct elf_segment_map *phdr_adjust_seg = NULL; | |
5246 | unsigned int phdr_adjust_num = 0; | |
9c5bfbb7 | 5247 | const struct elf_backend_data *bed; |
bc67d8a6 | 5248 | |
caf47ea6 | 5249 | bed = get_elf_backend_data (ibfd); |
252b5132 RH |
5250 | iehdr = elf_elfheader (ibfd); |
5251 | ||
bc67d8a6 | 5252 | map_first = NULL; |
c044fabd | 5253 | pointer_to_map = &map_first; |
252b5132 RH |
5254 | |
5255 | num_segments = elf_elfheader (ibfd)->e_phnum; | |
bc67d8a6 NC |
5256 | maxpagesize = get_elf_backend_data (obfd)->maxpagesize; |
5257 | ||
5258 | /* Returns the end address of the segment + 1. */ | |
aecc8f8a AM |
5259 | #define SEGMENT_END(segment, start) \ |
5260 | (start + (segment->p_memsz > segment->p_filesz \ | |
5261 | ? segment->p_memsz : segment->p_filesz)) | |
bc67d8a6 | 5262 | |
eecdbe52 JJ |
5263 | #define SECTION_SIZE(section, segment) \ |
5264 | (((section->flags & (SEC_HAS_CONTENTS | SEC_THREAD_LOCAL)) \ | |
5265 | != SEC_THREAD_LOCAL || segment->p_type == PT_TLS) \ | |
eea6121a | 5266 | ? section->size : 0) |
eecdbe52 | 5267 | |
b34976b6 | 5268 | /* Returns TRUE if the given section is contained within |
bc67d8a6 | 5269 | the given segment. VMA addresses are compared. */ |
aecc8f8a AM |
5270 | #define IS_CONTAINED_BY_VMA(section, segment) \ |
5271 | (section->vma >= segment->p_vaddr \ | |
eecdbe52 | 5272 | && (section->vma + SECTION_SIZE (section, segment) \ |
aecc8f8a | 5273 | <= (SEGMENT_END (segment, segment->p_vaddr)))) |
c044fabd | 5274 | |
b34976b6 | 5275 | /* Returns TRUE if the given section is contained within |
bc67d8a6 | 5276 | the given segment. LMA addresses are compared. */ |
aecc8f8a AM |
5277 | #define IS_CONTAINED_BY_LMA(section, segment, base) \ |
5278 | (section->lma >= base \ | |
eecdbe52 | 5279 | && (section->lma + SECTION_SIZE (section, segment) \ |
aecc8f8a | 5280 | <= SEGMENT_END (segment, base))) |
252b5132 | 5281 | |
0efc80c8 L |
5282 | /* Handle PT_NOTE segment. */ |
5283 | #define IS_NOTE(p, s) \ | |
aecc8f8a | 5284 | (p->p_type == PT_NOTE \ |
0efc80c8 | 5285 | && elf_section_type (s) == SHT_NOTE \ |
aecc8f8a | 5286 | && (bfd_vma) s->filepos >= p->p_offset \ |
cb3ff1e5 | 5287 | && ((bfd_vma) s->filepos + s->size \ |
aecc8f8a | 5288 | <= p->p_offset + p->p_filesz)) |
252b5132 | 5289 | |
0efc80c8 L |
5290 | /* Special case: corefile "NOTE" section containing regs, prpsinfo |
5291 | etc. */ | |
5292 | #define IS_COREFILE_NOTE(p, s) \ | |
5293 | (IS_NOTE (p, s) \ | |
5294 | && bfd_get_format (ibfd) == bfd_core \ | |
5295 | && s->vma == 0 \ | |
5296 | && s->lma == 0) | |
5297 | ||
252b5132 RH |
5298 | /* The complicated case when p_vaddr is 0 is to handle the Solaris |
5299 | linker, which generates a PT_INTERP section with p_vaddr and | |
5300 | p_memsz set to 0. */ | |
aecc8f8a AM |
5301 | #define IS_SOLARIS_PT_INTERP(p, s) \ |
5302 | (p->p_vaddr == 0 \ | |
5303 | && p->p_paddr == 0 \ | |
5304 | && p->p_memsz == 0 \ | |
5305 | && p->p_filesz > 0 \ | |
5306 | && (s->flags & SEC_HAS_CONTENTS) != 0 \ | |
eea6121a | 5307 | && s->size > 0 \ |
aecc8f8a | 5308 | && (bfd_vma) s->filepos >= p->p_offset \ |
cb3ff1e5 | 5309 | && ((bfd_vma) s->filepos + s->size \ |
aecc8f8a | 5310 | <= p->p_offset + p->p_filesz)) |
5c440b1e | 5311 | |
bc67d8a6 NC |
5312 | /* Decide if the given section should be included in the given segment. |
5313 | A section will be included if: | |
f5ffc919 | 5314 | 1. It is within the address space of the segment -- we use the LMA |
08a40648 | 5315 | if that is set for the segment and the VMA otherwise, |
0efc80c8 L |
5316 | 2. It is an allocated section or a NOTE section in a PT_NOTE |
5317 | segment. | |
bc67d8a6 | 5318 | 3. There is an output section associated with it, |
eecdbe52 | 5319 | 4. The section has not already been allocated to a previous segment. |
2b05f1b7 | 5320 | 5. PT_GNU_STACK segments do not include any sections. |
03394ac9 | 5321 | 6. PT_TLS segment includes only SHF_TLS sections. |
6f79b219 JJ |
5322 | 7. SHF_TLS sections are only in PT_TLS or PT_LOAD segments. |
5323 | 8. PT_DYNAMIC should not contain empty sections at the beginning | |
08a40648 | 5324 | (with the possible exception of .dynamic). */ |
9f17e2a6 | 5325 | #define IS_SECTION_IN_INPUT_SEGMENT(section, segment, bed) \ |
2b05f1b7 L |
5326 | ((((segment->p_paddr \ |
5327 | ? IS_CONTAINED_BY_LMA (section, segment, segment->p_paddr) \ | |
5328 | : IS_CONTAINED_BY_VMA (section, segment)) \ | |
5329 | && (section->flags & SEC_ALLOC) != 0) \ | |
0efc80c8 | 5330 | || IS_NOTE (segment, section)) \ |
2b05f1b7 L |
5331 | && segment->p_type != PT_GNU_STACK \ |
5332 | && (segment->p_type != PT_TLS \ | |
5333 | || (section->flags & SEC_THREAD_LOCAL)) \ | |
5334 | && (segment->p_type == PT_LOAD \ | |
5335 | || segment->p_type == PT_TLS \ | |
5336 | || (section->flags & SEC_THREAD_LOCAL) == 0) \ | |
5337 | && (segment->p_type != PT_DYNAMIC \ | |
5338 | || SECTION_SIZE (section, segment) > 0 \ | |
5339 | || (segment->p_paddr \ | |
5340 | ? segment->p_paddr != section->lma \ | |
5341 | : segment->p_vaddr != section->vma) \ | |
5342 | || (strcmp (bfd_get_section_name (ibfd, section), ".dynamic") \ | |
5343 | == 0)) \ | |
0067a569 | 5344 | && !section->segment_mark) |
bc67d8a6 | 5345 | |
9f17e2a6 L |
5346 | /* If the output section of a section in the input segment is NULL, |
5347 | it is removed from the corresponding output segment. */ | |
5348 | #define INCLUDE_SECTION_IN_SEGMENT(section, segment, bed) \ | |
5349 | (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed) \ | |
5350 | && section->output_section != NULL) | |
5351 | ||
b34976b6 | 5352 | /* Returns TRUE iff seg1 starts after the end of seg2. */ |
b5f852ea NC |
5353 | #define SEGMENT_AFTER_SEGMENT(seg1, seg2, field) \ |
5354 | (seg1->field >= SEGMENT_END (seg2, seg2->field)) | |
5355 | ||
5356 | /* Returns TRUE iff seg1 and seg2 overlap. Segments overlap iff both | |
5357 | their VMA address ranges and their LMA address ranges overlap. | |
5358 | It is possible to have overlapping VMA ranges without overlapping LMA | |
5359 | ranges. RedBoot images for example can have both .data and .bss mapped | |
5360 | to the same VMA range, but with the .data section mapped to a different | |
5361 | LMA. */ | |
aecc8f8a | 5362 | #define SEGMENT_OVERLAPS(seg1, seg2) \ |
b5f852ea | 5363 | ( !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_vaddr) \ |
08a40648 | 5364 | || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_vaddr)) \ |
b5f852ea | 5365 | && !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_paddr) \ |
08a40648 | 5366 | || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_paddr))) |
bc67d8a6 NC |
5367 | |
5368 | /* Initialise the segment mark field. */ | |
5369 | for (section = ibfd->sections; section != NULL; section = section->next) | |
b34976b6 | 5370 | section->segment_mark = FALSE; |
bc67d8a6 | 5371 | |
5c44b38e AM |
5372 | /* The Solaris linker creates program headers in which all the |
5373 | p_paddr fields are zero. When we try to objcopy or strip such a | |
5374 | file, we get confused. Check for this case, and if we find it | |
5375 | don't set the p_paddr_valid fields. */ | |
5376 | p_paddr_valid = FALSE; | |
5377 | for (i = 0, segment = elf_tdata (ibfd)->phdr; | |
5378 | i < num_segments; | |
5379 | i++, segment++) | |
5380 | if (segment->p_paddr != 0) | |
5381 | { | |
5382 | p_paddr_valid = TRUE; | |
5383 | break; | |
5384 | } | |
5385 | ||
252b5132 | 5386 | /* Scan through the segments specified in the program header |
bc67d8a6 | 5387 | of the input BFD. For this first scan we look for overlaps |
9ad5cbcf | 5388 | in the loadable segments. These can be created by weird |
aecc8f8a | 5389 | parameters to objcopy. Also, fix some solaris weirdness. */ |
bc67d8a6 NC |
5390 | for (i = 0, segment = elf_tdata (ibfd)->phdr; |
5391 | i < num_segments; | |
c044fabd | 5392 | i++, segment++) |
252b5132 | 5393 | { |
252b5132 | 5394 | unsigned int j; |
c044fabd | 5395 | Elf_Internal_Phdr *segment2; |
252b5132 | 5396 | |
aecc8f8a AM |
5397 | if (segment->p_type == PT_INTERP) |
5398 | for (section = ibfd->sections; section; section = section->next) | |
5399 | if (IS_SOLARIS_PT_INTERP (segment, section)) | |
5400 | { | |
5401 | /* Mininal change so that the normal section to segment | |
4cc11e76 | 5402 | assignment code will work. */ |
aecc8f8a AM |
5403 | segment->p_vaddr = section->vma; |
5404 | break; | |
5405 | } | |
5406 | ||
bc67d8a6 | 5407 | if (segment->p_type != PT_LOAD) |
b10a8ae0 L |
5408 | { |
5409 | /* Remove PT_GNU_RELRO segment. */ | |
5410 | if (segment->p_type == PT_GNU_RELRO) | |
5411 | segment->p_type = PT_NULL; | |
5412 | continue; | |
5413 | } | |
c044fabd | 5414 | |
bc67d8a6 | 5415 | /* Determine if this segment overlaps any previous segments. */ |
0067a569 | 5416 | for (j = 0, segment2 = elf_tdata (ibfd)->phdr; j < i; j++, segment2++) |
bc67d8a6 NC |
5417 | { |
5418 | bfd_signed_vma extra_length; | |
c044fabd | 5419 | |
bc67d8a6 | 5420 | if (segment2->p_type != PT_LOAD |
0067a569 | 5421 | || !SEGMENT_OVERLAPS (segment, segment2)) |
bc67d8a6 | 5422 | continue; |
c044fabd | 5423 | |
bc67d8a6 NC |
5424 | /* Merge the two segments together. */ |
5425 | if (segment2->p_vaddr < segment->p_vaddr) | |
5426 | { | |
c044fabd | 5427 | /* Extend SEGMENT2 to include SEGMENT and then delete |
08a40648 | 5428 | SEGMENT. */ |
0067a569 AM |
5429 | extra_length = (SEGMENT_END (segment, segment->p_vaddr) |
5430 | - SEGMENT_END (segment2, segment2->p_vaddr)); | |
c044fabd | 5431 | |
bc67d8a6 NC |
5432 | if (extra_length > 0) |
5433 | { | |
0067a569 | 5434 | segment2->p_memsz += extra_length; |
bc67d8a6 NC |
5435 | segment2->p_filesz += extra_length; |
5436 | } | |
c044fabd | 5437 | |
bc67d8a6 | 5438 | segment->p_type = PT_NULL; |
c044fabd | 5439 | |
bc67d8a6 NC |
5440 | /* Since we have deleted P we must restart the outer loop. */ |
5441 | i = 0; | |
5442 | segment = elf_tdata (ibfd)->phdr; | |
5443 | break; | |
5444 | } | |
5445 | else | |
5446 | { | |
c044fabd | 5447 | /* Extend SEGMENT to include SEGMENT2 and then delete |
08a40648 | 5448 | SEGMENT2. */ |
0067a569 AM |
5449 | extra_length = (SEGMENT_END (segment2, segment2->p_vaddr) |
5450 | - SEGMENT_END (segment, segment->p_vaddr)); | |
c044fabd | 5451 | |
bc67d8a6 NC |
5452 | if (extra_length > 0) |
5453 | { | |
0067a569 | 5454 | segment->p_memsz += extra_length; |
bc67d8a6 NC |
5455 | segment->p_filesz += extra_length; |
5456 | } | |
c044fabd | 5457 | |
bc67d8a6 NC |
5458 | segment2->p_type = PT_NULL; |
5459 | } | |
5460 | } | |
5461 | } | |
c044fabd | 5462 | |
bc67d8a6 NC |
5463 | /* The second scan attempts to assign sections to segments. */ |
5464 | for (i = 0, segment = elf_tdata (ibfd)->phdr; | |
5465 | i < num_segments; | |
0067a569 | 5466 | i++, segment++) |
bc67d8a6 | 5467 | { |
0067a569 AM |
5468 | unsigned int section_count; |
5469 | asection **sections; | |
5470 | asection *output_section; | |
5471 | unsigned int isec; | |
5472 | bfd_vma matching_lma; | |
5473 | bfd_vma suggested_lma; | |
5474 | unsigned int j; | |
dc810e39 | 5475 | bfd_size_type amt; |
0067a569 AM |
5476 | asection *first_section; |
5477 | bfd_boolean first_matching_lma; | |
5478 | bfd_boolean first_suggested_lma; | |
bc67d8a6 NC |
5479 | |
5480 | if (segment->p_type == PT_NULL) | |
5481 | continue; | |
c044fabd | 5482 | |
9f17e2a6 | 5483 | first_section = NULL; |
bc67d8a6 | 5484 | /* Compute how many sections might be placed into this segment. */ |
b5f852ea NC |
5485 | for (section = ibfd->sections, section_count = 0; |
5486 | section != NULL; | |
5487 | section = section->next) | |
9f17e2a6 L |
5488 | { |
5489 | /* Find the first section in the input segment, which may be | |
5490 | removed from the corresponding output segment. */ | |
5491 | if (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed)) | |
5492 | { | |
5493 | if (first_section == NULL) | |
5494 | first_section = section; | |
5495 | if (section->output_section != NULL) | |
5496 | ++section_count; | |
5497 | } | |
5498 | } | |
811072d8 | 5499 | |
b5f852ea NC |
5500 | /* Allocate a segment map big enough to contain |
5501 | all of the sections we have selected. */ | |
dc810e39 AM |
5502 | amt = sizeof (struct elf_segment_map); |
5503 | amt += ((bfd_size_type) section_count - 1) * sizeof (asection *); | |
a50b1753 | 5504 | map = (struct elf_segment_map *) bfd_zalloc (obfd, amt); |
bc67d8a6 | 5505 | if (map == NULL) |
b34976b6 | 5506 | return FALSE; |
252b5132 RH |
5507 | |
5508 | /* Initialise the fields of the segment map. Default to | |
5509 | using the physical address of the segment in the input BFD. */ | |
0067a569 AM |
5510 | map->next = NULL; |
5511 | map->p_type = segment->p_type; | |
5512 | map->p_flags = segment->p_flags; | |
bc67d8a6 | 5513 | map->p_flags_valid = 1; |
55d55ac7 | 5514 | |
9f17e2a6 L |
5515 | /* If the first section in the input segment is removed, there is |
5516 | no need to preserve segment physical address in the corresponding | |
5517 | output segment. */ | |
945c025a | 5518 | if (!first_section || first_section->output_section != NULL) |
9f17e2a6 L |
5519 | { |
5520 | map->p_paddr = segment->p_paddr; | |
5c44b38e | 5521 | map->p_paddr_valid = p_paddr_valid; |
9f17e2a6 | 5522 | } |
252b5132 RH |
5523 | |
5524 | /* Determine if this segment contains the ELF file header | |
5525 | and if it contains the program headers themselves. */ | |
bc67d8a6 NC |
5526 | map->includes_filehdr = (segment->p_offset == 0 |
5527 | && segment->p_filesz >= iehdr->e_ehsize); | |
bc67d8a6 | 5528 | map->includes_phdrs = 0; |
252b5132 | 5529 | |
0067a569 | 5530 | if (!phdr_included || segment->p_type != PT_LOAD) |
252b5132 | 5531 | { |
bc67d8a6 NC |
5532 | map->includes_phdrs = |
5533 | (segment->p_offset <= (bfd_vma) iehdr->e_phoff | |
5534 | && (segment->p_offset + segment->p_filesz | |
252b5132 RH |
5535 | >= ((bfd_vma) iehdr->e_phoff |
5536 | + iehdr->e_phnum * iehdr->e_phentsize))); | |
c044fabd | 5537 | |
bc67d8a6 | 5538 | if (segment->p_type == PT_LOAD && map->includes_phdrs) |
b34976b6 | 5539 | phdr_included = TRUE; |
252b5132 RH |
5540 | } |
5541 | ||
bc67d8a6 | 5542 | if (section_count == 0) |
252b5132 RH |
5543 | { |
5544 | /* Special segments, such as the PT_PHDR segment, may contain | |
5545 | no sections, but ordinary, loadable segments should contain | |
1ed89aa9 NC |
5546 | something. They are allowed by the ELF spec however, so only |
5547 | a warning is produced. */ | |
bc67d8a6 | 5548 | if (segment->p_type == PT_LOAD) |
0067a569 AM |
5549 | (*_bfd_error_handler) (_("%B: warning: Empty loadable segment" |
5550 | " detected, is this intentional ?\n"), | |
5551 | ibfd); | |
252b5132 | 5552 | |
bc67d8a6 | 5553 | map->count = 0; |
c044fabd KH |
5554 | *pointer_to_map = map; |
5555 | pointer_to_map = &map->next; | |
252b5132 RH |
5556 | |
5557 | continue; | |
5558 | } | |
5559 | ||
5560 | /* Now scan the sections in the input BFD again and attempt | |
5561 | to add their corresponding output sections to the segment map. | |
5562 | The problem here is how to handle an output section which has | |
5563 | been moved (ie had its LMA changed). There are four possibilities: | |
5564 | ||
5565 | 1. None of the sections have been moved. | |
5566 | In this case we can continue to use the segment LMA from the | |
5567 | input BFD. | |
5568 | ||
5569 | 2. All of the sections have been moved by the same amount. | |
5570 | In this case we can change the segment's LMA to match the LMA | |
5571 | of the first section. | |
5572 | ||
5573 | 3. Some of the sections have been moved, others have not. | |
5574 | In this case those sections which have not been moved can be | |
5575 | placed in the current segment which will have to have its size, | |
5576 | and possibly its LMA changed, and a new segment or segments will | |
5577 | have to be created to contain the other sections. | |
5578 | ||
b5f852ea | 5579 | 4. The sections have been moved, but not by the same amount. |
252b5132 RH |
5580 | In this case we can change the segment's LMA to match the LMA |
5581 | of the first section and we will have to create a new segment | |
5582 | or segments to contain the other sections. | |
5583 | ||
5584 | In order to save time, we allocate an array to hold the section | |
5585 | pointers that we are interested in. As these sections get assigned | |
5586 | to a segment, they are removed from this array. */ | |
5587 | ||
a50b1753 | 5588 | sections = (asection **) bfd_malloc2 (section_count, sizeof (asection *)); |
252b5132 | 5589 | if (sections == NULL) |
b34976b6 | 5590 | return FALSE; |
252b5132 RH |
5591 | |
5592 | /* Step One: Scan for segment vs section LMA conflicts. | |
5593 | Also add the sections to the section array allocated above. | |
5594 | Also add the sections to the current segment. In the common | |
5595 | case, where the sections have not been moved, this means that | |
5596 | we have completely filled the segment, and there is nothing | |
5597 | more to do. */ | |
252b5132 | 5598 | isec = 0; |
72730e0c | 5599 | matching_lma = 0; |
252b5132 | 5600 | suggested_lma = 0; |
0067a569 AM |
5601 | first_matching_lma = TRUE; |
5602 | first_suggested_lma = TRUE; | |
252b5132 | 5603 | |
147d51c2 | 5604 | for (section = ibfd->sections; |
bc67d8a6 NC |
5605 | section != NULL; |
5606 | section = section->next) | |
147d51c2 L |
5607 | if (section == first_section) |
5608 | break; | |
5609 | ||
5610 | for (j = 0; section != NULL; section = section->next) | |
252b5132 | 5611 | { |
caf47ea6 | 5612 | if (INCLUDE_SECTION_IN_SEGMENT (section, segment, bed)) |
c0f7859b | 5613 | { |
bc67d8a6 NC |
5614 | output_section = section->output_section; |
5615 | ||
0067a569 | 5616 | sections[j++] = section; |
252b5132 RH |
5617 | |
5618 | /* The Solaris native linker always sets p_paddr to 0. | |
5619 | We try to catch that case here, and set it to the | |
5e8d7549 NC |
5620 | correct value. Note - some backends require that |
5621 | p_paddr be left as zero. */ | |
5c44b38e | 5622 | if (!p_paddr_valid |
4455705d | 5623 | && segment->p_vaddr != 0 |
0067a569 | 5624 | && !bed->want_p_paddr_set_to_zero |
252b5132 | 5625 | && isec == 0 |
bc67d8a6 | 5626 | && output_section->lma != 0 |
0067a569 AM |
5627 | && output_section->vma == (segment->p_vaddr |
5628 | + (map->includes_filehdr | |
5629 | ? iehdr->e_ehsize | |
5630 | : 0) | |
5631 | + (map->includes_phdrs | |
5632 | ? (iehdr->e_phnum | |
5633 | * iehdr->e_phentsize) | |
5634 | : 0))) | |
bc67d8a6 | 5635 | map->p_paddr = segment->p_vaddr; |
252b5132 RH |
5636 | |
5637 | /* Match up the physical address of the segment with the | |
5638 | LMA address of the output section. */ | |
bc67d8a6 | 5639 | if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr) |
5e8d7549 | 5640 | || IS_COREFILE_NOTE (segment, section) |
0067a569 AM |
5641 | || (bed->want_p_paddr_set_to_zero |
5642 | && IS_CONTAINED_BY_VMA (output_section, segment))) | |
252b5132 | 5643 | { |
0067a569 AM |
5644 | if (first_matching_lma || output_section->lma < matching_lma) |
5645 | { | |
5646 | matching_lma = output_section->lma; | |
5647 | first_matching_lma = FALSE; | |
5648 | } | |
252b5132 RH |
5649 | |
5650 | /* We assume that if the section fits within the segment | |
bc67d8a6 | 5651 | then it does not overlap any other section within that |
252b5132 | 5652 | segment. */ |
0067a569 AM |
5653 | map->sections[isec++] = output_section; |
5654 | } | |
5655 | else if (first_suggested_lma) | |
5656 | { | |
5657 | suggested_lma = output_section->lma; | |
5658 | first_suggested_lma = FALSE; | |
252b5132 | 5659 | } |
147d51c2 L |
5660 | |
5661 | if (j == section_count) | |
5662 | break; | |
252b5132 RH |
5663 | } |
5664 | } | |
5665 | ||
bc67d8a6 | 5666 | BFD_ASSERT (j == section_count); |
252b5132 RH |
5667 | |
5668 | /* Step Two: Adjust the physical address of the current segment, | |
5669 | if necessary. */ | |
bc67d8a6 | 5670 | if (isec == section_count) |
252b5132 RH |
5671 | { |
5672 | /* All of the sections fitted within the segment as currently | |
5673 | specified. This is the default case. Add the segment to | |
5674 | the list of built segments and carry on to process the next | |
5675 | program header in the input BFD. */ | |
bc67d8a6 | 5676 | map->count = section_count; |
c044fabd KH |
5677 | *pointer_to_map = map; |
5678 | pointer_to_map = &map->next; | |
08a40648 | 5679 | |
5c44b38e AM |
5680 | if (p_paddr_valid |
5681 | && !bed->want_p_paddr_set_to_zero | |
147d51c2 | 5682 | && matching_lma != map->p_paddr |
5c44b38e AM |
5683 | && !map->includes_filehdr |
5684 | && !map->includes_phdrs) | |
3271a814 NS |
5685 | /* There is some padding before the first section in the |
5686 | segment. So, we must account for that in the output | |
5687 | segment's vma. */ | |
5688 | map->p_vaddr_offset = matching_lma - map->p_paddr; | |
08a40648 | 5689 | |
252b5132 RH |
5690 | free (sections); |
5691 | continue; | |
5692 | } | |
252b5132 RH |
5693 | else |
5694 | { | |
0067a569 | 5695 | if (!first_matching_lma) |
72730e0c AM |
5696 | { |
5697 | /* At least one section fits inside the current segment. | |
5698 | Keep it, but modify its physical address to match the | |
5699 | LMA of the first section that fitted. */ | |
bc67d8a6 | 5700 | map->p_paddr = matching_lma; |
72730e0c AM |
5701 | } |
5702 | else | |
5703 | { | |
5704 | /* None of the sections fitted inside the current segment. | |
5705 | Change the current segment's physical address to match | |
5706 | the LMA of the first section. */ | |
bc67d8a6 | 5707 | map->p_paddr = suggested_lma; |
72730e0c AM |
5708 | } |
5709 | ||
bc67d8a6 NC |
5710 | /* Offset the segment physical address from the lma |
5711 | to allow for space taken up by elf headers. */ | |
5712 | if (map->includes_filehdr) | |
010c8431 AM |
5713 | { |
5714 | if (map->p_paddr >= iehdr->e_ehsize) | |
5715 | map->p_paddr -= iehdr->e_ehsize; | |
5716 | else | |
5717 | { | |
5718 | map->includes_filehdr = FALSE; | |
5719 | map->includes_phdrs = FALSE; | |
5720 | } | |
5721 | } | |
252b5132 | 5722 | |
bc67d8a6 NC |
5723 | if (map->includes_phdrs) |
5724 | { | |
010c8431 AM |
5725 | if (map->p_paddr >= iehdr->e_phnum * iehdr->e_phentsize) |
5726 | { | |
5727 | map->p_paddr -= iehdr->e_phnum * iehdr->e_phentsize; | |
5728 | ||
5729 | /* iehdr->e_phnum is just an estimate of the number | |
5730 | of program headers that we will need. Make a note | |
5731 | here of the number we used and the segment we chose | |
5732 | to hold these headers, so that we can adjust the | |
5733 | offset when we know the correct value. */ | |
5734 | phdr_adjust_num = iehdr->e_phnum; | |
5735 | phdr_adjust_seg = map; | |
5736 | } | |
5737 | else | |
5738 | map->includes_phdrs = FALSE; | |
bc67d8a6 | 5739 | } |
252b5132 RH |
5740 | } |
5741 | ||
5742 | /* Step Three: Loop over the sections again, this time assigning | |
caf47ea6 | 5743 | those that fit to the current segment and removing them from the |
252b5132 RH |
5744 | sections array; but making sure not to leave large gaps. Once all |
5745 | possible sections have been assigned to the current segment it is | |
5746 | added to the list of built segments and if sections still remain | |
5747 | to be assigned, a new segment is constructed before repeating | |
5748 | the loop. */ | |
5749 | isec = 0; | |
5750 | do | |
5751 | { | |
bc67d8a6 | 5752 | map->count = 0; |
252b5132 | 5753 | suggested_lma = 0; |
0067a569 | 5754 | first_suggested_lma = TRUE; |
252b5132 RH |
5755 | |
5756 | /* Fill the current segment with sections that fit. */ | |
bc67d8a6 | 5757 | for (j = 0; j < section_count; j++) |
252b5132 | 5758 | { |
bc67d8a6 | 5759 | section = sections[j]; |
252b5132 | 5760 | |
bc67d8a6 | 5761 | if (section == NULL) |
252b5132 RH |
5762 | continue; |
5763 | ||
bc67d8a6 | 5764 | output_section = section->output_section; |
252b5132 | 5765 | |
bc67d8a6 | 5766 | BFD_ASSERT (output_section != NULL); |
c044fabd | 5767 | |
bc67d8a6 NC |
5768 | if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr) |
5769 | || IS_COREFILE_NOTE (segment, section)) | |
252b5132 | 5770 | { |
bc67d8a6 | 5771 | if (map->count == 0) |
252b5132 RH |
5772 | { |
5773 | /* If the first section in a segment does not start at | |
bc67d8a6 NC |
5774 | the beginning of the segment, then something is |
5775 | wrong. */ | |
0067a569 AM |
5776 | if (output_section->lma |
5777 | != (map->p_paddr | |
5778 | + (map->includes_filehdr ? iehdr->e_ehsize : 0) | |
5779 | + (map->includes_phdrs | |
5780 | ? iehdr->e_phnum * iehdr->e_phentsize | |
5781 | : 0))) | |
252b5132 RH |
5782 | abort (); |
5783 | } | |
5784 | else | |
5785 | { | |
0067a569 | 5786 | asection *prev_sec; |
252b5132 | 5787 | |
bc67d8a6 | 5788 | prev_sec = map->sections[map->count - 1]; |
252b5132 RH |
5789 | |
5790 | /* If the gap between the end of the previous section | |
bc67d8a6 NC |
5791 | and the start of this section is more than |
5792 | maxpagesize then we need to start a new segment. */ | |
eea6121a | 5793 | if ((BFD_ALIGN (prev_sec->lma + prev_sec->size, |
079e9a2f | 5794 | maxpagesize) |
caf47ea6 | 5795 | < BFD_ALIGN (output_section->lma, maxpagesize)) |
0067a569 | 5796 | || (prev_sec->lma + prev_sec->size |
079e9a2f | 5797 | > output_section->lma)) |
252b5132 | 5798 | { |
0067a569 AM |
5799 | if (first_suggested_lma) |
5800 | { | |
5801 | suggested_lma = output_section->lma; | |
5802 | first_suggested_lma = FALSE; | |
5803 | } | |
252b5132 RH |
5804 | |
5805 | continue; | |
5806 | } | |
5807 | } | |
5808 | ||
bc67d8a6 | 5809 | map->sections[map->count++] = output_section; |
252b5132 RH |
5810 | ++isec; |
5811 | sections[j] = NULL; | |
b34976b6 | 5812 | section->segment_mark = TRUE; |
252b5132 | 5813 | } |
0067a569 AM |
5814 | else if (first_suggested_lma) |
5815 | { | |
5816 | suggested_lma = output_section->lma; | |
5817 | first_suggested_lma = FALSE; | |
5818 | } | |
252b5132 RH |
5819 | } |
5820 | ||
bc67d8a6 | 5821 | BFD_ASSERT (map->count > 0); |
252b5132 RH |
5822 | |
5823 | /* Add the current segment to the list of built segments. */ | |
c044fabd KH |
5824 | *pointer_to_map = map; |
5825 | pointer_to_map = &map->next; | |
252b5132 | 5826 | |
bc67d8a6 | 5827 | if (isec < section_count) |
252b5132 RH |
5828 | { |
5829 | /* We still have not allocated all of the sections to | |
5830 | segments. Create a new segment here, initialise it | |
5831 | and carry on looping. */ | |
dc810e39 AM |
5832 | amt = sizeof (struct elf_segment_map); |
5833 | amt += ((bfd_size_type) section_count - 1) * sizeof (asection *); | |
a50b1753 | 5834 | map = (struct elf_segment_map *) bfd_alloc (obfd, amt); |
bc67d8a6 | 5835 | if (map == NULL) |
5ed6aba4 NC |
5836 | { |
5837 | free (sections); | |
5838 | return FALSE; | |
5839 | } | |
252b5132 RH |
5840 | |
5841 | /* Initialise the fields of the segment map. Set the physical | |
5842 | physical address to the LMA of the first section that has | |
5843 | not yet been assigned. */ | |
0067a569 AM |
5844 | map->next = NULL; |
5845 | map->p_type = segment->p_type; | |
5846 | map->p_flags = segment->p_flags; | |
5847 | map->p_flags_valid = 1; | |
5848 | map->p_paddr = suggested_lma; | |
5c44b38e | 5849 | map->p_paddr_valid = p_paddr_valid; |
bc67d8a6 | 5850 | map->includes_filehdr = 0; |
0067a569 | 5851 | map->includes_phdrs = 0; |
252b5132 RH |
5852 | } |
5853 | } | |
bc67d8a6 | 5854 | while (isec < section_count); |
252b5132 RH |
5855 | |
5856 | free (sections); | |
5857 | } | |
5858 | ||
bc67d8a6 NC |
5859 | elf_tdata (obfd)->segment_map = map_first; |
5860 | ||
5861 | /* If we had to estimate the number of program headers that were | |
9ad5cbcf | 5862 | going to be needed, then check our estimate now and adjust |
bc67d8a6 NC |
5863 | the offset if necessary. */ |
5864 | if (phdr_adjust_seg != NULL) | |
5865 | { | |
5866 | unsigned int count; | |
c044fabd | 5867 | |
bc67d8a6 | 5868 | for (count = 0, map = map_first; map != NULL; map = map->next) |
c044fabd | 5869 | count++; |
252b5132 | 5870 | |
bc67d8a6 NC |
5871 | if (count > phdr_adjust_num) |
5872 | phdr_adjust_seg->p_paddr | |
5873 | -= (count - phdr_adjust_num) * iehdr->e_phentsize; | |
5874 | } | |
c044fabd | 5875 | |
bc67d8a6 | 5876 | #undef SEGMENT_END |
eecdbe52 | 5877 | #undef SECTION_SIZE |
bc67d8a6 NC |
5878 | #undef IS_CONTAINED_BY_VMA |
5879 | #undef IS_CONTAINED_BY_LMA | |
0efc80c8 | 5880 | #undef IS_NOTE |
252b5132 | 5881 | #undef IS_COREFILE_NOTE |
bc67d8a6 | 5882 | #undef IS_SOLARIS_PT_INTERP |
9f17e2a6 | 5883 | #undef IS_SECTION_IN_INPUT_SEGMENT |
bc67d8a6 NC |
5884 | #undef INCLUDE_SECTION_IN_SEGMENT |
5885 | #undef SEGMENT_AFTER_SEGMENT | |
5886 | #undef SEGMENT_OVERLAPS | |
b34976b6 | 5887 | return TRUE; |
252b5132 RH |
5888 | } |
5889 | ||
84d1d650 L |
5890 | /* Copy ELF program header information. */ |
5891 | ||
5892 | static bfd_boolean | |
5893 | copy_elf_program_header (bfd *ibfd, bfd *obfd) | |
5894 | { | |
5895 | Elf_Internal_Ehdr *iehdr; | |
5896 | struct elf_segment_map *map; | |
5897 | struct elf_segment_map *map_first; | |
5898 | struct elf_segment_map **pointer_to_map; | |
5899 | Elf_Internal_Phdr *segment; | |
5900 | unsigned int i; | |
5901 | unsigned int num_segments; | |
5902 | bfd_boolean phdr_included = FALSE; | |
88967714 | 5903 | bfd_boolean p_paddr_valid; |
84d1d650 L |
5904 | |
5905 | iehdr = elf_elfheader (ibfd); | |
5906 | ||
5907 | map_first = NULL; | |
5908 | pointer_to_map = &map_first; | |
5909 | ||
88967714 AM |
5910 | /* If all the segment p_paddr fields are zero, don't set |
5911 | map->p_paddr_valid. */ | |
5912 | p_paddr_valid = FALSE; | |
84d1d650 | 5913 | num_segments = elf_elfheader (ibfd)->e_phnum; |
88967714 AM |
5914 | for (i = 0, segment = elf_tdata (ibfd)->phdr; |
5915 | i < num_segments; | |
5916 | i++, segment++) | |
5917 | if (segment->p_paddr != 0) | |
5918 | { | |
5919 | p_paddr_valid = TRUE; | |
5920 | break; | |
5921 | } | |
5922 | ||
84d1d650 L |
5923 | for (i = 0, segment = elf_tdata (ibfd)->phdr; |
5924 | i < num_segments; | |
5925 | i++, segment++) | |
5926 | { | |
5927 | asection *section; | |
5928 | unsigned int section_count; | |
5929 | bfd_size_type amt; | |
5930 | Elf_Internal_Shdr *this_hdr; | |
53020534 | 5931 | asection *first_section = NULL; |
a76e6f2f | 5932 | asection *lowest_section; |
84d1d650 | 5933 | |
84d1d650 L |
5934 | /* Compute how many sections are in this segment. */ |
5935 | for (section = ibfd->sections, section_count = 0; | |
5936 | section != NULL; | |
5937 | section = section->next) | |
5938 | { | |
5939 | this_hdr = &(elf_section_data(section)->this_hdr); | |
f4638467 | 5940 | if (ELF_SECTION_IN_SEGMENT (this_hdr, segment)) |
3271a814 | 5941 | { |
a76e6f2f AM |
5942 | if (first_section == NULL) |
5943 | first_section = section; | |
3271a814 NS |
5944 | section_count++; |
5945 | } | |
84d1d650 L |
5946 | } |
5947 | ||
5948 | /* Allocate a segment map big enough to contain | |
5949 | all of the sections we have selected. */ | |
5950 | amt = sizeof (struct elf_segment_map); | |
5951 | if (section_count != 0) | |
5952 | amt += ((bfd_size_type) section_count - 1) * sizeof (asection *); | |
a50b1753 | 5953 | map = (struct elf_segment_map *) bfd_zalloc (obfd, amt); |
84d1d650 L |
5954 | if (map == NULL) |
5955 | return FALSE; | |
5956 | ||
5957 | /* Initialize the fields of the output segment map with the | |
5958 | input segment. */ | |
5959 | map->next = NULL; | |
5960 | map->p_type = segment->p_type; | |
5961 | map->p_flags = segment->p_flags; | |
5962 | map->p_flags_valid = 1; | |
5963 | map->p_paddr = segment->p_paddr; | |
88967714 | 5964 | map->p_paddr_valid = p_paddr_valid; |
3f570048 AM |
5965 | map->p_align = segment->p_align; |
5966 | map->p_align_valid = 1; | |
3271a814 | 5967 | map->p_vaddr_offset = 0; |
84d1d650 | 5968 | |
9433b9b1 | 5969 | if (map->p_type == PT_GNU_RELRO) |
b10a8ae0 L |
5970 | { |
5971 | /* The PT_GNU_RELRO segment may contain the first a few | |
5972 | bytes in the .got.plt section even if the whole .got.plt | |
5973 | section isn't in the PT_GNU_RELRO segment. We won't | |
5974 | change the size of the PT_GNU_RELRO segment. */ | |
9433b9b1 | 5975 | map->p_size = segment->p_memsz; |
b10a8ae0 L |
5976 | map->p_size_valid = 1; |
5977 | } | |
5978 | ||
84d1d650 L |
5979 | /* Determine if this segment contains the ELF file header |
5980 | and if it contains the program headers themselves. */ | |
5981 | map->includes_filehdr = (segment->p_offset == 0 | |
5982 | && segment->p_filesz >= iehdr->e_ehsize); | |
5983 | ||
5984 | map->includes_phdrs = 0; | |
5985 | if (! phdr_included || segment->p_type != PT_LOAD) | |
5986 | { | |
5987 | map->includes_phdrs = | |
5988 | (segment->p_offset <= (bfd_vma) iehdr->e_phoff | |
5989 | && (segment->p_offset + segment->p_filesz | |
5990 | >= ((bfd_vma) iehdr->e_phoff | |
5991 | + iehdr->e_phnum * iehdr->e_phentsize))); | |
5992 | ||
5993 | if (segment->p_type == PT_LOAD && map->includes_phdrs) | |
5994 | phdr_included = TRUE; | |
5995 | } | |
5996 | ||
a76e6f2f | 5997 | lowest_section = first_section; |
84d1d650 L |
5998 | if (section_count != 0) |
5999 | { | |
6000 | unsigned int isec = 0; | |
6001 | ||
53020534 | 6002 | for (section = first_section; |
84d1d650 L |
6003 | section != NULL; |
6004 | section = section->next) | |
6005 | { | |
6006 | this_hdr = &(elf_section_data(section)->this_hdr); | |
f4638467 | 6007 | if (ELF_SECTION_IN_SEGMENT (this_hdr, segment)) |
53020534 L |
6008 | { |
6009 | map->sections[isec++] = section->output_section; | |
a76e6f2f AM |
6010 | if (section->lma < lowest_section->lma) |
6011 | lowest_section = section; | |
6012 | if ((section->flags & SEC_ALLOC) != 0) | |
6013 | { | |
6014 | bfd_vma seg_off; | |
6015 | ||
6016 | /* Section lmas are set up from PT_LOAD header | |
6017 | p_paddr in _bfd_elf_make_section_from_shdr. | |
6018 | If this header has a p_paddr that disagrees | |
6019 | with the section lma, flag the p_paddr as | |
6020 | invalid. */ | |
6021 | if ((section->flags & SEC_LOAD) != 0) | |
6022 | seg_off = this_hdr->sh_offset - segment->p_offset; | |
6023 | else | |
6024 | seg_off = this_hdr->sh_addr - segment->p_vaddr; | |
6025 | if (section->lma - segment->p_paddr != seg_off) | |
6026 | map->p_paddr_valid = FALSE; | |
6027 | } | |
53020534 L |
6028 | if (isec == section_count) |
6029 | break; | |
6030 | } | |
84d1d650 L |
6031 | } |
6032 | } | |
6033 | ||
a76e6f2f AM |
6034 | if (map->includes_filehdr && lowest_section != NULL) |
6035 | /* We need to keep the space used by the headers fixed. */ | |
6036 | map->header_size = lowest_section->vma - segment->p_vaddr; | |
6037 | ||
6038 | if (!map->includes_phdrs | |
6039 | && !map->includes_filehdr | |
6040 | && map->p_paddr_valid) | |
6041 | /* There is some other padding before the first section. */ | |
6042 | map->p_vaddr_offset = ((lowest_section ? lowest_section->lma : 0) | |
6043 | - segment->p_paddr); | |
6044 | ||
84d1d650 L |
6045 | map->count = section_count; |
6046 | *pointer_to_map = map; | |
6047 | pointer_to_map = &map->next; | |
6048 | } | |
6049 | ||
6050 | elf_tdata (obfd)->segment_map = map_first; | |
6051 | return TRUE; | |
6052 | } | |
6053 | ||
6054 | /* Copy private BFD data. This copies or rewrites ELF program header | |
6055 | information. */ | |
6056 | ||
6057 | static bfd_boolean | |
6058 | copy_private_bfd_data (bfd *ibfd, bfd *obfd) | |
6059 | { | |
84d1d650 L |
6060 | if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour |
6061 | || bfd_get_flavour (obfd) != bfd_target_elf_flavour) | |
6062 | return TRUE; | |
6063 | ||
6064 | if (elf_tdata (ibfd)->phdr == NULL) | |
6065 | return TRUE; | |
6066 | ||
6067 | if (ibfd->xvec == obfd->xvec) | |
6068 | { | |
cb3ff1e5 NC |
6069 | /* Check to see if any sections in the input BFD |
6070 | covered by ELF program header have changed. */ | |
d55ce4e2 | 6071 | Elf_Internal_Phdr *segment; |
84d1d650 L |
6072 | asection *section, *osec; |
6073 | unsigned int i, num_segments; | |
6074 | Elf_Internal_Shdr *this_hdr; | |
147d51c2 L |
6075 | const struct elf_backend_data *bed; |
6076 | ||
6077 | bed = get_elf_backend_data (ibfd); | |
6078 | ||
6079 | /* Regenerate the segment map if p_paddr is set to 0. */ | |
6080 | if (bed->want_p_paddr_set_to_zero) | |
6081 | goto rewrite; | |
84d1d650 L |
6082 | |
6083 | /* Initialize the segment mark field. */ | |
6084 | for (section = obfd->sections; section != NULL; | |
6085 | section = section->next) | |
6086 | section->segment_mark = FALSE; | |
6087 | ||
6088 | num_segments = elf_elfheader (ibfd)->e_phnum; | |
6089 | for (i = 0, segment = elf_tdata (ibfd)->phdr; | |
6090 | i < num_segments; | |
6091 | i++, segment++) | |
6092 | { | |
5f6999aa NC |
6093 | /* PR binutils/3535. The Solaris linker always sets the p_paddr |
6094 | and p_memsz fields of special segments (DYNAMIC, INTERP) to 0 | |
6095 | which severly confuses things, so always regenerate the segment | |
6096 | map in this case. */ | |
6097 | if (segment->p_paddr == 0 | |
6098 | && segment->p_memsz == 0 | |
6099 | && (segment->p_type == PT_INTERP || segment->p_type == PT_DYNAMIC)) | |
cb3ff1e5 | 6100 | goto rewrite; |
5f6999aa | 6101 | |
84d1d650 L |
6102 | for (section = ibfd->sections; |
6103 | section != NULL; section = section->next) | |
6104 | { | |
6105 | /* We mark the output section so that we know it comes | |
6106 | from the input BFD. */ | |
6107 | osec = section->output_section; | |
6108 | if (osec) | |
6109 | osec->segment_mark = TRUE; | |
6110 | ||
6111 | /* Check if this section is covered by the segment. */ | |
6112 | this_hdr = &(elf_section_data(section)->this_hdr); | |
f4638467 | 6113 | if (ELF_SECTION_IN_SEGMENT (this_hdr, segment)) |
84d1d650 L |
6114 | { |
6115 | /* FIXME: Check if its output section is changed or | |
6116 | removed. What else do we need to check? */ | |
6117 | if (osec == NULL | |
6118 | || section->flags != osec->flags | |
6119 | || section->lma != osec->lma | |
6120 | || section->vma != osec->vma | |
6121 | || section->size != osec->size | |
6122 | || section->rawsize != osec->rawsize | |
6123 | || section->alignment_power != osec->alignment_power) | |
6124 | goto rewrite; | |
6125 | } | |
6126 | } | |
6127 | } | |
6128 | ||
cb3ff1e5 | 6129 | /* Check to see if any output section do not come from the |
84d1d650 L |
6130 | input BFD. */ |
6131 | for (section = obfd->sections; section != NULL; | |
6132 | section = section->next) | |
6133 | { | |
6134 | if (section->segment_mark == FALSE) | |
6135 | goto rewrite; | |
6136 | else | |
6137 | section->segment_mark = FALSE; | |
6138 | } | |
6139 | ||
6140 | return copy_elf_program_header (ibfd, obfd); | |
6141 | } | |
6142 | ||
6143 | rewrite: | |
6144 | return rewrite_elf_program_header (ibfd, obfd); | |
6145 | } | |
6146 | ||
ccd2ec6a L |
6147 | /* Initialize private output section information from input section. */ |
6148 | ||
6149 | bfd_boolean | |
6150 | _bfd_elf_init_private_section_data (bfd *ibfd, | |
6151 | asection *isec, | |
6152 | bfd *obfd, | |
6153 | asection *osec, | |
6154 | struct bfd_link_info *link_info) | |
6155 | ||
6156 | { | |
6157 | Elf_Internal_Shdr *ihdr, *ohdr; | |
dfa7b0b8 | 6158 | bfd_boolean final_link = link_info != NULL && !link_info->relocatable; |
ccd2ec6a L |
6159 | |
6160 | if (ibfd->xvec->flavour != bfd_target_elf_flavour | |
6161 | || obfd->xvec->flavour != bfd_target_elf_flavour) | |
6162 | return TRUE; | |
6163 | ||
dfa7b0b8 AM |
6164 | /* For objcopy and relocatable link, don't copy the output ELF |
6165 | section type from input if the output BFD section flags have been | |
6166 | set to something different. For a final link allow some flags | |
6167 | that the linker clears to differ. */ | |
42bb2e33 | 6168 | if (elf_section_type (osec) == SHT_NULL |
dfa7b0b8 AM |
6169 | && (osec->flags == isec->flags |
6170 | || (final_link | |
6171 | && ((osec->flags ^ isec->flags) | |
0814be7d | 6172 | & ~(SEC_LINK_ONCE | SEC_LINK_DUPLICATES | SEC_RELOC)) == 0))) |
42bb2e33 | 6173 | elf_section_type (osec) = elf_section_type (isec); |
d270463e L |
6174 | |
6175 | /* FIXME: Is this correct for all OS/PROC specific flags? */ | |
6176 | elf_section_flags (osec) |= (elf_section_flags (isec) | |
6177 | & (SHF_MASKOS | SHF_MASKPROC)); | |
ccd2ec6a L |
6178 | |
6179 | /* Set things up for objcopy and relocatable link. The output | |
6180 | SHT_GROUP section will have its elf_next_in_group pointing back | |
6181 | to the input group members. Ignore linker created group section. | |
6182 | See elfNN_ia64_object_p in elfxx-ia64.c. */ | |
dfa7b0b8 | 6183 | if (!final_link) |
ccd2ec6a L |
6184 | { |
6185 | if (elf_sec_group (isec) == NULL | |
6186 | || (elf_sec_group (isec)->flags & SEC_LINKER_CREATED) == 0) | |
6187 | { | |
6188 | if (elf_section_flags (isec) & SHF_GROUP) | |
6189 | elf_section_flags (osec) |= SHF_GROUP; | |
6190 | elf_next_in_group (osec) = elf_next_in_group (isec); | |
9659de1c | 6191 | elf_section_data (osec)->group = elf_section_data (isec)->group; |
ccd2ec6a L |
6192 | } |
6193 | } | |
6194 | ||
6195 | ihdr = &elf_section_data (isec)->this_hdr; | |
6196 | ||
6197 | /* We need to handle elf_linked_to_section for SHF_LINK_ORDER. We | |
6198 | don't use the output section of the linked-to section since it | |
6199 | may be NULL at this point. */ | |
6200 | if ((ihdr->sh_flags & SHF_LINK_ORDER) != 0) | |
6201 | { | |
6202 | ohdr = &elf_section_data (osec)->this_hdr; | |
6203 | ohdr->sh_flags |= SHF_LINK_ORDER; | |
6204 | elf_linked_to_section (osec) = elf_linked_to_section (isec); | |
6205 | } | |
6206 | ||
6207 | osec->use_rela_p = isec->use_rela_p; | |
6208 | ||
6209 | return TRUE; | |
6210 | } | |
6211 | ||
252b5132 RH |
6212 | /* Copy private section information. This copies over the entsize |
6213 | field, and sometimes the info field. */ | |
6214 | ||
b34976b6 | 6215 | bfd_boolean |
217aa764 AM |
6216 | _bfd_elf_copy_private_section_data (bfd *ibfd, |
6217 | asection *isec, | |
6218 | bfd *obfd, | |
6219 | asection *osec) | |
252b5132 RH |
6220 | { |
6221 | Elf_Internal_Shdr *ihdr, *ohdr; | |
6222 | ||
6223 | if (ibfd->xvec->flavour != bfd_target_elf_flavour | |
6224 | || obfd->xvec->flavour != bfd_target_elf_flavour) | |
b34976b6 | 6225 | return TRUE; |
252b5132 | 6226 | |
252b5132 RH |
6227 | ihdr = &elf_section_data (isec)->this_hdr; |
6228 | ohdr = &elf_section_data (osec)->this_hdr; | |
6229 | ||
6230 | ohdr->sh_entsize = ihdr->sh_entsize; | |
6231 | ||
6232 | if (ihdr->sh_type == SHT_SYMTAB | |
6233 | || ihdr->sh_type == SHT_DYNSYM | |
6234 | || ihdr->sh_type == SHT_GNU_verneed | |
6235 | || ihdr->sh_type == SHT_GNU_verdef) | |
6236 | ohdr->sh_info = ihdr->sh_info; | |
6237 | ||
ccd2ec6a L |
6238 | return _bfd_elf_init_private_section_data (ibfd, isec, obfd, osec, |
6239 | NULL); | |
252b5132 RH |
6240 | } |
6241 | ||
d0bf826b AM |
6242 | /* Look at all the SHT_GROUP sections in IBFD, making any adjustments |
6243 | necessary if we are removing either the SHT_GROUP section or any of | |
6244 | the group member sections. DISCARDED is the value that a section's | |
6245 | output_section has if the section will be discarded, NULL when this | |
6246 | function is called from objcopy, bfd_abs_section_ptr when called | |
6247 | from the linker. */ | |
80fccad2 BW |
6248 | |
6249 | bfd_boolean | |
d0bf826b | 6250 | _bfd_elf_fixup_group_sections (bfd *ibfd, asection *discarded) |
80fccad2 | 6251 | { |
30288845 AM |
6252 | asection *isec; |
6253 | ||
30288845 | 6254 | for (isec = ibfd->sections; isec != NULL; isec = isec->next) |
415f38a6 | 6255 | if (elf_section_type (isec) == SHT_GROUP) |
30288845 AM |
6256 | { |
6257 | asection *first = elf_next_in_group (isec); | |
6258 | asection *s = first; | |
d0bf826b AM |
6259 | bfd_size_type removed = 0; |
6260 | ||
30288845 AM |
6261 | while (s != NULL) |
6262 | { | |
415f38a6 AM |
6263 | /* If this member section is being output but the |
6264 | SHT_GROUP section is not, then clear the group info | |
6265 | set up by _bfd_elf_copy_private_section_data. */ | |
d0bf826b AM |
6266 | if (s->output_section != discarded |
6267 | && isec->output_section == discarded) | |
30288845 AM |
6268 | { |
6269 | elf_section_flags (s->output_section) &= ~SHF_GROUP; | |
6270 | elf_group_name (s->output_section) = NULL; | |
6271 | } | |
415f38a6 AM |
6272 | /* Conversely, if the member section is not being output |
6273 | but the SHT_GROUP section is, then adjust its size. */ | |
d0bf826b AM |
6274 | else if (s->output_section == discarded |
6275 | && isec->output_section != discarded) | |
6276 | removed += 4; | |
30288845 AM |
6277 | s = elf_next_in_group (s); |
6278 | if (s == first) | |
6279 | break; | |
6280 | } | |
d0bf826b AM |
6281 | if (removed != 0) |
6282 | { | |
6283 | if (discarded != NULL) | |
6284 | { | |
6285 | /* If we've been called for ld -r, then we need to | |
6286 | adjust the input section size. This function may | |
6287 | be called multiple times, so save the original | |
6288 | size. */ | |
6289 | if (isec->rawsize == 0) | |
6290 | isec->rawsize = isec->size; | |
6291 | isec->size = isec->rawsize - removed; | |
6292 | } | |
6293 | else | |
6294 | { | |
6295 | /* Adjust the output section size when called from | |
6296 | objcopy. */ | |
6297 | isec->output_section->size -= removed; | |
6298 | } | |
6299 | } | |
30288845 AM |
6300 | } |
6301 | ||
80fccad2 BW |
6302 | return TRUE; |
6303 | } | |
6304 | ||
d0bf826b AM |
6305 | /* Copy private header information. */ |
6306 | ||
6307 | bfd_boolean | |
6308 | _bfd_elf_copy_private_header_data (bfd *ibfd, bfd *obfd) | |
6309 | { | |
6310 | if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour | |
6311 | || bfd_get_flavour (obfd) != bfd_target_elf_flavour) | |
6312 | return TRUE; | |
6313 | ||
6314 | /* Copy over private BFD data if it has not already been copied. | |
6315 | This must be done here, rather than in the copy_private_bfd_data | |
6316 | entry point, because the latter is called after the section | |
6317 | contents have been set, which means that the program headers have | |
6318 | already been worked out. */ | |
6319 | if (elf_tdata (obfd)->segment_map == NULL && elf_tdata (ibfd)->phdr != NULL) | |
6320 | { | |
6321 | if (! copy_private_bfd_data (ibfd, obfd)) | |
6322 | return FALSE; | |
6323 | } | |
6324 | ||
6325 | return _bfd_elf_fixup_group_sections (ibfd, NULL); | |
6326 | } | |
6327 | ||
252b5132 RH |
6328 | /* Copy private symbol information. If this symbol is in a section |
6329 | which we did not map into a BFD section, try to map the section | |
6330 | index correctly. We use special macro definitions for the mapped | |
6331 | section indices; these definitions are interpreted by the | |
6332 | swap_out_syms function. */ | |
6333 | ||
9ad5cbcf AM |
6334 | #define MAP_ONESYMTAB (SHN_HIOS + 1) |
6335 | #define MAP_DYNSYMTAB (SHN_HIOS + 2) | |
6336 | #define MAP_STRTAB (SHN_HIOS + 3) | |
6337 | #define MAP_SHSTRTAB (SHN_HIOS + 4) | |
6338 | #define MAP_SYM_SHNDX (SHN_HIOS + 5) | |
252b5132 | 6339 | |
b34976b6 | 6340 | bfd_boolean |
217aa764 AM |
6341 | _bfd_elf_copy_private_symbol_data (bfd *ibfd, |
6342 | asymbol *isymarg, | |
6343 | bfd *obfd, | |
6344 | asymbol *osymarg) | |
252b5132 RH |
6345 | { |
6346 | elf_symbol_type *isym, *osym; | |
6347 | ||
6348 | if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour | |
6349 | || bfd_get_flavour (obfd) != bfd_target_elf_flavour) | |
b34976b6 | 6350 | return TRUE; |
252b5132 RH |
6351 | |
6352 | isym = elf_symbol_from (ibfd, isymarg); | |
6353 | osym = elf_symbol_from (obfd, osymarg); | |
6354 | ||
6355 | if (isym != NULL | |
8424d8f5 | 6356 | && isym->internal_elf_sym.st_shndx != 0 |
252b5132 RH |
6357 | && osym != NULL |
6358 | && bfd_is_abs_section (isym->symbol.section)) | |
6359 | { | |
6360 | unsigned int shndx; | |
6361 | ||
6362 | shndx = isym->internal_elf_sym.st_shndx; | |
6363 | if (shndx == elf_onesymtab (ibfd)) | |
6364 | shndx = MAP_ONESYMTAB; | |
6365 | else if (shndx == elf_dynsymtab (ibfd)) | |
6366 | shndx = MAP_DYNSYMTAB; | |
6367 | else if (shndx == elf_tdata (ibfd)->strtab_section) | |
6368 | shndx = MAP_STRTAB; | |
6369 | else if (shndx == elf_tdata (ibfd)->shstrtab_section) | |
6370 | shndx = MAP_SHSTRTAB; | |
9ad5cbcf AM |
6371 | else if (shndx == elf_tdata (ibfd)->symtab_shndx_section) |
6372 | shndx = MAP_SYM_SHNDX; | |
252b5132 RH |
6373 | osym->internal_elf_sym.st_shndx = shndx; |
6374 | } | |
6375 | ||
b34976b6 | 6376 | return TRUE; |
252b5132 RH |
6377 | } |
6378 | ||
6379 | /* Swap out the symbols. */ | |
6380 | ||
b34976b6 | 6381 | static bfd_boolean |
217aa764 AM |
6382 | swap_out_syms (bfd *abfd, |
6383 | struct bfd_strtab_hash **sttp, | |
6384 | int relocatable_p) | |
252b5132 | 6385 | { |
9c5bfbb7 | 6386 | const struct elf_backend_data *bed; |
079e9a2f AM |
6387 | int symcount; |
6388 | asymbol **syms; | |
6389 | struct bfd_strtab_hash *stt; | |
6390 | Elf_Internal_Shdr *symtab_hdr; | |
9ad5cbcf | 6391 | Elf_Internal_Shdr *symtab_shndx_hdr; |
079e9a2f | 6392 | Elf_Internal_Shdr *symstrtab_hdr; |
f075ee0c AM |
6393 | bfd_byte *outbound_syms; |
6394 | bfd_byte *outbound_shndx; | |
079e9a2f AM |
6395 | int idx; |
6396 | bfd_size_type amt; | |
174fd7f9 | 6397 | bfd_boolean name_local_sections; |
252b5132 RH |
6398 | |
6399 | if (!elf_map_symbols (abfd)) | |
b34976b6 | 6400 | return FALSE; |
252b5132 | 6401 | |
c044fabd | 6402 | /* Dump out the symtabs. */ |
079e9a2f AM |
6403 | stt = _bfd_elf_stringtab_init (); |
6404 | if (stt == NULL) | |
b34976b6 | 6405 | return FALSE; |
252b5132 | 6406 | |
079e9a2f AM |
6407 | bed = get_elf_backend_data (abfd); |
6408 | symcount = bfd_get_symcount (abfd); | |
6409 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; | |
6410 | symtab_hdr->sh_type = SHT_SYMTAB; | |
6411 | symtab_hdr->sh_entsize = bed->s->sizeof_sym; | |
6412 | symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1); | |
6413 | symtab_hdr->sh_info = elf_num_locals (abfd) + 1; | |
72de5009 | 6414 | symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align; |
079e9a2f AM |
6415 | |
6416 | symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr; | |
6417 | symstrtab_hdr->sh_type = SHT_STRTAB; | |
6418 | ||
a50b1753 NC |
6419 | outbound_syms = (bfd_byte *) bfd_alloc2 (abfd, 1 + symcount, |
6420 | bed->s->sizeof_sym); | |
079e9a2f | 6421 | if (outbound_syms == NULL) |
5ed6aba4 NC |
6422 | { |
6423 | _bfd_stringtab_free (stt); | |
6424 | return FALSE; | |
6425 | } | |
217aa764 | 6426 | symtab_hdr->contents = outbound_syms; |
252b5132 | 6427 | |
9ad5cbcf AM |
6428 | outbound_shndx = NULL; |
6429 | symtab_shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr; | |
6430 | if (symtab_shndx_hdr->sh_name != 0) | |
6431 | { | |
6432 | amt = (bfd_size_type) (1 + symcount) * sizeof (Elf_External_Sym_Shndx); | |
a50b1753 NC |
6433 | outbound_shndx = (bfd_byte *) |
6434 | bfd_zalloc2 (abfd, 1 + symcount, sizeof (Elf_External_Sym_Shndx)); | |
9ad5cbcf | 6435 | if (outbound_shndx == NULL) |
5ed6aba4 NC |
6436 | { |
6437 | _bfd_stringtab_free (stt); | |
6438 | return FALSE; | |
6439 | } | |
6440 | ||
9ad5cbcf AM |
6441 | symtab_shndx_hdr->contents = outbound_shndx; |
6442 | symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX; | |
6443 | symtab_shndx_hdr->sh_size = amt; | |
6444 | symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx); | |
6445 | symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx); | |
6446 | } | |
6447 | ||
589e6347 | 6448 | /* Now generate the data (for "contents"). */ |
079e9a2f AM |
6449 | { |
6450 | /* Fill in zeroth symbol and swap it out. */ | |
6451 | Elf_Internal_Sym sym; | |
6452 | sym.st_name = 0; | |
6453 | sym.st_value = 0; | |
6454 | sym.st_size = 0; | |
6455 | sym.st_info = 0; | |
6456 | sym.st_other = 0; | |
6457 | sym.st_shndx = SHN_UNDEF; | |
9ad5cbcf | 6458 | bed->s->swap_symbol_out (abfd, &sym, outbound_syms, outbound_shndx); |
079e9a2f | 6459 | outbound_syms += bed->s->sizeof_sym; |
9ad5cbcf AM |
6460 | if (outbound_shndx != NULL) |
6461 | outbound_shndx += sizeof (Elf_External_Sym_Shndx); | |
079e9a2f | 6462 | } |
252b5132 | 6463 | |
174fd7f9 RS |
6464 | name_local_sections |
6465 | = (bed->elf_backend_name_local_section_symbols | |
6466 | && bed->elf_backend_name_local_section_symbols (abfd)); | |
6467 | ||
079e9a2f AM |
6468 | syms = bfd_get_outsymbols (abfd); |
6469 | for (idx = 0; idx < symcount; idx++) | |
252b5132 | 6470 | { |
252b5132 | 6471 | Elf_Internal_Sym sym; |
079e9a2f AM |
6472 | bfd_vma value = syms[idx]->value; |
6473 | elf_symbol_type *type_ptr; | |
6474 | flagword flags = syms[idx]->flags; | |
6475 | int type; | |
252b5132 | 6476 | |
174fd7f9 RS |
6477 | if (!name_local_sections |
6478 | && (flags & (BSF_SECTION_SYM | BSF_GLOBAL)) == BSF_SECTION_SYM) | |
079e9a2f AM |
6479 | { |
6480 | /* Local section symbols have no name. */ | |
6481 | sym.st_name = 0; | |
6482 | } | |
6483 | else | |
6484 | { | |
6485 | sym.st_name = (unsigned long) _bfd_stringtab_add (stt, | |
6486 | syms[idx]->name, | |
b34976b6 | 6487 | TRUE, FALSE); |
079e9a2f | 6488 | if (sym.st_name == (unsigned long) -1) |
5ed6aba4 NC |
6489 | { |
6490 | _bfd_stringtab_free (stt); | |
6491 | return FALSE; | |
6492 | } | |
079e9a2f | 6493 | } |
252b5132 | 6494 | |
079e9a2f | 6495 | type_ptr = elf_symbol_from (abfd, syms[idx]); |
252b5132 | 6496 | |
079e9a2f AM |
6497 | if ((flags & BSF_SECTION_SYM) == 0 |
6498 | && bfd_is_com_section (syms[idx]->section)) | |
6499 | { | |
6500 | /* ELF common symbols put the alignment into the `value' field, | |
6501 | and the size into the `size' field. This is backwards from | |
6502 | how BFD handles it, so reverse it here. */ | |
6503 | sym.st_size = value; | |
6504 | if (type_ptr == NULL | |
6505 | || type_ptr->internal_elf_sym.st_value == 0) | |
6506 | sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value)); | |
6507 | else | |
6508 | sym.st_value = type_ptr->internal_elf_sym.st_value; | |
6509 | sym.st_shndx = _bfd_elf_section_from_bfd_section | |
6510 | (abfd, syms[idx]->section); | |
6511 | } | |
6512 | else | |
6513 | { | |
6514 | asection *sec = syms[idx]->section; | |
cb33740c | 6515 | unsigned int shndx; |
252b5132 | 6516 | |
079e9a2f AM |
6517 | if (sec->output_section) |
6518 | { | |
6519 | value += sec->output_offset; | |
6520 | sec = sec->output_section; | |
6521 | } | |
589e6347 | 6522 | |
079e9a2f AM |
6523 | /* Don't add in the section vma for relocatable output. */ |
6524 | if (! relocatable_p) | |
6525 | value += sec->vma; | |
6526 | sym.st_value = value; | |
6527 | sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0; | |
6528 | ||
6529 | if (bfd_is_abs_section (sec) | |
6530 | && type_ptr != NULL | |
6531 | && type_ptr->internal_elf_sym.st_shndx != 0) | |
6532 | { | |
6533 | /* This symbol is in a real ELF section which we did | |
6534 | not create as a BFD section. Undo the mapping done | |
6535 | by copy_private_symbol_data. */ | |
6536 | shndx = type_ptr->internal_elf_sym.st_shndx; | |
6537 | switch (shndx) | |
6538 | { | |
6539 | case MAP_ONESYMTAB: | |
6540 | shndx = elf_onesymtab (abfd); | |
6541 | break; | |
6542 | case MAP_DYNSYMTAB: | |
6543 | shndx = elf_dynsymtab (abfd); | |
6544 | break; | |
6545 | case MAP_STRTAB: | |
6546 | shndx = elf_tdata (abfd)->strtab_section; | |
6547 | break; | |
6548 | case MAP_SHSTRTAB: | |
6549 | shndx = elf_tdata (abfd)->shstrtab_section; | |
6550 | break; | |
9ad5cbcf AM |
6551 | case MAP_SYM_SHNDX: |
6552 | shndx = elf_tdata (abfd)->symtab_shndx_section; | |
6553 | break; | |
079e9a2f AM |
6554 | default: |
6555 | break; | |
6556 | } | |
6557 | } | |
6558 | else | |
6559 | { | |
6560 | shndx = _bfd_elf_section_from_bfd_section (abfd, sec); | |
252b5132 | 6561 | |
cb33740c | 6562 | if (shndx == SHN_BAD) |
079e9a2f AM |
6563 | { |
6564 | asection *sec2; | |
6565 | ||
6566 | /* Writing this would be a hell of a lot easier if | |
6567 | we had some decent documentation on bfd, and | |
6568 | knew what to expect of the library, and what to | |
6569 | demand of applications. For example, it | |
6570 | appears that `objcopy' might not set the | |
6571 | section of a symbol to be a section that is | |
6572 | actually in the output file. */ | |
6573 | sec2 = bfd_get_section_by_name (abfd, sec->name); | |
589e6347 NC |
6574 | if (sec2 == NULL) |
6575 | { | |
6576 | _bfd_error_handler (_("\ | |
6577 | Unable to find equivalent output section for symbol '%s' from section '%s'"), | |
6578 | syms[idx]->name ? syms[idx]->name : "<Local sym>", | |
6579 | sec->name); | |
811072d8 | 6580 | bfd_set_error (bfd_error_invalid_operation); |
5ed6aba4 | 6581 | _bfd_stringtab_free (stt); |
589e6347 NC |
6582 | return FALSE; |
6583 | } | |
811072d8 | 6584 | |
079e9a2f | 6585 | shndx = _bfd_elf_section_from_bfd_section (abfd, sec2); |
cb33740c | 6586 | BFD_ASSERT (shndx != SHN_BAD); |
079e9a2f AM |
6587 | } |
6588 | } | |
252b5132 | 6589 | |
079e9a2f AM |
6590 | sym.st_shndx = shndx; |
6591 | } | |
252b5132 | 6592 | |
13ae64f3 JJ |
6593 | if ((flags & BSF_THREAD_LOCAL) != 0) |
6594 | type = STT_TLS; | |
d8045f23 NC |
6595 | else if ((flags & BSF_GNU_INDIRECT_FUNCTION) != 0) |
6596 | type = STT_GNU_IFUNC; | |
13ae64f3 | 6597 | else if ((flags & BSF_FUNCTION) != 0) |
079e9a2f AM |
6598 | type = STT_FUNC; |
6599 | else if ((flags & BSF_OBJECT) != 0) | |
6600 | type = STT_OBJECT; | |
d9352518 DB |
6601 | else if ((flags & BSF_RELC) != 0) |
6602 | type = STT_RELC; | |
6603 | else if ((flags & BSF_SRELC) != 0) | |
6604 | type = STT_SRELC; | |
079e9a2f AM |
6605 | else |
6606 | type = STT_NOTYPE; | |
252b5132 | 6607 | |
13ae64f3 JJ |
6608 | if (syms[idx]->section->flags & SEC_THREAD_LOCAL) |
6609 | type = STT_TLS; | |
6610 | ||
589e6347 | 6611 | /* Processor-specific types. */ |
079e9a2f AM |
6612 | if (type_ptr != NULL |
6613 | && bed->elf_backend_get_symbol_type) | |
6614 | type = ((*bed->elf_backend_get_symbol_type) | |
6615 | (&type_ptr->internal_elf_sym, type)); | |
252b5132 | 6616 | |
079e9a2f AM |
6617 | if (flags & BSF_SECTION_SYM) |
6618 | { | |
6619 | if (flags & BSF_GLOBAL) | |
6620 | sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION); | |
6621 | else | |
6622 | sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION); | |
6623 | } | |
6624 | else if (bfd_is_com_section (syms[idx]->section)) | |
0a40daed | 6625 | { |
504b7d20 | 6626 | #ifdef USE_STT_COMMON |
0a40daed MK |
6627 | if (type == STT_OBJECT) |
6628 | sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_COMMON); | |
6629 | else | |
504b7d20 | 6630 | #endif |
c91e322a | 6631 | sym.st_info = ELF_ST_INFO (STB_GLOBAL, type); |
0a40daed | 6632 | } |
079e9a2f AM |
6633 | else if (bfd_is_und_section (syms[idx]->section)) |
6634 | sym.st_info = ELF_ST_INFO (((flags & BSF_WEAK) | |
6635 | ? STB_WEAK | |
6636 | : STB_GLOBAL), | |
6637 | type); | |
6638 | else if (flags & BSF_FILE) | |
6639 | sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE); | |
6640 | else | |
6641 | { | |
6642 | int bind = STB_LOCAL; | |
252b5132 | 6643 | |
079e9a2f AM |
6644 | if (flags & BSF_LOCAL) |
6645 | bind = STB_LOCAL; | |
3e7a7d11 NC |
6646 | else if (flags & BSF_GNU_UNIQUE) |
6647 | bind = STB_GNU_UNIQUE; | |
079e9a2f AM |
6648 | else if (flags & BSF_WEAK) |
6649 | bind = STB_WEAK; | |
6650 | else if (flags & BSF_GLOBAL) | |
6651 | bind = STB_GLOBAL; | |
252b5132 | 6652 | |
079e9a2f AM |
6653 | sym.st_info = ELF_ST_INFO (bind, type); |
6654 | } | |
252b5132 | 6655 | |
079e9a2f AM |
6656 | if (type_ptr != NULL) |
6657 | sym.st_other = type_ptr->internal_elf_sym.st_other; | |
6658 | else | |
6659 | sym.st_other = 0; | |
252b5132 | 6660 | |
9ad5cbcf | 6661 | bed->s->swap_symbol_out (abfd, &sym, outbound_syms, outbound_shndx); |
079e9a2f | 6662 | outbound_syms += bed->s->sizeof_sym; |
9ad5cbcf AM |
6663 | if (outbound_shndx != NULL) |
6664 | outbound_shndx += sizeof (Elf_External_Sym_Shndx); | |
079e9a2f | 6665 | } |
252b5132 | 6666 | |
079e9a2f AM |
6667 | *sttp = stt; |
6668 | symstrtab_hdr->sh_size = _bfd_stringtab_size (stt); | |
6669 | symstrtab_hdr->sh_type = SHT_STRTAB; | |
252b5132 | 6670 | |
079e9a2f AM |
6671 | symstrtab_hdr->sh_flags = 0; |
6672 | symstrtab_hdr->sh_addr = 0; | |
6673 | symstrtab_hdr->sh_entsize = 0; | |
6674 | symstrtab_hdr->sh_link = 0; | |
6675 | symstrtab_hdr->sh_info = 0; | |
6676 | symstrtab_hdr->sh_addralign = 1; | |
252b5132 | 6677 | |
b34976b6 | 6678 | return TRUE; |
252b5132 RH |
6679 | } |
6680 | ||
6681 | /* Return the number of bytes required to hold the symtab vector. | |
6682 | ||
6683 | Note that we base it on the count plus 1, since we will null terminate | |
6684 | the vector allocated based on this size. However, the ELF symbol table | |
6685 | always has a dummy entry as symbol #0, so it ends up even. */ | |
6686 | ||
6687 | long | |
217aa764 | 6688 | _bfd_elf_get_symtab_upper_bound (bfd *abfd) |
252b5132 RH |
6689 | { |
6690 | long symcount; | |
6691 | long symtab_size; | |
6692 | Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr; | |
6693 | ||
6694 | symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym; | |
b99d1833 AM |
6695 | symtab_size = (symcount + 1) * (sizeof (asymbol *)); |
6696 | if (symcount > 0) | |
6697 | symtab_size -= sizeof (asymbol *); | |
252b5132 RH |
6698 | |
6699 | return symtab_size; | |
6700 | } | |
6701 | ||
6702 | long | |
217aa764 | 6703 | _bfd_elf_get_dynamic_symtab_upper_bound (bfd *abfd) |
252b5132 RH |
6704 | { |
6705 | long symcount; | |
6706 | long symtab_size; | |
6707 | Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr; | |
6708 | ||
6709 | if (elf_dynsymtab (abfd) == 0) | |
6710 | { | |
6711 | bfd_set_error (bfd_error_invalid_operation); | |
6712 | return -1; | |
6713 | } | |
6714 | ||
6715 | symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym; | |
b99d1833 AM |
6716 | symtab_size = (symcount + 1) * (sizeof (asymbol *)); |
6717 | if (symcount > 0) | |
6718 | symtab_size -= sizeof (asymbol *); | |
252b5132 RH |
6719 | |
6720 | return symtab_size; | |
6721 | } | |
6722 | ||
6723 | long | |
217aa764 AM |
6724 | _bfd_elf_get_reloc_upper_bound (bfd *abfd ATTRIBUTE_UNUSED, |
6725 | sec_ptr asect) | |
252b5132 RH |
6726 | { |
6727 | return (asect->reloc_count + 1) * sizeof (arelent *); | |
6728 | } | |
6729 | ||
6730 | /* Canonicalize the relocs. */ | |
6731 | ||
6732 | long | |
217aa764 AM |
6733 | _bfd_elf_canonicalize_reloc (bfd *abfd, |
6734 | sec_ptr section, | |
6735 | arelent **relptr, | |
6736 | asymbol **symbols) | |
252b5132 RH |
6737 | { |
6738 | arelent *tblptr; | |
6739 | unsigned int i; | |
9c5bfbb7 | 6740 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
252b5132 | 6741 | |
b34976b6 | 6742 | if (! bed->s->slurp_reloc_table (abfd, section, symbols, FALSE)) |
252b5132 RH |
6743 | return -1; |
6744 | ||
6745 | tblptr = section->relocation; | |
6746 | for (i = 0; i < section->reloc_count; i++) | |
6747 | *relptr++ = tblptr++; | |
6748 | ||
6749 | *relptr = NULL; | |
6750 | ||
6751 | return section->reloc_count; | |
6752 | } | |
6753 | ||
6754 | long | |
6cee3f79 | 6755 | _bfd_elf_canonicalize_symtab (bfd *abfd, asymbol **allocation) |
252b5132 | 6756 | { |
9c5bfbb7 | 6757 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
217aa764 | 6758 | long symcount = bed->s->slurp_symbol_table (abfd, allocation, FALSE); |
252b5132 RH |
6759 | |
6760 | if (symcount >= 0) | |
6761 | bfd_get_symcount (abfd) = symcount; | |
6762 | return symcount; | |
6763 | } | |
6764 | ||
6765 | long | |
217aa764 AM |
6766 | _bfd_elf_canonicalize_dynamic_symtab (bfd *abfd, |
6767 | asymbol **allocation) | |
252b5132 | 6768 | { |
9c5bfbb7 | 6769 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
217aa764 | 6770 | long symcount = bed->s->slurp_symbol_table (abfd, allocation, TRUE); |
1f70368c DJ |
6771 | |
6772 | if (symcount >= 0) | |
6773 | bfd_get_dynamic_symcount (abfd) = symcount; | |
6774 | return symcount; | |
252b5132 RH |
6775 | } |
6776 | ||
8615f3f2 AM |
6777 | /* Return the size required for the dynamic reloc entries. Any loadable |
6778 | section that was actually installed in the BFD, and has type SHT_REL | |
6779 | or SHT_RELA, and uses the dynamic symbol table, is considered to be a | |
6780 | dynamic reloc section. */ | |
252b5132 RH |
6781 | |
6782 | long | |
217aa764 | 6783 | _bfd_elf_get_dynamic_reloc_upper_bound (bfd *abfd) |
252b5132 RH |
6784 | { |
6785 | long ret; | |
6786 | asection *s; | |
6787 | ||
6788 | if (elf_dynsymtab (abfd) == 0) | |
6789 | { | |
6790 | bfd_set_error (bfd_error_invalid_operation); | |
6791 | return -1; | |
6792 | } | |
6793 | ||
6794 | ret = sizeof (arelent *); | |
6795 | for (s = abfd->sections; s != NULL; s = s->next) | |
266b05cf | 6796 | if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd) |
252b5132 RH |
6797 | && (elf_section_data (s)->this_hdr.sh_type == SHT_REL |
6798 | || elf_section_data (s)->this_hdr.sh_type == SHT_RELA)) | |
eea6121a | 6799 | ret += ((s->size / elf_section_data (s)->this_hdr.sh_entsize) |
252b5132 RH |
6800 | * sizeof (arelent *)); |
6801 | ||
6802 | return ret; | |
6803 | } | |
6804 | ||
8615f3f2 AM |
6805 | /* Canonicalize the dynamic relocation entries. Note that we return the |
6806 | dynamic relocations as a single block, although they are actually | |
6807 | associated with particular sections; the interface, which was | |
6808 | designed for SunOS style shared libraries, expects that there is only | |
6809 | one set of dynamic relocs. Any loadable section that was actually | |
6810 | installed in the BFD, and has type SHT_REL or SHT_RELA, and uses the | |
6811 | dynamic symbol table, is considered to be a dynamic reloc section. */ | |
252b5132 RH |
6812 | |
6813 | long | |
217aa764 AM |
6814 | _bfd_elf_canonicalize_dynamic_reloc (bfd *abfd, |
6815 | arelent **storage, | |
6816 | asymbol **syms) | |
252b5132 | 6817 | { |
217aa764 | 6818 | bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean); |
252b5132 RH |
6819 | asection *s; |
6820 | long ret; | |
6821 | ||
6822 | if (elf_dynsymtab (abfd) == 0) | |
6823 | { | |
6824 | bfd_set_error (bfd_error_invalid_operation); | |
6825 | return -1; | |
6826 | } | |
6827 | ||
6828 | slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table; | |
6829 | ret = 0; | |
6830 | for (s = abfd->sections; s != NULL; s = s->next) | |
6831 | { | |
266b05cf | 6832 | if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd) |
252b5132 RH |
6833 | && (elf_section_data (s)->this_hdr.sh_type == SHT_REL |
6834 | || elf_section_data (s)->this_hdr.sh_type == SHT_RELA)) | |
6835 | { | |
6836 | arelent *p; | |
6837 | long count, i; | |
6838 | ||
b34976b6 | 6839 | if (! (*slurp_relocs) (abfd, s, syms, TRUE)) |
252b5132 | 6840 | return -1; |
eea6121a | 6841 | count = s->size / elf_section_data (s)->this_hdr.sh_entsize; |
252b5132 RH |
6842 | p = s->relocation; |
6843 | for (i = 0; i < count; i++) | |
6844 | *storage++ = p++; | |
6845 | ret += count; | |
6846 | } | |
6847 | } | |
6848 | ||
6849 | *storage = NULL; | |
6850 | ||
6851 | return ret; | |
6852 | } | |
6853 | \f | |
6854 | /* Read in the version information. */ | |
6855 | ||
b34976b6 | 6856 | bfd_boolean |
fc0e6df6 | 6857 | _bfd_elf_slurp_version_tables (bfd *abfd, bfd_boolean default_imported_symver) |
252b5132 RH |
6858 | { |
6859 | bfd_byte *contents = NULL; | |
fc0e6df6 PB |
6860 | unsigned int freeidx = 0; |
6861 | ||
6862 | if (elf_dynverref (abfd) != 0) | |
6863 | { | |
6864 | Elf_Internal_Shdr *hdr; | |
6865 | Elf_External_Verneed *everneed; | |
6866 | Elf_Internal_Verneed *iverneed; | |
6867 | unsigned int i; | |
d0fb9a8d | 6868 | bfd_byte *contents_end; |
fc0e6df6 PB |
6869 | |
6870 | hdr = &elf_tdata (abfd)->dynverref_hdr; | |
6871 | ||
a50b1753 NC |
6872 | elf_tdata (abfd)->verref = (Elf_Internal_Verneed *) |
6873 | bfd_zalloc2 (abfd, hdr->sh_info, sizeof (Elf_Internal_Verneed)); | |
fc0e6df6 PB |
6874 | if (elf_tdata (abfd)->verref == NULL) |
6875 | goto error_return; | |
6876 | ||
6877 | elf_tdata (abfd)->cverrefs = hdr->sh_info; | |
6878 | ||
a50b1753 | 6879 | contents = (bfd_byte *) bfd_malloc (hdr->sh_size); |
fc0e6df6 | 6880 | if (contents == NULL) |
d0fb9a8d JJ |
6881 | { |
6882 | error_return_verref: | |
6883 | elf_tdata (abfd)->verref = NULL; | |
6884 | elf_tdata (abfd)->cverrefs = 0; | |
6885 | goto error_return; | |
6886 | } | |
fc0e6df6 PB |
6887 | if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0 |
6888 | || bfd_bread (contents, hdr->sh_size, abfd) != hdr->sh_size) | |
d0fb9a8d | 6889 | goto error_return_verref; |
fc0e6df6 | 6890 | |
d0fb9a8d JJ |
6891 | if (hdr->sh_info && hdr->sh_size < sizeof (Elf_External_Verneed)) |
6892 | goto error_return_verref; | |
6893 | ||
6894 | BFD_ASSERT (sizeof (Elf_External_Verneed) | |
6895 | == sizeof (Elf_External_Vernaux)); | |
6896 | contents_end = contents + hdr->sh_size - sizeof (Elf_External_Verneed); | |
fc0e6df6 PB |
6897 | everneed = (Elf_External_Verneed *) contents; |
6898 | iverneed = elf_tdata (abfd)->verref; | |
6899 | for (i = 0; i < hdr->sh_info; i++, iverneed++) | |
6900 | { | |
6901 | Elf_External_Vernaux *evernaux; | |
6902 | Elf_Internal_Vernaux *ivernaux; | |
6903 | unsigned int j; | |
6904 | ||
6905 | _bfd_elf_swap_verneed_in (abfd, everneed, iverneed); | |
6906 | ||
6907 | iverneed->vn_bfd = abfd; | |
6908 | ||
6909 | iverneed->vn_filename = | |
6910 | bfd_elf_string_from_elf_section (abfd, hdr->sh_link, | |
6911 | iverneed->vn_file); | |
6912 | if (iverneed->vn_filename == NULL) | |
d0fb9a8d | 6913 | goto error_return_verref; |
fc0e6df6 | 6914 | |
d0fb9a8d JJ |
6915 | if (iverneed->vn_cnt == 0) |
6916 | iverneed->vn_auxptr = NULL; | |
6917 | else | |
6918 | { | |
a50b1753 NC |
6919 | iverneed->vn_auxptr = (struct elf_internal_vernaux *) |
6920 | bfd_alloc2 (abfd, iverneed->vn_cnt, | |
6921 | sizeof (Elf_Internal_Vernaux)); | |
d0fb9a8d JJ |
6922 | if (iverneed->vn_auxptr == NULL) |
6923 | goto error_return_verref; | |
6924 | } | |
6925 | ||
6926 | if (iverneed->vn_aux | |
6927 | > (size_t) (contents_end - (bfd_byte *) everneed)) | |
6928 | goto error_return_verref; | |
fc0e6df6 PB |
6929 | |
6930 | evernaux = ((Elf_External_Vernaux *) | |
6931 | ((bfd_byte *) everneed + iverneed->vn_aux)); | |
6932 | ivernaux = iverneed->vn_auxptr; | |
6933 | for (j = 0; j < iverneed->vn_cnt; j++, ivernaux++) | |
6934 | { | |
6935 | _bfd_elf_swap_vernaux_in (abfd, evernaux, ivernaux); | |
6936 | ||
6937 | ivernaux->vna_nodename = | |
6938 | bfd_elf_string_from_elf_section (abfd, hdr->sh_link, | |
6939 | ivernaux->vna_name); | |
6940 | if (ivernaux->vna_nodename == NULL) | |
d0fb9a8d | 6941 | goto error_return_verref; |
fc0e6df6 PB |
6942 | |
6943 | if (j + 1 < iverneed->vn_cnt) | |
6944 | ivernaux->vna_nextptr = ivernaux + 1; | |
6945 | else | |
6946 | ivernaux->vna_nextptr = NULL; | |
6947 | ||
d0fb9a8d JJ |
6948 | if (ivernaux->vna_next |
6949 | > (size_t) (contents_end - (bfd_byte *) evernaux)) | |
6950 | goto error_return_verref; | |
6951 | ||
fc0e6df6 PB |
6952 | evernaux = ((Elf_External_Vernaux *) |
6953 | ((bfd_byte *) evernaux + ivernaux->vna_next)); | |
6954 | ||
6955 | if (ivernaux->vna_other > freeidx) | |
6956 | freeidx = ivernaux->vna_other; | |
6957 | } | |
6958 | ||
6959 | if (i + 1 < hdr->sh_info) | |
6960 | iverneed->vn_nextref = iverneed + 1; | |
6961 | else | |
6962 | iverneed->vn_nextref = NULL; | |
6963 | ||
d0fb9a8d JJ |
6964 | if (iverneed->vn_next |
6965 | > (size_t) (contents_end - (bfd_byte *) everneed)) | |
6966 | goto error_return_verref; | |
6967 | ||
fc0e6df6 PB |
6968 | everneed = ((Elf_External_Verneed *) |
6969 | ((bfd_byte *) everneed + iverneed->vn_next)); | |
6970 | } | |
6971 | ||
6972 | free (contents); | |
6973 | contents = NULL; | |
6974 | } | |
252b5132 RH |
6975 | |
6976 | if (elf_dynverdef (abfd) != 0) | |
6977 | { | |
6978 | Elf_Internal_Shdr *hdr; | |
6979 | Elf_External_Verdef *everdef; | |
6980 | Elf_Internal_Verdef *iverdef; | |
f631889e UD |
6981 | Elf_Internal_Verdef *iverdefarr; |
6982 | Elf_Internal_Verdef iverdefmem; | |
252b5132 | 6983 | unsigned int i; |
062e2358 | 6984 | unsigned int maxidx; |
d0fb9a8d | 6985 | bfd_byte *contents_end_def, *contents_end_aux; |
252b5132 RH |
6986 | |
6987 | hdr = &elf_tdata (abfd)->dynverdef_hdr; | |
6988 | ||
a50b1753 | 6989 | contents = (bfd_byte *) bfd_malloc (hdr->sh_size); |
252b5132 RH |
6990 | if (contents == NULL) |
6991 | goto error_return; | |
6992 | if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0 | |
217aa764 | 6993 | || bfd_bread (contents, hdr->sh_size, abfd) != hdr->sh_size) |
252b5132 RH |
6994 | goto error_return; |
6995 | ||
d0fb9a8d JJ |
6996 | if (hdr->sh_info && hdr->sh_size < sizeof (Elf_External_Verdef)) |
6997 | goto error_return; | |
6998 | ||
6999 | BFD_ASSERT (sizeof (Elf_External_Verdef) | |
7000 | >= sizeof (Elf_External_Verdaux)); | |
7001 | contents_end_def = contents + hdr->sh_size | |
7002 | - sizeof (Elf_External_Verdef); | |
7003 | contents_end_aux = contents + hdr->sh_size | |
7004 | - sizeof (Elf_External_Verdaux); | |
7005 | ||
f631889e UD |
7006 | /* We know the number of entries in the section but not the maximum |
7007 | index. Therefore we have to run through all entries and find | |
7008 | the maximum. */ | |
252b5132 | 7009 | everdef = (Elf_External_Verdef *) contents; |
f631889e UD |
7010 | maxidx = 0; |
7011 | for (i = 0; i < hdr->sh_info; ++i) | |
7012 | { | |
7013 | _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem); | |
7014 | ||
062e2358 AM |
7015 | if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) > maxidx) |
7016 | maxidx = iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION); | |
f631889e | 7017 | |
d0fb9a8d JJ |
7018 | if (iverdefmem.vd_next |
7019 | > (size_t) (contents_end_def - (bfd_byte *) everdef)) | |
7020 | goto error_return; | |
7021 | ||
f631889e UD |
7022 | everdef = ((Elf_External_Verdef *) |
7023 | ((bfd_byte *) everdef + iverdefmem.vd_next)); | |
7024 | } | |
7025 | ||
fc0e6df6 PB |
7026 | if (default_imported_symver) |
7027 | { | |
7028 | if (freeidx > maxidx) | |
7029 | maxidx = ++freeidx; | |
7030 | else | |
7031 | freeidx = ++maxidx; | |
7032 | } | |
a50b1753 NC |
7033 | elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *) |
7034 | bfd_zalloc2 (abfd, maxidx, sizeof (Elf_Internal_Verdef)); | |
f631889e UD |
7035 | if (elf_tdata (abfd)->verdef == NULL) |
7036 | goto error_return; | |
7037 | ||
7038 | elf_tdata (abfd)->cverdefs = maxidx; | |
7039 | ||
7040 | everdef = (Elf_External_Verdef *) contents; | |
7041 | iverdefarr = elf_tdata (abfd)->verdef; | |
7042 | for (i = 0; i < hdr->sh_info; i++) | |
252b5132 RH |
7043 | { |
7044 | Elf_External_Verdaux *everdaux; | |
7045 | Elf_Internal_Verdaux *iverdaux; | |
7046 | unsigned int j; | |
7047 | ||
f631889e UD |
7048 | _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem); |
7049 | ||
d0fb9a8d JJ |
7050 | if ((iverdefmem.vd_ndx & VERSYM_VERSION) == 0) |
7051 | { | |
7052 | error_return_verdef: | |
7053 | elf_tdata (abfd)->verdef = NULL; | |
7054 | elf_tdata (abfd)->cverdefs = 0; | |
7055 | goto error_return; | |
7056 | } | |
7057 | ||
f631889e UD |
7058 | iverdef = &iverdefarr[(iverdefmem.vd_ndx & VERSYM_VERSION) - 1]; |
7059 | memcpy (iverdef, &iverdefmem, sizeof (Elf_Internal_Verdef)); | |
252b5132 RH |
7060 | |
7061 | iverdef->vd_bfd = abfd; | |
7062 | ||
d0fb9a8d JJ |
7063 | if (iverdef->vd_cnt == 0) |
7064 | iverdef->vd_auxptr = NULL; | |
7065 | else | |
7066 | { | |
a50b1753 NC |
7067 | iverdef->vd_auxptr = (struct elf_internal_verdaux *) |
7068 | bfd_alloc2 (abfd, iverdef->vd_cnt, | |
7069 | sizeof (Elf_Internal_Verdaux)); | |
d0fb9a8d JJ |
7070 | if (iverdef->vd_auxptr == NULL) |
7071 | goto error_return_verdef; | |
7072 | } | |
7073 | ||
7074 | if (iverdef->vd_aux | |
7075 | > (size_t) (contents_end_aux - (bfd_byte *) everdef)) | |
7076 | goto error_return_verdef; | |
252b5132 RH |
7077 | |
7078 | everdaux = ((Elf_External_Verdaux *) | |
7079 | ((bfd_byte *) everdef + iverdef->vd_aux)); | |
7080 | iverdaux = iverdef->vd_auxptr; | |
7081 | for (j = 0; j < iverdef->vd_cnt; j++, iverdaux++) | |
7082 | { | |
7083 | _bfd_elf_swap_verdaux_in (abfd, everdaux, iverdaux); | |
7084 | ||
7085 | iverdaux->vda_nodename = | |
7086 | bfd_elf_string_from_elf_section (abfd, hdr->sh_link, | |
7087 | iverdaux->vda_name); | |
7088 | if (iverdaux->vda_nodename == NULL) | |
d0fb9a8d | 7089 | goto error_return_verdef; |
252b5132 RH |
7090 | |
7091 | if (j + 1 < iverdef->vd_cnt) | |
7092 | iverdaux->vda_nextptr = iverdaux + 1; | |
7093 | else | |
7094 | iverdaux->vda_nextptr = NULL; | |
7095 | ||
d0fb9a8d JJ |
7096 | if (iverdaux->vda_next |
7097 | > (size_t) (contents_end_aux - (bfd_byte *) everdaux)) | |
7098 | goto error_return_verdef; | |
7099 | ||
252b5132 RH |
7100 | everdaux = ((Elf_External_Verdaux *) |
7101 | ((bfd_byte *) everdaux + iverdaux->vda_next)); | |
7102 | } | |
7103 | ||
d0fb9a8d JJ |
7104 | if (iverdef->vd_cnt) |
7105 | iverdef->vd_nodename = iverdef->vd_auxptr->vda_nodename; | |
252b5132 | 7106 | |
d0fb9a8d | 7107 | if ((size_t) (iverdef - iverdefarr) + 1 < maxidx) |
252b5132 RH |
7108 | iverdef->vd_nextdef = iverdef + 1; |
7109 | else | |
7110 | iverdef->vd_nextdef = NULL; | |
7111 | ||
7112 | everdef = ((Elf_External_Verdef *) | |
7113 | ((bfd_byte *) everdef + iverdef->vd_next)); | |
7114 | } | |
7115 | ||
7116 | free (contents); | |
7117 | contents = NULL; | |
7118 | } | |
fc0e6df6 | 7119 | else if (default_imported_symver) |
252b5132 | 7120 | { |
fc0e6df6 PB |
7121 | if (freeidx < 3) |
7122 | freeidx = 3; | |
7123 | else | |
7124 | freeidx++; | |
252b5132 | 7125 | |
a50b1753 NC |
7126 | elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *) |
7127 | bfd_zalloc2 (abfd, freeidx, sizeof (Elf_Internal_Verdef)); | |
fc0e6df6 | 7128 | if (elf_tdata (abfd)->verdef == NULL) |
252b5132 RH |
7129 | goto error_return; |
7130 | ||
fc0e6df6 PB |
7131 | elf_tdata (abfd)->cverdefs = freeidx; |
7132 | } | |
252b5132 | 7133 | |
fc0e6df6 PB |
7134 | /* Create a default version based on the soname. */ |
7135 | if (default_imported_symver) | |
7136 | { | |
7137 | Elf_Internal_Verdef *iverdef; | |
7138 | Elf_Internal_Verdaux *iverdaux; | |
252b5132 | 7139 | |
fc0e6df6 | 7140 | iverdef = &elf_tdata (abfd)->verdef[freeidx - 1];; |
252b5132 | 7141 | |
fc0e6df6 PB |
7142 | iverdef->vd_version = VER_DEF_CURRENT; |
7143 | iverdef->vd_flags = 0; | |
7144 | iverdef->vd_ndx = freeidx; | |
7145 | iverdef->vd_cnt = 1; | |
252b5132 | 7146 | |
fc0e6df6 | 7147 | iverdef->vd_bfd = abfd; |
252b5132 | 7148 | |
fc0e6df6 PB |
7149 | iverdef->vd_nodename = bfd_elf_get_dt_soname (abfd); |
7150 | if (iverdef->vd_nodename == NULL) | |
d0fb9a8d | 7151 | goto error_return_verdef; |
fc0e6df6 | 7152 | iverdef->vd_nextdef = NULL; |
a50b1753 NC |
7153 | iverdef->vd_auxptr = (struct elf_internal_verdaux *) |
7154 | bfd_alloc (abfd, sizeof (Elf_Internal_Verdaux)); | |
d0fb9a8d JJ |
7155 | if (iverdef->vd_auxptr == NULL) |
7156 | goto error_return_verdef; | |
252b5132 | 7157 | |
fc0e6df6 PB |
7158 | iverdaux = iverdef->vd_auxptr; |
7159 | iverdaux->vda_nodename = iverdef->vd_nodename; | |
7160 | iverdaux->vda_nextptr = NULL; | |
252b5132 RH |
7161 | } |
7162 | ||
b34976b6 | 7163 | return TRUE; |
252b5132 RH |
7164 | |
7165 | error_return: | |
5ed6aba4 | 7166 | if (contents != NULL) |
252b5132 | 7167 | free (contents); |
b34976b6 | 7168 | return FALSE; |
252b5132 RH |
7169 | } |
7170 | \f | |
7171 | asymbol * | |
217aa764 | 7172 | _bfd_elf_make_empty_symbol (bfd *abfd) |
252b5132 RH |
7173 | { |
7174 | elf_symbol_type *newsym; | |
dc810e39 | 7175 | bfd_size_type amt = sizeof (elf_symbol_type); |
252b5132 | 7176 | |
a50b1753 | 7177 | newsym = (elf_symbol_type *) bfd_zalloc (abfd, amt); |
252b5132 RH |
7178 | if (!newsym) |
7179 | return NULL; | |
7180 | else | |
7181 | { | |
7182 | newsym->symbol.the_bfd = abfd; | |
7183 | return &newsym->symbol; | |
7184 | } | |
7185 | } | |
7186 | ||
7187 | void | |
217aa764 AM |
7188 | _bfd_elf_get_symbol_info (bfd *abfd ATTRIBUTE_UNUSED, |
7189 | asymbol *symbol, | |
7190 | symbol_info *ret) | |
252b5132 RH |
7191 | { |
7192 | bfd_symbol_info (symbol, ret); | |
7193 | } | |
7194 | ||
7195 | /* Return whether a symbol name implies a local symbol. Most targets | |
7196 | use this function for the is_local_label_name entry point, but some | |
7197 | override it. */ | |
7198 | ||
b34976b6 | 7199 | bfd_boolean |
217aa764 AM |
7200 | _bfd_elf_is_local_label_name (bfd *abfd ATTRIBUTE_UNUSED, |
7201 | const char *name) | |
252b5132 RH |
7202 | { |
7203 | /* Normal local symbols start with ``.L''. */ | |
7204 | if (name[0] == '.' && name[1] == 'L') | |
b34976b6 | 7205 | return TRUE; |
252b5132 RH |
7206 | |
7207 | /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate | |
7208 | DWARF debugging symbols starting with ``..''. */ | |
7209 | if (name[0] == '.' && name[1] == '.') | |
b34976b6 | 7210 | return TRUE; |
252b5132 RH |
7211 | |
7212 | /* gcc will sometimes generate symbols beginning with ``_.L_'' when | |
7213 | emitting DWARF debugging output. I suspect this is actually a | |
7214 | small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call | |
7215 | ASM_GENERATE_INTERNAL_LABEL, and this causes the leading | |
7216 | underscore to be emitted on some ELF targets). For ease of use, | |
7217 | we treat such symbols as local. */ | |
7218 | if (name[0] == '_' && name[1] == '.' && name[2] == 'L' && name[3] == '_') | |
b34976b6 | 7219 | return TRUE; |
252b5132 | 7220 | |
b34976b6 | 7221 | return FALSE; |
252b5132 RH |
7222 | } |
7223 | ||
7224 | alent * | |
217aa764 AM |
7225 | _bfd_elf_get_lineno (bfd *abfd ATTRIBUTE_UNUSED, |
7226 | asymbol *symbol ATTRIBUTE_UNUSED) | |
252b5132 RH |
7227 | { |
7228 | abort (); | |
7229 | return NULL; | |
7230 | } | |
7231 | ||
b34976b6 | 7232 | bfd_boolean |
217aa764 AM |
7233 | _bfd_elf_set_arch_mach (bfd *abfd, |
7234 | enum bfd_architecture arch, | |
7235 | unsigned long machine) | |
252b5132 RH |
7236 | { |
7237 | /* If this isn't the right architecture for this backend, and this | |
7238 | isn't the generic backend, fail. */ | |
7239 | if (arch != get_elf_backend_data (abfd)->arch | |
7240 | && arch != bfd_arch_unknown | |
7241 | && get_elf_backend_data (abfd)->arch != bfd_arch_unknown) | |
b34976b6 | 7242 | return FALSE; |
252b5132 RH |
7243 | |
7244 | return bfd_default_set_arch_mach (abfd, arch, machine); | |
7245 | } | |
7246 | ||
d1fad7c6 NC |
7247 | /* Find the function to a particular section and offset, |
7248 | for error reporting. */ | |
252b5132 | 7249 | |
b34976b6 | 7250 | static bfd_boolean |
b9d678e0 | 7251 | elf_find_function (bfd *abfd, |
217aa764 AM |
7252 | asection *section, |
7253 | asymbol **symbols, | |
7254 | bfd_vma offset, | |
7255 | const char **filename_ptr, | |
7256 | const char **functionname_ptr) | |
252b5132 | 7257 | { |
252b5132 | 7258 | const char *filename; |
57426232 | 7259 | asymbol *func, *file; |
252b5132 RH |
7260 | bfd_vma low_func; |
7261 | asymbol **p; | |
57426232 JB |
7262 | /* ??? Given multiple file symbols, it is impossible to reliably |
7263 | choose the right file name for global symbols. File symbols are | |
7264 | local symbols, and thus all file symbols must sort before any | |
7265 | global symbols. The ELF spec may be interpreted to say that a | |
7266 | file symbol must sort before other local symbols, but currently | |
7267 | ld -r doesn't do this. So, for ld -r output, it is possible to | |
7268 | make a better choice of file name for local symbols by ignoring | |
7269 | file symbols appearing after a given local symbol. */ | |
7270 | enum { nothing_seen, symbol_seen, file_after_symbol_seen } state; | |
b9d678e0 | 7271 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
252b5132 | 7272 | |
252b5132 RH |
7273 | filename = NULL; |
7274 | func = NULL; | |
57426232 | 7275 | file = NULL; |
252b5132 | 7276 | low_func = 0; |
57426232 | 7277 | state = nothing_seen; |
252b5132 RH |
7278 | |
7279 | for (p = symbols; *p != NULL; p++) | |
7280 | { | |
7281 | elf_symbol_type *q; | |
b9d678e0 | 7282 | unsigned int type; |
252b5132 RH |
7283 | |
7284 | q = (elf_symbol_type *) *p; | |
7285 | ||
b9d678e0 L |
7286 | type = ELF_ST_TYPE (q->internal_elf_sym.st_info); |
7287 | switch (type) | |
252b5132 | 7288 | { |
252b5132 | 7289 | case STT_FILE: |
57426232 JB |
7290 | file = &q->symbol; |
7291 | if (state == symbol_seen) | |
7292 | state = file_after_symbol_seen; | |
7293 | continue; | |
b9d678e0 L |
7294 | default: |
7295 | if (!bed->is_function_type (type)) | |
7296 | break; | |
252b5132 | 7297 | case STT_NOTYPE: |
6b40fcba | 7298 | if (bfd_get_section (&q->symbol) == section |
252b5132 RH |
7299 | && q->symbol.value >= low_func |
7300 | && q->symbol.value <= offset) | |
7301 | { | |
7302 | func = (asymbol *) q; | |
7303 | low_func = q->symbol.value; | |
a1923858 AM |
7304 | filename = NULL; |
7305 | if (file != NULL | |
7306 | && (ELF_ST_BIND (q->internal_elf_sym.st_info) == STB_LOCAL | |
7307 | || state != file_after_symbol_seen)) | |
57426232 | 7308 | filename = bfd_asymbol_name (file); |
252b5132 RH |
7309 | } |
7310 | break; | |
7311 | } | |
57426232 JB |
7312 | if (state == nothing_seen) |
7313 | state = symbol_seen; | |
252b5132 RH |
7314 | } |
7315 | ||
7316 | if (func == NULL) | |
b34976b6 | 7317 | return FALSE; |
252b5132 | 7318 | |
d1fad7c6 NC |
7319 | if (filename_ptr) |
7320 | *filename_ptr = filename; | |
7321 | if (functionname_ptr) | |
7322 | *functionname_ptr = bfd_asymbol_name (func); | |
7323 | ||
b34976b6 | 7324 | return TRUE; |
d1fad7c6 NC |
7325 | } |
7326 | ||
7327 | /* Find the nearest line to a particular section and offset, | |
7328 | for error reporting. */ | |
7329 | ||
b34976b6 | 7330 | bfd_boolean |
217aa764 AM |
7331 | _bfd_elf_find_nearest_line (bfd *abfd, |
7332 | asection *section, | |
7333 | asymbol **symbols, | |
7334 | bfd_vma offset, | |
7335 | const char **filename_ptr, | |
7336 | const char **functionname_ptr, | |
7337 | unsigned int *line_ptr) | |
d1fad7c6 | 7338 | { |
b34976b6 | 7339 | bfd_boolean found; |
d1fad7c6 NC |
7340 | |
7341 | if (_bfd_dwarf1_find_nearest_line (abfd, section, symbols, offset, | |
4e8a9624 AM |
7342 | filename_ptr, functionname_ptr, |
7343 | line_ptr)) | |
d1fad7c6 NC |
7344 | { |
7345 | if (!*functionname_ptr) | |
4e8a9624 AM |
7346 | elf_find_function (abfd, section, symbols, offset, |
7347 | *filename_ptr ? NULL : filename_ptr, | |
7348 | functionname_ptr); | |
7349 | ||
b34976b6 | 7350 | return TRUE; |
d1fad7c6 NC |
7351 | } |
7352 | ||
7353 | if (_bfd_dwarf2_find_nearest_line (abfd, section, symbols, offset, | |
4e8a9624 AM |
7354 | filename_ptr, functionname_ptr, |
7355 | line_ptr, 0, | |
7356 | &elf_tdata (abfd)->dwarf2_find_line_info)) | |
d1fad7c6 NC |
7357 | { |
7358 | if (!*functionname_ptr) | |
4e8a9624 AM |
7359 | elf_find_function (abfd, section, symbols, offset, |
7360 | *filename_ptr ? NULL : filename_ptr, | |
7361 | functionname_ptr); | |
7362 | ||
b34976b6 | 7363 | return TRUE; |
d1fad7c6 NC |
7364 | } |
7365 | ||
7366 | if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset, | |
4e8a9624 AM |
7367 | &found, filename_ptr, |
7368 | functionname_ptr, line_ptr, | |
7369 | &elf_tdata (abfd)->line_info)) | |
b34976b6 | 7370 | return FALSE; |
dc43ada5 | 7371 | if (found && (*functionname_ptr || *line_ptr)) |
b34976b6 | 7372 | return TRUE; |
d1fad7c6 NC |
7373 | |
7374 | if (symbols == NULL) | |
b34976b6 | 7375 | return FALSE; |
d1fad7c6 NC |
7376 | |
7377 | if (! elf_find_function (abfd, section, symbols, offset, | |
4e8a9624 | 7378 | filename_ptr, functionname_ptr)) |
b34976b6 | 7379 | return FALSE; |
d1fad7c6 | 7380 | |
252b5132 | 7381 | *line_ptr = 0; |
b34976b6 | 7382 | return TRUE; |
252b5132 RH |
7383 | } |
7384 | ||
5420f73d L |
7385 | /* Find the line for a symbol. */ |
7386 | ||
7387 | bfd_boolean | |
7388 | _bfd_elf_find_line (bfd *abfd, asymbol **symbols, asymbol *symbol, | |
7389 | const char **filename_ptr, unsigned int *line_ptr) | |
7390 | { | |
7391 | return _bfd_dwarf2_find_line (abfd, symbols, symbol, | |
7392 | filename_ptr, line_ptr, 0, | |
7393 | &elf_tdata (abfd)->dwarf2_find_line_info); | |
7394 | } | |
7395 | ||
4ab527b0 FF |
7396 | /* After a call to bfd_find_nearest_line, successive calls to |
7397 | bfd_find_inliner_info can be used to get source information about | |
7398 | each level of function inlining that terminated at the address | |
7399 | passed to bfd_find_nearest_line. Currently this is only supported | |
7400 | for DWARF2 with appropriate DWARF3 extensions. */ | |
7401 | ||
7402 | bfd_boolean | |
7403 | _bfd_elf_find_inliner_info (bfd *abfd, | |
7404 | const char **filename_ptr, | |
7405 | const char **functionname_ptr, | |
7406 | unsigned int *line_ptr) | |
7407 | { | |
7408 | bfd_boolean found; | |
7409 | found = _bfd_dwarf2_find_inliner_info (abfd, filename_ptr, | |
7410 | functionname_ptr, line_ptr, | |
7411 | & elf_tdata (abfd)->dwarf2_find_line_info); | |
7412 | return found; | |
7413 | } | |
7414 | ||
252b5132 | 7415 | int |
a6b96beb | 7416 | _bfd_elf_sizeof_headers (bfd *abfd, struct bfd_link_info *info) |
252b5132 | 7417 | { |
8ded5a0f AM |
7418 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
7419 | int ret = bed->s->sizeof_ehdr; | |
252b5132 | 7420 | |
a6b96beb | 7421 | if (!info->relocatable) |
8ded5a0f | 7422 | { |
62d7a5f6 | 7423 | bfd_size_type phdr_size = elf_tdata (abfd)->program_header_size; |
8ded5a0f | 7424 | |
62d7a5f6 AM |
7425 | if (phdr_size == (bfd_size_type) -1) |
7426 | { | |
7427 | struct elf_segment_map *m; | |
7428 | ||
7429 | phdr_size = 0; | |
7430 | for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next) | |
7431 | phdr_size += bed->s->sizeof_phdr; | |
8ded5a0f | 7432 | |
62d7a5f6 AM |
7433 | if (phdr_size == 0) |
7434 | phdr_size = get_program_header_size (abfd, info); | |
7435 | } | |
8ded5a0f AM |
7436 | |
7437 | elf_tdata (abfd)->program_header_size = phdr_size; | |
7438 | ret += phdr_size; | |
7439 | } | |
7440 | ||
252b5132 RH |
7441 | return ret; |
7442 | } | |
7443 | ||
b34976b6 | 7444 | bfd_boolean |
217aa764 AM |
7445 | _bfd_elf_set_section_contents (bfd *abfd, |
7446 | sec_ptr section, | |
0f867abe | 7447 | const void *location, |
217aa764 AM |
7448 | file_ptr offset, |
7449 | bfd_size_type count) | |
252b5132 RH |
7450 | { |
7451 | Elf_Internal_Shdr *hdr; | |
dc810e39 | 7452 | bfd_signed_vma pos; |
252b5132 RH |
7453 | |
7454 | if (! abfd->output_has_begun | |
217aa764 | 7455 | && ! _bfd_elf_compute_section_file_positions (abfd, NULL)) |
b34976b6 | 7456 | return FALSE; |
252b5132 RH |
7457 | |
7458 | hdr = &elf_section_data (section)->this_hdr; | |
dc810e39 AM |
7459 | pos = hdr->sh_offset + offset; |
7460 | if (bfd_seek (abfd, pos, SEEK_SET) != 0 | |
7461 | || bfd_bwrite (location, count, abfd) != count) | |
b34976b6 | 7462 | return FALSE; |
252b5132 | 7463 | |
b34976b6 | 7464 | return TRUE; |
252b5132 RH |
7465 | } |
7466 | ||
7467 | void | |
217aa764 AM |
7468 | _bfd_elf_no_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED, |
7469 | arelent *cache_ptr ATTRIBUTE_UNUSED, | |
7470 | Elf_Internal_Rela *dst ATTRIBUTE_UNUSED) | |
252b5132 RH |
7471 | { |
7472 | abort (); | |
7473 | } | |
7474 | ||
252b5132 RH |
7475 | /* Try to convert a non-ELF reloc into an ELF one. */ |
7476 | ||
b34976b6 | 7477 | bfd_boolean |
217aa764 | 7478 | _bfd_elf_validate_reloc (bfd *abfd, arelent *areloc) |
252b5132 | 7479 | { |
c044fabd | 7480 | /* Check whether we really have an ELF howto. */ |
252b5132 RH |
7481 | |
7482 | if ((*areloc->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec) | |
7483 | { | |
7484 | bfd_reloc_code_real_type code; | |
7485 | reloc_howto_type *howto; | |
7486 | ||
7487 | /* Alien reloc: Try to determine its type to replace it with an | |
c044fabd | 7488 | equivalent ELF reloc. */ |
252b5132 RH |
7489 | |
7490 | if (areloc->howto->pc_relative) | |
7491 | { | |
7492 | switch (areloc->howto->bitsize) | |
7493 | { | |
7494 | case 8: | |
7495 | code = BFD_RELOC_8_PCREL; | |
7496 | break; | |
7497 | case 12: | |
7498 | code = BFD_RELOC_12_PCREL; | |
7499 | break; | |
7500 | case 16: | |
7501 | code = BFD_RELOC_16_PCREL; | |
7502 | break; | |
7503 | case 24: | |
7504 | code = BFD_RELOC_24_PCREL; | |
7505 | break; | |
7506 | case 32: | |
7507 | code = BFD_RELOC_32_PCREL; | |
7508 | break; | |
7509 | case 64: | |
7510 | code = BFD_RELOC_64_PCREL; | |
7511 | break; | |
7512 | default: | |
7513 | goto fail; | |
7514 | } | |
7515 | ||
7516 | howto = bfd_reloc_type_lookup (abfd, code); | |
7517 | ||
7518 | if (areloc->howto->pcrel_offset != howto->pcrel_offset) | |
7519 | { | |
7520 | if (howto->pcrel_offset) | |
7521 | areloc->addend += areloc->address; | |
7522 | else | |
7523 | areloc->addend -= areloc->address; /* addend is unsigned!! */ | |
7524 | } | |
7525 | } | |
7526 | else | |
7527 | { | |
7528 | switch (areloc->howto->bitsize) | |
7529 | { | |
7530 | case 8: | |
7531 | code = BFD_RELOC_8; | |
7532 | break; | |
7533 | case 14: | |
7534 | code = BFD_RELOC_14; | |
7535 | break; | |
7536 | case 16: | |
7537 | code = BFD_RELOC_16; | |
7538 | break; | |
7539 | case 26: | |
7540 | code = BFD_RELOC_26; | |
7541 | break; | |
7542 | case 32: | |
7543 | code = BFD_RELOC_32; | |
7544 | break; | |
7545 | case 64: | |
7546 | code = BFD_RELOC_64; | |
7547 | break; | |
7548 | default: | |
7549 | goto fail; | |
7550 | } | |
7551 | ||
7552 | howto = bfd_reloc_type_lookup (abfd, code); | |
7553 | } | |
7554 | ||
7555 | if (howto) | |
7556 | areloc->howto = howto; | |
7557 | else | |
7558 | goto fail; | |
7559 | } | |
7560 | ||
b34976b6 | 7561 | return TRUE; |
252b5132 RH |
7562 | |
7563 | fail: | |
7564 | (*_bfd_error_handler) | |
d003868e AM |
7565 | (_("%B: unsupported relocation type %s"), |
7566 | abfd, areloc->howto->name); | |
252b5132 | 7567 | bfd_set_error (bfd_error_bad_value); |
b34976b6 | 7568 | return FALSE; |
252b5132 RH |
7569 | } |
7570 | ||
b34976b6 | 7571 | bfd_boolean |
217aa764 | 7572 | _bfd_elf_close_and_cleanup (bfd *abfd) |
252b5132 RH |
7573 | { |
7574 | if (bfd_get_format (abfd) == bfd_object) | |
7575 | { | |
b25e3d87 | 7576 | if (elf_tdata (abfd) != NULL && elf_shstrtab (abfd) != NULL) |
2b0f7ef9 | 7577 | _bfd_elf_strtab_free (elf_shstrtab (abfd)); |
6f140a15 | 7578 | _bfd_dwarf2_cleanup_debug_info (abfd); |
252b5132 RH |
7579 | } |
7580 | ||
7581 | return _bfd_generic_close_and_cleanup (abfd); | |
7582 | } | |
7583 | ||
7584 | /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY | |
7585 | in the relocation's offset. Thus we cannot allow any sort of sanity | |
7586 | range-checking to interfere. There is nothing else to do in processing | |
7587 | this reloc. */ | |
7588 | ||
7589 | bfd_reloc_status_type | |
217aa764 AM |
7590 | _bfd_elf_rel_vtable_reloc_fn |
7591 | (bfd *abfd ATTRIBUTE_UNUSED, arelent *re ATTRIBUTE_UNUSED, | |
fc0a2244 | 7592 | struct bfd_symbol *symbol ATTRIBUTE_UNUSED, |
217aa764 AM |
7593 | void *data ATTRIBUTE_UNUSED, asection *is ATTRIBUTE_UNUSED, |
7594 | bfd *obfd ATTRIBUTE_UNUSED, char **errmsg ATTRIBUTE_UNUSED) | |
252b5132 RH |
7595 | { |
7596 | return bfd_reloc_ok; | |
7597 | } | |
252b5132 RH |
7598 | \f |
7599 | /* Elf core file support. Much of this only works on native | |
7600 | toolchains, since we rely on knowing the | |
7601 | machine-dependent procfs structure in order to pick | |
c044fabd | 7602 | out details about the corefile. */ |
252b5132 RH |
7603 | |
7604 | #ifdef HAVE_SYS_PROCFS_H | |
16231b7b DG |
7605 | /* Needed for new procfs interface on sparc-solaris. */ |
7606 | # define _STRUCTURED_PROC 1 | |
252b5132 RH |
7607 | # include <sys/procfs.h> |
7608 | #endif | |
7609 | ||
261b8d08 PA |
7610 | /* Return a PID that identifies a "thread" for threaded cores, or the |
7611 | PID of the main process for non-threaded cores. */ | |
252b5132 RH |
7612 | |
7613 | static int | |
217aa764 | 7614 | elfcore_make_pid (bfd *abfd) |
252b5132 | 7615 | { |
261b8d08 PA |
7616 | int pid; |
7617 | ||
7618 | pid = elf_tdata (abfd)->core_lwpid; | |
7619 | if (pid == 0) | |
7620 | pid = elf_tdata (abfd)->core_pid; | |
7621 | ||
7622 | return pid; | |
252b5132 RH |
7623 | } |
7624 | ||
252b5132 RH |
7625 | /* If there isn't a section called NAME, make one, using |
7626 | data from SECT. Note, this function will generate a | |
7627 | reference to NAME, so you shouldn't deallocate or | |
c044fabd | 7628 | overwrite it. */ |
252b5132 | 7629 | |
b34976b6 | 7630 | static bfd_boolean |
217aa764 | 7631 | elfcore_maybe_make_sect (bfd *abfd, char *name, asection *sect) |
252b5132 | 7632 | { |
c044fabd | 7633 | asection *sect2; |
252b5132 RH |
7634 | |
7635 | if (bfd_get_section_by_name (abfd, name) != NULL) | |
b34976b6 | 7636 | return TRUE; |
252b5132 | 7637 | |
117ed4f8 | 7638 | sect2 = bfd_make_section_with_flags (abfd, name, sect->flags); |
252b5132 | 7639 | if (sect2 == NULL) |
b34976b6 | 7640 | return FALSE; |
252b5132 | 7641 | |
eea6121a | 7642 | sect2->size = sect->size; |
252b5132 | 7643 | sect2->filepos = sect->filepos; |
252b5132 | 7644 | sect2->alignment_power = sect->alignment_power; |
b34976b6 | 7645 | return TRUE; |
252b5132 RH |
7646 | } |
7647 | ||
bb0082d6 AM |
7648 | /* Create a pseudosection containing SIZE bytes at FILEPOS. This |
7649 | actually creates up to two pseudosections: | |
7650 | - For the single-threaded case, a section named NAME, unless | |
7651 | such a section already exists. | |
7652 | - For the multi-threaded case, a section named "NAME/PID", where | |
7653 | PID is elfcore_make_pid (abfd). | |
7654 | Both pseudosections have identical contents. */ | |
b34976b6 | 7655 | bfd_boolean |
217aa764 AM |
7656 | _bfd_elfcore_make_pseudosection (bfd *abfd, |
7657 | char *name, | |
7658 | size_t size, | |
7659 | ufile_ptr filepos) | |
bb0082d6 AM |
7660 | { |
7661 | char buf[100]; | |
7662 | char *threaded_name; | |
d4c88bbb | 7663 | size_t len; |
bb0082d6 AM |
7664 | asection *sect; |
7665 | ||
7666 | /* Build the section name. */ | |
7667 | ||
7668 | sprintf (buf, "%s/%d", name, elfcore_make_pid (abfd)); | |
d4c88bbb | 7669 | len = strlen (buf) + 1; |
a50b1753 | 7670 | threaded_name = (char *) bfd_alloc (abfd, len); |
bb0082d6 | 7671 | if (threaded_name == NULL) |
b34976b6 | 7672 | return FALSE; |
d4c88bbb | 7673 | memcpy (threaded_name, buf, len); |
bb0082d6 | 7674 | |
117ed4f8 AM |
7675 | sect = bfd_make_section_anyway_with_flags (abfd, threaded_name, |
7676 | SEC_HAS_CONTENTS); | |
bb0082d6 | 7677 | if (sect == NULL) |
b34976b6 | 7678 | return FALSE; |
eea6121a | 7679 | sect->size = size; |
bb0082d6 | 7680 | sect->filepos = filepos; |
bb0082d6 AM |
7681 | sect->alignment_power = 2; |
7682 | ||
936e320b | 7683 | return elfcore_maybe_make_sect (abfd, name, sect); |
bb0082d6 AM |
7684 | } |
7685 | ||
252b5132 | 7686 | /* prstatus_t exists on: |
4a938328 | 7687 | solaris 2.5+ |
252b5132 RH |
7688 | linux 2.[01] + glibc |
7689 | unixware 4.2 | |
7690 | */ | |
7691 | ||
7692 | #if defined (HAVE_PRSTATUS_T) | |
a7b97311 | 7693 | |
b34976b6 | 7694 | static bfd_boolean |
217aa764 | 7695 | elfcore_grok_prstatus (bfd *abfd, Elf_Internal_Note *note) |
252b5132 | 7696 | { |
eea6121a | 7697 | size_t size; |
7ee38065 | 7698 | int offset; |
252b5132 | 7699 | |
4a938328 MS |
7700 | if (note->descsz == sizeof (prstatus_t)) |
7701 | { | |
7702 | prstatus_t prstat; | |
252b5132 | 7703 | |
eea6121a | 7704 | size = sizeof (prstat.pr_reg); |
7ee38065 | 7705 | offset = offsetof (prstatus_t, pr_reg); |
4a938328 | 7706 | memcpy (&prstat, note->descdata, sizeof (prstat)); |
252b5132 | 7707 | |
fa49d224 NC |
7708 | /* Do not overwrite the core signal if it |
7709 | has already been set by another thread. */ | |
7710 | if (elf_tdata (abfd)->core_signal == 0) | |
7711 | elf_tdata (abfd)->core_signal = prstat.pr_cursig; | |
261b8d08 PA |
7712 | if (elf_tdata (abfd)->core_pid == 0) |
7713 | elf_tdata (abfd)->core_pid = prstat.pr_pid; | |
252b5132 | 7714 | |
4a938328 MS |
7715 | /* pr_who exists on: |
7716 | solaris 2.5+ | |
7717 | unixware 4.2 | |
7718 | pr_who doesn't exist on: | |
7719 | linux 2.[01] | |
7720 | */ | |
252b5132 | 7721 | #if defined (HAVE_PRSTATUS_T_PR_WHO) |
4a938328 | 7722 | elf_tdata (abfd)->core_lwpid = prstat.pr_who; |
261b8d08 PA |
7723 | #else |
7724 | elf_tdata (abfd)->core_lwpid = prstat.pr_pid; | |
252b5132 | 7725 | #endif |
4a938328 | 7726 | } |
7ee38065 | 7727 | #if defined (HAVE_PRSTATUS32_T) |
4a938328 MS |
7728 | else if (note->descsz == sizeof (prstatus32_t)) |
7729 | { | |
7730 | /* 64-bit host, 32-bit corefile */ | |
7731 | prstatus32_t prstat; | |
7732 | ||
eea6121a | 7733 | size = sizeof (prstat.pr_reg); |
7ee38065 | 7734 | offset = offsetof (prstatus32_t, pr_reg); |
4a938328 MS |
7735 | memcpy (&prstat, note->descdata, sizeof (prstat)); |
7736 | ||
fa49d224 NC |
7737 | /* Do not overwrite the core signal if it |
7738 | has already been set by another thread. */ | |
7739 | if (elf_tdata (abfd)->core_signal == 0) | |
7740 | elf_tdata (abfd)->core_signal = prstat.pr_cursig; | |
261b8d08 PA |
7741 | if (elf_tdata (abfd)->core_pid == 0) |
7742 | elf_tdata (abfd)->core_pid = prstat.pr_pid; | |
4a938328 MS |
7743 | |
7744 | /* pr_who exists on: | |
7745 | solaris 2.5+ | |
7746 | unixware 4.2 | |
7747 | pr_who doesn't exist on: | |
7748 | linux 2.[01] | |
7749 | */ | |
7ee38065 | 7750 | #if defined (HAVE_PRSTATUS32_T_PR_WHO) |
4a938328 | 7751 | elf_tdata (abfd)->core_lwpid = prstat.pr_who; |
261b8d08 PA |
7752 | #else |
7753 | elf_tdata (abfd)->core_lwpid = prstat.pr_pid; | |
4a938328 MS |
7754 | #endif |
7755 | } | |
7ee38065 | 7756 | #endif /* HAVE_PRSTATUS32_T */ |
4a938328 MS |
7757 | else |
7758 | { | |
7759 | /* Fail - we don't know how to handle any other | |
7760 | note size (ie. data object type). */ | |
b34976b6 | 7761 | return TRUE; |
4a938328 | 7762 | } |
252b5132 | 7763 | |
bb0082d6 | 7764 | /* Make a ".reg/999" section and a ".reg" section. */ |
936e320b | 7765 | return _bfd_elfcore_make_pseudosection (abfd, ".reg", |
eea6121a | 7766 | size, note->descpos + offset); |
252b5132 RH |
7767 | } |
7768 | #endif /* defined (HAVE_PRSTATUS_T) */ | |
7769 | ||
bb0082d6 | 7770 | /* Create a pseudosection containing the exact contents of NOTE. */ |
b34976b6 | 7771 | static bfd_boolean |
217aa764 AM |
7772 | elfcore_make_note_pseudosection (bfd *abfd, |
7773 | char *name, | |
7774 | Elf_Internal_Note *note) | |
252b5132 | 7775 | { |
936e320b AM |
7776 | return _bfd_elfcore_make_pseudosection (abfd, name, |
7777 | note->descsz, note->descpos); | |
252b5132 RH |
7778 | } |
7779 | ||
ff08c6bb JB |
7780 | /* There isn't a consistent prfpregset_t across platforms, |
7781 | but it doesn't matter, because we don't have to pick this | |
c044fabd KH |
7782 | data structure apart. */ |
7783 | ||
b34976b6 | 7784 | static bfd_boolean |
217aa764 | 7785 | elfcore_grok_prfpreg (bfd *abfd, Elf_Internal_Note *note) |
ff08c6bb JB |
7786 | { |
7787 | return elfcore_make_note_pseudosection (abfd, ".reg2", note); | |
7788 | } | |
7789 | ||
ff08c6bb | 7790 | /* Linux dumps the Intel SSE regs in a note named "LINUX" with a note |
971d4640 | 7791 | type of NT_PRXFPREG. Just include the whole note's contents |
ff08c6bb | 7792 | literally. */ |
c044fabd | 7793 | |
b34976b6 | 7794 | static bfd_boolean |
217aa764 | 7795 | elfcore_grok_prxfpreg (bfd *abfd, Elf_Internal_Note *note) |
ff08c6bb JB |
7796 | { |
7797 | return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note); | |
7798 | } | |
7799 | ||
4339cae0 L |
7800 | /* Linux dumps the Intel XSAVE extended state in a note named "LINUX" |
7801 | with a note type of NT_X86_XSTATE. Just include the whole note's | |
7802 | contents literally. */ | |
7803 | ||
7804 | static bfd_boolean | |
7805 | elfcore_grok_xstatereg (bfd *abfd, Elf_Internal_Note *note) | |
7806 | { | |
7807 | return elfcore_make_note_pseudosection (abfd, ".reg-xstate", note); | |
7808 | } | |
7809 | ||
97753bd5 AM |
7810 | static bfd_boolean |
7811 | elfcore_grok_ppc_vmx (bfd *abfd, Elf_Internal_Note *note) | |
7812 | { | |
7813 | return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vmx", note); | |
7814 | } | |
7815 | ||
89eeb0bc LM |
7816 | static bfd_boolean |
7817 | elfcore_grok_ppc_vsx (bfd *abfd, Elf_Internal_Note *note) | |
7818 | { | |
7819 | return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vsx", note); | |
7820 | } | |
97753bd5 | 7821 | |
0675e188 UW |
7822 | static bfd_boolean |
7823 | elfcore_grok_s390_high_gprs (bfd *abfd, Elf_Internal_Note *note) | |
7824 | { | |
7825 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-high-gprs", note); | |
7826 | } | |
7827 | ||
d7eeb400 MS |
7828 | static bfd_boolean |
7829 | elfcore_grok_s390_timer (bfd *abfd, Elf_Internal_Note *note) | |
7830 | { | |
7831 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-timer", note); | |
7832 | } | |
7833 | ||
7834 | static bfd_boolean | |
7835 | elfcore_grok_s390_todcmp (bfd *abfd, Elf_Internal_Note *note) | |
7836 | { | |
7837 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-todcmp", note); | |
7838 | } | |
7839 | ||
7840 | static bfd_boolean | |
7841 | elfcore_grok_s390_todpreg (bfd *abfd, Elf_Internal_Note *note) | |
7842 | { | |
7843 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-todpreg", note); | |
7844 | } | |
7845 | ||
7846 | static bfd_boolean | |
7847 | elfcore_grok_s390_ctrs (bfd *abfd, Elf_Internal_Note *note) | |
7848 | { | |
7849 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-ctrs", note); | |
7850 | } | |
7851 | ||
7852 | static bfd_boolean | |
7853 | elfcore_grok_s390_prefix (bfd *abfd, Elf_Internal_Note *note) | |
7854 | { | |
7855 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-prefix", note); | |
7856 | } | |
7857 | ||
252b5132 | 7858 | #if defined (HAVE_PRPSINFO_T) |
4a938328 | 7859 | typedef prpsinfo_t elfcore_psinfo_t; |
7ee38065 | 7860 | #if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */ |
4a938328 MS |
7861 | typedef prpsinfo32_t elfcore_psinfo32_t; |
7862 | #endif | |
252b5132 RH |
7863 | #endif |
7864 | ||
7865 | #if defined (HAVE_PSINFO_T) | |
4a938328 | 7866 | typedef psinfo_t elfcore_psinfo_t; |
7ee38065 | 7867 | #if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */ |
4a938328 MS |
7868 | typedef psinfo32_t elfcore_psinfo32_t; |
7869 | #endif | |
252b5132 RH |
7870 | #endif |
7871 | ||
252b5132 RH |
7872 | /* return a malloc'ed copy of a string at START which is at |
7873 | most MAX bytes long, possibly without a terminating '\0'. | |
c044fabd | 7874 | the copy will always have a terminating '\0'. */ |
252b5132 | 7875 | |
936e320b | 7876 | char * |
217aa764 | 7877 | _bfd_elfcore_strndup (bfd *abfd, char *start, size_t max) |
252b5132 | 7878 | { |
dc810e39 | 7879 | char *dups; |
a50b1753 | 7880 | char *end = (char *) memchr (start, '\0', max); |
dc810e39 | 7881 | size_t len; |
252b5132 RH |
7882 | |
7883 | if (end == NULL) | |
7884 | len = max; | |
7885 | else | |
7886 | len = end - start; | |
7887 | ||
a50b1753 | 7888 | dups = (char *) bfd_alloc (abfd, len + 1); |
dc810e39 | 7889 | if (dups == NULL) |
252b5132 RH |
7890 | return NULL; |
7891 | ||
dc810e39 AM |
7892 | memcpy (dups, start, len); |
7893 | dups[len] = '\0'; | |
252b5132 | 7894 | |
dc810e39 | 7895 | return dups; |
252b5132 RH |
7896 | } |
7897 | ||
bb0082d6 | 7898 | #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) |
b34976b6 | 7899 | static bfd_boolean |
217aa764 | 7900 | elfcore_grok_psinfo (bfd *abfd, Elf_Internal_Note *note) |
252b5132 | 7901 | { |
4a938328 MS |
7902 | if (note->descsz == sizeof (elfcore_psinfo_t)) |
7903 | { | |
7904 | elfcore_psinfo_t psinfo; | |
252b5132 | 7905 | |
7ee38065 | 7906 | memcpy (&psinfo, note->descdata, sizeof (psinfo)); |
252b5132 | 7907 | |
4a938328 | 7908 | elf_tdata (abfd)->core_program |
936e320b AM |
7909 | = _bfd_elfcore_strndup (abfd, psinfo.pr_fname, |
7910 | sizeof (psinfo.pr_fname)); | |
252b5132 | 7911 | |
4a938328 | 7912 | elf_tdata (abfd)->core_command |
936e320b AM |
7913 | = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs, |
7914 | sizeof (psinfo.pr_psargs)); | |
4a938328 | 7915 | } |
7ee38065 | 7916 | #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T) |
4a938328 MS |
7917 | else if (note->descsz == sizeof (elfcore_psinfo32_t)) |
7918 | { | |
7919 | /* 64-bit host, 32-bit corefile */ | |
7920 | elfcore_psinfo32_t psinfo; | |
7921 | ||
7ee38065 | 7922 | memcpy (&psinfo, note->descdata, sizeof (psinfo)); |
252b5132 | 7923 | |
4a938328 | 7924 | elf_tdata (abfd)->core_program |
936e320b AM |
7925 | = _bfd_elfcore_strndup (abfd, psinfo.pr_fname, |
7926 | sizeof (psinfo.pr_fname)); | |
4a938328 MS |
7927 | |
7928 | elf_tdata (abfd)->core_command | |
936e320b AM |
7929 | = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs, |
7930 | sizeof (psinfo.pr_psargs)); | |
4a938328 MS |
7931 | } |
7932 | #endif | |
7933 | ||
7934 | else | |
7935 | { | |
7936 | /* Fail - we don't know how to handle any other | |
7937 | note size (ie. data object type). */ | |
b34976b6 | 7938 | return TRUE; |
4a938328 | 7939 | } |
252b5132 RH |
7940 | |
7941 | /* Note that for some reason, a spurious space is tacked | |
7942 | onto the end of the args in some (at least one anyway) | |
c044fabd | 7943 | implementations, so strip it off if it exists. */ |
252b5132 RH |
7944 | |
7945 | { | |
c044fabd | 7946 | char *command = elf_tdata (abfd)->core_command; |
252b5132 RH |
7947 | int n = strlen (command); |
7948 | ||
7949 | if (0 < n && command[n - 1] == ' ') | |
7950 | command[n - 1] = '\0'; | |
7951 | } | |
7952 | ||
b34976b6 | 7953 | return TRUE; |
252b5132 RH |
7954 | } |
7955 | #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */ | |
7956 | ||
252b5132 | 7957 | #if defined (HAVE_PSTATUS_T) |
b34976b6 | 7958 | static bfd_boolean |
217aa764 | 7959 | elfcore_grok_pstatus (bfd *abfd, Elf_Internal_Note *note) |
252b5132 | 7960 | { |
f572a39d AM |
7961 | if (note->descsz == sizeof (pstatus_t) |
7962 | #if defined (HAVE_PXSTATUS_T) | |
7963 | || note->descsz == sizeof (pxstatus_t) | |
7964 | #endif | |
7965 | ) | |
4a938328 MS |
7966 | { |
7967 | pstatus_t pstat; | |
252b5132 | 7968 | |
4a938328 | 7969 | memcpy (&pstat, note->descdata, sizeof (pstat)); |
252b5132 | 7970 | |
4a938328 MS |
7971 | elf_tdata (abfd)->core_pid = pstat.pr_pid; |
7972 | } | |
7ee38065 | 7973 | #if defined (HAVE_PSTATUS32_T) |
4a938328 MS |
7974 | else if (note->descsz == sizeof (pstatus32_t)) |
7975 | { | |
7976 | /* 64-bit host, 32-bit corefile */ | |
7977 | pstatus32_t pstat; | |
252b5132 | 7978 | |
4a938328 | 7979 | memcpy (&pstat, note->descdata, sizeof (pstat)); |
252b5132 | 7980 | |
4a938328 MS |
7981 | elf_tdata (abfd)->core_pid = pstat.pr_pid; |
7982 | } | |
7983 | #endif | |
252b5132 RH |
7984 | /* Could grab some more details from the "representative" |
7985 | lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an | |
c044fabd | 7986 | NT_LWPSTATUS note, presumably. */ |
252b5132 | 7987 | |
b34976b6 | 7988 | return TRUE; |
252b5132 RH |
7989 | } |
7990 | #endif /* defined (HAVE_PSTATUS_T) */ | |
7991 | ||
252b5132 | 7992 | #if defined (HAVE_LWPSTATUS_T) |
b34976b6 | 7993 | static bfd_boolean |
217aa764 | 7994 | elfcore_grok_lwpstatus (bfd *abfd, Elf_Internal_Note *note) |
252b5132 RH |
7995 | { |
7996 | lwpstatus_t lwpstat; | |
7997 | char buf[100]; | |
c044fabd | 7998 | char *name; |
d4c88bbb | 7999 | size_t len; |
c044fabd | 8000 | asection *sect; |
252b5132 | 8001 | |
f572a39d AM |
8002 | if (note->descsz != sizeof (lwpstat) |
8003 | #if defined (HAVE_LWPXSTATUS_T) | |
8004 | && note->descsz != sizeof (lwpxstatus_t) | |
8005 | #endif | |
8006 | ) | |
b34976b6 | 8007 | return TRUE; |
252b5132 RH |
8008 | |
8009 | memcpy (&lwpstat, note->descdata, sizeof (lwpstat)); | |
8010 | ||
8011 | elf_tdata (abfd)->core_lwpid = lwpstat.pr_lwpid; | |
a1504221 JB |
8012 | /* Do not overwrite the core signal if it has already been set by |
8013 | another thread. */ | |
8014 | if (elf_tdata (abfd)->core_signal == 0) | |
8015 | elf_tdata (abfd)->core_signal = lwpstat.pr_cursig; | |
252b5132 | 8016 | |
c044fabd | 8017 | /* Make a ".reg/999" section. */ |
252b5132 RH |
8018 | |
8019 | sprintf (buf, ".reg/%d", elfcore_make_pid (abfd)); | |
d4c88bbb | 8020 | len = strlen (buf) + 1; |
217aa764 | 8021 | name = bfd_alloc (abfd, len); |
252b5132 | 8022 | if (name == NULL) |
b34976b6 | 8023 | return FALSE; |
d4c88bbb | 8024 | memcpy (name, buf, len); |
252b5132 | 8025 | |
117ed4f8 | 8026 | sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); |
252b5132 | 8027 | if (sect == NULL) |
b34976b6 | 8028 | return FALSE; |
252b5132 RH |
8029 | |
8030 | #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT) | |
eea6121a | 8031 | sect->size = sizeof (lwpstat.pr_context.uc_mcontext.gregs); |
252b5132 RH |
8032 | sect->filepos = note->descpos |
8033 | + offsetof (lwpstatus_t, pr_context.uc_mcontext.gregs); | |
8034 | #endif | |
8035 | ||
8036 | #if defined (HAVE_LWPSTATUS_T_PR_REG) | |
eea6121a | 8037 | sect->size = sizeof (lwpstat.pr_reg); |
252b5132 RH |
8038 | sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_reg); |
8039 | #endif | |
8040 | ||
252b5132 RH |
8041 | sect->alignment_power = 2; |
8042 | ||
8043 | if (!elfcore_maybe_make_sect (abfd, ".reg", sect)) | |
b34976b6 | 8044 | return FALSE; |
252b5132 RH |
8045 | |
8046 | /* Make a ".reg2/999" section */ | |
8047 | ||
8048 | sprintf (buf, ".reg2/%d", elfcore_make_pid (abfd)); | |
d4c88bbb | 8049 | len = strlen (buf) + 1; |
217aa764 | 8050 | name = bfd_alloc (abfd, len); |
252b5132 | 8051 | if (name == NULL) |
b34976b6 | 8052 | return FALSE; |
d4c88bbb | 8053 | memcpy (name, buf, len); |
252b5132 | 8054 | |
117ed4f8 | 8055 | sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); |
252b5132 | 8056 | if (sect == NULL) |
b34976b6 | 8057 | return FALSE; |
252b5132 RH |
8058 | |
8059 | #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT) | |
eea6121a | 8060 | sect->size = sizeof (lwpstat.pr_context.uc_mcontext.fpregs); |
252b5132 RH |
8061 | sect->filepos = note->descpos |
8062 | + offsetof (lwpstatus_t, pr_context.uc_mcontext.fpregs); | |
8063 | #endif | |
8064 | ||
8065 | #if defined (HAVE_LWPSTATUS_T_PR_FPREG) | |
eea6121a | 8066 | sect->size = sizeof (lwpstat.pr_fpreg); |
252b5132 RH |
8067 | sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_fpreg); |
8068 | #endif | |
8069 | ||
252b5132 RH |
8070 | sect->alignment_power = 2; |
8071 | ||
936e320b | 8072 | return elfcore_maybe_make_sect (abfd, ".reg2", sect); |
252b5132 RH |
8073 | } |
8074 | #endif /* defined (HAVE_LWPSTATUS_T) */ | |
8075 | ||
b34976b6 | 8076 | static bfd_boolean |
217aa764 | 8077 | elfcore_grok_win32pstatus (bfd *abfd, Elf_Internal_Note *note) |
16e9c715 NC |
8078 | { |
8079 | char buf[30]; | |
c044fabd | 8080 | char *name; |
d4c88bbb | 8081 | size_t len; |
c044fabd | 8082 | asection *sect; |
4a6636fb PA |
8083 | int type; |
8084 | int is_active_thread; | |
8085 | bfd_vma base_addr; | |
16e9c715 | 8086 | |
4a6636fb | 8087 | if (note->descsz < 728) |
b34976b6 | 8088 | return TRUE; |
16e9c715 | 8089 | |
4a6636fb PA |
8090 | if (! CONST_STRNEQ (note->namedata, "win32")) |
8091 | return TRUE; | |
8092 | ||
8093 | type = bfd_get_32 (abfd, note->descdata); | |
c044fabd | 8094 | |
4a6636fb | 8095 | switch (type) |
16e9c715 | 8096 | { |
4a6636fb | 8097 | case 1 /* NOTE_INFO_PROCESS */: |
16e9c715 | 8098 | /* FIXME: need to add ->core_command. */ |
4a6636fb PA |
8099 | /* process_info.pid */ |
8100 | elf_tdata (abfd)->core_pid = bfd_get_32 (abfd, note->descdata + 8); | |
8101 | /* process_info.signal */ | |
8102 | elf_tdata (abfd)->core_signal = bfd_get_32 (abfd, note->descdata + 12); | |
c044fabd | 8103 | break; |
16e9c715 | 8104 | |
4a6636fb | 8105 | case 2 /* NOTE_INFO_THREAD */: |
16e9c715 | 8106 | /* Make a ".reg/999" section. */ |
4a6636fb PA |
8107 | /* thread_info.tid */ |
8108 | sprintf (buf, ".reg/%ld", (long) bfd_get_32 (abfd, note->descdata + 8)); | |
c044fabd | 8109 | |
d4c88bbb | 8110 | len = strlen (buf) + 1; |
a50b1753 | 8111 | name = (char *) bfd_alloc (abfd, len); |
16e9c715 | 8112 | if (name == NULL) |
b34976b6 | 8113 | return FALSE; |
c044fabd | 8114 | |
d4c88bbb | 8115 | memcpy (name, buf, len); |
16e9c715 | 8116 | |
117ed4f8 | 8117 | sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); |
16e9c715 | 8118 | if (sect == NULL) |
b34976b6 | 8119 | return FALSE; |
c044fabd | 8120 | |
4a6636fb PA |
8121 | /* sizeof (thread_info.thread_context) */ |
8122 | sect->size = 716; | |
8123 | /* offsetof (thread_info.thread_context) */ | |
8124 | sect->filepos = note->descpos + 12; | |
16e9c715 NC |
8125 | sect->alignment_power = 2; |
8126 | ||
4a6636fb PA |
8127 | /* thread_info.is_active_thread */ |
8128 | is_active_thread = bfd_get_32 (abfd, note->descdata + 8); | |
8129 | ||
8130 | if (is_active_thread) | |
16e9c715 | 8131 | if (! elfcore_maybe_make_sect (abfd, ".reg", sect)) |
b34976b6 | 8132 | return FALSE; |
16e9c715 NC |
8133 | break; |
8134 | ||
4a6636fb | 8135 | case 3 /* NOTE_INFO_MODULE */: |
16e9c715 | 8136 | /* Make a ".module/xxxxxxxx" section. */ |
4a6636fb PA |
8137 | /* module_info.base_address */ |
8138 | base_addr = bfd_get_32 (abfd, note->descdata + 4); | |
0af1713e | 8139 | sprintf (buf, ".module/%08lx", (unsigned long) base_addr); |
c044fabd | 8140 | |
d4c88bbb | 8141 | len = strlen (buf) + 1; |
a50b1753 | 8142 | name = (char *) bfd_alloc (abfd, len); |
16e9c715 | 8143 | if (name == NULL) |
b34976b6 | 8144 | return FALSE; |
c044fabd | 8145 | |
d4c88bbb | 8146 | memcpy (name, buf, len); |
252b5132 | 8147 | |
117ed4f8 | 8148 | sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); |
c044fabd | 8149 | |
16e9c715 | 8150 | if (sect == NULL) |
b34976b6 | 8151 | return FALSE; |
c044fabd | 8152 | |
eea6121a | 8153 | sect->size = note->descsz; |
16e9c715 | 8154 | sect->filepos = note->descpos; |
16e9c715 NC |
8155 | sect->alignment_power = 2; |
8156 | break; | |
8157 | ||
8158 | default: | |
b34976b6 | 8159 | return TRUE; |
16e9c715 NC |
8160 | } |
8161 | ||
b34976b6 | 8162 | return TRUE; |
16e9c715 | 8163 | } |
252b5132 | 8164 | |
b34976b6 | 8165 | static bfd_boolean |
217aa764 | 8166 | elfcore_grok_note (bfd *abfd, Elf_Internal_Note *note) |
252b5132 | 8167 | { |
9c5bfbb7 | 8168 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
bb0082d6 | 8169 | |
252b5132 RH |
8170 | switch (note->type) |
8171 | { | |
8172 | default: | |
b34976b6 | 8173 | return TRUE; |
252b5132 | 8174 | |
252b5132 | 8175 | case NT_PRSTATUS: |
bb0082d6 AM |
8176 | if (bed->elf_backend_grok_prstatus) |
8177 | if ((*bed->elf_backend_grok_prstatus) (abfd, note)) | |
b34976b6 | 8178 | return TRUE; |
bb0082d6 | 8179 | #if defined (HAVE_PRSTATUS_T) |
252b5132 | 8180 | return elfcore_grok_prstatus (abfd, note); |
bb0082d6 | 8181 | #else |
b34976b6 | 8182 | return TRUE; |
252b5132 RH |
8183 | #endif |
8184 | ||
8185 | #if defined (HAVE_PSTATUS_T) | |
8186 | case NT_PSTATUS: | |
8187 | return elfcore_grok_pstatus (abfd, note); | |
8188 | #endif | |
8189 | ||
8190 | #if defined (HAVE_LWPSTATUS_T) | |
8191 | case NT_LWPSTATUS: | |
8192 | return elfcore_grok_lwpstatus (abfd, note); | |
8193 | #endif | |
8194 | ||
8195 | case NT_FPREGSET: /* FIXME: rename to NT_PRFPREG */ | |
8196 | return elfcore_grok_prfpreg (abfd, note); | |
8197 | ||
c044fabd | 8198 | case NT_WIN32PSTATUS: |
16e9c715 | 8199 | return elfcore_grok_win32pstatus (abfd, note); |
16e9c715 | 8200 | |
c044fabd | 8201 | case NT_PRXFPREG: /* Linux SSE extension */ |
e377ab71 MK |
8202 | if (note->namesz == 6 |
8203 | && strcmp (note->namedata, "LINUX") == 0) | |
ff08c6bb JB |
8204 | return elfcore_grok_prxfpreg (abfd, note); |
8205 | else | |
b34976b6 | 8206 | return TRUE; |
ff08c6bb | 8207 | |
4339cae0 L |
8208 | case NT_X86_XSTATE: /* Linux XSAVE extension */ |
8209 | if (note->namesz == 6 | |
8210 | && strcmp (note->namedata, "LINUX") == 0) | |
8211 | return elfcore_grok_xstatereg (abfd, note); | |
8212 | else | |
8213 | return TRUE; | |
8214 | ||
97753bd5 AM |
8215 | case NT_PPC_VMX: |
8216 | if (note->namesz == 6 | |
8217 | && strcmp (note->namedata, "LINUX") == 0) | |
8218 | return elfcore_grok_ppc_vmx (abfd, note); | |
8219 | else | |
8220 | return TRUE; | |
8221 | ||
89eeb0bc LM |
8222 | case NT_PPC_VSX: |
8223 | if (note->namesz == 6 | |
8224 | && strcmp (note->namedata, "LINUX") == 0) | |
8225 | return elfcore_grok_ppc_vsx (abfd, note); | |
8226 | else | |
8227 | return TRUE; | |
8228 | ||
0675e188 UW |
8229 | case NT_S390_HIGH_GPRS: |
8230 | if (note->namesz == 6 | |
8231 | && strcmp (note->namedata, "LINUX") == 0) | |
8232 | return elfcore_grok_s390_high_gprs (abfd, note); | |
8233 | else | |
8234 | return TRUE; | |
8235 | ||
d7eeb400 MS |
8236 | case NT_S390_TIMER: |
8237 | if (note->namesz == 6 | |
8238 | && strcmp (note->namedata, "LINUX") == 0) | |
8239 | return elfcore_grok_s390_timer (abfd, note); | |
8240 | else | |
8241 | return TRUE; | |
8242 | ||
8243 | case NT_S390_TODCMP: | |
8244 | if (note->namesz == 6 | |
8245 | && strcmp (note->namedata, "LINUX") == 0) | |
8246 | return elfcore_grok_s390_todcmp (abfd, note); | |
8247 | else | |
8248 | return TRUE; | |
8249 | ||
8250 | case NT_S390_TODPREG: | |
8251 | if (note->namesz == 6 | |
8252 | && strcmp (note->namedata, "LINUX") == 0) | |
8253 | return elfcore_grok_s390_todpreg (abfd, note); | |
8254 | else | |
8255 | return TRUE; | |
8256 | ||
8257 | case NT_S390_CTRS: | |
8258 | if (note->namesz == 6 | |
8259 | && strcmp (note->namedata, "LINUX") == 0) | |
8260 | return elfcore_grok_s390_ctrs (abfd, note); | |
8261 | else | |
8262 | return TRUE; | |
8263 | ||
8264 | case NT_S390_PREFIX: | |
8265 | if (note->namesz == 6 | |
8266 | && strcmp (note->namedata, "LINUX") == 0) | |
8267 | return elfcore_grok_s390_prefix (abfd, note); | |
8268 | else | |
8269 | return TRUE; | |
8270 | ||
252b5132 RH |
8271 | case NT_PRPSINFO: |
8272 | case NT_PSINFO: | |
bb0082d6 AM |
8273 | if (bed->elf_backend_grok_psinfo) |
8274 | if ((*bed->elf_backend_grok_psinfo) (abfd, note)) | |
b34976b6 | 8275 | return TRUE; |
bb0082d6 | 8276 | #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) |
252b5132 | 8277 | return elfcore_grok_psinfo (abfd, note); |
bb0082d6 | 8278 | #else |
b34976b6 | 8279 | return TRUE; |
252b5132 | 8280 | #endif |
3333a7c3 RM |
8281 | |
8282 | case NT_AUXV: | |
8283 | { | |
117ed4f8 AM |
8284 | asection *sect = bfd_make_section_anyway_with_flags (abfd, ".auxv", |
8285 | SEC_HAS_CONTENTS); | |
3333a7c3 RM |
8286 | |
8287 | if (sect == NULL) | |
8288 | return FALSE; | |
eea6121a | 8289 | sect->size = note->descsz; |
3333a7c3 | 8290 | sect->filepos = note->descpos; |
3333a7c3 RM |
8291 | sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32; |
8292 | ||
8293 | return TRUE; | |
8294 | } | |
252b5132 RH |
8295 | } |
8296 | } | |
8297 | ||
718175fa JK |
8298 | static bfd_boolean |
8299 | elfobj_grok_gnu_build_id (bfd *abfd, Elf_Internal_Note *note) | |
8300 | { | |
8301 | elf_tdata (abfd)->build_id_size = note->descsz; | |
a50b1753 | 8302 | elf_tdata (abfd)->build_id = (bfd_byte *) bfd_alloc (abfd, note->descsz); |
718175fa JK |
8303 | if (elf_tdata (abfd)->build_id == NULL) |
8304 | return FALSE; | |
8305 | ||
8306 | memcpy (elf_tdata (abfd)->build_id, note->descdata, note->descsz); | |
8307 | ||
8308 | return TRUE; | |
8309 | } | |
8310 | ||
8311 | static bfd_boolean | |
8312 | elfobj_grok_gnu_note (bfd *abfd, Elf_Internal_Note *note) | |
8313 | { | |
8314 | switch (note->type) | |
8315 | { | |
8316 | default: | |
8317 | return TRUE; | |
8318 | ||
8319 | case NT_GNU_BUILD_ID: | |
8320 | return elfobj_grok_gnu_build_id (abfd, note); | |
8321 | } | |
8322 | } | |
8323 | ||
b34976b6 | 8324 | static bfd_boolean |
217aa764 | 8325 | elfcore_netbsd_get_lwpid (Elf_Internal_Note *note, int *lwpidp) |
50b2bdb7 AM |
8326 | { |
8327 | char *cp; | |
8328 | ||
8329 | cp = strchr (note->namedata, '@'); | |
8330 | if (cp != NULL) | |
8331 | { | |
d2b64500 | 8332 | *lwpidp = atoi(cp + 1); |
b34976b6 | 8333 | return TRUE; |
50b2bdb7 | 8334 | } |
b34976b6 | 8335 | return FALSE; |
50b2bdb7 AM |
8336 | } |
8337 | ||
b34976b6 | 8338 | static bfd_boolean |
217aa764 | 8339 | elfcore_grok_netbsd_procinfo (bfd *abfd, Elf_Internal_Note *note) |
50b2bdb7 | 8340 | { |
50b2bdb7 AM |
8341 | /* Signal number at offset 0x08. */ |
8342 | elf_tdata (abfd)->core_signal | |
8343 | = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08); | |
8344 | ||
8345 | /* Process ID at offset 0x50. */ | |
8346 | elf_tdata (abfd)->core_pid | |
8347 | = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x50); | |
8348 | ||
8349 | /* Command name at 0x7c (max 32 bytes, including nul). */ | |
8350 | elf_tdata (abfd)->core_command | |
8351 | = _bfd_elfcore_strndup (abfd, note->descdata + 0x7c, 31); | |
8352 | ||
7720ba9f MK |
8353 | return elfcore_make_note_pseudosection (abfd, ".note.netbsdcore.procinfo", |
8354 | note); | |
50b2bdb7 AM |
8355 | } |
8356 | ||
b34976b6 | 8357 | static bfd_boolean |
217aa764 | 8358 | elfcore_grok_netbsd_note (bfd *abfd, Elf_Internal_Note *note) |
50b2bdb7 AM |
8359 | { |
8360 | int lwp; | |
8361 | ||
8362 | if (elfcore_netbsd_get_lwpid (note, &lwp)) | |
8363 | elf_tdata (abfd)->core_lwpid = lwp; | |
8364 | ||
b4db1224 | 8365 | if (note->type == NT_NETBSDCORE_PROCINFO) |
50b2bdb7 AM |
8366 | { |
8367 | /* NetBSD-specific core "procinfo". Note that we expect to | |
08a40648 AM |
8368 | find this note before any of the others, which is fine, |
8369 | since the kernel writes this note out first when it | |
8370 | creates a core file. */ | |
47d9a591 | 8371 | |
50b2bdb7 AM |
8372 | return elfcore_grok_netbsd_procinfo (abfd, note); |
8373 | } | |
8374 | ||
b4db1224 JT |
8375 | /* As of Jan 2002 there are no other machine-independent notes |
8376 | defined for NetBSD core files. If the note type is less | |
8377 | than the start of the machine-dependent note types, we don't | |
8378 | understand it. */ | |
47d9a591 | 8379 | |
b4db1224 | 8380 | if (note->type < NT_NETBSDCORE_FIRSTMACH) |
b34976b6 | 8381 | return TRUE; |
50b2bdb7 AM |
8382 | |
8383 | ||
8384 | switch (bfd_get_arch (abfd)) | |
8385 | { | |
08a40648 AM |
8386 | /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and |
8387 | PT_GETFPREGS == mach+2. */ | |
50b2bdb7 AM |
8388 | |
8389 | case bfd_arch_alpha: | |
8390 | case bfd_arch_sparc: | |
8391 | switch (note->type) | |
08a40648 AM |
8392 | { |
8393 | case NT_NETBSDCORE_FIRSTMACH+0: | |
8394 | return elfcore_make_note_pseudosection (abfd, ".reg", note); | |
50b2bdb7 | 8395 | |
08a40648 AM |
8396 | case NT_NETBSDCORE_FIRSTMACH+2: |
8397 | return elfcore_make_note_pseudosection (abfd, ".reg2", note); | |
50b2bdb7 | 8398 | |
08a40648 AM |
8399 | default: |
8400 | return TRUE; | |
8401 | } | |
50b2bdb7 | 8402 | |
08a40648 AM |
8403 | /* On all other arch's, PT_GETREGS == mach+1 and |
8404 | PT_GETFPREGS == mach+3. */ | |
50b2bdb7 AM |
8405 | |
8406 | default: | |
8407 | switch (note->type) | |
08a40648 AM |
8408 | { |
8409 | case NT_NETBSDCORE_FIRSTMACH+1: | |
8410 | return elfcore_make_note_pseudosection (abfd, ".reg", note); | |
50b2bdb7 | 8411 | |
08a40648 AM |
8412 | case NT_NETBSDCORE_FIRSTMACH+3: |
8413 | return elfcore_make_note_pseudosection (abfd, ".reg2", note); | |
50b2bdb7 | 8414 | |
08a40648 AM |
8415 | default: |
8416 | return TRUE; | |
8417 | } | |
50b2bdb7 AM |
8418 | } |
8419 | /* NOTREACHED */ | |
8420 | } | |
8421 | ||
67cc5033 MK |
8422 | static bfd_boolean |
8423 | elfcore_grok_openbsd_procinfo (bfd *abfd, Elf_Internal_Note *note) | |
8424 | { | |
8425 | /* Signal number at offset 0x08. */ | |
8426 | elf_tdata (abfd)->core_signal | |
8427 | = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08); | |
8428 | ||
8429 | /* Process ID at offset 0x20. */ | |
8430 | elf_tdata (abfd)->core_pid | |
8431 | = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x20); | |
8432 | ||
8433 | /* Command name at 0x48 (max 32 bytes, including nul). */ | |
8434 | elf_tdata (abfd)->core_command | |
8435 | = _bfd_elfcore_strndup (abfd, note->descdata + 0x48, 31); | |
8436 | ||
8437 | return TRUE; | |
8438 | } | |
8439 | ||
8440 | static bfd_boolean | |
8441 | elfcore_grok_openbsd_note (bfd *abfd, Elf_Internal_Note *note) | |
8442 | { | |
8443 | if (note->type == NT_OPENBSD_PROCINFO) | |
8444 | return elfcore_grok_openbsd_procinfo (abfd, note); | |
8445 | ||
8446 | if (note->type == NT_OPENBSD_REGS) | |
8447 | return elfcore_make_note_pseudosection (abfd, ".reg", note); | |
8448 | ||
8449 | if (note->type == NT_OPENBSD_FPREGS) | |
8450 | return elfcore_make_note_pseudosection (abfd, ".reg2", note); | |
8451 | ||
8452 | if (note->type == NT_OPENBSD_XFPREGS) | |
8453 | return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note); | |
8454 | ||
8455 | if (note->type == NT_OPENBSD_AUXV) | |
8456 | { | |
8457 | asection *sect = bfd_make_section_anyway_with_flags (abfd, ".auxv", | |
8458 | SEC_HAS_CONTENTS); | |
8459 | ||
8460 | if (sect == NULL) | |
8461 | return FALSE; | |
8462 | sect->size = note->descsz; | |
8463 | sect->filepos = note->descpos; | |
8464 | sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32; | |
8465 | ||
8466 | return TRUE; | |
8467 | } | |
8468 | ||
8469 | if (note->type == NT_OPENBSD_WCOOKIE) | |
8470 | { | |
8471 | asection *sect = bfd_make_section_anyway_with_flags (abfd, ".wcookie", | |
8472 | SEC_HAS_CONTENTS); | |
8473 | ||
8474 | if (sect == NULL) | |
8475 | return FALSE; | |
8476 | sect->size = note->descsz; | |
8477 | sect->filepos = note->descpos; | |
8478 | sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32; | |
8479 | ||
8480 | return TRUE; | |
8481 | } | |
8482 | ||
8483 | return TRUE; | |
8484 | } | |
8485 | ||
07c6e936 | 8486 | static bfd_boolean |
d3fd4074 | 8487 | elfcore_grok_nto_status (bfd *abfd, Elf_Internal_Note *note, long *tid) |
07c6e936 NC |
8488 | { |
8489 | void *ddata = note->descdata; | |
8490 | char buf[100]; | |
8491 | char *name; | |
8492 | asection *sect; | |
f8843e87 AM |
8493 | short sig; |
8494 | unsigned flags; | |
07c6e936 NC |
8495 | |
8496 | /* nto_procfs_status 'pid' field is at offset 0. */ | |
8497 | elf_tdata (abfd)->core_pid = bfd_get_32 (abfd, (bfd_byte *) ddata); | |
8498 | ||
f8843e87 AM |
8499 | /* nto_procfs_status 'tid' field is at offset 4. Pass it back. */ |
8500 | *tid = bfd_get_32 (abfd, (bfd_byte *) ddata + 4); | |
8501 | ||
8502 | /* nto_procfs_status 'flags' field is at offset 8. */ | |
8503 | flags = bfd_get_32 (abfd, (bfd_byte *) ddata + 8); | |
07c6e936 NC |
8504 | |
8505 | /* nto_procfs_status 'what' field is at offset 14. */ | |
f8843e87 AM |
8506 | if ((sig = bfd_get_16 (abfd, (bfd_byte *) ddata + 14)) > 0) |
8507 | { | |
8508 | elf_tdata (abfd)->core_signal = sig; | |
8509 | elf_tdata (abfd)->core_lwpid = *tid; | |
8510 | } | |
07c6e936 | 8511 | |
f8843e87 AM |
8512 | /* _DEBUG_FLAG_CURTID (current thread) is 0x80. Some cores |
8513 | do not come from signals so we make sure we set the current | |
8514 | thread just in case. */ | |
8515 | if (flags & 0x00000080) | |
8516 | elf_tdata (abfd)->core_lwpid = *tid; | |
07c6e936 NC |
8517 | |
8518 | /* Make a ".qnx_core_status/%d" section. */ | |
d3fd4074 | 8519 | sprintf (buf, ".qnx_core_status/%ld", *tid); |
07c6e936 | 8520 | |
a50b1753 | 8521 | name = (char *) bfd_alloc (abfd, strlen (buf) + 1); |
07c6e936 NC |
8522 | if (name == NULL) |
8523 | return FALSE; | |
8524 | strcpy (name, buf); | |
8525 | ||
117ed4f8 | 8526 | sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); |
07c6e936 NC |
8527 | if (sect == NULL) |
8528 | return FALSE; | |
8529 | ||
eea6121a | 8530 | sect->size = note->descsz; |
07c6e936 | 8531 | sect->filepos = note->descpos; |
07c6e936 NC |
8532 | sect->alignment_power = 2; |
8533 | ||
8534 | return (elfcore_maybe_make_sect (abfd, ".qnx_core_status", sect)); | |
8535 | } | |
8536 | ||
8537 | static bfd_boolean | |
d69f560c KW |
8538 | elfcore_grok_nto_regs (bfd *abfd, |
8539 | Elf_Internal_Note *note, | |
d3fd4074 | 8540 | long tid, |
d69f560c | 8541 | char *base) |
07c6e936 NC |
8542 | { |
8543 | char buf[100]; | |
8544 | char *name; | |
8545 | asection *sect; | |
8546 | ||
d69f560c | 8547 | /* Make a "(base)/%d" section. */ |
d3fd4074 | 8548 | sprintf (buf, "%s/%ld", base, tid); |
07c6e936 | 8549 | |
a50b1753 | 8550 | name = (char *) bfd_alloc (abfd, strlen (buf) + 1); |
07c6e936 NC |
8551 | if (name == NULL) |
8552 | return FALSE; | |
8553 | strcpy (name, buf); | |
8554 | ||
117ed4f8 | 8555 | sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); |
07c6e936 NC |
8556 | if (sect == NULL) |
8557 | return FALSE; | |
8558 | ||
eea6121a | 8559 | sect->size = note->descsz; |
07c6e936 | 8560 | sect->filepos = note->descpos; |
07c6e936 NC |
8561 | sect->alignment_power = 2; |
8562 | ||
f8843e87 AM |
8563 | /* This is the current thread. */ |
8564 | if (elf_tdata (abfd)->core_lwpid == tid) | |
d69f560c | 8565 | return elfcore_maybe_make_sect (abfd, base, sect); |
f8843e87 AM |
8566 | |
8567 | return TRUE; | |
07c6e936 NC |
8568 | } |
8569 | ||
8570 | #define BFD_QNT_CORE_INFO 7 | |
8571 | #define BFD_QNT_CORE_STATUS 8 | |
8572 | #define BFD_QNT_CORE_GREG 9 | |
8573 | #define BFD_QNT_CORE_FPREG 10 | |
8574 | ||
8575 | static bfd_boolean | |
217aa764 | 8576 | elfcore_grok_nto_note (bfd *abfd, Elf_Internal_Note *note) |
07c6e936 NC |
8577 | { |
8578 | /* Every GREG section has a STATUS section before it. Store the | |
811072d8 | 8579 | tid from the previous call to pass down to the next gregs |
07c6e936 | 8580 | function. */ |
d3fd4074 | 8581 | static long tid = 1; |
07c6e936 NC |
8582 | |
8583 | switch (note->type) | |
8584 | { | |
d69f560c KW |
8585 | case BFD_QNT_CORE_INFO: |
8586 | return elfcore_make_note_pseudosection (abfd, ".qnx_core_info", note); | |
8587 | case BFD_QNT_CORE_STATUS: | |
8588 | return elfcore_grok_nto_status (abfd, note, &tid); | |
8589 | case BFD_QNT_CORE_GREG: | |
8590 | return elfcore_grok_nto_regs (abfd, note, tid, ".reg"); | |
8591 | case BFD_QNT_CORE_FPREG: | |
8592 | return elfcore_grok_nto_regs (abfd, note, tid, ".reg2"); | |
8593 | default: | |
8594 | return TRUE; | |
07c6e936 NC |
8595 | } |
8596 | } | |
8597 | ||
b15fa79e AM |
8598 | static bfd_boolean |
8599 | elfcore_grok_spu_note (bfd *abfd, Elf_Internal_Note *note) | |
8600 | { | |
8601 | char *name; | |
8602 | asection *sect; | |
8603 | size_t len; | |
8604 | ||
8605 | /* Use note name as section name. */ | |
8606 | len = note->namesz; | |
a50b1753 | 8607 | name = (char *) bfd_alloc (abfd, len); |
b15fa79e AM |
8608 | if (name == NULL) |
8609 | return FALSE; | |
8610 | memcpy (name, note->namedata, len); | |
8611 | name[len - 1] = '\0'; | |
8612 | ||
8613 | sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); | |
8614 | if (sect == NULL) | |
8615 | return FALSE; | |
8616 | ||
8617 | sect->size = note->descsz; | |
8618 | sect->filepos = note->descpos; | |
8619 | sect->alignment_power = 1; | |
8620 | ||
8621 | return TRUE; | |
8622 | } | |
8623 | ||
7c76fa91 MS |
8624 | /* Function: elfcore_write_note |
8625 | ||
47d9a591 | 8626 | Inputs: |
a39f3346 | 8627 | buffer to hold note, and current size of buffer |
7c76fa91 MS |
8628 | name of note |
8629 | type of note | |
8630 | data for note | |
8631 | size of data for note | |
8632 | ||
a39f3346 AM |
8633 | Writes note to end of buffer. ELF64 notes are written exactly as |
8634 | for ELF32, despite the current (as of 2006) ELF gabi specifying | |
8635 | that they ought to have 8-byte namesz and descsz field, and have | |
8636 | 8-byte alignment. Other writers, eg. Linux kernel, do the same. | |
8637 | ||
7c76fa91 | 8638 | Return: |
a39f3346 | 8639 | Pointer to realloc'd buffer, *BUFSIZ updated. */ |
7c76fa91 MS |
8640 | |
8641 | char * | |
a39f3346 | 8642 | elfcore_write_note (bfd *abfd, |
217aa764 | 8643 | char *buf, |
a39f3346 | 8644 | int *bufsiz, |
217aa764 | 8645 | const char *name, |
a39f3346 | 8646 | int type, |
217aa764 | 8647 | const void *input, |
a39f3346 | 8648 | int size) |
7c76fa91 MS |
8649 | { |
8650 | Elf_External_Note *xnp; | |
d4c88bbb | 8651 | size_t namesz; |
d4c88bbb | 8652 | size_t newspace; |
a39f3346 | 8653 | char *dest; |
7c76fa91 | 8654 | |
d4c88bbb | 8655 | namesz = 0; |
d4c88bbb | 8656 | if (name != NULL) |
a39f3346 | 8657 | namesz = strlen (name) + 1; |
d4c88bbb | 8658 | |
a39f3346 | 8659 | newspace = 12 + ((namesz + 3) & -4) + ((size + 3) & -4); |
d4c88bbb | 8660 | |
a50b1753 | 8661 | buf = (char *) realloc (buf, *bufsiz + newspace); |
14b1c01e AM |
8662 | if (buf == NULL) |
8663 | return buf; | |
a39f3346 | 8664 | dest = buf + *bufsiz; |
7c76fa91 MS |
8665 | *bufsiz += newspace; |
8666 | xnp = (Elf_External_Note *) dest; | |
8667 | H_PUT_32 (abfd, namesz, xnp->namesz); | |
8668 | H_PUT_32 (abfd, size, xnp->descsz); | |
8669 | H_PUT_32 (abfd, type, xnp->type); | |
d4c88bbb AM |
8670 | dest = xnp->name; |
8671 | if (name != NULL) | |
8672 | { | |
8673 | memcpy (dest, name, namesz); | |
8674 | dest += namesz; | |
a39f3346 | 8675 | while (namesz & 3) |
d4c88bbb AM |
8676 | { |
8677 | *dest++ = '\0'; | |
a39f3346 | 8678 | ++namesz; |
d4c88bbb AM |
8679 | } |
8680 | } | |
8681 | memcpy (dest, input, size); | |
a39f3346 AM |
8682 | dest += size; |
8683 | while (size & 3) | |
8684 | { | |
8685 | *dest++ = '\0'; | |
8686 | ++size; | |
8687 | } | |
8688 | return buf; | |
7c76fa91 MS |
8689 | } |
8690 | ||
8691 | #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) | |
8692 | char * | |
217aa764 AM |
8693 | elfcore_write_prpsinfo (bfd *abfd, |
8694 | char *buf, | |
8695 | int *bufsiz, | |
8696 | const char *fname, | |
8697 | const char *psargs) | |
7c76fa91 | 8698 | { |
183e98be AM |
8699 | const char *note_name = "CORE"; |
8700 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
8701 | ||
8702 | if (bed->elf_backend_write_core_note != NULL) | |
8703 | { | |
8704 | char *ret; | |
8705 | ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz, | |
8706 | NT_PRPSINFO, fname, psargs); | |
8707 | if (ret != NULL) | |
8708 | return ret; | |
8709 | } | |
7c76fa91 | 8710 | |
183e98be AM |
8711 | #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T) |
8712 | if (bed->s->elfclass == ELFCLASS32) | |
8713 | { | |
8714 | #if defined (HAVE_PSINFO32_T) | |
8715 | psinfo32_t data; | |
8716 | int note_type = NT_PSINFO; | |
8717 | #else | |
8718 | prpsinfo32_t data; | |
8719 | int note_type = NT_PRPSINFO; | |
8720 | #endif | |
8721 | ||
8722 | memset (&data, 0, sizeof (data)); | |
8723 | strncpy (data.pr_fname, fname, sizeof (data.pr_fname)); | |
8724 | strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs)); | |
8725 | return elfcore_write_note (abfd, buf, bufsiz, | |
8726 | note_name, note_type, &data, sizeof (data)); | |
8727 | } | |
8728 | else | |
8729 | #endif | |
8730 | { | |
7c76fa91 | 8731 | #if defined (HAVE_PSINFO_T) |
183e98be AM |
8732 | psinfo_t data; |
8733 | int note_type = NT_PSINFO; | |
7c76fa91 | 8734 | #else |
183e98be AM |
8735 | prpsinfo_t data; |
8736 | int note_type = NT_PRPSINFO; | |
7c76fa91 MS |
8737 | #endif |
8738 | ||
183e98be AM |
8739 | memset (&data, 0, sizeof (data)); |
8740 | strncpy (data.pr_fname, fname, sizeof (data.pr_fname)); | |
8741 | strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs)); | |
8742 | return elfcore_write_note (abfd, buf, bufsiz, | |
8743 | note_name, note_type, &data, sizeof (data)); | |
8744 | } | |
7c76fa91 MS |
8745 | } |
8746 | #endif /* PSINFO_T or PRPSINFO_T */ | |
8747 | ||
8748 | #if defined (HAVE_PRSTATUS_T) | |
8749 | char * | |
217aa764 AM |
8750 | elfcore_write_prstatus (bfd *abfd, |
8751 | char *buf, | |
8752 | int *bufsiz, | |
8753 | long pid, | |
8754 | int cursig, | |
8755 | const void *gregs) | |
7c76fa91 | 8756 | { |
183e98be AM |
8757 | const char *note_name = "CORE"; |
8758 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
7c76fa91 | 8759 | |
183e98be AM |
8760 | if (bed->elf_backend_write_core_note != NULL) |
8761 | { | |
8762 | char *ret; | |
8763 | ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz, | |
8764 | NT_PRSTATUS, | |
8765 | pid, cursig, gregs); | |
8766 | if (ret != NULL) | |
8767 | return ret; | |
8768 | } | |
8769 | ||
8770 | #if defined (HAVE_PRSTATUS32_T) | |
8771 | if (bed->s->elfclass == ELFCLASS32) | |
8772 | { | |
8773 | prstatus32_t prstat; | |
8774 | ||
8775 | memset (&prstat, 0, sizeof (prstat)); | |
8776 | prstat.pr_pid = pid; | |
8777 | prstat.pr_cursig = cursig; | |
8778 | memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg)); | |
8779 | return elfcore_write_note (abfd, buf, bufsiz, note_name, | |
8780 | NT_PRSTATUS, &prstat, sizeof (prstat)); | |
8781 | } | |
8782 | else | |
8783 | #endif | |
8784 | { | |
8785 | prstatus_t prstat; | |
8786 | ||
8787 | memset (&prstat, 0, sizeof (prstat)); | |
8788 | prstat.pr_pid = pid; | |
8789 | prstat.pr_cursig = cursig; | |
8790 | memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg)); | |
8791 | return elfcore_write_note (abfd, buf, bufsiz, note_name, | |
8792 | NT_PRSTATUS, &prstat, sizeof (prstat)); | |
8793 | } | |
7c76fa91 MS |
8794 | } |
8795 | #endif /* HAVE_PRSTATUS_T */ | |
8796 | ||
51316059 MS |
8797 | #if defined (HAVE_LWPSTATUS_T) |
8798 | char * | |
217aa764 AM |
8799 | elfcore_write_lwpstatus (bfd *abfd, |
8800 | char *buf, | |
8801 | int *bufsiz, | |
8802 | long pid, | |
8803 | int cursig, | |
8804 | const void *gregs) | |
51316059 MS |
8805 | { |
8806 | lwpstatus_t lwpstat; | |
183e98be | 8807 | const char *note_name = "CORE"; |
51316059 MS |
8808 | |
8809 | memset (&lwpstat, 0, sizeof (lwpstat)); | |
8810 | lwpstat.pr_lwpid = pid >> 16; | |
8811 | lwpstat.pr_cursig = cursig; | |
8812 | #if defined (HAVE_LWPSTATUS_T_PR_REG) | |
8813 | memcpy (lwpstat.pr_reg, gregs, sizeof (lwpstat.pr_reg)); | |
8814 | #elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT) | |
8815 | #if !defined(gregs) | |
8816 | memcpy (lwpstat.pr_context.uc_mcontext.gregs, | |
8817 | gregs, sizeof (lwpstat.pr_context.uc_mcontext.gregs)); | |
8818 | #else | |
8819 | memcpy (lwpstat.pr_context.uc_mcontext.__gregs, | |
8820 | gregs, sizeof (lwpstat.pr_context.uc_mcontext.__gregs)); | |
8821 | #endif | |
8822 | #endif | |
47d9a591 | 8823 | return elfcore_write_note (abfd, buf, bufsiz, note_name, |
51316059 MS |
8824 | NT_LWPSTATUS, &lwpstat, sizeof (lwpstat)); |
8825 | } | |
8826 | #endif /* HAVE_LWPSTATUS_T */ | |
8827 | ||
7c76fa91 MS |
8828 | #if defined (HAVE_PSTATUS_T) |
8829 | char * | |
217aa764 AM |
8830 | elfcore_write_pstatus (bfd *abfd, |
8831 | char *buf, | |
8832 | int *bufsiz, | |
8833 | long pid, | |
6c10990d NC |
8834 | int cursig ATTRIBUTE_UNUSED, |
8835 | const void *gregs ATTRIBUTE_UNUSED) | |
7c76fa91 | 8836 | { |
183e98be AM |
8837 | const char *note_name = "CORE"; |
8838 | #if defined (HAVE_PSTATUS32_T) | |
8839 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
7c76fa91 | 8840 | |
183e98be AM |
8841 | if (bed->s->elfclass == ELFCLASS32) |
8842 | { | |
8843 | pstatus32_t pstat; | |
8844 | ||
8845 | memset (&pstat, 0, sizeof (pstat)); | |
8846 | pstat.pr_pid = pid & 0xffff; | |
8847 | buf = elfcore_write_note (abfd, buf, bufsiz, note_name, | |
8848 | NT_PSTATUS, &pstat, sizeof (pstat)); | |
8849 | return buf; | |
8850 | } | |
8851 | else | |
8852 | #endif | |
8853 | { | |
8854 | pstatus_t pstat; | |
8855 | ||
8856 | memset (&pstat, 0, sizeof (pstat)); | |
8857 | pstat.pr_pid = pid & 0xffff; | |
8858 | buf = elfcore_write_note (abfd, buf, bufsiz, note_name, | |
8859 | NT_PSTATUS, &pstat, sizeof (pstat)); | |
8860 | return buf; | |
8861 | } | |
7c76fa91 MS |
8862 | } |
8863 | #endif /* HAVE_PSTATUS_T */ | |
8864 | ||
8865 | char * | |
217aa764 AM |
8866 | elfcore_write_prfpreg (bfd *abfd, |
8867 | char *buf, | |
8868 | int *bufsiz, | |
8869 | const void *fpregs, | |
8870 | int size) | |
7c76fa91 | 8871 | { |
183e98be | 8872 | const char *note_name = "CORE"; |
47d9a591 | 8873 | return elfcore_write_note (abfd, buf, bufsiz, |
7c76fa91 MS |
8874 | note_name, NT_FPREGSET, fpregs, size); |
8875 | } | |
8876 | ||
8877 | char * | |
217aa764 AM |
8878 | elfcore_write_prxfpreg (bfd *abfd, |
8879 | char *buf, | |
8880 | int *bufsiz, | |
8881 | const void *xfpregs, | |
8882 | int size) | |
7c76fa91 MS |
8883 | { |
8884 | char *note_name = "LINUX"; | |
47d9a591 | 8885 | return elfcore_write_note (abfd, buf, bufsiz, |
7c76fa91 MS |
8886 | note_name, NT_PRXFPREG, xfpregs, size); |
8887 | } | |
8888 | ||
4339cae0 L |
8889 | char * |
8890 | elfcore_write_xstatereg (bfd *abfd, char *buf, int *bufsiz, | |
8891 | const void *xfpregs, int size) | |
8892 | { | |
8893 | char *note_name = "LINUX"; | |
8894 | return elfcore_write_note (abfd, buf, bufsiz, | |
8895 | note_name, NT_X86_XSTATE, xfpregs, size); | |
8896 | } | |
8897 | ||
97753bd5 AM |
8898 | char * |
8899 | elfcore_write_ppc_vmx (bfd *abfd, | |
8900 | char *buf, | |
8901 | int *bufsiz, | |
8902 | const void *ppc_vmx, | |
8903 | int size) | |
8904 | { | |
8905 | char *note_name = "LINUX"; | |
8906 | return elfcore_write_note (abfd, buf, bufsiz, | |
8907 | note_name, NT_PPC_VMX, ppc_vmx, size); | |
8908 | } | |
8909 | ||
89eeb0bc LM |
8910 | char * |
8911 | elfcore_write_ppc_vsx (bfd *abfd, | |
8912 | char *buf, | |
8913 | int *bufsiz, | |
8914 | const void *ppc_vsx, | |
8915 | int size) | |
8916 | { | |
8917 | char *note_name = "LINUX"; | |
8918 | return elfcore_write_note (abfd, buf, bufsiz, | |
8919 | note_name, NT_PPC_VSX, ppc_vsx, size); | |
8920 | } | |
8921 | ||
0675e188 UW |
8922 | static char * |
8923 | elfcore_write_s390_high_gprs (bfd *abfd, | |
8924 | char *buf, | |
8925 | int *bufsiz, | |
8926 | const void *s390_high_gprs, | |
8927 | int size) | |
8928 | { | |
8929 | char *note_name = "LINUX"; | |
8930 | return elfcore_write_note (abfd, buf, bufsiz, | |
8931 | note_name, NT_S390_HIGH_GPRS, | |
8932 | s390_high_gprs, size); | |
8933 | } | |
8934 | ||
d7eeb400 MS |
8935 | char * |
8936 | elfcore_write_s390_timer (bfd *abfd, | |
8937 | char *buf, | |
8938 | int *bufsiz, | |
8939 | const void *s390_timer, | |
8940 | int size) | |
8941 | { | |
8942 | char *note_name = "LINUX"; | |
8943 | return elfcore_write_note (abfd, buf, bufsiz, | |
8944 | note_name, NT_S390_TIMER, s390_timer, size); | |
8945 | } | |
8946 | ||
8947 | char * | |
8948 | elfcore_write_s390_todcmp (bfd *abfd, | |
8949 | char *buf, | |
8950 | int *bufsiz, | |
8951 | const void *s390_todcmp, | |
8952 | int size) | |
8953 | { | |
8954 | char *note_name = "LINUX"; | |
8955 | return elfcore_write_note (abfd, buf, bufsiz, | |
8956 | note_name, NT_S390_TODCMP, s390_todcmp, size); | |
8957 | } | |
8958 | ||
8959 | char * | |
8960 | elfcore_write_s390_todpreg (bfd *abfd, | |
8961 | char *buf, | |
8962 | int *bufsiz, | |
8963 | const void *s390_todpreg, | |
8964 | int size) | |
8965 | { | |
8966 | char *note_name = "LINUX"; | |
8967 | return elfcore_write_note (abfd, buf, bufsiz, | |
8968 | note_name, NT_S390_TODPREG, s390_todpreg, size); | |
8969 | } | |
8970 | ||
8971 | char * | |
8972 | elfcore_write_s390_ctrs (bfd *abfd, | |
8973 | char *buf, | |
8974 | int *bufsiz, | |
8975 | const void *s390_ctrs, | |
8976 | int size) | |
8977 | { | |
8978 | char *note_name = "LINUX"; | |
8979 | return elfcore_write_note (abfd, buf, bufsiz, | |
8980 | note_name, NT_S390_CTRS, s390_ctrs, size); | |
8981 | } | |
8982 | ||
8983 | char * | |
8984 | elfcore_write_s390_prefix (bfd *abfd, | |
8985 | char *buf, | |
8986 | int *bufsiz, | |
8987 | const void *s390_prefix, | |
8988 | int size) | |
8989 | { | |
8990 | char *note_name = "LINUX"; | |
8991 | return elfcore_write_note (abfd, buf, bufsiz, | |
8992 | note_name, NT_S390_PREFIX, s390_prefix, size); | |
8993 | } | |
8994 | ||
bb864ac1 CES |
8995 | char * |
8996 | elfcore_write_register_note (bfd *abfd, | |
8997 | char *buf, | |
8998 | int *bufsiz, | |
8999 | const char *section, | |
9000 | const void *data, | |
9001 | int size) | |
9002 | { | |
9003 | if (strcmp (section, ".reg2") == 0) | |
9004 | return elfcore_write_prfpreg (abfd, buf, bufsiz, data, size); | |
9005 | if (strcmp (section, ".reg-xfp") == 0) | |
9006 | return elfcore_write_prxfpreg (abfd, buf, bufsiz, data, size); | |
4339cae0 L |
9007 | if (strcmp (section, ".reg-xstate") == 0) |
9008 | return elfcore_write_xstatereg (abfd, buf, bufsiz, data, size); | |
bb864ac1 CES |
9009 | if (strcmp (section, ".reg-ppc-vmx") == 0) |
9010 | return elfcore_write_ppc_vmx (abfd, buf, bufsiz, data, size); | |
89eeb0bc LM |
9011 | if (strcmp (section, ".reg-ppc-vsx") == 0) |
9012 | return elfcore_write_ppc_vsx (abfd, buf, bufsiz, data, size); | |
0675e188 UW |
9013 | if (strcmp (section, ".reg-s390-high-gprs") == 0) |
9014 | return elfcore_write_s390_high_gprs (abfd, buf, bufsiz, data, size); | |
d7eeb400 MS |
9015 | if (strcmp (section, ".reg-s390-timer") == 0) |
9016 | return elfcore_write_s390_timer (abfd, buf, bufsiz, data, size); | |
9017 | if (strcmp (section, ".reg-s390-todcmp") == 0) | |
9018 | return elfcore_write_s390_todcmp (abfd, buf, bufsiz, data, size); | |
9019 | if (strcmp (section, ".reg-s390-todpreg") == 0) | |
9020 | return elfcore_write_s390_todpreg (abfd, buf, bufsiz, data, size); | |
9021 | if (strcmp (section, ".reg-s390-ctrs") == 0) | |
9022 | return elfcore_write_s390_ctrs (abfd, buf, bufsiz, data, size); | |
9023 | if (strcmp (section, ".reg-s390-prefix") == 0) | |
9024 | return elfcore_write_s390_prefix (abfd, buf, bufsiz, data, size); | |
bb864ac1 CES |
9025 | return NULL; |
9026 | } | |
9027 | ||
b34976b6 | 9028 | static bfd_boolean |
718175fa | 9029 | elf_parse_notes (bfd *abfd, char *buf, size_t size, file_ptr offset) |
252b5132 | 9030 | { |
c044fabd | 9031 | char *p; |
252b5132 | 9032 | |
252b5132 RH |
9033 | p = buf; |
9034 | while (p < buf + size) | |
9035 | { | |
c044fabd KH |
9036 | /* FIXME: bad alignment assumption. */ |
9037 | Elf_External_Note *xnp = (Elf_External_Note *) p; | |
252b5132 RH |
9038 | Elf_Internal_Note in; |
9039 | ||
baea7ef1 AM |
9040 | if (offsetof (Elf_External_Note, name) > buf - p + size) |
9041 | return FALSE; | |
9042 | ||
dc810e39 | 9043 | in.type = H_GET_32 (abfd, xnp->type); |
252b5132 | 9044 | |
dc810e39 | 9045 | in.namesz = H_GET_32 (abfd, xnp->namesz); |
252b5132 | 9046 | in.namedata = xnp->name; |
baea7ef1 AM |
9047 | if (in.namesz > buf - in.namedata + size) |
9048 | return FALSE; | |
252b5132 | 9049 | |
dc810e39 | 9050 | in.descsz = H_GET_32 (abfd, xnp->descsz); |
252b5132 RH |
9051 | in.descdata = in.namedata + BFD_ALIGN (in.namesz, 4); |
9052 | in.descpos = offset + (in.descdata - buf); | |
baea7ef1 AM |
9053 | if (in.descsz != 0 |
9054 | && (in.descdata >= buf + size | |
9055 | || in.descsz > buf - in.descdata + size)) | |
9056 | return FALSE; | |
252b5132 | 9057 | |
718175fa JK |
9058 | switch (bfd_get_format (abfd)) |
9059 | { | |
9060 | default: | |
9061 | return TRUE; | |
9062 | ||
9063 | case bfd_core: | |
9064 | if (CONST_STRNEQ (in.namedata, "NetBSD-CORE")) | |
9065 | { | |
9066 | if (! elfcore_grok_netbsd_note (abfd, &in)) | |
9067 | return FALSE; | |
9068 | } | |
67cc5033 MK |
9069 | else if (CONST_STRNEQ (in.namedata, "OpenBSD")) |
9070 | { | |
9071 | if (! elfcore_grok_openbsd_note (abfd, &in)) | |
9072 | return FALSE; | |
9073 | } | |
718175fa JK |
9074 | else if (CONST_STRNEQ (in.namedata, "QNX")) |
9075 | { | |
9076 | if (! elfcore_grok_nto_note (abfd, &in)) | |
9077 | return FALSE; | |
9078 | } | |
b15fa79e AM |
9079 | else if (CONST_STRNEQ (in.namedata, "SPU/")) |
9080 | { | |
9081 | if (! elfcore_grok_spu_note (abfd, &in)) | |
9082 | return FALSE; | |
9083 | } | |
718175fa JK |
9084 | else |
9085 | { | |
9086 | if (! elfcore_grok_note (abfd, &in)) | |
9087 | return FALSE; | |
9088 | } | |
9089 | break; | |
9090 | ||
9091 | case bfd_object: | |
9092 | if (in.namesz == sizeof "GNU" && strcmp (in.namedata, "GNU") == 0) | |
9093 | { | |
9094 | if (! elfobj_grok_gnu_note (abfd, &in)) | |
9095 | return FALSE; | |
9096 | } | |
9097 | break; | |
08a40648 | 9098 | } |
252b5132 RH |
9099 | |
9100 | p = in.descdata + BFD_ALIGN (in.descsz, 4); | |
9101 | } | |
9102 | ||
718175fa JK |
9103 | return TRUE; |
9104 | } | |
9105 | ||
9106 | static bfd_boolean | |
9107 | elf_read_notes (bfd *abfd, file_ptr offset, bfd_size_type size) | |
9108 | { | |
9109 | char *buf; | |
9110 | ||
9111 | if (size <= 0) | |
9112 | return TRUE; | |
9113 | ||
9114 | if (bfd_seek (abfd, offset, SEEK_SET) != 0) | |
9115 | return FALSE; | |
9116 | ||
a50b1753 | 9117 | buf = (char *) bfd_malloc (size); |
718175fa JK |
9118 | if (buf == NULL) |
9119 | return FALSE; | |
9120 | ||
9121 | if (bfd_bread (buf, size, abfd) != size | |
9122 | || !elf_parse_notes (abfd, buf, size, offset)) | |
9123 | { | |
9124 | free (buf); | |
9125 | return FALSE; | |
9126 | } | |
9127 | ||
252b5132 | 9128 | free (buf); |
b34976b6 | 9129 | return TRUE; |
252b5132 | 9130 | } |
98d8431c JB |
9131 | \f |
9132 | /* Providing external access to the ELF program header table. */ | |
9133 | ||
9134 | /* Return an upper bound on the number of bytes required to store a | |
9135 | copy of ABFD's program header table entries. Return -1 if an error | |
9136 | occurs; bfd_get_error will return an appropriate code. */ | |
c044fabd | 9137 | |
98d8431c | 9138 | long |
217aa764 | 9139 | bfd_get_elf_phdr_upper_bound (bfd *abfd) |
98d8431c JB |
9140 | { |
9141 | if (abfd->xvec->flavour != bfd_target_elf_flavour) | |
9142 | { | |
9143 | bfd_set_error (bfd_error_wrong_format); | |
9144 | return -1; | |
9145 | } | |
9146 | ||
936e320b | 9147 | return elf_elfheader (abfd)->e_phnum * sizeof (Elf_Internal_Phdr); |
98d8431c JB |
9148 | } |
9149 | ||
98d8431c JB |
9150 | /* Copy ABFD's program header table entries to *PHDRS. The entries |
9151 | will be stored as an array of Elf_Internal_Phdr structures, as | |
9152 | defined in include/elf/internal.h. To find out how large the | |
9153 | buffer needs to be, call bfd_get_elf_phdr_upper_bound. | |
9154 | ||
9155 | Return the number of program header table entries read, or -1 if an | |
9156 | error occurs; bfd_get_error will return an appropriate code. */ | |
c044fabd | 9157 | |
98d8431c | 9158 | int |
217aa764 | 9159 | bfd_get_elf_phdrs (bfd *abfd, void *phdrs) |
98d8431c JB |
9160 | { |
9161 | int num_phdrs; | |
9162 | ||
9163 | if (abfd->xvec->flavour != bfd_target_elf_flavour) | |
9164 | { | |
9165 | bfd_set_error (bfd_error_wrong_format); | |
9166 | return -1; | |
9167 | } | |
9168 | ||
9169 | num_phdrs = elf_elfheader (abfd)->e_phnum; | |
c044fabd | 9170 | memcpy (phdrs, elf_tdata (abfd)->phdr, |
98d8431c JB |
9171 | num_phdrs * sizeof (Elf_Internal_Phdr)); |
9172 | ||
9173 | return num_phdrs; | |
9174 | } | |
ae4221d7 | 9175 | |
db6751f2 | 9176 | enum elf_reloc_type_class |
217aa764 | 9177 | _bfd_elf_reloc_type_class (const Elf_Internal_Rela *rela ATTRIBUTE_UNUSED) |
db6751f2 JJ |
9178 | { |
9179 | return reloc_class_normal; | |
9180 | } | |
f8df10f4 | 9181 | |
47d9a591 | 9182 | /* For RELA architectures, return the relocation value for a |
f8df10f4 JJ |
9183 | relocation against a local symbol. */ |
9184 | ||
9185 | bfd_vma | |
217aa764 AM |
9186 | _bfd_elf_rela_local_sym (bfd *abfd, |
9187 | Elf_Internal_Sym *sym, | |
8517fae7 | 9188 | asection **psec, |
217aa764 | 9189 | Elf_Internal_Rela *rel) |
f8df10f4 | 9190 | { |
8517fae7 | 9191 | asection *sec = *psec; |
f8df10f4 JJ |
9192 | bfd_vma relocation; |
9193 | ||
9194 | relocation = (sec->output_section->vma | |
9195 | + sec->output_offset | |
9196 | + sym->st_value); | |
9197 | if ((sec->flags & SEC_MERGE) | |
c629eae0 | 9198 | && ELF_ST_TYPE (sym->st_info) == STT_SECTION |
68bfbfcc | 9199 | && sec->sec_info_type == ELF_INFO_TYPE_MERGE) |
f8df10f4 | 9200 | { |
f8df10f4 | 9201 | rel->r_addend = |
8517fae7 | 9202 | _bfd_merged_section_offset (abfd, psec, |
65765700 | 9203 | elf_section_data (sec)->sec_info, |
753731ee AM |
9204 | sym->st_value + rel->r_addend); |
9205 | if (sec != *psec) | |
9206 | { | |
9207 | /* If we have changed the section, and our original section is | |
9208 | marked with SEC_EXCLUDE, it means that the original | |
9209 | SEC_MERGE section has been completely subsumed in some | |
9210 | other SEC_MERGE section. In this case, we need to leave | |
9211 | some info around for --emit-relocs. */ | |
9212 | if ((sec->flags & SEC_EXCLUDE) != 0) | |
9213 | sec->kept_section = *psec; | |
9214 | sec = *psec; | |
9215 | } | |
8517fae7 AM |
9216 | rel->r_addend -= relocation; |
9217 | rel->r_addend += sec->output_section->vma + sec->output_offset; | |
f8df10f4 JJ |
9218 | } |
9219 | return relocation; | |
9220 | } | |
c629eae0 JJ |
9221 | |
9222 | bfd_vma | |
217aa764 AM |
9223 | _bfd_elf_rel_local_sym (bfd *abfd, |
9224 | Elf_Internal_Sym *sym, | |
9225 | asection **psec, | |
9226 | bfd_vma addend) | |
47d9a591 | 9227 | { |
c629eae0 JJ |
9228 | asection *sec = *psec; |
9229 | ||
68bfbfcc | 9230 | if (sec->sec_info_type != ELF_INFO_TYPE_MERGE) |
c629eae0 JJ |
9231 | return sym->st_value + addend; |
9232 | ||
9233 | return _bfd_merged_section_offset (abfd, psec, | |
65765700 | 9234 | elf_section_data (sec)->sec_info, |
753731ee | 9235 | sym->st_value + addend); |
c629eae0 JJ |
9236 | } |
9237 | ||
9238 | bfd_vma | |
217aa764 | 9239 | _bfd_elf_section_offset (bfd *abfd, |
92e4ec35 | 9240 | struct bfd_link_info *info, |
217aa764 AM |
9241 | asection *sec, |
9242 | bfd_vma offset) | |
c629eae0 | 9243 | { |
68bfbfcc | 9244 | switch (sec->sec_info_type) |
65765700 JJ |
9245 | { |
9246 | case ELF_INFO_TYPE_STABS: | |
eea6121a AM |
9247 | return _bfd_stab_section_offset (sec, elf_section_data (sec)->sec_info, |
9248 | offset); | |
65765700 | 9249 | case ELF_INFO_TYPE_EH_FRAME: |
92e4ec35 | 9250 | return _bfd_elf_eh_frame_section_offset (abfd, info, sec, offset); |
65765700 JJ |
9251 | default: |
9252 | return offset; | |
9253 | } | |
c629eae0 | 9254 | } |
3333a7c3 RM |
9255 | \f |
9256 | /* Create a new BFD as if by bfd_openr. Rather than opening a file, | |
9257 | reconstruct an ELF file by reading the segments out of remote memory | |
9258 | based on the ELF file header at EHDR_VMA and the ELF program headers it | |
9259 | points to. If not null, *LOADBASEP is filled in with the difference | |
9260 | between the VMAs from which the segments were read, and the VMAs the | |
9261 | file headers (and hence BFD's idea of each section's VMA) put them at. | |
9262 | ||
9263 | The function TARGET_READ_MEMORY is called to copy LEN bytes from the | |
9264 | remote memory at target address VMA into the local buffer at MYADDR; it | |
9265 | should return zero on success or an `errno' code on failure. TEMPL must | |
9266 | be a BFD for an ELF target with the word size and byte order found in | |
9267 | the remote memory. */ | |
9268 | ||
9269 | bfd * | |
217aa764 AM |
9270 | bfd_elf_bfd_from_remote_memory |
9271 | (bfd *templ, | |
9272 | bfd_vma ehdr_vma, | |
9273 | bfd_vma *loadbasep, | |
f075ee0c | 9274 | int (*target_read_memory) (bfd_vma, bfd_byte *, int)) |
3333a7c3 RM |
9275 | { |
9276 | return (*get_elf_backend_data (templ)->elf_backend_bfd_from_remote_memory) | |
9277 | (templ, ehdr_vma, loadbasep, target_read_memory); | |
9278 | } | |
4c45e5c9 JJ |
9279 | \f |
9280 | long | |
c9727e01 AM |
9281 | _bfd_elf_get_synthetic_symtab (bfd *abfd, |
9282 | long symcount ATTRIBUTE_UNUSED, | |
9283 | asymbol **syms ATTRIBUTE_UNUSED, | |
8615f3f2 | 9284 | long dynsymcount, |
c9727e01 AM |
9285 | asymbol **dynsyms, |
9286 | asymbol **ret) | |
4c45e5c9 JJ |
9287 | { |
9288 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
9289 | asection *relplt; | |
9290 | asymbol *s; | |
9291 | const char *relplt_name; | |
9292 | bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean); | |
9293 | arelent *p; | |
9294 | long count, i, n; | |
9295 | size_t size; | |
9296 | Elf_Internal_Shdr *hdr; | |
9297 | char *names; | |
9298 | asection *plt; | |
9299 | ||
8615f3f2 AM |
9300 | *ret = NULL; |
9301 | ||
90e3cdf2 JJ |
9302 | if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0) |
9303 | return 0; | |
9304 | ||
8615f3f2 AM |
9305 | if (dynsymcount <= 0) |
9306 | return 0; | |
9307 | ||
4c45e5c9 JJ |
9308 | if (!bed->plt_sym_val) |
9309 | return 0; | |
9310 | ||
9311 | relplt_name = bed->relplt_name; | |
9312 | if (relplt_name == NULL) | |
d35fd659 | 9313 | relplt_name = bed->rela_plts_and_copies_p ? ".rela.plt" : ".rel.plt"; |
4c45e5c9 JJ |
9314 | relplt = bfd_get_section_by_name (abfd, relplt_name); |
9315 | if (relplt == NULL) | |
9316 | return 0; | |
9317 | ||
9318 | hdr = &elf_section_data (relplt)->this_hdr; | |
9319 | if (hdr->sh_link != elf_dynsymtab (abfd) | |
9320 | || (hdr->sh_type != SHT_REL && hdr->sh_type != SHT_RELA)) | |
9321 | return 0; | |
9322 | ||
9323 | plt = bfd_get_section_by_name (abfd, ".plt"); | |
9324 | if (plt == NULL) | |
9325 | return 0; | |
9326 | ||
9327 | slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table; | |
c9727e01 | 9328 | if (! (*slurp_relocs) (abfd, relplt, dynsyms, TRUE)) |
4c45e5c9 JJ |
9329 | return -1; |
9330 | ||
eea6121a | 9331 | count = relplt->size / hdr->sh_entsize; |
4c45e5c9 JJ |
9332 | size = count * sizeof (asymbol); |
9333 | p = relplt->relocation; | |
cb53bf42 | 9334 | for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel) |
041de40d AM |
9335 | { |
9336 | size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt"); | |
9337 | if (p->addend != 0) | |
9338 | { | |
9339 | #ifdef BFD64 | |
9340 | size += sizeof ("+0x") - 1 + 8 + 8 * (bed->s->elfclass == ELFCLASS64); | |
9341 | #else | |
9342 | size += sizeof ("+0x") - 1 + 8; | |
9343 | #endif | |
9344 | } | |
9345 | } | |
4c45e5c9 | 9346 | |
a50b1753 | 9347 | s = *ret = (asymbol *) bfd_malloc (size); |
4c45e5c9 JJ |
9348 | if (s == NULL) |
9349 | return -1; | |
9350 | ||
9351 | names = (char *) (s + count); | |
9352 | p = relplt->relocation; | |
9353 | n = 0; | |
cb53bf42 | 9354 | for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel) |
4c45e5c9 JJ |
9355 | { |
9356 | size_t len; | |
9357 | bfd_vma addr; | |
9358 | ||
9359 | addr = bed->plt_sym_val (i, plt, p); | |
9360 | if (addr == (bfd_vma) -1) | |
9361 | continue; | |
9362 | ||
9363 | *s = **p->sym_ptr_ptr; | |
65a7a66f AM |
9364 | /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since |
9365 | we are defining a symbol, ensure one of them is set. */ | |
9366 | if ((s->flags & BSF_LOCAL) == 0) | |
9367 | s->flags |= BSF_GLOBAL; | |
6ba2a415 | 9368 | s->flags |= BSF_SYNTHETIC; |
4c45e5c9 JJ |
9369 | s->section = plt; |
9370 | s->value = addr - plt->vma; | |
9371 | s->name = names; | |
8f39ba8e | 9372 | s->udata.p = NULL; |
4c45e5c9 JJ |
9373 | len = strlen ((*p->sym_ptr_ptr)->name); |
9374 | memcpy (names, (*p->sym_ptr_ptr)->name, len); | |
9375 | names += len; | |
041de40d AM |
9376 | if (p->addend != 0) |
9377 | { | |
1d770845 | 9378 | char buf[30], *a; |
91d6fa6a | 9379 | |
041de40d AM |
9380 | memcpy (names, "+0x", sizeof ("+0x") - 1); |
9381 | names += sizeof ("+0x") - 1; | |
1d770845 L |
9382 | bfd_sprintf_vma (abfd, buf, p->addend); |
9383 | for (a = buf; *a == '0'; ++a) | |
9384 | ; | |
9385 | len = strlen (a); | |
9386 | memcpy (names, a, len); | |
9387 | names += len; | |
041de40d | 9388 | } |
4c45e5c9 JJ |
9389 | memcpy (names, "@plt", sizeof ("@plt")); |
9390 | names += sizeof ("@plt"); | |
8f39ba8e | 9391 | ++s, ++n; |
4c45e5c9 JJ |
9392 | } |
9393 | ||
9394 | return n; | |
9395 | } | |
3d7f7666 | 9396 | |
3b22753a L |
9397 | /* It is only used by x86-64 so far. */ |
9398 | asection _bfd_elf_large_com_section | |
9399 | = BFD_FAKE_SECTION (_bfd_elf_large_com_section, | |
f592407e | 9400 | SEC_IS_COMMON, NULL, "LARGE_COMMON", 0); |
ecca9871 | 9401 | |
d1036acb L |
9402 | void |
9403 | _bfd_elf_set_osabi (bfd * abfd, | |
9404 | struct bfd_link_info * link_info ATTRIBUTE_UNUSED) | |
9405 | { | |
9406 | Elf_Internal_Ehdr * i_ehdrp; /* ELF file header, internal form. */ | |
9407 | ||
9408 | i_ehdrp = elf_elfheader (abfd); | |
9409 | ||
9410 | i_ehdrp->e_ident[EI_OSABI] = get_elf_backend_data (abfd)->elf_osabi; | |
d8045f23 NC |
9411 | |
9412 | /* To make things simpler for the loader on Linux systems we set the | |
9413 | osabi field to ELFOSABI_LINUX if the binary contains symbols of | |
9414 | the STT_GNU_IFUNC type. */ | |
9415 | if (i_ehdrp->e_ident[EI_OSABI] == ELFOSABI_NONE | |
9416 | && elf_tdata (abfd)->has_ifunc_symbols) | |
9417 | i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_LINUX; | |
d1036acb | 9418 | } |
fcb93ecf PB |
9419 | |
9420 | ||
9421 | /* Return TRUE for ELF symbol types that represent functions. | |
9422 | This is the default version of this function, which is sufficient for | |
d8045f23 | 9423 | most targets. It returns true if TYPE is STT_FUNC or STT_GNU_IFUNC. */ |
fcb93ecf PB |
9424 | |
9425 | bfd_boolean | |
9426 | _bfd_elf_is_function_type (unsigned int type) | |
9427 | { | |
d8045f23 NC |
9428 | return (type == STT_FUNC |
9429 | || type == STT_GNU_IFUNC); | |
fcb93ecf | 9430 | } |