Commit | Line | Data |
---|---|---|
252b5132 | 1 | /* ELF executable support for BFD. |
340b6d91 AC |
2 | |
3 | Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, | |
652451f8 YZ |
4 | 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, |
5 | 2013 | |
72a80a16 | 6 | Free Software Foundation, Inc. |
252b5132 | 7 | |
5e8d7549 | 8 | This file is part of BFD, the Binary File Descriptor library. |
252b5132 | 9 | |
5e8d7549 NC |
10 | This program is free software; you can redistribute it and/or modify |
11 | it under the terms of the GNU General Public License as published by | |
cd123cb7 | 12 | the Free Software Foundation; either version 3 of the License, or |
5e8d7549 | 13 | (at your option) any later version. |
252b5132 | 14 | |
5e8d7549 NC |
15 | This program is distributed in the hope that it will be useful, |
16 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
18 | GNU General Public License for more details. | |
252b5132 | 19 | |
5e8d7549 | 20 | You should have received a copy of the GNU General Public License |
b34976b6 | 21 | along with this program; if not, write to the Free Software |
cd123cb7 NC |
22 | Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, |
23 | MA 02110-1301, USA. */ | |
24 | ||
252b5132 | 25 | |
1b74d094 BW |
26 | /* |
27 | SECTION | |
252b5132 RH |
28 | ELF backends |
29 | ||
30 | BFD support for ELF formats is being worked on. | |
31 | Currently, the best supported back ends are for sparc and i386 | |
32 | (running svr4 or Solaris 2). | |
33 | ||
34 | Documentation of the internals of the support code still needs | |
35 | to be written. The code is changing quickly enough that we | |
661a3fd4 | 36 | haven't bothered yet. */ |
252b5132 | 37 | |
7ee38065 MS |
38 | /* For sparc64-cross-sparc32. */ |
39 | #define _SYSCALL32 | |
252b5132 | 40 | #include "sysdep.h" |
3db64b00 | 41 | #include "bfd.h" |
252b5132 RH |
42 | #include "bfdlink.h" |
43 | #include "libbfd.h" | |
44 | #define ARCH_SIZE 0 | |
45 | #include "elf-bfd.h" | |
e0e8c97f | 46 | #include "libiberty.h" |
ff59fc36 | 47 | #include "safe-ctype.h" |
70a38d42 | 48 | #include "elf-linux-psinfo.h" |
252b5132 | 49 | |
8bc7f138 L |
50 | #ifdef CORE_HEADER |
51 | #include CORE_HEADER | |
52 | #endif | |
53 | ||
217aa764 | 54 | static int elf_sort_sections (const void *, const void *); |
c84fca4d | 55 | static bfd_boolean assign_file_positions_except_relocs (bfd *, struct bfd_link_info *); |
217aa764 AM |
56 | static bfd_boolean prep_headers (bfd *); |
57 | static bfd_boolean swap_out_syms (bfd *, struct bfd_strtab_hash **, int) ; | |
718175fa JK |
58 | static bfd_boolean elf_read_notes (bfd *, file_ptr, bfd_size_type) ; |
59 | static bfd_boolean elf_parse_notes (bfd *abfd, char *buf, size_t size, | |
60 | file_ptr offset); | |
50b2bdb7 | 61 | |
252b5132 RH |
62 | /* Swap version information in and out. The version information is |
63 | currently size independent. If that ever changes, this code will | |
64 | need to move into elfcode.h. */ | |
65 | ||
66 | /* Swap in a Verdef structure. */ | |
67 | ||
68 | void | |
217aa764 AM |
69 | _bfd_elf_swap_verdef_in (bfd *abfd, |
70 | const Elf_External_Verdef *src, | |
71 | Elf_Internal_Verdef *dst) | |
252b5132 | 72 | { |
dc810e39 AM |
73 | dst->vd_version = H_GET_16 (abfd, src->vd_version); |
74 | dst->vd_flags = H_GET_16 (abfd, src->vd_flags); | |
75 | dst->vd_ndx = H_GET_16 (abfd, src->vd_ndx); | |
76 | dst->vd_cnt = H_GET_16 (abfd, src->vd_cnt); | |
77 | dst->vd_hash = H_GET_32 (abfd, src->vd_hash); | |
78 | dst->vd_aux = H_GET_32 (abfd, src->vd_aux); | |
79 | dst->vd_next = H_GET_32 (abfd, src->vd_next); | |
252b5132 RH |
80 | } |
81 | ||
82 | /* Swap out a Verdef structure. */ | |
83 | ||
84 | void | |
217aa764 AM |
85 | _bfd_elf_swap_verdef_out (bfd *abfd, |
86 | const Elf_Internal_Verdef *src, | |
87 | Elf_External_Verdef *dst) | |
252b5132 | 88 | { |
dc810e39 AM |
89 | H_PUT_16 (abfd, src->vd_version, dst->vd_version); |
90 | H_PUT_16 (abfd, src->vd_flags, dst->vd_flags); | |
91 | H_PUT_16 (abfd, src->vd_ndx, dst->vd_ndx); | |
92 | H_PUT_16 (abfd, src->vd_cnt, dst->vd_cnt); | |
93 | H_PUT_32 (abfd, src->vd_hash, dst->vd_hash); | |
94 | H_PUT_32 (abfd, src->vd_aux, dst->vd_aux); | |
95 | H_PUT_32 (abfd, src->vd_next, dst->vd_next); | |
252b5132 RH |
96 | } |
97 | ||
98 | /* Swap in a Verdaux structure. */ | |
99 | ||
100 | void | |
217aa764 AM |
101 | _bfd_elf_swap_verdaux_in (bfd *abfd, |
102 | const Elf_External_Verdaux *src, | |
103 | Elf_Internal_Verdaux *dst) | |
252b5132 | 104 | { |
dc810e39 AM |
105 | dst->vda_name = H_GET_32 (abfd, src->vda_name); |
106 | dst->vda_next = H_GET_32 (abfd, src->vda_next); | |
252b5132 RH |
107 | } |
108 | ||
109 | /* Swap out a Verdaux structure. */ | |
110 | ||
111 | void | |
217aa764 AM |
112 | _bfd_elf_swap_verdaux_out (bfd *abfd, |
113 | const Elf_Internal_Verdaux *src, | |
114 | Elf_External_Verdaux *dst) | |
252b5132 | 115 | { |
dc810e39 AM |
116 | H_PUT_32 (abfd, src->vda_name, dst->vda_name); |
117 | H_PUT_32 (abfd, src->vda_next, dst->vda_next); | |
252b5132 RH |
118 | } |
119 | ||
120 | /* Swap in a Verneed structure. */ | |
121 | ||
122 | void | |
217aa764 AM |
123 | _bfd_elf_swap_verneed_in (bfd *abfd, |
124 | const Elf_External_Verneed *src, | |
125 | Elf_Internal_Verneed *dst) | |
252b5132 | 126 | { |
dc810e39 AM |
127 | dst->vn_version = H_GET_16 (abfd, src->vn_version); |
128 | dst->vn_cnt = H_GET_16 (abfd, src->vn_cnt); | |
129 | dst->vn_file = H_GET_32 (abfd, src->vn_file); | |
130 | dst->vn_aux = H_GET_32 (abfd, src->vn_aux); | |
131 | dst->vn_next = H_GET_32 (abfd, src->vn_next); | |
252b5132 RH |
132 | } |
133 | ||
134 | /* Swap out a Verneed structure. */ | |
135 | ||
136 | void | |
217aa764 AM |
137 | _bfd_elf_swap_verneed_out (bfd *abfd, |
138 | const Elf_Internal_Verneed *src, | |
139 | Elf_External_Verneed *dst) | |
252b5132 | 140 | { |
dc810e39 AM |
141 | H_PUT_16 (abfd, src->vn_version, dst->vn_version); |
142 | H_PUT_16 (abfd, src->vn_cnt, dst->vn_cnt); | |
143 | H_PUT_32 (abfd, src->vn_file, dst->vn_file); | |
144 | H_PUT_32 (abfd, src->vn_aux, dst->vn_aux); | |
145 | H_PUT_32 (abfd, src->vn_next, dst->vn_next); | |
252b5132 RH |
146 | } |
147 | ||
148 | /* Swap in a Vernaux structure. */ | |
149 | ||
150 | void | |
217aa764 AM |
151 | _bfd_elf_swap_vernaux_in (bfd *abfd, |
152 | const Elf_External_Vernaux *src, | |
153 | Elf_Internal_Vernaux *dst) | |
252b5132 | 154 | { |
dc810e39 AM |
155 | dst->vna_hash = H_GET_32 (abfd, src->vna_hash); |
156 | dst->vna_flags = H_GET_16 (abfd, src->vna_flags); | |
157 | dst->vna_other = H_GET_16 (abfd, src->vna_other); | |
158 | dst->vna_name = H_GET_32 (abfd, src->vna_name); | |
159 | dst->vna_next = H_GET_32 (abfd, src->vna_next); | |
252b5132 RH |
160 | } |
161 | ||
162 | /* Swap out a Vernaux structure. */ | |
163 | ||
164 | void | |
217aa764 AM |
165 | _bfd_elf_swap_vernaux_out (bfd *abfd, |
166 | const Elf_Internal_Vernaux *src, | |
167 | Elf_External_Vernaux *dst) | |
252b5132 | 168 | { |
dc810e39 AM |
169 | H_PUT_32 (abfd, src->vna_hash, dst->vna_hash); |
170 | H_PUT_16 (abfd, src->vna_flags, dst->vna_flags); | |
171 | H_PUT_16 (abfd, src->vna_other, dst->vna_other); | |
172 | H_PUT_32 (abfd, src->vna_name, dst->vna_name); | |
173 | H_PUT_32 (abfd, src->vna_next, dst->vna_next); | |
252b5132 RH |
174 | } |
175 | ||
176 | /* Swap in a Versym structure. */ | |
177 | ||
178 | void | |
217aa764 AM |
179 | _bfd_elf_swap_versym_in (bfd *abfd, |
180 | const Elf_External_Versym *src, | |
181 | Elf_Internal_Versym *dst) | |
252b5132 | 182 | { |
dc810e39 | 183 | dst->vs_vers = H_GET_16 (abfd, src->vs_vers); |
252b5132 RH |
184 | } |
185 | ||
186 | /* Swap out a Versym structure. */ | |
187 | ||
188 | void | |
217aa764 AM |
189 | _bfd_elf_swap_versym_out (bfd *abfd, |
190 | const Elf_Internal_Versym *src, | |
191 | Elf_External_Versym *dst) | |
252b5132 | 192 | { |
dc810e39 | 193 | H_PUT_16 (abfd, src->vs_vers, dst->vs_vers); |
252b5132 RH |
194 | } |
195 | ||
196 | /* Standard ELF hash function. Do not change this function; you will | |
197 | cause invalid hash tables to be generated. */ | |
3a99b017 | 198 | |
252b5132 | 199 | unsigned long |
217aa764 | 200 | bfd_elf_hash (const char *namearg) |
252b5132 | 201 | { |
3a99b017 | 202 | const unsigned char *name = (const unsigned char *) namearg; |
252b5132 RH |
203 | unsigned long h = 0; |
204 | unsigned long g; | |
205 | int ch; | |
206 | ||
207 | while ((ch = *name++) != '\0') | |
208 | { | |
209 | h = (h << 4) + ch; | |
210 | if ((g = (h & 0xf0000000)) != 0) | |
211 | { | |
212 | h ^= g >> 24; | |
213 | /* The ELF ABI says `h &= ~g', but this is equivalent in | |
214 | this case and on some machines one insn instead of two. */ | |
215 | h ^= g; | |
216 | } | |
217 | } | |
32dfa85d | 218 | return h & 0xffffffff; |
252b5132 RH |
219 | } |
220 | ||
fdc90cb4 JJ |
221 | /* DT_GNU_HASH hash function. Do not change this function; you will |
222 | cause invalid hash tables to be generated. */ | |
223 | ||
224 | unsigned long | |
225 | bfd_elf_gnu_hash (const char *namearg) | |
226 | { | |
227 | const unsigned char *name = (const unsigned char *) namearg; | |
228 | unsigned long h = 5381; | |
229 | unsigned char ch; | |
230 | ||
231 | while ((ch = *name++) != '\0') | |
232 | h = (h << 5) + h + ch; | |
233 | return h & 0xffffffff; | |
234 | } | |
235 | ||
0c8d6e5c AM |
236 | /* Create a tdata field OBJECT_SIZE bytes in length, zeroed out and with |
237 | the object_id field of an elf_obj_tdata field set to OBJECT_ID. */ | |
b34976b6 | 238 | bfd_boolean |
0c8d6e5c | 239 | bfd_elf_allocate_object (bfd *abfd, |
0ffa91dd | 240 | size_t object_size, |
4dfe6ac6 | 241 | enum elf_target_id object_id) |
252b5132 | 242 | { |
0ffa91dd NC |
243 | BFD_ASSERT (object_size >= sizeof (struct elf_obj_tdata)); |
244 | abfd->tdata.any = bfd_zalloc (abfd, object_size); | |
245 | if (abfd->tdata.any == NULL) | |
246 | return FALSE; | |
252b5132 | 247 | |
0ffa91dd NC |
248 | elf_object_id (abfd) = object_id; |
249 | elf_program_header_size (abfd) = (bfd_size_type) -1; | |
b34976b6 | 250 | return TRUE; |
252b5132 RH |
251 | } |
252 | ||
0ffa91dd NC |
253 | |
254 | bfd_boolean | |
ae95ffa6 | 255 | bfd_elf_make_object (bfd *abfd) |
0ffa91dd | 256 | { |
ae95ffa6 | 257 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
0ffa91dd | 258 | return bfd_elf_allocate_object (abfd, sizeof (struct elf_obj_tdata), |
ae95ffa6 | 259 | bed->target_id); |
0ffa91dd NC |
260 | } |
261 | ||
b34976b6 | 262 | bfd_boolean |
217aa764 | 263 | bfd_elf_mkcorefile (bfd *abfd) |
252b5132 | 264 | { |
c044fabd | 265 | /* I think this can be done just like an object file. */ |
ae95ffa6 | 266 | return abfd->xvec->_bfd_set_format[(int) bfd_object] (abfd); |
252b5132 RH |
267 | } |
268 | ||
72a80a16 | 269 | static char * |
217aa764 | 270 | bfd_elf_get_str_section (bfd *abfd, unsigned int shindex) |
252b5132 RH |
271 | { |
272 | Elf_Internal_Shdr **i_shdrp; | |
f075ee0c | 273 | bfd_byte *shstrtab = NULL; |
dc810e39 AM |
274 | file_ptr offset; |
275 | bfd_size_type shstrtabsize; | |
252b5132 RH |
276 | |
277 | i_shdrp = elf_elfsections (abfd); | |
74f2e02b AM |
278 | if (i_shdrp == 0 |
279 | || shindex >= elf_numsections (abfd) | |
280 | || i_shdrp[shindex] == 0) | |
f075ee0c | 281 | return NULL; |
252b5132 | 282 | |
f075ee0c | 283 | shstrtab = i_shdrp[shindex]->contents; |
252b5132 RH |
284 | if (shstrtab == NULL) |
285 | { | |
c044fabd | 286 | /* No cached one, attempt to read, and cache what we read. */ |
252b5132 RH |
287 | offset = i_shdrp[shindex]->sh_offset; |
288 | shstrtabsize = i_shdrp[shindex]->sh_size; | |
c6c60d09 JJ |
289 | |
290 | /* Allocate and clear an extra byte at the end, to prevent crashes | |
291 | in case the string table is not terminated. */ | |
3471d59d | 292 | if (shstrtabsize + 1 <= 1 |
a50b1753 | 293 | || (shstrtab = (bfd_byte *) bfd_alloc (abfd, shstrtabsize + 1)) == NULL |
c6c60d09 JJ |
294 | || bfd_seek (abfd, offset, SEEK_SET) != 0) |
295 | shstrtab = NULL; | |
296 | else if (bfd_bread (shstrtab, shstrtabsize, abfd) != shstrtabsize) | |
297 | { | |
298 | if (bfd_get_error () != bfd_error_system_call) | |
299 | bfd_set_error (bfd_error_file_truncated); | |
300 | shstrtab = NULL; | |
3471d59d CC |
301 | /* Once we've failed to read it, make sure we don't keep |
302 | trying. Otherwise, we'll keep allocating space for | |
303 | the string table over and over. */ | |
304 | i_shdrp[shindex]->sh_size = 0; | |
c6c60d09 JJ |
305 | } |
306 | else | |
307 | shstrtab[shstrtabsize] = '\0'; | |
217aa764 | 308 | i_shdrp[shindex]->contents = shstrtab; |
252b5132 | 309 | } |
f075ee0c | 310 | return (char *) shstrtab; |
252b5132 RH |
311 | } |
312 | ||
313 | char * | |
217aa764 AM |
314 | bfd_elf_string_from_elf_section (bfd *abfd, |
315 | unsigned int shindex, | |
316 | unsigned int strindex) | |
252b5132 RH |
317 | { |
318 | Elf_Internal_Shdr *hdr; | |
319 | ||
320 | if (strindex == 0) | |
321 | return ""; | |
322 | ||
74f2e02b AM |
323 | if (elf_elfsections (abfd) == NULL || shindex >= elf_numsections (abfd)) |
324 | return NULL; | |
325 | ||
252b5132 RH |
326 | hdr = elf_elfsections (abfd)[shindex]; |
327 | ||
328 | if (hdr->contents == NULL | |
329 | && bfd_elf_get_str_section (abfd, shindex) == NULL) | |
330 | return NULL; | |
331 | ||
332 | if (strindex >= hdr->sh_size) | |
333 | { | |
1b3a8575 | 334 | unsigned int shstrndx = elf_elfheader(abfd)->e_shstrndx; |
252b5132 | 335 | (*_bfd_error_handler) |
d003868e AM |
336 | (_("%B: invalid string offset %u >= %lu for section `%s'"), |
337 | abfd, strindex, (unsigned long) hdr->sh_size, | |
1b3a8575 | 338 | (shindex == shstrndx && strindex == hdr->sh_name |
252b5132 | 339 | ? ".shstrtab" |
1b3a8575 | 340 | : bfd_elf_string_from_elf_section (abfd, shstrndx, hdr->sh_name))); |
45b222d6 | 341 | return NULL; |
252b5132 RH |
342 | } |
343 | ||
344 | return ((char *) hdr->contents) + strindex; | |
345 | } | |
346 | ||
6cdc0ccc AM |
347 | /* Read and convert symbols to internal format. |
348 | SYMCOUNT specifies the number of symbols to read, starting from | |
349 | symbol SYMOFFSET. If any of INTSYM_BUF, EXTSYM_BUF or EXTSHNDX_BUF | |
350 | are non-NULL, they are used to store the internal symbols, external | |
b7c368d0 NC |
351 | symbols, and symbol section index extensions, respectively. |
352 | Returns a pointer to the internal symbol buffer (malloced if necessary) | |
353 | or NULL if there were no symbols or some kind of problem. */ | |
6cdc0ccc AM |
354 | |
355 | Elf_Internal_Sym * | |
217aa764 AM |
356 | bfd_elf_get_elf_syms (bfd *ibfd, |
357 | Elf_Internal_Shdr *symtab_hdr, | |
358 | size_t symcount, | |
359 | size_t symoffset, | |
360 | Elf_Internal_Sym *intsym_buf, | |
361 | void *extsym_buf, | |
362 | Elf_External_Sym_Shndx *extshndx_buf) | |
6cdc0ccc AM |
363 | { |
364 | Elf_Internal_Shdr *shndx_hdr; | |
217aa764 | 365 | void *alloc_ext; |
df622259 | 366 | const bfd_byte *esym; |
6cdc0ccc AM |
367 | Elf_External_Sym_Shndx *alloc_extshndx; |
368 | Elf_External_Sym_Shndx *shndx; | |
4dd07732 | 369 | Elf_Internal_Sym *alloc_intsym; |
6cdc0ccc AM |
370 | Elf_Internal_Sym *isym; |
371 | Elf_Internal_Sym *isymend; | |
9c5bfbb7 | 372 | const struct elf_backend_data *bed; |
6cdc0ccc AM |
373 | size_t extsym_size; |
374 | bfd_size_type amt; | |
375 | file_ptr pos; | |
376 | ||
e44a2c9c AM |
377 | if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour) |
378 | abort (); | |
379 | ||
6cdc0ccc AM |
380 | if (symcount == 0) |
381 | return intsym_buf; | |
382 | ||
383 | /* Normal syms might have section extension entries. */ | |
384 | shndx_hdr = NULL; | |
385 | if (symtab_hdr == &elf_tdata (ibfd)->symtab_hdr) | |
386 | shndx_hdr = &elf_tdata (ibfd)->symtab_shndx_hdr; | |
387 | ||
388 | /* Read the symbols. */ | |
389 | alloc_ext = NULL; | |
390 | alloc_extshndx = NULL; | |
4dd07732 | 391 | alloc_intsym = NULL; |
6cdc0ccc AM |
392 | bed = get_elf_backend_data (ibfd); |
393 | extsym_size = bed->s->sizeof_sym; | |
394 | amt = symcount * extsym_size; | |
395 | pos = symtab_hdr->sh_offset + symoffset * extsym_size; | |
396 | if (extsym_buf == NULL) | |
397 | { | |
d0fb9a8d | 398 | alloc_ext = bfd_malloc2 (symcount, extsym_size); |
6cdc0ccc AM |
399 | extsym_buf = alloc_ext; |
400 | } | |
401 | if (extsym_buf == NULL | |
402 | || bfd_seek (ibfd, pos, SEEK_SET) != 0 | |
403 | || bfd_bread (extsym_buf, amt, ibfd) != amt) | |
404 | { | |
405 | intsym_buf = NULL; | |
406 | goto out; | |
407 | } | |
408 | ||
409 | if (shndx_hdr == NULL || shndx_hdr->sh_size == 0) | |
410 | extshndx_buf = NULL; | |
411 | else | |
412 | { | |
413 | amt = symcount * sizeof (Elf_External_Sym_Shndx); | |
414 | pos = shndx_hdr->sh_offset + symoffset * sizeof (Elf_External_Sym_Shndx); | |
415 | if (extshndx_buf == NULL) | |
416 | { | |
a50b1753 NC |
417 | alloc_extshndx = (Elf_External_Sym_Shndx *) |
418 | bfd_malloc2 (symcount, sizeof (Elf_External_Sym_Shndx)); | |
6cdc0ccc AM |
419 | extshndx_buf = alloc_extshndx; |
420 | } | |
421 | if (extshndx_buf == NULL | |
422 | || bfd_seek (ibfd, pos, SEEK_SET) != 0 | |
423 | || bfd_bread (extshndx_buf, amt, ibfd) != amt) | |
424 | { | |
425 | intsym_buf = NULL; | |
426 | goto out; | |
427 | } | |
428 | } | |
429 | ||
430 | if (intsym_buf == NULL) | |
431 | { | |
a50b1753 NC |
432 | alloc_intsym = (Elf_Internal_Sym *) |
433 | bfd_malloc2 (symcount, sizeof (Elf_Internal_Sym)); | |
4dd07732 | 434 | intsym_buf = alloc_intsym; |
6cdc0ccc AM |
435 | if (intsym_buf == NULL) |
436 | goto out; | |
437 | } | |
438 | ||
439 | /* Convert the symbols to internal form. */ | |
440 | isymend = intsym_buf + symcount; | |
a50b1753 NC |
441 | for (esym = (const bfd_byte *) extsym_buf, isym = intsym_buf, |
442 | shndx = extshndx_buf; | |
6cdc0ccc AM |
443 | isym < isymend; |
444 | esym += extsym_size, isym++, shndx = shndx != NULL ? shndx + 1 : NULL) | |
8384fb8f AM |
445 | if (!(*bed->s->swap_symbol_in) (ibfd, esym, shndx, isym)) |
446 | { | |
447 | symoffset += (esym - (bfd_byte *) extsym_buf) / extsym_size; | |
448 | (*_bfd_error_handler) (_("%B symbol number %lu references " | |
449 | "nonexistent SHT_SYMTAB_SHNDX section"), | |
450 | ibfd, (unsigned long) symoffset); | |
4dd07732 AM |
451 | if (alloc_intsym != NULL) |
452 | free (alloc_intsym); | |
8384fb8f AM |
453 | intsym_buf = NULL; |
454 | goto out; | |
455 | } | |
6cdc0ccc AM |
456 | |
457 | out: | |
458 | if (alloc_ext != NULL) | |
459 | free (alloc_ext); | |
460 | if (alloc_extshndx != NULL) | |
461 | free (alloc_extshndx); | |
462 | ||
463 | return intsym_buf; | |
464 | } | |
465 | ||
5cab59f6 AM |
466 | /* Look up a symbol name. */ |
467 | const char * | |
be8dd2ca AM |
468 | bfd_elf_sym_name (bfd *abfd, |
469 | Elf_Internal_Shdr *symtab_hdr, | |
26c61ae5 L |
470 | Elf_Internal_Sym *isym, |
471 | asection *sym_sec) | |
5cab59f6 | 472 | { |
26c61ae5 | 473 | const char *name; |
5cab59f6 | 474 | unsigned int iname = isym->st_name; |
be8dd2ca | 475 | unsigned int shindex = symtab_hdr->sh_link; |
26c61ae5 | 476 | |
138f35cc JJ |
477 | if (iname == 0 && ELF_ST_TYPE (isym->st_info) == STT_SECTION |
478 | /* Check for a bogus st_shndx to avoid crashing. */ | |
4fbb74a6 | 479 | && isym->st_shndx < elf_numsections (abfd)) |
5cab59f6 AM |
480 | { |
481 | iname = elf_elfsections (abfd)[isym->st_shndx]->sh_name; | |
482 | shindex = elf_elfheader (abfd)->e_shstrndx; | |
483 | } | |
484 | ||
26c61ae5 L |
485 | name = bfd_elf_string_from_elf_section (abfd, shindex, iname); |
486 | if (name == NULL) | |
487 | name = "(null)"; | |
488 | else if (sym_sec && *name == '\0') | |
489 | name = bfd_section_name (abfd, sym_sec); | |
490 | ||
491 | return name; | |
5cab59f6 AM |
492 | } |
493 | ||
dbb410c3 AM |
494 | /* Elf_Internal_Shdr->contents is an array of these for SHT_GROUP |
495 | sections. The first element is the flags, the rest are section | |
496 | pointers. */ | |
497 | ||
498 | typedef union elf_internal_group { | |
499 | Elf_Internal_Shdr *shdr; | |
500 | unsigned int flags; | |
501 | } Elf_Internal_Group; | |
502 | ||
b885599b AM |
503 | /* Return the name of the group signature symbol. Why isn't the |
504 | signature just a string? */ | |
505 | ||
506 | static const char * | |
217aa764 | 507 | group_signature (bfd *abfd, Elf_Internal_Shdr *ghdr) |
b885599b | 508 | { |
9dce4196 | 509 | Elf_Internal_Shdr *hdr; |
9dce4196 AM |
510 | unsigned char esym[sizeof (Elf64_External_Sym)]; |
511 | Elf_External_Sym_Shndx eshndx; | |
512 | Elf_Internal_Sym isym; | |
b885599b | 513 | |
13792e9d L |
514 | /* First we need to ensure the symbol table is available. Make sure |
515 | that it is a symbol table section. */ | |
4fbb74a6 AM |
516 | if (ghdr->sh_link >= elf_numsections (abfd)) |
517 | return NULL; | |
13792e9d L |
518 | hdr = elf_elfsections (abfd) [ghdr->sh_link]; |
519 | if (hdr->sh_type != SHT_SYMTAB | |
520 | || ! bfd_section_from_shdr (abfd, ghdr->sh_link)) | |
b885599b AM |
521 | return NULL; |
522 | ||
9dce4196 AM |
523 | /* Go read the symbol. */ |
524 | hdr = &elf_tdata (abfd)->symtab_hdr; | |
6cdc0ccc AM |
525 | if (bfd_elf_get_elf_syms (abfd, hdr, 1, ghdr->sh_info, |
526 | &isym, esym, &eshndx) == NULL) | |
b885599b | 527 | return NULL; |
9dce4196 | 528 | |
26c61ae5 | 529 | return bfd_elf_sym_name (abfd, hdr, &isym, NULL); |
b885599b AM |
530 | } |
531 | ||
dbb410c3 AM |
532 | /* Set next_in_group list pointer, and group name for NEWSECT. */ |
533 | ||
b34976b6 | 534 | static bfd_boolean |
217aa764 | 535 | setup_group (bfd *abfd, Elf_Internal_Shdr *hdr, asection *newsect) |
dbb410c3 AM |
536 | { |
537 | unsigned int num_group = elf_tdata (abfd)->num_group; | |
538 | ||
539 | /* If num_group is zero, read in all SHT_GROUP sections. The count | |
540 | is set to -1 if there are no SHT_GROUP sections. */ | |
541 | if (num_group == 0) | |
542 | { | |
543 | unsigned int i, shnum; | |
544 | ||
545 | /* First count the number of groups. If we have a SHT_GROUP | |
546 | section with just a flag word (ie. sh_size is 4), ignore it. */ | |
9ad5cbcf | 547 | shnum = elf_numsections (abfd); |
dbb410c3 | 548 | num_group = 0; |
08a40648 | 549 | |
44534af3 | 550 | #define IS_VALID_GROUP_SECTION_HEADER(shdr, minsize) \ |
1783205a | 551 | ( (shdr)->sh_type == SHT_GROUP \ |
44534af3 | 552 | && (shdr)->sh_size >= minsize \ |
1783205a NC |
553 | && (shdr)->sh_entsize == GRP_ENTRY_SIZE \ |
554 | && ((shdr)->sh_size % GRP_ENTRY_SIZE) == 0) | |
08a40648 | 555 | |
dbb410c3 AM |
556 | for (i = 0; i < shnum; i++) |
557 | { | |
558 | Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i]; | |
1783205a | 559 | |
44534af3 | 560 | if (IS_VALID_GROUP_SECTION_HEADER (shdr, 2 * GRP_ENTRY_SIZE)) |
dbb410c3 AM |
561 | num_group += 1; |
562 | } | |
563 | ||
564 | if (num_group == 0) | |
20dbb49d L |
565 | { |
566 | num_group = (unsigned) -1; | |
567 | elf_tdata (abfd)->num_group = num_group; | |
568 | } | |
569 | else | |
dbb410c3 AM |
570 | { |
571 | /* We keep a list of elf section headers for group sections, | |
572 | so we can find them quickly. */ | |
20dbb49d | 573 | bfd_size_type amt; |
d0fb9a8d | 574 | |
20dbb49d | 575 | elf_tdata (abfd)->num_group = num_group; |
a50b1753 NC |
576 | elf_tdata (abfd)->group_sect_ptr = (Elf_Internal_Shdr **) |
577 | bfd_alloc2 (abfd, num_group, sizeof (Elf_Internal_Shdr *)); | |
dbb410c3 | 578 | if (elf_tdata (abfd)->group_sect_ptr == NULL) |
b34976b6 | 579 | return FALSE; |
dbb410c3 AM |
580 | |
581 | num_group = 0; | |
582 | for (i = 0; i < shnum; i++) | |
583 | { | |
584 | Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i]; | |
1783205a | 585 | |
44534af3 | 586 | if (IS_VALID_GROUP_SECTION_HEADER (shdr, 2 * GRP_ENTRY_SIZE)) |
dbb410c3 | 587 | { |
973ffd63 | 588 | unsigned char *src; |
dbb410c3 AM |
589 | Elf_Internal_Group *dest; |
590 | ||
591 | /* Add to list of sections. */ | |
592 | elf_tdata (abfd)->group_sect_ptr[num_group] = shdr; | |
593 | num_group += 1; | |
594 | ||
595 | /* Read the raw contents. */ | |
596 | BFD_ASSERT (sizeof (*dest) >= 4); | |
597 | amt = shdr->sh_size * sizeof (*dest) / 4; | |
a50b1753 NC |
598 | shdr->contents = (unsigned char *) |
599 | bfd_alloc2 (abfd, shdr->sh_size, sizeof (*dest) / 4); | |
1783205a NC |
600 | /* PR binutils/4110: Handle corrupt group headers. */ |
601 | if (shdr->contents == NULL) | |
602 | { | |
603 | _bfd_error_handler | |
604 | (_("%B: Corrupt size field in group section header: 0x%lx"), abfd, shdr->sh_size); | |
605 | bfd_set_error (bfd_error_bad_value); | |
606 | return FALSE; | |
607 | } | |
608 | ||
609 | memset (shdr->contents, 0, amt); | |
610 | ||
611 | if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0 | |
dbb410c3 AM |
612 | || (bfd_bread (shdr->contents, shdr->sh_size, abfd) |
613 | != shdr->sh_size)) | |
b34976b6 | 614 | return FALSE; |
dbb410c3 AM |
615 | |
616 | /* Translate raw contents, a flag word followed by an | |
617 | array of elf section indices all in target byte order, | |
618 | to the flag word followed by an array of elf section | |
619 | pointers. */ | |
620 | src = shdr->contents + shdr->sh_size; | |
621 | dest = (Elf_Internal_Group *) (shdr->contents + amt); | |
622 | while (1) | |
623 | { | |
624 | unsigned int idx; | |
625 | ||
626 | src -= 4; | |
627 | --dest; | |
628 | idx = H_GET_32 (abfd, src); | |
629 | if (src == shdr->contents) | |
630 | { | |
631 | dest->flags = idx; | |
b885599b AM |
632 | if (shdr->bfd_section != NULL && (idx & GRP_COMDAT)) |
633 | shdr->bfd_section->flags | |
634 | |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD; | |
dbb410c3 AM |
635 | break; |
636 | } | |
637 | if (idx >= shnum) | |
638 | { | |
639 | ((*_bfd_error_handler) | |
d003868e | 640 | (_("%B: invalid SHT_GROUP entry"), abfd)); |
dbb410c3 AM |
641 | idx = 0; |
642 | } | |
643 | dest->shdr = elf_elfsections (abfd)[idx]; | |
644 | } | |
645 | } | |
646 | } | |
647 | } | |
648 | } | |
649 | ||
650 | if (num_group != (unsigned) -1) | |
651 | { | |
652 | unsigned int i; | |
653 | ||
654 | for (i = 0; i < num_group; i++) | |
655 | { | |
656 | Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i]; | |
657 | Elf_Internal_Group *idx = (Elf_Internal_Group *) shdr->contents; | |
658 | unsigned int n_elt = shdr->sh_size / 4; | |
659 | ||
660 | /* Look through this group's sections to see if current | |
661 | section is a member. */ | |
662 | while (--n_elt != 0) | |
663 | if ((++idx)->shdr == hdr) | |
664 | { | |
e0e8c97f | 665 | asection *s = NULL; |
dbb410c3 AM |
666 | |
667 | /* We are a member of this group. Go looking through | |
668 | other members to see if any others are linked via | |
669 | next_in_group. */ | |
670 | idx = (Elf_Internal_Group *) shdr->contents; | |
671 | n_elt = shdr->sh_size / 4; | |
672 | while (--n_elt != 0) | |
673 | if ((s = (++idx)->shdr->bfd_section) != NULL | |
945906ff | 674 | && elf_next_in_group (s) != NULL) |
dbb410c3 AM |
675 | break; |
676 | if (n_elt != 0) | |
677 | { | |
dbb410c3 AM |
678 | /* Snarf the group name from other member, and |
679 | insert current section in circular list. */ | |
945906ff AM |
680 | elf_group_name (newsect) = elf_group_name (s); |
681 | elf_next_in_group (newsect) = elf_next_in_group (s); | |
682 | elf_next_in_group (s) = newsect; | |
dbb410c3 AM |
683 | } |
684 | else | |
685 | { | |
dbb410c3 AM |
686 | const char *gname; |
687 | ||
b885599b AM |
688 | gname = group_signature (abfd, shdr); |
689 | if (gname == NULL) | |
b34976b6 | 690 | return FALSE; |
945906ff | 691 | elf_group_name (newsect) = gname; |
dbb410c3 AM |
692 | |
693 | /* Start a circular list with one element. */ | |
945906ff | 694 | elf_next_in_group (newsect) = newsect; |
dbb410c3 | 695 | } |
b885599b | 696 | |
9dce4196 AM |
697 | /* If the group section has been created, point to the |
698 | new member. */ | |
dbb410c3 | 699 | if (shdr->bfd_section != NULL) |
945906ff | 700 | elf_next_in_group (shdr->bfd_section) = newsect; |
b885599b | 701 | |
dbb410c3 AM |
702 | i = num_group - 1; |
703 | break; | |
704 | } | |
705 | } | |
706 | } | |
707 | ||
945906ff | 708 | if (elf_group_name (newsect) == NULL) |
dbb410c3 | 709 | { |
d003868e AM |
710 | (*_bfd_error_handler) (_("%B: no group info for section %A"), |
711 | abfd, newsect); | |
dbb410c3 | 712 | } |
b34976b6 | 713 | return TRUE; |
dbb410c3 AM |
714 | } |
715 | ||
3d7f7666 | 716 | bfd_boolean |
dd863624 | 717 | _bfd_elf_setup_sections (bfd *abfd) |
3d7f7666 L |
718 | { |
719 | unsigned int i; | |
720 | unsigned int num_group = elf_tdata (abfd)->num_group; | |
721 | bfd_boolean result = TRUE; | |
dd863624 L |
722 | asection *s; |
723 | ||
724 | /* Process SHF_LINK_ORDER. */ | |
725 | for (s = abfd->sections; s != NULL; s = s->next) | |
726 | { | |
727 | Elf_Internal_Shdr *this_hdr = &elf_section_data (s)->this_hdr; | |
728 | if ((this_hdr->sh_flags & SHF_LINK_ORDER) != 0) | |
729 | { | |
730 | unsigned int elfsec = this_hdr->sh_link; | |
731 | /* FIXME: The old Intel compiler and old strip/objcopy may | |
732 | not set the sh_link or sh_info fields. Hence we could | |
733 | get the situation where elfsec is 0. */ | |
734 | if (elfsec == 0) | |
735 | { | |
4fbb74a6 | 736 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
dd863624 L |
737 | if (bed->link_order_error_handler) |
738 | bed->link_order_error_handler | |
739 | (_("%B: warning: sh_link not set for section `%A'"), | |
740 | abfd, s); | |
741 | } | |
742 | else | |
743 | { | |
91d6fa6a | 744 | asection *linksec = NULL; |
25bbc984 | 745 | |
4fbb74a6 AM |
746 | if (elfsec < elf_numsections (abfd)) |
747 | { | |
748 | this_hdr = elf_elfsections (abfd)[elfsec]; | |
91d6fa6a | 749 | linksec = this_hdr->bfd_section; |
4fbb74a6 | 750 | } |
25bbc984 L |
751 | |
752 | /* PR 1991, 2008: | |
753 | Some strip/objcopy may leave an incorrect value in | |
754 | sh_link. We don't want to proceed. */ | |
91d6fa6a | 755 | if (linksec == NULL) |
25bbc984 L |
756 | { |
757 | (*_bfd_error_handler) | |
758 | (_("%B: sh_link [%d] in section `%A' is incorrect"), | |
759 | s->owner, s, elfsec); | |
760 | result = FALSE; | |
761 | } | |
762 | ||
91d6fa6a | 763 | elf_linked_to_section (s) = linksec; |
dd863624 L |
764 | } |
765 | } | |
766 | } | |
3d7f7666 | 767 | |
dd863624 | 768 | /* Process section groups. */ |
3d7f7666 L |
769 | if (num_group == (unsigned) -1) |
770 | return result; | |
771 | ||
772 | for (i = 0; i < num_group; i++) | |
773 | { | |
774 | Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i]; | |
775 | Elf_Internal_Group *idx = (Elf_Internal_Group *) shdr->contents; | |
776 | unsigned int n_elt = shdr->sh_size / 4; | |
777 | ||
778 | while (--n_elt != 0) | |
779 | if ((++idx)->shdr->bfd_section) | |
780 | elf_sec_group (idx->shdr->bfd_section) = shdr->bfd_section; | |
781 | else if (idx->shdr->sh_type == SHT_RELA | |
782 | || idx->shdr->sh_type == SHT_REL) | |
783 | /* We won't include relocation sections in section groups in | |
784 | output object files. We adjust the group section size here | |
785 | so that relocatable link will work correctly when | |
786 | relocation sections are in section group in input object | |
787 | files. */ | |
788 | shdr->bfd_section->size -= 4; | |
789 | else | |
790 | { | |
791 | /* There are some unknown sections in the group. */ | |
792 | (*_bfd_error_handler) | |
d003868e AM |
793 | (_("%B: unknown [%d] section `%s' in group [%s]"), |
794 | abfd, | |
3d7f7666 | 795 | (unsigned int) idx->shdr->sh_type, |
1b3a8575 AM |
796 | bfd_elf_string_from_elf_section (abfd, |
797 | (elf_elfheader (abfd) | |
798 | ->e_shstrndx), | |
799 | idx->shdr->sh_name), | |
3d7f7666 L |
800 | shdr->bfd_section->name); |
801 | result = FALSE; | |
802 | } | |
803 | } | |
804 | return result; | |
805 | } | |
806 | ||
72adc230 AM |
807 | bfd_boolean |
808 | bfd_elf_is_group_section (bfd *abfd ATTRIBUTE_UNUSED, const asection *sec) | |
809 | { | |
810 | return elf_next_in_group (sec) != NULL; | |
811 | } | |
812 | ||
252b5132 RH |
813 | /* Make a BFD section from an ELF section. We store a pointer to the |
814 | BFD section in the bfd_section field of the header. */ | |
815 | ||
b34976b6 | 816 | bfd_boolean |
217aa764 AM |
817 | _bfd_elf_make_section_from_shdr (bfd *abfd, |
818 | Elf_Internal_Shdr *hdr, | |
6dc132d9 L |
819 | const char *name, |
820 | int shindex) | |
252b5132 RH |
821 | { |
822 | asection *newsect; | |
823 | flagword flags; | |
9c5bfbb7 | 824 | const struct elf_backend_data *bed; |
252b5132 RH |
825 | |
826 | if (hdr->bfd_section != NULL) | |
4e011fb5 | 827 | return TRUE; |
252b5132 RH |
828 | |
829 | newsect = bfd_make_section_anyway (abfd, name); | |
830 | if (newsect == NULL) | |
b34976b6 | 831 | return FALSE; |
252b5132 | 832 | |
1829f4b2 AM |
833 | hdr->bfd_section = newsect; |
834 | elf_section_data (newsect)->this_hdr = *hdr; | |
6dc132d9 | 835 | elf_section_data (newsect)->this_idx = shindex; |
1829f4b2 | 836 | |
2f89ff8d L |
837 | /* Always use the real type/flags. */ |
838 | elf_section_type (newsect) = hdr->sh_type; | |
839 | elf_section_flags (newsect) = hdr->sh_flags; | |
840 | ||
252b5132 RH |
841 | newsect->filepos = hdr->sh_offset; |
842 | ||
843 | if (! bfd_set_section_vma (abfd, newsect, hdr->sh_addr) | |
844 | || ! bfd_set_section_size (abfd, newsect, hdr->sh_size) | |
845 | || ! bfd_set_section_alignment (abfd, newsect, | |
72de5009 | 846 | bfd_log2 (hdr->sh_addralign))) |
b34976b6 | 847 | return FALSE; |
252b5132 RH |
848 | |
849 | flags = SEC_NO_FLAGS; | |
850 | if (hdr->sh_type != SHT_NOBITS) | |
851 | flags |= SEC_HAS_CONTENTS; | |
dbb410c3 | 852 | if (hdr->sh_type == SHT_GROUP) |
b3096250 | 853 | flags |= SEC_GROUP | SEC_EXCLUDE; |
252b5132 RH |
854 | if ((hdr->sh_flags & SHF_ALLOC) != 0) |
855 | { | |
856 | flags |= SEC_ALLOC; | |
857 | if (hdr->sh_type != SHT_NOBITS) | |
858 | flags |= SEC_LOAD; | |
859 | } | |
860 | if ((hdr->sh_flags & SHF_WRITE) == 0) | |
861 | flags |= SEC_READONLY; | |
862 | if ((hdr->sh_flags & SHF_EXECINSTR) != 0) | |
863 | flags |= SEC_CODE; | |
864 | else if ((flags & SEC_LOAD) != 0) | |
865 | flags |= SEC_DATA; | |
f5fa8ca2 JJ |
866 | if ((hdr->sh_flags & SHF_MERGE) != 0) |
867 | { | |
868 | flags |= SEC_MERGE; | |
869 | newsect->entsize = hdr->sh_entsize; | |
870 | if ((hdr->sh_flags & SHF_STRINGS) != 0) | |
871 | flags |= SEC_STRINGS; | |
872 | } | |
dbb410c3 AM |
873 | if (hdr->sh_flags & SHF_GROUP) |
874 | if (!setup_group (abfd, hdr, newsect)) | |
b34976b6 | 875 | return FALSE; |
13ae64f3 JJ |
876 | if ((hdr->sh_flags & SHF_TLS) != 0) |
877 | flags |= SEC_THREAD_LOCAL; | |
18ae9cc1 L |
878 | if ((hdr->sh_flags & SHF_EXCLUDE) != 0) |
879 | flags |= SEC_EXCLUDE; | |
252b5132 | 880 | |
3d2b39cf | 881 | if ((flags & SEC_ALLOC) == 0) |
7a6cc5fb | 882 | { |
3d2b39cf L |
883 | /* The debugging sections appear to be recognized only by name, |
884 | not any sort of flag. Their SEC_ALLOC bits are cleared. */ | |
3d2b39cf L |
885 | if (name [0] == '.') |
886 | { | |
f073ced3 AM |
887 | const char *p; |
888 | int n; | |
889 | if (name[1] == 'd') | |
890 | p = ".debug", n = 6; | |
891 | else if (name[1] == 'g' && name[2] == 'n') | |
892 | p = ".gnu.linkonce.wi.", n = 17; | |
893 | else if (name[1] == 'g' && name[2] == 'd') | |
894 | p = ".gdb_index", n = 11; /* yes we really do mean 11. */ | |
895 | else if (name[1] == 'l') | |
896 | p = ".line", n = 5; | |
897 | else if (name[1] == 's') | |
898 | p = ".stab", n = 5; | |
899 | else if (name[1] == 'z') | |
900 | p = ".zdebug", n = 7; | |
901 | else | |
902 | p = NULL, n = 0; | |
903 | if (p != NULL && strncmp (name, p, n) == 0) | |
3d2b39cf L |
904 | flags |= SEC_DEBUGGING; |
905 | } | |
906 | } | |
252b5132 RH |
907 | |
908 | /* As a GNU extension, if the name begins with .gnu.linkonce, we | |
909 | only link a single copy of the section. This is used to support | |
910 | g++. g++ will emit each template expansion in its own section. | |
911 | The symbols will be defined as weak, so that multiple definitions | |
912 | are permitted. The GNU linker extension is to actually discard | |
913 | all but one of the sections. */ | |
0112cd26 | 914 | if (CONST_STRNEQ (name, ".gnu.linkonce") |
b885599b | 915 | && elf_next_in_group (newsect) == NULL) |
252b5132 RH |
916 | flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD; |
917 | ||
fa152c49 JW |
918 | bed = get_elf_backend_data (abfd); |
919 | if (bed->elf_backend_section_flags) | |
920 | if (! bed->elf_backend_section_flags (&flags, hdr)) | |
b34976b6 | 921 | return FALSE; |
fa152c49 | 922 | |
252b5132 | 923 | if (! bfd_set_section_flags (abfd, newsect, flags)) |
b34976b6 | 924 | return FALSE; |
252b5132 | 925 | |
718175fa JK |
926 | /* We do not parse the PT_NOTE segments as we are interested even in the |
927 | separate debug info files which may have the segments offsets corrupted. | |
928 | PT_NOTEs from the core files are currently not parsed using BFD. */ | |
929 | if (hdr->sh_type == SHT_NOTE) | |
930 | { | |
baea7ef1 | 931 | bfd_byte *contents; |
718175fa | 932 | |
baea7ef1 | 933 | if (!bfd_malloc_and_get_section (abfd, newsect, &contents)) |
718175fa JK |
934 | return FALSE; |
935 | ||
baea7ef1 | 936 | elf_parse_notes (abfd, (char *) contents, hdr->sh_size, -1); |
718175fa JK |
937 | free (contents); |
938 | } | |
939 | ||
252b5132 RH |
940 | if ((flags & SEC_ALLOC) != 0) |
941 | { | |
942 | Elf_Internal_Phdr *phdr; | |
6ffd7900 AM |
943 | unsigned int i, nload; |
944 | ||
945 | /* Some ELF linkers produce binaries with all the program header | |
946 | p_paddr fields zero. If we have such a binary with more than | |
947 | one PT_LOAD header, then leave the section lma equal to vma | |
948 | so that we don't create sections with overlapping lma. */ | |
949 | phdr = elf_tdata (abfd)->phdr; | |
950 | for (nload = 0, i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++) | |
951 | if (phdr->p_paddr != 0) | |
952 | break; | |
953 | else if (phdr->p_type == PT_LOAD && phdr->p_memsz != 0) | |
954 | ++nload; | |
955 | if (i >= elf_elfheader (abfd)->e_phnum && nload > 1) | |
956 | return TRUE; | |
252b5132 | 957 | |
252b5132 RH |
958 | phdr = elf_tdata (abfd)->phdr; |
959 | for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++) | |
960 | { | |
86b2281f AM |
961 | if (((phdr->p_type == PT_LOAD |
962 | && (hdr->sh_flags & SHF_TLS) == 0) | |
963 | || phdr->p_type == PT_TLS) | |
9a83a553 | 964 | && ELF_SECTION_IN_SEGMENT (hdr, phdr)) |
252b5132 | 965 | { |
88967714 AM |
966 | if ((flags & SEC_LOAD) == 0) |
967 | newsect->lma = (phdr->p_paddr | |
968 | + hdr->sh_addr - phdr->p_vaddr); | |
969 | else | |
970 | /* We used to use the same adjustment for SEC_LOAD | |
971 | sections, but that doesn't work if the segment | |
972 | is packed with code from multiple VMAs. | |
973 | Instead we calculate the section LMA based on | |
974 | the segment LMA. It is assumed that the | |
975 | segment will contain sections with contiguous | |
976 | LMAs, even if the VMAs are not. */ | |
977 | newsect->lma = (phdr->p_paddr | |
978 | + hdr->sh_offset - phdr->p_offset); | |
979 | ||
980 | /* With contiguous segments, we can't tell from file | |
981 | offsets whether a section with zero size should | |
982 | be placed at the end of one segment or the | |
983 | beginning of the next. Decide based on vaddr. */ | |
984 | if (hdr->sh_addr >= phdr->p_vaddr | |
985 | && (hdr->sh_addr + hdr->sh_size | |
986 | <= phdr->p_vaddr + phdr->p_memsz)) | |
987 | break; | |
252b5132 RH |
988 | } |
989 | } | |
990 | } | |
991 | ||
4a114e3e L |
992 | /* Compress/decompress DWARF debug sections with names: .debug_* and |
993 | .zdebug_*, after the section flags is set. */ | |
994 | if ((flags & SEC_DEBUGGING) | |
995 | && ((name[1] == 'd' && name[6] == '_') | |
996 | || (name[1] == 'z' && name[7] == '_'))) | |
997 | { | |
998 | enum { nothing, compress, decompress } action = nothing; | |
4e011fb5 | 999 | char *new_name; |
4a114e3e L |
1000 | |
1001 | if (bfd_is_section_compressed (abfd, newsect)) | |
1002 | { | |
1003 | /* Compressed section. Check if we should decompress. */ | |
1004 | if ((abfd->flags & BFD_DECOMPRESS)) | |
1005 | action = decompress; | |
1006 | } | |
1007 | else | |
1008 | { | |
1009 | /* Normal section. Check if we should compress. */ | |
5a5ed5b0 | 1010 | if ((abfd->flags & BFD_COMPRESS) && newsect->size != 0) |
4a114e3e L |
1011 | action = compress; |
1012 | } | |
1013 | ||
4e011fb5 | 1014 | new_name = NULL; |
4a114e3e L |
1015 | switch (action) |
1016 | { | |
1017 | case nothing: | |
1018 | break; | |
1019 | case compress: | |
1020 | if (!bfd_init_section_compress_status (abfd, newsect)) | |
1021 | { | |
1022 | (*_bfd_error_handler) | |
bc823199 | 1023 | (_("%B: unable to initialize compress status for section %s"), |
4a114e3e L |
1024 | abfd, name); |
1025 | return FALSE; | |
1026 | } | |
4e011fb5 AM |
1027 | if (name[1] != 'z') |
1028 | { | |
1029 | unsigned int len = strlen (name); | |
1030 | ||
1031 | new_name = bfd_alloc (abfd, len + 2); | |
1032 | if (new_name == NULL) | |
1033 | return FALSE; | |
1034 | new_name[0] = '.'; | |
1035 | new_name[1] = 'z'; | |
1036 | memcpy (new_name + 2, name + 1, len); | |
1037 | } | |
4a114e3e L |
1038 | break; |
1039 | case decompress: | |
1040 | if (!bfd_init_section_decompress_status (abfd, newsect)) | |
1041 | { | |
1042 | (*_bfd_error_handler) | |
bc823199 | 1043 | (_("%B: unable to initialize decompress status for section %s"), |
4a114e3e L |
1044 | abfd, name); |
1045 | return FALSE; | |
1046 | } | |
4e011fb5 AM |
1047 | if (name[1] == 'z') |
1048 | { | |
1049 | unsigned int len = strlen (name); | |
1050 | ||
1051 | new_name = bfd_alloc (abfd, len); | |
1052 | if (new_name == NULL) | |
1053 | return FALSE; | |
1054 | new_name[0] = '.'; | |
1055 | memcpy (new_name + 1, name + 2, len - 1); | |
1056 | } | |
4a114e3e L |
1057 | break; |
1058 | } | |
4e011fb5 AM |
1059 | if (new_name != NULL) |
1060 | bfd_rename_section (abfd, newsect, new_name); | |
4a114e3e L |
1061 | } |
1062 | ||
b34976b6 | 1063 | return TRUE; |
252b5132 RH |
1064 | } |
1065 | ||
252b5132 RH |
1066 | const char *const bfd_elf_section_type_names[] = { |
1067 | "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB", | |
1068 | "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE", | |
1069 | "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM", | |
1070 | }; | |
1071 | ||
1049f94e | 1072 | /* ELF relocs are against symbols. If we are producing relocatable |
252b5132 RH |
1073 | output, and the reloc is against an external symbol, and nothing |
1074 | has given us any additional addend, the resulting reloc will also | |
1075 | be against the same symbol. In such a case, we don't want to | |
1076 | change anything about the way the reloc is handled, since it will | |
1077 | all be done at final link time. Rather than put special case code | |
1078 | into bfd_perform_relocation, all the reloc types use this howto | |
1079 | function. It just short circuits the reloc if producing | |
1049f94e | 1080 | relocatable output against an external symbol. */ |
252b5132 | 1081 | |
252b5132 | 1082 | bfd_reloc_status_type |
217aa764 AM |
1083 | bfd_elf_generic_reloc (bfd *abfd ATTRIBUTE_UNUSED, |
1084 | arelent *reloc_entry, | |
1085 | asymbol *symbol, | |
1086 | void *data ATTRIBUTE_UNUSED, | |
1087 | asection *input_section, | |
1088 | bfd *output_bfd, | |
1089 | char **error_message ATTRIBUTE_UNUSED) | |
1090 | { | |
1091 | if (output_bfd != NULL | |
252b5132 RH |
1092 | && (symbol->flags & BSF_SECTION_SYM) == 0 |
1093 | && (! reloc_entry->howto->partial_inplace | |
1094 | || reloc_entry->addend == 0)) | |
1095 | { | |
1096 | reloc_entry->address += input_section->output_offset; | |
1097 | return bfd_reloc_ok; | |
1098 | } | |
1099 | ||
1100 | return bfd_reloc_continue; | |
1101 | } | |
1102 | \f | |
0ac4564e L |
1103 | /* Copy the program header and other data from one object module to |
1104 | another. */ | |
252b5132 | 1105 | |
b34976b6 | 1106 | bfd_boolean |
217aa764 | 1107 | _bfd_elf_copy_private_bfd_data (bfd *ibfd, bfd *obfd) |
2d502050 L |
1108 | { |
1109 | if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour | |
1110 | || bfd_get_flavour (obfd) != bfd_target_elf_flavour) | |
b34976b6 | 1111 | return TRUE; |
2d502050 L |
1112 | |
1113 | BFD_ASSERT (!elf_flags_init (obfd) | |
1114 | || (elf_elfheader (obfd)->e_flags | |
1115 | == elf_elfheader (ibfd)->e_flags)); | |
1116 | ||
0ac4564e | 1117 | elf_gp (obfd) = elf_gp (ibfd); |
2d502050 | 1118 | elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags; |
b34976b6 | 1119 | elf_flags_init (obfd) = TRUE; |
104d59d1 JM |
1120 | |
1121 | /* Copy object attributes. */ | |
1122 | _bfd_elf_copy_obj_attributes (ibfd, obfd); | |
b34976b6 | 1123 | return TRUE; |
2d502050 L |
1124 | } |
1125 | ||
cedc298e L |
1126 | static const char * |
1127 | get_segment_type (unsigned int p_type) | |
1128 | { | |
1129 | const char *pt; | |
1130 | switch (p_type) | |
1131 | { | |
1132 | case PT_NULL: pt = "NULL"; break; | |
1133 | case PT_LOAD: pt = "LOAD"; break; | |
1134 | case PT_DYNAMIC: pt = "DYNAMIC"; break; | |
1135 | case PT_INTERP: pt = "INTERP"; break; | |
1136 | case PT_NOTE: pt = "NOTE"; break; | |
1137 | case PT_SHLIB: pt = "SHLIB"; break; | |
1138 | case PT_PHDR: pt = "PHDR"; break; | |
1139 | case PT_TLS: pt = "TLS"; break; | |
1140 | case PT_GNU_EH_FRAME: pt = "EH_FRAME"; break; | |
2b05f1b7 | 1141 | case PT_GNU_STACK: pt = "STACK"; break; |
cedc298e L |
1142 | case PT_GNU_RELRO: pt = "RELRO"; break; |
1143 | default: pt = NULL; break; | |
1144 | } | |
1145 | return pt; | |
1146 | } | |
1147 | ||
f0b79d91 L |
1148 | /* Print out the program headers. */ |
1149 | ||
b34976b6 | 1150 | bfd_boolean |
217aa764 | 1151 | _bfd_elf_print_private_bfd_data (bfd *abfd, void *farg) |
252b5132 | 1152 | { |
a50b1753 | 1153 | FILE *f = (FILE *) farg; |
252b5132 RH |
1154 | Elf_Internal_Phdr *p; |
1155 | asection *s; | |
1156 | bfd_byte *dynbuf = NULL; | |
1157 | ||
1158 | p = elf_tdata (abfd)->phdr; | |
1159 | if (p != NULL) | |
1160 | { | |
1161 | unsigned int i, c; | |
1162 | ||
1163 | fprintf (f, _("\nProgram Header:\n")); | |
1164 | c = elf_elfheader (abfd)->e_phnum; | |
1165 | for (i = 0; i < c; i++, p++) | |
1166 | { | |
cedc298e | 1167 | const char *pt = get_segment_type (p->p_type); |
252b5132 RH |
1168 | char buf[20]; |
1169 | ||
cedc298e | 1170 | if (pt == NULL) |
252b5132 | 1171 | { |
cedc298e L |
1172 | sprintf (buf, "0x%lx", p->p_type); |
1173 | pt = buf; | |
252b5132 | 1174 | } |
dc810e39 | 1175 | fprintf (f, "%8s off 0x", pt); |
60b89a18 | 1176 | bfd_fprintf_vma (abfd, f, p->p_offset); |
252b5132 | 1177 | fprintf (f, " vaddr 0x"); |
60b89a18 | 1178 | bfd_fprintf_vma (abfd, f, p->p_vaddr); |
252b5132 | 1179 | fprintf (f, " paddr 0x"); |
60b89a18 | 1180 | bfd_fprintf_vma (abfd, f, p->p_paddr); |
252b5132 RH |
1181 | fprintf (f, " align 2**%u\n", bfd_log2 (p->p_align)); |
1182 | fprintf (f, " filesz 0x"); | |
60b89a18 | 1183 | bfd_fprintf_vma (abfd, f, p->p_filesz); |
252b5132 | 1184 | fprintf (f, " memsz 0x"); |
60b89a18 | 1185 | bfd_fprintf_vma (abfd, f, p->p_memsz); |
252b5132 RH |
1186 | fprintf (f, " flags %c%c%c", |
1187 | (p->p_flags & PF_R) != 0 ? 'r' : '-', | |
1188 | (p->p_flags & PF_W) != 0 ? 'w' : '-', | |
1189 | (p->p_flags & PF_X) != 0 ? 'x' : '-'); | |
dc810e39 AM |
1190 | if ((p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X)) != 0) |
1191 | fprintf (f, " %lx", p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X)); | |
252b5132 RH |
1192 | fprintf (f, "\n"); |
1193 | } | |
1194 | } | |
1195 | ||
1196 | s = bfd_get_section_by_name (abfd, ".dynamic"); | |
1197 | if (s != NULL) | |
1198 | { | |
cb33740c | 1199 | unsigned int elfsec; |
dc810e39 | 1200 | unsigned long shlink; |
252b5132 RH |
1201 | bfd_byte *extdyn, *extdynend; |
1202 | size_t extdynsize; | |
217aa764 | 1203 | void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *); |
252b5132 RH |
1204 | |
1205 | fprintf (f, _("\nDynamic Section:\n")); | |
1206 | ||
eea6121a | 1207 | if (!bfd_malloc_and_get_section (abfd, s, &dynbuf)) |
252b5132 RH |
1208 | goto error_return; |
1209 | ||
1210 | elfsec = _bfd_elf_section_from_bfd_section (abfd, s); | |
cb33740c | 1211 | if (elfsec == SHN_BAD) |
252b5132 | 1212 | goto error_return; |
dc810e39 | 1213 | shlink = elf_elfsections (abfd)[elfsec]->sh_link; |
252b5132 RH |
1214 | |
1215 | extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn; | |
1216 | swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in; | |
1217 | ||
1218 | extdyn = dynbuf; | |
eea6121a | 1219 | extdynend = extdyn + s->size; |
252b5132 RH |
1220 | for (; extdyn < extdynend; extdyn += extdynsize) |
1221 | { | |
1222 | Elf_Internal_Dyn dyn; | |
ad9563d6 | 1223 | const char *name = ""; |
252b5132 | 1224 | char ab[20]; |
b34976b6 | 1225 | bfd_boolean stringp; |
ad9563d6 | 1226 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
252b5132 | 1227 | |
217aa764 | 1228 | (*swap_dyn_in) (abfd, extdyn, &dyn); |
252b5132 RH |
1229 | |
1230 | if (dyn.d_tag == DT_NULL) | |
1231 | break; | |
1232 | ||
b34976b6 | 1233 | stringp = FALSE; |
252b5132 RH |
1234 | switch (dyn.d_tag) |
1235 | { | |
1236 | default: | |
ad9563d6 CM |
1237 | if (bed->elf_backend_get_target_dtag) |
1238 | name = (*bed->elf_backend_get_target_dtag) (dyn.d_tag); | |
1239 | ||
1240 | if (!strcmp (name, "")) | |
1241 | { | |
1242 | sprintf (ab, "0x%lx", (unsigned long) dyn.d_tag); | |
1243 | name = ab; | |
1244 | } | |
252b5132 RH |
1245 | break; |
1246 | ||
b34976b6 | 1247 | case DT_NEEDED: name = "NEEDED"; stringp = TRUE; break; |
252b5132 RH |
1248 | case DT_PLTRELSZ: name = "PLTRELSZ"; break; |
1249 | case DT_PLTGOT: name = "PLTGOT"; break; | |
1250 | case DT_HASH: name = "HASH"; break; | |
1251 | case DT_STRTAB: name = "STRTAB"; break; | |
1252 | case DT_SYMTAB: name = "SYMTAB"; break; | |
1253 | case DT_RELA: name = "RELA"; break; | |
1254 | case DT_RELASZ: name = "RELASZ"; break; | |
1255 | case DT_RELAENT: name = "RELAENT"; break; | |
1256 | case DT_STRSZ: name = "STRSZ"; break; | |
1257 | case DT_SYMENT: name = "SYMENT"; break; | |
1258 | case DT_INIT: name = "INIT"; break; | |
1259 | case DT_FINI: name = "FINI"; break; | |
b34976b6 AM |
1260 | case DT_SONAME: name = "SONAME"; stringp = TRUE; break; |
1261 | case DT_RPATH: name = "RPATH"; stringp = TRUE; break; | |
252b5132 RH |
1262 | case DT_SYMBOLIC: name = "SYMBOLIC"; break; |
1263 | case DT_REL: name = "REL"; break; | |
1264 | case DT_RELSZ: name = "RELSZ"; break; | |
1265 | case DT_RELENT: name = "RELENT"; break; | |
1266 | case DT_PLTREL: name = "PLTREL"; break; | |
1267 | case DT_DEBUG: name = "DEBUG"; break; | |
1268 | case DT_TEXTREL: name = "TEXTREL"; break; | |
1269 | case DT_JMPREL: name = "JMPREL"; break; | |
94558834 L |
1270 | case DT_BIND_NOW: name = "BIND_NOW"; break; |
1271 | case DT_INIT_ARRAY: name = "INIT_ARRAY"; break; | |
1272 | case DT_FINI_ARRAY: name = "FINI_ARRAY"; break; | |
1273 | case DT_INIT_ARRAYSZ: name = "INIT_ARRAYSZ"; break; | |
1274 | case DT_FINI_ARRAYSZ: name = "FINI_ARRAYSZ"; break; | |
b34976b6 | 1275 | case DT_RUNPATH: name = "RUNPATH"; stringp = TRUE; break; |
94558834 L |
1276 | case DT_FLAGS: name = "FLAGS"; break; |
1277 | case DT_PREINIT_ARRAY: name = "PREINIT_ARRAY"; break; | |
1278 | case DT_PREINIT_ARRAYSZ: name = "PREINIT_ARRAYSZ"; break; | |
d48188b9 | 1279 | case DT_CHECKSUM: name = "CHECKSUM"; break; |
94558834 L |
1280 | case DT_PLTPADSZ: name = "PLTPADSZ"; break; |
1281 | case DT_MOVEENT: name = "MOVEENT"; break; | |
1282 | case DT_MOVESZ: name = "MOVESZ"; break; | |
1283 | case DT_FEATURE: name = "FEATURE"; break; | |
1284 | case DT_POSFLAG_1: name = "POSFLAG_1"; break; | |
1285 | case DT_SYMINSZ: name = "SYMINSZ"; break; | |
1286 | case DT_SYMINENT: name = "SYMINENT"; break; | |
b34976b6 AM |
1287 | case DT_CONFIG: name = "CONFIG"; stringp = TRUE; break; |
1288 | case DT_DEPAUDIT: name = "DEPAUDIT"; stringp = TRUE; break; | |
1289 | case DT_AUDIT: name = "AUDIT"; stringp = TRUE; break; | |
94558834 L |
1290 | case DT_PLTPAD: name = "PLTPAD"; break; |
1291 | case DT_MOVETAB: name = "MOVETAB"; break; | |
1292 | case DT_SYMINFO: name = "SYMINFO"; break; | |
1293 | case DT_RELACOUNT: name = "RELACOUNT"; break; | |
1294 | case DT_RELCOUNT: name = "RELCOUNT"; break; | |
1295 | case DT_FLAGS_1: name = "FLAGS_1"; break; | |
252b5132 RH |
1296 | case DT_VERSYM: name = "VERSYM"; break; |
1297 | case DT_VERDEF: name = "VERDEF"; break; | |
1298 | case DT_VERDEFNUM: name = "VERDEFNUM"; break; | |
1299 | case DT_VERNEED: name = "VERNEED"; break; | |
1300 | case DT_VERNEEDNUM: name = "VERNEEDNUM"; break; | |
b34976b6 | 1301 | case DT_AUXILIARY: name = "AUXILIARY"; stringp = TRUE; break; |
94558834 | 1302 | case DT_USED: name = "USED"; break; |
b34976b6 | 1303 | case DT_FILTER: name = "FILTER"; stringp = TRUE; break; |
fdc90cb4 | 1304 | case DT_GNU_HASH: name = "GNU_HASH"; break; |
252b5132 RH |
1305 | } |
1306 | ||
ad9563d6 | 1307 | fprintf (f, " %-20s ", name); |
252b5132 | 1308 | if (! stringp) |
a1f3c56e AN |
1309 | { |
1310 | fprintf (f, "0x"); | |
1311 | bfd_fprintf_vma (abfd, f, dyn.d_un.d_val); | |
1312 | } | |
252b5132 RH |
1313 | else |
1314 | { | |
1315 | const char *string; | |
dc810e39 | 1316 | unsigned int tagv = dyn.d_un.d_val; |
252b5132 | 1317 | |
dc810e39 | 1318 | string = bfd_elf_string_from_elf_section (abfd, shlink, tagv); |
252b5132 RH |
1319 | if (string == NULL) |
1320 | goto error_return; | |
1321 | fprintf (f, "%s", string); | |
1322 | } | |
1323 | fprintf (f, "\n"); | |
1324 | } | |
1325 | ||
1326 | free (dynbuf); | |
1327 | dynbuf = NULL; | |
1328 | } | |
1329 | ||
1330 | if ((elf_dynverdef (abfd) != 0 && elf_tdata (abfd)->verdef == NULL) | |
1331 | || (elf_dynverref (abfd) != 0 && elf_tdata (abfd)->verref == NULL)) | |
1332 | { | |
fc0e6df6 | 1333 | if (! _bfd_elf_slurp_version_tables (abfd, FALSE)) |
b34976b6 | 1334 | return FALSE; |
252b5132 RH |
1335 | } |
1336 | ||
1337 | if (elf_dynverdef (abfd) != 0) | |
1338 | { | |
1339 | Elf_Internal_Verdef *t; | |
1340 | ||
1341 | fprintf (f, _("\nVersion definitions:\n")); | |
1342 | for (t = elf_tdata (abfd)->verdef; t != NULL; t = t->vd_nextdef) | |
1343 | { | |
1344 | fprintf (f, "%d 0x%2.2x 0x%8.8lx %s\n", t->vd_ndx, | |
d0fb9a8d JJ |
1345 | t->vd_flags, t->vd_hash, |
1346 | t->vd_nodename ? t->vd_nodename : "<corrupt>"); | |
1347 | if (t->vd_auxptr != NULL && t->vd_auxptr->vda_nextptr != NULL) | |
252b5132 RH |
1348 | { |
1349 | Elf_Internal_Verdaux *a; | |
1350 | ||
1351 | fprintf (f, "\t"); | |
1352 | for (a = t->vd_auxptr->vda_nextptr; | |
1353 | a != NULL; | |
1354 | a = a->vda_nextptr) | |
d0fb9a8d JJ |
1355 | fprintf (f, "%s ", |
1356 | a->vda_nodename ? a->vda_nodename : "<corrupt>"); | |
252b5132 RH |
1357 | fprintf (f, "\n"); |
1358 | } | |
1359 | } | |
1360 | } | |
1361 | ||
1362 | if (elf_dynverref (abfd) != 0) | |
1363 | { | |
1364 | Elf_Internal_Verneed *t; | |
1365 | ||
1366 | fprintf (f, _("\nVersion References:\n")); | |
1367 | for (t = elf_tdata (abfd)->verref; t != NULL; t = t->vn_nextref) | |
1368 | { | |
1369 | Elf_Internal_Vernaux *a; | |
1370 | ||
d0fb9a8d JJ |
1371 | fprintf (f, _(" required from %s:\n"), |
1372 | t->vn_filename ? t->vn_filename : "<corrupt>"); | |
252b5132 RH |
1373 | for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) |
1374 | fprintf (f, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a->vna_hash, | |
d0fb9a8d JJ |
1375 | a->vna_flags, a->vna_other, |
1376 | a->vna_nodename ? a->vna_nodename : "<corrupt>"); | |
252b5132 RH |
1377 | } |
1378 | } | |
1379 | ||
b34976b6 | 1380 | return TRUE; |
252b5132 RH |
1381 | |
1382 | error_return: | |
1383 | if (dynbuf != NULL) | |
1384 | free (dynbuf); | |
b34976b6 | 1385 | return FALSE; |
252b5132 RH |
1386 | } |
1387 | ||
1388 | /* Display ELF-specific fields of a symbol. */ | |
1389 | ||
1390 | void | |
217aa764 AM |
1391 | bfd_elf_print_symbol (bfd *abfd, |
1392 | void *filep, | |
1393 | asymbol *symbol, | |
1394 | bfd_print_symbol_type how) | |
252b5132 | 1395 | { |
a50b1753 | 1396 | FILE *file = (FILE *) filep; |
252b5132 RH |
1397 | switch (how) |
1398 | { | |
1399 | case bfd_print_symbol_name: | |
1400 | fprintf (file, "%s", symbol->name); | |
1401 | break; | |
1402 | case bfd_print_symbol_more: | |
1403 | fprintf (file, "elf "); | |
60b89a18 | 1404 | bfd_fprintf_vma (abfd, file, symbol->value); |
0af1713e | 1405 | fprintf (file, " %lx", (unsigned long) symbol->flags); |
252b5132 RH |
1406 | break; |
1407 | case bfd_print_symbol_all: | |
1408 | { | |
4e8a9624 AM |
1409 | const char *section_name; |
1410 | const char *name = NULL; | |
9c5bfbb7 | 1411 | const struct elf_backend_data *bed; |
7a13edea | 1412 | unsigned char st_other; |
dbb410c3 | 1413 | bfd_vma val; |
c044fabd | 1414 | |
252b5132 | 1415 | section_name = symbol->section ? symbol->section->name : "(*none*)"; |
587ff49e RH |
1416 | |
1417 | bed = get_elf_backend_data (abfd); | |
1418 | if (bed->elf_backend_print_symbol_all) | |
c044fabd | 1419 | name = (*bed->elf_backend_print_symbol_all) (abfd, filep, symbol); |
587ff49e RH |
1420 | |
1421 | if (name == NULL) | |
1422 | { | |
7ee38065 | 1423 | name = symbol->name; |
217aa764 | 1424 | bfd_print_symbol_vandf (abfd, file, symbol); |
587ff49e RH |
1425 | } |
1426 | ||
252b5132 RH |
1427 | fprintf (file, " %s\t", section_name); |
1428 | /* Print the "other" value for a symbol. For common symbols, | |
1429 | we've already printed the size; now print the alignment. | |
1430 | For other symbols, we have no specified alignment, and | |
1431 | we've printed the address; now print the size. */ | |
dcf6c779 | 1432 | if (symbol->section && bfd_is_com_section (symbol->section)) |
dbb410c3 AM |
1433 | val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_value; |
1434 | else | |
1435 | val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_size; | |
1436 | bfd_fprintf_vma (abfd, file, val); | |
252b5132 RH |
1437 | |
1438 | /* If we have version information, print it. */ | |
1439 | if (elf_tdata (abfd)->dynversym_section != 0 | |
1440 | && (elf_tdata (abfd)->dynverdef_section != 0 | |
1441 | || elf_tdata (abfd)->dynverref_section != 0)) | |
1442 | { | |
1443 | unsigned int vernum; | |
1444 | const char *version_string; | |
1445 | ||
1446 | vernum = ((elf_symbol_type *) symbol)->version & VERSYM_VERSION; | |
1447 | ||
1448 | if (vernum == 0) | |
1449 | version_string = ""; | |
1450 | else if (vernum == 1) | |
1451 | version_string = "Base"; | |
1452 | else if (vernum <= elf_tdata (abfd)->cverdefs) | |
1453 | version_string = | |
1454 | elf_tdata (abfd)->verdef[vernum - 1].vd_nodename; | |
1455 | else | |
1456 | { | |
1457 | Elf_Internal_Verneed *t; | |
1458 | ||
1459 | version_string = ""; | |
1460 | for (t = elf_tdata (abfd)->verref; | |
1461 | t != NULL; | |
1462 | t = t->vn_nextref) | |
1463 | { | |
1464 | Elf_Internal_Vernaux *a; | |
1465 | ||
1466 | for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) | |
1467 | { | |
1468 | if (a->vna_other == vernum) | |
1469 | { | |
1470 | version_string = a->vna_nodename; | |
1471 | break; | |
1472 | } | |
1473 | } | |
1474 | } | |
1475 | } | |
1476 | ||
1477 | if ((((elf_symbol_type *) symbol)->version & VERSYM_HIDDEN) == 0) | |
1478 | fprintf (file, " %-11s", version_string); | |
1479 | else | |
1480 | { | |
1481 | int i; | |
1482 | ||
1483 | fprintf (file, " (%s)", version_string); | |
1484 | for (i = 10 - strlen (version_string); i > 0; --i) | |
1485 | putc (' ', file); | |
1486 | } | |
1487 | } | |
1488 | ||
1489 | /* If the st_other field is not zero, print it. */ | |
7a13edea | 1490 | st_other = ((elf_symbol_type *) symbol)->internal_elf_sym.st_other; |
c044fabd | 1491 | |
7a13edea NC |
1492 | switch (st_other) |
1493 | { | |
1494 | case 0: break; | |
1495 | case STV_INTERNAL: fprintf (file, " .internal"); break; | |
1496 | case STV_HIDDEN: fprintf (file, " .hidden"); break; | |
1497 | case STV_PROTECTED: fprintf (file, " .protected"); break; | |
1498 | default: | |
1499 | /* Some other non-defined flags are also present, so print | |
1500 | everything hex. */ | |
1501 | fprintf (file, " 0x%02x", (unsigned int) st_other); | |
1502 | } | |
252b5132 | 1503 | |
587ff49e | 1504 | fprintf (file, " %s", name); |
252b5132 RH |
1505 | } |
1506 | break; | |
1507 | } | |
1508 | } | |
252b5132 | 1509 | |
252b5132 RH |
1510 | /* Allocate an ELF string table--force the first byte to be zero. */ |
1511 | ||
1512 | struct bfd_strtab_hash * | |
217aa764 | 1513 | _bfd_elf_stringtab_init (void) |
252b5132 RH |
1514 | { |
1515 | struct bfd_strtab_hash *ret; | |
1516 | ||
1517 | ret = _bfd_stringtab_init (); | |
1518 | if (ret != NULL) | |
1519 | { | |
1520 | bfd_size_type loc; | |
1521 | ||
b34976b6 | 1522 | loc = _bfd_stringtab_add (ret, "", TRUE, FALSE); |
252b5132 RH |
1523 | BFD_ASSERT (loc == 0 || loc == (bfd_size_type) -1); |
1524 | if (loc == (bfd_size_type) -1) | |
1525 | { | |
1526 | _bfd_stringtab_free (ret); | |
1527 | ret = NULL; | |
1528 | } | |
1529 | } | |
1530 | return ret; | |
1531 | } | |
1532 | \f | |
1533 | /* ELF .o/exec file reading */ | |
1534 | ||
c044fabd | 1535 | /* Create a new bfd section from an ELF section header. */ |
252b5132 | 1536 | |
b34976b6 | 1537 | bfd_boolean |
217aa764 | 1538 | bfd_section_from_shdr (bfd *abfd, unsigned int shindex) |
252b5132 | 1539 | { |
4fbb74a6 AM |
1540 | Elf_Internal_Shdr *hdr; |
1541 | Elf_Internal_Ehdr *ehdr; | |
1542 | const struct elf_backend_data *bed; | |
90937f86 | 1543 | const char *name; |
252b5132 | 1544 | |
4fbb74a6 AM |
1545 | if (shindex >= elf_numsections (abfd)) |
1546 | return FALSE; | |
1547 | ||
1548 | hdr = elf_elfsections (abfd)[shindex]; | |
1549 | ehdr = elf_elfheader (abfd); | |
1550 | name = bfd_elf_string_from_elf_section (abfd, ehdr->e_shstrndx, | |
1b3a8575 | 1551 | hdr->sh_name); |
933d961a JJ |
1552 | if (name == NULL) |
1553 | return FALSE; | |
252b5132 | 1554 | |
4fbb74a6 | 1555 | bed = get_elf_backend_data (abfd); |
252b5132 RH |
1556 | switch (hdr->sh_type) |
1557 | { | |
1558 | case SHT_NULL: | |
1559 | /* Inactive section. Throw it away. */ | |
b34976b6 | 1560 | return TRUE; |
252b5132 RH |
1561 | |
1562 | case SHT_PROGBITS: /* Normal section with contents. */ | |
252b5132 RH |
1563 | case SHT_NOBITS: /* .bss section. */ |
1564 | case SHT_HASH: /* .hash section. */ | |
1565 | case SHT_NOTE: /* .note section. */ | |
25e27870 L |
1566 | case SHT_INIT_ARRAY: /* .init_array section. */ |
1567 | case SHT_FINI_ARRAY: /* .fini_array section. */ | |
1568 | case SHT_PREINIT_ARRAY: /* .preinit_array section. */ | |
7f1204bb | 1569 | case SHT_GNU_LIBLIST: /* .gnu.liblist section. */ |
fdc90cb4 | 1570 | case SHT_GNU_HASH: /* .gnu.hash section. */ |
6dc132d9 | 1571 | return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); |
252b5132 | 1572 | |
797fc050 | 1573 | case SHT_DYNAMIC: /* Dynamic linking information. */ |
6dc132d9 | 1574 | if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex)) |
b34976b6 | 1575 | return FALSE; |
cfcac11d NC |
1576 | if (hdr->sh_link > elf_numsections (abfd)) |
1577 | { | |
caa83f8b | 1578 | /* PR 10478: Accept Solaris binaries with a sh_link |
cfcac11d NC |
1579 | field set to SHN_BEFORE or SHN_AFTER. */ |
1580 | switch (bfd_get_arch (abfd)) | |
1581 | { | |
caa83f8b | 1582 | case bfd_arch_i386: |
cfcac11d NC |
1583 | case bfd_arch_sparc: |
1584 | if (hdr->sh_link == (SHN_LORESERVE & 0xffff) /* SHN_BEFORE */ | |
1585 | || hdr->sh_link == ((SHN_LORESERVE + 1) & 0xffff) /* SHN_AFTER */) | |
1586 | break; | |
1587 | /* Otherwise fall through. */ | |
1588 | default: | |
1589 | return FALSE; | |
1590 | } | |
1591 | } | |
1592 | else if (elf_elfsections (abfd)[hdr->sh_link] == NULL) | |
8e0ed13f | 1593 | return FALSE; |
cfcac11d | 1594 | else if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_STRTAB) |
797fc050 AM |
1595 | { |
1596 | Elf_Internal_Shdr *dynsymhdr; | |
1597 | ||
1598 | /* The shared libraries distributed with hpux11 have a bogus | |
1599 | sh_link field for the ".dynamic" section. Find the | |
1600 | string table for the ".dynsym" section instead. */ | |
1601 | if (elf_dynsymtab (abfd) != 0) | |
1602 | { | |
1603 | dynsymhdr = elf_elfsections (abfd)[elf_dynsymtab (abfd)]; | |
1604 | hdr->sh_link = dynsymhdr->sh_link; | |
1605 | } | |
1606 | else | |
1607 | { | |
1608 | unsigned int i, num_sec; | |
1609 | ||
1610 | num_sec = elf_numsections (abfd); | |
1611 | for (i = 1; i < num_sec; i++) | |
1612 | { | |
1613 | dynsymhdr = elf_elfsections (abfd)[i]; | |
1614 | if (dynsymhdr->sh_type == SHT_DYNSYM) | |
1615 | { | |
1616 | hdr->sh_link = dynsymhdr->sh_link; | |
1617 | break; | |
1618 | } | |
1619 | } | |
1620 | } | |
1621 | } | |
1622 | break; | |
1623 | ||
252b5132 RH |
1624 | case SHT_SYMTAB: /* A symbol table */ |
1625 | if (elf_onesymtab (abfd) == shindex) | |
b34976b6 | 1626 | return TRUE; |
252b5132 | 1627 | |
a50b2160 JJ |
1628 | if (hdr->sh_entsize != bed->s->sizeof_sym) |
1629 | return FALSE; | |
3337c1e5 | 1630 | if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size) |
eee3b786 AM |
1631 | { |
1632 | if (hdr->sh_size != 0) | |
1633 | return FALSE; | |
1634 | /* Some assemblers erroneously set sh_info to one with a | |
1635 | zero sh_size. ld sees this as a global symbol count | |
1636 | of (unsigned) -1. Fix it here. */ | |
1637 | hdr->sh_info = 0; | |
1638 | return TRUE; | |
1639 | } | |
252b5132 RH |
1640 | BFD_ASSERT (elf_onesymtab (abfd) == 0); |
1641 | elf_onesymtab (abfd) = shindex; | |
1642 | elf_tdata (abfd)->symtab_hdr = *hdr; | |
1643 | elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->symtab_hdr; | |
1644 | abfd->flags |= HAS_SYMS; | |
1645 | ||
1646 | /* Sometimes a shared object will map in the symbol table. If | |
08a40648 AM |
1647 | SHF_ALLOC is set, and this is a shared object, then we also |
1648 | treat this section as a BFD section. We can not base the | |
1649 | decision purely on SHF_ALLOC, because that flag is sometimes | |
1650 | set in a relocatable object file, which would confuse the | |
1651 | linker. */ | |
252b5132 RH |
1652 | if ((hdr->sh_flags & SHF_ALLOC) != 0 |
1653 | && (abfd->flags & DYNAMIC) != 0 | |
6dc132d9 L |
1654 | && ! _bfd_elf_make_section_from_shdr (abfd, hdr, name, |
1655 | shindex)) | |
b34976b6 | 1656 | return FALSE; |
252b5132 | 1657 | |
1b3a8575 AM |
1658 | /* Go looking for SHT_SYMTAB_SHNDX too, since if there is one we |
1659 | can't read symbols without that section loaded as well. It | |
1660 | is most likely specified by the next section header. */ | |
1661 | if (elf_elfsections (abfd)[elf_symtab_shndx (abfd)]->sh_link != shindex) | |
1662 | { | |
1663 | unsigned int i, num_sec; | |
1664 | ||
1665 | num_sec = elf_numsections (abfd); | |
1666 | for (i = shindex + 1; i < num_sec; i++) | |
1667 | { | |
1668 | Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i]; | |
1669 | if (hdr2->sh_type == SHT_SYMTAB_SHNDX | |
1670 | && hdr2->sh_link == shindex) | |
1671 | break; | |
1672 | } | |
1673 | if (i == num_sec) | |
1674 | for (i = 1; i < shindex; i++) | |
1675 | { | |
1676 | Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i]; | |
1677 | if (hdr2->sh_type == SHT_SYMTAB_SHNDX | |
1678 | && hdr2->sh_link == shindex) | |
1679 | break; | |
1680 | } | |
1681 | if (i != shindex) | |
1682 | return bfd_section_from_shdr (abfd, i); | |
1683 | } | |
b34976b6 | 1684 | return TRUE; |
252b5132 RH |
1685 | |
1686 | case SHT_DYNSYM: /* A dynamic symbol table */ | |
1687 | if (elf_dynsymtab (abfd) == shindex) | |
b34976b6 | 1688 | return TRUE; |
252b5132 | 1689 | |
a50b2160 JJ |
1690 | if (hdr->sh_entsize != bed->s->sizeof_sym) |
1691 | return FALSE; | |
eee3b786 AM |
1692 | if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size) |
1693 | { | |
1694 | if (hdr->sh_size != 0) | |
1695 | return FALSE; | |
1696 | /* Some linkers erroneously set sh_info to one with a | |
1697 | zero sh_size. ld sees this as a global symbol count | |
1698 | of (unsigned) -1. Fix it here. */ | |
1699 | hdr->sh_info = 0; | |
1700 | return TRUE; | |
1701 | } | |
252b5132 RH |
1702 | BFD_ASSERT (elf_dynsymtab (abfd) == 0); |
1703 | elf_dynsymtab (abfd) = shindex; | |
1704 | elf_tdata (abfd)->dynsymtab_hdr = *hdr; | |
1705 | elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->dynsymtab_hdr; | |
1706 | abfd->flags |= HAS_SYMS; | |
1707 | ||
1708 | /* Besides being a symbol table, we also treat this as a regular | |
1709 | section, so that objcopy can handle it. */ | |
6dc132d9 | 1710 | return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); |
252b5132 | 1711 | |
9ad5cbcf AM |
1712 | case SHT_SYMTAB_SHNDX: /* Symbol section indices when >64k sections */ |
1713 | if (elf_symtab_shndx (abfd) == shindex) | |
b34976b6 | 1714 | return TRUE; |
9ad5cbcf | 1715 | |
1b3a8575 | 1716 | BFD_ASSERT (elf_symtab_shndx (abfd) == 0); |
9ad5cbcf AM |
1717 | elf_symtab_shndx (abfd) = shindex; |
1718 | elf_tdata (abfd)->symtab_shndx_hdr = *hdr; | |
1719 | elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->symtab_shndx_hdr; | |
b34976b6 | 1720 | return TRUE; |
9ad5cbcf | 1721 | |
252b5132 RH |
1722 | case SHT_STRTAB: /* A string table */ |
1723 | if (hdr->bfd_section != NULL) | |
b34976b6 | 1724 | return TRUE; |
252b5132 RH |
1725 | if (ehdr->e_shstrndx == shindex) |
1726 | { | |
1727 | elf_tdata (abfd)->shstrtab_hdr = *hdr; | |
1728 | elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->shstrtab_hdr; | |
b34976b6 | 1729 | return TRUE; |
252b5132 | 1730 | } |
1b3a8575 AM |
1731 | if (elf_elfsections (abfd)[elf_onesymtab (abfd)]->sh_link == shindex) |
1732 | { | |
1733 | symtab_strtab: | |
1734 | elf_tdata (abfd)->strtab_hdr = *hdr; | |
1735 | elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->strtab_hdr; | |
1736 | return TRUE; | |
1737 | } | |
1738 | if (elf_elfsections (abfd)[elf_dynsymtab (abfd)]->sh_link == shindex) | |
1739 | { | |
1740 | dynsymtab_strtab: | |
1741 | elf_tdata (abfd)->dynstrtab_hdr = *hdr; | |
1742 | hdr = &elf_tdata (abfd)->dynstrtab_hdr; | |
1743 | elf_elfsections (abfd)[shindex] = hdr; | |
1744 | /* We also treat this as a regular section, so that objcopy | |
1745 | can handle it. */ | |
6dc132d9 L |
1746 | return _bfd_elf_make_section_from_shdr (abfd, hdr, name, |
1747 | shindex); | |
1b3a8575 | 1748 | } |
252b5132 | 1749 | |
1b3a8575 AM |
1750 | /* If the string table isn't one of the above, then treat it as a |
1751 | regular section. We need to scan all the headers to be sure, | |
1752 | just in case this strtab section appeared before the above. */ | |
1753 | if (elf_onesymtab (abfd) == 0 || elf_dynsymtab (abfd) == 0) | |
1754 | { | |
1755 | unsigned int i, num_sec; | |
252b5132 | 1756 | |
1b3a8575 AM |
1757 | num_sec = elf_numsections (abfd); |
1758 | for (i = 1; i < num_sec; i++) | |
1759 | { | |
1760 | Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i]; | |
1761 | if (hdr2->sh_link == shindex) | |
1762 | { | |
933d961a JJ |
1763 | /* Prevent endless recursion on broken objects. */ |
1764 | if (i == shindex) | |
1765 | return FALSE; | |
1b3a8575 AM |
1766 | if (! bfd_section_from_shdr (abfd, i)) |
1767 | return FALSE; | |
1768 | if (elf_onesymtab (abfd) == i) | |
1769 | goto symtab_strtab; | |
1770 | if (elf_dynsymtab (abfd) == i) | |
1771 | goto dynsymtab_strtab; | |
1772 | } | |
1773 | } | |
1774 | } | |
6dc132d9 | 1775 | return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); |
252b5132 RH |
1776 | |
1777 | case SHT_REL: | |
1778 | case SHT_RELA: | |
1779 | /* *These* do a lot of work -- but build no sections! */ | |
1780 | { | |
1781 | asection *target_sect; | |
d4730f92 | 1782 | Elf_Internal_Shdr *hdr2, **p_hdr; |
9ad5cbcf | 1783 | unsigned int num_sec = elf_numsections (abfd); |
d4730f92 BS |
1784 | struct bfd_elf_section_data *esdt; |
1785 | bfd_size_type amt; | |
252b5132 | 1786 | |
aa2ca951 JJ |
1787 | if (hdr->sh_entsize |
1788 | != (bfd_size_type) (hdr->sh_type == SHT_REL | |
a50b2160 JJ |
1789 | ? bed->s->sizeof_rel : bed->s->sizeof_rela)) |
1790 | return FALSE; | |
1791 | ||
03ae5f59 | 1792 | /* Check for a bogus link to avoid crashing. */ |
4fbb74a6 | 1793 | if (hdr->sh_link >= num_sec) |
03ae5f59 ILT |
1794 | { |
1795 | ((*_bfd_error_handler) | |
d003868e AM |
1796 | (_("%B: invalid link %lu for reloc section %s (index %u)"), |
1797 | abfd, hdr->sh_link, name, shindex)); | |
6dc132d9 L |
1798 | return _bfd_elf_make_section_from_shdr (abfd, hdr, name, |
1799 | shindex); | |
03ae5f59 ILT |
1800 | } |
1801 | ||
252b5132 RH |
1802 | /* For some incomprehensible reason Oracle distributes |
1803 | libraries for Solaris in which some of the objects have | |
1804 | bogus sh_link fields. It would be nice if we could just | |
1805 | reject them, but, unfortunately, some people need to use | |
1806 | them. We scan through the section headers; if we find only | |
1807 | one suitable symbol table, we clobber the sh_link to point | |
83b89087 L |
1808 | to it. I hope this doesn't break anything. |
1809 | ||
1810 | Don't do it on executable nor shared library. */ | |
1811 | if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0 | |
1812 | && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_SYMTAB | |
252b5132 RH |
1813 | && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_DYNSYM) |
1814 | { | |
9ad5cbcf | 1815 | unsigned int scan; |
252b5132 RH |
1816 | int found; |
1817 | ||
1818 | found = 0; | |
9ad5cbcf | 1819 | for (scan = 1; scan < num_sec; scan++) |
252b5132 RH |
1820 | { |
1821 | if (elf_elfsections (abfd)[scan]->sh_type == SHT_SYMTAB | |
1822 | || elf_elfsections (abfd)[scan]->sh_type == SHT_DYNSYM) | |
1823 | { | |
1824 | if (found != 0) | |
1825 | { | |
1826 | found = 0; | |
1827 | break; | |
1828 | } | |
1829 | found = scan; | |
1830 | } | |
1831 | } | |
1832 | if (found != 0) | |
1833 | hdr->sh_link = found; | |
1834 | } | |
1835 | ||
1836 | /* Get the symbol table. */ | |
1b3a8575 AM |
1837 | if ((elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_SYMTAB |
1838 | || elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_DYNSYM) | |
252b5132 | 1839 | && ! bfd_section_from_shdr (abfd, hdr->sh_link)) |
b34976b6 | 1840 | return FALSE; |
252b5132 RH |
1841 | |
1842 | /* If this reloc section does not use the main symbol table we | |
1843 | don't treat it as a reloc section. BFD can't adequately | |
1844 | represent such a section, so at least for now, we don't | |
c044fabd | 1845 | try. We just present it as a normal section. We also |
60bcf0fa | 1846 | can't use it as a reloc section if it points to the null |
83b89087 L |
1847 | section, an invalid section, another reloc section, or its |
1848 | sh_link points to the null section. */ | |
185ef66d | 1849 | if (hdr->sh_link != elf_onesymtab (abfd) |
83b89087 | 1850 | || hdr->sh_link == SHN_UNDEF |
185ef66d | 1851 | || hdr->sh_info == SHN_UNDEF |
185ef66d AM |
1852 | || hdr->sh_info >= num_sec |
1853 | || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_REL | |
1854 | || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_RELA) | |
6dc132d9 L |
1855 | return _bfd_elf_make_section_from_shdr (abfd, hdr, name, |
1856 | shindex); | |
252b5132 RH |
1857 | |
1858 | if (! bfd_section_from_shdr (abfd, hdr->sh_info)) | |
b34976b6 | 1859 | return FALSE; |
252b5132 RH |
1860 | target_sect = bfd_section_from_elf_index (abfd, hdr->sh_info); |
1861 | if (target_sect == NULL) | |
b34976b6 | 1862 | return FALSE; |
252b5132 | 1863 | |
d4730f92 BS |
1864 | esdt = elf_section_data (target_sect); |
1865 | if (hdr->sh_type == SHT_RELA) | |
1866 | p_hdr = &esdt->rela.hdr; | |
252b5132 | 1867 | else |
d4730f92 BS |
1868 | p_hdr = &esdt->rel.hdr; |
1869 | ||
1870 | BFD_ASSERT (*p_hdr == NULL); | |
1871 | amt = sizeof (*hdr2); | |
1872 | hdr2 = (Elf_Internal_Shdr *) bfd_alloc (abfd, amt); | |
1873 | if (hdr2 == NULL) | |
1874 | return FALSE; | |
252b5132 | 1875 | *hdr2 = *hdr; |
d4730f92 | 1876 | *p_hdr = hdr2; |
252b5132 | 1877 | elf_elfsections (abfd)[shindex] = hdr2; |
d9bc7a44 | 1878 | target_sect->reloc_count += NUM_SHDR_ENTRIES (hdr); |
252b5132 RH |
1879 | target_sect->flags |= SEC_RELOC; |
1880 | target_sect->relocation = NULL; | |
1881 | target_sect->rel_filepos = hdr->sh_offset; | |
bf572ba0 MM |
1882 | /* In the section to which the relocations apply, mark whether |
1883 | its relocations are of the REL or RELA variety. */ | |
72730e0c | 1884 | if (hdr->sh_size != 0) |
d4730f92 BS |
1885 | { |
1886 | if (hdr->sh_type == SHT_RELA) | |
1887 | target_sect->use_rela_p = 1; | |
1888 | } | |
252b5132 | 1889 | abfd->flags |= HAS_RELOC; |
b34976b6 | 1890 | return TRUE; |
252b5132 | 1891 | } |
252b5132 RH |
1892 | |
1893 | case SHT_GNU_verdef: | |
1894 | elf_dynverdef (abfd) = shindex; | |
1895 | elf_tdata (abfd)->dynverdef_hdr = *hdr; | |
6dc132d9 | 1896 | return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); |
252b5132 RH |
1897 | |
1898 | case SHT_GNU_versym: | |
a50b2160 JJ |
1899 | if (hdr->sh_entsize != sizeof (Elf_External_Versym)) |
1900 | return FALSE; | |
252b5132 RH |
1901 | elf_dynversym (abfd) = shindex; |
1902 | elf_tdata (abfd)->dynversym_hdr = *hdr; | |
6dc132d9 | 1903 | return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); |
252b5132 RH |
1904 | |
1905 | case SHT_GNU_verneed: | |
1906 | elf_dynverref (abfd) = shindex; | |
1907 | elf_tdata (abfd)->dynverref_hdr = *hdr; | |
6dc132d9 | 1908 | return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); |
252b5132 RH |
1909 | |
1910 | case SHT_SHLIB: | |
b34976b6 | 1911 | return TRUE; |
252b5132 | 1912 | |
dbb410c3 | 1913 | case SHT_GROUP: |
44534af3 | 1914 | if (! IS_VALID_GROUP_SECTION_HEADER (hdr, GRP_ENTRY_SIZE)) |
a50b2160 | 1915 | return FALSE; |
6dc132d9 | 1916 | if (!_bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex)) |
b34976b6 | 1917 | return FALSE; |
dbb410c3 AM |
1918 | if (hdr->contents != NULL) |
1919 | { | |
1920 | Elf_Internal_Group *idx = (Elf_Internal_Group *) hdr->contents; | |
1783205a | 1921 | unsigned int n_elt = hdr->sh_size / GRP_ENTRY_SIZE; |
dbb410c3 AM |
1922 | asection *s; |
1923 | ||
b885599b AM |
1924 | if (idx->flags & GRP_COMDAT) |
1925 | hdr->bfd_section->flags | |
1926 | |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD; | |
1927 | ||
45c5e9ed L |
1928 | /* We try to keep the same section order as it comes in. */ |
1929 | idx += n_elt; | |
dbb410c3 | 1930 | while (--n_elt != 0) |
1783205a NC |
1931 | { |
1932 | --idx; | |
1933 | ||
1934 | if (idx->shdr != NULL | |
1935 | && (s = idx->shdr->bfd_section) != NULL | |
1936 | && elf_next_in_group (s) != NULL) | |
1937 | { | |
1938 | elf_next_in_group (hdr->bfd_section) = s; | |
1939 | break; | |
1940 | } | |
1941 | } | |
dbb410c3 AM |
1942 | } |
1943 | break; | |
1944 | ||
252b5132 | 1945 | default: |
104d59d1 JM |
1946 | /* Possibly an attributes section. */ |
1947 | if (hdr->sh_type == SHT_GNU_ATTRIBUTES | |
1948 | || hdr->sh_type == bed->obj_attrs_section_type) | |
1949 | { | |
1950 | if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex)) | |
1951 | return FALSE; | |
1952 | _bfd_elf_parse_attributes (abfd, hdr); | |
1953 | return TRUE; | |
1954 | } | |
1955 | ||
252b5132 | 1956 | /* Check for any processor-specific section types. */ |
3eb70a79 L |
1957 | if (bed->elf_backend_section_from_shdr (abfd, hdr, name, shindex)) |
1958 | return TRUE; | |
1959 | ||
1960 | if (hdr->sh_type >= SHT_LOUSER && hdr->sh_type <= SHT_HIUSER) | |
1961 | { | |
1962 | if ((hdr->sh_flags & SHF_ALLOC) != 0) | |
1963 | /* FIXME: How to properly handle allocated section reserved | |
1964 | for applications? */ | |
1965 | (*_bfd_error_handler) | |
1966 | (_("%B: don't know how to handle allocated, application " | |
1967 | "specific section `%s' [0x%8x]"), | |
1968 | abfd, name, hdr->sh_type); | |
1969 | else | |
1970 | /* Allow sections reserved for applications. */ | |
1971 | return _bfd_elf_make_section_from_shdr (abfd, hdr, name, | |
1972 | shindex); | |
1973 | } | |
1974 | else if (hdr->sh_type >= SHT_LOPROC | |
1975 | && hdr->sh_type <= SHT_HIPROC) | |
1976 | /* FIXME: We should handle this section. */ | |
1977 | (*_bfd_error_handler) | |
1978 | (_("%B: don't know how to handle processor specific section " | |
1979 | "`%s' [0x%8x]"), | |
1980 | abfd, name, hdr->sh_type); | |
1981 | else if (hdr->sh_type >= SHT_LOOS && hdr->sh_type <= SHT_HIOS) | |
ff15b240 NC |
1982 | { |
1983 | /* Unrecognised OS-specific sections. */ | |
1984 | if ((hdr->sh_flags & SHF_OS_NONCONFORMING) != 0) | |
1985 | /* SHF_OS_NONCONFORMING indicates that special knowledge is | |
08a40648 | 1986 | required to correctly process the section and the file should |
ff15b240 NC |
1987 | be rejected with an error message. */ |
1988 | (*_bfd_error_handler) | |
1989 | (_("%B: don't know how to handle OS specific section " | |
1990 | "`%s' [0x%8x]"), | |
1991 | abfd, name, hdr->sh_type); | |
1992 | else | |
1993 | /* Otherwise it should be processed. */ | |
1994 | return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); | |
1995 | } | |
3eb70a79 L |
1996 | else |
1997 | /* FIXME: We should handle this section. */ | |
1998 | (*_bfd_error_handler) | |
1999 | (_("%B: don't know how to handle section `%s' [0x%8x]"), | |
2000 | abfd, name, hdr->sh_type); | |
2001 | ||
2002 | return FALSE; | |
252b5132 RH |
2003 | } |
2004 | ||
b34976b6 | 2005 | return TRUE; |
252b5132 RH |
2006 | } |
2007 | ||
87d72d41 | 2008 | /* Return the local symbol specified by ABFD, R_SYMNDX. */ |
ec338859 | 2009 | |
87d72d41 AM |
2010 | Elf_Internal_Sym * |
2011 | bfd_sym_from_r_symndx (struct sym_cache *cache, | |
2012 | bfd *abfd, | |
2013 | unsigned long r_symndx) | |
ec338859 | 2014 | { |
ec338859 AM |
2015 | unsigned int ent = r_symndx % LOCAL_SYM_CACHE_SIZE; |
2016 | ||
a5d1b3b5 AM |
2017 | if (cache->abfd != abfd || cache->indx[ent] != r_symndx) |
2018 | { | |
2019 | Elf_Internal_Shdr *symtab_hdr; | |
2020 | unsigned char esym[sizeof (Elf64_External_Sym)]; | |
2021 | Elf_External_Sym_Shndx eshndx; | |
ec338859 | 2022 | |
a5d1b3b5 AM |
2023 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; |
2024 | if (bfd_elf_get_elf_syms (abfd, symtab_hdr, 1, r_symndx, | |
87d72d41 | 2025 | &cache->sym[ent], esym, &eshndx) == NULL) |
a5d1b3b5 | 2026 | return NULL; |
9ad5cbcf | 2027 | |
a5d1b3b5 AM |
2028 | if (cache->abfd != abfd) |
2029 | { | |
2030 | memset (cache->indx, -1, sizeof (cache->indx)); | |
2031 | cache->abfd = abfd; | |
2032 | } | |
2033 | cache->indx[ent] = r_symndx; | |
ec338859 | 2034 | } |
a5d1b3b5 | 2035 | |
87d72d41 | 2036 | return &cache->sym[ent]; |
ec338859 AM |
2037 | } |
2038 | ||
252b5132 RH |
2039 | /* Given an ELF section number, retrieve the corresponding BFD |
2040 | section. */ | |
2041 | ||
2042 | asection * | |
91d6fa6a | 2043 | bfd_section_from_elf_index (bfd *abfd, unsigned int sec_index) |
252b5132 | 2044 | { |
91d6fa6a | 2045 | if (sec_index >= elf_numsections (abfd)) |
252b5132 | 2046 | return NULL; |
91d6fa6a | 2047 | return elf_elfsections (abfd)[sec_index]->bfd_section; |
252b5132 RH |
2048 | } |
2049 | ||
b35d266b | 2050 | static const struct bfd_elf_special_section special_sections_b[] = |
2f89ff8d | 2051 | { |
0112cd26 NC |
2052 | { STRING_COMMA_LEN (".bss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE }, |
2053 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2054 | }; |
2055 | ||
b35d266b | 2056 | static const struct bfd_elf_special_section special_sections_c[] = |
7f4d3958 | 2057 | { |
0112cd26 NC |
2058 | { STRING_COMMA_LEN (".comment"), 0, SHT_PROGBITS, 0 }, |
2059 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2060 | }; |
2061 | ||
b35d266b | 2062 | static const struct bfd_elf_special_section special_sections_d[] = |
7f4d3958 | 2063 | { |
0112cd26 NC |
2064 | { STRING_COMMA_LEN (".data"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE }, |
2065 | { STRING_COMMA_LEN (".data1"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE }, | |
a9a72a65 DE |
2066 | /* There are more DWARF sections than these, but they needn't be added here |
2067 | unless you have to cope with broken compilers that don't emit section | |
2068 | attributes or you want to help the user writing assembler. */ | |
0112cd26 NC |
2069 | { STRING_COMMA_LEN (".debug"), 0, SHT_PROGBITS, 0 }, |
2070 | { STRING_COMMA_LEN (".debug_line"), 0, SHT_PROGBITS, 0 }, | |
2071 | { STRING_COMMA_LEN (".debug_info"), 0, SHT_PROGBITS, 0 }, | |
2072 | { STRING_COMMA_LEN (".debug_abbrev"), 0, SHT_PROGBITS, 0 }, | |
2073 | { STRING_COMMA_LEN (".debug_aranges"), 0, SHT_PROGBITS, 0 }, | |
2074 | { STRING_COMMA_LEN (".dynamic"), 0, SHT_DYNAMIC, SHF_ALLOC }, | |
2075 | { STRING_COMMA_LEN (".dynstr"), 0, SHT_STRTAB, SHF_ALLOC }, | |
2076 | { STRING_COMMA_LEN (".dynsym"), 0, SHT_DYNSYM, SHF_ALLOC }, | |
2077 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2078 | }; |
2079 | ||
b35d266b | 2080 | static const struct bfd_elf_special_section special_sections_f[] = |
7f4d3958 | 2081 | { |
0112cd26 NC |
2082 | { STRING_COMMA_LEN (".fini"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR }, |
2083 | { STRING_COMMA_LEN (".fini_array"), 0, SHT_FINI_ARRAY, SHF_ALLOC + SHF_WRITE }, | |
2084 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2085 | }; |
2086 | ||
b35d266b | 2087 | static const struct bfd_elf_special_section special_sections_g[] = |
7f4d3958 | 2088 | { |
0112cd26 | 2089 | { STRING_COMMA_LEN (".gnu.linkonce.b"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE }, |
68efed41 | 2090 | { STRING_COMMA_LEN (".gnu.lto_"), -1, SHT_PROGBITS, SHF_EXCLUDE }, |
0112cd26 NC |
2091 | { STRING_COMMA_LEN (".got"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE }, |
2092 | { STRING_COMMA_LEN (".gnu.version"), 0, SHT_GNU_versym, 0 }, | |
2093 | { STRING_COMMA_LEN (".gnu.version_d"), 0, SHT_GNU_verdef, 0 }, | |
2094 | { STRING_COMMA_LEN (".gnu.version_r"), 0, SHT_GNU_verneed, 0 }, | |
2095 | { STRING_COMMA_LEN (".gnu.liblist"), 0, SHT_GNU_LIBLIST, SHF_ALLOC }, | |
2096 | { STRING_COMMA_LEN (".gnu.conflict"), 0, SHT_RELA, SHF_ALLOC }, | |
2097 | { STRING_COMMA_LEN (".gnu.hash"), 0, SHT_GNU_HASH, SHF_ALLOC }, | |
2098 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2099 | }; |
2100 | ||
b35d266b | 2101 | static const struct bfd_elf_special_section special_sections_h[] = |
7f4d3958 | 2102 | { |
0112cd26 NC |
2103 | { STRING_COMMA_LEN (".hash"), 0, SHT_HASH, SHF_ALLOC }, |
2104 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2105 | }; |
2106 | ||
b35d266b | 2107 | static const struct bfd_elf_special_section special_sections_i[] = |
7f4d3958 | 2108 | { |
0112cd26 NC |
2109 | { STRING_COMMA_LEN (".init"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR }, |
2110 | { STRING_COMMA_LEN (".init_array"), 0, SHT_INIT_ARRAY, SHF_ALLOC + SHF_WRITE }, | |
2111 | { STRING_COMMA_LEN (".interp"), 0, SHT_PROGBITS, 0 }, | |
2112 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2113 | }; |
2114 | ||
b35d266b | 2115 | static const struct bfd_elf_special_section special_sections_l[] = |
7f4d3958 | 2116 | { |
0112cd26 NC |
2117 | { STRING_COMMA_LEN (".line"), 0, SHT_PROGBITS, 0 }, |
2118 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2119 | }; |
2120 | ||
b35d266b | 2121 | static const struct bfd_elf_special_section special_sections_n[] = |
7f4d3958 | 2122 | { |
0112cd26 NC |
2123 | { STRING_COMMA_LEN (".note.GNU-stack"), 0, SHT_PROGBITS, 0 }, |
2124 | { STRING_COMMA_LEN (".note"), -1, SHT_NOTE, 0 }, | |
2125 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2126 | }; |
2127 | ||
b35d266b | 2128 | static const struct bfd_elf_special_section special_sections_p[] = |
7f4d3958 | 2129 | { |
0112cd26 NC |
2130 | { STRING_COMMA_LEN (".preinit_array"), 0, SHT_PREINIT_ARRAY, SHF_ALLOC + SHF_WRITE }, |
2131 | { STRING_COMMA_LEN (".plt"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR }, | |
2132 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2133 | }; |
2134 | ||
b35d266b | 2135 | static const struct bfd_elf_special_section special_sections_r[] = |
7f4d3958 | 2136 | { |
0112cd26 NC |
2137 | { STRING_COMMA_LEN (".rodata"), -2, SHT_PROGBITS, SHF_ALLOC }, |
2138 | { STRING_COMMA_LEN (".rodata1"), 0, SHT_PROGBITS, SHF_ALLOC }, | |
2139 | { STRING_COMMA_LEN (".rela"), -1, SHT_RELA, 0 }, | |
2140 | { STRING_COMMA_LEN (".rel"), -1, SHT_REL, 0 }, | |
2141 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2142 | }; |
2143 | ||
b35d266b | 2144 | static const struct bfd_elf_special_section special_sections_s[] = |
7f4d3958 | 2145 | { |
0112cd26 NC |
2146 | { STRING_COMMA_LEN (".shstrtab"), 0, SHT_STRTAB, 0 }, |
2147 | { STRING_COMMA_LEN (".strtab"), 0, SHT_STRTAB, 0 }, | |
2148 | { STRING_COMMA_LEN (".symtab"), 0, SHT_SYMTAB, 0 }, | |
60ff4dc4 HPN |
2149 | /* See struct bfd_elf_special_section declaration for the semantics of |
2150 | this special case where .prefix_length != strlen (.prefix). */ | |
2151 | { ".stabstr", 5, 3, SHT_STRTAB, 0 }, | |
0112cd26 | 2152 | { NULL, 0, 0, 0, 0 } |
2f89ff8d L |
2153 | }; |
2154 | ||
b35d266b | 2155 | static const struct bfd_elf_special_section special_sections_t[] = |
7f4d3958 | 2156 | { |
0112cd26 NC |
2157 | { STRING_COMMA_LEN (".text"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR }, |
2158 | { STRING_COMMA_LEN (".tbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS }, | |
2159 | { STRING_COMMA_LEN (".tdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS }, | |
2160 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2161 | }; |
2162 | ||
1b315056 CS |
2163 | static const struct bfd_elf_special_section special_sections_z[] = |
2164 | { | |
2165 | { STRING_COMMA_LEN (".zdebug_line"), 0, SHT_PROGBITS, 0 }, | |
2166 | { STRING_COMMA_LEN (".zdebug_info"), 0, SHT_PROGBITS, 0 }, | |
2167 | { STRING_COMMA_LEN (".zdebug_abbrev"), 0, SHT_PROGBITS, 0 }, | |
2168 | { STRING_COMMA_LEN (".zdebug_aranges"), 0, SHT_PROGBITS, 0 }, | |
2169 | { NULL, 0, 0, 0, 0 } | |
2170 | }; | |
2171 | ||
e4c93b56 | 2172 | static const struct bfd_elf_special_section * const special_sections[] = |
7f4d3958 | 2173 | { |
7f4d3958 | 2174 | special_sections_b, /* 'b' */ |
98ece1b3 | 2175 | special_sections_c, /* 'c' */ |
7f4d3958 L |
2176 | special_sections_d, /* 'd' */ |
2177 | NULL, /* 'e' */ | |
2178 | special_sections_f, /* 'f' */ | |
2179 | special_sections_g, /* 'g' */ | |
2180 | special_sections_h, /* 'h' */ | |
2181 | special_sections_i, /* 'i' */ | |
2182 | NULL, /* 'j' */ | |
2183 | NULL, /* 'k' */ | |
2184 | special_sections_l, /* 'l' */ | |
2185 | NULL, /* 'm' */ | |
2186 | special_sections_n, /* 'n' */ | |
2187 | NULL, /* 'o' */ | |
2188 | special_sections_p, /* 'p' */ | |
2189 | NULL, /* 'q' */ | |
2190 | special_sections_r, /* 'r' */ | |
2191 | special_sections_s, /* 's' */ | |
2192 | special_sections_t, /* 't' */ | |
1b315056 CS |
2193 | NULL, /* 'u' */ |
2194 | NULL, /* 'v' */ | |
2195 | NULL, /* 'w' */ | |
2196 | NULL, /* 'x' */ | |
2197 | NULL, /* 'y' */ | |
2198 | special_sections_z /* 'z' */ | |
7f4d3958 L |
2199 | }; |
2200 | ||
551b43fd AM |
2201 | const struct bfd_elf_special_section * |
2202 | _bfd_elf_get_special_section (const char *name, | |
2203 | const struct bfd_elf_special_section *spec, | |
2204 | unsigned int rela) | |
2f89ff8d L |
2205 | { |
2206 | int i; | |
7f4d3958 | 2207 | int len; |
7f4d3958 | 2208 | |
551b43fd | 2209 | len = strlen (name); |
7f4d3958 | 2210 | |
551b43fd | 2211 | for (i = 0; spec[i].prefix != NULL; i++) |
7dcb9820 AM |
2212 | { |
2213 | int suffix_len; | |
551b43fd | 2214 | int prefix_len = spec[i].prefix_length; |
7dcb9820 AM |
2215 | |
2216 | if (len < prefix_len) | |
2217 | continue; | |
551b43fd | 2218 | if (memcmp (name, spec[i].prefix, prefix_len) != 0) |
7dcb9820 AM |
2219 | continue; |
2220 | ||
551b43fd | 2221 | suffix_len = spec[i].suffix_length; |
7dcb9820 AM |
2222 | if (suffix_len <= 0) |
2223 | { | |
2224 | if (name[prefix_len] != 0) | |
2225 | { | |
2226 | if (suffix_len == 0) | |
2227 | continue; | |
2228 | if (name[prefix_len] != '.' | |
2229 | && (suffix_len == -2 | |
551b43fd | 2230 | || (rela && spec[i].type == SHT_REL))) |
7dcb9820 AM |
2231 | continue; |
2232 | } | |
2233 | } | |
2234 | else | |
2235 | { | |
2236 | if (len < prefix_len + suffix_len) | |
2237 | continue; | |
2238 | if (memcmp (name + len - suffix_len, | |
551b43fd | 2239 | spec[i].prefix + prefix_len, |
7dcb9820 AM |
2240 | suffix_len) != 0) |
2241 | continue; | |
2242 | } | |
551b43fd | 2243 | return &spec[i]; |
7dcb9820 | 2244 | } |
2f89ff8d L |
2245 | |
2246 | return NULL; | |
2247 | } | |
2248 | ||
7dcb9820 | 2249 | const struct bfd_elf_special_section * |
29ef7005 | 2250 | _bfd_elf_get_sec_type_attr (bfd *abfd, asection *sec) |
2f89ff8d | 2251 | { |
551b43fd AM |
2252 | int i; |
2253 | const struct bfd_elf_special_section *spec; | |
29ef7005 | 2254 | const struct elf_backend_data *bed; |
2f89ff8d L |
2255 | |
2256 | /* See if this is one of the special sections. */ | |
551b43fd AM |
2257 | if (sec->name == NULL) |
2258 | return NULL; | |
2f89ff8d | 2259 | |
29ef7005 L |
2260 | bed = get_elf_backend_data (abfd); |
2261 | spec = bed->special_sections; | |
2262 | if (spec) | |
2263 | { | |
2264 | spec = _bfd_elf_get_special_section (sec->name, | |
2265 | bed->special_sections, | |
2266 | sec->use_rela_p); | |
2267 | if (spec != NULL) | |
2268 | return spec; | |
2269 | } | |
2270 | ||
551b43fd AM |
2271 | if (sec->name[0] != '.') |
2272 | return NULL; | |
2f89ff8d | 2273 | |
551b43fd | 2274 | i = sec->name[1] - 'b'; |
1b315056 | 2275 | if (i < 0 || i > 'z' - 'b') |
551b43fd AM |
2276 | return NULL; |
2277 | ||
2278 | spec = special_sections[i]; | |
2f89ff8d | 2279 | |
551b43fd AM |
2280 | if (spec == NULL) |
2281 | return NULL; | |
2282 | ||
2283 | return _bfd_elf_get_special_section (sec->name, spec, sec->use_rela_p); | |
2f89ff8d L |
2284 | } |
2285 | ||
b34976b6 | 2286 | bfd_boolean |
217aa764 | 2287 | _bfd_elf_new_section_hook (bfd *abfd, asection *sec) |
252b5132 RH |
2288 | { |
2289 | struct bfd_elf_section_data *sdata; | |
551b43fd | 2290 | const struct elf_backend_data *bed; |
7dcb9820 | 2291 | const struct bfd_elf_special_section *ssect; |
252b5132 | 2292 | |
f0abc2a1 AM |
2293 | sdata = (struct bfd_elf_section_data *) sec->used_by_bfd; |
2294 | if (sdata == NULL) | |
2295 | { | |
a50b1753 NC |
2296 | sdata = (struct bfd_elf_section_data *) bfd_zalloc (abfd, |
2297 | sizeof (*sdata)); | |
f0abc2a1 AM |
2298 | if (sdata == NULL) |
2299 | return FALSE; | |
217aa764 | 2300 | sec->used_by_bfd = sdata; |
f0abc2a1 | 2301 | } |
bf572ba0 | 2302 | |
551b43fd AM |
2303 | /* Indicate whether or not this section should use RELA relocations. */ |
2304 | bed = get_elf_backend_data (abfd); | |
2305 | sec->use_rela_p = bed->default_use_rela_p; | |
2306 | ||
e843e0f8 L |
2307 | /* When we read a file, we don't need to set ELF section type and |
2308 | flags. They will be overridden in _bfd_elf_make_section_from_shdr | |
2309 | anyway. We will set ELF section type and flags for all linker | |
2310 | created sections. If user specifies BFD section flags, we will | |
2311 | set ELF section type and flags based on BFD section flags in | |
02ecc8e9 L |
2312 | elf_fake_sections. Special handling for .init_array/.fini_array |
2313 | output sections since they may contain .ctors/.dtors input | |
2314 | sections. We don't want _bfd_elf_init_private_section_data to | |
2315 | copy ELF section type from .ctors/.dtors input sections. */ | |
2316 | if (abfd->direction != read_direction | |
3496cb2a | 2317 | || (sec->flags & SEC_LINKER_CREATED) != 0) |
2f89ff8d | 2318 | { |
551b43fd | 2319 | ssect = (*bed->get_sec_type_attr) (abfd, sec); |
02ecc8e9 L |
2320 | if (ssect != NULL |
2321 | && (!sec->flags | |
2322 | || (sec->flags & SEC_LINKER_CREATED) != 0 | |
2323 | || ssect->type == SHT_INIT_ARRAY | |
2324 | || ssect->type == SHT_FINI_ARRAY)) | |
a31501e9 L |
2325 | { |
2326 | elf_section_type (sec) = ssect->type; | |
2327 | elf_section_flags (sec) = ssect->attr; | |
2328 | } | |
2f89ff8d L |
2329 | } |
2330 | ||
f592407e | 2331 | return _bfd_generic_new_section_hook (abfd, sec); |
252b5132 RH |
2332 | } |
2333 | ||
2334 | /* Create a new bfd section from an ELF program header. | |
2335 | ||
2336 | Since program segments have no names, we generate a synthetic name | |
2337 | of the form segment<NUM>, where NUM is generally the index in the | |
2338 | program header table. For segments that are split (see below) we | |
2339 | generate the names segment<NUM>a and segment<NUM>b. | |
2340 | ||
2341 | Note that some program segments may have a file size that is different than | |
2342 | (less than) the memory size. All this means is that at execution the | |
2343 | system must allocate the amount of memory specified by the memory size, | |
2344 | but only initialize it with the first "file size" bytes read from the | |
2345 | file. This would occur for example, with program segments consisting | |
2346 | of combined data+bss. | |
2347 | ||
2348 | To handle the above situation, this routine generates TWO bfd sections | |
2349 | for the single program segment. The first has the length specified by | |
2350 | the file size of the segment, and the second has the length specified | |
2351 | by the difference between the two sizes. In effect, the segment is split | |
d5191d0c | 2352 | into its initialized and uninitialized parts. |
252b5132 RH |
2353 | |
2354 | */ | |
2355 | ||
b34976b6 | 2356 | bfd_boolean |
217aa764 AM |
2357 | _bfd_elf_make_section_from_phdr (bfd *abfd, |
2358 | Elf_Internal_Phdr *hdr, | |
91d6fa6a | 2359 | int hdr_index, |
a50b1753 | 2360 | const char *type_name) |
252b5132 RH |
2361 | { |
2362 | asection *newsect; | |
2363 | char *name; | |
2364 | char namebuf[64]; | |
d4c88bbb | 2365 | size_t len; |
252b5132 RH |
2366 | int split; |
2367 | ||
2368 | split = ((hdr->p_memsz > 0) | |
2369 | && (hdr->p_filesz > 0) | |
2370 | && (hdr->p_memsz > hdr->p_filesz)); | |
d5191d0c AM |
2371 | |
2372 | if (hdr->p_filesz > 0) | |
252b5132 | 2373 | { |
91d6fa6a | 2374 | sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "a" : ""); |
d5191d0c | 2375 | len = strlen (namebuf) + 1; |
a50b1753 | 2376 | name = (char *) bfd_alloc (abfd, len); |
d5191d0c AM |
2377 | if (!name) |
2378 | return FALSE; | |
2379 | memcpy (name, namebuf, len); | |
2380 | newsect = bfd_make_section (abfd, name); | |
2381 | if (newsect == NULL) | |
2382 | return FALSE; | |
2383 | newsect->vma = hdr->p_vaddr; | |
2384 | newsect->lma = hdr->p_paddr; | |
2385 | newsect->size = hdr->p_filesz; | |
2386 | newsect->filepos = hdr->p_offset; | |
2387 | newsect->flags |= SEC_HAS_CONTENTS; | |
2388 | newsect->alignment_power = bfd_log2 (hdr->p_align); | |
2389 | if (hdr->p_type == PT_LOAD) | |
252b5132 | 2390 | { |
d5191d0c AM |
2391 | newsect->flags |= SEC_ALLOC; |
2392 | newsect->flags |= SEC_LOAD; | |
2393 | if (hdr->p_flags & PF_X) | |
2394 | { | |
2395 | /* FIXME: all we known is that it has execute PERMISSION, | |
2396 | may be data. */ | |
2397 | newsect->flags |= SEC_CODE; | |
2398 | } | |
2399 | } | |
2400 | if (!(hdr->p_flags & PF_W)) | |
2401 | { | |
2402 | newsect->flags |= SEC_READONLY; | |
252b5132 | 2403 | } |
252b5132 RH |
2404 | } |
2405 | ||
d5191d0c | 2406 | if (hdr->p_memsz > hdr->p_filesz) |
252b5132 | 2407 | { |
d5191d0c AM |
2408 | bfd_vma align; |
2409 | ||
91d6fa6a | 2410 | sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "b" : ""); |
d4c88bbb | 2411 | len = strlen (namebuf) + 1; |
a50b1753 | 2412 | name = (char *) bfd_alloc (abfd, len); |
252b5132 | 2413 | if (!name) |
b34976b6 | 2414 | return FALSE; |
d4c88bbb | 2415 | memcpy (name, namebuf, len); |
252b5132 RH |
2416 | newsect = bfd_make_section (abfd, name); |
2417 | if (newsect == NULL) | |
b34976b6 | 2418 | return FALSE; |
252b5132 RH |
2419 | newsect->vma = hdr->p_vaddr + hdr->p_filesz; |
2420 | newsect->lma = hdr->p_paddr + hdr->p_filesz; | |
eea6121a | 2421 | newsect->size = hdr->p_memsz - hdr->p_filesz; |
d5191d0c AM |
2422 | newsect->filepos = hdr->p_offset + hdr->p_filesz; |
2423 | align = newsect->vma & -newsect->vma; | |
2424 | if (align == 0 || align > hdr->p_align) | |
2425 | align = hdr->p_align; | |
2426 | newsect->alignment_power = bfd_log2 (align); | |
252b5132 RH |
2427 | if (hdr->p_type == PT_LOAD) |
2428 | { | |
d5191d0c AM |
2429 | /* Hack for gdb. Segments that have not been modified do |
2430 | not have their contents written to a core file, on the | |
2431 | assumption that a debugger can find the contents in the | |
2432 | executable. We flag this case by setting the fake | |
2433 | section size to zero. Note that "real" bss sections will | |
2434 | always have their contents dumped to the core file. */ | |
2435 | if (bfd_get_format (abfd) == bfd_core) | |
2436 | newsect->size = 0; | |
252b5132 RH |
2437 | newsect->flags |= SEC_ALLOC; |
2438 | if (hdr->p_flags & PF_X) | |
2439 | newsect->flags |= SEC_CODE; | |
2440 | } | |
2441 | if (!(hdr->p_flags & PF_W)) | |
2442 | newsect->flags |= SEC_READONLY; | |
2443 | } | |
2444 | ||
b34976b6 | 2445 | return TRUE; |
252b5132 RH |
2446 | } |
2447 | ||
b34976b6 | 2448 | bfd_boolean |
91d6fa6a | 2449 | bfd_section_from_phdr (bfd *abfd, Elf_Internal_Phdr *hdr, int hdr_index) |
20cfcaae | 2450 | { |
9c5bfbb7 | 2451 | const struct elf_backend_data *bed; |
20cfcaae NC |
2452 | |
2453 | switch (hdr->p_type) | |
2454 | { | |
2455 | case PT_NULL: | |
91d6fa6a | 2456 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "null"); |
20cfcaae NC |
2457 | |
2458 | case PT_LOAD: | |
91d6fa6a | 2459 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "load"); |
20cfcaae NC |
2460 | |
2461 | case PT_DYNAMIC: | |
91d6fa6a | 2462 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "dynamic"); |
20cfcaae NC |
2463 | |
2464 | case PT_INTERP: | |
91d6fa6a | 2465 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "interp"); |
20cfcaae NC |
2466 | |
2467 | case PT_NOTE: | |
91d6fa6a | 2468 | if (! _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "note")) |
b34976b6 | 2469 | return FALSE; |
718175fa | 2470 | if (! elf_read_notes (abfd, hdr->p_offset, hdr->p_filesz)) |
b34976b6 AM |
2471 | return FALSE; |
2472 | return TRUE; | |
20cfcaae NC |
2473 | |
2474 | case PT_SHLIB: | |
91d6fa6a | 2475 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "shlib"); |
20cfcaae NC |
2476 | |
2477 | case PT_PHDR: | |
91d6fa6a | 2478 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "phdr"); |
20cfcaae | 2479 | |
811072d8 | 2480 | case PT_GNU_EH_FRAME: |
91d6fa6a | 2481 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, |
811072d8 RM |
2482 | "eh_frame_hdr"); |
2483 | ||
2b05f1b7 | 2484 | case PT_GNU_STACK: |
91d6fa6a | 2485 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "stack"); |
9ee5e499 | 2486 | |
8c37241b | 2487 | case PT_GNU_RELRO: |
91d6fa6a | 2488 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "relro"); |
8c37241b | 2489 | |
20cfcaae | 2490 | default: |
8c1acd09 | 2491 | /* Check for any processor-specific program segment types. */ |
20cfcaae | 2492 | bed = get_elf_backend_data (abfd); |
91d6fa6a | 2493 | return bed->elf_backend_section_from_phdr (abfd, hdr, hdr_index, "proc"); |
20cfcaae NC |
2494 | } |
2495 | } | |
2496 | ||
d4730f92 BS |
2497 | /* Return the REL_HDR for SEC, assuming there is only a single one, either |
2498 | REL or RELA. */ | |
2499 | ||
2500 | Elf_Internal_Shdr * | |
2501 | _bfd_elf_single_rel_hdr (asection *sec) | |
2502 | { | |
2503 | if (elf_section_data (sec)->rel.hdr) | |
2504 | { | |
2505 | BFD_ASSERT (elf_section_data (sec)->rela.hdr == NULL); | |
2506 | return elf_section_data (sec)->rel.hdr; | |
2507 | } | |
2508 | else | |
2509 | return elf_section_data (sec)->rela.hdr; | |
2510 | } | |
2511 | ||
2512 | /* Allocate and initialize a section-header for a new reloc section, | |
2513 | containing relocations against ASECT. It is stored in RELDATA. If | |
2514 | USE_RELA_P is TRUE, we use RELA relocations; otherwise, we use REL | |
2515 | relocations. */ | |
23bc299b | 2516 | |
b34976b6 | 2517 | bfd_boolean |
217aa764 | 2518 | _bfd_elf_init_reloc_shdr (bfd *abfd, |
d4730f92 | 2519 | struct bfd_elf_section_reloc_data *reldata, |
217aa764 AM |
2520 | asection *asect, |
2521 | bfd_boolean use_rela_p) | |
23bc299b | 2522 | { |
d4730f92 | 2523 | Elf_Internal_Shdr *rel_hdr; |
23bc299b | 2524 | char *name; |
9c5bfbb7 | 2525 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
d4730f92 BS |
2526 | bfd_size_type amt; |
2527 | ||
2528 | amt = sizeof (Elf_Internal_Shdr); | |
2529 | BFD_ASSERT (reldata->hdr == NULL); | |
2530 | rel_hdr = bfd_zalloc (abfd, amt); | |
2531 | reldata->hdr = rel_hdr; | |
23bc299b | 2532 | |
d324f6d6 | 2533 | amt = sizeof ".rela" + strlen (asect->name); |
a50b1753 | 2534 | name = (char *) bfd_alloc (abfd, amt); |
23bc299b | 2535 | if (name == NULL) |
b34976b6 | 2536 | return FALSE; |
23bc299b MM |
2537 | sprintf (name, "%s%s", use_rela_p ? ".rela" : ".rel", asect->name); |
2538 | rel_hdr->sh_name = | |
2b0f7ef9 | 2539 | (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), name, |
b34976b6 | 2540 | FALSE); |
23bc299b | 2541 | if (rel_hdr->sh_name == (unsigned int) -1) |
b34976b6 | 2542 | return FALSE; |
23bc299b MM |
2543 | rel_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL; |
2544 | rel_hdr->sh_entsize = (use_rela_p | |
2545 | ? bed->s->sizeof_rela | |
2546 | : bed->s->sizeof_rel); | |
72de5009 | 2547 | rel_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align; |
23bc299b MM |
2548 | rel_hdr->sh_flags = 0; |
2549 | rel_hdr->sh_addr = 0; | |
2550 | rel_hdr->sh_size = 0; | |
2551 | rel_hdr->sh_offset = 0; | |
2552 | ||
b34976b6 | 2553 | return TRUE; |
23bc299b MM |
2554 | } |
2555 | ||
94be91de JB |
2556 | /* Return the default section type based on the passed in section flags. */ |
2557 | ||
2558 | int | |
2559 | bfd_elf_get_default_section_type (flagword flags) | |
2560 | { | |
2561 | if ((flags & SEC_ALLOC) != 0 | |
2e76e85a | 2562 | && (flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0) |
94be91de JB |
2563 | return SHT_NOBITS; |
2564 | return SHT_PROGBITS; | |
2565 | } | |
2566 | ||
d4730f92 BS |
2567 | struct fake_section_arg |
2568 | { | |
2569 | struct bfd_link_info *link_info; | |
2570 | bfd_boolean failed; | |
2571 | }; | |
2572 | ||
252b5132 RH |
2573 | /* Set up an ELF internal section header for a section. */ |
2574 | ||
252b5132 | 2575 | static void |
d4730f92 | 2576 | elf_fake_sections (bfd *abfd, asection *asect, void *fsarg) |
252b5132 | 2577 | { |
d4730f92 | 2578 | struct fake_section_arg *arg = (struct fake_section_arg *)fsarg; |
9c5bfbb7 | 2579 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
d4730f92 | 2580 | struct bfd_elf_section_data *esd = elf_section_data (asect); |
252b5132 | 2581 | Elf_Internal_Shdr *this_hdr; |
0414f35b | 2582 | unsigned int sh_type; |
252b5132 | 2583 | |
d4730f92 | 2584 | if (arg->failed) |
252b5132 RH |
2585 | { |
2586 | /* We already failed; just get out of the bfd_map_over_sections | |
08a40648 | 2587 | loop. */ |
252b5132 RH |
2588 | return; |
2589 | } | |
2590 | ||
d4730f92 | 2591 | this_hdr = &esd->this_hdr; |
252b5132 | 2592 | |
e57b5356 AM |
2593 | this_hdr->sh_name = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), |
2594 | asect->name, FALSE); | |
2595 | if (this_hdr->sh_name == (unsigned int) -1) | |
252b5132 | 2596 | { |
d4730f92 | 2597 | arg->failed = TRUE; |
252b5132 RH |
2598 | return; |
2599 | } | |
2600 | ||
a4d8e49b | 2601 | /* Don't clear sh_flags. Assembler may set additional bits. */ |
252b5132 RH |
2602 | |
2603 | if ((asect->flags & SEC_ALLOC) != 0 | |
2604 | || asect->user_set_vma) | |
2605 | this_hdr->sh_addr = asect->vma; | |
2606 | else | |
2607 | this_hdr->sh_addr = 0; | |
2608 | ||
2609 | this_hdr->sh_offset = 0; | |
eea6121a | 2610 | this_hdr->sh_size = asect->size; |
252b5132 | 2611 | this_hdr->sh_link = 0; |
72de5009 | 2612 | this_hdr->sh_addralign = (bfd_vma) 1 << asect->alignment_power; |
252b5132 RH |
2613 | /* The sh_entsize and sh_info fields may have been set already by |
2614 | copy_private_section_data. */ | |
2615 | ||
2616 | this_hdr->bfd_section = asect; | |
2617 | this_hdr->contents = NULL; | |
2618 | ||
3cddba1e L |
2619 | /* If the section type is unspecified, we set it based on |
2620 | asect->flags. */ | |
98ece1b3 AM |
2621 | if ((asect->flags & SEC_GROUP) != 0) |
2622 | sh_type = SHT_GROUP; | |
98ece1b3 | 2623 | else |
94be91de | 2624 | sh_type = bfd_elf_get_default_section_type (asect->flags); |
98ece1b3 | 2625 | |
3cddba1e | 2626 | if (this_hdr->sh_type == SHT_NULL) |
98ece1b3 AM |
2627 | this_hdr->sh_type = sh_type; |
2628 | else if (this_hdr->sh_type == SHT_NOBITS | |
2629 | && sh_type == SHT_PROGBITS | |
2630 | && (asect->flags & SEC_ALLOC) != 0) | |
3cddba1e | 2631 | { |
98ece1b3 AM |
2632 | /* Warn if we are changing a NOBITS section to PROGBITS, but |
2633 | allow the link to proceed. This can happen when users link | |
2634 | non-bss input sections to bss output sections, or emit data | |
2635 | to a bss output section via a linker script. */ | |
2636 | (*_bfd_error_handler) | |
58f0869b | 2637 | (_("warning: section `%A' type changed to PROGBITS"), asect); |
98ece1b3 | 2638 | this_hdr->sh_type = sh_type; |
3cddba1e L |
2639 | } |
2640 | ||
2f89ff8d | 2641 | switch (this_hdr->sh_type) |
252b5132 | 2642 | { |
2f89ff8d | 2643 | default: |
2f89ff8d L |
2644 | break; |
2645 | ||
2646 | case SHT_STRTAB: | |
2647 | case SHT_INIT_ARRAY: | |
2648 | case SHT_FINI_ARRAY: | |
2649 | case SHT_PREINIT_ARRAY: | |
2650 | case SHT_NOTE: | |
2651 | case SHT_NOBITS: | |
2652 | case SHT_PROGBITS: | |
2653 | break; | |
2654 | ||
2655 | case SHT_HASH: | |
c7ac6ff8 | 2656 | this_hdr->sh_entsize = bed->s->sizeof_hash_entry; |
2f89ff8d | 2657 | break; |
5de3bf90 | 2658 | |
2f89ff8d | 2659 | case SHT_DYNSYM: |
252b5132 | 2660 | this_hdr->sh_entsize = bed->s->sizeof_sym; |
2f89ff8d L |
2661 | break; |
2662 | ||
2663 | case SHT_DYNAMIC: | |
252b5132 | 2664 | this_hdr->sh_entsize = bed->s->sizeof_dyn; |
2f89ff8d L |
2665 | break; |
2666 | ||
2667 | case SHT_RELA: | |
2668 | if (get_elf_backend_data (abfd)->may_use_rela_p) | |
2669 | this_hdr->sh_entsize = bed->s->sizeof_rela; | |
2670 | break; | |
2671 | ||
2672 | case SHT_REL: | |
2673 | if (get_elf_backend_data (abfd)->may_use_rel_p) | |
2674 | this_hdr->sh_entsize = bed->s->sizeof_rel; | |
2675 | break; | |
2676 | ||
2677 | case SHT_GNU_versym: | |
252b5132 | 2678 | this_hdr->sh_entsize = sizeof (Elf_External_Versym); |
2f89ff8d L |
2679 | break; |
2680 | ||
2681 | case SHT_GNU_verdef: | |
252b5132 RH |
2682 | this_hdr->sh_entsize = 0; |
2683 | /* objcopy or strip will copy over sh_info, but may not set | |
08a40648 AM |
2684 | cverdefs. The linker will set cverdefs, but sh_info will be |
2685 | zero. */ | |
252b5132 RH |
2686 | if (this_hdr->sh_info == 0) |
2687 | this_hdr->sh_info = elf_tdata (abfd)->cverdefs; | |
2688 | else | |
2689 | BFD_ASSERT (elf_tdata (abfd)->cverdefs == 0 | |
2690 | || this_hdr->sh_info == elf_tdata (abfd)->cverdefs); | |
2f89ff8d L |
2691 | break; |
2692 | ||
2693 | case SHT_GNU_verneed: | |
252b5132 RH |
2694 | this_hdr->sh_entsize = 0; |
2695 | /* objcopy or strip will copy over sh_info, but may not set | |
08a40648 AM |
2696 | cverrefs. The linker will set cverrefs, but sh_info will be |
2697 | zero. */ | |
252b5132 RH |
2698 | if (this_hdr->sh_info == 0) |
2699 | this_hdr->sh_info = elf_tdata (abfd)->cverrefs; | |
2700 | else | |
2701 | BFD_ASSERT (elf_tdata (abfd)->cverrefs == 0 | |
2702 | || this_hdr->sh_info == elf_tdata (abfd)->cverrefs); | |
2f89ff8d L |
2703 | break; |
2704 | ||
2705 | case SHT_GROUP: | |
1783205a | 2706 | this_hdr->sh_entsize = GRP_ENTRY_SIZE; |
2f89ff8d | 2707 | break; |
fdc90cb4 JJ |
2708 | |
2709 | case SHT_GNU_HASH: | |
2710 | this_hdr->sh_entsize = bed->s->arch_size == 64 ? 0 : 4; | |
2711 | break; | |
dbb410c3 | 2712 | } |
252b5132 RH |
2713 | |
2714 | if ((asect->flags & SEC_ALLOC) != 0) | |
2715 | this_hdr->sh_flags |= SHF_ALLOC; | |
2716 | if ((asect->flags & SEC_READONLY) == 0) | |
2717 | this_hdr->sh_flags |= SHF_WRITE; | |
2718 | if ((asect->flags & SEC_CODE) != 0) | |
2719 | this_hdr->sh_flags |= SHF_EXECINSTR; | |
f5fa8ca2 JJ |
2720 | if ((asect->flags & SEC_MERGE) != 0) |
2721 | { | |
2722 | this_hdr->sh_flags |= SHF_MERGE; | |
2723 | this_hdr->sh_entsize = asect->entsize; | |
2724 | if ((asect->flags & SEC_STRINGS) != 0) | |
2725 | this_hdr->sh_flags |= SHF_STRINGS; | |
2726 | } | |
1126897b | 2727 | if ((asect->flags & SEC_GROUP) == 0 && elf_group_name (asect) != NULL) |
dbb410c3 | 2728 | this_hdr->sh_flags |= SHF_GROUP; |
13ae64f3 | 2729 | if ((asect->flags & SEC_THREAD_LOCAL) != 0) |
704afa60 JJ |
2730 | { |
2731 | this_hdr->sh_flags |= SHF_TLS; | |
3a800eb9 AM |
2732 | if (asect->size == 0 |
2733 | && (asect->flags & SEC_HAS_CONTENTS) == 0) | |
704afa60 | 2734 | { |
3a800eb9 | 2735 | struct bfd_link_order *o = asect->map_tail.link_order; |
b34976b6 | 2736 | |
704afa60 | 2737 | this_hdr->sh_size = 0; |
3a800eb9 AM |
2738 | if (o != NULL) |
2739 | { | |
704afa60 | 2740 | this_hdr->sh_size = o->offset + o->size; |
3a800eb9 AM |
2741 | if (this_hdr->sh_size != 0) |
2742 | this_hdr->sh_type = SHT_NOBITS; | |
2743 | } | |
704afa60 JJ |
2744 | } |
2745 | } | |
18ae9cc1 L |
2746 | if ((asect->flags & (SEC_GROUP | SEC_EXCLUDE)) == SEC_EXCLUDE) |
2747 | this_hdr->sh_flags |= SHF_EXCLUDE; | |
252b5132 | 2748 | |
d4730f92 BS |
2749 | /* If the section has relocs, set up a section header for the |
2750 | SHT_REL[A] section. If two relocation sections are required for | |
2751 | this section, it is up to the processor-specific back-end to | |
2752 | create the other. */ | |
2753 | if ((asect->flags & SEC_RELOC) != 0) | |
2754 | { | |
2755 | /* When doing a relocatable link, create both REL and RELA sections if | |
2756 | needed. */ | |
2757 | if (arg->link_info | |
2758 | /* Do the normal setup if we wouldn't create any sections here. */ | |
2759 | && esd->rel.count + esd->rela.count > 0 | |
2760 | && (arg->link_info->relocatable || arg->link_info->emitrelocations)) | |
2761 | { | |
2762 | if (esd->rel.count && esd->rel.hdr == NULL | |
2763 | && !_bfd_elf_init_reloc_shdr (abfd, &esd->rel, asect, FALSE)) | |
2764 | { | |
2765 | arg->failed = TRUE; | |
2766 | return; | |
2767 | } | |
2768 | if (esd->rela.count && esd->rela.hdr == NULL | |
2769 | && !_bfd_elf_init_reloc_shdr (abfd, &esd->rela, asect, TRUE)) | |
2770 | { | |
2771 | arg->failed = TRUE; | |
2772 | return; | |
2773 | } | |
2774 | } | |
2775 | else if (!_bfd_elf_init_reloc_shdr (abfd, | |
2776 | (asect->use_rela_p | |
2777 | ? &esd->rela : &esd->rel), | |
2778 | asect, | |
2779 | asect->use_rela_p)) | |
2780 | arg->failed = TRUE; | |
2781 | } | |
2782 | ||
252b5132 | 2783 | /* Check for processor-specific section types. */ |
0414f35b | 2784 | sh_type = this_hdr->sh_type; |
e1fddb6b AO |
2785 | if (bed->elf_backend_fake_sections |
2786 | && !(*bed->elf_backend_fake_sections) (abfd, this_hdr, asect)) | |
d4730f92 | 2787 | arg->failed = TRUE; |
252b5132 | 2788 | |
42bb2e33 | 2789 | if (sh_type == SHT_NOBITS && asect->size != 0) |
0414f35b AM |
2790 | { |
2791 | /* Don't change the header type from NOBITS if we are being | |
42bb2e33 | 2792 | called for objcopy --only-keep-debug. */ |
0414f35b AM |
2793 | this_hdr->sh_type = sh_type; |
2794 | } | |
252b5132 RH |
2795 | } |
2796 | ||
bcacc0f5 AM |
2797 | /* Fill in the contents of a SHT_GROUP section. Called from |
2798 | _bfd_elf_compute_section_file_positions for gas, objcopy, and | |
2799 | when ELF targets use the generic linker, ld. Called for ld -r | |
2800 | from bfd_elf_final_link. */ | |
dbb410c3 | 2801 | |
1126897b | 2802 | void |
217aa764 | 2803 | bfd_elf_set_group_contents (bfd *abfd, asection *sec, void *failedptrarg) |
dbb410c3 | 2804 | { |
a50b1753 | 2805 | bfd_boolean *failedptr = (bfd_boolean *) failedptrarg; |
9dce4196 | 2806 | asection *elt, *first; |
dbb410c3 | 2807 | unsigned char *loc; |
b34976b6 | 2808 | bfd_boolean gas; |
dbb410c3 | 2809 | |
7e4111ad L |
2810 | /* Ignore linker created group section. See elfNN_ia64_object_p in |
2811 | elfxx-ia64.c. */ | |
2812 | if (((sec->flags & (SEC_GROUP | SEC_LINKER_CREATED)) != SEC_GROUP) | |
dbb410c3 AM |
2813 | || *failedptr) |
2814 | return; | |
2815 | ||
bcacc0f5 AM |
2816 | if (elf_section_data (sec)->this_hdr.sh_info == 0) |
2817 | { | |
2818 | unsigned long symindx = 0; | |
2819 | ||
2820 | /* elf_group_id will have been set up by objcopy and the | |
2821 | generic linker. */ | |
2822 | if (elf_group_id (sec) != NULL) | |
2823 | symindx = elf_group_id (sec)->udata.i; | |
1126897b | 2824 | |
bcacc0f5 AM |
2825 | if (symindx == 0) |
2826 | { | |
2827 | /* If called from the assembler, swap_out_syms will have set up | |
2828 | elf_section_syms. */ | |
2829 | BFD_ASSERT (elf_section_syms (abfd) != NULL); | |
2830 | symindx = elf_section_syms (abfd)[sec->index]->udata.i; | |
2831 | } | |
2832 | elf_section_data (sec)->this_hdr.sh_info = symindx; | |
2833 | } | |
2834 | else if (elf_section_data (sec)->this_hdr.sh_info == (unsigned int) -2) | |
1126897b | 2835 | { |
bcacc0f5 AM |
2836 | /* The ELF backend linker sets sh_info to -2 when the group |
2837 | signature symbol is global, and thus the index can't be | |
2838 | set until all local symbols are output. */ | |
2839 | asection *igroup = elf_sec_group (elf_next_in_group (sec)); | |
2840 | struct bfd_elf_section_data *sec_data = elf_section_data (igroup); | |
2841 | unsigned long symndx = sec_data->this_hdr.sh_info; | |
2842 | unsigned long extsymoff = 0; | |
2843 | struct elf_link_hash_entry *h; | |
2844 | ||
2845 | if (!elf_bad_symtab (igroup->owner)) | |
2846 | { | |
2847 | Elf_Internal_Shdr *symtab_hdr; | |
2848 | ||
2849 | symtab_hdr = &elf_tdata (igroup->owner)->symtab_hdr; | |
2850 | extsymoff = symtab_hdr->sh_info; | |
2851 | } | |
2852 | h = elf_sym_hashes (igroup->owner)[symndx - extsymoff]; | |
2853 | while (h->root.type == bfd_link_hash_indirect | |
2854 | || h->root.type == bfd_link_hash_warning) | |
2855 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
2856 | ||
2857 | elf_section_data (sec)->this_hdr.sh_info = h->indx; | |
1126897b | 2858 | } |
dbb410c3 | 2859 | |
1126897b | 2860 | /* The contents won't be allocated for "ld -r" or objcopy. */ |
b34976b6 | 2861 | gas = TRUE; |
dbb410c3 AM |
2862 | if (sec->contents == NULL) |
2863 | { | |
b34976b6 | 2864 | gas = FALSE; |
a50b1753 | 2865 | sec->contents = (unsigned char *) bfd_alloc (abfd, sec->size); |
9dce4196 AM |
2866 | |
2867 | /* Arrange for the section to be written out. */ | |
2868 | elf_section_data (sec)->this_hdr.contents = sec->contents; | |
dbb410c3 AM |
2869 | if (sec->contents == NULL) |
2870 | { | |
b34976b6 | 2871 | *failedptr = TRUE; |
dbb410c3 AM |
2872 | return; |
2873 | } | |
2874 | } | |
2875 | ||
eea6121a | 2876 | loc = sec->contents + sec->size; |
dbb410c3 | 2877 | |
9dce4196 AM |
2878 | /* Get the pointer to the first section in the group that gas |
2879 | squirreled away here. objcopy arranges for this to be set to the | |
2880 | start of the input section group. */ | |
2881 | first = elt = elf_next_in_group (sec); | |
dbb410c3 AM |
2882 | |
2883 | /* First element is a flag word. Rest of section is elf section | |
2884 | indices for all the sections of the group. Write them backwards | |
2885 | just to keep the group in the same order as given in .section | |
2886 | directives, not that it matters. */ | |
2887 | while (elt != NULL) | |
2888 | { | |
9dce4196 | 2889 | asection *s; |
9dce4196 | 2890 | |
9dce4196 | 2891 | s = elt; |
415f38a6 AM |
2892 | if (!gas) |
2893 | s = s->output_section; | |
2894 | if (s != NULL | |
2895 | && !bfd_is_abs_section (s)) | |
01e1a5bc | 2896 | { |
415f38a6 AM |
2897 | unsigned int idx = elf_section_data (s)->this_idx; |
2898 | ||
01e1a5bc | 2899 | loc -= 4; |
01e1a5bc NC |
2900 | H_PUT_32 (abfd, idx, loc); |
2901 | } | |
945906ff | 2902 | elt = elf_next_in_group (elt); |
9dce4196 AM |
2903 | if (elt == first) |
2904 | break; | |
dbb410c3 AM |
2905 | } |
2906 | ||
3d7f7666 | 2907 | if ((loc -= 4) != sec->contents) |
9dce4196 | 2908 | abort (); |
dbb410c3 | 2909 | |
9dce4196 | 2910 | H_PUT_32 (abfd, sec->flags & SEC_LINK_ONCE ? GRP_COMDAT : 0, loc); |
dbb410c3 AM |
2911 | } |
2912 | ||
252b5132 RH |
2913 | /* Assign all ELF section numbers. The dummy first section is handled here |
2914 | too. The link/info pointers for the standard section types are filled | |
2915 | in here too, while we're at it. */ | |
2916 | ||
b34976b6 | 2917 | static bfd_boolean |
da9f89d4 | 2918 | assign_section_numbers (bfd *abfd, struct bfd_link_info *link_info) |
252b5132 RH |
2919 | { |
2920 | struct elf_obj_tdata *t = elf_tdata (abfd); | |
2921 | asection *sec; | |
2b0f7ef9 | 2922 | unsigned int section_number, secn; |
252b5132 | 2923 | Elf_Internal_Shdr **i_shdrp; |
47cc2cf5 | 2924 | struct bfd_elf_section_data *d; |
3516e984 | 2925 | bfd_boolean need_symtab; |
252b5132 RH |
2926 | |
2927 | section_number = 1; | |
2928 | ||
2b0f7ef9 JJ |
2929 | _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd)); |
2930 | ||
da9f89d4 L |
2931 | /* SHT_GROUP sections are in relocatable files only. */ |
2932 | if (link_info == NULL || link_info->relocatable) | |
252b5132 | 2933 | { |
da9f89d4 | 2934 | /* Put SHT_GROUP sections first. */ |
04dd1667 | 2935 | for (sec = abfd->sections; sec != NULL; sec = sec->next) |
47cc2cf5 | 2936 | { |
5daa8fe7 | 2937 | d = elf_section_data (sec); |
da9f89d4 L |
2938 | |
2939 | if (d->this_hdr.sh_type == SHT_GROUP) | |
08a40648 | 2940 | { |
5daa8fe7 | 2941 | if (sec->flags & SEC_LINKER_CREATED) |
da9f89d4 L |
2942 | { |
2943 | /* Remove the linker created SHT_GROUP sections. */ | |
5daa8fe7 | 2944 | bfd_section_list_remove (abfd, sec); |
da9f89d4 | 2945 | abfd->section_count--; |
da9f89d4 | 2946 | } |
08a40648 | 2947 | else |
4fbb74a6 | 2948 | d->this_idx = section_number++; |
da9f89d4 | 2949 | } |
47cc2cf5 PB |
2950 | } |
2951 | } | |
2952 | ||
2953 | for (sec = abfd->sections; sec; sec = sec->next) | |
2954 | { | |
2955 | d = elf_section_data (sec); | |
2956 | ||
2957 | if (d->this_hdr.sh_type != SHT_GROUP) | |
4fbb74a6 | 2958 | d->this_idx = section_number++; |
2b0f7ef9 | 2959 | _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->this_hdr.sh_name); |
d4730f92 | 2960 | if (d->rel.hdr) |
2b0f7ef9 | 2961 | { |
d4730f92 BS |
2962 | d->rel.idx = section_number++; |
2963 | _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel.hdr->sh_name); | |
2b0f7ef9 | 2964 | } |
d4730f92 BS |
2965 | else |
2966 | d->rel.idx = 0; | |
23bc299b | 2967 | |
d4730f92 | 2968 | if (d->rela.hdr) |
2b0f7ef9 | 2969 | { |
d4730f92 BS |
2970 | d->rela.idx = section_number++; |
2971 | _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rela.hdr->sh_name); | |
2b0f7ef9 | 2972 | } |
23bc299b | 2973 | else |
d4730f92 | 2974 | d->rela.idx = 0; |
252b5132 RH |
2975 | } |
2976 | ||
2977 | t->shstrtab_section = section_number++; | |
2b0f7ef9 | 2978 | _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->shstrtab_hdr.sh_name); |
252b5132 | 2979 | elf_elfheader (abfd)->e_shstrndx = t->shstrtab_section; |
252b5132 | 2980 | |
3516e984 L |
2981 | need_symtab = (bfd_get_symcount (abfd) > 0 |
2982 | || (link_info == NULL | |
2983 | && ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC)) | |
2984 | == HAS_RELOC))); | |
2985 | if (need_symtab) | |
252b5132 RH |
2986 | { |
2987 | t->symtab_section = section_number++; | |
2b0f7ef9 | 2988 | _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->symtab_hdr.sh_name); |
4fbb74a6 | 2989 | if (section_number > ((SHN_LORESERVE - 2) & 0xFFFF)) |
9ad5cbcf | 2990 | { |
9ad5cbcf AM |
2991 | t->symtab_shndx_section = section_number++; |
2992 | t->symtab_shndx_hdr.sh_name | |
2993 | = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), | |
b34976b6 | 2994 | ".symtab_shndx", FALSE); |
9ad5cbcf | 2995 | if (t->symtab_shndx_hdr.sh_name == (unsigned int) -1) |
b34976b6 | 2996 | return FALSE; |
9ad5cbcf | 2997 | } |
252b5132 | 2998 | t->strtab_section = section_number++; |
2b0f7ef9 | 2999 | _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->strtab_hdr.sh_name); |
252b5132 RH |
3000 | } |
3001 | ||
1c52a645 L |
3002 | if (section_number >= SHN_LORESERVE) |
3003 | { | |
3004 | _bfd_error_handler (_("%B: too many sections: %u"), | |
3005 | abfd, section_number); | |
3006 | return FALSE; | |
3007 | } | |
3008 | ||
2b0f7ef9 JJ |
3009 | _bfd_elf_strtab_finalize (elf_shstrtab (abfd)); |
3010 | t->shstrtab_hdr.sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd)); | |
9ad5cbcf AM |
3011 | |
3012 | elf_numsections (abfd) = section_number; | |
252b5132 RH |
3013 | elf_elfheader (abfd)->e_shnum = section_number; |
3014 | ||
3015 | /* Set up the list of section header pointers, in agreement with the | |
3016 | indices. */ | |
a50b1753 NC |
3017 | i_shdrp = (Elf_Internal_Shdr **) bfd_zalloc2 (abfd, section_number, |
3018 | sizeof (Elf_Internal_Shdr *)); | |
252b5132 | 3019 | if (i_shdrp == NULL) |
b34976b6 | 3020 | return FALSE; |
252b5132 | 3021 | |
a50b1753 NC |
3022 | i_shdrp[0] = (Elf_Internal_Shdr *) bfd_zalloc (abfd, |
3023 | sizeof (Elf_Internal_Shdr)); | |
252b5132 RH |
3024 | if (i_shdrp[0] == NULL) |
3025 | { | |
3026 | bfd_release (abfd, i_shdrp); | |
b34976b6 | 3027 | return FALSE; |
252b5132 | 3028 | } |
252b5132 RH |
3029 | |
3030 | elf_elfsections (abfd) = i_shdrp; | |
3031 | ||
3032 | i_shdrp[t->shstrtab_section] = &t->shstrtab_hdr; | |
3516e984 | 3033 | if (need_symtab) |
252b5132 RH |
3034 | { |
3035 | i_shdrp[t->symtab_section] = &t->symtab_hdr; | |
4fbb74a6 | 3036 | if (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF)) |
9ad5cbcf AM |
3037 | { |
3038 | i_shdrp[t->symtab_shndx_section] = &t->symtab_shndx_hdr; | |
3039 | t->symtab_shndx_hdr.sh_link = t->symtab_section; | |
3040 | } | |
252b5132 RH |
3041 | i_shdrp[t->strtab_section] = &t->strtab_hdr; |
3042 | t->symtab_hdr.sh_link = t->strtab_section; | |
3043 | } | |
38ce5b11 | 3044 | |
252b5132 RH |
3045 | for (sec = abfd->sections; sec; sec = sec->next) |
3046 | { | |
252b5132 RH |
3047 | asection *s; |
3048 | const char *name; | |
3049 | ||
91d6fa6a NC |
3050 | d = elf_section_data (sec); |
3051 | ||
252b5132 | 3052 | i_shdrp[d->this_idx] = &d->this_hdr; |
d4730f92 BS |
3053 | if (d->rel.idx != 0) |
3054 | i_shdrp[d->rel.idx] = d->rel.hdr; | |
3055 | if (d->rela.idx != 0) | |
3056 | i_shdrp[d->rela.idx] = d->rela.hdr; | |
252b5132 RH |
3057 | |
3058 | /* Fill in the sh_link and sh_info fields while we're at it. */ | |
3059 | ||
3060 | /* sh_link of a reloc section is the section index of the symbol | |
3061 | table. sh_info is the section index of the section to which | |
3062 | the relocation entries apply. */ | |
d4730f92 | 3063 | if (d->rel.idx != 0) |
252b5132 | 3064 | { |
d4730f92 BS |
3065 | d->rel.hdr->sh_link = t->symtab_section; |
3066 | d->rel.hdr->sh_info = d->this_idx; | |
252b5132 | 3067 | } |
d4730f92 | 3068 | if (d->rela.idx != 0) |
23bc299b | 3069 | { |
d4730f92 BS |
3070 | d->rela.hdr->sh_link = t->symtab_section; |
3071 | d->rela.hdr->sh_info = d->this_idx; | |
23bc299b | 3072 | } |
252b5132 | 3073 | |
38ce5b11 L |
3074 | /* We need to set up sh_link for SHF_LINK_ORDER. */ |
3075 | if ((d->this_hdr.sh_flags & SHF_LINK_ORDER) != 0) | |
3076 | { | |
3077 | s = elf_linked_to_section (sec); | |
3078 | if (s) | |
38ce5b11 | 3079 | { |
f2876037 | 3080 | /* elf_linked_to_section points to the input section. */ |
ccd2ec6a | 3081 | if (link_info != NULL) |
38ce5b11 | 3082 | { |
f2876037 | 3083 | /* Check discarded linkonce section. */ |
dbaa2011 | 3084 | if (discarded_section (s)) |
38ce5b11 | 3085 | { |
ccd2ec6a L |
3086 | asection *kept; |
3087 | (*_bfd_error_handler) | |
3088 | (_("%B: sh_link of section `%A' points to discarded section `%A' of `%B'"), | |
3089 | abfd, d->this_hdr.bfd_section, | |
3090 | s, s->owner); | |
3091 | /* Point to the kept section if it has the same | |
3092 | size as the discarded one. */ | |
c0f00686 | 3093 | kept = _bfd_elf_check_kept_section (s, link_info); |
ccd2ec6a | 3094 | if (kept == NULL) |
185d09ad | 3095 | { |
ccd2ec6a L |
3096 | bfd_set_error (bfd_error_bad_value); |
3097 | return FALSE; | |
185d09ad | 3098 | } |
ccd2ec6a | 3099 | s = kept; |
38ce5b11 | 3100 | } |
e424ecc8 | 3101 | |
ccd2ec6a L |
3102 | s = s->output_section; |
3103 | BFD_ASSERT (s != NULL); | |
38ce5b11 | 3104 | } |
f2876037 L |
3105 | else |
3106 | { | |
3107 | /* Handle objcopy. */ | |
3108 | if (s->output_section == NULL) | |
3109 | { | |
3110 | (*_bfd_error_handler) | |
3111 | (_("%B: sh_link of section `%A' points to removed section `%A' of `%B'"), | |
3112 | abfd, d->this_hdr.bfd_section, s, s->owner); | |
3113 | bfd_set_error (bfd_error_bad_value); | |
3114 | return FALSE; | |
3115 | } | |
3116 | s = s->output_section; | |
3117 | } | |
ccd2ec6a L |
3118 | d->this_hdr.sh_link = elf_section_data (s)->this_idx; |
3119 | } | |
3120 | else | |
3121 | { | |
3122 | /* PR 290: | |
3123 | The Intel C compiler generates SHT_IA_64_UNWIND with | |
3124 | SHF_LINK_ORDER. But it doesn't set the sh_link or | |
3125 | sh_info fields. Hence we could get the situation | |
08a40648 | 3126 | where s is NULL. */ |
ccd2ec6a L |
3127 | const struct elf_backend_data *bed |
3128 | = get_elf_backend_data (abfd); | |
3129 | if (bed->link_order_error_handler) | |
3130 | bed->link_order_error_handler | |
3131 | (_("%B: warning: sh_link not set for section `%A'"), | |
3132 | abfd, sec); | |
38ce5b11 L |
3133 | } |
3134 | } | |
3135 | ||
252b5132 RH |
3136 | switch (d->this_hdr.sh_type) |
3137 | { | |
3138 | case SHT_REL: | |
3139 | case SHT_RELA: | |
3140 | /* A reloc section which we are treating as a normal BFD | |
3141 | section. sh_link is the section index of the symbol | |
3142 | table. sh_info is the section index of the section to | |
3143 | which the relocation entries apply. We assume that an | |
3144 | allocated reloc section uses the dynamic symbol table. | |
3145 | FIXME: How can we be sure? */ | |
3146 | s = bfd_get_section_by_name (abfd, ".dynsym"); | |
3147 | if (s != NULL) | |
3148 | d->this_hdr.sh_link = elf_section_data (s)->this_idx; | |
3149 | ||
3150 | /* We look up the section the relocs apply to by name. */ | |
3151 | name = sec->name; | |
3152 | if (d->this_hdr.sh_type == SHT_REL) | |
3153 | name += 4; | |
3154 | else | |
3155 | name += 5; | |
3156 | s = bfd_get_section_by_name (abfd, name); | |
3157 | if (s != NULL) | |
3158 | d->this_hdr.sh_info = elf_section_data (s)->this_idx; | |
3159 | break; | |
3160 | ||
3161 | case SHT_STRTAB: | |
3162 | /* We assume that a section named .stab*str is a stabs | |
3163 | string section. We look for a section with the same name | |
3164 | but without the trailing ``str'', and set its sh_link | |
3165 | field to point to this section. */ | |
0112cd26 | 3166 | if (CONST_STRNEQ (sec->name, ".stab") |
252b5132 RH |
3167 | && strcmp (sec->name + strlen (sec->name) - 3, "str") == 0) |
3168 | { | |
3169 | size_t len; | |
3170 | char *alc; | |
3171 | ||
3172 | len = strlen (sec->name); | |
a50b1753 | 3173 | alc = (char *) bfd_malloc (len - 2); |
252b5132 | 3174 | if (alc == NULL) |
b34976b6 | 3175 | return FALSE; |
d4c88bbb | 3176 | memcpy (alc, sec->name, len - 3); |
252b5132 RH |
3177 | alc[len - 3] = '\0'; |
3178 | s = bfd_get_section_by_name (abfd, alc); | |
3179 | free (alc); | |
3180 | if (s != NULL) | |
3181 | { | |
3182 | elf_section_data (s)->this_hdr.sh_link = d->this_idx; | |
3183 | ||
3184 | /* This is a .stab section. */ | |
0594c12d AM |
3185 | if (elf_section_data (s)->this_hdr.sh_entsize == 0) |
3186 | elf_section_data (s)->this_hdr.sh_entsize | |
3187 | = 4 + 2 * bfd_get_arch_size (abfd) / 8; | |
252b5132 RH |
3188 | } |
3189 | } | |
3190 | break; | |
3191 | ||
3192 | case SHT_DYNAMIC: | |
3193 | case SHT_DYNSYM: | |
3194 | case SHT_GNU_verneed: | |
3195 | case SHT_GNU_verdef: | |
3196 | /* sh_link is the section header index of the string table | |
3197 | used for the dynamic entries, or the symbol table, or the | |
3198 | version strings. */ | |
3199 | s = bfd_get_section_by_name (abfd, ".dynstr"); | |
3200 | if (s != NULL) | |
3201 | d->this_hdr.sh_link = elf_section_data (s)->this_idx; | |
3202 | break; | |
3203 | ||
7f1204bb JJ |
3204 | case SHT_GNU_LIBLIST: |
3205 | /* sh_link is the section header index of the prelink library | |
08a40648 AM |
3206 | list used for the dynamic entries, or the symbol table, or |
3207 | the version strings. */ | |
7f1204bb JJ |
3208 | s = bfd_get_section_by_name (abfd, (sec->flags & SEC_ALLOC) |
3209 | ? ".dynstr" : ".gnu.libstr"); | |
3210 | if (s != NULL) | |
3211 | d->this_hdr.sh_link = elf_section_data (s)->this_idx; | |
3212 | break; | |
3213 | ||
252b5132 | 3214 | case SHT_HASH: |
fdc90cb4 | 3215 | case SHT_GNU_HASH: |
252b5132 RH |
3216 | case SHT_GNU_versym: |
3217 | /* sh_link is the section header index of the symbol table | |
3218 | this hash table or version table is for. */ | |
3219 | s = bfd_get_section_by_name (abfd, ".dynsym"); | |
3220 | if (s != NULL) | |
3221 | d->this_hdr.sh_link = elf_section_data (s)->this_idx; | |
3222 | break; | |
dbb410c3 AM |
3223 | |
3224 | case SHT_GROUP: | |
3225 | d->this_hdr.sh_link = t->symtab_section; | |
252b5132 RH |
3226 | } |
3227 | } | |
3228 | ||
2b0f7ef9 | 3229 | for (secn = 1; secn < section_number; ++secn) |
9ad5cbcf AM |
3230 | if (i_shdrp[secn] == NULL) |
3231 | i_shdrp[secn] = i_shdrp[0]; | |
3232 | else | |
3233 | i_shdrp[secn]->sh_name = _bfd_elf_strtab_offset (elf_shstrtab (abfd), | |
3234 | i_shdrp[secn]->sh_name); | |
b34976b6 | 3235 | return TRUE; |
252b5132 RH |
3236 | } |
3237 | ||
5372391b | 3238 | static bfd_boolean |
217aa764 | 3239 | sym_is_global (bfd *abfd, asymbol *sym) |
252b5132 RH |
3240 | { |
3241 | /* If the backend has a special mapping, use it. */ | |
9c5bfbb7 | 3242 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
217aa764 AM |
3243 | if (bed->elf_backend_sym_is_global) |
3244 | return (*bed->elf_backend_sym_is_global) (abfd, sym); | |
252b5132 | 3245 | |
e47bf690 | 3246 | return ((sym->flags & (BSF_GLOBAL | BSF_WEAK | BSF_GNU_UNIQUE)) != 0 |
252b5132 RH |
3247 | || bfd_is_und_section (bfd_get_section (sym)) |
3248 | || bfd_is_com_section (bfd_get_section (sym))); | |
3249 | } | |
3250 | ||
5372391b | 3251 | /* Don't output section symbols for sections that are not going to be |
c6d8cab4 | 3252 | output, that are duplicates or there is no BFD section. */ |
5372391b AM |
3253 | |
3254 | static bfd_boolean | |
3255 | ignore_section_sym (bfd *abfd, asymbol *sym) | |
3256 | { | |
c6d8cab4 L |
3257 | elf_symbol_type *type_ptr; |
3258 | ||
3259 | if ((sym->flags & BSF_SECTION_SYM) == 0) | |
3260 | return FALSE; | |
3261 | ||
3262 | type_ptr = elf_symbol_from (abfd, sym); | |
3263 | return ((type_ptr != NULL | |
3264 | && type_ptr->internal_elf_sym.st_shndx != 0 | |
3265 | && bfd_is_abs_section (sym->section)) | |
3266 | || !(sym->section->owner == abfd | |
0f0a5e58 | 3267 | || (sym->section->output_section->owner == abfd |
2633a79c AM |
3268 | && sym->section->output_offset == 0) |
3269 | || bfd_is_abs_section (sym->section))); | |
5372391b AM |
3270 | } |
3271 | ||
2633a79c AM |
3272 | /* Map symbol from it's internal number to the external number, moving |
3273 | all local symbols to be at the head of the list. */ | |
3274 | ||
b34976b6 | 3275 | static bfd_boolean |
217aa764 | 3276 | elf_map_symbols (bfd *abfd) |
252b5132 | 3277 | { |
dc810e39 | 3278 | unsigned int symcount = bfd_get_symcount (abfd); |
252b5132 RH |
3279 | asymbol **syms = bfd_get_outsymbols (abfd); |
3280 | asymbol **sect_syms; | |
dc810e39 AM |
3281 | unsigned int num_locals = 0; |
3282 | unsigned int num_globals = 0; | |
3283 | unsigned int num_locals2 = 0; | |
3284 | unsigned int num_globals2 = 0; | |
252b5132 | 3285 | int max_index = 0; |
dc810e39 | 3286 | unsigned int idx; |
252b5132 RH |
3287 | asection *asect; |
3288 | asymbol **new_syms; | |
252b5132 RH |
3289 | |
3290 | #ifdef DEBUG | |
3291 | fprintf (stderr, "elf_map_symbols\n"); | |
3292 | fflush (stderr); | |
3293 | #endif | |
3294 | ||
252b5132 RH |
3295 | for (asect = abfd->sections; asect; asect = asect->next) |
3296 | { | |
3297 | if (max_index < asect->index) | |
3298 | max_index = asect->index; | |
3299 | } | |
3300 | ||
3301 | max_index++; | |
a50b1753 | 3302 | sect_syms = (asymbol **) bfd_zalloc2 (abfd, max_index, sizeof (asymbol *)); |
252b5132 | 3303 | if (sect_syms == NULL) |
b34976b6 | 3304 | return FALSE; |
252b5132 | 3305 | elf_section_syms (abfd) = sect_syms; |
4e89ac30 | 3306 | elf_num_section_syms (abfd) = max_index; |
252b5132 | 3307 | |
079e9a2f AM |
3308 | /* Init sect_syms entries for any section symbols we have already |
3309 | decided to output. */ | |
252b5132 RH |
3310 | for (idx = 0; idx < symcount; idx++) |
3311 | { | |
dc810e39 | 3312 | asymbol *sym = syms[idx]; |
c044fabd | 3313 | |
252b5132 | 3314 | if ((sym->flags & BSF_SECTION_SYM) != 0 |
0f0a5e58 | 3315 | && sym->value == 0 |
2633a79c AM |
3316 | && !ignore_section_sym (abfd, sym) |
3317 | && !bfd_is_abs_section (sym->section)) | |
252b5132 | 3318 | { |
5372391b | 3319 | asection *sec = sym->section; |
252b5132 | 3320 | |
5372391b AM |
3321 | if (sec->owner != abfd) |
3322 | sec = sec->output_section; | |
252b5132 | 3323 | |
5372391b | 3324 | sect_syms[sec->index] = syms[idx]; |
252b5132 RH |
3325 | } |
3326 | } | |
3327 | ||
252b5132 RH |
3328 | /* Classify all of the symbols. */ |
3329 | for (idx = 0; idx < symcount; idx++) | |
3330 | { | |
2633a79c | 3331 | if (sym_is_global (abfd, syms[idx])) |
252b5132 | 3332 | num_globals++; |
2633a79c AM |
3333 | else if (!ignore_section_sym (abfd, syms[idx])) |
3334 | num_locals++; | |
252b5132 | 3335 | } |
079e9a2f | 3336 | |
5372391b | 3337 | /* We will be adding a section symbol for each normal BFD section. Most |
079e9a2f AM |
3338 | sections will already have a section symbol in outsymbols, but |
3339 | eg. SHT_GROUP sections will not, and we need the section symbol mapped | |
3340 | at least in that case. */ | |
252b5132 RH |
3341 | for (asect = abfd->sections; asect; asect = asect->next) |
3342 | { | |
079e9a2f | 3343 | if (sect_syms[asect->index] == NULL) |
252b5132 | 3344 | { |
079e9a2f | 3345 | if (!sym_is_global (abfd, asect->symbol)) |
252b5132 RH |
3346 | num_locals++; |
3347 | else | |
3348 | num_globals++; | |
252b5132 RH |
3349 | } |
3350 | } | |
3351 | ||
3352 | /* Now sort the symbols so the local symbols are first. */ | |
a50b1753 NC |
3353 | new_syms = (asymbol **) bfd_alloc2 (abfd, num_locals + num_globals, |
3354 | sizeof (asymbol *)); | |
dc810e39 | 3355 | |
252b5132 | 3356 | if (new_syms == NULL) |
b34976b6 | 3357 | return FALSE; |
252b5132 RH |
3358 | |
3359 | for (idx = 0; idx < symcount; idx++) | |
3360 | { | |
3361 | asymbol *sym = syms[idx]; | |
dc810e39 | 3362 | unsigned int i; |
252b5132 | 3363 | |
2633a79c AM |
3364 | if (sym_is_global (abfd, sym)) |
3365 | i = num_locals + num_globals2++; | |
3366 | else if (!ignore_section_sym (abfd, sym)) | |
252b5132 RH |
3367 | i = num_locals2++; |
3368 | else | |
2633a79c | 3369 | continue; |
252b5132 RH |
3370 | new_syms[i] = sym; |
3371 | sym->udata.i = i + 1; | |
3372 | } | |
3373 | for (asect = abfd->sections; asect; asect = asect->next) | |
3374 | { | |
079e9a2f | 3375 | if (sect_syms[asect->index] == NULL) |
252b5132 | 3376 | { |
079e9a2f | 3377 | asymbol *sym = asect->symbol; |
dc810e39 | 3378 | unsigned int i; |
252b5132 | 3379 | |
079e9a2f | 3380 | sect_syms[asect->index] = sym; |
252b5132 RH |
3381 | if (!sym_is_global (abfd, sym)) |
3382 | i = num_locals2++; | |
3383 | else | |
3384 | i = num_locals + num_globals2++; | |
3385 | new_syms[i] = sym; | |
3386 | sym->udata.i = i + 1; | |
3387 | } | |
3388 | } | |
3389 | ||
3390 | bfd_set_symtab (abfd, new_syms, num_locals + num_globals); | |
3391 | ||
3392 | elf_num_locals (abfd) = num_locals; | |
3393 | elf_num_globals (abfd) = num_globals; | |
b34976b6 | 3394 | return TRUE; |
252b5132 RH |
3395 | } |
3396 | ||
3397 | /* Align to the maximum file alignment that could be required for any | |
3398 | ELF data structure. */ | |
3399 | ||
268b6b39 | 3400 | static inline file_ptr |
217aa764 | 3401 | align_file_position (file_ptr off, int align) |
252b5132 RH |
3402 | { |
3403 | return (off + align - 1) & ~(align - 1); | |
3404 | } | |
3405 | ||
3406 | /* Assign a file position to a section, optionally aligning to the | |
3407 | required section alignment. */ | |
3408 | ||
217aa764 AM |
3409 | file_ptr |
3410 | _bfd_elf_assign_file_position_for_section (Elf_Internal_Shdr *i_shdrp, | |
3411 | file_ptr offset, | |
3412 | bfd_boolean align) | |
252b5132 | 3413 | { |
72de5009 AM |
3414 | if (align && i_shdrp->sh_addralign > 1) |
3415 | offset = BFD_ALIGN (offset, i_shdrp->sh_addralign); | |
252b5132 RH |
3416 | i_shdrp->sh_offset = offset; |
3417 | if (i_shdrp->bfd_section != NULL) | |
3418 | i_shdrp->bfd_section->filepos = offset; | |
3419 | if (i_shdrp->sh_type != SHT_NOBITS) | |
3420 | offset += i_shdrp->sh_size; | |
3421 | return offset; | |
3422 | } | |
3423 | ||
3424 | /* Compute the file positions we are going to put the sections at, and | |
3425 | otherwise prepare to begin writing out the ELF file. If LINK_INFO | |
3426 | is not NULL, this is being called by the ELF backend linker. */ | |
3427 | ||
b34976b6 | 3428 | bfd_boolean |
217aa764 AM |
3429 | _bfd_elf_compute_section_file_positions (bfd *abfd, |
3430 | struct bfd_link_info *link_info) | |
252b5132 | 3431 | { |
9c5bfbb7 | 3432 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
d4730f92 | 3433 | struct fake_section_arg fsargs; |
b34976b6 | 3434 | bfd_boolean failed; |
4b6c0f2f | 3435 | struct bfd_strtab_hash *strtab = NULL; |
252b5132 | 3436 | Elf_Internal_Shdr *shstrtab_hdr; |
3516e984 | 3437 | bfd_boolean need_symtab; |
252b5132 RH |
3438 | |
3439 | if (abfd->output_has_begun) | |
b34976b6 | 3440 | return TRUE; |
252b5132 RH |
3441 | |
3442 | /* Do any elf backend specific processing first. */ | |
3443 | if (bed->elf_backend_begin_write_processing) | |
3444 | (*bed->elf_backend_begin_write_processing) (abfd, link_info); | |
3445 | ||
3446 | if (! prep_headers (abfd)) | |
b34976b6 | 3447 | return FALSE; |
252b5132 | 3448 | |
e6c51ed4 NC |
3449 | /* Post process the headers if necessary. */ |
3450 | if (bed->elf_backend_post_process_headers) | |
3451 | (*bed->elf_backend_post_process_headers) (abfd, link_info); | |
3452 | ||
d4730f92 BS |
3453 | fsargs.failed = FALSE; |
3454 | fsargs.link_info = link_info; | |
3455 | bfd_map_over_sections (abfd, elf_fake_sections, &fsargs); | |
3456 | if (fsargs.failed) | |
b34976b6 | 3457 | return FALSE; |
252b5132 | 3458 | |
da9f89d4 | 3459 | if (!assign_section_numbers (abfd, link_info)) |
b34976b6 | 3460 | return FALSE; |
252b5132 RH |
3461 | |
3462 | /* The backend linker builds symbol table information itself. */ | |
3516e984 L |
3463 | need_symtab = (link_info == NULL |
3464 | && (bfd_get_symcount (abfd) > 0 | |
3465 | || ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC)) | |
3466 | == HAS_RELOC))); | |
3467 | if (need_symtab) | |
252b5132 RH |
3468 | { |
3469 | /* Non-zero if doing a relocatable link. */ | |
3470 | int relocatable_p = ! (abfd->flags & (EXEC_P | DYNAMIC)); | |
3471 | ||
3472 | if (! swap_out_syms (abfd, &strtab, relocatable_p)) | |
b34976b6 | 3473 | return FALSE; |
252b5132 RH |
3474 | } |
3475 | ||
d4730f92 | 3476 | failed = FALSE; |
1126897b | 3477 | if (link_info == NULL) |
dbb410c3 | 3478 | { |
1126897b | 3479 | bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed); |
dbb410c3 | 3480 | if (failed) |
b34976b6 | 3481 | return FALSE; |
dbb410c3 AM |
3482 | } |
3483 | ||
252b5132 RH |
3484 | shstrtab_hdr = &elf_tdata (abfd)->shstrtab_hdr; |
3485 | /* sh_name was set in prep_headers. */ | |
3486 | shstrtab_hdr->sh_type = SHT_STRTAB; | |
3487 | shstrtab_hdr->sh_flags = 0; | |
3488 | shstrtab_hdr->sh_addr = 0; | |
2b0f7ef9 | 3489 | shstrtab_hdr->sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd)); |
252b5132 RH |
3490 | shstrtab_hdr->sh_entsize = 0; |
3491 | shstrtab_hdr->sh_link = 0; | |
3492 | shstrtab_hdr->sh_info = 0; | |
3493 | /* sh_offset is set in assign_file_positions_except_relocs. */ | |
3494 | shstrtab_hdr->sh_addralign = 1; | |
3495 | ||
c84fca4d | 3496 | if (!assign_file_positions_except_relocs (abfd, link_info)) |
b34976b6 | 3497 | return FALSE; |
252b5132 | 3498 | |
3516e984 | 3499 | if (need_symtab) |
252b5132 RH |
3500 | { |
3501 | file_ptr off; | |
3502 | Elf_Internal_Shdr *hdr; | |
3503 | ||
3504 | off = elf_tdata (abfd)->next_file_pos; | |
3505 | ||
3506 | hdr = &elf_tdata (abfd)->symtab_hdr; | |
b34976b6 | 3507 | off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE); |
252b5132 | 3508 | |
9ad5cbcf AM |
3509 | hdr = &elf_tdata (abfd)->symtab_shndx_hdr; |
3510 | if (hdr->sh_size != 0) | |
b34976b6 | 3511 | off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE); |
9ad5cbcf | 3512 | |
252b5132 | 3513 | hdr = &elf_tdata (abfd)->strtab_hdr; |
b34976b6 | 3514 | off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE); |
252b5132 RH |
3515 | |
3516 | elf_tdata (abfd)->next_file_pos = off; | |
3517 | ||
3518 | /* Now that we know where the .strtab section goes, write it | |
08a40648 | 3519 | out. */ |
252b5132 RH |
3520 | if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0 |
3521 | || ! _bfd_stringtab_emit (abfd, strtab)) | |
b34976b6 | 3522 | return FALSE; |
252b5132 RH |
3523 | _bfd_stringtab_free (strtab); |
3524 | } | |
3525 | ||
b34976b6 | 3526 | abfd->output_has_begun = TRUE; |
252b5132 | 3527 | |
b34976b6 | 3528 | return TRUE; |
252b5132 RH |
3529 | } |
3530 | ||
8ded5a0f AM |
3531 | /* Make an initial estimate of the size of the program header. If we |
3532 | get the number wrong here, we'll redo section placement. */ | |
3533 | ||
3534 | static bfd_size_type | |
3535 | get_program_header_size (bfd *abfd, struct bfd_link_info *info) | |
3536 | { | |
3537 | size_t segs; | |
3538 | asection *s; | |
2b05f1b7 | 3539 | const struct elf_backend_data *bed; |
8ded5a0f AM |
3540 | |
3541 | /* Assume we will need exactly two PT_LOAD segments: one for text | |
3542 | and one for data. */ | |
3543 | segs = 2; | |
3544 | ||
3545 | s = bfd_get_section_by_name (abfd, ".interp"); | |
3546 | if (s != NULL && (s->flags & SEC_LOAD) != 0) | |
3547 | { | |
3548 | /* If we have a loadable interpreter section, we need a | |
3549 | PT_INTERP segment. In this case, assume we also need a | |
3550 | PT_PHDR segment, although that may not be true for all | |
3551 | targets. */ | |
3552 | segs += 2; | |
3553 | } | |
3554 | ||
3555 | if (bfd_get_section_by_name (abfd, ".dynamic") != NULL) | |
3556 | { | |
3557 | /* We need a PT_DYNAMIC segment. */ | |
3558 | ++segs; | |
f210dcff | 3559 | } |
08a40648 | 3560 | |
ceae84aa | 3561 | if (info != NULL && info->relro) |
f210dcff L |
3562 | { |
3563 | /* We need a PT_GNU_RELRO segment. */ | |
3564 | ++segs; | |
8ded5a0f AM |
3565 | } |
3566 | ||
3567 | if (elf_tdata (abfd)->eh_frame_hdr) | |
3568 | { | |
3569 | /* We need a PT_GNU_EH_FRAME segment. */ | |
3570 | ++segs; | |
3571 | } | |
3572 | ||
2b05f1b7 | 3573 | if (elf_tdata (abfd)->stack_flags) |
8ded5a0f | 3574 | { |
2b05f1b7 L |
3575 | /* We need a PT_GNU_STACK segment. */ |
3576 | ++segs; | |
3577 | } | |
94b11780 | 3578 | |
2b05f1b7 L |
3579 | for (s = abfd->sections; s != NULL; s = s->next) |
3580 | { | |
8ded5a0f | 3581 | if ((s->flags & SEC_LOAD) != 0 |
0112cd26 | 3582 | && CONST_STRNEQ (s->name, ".note")) |
8ded5a0f AM |
3583 | { |
3584 | /* We need a PT_NOTE segment. */ | |
3585 | ++segs; | |
1c5265b5 JJ |
3586 | /* Try to create just one PT_NOTE segment |
3587 | for all adjacent loadable .note* sections. | |
3588 | gABI requires that within a PT_NOTE segment | |
3589 | (and also inside of each SHT_NOTE section) | |
3590 | each note is padded to a multiple of 4 size, | |
3591 | so we check whether the sections are correctly | |
3592 | aligned. */ | |
3593 | if (s->alignment_power == 2) | |
3594 | while (s->next != NULL | |
3595 | && s->next->alignment_power == 2 | |
3596 | && (s->next->flags & SEC_LOAD) != 0 | |
3597 | && CONST_STRNEQ (s->next->name, ".note")) | |
3598 | s = s->next; | |
8ded5a0f AM |
3599 | } |
3600 | } | |
3601 | ||
3602 | for (s = abfd->sections; s != NULL; s = s->next) | |
3603 | { | |
3604 | if (s->flags & SEC_THREAD_LOCAL) | |
3605 | { | |
3606 | /* We need a PT_TLS segment. */ | |
3607 | ++segs; | |
3608 | break; | |
3609 | } | |
3610 | } | |
3611 | ||
3612 | /* Let the backend count up any program headers it might need. */ | |
2b05f1b7 | 3613 | bed = get_elf_backend_data (abfd); |
8ded5a0f AM |
3614 | if (bed->elf_backend_additional_program_headers) |
3615 | { | |
3616 | int a; | |
3617 | ||
3618 | a = (*bed->elf_backend_additional_program_headers) (abfd, info); | |
3619 | if (a == -1) | |
3620 | abort (); | |
3621 | segs += a; | |
3622 | } | |
3623 | ||
3624 | return segs * bed->s->sizeof_phdr; | |
3625 | } | |
3626 | ||
2ea37f1c NC |
3627 | /* Find the segment that contains the output_section of section. */ |
3628 | ||
3629 | Elf_Internal_Phdr * | |
3630 | _bfd_elf_find_segment_containing_section (bfd * abfd, asection * section) | |
3631 | { | |
3632 | struct elf_segment_map *m; | |
3633 | Elf_Internal_Phdr *p; | |
3634 | ||
3635 | for (m = elf_tdata (abfd)->segment_map, | |
3636 | p = elf_tdata (abfd)->phdr; | |
3637 | m != NULL; | |
3638 | m = m->next, p++) | |
3639 | { | |
3640 | int i; | |
3641 | ||
3642 | for (i = m->count - 1; i >= 0; i--) | |
3643 | if (m->sections[i] == section) | |
3644 | return p; | |
3645 | } | |
3646 | ||
3647 | return NULL; | |
3648 | } | |
3649 | ||
252b5132 RH |
3650 | /* Create a mapping from a set of sections to a program segment. */ |
3651 | ||
217aa764 AM |
3652 | static struct elf_segment_map * |
3653 | make_mapping (bfd *abfd, | |
3654 | asection **sections, | |
3655 | unsigned int from, | |
3656 | unsigned int to, | |
3657 | bfd_boolean phdr) | |
252b5132 RH |
3658 | { |
3659 | struct elf_segment_map *m; | |
3660 | unsigned int i; | |
3661 | asection **hdrpp; | |
dc810e39 | 3662 | bfd_size_type amt; |
252b5132 | 3663 | |
dc810e39 AM |
3664 | amt = sizeof (struct elf_segment_map); |
3665 | amt += (to - from - 1) * sizeof (asection *); | |
a50b1753 | 3666 | m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); |
252b5132 RH |
3667 | if (m == NULL) |
3668 | return NULL; | |
3669 | m->next = NULL; | |
3670 | m->p_type = PT_LOAD; | |
3671 | for (i = from, hdrpp = sections + from; i < to; i++, hdrpp++) | |
3672 | m->sections[i - from] = *hdrpp; | |
3673 | m->count = to - from; | |
3674 | ||
3675 | if (from == 0 && phdr) | |
3676 | { | |
3677 | /* Include the headers in the first PT_LOAD segment. */ | |
3678 | m->includes_filehdr = 1; | |
3679 | m->includes_phdrs = 1; | |
3680 | } | |
3681 | ||
3682 | return m; | |
3683 | } | |
3684 | ||
229fcec5 MM |
3685 | /* Create the PT_DYNAMIC segment, which includes DYNSEC. Returns NULL |
3686 | on failure. */ | |
3687 | ||
3688 | struct elf_segment_map * | |
3689 | _bfd_elf_make_dynamic_segment (bfd *abfd, asection *dynsec) | |
3690 | { | |
3691 | struct elf_segment_map *m; | |
3692 | ||
a50b1753 NC |
3693 | m = (struct elf_segment_map *) bfd_zalloc (abfd, |
3694 | sizeof (struct elf_segment_map)); | |
229fcec5 MM |
3695 | if (m == NULL) |
3696 | return NULL; | |
3697 | m->next = NULL; | |
3698 | m->p_type = PT_DYNAMIC; | |
3699 | m->count = 1; | |
3700 | m->sections[0] = dynsec; | |
08a40648 | 3701 | |
229fcec5 MM |
3702 | return m; |
3703 | } | |
3704 | ||
8ded5a0f | 3705 | /* Possibly add or remove segments from the segment map. */ |
252b5132 | 3706 | |
b34976b6 | 3707 | static bfd_boolean |
3dea8fca AM |
3708 | elf_modify_segment_map (bfd *abfd, |
3709 | struct bfd_link_info *info, | |
3710 | bfd_boolean remove_empty_load) | |
252b5132 | 3711 | { |
252e386e | 3712 | struct elf_segment_map **m; |
8ded5a0f | 3713 | const struct elf_backend_data *bed; |
252b5132 | 3714 | |
8ded5a0f AM |
3715 | /* The placement algorithm assumes that non allocated sections are |
3716 | not in PT_LOAD segments. We ensure this here by removing such | |
3717 | sections from the segment map. We also remove excluded | |
252e386e AM |
3718 | sections. Finally, any PT_LOAD segment without sections is |
3719 | removed. */ | |
3720 | m = &elf_tdata (abfd)->segment_map; | |
3721 | while (*m) | |
8ded5a0f AM |
3722 | { |
3723 | unsigned int i, new_count; | |
252b5132 | 3724 | |
252e386e | 3725 | for (new_count = 0, i = 0; i < (*m)->count; i++) |
8ded5a0f | 3726 | { |
252e386e AM |
3727 | if (((*m)->sections[i]->flags & SEC_EXCLUDE) == 0 |
3728 | && (((*m)->sections[i]->flags & SEC_ALLOC) != 0 | |
3729 | || (*m)->p_type != PT_LOAD)) | |
8ded5a0f | 3730 | { |
252e386e AM |
3731 | (*m)->sections[new_count] = (*m)->sections[i]; |
3732 | new_count++; | |
8ded5a0f AM |
3733 | } |
3734 | } | |
252e386e | 3735 | (*m)->count = new_count; |
252b5132 | 3736 | |
3dea8fca | 3737 | if (remove_empty_load && (*m)->p_type == PT_LOAD && (*m)->count == 0) |
252e386e AM |
3738 | *m = (*m)->next; |
3739 | else | |
3740 | m = &(*m)->next; | |
8ded5a0f | 3741 | } |
252b5132 | 3742 | |
8ded5a0f AM |
3743 | bed = get_elf_backend_data (abfd); |
3744 | if (bed->elf_backend_modify_segment_map != NULL) | |
252b5132 | 3745 | { |
252e386e | 3746 | if (!(*bed->elf_backend_modify_segment_map) (abfd, info)) |
8ded5a0f | 3747 | return FALSE; |
252b5132 | 3748 | } |
252b5132 | 3749 | |
8ded5a0f AM |
3750 | return TRUE; |
3751 | } | |
252b5132 | 3752 | |
8ded5a0f | 3753 | /* Set up a mapping from BFD sections to program segments. */ |
252b5132 | 3754 | |
8ded5a0f AM |
3755 | bfd_boolean |
3756 | _bfd_elf_map_sections_to_segments (bfd *abfd, struct bfd_link_info *info) | |
3757 | { | |
3758 | unsigned int count; | |
3759 | struct elf_segment_map *m; | |
3760 | asection **sections = NULL; | |
3761 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
3dea8fca | 3762 | bfd_boolean no_user_phdrs; |
252b5132 | 3763 | |
3dea8fca | 3764 | no_user_phdrs = elf_tdata (abfd)->segment_map == NULL; |
d324f6d6 RM |
3765 | |
3766 | if (info != NULL) | |
3767 | info->user_phdrs = !no_user_phdrs; | |
3768 | ||
3dea8fca | 3769 | if (no_user_phdrs && bfd_count_sections (abfd) != 0) |
252b5132 | 3770 | { |
8ded5a0f AM |
3771 | asection *s; |
3772 | unsigned int i; | |
3773 | struct elf_segment_map *mfirst; | |
3774 | struct elf_segment_map **pm; | |
3775 | asection *last_hdr; | |
3776 | bfd_vma last_size; | |
3777 | unsigned int phdr_index; | |
3778 | bfd_vma maxpagesize; | |
3779 | asection **hdrpp; | |
3780 | bfd_boolean phdr_in_segment = TRUE; | |
3781 | bfd_boolean writable; | |
3782 | int tls_count = 0; | |
3783 | asection *first_tls = NULL; | |
3784 | asection *dynsec, *eh_frame_hdr; | |
3785 | bfd_size_type amt; | |
8d06853e | 3786 | bfd_vma addr_mask, wrap_to = 0; |
252b5132 | 3787 | |
8ded5a0f | 3788 | /* Select the allocated sections, and sort them. */ |
252b5132 | 3789 | |
a50b1753 NC |
3790 | sections = (asection **) bfd_malloc2 (bfd_count_sections (abfd), |
3791 | sizeof (asection *)); | |
8ded5a0f | 3792 | if (sections == NULL) |
252b5132 | 3793 | goto error_return; |
252b5132 | 3794 | |
8d06853e AM |
3795 | /* Calculate top address, avoiding undefined behaviour of shift |
3796 | left operator when shift count is equal to size of type | |
3797 | being shifted. */ | |
3798 | addr_mask = ((bfd_vma) 1 << (bfd_arch_bits_per_address (abfd) - 1)) - 1; | |
3799 | addr_mask = (addr_mask << 1) + 1; | |
3800 | ||
8ded5a0f AM |
3801 | i = 0; |
3802 | for (s = abfd->sections; s != NULL; s = s->next) | |
3803 | { | |
3804 | if ((s->flags & SEC_ALLOC) != 0) | |
3805 | { | |
3806 | sections[i] = s; | |
3807 | ++i; | |
8d06853e AM |
3808 | /* A wrapping section potentially clashes with header. */ |
3809 | if (((s->lma + s->size) & addr_mask) < (s->lma & addr_mask)) | |
3810 | wrap_to = (s->lma + s->size) & addr_mask; | |
8ded5a0f AM |
3811 | } |
3812 | } | |
3813 | BFD_ASSERT (i <= bfd_count_sections (abfd)); | |
3814 | count = i; | |
252b5132 | 3815 | |
8ded5a0f | 3816 | qsort (sections, (size_t) count, sizeof (asection *), elf_sort_sections); |
252b5132 | 3817 | |
8ded5a0f | 3818 | /* Build the mapping. */ |
252b5132 | 3819 | |
8ded5a0f AM |
3820 | mfirst = NULL; |
3821 | pm = &mfirst; | |
252b5132 | 3822 | |
8ded5a0f AM |
3823 | /* If we have a .interp section, then create a PT_PHDR segment for |
3824 | the program headers and a PT_INTERP segment for the .interp | |
3825 | section. */ | |
3826 | s = bfd_get_section_by_name (abfd, ".interp"); | |
3827 | if (s != NULL && (s->flags & SEC_LOAD) != 0) | |
3828 | { | |
3829 | amt = sizeof (struct elf_segment_map); | |
a50b1753 | 3830 | m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); |
8ded5a0f AM |
3831 | if (m == NULL) |
3832 | goto error_return; | |
3833 | m->next = NULL; | |
3834 | m->p_type = PT_PHDR; | |
3835 | /* FIXME: UnixWare and Solaris set PF_X, Irix 5 does not. */ | |
3836 | m->p_flags = PF_R | PF_X; | |
3837 | m->p_flags_valid = 1; | |
3838 | m->includes_phdrs = 1; | |
252b5132 | 3839 | |
8ded5a0f AM |
3840 | *pm = m; |
3841 | pm = &m->next; | |
252b5132 | 3842 | |
8ded5a0f | 3843 | amt = sizeof (struct elf_segment_map); |
a50b1753 | 3844 | m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); |
8ded5a0f AM |
3845 | if (m == NULL) |
3846 | goto error_return; | |
3847 | m->next = NULL; | |
3848 | m->p_type = PT_INTERP; | |
3849 | m->count = 1; | |
3850 | m->sections[0] = s; | |
3851 | ||
3852 | *pm = m; | |
3853 | pm = &m->next; | |
252b5132 | 3854 | } |
8ded5a0f AM |
3855 | |
3856 | /* Look through the sections. We put sections in the same program | |
3857 | segment when the start of the second section can be placed within | |
3858 | a few bytes of the end of the first section. */ | |
3859 | last_hdr = NULL; | |
3860 | last_size = 0; | |
3861 | phdr_index = 0; | |
3862 | maxpagesize = bed->maxpagesize; | |
3863 | writable = FALSE; | |
3864 | dynsec = bfd_get_section_by_name (abfd, ".dynamic"); | |
3865 | if (dynsec != NULL | |
3866 | && (dynsec->flags & SEC_LOAD) == 0) | |
3867 | dynsec = NULL; | |
3868 | ||
3869 | /* Deal with -Ttext or something similar such that the first section | |
3870 | is not adjacent to the program headers. This is an | |
3871 | approximation, since at this point we don't know exactly how many | |
3872 | program headers we will need. */ | |
3873 | if (count > 0) | |
252b5132 | 3874 | { |
8ded5a0f AM |
3875 | bfd_size_type phdr_size = elf_tdata (abfd)->program_header_size; |
3876 | ||
62d7a5f6 | 3877 | if (phdr_size == (bfd_size_type) -1) |
8ded5a0f | 3878 | phdr_size = get_program_header_size (abfd, info); |
d2bcb0d1 | 3879 | phdr_size += bed->s->sizeof_ehdr; |
8ded5a0f | 3880 | if ((abfd->flags & D_PAGED) == 0 |
8d06853e AM |
3881 | || (sections[0]->lma & addr_mask) < phdr_size |
3882 | || ((sections[0]->lma & addr_mask) % maxpagesize | |
3883 | < phdr_size % maxpagesize) | |
3884 | || (sections[0]->lma & addr_mask & -maxpagesize) < wrap_to) | |
8ded5a0f | 3885 | phdr_in_segment = FALSE; |
252b5132 RH |
3886 | } |
3887 | ||
8ded5a0f | 3888 | for (i = 0, hdrpp = sections; i < count; i++, hdrpp++) |
252b5132 | 3889 | { |
8ded5a0f AM |
3890 | asection *hdr; |
3891 | bfd_boolean new_segment; | |
3892 | ||
3893 | hdr = *hdrpp; | |
3894 | ||
3895 | /* See if this section and the last one will fit in the same | |
3896 | segment. */ | |
3897 | ||
3898 | if (last_hdr == NULL) | |
3899 | { | |
3900 | /* If we don't have a segment yet, then we don't need a new | |
3901 | one (we build the last one after this loop). */ | |
3902 | new_segment = FALSE; | |
3903 | } | |
3904 | else if (last_hdr->lma - last_hdr->vma != hdr->lma - hdr->vma) | |
3905 | { | |
3906 | /* If this section has a different relation between the | |
3907 | virtual address and the load address, then we need a new | |
3908 | segment. */ | |
3909 | new_segment = TRUE; | |
3910 | } | |
b5599592 AM |
3911 | else if (hdr->lma < last_hdr->lma + last_size |
3912 | || last_hdr->lma + last_size < last_hdr->lma) | |
3913 | { | |
3914 | /* If this section has a load address that makes it overlap | |
3915 | the previous section, then we need a new segment. */ | |
3916 | new_segment = TRUE; | |
3917 | } | |
39948a60 NC |
3918 | /* In the next test we have to be careful when last_hdr->lma is close |
3919 | to the end of the address space. If the aligned address wraps | |
3920 | around to the start of the address space, then there are no more | |
3921 | pages left in memory and it is OK to assume that the current | |
3922 | section can be included in the current segment. */ | |
3923 | else if ((BFD_ALIGN (last_hdr->lma + last_size, maxpagesize) + maxpagesize | |
3924 | > last_hdr->lma) | |
3925 | && (BFD_ALIGN (last_hdr->lma + last_size, maxpagesize) + maxpagesize | |
4ff73856 | 3926 | <= hdr->lma)) |
8ded5a0f AM |
3927 | { |
3928 | /* If putting this section in this segment would force us to | |
3929 | skip a page in the segment, then we need a new segment. */ | |
3930 | new_segment = TRUE; | |
3931 | } | |
3932 | else if ((last_hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0 | |
3933 | && (hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) != 0) | |
3934 | { | |
3935 | /* We don't want to put a loadable section after a | |
3936 | nonloadable section in the same segment. | |
3937 | Consider .tbss sections as loadable for this purpose. */ | |
3938 | new_segment = TRUE; | |
3939 | } | |
3940 | else if ((abfd->flags & D_PAGED) == 0) | |
3941 | { | |
3942 | /* If the file is not demand paged, which means that we | |
3943 | don't require the sections to be correctly aligned in the | |
3944 | file, then there is no other reason for a new segment. */ | |
3945 | new_segment = FALSE; | |
3946 | } | |
3947 | else if (! writable | |
3948 | && (hdr->flags & SEC_READONLY) == 0 | |
8d06853e AM |
3949 | && (((last_hdr->lma + last_size - 1) & -maxpagesize) |
3950 | != (hdr->lma & -maxpagesize))) | |
8ded5a0f AM |
3951 | { |
3952 | /* We don't want to put a writable section in a read only | |
3953 | segment, unless they are on the same page in memory | |
3954 | anyhow. We already know that the last section does not | |
3955 | bring us past the current section on the page, so the | |
3956 | only case in which the new section is not on the same | |
3957 | page as the previous section is when the previous section | |
3958 | ends precisely on a page boundary. */ | |
3959 | new_segment = TRUE; | |
3960 | } | |
3961 | else | |
3962 | { | |
3963 | /* Otherwise, we can use the same segment. */ | |
3964 | new_segment = FALSE; | |
3965 | } | |
3966 | ||
2889e75b | 3967 | /* Allow interested parties a chance to override our decision. */ |
ceae84aa AM |
3968 | if (last_hdr != NULL |
3969 | && info != NULL | |
3970 | && info->callbacks->override_segment_assignment != NULL) | |
3971 | new_segment | |
3972 | = info->callbacks->override_segment_assignment (info, abfd, hdr, | |
3973 | last_hdr, | |
3974 | new_segment); | |
2889e75b | 3975 | |
8ded5a0f AM |
3976 | if (! new_segment) |
3977 | { | |
3978 | if ((hdr->flags & SEC_READONLY) == 0) | |
3979 | writable = TRUE; | |
3980 | last_hdr = hdr; | |
3981 | /* .tbss sections effectively have zero size. */ | |
3982 | if ((hdr->flags & (SEC_THREAD_LOCAL | SEC_LOAD)) | |
3983 | != SEC_THREAD_LOCAL) | |
3984 | last_size = hdr->size; | |
3985 | else | |
3986 | last_size = 0; | |
3987 | continue; | |
3988 | } | |
3989 | ||
3990 | /* We need a new program segment. We must create a new program | |
3991 | header holding all the sections from phdr_index until hdr. */ | |
3992 | ||
3993 | m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment); | |
3994 | if (m == NULL) | |
3995 | goto error_return; | |
3996 | ||
3997 | *pm = m; | |
3998 | pm = &m->next; | |
3999 | ||
252b5132 | 4000 | if ((hdr->flags & SEC_READONLY) == 0) |
b34976b6 | 4001 | writable = TRUE; |
8ded5a0f AM |
4002 | else |
4003 | writable = FALSE; | |
4004 | ||
baaff79e JJ |
4005 | last_hdr = hdr; |
4006 | /* .tbss sections effectively have zero size. */ | |
e5caec89 | 4007 | if ((hdr->flags & (SEC_THREAD_LOCAL | SEC_LOAD)) != SEC_THREAD_LOCAL) |
eea6121a | 4008 | last_size = hdr->size; |
baaff79e JJ |
4009 | else |
4010 | last_size = 0; | |
8ded5a0f AM |
4011 | phdr_index = i; |
4012 | phdr_in_segment = FALSE; | |
252b5132 RH |
4013 | } |
4014 | ||
86b2281f AM |
4015 | /* Create a final PT_LOAD program segment, but not if it's just |
4016 | for .tbss. */ | |
4017 | if (last_hdr != NULL | |
4018 | && (i - phdr_index != 1 | |
4019 | || ((last_hdr->flags & (SEC_THREAD_LOCAL | SEC_LOAD)) | |
4020 | != SEC_THREAD_LOCAL))) | |
8ded5a0f AM |
4021 | { |
4022 | m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment); | |
4023 | if (m == NULL) | |
4024 | goto error_return; | |
252b5132 | 4025 | |
8ded5a0f AM |
4026 | *pm = m; |
4027 | pm = &m->next; | |
4028 | } | |
252b5132 | 4029 | |
8ded5a0f AM |
4030 | /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */ |
4031 | if (dynsec != NULL) | |
4032 | { | |
4033 | m = _bfd_elf_make_dynamic_segment (abfd, dynsec); | |
4034 | if (m == NULL) | |
4035 | goto error_return; | |
4036 | *pm = m; | |
4037 | pm = &m->next; | |
4038 | } | |
252b5132 | 4039 | |
1c5265b5 JJ |
4040 | /* For each batch of consecutive loadable .note sections, |
4041 | add a PT_NOTE segment. We don't use bfd_get_section_by_name, | |
4042 | because if we link together nonloadable .note sections and | |
4043 | loadable .note sections, we will generate two .note sections | |
4044 | in the output file. FIXME: Using names for section types is | |
4045 | bogus anyhow. */ | |
8ded5a0f AM |
4046 | for (s = abfd->sections; s != NULL; s = s->next) |
4047 | { | |
4048 | if ((s->flags & SEC_LOAD) != 0 | |
0112cd26 | 4049 | && CONST_STRNEQ (s->name, ".note")) |
8ded5a0f | 4050 | { |
1c5265b5 | 4051 | asection *s2; |
91d6fa6a NC |
4052 | |
4053 | count = 1; | |
8ded5a0f | 4054 | amt = sizeof (struct elf_segment_map); |
1c5265b5 JJ |
4055 | if (s->alignment_power == 2) |
4056 | for (s2 = s; s2->next != NULL; s2 = s2->next) | |
55b581a6 JJ |
4057 | { |
4058 | if (s2->next->alignment_power == 2 | |
4059 | && (s2->next->flags & SEC_LOAD) != 0 | |
4060 | && CONST_STRNEQ (s2->next->name, ".note") | |
8d06853e AM |
4061 | && align_power (s2->lma + s2->size, 2) |
4062 | == s2->next->lma) | |
55b581a6 JJ |
4063 | count++; |
4064 | else | |
4065 | break; | |
4066 | } | |
1c5265b5 | 4067 | amt += (count - 1) * sizeof (asection *); |
a50b1753 | 4068 | m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); |
8ded5a0f AM |
4069 | if (m == NULL) |
4070 | goto error_return; | |
4071 | m->next = NULL; | |
4072 | m->p_type = PT_NOTE; | |
1c5265b5 JJ |
4073 | m->count = count; |
4074 | while (count > 1) | |
4075 | { | |
4076 | m->sections[m->count - count--] = s; | |
4077 | BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0); | |
4078 | s = s->next; | |
4079 | } | |
4080 | m->sections[m->count - 1] = s; | |
4081 | BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0); | |
8ded5a0f AM |
4082 | *pm = m; |
4083 | pm = &m->next; | |
4084 | } | |
4085 | if (s->flags & SEC_THREAD_LOCAL) | |
4086 | { | |
4087 | if (! tls_count) | |
4088 | first_tls = s; | |
4089 | tls_count++; | |
4090 | } | |
4091 | } | |
252b5132 | 4092 | |
8ded5a0f AM |
4093 | /* If there are any SHF_TLS output sections, add PT_TLS segment. */ |
4094 | if (tls_count > 0) | |
4095 | { | |
8ded5a0f AM |
4096 | amt = sizeof (struct elf_segment_map); |
4097 | amt += (tls_count - 1) * sizeof (asection *); | |
a50b1753 | 4098 | m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); |
8ded5a0f AM |
4099 | if (m == NULL) |
4100 | goto error_return; | |
4101 | m->next = NULL; | |
4102 | m->p_type = PT_TLS; | |
4103 | m->count = tls_count; | |
4104 | /* Mandated PF_R. */ | |
4105 | m->p_flags = PF_R; | |
4106 | m->p_flags_valid = 1; | |
91d6fa6a | 4107 | for (i = 0; i < (unsigned int) tls_count; ++i) |
8ded5a0f AM |
4108 | { |
4109 | BFD_ASSERT (first_tls->flags & SEC_THREAD_LOCAL); | |
4110 | m->sections[i] = first_tls; | |
4111 | first_tls = first_tls->next; | |
4112 | } | |
252b5132 | 4113 | |
8ded5a0f AM |
4114 | *pm = m; |
4115 | pm = &m->next; | |
4116 | } | |
252b5132 | 4117 | |
8ded5a0f AM |
4118 | /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME |
4119 | segment. */ | |
4120 | eh_frame_hdr = elf_tdata (abfd)->eh_frame_hdr; | |
4121 | if (eh_frame_hdr != NULL | |
4122 | && (eh_frame_hdr->output_section->flags & SEC_LOAD) != 0) | |
252b5132 | 4123 | { |
dc810e39 | 4124 | amt = sizeof (struct elf_segment_map); |
a50b1753 | 4125 | m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); |
252b5132 RH |
4126 | if (m == NULL) |
4127 | goto error_return; | |
4128 | m->next = NULL; | |
8ded5a0f | 4129 | m->p_type = PT_GNU_EH_FRAME; |
252b5132 | 4130 | m->count = 1; |
8ded5a0f | 4131 | m->sections[0] = eh_frame_hdr->output_section; |
252b5132 RH |
4132 | |
4133 | *pm = m; | |
4134 | pm = &m->next; | |
4135 | } | |
13ae64f3 | 4136 | |
8ded5a0f | 4137 | if (elf_tdata (abfd)->stack_flags) |
13ae64f3 | 4138 | { |
8ded5a0f | 4139 | amt = sizeof (struct elf_segment_map); |
a50b1753 | 4140 | m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); |
8ded5a0f AM |
4141 | if (m == NULL) |
4142 | goto error_return; | |
4143 | m->next = NULL; | |
2b05f1b7 | 4144 | m->p_type = PT_GNU_STACK; |
8ded5a0f | 4145 | m->p_flags = elf_tdata (abfd)->stack_flags; |
04c3a755 | 4146 | m->p_align = bed->stack_align; |
8ded5a0f | 4147 | m->p_flags_valid = 1; |
04c3a755 NS |
4148 | m->p_align_valid = m->p_align != 0; |
4149 | if (info->stacksize > 0) | |
4150 | { | |
4151 | m->p_size = info->stacksize; | |
4152 | m->p_size_valid = 1; | |
4153 | } | |
252b5132 | 4154 | |
8ded5a0f AM |
4155 | *pm = m; |
4156 | pm = &m->next; | |
4157 | } | |
65765700 | 4158 | |
ceae84aa | 4159 | if (info != NULL && info->relro) |
8ded5a0f | 4160 | { |
f210dcff L |
4161 | for (m = mfirst; m != NULL; m = m->next) |
4162 | { | |
3832a4d8 AM |
4163 | if (m->p_type == PT_LOAD |
4164 | && m->count != 0 | |
4165 | && m->sections[0]->vma >= info->relro_start | |
4166 | && m->sections[0]->vma < info->relro_end) | |
f210dcff | 4167 | { |
3832a4d8 AM |
4168 | i = m->count; |
4169 | while (--i != (unsigned) -1) | |
4170 | if ((m->sections[i]->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) | |
4171 | == (SEC_LOAD | SEC_HAS_CONTENTS)) | |
4172 | break; | |
4173 | ||
4174 | if (i == (unsigned) -1) | |
4175 | continue; | |
65765700 | 4176 | |
3832a4d8 AM |
4177 | if (m->sections[i]->vma + m->sections[i]->size |
4178 | >= info->relro_end) | |
f210dcff L |
4179 | break; |
4180 | } | |
be01b344 | 4181 | } |
f210dcff L |
4182 | |
4183 | /* Make a PT_GNU_RELRO segment only when it isn't empty. */ | |
4184 | if (m != NULL) | |
4185 | { | |
4186 | amt = sizeof (struct elf_segment_map); | |
a50b1753 | 4187 | m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); |
f210dcff L |
4188 | if (m == NULL) |
4189 | goto error_return; | |
4190 | m->next = NULL; | |
4191 | m->p_type = PT_GNU_RELRO; | |
4192 | m->p_flags = PF_R; | |
4193 | m->p_flags_valid = 1; | |
4194 | ||
4195 | *pm = m; | |
4196 | pm = &m->next; | |
4197 | } | |
8ded5a0f | 4198 | } |
9ee5e499 | 4199 | |
8ded5a0f AM |
4200 | free (sections); |
4201 | elf_tdata (abfd)->segment_map = mfirst; | |
9ee5e499 JJ |
4202 | } |
4203 | ||
3dea8fca | 4204 | if (!elf_modify_segment_map (abfd, info, no_user_phdrs)) |
8ded5a0f | 4205 | return FALSE; |
8c37241b | 4206 | |
8ded5a0f AM |
4207 | for (count = 0, m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next) |
4208 | ++count; | |
4209 | elf_tdata (abfd)->program_header_size = count * bed->s->sizeof_phdr; | |
252b5132 | 4210 | |
b34976b6 | 4211 | return TRUE; |
252b5132 RH |
4212 | |
4213 | error_return: | |
4214 | if (sections != NULL) | |
4215 | free (sections); | |
b34976b6 | 4216 | return FALSE; |
252b5132 RH |
4217 | } |
4218 | ||
4219 | /* Sort sections by address. */ | |
4220 | ||
4221 | static int | |
217aa764 | 4222 | elf_sort_sections (const void *arg1, const void *arg2) |
252b5132 RH |
4223 | { |
4224 | const asection *sec1 = *(const asection **) arg1; | |
4225 | const asection *sec2 = *(const asection **) arg2; | |
eecdbe52 | 4226 | bfd_size_type size1, size2; |
252b5132 RH |
4227 | |
4228 | /* Sort by LMA first, since this is the address used to | |
4229 | place the section into a segment. */ | |
4230 | if (sec1->lma < sec2->lma) | |
4231 | return -1; | |
4232 | else if (sec1->lma > sec2->lma) | |
4233 | return 1; | |
4234 | ||
4235 | /* Then sort by VMA. Normally the LMA and the VMA will be | |
4236 | the same, and this will do nothing. */ | |
4237 | if (sec1->vma < sec2->vma) | |
4238 | return -1; | |
4239 | else if (sec1->vma > sec2->vma) | |
4240 | return 1; | |
4241 | ||
4242 | /* Put !SEC_LOAD sections after SEC_LOAD ones. */ | |
4243 | ||
07c6e936 | 4244 | #define TOEND(x) (((x)->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0) |
252b5132 RH |
4245 | |
4246 | if (TOEND (sec1)) | |
4247 | { | |
4248 | if (TOEND (sec2)) | |
00a7cdc5 NC |
4249 | { |
4250 | /* If the indicies are the same, do not return 0 | |
4251 | here, but continue to try the next comparison. */ | |
4252 | if (sec1->target_index - sec2->target_index != 0) | |
4253 | return sec1->target_index - sec2->target_index; | |
4254 | } | |
252b5132 RH |
4255 | else |
4256 | return 1; | |
4257 | } | |
00a7cdc5 | 4258 | else if (TOEND (sec2)) |
252b5132 RH |
4259 | return -1; |
4260 | ||
4261 | #undef TOEND | |
4262 | ||
00a7cdc5 NC |
4263 | /* Sort by size, to put zero sized sections |
4264 | before others at the same address. */ | |
252b5132 | 4265 | |
eea6121a AM |
4266 | size1 = (sec1->flags & SEC_LOAD) ? sec1->size : 0; |
4267 | size2 = (sec2->flags & SEC_LOAD) ? sec2->size : 0; | |
eecdbe52 JJ |
4268 | |
4269 | if (size1 < size2) | |
252b5132 | 4270 | return -1; |
eecdbe52 | 4271 | if (size1 > size2) |
252b5132 RH |
4272 | return 1; |
4273 | ||
4274 | return sec1->target_index - sec2->target_index; | |
4275 | } | |
4276 | ||
340b6d91 AC |
4277 | /* Ian Lance Taylor writes: |
4278 | ||
4279 | We shouldn't be using % with a negative signed number. That's just | |
4280 | not good. We have to make sure either that the number is not | |
4281 | negative, or that the number has an unsigned type. When the types | |
4282 | are all the same size they wind up as unsigned. When file_ptr is a | |
4283 | larger signed type, the arithmetic winds up as signed long long, | |
4284 | which is wrong. | |
4285 | ||
4286 | What we're trying to say here is something like ``increase OFF by | |
4287 | the least amount that will cause it to be equal to the VMA modulo | |
4288 | the page size.'' */ | |
4289 | /* In other words, something like: | |
4290 | ||
4291 | vma_offset = m->sections[0]->vma % bed->maxpagesize; | |
4292 | off_offset = off % bed->maxpagesize; | |
4293 | if (vma_offset < off_offset) | |
4294 | adjustment = vma_offset + bed->maxpagesize - off_offset; | |
4295 | else | |
4296 | adjustment = vma_offset - off_offset; | |
08a40648 | 4297 | |
340b6d91 AC |
4298 | which can can be collapsed into the expression below. */ |
4299 | ||
4300 | static file_ptr | |
4301 | vma_page_aligned_bias (bfd_vma vma, ufile_ptr off, bfd_vma maxpagesize) | |
4302 | { | |
4303 | return ((vma - off) % maxpagesize); | |
4304 | } | |
4305 | ||
6d33f217 L |
4306 | static void |
4307 | print_segment_map (const struct elf_segment_map *m) | |
4308 | { | |
4309 | unsigned int j; | |
4310 | const char *pt = get_segment_type (m->p_type); | |
4311 | char buf[32]; | |
4312 | ||
4313 | if (pt == NULL) | |
4314 | { | |
4315 | if (m->p_type >= PT_LOPROC && m->p_type <= PT_HIPROC) | |
4316 | sprintf (buf, "LOPROC+%7.7x", | |
4317 | (unsigned int) (m->p_type - PT_LOPROC)); | |
4318 | else if (m->p_type >= PT_LOOS && m->p_type <= PT_HIOS) | |
4319 | sprintf (buf, "LOOS+%7.7x", | |
4320 | (unsigned int) (m->p_type - PT_LOOS)); | |
4321 | else | |
4322 | snprintf (buf, sizeof (buf), "%8.8x", | |
4323 | (unsigned int) m->p_type); | |
4324 | pt = buf; | |
4325 | } | |
4a97a0e5 | 4326 | fflush (stdout); |
6d33f217 L |
4327 | fprintf (stderr, "%s:", pt); |
4328 | for (j = 0; j < m->count; j++) | |
4329 | fprintf (stderr, " %s", m->sections [j]->name); | |
4330 | putc ('\n',stderr); | |
4a97a0e5 | 4331 | fflush (stderr); |
6d33f217 L |
4332 | } |
4333 | ||
32812159 AM |
4334 | static bfd_boolean |
4335 | write_zeros (bfd *abfd, file_ptr pos, bfd_size_type len) | |
4336 | { | |
4337 | void *buf; | |
4338 | bfd_boolean ret; | |
4339 | ||
4340 | if (bfd_seek (abfd, pos, SEEK_SET) != 0) | |
4341 | return FALSE; | |
4342 | buf = bfd_zmalloc (len); | |
4343 | if (buf == NULL) | |
4344 | return FALSE; | |
4345 | ret = bfd_bwrite (buf, len, abfd) == len; | |
4346 | free (buf); | |
4347 | return ret; | |
4348 | } | |
4349 | ||
252b5132 RH |
4350 | /* Assign file positions to the sections based on the mapping from |
4351 | sections to segments. This function also sets up some fields in | |
f3520d2f | 4352 | the file header. */ |
252b5132 | 4353 | |
b34976b6 | 4354 | static bfd_boolean |
f3520d2f AM |
4355 | assign_file_positions_for_load_sections (bfd *abfd, |
4356 | struct bfd_link_info *link_info) | |
252b5132 RH |
4357 | { |
4358 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
252b5132 | 4359 | struct elf_segment_map *m; |
252b5132 | 4360 | Elf_Internal_Phdr *phdrs; |
252b5132 | 4361 | Elf_Internal_Phdr *p; |
02bf8d82 | 4362 | file_ptr off; |
3f570048 | 4363 | bfd_size_type maxpagesize; |
f3520d2f | 4364 | unsigned int alloc; |
0920dee7 | 4365 | unsigned int i, j; |
2b0bc088 | 4366 | bfd_vma header_pad = 0; |
252b5132 | 4367 | |
e36284ab | 4368 | if (link_info == NULL |
ceae84aa | 4369 | && !_bfd_elf_map_sections_to_segments (abfd, link_info)) |
8ded5a0f | 4370 | return FALSE; |
252b5132 | 4371 | |
8ded5a0f | 4372 | alloc = 0; |
252b5132 | 4373 | for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next) |
2b0bc088 NC |
4374 | { |
4375 | ++alloc; | |
4376 | if (m->header_size) | |
4377 | header_pad = m->header_size; | |
4378 | } | |
252b5132 | 4379 | |
82f2dbf7 NC |
4380 | if (alloc) |
4381 | { | |
4382 | elf_elfheader (abfd)->e_phoff = bed->s->sizeof_ehdr; | |
4383 | elf_elfheader (abfd)->e_phentsize = bed->s->sizeof_phdr; | |
4384 | } | |
4385 | else | |
4386 | { | |
4387 | /* PR binutils/12467. */ | |
4388 | elf_elfheader (abfd)->e_phoff = 0; | |
4389 | elf_elfheader (abfd)->e_phentsize = 0; | |
4390 | } | |
d324f6d6 | 4391 | |
8ded5a0f | 4392 | elf_elfheader (abfd)->e_phnum = alloc; |
252b5132 | 4393 | |
62d7a5f6 | 4394 | if (elf_tdata (abfd)->program_header_size == (bfd_size_type) -1) |
8ded5a0f AM |
4395 | elf_tdata (abfd)->program_header_size = alloc * bed->s->sizeof_phdr; |
4396 | else | |
4397 | BFD_ASSERT (elf_tdata (abfd)->program_header_size | |
59e0647f | 4398 | >= alloc * bed->s->sizeof_phdr); |
252b5132 RH |
4399 | |
4400 | if (alloc == 0) | |
f3520d2f | 4401 | { |
8ded5a0f AM |
4402 | elf_tdata (abfd)->next_file_pos = bed->s->sizeof_ehdr; |
4403 | return TRUE; | |
f3520d2f | 4404 | } |
252b5132 | 4405 | |
57268894 HPN |
4406 | /* We're writing the size in elf_tdata (abfd)->program_header_size, |
4407 | see assign_file_positions_except_relocs, so make sure we have | |
4408 | that amount allocated, with trailing space cleared. | |
4409 | The variable alloc contains the computed need, while elf_tdata | |
4410 | (abfd)->program_header_size contains the size used for the | |
4411 | layout. | |
4412 | See ld/emultempl/elf-generic.em:gld${EMULATION_NAME}_map_segments | |
4413 | where the layout is forced to according to a larger size in the | |
4414 | last iterations for the testcase ld-elf/header. */ | |
4415 | BFD_ASSERT (elf_tdata (abfd)->program_header_size % bed->s->sizeof_phdr | |
4416 | == 0); | |
a50b1753 NC |
4417 | phdrs = (Elf_Internal_Phdr *) |
4418 | bfd_zalloc2 (abfd, | |
4419 | (elf_tdata (abfd)->program_header_size / bed->s->sizeof_phdr), | |
4420 | sizeof (Elf_Internal_Phdr)); | |
f3520d2f | 4421 | elf_tdata (abfd)->phdr = phdrs; |
252b5132 | 4422 | if (phdrs == NULL) |
b34976b6 | 4423 | return FALSE; |
252b5132 | 4424 | |
3f570048 AM |
4425 | maxpagesize = 1; |
4426 | if ((abfd->flags & D_PAGED) != 0) | |
4427 | maxpagesize = bed->maxpagesize; | |
4428 | ||
252b5132 RH |
4429 | off = bed->s->sizeof_ehdr; |
4430 | off += alloc * bed->s->sizeof_phdr; | |
2b0bc088 NC |
4431 | if (header_pad < (bfd_vma) off) |
4432 | header_pad = 0; | |
4433 | else | |
4434 | header_pad -= off; | |
4435 | off += header_pad; | |
252b5132 | 4436 | |
0920dee7 | 4437 | for (m = elf_tdata (abfd)->segment_map, p = phdrs, j = 0; |
252b5132 | 4438 | m != NULL; |
0920dee7 | 4439 | m = m->next, p++, j++) |
252b5132 | 4440 | { |
252b5132 | 4441 | asection **secpp; |
bf988460 AM |
4442 | bfd_vma off_adjust; |
4443 | bfd_boolean no_contents; | |
252b5132 RH |
4444 | |
4445 | /* If elf_segment_map is not from map_sections_to_segments, the | |
08a40648 | 4446 | sections may not be correctly ordered. NOTE: sorting should |
52e9b619 MS |
4447 | not be done to the PT_NOTE section of a corefile, which may |
4448 | contain several pseudo-sections artificially created by bfd. | |
4449 | Sorting these pseudo-sections breaks things badly. */ | |
47d9a591 AM |
4450 | if (m->count > 1 |
4451 | && !(elf_elfheader (abfd)->e_type == ET_CORE | |
52e9b619 | 4452 | && m->p_type == PT_NOTE)) |
252b5132 RH |
4453 | qsort (m->sections, (size_t) m->count, sizeof (asection *), |
4454 | elf_sort_sections); | |
4455 | ||
b301b248 AM |
4456 | /* An ELF segment (described by Elf_Internal_Phdr) may contain a |
4457 | number of sections with contents contributing to both p_filesz | |
4458 | and p_memsz, followed by a number of sections with no contents | |
4459 | that just contribute to p_memsz. In this loop, OFF tracks next | |
02bf8d82 | 4460 | available file offset for PT_LOAD and PT_NOTE segments. */ |
252b5132 | 4461 | p->p_type = m->p_type; |
28a7f3e7 | 4462 | p->p_flags = m->p_flags; |
252b5132 | 4463 | |
3f570048 AM |
4464 | if (m->count == 0) |
4465 | p->p_vaddr = 0; | |
4466 | else | |
3271a814 | 4467 | p->p_vaddr = m->sections[0]->vma - m->p_vaddr_offset; |
3f570048 AM |
4468 | |
4469 | if (m->p_paddr_valid) | |
4470 | p->p_paddr = m->p_paddr; | |
4471 | else if (m->count == 0) | |
4472 | p->p_paddr = 0; | |
4473 | else | |
08a40648 | 4474 | p->p_paddr = m->sections[0]->lma - m->p_vaddr_offset; |
3f570048 AM |
4475 | |
4476 | if (p->p_type == PT_LOAD | |
4477 | && (abfd->flags & D_PAGED) != 0) | |
4478 | { | |
4479 | /* p_align in demand paged PT_LOAD segments effectively stores | |
4480 | the maximum page size. When copying an executable with | |
4481 | objcopy, we set m->p_align from the input file. Use this | |
4482 | value for maxpagesize rather than bed->maxpagesize, which | |
4483 | may be different. Note that we use maxpagesize for PT_TLS | |
4484 | segment alignment later in this function, so we are relying | |
4485 | on at least one PT_LOAD segment appearing before a PT_TLS | |
4486 | segment. */ | |
4487 | if (m->p_align_valid) | |
4488 | maxpagesize = m->p_align; | |
4489 | ||
4490 | p->p_align = maxpagesize; | |
4491 | } | |
3271a814 NS |
4492 | else if (m->p_align_valid) |
4493 | p->p_align = m->p_align; | |
e970b90a DJ |
4494 | else if (m->count == 0) |
4495 | p->p_align = 1 << bed->s->log_file_align; | |
3f570048 AM |
4496 | else |
4497 | p->p_align = 0; | |
4498 | ||
bf988460 AM |
4499 | no_contents = FALSE; |
4500 | off_adjust = 0; | |
252b5132 | 4501 | if (p->p_type == PT_LOAD |
b301b248 | 4502 | && m->count > 0) |
252b5132 | 4503 | { |
b301b248 | 4504 | bfd_size_type align; |
a49e53ed | 4505 | unsigned int align_power = 0; |
b301b248 | 4506 | |
3271a814 NS |
4507 | if (m->p_align_valid) |
4508 | align = p->p_align; | |
4509 | else | |
252b5132 | 4510 | { |
3271a814 NS |
4511 | for (i = 0, secpp = m->sections; i < m->count; i++, secpp++) |
4512 | { | |
4513 | unsigned int secalign; | |
08a40648 | 4514 | |
3271a814 NS |
4515 | secalign = bfd_get_section_alignment (abfd, *secpp); |
4516 | if (secalign > align_power) | |
4517 | align_power = secalign; | |
4518 | } | |
4519 | align = (bfd_size_type) 1 << align_power; | |
4520 | if (align < maxpagesize) | |
4521 | align = maxpagesize; | |
b301b248 | 4522 | } |
252b5132 | 4523 | |
02bf8d82 AM |
4524 | for (i = 0; i < m->count; i++) |
4525 | if ((m->sections[i]->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0) | |
4526 | /* If we aren't making room for this section, then | |
4527 | it must be SHT_NOBITS regardless of what we've | |
4528 | set via struct bfd_elf_special_section. */ | |
4529 | elf_section_type (m->sections[i]) = SHT_NOBITS; | |
4530 | ||
bf988460 | 4531 | /* Find out whether this segment contains any loadable |
aea274d3 AM |
4532 | sections. */ |
4533 | no_contents = TRUE; | |
4534 | for (i = 0; i < m->count; i++) | |
4535 | if (elf_section_type (m->sections[i]) != SHT_NOBITS) | |
4536 | { | |
4537 | no_contents = FALSE; | |
4538 | break; | |
4539 | } | |
bf988460 | 4540 | |
85cfcbfb | 4541 | off_adjust = vma_page_aligned_bias (p->p_vaddr, off, align); |
bf988460 AM |
4542 | off += off_adjust; |
4543 | if (no_contents) | |
4544 | { | |
4545 | /* We shouldn't need to align the segment on disk since | |
4546 | the segment doesn't need file space, but the gABI | |
4547 | arguably requires the alignment and glibc ld.so | |
4548 | checks it. So to comply with the alignment | |
4549 | requirement but not waste file space, we adjust | |
4550 | p_offset for just this segment. (OFF_ADJUST is | |
4551 | subtracted from OFF later.) This may put p_offset | |
4552 | past the end of file, but that shouldn't matter. */ | |
4553 | } | |
4554 | else | |
4555 | off_adjust = 0; | |
252b5132 | 4556 | } |
b1a6d0b1 NC |
4557 | /* Make sure the .dynamic section is the first section in the |
4558 | PT_DYNAMIC segment. */ | |
4559 | else if (p->p_type == PT_DYNAMIC | |
4560 | && m->count > 1 | |
4561 | && strcmp (m->sections[0]->name, ".dynamic") != 0) | |
4562 | { | |
4563 | _bfd_error_handler | |
b301b248 AM |
4564 | (_("%B: The first section in the PT_DYNAMIC segment is not the .dynamic section"), |
4565 | abfd); | |
b1a6d0b1 NC |
4566 | bfd_set_error (bfd_error_bad_value); |
4567 | return FALSE; | |
4568 | } | |
3f001e84 JK |
4569 | /* Set the note section type to SHT_NOTE. */ |
4570 | else if (p->p_type == PT_NOTE) | |
4571 | for (i = 0; i < m->count; i++) | |
4572 | elf_section_type (m->sections[i]) = SHT_NOTE; | |
252b5132 | 4573 | |
252b5132 RH |
4574 | p->p_offset = 0; |
4575 | p->p_filesz = 0; | |
4576 | p->p_memsz = 0; | |
4577 | ||
4578 | if (m->includes_filehdr) | |
4579 | { | |
bf988460 | 4580 | if (!m->p_flags_valid) |
252b5132 | 4581 | p->p_flags |= PF_R; |
252b5132 RH |
4582 | p->p_filesz = bed->s->sizeof_ehdr; |
4583 | p->p_memsz = bed->s->sizeof_ehdr; | |
4584 | if (m->count > 0) | |
4585 | { | |
252b5132 RH |
4586 | if (p->p_vaddr < (bfd_vma) off) |
4587 | { | |
caf47ea6 | 4588 | (*_bfd_error_handler) |
b301b248 AM |
4589 | (_("%B: Not enough room for program headers, try linking with -N"), |
4590 | abfd); | |
252b5132 | 4591 | bfd_set_error (bfd_error_bad_value); |
b34976b6 | 4592 | return FALSE; |
252b5132 RH |
4593 | } |
4594 | ||
4595 | p->p_vaddr -= off; | |
bf988460 | 4596 | if (!m->p_paddr_valid) |
252b5132 RH |
4597 | p->p_paddr -= off; |
4598 | } | |
252b5132 RH |
4599 | } |
4600 | ||
4601 | if (m->includes_phdrs) | |
4602 | { | |
bf988460 | 4603 | if (!m->p_flags_valid) |
252b5132 RH |
4604 | p->p_flags |= PF_R; |
4605 | ||
f3520d2f | 4606 | if (!m->includes_filehdr) |
252b5132 RH |
4607 | { |
4608 | p->p_offset = bed->s->sizeof_ehdr; | |
4609 | ||
4610 | if (m->count > 0) | |
4611 | { | |
252b5132 | 4612 | p->p_vaddr -= off - p->p_offset; |
bf988460 | 4613 | if (!m->p_paddr_valid) |
252b5132 RH |
4614 | p->p_paddr -= off - p->p_offset; |
4615 | } | |
252b5132 RH |
4616 | } |
4617 | ||
4618 | p->p_filesz += alloc * bed->s->sizeof_phdr; | |
4619 | p->p_memsz += alloc * bed->s->sizeof_phdr; | |
2b0bc088 NC |
4620 | if (m->count) |
4621 | { | |
4622 | p->p_filesz += header_pad; | |
4623 | p->p_memsz += header_pad; | |
4624 | } | |
252b5132 RH |
4625 | } |
4626 | ||
4627 | if (p->p_type == PT_LOAD | |
4628 | || (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core)) | |
4629 | { | |
bf988460 | 4630 | if (!m->includes_filehdr && !m->includes_phdrs) |
02bf8d82 | 4631 | p->p_offset = off; |
252b5132 RH |
4632 | else |
4633 | { | |
4634 | file_ptr adjust; | |
4635 | ||
4636 | adjust = off - (p->p_offset + p->p_filesz); | |
bf988460 AM |
4637 | if (!no_contents) |
4638 | p->p_filesz += adjust; | |
252b5132 RH |
4639 | p->p_memsz += adjust; |
4640 | } | |
4641 | } | |
4642 | ||
1ea63fd2 AM |
4643 | /* Set up p_filesz, p_memsz, p_align and p_flags from the section |
4644 | maps. Set filepos for sections in PT_LOAD segments, and in | |
4645 | core files, for sections in PT_NOTE segments. | |
4646 | assign_file_positions_for_non_load_sections will set filepos | |
4647 | for other sections and update p_filesz for other segments. */ | |
252b5132 RH |
4648 | for (i = 0, secpp = m->sections; i < m->count; i++, secpp++) |
4649 | { | |
4650 | asection *sec; | |
252b5132 | 4651 | bfd_size_type align; |
627b32bc | 4652 | Elf_Internal_Shdr *this_hdr; |
252b5132 RH |
4653 | |
4654 | sec = *secpp; | |
02bf8d82 | 4655 | this_hdr = &elf_section_data (sec)->this_hdr; |
3f570048 | 4656 | align = (bfd_size_type) 1 << bfd_get_section_alignment (abfd, sec); |
252b5132 | 4657 | |
88967714 AM |
4658 | if ((p->p_type == PT_LOAD |
4659 | || p->p_type == PT_TLS) | |
4660 | && (this_hdr->sh_type != SHT_NOBITS | |
4661 | || ((this_hdr->sh_flags & SHF_ALLOC) != 0 | |
4662 | && ((this_hdr->sh_flags & SHF_TLS) == 0 | |
4663 | || p->p_type == PT_TLS)))) | |
252b5132 | 4664 | { |
b5599592 AM |
4665 | bfd_vma p_start = p->p_paddr; |
4666 | bfd_vma p_end = p_start + p->p_memsz; | |
4667 | bfd_vma s_start = sec->lma; | |
4668 | bfd_vma adjust = s_start - p_end; | |
252b5132 | 4669 | |
a2d1e028 L |
4670 | if (adjust != 0 |
4671 | && (s_start < p_end | |
4672 | || p_end < p_start)) | |
252b5132 | 4673 | { |
88967714 | 4674 | (*_bfd_error_handler) |
b5599592 AM |
4675 | (_("%B: section %A lma %#lx adjusted to %#lx"), abfd, sec, |
4676 | (unsigned long) s_start, (unsigned long) p_end); | |
88967714 | 4677 | adjust = 0; |
b5599592 | 4678 | sec->lma = p_end; |
1cfb7d1e | 4679 | } |
3ac9b6c9 | 4680 | p->p_memsz += adjust; |
1cfb7d1e | 4681 | |
88967714 AM |
4682 | if (this_hdr->sh_type != SHT_NOBITS) |
4683 | { | |
32812159 AM |
4684 | if (p->p_filesz + adjust < p->p_memsz) |
4685 | { | |
4686 | /* We have a PROGBITS section following NOBITS ones. | |
4687 | Allocate file space for the NOBITS section(s) and | |
4688 | zero it. */ | |
4689 | adjust = p->p_memsz - p->p_filesz; | |
4690 | if (!write_zeros (abfd, off, adjust)) | |
4691 | return FALSE; | |
4692 | } | |
88967714 AM |
4693 | off += adjust; |
4694 | p->p_filesz += adjust; | |
252b5132 | 4695 | } |
252b5132 RH |
4696 | } |
4697 | ||
4698 | if (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core) | |
4699 | { | |
b301b248 AM |
4700 | /* The section at i == 0 is the one that actually contains |
4701 | everything. */ | |
4a938328 MS |
4702 | if (i == 0) |
4703 | { | |
627b32bc | 4704 | this_hdr->sh_offset = sec->filepos = off; |
6a3cd2b4 AM |
4705 | off += this_hdr->sh_size; |
4706 | p->p_filesz = this_hdr->sh_size; | |
b301b248 AM |
4707 | p->p_memsz = 0; |
4708 | p->p_align = 1; | |
252b5132 | 4709 | } |
4a938328 | 4710 | else |
252b5132 | 4711 | { |
b301b248 | 4712 | /* The rest are fake sections that shouldn't be written. */ |
252b5132 | 4713 | sec->filepos = 0; |
eea6121a | 4714 | sec->size = 0; |
b301b248 AM |
4715 | sec->flags = 0; |
4716 | continue; | |
252b5132 | 4717 | } |
252b5132 RH |
4718 | } |
4719 | else | |
4720 | { | |
1e951488 | 4721 | if (p->p_type == PT_LOAD) |
b301b248 | 4722 | { |
1e951488 AM |
4723 | this_hdr->sh_offset = sec->filepos = off; |
4724 | if (this_hdr->sh_type != SHT_NOBITS) | |
4725 | off += this_hdr->sh_size; | |
4726 | } | |
4727 | else if (this_hdr->sh_type == SHT_NOBITS | |
4728 | && (this_hdr->sh_flags & SHF_TLS) != 0 | |
4729 | && this_hdr->sh_offset == 0) | |
4730 | { | |
4731 | /* This is a .tbss section that didn't get a PT_LOAD. | |
4732 | (See _bfd_elf_map_sections_to_segments "Create a | |
4733 | final PT_LOAD".) Set sh_offset to the value it | |
4734 | would have if we had created a zero p_filesz and | |
4735 | p_memsz PT_LOAD header for the section. This | |
4736 | also makes the PT_TLS header have the same | |
4737 | p_offset value. */ | |
4738 | bfd_vma adjust = vma_page_aligned_bias (this_hdr->sh_addr, | |
4739 | off, align); | |
4740 | this_hdr->sh_offset = sec->filepos = off + adjust; | |
b301b248 | 4741 | } |
252b5132 | 4742 | |
02bf8d82 | 4743 | if (this_hdr->sh_type != SHT_NOBITS) |
b301b248 | 4744 | { |
6a3cd2b4 | 4745 | p->p_filesz += this_hdr->sh_size; |
02bf8d82 AM |
4746 | /* A load section without SHF_ALLOC is something like |
4747 | a note section in a PT_NOTE segment. These take | |
4748 | file space but are not loaded into memory. */ | |
4749 | if ((this_hdr->sh_flags & SHF_ALLOC) != 0) | |
6a3cd2b4 | 4750 | p->p_memsz += this_hdr->sh_size; |
b301b248 | 4751 | } |
6a3cd2b4 | 4752 | else if ((this_hdr->sh_flags & SHF_ALLOC) != 0) |
13ae64f3 | 4753 | { |
6a3cd2b4 AM |
4754 | if (p->p_type == PT_TLS) |
4755 | p->p_memsz += this_hdr->sh_size; | |
4756 | ||
4757 | /* .tbss is special. It doesn't contribute to p_memsz of | |
4758 | normal segments. */ | |
4759 | else if ((this_hdr->sh_flags & SHF_TLS) == 0) | |
4760 | p->p_memsz += this_hdr->sh_size; | |
13ae64f3 JJ |
4761 | } |
4762 | ||
b10a8ae0 L |
4763 | if (align > p->p_align |
4764 | && !m->p_align_valid | |
4765 | && (p->p_type != PT_LOAD | |
4766 | || (abfd->flags & D_PAGED) == 0)) | |
252b5132 RH |
4767 | p->p_align = align; |
4768 | } | |
4769 | ||
bf988460 | 4770 | if (!m->p_flags_valid) |
252b5132 RH |
4771 | { |
4772 | p->p_flags |= PF_R; | |
02bf8d82 | 4773 | if ((this_hdr->sh_flags & SHF_EXECINSTR) != 0) |
252b5132 | 4774 | p->p_flags |= PF_X; |
02bf8d82 | 4775 | if ((this_hdr->sh_flags & SHF_WRITE) != 0) |
252b5132 RH |
4776 | p->p_flags |= PF_W; |
4777 | } | |
4778 | } | |
bf988460 | 4779 | off -= off_adjust; |
0920dee7 | 4780 | |
7c928300 AM |
4781 | /* Check that all sections are in a PT_LOAD segment. |
4782 | Don't check funky gdb generated core files. */ | |
4783 | if (p->p_type == PT_LOAD && bfd_get_format (abfd) != bfd_core) | |
9a83a553 AM |
4784 | { |
4785 | bfd_boolean check_vma = TRUE; | |
4786 | ||
4787 | for (i = 1; i < m->count; i++) | |
4788 | if (m->sections[i]->vma == m->sections[i - 1]->vma | |
4789 | && ELF_SECTION_SIZE (&(elf_section_data (m->sections[i]) | |
4790 | ->this_hdr), p) != 0 | |
4791 | && ELF_SECTION_SIZE (&(elf_section_data (m->sections[i - 1]) | |
4792 | ->this_hdr), p) != 0) | |
0920dee7 | 4793 | { |
9a83a553 AM |
4794 | /* Looks like we have overlays packed into the segment. */ |
4795 | check_vma = FALSE; | |
4796 | break; | |
0920dee7 | 4797 | } |
9a83a553 AM |
4798 | |
4799 | for (i = 0; i < m->count; i++) | |
4800 | { | |
4801 | Elf_Internal_Shdr *this_hdr; | |
4802 | asection *sec; | |
4803 | ||
4804 | sec = m->sections[i]; | |
4805 | this_hdr = &(elf_section_data(sec)->this_hdr); | |
86b2281f AM |
4806 | if (!ELF_SECTION_IN_SEGMENT_1 (this_hdr, p, check_vma, 0) |
4807 | && !ELF_TBSS_SPECIAL (this_hdr, p)) | |
9a83a553 AM |
4808 | { |
4809 | (*_bfd_error_handler) | |
4810 | (_("%B: section `%A' can't be allocated in segment %d"), | |
4811 | abfd, sec, j); | |
4812 | print_segment_map (m); | |
4813 | } | |
4814 | } | |
4815 | } | |
252b5132 RH |
4816 | } |
4817 | ||
f3520d2f AM |
4818 | elf_tdata (abfd)->next_file_pos = off; |
4819 | return TRUE; | |
4820 | } | |
4821 | ||
4822 | /* Assign file positions for the other sections. */ | |
4823 | ||
4824 | static bfd_boolean | |
4825 | assign_file_positions_for_non_load_sections (bfd *abfd, | |
4826 | struct bfd_link_info *link_info) | |
4827 | { | |
4828 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
4829 | Elf_Internal_Shdr **i_shdrpp; | |
4830 | Elf_Internal_Shdr **hdrpp; | |
4831 | Elf_Internal_Phdr *phdrs; | |
4832 | Elf_Internal_Phdr *p; | |
4833 | struct elf_segment_map *m; | |
62655c7b | 4834 | struct elf_segment_map *hdrs_segment; |
f3520d2f AM |
4835 | bfd_vma filehdr_vaddr, filehdr_paddr; |
4836 | bfd_vma phdrs_vaddr, phdrs_paddr; | |
4837 | file_ptr off; | |
4838 | unsigned int num_sec; | |
4839 | unsigned int i; | |
4840 | unsigned int count; | |
4841 | ||
5c182d5f AM |
4842 | i_shdrpp = elf_elfsections (abfd); |
4843 | num_sec = elf_numsections (abfd); | |
f3520d2f | 4844 | off = elf_tdata (abfd)->next_file_pos; |
5c182d5f AM |
4845 | for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++) |
4846 | { | |
4847 | struct elf_obj_tdata *tdata = elf_tdata (abfd); | |
4848 | Elf_Internal_Shdr *hdr; | |
4849 | ||
4850 | hdr = *hdrpp; | |
4851 | if (hdr->bfd_section != NULL | |
252e386e AM |
4852 | && (hdr->bfd_section->filepos != 0 |
4853 | || (hdr->sh_type == SHT_NOBITS | |
4854 | && hdr->contents == NULL))) | |
627b32bc | 4855 | BFD_ASSERT (hdr->sh_offset == hdr->bfd_section->filepos); |
5c182d5f AM |
4856 | else if ((hdr->sh_flags & SHF_ALLOC) != 0) |
4857 | { | |
e8d2ba53 AM |
4858 | if (hdr->sh_size != 0) |
4859 | (*_bfd_error_handler) | |
4860 | (_("%B: warning: allocated section `%s' not in segment"), | |
4861 | abfd, | |
4862 | (hdr->bfd_section == NULL | |
4863 | ? "*unknown*" | |
4864 | : hdr->bfd_section->name)); | |
3ba71138 L |
4865 | /* We don't need to page align empty sections. */ |
4866 | if ((abfd->flags & D_PAGED) != 0 && hdr->sh_size != 0) | |
5c182d5f AM |
4867 | off += vma_page_aligned_bias (hdr->sh_addr, off, |
4868 | bed->maxpagesize); | |
4869 | else | |
4870 | off += vma_page_aligned_bias (hdr->sh_addr, off, | |
4871 | hdr->sh_addralign); | |
4872 | off = _bfd_elf_assign_file_position_for_section (hdr, off, | |
4873 | FALSE); | |
4874 | } | |
4875 | else if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA) | |
4876 | && hdr->bfd_section == NULL) | |
4877 | || hdr == i_shdrpp[tdata->symtab_section] | |
4878 | || hdr == i_shdrpp[tdata->symtab_shndx_section] | |
4879 | || hdr == i_shdrpp[tdata->strtab_section]) | |
4880 | hdr->sh_offset = -1; | |
4881 | else | |
4882 | off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE); | |
5c182d5f AM |
4883 | } |
4884 | ||
252b5132 RH |
4885 | /* Now that we have set the section file positions, we can set up |
4886 | the file positions for the non PT_LOAD segments. */ | |
f3520d2f AM |
4887 | count = 0; |
4888 | filehdr_vaddr = 0; | |
4889 | filehdr_paddr = 0; | |
4890 | phdrs_vaddr = bed->maxpagesize + bed->s->sizeof_ehdr; | |
4891 | phdrs_paddr = 0; | |
62655c7b | 4892 | hdrs_segment = NULL; |
f3520d2f AM |
4893 | phdrs = elf_tdata (abfd)->phdr; |
4894 | for (m = elf_tdata (abfd)->segment_map, p = phdrs; | |
4895 | m != NULL; | |
4896 | m = m->next, p++) | |
4897 | { | |
4898 | ++count; | |
4899 | if (p->p_type != PT_LOAD) | |
4900 | continue; | |
4901 | ||
4902 | if (m->includes_filehdr) | |
4903 | { | |
4904 | filehdr_vaddr = p->p_vaddr; | |
4905 | filehdr_paddr = p->p_paddr; | |
4906 | } | |
4907 | if (m->includes_phdrs) | |
4908 | { | |
4909 | phdrs_vaddr = p->p_vaddr; | |
4910 | phdrs_paddr = p->p_paddr; | |
4911 | if (m->includes_filehdr) | |
4912 | { | |
62655c7b | 4913 | hdrs_segment = m; |
f3520d2f AM |
4914 | phdrs_vaddr += bed->s->sizeof_ehdr; |
4915 | phdrs_paddr += bed->s->sizeof_ehdr; | |
4916 | } | |
4917 | } | |
4918 | } | |
4919 | ||
62655c7b RM |
4920 | if (hdrs_segment != NULL && link_info != NULL) |
4921 | { | |
4922 | /* There is a segment that contains both the file headers and the | |
4923 | program headers, so provide a symbol __ehdr_start pointing there. | |
4924 | A program can use this to examine itself robustly. */ | |
4925 | ||
4926 | struct elf_link_hash_entry *hash | |
4927 | = elf_link_hash_lookup (elf_hash_table (link_info), "__ehdr_start", | |
4928 | FALSE, FALSE, TRUE); | |
4929 | /* If the symbol was referenced and not defined, define it. */ | |
4930 | if (hash != NULL | |
4931 | && (hash->root.type == bfd_link_hash_new | |
4932 | || hash->root.type == bfd_link_hash_undefined | |
4933 | || hash->root.type == bfd_link_hash_undefweak | |
4934 | || hash->root.type == bfd_link_hash_common)) | |
4935 | { | |
4936 | asection *s = NULL; | |
4937 | if (hdrs_segment->count != 0) | |
4938 | /* The segment contains sections, so use the first one. */ | |
4939 | s = hdrs_segment->sections[0]; | |
4940 | else | |
4941 | /* Use the first (i.e. lowest-addressed) section in any segment. */ | |
4942 | for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next) | |
4943 | if (m->count != 0) | |
4944 | { | |
4945 | s = m->sections[0]; | |
4946 | break; | |
4947 | } | |
4948 | ||
4949 | if (s != NULL) | |
4950 | { | |
4951 | hash->root.u.def.value = filehdr_vaddr - s->vma; | |
4952 | hash->root.u.def.section = s; | |
4953 | } | |
4954 | else | |
4955 | { | |
4956 | hash->root.u.def.value = filehdr_vaddr; | |
4957 | hash->root.u.def.section = bfd_abs_section_ptr; | |
4958 | } | |
4959 | ||
4960 | hash->root.type = bfd_link_hash_defined; | |
4961 | hash->def_regular = 1; | |
4962 | hash->non_elf = 0; | |
4963 | } | |
4964 | } | |
4965 | ||
252b5132 RH |
4966 | for (m = elf_tdata (abfd)->segment_map, p = phdrs; |
4967 | m != NULL; | |
4968 | m = m->next, p++) | |
4969 | { | |
129af99f | 4970 | if (p->p_type == PT_GNU_RELRO) |
252b5132 | 4971 | { |
b84a33b5 | 4972 | const Elf_Internal_Phdr *lp; |
3146fac4 | 4973 | struct elf_segment_map *lm; |
1ea63fd2 | 4974 | |
129af99f | 4975 | if (link_info != NULL) |
8c37241b | 4976 | { |
129af99f AS |
4977 | /* During linking the range of the RELRO segment is passed |
4978 | in link_info. */ | |
3146fac4 AM |
4979 | for (lm = elf_tdata (abfd)->segment_map, lp = phdrs; |
4980 | lm != NULL; | |
4981 | lm = lm->next, lp++) | |
8c37241b JJ |
4982 | { |
4983 | if (lp->p_type == PT_LOAD | |
b84a33b5 | 4984 | && lp->p_vaddr < link_info->relro_end |
3146fac4 AM |
4985 | && lp->p_vaddr + lp->p_filesz >= link_info->relro_end |
4986 | && lm->count != 0 | |
4987 | && lm->sections[0]->vma >= link_info->relro_start) | |
8c37241b JJ |
4988 | break; |
4989 | } | |
8981c88a L |
4990 | |
4991 | /* PR ld/14207. If the RELRO segment doesn't fit in the | |
4992 | LOAD segment, it should be removed. */ | |
3146fac4 | 4993 | BFD_ASSERT (lm != NULL); |
8c37241b | 4994 | } |
129af99f AS |
4995 | else |
4996 | { | |
4997 | /* Otherwise we are copying an executable or shared | |
b84a33b5 | 4998 | library, but we need to use the same linker logic. */ |
129af99f AS |
4999 | for (lp = phdrs; lp < phdrs + count; ++lp) |
5000 | { | |
5001 | if (lp->p_type == PT_LOAD | |
5002 | && lp->p_paddr == p->p_paddr) | |
5003 | break; | |
5004 | } | |
b84a33b5 AM |
5005 | } |
5006 | ||
5007 | if (lp < phdrs + count) | |
5008 | { | |
5009 | p->p_vaddr = lp->p_vaddr; | |
5010 | p->p_paddr = lp->p_paddr; | |
5011 | p->p_offset = lp->p_offset; | |
5012 | if (link_info != NULL) | |
5013 | p->p_filesz = link_info->relro_end - lp->p_vaddr; | |
5014 | else if (m->p_size_valid) | |
5015 | p->p_filesz = m->p_size; | |
129af99f AS |
5016 | else |
5017 | abort (); | |
b84a33b5 | 5018 | p->p_memsz = p->p_filesz; |
f3944f72 L |
5019 | /* Preserve the alignment and flags if they are valid. The |
5020 | gold linker generates RW/4 for the PT_GNU_RELRO section. | |
5021 | It is better for objcopy/strip to honor these attributes | |
5022 | otherwise gdb will choke when using separate debug files. | |
5023 | */ | |
5024 | if (!m->p_align_valid) | |
5025 | p->p_align = 1; | |
5026 | if (!m->p_flags_valid) | |
5027 | p->p_flags = (lp->p_flags & ~PF_W); | |
129af99f | 5028 | } |
9433b9b1 | 5029 | else |
b84a33b5 AM |
5030 | { |
5031 | memset (p, 0, sizeof *p); | |
5032 | p->p_type = PT_NULL; | |
5033 | } | |
129af99f | 5034 | } |
04c3a755 NS |
5035 | else if (p->p_type == PT_GNU_STACK) |
5036 | { | |
5037 | if (m->p_size_valid) | |
5038 | p->p_memsz = m->p_size; | |
5039 | } | |
129af99f AS |
5040 | else if (m->count != 0) |
5041 | { | |
5042 | if (p->p_type != PT_LOAD | |
5043 | && (p->p_type != PT_NOTE | |
5044 | || bfd_get_format (abfd) != bfd_core)) | |
5045 | { | |
129af99f AS |
5046 | BFD_ASSERT (!m->includes_filehdr && !m->includes_phdrs); |
5047 | ||
86b2281f | 5048 | p->p_filesz = 0; |
129af99f | 5049 | p->p_offset = m->sections[0]->filepos; |
86b2281f AM |
5050 | for (i = m->count; i-- != 0;) |
5051 | { | |
5052 | asection *sect = m->sections[i]; | |
5053 | Elf_Internal_Shdr *hdr = &elf_section_data (sect)->this_hdr; | |
5054 | if (hdr->sh_type != SHT_NOBITS) | |
5055 | { | |
5056 | p->p_filesz = (sect->filepos - m->sections[0]->filepos | |
5057 | + hdr->sh_size); | |
5058 | break; | |
5059 | } | |
5060 | } | |
129af99f AS |
5061 | } |
5062 | } | |
5063 | else if (m->includes_filehdr) | |
5064 | { | |
5065 | p->p_vaddr = filehdr_vaddr; | |
5066 | if (! m->p_paddr_valid) | |
5067 | p->p_paddr = filehdr_paddr; | |
5068 | } | |
5069 | else if (m->includes_phdrs) | |
5070 | { | |
5071 | p->p_vaddr = phdrs_vaddr; | |
5072 | if (! m->p_paddr_valid) | |
5073 | p->p_paddr = phdrs_paddr; | |
252b5132 RH |
5074 | } |
5075 | } | |
5076 | ||
252b5132 RH |
5077 | elf_tdata (abfd)->next_file_pos = off; |
5078 | ||
b34976b6 | 5079 | return TRUE; |
252b5132 RH |
5080 | } |
5081 | ||
252b5132 RH |
5082 | /* Work out the file positions of all the sections. This is called by |
5083 | _bfd_elf_compute_section_file_positions. All the section sizes and | |
5084 | VMAs must be known before this is called. | |
5085 | ||
e0638f70 AM |
5086 | Reloc sections come in two flavours: Those processed specially as |
5087 | "side-channel" data attached to a section to which they apply, and | |
5088 | those that bfd doesn't process as relocations. The latter sort are | |
5089 | stored in a normal bfd section by bfd_section_from_shdr. We don't | |
5090 | consider the former sort here, unless they form part of the loadable | |
5091 | image. Reloc sections not assigned here will be handled later by | |
5092 | assign_file_positions_for_relocs. | |
252b5132 RH |
5093 | |
5094 | We also don't set the positions of the .symtab and .strtab here. */ | |
5095 | ||
b34976b6 | 5096 | static bfd_boolean |
c84fca4d AO |
5097 | assign_file_positions_except_relocs (bfd *abfd, |
5098 | struct bfd_link_info *link_info) | |
252b5132 | 5099 | { |
5c182d5f AM |
5100 | struct elf_obj_tdata *tdata = elf_tdata (abfd); |
5101 | Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd); | |
252b5132 | 5102 | file_ptr off; |
9c5bfbb7 | 5103 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
252b5132 RH |
5104 | |
5105 | if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0 | |
5106 | && bfd_get_format (abfd) != bfd_core) | |
5107 | { | |
5c182d5f AM |
5108 | Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd); |
5109 | unsigned int num_sec = elf_numsections (abfd); | |
252b5132 RH |
5110 | Elf_Internal_Shdr **hdrpp; |
5111 | unsigned int i; | |
5112 | ||
5113 | /* Start after the ELF header. */ | |
5114 | off = i_ehdrp->e_ehsize; | |
5115 | ||
5116 | /* We are not creating an executable, which means that we are | |
5117 | not creating a program header, and that the actual order of | |
5118 | the sections in the file is unimportant. */ | |
9ad5cbcf | 5119 | for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++) |
252b5132 RH |
5120 | { |
5121 | Elf_Internal_Shdr *hdr; | |
5122 | ||
5123 | hdr = *hdrpp; | |
e0638f70 AM |
5124 | if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA) |
5125 | && hdr->bfd_section == NULL) | |
9ad5cbcf AM |
5126 | || i == tdata->symtab_section |
5127 | || i == tdata->symtab_shndx_section | |
252b5132 RH |
5128 | || i == tdata->strtab_section) |
5129 | { | |
5130 | hdr->sh_offset = -1; | |
252b5132 | 5131 | } |
9ad5cbcf | 5132 | else |
b34976b6 | 5133 | off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE); |
252b5132 RH |
5134 | } |
5135 | } | |
5136 | else | |
5137 | { | |
f3520d2f AM |
5138 | unsigned int alloc; |
5139 | ||
252b5132 | 5140 | /* Assign file positions for the loaded sections based on the |
08a40648 | 5141 | assignment of sections to segments. */ |
f3520d2f AM |
5142 | if (!assign_file_positions_for_load_sections (abfd, link_info)) |
5143 | return FALSE; | |
5144 | ||
5145 | /* And for non-load sections. */ | |
5146 | if (!assign_file_positions_for_non_load_sections (abfd, link_info)) | |
5147 | return FALSE; | |
5148 | ||
e36284ab AM |
5149 | if (bed->elf_backend_modify_program_headers != NULL) |
5150 | { | |
5151 | if (!(*bed->elf_backend_modify_program_headers) (abfd, link_info)) | |
5152 | return FALSE; | |
5153 | } | |
5154 | ||
f3520d2f AM |
5155 | /* Write out the program headers. */ |
5156 | alloc = tdata->program_header_size / bed->s->sizeof_phdr; | |
5157 | if (bfd_seek (abfd, (bfd_signed_vma) bed->s->sizeof_ehdr, SEEK_SET) != 0 | |
5158 | || bed->s->write_out_phdrs (abfd, tdata->phdr, alloc) != 0) | |
b34976b6 | 5159 | return FALSE; |
252b5132 | 5160 | |
5c182d5f | 5161 | off = tdata->next_file_pos; |
252b5132 RH |
5162 | } |
5163 | ||
5164 | /* Place the section headers. */ | |
45d6a902 | 5165 | off = align_file_position (off, 1 << bed->s->log_file_align); |
252b5132 RH |
5166 | i_ehdrp->e_shoff = off; |
5167 | off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize; | |
5168 | ||
5c182d5f | 5169 | tdata->next_file_pos = off; |
252b5132 | 5170 | |
b34976b6 | 5171 | return TRUE; |
252b5132 RH |
5172 | } |
5173 | ||
b34976b6 | 5174 | static bfd_boolean |
217aa764 | 5175 | prep_headers (bfd *abfd) |
252b5132 | 5176 | { |
3d540e93 | 5177 | Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form. */ |
2b0f7ef9 | 5178 | struct elf_strtab_hash *shstrtab; |
9c5bfbb7 | 5179 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
252b5132 RH |
5180 | |
5181 | i_ehdrp = elf_elfheader (abfd); | |
252b5132 | 5182 | |
2b0f7ef9 | 5183 | shstrtab = _bfd_elf_strtab_init (); |
252b5132 | 5184 | if (shstrtab == NULL) |
b34976b6 | 5185 | return FALSE; |
252b5132 RH |
5186 | |
5187 | elf_shstrtab (abfd) = shstrtab; | |
5188 | ||
5189 | i_ehdrp->e_ident[EI_MAG0] = ELFMAG0; | |
5190 | i_ehdrp->e_ident[EI_MAG1] = ELFMAG1; | |
5191 | i_ehdrp->e_ident[EI_MAG2] = ELFMAG2; | |
5192 | i_ehdrp->e_ident[EI_MAG3] = ELFMAG3; | |
5193 | ||
5194 | i_ehdrp->e_ident[EI_CLASS] = bed->s->elfclass; | |
5195 | i_ehdrp->e_ident[EI_DATA] = | |
5196 | bfd_big_endian (abfd) ? ELFDATA2MSB : ELFDATA2LSB; | |
5197 | i_ehdrp->e_ident[EI_VERSION] = bed->s->ev_current; | |
5198 | ||
252b5132 RH |
5199 | if ((abfd->flags & DYNAMIC) != 0) |
5200 | i_ehdrp->e_type = ET_DYN; | |
5201 | else if ((abfd->flags & EXEC_P) != 0) | |
5202 | i_ehdrp->e_type = ET_EXEC; | |
5203 | else if (bfd_get_format (abfd) == bfd_core) | |
5204 | i_ehdrp->e_type = ET_CORE; | |
5205 | else | |
5206 | i_ehdrp->e_type = ET_REL; | |
5207 | ||
5208 | switch (bfd_get_arch (abfd)) | |
5209 | { | |
5210 | case bfd_arch_unknown: | |
5211 | i_ehdrp->e_machine = EM_NONE; | |
5212 | break; | |
aa4f99bb AO |
5213 | |
5214 | /* There used to be a long list of cases here, each one setting | |
5215 | e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE | |
5216 | in the corresponding bfd definition. To avoid duplication, | |
5217 | the switch was removed. Machines that need special handling | |
5218 | can generally do it in elf_backend_final_write_processing(), | |
5219 | unless they need the information earlier than the final write. | |
5220 | Such need can generally be supplied by replacing the tests for | |
5221 | e_machine with the conditions used to determine it. */ | |
252b5132 | 5222 | default: |
9c5bfbb7 AM |
5223 | i_ehdrp->e_machine = bed->elf_machine_code; |
5224 | } | |
aa4f99bb | 5225 | |
252b5132 RH |
5226 | i_ehdrp->e_version = bed->s->ev_current; |
5227 | i_ehdrp->e_ehsize = bed->s->sizeof_ehdr; | |
5228 | ||
c044fabd | 5229 | /* No program header, for now. */ |
252b5132 RH |
5230 | i_ehdrp->e_phoff = 0; |
5231 | i_ehdrp->e_phentsize = 0; | |
5232 | i_ehdrp->e_phnum = 0; | |
5233 | ||
c044fabd | 5234 | /* Each bfd section is section header entry. */ |
252b5132 RH |
5235 | i_ehdrp->e_entry = bfd_get_start_address (abfd); |
5236 | i_ehdrp->e_shentsize = bed->s->sizeof_shdr; | |
5237 | ||
c044fabd | 5238 | /* If we're building an executable, we'll need a program header table. */ |
252b5132 | 5239 | if (abfd->flags & EXEC_P) |
0e71e495 BE |
5240 | /* It all happens later. */ |
5241 | ; | |
252b5132 RH |
5242 | else |
5243 | { | |
5244 | i_ehdrp->e_phentsize = 0; | |
252b5132 RH |
5245 | i_ehdrp->e_phoff = 0; |
5246 | } | |
5247 | ||
5248 | elf_tdata (abfd)->symtab_hdr.sh_name = | |
b34976b6 | 5249 | (unsigned int) _bfd_elf_strtab_add (shstrtab, ".symtab", FALSE); |
252b5132 | 5250 | elf_tdata (abfd)->strtab_hdr.sh_name = |
b34976b6 | 5251 | (unsigned int) _bfd_elf_strtab_add (shstrtab, ".strtab", FALSE); |
252b5132 | 5252 | elf_tdata (abfd)->shstrtab_hdr.sh_name = |
b34976b6 | 5253 | (unsigned int) _bfd_elf_strtab_add (shstrtab, ".shstrtab", FALSE); |
252b5132 RH |
5254 | if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1 |
5255 | || elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1 | |
5256 | || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1) | |
b34976b6 | 5257 | return FALSE; |
252b5132 | 5258 | |
b34976b6 | 5259 | return TRUE; |
252b5132 RH |
5260 | } |
5261 | ||
5262 | /* Assign file positions for all the reloc sections which are not part | |
5263 | of the loadable file image. */ | |
5264 | ||
5265 | void | |
217aa764 | 5266 | _bfd_elf_assign_file_positions_for_relocs (bfd *abfd) |
252b5132 RH |
5267 | { |
5268 | file_ptr off; | |
9ad5cbcf | 5269 | unsigned int i, num_sec; |
252b5132 RH |
5270 | Elf_Internal_Shdr **shdrpp; |
5271 | ||
5272 | off = elf_tdata (abfd)->next_file_pos; | |
5273 | ||
9ad5cbcf AM |
5274 | num_sec = elf_numsections (abfd); |
5275 | for (i = 1, shdrpp = elf_elfsections (abfd) + 1; i < num_sec; i++, shdrpp++) | |
252b5132 RH |
5276 | { |
5277 | Elf_Internal_Shdr *shdrp; | |
5278 | ||
5279 | shdrp = *shdrpp; | |
5280 | if ((shdrp->sh_type == SHT_REL || shdrp->sh_type == SHT_RELA) | |
5281 | && shdrp->sh_offset == -1) | |
b34976b6 | 5282 | off = _bfd_elf_assign_file_position_for_section (shdrp, off, TRUE); |
252b5132 RH |
5283 | } |
5284 | ||
5285 | elf_tdata (abfd)->next_file_pos = off; | |
5286 | } | |
5287 | ||
b34976b6 | 5288 | bfd_boolean |
217aa764 | 5289 | _bfd_elf_write_object_contents (bfd *abfd) |
252b5132 | 5290 | { |
9c5bfbb7 | 5291 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
252b5132 | 5292 | Elf_Internal_Shdr **i_shdrp; |
b34976b6 | 5293 | bfd_boolean failed; |
9ad5cbcf | 5294 | unsigned int count, num_sec; |
252b5132 RH |
5295 | |
5296 | if (! abfd->output_has_begun | |
217aa764 | 5297 | && ! _bfd_elf_compute_section_file_positions (abfd, NULL)) |
b34976b6 | 5298 | return FALSE; |
252b5132 RH |
5299 | |
5300 | i_shdrp = elf_elfsections (abfd); | |
252b5132 | 5301 | |
b34976b6 | 5302 | failed = FALSE; |
252b5132 RH |
5303 | bfd_map_over_sections (abfd, bed->s->write_relocs, &failed); |
5304 | if (failed) | |
b34976b6 | 5305 | return FALSE; |
252b5132 RH |
5306 | |
5307 | _bfd_elf_assign_file_positions_for_relocs (abfd); | |
5308 | ||
c044fabd | 5309 | /* After writing the headers, we need to write the sections too... */ |
9ad5cbcf AM |
5310 | num_sec = elf_numsections (abfd); |
5311 | for (count = 1; count < num_sec; count++) | |
252b5132 RH |
5312 | { |
5313 | if (bed->elf_backend_section_processing) | |
5314 | (*bed->elf_backend_section_processing) (abfd, i_shdrp[count]); | |
5315 | if (i_shdrp[count]->contents) | |
5316 | { | |
dc810e39 AM |
5317 | bfd_size_type amt = i_shdrp[count]->sh_size; |
5318 | ||
252b5132 | 5319 | if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0 |
dc810e39 | 5320 | || bfd_bwrite (i_shdrp[count]->contents, amt, abfd) != amt) |
b34976b6 | 5321 | return FALSE; |
252b5132 RH |
5322 | } |
5323 | } | |
5324 | ||
5325 | /* Write out the section header names. */ | |
26ae6d5e DJ |
5326 | if (elf_shstrtab (abfd) != NULL |
5327 | && (bfd_seek (abfd, elf_tdata (abfd)->shstrtab_hdr.sh_offset, SEEK_SET) != 0 | |
08a40648 | 5328 | || !_bfd_elf_strtab_emit (abfd, elf_shstrtab (abfd)))) |
b34976b6 | 5329 | return FALSE; |
252b5132 RH |
5330 | |
5331 | if (bed->elf_backend_final_write_processing) | |
5332 | (*bed->elf_backend_final_write_processing) (abfd, | |
5333 | elf_tdata (abfd)->linker); | |
5334 | ||
ff59fc36 RM |
5335 | if (!bed->s->write_shdrs_and_ehdr (abfd)) |
5336 | return FALSE; | |
5337 | ||
5338 | /* This is last since write_shdrs_and_ehdr can touch i_shdrp[0]. */ | |
bfb53a4f RM |
5339 | if (elf_tdata (abfd)->after_write_object_contents) |
5340 | return (*elf_tdata (abfd)->after_write_object_contents) (abfd); | |
ff59fc36 RM |
5341 | |
5342 | return TRUE; | |
252b5132 RH |
5343 | } |
5344 | ||
b34976b6 | 5345 | bfd_boolean |
217aa764 | 5346 | _bfd_elf_write_corefile_contents (bfd *abfd) |
252b5132 | 5347 | { |
c044fabd | 5348 | /* Hopefully this can be done just like an object file. */ |
252b5132 RH |
5349 | return _bfd_elf_write_object_contents (abfd); |
5350 | } | |
c044fabd KH |
5351 | |
5352 | /* Given a section, search the header to find them. */ | |
5353 | ||
cb33740c | 5354 | unsigned int |
198beae2 | 5355 | _bfd_elf_section_from_bfd_section (bfd *abfd, struct bfd_section *asect) |
252b5132 | 5356 | { |
9c5bfbb7 | 5357 | const struct elf_backend_data *bed; |
91d6fa6a | 5358 | unsigned int sec_index; |
252b5132 | 5359 | |
9ad5cbcf AM |
5360 | if (elf_section_data (asect) != NULL |
5361 | && elf_section_data (asect)->this_idx != 0) | |
5362 | return elf_section_data (asect)->this_idx; | |
5363 | ||
5364 | if (bfd_is_abs_section (asect)) | |
91d6fa6a | 5365 | sec_index = SHN_ABS; |
af746e92 | 5366 | else if (bfd_is_com_section (asect)) |
91d6fa6a | 5367 | sec_index = SHN_COMMON; |
af746e92 | 5368 | else if (bfd_is_und_section (asect)) |
91d6fa6a | 5369 | sec_index = SHN_UNDEF; |
af746e92 | 5370 | else |
91d6fa6a | 5371 | sec_index = SHN_BAD; |
252b5132 | 5372 | |
af746e92 | 5373 | bed = get_elf_backend_data (abfd); |
252b5132 RH |
5374 | if (bed->elf_backend_section_from_bfd_section) |
5375 | { | |
91d6fa6a | 5376 | int retval = sec_index; |
9ad5cbcf | 5377 | |
af746e92 AM |
5378 | if ((*bed->elf_backend_section_from_bfd_section) (abfd, asect, &retval)) |
5379 | return retval; | |
252b5132 RH |
5380 | } |
5381 | ||
91d6fa6a | 5382 | if (sec_index == SHN_BAD) |
af746e92 | 5383 | bfd_set_error (bfd_error_nonrepresentable_section); |
252b5132 | 5384 | |
91d6fa6a | 5385 | return sec_index; |
252b5132 RH |
5386 | } |
5387 | ||
5388 | /* Given a BFD symbol, return the index in the ELF symbol table, or -1 | |
5389 | on error. */ | |
5390 | ||
5391 | int | |
217aa764 | 5392 | _bfd_elf_symbol_from_bfd_symbol (bfd *abfd, asymbol **asym_ptr_ptr) |
252b5132 RH |
5393 | { |
5394 | asymbol *asym_ptr = *asym_ptr_ptr; | |
5395 | int idx; | |
5396 | flagword flags = asym_ptr->flags; | |
5397 | ||
5398 | /* When gas creates relocations against local labels, it creates its | |
5399 | own symbol for the section, but does put the symbol into the | |
5400 | symbol chain, so udata is 0. When the linker is generating | |
5401 | relocatable output, this section symbol may be for one of the | |
5402 | input sections rather than the output section. */ | |
5403 | if (asym_ptr->udata.i == 0 | |
5404 | && (flags & BSF_SECTION_SYM) | |
5405 | && asym_ptr->section) | |
5406 | { | |
5372391b | 5407 | asection *sec; |
252b5132 RH |
5408 | int indx; |
5409 | ||
5372391b AM |
5410 | sec = asym_ptr->section; |
5411 | if (sec->owner != abfd && sec->output_section != NULL) | |
5412 | sec = sec->output_section; | |
5413 | if (sec->owner == abfd | |
5414 | && (indx = sec->index) < elf_num_section_syms (abfd) | |
4e89ac30 | 5415 | && elf_section_syms (abfd)[indx] != NULL) |
252b5132 RH |
5416 | asym_ptr->udata.i = elf_section_syms (abfd)[indx]->udata.i; |
5417 | } | |
5418 | ||
5419 | idx = asym_ptr->udata.i; | |
5420 | ||
5421 | if (idx == 0) | |
5422 | { | |
5423 | /* This case can occur when using --strip-symbol on a symbol | |
08a40648 | 5424 | which is used in a relocation entry. */ |
252b5132 | 5425 | (*_bfd_error_handler) |
d003868e AM |
5426 | (_("%B: symbol `%s' required but not present"), |
5427 | abfd, bfd_asymbol_name (asym_ptr)); | |
252b5132 RH |
5428 | bfd_set_error (bfd_error_no_symbols); |
5429 | return -1; | |
5430 | } | |
5431 | ||
5432 | #if DEBUG & 4 | |
5433 | { | |
5434 | fprintf (stderr, | |
9ccb8af9 AM |
5435 | "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8lx\n", |
5436 | (long) asym_ptr, asym_ptr->name, idx, (long) flags); | |
252b5132 RH |
5437 | fflush (stderr); |
5438 | } | |
5439 | #endif | |
5440 | ||
5441 | return idx; | |
5442 | } | |
5443 | ||
84d1d650 | 5444 | /* Rewrite program header information. */ |
252b5132 | 5445 | |
b34976b6 | 5446 | static bfd_boolean |
84d1d650 | 5447 | rewrite_elf_program_header (bfd *ibfd, bfd *obfd) |
252b5132 | 5448 | { |
b34976b6 AM |
5449 | Elf_Internal_Ehdr *iehdr; |
5450 | struct elf_segment_map *map; | |
5451 | struct elf_segment_map *map_first; | |
5452 | struct elf_segment_map **pointer_to_map; | |
5453 | Elf_Internal_Phdr *segment; | |
5454 | asection *section; | |
5455 | unsigned int i; | |
5456 | unsigned int num_segments; | |
5457 | bfd_boolean phdr_included = FALSE; | |
5c44b38e | 5458 | bfd_boolean p_paddr_valid; |
b34976b6 AM |
5459 | bfd_vma maxpagesize; |
5460 | struct elf_segment_map *phdr_adjust_seg = NULL; | |
5461 | unsigned int phdr_adjust_num = 0; | |
9c5bfbb7 | 5462 | const struct elf_backend_data *bed; |
bc67d8a6 | 5463 | |
caf47ea6 | 5464 | bed = get_elf_backend_data (ibfd); |
252b5132 RH |
5465 | iehdr = elf_elfheader (ibfd); |
5466 | ||
bc67d8a6 | 5467 | map_first = NULL; |
c044fabd | 5468 | pointer_to_map = &map_first; |
252b5132 RH |
5469 | |
5470 | num_segments = elf_elfheader (ibfd)->e_phnum; | |
bc67d8a6 NC |
5471 | maxpagesize = get_elf_backend_data (obfd)->maxpagesize; |
5472 | ||
5473 | /* Returns the end address of the segment + 1. */ | |
aecc8f8a AM |
5474 | #define SEGMENT_END(segment, start) \ |
5475 | (start + (segment->p_memsz > segment->p_filesz \ | |
5476 | ? segment->p_memsz : segment->p_filesz)) | |
bc67d8a6 | 5477 | |
eecdbe52 JJ |
5478 | #define SECTION_SIZE(section, segment) \ |
5479 | (((section->flags & (SEC_HAS_CONTENTS | SEC_THREAD_LOCAL)) \ | |
5480 | != SEC_THREAD_LOCAL || segment->p_type == PT_TLS) \ | |
eea6121a | 5481 | ? section->size : 0) |
eecdbe52 | 5482 | |
b34976b6 | 5483 | /* Returns TRUE if the given section is contained within |
bc67d8a6 | 5484 | the given segment. VMA addresses are compared. */ |
aecc8f8a AM |
5485 | #define IS_CONTAINED_BY_VMA(section, segment) \ |
5486 | (section->vma >= segment->p_vaddr \ | |
eecdbe52 | 5487 | && (section->vma + SECTION_SIZE (section, segment) \ |
aecc8f8a | 5488 | <= (SEGMENT_END (segment, segment->p_vaddr)))) |
c044fabd | 5489 | |
b34976b6 | 5490 | /* Returns TRUE if the given section is contained within |
bc67d8a6 | 5491 | the given segment. LMA addresses are compared. */ |
aecc8f8a AM |
5492 | #define IS_CONTAINED_BY_LMA(section, segment, base) \ |
5493 | (section->lma >= base \ | |
eecdbe52 | 5494 | && (section->lma + SECTION_SIZE (section, segment) \ |
aecc8f8a | 5495 | <= SEGMENT_END (segment, base))) |
252b5132 | 5496 | |
0efc80c8 L |
5497 | /* Handle PT_NOTE segment. */ |
5498 | #define IS_NOTE(p, s) \ | |
aecc8f8a | 5499 | (p->p_type == PT_NOTE \ |
0efc80c8 | 5500 | && elf_section_type (s) == SHT_NOTE \ |
aecc8f8a | 5501 | && (bfd_vma) s->filepos >= p->p_offset \ |
cb3ff1e5 | 5502 | && ((bfd_vma) s->filepos + s->size \ |
aecc8f8a | 5503 | <= p->p_offset + p->p_filesz)) |
252b5132 | 5504 | |
0efc80c8 L |
5505 | /* Special case: corefile "NOTE" section containing regs, prpsinfo |
5506 | etc. */ | |
5507 | #define IS_COREFILE_NOTE(p, s) \ | |
5508 | (IS_NOTE (p, s) \ | |
5509 | && bfd_get_format (ibfd) == bfd_core \ | |
5510 | && s->vma == 0 \ | |
5511 | && s->lma == 0) | |
5512 | ||
252b5132 RH |
5513 | /* The complicated case when p_vaddr is 0 is to handle the Solaris |
5514 | linker, which generates a PT_INTERP section with p_vaddr and | |
5515 | p_memsz set to 0. */ | |
aecc8f8a AM |
5516 | #define IS_SOLARIS_PT_INTERP(p, s) \ |
5517 | (p->p_vaddr == 0 \ | |
5518 | && p->p_paddr == 0 \ | |
5519 | && p->p_memsz == 0 \ | |
5520 | && p->p_filesz > 0 \ | |
5521 | && (s->flags & SEC_HAS_CONTENTS) != 0 \ | |
eea6121a | 5522 | && s->size > 0 \ |
aecc8f8a | 5523 | && (bfd_vma) s->filepos >= p->p_offset \ |
cb3ff1e5 | 5524 | && ((bfd_vma) s->filepos + s->size \ |
aecc8f8a | 5525 | <= p->p_offset + p->p_filesz)) |
5c440b1e | 5526 | |
bc67d8a6 NC |
5527 | /* Decide if the given section should be included in the given segment. |
5528 | A section will be included if: | |
f5ffc919 | 5529 | 1. It is within the address space of the segment -- we use the LMA |
08a40648 | 5530 | if that is set for the segment and the VMA otherwise, |
0efc80c8 | 5531 | 2. It is an allocated section or a NOTE section in a PT_NOTE |
d324f6d6 | 5532 | segment. |
bc67d8a6 | 5533 | 3. There is an output section associated with it, |
eecdbe52 | 5534 | 4. The section has not already been allocated to a previous segment. |
2b05f1b7 | 5535 | 5. PT_GNU_STACK segments do not include any sections. |
03394ac9 | 5536 | 6. PT_TLS segment includes only SHF_TLS sections. |
6f79b219 JJ |
5537 | 7. SHF_TLS sections are only in PT_TLS or PT_LOAD segments. |
5538 | 8. PT_DYNAMIC should not contain empty sections at the beginning | |
08a40648 | 5539 | (with the possible exception of .dynamic). */ |
9f17e2a6 | 5540 | #define IS_SECTION_IN_INPUT_SEGMENT(section, segment, bed) \ |
2b05f1b7 L |
5541 | ((((segment->p_paddr \ |
5542 | ? IS_CONTAINED_BY_LMA (section, segment, segment->p_paddr) \ | |
5543 | : IS_CONTAINED_BY_VMA (section, segment)) \ | |
5544 | && (section->flags & SEC_ALLOC) != 0) \ | |
0efc80c8 | 5545 | || IS_NOTE (segment, section)) \ |
2b05f1b7 L |
5546 | && segment->p_type != PT_GNU_STACK \ |
5547 | && (segment->p_type != PT_TLS \ | |
5548 | || (section->flags & SEC_THREAD_LOCAL)) \ | |
5549 | && (segment->p_type == PT_LOAD \ | |
5550 | || segment->p_type == PT_TLS \ | |
5551 | || (section->flags & SEC_THREAD_LOCAL) == 0) \ | |
5552 | && (segment->p_type != PT_DYNAMIC \ | |
5553 | || SECTION_SIZE (section, segment) > 0 \ | |
5554 | || (segment->p_paddr \ | |
5555 | ? segment->p_paddr != section->lma \ | |
5556 | : segment->p_vaddr != section->vma) \ | |
5557 | || (strcmp (bfd_get_section_name (ibfd, section), ".dynamic") \ | |
5558 | == 0)) \ | |
0067a569 | 5559 | && !section->segment_mark) |
bc67d8a6 | 5560 | |
9f17e2a6 L |
5561 | /* If the output section of a section in the input segment is NULL, |
5562 | it is removed from the corresponding output segment. */ | |
5563 | #define INCLUDE_SECTION_IN_SEGMENT(section, segment, bed) \ | |
5564 | (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed) \ | |
5565 | && section->output_section != NULL) | |
5566 | ||
b34976b6 | 5567 | /* Returns TRUE iff seg1 starts after the end of seg2. */ |
b5f852ea NC |
5568 | #define SEGMENT_AFTER_SEGMENT(seg1, seg2, field) \ |
5569 | (seg1->field >= SEGMENT_END (seg2, seg2->field)) | |
5570 | ||
5571 | /* Returns TRUE iff seg1 and seg2 overlap. Segments overlap iff both | |
5572 | their VMA address ranges and their LMA address ranges overlap. | |
5573 | It is possible to have overlapping VMA ranges without overlapping LMA | |
5574 | ranges. RedBoot images for example can have both .data and .bss mapped | |
5575 | to the same VMA range, but with the .data section mapped to a different | |
5576 | LMA. */ | |
aecc8f8a | 5577 | #define SEGMENT_OVERLAPS(seg1, seg2) \ |
b5f852ea | 5578 | ( !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_vaddr) \ |
08a40648 | 5579 | || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_vaddr)) \ |
b5f852ea | 5580 | && !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_paddr) \ |
08a40648 | 5581 | || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_paddr))) |
bc67d8a6 NC |
5582 | |
5583 | /* Initialise the segment mark field. */ | |
5584 | for (section = ibfd->sections; section != NULL; section = section->next) | |
b34976b6 | 5585 | section->segment_mark = FALSE; |
bc67d8a6 | 5586 | |
5c44b38e AM |
5587 | /* The Solaris linker creates program headers in which all the |
5588 | p_paddr fields are zero. When we try to objcopy or strip such a | |
5589 | file, we get confused. Check for this case, and if we find it | |
5590 | don't set the p_paddr_valid fields. */ | |
5591 | p_paddr_valid = FALSE; | |
5592 | for (i = 0, segment = elf_tdata (ibfd)->phdr; | |
5593 | i < num_segments; | |
5594 | i++, segment++) | |
5595 | if (segment->p_paddr != 0) | |
5596 | { | |
5597 | p_paddr_valid = TRUE; | |
5598 | break; | |
5599 | } | |
5600 | ||
252b5132 | 5601 | /* Scan through the segments specified in the program header |
bc67d8a6 | 5602 | of the input BFD. For this first scan we look for overlaps |
9ad5cbcf | 5603 | in the loadable segments. These can be created by weird |
aecc8f8a | 5604 | parameters to objcopy. Also, fix some solaris weirdness. */ |
bc67d8a6 NC |
5605 | for (i = 0, segment = elf_tdata (ibfd)->phdr; |
5606 | i < num_segments; | |
c044fabd | 5607 | i++, segment++) |
252b5132 | 5608 | { |
252b5132 | 5609 | unsigned int j; |
c044fabd | 5610 | Elf_Internal_Phdr *segment2; |
252b5132 | 5611 | |
aecc8f8a AM |
5612 | if (segment->p_type == PT_INTERP) |
5613 | for (section = ibfd->sections; section; section = section->next) | |
5614 | if (IS_SOLARIS_PT_INTERP (segment, section)) | |
5615 | { | |
5616 | /* Mininal change so that the normal section to segment | |
4cc11e76 | 5617 | assignment code will work. */ |
aecc8f8a AM |
5618 | segment->p_vaddr = section->vma; |
5619 | break; | |
5620 | } | |
5621 | ||
bc67d8a6 | 5622 | if (segment->p_type != PT_LOAD) |
b10a8ae0 L |
5623 | { |
5624 | /* Remove PT_GNU_RELRO segment. */ | |
5625 | if (segment->p_type == PT_GNU_RELRO) | |
5626 | segment->p_type = PT_NULL; | |
5627 | continue; | |
5628 | } | |
c044fabd | 5629 | |
bc67d8a6 | 5630 | /* Determine if this segment overlaps any previous segments. */ |
0067a569 | 5631 | for (j = 0, segment2 = elf_tdata (ibfd)->phdr; j < i; j++, segment2++) |
bc67d8a6 NC |
5632 | { |
5633 | bfd_signed_vma extra_length; | |
c044fabd | 5634 | |
bc67d8a6 | 5635 | if (segment2->p_type != PT_LOAD |
0067a569 | 5636 | || !SEGMENT_OVERLAPS (segment, segment2)) |
bc67d8a6 | 5637 | continue; |
c044fabd | 5638 | |
bc67d8a6 NC |
5639 | /* Merge the two segments together. */ |
5640 | if (segment2->p_vaddr < segment->p_vaddr) | |
5641 | { | |
c044fabd | 5642 | /* Extend SEGMENT2 to include SEGMENT and then delete |
08a40648 | 5643 | SEGMENT. */ |
0067a569 AM |
5644 | extra_length = (SEGMENT_END (segment, segment->p_vaddr) |
5645 | - SEGMENT_END (segment2, segment2->p_vaddr)); | |
c044fabd | 5646 | |
bc67d8a6 NC |
5647 | if (extra_length > 0) |
5648 | { | |
0067a569 | 5649 | segment2->p_memsz += extra_length; |
bc67d8a6 NC |
5650 | segment2->p_filesz += extra_length; |
5651 | } | |
c044fabd | 5652 | |
bc67d8a6 | 5653 | segment->p_type = PT_NULL; |
c044fabd | 5654 | |
bc67d8a6 NC |
5655 | /* Since we have deleted P we must restart the outer loop. */ |
5656 | i = 0; | |
5657 | segment = elf_tdata (ibfd)->phdr; | |
5658 | break; | |
5659 | } | |
5660 | else | |
5661 | { | |
c044fabd | 5662 | /* Extend SEGMENT to include SEGMENT2 and then delete |
08a40648 | 5663 | SEGMENT2. */ |
0067a569 AM |
5664 | extra_length = (SEGMENT_END (segment2, segment2->p_vaddr) |
5665 | - SEGMENT_END (segment, segment->p_vaddr)); | |
c044fabd | 5666 | |
bc67d8a6 NC |
5667 | if (extra_length > 0) |
5668 | { | |
0067a569 | 5669 | segment->p_memsz += extra_length; |
bc67d8a6 NC |
5670 | segment->p_filesz += extra_length; |
5671 | } | |
c044fabd | 5672 | |
bc67d8a6 NC |
5673 | segment2->p_type = PT_NULL; |
5674 | } | |
5675 | } | |
5676 | } | |
c044fabd | 5677 | |
bc67d8a6 NC |
5678 | /* The second scan attempts to assign sections to segments. */ |
5679 | for (i = 0, segment = elf_tdata (ibfd)->phdr; | |
5680 | i < num_segments; | |
0067a569 | 5681 | i++, segment++) |
bc67d8a6 | 5682 | { |
0067a569 AM |
5683 | unsigned int section_count; |
5684 | asection **sections; | |
5685 | asection *output_section; | |
5686 | unsigned int isec; | |
5687 | bfd_vma matching_lma; | |
5688 | bfd_vma suggested_lma; | |
5689 | unsigned int j; | |
dc810e39 | 5690 | bfd_size_type amt; |
0067a569 AM |
5691 | asection *first_section; |
5692 | bfd_boolean first_matching_lma; | |
5693 | bfd_boolean first_suggested_lma; | |
bc67d8a6 NC |
5694 | |
5695 | if (segment->p_type == PT_NULL) | |
5696 | continue; | |
c044fabd | 5697 | |
9f17e2a6 | 5698 | first_section = NULL; |
bc67d8a6 | 5699 | /* Compute how many sections might be placed into this segment. */ |
b5f852ea NC |
5700 | for (section = ibfd->sections, section_count = 0; |
5701 | section != NULL; | |
5702 | section = section->next) | |
9f17e2a6 L |
5703 | { |
5704 | /* Find the first section in the input segment, which may be | |
5705 | removed from the corresponding output segment. */ | |
5706 | if (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed)) | |
5707 | { | |
5708 | if (first_section == NULL) | |
5709 | first_section = section; | |
5710 | if (section->output_section != NULL) | |
5711 | ++section_count; | |
5712 | } | |
5713 | } | |
811072d8 | 5714 | |
b5f852ea NC |
5715 | /* Allocate a segment map big enough to contain |
5716 | all of the sections we have selected. */ | |
dc810e39 AM |
5717 | amt = sizeof (struct elf_segment_map); |
5718 | amt += ((bfd_size_type) section_count - 1) * sizeof (asection *); | |
a50b1753 | 5719 | map = (struct elf_segment_map *) bfd_zalloc (obfd, amt); |
bc67d8a6 | 5720 | if (map == NULL) |
b34976b6 | 5721 | return FALSE; |
252b5132 RH |
5722 | |
5723 | /* Initialise the fields of the segment map. Default to | |
5724 | using the physical address of the segment in the input BFD. */ | |
0067a569 AM |
5725 | map->next = NULL; |
5726 | map->p_type = segment->p_type; | |
5727 | map->p_flags = segment->p_flags; | |
bc67d8a6 | 5728 | map->p_flags_valid = 1; |
55d55ac7 | 5729 | |
9f17e2a6 L |
5730 | /* If the first section in the input segment is removed, there is |
5731 | no need to preserve segment physical address in the corresponding | |
5732 | output segment. */ | |
945c025a | 5733 | if (!first_section || first_section->output_section != NULL) |
9f17e2a6 L |
5734 | { |
5735 | map->p_paddr = segment->p_paddr; | |
5c44b38e | 5736 | map->p_paddr_valid = p_paddr_valid; |
9f17e2a6 | 5737 | } |
252b5132 RH |
5738 | |
5739 | /* Determine if this segment contains the ELF file header | |
5740 | and if it contains the program headers themselves. */ | |
bc67d8a6 NC |
5741 | map->includes_filehdr = (segment->p_offset == 0 |
5742 | && segment->p_filesz >= iehdr->e_ehsize); | |
bc67d8a6 | 5743 | map->includes_phdrs = 0; |
252b5132 | 5744 | |
0067a569 | 5745 | if (!phdr_included || segment->p_type != PT_LOAD) |
252b5132 | 5746 | { |
bc67d8a6 NC |
5747 | map->includes_phdrs = |
5748 | (segment->p_offset <= (bfd_vma) iehdr->e_phoff | |
5749 | && (segment->p_offset + segment->p_filesz | |
252b5132 RH |
5750 | >= ((bfd_vma) iehdr->e_phoff |
5751 | + iehdr->e_phnum * iehdr->e_phentsize))); | |
c044fabd | 5752 | |
bc67d8a6 | 5753 | if (segment->p_type == PT_LOAD && map->includes_phdrs) |
b34976b6 | 5754 | phdr_included = TRUE; |
252b5132 RH |
5755 | } |
5756 | ||
bc67d8a6 | 5757 | if (section_count == 0) |
252b5132 RH |
5758 | { |
5759 | /* Special segments, such as the PT_PHDR segment, may contain | |
5760 | no sections, but ordinary, loadable segments should contain | |
1ed89aa9 NC |
5761 | something. They are allowed by the ELF spec however, so only |
5762 | a warning is produced. */ | |
bc67d8a6 | 5763 | if (segment->p_type == PT_LOAD) |
0067a569 AM |
5764 | (*_bfd_error_handler) (_("%B: warning: Empty loadable segment" |
5765 | " detected, is this intentional ?\n"), | |
5766 | ibfd); | |
252b5132 | 5767 | |
bc67d8a6 | 5768 | map->count = 0; |
c044fabd KH |
5769 | *pointer_to_map = map; |
5770 | pointer_to_map = &map->next; | |
252b5132 RH |
5771 | |
5772 | continue; | |
5773 | } | |
5774 | ||
5775 | /* Now scan the sections in the input BFD again and attempt | |
5776 | to add their corresponding output sections to the segment map. | |
5777 | The problem here is how to handle an output section which has | |
5778 | been moved (ie had its LMA changed). There are four possibilities: | |
5779 | ||
5780 | 1. None of the sections have been moved. | |
5781 | In this case we can continue to use the segment LMA from the | |
5782 | input BFD. | |
5783 | ||
5784 | 2. All of the sections have been moved by the same amount. | |
5785 | In this case we can change the segment's LMA to match the LMA | |
5786 | of the first section. | |
5787 | ||
5788 | 3. Some of the sections have been moved, others have not. | |
5789 | In this case those sections which have not been moved can be | |
5790 | placed in the current segment which will have to have its size, | |
5791 | and possibly its LMA changed, and a new segment or segments will | |
5792 | have to be created to contain the other sections. | |
5793 | ||
b5f852ea | 5794 | 4. The sections have been moved, but not by the same amount. |
252b5132 RH |
5795 | In this case we can change the segment's LMA to match the LMA |
5796 | of the first section and we will have to create a new segment | |
5797 | or segments to contain the other sections. | |
5798 | ||
5799 | In order to save time, we allocate an array to hold the section | |
5800 | pointers that we are interested in. As these sections get assigned | |
5801 | to a segment, they are removed from this array. */ | |
5802 | ||
a50b1753 | 5803 | sections = (asection **) bfd_malloc2 (section_count, sizeof (asection *)); |
252b5132 | 5804 | if (sections == NULL) |
b34976b6 | 5805 | return FALSE; |
252b5132 RH |
5806 | |
5807 | /* Step One: Scan for segment vs section LMA conflicts. | |
5808 | Also add the sections to the section array allocated above. | |
5809 | Also add the sections to the current segment. In the common | |
5810 | case, where the sections have not been moved, this means that | |
5811 | we have completely filled the segment, and there is nothing | |
5812 | more to do. */ | |
252b5132 | 5813 | isec = 0; |
72730e0c | 5814 | matching_lma = 0; |
252b5132 | 5815 | suggested_lma = 0; |
0067a569 AM |
5816 | first_matching_lma = TRUE; |
5817 | first_suggested_lma = TRUE; | |
252b5132 | 5818 | |
147d51c2 | 5819 | for (section = ibfd->sections; |
bc67d8a6 NC |
5820 | section != NULL; |
5821 | section = section->next) | |
147d51c2 L |
5822 | if (section == first_section) |
5823 | break; | |
5824 | ||
5825 | for (j = 0; section != NULL; section = section->next) | |
252b5132 | 5826 | { |
caf47ea6 | 5827 | if (INCLUDE_SECTION_IN_SEGMENT (section, segment, bed)) |
c0f7859b | 5828 | { |
bc67d8a6 NC |
5829 | output_section = section->output_section; |
5830 | ||
0067a569 | 5831 | sections[j++] = section; |
252b5132 RH |
5832 | |
5833 | /* The Solaris native linker always sets p_paddr to 0. | |
5834 | We try to catch that case here, and set it to the | |
5e8d7549 NC |
5835 | correct value. Note - some backends require that |
5836 | p_paddr be left as zero. */ | |
5c44b38e | 5837 | if (!p_paddr_valid |
4455705d | 5838 | && segment->p_vaddr != 0 |
0067a569 | 5839 | && !bed->want_p_paddr_set_to_zero |
252b5132 | 5840 | && isec == 0 |
bc67d8a6 | 5841 | && output_section->lma != 0 |
0067a569 AM |
5842 | && output_section->vma == (segment->p_vaddr |
5843 | + (map->includes_filehdr | |
5844 | ? iehdr->e_ehsize | |
5845 | : 0) | |
5846 | + (map->includes_phdrs | |
5847 | ? (iehdr->e_phnum | |
5848 | * iehdr->e_phentsize) | |
5849 | : 0))) | |
bc67d8a6 | 5850 | map->p_paddr = segment->p_vaddr; |
252b5132 RH |
5851 | |
5852 | /* Match up the physical address of the segment with the | |
5853 | LMA address of the output section. */ | |
bc67d8a6 | 5854 | if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr) |
5e8d7549 | 5855 | || IS_COREFILE_NOTE (segment, section) |
0067a569 AM |
5856 | || (bed->want_p_paddr_set_to_zero |
5857 | && IS_CONTAINED_BY_VMA (output_section, segment))) | |
252b5132 | 5858 | { |
0067a569 AM |
5859 | if (first_matching_lma || output_section->lma < matching_lma) |
5860 | { | |
5861 | matching_lma = output_section->lma; | |
5862 | first_matching_lma = FALSE; | |
5863 | } | |
252b5132 RH |
5864 | |
5865 | /* We assume that if the section fits within the segment | |
bc67d8a6 | 5866 | then it does not overlap any other section within that |
252b5132 | 5867 | segment. */ |
0067a569 AM |
5868 | map->sections[isec++] = output_section; |
5869 | } | |
5870 | else if (first_suggested_lma) | |
5871 | { | |
5872 | suggested_lma = output_section->lma; | |
5873 | first_suggested_lma = FALSE; | |
252b5132 | 5874 | } |
147d51c2 L |
5875 | |
5876 | if (j == section_count) | |
5877 | break; | |
252b5132 RH |
5878 | } |
5879 | } | |
5880 | ||
bc67d8a6 | 5881 | BFD_ASSERT (j == section_count); |
252b5132 RH |
5882 | |
5883 | /* Step Two: Adjust the physical address of the current segment, | |
5884 | if necessary. */ | |
bc67d8a6 | 5885 | if (isec == section_count) |
252b5132 RH |
5886 | { |
5887 | /* All of the sections fitted within the segment as currently | |
5888 | specified. This is the default case. Add the segment to | |
5889 | the list of built segments and carry on to process the next | |
5890 | program header in the input BFD. */ | |
bc67d8a6 | 5891 | map->count = section_count; |
c044fabd KH |
5892 | *pointer_to_map = map; |
5893 | pointer_to_map = &map->next; | |
08a40648 | 5894 | |
5c44b38e AM |
5895 | if (p_paddr_valid |
5896 | && !bed->want_p_paddr_set_to_zero | |
147d51c2 | 5897 | && matching_lma != map->p_paddr |
5c44b38e AM |
5898 | && !map->includes_filehdr |
5899 | && !map->includes_phdrs) | |
3271a814 NS |
5900 | /* There is some padding before the first section in the |
5901 | segment. So, we must account for that in the output | |
5902 | segment's vma. */ | |
5903 | map->p_vaddr_offset = matching_lma - map->p_paddr; | |
08a40648 | 5904 | |
252b5132 RH |
5905 | free (sections); |
5906 | continue; | |
5907 | } | |
252b5132 RH |
5908 | else |
5909 | { | |
0067a569 | 5910 | if (!first_matching_lma) |
72730e0c AM |
5911 | { |
5912 | /* At least one section fits inside the current segment. | |
5913 | Keep it, but modify its physical address to match the | |
5914 | LMA of the first section that fitted. */ | |
bc67d8a6 | 5915 | map->p_paddr = matching_lma; |
72730e0c AM |
5916 | } |
5917 | else | |
5918 | { | |
5919 | /* None of the sections fitted inside the current segment. | |
5920 | Change the current segment's physical address to match | |
5921 | the LMA of the first section. */ | |
bc67d8a6 | 5922 | map->p_paddr = suggested_lma; |
72730e0c AM |
5923 | } |
5924 | ||
bc67d8a6 NC |
5925 | /* Offset the segment physical address from the lma |
5926 | to allow for space taken up by elf headers. */ | |
5927 | if (map->includes_filehdr) | |
010c8431 AM |
5928 | { |
5929 | if (map->p_paddr >= iehdr->e_ehsize) | |
5930 | map->p_paddr -= iehdr->e_ehsize; | |
5931 | else | |
5932 | { | |
5933 | map->includes_filehdr = FALSE; | |
5934 | map->includes_phdrs = FALSE; | |
5935 | } | |
5936 | } | |
252b5132 | 5937 | |
bc67d8a6 NC |
5938 | if (map->includes_phdrs) |
5939 | { | |
010c8431 AM |
5940 | if (map->p_paddr >= iehdr->e_phnum * iehdr->e_phentsize) |
5941 | { | |
5942 | map->p_paddr -= iehdr->e_phnum * iehdr->e_phentsize; | |
5943 | ||
5944 | /* iehdr->e_phnum is just an estimate of the number | |
5945 | of program headers that we will need. Make a note | |
5946 | here of the number we used and the segment we chose | |
5947 | to hold these headers, so that we can adjust the | |
5948 | offset when we know the correct value. */ | |
5949 | phdr_adjust_num = iehdr->e_phnum; | |
5950 | phdr_adjust_seg = map; | |
5951 | } | |
5952 | else | |
5953 | map->includes_phdrs = FALSE; | |
bc67d8a6 | 5954 | } |
252b5132 RH |
5955 | } |
5956 | ||
5957 | /* Step Three: Loop over the sections again, this time assigning | |
caf47ea6 | 5958 | those that fit to the current segment and removing them from the |
252b5132 RH |
5959 | sections array; but making sure not to leave large gaps. Once all |
5960 | possible sections have been assigned to the current segment it is | |
5961 | added to the list of built segments and if sections still remain | |
5962 | to be assigned, a new segment is constructed before repeating | |
5963 | the loop. */ | |
5964 | isec = 0; | |
5965 | do | |
5966 | { | |
bc67d8a6 | 5967 | map->count = 0; |
252b5132 | 5968 | suggested_lma = 0; |
0067a569 | 5969 | first_suggested_lma = TRUE; |
252b5132 RH |
5970 | |
5971 | /* Fill the current segment with sections that fit. */ | |
bc67d8a6 | 5972 | for (j = 0; j < section_count; j++) |
252b5132 | 5973 | { |
bc67d8a6 | 5974 | section = sections[j]; |
252b5132 | 5975 | |
bc67d8a6 | 5976 | if (section == NULL) |
252b5132 RH |
5977 | continue; |
5978 | ||
bc67d8a6 | 5979 | output_section = section->output_section; |
252b5132 | 5980 | |
bc67d8a6 | 5981 | BFD_ASSERT (output_section != NULL); |
c044fabd | 5982 | |
bc67d8a6 NC |
5983 | if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr) |
5984 | || IS_COREFILE_NOTE (segment, section)) | |
252b5132 | 5985 | { |
bc67d8a6 | 5986 | if (map->count == 0) |
252b5132 RH |
5987 | { |
5988 | /* If the first section in a segment does not start at | |
bc67d8a6 NC |
5989 | the beginning of the segment, then something is |
5990 | wrong. */ | |
0067a569 AM |
5991 | if (output_section->lma |
5992 | != (map->p_paddr | |
5993 | + (map->includes_filehdr ? iehdr->e_ehsize : 0) | |
5994 | + (map->includes_phdrs | |
5995 | ? iehdr->e_phnum * iehdr->e_phentsize | |
5996 | : 0))) | |
252b5132 RH |
5997 | abort (); |
5998 | } | |
5999 | else | |
6000 | { | |
0067a569 | 6001 | asection *prev_sec; |
252b5132 | 6002 | |
bc67d8a6 | 6003 | prev_sec = map->sections[map->count - 1]; |
252b5132 RH |
6004 | |
6005 | /* If the gap between the end of the previous section | |
bc67d8a6 NC |
6006 | and the start of this section is more than |
6007 | maxpagesize then we need to start a new segment. */ | |
eea6121a | 6008 | if ((BFD_ALIGN (prev_sec->lma + prev_sec->size, |
079e9a2f | 6009 | maxpagesize) |
caf47ea6 | 6010 | < BFD_ALIGN (output_section->lma, maxpagesize)) |
0067a569 | 6011 | || (prev_sec->lma + prev_sec->size |
079e9a2f | 6012 | > output_section->lma)) |
252b5132 | 6013 | { |
0067a569 AM |
6014 | if (first_suggested_lma) |
6015 | { | |
6016 | suggested_lma = output_section->lma; | |
6017 | first_suggested_lma = FALSE; | |
6018 | } | |
252b5132 RH |
6019 | |
6020 | continue; | |
6021 | } | |
6022 | } | |
6023 | ||
bc67d8a6 | 6024 | map->sections[map->count++] = output_section; |
252b5132 RH |
6025 | ++isec; |
6026 | sections[j] = NULL; | |
b34976b6 | 6027 | section->segment_mark = TRUE; |
252b5132 | 6028 | } |
0067a569 AM |
6029 | else if (first_suggested_lma) |
6030 | { | |
6031 | suggested_lma = output_section->lma; | |
6032 | first_suggested_lma = FALSE; | |
6033 | } | |
252b5132 RH |
6034 | } |
6035 | ||
bc67d8a6 | 6036 | BFD_ASSERT (map->count > 0); |
252b5132 RH |
6037 | |
6038 | /* Add the current segment to the list of built segments. */ | |
c044fabd KH |
6039 | *pointer_to_map = map; |
6040 | pointer_to_map = &map->next; | |
252b5132 | 6041 | |
bc67d8a6 | 6042 | if (isec < section_count) |
252b5132 RH |
6043 | { |
6044 | /* We still have not allocated all of the sections to | |
6045 | segments. Create a new segment here, initialise it | |
6046 | and carry on looping. */ | |
dc810e39 AM |
6047 | amt = sizeof (struct elf_segment_map); |
6048 | amt += ((bfd_size_type) section_count - 1) * sizeof (asection *); | |
5964fc3a | 6049 | map = (struct elf_segment_map *) bfd_zalloc (obfd, amt); |
bc67d8a6 | 6050 | if (map == NULL) |
5ed6aba4 NC |
6051 | { |
6052 | free (sections); | |
6053 | return FALSE; | |
6054 | } | |
252b5132 RH |
6055 | |
6056 | /* Initialise the fields of the segment map. Set the physical | |
6057 | physical address to the LMA of the first section that has | |
6058 | not yet been assigned. */ | |
0067a569 AM |
6059 | map->next = NULL; |
6060 | map->p_type = segment->p_type; | |
6061 | map->p_flags = segment->p_flags; | |
6062 | map->p_flags_valid = 1; | |
6063 | map->p_paddr = suggested_lma; | |
5c44b38e | 6064 | map->p_paddr_valid = p_paddr_valid; |
bc67d8a6 | 6065 | map->includes_filehdr = 0; |
0067a569 | 6066 | map->includes_phdrs = 0; |
252b5132 RH |
6067 | } |
6068 | } | |
bc67d8a6 | 6069 | while (isec < section_count); |
252b5132 RH |
6070 | |
6071 | free (sections); | |
6072 | } | |
6073 | ||
bc67d8a6 NC |
6074 | elf_tdata (obfd)->segment_map = map_first; |
6075 | ||
6076 | /* If we had to estimate the number of program headers that were | |
9ad5cbcf | 6077 | going to be needed, then check our estimate now and adjust |
bc67d8a6 NC |
6078 | the offset if necessary. */ |
6079 | if (phdr_adjust_seg != NULL) | |
6080 | { | |
6081 | unsigned int count; | |
c044fabd | 6082 | |
bc67d8a6 | 6083 | for (count = 0, map = map_first; map != NULL; map = map->next) |
c044fabd | 6084 | count++; |
252b5132 | 6085 | |
bc67d8a6 NC |
6086 | if (count > phdr_adjust_num) |
6087 | phdr_adjust_seg->p_paddr | |
6088 | -= (count - phdr_adjust_num) * iehdr->e_phentsize; | |
6089 | } | |
c044fabd | 6090 | |
bc67d8a6 | 6091 | #undef SEGMENT_END |
eecdbe52 | 6092 | #undef SECTION_SIZE |
bc67d8a6 NC |
6093 | #undef IS_CONTAINED_BY_VMA |
6094 | #undef IS_CONTAINED_BY_LMA | |
0efc80c8 | 6095 | #undef IS_NOTE |
252b5132 | 6096 | #undef IS_COREFILE_NOTE |
bc67d8a6 | 6097 | #undef IS_SOLARIS_PT_INTERP |
9f17e2a6 | 6098 | #undef IS_SECTION_IN_INPUT_SEGMENT |
bc67d8a6 NC |
6099 | #undef INCLUDE_SECTION_IN_SEGMENT |
6100 | #undef SEGMENT_AFTER_SEGMENT | |
6101 | #undef SEGMENT_OVERLAPS | |
b34976b6 | 6102 | return TRUE; |
252b5132 RH |
6103 | } |
6104 | ||
84d1d650 L |
6105 | /* Copy ELF program header information. */ |
6106 | ||
6107 | static bfd_boolean | |
6108 | copy_elf_program_header (bfd *ibfd, bfd *obfd) | |
6109 | { | |
6110 | Elf_Internal_Ehdr *iehdr; | |
6111 | struct elf_segment_map *map; | |
6112 | struct elf_segment_map *map_first; | |
6113 | struct elf_segment_map **pointer_to_map; | |
6114 | Elf_Internal_Phdr *segment; | |
6115 | unsigned int i; | |
6116 | unsigned int num_segments; | |
6117 | bfd_boolean phdr_included = FALSE; | |
88967714 | 6118 | bfd_boolean p_paddr_valid; |
84d1d650 L |
6119 | |
6120 | iehdr = elf_elfheader (ibfd); | |
6121 | ||
6122 | map_first = NULL; | |
6123 | pointer_to_map = &map_first; | |
6124 | ||
88967714 AM |
6125 | /* If all the segment p_paddr fields are zero, don't set |
6126 | map->p_paddr_valid. */ | |
6127 | p_paddr_valid = FALSE; | |
84d1d650 | 6128 | num_segments = elf_elfheader (ibfd)->e_phnum; |
88967714 AM |
6129 | for (i = 0, segment = elf_tdata (ibfd)->phdr; |
6130 | i < num_segments; | |
6131 | i++, segment++) | |
6132 | if (segment->p_paddr != 0) | |
6133 | { | |
6134 | p_paddr_valid = TRUE; | |
6135 | break; | |
6136 | } | |
6137 | ||
84d1d650 L |
6138 | for (i = 0, segment = elf_tdata (ibfd)->phdr; |
6139 | i < num_segments; | |
6140 | i++, segment++) | |
6141 | { | |
6142 | asection *section; | |
6143 | unsigned int section_count; | |
6144 | bfd_size_type amt; | |
6145 | Elf_Internal_Shdr *this_hdr; | |
53020534 | 6146 | asection *first_section = NULL; |
a76e6f2f | 6147 | asection *lowest_section; |
84d1d650 | 6148 | |
84d1d650 L |
6149 | /* Compute how many sections are in this segment. */ |
6150 | for (section = ibfd->sections, section_count = 0; | |
6151 | section != NULL; | |
6152 | section = section->next) | |
6153 | { | |
6154 | this_hdr = &(elf_section_data(section)->this_hdr); | |
f4638467 | 6155 | if (ELF_SECTION_IN_SEGMENT (this_hdr, segment)) |
3271a814 | 6156 | { |
a76e6f2f AM |
6157 | if (first_section == NULL) |
6158 | first_section = section; | |
3271a814 NS |
6159 | section_count++; |
6160 | } | |
84d1d650 L |
6161 | } |
6162 | ||
6163 | /* Allocate a segment map big enough to contain | |
6164 | all of the sections we have selected. */ | |
6165 | amt = sizeof (struct elf_segment_map); | |
6166 | if (section_count != 0) | |
6167 | amt += ((bfd_size_type) section_count - 1) * sizeof (asection *); | |
a50b1753 | 6168 | map = (struct elf_segment_map *) bfd_zalloc (obfd, amt); |
84d1d650 L |
6169 | if (map == NULL) |
6170 | return FALSE; | |
6171 | ||
6172 | /* Initialize the fields of the output segment map with the | |
6173 | input segment. */ | |
6174 | map->next = NULL; | |
6175 | map->p_type = segment->p_type; | |
6176 | map->p_flags = segment->p_flags; | |
6177 | map->p_flags_valid = 1; | |
6178 | map->p_paddr = segment->p_paddr; | |
88967714 | 6179 | map->p_paddr_valid = p_paddr_valid; |
3f570048 AM |
6180 | map->p_align = segment->p_align; |
6181 | map->p_align_valid = 1; | |
3271a814 | 6182 | map->p_vaddr_offset = 0; |
84d1d650 | 6183 | |
04c3a755 NS |
6184 | if (map->p_type == PT_GNU_RELRO |
6185 | || map->p_type == PT_GNU_STACK) | |
b10a8ae0 L |
6186 | { |
6187 | /* The PT_GNU_RELRO segment may contain the first a few | |
6188 | bytes in the .got.plt section even if the whole .got.plt | |
6189 | section isn't in the PT_GNU_RELRO segment. We won't | |
04c3a755 NS |
6190 | change the size of the PT_GNU_RELRO segment. |
6191 | Similarly, PT_GNU_STACK size is significant on uclinux | |
6192 | systems. */ | |
9433b9b1 | 6193 | map->p_size = segment->p_memsz; |
b10a8ae0 L |
6194 | map->p_size_valid = 1; |
6195 | } | |
6196 | ||
84d1d650 L |
6197 | /* Determine if this segment contains the ELF file header |
6198 | and if it contains the program headers themselves. */ | |
6199 | map->includes_filehdr = (segment->p_offset == 0 | |
6200 | && segment->p_filesz >= iehdr->e_ehsize); | |
6201 | ||
6202 | map->includes_phdrs = 0; | |
6203 | if (! phdr_included || segment->p_type != PT_LOAD) | |
6204 | { | |
6205 | map->includes_phdrs = | |
6206 | (segment->p_offset <= (bfd_vma) iehdr->e_phoff | |
6207 | && (segment->p_offset + segment->p_filesz | |
6208 | >= ((bfd_vma) iehdr->e_phoff | |
6209 | + iehdr->e_phnum * iehdr->e_phentsize))); | |
6210 | ||
6211 | if (segment->p_type == PT_LOAD && map->includes_phdrs) | |
6212 | phdr_included = TRUE; | |
6213 | } | |
6214 | ||
a76e6f2f | 6215 | lowest_section = first_section; |
84d1d650 L |
6216 | if (section_count != 0) |
6217 | { | |
6218 | unsigned int isec = 0; | |
6219 | ||
53020534 | 6220 | for (section = first_section; |
84d1d650 L |
6221 | section != NULL; |
6222 | section = section->next) | |
6223 | { | |
6224 | this_hdr = &(elf_section_data(section)->this_hdr); | |
f4638467 | 6225 | if (ELF_SECTION_IN_SEGMENT (this_hdr, segment)) |
53020534 L |
6226 | { |
6227 | map->sections[isec++] = section->output_section; | |
a76e6f2f AM |
6228 | if (section->lma < lowest_section->lma) |
6229 | lowest_section = section; | |
6230 | if ((section->flags & SEC_ALLOC) != 0) | |
6231 | { | |
6232 | bfd_vma seg_off; | |
6233 | ||
6234 | /* Section lmas are set up from PT_LOAD header | |
6235 | p_paddr in _bfd_elf_make_section_from_shdr. | |
6236 | If this header has a p_paddr that disagrees | |
6237 | with the section lma, flag the p_paddr as | |
6238 | invalid. */ | |
6239 | if ((section->flags & SEC_LOAD) != 0) | |
6240 | seg_off = this_hdr->sh_offset - segment->p_offset; | |
6241 | else | |
6242 | seg_off = this_hdr->sh_addr - segment->p_vaddr; | |
6243 | if (section->lma - segment->p_paddr != seg_off) | |
6244 | map->p_paddr_valid = FALSE; | |
6245 | } | |
53020534 L |
6246 | if (isec == section_count) |
6247 | break; | |
6248 | } | |
84d1d650 L |
6249 | } |
6250 | } | |
6251 | ||
a76e6f2f AM |
6252 | if (map->includes_filehdr && lowest_section != NULL) |
6253 | /* We need to keep the space used by the headers fixed. */ | |
6254 | map->header_size = lowest_section->vma - segment->p_vaddr; | |
d324f6d6 | 6255 | |
a76e6f2f AM |
6256 | if (!map->includes_phdrs |
6257 | && !map->includes_filehdr | |
6258 | && map->p_paddr_valid) | |
6259 | /* There is some other padding before the first section. */ | |
6260 | map->p_vaddr_offset = ((lowest_section ? lowest_section->lma : 0) | |
6261 | - segment->p_paddr); | |
6262 | ||
84d1d650 L |
6263 | map->count = section_count; |
6264 | *pointer_to_map = map; | |
6265 | pointer_to_map = &map->next; | |
6266 | } | |
6267 | ||
6268 | elf_tdata (obfd)->segment_map = map_first; | |
6269 | return TRUE; | |
6270 | } | |
6271 | ||
6272 | /* Copy private BFD data. This copies or rewrites ELF program header | |
6273 | information. */ | |
6274 | ||
6275 | static bfd_boolean | |
6276 | copy_private_bfd_data (bfd *ibfd, bfd *obfd) | |
6277 | { | |
84d1d650 L |
6278 | if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour |
6279 | || bfd_get_flavour (obfd) != bfd_target_elf_flavour) | |
6280 | return TRUE; | |
6281 | ||
6282 | if (elf_tdata (ibfd)->phdr == NULL) | |
6283 | return TRUE; | |
6284 | ||
6285 | if (ibfd->xvec == obfd->xvec) | |
6286 | { | |
cb3ff1e5 NC |
6287 | /* Check to see if any sections in the input BFD |
6288 | covered by ELF program header have changed. */ | |
d55ce4e2 | 6289 | Elf_Internal_Phdr *segment; |
84d1d650 L |
6290 | asection *section, *osec; |
6291 | unsigned int i, num_segments; | |
6292 | Elf_Internal_Shdr *this_hdr; | |
147d51c2 L |
6293 | const struct elf_backend_data *bed; |
6294 | ||
6295 | bed = get_elf_backend_data (ibfd); | |
6296 | ||
6297 | /* Regenerate the segment map if p_paddr is set to 0. */ | |
6298 | if (bed->want_p_paddr_set_to_zero) | |
6299 | goto rewrite; | |
84d1d650 L |
6300 | |
6301 | /* Initialize the segment mark field. */ | |
6302 | for (section = obfd->sections; section != NULL; | |
6303 | section = section->next) | |
6304 | section->segment_mark = FALSE; | |
6305 | ||
6306 | num_segments = elf_elfheader (ibfd)->e_phnum; | |
6307 | for (i = 0, segment = elf_tdata (ibfd)->phdr; | |
6308 | i < num_segments; | |
6309 | i++, segment++) | |
6310 | { | |
5f6999aa NC |
6311 | /* PR binutils/3535. The Solaris linker always sets the p_paddr |
6312 | and p_memsz fields of special segments (DYNAMIC, INTERP) to 0 | |
6313 | which severly confuses things, so always regenerate the segment | |
6314 | map in this case. */ | |
6315 | if (segment->p_paddr == 0 | |
6316 | && segment->p_memsz == 0 | |
6317 | && (segment->p_type == PT_INTERP || segment->p_type == PT_DYNAMIC)) | |
cb3ff1e5 | 6318 | goto rewrite; |
5f6999aa | 6319 | |
84d1d650 L |
6320 | for (section = ibfd->sections; |
6321 | section != NULL; section = section->next) | |
6322 | { | |
6323 | /* We mark the output section so that we know it comes | |
6324 | from the input BFD. */ | |
6325 | osec = section->output_section; | |
6326 | if (osec) | |
6327 | osec->segment_mark = TRUE; | |
6328 | ||
6329 | /* Check if this section is covered by the segment. */ | |
6330 | this_hdr = &(elf_section_data(section)->this_hdr); | |
f4638467 | 6331 | if (ELF_SECTION_IN_SEGMENT (this_hdr, segment)) |
84d1d650 L |
6332 | { |
6333 | /* FIXME: Check if its output section is changed or | |
6334 | removed. What else do we need to check? */ | |
6335 | if (osec == NULL | |
6336 | || section->flags != osec->flags | |
6337 | || section->lma != osec->lma | |
6338 | || section->vma != osec->vma | |
6339 | || section->size != osec->size | |
6340 | || section->rawsize != osec->rawsize | |
6341 | || section->alignment_power != osec->alignment_power) | |
6342 | goto rewrite; | |
6343 | } | |
6344 | } | |
6345 | } | |
6346 | ||
cb3ff1e5 | 6347 | /* Check to see if any output section do not come from the |
84d1d650 L |
6348 | input BFD. */ |
6349 | for (section = obfd->sections; section != NULL; | |
6350 | section = section->next) | |
6351 | { | |
6352 | if (section->segment_mark == FALSE) | |
6353 | goto rewrite; | |
6354 | else | |
6355 | section->segment_mark = FALSE; | |
6356 | } | |
6357 | ||
6358 | return copy_elf_program_header (ibfd, obfd); | |
6359 | } | |
6360 | ||
6361 | rewrite: | |
f1d85785 L |
6362 | if (ibfd->xvec == obfd->xvec) |
6363 | { | |
6364 | /* When rewriting program header, set the output maxpagesize to | |
6365 | the maximum alignment of input PT_LOAD segments. */ | |
6366 | Elf_Internal_Phdr *segment; | |
6367 | unsigned int i; | |
6368 | unsigned int num_segments = elf_elfheader (ibfd)->e_phnum; | |
6369 | bfd_vma maxpagesize = 0; | |
6370 | ||
6371 | for (i = 0, segment = elf_tdata (ibfd)->phdr; | |
6372 | i < num_segments; | |
6373 | i++, segment++) | |
6374 | if (segment->p_type == PT_LOAD | |
6375 | && maxpagesize < segment->p_align) | |
6376 | maxpagesize = segment->p_align; | |
6377 | ||
6378 | if (maxpagesize != get_elf_backend_data (obfd)->maxpagesize) | |
6379 | bfd_emul_set_maxpagesize (bfd_get_target (obfd), maxpagesize); | |
6380 | } | |
6381 | ||
84d1d650 L |
6382 | return rewrite_elf_program_header (ibfd, obfd); |
6383 | } | |
6384 | ||
ccd2ec6a L |
6385 | /* Initialize private output section information from input section. */ |
6386 | ||
6387 | bfd_boolean | |
6388 | _bfd_elf_init_private_section_data (bfd *ibfd, | |
6389 | asection *isec, | |
6390 | bfd *obfd, | |
6391 | asection *osec, | |
6392 | struct bfd_link_info *link_info) | |
6393 | ||
6394 | { | |
6395 | Elf_Internal_Shdr *ihdr, *ohdr; | |
dfa7b0b8 | 6396 | bfd_boolean final_link = link_info != NULL && !link_info->relocatable; |
ccd2ec6a L |
6397 | |
6398 | if (ibfd->xvec->flavour != bfd_target_elf_flavour | |
6399 | || obfd->xvec->flavour != bfd_target_elf_flavour) | |
6400 | return TRUE; | |
6401 | ||
ba85c43e NC |
6402 | BFD_ASSERT (elf_section_data (osec) != NULL); |
6403 | ||
dfa7b0b8 AM |
6404 | /* For objcopy and relocatable link, don't copy the output ELF |
6405 | section type from input if the output BFD section flags have been | |
6406 | set to something different. For a final link allow some flags | |
6407 | that the linker clears to differ. */ | |
42bb2e33 | 6408 | if (elf_section_type (osec) == SHT_NULL |
dfa7b0b8 AM |
6409 | && (osec->flags == isec->flags |
6410 | || (final_link | |
6411 | && ((osec->flags ^ isec->flags) | |
0814be7d | 6412 | & ~(SEC_LINK_ONCE | SEC_LINK_DUPLICATES | SEC_RELOC)) == 0))) |
42bb2e33 | 6413 | elf_section_type (osec) = elf_section_type (isec); |
d270463e L |
6414 | |
6415 | /* FIXME: Is this correct for all OS/PROC specific flags? */ | |
6416 | elf_section_flags (osec) |= (elf_section_flags (isec) | |
6417 | & (SHF_MASKOS | SHF_MASKPROC)); | |
ccd2ec6a L |
6418 | |
6419 | /* Set things up for objcopy and relocatable link. The output | |
6420 | SHT_GROUP section will have its elf_next_in_group pointing back | |
6421 | to the input group members. Ignore linker created group section. | |
6422 | See elfNN_ia64_object_p in elfxx-ia64.c. */ | |
dfa7b0b8 | 6423 | if (!final_link) |
ccd2ec6a L |
6424 | { |
6425 | if (elf_sec_group (isec) == NULL | |
6426 | || (elf_sec_group (isec)->flags & SEC_LINKER_CREATED) == 0) | |
6427 | { | |
6428 | if (elf_section_flags (isec) & SHF_GROUP) | |
6429 | elf_section_flags (osec) |= SHF_GROUP; | |
6430 | elf_next_in_group (osec) = elf_next_in_group (isec); | |
9659de1c | 6431 | elf_section_data (osec)->group = elf_section_data (isec)->group; |
ccd2ec6a L |
6432 | } |
6433 | } | |
6434 | ||
6435 | ihdr = &elf_section_data (isec)->this_hdr; | |
6436 | ||
6437 | /* We need to handle elf_linked_to_section for SHF_LINK_ORDER. We | |
6438 | don't use the output section of the linked-to section since it | |
6439 | may be NULL at this point. */ | |
6440 | if ((ihdr->sh_flags & SHF_LINK_ORDER) != 0) | |
6441 | { | |
6442 | ohdr = &elf_section_data (osec)->this_hdr; | |
6443 | ohdr->sh_flags |= SHF_LINK_ORDER; | |
6444 | elf_linked_to_section (osec) = elf_linked_to_section (isec); | |
6445 | } | |
6446 | ||
6447 | osec->use_rela_p = isec->use_rela_p; | |
6448 | ||
6449 | return TRUE; | |
6450 | } | |
6451 | ||
252b5132 RH |
6452 | /* Copy private section information. This copies over the entsize |
6453 | field, and sometimes the info field. */ | |
6454 | ||
b34976b6 | 6455 | bfd_boolean |
217aa764 AM |
6456 | _bfd_elf_copy_private_section_data (bfd *ibfd, |
6457 | asection *isec, | |
6458 | bfd *obfd, | |
6459 | asection *osec) | |
252b5132 RH |
6460 | { |
6461 | Elf_Internal_Shdr *ihdr, *ohdr; | |
6462 | ||
6463 | if (ibfd->xvec->flavour != bfd_target_elf_flavour | |
6464 | || obfd->xvec->flavour != bfd_target_elf_flavour) | |
b34976b6 | 6465 | return TRUE; |
252b5132 | 6466 | |
252b5132 RH |
6467 | ihdr = &elf_section_data (isec)->this_hdr; |
6468 | ohdr = &elf_section_data (osec)->this_hdr; | |
6469 | ||
6470 | ohdr->sh_entsize = ihdr->sh_entsize; | |
6471 | ||
6472 | if (ihdr->sh_type == SHT_SYMTAB | |
6473 | || ihdr->sh_type == SHT_DYNSYM | |
6474 | || ihdr->sh_type == SHT_GNU_verneed | |
6475 | || ihdr->sh_type == SHT_GNU_verdef) | |
6476 | ohdr->sh_info = ihdr->sh_info; | |
6477 | ||
ccd2ec6a L |
6478 | return _bfd_elf_init_private_section_data (ibfd, isec, obfd, osec, |
6479 | NULL); | |
252b5132 RH |
6480 | } |
6481 | ||
d0bf826b AM |
6482 | /* Look at all the SHT_GROUP sections in IBFD, making any adjustments |
6483 | necessary if we are removing either the SHT_GROUP section or any of | |
6484 | the group member sections. DISCARDED is the value that a section's | |
6485 | output_section has if the section will be discarded, NULL when this | |
6486 | function is called from objcopy, bfd_abs_section_ptr when called | |
6487 | from the linker. */ | |
80fccad2 BW |
6488 | |
6489 | bfd_boolean | |
d0bf826b | 6490 | _bfd_elf_fixup_group_sections (bfd *ibfd, asection *discarded) |
80fccad2 | 6491 | { |
30288845 AM |
6492 | asection *isec; |
6493 | ||
30288845 | 6494 | for (isec = ibfd->sections; isec != NULL; isec = isec->next) |
415f38a6 | 6495 | if (elf_section_type (isec) == SHT_GROUP) |
30288845 AM |
6496 | { |
6497 | asection *first = elf_next_in_group (isec); | |
6498 | asection *s = first; | |
d0bf826b AM |
6499 | bfd_size_type removed = 0; |
6500 | ||
30288845 AM |
6501 | while (s != NULL) |
6502 | { | |
415f38a6 AM |
6503 | /* If this member section is being output but the |
6504 | SHT_GROUP section is not, then clear the group info | |
6505 | set up by _bfd_elf_copy_private_section_data. */ | |
d0bf826b AM |
6506 | if (s->output_section != discarded |
6507 | && isec->output_section == discarded) | |
30288845 AM |
6508 | { |
6509 | elf_section_flags (s->output_section) &= ~SHF_GROUP; | |
6510 | elf_group_name (s->output_section) = NULL; | |
6511 | } | |
415f38a6 AM |
6512 | /* Conversely, if the member section is not being output |
6513 | but the SHT_GROUP section is, then adjust its size. */ | |
d0bf826b AM |
6514 | else if (s->output_section == discarded |
6515 | && isec->output_section != discarded) | |
6516 | removed += 4; | |
30288845 AM |
6517 | s = elf_next_in_group (s); |
6518 | if (s == first) | |
6519 | break; | |
6520 | } | |
d0bf826b AM |
6521 | if (removed != 0) |
6522 | { | |
6523 | if (discarded != NULL) | |
6524 | { | |
6525 | /* If we've been called for ld -r, then we need to | |
6526 | adjust the input section size. This function may | |
6527 | be called multiple times, so save the original | |
6528 | size. */ | |
6529 | if (isec->rawsize == 0) | |
6530 | isec->rawsize = isec->size; | |
6531 | isec->size = isec->rawsize - removed; | |
6532 | } | |
6533 | else | |
6534 | { | |
6535 | /* Adjust the output section size when called from | |
6536 | objcopy. */ | |
6537 | isec->output_section->size -= removed; | |
6538 | } | |
6539 | } | |
30288845 AM |
6540 | } |
6541 | ||
80fccad2 BW |
6542 | return TRUE; |
6543 | } | |
6544 | ||
d0bf826b AM |
6545 | /* Copy private header information. */ |
6546 | ||
6547 | bfd_boolean | |
6548 | _bfd_elf_copy_private_header_data (bfd *ibfd, bfd *obfd) | |
6549 | { | |
6550 | if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour | |
6551 | || bfd_get_flavour (obfd) != bfd_target_elf_flavour) | |
6552 | return TRUE; | |
6553 | ||
6554 | /* Copy over private BFD data if it has not already been copied. | |
6555 | This must be done here, rather than in the copy_private_bfd_data | |
6556 | entry point, because the latter is called after the section | |
6557 | contents have been set, which means that the program headers have | |
6558 | already been worked out. */ | |
6559 | if (elf_tdata (obfd)->segment_map == NULL && elf_tdata (ibfd)->phdr != NULL) | |
6560 | { | |
6561 | if (! copy_private_bfd_data (ibfd, obfd)) | |
6562 | return FALSE; | |
6563 | } | |
6564 | ||
6565 | return _bfd_elf_fixup_group_sections (ibfd, NULL); | |
6566 | } | |
6567 | ||
252b5132 RH |
6568 | /* Copy private symbol information. If this symbol is in a section |
6569 | which we did not map into a BFD section, try to map the section | |
6570 | index correctly. We use special macro definitions for the mapped | |
6571 | section indices; these definitions are interpreted by the | |
6572 | swap_out_syms function. */ | |
6573 | ||
9ad5cbcf AM |
6574 | #define MAP_ONESYMTAB (SHN_HIOS + 1) |
6575 | #define MAP_DYNSYMTAB (SHN_HIOS + 2) | |
6576 | #define MAP_STRTAB (SHN_HIOS + 3) | |
6577 | #define MAP_SHSTRTAB (SHN_HIOS + 4) | |
6578 | #define MAP_SYM_SHNDX (SHN_HIOS + 5) | |
252b5132 | 6579 | |
b34976b6 | 6580 | bfd_boolean |
217aa764 AM |
6581 | _bfd_elf_copy_private_symbol_data (bfd *ibfd, |
6582 | asymbol *isymarg, | |
6583 | bfd *obfd, | |
6584 | asymbol *osymarg) | |
252b5132 RH |
6585 | { |
6586 | elf_symbol_type *isym, *osym; | |
6587 | ||
6588 | if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour | |
6589 | || bfd_get_flavour (obfd) != bfd_target_elf_flavour) | |
b34976b6 | 6590 | return TRUE; |
252b5132 RH |
6591 | |
6592 | isym = elf_symbol_from (ibfd, isymarg); | |
6593 | osym = elf_symbol_from (obfd, osymarg); | |
6594 | ||
6595 | if (isym != NULL | |
8424d8f5 | 6596 | && isym->internal_elf_sym.st_shndx != 0 |
252b5132 RH |
6597 | && osym != NULL |
6598 | && bfd_is_abs_section (isym->symbol.section)) | |
6599 | { | |
6600 | unsigned int shndx; | |
6601 | ||
6602 | shndx = isym->internal_elf_sym.st_shndx; | |
6603 | if (shndx == elf_onesymtab (ibfd)) | |
6604 | shndx = MAP_ONESYMTAB; | |
6605 | else if (shndx == elf_dynsymtab (ibfd)) | |
6606 | shndx = MAP_DYNSYMTAB; | |
6607 | else if (shndx == elf_tdata (ibfd)->strtab_section) | |
6608 | shndx = MAP_STRTAB; | |
6609 | else if (shndx == elf_tdata (ibfd)->shstrtab_section) | |
6610 | shndx = MAP_SHSTRTAB; | |
9ad5cbcf AM |
6611 | else if (shndx == elf_tdata (ibfd)->symtab_shndx_section) |
6612 | shndx = MAP_SYM_SHNDX; | |
252b5132 RH |
6613 | osym->internal_elf_sym.st_shndx = shndx; |
6614 | } | |
6615 | ||
b34976b6 | 6616 | return TRUE; |
252b5132 RH |
6617 | } |
6618 | ||
6619 | /* Swap out the symbols. */ | |
6620 | ||
b34976b6 | 6621 | static bfd_boolean |
217aa764 AM |
6622 | swap_out_syms (bfd *abfd, |
6623 | struct bfd_strtab_hash **sttp, | |
6624 | int relocatable_p) | |
252b5132 | 6625 | { |
9c5bfbb7 | 6626 | const struct elf_backend_data *bed; |
079e9a2f AM |
6627 | int symcount; |
6628 | asymbol **syms; | |
6629 | struct bfd_strtab_hash *stt; | |
6630 | Elf_Internal_Shdr *symtab_hdr; | |
9ad5cbcf | 6631 | Elf_Internal_Shdr *symtab_shndx_hdr; |
079e9a2f | 6632 | Elf_Internal_Shdr *symstrtab_hdr; |
f075ee0c AM |
6633 | bfd_byte *outbound_syms; |
6634 | bfd_byte *outbound_shndx; | |
079e9a2f AM |
6635 | int idx; |
6636 | bfd_size_type amt; | |
174fd7f9 | 6637 | bfd_boolean name_local_sections; |
252b5132 RH |
6638 | |
6639 | if (!elf_map_symbols (abfd)) | |
b34976b6 | 6640 | return FALSE; |
252b5132 | 6641 | |
c044fabd | 6642 | /* Dump out the symtabs. */ |
079e9a2f AM |
6643 | stt = _bfd_elf_stringtab_init (); |
6644 | if (stt == NULL) | |
b34976b6 | 6645 | return FALSE; |
252b5132 | 6646 | |
079e9a2f AM |
6647 | bed = get_elf_backend_data (abfd); |
6648 | symcount = bfd_get_symcount (abfd); | |
6649 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; | |
6650 | symtab_hdr->sh_type = SHT_SYMTAB; | |
6651 | symtab_hdr->sh_entsize = bed->s->sizeof_sym; | |
6652 | symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1); | |
6653 | symtab_hdr->sh_info = elf_num_locals (abfd) + 1; | |
72de5009 | 6654 | symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align; |
079e9a2f AM |
6655 | |
6656 | symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr; | |
6657 | symstrtab_hdr->sh_type = SHT_STRTAB; | |
6658 | ||
a50b1753 NC |
6659 | outbound_syms = (bfd_byte *) bfd_alloc2 (abfd, 1 + symcount, |
6660 | bed->s->sizeof_sym); | |
079e9a2f | 6661 | if (outbound_syms == NULL) |
5ed6aba4 NC |
6662 | { |
6663 | _bfd_stringtab_free (stt); | |
6664 | return FALSE; | |
6665 | } | |
217aa764 | 6666 | symtab_hdr->contents = outbound_syms; |
252b5132 | 6667 | |
9ad5cbcf AM |
6668 | outbound_shndx = NULL; |
6669 | symtab_shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr; | |
6670 | if (symtab_shndx_hdr->sh_name != 0) | |
6671 | { | |
6672 | amt = (bfd_size_type) (1 + symcount) * sizeof (Elf_External_Sym_Shndx); | |
a50b1753 NC |
6673 | outbound_shndx = (bfd_byte *) |
6674 | bfd_zalloc2 (abfd, 1 + symcount, sizeof (Elf_External_Sym_Shndx)); | |
9ad5cbcf | 6675 | if (outbound_shndx == NULL) |
5ed6aba4 NC |
6676 | { |
6677 | _bfd_stringtab_free (stt); | |
6678 | return FALSE; | |
6679 | } | |
6680 | ||
9ad5cbcf AM |
6681 | symtab_shndx_hdr->contents = outbound_shndx; |
6682 | symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX; | |
6683 | symtab_shndx_hdr->sh_size = amt; | |
6684 | symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx); | |
6685 | symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx); | |
6686 | } | |
6687 | ||
589e6347 | 6688 | /* Now generate the data (for "contents"). */ |
079e9a2f AM |
6689 | { |
6690 | /* Fill in zeroth symbol and swap it out. */ | |
6691 | Elf_Internal_Sym sym; | |
6692 | sym.st_name = 0; | |
6693 | sym.st_value = 0; | |
6694 | sym.st_size = 0; | |
6695 | sym.st_info = 0; | |
6696 | sym.st_other = 0; | |
6697 | sym.st_shndx = SHN_UNDEF; | |
35fc36a8 | 6698 | sym.st_target_internal = 0; |
9ad5cbcf | 6699 | bed->s->swap_symbol_out (abfd, &sym, outbound_syms, outbound_shndx); |
079e9a2f | 6700 | outbound_syms += bed->s->sizeof_sym; |
9ad5cbcf AM |
6701 | if (outbound_shndx != NULL) |
6702 | outbound_shndx += sizeof (Elf_External_Sym_Shndx); | |
079e9a2f | 6703 | } |
252b5132 | 6704 | |
174fd7f9 RS |
6705 | name_local_sections |
6706 | = (bed->elf_backend_name_local_section_symbols | |
6707 | && bed->elf_backend_name_local_section_symbols (abfd)); | |
6708 | ||
079e9a2f AM |
6709 | syms = bfd_get_outsymbols (abfd); |
6710 | for (idx = 0; idx < symcount; idx++) | |
252b5132 | 6711 | { |
252b5132 | 6712 | Elf_Internal_Sym sym; |
079e9a2f AM |
6713 | bfd_vma value = syms[idx]->value; |
6714 | elf_symbol_type *type_ptr; | |
6715 | flagword flags = syms[idx]->flags; | |
6716 | int type; | |
252b5132 | 6717 | |
174fd7f9 RS |
6718 | if (!name_local_sections |
6719 | && (flags & (BSF_SECTION_SYM | BSF_GLOBAL)) == BSF_SECTION_SYM) | |
079e9a2f AM |
6720 | { |
6721 | /* Local section symbols have no name. */ | |
6722 | sym.st_name = 0; | |
6723 | } | |
6724 | else | |
6725 | { | |
6726 | sym.st_name = (unsigned long) _bfd_stringtab_add (stt, | |
6727 | syms[idx]->name, | |
b34976b6 | 6728 | TRUE, FALSE); |
079e9a2f | 6729 | if (sym.st_name == (unsigned long) -1) |
5ed6aba4 NC |
6730 | { |
6731 | _bfd_stringtab_free (stt); | |
6732 | return FALSE; | |
6733 | } | |
079e9a2f | 6734 | } |
252b5132 | 6735 | |
079e9a2f | 6736 | type_ptr = elf_symbol_from (abfd, syms[idx]); |
252b5132 | 6737 | |
079e9a2f AM |
6738 | if ((flags & BSF_SECTION_SYM) == 0 |
6739 | && bfd_is_com_section (syms[idx]->section)) | |
6740 | { | |
6741 | /* ELF common symbols put the alignment into the `value' field, | |
6742 | and the size into the `size' field. This is backwards from | |
6743 | how BFD handles it, so reverse it here. */ | |
6744 | sym.st_size = value; | |
6745 | if (type_ptr == NULL | |
6746 | || type_ptr->internal_elf_sym.st_value == 0) | |
6747 | sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value)); | |
6748 | else | |
6749 | sym.st_value = type_ptr->internal_elf_sym.st_value; | |
6750 | sym.st_shndx = _bfd_elf_section_from_bfd_section | |
6751 | (abfd, syms[idx]->section); | |
6752 | } | |
6753 | else | |
6754 | { | |
6755 | asection *sec = syms[idx]->section; | |
cb33740c | 6756 | unsigned int shndx; |
252b5132 | 6757 | |
079e9a2f AM |
6758 | if (sec->output_section) |
6759 | { | |
6760 | value += sec->output_offset; | |
6761 | sec = sec->output_section; | |
6762 | } | |
589e6347 | 6763 | |
079e9a2f AM |
6764 | /* Don't add in the section vma for relocatable output. */ |
6765 | if (! relocatable_p) | |
6766 | value += sec->vma; | |
6767 | sym.st_value = value; | |
6768 | sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0; | |
6769 | ||
6770 | if (bfd_is_abs_section (sec) | |
6771 | && type_ptr != NULL | |
6772 | && type_ptr->internal_elf_sym.st_shndx != 0) | |
6773 | { | |
6774 | /* This symbol is in a real ELF section which we did | |
6775 | not create as a BFD section. Undo the mapping done | |
6776 | by copy_private_symbol_data. */ | |
6777 | shndx = type_ptr->internal_elf_sym.st_shndx; | |
6778 | switch (shndx) | |
6779 | { | |
6780 | case MAP_ONESYMTAB: | |
6781 | shndx = elf_onesymtab (abfd); | |
6782 | break; | |
6783 | case MAP_DYNSYMTAB: | |
6784 | shndx = elf_dynsymtab (abfd); | |
6785 | break; | |
6786 | case MAP_STRTAB: | |
6787 | shndx = elf_tdata (abfd)->strtab_section; | |
6788 | break; | |
6789 | case MAP_SHSTRTAB: | |
6790 | shndx = elf_tdata (abfd)->shstrtab_section; | |
6791 | break; | |
9ad5cbcf AM |
6792 | case MAP_SYM_SHNDX: |
6793 | shndx = elf_tdata (abfd)->symtab_shndx_section; | |
6794 | break; | |
079e9a2f | 6795 | default: |
15bc576a | 6796 | shndx = SHN_ABS; |
079e9a2f AM |
6797 | break; |
6798 | } | |
6799 | } | |
6800 | else | |
6801 | { | |
6802 | shndx = _bfd_elf_section_from_bfd_section (abfd, sec); | |
252b5132 | 6803 | |
cb33740c | 6804 | if (shndx == SHN_BAD) |
079e9a2f AM |
6805 | { |
6806 | asection *sec2; | |
6807 | ||
6808 | /* Writing this would be a hell of a lot easier if | |
6809 | we had some decent documentation on bfd, and | |
6810 | knew what to expect of the library, and what to | |
6811 | demand of applications. For example, it | |
6812 | appears that `objcopy' might not set the | |
6813 | section of a symbol to be a section that is | |
6814 | actually in the output file. */ | |
6815 | sec2 = bfd_get_section_by_name (abfd, sec->name); | |
589e6347 NC |
6816 | if (sec2 == NULL) |
6817 | { | |
6818 | _bfd_error_handler (_("\ | |
6819 | Unable to find equivalent output section for symbol '%s' from section '%s'"), | |
6820 | syms[idx]->name ? syms[idx]->name : "<Local sym>", | |
6821 | sec->name); | |
811072d8 | 6822 | bfd_set_error (bfd_error_invalid_operation); |
5ed6aba4 | 6823 | _bfd_stringtab_free (stt); |
589e6347 NC |
6824 | return FALSE; |
6825 | } | |
811072d8 | 6826 | |
079e9a2f | 6827 | shndx = _bfd_elf_section_from_bfd_section (abfd, sec2); |
cb33740c | 6828 | BFD_ASSERT (shndx != SHN_BAD); |
079e9a2f AM |
6829 | } |
6830 | } | |
252b5132 | 6831 | |
079e9a2f AM |
6832 | sym.st_shndx = shndx; |
6833 | } | |
252b5132 | 6834 | |
13ae64f3 JJ |
6835 | if ((flags & BSF_THREAD_LOCAL) != 0) |
6836 | type = STT_TLS; | |
d8045f23 NC |
6837 | else if ((flags & BSF_GNU_INDIRECT_FUNCTION) != 0) |
6838 | type = STT_GNU_IFUNC; | |
13ae64f3 | 6839 | else if ((flags & BSF_FUNCTION) != 0) |
079e9a2f AM |
6840 | type = STT_FUNC; |
6841 | else if ((flags & BSF_OBJECT) != 0) | |
6842 | type = STT_OBJECT; | |
d9352518 DB |
6843 | else if ((flags & BSF_RELC) != 0) |
6844 | type = STT_RELC; | |
6845 | else if ((flags & BSF_SRELC) != 0) | |
6846 | type = STT_SRELC; | |
079e9a2f AM |
6847 | else |
6848 | type = STT_NOTYPE; | |
252b5132 | 6849 | |
13ae64f3 JJ |
6850 | if (syms[idx]->section->flags & SEC_THREAD_LOCAL) |
6851 | type = STT_TLS; | |
6852 | ||
589e6347 | 6853 | /* Processor-specific types. */ |
079e9a2f AM |
6854 | if (type_ptr != NULL |
6855 | && bed->elf_backend_get_symbol_type) | |
6856 | type = ((*bed->elf_backend_get_symbol_type) | |
6857 | (&type_ptr->internal_elf_sym, type)); | |
252b5132 | 6858 | |
079e9a2f AM |
6859 | if (flags & BSF_SECTION_SYM) |
6860 | { | |
6861 | if (flags & BSF_GLOBAL) | |
6862 | sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION); | |
6863 | else | |
6864 | sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION); | |
6865 | } | |
6866 | else if (bfd_is_com_section (syms[idx]->section)) | |
0a40daed | 6867 | { |
504b7d20 | 6868 | #ifdef USE_STT_COMMON |
0a40daed MK |
6869 | if (type == STT_OBJECT) |
6870 | sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_COMMON); | |
6871 | else | |
504b7d20 | 6872 | #endif |
c91e322a | 6873 | sym.st_info = ELF_ST_INFO (STB_GLOBAL, type); |
0a40daed | 6874 | } |
079e9a2f AM |
6875 | else if (bfd_is_und_section (syms[idx]->section)) |
6876 | sym.st_info = ELF_ST_INFO (((flags & BSF_WEAK) | |
6877 | ? STB_WEAK | |
6878 | : STB_GLOBAL), | |
6879 | type); | |
6880 | else if (flags & BSF_FILE) | |
6881 | sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE); | |
6882 | else | |
6883 | { | |
6884 | int bind = STB_LOCAL; | |
252b5132 | 6885 | |
079e9a2f AM |
6886 | if (flags & BSF_LOCAL) |
6887 | bind = STB_LOCAL; | |
3e7a7d11 NC |
6888 | else if (flags & BSF_GNU_UNIQUE) |
6889 | bind = STB_GNU_UNIQUE; | |
079e9a2f AM |
6890 | else if (flags & BSF_WEAK) |
6891 | bind = STB_WEAK; | |
6892 | else if (flags & BSF_GLOBAL) | |
6893 | bind = STB_GLOBAL; | |
252b5132 | 6894 | |
079e9a2f AM |
6895 | sym.st_info = ELF_ST_INFO (bind, type); |
6896 | } | |
252b5132 | 6897 | |
079e9a2f | 6898 | if (type_ptr != NULL) |
35fc36a8 RS |
6899 | { |
6900 | sym.st_other = type_ptr->internal_elf_sym.st_other; | |
6901 | sym.st_target_internal | |
6902 | = type_ptr->internal_elf_sym.st_target_internal; | |
6903 | } | |
079e9a2f | 6904 | else |
35fc36a8 RS |
6905 | { |
6906 | sym.st_other = 0; | |
6907 | sym.st_target_internal = 0; | |
6908 | } | |
252b5132 | 6909 | |
9ad5cbcf | 6910 | bed->s->swap_symbol_out (abfd, &sym, outbound_syms, outbound_shndx); |
079e9a2f | 6911 | outbound_syms += bed->s->sizeof_sym; |
9ad5cbcf AM |
6912 | if (outbound_shndx != NULL) |
6913 | outbound_shndx += sizeof (Elf_External_Sym_Shndx); | |
079e9a2f | 6914 | } |
252b5132 | 6915 | |
079e9a2f AM |
6916 | *sttp = stt; |
6917 | symstrtab_hdr->sh_size = _bfd_stringtab_size (stt); | |
6918 | symstrtab_hdr->sh_type = SHT_STRTAB; | |
252b5132 | 6919 | |
079e9a2f AM |
6920 | symstrtab_hdr->sh_flags = 0; |
6921 | symstrtab_hdr->sh_addr = 0; | |
6922 | symstrtab_hdr->sh_entsize = 0; | |
6923 | symstrtab_hdr->sh_link = 0; | |
6924 | symstrtab_hdr->sh_info = 0; | |
6925 | symstrtab_hdr->sh_addralign = 1; | |
252b5132 | 6926 | |
b34976b6 | 6927 | return TRUE; |
252b5132 RH |
6928 | } |
6929 | ||
6930 | /* Return the number of bytes required to hold the symtab vector. | |
6931 | ||
6932 | Note that we base it on the count plus 1, since we will null terminate | |
6933 | the vector allocated based on this size. However, the ELF symbol table | |
6934 | always has a dummy entry as symbol #0, so it ends up even. */ | |
6935 | ||
6936 | long | |
217aa764 | 6937 | _bfd_elf_get_symtab_upper_bound (bfd *abfd) |
252b5132 RH |
6938 | { |
6939 | long symcount; | |
6940 | long symtab_size; | |
6941 | Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr; | |
6942 | ||
6943 | symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym; | |
b99d1833 AM |
6944 | symtab_size = (symcount + 1) * (sizeof (asymbol *)); |
6945 | if (symcount > 0) | |
6946 | symtab_size -= sizeof (asymbol *); | |
252b5132 RH |
6947 | |
6948 | return symtab_size; | |
6949 | } | |
6950 | ||
6951 | long | |
217aa764 | 6952 | _bfd_elf_get_dynamic_symtab_upper_bound (bfd *abfd) |
252b5132 RH |
6953 | { |
6954 | long symcount; | |
6955 | long symtab_size; | |
6956 | Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr; | |
6957 | ||
6958 | if (elf_dynsymtab (abfd) == 0) | |
6959 | { | |
6960 | bfd_set_error (bfd_error_invalid_operation); | |
6961 | return -1; | |
6962 | } | |
6963 | ||
6964 | symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym; | |
b99d1833 AM |
6965 | symtab_size = (symcount + 1) * (sizeof (asymbol *)); |
6966 | if (symcount > 0) | |
6967 | symtab_size -= sizeof (asymbol *); | |
252b5132 RH |
6968 | |
6969 | return symtab_size; | |
6970 | } | |
6971 | ||
6972 | long | |
217aa764 AM |
6973 | _bfd_elf_get_reloc_upper_bound (bfd *abfd ATTRIBUTE_UNUSED, |
6974 | sec_ptr asect) | |
252b5132 RH |
6975 | { |
6976 | return (asect->reloc_count + 1) * sizeof (arelent *); | |
6977 | } | |
6978 | ||
6979 | /* Canonicalize the relocs. */ | |
6980 | ||
6981 | long | |
217aa764 AM |
6982 | _bfd_elf_canonicalize_reloc (bfd *abfd, |
6983 | sec_ptr section, | |
6984 | arelent **relptr, | |
6985 | asymbol **symbols) | |
252b5132 RH |
6986 | { |
6987 | arelent *tblptr; | |
6988 | unsigned int i; | |
9c5bfbb7 | 6989 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
252b5132 | 6990 | |
b34976b6 | 6991 | if (! bed->s->slurp_reloc_table (abfd, section, symbols, FALSE)) |
252b5132 RH |
6992 | return -1; |
6993 | ||
6994 | tblptr = section->relocation; | |
6995 | for (i = 0; i < section->reloc_count; i++) | |
6996 | *relptr++ = tblptr++; | |
6997 | ||
6998 | *relptr = NULL; | |
6999 | ||
7000 | return section->reloc_count; | |
7001 | } | |
7002 | ||
7003 | long | |
6cee3f79 | 7004 | _bfd_elf_canonicalize_symtab (bfd *abfd, asymbol **allocation) |
252b5132 | 7005 | { |
9c5bfbb7 | 7006 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
217aa764 | 7007 | long symcount = bed->s->slurp_symbol_table (abfd, allocation, FALSE); |
252b5132 RH |
7008 | |
7009 | if (symcount >= 0) | |
7010 | bfd_get_symcount (abfd) = symcount; | |
7011 | return symcount; | |
7012 | } | |
7013 | ||
7014 | long | |
217aa764 AM |
7015 | _bfd_elf_canonicalize_dynamic_symtab (bfd *abfd, |
7016 | asymbol **allocation) | |
252b5132 | 7017 | { |
9c5bfbb7 | 7018 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
217aa764 | 7019 | long symcount = bed->s->slurp_symbol_table (abfd, allocation, TRUE); |
1f70368c DJ |
7020 | |
7021 | if (symcount >= 0) | |
7022 | bfd_get_dynamic_symcount (abfd) = symcount; | |
7023 | return symcount; | |
252b5132 RH |
7024 | } |
7025 | ||
8615f3f2 AM |
7026 | /* Return the size required for the dynamic reloc entries. Any loadable |
7027 | section that was actually installed in the BFD, and has type SHT_REL | |
7028 | or SHT_RELA, and uses the dynamic symbol table, is considered to be a | |
7029 | dynamic reloc section. */ | |
252b5132 RH |
7030 | |
7031 | long | |
217aa764 | 7032 | _bfd_elf_get_dynamic_reloc_upper_bound (bfd *abfd) |
252b5132 RH |
7033 | { |
7034 | long ret; | |
7035 | asection *s; | |
7036 | ||
7037 | if (elf_dynsymtab (abfd) == 0) | |
7038 | { | |
7039 | bfd_set_error (bfd_error_invalid_operation); | |
7040 | return -1; | |
7041 | } | |
7042 | ||
7043 | ret = sizeof (arelent *); | |
7044 | for (s = abfd->sections; s != NULL; s = s->next) | |
266b05cf | 7045 | if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd) |
252b5132 RH |
7046 | && (elf_section_data (s)->this_hdr.sh_type == SHT_REL |
7047 | || elf_section_data (s)->this_hdr.sh_type == SHT_RELA)) | |
eea6121a | 7048 | ret += ((s->size / elf_section_data (s)->this_hdr.sh_entsize) |
252b5132 RH |
7049 | * sizeof (arelent *)); |
7050 | ||
7051 | return ret; | |
7052 | } | |
7053 | ||
8615f3f2 AM |
7054 | /* Canonicalize the dynamic relocation entries. Note that we return the |
7055 | dynamic relocations as a single block, although they are actually | |
7056 | associated with particular sections; the interface, which was | |
7057 | designed for SunOS style shared libraries, expects that there is only | |
7058 | one set of dynamic relocs. Any loadable section that was actually | |
7059 | installed in the BFD, and has type SHT_REL or SHT_RELA, and uses the | |
7060 | dynamic symbol table, is considered to be a dynamic reloc section. */ | |
252b5132 RH |
7061 | |
7062 | long | |
217aa764 AM |
7063 | _bfd_elf_canonicalize_dynamic_reloc (bfd *abfd, |
7064 | arelent **storage, | |
7065 | asymbol **syms) | |
252b5132 | 7066 | { |
217aa764 | 7067 | bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean); |
252b5132 RH |
7068 | asection *s; |
7069 | long ret; | |
7070 | ||
7071 | if (elf_dynsymtab (abfd) == 0) | |
7072 | { | |
7073 | bfd_set_error (bfd_error_invalid_operation); | |
7074 | return -1; | |
7075 | } | |
7076 | ||
7077 | slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table; | |
7078 | ret = 0; | |
7079 | for (s = abfd->sections; s != NULL; s = s->next) | |
7080 | { | |
266b05cf | 7081 | if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd) |
252b5132 RH |
7082 | && (elf_section_data (s)->this_hdr.sh_type == SHT_REL |
7083 | || elf_section_data (s)->this_hdr.sh_type == SHT_RELA)) | |
7084 | { | |
7085 | arelent *p; | |
7086 | long count, i; | |
7087 | ||
b34976b6 | 7088 | if (! (*slurp_relocs) (abfd, s, syms, TRUE)) |
252b5132 | 7089 | return -1; |
eea6121a | 7090 | count = s->size / elf_section_data (s)->this_hdr.sh_entsize; |
252b5132 RH |
7091 | p = s->relocation; |
7092 | for (i = 0; i < count; i++) | |
7093 | *storage++ = p++; | |
7094 | ret += count; | |
7095 | } | |
7096 | } | |
7097 | ||
7098 | *storage = NULL; | |
7099 | ||
7100 | return ret; | |
7101 | } | |
7102 | \f | |
7103 | /* Read in the version information. */ | |
7104 | ||
b34976b6 | 7105 | bfd_boolean |
fc0e6df6 | 7106 | _bfd_elf_slurp_version_tables (bfd *abfd, bfd_boolean default_imported_symver) |
252b5132 RH |
7107 | { |
7108 | bfd_byte *contents = NULL; | |
fc0e6df6 PB |
7109 | unsigned int freeidx = 0; |
7110 | ||
7111 | if (elf_dynverref (abfd) != 0) | |
7112 | { | |
7113 | Elf_Internal_Shdr *hdr; | |
7114 | Elf_External_Verneed *everneed; | |
7115 | Elf_Internal_Verneed *iverneed; | |
7116 | unsigned int i; | |
d0fb9a8d | 7117 | bfd_byte *contents_end; |
fc0e6df6 PB |
7118 | |
7119 | hdr = &elf_tdata (abfd)->dynverref_hdr; | |
7120 | ||
a50b1753 NC |
7121 | elf_tdata (abfd)->verref = (Elf_Internal_Verneed *) |
7122 | bfd_zalloc2 (abfd, hdr->sh_info, sizeof (Elf_Internal_Verneed)); | |
fc0e6df6 PB |
7123 | if (elf_tdata (abfd)->verref == NULL) |
7124 | goto error_return; | |
7125 | ||
7126 | elf_tdata (abfd)->cverrefs = hdr->sh_info; | |
7127 | ||
a50b1753 | 7128 | contents = (bfd_byte *) bfd_malloc (hdr->sh_size); |
fc0e6df6 | 7129 | if (contents == NULL) |
d0fb9a8d JJ |
7130 | { |
7131 | error_return_verref: | |
7132 | elf_tdata (abfd)->verref = NULL; | |
7133 | elf_tdata (abfd)->cverrefs = 0; | |
7134 | goto error_return; | |
7135 | } | |
fc0e6df6 PB |
7136 | if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0 |
7137 | || bfd_bread (contents, hdr->sh_size, abfd) != hdr->sh_size) | |
d0fb9a8d | 7138 | goto error_return_verref; |
fc0e6df6 | 7139 | |
d0fb9a8d JJ |
7140 | if (hdr->sh_info && hdr->sh_size < sizeof (Elf_External_Verneed)) |
7141 | goto error_return_verref; | |
7142 | ||
7143 | BFD_ASSERT (sizeof (Elf_External_Verneed) | |
7144 | == sizeof (Elf_External_Vernaux)); | |
7145 | contents_end = contents + hdr->sh_size - sizeof (Elf_External_Verneed); | |
fc0e6df6 PB |
7146 | everneed = (Elf_External_Verneed *) contents; |
7147 | iverneed = elf_tdata (abfd)->verref; | |
7148 | for (i = 0; i < hdr->sh_info; i++, iverneed++) | |
7149 | { | |
7150 | Elf_External_Vernaux *evernaux; | |
7151 | Elf_Internal_Vernaux *ivernaux; | |
7152 | unsigned int j; | |
7153 | ||
7154 | _bfd_elf_swap_verneed_in (abfd, everneed, iverneed); | |
7155 | ||
7156 | iverneed->vn_bfd = abfd; | |
7157 | ||
7158 | iverneed->vn_filename = | |
7159 | bfd_elf_string_from_elf_section (abfd, hdr->sh_link, | |
7160 | iverneed->vn_file); | |
7161 | if (iverneed->vn_filename == NULL) | |
d0fb9a8d | 7162 | goto error_return_verref; |
fc0e6df6 | 7163 | |
d0fb9a8d JJ |
7164 | if (iverneed->vn_cnt == 0) |
7165 | iverneed->vn_auxptr = NULL; | |
7166 | else | |
7167 | { | |
a50b1753 NC |
7168 | iverneed->vn_auxptr = (struct elf_internal_vernaux *) |
7169 | bfd_alloc2 (abfd, iverneed->vn_cnt, | |
7170 | sizeof (Elf_Internal_Vernaux)); | |
d0fb9a8d JJ |
7171 | if (iverneed->vn_auxptr == NULL) |
7172 | goto error_return_verref; | |
7173 | } | |
7174 | ||
7175 | if (iverneed->vn_aux | |
7176 | > (size_t) (contents_end - (bfd_byte *) everneed)) | |
7177 | goto error_return_verref; | |
fc0e6df6 PB |
7178 | |
7179 | evernaux = ((Elf_External_Vernaux *) | |
7180 | ((bfd_byte *) everneed + iverneed->vn_aux)); | |
7181 | ivernaux = iverneed->vn_auxptr; | |
7182 | for (j = 0; j < iverneed->vn_cnt; j++, ivernaux++) | |
7183 | { | |
7184 | _bfd_elf_swap_vernaux_in (abfd, evernaux, ivernaux); | |
7185 | ||
7186 | ivernaux->vna_nodename = | |
7187 | bfd_elf_string_from_elf_section (abfd, hdr->sh_link, | |
7188 | ivernaux->vna_name); | |
7189 | if (ivernaux->vna_nodename == NULL) | |
d0fb9a8d | 7190 | goto error_return_verref; |
fc0e6df6 PB |
7191 | |
7192 | if (j + 1 < iverneed->vn_cnt) | |
7193 | ivernaux->vna_nextptr = ivernaux + 1; | |
7194 | else | |
7195 | ivernaux->vna_nextptr = NULL; | |
7196 | ||
d0fb9a8d JJ |
7197 | if (ivernaux->vna_next |
7198 | > (size_t) (contents_end - (bfd_byte *) evernaux)) | |
7199 | goto error_return_verref; | |
7200 | ||
fc0e6df6 PB |
7201 | evernaux = ((Elf_External_Vernaux *) |
7202 | ((bfd_byte *) evernaux + ivernaux->vna_next)); | |
7203 | ||
7204 | if (ivernaux->vna_other > freeidx) | |
7205 | freeidx = ivernaux->vna_other; | |
7206 | } | |
7207 | ||
7208 | if (i + 1 < hdr->sh_info) | |
7209 | iverneed->vn_nextref = iverneed + 1; | |
7210 | else | |
7211 | iverneed->vn_nextref = NULL; | |
7212 | ||
d0fb9a8d JJ |
7213 | if (iverneed->vn_next |
7214 | > (size_t) (contents_end - (bfd_byte *) everneed)) | |
7215 | goto error_return_verref; | |
7216 | ||
fc0e6df6 PB |
7217 | everneed = ((Elf_External_Verneed *) |
7218 | ((bfd_byte *) everneed + iverneed->vn_next)); | |
7219 | } | |
7220 | ||
7221 | free (contents); | |
7222 | contents = NULL; | |
7223 | } | |
252b5132 RH |
7224 | |
7225 | if (elf_dynverdef (abfd) != 0) | |
7226 | { | |
7227 | Elf_Internal_Shdr *hdr; | |
7228 | Elf_External_Verdef *everdef; | |
7229 | Elf_Internal_Verdef *iverdef; | |
f631889e UD |
7230 | Elf_Internal_Verdef *iverdefarr; |
7231 | Elf_Internal_Verdef iverdefmem; | |
252b5132 | 7232 | unsigned int i; |
062e2358 | 7233 | unsigned int maxidx; |
d0fb9a8d | 7234 | bfd_byte *contents_end_def, *contents_end_aux; |
252b5132 RH |
7235 | |
7236 | hdr = &elf_tdata (abfd)->dynverdef_hdr; | |
7237 | ||
a50b1753 | 7238 | contents = (bfd_byte *) bfd_malloc (hdr->sh_size); |
252b5132 RH |
7239 | if (contents == NULL) |
7240 | goto error_return; | |
7241 | if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0 | |
217aa764 | 7242 | || bfd_bread (contents, hdr->sh_size, abfd) != hdr->sh_size) |
252b5132 RH |
7243 | goto error_return; |
7244 | ||
d0fb9a8d JJ |
7245 | if (hdr->sh_info && hdr->sh_size < sizeof (Elf_External_Verdef)) |
7246 | goto error_return; | |
7247 | ||
7248 | BFD_ASSERT (sizeof (Elf_External_Verdef) | |
7249 | >= sizeof (Elf_External_Verdaux)); | |
7250 | contents_end_def = contents + hdr->sh_size | |
7251 | - sizeof (Elf_External_Verdef); | |
7252 | contents_end_aux = contents + hdr->sh_size | |
7253 | - sizeof (Elf_External_Verdaux); | |
7254 | ||
f631889e UD |
7255 | /* We know the number of entries in the section but not the maximum |
7256 | index. Therefore we have to run through all entries and find | |
7257 | the maximum. */ | |
252b5132 | 7258 | everdef = (Elf_External_Verdef *) contents; |
f631889e UD |
7259 | maxidx = 0; |
7260 | for (i = 0; i < hdr->sh_info; ++i) | |
7261 | { | |
7262 | _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem); | |
7263 | ||
062e2358 AM |
7264 | if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) > maxidx) |
7265 | maxidx = iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION); | |
f631889e | 7266 | |
d0fb9a8d JJ |
7267 | if (iverdefmem.vd_next |
7268 | > (size_t) (contents_end_def - (bfd_byte *) everdef)) | |
7269 | goto error_return; | |
7270 | ||
f631889e UD |
7271 | everdef = ((Elf_External_Verdef *) |
7272 | ((bfd_byte *) everdef + iverdefmem.vd_next)); | |
7273 | } | |
7274 | ||
fc0e6df6 PB |
7275 | if (default_imported_symver) |
7276 | { | |
7277 | if (freeidx > maxidx) | |
7278 | maxidx = ++freeidx; | |
7279 | else | |
7280 | freeidx = ++maxidx; | |
7281 | } | |
a50b1753 NC |
7282 | elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *) |
7283 | bfd_zalloc2 (abfd, maxidx, sizeof (Elf_Internal_Verdef)); | |
f631889e UD |
7284 | if (elf_tdata (abfd)->verdef == NULL) |
7285 | goto error_return; | |
7286 | ||
7287 | elf_tdata (abfd)->cverdefs = maxidx; | |
7288 | ||
7289 | everdef = (Elf_External_Verdef *) contents; | |
7290 | iverdefarr = elf_tdata (abfd)->verdef; | |
7291 | for (i = 0; i < hdr->sh_info; i++) | |
252b5132 RH |
7292 | { |
7293 | Elf_External_Verdaux *everdaux; | |
7294 | Elf_Internal_Verdaux *iverdaux; | |
7295 | unsigned int j; | |
7296 | ||
f631889e UD |
7297 | _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem); |
7298 | ||
d0fb9a8d JJ |
7299 | if ((iverdefmem.vd_ndx & VERSYM_VERSION) == 0) |
7300 | { | |
7301 | error_return_verdef: | |
7302 | elf_tdata (abfd)->verdef = NULL; | |
7303 | elf_tdata (abfd)->cverdefs = 0; | |
7304 | goto error_return; | |
7305 | } | |
7306 | ||
f631889e UD |
7307 | iverdef = &iverdefarr[(iverdefmem.vd_ndx & VERSYM_VERSION) - 1]; |
7308 | memcpy (iverdef, &iverdefmem, sizeof (Elf_Internal_Verdef)); | |
252b5132 RH |
7309 | |
7310 | iverdef->vd_bfd = abfd; | |
7311 | ||
d0fb9a8d JJ |
7312 | if (iverdef->vd_cnt == 0) |
7313 | iverdef->vd_auxptr = NULL; | |
7314 | else | |
7315 | { | |
a50b1753 NC |
7316 | iverdef->vd_auxptr = (struct elf_internal_verdaux *) |
7317 | bfd_alloc2 (abfd, iverdef->vd_cnt, | |
7318 | sizeof (Elf_Internal_Verdaux)); | |
d0fb9a8d JJ |
7319 | if (iverdef->vd_auxptr == NULL) |
7320 | goto error_return_verdef; | |
7321 | } | |
7322 | ||
7323 | if (iverdef->vd_aux | |
7324 | > (size_t) (contents_end_aux - (bfd_byte *) everdef)) | |
7325 | goto error_return_verdef; | |
252b5132 RH |
7326 | |
7327 | everdaux = ((Elf_External_Verdaux *) | |
7328 | ((bfd_byte *) everdef + iverdef->vd_aux)); | |
7329 | iverdaux = iverdef->vd_auxptr; | |
7330 | for (j = 0; j < iverdef->vd_cnt; j++, iverdaux++) | |
7331 | { | |
7332 | _bfd_elf_swap_verdaux_in (abfd, everdaux, iverdaux); | |
7333 | ||
7334 | iverdaux->vda_nodename = | |
7335 | bfd_elf_string_from_elf_section (abfd, hdr->sh_link, | |
7336 | iverdaux->vda_name); | |
7337 | if (iverdaux->vda_nodename == NULL) | |
d0fb9a8d | 7338 | goto error_return_verdef; |
252b5132 RH |
7339 | |
7340 | if (j + 1 < iverdef->vd_cnt) | |
7341 | iverdaux->vda_nextptr = iverdaux + 1; | |
7342 | else | |
7343 | iverdaux->vda_nextptr = NULL; | |
7344 | ||
d0fb9a8d JJ |
7345 | if (iverdaux->vda_next |
7346 | > (size_t) (contents_end_aux - (bfd_byte *) everdaux)) | |
7347 | goto error_return_verdef; | |
7348 | ||
252b5132 RH |
7349 | everdaux = ((Elf_External_Verdaux *) |
7350 | ((bfd_byte *) everdaux + iverdaux->vda_next)); | |
7351 | } | |
7352 | ||
d0fb9a8d JJ |
7353 | if (iverdef->vd_cnt) |
7354 | iverdef->vd_nodename = iverdef->vd_auxptr->vda_nodename; | |
252b5132 | 7355 | |
d0fb9a8d | 7356 | if ((size_t) (iverdef - iverdefarr) + 1 < maxidx) |
252b5132 RH |
7357 | iverdef->vd_nextdef = iverdef + 1; |
7358 | else | |
7359 | iverdef->vd_nextdef = NULL; | |
7360 | ||
7361 | everdef = ((Elf_External_Verdef *) | |
7362 | ((bfd_byte *) everdef + iverdef->vd_next)); | |
7363 | } | |
7364 | ||
7365 | free (contents); | |
7366 | contents = NULL; | |
7367 | } | |
fc0e6df6 | 7368 | else if (default_imported_symver) |
252b5132 | 7369 | { |
fc0e6df6 PB |
7370 | if (freeidx < 3) |
7371 | freeidx = 3; | |
7372 | else | |
7373 | freeidx++; | |
252b5132 | 7374 | |
a50b1753 NC |
7375 | elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *) |
7376 | bfd_zalloc2 (abfd, freeidx, sizeof (Elf_Internal_Verdef)); | |
fc0e6df6 | 7377 | if (elf_tdata (abfd)->verdef == NULL) |
252b5132 RH |
7378 | goto error_return; |
7379 | ||
fc0e6df6 PB |
7380 | elf_tdata (abfd)->cverdefs = freeidx; |
7381 | } | |
252b5132 | 7382 | |
fc0e6df6 PB |
7383 | /* Create a default version based on the soname. */ |
7384 | if (default_imported_symver) | |
7385 | { | |
7386 | Elf_Internal_Verdef *iverdef; | |
7387 | Elf_Internal_Verdaux *iverdaux; | |
252b5132 | 7388 | |
5bb3703f | 7389 | iverdef = &elf_tdata (abfd)->verdef[freeidx - 1]; |
252b5132 | 7390 | |
fc0e6df6 PB |
7391 | iverdef->vd_version = VER_DEF_CURRENT; |
7392 | iverdef->vd_flags = 0; | |
7393 | iverdef->vd_ndx = freeidx; | |
7394 | iverdef->vd_cnt = 1; | |
252b5132 | 7395 | |
fc0e6df6 | 7396 | iverdef->vd_bfd = abfd; |
252b5132 | 7397 | |
fc0e6df6 PB |
7398 | iverdef->vd_nodename = bfd_elf_get_dt_soname (abfd); |
7399 | if (iverdef->vd_nodename == NULL) | |
d0fb9a8d | 7400 | goto error_return_verdef; |
fc0e6df6 | 7401 | iverdef->vd_nextdef = NULL; |
a50b1753 NC |
7402 | iverdef->vd_auxptr = (struct elf_internal_verdaux *) |
7403 | bfd_alloc (abfd, sizeof (Elf_Internal_Verdaux)); | |
d0fb9a8d JJ |
7404 | if (iverdef->vd_auxptr == NULL) |
7405 | goto error_return_verdef; | |
252b5132 | 7406 | |
fc0e6df6 PB |
7407 | iverdaux = iverdef->vd_auxptr; |
7408 | iverdaux->vda_nodename = iverdef->vd_nodename; | |
7409 | iverdaux->vda_nextptr = NULL; | |
252b5132 RH |
7410 | } |
7411 | ||
b34976b6 | 7412 | return TRUE; |
252b5132 RH |
7413 | |
7414 | error_return: | |
5ed6aba4 | 7415 | if (contents != NULL) |
252b5132 | 7416 | free (contents); |
b34976b6 | 7417 | return FALSE; |
252b5132 RH |
7418 | } |
7419 | \f | |
7420 | asymbol * | |
217aa764 | 7421 | _bfd_elf_make_empty_symbol (bfd *abfd) |
252b5132 RH |
7422 | { |
7423 | elf_symbol_type *newsym; | |
dc810e39 | 7424 | bfd_size_type amt = sizeof (elf_symbol_type); |
252b5132 | 7425 | |
a50b1753 | 7426 | newsym = (elf_symbol_type *) bfd_zalloc (abfd, amt); |
252b5132 RH |
7427 | if (!newsym) |
7428 | return NULL; | |
7429 | else | |
7430 | { | |
7431 | newsym->symbol.the_bfd = abfd; | |
7432 | return &newsym->symbol; | |
7433 | } | |
7434 | } | |
7435 | ||
7436 | void | |
217aa764 AM |
7437 | _bfd_elf_get_symbol_info (bfd *abfd ATTRIBUTE_UNUSED, |
7438 | asymbol *symbol, | |
7439 | symbol_info *ret) | |
252b5132 RH |
7440 | { |
7441 | bfd_symbol_info (symbol, ret); | |
7442 | } | |
7443 | ||
7444 | /* Return whether a symbol name implies a local symbol. Most targets | |
7445 | use this function for the is_local_label_name entry point, but some | |
7446 | override it. */ | |
7447 | ||
b34976b6 | 7448 | bfd_boolean |
217aa764 AM |
7449 | _bfd_elf_is_local_label_name (bfd *abfd ATTRIBUTE_UNUSED, |
7450 | const char *name) | |
252b5132 RH |
7451 | { |
7452 | /* Normal local symbols start with ``.L''. */ | |
7453 | if (name[0] == '.' && name[1] == 'L') | |
b34976b6 | 7454 | return TRUE; |
252b5132 RH |
7455 | |
7456 | /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate | |
7457 | DWARF debugging symbols starting with ``..''. */ | |
7458 | if (name[0] == '.' && name[1] == '.') | |
b34976b6 | 7459 | return TRUE; |
252b5132 RH |
7460 | |
7461 | /* gcc will sometimes generate symbols beginning with ``_.L_'' when | |
7462 | emitting DWARF debugging output. I suspect this is actually a | |
7463 | small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call | |
7464 | ASM_GENERATE_INTERNAL_LABEL, and this causes the leading | |
7465 | underscore to be emitted on some ELF targets). For ease of use, | |
7466 | we treat such symbols as local. */ | |
7467 | if (name[0] == '_' && name[1] == '.' && name[2] == 'L' && name[3] == '_') | |
b34976b6 | 7468 | return TRUE; |
252b5132 | 7469 | |
b34976b6 | 7470 | return FALSE; |
252b5132 RH |
7471 | } |
7472 | ||
7473 | alent * | |
217aa764 AM |
7474 | _bfd_elf_get_lineno (bfd *abfd ATTRIBUTE_UNUSED, |
7475 | asymbol *symbol ATTRIBUTE_UNUSED) | |
252b5132 RH |
7476 | { |
7477 | abort (); | |
7478 | return NULL; | |
7479 | } | |
7480 | ||
b34976b6 | 7481 | bfd_boolean |
217aa764 AM |
7482 | _bfd_elf_set_arch_mach (bfd *abfd, |
7483 | enum bfd_architecture arch, | |
7484 | unsigned long machine) | |
252b5132 RH |
7485 | { |
7486 | /* If this isn't the right architecture for this backend, and this | |
7487 | isn't the generic backend, fail. */ | |
7488 | if (arch != get_elf_backend_data (abfd)->arch | |
7489 | && arch != bfd_arch_unknown | |
7490 | && get_elf_backend_data (abfd)->arch != bfd_arch_unknown) | |
b34976b6 | 7491 | return FALSE; |
252b5132 RH |
7492 | |
7493 | return bfd_default_set_arch_mach (abfd, arch, machine); | |
7494 | } | |
7495 | ||
d1fad7c6 NC |
7496 | /* Find the function to a particular section and offset, |
7497 | for error reporting. */ | |
252b5132 | 7498 | |
b34976b6 | 7499 | static bfd_boolean |
b9d678e0 | 7500 | elf_find_function (bfd *abfd, |
217aa764 AM |
7501 | asection *section, |
7502 | asymbol **symbols, | |
7503 | bfd_vma offset, | |
7504 | const char **filename_ptr, | |
7505 | const char **functionname_ptr) | |
252b5132 | 7506 | { |
aef36ac1 | 7507 | static asection *last_section; |
1965d5f2 | 7508 | static asymbol **last_symbols; |
aef36ac1 AM |
7509 | static asymbol *func; |
7510 | static const char *filename; | |
7511 | static bfd_size_type func_size; | |
252b5132 | 7512 | |
a06c7d5a NC |
7513 | if (symbols == NULL) |
7514 | return FALSE; | |
7515 | ||
aef36ac1 | 7516 | if (last_section != section |
1965d5f2 | 7517 | || last_symbols != symbols |
aef36ac1 AM |
7518 | || func == NULL |
7519 | || offset < func->value | |
7520 | || offset >= func->value + func_size) | |
7521 | { | |
7522 | asymbol *file; | |
7523 | bfd_vma low_func; | |
7524 | asymbol **p; | |
7525 | /* ??? Given multiple file symbols, it is impossible to reliably | |
7526 | choose the right file name for global symbols. File symbols are | |
7527 | local symbols, and thus all file symbols must sort before any | |
7528 | global symbols. The ELF spec may be interpreted to say that a | |
7529 | file symbol must sort before other local symbols, but currently | |
7530 | ld -r doesn't do this. So, for ld -r output, it is possible to | |
7531 | make a better choice of file name for local symbols by ignoring | |
7532 | file symbols appearing after a given local symbol. */ | |
7533 | enum { nothing_seen, symbol_seen, file_after_symbol_seen } state; | |
7534 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
7535 | ||
7536 | filename = NULL; | |
7537 | func = NULL; | |
7538 | file = NULL; | |
7539 | low_func = 0; | |
7540 | state = nothing_seen; | |
7541 | func_size = 0; | |
7542 | last_section = section; | |
1965d5f2 | 7543 | last_symbols = symbols; |
aef36ac1 AM |
7544 | |
7545 | for (p = symbols; *p != NULL; p++) | |
7546 | { | |
7547 | asymbol *sym = *p; | |
7548 | bfd_vma code_off; | |
7549 | bfd_size_type size; | |
7550 | ||
7551 | if ((sym->flags & BSF_FILE) != 0) | |
7552 | { | |
7553 | file = sym; | |
7554 | if (state == symbol_seen) | |
7555 | state = file_after_symbol_seen; | |
7556 | continue; | |
7557 | } | |
ff9e0f5b | 7558 | |
aef36ac1 AM |
7559 | size = bed->maybe_function_sym (sym, section, &code_off); |
7560 | if (size != 0 | |
7561 | && code_off <= offset | |
7562 | && (code_off > low_func | |
7563 | || (code_off == low_func | |
7564 | && size > func_size))) | |
7565 | { | |
7566 | func = sym; | |
7567 | func_size = size; | |
7568 | low_func = code_off; | |
7569 | filename = NULL; | |
7570 | if (file != NULL | |
7571 | && ((sym->flags & BSF_LOCAL) != 0 | |
7572 | || state != file_after_symbol_seen)) | |
7573 | filename = bfd_asymbol_name (file); | |
7574 | } | |
7575 | if (state == nothing_seen) | |
7576 | state = symbol_seen; | |
252b5132 RH |
7577 | } |
7578 | } | |
7579 | ||
7580 | if (func == NULL) | |
b34976b6 | 7581 | return FALSE; |
252b5132 | 7582 | |
d1fad7c6 NC |
7583 | if (filename_ptr) |
7584 | *filename_ptr = filename; | |
7585 | if (functionname_ptr) | |
7586 | *functionname_ptr = bfd_asymbol_name (func); | |
7587 | ||
b34976b6 | 7588 | return TRUE; |
d1fad7c6 NC |
7589 | } |
7590 | ||
7591 | /* Find the nearest line to a particular section and offset, | |
7592 | for error reporting. */ | |
7593 | ||
b34976b6 | 7594 | bfd_boolean |
217aa764 AM |
7595 | _bfd_elf_find_nearest_line (bfd *abfd, |
7596 | asection *section, | |
7597 | asymbol **symbols, | |
7598 | bfd_vma offset, | |
7599 | const char **filename_ptr, | |
7600 | const char **functionname_ptr, | |
7601 | unsigned int *line_ptr) | |
9b8d1a36 CC |
7602 | { |
7603 | return _bfd_elf_find_nearest_line_discriminator (abfd, section, symbols, | |
7604 | offset, filename_ptr, | |
7605 | functionname_ptr, | |
7606 | line_ptr, | |
7607 | NULL); | |
7608 | } | |
7609 | ||
7610 | bfd_boolean | |
7611 | _bfd_elf_find_nearest_line_discriminator (bfd *abfd, | |
7612 | asection *section, | |
7613 | asymbol **symbols, | |
7614 | bfd_vma offset, | |
7615 | const char **filename_ptr, | |
7616 | const char **functionname_ptr, | |
7617 | unsigned int *line_ptr, | |
7618 | unsigned int *discriminator_ptr) | |
d1fad7c6 | 7619 | { |
b34976b6 | 7620 | bfd_boolean found; |
d1fad7c6 NC |
7621 | |
7622 | if (_bfd_dwarf1_find_nearest_line (abfd, section, symbols, offset, | |
4e8a9624 AM |
7623 | filename_ptr, functionname_ptr, |
7624 | line_ptr)) | |
d1fad7c6 NC |
7625 | { |
7626 | if (!*functionname_ptr) | |
4e8a9624 AM |
7627 | elf_find_function (abfd, section, symbols, offset, |
7628 | *filename_ptr ? NULL : filename_ptr, | |
7629 | functionname_ptr); | |
7630 | ||
b34976b6 | 7631 | return TRUE; |
d1fad7c6 NC |
7632 | } |
7633 | ||
fc28f9aa TG |
7634 | if (_bfd_dwarf2_find_nearest_line (abfd, dwarf_debug_sections, |
7635 | section, symbols, offset, | |
4e8a9624 | 7636 | filename_ptr, functionname_ptr, |
9b8d1a36 | 7637 | line_ptr, discriminator_ptr, 0, |
4e8a9624 | 7638 | &elf_tdata (abfd)->dwarf2_find_line_info)) |
d1fad7c6 NC |
7639 | { |
7640 | if (!*functionname_ptr) | |
4e8a9624 AM |
7641 | elf_find_function (abfd, section, symbols, offset, |
7642 | *filename_ptr ? NULL : filename_ptr, | |
7643 | functionname_ptr); | |
7644 | ||
b34976b6 | 7645 | return TRUE; |
d1fad7c6 NC |
7646 | } |
7647 | ||
7648 | if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset, | |
4e8a9624 AM |
7649 | &found, filename_ptr, |
7650 | functionname_ptr, line_ptr, | |
7651 | &elf_tdata (abfd)->line_info)) | |
b34976b6 | 7652 | return FALSE; |
dc43ada5 | 7653 | if (found && (*functionname_ptr || *line_ptr)) |
b34976b6 | 7654 | return TRUE; |
d1fad7c6 NC |
7655 | |
7656 | if (symbols == NULL) | |
b34976b6 | 7657 | return FALSE; |
d1fad7c6 NC |
7658 | |
7659 | if (! elf_find_function (abfd, section, symbols, offset, | |
4e8a9624 | 7660 | filename_ptr, functionname_ptr)) |
b34976b6 | 7661 | return FALSE; |
d1fad7c6 | 7662 | |
252b5132 | 7663 | *line_ptr = 0; |
b34976b6 | 7664 | return TRUE; |
252b5132 RH |
7665 | } |
7666 | ||
5420f73d L |
7667 | /* Find the line for a symbol. */ |
7668 | ||
7669 | bfd_boolean | |
7670 | _bfd_elf_find_line (bfd *abfd, asymbol **symbols, asymbol *symbol, | |
7671 | const char **filename_ptr, unsigned int *line_ptr) | |
9b8d1a36 CC |
7672 | { |
7673 | return _bfd_elf_find_line_discriminator (abfd, symbols, symbol, | |
7674 | filename_ptr, line_ptr, | |
7675 | NULL); | |
7676 | } | |
7677 | ||
7678 | bfd_boolean | |
7679 | _bfd_elf_find_line_discriminator (bfd *abfd, asymbol **symbols, asymbol *symbol, | |
7680 | const char **filename_ptr, | |
7681 | unsigned int *line_ptr, | |
7682 | unsigned int *discriminator_ptr) | |
5420f73d L |
7683 | { |
7684 | return _bfd_dwarf2_find_line (abfd, symbols, symbol, | |
9b8d1a36 | 7685 | filename_ptr, line_ptr, discriminator_ptr, 0, |
5420f73d L |
7686 | &elf_tdata (abfd)->dwarf2_find_line_info); |
7687 | } | |
7688 | ||
4ab527b0 FF |
7689 | /* After a call to bfd_find_nearest_line, successive calls to |
7690 | bfd_find_inliner_info can be used to get source information about | |
7691 | each level of function inlining that terminated at the address | |
7692 | passed to bfd_find_nearest_line. Currently this is only supported | |
7693 | for DWARF2 with appropriate DWARF3 extensions. */ | |
7694 | ||
7695 | bfd_boolean | |
7696 | _bfd_elf_find_inliner_info (bfd *abfd, | |
7697 | const char **filename_ptr, | |
7698 | const char **functionname_ptr, | |
7699 | unsigned int *line_ptr) | |
7700 | { | |
7701 | bfd_boolean found; | |
7702 | found = _bfd_dwarf2_find_inliner_info (abfd, filename_ptr, | |
7703 | functionname_ptr, line_ptr, | |
7704 | & elf_tdata (abfd)->dwarf2_find_line_info); | |
7705 | return found; | |
7706 | } | |
7707 | ||
252b5132 | 7708 | int |
a6b96beb | 7709 | _bfd_elf_sizeof_headers (bfd *abfd, struct bfd_link_info *info) |
252b5132 | 7710 | { |
8ded5a0f AM |
7711 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
7712 | int ret = bed->s->sizeof_ehdr; | |
252b5132 | 7713 | |
a6b96beb | 7714 | if (!info->relocatable) |
8ded5a0f | 7715 | { |
62d7a5f6 | 7716 | bfd_size_type phdr_size = elf_tdata (abfd)->program_header_size; |
8ded5a0f | 7717 | |
62d7a5f6 AM |
7718 | if (phdr_size == (bfd_size_type) -1) |
7719 | { | |
7720 | struct elf_segment_map *m; | |
7721 | ||
7722 | phdr_size = 0; | |
7723 | for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next) | |
7724 | phdr_size += bed->s->sizeof_phdr; | |
8ded5a0f | 7725 | |
62d7a5f6 AM |
7726 | if (phdr_size == 0) |
7727 | phdr_size = get_program_header_size (abfd, info); | |
7728 | } | |
8ded5a0f AM |
7729 | |
7730 | elf_tdata (abfd)->program_header_size = phdr_size; | |
7731 | ret += phdr_size; | |
7732 | } | |
7733 | ||
252b5132 RH |
7734 | return ret; |
7735 | } | |
7736 | ||
b34976b6 | 7737 | bfd_boolean |
217aa764 AM |
7738 | _bfd_elf_set_section_contents (bfd *abfd, |
7739 | sec_ptr section, | |
0f867abe | 7740 | const void *location, |
217aa764 AM |
7741 | file_ptr offset, |
7742 | bfd_size_type count) | |
252b5132 RH |
7743 | { |
7744 | Elf_Internal_Shdr *hdr; | |
dc810e39 | 7745 | bfd_signed_vma pos; |
252b5132 RH |
7746 | |
7747 | if (! abfd->output_has_begun | |
217aa764 | 7748 | && ! _bfd_elf_compute_section_file_positions (abfd, NULL)) |
b34976b6 | 7749 | return FALSE; |
252b5132 RH |
7750 | |
7751 | hdr = &elf_section_data (section)->this_hdr; | |
dc810e39 AM |
7752 | pos = hdr->sh_offset + offset; |
7753 | if (bfd_seek (abfd, pos, SEEK_SET) != 0 | |
7754 | || bfd_bwrite (location, count, abfd) != count) | |
b34976b6 | 7755 | return FALSE; |
252b5132 | 7756 | |
b34976b6 | 7757 | return TRUE; |
252b5132 RH |
7758 | } |
7759 | ||
7760 | void | |
217aa764 AM |
7761 | _bfd_elf_no_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED, |
7762 | arelent *cache_ptr ATTRIBUTE_UNUSED, | |
7763 | Elf_Internal_Rela *dst ATTRIBUTE_UNUSED) | |
252b5132 RH |
7764 | { |
7765 | abort (); | |
7766 | } | |
7767 | ||
252b5132 RH |
7768 | /* Try to convert a non-ELF reloc into an ELF one. */ |
7769 | ||
b34976b6 | 7770 | bfd_boolean |
217aa764 | 7771 | _bfd_elf_validate_reloc (bfd *abfd, arelent *areloc) |
252b5132 | 7772 | { |
c044fabd | 7773 | /* Check whether we really have an ELF howto. */ |
252b5132 RH |
7774 | |
7775 | if ((*areloc->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec) | |
7776 | { | |
7777 | bfd_reloc_code_real_type code; | |
7778 | reloc_howto_type *howto; | |
7779 | ||
7780 | /* Alien reloc: Try to determine its type to replace it with an | |
c044fabd | 7781 | equivalent ELF reloc. */ |
252b5132 RH |
7782 | |
7783 | if (areloc->howto->pc_relative) | |
7784 | { | |
7785 | switch (areloc->howto->bitsize) | |
7786 | { | |
7787 | case 8: | |
7788 | code = BFD_RELOC_8_PCREL; | |
7789 | break; | |
7790 | case 12: | |
7791 | code = BFD_RELOC_12_PCREL; | |
7792 | break; | |
7793 | case 16: | |
7794 | code = BFD_RELOC_16_PCREL; | |
7795 | break; | |
7796 | case 24: | |
7797 | code = BFD_RELOC_24_PCREL; | |
7798 | break; | |
7799 | case 32: | |
7800 | code = BFD_RELOC_32_PCREL; | |
7801 | break; | |
7802 | case 64: | |
7803 | code = BFD_RELOC_64_PCREL; | |
7804 | break; | |
7805 | default: | |
7806 | goto fail; | |
7807 | } | |
7808 | ||
7809 | howto = bfd_reloc_type_lookup (abfd, code); | |
7810 | ||
7811 | if (areloc->howto->pcrel_offset != howto->pcrel_offset) | |
7812 | { | |
7813 | if (howto->pcrel_offset) | |
7814 | areloc->addend += areloc->address; | |
7815 | else | |
7816 | areloc->addend -= areloc->address; /* addend is unsigned!! */ | |
7817 | } | |
7818 | } | |
7819 | else | |
7820 | { | |
7821 | switch (areloc->howto->bitsize) | |
7822 | { | |
7823 | case 8: | |
7824 | code = BFD_RELOC_8; | |
7825 | break; | |
7826 | case 14: | |
7827 | code = BFD_RELOC_14; | |
7828 | break; | |
7829 | case 16: | |
7830 | code = BFD_RELOC_16; | |
7831 | break; | |
7832 | case 26: | |
7833 | code = BFD_RELOC_26; | |
7834 | break; | |
7835 | case 32: | |
7836 | code = BFD_RELOC_32; | |
7837 | break; | |
7838 | case 64: | |
7839 | code = BFD_RELOC_64; | |
7840 | break; | |
7841 | default: | |
7842 | goto fail; | |
7843 | } | |
7844 | ||
7845 | howto = bfd_reloc_type_lookup (abfd, code); | |
7846 | } | |
7847 | ||
7848 | if (howto) | |
7849 | areloc->howto = howto; | |
7850 | else | |
7851 | goto fail; | |
7852 | } | |
7853 | ||
b34976b6 | 7854 | return TRUE; |
252b5132 RH |
7855 | |
7856 | fail: | |
7857 | (*_bfd_error_handler) | |
d003868e AM |
7858 | (_("%B: unsupported relocation type %s"), |
7859 | abfd, areloc->howto->name); | |
252b5132 | 7860 | bfd_set_error (bfd_error_bad_value); |
b34976b6 | 7861 | return FALSE; |
252b5132 RH |
7862 | } |
7863 | ||
b34976b6 | 7864 | bfd_boolean |
217aa764 | 7865 | _bfd_elf_close_and_cleanup (bfd *abfd) |
252b5132 | 7866 | { |
d9071b0c TG |
7867 | struct elf_obj_tdata *tdata = elf_tdata (abfd); |
7868 | if (bfd_get_format (abfd) == bfd_object && tdata != NULL) | |
252b5132 | 7869 | { |
d9071b0c | 7870 | if (elf_shstrtab (abfd) != NULL) |
2b0f7ef9 | 7871 | _bfd_elf_strtab_free (elf_shstrtab (abfd)); |
d9071b0c | 7872 | _bfd_dwarf2_cleanup_debug_info (abfd, &tdata->dwarf2_find_line_info); |
252b5132 RH |
7873 | } |
7874 | ||
7875 | return _bfd_generic_close_and_cleanup (abfd); | |
7876 | } | |
7877 | ||
7878 | /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY | |
7879 | in the relocation's offset. Thus we cannot allow any sort of sanity | |
7880 | range-checking to interfere. There is nothing else to do in processing | |
7881 | this reloc. */ | |
7882 | ||
7883 | bfd_reloc_status_type | |
217aa764 AM |
7884 | _bfd_elf_rel_vtable_reloc_fn |
7885 | (bfd *abfd ATTRIBUTE_UNUSED, arelent *re ATTRIBUTE_UNUSED, | |
fc0a2244 | 7886 | struct bfd_symbol *symbol ATTRIBUTE_UNUSED, |
217aa764 AM |
7887 | void *data ATTRIBUTE_UNUSED, asection *is ATTRIBUTE_UNUSED, |
7888 | bfd *obfd ATTRIBUTE_UNUSED, char **errmsg ATTRIBUTE_UNUSED) | |
252b5132 RH |
7889 | { |
7890 | return bfd_reloc_ok; | |
7891 | } | |
252b5132 RH |
7892 | \f |
7893 | /* Elf core file support. Much of this only works on native | |
7894 | toolchains, since we rely on knowing the | |
7895 | machine-dependent procfs structure in order to pick | |
c044fabd | 7896 | out details about the corefile. */ |
252b5132 RH |
7897 | |
7898 | #ifdef HAVE_SYS_PROCFS_H | |
16231b7b DG |
7899 | /* Needed for new procfs interface on sparc-solaris. */ |
7900 | # define _STRUCTURED_PROC 1 | |
252b5132 RH |
7901 | # include <sys/procfs.h> |
7902 | #endif | |
7903 | ||
261b8d08 PA |
7904 | /* Return a PID that identifies a "thread" for threaded cores, or the |
7905 | PID of the main process for non-threaded cores. */ | |
252b5132 RH |
7906 | |
7907 | static int | |
217aa764 | 7908 | elfcore_make_pid (bfd *abfd) |
252b5132 | 7909 | { |
261b8d08 PA |
7910 | int pid; |
7911 | ||
7912 | pid = elf_tdata (abfd)->core_lwpid; | |
7913 | if (pid == 0) | |
7914 | pid = elf_tdata (abfd)->core_pid; | |
7915 | ||
7916 | return pid; | |
252b5132 RH |
7917 | } |
7918 | ||
252b5132 RH |
7919 | /* If there isn't a section called NAME, make one, using |
7920 | data from SECT. Note, this function will generate a | |
7921 | reference to NAME, so you shouldn't deallocate or | |
c044fabd | 7922 | overwrite it. */ |
252b5132 | 7923 | |
b34976b6 | 7924 | static bfd_boolean |
217aa764 | 7925 | elfcore_maybe_make_sect (bfd *abfd, char *name, asection *sect) |
252b5132 | 7926 | { |
c044fabd | 7927 | asection *sect2; |
252b5132 RH |
7928 | |
7929 | if (bfd_get_section_by_name (abfd, name) != NULL) | |
b34976b6 | 7930 | return TRUE; |
252b5132 | 7931 | |
117ed4f8 | 7932 | sect2 = bfd_make_section_with_flags (abfd, name, sect->flags); |
252b5132 | 7933 | if (sect2 == NULL) |
b34976b6 | 7934 | return FALSE; |
252b5132 | 7935 | |
eea6121a | 7936 | sect2->size = sect->size; |
252b5132 | 7937 | sect2->filepos = sect->filepos; |
252b5132 | 7938 | sect2->alignment_power = sect->alignment_power; |
b34976b6 | 7939 | return TRUE; |
252b5132 RH |
7940 | } |
7941 | ||
bb0082d6 AM |
7942 | /* Create a pseudosection containing SIZE bytes at FILEPOS. This |
7943 | actually creates up to two pseudosections: | |
7944 | - For the single-threaded case, a section named NAME, unless | |
7945 | such a section already exists. | |
7946 | - For the multi-threaded case, a section named "NAME/PID", where | |
7947 | PID is elfcore_make_pid (abfd). | |
7948 | Both pseudosections have identical contents. */ | |
b34976b6 | 7949 | bfd_boolean |
217aa764 AM |
7950 | _bfd_elfcore_make_pseudosection (bfd *abfd, |
7951 | char *name, | |
7952 | size_t size, | |
7953 | ufile_ptr filepos) | |
bb0082d6 AM |
7954 | { |
7955 | char buf[100]; | |
7956 | char *threaded_name; | |
d4c88bbb | 7957 | size_t len; |
bb0082d6 AM |
7958 | asection *sect; |
7959 | ||
7960 | /* Build the section name. */ | |
7961 | ||
7962 | sprintf (buf, "%s/%d", name, elfcore_make_pid (abfd)); | |
d4c88bbb | 7963 | len = strlen (buf) + 1; |
a50b1753 | 7964 | threaded_name = (char *) bfd_alloc (abfd, len); |
bb0082d6 | 7965 | if (threaded_name == NULL) |
b34976b6 | 7966 | return FALSE; |
d4c88bbb | 7967 | memcpy (threaded_name, buf, len); |
bb0082d6 | 7968 | |
117ed4f8 AM |
7969 | sect = bfd_make_section_anyway_with_flags (abfd, threaded_name, |
7970 | SEC_HAS_CONTENTS); | |
bb0082d6 | 7971 | if (sect == NULL) |
b34976b6 | 7972 | return FALSE; |
eea6121a | 7973 | sect->size = size; |
bb0082d6 | 7974 | sect->filepos = filepos; |
bb0082d6 AM |
7975 | sect->alignment_power = 2; |
7976 | ||
936e320b | 7977 | return elfcore_maybe_make_sect (abfd, name, sect); |
bb0082d6 AM |
7978 | } |
7979 | ||
252b5132 | 7980 | /* prstatus_t exists on: |
4a938328 | 7981 | solaris 2.5+ |
252b5132 RH |
7982 | linux 2.[01] + glibc |
7983 | unixware 4.2 | |
7984 | */ | |
7985 | ||
7986 | #if defined (HAVE_PRSTATUS_T) | |
a7b97311 | 7987 | |
b34976b6 | 7988 | static bfd_boolean |
217aa764 | 7989 | elfcore_grok_prstatus (bfd *abfd, Elf_Internal_Note *note) |
252b5132 | 7990 | { |
eea6121a | 7991 | size_t size; |
7ee38065 | 7992 | int offset; |
252b5132 | 7993 | |
4a938328 MS |
7994 | if (note->descsz == sizeof (prstatus_t)) |
7995 | { | |
7996 | prstatus_t prstat; | |
252b5132 | 7997 | |
eea6121a | 7998 | size = sizeof (prstat.pr_reg); |
7ee38065 | 7999 | offset = offsetof (prstatus_t, pr_reg); |
4a938328 | 8000 | memcpy (&prstat, note->descdata, sizeof (prstat)); |
252b5132 | 8001 | |
fa49d224 NC |
8002 | /* Do not overwrite the core signal if it |
8003 | has already been set by another thread. */ | |
8004 | if (elf_tdata (abfd)->core_signal == 0) | |
8005 | elf_tdata (abfd)->core_signal = prstat.pr_cursig; | |
261b8d08 PA |
8006 | if (elf_tdata (abfd)->core_pid == 0) |
8007 | elf_tdata (abfd)->core_pid = prstat.pr_pid; | |
252b5132 | 8008 | |
4a938328 MS |
8009 | /* pr_who exists on: |
8010 | solaris 2.5+ | |
8011 | unixware 4.2 | |
8012 | pr_who doesn't exist on: | |
8013 | linux 2.[01] | |
8014 | */ | |
252b5132 | 8015 | #if defined (HAVE_PRSTATUS_T_PR_WHO) |
4a938328 | 8016 | elf_tdata (abfd)->core_lwpid = prstat.pr_who; |
261b8d08 PA |
8017 | #else |
8018 | elf_tdata (abfd)->core_lwpid = prstat.pr_pid; | |
252b5132 | 8019 | #endif |
4a938328 | 8020 | } |
7ee38065 | 8021 | #if defined (HAVE_PRSTATUS32_T) |
4a938328 MS |
8022 | else if (note->descsz == sizeof (prstatus32_t)) |
8023 | { | |
8024 | /* 64-bit host, 32-bit corefile */ | |
8025 | prstatus32_t prstat; | |
8026 | ||
eea6121a | 8027 | size = sizeof (prstat.pr_reg); |
7ee38065 | 8028 | offset = offsetof (prstatus32_t, pr_reg); |
4a938328 MS |
8029 | memcpy (&prstat, note->descdata, sizeof (prstat)); |
8030 | ||
fa49d224 NC |
8031 | /* Do not overwrite the core signal if it |
8032 | has already been set by another thread. */ | |
8033 | if (elf_tdata (abfd)->core_signal == 0) | |
8034 | elf_tdata (abfd)->core_signal = prstat.pr_cursig; | |
261b8d08 PA |
8035 | if (elf_tdata (abfd)->core_pid == 0) |
8036 | elf_tdata (abfd)->core_pid = prstat.pr_pid; | |
4a938328 MS |
8037 | |
8038 | /* pr_who exists on: | |
8039 | solaris 2.5+ | |
8040 | unixware 4.2 | |
8041 | pr_who doesn't exist on: | |
8042 | linux 2.[01] | |
8043 | */ | |
7ee38065 | 8044 | #if defined (HAVE_PRSTATUS32_T_PR_WHO) |
4a938328 | 8045 | elf_tdata (abfd)->core_lwpid = prstat.pr_who; |
261b8d08 PA |
8046 | #else |
8047 | elf_tdata (abfd)->core_lwpid = prstat.pr_pid; | |
4a938328 MS |
8048 | #endif |
8049 | } | |
7ee38065 | 8050 | #endif /* HAVE_PRSTATUS32_T */ |
4a938328 MS |
8051 | else |
8052 | { | |
8053 | /* Fail - we don't know how to handle any other | |
8054 | note size (ie. data object type). */ | |
b34976b6 | 8055 | return TRUE; |
4a938328 | 8056 | } |
252b5132 | 8057 | |
bb0082d6 | 8058 | /* Make a ".reg/999" section and a ".reg" section. */ |
936e320b | 8059 | return _bfd_elfcore_make_pseudosection (abfd, ".reg", |
eea6121a | 8060 | size, note->descpos + offset); |
252b5132 RH |
8061 | } |
8062 | #endif /* defined (HAVE_PRSTATUS_T) */ | |
8063 | ||
bb0082d6 | 8064 | /* Create a pseudosection containing the exact contents of NOTE. */ |
b34976b6 | 8065 | static bfd_boolean |
217aa764 AM |
8066 | elfcore_make_note_pseudosection (bfd *abfd, |
8067 | char *name, | |
8068 | Elf_Internal_Note *note) | |
252b5132 | 8069 | { |
936e320b AM |
8070 | return _bfd_elfcore_make_pseudosection (abfd, name, |
8071 | note->descsz, note->descpos); | |
252b5132 RH |
8072 | } |
8073 | ||
ff08c6bb JB |
8074 | /* There isn't a consistent prfpregset_t across platforms, |
8075 | but it doesn't matter, because we don't have to pick this | |
c044fabd KH |
8076 | data structure apart. */ |
8077 | ||
b34976b6 | 8078 | static bfd_boolean |
217aa764 | 8079 | elfcore_grok_prfpreg (bfd *abfd, Elf_Internal_Note *note) |
ff08c6bb JB |
8080 | { |
8081 | return elfcore_make_note_pseudosection (abfd, ".reg2", note); | |
8082 | } | |
8083 | ||
ff08c6bb | 8084 | /* Linux dumps the Intel SSE regs in a note named "LINUX" with a note |
971d4640 | 8085 | type of NT_PRXFPREG. Just include the whole note's contents |
ff08c6bb | 8086 | literally. */ |
c044fabd | 8087 | |
b34976b6 | 8088 | static bfd_boolean |
217aa764 | 8089 | elfcore_grok_prxfpreg (bfd *abfd, Elf_Internal_Note *note) |
ff08c6bb JB |
8090 | { |
8091 | return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note); | |
8092 | } | |
8093 | ||
4339cae0 L |
8094 | /* Linux dumps the Intel XSAVE extended state in a note named "LINUX" |
8095 | with a note type of NT_X86_XSTATE. Just include the whole note's | |
8096 | contents literally. */ | |
8097 | ||
8098 | static bfd_boolean | |
8099 | elfcore_grok_xstatereg (bfd *abfd, Elf_Internal_Note *note) | |
8100 | { | |
8101 | return elfcore_make_note_pseudosection (abfd, ".reg-xstate", note); | |
8102 | } | |
8103 | ||
97753bd5 AM |
8104 | static bfd_boolean |
8105 | elfcore_grok_ppc_vmx (bfd *abfd, Elf_Internal_Note *note) | |
8106 | { | |
8107 | return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vmx", note); | |
8108 | } | |
8109 | ||
89eeb0bc LM |
8110 | static bfd_boolean |
8111 | elfcore_grok_ppc_vsx (bfd *abfd, Elf_Internal_Note *note) | |
8112 | { | |
8113 | return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vsx", note); | |
8114 | } | |
97753bd5 | 8115 | |
0675e188 UW |
8116 | static bfd_boolean |
8117 | elfcore_grok_s390_high_gprs (bfd *abfd, Elf_Internal_Note *note) | |
8118 | { | |
8119 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-high-gprs", note); | |
8120 | } | |
8121 | ||
d7eeb400 MS |
8122 | static bfd_boolean |
8123 | elfcore_grok_s390_timer (bfd *abfd, Elf_Internal_Note *note) | |
8124 | { | |
8125 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-timer", note); | |
8126 | } | |
8127 | ||
8128 | static bfd_boolean | |
8129 | elfcore_grok_s390_todcmp (bfd *abfd, Elf_Internal_Note *note) | |
8130 | { | |
8131 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-todcmp", note); | |
8132 | } | |
8133 | ||
8134 | static bfd_boolean | |
8135 | elfcore_grok_s390_todpreg (bfd *abfd, Elf_Internal_Note *note) | |
8136 | { | |
8137 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-todpreg", note); | |
8138 | } | |
8139 | ||
8140 | static bfd_boolean | |
8141 | elfcore_grok_s390_ctrs (bfd *abfd, Elf_Internal_Note *note) | |
8142 | { | |
8143 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-ctrs", note); | |
8144 | } | |
8145 | ||
8146 | static bfd_boolean | |
8147 | elfcore_grok_s390_prefix (bfd *abfd, Elf_Internal_Note *note) | |
8148 | { | |
8149 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-prefix", note); | |
8150 | } | |
8151 | ||
355b81d9 UW |
8152 | static bfd_boolean |
8153 | elfcore_grok_s390_last_break (bfd *abfd, Elf_Internal_Note *note) | |
8154 | { | |
8155 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-last-break", note); | |
8156 | } | |
8157 | ||
8158 | static bfd_boolean | |
8159 | elfcore_grok_s390_system_call (bfd *abfd, Elf_Internal_Note *note) | |
8160 | { | |
8161 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-system-call", note); | |
8162 | } | |
8163 | ||
faa9a424 UW |
8164 | static bfd_boolean |
8165 | elfcore_grok_arm_vfp (bfd *abfd, Elf_Internal_Note *note) | |
8166 | { | |
8167 | return elfcore_make_note_pseudosection (abfd, ".reg-arm-vfp", note); | |
8168 | } | |
8169 | ||
652451f8 YZ |
8170 | static bfd_boolean |
8171 | elfcore_grok_aarch_tls (bfd *abfd, Elf_Internal_Note *note) | |
8172 | { | |
8173 | return elfcore_make_note_pseudosection (abfd, ".reg-aarch-tls", note); | |
8174 | } | |
8175 | ||
8176 | static bfd_boolean | |
8177 | elfcore_grok_aarch_hw_break (bfd *abfd, Elf_Internal_Note *note) | |
8178 | { | |
8179 | return elfcore_make_note_pseudosection (abfd, ".reg-aarch-hw-break", note); | |
8180 | } | |
8181 | ||
8182 | static bfd_boolean | |
8183 | elfcore_grok_aarch_hw_watch (bfd *abfd, Elf_Internal_Note *note) | |
8184 | { | |
8185 | return elfcore_make_note_pseudosection (abfd, ".reg-aarch-hw-watch", note); | |
8186 | } | |
8187 | ||
252b5132 | 8188 | #if defined (HAVE_PRPSINFO_T) |
4a938328 | 8189 | typedef prpsinfo_t elfcore_psinfo_t; |
7ee38065 | 8190 | #if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */ |
4a938328 MS |
8191 | typedef prpsinfo32_t elfcore_psinfo32_t; |
8192 | #endif | |
252b5132 RH |
8193 | #endif |
8194 | ||
8195 | #if defined (HAVE_PSINFO_T) | |
4a938328 | 8196 | typedef psinfo_t elfcore_psinfo_t; |
7ee38065 | 8197 | #if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */ |
4a938328 MS |
8198 | typedef psinfo32_t elfcore_psinfo32_t; |
8199 | #endif | |
252b5132 RH |
8200 | #endif |
8201 | ||
252b5132 RH |
8202 | /* return a malloc'ed copy of a string at START which is at |
8203 | most MAX bytes long, possibly without a terminating '\0'. | |
c044fabd | 8204 | the copy will always have a terminating '\0'. */ |
252b5132 | 8205 | |
936e320b | 8206 | char * |
217aa764 | 8207 | _bfd_elfcore_strndup (bfd *abfd, char *start, size_t max) |
252b5132 | 8208 | { |
dc810e39 | 8209 | char *dups; |
a50b1753 | 8210 | char *end = (char *) memchr (start, '\0', max); |
dc810e39 | 8211 | size_t len; |
252b5132 RH |
8212 | |
8213 | if (end == NULL) | |
8214 | len = max; | |
8215 | else | |
8216 | len = end - start; | |
8217 | ||
a50b1753 | 8218 | dups = (char *) bfd_alloc (abfd, len + 1); |
dc810e39 | 8219 | if (dups == NULL) |
252b5132 RH |
8220 | return NULL; |
8221 | ||
dc810e39 AM |
8222 | memcpy (dups, start, len); |
8223 | dups[len] = '\0'; | |
252b5132 | 8224 | |
dc810e39 | 8225 | return dups; |
252b5132 RH |
8226 | } |
8227 | ||
bb0082d6 | 8228 | #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) |
b34976b6 | 8229 | static bfd_boolean |
217aa764 | 8230 | elfcore_grok_psinfo (bfd *abfd, Elf_Internal_Note *note) |
252b5132 | 8231 | { |
4a938328 MS |
8232 | if (note->descsz == sizeof (elfcore_psinfo_t)) |
8233 | { | |
8234 | elfcore_psinfo_t psinfo; | |
252b5132 | 8235 | |
7ee38065 | 8236 | memcpy (&psinfo, note->descdata, sizeof (psinfo)); |
252b5132 | 8237 | |
335e41d4 | 8238 | #if defined (HAVE_PSINFO_T_PR_PID) || defined (HAVE_PRPSINFO_T_PR_PID) |
bc989cdc | 8239 | elf_tdata (abfd)->core_pid = psinfo.pr_pid; |
335e41d4 | 8240 | #endif |
4a938328 | 8241 | elf_tdata (abfd)->core_program |
936e320b AM |
8242 | = _bfd_elfcore_strndup (abfd, psinfo.pr_fname, |
8243 | sizeof (psinfo.pr_fname)); | |
252b5132 | 8244 | |
4a938328 | 8245 | elf_tdata (abfd)->core_command |
936e320b AM |
8246 | = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs, |
8247 | sizeof (psinfo.pr_psargs)); | |
4a938328 | 8248 | } |
7ee38065 | 8249 | #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T) |
4a938328 MS |
8250 | else if (note->descsz == sizeof (elfcore_psinfo32_t)) |
8251 | { | |
8252 | /* 64-bit host, 32-bit corefile */ | |
8253 | elfcore_psinfo32_t psinfo; | |
8254 | ||
7ee38065 | 8255 | memcpy (&psinfo, note->descdata, sizeof (psinfo)); |
252b5132 | 8256 | |
335e41d4 | 8257 | #if defined (HAVE_PSINFO32_T_PR_PID) || defined (HAVE_PRPSINFO32_T_PR_PID) |
bc989cdc | 8258 | elf_tdata (abfd)->core_pid = psinfo.pr_pid; |
335e41d4 | 8259 | #endif |
4a938328 | 8260 | elf_tdata (abfd)->core_program |
936e320b AM |
8261 | = _bfd_elfcore_strndup (abfd, psinfo.pr_fname, |
8262 | sizeof (psinfo.pr_fname)); | |
4a938328 MS |
8263 | |
8264 | elf_tdata (abfd)->core_command | |
936e320b AM |
8265 | = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs, |
8266 | sizeof (psinfo.pr_psargs)); | |
4a938328 MS |
8267 | } |
8268 | #endif | |
8269 | ||
8270 | else | |
8271 | { | |
8272 | /* Fail - we don't know how to handle any other | |
8273 | note size (ie. data object type). */ | |
b34976b6 | 8274 | return TRUE; |
4a938328 | 8275 | } |
252b5132 RH |
8276 | |
8277 | /* Note that for some reason, a spurious space is tacked | |
8278 | onto the end of the args in some (at least one anyway) | |
c044fabd | 8279 | implementations, so strip it off if it exists. */ |
252b5132 RH |
8280 | |
8281 | { | |
c044fabd | 8282 | char *command = elf_tdata (abfd)->core_command; |
252b5132 RH |
8283 | int n = strlen (command); |
8284 | ||
8285 | if (0 < n && command[n - 1] == ' ') | |
8286 | command[n - 1] = '\0'; | |
8287 | } | |
8288 | ||
b34976b6 | 8289 | return TRUE; |
252b5132 RH |
8290 | } |
8291 | #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */ | |
8292 | ||
252b5132 | 8293 | #if defined (HAVE_PSTATUS_T) |
b34976b6 | 8294 | static bfd_boolean |
217aa764 | 8295 | elfcore_grok_pstatus (bfd *abfd, Elf_Internal_Note *note) |
252b5132 | 8296 | { |
f572a39d AM |
8297 | if (note->descsz == sizeof (pstatus_t) |
8298 | #if defined (HAVE_PXSTATUS_T) | |
8299 | || note->descsz == sizeof (pxstatus_t) | |
8300 | #endif | |
8301 | ) | |
4a938328 MS |
8302 | { |
8303 | pstatus_t pstat; | |
252b5132 | 8304 | |
4a938328 | 8305 | memcpy (&pstat, note->descdata, sizeof (pstat)); |
252b5132 | 8306 | |
4a938328 MS |
8307 | elf_tdata (abfd)->core_pid = pstat.pr_pid; |
8308 | } | |
7ee38065 | 8309 | #if defined (HAVE_PSTATUS32_T) |
4a938328 MS |
8310 | else if (note->descsz == sizeof (pstatus32_t)) |
8311 | { | |
8312 | /* 64-bit host, 32-bit corefile */ | |
8313 | pstatus32_t pstat; | |
252b5132 | 8314 | |
4a938328 | 8315 | memcpy (&pstat, note->descdata, sizeof (pstat)); |
252b5132 | 8316 | |
4a938328 MS |
8317 | elf_tdata (abfd)->core_pid = pstat.pr_pid; |
8318 | } | |
8319 | #endif | |
252b5132 RH |
8320 | /* Could grab some more details from the "representative" |
8321 | lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an | |
c044fabd | 8322 | NT_LWPSTATUS note, presumably. */ |
252b5132 | 8323 | |
b34976b6 | 8324 | return TRUE; |
252b5132 RH |
8325 | } |
8326 | #endif /* defined (HAVE_PSTATUS_T) */ | |
8327 | ||
252b5132 | 8328 | #if defined (HAVE_LWPSTATUS_T) |
b34976b6 | 8329 | static bfd_boolean |
217aa764 | 8330 | elfcore_grok_lwpstatus (bfd *abfd, Elf_Internal_Note *note) |
252b5132 RH |
8331 | { |
8332 | lwpstatus_t lwpstat; | |
8333 | char buf[100]; | |
c044fabd | 8334 | char *name; |
d4c88bbb | 8335 | size_t len; |
c044fabd | 8336 | asection *sect; |
252b5132 | 8337 | |
f572a39d AM |
8338 | if (note->descsz != sizeof (lwpstat) |
8339 | #if defined (HAVE_LWPXSTATUS_T) | |
8340 | && note->descsz != sizeof (lwpxstatus_t) | |
8341 | #endif | |
8342 | ) | |
b34976b6 | 8343 | return TRUE; |
252b5132 RH |
8344 | |
8345 | memcpy (&lwpstat, note->descdata, sizeof (lwpstat)); | |
8346 | ||
8347 | elf_tdata (abfd)->core_lwpid = lwpstat.pr_lwpid; | |
a1504221 JB |
8348 | /* Do not overwrite the core signal if it has already been set by |
8349 | another thread. */ | |
8350 | if (elf_tdata (abfd)->core_signal == 0) | |
8351 | elf_tdata (abfd)->core_signal = lwpstat.pr_cursig; | |
252b5132 | 8352 | |
c044fabd | 8353 | /* Make a ".reg/999" section. */ |
252b5132 RH |
8354 | |
8355 | sprintf (buf, ".reg/%d", elfcore_make_pid (abfd)); | |
d4c88bbb | 8356 | len = strlen (buf) + 1; |
217aa764 | 8357 | name = bfd_alloc (abfd, len); |
252b5132 | 8358 | if (name == NULL) |
b34976b6 | 8359 | return FALSE; |
d4c88bbb | 8360 | memcpy (name, buf, len); |
252b5132 | 8361 | |
117ed4f8 | 8362 | sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); |
252b5132 | 8363 | if (sect == NULL) |
b34976b6 | 8364 | return FALSE; |
252b5132 RH |
8365 | |
8366 | #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT) | |
eea6121a | 8367 | sect->size = sizeof (lwpstat.pr_context.uc_mcontext.gregs); |
252b5132 RH |
8368 | sect->filepos = note->descpos |
8369 | + offsetof (lwpstatus_t, pr_context.uc_mcontext.gregs); | |
8370 | #endif | |
8371 | ||
8372 | #if defined (HAVE_LWPSTATUS_T_PR_REG) | |
eea6121a | 8373 | sect->size = sizeof (lwpstat.pr_reg); |
252b5132 RH |
8374 | sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_reg); |
8375 | #endif | |
8376 | ||
252b5132 RH |
8377 | sect->alignment_power = 2; |
8378 | ||
8379 | if (!elfcore_maybe_make_sect (abfd, ".reg", sect)) | |
b34976b6 | 8380 | return FALSE; |
252b5132 RH |
8381 | |
8382 | /* Make a ".reg2/999" section */ | |
8383 | ||
8384 | sprintf (buf, ".reg2/%d", elfcore_make_pid (abfd)); | |
d4c88bbb | 8385 | len = strlen (buf) + 1; |
217aa764 | 8386 | name = bfd_alloc (abfd, len); |
252b5132 | 8387 | if (name == NULL) |
b34976b6 | 8388 | return FALSE; |
d4c88bbb | 8389 | memcpy (name, buf, len); |
252b5132 | 8390 | |
117ed4f8 | 8391 | sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); |
252b5132 | 8392 | if (sect == NULL) |
b34976b6 | 8393 | return FALSE; |
252b5132 RH |
8394 | |
8395 | #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT) | |
eea6121a | 8396 | sect->size = sizeof (lwpstat.pr_context.uc_mcontext.fpregs); |
252b5132 RH |
8397 | sect->filepos = note->descpos |
8398 | + offsetof (lwpstatus_t, pr_context.uc_mcontext.fpregs); | |
8399 | #endif | |
8400 | ||
8401 | #if defined (HAVE_LWPSTATUS_T_PR_FPREG) | |
eea6121a | 8402 | sect->size = sizeof (lwpstat.pr_fpreg); |
252b5132 RH |
8403 | sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_fpreg); |
8404 | #endif | |
8405 | ||
252b5132 RH |
8406 | sect->alignment_power = 2; |
8407 | ||
936e320b | 8408 | return elfcore_maybe_make_sect (abfd, ".reg2", sect); |
252b5132 RH |
8409 | } |
8410 | #endif /* defined (HAVE_LWPSTATUS_T) */ | |
8411 | ||
b34976b6 | 8412 | static bfd_boolean |
217aa764 | 8413 | elfcore_grok_win32pstatus (bfd *abfd, Elf_Internal_Note *note) |
16e9c715 NC |
8414 | { |
8415 | char buf[30]; | |
c044fabd | 8416 | char *name; |
d4c88bbb | 8417 | size_t len; |
c044fabd | 8418 | asection *sect; |
4a6636fb PA |
8419 | int type; |
8420 | int is_active_thread; | |
8421 | bfd_vma base_addr; | |
16e9c715 | 8422 | |
4a6636fb | 8423 | if (note->descsz < 728) |
b34976b6 | 8424 | return TRUE; |
16e9c715 | 8425 | |
4a6636fb PA |
8426 | if (! CONST_STRNEQ (note->namedata, "win32")) |
8427 | return TRUE; | |
8428 | ||
8429 | type = bfd_get_32 (abfd, note->descdata); | |
c044fabd | 8430 | |
4a6636fb | 8431 | switch (type) |
16e9c715 | 8432 | { |
4a6636fb | 8433 | case 1 /* NOTE_INFO_PROCESS */: |
16e9c715 | 8434 | /* FIXME: need to add ->core_command. */ |
4a6636fb PA |
8435 | /* process_info.pid */ |
8436 | elf_tdata (abfd)->core_pid = bfd_get_32 (abfd, note->descdata + 8); | |
8437 | /* process_info.signal */ | |
8438 | elf_tdata (abfd)->core_signal = bfd_get_32 (abfd, note->descdata + 12); | |
c044fabd | 8439 | break; |
16e9c715 | 8440 | |
4a6636fb | 8441 | case 2 /* NOTE_INFO_THREAD */: |
16e9c715 | 8442 | /* Make a ".reg/999" section. */ |
4a6636fb PA |
8443 | /* thread_info.tid */ |
8444 | sprintf (buf, ".reg/%ld", (long) bfd_get_32 (abfd, note->descdata + 8)); | |
c044fabd | 8445 | |
d4c88bbb | 8446 | len = strlen (buf) + 1; |
a50b1753 | 8447 | name = (char *) bfd_alloc (abfd, len); |
16e9c715 | 8448 | if (name == NULL) |
b34976b6 | 8449 | return FALSE; |
c044fabd | 8450 | |
d4c88bbb | 8451 | memcpy (name, buf, len); |
16e9c715 | 8452 | |
117ed4f8 | 8453 | sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); |
16e9c715 | 8454 | if (sect == NULL) |
b34976b6 | 8455 | return FALSE; |
c044fabd | 8456 | |
4a6636fb PA |
8457 | /* sizeof (thread_info.thread_context) */ |
8458 | sect->size = 716; | |
8459 | /* offsetof (thread_info.thread_context) */ | |
8460 | sect->filepos = note->descpos + 12; | |
16e9c715 NC |
8461 | sect->alignment_power = 2; |
8462 | ||
4a6636fb PA |
8463 | /* thread_info.is_active_thread */ |
8464 | is_active_thread = bfd_get_32 (abfd, note->descdata + 8); | |
8465 | ||
8466 | if (is_active_thread) | |
16e9c715 | 8467 | if (! elfcore_maybe_make_sect (abfd, ".reg", sect)) |
b34976b6 | 8468 | return FALSE; |
16e9c715 NC |
8469 | break; |
8470 | ||
4a6636fb | 8471 | case 3 /* NOTE_INFO_MODULE */: |
16e9c715 | 8472 | /* Make a ".module/xxxxxxxx" section. */ |
4a6636fb PA |
8473 | /* module_info.base_address */ |
8474 | base_addr = bfd_get_32 (abfd, note->descdata + 4); | |
0af1713e | 8475 | sprintf (buf, ".module/%08lx", (unsigned long) base_addr); |
c044fabd | 8476 | |
d4c88bbb | 8477 | len = strlen (buf) + 1; |
a50b1753 | 8478 | name = (char *) bfd_alloc (abfd, len); |
16e9c715 | 8479 | if (name == NULL) |
b34976b6 | 8480 | return FALSE; |
c044fabd | 8481 | |
d4c88bbb | 8482 | memcpy (name, buf, len); |
252b5132 | 8483 | |
117ed4f8 | 8484 | sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); |
c044fabd | 8485 | |
16e9c715 | 8486 | if (sect == NULL) |
b34976b6 | 8487 | return FALSE; |
c044fabd | 8488 | |
eea6121a | 8489 | sect->size = note->descsz; |
16e9c715 | 8490 | sect->filepos = note->descpos; |
16e9c715 NC |
8491 | sect->alignment_power = 2; |
8492 | break; | |
8493 | ||
8494 | default: | |
b34976b6 | 8495 | return TRUE; |
16e9c715 NC |
8496 | } |
8497 | ||
b34976b6 | 8498 | return TRUE; |
16e9c715 | 8499 | } |
252b5132 | 8500 | |
b34976b6 | 8501 | static bfd_boolean |
217aa764 | 8502 | elfcore_grok_note (bfd *abfd, Elf_Internal_Note *note) |
252b5132 | 8503 | { |
9c5bfbb7 | 8504 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
bb0082d6 | 8505 | |
252b5132 RH |
8506 | switch (note->type) |
8507 | { | |
8508 | default: | |
b34976b6 | 8509 | return TRUE; |
252b5132 | 8510 | |
252b5132 | 8511 | case NT_PRSTATUS: |
bb0082d6 AM |
8512 | if (bed->elf_backend_grok_prstatus) |
8513 | if ((*bed->elf_backend_grok_prstatus) (abfd, note)) | |
b34976b6 | 8514 | return TRUE; |
bb0082d6 | 8515 | #if defined (HAVE_PRSTATUS_T) |
252b5132 | 8516 | return elfcore_grok_prstatus (abfd, note); |
bb0082d6 | 8517 | #else |
b34976b6 | 8518 | return TRUE; |
252b5132 RH |
8519 | #endif |
8520 | ||
8521 | #if defined (HAVE_PSTATUS_T) | |
8522 | case NT_PSTATUS: | |
8523 | return elfcore_grok_pstatus (abfd, note); | |
8524 | #endif | |
8525 | ||
8526 | #if defined (HAVE_LWPSTATUS_T) | |
8527 | case NT_LWPSTATUS: | |
8528 | return elfcore_grok_lwpstatus (abfd, note); | |
8529 | #endif | |
8530 | ||
8531 | case NT_FPREGSET: /* FIXME: rename to NT_PRFPREG */ | |
8532 | return elfcore_grok_prfpreg (abfd, note); | |
8533 | ||
c044fabd | 8534 | case NT_WIN32PSTATUS: |
16e9c715 | 8535 | return elfcore_grok_win32pstatus (abfd, note); |
16e9c715 | 8536 | |
c044fabd | 8537 | case NT_PRXFPREG: /* Linux SSE extension */ |
e377ab71 MK |
8538 | if (note->namesz == 6 |
8539 | && strcmp (note->namedata, "LINUX") == 0) | |
ff08c6bb JB |
8540 | return elfcore_grok_prxfpreg (abfd, note); |
8541 | else | |
b34976b6 | 8542 | return TRUE; |
ff08c6bb | 8543 | |
4339cae0 L |
8544 | case NT_X86_XSTATE: /* Linux XSAVE extension */ |
8545 | if (note->namesz == 6 | |
8546 | && strcmp (note->namedata, "LINUX") == 0) | |
8547 | return elfcore_grok_xstatereg (abfd, note); | |
8548 | else | |
8549 | return TRUE; | |
8550 | ||
97753bd5 AM |
8551 | case NT_PPC_VMX: |
8552 | if (note->namesz == 6 | |
8553 | && strcmp (note->namedata, "LINUX") == 0) | |
8554 | return elfcore_grok_ppc_vmx (abfd, note); | |
8555 | else | |
8556 | return TRUE; | |
8557 | ||
89eeb0bc LM |
8558 | case NT_PPC_VSX: |
8559 | if (note->namesz == 6 | |
8560 | && strcmp (note->namedata, "LINUX") == 0) | |
8561 | return elfcore_grok_ppc_vsx (abfd, note); | |
8562 | else | |
8563 | return TRUE; | |
8564 | ||
0675e188 UW |
8565 | case NT_S390_HIGH_GPRS: |
8566 | if (note->namesz == 6 | |
8567 | && strcmp (note->namedata, "LINUX") == 0) | |
8568 | return elfcore_grok_s390_high_gprs (abfd, note); | |
8569 | else | |
8570 | return TRUE; | |
8571 | ||
d7eeb400 MS |
8572 | case NT_S390_TIMER: |
8573 | if (note->namesz == 6 | |
8574 | && strcmp (note->namedata, "LINUX") == 0) | |
8575 | return elfcore_grok_s390_timer (abfd, note); | |
8576 | else | |
8577 | return TRUE; | |
8578 | ||
8579 | case NT_S390_TODCMP: | |
8580 | if (note->namesz == 6 | |
8581 | && strcmp (note->namedata, "LINUX") == 0) | |
8582 | return elfcore_grok_s390_todcmp (abfd, note); | |
8583 | else | |
8584 | return TRUE; | |
8585 | ||
8586 | case NT_S390_TODPREG: | |
8587 | if (note->namesz == 6 | |
8588 | && strcmp (note->namedata, "LINUX") == 0) | |
8589 | return elfcore_grok_s390_todpreg (abfd, note); | |
8590 | else | |
8591 | return TRUE; | |
8592 | ||
8593 | case NT_S390_CTRS: | |
8594 | if (note->namesz == 6 | |
8595 | && strcmp (note->namedata, "LINUX") == 0) | |
8596 | return elfcore_grok_s390_ctrs (abfd, note); | |
8597 | else | |
8598 | return TRUE; | |
8599 | ||
8600 | case NT_S390_PREFIX: | |
8601 | if (note->namesz == 6 | |
8602 | && strcmp (note->namedata, "LINUX") == 0) | |
8603 | return elfcore_grok_s390_prefix (abfd, note); | |
8604 | else | |
8605 | return TRUE; | |
8606 | ||
355b81d9 UW |
8607 | case NT_S390_LAST_BREAK: |
8608 | if (note->namesz == 6 | |
8609 | && strcmp (note->namedata, "LINUX") == 0) | |
8610 | return elfcore_grok_s390_last_break (abfd, note); | |
8611 | else | |
8612 | return TRUE; | |
8613 | ||
8614 | case NT_S390_SYSTEM_CALL: | |
8615 | if (note->namesz == 6 | |
8616 | && strcmp (note->namedata, "LINUX") == 0) | |
8617 | return elfcore_grok_s390_system_call (abfd, note); | |
8618 | else | |
8619 | return TRUE; | |
8620 | ||
faa9a424 UW |
8621 | case NT_ARM_VFP: |
8622 | if (note->namesz == 6 | |
8623 | && strcmp (note->namedata, "LINUX") == 0) | |
8624 | return elfcore_grok_arm_vfp (abfd, note); | |
8625 | else | |
8626 | return TRUE; | |
8627 | ||
652451f8 YZ |
8628 | case NT_ARM_TLS: |
8629 | if (note->namesz == 6 | |
8630 | && strcmp (note->namedata, "LINUX") == 0) | |
8631 | return elfcore_grok_aarch_tls (abfd, note); | |
8632 | else | |
8633 | return TRUE; | |
8634 | ||
8635 | case NT_ARM_HW_BREAK: | |
8636 | if (note->namesz == 6 | |
8637 | && strcmp (note->namedata, "LINUX") == 0) | |
8638 | return elfcore_grok_aarch_hw_break (abfd, note); | |
8639 | else | |
8640 | return TRUE; | |
8641 | ||
8642 | case NT_ARM_HW_WATCH: | |
8643 | if (note->namesz == 6 | |
8644 | && strcmp (note->namedata, "LINUX") == 0) | |
8645 | return elfcore_grok_aarch_hw_watch (abfd, note); | |
8646 | else | |
8647 | return TRUE; | |
8648 | ||
252b5132 RH |
8649 | case NT_PRPSINFO: |
8650 | case NT_PSINFO: | |
bb0082d6 AM |
8651 | if (bed->elf_backend_grok_psinfo) |
8652 | if ((*bed->elf_backend_grok_psinfo) (abfd, note)) | |
b34976b6 | 8653 | return TRUE; |
bb0082d6 | 8654 | #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) |
252b5132 | 8655 | return elfcore_grok_psinfo (abfd, note); |
bb0082d6 | 8656 | #else |
b34976b6 | 8657 | return TRUE; |
252b5132 | 8658 | #endif |
3333a7c3 RM |
8659 | |
8660 | case NT_AUXV: | |
8661 | { | |
117ed4f8 AM |
8662 | asection *sect = bfd_make_section_anyway_with_flags (abfd, ".auxv", |
8663 | SEC_HAS_CONTENTS); | |
3333a7c3 RM |
8664 | |
8665 | if (sect == NULL) | |
8666 | return FALSE; | |
eea6121a | 8667 | sect->size = note->descsz; |
3333a7c3 | 8668 | sect->filepos = note->descpos; |
3333a7c3 RM |
8669 | sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32; |
8670 | ||
8671 | return TRUE; | |
8672 | } | |
9015683b | 8673 | |
451b7c33 TT |
8674 | case NT_FILE: |
8675 | return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.file", | |
8676 | note); | |
8677 | ||
9015683b TT |
8678 | case NT_SIGINFO: |
8679 | return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.siginfo", | |
8680 | note); | |
252b5132 RH |
8681 | } |
8682 | } | |
8683 | ||
718175fa JK |
8684 | static bfd_boolean |
8685 | elfobj_grok_gnu_build_id (bfd *abfd, Elf_Internal_Note *note) | |
8686 | { | |
8687 | elf_tdata (abfd)->build_id_size = note->descsz; | |
a50b1753 | 8688 | elf_tdata (abfd)->build_id = (bfd_byte *) bfd_alloc (abfd, note->descsz); |
718175fa JK |
8689 | if (elf_tdata (abfd)->build_id == NULL) |
8690 | return FALSE; | |
8691 | ||
8692 | memcpy (elf_tdata (abfd)->build_id, note->descdata, note->descsz); | |
8693 | ||
8694 | return TRUE; | |
8695 | } | |
8696 | ||
8697 | static bfd_boolean | |
8698 | elfobj_grok_gnu_note (bfd *abfd, Elf_Internal_Note *note) | |
8699 | { | |
8700 | switch (note->type) | |
8701 | { | |
8702 | default: | |
8703 | return TRUE; | |
8704 | ||
8705 | case NT_GNU_BUILD_ID: | |
8706 | return elfobj_grok_gnu_build_id (abfd, note); | |
8707 | } | |
8708 | } | |
8709 | ||
e21e5835 NC |
8710 | static bfd_boolean |
8711 | elfobj_grok_stapsdt_note_1 (bfd *abfd, Elf_Internal_Note *note) | |
8712 | { | |
8713 | struct sdt_note *cur = | |
8714 | (struct sdt_note *) bfd_alloc (abfd, sizeof (struct sdt_note) | |
8715 | + note->descsz); | |
8716 | ||
8717 | cur->next = (struct sdt_note *) (elf_tdata (abfd))->sdt_note_head; | |
8718 | cur->size = (bfd_size_type) note->descsz; | |
8719 | memcpy (cur->data, note->descdata, note->descsz); | |
8720 | ||
8721 | elf_tdata (abfd)->sdt_note_head = cur; | |
8722 | ||
8723 | return TRUE; | |
8724 | } | |
8725 | ||
8726 | static bfd_boolean | |
8727 | elfobj_grok_stapsdt_note (bfd *abfd, Elf_Internal_Note *note) | |
8728 | { | |
8729 | switch (note->type) | |
8730 | { | |
8731 | case NT_STAPSDT: | |
8732 | return elfobj_grok_stapsdt_note_1 (abfd, note); | |
8733 | ||
8734 | default: | |
8735 | return TRUE; | |
8736 | } | |
8737 | } | |
8738 | ||
b34976b6 | 8739 | static bfd_boolean |
217aa764 | 8740 | elfcore_netbsd_get_lwpid (Elf_Internal_Note *note, int *lwpidp) |
50b2bdb7 AM |
8741 | { |
8742 | char *cp; | |
8743 | ||
8744 | cp = strchr (note->namedata, '@'); | |
8745 | if (cp != NULL) | |
8746 | { | |
d2b64500 | 8747 | *lwpidp = atoi(cp + 1); |
b34976b6 | 8748 | return TRUE; |
50b2bdb7 | 8749 | } |
b34976b6 | 8750 | return FALSE; |
50b2bdb7 AM |
8751 | } |
8752 | ||
b34976b6 | 8753 | static bfd_boolean |
217aa764 | 8754 | elfcore_grok_netbsd_procinfo (bfd *abfd, Elf_Internal_Note *note) |
50b2bdb7 | 8755 | { |
50b2bdb7 AM |
8756 | /* Signal number at offset 0x08. */ |
8757 | elf_tdata (abfd)->core_signal | |
8758 | = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08); | |
8759 | ||
8760 | /* Process ID at offset 0x50. */ | |
8761 | elf_tdata (abfd)->core_pid | |
8762 | = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x50); | |
8763 | ||
8764 | /* Command name at 0x7c (max 32 bytes, including nul). */ | |
8765 | elf_tdata (abfd)->core_command | |
8766 | = _bfd_elfcore_strndup (abfd, note->descdata + 0x7c, 31); | |
8767 | ||
7720ba9f MK |
8768 | return elfcore_make_note_pseudosection (abfd, ".note.netbsdcore.procinfo", |
8769 | note); | |
50b2bdb7 AM |
8770 | } |
8771 | ||
b34976b6 | 8772 | static bfd_boolean |
217aa764 | 8773 | elfcore_grok_netbsd_note (bfd *abfd, Elf_Internal_Note *note) |
50b2bdb7 AM |
8774 | { |
8775 | int lwp; | |
8776 | ||
8777 | if (elfcore_netbsd_get_lwpid (note, &lwp)) | |
8778 | elf_tdata (abfd)->core_lwpid = lwp; | |
8779 | ||
b4db1224 | 8780 | if (note->type == NT_NETBSDCORE_PROCINFO) |
50b2bdb7 AM |
8781 | { |
8782 | /* NetBSD-specific core "procinfo". Note that we expect to | |
08a40648 AM |
8783 | find this note before any of the others, which is fine, |
8784 | since the kernel writes this note out first when it | |
8785 | creates a core file. */ | |
47d9a591 | 8786 | |
50b2bdb7 AM |
8787 | return elfcore_grok_netbsd_procinfo (abfd, note); |
8788 | } | |
8789 | ||
b4db1224 JT |
8790 | /* As of Jan 2002 there are no other machine-independent notes |
8791 | defined for NetBSD core files. If the note type is less | |
8792 | than the start of the machine-dependent note types, we don't | |
8793 | understand it. */ | |
47d9a591 | 8794 | |
b4db1224 | 8795 | if (note->type < NT_NETBSDCORE_FIRSTMACH) |
b34976b6 | 8796 | return TRUE; |
50b2bdb7 AM |
8797 | |
8798 | ||
8799 | switch (bfd_get_arch (abfd)) | |
8800 | { | |
08a40648 AM |
8801 | /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and |
8802 | PT_GETFPREGS == mach+2. */ | |
50b2bdb7 AM |
8803 | |
8804 | case bfd_arch_alpha: | |
8805 | case bfd_arch_sparc: | |
8806 | switch (note->type) | |
08a40648 AM |
8807 | { |
8808 | case NT_NETBSDCORE_FIRSTMACH+0: | |
8809 | return elfcore_make_note_pseudosection (abfd, ".reg", note); | |
50b2bdb7 | 8810 | |
08a40648 AM |
8811 | case NT_NETBSDCORE_FIRSTMACH+2: |
8812 | return elfcore_make_note_pseudosection (abfd, ".reg2", note); | |
50b2bdb7 | 8813 | |
08a40648 AM |
8814 | default: |
8815 | return TRUE; | |
8816 | } | |
50b2bdb7 | 8817 | |
08a40648 AM |
8818 | /* On all other arch's, PT_GETREGS == mach+1 and |
8819 | PT_GETFPREGS == mach+3. */ | |
50b2bdb7 AM |
8820 | |
8821 | default: | |
8822 | switch (note->type) | |
08a40648 AM |
8823 | { |
8824 | case NT_NETBSDCORE_FIRSTMACH+1: | |
8825 | return elfcore_make_note_pseudosection (abfd, ".reg", note); | |
50b2bdb7 | 8826 | |
08a40648 AM |
8827 | case NT_NETBSDCORE_FIRSTMACH+3: |
8828 | return elfcore_make_note_pseudosection (abfd, ".reg2", note); | |
50b2bdb7 | 8829 | |
08a40648 AM |
8830 | default: |
8831 | return TRUE; | |
8832 | } | |
50b2bdb7 AM |
8833 | } |
8834 | /* NOTREACHED */ | |
8835 | } | |
8836 | ||
67cc5033 MK |
8837 | static bfd_boolean |
8838 | elfcore_grok_openbsd_procinfo (bfd *abfd, Elf_Internal_Note *note) | |
8839 | { | |
8840 | /* Signal number at offset 0x08. */ | |
8841 | elf_tdata (abfd)->core_signal | |
8842 | = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08); | |
8843 | ||
8844 | /* Process ID at offset 0x20. */ | |
8845 | elf_tdata (abfd)->core_pid | |
8846 | = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x20); | |
8847 | ||
8848 | /* Command name at 0x48 (max 32 bytes, including nul). */ | |
8849 | elf_tdata (abfd)->core_command | |
8850 | = _bfd_elfcore_strndup (abfd, note->descdata + 0x48, 31); | |
8851 | ||
8852 | return TRUE; | |
8853 | } | |
8854 | ||
8855 | static bfd_boolean | |
8856 | elfcore_grok_openbsd_note (bfd *abfd, Elf_Internal_Note *note) | |
8857 | { | |
8858 | if (note->type == NT_OPENBSD_PROCINFO) | |
8859 | return elfcore_grok_openbsd_procinfo (abfd, note); | |
8860 | ||
8861 | if (note->type == NT_OPENBSD_REGS) | |
8862 | return elfcore_make_note_pseudosection (abfd, ".reg", note); | |
8863 | ||
8864 | if (note->type == NT_OPENBSD_FPREGS) | |
8865 | return elfcore_make_note_pseudosection (abfd, ".reg2", note); | |
8866 | ||
8867 | if (note->type == NT_OPENBSD_XFPREGS) | |
8868 | return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note); | |
8869 | ||
8870 | if (note->type == NT_OPENBSD_AUXV) | |
8871 | { | |
8872 | asection *sect = bfd_make_section_anyway_with_flags (abfd, ".auxv", | |
8873 | SEC_HAS_CONTENTS); | |
8874 | ||
8875 | if (sect == NULL) | |
8876 | return FALSE; | |
8877 | sect->size = note->descsz; | |
8878 | sect->filepos = note->descpos; | |
8879 | sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32; | |
8880 | ||
8881 | return TRUE; | |
8882 | } | |
8883 | ||
8884 | if (note->type == NT_OPENBSD_WCOOKIE) | |
8885 | { | |
8886 | asection *sect = bfd_make_section_anyway_with_flags (abfd, ".wcookie", | |
8887 | SEC_HAS_CONTENTS); | |
8888 | ||
8889 | if (sect == NULL) | |
8890 | return FALSE; | |
8891 | sect->size = note->descsz; | |
8892 | sect->filepos = note->descpos; | |
8893 | sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32; | |
8894 | ||
8895 | return TRUE; | |
8896 | } | |
8897 | ||
8898 | return TRUE; | |
8899 | } | |
8900 | ||
07c6e936 | 8901 | static bfd_boolean |
d3fd4074 | 8902 | elfcore_grok_nto_status (bfd *abfd, Elf_Internal_Note *note, long *tid) |
07c6e936 NC |
8903 | { |
8904 | void *ddata = note->descdata; | |
8905 | char buf[100]; | |
8906 | char *name; | |
8907 | asection *sect; | |
f8843e87 AM |
8908 | short sig; |
8909 | unsigned flags; | |
07c6e936 NC |
8910 | |
8911 | /* nto_procfs_status 'pid' field is at offset 0. */ | |
8912 | elf_tdata (abfd)->core_pid = bfd_get_32 (abfd, (bfd_byte *) ddata); | |
8913 | ||
f8843e87 AM |
8914 | /* nto_procfs_status 'tid' field is at offset 4. Pass it back. */ |
8915 | *tid = bfd_get_32 (abfd, (bfd_byte *) ddata + 4); | |
8916 | ||
8917 | /* nto_procfs_status 'flags' field is at offset 8. */ | |
8918 | flags = bfd_get_32 (abfd, (bfd_byte *) ddata + 8); | |
07c6e936 NC |
8919 | |
8920 | /* nto_procfs_status 'what' field is at offset 14. */ | |
f8843e87 AM |
8921 | if ((sig = bfd_get_16 (abfd, (bfd_byte *) ddata + 14)) > 0) |
8922 | { | |
8923 | elf_tdata (abfd)->core_signal = sig; | |
8924 | elf_tdata (abfd)->core_lwpid = *tid; | |
8925 | } | |
07c6e936 | 8926 | |
f8843e87 AM |
8927 | /* _DEBUG_FLAG_CURTID (current thread) is 0x80. Some cores |
8928 | do not come from signals so we make sure we set the current | |
8929 | thread just in case. */ | |
8930 | if (flags & 0x00000080) | |
8931 | elf_tdata (abfd)->core_lwpid = *tid; | |
07c6e936 NC |
8932 | |
8933 | /* Make a ".qnx_core_status/%d" section. */ | |
d3fd4074 | 8934 | sprintf (buf, ".qnx_core_status/%ld", *tid); |
07c6e936 | 8935 | |
a50b1753 | 8936 | name = (char *) bfd_alloc (abfd, strlen (buf) + 1); |
07c6e936 NC |
8937 | if (name == NULL) |
8938 | return FALSE; | |
8939 | strcpy (name, buf); | |
8940 | ||
117ed4f8 | 8941 | sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); |
07c6e936 NC |
8942 | if (sect == NULL) |
8943 | return FALSE; | |
8944 | ||
eea6121a | 8945 | sect->size = note->descsz; |
07c6e936 | 8946 | sect->filepos = note->descpos; |
07c6e936 NC |
8947 | sect->alignment_power = 2; |
8948 | ||
8949 | return (elfcore_maybe_make_sect (abfd, ".qnx_core_status", sect)); | |
8950 | } | |
8951 | ||
8952 | static bfd_boolean | |
d69f560c KW |
8953 | elfcore_grok_nto_regs (bfd *abfd, |
8954 | Elf_Internal_Note *note, | |
d3fd4074 | 8955 | long tid, |
d69f560c | 8956 | char *base) |
07c6e936 NC |
8957 | { |
8958 | char buf[100]; | |
8959 | char *name; | |
8960 | asection *sect; | |
8961 | ||
d69f560c | 8962 | /* Make a "(base)/%d" section. */ |
d3fd4074 | 8963 | sprintf (buf, "%s/%ld", base, tid); |
07c6e936 | 8964 | |
a50b1753 | 8965 | name = (char *) bfd_alloc (abfd, strlen (buf) + 1); |
07c6e936 NC |
8966 | if (name == NULL) |
8967 | return FALSE; | |
8968 | strcpy (name, buf); | |
8969 | ||
117ed4f8 | 8970 | sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); |
07c6e936 NC |
8971 | if (sect == NULL) |
8972 | return FALSE; | |
8973 | ||
eea6121a | 8974 | sect->size = note->descsz; |
07c6e936 | 8975 | sect->filepos = note->descpos; |
07c6e936 NC |
8976 | sect->alignment_power = 2; |
8977 | ||
f8843e87 AM |
8978 | /* This is the current thread. */ |
8979 | if (elf_tdata (abfd)->core_lwpid == tid) | |
d69f560c | 8980 | return elfcore_maybe_make_sect (abfd, base, sect); |
f8843e87 AM |
8981 | |
8982 | return TRUE; | |
07c6e936 NC |
8983 | } |
8984 | ||
8985 | #define BFD_QNT_CORE_INFO 7 | |
8986 | #define BFD_QNT_CORE_STATUS 8 | |
8987 | #define BFD_QNT_CORE_GREG 9 | |
8988 | #define BFD_QNT_CORE_FPREG 10 | |
8989 | ||
8990 | static bfd_boolean | |
217aa764 | 8991 | elfcore_grok_nto_note (bfd *abfd, Elf_Internal_Note *note) |
07c6e936 NC |
8992 | { |
8993 | /* Every GREG section has a STATUS section before it. Store the | |
811072d8 | 8994 | tid from the previous call to pass down to the next gregs |
07c6e936 | 8995 | function. */ |
d3fd4074 | 8996 | static long tid = 1; |
07c6e936 NC |
8997 | |
8998 | switch (note->type) | |
8999 | { | |
d69f560c KW |
9000 | case BFD_QNT_CORE_INFO: |
9001 | return elfcore_make_note_pseudosection (abfd, ".qnx_core_info", note); | |
9002 | case BFD_QNT_CORE_STATUS: | |
9003 | return elfcore_grok_nto_status (abfd, note, &tid); | |
9004 | case BFD_QNT_CORE_GREG: | |
9005 | return elfcore_grok_nto_regs (abfd, note, tid, ".reg"); | |
9006 | case BFD_QNT_CORE_FPREG: | |
9007 | return elfcore_grok_nto_regs (abfd, note, tid, ".reg2"); | |
9008 | default: | |
9009 | return TRUE; | |
07c6e936 NC |
9010 | } |
9011 | } | |
9012 | ||
b15fa79e AM |
9013 | static bfd_boolean |
9014 | elfcore_grok_spu_note (bfd *abfd, Elf_Internal_Note *note) | |
9015 | { | |
9016 | char *name; | |
9017 | asection *sect; | |
9018 | size_t len; | |
9019 | ||
9020 | /* Use note name as section name. */ | |
9021 | len = note->namesz; | |
a50b1753 | 9022 | name = (char *) bfd_alloc (abfd, len); |
b15fa79e AM |
9023 | if (name == NULL) |
9024 | return FALSE; | |
9025 | memcpy (name, note->namedata, len); | |
9026 | name[len - 1] = '\0'; | |
9027 | ||
9028 | sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); | |
9029 | if (sect == NULL) | |
9030 | return FALSE; | |
9031 | ||
9032 | sect->size = note->descsz; | |
9033 | sect->filepos = note->descpos; | |
9034 | sect->alignment_power = 1; | |
9035 | ||
9036 | return TRUE; | |
9037 | } | |
9038 | ||
7c76fa91 MS |
9039 | /* Function: elfcore_write_note |
9040 | ||
47d9a591 | 9041 | Inputs: |
a39f3346 | 9042 | buffer to hold note, and current size of buffer |
7c76fa91 MS |
9043 | name of note |
9044 | type of note | |
9045 | data for note | |
9046 | size of data for note | |
9047 | ||
a39f3346 AM |
9048 | Writes note to end of buffer. ELF64 notes are written exactly as |
9049 | for ELF32, despite the current (as of 2006) ELF gabi specifying | |
9050 | that they ought to have 8-byte namesz and descsz field, and have | |
9051 | 8-byte alignment. Other writers, eg. Linux kernel, do the same. | |
9052 | ||
7c76fa91 | 9053 | Return: |
a39f3346 | 9054 | Pointer to realloc'd buffer, *BUFSIZ updated. */ |
7c76fa91 MS |
9055 | |
9056 | char * | |
a39f3346 | 9057 | elfcore_write_note (bfd *abfd, |
217aa764 | 9058 | char *buf, |
a39f3346 | 9059 | int *bufsiz, |
217aa764 | 9060 | const char *name, |
a39f3346 | 9061 | int type, |
217aa764 | 9062 | const void *input, |
a39f3346 | 9063 | int size) |
7c76fa91 MS |
9064 | { |
9065 | Elf_External_Note *xnp; | |
d4c88bbb | 9066 | size_t namesz; |
d4c88bbb | 9067 | size_t newspace; |
a39f3346 | 9068 | char *dest; |
7c76fa91 | 9069 | |
d4c88bbb | 9070 | namesz = 0; |
d4c88bbb | 9071 | if (name != NULL) |
a39f3346 | 9072 | namesz = strlen (name) + 1; |
d4c88bbb | 9073 | |
a39f3346 | 9074 | newspace = 12 + ((namesz + 3) & -4) + ((size + 3) & -4); |
d4c88bbb | 9075 | |
a50b1753 | 9076 | buf = (char *) realloc (buf, *bufsiz + newspace); |
14b1c01e AM |
9077 | if (buf == NULL) |
9078 | return buf; | |
a39f3346 | 9079 | dest = buf + *bufsiz; |
7c76fa91 MS |
9080 | *bufsiz += newspace; |
9081 | xnp = (Elf_External_Note *) dest; | |
9082 | H_PUT_32 (abfd, namesz, xnp->namesz); | |
9083 | H_PUT_32 (abfd, size, xnp->descsz); | |
9084 | H_PUT_32 (abfd, type, xnp->type); | |
d4c88bbb AM |
9085 | dest = xnp->name; |
9086 | if (name != NULL) | |
9087 | { | |
9088 | memcpy (dest, name, namesz); | |
9089 | dest += namesz; | |
a39f3346 | 9090 | while (namesz & 3) |
d4c88bbb AM |
9091 | { |
9092 | *dest++ = '\0'; | |
a39f3346 | 9093 | ++namesz; |
d4c88bbb AM |
9094 | } |
9095 | } | |
9096 | memcpy (dest, input, size); | |
a39f3346 AM |
9097 | dest += size; |
9098 | while (size & 3) | |
9099 | { | |
9100 | *dest++ = '\0'; | |
9101 | ++size; | |
9102 | } | |
9103 | return buf; | |
7c76fa91 MS |
9104 | } |
9105 | ||
7c76fa91 | 9106 | char * |
217aa764 AM |
9107 | elfcore_write_prpsinfo (bfd *abfd, |
9108 | char *buf, | |
9109 | int *bufsiz, | |
9110 | const char *fname, | |
9111 | const char *psargs) | |
7c76fa91 | 9112 | { |
183e98be AM |
9113 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
9114 | ||
9115 | if (bed->elf_backend_write_core_note != NULL) | |
9116 | { | |
9117 | char *ret; | |
9118 | ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz, | |
9119 | NT_PRPSINFO, fname, psargs); | |
9120 | if (ret != NULL) | |
9121 | return ret; | |
9122 | } | |
7c76fa91 | 9123 | |
1f20dca5 | 9124 | #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) |
183e98be AM |
9125 | #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T) |
9126 | if (bed->s->elfclass == ELFCLASS32) | |
9127 | { | |
9128 | #if defined (HAVE_PSINFO32_T) | |
9129 | psinfo32_t data; | |
9130 | int note_type = NT_PSINFO; | |
9131 | #else | |
9132 | prpsinfo32_t data; | |
9133 | int note_type = NT_PRPSINFO; | |
9134 | #endif | |
9135 | ||
9136 | memset (&data, 0, sizeof (data)); | |
9137 | strncpy (data.pr_fname, fname, sizeof (data.pr_fname)); | |
9138 | strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs)); | |
9139 | return elfcore_write_note (abfd, buf, bufsiz, | |
1f20dca5 | 9140 | "CORE", note_type, &data, sizeof (data)); |
183e98be AM |
9141 | } |
9142 | else | |
9143 | #endif | |
9144 | { | |
7c76fa91 | 9145 | #if defined (HAVE_PSINFO_T) |
183e98be AM |
9146 | psinfo_t data; |
9147 | int note_type = NT_PSINFO; | |
7c76fa91 | 9148 | #else |
183e98be AM |
9149 | prpsinfo_t data; |
9150 | int note_type = NT_PRPSINFO; | |
7c76fa91 MS |
9151 | #endif |
9152 | ||
183e98be AM |
9153 | memset (&data, 0, sizeof (data)); |
9154 | strncpy (data.pr_fname, fname, sizeof (data.pr_fname)); | |
9155 | strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs)); | |
9156 | return elfcore_write_note (abfd, buf, bufsiz, | |
1f20dca5 | 9157 | "CORE", note_type, &data, sizeof (data)); |
183e98be | 9158 | } |
7c76fa91 MS |
9159 | #endif /* PSINFO_T or PRPSINFO_T */ |
9160 | ||
1f20dca5 UW |
9161 | free (buf); |
9162 | return NULL; | |
9163 | } | |
9164 | ||
70a38d42 SDJ |
9165 | char * |
9166 | elfcore_write_linux_prpsinfo32 | |
9167 | (bfd *abfd, char *buf, int *bufsiz, | |
9168 | const struct elf_internal_linux_prpsinfo *prpsinfo) | |
9169 | { | |
9170 | struct elf_external_linux_prpsinfo32 data; | |
9171 | ||
9172 | memset (&data, 0, sizeof (data)); | |
9173 | LINUX_PRPSINFO32_SWAP_FIELDS (abfd, prpsinfo, data); | |
9174 | ||
9175 | return elfcore_write_note (abfd, buf, bufsiz, "CORE", NT_PRPSINFO, | |
9176 | &data, sizeof (data)); | |
9177 | } | |
9178 | ||
9179 | char * | |
9180 | elfcore_write_linux_prpsinfo64 | |
9181 | (bfd *abfd, char *buf, int *bufsiz, | |
9182 | const struct elf_internal_linux_prpsinfo *prpsinfo) | |
9183 | { | |
9184 | struct elf_external_linux_prpsinfo64 data; | |
9185 | ||
9186 | memset (&data, 0, sizeof (data)); | |
9187 | LINUX_PRPSINFO64_SWAP_FIELDS (abfd, prpsinfo, data); | |
9188 | ||
9189 | return elfcore_write_note (abfd, buf, bufsiz, | |
9190 | "CORE", NT_PRPSINFO, &data, sizeof (data)); | |
9191 | } | |
9192 | ||
7c76fa91 | 9193 | char * |
217aa764 AM |
9194 | elfcore_write_prstatus (bfd *abfd, |
9195 | char *buf, | |
9196 | int *bufsiz, | |
9197 | long pid, | |
9198 | int cursig, | |
9199 | const void *gregs) | |
7c76fa91 | 9200 | { |
183e98be | 9201 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
7c76fa91 | 9202 | |
183e98be AM |
9203 | if (bed->elf_backend_write_core_note != NULL) |
9204 | { | |
9205 | char *ret; | |
9206 | ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz, | |
9207 | NT_PRSTATUS, | |
9208 | pid, cursig, gregs); | |
9209 | if (ret != NULL) | |
9210 | return ret; | |
9211 | } | |
9212 | ||
1f20dca5 | 9213 | #if defined (HAVE_PRSTATUS_T) |
183e98be AM |
9214 | #if defined (HAVE_PRSTATUS32_T) |
9215 | if (bed->s->elfclass == ELFCLASS32) | |
9216 | { | |
9217 | prstatus32_t prstat; | |
9218 | ||
9219 | memset (&prstat, 0, sizeof (prstat)); | |
9220 | prstat.pr_pid = pid; | |
9221 | prstat.pr_cursig = cursig; | |
9222 | memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg)); | |
1f20dca5 | 9223 | return elfcore_write_note (abfd, buf, bufsiz, "CORE", |
183e98be AM |
9224 | NT_PRSTATUS, &prstat, sizeof (prstat)); |
9225 | } | |
9226 | else | |
9227 | #endif | |
9228 | { | |
9229 | prstatus_t prstat; | |
9230 | ||
9231 | memset (&prstat, 0, sizeof (prstat)); | |
9232 | prstat.pr_pid = pid; | |
9233 | prstat.pr_cursig = cursig; | |
9234 | memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg)); | |
1f20dca5 | 9235 | return elfcore_write_note (abfd, buf, bufsiz, "CORE", |
183e98be AM |
9236 | NT_PRSTATUS, &prstat, sizeof (prstat)); |
9237 | } | |
7c76fa91 MS |
9238 | #endif /* HAVE_PRSTATUS_T */ |
9239 | ||
1f20dca5 UW |
9240 | free (buf); |
9241 | return NULL; | |
9242 | } | |
9243 | ||
51316059 MS |
9244 | #if defined (HAVE_LWPSTATUS_T) |
9245 | char * | |
217aa764 AM |
9246 | elfcore_write_lwpstatus (bfd *abfd, |
9247 | char *buf, | |
9248 | int *bufsiz, | |
9249 | long pid, | |
9250 | int cursig, | |
9251 | const void *gregs) | |
51316059 MS |
9252 | { |
9253 | lwpstatus_t lwpstat; | |
183e98be | 9254 | const char *note_name = "CORE"; |
51316059 MS |
9255 | |
9256 | memset (&lwpstat, 0, sizeof (lwpstat)); | |
9257 | lwpstat.pr_lwpid = pid >> 16; | |
9258 | lwpstat.pr_cursig = cursig; | |
9259 | #if defined (HAVE_LWPSTATUS_T_PR_REG) | |
9260 | memcpy (lwpstat.pr_reg, gregs, sizeof (lwpstat.pr_reg)); | |
9261 | #elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT) | |
9262 | #if !defined(gregs) | |
9263 | memcpy (lwpstat.pr_context.uc_mcontext.gregs, | |
9264 | gregs, sizeof (lwpstat.pr_context.uc_mcontext.gregs)); | |
9265 | #else | |
9266 | memcpy (lwpstat.pr_context.uc_mcontext.__gregs, | |
9267 | gregs, sizeof (lwpstat.pr_context.uc_mcontext.__gregs)); | |
9268 | #endif | |
9269 | #endif | |
47d9a591 | 9270 | return elfcore_write_note (abfd, buf, bufsiz, note_name, |
51316059 MS |
9271 | NT_LWPSTATUS, &lwpstat, sizeof (lwpstat)); |
9272 | } | |
9273 | #endif /* HAVE_LWPSTATUS_T */ | |
9274 | ||
7c76fa91 MS |
9275 | #if defined (HAVE_PSTATUS_T) |
9276 | char * | |
217aa764 AM |
9277 | elfcore_write_pstatus (bfd *abfd, |
9278 | char *buf, | |
9279 | int *bufsiz, | |
9280 | long pid, | |
6c10990d NC |
9281 | int cursig ATTRIBUTE_UNUSED, |
9282 | const void *gregs ATTRIBUTE_UNUSED) | |
7c76fa91 | 9283 | { |
183e98be AM |
9284 | const char *note_name = "CORE"; |
9285 | #if defined (HAVE_PSTATUS32_T) | |
9286 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
7c76fa91 | 9287 | |
183e98be AM |
9288 | if (bed->s->elfclass == ELFCLASS32) |
9289 | { | |
9290 | pstatus32_t pstat; | |
9291 | ||
9292 | memset (&pstat, 0, sizeof (pstat)); | |
9293 | pstat.pr_pid = pid & 0xffff; | |
9294 | buf = elfcore_write_note (abfd, buf, bufsiz, note_name, | |
9295 | NT_PSTATUS, &pstat, sizeof (pstat)); | |
9296 | return buf; | |
9297 | } | |
9298 | else | |
9299 | #endif | |
9300 | { | |
9301 | pstatus_t pstat; | |
9302 | ||
9303 | memset (&pstat, 0, sizeof (pstat)); | |
9304 | pstat.pr_pid = pid & 0xffff; | |
9305 | buf = elfcore_write_note (abfd, buf, bufsiz, note_name, | |
9306 | NT_PSTATUS, &pstat, sizeof (pstat)); | |
9307 | return buf; | |
9308 | } | |
7c76fa91 MS |
9309 | } |
9310 | #endif /* HAVE_PSTATUS_T */ | |
9311 | ||
9312 | char * | |
217aa764 AM |
9313 | elfcore_write_prfpreg (bfd *abfd, |
9314 | char *buf, | |
9315 | int *bufsiz, | |
9316 | const void *fpregs, | |
9317 | int size) | |
7c76fa91 | 9318 | { |
183e98be | 9319 | const char *note_name = "CORE"; |
47d9a591 | 9320 | return elfcore_write_note (abfd, buf, bufsiz, |
7c76fa91 MS |
9321 | note_name, NT_FPREGSET, fpregs, size); |
9322 | } | |
9323 | ||
9324 | char * | |
217aa764 AM |
9325 | elfcore_write_prxfpreg (bfd *abfd, |
9326 | char *buf, | |
9327 | int *bufsiz, | |
9328 | const void *xfpregs, | |
9329 | int size) | |
7c76fa91 MS |
9330 | { |
9331 | char *note_name = "LINUX"; | |
47d9a591 | 9332 | return elfcore_write_note (abfd, buf, bufsiz, |
7c76fa91 MS |
9333 | note_name, NT_PRXFPREG, xfpregs, size); |
9334 | } | |
9335 | ||
4339cae0 L |
9336 | char * |
9337 | elfcore_write_xstatereg (bfd *abfd, char *buf, int *bufsiz, | |
9338 | const void *xfpregs, int size) | |
9339 | { | |
9340 | char *note_name = "LINUX"; | |
9341 | return elfcore_write_note (abfd, buf, bufsiz, | |
9342 | note_name, NT_X86_XSTATE, xfpregs, size); | |
9343 | } | |
9344 | ||
97753bd5 AM |
9345 | char * |
9346 | elfcore_write_ppc_vmx (bfd *abfd, | |
9347 | char *buf, | |
9348 | int *bufsiz, | |
9349 | const void *ppc_vmx, | |
9350 | int size) | |
9351 | { | |
9352 | char *note_name = "LINUX"; | |
9353 | return elfcore_write_note (abfd, buf, bufsiz, | |
9354 | note_name, NT_PPC_VMX, ppc_vmx, size); | |
9355 | } | |
9356 | ||
89eeb0bc LM |
9357 | char * |
9358 | elfcore_write_ppc_vsx (bfd *abfd, | |
9359 | char *buf, | |
9360 | int *bufsiz, | |
9361 | const void *ppc_vsx, | |
9362 | int size) | |
9363 | { | |
9364 | char *note_name = "LINUX"; | |
9365 | return elfcore_write_note (abfd, buf, bufsiz, | |
9366 | note_name, NT_PPC_VSX, ppc_vsx, size); | |
9367 | } | |
9368 | ||
0675e188 UW |
9369 | static char * |
9370 | elfcore_write_s390_high_gprs (bfd *abfd, | |
9371 | char *buf, | |
9372 | int *bufsiz, | |
9373 | const void *s390_high_gprs, | |
9374 | int size) | |
9375 | { | |
9376 | char *note_name = "LINUX"; | |
9377 | return elfcore_write_note (abfd, buf, bufsiz, | |
9378 | note_name, NT_S390_HIGH_GPRS, | |
9379 | s390_high_gprs, size); | |
9380 | } | |
9381 | ||
d7eeb400 MS |
9382 | char * |
9383 | elfcore_write_s390_timer (bfd *abfd, | |
9384 | char *buf, | |
9385 | int *bufsiz, | |
9386 | const void *s390_timer, | |
9387 | int size) | |
9388 | { | |
9389 | char *note_name = "LINUX"; | |
9390 | return elfcore_write_note (abfd, buf, bufsiz, | |
9391 | note_name, NT_S390_TIMER, s390_timer, size); | |
9392 | } | |
9393 | ||
9394 | char * | |
9395 | elfcore_write_s390_todcmp (bfd *abfd, | |
9396 | char *buf, | |
9397 | int *bufsiz, | |
9398 | const void *s390_todcmp, | |
9399 | int size) | |
9400 | { | |
9401 | char *note_name = "LINUX"; | |
9402 | return elfcore_write_note (abfd, buf, bufsiz, | |
9403 | note_name, NT_S390_TODCMP, s390_todcmp, size); | |
9404 | } | |
9405 | ||
9406 | char * | |
9407 | elfcore_write_s390_todpreg (bfd *abfd, | |
9408 | char *buf, | |
9409 | int *bufsiz, | |
9410 | const void *s390_todpreg, | |
9411 | int size) | |
9412 | { | |
9413 | char *note_name = "LINUX"; | |
9414 | return elfcore_write_note (abfd, buf, bufsiz, | |
9415 | note_name, NT_S390_TODPREG, s390_todpreg, size); | |
9416 | } | |
9417 | ||
9418 | char * | |
9419 | elfcore_write_s390_ctrs (bfd *abfd, | |
9420 | char *buf, | |
9421 | int *bufsiz, | |
9422 | const void *s390_ctrs, | |
9423 | int size) | |
9424 | { | |
9425 | char *note_name = "LINUX"; | |
9426 | return elfcore_write_note (abfd, buf, bufsiz, | |
9427 | note_name, NT_S390_CTRS, s390_ctrs, size); | |
9428 | } | |
9429 | ||
9430 | char * | |
9431 | elfcore_write_s390_prefix (bfd *abfd, | |
9432 | char *buf, | |
9433 | int *bufsiz, | |
9434 | const void *s390_prefix, | |
9435 | int size) | |
9436 | { | |
9437 | char *note_name = "LINUX"; | |
9438 | return elfcore_write_note (abfd, buf, bufsiz, | |
9439 | note_name, NT_S390_PREFIX, s390_prefix, size); | |
9440 | } | |
9441 | ||
355b81d9 UW |
9442 | char * |
9443 | elfcore_write_s390_last_break (bfd *abfd, | |
9444 | char *buf, | |
9445 | int *bufsiz, | |
9446 | const void *s390_last_break, | |
9447 | int size) | |
9448 | { | |
9449 | char *note_name = "LINUX"; | |
9450 | return elfcore_write_note (abfd, buf, bufsiz, | |
9451 | note_name, NT_S390_LAST_BREAK, | |
9452 | s390_last_break, size); | |
9453 | } | |
9454 | ||
9455 | char * | |
9456 | elfcore_write_s390_system_call (bfd *abfd, | |
9457 | char *buf, | |
9458 | int *bufsiz, | |
9459 | const void *s390_system_call, | |
9460 | int size) | |
9461 | { | |
9462 | char *note_name = "LINUX"; | |
9463 | return elfcore_write_note (abfd, buf, bufsiz, | |
9464 | note_name, NT_S390_SYSTEM_CALL, | |
9465 | s390_system_call, size); | |
9466 | } | |
9467 | ||
faa9a424 UW |
9468 | char * |
9469 | elfcore_write_arm_vfp (bfd *abfd, | |
9470 | char *buf, | |
9471 | int *bufsiz, | |
9472 | const void *arm_vfp, | |
9473 | int size) | |
9474 | { | |
9475 | char *note_name = "LINUX"; | |
9476 | return elfcore_write_note (abfd, buf, bufsiz, | |
9477 | note_name, NT_ARM_VFP, arm_vfp, size); | |
9478 | } | |
9479 | ||
652451f8 YZ |
9480 | char * |
9481 | elfcore_write_aarch_tls (bfd *abfd, | |
9482 | char *buf, | |
9483 | int *bufsiz, | |
9484 | const void *aarch_tls, | |
9485 | int size) | |
9486 | { | |
9487 | char *note_name = "LINUX"; | |
9488 | return elfcore_write_note (abfd, buf, bufsiz, | |
9489 | note_name, NT_ARM_TLS, aarch_tls, size); | |
9490 | } | |
9491 | ||
9492 | char * | |
9493 | elfcore_write_aarch_hw_break (bfd *abfd, | |
9494 | char *buf, | |
9495 | int *bufsiz, | |
9496 | const void *aarch_hw_break, | |
9497 | int size) | |
9498 | { | |
9499 | char *note_name = "LINUX"; | |
9500 | return elfcore_write_note (abfd, buf, bufsiz, | |
9501 | note_name, NT_ARM_HW_BREAK, aarch_hw_break, size); | |
9502 | } | |
9503 | ||
9504 | char * | |
9505 | elfcore_write_aarch_hw_watch (bfd *abfd, | |
9506 | char *buf, | |
9507 | int *bufsiz, | |
9508 | const void *aarch_hw_watch, | |
9509 | int size) | |
9510 | { | |
9511 | char *note_name = "LINUX"; | |
9512 | return elfcore_write_note (abfd, buf, bufsiz, | |
9513 | note_name, NT_ARM_HW_WATCH, aarch_hw_watch, size); | |
9514 | } | |
9515 | ||
bb864ac1 CES |
9516 | char * |
9517 | elfcore_write_register_note (bfd *abfd, | |
9518 | char *buf, | |
9519 | int *bufsiz, | |
9520 | const char *section, | |
9521 | const void *data, | |
9522 | int size) | |
9523 | { | |
9524 | if (strcmp (section, ".reg2") == 0) | |
9525 | return elfcore_write_prfpreg (abfd, buf, bufsiz, data, size); | |
9526 | if (strcmp (section, ".reg-xfp") == 0) | |
9527 | return elfcore_write_prxfpreg (abfd, buf, bufsiz, data, size); | |
4339cae0 L |
9528 | if (strcmp (section, ".reg-xstate") == 0) |
9529 | return elfcore_write_xstatereg (abfd, buf, bufsiz, data, size); | |
bb864ac1 CES |
9530 | if (strcmp (section, ".reg-ppc-vmx") == 0) |
9531 | return elfcore_write_ppc_vmx (abfd, buf, bufsiz, data, size); | |
89eeb0bc LM |
9532 | if (strcmp (section, ".reg-ppc-vsx") == 0) |
9533 | return elfcore_write_ppc_vsx (abfd, buf, bufsiz, data, size); | |
0675e188 UW |
9534 | if (strcmp (section, ".reg-s390-high-gprs") == 0) |
9535 | return elfcore_write_s390_high_gprs (abfd, buf, bufsiz, data, size); | |
d7eeb400 MS |
9536 | if (strcmp (section, ".reg-s390-timer") == 0) |
9537 | return elfcore_write_s390_timer (abfd, buf, bufsiz, data, size); | |
9538 | if (strcmp (section, ".reg-s390-todcmp") == 0) | |
9539 | return elfcore_write_s390_todcmp (abfd, buf, bufsiz, data, size); | |
9540 | if (strcmp (section, ".reg-s390-todpreg") == 0) | |
9541 | return elfcore_write_s390_todpreg (abfd, buf, bufsiz, data, size); | |
9542 | if (strcmp (section, ".reg-s390-ctrs") == 0) | |
9543 | return elfcore_write_s390_ctrs (abfd, buf, bufsiz, data, size); | |
9544 | if (strcmp (section, ".reg-s390-prefix") == 0) | |
9545 | return elfcore_write_s390_prefix (abfd, buf, bufsiz, data, size); | |
355b81d9 UW |
9546 | if (strcmp (section, ".reg-s390-last-break") == 0) |
9547 | return elfcore_write_s390_last_break (abfd, buf, bufsiz, data, size); | |
9548 | if (strcmp (section, ".reg-s390-system-call") == 0) | |
9549 | return elfcore_write_s390_system_call (abfd, buf, bufsiz, data, size); | |
faa9a424 UW |
9550 | if (strcmp (section, ".reg-arm-vfp") == 0) |
9551 | return elfcore_write_arm_vfp (abfd, buf, bufsiz, data, size); | |
652451f8 YZ |
9552 | if (strcmp (section, ".reg-aarch-tls") == 0) |
9553 | return elfcore_write_aarch_tls (abfd, buf, bufsiz, data, size); | |
9554 | if (strcmp (section, ".reg-aarch-hw-break") == 0) | |
9555 | return elfcore_write_aarch_hw_break (abfd, buf, bufsiz, data, size); | |
9556 | if (strcmp (section, ".reg-aarch-hw-watch") == 0) | |
9557 | return elfcore_write_aarch_hw_watch (abfd, buf, bufsiz, data, size); | |
bb864ac1 CES |
9558 | return NULL; |
9559 | } | |
9560 | ||
b34976b6 | 9561 | static bfd_boolean |
718175fa | 9562 | elf_parse_notes (bfd *abfd, char *buf, size_t size, file_ptr offset) |
252b5132 | 9563 | { |
c044fabd | 9564 | char *p; |
252b5132 | 9565 | |
252b5132 RH |
9566 | p = buf; |
9567 | while (p < buf + size) | |
9568 | { | |
c044fabd KH |
9569 | /* FIXME: bad alignment assumption. */ |
9570 | Elf_External_Note *xnp = (Elf_External_Note *) p; | |
252b5132 RH |
9571 | Elf_Internal_Note in; |
9572 | ||
baea7ef1 AM |
9573 | if (offsetof (Elf_External_Note, name) > buf - p + size) |
9574 | return FALSE; | |
9575 | ||
dc810e39 | 9576 | in.type = H_GET_32 (abfd, xnp->type); |
252b5132 | 9577 | |
dc810e39 | 9578 | in.namesz = H_GET_32 (abfd, xnp->namesz); |
252b5132 | 9579 | in.namedata = xnp->name; |
baea7ef1 AM |
9580 | if (in.namesz > buf - in.namedata + size) |
9581 | return FALSE; | |
252b5132 | 9582 | |
dc810e39 | 9583 | in.descsz = H_GET_32 (abfd, xnp->descsz); |
252b5132 RH |
9584 | in.descdata = in.namedata + BFD_ALIGN (in.namesz, 4); |
9585 | in.descpos = offset + (in.descdata - buf); | |
baea7ef1 AM |
9586 | if (in.descsz != 0 |
9587 | && (in.descdata >= buf + size | |
9588 | || in.descsz > buf - in.descdata + size)) | |
9589 | return FALSE; | |
252b5132 | 9590 | |
718175fa JK |
9591 | switch (bfd_get_format (abfd)) |
9592 | { | |
9593 | default: | |
9594 | return TRUE; | |
9595 | ||
9596 | case bfd_core: | |
9597 | if (CONST_STRNEQ (in.namedata, "NetBSD-CORE")) | |
9598 | { | |
9599 | if (! elfcore_grok_netbsd_note (abfd, &in)) | |
9600 | return FALSE; | |
9601 | } | |
67cc5033 MK |
9602 | else if (CONST_STRNEQ (in.namedata, "OpenBSD")) |
9603 | { | |
9604 | if (! elfcore_grok_openbsd_note (abfd, &in)) | |
9605 | return FALSE; | |
9606 | } | |
718175fa JK |
9607 | else if (CONST_STRNEQ (in.namedata, "QNX")) |
9608 | { | |
9609 | if (! elfcore_grok_nto_note (abfd, &in)) | |
9610 | return FALSE; | |
9611 | } | |
b15fa79e AM |
9612 | else if (CONST_STRNEQ (in.namedata, "SPU/")) |
9613 | { | |
9614 | if (! elfcore_grok_spu_note (abfd, &in)) | |
9615 | return FALSE; | |
9616 | } | |
718175fa JK |
9617 | else |
9618 | { | |
9619 | if (! elfcore_grok_note (abfd, &in)) | |
9620 | return FALSE; | |
9621 | } | |
9622 | break; | |
9623 | ||
9624 | case bfd_object: | |
9625 | if (in.namesz == sizeof "GNU" && strcmp (in.namedata, "GNU") == 0) | |
9626 | { | |
9627 | if (! elfobj_grok_gnu_note (abfd, &in)) | |
9628 | return FALSE; | |
9629 | } | |
e21e5835 NC |
9630 | else if (in.namesz == sizeof "stapsdt" |
9631 | && strcmp (in.namedata, "stapsdt") == 0) | |
9632 | { | |
9633 | if (! elfobj_grok_stapsdt_note (abfd, &in)) | |
9634 | return FALSE; | |
9635 | } | |
718175fa | 9636 | break; |
08a40648 | 9637 | } |
252b5132 RH |
9638 | |
9639 | p = in.descdata + BFD_ALIGN (in.descsz, 4); | |
9640 | } | |
9641 | ||
718175fa JK |
9642 | return TRUE; |
9643 | } | |
9644 | ||
9645 | static bfd_boolean | |
9646 | elf_read_notes (bfd *abfd, file_ptr offset, bfd_size_type size) | |
9647 | { | |
9648 | char *buf; | |
9649 | ||
9650 | if (size <= 0) | |
9651 | return TRUE; | |
9652 | ||
9653 | if (bfd_seek (abfd, offset, SEEK_SET) != 0) | |
9654 | return FALSE; | |
9655 | ||
a50b1753 | 9656 | buf = (char *) bfd_malloc (size); |
718175fa JK |
9657 | if (buf == NULL) |
9658 | return FALSE; | |
9659 | ||
9660 | if (bfd_bread (buf, size, abfd) != size | |
9661 | || !elf_parse_notes (abfd, buf, size, offset)) | |
9662 | { | |
9663 | free (buf); | |
9664 | return FALSE; | |
9665 | } | |
9666 | ||
252b5132 | 9667 | free (buf); |
b34976b6 | 9668 | return TRUE; |
252b5132 | 9669 | } |
98d8431c JB |
9670 | \f |
9671 | /* Providing external access to the ELF program header table. */ | |
9672 | ||
9673 | /* Return an upper bound on the number of bytes required to store a | |
9674 | copy of ABFD's program header table entries. Return -1 if an error | |
9675 | occurs; bfd_get_error will return an appropriate code. */ | |
c044fabd | 9676 | |
98d8431c | 9677 | long |
217aa764 | 9678 | bfd_get_elf_phdr_upper_bound (bfd *abfd) |
98d8431c JB |
9679 | { |
9680 | if (abfd->xvec->flavour != bfd_target_elf_flavour) | |
9681 | { | |
9682 | bfd_set_error (bfd_error_wrong_format); | |
9683 | return -1; | |
9684 | } | |
9685 | ||
936e320b | 9686 | return elf_elfheader (abfd)->e_phnum * sizeof (Elf_Internal_Phdr); |
98d8431c JB |
9687 | } |
9688 | ||
98d8431c JB |
9689 | /* Copy ABFD's program header table entries to *PHDRS. The entries |
9690 | will be stored as an array of Elf_Internal_Phdr structures, as | |
9691 | defined in include/elf/internal.h. To find out how large the | |
9692 | buffer needs to be, call bfd_get_elf_phdr_upper_bound. | |
9693 | ||
9694 | Return the number of program header table entries read, or -1 if an | |
9695 | error occurs; bfd_get_error will return an appropriate code. */ | |
c044fabd | 9696 | |
98d8431c | 9697 | int |
217aa764 | 9698 | bfd_get_elf_phdrs (bfd *abfd, void *phdrs) |
98d8431c JB |
9699 | { |
9700 | int num_phdrs; | |
9701 | ||
9702 | if (abfd->xvec->flavour != bfd_target_elf_flavour) | |
9703 | { | |
9704 | bfd_set_error (bfd_error_wrong_format); | |
9705 | return -1; | |
9706 | } | |
9707 | ||
9708 | num_phdrs = elf_elfheader (abfd)->e_phnum; | |
c044fabd | 9709 | memcpy (phdrs, elf_tdata (abfd)->phdr, |
98d8431c JB |
9710 | num_phdrs * sizeof (Elf_Internal_Phdr)); |
9711 | ||
9712 | return num_phdrs; | |
9713 | } | |
ae4221d7 | 9714 | |
db6751f2 | 9715 | enum elf_reloc_type_class |
217aa764 | 9716 | _bfd_elf_reloc_type_class (const Elf_Internal_Rela *rela ATTRIBUTE_UNUSED) |
db6751f2 JJ |
9717 | { |
9718 | return reloc_class_normal; | |
9719 | } | |
f8df10f4 | 9720 | |
47d9a591 | 9721 | /* For RELA architectures, return the relocation value for a |
f8df10f4 JJ |
9722 | relocation against a local symbol. */ |
9723 | ||
9724 | bfd_vma | |
217aa764 AM |
9725 | _bfd_elf_rela_local_sym (bfd *abfd, |
9726 | Elf_Internal_Sym *sym, | |
8517fae7 | 9727 | asection **psec, |
217aa764 | 9728 | Elf_Internal_Rela *rel) |
f8df10f4 | 9729 | { |
8517fae7 | 9730 | asection *sec = *psec; |
f8df10f4 JJ |
9731 | bfd_vma relocation; |
9732 | ||
9733 | relocation = (sec->output_section->vma | |
9734 | + sec->output_offset | |
9735 | + sym->st_value); | |
9736 | if ((sec->flags & SEC_MERGE) | |
c629eae0 | 9737 | && ELF_ST_TYPE (sym->st_info) == STT_SECTION |
dbaa2011 | 9738 | && sec->sec_info_type == SEC_INFO_TYPE_MERGE) |
f8df10f4 | 9739 | { |
f8df10f4 | 9740 | rel->r_addend = |
8517fae7 | 9741 | _bfd_merged_section_offset (abfd, psec, |
65765700 | 9742 | elf_section_data (sec)->sec_info, |
753731ee AM |
9743 | sym->st_value + rel->r_addend); |
9744 | if (sec != *psec) | |
9745 | { | |
9746 | /* If we have changed the section, and our original section is | |
9747 | marked with SEC_EXCLUDE, it means that the original | |
9748 | SEC_MERGE section has been completely subsumed in some | |
9749 | other SEC_MERGE section. In this case, we need to leave | |
9750 | some info around for --emit-relocs. */ | |
9751 | if ((sec->flags & SEC_EXCLUDE) != 0) | |
9752 | sec->kept_section = *psec; | |
9753 | sec = *psec; | |
9754 | } | |
8517fae7 AM |
9755 | rel->r_addend -= relocation; |
9756 | rel->r_addend += sec->output_section->vma + sec->output_offset; | |
f8df10f4 JJ |
9757 | } |
9758 | return relocation; | |
9759 | } | |
c629eae0 JJ |
9760 | |
9761 | bfd_vma | |
217aa764 AM |
9762 | _bfd_elf_rel_local_sym (bfd *abfd, |
9763 | Elf_Internal_Sym *sym, | |
9764 | asection **psec, | |
9765 | bfd_vma addend) | |
47d9a591 | 9766 | { |
c629eae0 JJ |
9767 | asection *sec = *psec; |
9768 | ||
dbaa2011 | 9769 | if (sec->sec_info_type != SEC_INFO_TYPE_MERGE) |
c629eae0 JJ |
9770 | return sym->st_value + addend; |
9771 | ||
9772 | return _bfd_merged_section_offset (abfd, psec, | |
65765700 | 9773 | elf_section_data (sec)->sec_info, |
753731ee | 9774 | sym->st_value + addend); |
c629eae0 JJ |
9775 | } |
9776 | ||
9777 | bfd_vma | |
217aa764 | 9778 | _bfd_elf_section_offset (bfd *abfd, |
92e4ec35 | 9779 | struct bfd_link_info *info, |
217aa764 AM |
9780 | asection *sec, |
9781 | bfd_vma offset) | |
c629eae0 | 9782 | { |
68bfbfcc | 9783 | switch (sec->sec_info_type) |
65765700 | 9784 | { |
dbaa2011 | 9785 | case SEC_INFO_TYPE_STABS: |
eea6121a AM |
9786 | return _bfd_stab_section_offset (sec, elf_section_data (sec)->sec_info, |
9787 | offset); | |
dbaa2011 | 9788 | case SEC_INFO_TYPE_EH_FRAME: |
92e4ec35 | 9789 | return _bfd_elf_eh_frame_section_offset (abfd, info, sec, offset); |
65765700 | 9790 | default: |
310fd250 L |
9791 | if ((sec->flags & SEC_ELF_REVERSE_COPY) != 0) |
9792 | { | |
9793 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
9794 | bfd_size_type address_size = bed->s->arch_size / 8; | |
9795 | offset = sec->size - offset - address_size; | |
9796 | } | |
65765700 JJ |
9797 | return offset; |
9798 | } | |
c629eae0 | 9799 | } |
3333a7c3 RM |
9800 | \f |
9801 | /* Create a new BFD as if by bfd_openr. Rather than opening a file, | |
9802 | reconstruct an ELF file by reading the segments out of remote memory | |
9803 | based on the ELF file header at EHDR_VMA and the ELF program headers it | |
9804 | points to. If not null, *LOADBASEP is filled in with the difference | |
9805 | between the VMAs from which the segments were read, and the VMAs the | |
9806 | file headers (and hence BFD's idea of each section's VMA) put them at. | |
9807 | ||
9808 | The function TARGET_READ_MEMORY is called to copy LEN bytes from the | |
9809 | remote memory at target address VMA into the local buffer at MYADDR; it | |
9810 | should return zero on success or an `errno' code on failure. TEMPL must | |
9811 | be a BFD for an ELF target with the word size and byte order found in | |
9812 | the remote memory. */ | |
9813 | ||
9814 | bfd * | |
217aa764 AM |
9815 | bfd_elf_bfd_from_remote_memory |
9816 | (bfd *templ, | |
9817 | bfd_vma ehdr_vma, | |
9818 | bfd_vma *loadbasep, | |
fe78531d | 9819 | int (*target_read_memory) (bfd_vma, bfd_byte *, bfd_size_type)) |
3333a7c3 RM |
9820 | { |
9821 | return (*get_elf_backend_data (templ)->elf_backend_bfd_from_remote_memory) | |
9822 | (templ, ehdr_vma, loadbasep, target_read_memory); | |
9823 | } | |
4c45e5c9 JJ |
9824 | \f |
9825 | long | |
c9727e01 AM |
9826 | _bfd_elf_get_synthetic_symtab (bfd *abfd, |
9827 | long symcount ATTRIBUTE_UNUSED, | |
9828 | asymbol **syms ATTRIBUTE_UNUSED, | |
8615f3f2 | 9829 | long dynsymcount, |
c9727e01 AM |
9830 | asymbol **dynsyms, |
9831 | asymbol **ret) | |
4c45e5c9 JJ |
9832 | { |
9833 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
9834 | asection *relplt; | |
9835 | asymbol *s; | |
9836 | const char *relplt_name; | |
9837 | bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean); | |
9838 | arelent *p; | |
9839 | long count, i, n; | |
9840 | size_t size; | |
9841 | Elf_Internal_Shdr *hdr; | |
9842 | char *names; | |
9843 | asection *plt; | |
9844 | ||
8615f3f2 AM |
9845 | *ret = NULL; |
9846 | ||
90e3cdf2 JJ |
9847 | if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0) |
9848 | return 0; | |
9849 | ||
8615f3f2 AM |
9850 | if (dynsymcount <= 0) |
9851 | return 0; | |
9852 | ||
4c45e5c9 JJ |
9853 | if (!bed->plt_sym_val) |
9854 | return 0; | |
9855 | ||
9856 | relplt_name = bed->relplt_name; | |
9857 | if (relplt_name == NULL) | |
d35fd659 | 9858 | relplt_name = bed->rela_plts_and_copies_p ? ".rela.plt" : ".rel.plt"; |
4c45e5c9 JJ |
9859 | relplt = bfd_get_section_by_name (abfd, relplt_name); |
9860 | if (relplt == NULL) | |
9861 | return 0; | |
9862 | ||
9863 | hdr = &elf_section_data (relplt)->this_hdr; | |
9864 | if (hdr->sh_link != elf_dynsymtab (abfd) | |
9865 | || (hdr->sh_type != SHT_REL && hdr->sh_type != SHT_RELA)) | |
9866 | return 0; | |
9867 | ||
9868 | plt = bfd_get_section_by_name (abfd, ".plt"); | |
9869 | if (plt == NULL) | |
9870 | return 0; | |
9871 | ||
9872 | slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table; | |
c9727e01 | 9873 | if (! (*slurp_relocs) (abfd, relplt, dynsyms, TRUE)) |
4c45e5c9 JJ |
9874 | return -1; |
9875 | ||
eea6121a | 9876 | count = relplt->size / hdr->sh_entsize; |
4c45e5c9 JJ |
9877 | size = count * sizeof (asymbol); |
9878 | p = relplt->relocation; | |
cb53bf42 | 9879 | for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel) |
041de40d AM |
9880 | { |
9881 | size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt"); | |
9882 | if (p->addend != 0) | |
9883 | { | |
9884 | #ifdef BFD64 | |
9885 | size += sizeof ("+0x") - 1 + 8 + 8 * (bed->s->elfclass == ELFCLASS64); | |
9886 | #else | |
9887 | size += sizeof ("+0x") - 1 + 8; | |
9888 | #endif | |
9889 | } | |
9890 | } | |
4c45e5c9 | 9891 | |
a50b1753 | 9892 | s = *ret = (asymbol *) bfd_malloc (size); |
4c45e5c9 JJ |
9893 | if (s == NULL) |
9894 | return -1; | |
9895 | ||
9896 | names = (char *) (s + count); | |
9897 | p = relplt->relocation; | |
9898 | n = 0; | |
cb53bf42 | 9899 | for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel) |
4c45e5c9 JJ |
9900 | { |
9901 | size_t len; | |
9902 | bfd_vma addr; | |
9903 | ||
9904 | addr = bed->plt_sym_val (i, plt, p); | |
9905 | if (addr == (bfd_vma) -1) | |
9906 | continue; | |
9907 | ||
9908 | *s = **p->sym_ptr_ptr; | |
65a7a66f AM |
9909 | /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since |
9910 | we are defining a symbol, ensure one of them is set. */ | |
9911 | if ((s->flags & BSF_LOCAL) == 0) | |
9912 | s->flags |= BSF_GLOBAL; | |
6ba2a415 | 9913 | s->flags |= BSF_SYNTHETIC; |
4c45e5c9 JJ |
9914 | s->section = plt; |
9915 | s->value = addr - plt->vma; | |
9916 | s->name = names; | |
8f39ba8e | 9917 | s->udata.p = NULL; |
4c45e5c9 JJ |
9918 | len = strlen ((*p->sym_ptr_ptr)->name); |
9919 | memcpy (names, (*p->sym_ptr_ptr)->name, len); | |
9920 | names += len; | |
041de40d AM |
9921 | if (p->addend != 0) |
9922 | { | |
1d770845 | 9923 | char buf[30], *a; |
d324f6d6 | 9924 | |
041de40d AM |
9925 | memcpy (names, "+0x", sizeof ("+0x") - 1); |
9926 | names += sizeof ("+0x") - 1; | |
1d770845 L |
9927 | bfd_sprintf_vma (abfd, buf, p->addend); |
9928 | for (a = buf; *a == '0'; ++a) | |
9929 | ; | |
9930 | len = strlen (a); | |
9931 | memcpy (names, a, len); | |
9932 | names += len; | |
041de40d | 9933 | } |
4c45e5c9 JJ |
9934 | memcpy (names, "@plt", sizeof ("@plt")); |
9935 | names += sizeof ("@plt"); | |
8f39ba8e | 9936 | ++s, ++n; |
4c45e5c9 JJ |
9937 | } |
9938 | ||
9939 | return n; | |
9940 | } | |
3d7f7666 | 9941 | |
3b22753a L |
9942 | /* It is only used by x86-64 so far. */ |
9943 | asection _bfd_elf_large_com_section | |
9944 | = BFD_FAKE_SECTION (_bfd_elf_large_com_section, | |
f592407e | 9945 | SEC_IS_COMMON, NULL, "LARGE_COMMON", 0); |
ecca9871 | 9946 | |
d1036acb L |
9947 | void |
9948 | _bfd_elf_set_osabi (bfd * abfd, | |
9949 | struct bfd_link_info * link_info ATTRIBUTE_UNUSED) | |
9950 | { | |
9951 | Elf_Internal_Ehdr * i_ehdrp; /* ELF file header, internal form. */ | |
9952 | ||
9953 | i_ehdrp = elf_elfheader (abfd); | |
9954 | ||
9955 | i_ehdrp->e_ident[EI_OSABI] = get_elf_backend_data (abfd)->elf_osabi; | |
d8045f23 NC |
9956 | |
9957 | /* To make things simpler for the loader on Linux systems we set the | |
9c55345c | 9958 | osabi field to ELFOSABI_GNU if the binary contains symbols of |
f64b2e8d | 9959 | the STT_GNU_IFUNC type or STB_GNU_UNIQUE binding. */ |
d8045f23 | 9960 | if (i_ehdrp->e_ident[EI_OSABI] == ELFOSABI_NONE |
f64b2e8d | 9961 | && elf_tdata (abfd)->has_gnu_symbols) |
9c55345c | 9962 | i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_GNU; |
d1036acb | 9963 | } |
fcb93ecf PB |
9964 | |
9965 | ||
9966 | /* Return TRUE for ELF symbol types that represent functions. | |
9967 | This is the default version of this function, which is sufficient for | |
d8045f23 | 9968 | most targets. It returns true if TYPE is STT_FUNC or STT_GNU_IFUNC. */ |
fcb93ecf PB |
9969 | |
9970 | bfd_boolean | |
9971 | _bfd_elf_is_function_type (unsigned int type) | |
9972 | { | |
d8045f23 NC |
9973 | return (type == STT_FUNC |
9974 | || type == STT_GNU_IFUNC); | |
fcb93ecf | 9975 | } |
9f296da3 | 9976 | |
aef36ac1 AM |
9977 | /* If the ELF symbol SYM might be a function in SEC, return the |
9978 | function size and set *CODE_OFF to the function's entry point, | |
9979 | otherwise return zero. */ | |
9f296da3 | 9980 | |
aef36ac1 AM |
9981 | bfd_size_type |
9982 | _bfd_elf_maybe_function_sym (const asymbol *sym, asection *sec, | |
9983 | bfd_vma *code_off) | |
9f296da3 | 9984 | { |
aef36ac1 AM |
9985 | bfd_size_type size; |
9986 | ||
ff9e0f5b | 9987 | if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT |
aef36ac1 AM |
9988 | | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0 |
9989 | || sym->section != sec) | |
9990 | return 0; | |
ff9e0f5b | 9991 | |
ff9e0f5b | 9992 | *code_off = sym->value; |
aef36ac1 AM |
9993 | size = 0; |
9994 | if (!(sym->flags & BSF_SYNTHETIC)) | |
9995 | size = ((elf_symbol_type *) sym)->internal_elf_sym.st_size; | |
9996 | if (size == 0) | |
9997 | size = 1; | |
9998 | return size; | |
9f296da3 | 9999 | } |