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