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