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