Commit | Line | Data |
---|---|---|
252b5132 | 1 | /* ELF executable support for BFD. |
340b6d91 AC |
2 | |
3 | Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, | |
b7c368d0 | 4 | 2002, 2003, 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc. |
252b5132 | 5 | |
5e8d7549 | 6 | This file is part of BFD, the Binary File Descriptor library. |
252b5132 | 7 | |
5e8d7549 NC |
8 | This program is free software; you can redistribute it and/or modify |
9 | it under the terms of the GNU General Public License as published by | |
cd123cb7 | 10 | the Free Software Foundation; either version 3 of the License, or |
5e8d7549 | 11 | (at your option) any later version. |
252b5132 | 12 | |
5e8d7549 NC |
13 | This program is distributed in the hope that it will be useful, |
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | GNU General Public License for more details. | |
252b5132 | 17 | |
5e8d7549 | 18 | You should have received a copy of the GNU General Public License |
b34976b6 | 19 | along with this program; if not, write to the Free Software |
cd123cb7 NC |
20 | Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, |
21 | MA 02110-1301, USA. */ | |
22 | ||
252b5132 | 23 | |
1b74d094 BW |
24 | /* |
25 | SECTION | |
252b5132 RH |
26 | ELF backends |
27 | ||
28 | BFD support for ELF formats is being worked on. | |
29 | Currently, the best supported back ends are for sparc and i386 | |
30 | (running svr4 or Solaris 2). | |
31 | ||
32 | Documentation of the internals of the support code still needs | |
33 | to be written. The code is changing quickly enough that we | |
661a3fd4 | 34 | haven't bothered yet. */ |
252b5132 | 35 | |
7ee38065 MS |
36 | /* For sparc64-cross-sparc32. */ |
37 | #define _SYSCALL32 | |
252b5132 | 38 | #include "sysdep.h" |
3db64b00 | 39 | #include "bfd.h" |
252b5132 RH |
40 | #include "bfdlink.h" |
41 | #include "libbfd.h" | |
42 | #define ARCH_SIZE 0 | |
43 | #include "elf-bfd.h" | |
e0e8c97f | 44 | #include "libiberty.h" |
ff59fc36 | 45 | #include "safe-ctype.h" |
252b5132 | 46 | |
217aa764 | 47 | static int elf_sort_sections (const void *, const void *); |
c84fca4d | 48 | static bfd_boolean assign_file_positions_except_relocs (bfd *, struct bfd_link_info *); |
217aa764 AM |
49 | static bfd_boolean prep_headers (bfd *); |
50 | static bfd_boolean swap_out_syms (bfd *, struct bfd_strtab_hash **, int) ; | |
718175fa JK |
51 | static bfd_boolean elf_read_notes (bfd *, file_ptr, bfd_size_type) ; |
52 | static bfd_boolean elf_parse_notes (bfd *abfd, char *buf, size_t size, | |
53 | file_ptr offset); | |
50b2bdb7 | 54 | |
252b5132 RH |
55 | /* Swap version information in and out. The version information is |
56 | currently size independent. If that ever changes, this code will | |
57 | need to move into elfcode.h. */ | |
58 | ||
59 | /* Swap in a Verdef structure. */ | |
60 | ||
61 | void | |
217aa764 AM |
62 | _bfd_elf_swap_verdef_in (bfd *abfd, |
63 | const Elf_External_Verdef *src, | |
64 | Elf_Internal_Verdef *dst) | |
252b5132 | 65 | { |
dc810e39 AM |
66 | dst->vd_version = H_GET_16 (abfd, src->vd_version); |
67 | dst->vd_flags = H_GET_16 (abfd, src->vd_flags); | |
68 | dst->vd_ndx = H_GET_16 (abfd, src->vd_ndx); | |
69 | dst->vd_cnt = H_GET_16 (abfd, src->vd_cnt); | |
70 | dst->vd_hash = H_GET_32 (abfd, src->vd_hash); | |
71 | dst->vd_aux = H_GET_32 (abfd, src->vd_aux); | |
72 | dst->vd_next = H_GET_32 (abfd, src->vd_next); | |
252b5132 RH |
73 | } |
74 | ||
75 | /* Swap out a Verdef structure. */ | |
76 | ||
77 | void | |
217aa764 AM |
78 | _bfd_elf_swap_verdef_out (bfd *abfd, |
79 | const Elf_Internal_Verdef *src, | |
80 | Elf_External_Verdef *dst) | |
252b5132 | 81 | { |
dc810e39 AM |
82 | H_PUT_16 (abfd, src->vd_version, dst->vd_version); |
83 | H_PUT_16 (abfd, src->vd_flags, dst->vd_flags); | |
84 | H_PUT_16 (abfd, src->vd_ndx, dst->vd_ndx); | |
85 | H_PUT_16 (abfd, src->vd_cnt, dst->vd_cnt); | |
86 | H_PUT_32 (abfd, src->vd_hash, dst->vd_hash); | |
87 | H_PUT_32 (abfd, src->vd_aux, dst->vd_aux); | |
88 | H_PUT_32 (abfd, src->vd_next, dst->vd_next); | |
252b5132 RH |
89 | } |
90 | ||
91 | /* Swap in a Verdaux structure. */ | |
92 | ||
93 | void | |
217aa764 AM |
94 | _bfd_elf_swap_verdaux_in (bfd *abfd, |
95 | const Elf_External_Verdaux *src, | |
96 | Elf_Internal_Verdaux *dst) | |
252b5132 | 97 | { |
dc810e39 AM |
98 | dst->vda_name = H_GET_32 (abfd, src->vda_name); |
99 | dst->vda_next = H_GET_32 (abfd, src->vda_next); | |
252b5132 RH |
100 | } |
101 | ||
102 | /* Swap out a Verdaux structure. */ | |
103 | ||
104 | void | |
217aa764 AM |
105 | _bfd_elf_swap_verdaux_out (bfd *abfd, |
106 | const Elf_Internal_Verdaux *src, | |
107 | Elf_External_Verdaux *dst) | |
252b5132 | 108 | { |
dc810e39 AM |
109 | H_PUT_32 (abfd, src->vda_name, dst->vda_name); |
110 | H_PUT_32 (abfd, src->vda_next, dst->vda_next); | |
252b5132 RH |
111 | } |
112 | ||
113 | /* Swap in a Verneed structure. */ | |
114 | ||
115 | void | |
217aa764 AM |
116 | _bfd_elf_swap_verneed_in (bfd *abfd, |
117 | const Elf_External_Verneed *src, | |
118 | Elf_Internal_Verneed *dst) | |
252b5132 | 119 | { |
dc810e39 AM |
120 | dst->vn_version = H_GET_16 (abfd, src->vn_version); |
121 | dst->vn_cnt = H_GET_16 (abfd, src->vn_cnt); | |
122 | dst->vn_file = H_GET_32 (abfd, src->vn_file); | |
123 | dst->vn_aux = H_GET_32 (abfd, src->vn_aux); | |
124 | dst->vn_next = H_GET_32 (abfd, src->vn_next); | |
252b5132 RH |
125 | } |
126 | ||
127 | /* Swap out a Verneed structure. */ | |
128 | ||
129 | void | |
217aa764 AM |
130 | _bfd_elf_swap_verneed_out (bfd *abfd, |
131 | const Elf_Internal_Verneed *src, | |
132 | Elf_External_Verneed *dst) | |
252b5132 | 133 | { |
dc810e39 AM |
134 | H_PUT_16 (abfd, src->vn_version, dst->vn_version); |
135 | H_PUT_16 (abfd, src->vn_cnt, dst->vn_cnt); | |
136 | H_PUT_32 (abfd, src->vn_file, dst->vn_file); | |
137 | H_PUT_32 (abfd, src->vn_aux, dst->vn_aux); | |
138 | H_PUT_32 (abfd, src->vn_next, dst->vn_next); | |
252b5132 RH |
139 | } |
140 | ||
141 | /* Swap in a Vernaux structure. */ | |
142 | ||
143 | void | |
217aa764 AM |
144 | _bfd_elf_swap_vernaux_in (bfd *abfd, |
145 | const Elf_External_Vernaux *src, | |
146 | Elf_Internal_Vernaux *dst) | |
252b5132 | 147 | { |
dc810e39 AM |
148 | dst->vna_hash = H_GET_32 (abfd, src->vna_hash); |
149 | dst->vna_flags = H_GET_16 (abfd, src->vna_flags); | |
150 | dst->vna_other = H_GET_16 (abfd, src->vna_other); | |
151 | dst->vna_name = H_GET_32 (abfd, src->vna_name); | |
152 | dst->vna_next = H_GET_32 (abfd, src->vna_next); | |
252b5132 RH |
153 | } |
154 | ||
155 | /* Swap out a Vernaux structure. */ | |
156 | ||
157 | void | |
217aa764 AM |
158 | _bfd_elf_swap_vernaux_out (bfd *abfd, |
159 | const Elf_Internal_Vernaux *src, | |
160 | Elf_External_Vernaux *dst) | |
252b5132 | 161 | { |
dc810e39 AM |
162 | H_PUT_32 (abfd, src->vna_hash, dst->vna_hash); |
163 | H_PUT_16 (abfd, src->vna_flags, dst->vna_flags); | |
164 | H_PUT_16 (abfd, src->vna_other, dst->vna_other); | |
165 | H_PUT_32 (abfd, src->vna_name, dst->vna_name); | |
166 | H_PUT_32 (abfd, src->vna_next, dst->vna_next); | |
252b5132 RH |
167 | } |
168 | ||
169 | /* Swap in a Versym structure. */ | |
170 | ||
171 | void | |
217aa764 AM |
172 | _bfd_elf_swap_versym_in (bfd *abfd, |
173 | const Elf_External_Versym *src, | |
174 | Elf_Internal_Versym *dst) | |
252b5132 | 175 | { |
dc810e39 | 176 | dst->vs_vers = H_GET_16 (abfd, src->vs_vers); |
252b5132 RH |
177 | } |
178 | ||
179 | /* Swap out a Versym structure. */ | |
180 | ||
181 | void | |
217aa764 AM |
182 | _bfd_elf_swap_versym_out (bfd *abfd, |
183 | const Elf_Internal_Versym *src, | |
184 | Elf_External_Versym *dst) | |
252b5132 | 185 | { |
dc810e39 | 186 | H_PUT_16 (abfd, src->vs_vers, dst->vs_vers); |
252b5132 RH |
187 | } |
188 | ||
189 | /* Standard ELF hash function. Do not change this function; you will | |
190 | cause invalid hash tables to be generated. */ | |
3a99b017 | 191 | |
252b5132 | 192 | unsigned long |
217aa764 | 193 | bfd_elf_hash (const char *namearg) |
252b5132 | 194 | { |
3a99b017 | 195 | const unsigned char *name = (const unsigned char *) namearg; |
252b5132 RH |
196 | unsigned long h = 0; |
197 | unsigned long g; | |
198 | int ch; | |
199 | ||
200 | while ((ch = *name++) != '\0') | |
201 | { | |
202 | h = (h << 4) + ch; | |
203 | if ((g = (h & 0xf0000000)) != 0) | |
204 | { | |
205 | h ^= g >> 24; | |
206 | /* The ELF ABI says `h &= ~g', but this is equivalent in | |
207 | this case and on some machines one insn instead of two. */ | |
208 | h ^= g; | |
209 | } | |
210 | } | |
32dfa85d | 211 | return h & 0xffffffff; |
252b5132 RH |
212 | } |
213 | ||
fdc90cb4 JJ |
214 | /* DT_GNU_HASH hash function. Do not change this function; you will |
215 | cause invalid hash tables to be generated. */ | |
216 | ||
217 | unsigned long | |
218 | bfd_elf_gnu_hash (const char *namearg) | |
219 | { | |
220 | const unsigned char *name = (const unsigned char *) namearg; | |
221 | unsigned long h = 5381; | |
222 | unsigned char ch; | |
223 | ||
224 | while ((ch = *name++) != '\0') | |
225 | h = (h << 5) + h + ch; | |
226 | return h & 0xffffffff; | |
227 | } | |
228 | ||
0c8d6e5c AM |
229 | /* Create a tdata field OBJECT_SIZE bytes in length, zeroed out and with |
230 | the object_id field of an elf_obj_tdata field set to OBJECT_ID. */ | |
b34976b6 | 231 | bfd_boolean |
0c8d6e5c | 232 | bfd_elf_allocate_object (bfd *abfd, |
0ffa91dd NC |
233 | size_t object_size, |
234 | enum elf_object_id object_id) | |
252b5132 | 235 | { |
0ffa91dd NC |
236 | BFD_ASSERT (object_size >= sizeof (struct elf_obj_tdata)); |
237 | abfd->tdata.any = bfd_zalloc (abfd, object_size); | |
238 | if (abfd->tdata.any == NULL) | |
239 | return FALSE; | |
252b5132 | 240 | |
0ffa91dd NC |
241 | elf_object_id (abfd) = object_id; |
242 | elf_program_header_size (abfd) = (bfd_size_type) -1; | |
b34976b6 | 243 | return TRUE; |
252b5132 RH |
244 | } |
245 | ||
0ffa91dd NC |
246 | |
247 | bfd_boolean | |
248 | bfd_elf_make_generic_object (bfd *abfd) | |
249 | { | |
250 | return bfd_elf_allocate_object (abfd, sizeof (struct elf_obj_tdata), | |
251 | GENERIC_ELF_TDATA); | |
252 | } | |
253 | ||
b34976b6 | 254 | bfd_boolean |
217aa764 | 255 | bfd_elf_mkcorefile (bfd *abfd) |
252b5132 | 256 | { |
c044fabd | 257 | /* I think this can be done just like an object file. */ |
0ffa91dd | 258 | return bfd_elf_make_generic_object (abfd); |
252b5132 RH |
259 | } |
260 | ||
261 | char * | |
217aa764 | 262 | bfd_elf_get_str_section (bfd *abfd, unsigned int shindex) |
252b5132 RH |
263 | { |
264 | Elf_Internal_Shdr **i_shdrp; | |
f075ee0c | 265 | bfd_byte *shstrtab = NULL; |
dc810e39 AM |
266 | file_ptr offset; |
267 | bfd_size_type shstrtabsize; | |
252b5132 RH |
268 | |
269 | i_shdrp = elf_elfsections (abfd); | |
74f2e02b AM |
270 | if (i_shdrp == 0 |
271 | || shindex >= elf_numsections (abfd) | |
272 | || i_shdrp[shindex] == 0) | |
f075ee0c | 273 | return NULL; |
252b5132 | 274 | |
f075ee0c | 275 | shstrtab = i_shdrp[shindex]->contents; |
252b5132 RH |
276 | if (shstrtab == NULL) |
277 | { | |
c044fabd | 278 | /* No cached one, attempt to read, and cache what we read. */ |
252b5132 RH |
279 | offset = i_shdrp[shindex]->sh_offset; |
280 | shstrtabsize = i_shdrp[shindex]->sh_size; | |
c6c60d09 JJ |
281 | |
282 | /* Allocate and clear an extra byte at the end, to prevent crashes | |
283 | in case the string table is not terminated. */ | |
3471d59d | 284 | if (shstrtabsize + 1 <= 1 |
c6c60d09 JJ |
285 | || (shstrtab = bfd_alloc (abfd, shstrtabsize + 1)) == NULL |
286 | || bfd_seek (abfd, offset, SEEK_SET) != 0) | |
287 | shstrtab = NULL; | |
288 | else if (bfd_bread (shstrtab, shstrtabsize, abfd) != shstrtabsize) | |
289 | { | |
290 | if (bfd_get_error () != bfd_error_system_call) | |
291 | bfd_set_error (bfd_error_file_truncated); | |
292 | shstrtab = NULL; | |
3471d59d CC |
293 | /* Once we've failed to read it, make sure we don't keep |
294 | trying. Otherwise, we'll keep allocating space for | |
295 | the string table over and over. */ | |
296 | i_shdrp[shindex]->sh_size = 0; | |
c6c60d09 JJ |
297 | } |
298 | else | |
299 | shstrtab[shstrtabsize] = '\0'; | |
217aa764 | 300 | i_shdrp[shindex]->contents = shstrtab; |
252b5132 | 301 | } |
f075ee0c | 302 | return (char *) shstrtab; |
252b5132 RH |
303 | } |
304 | ||
305 | char * | |
217aa764 AM |
306 | bfd_elf_string_from_elf_section (bfd *abfd, |
307 | unsigned int shindex, | |
308 | unsigned int strindex) | |
252b5132 RH |
309 | { |
310 | Elf_Internal_Shdr *hdr; | |
311 | ||
312 | if (strindex == 0) | |
313 | return ""; | |
314 | ||
74f2e02b AM |
315 | if (elf_elfsections (abfd) == NULL || shindex >= elf_numsections (abfd)) |
316 | return NULL; | |
317 | ||
252b5132 RH |
318 | hdr = elf_elfsections (abfd)[shindex]; |
319 | ||
320 | if (hdr->contents == NULL | |
321 | && bfd_elf_get_str_section (abfd, shindex) == NULL) | |
322 | return NULL; | |
323 | ||
324 | if (strindex >= hdr->sh_size) | |
325 | { | |
1b3a8575 | 326 | unsigned int shstrndx = elf_elfheader(abfd)->e_shstrndx; |
252b5132 | 327 | (*_bfd_error_handler) |
d003868e AM |
328 | (_("%B: invalid string offset %u >= %lu for section `%s'"), |
329 | abfd, strindex, (unsigned long) hdr->sh_size, | |
1b3a8575 | 330 | (shindex == shstrndx && strindex == hdr->sh_name |
252b5132 | 331 | ? ".shstrtab" |
1b3a8575 | 332 | : bfd_elf_string_from_elf_section (abfd, shstrndx, hdr->sh_name))); |
252b5132 RH |
333 | return ""; |
334 | } | |
335 | ||
336 | return ((char *) hdr->contents) + strindex; | |
337 | } | |
338 | ||
6cdc0ccc AM |
339 | /* Read and convert symbols to internal format. |
340 | SYMCOUNT specifies the number of symbols to read, starting from | |
341 | symbol SYMOFFSET. If any of INTSYM_BUF, EXTSYM_BUF or EXTSHNDX_BUF | |
342 | are non-NULL, they are used to store the internal symbols, external | |
b7c368d0 NC |
343 | symbols, and symbol section index extensions, respectively. |
344 | Returns a pointer to the internal symbol buffer (malloced if necessary) | |
345 | or NULL if there were no symbols or some kind of problem. */ | |
6cdc0ccc AM |
346 | |
347 | Elf_Internal_Sym * | |
217aa764 AM |
348 | bfd_elf_get_elf_syms (bfd *ibfd, |
349 | Elf_Internal_Shdr *symtab_hdr, | |
350 | size_t symcount, | |
351 | size_t symoffset, | |
352 | Elf_Internal_Sym *intsym_buf, | |
353 | void *extsym_buf, | |
354 | Elf_External_Sym_Shndx *extshndx_buf) | |
6cdc0ccc AM |
355 | { |
356 | Elf_Internal_Shdr *shndx_hdr; | |
217aa764 | 357 | void *alloc_ext; |
df622259 | 358 | const bfd_byte *esym; |
6cdc0ccc AM |
359 | Elf_External_Sym_Shndx *alloc_extshndx; |
360 | Elf_External_Sym_Shndx *shndx; | |
361 | Elf_Internal_Sym *isym; | |
362 | Elf_Internal_Sym *isymend; | |
9c5bfbb7 | 363 | const struct elf_backend_data *bed; |
6cdc0ccc AM |
364 | size_t extsym_size; |
365 | bfd_size_type amt; | |
366 | file_ptr pos; | |
367 | ||
e44a2c9c AM |
368 | if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour) |
369 | abort (); | |
370 | ||
6cdc0ccc AM |
371 | if (symcount == 0) |
372 | return intsym_buf; | |
373 | ||
374 | /* Normal syms might have section extension entries. */ | |
375 | shndx_hdr = NULL; | |
376 | if (symtab_hdr == &elf_tdata (ibfd)->symtab_hdr) | |
377 | shndx_hdr = &elf_tdata (ibfd)->symtab_shndx_hdr; | |
378 | ||
379 | /* Read the symbols. */ | |
380 | alloc_ext = NULL; | |
381 | alloc_extshndx = NULL; | |
382 | bed = get_elf_backend_data (ibfd); | |
383 | extsym_size = bed->s->sizeof_sym; | |
384 | amt = symcount * extsym_size; | |
385 | pos = symtab_hdr->sh_offset + symoffset * extsym_size; | |
386 | if (extsym_buf == NULL) | |
387 | { | |
d0fb9a8d | 388 | alloc_ext = bfd_malloc2 (symcount, extsym_size); |
6cdc0ccc AM |
389 | extsym_buf = alloc_ext; |
390 | } | |
391 | if (extsym_buf == NULL | |
392 | || bfd_seek (ibfd, pos, SEEK_SET) != 0 | |
393 | || bfd_bread (extsym_buf, amt, ibfd) != amt) | |
394 | { | |
395 | intsym_buf = NULL; | |
396 | goto out; | |
397 | } | |
398 | ||
399 | if (shndx_hdr == NULL || shndx_hdr->sh_size == 0) | |
400 | extshndx_buf = NULL; | |
401 | else | |
402 | { | |
403 | amt = symcount * sizeof (Elf_External_Sym_Shndx); | |
404 | pos = shndx_hdr->sh_offset + symoffset * sizeof (Elf_External_Sym_Shndx); | |
405 | if (extshndx_buf == NULL) | |
406 | { | |
d0fb9a8d JJ |
407 | alloc_extshndx = bfd_malloc2 (symcount, |
408 | sizeof (Elf_External_Sym_Shndx)); | |
6cdc0ccc AM |
409 | extshndx_buf = alloc_extshndx; |
410 | } | |
411 | if (extshndx_buf == NULL | |
412 | || bfd_seek (ibfd, pos, SEEK_SET) != 0 | |
413 | || bfd_bread (extshndx_buf, amt, ibfd) != amt) | |
414 | { | |
415 | intsym_buf = NULL; | |
416 | goto out; | |
417 | } | |
418 | } | |
419 | ||
420 | if (intsym_buf == NULL) | |
421 | { | |
d0fb9a8d | 422 | intsym_buf = bfd_malloc2 (symcount, sizeof (Elf_Internal_Sym)); |
6cdc0ccc AM |
423 | if (intsym_buf == NULL) |
424 | goto out; | |
425 | } | |
426 | ||
427 | /* Convert the symbols to internal form. */ | |
428 | isymend = intsym_buf + symcount; | |
429 | for (esym = extsym_buf, isym = intsym_buf, shndx = extshndx_buf; | |
430 | isym < isymend; | |
431 | esym += extsym_size, isym++, shndx = shndx != NULL ? shndx + 1 : NULL) | |
8384fb8f AM |
432 | if (!(*bed->s->swap_symbol_in) (ibfd, esym, shndx, isym)) |
433 | { | |
434 | symoffset += (esym - (bfd_byte *) extsym_buf) / extsym_size; | |
435 | (*_bfd_error_handler) (_("%B symbol number %lu references " | |
436 | "nonexistent SHT_SYMTAB_SHNDX section"), | |
437 | ibfd, (unsigned long) symoffset); | |
438 | intsym_buf = NULL; | |
439 | goto out; | |
440 | } | |
6cdc0ccc AM |
441 | |
442 | out: | |
443 | if (alloc_ext != NULL) | |
444 | free (alloc_ext); | |
445 | if (alloc_extshndx != NULL) | |
446 | free (alloc_extshndx); | |
447 | ||
448 | return intsym_buf; | |
449 | } | |
450 | ||
5cab59f6 AM |
451 | /* Look up a symbol name. */ |
452 | const char * | |
be8dd2ca AM |
453 | bfd_elf_sym_name (bfd *abfd, |
454 | Elf_Internal_Shdr *symtab_hdr, | |
26c61ae5 L |
455 | Elf_Internal_Sym *isym, |
456 | asection *sym_sec) | |
5cab59f6 | 457 | { |
26c61ae5 | 458 | const char *name; |
5cab59f6 | 459 | unsigned int iname = isym->st_name; |
be8dd2ca | 460 | unsigned int shindex = symtab_hdr->sh_link; |
26c61ae5 | 461 | |
138f35cc JJ |
462 | if (iname == 0 && ELF_ST_TYPE (isym->st_info) == STT_SECTION |
463 | /* Check for a bogus st_shndx to avoid crashing. */ | |
4fbb74a6 | 464 | && isym->st_shndx < elf_numsections (abfd)) |
5cab59f6 AM |
465 | { |
466 | iname = elf_elfsections (abfd)[isym->st_shndx]->sh_name; | |
467 | shindex = elf_elfheader (abfd)->e_shstrndx; | |
468 | } | |
469 | ||
26c61ae5 L |
470 | name = bfd_elf_string_from_elf_section (abfd, shindex, iname); |
471 | if (name == NULL) | |
472 | name = "(null)"; | |
473 | else if (sym_sec && *name == '\0') | |
474 | name = bfd_section_name (abfd, sym_sec); | |
475 | ||
476 | return name; | |
5cab59f6 AM |
477 | } |
478 | ||
dbb410c3 AM |
479 | /* Elf_Internal_Shdr->contents is an array of these for SHT_GROUP |
480 | sections. The first element is the flags, the rest are section | |
481 | pointers. */ | |
482 | ||
483 | typedef union elf_internal_group { | |
484 | Elf_Internal_Shdr *shdr; | |
485 | unsigned int flags; | |
486 | } Elf_Internal_Group; | |
487 | ||
b885599b AM |
488 | /* Return the name of the group signature symbol. Why isn't the |
489 | signature just a string? */ | |
490 | ||
491 | static const char * | |
217aa764 | 492 | group_signature (bfd *abfd, Elf_Internal_Shdr *ghdr) |
b885599b | 493 | { |
9dce4196 | 494 | Elf_Internal_Shdr *hdr; |
9dce4196 AM |
495 | unsigned char esym[sizeof (Elf64_External_Sym)]; |
496 | Elf_External_Sym_Shndx eshndx; | |
497 | Elf_Internal_Sym isym; | |
b885599b | 498 | |
13792e9d L |
499 | /* First we need to ensure the symbol table is available. Make sure |
500 | that it is a symbol table section. */ | |
4fbb74a6 AM |
501 | if (ghdr->sh_link >= elf_numsections (abfd)) |
502 | return NULL; | |
13792e9d L |
503 | hdr = elf_elfsections (abfd) [ghdr->sh_link]; |
504 | if (hdr->sh_type != SHT_SYMTAB | |
505 | || ! bfd_section_from_shdr (abfd, ghdr->sh_link)) | |
b885599b AM |
506 | return NULL; |
507 | ||
9dce4196 AM |
508 | /* Go read the symbol. */ |
509 | hdr = &elf_tdata (abfd)->symtab_hdr; | |
6cdc0ccc AM |
510 | if (bfd_elf_get_elf_syms (abfd, hdr, 1, ghdr->sh_info, |
511 | &isym, esym, &eshndx) == NULL) | |
b885599b | 512 | return NULL; |
9dce4196 | 513 | |
26c61ae5 | 514 | return bfd_elf_sym_name (abfd, hdr, &isym, NULL); |
b885599b AM |
515 | } |
516 | ||
dbb410c3 AM |
517 | /* Set next_in_group list pointer, and group name for NEWSECT. */ |
518 | ||
b34976b6 | 519 | static bfd_boolean |
217aa764 | 520 | setup_group (bfd *abfd, Elf_Internal_Shdr *hdr, asection *newsect) |
dbb410c3 AM |
521 | { |
522 | unsigned int num_group = elf_tdata (abfd)->num_group; | |
523 | ||
524 | /* If num_group is zero, read in all SHT_GROUP sections. The count | |
525 | is set to -1 if there are no SHT_GROUP sections. */ | |
526 | if (num_group == 0) | |
527 | { | |
528 | unsigned int i, shnum; | |
529 | ||
530 | /* First count the number of groups. If we have a SHT_GROUP | |
531 | section with just a flag word (ie. sh_size is 4), ignore it. */ | |
9ad5cbcf | 532 | shnum = elf_numsections (abfd); |
dbb410c3 | 533 | num_group = 0; |
08a40648 | 534 | |
1783205a NC |
535 | #define IS_VALID_GROUP_SECTION_HEADER(shdr) \ |
536 | ( (shdr)->sh_type == SHT_GROUP \ | |
537 | && (shdr)->sh_size >= (2 * GRP_ENTRY_SIZE) \ | |
538 | && (shdr)->sh_entsize == GRP_ENTRY_SIZE \ | |
539 | && ((shdr)->sh_size % GRP_ENTRY_SIZE) == 0) | |
08a40648 | 540 | |
dbb410c3 AM |
541 | for (i = 0; i < shnum; i++) |
542 | { | |
543 | Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i]; | |
1783205a NC |
544 | |
545 | if (IS_VALID_GROUP_SECTION_HEADER (shdr)) | |
dbb410c3 AM |
546 | num_group += 1; |
547 | } | |
548 | ||
549 | if (num_group == 0) | |
20dbb49d L |
550 | { |
551 | num_group = (unsigned) -1; | |
552 | elf_tdata (abfd)->num_group = num_group; | |
553 | } | |
554 | else | |
dbb410c3 AM |
555 | { |
556 | /* We keep a list of elf section headers for group sections, | |
557 | so we can find them quickly. */ | |
20dbb49d | 558 | bfd_size_type amt; |
d0fb9a8d | 559 | |
20dbb49d | 560 | elf_tdata (abfd)->num_group = num_group; |
d0fb9a8d JJ |
561 | elf_tdata (abfd)->group_sect_ptr |
562 | = bfd_alloc2 (abfd, num_group, sizeof (Elf_Internal_Shdr *)); | |
dbb410c3 | 563 | if (elf_tdata (abfd)->group_sect_ptr == NULL) |
b34976b6 | 564 | return FALSE; |
dbb410c3 AM |
565 | |
566 | num_group = 0; | |
567 | for (i = 0; i < shnum; i++) | |
568 | { | |
569 | Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i]; | |
1783205a NC |
570 | |
571 | if (IS_VALID_GROUP_SECTION_HEADER (shdr)) | |
dbb410c3 | 572 | { |
973ffd63 | 573 | unsigned char *src; |
dbb410c3 AM |
574 | Elf_Internal_Group *dest; |
575 | ||
576 | /* Add to list of sections. */ | |
577 | elf_tdata (abfd)->group_sect_ptr[num_group] = shdr; | |
578 | num_group += 1; | |
579 | ||
580 | /* Read the raw contents. */ | |
581 | BFD_ASSERT (sizeof (*dest) >= 4); | |
582 | amt = shdr->sh_size * sizeof (*dest) / 4; | |
d0fb9a8d JJ |
583 | shdr->contents = bfd_alloc2 (abfd, shdr->sh_size, |
584 | sizeof (*dest) / 4); | |
1783205a NC |
585 | /* PR binutils/4110: Handle corrupt group headers. */ |
586 | if (shdr->contents == NULL) | |
587 | { | |
588 | _bfd_error_handler | |
589 | (_("%B: Corrupt size field in group section header: 0x%lx"), abfd, shdr->sh_size); | |
590 | bfd_set_error (bfd_error_bad_value); | |
591 | return FALSE; | |
592 | } | |
593 | ||
594 | memset (shdr->contents, 0, amt); | |
595 | ||
596 | if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0 | |
dbb410c3 AM |
597 | || (bfd_bread (shdr->contents, shdr->sh_size, abfd) |
598 | != shdr->sh_size)) | |
b34976b6 | 599 | return FALSE; |
dbb410c3 AM |
600 | |
601 | /* Translate raw contents, a flag word followed by an | |
602 | array of elf section indices all in target byte order, | |
603 | to the flag word followed by an array of elf section | |
604 | pointers. */ | |
605 | src = shdr->contents + shdr->sh_size; | |
606 | dest = (Elf_Internal_Group *) (shdr->contents + amt); | |
607 | while (1) | |
608 | { | |
609 | unsigned int idx; | |
610 | ||
611 | src -= 4; | |
612 | --dest; | |
613 | idx = H_GET_32 (abfd, src); | |
614 | if (src == shdr->contents) | |
615 | { | |
616 | dest->flags = idx; | |
b885599b AM |
617 | if (shdr->bfd_section != NULL && (idx & GRP_COMDAT)) |
618 | shdr->bfd_section->flags | |
619 | |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD; | |
dbb410c3 AM |
620 | break; |
621 | } | |
622 | if (idx >= shnum) | |
623 | { | |
624 | ((*_bfd_error_handler) | |
d003868e | 625 | (_("%B: invalid SHT_GROUP entry"), abfd)); |
dbb410c3 AM |
626 | idx = 0; |
627 | } | |
628 | dest->shdr = elf_elfsections (abfd)[idx]; | |
629 | } | |
630 | } | |
631 | } | |
632 | } | |
633 | } | |
634 | ||
635 | if (num_group != (unsigned) -1) | |
636 | { | |
637 | unsigned int i; | |
638 | ||
639 | for (i = 0; i < num_group; i++) | |
640 | { | |
641 | Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i]; | |
642 | Elf_Internal_Group *idx = (Elf_Internal_Group *) shdr->contents; | |
643 | unsigned int n_elt = shdr->sh_size / 4; | |
644 | ||
645 | /* Look through this group's sections to see if current | |
646 | section is a member. */ | |
647 | while (--n_elt != 0) | |
648 | if ((++idx)->shdr == hdr) | |
649 | { | |
e0e8c97f | 650 | asection *s = NULL; |
dbb410c3 AM |
651 | |
652 | /* We are a member of this group. Go looking through | |
653 | other members to see if any others are linked via | |
654 | next_in_group. */ | |
655 | idx = (Elf_Internal_Group *) shdr->contents; | |
656 | n_elt = shdr->sh_size / 4; | |
657 | while (--n_elt != 0) | |
658 | if ((s = (++idx)->shdr->bfd_section) != NULL | |
945906ff | 659 | && elf_next_in_group (s) != NULL) |
dbb410c3 AM |
660 | break; |
661 | if (n_elt != 0) | |
662 | { | |
dbb410c3 AM |
663 | /* Snarf the group name from other member, and |
664 | insert current section in circular list. */ | |
945906ff AM |
665 | elf_group_name (newsect) = elf_group_name (s); |
666 | elf_next_in_group (newsect) = elf_next_in_group (s); | |
667 | elf_next_in_group (s) = newsect; | |
dbb410c3 AM |
668 | } |
669 | else | |
670 | { | |
dbb410c3 AM |
671 | const char *gname; |
672 | ||
b885599b AM |
673 | gname = group_signature (abfd, shdr); |
674 | if (gname == NULL) | |
b34976b6 | 675 | return FALSE; |
945906ff | 676 | elf_group_name (newsect) = gname; |
dbb410c3 AM |
677 | |
678 | /* Start a circular list with one element. */ | |
945906ff | 679 | elf_next_in_group (newsect) = newsect; |
dbb410c3 | 680 | } |
b885599b | 681 | |
9dce4196 AM |
682 | /* If the group section has been created, point to the |
683 | new member. */ | |
dbb410c3 | 684 | if (shdr->bfd_section != NULL) |
945906ff | 685 | elf_next_in_group (shdr->bfd_section) = newsect; |
b885599b | 686 | |
dbb410c3 AM |
687 | i = num_group - 1; |
688 | break; | |
689 | } | |
690 | } | |
691 | } | |
692 | ||
945906ff | 693 | if (elf_group_name (newsect) == NULL) |
dbb410c3 | 694 | { |
d003868e AM |
695 | (*_bfd_error_handler) (_("%B: no group info for section %A"), |
696 | abfd, newsect); | |
dbb410c3 | 697 | } |
b34976b6 | 698 | return TRUE; |
dbb410c3 AM |
699 | } |
700 | ||
3d7f7666 | 701 | bfd_boolean |
dd863624 | 702 | _bfd_elf_setup_sections (bfd *abfd) |
3d7f7666 L |
703 | { |
704 | unsigned int i; | |
705 | unsigned int num_group = elf_tdata (abfd)->num_group; | |
706 | bfd_boolean result = TRUE; | |
dd863624 L |
707 | asection *s; |
708 | ||
709 | /* Process SHF_LINK_ORDER. */ | |
710 | for (s = abfd->sections; s != NULL; s = s->next) | |
711 | { | |
712 | Elf_Internal_Shdr *this_hdr = &elf_section_data (s)->this_hdr; | |
713 | if ((this_hdr->sh_flags & SHF_LINK_ORDER) != 0) | |
714 | { | |
715 | unsigned int elfsec = this_hdr->sh_link; | |
716 | /* FIXME: The old Intel compiler and old strip/objcopy may | |
717 | not set the sh_link or sh_info fields. Hence we could | |
718 | get the situation where elfsec is 0. */ | |
719 | if (elfsec == 0) | |
720 | { | |
4fbb74a6 | 721 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
dd863624 L |
722 | if (bed->link_order_error_handler) |
723 | bed->link_order_error_handler | |
724 | (_("%B: warning: sh_link not set for section `%A'"), | |
725 | abfd, s); | |
726 | } | |
727 | else | |
728 | { | |
4fbb74a6 | 729 | asection *link = NULL; |
25bbc984 | 730 | |
4fbb74a6 AM |
731 | if (elfsec < elf_numsections (abfd)) |
732 | { | |
733 | this_hdr = elf_elfsections (abfd)[elfsec]; | |
734 | link = this_hdr->bfd_section; | |
735 | } | |
25bbc984 L |
736 | |
737 | /* PR 1991, 2008: | |
738 | Some strip/objcopy may leave an incorrect value in | |
739 | sh_link. We don't want to proceed. */ | |
25bbc984 L |
740 | if (link == NULL) |
741 | { | |
742 | (*_bfd_error_handler) | |
743 | (_("%B: sh_link [%d] in section `%A' is incorrect"), | |
744 | s->owner, s, elfsec); | |
745 | result = FALSE; | |
746 | } | |
747 | ||
748 | elf_linked_to_section (s) = link; | |
dd863624 L |
749 | } |
750 | } | |
751 | } | |
3d7f7666 | 752 | |
dd863624 | 753 | /* Process section groups. */ |
3d7f7666 L |
754 | if (num_group == (unsigned) -1) |
755 | return result; | |
756 | ||
757 | for (i = 0; i < num_group; i++) | |
758 | { | |
759 | Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i]; | |
760 | Elf_Internal_Group *idx = (Elf_Internal_Group *) shdr->contents; | |
761 | unsigned int n_elt = shdr->sh_size / 4; | |
762 | ||
763 | while (--n_elt != 0) | |
764 | if ((++idx)->shdr->bfd_section) | |
765 | elf_sec_group (idx->shdr->bfd_section) = shdr->bfd_section; | |
766 | else if (idx->shdr->sh_type == SHT_RELA | |
767 | || idx->shdr->sh_type == SHT_REL) | |
768 | /* We won't include relocation sections in section groups in | |
769 | output object files. We adjust the group section size here | |
770 | so that relocatable link will work correctly when | |
771 | relocation sections are in section group in input object | |
772 | files. */ | |
773 | shdr->bfd_section->size -= 4; | |
774 | else | |
775 | { | |
776 | /* There are some unknown sections in the group. */ | |
777 | (*_bfd_error_handler) | |
d003868e AM |
778 | (_("%B: unknown [%d] section `%s' in group [%s]"), |
779 | abfd, | |
3d7f7666 | 780 | (unsigned int) idx->shdr->sh_type, |
1b3a8575 AM |
781 | bfd_elf_string_from_elf_section (abfd, |
782 | (elf_elfheader (abfd) | |
783 | ->e_shstrndx), | |
784 | idx->shdr->sh_name), | |
3d7f7666 L |
785 | shdr->bfd_section->name); |
786 | result = FALSE; | |
787 | } | |
788 | } | |
789 | return result; | |
790 | } | |
791 | ||
72adc230 AM |
792 | bfd_boolean |
793 | bfd_elf_is_group_section (bfd *abfd ATTRIBUTE_UNUSED, const asection *sec) | |
794 | { | |
795 | return elf_next_in_group (sec) != NULL; | |
796 | } | |
797 | ||
252b5132 RH |
798 | /* Make a BFD section from an ELF section. We store a pointer to the |
799 | BFD section in the bfd_section field of the header. */ | |
800 | ||
b34976b6 | 801 | bfd_boolean |
217aa764 AM |
802 | _bfd_elf_make_section_from_shdr (bfd *abfd, |
803 | Elf_Internal_Shdr *hdr, | |
6dc132d9 L |
804 | const char *name, |
805 | int shindex) | |
252b5132 RH |
806 | { |
807 | asection *newsect; | |
808 | flagword flags; | |
9c5bfbb7 | 809 | const struct elf_backend_data *bed; |
252b5132 RH |
810 | |
811 | if (hdr->bfd_section != NULL) | |
812 | { | |
813 | BFD_ASSERT (strcmp (name, | |
814 | bfd_get_section_name (abfd, hdr->bfd_section)) == 0); | |
b34976b6 | 815 | return TRUE; |
252b5132 RH |
816 | } |
817 | ||
818 | newsect = bfd_make_section_anyway (abfd, name); | |
819 | if (newsect == NULL) | |
b34976b6 | 820 | return FALSE; |
252b5132 | 821 | |
1829f4b2 AM |
822 | hdr->bfd_section = newsect; |
823 | elf_section_data (newsect)->this_hdr = *hdr; | |
6dc132d9 | 824 | elf_section_data (newsect)->this_idx = shindex; |
1829f4b2 | 825 | |
2f89ff8d L |
826 | /* Always use the real type/flags. */ |
827 | elf_section_type (newsect) = hdr->sh_type; | |
828 | elf_section_flags (newsect) = hdr->sh_flags; | |
829 | ||
252b5132 RH |
830 | newsect->filepos = hdr->sh_offset; |
831 | ||
832 | if (! bfd_set_section_vma (abfd, newsect, hdr->sh_addr) | |
833 | || ! bfd_set_section_size (abfd, newsect, hdr->sh_size) | |
834 | || ! bfd_set_section_alignment (abfd, newsect, | |
72de5009 | 835 | bfd_log2 (hdr->sh_addralign))) |
b34976b6 | 836 | return FALSE; |
252b5132 RH |
837 | |
838 | flags = SEC_NO_FLAGS; | |
839 | if (hdr->sh_type != SHT_NOBITS) | |
840 | flags |= SEC_HAS_CONTENTS; | |
dbb410c3 | 841 | if (hdr->sh_type == SHT_GROUP) |
b3096250 | 842 | flags |= SEC_GROUP | SEC_EXCLUDE; |
252b5132 RH |
843 | if ((hdr->sh_flags & SHF_ALLOC) != 0) |
844 | { | |
845 | flags |= SEC_ALLOC; | |
846 | if (hdr->sh_type != SHT_NOBITS) | |
847 | flags |= SEC_LOAD; | |
848 | } | |
849 | if ((hdr->sh_flags & SHF_WRITE) == 0) | |
850 | flags |= SEC_READONLY; | |
851 | if ((hdr->sh_flags & SHF_EXECINSTR) != 0) | |
852 | flags |= SEC_CODE; | |
853 | else if ((flags & SEC_LOAD) != 0) | |
854 | flags |= SEC_DATA; | |
f5fa8ca2 JJ |
855 | if ((hdr->sh_flags & SHF_MERGE) != 0) |
856 | { | |
857 | flags |= SEC_MERGE; | |
858 | newsect->entsize = hdr->sh_entsize; | |
859 | if ((hdr->sh_flags & SHF_STRINGS) != 0) | |
860 | flags |= SEC_STRINGS; | |
861 | } | |
dbb410c3 AM |
862 | if (hdr->sh_flags & SHF_GROUP) |
863 | if (!setup_group (abfd, hdr, newsect)) | |
b34976b6 | 864 | return FALSE; |
13ae64f3 JJ |
865 | if ((hdr->sh_flags & SHF_TLS) != 0) |
866 | flags |= SEC_THREAD_LOCAL; | |
252b5132 | 867 | |
3d2b39cf | 868 | if ((flags & SEC_ALLOC) == 0) |
7a6cc5fb | 869 | { |
3d2b39cf L |
870 | /* The debugging sections appear to be recognized only by name, |
871 | not any sort of flag. Their SEC_ALLOC bits are cleared. */ | |
872 | static const struct | |
873 | { | |
874 | const char *name; | |
875 | int len; | |
876 | } debug_sections [] = | |
877 | { | |
0112cd26 | 878 | { STRING_COMMA_LEN ("debug") }, /* 'd' */ |
3d2b39cf L |
879 | { NULL, 0 }, /* 'e' */ |
880 | { NULL, 0 }, /* 'f' */ | |
0112cd26 | 881 | { STRING_COMMA_LEN ("gnu.linkonce.wi.") }, /* 'g' */ |
3d2b39cf L |
882 | { NULL, 0 }, /* 'h' */ |
883 | { NULL, 0 }, /* 'i' */ | |
884 | { NULL, 0 }, /* 'j' */ | |
885 | { NULL, 0 }, /* 'k' */ | |
0112cd26 | 886 | { STRING_COMMA_LEN ("line") }, /* 'l' */ |
3d2b39cf L |
887 | { NULL, 0 }, /* 'm' */ |
888 | { NULL, 0 }, /* 'n' */ | |
889 | { NULL, 0 }, /* 'o' */ | |
890 | { NULL, 0 }, /* 'p' */ | |
891 | { NULL, 0 }, /* 'q' */ | |
892 | { NULL, 0 }, /* 'r' */ | |
0112cd26 | 893 | { STRING_COMMA_LEN ("stab") } /* 's' */ |
3d2b39cf | 894 | }; |
08a40648 | 895 | |
3d2b39cf L |
896 | if (name [0] == '.') |
897 | { | |
898 | int i = name [1] - 'd'; | |
899 | if (i >= 0 | |
900 | && i < (int) ARRAY_SIZE (debug_sections) | |
901 | && debug_sections [i].name != NULL | |
902 | && strncmp (&name [1], debug_sections [i].name, | |
903 | debug_sections [i].len) == 0) | |
904 | flags |= SEC_DEBUGGING; | |
905 | } | |
906 | } | |
252b5132 RH |
907 | |
908 | /* As a GNU extension, if the name begins with .gnu.linkonce, we | |
909 | only link a single copy of the section. This is used to support | |
910 | g++. g++ will emit each template expansion in its own section. | |
911 | The symbols will be defined as weak, so that multiple definitions | |
912 | are permitted. The GNU linker extension is to actually discard | |
913 | all but one of the sections. */ | |
0112cd26 | 914 | if (CONST_STRNEQ (name, ".gnu.linkonce") |
b885599b | 915 | && elf_next_in_group (newsect) == NULL) |
252b5132 RH |
916 | flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD; |
917 | ||
fa152c49 JW |
918 | bed = get_elf_backend_data (abfd); |
919 | if (bed->elf_backend_section_flags) | |
920 | if (! bed->elf_backend_section_flags (&flags, hdr)) | |
b34976b6 | 921 | return FALSE; |
fa152c49 | 922 | |
252b5132 | 923 | if (! bfd_set_section_flags (abfd, newsect, flags)) |
b34976b6 | 924 | return FALSE; |
252b5132 | 925 | |
718175fa JK |
926 | /* We do not parse the PT_NOTE segments as we are interested even in the |
927 | separate debug info files which may have the segments offsets corrupted. | |
928 | PT_NOTEs from the core files are currently not parsed using BFD. */ | |
929 | if (hdr->sh_type == SHT_NOTE) | |
930 | { | |
931 | char *contents; | |
932 | ||
933 | contents = bfd_malloc (hdr->sh_size); | |
934 | if (!contents) | |
935 | return FALSE; | |
936 | ||
937 | if (!bfd_get_section_contents (abfd, hdr->bfd_section, contents, 0, | |
938 | hdr->sh_size) | |
939 | || !elf_parse_notes (abfd, contents, hdr->sh_size, -1)) | |
940 | { | |
941 | free (contents); | |
942 | return FALSE; | |
943 | } | |
944 | ||
945 | free (contents); | |
946 | } | |
947 | ||
252b5132 RH |
948 | if ((flags & SEC_ALLOC) != 0) |
949 | { | |
950 | Elf_Internal_Phdr *phdr; | |
6ffd7900 AM |
951 | unsigned int i, nload; |
952 | ||
953 | /* Some ELF linkers produce binaries with all the program header | |
954 | p_paddr fields zero. If we have such a binary with more than | |
955 | one PT_LOAD header, then leave the section lma equal to vma | |
956 | so that we don't create sections with overlapping lma. */ | |
957 | phdr = elf_tdata (abfd)->phdr; | |
958 | for (nload = 0, i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++) | |
959 | if (phdr->p_paddr != 0) | |
960 | break; | |
961 | else if (phdr->p_type == PT_LOAD && phdr->p_memsz != 0) | |
962 | ++nload; | |
963 | if (i >= elf_elfheader (abfd)->e_phnum && nload > 1) | |
964 | return TRUE; | |
252b5132 | 965 | |
252b5132 RH |
966 | phdr = elf_tdata (abfd)->phdr; |
967 | for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++) | |
968 | { | |
88967714 AM |
969 | /* This section is part of this segment if its file |
970 | offset plus size lies within the segment's memory | |
971 | span and, if the section is loaded, the extent of the | |
972 | loaded data lies within the extent of the segment. | |
973 | ||
974 | Note - we used to check the p_paddr field as well, and | |
975 | refuse to set the LMA if it was 0. This is wrong | |
976 | though, as a perfectly valid initialised segment can | |
977 | have a p_paddr of zero. Some architectures, eg ARM, | |
978 | place special significance on the address 0 and | |
979 | executables need to be able to have a segment which | |
980 | covers this address. */ | |
981 | if (phdr->p_type == PT_LOAD | |
982 | && (bfd_vma) hdr->sh_offset >= phdr->p_offset | |
983 | && (hdr->sh_offset + hdr->sh_size | |
984 | <= phdr->p_offset + phdr->p_memsz) | |
985 | && ((flags & SEC_LOAD) == 0 | |
986 | || (hdr->sh_offset + hdr->sh_size | |
987 | <= phdr->p_offset + phdr->p_filesz))) | |
252b5132 | 988 | { |
88967714 AM |
989 | if ((flags & SEC_LOAD) == 0) |
990 | newsect->lma = (phdr->p_paddr | |
991 | + hdr->sh_addr - phdr->p_vaddr); | |
992 | else | |
993 | /* We used to use the same adjustment for SEC_LOAD | |
994 | sections, but that doesn't work if the segment | |
995 | is packed with code from multiple VMAs. | |
996 | Instead we calculate the section LMA based on | |
997 | the segment LMA. It is assumed that the | |
998 | segment will contain sections with contiguous | |
999 | LMAs, even if the VMAs are not. */ | |
1000 | newsect->lma = (phdr->p_paddr | |
1001 | + hdr->sh_offset - phdr->p_offset); | |
1002 | ||
1003 | /* With contiguous segments, we can't tell from file | |
1004 | offsets whether a section with zero size should | |
1005 | be placed at the end of one segment or the | |
1006 | beginning of the next. Decide based on vaddr. */ | |
1007 | if (hdr->sh_addr >= phdr->p_vaddr | |
1008 | && (hdr->sh_addr + hdr->sh_size | |
1009 | <= phdr->p_vaddr + phdr->p_memsz)) | |
1010 | break; | |
252b5132 RH |
1011 | } |
1012 | } | |
1013 | } | |
1014 | ||
b34976b6 | 1015 | return TRUE; |
252b5132 RH |
1016 | } |
1017 | ||
1018 | /* | |
1019 | INTERNAL_FUNCTION | |
1020 | bfd_elf_find_section | |
1021 | ||
1022 | SYNOPSIS | |
1023 | struct elf_internal_shdr *bfd_elf_find_section (bfd *abfd, char *name); | |
1024 | ||
1025 | DESCRIPTION | |
1026 | Helper functions for GDB to locate the string tables. | |
1027 | Since BFD hides string tables from callers, GDB needs to use an | |
1028 | internal hook to find them. Sun's .stabstr, in particular, | |
1029 | isn't even pointed to by the .stab section, so ordinary | |
1030 | mechanisms wouldn't work to find it, even if we had some. | |
1031 | */ | |
1032 | ||
1033 | struct elf_internal_shdr * | |
217aa764 | 1034 | bfd_elf_find_section (bfd *abfd, char *name) |
252b5132 RH |
1035 | { |
1036 | Elf_Internal_Shdr **i_shdrp; | |
1037 | char *shstrtab; | |
1038 | unsigned int max; | |
1039 | unsigned int i; | |
1040 | ||
1041 | i_shdrp = elf_elfsections (abfd); | |
1042 | if (i_shdrp != NULL) | |
1043 | { | |
9ad5cbcf AM |
1044 | shstrtab = bfd_elf_get_str_section (abfd, |
1045 | elf_elfheader (abfd)->e_shstrndx); | |
252b5132 RH |
1046 | if (shstrtab != NULL) |
1047 | { | |
9ad5cbcf | 1048 | max = elf_numsections (abfd); |
252b5132 RH |
1049 | for (i = 1; i < max; i++) |
1050 | if (!strcmp (&shstrtab[i_shdrp[i]->sh_name], name)) | |
1051 | return i_shdrp[i]; | |
1052 | } | |
1053 | } | |
1054 | return 0; | |
1055 | } | |
1056 | ||
1057 | const char *const bfd_elf_section_type_names[] = { | |
1058 | "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB", | |
1059 | "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE", | |
1060 | "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM", | |
1061 | }; | |
1062 | ||
1049f94e | 1063 | /* ELF relocs are against symbols. If we are producing relocatable |
252b5132 RH |
1064 | output, and the reloc is against an external symbol, and nothing |
1065 | has given us any additional addend, the resulting reloc will also | |
1066 | be against the same symbol. In such a case, we don't want to | |
1067 | change anything about the way the reloc is handled, since it will | |
1068 | all be done at final link time. Rather than put special case code | |
1069 | into bfd_perform_relocation, all the reloc types use this howto | |
1070 | function. It just short circuits the reloc if producing | |
1049f94e | 1071 | relocatable output against an external symbol. */ |
252b5132 | 1072 | |
252b5132 | 1073 | bfd_reloc_status_type |
217aa764 AM |
1074 | bfd_elf_generic_reloc (bfd *abfd ATTRIBUTE_UNUSED, |
1075 | arelent *reloc_entry, | |
1076 | asymbol *symbol, | |
1077 | void *data ATTRIBUTE_UNUSED, | |
1078 | asection *input_section, | |
1079 | bfd *output_bfd, | |
1080 | char **error_message ATTRIBUTE_UNUSED) | |
1081 | { | |
1082 | if (output_bfd != NULL | |
252b5132 RH |
1083 | && (symbol->flags & BSF_SECTION_SYM) == 0 |
1084 | && (! reloc_entry->howto->partial_inplace | |
1085 | || reloc_entry->addend == 0)) | |
1086 | { | |
1087 | reloc_entry->address += input_section->output_offset; | |
1088 | return bfd_reloc_ok; | |
1089 | } | |
1090 | ||
1091 | return bfd_reloc_continue; | |
1092 | } | |
1093 | \f | |
0ac4564e L |
1094 | /* Copy the program header and other data from one object module to |
1095 | another. */ | |
252b5132 | 1096 | |
b34976b6 | 1097 | bfd_boolean |
217aa764 | 1098 | _bfd_elf_copy_private_bfd_data (bfd *ibfd, bfd *obfd) |
2d502050 L |
1099 | { |
1100 | if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour | |
1101 | || bfd_get_flavour (obfd) != bfd_target_elf_flavour) | |
b34976b6 | 1102 | return TRUE; |
2d502050 L |
1103 | |
1104 | BFD_ASSERT (!elf_flags_init (obfd) | |
1105 | || (elf_elfheader (obfd)->e_flags | |
1106 | == elf_elfheader (ibfd)->e_flags)); | |
1107 | ||
0ac4564e | 1108 | elf_gp (obfd) = elf_gp (ibfd); |
2d502050 | 1109 | elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags; |
b34976b6 | 1110 | elf_flags_init (obfd) = TRUE; |
104d59d1 JM |
1111 | |
1112 | /* Copy object attributes. */ | |
1113 | _bfd_elf_copy_obj_attributes (ibfd, obfd); | |
1114 | ||
b34976b6 | 1115 | return TRUE; |
2d502050 L |
1116 | } |
1117 | ||
cedc298e L |
1118 | static const char * |
1119 | get_segment_type (unsigned int p_type) | |
1120 | { | |
1121 | const char *pt; | |
1122 | switch (p_type) | |
1123 | { | |
1124 | case PT_NULL: pt = "NULL"; break; | |
1125 | case PT_LOAD: pt = "LOAD"; break; | |
1126 | case PT_DYNAMIC: pt = "DYNAMIC"; break; | |
1127 | case PT_INTERP: pt = "INTERP"; break; | |
1128 | case PT_NOTE: pt = "NOTE"; break; | |
1129 | case PT_SHLIB: pt = "SHLIB"; break; | |
1130 | case PT_PHDR: pt = "PHDR"; break; | |
1131 | case PT_TLS: pt = "TLS"; break; | |
1132 | case PT_GNU_EH_FRAME: pt = "EH_FRAME"; break; | |
2b05f1b7 | 1133 | case PT_GNU_STACK: pt = "STACK"; break; |
cedc298e L |
1134 | case PT_GNU_RELRO: pt = "RELRO"; break; |
1135 | default: pt = NULL; break; | |
1136 | } | |
1137 | return pt; | |
1138 | } | |
1139 | ||
f0b79d91 L |
1140 | /* Print out the program headers. */ |
1141 | ||
b34976b6 | 1142 | bfd_boolean |
217aa764 | 1143 | _bfd_elf_print_private_bfd_data (bfd *abfd, void *farg) |
252b5132 | 1144 | { |
217aa764 | 1145 | FILE *f = farg; |
252b5132 RH |
1146 | Elf_Internal_Phdr *p; |
1147 | asection *s; | |
1148 | bfd_byte *dynbuf = NULL; | |
1149 | ||
1150 | p = elf_tdata (abfd)->phdr; | |
1151 | if (p != NULL) | |
1152 | { | |
1153 | unsigned int i, c; | |
1154 | ||
1155 | fprintf (f, _("\nProgram Header:\n")); | |
1156 | c = elf_elfheader (abfd)->e_phnum; | |
1157 | for (i = 0; i < c; i++, p++) | |
1158 | { | |
cedc298e | 1159 | const char *pt = get_segment_type (p->p_type); |
252b5132 RH |
1160 | char buf[20]; |
1161 | ||
cedc298e | 1162 | if (pt == NULL) |
252b5132 | 1163 | { |
cedc298e L |
1164 | sprintf (buf, "0x%lx", p->p_type); |
1165 | pt = buf; | |
252b5132 | 1166 | } |
dc810e39 | 1167 | fprintf (f, "%8s off 0x", pt); |
60b89a18 | 1168 | bfd_fprintf_vma (abfd, f, p->p_offset); |
252b5132 | 1169 | fprintf (f, " vaddr 0x"); |
60b89a18 | 1170 | bfd_fprintf_vma (abfd, f, p->p_vaddr); |
252b5132 | 1171 | fprintf (f, " paddr 0x"); |
60b89a18 | 1172 | bfd_fprintf_vma (abfd, f, p->p_paddr); |
252b5132 RH |
1173 | fprintf (f, " align 2**%u\n", bfd_log2 (p->p_align)); |
1174 | fprintf (f, " filesz 0x"); | |
60b89a18 | 1175 | bfd_fprintf_vma (abfd, f, p->p_filesz); |
252b5132 | 1176 | fprintf (f, " memsz 0x"); |
60b89a18 | 1177 | bfd_fprintf_vma (abfd, f, p->p_memsz); |
252b5132 RH |
1178 | fprintf (f, " flags %c%c%c", |
1179 | (p->p_flags & PF_R) != 0 ? 'r' : '-', | |
1180 | (p->p_flags & PF_W) != 0 ? 'w' : '-', | |
1181 | (p->p_flags & PF_X) != 0 ? 'x' : '-'); | |
dc810e39 AM |
1182 | if ((p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X)) != 0) |
1183 | fprintf (f, " %lx", p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X)); | |
252b5132 RH |
1184 | fprintf (f, "\n"); |
1185 | } | |
1186 | } | |
1187 | ||
1188 | s = bfd_get_section_by_name (abfd, ".dynamic"); | |
1189 | if (s != NULL) | |
1190 | { | |
cb33740c | 1191 | unsigned int elfsec; |
dc810e39 | 1192 | unsigned long shlink; |
252b5132 RH |
1193 | bfd_byte *extdyn, *extdynend; |
1194 | size_t extdynsize; | |
217aa764 | 1195 | void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *); |
252b5132 RH |
1196 | |
1197 | fprintf (f, _("\nDynamic Section:\n")); | |
1198 | ||
eea6121a | 1199 | if (!bfd_malloc_and_get_section (abfd, s, &dynbuf)) |
252b5132 RH |
1200 | goto error_return; |
1201 | ||
1202 | elfsec = _bfd_elf_section_from_bfd_section (abfd, s); | |
cb33740c | 1203 | if (elfsec == SHN_BAD) |
252b5132 | 1204 | goto error_return; |
dc810e39 | 1205 | shlink = elf_elfsections (abfd)[elfsec]->sh_link; |
252b5132 RH |
1206 | |
1207 | extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn; | |
1208 | swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in; | |
1209 | ||
1210 | extdyn = dynbuf; | |
eea6121a | 1211 | extdynend = extdyn + s->size; |
252b5132 RH |
1212 | for (; extdyn < extdynend; extdyn += extdynsize) |
1213 | { | |
1214 | Elf_Internal_Dyn dyn; | |
ad9563d6 | 1215 | const char *name = ""; |
252b5132 | 1216 | char ab[20]; |
b34976b6 | 1217 | bfd_boolean stringp; |
ad9563d6 | 1218 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
252b5132 | 1219 | |
217aa764 | 1220 | (*swap_dyn_in) (abfd, extdyn, &dyn); |
252b5132 RH |
1221 | |
1222 | if (dyn.d_tag == DT_NULL) | |
1223 | break; | |
1224 | ||
b34976b6 | 1225 | stringp = FALSE; |
252b5132 RH |
1226 | switch (dyn.d_tag) |
1227 | { | |
1228 | default: | |
ad9563d6 CM |
1229 | if (bed->elf_backend_get_target_dtag) |
1230 | name = (*bed->elf_backend_get_target_dtag) (dyn.d_tag); | |
1231 | ||
1232 | if (!strcmp (name, "")) | |
1233 | { | |
1234 | sprintf (ab, "0x%lx", (unsigned long) dyn.d_tag); | |
1235 | name = ab; | |
1236 | } | |
252b5132 RH |
1237 | break; |
1238 | ||
b34976b6 | 1239 | case DT_NEEDED: name = "NEEDED"; stringp = TRUE; break; |
252b5132 RH |
1240 | case DT_PLTRELSZ: name = "PLTRELSZ"; break; |
1241 | case DT_PLTGOT: name = "PLTGOT"; break; | |
1242 | case DT_HASH: name = "HASH"; break; | |
1243 | case DT_STRTAB: name = "STRTAB"; break; | |
1244 | case DT_SYMTAB: name = "SYMTAB"; break; | |
1245 | case DT_RELA: name = "RELA"; break; | |
1246 | case DT_RELASZ: name = "RELASZ"; break; | |
1247 | case DT_RELAENT: name = "RELAENT"; break; | |
1248 | case DT_STRSZ: name = "STRSZ"; break; | |
1249 | case DT_SYMENT: name = "SYMENT"; break; | |
1250 | case DT_INIT: name = "INIT"; break; | |
1251 | case DT_FINI: name = "FINI"; break; | |
b34976b6 AM |
1252 | case DT_SONAME: name = "SONAME"; stringp = TRUE; break; |
1253 | case DT_RPATH: name = "RPATH"; stringp = TRUE; break; | |
252b5132 RH |
1254 | case DT_SYMBOLIC: name = "SYMBOLIC"; break; |
1255 | case DT_REL: name = "REL"; break; | |
1256 | case DT_RELSZ: name = "RELSZ"; break; | |
1257 | case DT_RELENT: name = "RELENT"; break; | |
1258 | case DT_PLTREL: name = "PLTREL"; break; | |
1259 | case DT_DEBUG: name = "DEBUG"; break; | |
1260 | case DT_TEXTREL: name = "TEXTREL"; break; | |
1261 | case DT_JMPREL: name = "JMPREL"; break; | |
94558834 L |
1262 | case DT_BIND_NOW: name = "BIND_NOW"; break; |
1263 | case DT_INIT_ARRAY: name = "INIT_ARRAY"; break; | |
1264 | case DT_FINI_ARRAY: name = "FINI_ARRAY"; break; | |
1265 | case DT_INIT_ARRAYSZ: name = "INIT_ARRAYSZ"; break; | |
1266 | case DT_FINI_ARRAYSZ: name = "FINI_ARRAYSZ"; break; | |
b34976b6 | 1267 | case DT_RUNPATH: name = "RUNPATH"; stringp = TRUE; break; |
94558834 L |
1268 | case DT_FLAGS: name = "FLAGS"; break; |
1269 | case DT_PREINIT_ARRAY: name = "PREINIT_ARRAY"; break; | |
1270 | case DT_PREINIT_ARRAYSZ: name = "PREINIT_ARRAYSZ"; break; | |
d48188b9 | 1271 | case DT_CHECKSUM: name = "CHECKSUM"; break; |
94558834 L |
1272 | case DT_PLTPADSZ: name = "PLTPADSZ"; break; |
1273 | case DT_MOVEENT: name = "MOVEENT"; break; | |
1274 | case DT_MOVESZ: name = "MOVESZ"; break; | |
1275 | case DT_FEATURE: name = "FEATURE"; break; | |
1276 | case DT_POSFLAG_1: name = "POSFLAG_1"; break; | |
1277 | case DT_SYMINSZ: name = "SYMINSZ"; break; | |
1278 | case DT_SYMINENT: name = "SYMINENT"; break; | |
b34976b6 AM |
1279 | case DT_CONFIG: name = "CONFIG"; stringp = TRUE; break; |
1280 | case DT_DEPAUDIT: name = "DEPAUDIT"; stringp = TRUE; break; | |
1281 | case DT_AUDIT: name = "AUDIT"; stringp = TRUE; break; | |
94558834 L |
1282 | case DT_PLTPAD: name = "PLTPAD"; break; |
1283 | case DT_MOVETAB: name = "MOVETAB"; break; | |
1284 | case DT_SYMINFO: name = "SYMINFO"; break; | |
1285 | case DT_RELACOUNT: name = "RELACOUNT"; break; | |
1286 | case DT_RELCOUNT: name = "RELCOUNT"; break; | |
1287 | case DT_FLAGS_1: name = "FLAGS_1"; break; | |
252b5132 RH |
1288 | case DT_VERSYM: name = "VERSYM"; break; |
1289 | case DT_VERDEF: name = "VERDEF"; break; | |
1290 | case DT_VERDEFNUM: name = "VERDEFNUM"; break; | |
1291 | case DT_VERNEED: name = "VERNEED"; break; | |
1292 | case DT_VERNEEDNUM: name = "VERNEEDNUM"; break; | |
b34976b6 | 1293 | case DT_AUXILIARY: name = "AUXILIARY"; stringp = TRUE; break; |
94558834 | 1294 | case DT_USED: name = "USED"; break; |
b34976b6 | 1295 | case DT_FILTER: name = "FILTER"; stringp = TRUE; break; |
fdc90cb4 | 1296 | case DT_GNU_HASH: name = "GNU_HASH"; break; |
252b5132 RH |
1297 | } |
1298 | ||
ad9563d6 | 1299 | fprintf (f, " %-20s ", name); |
252b5132 | 1300 | if (! stringp) |
a1f3c56e AN |
1301 | { |
1302 | fprintf (f, "0x"); | |
1303 | bfd_fprintf_vma (abfd, f, dyn.d_un.d_val); | |
1304 | } | |
252b5132 RH |
1305 | else |
1306 | { | |
1307 | const char *string; | |
dc810e39 | 1308 | unsigned int tagv = dyn.d_un.d_val; |
252b5132 | 1309 | |
dc810e39 | 1310 | string = bfd_elf_string_from_elf_section (abfd, shlink, tagv); |
252b5132 RH |
1311 | if (string == NULL) |
1312 | goto error_return; | |
1313 | fprintf (f, "%s", string); | |
1314 | } | |
1315 | fprintf (f, "\n"); | |
1316 | } | |
1317 | ||
1318 | free (dynbuf); | |
1319 | dynbuf = NULL; | |
1320 | } | |
1321 | ||
1322 | if ((elf_dynverdef (abfd) != 0 && elf_tdata (abfd)->verdef == NULL) | |
1323 | || (elf_dynverref (abfd) != 0 && elf_tdata (abfd)->verref == NULL)) | |
1324 | { | |
fc0e6df6 | 1325 | if (! _bfd_elf_slurp_version_tables (abfd, FALSE)) |
b34976b6 | 1326 | return FALSE; |
252b5132 RH |
1327 | } |
1328 | ||
1329 | if (elf_dynverdef (abfd) != 0) | |
1330 | { | |
1331 | Elf_Internal_Verdef *t; | |
1332 | ||
1333 | fprintf (f, _("\nVersion definitions:\n")); | |
1334 | for (t = elf_tdata (abfd)->verdef; t != NULL; t = t->vd_nextdef) | |
1335 | { | |
1336 | fprintf (f, "%d 0x%2.2x 0x%8.8lx %s\n", t->vd_ndx, | |
d0fb9a8d JJ |
1337 | t->vd_flags, t->vd_hash, |
1338 | t->vd_nodename ? t->vd_nodename : "<corrupt>"); | |
1339 | if (t->vd_auxptr != NULL && t->vd_auxptr->vda_nextptr != NULL) | |
252b5132 RH |
1340 | { |
1341 | Elf_Internal_Verdaux *a; | |
1342 | ||
1343 | fprintf (f, "\t"); | |
1344 | for (a = t->vd_auxptr->vda_nextptr; | |
1345 | a != NULL; | |
1346 | a = a->vda_nextptr) | |
d0fb9a8d JJ |
1347 | fprintf (f, "%s ", |
1348 | a->vda_nodename ? a->vda_nodename : "<corrupt>"); | |
252b5132 RH |
1349 | fprintf (f, "\n"); |
1350 | } | |
1351 | } | |
1352 | } | |
1353 | ||
1354 | if (elf_dynverref (abfd) != 0) | |
1355 | { | |
1356 | Elf_Internal_Verneed *t; | |
1357 | ||
1358 | fprintf (f, _("\nVersion References:\n")); | |
1359 | for (t = elf_tdata (abfd)->verref; t != NULL; t = t->vn_nextref) | |
1360 | { | |
1361 | Elf_Internal_Vernaux *a; | |
1362 | ||
d0fb9a8d JJ |
1363 | fprintf (f, _(" required from %s:\n"), |
1364 | t->vn_filename ? t->vn_filename : "<corrupt>"); | |
252b5132 RH |
1365 | for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) |
1366 | fprintf (f, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a->vna_hash, | |
d0fb9a8d JJ |
1367 | a->vna_flags, a->vna_other, |
1368 | a->vna_nodename ? a->vna_nodename : "<corrupt>"); | |
252b5132 RH |
1369 | } |
1370 | } | |
1371 | ||
b34976b6 | 1372 | return TRUE; |
252b5132 RH |
1373 | |
1374 | error_return: | |
1375 | if (dynbuf != NULL) | |
1376 | free (dynbuf); | |
b34976b6 | 1377 | return FALSE; |
252b5132 RH |
1378 | } |
1379 | ||
1380 | /* Display ELF-specific fields of a symbol. */ | |
1381 | ||
1382 | void | |
217aa764 AM |
1383 | bfd_elf_print_symbol (bfd *abfd, |
1384 | void *filep, | |
1385 | asymbol *symbol, | |
1386 | bfd_print_symbol_type how) | |
252b5132 | 1387 | { |
217aa764 | 1388 | FILE *file = filep; |
252b5132 RH |
1389 | switch (how) |
1390 | { | |
1391 | case bfd_print_symbol_name: | |
1392 | fprintf (file, "%s", symbol->name); | |
1393 | break; | |
1394 | case bfd_print_symbol_more: | |
1395 | fprintf (file, "elf "); | |
60b89a18 | 1396 | bfd_fprintf_vma (abfd, file, symbol->value); |
252b5132 RH |
1397 | fprintf (file, " %lx", (long) symbol->flags); |
1398 | break; | |
1399 | case bfd_print_symbol_all: | |
1400 | { | |
4e8a9624 AM |
1401 | const char *section_name; |
1402 | const char *name = NULL; | |
9c5bfbb7 | 1403 | const struct elf_backend_data *bed; |
7a13edea | 1404 | unsigned char st_other; |
dbb410c3 | 1405 | bfd_vma val; |
c044fabd | 1406 | |
252b5132 | 1407 | section_name = symbol->section ? symbol->section->name : "(*none*)"; |
587ff49e RH |
1408 | |
1409 | bed = get_elf_backend_data (abfd); | |
1410 | if (bed->elf_backend_print_symbol_all) | |
c044fabd | 1411 | name = (*bed->elf_backend_print_symbol_all) (abfd, filep, symbol); |
587ff49e RH |
1412 | |
1413 | if (name == NULL) | |
1414 | { | |
7ee38065 | 1415 | name = symbol->name; |
217aa764 | 1416 | bfd_print_symbol_vandf (abfd, file, symbol); |
587ff49e RH |
1417 | } |
1418 | ||
252b5132 RH |
1419 | fprintf (file, " %s\t", section_name); |
1420 | /* Print the "other" value for a symbol. For common symbols, | |
1421 | we've already printed the size; now print the alignment. | |
1422 | For other symbols, we have no specified alignment, and | |
1423 | we've printed the address; now print the size. */ | |
dcf6c779 | 1424 | if (symbol->section && bfd_is_com_section (symbol->section)) |
dbb410c3 AM |
1425 | val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_value; |
1426 | else | |
1427 | val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_size; | |
1428 | bfd_fprintf_vma (abfd, file, val); | |
252b5132 RH |
1429 | |
1430 | /* If we have version information, print it. */ | |
1431 | if (elf_tdata (abfd)->dynversym_section != 0 | |
1432 | && (elf_tdata (abfd)->dynverdef_section != 0 | |
1433 | || elf_tdata (abfd)->dynverref_section != 0)) | |
1434 | { | |
1435 | unsigned int vernum; | |
1436 | const char *version_string; | |
1437 | ||
1438 | vernum = ((elf_symbol_type *) symbol)->version & VERSYM_VERSION; | |
1439 | ||
1440 | if (vernum == 0) | |
1441 | version_string = ""; | |
1442 | else if (vernum == 1) | |
1443 | version_string = "Base"; | |
1444 | else if (vernum <= elf_tdata (abfd)->cverdefs) | |
1445 | version_string = | |
1446 | elf_tdata (abfd)->verdef[vernum - 1].vd_nodename; | |
1447 | else | |
1448 | { | |
1449 | Elf_Internal_Verneed *t; | |
1450 | ||
1451 | version_string = ""; | |
1452 | for (t = elf_tdata (abfd)->verref; | |
1453 | t != NULL; | |
1454 | t = t->vn_nextref) | |
1455 | { | |
1456 | Elf_Internal_Vernaux *a; | |
1457 | ||
1458 | for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) | |
1459 | { | |
1460 | if (a->vna_other == vernum) | |
1461 | { | |
1462 | version_string = a->vna_nodename; | |
1463 | break; | |
1464 | } | |
1465 | } | |
1466 | } | |
1467 | } | |
1468 | ||
1469 | if ((((elf_symbol_type *) symbol)->version & VERSYM_HIDDEN) == 0) | |
1470 | fprintf (file, " %-11s", version_string); | |
1471 | else | |
1472 | { | |
1473 | int i; | |
1474 | ||
1475 | fprintf (file, " (%s)", version_string); | |
1476 | for (i = 10 - strlen (version_string); i > 0; --i) | |
1477 | putc (' ', file); | |
1478 | } | |
1479 | } | |
1480 | ||
1481 | /* If the st_other field is not zero, print it. */ | |
7a13edea | 1482 | st_other = ((elf_symbol_type *) symbol)->internal_elf_sym.st_other; |
c044fabd | 1483 | |
7a13edea NC |
1484 | switch (st_other) |
1485 | { | |
1486 | case 0: break; | |
1487 | case STV_INTERNAL: fprintf (file, " .internal"); break; | |
1488 | case STV_HIDDEN: fprintf (file, " .hidden"); break; | |
1489 | case STV_PROTECTED: fprintf (file, " .protected"); break; | |
1490 | default: | |
1491 | /* Some other non-defined flags are also present, so print | |
1492 | everything hex. */ | |
1493 | fprintf (file, " 0x%02x", (unsigned int) st_other); | |
1494 | } | |
252b5132 | 1495 | |
587ff49e | 1496 | fprintf (file, " %s", name); |
252b5132 RH |
1497 | } |
1498 | break; | |
1499 | } | |
1500 | } | |
252b5132 | 1501 | |
252b5132 RH |
1502 | /* Allocate an ELF string table--force the first byte to be zero. */ |
1503 | ||
1504 | struct bfd_strtab_hash * | |
217aa764 | 1505 | _bfd_elf_stringtab_init (void) |
252b5132 RH |
1506 | { |
1507 | struct bfd_strtab_hash *ret; | |
1508 | ||
1509 | ret = _bfd_stringtab_init (); | |
1510 | if (ret != NULL) | |
1511 | { | |
1512 | bfd_size_type loc; | |
1513 | ||
b34976b6 | 1514 | loc = _bfd_stringtab_add (ret, "", TRUE, FALSE); |
252b5132 RH |
1515 | BFD_ASSERT (loc == 0 || loc == (bfd_size_type) -1); |
1516 | if (loc == (bfd_size_type) -1) | |
1517 | { | |
1518 | _bfd_stringtab_free (ret); | |
1519 | ret = NULL; | |
1520 | } | |
1521 | } | |
1522 | return ret; | |
1523 | } | |
1524 | \f | |
1525 | /* ELF .o/exec file reading */ | |
1526 | ||
c044fabd | 1527 | /* Create a new bfd section from an ELF section header. */ |
252b5132 | 1528 | |
b34976b6 | 1529 | bfd_boolean |
217aa764 | 1530 | bfd_section_from_shdr (bfd *abfd, unsigned int shindex) |
252b5132 | 1531 | { |
4fbb74a6 AM |
1532 | Elf_Internal_Shdr *hdr; |
1533 | Elf_Internal_Ehdr *ehdr; | |
1534 | const struct elf_backend_data *bed; | |
90937f86 | 1535 | const char *name; |
252b5132 | 1536 | |
4fbb74a6 AM |
1537 | if (shindex >= elf_numsections (abfd)) |
1538 | return FALSE; | |
1539 | ||
1540 | hdr = elf_elfsections (abfd)[shindex]; | |
1541 | ehdr = elf_elfheader (abfd); | |
1542 | name = bfd_elf_string_from_elf_section (abfd, ehdr->e_shstrndx, | |
1b3a8575 | 1543 | hdr->sh_name); |
933d961a JJ |
1544 | if (name == NULL) |
1545 | return FALSE; | |
252b5132 | 1546 | |
4fbb74a6 | 1547 | bed = get_elf_backend_data (abfd); |
252b5132 RH |
1548 | switch (hdr->sh_type) |
1549 | { | |
1550 | case SHT_NULL: | |
1551 | /* Inactive section. Throw it away. */ | |
b34976b6 | 1552 | return TRUE; |
252b5132 RH |
1553 | |
1554 | case SHT_PROGBITS: /* Normal section with contents. */ | |
252b5132 RH |
1555 | case SHT_NOBITS: /* .bss section. */ |
1556 | case SHT_HASH: /* .hash section. */ | |
1557 | case SHT_NOTE: /* .note section. */ | |
25e27870 L |
1558 | case SHT_INIT_ARRAY: /* .init_array section. */ |
1559 | case SHT_FINI_ARRAY: /* .fini_array section. */ | |
1560 | case SHT_PREINIT_ARRAY: /* .preinit_array section. */ | |
7f1204bb | 1561 | case SHT_GNU_LIBLIST: /* .gnu.liblist section. */ |
fdc90cb4 | 1562 | case SHT_GNU_HASH: /* .gnu.hash section. */ |
6dc132d9 | 1563 | return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); |
252b5132 | 1564 | |
797fc050 | 1565 | case SHT_DYNAMIC: /* Dynamic linking information. */ |
6dc132d9 | 1566 | if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex)) |
b34976b6 | 1567 | return FALSE; |
8e0ed13f NC |
1568 | if (hdr->sh_link > elf_numsections (abfd) |
1569 | || elf_elfsections (abfd)[hdr->sh_link] == NULL) | |
1570 | return FALSE; | |
797fc050 AM |
1571 | if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_STRTAB) |
1572 | { | |
1573 | Elf_Internal_Shdr *dynsymhdr; | |
1574 | ||
1575 | /* The shared libraries distributed with hpux11 have a bogus | |
1576 | sh_link field for the ".dynamic" section. Find the | |
1577 | string table for the ".dynsym" section instead. */ | |
1578 | if (elf_dynsymtab (abfd) != 0) | |
1579 | { | |
1580 | dynsymhdr = elf_elfsections (abfd)[elf_dynsymtab (abfd)]; | |
1581 | hdr->sh_link = dynsymhdr->sh_link; | |
1582 | } | |
1583 | else | |
1584 | { | |
1585 | unsigned int i, num_sec; | |
1586 | ||
1587 | num_sec = elf_numsections (abfd); | |
1588 | for (i = 1; i < num_sec; i++) | |
1589 | { | |
1590 | dynsymhdr = elf_elfsections (abfd)[i]; | |
1591 | if (dynsymhdr->sh_type == SHT_DYNSYM) | |
1592 | { | |
1593 | hdr->sh_link = dynsymhdr->sh_link; | |
1594 | break; | |
1595 | } | |
1596 | } | |
1597 | } | |
1598 | } | |
1599 | break; | |
1600 | ||
252b5132 RH |
1601 | case SHT_SYMTAB: /* A symbol table */ |
1602 | if (elf_onesymtab (abfd) == shindex) | |
b34976b6 | 1603 | return TRUE; |
252b5132 | 1604 | |
a50b2160 JJ |
1605 | if (hdr->sh_entsize != bed->s->sizeof_sym) |
1606 | return FALSE; | |
252b5132 RH |
1607 | BFD_ASSERT (elf_onesymtab (abfd) == 0); |
1608 | elf_onesymtab (abfd) = shindex; | |
1609 | elf_tdata (abfd)->symtab_hdr = *hdr; | |
1610 | elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->symtab_hdr; | |
1611 | abfd->flags |= HAS_SYMS; | |
1612 | ||
1613 | /* Sometimes a shared object will map in the symbol table. If | |
08a40648 AM |
1614 | SHF_ALLOC is set, and this is a shared object, then we also |
1615 | treat this section as a BFD section. We can not base the | |
1616 | decision purely on SHF_ALLOC, because that flag is sometimes | |
1617 | set in a relocatable object file, which would confuse the | |
1618 | linker. */ | |
252b5132 RH |
1619 | if ((hdr->sh_flags & SHF_ALLOC) != 0 |
1620 | && (abfd->flags & DYNAMIC) != 0 | |
6dc132d9 L |
1621 | && ! _bfd_elf_make_section_from_shdr (abfd, hdr, name, |
1622 | shindex)) | |
b34976b6 | 1623 | return FALSE; |
252b5132 | 1624 | |
1b3a8575 AM |
1625 | /* Go looking for SHT_SYMTAB_SHNDX too, since if there is one we |
1626 | can't read symbols without that section loaded as well. It | |
1627 | is most likely specified by the next section header. */ | |
1628 | if (elf_elfsections (abfd)[elf_symtab_shndx (abfd)]->sh_link != shindex) | |
1629 | { | |
1630 | unsigned int i, num_sec; | |
1631 | ||
1632 | num_sec = elf_numsections (abfd); | |
1633 | for (i = shindex + 1; i < num_sec; i++) | |
1634 | { | |
1635 | Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i]; | |
1636 | if (hdr2->sh_type == SHT_SYMTAB_SHNDX | |
1637 | && hdr2->sh_link == shindex) | |
1638 | break; | |
1639 | } | |
1640 | if (i == num_sec) | |
1641 | for (i = 1; i < shindex; i++) | |
1642 | { | |
1643 | Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i]; | |
1644 | if (hdr2->sh_type == SHT_SYMTAB_SHNDX | |
1645 | && hdr2->sh_link == shindex) | |
1646 | break; | |
1647 | } | |
1648 | if (i != shindex) | |
1649 | return bfd_section_from_shdr (abfd, i); | |
1650 | } | |
b34976b6 | 1651 | return TRUE; |
252b5132 RH |
1652 | |
1653 | case SHT_DYNSYM: /* A dynamic symbol table */ | |
1654 | if (elf_dynsymtab (abfd) == shindex) | |
b34976b6 | 1655 | return TRUE; |
252b5132 | 1656 | |
a50b2160 JJ |
1657 | if (hdr->sh_entsize != bed->s->sizeof_sym) |
1658 | return FALSE; | |
252b5132 RH |
1659 | BFD_ASSERT (elf_dynsymtab (abfd) == 0); |
1660 | elf_dynsymtab (abfd) = shindex; | |
1661 | elf_tdata (abfd)->dynsymtab_hdr = *hdr; | |
1662 | elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->dynsymtab_hdr; | |
1663 | abfd->flags |= HAS_SYMS; | |
1664 | ||
1665 | /* Besides being a symbol table, we also treat this as a regular | |
1666 | section, so that objcopy can handle it. */ | |
6dc132d9 | 1667 | return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); |
252b5132 | 1668 | |
9ad5cbcf AM |
1669 | case SHT_SYMTAB_SHNDX: /* Symbol section indices when >64k sections */ |
1670 | if (elf_symtab_shndx (abfd) == shindex) | |
b34976b6 | 1671 | return TRUE; |
9ad5cbcf | 1672 | |
1b3a8575 | 1673 | BFD_ASSERT (elf_symtab_shndx (abfd) == 0); |
9ad5cbcf AM |
1674 | elf_symtab_shndx (abfd) = shindex; |
1675 | elf_tdata (abfd)->symtab_shndx_hdr = *hdr; | |
1676 | elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->symtab_shndx_hdr; | |
b34976b6 | 1677 | return TRUE; |
9ad5cbcf | 1678 | |
252b5132 RH |
1679 | case SHT_STRTAB: /* A string table */ |
1680 | if (hdr->bfd_section != NULL) | |
b34976b6 | 1681 | return TRUE; |
252b5132 RH |
1682 | if (ehdr->e_shstrndx == shindex) |
1683 | { | |
1684 | elf_tdata (abfd)->shstrtab_hdr = *hdr; | |
1685 | elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->shstrtab_hdr; | |
b34976b6 | 1686 | return TRUE; |
252b5132 | 1687 | } |
1b3a8575 AM |
1688 | if (elf_elfsections (abfd)[elf_onesymtab (abfd)]->sh_link == shindex) |
1689 | { | |
1690 | symtab_strtab: | |
1691 | elf_tdata (abfd)->strtab_hdr = *hdr; | |
1692 | elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->strtab_hdr; | |
1693 | return TRUE; | |
1694 | } | |
1695 | if (elf_elfsections (abfd)[elf_dynsymtab (abfd)]->sh_link == shindex) | |
1696 | { | |
1697 | dynsymtab_strtab: | |
1698 | elf_tdata (abfd)->dynstrtab_hdr = *hdr; | |
1699 | hdr = &elf_tdata (abfd)->dynstrtab_hdr; | |
1700 | elf_elfsections (abfd)[shindex] = hdr; | |
1701 | /* We also treat this as a regular section, so that objcopy | |
1702 | can handle it. */ | |
6dc132d9 L |
1703 | return _bfd_elf_make_section_from_shdr (abfd, hdr, name, |
1704 | shindex); | |
1b3a8575 | 1705 | } |
252b5132 | 1706 | |
1b3a8575 AM |
1707 | /* If the string table isn't one of the above, then treat it as a |
1708 | regular section. We need to scan all the headers to be sure, | |
1709 | just in case this strtab section appeared before the above. */ | |
1710 | if (elf_onesymtab (abfd) == 0 || elf_dynsymtab (abfd) == 0) | |
1711 | { | |
1712 | unsigned int i, num_sec; | |
252b5132 | 1713 | |
1b3a8575 AM |
1714 | num_sec = elf_numsections (abfd); |
1715 | for (i = 1; i < num_sec; i++) | |
1716 | { | |
1717 | Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i]; | |
1718 | if (hdr2->sh_link == shindex) | |
1719 | { | |
933d961a JJ |
1720 | /* Prevent endless recursion on broken objects. */ |
1721 | if (i == shindex) | |
1722 | return FALSE; | |
1b3a8575 AM |
1723 | if (! bfd_section_from_shdr (abfd, i)) |
1724 | return FALSE; | |
1725 | if (elf_onesymtab (abfd) == i) | |
1726 | goto symtab_strtab; | |
1727 | if (elf_dynsymtab (abfd) == i) | |
1728 | goto dynsymtab_strtab; | |
1729 | } | |
1730 | } | |
1731 | } | |
6dc132d9 | 1732 | return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); |
252b5132 RH |
1733 | |
1734 | case SHT_REL: | |
1735 | case SHT_RELA: | |
1736 | /* *These* do a lot of work -- but build no sections! */ | |
1737 | { | |
1738 | asection *target_sect; | |
1739 | Elf_Internal_Shdr *hdr2; | |
9ad5cbcf | 1740 | unsigned int num_sec = elf_numsections (abfd); |
252b5132 | 1741 | |
aa2ca951 JJ |
1742 | if (hdr->sh_entsize |
1743 | != (bfd_size_type) (hdr->sh_type == SHT_REL | |
a50b2160 JJ |
1744 | ? bed->s->sizeof_rel : bed->s->sizeof_rela)) |
1745 | return FALSE; | |
1746 | ||
03ae5f59 | 1747 | /* Check for a bogus link to avoid crashing. */ |
4fbb74a6 | 1748 | if (hdr->sh_link >= num_sec) |
03ae5f59 ILT |
1749 | { |
1750 | ((*_bfd_error_handler) | |
d003868e AM |
1751 | (_("%B: invalid link %lu for reloc section %s (index %u)"), |
1752 | abfd, hdr->sh_link, name, shindex)); | |
6dc132d9 L |
1753 | return _bfd_elf_make_section_from_shdr (abfd, hdr, name, |
1754 | shindex); | |
03ae5f59 ILT |
1755 | } |
1756 | ||
252b5132 RH |
1757 | /* For some incomprehensible reason Oracle distributes |
1758 | libraries for Solaris in which some of the objects have | |
1759 | bogus sh_link fields. It would be nice if we could just | |
1760 | reject them, but, unfortunately, some people need to use | |
1761 | them. We scan through the section headers; if we find only | |
1762 | one suitable symbol table, we clobber the sh_link to point | |
1763 | to it. I hope this doesn't break anything. */ | |
1764 | if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_SYMTAB | |
1765 | && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_DYNSYM) | |
1766 | { | |
9ad5cbcf | 1767 | unsigned int scan; |
252b5132 RH |
1768 | int found; |
1769 | ||
1770 | found = 0; | |
9ad5cbcf | 1771 | for (scan = 1; scan < num_sec; scan++) |
252b5132 RH |
1772 | { |
1773 | if (elf_elfsections (abfd)[scan]->sh_type == SHT_SYMTAB | |
1774 | || elf_elfsections (abfd)[scan]->sh_type == SHT_DYNSYM) | |
1775 | { | |
1776 | if (found != 0) | |
1777 | { | |
1778 | found = 0; | |
1779 | break; | |
1780 | } | |
1781 | found = scan; | |
1782 | } | |
1783 | } | |
1784 | if (found != 0) | |
1785 | hdr->sh_link = found; | |
1786 | } | |
1787 | ||
1788 | /* Get the symbol table. */ | |
1b3a8575 AM |
1789 | if ((elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_SYMTAB |
1790 | || elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_DYNSYM) | |
252b5132 | 1791 | && ! bfd_section_from_shdr (abfd, hdr->sh_link)) |
b34976b6 | 1792 | return FALSE; |
252b5132 RH |
1793 | |
1794 | /* If this reloc section does not use the main symbol table we | |
1795 | don't treat it as a reloc section. BFD can't adequately | |
1796 | represent such a section, so at least for now, we don't | |
c044fabd | 1797 | try. We just present it as a normal section. We also |
60bcf0fa | 1798 | can't use it as a reloc section if it points to the null |
185ef66d AM |
1799 | section, an invalid section, or another reloc section. */ |
1800 | if (hdr->sh_link != elf_onesymtab (abfd) | |
1801 | || hdr->sh_info == SHN_UNDEF | |
185ef66d AM |
1802 | || hdr->sh_info >= num_sec |
1803 | || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_REL | |
1804 | || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_RELA) | |
6dc132d9 L |
1805 | return _bfd_elf_make_section_from_shdr (abfd, hdr, name, |
1806 | shindex); | |
252b5132 RH |
1807 | |
1808 | if (! bfd_section_from_shdr (abfd, hdr->sh_info)) | |
b34976b6 | 1809 | return FALSE; |
252b5132 RH |
1810 | target_sect = bfd_section_from_elf_index (abfd, hdr->sh_info); |
1811 | if (target_sect == NULL) | |
b34976b6 | 1812 | return FALSE; |
252b5132 RH |
1813 | |
1814 | if ((target_sect->flags & SEC_RELOC) == 0 | |
1815 | || target_sect->reloc_count == 0) | |
1816 | hdr2 = &elf_section_data (target_sect)->rel_hdr; | |
1817 | else | |
1818 | { | |
dc810e39 | 1819 | bfd_size_type amt; |
252b5132 | 1820 | BFD_ASSERT (elf_section_data (target_sect)->rel_hdr2 == NULL); |
dc810e39 | 1821 | amt = sizeof (*hdr2); |
217aa764 | 1822 | hdr2 = bfd_alloc (abfd, amt); |
14b1c01e AM |
1823 | if (hdr2 == NULL) |
1824 | return FALSE; | |
252b5132 RH |
1825 | elf_section_data (target_sect)->rel_hdr2 = hdr2; |
1826 | } | |
1827 | *hdr2 = *hdr; | |
1828 | elf_elfsections (abfd)[shindex] = hdr2; | |
d9bc7a44 | 1829 | target_sect->reloc_count += NUM_SHDR_ENTRIES (hdr); |
252b5132 RH |
1830 | target_sect->flags |= SEC_RELOC; |
1831 | target_sect->relocation = NULL; | |
1832 | target_sect->rel_filepos = hdr->sh_offset; | |
bf572ba0 MM |
1833 | /* In the section to which the relocations apply, mark whether |
1834 | its relocations are of the REL or RELA variety. */ | |
72730e0c | 1835 | if (hdr->sh_size != 0) |
68bfbfcc | 1836 | target_sect->use_rela_p = hdr->sh_type == SHT_RELA; |
252b5132 | 1837 | abfd->flags |= HAS_RELOC; |
b34976b6 | 1838 | return TRUE; |
252b5132 | 1839 | } |
252b5132 RH |
1840 | |
1841 | case SHT_GNU_verdef: | |
1842 | elf_dynverdef (abfd) = shindex; | |
1843 | elf_tdata (abfd)->dynverdef_hdr = *hdr; | |
6dc132d9 | 1844 | return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); |
252b5132 RH |
1845 | |
1846 | case SHT_GNU_versym: | |
a50b2160 JJ |
1847 | if (hdr->sh_entsize != sizeof (Elf_External_Versym)) |
1848 | return FALSE; | |
252b5132 RH |
1849 | elf_dynversym (abfd) = shindex; |
1850 | elf_tdata (abfd)->dynversym_hdr = *hdr; | |
6dc132d9 | 1851 | return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); |
252b5132 RH |
1852 | |
1853 | case SHT_GNU_verneed: | |
1854 | elf_dynverref (abfd) = shindex; | |
1855 | elf_tdata (abfd)->dynverref_hdr = *hdr; | |
6dc132d9 | 1856 | return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); |
252b5132 RH |
1857 | |
1858 | case SHT_SHLIB: | |
b34976b6 | 1859 | return TRUE; |
252b5132 | 1860 | |
dbb410c3 | 1861 | case SHT_GROUP: |
b885599b AM |
1862 | /* We need a BFD section for objcopy and relocatable linking, |
1863 | and it's handy to have the signature available as the section | |
1864 | name. */ | |
1783205a | 1865 | if (! IS_VALID_GROUP_SECTION_HEADER (hdr)) |
a50b2160 | 1866 | return FALSE; |
b885599b AM |
1867 | name = group_signature (abfd, hdr); |
1868 | if (name == NULL) | |
b34976b6 | 1869 | return FALSE; |
6dc132d9 | 1870 | if (!_bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex)) |
b34976b6 | 1871 | return FALSE; |
dbb410c3 AM |
1872 | if (hdr->contents != NULL) |
1873 | { | |
1874 | Elf_Internal_Group *idx = (Elf_Internal_Group *) hdr->contents; | |
1783205a | 1875 | unsigned int n_elt = hdr->sh_size / GRP_ENTRY_SIZE; |
dbb410c3 AM |
1876 | asection *s; |
1877 | ||
b885599b AM |
1878 | if (idx->flags & GRP_COMDAT) |
1879 | hdr->bfd_section->flags | |
1880 | |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD; | |
1881 | ||
45c5e9ed L |
1882 | /* We try to keep the same section order as it comes in. */ |
1883 | idx += n_elt; | |
dbb410c3 | 1884 | while (--n_elt != 0) |
1783205a NC |
1885 | { |
1886 | --idx; | |
1887 | ||
1888 | if (idx->shdr != NULL | |
1889 | && (s = idx->shdr->bfd_section) != NULL | |
1890 | && elf_next_in_group (s) != NULL) | |
1891 | { | |
1892 | elf_next_in_group (hdr->bfd_section) = s; | |
1893 | break; | |
1894 | } | |
1895 | } | |
dbb410c3 AM |
1896 | } |
1897 | break; | |
1898 | ||
252b5132 | 1899 | default: |
104d59d1 JM |
1900 | /* Possibly an attributes section. */ |
1901 | if (hdr->sh_type == SHT_GNU_ATTRIBUTES | |
1902 | || hdr->sh_type == bed->obj_attrs_section_type) | |
1903 | { | |
1904 | if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex)) | |
1905 | return FALSE; | |
1906 | _bfd_elf_parse_attributes (abfd, hdr); | |
1907 | return TRUE; | |
1908 | } | |
1909 | ||
252b5132 | 1910 | /* Check for any processor-specific section types. */ |
3eb70a79 L |
1911 | if (bed->elf_backend_section_from_shdr (abfd, hdr, name, shindex)) |
1912 | return TRUE; | |
1913 | ||
1914 | if (hdr->sh_type >= SHT_LOUSER && hdr->sh_type <= SHT_HIUSER) | |
1915 | { | |
1916 | if ((hdr->sh_flags & SHF_ALLOC) != 0) | |
1917 | /* FIXME: How to properly handle allocated section reserved | |
1918 | for applications? */ | |
1919 | (*_bfd_error_handler) | |
1920 | (_("%B: don't know how to handle allocated, application " | |
1921 | "specific section `%s' [0x%8x]"), | |
1922 | abfd, name, hdr->sh_type); | |
1923 | else | |
1924 | /* Allow sections reserved for applications. */ | |
1925 | return _bfd_elf_make_section_from_shdr (abfd, hdr, name, | |
1926 | shindex); | |
1927 | } | |
1928 | else if (hdr->sh_type >= SHT_LOPROC | |
1929 | && hdr->sh_type <= SHT_HIPROC) | |
1930 | /* FIXME: We should handle this section. */ | |
1931 | (*_bfd_error_handler) | |
1932 | (_("%B: don't know how to handle processor specific section " | |
1933 | "`%s' [0x%8x]"), | |
1934 | abfd, name, hdr->sh_type); | |
1935 | else if (hdr->sh_type >= SHT_LOOS && hdr->sh_type <= SHT_HIOS) | |
ff15b240 NC |
1936 | { |
1937 | /* Unrecognised OS-specific sections. */ | |
1938 | if ((hdr->sh_flags & SHF_OS_NONCONFORMING) != 0) | |
1939 | /* SHF_OS_NONCONFORMING indicates that special knowledge is | |
08a40648 | 1940 | required to correctly process the section and the file should |
ff15b240 NC |
1941 | be rejected with an error message. */ |
1942 | (*_bfd_error_handler) | |
1943 | (_("%B: don't know how to handle OS specific section " | |
1944 | "`%s' [0x%8x]"), | |
1945 | abfd, name, hdr->sh_type); | |
1946 | else | |
1947 | /* Otherwise it should be processed. */ | |
1948 | return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); | |
1949 | } | |
3eb70a79 L |
1950 | else |
1951 | /* FIXME: We should handle this section. */ | |
1952 | (*_bfd_error_handler) | |
1953 | (_("%B: don't know how to handle section `%s' [0x%8x]"), | |
1954 | abfd, name, hdr->sh_type); | |
1955 | ||
1956 | return FALSE; | |
252b5132 RH |
1957 | } |
1958 | ||
b34976b6 | 1959 | return TRUE; |
252b5132 RH |
1960 | } |
1961 | ||
ec338859 AM |
1962 | /* Return the section for the local symbol specified by ABFD, R_SYMNDX. |
1963 | Return SEC for sections that have no elf section, and NULL on error. */ | |
1964 | ||
1965 | asection * | |
217aa764 AM |
1966 | bfd_section_from_r_symndx (bfd *abfd, |
1967 | struct sym_sec_cache *cache, | |
1968 | asection *sec, | |
1969 | unsigned long r_symndx) | |
ec338859 | 1970 | { |
ec338859 | 1971 | unsigned int ent = r_symndx % LOCAL_SYM_CACHE_SIZE; |
a5d1b3b5 | 1972 | asection *s; |
ec338859 | 1973 | |
a5d1b3b5 AM |
1974 | if (cache->abfd != abfd || cache->indx[ent] != r_symndx) |
1975 | { | |
1976 | Elf_Internal_Shdr *symtab_hdr; | |
1977 | unsigned char esym[sizeof (Elf64_External_Sym)]; | |
1978 | Elf_External_Sym_Shndx eshndx; | |
1979 | Elf_Internal_Sym isym; | |
ec338859 | 1980 | |
a5d1b3b5 AM |
1981 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; |
1982 | if (bfd_elf_get_elf_syms (abfd, symtab_hdr, 1, r_symndx, | |
1983 | &isym, esym, &eshndx) == NULL) | |
1984 | return NULL; | |
9ad5cbcf | 1985 | |
a5d1b3b5 AM |
1986 | if (cache->abfd != abfd) |
1987 | { | |
1988 | memset (cache->indx, -1, sizeof (cache->indx)); | |
1989 | cache->abfd = abfd; | |
1990 | } | |
1991 | cache->indx[ent] = r_symndx; | |
1992 | cache->shndx[ent] = isym.st_shndx; | |
ec338859 | 1993 | } |
a5d1b3b5 AM |
1994 | |
1995 | s = bfd_section_from_elf_index (abfd, cache->shndx[ent]); | |
1996 | if (s != NULL) | |
1997 | return s; | |
1998 | ||
1999 | return sec; | |
ec338859 AM |
2000 | } |
2001 | ||
252b5132 RH |
2002 | /* Given an ELF section number, retrieve the corresponding BFD |
2003 | section. */ | |
2004 | ||
2005 | asection * | |
217aa764 | 2006 | bfd_section_from_elf_index (bfd *abfd, unsigned int index) |
252b5132 | 2007 | { |
9ad5cbcf | 2008 | if (index >= elf_numsections (abfd)) |
252b5132 RH |
2009 | return NULL; |
2010 | return elf_elfsections (abfd)[index]->bfd_section; | |
2011 | } | |
2012 | ||
b35d266b | 2013 | static const struct bfd_elf_special_section special_sections_b[] = |
2f89ff8d | 2014 | { |
0112cd26 NC |
2015 | { STRING_COMMA_LEN (".bss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE }, |
2016 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2017 | }; |
2018 | ||
b35d266b | 2019 | static const struct bfd_elf_special_section special_sections_c[] = |
7f4d3958 | 2020 | { |
0112cd26 NC |
2021 | { STRING_COMMA_LEN (".comment"), 0, SHT_PROGBITS, 0 }, |
2022 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2023 | }; |
2024 | ||
b35d266b | 2025 | static const struct bfd_elf_special_section special_sections_d[] = |
7f4d3958 | 2026 | { |
0112cd26 NC |
2027 | { STRING_COMMA_LEN (".data"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE }, |
2028 | { STRING_COMMA_LEN (".data1"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE }, | |
2029 | { STRING_COMMA_LEN (".debug"), 0, SHT_PROGBITS, 0 }, | |
2030 | { STRING_COMMA_LEN (".debug_line"), 0, SHT_PROGBITS, 0 }, | |
2031 | { STRING_COMMA_LEN (".debug_info"), 0, SHT_PROGBITS, 0 }, | |
2032 | { STRING_COMMA_LEN (".debug_abbrev"), 0, SHT_PROGBITS, 0 }, | |
2033 | { STRING_COMMA_LEN (".debug_aranges"), 0, SHT_PROGBITS, 0 }, | |
2034 | { STRING_COMMA_LEN (".dynamic"), 0, SHT_DYNAMIC, SHF_ALLOC }, | |
2035 | { STRING_COMMA_LEN (".dynstr"), 0, SHT_STRTAB, SHF_ALLOC }, | |
2036 | { STRING_COMMA_LEN (".dynsym"), 0, SHT_DYNSYM, SHF_ALLOC }, | |
2037 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2038 | }; |
2039 | ||
b35d266b | 2040 | static const struct bfd_elf_special_section special_sections_f[] = |
7f4d3958 | 2041 | { |
0112cd26 NC |
2042 | { STRING_COMMA_LEN (".fini"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR }, |
2043 | { STRING_COMMA_LEN (".fini_array"), 0, SHT_FINI_ARRAY, SHF_ALLOC + SHF_WRITE }, | |
2044 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2045 | }; |
2046 | ||
b35d266b | 2047 | static const struct bfd_elf_special_section special_sections_g[] = |
7f4d3958 | 2048 | { |
0112cd26 NC |
2049 | { STRING_COMMA_LEN (".gnu.linkonce.b"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE }, |
2050 | { STRING_COMMA_LEN (".got"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE }, | |
2051 | { STRING_COMMA_LEN (".gnu.version"), 0, SHT_GNU_versym, 0 }, | |
2052 | { STRING_COMMA_LEN (".gnu.version_d"), 0, SHT_GNU_verdef, 0 }, | |
2053 | { STRING_COMMA_LEN (".gnu.version_r"), 0, SHT_GNU_verneed, 0 }, | |
2054 | { STRING_COMMA_LEN (".gnu.liblist"), 0, SHT_GNU_LIBLIST, SHF_ALLOC }, | |
2055 | { STRING_COMMA_LEN (".gnu.conflict"), 0, SHT_RELA, SHF_ALLOC }, | |
2056 | { STRING_COMMA_LEN (".gnu.hash"), 0, SHT_GNU_HASH, SHF_ALLOC }, | |
2057 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2058 | }; |
2059 | ||
b35d266b | 2060 | static const struct bfd_elf_special_section special_sections_h[] = |
7f4d3958 | 2061 | { |
0112cd26 NC |
2062 | { STRING_COMMA_LEN (".hash"), 0, SHT_HASH, SHF_ALLOC }, |
2063 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2064 | }; |
2065 | ||
b35d266b | 2066 | static const struct bfd_elf_special_section special_sections_i[] = |
7f4d3958 | 2067 | { |
0112cd26 NC |
2068 | { STRING_COMMA_LEN (".init"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR }, |
2069 | { STRING_COMMA_LEN (".init_array"), 0, SHT_INIT_ARRAY, SHF_ALLOC + SHF_WRITE }, | |
2070 | { STRING_COMMA_LEN (".interp"), 0, SHT_PROGBITS, 0 }, | |
2071 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2072 | }; |
2073 | ||
b35d266b | 2074 | static const struct bfd_elf_special_section special_sections_l[] = |
7f4d3958 | 2075 | { |
0112cd26 NC |
2076 | { STRING_COMMA_LEN (".line"), 0, SHT_PROGBITS, 0 }, |
2077 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2078 | }; |
2079 | ||
b35d266b | 2080 | static const struct bfd_elf_special_section special_sections_n[] = |
7f4d3958 | 2081 | { |
0112cd26 NC |
2082 | { STRING_COMMA_LEN (".note.GNU-stack"), 0, SHT_PROGBITS, 0 }, |
2083 | { STRING_COMMA_LEN (".note"), -1, SHT_NOTE, 0 }, | |
2084 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2085 | }; |
2086 | ||
b35d266b | 2087 | static const struct bfd_elf_special_section special_sections_p[] = |
7f4d3958 | 2088 | { |
0112cd26 NC |
2089 | { STRING_COMMA_LEN (".preinit_array"), 0, SHT_PREINIT_ARRAY, SHF_ALLOC + SHF_WRITE }, |
2090 | { STRING_COMMA_LEN (".plt"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR }, | |
2091 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2092 | }; |
2093 | ||
b35d266b | 2094 | static const struct bfd_elf_special_section special_sections_r[] = |
7f4d3958 | 2095 | { |
0112cd26 NC |
2096 | { STRING_COMMA_LEN (".rodata"), -2, SHT_PROGBITS, SHF_ALLOC }, |
2097 | { STRING_COMMA_LEN (".rodata1"), 0, SHT_PROGBITS, SHF_ALLOC }, | |
2098 | { STRING_COMMA_LEN (".rela"), -1, SHT_RELA, 0 }, | |
2099 | { STRING_COMMA_LEN (".rel"), -1, SHT_REL, 0 }, | |
2100 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2101 | }; |
2102 | ||
b35d266b | 2103 | static const struct bfd_elf_special_section special_sections_s[] = |
7f4d3958 | 2104 | { |
0112cd26 NC |
2105 | { STRING_COMMA_LEN (".shstrtab"), 0, SHT_STRTAB, 0 }, |
2106 | { STRING_COMMA_LEN (".strtab"), 0, SHT_STRTAB, 0 }, | |
2107 | { STRING_COMMA_LEN (".symtab"), 0, SHT_SYMTAB, 0 }, | |
60ff4dc4 HPN |
2108 | /* See struct bfd_elf_special_section declaration for the semantics of |
2109 | this special case where .prefix_length != strlen (.prefix). */ | |
2110 | { ".stabstr", 5, 3, SHT_STRTAB, 0 }, | |
0112cd26 | 2111 | { NULL, 0, 0, 0, 0 } |
2f89ff8d L |
2112 | }; |
2113 | ||
b35d266b | 2114 | static const struct bfd_elf_special_section special_sections_t[] = |
7f4d3958 | 2115 | { |
0112cd26 NC |
2116 | { STRING_COMMA_LEN (".text"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR }, |
2117 | { STRING_COMMA_LEN (".tbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS }, | |
2118 | { STRING_COMMA_LEN (".tdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS }, | |
2119 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2120 | }; |
2121 | ||
b35d266b | 2122 | static const struct bfd_elf_special_section *special_sections[] = |
7f4d3958 | 2123 | { |
7f4d3958 | 2124 | special_sections_b, /* 'b' */ |
98ece1b3 | 2125 | special_sections_c, /* 'c' */ |
7f4d3958 L |
2126 | special_sections_d, /* 'd' */ |
2127 | NULL, /* 'e' */ | |
2128 | special_sections_f, /* 'f' */ | |
2129 | special_sections_g, /* 'g' */ | |
2130 | special_sections_h, /* 'h' */ | |
2131 | special_sections_i, /* 'i' */ | |
2132 | NULL, /* 'j' */ | |
2133 | NULL, /* 'k' */ | |
2134 | special_sections_l, /* 'l' */ | |
2135 | NULL, /* 'm' */ | |
2136 | special_sections_n, /* 'n' */ | |
2137 | NULL, /* 'o' */ | |
2138 | special_sections_p, /* 'p' */ | |
2139 | NULL, /* 'q' */ | |
2140 | special_sections_r, /* 'r' */ | |
2141 | special_sections_s, /* 's' */ | |
2142 | special_sections_t, /* 't' */ | |
7f4d3958 L |
2143 | }; |
2144 | ||
551b43fd AM |
2145 | const struct bfd_elf_special_section * |
2146 | _bfd_elf_get_special_section (const char *name, | |
2147 | const struct bfd_elf_special_section *spec, | |
2148 | unsigned int rela) | |
2f89ff8d L |
2149 | { |
2150 | int i; | |
7f4d3958 | 2151 | int len; |
7f4d3958 | 2152 | |
551b43fd | 2153 | len = strlen (name); |
7f4d3958 | 2154 | |
551b43fd | 2155 | for (i = 0; spec[i].prefix != NULL; i++) |
7dcb9820 AM |
2156 | { |
2157 | int suffix_len; | |
551b43fd | 2158 | int prefix_len = spec[i].prefix_length; |
7dcb9820 AM |
2159 | |
2160 | if (len < prefix_len) | |
2161 | continue; | |
551b43fd | 2162 | if (memcmp (name, spec[i].prefix, prefix_len) != 0) |
7dcb9820 AM |
2163 | continue; |
2164 | ||
551b43fd | 2165 | suffix_len = spec[i].suffix_length; |
7dcb9820 AM |
2166 | if (suffix_len <= 0) |
2167 | { | |
2168 | if (name[prefix_len] != 0) | |
2169 | { | |
2170 | if (suffix_len == 0) | |
2171 | continue; | |
2172 | if (name[prefix_len] != '.' | |
2173 | && (suffix_len == -2 | |
551b43fd | 2174 | || (rela && spec[i].type == SHT_REL))) |
7dcb9820 AM |
2175 | continue; |
2176 | } | |
2177 | } | |
2178 | else | |
2179 | { | |
2180 | if (len < prefix_len + suffix_len) | |
2181 | continue; | |
2182 | if (memcmp (name + len - suffix_len, | |
551b43fd | 2183 | spec[i].prefix + prefix_len, |
7dcb9820 AM |
2184 | suffix_len) != 0) |
2185 | continue; | |
2186 | } | |
551b43fd | 2187 | return &spec[i]; |
7dcb9820 | 2188 | } |
2f89ff8d L |
2189 | |
2190 | return NULL; | |
2191 | } | |
2192 | ||
7dcb9820 | 2193 | const struct bfd_elf_special_section * |
29ef7005 | 2194 | _bfd_elf_get_sec_type_attr (bfd *abfd, asection *sec) |
2f89ff8d | 2195 | { |
551b43fd AM |
2196 | int i; |
2197 | const struct bfd_elf_special_section *spec; | |
29ef7005 | 2198 | const struct elf_backend_data *bed; |
2f89ff8d L |
2199 | |
2200 | /* See if this is one of the special sections. */ | |
551b43fd AM |
2201 | if (sec->name == NULL) |
2202 | return NULL; | |
2f89ff8d | 2203 | |
29ef7005 L |
2204 | bed = get_elf_backend_data (abfd); |
2205 | spec = bed->special_sections; | |
2206 | if (spec) | |
2207 | { | |
2208 | spec = _bfd_elf_get_special_section (sec->name, | |
2209 | bed->special_sections, | |
2210 | sec->use_rela_p); | |
2211 | if (spec != NULL) | |
2212 | return spec; | |
2213 | } | |
2214 | ||
551b43fd AM |
2215 | if (sec->name[0] != '.') |
2216 | return NULL; | |
2f89ff8d | 2217 | |
551b43fd AM |
2218 | i = sec->name[1] - 'b'; |
2219 | if (i < 0 || i > 't' - 'b') | |
2220 | return NULL; | |
2221 | ||
2222 | spec = special_sections[i]; | |
2f89ff8d | 2223 | |
551b43fd AM |
2224 | if (spec == NULL) |
2225 | return NULL; | |
2226 | ||
2227 | return _bfd_elf_get_special_section (sec->name, spec, sec->use_rela_p); | |
2f89ff8d L |
2228 | } |
2229 | ||
b34976b6 | 2230 | bfd_boolean |
217aa764 | 2231 | _bfd_elf_new_section_hook (bfd *abfd, asection *sec) |
252b5132 RH |
2232 | { |
2233 | struct bfd_elf_section_data *sdata; | |
551b43fd | 2234 | const struct elf_backend_data *bed; |
7dcb9820 | 2235 | const struct bfd_elf_special_section *ssect; |
252b5132 | 2236 | |
f0abc2a1 AM |
2237 | sdata = (struct bfd_elf_section_data *) sec->used_by_bfd; |
2238 | if (sdata == NULL) | |
2239 | { | |
217aa764 | 2240 | sdata = bfd_zalloc (abfd, sizeof (*sdata)); |
f0abc2a1 AM |
2241 | if (sdata == NULL) |
2242 | return FALSE; | |
217aa764 | 2243 | sec->used_by_bfd = sdata; |
f0abc2a1 | 2244 | } |
bf572ba0 | 2245 | |
551b43fd AM |
2246 | /* Indicate whether or not this section should use RELA relocations. */ |
2247 | bed = get_elf_backend_data (abfd); | |
2248 | sec->use_rela_p = bed->default_use_rela_p; | |
2249 | ||
e843e0f8 L |
2250 | /* When we read a file, we don't need to set ELF section type and |
2251 | flags. They will be overridden in _bfd_elf_make_section_from_shdr | |
2252 | anyway. We will set ELF section type and flags for all linker | |
2253 | created sections. If user specifies BFD section flags, we will | |
2254 | set ELF section type and flags based on BFD section flags in | |
2255 | elf_fake_sections. */ | |
2256 | if ((!sec->flags && abfd->direction != read_direction) | |
3496cb2a | 2257 | || (sec->flags & SEC_LINKER_CREATED) != 0) |
2f89ff8d | 2258 | { |
551b43fd | 2259 | ssect = (*bed->get_sec_type_attr) (abfd, sec); |
a31501e9 L |
2260 | if (ssect != NULL) |
2261 | { | |
2262 | elf_section_type (sec) = ssect->type; | |
2263 | elf_section_flags (sec) = ssect->attr; | |
2264 | } | |
2f89ff8d L |
2265 | } |
2266 | ||
f592407e | 2267 | return _bfd_generic_new_section_hook (abfd, sec); |
252b5132 RH |
2268 | } |
2269 | ||
2270 | /* Create a new bfd section from an ELF program header. | |
2271 | ||
2272 | Since program segments have no names, we generate a synthetic name | |
2273 | of the form segment<NUM>, where NUM is generally the index in the | |
2274 | program header table. For segments that are split (see below) we | |
2275 | generate the names segment<NUM>a and segment<NUM>b. | |
2276 | ||
2277 | Note that some program segments may have a file size that is different than | |
2278 | (less than) the memory size. All this means is that at execution the | |
2279 | system must allocate the amount of memory specified by the memory size, | |
2280 | but only initialize it with the first "file size" bytes read from the | |
2281 | file. This would occur for example, with program segments consisting | |
2282 | of combined data+bss. | |
2283 | ||
2284 | To handle the above situation, this routine generates TWO bfd sections | |
2285 | for the single program segment. The first has the length specified by | |
2286 | the file size of the segment, and the second has the length specified | |
2287 | by the difference between the two sizes. In effect, the segment is split | |
d5191d0c | 2288 | into its initialized and uninitialized parts. |
252b5132 RH |
2289 | |
2290 | */ | |
2291 | ||
b34976b6 | 2292 | bfd_boolean |
217aa764 AM |
2293 | _bfd_elf_make_section_from_phdr (bfd *abfd, |
2294 | Elf_Internal_Phdr *hdr, | |
2295 | int index, | |
2296 | const char *typename) | |
252b5132 RH |
2297 | { |
2298 | asection *newsect; | |
2299 | char *name; | |
2300 | char namebuf[64]; | |
d4c88bbb | 2301 | size_t len; |
252b5132 RH |
2302 | int split; |
2303 | ||
2304 | split = ((hdr->p_memsz > 0) | |
2305 | && (hdr->p_filesz > 0) | |
2306 | && (hdr->p_memsz > hdr->p_filesz)); | |
d5191d0c AM |
2307 | |
2308 | if (hdr->p_filesz > 0) | |
252b5132 | 2309 | { |
d5191d0c AM |
2310 | sprintf (namebuf, "%s%d%s", typename, index, split ? "a" : ""); |
2311 | len = strlen (namebuf) + 1; | |
2312 | name = bfd_alloc (abfd, len); | |
2313 | if (!name) | |
2314 | return FALSE; | |
2315 | memcpy (name, namebuf, len); | |
2316 | newsect = bfd_make_section (abfd, name); | |
2317 | if (newsect == NULL) | |
2318 | return FALSE; | |
2319 | newsect->vma = hdr->p_vaddr; | |
2320 | newsect->lma = hdr->p_paddr; | |
2321 | newsect->size = hdr->p_filesz; | |
2322 | newsect->filepos = hdr->p_offset; | |
2323 | newsect->flags |= SEC_HAS_CONTENTS; | |
2324 | newsect->alignment_power = bfd_log2 (hdr->p_align); | |
2325 | if (hdr->p_type == PT_LOAD) | |
252b5132 | 2326 | { |
d5191d0c AM |
2327 | newsect->flags |= SEC_ALLOC; |
2328 | newsect->flags |= SEC_LOAD; | |
2329 | if (hdr->p_flags & PF_X) | |
2330 | { | |
2331 | /* FIXME: all we known is that it has execute PERMISSION, | |
2332 | may be data. */ | |
2333 | newsect->flags |= SEC_CODE; | |
2334 | } | |
2335 | } | |
2336 | if (!(hdr->p_flags & PF_W)) | |
2337 | { | |
2338 | newsect->flags |= SEC_READONLY; | |
252b5132 | 2339 | } |
252b5132 RH |
2340 | } |
2341 | ||
d5191d0c | 2342 | if (hdr->p_memsz > hdr->p_filesz) |
252b5132 | 2343 | { |
d5191d0c AM |
2344 | bfd_vma align; |
2345 | ||
2346 | sprintf (namebuf, "%s%d%s", typename, index, split ? "b" : ""); | |
d4c88bbb | 2347 | len = strlen (namebuf) + 1; |
217aa764 | 2348 | name = bfd_alloc (abfd, len); |
252b5132 | 2349 | if (!name) |
b34976b6 | 2350 | return FALSE; |
d4c88bbb | 2351 | memcpy (name, namebuf, len); |
252b5132 RH |
2352 | newsect = bfd_make_section (abfd, name); |
2353 | if (newsect == NULL) | |
b34976b6 | 2354 | return FALSE; |
252b5132 RH |
2355 | newsect->vma = hdr->p_vaddr + hdr->p_filesz; |
2356 | newsect->lma = hdr->p_paddr + hdr->p_filesz; | |
eea6121a | 2357 | newsect->size = hdr->p_memsz - hdr->p_filesz; |
d5191d0c AM |
2358 | newsect->filepos = hdr->p_offset + hdr->p_filesz; |
2359 | align = newsect->vma & -newsect->vma; | |
2360 | if (align == 0 || align > hdr->p_align) | |
2361 | align = hdr->p_align; | |
2362 | newsect->alignment_power = bfd_log2 (align); | |
252b5132 RH |
2363 | if (hdr->p_type == PT_LOAD) |
2364 | { | |
d5191d0c AM |
2365 | /* Hack for gdb. Segments that have not been modified do |
2366 | not have their contents written to a core file, on the | |
2367 | assumption that a debugger can find the contents in the | |
2368 | executable. We flag this case by setting the fake | |
2369 | section size to zero. Note that "real" bss sections will | |
2370 | always have their contents dumped to the core file. */ | |
2371 | if (bfd_get_format (abfd) == bfd_core) | |
2372 | newsect->size = 0; | |
252b5132 RH |
2373 | newsect->flags |= SEC_ALLOC; |
2374 | if (hdr->p_flags & PF_X) | |
2375 | newsect->flags |= SEC_CODE; | |
2376 | } | |
2377 | if (!(hdr->p_flags & PF_W)) | |
2378 | newsect->flags |= SEC_READONLY; | |
2379 | } | |
2380 | ||
b34976b6 | 2381 | return TRUE; |
252b5132 RH |
2382 | } |
2383 | ||
b34976b6 | 2384 | bfd_boolean |
217aa764 | 2385 | bfd_section_from_phdr (bfd *abfd, Elf_Internal_Phdr *hdr, int index) |
20cfcaae | 2386 | { |
9c5bfbb7 | 2387 | const struct elf_backend_data *bed; |
20cfcaae NC |
2388 | |
2389 | switch (hdr->p_type) | |
2390 | { | |
2391 | case PT_NULL: | |
2392 | return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "null"); | |
2393 | ||
2394 | case PT_LOAD: | |
2395 | return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "load"); | |
2396 | ||
2397 | case PT_DYNAMIC: | |
2398 | return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "dynamic"); | |
2399 | ||
2400 | case PT_INTERP: | |
2401 | return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "interp"); | |
2402 | ||
2403 | case PT_NOTE: | |
2404 | if (! _bfd_elf_make_section_from_phdr (abfd, hdr, index, "note")) | |
b34976b6 | 2405 | return FALSE; |
718175fa | 2406 | if (! elf_read_notes (abfd, hdr->p_offset, hdr->p_filesz)) |
b34976b6 AM |
2407 | return FALSE; |
2408 | return TRUE; | |
20cfcaae NC |
2409 | |
2410 | case PT_SHLIB: | |
2411 | return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "shlib"); | |
2412 | ||
2413 | case PT_PHDR: | |
2414 | return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "phdr"); | |
2415 | ||
811072d8 RM |
2416 | case PT_GNU_EH_FRAME: |
2417 | return _bfd_elf_make_section_from_phdr (abfd, hdr, index, | |
2418 | "eh_frame_hdr"); | |
2419 | ||
2b05f1b7 L |
2420 | case PT_GNU_STACK: |
2421 | return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "stack"); | |
9ee5e499 | 2422 | |
8c37241b JJ |
2423 | case PT_GNU_RELRO: |
2424 | return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "relro"); | |
2425 | ||
20cfcaae | 2426 | default: |
8c1acd09 | 2427 | /* Check for any processor-specific program segment types. */ |
20cfcaae | 2428 | bed = get_elf_backend_data (abfd); |
d27f5fa1 | 2429 | return bed->elf_backend_section_from_phdr (abfd, hdr, index, "proc"); |
20cfcaae NC |
2430 | } |
2431 | } | |
2432 | ||
23bc299b | 2433 | /* Initialize REL_HDR, the section-header for new section, containing |
b34976b6 | 2434 | relocations against ASECT. If USE_RELA_P is TRUE, we use RELA |
23bc299b MM |
2435 | relocations; otherwise, we use REL relocations. */ |
2436 | ||
b34976b6 | 2437 | bfd_boolean |
217aa764 AM |
2438 | _bfd_elf_init_reloc_shdr (bfd *abfd, |
2439 | Elf_Internal_Shdr *rel_hdr, | |
2440 | asection *asect, | |
2441 | bfd_boolean use_rela_p) | |
23bc299b MM |
2442 | { |
2443 | char *name; | |
9c5bfbb7 | 2444 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
dc810e39 | 2445 | bfd_size_type amt = sizeof ".rela" + strlen (asect->name); |
23bc299b | 2446 | |
dc810e39 | 2447 | name = bfd_alloc (abfd, amt); |
23bc299b | 2448 | if (name == NULL) |
b34976b6 | 2449 | return FALSE; |
23bc299b MM |
2450 | sprintf (name, "%s%s", use_rela_p ? ".rela" : ".rel", asect->name); |
2451 | rel_hdr->sh_name = | |
2b0f7ef9 | 2452 | (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), name, |
b34976b6 | 2453 | FALSE); |
23bc299b | 2454 | if (rel_hdr->sh_name == (unsigned int) -1) |
b34976b6 | 2455 | return FALSE; |
23bc299b MM |
2456 | rel_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL; |
2457 | rel_hdr->sh_entsize = (use_rela_p | |
2458 | ? bed->s->sizeof_rela | |
2459 | : bed->s->sizeof_rel); | |
72de5009 | 2460 | rel_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align; |
23bc299b MM |
2461 | rel_hdr->sh_flags = 0; |
2462 | rel_hdr->sh_addr = 0; | |
2463 | rel_hdr->sh_size = 0; | |
2464 | rel_hdr->sh_offset = 0; | |
2465 | ||
b34976b6 | 2466 | return TRUE; |
23bc299b MM |
2467 | } |
2468 | ||
252b5132 RH |
2469 | /* Set up an ELF internal section header for a section. */ |
2470 | ||
252b5132 | 2471 | static void |
217aa764 | 2472 | elf_fake_sections (bfd *abfd, asection *asect, void *failedptrarg) |
252b5132 | 2473 | { |
9c5bfbb7 | 2474 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
217aa764 | 2475 | bfd_boolean *failedptr = failedptrarg; |
252b5132 | 2476 | Elf_Internal_Shdr *this_hdr; |
0414f35b | 2477 | unsigned int sh_type; |
252b5132 RH |
2478 | |
2479 | if (*failedptr) | |
2480 | { | |
2481 | /* We already failed; just get out of the bfd_map_over_sections | |
08a40648 | 2482 | loop. */ |
252b5132 RH |
2483 | return; |
2484 | } | |
2485 | ||
2486 | this_hdr = &elf_section_data (asect)->this_hdr; | |
2487 | ||
e57b5356 AM |
2488 | this_hdr->sh_name = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), |
2489 | asect->name, FALSE); | |
2490 | if (this_hdr->sh_name == (unsigned int) -1) | |
252b5132 | 2491 | { |
b34976b6 | 2492 | *failedptr = TRUE; |
252b5132 RH |
2493 | return; |
2494 | } | |
2495 | ||
a4d8e49b | 2496 | /* Don't clear sh_flags. Assembler may set additional bits. */ |
252b5132 RH |
2497 | |
2498 | if ((asect->flags & SEC_ALLOC) != 0 | |
2499 | || asect->user_set_vma) | |
2500 | this_hdr->sh_addr = asect->vma; | |
2501 | else | |
2502 | this_hdr->sh_addr = 0; | |
2503 | ||
2504 | this_hdr->sh_offset = 0; | |
eea6121a | 2505 | this_hdr->sh_size = asect->size; |
252b5132 | 2506 | this_hdr->sh_link = 0; |
72de5009 | 2507 | this_hdr->sh_addralign = (bfd_vma) 1 << asect->alignment_power; |
252b5132 RH |
2508 | /* The sh_entsize and sh_info fields may have been set already by |
2509 | copy_private_section_data. */ | |
2510 | ||
2511 | this_hdr->bfd_section = asect; | |
2512 | this_hdr->contents = NULL; | |
2513 | ||
3cddba1e L |
2514 | /* If the section type is unspecified, we set it based on |
2515 | asect->flags. */ | |
98ece1b3 AM |
2516 | if ((asect->flags & SEC_GROUP) != 0) |
2517 | sh_type = SHT_GROUP; | |
2518 | else if ((asect->flags & SEC_ALLOC) != 0 | |
2519 | && (((asect->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0) | |
2520 | || (asect->flags & SEC_NEVER_LOAD) != 0)) | |
2521 | sh_type = SHT_NOBITS; | |
2522 | else | |
2523 | sh_type = SHT_PROGBITS; | |
2524 | ||
3cddba1e | 2525 | if (this_hdr->sh_type == SHT_NULL) |
98ece1b3 AM |
2526 | this_hdr->sh_type = sh_type; |
2527 | else if (this_hdr->sh_type == SHT_NOBITS | |
2528 | && sh_type == SHT_PROGBITS | |
2529 | && (asect->flags & SEC_ALLOC) != 0) | |
3cddba1e | 2530 | { |
98ece1b3 AM |
2531 | /* Warn if we are changing a NOBITS section to PROGBITS, but |
2532 | allow the link to proceed. This can happen when users link | |
2533 | non-bss input sections to bss output sections, or emit data | |
2534 | to a bss output section via a linker script. */ | |
2535 | (*_bfd_error_handler) | |
58f0869b | 2536 | (_("warning: section `%A' type changed to PROGBITS"), asect); |
98ece1b3 | 2537 | this_hdr->sh_type = sh_type; |
3cddba1e L |
2538 | } |
2539 | ||
2f89ff8d | 2540 | switch (this_hdr->sh_type) |
252b5132 | 2541 | { |
2f89ff8d | 2542 | default: |
2f89ff8d L |
2543 | break; |
2544 | ||
2545 | case SHT_STRTAB: | |
2546 | case SHT_INIT_ARRAY: | |
2547 | case SHT_FINI_ARRAY: | |
2548 | case SHT_PREINIT_ARRAY: | |
2549 | case SHT_NOTE: | |
2550 | case SHT_NOBITS: | |
2551 | case SHT_PROGBITS: | |
2552 | break; | |
2553 | ||
2554 | case SHT_HASH: | |
c7ac6ff8 | 2555 | this_hdr->sh_entsize = bed->s->sizeof_hash_entry; |
2f89ff8d | 2556 | break; |
5de3bf90 | 2557 | |
2f89ff8d | 2558 | case SHT_DYNSYM: |
252b5132 | 2559 | this_hdr->sh_entsize = bed->s->sizeof_sym; |
2f89ff8d L |
2560 | break; |
2561 | ||
2562 | case SHT_DYNAMIC: | |
252b5132 | 2563 | this_hdr->sh_entsize = bed->s->sizeof_dyn; |
2f89ff8d L |
2564 | break; |
2565 | ||
2566 | case SHT_RELA: | |
2567 | if (get_elf_backend_data (abfd)->may_use_rela_p) | |
2568 | this_hdr->sh_entsize = bed->s->sizeof_rela; | |
2569 | break; | |
2570 | ||
2571 | case SHT_REL: | |
2572 | if (get_elf_backend_data (abfd)->may_use_rel_p) | |
2573 | this_hdr->sh_entsize = bed->s->sizeof_rel; | |
2574 | break; | |
2575 | ||
2576 | case SHT_GNU_versym: | |
252b5132 | 2577 | this_hdr->sh_entsize = sizeof (Elf_External_Versym); |
2f89ff8d L |
2578 | break; |
2579 | ||
2580 | case SHT_GNU_verdef: | |
252b5132 RH |
2581 | this_hdr->sh_entsize = 0; |
2582 | /* objcopy or strip will copy over sh_info, but may not set | |
08a40648 AM |
2583 | cverdefs. The linker will set cverdefs, but sh_info will be |
2584 | zero. */ | |
252b5132 RH |
2585 | if (this_hdr->sh_info == 0) |
2586 | this_hdr->sh_info = elf_tdata (abfd)->cverdefs; | |
2587 | else | |
2588 | BFD_ASSERT (elf_tdata (abfd)->cverdefs == 0 | |
2589 | || this_hdr->sh_info == elf_tdata (abfd)->cverdefs); | |
2f89ff8d L |
2590 | break; |
2591 | ||
2592 | case SHT_GNU_verneed: | |
252b5132 RH |
2593 | this_hdr->sh_entsize = 0; |
2594 | /* objcopy or strip will copy over sh_info, but may not set | |
08a40648 AM |
2595 | cverrefs. The linker will set cverrefs, but sh_info will be |
2596 | zero. */ | |
252b5132 RH |
2597 | if (this_hdr->sh_info == 0) |
2598 | this_hdr->sh_info = elf_tdata (abfd)->cverrefs; | |
2599 | else | |
2600 | BFD_ASSERT (elf_tdata (abfd)->cverrefs == 0 | |
2601 | || this_hdr->sh_info == elf_tdata (abfd)->cverrefs); | |
2f89ff8d L |
2602 | break; |
2603 | ||
2604 | case SHT_GROUP: | |
1783205a | 2605 | this_hdr->sh_entsize = GRP_ENTRY_SIZE; |
2f89ff8d | 2606 | break; |
fdc90cb4 JJ |
2607 | |
2608 | case SHT_GNU_HASH: | |
2609 | this_hdr->sh_entsize = bed->s->arch_size == 64 ? 0 : 4; | |
2610 | break; | |
dbb410c3 | 2611 | } |
252b5132 RH |
2612 | |
2613 | if ((asect->flags & SEC_ALLOC) != 0) | |
2614 | this_hdr->sh_flags |= SHF_ALLOC; | |
2615 | if ((asect->flags & SEC_READONLY) == 0) | |
2616 | this_hdr->sh_flags |= SHF_WRITE; | |
2617 | if ((asect->flags & SEC_CODE) != 0) | |
2618 | this_hdr->sh_flags |= SHF_EXECINSTR; | |
f5fa8ca2 JJ |
2619 | if ((asect->flags & SEC_MERGE) != 0) |
2620 | { | |
2621 | this_hdr->sh_flags |= SHF_MERGE; | |
2622 | this_hdr->sh_entsize = asect->entsize; | |
2623 | if ((asect->flags & SEC_STRINGS) != 0) | |
2624 | this_hdr->sh_flags |= SHF_STRINGS; | |
2625 | } | |
1126897b | 2626 | if ((asect->flags & SEC_GROUP) == 0 && elf_group_name (asect) != NULL) |
dbb410c3 | 2627 | this_hdr->sh_flags |= SHF_GROUP; |
13ae64f3 | 2628 | if ((asect->flags & SEC_THREAD_LOCAL) != 0) |
704afa60 JJ |
2629 | { |
2630 | this_hdr->sh_flags |= SHF_TLS; | |
3a800eb9 AM |
2631 | if (asect->size == 0 |
2632 | && (asect->flags & SEC_HAS_CONTENTS) == 0) | |
704afa60 | 2633 | { |
3a800eb9 | 2634 | struct bfd_link_order *o = asect->map_tail.link_order; |
b34976b6 | 2635 | |
704afa60 | 2636 | this_hdr->sh_size = 0; |
3a800eb9 AM |
2637 | if (o != NULL) |
2638 | { | |
704afa60 | 2639 | this_hdr->sh_size = o->offset + o->size; |
3a800eb9 AM |
2640 | if (this_hdr->sh_size != 0) |
2641 | this_hdr->sh_type = SHT_NOBITS; | |
2642 | } | |
704afa60 JJ |
2643 | } |
2644 | } | |
252b5132 RH |
2645 | |
2646 | /* Check for processor-specific section types. */ | |
0414f35b | 2647 | sh_type = this_hdr->sh_type; |
e1fddb6b AO |
2648 | if (bed->elf_backend_fake_sections |
2649 | && !(*bed->elf_backend_fake_sections) (abfd, this_hdr, asect)) | |
b34976b6 | 2650 | *failedptr = TRUE; |
252b5132 | 2651 | |
42bb2e33 | 2652 | if (sh_type == SHT_NOBITS && asect->size != 0) |
0414f35b AM |
2653 | { |
2654 | /* Don't change the header type from NOBITS if we are being | |
42bb2e33 | 2655 | called for objcopy --only-keep-debug. */ |
0414f35b AM |
2656 | this_hdr->sh_type = sh_type; |
2657 | } | |
2658 | ||
252b5132 | 2659 | /* If the section has relocs, set up a section header for the |
23bc299b MM |
2660 | SHT_REL[A] section. If two relocation sections are required for |
2661 | this section, it is up to the processor-specific back-end to | |
c044fabd | 2662 | create the other. */ |
23bc299b | 2663 | if ((asect->flags & SEC_RELOC) != 0 |
c044fabd | 2664 | && !_bfd_elf_init_reloc_shdr (abfd, |
23bc299b | 2665 | &elf_section_data (asect)->rel_hdr, |
c044fabd | 2666 | asect, |
68bfbfcc | 2667 | asect->use_rela_p)) |
b34976b6 | 2668 | *failedptr = TRUE; |
252b5132 RH |
2669 | } |
2670 | ||
dbb410c3 AM |
2671 | /* Fill in the contents of a SHT_GROUP section. */ |
2672 | ||
1126897b | 2673 | void |
217aa764 | 2674 | bfd_elf_set_group_contents (bfd *abfd, asection *sec, void *failedptrarg) |
dbb410c3 | 2675 | { |
217aa764 | 2676 | bfd_boolean *failedptr = failedptrarg; |
dbb410c3 | 2677 | unsigned long symindx; |
9dce4196 | 2678 | asection *elt, *first; |
dbb410c3 | 2679 | unsigned char *loc; |
b34976b6 | 2680 | bfd_boolean gas; |
dbb410c3 | 2681 | |
7e4111ad L |
2682 | /* Ignore linker created group section. See elfNN_ia64_object_p in |
2683 | elfxx-ia64.c. */ | |
2684 | if (((sec->flags & (SEC_GROUP | SEC_LINKER_CREATED)) != SEC_GROUP) | |
dbb410c3 AM |
2685 | || *failedptr) |
2686 | return; | |
2687 | ||
1126897b AM |
2688 | symindx = 0; |
2689 | if (elf_group_id (sec) != NULL) | |
2690 | symindx = elf_group_id (sec)->udata.i; | |
2691 | ||
2692 | if (symindx == 0) | |
2693 | { | |
2694 | /* If called from the assembler, swap_out_syms will have set up | |
2695 | elf_section_syms; If called for "ld -r", use target_index. */ | |
2696 | if (elf_section_syms (abfd) != NULL) | |
2697 | symindx = elf_section_syms (abfd)[sec->index]->udata.i; | |
2698 | else | |
2699 | symindx = sec->target_index; | |
2700 | } | |
dbb410c3 AM |
2701 | elf_section_data (sec)->this_hdr.sh_info = symindx; |
2702 | ||
1126897b | 2703 | /* The contents won't be allocated for "ld -r" or objcopy. */ |
b34976b6 | 2704 | gas = TRUE; |
dbb410c3 AM |
2705 | if (sec->contents == NULL) |
2706 | { | |
b34976b6 | 2707 | gas = FALSE; |
eea6121a | 2708 | sec->contents = bfd_alloc (abfd, sec->size); |
9dce4196 AM |
2709 | |
2710 | /* Arrange for the section to be written out. */ | |
2711 | elf_section_data (sec)->this_hdr.contents = sec->contents; | |
dbb410c3 AM |
2712 | if (sec->contents == NULL) |
2713 | { | |
b34976b6 | 2714 | *failedptr = TRUE; |
dbb410c3 AM |
2715 | return; |
2716 | } | |
2717 | } | |
2718 | ||
eea6121a | 2719 | loc = sec->contents + sec->size; |
dbb410c3 | 2720 | |
9dce4196 AM |
2721 | /* Get the pointer to the first section in the group that gas |
2722 | squirreled away here. objcopy arranges for this to be set to the | |
2723 | start of the input section group. */ | |
2724 | first = elt = elf_next_in_group (sec); | |
dbb410c3 AM |
2725 | |
2726 | /* First element is a flag word. Rest of section is elf section | |
2727 | indices for all the sections of the group. Write them backwards | |
2728 | just to keep the group in the same order as given in .section | |
2729 | directives, not that it matters. */ | |
2730 | while (elt != NULL) | |
2731 | { | |
9dce4196 AM |
2732 | asection *s; |
2733 | unsigned int idx; | |
2734 | ||
dbb410c3 | 2735 | loc -= 4; |
9dce4196 AM |
2736 | s = elt; |
2737 | if (!gas) | |
2738 | s = s->output_section; | |
2739 | idx = 0; | |
2740 | if (s != NULL) | |
2741 | idx = elf_section_data (s)->this_idx; | |
2742 | H_PUT_32 (abfd, idx, loc); | |
945906ff | 2743 | elt = elf_next_in_group (elt); |
9dce4196 AM |
2744 | if (elt == first) |
2745 | break; | |
dbb410c3 AM |
2746 | } |
2747 | ||
3d7f7666 | 2748 | if ((loc -= 4) != sec->contents) |
9dce4196 | 2749 | abort (); |
dbb410c3 | 2750 | |
9dce4196 | 2751 | H_PUT_32 (abfd, sec->flags & SEC_LINK_ONCE ? GRP_COMDAT : 0, loc); |
dbb410c3 AM |
2752 | } |
2753 | ||
252b5132 RH |
2754 | /* Assign all ELF section numbers. The dummy first section is handled here |
2755 | too. The link/info pointers for the standard section types are filled | |
2756 | in here too, while we're at it. */ | |
2757 | ||
b34976b6 | 2758 | static bfd_boolean |
da9f89d4 | 2759 | assign_section_numbers (bfd *abfd, struct bfd_link_info *link_info) |
252b5132 RH |
2760 | { |
2761 | struct elf_obj_tdata *t = elf_tdata (abfd); | |
2762 | asection *sec; | |
2b0f7ef9 | 2763 | unsigned int section_number, secn; |
252b5132 | 2764 | Elf_Internal_Shdr **i_shdrp; |
47cc2cf5 | 2765 | struct bfd_elf_section_data *d; |
252b5132 RH |
2766 | |
2767 | section_number = 1; | |
2768 | ||
2b0f7ef9 JJ |
2769 | _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd)); |
2770 | ||
da9f89d4 L |
2771 | /* SHT_GROUP sections are in relocatable files only. */ |
2772 | if (link_info == NULL || link_info->relocatable) | |
252b5132 | 2773 | { |
da9f89d4 | 2774 | /* Put SHT_GROUP sections first. */ |
04dd1667 | 2775 | for (sec = abfd->sections; sec != NULL; sec = sec->next) |
47cc2cf5 | 2776 | { |
5daa8fe7 | 2777 | d = elf_section_data (sec); |
da9f89d4 L |
2778 | |
2779 | if (d->this_hdr.sh_type == SHT_GROUP) | |
08a40648 | 2780 | { |
5daa8fe7 | 2781 | if (sec->flags & SEC_LINKER_CREATED) |
da9f89d4 L |
2782 | { |
2783 | /* Remove the linker created SHT_GROUP sections. */ | |
5daa8fe7 | 2784 | bfd_section_list_remove (abfd, sec); |
da9f89d4 | 2785 | abfd->section_count--; |
da9f89d4 | 2786 | } |
08a40648 | 2787 | else |
4fbb74a6 | 2788 | d->this_idx = section_number++; |
da9f89d4 | 2789 | } |
47cc2cf5 PB |
2790 | } |
2791 | } | |
2792 | ||
2793 | for (sec = abfd->sections; sec; sec = sec->next) | |
2794 | { | |
2795 | d = elf_section_data (sec); | |
2796 | ||
2797 | if (d->this_hdr.sh_type != SHT_GROUP) | |
4fbb74a6 | 2798 | d->this_idx = section_number++; |
2b0f7ef9 | 2799 | _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->this_hdr.sh_name); |
252b5132 RH |
2800 | if ((sec->flags & SEC_RELOC) == 0) |
2801 | d->rel_idx = 0; | |
2802 | else | |
2b0f7ef9 JJ |
2803 | { |
2804 | d->rel_idx = section_number++; | |
2805 | _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel_hdr.sh_name); | |
2806 | } | |
23bc299b MM |
2807 | |
2808 | if (d->rel_hdr2) | |
2b0f7ef9 JJ |
2809 | { |
2810 | d->rel_idx2 = section_number++; | |
2811 | _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel_hdr2->sh_name); | |
2812 | } | |
23bc299b MM |
2813 | else |
2814 | d->rel_idx2 = 0; | |
252b5132 RH |
2815 | } |
2816 | ||
2817 | t->shstrtab_section = section_number++; | |
2b0f7ef9 | 2818 | _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->shstrtab_hdr.sh_name); |
252b5132 | 2819 | elf_elfheader (abfd)->e_shstrndx = t->shstrtab_section; |
252b5132 RH |
2820 | |
2821 | if (bfd_get_symcount (abfd) > 0) | |
2822 | { | |
2823 | t->symtab_section = section_number++; | |
2b0f7ef9 | 2824 | _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->symtab_hdr.sh_name); |
4fbb74a6 | 2825 | if (section_number > ((SHN_LORESERVE - 2) & 0xFFFF)) |
9ad5cbcf | 2826 | { |
9ad5cbcf AM |
2827 | t->symtab_shndx_section = section_number++; |
2828 | t->symtab_shndx_hdr.sh_name | |
2829 | = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), | |
b34976b6 | 2830 | ".symtab_shndx", FALSE); |
9ad5cbcf | 2831 | if (t->symtab_shndx_hdr.sh_name == (unsigned int) -1) |
b34976b6 | 2832 | return FALSE; |
9ad5cbcf | 2833 | } |
252b5132 | 2834 | t->strtab_section = section_number++; |
2b0f7ef9 | 2835 | _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->strtab_hdr.sh_name); |
252b5132 RH |
2836 | } |
2837 | ||
2b0f7ef9 JJ |
2838 | _bfd_elf_strtab_finalize (elf_shstrtab (abfd)); |
2839 | t->shstrtab_hdr.sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd)); | |
9ad5cbcf AM |
2840 | |
2841 | elf_numsections (abfd) = section_number; | |
252b5132 RH |
2842 | elf_elfheader (abfd)->e_shnum = section_number; |
2843 | ||
2844 | /* Set up the list of section header pointers, in agreement with the | |
2845 | indices. */ | |
d0fb9a8d | 2846 | i_shdrp = bfd_zalloc2 (abfd, section_number, sizeof (Elf_Internal_Shdr *)); |
252b5132 | 2847 | if (i_shdrp == NULL) |
b34976b6 | 2848 | return FALSE; |
252b5132 | 2849 | |
d0fb9a8d | 2850 | i_shdrp[0] = bfd_zalloc (abfd, sizeof (Elf_Internal_Shdr)); |
252b5132 RH |
2851 | if (i_shdrp[0] == NULL) |
2852 | { | |
2853 | bfd_release (abfd, i_shdrp); | |
b34976b6 | 2854 | return FALSE; |
252b5132 | 2855 | } |
252b5132 RH |
2856 | |
2857 | elf_elfsections (abfd) = i_shdrp; | |
2858 | ||
2859 | i_shdrp[t->shstrtab_section] = &t->shstrtab_hdr; | |
2860 | if (bfd_get_symcount (abfd) > 0) | |
2861 | { | |
2862 | i_shdrp[t->symtab_section] = &t->symtab_hdr; | |
4fbb74a6 | 2863 | if (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF)) |
9ad5cbcf AM |
2864 | { |
2865 | i_shdrp[t->symtab_shndx_section] = &t->symtab_shndx_hdr; | |
2866 | t->symtab_shndx_hdr.sh_link = t->symtab_section; | |
2867 | } | |
252b5132 RH |
2868 | i_shdrp[t->strtab_section] = &t->strtab_hdr; |
2869 | t->symtab_hdr.sh_link = t->strtab_section; | |
2870 | } | |
38ce5b11 | 2871 | |
252b5132 RH |
2872 | for (sec = abfd->sections; sec; sec = sec->next) |
2873 | { | |
2874 | struct bfd_elf_section_data *d = elf_section_data (sec); | |
2875 | asection *s; | |
2876 | const char *name; | |
2877 | ||
2878 | i_shdrp[d->this_idx] = &d->this_hdr; | |
2879 | if (d->rel_idx != 0) | |
2880 | i_shdrp[d->rel_idx] = &d->rel_hdr; | |
23bc299b MM |
2881 | if (d->rel_idx2 != 0) |
2882 | i_shdrp[d->rel_idx2] = d->rel_hdr2; | |
252b5132 RH |
2883 | |
2884 | /* Fill in the sh_link and sh_info fields while we're at it. */ | |
2885 | ||
2886 | /* sh_link of a reloc section is the section index of the symbol | |
2887 | table. sh_info is the section index of the section to which | |
2888 | the relocation entries apply. */ | |
2889 | if (d->rel_idx != 0) | |
2890 | { | |
2891 | d->rel_hdr.sh_link = t->symtab_section; | |
2892 | d->rel_hdr.sh_info = d->this_idx; | |
2893 | } | |
23bc299b MM |
2894 | if (d->rel_idx2 != 0) |
2895 | { | |
2896 | d->rel_hdr2->sh_link = t->symtab_section; | |
2897 | d->rel_hdr2->sh_info = d->this_idx; | |
2898 | } | |
252b5132 | 2899 | |
38ce5b11 L |
2900 | /* We need to set up sh_link for SHF_LINK_ORDER. */ |
2901 | if ((d->this_hdr.sh_flags & SHF_LINK_ORDER) != 0) | |
2902 | { | |
2903 | s = elf_linked_to_section (sec); | |
2904 | if (s) | |
38ce5b11 | 2905 | { |
f2876037 | 2906 | /* elf_linked_to_section points to the input section. */ |
ccd2ec6a | 2907 | if (link_info != NULL) |
38ce5b11 | 2908 | { |
f2876037 | 2909 | /* Check discarded linkonce section. */ |
ccd2ec6a | 2910 | if (elf_discarded_section (s)) |
38ce5b11 | 2911 | { |
ccd2ec6a L |
2912 | asection *kept; |
2913 | (*_bfd_error_handler) | |
2914 | (_("%B: sh_link of section `%A' points to discarded section `%A' of `%B'"), | |
2915 | abfd, d->this_hdr.bfd_section, | |
2916 | s, s->owner); | |
2917 | /* Point to the kept section if it has the same | |
2918 | size as the discarded one. */ | |
c0f00686 | 2919 | kept = _bfd_elf_check_kept_section (s, link_info); |
ccd2ec6a | 2920 | if (kept == NULL) |
185d09ad | 2921 | { |
ccd2ec6a L |
2922 | bfd_set_error (bfd_error_bad_value); |
2923 | return FALSE; | |
185d09ad | 2924 | } |
ccd2ec6a | 2925 | s = kept; |
38ce5b11 | 2926 | } |
e424ecc8 | 2927 | |
ccd2ec6a L |
2928 | s = s->output_section; |
2929 | BFD_ASSERT (s != NULL); | |
38ce5b11 | 2930 | } |
f2876037 L |
2931 | else |
2932 | { | |
2933 | /* Handle objcopy. */ | |
2934 | if (s->output_section == NULL) | |
2935 | { | |
2936 | (*_bfd_error_handler) | |
2937 | (_("%B: sh_link of section `%A' points to removed section `%A' of `%B'"), | |
2938 | abfd, d->this_hdr.bfd_section, s, s->owner); | |
2939 | bfd_set_error (bfd_error_bad_value); | |
2940 | return FALSE; | |
2941 | } | |
2942 | s = s->output_section; | |
2943 | } | |
ccd2ec6a L |
2944 | d->this_hdr.sh_link = elf_section_data (s)->this_idx; |
2945 | } | |
2946 | else | |
2947 | { | |
2948 | /* PR 290: | |
2949 | The Intel C compiler generates SHT_IA_64_UNWIND with | |
2950 | SHF_LINK_ORDER. But it doesn't set the sh_link or | |
2951 | sh_info fields. Hence we could get the situation | |
08a40648 | 2952 | where s is NULL. */ |
ccd2ec6a L |
2953 | const struct elf_backend_data *bed |
2954 | = get_elf_backend_data (abfd); | |
2955 | if (bed->link_order_error_handler) | |
2956 | bed->link_order_error_handler | |
2957 | (_("%B: warning: sh_link not set for section `%A'"), | |
2958 | abfd, sec); | |
38ce5b11 L |
2959 | } |
2960 | } | |
2961 | ||
252b5132 RH |
2962 | switch (d->this_hdr.sh_type) |
2963 | { | |
2964 | case SHT_REL: | |
2965 | case SHT_RELA: | |
2966 | /* A reloc section which we are treating as a normal BFD | |
2967 | section. sh_link is the section index of the symbol | |
2968 | table. sh_info is the section index of the section to | |
2969 | which the relocation entries apply. We assume that an | |
2970 | allocated reloc section uses the dynamic symbol table. | |
2971 | FIXME: How can we be sure? */ | |
2972 | s = bfd_get_section_by_name (abfd, ".dynsym"); | |
2973 | if (s != NULL) | |
2974 | d->this_hdr.sh_link = elf_section_data (s)->this_idx; | |
2975 | ||
2976 | /* We look up the section the relocs apply to by name. */ | |
2977 | name = sec->name; | |
2978 | if (d->this_hdr.sh_type == SHT_REL) | |
2979 | name += 4; | |
2980 | else | |
2981 | name += 5; | |
2982 | s = bfd_get_section_by_name (abfd, name); | |
2983 | if (s != NULL) | |
2984 | d->this_hdr.sh_info = elf_section_data (s)->this_idx; | |
2985 | break; | |
2986 | ||
2987 | case SHT_STRTAB: | |
2988 | /* We assume that a section named .stab*str is a stabs | |
2989 | string section. We look for a section with the same name | |
2990 | but without the trailing ``str'', and set its sh_link | |
2991 | field to point to this section. */ | |
0112cd26 | 2992 | if (CONST_STRNEQ (sec->name, ".stab") |
252b5132 RH |
2993 | && strcmp (sec->name + strlen (sec->name) - 3, "str") == 0) |
2994 | { | |
2995 | size_t len; | |
2996 | char *alc; | |
2997 | ||
2998 | len = strlen (sec->name); | |
217aa764 | 2999 | alc = bfd_malloc (len - 2); |
252b5132 | 3000 | if (alc == NULL) |
b34976b6 | 3001 | return FALSE; |
d4c88bbb | 3002 | memcpy (alc, sec->name, len - 3); |
252b5132 RH |
3003 | alc[len - 3] = '\0'; |
3004 | s = bfd_get_section_by_name (abfd, alc); | |
3005 | free (alc); | |
3006 | if (s != NULL) | |
3007 | { | |
3008 | elf_section_data (s)->this_hdr.sh_link = d->this_idx; | |
3009 | ||
3010 | /* This is a .stab section. */ | |
0594c12d AM |
3011 | if (elf_section_data (s)->this_hdr.sh_entsize == 0) |
3012 | elf_section_data (s)->this_hdr.sh_entsize | |
3013 | = 4 + 2 * bfd_get_arch_size (abfd) / 8; | |
252b5132 RH |
3014 | } |
3015 | } | |
3016 | break; | |
3017 | ||
3018 | case SHT_DYNAMIC: | |
3019 | case SHT_DYNSYM: | |
3020 | case SHT_GNU_verneed: | |
3021 | case SHT_GNU_verdef: | |
3022 | /* sh_link is the section header index of the string table | |
3023 | used for the dynamic entries, or the symbol table, or the | |
3024 | version strings. */ | |
3025 | s = bfd_get_section_by_name (abfd, ".dynstr"); | |
3026 | if (s != NULL) | |
3027 | d->this_hdr.sh_link = elf_section_data (s)->this_idx; | |
3028 | break; | |
3029 | ||
7f1204bb JJ |
3030 | case SHT_GNU_LIBLIST: |
3031 | /* sh_link is the section header index of the prelink library | |
08a40648 AM |
3032 | list used for the dynamic entries, or the symbol table, or |
3033 | the version strings. */ | |
7f1204bb JJ |
3034 | s = bfd_get_section_by_name (abfd, (sec->flags & SEC_ALLOC) |
3035 | ? ".dynstr" : ".gnu.libstr"); | |
3036 | if (s != NULL) | |
3037 | d->this_hdr.sh_link = elf_section_data (s)->this_idx; | |
3038 | break; | |
3039 | ||
252b5132 | 3040 | case SHT_HASH: |
fdc90cb4 | 3041 | case SHT_GNU_HASH: |
252b5132 RH |
3042 | case SHT_GNU_versym: |
3043 | /* sh_link is the section header index of the symbol table | |
3044 | this hash table or version table is for. */ | |
3045 | s = bfd_get_section_by_name (abfd, ".dynsym"); | |
3046 | if (s != NULL) | |
3047 | d->this_hdr.sh_link = elf_section_data (s)->this_idx; | |
3048 | break; | |
dbb410c3 AM |
3049 | |
3050 | case SHT_GROUP: | |
3051 | d->this_hdr.sh_link = t->symtab_section; | |
252b5132 RH |
3052 | } |
3053 | } | |
3054 | ||
2b0f7ef9 | 3055 | for (secn = 1; secn < section_number; ++secn) |
9ad5cbcf AM |
3056 | if (i_shdrp[secn] == NULL) |
3057 | i_shdrp[secn] = i_shdrp[0]; | |
3058 | else | |
3059 | i_shdrp[secn]->sh_name = _bfd_elf_strtab_offset (elf_shstrtab (abfd), | |
3060 | i_shdrp[secn]->sh_name); | |
b34976b6 | 3061 | return TRUE; |
252b5132 RH |
3062 | } |
3063 | ||
3064 | /* Map symbol from it's internal number to the external number, moving | |
3065 | all local symbols to be at the head of the list. */ | |
3066 | ||
5372391b | 3067 | static bfd_boolean |
217aa764 | 3068 | sym_is_global (bfd *abfd, asymbol *sym) |
252b5132 RH |
3069 | { |
3070 | /* If the backend has a special mapping, use it. */ | |
9c5bfbb7 | 3071 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
217aa764 AM |
3072 | if (bed->elf_backend_sym_is_global) |
3073 | return (*bed->elf_backend_sym_is_global) (abfd, sym); | |
252b5132 RH |
3074 | |
3075 | return ((sym->flags & (BSF_GLOBAL | BSF_WEAK)) != 0 | |
3076 | || bfd_is_und_section (bfd_get_section (sym)) | |
3077 | || bfd_is_com_section (bfd_get_section (sym))); | |
3078 | } | |
3079 | ||
5372391b AM |
3080 | /* Don't output section symbols for sections that are not going to be |
3081 | output. Also, don't output section symbols for reloc and other | |
3082 | special sections. */ | |
3083 | ||
3084 | static bfd_boolean | |
3085 | ignore_section_sym (bfd *abfd, asymbol *sym) | |
3086 | { | |
3087 | return ((sym->flags & BSF_SECTION_SYM) != 0 | |
3088 | && (sym->value != 0 | |
3089 | || (sym->section->owner != abfd | |
3090 | && (sym->section->output_section->owner != abfd | |
3091 | || sym->section->output_offset != 0)))); | |
3092 | } | |
3093 | ||
b34976b6 | 3094 | static bfd_boolean |
217aa764 | 3095 | elf_map_symbols (bfd *abfd) |
252b5132 | 3096 | { |
dc810e39 | 3097 | unsigned int symcount = bfd_get_symcount (abfd); |
252b5132 RH |
3098 | asymbol **syms = bfd_get_outsymbols (abfd); |
3099 | asymbol **sect_syms; | |
dc810e39 AM |
3100 | unsigned int num_locals = 0; |
3101 | unsigned int num_globals = 0; | |
3102 | unsigned int num_locals2 = 0; | |
3103 | unsigned int num_globals2 = 0; | |
252b5132 | 3104 | int max_index = 0; |
dc810e39 | 3105 | unsigned int idx; |
252b5132 RH |
3106 | asection *asect; |
3107 | asymbol **new_syms; | |
252b5132 RH |
3108 | |
3109 | #ifdef DEBUG | |
3110 | fprintf (stderr, "elf_map_symbols\n"); | |
3111 | fflush (stderr); | |
3112 | #endif | |
3113 | ||
252b5132 RH |
3114 | for (asect = abfd->sections; asect; asect = asect->next) |
3115 | { | |
3116 | if (max_index < asect->index) | |
3117 | max_index = asect->index; | |
3118 | } | |
3119 | ||
3120 | max_index++; | |
d0fb9a8d | 3121 | sect_syms = bfd_zalloc2 (abfd, max_index, sizeof (asymbol *)); |
252b5132 | 3122 | if (sect_syms == NULL) |
b34976b6 | 3123 | return FALSE; |
252b5132 | 3124 | elf_section_syms (abfd) = sect_syms; |
4e89ac30 | 3125 | elf_num_section_syms (abfd) = max_index; |
252b5132 | 3126 | |
079e9a2f AM |
3127 | /* Init sect_syms entries for any section symbols we have already |
3128 | decided to output. */ | |
252b5132 RH |
3129 | for (idx = 0; idx < symcount; idx++) |
3130 | { | |
dc810e39 | 3131 | asymbol *sym = syms[idx]; |
c044fabd | 3132 | |
252b5132 | 3133 | if ((sym->flags & BSF_SECTION_SYM) != 0 |
5372391b | 3134 | && !ignore_section_sym (abfd, sym)) |
252b5132 | 3135 | { |
5372391b | 3136 | asection *sec = sym->section; |
252b5132 | 3137 | |
5372391b AM |
3138 | if (sec->owner != abfd) |
3139 | sec = sec->output_section; | |
252b5132 | 3140 | |
5372391b | 3141 | sect_syms[sec->index] = syms[idx]; |
252b5132 RH |
3142 | } |
3143 | } | |
3144 | ||
252b5132 RH |
3145 | /* Classify all of the symbols. */ |
3146 | for (idx = 0; idx < symcount; idx++) | |
3147 | { | |
5372391b AM |
3148 | if (ignore_section_sym (abfd, syms[idx])) |
3149 | continue; | |
252b5132 RH |
3150 | if (!sym_is_global (abfd, syms[idx])) |
3151 | num_locals++; | |
3152 | else | |
3153 | num_globals++; | |
3154 | } | |
079e9a2f | 3155 | |
5372391b | 3156 | /* We will be adding a section symbol for each normal BFD section. Most |
079e9a2f AM |
3157 | sections will already have a section symbol in outsymbols, but |
3158 | eg. SHT_GROUP sections will not, and we need the section symbol mapped | |
3159 | at least in that case. */ | |
252b5132 RH |
3160 | for (asect = abfd->sections; asect; asect = asect->next) |
3161 | { | |
079e9a2f | 3162 | if (sect_syms[asect->index] == NULL) |
252b5132 | 3163 | { |
079e9a2f | 3164 | if (!sym_is_global (abfd, asect->symbol)) |
252b5132 RH |
3165 | num_locals++; |
3166 | else | |
3167 | num_globals++; | |
252b5132 RH |
3168 | } |
3169 | } | |
3170 | ||
3171 | /* Now sort the symbols so the local symbols are first. */ | |
d0fb9a8d | 3172 | new_syms = bfd_alloc2 (abfd, num_locals + num_globals, sizeof (asymbol *)); |
dc810e39 | 3173 | |
252b5132 | 3174 | if (new_syms == NULL) |
b34976b6 | 3175 | return FALSE; |
252b5132 RH |
3176 | |
3177 | for (idx = 0; idx < symcount; idx++) | |
3178 | { | |
3179 | asymbol *sym = syms[idx]; | |
dc810e39 | 3180 | unsigned int i; |
252b5132 | 3181 | |
5372391b AM |
3182 | if (ignore_section_sym (abfd, sym)) |
3183 | continue; | |
252b5132 RH |
3184 | if (!sym_is_global (abfd, sym)) |
3185 | i = num_locals2++; | |
3186 | else | |
3187 | i = num_locals + num_globals2++; | |
3188 | new_syms[i] = sym; | |
3189 | sym->udata.i = i + 1; | |
3190 | } | |
3191 | for (asect = abfd->sections; asect; asect = asect->next) | |
3192 | { | |
079e9a2f | 3193 | if (sect_syms[asect->index] == NULL) |
252b5132 | 3194 | { |
079e9a2f | 3195 | asymbol *sym = asect->symbol; |
dc810e39 | 3196 | unsigned int i; |
252b5132 | 3197 | |
079e9a2f | 3198 | sect_syms[asect->index] = sym; |
252b5132 RH |
3199 | if (!sym_is_global (abfd, sym)) |
3200 | i = num_locals2++; | |
3201 | else | |
3202 | i = num_locals + num_globals2++; | |
3203 | new_syms[i] = sym; | |
3204 | sym->udata.i = i + 1; | |
3205 | } | |
3206 | } | |
3207 | ||
3208 | bfd_set_symtab (abfd, new_syms, num_locals + num_globals); | |
3209 | ||
3210 | elf_num_locals (abfd) = num_locals; | |
3211 | elf_num_globals (abfd) = num_globals; | |
b34976b6 | 3212 | return TRUE; |
252b5132 RH |
3213 | } |
3214 | ||
3215 | /* Align to the maximum file alignment that could be required for any | |
3216 | ELF data structure. */ | |
3217 | ||
268b6b39 | 3218 | static inline file_ptr |
217aa764 | 3219 | align_file_position (file_ptr off, int align) |
252b5132 RH |
3220 | { |
3221 | return (off + align - 1) & ~(align - 1); | |
3222 | } | |
3223 | ||
3224 | /* Assign a file position to a section, optionally aligning to the | |
3225 | required section alignment. */ | |
3226 | ||
217aa764 AM |
3227 | file_ptr |
3228 | _bfd_elf_assign_file_position_for_section (Elf_Internal_Shdr *i_shdrp, | |
3229 | file_ptr offset, | |
3230 | bfd_boolean align) | |
252b5132 | 3231 | { |
72de5009 AM |
3232 | if (align && i_shdrp->sh_addralign > 1) |
3233 | offset = BFD_ALIGN (offset, i_shdrp->sh_addralign); | |
252b5132 RH |
3234 | i_shdrp->sh_offset = offset; |
3235 | if (i_shdrp->bfd_section != NULL) | |
3236 | i_shdrp->bfd_section->filepos = offset; | |
3237 | if (i_shdrp->sh_type != SHT_NOBITS) | |
3238 | offset += i_shdrp->sh_size; | |
3239 | return offset; | |
3240 | } | |
3241 | ||
3242 | /* Compute the file positions we are going to put the sections at, and | |
3243 | otherwise prepare to begin writing out the ELF file. If LINK_INFO | |
3244 | is not NULL, this is being called by the ELF backend linker. */ | |
3245 | ||
b34976b6 | 3246 | bfd_boolean |
217aa764 AM |
3247 | _bfd_elf_compute_section_file_positions (bfd *abfd, |
3248 | struct bfd_link_info *link_info) | |
252b5132 | 3249 | { |
9c5bfbb7 | 3250 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
b34976b6 | 3251 | bfd_boolean failed; |
4b6c0f2f | 3252 | struct bfd_strtab_hash *strtab = NULL; |
252b5132 RH |
3253 | Elf_Internal_Shdr *shstrtab_hdr; |
3254 | ||
3255 | if (abfd->output_has_begun) | |
b34976b6 | 3256 | return TRUE; |
252b5132 RH |
3257 | |
3258 | /* Do any elf backend specific processing first. */ | |
3259 | if (bed->elf_backend_begin_write_processing) | |
3260 | (*bed->elf_backend_begin_write_processing) (abfd, link_info); | |
3261 | ||
3262 | if (! prep_headers (abfd)) | |
b34976b6 | 3263 | return FALSE; |
252b5132 | 3264 | |
e6c51ed4 NC |
3265 | /* Post process the headers if necessary. */ |
3266 | if (bed->elf_backend_post_process_headers) | |
3267 | (*bed->elf_backend_post_process_headers) (abfd, link_info); | |
3268 | ||
b34976b6 | 3269 | failed = FALSE; |
252b5132 RH |
3270 | bfd_map_over_sections (abfd, elf_fake_sections, &failed); |
3271 | if (failed) | |
b34976b6 | 3272 | return FALSE; |
252b5132 | 3273 | |
da9f89d4 | 3274 | if (!assign_section_numbers (abfd, link_info)) |
b34976b6 | 3275 | return FALSE; |
252b5132 RH |
3276 | |
3277 | /* The backend linker builds symbol table information itself. */ | |
3278 | if (link_info == NULL && bfd_get_symcount (abfd) > 0) | |
3279 | { | |
3280 | /* Non-zero if doing a relocatable link. */ | |
3281 | int relocatable_p = ! (abfd->flags & (EXEC_P | DYNAMIC)); | |
3282 | ||
3283 | if (! swap_out_syms (abfd, &strtab, relocatable_p)) | |
b34976b6 | 3284 | return FALSE; |
252b5132 RH |
3285 | } |
3286 | ||
1126897b | 3287 | if (link_info == NULL) |
dbb410c3 | 3288 | { |
1126897b | 3289 | bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed); |
dbb410c3 | 3290 | if (failed) |
b34976b6 | 3291 | return FALSE; |
dbb410c3 AM |
3292 | } |
3293 | ||
252b5132 RH |
3294 | shstrtab_hdr = &elf_tdata (abfd)->shstrtab_hdr; |
3295 | /* sh_name was set in prep_headers. */ | |
3296 | shstrtab_hdr->sh_type = SHT_STRTAB; | |
3297 | shstrtab_hdr->sh_flags = 0; | |
3298 | shstrtab_hdr->sh_addr = 0; | |
2b0f7ef9 | 3299 | shstrtab_hdr->sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd)); |
252b5132 RH |
3300 | shstrtab_hdr->sh_entsize = 0; |
3301 | shstrtab_hdr->sh_link = 0; | |
3302 | shstrtab_hdr->sh_info = 0; | |
3303 | /* sh_offset is set in assign_file_positions_except_relocs. */ | |
3304 | shstrtab_hdr->sh_addralign = 1; | |
3305 | ||
c84fca4d | 3306 | if (!assign_file_positions_except_relocs (abfd, link_info)) |
b34976b6 | 3307 | return FALSE; |
252b5132 RH |
3308 | |
3309 | if (link_info == NULL && bfd_get_symcount (abfd) > 0) | |
3310 | { | |
3311 | file_ptr off; | |
3312 | Elf_Internal_Shdr *hdr; | |
3313 | ||
3314 | off = elf_tdata (abfd)->next_file_pos; | |
3315 | ||
3316 | hdr = &elf_tdata (abfd)->symtab_hdr; | |
b34976b6 | 3317 | off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE); |
252b5132 | 3318 | |
9ad5cbcf AM |
3319 | hdr = &elf_tdata (abfd)->symtab_shndx_hdr; |
3320 | if (hdr->sh_size != 0) | |
b34976b6 | 3321 | off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE); |
9ad5cbcf | 3322 | |
252b5132 | 3323 | hdr = &elf_tdata (abfd)->strtab_hdr; |
b34976b6 | 3324 | off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE); |
252b5132 RH |
3325 | |
3326 | elf_tdata (abfd)->next_file_pos = off; | |
3327 | ||
3328 | /* Now that we know where the .strtab section goes, write it | |
08a40648 | 3329 | out. */ |
252b5132 RH |
3330 | if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0 |
3331 | || ! _bfd_stringtab_emit (abfd, strtab)) | |
b34976b6 | 3332 | return FALSE; |
252b5132 RH |
3333 | _bfd_stringtab_free (strtab); |
3334 | } | |
3335 | ||
b34976b6 | 3336 | abfd->output_has_begun = TRUE; |
252b5132 | 3337 | |
b34976b6 | 3338 | return TRUE; |
252b5132 RH |
3339 | } |
3340 | ||
8ded5a0f AM |
3341 | /* Make an initial estimate of the size of the program header. If we |
3342 | get the number wrong here, we'll redo section placement. */ | |
3343 | ||
3344 | static bfd_size_type | |
3345 | get_program_header_size (bfd *abfd, struct bfd_link_info *info) | |
3346 | { | |
3347 | size_t segs; | |
3348 | asection *s; | |
2b05f1b7 | 3349 | const struct elf_backend_data *bed; |
8ded5a0f AM |
3350 | |
3351 | /* Assume we will need exactly two PT_LOAD segments: one for text | |
3352 | and one for data. */ | |
3353 | segs = 2; | |
3354 | ||
3355 | s = bfd_get_section_by_name (abfd, ".interp"); | |
3356 | if (s != NULL && (s->flags & SEC_LOAD) != 0) | |
3357 | { | |
3358 | /* If we have a loadable interpreter section, we need a | |
3359 | PT_INTERP segment. In this case, assume we also need a | |
3360 | PT_PHDR segment, although that may not be true for all | |
3361 | targets. */ | |
3362 | segs += 2; | |
3363 | } | |
3364 | ||
3365 | if (bfd_get_section_by_name (abfd, ".dynamic") != NULL) | |
3366 | { | |
3367 | /* We need a PT_DYNAMIC segment. */ | |
3368 | ++segs; | |
f210dcff | 3369 | } |
08a40648 | 3370 | |
f210dcff L |
3371 | if (info->relro) |
3372 | { | |
3373 | /* We need a PT_GNU_RELRO segment. */ | |
3374 | ++segs; | |
8ded5a0f AM |
3375 | } |
3376 | ||
3377 | if (elf_tdata (abfd)->eh_frame_hdr) | |
3378 | { | |
3379 | /* We need a PT_GNU_EH_FRAME segment. */ | |
3380 | ++segs; | |
3381 | } | |
3382 | ||
2b05f1b7 | 3383 | if (elf_tdata (abfd)->stack_flags) |
8ded5a0f | 3384 | { |
2b05f1b7 L |
3385 | /* We need a PT_GNU_STACK segment. */ |
3386 | ++segs; | |
3387 | } | |
94b11780 | 3388 | |
2b05f1b7 L |
3389 | for (s = abfd->sections; s != NULL; s = s->next) |
3390 | { | |
8ded5a0f | 3391 | if ((s->flags & SEC_LOAD) != 0 |
0112cd26 | 3392 | && CONST_STRNEQ (s->name, ".note")) |
8ded5a0f AM |
3393 | { |
3394 | /* We need a PT_NOTE segment. */ | |
3395 | ++segs; | |
1c5265b5 JJ |
3396 | /* Try to create just one PT_NOTE segment |
3397 | for all adjacent loadable .note* sections. | |
3398 | gABI requires that within a PT_NOTE segment | |
3399 | (and also inside of each SHT_NOTE section) | |
3400 | each note is padded to a multiple of 4 size, | |
3401 | so we check whether the sections are correctly | |
3402 | aligned. */ | |
3403 | if (s->alignment_power == 2) | |
3404 | while (s->next != NULL | |
3405 | && s->next->alignment_power == 2 | |
3406 | && (s->next->flags & SEC_LOAD) != 0 | |
3407 | && CONST_STRNEQ (s->next->name, ".note")) | |
3408 | s = s->next; | |
8ded5a0f AM |
3409 | } |
3410 | } | |
3411 | ||
3412 | for (s = abfd->sections; s != NULL; s = s->next) | |
3413 | { | |
3414 | if (s->flags & SEC_THREAD_LOCAL) | |
3415 | { | |
3416 | /* We need a PT_TLS segment. */ | |
3417 | ++segs; | |
3418 | break; | |
3419 | } | |
3420 | } | |
3421 | ||
3422 | /* Let the backend count up any program headers it might need. */ | |
2b05f1b7 | 3423 | bed = get_elf_backend_data (abfd); |
8ded5a0f AM |
3424 | if (bed->elf_backend_additional_program_headers) |
3425 | { | |
3426 | int a; | |
3427 | ||
3428 | a = (*bed->elf_backend_additional_program_headers) (abfd, info); | |
3429 | if (a == -1) | |
3430 | abort (); | |
3431 | segs += a; | |
3432 | } | |
3433 | ||
3434 | return segs * bed->s->sizeof_phdr; | |
3435 | } | |
3436 | ||
2ea37f1c NC |
3437 | /* Find the segment that contains the output_section of section. */ |
3438 | ||
3439 | Elf_Internal_Phdr * | |
3440 | _bfd_elf_find_segment_containing_section (bfd * abfd, asection * section) | |
3441 | { | |
3442 | struct elf_segment_map *m; | |
3443 | Elf_Internal_Phdr *p; | |
3444 | ||
3445 | for (m = elf_tdata (abfd)->segment_map, | |
3446 | p = elf_tdata (abfd)->phdr; | |
3447 | m != NULL; | |
3448 | m = m->next, p++) | |
3449 | { | |
3450 | int i; | |
3451 | ||
3452 | for (i = m->count - 1; i >= 0; i--) | |
3453 | if (m->sections[i] == section) | |
3454 | return p; | |
3455 | } | |
3456 | ||
3457 | return NULL; | |
3458 | } | |
3459 | ||
252b5132 RH |
3460 | /* Create a mapping from a set of sections to a program segment. */ |
3461 | ||
217aa764 AM |
3462 | static struct elf_segment_map * |
3463 | make_mapping (bfd *abfd, | |
3464 | asection **sections, | |
3465 | unsigned int from, | |
3466 | unsigned int to, | |
3467 | bfd_boolean phdr) | |
252b5132 RH |
3468 | { |
3469 | struct elf_segment_map *m; | |
3470 | unsigned int i; | |
3471 | asection **hdrpp; | |
dc810e39 | 3472 | bfd_size_type amt; |
252b5132 | 3473 | |
dc810e39 AM |
3474 | amt = sizeof (struct elf_segment_map); |
3475 | amt += (to - from - 1) * sizeof (asection *); | |
217aa764 | 3476 | m = bfd_zalloc (abfd, amt); |
252b5132 RH |
3477 | if (m == NULL) |
3478 | return NULL; | |
3479 | m->next = NULL; | |
3480 | m->p_type = PT_LOAD; | |
3481 | for (i = from, hdrpp = sections + from; i < to; i++, hdrpp++) | |
3482 | m->sections[i - from] = *hdrpp; | |
3483 | m->count = to - from; | |
3484 | ||
3485 | if (from == 0 && phdr) | |
3486 | { | |
3487 | /* Include the headers in the first PT_LOAD segment. */ | |
3488 | m->includes_filehdr = 1; | |
3489 | m->includes_phdrs = 1; | |
3490 | } | |
3491 | ||
3492 | return m; | |
3493 | } | |
3494 | ||
229fcec5 MM |
3495 | /* Create the PT_DYNAMIC segment, which includes DYNSEC. Returns NULL |
3496 | on failure. */ | |
3497 | ||
3498 | struct elf_segment_map * | |
3499 | _bfd_elf_make_dynamic_segment (bfd *abfd, asection *dynsec) | |
3500 | { | |
3501 | struct elf_segment_map *m; | |
3502 | ||
3503 | m = bfd_zalloc (abfd, sizeof (struct elf_segment_map)); | |
3504 | if (m == NULL) | |
3505 | return NULL; | |
3506 | m->next = NULL; | |
3507 | m->p_type = PT_DYNAMIC; | |
3508 | m->count = 1; | |
3509 | m->sections[0] = dynsec; | |
08a40648 | 3510 | |
229fcec5 MM |
3511 | return m; |
3512 | } | |
3513 | ||
8ded5a0f | 3514 | /* Possibly add or remove segments from the segment map. */ |
252b5132 | 3515 | |
b34976b6 | 3516 | static bfd_boolean |
3dea8fca AM |
3517 | elf_modify_segment_map (bfd *abfd, |
3518 | struct bfd_link_info *info, | |
3519 | bfd_boolean remove_empty_load) | |
252b5132 | 3520 | { |
252e386e | 3521 | struct elf_segment_map **m; |
8ded5a0f | 3522 | const struct elf_backend_data *bed; |
252b5132 | 3523 | |
8ded5a0f AM |
3524 | /* The placement algorithm assumes that non allocated sections are |
3525 | not in PT_LOAD segments. We ensure this here by removing such | |
3526 | sections from the segment map. We also remove excluded | |
252e386e AM |
3527 | sections. Finally, any PT_LOAD segment without sections is |
3528 | removed. */ | |
3529 | m = &elf_tdata (abfd)->segment_map; | |
3530 | while (*m) | |
8ded5a0f AM |
3531 | { |
3532 | unsigned int i, new_count; | |
252b5132 | 3533 | |
252e386e | 3534 | for (new_count = 0, i = 0; i < (*m)->count; i++) |
8ded5a0f | 3535 | { |
252e386e AM |
3536 | if (((*m)->sections[i]->flags & SEC_EXCLUDE) == 0 |
3537 | && (((*m)->sections[i]->flags & SEC_ALLOC) != 0 | |
3538 | || (*m)->p_type != PT_LOAD)) | |
8ded5a0f | 3539 | { |
252e386e AM |
3540 | (*m)->sections[new_count] = (*m)->sections[i]; |
3541 | new_count++; | |
8ded5a0f AM |
3542 | } |
3543 | } | |
252e386e | 3544 | (*m)->count = new_count; |
252b5132 | 3545 | |
3dea8fca | 3546 | if (remove_empty_load && (*m)->p_type == PT_LOAD && (*m)->count == 0) |
252e386e AM |
3547 | *m = (*m)->next; |
3548 | else | |
3549 | m = &(*m)->next; | |
8ded5a0f | 3550 | } |
252b5132 | 3551 | |
8ded5a0f AM |
3552 | bed = get_elf_backend_data (abfd); |
3553 | if (bed->elf_backend_modify_segment_map != NULL) | |
252b5132 | 3554 | { |
252e386e | 3555 | if (!(*bed->elf_backend_modify_segment_map) (abfd, info)) |
8ded5a0f | 3556 | return FALSE; |
252b5132 | 3557 | } |
252b5132 | 3558 | |
8ded5a0f AM |
3559 | return TRUE; |
3560 | } | |
252b5132 | 3561 | |
8ded5a0f | 3562 | /* Set up a mapping from BFD sections to program segments. */ |
252b5132 | 3563 | |
8ded5a0f AM |
3564 | bfd_boolean |
3565 | _bfd_elf_map_sections_to_segments (bfd *abfd, struct bfd_link_info *info) | |
3566 | { | |
3567 | unsigned int count; | |
3568 | struct elf_segment_map *m; | |
3569 | asection **sections = NULL; | |
3570 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
3dea8fca | 3571 | bfd_boolean no_user_phdrs; |
252b5132 | 3572 | |
3dea8fca AM |
3573 | no_user_phdrs = elf_tdata (abfd)->segment_map == NULL; |
3574 | if (no_user_phdrs && bfd_count_sections (abfd) != 0) | |
252b5132 | 3575 | { |
8ded5a0f AM |
3576 | asection *s; |
3577 | unsigned int i; | |
3578 | struct elf_segment_map *mfirst; | |
3579 | struct elf_segment_map **pm; | |
3580 | asection *last_hdr; | |
3581 | bfd_vma last_size; | |
3582 | unsigned int phdr_index; | |
3583 | bfd_vma maxpagesize; | |
3584 | asection **hdrpp; | |
3585 | bfd_boolean phdr_in_segment = TRUE; | |
3586 | bfd_boolean writable; | |
3587 | int tls_count = 0; | |
3588 | asection *first_tls = NULL; | |
3589 | asection *dynsec, *eh_frame_hdr; | |
3590 | bfd_size_type amt; | |
252b5132 | 3591 | |
8ded5a0f | 3592 | /* Select the allocated sections, and sort them. */ |
252b5132 | 3593 | |
8ded5a0f AM |
3594 | sections = bfd_malloc2 (bfd_count_sections (abfd), sizeof (asection *)); |
3595 | if (sections == NULL) | |
252b5132 | 3596 | goto error_return; |
252b5132 | 3597 | |
8ded5a0f AM |
3598 | i = 0; |
3599 | for (s = abfd->sections; s != NULL; s = s->next) | |
3600 | { | |
3601 | if ((s->flags & SEC_ALLOC) != 0) | |
3602 | { | |
3603 | sections[i] = s; | |
3604 | ++i; | |
3605 | } | |
3606 | } | |
3607 | BFD_ASSERT (i <= bfd_count_sections (abfd)); | |
3608 | count = i; | |
252b5132 | 3609 | |
8ded5a0f | 3610 | qsort (sections, (size_t) count, sizeof (asection *), elf_sort_sections); |
252b5132 | 3611 | |
8ded5a0f | 3612 | /* Build the mapping. */ |
252b5132 | 3613 | |
8ded5a0f AM |
3614 | mfirst = NULL; |
3615 | pm = &mfirst; | |
252b5132 | 3616 | |
8ded5a0f AM |
3617 | /* If we have a .interp section, then create a PT_PHDR segment for |
3618 | the program headers and a PT_INTERP segment for the .interp | |
3619 | section. */ | |
3620 | s = bfd_get_section_by_name (abfd, ".interp"); | |
3621 | if (s != NULL && (s->flags & SEC_LOAD) != 0) | |
3622 | { | |
3623 | amt = sizeof (struct elf_segment_map); | |
3624 | m = bfd_zalloc (abfd, amt); | |
3625 | if (m == NULL) | |
3626 | goto error_return; | |
3627 | m->next = NULL; | |
3628 | m->p_type = PT_PHDR; | |
3629 | /* FIXME: UnixWare and Solaris set PF_X, Irix 5 does not. */ | |
3630 | m->p_flags = PF_R | PF_X; | |
3631 | m->p_flags_valid = 1; | |
3632 | m->includes_phdrs = 1; | |
252b5132 | 3633 | |
8ded5a0f AM |
3634 | *pm = m; |
3635 | pm = &m->next; | |
252b5132 | 3636 | |
8ded5a0f AM |
3637 | amt = sizeof (struct elf_segment_map); |
3638 | m = bfd_zalloc (abfd, amt); | |
3639 | if (m == NULL) | |
3640 | goto error_return; | |
3641 | m->next = NULL; | |
3642 | m->p_type = PT_INTERP; | |
3643 | m->count = 1; | |
3644 | m->sections[0] = s; | |
3645 | ||
3646 | *pm = m; | |
3647 | pm = &m->next; | |
252b5132 | 3648 | } |
8ded5a0f AM |
3649 | |
3650 | /* Look through the sections. We put sections in the same program | |
3651 | segment when the start of the second section can be placed within | |
3652 | a few bytes of the end of the first section. */ | |
3653 | last_hdr = NULL; | |
3654 | last_size = 0; | |
3655 | phdr_index = 0; | |
3656 | maxpagesize = bed->maxpagesize; | |
3657 | writable = FALSE; | |
3658 | dynsec = bfd_get_section_by_name (abfd, ".dynamic"); | |
3659 | if (dynsec != NULL | |
3660 | && (dynsec->flags & SEC_LOAD) == 0) | |
3661 | dynsec = NULL; | |
3662 | ||
3663 | /* Deal with -Ttext or something similar such that the first section | |
3664 | is not adjacent to the program headers. This is an | |
3665 | approximation, since at this point we don't know exactly how many | |
3666 | program headers we will need. */ | |
3667 | if (count > 0) | |
252b5132 | 3668 | { |
8ded5a0f AM |
3669 | bfd_size_type phdr_size = elf_tdata (abfd)->program_header_size; |
3670 | ||
62d7a5f6 | 3671 | if (phdr_size == (bfd_size_type) -1) |
8ded5a0f AM |
3672 | phdr_size = get_program_header_size (abfd, info); |
3673 | if ((abfd->flags & D_PAGED) == 0 | |
3674 | || sections[0]->lma < phdr_size | |
3675 | || sections[0]->lma % maxpagesize < phdr_size % maxpagesize) | |
3676 | phdr_in_segment = FALSE; | |
252b5132 RH |
3677 | } |
3678 | ||
8ded5a0f | 3679 | for (i = 0, hdrpp = sections; i < count; i++, hdrpp++) |
252b5132 | 3680 | { |
8ded5a0f AM |
3681 | asection *hdr; |
3682 | bfd_boolean new_segment; | |
3683 | ||
3684 | hdr = *hdrpp; | |
3685 | ||
3686 | /* See if this section and the last one will fit in the same | |
3687 | segment. */ | |
3688 | ||
3689 | if (last_hdr == NULL) | |
3690 | { | |
3691 | /* If we don't have a segment yet, then we don't need a new | |
3692 | one (we build the last one after this loop). */ | |
3693 | new_segment = FALSE; | |
3694 | } | |
3695 | else if (last_hdr->lma - last_hdr->vma != hdr->lma - hdr->vma) | |
3696 | { | |
3697 | /* If this section has a different relation between the | |
3698 | virtual address and the load address, then we need a new | |
3699 | segment. */ | |
3700 | new_segment = TRUE; | |
3701 | } | |
3702 | else if (BFD_ALIGN (last_hdr->lma + last_size, maxpagesize) | |
3703 | < BFD_ALIGN (hdr->lma, maxpagesize)) | |
3704 | { | |
3705 | /* If putting this section in this segment would force us to | |
3706 | skip a page in the segment, then we need a new segment. */ | |
3707 | new_segment = TRUE; | |
3708 | } | |
3709 | else if ((last_hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0 | |
3710 | && (hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) != 0) | |
3711 | { | |
3712 | /* We don't want to put a loadable section after a | |
3713 | nonloadable section in the same segment. | |
3714 | Consider .tbss sections as loadable for this purpose. */ | |
3715 | new_segment = TRUE; | |
3716 | } | |
3717 | else if ((abfd->flags & D_PAGED) == 0) | |
3718 | { | |
3719 | /* If the file is not demand paged, which means that we | |
3720 | don't require the sections to be correctly aligned in the | |
3721 | file, then there is no other reason for a new segment. */ | |
3722 | new_segment = FALSE; | |
3723 | } | |
3724 | else if (! writable | |
3725 | && (hdr->flags & SEC_READONLY) == 0 | |
3726 | && (((last_hdr->lma + last_size - 1) | |
3727 | & ~(maxpagesize - 1)) | |
3728 | != (hdr->lma & ~(maxpagesize - 1)))) | |
3729 | { | |
3730 | /* We don't want to put a writable section in a read only | |
3731 | segment, unless they are on the same page in memory | |
3732 | anyhow. We already know that the last section does not | |
3733 | bring us past the current section on the page, so the | |
3734 | only case in which the new section is not on the same | |
3735 | page as the previous section is when the previous section | |
3736 | ends precisely on a page boundary. */ | |
3737 | new_segment = TRUE; | |
3738 | } | |
3739 | else | |
3740 | { | |
3741 | /* Otherwise, we can use the same segment. */ | |
3742 | new_segment = FALSE; | |
3743 | } | |
3744 | ||
2889e75b NC |
3745 | /* Allow interested parties a chance to override our decision. */ |
3746 | if (last_hdr && info->callbacks->override_segment_assignment) | |
3747 | new_segment = info->callbacks->override_segment_assignment (info, abfd, hdr, last_hdr, new_segment); | |
3748 | ||
8ded5a0f AM |
3749 | if (! new_segment) |
3750 | { | |
3751 | if ((hdr->flags & SEC_READONLY) == 0) | |
3752 | writable = TRUE; | |
3753 | last_hdr = hdr; | |
3754 | /* .tbss sections effectively have zero size. */ | |
3755 | if ((hdr->flags & (SEC_THREAD_LOCAL | SEC_LOAD)) | |
3756 | != SEC_THREAD_LOCAL) | |
3757 | last_size = hdr->size; | |
3758 | else | |
3759 | last_size = 0; | |
3760 | continue; | |
3761 | } | |
3762 | ||
3763 | /* We need a new program segment. We must create a new program | |
3764 | header holding all the sections from phdr_index until hdr. */ | |
3765 | ||
3766 | m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment); | |
3767 | if (m == NULL) | |
3768 | goto error_return; | |
3769 | ||
3770 | *pm = m; | |
3771 | pm = &m->next; | |
3772 | ||
252b5132 | 3773 | if ((hdr->flags & SEC_READONLY) == 0) |
b34976b6 | 3774 | writable = TRUE; |
8ded5a0f AM |
3775 | else |
3776 | writable = FALSE; | |
3777 | ||
baaff79e JJ |
3778 | last_hdr = hdr; |
3779 | /* .tbss sections effectively have zero size. */ | |
e5caec89 | 3780 | if ((hdr->flags & (SEC_THREAD_LOCAL | SEC_LOAD)) != SEC_THREAD_LOCAL) |
eea6121a | 3781 | last_size = hdr->size; |
baaff79e JJ |
3782 | else |
3783 | last_size = 0; | |
8ded5a0f AM |
3784 | phdr_index = i; |
3785 | phdr_in_segment = FALSE; | |
252b5132 RH |
3786 | } |
3787 | ||
8ded5a0f AM |
3788 | /* Create a final PT_LOAD program segment. */ |
3789 | if (last_hdr != NULL) | |
3790 | { | |
3791 | m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment); | |
3792 | if (m == NULL) | |
3793 | goto error_return; | |
252b5132 | 3794 | |
8ded5a0f AM |
3795 | *pm = m; |
3796 | pm = &m->next; | |
3797 | } | |
252b5132 | 3798 | |
8ded5a0f AM |
3799 | /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */ |
3800 | if (dynsec != NULL) | |
3801 | { | |
3802 | m = _bfd_elf_make_dynamic_segment (abfd, dynsec); | |
3803 | if (m == NULL) | |
3804 | goto error_return; | |
3805 | *pm = m; | |
3806 | pm = &m->next; | |
3807 | } | |
252b5132 | 3808 | |
1c5265b5 JJ |
3809 | /* For each batch of consecutive loadable .note sections, |
3810 | add a PT_NOTE segment. We don't use bfd_get_section_by_name, | |
3811 | because if we link together nonloadable .note sections and | |
3812 | loadable .note sections, we will generate two .note sections | |
3813 | in the output file. FIXME: Using names for section types is | |
3814 | bogus anyhow. */ | |
8ded5a0f AM |
3815 | for (s = abfd->sections; s != NULL; s = s->next) |
3816 | { | |
3817 | if ((s->flags & SEC_LOAD) != 0 | |
0112cd26 | 3818 | && CONST_STRNEQ (s->name, ".note")) |
8ded5a0f | 3819 | { |
1c5265b5 JJ |
3820 | asection *s2; |
3821 | unsigned count = 1; | |
8ded5a0f | 3822 | amt = sizeof (struct elf_segment_map); |
1c5265b5 JJ |
3823 | if (s->alignment_power == 2) |
3824 | for (s2 = s; s2->next != NULL; s2 = s2->next) | |
55b581a6 JJ |
3825 | { |
3826 | if (s2->next->alignment_power == 2 | |
3827 | && (s2->next->flags & SEC_LOAD) != 0 | |
3828 | && CONST_STRNEQ (s2->next->name, ".note") | |
3829 | && align_power (s2->vma + s2->size, 2) | |
3830 | == s2->next->vma) | |
3831 | count++; | |
3832 | else | |
3833 | break; | |
3834 | } | |
1c5265b5 | 3835 | amt += (count - 1) * sizeof (asection *); |
8ded5a0f AM |
3836 | m = bfd_zalloc (abfd, amt); |
3837 | if (m == NULL) | |
3838 | goto error_return; | |
3839 | m->next = NULL; | |
3840 | m->p_type = PT_NOTE; | |
1c5265b5 JJ |
3841 | m->count = count; |
3842 | while (count > 1) | |
3843 | { | |
3844 | m->sections[m->count - count--] = s; | |
3845 | BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0); | |
3846 | s = s->next; | |
3847 | } | |
3848 | m->sections[m->count - 1] = s; | |
3849 | BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0); | |
8ded5a0f AM |
3850 | *pm = m; |
3851 | pm = &m->next; | |
3852 | } | |
3853 | if (s->flags & SEC_THREAD_LOCAL) | |
3854 | { | |
3855 | if (! tls_count) | |
3856 | first_tls = s; | |
3857 | tls_count++; | |
3858 | } | |
3859 | } | |
252b5132 | 3860 | |
8ded5a0f AM |
3861 | /* If there are any SHF_TLS output sections, add PT_TLS segment. */ |
3862 | if (tls_count > 0) | |
3863 | { | |
3864 | int i; | |
252b5132 | 3865 | |
8ded5a0f AM |
3866 | amt = sizeof (struct elf_segment_map); |
3867 | amt += (tls_count - 1) * sizeof (asection *); | |
3868 | m = bfd_zalloc (abfd, amt); | |
3869 | if (m == NULL) | |
3870 | goto error_return; | |
3871 | m->next = NULL; | |
3872 | m->p_type = PT_TLS; | |
3873 | m->count = tls_count; | |
3874 | /* Mandated PF_R. */ | |
3875 | m->p_flags = PF_R; | |
3876 | m->p_flags_valid = 1; | |
3877 | for (i = 0; i < tls_count; ++i) | |
3878 | { | |
3879 | BFD_ASSERT (first_tls->flags & SEC_THREAD_LOCAL); | |
3880 | m->sections[i] = first_tls; | |
3881 | first_tls = first_tls->next; | |
3882 | } | |
252b5132 | 3883 | |
8ded5a0f AM |
3884 | *pm = m; |
3885 | pm = &m->next; | |
3886 | } | |
252b5132 | 3887 | |
8ded5a0f AM |
3888 | /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME |
3889 | segment. */ | |
3890 | eh_frame_hdr = elf_tdata (abfd)->eh_frame_hdr; | |
3891 | if (eh_frame_hdr != NULL | |
3892 | && (eh_frame_hdr->output_section->flags & SEC_LOAD) != 0) | |
252b5132 | 3893 | { |
dc810e39 | 3894 | amt = sizeof (struct elf_segment_map); |
217aa764 | 3895 | m = bfd_zalloc (abfd, amt); |
252b5132 RH |
3896 | if (m == NULL) |
3897 | goto error_return; | |
3898 | m->next = NULL; | |
8ded5a0f | 3899 | m->p_type = PT_GNU_EH_FRAME; |
252b5132 | 3900 | m->count = 1; |
8ded5a0f | 3901 | m->sections[0] = eh_frame_hdr->output_section; |
252b5132 RH |
3902 | |
3903 | *pm = m; | |
3904 | pm = &m->next; | |
3905 | } | |
13ae64f3 | 3906 | |
8ded5a0f | 3907 | if (elf_tdata (abfd)->stack_flags) |
13ae64f3 | 3908 | { |
8ded5a0f AM |
3909 | amt = sizeof (struct elf_segment_map); |
3910 | m = bfd_zalloc (abfd, amt); | |
3911 | if (m == NULL) | |
3912 | goto error_return; | |
3913 | m->next = NULL; | |
2b05f1b7 | 3914 | m->p_type = PT_GNU_STACK; |
8ded5a0f AM |
3915 | m->p_flags = elf_tdata (abfd)->stack_flags; |
3916 | m->p_flags_valid = 1; | |
252b5132 | 3917 | |
8ded5a0f AM |
3918 | *pm = m; |
3919 | pm = &m->next; | |
3920 | } | |
65765700 | 3921 | |
f210dcff | 3922 | if (info->relro) |
8ded5a0f | 3923 | { |
f210dcff L |
3924 | for (m = mfirst; m != NULL; m = m->next) |
3925 | { | |
3926 | if (m->p_type == PT_LOAD) | |
3927 | { | |
3928 | asection *last = m->sections[m->count - 1]; | |
3929 | bfd_vma vaddr = m->sections[0]->vma; | |
3930 | bfd_vma filesz = last->vma - vaddr + last->size; | |
65765700 | 3931 | |
f210dcff L |
3932 | if (vaddr < info->relro_end |
3933 | && vaddr >= info->relro_start | |
3934 | && (vaddr + filesz) >= info->relro_end) | |
3935 | break; | |
3936 | } | |
3937 | } | |
3938 | ||
3939 | /* Make a PT_GNU_RELRO segment only when it isn't empty. */ | |
3940 | if (m != NULL) | |
3941 | { | |
3942 | amt = sizeof (struct elf_segment_map); | |
3943 | m = bfd_zalloc (abfd, amt); | |
3944 | if (m == NULL) | |
3945 | goto error_return; | |
3946 | m->next = NULL; | |
3947 | m->p_type = PT_GNU_RELRO; | |
3948 | m->p_flags = PF_R; | |
3949 | m->p_flags_valid = 1; | |
3950 | ||
3951 | *pm = m; | |
3952 | pm = &m->next; | |
3953 | } | |
8ded5a0f | 3954 | } |
9ee5e499 | 3955 | |
8ded5a0f AM |
3956 | free (sections); |
3957 | elf_tdata (abfd)->segment_map = mfirst; | |
9ee5e499 JJ |
3958 | } |
3959 | ||
3dea8fca | 3960 | if (!elf_modify_segment_map (abfd, info, no_user_phdrs)) |
8ded5a0f | 3961 | return FALSE; |
8c37241b | 3962 | |
8ded5a0f AM |
3963 | for (count = 0, m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next) |
3964 | ++count; | |
3965 | elf_tdata (abfd)->program_header_size = count * bed->s->sizeof_phdr; | |
252b5132 | 3966 | |
b34976b6 | 3967 | return TRUE; |
252b5132 RH |
3968 | |
3969 | error_return: | |
3970 | if (sections != NULL) | |
3971 | free (sections); | |
b34976b6 | 3972 | return FALSE; |
252b5132 RH |
3973 | } |
3974 | ||
3975 | /* Sort sections by address. */ | |
3976 | ||
3977 | static int | |
217aa764 | 3978 | elf_sort_sections (const void *arg1, const void *arg2) |
252b5132 RH |
3979 | { |
3980 | const asection *sec1 = *(const asection **) arg1; | |
3981 | const asection *sec2 = *(const asection **) arg2; | |
eecdbe52 | 3982 | bfd_size_type size1, size2; |
252b5132 RH |
3983 | |
3984 | /* Sort by LMA first, since this is the address used to | |
3985 | place the section into a segment. */ | |
3986 | if (sec1->lma < sec2->lma) | |
3987 | return -1; | |
3988 | else if (sec1->lma > sec2->lma) | |
3989 | return 1; | |
3990 | ||
3991 | /* Then sort by VMA. Normally the LMA and the VMA will be | |
3992 | the same, and this will do nothing. */ | |
3993 | if (sec1->vma < sec2->vma) | |
3994 | return -1; | |
3995 | else if (sec1->vma > sec2->vma) | |
3996 | return 1; | |
3997 | ||
3998 | /* Put !SEC_LOAD sections after SEC_LOAD ones. */ | |
3999 | ||
07c6e936 | 4000 | #define TOEND(x) (((x)->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0) |
252b5132 RH |
4001 | |
4002 | if (TOEND (sec1)) | |
4003 | { | |
4004 | if (TOEND (sec2)) | |
00a7cdc5 NC |
4005 | { |
4006 | /* If the indicies are the same, do not return 0 | |
4007 | here, but continue to try the next comparison. */ | |
4008 | if (sec1->target_index - sec2->target_index != 0) | |
4009 | return sec1->target_index - sec2->target_index; | |
4010 | } | |
252b5132 RH |
4011 | else |
4012 | return 1; | |
4013 | } | |
00a7cdc5 | 4014 | else if (TOEND (sec2)) |
252b5132 RH |
4015 | return -1; |
4016 | ||
4017 | #undef TOEND | |
4018 | ||
00a7cdc5 NC |
4019 | /* Sort by size, to put zero sized sections |
4020 | before others at the same address. */ | |
252b5132 | 4021 | |
eea6121a AM |
4022 | size1 = (sec1->flags & SEC_LOAD) ? sec1->size : 0; |
4023 | size2 = (sec2->flags & SEC_LOAD) ? sec2->size : 0; | |
eecdbe52 JJ |
4024 | |
4025 | if (size1 < size2) | |
252b5132 | 4026 | return -1; |
eecdbe52 | 4027 | if (size1 > size2) |
252b5132 RH |
4028 | return 1; |
4029 | ||
4030 | return sec1->target_index - sec2->target_index; | |
4031 | } | |
4032 | ||
340b6d91 AC |
4033 | /* Ian Lance Taylor writes: |
4034 | ||
4035 | We shouldn't be using % with a negative signed number. That's just | |
4036 | not good. We have to make sure either that the number is not | |
4037 | negative, or that the number has an unsigned type. When the types | |
4038 | are all the same size they wind up as unsigned. When file_ptr is a | |
4039 | larger signed type, the arithmetic winds up as signed long long, | |
4040 | which is wrong. | |
4041 | ||
4042 | What we're trying to say here is something like ``increase OFF by | |
4043 | the least amount that will cause it to be equal to the VMA modulo | |
4044 | the page size.'' */ | |
4045 | /* In other words, something like: | |
4046 | ||
4047 | vma_offset = m->sections[0]->vma % bed->maxpagesize; | |
4048 | off_offset = off % bed->maxpagesize; | |
4049 | if (vma_offset < off_offset) | |
4050 | adjustment = vma_offset + bed->maxpagesize - off_offset; | |
4051 | else | |
4052 | adjustment = vma_offset - off_offset; | |
08a40648 | 4053 | |
340b6d91 AC |
4054 | which can can be collapsed into the expression below. */ |
4055 | ||
4056 | static file_ptr | |
4057 | vma_page_aligned_bias (bfd_vma vma, ufile_ptr off, bfd_vma maxpagesize) | |
4058 | { | |
4059 | return ((vma - off) % maxpagesize); | |
4060 | } | |
4061 | ||
6d33f217 L |
4062 | static void |
4063 | print_segment_map (const struct elf_segment_map *m) | |
4064 | { | |
4065 | unsigned int j; | |
4066 | const char *pt = get_segment_type (m->p_type); | |
4067 | char buf[32]; | |
4068 | ||
4069 | if (pt == NULL) | |
4070 | { | |
4071 | if (m->p_type >= PT_LOPROC && m->p_type <= PT_HIPROC) | |
4072 | sprintf (buf, "LOPROC+%7.7x", | |
4073 | (unsigned int) (m->p_type - PT_LOPROC)); | |
4074 | else if (m->p_type >= PT_LOOS && m->p_type <= PT_HIOS) | |
4075 | sprintf (buf, "LOOS+%7.7x", | |
4076 | (unsigned int) (m->p_type - PT_LOOS)); | |
4077 | else | |
4078 | snprintf (buf, sizeof (buf), "%8.8x", | |
4079 | (unsigned int) m->p_type); | |
4080 | pt = buf; | |
4081 | } | |
4082 | fprintf (stderr, "%s:", pt); | |
4083 | for (j = 0; j < m->count; j++) | |
4084 | fprintf (stderr, " %s", m->sections [j]->name); | |
4085 | putc ('\n',stderr); | |
4086 | } | |
4087 | ||
252b5132 RH |
4088 | /* Assign file positions to the sections based on the mapping from |
4089 | sections to segments. This function also sets up some fields in | |
f3520d2f | 4090 | the file header. */ |
252b5132 | 4091 | |
b34976b6 | 4092 | static bfd_boolean |
f3520d2f AM |
4093 | assign_file_positions_for_load_sections (bfd *abfd, |
4094 | struct bfd_link_info *link_info) | |
252b5132 RH |
4095 | { |
4096 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
252b5132 | 4097 | struct elf_segment_map *m; |
252b5132 | 4098 | Elf_Internal_Phdr *phdrs; |
252b5132 | 4099 | Elf_Internal_Phdr *p; |
02bf8d82 | 4100 | file_ptr off; |
3f570048 | 4101 | bfd_size_type maxpagesize; |
f3520d2f | 4102 | unsigned int alloc; |
0920dee7 | 4103 | unsigned int i, j; |
252b5132 | 4104 | |
e36284ab | 4105 | if (link_info == NULL |
3dea8fca | 4106 | && !elf_modify_segment_map (abfd, link_info, FALSE)) |
8ded5a0f | 4107 | return FALSE; |
252b5132 | 4108 | |
8ded5a0f | 4109 | alloc = 0; |
252b5132 | 4110 | for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next) |
8ded5a0f | 4111 | ++alloc; |
252b5132 RH |
4112 | |
4113 | elf_elfheader (abfd)->e_phoff = bed->s->sizeof_ehdr; | |
4114 | elf_elfheader (abfd)->e_phentsize = bed->s->sizeof_phdr; | |
8ded5a0f | 4115 | elf_elfheader (abfd)->e_phnum = alloc; |
252b5132 | 4116 | |
62d7a5f6 | 4117 | if (elf_tdata (abfd)->program_header_size == (bfd_size_type) -1) |
8ded5a0f AM |
4118 | elf_tdata (abfd)->program_header_size = alloc * bed->s->sizeof_phdr; |
4119 | else | |
4120 | BFD_ASSERT (elf_tdata (abfd)->program_header_size | |
59e0647f | 4121 | >= alloc * bed->s->sizeof_phdr); |
252b5132 RH |
4122 | |
4123 | if (alloc == 0) | |
f3520d2f | 4124 | { |
8ded5a0f AM |
4125 | elf_tdata (abfd)->next_file_pos = bed->s->sizeof_ehdr; |
4126 | return TRUE; | |
f3520d2f | 4127 | } |
252b5132 | 4128 | |
d0fb9a8d | 4129 | phdrs = bfd_alloc2 (abfd, alloc, sizeof (Elf_Internal_Phdr)); |
f3520d2f | 4130 | elf_tdata (abfd)->phdr = phdrs; |
252b5132 | 4131 | if (phdrs == NULL) |
b34976b6 | 4132 | return FALSE; |
252b5132 | 4133 | |
3f570048 AM |
4134 | maxpagesize = 1; |
4135 | if ((abfd->flags & D_PAGED) != 0) | |
4136 | maxpagesize = bed->maxpagesize; | |
4137 | ||
252b5132 RH |
4138 | off = bed->s->sizeof_ehdr; |
4139 | off += alloc * bed->s->sizeof_phdr; | |
4140 | ||
0920dee7 | 4141 | for (m = elf_tdata (abfd)->segment_map, p = phdrs, j = 0; |
252b5132 | 4142 | m != NULL; |
0920dee7 | 4143 | m = m->next, p++, j++) |
252b5132 | 4144 | { |
252b5132 | 4145 | asection **secpp; |
bf988460 AM |
4146 | bfd_vma off_adjust; |
4147 | bfd_boolean no_contents; | |
252b5132 RH |
4148 | |
4149 | /* If elf_segment_map is not from map_sections_to_segments, the | |
08a40648 | 4150 | sections may not be correctly ordered. NOTE: sorting should |
52e9b619 MS |
4151 | not be done to the PT_NOTE section of a corefile, which may |
4152 | contain several pseudo-sections artificially created by bfd. | |
4153 | Sorting these pseudo-sections breaks things badly. */ | |
47d9a591 AM |
4154 | if (m->count > 1 |
4155 | && !(elf_elfheader (abfd)->e_type == ET_CORE | |
52e9b619 | 4156 | && m->p_type == PT_NOTE)) |
252b5132 RH |
4157 | qsort (m->sections, (size_t) m->count, sizeof (asection *), |
4158 | elf_sort_sections); | |
4159 | ||
b301b248 AM |
4160 | /* An ELF segment (described by Elf_Internal_Phdr) may contain a |
4161 | number of sections with contents contributing to both p_filesz | |
4162 | and p_memsz, followed by a number of sections with no contents | |
4163 | that just contribute to p_memsz. In this loop, OFF tracks next | |
02bf8d82 | 4164 | available file offset for PT_LOAD and PT_NOTE segments. */ |
252b5132 | 4165 | p->p_type = m->p_type; |
28a7f3e7 | 4166 | p->p_flags = m->p_flags; |
252b5132 | 4167 | |
3f570048 AM |
4168 | if (m->count == 0) |
4169 | p->p_vaddr = 0; | |
4170 | else | |
3271a814 | 4171 | p->p_vaddr = m->sections[0]->vma - m->p_vaddr_offset; |
3f570048 AM |
4172 | |
4173 | if (m->p_paddr_valid) | |
4174 | p->p_paddr = m->p_paddr; | |
4175 | else if (m->count == 0) | |
4176 | p->p_paddr = 0; | |
4177 | else | |
08a40648 | 4178 | p->p_paddr = m->sections[0]->lma - m->p_vaddr_offset; |
3f570048 AM |
4179 | |
4180 | if (p->p_type == PT_LOAD | |
4181 | && (abfd->flags & D_PAGED) != 0) | |
4182 | { | |
4183 | /* p_align in demand paged PT_LOAD segments effectively stores | |
4184 | the maximum page size. When copying an executable with | |
4185 | objcopy, we set m->p_align from the input file. Use this | |
4186 | value for maxpagesize rather than bed->maxpagesize, which | |
4187 | may be different. Note that we use maxpagesize for PT_TLS | |
4188 | segment alignment later in this function, so we are relying | |
4189 | on at least one PT_LOAD segment appearing before a PT_TLS | |
4190 | segment. */ | |
4191 | if (m->p_align_valid) | |
4192 | maxpagesize = m->p_align; | |
4193 | ||
4194 | p->p_align = maxpagesize; | |
4195 | } | |
3271a814 NS |
4196 | else if (m->p_align_valid) |
4197 | p->p_align = m->p_align; | |
e970b90a DJ |
4198 | else if (m->count == 0) |
4199 | p->p_align = 1 << bed->s->log_file_align; | |
3f570048 AM |
4200 | else |
4201 | p->p_align = 0; | |
4202 | ||
bf988460 AM |
4203 | no_contents = FALSE; |
4204 | off_adjust = 0; | |
252b5132 | 4205 | if (p->p_type == PT_LOAD |
b301b248 | 4206 | && m->count > 0) |
252b5132 | 4207 | { |
b301b248 | 4208 | bfd_size_type align; |
a49e53ed | 4209 | unsigned int align_power = 0; |
b301b248 | 4210 | |
3271a814 NS |
4211 | if (m->p_align_valid) |
4212 | align = p->p_align; | |
4213 | else | |
252b5132 | 4214 | { |
3271a814 NS |
4215 | for (i = 0, secpp = m->sections; i < m->count; i++, secpp++) |
4216 | { | |
4217 | unsigned int secalign; | |
08a40648 | 4218 | |
3271a814 NS |
4219 | secalign = bfd_get_section_alignment (abfd, *secpp); |
4220 | if (secalign > align_power) | |
4221 | align_power = secalign; | |
4222 | } | |
4223 | align = (bfd_size_type) 1 << align_power; | |
4224 | if (align < maxpagesize) | |
4225 | align = maxpagesize; | |
b301b248 | 4226 | } |
252b5132 | 4227 | |
02bf8d82 AM |
4228 | for (i = 0; i < m->count; i++) |
4229 | if ((m->sections[i]->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0) | |
4230 | /* If we aren't making room for this section, then | |
4231 | it must be SHT_NOBITS regardless of what we've | |
4232 | set via struct bfd_elf_special_section. */ | |
4233 | elf_section_type (m->sections[i]) = SHT_NOBITS; | |
4234 | ||
bf988460 AM |
4235 | /* Find out whether this segment contains any loadable |
4236 | sections. If the first section isn't loadable, the same | |
4237 | holds for any other sections. */ | |
4238 | i = 0; | |
4239 | while (elf_section_type (m->sections[i]) == SHT_NOBITS) | |
b301b248 | 4240 | { |
bf988460 AM |
4241 | /* If a segment starts with .tbss, we need to look |
4242 | at the next section to decide whether the segment | |
4243 | has any loadable sections. */ | |
4244 | if ((elf_section_flags (m->sections[i]) & SHF_TLS) == 0 | |
4245 | || ++i >= m->count) | |
b301b248 | 4246 | { |
bf988460 AM |
4247 | no_contents = TRUE; |
4248 | break; | |
b301b248 | 4249 | } |
252b5132 | 4250 | } |
bf988460 AM |
4251 | |
4252 | off_adjust = vma_page_aligned_bias (m->sections[0]->vma, off, align); | |
4253 | off += off_adjust; | |
4254 | if (no_contents) | |
4255 | { | |
4256 | /* We shouldn't need to align the segment on disk since | |
4257 | the segment doesn't need file space, but the gABI | |
4258 | arguably requires the alignment and glibc ld.so | |
4259 | checks it. So to comply with the alignment | |
4260 | requirement but not waste file space, we adjust | |
4261 | p_offset for just this segment. (OFF_ADJUST is | |
4262 | subtracted from OFF later.) This may put p_offset | |
4263 | past the end of file, but that shouldn't matter. */ | |
4264 | } | |
4265 | else | |
4266 | off_adjust = 0; | |
252b5132 | 4267 | } |
b1a6d0b1 NC |
4268 | /* Make sure the .dynamic section is the first section in the |
4269 | PT_DYNAMIC segment. */ | |
4270 | else if (p->p_type == PT_DYNAMIC | |
4271 | && m->count > 1 | |
4272 | && strcmp (m->sections[0]->name, ".dynamic") != 0) | |
4273 | { | |
4274 | _bfd_error_handler | |
b301b248 AM |
4275 | (_("%B: The first section in the PT_DYNAMIC segment is not the .dynamic section"), |
4276 | abfd); | |
b1a6d0b1 NC |
4277 | bfd_set_error (bfd_error_bad_value); |
4278 | return FALSE; | |
4279 | } | |
3f001e84 JK |
4280 | /* Set the note section type to SHT_NOTE. */ |
4281 | else if (p->p_type == PT_NOTE) | |
4282 | for (i = 0; i < m->count; i++) | |
4283 | elf_section_type (m->sections[i]) = SHT_NOTE; | |
252b5132 | 4284 | |
252b5132 RH |
4285 | p->p_offset = 0; |
4286 | p->p_filesz = 0; | |
4287 | p->p_memsz = 0; | |
4288 | ||
4289 | if (m->includes_filehdr) | |
4290 | { | |
bf988460 | 4291 | if (!m->p_flags_valid) |
252b5132 | 4292 | p->p_flags |= PF_R; |
252b5132 RH |
4293 | p->p_filesz = bed->s->sizeof_ehdr; |
4294 | p->p_memsz = bed->s->sizeof_ehdr; | |
4295 | if (m->count > 0) | |
4296 | { | |
4297 | BFD_ASSERT (p->p_type == PT_LOAD); | |
4298 | ||
4299 | if (p->p_vaddr < (bfd_vma) off) | |
4300 | { | |
caf47ea6 | 4301 | (*_bfd_error_handler) |
b301b248 AM |
4302 | (_("%B: Not enough room for program headers, try linking with -N"), |
4303 | abfd); | |
252b5132 | 4304 | bfd_set_error (bfd_error_bad_value); |
b34976b6 | 4305 | return FALSE; |
252b5132 RH |
4306 | } |
4307 | ||
4308 | p->p_vaddr -= off; | |
bf988460 | 4309 | if (!m->p_paddr_valid) |
252b5132 RH |
4310 | p->p_paddr -= off; |
4311 | } | |
252b5132 RH |
4312 | } |
4313 | ||
4314 | if (m->includes_phdrs) | |
4315 | { | |
bf988460 | 4316 | if (!m->p_flags_valid) |
252b5132 RH |
4317 | p->p_flags |= PF_R; |
4318 | ||
f3520d2f | 4319 | if (!m->includes_filehdr) |
252b5132 RH |
4320 | { |
4321 | p->p_offset = bed->s->sizeof_ehdr; | |
4322 | ||
4323 | if (m->count > 0) | |
4324 | { | |
4325 | BFD_ASSERT (p->p_type == PT_LOAD); | |
4326 | p->p_vaddr -= off - p->p_offset; | |
bf988460 | 4327 | if (!m->p_paddr_valid) |
252b5132 RH |
4328 | p->p_paddr -= off - p->p_offset; |
4329 | } | |
252b5132 RH |
4330 | } |
4331 | ||
4332 | p->p_filesz += alloc * bed->s->sizeof_phdr; | |
4333 | p->p_memsz += alloc * bed->s->sizeof_phdr; | |
4334 | } | |
4335 | ||
4336 | if (p->p_type == PT_LOAD | |
4337 | || (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core)) | |
4338 | { | |
bf988460 | 4339 | if (!m->includes_filehdr && !m->includes_phdrs) |
02bf8d82 | 4340 | p->p_offset = off; |
252b5132 RH |
4341 | else |
4342 | { | |
4343 | file_ptr adjust; | |
4344 | ||
4345 | adjust = off - (p->p_offset + p->p_filesz); | |
bf988460 AM |
4346 | if (!no_contents) |
4347 | p->p_filesz += adjust; | |
252b5132 RH |
4348 | p->p_memsz += adjust; |
4349 | } | |
4350 | } | |
4351 | ||
1ea63fd2 AM |
4352 | /* Set up p_filesz, p_memsz, p_align and p_flags from the section |
4353 | maps. Set filepos for sections in PT_LOAD segments, and in | |
4354 | core files, for sections in PT_NOTE segments. | |
4355 | assign_file_positions_for_non_load_sections will set filepos | |
4356 | for other sections and update p_filesz for other segments. */ | |
252b5132 RH |
4357 | for (i = 0, secpp = m->sections; i < m->count; i++, secpp++) |
4358 | { | |
4359 | asection *sec; | |
252b5132 | 4360 | bfd_size_type align; |
627b32bc | 4361 | Elf_Internal_Shdr *this_hdr; |
252b5132 RH |
4362 | |
4363 | sec = *secpp; | |
02bf8d82 | 4364 | this_hdr = &elf_section_data (sec)->this_hdr; |
3f570048 | 4365 | align = (bfd_size_type) 1 << bfd_get_section_alignment (abfd, sec); |
252b5132 | 4366 | |
88967714 AM |
4367 | if ((p->p_type == PT_LOAD |
4368 | || p->p_type == PT_TLS) | |
4369 | && (this_hdr->sh_type != SHT_NOBITS | |
4370 | || ((this_hdr->sh_flags & SHF_ALLOC) != 0 | |
4371 | && ((this_hdr->sh_flags & SHF_TLS) == 0 | |
4372 | || p->p_type == PT_TLS)))) | |
252b5132 | 4373 | { |
11701589 | 4374 | bfd_signed_vma adjust = sec->vma - (p->p_vaddr + p->p_memsz); |
252b5132 | 4375 | |
88967714 | 4376 | if (adjust < 0) |
252b5132 | 4377 | { |
88967714 | 4378 | (*_bfd_error_handler) |
11701589 | 4379 | (_("%B: section %A vma 0x%lx overlaps previous sections"), |
88967714 AM |
4380 | abfd, sec, (unsigned long) sec->lma); |
4381 | adjust = 0; | |
4382 | } | |
4383 | p->p_memsz += adjust; | |
0e922b77 | 4384 | |
88967714 AM |
4385 | if (this_hdr->sh_type != SHT_NOBITS) |
4386 | { | |
4387 | off += adjust; | |
4388 | p->p_filesz += adjust; | |
252b5132 | 4389 | } |
252b5132 RH |
4390 | } |
4391 | ||
4392 | if (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core) | |
4393 | { | |
b301b248 AM |
4394 | /* The section at i == 0 is the one that actually contains |
4395 | everything. */ | |
4a938328 MS |
4396 | if (i == 0) |
4397 | { | |
627b32bc | 4398 | this_hdr->sh_offset = sec->filepos = off; |
6a3cd2b4 AM |
4399 | off += this_hdr->sh_size; |
4400 | p->p_filesz = this_hdr->sh_size; | |
b301b248 AM |
4401 | p->p_memsz = 0; |
4402 | p->p_align = 1; | |
252b5132 | 4403 | } |
4a938328 | 4404 | else |
252b5132 | 4405 | { |
b301b248 | 4406 | /* The rest are fake sections that shouldn't be written. */ |
252b5132 | 4407 | sec->filepos = 0; |
eea6121a | 4408 | sec->size = 0; |
b301b248 AM |
4409 | sec->flags = 0; |
4410 | continue; | |
252b5132 | 4411 | } |
252b5132 RH |
4412 | } |
4413 | else | |
4414 | { | |
b301b248 AM |
4415 | if (p->p_type == PT_LOAD) |
4416 | { | |
02bf8d82 AM |
4417 | this_hdr->sh_offset = sec->filepos = off; |
4418 | if (this_hdr->sh_type != SHT_NOBITS) | |
6a3cd2b4 | 4419 | off += this_hdr->sh_size; |
b301b248 | 4420 | } |
252b5132 | 4421 | |
02bf8d82 | 4422 | if (this_hdr->sh_type != SHT_NOBITS) |
b301b248 | 4423 | { |
6a3cd2b4 | 4424 | p->p_filesz += this_hdr->sh_size; |
02bf8d82 AM |
4425 | /* A load section without SHF_ALLOC is something like |
4426 | a note section in a PT_NOTE segment. These take | |
4427 | file space but are not loaded into memory. */ | |
4428 | if ((this_hdr->sh_flags & SHF_ALLOC) != 0) | |
6a3cd2b4 | 4429 | p->p_memsz += this_hdr->sh_size; |
b301b248 | 4430 | } |
6a3cd2b4 | 4431 | else if ((this_hdr->sh_flags & SHF_ALLOC) != 0) |
13ae64f3 | 4432 | { |
6a3cd2b4 AM |
4433 | if (p->p_type == PT_TLS) |
4434 | p->p_memsz += this_hdr->sh_size; | |
4435 | ||
4436 | /* .tbss is special. It doesn't contribute to p_memsz of | |
4437 | normal segments. */ | |
4438 | else if ((this_hdr->sh_flags & SHF_TLS) == 0) | |
4439 | p->p_memsz += this_hdr->sh_size; | |
13ae64f3 JJ |
4440 | } |
4441 | ||
b10a8ae0 L |
4442 | if (align > p->p_align |
4443 | && !m->p_align_valid | |
4444 | && (p->p_type != PT_LOAD | |
4445 | || (abfd->flags & D_PAGED) == 0)) | |
252b5132 RH |
4446 | p->p_align = align; |
4447 | } | |
4448 | ||
bf988460 | 4449 | if (!m->p_flags_valid) |
252b5132 RH |
4450 | { |
4451 | p->p_flags |= PF_R; | |
02bf8d82 | 4452 | if ((this_hdr->sh_flags & SHF_EXECINSTR) != 0) |
252b5132 | 4453 | p->p_flags |= PF_X; |
02bf8d82 | 4454 | if ((this_hdr->sh_flags & SHF_WRITE) != 0) |
252b5132 RH |
4455 | p->p_flags |= PF_W; |
4456 | } | |
4457 | } | |
bf988460 | 4458 | off -= off_adjust; |
0920dee7 | 4459 | |
7c928300 AM |
4460 | /* Check that all sections are in a PT_LOAD segment. |
4461 | Don't check funky gdb generated core files. */ | |
4462 | if (p->p_type == PT_LOAD && bfd_get_format (abfd) != bfd_core) | |
0920dee7 L |
4463 | for (i = 0, secpp = m->sections; i < m->count; i++, secpp++) |
4464 | { | |
4465 | Elf_Internal_Shdr *this_hdr; | |
4466 | asection *sec; | |
4467 | ||
4468 | sec = *secpp; | |
4469 | this_hdr = &(elf_section_data(sec)->this_hdr); | |
4470 | if (this_hdr->sh_size != 0 | |
4471 | && !ELF_IS_SECTION_IN_SEGMENT_FILE (this_hdr, p)) | |
4472 | { | |
4473 | (*_bfd_error_handler) | |
4474 | (_("%B: section `%A' can't be allocated in segment %d"), | |
4475 | abfd, sec, j); | |
6d33f217 | 4476 | print_segment_map (m); |
0920dee7 L |
4477 | bfd_set_error (bfd_error_bad_value); |
4478 | return FALSE; | |
4479 | } | |
4480 | } | |
252b5132 RH |
4481 | } |
4482 | ||
f3520d2f AM |
4483 | elf_tdata (abfd)->next_file_pos = off; |
4484 | return TRUE; | |
4485 | } | |
4486 | ||
4487 | /* Assign file positions for the other sections. */ | |
4488 | ||
4489 | static bfd_boolean | |
4490 | assign_file_positions_for_non_load_sections (bfd *abfd, | |
4491 | struct bfd_link_info *link_info) | |
4492 | { | |
4493 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
4494 | Elf_Internal_Shdr **i_shdrpp; | |
4495 | Elf_Internal_Shdr **hdrpp; | |
4496 | Elf_Internal_Phdr *phdrs; | |
4497 | Elf_Internal_Phdr *p; | |
4498 | struct elf_segment_map *m; | |
4499 | bfd_vma filehdr_vaddr, filehdr_paddr; | |
4500 | bfd_vma phdrs_vaddr, phdrs_paddr; | |
4501 | file_ptr off; | |
4502 | unsigned int num_sec; | |
4503 | unsigned int i; | |
4504 | unsigned int count; | |
4505 | ||
5c182d5f AM |
4506 | i_shdrpp = elf_elfsections (abfd); |
4507 | num_sec = elf_numsections (abfd); | |
f3520d2f | 4508 | off = elf_tdata (abfd)->next_file_pos; |
5c182d5f AM |
4509 | for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++) |
4510 | { | |
4511 | struct elf_obj_tdata *tdata = elf_tdata (abfd); | |
4512 | Elf_Internal_Shdr *hdr; | |
4513 | ||
4514 | hdr = *hdrpp; | |
4515 | if (hdr->bfd_section != NULL | |
252e386e AM |
4516 | && (hdr->bfd_section->filepos != 0 |
4517 | || (hdr->sh_type == SHT_NOBITS | |
4518 | && hdr->contents == NULL))) | |
627b32bc | 4519 | BFD_ASSERT (hdr->sh_offset == hdr->bfd_section->filepos); |
5c182d5f AM |
4520 | else if ((hdr->sh_flags & SHF_ALLOC) != 0) |
4521 | { | |
49c13adb L |
4522 | if (hdr->sh_size != 0) |
4523 | ((*_bfd_error_handler) | |
4524 | (_("%B: warning: allocated section `%s' not in segment"), | |
3ba71138 L |
4525 | abfd, |
4526 | (hdr->bfd_section == NULL | |
4527 | ? "*unknown*" | |
4528 | : hdr->bfd_section->name))); | |
4529 | /* We don't need to page align empty sections. */ | |
4530 | if ((abfd->flags & D_PAGED) != 0 && hdr->sh_size != 0) | |
5c182d5f AM |
4531 | off += vma_page_aligned_bias (hdr->sh_addr, off, |
4532 | bed->maxpagesize); | |
4533 | else | |
4534 | off += vma_page_aligned_bias (hdr->sh_addr, off, | |
4535 | hdr->sh_addralign); | |
4536 | off = _bfd_elf_assign_file_position_for_section (hdr, off, | |
4537 | FALSE); | |
4538 | } | |
4539 | else if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA) | |
4540 | && hdr->bfd_section == NULL) | |
4541 | || hdr == i_shdrpp[tdata->symtab_section] | |
4542 | || hdr == i_shdrpp[tdata->symtab_shndx_section] | |
4543 | || hdr == i_shdrpp[tdata->strtab_section]) | |
4544 | hdr->sh_offset = -1; | |
4545 | else | |
4546 | off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE); | |
5c182d5f AM |
4547 | } |
4548 | ||
252b5132 RH |
4549 | /* Now that we have set the section file positions, we can set up |
4550 | the file positions for the non PT_LOAD segments. */ | |
f3520d2f AM |
4551 | count = 0; |
4552 | filehdr_vaddr = 0; | |
4553 | filehdr_paddr = 0; | |
4554 | phdrs_vaddr = bed->maxpagesize + bed->s->sizeof_ehdr; | |
4555 | phdrs_paddr = 0; | |
4556 | phdrs = elf_tdata (abfd)->phdr; | |
4557 | for (m = elf_tdata (abfd)->segment_map, p = phdrs; | |
4558 | m != NULL; | |
4559 | m = m->next, p++) | |
4560 | { | |
4561 | ++count; | |
4562 | if (p->p_type != PT_LOAD) | |
4563 | continue; | |
4564 | ||
4565 | if (m->includes_filehdr) | |
4566 | { | |
4567 | filehdr_vaddr = p->p_vaddr; | |
4568 | filehdr_paddr = p->p_paddr; | |
4569 | } | |
4570 | if (m->includes_phdrs) | |
4571 | { | |
4572 | phdrs_vaddr = p->p_vaddr; | |
4573 | phdrs_paddr = p->p_paddr; | |
4574 | if (m->includes_filehdr) | |
4575 | { | |
4576 | phdrs_vaddr += bed->s->sizeof_ehdr; | |
4577 | phdrs_paddr += bed->s->sizeof_ehdr; | |
4578 | } | |
4579 | } | |
4580 | } | |
4581 | ||
252b5132 RH |
4582 | for (m = elf_tdata (abfd)->segment_map, p = phdrs; |
4583 | m != NULL; | |
4584 | m = m->next, p++) | |
4585 | { | |
1ea63fd2 | 4586 | if (m->count != 0) |
252b5132 | 4587 | { |
1ea63fd2 | 4588 | if (p->p_type != PT_LOAD |
b10a8ae0 L |
4589 | && (p->p_type != PT_NOTE |
4590 | || bfd_get_format (abfd) != bfd_core)) | |
229fcec5 | 4591 | { |
1ea63fd2 | 4592 | Elf_Internal_Shdr *hdr; |
b10a8ae0 L |
4593 | asection *sect; |
4594 | ||
1ea63fd2 AM |
4595 | BFD_ASSERT (!m->includes_filehdr && !m->includes_phdrs); |
4596 | ||
b10a8ae0 L |
4597 | sect = m->sections[m->count - 1]; |
4598 | hdr = &elf_section_data (sect)->this_hdr; | |
4599 | p->p_filesz = sect->filepos - m->sections[0]->filepos; | |
1ea63fd2 AM |
4600 | if (hdr->sh_type != SHT_NOBITS) |
4601 | p->p_filesz += hdr->sh_size; | |
4602 | ||
b10a8ae0 L |
4603 | if (p->p_type == PT_GNU_RELRO) |
4604 | { | |
4605 | /* When we get here, we are copying executable | |
4606 | or shared library. But we need to use the same | |
4607 | linker logic. */ | |
4608 | Elf_Internal_Phdr *lp; | |
4609 | ||
4610 | for (lp = phdrs; lp < phdrs + count; ++lp) | |
4611 | { | |
4612 | if (lp->p_type == PT_LOAD | |
4613 | && lp->p_paddr == p->p_paddr) | |
4614 | break; | |
4615 | } | |
4616 | ||
4617 | if (lp < phdrs + count) | |
4618 | { | |
4619 | /* We should use p_size if it is valid since it | |
4620 | may contain the first few bytes of the next | |
4621 | SEC_ALLOC section. */ | |
4622 | if (m->p_size_valid) | |
4623 | p->p_filesz = m->p_size; | |
4624 | else | |
4625 | abort (); | |
4626 | p->p_vaddr = lp->p_vaddr; | |
4627 | p->p_offset = lp->p_offset; | |
4628 | p->p_memsz = p->p_filesz; | |
4629 | p->p_align = 1; | |
4630 | } | |
4631 | else | |
4632 | abort (); | |
4633 | } | |
4634 | else | |
4635 | p->p_offset = m->sections[0]->filepos; | |
229fcec5 | 4636 | } |
252b5132 | 4637 | } |
1ea63fd2 | 4638 | else |
252b5132 RH |
4639 | { |
4640 | if (m->includes_filehdr) | |
4641 | { | |
4642 | p->p_vaddr = filehdr_vaddr; | |
4643 | if (! m->p_paddr_valid) | |
4644 | p->p_paddr = filehdr_paddr; | |
4645 | } | |
4646 | else if (m->includes_phdrs) | |
4647 | { | |
4648 | p->p_vaddr = phdrs_vaddr; | |
4649 | if (! m->p_paddr_valid) | |
4650 | p->p_paddr = phdrs_paddr; | |
4651 | } | |
8c37241b JJ |
4652 | else if (p->p_type == PT_GNU_RELRO) |
4653 | { | |
4654 | Elf_Internal_Phdr *lp; | |
4655 | ||
4656 | for (lp = phdrs; lp < phdrs + count; ++lp) | |
4657 | { | |
4658 | if (lp->p_type == PT_LOAD | |
4659 | && lp->p_vaddr <= link_info->relro_end | |
4660 | && lp->p_vaddr >= link_info->relro_start | |
e36284ab AM |
4661 | && (lp->p_vaddr + lp->p_filesz |
4662 | >= link_info->relro_end)) | |
8c37241b JJ |
4663 | break; |
4664 | } | |
4665 | ||
4666 | if (lp < phdrs + count | |
4667 | && link_info->relro_end > lp->p_vaddr) | |
4668 | { | |
4669 | p->p_vaddr = lp->p_vaddr; | |
4670 | p->p_paddr = lp->p_paddr; | |
4671 | p->p_offset = lp->p_offset; | |
4672 | p->p_filesz = link_info->relro_end - lp->p_vaddr; | |
4673 | p->p_memsz = p->p_filesz; | |
4674 | p->p_align = 1; | |
4675 | p->p_flags = (lp->p_flags & ~PF_W); | |
4676 | } | |
4677 | else | |
4678 | { | |
4679 | memset (p, 0, sizeof *p); | |
4680 | p->p_type = PT_NULL; | |
4681 | } | |
4682 | } | |
252b5132 RH |
4683 | } |
4684 | } | |
4685 | ||
252b5132 RH |
4686 | elf_tdata (abfd)->next_file_pos = off; |
4687 | ||
b34976b6 | 4688 | return TRUE; |
252b5132 RH |
4689 | } |
4690 | ||
252b5132 RH |
4691 | /* Work out the file positions of all the sections. This is called by |
4692 | _bfd_elf_compute_section_file_positions. All the section sizes and | |
4693 | VMAs must be known before this is called. | |
4694 | ||
e0638f70 AM |
4695 | Reloc sections come in two flavours: Those processed specially as |
4696 | "side-channel" data attached to a section to which they apply, and | |
4697 | those that bfd doesn't process as relocations. The latter sort are | |
4698 | stored in a normal bfd section by bfd_section_from_shdr. We don't | |
4699 | consider the former sort here, unless they form part of the loadable | |
4700 | image. Reloc sections not assigned here will be handled later by | |
4701 | assign_file_positions_for_relocs. | |
252b5132 RH |
4702 | |
4703 | We also don't set the positions of the .symtab and .strtab here. */ | |
4704 | ||
b34976b6 | 4705 | static bfd_boolean |
c84fca4d AO |
4706 | assign_file_positions_except_relocs (bfd *abfd, |
4707 | struct bfd_link_info *link_info) | |
252b5132 | 4708 | { |
5c182d5f AM |
4709 | struct elf_obj_tdata *tdata = elf_tdata (abfd); |
4710 | Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd); | |
252b5132 | 4711 | file_ptr off; |
9c5bfbb7 | 4712 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
252b5132 RH |
4713 | |
4714 | if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0 | |
4715 | && bfd_get_format (abfd) != bfd_core) | |
4716 | { | |
5c182d5f AM |
4717 | Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd); |
4718 | unsigned int num_sec = elf_numsections (abfd); | |
252b5132 RH |
4719 | Elf_Internal_Shdr **hdrpp; |
4720 | unsigned int i; | |
4721 | ||
4722 | /* Start after the ELF header. */ | |
4723 | off = i_ehdrp->e_ehsize; | |
4724 | ||
4725 | /* We are not creating an executable, which means that we are | |
4726 | not creating a program header, and that the actual order of | |
4727 | the sections in the file is unimportant. */ | |
9ad5cbcf | 4728 | for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++) |
252b5132 RH |
4729 | { |
4730 | Elf_Internal_Shdr *hdr; | |
4731 | ||
4732 | hdr = *hdrpp; | |
e0638f70 AM |
4733 | if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA) |
4734 | && hdr->bfd_section == NULL) | |
9ad5cbcf AM |
4735 | || i == tdata->symtab_section |
4736 | || i == tdata->symtab_shndx_section | |
252b5132 RH |
4737 | || i == tdata->strtab_section) |
4738 | { | |
4739 | hdr->sh_offset = -1; | |
252b5132 | 4740 | } |
9ad5cbcf | 4741 | else |
b34976b6 | 4742 | off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE); |
252b5132 RH |
4743 | } |
4744 | } | |
4745 | else | |
4746 | { | |
f3520d2f AM |
4747 | unsigned int alloc; |
4748 | ||
252b5132 | 4749 | /* Assign file positions for the loaded sections based on the |
08a40648 | 4750 | assignment of sections to segments. */ |
f3520d2f AM |
4751 | if (!assign_file_positions_for_load_sections (abfd, link_info)) |
4752 | return FALSE; | |
4753 | ||
4754 | /* And for non-load sections. */ | |
4755 | if (!assign_file_positions_for_non_load_sections (abfd, link_info)) | |
4756 | return FALSE; | |
4757 | ||
e36284ab AM |
4758 | if (bed->elf_backend_modify_program_headers != NULL) |
4759 | { | |
4760 | if (!(*bed->elf_backend_modify_program_headers) (abfd, link_info)) | |
4761 | return FALSE; | |
4762 | } | |
4763 | ||
f3520d2f AM |
4764 | /* Write out the program headers. */ |
4765 | alloc = tdata->program_header_size / bed->s->sizeof_phdr; | |
4766 | if (bfd_seek (abfd, (bfd_signed_vma) bed->s->sizeof_ehdr, SEEK_SET) != 0 | |
4767 | || bed->s->write_out_phdrs (abfd, tdata->phdr, alloc) != 0) | |
b34976b6 | 4768 | return FALSE; |
252b5132 | 4769 | |
5c182d5f | 4770 | off = tdata->next_file_pos; |
252b5132 RH |
4771 | } |
4772 | ||
4773 | /* Place the section headers. */ | |
45d6a902 | 4774 | off = align_file_position (off, 1 << bed->s->log_file_align); |
252b5132 RH |
4775 | i_ehdrp->e_shoff = off; |
4776 | off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize; | |
4777 | ||
5c182d5f | 4778 | tdata->next_file_pos = off; |
252b5132 | 4779 | |
b34976b6 | 4780 | return TRUE; |
252b5132 RH |
4781 | } |
4782 | ||
b34976b6 | 4783 | static bfd_boolean |
217aa764 | 4784 | prep_headers (bfd *abfd) |
252b5132 RH |
4785 | { |
4786 | Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */ | |
4787 | Elf_Internal_Phdr *i_phdrp = 0; /* Program header table, internal form */ | |
2b0f7ef9 | 4788 | struct elf_strtab_hash *shstrtab; |
9c5bfbb7 | 4789 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
252b5132 RH |
4790 | |
4791 | i_ehdrp = elf_elfheader (abfd); | |
252b5132 | 4792 | |
2b0f7ef9 | 4793 | shstrtab = _bfd_elf_strtab_init (); |
252b5132 | 4794 | if (shstrtab == NULL) |
b34976b6 | 4795 | return FALSE; |
252b5132 RH |
4796 | |
4797 | elf_shstrtab (abfd) = shstrtab; | |
4798 | ||
4799 | i_ehdrp->e_ident[EI_MAG0] = ELFMAG0; | |
4800 | i_ehdrp->e_ident[EI_MAG1] = ELFMAG1; | |
4801 | i_ehdrp->e_ident[EI_MAG2] = ELFMAG2; | |
4802 | i_ehdrp->e_ident[EI_MAG3] = ELFMAG3; | |
4803 | ||
4804 | i_ehdrp->e_ident[EI_CLASS] = bed->s->elfclass; | |
4805 | i_ehdrp->e_ident[EI_DATA] = | |
4806 | bfd_big_endian (abfd) ? ELFDATA2MSB : ELFDATA2LSB; | |
4807 | i_ehdrp->e_ident[EI_VERSION] = bed->s->ev_current; | |
4808 | ||
252b5132 RH |
4809 | if ((abfd->flags & DYNAMIC) != 0) |
4810 | i_ehdrp->e_type = ET_DYN; | |
4811 | else if ((abfd->flags & EXEC_P) != 0) | |
4812 | i_ehdrp->e_type = ET_EXEC; | |
4813 | else if (bfd_get_format (abfd) == bfd_core) | |
4814 | i_ehdrp->e_type = ET_CORE; | |
4815 | else | |
4816 | i_ehdrp->e_type = ET_REL; | |
4817 | ||
4818 | switch (bfd_get_arch (abfd)) | |
4819 | { | |
4820 | case bfd_arch_unknown: | |
4821 | i_ehdrp->e_machine = EM_NONE; | |
4822 | break; | |
aa4f99bb AO |
4823 | |
4824 | /* There used to be a long list of cases here, each one setting | |
4825 | e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE | |
4826 | in the corresponding bfd definition. To avoid duplication, | |
4827 | the switch was removed. Machines that need special handling | |
4828 | can generally do it in elf_backend_final_write_processing(), | |
4829 | unless they need the information earlier than the final write. | |
4830 | Such need can generally be supplied by replacing the tests for | |
4831 | e_machine with the conditions used to determine it. */ | |
252b5132 | 4832 | default: |
9c5bfbb7 AM |
4833 | i_ehdrp->e_machine = bed->elf_machine_code; |
4834 | } | |
aa4f99bb | 4835 | |
252b5132 RH |
4836 | i_ehdrp->e_version = bed->s->ev_current; |
4837 | i_ehdrp->e_ehsize = bed->s->sizeof_ehdr; | |
4838 | ||
c044fabd | 4839 | /* No program header, for now. */ |
252b5132 RH |
4840 | i_ehdrp->e_phoff = 0; |
4841 | i_ehdrp->e_phentsize = 0; | |
4842 | i_ehdrp->e_phnum = 0; | |
4843 | ||
c044fabd | 4844 | /* Each bfd section is section header entry. */ |
252b5132 RH |
4845 | i_ehdrp->e_entry = bfd_get_start_address (abfd); |
4846 | i_ehdrp->e_shentsize = bed->s->sizeof_shdr; | |
4847 | ||
c044fabd | 4848 | /* If we're building an executable, we'll need a program header table. */ |
252b5132 | 4849 | if (abfd->flags & EXEC_P) |
0e71e495 BE |
4850 | /* It all happens later. */ |
4851 | ; | |
252b5132 RH |
4852 | else |
4853 | { | |
4854 | i_ehdrp->e_phentsize = 0; | |
4855 | i_phdrp = 0; | |
4856 | i_ehdrp->e_phoff = 0; | |
4857 | } | |
4858 | ||
4859 | elf_tdata (abfd)->symtab_hdr.sh_name = | |
b34976b6 | 4860 | (unsigned int) _bfd_elf_strtab_add (shstrtab, ".symtab", FALSE); |
252b5132 | 4861 | elf_tdata (abfd)->strtab_hdr.sh_name = |
b34976b6 | 4862 | (unsigned int) _bfd_elf_strtab_add (shstrtab, ".strtab", FALSE); |
252b5132 | 4863 | elf_tdata (abfd)->shstrtab_hdr.sh_name = |
b34976b6 | 4864 | (unsigned int) _bfd_elf_strtab_add (shstrtab, ".shstrtab", FALSE); |
252b5132 RH |
4865 | if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1 |
4866 | || elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1 | |
4867 | || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1) | |
b34976b6 | 4868 | return FALSE; |
252b5132 | 4869 | |
b34976b6 | 4870 | return TRUE; |
252b5132 RH |
4871 | } |
4872 | ||
4873 | /* Assign file positions for all the reloc sections which are not part | |
4874 | of the loadable file image. */ | |
4875 | ||
4876 | void | |
217aa764 | 4877 | _bfd_elf_assign_file_positions_for_relocs (bfd *abfd) |
252b5132 RH |
4878 | { |
4879 | file_ptr off; | |
9ad5cbcf | 4880 | unsigned int i, num_sec; |
252b5132 RH |
4881 | Elf_Internal_Shdr **shdrpp; |
4882 | ||
4883 | off = elf_tdata (abfd)->next_file_pos; | |
4884 | ||
9ad5cbcf AM |
4885 | num_sec = elf_numsections (abfd); |
4886 | for (i = 1, shdrpp = elf_elfsections (abfd) + 1; i < num_sec; i++, shdrpp++) | |
252b5132 RH |
4887 | { |
4888 | Elf_Internal_Shdr *shdrp; | |
4889 | ||
4890 | shdrp = *shdrpp; | |
4891 | if ((shdrp->sh_type == SHT_REL || shdrp->sh_type == SHT_RELA) | |
4892 | && shdrp->sh_offset == -1) | |
b34976b6 | 4893 | off = _bfd_elf_assign_file_position_for_section (shdrp, off, TRUE); |
252b5132 RH |
4894 | } |
4895 | ||
4896 | elf_tdata (abfd)->next_file_pos = off; | |
4897 | } | |
4898 | ||
b34976b6 | 4899 | bfd_boolean |
217aa764 | 4900 | _bfd_elf_write_object_contents (bfd *abfd) |
252b5132 | 4901 | { |
9c5bfbb7 | 4902 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
252b5132 RH |
4903 | Elf_Internal_Ehdr *i_ehdrp; |
4904 | Elf_Internal_Shdr **i_shdrp; | |
b34976b6 | 4905 | bfd_boolean failed; |
9ad5cbcf | 4906 | unsigned int count, num_sec; |
252b5132 RH |
4907 | |
4908 | if (! abfd->output_has_begun | |
217aa764 | 4909 | && ! _bfd_elf_compute_section_file_positions (abfd, NULL)) |
b34976b6 | 4910 | return FALSE; |
252b5132 RH |
4911 | |
4912 | i_shdrp = elf_elfsections (abfd); | |
4913 | i_ehdrp = elf_elfheader (abfd); | |
4914 | ||
b34976b6 | 4915 | failed = FALSE; |
252b5132 RH |
4916 | bfd_map_over_sections (abfd, bed->s->write_relocs, &failed); |
4917 | if (failed) | |
b34976b6 | 4918 | return FALSE; |
252b5132 RH |
4919 | |
4920 | _bfd_elf_assign_file_positions_for_relocs (abfd); | |
4921 | ||
c044fabd | 4922 | /* After writing the headers, we need to write the sections too... */ |
9ad5cbcf AM |
4923 | num_sec = elf_numsections (abfd); |
4924 | for (count = 1; count < num_sec; count++) | |
252b5132 RH |
4925 | { |
4926 | if (bed->elf_backend_section_processing) | |
4927 | (*bed->elf_backend_section_processing) (abfd, i_shdrp[count]); | |
4928 | if (i_shdrp[count]->contents) | |
4929 | { | |
dc810e39 AM |
4930 | bfd_size_type amt = i_shdrp[count]->sh_size; |
4931 | ||
252b5132 | 4932 | if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0 |
dc810e39 | 4933 | || bfd_bwrite (i_shdrp[count]->contents, amt, abfd) != amt) |
b34976b6 | 4934 | return FALSE; |
252b5132 RH |
4935 | } |
4936 | } | |
4937 | ||
4938 | /* Write out the section header names. */ | |
26ae6d5e DJ |
4939 | if (elf_shstrtab (abfd) != NULL |
4940 | && (bfd_seek (abfd, elf_tdata (abfd)->shstrtab_hdr.sh_offset, SEEK_SET) != 0 | |
08a40648 | 4941 | || !_bfd_elf_strtab_emit (abfd, elf_shstrtab (abfd)))) |
b34976b6 | 4942 | return FALSE; |
252b5132 RH |
4943 | |
4944 | if (bed->elf_backend_final_write_processing) | |
4945 | (*bed->elf_backend_final_write_processing) (abfd, | |
4946 | elf_tdata (abfd)->linker); | |
4947 | ||
ff59fc36 RM |
4948 | if (!bed->s->write_shdrs_and_ehdr (abfd)) |
4949 | return FALSE; | |
4950 | ||
4951 | /* This is last since write_shdrs_and_ehdr can touch i_shdrp[0]. */ | |
bfb53a4f RM |
4952 | if (elf_tdata (abfd)->after_write_object_contents) |
4953 | return (*elf_tdata (abfd)->after_write_object_contents) (abfd); | |
ff59fc36 RM |
4954 | |
4955 | return TRUE; | |
252b5132 RH |
4956 | } |
4957 | ||
b34976b6 | 4958 | bfd_boolean |
217aa764 | 4959 | _bfd_elf_write_corefile_contents (bfd *abfd) |
252b5132 | 4960 | { |
c044fabd | 4961 | /* Hopefully this can be done just like an object file. */ |
252b5132 RH |
4962 | return _bfd_elf_write_object_contents (abfd); |
4963 | } | |
c044fabd KH |
4964 | |
4965 | /* Given a section, search the header to find them. */ | |
4966 | ||
cb33740c | 4967 | unsigned int |
198beae2 | 4968 | _bfd_elf_section_from_bfd_section (bfd *abfd, struct bfd_section *asect) |
252b5132 | 4969 | { |
9c5bfbb7 | 4970 | const struct elf_backend_data *bed; |
cb33740c | 4971 | unsigned int index; |
252b5132 | 4972 | |
9ad5cbcf AM |
4973 | if (elf_section_data (asect) != NULL |
4974 | && elf_section_data (asect)->this_idx != 0) | |
4975 | return elf_section_data (asect)->this_idx; | |
4976 | ||
4977 | if (bfd_is_abs_section (asect)) | |
af746e92 AM |
4978 | index = SHN_ABS; |
4979 | else if (bfd_is_com_section (asect)) | |
4980 | index = SHN_COMMON; | |
4981 | else if (bfd_is_und_section (asect)) | |
4982 | index = SHN_UNDEF; | |
4983 | else | |
cb33740c | 4984 | index = SHN_BAD; |
252b5132 | 4985 | |
af746e92 | 4986 | bed = get_elf_backend_data (abfd); |
252b5132 RH |
4987 | if (bed->elf_backend_section_from_bfd_section) |
4988 | { | |
af746e92 | 4989 | int retval = index; |
9ad5cbcf | 4990 | |
af746e92 AM |
4991 | if ((*bed->elf_backend_section_from_bfd_section) (abfd, asect, &retval)) |
4992 | return retval; | |
252b5132 RH |
4993 | } |
4994 | ||
cb33740c | 4995 | if (index == SHN_BAD) |
af746e92 | 4996 | bfd_set_error (bfd_error_nonrepresentable_section); |
252b5132 | 4997 | |
af746e92 | 4998 | return index; |
252b5132 RH |
4999 | } |
5000 | ||
5001 | /* Given a BFD symbol, return the index in the ELF symbol table, or -1 | |
5002 | on error. */ | |
5003 | ||
5004 | int | |
217aa764 | 5005 | _bfd_elf_symbol_from_bfd_symbol (bfd *abfd, asymbol **asym_ptr_ptr) |
252b5132 RH |
5006 | { |
5007 | asymbol *asym_ptr = *asym_ptr_ptr; | |
5008 | int idx; | |
5009 | flagword flags = asym_ptr->flags; | |
5010 | ||
5011 | /* When gas creates relocations against local labels, it creates its | |
5012 | own symbol for the section, but does put the symbol into the | |
5013 | symbol chain, so udata is 0. When the linker is generating | |
5014 | relocatable output, this section symbol may be for one of the | |
5015 | input sections rather than the output section. */ | |
5016 | if (asym_ptr->udata.i == 0 | |
5017 | && (flags & BSF_SECTION_SYM) | |
5018 | && asym_ptr->section) | |
5019 | { | |
5372391b | 5020 | asection *sec; |
252b5132 RH |
5021 | int indx; |
5022 | ||
5372391b AM |
5023 | sec = asym_ptr->section; |
5024 | if (sec->owner != abfd && sec->output_section != NULL) | |
5025 | sec = sec->output_section; | |
5026 | if (sec->owner == abfd | |
5027 | && (indx = sec->index) < elf_num_section_syms (abfd) | |
4e89ac30 | 5028 | && elf_section_syms (abfd)[indx] != NULL) |
252b5132 RH |
5029 | asym_ptr->udata.i = elf_section_syms (abfd)[indx]->udata.i; |
5030 | } | |
5031 | ||
5032 | idx = asym_ptr->udata.i; | |
5033 | ||
5034 | if (idx == 0) | |
5035 | { | |
5036 | /* This case can occur when using --strip-symbol on a symbol | |
08a40648 | 5037 | which is used in a relocation entry. */ |
252b5132 | 5038 | (*_bfd_error_handler) |
d003868e AM |
5039 | (_("%B: symbol `%s' required but not present"), |
5040 | abfd, bfd_asymbol_name (asym_ptr)); | |
252b5132 RH |
5041 | bfd_set_error (bfd_error_no_symbols); |
5042 | return -1; | |
5043 | } | |
5044 | ||
5045 | #if DEBUG & 4 | |
5046 | { | |
5047 | fprintf (stderr, | |
661a3fd4 | 5048 | "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8lx%s\n", |
252b5132 RH |
5049 | (long) asym_ptr, asym_ptr->name, idx, flags, |
5050 | elf_symbol_flags (flags)); | |
5051 | fflush (stderr); | |
5052 | } | |
5053 | #endif | |
5054 | ||
5055 | return idx; | |
5056 | } | |
5057 | ||
84d1d650 | 5058 | /* Rewrite program header information. */ |
252b5132 | 5059 | |
b34976b6 | 5060 | static bfd_boolean |
84d1d650 | 5061 | rewrite_elf_program_header (bfd *ibfd, bfd *obfd) |
252b5132 | 5062 | { |
b34976b6 AM |
5063 | Elf_Internal_Ehdr *iehdr; |
5064 | struct elf_segment_map *map; | |
5065 | struct elf_segment_map *map_first; | |
5066 | struct elf_segment_map **pointer_to_map; | |
5067 | Elf_Internal_Phdr *segment; | |
5068 | asection *section; | |
5069 | unsigned int i; | |
5070 | unsigned int num_segments; | |
5071 | bfd_boolean phdr_included = FALSE; | |
5c44b38e | 5072 | bfd_boolean p_paddr_valid; |
b34976b6 AM |
5073 | bfd_vma maxpagesize; |
5074 | struct elf_segment_map *phdr_adjust_seg = NULL; | |
5075 | unsigned int phdr_adjust_num = 0; | |
9c5bfbb7 | 5076 | const struct elf_backend_data *bed; |
bc67d8a6 | 5077 | |
caf47ea6 | 5078 | bed = get_elf_backend_data (ibfd); |
252b5132 RH |
5079 | iehdr = elf_elfheader (ibfd); |
5080 | ||
bc67d8a6 | 5081 | map_first = NULL; |
c044fabd | 5082 | pointer_to_map = &map_first; |
252b5132 RH |
5083 | |
5084 | num_segments = elf_elfheader (ibfd)->e_phnum; | |
bc67d8a6 NC |
5085 | maxpagesize = get_elf_backend_data (obfd)->maxpagesize; |
5086 | ||
5087 | /* Returns the end address of the segment + 1. */ | |
aecc8f8a AM |
5088 | #define SEGMENT_END(segment, start) \ |
5089 | (start + (segment->p_memsz > segment->p_filesz \ | |
5090 | ? segment->p_memsz : segment->p_filesz)) | |
bc67d8a6 | 5091 | |
eecdbe52 JJ |
5092 | #define SECTION_SIZE(section, segment) \ |
5093 | (((section->flags & (SEC_HAS_CONTENTS | SEC_THREAD_LOCAL)) \ | |
5094 | != SEC_THREAD_LOCAL || segment->p_type == PT_TLS) \ | |
eea6121a | 5095 | ? section->size : 0) |
eecdbe52 | 5096 | |
b34976b6 | 5097 | /* Returns TRUE if the given section is contained within |
bc67d8a6 | 5098 | the given segment. VMA addresses are compared. */ |
aecc8f8a AM |
5099 | #define IS_CONTAINED_BY_VMA(section, segment) \ |
5100 | (section->vma >= segment->p_vaddr \ | |
eecdbe52 | 5101 | && (section->vma + SECTION_SIZE (section, segment) \ |
aecc8f8a | 5102 | <= (SEGMENT_END (segment, segment->p_vaddr)))) |
c044fabd | 5103 | |
b34976b6 | 5104 | /* Returns TRUE if the given section is contained within |
bc67d8a6 | 5105 | the given segment. LMA addresses are compared. */ |
aecc8f8a AM |
5106 | #define IS_CONTAINED_BY_LMA(section, segment, base) \ |
5107 | (section->lma >= base \ | |
eecdbe52 | 5108 | && (section->lma + SECTION_SIZE (section, segment) \ |
aecc8f8a | 5109 | <= SEGMENT_END (segment, base))) |
252b5132 | 5110 | |
0efc80c8 L |
5111 | /* Handle PT_NOTE segment. */ |
5112 | #define IS_NOTE(p, s) \ | |
aecc8f8a | 5113 | (p->p_type == PT_NOTE \ |
0efc80c8 | 5114 | && elf_section_type (s) == SHT_NOTE \ |
aecc8f8a | 5115 | && (bfd_vma) s->filepos >= p->p_offset \ |
cb3ff1e5 | 5116 | && ((bfd_vma) s->filepos + s->size \ |
aecc8f8a | 5117 | <= p->p_offset + p->p_filesz)) |
252b5132 | 5118 | |
0efc80c8 L |
5119 | /* Special case: corefile "NOTE" section containing regs, prpsinfo |
5120 | etc. */ | |
5121 | #define IS_COREFILE_NOTE(p, s) \ | |
5122 | (IS_NOTE (p, s) \ | |
5123 | && bfd_get_format (ibfd) == bfd_core \ | |
5124 | && s->vma == 0 \ | |
5125 | && s->lma == 0) | |
5126 | ||
252b5132 RH |
5127 | /* The complicated case when p_vaddr is 0 is to handle the Solaris |
5128 | linker, which generates a PT_INTERP section with p_vaddr and | |
5129 | p_memsz set to 0. */ | |
aecc8f8a AM |
5130 | #define IS_SOLARIS_PT_INTERP(p, s) \ |
5131 | (p->p_vaddr == 0 \ | |
5132 | && p->p_paddr == 0 \ | |
5133 | && p->p_memsz == 0 \ | |
5134 | && p->p_filesz > 0 \ | |
5135 | && (s->flags & SEC_HAS_CONTENTS) != 0 \ | |
eea6121a | 5136 | && s->size > 0 \ |
aecc8f8a | 5137 | && (bfd_vma) s->filepos >= p->p_offset \ |
cb3ff1e5 | 5138 | && ((bfd_vma) s->filepos + s->size \ |
aecc8f8a | 5139 | <= p->p_offset + p->p_filesz)) |
5c440b1e | 5140 | |
bc67d8a6 NC |
5141 | /* Decide if the given section should be included in the given segment. |
5142 | A section will be included if: | |
f5ffc919 | 5143 | 1. It is within the address space of the segment -- we use the LMA |
08a40648 | 5144 | if that is set for the segment and the VMA otherwise, |
0efc80c8 L |
5145 | 2. It is an allocated section or a NOTE section in a PT_NOTE |
5146 | segment. | |
bc67d8a6 | 5147 | 3. There is an output section associated with it, |
eecdbe52 | 5148 | 4. The section has not already been allocated to a previous segment. |
2b05f1b7 | 5149 | 5. PT_GNU_STACK segments do not include any sections. |
03394ac9 | 5150 | 6. PT_TLS segment includes only SHF_TLS sections. |
6f79b219 JJ |
5151 | 7. SHF_TLS sections are only in PT_TLS or PT_LOAD segments. |
5152 | 8. PT_DYNAMIC should not contain empty sections at the beginning | |
08a40648 | 5153 | (with the possible exception of .dynamic). */ |
9f17e2a6 | 5154 | #define IS_SECTION_IN_INPUT_SEGMENT(section, segment, bed) \ |
2b05f1b7 L |
5155 | ((((segment->p_paddr \ |
5156 | ? IS_CONTAINED_BY_LMA (section, segment, segment->p_paddr) \ | |
5157 | : IS_CONTAINED_BY_VMA (section, segment)) \ | |
5158 | && (section->flags & SEC_ALLOC) != 0) \ | |
0efc80c8 | 5159 | || IS_NOTE (segment, section)) \ |
2b05f1b7 L |
5160 | && segment->p_type != PT_GNU_STACK \ |
5161 | && (segment->p_type != PT_TLS \ | |
5162 | || (section->flags & SEC_THREAD_LOCAL)) \ | |
5163 | && (segment->p_type == PT_LOAD \ | |
5164 | || segment->p_type == PT_TLS \ | |
5165 | || (section->flags & SEC_THREAD_LOCAL) == 0) \ | |
5166 | && (segment->p_type != PT_DYNAMIC \ | |
5167 | || SECTION_SIZE (section, segment) > 0 \ | |
5168 | || (segment->p_paddr \ | |
5169 | ? segment->p_paddr != section->lma \ | |
5170 | : segment->p_vaddr != section->vma) \ | |
5171 | || (strcmp (bfd_get_section_name (ibfd, section), ".dynamic") \ | |
5172 | == 0)) \ | |
0067a569 | 5173 | && !section->segment_mark) |
bc67d8a6 | 5174 | |
9f17e2a6 L |
5175 | /* If the output section of a section in the input segment is NULL, |
5176 | it is removed from the corresponding output segment. */ | |
5177 | #define INCLUDE_SECTION_IN_SEGMENT(section, segment, bed) \ | |
5178 | (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed) \ | |
5179 | && section->output_section != NULL) | |
5180 | ||
b34976b6 | 5181 | /* Returns TRUE iff seg1 starts after the end of seg2. */ |
b5f852ea NC |
5182 | #define SEGMENT_AFTER_SEGMENT(seg1, seg2, field) \ |
5183 | (seg1->field >= SEGMENT_END (seg2, seg2->field)) | |
5184 | ||
5185 | /* Returns TRUE iff seg1 and seg2 overlap. Segments overlap iff both | |
5186 | their VMA address ranges and their LMA address ranges overlap. | |
5187 | It is possible to have overlapping VMA ranges without overlapping LMA | |
5188 | ranges. RedBoot images for example can have both .data and .bss mapped | |
5189 | to the same VMA range, but with the .data section mapped to a different | |
5190 | LMA. */ | |
aecc8f8a | 5191 | #define SEGMENT_OVERLAPS(seg1, seg2) \ |
b5f852ea | 5192 | ( !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_vaddr) \ |
08a40648 | 5193 | || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_vaddr)) \ |
b5f852ea | 5194 | && !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_paddr) \ |
08a40648 | 5195 | || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_paddr))) |
bc67d8a6 NC |
5196 | |
5197 | /* Initialise the segment mark field. */ | |
5198 | for (section = ibfd->sections; section != NULL; section = section->next) | |
b34976b6 | 5199 | section->segment_mark = FALSE; |
bc67d8a6 | 5200 | |
5c44b38e AM |
5201 | /* The Solaris linker creates program headers in which all the |
5202 | p_paddr fields are zero. When we try to objcopy or strip such a | |
5203 | file, we get confused. Check for this case, and if we find it | |
5204 | don't set the p_paddr_valid fields. */ | |
5205 | p_paddr_valid = FALSE; | |
5206 | for (i = 0, segment = elf_tdata (ibfd)->phdr; | |
5207 | i < num_segments; | |
5208 | i++, segment++) | |
5209 | if (segment->p_paddr != 0) | |
5210 | { | |
5211 | p_paddr_valid = TRUE; | |
5212 | break; | |
5213 | } | |
5214 | ||
252b5132 | 5215 | /* Scan through the segments specified in the program header |
bc67d8a6 | 5216 | of the input BFD. For this first scan we look for overlaps |
9ad5cbcf | 5217 | in the loadable segments. These can be created by weird |
aecc8f8a | 5218 | parameters to objcopy. Also, fix some solaris weirdness. */ |
bc67d8a6 NC |
5219 | for (i = 0, segment = elf_tdata (ibfd)->phdr; |
5220 | i < num_segments; | |
c044fabd | 5221 | i++, segment++) |
252b5132 | 5222 | { |
252b5132 | 5223 | unsigned int j; |
c044fabd | 5224 | Elf_Internal_Phdr *segment2; |
252b5132 | 5225 | |
aecc8f8a AM |
5226 | if (segment->p_type == PT_INTERP) |
5227 | for (section = ibfd->sections; section; section = section->next) | |
5228 | if (IS_SOLARIS_PT_INTERP (segment, section)) | |
5229 | { | |
5230 | /* Mininal change so that the normal section to segment | |
4cc11e76 | 5231 | assignment code will work. */ |
aecc8f8a AM |
5232 | segment->p_vaddr = section->vma; |
5233 | break; | |
5234 | } | |
5235 | ||
bc67d8a6 | 5236 | if (segment->p_type != PT_LOAD) |
b10a8ae0 L |
5237 | { |
5238 | /* Remove PT_GNU_RELRO segment. */ | |
5239 | if (segment->p_type == PT_GNU_RELRO) | |
5240 | segment->p_type = PT_NULL; | |
5241 | continue; | |
5242 | } | |
c044fabd | 5243 | |
bc67d8a6 | 5244 | /* Determine if this segment overlaps any previous segments. */ |
0067a569 | 5245 | for (j = 0, segment2 = elf_tdata (ibfd)->phdr; j < i; j++, segment2++) |
bc67d8a6 NC |
5246 | { |
5247 | bfd_signed_vma extra_length; | |
c044fabd | 5248 | |
bc67d8a6 | 5249 | if (segment2->p_type != PT_LOAD |
0067a569 | 5250 | || !SEGMENT_OVERLAPS (segment, segment2)) |
bc67d8a6 | 5251 | continue; |
c044fabd | 5252 | |
bc67d8a6 NC |
5253 | /* Merge the two segments together. */ |
5254 | if (segment2->p_vaddr < segment->p_vaddr) | |
5255 | { | |
c044fabd | 5256 | /* Extend SEGMENT2 to include SEGMENT and then delete |
08a40648 | 5257 | SEGMENT. */ |
0067a569 AM |
5258 | extra_length = (SEGMENT_END (segment, segment->p_vaddr) |
5259 | - SEGMENT_END (segment2, segment2->p_vaddr)); | |
c044fabd | 5260 | |
bc67d8a6 NC |
5261 | if (extra_length > 0) |
5262 | { | |
0067a569 | 5263 | segment2->p_memsz += extra_length; |
bc67d8a6 NC |
5264 | segment2->p_filesz += extra_length; |
5265 | } | |
c044fabd | 5266 | |
bc67d8a6 | 5267 | segment->p_type = PT_NULL; |
c044fabd | 5268 | |
bc67d8a6 NC |
5269 | /* Since we have deleted P we must restart the outer loop. */ |
5270 | i = 0; | |
5271 | segment = elf_tdata (ibfd)->phdr; | |
5272 | break; | |
5273 | } | |
5274 | else | |
5275 | { | |
c044fabd | 5276 | /* Extend SEGMENT to include SEGMENT2 and then delete |
08a40648 | 5277 | SEGMENT2. */ |
0067a569 AM |
5278 | extra_length = (SEGMENT_END (segment2, segment2->p_vaddr) |
5279 | - SEGMENT_END (segment, segment->p_vaddr)); | |
c044fabd | 5280 | |
bc67d8a6 NC |
5281 | if (extra_length > 0) |
5282 | { | |
0067a569 | 5283 | segment->p_memsz += extra_length; |
bc67d8a6 NC |
5284 | segment->p_filesz += extra_length; |
5285 | } | |
c044fabd | 5286 | |
bc67d8a6 NC |
5287 | segment2->p_type = PT_NULL; |
5288 | } | |
5289 | } | |
5290 | } | |
c044fabd | 5291 | |
bc67d8a6 NC |
5292 | /* The second scan attempts to assign sections to segments. */ |
5293 | for (i = 0, segment = elf_tdata (ibfd)->phdr; | |
5294 | i < num_segments; | |
0067a569 | 5295 | i++, segment++) |
bc67d8a6 | 5296 | { |
0067a569 AM |
5297 | unsigned int section_count; |
5298 | asection **sections; | |
5299 | asection *output_section; | |
5300 | unsigned int isec; | |
5301 | bfd_vma matching_lma; | |
5302 | bfd_vma suggested_lma; | |
5303 | unsigned int j; | |
dc810e39 | 5304 | bfd_size_type amt; |
0067a569 AM |
5305 | asection *first_section; |
5306 | bfd_boolean first_matching_lma; | |
5307 | bfd_boolean first_suggested_lma; | |
bc67d8a6 NC |
5308 | |
5309 | if (segment->p_type == PT_NULL) | |
5310 | continue; | |
c044fabd | 5311 | |
9f17e2a6 | 5312 | first_section = NULL; |
bc67d8a6 | 5313 | /* Compute how many sections might be placed into this segment. */ |
b5f852ea NC |
5314 | for (section = ibfd->sections, section_count = 0; |
5315 | section != NULL; | |
5316 | section = section->next) | |
9f17e2a6 L |
5317 | { |
5318 | /* Find the first section in the input segment, which may be | |
5319 | removed from the corresponding output segment. */ | |
5320 | if (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed)) | |
5321 | { | |
5322 | if (first_section == NULL) | |
5323 | first_section = section; | |
5324 | if (section->output_section != NULL) | |
5325 | ++section_count; | |
5326 | } | |
5327 | } | |
811072d8 | 5328 | |
b5f852ea NC |
5329 | /* Allocate a segment map big enough to contain |
5330 | all of the sections we have selected. */ | |
dc810e39 AM |
5331 | amt = sizeof (struct elf_segment_map); |
5332 | amt += ((bfd_size_type) section_count - 1) * sizeof (asection *); | |
41f8ce69 | 5333 | map = bfd_zalloc (obfd, amt); |
bc67d8a6 | 5334 | if (map == NULL) |
b34976b6 | 5335 | return FALSE; |
252b5132 RH |
5336 | |
5337 | /* Initialise the fields of the segment map. Default to | |
5338 | using the physical address of the segment in the input BFD. */ | |
0067a569 AM |
5339 | map->next = NULL; |
5340 | map->p_type = segment->p_type; | |
5341 | map->p_flags = segment->p_flags; | |
bc67d8a6 | 5342 | map->p_flags_valid = 1; |
55d55ac7 | 5343 | |
9f17e2a6 L |
5344 | /* If the first section in the input segment is removed, there is |
5345 | no need to preserve segment physical address in the corresponding | |
5346 | output segment. */ | |
945c025a | 5347 | if (!first_section || first_section->output_section != NULL) |
9f17e2a6 L |
5348 | { |
5349 | map->p_paddr = segment->p_paddr; | |
5c44b38e | 5350 | map->p_paddr_valid = p_paddr_valid; |
9f17e2a6 | 5351 | } |
252b5132 RH |
5352 | |
5353 | /* Determine if this segment contains the ELF file header | |
5354 | and if it contains the program headers themselves. */ | |
bc67d8a6 NC |
5355 | map->includes_filehdr = (segment->p_offset == 0 |
5356 | && segment->p_filesz >= iehdr->e_ehsize); | |
bc67d8a6 | 5357 | map->includes_phdrs = 0; |
252b5132 | 5358 | |
0067a569 | 5359 | if (!phdr_included || segment->p_type != PT_LOAD) |
252b5132 | 5360 | { |
bc67d8a6 NC |
5361 | map->includes_phdrs = |
5362 | (segment->p_offset <= (bfd_vma) iehdr->e_phoff | |
5363 | && (segment->p_offset + segment->p_filesz | |
252b5132 RH |
5364 | >= ((bfd_vma) iehdr->e_phoff |
5365 | + iehdr->e_phnum * iehdr->e_phentsize))); | |
c044fabd | 5366 | |
bc67d8a6 | 5367 | if (segment->p_type == PT_LOAD && map->includes_phdrs) |
b34976b6 | 5368 | phdr_included = TRUE; |
252b5132 RH |
5369 | } |
5370 | ||
bc67d8a6 | 5371 | if (section_count == 0) |
252b5132 RH |
5372 | { |
5373 | /* Special segments, such as the PT_PHDR segment, may contain | |
5374 | no sections, but ordinary, loadable segments should contain | |
1ed89aa9 NC |
5375 | something. They are allowed by the ELF spec however, so only |
5376 | a warning is produced. */ | |
bc67d8a6 | 5377 | if (segment->p_type == PT_LOAD) |
0067a569 AM |
5378 | (*_bfd_error_handler) (_("%B: warning: Empty loadable segment" |
5379 | " detected, is this intentional ?\n"), | |
5380 | ibfd); | |
252b5132 | 5381 | |
bc67d8a6 | 5382 | map->count = 0; |
c044fabd KH |
5383 | *pointer_to_map = map; |
5384 | pointer_to_map = &map->next; | |
252b5132 RH |
5385 | |
5386 | continue; | |
5387 | } | |
5388 | ||
5389 | /* Now scan the sections in the input BFD again and attempt | |
5390 | to add their corresponding output sections to the segment map. | |
5391 | The problem here is how to handle an output section which has | |
5392 | been moved (ie had its LMA changed). There are four possibilities: | |
5393 | ||
5394 | 1. None of the sections have been moved. | |
5395 | In this case we can continue to use the segment LMA from the | |
5396 | input BFD. | |
5397 | ||
5398 | 2. All of the sections have been moved by the same amount. | |
5399 | In this case we can change the segment's LMA to match the LMA | |
5400 | of the first section. | |
5401 | ||
5402 | 3. Some of the sections have been moved, others have not. | |
5403 | In this case those sections which have not been moved can be | |
5404 | placed in the current segment which will have to have its size, | |
5405 | and possibly its LMA changed, and a new segment or segments will | |
5406 | have to be created to contain the other sections. | |
5407 | ||
b5f852ea | 5408 | 4. The sections have been moved, but not by the same amount. |
252b5132 RH |
5409 | In this case we can change the segment's LMA to match the LMA |
5410 | of the first section and we will have to create a new segment | |
5411 | or segments to contain the other sections. | |
5412 | ||
5413 | In order to save time, we allocate an array to hold the section | |
5414 | pointers that we are interested in. As these sections get assigned | |
5415 | to a segment, they are removed from this array. */ | |
5416 | ||
d0fb9a8d | 5417 | sections = bfd_malloc2 (section_count, sizeof (asection *)); |
252b5132 | 5418 | if (sections == NULL) |
b34976b6 | 5419 | return FALSE; |
252b5132 RH |
5420 | |
5421 | /* Step One: Scan for segment vs section LMA conflicts. | |
5422 | Also add the sections to the section array allocated above. | |
5423 | Also add the sections to the current segment. In the common | |
5424 | case, where the sections have not been moved, this means that | |
5425 | we have completely filled the segment, and there is nothing | |
5426 | more to do. */ | |
252b5132 | 5427 | isec = 0; |
72730e0c | 5428 | matching_lma = 0; |
252b5132 | 5429 | suggested_lma = 0; |
0067a569 AM |
5430 | first_matching_lma = TRUE; |
5431 | first_suggested_lma = TRUE; | |
252b5132 | 5432 | |
147d51c2 | 5433 | for (section = ibfd->sections; |
bc67d8a6 NC |
5434 | section != NULL; |
5435 | section = section->next) | |
147d51c2 L |
5436 | if (section == first_section) |
5437 | break; | |
5438 | ||
5439 | for (j = 0; section != NULL; section = section->next) | |
252b5132 | 5440 | { |
caf47ea6 | 5441 | if (INCLUDE_SECTION_IN_SEGMENT (section, segment, bed)) |
c0f7859b | 5442 | { |
bc67d8a6 NC |
5443 | output_section = section->output_section; |
5444 | ||
0067a569 | 5445 | sections[j++] = section; |
252b5132 RH |
5446 | |
5447 | /* The Solaris native linker always sets p_paddr to 0. | |
5448 | We try to catch that case here, and set it to the | |
5e8d7549 NC |
5449 | correct value. Note - some backends require that |
5450 | p_paddr be left as zero. */ | |
5c44b38e | 5451 | if (!p_paddr_valid |
4455705d | 5452 | && segment->p_vaddr != 0 |
0067a569 | 5453 | && !bed->want_p_paddr_set_to_zero |
252b5132 | 5454 | && isec == 0 |
bc67d8a6 | 5455 | && output_section->lma != 0 |
0067a569 AM |
5456 | && output_section->vma == (segment->p_vaddr |
5457 | + (map->includes_filehdr | |
5458 | ? iehdr->e_ehsize | |
5459 | : 0) | |
5460 | + (map->includes_phdrs | |
5461 | ? (iehdr->e_phnum | |
5462 | * iehdr->e_phentsize) | |
5463 | : 0))) | |
bc67d8a6 | 5464 | map->p_paddr = segment->p_vaddr; |
252b5132 RH |
5465 | |
5466 | /* Match up the physical address of the segment with the | |
5467 | LMA address of the output section. */ | |
bc67d8a6 | 5468 | if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr) |
5e8d7549 | 5469 | || IS_COREFILE_NOTE (segment, section) |
0067a569 AM |
5470 | || (bed->want_p_paddr_set_to_zero |
5471 | && IS_CONTAINED_BY_VMA (output_section, segment))) | |
252b5132 | 5472 | { |
0067a569 AM |
5473 | if (first_matching_lma || output_section->lma < matching_lma) |
5474 | { | |
5475 | matching_lma = output_section->lma; | |
5476 | first_matching_lma = FALSE; | |
5477 | } | |
252b5132 RH |
5478 | |
5479 | /* We assume that if the section fits within the segment | |
bc67d8a6 | 5480 | then it does not overlap any other section within that |
252b5132 | 5481 | segment. */ |
0067a569 AM |
5482 | map->sections[isec++] = output_section; |
5483 | } | |
5484 | else if (first_suggested_lma) | |
5485 | { | |
5486 | suggested_lma = output_section->lma; | |
5487 | first_suggested_lma = FALSE; | |
252b5132 | 5488 | } |
147d51c2 L |
5489 | |
5490 | if (j == section_count) | |
5491 | break; | |
252b5132 RH |
5492 | } |
5493 | } | |
5494 | ||
bc67d8a6 | 5495 | BFD_ASSERT (j == section_count); |
252b5132 RH |
5496 | |
5497 | /* Step Two: Adjust the physical address of the current segment, | |
5498 | if necessary. */ | |
bc67d8a6 | 5499 | if (isec == section_count) |
252b5132 RH |
5500 | { |
5501 | /* All of the sections fitted within the segment as currently | |
5502 | specified. This is the default case. Add the segment to | |
5503 | the list of built segments and carry on to process the next | |
5504 | program header in the input BFD. */ | |
bc67d8a6 | 5505 | map->count = section_count; |
c044fabd KH |
5506 | *pointer_to_map = map; |
5507 | pointer_to_map = &map->next; | |
08a40648 | 5508 | |
5c44b38e AM |
5509 | if (p_paddr_valid |
5510 | && !bed->want_p_paddr_set_to_zero | |
147d51c2 | 5511 | && matching_lma != map->p_paddr |
5c44b38e AM |
5512 | && !map->includes_filehdr |
5513 | && !map->includes_phdrs) | |
3271a814 NS |
5514 | /* There is some padding before the first section in the |
5515 | segment. So, we must account for that in the output | |
5516 | segment's vma. */ | |
5517 | map->p_vaddr_offset = matching_lma - map->p_paddr; | |
08a40648 | 5518 | |
252b5132 RH |
5519 | free (sections); |
5520 | continue; | |
5521 | } | |
252b5132 RH |
5522 | else |
5523 | { | |
0067a569 | 5524 | if (!first_matching_lma) |
72730e0c AM |
5525 | { |
5526 | /* At least one section fits inside the current segment. | |
5527 | Keep it, but modify its physical address to match the | |
5528 | LMA of the first section that fitted. */ | |
bc67d8a6 | 5529 | map->p_paddr = matching_lma; |
72730e0c AM |
5530 | } |
5531 | else | |
5532 | { | |
5533 | /* None of the sections fitted inside the current segment. | |
5534 | Change the current segment's physical address to match | |
5535 | the LMA of the first section. */ | |
bc67d8a6 | 5536 | map->p_paddr = suggested_lma; |
72730e0c AM |
5537 | } |
5538 | ||
bc67d8a6 NC |
5539 | /* Offset the segment physical address from the lma |
5540 | to allow for space taken up by elf headers. */ | |
5541 | if (map->includes_filehdr) | |
5542 | map->p_paddr -= iehdr->e_ehsize; | |
252b5132 | 5543 | |
bc67d8a6 NC |
5544 | if (map->includes_phdrs) |
5545 | { | |
5546 | map->p_paddr -= iehdr->e_phnum * iehdr->e_phentsize; | |
5547 | ||
5548 | /* iehdr->e_phnum is just an estimate of the number | |
5549 | of program headers that we will need. Make a note | |
5550 | here of the number we used and the segment we chose | |
5551 | to hold these headers, so that we can adjust the | |
5552 | offset when we know the correct value. */ | |
5553 | phdr_adjust_num = iehdr->e_phnum; | |
5554 | phdr_adjust_seg = map; | |
5555 | } | |
252b5132 RH |
5556 | } |
5557 | ||
5558 | /* Step Three: Loop over the sections again, this time assigning | |
caf47ea6 | 5559 | those that fit to the current segment and removing them from the |
252b5132 RH |
5560 | sections array; but making sure not to leave large gaps. Once all |
5561 | possible sections have been assigned to the current segment it is | |
5562 | added to the list of built segments and if sections still remain | |
5563 | to be assigned, a new segment is constructed before repeating | |
5564 | the loop. */ | |
5565 | isec = 0; | |
5566 | do | |
5567 | { | |
bc67d8a6 | 5568 | map->count = 0; |
252b5132 | 5569 | suggested_lma = 0; |
0067a569 | 5570 | first_suggested_lma = TRUE; |
252b5132 RH |
5571 | |
5572 | /* Fill the current segment with sections that fit. */ | |
bc67d8a6 | 5573 | for (j = 0; j < section_count; j++) |
252b5132 | 5574 | { |
bc67d8a6 | 5575 | section = sections[j]; |
252b5132 | 5576 | |
bc67d8a6 | 5577 | if (section == NULL) |
252b5132 RH |
5578 | continue; |
5579 | ||
bc67d8a6 | 5580 | output_section = section->output_section; |
252b5132 | 5581 | |
bc67d8a6 | 5582 | BFD_ASSERT (output_section != NULL); |
c044fabd | 5583 | |
bc67d8a6 NC |
5584 | if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr) |
5585 | || IS_COREFILE_NOTE (segment, section)) | |
252b5132 | 5586 | { |
bc67d8a6 | 5587 | if (map->count == 0) |
252b5132 RH |
5588 | { |
5589 | /* If the first section in a segment does not start at | |
bc67d8a6 NC |
5590 | the beginning of the segment, then something is |
5591 | wrong. */ | |
0067a569 AM |
5592 | if (output_section->lma |
5593 | != (map->p_paddr | |
5594 | + (map->includes_filehdr ? iehdr->e_ehsize : 0) | |
5595 | + (map->includes_phdrs | |
5596 | ? iehdr->e_phnum * iehdr->e_phentsize | |
5597 | : 0))) | |
252b5132 RH |
5598 | abort (); |
5599 | } | |
5600 | else | |
5601 | { | |
0067a569 | 5602 | asection *prev_sec; |
252b5132 | 5603 | |
bc67d8a6 | 5604 | prev_sec = map->sections[map->count - 1]; |
252b5132 RH |
5605 | |
5606 | /* If the gap between the end of the previous section | |
bc67d8a6 NC |
5607 | and the start of this section is more than |
5608 | maxpagesize then we need to start a new segment. */ | |
eea6121a | 5609 | if ((BFD_ALIGN (prev_sec->lma + prev_sec->size, |
079e9a2f | 5610 | maxpagesize) |
caf47ea6 | 5611 | < BFD_ALIGN (output_section->lma, maxpagesize)) |
0067a569 | 5612 | || (prev_sec->lma + prev_sec->size |
079e9a2f | 5613 | > output_section->lma)) |
252b5132 | 5614 | { |
0067a569 AM |
5615 | if (first_suggested_lma) |
5616 | { | |
5617 | suggested_lma = output_section->lma; | |
5618 | first_suggested_lma = FALSE; | |
5619 | } | |
252b5132 RH |
5620 | |
5621 | continue; | |
5622 | } | |
5623 | } | |
5624 | ||
bc67d8a6 | 5625 | map->sections[map->count++] = output_section; |
252b5132 RH |
5626 | ++isec; |
5627 | sections[j] = NULL; | |
b34976b6 | 5628 | section->segment_mark = TRUE; |
252b5132 | 5629 | } |
0067a569 AM |
5630 | else if (first_suggested_lma) |
5631 | { | |
5632 | suggested_lma = output_section->lma; | |
5633 | first_suggested_lma = FALSE; | |
5634 | } | |
252b5132 RH |
5635 | } |
5636 | ||
bc67d8a6 | 5637 | BFD_ASSERT (map->count > 0); |
252b5132 RH |
5638 | |
5639 | /* Add the current segment to the list of built segments. */ | |
c044fabd KH |
5640 | *pointer_to_map = map; |
5641 | pointer_to_map = &map->next; | |
252b5132 | 5642 | |
bc67d8a6 | 5643 | if (isec < section_count) |
252b5132 RH |
5644 | { |
5645 | /* We still have not allocated all of the sections to | |
5646 | segments. Create a new segment here, initialise it | |
5647 | and carry on looping. */ | |
dc810e39 AM |
5648 | amt = sizeof (struct elf_segment_map); |
5649 | amt += ((bfd_size_type) section_count - 1) * sizeof (asection *); | |
217aa764 | 5650 | map = bfd_alloc (obfd, amt); |
bc67d8a6 | 5651 | if (map == NULL) |
5ed6aba4 NC |
5652 | { |
5653 | free (sections); | |
5654 | return FALSE; | |
5655 | } | |
252b5132 RH |
5656 | |
5657 | /* Initialise the fields of the segment map. Set the physical | |
5658 | physical address to the LMA of the first section that has | |
5659 | not yet been assigned. */ | |
0067a569 AM |
5660 | map->next = NULL; |
5661 | map->p_type = segment->p_type; | |
5662 | map->p_flags = segment->p_flags; | |
5663 | map->p_flags_valid = 1; | |
5664 | map->p_paddr = suggested_lma; | |
5c44b38e | 5665 | map->p_paddr_valid = p_paddr_valid; |
bc67d8a6 | 5666 | map->includes_filehdr = 0; |
0067a569 | 5667 | map->includes_phdrs = 0; |
252b5132 RH |
5668 | } |
5669 | } | |
bc67d8a6 | 5670 | while (isec < section_count); |
252b5132 RH |
5671 | |
5672 | free (sections); | |
5673 | } | |
5674 | ||
bc67d8a6 NC |
5675 | elf_tdata (obfd)->segment_map = map_first; |
5676 | ||
5677 | /* If we had to estimate the number of program headers that were | |
9ad5cbcf | 5678 | going to be needed, then check our estimate now and adjust |
bc67d8a6 NC |
5679 | the offset if necessary. */ |
5680 | if (phdr_adjust_seg != NULL) | |
5681 | { | |
5682 | unsigned int count; | |
c044fabd | 5683 | |
bc67d8a6 | 5684 | for (count = 0, map = map_first; map != NULL; map = map->next) |
c044fabd | 5685 | count++; |
252b5132 | 5686 | |
bc67d8a6 NC |
5687 | if (count > phdr_adjust_num) |
5688 | phdr_adjust_seg->p_paddr | |
5689 | -= (count - phdr_adjust_num) * iehdr->e_phentsize; | |
5690 | } | |
c044fabd | 5691 | |
bc67d8a6 | 5692 | #undef SEGMENT_END |
eecdbe52 | 5693 | #undef SECTION_SIZE |
bc67d8a6 NC |
5694 | #undef IS_CONTAINED_BY_VMA |
5695 | #undef IS_CONTAINED_BY_LMA | |
0efc80c8 | 5696 | #undef IS_NOTE |
252b5132 | 5697 | #undef IS_COREFILE_NOTE |
bc67d8a6 | 5698 | #undef IS_SOLARIS_PT_INTERP |
9f17e2a6 | 5699 | #undef IS_SECTION_IN_INPUT_SEGMENT |
bc67d8a6 NC |
5700 | #undef INCLUDE_SECTION_IN_SEGMENT |
5701 | #undef SEGMENT_AFTER_SEGMENT | |
5702 | #undef SEGMENT_OVERLAPS | |
b34976b6 | 5703 | return TRUE; |
252b5132 RH |
5704 | } |
5705 | ||
84d1d650 L |
5706 | /* Copy ELF program header information. */ |
5707 | ||
5708 | static bfd_boolean | |
5709 | copy_elf_program_header (bfd *ibfd, bfd *obfd) | |
5710 | { | |
5711 | Elf_Internal_Ehdr *iehdr; | |
5712 | struct elf_segment_map *map; | |
5713 | struct elf_segment_map *map_first; | |
5714 | struct elf_segment_map **pointer_to_map; | |
5715 | Elf_Internal_Phdr *segment; | |
5716 | unsigned int i; | |
5717 | unsigned int num_segments; | |
5718 | bfd_boolean phdr_included = FALSE; | |
88967714 | 5719 | bfd_boolean p_paddr_valid; |
84d1d650 L |
5720 | |
5721 | iehdr = elf_elfheader (ibfd); | |
5722 | ||
5723 | map_first = NULL; | |
5724 | pointer_to_map = &map_first; | |
5725 | ||
88967714 AM |
5726 | /* If all the segment p_paddr fields are zero, don't set |
5727 | map->p_paddr_valid. */ | |
5728 | p_paddr_valid = FALSE; | |
84d1d650 | 5729 | num_segments = elf_elfheader (ibfd)->e_phnum; |
88967714 AM |
5730 | for (i = 0, segment = elf_tdata (ibfd)->phdr; |
5731 | i < num_segments; | |
5732 | i++, segment++) | |
5733 | if (segment->p_paddr != 0) | |
5734 | { | |
5735 | p_paddr_valid = TRUE; | |
5736 | break; | |
5737 | } | |
5738 | ||
84d1d650 L |
5739 | for (i = 0, segment = elf_tdata (ibfd)->phdr; |
5740 | i < num_segments; | |
5741 | i++, segment++) | |
5742 | { | |
5743 | asection *section; | |
5744 | unsigned int section_count; | |
5745 | bfd_size_type amt; | |
5746 | Elf_Internal_Shdr *this_hdr; | |
53020534 | 5747 | asection *first_section = NULL; |
c981028a | 5748 | asection *lowest_section = NULL; |
84d1d650 | 5749 | |
84d1d650 L |
5750 | /* Compute how many sections are in this segment. */ |
5751 | for (section = ibfd->sections, section_count = 0; | |
5752 | section != NULL; | |
5753 | section = section->next) | |
5754 | { | |
5755 | this_hdr = &(elf_section_data(section)->this_hdr); | |
5756 | if (ELF_IS_SECTION_IN_SEGMENT_FILE (this_hdr, segment)) | |
3271a814 | 5757 | { |
53020534 | 5758 | if (!first_section) |
c981028a DJ |
5759 | first_section = lowest_section = section; |
5760 | if (section->lma < lowest_section->lma) | |
5761 | lowest_section = section; | |
3271a814 NS |
5762 | section_count++; |
5763 | } | |
84d1d650 L |
5764 | } |
5765 | ||
5766 | /* Allocate a segment map big enough to contain | |
5767 | all of the sections we have selected. */ | |
5768 | amt = sizeof (struct elf_segment_map); | |
5769 | if (section_count != 0) | |
5770 | amt += ((bfd_size_type) section_count - 1) * sizeof (asection *); | |
41f8ce69 | 5771 | map = bfd_zalloc (obfd, amt); |
84d1d650 L |
5772 | if (map == NULL) |
5773 | return FALSE; | |
5774 | ||
5775 | /* Initialize the fields of the output segment map with the | |
5776 | input segment. */ | |
5777 | map->next = NULL; | |
5778 | map->p_type = segment->p_type; | |
5779 | map->p_flags = segment->p_flags; | |
5780 | map->p_flags_valid = 1; | |
5781 | map->p_paddr = segment->p_paddr; | |
88967714 | 5782 | map->p_paddr_valid = p_paddr_valid; |
3f570048 AM |
5783 | map->p_align = segment->p_align; |
5784 | map->p_align_valid = 1; | |
3271a814 | 5785 | map->p_vaddr_offset = 0; |
84d1d650 | 5786 | |
b10a8ae0 L |
5787 | if (map->p_type == PT_GNU_RELRO |
5788 | && segment->p_filesz == segment->p_memsz) | |
5789 | { | |
5790 | /* The PT_GNU_RELRO segment may contain the first a few | |
5791 | bytes in the .got.plt section even if the whole .got.plt | |
5792 | section isn't in the PT_GNU_RELRO segment. We won't | |
5793 | change the size of the PT_GNU_RELRO segment. */ | |
5794 | map->p_size = segment->p_filesz; | |
5795 | map->p_size_valid = 1; | |
5796 | } | |
5797 | ||
84d1d650 L |
5798 | /* Determine if this segment contains the ELF file header |
5799 | and if it contains the program headers themselves. */ | |
5800 | map->includes_filehdr = (segment->p_offset == 0 | |
5801 | && segment->p_filesz >= iehdr->e_ehsize); | |
5802 | ||
5803 | map->includes_phdrs = 0; | |
5804 | if (! phdr_included || segment->p_type != PT_LOAD) | |
5805 | { | |
5806 | map->includes_phdrs = | |
5807 | (segment->p_offset <= (bfd_vma) iehdr->e_phoff | |
5808 | && (segment->p_offset + segment->p_filesz | |
5809 | >= ((bfd_vma) iehdr->e_phoff | |
5810 | + iehdr->e_phnum * iehdr->e_phentsize))); | |
5811 | ||
5812 | if (segment->p_type == PT_LOAD && map->includes_phdrs) | |
5813 | phdr_included = TRUE; | |
5814 | } | |
5815 | ||
88967714 AM |
5816 | if (!map->includes_phdrs |
5817 | && !map->includes_filehdr | |
5818 | && map->p_paddr_valid) | |
3271a814 | 5819 | /* There is some other padding before the first section. */ |
c981028a | 5820 | map->p_vaddr_offset = ((lowest_section ? lowest_section->lma : 0) |
53020534 | 5821 | - segment->p_paddr); |
08a40648 | 5822 | |
84d1d650 L |
5823 | if (section_count != 0) |
5824 | { | |
5825 | unsigned int isec = 0; | |
5826 | ||
53020534 | 5827 | for (section = first_section; |
84d1d650 L |
5828 | section != NULL; |
5829 | section = section->next) | |
5830 | { | |
5831 | this_hdr = &(elf_section_data(section)->this_hdr); | |
5832 | if (ELF_IS_SECTION_IN_SEGMENT_FILE (this_hdr, segment)) | |
53020534 L |
5833 | { |
5834 | map->sections[isec++] = section->output_section; | |
5835 | if (isec == section_count) | |
5836 | break; | |
5837 | } | |
84d1d650 L |
5838 | } |
5839 | } | |
5840 | ||
5841 | map->count = section_count; | |
5842 | *pointer_to_map = map; | |
5843 | pointer_to_map = &map->next; | |
5844 | } | |
5845 | ||
5846 | elf_tdata (obfd)->segment_map = map_first; | |
5847 | return TRUE; | |
5848 | } | |
5849 | ||
5850 | /* Copy private BFD data. This copies or rewrites ELF program header | |
5851 | information. */ | |
5852 | ||
5853 | static bfd_boolean | |
5854 | copy_private_bfd_data (bfd *ibfd, bfd *obfd) | |
5855 | { | |
84d1d650 L |
5856 | if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour |
5857 | || bfd_get_flavour (obfd) != bfd_target_elf_flavour) | |
5858 | return TRUE; | |
5859 | ||
5860 | if (elf_tdata (ibfd)->phdr == NULL) | |
5861 | return TRUE; | |
5862 | ||
5863 | if (ibfd->xvec == obfd->xvec) | |
5864 | { | |
cb3ff1e5 NC |
5865 | /* Check to see if any sections in the input BFD |
5866 | covered by ELF program header have changed. */ | |
d55ce4e2 | 5867 | Elf_Internal_Phdr *segment; |
84d1d650 L |
5868 | asection *section, *osec; |
5869 | unsigned int i, num_segments; | |
5870 | Elf_Internal_Shdr *this_hdr; | |
147d51c2 L |
5871 | const struct elf_backend_data *bed; |
5872 | ||
5873 | bed = get_elf_backend_data (ibfd); | |
5874 | ||
5875 | /* Regenerate the segment map if p_paddr is set to 0. */ | |
5876 | if (bed->want_p_paddr_set_to_zero) | |
5877 | goto rewrite; | |
84d1d650 L |
5878 | |
5879 | /* Initialize the segment mark field. */ | |
5880 | for (section = obfd->sections; section != NULL; | |
5881 | section = section->next) | |
5882 | section->segment_mark = FALSE; | |
5883 | ||
5884 | num_segments = elf_elfheader (ibfd)->e_phnum; | |
5885 | for (i = 0, segment = elf_tdata (ibfd)->phdr; | |
5886 | i < num_segments; | |
5887 | i++, segment++) | |
5888 | { | |
5f6999aa NC |
5889 | /* PR binutils/3535. The Solaris linker always sets the p_paddr |
5890 | and p_memsz fields of special segments (DYNAMIC, INTERP) to 0 | |
5891 | which severly confuses things, so always regenerate the segment | |
5892 | map in this case. */ | |
5893 | if (segment->p_paddr == 0 | |
5894 | && segment->p_memsz == 0 | |
5895 | && (segment->p_type == PT_INTERP || segment->p_type == PT_DYNAMIC)) | |
cb3ff1e5 | 5896 | goto rewrite; |
5f6999aa | 5897 | |
84d1d650 L |
5898 | for (section = ibfd->sections; |
5899 | section != NULL; section = section->next) | |
5900 | { | |
5901 | /* We mark the output section so that we know it comes | |
5902 | from the input BFD. */ | |
5903 | osec = section->output_section; | |
5904 | if (osec) | |
5905 | osec->segment_mark = TRUE; | |
5906 | ||
5907 | /* Check if this section is covered by the segment. */ | |
5908 | this_hdr = &(elf_section_data(section)->this_hdr); | |
5909 | if (ELF_IS_SECTION_IN_SEGMENT_FILE (this_hdr, segment)) | |
5910 | { | |
5911 | /* FIXME: Check if its output section is changed or | |
5912 | removed. What else do we need to check? */ | |
5913 | if (osec == NULL | |
5914 | || section->flags != osec->flags | |
5915 | || section->lma != osec->lma | |
5916 | || section->vma != osec->vma | |
5917 | || section->size != osec->size | |
5918 | || section->rawsize != osec->rawsize | |
5919 | || section->alignment_power != osec->alignment_power) | |
5920 | goto rewrite; | |
5921 | } | |
5922 | } | |
5923 | } | |
5924 | ||
cb3ff1e5 | 5925 | /* Check to see if any output section do not come from the |
84d1d650 L |
5926 | input BFD. */ |
5927 | for (section = obfd->sections; section != NULL; | |
5928 | section = section->next) | |
5929 | { | |
5930 | if (section->segment_mark == FALSE) | |
5931 | goto rewrite; | |
5932 | else | |
5933 | section->segment_mark = FALSE; | |
5934 | } | |
5935 | ||
5936 | return copy_elf_program_header (ibfd, obfd); | |
5937 | } | |
5938 | ||
5939 | rewrite: | |
5940 | return rewrite_elf_program_header (ibfd, obfd); | |
5941 | } | |
5942 | ||
ccd2ec6a L |
5943 | /* Initialize private output section information from input section. */ |
5944 | ||
5945 | bfd_boolean | |
5946 | _bfd_elf_init_private_section_data (bfd *ibfd, | |
5947 | asection *isec, | |
5948 | bfd *obfd, | |
5949 | asection *osec, | |
5950 | struct bfd_link_info *link_info) | |
5951 | ||
5952 | { | |
5953 | Elf_Internal_Shdr *ihdr, *ohdr; | |
5954 | bfd_boolean need_group = link_info == NULL || link_info->relocatable; | |
5955 | ||
5956 | if (ibfd->xvec->flavour != bfd_target_elf_flavour | |
5957 | || obfd->xvec->flavour != bfd_target_elf_flavour) | |
5958 | return TRUE; | |
5959 | ||
e843e0f8 | 5960 | /* Don't copy the output ELF section type from input if the |
d3fd4074 | 5961 | output BFD section flags have been set to something different. |
e843e0f8 L |
5962 | elf_fake_sections will set ELF section type based on BFD |
5963 | section flags. */ | |
42bb2e33 AM |
5964 | if (elf_section_type (osec) == SHT_NULL |
5965 | && (osec->flags == isec->flags || !osec->flags)) | |
5966 | elf_section_type (osec) = elf_section_type (isec); | |
d270463e L |
5967 | |
5968 | /* FIXME: Is this correct for all OS/PROC specific flags? */ | |
5969 | elf_section_flags (osec) |= (elf_section_flags (isec) | |
5970 | & (SHF_MASKOS | SHF_MASKPROC)); | |
ccd2ec6a L |
5971 | |
5972 | /* Set things up for objcopy and relocatable link. The output | |
5973 | SHT_GROUP section will have its elf_next_in_group pointing back | |
5974 | to the input group members. Ignore linker created group section. | |
5975 | See elfNN_ia64_object_p in elfxx-ia64.c. */ | |
ccd2ec6a L |
5976 | if (need_group) |
5977 | { | |
5978 | if (elf_sec_group (isec) == NULL | |
5979 | || (elf_sec_group (isec)->flags & SEC_LINKER_CREATED) == 0) | |
5980 | { | |
5981 | if (elf_section_flags (isec) & SHF_GROUP) | |
5982 | elf_section_flags (osec) |= SHF_GROUP; | |
5983 | elf_next_in_group (osec) = elf_next_in_group (isec); | |
5984 | elf_group_name (osec) = elf_group_name (isec); | |
5985 | } | |
5986 | } | |
5987 | ||
5988 | ihdr = &elf_section_data (isec)->this_hdr; | |
5989 | ||
5990 | /* We need to handle elf_linked_to_section for SHF_LINK_ORDER. We | |
5991 | don't use the output section of the linked-to section since it | |
5992 | may be NULL at this point. */ | |
5993 | if ((ihdr->sh_flags & SHF_LINK_ORDER) != 0) | |
5994 | { | |
5995 | ohdr = &elf_section_data (osec)->this_hdr; | |
5996 | ohdr->sh_flags |= SHF_LINK_ORDER; | |
5997 | elf_linked_to_section (osec) = elf_linked_to_section (isec); | |
5998 | } | |
5999 | ||
6000 | osec->use_rela_p = isec->use_rela_p; | |
6001 | ||
6002 | return TRUE; | |
6003 | } | |
6004 | ||
252b5132 RH |
6005 | /* Copy private section information. This copies over the entsize |
6006 | field, and sometimes the info field. */ | |
6007 | ||
b34976b6 | 6008 | bfd_boolean |
217aa764 AM |
6009 | _bfd_elf_copy_private_section_data (bfd *ibfd, |
6010 | asection *isec, | |
6011 | bfd *obfd, | |
6012 | asection *osec) | |
252b5132 RH |
6013 | { |
6014 | Elf_Internal_Shdr *ihdr, *ohdr; | |
6015 | ||
6016 | if (ibfd->xvec->flavour != bfd_target_elf_flavour | |
6017 | || obfd->xvec->flavour != bfd_target_elf_flavour) | |
b34976b6 | 6018 | return TRUE; |
252b5132 | 6019 | |
252b5132 RH |
6020 | ihdr = &elf_section_data (isec)->this_hdr; |
6021 | ohdr = &elf_section_data (osec)->this_hdr; | |
6022 | ||
6023 | ohdr->sh_entsize = ihdr->sh_entsize; | |
6024 | ||
6025 | if (ihdr->sh_type == SHT_SYMTAB | |
6026 | || ihdr->sh_type == SHT_DYNSYM | |
6027 | || ihdr->sh_type == SHT_GNU_verneed | |
6028 | || ihdr->sh_type == SHT_GNU_verdef) | |
6029 | ohdr->sh_info = ihdr->sh_info; | |
6030 | ||
ccd2ec6a L |
6031 | return _bfd_elf_init_private_section_data (ibfd, isec, obfd, osec, |
6032 | NULL); | |
252b5132 RH |
6033 | } |
6034 | ||
80fccad2 BW |
6035 | /* Copy private header information. */ |
6036 | ||
6037 | bfd_boolean | |
6038 | _bfd_elf_copy_private_header_data (bfd *ibfd, bfd *obfd) | |
6039 | { | |
30288845 AM |
6040 | asection *isec; |
6041 | ||
80fccad2 BW |
6042 | if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour |
6043 | || bfd_get_flavour (obfd) != bfd_target_elf_flavour) | |
6044 | return TRUE; | |
6045 | ||
6046 | /* Copy over private BFD data if it has not already been copied. | |
6047 | This must be done here, rather than in the copy_private_bfd_data | |
6048 | entry point, because the latter is called after the section | |
6049 | contents have been set, which means that the program headers have | |
6050 | already been worked out. */ | |
6051 | if (elf_tdata (obfd)->segment_map == NULL && elf_tdata (ibfd)->phdr != NULL) | |
6052 | { | |
6053 | if (! copy_private_bfd_data (ibfd, obfd)) | |
6054 | return FALSE; | |
6055 | } | |
6056 | ||
30288845 AM |
6057 | /* _bfd_elf_copy_private_section_data copied over the SHF_GROUP flag |
6058 | but this might be wrong if we deleted the group section. */ | |
6059 | for (isec = ibfd->sections; isec != NULL; isec = isec->next) | |
6060 | if (elf_section_type (isec) == SHT_GROUP | |
6061 | && isec->output_section == NULL) | |
6062 | { | |
6063 | asection *first = elf_next_in_group (isec); | |
6064 | asection *s = first; | |
6065 | while (s != NULL) | |
6066 | { | |
6067 | if (s->output_section != NULL) | |
6068 | { | |
6069 | elf_section_flags (s->output_section) &= ~SHF_GROUP; | |
6070 | elf_group_name (s->output_section) = NULL; | |
6071 | } | |
6072 | s = elf_next_in_group (s); | |
6073 | if (s == first) | |
6074 | break; | |
6075 | } | |
6076 | } | |
6077 | ||
80fccad2 BW |
6078 | return TRUE; |
6079 | } | |
6080 | ||
252b5132 RH |
6081 | /* Copy private symbol information. If this symbol is in a section |
6082 | which we did not map into a BFD section, try to map the section | |
6083 | index correctly. We use special macro definitions for the mapped | |
6084 | section indices; these definitions are interpreted by the | |
6085 | swap_out_syms function. */ | |
6086 | ||
9ad5cbcf AM |
6087 | #define MAP_ONESYMTAB (SHN_HIOS + 1) |
6088 | #define MAP_DYNSYMTAB (SHN_HIOS + 2) | |
6089 | #define MAP_STRTAB (SHN_HIOS + 3) | |
6090 | #define MAP_SHSTRTAB (SHN_HIOS + 4) | |
6091 | #define MAP_SYM_SHNDX (SHN_HIOS + 5) | |
252b5132 | 6092 | |
b34976b6 | 6093 | bfd_boolean |
217aa764 AM |
6094 | _bfd_elf_copy_private_symbol_data (bfd *ibfd, |
6095 | asymbol *isymarg, | |
6096 | bfd *obfd, | |
6097 | asymbol *osymarg) | |
252b5132 RH |
6098 | { |
6099 | elf_symbol_type *isym, *osym; | |
6100 | ||
6101 | if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour | |
6102 | || bfd_get_flavour (obfd) != bfd_target_elf_flavour) | |
b34976b6 | 6103 | return TRUE; |
252b5132 RH |
6104 | |
6105 | isym = elf_symbol_from (ibfd, isymarg); | |
6106 | osym = elf_symbol_from (obfd, osymarg); | |
6107 | ||
6108 | if (isym != NULL | |
8424d8f5 | 6109 | && isym->internal_elf_sym.st_shndx != 0 |
252b5132 RH |
6110 | && osym != NULL |
6111 | && bfd_is_abs_section (isym->symbol.section)) | |
6112 | { | |
6113 | unsigned int shndx; | |
6114 | ||
6115 | shndx = isym->internal_elf_sym.st_shndx; | |
6116 | if (shndx == elf_onesymtab (ibfd)) | |
6117 | shndx = MAP_ONESYMTAB; | |
6118 | else if (shndx == elf_dynsymtab (ibfd)) | |
6119 | shndx = MAP_DYNSYMTAB; | |
6120 | else if (shndx == elf_tdata (ibfd)->strtab_section) | |
6121 | shndx = MAP_STRTAB; | |
6122 | else if (shndx == elf_tdata (ibfd)->shstrtab_section) | |
6123 | shndx = MAP_SHSTRTAB; | |
9ad5cbcf AM |
6124 | else if (shndx == elf_tdata (ibfd)->symtab_shndx_section) |
6125 | shndx = MAP_SYM_SHNDX; | |
252b5132 RH |
6126 | osym->internal_elf_sym.st_shndx = shndx; |
6127 | } | |
6128 | ||
b34976b6 | 6129 | return TRUE; |
252b5132 RH |
6130 | } |
6131 | ||
6132 | /* Swap out the symbols. */ | |
6133 | ||
b34976b6 | 6134 | static bfd_boolean |
217aa764 AM |
6135 | swap_out_syms (bfd *abfd, |
6136 | struct bfd_strtab_hash **sttp, | |
6137 | int relocatable_p) | |
252b5132 | 6138 | { |
9c5bfbb7 | 6139 | const struct elf_backend_data *bed; |
079e9a2f AM |
6140 | int symcount; |
6141 | asymbol **syms; | |
6142 | struct bfd_strtab_hash *stt; | |
6143 | Elf_Internal_Shdr *symtab_hdr; | |
9ad5cbcf | 6144 | Elf_Internal_Shdr *symtab_shndx_hdr; |
079e9a2f | 6145 | Elf_Internal_Shdr *symstrtab_hdr; |
f075ee0c AM |
6146 | bfd_byte *outbound_syms; |
6147 | bfd_byte *outbound_shndx; | |
079e9a2f AM |
6148 | int idx; |
6149 | bfd_size_type amt; | |
174fd7f9 | 6150 | bfd_boolean name_local_sections; |
252b5132 RH |
6151 | |
6152 | if (!elf_map_symbols (abfd)) | |
b34976b6 | 6153 | return FALSE; |
252b5132 | 6154 | |
c044fabd | 6155 | /* Dump out the symtabs. */ |
079e9a2f AM |
6156 | stt = _bfd_elf_stringtab_init (); |
6157 | if (stt == NULL) | |
b34976b6 | 6158 | return FALSE; |
252b5132 | 6159 | |
079e9a2f AM |
6160 | bed = get_elf_backend_data (abfd); |
6161 | symcount = bfd_get_symcount (abfd); | |
6162 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; | |
6163 | symtab_hdr->sh_type = SHT_SYMTAB; | |
6164 | symtab_hdr->sh_entsize = bed->s->sizeof_sym; | |
6165 | symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1); | |
6166 | symtab_hdr->sh_info = elf_num_locals (abfd) + 1; | |
72de5009 | 6167 | symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align; |
079e9a2f AM |
6168 | |
6169 | symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr; | |
6170 | symstrtab_hdr->sh_type = SHT_STRTAB; | |
6171 | ||
d0fb9a8d | 6172 | outbound_syms = bfd_alloc2 (abfd, 1 + symcount, bed->s->sizeof_sym); |
079e9a2f | 6173 | if (outbound_syms == NULL) |
5ed6aba4 NC |
6174 | { |
6175 | _bfd_stringtab_free (stt); | |
6176 | return FALSE; | |
6177 | } | |
217aa764 | 6178 | symtab_hdr->contents = outbound_syms; |
252b5132 | 6179 | |
9ad5cbcf AM |
6180 | outbound_shndx = NULL; |
6181 | symtab_shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr; | |
6182 | if (symtab_shndx_hdr->sh_name != 0) | |
6183 | { | |
6184 | amt = (bfd_size_type) (1 + symcount) * sizeof (Elf_External_Sym_Shndx); | |
d0fb9a8d JJ |
6185 | outbound_shndx = bfd_zalloc2 (abfd, 1 + symcount, |
6186 | sizeof (Elf_External_Sym_Shndx)); | |
9ad5cbcf | 6187 | if (outbound_shndx == NULL) |
5ed6aba4 NC |
6188 | { |
6189 | _bfd_stringtab_free (stt); | |
6190 | return FALSE; | |
6191 | } | |
6192 | ||
9ad5cbcf AM |
6193 | symtab_shndx_hdr->contents = outbound_shndx; |
6194 | symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX; | |
6195 | symtab_shndx_hdr->sh_size = amt; | |
6196 | symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx); | |
6197 | symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx); | |
6198 | } | |
6199 | ||
589e6347 | 6200 | /* Now generate the data (for "contents"). */ |
079e9a2f AM |
6201 | { |
6202 | /* Fill in zeroth symbol and swap it out. */ | |
6203 | Elf_Internal_Sym sym; | |
6204 | sym.st_name = 0; | |
6205 | sym.st_value = 0; | |
6206 | sym.st_size = 0; | |
6207 | sym.st_info = 0; | |
6208 | sym.st_other = 0; | |
6209 | sym.st_shndx = SHN_UNDEF; | |
9ad5cbcf | 6210 | bed->s->swap_symbol_out (abfd, &sym, outbound_syms, outbound_shndx); |
079e9a2f | 6211 | outbound_syms += bed->s->sizeof_sym; |
9ad5cbcf AM |
6212 | if (outbound_shndx != NULL) |
6213 | outbound_shndx += sizeof (Elf_External_Sym_Shndx); | |
079e9a2f | 6214 | } |
252b5132 | 6215 | |
174fd7f9 RS |
6216 | name_local_sections |
6217 | = (bed->elf_backend_name_local_section_symbols | |
6218 | && bed->elf_backend_name_local_section_symbols (abfd)); | |
6219 | ||
079e9a2f AM |
6220 | syms = bfd_get_outsymbols (abfd); |
6221 | for (idx = 0; idx < symcount; idx++) | |
252b5132 | 6222 | { |
252b5132 | 6223 | Elf_Internal_Sym sym; |
079e9a2f AM |
6224 | bfd_vma value = syms[idx]->value; |
6225 | elf_symbol_type *type_ptr; | |
6226 | flagword flags = syms[idx]->flags; | |
6227 | int type; | |
252b5132 | 6228 | |
174fd7f9 RS |
6229 | if (!name_local_sections |
6230 | && (flags & (BSF_SECTION_SYM | BSF_GLOBAL)) == BSF_SECTION_SYM) | |
079e9a2f AM |
6231 | { |
6232 | /* Local section symbols have no name. */ | |
6233 | sym.st_name = 0; | |
6234 | } | |
6235 | else | |
6236 | { | |
6237 | sym.st_name = (unsigned long) _bfd_stringtab_add (stt, | |
6238 | syms[idx]->name, | |
b34976b6 | 6239 | TRUE, FALSE); |
079e9a2f | 6240 | if (sym.st_name == (unsigned long) -1) |
5ed6aba4 NC |
6241 | { |
6242 | _bfd_stringtab_free (stt); | |
6243 | return FALSE; | |
6244 | } | |
079e9a2f | 6245 | } |
252b5132 | 6246 | |
079e9a2f | 6247 | type_ptr = elf_symbol_from (abfd, syms[idx]); |
252b5132 | 6248 | |
079e9a2f AM |
6249 | if ((flags & BSF_SECTION_SYM) == 0 |
6250 | && bfd_is_com_section (syms[idx]->section)) | |
6251 | { | |
6252 | /* ELF common symbols put the alignment into the `value' field, | |
6253 | and the size into the `size' field. This is backwards from | |
6254 | how BFD handles it, so reverse it here. */ | |
6255 | sym.st_size = value; | |
6256 | if (type_ptr == NULL | |
6257 | || type_ptr->internal_elf_sym.st_value == 0) | |
6258 | sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value)); | |
6259 | else | |
6260 | sym.st_value = type_ptr->internal_elf_sym.st_value; | |
6261 | sym.st_shndx = _bfd_elf_section_from_bfd_section | |
6262 | (abfd, syms[idx]->section); | |
6263 | } | |
6264 | else | |
6265 | { | |
6266 | asection *sec = syms[idx]->section; | |
cb33740c | 6267 | unsigned int shndx; |
252b5132 | 6268 | |
079e9a2f AM |
6269 | if (sec->output_section) |
6270 | { | |
6271 | value += sec->output_offset; | |
6272 | sec = sec->output_section; | |
6273 | } | |
589e6347 | 6274 | |
079e9a2f AM |
6275 | /* Don't add in the section vma for relocatable output. */ |
6276 | if (! relocatable_p) | |
6277 | value += sec->vma; | |
6278 | sym.st_value = value; | |
6279 | sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0; | |
6280 | ||
6281 | if (bfd_is_abs_section (sec) | |
6282 | && type_ptr != NULL | |
6283 | && type_ptr->internal_elf_sym.st_shndx != 0) | |
6284 | { | |
6285 | /* This symbol is in a real ELF section which we did | |
6286 | not create as a BFD section. Undo the mapping done | |
6287 | by copy_private_symbol_data. */ | |
6288 | shndx = type_ptr->internal_elf_sym.st_shndx; | |
6289 | switch (shndx) | |
6290 | { | |
6291 | case MAP_ONESYMTAB: | |
6292 | shndx = elf_onesymtab (abfd); | |
6293 | break; | |
6294 | case MAP_DYNSYMTAB: | |
6295 | shndx = elf_dynsymtab (abfd); | |
6296 | break; | |
6297 | case MAP_STRTAB: | |
6298 | shndx = elf_tdata (abfd)->strtab_section; | |
6299 | break; | |
6300 | case MAP_SHSTRTAB: | |
6301 | shndx = elf_tdata (abfd)->shstrtab_section; | |
6302 | break; | |
9ad5cbcf AM |
6303 | case MAP_SYM_SHNDX: |
6304 | shndx = elf_tdata (abfd)->symtab_shndx_section; | |
6305 | break; | |
079e9a2f AM |
6306 | default: |
6307 | break; | |
6308 | } | |
6309 | } | |
6310 | else | |
6311 | { | |
6312 | shndx = _bfd_elf_section_from_bfd_section (abfd, sec); | |
252b5132 | 6313 | |
cb33740c | 6314 | if (shndx == SHN_BAD) |
079e9a2f AM |
6315 | { |
6316 | asection *sec2; | |
6317 | ||
6318 | /* Writing this would be a hell of a lot easier if | |
6319 | we had some decent documentation on bfd, and | |
6320 | knew what to expect of the library, and what to | |
6321 | demand of applications. For example, it | |
6322 | appears that `objcopy' might not set the | |
6323 | section of a symbol to be a section that is | |
6324 | actually in the output file. */ | |
6325 | sec2 = bfd_get_section_by_name (abfd, sec->name); | |
589e6347 NC |
6326 | if (sec2 == NULL) |
6327 | { | |
6328 | _bfd_error_handler (_("\ | |
6329 | Unable to find equivalent output section for symbol '%s' from section '%s'"), | |
6330 | syms[idx]->name ? syms[idx]->name : "<Local sym>", | |
6331 | sec->name); | |
811072d8 | 6332 | bfd_set_error (bfd_error_invalid_operation); |
5ed6aba4 | 6333 | _bfd_stringtab_free (stt); |
589e6347 NC |
6334 | return FALSE; |
6335 | } | |
811072d8 | 6336 | |
079e9a2f | 6337 | shndx = _bfd_elf_section_from_bfd_section (abfd, sec2); |
cb33740c | 6338 | BFD_ASSERT (shndx != SHN_BAD); |
079e9a2f AM |
6339 | } |
6340 | } | |
252b5132 | 6341 | |
079e9a2f AM |
6342 | sym.st_shndx = shndx; |
6343 | } | |
252b5132 | 6344 | |
13ae64f3 JJ |
6345 | if ((flags & BSF_THREAD_LOCAL) != 0) |
6346 | type = STT_TLS; | |
6347 | else if ((flags & BSF_FUNCTION) != 0) | |
079e9a2f AM |
6348 | type = STT_FUNC; |
6349 | else if ((flags & BSF_OBJECT) != 0) | |
6350 | type = STT_OBJECT; | |
d9352518 DB |
6351 | else if ((flags & BSF_RELC) != 0) |
6352 | type = STT_RELC; | |
6353 | else if ((flags & BSF_SRELC) != 0) | |
6354 | type = STT_SRELC; | |
079e9a2f AM |
6355 | else |
6356 | type = STT_NOTYPE; | |
252b5132 | 6357 | |
13ae64f3 JJ |
6358 | if (syms[idx]->section->flags & SEC_THREAD_LOCAL) |
6359 | type = STT_TLS; | |
6360 | ||
589e6347 | 6361 | /* Processor-specific types. */ |
079e9a2f AM |
6362 | if (type_ptr != NULL |
6363 | && bed->elf_backend_get_symbol_type) | |
6364 | type = ((*bed->elf_backend_get_symbol_type) | |
6365 | (&type_ptr->internal_elf_sym, type)); | |
252b5132 | 6366 | |
079e9a2f AM |
6367 | if (flags & BSF_SECTION_SYM) |
6368 | { | |
6369 | if (flags & BSF_GLOBAL) | |
6370 | sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION); | |
6371 | else | |
6372 | sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION); | |
6373 | } | |
6374 | else if (bfd_is_com_section (syms[idx]->section)) | |
0a40daed | 6375 | { |
504b7d20 | 6376 | #ifdef USE_STT_COMMON |
0a40daed MK |
6377 | if (type == STT_OBJECT) |
6378 | sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_COMMON); | |
6379 | else | |
6380 | #else | |
6381 | sym.st_info = ELF_ST_INFO (STB_GLOBAL, type); | |
504b7d20 | 6382 | #endif |
0a40daed | 6383 | } |
079e9a2f AM |
6384 | else if (bfd_is_und_section (syms[idx]->section)) |
6385 | sym.st_info = ELF_ST_INFO (((flags & BSF_WEAK) | |
6386 | ? STB_WEAK | |
6387 | : STB_GLOBAL), | |
6388 | type); | |
6389 | else if (flags & BSF_FILE) | |
6390 | sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE); | |
6391 | else | |
6392 | { | |
6393 | int bind = STB_LOCAL; | |
252b5132 | 6394 | |
079e9a2f AM |
6395 | if (flags & BSF_LOCAL) |
6396 | bind = STB_LOCAL; | |
6397 | else if (flags & BSF_WEAK) | |
6398 | bind = STB_WEAK; | |
6399 | else if (flags & BSF_GLOBAL) | |
6400 | bind = STB_GLOBAL; | |
252b5132 | 6401 | |
079e9a2f AM |
6402 | sym.st_info = ELF_ST_INFO (bind, type); |
6403 | } | |
252b5132 | 6404 | |
079e9a2f AM |
6405 | if (type_ptr != NULL) |
6406 | sym.st_other = type_ptr->internal_elf_sym.st_other; | |
6407 | else | |
6408 | sym.st_other = 0; | |
252b5132 | 6409 | |
9ad5cbcf | 6410 | bed->s->swap_symbol_out (abfd, &sym, outbound_syms, outbound_shndx); |
079e9a2f | 6411 | outbound_syms += bed->s->sizeof_sym; |
9ad5cbcf AM |
6412 | if (outbound_shndx != NULL) |
6413 | outbound_shndx += sizeof (Elf_External_Sym_Shndx); | |
079e9a2f | 6414 | } |
252b5132 | 6415 | |
079e9a2f AM |
6416 | *sttp = stt; |
6417 | symstrtab_hdr->sh_size = _bfd_stringtab_size (stt); | |
6418 | symstrtab_hdr->sh_type = SHT_STRTAB; | |
252b5132 | 6419 | |
079e9a2f AM |
6420 | symstrtab_hdr->sh_flags = 0; |
6421 | symstrtab_hdr->sh_addr = 0; | |
6422 | symstrtab_hdr->sh_entsize = 0; | |
6423 | symstrtab_hdr->sh_link = 0; | |
6424 | symstrtab_hdr->sh_info = 0; | |
6425 | symstrtab_hdr->sh_addralign = 1; | |
252b5132 | 6426 | |
b34976b6 | 6427 | return TRUE; |
252b5132 RH |
6428 | } |
6429 | ||
6430 | /* Return the number of bytes required to hold the symtab vector. | |
6431 | ||
6432 | Note that we base it on the count plus 1, since we will null terminate | |
6433 | the vector allocated based on this size. However, the ELF symbol table | |
6434 | always has a dummy entry as symbol #0, so it ends up even. */ | |
6435 | ||
6436 | long | |
217aa764 | 6437 | _bfd_elf_get_symtab_upper_bound (bfd *abfd) |
252b5132 RH |
6438 | { |
6439 | long symcount; | |
6440 | long symtab_size; | |
6441 | Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr; | |
6442 | ||
6443 | symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym; | |
b99d1833 AM |
6444 | symtab_size = (symcount + 1) * (sizeof (asymbol *)); |
6445 | if (symcount > 0) | |
6446 | symtab_size -= sizeof (asymbol *); | |
252b5132 RH |
6447 | |
6448 | return symtab_size; | |
6449 | } | |
6450 | ||
6451 | long | |
217aa764 | 6452 | _bfd_elf_get_dynamic_symtab_upper_bound (bfd *abfd) |
252b5132 RH |
6453 | { |
6454 | long symcount; | |
6455 | long symtab_size; | |
6456 | Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr; | |
6457 | ||
6458 | if (elf_dynsymtab (abfd) == 0) | |
6459 | { | |
6460 | bfd_set_error (bfd_error_invalid_operation); | |
6461 | return -1; | |
6462 | } | |
6463 | ||
6464 | symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym; | |
b99d1833 AM |
6465 | symtab_size = (symcount + 1) * (sizeof (asymbol *)); |
6466 | if (symcount > 0) | |
6467 | symtab_size -= sizeof (asymbol *); | |
252b5132 RH |
6468 | |
6469 | return symtab_size; | |
6470 | } | |
6471 | ||
6472 | long | |
217aa764 AM |
6473 | _bfd_elf_get_reloc_upper_bound (bfd *abfd ATTRIBUTE_UNUSED, |
6474 | sec_ptr asect) | |
252b5132 RH |
6475 | { |
6476 | return (asect->reloc_count + 1) * sizeof (arelent *); | |
6477 | } | |
6478 | ||
6479 | /* Canonicalize the relocs. */ | |
6480 | ||
6481 | long | |
217aa764 AM |
6482 | _bfd_elf_canonicalize_reloc (bfd *abfd, |
6483 | sec_ptr section, | |
6484 | arelent **relptr, | |
6485 | asymbol **symbols) | |
252b5132 RH |
6486 | { |
6487 | arelent *tblptr; | |
6488 | unsigned int i; | |
9c5bfbb7 | 6489 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
252b5132 | 6490 | |
b34976b6 | 6491 | if (! bed->s->slurp_reloc_table (abfd, section, symbols, FALSE)) |
252b5132 RH |
6492 | return -1; |
6493 | ||
6494 | tblptr = section->relocation; | |
6495 | for (i = 0; i < section->reloc_count; i++) | |
6496 | *relptr++ = tblptr++; | |
6497 | ||
6498 | *relptr = NULL; | |
6499 | ||
6500 | return section->reloc_count; | |
6501 | } | |
6502 | ||
6503 | long | |
6cee3f79 | 6504 | _bfd_elf_canonicalize_symtab (bfd *abfd, asymbol **allocation) |
252b5132 | 6505 | { |
9c5bfbb7 | 6506 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
217aa764 | 6507 | long symcount = bed->s->slurp_symbol_table (abfd, allocation, FALSE); |
252b5132 RH |
6508 | |
6509 | if (symcount >= 0) | |
6510 | bfd_get_symcount (abfd) = symcount; | |
6511 | return symcount; | |
6512 | } | |
6513 | ||
6514 | long | |
217aa764 AM |
6515 | _bfd_elf_canonicalize_dynamic_symtab (bfd *abfd, |
6516 | asymbol **allocation) | |
252b5132 | 6517 | { |
9c5bfbb7 | 6518 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
217aa764 | 6519 | long symcount = bed->s->slurp_symbol_table (abfd, allocation, TRUE); |
1f70368c DJ |
6520 | |
6521 | if (symcount >= 0) | |
6522 | bfd_get_dynamic_symcount (abfd) = symcount; | |
6523 | return symcount; | |
252b5132 RH |
6524 | } |
6525 | ||
8615f3f2 AM |
6526 | /* Return the size required for the dynamic reloc entries. Any loadable |
6527 | section that was actually installed in the BFD, and has type SHT_REL | |
6528 | or SHT_RELA, and uses the dynamic symbol table, is considered to be a | |
6529 | dynamic reloc section. */ | |
252b5132 RH |
6530 | |
6531 | long | |
217aa764 | 6532 | _bfd_elf_get_dynamic_reloc_upper_bound (bfd *abfd) |
252b5132 RH |
6533 | { |
6534 | long ret; | |
6535 | asection *s; | |
6536 | ||
6537 | if (elf_dynsymtab (abfd) == 0) | |
6538 | { | |
6539 | bfd_set_error (bfd_error_invalid_operation); | |
6540 | return -1; | |
6541 | } | |
6542 | ||
6543 | ret = sizeof (arelent *); | |
6544 | for (s = abfd->sections; s != NULL; s = s->next) | |
266b05cf | 6545 | if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd) |
252b5132 RH |
6546 | && (elf_section_data (s)->this_hdr.sh_type == SHT_REL |
6547 | || elf_section_data (s)->this_hdr.sh_type == SHT_RELA)) | |
eea6121a | 6548 | ret += ((s->size / elf_section_data (s)->this_hdr.sh_entsize) |
252b5132 RH |
6549 | * sizeof (arelent *)); |
6550 | ||
6551 | return ret; | |
6552 | } | |
6553 | ||
8615f3f2 AM |
6554 | /* Canonicalize the dynamic relocation entries. Note that we return the |
6555 | dynamic relocations as a single block, although they are actually | |
6556 | associated with particular sections; the interface, which was | |
6557 | designed for SunOS style shared libraries, expects that there is only | |
6558 | one set of dynamic relocs. Any loadable section that was actually | |
6559 | installed in the BFD, and has type SHT_REL or SHT_RELA, and uses the | |
6560 | dynamic symbol table, is considered to be a dynamic reloc section. */ | |
252b5132 RH |
6561 | |
6562 | long | |
217aa764 AM |
6563 | _bfd_elf_canonicalize_dynamic_reloc (bfd *abfd, |
6564 | arelent **storage, | |
6565 | asymbol **syms) | |
252b5132 | 6566 | { |
217aa764 | 6567 | bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean); |
252b5132 RH |
6568 | asection *s; |
6569 | long ret; | |
6570 | ||
6571 | if (elf_dynsymtab (abfd) == 0) | |
6572 | { | |
6573 | bfd_set_error (bfd_error_invalid_operation); | |
6574 | return -1; | |
6575 | } | |
6576 | ||
6577 | slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table; | |
6578 | ret = 0; | |
6579 | for (s = abfd->sections; s != NULL; s = s->next) | |
6580 | { | |
266b05cf | 6581 | if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd) |
252b5132 RH |
6582 | && (elf_section_data (s)->this_hdr.sh_type == SHT_REL |
6583 | || elf_section_data (s)->this_hdr.sh_type == SHT_RELA)) | |
6584 | { | |
6585 | arelent *p; | |
6586 | long count, i; | |
6587 | ||
b34976b6 | 6588 | if (! (*slurp_relocs) (abfd, s, syms, TRUE)) |
252b5132 | 6589 | return -1; |
eea6121a | 6590 | count = s->size / elf_section_data (s)->this_hdr.sh_entsize; |
252b5132 RH |
6591 | p = s->relocation; |
6592 | for (i = 0; i < count; i++) | |
6593 | *storage++ = p++; | |
6594 | ret += count; | |
6595 | } | |
6596 | } | |
6597 | ||
6598 | *storage = NULL; | |
6599 | ||
6600 | return ret; | |
6601 | } | |
6602 | \f | |
6603 | /* Read in the version information. */ | |
6604 | ||
b34976b6 | 6605 | bfd_boolean |
fc0e6df6 | 6606 | _bfd_elf_slurp_version_tables (bfd *abfd, bfd_boolean default_imported_symver) |
252b5132 RH |
6607 | { |
6608 | bfd_byte *contents = NULL; | |
fc0e6df6 PB |
6609 | unsigned int freeidx = 0; |
6610 | ||
6611 | if (elf_dynverref (abfd) != 0) | |
6612 | { | |
6613 | Elf_Internal_Shdr *hdr; | |
6614 | Elf_External_Verneed *everneed; | |
6615 | Elf_Internal_Verneed *iverneed; | |
6616 | unsigned int i; | |
d0fb9a8d | 6617 | bfd_byte *contents_end; |
fc0e6df6 PB |
6618 | |
6619 | hdr = &elf_tdata (abfd)->dynverref_hdr; | |
6620 | ||
d0fb9a8d JJ |
6621 | elf_tdata (abfd)->verref = bfd_zalloc2 (abfd, hdr->sh_info, |
6622 | sizeof (Elf_Internal_Verneed)); | |
fc0e6df6 PB |
6623 | if (elf_tdata (abfd)->verref == NULL) |
6624 | goto error_return; | |
6625 | ||
6626 | elf_tdata (abfd)->cverrefs = hdr->sh_info; | |
6627 | ||
6628 | contents = bfd_malloc (hdr->sh_size); | |
6629 | if (contents == NULL) | |
d0fb9a8d JJ |
6630 | { |
6631 | error_return_verref: | |
6632 | elf_tdata (abfd)->verref = NULL; | |
6633 | elf_tdata (abfd)->cverrefs = 0; | |
6634 | goto error_return; | |
6635 | } | |
fc0e6df6 PB |
6636 | if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0 |
6637 | || bfd_bread (contents, hdr->sh_size, abfd) != hdr->sh_size) | |
d0fb9a8d | 6638 | goto error_return_verref; |
fc0e6df6 | 6639 | |
d0fb9a8d JJ |
6640 | if (hdr->sh_info && hdr->sh_size < sizeof (Elf_External_Verneed)) |
6641 | goto error_return_verref; | |
6642 | ||
6643 | BFD_ASSERT (sizeof (Elf_External_Verneed) | |
6644 | == sizeof (Elf_External_Vernaux)); | |
6645 | contents_end = contents + hdr->sh_size - sizeof (Elf_External_Verneed); | |
fc0e6df6 PB |
6646 | everneed = (Elf_External_Verneed *) contents; |
6647 | iverneed = elf_tdata (abfd)->verref; | |
6648 | for (i = 0; i < hdr->sh_info; i++, iverneed++) | |
6649 | { | |
6650 | Elf_External_Vernaux *evernaux; | |
6651 | Elf_Internal_Vernaux *ivernaux; | |
6652 | unsigned int j; | |
6653 | ||
6654 | _bfd_elf_swap_verneed_in (abfd, everneed, iverneed); | |
6655 | ||
6656 | iverneed->vn_bfd = abfd; | |
6657 | ||
6658 | iverneed->vn_filename = | |
6659 | bfd_elf_string_from_elf_section (abfd, hdr->sh_link, | |
6660 | iverneed->vn_file); | |
6661 | if (iverneed->vn_filename == NULL) | |
d0fb9a8d | 6662 | goto error_return_verref; |
fc0e6df6 | 6663 | |
d0fb9a8d JJ |
6664 | if (iverneed->vn_cnt == 0) |
6665 | iverneed->vn_auxptr = NULL; | |
6666 | else | |
6667 | { | |
6668 | iverneed->vn_auxptr = bfd_alloc2 (abfd, iverneed->vn_cnt, | |
6669 | sizeof (Elf_Internal_Vernaux)); | |
6670 | if (iverneed->vn_auxptr == NULL) | |
6671 | goto error_return_verref; | |
6672 | } | |
6673 | ||
6674 | if (iverneed->vn_aux | |
6675 | > (size_t) (contents_end - (bfd_byte *) everneed)) | |
6676 | goto error_return_verref; | |
fc0e6df6 PB |
6677 | |
6678 | evernaux = ((Elf_External_Vernaux *) | |
6679 | ((bfd_byte *) everneed + iverneed->vn_aux)); | |
6680 | ivernaux = iverneed->vn_auxptr; | |
6681 | for (j = 0; j < iverneed->vn_cnt; j++, ivernaux++) | |
6682 | { | |
6683 | _bfd_elf_swap_vernaux_in (abfd, evernaux, ivernaux); | |
6684 | ||
6685 | ivernaux->vna_nodename = | |
6686 | bfd_elf_string_from_elf_section (abfd, hdr->sh_link, | |
6687 | ivernaux->vna_name); | |
6688 | if (ivernaux->vna_nodename == NULL) | |
d0fb9a8d | 6689 | goto error_return_verref; |
fc0e6df6 PB |
6690 | |
6691 | if (j + 1 < iverneed->vn_cnt) | |
6692 | ivernaux->vna_nextptr = ivernaux + 1; | |
6693 | else | |
6694 | ivernaux->vna_nextptr = NULL; | |
6695 | ||
d0fb9a8d JJ |
6696 | if (ivernaux->vna_next |
6697 | > (size_t) (contents_end - (bfd_byte *) evernaux)) | |
6698 | goto error_return_verref; | |
6699 | ||
fc0e6df6 PB |
6700 | evernaux = ((Elf_External_Vernaux *) |
6701 | ((bfd_byte *) evernaux + ivernaux->vna_next)); | |
6702 | ||
6703 | if (ivernaux->vna_other > freeidx) | |
6704 | freeidx = ivernaux->vna_other; | |
6705 | } | |
6706 | ||
6707 | if (i + 1 < hdr->sh_info) | |
6708 | iverneed->vn_nextref = iverneed + 1; | |
6709 | else | |
6710 | iverneed->vn_nextref = NULL; | |
6711 | ||
d0fb9a8d JJ |
6712 | if (iverneed->vn_next |
6713 | > (size_t) (contents_end - (bfd_byte *) everneed)) | |
6714 | goto error_return_verref; | |
6715 | ||
fc0e6df6 PB |
6716 | everneed = ((Elf_External_Verneed *) |
6717 | ((bfd_byte *) everneed + iverneed->vn_next)); | |
6718 | } | |
6719 | ||
6720 | free (contents); | |
6721 | contents = NULL; | |
6722 | } | |
252b5132 RH |
6723 | |
6724 | if (elf_dynverdef (abfd) != 0) | |
6725 | { | |
6726 | Elf_Internal_Shdr *hdr; | |
6727 | Elf_External_Verdef *everdef; | |
6728 | Elf_Internal_Verdef *iverdef; | |
f631889e UD |
6729 | Elf_Internal_Verdef *iverdefarr; |
6730 | Elf_Internal_Verdef iverdefmem; | |
252b5132 | 6731 | unsigned int i; |
062e2358 | 6732 | unsigned int maxidx; |
d0fb9a8d | 6733 | bfd_byte *contents_end_def, *contents_end_aux; |
252b5132 RH |
6734 | |
6735 | hdr = &elf_tdata (abfd)->dynverdef_hdr; | |
6736 | ||
217aa764 | 6737 | contents = bfd_malloc (hdr->sh_size); |
252b5132 RH |
6738 | if (contents == NULL) |
6739 | goto error_return; | |
6740 | if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0 | |
217aa764 | 6741 | || bfd_bread (contents, hdr->sh_size, abfd) != hdr->sh_size) |
252b5132 RH |
6742 | goto error_return; |
6743 | ||
d0fb9a8d JJ |
6744 | if (hdr->sh_info && hdr->sh_size < sizeof (Elf_External_Verdef)) |
6745 | goto error_return; | |
6746 | ||
6747 | BFD_ASSERT (sizeof (Elf_External_Verdef) | |
6748 | >= sizeof (Elf_External_Verdaux)); | |
6749 | contents_end_def = contents + hdr->sh_size | |
6750 | - sizeof (Elf_External_Verdef); | |
6751 | contents_end_aux = contents + hdr->sh_size | |
6752 | - sizeof (Elf_External_Verdaux); | |
6753 | ||
f631889e UD |
6754 | /* We know the number of entries in the section but not the maximum |
6755 | index. Therefore we have to run through all entries and find | |
6756 | the maximum. */ | |
252b5132 | 6757 | everdef = (Elf_External_Verdef *) contents; |
f631889e UD |
6758 | maxidx = 0; |
6759 | for (i = 0; i < hdr->sh_info; ++i) | |
6760 | { | |
6761 | _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem); | |
6762 | ||
062e2358 AM |
6763 | if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) > maxidx) |
6764 | maxidx = iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION); | |
f631889e | 6765 | |
d0fb9a8d JJ |
6766 | if (iverdefmem.vd_next |
6767 | > (size_t) (contents_end_def - (bfd_byte *) everdef)) | |
6768 | goto error_return; | |
6769 | ||
f631889e UD |
6770 | everdef = ((Elf_External_Verdef *) |
6771 | ((bfd_byte *) everdef + iverdefmem.vd_next)); | |
6772 | } | |
6773 | ||
fc0e6df6 PB |
6774 | if (default_imported_symver) |
6775 | { | |
6776 | if (freeidx > maxidx) | |
6777 | maxidx = ++freeidx; | |
6778 | else | |
6779 | freeidx = ++maxidx; | |
6780 | } | |
d0fb9a8d JJ |
6781 | elf_tdata (abfd)->verdef = bfd_zalloc2 (abfd, maxidx, |
6782 | sizeof (Elf_Internal_Verdef)); | |
f631889e UD |
6783 | if (elf_tdata (abfd)->verdef == NULL) |
6784 | goto error_return; | |
6785 | ||
6786 | elf_tdata (abfd)->cverdefs = maxidx; | |
6787 | ||
6788 | everdef = (Elf_External_Verdef *) contents; | |
6789 | iverdefarr = elf_tdata (abfd)->verdef; | |
6790 | for (i = 0; i < hdr->sh_info; i++) | |
252b5132 RH |
6791 | { |
6792 | Elf_External_Verdaux *everdaux; | |
6793 | Elf_Internal_Verdaux *iverdaux; | |
6794 | unsigned int j; | |
6795 | ||
f631889e UD |
6796 | _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem); |
6797 | ||
d0fb9a8d JJ |
6798 | if ((iverdefmem.vd_ndx & VERSYM_VERSION) == 0) |
6799 | { | |
6800 | error_return_verdef: | |
6801 | elf_tdata (abfd)->verdef = NULL; | |
6802 | elf_tdata (abfd)->cverdefs = 0; | |
6803 | goto error_return; | |
6804 | } | |
6805 | ||
f631889e UD |
6806 | iverdef = &iverdefarr[(iverdefmem.vd_ndx & VERSYM_VERSION) - 1]; |
6807 | memcpy (iverdef, &iverdefmem, sizeof (Elf_Internal_Verdef)); | |
252b5132 RH |
6808 | |
6809 | iverdef->vd_bfd = abfd; | |
6810 | ||
d0fb9a8d JJ |
6811 | if (iverdef->vd_cnt == 0) |
6812 | iverdef->vd_auxptr = NULL; | |
6813 | else | |
6814 | { | |
6815 | iverdef->vd_auxptr = bfd_alloc2 (abfd, iverdef->vd_cnt, | |
6816 | sizeof (Elf_Internal_Verdaux)); | |
6817 | if (iverdef->vd_auxptr == NULL) | |
6818 | goto error_return_verdef; | |
6819 | } | |
6820 | ||
6821 | if (iverdef->vd_aux | |
6822 | > (size_t) (contents_end_aux - (bfd_byte *) everdef)) | |
6823 | goto error_return_verdef; | |
252b5132 RH |
6824 | |
6825 | everdaux = ((Elf_External_Verdaux *) | |
6826 | ((bfd_byte *) everdef + iverdef->vd_aux)); | |
6827 | iverdaux = iverdef->vd_auxptr; | |
6828 | for (j = 0; j < iverdef->vd_cnt; j++, iverdaux++) | |
6829 | { | |
6830 | _bfd_elf_swap_verdaux_in (abfd, everdaux, iverdaux); | |
6831 | ||
6832 | iverdaux->vda_nodename = | |
6833 | bfd_elf_string_from_elf_section (abfd, hdr->sh_link, | |
6834 | iverdaux->vda_name); | |
6835 | if (iverdaux->vda_nodename == NULL) | |
d0fb9a8d | 6836 | goto error_return_verdef; |
252b5132 RH |
6837 | |
6838 | if (j + 1 < iverdef->vd_cnt) | |
6839 | iverdaux->vda_nextptr = iverdaux + 1; | |
6840 | else | |
6841 | iverdaux->vda_nextptr = NULL; | |
6842 | ||
d0fb9a8d JJ |
6843 | if (iverdaux->vda_next |
6844 | > (size_t) (contents_end_aux - (bfd_byte *) everdaux)) | |
6845 | goto error_return_verdef; | |
6846 | ||
252b5132 RH |
6847 | everdaux = ((Elf_External_Verdaux *) |
6848 | ((bfd_byte *) everdaux + iverdaux->vda_next)); | |
6849 | } | |
6850 | ||
d0fb9a8d JJ |
6851 | if (iverdef->vd_cnt) |
6852 | iverdef->vd_nodename = iverdef->vd_auxptr->vda_nodename; | |
252b5132 | 6853 | |
d0fb9a8d | 6854 | if ((size_t) (iverdef - iverdefarr) + 1 < maxidx) |
252b5132 RH |
6855 | iverdef->vd_nextdef = iverdef + 1; |
6856 | else | |
6857 | iverdef->vd_nextdef = NULL; | |
6858 | ||
6859 | everdef = ((Elf_External_Verdef *) | |
6860 | ((bfd_byte *) everdef + iverdef->vd_next)); | |
6861 | } | |
6862 | ||
6863 | free (contents); | |
6864 | contents = NULL; | |
6865 | } | |
fc0e6df6 | 6866 | else if (default_imported_symver) |
252b5132 | 6867 | { |
fc0e6df6 PB |
6868 | if (freeidx < 3) |
6869 | freeidx = 3; | |
6870 | else | |
6871 | freeidx++; | |
252b5132 | 6872 | |
d0fb9a8d JJ |
6873 | elf_tdata (abfd)->verdef = bfd_zalloc2 (abfd, freeidx, |
6874 | sizeof (Elf_Internal_Verdef)); | |
fc0e6df6 | 6875 | if (elf_tdata (abfd)->verdef == NULL) |
252b5132 RH |
6876 | goto error_return; |
6877 | ||
fc0e6df6 PB |
6878 | elf_tdata (abfd)->cverdefs = freeidx; |
6879 | } | |
252b5132 | 6880 | |
fc0e6df6 PB |
6881 | /* Create a default version based on the soname. */ |
6882 | if (default_imported_symver) | |
6883 | { | |
6884 | Elf_Internal_Verdef *iverdef; | |
6885 | Elf_Internal_Verdaux *iverdaux; | |
252b5132 | 6886 | |
fc0e6df6 | 6887 | iverdef = &elf_tdata (abfd)->verdef[freeidx - 1];; |
252b5132 | 6888 | |
fc0e6df6 PB |
6889 | iverdef->vd_version = VER_DEF_CURRENT; |
6890 | iverdef->vd_flags = 0; | |
6891 | iverdef->vd_ndx = freeidx; | |
6892 | iverdef->vd_cnt = 1; | |
252b5132 | 6893 | |
fc0e6df6 | 6894 | iverdef->vd_bfd = abfd; |
252b5132 | 6895 | |
fc0e6df6 PB |
6896 | iverdef->vd_nodename = bfd_elf_get_dt_soname (abfd); |
6897 | if (iverdef->vd_nodename == NULL) | |
d0fb9a8d | 6898 | goto error_return_verdef; |
fc0e6df6 | 6899 | iverdef->vd_nextdef = NULL; |
d0fb9a8d JJ |
6900 | iverdef->vd_auxptr = bfd_alloc (abfd, sizeof (Elf_Internal_Verdaux)); |
6901 | if (iverdef->vd_auxptr == NULL) | |
6902 | goto error_return_verdef; | |
252b5132 | 6903 | |
fc0e6df6 PB |
6904 | iverdaux = iverdef->vd_auxptr; |
6905 | iverdaux->vda_nodename = iverdef->vd_nodename; | |
6906 | iverdaux->vda_nextptr = NULL; | |
252b5132 RH |
6907 | } |
6908 | ||
b34976b6 | 6909 | return TRUE; |
252b5132 RH |
6910 | |
6911 | error_return: | |
5ed6aba4 | 6912 | if (contents != NULL) |
252b5132 | 6913 | free (contents); |
b34976b6 | 6914 | return FALSE; |
252b5132 RH |
6915 | } |
6916 | \f | |
6917 | asymbol * | |
217aa764 | 6918 | _bfd_elf_make_empty_symbol (bfd *abfd) |
252b5132 RH |
6919 | { |
6920 | elf_symbol_type *newsym; | |
dc810e39 | 6921 | bfd_size_type amt = sizeof (elf_symbol_type); |
252b5132 | 6922 | |
217aa764 | 6923 | newsym = bfd_zalloc (abfd, amt); |
252b5132 RH |
6924 | if (!newsym) |
6925 | return NULL; | |
6926 | else | |
6927 | { | |
6928 | newsym->symbol.the_bfd = abfd; | |
6929 | return &newsym->symbol; | |
6930 | } | |
6931 | } | |
6932 | ||
6933 | void | |
217aa764 AM |
6934 | _bfd_elf_get_symbol_info (bfd *abfd ATTRIBUTE_UNUSED, |
6935 | asymbol *symbol, | |
6936 | symbol_info *ret) | |
252b5132 RH |
6937 | { |
6938 | bfd_symbol_info (symbol, ret); | |
6939 | } | |
6940 | ||
6941 | /* Return whether a symbol name implies a local symbol. Most targets | |
6942 | use this function for the is_local_label_name entry point, but some | |
6943 | override it. */ | |
6944 | ||
b34976b6 | 6945 | bfd_boolean |
217aa764 AM |
6946 | _bfd_elf_is_local_label_name (bfd *abfd ATTRIBUTE_UNUSED, |
6947 | const char *name) | |
252b5132 RH |
6948 | { |
6949 | /* Normal local symbols start with ``.L''. */ | |
6950 | if (name[0] == '.' && name[1] == 'L') | |
b34976b6 | 6951 | return TRUE; |
252b5132 RH |
6952 | |
6953 | /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate | |
6954 | DWARF debugging symbols starting with ``..''. */ | |
6955 | if (name[0] == '.' && name[1] == '.') | |
b34976b6 | 6956 | return TRUE; |
252b5132 RH |
6957 | |
6958 | /* gcc will sometimes generate symbols beginning with ``_.L_'' when | |
6959 | emitting DWARF debugging output. I suspect this is actually a | |
6960 | small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call | |
6961 | ASM_GENERATE_INTERNAL_LABEL, and this causes the leading | |
6962 | underscore to be emitted on some ELF targets). For ease of use, | |
6963 | we treat such symbols as local. */ | |
6964 | if (name[0] == '_' && name[1] == '.' && name[2] == 'L' && name[3] == '_') | |
b34976b6 | 6965 | return TRUE; |
252b5132 | 6966 | |
b34976b6 | 6967 | return FALSE; |
252b5132 RH |
6968 | } |
6969 | ||
6970 | alent * | |
217aa764 AM |
6971 | _bfd_elf_get_lineno (bfd *abfd ATTRIBUTE_UNUSED, |
6972 | asymbol *symbol ATTRIBUTE_UNUSED) | |
252b5132 RH |
6973 | { |
6974 | abort (); | |
6975 | return NULL; | |
6976 | } | |
6977 | ||
b34976b6 | 6978 | bfd_boolean |
217aa764 AM |
6979 | _bfd_elf_set_arch_mach (bfd *abfd, |
6980 | enum bfd_architecture arch, | |
6981 | unsigned long machine) | |
252b5132 RH |
6982 | { |
6983 | /* If this isn't the right architecture for this backend, and this | |
6984 | isn't the generic backend, fail. */ | |
6985 | if (arch != get_elf_backend_data (abfd)->arch | |
6986 | && arch != bfd_arch_unknown | |
6987 | && get_elf_backend_data (abfd)->arch != bfd_arch_unknown) | |
b34976b6 | 6988 | return FALSE; |
252b5132 RH |
6989 | |
6990 | return bfd_default_set_arch_mach (abfd, arch, machine); | |
6991 | } | |
6992 | ||
d1fad7c6 NC |
6993 | /* Find the function to a particular section and offset, |
6994 | for error reporting. */ | |
252b5132 | 6995 | |
b34976b6 | 6996 | static bfd_boolean |
217aa764 AM |
6997 | elf_find_function (bfd *abfd ATTRIBUTE_UNUSED, |
6998 | asection *section, | |
6999 | asymbol **symbols, | |
7000 | bfd_vma offset, | |
7001 | const char **filename_ptr, | |
7002 | const char **functionname_ptr) | |
252b5132 | 7003 | { |
252b5132 | 7004 | const char *filename; |
57426232 | 7005 | asymbol *func, *file; |
252b5132 RH |
7006 | bfd_vma low_func; |
7007 | asymbol **p; | |
57426232 JB |
7008 | /* ??? Given multiple file symbols, it is impossible to reliably |
7009 | choose the right file name for global symbols. File symbols are | |
7010 | local symbols, and thus all file symbols must sort before any | |
7011 | global symbols. The ELF spec may be interpreted to say that a | |
7012 | file symbol must sort before other local symbols, but currently | |
7013 | ld -r doesn't do this. So, for ld -r output, it is possible to | |
7014 | make a better choice of file name for local symbols by ignoring | |
7015 | file symbols appearing after a given local symbol. */ | |
7016 | enum { nothing_seen, symbol_seen, file_after_symbol_seen } state; | |
252b5132 | 7017 | |
252b5132 RH |
7018 | filename = NULL; |
7019 | func = NULL; | |
57426232 | 7020 | file = NULL; |
252b5132 | 7021 | low_func = 0; |
57426232 | 7022 | state = nothing_seen; |
252b5132 RH |
7023 | |
7024 | for (p = symbols; *p != NULL; p++) | |
7025 | { | |
7026 | elf_symbol_type *q; | |
7027 | ||
7028 | q = (elf_symbol_type *) *p; | |
7029 | ||
252b5132 RH |
7030 | switch (ELF_ST_TYPE (q->internal_elf_sym.st_info)) |
7031 | { | |
7032 | default: | |
7033 | break; | |
7034 | case STT_FILE: | |
57426232 JB |
7035 | file = &q->symbol; |
7036 | if (state == symbol_seen) | |
7037 | state = file_after_symbol_seen; | |
7038 | continue; | |
252b5132 RH |
7039 | case STT_NOTYPE: |
7040 | case STT_FUNC: | |
6b40fcba | 7041 | if (bfd_get_section (&q->symbol) == section |
252b5132 RH |
7042 | && q->symbol.value >= low_func |
7043 | && q->symbol.value <= offset) | |
7044 | { | |
7045 | func = (asymbol *) q; | |
7046 | low_func = q->symbol.value; | |
a1923858 AM |
7047 | filename = NULL; |
7048 | if (file != NULL | |
7049 | && (ELF_ST_BIND (q->internal_elf_sym.st_info) == STB_LOCAL | |
7050 | || state != file_after_symbol_seen)) | |
57426232 | 7051 | filename = bfd_asymbol_name (file); |
252b5132 RH |
7052 | } |
7053 | break; | |
7054 | } | |
57426232 JB |
7055 | if (state == nothing_seen) |
7056 | state = symbol_seen; | |
252b5132 RH |
7057 | } |
7058 | ||
7059 | if (func == NULL) | |
b34976b6 | 7060 | return FALSE; |
252b5132 | 7061 | |
d1fad7c6 NC |
7062 | if (filename_ptr) |
7063 | *filename_ptr = filename; | |
7064 | if (functionname_ptr) | |
7065 | *functionname_ptr = bfd_asymbol_name (func); | |
7066 | ||
b34976b6 | 7067 | return TRUE; |
d1fad7c6 NC |
7068 | } |
7069 | ||
7070 | /* Find the nearest line to a particular section and offset, | |
7071 | for error reporting. */ | |
7072 | ||
b34976b6 | 7073 | bfd_boolean |
217aa764 AM |
7074 | _bfd_elf_find_nearest_line (bfd *abfd, |
7075 | asection *section, | |
7076 | asymbol **symbols, | |
7077 | bfd_vma offset, | |
7078 | const char **filename_ptr, | |
7079 | const char **functionname_ptr, | |
7080 | unsigned int *line_ptr) | |
d1fad7c6 | 7081 | { |
b34976b6 | 7082 | bfd_boolean found; |
d1fad7c6 NC |
7083 | |
7084 | if (_bfd_dwarf1_find_nearest_line (abfd, section, symbols, offset, | |
4e8a9624 AM |
7085 | filename_ptr, functionname_ptr, |
7086 | line_ptr)) | |
d1fad7c6 NC |
7087 | { |
7088 | if (!*functionname_ptr) | |
4e8a9624 AM |
7089 | elf_find_function (abfd, section, symbols, offset, |
7090 | *filename_ptr ? NULL : filename_ptr, | |
7091 | functionname_ptr); | |
7092 | ||
b34976b6 | 7093 | return TRUE; |
d1fad7c6 NC |
7094 | } |
7095 | ||
7096 | if (_bfd_dwarf2_find_nearest_line (abfd, section, symbols, offset, | |
4e8a9624 AM |
7097 | filename_ptr, functionname_ptr, |
7098 | line_ptr, 0, | |
7099 | &elf_tdata (abfd)->dwarf2_find_line_info)) | |
d1fad7c6 NC |
7100 | { |
7101 | if (!*functionname_ptr) | |
4e8a9624 AM |
7102 | elf_find_function (abfd, section, symbols, offset, |
7103 | *filename_ptr ? NULL : filename_ptr, | |
7104 | functionname_ptr); | |
7105 | ||
b34976b6 | 7106 | return TRUE; |
d1fad7c6 NC |
7107 | } |
7108 | ||
7109 | if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset, | |
4e8a9624 AM |
7110 | &found, filename_ptr, |
7111 | functionname_ptr, line_ptr, | |
7112 | &elf_tdata (abfd)->line_info)) | |
b34976b6 | 7113 | return FALSE; |
dc43ada5 | 7114 | if (found && (*functionname_ptr || *line_ptr)) |
b34976b6 | 7115 | return TRUE; |
d1fad7c6 NC |
7116 | |
7117 | if (symbols == NULL) | |
b34976b6 | 7118 | return FALSE; |
d1fad7c6 NC |
7119 | |
7120 | if (! elf_find_function (abfd, section, symbols, offset, | |
4e8a9624 | 7121 | filename_ptr, functionname_ptr)) |
b34976b6 | 7122 | return FALSE; |
d1fad7c6 | 7123 | |
252b5132 | 7124 | *line_ptr = 0; |
b34976b6 | 7125 | return TRUE; |
252b5132 RH |
7126 | } |
7127 | ||
5420f73d L |
7128 | /* Find the line for a symbol. */ |
7129 | ||
7130 | bfd_boolean | |
7131 | _bfd_elf_find_line (bfd *abfd, asymbol **symbols, asymbol *symbol, | |
7132 | const char **filename_ptr, unsigned int *line_ptr) | |
7133 | { | |
7134 | return _bfd_dwarf2_find_line (abfd, symbols, symbol, | |
7135 | filename_ptr, line_ptr, 0, | |
7136 | &elf_tdata (abfd)->dwarf2_find_line_info); | |
7137 | } | |
7138 | ||
4ab527b0 FF |
7139 | /* After a call to bfd_find_nearest_line, successive calls to |
7140 | bfd_find_inliner_info can be used to get source information about | |
7141 | each level of function inlining that terminated at the address | |
7142 | passed to bfd_find_nearest_line. Currently this is only supported | |
7143 | for DWARF2 with appropriate DWARF3 extensions. */ | |
7144 | ||
7145 | bfd_boolean | |
7146 | _bfd_elf_find_inliner_info (bfd *abfd, | |
7147 | const char **filename_ptr, | |
7148 | const char **functionname_ptr, | |
7149 | unsigned int *line_ptr) | |
7150 | { | |
7151 | bfd_boolean found; | |
7152 | found = _bfd_dwarf2_find_inliner_info (abfd, filename_ptr, | |
7153 | functionname_ptr, line_ptr, | |
7154 | & elf_tdata (abfd)->dwarf2_find_line_info); | |
7155 | return found; | |
7156 | } | |
7157 | ||
252b5132 | 7158 | int |
a6b96beb | 7159 | _bfd_elf_sizeof_headers (bfd *abfd, struct bfd_link_info *info) |
252b5132 | 7160 | { |
8ded5a0f AM |
7161 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
7162 | int ret = bed->s->sizeof_ehdr; | |
252b5132 | 7163 | |
a6b96beb | 7164 | if (!info->relocatable) |
8ded5a0f | 7165 | { |
62d7a5f6 | 7166 | bfd_size_type phdr_size = elf_tdata (abfd)->program_header_size; |
8ded5a0f | 7167 | |
62d7a5f6 AM |
7168 | if (phdr_size == (bfd_size_type) -1) |
7169 | { | |
7170 | struct elf_segment_map *m; | |
7171 | ||
7172 | phdr_size = 0; | |
7173 | for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next) | |
7174 | phdr_size += bed->s->sizeof_phdr; | |
8ded5a0f | 7175 | |
62d7a5f6 AM |
7176 | if (phdr_size == 0) |
7177 | phdr_size = get_program_header_size (abfd, info); | |
7178 | } | |
8ded5a0f AM |
7179 | |
7180 | elf_tdata (abfd)->program_header_size = phdr_size; | |
7181 | ret += phdr_size; | |
7182 | } | |
7183 | ||
252b5132 RH |
7184 | return ret; |
7185 | } | |
7186 | ||
b34976b6 | 7187 | bfd_boolean |
217aa764 AM |
7188 | _bfd_elf_set_section_contents (bfd *abfd, |
7189 | sec_ptr section, | |
0f867abe | 7190 | const void *location, |
217aa764 AM |
7191 | file_ptr offset, |
7192 | bfd_size_type count) | |
252b5132 RH |
7193 | { |
7194 | Elf_Internal_Shdr *hdr; | |
dc810e39 | 7195 | bfd_signed_vma pos; |
252b5132 RH |
7196 | |
7197 | if (! abfd->output_has_begun | |
217aa764 | 7198 | && ! _bfd_elf_compute_section_file_positions (abfd, NULL)) |
b34976b6 | 7199 | return FALSE; |
252b5132 RH |
7200 | |
7201 | hdr = &elf_section_data (section)->this_hdr; | |
dc810e39 AM |
7202 | pos = hdr->sh_offset + offset; |
7203 | if (bfd_seek (abfd, pos, SEEK_SET) != 0 | |
7204 | || bfd_bwrite (location, count, abfd) != count) | |
b34976b6 | 7205 | return FALSE; |
252b5132 | 7206 | |
b34976b6 | 7207 | return TRUE; |
252b5132 RH |
7208 | } |
7209 | ||
7210 | void | |
217aa764 AM |
7211 | _bfd_elf_no_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED, |
7212 | arelent *cache_ptr ATTRIBUTE_UNUSED, | |
7213 | Elf_Internal_Rela *dst ATTRIBUTE_UNUSED) | |
252b5132 RH |
7214 | { |
7215 | abort (); | |
7216 | } | |
7217 | ||
252b5132 RH |
7218 | /* Try to convert a non-ELF reloc into an ELF one. */ |
7219 | ||
b34976b6 | 7220 | bfd_boolean |
217aa764 | 7221 | _bfd_elf_validate_reloc (bfd *abfd, arelent *areloc) |
252b5132 | 7222 | { |
c044fabd | 7223 | /* Check whether we really have an ELF howto. */ |
252b5132 RH |
7224 | |
7225 | if ((*areloc->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec) | |
7226 | { | |
7227 | bfd_reloc_code_real_type code; | |
7228 | reloc_howto_type *howto; | |
7229 | ||
7230 | /* Alien reloc: Try to determine its type to replace it with an | |
c044fabd | 7231 | equivalent ELF reloc. */ |
252b5132 RH |
7232 | |
7233 | if (areloc->howto->pc_relative) | |
7234 | { | |
7235 | switch (areloc->howto->bitsize) | |
7236 | { | |
7237 | case 8: | |
7238 | code = BFD_RELOC_8_PCREL; | |
7239 | break; | |
7240 | case 12: | |
7241 | code = BFD_RELOC_12_PCREL; | |
7242 | break; | |
7243 | case 16: | |
7244 | code = BFD_RELOC_16_PCREL; | |
7245 | break; | |
7246 | case 24: | |
7247 | code = BFD_RELOC_24_PCREL; | |
7248 | break; | |
7249 | case 32: | |
7250 | code = BFD_RELOC_32_PCREL; | |
7251 | break; | |
7252 | case 64: | |
7253 | code = BFD_RELOC_64_PCREL; | |
7254 | break; | |
7255 | default: | |
7256 | goto fail; | |
7257 | } | |
7258 | ||
7259 | howto = bfd_reloc_type_lookup (abfd, code); | |
7260 | ||
7261 | if (areloc->howto->pcrel_offset != howto->pcrel_offset) | |
7262 | { | |
7263 | if (howto->pcrel_offset) | |
7264 | areloc->addend += areloc->address; | |
7265 | else | |
7266 | areloc->addend -= areloc->address; /* addend is unsigned!! */ | |
7267 | } | |
7268 | } | |
7269 | else | |
7270 | { | |
7271 | switch (areloc->howto->bitsize) | |
7272 | { | |
7273 | case 8: | |
7274 | code = BFD_RELOC_8; | |
7275 | break; | |
7276 | case 14: | |
7277 | code = BFD_RELOC_14; | |
7278 | break; | |
7279 | case 16: | |
7280 | code = BFD_RELOC_16; | |
7281 | break; | |
7282 | case 26: | |
7283 | code = BFD_RELOC_26; | |
7284 | break; | |
7285 | case 32: | |
7286 | code = BFD_RELOC_32; | |
7287 | break; | |
7288 | case 64: | |
7289 | code = BFD_RELOC_64; | |
7290 | break; | |
7291 | default: | |
7292 | goto fail; | |
7293 | } | |
7294 | ||
7295 | howto = bfd_reloc_type_lookup (abfd, code); | |
7296 | } | |
7297 | ||
7298 | if (howto) | |
7299 | areloc->howto = howto; | |
7300 | else | |
7301 | goto fail; | |
7302 | } | |
7303 | ||
b34976b6 | 7304 | return TRUE; |
252b5132 RH |
7305 | |
7306 | fail: | |
7307 | (*_bfd_error_handler) | |
d003868e AM |
7308 | (_("%B: unsupported relocation type %s"), |
7309 | abfd, areloc->howto->name); | |
252b5132 | 7310 | bfd_set_error (bfd_error_bad_value); |
b34976b6 | 7311 | return FALSE; |
252b5132 RH |
7312 | } |
7313 | ||
b34976b6 | 7314 | bfd_boolean |
217aa764 | 7315 | _bfd_elf_close_and_cleanup (bfd *abfd) |
252b5132 RH |
7316 | { |
7317 | if (bfd_get_format (abfd) == bfd_object) | |
7318 | { | |
b25e3d87 | 7319 | if (elf_tdata (abfd) != NULL && elf_shstrtab (abfd) != NULL) |
2b0f7ef9 | 7320 | _bfd_elf_strtab_free (elf_shstrtab (abfd)); |
6f140a15 | 7321 | _bfd_dwarf2_cleanup_debug_info (abfd); |
252b5132 RH |
7322 | } |
7323 | ||
7324 | return _bfd_generic_close_and_cleanup (abfd); | |
7325 | } | |
7326 | ||
7327 | /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY | |
7328 | in the relocation's offset. Thus we cannot allow any sort of sanity | |
7329 | range-checking to interfere. There is nothing else to do in processing | |
7330 | this reloc. */ | |
7331 | ||
7332 | bfd_reloc_status_type | |
217aa764 AM |
7333 | _bfd_elf_rel_vtable_reloc_fn |
7334 | (bfd *abfd ATTRIBUTE_UNUSED, arelent *re ATTRIBUTE_UNUSED, | |
fc0a2244 | 7335 | struct bfd_symbol *symbol ATTRIBUTE_UNUSED, |
217aa764 AM |
7336 | void *data ATTRIBUTE_UNUSED, asection *is ATTRIBUTE_UNUSED, |
7337 | bfd *obfd ATTRIBUTE_UNUSED, char **errmsg ATTRIBUTE_UNUSED) | |
252b5132 RH |
7338 | { |
7339 | return bfd_reloc_ok; | |
7340 | } | |
252b5132 RH |
7341 | \f |
7342 | /* Elf core file support. Much of this only works on native | |
7343 | toolchains, since we rely on knowing the | |
7344 | machine-dependent procfs structure in order to pick | |
c044fabd | 7345 | out details about the corefile. */ |
252b5132 RH |
7346 | |
7347 | #ifdef HAVE_SYS_PROCFS_H | |
7348 | # include <sys/procfs.h> | |
7349 | #endif | |
7350 | ||
c044fabd | 7351 | /* FIXME: this is kinda wrong, but it's what gdb wants. */ |
252b5132 RH |
7352 | |
7353 | static int | |
217aa764 | 7354 | elfcore_make_pid (bfd *abfd) |
252b5132 RH |
7355 | { |
7356 | return ((elf_tdata (abfd)->core_lwpid << 16) | |
7357 | + (elf_tdata (abfd)->core_pid)); | |
7358 | } | |
7359 | ||
252b5132 RH |
7360 | /* If there isn't a section called NAME, make one, using |
7361 | data from SECT. Note, this function will generate a | |
7362 | reference to NAME, so you shouldn't deallocate or | |
c044fabd | 7363 | overwrite it. */ |
252b5132 | 7364 | |
b34976b6 | 7365 | static bfd_boolean |
217aa764 | 7366 | elfcore_maybe_make_sect (bfd *abfd, char *name, asection *sect) |
252b5132 | 7367 | { |
c044fabd | 7368 | asection *sect2; |
252b5132 RH |
7369 | |
7370 | if (bfd_get_section_by_name (abfd, name) != NULL) | |
b34976b6 | 7371 | return TRUE; |
252b5132 | 7372 | |
117ed4f8 | 7373 | sect2 = bfd_make_section_with_flags (abfd, name, sect->flags); |
252b5132 | 7374 | if (sect2 == NULL) |
b34976b6 | 7375 | return FALSE; |
252b5132 | 7376 | |
eea6121a | 7377 | sect2->size = sect->size; |
252b5132 | 7378 | sect2->filepos = sect->filepos; |
252b5132 | 7379 | sect2->alignment_power = sect->alignment_power; |
b34976b6 | 7380 | return TRUE; |
252b5132 RH |
7381 | } |
7382 | ||
bb0082d6 AM |
7383 | /* Create a pseudosection containing SIZE bytes at FILEPOS. This |
7384 | actually creates up to two pseudosections: | |
7385 | - For the single-threaded case, a section named NAME, unless | |
7386 | such a section already exists. | |
7387 | - For the multi-threaded case, a section named "NAME/PID", where | |
7388 | PID is elfcore_make_pid (abfd). | |
7389 | Both pseudosections have identical contents. */ | |
b34976b6 | 7390 | bfd_boolean |
217aa764 AM |
7391 | _bfd_elfcore_make_pseudosection (bfd *abfd, |
7392 | char *name, | |
7393 | size_t size, | |
7394 | ufile_ptr filepos) | |
bb0082d6 AM |
7395 | { |
7396 | char buf[100]; | |
7397 | char *threaded_name; | |
d4c88bbb | 7398 | size_t len; |
bb0082d6 AM |
7399 | asection *sect; |
7400 | ||
7401 | /* Build the section name. */ | |
7402 | ||
7403 | sprintf (buf, "%s/%d", name, elfcore_make_pid (abfd)); | |
d4c88bbb | 7404 | len = strlen (buf) + 1; |
217aa764 | 7405 | threaded_name = bfd_alloc (abfd, len); |
bb0082d6 | 7406 | if (threaded_name == NULL) |
b34976b6 | 7407 | return FALSE; |
d4c88bbb | 7408 | memcpy (threaded_name, buf, len); |
bb0082d6 | 7409 | |
117ed4f8 AM |
7410 | sect = bfd_make_section_anyway_with_flags (abfd, threaded_name, |
7411 | SEC_HAS_CONTENTS); | |
bb0082d6 | 7412 | if (sect == NULL) |
b34976b6 | 7413 | return FALSE; |
eea6121a | 7414 | sect->size = size; |
bb0082d6 | 7415 | sect->filepos = filepos; |
bb0082d6 AM |
7416 | sect->alignment_power = 2; |
7417 | ||
936e320b | 7418 | return elfcore_maybe_make_sect (abfd, name, sect); |
bb0082d6 AM |
7419 | } |
7420 | ||
252b5132 | 7421 | /* prstatus_t exists on: |
4a938328 | 7422 | solaris 2.5+ |
252b5132 RH |
7423 | linux 2.[01] + glibc |
7424 | unixware 4.2 | |
7425 | */ | |
7426 | ||
7427 | #if defined (HAVE_PRSTATUS_T) | |
a7b97311 | 7428 | |
b34976b6 | 7429 | static bfd_boolean |
217aa764 | 7430 | elfcore_grok_prstatus (bfd *abfd, Elf_Internal_Note *note) |
252b5132 | 7431 | { |
eea6121a | 7432 | size_t size; |
7ee38065 | 7433 | int offset; |
252b5132 | 7434 | |
4a938328 MS |
7435 | if (note->descsz == sizeof (prstatus_t)) |
7436 | { | |
7437 | prstatus_t prstat; | |
252b5132 | 7438 | |
eea6121a | 7439 | size = sizeof (prstat.pr_reg); |
7ee38065 | 7440 | offset = offsetof (prstatus_t, pr_reg); |
4a938328 | 7441 | memcpy (&prstat, note->descdata, sizeof (prstat)); |
252b5132 | 7442 | |
fa49d224 NC |
7443 | /* Do not overwrite the core signal if it |
7444 | has already been set by another thread. */ | |
7445 | if (elf_tdata (abfd)->core_signal == 0) | |
7446 | elf_tdata (abfd)->core_signal = prstat.pr_cursig; | |
4a938328 | 7447 | elf_tdata (abfd)->core_pid = prstat.pr_pid; |
252b5132 | 7448 | |
4a938328 MS |
7449 | /* pr_who exists on: |
7450 | solaris 2.5+ | |
7451 | unixware 4.2 | |
7452 | pr_who doesn't exist on: | |
7453 | linux 2.[01] | |
7454 | */ | |
252b5132 | 7455 | #if defined (HAVE_PRSTATUS_T_PR_WHO) |
4a938328 | 7456 | elf_tdata (abfd)->core_lwpid = prstat.pr_who; |
252b5132 | 7457 | #endif |
4a938328 | 7458 | } |
7ee38065 | 7459 | #if defined (HAVE_PRSTATUS32_T) |
4a938328 MS |
7460 | else if (note->descsz == sizeof (prstatus32_t)) |
7461 | { | |
7462 | /* 64-bit host, 32-bit corefile */ | |
7463 | prstatus32_t prstat; | |
7464 | ||
eea6121a | 7465 | size = sizeof (prstat.pr_reg); |
7ee38065 | 7466 | offset = offsetof (prstatus32_t, pr_reg); |
4a938328 MS |
7467 | memcpy (&prstat, note->descdata, sizeof (prstat)); |
7468 | ||
fa49d224 NC |
7469 | /* Do not overwrite the core signal if it |
7470 | has already been set by another thread. */ | |
7471 | if (elf_tdata (abfd)->core_signal == 0) | |
7472 | elf_tdata (abfd)->core_signal = prstat.pr_cursig; | |
4a938328 MS |
7473 | elf_tdata (abfd)->core_pid = prstat.pr_pid; |
7474 | ||
7475 | /* pr_who exists on: | |
7476 | solaris 2.5+ | |
7477 | unixware 4.2 | |
7478 | pr_who doesn't exist on: | |
7479 | linux 2.[01] | |
7480 | */ | |
7ee38065 | 7481 | #if defined (HAVE_PRSTATUS32_T_PR_WHO) |
4a938328 MS |
7482 | elf_tdata (abfd)->core_lwpid = prstat.pr_who; |
7483 | #endif | |
7484 | } | |
7ee38065 | 7485 | #endif /* HAVE_PRSTATUS32_T */ |
4a938328 MS |
7486 | else |
7487 | { | |
7488 | /* Fail - we don't know how to handle any other | |
7489 | note size (ie. data object type). */ | |
b34976b6 | 7490 | return TRUE; |
4a938328 | 7491 | } |
252b5132 | 7492 | |
bb0082d6 | 7493 | /* Make a ".reg/999" section and a ".reg" section. */ |
936e320b | 7494 | return _bfd_elfcore_make_pseudosection (abfd, ".reg", |
eea6121a | 7495 | size, note->descpos + offset); |
252b5132 RH |
7496 | } |
7497 | #endif /* defined (HAVE_PRSTATUS_T) */ | |
7498 | ||
bb0082d6 | 7499 | /* Create a pseudosection containing the exact contents of NOTE. */ |
b34976b6 | 7500 | static bfd_boolean |
217aa764 AM |
7501 | elfcore_make_note_pseudosection (bfd *abfd, |
7502 | char *name, | |
7503 | Elf_Internal_Note *note) | |
252b5132 | 7504 | { |
936e320b AM |
7505 | return _bfd_elfcore_make_pseudosection (abfd, name, |
7506 | note->descsz, note->descpos); | |
252b5132 RH |
7507 | } |
7508 | ||
ff08c6bb JB |
7509 | /* There isn't a consistent prfpregset_t across platforms, |
7510 | but it doesn't matter, because we don't have to pick this | |
c044fabd KH |
7511 | data structure apart. */ |
7512 | ||
b34976b6 | 7513 | static bfd_boolean |
217aa764 | 7514 | elfcore_grok_prfpreg (bfd *abfd, Elf_Internal_Note *note) |
ff08c6bb JB |
7515 | { |
7516 | return elfcore_make_note_pseudosection (abfd, ".reg2", note); | |
7517 | } | |
7518 | ||
ff08c6bb | 7519 | /* Linux dumps the Intel SSE regs in a note named "LINUX" with a note |
971d4640 | 7520 | type of NT_PRXFPREG. Just include the whole note's contents |
ff08c6bb | 7521 | literally. */ |
c044fabd | 7522 | |
b34976b6 | 7523 | static bfd_boolean |
217aa764 | 7524 | elfcore_grok_prxfpreg (bfd *abfd, Elf_Internal_Note *note) |
ff08c6bb JB |
7525 | { |
7526 | return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note); | |
7527 | } | |
7528 | ||
97753bd5 AM |
7529 | static bfd_boolean |
7530 | elfcore_grok_ppc_vmx (bfd *abfd, Elf_Internal_Note *note) | |
7531 | { | |
7532 | return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vmx", note); | |
7533 | } | |
7534 | ||
7535 | ||
252b5132 | 7536 | #if defined (HAVE_PRPSINFO_T) |
4a938328 | 7537 | typedef prpsinfo_t elfcore_psinfo_t; |
7ee38065 | 7538 | #if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */ |
4a938328 MS |
7539 | typedef prpsinfo32_t elfcore_psinfo32_t; |
7540 | #endif | |
252b5132 RH |
7541 | #endif |
7542 | ||
7543 | #if defined (HAVE_PSINFO_T) | |
4a938328 | 7544 | typedef psinfo_t elfcore_psinfo_t; |
7ee38065 | 7545 | #if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */ |
4a938328 MS |
7546 | typedef psinfo32_t elfcore_psinfo32_t; |
7547 | #endif | |
252b5132 RH |
7548 | #endif |
7549 | ||
252b5132 RH |
7550 | /* return a malloc'ed copy of a string at START which is at |
7551 | most MAX bytes long, possibly without a terminating '\0'. | |
c044fabd | 7552 | the copy will always have a terminating '\0'. */ |
252b5132 | 7553 | |
936e320b | 7554 | char * |
217aa764 | 7555 | _bfd_elfcore_strndup (bfd *abfd, char *start, size_t max) |
252b5132 | 7556 | { |
dc810e39 | 7557 | char *dups; |
c044fabd | 7558 | char *end = memchr (start, '\0', max); |
dc810e39 | 7559 | size_t len; |
252b5132 RH |
7560 | |
7561 | if (end == NULL) | |
7562 | len = max; | |
7563 | else | |
7564 | len = end - start; | |
7565 | ||
217aa764 | 7566 | dups = bfd_alloc (abfd, len + 1); |
dc810e39 | 7567 | if (dups == NULL) |
252b5132 RH |
7568 | return NULL; |
7569 | ||
dc810e39 AM |
7570 | memcpy (dups, start, len); |
7571 | dups[len] = '\0'; | |
252b5132 | 7572 | |
dc810e39 | 7573 | return dups; |
252b5132 RH |
7574 | } |
7575 | ||
bb0082d6 | 7576 | #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) |
b34976b6 | 7577 | static bfd_boolean |
217aa764 | 7578 | elfcore_grok_psinfo (bfd *abfd, Elf_Internal_Note *note) |
252b5132 | 7579 | { |
4a938328 MS |
7580 | if (note->descsz == sizeof (elfcore_psinfo_t)) |
7581 | { | |
7582 | elfcore_psinfo_t psinfo; | |
252b5132 | 7583 | |
7ee38065 | 7584 | memcpy (&psinfo, note->descdata, sizeof (psinfo)); |
252b5132 | 7585 | |
4a938328 | 7586 | elf_tdata (abfd)->core_program |
936e320b AM |
7587 | = _bfd_elfcore_strndup (abfd, psinfo.pr_fname, |
7588 | sizeof (psinfo.pr_fname)); | |
252b5132 | 7589 | |
4a938328 | 7590 | elf_tdata (abfd)->core_command |
936e320b AM |
7591 | = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs, |
7592 | sizeof (psinfo.pr_psargs)); | |
4a938328 | 7593 | } |
7ee38065 | 7594 | #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T) |
4a938328 MS |
7595 | else if (note->descsz == sizeof (elfcore_psinfo32_t)) |
7596 | { | |
7597 | /* 64-bit host, 32-bit corefile */ | |
7598 | elfcore_psinfo32_t psinfo; | |
7599 | ||
7ee38065 | 7600 | memcpy (&psinfo, note->descdata, sizeof (psinfo)); |
252b5132 | 7601 | |
4a938328 | 7602 | elf_tdata (abfd)->core_program |
936e320b AM |
7603 | = _bfd_elfcore_strndup (abfd, psinfo.pr_fname, |
7604 | sizeof (psinfo.pr_fname)); | |
4a938328 MS |
7605 | |
7606 | elf_tdata (abfd)->core_command | |
936e320b AM |
7607 | = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs, |
7608 | sizeof (psinfo.pr_psargs)); | |
4a938328 MS |
7609 | } |
7610 | #endif | |
7611 | ||
7612 | else | |
7613 | { | |
7614 | /* Fail - we don't know how to handle any other | |
7615 | note size (ie. data object type). */ | |
b34976b6 | 7616 | return TRUE; |
4a938328 | 7617 | } |
252b5132 RH |
7618 | |
7619 | /* Note that for some reason, a spurious space is tacked | |
7620 | onto the end of the args in some (at least one anyway) | |
c044fabd | 7621 | implementations, so strip it off if it exists. */ |
252b5132 RH |
7622 | |
7623 | { | |
c044fabd | 7624 | char *command = elf_tdata (abfd)->core_command; |
252b5132 RH |
7625 | int n = strlen (command); |
7626 | ||
7627 | if (0 < n && command[n - 1] == ' ') | |
7628 | command[n - 1] = '\0'; | |
7629 | } | |
7630 | ||
b34976b6 | 7631 | return TRUE; |
252b5132 RH |
7632 | } |
7633 | #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */ | |
7634 | ||
252b5132 | 7635 | #if defined (HAVE_PSTATUS_T) |
b34976b6 | 7636 | static bfd_boolean |
217aa764 | 7637 | elfcore_grok_pstatus (bfd *abfd, Elf_Internal_Note *note) |
252b5132 | 7638 | { |
f572a39d AM |
7639 | if (note->descsz == sizeof (pstatus_t) |
7640 | #if defined (HAVE_PXSTATUS_T) | |
7641 | || note->descsz == sizeof (pxstatus_t) | |
7642 | #endif | |
7643 | ) | |
4a938328 MS |
7644 | { |
7645 | pstatus_t pstat; | |
252b5132 | 7646 | |
4a938328 | 7647 | memcpy (&pstat, note->descdata, sizeof (pstat)); |
252b5132 | 7648 | |
4a938328 MS |
7649 | elf_tdata (abfd)->core_pid = pstat.pr_pid; |
7650 | } | |
7ee38065 | 7651 | #if defined (HAVE_PSTATUS32_T) |
4a938328 MS |
7652 | else if (note->descsz == sizeof (pstatus32_t)) |
7653 | { | |
7654 | /* 64-bit host, 32-bit corefile */ | |
7655 | pstatus32_t pstat; | |
252b5132 | 7656 | |
4a938328 | 7657 | memcpy (&pstat, note->descdata, sizeof (pstat)); |
252b5132 | 7658 | |
4a938328 MS |
7659 | elf_tdata (abfd)->core_pid = pstat.pr_pid; |
7660 | } | |
7661 | #endif | |
252b5132 RH |
7662 | /* Could grab some more details from the "representative" |
7663 | lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an | |
c044fabd | 7664 | NT_LWPSTATUS note, presumably. */ |
252b5132 | 7665 | |
b34976b6 | 7666 | return TRUE; |
252b5132 RH |
7667 | } |
7668 | #endif /* defined (HAVE_PSTATUS_T) */ | |
7669 | ||
252b5132 | 7670 | #if defined (HAVE_LWPSTATUS_T) |
b34976b6 | 7671 | static bfd_boolean |
217aa764 | 7672 | elfcore_grok_lwpstatus (bfd *abfd, Elf_Internal_Note *note) |
252b5132 RH |
7673 | { |
7674 | lwpstatus_t lwpstat; | |
7675 | char buf[100]; | |
c044fabd | 7676 | char *name; |
d4c88bbb | 7677 | size_t len; |
c044fabd | 7678 | asection *sect; |
252b5132 | 7679 | |
f572a39d AM |
7680 | if (note->descsz != sizeof (lwpstat) |
7681 | #if defined (HAVE_LWPXSTATUS_T) | |
7682 | && note->descsz != sizeof (lwpxstatus_t) | |
7683 | #endif | |
7684 | ) | |
b34976b6 | 7685 | return TRUE; |
252b5132 RH |
7686 | |
7687 | memcpy (&lwpstat, note->descdata, sizeof (lwpstat)); | |
7688 | ||
7689 | elf_tdata (abfd)->core_lwpid = lwpstat.pr_lwpid; | |
7690 | elf_tdata (abfd)->core_signal = lwpstat.pr_cursig; | |
7691 | ||
c044fabd | 7692 | /* Make a ".reg/999" section. */ |
252b5132 RH |
7693 | |
7694 | sprintf (buf, ".reg/%d", elfcore_make_pid (abfd)); | |
d4c88bbb | 7695 | len = strlen (buf) + 1; |
217aa764 | 7696 | name = bfd_alloc (abfd, len); |
252b5132 | 7697 | if (name == NULL) |
b34976b6 | 7698 | return FALSE; |
d4c88bbb | 7699 | memcpy (name, buf, len); |
252b5132 | 7700 | |
117ed4f8 | 7701 | sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); |
252b5132 | 7702 | if (sect == NULL) |
b34976b6 | 7703 | return FALSE; |
252b5132 RH |
7704 | |
7705 | #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT) | |
eea6121a | 7706 | sect->size = sizeof (lwpstat.pr_context.uc_mcontext.gregs); |
252b5132 RH |
7707 | sect->filepos = note->descpos |
7708 | + offsetof (lwpstatus_t, pr_context.uc_mcontext.gregs); | |
7709 | #endif | |
7710 | ||
7711 | #if defined (HAVE_LWPSTATUS_T_PR_REG) | |
eea6121a | 7712 | sect->size = sizeof (lwpstat.pr_reg); |
252b5132 RH |
7713 | sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_reg); |
7714 | #endif | |
7715 | ||
252b5132 RH |
7716 | sect->alignment_power = 2; |
7717 | ||
7718 | if (!elfcore_maybe_make_sect (abfd, ".reg", sect)) | |
b34976b6 | 7719 | return FALSE; |
252b5132 RH |
7720 | |
7721 | /* Make a ".reg2/999" section */ | |
7722 | ||
7723 | sprintf (buf, ".reg2/%d", elfcore_make_pid (abfd)); | |
d4c88bbb | 7724 | len = strlen (buf) + 1; |
217aa764 | 7725 | name = bfd_alloc (abfd, len); |
252b5132 | 7726 | if (name == NULL) |
b34976b6 | 7727 | return FALSE; |
d4c88bbb | 7728 | memcpy (name, buf, len); |
252b5132 | 7729 | |
117ed4f8 | 7730 | sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); |
252b5132 | 7731 | if (sect == NULL) |
b34976b6 | 7732 | return FALSE; |
252b5132 RH |
7733 | |
7734 | #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT) | |
eea6121a | 7735 | sect->size = sizeof (lwpstat.pr_context.uc_mcontext.fpregs); |
252b5132 RH |
7736 | sect->filepos = note->descpos |
7737 | + offsetof (lwpstatus_t, pr_context.uc_mcontext.fpregs); | |
7738 | #endif | |
7739 | ||
7740 | #if defined (HAVE_LWPSTATUS_T_PR_FPREG) | |
eea6121a | 7741 | sect->size = sizeof (lwpstat.pr_fpreg); |
252b5132 RH |
7742 | sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_fpreg); |
7743 | #endif | |
7744 | ||
252b5132 RH |
7745 | sect->alignment_power = 2; |
7746 | ||
936e320b | 7747 | return elfcore_maybe_make_sect (abfd, ".reg2", sect); |
252b5132 RH |
7748 | } |
7749 | #endif /* defined (HAVE_LWPSTATUS_T) */ | |
7750 | ||
b34976b6 | 7751 | static bfd_boolean |
217aa764 | 7752 | elfcore_grok_win32pstatus (bfd *abfd, Elf_Internal_Note *note) |
16e9c715 NC |
7753 | { |
7754 | char buf[30]; | |
c044fabd | 7755 | char *name; |
d4c88bbb | 7756 | size_t len; |
c044fabd | 7757 | asection *sect; |
4a6636fb PA |
7758 | int type; |
7759 | int is_active_thread; | |
7760 | bfd_vma base_addr; | |
16e9c715 | 7761 | |
4a6636fb | 7762 | if (note->descsz < 728) |
b34976b6 | 7763 | return TRUE; |
16e9c715 | 7764 | |
4a6636fb PA |
7765 | if (! CONST_STRNEQ (note->namedata, "win32")) |
7766 | return TRUE; | |
7767 | ||
7768 | type = bfd_get_32 (abfd, note->descdata); | |
c044fabd | 7769 | |
4a6636fb | 7770 | switch (type) |
16e9c715 | 7771 | { |
4a6636fb | 7772 | case 1 /* NOTE_INFO_PROCESS */: |
16e9c715 | 7773 | /* FIXME: need to add ->core_command. */ |
4a6636fb PA |
7774 | /* process_info.pid */ |
7775 | elf_tdata (abfd)->core_pid = bfd_get_32 (abfd, note->descdata + 8); | |
7776 | /* process_info.signal */ | |
7777 | elf_tdata (abfd)->core_signal = bfd_get_32 (abfd, note->descdata + 12); | |
c044fabd | 7778 | break; |
16e9c715 | 7779 | |
4a6636fb | 7780 | case 2 /* NOTE_INFO_THREAD */: |
16e9c715 | 7781 | /* Make a ".reg/999" section. */ |
4a6636fb PA |
7782 | /* thread_info.tid */ |
7783 | sprintf (buf, ".reg/%ld", (long) bfd_get_32 (abfd, note->descdata + 8)); | |
c044fabd | 7784 | |
d4c88bbb | 7785 | len = strlen (buf) + 1; |
217aa764 | 7786 | name = bfd_alloc (abfd, len); |
16e9c715 | 7787 | if (name == NULL) |
b34976b6 | 7788 | return FALSE; |
c044fabd | 7789 | |
d4c88bbb | 7790 | memcpy (name, buf, len); |
16e9c715 | 7791 | |
117ed4f8 | 7792 | sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); |
16e9c715 | 7793 | if (sect == NULL) |
b34976b6 | 7794 | return FALSE; |
c044fabd | 7795 | |
4a6636fb PA |
7796 | /* sizeof (thread_info.thread_context) */ |
7797 | sect->size = 716; | |
7798 | /* offsetof (thread_info.thread_context) */ | |
7799 | sect->filepos = note->descpos + 12; | |
16e9c715 NC |
7800 | sect->alignment_power = 2; |
7801 | ||
4a6636fb PA |
7802 | /* thread_info.is_active_thread */ |
7803 | is_active_thread = bfd_get_32 (abfd, note->descdata + 8); | |
7804 | ||
7805 | if (is_active_thread) | |
16e9c715 | 7806 | if (! elfcore_maybe_make_sect (abfd, ".reg", sect)) |
b34976b6 | 7807 | return FALSE; |
16e9c715 NC |
7808 | break; |
7809 | ||
4a6636fb | 7810 | case 3 /* NOTE_INFO_MODULE */: |
16e9c715 | 7811 | /* Make a ".module/xxxxxxxx" section. */ |
4a6636fb PA |
7812 | /* module_info.base_address */ |
7813 | base_addr = bfd_get_32 (abfd, note->descdata + 4); | |
7814 | sprintf (buf, ".module/%08lx", (long) base_addr); | |
c044fabd | 7815 | |
d4c88bbb | 7816 | len = strlen (buf) + 1; |
217aa764 | 7817 | name = bfd_alloc (abfd, len); |
16e9c715 | 7818 | if (name == NULL) |
b34976b6 | 7819 | return FALSE; |
c044fabd | 7820 | |
d4c88bbb | 7821 | memcpy (name, buf, len); |
252b5132 | 7822 | |
117ed4f8 | 7823 | sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); |
c044fabd | 7824 | |
16e9c715 | 7825 | if (sect == NULL) |
b34976b6 | 7826 | return FALSE; |
c044fabd | 7827 | |
eea6121a | 7828 | sect->size = note->descsz; |
16e9c715 | 7829 | sect->filepos = note->descpos; |
16e9c715 NC |
7830 | sect->alignment_power = 2; |
7831 | break; | |
7832 | ||
7833 | default: | |
b34976b6 | 7834 | return TRUE; |
16e9c715 NC |
7835 | } |
7836 | ||
b34976b6 | 7837 | return TRUE; |
16e9c715 | 7838 | } |
252b5132 | 7839 | |
b34976b6 | 7840 | static bfd_boolean |
217aa764 | 7841 | elfcore_grok_note (bfd *abfd, Elf_Internal_Note *note) |
252b5132 | 7842 | { |
9c5bfbb7 | 7843 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
bb0082d6 | 7844 | |
252b5132 RH |
7845 | switch (note->type) |
7846 | { | |
7847 | default: | |
b34976b6 | 7848 | return TRUE; |
252b5132 | 7849 | |
252b5132 | 7850 | case NT_PRSTATUS: |
bb0082d6 AM |
7851 | if (bed->elf_backend_grok_prstatus) |
7852 | if ((*bed->elf_backend_grok_prstatus) (abfd, note)) | |
b34976b6 | 7853 | return TRUE; |
bb0082d6 | 7854 | #if defined (HAVE_PRSTATUS_T) |
252b5132 | 7855 | return elfcore_grok_prstatus (abfd, note); |
bb0082d6 | 7856 | #else |
b34976b6 | 7857 | return TRUE; |
252b5132 RH |
7858 | #endif |
7859 | ||
7860 | #if defined (HAVE_PSTATUS_T) | |
7861 | case NT_PSTATUS: | |
7862 | return elfcore_grok_pstatus (abfd, note); | |
7863 | #endif | |
7864 | ||
7865 | #if defined (HAVE_LWPSTATUS_T) | |
7866 | case NT_LWPSTATUS: | |
7867 | return elfcore_grok_lwpstatus (abfd, note); | |
7868 | #endif | |
7869 | ||
7870 | case NT_FPREGSET: /* FIXME: rename to NT_PRFPREG */ | |
7871 | return elfcore_grok_prfpreg (abfd, note); | |
7872 | ||
c044fabd | 7873 | case NT_WIN32PSTATUS: |
16e9c715 | 7874 | return elfcore_grok_win32pstatus (abfd, note); |
16e9c715 | 7875 | |
c044fabd | 7876 | case NT_PRXFPREG: /* Linux SSE extension */ |
e377ab71 MK |
7877 | if (note->namesz == 6 |
7878 | && strcmp (note->namedata, "LINUX") == 0) | |
ff08c6bb JB |
7879 | return elfcore_grok_prxfpreg (abfd, note); |
7880 | else | |
b34976b6 | 7881 | return TRUE; |
ff08c6bb | 7882 | |
97753bd5 AM |
7883 | case NT_PPC_VMX: |
7884 | if (note->namesz == 6 | |
7885 | && strcmp (note->namedata, "LINUX") == 0) | |
7886 | return elfcore_grok_ppc_vmx (abfd, note); | |
7887 | else | |
7888 | return TRUE; | |
7889 | ||
252b5132 RH |
7890 | case NT_PRPSINFO: |
7891 | case NT_PSINFO: | |
bb0082d6 AM |
7892 | if (bed->elf_backend_grok_psinfo) |
7893 | if ((*bed->elf_backend_grok_psinfo) (abfd, note)) | |
b34976b6 | 7894 | return TRUE; |
bb0082d6 | 7895 | #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) |
252b5132 | 7896 | return elfcore_grok_psinfo (abfd, note); |
bb0082d6 | 7897 | #else |
b34976b6 | 7898 | return TRUE; |
252b5132 | 7899 | #endif |
3333a7c3 RM |
7900 | |
7901 | case NT_AUXV: | |
7902 | { | |
117ed4f8 AM |
7903 | asection *sect = bfd_make_section_anyway_with_flags (abfd, ".auxv", |
7904 | SEC_HAS_CONTENTS); | |
3333a7c3 RM |
7905 | |
7906 | if (sect == NULL) | |
7907 | return FALSE; | |
eea6121a | 7908 | sect->size = note->descsz; |
3333a7c3 | 7909 | sect->filepos = note->descpos; |
3333a7c3 RM |
7910 | sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32; |
7911 | ||
7912 | return TRUE; | |
7913 | } | |
252b5132 RH |
7914 | } |
7915 | } | |
7916 | ||
718175fa JK |
7917 | static bfd_boolean |
7918 | elfobj_grok_gnu_build_id (bfd *abfd, Elf_Internal_Note *note) | |
7919 | { | |
7920 | elf_tdata (abfd)->build_id_size = note->descsz; | |
7921 | elf_tdata (abfd)->build_id = bfd_alloc (abfd, note->descsz); | |
7922 | if (elf_tdata (abfd)->build_id == NULL) | |
7923 | return FALSE; | |
7924 | ||
7925 | memcpy (elf_tdata (abfd)->build_id, note->descdata, note->descsz); | |
7926 | ||
7927 | return TRUE; | |
7928 | } | |
7929 | ||
7930 | static bfd_boolean | |
7931 | elfobj_grok_gnu_note (bfd *abfd, Elf_Internal_Note *note) | |
7932 | { | |
7933 | switch (note->type) | |
7934 | { | |
7935 | default: | |
7936 | return TRUE; | |
7937 | ||
7938 | case NT_GNU_BUILD_ID: | |
7939 | return elfobj_grok_gnu_build_id (abfd, note); | |
7940 | } | |
7941 | } | |
7942 | ||
b34976b6 | 7943 | static bfd_boolean |
217aa764 | 7944 | elfcore_netbsd_get_lwpid (Elf_Internal_Note *note, int *lwpidp) |
50b2bdb7 AM |
7945 | { |
7946 | char *cp; | |
7947 | ||
7948 | cp = strchr (note->namedata, '@'); | |
7949 | if (cp != NULL) | |
7950 | { | |
d2b64500 | 7951 | *lwpidp = atoi(cp + 1); |
b34976b6 | 7952 | return TRUE; |
50b2bdb7 | 7953 | } |
b34976b6 | 7954 | return FALSE; |
50b2bdb7 AM |
7955 | } |
7956 | ||
b34976b6 | 7957 | static bfd_boolean |
217aa764 | 7958 | elfcore_grok_netbsd_procinfo (bfd *abfd, Elf_Internal_Note *note) |
50b2bdb7 | 7959 | { |
50b2bdb7 AM |
7960 | /* Signal number at offset 0x08. */ |
7961 | elf_tdata (abfd)->core_signal | |
7962 | = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08); | |
7963 | ||
7964 | /* Process ID at offset 0x50. */ | |
7965 | elf_tdata (abfd)->core_pid | |
7966 | = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x50); | |
7967 | ||
7968 | /* Command name at 0x7c (max 32 bytes, including nul). */ | |
7969 | elf_tdata (abfd)->core_command | |
7970 | = _bfd_elfcore_strndup (abfd, note->descdata + 0x7c, 31); | |
7971 | ||
7720ba9f MK |
7972 | return elfcore_make_note_pseudosection (abfd, ".note.netbsdcore.procinfo", |
7973 | note); | |
50b2bdb7 AM |
7974 | } |
7975 | ||
b34976b6 | 7976 | static bfd_boolean |
217aa764 | 7977 | elfcore_grok_netbsd_note (bfd *abfd, Elf_Internal_Note *note) |
50b2bdb7 AM |
7978 | { |
7979 | int lwp; | |
7980 | ||
7981 | if (elfcore_netbsd_get_lwpid (note, &lwp)) | |
7982 | elf_tdata (abfd)->core_lwpid = lwp; | |
7983 | ||
b4db1224 | 7984 | if (note->type == NT_NETBSDCORE_PROCINFO) |
50b2bdb7 AM |
7985 | { |
7986 | /* NetBSD-specific core "procinfo". Note that we expect to | |
08a40648 AM |
7987 | find this note before any of the others, which is fine, |
7988 | since the kernel writes this note out first when it | |
7989 | creates a core file. */ | |
47d9a591 | 7990 | |
50b2bdb7 AM |
7991 | return elfcore_grok_netbsd_procinfo (abfd, note); |
7992 | } | |
7993 | ||
b4db1224 JT |
7994 | /* As of Jan 2002 there are no other machine-independent notes |
7995 | defined for NetBSD core files. If the note type is less | |
7996 | than the start of the machine-dependent note types, we don't | |
7997 | understand it. */ | |
47d9a591 | 7998 | |
b4db1224 | 7999 | if (note->type < NT_NETBSDCORE_FIRSTMACH) |
b34976b6 | 8000 | return TRUE; |
50b2bdb7 AM |
8001 | |
8002 | ||
8003 | switch (bfd_get_arch (abfd)) | |
8004 | { | |
08a40648 AM |
8005 | /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and |
8006 | PT_GETFPREGS == mach+2. */ | |
50b2bdb7 AM |
8007 | |
8008 | case bfd_arch_alpha: | |
8009 | case bfd_arch_sparc: | |
8010 | switch (note->type) | |
08a40648 AM |
8011 | { |
8012 | case NT_NETBSDCORE_FIRSTMACH+0: | |
8013 | return elfcore_make_note_pseudosection (abfd, ".reg", note); | |
50b2bdb7 | 8014 | |
08a40648 AM |
8015 | case NT_NETBSDCORE_FIRSTMACH+2: |
8016 | return elfcore_make_note_pseudosection (abfd, ".reg2", note); | |
50b2bdb7 | 8017 | |
08a40648 AM |
8018 | default: |
8019 | return TRUE; | |
8020 | } | |
50b2bdb7 | 8021 | |
08a40648 AM |
8022 | /* On all other arch's, PT_GETREGS == mach+1 and |
8023 | PT_GETFPREGS == mach+3. */ | |
50b2bdb7 AM |
8024 | |
8025 | default: | |
8026 | switch (note->type) | |
08a40648 AM |
8027 | { |
8028 | case NT_NETBSDCORE_FIRSTMACH+1: | |
8029 | return elfcore_make_note_pseudosection (abfd, ".reg", note); | |
50b2bdb7 | 8030 | |
08a40648 AM |
8031 | case NT_NETBSDCORE_FIRSTMACH+3: |
8032 | return elfcore_make_note_pseudosection (abfd, ".reg2", note); | |
50b2bdb7 | 8033 | |
08a40648 AM |
8034 | default: |
8035 | return TRUE; | |
8036 | } | |
50b2bdb7 AM |
8037 | } |
8038 | /* NOTREACHED */ | |
8039 | } | |
8040 | ||
07c6e936 | 8041 | static bfd_boolean |
d3fd4074 | 8042 | elfcore_grok_nto_status (bfd *abfd, Elf_Internal_Note *note, long *tid) |
07c6e936 NC |
8043 | { |
8044 | void *ddata = note->descdata; | |
8045 | char buf[100]; | |
8046 | char *name; | |
8047 | asection *sect; | |
f8843e87 AM |
8048 | short sig; |
8049 | unsigned flags; | |
07c6e936 NC |
8050 | |
8051 | /* nto_procfs_status 'pid' field is at offset 0. */ | |
8052 | elf_tdata (abfd)->core_pid = bfd_get_32 (abfd, (bfd_byte *) ddata); | |
8053 | ||
f8843e87 AM |
8054 | /* nto_procfs_status 'tid' field is at offset 4. Pass it back. */ |
8055 | *tid = bfd_get_32 (abfd, (bfd_byte *) ddata + 4); | |
8056 | ||
8057 | /* nto_procfs_status 'flags' field is at offset 8. */ | |
8058 | flags = bfd_get_32 (abfd, (bfd_byte *) ddata + 8); | |
07c6e936 NC |
8059 | |
8060 | /* nto_procfs_status 'what' field is at offset 14. */ | |
f8843e87 AM |
8061 | if ((sig = bfd_get_16 (abfd, (bfd_byte *) ddata + 14)) > 0) |
8062 | { | |
8063 | elf_tdata (abfd)->core_signal = sig; | |
8064 | elf_tdata (abfd)->core_lwpid = *tid; | |
8065 | } | |
07c6e936 | 8066 | |
f8843e87 AM |
8067 | /* _DEBUG_FLAG_CURTID (current thread) is 0x80. Some cores |
8068 | do not come from signals so we make sure we set the current | |
8069 | thread just in case. */ | |
8070 | if (flags & 0x00000080) | |
8071 | elf_tdata (abfd)->core_lwpid = *tid; | |
07c6e936 NC |
8072 | |
8073 | /* Make a ".qnx_core_status/%d" section. */ | |
d3fd4074 | 8074 | sprintf (buf, ".qnx_core_status/%ld", *tid); |
07c6e936 | 8075 | |
217aa764 | 8076 | name = bfd_alloc (abfd, strlen (buf) + 1); |
07c6e936 NC |
8077 | if (name == NULL) |
8078 | return FALSE; | |
8079 | strcpy (name, buf); | |
8080 | ||
117ed4f8 | 8081 | sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); |
07c6e936 NC |
8082 | if (sect == NULL) |
8083 | return FALSE; | |
8084 | ||
eea6121a | 8085 | sect->size = note->descsz; |
07c6e936 | 8086 | sect->filepos = note->descpos; |
07c6e936 NC |
8087 | sect->alignment_power = 2; |
8088 | ||
8089 | return (elfcore_maybe_make_sect (abfd, ".qnx_core_status", sect)); | |
8090 | } | |
8091 | ||
8092 | static bfd_boolean | |
d69f560c KW |
8093 | elfcore_grok_nto_regs (bfd *abfd, |
8094 | Elf_Internal_Note *note, | |
d3fd4074 | 8095 | long tid, |
d69f560c | 8096 | char *base) |
07c6e936 NC |
8097 | { |
8098 | char buf[100]; | |
8099 | char *name; | |
8100 | asection *sect; | |
8101 | ||
d69f560c | 8102 | /* Make a "(base)/%d" section. */ |
d3fd4074 | 8103 | sprintf (buf, "%s/%ld", base, tid); |
07c6e936 | 8104 | |
217aa764 | 8105 | name = bfd_alloc (abfd, strlen (buf) + 1); |
07c6e936 NC |
8106 | if (name == NULL) |
8107 | return FALSE; | |
8108 | strcpy (name, buf); | |
8109 | ||
117ed4f8 | 8110 | sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); |
07c6e936 NC |
8111 | if (sect == NULL) |
8112 | return FALSE; | |
8113 | ||
eea6121a | 8114 | sect->size = note->descsz; |
07c6e936 | 8115 | sect->filepos = note->descpos; |
07c6e936 NC |
8116 | sect->alignment_power = 2; |
8117 | ||
f8843e87 AM |
8118 | /* This is the current thread. */ |
8119 | if (elf_tdata (abfd)->core_lwpid == tid) | |
d69f560c | 8120 | return elfcore_maybe_make_sect (abfd, base, sect); |
f8843e87 AM |
8121 | |
8122 | return TRUE; | |
07c6e936 NC |
8123 | } |
8124 | ||
8125 | #define BFD_QNT_CORE_INFO 7 | |
8126 | #define BFD_QNT_CORE_STATUS 8 | |
8127 | #define BFD_QNT_CORE_GREG 9 | |
8128 | #define BFD_QNT_CORE_FPREG 10 | |
8129 | ||
8130 | static bfd_boolean | |
217aa764 | 8131 | elfcore_grok_nto_note (bfd *abfd, Elf_Internal_Note *note) |
07c6e936 NC |
8132 | { |
8133 | /* Every GREG section has a STATUS section before it. Store the | |
811072d8 | 8134 | tid from the previous call to pass down to the next gregs |
07c6e936 | 8135 | function. */ |
d3fd4074 | 8136 | static long tid = 1; |
07c6e936 NC |
8137 | |
8138 | switch (note->type) | |
8139 | { | |
d69f560c KW |
8140 | case BFD_QNT_CORE_INFO: |
8141 | return elfcore_make_note_pseudosection (abfd, ".qnx_core_info", note); | |
8142 | case BFD_QNT_CORE_STATUS: | |
8143 | return elfcore_grok_nto_status (abfd, note, &tid); | |
8144 | case BFD_QNT_CORE_GREG: | |
8145 | return elfcore_grok_nto_regs (abfd, note, tid, ".reg"); | |
8146 | case BFD_QNT_CORE_FPREG: | |
8147 | return elfcore_grok_nto_regs (abfd, note, tid, ".reg2"); | |
8148 | default: | |
8149 | return TRUE; | |
07c6e936 NC |
8150 | } |
8151 | } | |
8152 | ||
b15fa79e AM |
8153 | static bfd_boolean |
8154 | elfcore_grok_spu_note (bfd *abfd, Elf_Internal_Note *note) | |
8155 | { | |
8156 | char *name; | |
8157 | asection *sect; | |
8158 | size_t len; | |
8159 | ||
8160 | /* Use note name as section name. */ | |
8161 | len = note->namesz; | |
8162 | name = bfd_alloc (abfd, len); | |
8163 | if (name == NULL) | |
8164 | return FALSE; | |
8165 | memcpy (name, note->namedata, len); | |
8166 | name[len - 1] = '\0'; | |
8167 | ||
8168 | sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); | |
8169 | if (sect == NULL) | |
8170 | return FALSE; | |
8171 | ||
8172 | sect->size = note->descsz; | |
8173 | sect->filepos = note->descpos; | |
8174 | sect->alignment_power = 1; | |
8175 | ||
8176 | return TRUE; | |
8177 | } | |
8178 | ||
7c76fa91 MS |
8179 | /* Function: elfcore_write_note |
8180 | ||
47d9a591 | 8181 | Inputs: |
a39f3346 | 8182 | buffer to hold note, and current size of buffer |
7c76fa91 MS |
8183 | name of note |
8184 | type of note | |
8185 | data for note | |
8186 | size of data for note | |
8187 | ||
a39f3346 AM |
8188 | Writes note to end of buffer. ELF64 notes are written exactly as |
8189 | for ELF32, despite the current (as of 2006) ELF gabi specifying | |
8190 | that they ought to have 8-byte namesz and descsz field, and have | |
8191 | 8-byte alignment. Other writers, eg. Linux kernel, do the same. | |
8192 | ||
7c76fa91 | 8193 | Return: |
a39f3346 | 8194 | Pointer to realloc'd buffer, *BUFSIZ updated. */ |
7c76fa91 MS |
8195 | |
8196 | char * | |
a39f3346 | 8197 | elfcore_write_note (bfd *abfd, |
217aa764 | 8198 | char *buf, |
a39f3346 | 8199 | int *bufsiz, |
217aa764 | 8200 | const char *name, |
a39f3346 | 8201 | int type, |
217aa764 | 8202 | const void *input, |
a39f3346 | 8203 | int size) |
7c76fa91 MS |
8204 | { |
8205 | Elf_External_Note *xnp; | |
d4c88bbb | 8206 | size_t namesz; |
d4c88bbb | 8207 | size_t newspace; |
a39f3346 | 8208 | char *dest; |
7c76fa91 | 8209 | |
d4c88bbb | 8210 | namesz = 0; |
d4c88bbb | 8211 | if (name != NULL) |
a39f3346 | 8212 | namesz = strlen (name) + 1; |
d4c88bbb | 8213 | |
a39f3346 | 8214 | newspace = 12 + ((namesz + 3) & -4) + ((size + 3) & -4); |
d4c88bbb | 8215 | |
a39f3346 | 8216 | buf = realloc (buf, *bufsiz + newspace); |
14b1c01e AM |
8217 | if (buf == NULL) |
8218 | return buf; | |
a39f3346 | 8219 | dest = buf + *bufsiz; |
7c76fa91 MS |
8220 | *bufsiz += newspace; |
8221 | xnp = (Elf_External_Note *) dest; | |
8222 | H_PUT_32 (abfd, namesz, xnp->namesz); | |
8223 | H_PUT_32 (abfd, size, xnp->descsz); | |
8224 | H_PUT_32 (abfd, type, xnp->type); | |
d4c88bbb AM |
8225 | dest = xnp->name; |
8226 | if (name != NULL) | |
8227 | { | |
8228 | memcpy (dest, name, namesz); | |
8229 | dest += namesz; | |
a39f3346 | 8230 | while (namesz & 3) |
d4c88bbb AM |
8231 | { |
8232 | *dest++ = '\0'; | |
a39f3346 | 8233 | ++namesz; |
d4c88bbb AM |
8234 | } |
8235 | } | |
8236 | memcpy (dest, input, size); | |
a39f3346 AM |
8237 | dest += size; |
8238 | while (size & 3) | |
8239 | { | |
8240 | *dest++ = '\0'; | |
8241 | ++size; | |
8242 | } | |
8243 | return buf; | |
7c76fa91 MS |
8244 | } |
8245 | ||
8246 | #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) | |
8247 | char * | |
217aa764 AM |
8248 | elfcore_write_prpsinfo (bfd *abfd, |
8249 | char *buf, | |
8250 | int *bufsiz, | |
8251 | const char *fname, | |
8252 | const char *psargs) | |
7c76fa91 | 8253 | { |
183e98be AM |
8254 | const char *note_name = "CORE"; |
8255 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
8256 | ||
8257 | if (bed->elf_backend_write_core_note != NULL) | |
8258 | { | |
8259 | char *ret; | |
8260 | ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz, | |
8261 | NT_PRPSINFO, fname, psargs); | |
8262 | if (ret != NULL) | |
8263 | return ret; | |
8264 | } | |
7c76fa91 | 8265 | |
183e98be AM |
8266 | #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T) |
8267 | if (bed->s->elfclass == ELFCLASS32) | |
8268 | { | |
8269 | #if defined (HAVE_PSINFO32_T) | |
8270 | psinfo32_t data; | |
8271 | int note_type = NT_PSINFO; | |
8272 | #else | |
8273 | prpsinfo32_t data; | |
8274 | int note_type = NT_PRPSINFO; | |
8275 | #endif | |
8276 | ||
8277 | memset (&data, 0, sizeof (data)); | |
8278 | strncpy (data.pr_fname, fname, sizeof (data.pr_fname)); | |
8279 | strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs)); | |
8280 | return elfcore_write_note (abfd, buf, bufsiz, | |
8281 | note_name, note_type, &data, sizeof (data)); | |
8282 | } | |
8283 | else | |
8284 | #endif | |
8285 | { | |
7c76fa91 | 8286 | #if defined (HAVE_PSINFO_T) |
183e98be AM |
8287 | psinfo_t data; |
8288 | int note_type = NT_PSINFO; | |
7c76fa91 | 8289 | #else |
183e98be AM |
8290 | prpsinfo_t data; |
8291 | int note_type = NT_PRPSINFO; | |
7c76fa91 MS |
8292 | #endif |
8293 | ||
183e98be AM |
8294 | memset (&data, 0, sizeof (data)); |
8295 | strncpy (data.pr_fname, fname, sizeof (data.pr_fname)); | |
8296 | strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs)); | |
8297 | return elfcore_write_note (abfd, buf, bufsiz, | |
8298 | note_name, note_type, &data, sizeof (data)); | |
8299 | } | |
7c76fa91 MS |
8300 | } |
8301 | #endif /* PSINFO_T or PRPSINFO_T */ | |
8302 | ||
8303 | #if defined (HAVE_PRSTATUS_T) | |
8304 | char * | |
217aa764 AM |
8305 | elfcore_write_prstatus (bfd *abfd, |
8306 | char *buf, | |
8307 | int *bufsiz, | |
8308 | long pid, | |
8309 | int cursig, | |
8310 | const void *gregs) | |
7c76fa91 | 8311 | { |
183e98be AM |
8312 | const char *note_name = "CORE"; |
8313 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
7c76fa91 | 8314 | |
183e98be AM |
8315 | if (bed->elf_backend_write_core_note != NULL) |
8316 | { | |
8317 | char *ret; | |
8318 | ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz, | |
8319 | NT_PRSTATUS, | |
8320 | pid, cursig, gregs); | |
8321 | if (ret != NULL) | |
8322 | return ret; | |
8323 | } | |
8324 | ||
8325 | #if defined (HAVE_PRSTATUS32_T) | |
8326 | if (bed->s->elfclass == ELFCLASS32) | |
8327 | { | |
8328 | prstatus32_t prstat; | |
8329 | ||
8330 | memset (&prstat, 0, sizeof (prstat)); | |
8331 | prstat.pr_pid = pid; | |
8332 | prstat.pr_cursig = cursig; | |
8333 | memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg)); | |
8334 | return elfcore_write_note (abfd, buf, bufsiz, note_name, | |
8335 | NT_PRSTATUS, &prstat, sizeof (prstat)); | |
8336 | } | |
8337 | else | |
8338 | #endif | |
8339 | { | |
8340 | prstatus_t prstat; | |
8341 | ||
8342 | memset (&prstat, 0, sizeof (prstat)); | |
8343 | prstat.pr_pid = pid; | |
8344 | prstat.pr_cursig = cursig; | |
8345 | memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg)); | |
8346 | return elfcore_write_note (abfd, buf, bufsiz, note_name, | |
8347 | NT_PRSTATUS, &prstat, sizeof (prstat)); | |
8348 | } | |
7c76fa91 MS |
8349 | } |
8350 | #endif /* HAVE_PRSTATUS_T */ | |
8351 | ||
51316059 MS |
8352 | #if defined (HAVE_LWPSTATUS_T) |
8353 | char * | |
217aa764 AM |
8354 | elfcore_write_lwpstatus (bfd *abfd, |
8355 | char *buf, | |
8356 | int *bufsiz, | |
8357 | long pid, | |
8358 | int cursig, | |
8359 | const void *gregs) | |
51316059 MS |
8360 | { |
8361 | lwpstatus_t lwpstat; | |
183e98be | 8362 | const char *note_name = "CORE"; |
51316059 MS |
8363 | |
8364 | memset (&lwpstat, 0, sizeof (lwpstat)); | |
8365 | lwpstat.pr_lwpid = pid >> 16; | |
8366 | lwpstat.pr_cursig = cursig; | |
8367 | #if defined (HAVE_LWPSTATUS_T_PR_REG) | |
8368 | memcpy (lwpstat.pr_reg, gregs, sizeof (lwpstat.pr_reg)); | |
8369 | #elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT) | |
8370 | #if !defined(gregs) | |
8371 | memcpy (lwpstat.pr_context.uc_mcontext.gregs, | |
8372 | gregs, sizeof (lwpstat.pr_context.uc_mcontext.gregs)); | |
8373 | #else | |
8374 | memcpy (lwpstat.pr_context.uc_mcontext.__gregs, | |
8375 | gregs, sizeof (lwpstat.pr_context.uc_mcontext.__gregs)); | |
8376 | #endif | |
8377 | #endif | |
47d9a591 | 8378 | return elfcore_write_note (abfd, buf, bufsiz, note_name, |
51316059 MS |
8379 | NT_LWPSTATUS, &lwpstat, sizeof (lwpstat)); |
8380 | } | |
8381 | #endif /* HAVE_LWPSTATUS_T */ | |
8382 | ||
7c76fa91 MS |
8383 | #if defined (HAVE_PSTATUS_T) |
8384 | char * | |
217aa764 AM |
8385 | elfcore_write_pstatus (bfd *abfd, |
8386 | char *buf, | |
8387 | int *bufsiz, | |
8388 | long pid, | |
6c10990d NC |
8389 | int cursig ATTRIBUTE_UNUSED, |
8390 | const void *gregs ATTRIBUTE_UNUSED) | |
7c76fa91 | 8391 | { |
183e98be AM |
8392 | const char *note_name = "CORE"; |
8393 | #if defined (HAVE_PSTATUS32_T) | |
8394 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
7c76fa91 | 8395 | |
183e98be AM |
8396 | if (bed->s->elfclass == ELFCLASS32) |
8397 | { | |
8398 | pstatus32_t pstat; | |
8399 | ||
8400 | memset (&pstat, 0, sizeof (pstat)); | |
8401 | pstat.pr_pid = pid & 0xffff; | |
8402 | buf = elfcore_write_note (abfd, buf, bufsiz, note_name, | |
8403 | NT_PSTATUS, &pstat, sizeof (pstat)); | |
8404 | return buf; | |
8405 | } | |
8406 | else | |
8407 | #endif | |
8408 | { | |
8409 | pstatus_t pstat; | |
8410 | ||
8411 | memset (&pstat, 0, sizeof (pstat)); | |
8412 | pstat.pr_pid = pid & 0xffff; | |
8413 | buf = elfcore_write_note (abfd, buf, bufsiz, note_name, | |
8414 | NT_PSTATUS, &pstat, sizeof (pstat)); | |
8415 | return buf; | |
8416 | } | |
7c76fa91 MS |
8417 | } |
8418 | #endif /* HAVE_PSTATUS_T */ | |
8419 | ||
8420 | char * | |
217aa764 AM |
8421 | elfcore_write_prfpreg (bfd *abfd, |
8422 | char *buf, | |
8423 | int *bufsiz, | |
8424 | const void *fpregs, | |
8425 | int size) | |
7c76fa91 | 8426 | { |
183e98be | 8427 | const char *note_name = "CORE"; |
47d9a591 | 8428 | return elfcore_write_note (abfd, buf, bufsiz, |
7c76fa91 MS |
8429 | note_name, NT_FPREGSET, fpregs, size); |
8430 | } | |
8431 | ||
8432 | char * | |
217aa764 AM |
8433 | elfcore_write_prxfpreg (bfd *abfd, |
8434 | char *buf, | |
8435 | int *bufsiz, | |
8436 | const void *xfpregs, | |
8437 | int size) | |
7c76fa91 MS |
8438 | { |
8439 | char *note_name = "LINUX"; | |
47d9a591 | 8440 | return elfcore_write_note (abfd, buf, bufsiz, |
7c76fa91 MS |
8441 | note_name, NT_PRXFPREG, xfpregs, size); |
8442 | } | |
8443 | ||
97753bd5 AM |
8444 | char * |
8445 | elfcore_write_ppc_vmx (bfd *abfd, | |
8446 | char *buf, | |
8447 | int *bufsiz, | |
8448 | const void *ppc_vmx, | |
8449 | int size) | |
8450 | { | |
8451 | char *note_name = "LINUX"; | |
8452 | return elfcore_write_note (abfd, buf, bufsiz, | |
8453 | note_name, NT_PPC_VMX, ppc_vmx, size); | |
8454 | } | |
8455 | ||
bb864ac1 CES |
8456 | char * |
8457 | elfcore_write_register_note (bfd *abfd, | |
8458 | char *buf, | |
8459 | int *bufsiz, | |
8460 | const char *section, | |
8461 | const void *data, | |
8462 | int size) | |
8463 | { | |
8464 | if (strcmp (section, ".reg2") == 0) | |
8465 | return elfcore_write_prfpreg (abfd, buf, bufsiz, data, size); | |
8466 | if (strcmp (section, ".reg-xfp") == 0) | |
8467 | return elfcore_write_prxfpreg (abfd, buf, bufsiz, data, size); | |
8468 | if (strcmp (section, ".reg-ppc-vmx") == 0) | |
8469 | return elfcore_write_ppc_vmx (abfd, buf, bufsiz, data, size); | |
8470 | return NULL; | |
8471 | } | |
8472 | ||
b34976b6 | 8473 | static bfd_boolean |
718175fa | 8474 | elf_parse_notes (bfd *abfd, char *buf, size_t size, file_ptr offset) |
252b5132 | 8475 | { |
c044fabd | 8476 | char *p; |
252b5132 | 8477 | |
252b5132 RH |
8478 | p = buf; |
8479 | while (p < buf + size) | |
8480 | { | |
c044fabd KH |
8481 | /* FIXME: bad alignment assumption. */ |
8482 | Elf_External_Note *xnp = (Elf_External_Note *) p; | |
252b5132 RH |
8483 | Elf_Internal_Note in; |
8484 | ||
dc810e39 | 8485 | in.type = H_GET_32 (abfd, xnp->type); |
252b5132 | 8486 | |
dc810e39 | 8487 | in.namesz = H_GET_32 (abfd, xnp->namesz); |
252b5132 RH |
8488 | in.namedata = xnp->name; |
8489 | ||
dc810e39 | 8490 | in.descsz = H_GET_32 (abfd, xnp->descsz); |
252b5132 RH |
8491 | in.descdata = in.namedata + BFD_ALIGN (in.namesz, 4); |
8492 | in.descpos = offset + (in.descdata - buf); | |
8493 | ||
718175fa JK |
8494 | switch (bfd_get_format (abfd)) |
8495 | { | |
8496 | default: | |
8497 | return TRUE; | |
8498 | ||
8499 | case bfd_core: | |
8500 | if (CONST_STRNEQ (in.namedata, "NetBSD-CORE")) | |
8501 | { | |
8502 | if (! elfcore_grok_netbsd_note (abfd, &in)) | |
8503 | return FALSE; | |
8504 | } | |
8505 | else if (CONST_STRNEQ (in.namedata, "QNX")) | |
8506 | { | |
8507 | if (! elfcore_grok_nto_note (abfd, &in)) | |
8508 | return FALSE; | |
8509 | } | |
b15fa79e AM |
8510 | else if (CONST_STRNEQ (in.namedata, "SPU/")) |
8511 | { | |
8512 | if (! elfcore_grok_spu_note (abfd, &in)) | |
8513 | return FALSE; | |
8514 | } | |
718175fa JK |
8515 | else |
8516 | { | |
8517 | if (! elfcore_grok_note (abfd, &in)) | |
8518 | return FALSE; | |
8519 | } | |
8520 | break; | |
8521 | ||
8522 | case bfd_object: | |
8523 | if (in.namesz == sizeof "GNU" && strcmp (in.namedata, "GNU") == 0) | |
8524 | { | |
8525 | if (! elfobj_grok_gnu_note (abfd, &in)) | |
8526 | return FALSE; | |
8527 | } | |
8528 | break; | |
08a40648 | 8529 | } |
252b5132 RH |
8530 | |
8531 | p = in.descdata + BFD_ALIGN (in.descsz, 4); | |
8532 | } | |
8533 | ||
718175fa JK |
8534 | return TRUE; |
8535 | } | |
8536 | ||
8537 | static bfd_boolean | |
8538 | elf_read_notes (bfd *abfd, file_ptr offset, bfd_size_type size) | |
8539 | { | |
8540 | char *buf; | |
8541 | ||
8542 | if (size <= 0) | |
8543 | return TRUE; | |
8544 | ||
8545 | if (bfd_seek (abfd, offset, SEEK_SET) != 0) | |
8546 | return FALSE; | |
8547 | ||
8548 | buf = bfd_malloc (size); | |
8549 | if (buf == NULL) | |
8550 | return FALSE; | |
8551 | ||
8552 | if (bfd_bread (buf, size, abfd) != size | |
8553 | || !elf_parse_notes (abfd, buf, size, offset)) | |
8554 | { | |
8555 | free (buf); | |
8556 | return FALSE; | |
8557 | } | |
8558 | ||
252b5132 | 8559 | free (buf); |
b34976b6 | 8560 | return TRUE; |
252b5132 | 8561 | } |
98d8431c JB |
8562 | \f |
8563 | /* Providing external access to the ELF program header table. */ | |
8564 | ||
8565 | /* Return an upper bound on the number of bytes required to store a | |
8566 | copy of ABFD's program header table entries. Return -1 if an error | |
8567 | occurs; bfd_get_error will return an appropriate code. */ | |
c044fabd | 8568 | |
98d8431c | 8569 | long |
217aa764 | 8570 | bfd_get_elf_phdr_upper_bound (bfd *abfd) |
98d8431c JB |
8571 | { |
8572 | if (abfd->xvec->flavour != bfd_target_elf_flavour) | |
8573 | { | |
8574 | bfd_set_error (bfd_error_wrong_format); | |
8575 | return -1; | |
8576 | } | |
8577 | ||
936e320b | 8578 | return elf_elfheader (abfd)->e_phnum * sizeof (Elf_Internal_Phdr); |
98d8431c JB |
8579 | } |
8580 | ||
98d8431c JB |
8581 | /* Copy ABFD's program header table entries to *PHDRS. The entries |
8582 | will be stored as an array of Elf_Internal_Phdr structures, as | |
8583 | defined in include/elf/internal.h. To find out how large the | |
8584 | buffer needs to be, call bfd_get_elf_phdr_upper_bound. | |
8585 | ||
8586 | Return the number of program header table entries read, or -1 if an | |
8587 | error occurs; bfd_get_error will return an appropriate code. */ | |
c044fabd | 8588 | |
98d8431c | 8589 | int |
217aa764 | 8590 | bfd_get_elf_phdrs (bfd *abfd, void *phdrs) |
98d8431c JB |
8591 | { |
8592 | int num_phdrs; | |
8593 | ||
8594 | if (abfd->xvec->flavour != bfd_target_elf_flavour) | |
8595 | { | |
8596 | bfd_set_error (bfd_error_wrong_format); | |
8597 | return -1; | |
8598 | } | |
8599 | ||
8600 | num_phdrs = elf_elfheader (abfd)->e_phnum; | |
c044fabd | 8601 | memcpy (phdrs, elf_tdata (abfd)->phdr, |
98d8431c JB |
8602 | num_phdrs * sizeof (Elf_Internal_Phdr)); |
8603 | ||
8604 | return num_phdrs; | |
8605 | } | |
ae4221d7 | 8606 | |
db6751f2 | 8607 | enum elf_reloc_type_class |
217aa764 | 8608 | _bfd_elf_reloc_type_class (const Elf_Internal_Rela *rela ATTRIBUTE_UNUSED) |
db6751f2 JJ |
8609 | { |
8610 | return reloc_class_normal; | |
8611 | } | |
f8df10f4 | 8612 | |
47d9a591 | 8613 | /* For RELA architectures, return the relocation value for a |
f8df10f4 JJ |
8614 | relocation against a local symbol. */ |
8615 | ||
8616 | bfd_vma | |
217aa764 AM |
8617 | _bfd_elf_rela_local_sym (bfd *abfd, |
8618 | Elf_Internal_Sym *sym, | |
8517fae7 | 8619 | asection **psec, |
217aa764 | 8620 | Elf_Internal_Rela *rel) |
f8df10f4 | 8621 | { |
8517fae7 | 8622 | asection *sec = *psec; |
f8df10f4 JJ |
8623 | bfd_vma relocation; |
8624 | ||
8625 | relocation = (sec->output_section->vma | |
8626 | + sec->output_offset | |
8627 | + sym->st_value); | |
8628 | if ((sec->flags & SEC_MERGE) | |
c629eae0 | 8629 | && ELF_ST_TYPE (sym->st_info) == STT_SECTION |
68bfbfcc | 8630 | && sec->sec_info_type == ELF_INFO_TYPE_MERGE) |
f8df10f4 | 8631 | { |
f8df10f4 | 8632 | rel->r_addend = |
8517fae7 | 8633 | _bfd_merged_section_offset (abfd, psec, |
65765700 | 8634 | elf_section_data (sec)->sec_info, |
753731ee AM |
8635 | sym->st_value + rel->r_addend); |
8636 | if (sec != *psec) | |
8637 | { | |
8638 | /* If we have changed the section, and our original section is | |
8639 | marked with SEC_EXCLUDE, it means that the original | |
8640 | SEC_MERGE section has been completely subsumed in some | |
8641 | other SEC_MERGE section. In this case, we need to leave | |
8642 | some info around for --emit-relocs. */ | |
8643 | if ((sec->flags & SEC_EXCLUDE) != 0) | |
8644 | sec->kept_section = *psec; | |
8645 | sec = *psec; | |
8646 | } | |
8517fae7 AM |
8647 | rel->r_addend -= relocation; |
8648 | rel->r_addend += sec->output_section->vma + sec->output_offset; | |
f8df10f4 JJ |
8649 | } |
8650 | return relocation; | |
8651 | } | |
c629eae0 JJ |
8652 | |
8653 | bfd_vma | |
217aa764 AM |
8654 | _bfd_elf_rel_local_sym (bfd *abfd, |
8655 | Elf_Internal_Sym *sym, | |
8656 | asection **psec, | |
8657 | bfd_vma addend) | |
47d9a591 | 8658 | { |
c629eae0 JJ |
8659 | asection *sec = *psec; |
8660 | ||
68bfbfcc | 8661 | if (sec->sec_info_type != ELF_INFO_TYPE_MERGE) |
c629eae0 JJ |
8662 | return sym->st_value + addend; |
8663 | ||
8664 | return _bfd_merged_section_offset (abfd, psec, | |
65765700 | 8665 | elf_section_data (sec)->sec_info, |
753731ee | 8666 | sym->st_value + addend); |
c629eae0 JJ |
8667 | } |
8668 | ||
8669 | bfd_vma | |
217aa764 | 8670 | _bfd_elf_section_offset (bfd *abfd, |
92e4ec35 | 8671 | struct bfd_link_info *info, |
217aa764 AM |
8672 | asection *sec, |
8673 | bfd_vma offset) | |
c629eae0 | 8674 | { |
68bfbfcc | 8675 | switch (sec->sec_info_type) |
65765700 JJ |
8676 | { |
8677 | case ELF_INFO_TYPE_STABS: | |
eea6121a AM |
8678 | return _bfd_stab_section_offset (sec, elf_section_data (sec)->sec_info, |
8679 | offset); | |
65765700 | 8680 | case ELF_INFO_TYPE_EH_FRAME: |
92e4ec35 | 8681 | return _bfd_elf_eh_frame_section_offset (abfd, info, sec, offset); |
65765700 JJ |
8682 | default: |
8683 | return offset; | |
8684 | } | |
c629eae0 | 8685 | } |
3333a7c3 RM |
8686 | \f |
8687 | /* Create a new BFD as if by bfd_openr. Rather than opening a file, | |
8688 | reconstruct an ELF file by reading the segments out of remote memory | |
8689 | based on the ELF file header at EHDR_VMA and the ELF program headers it | |
8690 | points to. If not null, *LOADBASEP is filled in with the difference | |
8691 | between the VMAs from which the segments were read, and the VMAs the | |
8692 | file headers (and hence BFD's idea of each section's VMA) put them at. | |
8693 | ||
8694 | The function TARGET_READ_MEMORY is called to copy LEN bytes from the | |
8695 | remote memory at target address VMA into the local buffer at MYADDR; it | |
8696 | should return zero on success or an `errno' code on failure. TEMPL must | |
8697 | be a BFD for an ELF target with the word size and byte order found in | |
8698 | the remote memory. */ | |
8699 | ||
8700 | bfd * | |
217aa764 AM |
8701 | bfd_elf_bfd_from_remote_memory |
8702 | (bfd *templ, | |
8703 | bfd_vma ehdr_vma, | |
8704 | bfd_vma *loadbasep, | |
f075ee0c | 8705 | int (*target_read_memory) (bfd_vma, bfd_byte *, int)) |
3333a7c3 RM |
8706 | { |
8707 | return (*get_elf_backend_data (templ)->elf_backend_bfd_from_remote_memory) | |
8708 | (templ, ehdr_vma, loadbasep, target_read_memory); | |
8709 | } | |
4c45e5c9 JJ |
8710 | \f |
8711 | long | |
c9727e01 AM |
8712 | _bfd_elf_get_synthetic_symtab (bfd *abfd, |
8713 | long symcount ATTRIBUTE_UNUSED, | |
8714 | asymbol **syms ATTRIBUTE_UNUSED, | |
8615f3f2 | 8715 | long dynsymcount, |
c9727e01 AM |
8716 | asymbol **dynsyms, |
8717 | asymbol **ret) | |
4c45e5c9 JJ |
8718 | { |
8719 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
8720 | asection *relplt; | |
8721 | asymbol *s; | |
8722 | const char *relplt_name; | |
8723 | bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean); | |
8724 | arelent *p; | |
8725 | long count, i, n; | |
8726 | size_t size; | |
8727 | Elf_Internal_Shdr *hdr; | |
8728 | char *names; | |
8729 | asection *plt; | |
8730 | ||
8615f3f2 AM |
8731 | *ret = NULL; |
8732 | ||
90e3cdf2 JJ |
8733 | if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0) |
8734 | return 0; | |
8735 | ||
8615f3f2 AM |
8736 | if (dynsymcount <= 0) |
8737 | return 0; | |
8738 | ||
4c45e5c9 JJ |
8739 | if (!bed->plt_sym_val) |
8740 | return 0; | |
8741 | ||
8742 | relplt_name = bed->relplt_name; | |
8743 | if (relplt_name == NULL) | |
8744 | relplt_name = bed->default_use_rela_p ? ".rela.plt" : ".rel.plt"; | |
8745 | relplt = bfd_get_section_by_name (abfd, relplt_name); | |
8746 | if (relplt == NULL) | |
8747 | return 0; | |
8748 | ||
8749 | hdr = &elf_section_data (relplt)->this_hdr; | |
8750 | if (hdr->sh_link != elf_dynsymtab (abfd) | |
8751 | || (hdr->sh_type != SHT_REL && hdr->sh_type != SHT_RELA)) | |
8752 | return 0; | |
8753 | ||
8754 | plt = bfd_get_section_by_name (abfd, ".plt"); | |
8755 | if (plt == NULL) | |
8756 | return 0; | |
8757 | ||
8758 | slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table; | |
c9727e01 | 8759 | if (! (*slurp_relocs) (abfd, relplt, dynsyms, TRUE)) |
4c45e5c9 JJ |
8760 | return -1; |
8761 | ||
eea6121a | 8762 | count = relplt->size / hdr->sh_entsize; |
4c45e5c9 JJ |
8763 | size = count * sizeof (asymbol); |
8764 | p = relplt->relocation; | |
b7fd5ce1 | 8765 | for (i = 0; i < count; i++, p++) |
4c45e5c9 JJ |
8766 | size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt"); |
8767 | ||
8768 | s = *ret = bfd_malloc (size); | |
8769 | if (s == NULL) | |
8770 | return -1; | |
8771 | ||
8772 | names = (char *) (s + count); | |
8773 | p = relplt->relocation; | |
8774 | n = 0; | |
8f39ba8e | 8775 | for (i = 0; i < count; i++, p++) |
4c45e5c9 JJ |
8776 | { |
8777 | size_t len; | |
8778 | bfd_vma addr; | |
8779 | ||
8780 | addr = bed->plt_sym_val (i, plt, p); | |
8781 | if (addr == (bfd_vma) -1) | |
8782 | continue; | |
8783 | ||
8784 | *s = **p->sym_ptr_ptr; | |
65a7a66f AM |
8785 | /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since |
8786 | we are defining a symbol, ensure one of them is set. */ | |
8787 | if ((s->flags & BSF_LOCAL) == 0) | |
8788 | s->flags |= BSF_GLOBAL; | |
4c45e5c9 JJ |
8789 | s->section = plt; |
8790 | s->value = addr - plt->vma; | |
8791 | s->name = names; | |
8f39ba8e | 8792 | s->udata.p = NULL; |
4c45e5c9 JJ |
8793 | len = strlen ((*p->sym_ptr_ptr)->name); |
8794 | memcpy (names, (*p->sym_ptr_ptr)->name, len); | |
8795 | names += len; | |
8796 | memcpy (names, "@plt", sizeof ("@plt")); | |
8797 | names += sizeof ("@plt"); | |
8f39ba8e | 8798 | ++s, ++n; |
4c45e5c9 JJ |
8799 | } |
8800 | ||
8801 | return n; | |
8802 | } | |
3d7f7666 | 8803 | |
3b22753a L |
8804 | /* It is only used by x86-64 so far. */ |
8805 | asection _bfd_elf_large_com_section | |
8806 | = BFD_FAKE_SECTION (_bfd_elf_large_com_section, | |
f592407e | 8807 | SEC_IS_COMMON, NULL, "LARGE_COMMON", 0); |
ecca9871 | 8808 | |
d1036acb L |
8809 | void |
8810 | _bfd_elf_set_osabi (bfd * abfd, | |
8811 | struct bfd_link_info * link_info ATTRIBUTE_UNUSED) | |
8812 | { | |
8813 | Elf_Internal_Ehdr * i_ehdrp; /* ELF file header, internal form. */ | |
8814 | ||
8815 | i_ehdrp = elf_elfheader (abfd); | |
8816 | ||
8817 | i_ehdrp->e_ident[EI_OSABI] = get_elf_backend_data (abfd)->elf_osabi; | |
8818 | } | |
fcb93ecf PB |
8819 | |
8820 | ||
8821 | /* Return TRUE for ELF symbol types that represent functions. | |
8822 | This is the default version of this function, which is sufficient for | |
8823 | most targets. It returns true if TYPE is STT_FUNC. */ | |
8824 | ||
8825 | bfd_boolean | |
8826 | _bfd_elf_is_function_type (unsigned int type) | |
8827 | { | |
8828 | return (type == STT_FUNC); | |
8829 | } |