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