Commit | Line | Data |
---|---|---|
252b5132 | 1 | /* ELF executable support for BFD. |
340b6d91 AC |
2 | |
3 | Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, | |
72a80a16 AM |
4 | 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009 |
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 | |
217aa764 | 48 | static int elf_sort_sections (const void *, const void *); |
c84fca4d | 49 | static bfd_boolean assign_file_positions_except_relocs (bfd *, struct bfd_link_info *); |
217aa764 AM |
50 | static bfd_boolean prep_headers (bfd *); |
51 | static bfd_boolean swap_out_syms (bfd *, struct bfd_strtab_hash **, int) ; | |
718175fa JK |
52 | static bfd_boolean elf_read_notes (bfd *, file_ptr, bfd_size_type) ; |
53 | static bfd_boolean elf_parse_notes (bfd *abfd, char *buf, size_t size, | |
54 | file_ptr offset); | |
50b2bdb7 | 55 | |
252b5132 RH |
56 | /* Swap version information in and out. The version information is |
57 | currently size independent. If that ever changes, this code will | |
58 | need to move into elfcode.h. */ | |
59 | ||
60 | /* Swap in a Verdef structure. */ | |
61 | ||
62 | void | |
217aa764 AM |
63 | _bfd_elf_swap_verdef_in (bfd *abfd, |
64 | const Elf_External_Verdef *src, | |
65 | Elf_Internal_Verdef *dst) | |
252b5132 | 66 | { |
dc810e39 AM |
67 | dst->vd_version = H_GET_16 (abfd, src->vd_version); |
68 | dst->vd_flags = H_GET_16 (abfd, src->vd_flags); | |
69 | dst->vd_ndx = H_GET_16 (abfd, src->vd_ndx); | |
70 | dst->vd_cnt = H_GET_16 (abfd, src->vd_cnt); | |
71 | dst->vd_hash = H_GET_32 (abfd, src->vd_hash); | |
72 | dst->vd_aux = H_GET_32 (abfd, src->vd_aux); | |
73 | dst->vd_next = H_GET_32 (abfd, src->vd_next); | |
252b5132 RH |
74 | } |
75 | ||
76 | /* Swap out a Verdef structure. */ | |
77 | ||
78 | void | |
217aa764 AM |
79 | _bfd_elf_swap_verdef_out (bfd *abfd, |
80 | const Elf_Internal_Verdef *src, | |
81 | Elf_External_Verdef *dst) | |
252b5132 | 82 | { |
dc810e39 AM |
83 | H_PUT_16 (abfd, src->vd_version, dst->vd_version); |
84 | H_PUT_16 (abfd, src->vd_flags, dst->vd_flags); | |
85 | H_PUT_16 (abfd, src->vd_ndx, dst->vd_ndx); | |
86 | H_PUT_16 (abfd, src->vd_cnt, dst->vd_cnt); | |
87 | H_PUT_32 (abfd, src->vd_hash, dst->vd_hash); | |
88 | H_PUT_32 (abfd, src->vd_aux, dst->vd_aux); | |
89 | H_PUT_32 (abfd, src->vd_next, dst->vd_next); | |
252b5132 RH |
90 | } |
91 | ||
92 | /* Swap in a Verdaux structure. */ | |
93 | ||
94 | void | |
217aa764 AM |
95 | _bfd_elf_swap_verdaux_in (bfd *abfd, |
96 | const Elf_External_Verdaux *src, | |
97 | Elf_Internal_Verdaux *dst) | |
252b5132 | 98 | { |
dc810e39 AM |
99 | dst->vda_name = H_GET_32 (abfd, src->vda_name); |
100 | dst->vda_next = H_GET_32 (abfd, src->vda_next); | |
252b5132 RH |
101 | } |
102 | ||
103 | /* Swap out a Verdaux structure. */ | |
104 | ||
105 | void | |
217aa764 AM |
106 | _bfd_elf_swap_verdaux_out (bfd *abfd, |
107 | const Elf_Internal_Verdaux *src, | |
108 | Elf_External_Verdaux *dst) | |
252b5132 | 109 | { |
dc810e39 AM |
110 | H_PUT_32 (abfd, src->vda_name, dst->vda_name); |
111 | H_PUT_32 (abfd, src->vda_next, dst->vda_next); | |
252b5132 RH |
112 | } |
113 | ||
114 | /* Swap in a Verneed structure. */ | |
115 | ||
116 | void | |
217aa764 AM |
117 | _bfd_elf_swap_verneed_in (bfd *abfd, |
118 | const Elf_External_Verneed *src, | |
119 | Elf_Internal_Verneed *dst) | |
252b5132 | 120 | { |
dc810e39 AM |
121 | dst->vn_version = H_GET_16 (abfd, src->vn_version); |
122 | dst->vn_cnt = H_GET_16 (abfd, src->vn_cnt); | |
123 | dst->vn_file = H_GET_32 (abfd, src->vn_file); | |
124 | dst->vn_aux = H_GET_32 (abfd, src->vn_aux); | |
125 | dst->vn_next = H_GET_32 (abfd, src->vn_next); | |
252b5132 RH |
126 | } |
127 | ||
128 | /* Swap out a Verneed structure. */ | |
129 | ||
130 | void | |
217aa764 AM |
131 | _bfd_elf_swap_verneed_out (bfd *abfd, |
132 | const Elf_Internal_Verneed *src, | |
133 | Elf_External_Verneed *dst) | |
252b5132 | 134 | { |
dc810e39 AM |
135 | H_PUT_16 (abfd, src->vn_version, dst->vn_version); |
136 | H_PUT_16 (abfd, src->vn_cnt, dst->vn_cnt); | |
137 | H_PUT_32 (abfd, src->vn_file, dst->vn_file); | |
138 | H_PUT_32 (abfd, src->vn_aux, dst->vn_aux); | |
139 | H_PUT_32 (abfd, src->vn_next, dst->vn_next); | |
252b5132 RH |
140 | } |
141 | ||
142 | /* Swap in a Vernaux structure. */ | |
143 | ||
144 | void | |
217aa764 AM |
145 | _bfd_elf_swap_vernaux_in (bfd *abfd, |
146 | const Elf_External_Vernaux *src, | |
147 | Elf_Internal_Vernaux *dst) | |
252b5132 | 148 | { |
dc810e39 AM |
149 | dst->vna_hash = H_GET_32 (abfd, src->vna_hash); |
150 | dst->vna_flags = H_GET_16 (abfd, src->vna_flags); | |
151 | dst->vna_other = H_GET_16 (abfd, src->vna_other); | |
152 | dst->vna_name = H_GET_32 (abfd, src->vna_name); | |
153 | dst->vna_next = H_GET_32 (abfd, src->vna_next); | |
252b5132 RH |
154 | } |
155 | ||
156 | /* Swap out a Vernaux structure. */ | |
157 | ||
158 | void | |
217aa764 AM |
159 | _bfd_elf_swap_vernaux_out (bfd *abfd, |
160 | const Elf_Internal_Vernaux *src, | |
161 | Elf_External_Vernaux *dst) | |
252b5132 | 162 | { |
dc810e39 AM |
163 | H_PUT_32 (abfd, src->vna_hash, dst->vna_hash); |
164 | H_PUT_16 (abfd, src->vna_flags, dst->vna_flags); | |
165 | H_PUT_16 (abfd, src->vna_other, dst->vna_other); | |
166 | H_PUT_32 (abfd, src->vna_name, dst->vna_name); | |
167 | H_PUT_32 (abfd, src->vna_next, dst->vna_next); | |
252b5132 RH |
168 | } |
169 | ||
170 | /* Swap in a Versym structure. */ | |
171 | ||
172 | void | |
217aa764 AM |
173 | _bfd_elf_swap_versym_in (bfd *abfd, |
174 | const Elf_External_Versym *src, | |
175 | Elf_Internal_Versym *dst) | |
252b5132 | 176 | { |
dc810e39 | 177 | dst->vs_vers = H_GET_16 (abfd, src->vs_vers); |
252b5132 RH |
178 | } |
179 | ||
180 | /* Swap out a Versym structure. */ | |
181 | ||
182 | void | |
217aa764 AM |
183 | _bfd_elf_swap_versym_out (bfd *abfd, |
184 | const Elf_Internal_Versym *src, | |
185 | Elf_External_Versym *dst) | |
252b5132 | 186 | { |
dc810e39 | 187 | H_PUT_16 (abfd, src->vs_vers, dst->vs_vers); |
252b5132 RH |
188 | } |
189 | ||
190 | /* Standard ELF hash function. Do not change this function; you will | |
191 | cause invalid hash tables to be generated. */ | |
3a99b017 | 192 | |
252b5132 | 193 | unsigned long |
217aa764 | 194 | bfd_elf_hash (const char *namearg) |
252b5132 | 195 | { |
3a99b017 | 196 | const unsigned char *name = (const unsigned char *) namearg; |
252b5132 RH |
197 | unsigned long h = 0; |
198 | unsigned long g; | |
199 | int ch; | |
200 | ||
201 | while ((ch = *name++) != '\0') | |
202 | { | |
203 | h = (h << 4) + ch; | |
204 | if ((g = (h & 0xf0000000)) != 0) | |
205 | { | |
206 | h ^= g >> 24; | |
207 | /* The ELF ABI says `h &= ~g', but this is equivalent in | |
208 | this case and on some machines one insn instead of two. */ | |
209 | h ^= g; | |
210 | } | |
211 | } | |
32dfa85d | 212 | return h & 0xffffffff; |
252b5132 RH |
213 | } |
214 | ||
fdc90cb4 JJ |
215 | /* DT_GNU_HASH hash function. Do not change this function; you will |
216 | cause invalid hash tables to be generated. */ | |
217 | ||
218 | unsigned long | |
219 | bfd_elf_gnu_hash (const char *namearg) | |
220 | { | |
221 | const unsigned char *name = (const unsigned char *) namearg; | |
222 | unsigned long h = 5381; | |
223 | unsigned char ch; | |
224 | ||
225 | while ((ch = *name++) != '\0') | |
226 | h = (h << 5) + h + ch; | |
227 | return h & 0xffffffff; | |
228 | } | |
229 | ||
0c8d6e5c AM |
230 | /* Create a tdata field OBJECT_SIZE bytes in length, zeroed out and with |
231 | the object_id field of an elf_obj_tdata field set to OBJECT_ID. */ | |
b34976b6 | 232 | bfd_boolean |
0c8d6e5c | 233 | bfd_elf_allocate_object (bfd *abfd, |
0ffa91dd NC |
234 | size_t object_size, |
235 | enum elf_object_id object_id) | |
252b5132 | 236 | { |
0ffa91dd NC |
237 | BFD_ASSERT (object_size >= sizeof (struct elf_obj_tdata)); |
238 | abfd->tdata.any = bfd_zalloc (abfd, object_size); | |
239 | if (abfd->tdata.any == NULL) | |
240 | return FALSE; | |
252b5132 | 241 | |
0ffa91dd NC |
242 | elf_object_id (abfd) = object_id; |
243 | elf_program_header_size (abfd) = (bfd_size_type) -1; | |
b34976b6 | 244 | return TRUE; |
252b5132 RH |
245 | } |
246 | ||
0ffa91dd NC |
247 | |
248 | bfd_boolean | |
249 | bfd_elf_make_generic_object (bfd *abfd) | |
250 | { | |
251 | return bfd_elf_allocate_object (abfd, sizeof (struct elf_obj_tdata), | |
252 | GENERIC_ELF_TDATA); | |
253 | } | |
254 | ||
b34976b6 | 255 | bfd_boolean |
217aa764 | 256 | bfd_elf_mkcorefile (bfd *abfd) |
252b5132 | 257 | { |
c044fabd | 258 | /* I think this can be done just like an object file. */ |
0ffa91dd | 259 | return bfd_elf_make_generic_object (abfd); |
252b5132 RH |
260 | } |
261 | ||
72a80a16 | 262 | static char * |
217aa764 | 263 | bfd_elf_get_str_section (bfd *abfd, unsigned int shindex) |
252b5132 RH |
264 | { |
265 | Elf_Internal_Shdr **i_shdrp; | |
f075ee0c | 266 | bfd_byte *shstrtab = NULL; |
dc810e39 AM |
267 | file_ptr offset; |
268 | bfd_size_type shstrtabsize; | |
252b5132 RH |
269 | |
270 | i_shdrp = elf_elfsections (abfd); | |
74f2e02b AM |
271 | if (i_shdrp == 0 |
272 | || shindex >= elf_numsections (abfd) | |
273 | || i_shdrp[shindex] == 0) | |
f075ee0c | 274 | return NULL; |
252b5132 | 275 | |
f075ee0c | 276 | shstrtab = i_shdrp[shindex]->contents; |
252b5132 RH |
277 | if (shstrtab == NULL) |
278 | { | |
c044fabd | 279 | /* No cached one, attempt to read, and cache what we read. */ |
252b5132 RH |
280 | offset = i_shdrp[shindex]->sh_offset; |
281 | shstrtabsize = i_shdrp[shindex]->sh_size; | |
c6c60d09 JJ |
282 | |
283 | /* Allocate and clear an extra byte at the end, to prevent crashes | |
284 | in case the string table is not terminated. */ | |
3471d59d | 285 | if (shstrtabsize + 1 <= 1 |
c6c60d09 JJ |
286 | || (shstrtab = bfd_alloc (abfd, shstrtabsize + 1)) == NULL |
287 | || bfd_seek (abfd, offset, SEEK_SET) != 0) | |
288 | shstrtab = NULL; | |
289 | else if (bfd_bread (shstrtab, shstrtabsize, abfd) != shstrtabsize) | |
290 | { | |
291 | if (bfd_get_error () != bfd_error_system_call) | |
292 | bfd_set_error (bfd_error_file_truncated); | |
293 | shstrtab = NULL; | |
3471d59d CC |
294 | /* Once we've failed to read it, make sure we don't keep |
295 | trying. Otherwise, we'll keep allocating space for | |
296 | the string table over and over. */ | |
297 | i_shdrp[shindex]->sh_size = 0; | |
c6c60d09 JJ |
298 | } |
299 | else | |
300 | shstrtab[shstrtabsize] = '\0'; | |
217aa764 | 301 | i_shdrp[shindex]->contents = shstrtab; |
252b5132 | 302 | } |
f075ee0c | 303 | return (char *) shstrtab; |
252b5132 RH |
304 | } |
305 | ||
306 | char * | |
217aa764 AM |
307 | bfd_elf_string_from_elf_section (bfd *abfd, |
308 | unsigned int shindex, | |
309 | unsigned int strindex) | |
252b5132 RH |
310 | { |
311 | Elf_Internal_Shdr *hdr; | |
312 | ||
313 | if (strindex == 0) | |
314 | return ""; | |
315 | ||
74f2e02b AM |
316 | if (elf_elfsections (abfd) == NULL || shindex >= elf_numsections (abfd)) |
317 | return NULL; | |
318 | ||
252b5132 RH |
319 | hdr = elf_elfsections (abfd)[shindex]; |
320 | ||
321 | if (hdr->contents == NULL | |
322 | && bfd_elf_get_str_section (abfd, shindex) == NULL) | |
323 | return NULL; | |
324 | ||
325 | if (strindex >= hdr->sh_size) | |
326 | { | |
1b3a8575 | 327 | unsigned int shstrndx = elf_elfheader(abfd)->e_shstrndx; |
252b5132 | 328 | (*_bfd_error_handler) |
d003868e AM |
329 | (_("%B: invalid string offset %u >= %lu for section `%s'"), |
330 | abfd, strindex, (unsigned long) hdr->sh_size, | |
1b3a8575 | 331 | (shindex == shstrndx && strindex == hdr->sh_name |
252b5132 | 332 | ? ".shstrtab" |
1b3a8575 | 333 | : bfd_elf_string_from_elf_section (abfd, shstrndx, hdr->sh_name))); |
45b222d6 | 334 | return NULL; |
252b5132 RH |
335 | } |
336 | ||
337 | return ((char *) hdr->contents) + strindex; | |
338 | } | |
339 | ||
6cdc0ccc AM |
340 | /* Read and convert symbols to internal format. |
341 | SYMCOUNT specifies the number of symbols to read, starting from | |
342 | symbol SYMOFFSET. If any of INTSYM_BUF, EXTSYM_BUF or EXTSHNDX_BUF | |
343 | are non-NULL, they are used to store the internal symbols, external | |
b7c368d0 NC |
344 | symbols, and symbol section index extensions, respectively. |
345 | Returns a pointer to the internal symbol buffer (malloced if necessary) | |
346 | or NULL if there were no symbols or some kind of problem. */ | |
6cdc0ccc AM |
347 | |
348 | Elf_Internal_Sym * | |
217aa764 AM |
349 | bfd_elf_get_elf_syms (bfd *ibfd, |
350 | Elf_Internal_Shdr *symtab_hdr, | |
351 | size_t symcount, | |
352 | size_t symoffset, | |
353 | Elf_Internal_Sym *intsym_buf, | |
354 | void *extsym_buf, | |
355 | Elf_External_Sym_Shndx *extshndx_buf) | |
6cdc0ccc AM |
356 | { |
357 | Elf_Internal_Shdr *shndx_hdr; | |
217aa764 | 358 | void *alloc_ext; |
df622259 | 359 | const bfd_byte *esym; |
6cdc0ccc AM |
360 | Elf_External_Sym_Shndx *alloc_extshndx; |
361 | Elf_External_Sym_Shndx *shndx; | |
4dd07732 | 362 | Elf_Internal_Sym *alloc_intsym; |
6cdc0ccc AM |
363 | Elf_Internal_Sym *isym; |
364 | Elf_Internal_Sym *isymend; | |
9c5bfbb7 | 365 | const struct elf_backend_data *bed; |
6cdc0ccc AM |
366 | size_t extsym_size; |
367 | bfd_size_type amt; | |
368 | file_ptr pos; | |
369 | ||
e44a2c9c AM |
370 | if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour) |
371 | abort (); | |
372 | ||
6cdc0ccc AM |
373 | if (symcount == 0) |
374 | return intsym_buf; | |
375 | ||
376 | /* Normal syms might have section extension entries. */ | |
377 | shndx_hdr = NULL; | |
378 | if (symtab_hdr == &elf_tdata (ibfd)->symtab_hdr) | |
379 | shndx_hdr = &elf_tdata (ibfd)->symtab_shndx_hdr; | |
380 | ||
381 | /* Read the symbols. */ | |
382 | alloc_ext = NULL; | |
383 | alloc_extshndx = NULL; | |
4dd07732 | 384 | alloc_intsym = NULL; |
6cdc0ccc AM |
385 | bed = get_elf_backend_data (ibfd); |
386 | extsym_size = bed->s->sizeof_sym; | |
387 | amt = symcount * extsym_size; | |
388 | pos = symtab_hdr->sh_offset + symoffset * extsym_size; | |
389 | if (extsym_buf == NULL) | |
390 | { | |
d0fb9a8d | 391 | alloc_ext = bfd_malloc2 (symcount, extsym_size); |
6cdc0ccc AM |
392 | extsym_buf = alloc_ext; |
393 | } | |
394 | if (extsym_buf == NULL | |
395 | || bfd_seek (ibfd, pos, SEEK_SET) != 0 | |
396 | || bfd_bread (extsym_buf, amt, ibfd) != amt) | |
397 | { | |
398 | intsym_buf = NULL; | |
399 | goto out; | |
400 | } | |
401 | ||
402 | if (shndx_hdr == NULL || shndx_hdr->sh_size == 0) | |
403 | extshndx_buf = NULL; | |
404 | else | |
405 | { | |
406 | amt = symcount * sizeof (Elf_External_Sym_Shndx); | |
407 | pos = shndx_hdr->sh_offset + symoffset * sizeof (Elf_External_Sym_Shndx); | |
408 | if (extshndx_buf == NULL) | |
409 | { | |
d0fb9a8d JJ |
410 | alloc_extshndx = bfd_malloc2 (symcount, |
411 | sizeof (Elf_External_Sym_Shndx)); | |
6cdc0ccc AM |
412 | extshndx_buf = alloc_extshndx; |
413 | } | |
414 | if (extshndx_buf == NULL | |
415 | || bfd_seek (ibfd, pos, SEEK_SET) != 0 | |
416 | || bfd_bread (extshndx_buf, amt, ibfd) != amt) | |
417 | { | |
418 | intsym_buf = NULL; | |
419 | goto out; | |
420 | } | |
421 | } | |
422 | ||
423 | if (intsym_buf == NULL) | |
424 | { | |
4dd07732 AM |
425 | alloc_intsym = bfd_malloc2 (symcount, sizeof (Elf_Internal_Sym)); |
426 | intsym_buf = alloc_intsym; | |
6cdc0ccc AM |
427 | if (intsym_buf == NULL) |
428 | goto out; | |
429 | } | |
430 | ||
431 | /* Convert the symbols to internal form. */ | |
432 | isymend = intsym_buf + symcount; | |
433 | for (esym = extsym_buf, isym = intsym_buf, shndx = extshndx_buf; | |
434 | isym < isymend; | |
435 | esym += extsym_size, isym++, shndx = shndx != NULL ? shndx + 1 : NULL) | |
8384fb8f AM |
436 | if (!(*bed->s->swap_symbol_in) (ibfd, esym, shndx, isym)) |
437 | { | |
438 | symoffset += (esym - (bfd_byte *) extsym_buf) / extsym_size; | |
439 | (*_bfd_error_handler) (_("%B symbol number %lu references " | |
440 | "nonexistent SHT_SYMTAB_SHNDX section"), | |
441 | ibfd, (unsigned long) symoffset); | |
4dd07732 AM |
442 | if (alloc_intsym != NULL) |
443 | free (alloc_intsym); | |
8384fb8f AM |
444 | intsym_buf = NULL; |
445 | goto out; | |
446 | } | |
6cdc0ccc AM |
447 | |
448 | out: | |
449 | if (alloc_ext != NULL) | |
450 | free (alloc_ext); | |
451 | if (alloc_extshndx != NULL) | |
452 | free (alloc_extshndx); | |
453 | ||
454 | return intsym_buf; | |
455 | } | |
456 | ||
5cab59f6 AM |
457 | /* Look up a symbol name. */ |
458 | const char * | |
be8dd2ca AM |
459 | bfd_elf_sym_name (bfd *abfd, |
460 | Elf_Internal_Shdr *symtab_hdr, | |
26c61ae5 L |
461 | Elf_Internal_Sym *isym, |
462 | asection *sym_sec) | |
5cab59f6 | 463 | { |
26c61ae5 | 464 | const char *name; |
5cab59f6 | 465 | unsigned int iname = isym->st_name; |
be8dd2ca | 466 | unsigned int shindex = symtab_hdr->sh_link; |
26c61ae5 | 467 | |
138f35cc JJ |
468 | if (iname == 0 && ELF_ST_TYPE (isym->st_info) == STT_SECTION |
469 | /* Check for a bogus st_shndx to avoid crashing. */ | |
4fbb74a6 | 470 | && isym->st_shndx < elf_numsections (abfd)) |
5cab59f6 AM |
471 | { |
472 | iname = elf_elfsections (abfd)[isym->st_shndx]->sh_name; | |
473 | shindex = elf_elfheader (abfd)->e_shstrndx; | |
474 | } | |
475 | ||
26c61ae5 L |
476 | name = bfd_elf_string_from_elf_section (abfd, shindex, iname); |
477 | if (name == NULL) | |
478 | name = "(null)"; | |
479 | else if (sym_sec && *name == '\0') | |
480 | name = bfd_section_name (abfd, sym_sec); | |
481 | ||
482 | return name; | |
5cab59f6 AM |
483 | } |
484 | ||
dbb410c3 AM |
485 | /* Elf_Internal_Shdr->contents is an array of these for SHT_GROUP |
486 | sections. The first element is the flags, the rest are section | |
487 | pointers. */ | |
488 | ||
489 | typedef union elf_internal_group { | |
490 | Elf_Internal_Shdr *shdr; | |
491 | unsigned int flags; | |
492 | } Elf_Internal_Group; | |
493 | ||
b885599b AM |
494 | /* Return the name of the group signature symbol. Why isn't the |
495 | signature just a string? */ | |
496 | ||
497 | static const char * | |
217aa764 | 498 | group_signature (bfd *abfd, Elf_Internal_Shdr *ghdr) |
b885599b | 499 | { |
9dce4196 | 500 | Elf_Internal_Shdr *hdr; |
9dce4196 AM |
501 | unsigned char esym[sizeof (Elf64_External_Sym)]; |
502 | Elf_External_Sym_Shndx eshndx; | |
503 | Elf_Internal_Sym isym; | |
b885599b | 504 | |
13792e9d L |
505 | /* First we need to ensure the symbol table is available. Make sure |
506 | that it is a symbol table section. */ | |
4fbb74a6 AM |
507 | if (ghdr->sh_link >= elf_numsections (abfd)) |
508 | return NULL; | |
13792e9d L |
509 | hdr = elf_elfsections (abfd) [ghdr->sh_link]; |
510 | if (hdr->sh_type != SHT_SYMTAB | |
511 | || ! bfd_section_from_shdr (abfd, ghdr->sh_link)) | |
b885599b AM |
512 | return NULL; |
513 | ||
9dce4196 AM |
514 | /* Go read the symbol. */ |
515 | hdr = &elf_tdata (abfd)->symtab_hdr; | |
6cdc0ccc AM |
516 | if (bfd_elf_get_elf_syms (abfd, hdr, 1, ghdr->sh_info, |
517 | &isym, esym, &eshndx) == NULL) | |
b885599b | 518 | return NULL; |
9dce4196 | 519 | |
26c61ae5 | 520 | return bfd_elf_sym_name (abfd, hdr, &isym, NULL); |
b885599b AM |
521 | } |
522 | ||
dbb410c3 AM |
523 | /* Set next_in_group list pointer, and group name for NEWSECT. */ |
524 | ||
b34976b6 | 525 | static bfd_boolean |
217aa764 | 526 | setup_group (bfd *abfd, Elf_Internal_Shdr *hdr, asection *newsect) |
dbb410c3 AM |
527 | { |
528 | unsigned int num_group = elf_tdata (abfd)->num_group; | |
529 | ||
530 | /* If num_group is zero, read in all SHT_GROUP sections. The count | |
531 | is set to -1 if there are no SHT_GROUP sections. */ | |
532 | if (num_group == 0) | |
533 | { | |
534 | unsigned int i, shnum; | |
535 | ||
536 | /* First count the number of groups. If we have a SHT_GROUP | |
537 | section with just a flag word (ie. sh_size is 4), ignore it. */ | |
9ad5cbcf | 538 | shnum = elf_numsections (abfd); |
dbb410c3 | 539 | num_group = 0; |
08a40648 | 540 | |
1783205a NC |
541 | #define IS_VALID_GROUP_SECTION_HEADER(shdr) \ |
542 | ( (shdr)->sh_type == SHT_GROUP \ | |
543 | && (shdr)->sh_size >= (2 * GRP_ENTRY_SIZE) \ | |
544 | && (shdr)->sh_entsize == GRP_ENTRY_SIZE \ | |
545 | && ((shdr)->sh_size % GRP_ENTRY_SIZE) == 0) | |
08a40648 | 546 | |
dbb410c3 AM |
547 | for (i = 0; i < shnum; i++) |
548 | { | |
549 | Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i]; | |
1783205a NC |
550 | |
551 | if (IS_VALID_GROUP_SECTION_HEADER (shdr)) | |
dbb410c3 AM |
552 | num_group += 1; |
553 | } | |
554 | ||
555 | if (num_group == 0) | |
20dbb49d L |
556 | { |
557 | num_group = (unsigned) -1; | |
558 | elf_tdata (abfd)->num_group = num_group; | |
559 | } | |
560 | else | |
dbb410c3 AM |
561 | { |
562 | /* We keep a list of elf section headers for group sections, | |
563 | so we can find them quickly. */ | |
20dbb49d | 564 | bfd_size_type amt; |
d0fb9a8d | 565 | |
20dbb49d | 566 | elf_tdata (abfd)->num_group = num_group; |
d0fb9a8d JJ |
567 | elf_tdata (abfd)->group_sect_ptr |
568 | = bfd_alloc2 (abfd, num_group, sizeof (Elf_Internal_Shdr *)); | |
dbb410c3 | 569 | if (elf_tdata (abfd)->group_sect_ptr == NULL) |
b34976b6 | 570 | return FALSE; |
dbb410c3 AM |
571 | |
572 | num_group = 0; | |
573 | for (i = 0; i < shnum; i++) | |
574 | { | |
575 | Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i]; | |
1783205a NC |
576 | |
577 | if (IS_VALID_GROUP_SECTION_HEADER (shdr)) | |
dbb410c3 | 578 | { |
973ffd63 | 579 | unsigned char *src; |
dbb410c3 AM |
580 | Elf_Internal_Group *dest; |
581 | ||
582 | /* Add to list of sections. */ | |
583 | elf_tdata (abfd)->group_sect_ptr[num_group] = shdr; | |
584 | num_group += 1; | |
585 | ||
586 | /* Read the raw contents. */ | |
587 | BFD_ASSERT (sizeof (*dest) >= 4); | |
588 | amt = shdr->sh_size * sizeof (*dest) / 4; | |
d0fb9a8d JJ |
589 | shdr->contents = bfd_alloc2 (abfd, shdr->sh_size, |
590 | sizeof (*dest) / 4); | |
1783205a NC |
591 | /* PR binutils/4110: Handle corrupt group headers. */ |
592 | if (shdr->contents == NULL) | |
593 | { | |
594 | _bfd_error_handler | |
595 | (_("%B: Corrupt size field in group section header: 0x%lx"), abfd, shdr->sh_size); | |
596 | bfd_set_error (bfd_error_bad_value); | |
597 | return FALSE; | |
598 | } | |
599 | ||
600 | memset (shdr->contents, 0, amt); | |
601 | ||
602 | if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0 | |
dbb410c3 AM |
603 | || (bfd_bread (shdr->contents, shdr->sh_size, abfd) |
604 | != shdr->sh_size)) | |
b34976b6 | 605 | return FALSE; |
dbb410c3 AM |
606 | |
607 | /* Translate raw contents, a flag word followed by an | |
608 | array of elf section indices all in target byte order, | |
609 | to the flag word followed by an array of elf section | |
610 | pointers. */ | |
611 | src = shdr->contents + shdr->sh_size; | |
612 | dest = (Elf_Internal_Group *) (shdr->contents + amt); | |
613 | while (1) | |
614 | { | |
615 | unsigned int idx; | |
616 | ||
617 | src -= 4; | |
618 | --dest; | |
619 | idx = H_GET_32 (abfd, src); | |
620 | if (src == shdr->contents) | |
621 | { | |
622 | dest->flags = idx; | |
b885599b AM |
623 | if (shdr->bfd_section != NULL && (idx & GRP_COMDAT)) |
624 | shdr->bfd_section->flags | |
625 | |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD; | |
dbb410c3 AM |
626 | break; |
627 | } | |
628 | if (idx >= shnum) | |
629 | { | |
630 | ((*_bfd_error_handler) | |
d003868e | 631 | (_("%B: invalid SHT_GROUP entry"), abfd)); |
dbb410c3 AM |
632 | idx = 0; |
633 | } | |
634 | dest->shdr = elf_elfsections (abfd)[idx]; | |
635 | } | |
636 | } | |
637 | } | |
638 | } | |
639 | } | |
640 | ||
641 | if (num_group != (unsigned) -1) | |
642 | { | |
643 | unsigned int i; | |
644 | ||
645 | for (i = 0; i < num_group; i++) | |
646 | { | |
647 | Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i]; | |
648 | Elf_Internal_Group *idx = (Elf_Internal_Group *) shdr->contents; | |
649 | unsigned int n_elt = shdr->sh_size / 4; | |
650 | ||
651 | /* Look through this group's sections to see if current | |
652 | section is a member. */ | |
653 | while (--n_elt != 0) | |
654 | if ((++idx)->shdr == hdr) | |
655 | { | |
e0e8c97f | 656 | asection *s = NULL; |
dbb410c3 AM |
657 | |
658 | /* We are a member of this group. Go looking through | |
659 | other members to see if any others are linked via | |
660 | next_in_group. */ | |
661 | idx = (Elf_Internal_Group *) shdr->contents; | |
662 | n_elt = shdr->sh_size / 4; | |
663 | while (--n_elt != 0) | |
664 | if ((s = (++idx)->shdr->bfd_section) != NULL | |
945906ff | 665 | && elf_next_in_group (s) != NULL) |
dbb410c3 AM |
666 | break; |
667 | if (n_elt != 0) | |
668 | { | |
dbb410c3 AM |
669 | /* Snarf the group name from other member, and |
670 | insert current section in circular list. */ | |
945906ff AM |
671 | elf_group_name (newsect) = elf_group_name (s); |
672 | elf_next_in_group (newsect) = elf_next_in_group (s); | |
673 | elf_next_in_group (s) = newsect; | |
dbb410c3 AM |
674 | } |
675 | else | |
676 | { | |
dbb410c3 AM |
677 | const char *gname; |
678 | ||
b885599b AM |
679 | gname = group_signature (abfd, shdr); |
680 | if (gname == NULL) | |
b34976b6 | 681 | return FALSE; |
945906ff | 682 | elf_group_name (newsect) = gname; |
dbb410c3 AM |
683 | |
684 | /* Start a circular list with one element. */ | |
945906ff | 685 | elf_next_in_group (newsect) = newsect; |
dbb410c3 | 686 | } |
b885599b | 687 | |
9dce4196 AM |
688 | /* If the group section has been created, point to the |
689 | new member. */ | |
dbb410c3 | 690 | if (shdr->bfd_section != NULL) |
945906ff | 691 | elf_next_in_group (shdr->bfd_section) = newsect; |
b885599b | 692 | |
dbb410c3 AM |
693 | i = num_group - 1; |
694 | break; | |
695 | } | |
696 | } | |
697 | } | |
698 | ||
945906ff | 699 | if (elf_group_name (newsect) == NULL) |
dbb410c3 | 700 | { |
d003868e AM |
701 | (*_bfd_error_handler) (_("%B: no group info for section %A"), |
702 | abfd, newsect); | |
dbb410c3 | 703 | } |
b34976b6 | 704 | return TRUE; |
dbb410c3 AM |
705 | } |
706 | ||
3d7f7666 | 707 | bfd_boolean |
dd863624 | 708 | _bfd_elf_setup_sections (bfd *abfd) |
3d7f7666 L |
709 | { |
710 | unsigned int i; | |
711 | unsigned int num_group = elf_tdata (abfd)->num_group; | |
712 | bfd_boolean result = TRUE; | |
dd863624 L |
713 | asection *s; |
714 | ||
715 | /* Process SHF_LINK_ORDER. */ | |
716 | for (s = abfd->sections; s != NULL; s = s->next) | |
717 | { | |
718 | Elf_Internal_Shdr *this_hdr = &elf_section_data (s)->this_hdr; | |
719 | if ((this_hdr->sh_flags & SHF_LINK_ORDER) != 0) | |
720 | { | |
721 | unsigned int elfsec = this_hdr->sh_link; | |
722 | /* FIXME: The old Intel compiler and old strip/objcopy may | |
723 | not set the sh_link or sh_info fields. Hence we could | |
724 | get the situation where elfsec is 0. */ | |
725 | if (elfsec == 0) | |
726 | { | |
4fbb74a6 | 727 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
dd863624 L |
728 | if (bed->link_order_error_handler) |
729 | bed->link_order_error_handler | |
730 | (_("%B: warning: sh_link not set for section `%A'"), | |
731 | abfd, s); | |
732 | } | |
733 | else | |
734 | { | |
4fbb74a6 | 735 | asection *link = NULL; |
25bbc984 | 736 | |
4fbb74a6 AM |
737 | if (elfsec < elf_numsections (abfd)) |
738 | { | |
739 | this_hdr = elf_elfsections (abfd)[elfsec]; | |
740 | link = this_hdr->bfd_section; | |
741 | } | |
25bbc984 L |
742 | |
743 | /* PR 1991, 2008: | |
744 | Some strip/objcopy may leave an incorrect value in | |
745 | sh_link. We don't want to proceed. */ | |
25bbc984 L |
746 | if (link == NULL) |
747 | { | |
748 | (*_bfd_error_handler) | |
749 | (_("%B: sh_link [%d] in section `%A' is incorrect"), | |
750 | s->owner, s, elfsec); | |
751 | result = FALSE; | |
752 | } | |
753 | ||
754 | elf_linked_to_section (s) = link; | |
dd863624 L |
755 | } |
756 | } | |
757 | } | |
3d7f7666 | 758 | |
dd863624 | 759 | /* Process section groups. */ |
3d7f7666 L |
760 | if (num_group == (unsigned) -1) |
761 | return result; | |
762 | ||
763 | for (i = 0; i < num_group; i++) | |
764 | { | |
765 | Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i]; | |
766 | Elf_Internal_Group *idx = (Elf_Internal_Group *) shdr->contents; | |
767 | unsigned int n_elt = shdr->sh_size / 4; | |
768 | ||
769 | while (--n_elt != 0) | |
770 | if ((++idx)->shdr->bfd_section) | |
771 | elf_sec_group (idx->shdr->bfd_section) = shdr->bfd_section; | |
772 | else if (idx->shdr->sh_type == SHT_RELA | |
773 | || idx->shdr->sh_type == SHT_REL) | |
774 | /* We won't include relocation sections in section groups in | |
775 | output object files. We adjust the group section size here | |
776 | so that relocatable link will work correctly when | |
777 | relocation sections are in section group in input object | |
778 | files. */ | |
779 | shdr->bfd_section->size -= 4; | |
780 | else | |
781 | { | |
782 | /* There are some unknown sections in the group. */ | |
783 | (*_bfd_error_handler) | |
d003868e AM |
784 | (_("%B: unknown [%d] section `%s' in group [%s]"), |
785 | abfd, | |
3d7f7666 | 786 | (unsigned int) idx->shdr->sh_type, |
1b3a8575 AM |
787 | bfd_elf_string_from_elf_section (abfd, |
788 | (elf_elfheader (abfd) | |
789 | ->e_shstrndx), | |
790 | idx->shdr->sh_name), | |
3d7f7666 L |
791 | shdr->bfd_section->name); |
792 | result = FALSE; | |
793 | } | |
794 | } | |
795 | return result; | |
796 | } | |
797 | ||
72adc230 AM |
798 | bfd_boolean |
799 | bfd_elf_is_group_section (bfd *abfd ATTRIBUTE_UNUSED, const asection *sec) | |
800 | { | |
801 | return elf_next_in_group (sec) != NULL; | |
802 | } | |
803 | ||
252b5132 RH |
804 | /* Make a BFD section from an ELF section. We store a pointer to the |
805 | BFD section in the bfd_section field of the header. */ | |
806 | ||
b34976b6 | 807 | bfd_boolean |
217aa764 AM |
808 | _bfd_elf_make_section_from_shdr (bfd *abfd, |
809 | Elf_Internal_Shdr *hdr, | |
6dc132d9 L |
810 | const char *name, |
811 | int shindex) | |
252b5132 RH |
812 | { |
813 | asection *newsect; | |
814 | flagword flags; | |
9c5bfbb7 | 815 | const struct elf_backend_data *bed; |
252b5132 RH |
816 | |
817 | if (hdr->bfd_section != NULL) | |
818 | { | |
819 | BFD_ASSERT (strcmp (name, | |
820 | bfd_get_section_name (abfd, hdr->bfd_section)) == 0); | |
b34976b6 | 821 | return TRUE; |
252b5132 RH |
822 | } |
823 | ||
824 | newsect = bfd_make_section_anyway (abfd, name); | |
825 | if (newsect == NULL) | |
b34976b6 | 826 | return FALSE; |
252b5132 | 827 | |
1829f4b2 AM |
828 | hdr->bfd_section = newsect; |
829 | elf_section_data (newsect)->this_hdr = *hdr; | |
6dc132d9 | 830 | elf_section_data (newsect)->this_idx = shindex; |
1829f4b2 | 831 | |
2f89ff8d L |
832 | /* Always use the real type/flags. */ |
833 | elf_section_type (newsect) = hdr->sh_type; | |
834 | elf_section_flags (newsect) = hdr->sh_flags; | |
835 | ||
252b5132 RH |
836 | newsect->filepos = hdr->sh_offset; |
837 | ||
838 | if (! bfd_set_section_vma (abfd, newsect, hdr->sh_addr) | |
839 | || ! bfd_set_section_size (abfd, newsect, hdr->sh_size) | |
840 | || ! bfd_set_section_alignment (abfd, newsect, | |
72de5009 | 841 | bfd_log2 (hdr->sh_addralign))) |
b34976b6 | 842 | return FALSE; |
252b5132 RH |
843 | |
844 | flags = SEC_NO_FLAGS; | |
845 | if (hdr->sh_type != SHT_NOBITS) | |
846 | flags |= SEC_HAS_CONTENTS; | |
dbb410c3 | 847 | if (hdr->sh_type == SHT_GROUP) |
b3096250 | 848 | flags |= SEC_GROUP | SEC_EXCLUDE; |
252b5132 RH |
849 | if ((hdr->sh_flags & SHF_ALLOC) != 0) |
850 | { | |
851 | flags |= SEC_ALLOC; | |
852 | if (hdr->sh_type != SHT_NOBITS) | |
853 | flags |= SEC_LOAD; | |
854 | } | |
855 | if ((hdr->sh_flags & SHF_WRITE) == 0) | |
856 | flags |= SEC_READONLY; | |
857 | if ((hdr->sh_flags & SHF_EXECINSTR) != 0) | |
858 | flags |= SEC_CODE; | |
859 | else if ((flags & SEC_LOAD) != 0) | |
860 | flags |= SEC_DATA; | |
f5fa8ca2 JJ |
861 | if ((hdr->sh_flags & SHF_MERGE) != 0) |
862 | { | |
863 | flags |= SEC_MERGE; | |
864 | newsect->entsize = hdr->sh_entsize; | |
865 | if ((hdr->sh_flags & SHF_STRINGS) != 0) | |
866 | flags |= SEC_STRINGS; | |
867 | } | |
dbb410c3 AM |
868 | if (hdr->sh_flags & SHF_GROUP) |
869 | if (!setup_group (abfd, hdr, newsect)) | |
b34976b6 | 870 | return FALSE; |
13ae64f3 JJ |
871 | if ((hdr->sh_flags & SHF_TLS) != 0) |
872 | flags |= SEC_THREAD_LOCAL; | |
252b5132 | 873 | |
3d2b39cf | 874 | if ((flags & SEC_ALLOC) == 0) |
7a6cc5fb | 875 | { |
3d2b39cf L |
876 | /* The debugging sections appear to be recognized only by name, |
877 | not any sort of flag. Their SEC_ALLOC bits are cleared. */ | |
878 | static const struct | |
879 | { | |
880 | const char *name; | |
881 | int len; | |
882 | } debug_sections [] = | |
883 | { | |
0112cd26 | 884 | { STRING_COMMA_LEN ("debug") }, /* 'd' */ |
3d2b39cf L |
885 | { NULL, 0 }, /* 'e' */ |
886 | { NULL, 0 }, /* 'f' */ | |
0112cd26 | 887 | { STRING_COMMA_LEN ("gnu.linkonce.wi.") }, /* 'g' */ |
3d2b39cf L |
888 | { NULL, 0 }, /* 'h' */ |
889 | { NULL, 0 }, /* 'i' */ | |
890 | { NULL, 0 }, /* 'j' */ | |
891 | { NULL, 0 }, /* 'k' */ | |
0112cd26 | 892 | { STRING_COMMA_LEN ("line") }, /* 'l' */ |
3d2b39cf L |
893 | { NULL, 0 }, /* 'm' */ |
894 | { NULL, 0 }, /* 'n' */ | |
895 | { NULL, 0 }, /* 'o' */ | |
896 | { NULL, 0 }, /* 'p' */ | |
897 | { NULL, 0 }, /* 'q' */ | |
898 | { NULL, 0 }, /* 'r' */ | |
1b315056 CS |
899 | { STRING_COMMA_LEN ("stab") }, /* 's' */ |
900 | { NULL, 0 }, /* 't' */ | |
901 | { NULL, 0 }, /* 'u' */ | |
902 | { NULL, 0 }, /* 'v' */ | |
903 | { NULL, 0 }, /* 'w' */ | |
904 | { NULL, 0 }, /* 'x' */ | |
905 | { NULL, 0 }, /* 'y' */ | |
906 | { STRING_COMMA_LEN ("zdebug") } /* 'z' */ | |
3d2b39cf | 907 | }; |
08a40648 | 908 | |
3d2b39cf L |
909 | if (name [0] == '.') |
910 | { | |
911 | int i = name [1] - 'd'; | |
912 | if (i >= 0 | |
913 | && i < (int) ARRAY_SIZE (debug_sections) | |
914 | && debug_sections [i].name != NULL | |
915 | && strncmp (&name [1], debug_sections [i].name, | |
916 | debug_sections [i].len) == 0) | |
917 | flags |= SEC_DEBUGGING; | |
918 | } | |
919 | } | |
252b5132 RH |
920 | |
921 | /* As a GNU extension, if the name begins with .gnu.linkonce, we | |
922 | only link a single copy of the section. This is used to support | |
923 | g++. g++ will emit each template expansion in its own section. | |
924 | The symbols will be defined as weak, so that multiple definitions | |
925 | are permitted. The GNU linker extension is to actually discard | |
926 | all but one of the sections. */ | |
0112cd26 | 927 | if (CONST_STRNEQ (name, ".gnu.linkonce") |
b885599b | 928 | && elf_next_in_group (newsect) == NULL) |
252b5132 RH |
929 | flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD; |
930 | ||
fa152c49 JW |
931 | bed = get_elf_backend_data (abfd); |
932 | if (bed->elf_backend_section_flags) | |
933 | if (! bed->elf_backend_section_flags (&flags, hdr)) | |
b34976b6 | 934 | return FALSE; |
fa152c49 | 935 | |
252b5132 | 936 | if (! bfd_set_section_flags (abfd, newsect, flags)) |
b34976b6 | 937 | return FALSE; |
252b5132 | 938 | |
718175fa JK |
939 | /* We do not parse the PT_NOTE segments as we are interested even in the |
940 | separate debug info files which may have the segments offsets corrupted. | |
941 | PT_NOTEs from the core files are currently not parsed using BFD. */ | |
942 | if (hdr->sh_type == SHT_NOTE) | |
943 | { | |
baea7ef1 | 944 | bfd_byte *contents; |
718175fa | 945 | |
baea7ef1 | 946 | if (!bfd_malloc_and_get_section (abfd, newsect, &contents)) |
718175fa JK |
947 | return FALSE; |
948 | ||
baea7ef1 | 949 | elf_parse_notes (abfd, (char *) contents, hdr->sh_size, -1); |
718175fa JK |
950 | free (contents); |
951 | } | |
952 | ||
252b5132 RH |
953 | if ((flags & SEC_ALLOC) != 0) |
954 | { | |
955 | Elf_Internal_Phdr *phdr; | |
6ffd7900 AM |
956 | unsigned int i, nload; |
957 | ||
958 | /* Some ELF linkers produce binaries with all the program header | |
959 | p_paddr fields zero. If we have such a binary with more than | |
960 | one PT_LOAD header, then leave the section lma equal to vma | |
961 | so that we don't create sections with overlapping lma. */ | |
962 | phdr = elf_tdata (abfd)->phdr; | |
963 | for (nload = 0, i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++) | |
964 | if (phdr->p_paddr != 0) | |
965 | break; | |
966 | else if (phdr->p_type == PT_LOAD && phdr->p_memsz != 0) | |
967 | ++nload; | |
968 | if (i >= elf_elfheader (abfd)->e_phnum && nload > 1) | |
969 | return TRUE; | |
252b5132 | 970 | |
252b5132 RH |
971 | phdr = elf_tdata (abfd)->phdr; |
972 | for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++) | |
973 | { | |
88967714 | 974 | if (phdr->p_type == PT_LOAD |
e7e75368 | 975 | && ELF_IS_SECTION_IN_SEGMENT (hdr, phdr)) |
252b5132 | 976 | { |
88967714 AM |
977 | if ((flags & SEC_LOAD) == 0) |
978 | newsect->lma = (phdr->p_paddr | |
979 | + hdr->sh_addr - phdr->p_vaddr); | |
980 | else | |
981 | /* We used to use the same adjustment for SEC_LOAD | |
982 | sections, but that doesn't work if the segment | |
983 | is packed with code from multiple VMAs. | |
984 | Instead we calculate the section LMA based on | |
985 | the segment LMA. It is assumed that the | |
986 | segment will contain sections with contiguous | |
987 | LMAs, even if the VMAs are not. */ | |
988 | newsect->lma = (phdr->p_paddr | |
989 | + hdr->sh_offset - phdr->p_offset); | |
990 | ||
991 | /* With contiguous segments, we can't tell from file | |
992 | offsets whether a section with zero size should | |
993 | be placed at the end of one segment or the | |
994 | beginning of the next. Decide based on vaddr. */ | |
995 | if (hdr->sh_addr >= phdr->p_vaddr | |
996 | && (hdr->sh_addr + hdr->sh_size | |
997 | <= phdr->p_vaddr + phdr->p_memsz)) | |
998 | break; | |
252b5132 RH |
999 | } |
1000 | } | |
1001 | } | |
1002 | ||
b34976b6 | 1003 | return TRUE; |
252b5132 RH |
1004 | } |
1005 | ||
252b5132 RH |
1006 | const char *const bfd_elf_section_type_names[] = { |
1007 | "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB", | |
1008 | "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE", | |
1009 | "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM", | |
1010 | }; | |
1011 | ||
1049f94e | 1012 | /* ELF relocs are against symbols. If we are producing relocatable |
252b5132 RH |
1013 | output, and the reloc is against an external symbol, and nothing |
1014 | has given us any additional addend, the resulting reloc will also | |
1015 | be against the same symbol. In such a case, we don't want to | |
1016 | change anything about the way the reloc is handled, since it will | |
1017 | all be done at final link time. Rather than put special case code | |
1018 | into bfd_perform_relocation, all the reloc types use this howto | |
1019 | function. It just short circuits the reloc if producing | |
1049f94e | 1020 | relocatable output against an external symbol. */ |
252b5132 | 1021 | |
252b5132 | 1022 | bfd_reloc_status_type |
217aa764 AM |
1023 | bfd_elf_generic_reloc (bfd *abfd ATTRIBUTE_UNUSED, |
1024 | arelent *reloc_entry, | |
1025 | asymbol *symbol, | |
1026 | void *data ATTRIBUTE_UNUSED, | |
1027 | asection *input_section, | |
1028 | bfd *output_bfd, | |
1029 | char **error_message ATTRIBUTE_UNUSED) | |
1030 | { | |
1031 | if (output_bfd != NULL | |
252b5132 RH |
1032 | && (symbol->flags & BSF_SECTION_SYM) == 0 |
1033 | && (! reloc_entry->howto->partial_inplace | |
1034 | || reloc_entry->addend == 0)) | |
1035 | { | |
1036 | reloc_entry->address += input_section->output_offset; | |
1037 | return bfd_reloc_ok; | |
1038 | } | |
1039 | ||
1040 | return bfd_reloc_continue; | |
1041 | } | |
1042 | \f | |
0ac4564e L |
1043 | /* Copy the program header and other data from one object module to |
1044 | another. */ | |
252b5132 | 1045 | |
b34976b6 | 1046 | bfd_boolean |
217aa764 | 1047 | _bfd_elf_copy_private_bfd_data (bfd *ibfd, bfd *obfd) |
2d502050 L |
1048 | { |
1049 | if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour | |
1050 | || bfd_get_flavour (obfd) != bfd_target_elf_flavour) | |
b34976b6 | 1051 | return TRUE; |
2d502050 L |
1052 | |
1053 | BFD_ASSERT (!elf_flags_init (obfd) | |
1054 | || (elf_elfheader (obfd)->e_flags | |
1055 | == elf_elfheader (ibfd)->e_flags)); | |
1056 | ||
0ac4564e | 1057 | elf_gp (obfd) = elf_gp (ibfd); |
2d502050 | 1058 | elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags; |
b34976b6 | 1059 | elf_flags_init (obfd) = TRUE; |
104d59d1 JM |
1060 | |
1061 | /* Copy object attributes. */ | |
1062 | _bfd_elf_copy_obj_attributes (ibfd, obfd); | |
1063 | ||
b34976b6 | 1064 | return TRUE; |
2d502050 L |
1065 | } |
1066 | ||
cedc298e L |
1067 | static const char * |
1068 | get_segment_type (unsigned int p_type) | |
1069 | { | |
1070 | const char *pt; | |
1071 | switch (p_type) | |
1072 | { | |
1073 | case PT_NULL: pt = "NULL"; break; | |
1074 | case PT_LOAD: pt = "LOAD"; break; | |
1075 | case PT_DYNAMIC: pt = "DYNAMIC"; break; | |
1076 | case PT_INTERP: pt = "INTERP"; break; | |
1077 | case PT_NOTE: pt = "NOTE"; break; | |
1078 | case PT_SHLIB: pt = "SHLIB"; break; | |
1079 | case PT_PHDR: pt = "PHDR"; break; | |
1080 | case PT_TLS: pt = "TLS"; break; | |
1081 | case PT_GNU_EH_FRAME: pt = "EH_FRAME"; break; | |
2b05f1b7 | 1082 | case PT_GNU_STACK: pt = "STACK"; break; |
cedc298e L |
1083 | case PT_GNU_RELRO: pt = "RELRO"; break; |
1084 | default: pt = NULL; break; | |
1085 | } | |
1086 | return pt; | |
1087 | } | |
1088 | ||
f0b79d91 L |
1089 | /* Print out the program headers. */ |
1090 | ||
b34976b6 | 1091 | bfd_boolean |
217aa764 | 1092 | _bfd_elf_print_private_bfd_data (bfd *abfd, void *farg) |
252b5132 | 1093 | { |
217aa764 | 1094 | FILE *f = farg; |
252b5132 RH |
1095 | Elf_Internal_Phdr *p; |
1096 | asection *s; | |
1097 | bfd_byte *dynbuf = NULL; | |
1098 | ||
1099 | p = elf_tdata (abfd)->phdr; | |
1100 | if (p != NULL) | |
1101 | { | |
1102 | unsigned int i, c; | |
1103 | ||
1104 | fprintf (f, _("\nProgram Header:\n")); | |
1105 | c = elf_elfheader (abfd)->e_phnum; | |
1106 | for (i = 0; i < c; i++, p++) | |
1107 | { | |
cedc298e | 1108 | const char *pt = get_segment_type (p->p_type); |
252b5132 RH |
1109 | char buf[20]; |
1110 | ||
cedc298e | 1111 | if (pt == NULL) |
252b5132 | 1112 | { |
cedc298e L |
1113 | sprintf (buf, "0x%lx", p->p_type); |
1114 | pt = buf; | |
252b5132 | 1115 | } |
dc810e39 | 1116 | fprintf (f, "%8s off 0x", pt); |
60b89a18 | 1117 | bfd_fprintf_vma (abfd, f, p->p_offset); |
252b5132 | 1118 | fprintf (f, " vaddr 0x"); |
60b89a18 | 1119 | bfd_fprintf_vma (abfd, f, p->p_vaddr); |
252b5132 | 1120 | fprintf (f, " paddr 0x"); |
60b89a18 | 1121 | bfd_fprintf_vma (abfd, f, p->p_paddr); |
252b5132 RH |
1122 | fprintf (f, " align 2**%u\n", bfd_log2 (p->p_align)); |
1123 | fprintf (f, " filesz 0x"); | |
60b89a18 | 1124 | bfd_fprintf_vma (abfd, f, p->p_filesz); |
252b5132 | 1125 | fprintf (f, " memsz 0x"); |
60b89a18 | 1126 | bfd_fprintf_vma (abfd, f, p->p_memsz); |
252b5132 RH |
1127 | fprintf (f, " flags %c%c%c", |
1128 | (p->p_flags & PF_R) != 0 ? 'r' : '-', | |
1129 | (p->p_flags & PF_W) != 0 ? 'w' : '-', | |
1130 | (p->p_flags & PF_X) != 0 ? 'x' : '-'); | |
dc810e39 AM |
1131 | if ((p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X)) != 0) |
1132 | fprintf (f, " %lx", p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X)); | |
252b5132 RH |
1133 | fprintf (f, "\n"); |
1134 | } | |
1135 | } | |
1136 | ||
1137 | s = bfd_get_section_by_name (abfd, ".dynamic"); | |
1138 | if (s != NULL) | |
1139 | { | |
cb33740c | 1140 | unsigned int elfsec; |
dc810e39 | 1141 | unsigned long shlink; |
252b5132 RH |
1142 | bfd_byte *extdyn, *extdynend; |
1143 | size_t extdynsize; | |
217aa764 | 1144 | void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *); |
252b5132 RH |
1145 | |
1146 | fprintf (f, _("\nDynamic Section:\n")); | |
1147 | ||
eea6121a | 1148 | if (!bfd_malloc_and_get_section (abfd, s, &dynbuf)) |
252b5132 RH |
1149 | goto error_return; |
1150 | ||
1151 | elfsec = _bfd_elf_section_from_bfd_section (abfd, s); | |
cb33740c | 1152 | if (elfsec == SHN_BAD) |
252b5132 | 1153 | goto error_return; |
dc810e39 | 1154 | shlink = elf_elfsections (abfd)[elfsec]->sh_link; |
252b5132 RH |
1155 | |
1156 | extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn; | |
1157 | swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in; | |
1158 | ||
1159 | extdyn = dynbuf; | |
eea6121a | 1160 | extdynend = extdyn + s->size; |
252b5132 RH |
1161 | for (; extdyn < extdynend; extdyn += extdynsize) |
1162 | { | |
1163 | Elf_Internal_Dyn dyn; | |
ad9563d6 | 1164 | const char *name = ""; |
252b5132 | 1165 | char ab[20]; |
b34976b6 | 1166 | bfd_boolean stringp; |
ad9563d6 | 1167 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
252b5132 | 1168 | |
217aa764 | 1169 | (*swap_dyn_in) (abfd, extdyn, &dyn); |
252b5132 RH |
1170 | |
1171 | if (dyn.d_tag == DT_NULL) | |
1172 | break; | |
1173 | ||
b34976b6 | 1174 | stringp = FALSE; |
252b5132 RH |
1175 | switch (dyn.d_tag) |
1176 | { | |
1177 | default: | |
ad9563d6 CM |
1178 | if (bed->elf_backend_get_target_dtag) |
1179 | name = (*bed->elf_backend_get_target_dtag) (dyn.d_tag); | |
1180 | ||
1181 | if (!strcmp (name, "")) | |
1182 | { | |
1183 | sprintf (ab, "0x%lx", (unsigned long) dyn.d_tag); | |
1184 | name = ab; | |
1185 | } | |
252b5132 RH |
1186 | break; |
1187 | ||
b34976b6 | 1188 | case DT_NEEDED: name = "NEEDED"; stringp = TRUE; break; |
252b5132 RH |
1189 | case DT_PLTRELSZ: name = "PLTRELSZ"; break; |
1190 | case DT_PLTGOT: name = "PLTGOT"; break; | |
1191 | case DT_HASH: name = "HASH"; break; | |
1192 | case DT_STRTAB: name = "STRTAB"; break; | |
1193 | case DT_SYMTAB: name = "SYMTAB"; break; | |
1194 | case DT_RELA: name = "RELA"; break; | |
1195 | case DT_RELASZ: name = "RELASZ"; break; | |
1196 | case DT_RELAENT: name = "RELAENT"; break; | |
1197 | case DT_STRSZ: name = "STRSZ"; break; | |
1198 | case DT_SYMENT: name = "SYMENT"; break; | |
1199 | case DT_INIT: name = "INIT"; break; | |
1200 | case DT_FINI: name = "FINI"; break; | |
b34976b6 AM |
1201 | case DT_SONAME: name = "SONAME"; stringp = TRUE; break; |
1202 | case DT_RPATH: name = "RPATH"; stringp = TRUE; break; | |
252b5132 RH |
1203 | case DT_SYMBOLIC: name = "SYMBOLIC"; break; |
1204 | case DT_REL: name = "REL"; break; | |
1205 | case DT_RELSZ: name = "RELSZ"; break; | |
1206 | case DT_RELENT: name = "RELENT"; break; | |
1207 | case DT_PLTREL: name = "PLTREL"; break; | |
1208 | case DT_DEBUG: name = "DEBUG"; break; | |
1209 | case DT_TEXTREL: name = "TEXTREL"; break; | |
1210 | case DT_JMPREL: name = "JMPREL"; break; | |
94558834 L |
1211 | case DT_BIND_NOW: name = "BIND_NOW"; break; |
1212 | case DT_INIT_ARRAY: name = "INIT_ARRAY"; break; | |
1213 | case DT_FINI_ARRAY: name = "FINI_ARRAY"; break; | |
1214 | case DT_INIT_ARRAYSZ: name = "INIT_ARRAYSZ"; break; | |
1215 | case DT_FINI_ARRAYSZ: name = "FINI_ARRAYSZ"; break; | |
b34976b6 | 1216 | case DT_RUNPATH: name = "RUNPATH"; stringp = TRUE; break; |
94558834 L |
1217 | case DT_FLAGS: name = "FLAGS"; break; |
1218 | case DT_PREINIT_ARRAY: name = "PREINIT_ARRAY"; break; | |
1219 | case DT_PREINIT_ARRAYSZ: name = "PREINIT_ARRAYSZ"; break; | |
d48188b9 | 1220 | case DT_CHECKSUM: name = "CHECKSUM"; break; |
94558834 L |
1221 | case DT_PLTPADSZ: name = "PLTPADSZ"; break; |
1222 | case DT_MOVEENT: name = "MOVEENT"; break; | |
1223 | case DT_MOVESZ: name = "MOVESZ"; break; | |
1224 | case DT_FEATURE: name = "FEATURE"; break; | |
1225 | case DT_POSFLAG_1: name = "POSFLAG_1"; break; | |
1226 | case DT_SYMINSZ: name = "SYMINSZ"; break; | |
1227 | case DT_SYMINENT: name = "SYMINENT"; break; | |
b34976b6 AM |
1228 | case DT_CONFIG: name = "CONFIG"; stringp = TRUE; break; |
1229 | case DT_DEPAUDIT: name = "DEPAUDIT"; stringp = TRUE; break; | |
1230 | case DT_AUDIT: name = "AUDIT"; stringp = TRUE; break; | |
94558834 L |
1231 | case DT_PLTPAD: name = "PLTPAD"; break; |
1232 | case DT_MOVETAB: name = "MOVETAB"; break; | |
1233 | case DT_SYMINFO: name = "SYMINFO"; break; | |
1234 | case DT_RELACOUNT: name = "RELACOUNT"; break; | |
1235 | case DT_RELCOUNT: name = "RELCOUNT"; break; | |
1236 | case DT_FLAGS_1: name = "FLAGS_1"; break; | |
252b5132 RH |
1237 | case DT_VERSYM: name = "VERSYM"; break; |
1238 | case DT_VERDEF: name = "VERDEF"; break; | |
1239 | case DT_VERDEFNUM: name = "VERDEFNUM"; break; | |
1240 | case DT_VERNEED: name = "VERNEED"; break; | |
1241 | case DT_VERNEEDNUM: name = "VERNEEDNUM"; break; | |
b34976b6 | 1242 | case DT_AUXILIARY: name = "AUXILIARY"; stringp = TRUE; break; |
94558834 | 1243 | case DT_USED: name = "USED"; break; |
b34976b6 | 1244 | case DT_FILTER: name = "FILTER"; stringp = TRUE; break; |
fdc90cb4 | 1245 | case DT_GNU_HASH: name = "GNU_HASH"; break; |
252b5132 RH |
1246 | } |
1247 | ||
ad9563d6 | 1248 | fprintf (f, " %-20s ", name); |
252b5132 | 1249 | if (! stringp) |
a1f3c56e AN |
1250 | { |
1251 | fprintf (f, "0x"); | |
1252 | bfd_fprintf_vma (abfd, f, dyn.d_un.d_val); | |
1253 | } | |
252b5132 RH |
1254 | else |
1255 | { | |
1256 | const char *string; | |
dc810e39 | 1257 | unsigned int tagv = dyn.d_un.d_val; |
252b5132 | 1258 | |
dc810e39 | 1259 | string = bfd_elf_string_from_elf_section (abfd, shlink, tagv); |
252b5132 RH |
1260 | if (string == NULL) |
1261 | goto error_return; | |
1262 | fprintf (f, "%s", string); | |
1263 | } | |
1264 | fprintf (f, "\n"); | |
1265 | } | |
1266 | ||
1267 | free (dynbuf); | |
1268 | dynbuf = NULL; | |
1269 | } | |
1270 | ||
1271 | if ((elf_dynverdef (abfd) != 0 && elf_tdata (abfd)->verdef == NULL) | |
1272 | || (elf_dynverref (abfd) != 0 && elf_tdata (abfd)->verref == NULL)) | |
1273 | { | |
fc0e6df6 | 1274 | if (! _bfd_elf_slurp_version_tables (abfd, FALSE)) |
b34976b6 | 1275 | return FALSE; |
252b5132 RH |
1276 | } |
1277 | ||
1278 | if (elf_dynverdef (abfd) != 0) | |
1279 | { | |
1280 | Elf_Internal_Verdef *t; | |
1281 | ||
1282 | fprintf (f, _("\nVersion definitions:\n")); | |
1283 | for (t = elf_tdata (abfd)->verdef; t != NULL; t = t->vd_nextdef) | |
1284 | { | |
1285 | fprintf (f, "%d 0x%2.2x 0x%8.8lx %s\n", t->vd_ndx, | |
d0fb9a8d JJ |
1286 | t->vd_flags, t->vd_hash, |
1287 | t->vd_nodename ? t->vd_nodename : "<corrupt>"); | |
1288 | if (t->vd_auxptr != NULL && t->vd_auxptr->vda_nextptr != NULL) | |
252b5132 RH |
1289 | { |
1290 | Elf_Internal_Verdaux *a; | |
1291 | ||
1292 | fprintf (f, "\t"); | |
1293 | for (a = t->vd_auxptr->vda_nextptr; | |
1294 | a != NULL; | |
1295 | a = a->vda_nextptr) | |
d0fb9a8d JJ |
1296 | fprintf (f, "%s ", |
1297 | a->vda_nodename ? a->vda_nodename : "<corrupt>"); | |
252b5132 RH |
1298 | fprintf (f, "\n"); |
1299 | } | |
1300 | } | |
1301 | } | |
1302 | ||
1303 | if (elf_dynverref (abfd) != 0) | |
1304 | { | |
1305 | Elf_Internal_Verneed *t; | |
1306 | ||
1307 | fprintf (f, _("\nVersion References:\n")); | |
1308 | for (t = elf_tdata (abfd)->verref; t != NULL; t = t->vn_nextref) | |
1309 | { | |
1310 | Elf_Internal_Vernaux *a; | |
1311 | ||
d0fb9a8d JJ |
1312 | fprintf (f, _(" required from %s:\n"), |
1313 | t->vn_filename ? t->vn_filename : "<corrupt>"); | |
252b5132 RH |
1314 | for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) |
1315 | fprintf (f, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a->vna_hash, | |
d0fb9a8d JJ |
1316 | a->vna_flags, a->vna_other, |
1317 | a->vna_nodename ? a->vna_nodename : "<corrupt>"); | |
252b5132 RH |
1318 | } |
1319 | } | |
1320 | ||
b34976b6 | 1321 | return TRUE; |
252b5132 RH |
1322 | |
1323 | error_return: | |
1324 | if (dynbuf != NULL) | |
1325 | free (dynbuf); | |
b34976b6 | 1326 | return FALSE; |
252b5132 RH |
1327 | } |
1328 | ||
1329 | /* Display ELF-specific fields of a symbol. */ | |
1330 | ||
1331 | void | |
217aa764 AM |
1332 | bfd_elf_print_symbol (bfd *abfd, |
1333 | void *filep, | |
1334 | asymbol *symbol, | |
1335 | bfd_print_symbol_type how) | |
252b5132 | 1336 | { |
217aa764 | 1337 | FILE *file = filep; |
252b5132 RH |
1338 | switch (how) |
1339 | { | |
1340 | case bfd_print_symbol_name: | |
1341 | fprintf (file, "%s", symbol->name); | |
1342 | break; | |
1343 | case bfd_print_symbol_more: | |
1344 | fprintf (file, "elf "); | |
60b89a18 | 1345 | bfd_fprintf_vma (abfd, file, symbol->value); |
0af1713e | 1346 | fprintf (file, " %lx", (unsigned long) symbol->flags); |
252b5132 RH |
1347 | break; |
1348 | case bfd_print_symbol_all: | |
1349 | { | |
4e8a9624 AM |
1350 | const char *section_name; |
1351 | const char *name = NULL; | |
9c5bfbb7 | 1352 | const struct elf_backend_data *bed; |
7a13edea | 1353 | unsigned char st_other; |
dbb410c3 | 1354 | bfd_vma val; |
c044fabd | 1355 | |
252b5132 | 1356 | section_name = symbol->section ? symbol->section->name : "(*none*)"; |
587ff49e RH |
1357 | |
1358 | bed = get_elf_backend_data (abfd); | |
1359 | if (bed->elf_backend_print_symbol_all) | |
c044fabd | 1360 | name = (*bed->elf_backend_print_symbol_all) (abfd, filep, symbol); |
587ff49e RH |
1361 | |
1362 | if (name == NULL) | |
1363 | { | |
7ee38065 | 1364 | name = symbol->name; |
217aa764 | 1365 | bfd_print_symbol_vandf (abfd, file, symbol); |
587ff49e RH |
1366 | } |
1367 | ||
252b5132 RH |
1368 | fprintf (file, " %s\t", section_name); |
1369 | /* Print the "other" value for a symbol. For common symbols, | |
1370 | we've already printed the size; now print the alignment. | |
1371 | For other symbols, we have no specified alignment, and | |
1372 | we've printed the address; now print the size. */ | |
dcf6c779 | 1373 | if (symbol->section && bfd_is_com_section (symbol->section)) |
dbb410c3 AM |
1374 | val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_value; |
1375 | else | |
1376 | val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_size; | |
1377 | bfd_fprintf_vma (abfd, file, val); | |
252b5132 RH |
1378 | |
1379 | /* If we have version information, print it. */ | |
1380 | if (elf_tdata (abfd)->dynversym_section != 0 | |
1381 | && (elf_tdata (abfd)->dynverdef_section != 0 | |
1382 | || elf_tdata (abfd)->dynverref_section != 0)) | |
1383 | { | |
1384 | unsigned int vernum; | |
1385 | const char *version_string; | |
1386 | ||
1387 | vernum = ((elf_symbol_type *) symbol)->version & VERSYM_VERSION; | |
1388 | ||
1389 | if (vernum == 0) | |
1390 | version_string = ""; | |
1391 | else if (vernum == 1) | |
1392 | version_string = "Base"; | |
1393 | else if (vernum <= elf_tdata (abfd)->cverdefs) | |
1394 | version_string = | |
1395 | elf_tdata (abfd)->verdef[vernum - 1].vd_nodename; | |
1396 | else | |
1397 | { | |
1398 | Elf_Internal_Verneed *t; | |
1399 | ||
1400 | version_string = ""; | |
1401 | for (t = elf_tdata (abfd)->verref; | |
1402 | t != NULL; | |
1403 | t = t->vn_nextref) | |
1404 | { | |
1405 | Elf_Internal_Vernaux *a; | |
1406 | ||
1407 | for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) | |
1408 | { | |
1409 | if (a->vna_other == vernum) | |
1410 | { | |
1411 | version_string = a->vna_nodename; | |
1412 | break; | |
1413 | } | |
1414 | } | |
1415 | } | |
1416 | } | |
1417 | ||
1418 | if ((((elf_symbol_type *) symbol)->version & VERSYM_HIDDEN) == 0) | |
1419 | fprintf (file, " %-11s", version_string); | |
1420 | else | |
1421 | { | |
1422 | int i; | |
1423 | ||
1424 | fprintf (file, " (%s)", version_string); | |
1425 | for (i = 10 - strlen (version_string); i > 0; --i) | |
1426 | putc (' ', file); | |
1427 | } | |
1428 | } | |
1429 | ||
1430 | /* If the st_other field is not zero, print it. */ | |
7a13edea | 1431 | st_other = ((elf_symbol_type *) symbol)->internal_elf_sym.st_other; |
c044fabd | 1432 | |
7a13edea NC |
1433 | switch (st_other) |
1434 | { | |
1435 | case 0: break; | |
1436 | case STV_INTERNAL: fprintf (file, " .internal"); break; | |
1437 | case STV_HIDDEN: fprintf (file, " .hidden"); break; | |
1438 | case STV_PROTECTED: fprintf (file, " .protected"); break; | |
1439 | default: | |
1440 | /* Some other non-defined flags are also present, so print | |
1441 | everything hex. */ | |
1442 | fprintf (file, " 0x%02x", (unsigned int) st_other); | |
1443 | } | |
252b5132 | 1444 | |
587ff49e | 1445 | fprintf (file, " %s", name); |
252b5132 RH |
1446 | } |
1447 | break; | |
1448 | } | |
1449 | } | |
252b5132 | 1450 | |
252b5132 RH |
1451 | /* Allocate an ELF string table--force the first byte to be zero. */ |
1452 | ||
1453 | struct bfd_strtab_hash * | |
217aa764 | 1454 | _bfd_elf_stringtab_init (void) |
252b5132 RH |
1455 | { |
1456 | struct bfd_strtab_hash *ret; | |
1457 | ||
1458 | ret = _bfd_stringtab_init (); | |
1459 | if (ret != NULL) | |
1460 | { | |
1461 | bfd_size_type loc; | |
1462 | ||
b34976b6 | 1463 | loc = _bfd_stringtab_add (ret, "", TRUE, FALSE); |
252b5132 RH |
1464 | BFD_ASSERT (loc == 0 || loc == (bfd_size_type) -1); |
1465 | if (loc == (bfd_size_type) -1) | |
1466 | { | |
1467 | _bfd_stringtab_free (ret); | |
1468 | ret = NULL; | |
1469 | } | |
1470 | } | |
1471 | return ret; | |
1472 | } | |
1473 | \f | |
1474 | /* ELF .o/exec file reading */ | |
1475 | ||
c044fabd | 1476 | /* Create a new bfd section from an ELF section header. */ |
252b5132 | 1477 | |
b34976b6 | 1478 | bfd_boolean |
217aa764 | 1479 | bfd_section_from_shdr (bfd *abfd, unsigned int shindex) |
252b5132 | 1480 | { |
4fbb74a6 AM |
1481 | Elf_Internal_Shdr *hdr; |
1482 | Elf_Internal_Ehdr *ehdr; | |
1483 | const struct elf_backend_data *bed; | |
90937f86 | 1484 | const char *name; |
252b5132 | 1485 | |
4fbb74a6 AM |
1486 | if (shindex >= elf_numsections (abfd)) |
1487 | return FALSE; | |
1488 | ||
1489 | hdr = elf_elfsections (abfd)[shindex]; | |
1490 | ehdr = elf_elfheader (abfd); | |
1491 | name = bfd_elf_string_from_elf_section (abfd, ehdr->e_shstrndx, | |
1b3a8575 | 1492 | hdr->sh_name); |
933d961a JJ |
1493 | if (name == NULL) |
1494 | return FALSE; | |
252b5132 | 1495 | |
4fbb74a6 | 1496 | bed = get_elf_backend_data (abfd); |
252b5132 RH |
1497 | switch (hdr->sh_type) |
1498 | { | |
1499 | case SHT_NULL: | |
1500 | /* Inactive section. Throw it away. */ | |
b34976b6 | 1501 | return TRUE; |
252b5132 RH |
1502 | |
1503 | case SHT_PROGBITS: /* Normal section with contents. */ | |
252b5132 RH |
1504 | case SHT_NOBITS: /* .bss section. */ |
1505 | case SHT_HASH: /* .hash section. */ | |
1506 | case SHT_NOTE: /* .note section. */ | |
25e27870 L |
1507 | case SHT_INIT_ARRAY: /* .init_array section. */ |
1508 | case SHT_FINI_ARRAY: /* .fini_array section. */ | |
1509 | case SHT_PREINIT_ARRAY: /* .preinit_array section. */ | |
7f1204bb | 1510 | case SHT_GNU_LIBLIST: /* .gnu.liblist section. */ |
fdc90cb4 | 1511 | case SHT_GNU_HASH: /* .gnu.hash section. */ |
6dc132d9 | 1512 | return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); |
252b5132 | 1513 | |
797fc050 | 1514 | case SHT_DYNAMIC: /* Dynamic linking information. */ |
6dc132d9 | 1515 | if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex)) |
b34976b6 | 1516 | return FALSE; |
8e0ed13f NC |
1517 | if (hdr->sh_link > elf_numsections (abfd) |
1518 | || elf_elfsections (abfd)[hdr->sh_link] == NULL) | |
1519 | return FALSE; | |
797fc050 AM |
1520 | if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_STRTAB) |
1521 | { | |
1522 | Elf_Internal_Shdr *dynsymhdr; | |
1523 | ||
1524 | /* The shared libraries distributed with hpux11 have a bogus | |
1525 | sh_link field for the ".dynamic" section. Find the | |
1526 | string table for the ".dynsym" section instead. */ | |
1527 | if (elf_dynsymtab (abfd) != 0) | |
1528 | { | |
1529 | dynsymhdr = elf_elfsections (abfd)[elf_dynsymtab (abfd)]; | |
1530 | hdr->sh_link = dynsymhdr->sh_link; | |
1531 | } | |
1532 | else | |
1533 | { | |
1534 | unsigned int i, num_sec; | |
1535 | ||
1536 | num_sec = elf_numsections (abfd); | |
1537 | for (i = 1; i < num_sec; i++) | |
1538 | { | |
1539 | dynsymhdr = elf_elfsections (abfd)[i]; | |
1540 | if (dynsymhdr->sh_type == SHT_DYNSYM) | |
1541 | { | |
1542 | hdr->sh_link = dynsymhdr->sh_link; | |
1543 | break; | |
1544 | } | |
1545 | } | |
1546 | } | |
1547 | } | |
1548 | break; | |
1549 | ||
252b5132 RH |
1550 | case SHT_SYMTAB: /* A symbol table */ |
1551 | if (elf_onesymtab (abfd) == shindex) | |
b34976b6 | 1552 | return TRUE; |
252b5132 | 1553 | |
a50b2160 JJ |
1554 | if (hdr->sh_entsize != bed->s->sizeof_sym) |
1555 | return FALSE; | |
3337c1e5 AM |
1556 | if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size) |
1557 | return FALSE; | |
252b5132 RH |
1558 | BFD_ASSERT (elf_onesymtab (abfd) == 0); |
1559 | elf_onesymtab (abfd) = shindex; | |
1560 | elf_tdata (abfd)->symtab_hdr = *hdr; | |
1561 | elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->symtab_hdr; | |
1562 | abfd->flags |= HAS_SYMS; | |
1563 | ||
1564 | /* Sometimes a shared object will map in the symbol table. If | |
08a40648 AM |
1565 | SHF_ALLOC is set, and this is a shared object, then we also |
1566 | treat this section as a BFD section. We can not base the | |
1567 | decision purely on SHF_ALLOC, because that flag is sometimes | |
1568 | set in a relocatable object file, which would confuse the | |
1569 | linker. */ | |
252b5132 RH |
1570 | if ((hdr->sh_flags & SHF_ALLOC) != 0 |
1571 | && (abfd->flags & DYNAMIC) != 0 | |
6dc132d9 L |
1572 | && ! _bfd_elf_make_section_from_shdr (abfd, hdr, name, |
1573 | shindex)) | |
b34976b6 | 1574 | return FALSE; |
252b5132 | 1575 | |
1b3a8575 AM |
1576 | /* Go looking for SHT_SYMTAB_SHNDX too, since if there is one we |
1577 | can't read symbols without that section loaded as well. It | |
1578 | is most likely specified by the next section header. */ | |
1579 | if (elf_elfsections (abfd)[elf_symtab_shndx (abfd)]->sh_link != shindex) | |
1580 | { | |
1581 | unsigned int i, num_sec; | |
1582 | ||
1583 | num_sec = elf_numsections (abfd); | |
1584 | for (i = shindex + 1; i < num_sec; i++) | |
1585 | { | |
1586 | Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i]; | |
1587 | if (hdr2->sh_type == SHT_SYMTAB_SHNDX | |
1588 | && hdr2->sh_link == shindex) | |
1589 | break; | |
1590 | } | |
1591 | if (i == num_sec) | |
1592 | for (i = 1; i < shindex; i++) | |
1593 | { | |
1594 | Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i]; | |
1595 | if (hdr2->sh_type == SHT_SYMTAB_SHNDX | |
1596 | && hdr2->sh_link == shindex) | |
1597 | break; | |
1598 | } | |
1599 | if (i != shindex) | |
1600 | return bfd_section_from_shdr (abfd, i); | |
1601 | } | |
b34976b6 | 1602 | return TRUE; |
252b5132 RH |
1603 | |
1604 | case SHT_DYNSYM: /* A dynamic symbol table */ | |
1605 | if (elf_dynsymtab (abfd) == shindex) | |
b34976b6 | 1606 | return TRUE; |
252b5132 | 1607 | |
a50b2160 JJ |
1608 | if (hdr->sh_entsize != bed->s->sizeof_sym) |
1609 | return FALSE; | |
252b5132 RH |
1610 | BFD_ASSERT (elf_dynsymtab (abfd) == 0); |
1611 | elf_dynsymtab (abfd) = shindex; | |
1612 | elf_tdata (abfd)->dynsymtab_hdr = *hdr; | |
1613 | elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->dynsymtab_hdr; | |
1614 | abfd->flags |= HAS_SYMS; | |
1615 | ||
1616 | /* Besides being a symbol table, we also treat this as a regular | |
1617 | section, so that objcopy can handle it. */ | |
6dc132d9 | 1618 | return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); |
252b5132 | 1619 | |
9ad5cbcf AM |
1620 | case SHT_SYMTAB_SHNDX: /* Symbol section indices when >64k sections */ |
1621 | if (elf_symtab_shndx (abfd) == shindex) | |
b34976b6 | 1622 | return TRUE; |
9ad5cbcf | 1623 | |
1b3a8575 | 1624 | BFD_ASSERT (elf_symtab_shndx (abfd) == 0); |
9ad5cbcf AM |
1625 | elf_symtab_shndx (abfd) = shindex; |
1626 | elf_tdata (abfd)->symtab_shndx_hdr = *hdr; | |
1627 | elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->symtab_shndx_hdr; | |
b34976b6 | 1628 | return TRUE; |
9ad5cbcf | 1629 | |
252b5132 RH |
1630 | case SHT_STRTAB: /* A string table */ |
1631 | if (hdr->bfd_section != NULL) | |
b34976b6 | 1632 | return TRUE; |
252b5132 RH |
1633 | if (ehdr->e_shstrndx == shindex) |
1634 | { | |
1635 | elf_tdata (abfd)->shstrtab_hdr = *hdr; | |
1636 | elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->shstrtab_hdr; | |
b34976b6 | 1637 | return TRUE; |
252b5132 | 1638 | } |
1b3a8575 AM |
1639 | if (elf_elfsections (abfd)[elf_onesymtab (abfd)]->sh_link == shindex) |
1640 | { | |
1641 | symtab_strtab: | |
1642 | elf_tdata (abfd)->strtab_hdr = *hdr; | |
1643 | elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->strtab_hdr; | |
1644 | return TRUE; | |
1645 | } | |
1646 | if (elf_elfsections (abfd)[elf_dynsymtab (abfd)]->sh_link == shindex) | |
1647 | { | |
1648 | dynsymtab_strtab: | |
1649 | elf_tdata (abfd)->dynstrtab_hdr = *hdr; | |
1650 | hdr = &elf_tdata (abfd)->dynstrtab_hdr; | |
1651 | elf_elfsections (abfd)[shindex] = hdr; | |
1652 | /* We also treat this as a regular section, so that objcopy | |
1653 | can handle it. */ | |
6dc132d9 L |
1654 | return _bfd_elf_make_section_from_shdr (abfd, hdr, name, |
1655 | shindex); | |
1b3a8575 | 1656 | } |
252b5132 | 1657 | |
1b3a8575 AM |
1658 | /* If the string table isn't one of the above, then treat it as a |
1659 | regular section. We need to scan all the headers to be sure, | |
1660 | just in case this strtab section appeared before the above. */ | |
1661 | if (elf_onesymtab (abfd) == 0 || elf_dynsymtab (abfd) == 0) | |
1662 | { | |
1663 | unsigned int i, num_sec; | |
252b5132 | 1664 | |
1b3a8575 AM |
1665 | num_sec = elf_numsections (abfd); |
1666 | for (i = 1; i < num_sec; i++) | |
1667 | { | |
1668 | Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i]; | |
1669 | if (hdr2->sh_link == shindex) | |
1670 | { | |
933d961a JJ |
1671 | /* Prevent endless recursion on broken objects. */ |
1672 | if (i == shindex) | |
1673 | return FALSE; | |
1b3a8575 AM |
1674 | if (! bfd_section_from_shdr (abfd, i)) |
1675 | return FALSE; | |
1676 | if (elf_onesymtab (abfd) == i) | |
1677 | goto symtab_strtab; | |
1678 | if (elf_dynsymtab (abfd) == i) | |
1679 | goto dynsymtab_strtab; | |
1680 | } | |
1681 | } | |
1682 | } | |
6dc132d9 | 1683 | return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); |
252b5132 RH |
1684 | |
1685 | case SHT_REL: | |
1686 | case SHT_RELA: | |
1687 | /* *These* do a lot of work -- but build no sections! */ | |
1688 | { | |
1689 | asection *target_sect; | |
1690 | Elf_Internal_Shdr *hdr2; | |
9ad5cbcf | 1691 | unsigned int num_sec = elf_numsections (abfd); |
252b5132 | 1692 | |
aa2ca951 JJ |
1693 | if (hdr->sh_entsize |
1694 | != (bfd_size_type) (hdr->sh_type == SHT_REL | |
a50b2160 JJ |
1695 | ? bed->s->sizeof_rel : bed->s->sizeof_rela)) |
1696 | return FALSE; | |
1697 | ||
03ae5f59 | 1698 | /* Check for a bogus link to avoid crashing. */ |
4fbb74a6 | 1699 | if (hdr->sh_link >= num_sec) |
03ae5f59 ILT |
1700 | { |
1701 | ((*_bfd_error_handler) | |
d003868e AM |
1702 | (_("%B: invalid link %lu for reloc section %s (index %u)"), |
1703 | abfd, hdr->sh_link, name, shindex)); | |
6dc132d9 L |
1704 | return _bfd_elf_make_section_from_shdr (abfd, hdr, name, |
1705 | shindex); | |
03ae5f59 ILT |
1706 | } |
1707 | ||
252b5132 RH |
1708 | /* For some incomprehensible reason Oracle distributes |
1709 | libraries for Solaris in which some of the objects have | |
1710 | bogus sh_link fields. It would be nice if we could just | |
1711 | reject them, but, unfortunately, some people need to use | |
1712 | them. We scan through the section headers; if we find only | |
1713 | one suitable symbol table, we clobber the sh_link to point | |
83b89087 L |
1714 | to it. I hope this doesn't break anything. |
1715 | ||
1716 | Don't do it on executable nor shared library. */ | |
1717 | if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0 | |
1718 | && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_SYMTAB | |
252b5132 RH |
1719 | && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_DYNSYM) |
1720 | { | |
9ad5cbcf | 1721 | unsigned int scan; |
252b5132 RH |
1722 | int found; |
1723 | ||
1724 | found = 0; | |
9ad5cbcf | 1725 | for (scan = 1; scan < num_sec; scan++) |
252b5132 RH |
1726 | { |
1727 | if (elf_elfsections (abfd)[scan]->sh_type == SHT_SYMTAB | |
1728 | || elf_elfsections (abfd)[scan]->sh_type == SHT_DYNSYM) | |
1729 | { | |
1730 | if (found != 0) | |
1731 | { | |
1732 | found = 0; | |
1733 | break; | |
1734 | } | |
1735 | found = scan; | |
1736 | } | |
1737 | } | |
1738 | if (found != 0) | |
1739 | hdr->sh_link = found; | |
1740 | } | |
1741 | ||
1742 | /* Get the symbol table. */ | |
1b3a8575 AM |
1743 | if ((elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_SYMTAB |
1744 | || elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_DYNSYM) | |
252b5132 | 1745 | && ! bfd_section_from_shdr (abfd, hdr->sh_link)) |
b34976b6 | 1746 | return FALSE; |
252b5132 RH |
1747 | |
1748 | /* If this reloc section does not use the main symbol table we | |
1749 | don't treat it as a reloc section. BFD can't adequately | |
1750 | represent such a section, so at least for now, we don't | |
c044fabd | 1751 | try. We just present it as a normal section. We also |
60bcf0fa | 1752 | can't use it as a reloc section if it points to the null |
83b89087 L |
1753 | section, an invalid section, another reloc section, or its |
1754 | sh_link points to the null section. */ | |
185ef66d | 1755 | if (hdr->sh_link != elf_onesymtab (abfd) |
83b89087 | 1756 | || hdr->sh_link == SHN_UNDEF |
185ef66d | 1757 | || hdr->sh_info == SHN_UNDEF |
185ef66d AM |
1758 | || hdr->sh_info >= num_sec |
1759 | || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_REL | |
1760 | || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_RELA) | |
6dc132d9 L |
1761 | return _bfd_elf_make_section_from_shdr (abfd, hdr, name, |
1762 | shindex); | |
252b5132 RH |
1763 | |
1764 | if (! bfd_section_from_shdr (abfd, hdr->sh_info)) | |
b34976b6 | 1765 | return FALSE; |
252b5132 RH |
1766 | target_sect = bfd_section_from_elf_index (abfd, hdr->sh_info); |
1767 | if (target_sect == NULL) | |
b34976b6 | 1768 | return FALSE; |
252b5132 RH |
1769 | |
1770 | if ((target_sect->flags & SEC_RELOC) == 0 | |
1771 | || target_sect->reloc_count == 0) | |
1772 | hdr2 = &elf_section_data (target_sect)->rel_hdr; | |
1773 | else | |
1774 | { | |
dc810e39 | 1775 | bfd_size_type amt; |
252b5132 | 1776 | BFD_ASSERT (elf_section_data (target_sect)->rel_hdr2 == NULL); |
dc810e39 | 1777 | amt = sizeof (*hdr2); |
217aa764 | 1778 | hdr2 = bfd_alloc (abfd, amt); |
14b1c01e AM |
1779 | if (hdr2 == NULL) |
1780 | return FALSE; | |
252b5132 RH |
1781 | elf_section_data (target_sect)->rel_hdr2 = hdr2; |
1782 | } | |
1783 | *hdr2 = *hdr; | |
1784 | elf_elfsections (abfd)[shindex] = hdr2; | |
d9bc7a44 | 1785 | target_sect->reloc_count += NUM_SHDR_ENTRIES (hdr); |
252b5132 RH |
1786 | target_sect->flags |= SEC_RELOC; |
1787 | target_sect->relocation = NULL; | |
1788 | target_sect->rel_filepos = hdr->sh_offset; | |
bf572ba0 MM |
1789 | /* In the section to which the relocations apply, mark whether |
1790 | its relocations are of the REL or RELA variety. */ | |
72730e0c | 1791 | if (hdr->sh_size != 0) |
68bfbfcc | 1792 | target_sect->use_rela_p = hdr->sh_type == SHT_RELA; |
252b5132 | 1793 | abfd->flags |= HAS_RELOC; |
b34976b6 | 1794 | return TRUE; |
252b5132 | 1795 | } |
252b5132 RH |
1796 | |
1797 | case SHT_GNU_verdef: | |
1798 | elf_dynverdef (abfd) = shindex; | |
1799 | elf_tdata (abfd)->dynverdef_hdr = *hdr; | |
6dc132d9 | 1800 | return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); |
252b5132 RH |
1801 | |
1802 | case SHT_GNU_versym: | |
a50b2160 JJ |
1803 | if (hdr->sh_entsize != sizeof (Elf_External_Versym)) |
1804 | return FALSE; | |
252b5132 RH |
1805 | elf_dynversym (abfd) = shindex; |
1806 | elf_tdata (abfd)->dynversym_hdr = *hdr; | |
6dc132d9 | 1807 | return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); |
252b5132 RH |
1808 | |
1809 | case SHT_GNU_verneed: | |
1810 | elf_dynverref (abfd) = shindex; | |
1811 | elf_tdata (abfd)->dynverref_hdr = *hdr; | |
6dc132d9 | 1812 | return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); |
252b5132 RH |
1813 | |
1814 | case SHT_SHLIB: | |
b34976b6 | 1815 | return TRUE; |
252b5132 | 1816 | |
dbb410c3 | 1817 | case SHT_GROUP: |
1783205a | 1818 | if (! IS_VALID_GROUP_SECTION_HEADER (hdr)) |
a50b2160 | 1819 | return FALSE; |
6dc132d9 | 1820 | if (!_bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex)) |
b34976b6 | 1821 | return FALSE; |
dbb410c3 AM |
1822 | if (hdr->contents != NULL) |
1823 | { | |
1824 | Elf_Internal_Group *idx = (Elf_Internal_Group *) hdr->contents; | |
1783205a | 1825 | unsigned int n_elt = hdr->sh_size / GRP_ENTRY_SIZE; |
dbb410c3 AM |
1826 | asection *s; |
1827 | ||
b885599b AM |
1828 | if (idx->flags & GRP_COMDAT) |
1829 | hdr->bfd_section->flags | |
1830 | |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD; | |
1831 | ||
45c5e9ed L |
1832 | /* We try to keep the same section order as it comes in. */ |
1833 | idx += n_elt; | |
dbb410c3 | 1834 | while (--n_elt != 0) |
1783205a NC |
1835 | { |
1836 | --idx; | |
1837 | ||
1838 | if (idx->shdr != NULL | |
1839 | && (s = idx->shdr->bfd_section) != NULL | |
1840 | && elf_next_in_group (s) != NULL) | |
1841 | { | |
1842 | elf_next_in_group (hdr->bfd_section) = s; | |
1843 | break; | |
1844 | } | |
1845 | } | |
dbb410c3 AM |
1846 | } |
1847 | break; | |
1848 | ||
252b5132 | 1849 | default: |
104d59d1 JM |
1850 | /* Possibly an attributes section. */ |
1851 | if (hdr->sh_type == SHT_GNU_ATTRIBUTES | |
1852 | || hdr->sh_type == bed->obj_attrs_section_type) | |
1853 | { | |
1854 | if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex)) | |
1855 | return FALSE; | |
1856 | _bfd_elf_parse_attributes (abfd, hdr); | |
1857 | return TRUE; | |
1858 | } | |
1859 | ||
252b5132 | 1860 | /* Check for any processor-specific section types. */ |
3eb70a79 L |
1861 | if (bed->elf_backend_section_from_shdr (abfd, hdr, name, shindex)) |
1862 | return TRUE; | |
1863 | ||
1864 | if (hdr->sh_type >= SHT_LOUSER && hdr->sh_type <= SHT_HIUSER) | |
1865 | { | |
1866 | if ((hdr->sh_flags & SHF_ALLOC) != 0) | |
1867 | /* FIXME: How to properly handle allocated section reserved | |
1868 | for applications? */ | |
1869 | (*_bfd_error_handler) | |
1870 | (_("%B: don't know how to handle allocated, application " | |
1871 | "specific section `%s' [0x%8x]"), | |
1872 | abfd, name, hdr->sh_type); | |
1873 | else | |
1874 | /* Allow sections reserved for applications. */ | |
1875 | return _bfd_elf_make_section_from_shdr (abfd, hdr, name, | |
1876 | shindex); | |
1877 | } | |
1878 | else if (hdr->sh_type >= SHT_LOPROC | |
1879 | && hdr->sh_type <= SHT_HIPROC) | |
1880 | /* FIXME: We should handle this section. */ | |
1881 | (*_bfd_error_handler) | |
1882 | (_("%B: don't know how to handle processor specific section " | |
1883 | "`%s' [0x%8x]"), | |
1884 | abfd, name, hdr->sh_type); | |
1885 | else if (hdr->sh_type >= SHT_LOOS && hdr->sh_type <= SHT_HIOS) | |
ff15b240 NC |
1886 | { |
1887 | /* Unrecognised OS-specific sections. */ | |
1888 | if ((hdr->sh_flags & SHF_OS_NONCONFORMING) != 0) | |
1889 | /* SHF_OS_NONCONFORMING indicates that special knowledge is | |
08a40648 | 1890 | required to correctly process the section and the file should |
ff15b240 NC |
1891 | be rejected with an error message. */ |
1892 | (*_bfd_error_handler) | |
1893 | (_("%B: don't know how to handle OS specific section " | |
1894 | "`%s' [0x%8x]"), | |
1895 | abfd, name, hdr->sh_type); | |
1896 | else | |
1897 | /* Otherwise it should be processed. */ | |
1898 | return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); | |
1899 | } | |
3eb70a79 L |
1900 | else |
1901 | /* FIXME: We should handle this section. */ | |
1902 | (*_bfd_error_handler) | |
1903 | (_("%B: don't know how to handle section `%s' [0x%8x]"), | |
1904 | abfd, name, hdr->sh_type); | |
1905 | ||
1906 | return FALSE; | |
252b5132 RH |
1907 | } |
1908 | ||
b34976b6 | 1909 | return TRUE; |
252b5132 RH |
1910 | } |
1911 | ||
87d72d41 | 1912 | /* Return the local symbol specified by ABFD, R_SYMNDX. */ |
ec338859 | 1913 | |
87d72d41 AM |
1914 | Elf_Internal_Sym * |
1915 | bfd_sym_from_r_symndx (struct sym_cache *cache, | |
1916 | bfd *abfd, | |
1917 | unsigned long r_symndx) | |
ec338859 | 1918 | { |
ec338859 AM |
1919 | unsigned int ent = r_symndx % LOCAL_SYM_CACHE_SIZE; |
1920 | ||
a5d1b3b5 AM |
1921 | if (cache->abfd != abfd || cache->indx[ent] != r_symndx) |
1922 | { | |
1923 | Elf_Internal_Shdr *symtab_hdr; | |
1924 | unsigned char esym[sizeof (Elf64_External_Sym)]; | |
1925 | Elf_External_Sym_Shndx eshndx; | |
ec338859 | 1926 | |
a5d1b3b5 AM |
1927 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; |
1928 | if (bfd_elf_get_elf_syms (abfd, symtab_hdr, 1, r_symndx, | |
87d72d41 | 1929 | &cache->sym[ent], esym, &eshndx) == NULL) |
a5d1b3b5 | 1930 | return NULL; |
9ad5cbcf | 1931 | |
a5d1b3b5 AM |
1932 | if (cache->abfd != abfd) |
1933 | { | |
1934 | memset (cache->indx, -1, sizeof (cache->indx)); | |
1935 | cache->abfd = abfd; | |
1936 | } | |
1937 | cache->indx[ent] = r_symndx; | |
ec338859 | 1938 | } |
a5d1b3b5 | 1939 | |
87d72d41 | 1940 | return &cache->sym[ent]; |
ec338859 AM |
1941 | } |
1942 | ||
252b5132 RH |
1943 | /* Given an ELF section number, retrieve the corresponding BFD |
1944 | section. */ | |
1945 | ||
1946 | asection * | |
217aa764 | 1947 | bfd_section_from_elf_index (bfd *abfd, unsigned int index) |
252b5132 | 1948 | { |
9ad5cbcf | 1949 | if (index >= elf_numsections (abfd)) |
252b5132 RH |
1950 | return NULL; |
1951 | return elf_elfsections (abfd)[index]->bfd_section; | |
1952 | } | |
1953 | ||
b35d266b | 1954 | static const struct bfd_elf_special_section special_sections_b[] = |
2f89ff8d | 1955 | { |
0112cd26 NC |
1956 | { STRING_COMMA_LEN (".bss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE }, |
1957 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
1958 | }; |
1959 | ||
b35d266b | 1960 | static const struct bfd_elf_special_section special_sections_c[] = |
7f4d3958 | 1961 | { |
0112cd26 NC |
1962 | { STRING_COMMA_LEN (".comment"), 0, SHT_PROGBITS, 0 }, |
1963 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
1964 | }; |
1965 | ||
b35d266b | 1966 | static const struct bfd_elf_special_section special_sections_d[] = |
7f4d3958 | 1967 | { |
0112cd26 NC |
1968 | { STRING_COMMA_LEN (".data"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE }, |
1969 | { STRING_COMMA_LEN (".data1"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE }, | |
1970 | { STRING_COMMA_LEN (".debug"), 0, SHT_PROGBITS, 0 }, | |
1971 | { STRING_COMMA_LEN (".debug_line"), 0, SHT_PROGBITS, 0 }, | |
1972 | { STRING_COMMA_LEN (".debug_info"), 0, SHT_PROGBITS, 0 }, | |
1973 | { STRING_COMMA_LEN (".debug_abbrev"), 0, SHT_PROGBITS, 0 }, | |
1974 | { STRING_COMMA_LEN (".debug_aranges"), 0, SHT_PROGBITS, 0 }, | |
1975 | { STRING_COMMA_LEN (".dynamic"), 0, SHT_DYNAMIC, SHF_ALLOC }, | |
1976 | { STRING_COMMA_LEN (".dynstr"), 0, SHT_STRTAB, SHF_ALLOC }, | |
1977 | { STRING_COMMA_LEN (".dynsym"), 0, SHT_DYNSYM, SHF_ALLOC }, | |
1978 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
1979 | }; |
1980 | ||
b35d266b | 1981 | static const struct bfd_elf_special_section special_sections_f[] = |
7f4d3958 | 1982 | { |
0112cd26 NC |
1983 | { STRING_COMMA_LEN (".fini"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR }, |
1984 | { STRING_COMMA_LEN (".fini_array"), 0, SHT_FINI_ARRAY, SHF_ALLOC + SHF_WRITE }, | |
1985 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
1986 | }; |
1987 | ||
b35d266b | 1988 | static const struct bfd_elf_special_section special_sections_g[] = |
7f4d3958 | 1989 | { |
0112cd26 NC |
1990 | { STRING_COMMA_LEN (".gnu.linkonce.b"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE }, |
1991 | { STRING_COMMA_LEN (".got"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE }, | |
1992 | { STRING_COMMA_LEN (".gnu.version"), 0, SHT_GNU_versym, 0 }, | |
1993 | { STRING_COMMA_LEN (".gnu.version_d"), 0, SHT_GNU_verdef, 0 }, | |
1994 | { STRING_COMMA_LEN (".gnu.version_r"), 0, SHT_GNU_verneed, 0 }, | |
1995 | { STRING_COMMA_LEN (".gnu.liblist"), 0, SHT_GNU_LIBLIST, SHF_ALLOC }, | |
1996 | { STRING_COMMA_LEN (".gnu.conflict"), 0, SHT_RELA, SHF_ALLOC }, | |
1997 | { STRING_COMMA_LEN (".gnu.hash"), 0, SHT_GNU_HASH, SHF_ALLOC }, | |
1998 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
1999 | }; |
2000 | ||
b35d266b | 2001 | static const struct bfd_elf_special_section special_sections_h[] = |
7f4d3958 | 2002 | { |
0112cd26 NC |
2003 | { STRING_COMMA_LEN (".hash"), 0, SHT_HASH, SHF_ALLOC }, |
2004 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2005 | }; |
2006 | ||
b35d266b | 2007 | static const struct bfd_elf_special_section special_sections_i[] = |
7f4d3958 | 2008 | { |
0112cd26 NC |
2009 | { STRING_COMMA_LEN (".init"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR }, |
2010 | { STRING_COMMA_LEN (".init_array"), 0, SHT_INIT_ARRAY, SHF_ALLOC + SHF_WRITE }, | |
2011 | { STRING_COMMA_LEN (".interp"), 0, SHT_PROGBITS, 0 }, | |
2012 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2013 | }; |
2014 | ||
b35d266b | 2015 | static const struct bfd_elf_special_section special_sections_l[] = |
7f4d3958 | 2016 | { |
0112cd26 NC |
2017 | { STRING_COMMA_LEN (".line"), 0, SHT_PROGBITS, 0 }, |
2018 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2019 | }; |
2020 | ||
b35d266b | 2021 | static const struct bfd_elf_special_section special_sections_n[] = |
7f4d3958 | 2022 | { |
0112cd26 NC |
2023 | { STRING_COMMA_LEN (".note.GNU-stack"), 0, SHT_PROGBITS, 0 }, |
2024 | { STRING_COMMA_LEN (".note"), -1, SHT_NOTE, 0 }, | |
2025 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2026 | }; |
2027 | ||
b35d266b | 2028 | static const struct bfd_elf_special_section special_sections_p[] = |
7f4d3958 | 2029 | { |
0112cd26 NC |
2030 | { STRING_COMMA_LEN (".preinit_array"), 0, SHT_PREINIT_ARRAY, SHF_ALLOC + SHF_WRITE }, |
2031 | { STRING_COMMA_LEN (".plt"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR }, | |
2032 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2033 | }; |
2034 | ||
b35d266b | 2035 | static const struct bfd_elf_special_section special_sections_r[] = |
7f4d3958 | 2036 | { |
0112cd26 NC |
2037 | { STRING_COMMA_LEN (".rodata"), -2, SHT_PROGBITS, SHF_ALLOC }, |
2038 | { STRING_COMMA_LEN (".rodata1"), 0, SHT_PROGBITS, SHF_ALLOC }, | |
2039 | { STRING_COMMA_LEN (".rela"), -1, SHT_RELA, 0 }, | |
2040 | { STRING_COMMA_LEN (".rel"), -1, SHT_REL, 0 }, | |
2041 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2042 | }; |
2043 | ||
b35d266b | 2044 | static const struct bfd_elf_special_section special_sections_s[] = |
7f4d3958 | 2045 | { |
0112cd26 NC |
2046 | { STRING_COMMA_LEN (".shstrtab"), 0, SHT_STRTAB, 0 }, |
2047 | { STRING_COMMA_LEN (".strtab"), 0, SHT_STRTAB, 0 }, | |
2048 | { STRING_COMMA_LEN (".symtab"), 0, SHT_SYMTAB, 0 }, | |
60ff4dc4 HPN |
2049 | /* See struct bfd_elf_special_section declaration for the semantics of |
2050 | this special case where .prefix_length != strlen (.prefix). */ | |
2051 | { ".stabstr", 5, 3, SHT_STRTAB, 0 }, | |
0112cd26 | 2052 | { NULL, 0, 0, 0, 0 } |
2f89ff8d L |
2053 | }; |
2054 | ||
b35d266b | 2055 | static const struct bfd_elf_special_section special_sections_t[] = |
7f4d3958 | 2056 | { |
0112cd26 NC |
2057 | { STRING_COMMA_LEN (".text"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR }, |
2058 | { STRING_COMMA_LEN (".tbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS }, | |
2059 | { STRING_COMMA_LEN (".tdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS }, | |
2060 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2061 | }; |
2062 | ||
1b315056 CS |
2063 | static const struct bfd_elf_special_section special_sections_z[] = |
2064 | { | |
2065 | { STRING_COMMA_LEN (".zdebug_line"), 0, SHT_PROGBITS, 0 }, | |
2066 | { STRING_COMMA_LEN (".zdebug_info"), 0, SHT_PROGBITS, 0 }, | |
2067 | { STRING_COMMA_LEN (".zdebug_abbrev"), 0, SHT_PROGBITS, 0 }, | |
2068 | { STRING_COMMA_LEN (".zdebug_aranges"), 0, SHT_PROGBITS, 0 }, | |
2069 | { NULL, 0, 0, 0, 0 } | |
2070 | }; | |
2071 | ||
b35d266b | 2072 | static const struct bfd_elf_special_section *special_sections[] = |
7f4d3958 | 2073 | { |
7f4d3958 | 2074 | special_sections_b, /* 'b' */ |
98ece1b3 | 2075 | special_sections_c, /* 'c' */ |
7f4d3958 L |
2076 | special_sections_d, /* 'd' */ |
2077 | NULL, /* 'e' */ | |
2078 | special_sections_f, /* 'f' */ | |
2079 | special_sections_g, /* 'g' */ | |
2080 | special_sections_h, /* 'h' */ | |
2081 | special_sections_i, /* 'i' */ | |
2082 | NULL, /* 'j' */ | |
2083 | NULL, /* 'k' */ | |
2084 | special_sections_l, /* 'l' */ | |
2085 | NULL, /* 'm' */ | |
2086 | special_sections_n, /* 'n' */ | |
2087 | NULL, /* 'o' */ | |
2088 | special_sections_p, /* 'p' */ | |
2089 | NULL, /* 'q' */ | |
2090 | special_sections_r, /* 'r' */ | |
2091 | special_sections_s, /* 's' */ | |
2092 | special_sections_t, /* 't' */ | |
1b315056 CS |
2093 | NULL, /* 'u' */ |
2094 | NULL, /* 'v' */ | |
2095 | NULL, /* 'w' */ | |
2096 | NULL, /* 'x' */ | |
2097 | NULL, /* 'y' */ | |
2098 | special_sections_z /* 'z' */ | |
7f4d3958 L |
2099 | }; |
2100 | ||
551b43fd AM |
2101 | const struct bfd_elf_special_section * |
2102 | _bfd_elf_get_special_section (const char *name, | |
2103 | const struct bfd_elf_special_section *spec, | |
2104 | unsigned int rela) | |
2f89ff8d L |
2105 | { |
2106 | int i; | |
7f4d3958 | 2107 | int len; |
7f4d3958 | 2108 | |
551b43fd | 2109 | len = strlen (name); |
7f4d3958 | 2110 | |
551b43fd | 2111 | for (i = 0; spec[i].prefix != NULL; i++) |
7dcb9820 AM |
2112 | { |
2113 | int suffix_len; | |
551b43fd | 2114 | int prefix_len = spec[i].prefix_length; |
7dcb9820 AM |
2115 | |
2116 | if (len < prefix_len) | |
2117 | continue; | |
551b43fd | 2118 | if (memcmp (name, spec[i].prefix, prefix_len) != 0) |
7dcb9820 AM |
2119 | continue; |
2120 | ||
551b43fd | 2121 | suffix_len = spec[i].suffix_length; |
7dcb9820 AM |
2122 | if (suffix_len <= 0) |
2123 | { | |
2124 | if (name[prefix_len] != 0) | |
2125 | { | |
2126 | if (suffix_len == 0) | |
2127 | continue; | |
2128 | if (name[prefix_len] != '.' | |
2129 | && (suffix_len == -2 | |
551b43fd | 2130 | || (rela && spec[i].type == SHT_REL))) |
7dcb9820 AM |
2131 | continue; |
2132 | } | |
2133 | } | |
2134 | else | |
2135 | { | |
2136 | if (len < prefix_len + suffix_len) | |
2137 | continue; | |
2138 | if (memcmp (name + len - suffix_len, | |
551b43fd | 2139 | spec[i].prefix + prefix_len, |
7dcb9820 AM |
2140 | suffix_len) != 0) |
2141 | continue; | |
2142 | } | |
551b43fd | 2143 | return &spec[i]; |
7dcb9820 | 2144 | } |
2f89ff8d L |
2145 | |
2146 | return NULL; | |
2147 | } | |
2148 | ||
7dcb9820 | 2149 | const struct bfd_elf_special_section * |
29ef7005 | 2150 | _bfd_elf_get_sec_type_attr (bfd *abfd, asection *sec) |
2f89ff8d | 2151 | { |
551b43fd AM |
2152 | int i; |
2153 | const struct bfd_elf_special_section *spec; | |
29ef7005 | 2154 | const struct elf_backend_data *bed; |
2f89ff8d L |
2155 | |
2156 | /* See if this is one of the special sections. */ | |
551b43fd AM |
2157 | if (sec->name == NULL) |
2158 | return NULL; | |
2f89ff8d | 2159 | |
29ef7005 L |
2160 | bed = get_elf_backend_data (abfd); |
2161 | spec = bed->special_sections; | |
2162 | if (spec) | |
2163 | { | |
2164 | spec = _bfd_elf_get_special_section (sec->name, | |
2165 | bed->special_sections, | |
2166 | sec->use_rela_p); | |
2167 | if (spec != NULL) | |
2168 | return spec; | |
2169 | } | |
2170 | ||
551b43fd AM |
2171 | if (sec->name[0] != '.') |
2172 | return NULL; | |
2f89ff8d | 2173 | |
551b43fd | 2174 | i = sec->name[1] - 'b'; |
1b315056 | 2175 | if (i < 0 || i > 'z' - 'b') |
551b43fd AM |
2176 | return NULL; |
2177 | ||
2178 | spec = special_sections[i]; | |
2f89ff8d | 2179 | |
551b43fd AM |
2180 | if (spec == NULL) |
2181 | return NULL; | |
2182 | ||
2183 | return _bfd_elf_get_special_section (sec->name, spec, sec->use_rela_p); | |
2f89ff8d L |
2184 | } |
2185 | ||
b34976b6 | 2186 | bfd_boolean |
217aa764 | 2187 | _bfd_elf_new_section_hook (bfd *abfd, asection *sec) |
252b5132 RH |
2188 | { |
2189 | struct bfd_elf_section_data *sdata; | |
551b43fd | 2190 | const struct elf_backend_data *bed; |
7dcb9820 | 2191 | const struct bfd_elf_special_section *ssect; |
252b5132 | 2192 | |
f0abc2a1 AM |
2193 | sdata = (struct bfd_elf_section_data *) sec->used_by_bfd; |
2194 | if (sdata == NULL) | |
2195 | { | |
217aa764 | 2196 | sdata = bfd_zalloc (abfd, sizeof (*sdata)); |
f0abc2a1 AM |
2197 | if (sdata == NULL) |
2198 | return FALSE; | |
217aa764 | 2199 | sec->used_by_bfd = sdata; |
f0abc2a1 | 2200 | } |
bf572ba0 | 2201 | |
551b43fd AM |
2202 | /* Indicate whether or not this section should use RELA relocations. */ |
2203 | bed = get_elf_backend_data (abfd); | |
2204 | sec->use_rela_p = bed->default_use_rela_p; | |
2205 | ||
e843e0f8 L |
2206 | /* When we read a file, we don't need to set ELF section type and |
2207 | flags. They will be overridden in _bfd_elf_make_section_from_shdr | |
2208 | anyway. We will set ELF section type and flags for all linker | |
2209 | created sections. If user specifies BFD section flags, we will | |
2210 | set ELF section type and flags based on BFD section flags in | |
2211 | elf_fake_sections. */ | |
2212 | if ((!sec->flags && abfd->direction != read_direction) | |
3496cb2a | 2213 | || (sec->flags & SEC_LINKER_CREATED) != 0) |
2f89ff8d | 2214 | { |
551b43fd | 2215 | ssect = (*bed->get_sec_type_attr) (abfd, sec); |
a31501e9 L |
2216 | if (ssect != NULL) |
2217 | { | |
2218 | elf_section_type (sec) = ssect->type; | |
2219 | elf_section_flags (sec) = ssect->attr; | |
2220 | } | |
2f89ff8d L |
2221 | } |
2222 | ||
f592407e | 2223 | return _bfd_generic_new_section_hook (abfd, sec); |
252b5132 RH |
2224 | } |
2225 | ||
2226 | /* Create a new bfd section from an ELF program header. | |
2227 | ||
2228 | Since program segments have no names, we generate a synthetic name | |
2229 | of the form segment<NUM>, where NUM is generally the index in the | |
2230 | program header table. For segments that are split (see below) we | |
2231 | generate the names segment<NUM>a and segment<NUM>b. | |
2232 | ||
2233 | Note that some program segments may have a file size that is different than | |
2234 | (less than) the memory size. All this means is that at execution the | |
2235 | system must allocate the amount of memory specified by the memory size, | |
2236 | but only initialize it with the first "file size" bytes read from the | |
2237 | file. This would occur for example, with program segments consisting | |
2238 | of combined data+bss. | |
2239 | ||
2240 | To handle the above situation, this routine generates TWO bfd sections | |
2241 | for the single program segment. The first has the length specified by | |
2242 | the file size of the segment, and the second has the length specified | |
2243 | by the difference between the two sizes. In effect, the segment is split | |
d5191d0c | 2244 | into its initialized and uninitialized parts. |
252b5132 RH |
2245 | |
2246 | */ | |
2247 | ||
b34976b6 | 2248 | bfd_boolean |
217aa764 AM |
2249 | _bfd_elf_make_section_from_phdr (bfd *abfd, |
2250 | Elf_Internal_Phdr *hdr, | |
2251 | int index, | |
2252 | const char *typename) | |
252b5132 RH |
2253 | { |
2254 | asection *newsect; | |
2255 | char *name; | |
2256 | char namebuf[64]; | |
d4c88bbb | 2257 | size_t len; |
252b5132 RH |
2258 | int split; |
2259 | ||
2260 | split = ((hdr->p_memsz > 0) | |
2261 | && (hdr->p_filesz > 0) | |
2262 | && (hdr->p_memsz > hdr->p_filesz)); | |
d5191d0c AM |
2263 | |
2264 | if (hdr->p_filesz > 0) | |
252b5132 | 2265 | { |
d5191d0c AM |
2266 | sprintf (namebuf, "%s%d%s", typename, index, split ? "a" : ""); |
2267 | len = strlen (namebuf) + 1; | |
2268 | name = bfd_alloc (abfd, len); | |
2269 | if (!name) | |
2270 | return FALSE; | |
2271 | memcpy (name, namebuf, len); | |
2272 | newsect = bfd_make_section (abfd, name); | |
2273 | if (newsect == NULL) | |
2274 | return FALSE; | |
2275 | newsect->vma = hdr->p_vaddr; | |
2276 | newsect->lma = hdr->p_paddr; | |
2277 | newsect->size = hdr->p_filesz; | |
2278 | newsect->filepos = hdr->p_offset; | |
2279 | newsect->flags |= SEC_HAS_CONTENTS; | |
2280 | newsect->alignment_power = bfd_log2 (hdr->p_align); | |
2281 | if (hdr->p_type == PT_LOAD) | |
252b5132 | 2282 | { |
d5191d0c AM |
2283 | newsect->flags |= SEC_ALLOC; |
2284 | newsect->flags |= SEC_LOAD; | |
2285 | if (hdr->p_flags & PF_X) | |
2286 | { | |
2287 | /* FIXME: all we known is that it has execute PERMISSION, | |
2288 | may be data. */ | |
2289 | newsect->flags |= SEC_CODE; | |
2290 | } | |
2291 | } | |
2292 | if (!(hdr->p_flags & PF_W)) | |
2293 | { | |
2294 | newsect->flags |= SEC_READONLY; | |
252b5132 | 2295 | } |
252b5132 RH |
2296 | } |
2297 | ||
d5191d0c | 2298 | if (hdr->p_memsz > hdr->p_filesz) |
252b5132 | 2299 | { |
d5191d0c AM |
2300 | bfd_vma align; |
2301 | ||
2302 | sprintf (namebuf, "%s%d%s", typename, index, split ? "b" : ""); | |
d4c88bbb | 2303 | len = strlen (namebuf) + 1; |
217aa764 | 2304 | name = bfd_alloc (abfd, len); |
252b5132 | 2305 | if (!name) |
b34976b6 | 2306 | return FALSE; |
d4c88bbb | 2307 | memcpy (name, namebuf, len); |
252b5132 RH |
2308 | newsect = bfd_make_section (abfd, name); |
2309 | if (newsect == NULL) | |
b34976b6 | 2310 | return FALSE; |
252b5132 RH |
2311 | newsect->vma = hdr->p_vaddr + hdr->p_filesz; |
2312 | newsect->lma = hdr->p_paddr + hdr->p_filesz; | |
eea6121a | 2313 | newsect->size = hdr->p_memsz - hdr->p_filesz; |
d5191d0c AM |
2314 | newsect->filepos = hdr->p_offset + hdr->p_filesz; |
2315 | align = newsect->vma & -newsect->vma; | |
2316 | if (align == 0 || align > hdr->p_align) | |
2317 | align = hdr->p_align; | |
2318 | newsect->alignment_power = bfd_log2 (align); | |
252b5132 RH |
2319 | if (hdr->p_type == PT_LOAD) |
2320 | { | |
d5191d0c AM |
2321 | /* Hack for gdb. Segments that have not been modified do |
2322 | not have their contents written to a core file, on the | |
2323 | assumption that a debugger can find the contents in the | |
2324 | executable. We flag this case by setting the fake | |
2325 | section size to zero. Note that "real" bss sections will | |
2326 | always have their contents dumped to the core file. */ | |
2327 | if (bfd_get_format (abfd) == bfd_core) | |
2328 | newsect->size = 0; | |
252b5132 RH |
2329 | newsect->flags |= SEC_ALLOC; |
2330 | if (hdr->p_flags & PF_X) | |
2331 | newsect->flags |= SEC_CODE; | |
2332 | } | |
2333 | if (!(hdr->p_flags & PF_W)) | |
2334 | newsect->flags |= SEC_READONLY; | |
2335 | } | |
2336 | ||
b34976b6 | 2337 | return TRUE; |
252b5132 RH |
2338 | } |
2339 | ||
b34976b6 | 2340 | bfd_boolean |
217aa764 | 2341 | bfd_section_from_phdr (bfd *abfd, Elf_Internal_Phdr *hdr, int index) |
20cfcaae | 2342 | { |
9c5bfbb7 | 2343 | const struct elf_backend_data *bed; |
20cfcaae NC |
2344 | |
2345 | switch (hdr->p_type) | |
2346 | { | |
2347 | case PT_NULL: | |
2348 | return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "null"); | |
2349 | ||
2350 | case PT_LOAD: | |
2351 | return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "load"); | |
2352 | ||
2353 | case PT_DYNAMIC: | |
2354 | return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "dynamic"); | |
2355 | ||
2356 | case PT_INTERP: | |
2357 | return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "interp"); | |
2358 | ||
2359 | case PT_NOTE: | |
2360 | if (! _bfd_elf_make_section_from_phdr (abfd, hdr, index, "note")) | |
b34976b6 | 2361 | return FALSE; |
718175fa | 2362 | if (! elf_read_notes (abfd, hdr->p_offset, hdr->p_filesz)) |
b34976b6 AM |
2363 | return FALSE; |
2364 | return TRUE; | |
20cfcaae NC |
2365 | |
2366 | case PT_SHLIB: | |
2367 | return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "shlib"); | |
2368 | ||
2369 | case PT_PHDR: | |
2370 | return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "phdr"); | |
2371 | ||
811072d8 RM |
2372 | case PT_GNU_EH_FRAME: |
2373 | return _bfd_elf_make_section_from_phdr (abfd, hdr, index, | |
2374 | "eh_frame_hdr"); | |
2375 | ||
2b05f1b7 L |
2376 | case PT_GNU_STACK: |
2377 | return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "stack"); | |
9ee5e499 | 2378 | |
8c37241b JJ |
2379 | case PT_GNU_RELRO: |
2380 | return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "relro"); | |
2381 | ||
20cfcaae | 2382 | default: |
8c1acd09 | 2383 | /* Check for any processor-specific program segment types. */ |
20cfcaae | 2384 | bed = get_elf_backend_data (abfd); |
d27f5fa1 | 2385 | return bed->elf_backend_section_from_phdr (abfd, hdr, index, "proc"); |
20cfcaae NC |
2386 | } |
2387 | } | |
2388 | ||
23bc299b | 2389 | /* Initialize REL_HDR, the section-header for new section, containing |
b34976b6 | 2390 | relocations against ASECT. If USE_RELA_P is TRUE, we use RELA |
23bc299b MM |
2391 | relocations; otherwise, we use REL relocations. */ |
2392 | ||
b34976b6 | 2393 | bfd_boolean |
217aa764 AM |
2394 | _bfd_elf_init_reloc_shdr (bfd *abfd, |
2395 | Elf_Internal_Shdr *rel_hdr, | |
2396 | asection *asect, | |
2397 | bfd_boolean use_rela_p) | |
23bc299b MM |
2398 | { |
2399 | char *name; | |
9c5bfbb7 | 2400 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
dc810e39 | 2401 | bfd_size_type amt = sizeof ".rela" + strlen (asect->name); |
23bc299b | 2402 | |
dc810e39 | 2403 | name = bfd_alloc (abfd, amt); |
23bc299b | 2404 | if (name == NULL) |
b34976b6 | 2405 | return FALSE; |
23bc299b MM |
2406 | sprintf (name, "%s%s", use_rela_p ? ".rela" : ".rel", asect->name); |
2407 | rel_hdr->sh_name = | |
2b0f7ef9 | 2408 | (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), name, |
b34976b6 | 2409 | FALSE); |
23bc299b | 2410 | if (rel_hdr->sh_name == (unsigned int) -1) |
b34976b6 | 2411 | return FALSE; |
23bc299b MM |
2412 | rel_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL; |
2413 | rel_hdr->sh_entsize = (use_rela_p | |
2414 | ? bed->s->sizeof_rela | |
2415 | : bed->s->sizeof_rel); | |
72de5009 | 2416 | rel_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align; |
23bc299b MM |
2417 | rel_hdr->sh_flags = 0; |
2418 | rel_hdr->sh_addr = 0; | |
2419 | rel_hdr->sh_size = 0; | |
2420 | rel_hdr->sh_offset = 0; | |
2421 | ||
b34976b6 | 2422 | return TRUE; |
23bc299b MM |
2423 | } |
2424 | ||
252b5132 RH |
2425 | /* Set up an ELF internal section header for a section. */ |
2426 | ||
252b5132 | 2427 | static void |
217aa764 | 2428 | elf_fake_sections (bfd *abfd, asection *asect, void *failedptrarg) |
252b5132 | 2429 | { |
9c5bfbb7 | 2430 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
217aa764 | 2431 | bfd_boolean *failedptr = failedptrarg; |
252b5132 | 2432 | Elf_Internal_Shdr *this_hdr; |
0414f35b | 2433 | unsigned int sh_type; |
252b5132 RH |
2434 | |
2435 | if (*failedptr) | |
2436 | { | |
2437 | /* We already failed; just get out of the bfd_map_over_sections | |
08a40648 | 2438 | loop. */ |
252b5132 RH |
2439 | return; |
2440 | } | |
2441 | ||
2442 | this_hdr = &elf_section_data (asect)->this_hdr; | |
2443 | ||
e57b5356 AM |
2444 | this_hdr->sh_name = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), |
2445 | asect->name, FALSE); | |
2446 | if (this_hdr->sh_name == (unsigned int) -1) | |
252b5132 | 2447 | { |
b34976b6 | 2448 | *failedptr = TRUE; |
252b5132 RH |
2449 | return; |
2450 | } | |
2451 | ||
a4d8e49b | 2452 | /* Don't clear sh_flags. Assembler may set additional bits. */ |
252b5132 RH |
2453 | |
2454 | if ((asect->flags & SEC_ALLOC) != 0 | |
2455 | || asect->user_set_vma) | |
2456 | this_hdr->sh_addr = asect->vma; | |
2457 | else | |
2458 | this_hdr->sh_addr = 0; | |
2459 | ||
2460 | this_hdr->sh_offset = 0; | |
eea6121a | 2461 | this_hdr->sh_size = asect->size; |
252b5132 | 2462 | this_hdr->sh_link = 0; |
72de5009 | 2463 | this_hdr->sh_addralign = (bfd_vma) 1 << asect->alignment_power; |
252b5132 RH |
2464 | /* The sh_entsize and sh_info fields may have been set already by |
2465 | copy_private_section_data. */ | |
2466 | ||
2467 | this_hdr->bfd_section = asect; | |
2468 | this_hdr->contents = NULL; | |
2469 | ||
3cddba1e L |
2470 | /* If the section type is unspecified, we set it based on |
2471 | asect->flags. */ | |
98ece1b3 AM |
2472 | if ((asect->flags & SEC_GROUP) != 0) |
2473 | sh_type = SHT_GROUP; | |
2474 | else if ((asect->flags & SEC_ALLOC) != 0 | |
2475 | && (((asect->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0) | |
2476 | || (asect->flags & SEC_NEVER_LOAD) != 0)) | |
2477 | sh_type = SHT_NOBITS; | |
2478 | else | |
2479 | sh_type = SHT_PROGBITS; | |
2480 | ||
3cddba1e | 2481 | if (this_hdr->sh_type == SHT_NULL) |
98ece1b3 AM |
2482 | this_hdr->sh_type = sh_type; |
2483 | else if (this_hdr->sh_type == SHT_NOBITS | |
2484 | && sh_type == SHT_PROGBITS | |
2485 | && (asect->flags & SEC_ALLOC) != 0) | |
3cddba1e | 2486 | { |
98ece1b3 AM |
2487 | /* Warn if we are changing a NOBITS section to PROGBITS, but |
2488 | allow the link to proceed. This can happen when users link | |
2489 | non-bss input sections to bss output sections, or emit data | |
2490 | to a bss output section via a linker script. */ | |
2491 | (*_bfd_error_handler) | |
58f0869b | 2492 | (_("warning: section `%A' type changed to PROGBITS"), asect); |
98ece1b3 | 2493 | this_hdr->sh_type = sh_type; |
3cddba1e L |
2494 | } |
2495 | ||
2f89ff8d | 2496 | switch (this_hdr->sh_type) |
252b5132 | 2497 | { |
2f89ff8d | 2498 | default: |
2f89ff8d L |
2499 | break; |
2500 | ||
2501 | case SHT_STRTAB: | |
2502 | case SHT_INIT_ARRAY: | |
2503 | case SHT_FINI_ARRAY: | |
2504 | case SHT_PREINIT_ARRAY: | |
2505 | case SHT_NOTE: | |
2506 | case SHT_NOBITS: | |
2507 | case SHT_PROGBITS: | |
2508 | break; | |
2509 | ||
2510 | case SHT_HASH: | |
c7ac6ff8 | 2511 | this_hdr->sh_entsize = bed->s->sizeof_hash_entry; |
2f89ff8d | 2512 | break; |
5de3bf90 | 2513 | |
2f89ff8d | 2514 | case SHT_DYNSYM: |
252b5132 | 2515 | this_hdr->sh_entsize = bed->s->sizeof_sym; |
2f89ff8d L |
2516 | break; |
2517 | ||
2518 | case SHT_DYNAMIC: | |
252b5132 | 2519 | this_hdr->sh_entsize = bed->s->sizeof_dyn; |
2f89ff8d L |
2520 | break; |
2521 | ||
2522 | case SHT_RELA: | |
2523 | if (get_elf_backend_data (abfd)->may_use_rela_p) | |
2524 | this_hdr->sh_entsize = bed->s->sizeof_rela; | |
2525 | break; | |
2526 | ||
2527 | case SHT_REL: | |
2528 | if (get_elf_backend_data (abfd)->may_use_rel_p) | |
2529 | this_hdr->sh_entsize = bed->s->sizeof_rel; | |
2530 | break; | |
2531 | ||
2532 | case SHT_GNU_versym: | |
252b5132 | 2533 | this_hdr->sh_entsize = sizeof (Elf_External_Versym); |
2f89ff8d L |
2534 | break; |
2535 | ||
2536 | case SHT_GNU_verdef: | |
252b5132 RH |
2537 | this_hdr->sh_entsize = 0; |
2538 | /* objcopy or strip will copy over sh_info, but may not set | |
08a40648 AM |
2539 | cverdefs. The linker will set cverdefs, but sh_info will be |
2540 | zero. */ | |
252b5132 RH |
2541 | if (this_hdr->sh_info == 0) |
2542 | this_hdr->sh_info = elf_tdata (abfd)->cverdefs; | |
2543 | else | |
2544 | BFD_ASSERT (elf_tdata (abfd)->cverdefs == 0 | |
2545 | || this_hdr->sh_info == elf_tdata (abfd)->cverdefs); | |
2f89ff8d L |
2546 | break; |
2547 | ||
2548 | case SHT_GNU_verneed: | |
252b5132 RH |
2549 | this_hdr->sh_entsize = 0; |
2550 | /* objcopy or strip will copy over sh_info, but may not set | |
08a40648 AM |
2551 | cverrefs. The linker will set cverrefs, but sh_info will be |
2552 | zero. */ | |
252b5132 RH |
2553 | if (this_hdr->sh_info == 0) |
2554 | this_hdr->sh_info = elf_tdata (abfd)->cverrefs; | |
2555 | else | |
2556 | BFD_ASSERT (elf_tdata (abfd)->cverrefs == 0 | |
2557 | || this_hdr->sh_info == elf_tdata (abfd)->cverrefs); | |
2f89ff8d L |
2558 | break; |
2559 | ||
2560 | case SHT_GROUP: | |
1783205a | 2561 | this_hdr->sh_entsize = GRP_ENTRY_SIZE; |
2f89ff8d | 2562 | break; |
fdc90cb4 JJ |
2563 | |
2564 | case SHT_GNU_HASH: | |
2565 | this_hdr->sh_entsize = bed->s->arch_size == 64 ? 0 : 4; | |
2566 | break; | |
dbb410c3 | 2567 | } |
252b5132 RH |
2568 | |
2569 | if ((asect->flags & SEC_ALLOC) != 0) | |
2570 | this_hdr->sh_flags |= SHF_ALLOC; | |
2571 | if ((asect->flags & SEC_READONLY) == 0) | |
2572 | this_hdr->sh_flags |= SHF_WRITE; | |
2573 | if ((asect->flags & SEC_CODE) != 0) | |
2574 | this_hdr->sh_flags |= SHF_EXECINSTR; | |
f5fa8ca2 JJ |
2575 | if ((asect->flags & SEC_MERGE) != 0) |
2576 | { | |
2577 | this_hdr->sh_flags |= SHF_MERGE; | |
2578 | this_hdr->sh_entsize = asect->entsize; | |
2579 | if ((asect->flags & SEC_STRINGS) != 0) | |
2580 | this_hdr->sh_flags |= SHF_STRINGS; | |
2581 | } | |
1126897b | 2582 | if ((asect->flags & SEC_GROUP) == 0 && elf_group_name (asect) != NULL) |
dbb410c3 | 2583 | this_hdr->sh_flags |= SHF_GROUP; |
13ae64f3 | 2584 | if ((asect->flags & SEC_THREAD_LOCAL) != 0) |
704afa60 JJ |
2585 | { |
2586 | this_hdr->sh_flags |= SHF_TLS; | |
3a800eb9 AM |
2587 | if (asect->size == 0 |
2588 | && (asect->flags & SEC_HAS_CONTENTS) == 0) | |
704afa60 | 2589 | { |
3a800eb9 | 2590 | struct bfd_link_order *o = asect->map_tail.link_order; |
b34976b6 | 2591 | |
704afa60 | 2592 | this_hdr->sh_size = 0; |
3a800eb9 AM |
2593 | if (o != NULL) |
2594 | { | |
704afa60 | 2595 | this_hdr->sh_size = o->offset + o->size; |
3a800eb9 AM |
2596 | if (this_hdr->sh_size != 0) |
2597 | this_hdr->sh_type = SHT_NOBITS; | |
2598 | } | |
704afa60 JJ |
2599 | } |
2600 | } | |
252b5132 RH |
2601 | |
2602 | /* Check for processor-specific section types. */ | |
0414f35b | 2603 | sh_type = this_hdr->sh_type; |
e1fddb6b AO |
2604 | if (bed->elf_backend_fake_sections |
2605 | && !(*bed->elf_backend_fake_sections) (abfd, this_hdr, asect)) | |
b34976b6 | 2606 | *failedptr = TRUE; |
252b5132 | 2607 | |
42bb2e33 | 2608 | if (sh_type == SHT_NOBITS && asect->size != 0) |
0414f35b AM |
2609 | { |
2610 | /* Don't change the header type from NOBITS if we are being | |
42bb2e33 | 2611 | called for objcopy --only-keep-debug. */ |
0414f35b AM |
2612 | this_hdr->sh_type = sh_type; |
2613 | } | |
2614 | ||
252b5132 | 2615 | /* If the section has relocs, set up a section header for the |
23bc299b MM |
2616 | SHT_REL[A] section. If two relocation sections are required for |
2617 | this section, it is up to the processor-specific back-end to | |
c044fabd | 2618 | create the other. */ |
23bc299b | 2619 | if ((asect->flags & SEC_RELOC) != 0 |
c044fabd | 2620 | && !_bfd_elf_init_reloc_shdr (abfd, |
23bc299b | 2621 | &elf_section_data (asect)->rel_hdr, |
c044fabd | 2622 | asect, |
68bfbfcc | 2623 | asect->use_rela_p)) |
b34976b6 | 2624 | *failedptr = TRUE; |
252b5132 RH |
2625 | } |
2626 | ||
bcacc0f5 AM |
2627 | /* Fill in the contents of a SHT_GROUP section. Called from |
2628 | _bfd_elf_compute_section_file_positions for gas, objcopy, and | |
2629 | when ELF targets use the generic linker, ld. Called for ld -r | |
2630 | from bfd_elf_final_link. */ | |
dbb410c3 | 2631 | |
1126897b | 2632 | void |
217aa764 | 2633 | bfd_elf_set_group_contents (bfd *abfd, asection *sec, void *failedptrarg) |
dbb410c3 | 2634 | { |
217aa764 | 2635 | bfd_boolean *failedptr = failedptrarg; |
9dce4196 | 2636 | asection *elt, *first; |
dbb410c3 | 2637 | unsigned char *loc; |
b34976b6 | 2638 | bfd_boolean gas; |
dbb410c3 | 2639 | |
7e4111ad L |
2640 | /* Ignore linker created group section. See elfNN_ia64_object_p in |
2641 | elfxx-ia64.c. */ | |
2642 | if (((sec->flags & (SEC_GROUP | SEC_LINKER_CREATED)) != SEC_GROUP) | |
dbb410c3 AM |
2643 | || *failedptr) |
2644 | return; | |
2645 | ||
bcacc0f5 AM |
2646 | if (elf_section_data (sec)->this_hdr.sh_info == 0) |
2647 | { | |
2648 | unsigned long symindx = 0; | |
2649 | ||
2650 | /* elf_group_id will have been set up by objcopy and the | |
2651 | generic linker. */ | |
2652 | if (elf_group_id (sec) != NULL) | |
2653 | symindx = elf_group_id (sec)->udata.i; | |
1126897b | 2654 | |
bcacc0f5 AM |
2655 | if (symindx == 0) |
2656 | { | |
2657 | /* If called from the assembler, swap_out_syms will have set up | |
2658 | elf_section_syms. */ | |
2659 | BFD_ASSERT (elf_section_syms (abfd) != NULL); | |
2660 | symindx = elf_section_syms (abfd)[sec->index]->udata.i; | |
2661 | } | |
2662 | elf_section_data (sec)->this_hdr.sh_info = symindx; | |
2663 | } | |
2664 | else if (elf_section_data (sec)->this_hdr.sh_info == (unsigned int) -2) | |
1126897b | 2665 | { |
bcacc0f5 AM |
2666 | /* The ELF backend linker sets sh_info to -2 when the group |
2667 | signature symbol is global, and thus the index can't be | |
2668 | set until all local symbols are output. */ | |
2669 | asection *igroup = elf_sec_group (elf_next_in_group (sec)); | |
2670 | struct bfd_elf_section_data *sec_data = elf_section_data (igroup); | |
2671 | unsigned long symndx = sec_data->this_hdr.sh_info; | |
2672 | unsigned long extsymoff = 0; | |
2673 | struct elf_link_hash_entry *h; | |
2674 | ||
2675 | if (!elf_bad_symtab (igroup->owner)) | |
2676 | { | |
2677 | Elf_Internal_Shdr *symtab_hdr; | |
2678 | ||
2679 | symtab_hdr = &elf_tdata (igroup->owner)->symtab_hdr; | |
2680 | extsymoff = symtab_hdr->sh_info; | |
2681 | } | |
2682 | h = elf_sym_hashes (igroup->owner)[symndx - extsymoff]; | |
2683 | while (h->root.type == bfd_link_hash_indirect | |
2684 | || h->root.type == bfd_link_hash_warning) | |
2685 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
2686 | ||
2687 | elf_section_data (sec)->this_hdr.sh_info = h->indx; | |
1126897b | 2688 | } |
dbb410c3 | 2689 | |
1126897b | 2690 | /* The contents won't be allocated for "ld -r" or objcopy. */ |
b34976b6 | 2691 | gas = TRUE; |
dbb410c3 AM |
2692 | if (sec->contents == NULL) |
2693 | { | |
b34976b6 | 2694 | gas = FALSE; |
eea6121a | 2695 | sec->contents = bfd_alloc (abfd, sec->size); |
9dce4196 AM |
2696 | |
2697 | /* Arrange for the section to be written out. */ | |
2698 | elf_section_data (sec)->this_hdr.contents = sec->contents; | |
dbb410c3 AM |
2699 | if (sec->contents == NULL) |
2700 | { | |
b34976b6 | 2701 | *failedptr = TRUE; |
dbb410c3 AM |
2702 | return; |
2703 | } | |
2704 | } | |
2705 | ||
eea6121a | 2706 | loc = sec->contents + sec->size; |
dbb410c3 | 2707 | |
9dce4196 AM |
2708 | /* Get the pointer to the first section in the group that gas |
2709 | squirreled away here. objcopy arranges for this to be set to the | |
2710 | start of the input section group. */ | |
2711 | first = elt = elf_next_in_group (sec); | |
dbb410c3 AM |
2712 | |
2713 | /* First element is a flag word. Rest of section is elf section | |
2714 | indices for all the sections of the group. Write them backwards | |
2715 | just to keep the group in the same order as given in .section | |
2716 | directives, not that it matters. */ | |
2717 | while (elt != NULL) | |
2718 | { | |
9dce4196 AM |
2719 | asection *s; |
2720 | unsigned int idx; | |
2721 | ||
9dce4196 | 2722 | s = elt; |
01e1a5bc NC |
2723 | if (! elf_discarded_section (s)) |
2724 | { | |
2725 | loc -= 4; | |
2726 | if (!gas) | |
2727 | s = s->output_section; | |
2728 | idx = 0; | |
2729 | if (s != NULL) | |
2730 | idx = elf_section_data (s)->this_idx; | |
2731 | H_PUT_32 (abfd, idx, loc); | |
2732 | } | |
945906ff | 2733 | elt = elf_next_in_group (elt); |
9dce4196 AM |
2734 | if (elt == first) |
2735 | break; | |
dbb410c3 AM |
2736 | } |
2737 | ||
3d7f7666 | 2738 | if ((loc -= 4) != sec->contents) |
9dce4196 | 2739 | abort (); |
dbb410c3 | 2740 | |
9dce4196 | 2741 | H_PUT_32 (abfd, sec->flags & SEC_LINK_ONCE ? GRP_COMDAT : 0, loc); |
dbb410c3 AM |
2742 | } |
2743 | ||
252b5132 RH |
2744 | /* Assign all ELF section numbers. The dummy first section is handled here |
2745 | too. The link/info pointers for the standard section types are filled | |
2746 | in here too, while we're at it. */ | |
2747 | ||
b34976b6 | 2748 | static bfd_boolean |
da9f89d4 | 2749 | assign_section_numbers (bfd *abfd, struct bfd_link_info *link_info) |
252b5132 RH |
2750 | { |
2751 | struct elf_obj_tdata *t = elf_tdata (abfd); | |
2752 | asection *sec; | |
2b0f7ef9 | 2753 | unsigned int section_number, secn; |
252b5132 | 2754 | Elf_Internal_Shdr **i_shdrp; |
47cc2cf5 | 2755 | struct bfd_elf_section_data *d; |
3516e984 | 2756 | bfd_boolean need_symtab; |
252b5132 RH |
2757 | |
2758 | section_number = 1; | |
2759 | ||
2b0f7ef9 JJ |
2760 | _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd)); |
2761 | ||
da9f89d4 L |
2762 | /* SHT_GROUP sections are in relocatable files only. */ |
2763 | if (link_info == NULL || link_info->relocatable) | |
252b5132 | 2764 | { |
da9f89d4 | 2765 | /* Put SHT_GROUP sections first. */ |
04dd1667 | 2766 | for (sec = abfd->sections; sec != NULL; sec = sec->next) |
47cc2cf5 | 2767 | { |
5daa8fe7 | 2768 | d = elf_section_data (sec); |
da9f89d4 L |
2769 | |
2770 | if (d->this_hdr.sh_type == SHT_GROUP) | |
08a40648 | 2771 | { |
5daa8fe7 | 2772 | if (sec->flags & SEC_LINKER_CREATED) |
da9f89d4 L |
2773 | { |
2774 | /* Remove the linker created SHT_GROUP sections. */ | |
5daa8fe7 | 2775 | bfd_section_list_remove (abfd, sec); |
da9f89d4 | 2776 | abfd->section_count--; |
da9f89d4 | 2777 | } |
08a40648 | 2778 | else |
4fbb74a6 | 2779 | d->this_idx = section_number++; |
da9f89d4 | 2780 | } |
47cc2cf5 PB |
2781 | } |
2782 | } | |
2783 | ||
2784 | for (sec = abfd->sections; sec; sec = sec->next) | |
2785 | { | |
2786 | d = elf_section_data (sec); | |
2787 | ||
2788 | if (d->this_hdr.sh_type != SHT_GROUP) | |
4fbb74a6 | 2789 | d->this_idx = section_number++; |
2b0f7ef9 | 2790 | _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->this_hdr.sh_name); |
252b5132 RH |
2791 | if ((sec->flags & SEC_RELOC) == 0) |
2792 | d->rel_idx = 0; | |
2793 | else | |
2b0f7ef9 JJ |
2794 | { |
2795 | d->rel_idx = section_number++; | |
2796 | _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel_hdr.sh_name); | |
2797 | } | |
23bc299b MM |
2798 | |
2799 | if (d->rel_hdr2) | |
2b0f7ef9 JJ |
2800 | { |
2801 | d->rel_idx2 = section_number++; | |
2802 | _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel_hdr2->sh_name); | |
2803 | } | |
23bc299b MM |
2804 | else |
2805 | d->rel_idx2 = 0; | |
252b5132 RH |
2806 | } |
2807 | ||
2808 | t->shstrtab_section = section_number++; | |
2b0f7ef9 | 2809 | _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->shstrtab_hdr.sh_name); |
252b5132 | 2810 | elf_elfheader (abfd)->e_shstrndx = t->shstrtab_section; |
252b5132 | 2811 | |
3516e984 L |
2812 | need_symtab = (bfd_get_symcount (abfd) > 0 |
2813 | || (link_info == NULL | |
2814 | && ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC)) | |
2815 | == HAS_RELOC))); | |
2816 | if (need_symtab) | |
252b5132 RH |
2817 | { |
2818 | t->symtab_section = section_number++; | |
2b0f7ef9 | 2819 | _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->symtab_hdr.sh_name); |
4fbb74a6 | 2820 | if (section_number > ((SHN_LORESERVE - 2) & 0xFFFF)) |
9ad5cbcf | 2821 | { |
9ad5cbcf AM |
2822 | t->symtab_shndx_section = section_number++; |
2823 | t->symtab_shndx_hdr.sh_name | |
2824 | = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), | |
b34976b6 | 2825 | ".symtab_shndx", FALSE); |
9ad5cbcf | 2826 | if (t->symtab_shndx_hdr.sh_name == (unsigned int) -1) |
b34976b6 | 2827 | return FALSE; |
9ad5cbcf | 2828 | } |
252b5132 | 2829 | t->strtab_section = section_number++; |
2b0f7ef9 | 2830 | _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->strtab_hdr.sh_name); |
252b5132 RH |
2831 | } |
2832 | ||
2b0f7ef9 JJ |
2833 | _bfd_elf_strtab_finalize (elf_shstrtab (abfd)); |
2834 | t->shstrtab_hdr.sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd)); | |
9ad5cbcf AM |
2835 | |
2836 | elf_numsections (abfd) = section_number; | |
252b5132 RH |
2837 | elf_elfheader (abfd)->e_shnum = section_number; |
2838 | ||
2839 | /* Set up the list of section header pointers, in agreement with the | |
2840 | indices. */ | |
d0fb9a8d | 2841 | i_shdrp = bfd_zalloc2 (abfd, section_number, sizeof (Elf_Internal_Shdr *)); |
252b5132 | 2842 | if (i_shdrp == NULL) |
b34976b6 | 2843 | return FALSE; |
252b5132 | 2844 | |
d0fb9a8d | 2845 | i_shdrp[0] = bfd_zalloc (abfd, sizeof (Elf_Internal_Shdr)); |
252b5132 RH |
2846 | if (i_shdrp[0] == NULL) |
2847 | { | |
2848 | bfd_release (abfd, i_shdrp); | |
b34976b6 | 2849 | return FALSE; |
252b5132 | 2850 | } |
252b5132 RH |
2851 | |
2852 | elf_elfsections (abfd) = i_shdrp; | |
2853 | ||
2854 | i_shdrp[t->shstrtab_section] = &t->shstrtab_hdr; | |
3516e984 | 2855 | if (need_symtab) |
252b5132 RH |
2856 | { |
2857 | i_shdrp[t->symtab_section] = &t->symtab_hdr; | |
4fbb74a6 | 2858 | if (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF)) |
9ad5cbcf AM |
2859 | { |
2860 | i_shdrp[t->symtab_shndx_section] = &t->symtab_shndx_hdr; | |
2861 | t->symtab_shndx_hdr.sh_link = t->symtab_section; | |
2862 | } | |
252b5132 RH |
2863 | i_shdrp[t->strtab_section] = &t->strtab_hdr; |
2864 | t->symtab_hdr.sh_link = t->strtab_section; | |
2865 | } | |
38ce5b11 | 2866 | |
252b5132 RH |
2867 | for (sec = abfd->sections; sec; sec = sec->next) |
2868 | { | |
2869 | struct bfd_elf_section_data *d = elf_section_data (sec); | |
2870 | asection *s; | |
2871 | const char *name; | |
2872 | ||
2873 | i_shdrp[d->this_idx] = &d->this_hdr; | |
2874 | if (d->rel_idx != 0) | |
2875 | i_shdrp[d->rel_idx] = &d->rel_hdr; | |
23bc299b MM |
2876 | if (d->rel_idx2 != 0) |
2877 | i_shdrp[d->rel_idx2] = d->rel_hdr2; | |
252b5132 RH |
2878 | |
2879 | /* Fill in the sh_link and sh_info fields while we're at it. */ | |
2880 | ||
2881 | /* sh_link of a reloc section is the section index of the symbol | |
2882 | table. sh_info is the section index of the section to which | |
2883 | the relocation entries apply. */ | |
2884 | if (d->rel_idx != 0) | |
2885 | { | |
2886 | d->rel_hdr.sh_link = t->symtab_section; | |
2887 | d->rel_hdr.sh_info = d->this_idx; | |
2888 | } | |
23bc299b MM |
2889 | if (d->rel_idx2 != 0) |
2890 | { | |
2891 | d->rel_hdr2->sh_link = t->symtab_section; | |
2892 | d->rel_hdr2->sh_info = d->this_idx; | |
2893 | } | |
252b5132 | 2894 | |
38ce5b11 L |
2895 | /* We need to set up sh_link for SHF_LINK_ORDER. */ |
2896 | if ((d->this_hdr.sh_flags & SHF_LINK_ORDER) != 0) | |
2897 | { | |
2898 | s = elf_linked_to_section (sec); | |
2899 | if (s) | |
38ce5b11 | 2900 | { |
f2876037 | 2901 | /* elf_linked_to_section points to the input section. */ |
ccd2ec6a | 2902 | if (link_info != NULL) |
38ce5b11 | 2903 | { |
f2876037 | 2904 | /* Check discarded linkonce section. */ |
ccd2ec6a | 2905 | if (elf_discarded_section (s)) |
38ce5b11 | 2906 | { |
ccd2ec6a L |
2907 | asection *kept; |
2908 | (*_bfd_error_handler) | |
2909 | (_("%B: sh_link of section `%A' points to discarded section `%A' of `%B'"), | |
2910 | abfd, d->this_hdr.bfd_section, | |
2911 | s, s->owner); | |
2912 | /* Point to the kept section if it has the same | |
2913 | size as the discarded one. */ | |
c0f00686 | 2914 | kept = _bfd_elf_check_kept_section (s, link_info); |
ccd2ec6a | 2915 | if (kept == NULL) |
185d09ad | 2916 | { |
ccd2ec6a L |
2917 | bfd_set_error (bfd_error_bad_value); |
2918 | return FALSE; | |
185d09ad | 2919 | } |
ccd2ec6a | 2920 | s = kept; |
38ce5b11 | 2921 | } |
e424ecc8 | 2922 | |
ccd2ec6a L |
2923 | s = s->output_section; |
2924 | BFD_ASSERT (s != NULL); | |
38ce5b11 | 2925 | } |
f2876037 L |
2926 | else |
2927 | { | |
2928 | /* Handle objcopy. */ | |
2929 | if (s->output_section == NULL) | |
2930 | { | |
2931 | (*_bfd_error_handler) | |
2932 | (_("%B: sh_link of section `%A' points to removed section `%A' of `%B'"), | |
2933 | abfd, d->this_hdr.bfd_section, s, s->owner); | |
2934 | bfd_set_error (bfd_error_bad_value); | |
2935 | return FALSE; | |
2936 | } | |
2937 | s = s->output_section; | |
2938 | } | |
ccd2ec6a L |
2939 | d->this_hdr.sh_link = elf_section_data (s)->this_idx; |
2940 | } | |
2941 | else | |
2942 | { | |
2943 | /* PR 290: | |
2944 | The Intel C compiler generates SHT_IA_64_UNWIND with | |
2945 | SHF_LINK_ORDER. But it doesn't set the sh_link or | |
2946 | sh_info fields. Hence we could get the situation | |
08a40648 | 2947 | where s is NULL. */ |
ccd2ec6a L |
2948 | const struct elf_backend_data *bed |
2949 | = get_elf_backend_data (abfd); | |
2950 | if (bed->link_order_error_handler) | |
2951 | bed->link_order_error_handler | |
2952 | (_("%B: warning: sh_link not set for section `%A'"), | |
2953 | abfd, sec); | |
38ce5b11 L |
2954 | } |
2955 | } | |
2956 | ||
252b5132 RH |
2957 | switch (d->this_hdr.sh_type) |
2958 | { | |
2959 | case SHT_REL: | |
2960 | case SHT_RELA: | |
2961 | /* A reloc section which we are treating as a normal BFD | |
2962 | section. sh_link is the section index of the symbol | |
2963 | table. sh_info is the section index of the section to | |
2964 | which the relocation entries apply. We assume that an | |
2965 | allocated reloc section uses the dynamic symbol table. | |
2966 | FIXME: How can we be sure? */ | |
2967 | s = bfd_get_section_by_name (abfd, ".dynsym"); | |
2968 | if (s != NULL) | |
2969 | d->this_hdr.sh_link = elf_section_data (s)->this_idx; | |
2970 | ||
2971 | /* We look up the section the relocs apply to by name. */ | |
2972 | name = sec->name; | |
2973 | if (d->this_hdr.sh_type == SHT_REL) | |
2974 | name += 4; | |
2975 | else | |
2976 | name += 5; | |
2977 | s = bfd_get_section_by_name (abfd, name); | |
2978 | if (s != NULL) | |
2979 | d->this_hdr.sh_info = elf_section_data (s)->this_idx; | |
2980 | break; | |
2981 | ||
2982 | case SHT_STRTAB: | |
2983 | /* We assume that a section named .stab*str is a stabs | |
2984 | string section. We look for a section with the same name | |
2985 | but without the trailing ``str'', and set its sh_link | |
2986 | field to point to this section. */ | |
0112cd26 | 2987 | if (CONST_STRNEQ (sec->name, ".stab") |
252b5132 RH |
2988 | && strcmp (sec->name + strlen (sec->name) - 3, "str") == 0) |
2989 | { | |
2990 | size_t len; | |
2991 | char *alc; | |
2992 | ||
2993 | len = strlen (sec->name); | |
217aa764 | 2994 | alc = bfd_malloc (len - 2); |
252b5132 | 2995 | if (alc == NULL) |
b34976b6 | 2996 | return FALSE; |
d4c88bbb | 2997 | memcpy (alc, sec->name, len - 3); |
252b5132 RH |
2998 | alc[len - 3] = '\0'; |
2999 | s = bfd_get_section_by_name (abfd, alc); | |
3000 | free (alc); | |
3001 | if (s != NULL) | |
3002 | { | |
3003 | elf_section_data (s)->this_hdr.sh_link = d->this_idx; | |
3004 | ||
3005 | /* This is a .stab section. */ | |
0594c12d AM |
3006 | if (elf_section_data (s)->this_hdr.sh_entsize == 0) |
3007 | elf_section_data (s)->this_hdr.sh_entsize | |
3008 | = 4 + 2 * bfd_get_arch_size (abfd) / 8; | |
252b5132 RH |
3009 | } |
3010 | } | |
3011 | break; | |
3012 | ||
3013 | case SHT_DYNAMIC: | |
3014 | case SHT_DYNSYM: | |
3015 | case SHT_GNU_verneed: | |
3016 | case SHT_GNU_verdef: | |
3017 | /* sh_link is the section header index of the string table | |
3018 | used for the dynamic entries, or the symbol table, or the | |
3019 | version strings. */ | |
3020 | s = bfd_get_section_by_name (abfd, ".dynstr"); | |
3021 | if (s != NULL) | |
3022 | d->this_hdr.sh_link = elf_section_data (s)->this_idx; | |
3023 | break; | |
3024 | ||
7f1204bb JJ |
3025 | case SHT_GNU_LIBLIST: |
3026 | /* sh_link is the section header index of the prelink library | |
08a40648 AM |
3027 | list used for the dynamic entries, or the symbol table, or |
3028 | the version strings. */ | |
7f1204bb JJ |
3029 | s = bfd_get_section_by_name (abfd, (sec->flags & SEC_ALLOC) |
3030 | ? ".dynstr" : ".gnu.libstr"); | |
3031 | if (s != NULL) | |
3032 | d->this_hdr.sh_link = elf_section_data (s)->this_idx; | |
3033 | break; | |
3034 | ||
252b5132 | 3035 | case SHT_HASH: |
fdc90cb4 | 3036 | case SHT_GNU_HASH: |
252b5132 RH |
3037 | case SHT_GNU_versym: |
3038 | /* sh_link is the section header index of the symbol table | |
3039 | this hash table or version table is for. */ | |
3040 | s = bfd_get_section_by_name (abfd, ".dynsym"); | |
3041 | if (s != NULL) | |
3042 | d->this_hdr.sh_link = elf_section_data (s)->this_idx; | |
3043 | break; | |
dbb410c3 AM |
3044 | |
3045 | case SHT_GROUP: | |
3046 | d->this_hdr.sh_link = t->symtab_section; | |
252b5132 RH |
3047 | } |
3048 | } | |
3049 | ||
2b0f7ef9 | 3050 | for (secn = 1; secn < section_number; ++secn) |
9ad5cbcf AM |
3051 | if (i_shdrp[secn] == NULL) |
3052 | i_shdrp[secn] = i_shdrp[0]; | |
3053 | else | |
3054 | i_shdrp[secn]->sh_name = _bfd_elf_strtab_offset (elf_shstrtab (abfd), | |
3055 | i_shdrp[secn]->sh_name); | |
b34976b6 | 3056 | return TRUE; |
252b5132 RH |
3057 | } |
3058 | ||
3059 | /* Map symbol from it's internal number to the external number, moving | |
3060 | all local symbols to be at the head of the list. */ | |
3061 | ||
5372391b | 3062 | static bfd_boolean |
217aa764 | 3063 | sym_is_global (bfd *abfd, asymbol *sym) |
252b5132 RH |
3064 | { |
3065 | /* If the backend has a special mapping, use it. */ | |
9c5bfbb7 | 3066 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
217aa764 AM |
3067 | if (bed->elf_backend_sym_is_global) |
3068 | return (*bed->elf_backend_sym_is_global) (abfd, sym); | |
252b5132 | 3069 | |
e47bf690 | 3070 | return ((sym->flags & (BSF_GLOBAL | BSF_WEAK | BSF_GNU_UNIQUE)) != 0 |
252b5132 RH |
3071 | || bfd_is_und_section (bfd_get_section (sym)) |
3072 | || bfd_is_com_section (bfd_get_section (sym))); | |
3073 | } | |
3074 | ||
5372391b | 3075 | /* Don't output section symbols for sections that are not going to be |
0f0a5e58 | 3076 | output. */ |
5372391b AM |
3077 | |
3078 | static bfd_boolean | |
3079 | ignore_section_sym (bfd *abfd, asymbol *sym) | |
3080 | { | |
3081 | return ((sym->flags & BSF_SECTION_SYM) != 0 | |
0f0a5e58 AM |
3082 | && !(sym->section->owner == abfd |
3083 | || (sym->section->output_section->owner == abfd | |
3084 | && sym->section->output_offset == 0))); | |
5372391b AM |
3085 | } |
3086 | ||
b34976b6 | 3087 | static bfd_boolean |
217aa764 | 3088 | elf_map_symbols (bfd *abfd) |
252b5132 | 3089 | { |
dc810e39 | 3090 | unsigned int symcount = bfd_get_symcount (abfd); |
252b5132 RH |
3091 | asymbol **syms = bfd_get_outsymbols (abfd); |
3092 | asymbol **sect_syms; | |
dc810e39 AM |
3093 | unsigned int num_locals = 0; |
3094 | unsigned int num_globals = 0; | |
3095 | unsigned int num_locals2 = 0; | |
3096 | unsigned int num_globals2 = 0; | |
252b5132 | 3097 | int max_index = 0; |
dc810e39 | 3098 | unsigned int idx; |
252b5132 RH |
3099 | asection *asect; |
3100 | asymbol **new_syms; | |
252b5132 RH |
3101 | |
3102 | #ifdef DEBUG | |
3103 | fprintf (stderr, "elf_map_symbols\n"); | |
3104 | fflush (stderr); | |
3105 | #endif | |
3106 | ||
252b5132 RH |
3107 | for (asect = abfd->sections; asect; asect = asect->next) |
3108 | { | |
3109 | if (max_index < asect->index) | |
3110 | max_index = asect->index; | |
3111 | } | |
3112 | ||
3113 | max_index++; | |
d0fb9a8d | 3114 | sect_syms = bfd_zalloc2 (abfd, max_index, sizeof (asymbol *)); |
252b5132 | 3115 | if (sect_syms == NULL) |
b34976b6 | 3116 | return FALSE; |
252b5132 | 3117 | elf_section_syms (abfd) = sect_syms; |
4e89ac30 | 3118 | elf_num_section_syms (abfd) = max_index; |
252b5132 | 3119 | |
079e9a2f AM |
3120 | /* Init sect_syms entries for any section symbols we have already |
3121 | decided to output. */ | |
252b5132 RH |
3122 | for (idx = 0; idx < symcount; idx++) |
3123 | { | |
dc810e39 | 3124 | asymbol *sym = syms[idx]; |
c044fabd | 3125 | |
252b5132 | 3126 | if ((sym->flags & BSF_SECTION_SYM) != 0 |
0f0a5e58 | 3127 | && sym->value == 0 |
5372391b | 3128 | && !ignore_section_sym (abfd, sym)) |
252b5132 | 3129 | { |
5372391b | 3130 | asection *sec = sym->section; |
252b5132 | 3131 | |
5372391b AM |
3132 | if (sec->owner != abfd) |
3133 | sec = sec->output_section; | |
252b5132 | 3134 | |
5372391b | 3135 | sect_syms[sec->index] = syms[idx]; |
252b5132 RH |
3136 | } |
3137 | } | |
3138 | ||
252b5132 RH |
3139 | /* Classify all of the symbols. */ |
3140 | for (idx = 0; idx < symcount; idx++) | |
3141 | { | |
5372391b AM |
3142 | if (ignore_section_sym (abfd, syms[idx])) |
3143 | continue; | |
252b5132 RH |
3144 | if (!sym_is_global (abfd, syms[idx])) |
3145 | num_locals++; | |
3146 | else | |
3147 | num_globals++; | |
3148 | } | |
079e9a2f | 3149 | |
5372391b | 3150 | /* We will be adding a section symbol for each normal BFD section. Most |
079e9a2f AM |
3151 | sections will already have a section symbol in outsymbols, but |
3152 | eg. SHT_GROUP sections will not, and we need the section symbol mapped | |
3153 | at least in that case. */ | |
252b5132 RH |
3154 | for (asect = abfd->sections; asect; asect = asect->next) |
3155 | { | |
079e9a2f | 3156 | if (sect_syms[asect->index] == NULL) |
252b5132 | 3157 | { |
079e9a2f | 3158 | if (!sym_is_global (abfd, asect->symbol)) |
252b5132 RH |
3159 | num_locals++; |
3160 | else | |
3161 | num_globals++; | |
252b5132 RH |
3162 | } |
3163 | } | |
3164 | ||
3165 | /* Now sort the symbols so the local symbols are first. */ | |
d0fb9a8d | 3166 | new_syms = bfd_alloc2 (abfd, num_locals + num_globals, sizeof (asymbol *)); |
dc810e39 | 3167 | |
252b5132 | 3168 | if (new_syms == NULL) |
b34976b6 | 3169 | return FALSE; |
252b5132 RH |
3170 | |
3171 | for (idx = 0; idx < symcount; idx++) | |
3172 | { | |
3173 | asymbol *sym = syms[idx]; | |
dc810e39 | 3174 | unsigned int i; |
252b5132 | 3175 | |
5372391b AM |
3176 | if (ignore_section_sym (abfd, sym)) |
3177 | continue; | |
252b5132 RH |
3178 | if (!sym_is_global (abfd, sym)) |
3179 | i = num_locals2++; | |
3180 | else | |
3181 | i = num_locals + num_globals2++; | |
3182 | new_syms[i] = sym; | |
3183 | sym->udata.i = i + 1; | |
3184 | } | |
3185 | for (asect = abfd->sections; asect; asect = asect->next) | |
3186 | { | |
079e9a2f | 3187 | if (sect_syms[asect->index] == NULL) |
252b5132 | 3188 | { |
079e9a2f | 3189 | asymbol *sym = asect->symbol; |
dc810e39 | 3190 | unsigned int i; |
252b5132 | 3191 | |
079e9a2f | 3192 | sect_syms[asect->index] = sym; |
252b5132 RH |
3193 | if (!sym_is_global (abfd, sym)) |
3194 | i = num_locals2++; | |
3195 | else | |
3196 | i = num_locals + num_globals2++; | |
3197 | new_syms[i] = sym; | |
3198 | sym->udata.i = i + 1; | |
3199 | } | |
3200 | } | |
3201 | ||
3202 | bfd_set_symtab (abfd, new_syms, num_locals + num_globals); | |
3203 | ||
3204 | elf_num_locals (abfd) = num_locals; | |
3205 | elf_num_globals (abfd) = num_globals; | |
b34976b6 | 3206 | return TRUE; |
252b5132 RH |
3207 | } |
3208 | ||
3209 | /* Align to the maximum file alignment that could be required for any | |
3210 | ELF data structure. */ | |
3211 | ||
268b6b39 | 3212 | static inline file_ptr |
217aa764 | 3213 | align_file_position (file_ptr off, int align) |
252b5132 RH |
3214 | { |
3215 | return (off + align - 1) & ~(align - 1); | |
3216 | } | |
3217 | ||
3218 | /* Assign a file position to a section, optionally aligning to the | |
3219 | required section alignment. */ | |
3220 | ||
217aa764 AM |
3221 | file_ptr |
3222 | _bfd_elf_assign_file_position_for_section (Elf_Internal_Shdr *i_shdrp, | |
3223 | file_ptr offset, | |
3224 | bfd_boolean align) | |
252b5132 | 3225 | { |
72de5009 AM |
3226 | if (align && i_shdrp->sh_addralign > 1) |
3227 | offset = BFD_ALIGN (offset, i_shdrp->sh_addralign); | |
252b5132 RH |
3228 | i_shdrp->sh_offset = offset; |
3229 | if (i_shdrp->bfd_section != NULL) | |
3230 | i_shdrp->bfd_section->filepos = offset; | |
3231 | if (i_shdrp->sh_type != SHT_NOBITS) | |
3232 | offset += i_shdrp->sh_size; | |
3233 | return offset; | |
3234 | } | |
3235 | ||
3236 | /* Compute the file positions we are going to put the sections at, and | |
3237 | otherwise prepare to begin writing out the ELF file. If LINK_INFO | |
3238 | is not NULL, this is being called by the ELF backend linker. */ | |
3239 | ||
b34976b6 | 3240 | bfd_boolean |
217aa764 AM |
3241 | _bfd_elf_compute_section_file_positions (bfd *abfd, |
3242 | struct bfd_link_info *link_info) | |
252b5132 | 3243 | { |
9c5bfbb7 | 3244 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
b34976b6 | 3245 | bfd_boolean failed; |
4b6c0f2f | 3246 | struct bfd_strtab_hash *strtab = NULL; |
252b5132 | 3247 | Elf_Internal_Shdr *shstrtab_hdr; |
3516e984 | 3248 | bfd_boolean need_symtab; |
252b5132 RH |
3249 | |
3250 | if (abfd->output_has_begun) | |
b34976b6 | 3251 | return TRUE; |
252b5132 RH |
3252 | |
3253 | /* Do any elf backend specific processing first. */ | |
3254 | if (bed->elf_backend_begin_write_processing) | |
3255 | (*bed->elf_backend_begin_write_processing) (abfd, link_info); | |
3256 | ||
3257 | if (! prep_headers (abfd)) | |
b34976b6 | 3258 | return FALSE; |
252b5132 | 3259 | |
e6c51ed4 NC |
3260 | /* Post process the headers if necessary. */ |
3261 | if (bed->elf_backend_post_process_headers) | |
3262 | (*bed->elf_backend_post_process_headers) (abfd, link_info); | |
3263 | ||
b34976b6 | 3264 | failed = FALSE; |
252b5132 RH |
3265 | bfd_map_over_sections (abfd, elf_fake_sections, &failed); |
3266 | if (failed) | |
b34976b6 | 3267 | return FALSE; |
252b5132 | 3268 | |
da9f89d4 | 3269 | if (!assign_section_numbers (abfd, link_info)) |
b34976b6 | 3270 | return FALSE; |
252b5132 RH |
3271 | |
3272 | /* The backend linker builds symbol table information itself. */ | |
3516e984 L |
3273 | need_symtab = (link_info == NULL |
3274 | && (bfd_get_symcount (abfd) > 0 | |
3275 | || ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC)) | |
3276 | == HAS_RELOC))); | |
3277 | if (need_symtab) | |
252b5132 RH |
3278 | { |
3279 | /* Non-zero if doing a relocatable link. */ | |
3280 | int relocatable_p = ! (abfd->flags & (EXEC_P | DYNAMIC)); | |
3281 | ||
3282 | if (! swap_out_syms (abfd, &strtab, relocatable_p)) | |
b34976b6 | 3283 | return FALSE; |
252b5132 RH |
3284 | } |
3285 | ||
1126897b | 3286 | if (link_info == NULL) |
dbb410c3 | 3287 | { |
1126897b | 3288 | bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed); |
dbb410c3 | 3289 | if (failed) |
b34976b6 | 3290 | return FALSE; |
dbb410c3 AM |
3291 | } |
3292 | ||
252b5132 RH |
3293 | shstrtab_hdr = &elf_tdata (abfd)->shstrtab_hdr; |
3294 | /* sh_name was set in prep_headers. */ | |
3295 | shstrtab_hdr->sh_type = SHT_STRTAB; | |
3296 | shstrtab_hdr->sh_flags = 0; | |
3297 | shstrtab_hdr->sh_addr = 0; | |
2b0f7ef9 | 3298 | shstrtab_hdr->sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd)); |
252b5132 RH |
3299 | shstrtab_hdr->sh_entsize = 0; |
3300 | shstrtab_hdr->sh_link = 0; | |
3301 | shstrtab_hdr->sh_info = 0; | |
3302 | /* sh_offset is set in assign_file_positions_except_relocs. */ | |
3303 | shstrtab_hdr->sh_addralign = 1; | |
3304 | ||
c84fca4d | 3305 | if (!assign_file_positions_except_relocs (abfd, link_info)) |
b34976b6 | 3306 | return FALSE; |
252b5132 | 3307 | |
3516e984 | 3308 | if (need_symtab) |
252b5132 RH |
3309 | { |
3310 | file_ptr off; | |
3311 | Elf_Internal_Shdr *hdr; | |
3312 | ||
3313 | off = elf_tdata (abfd)->next_file_pos; | |
3314 | ||
3315 | hdr = &elf_tdata (abfd)->symtab_hdr; | |
b34976b6 | 3316 | off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE); |
252b5132 | 3317 | |
9ad5cbcf AM |
3318 | hdr = &elf_tdata (abfd)->symtab_shndx_hdr; |
3319 | if (hdr->sh_size != 0) | |
b34976b6 | 3320 | off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE); |
9ad5cbcf | 3321 | |
252b5132 | 3322 | hdr = &elf_tdata (abfd)->strtab_hdr; |
b34976b6 | 3323 | off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE); |
252b5132 RH |
3324 | |
3325 | elf_tdata (abfd)->next_file_pos = off; | |
3326 | ||
3327 | /* Now that we know where the .strtab section goes, write it | |
08a40648 | 3328 | out. */ |
252b5132 RH |
3329 | if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0 |
3330 | || ! _bfd_stringtab_emit (abfd, strtab)) | |
b34976b6 | 3331 | return FALSE; |
252b5132 RH |
3332 | _bfd_stringtab_free (strtab); |
3333 | } | |
3334 | ||
b34976b6 | 3335 | abfd->output_has_begun = TRUE; |
252b5132 | 3336 | |
b34976b6 | 3337 | return TRUE; |
252b5132 RH |
3338 | } |
3339 | ||
8ded5a0f AM |
3340 | /* Make an initial estimate of the size of the program header. If we |
3341 | get the number wrong here, we'll redo section placement. */ | |
3342 | ||
3343 | static bfd_size_type | |
3344 | get_program_header_size (bfd *abfd, struct bfd_link_info *info) | |
3345 | { | |
3346 | size_t segs; | |
3347 | asection *s; | |
2b05f1b7 | 3348 | const struct elf_backend_data *bed; |
8ded5a0f AM |
3349 | |
3350 | /* Assume we will need exactly two PT_LOAD segments: one for text | |
3351 | and one for data. */ | |
3352 | segs = 2; | |
3353 | ||
3354 | s = bfd_get_section_by_name (abfd, ".interp"); | |
3355 | if (s != NULL && (s->flags & SEC_LOAD) != 0) | |
3356 | { | |
3357 | /* If we have a loadable interpreter section, we need a | |
3358 | PT_INTERP segment. In this case, assume we also need a | |
3359 | PT_PHDR segment, although that may not be true for all | |
3360 | targets. */ | |
3361 | segs += 2; | |
3362 | } | |
3363 | ||
3364 | if (bfd_get_section_by_name (abfd, ".dynamic") != NULL) | |
3365 | { | |
3366 | /* We need a PT_DYNAMIC segment. */ | |
3367 | ++segs; | |
f210dcff | 3368 | } |
08a40648 | 3369 | |
ceae84aa | 3370 | if (info != NULL && info->relro) |
f210dcff L |
3371 | { |
3372 | /* We need a PT_GNU_RELRO segment. */ | |
3373 | ++segs; | |
8ded5a0f AM |
3374 | } |
3375 | ||
3376 | if (elf_tdata (abfd)->eh_frame_hdr) | |
3377 | { | |
3378 | /* We need a PT_GNU_EH_FRAME segment. */ | |
3379 | ++segs; | |
3380 | } | |
3381 | ||
2b05f1b7 | 3382 | if (elf_tdata (abfd)->stack_flags) |
8ded5a0f | 3383 | { |
2b05f1b7 L |
3384 | /* We need a PT_GNU_STACK segment. */ |
3385 | ++segs; | |
3386 | } | |
94b11780 | 3387 | |
2b05f1b7 L |
3388 | for (s = abfd->sections; s != NULL; s = s->next) |
3389 | { | |
8ded5a0f | 3390 | if ((s->flags & SEC_LOAD) != 0 |
0112cd26 | 3391 | && CONST_STRNEQ (s->name, ".note")) |
8ded5a0f AM |
3392 | { |
3393 | /* We need a PT_NOTE segment. */ | |
3394 | ++segs; | |
1c5265b5 JJ |
3395 | /* Try to create just one PT_NOTE segment |
3396 | for all adjacent loadable .note* sections. | |
3397 | gABI requires that within a PT_NOTE segment | |
3398 | (and also inside of each SHT_NOTE section) | |
3399 | each note is padded to a multiple of 4 size, | |
3400 | so we check whether the sections are correctly | |
3401 | aligned. */ | |
3402 | if (s->alignment_power == 2) | |
3403 | while (s->next != NULL | |
3404 | && s->next->alignment_power == 2 | |
3405 | && (s->next->flags & SEC_LOAD) != 0 | |
3406 | && CONST_STRNEQ (s->next->name, ".note")) | |
3407 | s = s->next; | |
8ded5a0f AM |
3408 | } |
3409 | } | |
3410 | ||
3411 | for (s = abfd->sections; s != NULL; s = s->next) | |
3412 | { | |
3413 | if (s->flags & SEC_THREAD_LOCAL) | |
3414 | { | |
3415 | /* We need a PT_TLS segment. */ | |
3416 | ++segs; | |
3417 | break; | |
3418 | } | |
3419 | } | |
3420 | ||
3421 | /* Let the backend count up any program headers it might need. */ | |
2b05f1b7 | 3422 | bed = get_elf_backend_data (abfd); |
8ded5a0f AM |
3423 | if (bed->elf_backend_additional_program_headers) |
3424 | { | |
3425 | int a; | |
3426 | ||
3427 | a = (*bed->elf_backend_additional_program_headers) (abfd, info); | |
3428 | if (a == -1) | |
3429 | abort (); | |
3430 | segs += a; | |
3431 | } | |
3432 | ||
3433 | return segs * bed->s->sizeof_phdr; | |
3434 | } | |
3435 | ||
2ea37f1c NC |
3436 | /* Find the segment that contains the output_section of section. */ |
3437 | ||
3438 | Elf_Internal_Phdr * | |
3439 | _bfd_elf_find_segment_containing_section (bfd * abfd, asection * section) | |
3440 | { | |
3441 | struct elf_segment_map *m; | |
3442 | Elf_Internal_Phdr *p; | |
3443 | ||
3444 | for (m = elf_tdata (abfd)->segment_map, | |
3445 | p = elf_tdata (abfd)->phdr; | |
3446 | m != NULL; | |
3447 | m = m->next, p++) | |
3448 | { | |
3449 | int i; | |
3450 | ||
3451 | for (i = m->count - 1; i >= 0; i--) | |
3452 | if (m->sections[i] == section) | |
3453 | return p; | |
3454 | } | |
3455 | ||
3456 | return NULL; | |
3457 | } | |
3458 | ||
252b5132 RH |
3459 | /* Create a mapping from a set of sections to a program segment. */ |
3460 | ||
217aa764 AM |
3461 | static struct elf_segment_map * |
3462 | make_mapping (bfd *abfd, | |
3463 | asection **sections, | |
3464 | unsigned int from, | |
3465 | unsigned int to, | |
3466 | bfd_boolean phdr) | |
252b5132 RH |
3467 | { |
3468 | struct elf_segment_map *m; | |
3469 | unsigned int i; | |
3470 | asection **hdrpp; | |
dc810e39 | 3471 | bfd_size_type amt; |
252b5132 | 3472 | |
dc810e39 AM |
3473 | amt = sizeof (struct elf_segment_map); |
3474 | amt += (to - from - 1) * sizeof (asection *); | |
217aa764 | 3475 | m = bfd_zalloc (abfd, amt); |
252b5132 RH |
3476 | if (m == NULL) |
3477 | return NULL; | |
3478 | m->next = NULL; | |
3479 | m->p_type = PT_LOAD; | |
3480 | for (i = from, hdrpp = sections + from; i < to; i++, hdrpp++) | |
3481 | m->sections[i - from] = *hdrpp; | |
3482 | m->count = to - from; | |
3483 | ||
3484 | if (from == 0 && phdr) | |
3485 | { | |
3486 | /* Include the headers in the first PT_LOAD segment. */ | |
3487 | m->includes_filehdr = 1; | |
3488 | m->includes_phdrs = 1; | |
3489 | } | |
3490 | ||
3491 | return m; | |
3492 | } | |
3493 | ||
229fcec5 MM |
3494 | /* Create the PT_DYNAMIC segment, which includes DYNSEC. Returns NULL |
3495 | on failure. */ | |
3496 | ||
3497 | struct elf_segment_map * | |
3498 | _bfd_elf_make_dynamic_segment (bfd *abfd, asection *dynsec) | |
3499 | { | |
3500 | struct elf_segment_map *m; | |
3501 | ||
3502 | m = bfd_zalloc (abfd, sizeof (struct elf_segment_map)); | |
3503 | if (m == NULL) | |
3504 | return NULL; | |
3505 | m->next = NULL; | |
3506 | m->p_type = PT_DYNAMIC; | |
3507 | m->count = 1; | |
3508 | m->sections[0] = dynsec; | |
08a40648 | 3509 | |
229fcec5 MM |
3510 | return m; |
3511 | } | |
3512 | ||
8ded5a0f | 3513 | /* Possibly add or remove segments from the segment map. */ |
252b5132 | 3514 | |
b34976b6 | 3515 | static bfd_boolean |
3dea8fca AM |
3516 | elf_modify_segment_map (bfd *abfd, |
3517 | struct bfd_link_info *info, | |
3518 | bfd_boolean remove_empty_load) | |
252b5132 | 3519 | { |
252e386e | 3520 | struct elf_segment_map **m; |
8ded5a0f | 3521 | const struct elf_backend_data *bed; |
252b5132 | 3522 | |
8ded5a0f AM |
3523 | /* The placement algorithm assumes that non allocated sections are |
3524 | not in PT_LOAD segments. We ensure this here by removing such | |
3525 | sections from the segment map. We also remove excluded | |
252e386e AM |
3526 | sections. Finally, any PT_LOAD segment without sections is |
3527 | removed. */ | |
3528 | m = &elf_tdata (abfd)->segment_map; | |
3529 | while (*m) | |
8ded5a0f AM |
3530 | { |
3531 | unsigned int i, new_count; | |
252b5132 | 3532 | |
252e386e | 3533 | for (new_count = 0, i = 0; i < (*m)->count; i++) |
8ded5a0f | 3534 | { |
252e386e AM |
3535 | if (((*m)->sections[i]->flags & SEC_EXCLUDE) == 0 |
3536 | && (((*m)->sections[i]->flags & SEC_ALLOC) != 0 | |
3537 | || (*m)->p_type != PT_LOAD)) | |
8ded5a0f | 3538 | { |
252e386e AM |
3539 | (*m)->sections[new_count] = (*m)->sections[i]; |
3540 | new_count++; | |
8ded5a0f AM |
3541 | } |
3542 | } | |
252e386e | 3543 | (*m)->count = new_count; |
252b5132 | 3544 | |
3dea8fca | 3545 | if (remove_empty_load && (*m)->p_type == PT_LOAD && (*m)->count == 0) |
252e386e AM |
3546 | *m = (*m)->next; |
3547 | else | |
3548 | m = &(*m)->next; | |
8ded5a0f | 3549 | } |
252b5132 | 3550 | |
8ded5a0f AM |
3551 | bed = get_elf_backend_data (abfd); |
3552 | if (bed->elf_backend_modify_segment_map != NULL) | |
252b5132 | 3553 | { |
252e386e | 3554 | if (!(*bed->elf_backend_modify_segment_map) (abfd, info)) |
8ded5a0f | 3555 | return FALSE; |
252b5132 | 3556 | } |
252b5132 | 3557 | |
8ded5a0f AM |
3558 | return TRUE; |
3559 | } | |
252b5132 | 3560 | |
8ded5a0f | 3561 | /* Set up a mapping from BFD sections to program segments. */ |
252b5132 | 3562 | |
8ded5a0f AM |
3563 | bfd_boolean |
3564 | _bfd_elf_map_sections_to_segments (bfd *abfd, struct bfd_link_info *info) | |
3565 | { | |
3566 | unsigned int count; | |
3567 | struct elf_segment_map *m; | |
3568 | asection **sections = NULL; | |
3569 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
3dea8fca | 3570 | bfd_boolean no_user_phdrs; |
252b5132 | 3571 | |
3dea8fca AM |
3572 | no_user_phdrs = elf_tdata (abfd)->segment_map == NULL; |
3573 | if (no_user_phdrs && bfd_count_sections (abfd) != 0) | |
252b5132 | 3574 | { |
8ded5a0f AM |
3575 | asection *s; |
3576 | unsigned int i; | |
3577 | struct elf_segment_map *mfirst; | |
3578 | struct elf_segment_map **pm; | |
3579 | asection *last_hdr; | |
3580 | bfd_vma last_size; | |
3581 | unsigned int phdr_index; | |
3582 | bfd_vma maxpagesize; | |
3583 | asection **hdrpp; | |
3584 | bfd_boolean phdr_in_segment = TRUE; | |
3585 | bfd_boolean writable; | |
3586 | int tls_count = 0; | |
3587 | asection *first_tls = NULL; | |
3588 | asection *dynsec, *eh_frame_hdr; | |
3589 | bfd_size_type amt; | |
252b5132 | 3590 | |
8ded5a0f | 3591 | /* Select the allocated sections, and sort them. */ |
252b5132 | 3592 | |
8ded5a0f AM |
3593 | sections = bfd_malloc2 (bfd_count_sections (abfd), sizeof (asection *)); |
3594 | if (sections == NULL) | |
252b5132 | 3595 | goto error_return; |
252b5132 | 3596 | |
8ded5a0f AM |
3597 | i = 0; |
3598 | for (s = abfd->sections; s != NULL; s = s->next) | |
3599 | { | |
3600 | if ((s->flags & SEC_ALLOC) != 0) | |
3601 | { | |
3602 | sections[i] = s; | |
3603 | ++i; | |
3604 | } | |
3605 | } | |
3606 | BFD_ASSERT (i <= bfd_count_sections (abfd)); | |
3607 | count = i; | |
252b5132 | 3608 | |
8ded5a0f | 3609 | qsort (sections, (size_t) count, sizeof (asection *), elf_sort_sections); |
252b5132 | 3610 | |
8ded5a0f | 3611 | /* Build the mapping. */ |
252b5132 | 3612 | |
8ded5a0f AM |
3613 | mfirst = NULL; |
3614 | pm = &mfirst; | |
252b5132 | 3615 | |
8ded5a0f AM |
3616 | /* If we have a .interp section, then create a PT_PHDR segment for |
3617 | the program headers and a PT_INTERP segment for the .interp | |
3618 | section. */ | |
3619 | s = bfd_get_section_by_name (abfd, ".interp"); | |
3620 | if (s != NULL && (s->flags & SEC_LOAD) != 0) | |
3621 | { | |
3622 | amt = sizeof (struct elf_segment_map); | |
3623 | m = bfd_zalloc (abfd, amt); | |
3624 | if (m == NULL) | |
3625 | goto error_return; | |
3626 | m->next = NULL; | |
3627 | m->p_type = PT_PHDR; | |
3628 | /* FIXME: UnixWare and Solaris set PF_X, Irix 5 does not. */ | |
3629 | m->p_flags = PF_R | PF_X; | |
3630 | m->p_flags_valid = 1; | |
3631 | m->includes_phdrs = 1; | |
252b5132 | 3632 | |
8ded5a0f AM |
3633 | *pm = m; |
3634 | pm = &m->next; | |
252b5132 | 3635 | |
8ded5a0f AM |
3636 | amt = sizeof (struct elf_segment_map); |
3637 | m = bfd_zalloc (abfd, amt); | |
3638 | if (m == NULL) | |
3639 | goto error_return; | |
3640 | m->next = NULL; | |
3641 | m->p_type = PT_INTERP; | |
3642 | m->count = 1; | |
3643 | m->sections[0] = s; | |
3644 | ||
3645 | *pm = m; | |
3646 | pm = &m->next; | |
252b5132 | 3647 | } |
8ded5a0f AM |
3648 | |
3649 | /* Look through the sections. We put sections in the same program | |
3650 | segment when the start of the second section can be placed within | |
3651 | a few bytes of the end of the first section. */ | |
3652 | last_hdr = NULL; | |
3653 | last_size = 0; | |
3654 | phdr_index = 0; | |
3655 | maxpagesize = bed->maxpagesize; | |
3656 | writable = FALSE; | |
3657 | dynsec = bfd_get_section_by_name (abfd, ".dynamic"); | |
3658 | if (dynsec != NULL | |
3659 | && (dynsec->flags & SEC_LOAD) == 0) | |
3660 | dynsec = NULL; | |
3661 | ||
3662 | /* Deal with -Ttext or something similar such that the first section | |
3663 | is not adjacent to the program headers. This is an | |
3664 | approximation, since at this point we don't know exactly how many | |
3665 | program headers we will need. */ | |
3666 | if (count > 0) | |
252b5132 | 3667 | { |
8ded5a0f AM |
3668 | bfd_size_type phdr_size = elf_tdata (abfd)->program_header_size; |
3669 | ||
62d7a5f6 | 3670 | if (phdr_size == (bfd_size_type) -1) |
8ded5a0f AM |
3671 | phdr_size = get_program_header_size (abfd, info); |
3672 | if ((abfd->flags & D_PAGED) == 0 | |
3673 | || sections[0]->lma < phdr_size | |
3674 | || sections[0]->lma % maxpagesize < phdr_size % maxpagesize) | |
3675 | phdr_in_segment = FALSE; | |
252b5132 RH |
3676 | } |
3677 | ||
8ded5a0f | 3678 | for (i = 0, hdrpp = sections; i < count; i++, hdrpp++) |
252b5132 | 3679 | { |
8ded5a0f AM |
3680 | asection *hdr; |
3681 | bfd_boolean new_segment; | |
3682 | ||
3683 | hdr = *hdrpp; | |
3684 | ||
3685 | /* See if this section and the last one will fit in the same | |
3686 | segment. */ | |
3687 | ||
3688 | if (last_hdr == NULL) | |
3689 | { | |
3690 | /* If we don't have a segment yet, then we don't need a new | |
3691 | one (we build the last one after this loop). */ | |
3692 | new_segment = FALSE; | |
3693 | } | |
3694 | else if (last_hdr->lma - last_hdr->vma != hdr->lma - hdr->vma) | |
3695 | { | |
3696 | /* If this section has a different relation between the | |
3697 | virtual address and the load address, then we need a new | |
3698 | segment. */ | |
3699 | new_segment = TRUE; | |
3700 | } | |
39948a60 NC |
3701 | /* In the next test we have to be careful when last_hdr->lma is close |
3702 | to the end of the address space. If the aligned address wraps | |
3703 | around to the start of the address space, then there are no more | |
3704 | pages left in memory and it is OK to assume that the current | |
3705 | section can be included in the current segment. */ | |
3706 | else if ((BFD_ALIGN (last_hdr->lma + last_size, maxpagesize) + maxpagesize | |
3707 | > last_hdr->lma) | |
3708 | && (BFD_ALIGN (last_hdr->lma + last_size, maxpagesize) + maxpagesize | |
4ff73856 | 3709 | <= hdr->lma)) |
8ded5a0f AM |
3710 | { |
3711 | /* If putting this section in this segment would force us to | |
3712 | skip a page in the segment, then we need a new segment. */ | |
3713 | new_segment = TRUE; | |
3714 | } | |
3715 | else if ((last_hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0 | |
3716 | && (hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) != 0) | |
3717 | { | |
3718 | /* We don't want to put a loadable section after a | |
3719 | nonloadable section in the same segment. | |
3720 | Consider .tbss sections as loadable for this purpose. */ | |
3721 | new_segment = TRUE; | |
3722 | } | |
3723 | else if ((abfd->flags & D_PAGED) == 0) | |
3724 | { | |
3725 | /* If the file is not demand paged, which means that we | |
3726 | don't require the sections to be correctly aligned in the | |
3727 | file, then there is no other reason for a new segment. */ | |
3728 | new_segment = FALSE; | |
3729 | } | |
3730 | else if (! writable | |
3731 | && (hdr->flags & SEC_READONLY) == 0 | |
3732 | && (((last_hdr->lma + last_size - 1) | |
3733 | & ~(maxpagesize - 1)) | |
3734 | != (hdr->lma & ~(maxpagesize - 1)))) | |
3735 | { | |
3736 | /* We don't want to put a writable section in a read only | |
3737 | segment, unless they are on the same page in memory | |
3738 | anyhow. We already know that the last section does not | |
3739 | bring us past the current section on the page, so the | |
3740 | only case in which the new section is not on the same | |
3741 | page as the previous section is when the previous section | |
3742 | ends precisely on a page boundary. */ | |
3743 | new_segment = TRUE; | |
3744 | } | |
3745 | else | |
3746 | { | |
3747 | /* Otherwise, we can use the same segment. */ | |
3748 | new_segment = FALSE; | |
3749 | } | |
3750 | ||
2889e75b | 3751 | /* Allow interested parties a chance to override our decision. */ |
ceae84aa AM |
3752 | if (last_hdr != NULL |
3753 | && info != NULL | |
3754 | && info->callbacks->override_segment_assignment != NULL) | |
3755 | new_segment | |
3756 | = info->callbacks->override_segment_assignment (info, abfd, hdr, | |
3757 | last_hdr, | |
3758 | new_segment); | |
2889e75b | 3759 | |
8ded5a0f AM |
3760 | if (! new_segment) |
3761 | { | |
3762 | if ((hdr->flags & SEC_READONLY) == 0) | |
3763 | writable = TRUE; | |
3764 | last_hdr = hdr; | |
3765 | /* .tbss sections effectively have zero size. */ | |
3766 | if ((hdr->flags & (SEC_THREAD_LOCAL | SEC_LOAD)) | |
3767 | != SEC_THREAD_LOCAL) | |
3768 | last_size = hdr->size; | |
3769 | else | |
3770 | last_size = 0; | |
3771 | continue; | |
3772 | } | |
3773 | ||
3774 | /* We need a new program segment. We must create a new program | |
3775 | header holding all the sections from phdr_index until hdr. */ | |
3776 | ||
3777 | m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment); | |
3778 | if (m == NULL) | |
3779 | goto error_return; | |
3780 | ||
3781 | *pm = m; | |
3782 | pm = &m->next; | |
3783 | ||
252b5132 | 3784 | if ((hdr->flags & SEC_READONLY) == 0) |
b34976b6 | 3785 | writable = TRUE; |
8ded5a0f AM |
3786 | else |
3787 | writable = FALSE; | |
3788 | ||
baaff79e JJ |
3789 | last_hdr = hdr; |
3790 | /* .tbss sections effectively have zero size. */ | |
e5caec89 | 3791 | if ((hdr->flags & (SEC_THREAD_LOCAL | SEC_LOAD)) != SEC_THREAD_LOCAL) |
eea6121a | 3792 | last_size = hdr->size; |
baaff79e JJ |
3793 | else |
3794 | last_size = 0; | |
8ded5a0f AM |
3795 | phdr_index = i; |
3796 | phdr_in_segment = FALSE; | |
252b5132 RH |
3797 | } |
3798 | ||
8ded5a0f AM |
3799 | /* Create a final PT_LOAD program segment. */ |
3800 | if (last_hdr != NULL) | |
3801 | { | |
3802 | m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment); | |
3803 | if (m == NULL) | |
3804 | goto error_return; | |
252b5132 | 3805 | |
8ded5a0f AM |
3806 | *pm = m; |
3807 | pm = &m->next; | |
3808 | } | |
252b5132 | 3809 | |
8ded5a0f AM |
3810 | /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */ |
3811 | if (dynsec != NULL) | |
3812 | { | |
3813 | m = _bfd_elf_make_dynamic_segment (abfd, dynsec); | |
3814 | if (m == NULL) | |
3815 | goto error_return; | |
3816 | *pm = m; | |
3817 | pm = &m->next; | |
3818 | } | |
252b5132 | 3819 | |
1c5265b5 JJ |
3820 | /* For each batch of consecutive loadable .note sections, |
3821 | add a PT_NOTE segment. We don't use bfd_get_section_by_name, | |
3822 | because if we link together nonloadable .note sections and | |
3823 | loadable .note sections, we will generate two .note sections | |
3824 | in the output file. FIXME: Using names for section types is | |
3825 | bogus anyhow. */ | |
8ded5a0f AM |
3826 | for (s = abfd->sections; s != NULL; s = s->next) |
3827 | { | |
3828 | if ((s->flags & SEC_LOAD) != 0 | |
0112cd26 | 3829 | && CONST_STRNEQ (s->name, ".note")) |
8ded5a0f | 3830 | { |
1c5265b5 JJ |
3831 | asection *s2; |
3832 | unsigned count = 1; | |
8ded5a0f | 3833 | amt = sizeof (struct elf_segment_map); |
1c5265b5 JJ |
3834 | if (s->alignment_power == 2) |
3835 | for (s2 = s; s2->next != NULL; s2 = s2->next) | |
55b581a6 JJ |
3836 | { |
3837 | if (s2->next->alignment_power == 2 | |
3838 | && (s2->next->flags & SEC_LOAD) != 0 | |
3839 | && CONST_STRNEQ (s2->next->name, ".note") | |
3840 | && align_power (s2->vma + s2->size, 2) | |
3841 | == s2->next->vma) | |
3842 | count++; | |
3843 | else | |
3844 | break; | |
3845 | } | |
1c5265b5 | 3846 | amt += (count - 1) * sizeof (asection *); |
8ded5a0f AM |
3847 | m = bfd_zalloc (abfd, amt); |
3848 | if (m == NULL) | |
3849 | goto error_return; | |
3850 | m->next = NULL; | |
3851 | m->p_type = PT_NOTE; | |
1c5265b5 JJ |
3852 | m->count = count; |
3853 | while (count > 1) | |
3854 | { | |
3855 | m->sections[m->count - count--] = s; | |
3856 | BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0); | |
3857 | s = s->next; | |
3858 | } | |
3859 | m->sections[m->count - 1] = s; | |
3860 | BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0); | |
8ded5a0f AM |
3861 | *pm = m; |
3862 | pm = &m->next; | |
3863 | } | |
3864 | if (s->flags & SEC_THREAD_LOCAL) | |
3865 | { | |
3866 | if (! tls_count) | |
3867 | first_tls = s; | |
3868 | tls_count++; | |
3869 | } | |
3870 | } | |
252b5132 | 3871 | |
8ded5a0f AM |
3872 | /* If there are any SHF_TLS output sections, add PT_TLS segment. */ |
3873 | if (tls_count > 0) | |
3874 | { | |
3875 | int i; | |
252b5132 | 3876 | |
8ded5a0f AM |
3877 | amt = sizeof (struct elf_segment_map); |
3878 | amt += (tls_count - 1) * sizeof (asection *); | |
3879 | m = bfd_zalloc (abfd, amt); | |
3880 | if (m == NULL) | |
3881 | goto error_return; | |
3882 | m->next = NULL; | |
3883 | m->p_type = PT_TLS; | |
3884 | m->count = tls_count; | |
3885 | /* Mandated PF_R. */ | |
3886 | m->p_flags = PF_R; | |
3887 | m->p_flags_valid = 1; | |
3888 | for (i = 0; i < tls_count; ++i) | |
3889 | { | |
3890 | BFD_ASSERT (first_tls->flags & SEC_THREAD_LOCAL); | |
3891 | m->sections[i] = first_tls; | |
3892 | first_tls = first_tls->next; | |
3893 | } | |
252b5132 | 3894 | |
8ded5a0f AM |
3895 | *pm = m; |
3896 | pm = &m->next; | |
3897 | } | |
252b5132 | 3898 | |
8ded5a0f AM |
3899 | /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME |
3900 | segment. */ | |
3901 | eh_frame_hdr = elf_tdata (abfd)->eh_frame_hdr; | |
3902 | if (eh_frame_hdr != NULL | |
3903 | && (eh_frame_hdr->output_section->flags & SEC_LOAD) != 0) | |
252b5132 | 3904 | { |
dc810e39 | 3905 | amt = sizeof (struct elf_segment_map); |
217aa764 | 3906 | m = bfd_zalloc (abfd, amt); |
252b5132 RH |
3907 | if (m == NULL) |
3908 | goto error_return; | |
3909 | m->next = NULL; | |
8ded5a0f | 3910 | m->p_type = PT_GNU_EH_FRAME; |
252b5132 | 3911 | m->count = 1; |
8ded5a0f | 3912 | m->sections[0] = eh_frame_hdr->output_section; |
252b5132 RH |
3913 | |
3914 | *pm = m; | |
3915 | pm = &m->next; | |
3916 | } | |
13ae64f3 | 3917 | |
8ded5a0f | 3918 | if (elf_tdata (abfd)->stack_flags) |
13ae64f3 | 3919 | { |
8ded5a0f AM |
3920 | amt = sizeof (struct elf_segment_map); |
3921 | m = bfd_zalloc (abfd, amt); | |
3922 | if (m == NULL) | |
3923 | goto error_return; | |
3924 | m->next = NULL; | |
2b05f1b7 | 3925 | m->p_type = PT_GNU_STACK; |
8ded5a0f AM |
3926 | m->p_flags = elf_tdata (abfd)->stack_flags; |
3927 | m->p_flags_valid = 1; | |
252b5132 | 3928 | |
8ded5a0f AM |
3929 | *pm = m; |
3930 | pm = &m->next; | |
3931 | } | |
65765700 | 3932 | |
ceae84aa | 3933 | if (info != NULL && info->relro) |
8ded5a0f | 3934 | { |
f210dcff L |
3935 | for (m = mfirst; m != NULL; m = m->next) |
3936 | { | |
3937 | if (m->p_type == PT_LOAD) | |
3938 | { | |
3939 | asection *last = m->sections[m->count - 1]; | |
3940 | bfd_vma vaddr = m->sections[0]->vma; | |
3941 | bfd_vma filesz = last->vma - vaddr + last->size; | |
65765700 | 3942 | |
f210dcff L |
3943 | if (vaddr < info->relro_end |
3944 | && vaddr >= info->relro_start | |
3945 | && (vaddr + filesz) >= info->relro_end) | |
3946 | break; | |
3947 | } | |
3948 | } | |
3949 | ||
3950 | /* Make a PT_GNU_RELRO segment only when it isn't empty. */ | |
3951 | if (m != NULL) | |
3952 | { | |
3953 | amt = sizeof (struct elf_segment_map); | |
3954 | m = bfd_zalloc (abfd, amt); | |
3955 | if (m == NULL) | |
3956 | goto error_return; | |
3957 | m->next = NULL; | |
3958 | m->p_type = PT_GNU_RELRO; | |
3959 | m->p_flags = PF_R; | |
3960 | m->p_flags_valid = 1; | |
3961 | ||
3962 | *pm = m; | |
3963 | pm = &m->next; | |
3964 | } | |
8ded5a0f | 3965 | } |
9ee5e499 | 3966 | |
8ded5a0f AM |
3967 | free (sections); |
3968 | elf_tdata (abfd)->segment_map = mfirst; | |
9ee5e499 JJ |
3969 | } |
3970 | ||
3dea8fca | 3971 | if (!elf_modify_segment_map (abfd, info, no_user_phdrs)) |
8ded5a0f | 3972 | return FALSE; |
8c37241b | 3973 | |
8ded5a0f AM |
3974 | for (count = 0, m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next) |
3975 | ++count; | |
3976 | elf_tdata (abfd)->program_header_size = count * bed->s->sizeof_phdr; | |
252b5132 | 3977 | |
b34976b6 | 3978 | return TRUE; |
252b5132 RH |
3979 | |
3980 | error_return: | |
3981 | if (sections != NULL) | |
3982 | free (sections); | |
b34976b6 | 3983 | return FALSE; |
252b5132 RH |
3984 | } |
3985 | ||
3986 | /* Sort sections by address. */ | |
3987 | ||
3988 | static int | |
217aa764 | 3989 | elf_sort_sections (const void *arg1, const void *arg2) |
252b5132 RH |
3990 | { |
3991 | const asection *sec1 = *(const asection **) arg1; | |
3992 | const asection *sec2 = *(const asection **) arg2; | |
eecdbe52 | 3993 | bfd_size_type size1, size2; |
252b5132 RH |
3994 | |
3995 | /* Sort by LMA first, since this is the address used to | |
3996 | place the section into a segment. */ | |
3997 | if (sec1->lma < sec2->lma) | |
3998 | return -1; | |
3999 | else if (sec1->lma > sec2->lma) | |
4000 | return 1; | |
4001 | ||
4002 | /* Then sort by VMA. Normally the LMA and the VMA will be | |
4003 | the same, and this will do nothing. */ | |
4004 | if (sec1->vma < sec2->vma) | |
4005 | return -1; | |
4006 | else if (sec1->vma > sec2->vma) | |
4007 | return 1; | |
4008 | ||
4009 | /* Put !SEC_LOAD sections after SEC_LOAD ones. */ | |
4010 | ||
07c6e936 | 4011 | #define TOEND(x) (((x)->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0) |
252b5132 RH |
4012 | |
4013 | if (TOEND (sec1)) | |
4014 | { | |
4015 | if (TOEND (sec2)) | |
00a7cdc5 NC |
4016 | { |
4017 | /* If the indicies are the same, do not return 0 | |
4018 | here, but continue to try the next comparison. */ | |
4019 | if (sec1->target_index - sec2->target_index != 0) | |
4020 | return sec1->target_index - sec2->target_index; | |
4021 | } | |
252b5132 RH |
4022 | else |
4023 | return 1; | |
4024 | } | |
00a7cdc5 | 4025 | else if (TOEND (sec2)) |
252b5132 RH |
4026 | return -1; |
4027 | ||
4028 | #undef TOEND | |
4029 | ||
00a7cdc5 NC |
4030 | /* Sort by size, to put zero sized sections |
4031 | before others at the same address. */ | |
252b5132 | 4032 | |
eea6121a AM |
4033 | size1 = (sec1->flags & SEC_LOAD) ? sec1->size : 0; |
4034 | size2 = (sec2->flags & SEC_LOAD) ? sec2->size : 0; | |
eecdbe52 JJ |
4035 | |
4036 | if (size1 < size2) | |
252b5132 | 4037 | return -1; |
eecdbe52 | 4038 | if (size1 > size2) |
252b5132 RH |
4039 | return 1; |
4040 | ||
4041 | return sec1->target_index - sec2->target_index; | |
4042 | } | |
4043 | ||
340b6d91 AC |
4044 | /* Ian Lance Taylor writes: |
4045 | ||
4046 | We shouldn't be using % with a negative signed number. That's just | |
4047 | not good. We have to make sure either that the number is not | |
4048 | negative, or that the number has an unsigned type. When the types | |
4049 | are all the same size they wind up as unsigned. When file_ptr is a | |
4050 | larger signed type, the arithmetic winds up as signed long long, | |
4051 | which is wrong. | |
4052 | ||
4053 | What we're trying to say here is something like ``increase OFF by | |
4054 | the least amount that will cause it to be equal to the VMA modulo | |
4055 | the page size.'' */ | |
4056 | /* In other words, something like: | |
4057 | ||
4058 | vma_offset = m->sections[0]->vma % bed->maxpagesize; | |
4059 | off_offset = off % bed->maxpagesize; | |
4060 | if (vma_offset < off_offset) | |
4061 | adjustment = vma_offset + bed->maxpagesize - off_offset; | |
4062 | else | |
4063 | adjustment = vma_offset - off_offset; | |
08a40648 | 4064 | |
340b6d91 AC |
4065 | which can can be collapsed into the expression below. */ |
4066 | ||
4067 | static file_ptr | |
4068 | vma_page_aligned_bias (bfd_vma vma, ufile_ptr off, bfd_vma maxpagesize) | |
4069 | { | |
4070 | return ((vma - off) % maxpagesize); | |
4071 | } | |
4072 | ||
6d33f217 L |
4073 | static void |
4074 | print_segment_map (const struct elf_segment_map *m) | |
4075 | { | |
4076 | unsigned int j; | |
4077 | const char *pt = get_segment_type (m->p_type); | |
4078 | char buf[32]; | |
4079 | ||
4080 | if (pt == NULL) | |
4081 | { | |
4082 | if (m->p_type >= PT_LOPROC && m->p_type <= PT_HIPROC) | |
4083 | sprintf (buf, "LOPROC+%7.7x", | |
4084 | (unsigned int) (m->p_type - PT_LOPROC)); | |
4085 | else if (m->p_type >= PT_LOOS && m->p_type <= PT_HIOS) | |
4086 | sprintf (buf, "LOOS+%7.7x", | |
4087 | (unsigned int) (m->p_type - PT_LOOS)); | |
4088 | else | |
4089 | snprintf (buf, sizeof (buf), "%8.8x", | |
4090 | (unsigned int) m->p_type); | |
4091 | pt = buf; | |
4092 | } | |
4093 | fprintf (stderr, "%s:", pt); | |
4094 | for (j = 0; j < m->count; j++) | |
4095 | fprintf (stderr, " %s", m->sections [j]->name); | |
4096 | putc ('\n',stderr); | |
4097 | } | |
4098 | ||
252b5132 RH |
4099 | /* Assign file positions to the sections based on the mapping from |
4100 | sections to segments. This function also sets up some fields in | |
f3520d2f | 4101 | the file header. */ |
252b5132 | 4102 | |
b34976b6 | 4103 | static bfd_boolean |
f3520d2f AM |
4104 | assign_file_positions_for_load_sections (bfd *abfd, |
4105 | struct bfd_link_info *link_info) | |
252b5132 RH |
4106 | { |
4107 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
252b5132 | 4108 | struct elf_segment_map *m; |
252b5132 | 4109 | Elf_Internal_Phdr *phdrs; |
252b5132 | 4110 | Elf_Internal_Phdr *p; |
02bf8d82 | 4111 | file_ptr off; |
3f570048 | 4112 | bfd_size_type maxpagesize; |
f3520d2f | 4113 | unsigned int alloc; |
0920dee7 | 4114 | unsigned int i, j; |
2b0bc088 | 4115 | bfd_vma header_pad = 0; |
252b5132 | 4116 | |
e36284ab | 4117 | if (link_info == NULL |
ceae84aa | 4118 | && !_bfd_elf_map_sections_to_segments (abfd, link_info)) |
8ded5a0f | 4119 | return FALSE; |
252b5132 | 4120 | |
8ded5a0f | 4121 | alloc = 0; |
252b5132 | 4122 | for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next) |
2b0bc088 NC |
4123 | { |
4124 | ++alloc; | |
4125 | if (m->header_size) | |
4126 | header_pad = m->header_size; | |
4127 | } | |
252b5132 RH |
4128 | |
4129 | elf_elfheader (abfd)->e_phoff = bed->s->sizeof_ehdr; | |
4130 | elf_elfheader (abfd)->e_phentsize = bed->s->sizeof_phdr; | |
8ded5a0f | 4131 | elf_elfheader (abfd)->e_phnum = alloc; |
252b5132 | 4132 | |
62d7a5f6 | 4133 | if (elf_tdata (abfd)->program_header_size == (bfd_size_type) -1) |
8ded5a0f AM |
4134 | elf_tdata (abfd)->program_header_size = alloc * bed->s->sizeof_phdr; |
4135 | else | |
4136 | BFD_ASSERT (elf_tdata (abfd)->program_header_size | |
59e0647f | 4137 | >= alloc * bed->s->sizeof_phdr); |
252b5132 RH |
4138 | |
4139 | if (alloc == 0) | |
f3520d2f | 4140 | { |
8ded5a0f AM |
4141 | elf_tdata (abfd)->next_file_pos = bed->s->sizeof_ehdr; |
4142 | return TRUE; | |
f3520d2f | 4143 | } |
252b5132 | 4144 | |
57268894 HPN |
4145 | /* We're writing the size in elf_tdata (abfd)->program_header_size, |
4146 | see assign_file_positions_except_relocs, so make sure we have | |
4147 | that amount allocated, with trailing space cleared. | |
4148 | The variable alloc contains the computed need, while elf_tdata | |
4149 | (abfd)->program_header_size contains the size used for the | |
4150 | layout. | |
4151 | See ld/emultempl/elf-generic.em:gld${EMULATION_NAME}_map_segments | |
4152 | where the layout is forced to according to a larger size in the | |
4153 | last iterations for the testcase ld-elf/header. */ | |
4154 | BFD_ASSERT (elf_tdata (abfd)->program_header_size % bed->s->sizeof_phdr | |
4155 | == 0); | |
4156 | phdrs = bfd_zalloc2 (abfd, | |
4157 | (elf_tdata (abfd)->program_header_size | |
4158 | / bed->s->sizeof_phdr), | |
4159 | sizeof (Elf_Internal_Phdr)); | |
f3520d2f | 4160 | elf_tdata (abfd)->phdr = phdrs; |
252b5132 | 4161 | if (phdrs == NULL) |
b34976b6 | 4162 | return FALSE; |
252b5132 | 4163 | |
3f570048 AM |
4164 | maxpagesize = 1; |
4165 | if ((abfd->flags & D_PAGED) != 0) | |
4166 | maxpagesize = bed->maxpagesize; | |
4167 | ||
252b5132 RH |
4168 | off = bed->s->sizeof_ehdr; |
4169 | off += alloc * bed->s->sizeof_phdr; | |
2b0bc088 NC |
4170 | if (header_pad < (bfd_vma) off) |
4171 | header_pad = 0; | |
4172 | else | |
4173 | header_pad -= off; | |
4174 | off += header_pad; | |
252b5132 | 4175 | |
0920dee7 | 4176 | for (m = elf_tdata (abfd)->segment_map, p = phdrs, j = 0; |
252b5132 | 4177 | m != NULL; |
0920dee7 | 4178 | m = m->next, p++, j++) |
252b5132 | 4179 | { |
252b5132 | 4180 | asection **secpp; |
bf988460 AM |
4181 | bfd_vma off_adjust; |
4182 | bfd_boolean no_contents; | |
252b5132 RH |
4183 | |
4184 | /* If elf_segment_map is not from map_sections_to_segments, the | |
08a40648 | 4185 | sections may not be correctly ordered. NOTE: sorting should |
52e9b619 MS |
4186 | not be done to the PT_NOTE section of a corefile, which may |
4187 | contain several pseudo-sections artificially created by bfd. | |
4188 | Sorting these pseudo-sections breaks things badly. */ | |
47d9a591 AM |
4189 | if (m->count > 1 |
4190 | && !(elf_elfheader (abfd)->e_type == ET_CORE | |
52e9b619 | 4191 | && m->p_type == PT_NOTE)) |
252b5132 RH |
4192 | qsort (m->sections, (size_t) m->count, sizeof (asection *), |
4193 | elf_sort_sections); | |
4194 | ||
b301b248 AM |
4195 | /* An ELF segment (described by Elf_Internal_Phdr) may contain a |
4196 | number of sections with contents contributing to both p_filesz | |
4197 | and p_memsz, followed by a number of sections with no contents | |
4198 | that just contribute to p_memsz. In this loop, OFF tracks next | |
02bf8d82 | 4199 | available file offset for PT_LOAD and PT_NOTE segments. */ |
252b5132 | 4200 | p->p_type = m->p_type; |
28a7f3e7 | 4201 | p->p_flags = m->p_flags; |
252b5132 | 4202 | |
3f570048 AM |
4203 | if (m->count == 0) |
4204 | p->p_vaddr = 0; | |
4205 | else | |
3271a814 | 4206 | p->p_vaddr = m->sections[0]->vma - m->p_vaddr_offset; |
3f570048 AM |
4207 | |
4208 | if (m->p_paddr_valid) | |
4209 | p->p_paddr = m->p_paddr; | |
4210 | else if (m->count == 0) | |
4211 | p->p_paddr = 0; | |
4212 | else | |
08a40648 | 4213 | p->p_paddr = m->sections[0]->lma - m->p_vaddr_offset; |
3f570048 AM |
4214 | |
4215 | if (p->p_type == PT_LOAD | |
4216 | && (abfd->flags & D_PAGED) != 0) | |
4217 | { | |
4218 | /* p_align in demand paged PT_LOAD segments effectively stores | |
4219 | the maximum page size. When copying an executable with | |
4220 | objcopy, we set m->p_align from the input file. Use this | |
4221 | value for maxpagesize rather than bed->maxpagesize, which | |
4222 | may be different. Note that we use maxpagesize for PT_TLS | |
4223 | segment alignment later in this function, so we are relying | |
4224 | on at least one PT_LOAD segment appearing before a PT_TLS | |
4225 | segment. */ | |
4226 | if (m->p_align_valid) | |
4227 | maxpagesize = m->p_align; | |
4228 | ||
4229 | p->p_align = maxpagesize; | |
4230 | } | |
3271a814 NS |
4231 | else if (m->p_align_valid) |
4232 | p->p_align = m->p_align; | |
e970b90a DJ |
4233 | else if (m->count == 0) |
4234 | p->p_align = 1 << bed->s->log_file_align; | |
3f570048 AM |
4235 | else |
4236 | p->p_align = 0; | |
4237 | ||
bf988460 AM |
4238 | no_contents = FALSE; |
4239 | off_adjust = 0; | |
252b5132 | 4240 | if (p->p_type == PT_LOAD |
b301b248 | 4241 | && m->count > 0) |
252b5132 | 4242 | { |
b301b248 | 4243 | bfd_size_type align; |
a49e53ed | 4244 | unsigned int align_power = 0; |
b301b248 | 4245 | |
3271a814 NS |
4246 | if (m->p_align_valid) |
4247 | align = p->p_align; | |
4248 | else | |
252b5132 | 4249 | { |
3271a814 NS |
4250 | for (i = 0, secpp = m->sections; i < m->count; i++, secpp++) |
4251 | { | |
4252 | unsigned int secalign; | |
08a40648 | 4253 | |
3271a814 NS |
4254 | secalign = bfd_get_section_alignment (abfd, *secpp); |
4255 | if (secalign > align_power) | |
4256 | align_power = secalign; | |
4257 | } | |
4258 | align = (bfd_size_type) 1 << align_power; | |
4259 | if (align < maxpagesize) | |
4260 | align = maxpagesize; | |
b301b248 | 4261 | } |
252b5132 | 4262 | |
02bf8d82 AM |
4263 | for (i = 0; i < m->count; i++) |
4264 | if ((m->sections[i]->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0) | |
4265 | /* If we aren't making room for this section, then | |
4266 | it must be SHT_NOBITS regardless of what we've | |
4267 | set via struct bfd_elf_special_section. */ | |
4268 | elf_section_type (m->sections[i]) = SHT_NOBITS; | |
4269 | ||
bf988460 | 4270 | /* Find out whether this segment contains any loadable |
aea274d3 AM |
4271 | sections. */ |
4272 | no_contents = TRUE; | |
4273 | for (i = 0; i < m->count; i++) | |
4274 | if (elf_section_type (m->sections[i]) != SHT_NOBITS) | |
4275 | { | |
4276 | no_contents = FALSE; | |
4277 | break; | |
4278 | } | |
bf988460 AM |
4279 | |
4280 | off_adjust = vma_page_aligned_bias (m->sections[0]->vma, off, align); | |
4281 | off += off_adjust; | |
4282 | if (no_contents) | |
4283 | { | |
4284 | /* We shouldn't need to align the segment on disk since | |
4285 | the segment doesn't need file space, but the gABI | |
4286 | arguably requires the alignment and glibc ld.so | |
4287 | checks it. So to comply with the alignment | |
4288 | requirement but not waste file space, we adjust | |
4289 | p_offset for just this segment. (OFF_ADJUST is | |
4290 | subtracted from OFF later.) This may put p_offset | |
4291 | past the end of file, but that shouldn't matter. */ | |
4292 | } | |
4293 | else | |
4294 | off_adjust = 0; | |
252b5132 | 4295 | } |
b1a6d0b1 NC |
4296 | /* Make sure the .dynamic section is the first section in the |
4297 | PT_DYNAMIC segment. */ | |
4298 | else if (p->p_type == PT_DYNAMIC | |
4299 | && m->count > 1 | |
4300 | && strcmp (m->sections[0]->name, ".dynamic") != 0) | |
4301 | { | |
4302 | _bfd_error_handler | |
b301b248 AM |
4303 | (_("%B: The first section in the PT_DYNAMIC segment is not the .dynamic section"), |
4304 | abfd); | |
b1a6d0b1 NC |
4305 | bfd_set_error (bfd_error_bad_value); |
4306 | return FALSE; | |
4307 | } | |
3f001e84 JK |
4308 | /* Set the note section type to SHT_NOTE. */ |
4309 | else if (p->p_type == PT_NOTE) | |
4310 | for (i = 0; i < m->count; i++) | |
4311 | elf_section_type (m->sections[i]) = SHT_NOTE; | |
252b5132 | 4312 | |
252b5132 RH |
4313 | p->p_offset = 0; |
4314 | p->p_filesz = 0; | |
4315 | p->p_memsz = 0; | |
4316 | ||
4317 | if (m->includes_filehdr) | |
4318 | { | |
bf988460 | 4319 | if (!m->p_flags_valid) |
252b5132 | 4320 | p->p_flags |= PF_R; |
252b5132 RH |
4321 | p->p_filesz = bed->s->sizeof_ehdr; |
4322 | p->p_memsz = bed->s->sizeof_ehdr; | |
4323 | if (m->count > 0) | |
4324 | { | |
4325 | BFD_ASSERT (p->p_type == PT_LOAD); | |
4326 | ||
4327 | if (p->p_vaddr < (bfd_vma) off) | |
4328 | { | |
caf47ea6 | 4329 | (*_bfd_error_handler) |
b301b248 AM |
4330 | (_("%B: Not enough room for program headers, try linking with -N"), |
4331 | abfd); | |
252b5132 | 4332 | bfd_set_error (bfd_error_bad_value); |
b34976b6 | 4333 | return FALSE; |
252b5132 RH |
4334 | } |
4335 | ||
4336 | p->p_vaddr -= off; | |
bf988460 | 4337 | if (!m->p_paddr_valid) |
252b5132 RH |
4338 | p->p_paddr -= off; |
4339 | } | |
252b5132 RH |
4340 | } |
4341 | ||
4342 | if (m->includes_phdrs) | |
4343 | { | |
bf988460 | 4344 | if (!m->p_flags_valid) |
252b5132 RH |
4345 | p->p_flags |= PF_R; |
4346 | ||
f3520d2f | 4347 | if (!m->includes_filehdr) |
252b5132 RH |
4348 | { |
4349 | p->p_offset = bed->s->sizeof_ehdr; | |
4350 | ||
4351 | if (m->count > 0) | |
4352 | { | |
4353 | BFD_ASSERT (p->p_type == PT_LOAD); | |
4354 | p->p_vaddr -= off - p->p_offset; | |
bf988460 | 4355 | if (!m->p_paddr_valid) |
252b5132 RH |
4356 | p->p_paddr -= off - p->p_offset; |
4357 | } | |
252b5132 RH |
4358 | } |
4359 | ||
4360 | p->p_filesz += alloc * bed->s->sizeof_phdr; | |
4361 | p->p_memsz += alloc * bed->s->sizeof_phdr; | |
2b0bc088 NC |
4362 | if (m->count) |
4363 | { | |
4364 | p->p_filesz += header_pad; | |
4365 | p->p_memsz += header_pad; | |
4366 | } | |
252b5132 RH |
4367 | } |
4368 | ||
4369 | if (p->p_type == PT_LOAD | |
4370 | || (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core)) | |
4371 | { | |
bf988460 | 4372 | if (!m->includes_filehdr && !m->includes_phdrs) |
02bf8d82 | 4373 | p->p_offset = off; |
252b5132 RH |
4374 | else |
4375 | { | |
4376 | file_ptr adjust; | |
4377 | ||
4378 | adjust = off - (p->p_offset + p->p_filesz); | |
bf988460 AM |
4379 | if (!no_contents) |
4380 | p->p_filesz += adjust; | |
252b5132 RH |
4381 | p->p_memsz += adjust; |
4382 | } | |
4383 | } | |
4384 | ||
1ea63fd2 AM |
4385 | /* Set up p_filesz, p_memsz, p_align and p_flags from the section |
4386 | maps. Set filepos for sections in PT_LOAD segments, and in | |
4387 | core files, for sections in PT_NOTE segments. | |
4388 | assign_file_positions_for_non_load_sections will set filepos | |
4389 | for other sections and update p_filesz for other segments. */ | |
252b5132 RH |
4390 | for (i = 0, secpp = m->sections; i < m->count; i++, secpp++) |
4391 | { | |
4392 | asection *sec; | |
252b5132 | 4393 | bfd_size_type align; |
627b32bc | 4394 | Elf_Internal_Shdr *this_hdr; |
252b5132 RH |
4395 | |
4396 | sec = *secpp; | |
02bf8d82 | 4397 | this_hdr = &elf_section_data (sec)->this_hdr; |
3f570048 | 4398 | align = (bfd_size_type) 1 << bfd_get_section_alignment (abfd, sec); |
252b5132 | 4399 | |
88967714 AM |
4400 | if ((p->p_type == PT_LOAD |
4401 | || p->p_type == PT_TLS) | |
4402 | && (this_hdr->sh_type != SHT_NOBITS | |
4403 | || ((this_hdr->sh_flags & SHF_ALLOC) != 0 | |
4404 | && ((this_hdr->sh_flags & SHF_TLS) == 0 | |
4405 | || p->p_type == PT_TLS)))) | |
252b5132 | 4406 | { |
11701589 | 4407 | bfd_signed_vma adjust = sec->vma - (p->p_vaddr + p->p_memsz); |
252b5132 | 4408 | |
88967714 | 4409 | if (adjust < 0) |
252b5132 | 4410 | { |
88967714 | 4411 | (*_bfd_error_handler) |
11701589 | 4412 | (_("%B: section %A vma 0x%lx overlaps previous sections"), |
37c43c55 | 4413 | abfd, sec, (unsigned long) sec->vma); |
88967714 AM |
4414 | adjust = 0; |
4415 | } | |
4416 | p->p_memsz += adjust; | |
0e922b77 | 4417 | |
88967714 AM |
4418 | if (this_hdr->sh_type != SHT_NOBITS) |
4419 | { | |
4420 | off += adjust; | |
4421 | p->p_filesz += adjust; | |
252b5132 | 4422 | } |
252b5132 RH |
4423 | } |
4424 | ||
4425 | if (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core) | |
4426 | { | |
b301b248 AM |
4427 | /* The section at i == 0 is the one that actually contains |
4428 | everything. */ | |
4a938328 MS |
4429 | if (i == 0) |
4430 | { | |
627b32bc | 4431 | this_hdr->sh_offset = sec->filepos = off; |
6a3cd2b4 AM |
4432 | off += this_hdr->sh_size; |
4433 | p->p_filesz = this_hdr->sh_size; | |
b301b248 AM |
4434 | p->p_memsz = 0; |
4435 | p->p_align = 1; | |
252b5132 | 4436 | } |
4a938328 | 4437 | else |
252b5132 | 4438 | { |
b301b248 | 4439 | /* The rest are fake sections that shouldn't be written. */ |
252b5132 | 4440 | sec->filepos = 0; |
eea6121a | 4441 | sec->size = 0; |
b301b248 AM |
4442 | sec->flags = 0; |
4443 | continue; | |
252b5132 | 4444 | } |
252b5132 RH |
4445 | } |
4446 | else | |
4447 | { | |
b301b248 AM |
4448 | if (p->p_type == PT_LOAD) |
4449 | { | |
02bf8d82 AM |
4450 | this_hdr->sh_offset = sec->filepos = off; |
4451 | if (this_hdr->sh_type != SHT_NOBITS) | |
6a3cd2b4 | 4452 | off += this_hdr->sh_size; |
b301b248 | 4453 | } |
252b5132 | 4454 | |
02bf8d82 | 4455 | if (this_hdr->sh_type != SHT_NOBITS) |
b301b248 | 4456 | { |
6a3cd2b4 | 4457 | p->p_filesz += this_hdr->sh_size; |
02bf8d82 AM |
4458 | /* A load section without SHF_ALLOC is something like |
4459 | a note section in a PT_NOTE segment. These take | |
4460 | file space but are not loaded into memory. */ | |
4461 | if ((this_hdr->sh_flags & SHF_ALLOC) != 0) | |
6a3cd2b4 | 4462 | p->p_memsz += this_hdr->sh_size; |
b301b248 | 4463 | } |
6a3cd2b4 | 4464 | else if ((this_hdr->sh_flags & SHF_ALLOC) != 0) |
13ae64f3 | 4465 | { |
6a3cd2b4 AM |
4466 | if (p->p_type == PT_TLS) |
4467 | p->p_memsz += this_hdr->sh_size; | |
4468 | ||
4469 | /* .tbss is special. It doesn't contribute to p_memsz of | |
4470 | normal segments. */ | |
4471 | else if ((this_hdr->sh_flags & SHF_TLS) == 0) | |
4472 | p->p_memsz += this_hdr->sh_size; | |
13ae64f3 JJ |
4473 | } |
4474 | ||
b10a8ae0 L |
4475 | if (align > p->p_align |
4476 | && !m->p_align_valid | |
4477 | && (p->p_type != PT_LOAD | |
4478 | || (abfd->flags & D_PAGED) == 0)) | |
252b5132 RH |
4479 | p->p_align = align; |
4480 | } | |
4481 | ||
bf988460 | 4482 | if (!m->p_flags_valid) |
252b5132 RH |
4483 | { |
4484 | p->p_flags |= PF_R; | |
02bf8d82 | 4485 | if ((this_hdr->sh_flags & SHF_EXECINSTR) != 0) |
252b5132 | 4486 | p->p_flags |= PF_X; |
02bf8d82 | 4487 | if ((this_hdr->sh_flags & SHF_WRITE) != 0) |
252b5132 RH |
4488 | p->p_flags |= PF_W; |
4489 | } | |
4490 | } | |
bf988460 | 4491 | off -= off_adjust; |
0920dee7 | 4492 | |
7c928300 AM |
4493 | /* Check that all sections are in a PT_LOAD segment. |
4494 | Don't check funky gdb generated core files. */ | |
4495 | if (p->p_type == PT_LOAD && bfd_get_format (abfd) != bfd_core) | |
0920dee7 L |
4496 | for (i = 0, secpp = m->sections; i < m->count; i++, secpp++) |
4497 | { | |
4498 | Elf_Internal_Shdr *this_hdr; | |
4499 | asection *sec; | |
4500 | ||
4501 | sec = *secpp; | |
4502 | this_hdr = &(elf_section_data(sec)->this_hdr); | |
4503 | if (this_hdr->sh_size != 0 | |
4504 | && !ELF_IS_SECTION_IN_SEGMENT_FILE (this_hdr, p)) | |
4505 | { | |
4506 | (*_bfd_error_handler) | |
4507 | (_("%B: section `%A' can't be allocated in segment %d"), | |
4508 | abfd, sec, j); | |
6d33f217 | 4509 | print_segment_map (m); |
0920dee7 L |
4510 | bfd_set_error (bfd_error_bad_value); |
4511 | return FALSE; | |
4512 | } | |
4513 | } | |
252b5132 RH |
4514 | } |
4515 | ||
f3520d2f AM |
4516 | elf_tdata (abfd)->next_file_pos = off; |
4517 | return TRUE; | |
4518 | } | |
4519 | ||
4520 | /* Assign file positions for the other sections. */ | |
4521 | ||
4522 | static bfd_boolean | |
4523 | assign_file_positions_for_non_load_sections (bfd *abfd, | |
4524 | struct bfd_link_info *link_info) | |
4525 | { | |
4526 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
4527 | Elf_Internal_Shdr **i_shdrpp; | |
4528 | Elf_Internal_Shdr **hdrpp; | |
4529 | Elf_Internal_Phdr *phdrs; | |
4530 | Elf_Internal_Phdr *p; | |
4531 | struct elf_segment_map *m; | |
4532 | bfd_vma filehdr_vaddr, filehdr_paddr; | |
4533 | bfd_vma phdrs_vaddr, phdrs_paddr; | |
4534 | file_ptr off; | |
4535 | unsigned int num_sec; | |
4536 | unsigned int i; | |
4537 | unsigned int count; | |
4538 | ||
5c182d5f AM |
4539 | i_shdrpp = elf_elfsections (abfd); |
4540 | num_sec = elf_numsections (abfd); | |
f3520d2f | 4541 | off = elf_tdata (abfd)->next_file_pos; |
5c182d5f AM |
4542 | for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++) |
4543 | { | |
4544 | struct elf_obj_tdata *tdata = elf_tdata (abfd); | |
4545 | Elf_Internal_Shdr *hdr; | |
4546 | ||
4547 | hdr = *hdrpp; | |
4548 | if (hdr->bfd_section != NULL | |
252e386e AM |
4549 | && (hdr->bfd_section->filepos != 0 |
4550 | || (hdr->sh_type == SHT_NOBITS | |
4551 | && hdr->contents == NULL))) | |
627b32bc | 4552 | BFD_ASSERT (hdr->sh_offset == hdr->bfd_section->filepos); |
5c182d5f AM |
4553 | else if ((hdr->sh_flags & SHF_ALLOC) != 0) |
4554 | { | |
49c13adb L |
4555 | if (hdr->sh_size != 0) |
4556 | ((*_bfd_error_handler) | |
4557 | (_("%B: warning: allocated section `%s' not in segment"), | |
3ba71138 L |
4558 | abfd, |
4559 | (hdr->bfd_section == NULL | |
4560 | ? "*unknown*" | |
4561 | : hdr->bfd_section->name))); | |
4562 | /* We don't need to page align empty sections. */ | |
4563 | if ((abfd->flags & D_PAGED) != 0 && hdr->sh_size != 0) | |
5c182d5f AM |
4564 | off += vma_page_aligned_bias (hdr->sh_addr, off, |
4565 | bed->maxpagesize); | |
4566 | else | |
4567 | off += vma_page_aligned_bias (hdr->sh_addr, off, | |
4568 | hdr->sh_addralign); | |
4569 | off = _bfd_elf_assign_file_position_for_section (hdr, off, | |
4570 | FALSE); | |
4571 | } | |
4572 | else if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA) | |
4573 | && hdr->bfd_section == NULL) | |
4574 | || hdr == i_shdrpp[tdata->symtab_section] | |
4575 | || hdr == i_shdrpp[tdata->symtab_shndx_section] | |
4576 | || hdr == i_shdrpp[tdata->strtab_section]) | |
4577 | hdr->sh_offset = -1; | |
4578 | else | |
4579 | off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE); | |
5c182d5f AM |
4580 | } |
4581 | ||
252b5132 RH |
4582 | /* Now that we have set the section file positions, we can set up |
4583 | the file positions for the non PT_LOAD segments. */ | |
f3520d2f AM |
4584 | count = 0; |
4585 | filehdr_vaddr = 0; | |
4586 | filehdr_paddr = 0; | |
4587 | phdrs_vaddr = bed->maxpagesize + bed->s->sizeof_ehdr; | |
4588 | phdrs_paddr = 0; | |
4589 | phdrs = elf_tdata (abfd)->phdr; | |
4590 | for (m = elf_tdata (abfd)->segment_map, p = phdrs; | |
4591 | m != NULL; | |
4592 | m = m->next, p++) | |
4593 | { | |
4594 | ++count; | |
4595 | if (p->p_type != PT_LOAD) | |
4596 | continue; | |
4597 | ||
4598 | if (m->includes_filehdr) | |
4599 | { | |
4600 | filehdr_vaddr = p->p_vaddr; | |
4601 | filehdr_paddr = p->p_paddr; | |
4602 | } | |
4603 | if (m->includes_phdrs) | |
4604 | { | |
4605 | phdrs_vaddr = p->p_vaddr; | |
4606 | phdrs_paddr = p->p_paddr; | |
4607 | if (m->includes_filehdr) | |
4608 | { | |
4609 | phdrs_vaddr += bed->s->sizeof_ehdr; | |
4610 | phdrs_paddr += bed->s->sizeof_ehdr; | |
4611 | } | |
4612 | } | |
4613 | } | |
4614 | ||
252b5132 RH |
4615 | for (m = elf_tdata (abfd)->segment_map, p = phdrs; |
4616 | m != NULL; | |
4617 | m = m->next, p++) | |
4618 | { | |
129af99f | 4619 | if (p->p_type == PT_GNU_RELRO) |
252b5132 | 4620 | { |
b84a33b5 AM |
4621 | const Elf_Internal_Phdr *lp; |
4622 | ||
129af99f | 4623 | BFD_ASSERT (!m->includes_filehdr && !m->includes_phdrs); |
1ea63fd2 | 4624 | |
129af99f | 4625 | if (link_info != NULL) |
8c37241b | 4626 | { |
129af99f AS |
4627 | /* During linking the range of the RELRO segment is passed |
4628 | in link_info. */ | |
8c37241b JJ |
4629 | for (lp = phdrs; lp < phdrs + count; ++lp) |
4630 | { | |
4631 | if (lp->p_type == PT_LOAD | |
8c37241b | 4632 | && lp->p_vaddr >= link_info->relro_start |
b84a33b5 AM |
4633 | && lp->p_vaddr < link_info->relro_end |
4634 | && lp->p_vaddr + lp->p_filesz >= link_info->relro_end) | |
8c37241b JJ |
4635 | break; |
4636 | } | |
8c37241b | 4637 | } |
129af99f AS |
4638 | else |
4639 | { | |
4640 | /* Otherwise we are copying an executable or shared | |
b84a33b5 | 4641 | library, but we need to use the same linker logic. */ |
129af99f AS |
4642 | for (lp = phdrs; lp < phdrs + count; ++lp) |
4643 | { | |
4644 | if (lp->p_type == PT_LOAD | |
4645 | && lp->p_paddr == p->p_paddr) | |
4646 | break; | |
4647 | } | |
b84a33b5 AM |
4648 | } |
4649 | ||
4650 | if (lp < phdrs + count) | |
4651 | { | |
4652 | p->p_vaddr = lp->p_vaddr; | |
4653 | p->p_paddr = lp->p_paddr; | |
4654 | p->p_offset = lp->p_offset; | |
4655 | if (link_info != NULL) | |
4656 | p->p_filesz = link_info->relro_end - lp->p_vaddr; | |
4657 | else if (m->p_size_valid) | |
4658 | p->p_filesz = m->p_size; | |
129af99f AS |
4659 | else |
4660 | abort (); | |
b84a33b5 AM |
4661 | p->p_memsz = p->p_filesz; |
4662 | p->p_align = 1; | |
4663 | p->p_flags = (lp->p_flags & ~PF_W); | |
129af99f | 4664 | } |
9433b9b1 | 4665 | else |
b84a33b5 AM |
4666 | { |
4667 | memset (p, 0, sizeof *p); | |
4668 | p->p_type = PT_NULL; | |
4669 | } | |
129af99f AS |
4670 | } |
4671 | else if (m->count != 0) | |
4672 | { | |
4673 | if (p->p_type != PT_LOAD | |
4674 | && (p->p_type != PT_NOTE | |
4675 | || bfd_get_format (abfd) != bfd_core)) | |
4676 | { | |
4677 | Elf_Internal_Shdr *hdr; | |
4678 | asection *sect; | |
4679 | ||
4680 | BFD_ASSERT (!m->includes_filehdr && !m->includes_phdrs); | |
4681 | ||
4682 | sect = m->sections[m->count - 1]; | |
4683 | hdr = &elf_section_data (sect)->this_hdr; | |
4684 | p->p_filesz = sect->filepos - m->sections[0]->filepos; | |
4685 | if (hdr->sh_type != SHT_NOBITS) | |
4686 | p->p_filesz += hdr->sh_size; | |
4687 | p->p_offset = m->sections[0]->filepos; | |
4688 | } | |
4689 | } | |
4690 | else if (m->includes_filehdr) | |
4691 | { | |
4692 | p->p_vaddr = filehdr_vaddr; | |
4693 | if (! m->p_paddr_valid) | |
4694 | p->p_paddr = filehdr_paddr; | |
4695 | } | |
4696 | else if (m->includes_phdrs) | |
4697 | { | |
4698 | p->p_vaddr = phdrs_vaddr; | |
4699 | if (! m->p_paddr_valid) | |
4700 | p->p_paddr = phdrs_paddr; | |
252b5132 RH |
4701 | } |
4702 | } | |
4703 | ||
252b5132 RH |
4704 | elf_tdata (abfd)->next_file_pos = off; |
4705 | ||
b34976b6 | 4706 | return TRUE; |
252b5132 RH |
4707 | } |
4708 | ||
252b5132 RH |
4709 | /* Work out the file positions of all the sections. This is called by |
4710 | _bfd_elf_compute_section_file_positions. All the section sizes and | |
4711 | VMAs must be known before this is called. | |
4712 | ||
e0638f70 AM |
4713 | Reloc sections come in two flavours: Those processed specially as |
4714 | "side-channel" data attached to a section to which they apply, and | |
4715 | those that bfd doesn't process as relocations. The latter sort are | |
4716 | stored in a normal bfd section by bfd_section_from_shdr. We don't | |
4717 | consider the former sort here, unless they form part of the loadable | |
4718 | image. Reloc sections not assigned here will be handled later by | |
4719 | assign_file_positions_for_relocs. | |
252b5132 RH |
4720 | |
4721 | We also don't set the positions of the .symtab and .strtab here. */ | |
4722 | ||
b34976b6 | 4723 | static bfd_boolean |
c84fca4d AO |
4724 | assign_file_positions_except_relocs (bfd *abfd, |
4725 | struct bfd_link_info *link_info) | |
252b5132 | 4726 | { |
5c182d5f AM |
4727 | struct elf_obj_tdata *tdata = elf_tdata (abfd); |
4728 | Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd); | |
252b5132 | 4729 | file_ptr off; |
9c5bfbb7 | 4730 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
252b5132 RH |
4731 | |
4732 | if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0 | |
4733 | && bfd_get_format (abfd) != bfd_core) | |
4734 | { | |
5c182d5f AM |
4735 | Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd); |
4736 | unsigned int num_sec = elf_numsections (abfd); | |
252b5132 RH |
4737 | Elf_Internal_Shdr **hdrpp; |
4738 | unsigned int i; | |
4739 | ||
4740 | /* Start after the ELF header. */ | |
4741 | off = i_ehdrp->e_ehsize; | |
4742 | ||
4743 | /* We are not creating an executable, which means that we are | |
4744 | not creating a program header, and that the actual order of | |
4745 | the sections in the file is unimportant. */ | |
9ad5cbcf | 4746 | for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++) |
252b5132 RH |
4747 | { |
4748 | Elf_Internal_Shdr *hdr; | |
4749 | ||
4750 | hdr = *hdrpp; | |
e0638f70 AM |
4751 | if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA) |
4752 | && hdr->bfd_section == NULL) | |
9ad5cbcf AM |
4753 | || i == tdata->symtab_section |
4754 | || i == tdata->symtab_shndx_section | |
252b5132 RH |
4755 | || i == tdata->strtab_section) |
4756 | { | |
4757 | hdr->sh_offset = -1; | |
252b5132 | 4758 | } |
9ad5cbcf | 4759 | else |
b34976b6 | 4760 | off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE); |
252b5132 RH |
4761 | } |
4762 | } | |
4763 | else | |
4764 | { | |
f3520d2f AM |
4765 | unsigned int alloc; |
4766 | ||
252b5132 | 4767 | /* Assign file positions for the loaded sections based on the |
08a40648 | 4768 | assignment of sections to segments. */ |
f3520d2f AM |
4769 | if (!assign_file_positions_for_load_sections (abfd, link_info)) |
4770 | return FALSE; | |
4771 | ||
4772 | /* And for non-load sections. */ | |
4773 | if (!assign_file_positions_for_non_load_sections (abfd, link_info)) | |
4774 | return FALSE; | |
4775 | ||
e36284ab AM |
4776 | if (bed->elf_backend_modify_program_headers != NULL) |
4777 | { | |
4778 | if (!(*bed->elf_backend_modify_program_headers) (abfd, link_info)) | |
4779 | return FALSE; | |
4780 | } | |
4781 | ||
f3520d2f AM |
4782 | /* Write out the program headers. */ |
4783 | alloc = tdata->program_header_size / bed->s->sizeof_phdr; | |
4784 | if (bfd_seek (abfd, (bfd_signed_vma) bed->s->sizeof_ehdr, SEEK_SET) != 0 | |
4785 | || bed->s->write_out_phdrs (abfd, tdata->phdr, alloc) != 0) | |
b34976b6 | 4786 | return FALSE; |
252b5132 | 4787 | |
5c182d5f | 4788 | off = tdata->next_file_pos; |
252b5132 RH |
4789 | } |
4790 | ||
4791 | /* Place the section headers. */ | |
45d6a902 | 4792 | off = align_file_position (off, 1 << bed->s->log_file_align); |
252b5132 RH |
4793 | i_ehdrp->e_shoff = off; |
4794 | off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize; | |
4795 | ||
5c182d5f | 4796 | tdata->next_file_pos = off; |
252b5132 | 4797 | |
b34976b6 | 4798 | return TRUE; |
252b5132 RH |
4799 | } |
4800 | ||
b34976b6 | 4801 | static bfd_boolean |
217aa764 | 4802 | prep_headers (bfd *abfd) |
252b5132 RH |
4803 | { |
4804 | Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */ | |
4805 | Elf_Internal_Phdr *i_phdrp = 0; /* Program header table, internal form */ | |
2b0f7ef9 | 4806 | struct elf_strtab_hash *shstrtab; |
9c5bfbb7 | 4807 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
252b5132 RH |
4808 | |
4809 | i_ehdrp = elf_elfheader (abfd); | |
252b5132 | 4810 | |
2b0f7ef9 | 4811 | shstrtab = _bfd_elf_strtab_init (); |
252b5132 | 4812 | if (shstrtab == NULL) |
b34976b6 | 4813 | return FALSE; |
252b5132 RH |
4814 | |
4815 | elf_shstrtab (abfd) = shstrtab; | |
4816 | ||
4817 | i_ehdrp->e_ident[EI_MAG0] = ELFMAG0; | |
4818 | i_ehdrp->e_ident[EI_MAG1] = ELFMAG1; | |
4819 | i_ehdrp->e_ident[EI_MAG2] = ELFMAG2; | |
4820 | i_ehdrp->e_ident[EI_MAG3] = ELFMAG3; | |
4821 | ||
4822 | i_ehdrp->e_ident[EI_CLASS] = bed->s->elfclass; | |
4823 | i_ehdrp->e_ident[EI_DATA] = | |
4824 | bfd_big_endian (abfd) ? ELFDATA2MSB : ELFDATA2LSB; | |
4825 | i_ehdrp->e_ident[EI_VERSION] = bed->s->ev_current; | |
4826 | ||
252b5132 RH |
4827 | if ((abfd->flags & DYNAMIC) != 0) |
4828 | i_ehdrp->e_type = ET_DYN; | |
4829 | else if ((abfd->flags & EXEC_P) != 0) | |
4830 | i_ehdrp->e_type = ET_EXEC; | |
4831 | else if (bfd_get_format (abfd) == bfd_core) | |
4832 | i_ehdrp->e_type = ET_CORE; | |
4833 | else | |
4834 | i_ehdrp->e_type = ET_REL; | |
4835 | ||
4836 | switch (bfd_get_arch (abfd)) | |
4837 | { | |
4838 | case bfd_arch_unknown: | |
4839 | i_ehdrp->e_machine = EM_NONE; | |
4840 | break; | |
aa4f99bb AO |
4841 | |
4842 | /* There used to be a long list of cases here, each one setting | |
4843 | e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE | |
4844 | in the corresponding bfd definition. To avoid duplication, | |
4845 | the switch was removed. Machines that need special handling | |
4846 | can generally do it in elf_backend_final_write_processing(), | |
4847 | unless they need the information earlier than the final write. | |
4848 | Such need can generally be supplied by replacing the tests for | |
4849 | e_machine with the conditions used to determine it. */ | |
252b5132 | 4850 | default: |
9c5bfbb7 AM |
4851 | i_ehdrp->e_machine = bed->elf_machine_code; |
4852 | } | |
aa4f99bb | 4853 | |
252b5132 RH |
4854 | i_ehdrp->e_version = bed->s->ev_current; |
4855 | i_ehdrp->e_ehsize = bed->s->sizeof_ehdr; | |
4856 | ||
c044fabd | 4857 | /* No program header, for now. */ |
252b5132 RH |
4858 | i_ehdrp->e_phoff = 0; |
4859 | i_ehdrp->e_phentsize = 0; | |
4860 | i_ehdrp->e_phnum = 0; | |
4861 | ||
c044fabd | 4862 | /* Each bfd section is section header entry. */ |
252b5132 RH |
4863 | i_ehdrp->e_entry = bfd_get_start_address (abfd); |
4864 | i_ehdrp->e_shentsize = bed->s->sizeof_shdr; | |
4865 | ||
c044fabd | 4866 | /* If we're building an executable, we'll need a program header table. */ |
252b5132 | 4867 | if (abfd->flags & EXEC_P) |
0e71e495 BE |
4868 | /* It all happens later. */ |
4869 | ; | |
252b5132 RH |
4870 | else |
4871 | { | |
4872 | i_ehdrp->e_phentsize = 0; | |
4873 | i_phdrp = 0; | |
4874 | i_ehdrp->e_phoff = 0; | |
4875 | } | |
4876 | ||
4877 | elf_tdata (abfd)->symtab_hdr.sh_name = | |
b34976b6 | 4878 | (unsigned int) _bfd_elf_strtab_add (shstrtab, ".symtab", FALSE); |
252b5132 | 4879 | elf_tdata (abfd)->strtab_hdr.sh_name = |
b34976b6 | 4880 | (unsigned int) _bfd_elf_strtab_add (shstrtab, ".strtab", FALSE); |
252b5132 | 4881 | elf_tdata (abfd)->shstrtab_hdr.sh_name = |
b34976b6 | 4882 | (unsigned int) _bfd_elf_strtab_add (shstrtab, ".shstrtab", FALSE); |
252b5132 RH |
4883 | if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1 |
4884 | || elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1 | |
4885 | || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1) | |
b34976b6 | 4886 | return FALSE; |
252b5132 | 4887 | |
b34976b6 | 4888 | return TRUE; |
252b5132 RH |
4889 | } |
4890 | ||
4891 | /* Assign file positions for all the reloc sections which are not part | |
4892 | of the loadable file image. */ | |
4893 | ||
4894 | void | |
217aa764 | 4895 | _bfd_elf_assign_file_positions_for_relocs (bfd *abfd) |
252b5132 RH |
4896 | { |
4897 | file_ptr off; | |
9ad5cbcf | 4898 | unsigned int i, num_sec; |
252b5132 RH |
4899 | Elf_Internal_Shdr **shdrpp; |
4900 | ||
4901 | off = elf_tdata (abfd)->next_file_pos; | |
4902 | ||
9ad5cbcf AM |
4903 | num_sec = elf_numsections (abfd); |
4904 | for (i = 1, shdrpp = elf_elfsections (abfd) + 1; i < num_sec; i++, shdrpp++) | |
252b5132 RH |
4905 | { |
4906 | Elf_Internal_Shdr *shdrp; | |
4907 | ||
4908 | shdrp = *shdrpp; | |
4909 | if ((shdrp->sh_type == SHT_REL || shdrp->sh_type == SHT_RELA) | |
4910 | && shdrp->sh_offset == -1) | |
b34976b6 | 4911 | off = _bfd_elf_assign_file_position_for_section (shdrp, off, TRUE); |
252b5132 RH |
4912 | } |
4913 | ||
4914 | elf_tdata (abfd)->next_file_pos = off; | |
4915 | } | |
4916 | ||
b34976b6 | 4917 | bfd_boolean |
217aa764 | 4918 | _bfd_elf_write_object_contents (bfd *abfd) |
252b5132 | 4919 | { |
9c5bfbb7 | 4920 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
252b5132 RH |
4921 | Elf_Internal_Ehdr *i_ehdrp; |
4922 | Elf_Internal_Shdr **i_shdrp; | |
b34976b6 | 4923 | bfd_boolean failed; |
9ad5cbcf | 4924 | unsigned int count, num_sec; |
252b5132 RH |
4925 | |
4926 | if (! abfd->output_has_begun | |
217aa764 | 4927 | && ! _bfd_elf_compute_section_file_positions (abfd, NULL)) |
b34976b6 | 4928 | return FALSE; |
252b5132 RH |
4929 | |
4930 | i_shdrp = elf_elfsections (abfd); | |
4931 | i_ehdrp = elf_elfheader (abfd); | |
4932 | ||
b34976b6 | 4933 | failed = FALSE; |
252b5132 RH |
4934 | bfd_map_over_sections (abfd, bed->s->write_relocs, &failed); |
4935 | if (failed) | |
b34976b6 | 4936 | return FALSE; |
252b5132 RH |
4937 | |
4938 | _bfd_elf_assign_file_positions_for_relocs (abfd); | |
4939 | ||
c044fabd | 4940 | /* After writing the headers, we need to write the sections too... */ |
9ad5cbcf AM |
4941 | num_sec = elf_numsections (abfd); |
4942 | for (count = 1; count < num_sec; count++) | |
252b5132 RH |
4943 | { |
4944 | if (bed->elf_backend_section_processing) | |
4945 | (*bed->elf_backend_section_processing) (abfd, i_shdrp[count]); | |
4946 | if (i_shdrp[count]->contents) | |
4947 | { | |
dc810e39 AM |
4948 | bfd_size_type amt = i_shdrp[count]->sh_size; |
4949 | ||
252b5132 | 4950 | if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0 |
dc810e39 | 4951 | || bfd_bwrite (i_shdrp[count]->contents, amt, abfd) != amt) |
b34976b6 | 4952 | return FALSE; |
252b5132 RH |
4953 | } |
4954 | } | |
4955 | ||
4956 | /* Write out the section header names. */ | |
26ae6d5e DJ |
4957 | if (elf_shstrtab (abfd) != NULL |
4958 | && (bfd_seek (abfd, elf_tdata (abfd)->shstrtab_hdr.sh_offset, SEEK_SET) != 0 | |
08a40648 | 4959 | || !_bfd_elf_strtab_emit (abfd, elf_shstrtab (abfd)))) |
b34976b6 | 4960 | return FALSE; |
252b5132 RH |
4961 | |
4962 | if (bed->elf_backend_final_write_processing) | |
4963 | (*bed->elf_backend_final_write_processing) (abfd, | |
4964 | elf_tdata (abfd)->linker); | |
4965 | ||
ff59fc36 RM |
4966 | if (!bed->s->write_shdrs_and_ehdr (abfd)) |
4967 | return FALSE; | |
4968 | ||
4969 | /* This is last since write_shdrs_and_ehdr can touch i_shdrp[0]. */ | |
bfb53a4f RM |
4970 | if (elf_tdata (abfd)->after_write_object_contents) |
4971 | return (*elf_tdata (abfd)->after_write_object_contents) (abfd); | |
ff59fc36 RM |
4972 | |
4973 | return TRUE; | |
252b5132 RH |
4974 | } |
4975 | ||
b34976b6 | 4976 | bfd_boolean |
217aa764 | 4977 | _bfd_elf_write_corefile_contents (bfd *abfd) |
252b5132 | 4978 | { |
c044fabd | 4979 | /* Hopefully this can be done just like an object file. */ |
252b5132 RH |
4980 | return _bfd_elf_write_object_contents (abfd); |
4981 | } | |
c044fabd KH |
4982 | |
4983 | /* Given a section, search the header to find them. */ | |
4984 | ||
cb33740c | 4985 | unsigned int |
198beae2 | 4986 | _bfd_elf_section_from_bfd_section (bfd *abfd, struct bfd_section *asect) |
252b5132 | 4987 | { |
9c5bfbb7 | 4988 | const struct elf_backend_data *bed; |
cb33740c | 4989 | unsigned int index; |
252b5132 | 4990 | |
9ad5cbcf AM |
4991 | if (elf_section_data (asect) != NULL |
4992 | && elf_section_data (asect)->this_idx != 0) | |
4993 | return elf_section_data (asect)->this_idx; | |
4994 | ||
4995 | if (bfd_is_abs_section (asect)) | |
af746e92 AM |
4996 | index = SHN_ABS; |
4997 | else if (bfd_is_com_section (asect)) | |
4998 | index = SHN_COMMON; | |
4999 | else if (bfd_is_und_section (asect)) | |
5000 | index = SHN_UNDEF; | |
5001 | else | |
cb33740c | 5002 | index = SHN_BAD; |
252b5132 | 5003 | |
af746e92 | 5004 | bed = get_elf_backend_data (abfd); |
252b5132 RH |
5005 | if (bed->elf_backend_section_from_bfd_section) |
5006 | { | |
af746e92 | 5007 | int retval = index; |
9ad5cbcf | 5008 | |
af746e92 AM |
5009 | if ((*bed->elf_backend_section_from_bfd_section) (abfd, asect, &retval)) |
5010 | return retval; | |
252b5132 RH |
5011 | } |
5012 | ||
cb33740c | 5013 | if (index == SHN_BAD) |
af746e92 | 5014 | bfd_set_error (bfd_error_nonrepresentable_section); |
252b5132 | 5015 | |
af746e92 | 5016 | return index; |
252b5132 RH |
5017 | } |
5018 | ||
5019 | /* Given a BFD symbol, return the index in the ELF symbol table, or -1 | |
5020 | on error. */ | |
5021 | ||
5022 | int | |
217aa764 | 5023 | _bfd_elf_symbol_from_bfd_symbol (bfd *abfd, asymbol **asym_ptr_ptr) |
252b5132 RH |
5024 | { |
5025 | asymbol *asym_ptr = *asym_ptr_ptr; | |
5026 | int idx; | |
5027 | flagword flags = asym_ptr->flags; | |
5028 | ||
5029 | /* When gas creates relocations against local labels, it creates its | |
5030 | own symbol for the section, but does put the symbol into the | |
5031 | symbol chain, so udata is 0. When the linker is generating | |
5032 | relocatable output, this section symbol may be for one of the | |
5033 | input sections rather than the output section. */ | |
5034 | if (asym_ptr->udata.i == 0 | |
5035 | && (flags & BSF_SECTION_SYM) | |
5036 | && asym_ptr->section) | |
5037 | { | |
5372391b | 5038 | asection *sec; |
252b5132 RH |
5039 | int indx; |
5040 | ||
5372391b AM |
5041 | sec = asym_ptr->section; |
5042 | if (sec->owner != abfd && sec->output_section != NULL) | |
5043 | sec = sec->output_section; | |
5044 | if (sec->owner == abfd | |
5045 | && (indx = sec->index) < elf_num_section_syms (abfd) | |
4e89ac30 | 5046 | && elf_section_syms (abfd)[indx] != NULL) |
252b5132 RH |
5047 | asym_ptr->udata.i = elf_section_syms (abfd)[indx]->udata.i; |
5048 | } | |
5049 | ||
5050 | idx = asym_ptr->udata.i; | |
5051 | ||
5052 | if (idx == 0) | |
5053 | { | |
5054 | /* This case can occur when using --strip-symbol on a symbol | |
08a40648 | 5055 | which is used in a relocation entry. */ |
252b5132 | 5056 | (*_bfd_error_handler) |
d003868e AM |
5057 | (_("%B: symbol `%s' required but not present"), |
5058 | abfd, bfd_asymbol_name (asym_ptr)); | |
252b5132 RH |
5059 | bfd_set_error (bfd_error_no_symbols); |
5060 | return -1; | |
5061 | } | |
5062 | ||
5063 | #if DEBUG & 4 | |
5064 | { | |
5065 | fprintf (stderr, | |
661a3fd4 | 5066 | "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8lx%s\n", |
252b5132 RH |
5067 | (long) asym_ptr, asym_ptr->name, idx, flags, |
5068 | elf_symbol_flags (flags)); | |
5069 | fflush (stderr); | |
5070 | } | |
5071 | #endif | |
5072 | ||
5073 | return idx; | |
5074 | } | |
5075 | ||
84d1d650 | 5076 | /* Rewrite program header information. */ |
252b5132 | 5077 | |
b34976b6 | 5078 | static bfd_boolean |
84d1d650 | 5079 | rewrite_elf_program_header (bfd *ibfd, bfd *obfd) |
252b5132 | 5080 | { |
b34976b6 AM |
5081 | Elf_Internal_Ehdr *iehdr; |
5082 | struct elf_segment_map *map; | |
5083 | struct elf_segment_map *map_first; | |
5084 | struct elf_segment_map **pointer_to_map; | |
5085 | Elf_Internal_Phdr *segment; | |
5086 | asection *section; | |
5087 | unsigned int i; | |
5088 | unsigned int num_segments; | |
5089 | bfd_boolean phdr_included = FALSE; | |
5c44b38e | 5090 | bfd_boolean p_paddr_valid; |
b34976b6 AM |
5091 | bfd_vma maxpagesize; |
5092 | struct elf_segment_map *phdr_adjust_seg = NULL; | |
5093 | unsigned int phdr_adjust_num = 0; | |
9c5bfbb7 | 5094 | const struct elf_backend_data *bed; |
bc67d8a6 | 5095 | |
caf47ea6 | 5096 | bed = get_elf_backend_data (ibfd); |
252b5132 RH |
5097 | iehdr = elf_elfheader (ibfd); |
5098 | ||
bc67d8a6 | 5099 | map_first = NULL; |
c044fabd | 5100 | pointer_to_map = &map_first; |
252b5132 RH |
5101 | |
5102 | num_segments = elf_elfheader (ibfd)->e_phnum; | |
bc67d8a6 NC |
5103 | maxpagesize = get_elf_backend_data (obfd)->maxpagesize; |
5104 | ||
5105 | /* Returns the end address of the segment + 1. */ | |
aecc8f8a AM |
5106 | #define SEGMENT_END(segment, start) \ |
5107 | (start + (segment->p_memsz > segment->p_filesz \ | |
5108 | ? segment->p_memsz : segment->p_filesz)) | |
bc67d8a6 | 5109 | |
eecdbe52 JJ |
5110 | #define SECTION_SIZE(section, segment) \ |
5111 | (((section->flags & (SEC_HAS_CONTENTS | SEC_THREAD_LOCAL)) \ | |
5112 | != SEC_THREAD_LOCAL || segment->p_type == PT_TLS) \ | |
eea6121a | 5113 | ? section->size : 0) |
eecdbe52 | 5114 | |
b34976b6 | 5115 | /* Returns TRUE if the given section is contained within |
bc67d8a6 | 5116 | the given segment. VMA addresses are compared. */ |
aecc8f8a AM |
5117 | #define IS_CONTAINED_BY_VMA(section, segment) \ |
5118 | (section->vma >= segment->p_vaddr \ | |
eecdbe52 | 5119 | && (section->vma + SECTION_SIZE (section, segment) \ |
aecc8f8a | 5120 | <= (SEGMENT_END (segment, segment->p_vaddr)))) |
c044fabd | 5121 | |
b34976b6 | 5122 | /* Returns TRUE if the given section is contained within |
bc67d8a6 | 5123 | the given segment. LMA addresses are compared. */ |
aecc8f8a AM |
5124 | #define IS_CONTAINED_BY_LMA(section, segment, base) \ |
5125 | (section->lma >= base \ | |
eecdbe52 | 5126 | && (section->lma + SECTION_SIZE (section, segment) \ |
aecc8f8a | 5127 | <= SEGMENT_END (segment, base))) |
252b5132 | 5128 | |
0efc80c8 L |
5129 | /* Handle PT_NOTE segment. */ |
5130 | #define IS_NOTE(p, s) \ | |
aecc8f8a | 5131 | (p->p_type == PT_NOTE \ |
0efc80c8 | 5132 | && elf_section_type (s) == SHT_NOTE \ |
aecc8f8a | 5133 | && (bfd_vma) s->filepos >= p->p_offset \ |
cb3ff1e5 | 5134 | && ((bfd_vma) s->filepos + s->size \ |
aecc8f8a | 5135 | <= p->p_offset + p->p_filesz)) |
252b5132 | 5136 | |
0efc80c8 L |
5137 | /* Special case: corefile "NOTE" section containing regs, prpsinfo |
5138 | etc. */ | |
5139 | #define IS_COREFILE_NOTE(p, s) \ | |
5140 | (IS_NOTE (p, s) \ | |
5141 | && bfd_get_format (ibfd) == bfd_core \ | |
5142 | && s->vma == 0 \ | |
5143 | && s->lma == 0) | |
5144 | ||
252b5132 RH |
5145 | /* The complicated case when p_vaddr is 0 is to handle the Solaris |
5146 | linker, which generates a PT_INTERP section with p_vaddr and | |
5147 | p_memsz set to 0. */ | |
aecc8f8a AM |
5148 | #define IS_SOLARIS_PT_INTERP(p, s) \ |
5149 | (p->p_vaddr == 0 \ | |
5150 | && p->p_paddr == 0 \ | |
5151 | && p->p_memsz == 0 \ | |
5152 | && p->p_filesz > 0 \ | |
5153 | && (s->flags & SEC_HAS_CONTENTS) != 0 \ | |
eea6121a | 5154 | && s->size > 0 \ |
aecc8f8a | 5155 | && (bfd_vma) s->filepos >= p->p_offset \ |
cb3ff1e5 | 5156 | && ((bfd_vma) s->filepos + s->size \ |
aecc8f8a | 5157 | <= p->p_offset + p->p_filesz)) |
5c440b1e | 5158 | |
bc67d8a6 NC |
5159 | /* Decide if the given section should be included in the given segment. |
5160 | A section will be included if: | |
f5ffc919 | 5161 | 1. It is within the address space of the segment -- we use the LMA |
08a40648 | 5162 | if that is set for the segment and the VMA otherwise, |
0efc80c8 L |
5163 | 2. It is an allocated section or a NOTE section in a PT_NOTE |
5164 | segment. | |
bc67d8a6 | 5165 | 3. There is an output section associated with it, |
eecdbe52 | 5166 | 4. The section has not already been allocated to a previous segment. |
2b05f1b7 | 5167 | 5. PT_GNU_STACK segments do not include any sections. |
03394ac9 | 5168 | 6. PT_TLS segment includes only SHF_TLS sections. |
6f79b219 JJ |
5169 | 7. SHF_TLS sections are only in PT_TLS or PT_LOAD segments. |
5170 | 8. PT_DYNAMIC should not contain empty sections at the beginning | |
08a40648 | 5171 | (with the possible exception of .dynamic). */ |
9f17e2a6 | 5172 | #define IS_SECTION_IN_INPUT_SEGMENT(section, segment, bed) \ |
2b05f1b7 L |
5173 | ((((segment->p_paddr \ |
5174 | ? IS_CONTAINED_BY_LMA (section, segment, segment->p_paddr) \ | |
5175 | : IS_CONTAINED_BY_VMA (section, segment)) \ | |
5176 | && (section->flags & SEC_ALLOC) != 0) \ | |
0efc80c8 | 5177 | || IS_NOTE (segment, section)) \ |
2b05f1b7 L |
5178 | && segment->p_type != PT_GNU_STACK \ |
5179 | && (segment->p_type != PT_TLS \ | |
5180 | || (section->flags & SEC_THREAD_LOCAL)) \ | |
5181 | && (segment->p_type == PT_LOAD \ | |
5182 | || segment->p_type == PT_TLS \ | |
5183 | || (section->flags & SEC_THREAD_LOCAL) == 0) \ | |
5184 | && (segment->p_type != PT_DYNAMIC \ | |
5185 | || SECTION_SIZE (section, segment) > 0 \ | |
5186 | || (segment->p_paddr \ | |
5187 | ? segment->p_paddr != section->lma \ | |
5188 | : segment->p_vaddr != section->vma) \ | |
5189 | || (strcmp (bfd_get_section_name (ibfd, section), ".dynamic") \ | |
5190 | == 0)) \ | |
0067a569 | 5191 | && !section->segment_mark) |
bc67d8a6 | 5192 | |
9f17e2a6 L |
5193 | /* If the output section of a section in the input segment is NULL, |
5194 | it is removed from the corresponding output segment. */ | |
5195 | #define INCLUDE_SECTION_IN_SEGMENT(section, segment, bed) \ | |
5196 | (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed) \ | |
5197 | && section->output_section != NULL) | |
5198 | ||
b34976b6 | 5199 | /* Returns TRUE iff seg1 starts after the end of seg2. */ |
b5f852ea NC |
5200 | #define SEGMENT_AFTER_SEGMENT(seg1, seg2, field) \ |
5201 | (seg1->field >= SEGMENT_END (seg2, seg2->field)) | |
5202 | ||
5203 | /* Returns TRUE iff seg1 and seg2 overlap. Segments overlap iff both | |
5204 | their VMA address ranges and their LMA address ranges overlap. | |
5205 | It is possible to have overlapping VMA ranges without overlapping LMA | |
5206 | ranges. RedBoot images for example can have both .data and .bss mapped | |
5207 | to the same VMA range, but with the .data section mapped to a different | |
5208 | LMA. */ | |
aecc8f8a | 5209 | #define SEGMENT_OVERLAPS(seg1, seg2) \ |
b5f852ea | 5210 | ( !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_vaddr) \ |
08a40648 | 5211 | || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_vaddr)) \ |
b5f852ea | 5212 | && !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_paddr) \ |
08a40648 | 5213 | || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_paddr))) |
bc67d8a6 NC |
5214 | |
5215 | /* Initialise the segment mark field. */ | |
5216 | for (section = ibfd->sections; section != NULL; section = section->next) | |
b34976b6 | 5217 | section->segment_mark = FALSE; |
bc67d8a6 | 5218 | |
5c44b38e AM |
5219 | /* The Solaris linker creates program headers in which all the |
5220 | p_paddr fields are zero. When we try to objcopy or strip such a | |
5221 | file, we get confused. Check for this case, and if we find it | |
5222 | don't set the p_paddr_valid fields. */ | |
5223 | p_paddr_valid = FALSE; | |
5224 | for (i = 0, segment = elf_tdata (ibfd)->phdr; | |
5225 | i < num_segments; | |
5226 | i++, segment++) | |
5227 | if (segment->p_paddr != 0) | |
5228 | { | |
5229 | p_paddr_valid = TRUE; | |
5230 | break; | |
5231 | } | |
5232 | ||
252b5132 | 5233 | /* Scan through the segments specified in the program header |
bc67d8a6 | 5234 | of the input BFD. For this first scan we look for overlaps |
9ad5cbcf | 5235 | in the loadable segments. These can be created by weird |
aecc8f8a | 5236 | parameters to objcopy. Also, fix some solaris weirdness. */ |
bc67d8a6 NC |
5237 | for (i = 0, segment = elf_tdata (ibfd)->phdr; |
5238 | i < num_segments; | |
c044fabd | 5239 | i++, segment++) |
252b5132 | 5240 | { |
252b5132 | 5241 | unsigned int j; |
c044fabd | 5242 | Elf_Internal_Phdr *segment2; |
252b5132 | 5243 | |
aecc8f8a AM |
5244 | if (segment->p_type == PT_INTERP) |
5245 | for (section = ibfd->sections; section; section = section->next) | |
5246 | if (IS_SOLARIS_PT_INTERP (segment, section)) | |
5247 | { | |
5248 | /* Mininal change so that the normal section to segment | |
4cc11e76 | 5249 | assignment code will work. */ |
aecc8f8a AM |
5250 | segment->p_vaddr = section->vma; |
5251 | break; | |
5252 | } | |
5253 | ||
bc67d8a6 | 5254 | if (segment->p_type != PT_LOAD) |
b10a8ae0 L |
5255 | { |
5256 | /* Remove PT_GNU_RELRO segment. */ | |
5257 | if (segment->p_type == PT_GNU_RELRO) | |
5258 | segment->p_type = PT_NULL; | |
5259 | continue; | |
5260 | } | |
c044fabd | 5261 | |
bc67d8a6 | 5262 | /* Determine if this segment overlaps any previous segments. */ |
0067a569 | 5263 | for (j = 0, segment2 = elf_tdata (ibfd)->phdr; j < i; j++, segment2++) |
bc67d8a6 NC |
5264 | { |
5265 | bfd_signed_vma extra_length; | |
c044fabd | 5266 | |
bc67d8a6 | 5267 | if (segment2->p_type != PT_LOAD |
0067a569 | 5268 | || !SEGMENT_OVERLAPS (segment, segment2)) |
bc67d8a6 | 5269 | continue; |
c044fabd | 5270 | |
bc67d8a6 NC |
5271 | /* Merge the two segments together. */ |
5272 | if (segment2->p_vaddr < segment->p_vaddr) | |
5273 | { | |
c044fabd | 5274 | /* Extend SEGMENT2 to include SEGMENT and then delete |
08a40648 | 5275 | SEGMENT. */ |
0067a569 AM |
5276 | extra_length = (SEGMENT_END (segment, segment->p_vaddr) |
5277 | - SEGMENT_END (segment2, segment2->p_vaddr)); | |
c044fabd | 5278 | |
bc67d8a6 NC |
5279 | if (extra_length > 0) |
5280 | { | |
0067a569 | 5281 | segment2->p_memsz += extra_length; |
bc67d8a6 NC |
5282 | segment2->p_filesz += extra_length; |
5283 | } | |
c044fabd | 5284 | |
bc67d8a6 | 5285 | segment->p_type = PT_NULL; |
c044fabd | 5286 | |
bc67d8a6 NC |
5287 | /* Since we have deleted P we must restart the outer loop. */ |
5288 | i = 0; | |
5289 | segment = elf_tdata (ibfd)->phdr; | |
5290 | break; | |
5291 | } | |
5292 | else | |
5293 | { | |
c044fabd | 5294 | /* Extend SEGMENT to include SEGMENT2 and then delete |
08a40648 | 5295 | SEGMENT2. */ |
0067a569 AM |
5296 | extra_length = (SEGMENT_END (segment2, segment2->p_vaddr) |
5297 | - SEGMENT_END (segment, segment->p_vaddr)); | |
c044fabd | 5298 | |
bc67d8a6 NC |
5299 | if (extra_length > 0) |
5300 | { | |
0067a569 | 5301 | segment->p_memsz += extra_length; |
bc67d8a6 NC |
5302 | segment->p_filesz += extra_length; |
5303 | } | |
c044fabd | 5304 | |
bc67d8a6 NC |
5305 | segment2->p_type = PT_NULL; |
5306 | } | |
5307 | } | |
5308 | } | |
c044fabd | 5309 | |
bc67d8a6 NC |
5310 | /* The second scan attempts to assign sections to segments. */ |
5311 | for (i = 0, segment = elf_tdata (ibfd)->phdr; | |
5312 | i < num_segments; | |
0067a569 | 5313 | i++, segment++) |
bc67d8a6 | 5314 | { |
0067a569 AM |
5315 | unsigned int section_count; |
5316 | asection **sections; | |
5317 | asection *output_section; | |
5318 | unsigned int isec; | |
5319 | bfd_vma matching_lma; | |
5320 | bfd_vma suggested_lma; | |
5321 | unsigned int j; | |
dc810e39 | 5322 | bfd_size_type amt; |
0067a569 AM |
5323 | asection *first_section; |
5324 | bfd_boolean first_matching_lma; | |
5325 | bfd_boolean first_suggested_lma; | |
bc67d8a6 NC |
5326 | |
5327 | if (segment->p_type == PT_NULL) | |
5328 | continue; | |
c044fabd | 5329 | |
9f17e2a6 | 5330 | first_section = NULL; |
bc67d8a6 | 5331 | /* Compute how many sections might be placed into this segment. */ |
b5f852ea NC |
5332 | for (section = ibfd->sections, section_count = 0; |
5333 | section != NULL; | |
5334 | section = section->next) | |
9f17e2a6 L |
5335 | { |
5336 | /* Find the first section in the input segment, which may be | |
5337 | removed from the corresponding output segment. */ | |
5338 | if (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed)) | |
5339 | { | |
5340 | if (first_section == NULL) | |
5341 | first_section = section; | |
5342 | if (section->output_section != NULL) | |
5343 | ++section_count; | |
5344 | } | |
5345 | } | |
811072d8 | 5346 | |
b5f852ea NC |
5347 | /* Allocate a segment map big enough to contain |
5348 | all of the sections we have selected. */ | |
dc810e39 AM |
5349 | amt = sizeof (struct elf_segment_map); |
5350 | amt += ((bfd_size_type) section_count - 1) * sizeof (asection *); | |
41f8ce69 | 5351 | map = bfd_zalloc (obfd, amt); |
bc67d8a6 | 5352 | if (map == NULL) |
b34976b6 | 5353 | return FALSE; |
252b5132 RH |
5354 | |
5355 | /* Initialise the fields of the segment map. Default to | |
5356 | using the physical address of the segment in the input BFD. */ | |
0067a569 AM |
5357 | map->next = NULL; |
5358 | map->p_type = segment->p_type; | |
5359 | map->p_flags = segment->p_flags; | |
bc67d8a6 | 5360 | map->p_flags_valid = 1; |
55d55ac7 | 5361 | |
9f17e2a6 L |
5362 | /* If the first section in the input segment is removed, there is |
5363 | no need to preserve segment physical address in the corresponding | |
5364 | output segment. */ | |
945c025a | 5365 | if (!first_section || first_section->output_section != NULL) |
9f17e2a6 L |
5366 | { |
5367 | map->p_paddr = segment->p_paddr; | |
5c44b38e | 5368 | map->p_paddr_valid = p_paddr_valid; |
9f17e2a6 | 5369 | } |
252b5132 RH |
5370 | |
5371 | /* Determine if this segment contains the ELF file header | |
5372 | and if it contains the program headers themselves. */ | |
bc67d8a6 NC |
5373 | map->includes_filehdr = (segment->p_offset == 0 |
5374 | && segment->p_filesz >= iehdr->e_ehsize); | |
bc67d8a6 | 5375 | map->includes_phdrs = 0; |
252b5132 | 5376 | |
0067a569 | 5377 | if (!phdr_included || segment->p_type != PT_LOAD) |
252b5132 | 5378 | { |
bc67d8a6 NC |
5379 | map->includes_phdrs = |
5380 | (segment->p_offset <= (bfd_vma) iehdr->e_phoff | |
5381 | && (segment->p_offset + segment->p_filesz | |
252b5132 RH |
5382 | >= ((bfd_vma) iehdr->e_phoff |
5383 | + iehdr->e_phnum * iehdr->e_phentsize))); | |
c044fabd | 5384 | |
bc67d8a6 | 5385 | if (segment->p_type == PT_LOAD && map->includes_phdrs) |
b34976b6 | 5386 | phdr_included = TRUE; |
252b5132 RH |
5387 | } |
5388 | ||
bc67d8a6 | 5389 | if (section_count == 0) |
252b5132 RH |
5390 | { |
5391 | /* Special segments, such as the PT_PHDR segment, may contain | |
5392 | no sections, but ordinary, loadable segments should contain | |
1ed89aa9 NC |
5393 | something. They are allowed by the ELF spec however, so only |
5394 | a warning is produced. */ | |
bc67d8a6 | 5395 | if (segment->p_type == PT_LOAD) |
0067a569 AM |
5396 | (*_bfd_error_handler) (_("%B: warning: Empty loadable segment" |
5397 | " detected, is this intentional ?\n"), | |
5398 | ibfd); | |
252b5132 | 5399 | |
bc67d8a6 | 5400 | map->count = 0; |
c044fabd KH |
5401 | *pointer_to_map = map; |
5402 | pointer_to_map = &map->next; | |
252b5132 RH |
5403 | |
5404 | continue; | |
5405 | } | |
5406 | ||
5407 | /* Now scan the sections in the input BFD again and attempt | |
5408 | to add their corresponding output sections to the segment map. | |
5409 | The problem here is how to handle an output section which has | |
5410 | been moved (ie had its LMA changed). There are four possibilities: | |
5411 | ||
5412 | 1. None of the sections have been moved. | |
5413 | In this case we can continue to use the segment LMA from the | |
5414 | input BFD. | |
5415 | ||
5416 | 2. All of the sections have been moved by the same amount. | |
5417 | In this case we can change the segment's LMA to match the LMA | |
5418 | of the first section. | |
5419 | ||
5420 | 3. Some of the sections have been moved, others have not. | |
5421 | In this case those sections which have not been moved can be | |
5422 | placed in the current segment which will have to have its size, | |
5423 | and possibly its LMA changed, and a new segment or segments will | |
5424 | have to be created to contain the other sections. | |
5425 | ||
b5f852ea | 5426 | 4. The sections have been moved, but not by the same amount. |
252b5132 RH |
5427 | In this case we can change the segment's LMA to match the LMA |
5428 | of the first section and we will have to create a new segment | |
5429 | or segments to contain the other sections. | |
5430 | ||
5431 | In order to save time, we allocate an array to hold the section | |
5432 | pointers that we are interested in. As these sections get assigned | |
5433 | to a segment, they are removed from this array. */ | |
5434 | ||
d0fb9a8d | 5435 | sections = bfd_malloc2 (section_count, sizeof (asection *)); |
252b5132 | 5436 | if (sections == NULL) |
b34976b6 | 5437 | return FALSE; |
252b5132 RH |
5438 | |
5439 | /* Step One: Scan for segment vs section LMA conflicts. | |
5440 | Also add the sections to the section array allocated above. | |
5441 | Also add the sections to the current segment. In the common | |
5442 | case, where the sections have not been moved, this means that | |
5443 | we have completely filled the segment, and there is nothing | |
5444 | more to do. */ | |
252b5132 | 5445 | isec = 0; |
72730e0c | 5446 | matching_lma = 0; |
252b5132 | 5447 | suggested_lma = 0; |
0067a569 AM |
5448 | first_matching_lma = TRUE; |
5449 | first_suggested_lma = TRUE; | |
252b5132 | 5450 | |
147d51c2 | 5451 | for (section = ibfd->sections; |
bc67d8a6 NC |
5452 | section != NULL; |
5453 | section = section->next) | |
147d51c2 L |
5454 | if (section == first_section) |
5455 | break; | |
5456 | ||
5457 | for (j = 0; section != NULL; section = section->next) | |
252b5132 | 5458 | { |
caf47ea6 | 5459 | if (INCLUDE_SECTION_IN_SEGMENT (section, segment, bed)) |
c0f7859b | 5460 | { |
bc67d8a6 NC |
5461 | output_section = section->output_section; |
5462 | ||
0067a569 | 5463 | sections[j++] = section; |
252b5132 RH |
5464 | |
5465 | /* The Solaris native linker always sets p_paddr to 0. | |
5466 | We try to catch that case here, and set it to the | |
5e8d7549 NC |
5467 | correct value. Note - some backends require that |
5468 | p_paddr be left as zero. */ | |
5c44b38e | 5469 | if (!p_paddr_valid |
4455705d | 5470 | && segment->p_vaddr != 0 |
0067a569 | 5471 | && !bed->want_p_paddr_set_to_zero |
252b5132 | 5472 | && isec == 0 |
bc67d8a6 | 5473 | && output_section->lma != 0 |
0067a569 AM |
5474 | && output_section->vma == (segment->p_vaddr |
5475 | + (map->includes_filehdr | |
5476 | ? iehdr->e_ehsize | |
5477 | : 0) | |
5478 | + (map->includes_phdrs | |
5479 | ? (iehdr->e_phnum | |
5480 | * iehdr->e_phentsize) | |
5481 | : 0))) | |
bc67d8a6 | 5482 | map->p_paddr = segment->p_vaddr; |
252b5132 RH |
5483 | |
5484 | /* Match up the physical address of the segment with the | |
5485 | LMA address of the output section. */ | |
bc67d8a6 | 5486 | if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr) |
5e8d7549 | 5487 | || IS_COREFILE_NOTE (segment, section) |
0067a569 AM |
5488 | || (bed->want_p_paddr_set_to_zero |
5489 | && IS_CONTAINED_BY_VMA (output_section, segment))) | |
252b5132 | 5490 | { |
0067a569 AM |
5491 | if (first_matching_lma || output_section->lma < matching_lma) |
5492 | { | |
5493 | matching_lma = output_section->lma; | |
5494 | first_matching_lma = FALSE; | |
5495 | } | |
252b5132 RH |
5496 | |
5497 | /* We assume that if the section fits within the segment | |
bc67d8a6 | 5498 | then it does not overlap any other section within that |
252b5132 | 5499 | segment. */ |
0067a569 AM |
5500 | map->sections[isec++] = output_section; |
5501 | } | |
5502 | else if (first_suggested_lma) | |
5503 | { | |
5504 | suggested_lma = output_section->lma; | |
5505 | first_suggested_lma = FALSE; | |
252b5132 | 5506 | } |
147d51c2 L |
5507 | |
5508 | if (j == section_count) | |
5509 | break; | |
252b5132 RH |
5510 | } |
5511 | } | |
5512 | ||
bc67d8a6 | 5513 | BFD_ASSERT (j == section_count); |
252b5132 RH |
5514 | |
5515 | /* Step Two: Adjust the physical address of the current segment, | |
5516 | if necessary. */ | |
bc67d8a6 | 5517 | if (isec == section_count) |
252b5132 RH |
5518 | { |
5519 | /* All of the sections fitted within the segment as currently | |
5520 | specified. This is the default case. Add the segment to | |
5521 | the list of built segments and carry on to process the next | |
5522 | program header in the input BFD. */ | |
bc67d8a6 | 5523 | map->count = section_count; |
c044fabd KH |
5524 | *pointer_to_map = map; |
5525 | pointer_to_map = &map->next; | |
08a40648 | 5526 | |
5c44b38e AM |
5527 | if (p_paddr_valid |
5528 | && !bed->want_p_paddr_set_to_zero | |
147d51c2 | 5529 | && matching_lma != map->p_paddr |
5c44b38e AM |
5530 | && !map->includes_filehdr |
5531 | && !map->includes_phdrs) | |
3271a814 NS |
5532 | /* There is some padding before the first section in the |
5533 | segment. So, we must account for that in the output | |
5534 | segment's vma. */ | |
5535 | map->p_vaddr_offset = matching_lma - map->p_paddr; | |
08a40648 | 5536 | |
252b5132 RH |
5537 | free (sections); |
5538 | continue; | |
5539 | } | |
252b5132 RH |
5540 | else |
5541 | { | |
0067a569 | 5542 | if (!first_matching_lma) |
72730e0c AM |
5543 | { |
5544 | /* At least one section fits inside the current segment. | |
5545 | Keep it, but modify its physical address to match the | |
5546 | LMA of the first section that fitted. */ | |
bc67d8a6 | 5547 | map->p_paddr = matching_lma; |
72730e0c AM |
5548 | } |
5549 | else | |
5550 | { | |
5551 | /* None of the sections fitted inside the current segment. | |
5552 | Change the current segment's physical address to match | |
5553 | the LMA of the first section. */ | |
bc67d8a6 | 5554 | map->p_paddr = suggested_lma; |
72730e0c AM |
5555 | } |
5556 | ||
bc67d8a6 NC |
5557 | /* Offset the segment physical address from the lma |
5558 | to allow for space taken up by elf headers. */ | |
5559 | if (map->includes_filehdr) | |
010c8431 AM |
5560 | { |
5561 | if (map->p_paddr >= iehdr->e_ehsize) | |
5562 | map->p_paddr -= iehdr->e_ehsize; | |
5563 | else | |
5564 | { | |
5565 | map->includes_filehdr = FALSE; | |
5566 | map->includes_phdrs = FALSE; | |
5567 | } | |
5568 | } | |
252b5132 | 5569 | |
bc67d8a6 NC |
5570 | if (map->includes_phdrs) |
5571 | { | |
010c8431 AM |
5572 | if (map->p_paddr >= iehdr->e_phnum * iehdr->e_phentsize) |
5573 | { | |
5574 | map->p_paddr -= iehdr->e_phnum * iehdr->e_phentsize; | |
5575 | ||
5576 | /* iehdr->e_phnum is just an estimate of the number | |
5577 | of program headers that we will need. Make a note | |
5578 | here of the number we used and the segment we chose | |
5579 | to hold these headers, so that we can adjust the | |
5580 | offset when we know the correct value. */ | |
5581 | phdr_adjust_num = iehdr->e_phnum; | |
5582 | phdr_adjust_seg = map; | |
5583 | } | |
5584 | else | |
5585 | map->includes_phdrs = FALSE; | |
bc67d8a6 | 5586 | } |
252b5132 RH |
5587 | } |
5588 | ||
5589 | /* Step Three: Loop over the sections again, this time assigning | |
caf47ea6 | 5590 | those that fit to the current segment and removing them from the |
252b5132 RH |
5591 | sections array; but making sure not to leave large gaps. Once all |
5592 | possible sections have been assigned to the current segment it is | |
5593 | added to the list of built segments and if sections still remain | |
5594 | to be assigned, a new segment is constructed before repeating | |
5595 | the loop. */ | |
5596 | isec = 0; | |
5597 | do | |
5598 | { | |
bc67d8a6 | 5599 | map->count = 0; |
252b5132 | 5600 | suggested_lma = 0; |
0067a569 | 5601 | first_suggested_lma = TRUE; |
252b5132 RH |
5602 | |
5603 | /* Fill the current segment with sections that fit. */ | |
bc67d8a6 | 5604 | for (j = 0; j < section_count; j++) |
252b5132 | 5605 | { |
bc67d8a6 | 5606 | section = sections[j]; |
252b5132 | 5607 | |
bc67d8a6 | 5608 | if (section == NULL) |
252b5132 RH |
5609 | continue; |
5610 | ||
bc67d8a6 | 5611 | output_section = section->output_section; |
252b5132 | 5612 | |
bc67d8a6 | 5613 | BFD_ASSERT (output_section != NULL); |
c044fabd | 5614 | |
bc67d8a6 NC |
5615 | if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr) |
5616 | || IS_COREFILE_NOTE (segment, section)) | |
252b5132 | 5617 | { |
bc67d8a6 | 5618 | if (map->count == 0) |
252b5132 RH |
5619 | { |
5620 | /* If the first section in a segment does not start at | |
bc67d8a6 NC |
5621 | the beginning of the segment, then something is |
5622 | wrong. */ | |
0067a569 AM |
5623 | if (output_section->lma |
5624 | != (map->p_paddr | |
5625 | + (map->includes_filehdr ? iehdr->e_ehsize : 0) | |
5626 | + (map->includes_phdrs | |
5627 | ? iehdr->e_phnum * iehdr->e_phentsize | |
5628 | : 0))) | |
252b5132 RH |
5629 | abort (); |
5630 | } | |
5631 | else | |
5632 | { | |
0067a569 | 5633 | asection *prev_sec; |
252b5132 | 5634 | |
bc67d8a6 | 5635 | prev_sec = map->sections[map->count - 1]; |
252b5132 RH |
5636 | |
5637 | /* If the gap between the end of the previous section | |
bc67d8a6 NC |
5638 | and the start of this section is more than |
5639 | maxpagesize then we need to start a new segment. */ | |
eea6121a | 5640 | if ((BFD_ALIGN (prev_sec->lma + prev_sec->size, |
079e9a2f | 5641 | maxpagesize) |
caf47ea6 | 5642 | < BFD_ALIGN (output_section->lma, maxpagesize)) |
0067a569 | 5643 | || (prev_sec->lma + prev_sec->size |
079e9a2f | 5644 | > output_section->lma)) |
252b5132 | 5645 | { |
0067a569 AM |
5646 | if (first_suggested_lma) |
5647 | { | |
5648 | suggested_lma = output_section->lma; | |
5649 | first_suggested_lma = FALSE; | |
5650 | } | |
252b5132 RH |
5651 | |
5652 | continue; | |
5653 | } | |
5654 | } | |
5655 | ||
bc67d8a6 | 5656 | map->sections[map->count++] = output_section; |
252b5132 RH |
5657 | ++isec; |
5658 | sections[j] = NULL; | |
b34976b6 | 5659 | section->segment_mark = TRUE; |
252b5132 | 5660 | } |
0067a569 AM |
5661 | else if (first_suggested_lma) |
5662 | { | |
5663 | suggested_lma = output_section->lma; | |
5664 | first_suggested_lma = FALSE; | |
5665 | } | |
252b5132 RH |
5666 | } |
5667 | ||
bc67d8a6 | 5668 | BFD_ASSERT (map->count > 0); |
252b5132 RH |
5669 | |
5670 | /* Add the current segment to the list of built segments. */ | |
c044fabd KH |
5671 | *pointer_to_map = map; |
5672 | pointer_to_map = &map->next; | |
252b5132 | 5673 | |
bc67d8a6 | 5674 | if (isec < section_count) |
252b5132 RH |
5675 | { |
5676 | /* We still have not allocated all of the sections to | |
5677 | segments. Create a new segment here, initialise it | |
5678 | and carry on looping. */ | |
dc810e39 AM |
5679 | amt = sizeof (struct elf_segment_map); |
5680 | amt += ((bfd_size_type) section_count - 1) * sizeof (asection *); | |
217aa764 | 5681 | map = bfd_alloc (obfd, amt); |
bc67d8a6 | 5682 | if (map == NULL) |
5ed6aba4 NC |
5683 | { |
5684 | free (sections); | |
5685 | return FALSE; | |
5686 | } | |
252b5132 RH |
5687 | |
5688 | /* Initialise the fields of the segment map. Set the physical | |
5689 | physical address to the LMA of the first section that has | |
5690 | not yet been assigned. */ | |
0067a569 AM |
5691 | map->next = NULL; |
5692 | map->p_type = segment->p_type; | |
5693 | map->p_flags = segment->p_flags; | |
5694 | map->p_flags_valid = 1; | |
5695 | map->p_paddr = suggested_lma; | |
5c44b38e | 5696 | map->p_paddr_valid = p_paddr_valid; |
bc67d8a6 | 5697 | map->includes_filehdr = 0; |
0067a569 | 5698 | map->includes_phdrs = 0; |
252b5132 RH |
5699 | } |
5700 | } | |
bc67d8a6 | 5701 | while (isec < section_count); |
252b5132 RH |
5702 | |
5703 | free (sections); | |
5704 | } | |
5705 | ||
bc67d8a6 NC |
5706 | elf_tdata (obfd)->segment_map = map_first; |
5707 | ||
5708 | /* If we had to estimate the number of program headers that were | |
9ad5cbcf | 5709 | going to be needed, then check our estimate now and adjust |
bc67d8a6 NC |
5710 | the offset if necessary. */ |
5711 | if (phdr_adjust_seg != NULL) | |
5712 | { | |
5713 | unsigned int count; | |
c044fabd | 5714 | |
bc67d8a6 | 5715 | for (count = 0, map = map_first; map != NULL; map = map->next) |
c044fabd | 5716 | count++; |
252b5132 | 5717 | |
bc67d8a6 NC |
5718 | if (count > phdr_adjust_num) |
5719 | phdr_adjust_seg->p_paddr | |
5720 | -= (count - phdr_adjust_num) * iehdr->e_phentsize; | |
5721 | } | |
c044fabd | 5722 | |
bc67d8a6 | 5723 | #undef SEGMENT_END |
eecdbe52 | 5724 | #undef SECTION_SIZE |
bc67d8a6 NC |
5725 | #undef IS_CONTAINED_BY_VMA |
5726 | #undef IS_CONTAINED_BY_LMA | |
0efc80c8 | 5727 | #undef IS_NOTE |
252b5132 | 5728 | #undef IS_COREFILE_NOTE |
bc67d8a6 | 5729 | #undef IS_SOLARIS_PT_INTERP |
9f17e2a6 | 5730 | #undef IS_SECTION_IN_INPUT_SEGMENT |
bc67d8a6 NC |
5731 | #undef INCLUDE_SECTION_IN_SEGMENT |
5732 | #undef SEGMENT_AFTER_SEGMENT | |
5733 | #undef SEGMENT_OVERLAPS | |
b34976b6 | 5734 | return TRUE; |
252b5132 RH |
5735 | } |
5736 | ||
84d1d650 L |
5737 | /* Copy ELF program header information. */ |
5738 | ||
5739 | static bfd_boolean | |
5740 | copy_elf_program_header (bfd *ibfd, bfd *obfd) | |
5741 | { | |
5742 | Elf_Internal_Ehdr *iehdr; | |
5743 | struct elf_segment_map *map; | |
5744 | struct elf_segment_map *map_first; | |
5745 | struct elf_segment_map **pointer_to_map; | |
5746 | Elf_Internal_Phdr *segment; | |
5747 | unsigned int i; | |
5748 | unsigned int num_segments; | |
5749 | bfd_boolean phdr_included = FALSE; | |
88967714 | 5750 | bfd_boolean p_paddr_valid; |
84d1d650 L |
5751 | |
5752 | iehdr = elf_elfheader (ibfd); | |
5753 | ||
5754 | map_first = NULL; | |
5755 | pointer_to_map = &map_first; | |
5756 | ||
88967714 AM |
5757 | /* If all the segment p_paddr fields are zero, don't set |
5758 | map->p_paddr_valid. */ | |
5759 | p_paddr_valid = FALSE; | |
84d1d650 | 5760 | num_segments = elf_elfheader (ibfd)->e_phnum; |
88967714 AM |
5761 | for (i = 0, segment = elf_tdata (ibfd)->phdr; |
5762 | i < num_segments; | |
5763 | i++, segment++) | |
5764 | if (segment->p_paddr != 0) | |
5765 | { | |
5766 | p_paddr_valid = TRUE; | |
5767 | break; | |
5768 | } | |
5769 | ||
84d1d650 L |
5770 | for (i = 0, segment = elf_tdata (ibfd)->phdr; |
5771 | i < num_segments; | |
5772 | i++, segment++) | |
5773 | { | |
5774 | asection *section; | |
5775 | unsigned int section_count; | |
5776 | bfd_size_type amt; | |
5777 | Elf_Internal_Shdr *this_hdr; | |
53020534 | 5778 | asection *first_section = NULL; |
c981028a | 5779 | asection *lowest_section = NULL; |
84d1d650 | 5780 | |
84d1d650 L |
5781 | /* Compute how many sections are in this segment. */ |
5782 | for (section = ibfd->sections, section_count = 0; | |
5783 | section != NULL; | |
5784 | section = section->next) | |
5785 | { | |
5786 | this_hdr = &(elf_section_data(section)->this_hdr); | |
5787 | if (ELF_IS_SECTION_IN_SEGMENT_FILE (this_hdr, segment)) | |
3271a814 | 5788 | { |
53020534 | 5789 | if (!first_section) |
c981028a DJ |
5790 | first_section = lowest_section = section; |
5791 | if (section->lma < lowest_section->lma) | |
5792 | lowest_section = section; | |
3271a814 NS |
5793 | section_count++; |
5794 | } | |
84d1d650 L |
5795 | } |
5796 | ||
5797 | /* Allocate a segment map big enough to contain | |
5798 | all of the sections we have selected. */ | |
5799 | amt = sizeof (struct elf_segment_map); | |
5800 | if (section_count != 0) | |
5801 | amt += ((bfd_size_type) section_count - 1) * sizeof (asection *); | |
41f8ce69 | 5802 | map = bfd_zalloc (obfd, amt); |
84d1d650 L |
5803 | if (map == NULL) |
5804 | return FALSE; | |
5805 | ||
5806 | /* Initialize the fields of the output segment map with the | |
5807 | input segment. */ | |
5808 | map->next = NULL; | |
5809 | map->p_type = segment->p_type; | |
5810 | map->p_flags = segment->p_flags; | |
5811 | map->p_flags_valid = 1; | |
5812 | map->p_paddr = segment->p_paddr; | |
88967714 | 5813 | map->p_paddr_valid = p_paddr_valid; |
3f570048 AM |
5814 | map->p_align = segment->p_align; |
5815 | map->p_align_valid = 1; | |
3271a814 | 5816 | map->p_vaddr_offset = 0; |
84d1d650 | 5817 | |
9433b9b1 | 5818 | if (map->p_type == PT_GNU_RELRO) |
b10a8ae0 L |
5819 | { |
5820 | /* The PT_GNU_RELRO segment may contain the first a few | |
5821 | bytes in the .got.plt section even if the whole .got.plt | |
5822 | section isn't in the PT_GNU_RELRO segment. We won't | |
5823 | change the size of the PT_GNU_RELRO segment. */ | |
9433b9b1 | 5824 | map->p_size = segment->p_memsz; |
b10a8ae0 L |
5825 | map->p_size_valid = 1; |
5826 | } | |
5827 | ||
84d1d650 L |
5828 | /* Determine if this segment contains the ELF file header |
5829 | and if it contains the program headers themselves. */ | |
5830 | map->includes_filehdr = (segment->p_offset == 0 | |
5831 | && segment->p_filesz >= iehdr->e_ehsize); | |
5832 | ||
5833 | map->includes_phdrs = 0; | |
5834 | if (! phdr_included || segment->p_type != PT_LOAD) | |
5835 | { | |
5836 | map->includes_phdrs = | |
5837 | (segment->p_offset <= (bfd_vma) iehdr->e_phoff | |
5838 | && (segment->p_offset + segment->p_filesz | |
5839 | >= ((bfd_vma) iehdr->e_phoff | |
5840 | + iehdr->e_phnum * iehdr->e_phentsize))); | |
5841 | ||
5842 | if (segment->p_type == PT_LOAD && map->includes_phdrs) | |
5843 | phdr_included = TRUE; | |
5844 | } | |
5845 | ||
2b0bc088 NC |
5846 | if (map->includes_filehdr && first_section) |
5847 | /* We need to keep the space used by the headers fixed. */ | |
5848 | map->header_size = first_section->vma - segment->p_vaddr; | |
5849 | ||
88967714 AM |
5850 | if (!map->includes_phdrs |
5851 | && !map->includes_filehdr | |
5852 | && map->p_paddr_valid) | |
3271a814 | 5853 | /* There is some other padding before the first section. */ |
c981028a | 5854 | map->p_vaddr_offset = ((lowest_section ? lowest_section->lma : 0) |
53020534 | 5855 | - segment->p_paddr); |
08a40648 | 5856 | |
84d1d650 L |
5857 | if (section_count != 0) |
5858 | { | |
5859 | unsigned int isec = 0; | |
5860 | ||
53020534 | 5861 | for (section = first_section; |
84d1d650 L |
5862 | section != NULL; |
5863 | section = section->next) | |
5864 | { | |
5865 | this_hdr = &(elf_section_data(section)->this_hdr); | |
5866 | if (ELF_IS_SECTION_IN_SEGMENT_FILE (this_hdr, segment)) | |
53020534 L |
5867 | { |
5868 | map->sections[isec++] = section->output_section; | |
5869 | if (isec == section_count) | |
5870 | break; | |
5871 | } | |
84d1d650 L |
5872 | } |
5873 | } | |
5874 | ||
5875 | map->count = section_count; | |
5876 | *pointer_to_map = map; | |
5877 | pointer_to_map = &map->next; | |
5878 | } | |
5879 | ||
5880 | elf_tdata (obfd)->segment_map = map_first; | |
5881 | return TRUE; | |
5882 | } | |
5883 | ||
5884 | /* Copy private BFD data. This copies or rewrites ELF program header | |
5885 | information. */ | |
5886 | ||
5887 | static bfd_boolean | |
5888 | copy_private_bfd_data (bfd *ibfd, bfd *obfd) | |
5889 | { | |
84d1d650 L |
5890 | if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour |
5891 | || bfd_get_flavour (obfd) != bfd_target_elf_flavour) | |
5892 | return TRUE; | |
5893 | ||
5894 | if (elf_tdata (ibfd)->phdr == NULL) | |
5895 | return TRUE; | |
5896 | ||
5897 | if (ibfd->xvec == obfd->xvec) | |
5898 | { | |
cb3ff1e5 NC |
5899 | /* Check to see if any sections in the input BFD |
5900 | covered by ELF program header have changed. */ | |
d55ce4e2 | 5901 | Elf_Internal_Phdr *segment; |
84d1d650 L |
5902 | asection *section, *osec; |
5903 | unsigned int i, num_segments; | |
5904 | Elf_Internal_Shdr *this_hdr; | |
147d51c2 L |
5905 | const struct elf_backend_data *bed; |
5906 | ||
5907 | bed = get_elf_backend_data (ibfd); | |
5908 | ||
5909 | /* Regenerate the segment map if p_paddr is set to 0. */ | |
5910 | if (bed->want_p_paddr_set_to_zero) | |
5911 | goto rewrite; | |
84d1d650 L |
5912 | |
5913 | /* Initialize the segment mark field. */ | |
5914 | for (section = obfd->sections; section != NULL; | |
5915 | section = section->next) | |
5916 | section->segment_mark = FALSE; | |
5917 | ||
5918 | num_segments = elf_elfheader (ibfd)->e_phnum; | |
5919 | for (i = 0, segment = elf_tdata (ibfd)->phdr; | |
5920 | i < num_segments; | |
5921 | i++, segment++) | |
5922 | { | |
5f6999aa NC |
5923 | /* PR binutils/3535. The Solaris linker always sets the p_paddr |
5924 | and p_memsz fields of special segments (DYNAMIC, INTERP) to 0 | |
5925 | which severly confuses things, so always regenerate the segment | |
5926 | map in this case. */ | |
5927 | if (segment->p_paddr == 0 | |
5928 | && segment->p_memsz == 0 | |
5929 | && (segment->p_type == PT_INTERP || segment->p_type == PT_DYNAMIC)) | |
cb3ff1e5 | 5930 | goto rewrite; |
5f6999aa | 5931 | |
84d1d650 L |
5932 | for (section = ibfd->sections; |
5933 | section != NULL; section = section->next) | |
5934 | { | |
5935 | /* We mark the output section so that we know it comes | |
5936 | from the input BFD. */ | |
5937 | osec = section->output_section; | |
5938 | if (osec) | |
5939 | osec->segment_mark = TRUE; | |
5940 | ||
5941 | /* Check if this section is covered by the segment. */ | |
5942 | this_hdr = &(elf_section_data(section)->this_hdr); | |
5943 | if (ELF_IS_SECTION_IN_SEGMENT_FILE (this_hdr, segment)) | |
5944 | { | |
5945 | /* FIXME: Check if its output section is changed or | |
5946 | removed. What else do we need to check? */ | |
5947 | if (osec == NULL | |
5948 | || section->flags != osec->flags | |
5949 | || section->lma != osec->lma | |
5950 | || section->vma != osec->vma | |
5951 | || section->size != osec->size | |
5952 | || section->rawsize != osec->rawsize | |
5953 | || section->alignment_power != osec->alignment_power) | |
5954 | goto rewrite; | |
5955 | } | |
5956 | } | |
5957 | } | |
5958 | ||
cb3ff1e5 | 5959 | /* Check to see if any output section do not come from the |
84d1d650 L |
5960 | input BFD. */ |
5961 | for (section = obfd->sections; section != NULL; | |
5962 | section = section->next) | |
5963 | { | |
5964 | if (section->segment_mark == FALSE) | |
5965 | goto rewrite; | |
5966 | else | |
5967 | section->segment_mark = FALSE; | |
5968 | } | |
5969 | ||
5970 | return copy_elf_program_header (ibfd, obfd); | |
5971 | } | |
5972 | ||
5973 | rewrite: | |
5974 | return rewrite_elf_program_header (ibfd, obfd); | |
5975 | } | |
5976 | ||
ccd2ec6a L |
5977 | /* Initialize private output section information from input section. */ |
5978 | ||
5979 | bfd_boolean | |
5980 | _bfd_elf_init_private_section_data (bfd *ibfd, | |
5981 | asection *isec, | |
5982 | bfd *obfd, | |
5983 | asection *osec, | |
5984 | struct bfd_link_info *link_info) | |
5985 | ||
5986 | { | |
5987 | Elf_Internal_Shdr *ihdr, *ohdr; | |
5988 | bfd_boolean need_group = link_info == NULL || link_info->relocatable; | |
5989 | ||
5990 | if (ibfd->xvec->flavour != bfd_target_elf_flavour | |
5991 | || obfd->xvec->flavour != bfd_target_elf_flavour) | |
5992 | return TRUE; | |
5993 | ||
e843e0f8 | 5994 | /* Don't copy the output ELF section type from input if the |
d3fd4074 | 5995 | output BFD section flags have been set to something different. |
e843e0f8 L |
5996 | elf_fake_sections will set ELF section type based on BFD |
5997 | section flags. */ | |
42bb2e33 AM |
5998 | if (elf_section_type (osec) == SHT_NULL |
5999 | && (osec->flags == isec->flags || !osec->flags)) | |
6000 | elf_section_type (osec) = elf_section_type (isec); | |
d270463e L |
6001 | |
6002 | /* FIXME: Is this correct for all OS/PROC specific flags? */ | |
6003 | elf_section_flags (osec) |= (elf_section_flags (isec) | |
6004 | & (SHF_MASKOS | SHF_MASKPROC)); | |
ccd2ec6a L |
6005 | |
6006 | /* Set things up for objcopy and relocatable link. The output | |
6007 | SHT_GROUP section will have its elf_next_in_group pointing back | |
6008 | to the input group members. Ignore linker created group section. | |
6009 | See elfNN_ia64_object_p in elfxx-ia64.c. */ | |
ccd2ec6a L |
6010 | if (need_group) |
6011 | { | |
6012 | if (elf_sec_group (isec) == NULL | |
6013 | || (elf_sec_group (isec)->flags & SEC_LINKER_CREATED) == 0) | |
6014 | { | |
6015 | if (elf_section_flags (isec) & SHF_GROUP) | |
6016 | elf_section_flags (osec) |= SHF_GROUP; | |
6017 | elf_next_in_group (osec) = elf_next_in_group (isec); | |
9659de1c | 6018 | elf_section_data (osec)->group = elf_section_data (isec)->group; |
ccd2ec6a L |
6019 | } |
6020 | } | |
6021 | ||
6022 | ihdr = &elf_section_data (isec)->this_hdr; | |
6023 | ||
6024 | /* We need to handle elf_linked_to_section for SHF_LINK_ORDER. We | |
6025 | don't use the output section of the linked-to section since it | |
6026 | may be NULL at this point. */ | |
6027 | if ((ihdr->sh_flags & SHF_LINK_ORDER) != 0) | |
6028 | { | |
6029 | ohdr = &elf_section_data (osec)->this_hdr; | |
6030 | ohdr->sh_flags |= SHF_LINK_ORDER; | |
6031 | elf_linked_to_section (osec) = elf_linked_to_section (isec); | |
6032 | } | |
6033 | ||
6034 | osec->use_rela_p = isec->use_rela_p; | |
6035 | ||
6036 | return TRUE; | |
6037 | } | |
6038 | ||
252b5132 RH |
6039 | /* Copy private section information. This copies over the entsize |
6040 | field, and sometimes the info field. */ | |
6041 | ||
b34976b6 | 6042 | bfd_boolean |
217aa764 AM |
6043 | _bfd_elf_copy_private_section_data (bfd *ibfd, |
6044 | asection *isec, | |
6045 | bfd *obfd, | |
6046 | asection *osec) | |
252b5132 RH |
6047 | { |
6048 | Elf_Internal_Shdr *ihdr, *ohdr; | |
6049 | ||
6050 | if (ibfd->xvec->flavour != bfd_target_elf_flavour | |
6051 | || obfd->xvec->flavour != bfd_target_elf_flavour) | |
b34976b6 | 6052 | return TRUE; |
252b5132 | 6053 | |
252b5132 RH |
6054 | ihdr = &elf_section_data (isec)->this_hdr; |
6055 | ohdr = &elf_section_data (osec)->this_hdr; | |
6056 | ||
6057 | ohdr->sh_entsize = ihdr->sh_entsize; | |
6058 | ||
6059 | if (ihdr->sh_type == SHT_SYMTAB | |
6060 | || ihdr->sh_type == SHT_DYNSYM | |
6061 | || ihdr->sh_type == SHT_GNU_verneed | |
6062 | || ihdr->sh_type == SHT_GNU_verdef) | |
6063 | ohdr->sh_info = ihdr->sh_info; | |
6064 | ||
ccd2ec6a L |
6065 | return _bfd_elf_init_private_section_data (ibfd, isec, obfd, osec, |
6066 | NULL); | |
252b5132 RH |
6067 | } |
6068 | ||
80fccad2 BW |
6069 | /* Copy private header information. */ |
6070 | ||
6071 | bfd_boolean | |
6072 | _bfd_elf_copy_private_header_data (bfd *ibfd, bfd *obfd) | |
6073 | { | |
30288845 AM |
6074 | asection *isec; |
6075 | ||
80fccad2 BW |
6076 | if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour |
6077 | || bfd_get_flavour (obfd) != bfd_target_elf_flavour) | |
6078 | return TRUE; | |
6079 | ||
6080 | /* Copy over private BFD data if it has not already been copied. | |
6081 | This must be done here, rather than in the copy_private_bfd_data | |
6082 | entry point, because the latter is called after the section | |
6083 | contents have been set, which means that the program headers have | |
6084 | already been worked out. */ | |
6085 | if (elf_tdata (obfd)->segment_map == NULL && elf_tdata (ibfd)->phdr != NULL) | |
6086 | { | |
6087 | if (! copy_private_bfd_data (ibfd, obfd)) | |
6088 | return FALSE; | |
6089 | } | |
6090 | ||
30288845 AM |
6091 | /* _bfd_elf_copy_private_section_data copied over the SHF_GROUP flag |
6092 | but this might be wrong if we deleted the group section. */ | |
6093 | for (isec = ibfd->sections; isec != NULL; isec = isec->next) | |
6094 | if (elf_section_type (isec) == SHT_GROUP | |
6095 | && isec->output_section == NULL) | |
6096 | { | |
6097 | asection *first = elf_next_in_group (isec); | |
6098 | asection *s = first; | |
6099 | while (s != NULL) | |
6100 | { | |
6101 | if (s->output_section != NULL) | |
6102 | { | |
6103 | elf_section_flags (s->output_section) &= ~SHF_GROUP; | |
6104 | elf_group_name (s->output_section) = NULL; | |
6105 | } | |
6106 | s = elf_next_in_group (s); | |
6107 | if (s == first) | |
6108 | break; | |
6109 | } | |
6110 | } | |
6111 | ||
80fccad2 BW |
6112 | return TRUE; |
6113 | } | |
6114 | ||
252b5132 RH |
6115 | /* Copy private symbol information. If this symbol is in a section |
6116 | which we did not map into a BFD section, try to map the section | |
6117 | index correctly. We use special macro definitions for the mapped | |
6118 | section indices; these definitions are interpreted by the | |
6119 | swap_out_syms function. */ | |
6120 | ||
9ad5cbcf AM |
6121 | #define MAP_ONESYMTAB (SHN_HIOS + 1) |
6122 | #define MAP_DYNSYMTAB (SHN_HIOS + 2) | |
6123 | #define MAP_STRTAB (SHN_HIOS + 3) | |
6124 | #define MAP_SHSTRTAB (SHN_HIOS + 4) | |
6125 | #define MAP_SYM_SHNDX (SHN_HIOS + 5) | |
252b5132 | 6126 | |
b34976b6 | 6127 | bfd_boolean |
217aa764 AM |
6128 | _bfd_elf_copy_private_symbol_data (bfd *ibfd, |
6129 | asymbol *isymarg, | |
6130 | bfd *obfd, | |
6131 | asymbol *osymarg) | |
252b5132 RH |
6132 | { |
6133 | elf_symbol_type *isym, *osym; | |
6134 | ||
6135 | if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour | |
6136 | || bfd_get_flavour (obfd) != bfd_target_elf_flavour) | |
b34976b6 | 6137 | return TRUE; |
252b5132 RH |
6138 | |
6139 | isym = elf_symbol_from (ibfd, isymarg); | |
6140 | osym = elf_symbol_from (obfd, osymarg); | |
6141 | ||
6142 | if (isym != NULL | |
8424d8f5 | 6143 | && isym->internal_elf_sym.st_shndx != 0 |
252b5132 RH |
6144 | && osym != NULL |
6145 | && bfd_is_abs_section (isym->symbol.section)) | |
6146 | { | |
6147 | unsigned int shndx; | |
6148 | ||
6149 | shndx = isym->internal_elf_sym.st_shndx; | |
6150 | if (shndx == elf_onesymtab (ibfd)) | |
6151 | shndx = MAP_ONESYMTAB; | |
6152 | else if (shndx == elf_dynsymtab (ibfd)) | |
6153 | shndx = MAP_DYNSYMTAB; | |
6154 | else if (shndx == elf_tdata (ibfd)->strtab_section) | |
6155 | shndx = MAP_STRTAB; | |
6156 | else if (shndx == elf_tdata (ibfd)->shstrtab_section) | |
6157 | shndx = MAP_SHSTRTAB; | |
9ad5cbcf AM |
6158 | else if (shndx == elf_tdata (ibfd)->symtab_shndx_section) |
6159 | shndx = MAP_SYM_SHNDX; | |
252b5132 RH |
6160 | osym->internal_elf_sym.st_shndx = shndx; |
6161 | } | |
6162 | ||
b34976b6 | 6163 | return TRUE; |
252b5132 RH |
6164 | } |
6165 | ||
6166 | /* Swap out the symbols. */ | |
6167 | ||
b34976b6 | 6168 | static bfd_boolean |
217aa764 AM |
6169 | swap_out_syms (bfd *abfd, |
6170 | struct bfd_strtab_hash **sttp, | |
6171 | int relocatable_p) | |
252b5132 | 6172 | { |
9c5bfbb7 | 6173 | const struct elf_backend_data *bed; |
079e9a2f AM |
6174 | int symcount; |
6175 | asymbol **syms; | |
6176 | struct bfd_strtab_hash *stt; | |
6177 | Elf_Internal_Shdr *symtab_hdr; | |
9ad5cbcf | 6178 | Elf_Internal_Shdr *symtab_shndx_hdr; |
079e9a2f | 6179 | Elf_Internal_Shdr *symstrtab_hdr; |
f075ee0c AM |
6180 | bfd_byte *outbound_syms; |
6181 | bfd_byte *outbound_shndx; | |
079e9a2f AM |
6182 | int idx; |
6183 | bfd_size_type amt; | |
174fd7f9 | 6184 | bfd_boolean name_local_sections; |
252b5132 RH |
6185 | |
6186 | if (!elf_map_symbols (abfd)) | |
b34976b6 | 6187 | return FALSE; |
252b5132 | 6188 | |
c044fabd | 6189 | /* Dump out the symtabs. */ |
079e9a2f AM |
6190 | stt = _bfd_elf_stringtab_init (); |
6191 | if (stt == NULL) | |
b34976b6 | 6192 | return FALSE; |
252b5132 | 6193 | |
079e9a2f AM |
6194 | bed = get_elf_backend_data (abfd); |
6195 | symcount = bfd_get_symcount (abfd); | |
6196 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; | |
6197 | symtab_hdr->sh_type = SHT_SYMTAB; | |
6198 | symtab_hdr->sh_entsize = bed->s->sizeof_sym; | |
6199 | symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1); | |
6200 | symtab_hdr->sh_info = elf_num_locals (abfd) + 1; | |
72de5009 | 6201 | symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align; |
079e9a2f AM |
6202 | |
6203 | symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr; | |
6204 | symstrtab_hdr->sh_type = SHT_STRTAB; | |
6205 | ||
d0fb9a8d | 6206 | outbound_syms = bfd_alloc2 (abfd, 1 + symcount, bed->s->sizeof_sym); |
079e9a2f | 6207 | if (outbound_syms == NULL) |
5ed6aba4 NC |
6208 | { |
6209 | _bfd_stringtab_free (stt); | |
6210 | return FALSE; | |
6211 | } | |
217aa764 | 6212 | symtab_hdr->contents = outbound_syms; |
252b5132 | 6213 | |
9ad5cbcf AM |
6214 | outbound_shndx = NULL; |
6215 | symtab_shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr; | |
6216 | if (symtab_shndx_hdr->sh_name != 0) | |
6217 | { | |
6218 | amt = (bfd_size_type) (1 + symcount) * sizeof (Elf_External_Sym_Shndx); | |
d0fb9a8d JJ |
6219 | outbound_shndx = bfd_zalloc2 (abfd, 1 + symcount, |
6220 | sizeof (Elf_External_Sym_Shndx)); | |
9ad5cbcf | 6221 | if (outbound_shndx == NULL) |
5ed6aba4 NC |
6222 | { |
6223 | _bfd_stringtab_free (stt); | |
6224 | return FALSE; | |
6225 | } | |
6226 | ||
9ad5cbcf AM |
6227 | symtab_shndx_hdr->contents = outbound_shndx; |
6228 | symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX; | |
6229 | symtab_shndx_hdr->sh_size = amt; | |
6230 | symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx); | |
6231 | symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx); | |
6232 | } | |
6233 | ||
589e6347 | 6234 | /* Now generate the data (for "contents"). */ |
079e9a2f AM |
6235 | { |
6236 | /* Fill in zeroth symbol and swap it out. */ | |
6237 | Elf_Internal_Sym sym; | |
6238 | sym.st_name = 0; | |
6239 | sym.st_value = 0; | |
6240 | sym.st_size = 0; | |
6241 | sym.st_info = 0; | |
6242 | sym.st_other = 0; | |
6243 | sym.st_shndx = SHN_UNDEF; | |
9ad5cbcf | 6244 | bed->s->swap_symbol_out (abfd, &sym, outbound_syms, outbound_shndx); |
079e9a2f | 6245 | outbound_syms += bed->s->sizeof_sym; |
9ad5cbcf AM |
6246 | if (outbound_shndx != NULL) |
6247 | outbound_shndx += sizeof (Elf_External_Sym_Shndx); | |
079e9a2f | 6248 | } |
252b5132 | 6249 | |
174fd7f9 RS |
6250 | name_local_sections |
6251 | = (bed->elf_backend_name_local_section_symbols | |
6252 | && bed->elf_backend_name_local_section_symbols (abfd)); | |
6253 | ||
079e9a2f AM |
6254 | syms = bfd_get_outsymbols (abfd); |
6255 | for (idx = 0; idx < symcount; idx++) | |
252b5132 | 6256 | { |
252b5132 | 6257 | Elf_Internal_Sym sym; |
079e9a2f AM |
6258 | bfd_vma value = syms[idx]->value; |
6259 | elf_symbol_type *type_ptr; | |
6260 | flagword flags = syms[idx]->flags; | |
6261 | int type; | |
252b5132 | 6262 | |
174fd7f9 RS |
6263 | if (!name_local_sections |
6264 | && (flags & (BSF_SECTION_SYM | BSF_GLOBAL)) == BSF_SECTION_SYM) | |
079e9a2f AM |
6265 | { |
6266 | /* Local section symbols have no name. */ | |
6267 | sym.st_name = 0; | |
6268 | } | |
6269 | else | |
6270 | { | |
6271 | sym.st_name = (unsigned long) _bfd_stringtab_add (stt, | |
6272 | syms[idx]->name, | |
b34976b6 | 6273 | TRUE, FALSE); |
079e9a2f | 6274 | if (sym.st_name == (unsigned long) -1) |
5ed6aba4 NC |
6275 | { |
6276 | _bfd_stringtab_free (stt); | |
6277 | return FALSE; | |
6278 | } | |
079e9a2f | 6279 | } |
252b5132 | 6280 | |
079e9a2f | 6281 | type_ptr = elf_symbol_from (abfd, syms[idx]); |
252b5132 | 6282 | |
079e9a2f AM |
6283 | if ((flags & BSF_SECTION_SYM) == 0 |
6284 | && bfd_is_com_section (syms[idx]->section)) | |
6285 | { | |
6286 | /* ELF common symbols put the alignment into the `value' field, | |
6287 | and the size into the `size' field. This is backwards from | |
6288 | how BFD handles it, so reverse it here. */ | |
6289 | sym.st_size = value; | |
6290 | if (type_ptr == NULL | |
6291 | || type_ptr->internal_elf_sym.st_value == 0) | |
6292 | sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value)); | |
6293 | else | |
6294 | sym.st_value = type_ptr->internal_elf_sym.st_value; | |
6295 | sym.st_shndx = _bfd_elf_section_from_bfd_section | |
6296 | (abfd, syms[idx]->section); | |
6297 | } | |
6298 | else | |
6299 | { | |
6300 | asection *sec = syms[idx]->section; | |
cb33740c | 6301 | unsigned int shndx; |
252b5132 | 6302 | |
079e9a2f AM |
6303 | if (sec->output_section) |
6304 | { | |
6305 | value += sec->output_offset; | |
6306 | sec = sec->output_section; | |
6307 | } | |
589e6347 | 6308 | |
079e9a2f AM |
6309 | /* Don't add in the section vma for relocatable output. */ |
6310 | if (! relocatable_p) | |
6311 | value += sec->vma; | |
6312 | sym.st_value = value; | |
6313 | sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0; | |
6314 | ||
6315 | if (bfd_is_abs_section (sec) | |
6316 | && type_ptr != NULL | |
6317 | && type_ptr->internal_elf_sym.st_shndx != 0) | |
6318 | { | |
6319 | /* This symbol is in a real ELF section which we did | |
6320 | not create as a BFD section. Undo the mapping done | |
6321 | by copy_private_symbol_data. */ | |
6322 | shndx = type_ptr->internal_elf_sym.st_shndx; | |
6323 | switch (shndx) | |
6324 | { | |
6325 | case MAP_ONESYMTAB: | |
6326 | shndx = elf_onesymtab (abfd); | |
6327 | break; | |
6328 | case MAP_DYNSYMTAB: | |
6329 | shndx = elf_dynsymtab (abfd); | |
6330 | break; | |
6331 | case MAP_STRTAB: | |
6332 | shndx = elf_tdata (abfd)->strtab_section; | |
6333 | break; | |
6334 | case MAP_SHSTRTAB: | |
6335 | shndx = elf_tdata (abfd)->shstrtab_section; | |
6336 | break; | |
9ad5cbcf AM |
6337 | case MAP_SYM_SHNDX: |
6338 | shndx = elf_tdata (abfd)->symtab_shndx_section; | |
6339 | break; | |
079e9a2f AM |
6340 | default: |
6341 | break; | |
6342 | } | |
6343 | } | |
6344 | else | |
6345 | { | |
6346 | shndx = _bfd_elf_section_from_bfd_section (abfd, sec); | |
252b5132 | 6347 | |
cb33740c | 6348 | if (shndx == SHN_BAD) |
079e9a2f AM |
6349 | { |
6350 | asection *sec2; | |
6351 | ||
6352 | /* Writing this would be a hell of a lot easier if | |
6353 | we had some decent documentation on bfd, and | |
6354 | knew what to expect of the library, and what to | |
6355 | demand of applications. For example, it | |
6356 | appears that `objcopy' might not set the | |
6357 | section of a symbol to be a section that is | |
6358 | actually in the output file. */ | |
6359 | sec2 = bfd_get_section_by_name (abfd, sec->name); | |
589e6347 NC |
6360 | if (sec2 == NULL) |
6361 | { | |
6362 | _bfd_error_handler (_("\ | |
6363 | Unable to find equivalent output section for symbol '%s' from section '%s'"), | |
6364 | syms[idx]->name ? syms[idx]->name : "<Local sym>", | |
6365 | sec->name); | |
811072d8 | 6366 | bfd_set_error (bfd_error_invalid_operation); |
5ed6aba4 | 6367 | _bfd_stringtab_free (stt); |
589e6347 NC |
6368 | return FALSE; |
6369 | } | |
811072d8 | 6370 | |
079e9a2f | 6371 | shndx = _bfd_elf_section_from_bfd_section (abfd, sec2); |
cb33740c | 6372 | BFD_ASSERT (shndx != SHN_BAD); |
079e9a2f AM |
6373 | } |
6374 | } | |
252b5132 | 6375 | |
079e9a2f AM |
6376 | sym.st_shndx = shndx; |
6377 | } | |
252b5132 | 6378 | |
13ae64f3 JJ |
6379 | if ((flags & BSF_THREAD_LOCAL) != 0) |
6380 | type = STT_TLS; | |
d8045f23 NC |
6381 | else if ((flags & BSF_GNU_INDIRECT_FUNCTION) != 0) |
6382 | type = STT_GNU_IFUNC; | |
13ae64f3 | 6383 | else if ((flags & BSF_FUNCTION) != 0) |
079e9a2f AM |
6384 | type = STT_FUNC; |
6385 | else if ((flags & BSF_OBJECT) != 0) | |
6386 | type = STT_OBJECT; | |
d9352518 DB |
6387 | else if ((flags & BSF_RELC) != 0) |
6388 | type = STT_RELC; | |
6389 | else if ((flags & BSF_SRELC) != 0) | |
6390 | type = STT_SRELC; | |
079e9a2f AM |
6391 | else |
6392 | type = STT_NOTYPE; | |
252b5132 | 6393 | |
13ae64f3 JJ |
6394 | if (syms[idx]->section->flags & SEC_THREAD_LOCAL) |
6395 | type = STT_TLS; | |
6396 | ||
589e6347 | 6397 | /* Processor-specific types. */ |
079e9a2f AM |
6398 | if (type_ptr != NULL |
6399 | && bed->elf_backend_get_symbol_type) | |
6400 | type = ((*bed->elf_backend_get_symbol_type) | |
6401 | (&type_ptr->internal_elf_sym, type)); | |
252b5132 | 6402 | |
079e9a2f AM |
6403 | if (flags & BSF_SECTION_SYM) |
6404 | { | |
6405 | if (flags & BSF_GLOBAL) | |
6406 | sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION); | |
6407 | else | |
6408 | sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION); | |
6409 | } | |
6410 | else if (bfd_is_com_section (syms[idx]->section)) | |
0a40daed | 6411 | { |
504b7d20 | 6412 | #ifdef USE_STT_COMMON |
0a40daed MK |
6413 | if (type == STT_OBJECT) |
6414 | sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_COMMON); | |
6415 | else | |
504b7d20 | 6416 | #endif |
c91e322a | 6417 | sym.st_info = ELF_ST_INFO (STB_GLOBAL, type); |
0a40daed | 6418 | } |
079e9a2f AM |
6419 | else if (bfd_is_und_section (syms[idx]->section)) |
6420 | sym.st_info = ELF_ST_INFO (((flags & BSF_WEAK) | |
6421 | ? STB_WEAK | |
6422 | : STB_GLOBAL), | |
6423 | type); | |
6424 | else if (flags & BSF_FILE) | |
6425 | sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE); | |
6426 | else | |
6427 | { | |
6428 | int bind = STB_LOCAL; | |
252b5132 | 6429 | |
079e9a2f AM |
6430 | if (flags & BSF_LOCAL) |
6431 | bind = STB_LOCAL; | |
3e7a7d11 NC |
6432 | else if (flags & BSF_GNU_UNIQUE) |
6433 | bind = STB_GNU_UNIQUE; | |
079e9a2f AM |
6434 | else if (flags & BSF_WEAK) |
6435 | bind = STB_WEAK; | |
6436 | else if (flags & BSF_GLOBAL) | |
6437 | bind = STB_GLOBAL; | |
252b5132 | 6438 | |
079e9a2f AM |
6439 | sym.st_info = ELF_ST_INFO (bind, type); |
6440 | } | |
252b5132 | 6441 | |
079e9a2f AM |
6442 | if (type_ptr != NULL) |
6443 | sym.st_other = type_ptr->internal_elf_sym.st_other; | |
6444 | else | |
6445 | sym.st_other = 0; | |
252b5132 | 6446 | |
9ad5cbcf | 6447 | bed->s->swap_symbol_out (abfd, &sym, outbound_syms, outbound_shndx); |
079e9a2f | 6448 | outbound_syms += bed->s->sizeof_sym; |
9ad5cbcf AM |
6449 | if (outbound_shndx != NULL) |
6450 | outbound_shndx += sizeof (Elf_External_Sym_Shndx); | |
079e9a2f | 6451 | } |
252b5132 | 6452 | |
079e9a2f AM |
6453 | *sttp = stt; |
6454 | symstrtab_hdr->sh_size = _bfd_stringtab_size (stt); | |
6455 | symstrtab_hdr->sh_type = SHT_STRTAB; | |
252b5132 | 6456 | |
079e9a2f AM |
6457 | symstrtab_hdr->sh_flags = 0; |
6458 | symstrtab_hdr->sh_addr = 0; | |
6459 | symstrtab_hdr->sh_entsize = 0; | |
6460 | symstrtab_hdr->sh_link = 0; | |
6461 | symstrtab_hdr->sh_info = 0; | |
6462 | symstrtab_hdr->sh_addralign = 1; | |
252b5132 | 6463 | |
b34976b6 | 6464 | return TRUE; |
252b5132 RH |
6465 | } |
6466 | ||
6467 | /* Return the number of bytes required to hold the symtab vector. | |
6468 | ||
6469 | Note that we base it on the count plus 1, since we will null terminate | |
6470 | the vector allocated based on this size. However, the ELF symbol table | |
6471 | always has a dummy entry as symbol #0, so it ends up even. */ | |
6472 | ||
6473 | long | |
217aa764 | 6474 | _bfd_elf_get_symtab_upper_bound (bfd *abfd) |
252b5132 RH |
6475 | { |
6476 | long symcount; | |
6477 | long symtab_size; | |
6478 | Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr; | |
6479 | ||
6480 | symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym; | |
b99d1833 AM |
6481 | symtab_size = (symcount + 1) * (sizeof (asymbol *)); |
6482 | if (symcount > 0) | |
6483 | symtab_size -= sizeof (asymbol *); | |
252b5132 RH |
6484 | |
6485 | return symtab_size; | |
6486 | } | |
6487 | ||
6488 | long | |
217aa764 | 6489 | _bfd_elf_get_dynamic_symtab_upper_bound (bfd *abfd) |
252b5132 RH |
6490 | { |
6491 | long symcount; | |
6492 | long symtab_size; | |
6493 | Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr; | |
6494 | ||
6495 | if (elf_dynsymtab (abfd) == 0) | |
6496 | { | |
6497 | bfd_set_error (bfd_error_invalid_operation); | |
6498 | return -1; | |
6499 | } | |
6500 | ||
6501 | symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym; | |
b99d1833 AM |
6502 | symtab_size = (symcount + 1) * (sizeof (asymbol *)); |
6503 | if (symcount > 0) | |
6504 | symtab_size -= sizeof (asymbol *); | |
252b5132 RH |
6505 | |
6506 | return symtab_size; | |
6507 | } | |
6508 | ||
6509 | long | |
217aa764 AM |
6510 | _bfd_elf_get_reloc_upper_bound (bfd *abfd ATTRIBUTE_UNUSED, |
6511 | sec_ptr asect) | |
252b5132 RH |
6512 | { |
6513 | return (asect->reloc_count + 1) * sizeof (arelent *); | |
6514 | } | |
6515 | ||
6516 | /* Canonicalize the relocs. */ | |
6517 | ||
6518 | long | |
217aa764 AM |
6519 | _bfd_elf_canonicalize_reloc (bfd *abfd, |
6520 | sec_ptr section, | |
6521 | arelent **relptr, | |
6522 | asymbol **symbols) | |
252b5132 RH |
6523 | { |
6524 | arelent *tblptr; | |
6525 | unsigned int i; | |
9c5bfbb7 | 6526 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
252b5132 | 6527 | |
b34976b6 | 6528 | if (! bed->s->slurp_reloc_table (abfd, section, symbols, FALSE)) |
252b5132 RH |
6529 | return -1; |
6530 | ||
6531 | tblptr = section->relocation; | |
6532 | for (i = 0; i < section->reloc_count; i++) | |
6533 | *relptr++ = tblptr++; | |
6534 | ||
6535 | *relptr = NULL; | |
6536 | ||
6537 | return section->reloc_count; | |
6538 | } | |
6539 | ||
6540 | long | |
6cee3f79 | 6541 | _bfd_elf_canonicalize_symtab (bfd *abfd, asymbol **allocation) |
252b5132 | 6542 | { |
9c5bfbb7 | 6543 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
217aa764 | 6544 | long symcount = bed->s->slurp_symbol_table (abfd, allocation, FALSE); |
252b5132 RH |
6545 | |
6546 | if (symcount >= 0) | |
6547 | bfd_get_symcount (abfd) = symcount; | |
6548 | return symcount; | |
6549 | } | |
6550 | ||
6551 | long | |
217aa764 AM |
6552 | _bfd_elf_canonicalize_dynamic_symtab (bfd *abfd, |
6553 | asymbol **allocation) | |
252b5132 | 6554 | { |
9c5bfbb7 | 6555 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
217aa764 | 6556 | long symcount = bed->s->slurp_symbol_table (abfd, allocation, TRUE); |
1f70368c DJ |
6557 | |
6558 | if (symcount >= 0) | |
6559 | bfd_get_dynamic_symcount (abfd) = symcount; | |
6560 | return symcount; | |
252b5132 RH |
6561 | } |
6562 | ||
8615f3f2 AM |
6563 | /* Return the size required for the dynamic reloc entries. Any loadable |
6564 | section that was actually installed in the BFD, and has type SHT_REL | |
6565 | or SHT_RELA, and uses the dynamic symbol table, is considered to be a | |
6566 | dynamic reloc section. */ | |
252b5132 RH |
6567 | |
6568 | long | |
217aa764 | 6569 | _bfd_elf_get_dynamic_reloc_upper_bound (bfd *abfd) |
252b5132 RH |
6570 | { |
6571 | long ret; | |
6572 | asection *s; | |
6573 | ||
6574 | if (elf_dynsymtab (abfd) == 0) | |
6575 | { | |
6576 | bfd_set_error (bfd_error_invalid_operation); | |
6577 | return -1; | |
6578 | } | |
6579 | ||
6580 | ret = sizeof (arelent *); | |
6581 | for (s = abfd->sections; s != NULL; s = s->next) | |
266b05cf | 6582 | if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd) |
252b5132 RH |
6583 | && (elf_section_data (s)->this_hdr.sh_type == SHT_REL |
6584 | || elf_section_data (s)->this_hdr.sh_type == SHT_RELA)) | |
eea6121a | 6585 | ret += ((s->size / elf_section_data (s)->this_hdr.sh_entsize) |
252b5132 RH |
6586 | * sizeof (arelent *)); |
6587 | ||
6588 | return ret; | |
6589 | } | |
6590 | ||
8615f3f2 AM |
6591 | /* Canonicalize the dynamic relocation entries. Note that we return the |
6592 | dynamic relocations as a single block, although they are actually | |
6593 | associated with particular sections; the interface, which was | |
6594 | designed for SunOS style shared libraries, expects that there is only | |
6595 | one set of dynamic relocs. Any loadable section that was actually | |
6596 | installed in the BFD, and has type SHT_REL or SHT_RELA, and uses the | |
6597 | dynamic symbol table, is considered to be a dynamic reloc section. */ | |
252b5132 RH |
6598 | |
6599 | long | |
217aa764 AM |
6600 | _bfd_elf_canonicalize_dynamic_reloc (bfd *abfd, |
6601 | arelent **storage, | |
6602 | asymbol **syms) | |
252b5132 | 6603 | { |
217aa764 | 6604 | bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean); |
252b5132 RH |
6605 | asection *s; |
6606 | long ret; | |
6607 | ||
6608 | if (elf_dynsymtab (abfd) == 0) | |
6609 | { | |
6610 | bfd_set_error (bfd_error_invalid_operation); | |
6611 | return -1; | |
6612 | } | |
6613 | ||
6614 | slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table; | |
6615 | ret = 0; | |
6616 | for (s = abfd->sections; s != NULL; s = s->next) | |
6617 | { | |
266b05cf | 6618 | if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd) |
252b5132 RH |
6619 | && (elf_section_data (s)->this_hdr.sh_type == SHT_REL |
6620 | || elf_section_data (s)->this_hdr.sh_type == SHT_RELA)) | |
6621 | { | |
6622 | arelent *p; | |
6623 | long count, i; | |
6624 | ||
b34976b6 | 6625 | if (! (*slurp_relocs) (abfd, s, syms, TRUE)) |
252b5132 | 6626 | return -1; |
eea6121a | 6627 | count = s->size / elf_section_data (s)->this_hdr.sh_entsize; |
252b5132 RH |
6628 | p = s->relocation; |
6629 | for (i = 0; i < count; i++) | |
6630 | *storage++ = p++; | |
6631 | ret += count; | |
6632 | } | |
6633 | } | |
6634 | ||
6635 | *storage = NULL; | |
6636 | ||
6637 | return ret; | |
6638 | } | |
6639 | \f | |
6640 | /* Read in the version information. */ | |
6641 | ||
b34976b6 | 6642 | bfd_boolean |
fc0e6df6 | 6643 | _bfd_elf_slurp_version_tables (bfd *abfd, bfd_boolean default_imported_symver) |
252b5132 RH |
6644 | { |
6645 | bfd_byte *contents = NULL; | |
fc0e6df6 PB |
6646 | unsigned int freeidx = 0; |
6647 | ||
6648 | if (elf_dynverref (abfd) != 0) | |
6649 | { | |
6650 | Elf_Internal_Shdr *hdr; | |
6651 | Elf_External_Verneed *everneed; | |
6652 | Elf_Internal_Verneed *iverneed; | |
6653 | unsigned int i; | |
d0fb9a8d | 6654 | bfd_byte *contents_end; |
fc0e6df6 PB |
6655 | |
6656 | hdr = &elf_tdata (abfd)->dynverref_hdr; | |
6657 | ||
d0fb9a8d JJ |
6658 | elf_tdata (abfd)->verref = bfd_zalloc2 (abfd, hdr->sh_info, |
6659 | sizeof (Elf_Internal_Verneed)); | |
fc0e6df6 PB |
6660 | if (elf_tdata (abfd)->verref == NULL) |
6661 | goto error_return; | |
6662 | ||
6663 | elf_tdata (abfd)->cverrefs = hdr->sh_info; | |
6664 | ||
6665 | contents = bfd_malloc (hdr->sh_size); | |
6666 | if (contents == NULL) | |
d0fb9a8d JJ |
6667 | { |
6668 | error_return_verref: | |
6669 | elf_tdata (abfd)->verref = NULL; | |
6670 | elf_tdata (abfd)->cverrefs = 0; | |
6671 | goto error_return; | |
6672 | } | |
fc0e6df6 PB |
6673 | if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0 |
6674 | || bfd_bread (contents, hdr->sh_size, abfd) != hdr->sh_size) | |
d0fb9a8d | 6675 | goto error_return_verref; |
fc0e6df6 | 6676 | |
d0fb9a8d JJ |
6677 | if (hdr->sh_info && hdr->sh_size < sizeof (Elf_External_Verneed)) |
6678 | goto error_return_verref; | |
6679 | ||
6680 | BFD_ASSERT (sizeof (Elf_External_Verneed) | |
6681 | == sizeof (Elf_External_Vernaux)); | |
6682 | contents_end = contents + hdr->sh_size - sizeof (Elf_External_Verneed); | |
fc0e6df6 PB |
6683 | everneed = (Elf_External_Verneed *) contents; |
6684 | iverneed = elf_tdata (abfd)->verref; | |
6685 | for (i = 0; i < hdr->sh_info; i++, iverneed++) | |
6686 | { | |
6687 | Elf_External_Vernaux *evernaux; | |
6688 | Elf_Internal_Vernaux *ivernaux; | |
6689 | unsigned int j; | |
6690 | ||
6691 | _bfd_elf_swap_verneed_in (abfd, everneed, iverneed); | |
6692 | ||
6693 | iverneed->vn_bfd = abfd; | |
6694 | ||
6695 | iverneed->vn_filename = | |
6696 | bfd_elf_string_from_elf_section (abfd, hdr->sh_link, | |
6697 | iverneed->vn_file); | |
6698 | if (iverneed->vn_filename == NULL) | |
d0fb9a8d | 6699 | goto error_return_verref; |
fc0e6df6 | 6700 | |
d0fb9a8d JJ |
6701 | if (iverneed->vn_cnt == 0) |
6702 | iverneed->vn_auxptr = NULL; | |
6703 | else | |
6704 | { | |
6705 | iverneed->vn_auxptr = bfd_alloc2 (abfd, iverneed->vn_cnt, | |
6706 | sizeof (Elf_Internal_Vernaux)); | |
6707 | if (iverneed->vn_auxptr == NULL) | |
6708 | goto error_return_verref; | |
6709 | } | |
6710 | ||
6711 | if (iverneed->vn_aux | |
6712 | > (size_t) (contents_end - (bfd_byte *) everneed)) | |
6713 | goto error_return_verref; | |
fc0e6df6 PB |
6714 | |
6715 | evernaux = ((Elf_External_Vernaux *) | |
6716 | ((bfd_byte *) everneed + iverneed->vn_aux)); | |
6717 | ivernaux = iverneed->vn_auxptr; | |
6718 | for (j = 0; j < iverneed->vn_cnt; j++, ivernaux++) | |
6719 | { | |
6720 | _bfd_elf_swap_vernaux_in (abfd, evernaux, ivernaux); | |
6721 | ||
6722 | ivernaux->vna_nodename = | |
6723 | bfd_elf_string_from_elf_section (abfd, hdr->sh_link, | |
6724 | ivernaux->vna_name); | |
6725 | if (ivernaux->vna_nodename == NULL) | |
d0fb9a8d | 6726 | goto error_return_verref; |
fc0e6df6 PB |
6727 | |
6728 | if (j + 1 < iverneed->vn_cnt) | |
6729 | ivernaux->vna_nextptr = ivernaux + 1; | |
6730 | else | |
6731 | ivernaux->vna_nextptr = NULL; | |
6732 | ||
d0fb9a8d JJ |
6733 | if (ivernaux->vna_next |
6734 | > (size_t) (contents_end - (bfd_byte *) evernaux)) | |
6735 | goto error_return_verref; | |
6736 | ||
fc0e6df6 PB |
6737 | evernaux = ((Elf_External_Vernaux *) |
6738 | ((bfd_byte *) evernaux + ivernaux->vna_next)); | |
6739 | ||
6740 | if (ivernaux->vna_other > freeidx) | |
6741 | freeidx = ivernaux->vna_other; | |
6742 | } | |
6743 | ||
6744 | if (i + 1 < hdr->sh_info) | |
6745 | iverneed->vn_nextref = iverneed + 1; | |
6746 | else | |
6747 | iverneed->vn_nextref = NULL; | |
6748 | ||
d0fb9a8d JJ |
6749 | if (iverneed->vn_next |
6750 | > (size_t) (contents_end - (bfd_byte *) everneed)) | |
6751 | goto error_return_verref; | |
6752 | ||
fc0e6df6 PB |
6753 | everneed = ((Elf_External_Verneed *) |
6754 | ((bfd_byte *) everneed + iverneed->vn_next)); | |
6755 | } | |
6756 | ||
6757 | free (contents); | |
6758 | contents = NULL; | |
6759 | } | |
252b5132 RH |
6760 | |
6761 | if (elf_dynverdef (abfd) != 0) | |
6762 | { | |
6763 | Elf_Internal_Shdr *hdr; | |
6764 | Elf_External_Verdef *everdef; | |
6765 | Elf_Internal_Verdef *iverdef; | |
f631889e UD |
6766 | Elf_Internal_Verdef *iverdefarr; |
6767 | Elf_Internal_Verdef iverdefmem; | |
252b5132 | 6768 | unsigned int i; |
062e2358 | 6769 | unsigned int maxidx; |
d0fb9a8d | 6770 | bfd_byte *contents_end_def, *contents_end_aux; |
252b5132 RH |
6771 | |
6772 | hdr = &elf_tdata (abfd)->dynverdef_hdr; | |
6773 | ||
217aa764 | 6774 | contents = bfd_malloc (hdr->sh_size); |
252b5132 RH |
6775 | if (contents == NULL) |
6776 | goto error_return; | |
6777 | if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0 | |
217aa764 | 6778 | || bfd_bread (contents, hdr->sh_size, abfd) != hdr->sh_size) |
252b5132 RH |
6779 | goto error_return; |
6780 | ||
d0fb9a8d JJ |
6781 | if (hdr->sh_info && hdr->sh_size < sizeof (Elf_External_Verdef)) |
6782 | goto error_return; | |
6783 | ||
6784 | BFD_ASSERT (sizeof (Elf_External_Verdef) | |
6785 | >= sizeof (Elf_External_Verdaux)); | |
6786 | contents_end_def = contents + hdr->sh_size | |
6787 | - sizeof (Elf_External_Verdef); | |
6788 | contents_end_aux = contents + hdr->sh_size | |
6789 | - sizeof (Elf_External_Verdaux); | |
6790 | ||
f631889e UD |
6791 | /* We know the number of entries in the section but not the maximum |
6792 | index. Therefore we have to run through all entries and find | |
6793 | the maximum. */ | |
252b5132 | 6794 | everdef = (Elf_External_Verdef *) contents; |
f631889e UD |
6795 | maxidx = 0; |
6796 | for (i = 0; i < hdr->sh_info; ++i) | |
6797 | { | |
6798 | _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem); | |
6799 | ||
062e2358 AM |
6800 | if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) > maxidx) |
6801 | maxidx = iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION); | |
f631889e | 6802 | |
d0fb9a8d JJ |
6803 | if (iverdefmem.vd_next |
6804 | > (size_t) (contents_end_def - (bfd_byte *) everdef)) | |
6805 | goto error_return; | |
6806 | ||
f631889e UD |
6807 | everdef = ((Elf_External_Verdef *) |
6808 | ((bfd_byte *) everdef + iverdefmem.vd_next)); | |
6809 | } | |
6810 | ||
fc0e6df6 PB |
6811 | if (default_imported_symver) |
6812 | { | |
6813 | if (freeidx > maxidx) | |
6814 | maxidx = ++freeidx; | |
6815 | else | |
6816 | freeidx = ++maxidx; | |
6817 | } | |
d0fb9a8d JJ |
6818 | elf_tdata (abfd)->verdef = bfd_zalloc2 (abfd, maxidx, |
6819 | sizeof (Elf_Internal_Verdef)); | |
f631889e UD |
6820 | if (elf_tdata (abfd)->verdef == NULL) |
6821 | goto error_return; | |
6822 | ||
6823 | elf_tdata (abfd)->cverdefs = maxidx; | |
6824 | ||
6825 | everdef = (Elf_External_Verdef *) contents; | |
6826 | iverdefarr = elf_tdata (abfd)->verdef; | |
6827 | for (i = 0; i < hdr->sh_info; i++) | |
252b5132 RH |
6828 | { |
6829 | Elf_External_Verdaux *everdaux; | |
6830 | Elf_Internal_Verdaux *iverdaux; | |
6831 | unsigned int j; | |
6832 | ||
f631889e UD |
6833 | _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem); |
6834 | ||
d0fb9a8d JJ |
6835 | if ((iverdefmem.vd_ndx & VERSYM_VERSION) == 0) |
6836 | { | |
6837 | error_return_verdef: | |
6838 | elf_tdata (abfd)->verdef = NULL; | |
6839 | elf_tdata (abfd)->cverdefs = 0; | |
6840 | goto error_return; | |
6841 | } | |
6842 | ||
f631889e UD |
6843 | iverdef = &iverdefarr[(iverdefmem.vd_ndx & VERSYM_VERSION) - 1]; |
6844 | memcpy (iverdef, &iverdefmem, sizeof (Elf_Internal_Verdef)); | |
252b5132 RH |
6845 | |
6846 | iverdef->vd_bfd = abfd; | |
6847 | ||
d0fb9a8d JJ |
6848 | if (iverdef->vd_cnt == 0) |
6849 | iverdef->vd_auxptr = NULL; | |
6850 | else | |
6851 | { | |
6852 | iverdef->vd_auxptr = bfd_alloc2 (abfd, iverdef->vd_cnt, | |
6853 | sizeof (Elf_Internal_Verdaux)); | |
6854 | if (iverdef->vd_auxptr == NULL) | |
6855 | goto error_return_verdef; | |
6856 | } | |
6857 | ||
6858 | if (iverdef->vd_aux | |
6859 | > (size_t) (contents_end_aux - (bfd_byte *) everdef)) | |
6860 | goto error_return_verdef; | |
252b5132 RH |
6861 | |
6862 | everdaux = ((Elf_External_Verdaux *) | |
6863 | ((bfd_byte *) everdef + iverdef->vd_aux)); | |
6864 | iverdaux = iverdef->vd_auxptr; | |
6865 | for (j = 0; j < iverdef->vd_cnt; j++, iverdaux++) | |
6866 | { | |
6867 | _bfd_elf_swap_verdaux_in (abfd, everdaux, iverdaux); | |
6868 | ||
6869 | iverdaux->vda_nodename = | |
6870 | bfd_elf_string_from_elf_section (abfd, hdr->sh_link, | |
6871 | iverdaux->vda_name); | |
6872 | if (iverdaux->vda_nodename == NULL) | |
d0fb9a8d | 6873 | goto error_return_verdef; |
252b5132 RH |
6874 | |
6875 | if (j + 1 < iverdef->vd_cnt) | |
6876 | iverdaux->vda_nextptr = iverdaux + 1; | |
6877 | else | |
6878 | iverdaux->vda_nextptr = NULL; | |
6879 | ||
d0fb9a8d JJ |
6880 | if (iverdaux->vda_next |
6881 | > (size_t) (contents_end_aux - (bfd_byte *) everdaux)) | |
6882 | goto error_return_verdef; | |
6883 | ||
252b5132 RH |
6884 | everdaux = ((Elf_External_Verdaux *) |
6885 | ((bfd_byte *) everdaux + iverdaux->vda_next)); | |
6886 | } | |
6887 | ||
d0fb9a8d JJ |
6888 | if (iverdef->vd_cnt) |
6889 | iverdef->vd_nodename = iverdef->vd_auxptr->vda_nodename; | |
252b5132 | 6890 | |
d0fb9a8d | 6891 | if ((size_t) (iverdef - iverdefarr) + 1 < maxidx) |
252b5132 RH |
6892 | iverdef->vd_nextdef = iverdef + 1; |
6893 | else | |
6894 | iverdef->vd_nextdef = NULL; | |
6895 | ||
6896 | everdef = ((Elf_External_Verdef *) | |
6897 | ((bfd_byte *) everdef + iverdef->vd_next)); | |
6898 | } | |
6899 | ||
6900 | free (contents); | |
6901 | contents = NULL; | |
6902 | } | |
fc0e6df6 | 6903 | else if (default_imported_symver) |
252b5132 | 6904 | { |
fc0e6df6 PB |
6905 | if (freeidx < 3) |
6906 | freeidx = 3; | |
6907 | else | |
6908 | freeidx++; | |
252b5132 | 6909 | |
d0fb9a8d JJ |
6910 | elf_tdata (abfd)->verdef = bfd_zalloc2 (abfd, freeidx, |
6911 | sizeof (Elf_Internal_Verdef)); | |
fc0e6df6 | 6912 | if (elf_tdata (abfd)->verdef == NULL) |
252b5132 RH |
6913 | goto error_return; |
6914 | ||
fc0e6df6 PB |
6915 | elf_tdata (abfd)->cverdefs = freeidx; |
6916 | } | |
252b5132 | 6917 | |
fc0e6df6 PB |
6918 | /* Create a default version based on the soname. */ |
6919 | if (default_imported_symver) | |
6920 | { | |
6921 | Elf_Internal_Verdef *iverdef; | |
6922 | Elf_Internal_Verdaux *iverdaux; | |
252b5132 | 6923 | |
fc0e6df6 | 6924 | iverdef = &elf_tdata (abfd)->verdef[freeidx - 1];; |
252b5132 | 6925 | |
fc0e6df6 PB |
6926 | iverdef->vd_version = VER_DEF_CURRENT; |
6927 | iverdef->vd_flags = 0; | |
6928 | iverdef->vd_ndx = freeidx; | |
6929 | iverdef->vd_cnt = 1; | |
252b5132 | 6930 | |
fc0e6df6 | 6931 | iverdef->vd_bfd = abfd; |
252b5132 | 6932 | |
fc0e6df6 PB |
6933 | iverdef->vd_nodename = bfd_elf_get_dt_soname (abfd); |
6934 | if (iverdef->vd_nodename == NULL) | |
d0fb9a8d | 6935 | goto error_return_verdef; |
fc0e6df6 | 6936 | iverdef->vd_nextdef = NULL; |
d0fb9a8d JJ |
6937 | iverdef->vd_auxptr = bfd_alloc (abfd, sizeof (Elf_Internal_Verdaux)); |
6938 | if (iverdef->vd_auxptr == NULL) | |
6939 | goto error_return_verdef; | |
252b5132 | 6940 | |
fc0e6df6 PB |
6941 | iverdaux = iverdef->vd_auxptr; |
6942 | iverdaux->vda_nodename = iverdef->vd_nodename; | |
6943 | iverdaux->vda_nextptr = NULL; | |
252b5132 RH |
6944 | } |
6945 | ||
b34976b6 | 6946 | return TRUE; |
252b5132 RH |
6947 | |
6948 | error_return: | |
5ed6aba4 | 6949 | if (contents != NULL) |
252b5132 | 6950 | free (contents); |
b34976b6 | 6951 | return FALSE; |
252b5132 RH |
6952 | } |
6953 | \f | |
6954 | asymbol * | |
217aa764 | 6955 | _bfd_elf_make_empty_symbol (bfd *abfd) |
252b5132 RH |
6956 | { |
6957 | elf_symbol_type *newsym; | |
dc810e39 | 6958 | bfd_size_type amt = sizeof (elf_symbol_type); |
252b5132 | 6959 | |
217aa764 | 6960 | newsym = bfd_zalloc (abfd, amt); |
252b5132 RH |
6961 | if (!newsym) |
6962 | return NULL; | |
6963 | else | |
6964 | { | |
6965 | newsym->symbol.the_bfd = abfd; | |
6966 | return &newsym->symbol; | |
6967 | } | |
6968 | } | |
6969 | ||
6970 | void | |
217aa764 AM |
6971 | _bfd_elf_get_symbol_info (bfd *abfd ATTRIBUTE_UNUSED, |
6972 | asymbol *symbol, | |
6973 | symbol_info *ret) | |
252b5132 RH |
6974 | { |
6975 | bfd_symbol_info (symbol, ret); | |
6976 | } | |
6977 | ||
6978 | /* Return whether a symbol name implies a local symbol. Most targets | |
6979 | use this function for the is_local_label_name entry point, but some | |
6980 | override it. */ | |
6981 | ||
b34976b6 | 6982 | bfd_boolean |
217aa764 AM |
6983 | _bfd_elf_is_local_label_name (bfd *abfd ATTRIBUTE_UNUSED, |
6984 | const char *name) | |
252b5132 RH |
6985 | { |
6986 | /* Normal local symbols start with ``.L''. */ | |
6987 | if (name[0] == '.' && name[1] == 'L') | |
b34976b6 | 6988 | return TRUE; |
252b5132 RH |
6989 | |
6990 | /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate | |
6991 | DWARF debugging symbols starting with ``..''. */ | |
6992 | if (name[0] == '.' && name[1] == '.') | |
b34976b6 | 6993 | return TRUE; |
252b5132 RH |
6994 | |
6995 | /* gcc will sometimes generate symbols beginning with ``_.L_'' when | |
6996 | emitting DWARF debugging output. I suspect this is actually a | |
6997 | small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call | |
6998 | ASM_GENERATE_INTERNAL_LABEL, and this causes the leading | |
6999 | underscore to be emitted on some ELF targets). For ease of use, | |
7000 | we treat such symbols as local. */ | |
7001 | if (name[0] == '_' && name[1] == '.' && name[2] == 'L' && name[3] == '_') | |
b34976b6 | 7002 | return TRUE; |
252b5132 | 7003 | |
b34976b6 | 7004 | return FALSE; |
252b5132 RH |
7005 | } |
7006 | ||
7007 | alent * | |
217aa764 AM |
7008 | _bfd_elf_get_lineno (bfd *abfd ATTRIBUTE_UNUSED, |
7009 | asymbol *symbol ATTRIBUTE_UNUSED) | |
252b5132 RH |
7010 | { |
7011 | abort (); | |
7012 | return NULL; | |
7013 | } | |
7014 | ||
b34976b6 | 7015 | bfd_boolean |
217aa764 AM |
7016 | _bfd_elf_set_arch_mach (bfd *abfd, |
7017 | enum bfd_architecture arch, | |
7018 | unsigned long machine) | |
252b5132 RH |
7019 | { |
7020 | /* If this isn't the right architecture for this backend, and this | |
7021 | isn't the generic backend, fail. */ | |
7022 | if (arch != get_elf_backend_data (abfd)->arch | |
7023 | && arch != bfd_arch_unknown | |
7024 | && get_elf_backend_data (abfd)->arch != bfd_arch_unknown) | |
b34976b6 | 7025 | return FALSE; |
252b5132 RH |
7026 | |
7027 | return bfd_default_set_arch_mach (abfd, arch, machine); | |
7028 | } | |
7029 | ||
d1fad7c6 NC |
7030 | /* Find the function to a particular section and offset, |
7031 | for error reporting. */ | |
252b5132 | 7032 | |
b34976b6 | 7033 | static bfd_boolean |
b9d678e0 | 7034 | elf_find_function (bfd *abfd, |
217aa764 AM |
7035 | asection *section, |
7036 | asymbol **symbols, | |
7037 | bfd_vma offset, | |
7038 | const char **filename_ptr, | |
7039 | const char **functionname_ptr) | |
252b5132 | 7040 | { |
252b5132 | 7041 | const char *filename; |
57426232 | 7042 | asymbol *func, *file; |
252b5132 RH |
7043 | bfd_vma low_func; |
7044 | asymbol **p; | |
57426232 JB |
7045 | /* ??? Given multiple file symbols, it is impossible to reliably |
7046 | choose the right file name for global symbols. File symbols are | |
7047 | local symbols, and thus all file symbols must sort before any | |
7048 | global symbols. The ELF spec may be interpreted to say that a | |
7049 | file symbol must sort before other local symbols, but currently | |
7050 | ld -r doesn't do this. So, for ld -r output, it is possible to | |
7051 | make a better choice of file name for local symbols by ignoring | |
7052 | file symbols appearing after a given local symbol. */ | |
7053 | enum { nothing_seen, symbol_seen, file_after_symbol_seen } state; | |
b9d678e0 | 7054 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
252b5132 | 7055 | |
252b5132 RH |
7056 | filename = NULL; |
7057 | func = NULL; | |
57426232 | 7058 | file = NULL; |
252b5132 | 7059 | low_func = 0; |
57426232 | 7060 | state = nothing_seen; |
252b5132 RH |
7061 | |
7062 | for (p = symbols; *p != NULL; p++) | |
7063 | { | |
7064 | elf_symbol_type *q; | |
b9d678e0 | 7065 | unsigned int type; |
252b5132 RH |
7066 | |
7067 | q = (elf_symbol_type *) *p; | |
7068 | ||
b9d678e0 L |
7069 | type = ELF_ST_TYPE (q->internal_elf_sym.st_info); |
7070 | switch (type) | |
252b5132 | 7071 | { |
252b5132 | 7072 | case STT_FILE: |
57426232 JB |
7073 | file = &q->symbol; |
7074 | if (state == symbol_seen) | |
7075 | state = file_after_symbol_seen; | |
7076 | continue; | |
b9d678e0 L |
7077 | default: |
7078 | if (!bed->is_function_type (type)) | |
7079 | break; | |
252b5132 | 7080 | case STT_NOTYPE: |
6b40fcba | 7081 | if (bfd_get_section (&q->symbol) == section |
252b5132 RH |
7082 | && q->symbol.value >= low_func |
7083 | && q->symbol.value <= offset) | |
7084 | { | |
7085 | func = (asymbol *) q; | |
7086 | low_func = q->symbol.value; | |
a1923858 AM |
7087 | filename = NULL; |
7088 | if (file != NULL | |
7089 | && (ELF_ST_BIND (q->internal_elf_sym.st_info) == STB_LOCAL | |
7090 | || state != file_after_symbol_seen)) | |
57426232 | 7091 | filename = bfd_asymbol_name (file); |
252b5132 RH |
7092 | } |
7093 | break; | |
7094 | } | |
57426232 JB |
7095 | if (state == nothing_seen) |
7096 | state = symbol_seen; | |
252b5132 RH |
7097 | } |
7098 | ||
7099 | if (func == NULL) | |
b34976b6 | 7100 | return FALSE; |
252b5132 | 7101 | |
d1fad7c6 NC |
7102 | if (filename_ptr) |
7103 | *filename_ptr = filename; | |
7104 | if (functionname_ptr) | |
7105 | *functionname_ptr = bfd_asymbol_name (func); | |
7106 | ||
b34976b6 | 7107 | return TRUE; |
d1fad7c6 NC |
7108 | } |
7109 | ||
7110 | /* Find the nearest line to a particular section and offset, | |
7111 | for error reporting. */ | |
7112 | ||
b34976b6 | 7113 | bfd_boolean |
217aa764 AM |
7114 | _bfd_elf_find_nearest_line (bfd *abfd, |
7115 | asection *section, | |
7116 | asymbol **symbols, | |
7117 | bfd_vma offset, | |
7118 | const char **filename_ptr, | |
7119 | const char **functionname_ptr, | |
7120 | unsigned int *line_ptr) | |
d1fad7c6 | 7121 | { |
b34976b6 | 7122 | bfd_boolean found; |
d1fad7c6 NC |
7123 | |
7124 | if (_bfd_dwarf1_find_nearest_line (abfd, section, symbols, offset, | |
4e8a9624 AM |
7125 | filename_ptr, functionname_ptr, |
7126 | line_ptr)) | |
d1fad7c6 NC |
7127 | { |
7128 | if (!*functionname_ptr) | |
4e8a9624 AM |
7129 | elf_find_function (abfd, section, symbols, offset, |
7130 | *filename_ptr ? NULL : filename_ptr, | |
7131 | functionname_ptr); | |
7132 | ||
b34976b6 | 7133 | return TRUE; |
d1fad7c6 NC |
7134 | } |
7135 | ||
7136 | if (_bfd_dwarf2_find_nearest_line (abfd, section, symbols, offset, | |
4e8a9624 AM |
7137 | filename_ptr, functionname_ptr, |
7138 | line_ptr, 0, | |
7139 | &elf_tdata (abfd)->dwarf2_find_line_info)) | |
d1fad7c6 NC |
7140 | { |
7141 | if (!*functionname_ptr) | |
4e8a9624 AM |
7142 | elf_find_function (abfd, section, symbols, offset, |
7143 | *filename_ptr ? NULL : filename_ptr, | |
7144 | functionname_ptr); | |
7145 | ||
b34976b6 | 7146 | return TRUE; |
d1fad7c6 NC |
7147 | } |
7148 | ||
7149 | if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset, | |
4e8a9624 AM |
7150 | &found, filename_ptr, |
7151 | functionname_ptr, line_ptr, | |
7152 | &elf_tdata (abfd)->line_info)) | |
b34976b6 | 7153 | return FALSE; |
dc43ada5 | 7154 | if (found && (*functionname_ptr || *line_ptr)) |
b34976b6 | 7155 | return TRUE; |
d1fad7c6 NC |
7156 | |
7157 | if (symbols == NULL) | |
b34976b6 | 7158 | return FALSE; |
d1fad7c6 NC |
7159 | |
7160 | if (! elf_find_function (abfd, section, symbols, offset, | |
4e8a9624 | 7161 | filename_ptr, functionname_ptr)) |
b34976b6 | 7162 | return FALSE; |
d1fad7c6 | 7163 | |
252b5132 | 7164 | *line_ptr = 0; |
b34976b6 | 7165 | return TRUE; |
252b5132 RH |
7166 | } |
7167 | ||
5420f73d L |
7168 | /* Find the line for a symbol. */ |
7169 | ||
7170 | bfd_boolean | |
7171 | _bfd_elf_find_line (bfd *abfd, asymbol **symbols, asymbol *symbol, | |
7172 | const char **filename_ptr, unsigned int *line_ptr) | |
7173 | { | |
7174 | return _bfd_dwarf2_find_line (abfd, symbols, symbol, | |
7175 | filename_ptr, line_ptr, 0, | |
7176 | &elf_tdata (abfd)->dwarf2_find_line_info); | |
7177 | } | |
7178 | ||
4ab527b0 FF |
7179 | /* After a call to bfd_find_nearest_line, successive calls to |
7180 | bfd_find_inliner_info can be used to get source information about | |
7181 | each level of function inlining that terminated at the address | |
7182 | passed to bfd_find_nearest_line. Currently this is only supported | |
7183 | for DWARF2 with appropriate DWARF3 extensions. */ | |
7184 | ||
7185 | bfd_boolean | |
7186 | _bfd_elf_find_inliner_info (bfd *abfd, | |
7187 | const char **filename_ptr, | |
7188 | const char **functionname_ptr, | |
7189 | unsigned int *line_ptr) | |
7190 | { | |
7191 | bfd_boolean found; | |
7192 | found = _bfd_dwarf2_find_inliner_info (abfd, filename_ptr, | |
7193 | functionname_ptr, line_ptr, | |
7194 | & elf_tdata (abfd)->dwarf2_find_line_info); | |
7195 | return found; | |
7196 | } | |
7197 | ||
252b5132 | 7198 | int |
a6b96beb | 7199 | _bfd_elf_sizeof_headers (bfd *abfd, struct bfd_link_info *info) |
252b5132 | 7200 | { |
8ded5a0f AM |
7201 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
7202 | int ret = bed->s->sizeof_ehdr; | |
252b5132 | 7203 | |
a6b96beb | 7204 | if (!info->relocatable) |
8ded5a0f | 7205 | { |
62d7a5f6 | 7206 | bfd_size_type phdr_size = elf_tdata (abfd)->program_header_size; |
8ded5a0f | 7207 | |
62d7a5f6 AM |
7208 | if (phdr_size == (bfd_size_type) -1) |
7209 | { | |
7210 | struct elf_segment_map *m; | |
7211 | ||
7212 | phdr_size = 0; | |
7213 | for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next) | |
7214 | phdr_size += bed->s->sizeof_phdr; | |
8ded5a0f | 7215 | |
62d7a5f6 AM |
7216 | if (phdr_size == 0) |
7217 | phdr_size = get_program_header_size (abfd, info); | |
7218 | } | |
8ded5a0f AM |
7219 | |
7220 | elf_tdata (abfd)->program_header_size = phdr_size; | |
7221 | ret += phdr_size; | |
7222 | } | |
7223 | ||
252b5132 RH |
7224 | return ret; |
7225 | } | |
7226 | ||
b34976b6 | 7227 | bfd_boolean |
217aa764 AM |
7228 | _bfd_elf_set_section_contents (bfd *abfd, |
7229 | sec_ptr section, | |
0f867abe | 7230 | const void *location, |
217aa764 AM |
7231 | file_ptr offset, |
7232 | bfd_size_type count) | |
252b5132 RH |
7233 | { |
7234 | Elf_Internal_Shdr *hdr; | |
dc810e39 | 7235 | bfd_signed_vma pos; |
252b5132 RH |
7236 | |
7237 | if (! abfd->output_has_begun | |
217aa764 | 7238 | && ! _bfd_elf_compute_section_file_positions (abfd, NULL)) |
b34976b6 | 7239 | return FALSE; |
252b5132 RH |
7240 | |
7241 | hdr = &elf_section_data (section)->this_hdr; | |
dc810e39 AM |
7242 | pos = hdr->sh_offset + offset; |
7243 | if (bfd_seek (abfd, pos, SEEK_SET) != 0 | |
7244 | || bfd_bwrite (location, count, abfd) != count) | |
b34976b6 | 7245 | return FALSE; |
252b5132 | 7246 | |
b34976b6 | 7247 | return TRUE; |
252b5132 RH |
7248 | } |
7249 | ||
7250 | void | |
217aa764 AM |
7251 | _bfd_elf_no_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED, |
7252 | arelent *cache_ptr ATTRIBUTE_UNUSED, | |
7253 | Elf_Internal_Rela *dst ATTRIBUTE_UNUSED) | |
252b5132 RH |
7254 | { |
7255 | abort (); | |
7256 | } | |
7257 | ||
252b5132 RH |
7258 | /* Try to convert a non-ELF reloc into an ELF one. */ |
7259 | ||
b34976b6 | 7260 | bfd_boolean |
217aa764 | 7261 | _bfd_elf_validate_reloc (bfd *abfd, arelent *areloc) |
252b5132 | 7262 | { |
c044fabd | 7263 | /* Check whether we really have an ELF howto. */ |
252b5132 RH |
7264 | |
7265 | if ((*areloc->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec) | |
7266 | { | |
7267 | bfd_reloc_code_real_type code; | |
7268 | reloc_howto_type *howto; | |
7269 | ||
7270 | /* Alien reloc: Try to determine its type to replace it with an | |
c044fabd | 7271 | equivalent ELF reloc. */ |
252b5132 RH |
7272 | |
7273 | if (areloc->howto->pc_relative) | |
7274 | { | |
7275 | switch (areloc->howto->bitsize) | |
7276 | { | |
7277 | case 8: | |
7278 | code = BFD_RELOC_8_PCREL; | |
7279 | break; | |
7280 | case 12: | |
7281 | code = BFD_RELOC_12_PCREL; | |
7282 | break; | |
7283 | case 16: | |
7284 | code = BFD_RELOC_16_PCREL; | |
7285 | break; | |
7286 | case 24: | |
7287 | code = BFD_RELOC_24_PCREL; | |
7288 | break; | |
7289 | case 32: | |
7290 | code = BFD_RELOC_32_PCREL; | |
7291 | break; | |
7292 | case 64: | |
7293 | code = BFD_RELOC_64_PCREL; | |
7294 | break; | |
7295 | default: | |
7296 | goto fail; | |
7297 | } | |
7298 | ||
7299 | howto = bfd_reloc_type_lookup (abfd, code); | |
7300 | ||
7301 | if (areloc->howto->pcrel_offset != howto->pcrel_offset) | |
7302 | { | |
7303 | if (howto->pcrel_offset) | |
7304 | areloc->addend += areloc->address; | |
7305 | else | |
7306 | areloc->addend -= areloc->address; /* addend is unsigned!! */ | |
7307 | } | |
7308 | } | |
7309 | else | |
7310 | { | |
7311 | switch (areloc->howto->bitsize) | |
7312 | { | |
7313 | case 8: | |
7314 | code = BFD_RELOC_8; | |
7315 | break; | |
7316 | case 14: | |
7317 | code = BFD_RELOC_14; | |
7318 | break; | |
7319 | case 16: | |
7320 | code = BFD_RELOC_16; | |
7321 | break; | |
7322 | case 26: | |
7323 | code = BFD_RELOC_26; | |
7324 | break; | |
7325 | case 32: | |
7326 | code = BFD_RELOC_32; | |
7327 | break; | |
7328 | case 64: | |
7329 | code = BFD_RELOC_64; | |
7330 | break; | |
7331 | default: | |
7332 | goto fail; | |
7333 | } | |
7334 | ||
7335 | howto = bfd_reloc_type_lookup (abfd, code); | |
7336 | } | |
7337 | ||
7338 | if (howto) | |
7339 | areloc->howto = howto; | |
7340 | else | |
7341 | goto fail; | |
7342 | } | |
7343 | ||
b34976b6 | 7344 | return TRUE; |
252b5132 RH |
7345 | |
7346 | fail: | |
7347 | (*_bfd_error_handler) | |
d003868e AM |
7348 | (_("%B: unsupported relocation type %s"), |
7349 | abfd, areloc->howto->name); | |
252b5132 | 7350 | bfd_set_error (bfd_error_bad_value); |
b34976b6 | 7351 | return FALSE; |
252b5132 RH |
7352 | } |
7353 | ||
b34976b6 | 7354 | bfd_boolean |
217aa764 | 7355 | _bfd_elf_close_and_cleanup (bfd *abfd) |
252b5132 RH |
7356 | { |
7357 | if (bfd_get_format (abfd) == bfd_object) | |
7358 | { | |
b25e3d87 | 7359 | if (elf_tdata (abfd) != NULL && elf_shstrtab (abfd) != NULL) |
2b0f7ef9 | 7360 | _bfd_elf_strtab_free (elf_shstrtab (abfd)); |
6f140a15 | 7361 | _bfd_dwarf2_cleanup_debug_info (abfd); |
252b5132 RH |
7362 | } |
7363 | ||
7364 | return _bfd_generic_close_and_cleanup (abfd); | |
7365 | } | |
7366 | ||
7367 | /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY | |
7368 | in the relocation's offset. Thus we cannot allow any sort of sanity | |
7369 | range-checking to interfere. There is nothing else to do in processing | |
7370 | this reloc. */ | |
7371 | ||
7372 | bfd_reloc_status_type | |
217aa764 AM |
7373 | _bfd_elf_rel_vtable_reloc_fn |
7374 | (bfd *abfd ATTRIBUTE_UNUSED, arelent *re ATTRIBUTE_UNUSED, | |
fc0a2244 | 7375 | struct bfd_symbol *symbol ATTRIBUTE_UNUSED, |
217aa764 AM |
7376 | void *data ATTRIBUTE_UNUSED, asection *is ATTRIBUTE_UNUSED, |
7377 | bfd *obfd ATTRIBUTE_UNUSED, char **errmsg ATTRIBUTE_UNUSED) | |
252b5132 RH |
7378 | { |
7379 | return bfd_reloc_ok; | |
7380 | } | |
252b5132 RH |
7381 | \f |
7382 | /* Elf core file support. Much of this only works on native | |
7383 | toolchains, since we rely on knowing the | |
7384 | machine-dependent procfs structure in order to pick | |
c044fabd | 7385 | out details about the corefile. */ |
252b5132 RH |
7386 | |
7387 | #ifdef HAVE_SYS_PROCFS_H | |
7388 | # include <sys/procfs.h> | |
7389 | #endif | |
7390 | ||
c044fabd | 7391 | /* FIXME: this is kinda wrong, but it's what gdb wants. */ |
252b5132 RH |
7392 | |
7393 | static int | |
217aa764 | 7394 | elfcore_make_pid (bfd *abfd) |
252b5132 RH |
7395 | { |
7396 | return ((elf_tdata (abfd)->core_lwpid << 16) | |
7397 | + (elf_tdata (abfd)->core_pid)); | |
7398 | } | |
7399 | ||
252b5132 RH |
7400 | /* If there isn't a section called NAME, make one, using |
7401 | data from SECT. Note, this function will generate a | |
7402 | reference to NAME, so you shouldn't deallocate or | |
c044fabd | 7403 | overwrite it. */ |
252b5132 | 7404 | |
b34976b6 | 7405 | static bfd_boolean |
217aa764 | 7406 | elfcore_maybe_make_sect (bfd *abfd, char *name, asection *sect) |
252b5132 | 7407 | { |
c044fabd | 7408 | asection *sect2; |
252b5132 RH |
7409 | |
7410 | if (bfd_get_section_by_name (abfd, name) != NULL) | |
b34976b6 | 7411 | return TRUE; |
252b5132 | 7412 | |
117ed4f8 | 7413 | sect2 = bfd_make_section_with_flags (abfd, name, sect->flags); |
252b5132 | 7414 | if (sect2 == NULL) |
b34976b6 | 7415 | return FALSE; |
252b5132 | 7416 | |
eea6121a | 7417 | sect2->size = sect->size; |
252b5132 | 7418 | sect2->filepos = sect->filepos; |
252b5132 | 7419 | sect2->alignment_power = sect->alignment_power; |
b34976b6 | 7420 | return TRUE; |
252b5132 RH |
7421 | } |
7422 | ||
bb0082d6 AM |
7423 | /* Create a pseudosection containing SIZE bytes at FILEPOS. This |
7424 | actually creates up to two pseudosections: | |
7425 | - For the single-threaded case, a section named NAME, unless | |
7426 | such a section already exists. | |
7427 | - For the multi-threaded case, a section named "NAME/PID", where | |
7428 | PID is elfcore_make_pid (abfd). | |
7429 | Both pseudosections have identical contents. */ | |
b34976b6 | 7430 | bfd_boolean |
217aa764 AM |
7431 | _bfd_elfcore_make_pseudosection (bfd *abfd, |
7432 | char *name, | |
7433 | size_t size, | |
7434 | ufile_ptr filepos) | |
bb0082d6 AM |
7435 | { |
7436 | char buf[100]; | |
7437 | char *threaded_name; | |
d4c88bbb | 7438 | size_t len; |
bb0082d6 AM |
7439 | asection *sect; |
7440 | ||
7441 | /* Build the section name. */ | |
7442 | ||
7443 | sprintf (buf, "%s/%d", name, elfcore_make_pid (abfd)); | |
d4c88bbb | 7444 | len = strlen (buf) + 1; |
217aa764 | 7445 | threaded_name = bfd_alloc (abfd, len); |
bb0082d6 | 7446 | if (threaded_name == NULL) |
b34976b6 | 7447 | return FALSE; |
d4c88bbb | 7448 | memcpy (threaded_name, buf, len); |
bb0082d6 | 7449 | |
117ed4f8 AM |
7450 | sect = bfd_make_section_anyway_with_flags (abfd, threaded_name, |
7451 | SEC_HAS_CONTENTS); | |
bb0082d6 | 7452 | if (sect == NULL) |
b34976b6 | 7453 | return FALSE; |
eea6121a | 7454 | sect->size = size; |
bb0082d6 | 7455 | sect->filepos = filepos; |
bb0082d6 AM |
7456 | sect->alignment_power = 2; |
7457 | ||
936e320b | 7458 | return elfcore_maybe_make_sect (abfd, name, sect); |
bb0082d6 AM |
7459 | } |
7460 | ||
252b5132 | 7461 | /* prstatus_t exists on: |
4a938328 | 7462 | solaris 2.5+ |
252b5132 RH |
7463 | linux 2.[01] + glibc |
7464 | unixware 4.2 | |
7465 | */ | |
7466 | ||
7467 | #if defined (HAVE_PRSTATUS_T) | |
a7b97311 | 7468 | |
b34976b6 | 7469 | static bfd_boolean |
217aa764 | 7470 | elfcore_grok_prstatus (bfd *abfd, Elf_Internal_Note *note) |
252b5132 | 7471 | { |
eea6121a | 7472 | size_t size; |
7ee38065 | 7473 | int offset; |
252b5132 | 7474 | |
4a938328 MS |
7475 | if (note->descsz == sizeof (prstatus_t)) |
7476 | { | |
7477 | prstatus_t prstat; | |
252b5132 | 7478 | |
eea6121a | 7479 | size = sizeof (prstat.pr_reg); |
7ee38065 | 7480 | offset = offsetof (prstatus_t, pr_reg); |
4a938328 | 7481 | memcpy (&prstat, note->descdata, sizeof (prstat)); |
252b5132 | 7482 | |
fa49d224 NC |
7483 | /* Do not overwrite the core signal if it |
7484 | has already been set by another thread. */ | |
7485 | if (elf_tdata (abfd)->core_signal == 0) | |
7486 | elf_tdata (abfd)->core_signal = prstat.pr_cursig; | |
4a938328 | 7487 | elf_tdata (abfd)->core_pid = prstat.pr_pid; |
252b5132 | 7488 | |
4a938328 MS |
7489 | /* pr_who exists on: |
7490 | solaris 2.5+ | |
7491 | unixware 4.2 | |
7492 | pr_who doesn't exist on: | |
7493 | linux 2.[01] | |
7494 | */ | |
252b5132 | 7495 | #if defined (HAVE_PRSTATUS_T_PR_WHO) |
4a938328 | 7496 | elf_tdata (abfd)->core_lwpid = prstat.pr_who; |
252b5132 | 7497 | #endif |
4a938328 | 7498 | } |
7ee38065 | 7499 | #if defined (HAVE_PRSTATUS32_T) |
4a938328 MS |
7500 | else if (note->descsz == sizeof (prstatus32_t)) |
7501 | { | |
7502 | /* 64-bit host, 32-bit corefile */ | |
7503 | prstatus32_t prstat; | |
7504 | ||
eea6121a | 7505 | size = sizeof (prstat.pr_reg); |
7ee38065 | 7506 | offset = offsetof (prstatus32_t, pr_reg); |
4a938328 MS |
7507 | memcpy (&prstat, note->descdata, sizeof (prstat)); |
7508 | ||
fa49d224 NC |
7509 | /* Do not overwrite the core signal if it |
7510 | has already been set by another thread. */ | |
7511 | if (elf_tdata (abfd)->core_signal == 0) | |
7512 | elf_tdata (abfd)->core_signal = prstat.pr_cursig; | |
4a938328 MS |
7513 | elf_tdata (abfd)->core_pid = prstat.pr_pid; |
7514 | ||
7515 | /* pr_who exists on: | |
7516 | solaris 2.5+ | |
7517 | unixware 4.2 | |
7518 | pr_who doesn't exist on: | |
7519 | linux 2.[01] | |
7520 | */ | |
7ee38065 | 7521 | #if defined (HAVE_PRSTATUS32_T_PR_WHO) |
4a938328 MS |
7522 | elf_tdata (abfd)->core_lwpid = prstat.pr_who; |
7523 | #endif | |
7524 | } | |
7ee38065 | 7525 | #endif /* HAVE_PRSTATUS32_T */ |
4a938328 MS |
7526 | else |
7527 | { | |
7528 | /* Fail - we don't know how to handle any other | |
7529 | note size (ie. data object type). */ | |
b34976b6 | 7530 | return TRUE; |
4a938328 | 7531 | } |
252b5132 | 7532 | |
bb0082d6 | 7533 | /* Make a ".reg/999" section and a ".reg" section. */ |
936e320b | 7534 | return _bfd_elfcore_make_pseudosection (abfd, ".reg", |
eea6121a | 7535 | size, note->descpos + offset); |
252b5132 RH |
7536 | } |
7537 | #endif /* defined (HAVE_PRSTATUS_T) */ | |
7538 | ||
bb0082d6 | 7539 | /* Create a pseudosection containing the exact contents of NOTE. */ |
b34976b6 | 7540 | static bfd_boolean |
217aa764 AM |
7541 | elfcore_make_note_pseudosection (bfd *abfd, |
7542 | char *name, | |
7543 | Elf_Internal_Note *note) | |
252b5132 | 7544 | { |
936e320b AM |
7545 | return _bfd_elfcore_make_pseudosection (abfd, name, |
7546 | note->descsz, note->descpos); | |
252b5132 RH |
7547 | } |
7548 | ||
ff08c6bb JB |
7549 | /* There isn't a consistent prfpregset_t across platforms, |
7550 | but it doesn't matter, because we don't have to pick this | |
c044fabd KH |
7551 | data structure apart. */ |
7552 | ||
b34976b6 | 7553 | static bfd_boolean |
217aa764 | 7554 | elfcore_grok_prfpreg (bfd *abfd, Elf_Internal_Note *note) |
ff08c6bb JB |
7555 | { |
7556 | return elfcore_make_note_pseudosection (abfd, ".reg2", note); | |
7557 | } | |
7558 | ||
ff08c6bb | 7559 | /* Linux dumps the Intel SSE regs in a note named "LINUX" with a note |
971d4640 | 7560 | type of NT_PRXFPREG. Just include the whole note's contents |
ff08c6bb | 7561 | literally. */ |
c044fabd | 7562 | |
b34976b6 | 7563 | static bfd_boolean |
217aa764 | 7564 | elfcore_grok_prxfpreg (bfd *abfd, Elf_Internal_Note *note) |
ff08c6bb JB |
7565 | { |
7566 | return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note); | |
7567 | } | |
7568 | ||
97753bd5 AM |
7569 | static bfd_boolean |
7570 | elfcore_grok_ppc_vmx (bfd *abfd, Elf_Internal_Note *note) | |
7571 | { | |
7572 | return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vmx", note); | |
7573 | } | |
7574 | ||
89eeb0bc LM |
7575 | static bfd_boolean |
7576 | elfcore_grok_ppc_vsx (bfd *abfd, Elf_Internal_Note *note) | |
7577 | { | |
7578 | return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vsx", note); | |
7579 | } | |
97753bd5 | 7580 | |
252b5132 | 7581 | #if defined (HAVE_PRPSINFO_T) |
4a938328 | 7582 | typedef prpsinfo_t elfcore_psinfo_t; |
7ee38065 | 7583 | #if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */ |
4a938328 MS |
7584 | typedef prpsinfo32_t elfcore_psinfo32_t; |
7585 | #endif | |
252b5132 RH |
7586 | #endif |
7587 | ||
7588 | #if defined (HAVE_PSINFO_T) | |
4a938328 | 7589 | typedef psinfo_t elfcore_psinfo_t; |
7ee38065 | 7590 | #if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */ |
4a938328 MS |
7591 | typedef psinfo32_t elfcore_psinfo32_t; |
7592 | #endif | |
252b5132 RH |
7593 | #endif |
7594 | ||
252b5132 RH |
7595 | /* return a malloc'ed copy of a string at START which is at |
7596 | most MAX bytes long, possibly without a terminating '\0'. | |
c044fabd | 7597 | the copy will always have a terminating '\0'. */ |
252b5132 | 7598 | |
936e320b | 7599 | char * |
217aa764 | 7600 | _bfd_elfcore_strndup (bfd *abfd, char *start, size_t max) |
252b5132 | 7601 | { |
dc810e39 | 7602 | char *dups; |
c044fabd | 7603 | char *end = memchr (start, '\0', max); |
dc810e39 | 7604 | size_t len; |
252b5132 RH |
7605 | |
7606 | if (end == NULL) | |
7607 | len = max; | |
7608 | else | |
7609 | len = end - start; | |
7610 | ||
217aa764 | 7611 | dups = bfd_alloc (abfd, len + 1); |
dc810e39 | 7612 | if (dups == NULL) |
252b5132 RH |
7613 | return NULL; |
7614 | ||
dc810e39 AM |
7615 | memcpy (dups, start, len); |
7616 | dups[len] = '\0'; | |
252b5132 | 7617 | |
dc810e39 | 7618 | return dups; |
252b5132 RH |
7619 | } |
7620 | ||
bb0082d6 | 7621 | #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) |
b34976b6 | 7622 | static bfd_boolean |
217aa764 | 7623 | elfcore_grok_psinfo (bfd *abfd, Elf_Internal_Note *note) |
252b5132 | 7624 | { |
4a938328 MS |
7625 | if (note->descsz == sizeof (elfcore_psinfo_t)) |
7626 | { | |
7627 | elfcore_psinfo_t psinfo; | |
252b5132 | 7628 | |
7ee38065 | 7629 | memcpy (&psinfo, note->descdata, sizeof (psinfo)); |
252b5132 | 7630 | |
4a938328 | 7631 | elf_tdata (abfd)->core_program |
936e320b AM |
7632 | = _bfd_elfcore_strndup (abfd, psinfo.pr_fname, |
7633 | sizeof (psinfo.pr_fname)); | |
252b5132 | 7634 | |
4a938328 | 7635 | elf_tdata (abfd)->core_command |
936e320b AM |
7636 | = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs, |
7637 | sizeof (psinfo.pr_psargs)); | |
4a938328 | 7638 | } |
7ee38065 | 7639 | #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T) |
4a938328 MS |
7640 | else if (note->descsz == sizeof (elfcore_psinfo32_t)) |
7641 | { | |
7642 | /* 64-bit host, 32-bit corefile */ | |
7643 | elfcore_psinfo32_t psinfo; | |
7644 | ||
7ee38065 | 7645 | memcpy (&psinfo, note->descdata, sizeof (psinfo)); |
252b5132 | 7646 | |
4a938328 | 7647 | elf_tdata (abfd)->core_program |
936e320b AM |
7648 | = _bfd_elfcore_strndup (abfd, psinfo.pr_fname, |
7649 | sizeof (psinfo.pr_fname)); | |
4a938328 MS |
7650 | |
7651 | elf_tdata (abfd)->core_command | |
936e320b AM |
7652 | = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs, |
7653 | sizeof (psinfo.pr_psargs)); | |
4a938328 MS |
7654 | } |
7655 | #endif | |
7656 | ||
7657 | else | |
7658 | { | |
7659 | /* Fail - we don't know how to handle any other | |
7660 | note size (ie. data object type). */ | |
b34976b6 | 7661 | return TRUE; |
4a938328 | 7662 | } |
252b5132 RH |
7663 | |
7664 | /* Note that for some reason, a spurious space is tacked | |
7665 | onto the end of the args in some (at least one anyway) | |
c044fabd | 7666 | implementations, so strip it off if it exists. */ |
252b5132 RH |
7667 | |
7668 | { | |
c044fabd | 7669 | char *command = elf_tdata (abfd)->core_command; |
252b5132 RH |
7670 | int n = strlen (command); |
7671 | ||
7672 | if (0 < n && command[n - 1] == ' ') | |
7673 | command[n - 1] = '\0'; | |
7674 | } | |
7675 | ||
b34976b6 | 7676 | return TRUE; |
252b5132 RH |
7677 | } |
7678 | #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */ | |
7679 | ||
252b5132 | 7680 | #if defined (HAVE_PSTATUS_T) |
b34976b6 | 7681 | static bfd_boolean |
217aa764 | 7682 | elfcore_grok_pstatus (bfd *abfd, Elf_Internal_Note *note) |
252b5132 | 7683 | { |
f572a39d AM |
7684 | if (note->descsz == sizeof (pstatus_t) |
7685 | #if defined (HAVE_PXSTATUS_T) | |
7686 | || note->descsz == sizeof (pxstatus_t) | |
7687 | #endif | |
7688 | ) | |
4a938328 MS |
7689 | { |
7690 | pstatus_t pstat; | |
252b5132 | 7691 | |
4a938328 | 7692 | memcpy (&pstat, note->descdata, sizeof (pstat)); |
252b5132 | 7693 | |
4a938328 MS |
7694 | elf_tdata (abfd)->core_pid = pstat.pr_pid; |
7695 | } | |
7ee38065 | 7696 | #if defined (HAVE_PSTATUS32_T) |
4a938328 MS |
7697 | else if (note->descsz == sizeof (pstatus32_t)) |
7698 | { | |
7699 | /* 64-bit host, 32-bit corefile */ | |
7700 | pstatus32_t pstat; | |
252b5132 | 7701 | |
4a938328 | 7702 | memcpy (&pstat, note->descdata, sizeof (pstat)); |
252b5132 | 7703 | |
4a938328 MS |
7704 | elf_tdata (abfd)->core_pid = pstat.pr_pid; |
7705 | } | |
7706 | #endif | |
252b5132 RH |
7707 | /* Could grab some more details from the "representative" |
7708 | lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an | |
c044fabd | 7709 | NT_LWPSTATUS note, presumably. */ |
252b5132 | 7710 | |
b34976b6 | 7711 | return TRUE; |
252b5132 RH |
7712 | } |
7713 | #endif /* defined (HAVE_PSTATUS_T) */ | |
7714 | ||
252b5132 | 7715 | #if defined (HAVE_LWPSTATUS_T) |
b34976b6 | 7716 | static bfd_boolean |
217aa764 | 7717 | elfcore_grok_lwpstatus (bfd *abfd, Elf_Internal_Note *note) |
252b5132 RH |
7718 | { |
7719 | lwpstatus_t lwpstat; | |
7720 | char buf[100]; | |
c044fabd | 7721 | char *name; |
d4c88bbb | 7722 | size_t len; |
c044fabd | 7723 | asection *sect; |
252b5132 | 7724 | |
f572a39d AM |
7725 | if (note->descsz != sizeof (lwpstat) |
7726 | #if defined (HAVE_LWPXSTATUS_T) | |
7727 | && note->descsz != sizeof (lwpxstatus_t) | |
7728 | #endif | |
7729 | ) | |
b34976b6 | 7730 | return TRUE; |
252b5132 RH |
7731 | |
7732 | memcpy (&lwpstat, note->descdata, sizeof (lwpstat)); | |
7733 | ||
7734 | elf_tdata (abfd)->core_lwpid = lwpstat.pr_lwpid; | |
7735 | elf_tdata (abfd)->core_signal = lwpstat.pr_cursig; | |
7736 | ||
c044fabd | 7737 | /* Make a ".reg/999" section. */ |
252b5132 RH |
7738 | |
7739 | sprintf (buf, ".reg/%d", elfcore_make_pid (abfd)); | |
d4c88bbb | 7740 | len = strlen (buf) + 1; |
217aa764 | 7741 | name = bfd_alloc (abfd, len); |
252b5132 | 7742 | if (name == NULL) |
b34976b6 | 7743 | return FALSE; |
d4c88bbb | 7744 | memcpy (name, buf, len); |
252b5132 | 7745 | |
117ed4f8 | 7746 | sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); |
252b5132 | 7747 | if (sect == NULL) |
b34976b6 | 7748 | return FALSE; |
252b5132 RH |
7749 | |
7750 | #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT) | |
eea6121a | 7751 | sect->size = sizeof (lwpstat.pr_context.uc_mcontext.gregs); |
252b5132 RH |
7752 | sect->filepos = note->descpos |
7753 | + offsetof (lwpstatus_t, pr_context.uc_mcontext.gregs); | |
7754 | #endif | |
7755 | ||
7756 | #if defined (HAVE_LWPSTATUS_T_PR_REG) | |
eea6121a | 7757 | sect->size = sizeof (lwpstat.pr_reg); |
252b5132 RH |
7758 | sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_reg); |
7759 | #endif | |
7760 | ||
252b5132 RH |
7761 | sect->alignment_power = 2; |
7762 | ||
7763 | if (!elfcore_maybe_make_sect (abfd, ".reg", sect)) | |
b34976b6 | 7764 | return FALSE; |
252b5132 RH |
7765 | |
7766 | /* Make a ".reg2/999" section */ | |
7767 | ||
7768 | sprintf (buf, ".reg2/%d", elfcore_make_pid (abfd)); | |
d4c88bbb | 7769 | len = strlen (buf) + 1; |
217aa764 | 7770 | name = bfd_alloc (abfd, len); |
252b5132 | 7771 | if (name == NULL) |
b34976b6 | 7772 | return FALSE; |
d4c88bbb | 7773 | memcpy (name, buf, len); |
252b5132 | 7774 | |
117ed4f8 | 7775 | sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); |
252b5132 | 7776 | if (sect == NULL) |
b34976b6 | 7777 | return FALSE; |
252b5132 RH |
7778 | |
7779 | #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT) | |
eea6121a | 7780 | sect->size = sizeof (lwpstat.pr_context.uc_mcontext.fpregs); |
252b5132 RH |
7781 | sect->filepos = note->descpos |
7782 | + offsetof (lwpstatus_t, pr_context.uc_mcontext.fpregs); | |
7783 | #endif | |
7784 | ||
7785 | #if defined (HAVE_LWPSTATUS_T_PR_FPREG) | |
eea6121a | 7786 | sect->size = sizeof (lwpstat.pr_fpreg); |
252b5132 RH |
7787 | sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_fpreg); |
7788 | #endif | |
7789 | ||
252b5132 RH |
7790 | sect->alignment_power = 2; |
7791 | ||
936e320b | 7792 | return elfcore_maybe_make_sect (abfd, ".reg2", sect); |
252b5132 RH |
7793 | } |
7794 | #endif /* defined (HAVE_LWPSTATUS_T) */ | |
7795 | ||
b34976b6 | 7796 | static bfd_boolean |
217aa764 | 7797 | elfcore_grok_win32pstatus (bfd *abfd, Elf_Internal_Note *note) |
16e9c715 NC |
7798 | { |
7799 | char buf[30]; | |
c044fabd | 7800 | char *name; |
d4c88bbb | 7801 | size_t len; |
c044fabd | 7802 | asection *sect; |
4a6636fb PA |
7803 | int type; |
7804 | int is_active_thread; | |
7805 | bfd_vma base_addr; | |
16e9c715 | 7806 | |
4a6636fb | 7807 | if (note->descsz < 728) |
b34976b6 | 7808 | return TRUE; |
16e9c715 | 7809 | |
4a6636fb PA |
7810 | if (! CONST_STRNEQ (note->namedata, "win32")) |
7811 | return TRUE; | |
7812 | ||
7813 | type = bfd_get_32 (abfd, note->descdata); | |
c044fabd | 7814 | |
4a6636fb | 7815 | switch (type) |
16e9c715 | 7816 | { |
4a6636fb | 7817 | case 1 /* NOTE_INFO_PROCESS */: |
16e9c715 | 7818 | /* FIXME: need to add ->core_command. */ |
4a6636fb PA |
7819 | /* process_info.pid */ |
7820 | elf_tdata (abfd)->core_pid = bfd_get_32 (abfd, note->descdata + 8); | |
7821 | /* process_info.signal */ | |
7822 | elf_tdata (abfd)->core_signal = bfd_get_32 (abfd, note->descdata + 12); | |
c044fabd | 7823 | break; |
16e9c715 | 7824 | |
4a6636fb | 7825 | case 2 /* NOTE_INFO_THREAD */: |
16e9c715 | 7826 | /* Make a ".reg/999" section. */ |
4a6636fb PA |
7827 | /* thread_info.tid */ |
7828 | sprintf (buf, ".reg/%ld", (long) bfd_get_32 (abfd, note->descdata + 8)); | |
c044fabd | 7829 | |
d4c88bbb | 7830 | len = strlen (buf) + 1; |
217aa764 | 7831 | name = bfd_alloc (abfd, len); |
16e9c715 | 7832 | if (name == NULL) |
b34976b6 | 7833 | return FALSE; |
c044fabd | 7834 | |
d4c88bbb | 7835 | memcpy (name, buf, len); |
16e9c715 | 7836 | |
117ed4f8 | 7837 | sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); |
16e9c715 | 7838 | if (sect == NULL) |
b34976b6 | 7839 | return FALSE; |
c044fabd | 7840 | |
4a6636fb PA |
7841 | /* sizeof (thread_info.thread_context) */ |
7842 | sect->size = 716; | |
7843 | /* offsetof (thread_info.thread_context) */ | |
7844 | sect->filepos = note->descpos + 12; | |
16e9c715 NC |
7845 | sect->alignment_power = 2; |
7846 | ||
4a6636fb PA |
7847 | /* thread_info.is_active_thread */ |
7848 | is_active_thread = bfd_get_32 (abfd, note->descdata + 8); | |
7849 | ||
7850 | if (is_active_thread) | |
16e9c715 | 7851 | if (! elfcore_maybe_make_sect (abfd, ".reg", sect)) |
b34976b6 | 7852 | return FALSE; |
16e9c715 NC |
7853 | break; |
7854 | ||
4a6636fb | 7855 | case 3 /* NOTE_INFO_MODULE */: |
16e9c715 | 7856 | /* Make a ".module/xxxxxxxx" section. */ |
4a6636fb PA |
7857 | /* module_info.base_address */ |
7858 | base_addr = bfd_get_32 (abfd, note->descdata + 4); | |
0af1713e | 7859 | sprintf (buf, ".module/%08lx", (unsigned long) base_addr); |
c044fabd | 7860 | |
d4c88bbb | 7861 | len = strlen (buf) + 1; |
217aa764 | 7862 | name = bfd_alloc (abfd, len); |
16e9c715 | 7863 | if (name == NULL) |
b34976b6 | 7864 | return FALSE; |
c044fabd | 7865 | |
d4c88bbb | 7866 | memcpy (name, buf, len); |
252b5132 | 7867 | |
117ed4f8 | 7868 | sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); |
c044fabd | 7869 | |
16e9c715 | 7870 | if (sect == NULL) |
b34976b6 | 7871 | return FALSE; |
c044fabd | 7872 | |
eea6121a | 7873 | sect->size = note->descsz; |
16e9c715 | 7874 | sect->filepos = note->descpos; |
16e9c715 NC |
7875 | sect->alignment_power = 2; |
7876 | break; | |
7877 | ||
7878 | default: | |
b34976b6 | 7879 | return TRUE; |
16e9c715 NC |
7880 | } |
7881 | ||
b34976b6 | 7882 | return TRUE; |
16e9c715 | 7883 | } |
252b5132 | 7884 | |
b34976b6 | 7885 | static bfd_boolean |
217aa764 | 7886 | elfcore_grok_note (bfd *abfd, Elf_Internal_Note *note) |
252b5132 | 7887 | { |
9c5bfbb7 | 7888 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
bb0082d6 | 7889 | |
252b5132 RH |
7890 | switch (note->type) |
7891 | { | |
7892 | default: | |
b34976b6 | 7893 | return TRUE; |
252b5132 | 7894 | |
252b5132 | 7895 | case NT_PRSTATUS: |
bb0082d6 AM |
7896 | if (bed->elf_backend_grok_prstatus) |
7897 | if ((*bed->elf_backend_grok_prstatus) (abfd, note)) | |
b34976b6 | 7898 | return TRUE; |
bb0082d6 | 7899 | #if defined (HAVE_PRSTATUS_T) |
252b5132 | 7900 | return elfcore_grok_prstatus (abfd, note); |
bb0082d6 | 7901 | #else |
b34976b6 | 7902 | return TRUE; |
252b5132 RH |
7903 | #endif |
7904 | ||
7905 | #if defined (HAVE_PSTATUS_T) | |
7906 | case NT_PSTATUS: | |
7907 | return elfcore_grok_pstatus (abfd, note); | |
7908 | #endif | |
7909 | ||
7910 | #if defined (HAVE_LWPSTATUS_T) | |
7911 | case NT_LWPSTATUS: | |
7912 | return elfcore_grok_lwpstatus (abfd, note); | |
7913 | #endif | |
7914 | ||
7915 | case NT_FPREGSET: /* FIXME: rename to NT_PRFPREG */ | |
7916 | return elfcore_grok_prfpreg (abfd, note); | |
7917 | ||
c044fabd | 7918 | case NT_WIN32PSTATUS: |
16e9c715 | 7919 | return elfcore_grok_win32pstatus (abfd, note); |
16e9c715 | 7920 | |
c044fabd | 7921 | case NT_PRXFPREG: /* Linux SSE extension */ |
e377ab71 MK |
7922 | if (note->namesz == 6 |
7923 | && strcmp (note->namedata, "LINUX") == 0) | |
ff08c6bb JB |
7924 | return elfcore_grok_prxfpreg (abfd, note); |
7925 | else | |
b34976b6 | 7926 | return TRUE; |
ff08c6bb | 7927 | |
97753bd5 AM |
7928 | case NT_PPC_VMX: |
7929 | if (note->namesz == 6 | |
7930 | && strcmp (note->namedata, "LINUX") == 0) | |
7931 | return elfcore_grok_ppc_vmx (abfd, note); | |
7932 | else | |
7933 | return TRUE; | |
7934 | ||
89eeb0bc LM |
7935 | case NT_PPC_VSX: |
7936 | if (note->namesz == 6 | |
7937 | && strcmp (note->namedata, "LINUX") == 0) | |
7938 | return elfcore_grok_ppc_vsx (abfd, note); | |
7939 | else | |
7940 | return TRUE; | |
7941 | ||
252b5132 RH |
7942 | case NT_PRPSINFO: |
7943 | case NT_PSINFO: | |
bb0082d6 AM |
7944 | if (bed->elf_backend_grok_psinfo) |
7945 | if ((*bed->elf_backend_grok_psinfo) (abfd, note)) | |
b34976b6 | 7946 | return TRUE; |
bb0082d6 | 7947 | #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) |
252b5132 | 7948 | return elfcore_grok_psinfo (abfd, note); |
bb0082d6 | 7949 | #else |
b34976b6 | 7950 | return TRUE; |
252b5132 | 7951 | #endif |
3333a7c3 RM |
7952 | |
7953 | case NT_AUXV: | |
7954 | { | |
117ed4f8 AM |
7955 | asection *sect = bfd_make_section_anyway_with_flags (abfd, ".auxv", |
7956 | SEC_HAS_CONTENTS); | |
3333a7c3 RM |
7957 | |
7958 | if (sect == NULL) | |
7959 | return FALSE; | |
eea6121a | 7960 | sect->size = note->descsz; |
3333a7c3 | 7961 | sect->filepos = note->descpos; |
3333a7c3 RM |
7962 | sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32; |
7963 | ||
7964 | return TRUE; | |
7965 | } | |
252b5132 RH |
7966 | } |
7967 | } | |
7968 | ||
718175fa JK |
7969 | static bfd_boolean |
7970 | elfobj_grok_gnu_build_id (bfd *abfd, Elf_Internal_Note *note) | |
7971 | { | |
7972 | elf_tdata (abfd)->build_id_size = note->descsz; | |
7973 | elf_tdata (abfd)->build_id = bfd_alloc (abfd, note->descsz); | |
7974 | if (elf_tdata (abfd)->build_id == NULL) | |
7975 | return FALSE; | |
7976 | ||
7977 | memcpy (elf_tdata (abfd)->build_id, note->descdata, note->descsz); | |
7978 | ||
7979 | return TRUE; | |
7980 | } | |
7981 | ||
7982 | static bfd_boolean | |
7983 | elfobj_grok_gnu_note (bfd *abfd, Elf_Internal_Note *note) | |
7984 | { | |
7985 | switch (note->type) | |
7986 | { | |
7987 | default: | |
7988 | return TRUE; | |
7989 | ||
7990 | case NT_GNU_BUILD_ID: | |
7991 | return elfobj_grok_gnu_build_id (abfd, note); | |
7992 | } | |
7993 | } | |
7994 | ||
b34976b6 | 7995 | static bfd_boolean |
217aa764 | 7996 | elfcore_netbsd_get_lwpid (Elf_Internal_Note *note, int *lwpidp) |
50b2bdb7 AM |
7997 | { |
7998 | char *cp; | |
7999 | ||
8000 | cp = strchr (note->namedata, '@'); | |
8001 | if (cp != NULL) | |
8002 | { | |
d2b64500 | 8003 | *lwpidp = atoi(cp + 1); |
b34976b6 | 8004 | return TRUE; |
50b2bdb7 | 8005 | } |
b34976b6 | 8006 | return FALSE; |
50b2bdb7 AM |
8007 | } |
8008 | ||
b34976b6 | 8009 | static bfd_boolean |
217aa764 | 8010 | elfcore_grok_netbsd_procinfo (bfd *abfd, Elf_Internal_Note *note) |
50b2bdb7 | 8011 | { |
50b2bdb7 AM |
8012 | /* Signal number at offset 0x08. */ |
8013 | elf_tdata (abfd)->core_signal | |
8014 | = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08); | |
8015 | ||
8016 | /* Process ID at offset 0x50. */ | |
8017 | elf_tdata (abfd)->core_pid | |
8018 | = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x50); | |
8019 | ||
8020 | /* Command name at 0x7c (max 32 bytes, including nul). */ | |
8021 | elf_tdata (abfd)->core_command | |
8022 | = _bfd_elfcore_strndup (abfd, note->descdata + 0x7c, 31); | |
8023 | ||
7720ba9f MK |
8024 | return elfcore_make_note_pseudosection (abfd, ".note.netbsdcore.procinfo", |
8025 | note); | |
50b2bdb7 AM |
8026 | } |
8027 | ||
b34976b6 | 8028 | static bfd_boolean |
217aa764 | 8029 | elfcore_grok_netbsd_note (bfd *abfd, Elf_Internal_Note *note) |
50b2bdb7 AM |
8030 | { |
8031 | int lwp; | |
8032 | ||
8033 | if (elfcore_netbsd_get_lwpid (note, &lwp)) | |
8034 | elf_tdata (abfd)->core_lwpid = lwp; | |
8035 | ||
b4db1224 | 8036 | if (note->type == NT_NETBSDCORE_PROCINFO) |
50b2bdb7 AM |
8037 | { |
8038 | /* NetBSD-specific core "procinfo". Note that we expect to | |
08a40648 AM |
8039 | find this note before any of the others, which is fine, |
8040 | since the kernel writes this note out first when it | |
8041 | creates a core file. */ | |
47d9a591 | 8042 | |
50b2bdb7 AM |
8043 | return elfcore_grok_netbsd_procinfo (abfd, note); |
8044 | } | |
8045 | ||
b4db1224 JT |
8046 | /* As of Jan 2002 there are no other machine-independent notes |
8047 | defined for NetBSD core files. If the note type is less | |
8048 | than the start of the machine-dependent note types, we don't | |
8049 | understand it. */ | |
47d9a591 | 8050 | |
b4db1224 | 8051 | if (note->type < NT_NETBSDCORE_FIRSTMACH) |
b34976b6 | 8052 | return TRUE; |
50b2bdb7 AM |
8053 | |
8054 | ||
8055 | switch (bfd_get_arch (abfd)) | |
8056 | { | |
08a40648 AM |
8057 | /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and |
8058 | PT_GETFPREGS == mach+2. */ | |
50b2bdb7 AM |
8059 | |
8060 | case bfd_arch_alpha: | |
8061 | case bfd_arch_sparc: | |
8062 | switch (note->type) | |
08a40648 AM |
8063 | { |
8064 | case NT_NETBSDCORE_FIRSTMACH+0: | |
8065 | return elfcore_make_note_pseudosection (abfd, ".reg", note); | |
50b2bdb7 | 8066 | |
08a40648 AM |
8067 | case NT_NETBSDCORE_FIRSTMACH+2: |
8068 | return elfcore_make_note_pseudosection (abfd, ".reg2", note); | |
50b2bdb7 | 8069 | |
08a40648 AM |
8070 | default: |
8071 | return TRUE; | |
8072 | } | |
50b2bdb7 | 8073 | |
08a40648 AM |
8074 | /* On all other arch's, PT_GETREGS == mach+1 and |
8075 | PT_GETFPREGS == mach+3. */ | |
50b2bdb7 AM |
8076 | |
8077 | default: | |
8078 | switch (note->type) | |
08a40648 AM |
8079 | { |
8080 | case NT_NETBSDCORE_FIRSTMACH+1: | |
8081 | return elfcore_make_note_pseudosection (abfd, ".reg", note); | |
50b2bdb7 | 8082 | |
08a40648 AM |
8083 | case NT_NETBSDCORE_FIRSTMACH+3: |
8084 | return elfcore_make_note_pseudosection (abfd, ".reg2", note); | |
50b2bdb7 | 8085 | |
08a40648 AM |
8086 | default: |
8087 | return TRUE; | |
8088 | } | |
50b2bdb7 AM |
8089 | } |
8090 | /* NOTREACHED */ | |
8091 | } | |
8092 | ||
67cc5033 MK |
8093 | static bfd_boolean |
8094 | elfcore_grok_openbsd_procinfo (bfd *abfd, Elf_Internal_Note *note) | |
8095 | { | |
8096 | /* Signal number at offset 0x08. */ | |
8097 | elf_tdata (abfd)->core_signal | |
8098 | = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08); | |
8099 | ||
8100 | /* Process ID at offset 0x20. */ | |
8101 | elf_tdata (abfd)->core_pid | |
8102 | = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x20); | |
8103 | ||
8104 | /* Command name at 0x48 (max 32 bytes, including nul). */ | |
8105 | elf_tdata (abfd)->core_command | |
8106 | = _bfd_elfcore_strndup (abfd, note->descdata + 0x48, 31); | |
8107 | ||
8108 | return TRUE; | |
8109 | } | |
8110 | ||
8111 | static bfd_boolean | |
8112 | elfcore_grok_openbsd_note (bfd *abfd, Elf_Internal_Note *note) | |
8113 | { | |
8114 | if (note->type == NT_OPENBSD_PROCINFO) | |
8115 | return elfcore_grok_openbsd_procinfo (abfd, note); | |
8116 | ||
8117 | if (note->type == NT_OPENBSD_REGS) | |
8118 | return elfcore_make_note_pseudosection (abfd, ".reg", note); | |
8119 | ||
8120 | if (note->type == NT_OPENBSD_FPREGS) | |
8121 | return elfcore_make_note_pseudosection (abfd, ".reg2", note); | |
8122 | ||
8123 | if (note->type == NT_OPENBSD_XFPREGS) | |
8124 | return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note); | |
8125 | ||
8126 | if (note->type == NT_OPENBSD_AUXV) | |
8127 | { | |
8128 | asection *sect = bfd_make_section_anyway_with_flags (abfd, ".auxv", | |
8129 | SEC_HAS_CONTENTS); | |
8130 | ||
8131 | if (sect == NULL) | |
8132 | return FALSE; | |
8133 | sect->size = note->descsz; | |
8134 | sect->filepos = note->descpos; | |
8135 | sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32; | |
8136 | ||
8137 | return TRUE; | |
8138 | } | |
8139 | ||
8140 | if (note->type == NT_OPENBSD_WCOOKIE) | |
8141 | { | |
8142 | asection *sect = bfd_make_section_anyway_with_flags (abfd, ".wcookie", | |
8143 | SEC_HAS_CONTENTS); | |
8144 | ||
8145 | if (sect == NULL) | |
8146 | return FALSE; | |
8147 | sect->size = note->descsz; | |
8148 | sect->filepos = note->descpos; | |
8149 | sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32; | |
8150 | ||
8151 | return TRUE; | |
8152 | } | |
8153 | ||
8154 | return TRUE; | |
8155 | } | |
8156 | ||
07c6e936 | 8157 | static bfd_boolean |
d3fd4074 | 8158 | elfcore_grok_nto_status (bfd *abfd, Elf_Internal_Note *note, long *tid) |
07c6e936 NC |
8159 | { |
8160 | void *ddata = note->descdata; | |
8161 | char buf[100]; | |
8162 | char *name; | |
8163 | asection *sect; | |
f8843e87 AM |
8164 | short sig; |
8165 | unsigned flags; | |
07c6e936 NC |
8166 | |
8167 | /* nto_procfs_status 'pid' field is at offset 0. */ | |
8168 | elf_tdata (abfd)->core_pid = bfd_get_32 (abfd, (bfd_byte *) ddata); | |
8169 | ||
f8843e87 AM |
8170 | /* nto_procfs_status 'tid' field is at offset 4. Pass it back. */ |
8171 | *tid = bfd_get_32 (abfd, (bfd_byte *) ddata + 4); | |
8172 | ||
8173 | /* nto_procfs_status 'flags' field is at offset 8. */ | |
8174 | flags = bfd_get_32 (abfd, (bfd_byte *) ddata + 8); | |
07c6e936 NC |
8175 | |
8176 | /* nto_procfs_status 'what' field is at offset 14. */ | |
f8843e87 AM |
8177 | if ((sig = bfd_get_16 (abfd, (bfd_byte *) ddata + 14)) > 0) |
8178 | { | |
8179 | elf_tdata (abfd)->core_signal = sig; | |
8180 | elf_tdata (abfd)->core_lwpid = *tid; | |
8181 | } | |
07c6e936 | 8182 | |
f8843e87 AM |
8183 | /* _DEBUG_FLAG_CURTID (current thread) is 0x80. Some cores |
8184 | do not come from signals so we make sure we set the current | |
8185 | thread just in case. */ | |
8186 | if (flags & 0x00000080) | |
8187 | elf_tdata (abfd)->core_lwpid = *tid; | |
07c6e936 NC |
8188 | |
8189 | /* Make a ".qnx_core_status/%d" section. */ | |
d3fd4074 | 8190 | sprintf (buf, ".qnx_core_status/%ld", *tid); |
07c6e936 | 8191 | |
217aa764 | 8192 | name = bfd_alloc (abfd, strlen (buf) + 1); |
07c6e936 NC |
8193 | if (name == NULL) |
8194 | return FALSE; | |
8195 | strcpy (name, buf); | |
8196 | ||
117ed4f8 | 8197 | sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); |
07c6e936 NC |
8198 | if (sect == NULL) |
8199 | return FALSE; | |
8200 | ||
eea6121a | 8201 | sect->size = note->descsz; |
07c6e936 | 8202 | sect->filepos = note->descpos; |
07c6e936 NC |
8203 | sect->alignment_power = 2; |
8204 | ||
8205 | return (elfcore_maybe_make_sect (abfd, ".qnx_core_status", sect)); | |
8206 | } | |
8207 | ||
8208 | static bfd_boolean | |
d69f560c KW |
8209 | elfcore_grok_nto_regs (bfd *abfd, |
8210 | Elf_Internal_Note *note, | |
d3fd4074 | 8211 | long tid, |
d69f560c | 8212 | char *base) |
07c6e936 NC |
8213 | { |
8214 | char buf[100]; | |
8215 | char *name; | |
8216 | asection *sect; | |
8217 | ||
d69f560c | 8218 | /* Make a "(base)/%d" section. */ |
d3fd4074 | 8219 | sprintf (buf, "%s/%ld", base, tid); |
07c6e936 | 8220 | |
217aa764 | 8221 | name = bfd_alloc (abfd, strlen (buf) + 1); |
07c6e936 NC |
8222 | if (name == NULL) |
8223 | return FALSE; | |
8224 | strcpy (name, buf); | |
8225 | ||
117ed4f8 | 8226 | sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); |
07c6e936 NC |
8227 | if (sect == NULL) |
8228 | return FALSE; | |
8229 | ||
eea6121a | 8230 | sect->size = note->descsz; |
07c6e936 | 8231 | sect->filepos = note->descpos; |
07c6e936 NC |
8232 | sect->alignment_power = 2; |
8233 | ||
f8843e87 AM |
8234 | /* This is the current thread. */ |
8235 | if (elf_tdata (abfd)->core_lwpid == tid) | |
d69f560c | 8236 | return elfcore_maybe_make_sect (abfd, base, sect); |
f8843e87 AM |
8237 | |
8238 | return TRUE; | |
07c6e936 NC |
8239 | } |
8240 | ||
8241 | #define BFD_QNT_CORE_INFO 7 | |
8242 | #define BFD_QNT_CORE_STATUS 8 | |
8243 | #define BFD_QNT_CORE_GREG 9 | |
8244 | #define BFD_QNT_CORE_FPREG 10 | |
8245 | ||
8246 | static bfd_boolean | |
217aa764 | 8247 | elfcore_grok_nto_note (bfd *abfd, Elf_Internal_Note *note) |
07c6e936 NC |
8248 | { |
8249 | /* Every GREG section has a STATUS section before it. Store the | |
811072d8 | 8250 | tid from the previous call to pass down to the next gregs |
07c6e936 | 8251 | function. */ |
d3fd4074 | 8252 | static long tid = 1; |
07c6e936 NC |
8253 | |
8254 | switch (note->type) | |
8255 | { | |
d69f560c KW |
8256 | case BFD_QNT_CORE_INFO: |
8257 | return elfcore_make_note_pseudosection (abfd, ".qnx_core_info", note); | |
8258 | case BFD_QNT_CORE_STATUS: | |
8259 | return elfcore_grok_nto_status (abfd, note, &tid); | |
8260 | case BFD_QNT_CORE_GREG: | |
8261 | return elfcore_grok_nto_regs (abfd, note, tid, ".reg"); | |
8262 | case BFD_QNT_CORE_FPREG: | |
8263 | return elfcore_grok_nto_regs (abfd, note, tid, ".reg2"); | |
8264 | default: | |
8265 | return TRUE; | |
07c6e936 NC |
8266 | } |
8267 | } | |
8268 | ||
b15fa79e AM |
8269 | static bfd_boolean |
8270 | elfcore_grok_spu_note (bfd *abfd, Elf_Internal_Note *note) | |
8271 | { | |
8272 | char *name; | |
8273 | asection *sect; | |
8274 | size_t len; | |
8275 | ||
8276 | /* Use note name as section name. */ | |
8277 | len = note->namesz; | |
8278 | name = bfd_alloc (abfd, len); | |
8279 | if (name == NULL) | |
8280 | return FALSE; | |
8281 | memcpy (name, note->namedata, len); | |
8282 | name[len - 1] = '\0'; | |
8283 | ||
8284 | sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); | |
8285 | if (sect == NULL) | |
8286 | return FALSE; | |
8287 | ||
8288 | sect->size = note->descsz; | |
8289 | sect->filepos = note->descpos; | |
8290 | sect->alignment_power = 1; | |
8291 | ||
8292 | return TRUE; | |
8293 | } | |
8294 | ||
7c76fa91 MS |
8295 | /* Function: elfcore_write_note |
8296 | ||
47d9a591 | 8297 | Inputs: |
a39f3346 | 8298 | buffer to hold note, and current size of buffer |
7c76fa91 MS |
8299 | name of note |
8300 | type of note | |
8301 | data for note | |
8302 | size of data for note | |
8303 | ||
a39f3346 AM |
8304 | Writes note to end of buffer. ELF64 notes are written exactly as |
8305 | for ELF32, despite the current (as of 2006) ELF gabi specifying | |
8306 | that they ought to have 8-byte namesz and descsz field, and have | |
8307 | 8-byte alignment. Other writers, eg. Linux kernel, do the same. | |
8308 | ||
7c76fa91 | 8309 | Return: |
a39f3346 | 8310 | Pointer to realloc'd buffer, *BUFSIZ updated. */ |
7c76fa91 MS |
8311 | |
8312 | char * | |
a39f3346 | 8313 | elfcore_write_note (bfd *abfd, |
217aa764 | 8314 | char *buf, |
a39f3346 | 8315 | int *bufsiz, |
217aa764 | 8316 | const char *name, |
a39f3346 | 8317 | int type, |
217aa764 | 8318 | const void *input, |
a39f3346 | 8319 | int size) |
7c76fa91 MS |
8320 | { |
8321 | Elf_External_Note *xnp; | |
d4c88bbb | 8322 | size_t namesz; |
d4c88bbb | 8323 | size_t newspace; |
a39f3346 | 8324 | char *dest; |
7c76fa91 | 8325 | |
d4c88bbb | 8326 | namesz = 0; |
d4c88bbb | 8327 | if (name != NULL) |
a39f3346 | 8328 | namesz = strlen (name) + 1; |
d4c88bbb | 8329 | |
a39f3346 | 8330 | newspace = 12 + ((namesz + 3) & -4) + ((size + 3) & -4); |
d4c88bbb | 8331 | |
a39f3346 | 8332 | buf = realloc (buf, *bufsiz + newspace); |
14b1c01e AM |
8333 | if (buf == NULL) |
8334 | return buf; | |
a39f3346 | 8335 | dest = buf + *bufsiz; |
7c76fa91 MS |
8336 | *bufsiz += newspace; |
8337 | xnp = (Elf_External_Note *) dest; | |
8338 | H_PUT_32 (abfd, namesz, xnp->namesz); | |
8339 | H_PUT_32 (abfd, size, xnp->descsz); | |
8340 | H_PUT_32 (abfd, type, xnp->type); | |
d4c88bbb AM |
8341 | dest = xnp->name; |
8342 | if (name != NULL) | |
8343 | { | |
8344 | memcpy (dest, name, namesz); | |
8345 | dest += namesz; | |
a39f3346 | 8346 | while (namesz & 3) |
d4c88bbb AM |
8347 | { |
8348 | *dest++ = '\0'; | |
a39f3346 | 8349 | ++namesz; |
d4c88bbb AM |
8350 | } |
8351 | } | |
8352 | memcpy (dest, input, size); | |
a39f3346 AM |
8353 | dest += size; |
8354 | while (size & 3) | |
8355 | { | |
8356 | *dest++ = '\0'; | |
8357 | ++size; | |
8358 | } | |
8359 | return buf; | |
7c76fa91 MS |
8360 | } |
8361 | ||
8362 | #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) | |
8363 | char * | |
217aa764 AM |
8364 | elfcore_write_prpsinfo (bfd *abfd, |
8365 | char *buf, | |
8366 | int *bufsiz, | |
8367 | const char *fname, | |
8368 | const char *psargs) | |
7c76fa91 | 8369 | { |
183e98be AM |
8370 | const char *note_name = "CORE"; |
8371 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
8372 | ||
8373 | if (bed->elf_backend_write_core_note != NULL) | |
8374 | { | |
8375 | char *ret; | |
8376 | ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz, | |
8377 | NT_PRPSINFO, fname, psargs); | |
8378 | if (ret != NULL) | |
8379 | return ret; | |
8380 | } | |
7c76fa91 | 8381 | |
183e98be AM |
8382 | #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T) |
8383 | if (bed->s->elfclass == ELFCLASS32) | |
8384 | { | |
8385 | #if defined (HAVE_PSINFO32_T) | |
8386 | psinfo32_t data; | |
8387 | int note_type = NT_PSINFO; | |
8388 | #else | |
8389 | prpsinfo32_t data; | |
8390 | int note_type = NT_PRPSINFO; | |
8391 | #endif | |
8392 | ||
8393 | memset (&data, 0, sizeof (data)); | |
8394 | strncpy (data.pr_fname, fname, sizeof (data.pr_fname)); | |
8395 | strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs)); | |
8396 | return elfcore_write_note (abfd, buf, bufsiz, | |
8397 | note_name, note_type, &data, sizeof (data)); | |
8398 | } | |
8399 | else | |
8400 | #endif | |
8401 | { | |
7c76fa91 | 8402 | #if defined (HAVE_PSINFO_T) |
183e98be AM |
8403 | psinfo_t data; |
8404 | int note_type = NT_PSINFO; | |
7c76fa91 | 8405 | #else |
183e98be AM |
8406 | prpsinfo_t data; |
8407 | int note_type = NT_PRPSINFO; | |
7c76fa91 MS |
8408 | #endif |
8409 | ||
183e98be AM |
8410 | memset (&data, 0, sizeof (data)); |
8411 | strncpy (data.pr_fname, fname, sizeof (data.pr_fname)); | |
8412 | strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs)); | |
8413 | return elfcore_write_note (abfd, buf, bufsiz, | |
8414 | note_name, note_type, &data, sizeof (data)); | |
8415 | } | |
7c76fa91 MS |
8416 | } |
8417 | #endif /* PSINFO_T or PRPSINFO_T */ | |
8418 | ||
8419 | #if defined (HAVE_PRSTATUS_T) | |
8420 | char * | |
217aa764 AM |
8421 | elfcore_write_prstatus (bfd *abfd, |
8422 | char *buf, | |
8423 | int *bufsiz, | |
8424 | long pid, | |
8425 | int cursig, | |
8426 | const void *gregs) | |
7c76fa91 | 8427 | { |
183e98be AM |
8428 | const char *note_name = "CORE"; |
8429 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
7c76fa91 | 8430 | |
183e98be AM |
8431 | if (bed->elf_backend_write_core_note != NULL) |
8432 | { | |
8433 | char *ret; | |
8434 | ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz, | |
8435 | NT_PRSTATUS, | |
8436 | pid, cursig, gregs); | |
8437 | if (ret != NULL) | |
8438 | return ret; | |
8439 | } | |
8440 | ||
8441 | #if defined (HAVE_PRSTATUS32_T) | |
8442 | if (bed->s->elfclass == ELFCLASS32) | |
8443 | { | |
8444 | prstatus32_t prstat; | |
8445 | ||
8446 | memset (&prstat, 0, sizeof (prstat)); | |
8447 | prstat.pr_pid = pid; | |
8448 | prstat.pr_cursig = cursig; | |
8449 | memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg)); | |
8450 | return elfcore_write_note (abfd, buf, bufsiz, note_name, | |
8451 | NT_PRSTATUS, &prstat, sizeof (prstat)); | |
8452 | } | |
8453 | else | |
8454 | #endif | |
8455 | { | |
8456 | prstatus_t prstat; | |
8457 | ||
8458 | memset (&prstat, 0, sizeof (prstat)); | |
8459 | prstat.pr_pid = pid; | |
8460 | prstat.pr_cursig = cursig; | |
8461 | memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg)); | |
8462 | return elfcore_write_note (abfd, buf, bufsiz, note_name, | |
8463 | NT_PRSTATUS, &prstat, sizeof (prstat)); | |
8464 | } | |
7c76fa91 MS |
8465 | } |
8466 | #endif /* HAVE_PRSTATUS_T */ | |
8467 | ||
51316059 MS |
8468 | #if defined (HAVE_LWPSTATUS_T) |
8469 | char * | |
217aa764 AM |
8470 | elfcore_write_lwpstatus (bfd *abfd, |
8471 | char *buf, | |
8472 | int *bufsiz, | |
8473 | long pid, | |
8474 | int cursig, | |
8475 | const void *gregs) | |
51316059 MS |
8476 | { |
8477 | lwpstatus_t lwpstat; | |
183e98be | 8478 | const char *note_name = "CORE"; |
51316059 MS |
8479 | |
8480 | memset (&lwpstat, 0, sizeof (lwpstat)); | |
8481 | lwpstat.pr_lwpid = pid >> 16; | |
8482 | lwpstat.pr_cursig = cursig; | |
8483 | #if defined (HAVE_LWPSTATUS_T_PR_REG) | |
8484 | memcpy (lwpstat.pr_reg, gregs, sizeof (lwpstat.pr_reg)); | |
8485 | #elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT) | |
8486 | #if !defined(gregs) | |
8487 | memcpy (lwpstat.pr_context.uc_mcontext.gregs, | |
8488 | gregs, sizeof (lwpstat.pr_context.uc_mcontext.gregs)); | |
8489 | #else | |
8490 | memcpy (lwpstat.pr_context.uc_mcontext.__gregs, | |
8491 | gregs, sizeof (lwpstat.pr_context.uc_mcontext.__gregs)); | |
8492 | #endif | |
8493 | #endif | |
47d9a591 | 8494 | return elfcore_write_note (abfd, buf, bufsiz, note_name, |
51316059 MS |
8495 | NT_LWPSTATUS, &lwpstat, sizeof (lwpstat)); |
8496 | } | |
8497 | #endif /* HAVE_LWPSTATUS_T */ | |
8498 | ||
7c76fa91 MS |
8499 | #if defined (HAVE_PSTATUS_T) |
8500 | char * | |
217aa764 AM |
8501 | elfcore_write_pstatus (bfd *abfd, |
8502 | char *buf, | |
8503 | int *bufsiz, | |
8504 | long pid, | |
6c10990d NC |
8505 | int cursig ATTRIBUTE_UNUSED, |
8506 | const void *gregs ATTRIBUTE_UNUSED) | |
7c76fa91 | 8507 | { |
183e98be AM |
8508 | const char *note_name = "CORE"; |
8509 | #if defined (HAVE_PSTATUS32_T) | |
8510 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
7c76fa91 | 8511 | |
183e98be AM |
8512 | if (bed->s->elfclass == ELFCLASS32) |
8513 | { | |
8514 | pstatus32_t pstat; | |
8515 | ||
8516 | memset (&pstat, 0, sizeof (pstat)); | |
8517 | pstat.pr_pid = pid & 0xffff; | |
8518 | buf = elfcore_write_note (abfd, buf, bufsiz, note_name, | |
8519 | NT_PSTATUS, &pstat, sizeof (pstat)); | |
8520 | return buf; | |
8521 | } | |
8522 | else | |
8523 | #endif | |
8524 | { | |
8525 | pstatus_t pstat; | |
8526 | ||
8527 | memset (&pstat, 0, sizeof (pstat)); | |
8528 | pstat.pr_pid = pid & 0xffff; | |
8529 | buf = elfcore_write_note (abfd, buf, bufsiz, note_name, | |
8530 | NT_PSTATUS, &pstat, sizeof (pstat)); | |
8531 | return buf; | |
8532 | } | |
7c76fa91 MS |
8533 | } |
8534 | #endif /* HAVE_PSTATUS_T */ | |
8535 | ||
8536 | char * | |
217aa764 AM |
8537 | elfcore_write_prfpreg (bfd *abfd, |
8538 | char *buf, | |
8539 | int *bufsiz, | |
8540 | const void *fpregs, | |
8541 | int size) | |
7c76fa91 | 8542 | { |
183e98be | 8543 | const char *note_name = "CORE"; |
47d9a591 | 8544 | return elfcore_write_note (abfd, buf, bufsiz, |
7c76fa91 MS |
8545 | note_name, NT_FPREGSET, fpregs, size); |
8546 | } | |
8547 | ||
8548 | char * | |
217aa764 AM |
8549 | elfcore_write_prxfpreg (bfd *abfd, |
8550 | char *buf, | |
8551 | int *bufsiz, | |
8552 | const void *xfpregs, | |
8553 | int size) | |
7c76fa91 MS |
8554 | { |
8555 | char *note_name = "LINUX"; | |
47d9a591 | 8556 | return elfcore_write_note (abfd, buf, bufsiz, |
7c76fa91 MS |
8557 | note_name, NT_PRXFPREG, xfpregs, size); |
8558 | } | |
8559 | ||
97753bd5 AM |
8560 | char * |
8561 | elfcore_write_ppc_vmx (bfd *abfd, | |
8562 | char *buf, | |
8563 | int *bufsiz, | |
8564 | const void *ppc_vmx, | |
8565 | int size) | |
8566 | { | |
8567 | char *note_name = "LINUX"; | |
8568 | return elfcore_write_note (abfd, buf, bufsiz, | |
8569 | note_name, NT_PPC_VMX, ppc_vmx, size); | |
8570 | } | |
8571 | ||
89eeb0bc LM |
8572 | char * |
8573 | elfcore_write_ppc_vsx (bfd *abfd, | |
8574 | char *buf, | |
8575 | int *bufsiz, | |
8576 | const void *ppc_vsx, | |
8577 | int size) | |
8578 | { | |
8579 | char *note_name = "LINUX"; | |
8580 | return elfcore_write_note (abfd, buf, bufsiz, | |
8581 | note_name, NT_PPC_VSX, ppc_vsx, size); | |
8582 | } | |
8583 | ||
bb864ac1 CES |
8584 | char * |
8585 | elfcore_write_register_note (bfd *abfd, | |
8586 | char *buf, | |
8587 | int *bufsiz, | |
8588 | const char *section, | |
8589 | const void *data, | |
8590 | int size) | |
8591 | { | |
8592 | if (strcmp (section, ".reg2") == 0) | |
8593 | return elfcore_write_prfpreg (abfd, buf, bufsiz, data, size); | |
8594 | if (strcmp (section, ".reg-xfp") == 0) | |
8595 | return elfcore_write_prxfpreg (abfd, buf, bufsiz, data, size); | |
8596 | if (strcmp (section, ".reg-ppc-vmx") == 0) | |
8597 | return elfcore_write_ppc_vmx (abfd, buf, bufsiz, data, size); | |
89eeb0bc LM |
8598 | if (strcmp (section, ".reg-ppc-vsx") == 0) |
8599 | return elfcore_write_ppc_vsx (abfd, buf, bufsiz, data, size); | |
bb864ac1 CES |
8600 | return NULL; |
8601 | } | |
8602 | ||
b34976b6 | 8603 | static bfd_boolean |
718175fa | 8604 | elf_parse_notes (bfd *abfd, char *buf, size_t size, file_ptr offset) |
252b5132 | 8605 | { |
c044fabd | 8606 | char *p; |
252b5132 | 8607 | |
252b5132 RH |
8608 | p = buf; |
8609 | while (p < buf + size) | |
8610 | { | |
c044fabd KH |
8611 | /* FIXME: bad alignment assumption. */ |
8612 | Elf_External_Note *xnp = (Elf_External_Note *) p; | |
252b5132 RH |
8613 | Elf_Internal_Note in; |
8614 | ||
baea7ef1 AM |
8615 | if (offsetof (Elf_External_Note, name) > buf - p + size) |
8616 | return FALSE; | |
8617 | ||
dc810e39 | 8618 | in.type = H_GET_32 (abfd, xnp->type); |
252b5132 | 8619 | |
dc810e39 | 8620 | in.namesz = H_GET_32 (abfd, xnp->namesz); |
252b5132 | 8621 | in.namedata = xnp->name; |
baea7ef1 AM |
8622 | if (in.namesz > buf - in.namedata + size) |
8623 | return FALSE; | |
252b5132 | 8624 | |
dc810e39 | 8625 | in.descsz = H_GET_32 (abfd, xnp->descsz); |
252b5132 RH |
8626 | in.descdata = in.namedata + BFD_ALIGN (in.namesz, 4); |
8627 | in.descpos = offset + (in.descdata - buf); | |
baea7ef1 AM |
8628 | if (in.descsz != 0 |
8629 | && (in.descdata >= buf + size | |
8630 | || in.descsz > buf - in.descdata + size)) | |
8631 | return FALSE; | |
252b5132 | 8632 | |
718175fa JK |
8633 | switch (bfd_get_format (abfd)) |
8634 | { | |
8635 | default: | |
8636 | return TRUE; | |
8637 | ||
8638 | case bfd_core: | |
8639 | if (CONST_STRNEQ (in.namedata, "NetBSD-CORE")) | |
8640 | { | |
8641 | if (! elfcore_grok_netbsd_note (abfd, &in)) | |
8642 | return FALSE; | |
8643 | } | |
67cc5033 MK |
8644 | else if (CONST_STRNEQ (in.namedata, "OpenBSD")) |
8645 | { | |
8646 | if (! elfcore_grok_openbsd_note (abfd, &in)) | |
8647 | return FALSE; | |
8648 | } | |
718175fa JK |
8649 | else if (CONST_STRNEQ (in.namedata, "QNX")) |
8650 | { | |
8651 | if (! elfcore_grok_nto_note (abfd, &in)) | |
8652 | return FALSE; | |
8653 | } | |
b15fa79e AM |
8654 | else if (CONST_STRNEQ (in.namedata, "SPU/")) |
8655 | { | |
8656 | if (! elfcore_grok_spu_note (abfd, &in)) | |
8657 | return FALSE; | |
8658 | } | |
718175fa JK |
8659 | else |
8660 | { | |
8661 | if (! elfcore_grok_note (abfd, &in)) | |
8662 | return FALSE; | |
8663 | } | |
8664 | break; | |
8665 | ||
8666 | case bfd_object: | |
8667 | if (in.namesz == sizeof "GNU" && strcmp (in.namedata, "GNU") == 0) | |
8668 | { | |
8669 | if (! elfobj_grok_gnu_note (abfd, &in)) | |
8670 | return FALSE; | |
8671 | } | |
8672 | break; | |
08a40648 | 8673 | } |
252b5132 RH |
8674 | |
8675 | p = in.descdata + BFD_ALIGN (in.descsz, 4); | |
8676 | } | |
8677 | ||
718175fa JK |
8678 | return TRUE; |
8679 | } | |
8680 | ||
8681 | static bfd_boolean | |
8682 | elf_read_notes (bfd *abfd, file_ptr offset, bfd_size_type size) | |
8683 | { | |
8684 | char *buf; | |
8685 | ||
8686 | if (size <= 0) | |
8687 | return TRUE; | |
8688 | ||
8689 | if (bfd_seek (abfd, offset, SEEK_SET) != 0) | |
8690 | return FALSE; | |
8691 | ||
8692 | buf = bfd_malloc (size); | |
8693 | if (buf == NULL) | |
8694 | return FALSE; | |
8695 | ||
8696 | if (bfd_bread (buf, size, abfd) != size | |
8697 | || !elf_parse_notes (abfd, buf, size, offset)) | |
8698 | { | |
8699 | free (buf); | |
8700 | return FALSE; | |
8701 | } | |
8702 | ||
252b5132 | 8703 | free (buf); |
b34976b6 | 8704 | return TRUE; |
252b5132 | 8705 | } |
98d8431c JB |
8706 | \f |
8707 | /* Providing external access to the ELF program header table. */ | |
8708 | ||
8709 | /* Return an upper bound on the number of bytes required to store a | |
8710 | copy of ABFD's program header table entries. Return -1 if an error | |
8711 | occurs; bfd_get_error will return an appropriate code. */ | |
c044fabd | 8712 | |
98d8431c | 8713 | long |
217aa764 | 8714 | bfd_get_elf_phdr_upper_bound (bfd *abfd) |
98d8431c JB |
8715 | { |
8716 | if (abfd->xvec->flavour != bfd_target_elf_flavour) | |
8717 | { | |
8718 | bfd_set_error (bfd_error_wrong_format); | |
8719 | return -1; | |
8720 | } | |
8721 | ||
936e320b | 8722 | return elf_elfheader (abfd)->e_phnum * sizeof (Elf_Internal_Phdr); |
98d8431c JB |
8723 | } |
8724 | ||
98d8431c JB |
8725 | /* Copy ABFD's program header table entries to *PHDRS. The entries |
8726 | will be stored as an array of Elf_Internal_Phdr structures, as | |
8727 | defined in include/elf/internal.h. To find out how large the | |
8728 | buffer needs to be, call bfd_get_elf_phdr_upper_bound. | |
8729 | ||
8730 | Return the number of program header table entries read, or -1 if an | |
8731 | error occurs; bfd_get_error will return an appropriate code. */ | |
c044fabd | 8732 | |
98d8431c | 8733 | int |
217aa764 | 8734 | bfd_get_elf_phdrs (bfd *abfd, void *phdrs) |
98d8431c JB |
8735 | { |
8736 | int num_phdrs; | |
8737 | ||
8738 | if (abfd->xvec->flavour != bfd_target_elf_flavour) | |
8739 | { | |
8740 | bfd_set_error (bfd_error_wrong_format); | |
8741 | return -1; | |
8742 | } | |
8743 | ||
8744 | num_phdrs = elf_elfheader (abfd)->e_phnum; | |
c044fabd | 8745 | memcpy (phdrs, elf_tdata (abfd)->phdr, |
98d8431c JB |
8746 | num_phdrs * sizeof (Elf_Internal_Phdr)); |
8747 | ||
8748 | return num_phdrs; | |
8749 | } | |
ae4221d7 | 8750 | |
db6751f2 | 8751 | enum elf_reloc_type_class |
217aa764 | 8752 | _bfd_elf_reloc_type_class (const Elf_Internal_Rela *rela ATTRIBUTE_UNUSED) |
db6751f2 JJ |
8753 | { |
8754 | return reloc_class_normal; | |
8755 | } | |
f8df10f4 | 8756 | |
47d9a591 | 8757 | /* For RELA architectures, return the relocation value for a |
f8df10f4 JJ |
8758 | relocation against a local symbol. */ |
8759 | ||
8760 | bfd_vma | |
217aa764 AM |
8761 | _bfd_elf_rela_local_sym (bfd *abfd, |
8762 | Elf_Internal_Sym *sym, | |
8517fae7 | 8763 | asection **psec, |
217aa764 | 8764 | Elf_Internal_Rela *rel) |
f8df10f4 | 8765 | { |
8517fae7 | 8766 | asection *sec = *psec; |
f8df10f4 JJ |
8767 | bfd_vma relocation; |
8768 | ||
8769 | relocation = (sec->output_section->vma | |
8770 | + sec->output_offset | |
8771 | + sym->st_value); | |
8772 | if ((sec->flags & SEC_MERGE) | |
c629eae0 | 8773 | && ELF_ST_TYPE (sym->st_info) == STT_SECTION |
68bfbfcc | 8774 | && sec->sec_info_type == ELF_INFO_TYPE_MERGE) |
f8df10f4 | 8775 | { |
f8df10f4 | 8776 | rel->r_addend = |
8517fae7 | 8777 | _bfd_merged_section_offset (abfd, psec, |
65765700 | 8778 | elf_section_data (sec)->sec_info, |
753731ee AM |
8779 | sym->st_value + rel->r_addend); |
8780 | if (sec != *psec) | |
8781 | { | |
8782 | /* If we have changed the section, and our original section is | |
8783 | marked with SEC_EXCLUDE, it means that the original | |
8784 | SEC_MERGE section has been completely subsumed in some | |
8785 | other SEC_MERGE section. In this case, we need to leave | |
8786 | some info around for --emit-relocs. */ | |
8787 | if ((sec->flags & SEC_EXCLUDE) != 0) | |
8788 | sec->kept_section = *psec; | |
8789 | sec = *psec; | |
8790 | } | |
8517fae7 AM |
8791 | rel->r_addend -= relocation; |
8792 | rel->r_addend += sec->output_section->vma + sec->output_offset; | |
f8df10f4 JJ |
8793 | } |
8794 | return relocation; | |
8795 | } | |
c629eae0 JJ |
8796 | |
8797 | bfd_vma | |
217aa764 AM |
8798 | _bfd_elf_rel_local_sym (bfd *abfd, |
8799 | Elf_Internal_Sym *sym, | |
8800 | asection **psec, | |
8801 | bfd_vma addend) | |
47d9a591 | 8802 | { |
c629eae0 JJ |
8803 | asection *sec = *psec; |
8804 | ||
68bfbfcc | 8805 | if (sec->sec_info_type != ELF_INFO_TYPE_MERGE) |
c629eae0 JJ |
8806 | return sym->st_value + addend; |
8807 | ||
8808 | return _bfd_merged_section_offset (abfd, psec, | |
65765700 | 8809 | elf_section_data (sec)->sec_info, |
753731ee | 8810 | sym->st_value + addend); |
c629eae0 JJ |
8811 | } |
8812 | ||
8813 | bfd_vma | |
217aa764 | 8814 | _bfd_elf_section_offset (bfd *abfd, |
92e4ec35 | 8815 | struct bfd_link_info *info, |
217aa764 AM |
8816 | asection *sec, |
8817 | bfd_vma offset) | |
c629eae0 | 8818 | { |
68bfbfcc | 8819 | switch (sec->sec_info_type) |
65765700 JJ |
8820 | { |
8821 | case ELF_INFO_TYPE_STABS: | |
eea6121a AM |
8822 | return _bfd_stab_section_offset (sec, elf_section_data (sec)->sec_info, |
8823 | offset); | |
65765700 | 8824 | case ELF_INFO_TYPE_EH_FRAME: |
92e4ec35 | 8825 | return _bfd_elf_eh_frame_section_offset (abfd, info, sec, offset); |
65765700 JJ |
8826 | default: |
8827 | return offset; | |
8828 | } | |
c629eae0 | 8829 | } |
3333a7c3 RM |
8830 | \f |
8831 | /* Create a new BFD as if by bfd_openr. Rather than opening a file, | |
8832 | reconstruct an ELF file by reading the segments out of remote memory | |
8833 | based on the ELF file header at EHDR_VMA and the ELF program headers it | |
8834 | points to. If not null, *LOADBASEP is filled in with the difference | |
8835 | between the VMAs from which the segments were read, and the VMAs the | |
8836 | file headers (and hence BFD's idea of each section's VMA) put them at. | |
8837 | ||
8838 | The function TARGET_READ_MEMORY is called to copy LEN bytes from the | |
8839 | remote memory at target address VMA into the local buffer at MYADDR; it | |
8840 | should return zero on success or an `errno' code on failure. TEMPL must | |
8841 | be a BFD for an ELF target with the word size and byte order found in | |
8842 | the remote memory. */ | |
8843 | ||
8844 | bfd * | |
217aa764 AM |
8845 | bfd_elf_bfd_from_remote_memory |
8846 | (bfd *templ, | |
8847 | bfd_vma ehdr_vma, | |
8848 | bfd_vma *loadbasep, | |
f075ee0c | 8849 | int (*target_read_memory) (bfd_vma, bfd_byte *, int)) |
3333a7c3 RM |
8850 | { |
8851 | return (*get_elf_backend_data (templ)->elf_backend_bfd_from_remote_memory) | |
8852 | (templ, ehdr_vma, loadbasep, target_read_memory); | |
8853 | } | |
4c45e5c9 JJ |
8854 | \f |
8855 | long | |
c9727e01 AM |
8856 | _bfd_elf_get_synthetic_symtab (bfd *abfd, |
8857 | long symcount ATTRIBUTE_UNUSED, | |
8858 | asymbol **syms ATTRIBUTE_UNUSED, | |
8615f3f2 | 8859 | long dynsymcount, |
c9727e01 AM |
8860 | asymbol **dynsyms, |
8861 | asymbol **ret) | |
4c45e5c9 JJ |
8862 | { |
8863 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
8864 | asection *relplt; | |
8865 | asymbol *s; | |
8866 | const char *relplt_name; | |
8867 | bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean); | |
8868 | arelent *p; | |
8869 | long count, i, n; | |
8870 | size_t size; | |
8871 | Elf_Internal_Shdr *hdr; | |
8872 | char *names; | |
8873 | asection *plt; | |
8874 | ||
8615f3f2 AM |
8875 | *ret = NULL; |
8876 | ||
90e3cdf2 JJ |
8877 | if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0) |
8878 | return 0; | |
8879 | ||
8615f3f2 AM |
8880 | if (dynsymcount <= 0) |
8881 | return 0; | |
8882 | ||
4c45e5c9 JJ |
8883 | if (!bed->plt_sym_val) |
8884 | return 0; | |
8885 | ||
8886 | relplt_name = bed->relplt_name; | |
8887 | if (relplt_name == NULL) | |
d35fd659 | 8888 | relplt_name = bed->rela_plts_and_copies_p ? ".rela.plt" : ".rel.plt"; |
4c45e5c9 JJ |
8889 | relplt = bfd_get_section_by_name (abfd, relplt_name); |
8890 | if (relplt == NULL) | |
8891 | return 0; | |
8892 | ||
8893 | hdr = &elf_section_data (relplt)->this_hdr; | |
8894 | if (hdr->sh_link != elf_dynsymtab (abfd) | |
8895 | || (hdr->sh_type != SHT_REL && hdr->sh_type != SHT_RELA)) | |
8896 | return 0; | |
8897 | ||
8898 | plt = bfd_get_section_by_name (abfd, ".plt"); | |
8899 | if (plt == NULL) | |
8900 | return 0; | |
8901 | ||
8902 | slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table; | |
c9727e01 | 8903 | if (! (*slurp_relocs) (abfd, relplt, dynsyms, TRUE)) |
4c45e5c9 JJ |
8904 | return -1; |
8905 | ||
eea6121a | 8906 | count = relplt->size / hdr->sh_entsize; |
4c45e5c9 JJ |
8907 | size = count * sizeof (asymbol); |
8908 | p = relplt->relocation; | |
cb53bf42 | 8909 | for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel) |
041de40d AM |
8910 | { |
8911 | size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt"); | |
8912 | if (p->addend != 0) | |
8913 | { | |
8914 | #ifdef BFD64 | |
8915 | size += sizeof ("+0x") - 1 + 8 + 8 * (bed->s->elfclass == ELFCLASS64); | |
8916 | #else | |
8917 | size += sizeof ("+0x") - 1 + 8; | |
8918 | #endif | |
8919 | } | |
8920 | } | |
4c45e5c9 JJ |
8921 | |
8922 | s = *ret = bfd_malloc (size); | |
8923 | if (s == NULL) | |
8924 | return -1; | |
8925 | ||
8926 | names = (char *) (s + count); | |
8927 | p = relplt->relocation; | |
8928 | n = 0; | |
cb53bf42 | 8929 | for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel) |
4c45e5c9 JJ |
8930 | { |
8931 | size_t len; | |
8932 | bfd_vma addr; | |
8933 | ||
8934 | addr = bed->plt_sym_val (i, plt, p); | |
8935 | if (addr == (bfd_vma) -1) | |
8936 | continue; | |
8937 | ||
8938 | *s = **p->sym_ptr_ptr; | |
65a7a66f AM |
8939 | /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since |
8940 | we are defining a symbol, ensure one of them is set. */ | |
8941 | if ((s->flags & BSF_LOCAL) == 0) | |
8942 | s->flags |= BSF_GLOBAL; | |
6ba2a415 | 8943 | s->flags |= BSF_SYNTHETIC; |
4c45e5c9 JJ |
8944 | s->section = plt; |
8945 | s->value = addr - plt->vma; | |
8946 | s->name = names; | |
8f39ba8e | 8947 | s->udata.p = NULL; |
4c45e5c9 JJ |
8948 | len = strlen ((*p->sym_ptr_ptr)->name); |
8949 | memcpy (names, (*p->sym_ptr_ptr)->name, len); | |
8950 | names += len; | |
041de40d AM |
8951 | if (p->addend != 0) |
8952 | { | |
1d770845 L |
8953 | char buf[30], *a; |
8954 | int len; | |
041de40d AM |
8955 | memcpy (names, "+0x", sizeof ("+0x") - 1); |
8956 | names += sizeof ("+0x") - 1; | |
1d770845 L |
8957 | bfd_sprintf_vma (abfd, buf, p->addend); |
8958 | for (a = buf; *a == '0'; ++a) | |
8959 | ; | |
8960 | len = strlen (a); | |
8961 | memcpy (names, a, len); | |
8962 | names += len; | |
041de40d | 8963 | } |
4c45e5c9 JJ |
8964 | memcpy (names, "@plt", sizeof ("@plt")); |
8965 | names += sizeof ("@plt"); | |
8f39ba8e | 8966 | ++s, ++n; |
4c45e5c9 JJ |
8967 | } |
8968 | ||
8969 | return n; | |
8970 | } | |
3d7f7666 | 8971 | |
3b22753a L |
8972 | /* It is only used by x86-64 so far. */ |
8973 | asection _bfd_elf_large_com_section | |
8974 | = BFD_FAKE_SECTION (_bfd_elf_large_com_section, | |
f592407e | 8975 | SEC_IS_COMMON, NULL, "LARGE_COMMON", 0); |
ecca9871 | 8976 | |
d1036acb L |
8977 | void |
8978 | _bfd_elf_set_osabi (bfd * abfd, | |
8979 | struct bfd_link_info * link_info ATTRIBUTE_UNUSED) | |
8980 | { | |
8981 | Elf_Internal_Ehdr * i_ehdrp; /* ELF file header, internal form. */ | |
8982 | ||
8983 | i_ehdrp = elf_elfheader (abfd); | |
8984 | ||
8985 | i_ehdrp->e_ident[EI_OSABI] = get_elf_backend_data (abfd)->elf_osabi; | |
d8045f23 NC |
8986 | |
8987 | /* To make things simpler for the loader on Linux systems we set the | |
8988 | osabi field to ELFOSABI_LINUX if the binary contains symbols of | |
8989 | the STT_GNU_IFUNC type. */ | |
8990 | if (i_ehdrp->e_ident[EI_OSABI] == ELFOSABI_NONE | |
8991 | && elf_tdata (abfd)->has_ifunc_symbols) | |
8992 | i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_LINUX; | |
d1036acb | 8993 | } |
fcb93ecf PB |
8994 | |
8995 | ||
8996 | /* Return TRUE for ELF symbol types that represent functions. | |
8997 | This is the default version of this function, which is sufficient for | |
d8045f23 | 8998 | most targets. It returns true if TYPE is STT_FUNC or STT_GNU_IFUNC. */ |
fcb93ecf PB |
8999 | |
9000 | bfd_boolean | |
9001 | _bfd_elf_is_function_type (unsigned int type) | |
9002 | { | |
d8045f23 NC |
9003 | return (type == STT_FUNC |
9004 | || type == STT_GNU_IFUNC); | |
fcb93ecf | 9005 | } |