]>
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 | |
10 | the Free Software Foundation; either version 2 of the License, or | |
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 |
3e110533 | 20 | Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */ |
252b5132 | 21 | |
1b74d094 BW |
22 | /* |
23 | SECTION | |
252b5132 RH |
24 | ELF backends |
25 | ||
26 | BFD support for ELF formats is being worked on. | |
27 | Currently, the best supported back ends are for sparc and i386 | |
28 | (running svr4 or Solaris 2). | |
29 | ||
30 | Documentation of the internals of the support code still needs | |
31 | to be written. The code is changing quickly enough that we | |
661a3fd4 | 32 | haven't bothered yet. */ |
252b5132 | 33 | |
7ee38065 MS |
34 | /* For sparc64-cross-sparc32. */ |
35 | #define _SYSCALL32 | |
252b5132 | 36 | #include "sysdep.h" |
3db64b00 | 37 | #include "bfd.h" |
252b5132 RH |
38 | #include "bfdlink.h" |
39 | #include "libbfd.h" | |
40 | #define ARCH_SIZE 0 | |
41 | #include "elf-bfd.h" | |
e0e8c97f | 42 | #include "libiberty.h" |
252b5132 | 43 | |
217aa764 | 44 | static int elf_sort_sections (const void *, const void *); |
c84fca4d | 45 | static bfd_boolean assign_file_positions_except_relocs (bfd *, struct bfd_link_info *); |
217aa764 AM |
46 | static bfd_boolean prep_headers (bfd *); |
47 | static bfd_boolean swap_out_syms (bfd *, struct bfd_strtab_hash **, int) ; | |
48 | static bfd_boolean elfcore_read_notes (bfd *, file_ptr, bfd_size_type) ; | |
50b2bdb7 | 49 | |
252b5132 RH |
50 | /* Swap version information in and out. The version information is |
51 | currently size independent. If that ever changes, this code will | |
52 | need to move into elfcode.h. */ | |
53 | ||
54 | /* Swap in a Verdef structure. */ | |
55 | ||
56 | void | |
217aa764 AM |
57 | _bfd_elf_swap_verdef_in (bfd *abfd, |
58 | const Elf_External_Verdef *src, | |
59 | Elf_Internal_Verdef *dst) | |
252b5132 | 60 | { |
dc810e39 AM |
61 | dst->vd_version = H_GET_16 (abfd, src->vd_version); |
62 | dst->vd_flags = H_GET_16 (abfd, src->vd_flags); | |
63 | dst->vd_ndx = H_GET_16 (abfd, src->vd_ndx); | |
64 | dst->vd_cnt = H_GET_16 (abfd, src->vd_cnt); | |
65 | dst->vd_hash = H_GET_32 (abfd, src->vd_hash); | |
66 | dst->vd_aux = H_GET_32 (abfd, src->vd_aux); | |
67 | dst->vd_next = H_GET_32 (abfd, src->vd_next); | |
252b5132 RH |
68 | } |
69 | ||
70 | /* Swap out a Verdef structure. */ | |
71 | ||
72 | void | |
217aa764 AM |
73 | _bfd_elf_swap_verdef_out (bfd *abfd, |
74 | const Elf_Internal_Verdef *src, | |
75 | Elf_External_Verdef *dst) | |
252b5132 | 76 | { |
dc810e39 AM |
77 | H_PUT_16 (abfd, src->vd_version, dst->vd_version); |
78 | H_PUT_16 (abfd, src->vd_flags, dst->vd_flags); | |
79 | H_PUT_16 (abfd, src->vd_ndx, dst->vd_ndx); | |
80 | H_PUT_16 (abfd, src->vd_cnt, dst->vd_cnt); | |
81 | H_PUT_32 (abfd, src->vd_hash, dst->vd_hash); | |
82 | H_PUT_32 (abfd, src->vd_aux, dst->vd_aux); | |
83 | H_PUT_32 (abfd, src->vd_next, dst->vd_next); | |
252b5132 RH |
84 | } |
85 | ||
86 | /* Swap in a Verdaux structure. */ | |
87 | ||
88 | void | |
217aa764 AM |
89 | _bfd_elf_swap_verdaux_in (bfd *abfd, |
90 | const Elf_External_Verdaux *src, | |
91 | Elf_Internal_Verdaux *dst) | |
252b5132 | 92 | { |
dc810e39 AM |
93 | dst->vda_name = H_GET_32 (abfd, src->vda_name); |
94 | dst->vda_next = H_GET_32 (abfd, src->vda_next); | |
252b5132 RH |
95 | } |
96 | ||
97 | /* Swap out a Verdaux structure. */ | |
98 | ||
99 | void | |
217aa764 AM |
100 | _bfd_elf_swap_verdaux_out (bfd *abfd, |
101 | const Elf_Internal_Verdaux *src, | |
102 | Elf_External_Verdaux *dst) | |
252b5132 | 103 | { |
dc810e39 AM |
104 | H_PUT_32 (abfd, src->vda_name, dst->vda_name); |
105 | H_PUT_32 (abfd, src->vda_next, dst->vda_next); | |
252b5132 RH |
106 | } |
107 | ||
108 | /* Swap in a Verneed structure. */ | |
109 | ||
110 | void | |
217aa764 AM |
111 | _bfd_elf_swap_verneed_in (bfd *abfd, |
112 | const Elf_External_Verneed *src, | |
113 | Elf_Internal_Verneed *dst) | |
252b5132 | 114 | { |
dc810e39 AM |
115 | dst->vn_version = H_GET_16 (abfd, src->vn_version); |
116 | dst->vn_cnt = H_GET_16 (abfd, src->vn_cnt); | |
117 | dst->vn_file = H_GET_32 (abfd, src->vn_file); | |
118 | dst->vn_aux = H_GET_32 (abfd, src->vn_aux); | |
119 | dst->vn_next = H_GET_32 (abfd, src->vn_next); | |
252b5132 RH |
120 | } |
121 | ||
122 | /* Swap out a Verneed structure. */ | |
123 | ||
124 | void | |
217aa764 AM |
125 | _bfd_elf_swap_verneed_out (bfd *abfd, |
126 | const Elf_Internal_Verneed *src, | |
127 | Elf_External_Verneed *dst) | |
252b5132 | 128 | { |
dc810e39 AM |
129 | H_PUT_16 (abfd, src->vn_version, dst->vn_version); |
130 | H_PUT_16 (abfd, src->vn_cnt, dst->vn_cnt); | |
131 | H_PUT_32 (abfd, src->vn_file, dst->vn_file); | |
132 | H_PUT_32 (abfd, src->vn_aux, dst->vn_aux); | |
133 | H_PUT_32 (abfd, src->vn_next, dst->vn_next); | |
252b5132 RH |
134 | } |
135 | ||
136 | /* Swap in a Vernaux structure. */ | |
137 | ||
138 | void | |
217aa764 AM |
139 | _bfd_elf_swap_vernaux_in (bfd *abfd, |
140 | const Elf_External_Vernaux *src, | |
141 | Elf_Internal_Vernaux *dst) | |
252b5132 | 142 | { |
dc810e39 AM |
143 | dst->vna_hash = H_GET_32 (abfd, src->vna_hash); |
144 | dst->vna_flags = H_GET_16 (abfd, src->vna_flags); | |
145 | dst->vna_other = H_GET_16 (abfd, src->vna_other); | |
146 | dst->vna_name = H_GET_32 (abfd, src->vna_name); | |
147 | dst->vna_next = H_GET_32 (abfd, src->vna_next); | |
252b5132 RH |
148 | } |
149 | ||
150 | /* Swap out a Vernaux structure. */ | |
151 | ||
152 | void | |
217aa764 AM |
153 | _bfd_elf_swap_vernaux_out (bfd *abfd, |
154 | const Elf_Internal_Vernaux *src, | |
155 | Elf_External_Vernaux *dst) | |
252b5132 | 156 | { |
dc810e39 AM |
157 | H_PUT_32 (abfd, src->vna_hash, dst->vna_hash); |
158 | H_PUT_16 (abfd, src->vna_flags, dst->vna_flags); | |
159 | H_PUT_16 (abfd, src->vna_other, dst->vna_other); | |
160 | H_PUT_32 (abfd, src->vna_name, dst->vna_name); | |
161 | H_PUT_32 (abfd, src->vna_next, dst->vna_next); | |
252b5132 RH |
162 | } |
163 | ||
164 | /* Swap in a Versym structure. */ | |
165 | ||
166 | void | |
217aa764 AM |
167 | _bfd_elf_swap_versym_in (bfd *abfd, |
168 | const Elf_External_Versym *src, | |
169 | Elf_Internal_Versym *dst) | |
252b5132 | 170 | { |
dc810e39 | 171 | dst->vs_vers = H_GET_16 (abfd, src->vs_vers); |
252b5132 RH |
172 | } |
173 | ||
174 | /* Swap out a Versym structure. */ | |
175 | ||
176 | void | |
217aa764 AM |
177 | _bfd_elf_swap_versym_out (bfd *abfd, |
178 | const Elf_Internal_Versym *src, | |
179 | Elf_External_Versym *dst) | |
252b5132 | 180 | { |
dc810e39 | 181 | H_PUT_16 (abfd, src->vs_vers, dst->vs_vers); |
252b5132 RH |
182 | } |
183 | ||
184 | /* Standard ELF hash function. Do not change this function; you will | |
185 | cause invalid hash tables to be generated. */ | |
3a99b017 | 186 | |
252b5132 | 187 | unsigned long |
217aa764 | 188 | bfd_elf_hash (const char *namearg) |
252b5132 | 189 | { |
3a99b017 | 190 | const unsigned char *name = (const unsigned char *) namearg; |
252b5132 RH |
191 | unsigned long h = 0; |
192 | unsigned long g; | |
193 | int ch; | |
194 | ||
195 | while ((ch = *name++) != '\0') | |
196 | { | |
197 | h = (h << 4) + ch; | |
198 | if ((g = (h & 0xf0000000)) != 0) | |
199 | { | |
200 | h ^= g >> 24; | |
201 | /* The ELF ABI says `h &= ~g', but this is equivalent in | |
202 | this case and on some machines one insn instead of two. */ | |
203 | h ^= g; | |
204 | } | |
205 | } | |
32dfa85d | 206 | return h & 0xffffffff; |
252b5132 RH |
207 | } |
208 | ||
fdc90cb4 JJ |
209 | /* DT_GNU_HASH hash function. Do not change this function; you will |
210 | cause invalid hash tables to be generated. */ | |
211 | ||
212 | unsigned long | |
213 | bfd_elf_gnu_hash (const char *namearg) | |
214 | { | |
215 | const unsigned char *name = (const unsigned char *) namearg; | |
216 | unsigned long h = 5381; | |
217 | unsigned char ch; | |
218 | ||
219 | while ((ch = *name++) != '\0') | |
220 | h = (h << 5) + h + ch; | |
221 | return h & 0xffffffff; | |
222 | } | |
223 | ||
b34976b6 | 224 | bfd_boolean |
217aa764 | 225 | bfd_elf_mkobject (bfd *abfd) |
252b5132 | 226 | { |
62d7a5f6 AM |
227 | if (abfd->tdata.any == NULL) |
228 | { | |
229 | abfd->tdata.any = bfd_zalloc (abfd, sizeof (struct elf_obj_tdata)); | |
230 | if (abfd->tdata.any == NULL) | |
231 | return FALSE; | |
232 | } | |
233 | ||
234 | elf_tdata (abfd)->program_header_size = (bfd_size_type) -1; | |
252b5132 | 235 | |
b34976b6 | 236 | return TRUE; |
252b5132 RH |
237 | } |
238 | ||
b34976b6 | 239 | bfd_boolean |
217aa764 | 240 | bfd_elf_mkcorefile (bfd *abfd) |
252b5132 | 241 | { |
c044fabd | 242 | /* I think this can be done just like an object file. */ |
252b5132 RH |
243 | return bfd_elf_mkobject (abfd); |
244 | } | |
245 | ||
246 | char * | |
217aa764 | 247 | bfd_elf_get_str_section (bfd *abfd, unsigned int shindex) |
252b5132 RH |
248 | { |
249 | Elf_Internal_Shdr **i_shdrp; | |
f075ee0c | 250 | bfd_byte *shstrtab = NULL; |
dc810e39 AM |
251 | file_ptr offset; |
252 | bfd_size_type shstrtabsize; | |
252b5132 RH |
253 | |
254 | i_shdrp = elf_elfsections (abfd); | |
74f2e02b AM |
255 | if (i_shdrp == 0 |
256 | || shindex >= elf_numsections (abfd) | |
257 | || i_shdrp[shindex] == 0) | |
f075ee0c | 258 | return NULL; |
252b5132 | 259 | |
f075ee0c | 260 | shstrtab = i_shdrp[shindex]->contents; |
252b5132 RH |
261 | if (shstrtab == NULL) |
262 | { | |
c044fabd | 263 | /* No cached one, attempt to read, and cache what we read. */ |
252b5132 RH |
264 | offset = i_shdrp[shindex]->sh_offset; |
265 | shstrtabsize = i_shdrp[shindex]->sh_size; | |
c6c60d09 JJ |
266 | |
267 | /* Allocate and clear an extra byte at the end, to prevent crashes | |
268 | in case the string table is not terminated. */ | |
269 | if (shstrtabsize + 1 == 0 | |
270 | || (shstrtab = bfd_alloc (abfd, shstrtabsize + 1)) == NULL | |
271 | || bfd_seek (abfd, offset, SEEK_SET) != 0) | |
272 | shstrtab = NULL; | |
273 | else if (bfd_bread (shstrtab, shstrtabsize, abfd) != shstrtabsize) | |
274 | { | |
275 | if (bfd_get_error () != bfd_error_system_call) | |
276 | bfd_set_error (bfd_error_file_truncated); | |
277 | shstrtab = NULL; | |
278 | } | |
279 | else | |
280 | shstrtab[shstrtabsize] = '\0'; | |
217aa764 | 281 | i_shdrp[shindex]->contents = shstrtab; |
252b5132 | 282 | } |
f075ee0c | 283 | return (char *) shstrtab; |
252b5132 RH |
284 | } |
285 | ||
286 | char * | |
217aa764 AM |
287 | bfd_elf_string_from_elf_section (bfd *abfd, |
288 | unsigned int shindex, | |
289 | unsigned int strindex) | |
252b5132 RH |
290 | { |
291 | Elf_Internal_Shdr *hdr; | |
292 | ||
293 | if (strindex == 0) | |
294 | return ""; | |
295 | ||
74f2e02b AM |
296 | if (elf_elfsections (abfd) == NULL || shindex >= elf_numsections (abfd)) |
297 | return NULL; | |
298 | ||
252b5132 RH |
299 | hdr = elf_elfsections (abfd)[shindex]; |
300 | ||
301 | if (hdr->contents == NULL | |
302 | && bfd_elf_get_str_section (abfd, shindex) == NULL) | |
303 | return NULL; | |
304 | ||
305 | if (strindex >= hdr->sh_size) | |
306 | { | |
1b3a8575 | 307 | unsigned int shstrndx = elf_elfheader(abfd)->e_shstrndx; |
252b5132 | 308 | (*_bfd_error_handler) |
d003868e AM |
309 | (_("%B: invalid string offset %u >= %lu for section `%s'"), |
310 | abfd, strindex, (unsigned long) hdr->sh_size, | |
1b3a8575 | 311 | (shindex == shstrndx && strindex == hdr->sh_name |
252b5132 | 312 | ? ".shstrtab" |
1b3a8575 | 313 | : bfd_elf_string_from_elf_section (abfd, shstrndx, hdr->sh_name))); |
252b5132 RH |
314 | return ""; |
315 | } | |
316 | ||
317 | return ((char *) hdr->contents) + strindex; | |
318 | } | |
319 | ||
6cdc0ccc AM |
320 | /* Read and convert symbols to internal format. |
321 | SYMCOUNT specifies the number of symbols to read, starting from | |
322 | symbol SYMOFFSET. If any of INTSYM_BUF, EXTSYM_BUF or EXTSHNDX_BUF | |
323 | are non-NULL, they are used to store the internal symbols, external | |
324 | symbols, and symbol section index extensions, respectively. */ | |
325 | ||
326 | Elf_Internal_Sym * | |
217aa764 AM |
327 | bfd_elf_get_elf_syms (bfd *ibfd, |
328 | Elf_Internal_Shdr *symtab_hdr, | |
329 | size_t symcount, | |
330 | size_t symoffset, | |
331 | Elf_Internal_Sym *intsym_buf, | |
332 | void *extsym_buf, | |
333 | Elf_External_Sym_Shndx *extshndx_buf) | |
6cdc0ccc AM |
334 | { |
335 | Elf_Internal_Shdr *shndx_hdr; | |
217aa764 | 336 | void *alloc_ext; |
df622259 | 337 | const bfd_byte *esym; |
6cdc0ccc AM |
338 | Elf_External_Sym_Shndx *alloc_extshndx; |
339 | Elf_External_Sym_Shndx *shndx; | |
340 | Elf_Internal_Sym *isym; | |
341 | Elf_Internal_Sym *isymend; | |
9c5bfbb7 | 342 | const struct elf_backend_data *bed; |
6cdc0ccc AM |
343 | size_t extsym_size; |
344 | bfd_size_type amt; | |
345 | file_ptr pos; | |
346 | ||
347 | if (symcount == 0) | |
348 | return intsym_buf; | |
349 | ||
350 | /* Normal syms might have section extension entries. */ | |
351 | shndx_hdr = NULL; | |
352 | if (symtab_hdr == &elf_tdata (ibfd)->symtab_hdr) | |
353 | shndx_hdr = &elf_tdata (ibfd)->symtab_shndx_hdr; | |
354 | ||
355 | /* Read the symbols. */ | |
356 | alloc_ext = NULL; | |
357 | alloc_extshndx = NULL; | |
358 | bed = get_elf_backend_data (ibfd); | |
359 | extsym_size = bed->s->sizeof_sym; | |
360 | amt = symcount * extsym_size; | |
361 | pos = symtab_hdr->sh_offset + symoffset * extsym_size; | |
362 | if (extsym_buf == NULL) | |
363 | { | |
d0fb9a8d | 364 | alloc_ext = bfd_malloc2 (symcount, extsym_size); |
6cdc0ccc AM |
365 | extsym_buf = alloc_ext; |
366 | } | |
367 | if (extsym_buf == NULL | |
368 | || bfd_seek (ibfd, pos, SEEK_SET) != 0 | |
369 | || bfd_bread (extsym_buf, amt, ibfd) != amt) | |
370 | { | |
371 | intsym_buf = NULL; | |
372 | goto out; | |
373 | } | |
374 | ||
375 | if (shndx_hdr == NULL || shndx_hdr->sh_size == 0) | |
376 | extshndx_buf = NULL; | |
377 | else | |
378 | { | |
379 | amt = symcount * sizeof (Elf_External_Sym_Shndx); | |
380 | pos = shndx_hdr->sh_offset + symoffset * sizeof (Elf_External_Sym_Shndx); | |
381 | if (extshndx_buf == NULL) | |
382 | { | |
d0fb9a8d JJ |
383 | alloc_extshndx = bfd_malloc2 (symcount, |
384 | sizeof (Elf_External_Sym_Shndx)); | |
6cdc0ccc AM |
385 | extshndx_buf = alloc_extshndx; |
386 | } | |
387 | if (extshndx_buf == NULL | |
388 | || bfd_seek (ibfd, pos, SEEK_SET) != 0 | |
389 | || bfd_bread (extshndx_buf, amt, ibfd) != amt) | |
390 | { | |
391 | intsym_buf = NULL; | |
392 | goto out; | |
393 | } | |
394 | } | |
395 | ||
396 | if (intsym_buf == NULL) | |
397 | { | |
d0fb9a8d | 398 | intsym_buf = bfd_malloc2 (symcount, sizeof (Elf_Internal_Sym)); |
6cdc0ccc AM |
399 | if (intsym_buf == NULL) |
400 | goto out; | |
401 | } | |
402 | ||
403 | /* Convert the symbols to internal form. */ | |
404 | isymend = intsym_buf + symcount; | |
405 | for (esym = extsym_buf, isym = intsym_buf, shndx = extshndx_buf; | |
406 | isym < isymend; | |
407 | esym += extsym_size, isym++, shndx = shndx != NULL ? shndx + 1 : NULL) | |
8384fb8f AM |
408 | if (!(*bed->s->swap_symbol_in) (ibfd, esym, shndx, isym)) |
409 | { | |
410 | symoffset += (esym - (bfd_byte *) extsym_buf) / extsym_size; | |
411 | (*_bfd_error_handler) (_("%B symbol number %lu references " | |
412 | "nonexistent SHT_SYMTAB_SHNDX section"), | |
413 | ibfd, (unsigned long) symoffset); | |
414 | intsym_buf = NULL; | |
415 | goto out; | |
416 | } | |
6cdc0ccc AM |
417 | |
418 | out: | |
419 | if (alloc_ext != NULL) | |
420 | free (alloc_ext); | |
421 | if (alloc_extshndx != NULL) | |
422 | free (alloc_extshndx); | |
423 | ||
424 | return intsym_buf; | |
425 | } | |
426 | ||
5cab59f6 AM |
427 | /* Look up a symbol name. */ |
428 | const char * | |
be8dd2ca AM |
429 | bfd_elf_sym_name (bfd *abfd, |
430 | Elf_Internal_Shdr *symtab_hdr, | |
26c61ae5 L |
431 | Elf_Internal_Sym *isym, |
432 | asection *sym_sec) | |
5cab59f6 | 433 | { |
26c61ae5 | 434 | const char *name; |
5cab59f6 | 435 | unsigned int iname = isym->st_name; |
be8dd2ca | 436 | unsigned int shindex = symtab_hdr->sh_link; |
26c61ae5 | 437 | |
138f35cc JJ |
438 | if (iname == 0 && ELF_ST_TYPE (isym->st_info) == STT_SECTION |
439 | /* Check for a bogus st_shndx to avoid crashing. */ | |
440 | && isym->st_shndx < elf_numsections (abfd) | |
441 | && !(isym->st_shndx >= SHN_LORESERVE && isym->st_shndx <= SHN_HIRESERVE)) | |
5cab59f6 AM |
442 | { |
443 | iname = elf_elfsections (abfd)[isym->st_shndx]->sh_name; | |
444 | shindex = elf_elfheader (abfd)->e_shstrndx; | |
445 | } | |
446 | ||
26c61ae5 L |
447 | name = bfd_elf_string_from_elf_section (abfd, shindex, iname); |
448 | if (name == NULL) | |
449 | name = "(null)"; | |
450 | else if (sym_sec && *name == '\0') | |
451 | name = bfd_section_name (abfd, sym_sec); | |
452 | ||
453 | return name; | |
5cab59f6 AM |
454 | } |
455 | ||
dbb410c3 AM |
456 | /* Elf_Internal_Shdr->contents is an array of these for SHT_GROUP |
457 | sections. The first element is the flags, the rest are section | |
458 | pointers. */ | |
459 | ||
460 | typedef union elf_internal_group { | |
461 | Elf_Internal_Shdr *shdr; | |
462 | unsigned int flags; | |
463 | } Elf_Internal_Group; | |
464 | ||
b885599b AM |
465 | /* Return the name of the group signature symbol. Why isn't the |
466 | signature just a string? */ | |
467 | ||
468 | static const char * | |
217aa764 | 469 | group_signature (bfd *abfd, Elf_Internal_Shdr *ghdr) |
b885599b | 470 | { |
9dce4196 | 471 | Elf_Internal_Shdr *hdr; |
9dce4196 AM |
472 | unsigned char esym[sizeof (Elf64_External_Sym)]; |
473 | Elf_External_Sym_Shndx eshndx; | |
474 | Elf_Internal_Sym isym; | |
b885599b | 475 | |
13792e9d L |
476 | /* First we need to ensure the symbol table is available. Make sure |
477 | that it is a symbol table section. */ | |
478 | hdr = elf_elfsections (abfd) [ghdr->sh_link]; | |
479 | if (hdr->sh_type != SHT_SYMTAB | |
480 | || ! bfd_section_from_shdr (abfd, ghdr->sh_link)) | |
b885599b AM |
481 | return NULL; |
482 | ||
9dce4196 AM |
483 | /* Go read the symbol. */ |
484 | hdr = &elf_tdata (abfd)->symtab_hdr; | |
6cdc0ccc AM |
485 | if (bfd_elf_get_elf_syms (abfd, hdr, 1, ghdr->sh_info, |
486 | &isym, esym, &eshndx) == NULL) | |
b885599b | 487 | return NULL; |
9dce4196 | 488 | |
26c61ae5 | 489 | return bfd_elf_sym_name (abfd, hdr, &isym, NULL); |
b885599b AM |
490 | } |
491 | ||
dbb410c3 AM |
492 | /* Set next_in_group list pointer, and group name for NEWSECT. */ |
493 | ||
b34976b6 | 494 | static bfd_boolean |
217aa764 | 495 | setup_group (bfd *abfd, Elf_Internal_Shdr *hdr, asection *newsect) |
dbb410c3 AM |
496 | { |
497 | unsigned int num_group = elf_tdata (abfd)->num_group; | |
498 | ||
499 | /* If num_group is zero, read in all SHT_GROUP sections. The count | |
500 | is set to -1 if there are no SHT_GROUP sections. */ | |
501 | if (num_group == 0) | |
502 | { | |
503 | unsigned int i, shnum; | |
504 | ||
505 | /* First count the number of groups. If we have a SHT_GROUP | |
506 | section with just a flag word (ie. sh_size is 4), ignore it. */ | |
9ad5cbcf | 507 | shnum = elf_numsections (abfd); |
dbb410c3 | 508 | num_group = 0; |
1783205a NC |
509 | |
510 | #define IS_VALID_GROUP_SECTION_HEADER(shdr) \ | |
511 | ( (shdr)->sh_type == SHT_GROUP \ | |
512 | && (shdr)->sh_size >= (2 * GRP_ENTRY_SIZE) \ | |
513 | && (shdr)->sh_entsize == GRP_ENTRY_SIZE \ | |
514 | && ((shdr)->sh_size % GRP_ENTRY_SIZE) == 0) | |
515 | ||
dbb410c3 AM |
516 | for (i = 0; i < shnum; i++) |
517 | { | |
518 | Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i]; | |
1783205a NC |
519 | |
520 | if (IS_VALID_GROUP_SECTION_HEADER (shdr)) | |
dbb410c3 AM |
521 | num_group += 1; |
522 | } | |
523 | ||
524 | if (num_group == 0) | |
20dbb49d L |
525 | { |
526 | num_group = (unsigned) -1; | |
527 | elf_tdata (abfd)->num_group = num_group; | |
528 | } | |
529 | else | |
dbb410c3 AM |
530 | { |
531 | /* We keep a list of elf section headers for group sections, | |
532 | so we can find them quickly. */ | |
20dbb49d | 533 | bfd_size_type amt; |
d0fb9a8d | 534 | |
20dbb49d | 535 | elf_tdata (abfd)->num_group = num_group; |
d0fb9a8d JJ |
536 | elf_tdata (abfd)->group_sect_ptr |
537 | = bfd_alloc2 (abfd, num_group, sizeof (Elf_Internal_Shdr *)); | |
dbb410c3 | 538 | if (elf_tdata (abfd)->group_sect_ptr == NULL) |
b34976b6 | 539 | return FALSE; |
dbb410c3 AM |
540 | |
541 | num_group = 0; | |
542 | for (i = 0; i < shnum; i++) | |
543 | { | |
544 | Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i]; | |
1783205a NC |
545 | |
546 | if (IS_VALID_GROUP_SECTION_HEADER (shdr)) | |
dbb410c3 | 547 | { |
973ffd63 | 548 | unsigned char *src; |
dbb410c3 AM |
549 | Elf_Internal_Group *dest; |
550 | ||
551 | /* Add to list of sections. */ | |
552 | elf_tdata (abfd)->group_sect_ptr[num_group] = shdr; | |
553 | num_group += 1; | |
554 | ||
555 | /* Read the raw contents. */ | |
556 | BFD_ASSERT (sizeof (*dest) >= 4); | |
557 | amt = shdr->sh_size * sizeof (*dest) / 4; | |
d0fb9a8d JJ |
558 | shdr->contents = bfd_alloc2 (abfd, shdr->sh_size, |
559 | sizeof (*dest) / 4); | |
1783205a NC |
560 | /* PR binutils/4110: Handle corrupt group headers. */ |
561 | if (shdr->contents == NULL) | |
562 | { | |
563 | _bfd_error_handler | |
564 | (_("%B: Corrupt size field in group section header: 0x%lx"), abfd, shdr->sh_size); | |
565 | bfd_set_error (bfd_error_bad_value); | |
566 | return FALSE; | |
567 | } | |
568 | ||
569 | memset (shdr->contents, 0, amt); | |
570 | ||
571 | if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0 | |
dbb410c3 AM |
572 | || (bfd_bread (shdr->contents, shdr->sh_size, abfd) |
573 | != shdr->sh_size)) | |
b34976b6 | 574 | return FALSE; |
dbb410c3 AM |
575 | |
576 | /* Translate raw contents, a flag word followed by an | |
577 | array of elf section indices all in target byte order, | |
578 | to the flag word followed by an array of elf section | |
579 | pointers. */ | |
580 | src = shdr->contents + shdr->sh_size; | |
581 | dest = (Elf_Internal_Group *) (shdr->contents + amt); | |
582 | while (1) | |
583 | { | |
584 | unsigned int idx; | |
585 | ||
586 | src -= 4; | |
587 | --dest; | |
588 | idx = H_GET_32 (abfd, src); | |
589 | if (src == shdr->contents) | |
590 | { | |
591 | dest->flags = idx; | |
b885599b AM |
592 | if (shdr->bfd_section != NULL && (idx & GRP_COMDAT)) |
593 | shdr->bfd_section->flags | |
594 | |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD; | |
dbb410c3 AM |
595 | break; |
596 | } | |
597 | if (idx >= shnum) | |
598 | { | |
599 | ((*_bfd_error_handler) | |
d003868e | 600 | (_("%B: invalid SHT_GROUP entry"), abfd)); |
dbb410c3 AM |
601 | idx = 0; |
602 | } | |
603 | dest->shdr = elf_elfsections (abfd)[idx]; | |
604 | } | |
605 | } | |
606 | } | |
607 | } | |
608 | } | |
609 | ||
610 | if (num_group != (unsigned) -1) | |
611 | { | |
612 | unsigned int i; | |
613 | ||
614 | for (i = 0; i < num_group; i++) | |
615 | { | |
616 | Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i]; | |
617 | Elf_Internal_Group *idx = (Elf_Internal_Group *) shdr->contents; | |
618 | unsigned int n_elt = shdr->sh_size / 4; | |
619 | ||
620 | /* Look through this group's sections to see if current | |
621 | section is a member. */ | |
622 | while (--n_elt != 0) | |
623 | if ((++idx)->shdr == hdr) | |
624 | { | |
e0e8c97f | 625 | asection *s = NULL; |
dbb410c3 AM |
626 | |
627 | /* We are a member of this group. Go looking through | |
628 | other members to see if any others are linked via | |
629 | next_in_group. */ | |
630 | idx = (Elf_Internal_Group *) shdr->contents; | |
631 | n_elt = shdr->sh_size / 4; | |
632 | while (--n_elt != 0) | |
633 | if ((s = (++idx)->shdr->bfd_section) != NULL | |
945906ff | 634 | && elf_next_in_group (s) != NULL) |
dbb410c3 AM |
635 | break; |
636 | if (n_elt != 0) | |
637 | { | |
dbb410c3 AM |
638 | /* Snarf the group name from other member, and |
639 | insert current section in circular list. */ | |
945906ff AM |
640 | elf_group_name (newsect) = elf_group_name (s); |
641 | elf_next_in_group (newsect) = elf_next_in_group (s); | |
642 | elf_next_in_group (s) = newsect; | |
dbb410c3 AM |
643 | } |
644 | else | |
645 | { | |
dbb410c3 AM |
646 | const char *gname; |
647 | ||
b885599b AM |
648 | gname = group_signature (abfd, shdr); |
649 | if (gname == NULL) | |
b34976b6 | 650 | return FALSE; |
945906ff | 651 | elf_group_name (newsect) = gname; |
dbb410c3 AM |
652 | |
653 | /* Start a circular list with one element. */ | |
945906ff | 654 | elf_next_in_group (newsect) = newsect; |
dbb410c3 | 655 | } |
b885599b | 656 | |
9dce4196 AM |
657 | /* If the group section has been created, point to the |
658 | new member. */ | |
dbb410c3 | 659 | if (shdr->bfd_section != NULL) |
945906ff | 660 | elf_next_in_group (shdr->bfd_section) = newsect; |
b885599b | 661 | |
dbb410c3 AM |
662 | i = num_group - 1; |
663 | break; | |
664 | } | |
665 | } | |
666 | } | |
667 | ||
945906ff | 668 | if (elf_group_name (newsect) == NULL) |
dbb410c3 | 669 | { |
d003868e AM |
670 | (*_bfd_error_handler) (_("%B: no group info for section %A"), |
671 | abfd, newsect); | |
dbb410c3 | 672 | } |
b34976b6 | 673 | return TRUE; |
dbb410c3 AM |
674 | } |
675 | ||
3d7f7666 | 676 | bfd_boolean |
dd863624 | 677 | _bfd_elf_setup_sections (bfd *abfd) |
3d7f7666 L |
678 | { |
679 | unsigned int i; | |
680 | unsigned int num_group = elf_tdata (abfd)->num_group; | |
681 | bfd_boolean result = TRUE; | |
dd863624 L |
682 | asection *s; |
683 | ||
684 | /* Process SHF_LINK_ORDER. */ | |
685 | for (s = abfd->sections; s != NULL; s = s->next) | |
686 | { | |
687 | Elf_Internal_Shdr *this_hdr = &elf_section_data (s)->this_hdr; | |
688 | if ((this_hdr->sh_flags & SHF_LINK_ORDER) != 0) | |
689 | { | |
690 | unsigned int elfsec = this_hdr->sh_link; | |
691 | /* FIXME: The old Intel compiler and old strip/objcopy may | |
692 | not set the sh_link or sh_info fields. Hence we could | |
693 | get the situation where elfsec is 0. */ | |
694 | if (elfsec == 0) | |
695 | { | |
696 | const struct elf_backend_data *bed | |
697 | = get_elf_backend_data (abfd); | |
698 | if (bed->link_order_error_handler) | |
699 | bed->link_order_error_handler | |
700 | (_("%B: warning: sh_link not set for section `%A'"), | |
701 | abfd, s); | |
702 | } | |
703 | else | |
704 | { | |
25bbc984 L |
705 | asection *link; |
706 | ||
dd863624 | 707 | this_hdr = elf_elfsections (abfd)[elfsec]; |
25bbc984 L |
708 | |
709 | /* PR 1991, 2008: | |
710 | Some strip/objcopy may leave an incorrect value in | |
711 | sh_link. We don't want to proceed. */ | |
712 | link = this_hdr->bfd_section; | |
713 | if (link == NULL) | |
714 | { | |
715 | (*_bfd_error_handler) | |
716 | (_("%B: sh_link [%d] in section `%A' is incorrect"), | |
717 | s->owner, s, elfsec); | |
718 | result = FALSE; | |
719 | } | |
720 | ||
721 | elf_linked_to_section (s) = link; | |
dd863624 L |
722 | } |
723 | } | |
724 | } | |
3d7f7666 | 725 | |
dd863624 | 726 | /* Process section groups. */ |
3d7f7666 L |
727 | if (num_group == (unsigned) -1) |
728 | return result; | |
729 | ||
730 | for (i = 0; i < num_group; i++) | |
731 | { | |
732 | Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i]; | |
733 | Elf_Internal_Group *idx = (Elf_Internal_Group *) shdr->contents; | |
734 | unsigned int n_elt = shdr->sh_size / 4; | |
735 | ||
736 | while (--n_elt != 0) | |
737 | if ((++idx)->shdr->bfd_section) | |
738 | elf_sec_group (idx->shdr->bfd_section) = shdr->bfd_section; | |
739 | else if (idx->shdr->sh_type == SHT_RELA | |
740 | || idx->shdr->sh_type == SHT_REL) | |
741 | /* We won't include relocation sections in section groups in | |
742 | output object files. We adjust the group section size here | |
743 | so that relocatable link will work correctly when | |
744 | relocation sections are in section group in input object | |
745 | files. */ | |
746 | shdr->bfd_section->size -= 4; | |
747 | else | |
748 | { | |
749 | /* There are some unknown sections in the group. */ | |
750 | (*_bfd_error_handler) | |
d003868e AM |
751 | (_("%B: unknown [%d] section `%s' in group [%s]"), |
752 | abfd, | |
3d7f7666 | 753 | (unsigned int) idx->shdr->sh_type, |
1b3a8575 AM |
754 | bfd_elf_string_from_elf_section (abfd, |
755 | (elf_elfheader (abfd) | |
756 | ->e_shstrndx), | |
757 | idx->shdr->sh_name), | |
3d7f7666 L |
758 | shdr->bfd_section->name); |
759 | result = FALSE; | |
760 | } | |
761 | } | |
762 | return result; | |
763 | } | |
764 | ||
72adc230 AM |
765 | bfd_boolean |
766 | bfd_elf_is_group_section (bfd *abfd ATTRIBUTE_UNUSED, const asection *sec) | |
767 | { | |
768 | return elf_next_in_group (sec) != NULL; | |
769 | } | |
770 | ||
252b5132 RH |
771 | /* Make a BFD section from an ELF section. We store a pointer to the |
772 | BFD section in the bfd_section field of the header. */ | |
773 | ||
b34976b6 | 774 | bfd_boolean |
217aa764 AM |
775 | _bfd_elf_make_section_from_shdr (bfd *abfd, |
776 | Elf_Internal_Shdr *hdr, | |
6dc132d9 L |
777 | const char *name, |
778 | int shindex) | |
252b5132 RH |
779 | { |
780 | asection *newsect; | |
781 | flagword flags; | |
9c5bfbb7 | 782 | const struct elf_backend_data *bed; |
252b5132 RH |
783 | |
784 | if (hdr->bfd_section != NULL) | |
785 | { | |
786 | BFD_ASSERT (strcmp (name, | |
787 | bfd_get_section_name (abfd, hdr->bfd_section)) == 0); | |
b34976b6 | 788 | return TRUE; |
252b5132 RH |
789 | } |
790 | ||
791 | newsect = bfd_make_section_anyway (abfd, name); | |
792 | if (newsect == NULL) | |
b34976b6 | 793 | return FALSE; |
252b5132 | 794 | |
1829f4b2 AM |
795 | hdr->bfd_section = newsect; |
796 | elf_section_data (newsect)->this_hdr = *hdr; | |
6dc132d9 | 797 | elf_section_data (newsect)->this_idx = shindex; |
1829f4b2 | 798 | |
2f89ff8d L |
799 | /* Always use the real type/flags. */ |
800 | elf_section_type (newsect) = hdr->sh_type; | |
801 | elf_section_flags (newsect) = hdr->sh_flags; | |
802 | ||
252b5132 RH |
803 | newsect->filepos = hdr->sh_offset; |
804 | ||
805 | if (! bfd_set_section_vma (abfd, newsect, hdr->sh_addr) | |
806 | || ! bfd_set_section_size (abfd, newsect, hdr->sh_size) | |
807 | || ! bfd_set_section_alignment (abfd, newsect, | |
dc810e39 | 808 | bfd_log2 ((bfd_vma) hdr->sh_addralign))) |
b34976b6 | 809 | return FALSE; |
252b5132 RH |
810 | |
811 | flags = SEC_NO_FLAGS; | |
812 | if (hdr->sh_type != SHT_NOBITS) | |
813 | flags |= SEC_HAS_CONTENTS; | |
dbb410c3 | 814 | if (hdr->sh_type == SHT_GROUP) |
b3096250 | 815 | flags |= SEC_GROUP | SEC_EXCLUDE; |
252b5132 RH |
816 | if ((hdr->sh_flags & SHF_ALLOC) != 0) |
817 | { | |
818 | flags |= SEC_ALLOC; | |
819 | if (hdr->sh_type != SHT_NOBITS) | |
820 | flags |= SEC_LOAD; | |
821 | } | |
822 | if ((hdr->sh_flags & SHF_WRITE) == 0) | |
823 | flags |= SEC_READONLY; | |
824 | if ((hdr->sh_flags & SHF_EXECINSTR) != 0) | |
825 | flags |= SEC_CODE; | |
826 | else if ((flags & SEC_LOAD) != 0) | |
827 | flags |= SEC_DATA; | |
f5fa8ca2 JJ |
828 | if ((hdr->sh_flags & SHF_MERGE) != 0) |
829 | { | |
830 | flags |= SEC_MERGE; | |
831 | newsect->entsize = hdr->sh_entsize; | |
832 | if ((hdr->sh_flags & SHF_STRINGS) != 0) | |
833 | flags |= SEC_STRINGS; | |
834 | } | |
dbb410c3 AM |
835 | if (hdr->sh_flags & SHF_GROUP) |
836 | if (!setup_group (abfd, hdr, newsect)) | |
b34976b6 | 837 | return FALSE; |
13ae64f3 JJ |
838 | if ((hdr->sh_flags & SHF_TLS) != 0) |
839 | flags |= SEC_THREAD_LOCAL; | |
252b5132 | 840 | |
3d2b39cf | 841 | if ((flags & SEC_ALLOC) == 0) |
7a6cc5fb | 842 | { |
3d2b39cf L |
843 | /* The debugging sections appear to be recognized only by name, |
844 | not any sort of flag. Their SEC_ALLOC bits are cleared. */ | |
845 | static const struct | |
846 | { | |
847 | const char *name; | |
848 | int len; | |
849 | } debug_sections [] = | |
850 | { | |
0112cd26 | 851 | { STRING_COMMA_LEN ("debug") }, /* 'd' */ |
3d2b39cf L |
852 | { NULL, 0 }, /* 'e' */ |
853 | { NULL, 0 }, /* 'f' */ | |
0112cd26 | 854 | { STRING_COMMA_LEN ("gnu.linkonce.wi.") }, /* 'g' */ |
3d2b39cf L |
855 | { NULL, 0 }, /* 'h' */ |
856 | { NULL, 0 }, /* 'i' */ | |
857 | { NULL, 0 }, /* 'j' */ | |
858 | { NULL, 0 }, /* 'k' */ | |
0112cd26 | 859 | { STRING_COMMA_LEN ("line") }, /* 'l' */ |
3d2b39cf L |
860 | { NULL, 0 }, /* 'm' */ |
861 | { NULL, 0 }, /* 'n' */ | |
862 | { NULL, 0 }, /* 'o' */ | |
863 | { NULL, 0 }, /* 'p' */ | |
864 | { NULL, 0 }, /* 'q' */ | |
865 | { NULL, 0 }, /* 'r' */ | |
0112cd26 | 866 | { STRING_COMMA_LEN ("stab") } /* 's' */ |
3d2b39cf L |
867 | }; |
868 | ||
869 | if (name [0] == '.') | |
870 | { | |
871 | int i = name [1] - 'd'; | |
872 | if (i >= 0 | |
873 | && i < (int) ARRAY_SIZE (debug_sections) | |
874 | && debug_sections [i].name != NULL | |
875 | && strncmp (&name [1], debug_sections [i].name, | |
876 | debug_sections [i].len) == 0) | |
877 | flags |= SEC_DEBUGGING; | |
878 | } | |
879 | } | |
252b5132 RH |
880 | |
881 | /* As a GNU extension, if the name begins with .gnu.linkonce, we | |
882 | only link a single copy of the section. This is used to support | |
883 | g++. g++ will emit each template expansion in its own section. | |
884 | The symbols will be defined as weak, so that multiple definitions | |
885 | are permitted. The GNU linker extension is to actually discard | |
886 | all but one of the sections. */ | |
0112cd26 | 887 | if (CONST_STRNEQ (name, ".gnu.linkonce") |
b885599b | 888 | && elf_next_in_group (newsect) == NULL) |
252b5132 RH |
889 | flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD; |
890 | ||
fa152c49 JW |
891 | bed = get_elf_backend_data (abfd); |
892 | if (bed->elf_backend_section_flags) | |
893 | if (! bed->elf_backend_section_flags (&flags, hdr)) | |
b34976b6 | 894 | return FALSE; |
fa152c49 | 895 | |
252b5132 | 896 | if (! bfd_set_section_flags (abfd, newsect, flags)) |
b34976b6 | 897 | return FALSE; |
252b5132 RH |
898 | |
899 | if ((flags & SEC_ALLOC) != 0) | |
900 | { | |
901 | Elf_Internal_Phdr *phdr; | |
902 | unsigned int i; | |
903 | ||
904 | /* Look through the phdrs to see if we need to adjust the lma. | |
905 | If all the p_paddr fields are zero, we ignore them, since | |
906 | some ELF linkers produce such output. */ | |
907 | phdr = elf_tdata (abfd)->phdr; | |
908 | for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++) | |
909 | { | |
910 | if (phdr->p_paddr != 0) | |
911 | break; | |
912 | } | |
913 | if (i < elf_elfheader (abfd)->e_phnum) | |
914 | { | |
915 | phdr = elf_tdata (abfd)->phdr; | |
916 | for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++) | |
917 | { | |
e0e8c97f NC |
918 | /* This section is part of this segment if its file |
919 | offset plus size lies within the segment's memory | |
920 | span and, if the section is loaded, the extent of the | |
47d9a591 | 921 | loaded data lies within the extent of the segment. |
bf36db18 NC |
922 | |
923 | Note - we used to check the p_paddr field as well, and | |
924 | refuse to set the LMA if it was 0. This is wrong | |
dba143ef | 925 | though, as a perfectly valid initialised segment can |
bf36db18 | 926 | have a p_paddr of zero. Some architectures, eg ARM, |
dba143ef | 927 | place special significance on the address 0 and |
bf36db18 NC |
928 | executables need to be able to have a segment which |
929 | covers this address. */ | |
252b5132 | 930 | if (phdr->p_type == PT_LOAD |
e0e8c97f NC |
931 | && (bfd_vma) hdr->sh_offset >= phdr->p_offset |
932 | && (hdr->sh_offset + hdr->sh_size | |
933 | <= phdr->p_offset + phdr->p_memsz) | |
252b5132 | 934 | && ((flags & SEC_LOAD) == 0 |
d7866f04 AM |
935 | || (hdr->sh_offset + hdr->sh_size |
936 | <= phdr->p_offset + phdr->p_filesz))) | |
252b5132 | 937 | { |
dba143ef | 938 | if ((flags & SEC_LOAD) == 0) |
d7866f04 AM |
939 | newsect->lma = (phdr->p_paddr |
940 | + hdr->sh_addr - phdr->p_vaddr); | |
dba143ef AM |
941 | else |
942 | /* We used to use the same adjustment for SEC_LOAD | |
943 | sections, but that doesn't work if the segment | |
944 | is packed with code from multiple VMAs. | |
945 | Instead we calculate the section LMA based on | |
946 | the segment LMA. It is assumed that the | |
947 | segment will contain sections with contiguous | |
948 | LMAs, even if the VMAs are not. */ | |
949 | newsect->lma = (phdr->p_paddr | |
950 | + hdr->sh_offset - phdr->p_offset); | |
d7866f04 AM |
951 | |
952 | /* With contiguous segments, we can't tell from file | |
953 | offsets whether a section with zero size should | |
954 | be placed at the end of one segment or the | |
955 | beginning of the next. Decide based on vaddr. */ | |
956 | if (hdr->sh_addr >= phdr->p_vaddr | |
957 | && (hdr->sh_addr + hdr->sh_size | |
958 | <= phdr->p_vaddr + phdr->p_memsz)) | |
959 | break; | |
252b5132 RH |
960 | } |
961 | } | |
962 | } | |
963 | } | |
964 | ||
b34976b6 | 965 | return TRUE; |
252b5132 RH |
966 | } |
967 | ||
968 | /* | |
969 | INTERNAL_FUNCTION | |
970 | bfd_elf_find_section | |
971 | ||
972 | SYNOPSIS | |
973 | struct elf_internal_shdr *bfd_elf_find_section (bfd *abfd, char *name); | |
974 | ||
975 | DESCRIPTION | |
976 | Helper functions for GDB to locate the string tables. | |
977 | Since BFD hides string tables from callers, GDB needs to use an | |
978 | internal hook to find them. Sun's .stabstr, in particular, | |
979 | isn't even pointed to by the .stab section, so ordinary | |
980 | mechanisms wouldn't work to find it, even if we had some. | |
981 | */ | |
982 | ||
983 | struct elf_internal_shdr * | |
217aa764 | 984 | bfd_elf_find_section (bfd *abfd, char *name) |
252b5132 RH |
985 | { |
986 | Elf_Internal_Shdr **i_shdrp; | |
987 | char *shstrtab; | |
988 | unsigned int max; | |
989 | unsigned int i; | |
990 | ||
991 | i_shdrp = elf_elfsections (abfd); | |
992 | if (i_shdrp != NULL) | |
993 | { | |
9ad5cbcf AM |
994 | shstrtab = bfd_elf_get_str_section (abfd, |
995 | elf_elfheader (abfd)->e_shstrndx); | |
252b5132 RH |
996 | if (shstrtab != NULL) |
997 | { | |
9ad5cbcf | 998 | max = elf_numsections (abfd); |
252b5132 RH |
999 | for (i = 1; i < max; i++) |
1000 | if (!strcmp (&shstrtab[i_shdrp[i]->sh_name], name)) | |
1001 | return i_shdrp[i]; | |
1002 | } | |
1003 | } | |
1004 | return 0; | |
1005 | } | |
1006 | ||
1007 | const char *const bfd_elf_section_type_names[] = { | |
1008 | "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB", | |
1009 | "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE", | |
1010 | "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM", | |
1011 | }; | |
1012 | ||
1049f94e | 1013 | /* ELF relocs are against symbols. If we are producing relocatable |
252b5132 RH |
1014 | output, and the reloc is against an external symbol, and nothing |
1015 | has given us any additional addend, the resulting reloc will also | |
1016 | be against the same symbol. In such a case, we don't want to | |
1017 | change anything about the way the reloc is handled, since it will | |
1018 | all be done at final link time. Rather than put special case code | |
1019 | into bfd_perform_relocation, all the reloc types use this howto | |
1020 | function. It just short circuits the reloc if producing | |
1049f94e | 1021 | relocatable output against an external symbol. */ |
252b5132 | 1022 | |
252b5132 | 1023 | bfd_reloc_status_type |
217aa764 AM |
1024 | bfd_elf_generic_reloc (bfd *abfd ATTRIBUTE_UNUSED, |
1025 | arelent *reloc_entry, | |
1026 | asymbol *symbol, | |
1027 | void *data ATTRIBUTE_UNUSED, | |
1028 | asection *input_section, | |
1029 | bfd *output_bfd, | |
1030 | char **error_message ATTRIBUTE_UNUSED) | |
1031 | { | |
1032 | if (output_bfd != NULL | |
252b5132 RH |
1033 | && (symbol->flags & BSF_SECTION_SYM) == 0 |
1034 | && (! reloc_entry->howto->partial_inplace | |
1035 | || reloc_entry->addend == 0)) | |
1036 | { | |
1037 | reloc_entry->address += input_section->output_offset; | |
1038 | return bfd_reloc_ok; | |
1039 | } | |
1040 | ||
1041 | return bfd_reloc_continue; | |
1042 | } | |
1043 | \f | |
d3c456e9 JJ |
1044 | /* Make sure sec_info_type is cleared if sec_info is cleared too. */ |
1045 | ||
1046 | static void | |
217aa764 AM |
1047 | merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED, |
1048 | asection *sec) | |
d3c456e9 | 1049 | { |
68bfbfcc AM |
1050 | BFD_ASSERT (sec->sec_info_type == ELF_INFO_TYPE_MERGE); |
1051 | sec->sec_info_type = ELF_INFO_TYPE_NONE; | |
d3c456e9 JJ |
1052 | } |
1053 | ||
8550eb6e JJ |
1054 | /* Finish SHF_MERGE section merging. */ |
1055 | ||
b34976b6 | 1056 | bfd_boolean |
217aa764 | 1057 | _bfd_elf_merge_sections (bfd *abfd, struct bfd_link_info *info) |
8550eb6e | 1058 | { |
57ceae94 AM |
1059 | bfd *ibfd; |
1060 | asection *sec; | |
1061 | ||
0eddce27 | 1062 | if (!is_elf_hash_table (info->hash)) |
b34976b6 | 1063 | return FALSE; |
57ceae94 AM |
1064 | |
1065 | for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next) | |
1066 | if ((ibfd->flags & DYNAMIC) == 0) | |
1067 | for (sec = ibfd->sections; sec != NULL; sec = sec->next) | |
1068 | if ((sec->flags & SEC_MERGE) != 0 | |
1069 | && !bfd_is_abs_section (sec->output_section)) | |
1070 | { | |
1071 | struct bfd_elf_section_data *secdata; | |
1072 | ||
1073 | secdata = elf_section_data (sec); | |
1074 | if (! _bfd_add_merge_section (abfd, | |
1075 | &elf_hash_table (info)->merge_info, | |
1076 | sec, &secdata->sec_info)) | |
1077 | return FALSE; | |
1078 | else if (secdata->sec_info) | |
1079 | sec->sec_info_type = ELF_INFO_TYPE_MERGE; | |
1080 | } | |
1081 | ||
1082 | if (elf_hash_table (info)->merge_info != NULL) | |
1083 | _bfd_merge_sections (abfd, info, elf_hash_table (info)->merge_info, | |
d3c456e9 | 1084 | merge_sections_remove_hook); |
b34976b6 | 1085 | return TRUE; |
8550eb6e | 1086 | } |
2d653fc7 AM |
1087 | |
1088 | void | |
217aa764 | 1089 | _bfd_elf_link_just_syms (asection *sec, struct bfd_link_info *info) |
2d653fc7 AM |
1090 | { |
1091 | sec->output_section = bfd_abs_section_ptr; | |
1092 | sec->output_offset = sec->vma; | |
0eddce27 | 1093 | if (!is_elf_hash_table (info->hash)) |
2d653fc7 AM |
1094 | return; |
1095 | ||
68bfbfcc | 1096 | sec->sec_info_type = ELF_INFO_TYPE_JUST_SYMS; |
2d653fc7 | 1097 | } |
8550eb6e | 1098 | \f |
0ac4564e L |
1099 | /* Copy the program header and other data from one object module to |
1100 | another. */ | |
252b5132 | 1101 | |
b34976b6 | 1102 | bfd_boolean |
217aa764 | 1103 | _bfd_elf_copy_private_bfd_data (bfd *ibfd, bfd *obfd) |
2d502050 L |
1104 | { |
1105 | if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour | |
1106 | || bfd_get_flavour (obfd) != bfd_target_elf_flavour) | |
b34976b6 | 1107 | return TRUE; |
2d502050 L |
1108 | |
1109 | BFD_ASSERT (!elf_flags_init (obfd) | |
1110 | || (elf_elfheader (obfd)->e_flags | |
1111 | == elf_elfheader (ibfd)->e_flags)); | |
1112 | ||
0ac4564e | 1113 | elf_gp (obfd) = elf_gp (ibfd); |
2d502050 | 1114 | elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags; |
b34976b6 AM |
1115 | elf_flags_init (obfd) = TRUE; |
1116 | return TRUE; | |
2d502050 L |
1117 | } |
1118 | ||
cedc298e L |
1119 | static const char * |
1120 | get_segment_type (unsigned int p_type) | |
1121 | { | |
1122 | const char *pt; | |
1123 | switch (p_type) | |
1124 | { | |
1125 | case PT_NULL: pt = "NULL"; break; | |
1126 | case PT_LOAD: pt = "LOAD"; break; | |
1127 | case PT_DYNAMIC: pt = "DYNAMIC"; break; | |
1128 | case PT_INTERP: pt = "INTERP"; break; | |
1129 | case PT_NOTE: pt = "NOTE"; break; | |
1130 | case PT_SHLIB: pt = "SHLIB"; break; | |
1131 | case PT_PHDR: pt = "PHDR"; break; | |
1132 | case PT_TLS: pt = "TLS"; break; | |
1133 | case PT_GNU_EH_FRAME: pt = "EH_FRAME"; break; | |
1134 | case PT_GNU_STACK: pt = "STACK"; break; | |
1135 | case PT_GNU_RELRO: pt = "RELRO"; break; | |
1136 | default: pt = NULL; break; | |
1137 | } | |
1138 | return pt; | |
1139 | } | |
1140 | ||
f0b79d91 L |
1141 | /* Print out the program headers. */ |
1142 | ||
b34976b6 | 1143 | bfd_boolean |
217aa764 | 1144 | _bfd_elf_print_private_bfd_data (bfd *abfd, void *farg) |
252b5132 | 1145 | { |
217aa764 | 1146 | FILE *f = farg; |
252b5132 RH |
1147 | Elf_Internal_Phdr *p; |
1148 | asection *s; | |
1149 | bfd_byte *dynbuf = NULL; | |
1150 | ||
1151 | p = elf_tdata (abfd)->phdr; | |
1152 | if (p != NULL) | |
1153 | { | |
1154 | unsigned int i, c; | |
1155 | ||
1156 | fprintf (f, _("\nProgram Header:\n")); | |
1157 | c = elf_elfheader (abfd)->e_phnum; | |
1158 | for (i = 0; i < c; i++, p++) | |
1159 | { | |
cedc298e | 1160 | const char *pt = get_segment_type (p->p_type); |
252b5132 RH |
1161 | char buf[20]; |
1162 | ||
cedc298e | 1163 | if (pt == NULL) |
252b5132 | 1164 | { |
cedc298e L |
1165 | sprintf (buf, "0x%lx", p->p_type); |
1166 | pt = buf; | |
252b5132 | 1167 | } |
dc810e39 | 1168 | fprintf (f, "%8s off 0x", pt); |
60b89a18 | 1169 | bfd_fprintf_vma (abfd, f, p->p_offset); |
252b5132 | 1170 | fprintf (f, " vaddr 0x"); |
60b89a18 | 1171 | bfd_fprintf_vma (abfd, f, p->p_vaddr); |
252b5132 | 1172 | fprintf (f, " paddr 0x"); |
60b89a18 | 1173 | bfd_fprintf_vma (abfd, f, p->p_paddr); |
252b5132 RH |
1174 | fprintf (f, " align 2**%u\n", bfd_log2 (p->p_align)); |
1175 | fprintf (f, " filesz 0x"); | |
60b89a18 | 1176 | bfd_fprintf_vma (abfd, f, p->p_filesz); |
252b5132 | 1177 | fprintf (f, " memsz 0x"); |
60b89a18 | 1178 | bfd_fprintf_vma (abfd, f, p->p_memsz); |
252b5132 RH |
1179 | fprintf (f, " flags %c%c%c", |
1180 | (p->p_flags & PF_R) != 0 ? 'r' : '-', | |
1181 | (p->p_flags & PF_W) != 0 ? 'w' : '-', | |
1182 | (p->p_flags & PF_X) != 0 ? 'x' : '-'); | |
dc810e39 AM |
1183 | if ((p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X)) != 0) |
1184 | fprintf (f, " %lx", p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X)); | |
252b5132 RH |
1185 | fprintf (f, "\n"); |
1186 | } | |
1187 | } | |
1188 | ||
1189 | s = bfd_get_section_by_name (abfd, ".dynamic"); | |
1190 | if (s != NULL) | |
1191 | { | |
1192 | int elfsec; | |
dc810e39 | 1193 | unsigned long shlink; |
252b5132 RH |
1194 | bfd_byte *extdyn, *extdynend; |
1195 | size_t extdynsize; | |
217aa764 | 1196 | void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *); |
252b5132 RH |
1197 | |
1198 | fprintf (f, _("\nDynamic Section:\n")); | |
1199 | ||
eea6121a | 1200 | if (!bfd_malloc_and_get_section (abfd, s, &dynbuf)) |
252b5132 RH |
1201 | goto error_return; |
1202 | ||
1203 | elfsec = _bfd_elf_section_from_bfd_section (abfd, s); | |
1204 | if (elfsec == -1) | |
1205 | goto error_return; | |
dc810e39 | 1206 | shlink = elf_elfsections (abfd)[elfsec]->sh_link; |
252b5132 RH |
1207 | |
1208 | extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn; | |
1209 | swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in; | |
1210 | ||
1211 | extdyn = dynbuf; | |
eea6121a | 1212 | extdynend = extdyn + s->size; |
252b5132 RH |
1213 | for (; extdyn < extdynend; extdyn += extdynsize) |
1214 | { | |
1215 | Elf_Internal_Dyn dyn; | |
1216 | const char *name; | |
1217 | char ab[20]; | |
b34976b6 | 1218 | bfd_boolean stringp; |
252b5132 | 1219 | |
217aa764 | 1220 | (*swap_dyn_in) (abfd, extdyn, &dyn); |
252b5132 RH |
1221 | |
1222 | if (dyn.d_tag == DT_NULL) | |
1223 | break; | |
1224 | ||
b34976b6 | 1225 | stringp = FALSE; |
252b5132 RH |
1226 | switch (dyn.d_tag) |
1227 | { | |
1228 | default: | |
1229 | sprintf (ab, "0x%lx", (unsigned long) dyn.d_tag); | |
1230 | name = ab; | |
1231 | break; | |
1232 | ||
b34976b6 | 1233 | case DT_NEEDED: name = "NEEDED"; stringp = TRUE; break; |
252b5132 RH |
1234 | case DT_PLTRELSZ: name = "PLTRELSZ"; break; |
1235 | case DT_PLTGOT: name = "PLTGOT"; break; | |
1236 | case DT_HASH: name = "HASH"; break; | |
1237 | case DT_STRTAB: name = "STRTAB"; break; | |
1238 | case DT_SYMTAB: name = "SYMTAB"; break; | |
1239 | case DT_RELA: name = "RELA"; break; | |
1240 | case DT_RELASZ: name = "RELASZ"; break; | |
1241 | case DT_RELAENT: name = "RELAENT"; break; | |
1242 | case DT_STRSZ: name = "STRSZ"; break; | |
1243 | case DT_SYMENT: name = "SYMENT"; break; | |
1244 | case DT_INIT: name = "INIT"; break; | |
1245 | case DT_FINI: name = "FINI"; break; | |
b34976b6 AM |
1246 | case DT_SONAME: name = "SONAME"; stringp = TRUE; break; |
1247 | case DT_RPATH: name = "RPATH"; stringp = TRUE; break; | |
252b5132 RH |
1248 | case DT_SYMBOLIC: name = "SYMBOLIC"; break; |
1249 | case DT_REL: name = "REL"; break; | |
1250 | case DT_RELSZ: name = "RELSZ"; break; | |
1251 | case DT_RELENT: name = "RELENT"; break; | |
1252 | case DT_PLTREL: name = "PLTREL"; break; | |
1253 | case DT_DEBUG: name = "DEBUG"; break; | |
1254 | case DT_TEXTREL: name = "TEXTREL"; break; | |
1255 | case DT_JMPREL: name = "JMPREL"; break; | |
94558834 L |
1256 | case DT_BIND_NOW: name = "BIND_NOW"; break; |
1257 | case DT_INIT_ARRAY: name = "INIT_ARRAY"; break; | |
1258 | case DT_FINI_ARRAY: name = "FINI_ARRAY"; break; | |
1259 | case DT_INIT_ARRAYSZ: name = "INIT_ARRAYSZ"; break; | |
1260 | case DT_FINI_ARRAYSZ: name = "FINI_ARRAYSZ"; break; | |
b34976b6 | 1261 | case DT_RUNPATH: name = "RUNPATH"; stringp = TRUE; break; |
94558834 L |
1262 | case DT_FLAGS: name = "FLAGS"; break; |
1263 | case DT_PREINIT_ARRAY: name = "PREINIT_ARRAY"; break; | |
1264 | case DT_PREINIT_ARRAYSZ: name = "PREINIT_ARRAYSZ"; break; | |
d48188b9 | 1265 | case DT_CHECKSUM: name = "CHECKSUM"; break; |
94558834 L |
1266 | case DT_PLTPADSZ: name = "PLTPADSZ"; break; |
1267 | case DT_MOVEENT: name = "MOVEENT"; break; | |
1268 | case DT_MOVESZ: name = "MOVESZ"; break; | |
1269 | case DT_FEATURE: name = "FEATURE"; break; | |
1270 | case DT_POSFLAG_1: name = "POSFLAG_1"; break; | |
1271 | case DT_SYMINSZ: name = "SYMINSZ"; break; | |
1272 | case DT_SYMINENT: name = "SYMINENT"; break; | |
b34976b6 AM |
1273 | case DT_CONFIG: name = "CONFIG"; stringp = TRUE; break; |
1274 | case DT_DEPAUDIT: name = "DEPAUDIT"; stringp = TRUE; break; | |
1275 | case DT_AUDIT: name = "AUDIT"; stringp = TRUE; break; | |
94558834 L |
1276 | case DT_PLTPAD: name = "PLTPAD"; break; |
1277 | case DT_MOVETAB: name = "MOVETAB"; break; | |
1278 | case DT_SYMINFO: name = "SYMINFO"; break; | |
1279 | case DT_RELACOUNT: name = "RELACOUNT"; break; | |
1280 | case DT_RELCOUNT: name = "RELCOUNT"; break; | |
1281 | case DT_FLAGS_1: name = "FLAGS_1"; break; | |
252b5132 RH |
1282 | case DT_VERSYM: name = "VERSYM"; break; |
1283 | case DT_VERDEF: name = "VERDEF"; break; | |
1284 | case DT_VERDEFNUM: name = "VERDEFNUM"; break; | |
1285 | case DT_VERNEED: name = "VERNEED"; break; | |
1286 | case DT_VERNEEDNUM: name = "VERNEEDNUM"; break; | |
b34976b6 | 1287 | case DT_AUXILIARY: name = "AUXILIARY"; stringp = TRUE; break; |
94558834 | 1288 | case DT_USED: name = "USED"; break; |
b34976b6 | 1289 | case DT_FILTER: name = "FILTER"; stringp = TRUE; break; |
fdc90cb4 | 1290 | case DT_GNU_HASH: name = "GNU_HASH"; break; |
252b5132 RH |
1291 | } |
1292 | ||
1293 | fprintf (f, " %-11s ", name); | |
1294 | if (! stringp) | |
1295 | fprintf (f, "0x%lx", (unsigned long) dyn.d_un.d_val); | |
1296 | else | |
1297 | { | |
1298 | const char *string; | |
dc810e39 | 1299 | unsigned int tagv = dyn.d_un.d_val; |
252b5132 | 1300 | |
dc810e39 | 1301 | string = bfd_elf_string_from_elf_section (abfd, shlink, tagv); |
252b5132 RH |
1302 | if (string == NULL) |
1303 | goto error_return; | |
1304 | fprintf (f, "%s", string); | |
1305 | } | |
1306 | fprintf (f, "\n"); | |
1307 | } | |
1308 | ||
1309 | free (dynbuf); | |
1310 | dynbuf = NULL; | |
1311 | } | |
1312 | ||
1313 | if ((elf_dynverdef (abfd) != 0 && elf_tdata (abfd)->verdef == NULL) | |
1314 | || (elf_dynverref (abfd) != 0 && elf_tdata (abfd)->verref == NULL)) | |
1315 | { | |
fc0e6df6 | 1316 | if (! _bfd_elf_slurp_version_tables (abfd, FALSE)) |
b34976b6 | 1317 | return FALSE; |
252b5132 RH |
1318 | } |
1319 | ||
1320 | if (elf_dynverdef (abfd) != 0) | |
1321 | { | |
1322 | Elf_Internal_Verdef *t; | |
1323 | ||
1324 | fprintf (f, _("\nVersion definitions:\n")); | |
1325 | for (t = elf_tdata (abfd)->verdef; t != NULL; t = t->vd_nextdef) | |
1326 | { | |
1327 | fprintf (f, "%d 0x%2.2x 0x%8.8lx %s\n", t->vd_ndx, | |
d0fb9a8d JJ |
1328 | t->vd_flags, t->vd_hash, |
1329 | t->vd_nodename ? t->vd_nodename : "<corrupt>"); | |
1330 | if (t->vd_auxptr != NULL && t->vd_auxptr->vda_nextptr != NULL) | |
252b5132 RH |
1331 | { |
1332 | Elf_Internal_Verdaux *a; | |
1333 | ||
1334 | fprintf (f, "\t"); | |
1335 | for (a = t->vd_auxptr->vda_nextptr; | |
1336 | a != NULL; | |
1337 | a = a->vda_nextptr) | |
d0fb9a8d JJ |
1338 | fprintf (f, "%s ", |
1339 | a->vda_nodename ? a->vda_nodename : "<corrupt>"); | |
252b5132 RH |
1340 | fprintf (f, "\n"); |
1341 | } | |
1342 | } | |
1343 | } | |
1344 | ||
1345 | if (elf_dynverref (abfd) != 0) | |
1346 | { | |
1347 | Elf_Internal_Verneed *t; | |
1348 | ||
1349 | fprintf (f, _("\nVersion References:\n")); | |
1350 | for (t = elf_tdata (abfd)->verref; t != NULL; t = t->vn_nextref) | |
1351 | { | |
1352 | Elf_Internal_Vernaux *a; | |
1353 | ||
d0fb9a8d JJ |
1354 | fprintf (f, _(" required from %s:\n"), |
1355 | t->vn_filename ? t->vn_filename : "<corrupt>"); | |
252b5132 RH |
1356 | for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) |
1357 | fprintf (f, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a->vna_hash, | |
d0fb9a8d JJ |
1358 | a->vna_flags, a->vna_other, |
1359 | a->vna_nodename ? a->vna_nodename : "<corrupt>"); | |
252b5132 RH |
1360 | } |
1361 | } | |
1362 | ||
b34976b6 | 1363 | return TRUE; |
252b5132 RH |
1364 | |
1365 | error_return: | |
1366 | if (dynbuf != NULL) | |
1367 | free (dynbuf); | |
b34976b6 | 1368 | return FALSE; |
252b5132 RH |
1369 | } |
1370 | ||
1371 | /* Display ELF-specific fields of a symbol. */ | |
1372 | ||
1373 | void | |
217aa764 AM |
1374 | bfd_elf_print_symbol (bfd *abfd, |
1375 | void *filep, | |
1376 | asymbol *symbol, | |
1377 | bfd_print_symbol_type how) | |
252b5132 | 1378 | { |
217aa764 | 1379 | FILE *file = filep; |
252b5132 RH |
1380 | switch (how) |
1381 | { | |
1382 | case bfd_print_symbol_name: | |
1383 | fprintf (file, "%s", symbol->name); | |
1384 | break; | |
1385 | case bfd_print_symbol_more: | |
1386 | fprintf (file, "elf "); | |
60b89a18 | 1387 | bfd_fprintf_vma (abfd, file, symbol->value); |
252b5132 RH |
1388 | fprintf (file, " %lx", (long) symbol->flags); |
1389 | break; | |
1390 | case bfd_print_symbol_all: | |
1391 | { | |
4e8a9624 AM |
1392 | const char *section_name; |
1393 | const char *name = NULL; | |
9c5bfbb7 | 1394 | const struct elf_backend_data *bed; |
7a13edea | 1395 | unsigned char st_other; |
dbb410c3 | 1396 | bfd_vma val; |
c044fabd | 1397 | |
252b5132 | 1398 | section_name = symbol->section ? symbol->section->name : "(*none*)"; |
587ff49e RH |
1399 | |
1400 | bed = get_elf_backend_data (abfd); | |
1401 | if (bed->elf_backend_print_symbol_all) | |
c044fabd | 1402 | name = (*bed->elf_backend_print_symbol_all) (abfd, filep, symbol); |
587ff49e RH |
1403 | |
1404 | if (name == NULL) | |
1405 | { | |
7ee38065 | 1406 | name = symbol->name; |
217aa764 | 1407 | bfd_print_symbol_vandf (abfd, file, symbol); |
587ff49e RH |
1408 | } |
1409 | ||
252b5132 RH |
1410 | fprintf (file, " %s\t", section_name); |
1411 | /* Print the "other" value for a symbol. For common symbols, | |
1412 | we've already printed the size; now print the alignment. | |
1413 | For other symbols, we have no specified alignment, and | |
1414 | we've printed the address; now print the size. */ | |
dbb410c3 AM |
1415 | if (bfd_is_com_section (symbol->section)) |
1416 | val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_value; | |
1417 | else | |
1418 | val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_size; | |
1419 | bfd_fprintf_vma (abfd, file, val); | |
252b5132 RH |
1420 | |
1421 | /* If we have version information, print it. */ | |
1422 | if (elf_tdata (abfd)->dynversym_section != 0 | |
1423 | && (elf_tdata (abfd)->dynverdef_section != 0 | |
1424 | || elf_tdata (abfd)->dynverref_section != 0)) | |
1425 | { | |
1426 | unsigned int vernum; | |
1427 | const char *version_string; | |
1428 | ||
1429 | vernum = ((elf_symbol_type *) symbol)->version & VERSYM_VERSION; | |
1430 | ||
1431 | if (vernum == 0) | |
1432 | version_string = ""; | |
1433 | else if (vernum == 1) | |
1434 | version_string = "Base"; | |
1435 | else if (vernum <= elf_tdata (abfd)->cverdefs) | |
1436 | version_string = | |
1437 | elf_tdata (abfd)->verdef[vernum - 1].vd_nodename; | |
1438 | else | |
1439 | { | |
1440 | Elf_Internal_Verneed *t; | |
1441 | ||
1442 | version_string = ""; | |
1443 | for (t = elf_tdata (abfd)->verref; | |
1444 | t != NULL; | |
1445 | t = t->vn_nextref) | |
1446 | { | |
1447 | Elf_Internal_Vernaux *a; | |
1448 | ||
1449 | for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) | |
1450 | { | |
1451 | if (a->vna_other == vernum) | |
1452 | { | |
1453 | version_string = a->vna_nodename; | |
1454 | break; | |
1455 | } | |
1456 | } | |
1457 | } | |
1458 | } | |
1459 | ||
1460 | if ((((elf_symbol_type *) symbol)->version & VERSYM_HIDDEN) == 0) | |
1461 | fprintf (file, " %-11s", version_string); | |
1462 | else | |
1463 | { | |
1464 | int i; | |
1465 | ||
1466 | fprintf (file, " (%s)", version_string); | |
1467 | for (i = 10 - strlen (version_string); i > 0; --i) | |
1468 | putc (' ', file); | |
1469 | } | |
1470 | } | |
1471 | ||
1472 | /* If the st_other field is not zero, print it. */ | |
7a13edea | 1473 | st_other = ((elf_symbol_type *) symbol)->internal_elf_sym.st_other; |
c044fabd | 1474 | |
7a13edea NC |
1475 | switch (st_other) |
1476 | { | |
1477 | case 0: break; | |
1478 | case STV_INTERNAL: fprintf (file, " .internal"); break; | |
1479 | case STV_HIDDEN: fprintf (file, " .hidden"); break; | |
1480 | case STV_PROTECTED: fprintf (file, " .protected"); break; | |
1481 | default: | |
1482 | /* Some other non-defined flags are also present, so print | |
1483 | everything hex. */ | |
1484 | fprintf (file, " 0x%02x", (unsigned int) st_other); | |
1485 | } | |
252b5132 | 1486 | |
587ff49e | 1487 | fprintf (file, " %s", name); |
252b5132 RH |
1488 | } |
1489 | break; | |
1490 | } | |
1491 | } | |
1492 | \f | |
1493 | /* Create an entry in an ELF linker hash table. */ | |
1494 | ||
1495 | struct bfd_hash_entry * | |
217aa764 AM |
1496 | _bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry, |
1497 | struct bfd_hash_table *table, | |
1498 | const char *string) | |
252b5132 | 1499 | { |
252b5132 RH |
1500 | /* Allocate the structure if it has not already been allocated by a |
1501 | subclass. */ | |
51b64d56 AM |
1502 | if (entry == NULL) |
1503 | { | |
1504 | entry = bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry)); | |
1505 | if (entry == NULL) | |
1506 | return entry; | |
1507 | } | |
252b5132 RH |
1508 | |
1509 | /* Call the allocation method of the superclass. */ | |
51b64d56 AM |
1510 | entry = _bfd_link_hash_newfunc (entry, table, string); |
1511 | if (entry != NULL) | |
252b5132 | 1512 | { |
51b64d56 AM |
1513 | struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry; |
1514 | struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table; | |
1515 | ||
252b5132 RH |
1516 | /* Set local fields. */ |
1517 | ret->indx = -1; | |
252b5132 | 1518 | ret->dynindx = -1; |
a6aa5195 AM |
1519 | ret->got = htab->init_got_refcount; |
1520 | ret->plt = htab->init_plt_refcount; | |
f6e332e6 AM |
1521 | memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry) |
1522 | - offsetof (struct elf_link_hash_entry, size))); | |
252b5132 RH |
1523 | /* Assume that we have been called by a non-ELF symbol reader. |
1524 | This flag is then reset by the code which reads an ELF input | |
1525 | file. This ensures that a symbol created by a non-ELF symbol | |
1526 | reader will have the flag set correctly. */ | |
f5385ebf | 1527 | ret->non_elf = 1; |
252b5132 RH |
1528 | } |
1529 | ||
51b64d56 | 1530 | return entry; |
252b5132 RH |
1531 | } |
1532 | ||
2920b85c | 1533 | /* Copy data from an indirect symbol to its direct symbol, hiding the |
0a991dfe | 1534 | old indirect symbol. Also used for copying flags to a weakdef. */ |
2920b85c | 1535 | |
c61b8717 | 1536 | void |
fcfa13d2 | 1537 | _bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info, |
217aa764 AM |
1538 | struct elf_link_hash_entry *dir, |
1539 | struct elf_link_hash_entry *ind) | |
2920b85c | 1540 | { |
fcfa13d2 | 1541 | struct elf_link_hash_table *htab; |
3c3e9281 | 1542 | |
2920b85c RH |
1543 | /* Copy down any references that we may have already seen to the |
1544 | symbol which just became indirect. */ | |
1545 | ||
f5385ebf AM |
1546 | dir->ref_dynamic |= ind->ref_dynamic; |
1547 | dir->ref_regular |= ind->ref_regular; | |
1548 | dir->ref_regular_nonweak |= ind->ref_regular_nonweak; | |
1549 | dir->non_got_ref |= ind->non_got_ref; | |
1550 | dir->needs_plt |= ind->needs_plt; | |
1551 | dir->pointer_equality_needed |= ind->pointer_equality_needed; | |
2920b85c | 1552 | |
1e370bd2 | 1553 | if (ind->root.type != bfd_link_hash_indirect) |
0a991dfe AM |
1554 | return; |
1555 | ||
51b64d56 | 1556 | /* Copy over the global and procedure linkage table refcount entries. |
2920b85c | 1557 | These may have been already set up by a check_relocs routine. */ |
fcfa13d2 AM |
1558 | htab = elf_hash_table (info); |
1559 | if (ind->got.refcount > htab->init_got_refcount.refcount) | |
2920b85c | 1560 | { |
fcfa13d2 AM |
1561 | if (dir->got.refcount < 0) |
1562 | dir->got.refcount = 0; | |
1563 | dir->got.refcount += ind->got.refcount; | |
1564 | ind->got.refcount = htab->init_got_refcount.refcount; | |
2920b85c | 1565 | } |
2920b85c | 1566 | |
fcfa13d2 | 1567 | if (ind->plt.refcount > htab->init_plt_refcount.refcount) |
2920b85c | 1568 | { |
fcfa13d2 AM |
1569 | if (dir->plt.refcount < 0) |
1570 | dir->plt.refcount = 0; | |
1571 | dir->plt.refcount += ind->plt.refcount; | |
1572 | ind->plt.refcount = htab->init_plt_refcount.refcount; | |
2920b85c | 1573 | } |
2920b85c | 1574 | |
fcfa13d2 | 1575 | if (ind->dynindx != -1) |
2920b85c | 1576 | { |
fcfa13d2 AM |
1577 | if (dir->dynindx != -1) |
1578 | _bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index); | |
2920b85c RH |
1579 | dir->dynindx = ind->dynindx; |
1580 | dir->dynstr_index = ind->dynstr_index; | |
1581 | ind->dynindx = -1; | |
1582 | ind->dynstr_index = 0; | |
1583 | } | |
2920b85c RH |
1584 | } |
1585 | ||
c61b8717 | 1586 | void |
217aa764 AM |
1587 | _bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info, |
1588 | struct elf_link_hash_entry *h, | |
1589 | bfd_boolean force_local) | |
2920b85c | 1590 | { |
a6aa5195 | 1591 | h->plt = elf_hash_table (info)->init_plt_offset; |
f5385ebf | 1592 | h->needs_plt = 0; |
e5094212 AM |
1593 | if (force_local) |
1594 | { | |
f5385ebf | 1595 | h->forced_local = 1; |
e5094212 AM |
1596 | if (h->dynindx != -1) |
1597 | { | |
1598 | h->dynindx = -1; | |
1599 | _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr, | |
1600 | h->dynstr_index); | |
1601 | } | |
1602 | } | |
2920b85c RH |
1603 | } |
1604 | ||
252b5132 RH |
1605 | /* Initialize an ELF linker hash table. */ |
1606 | ||
b34976b6 | 1607 | bfd_boolean |
217aa764 AM |
1608 | _bfd_elf_link_hash_table_init |
1609 | (struct elf_link_hash_table *table, | |
1610 | bfd *abfd, | |
1611 | struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *, | |
1612 | struct bfd_hash_table *, | |
66eb6687 AM |
1613 | const char *), |
1614 | unsigned int entsize) | |
252b5132 | 1615 | { |
b34976b6 | 1616 | bfd_boolean ret; |
a6aa5195 | 1617 | int can_refcount = get_elf_backend_data (abfd)->can_refcount; |
8ea2e4bd | 1618 | |
effdf42a | 1619 | memset (table, 0, sizeof * table); |
a6aa5195 AM |
1620 | table->init_got_refcount.refcount = can_refcount - 1; |
1621 | table->init_plt_refcount.refcount = can_refcount - 1; | |
1622 | table->init_got_offset.offset = -(bfd_vma) 1; | |
1623 | table->init_plt_offset.offset = -(bfd_vma) 1; | |
252b5132 RH |
1624 | /* The first dynamic symbol is a dummy. */ |
1625 | table->dynsymcount = 1; | |
73722af0 | 1626 | |
66eb6687 | 1627 | ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize); |
8ea2e4bd NC |
1628 | table->root.type = bfd_link_elf_hash_table; |
1629 | ||
1630 | return ret; | |
252b5132 RH |
1631 | } |
1632 | ||
1633 | /* Create an ELF linker hash table. */ | |
1634 | ||
1635 | struct bfd_link_hash_table * | |
217aa764 | 1636 | _bfd_elf_link_hash_table_create (bfd *abfd) |
252b5132 RH |
1637 | { |
1638 | struct elf_link_hash_table *ret; | |
dc810e39 | 1639 | bfd_size_type amt = sizeof (struct elf_link_hash_table); |
252b5132 | 1640 | |
217aa764 AM |
1641 | ret = bfd_malloc (amt); |
1642 | if (ret == NULL) | |
252b5132 RH |
1643 | return NULL; |
1644 | ||
66eb6687 AM |
1645 | if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc, |
1646 | sizeof (struct elf_link_hash_entry))) | |
252b5132 | 1647 | { |
e2d34d7d | 1648 | free (ret); |
252b5132 RH |
1649 | return NULL; |
1650 | } | |
1651 | ||
1652 | return &ret->root; | |
1653 | } | |
1654 | ||
1655 | /* This is a hook for the ELF emulation code in the generic linker to | |
1656 | tell the backend linker what file name to use for the DT_NEEDED | |
4a43e768 | 1657 | entry for a dynamic object. */ |
252b5132 RH |
1658 | |
1659 | void | |
217aa764 | 1660 | bfd_elf_set_dt_needed_name (bfd *abfd, const char *name) |
252b5132 RH |
1661 | { |
1662 | if (bfd_get_flavour (abfd) == bfd_target_elf_flavour | |
1663 | && bfd_get_format (abfd) == bfd_object) | |
1664 | elf_dt_name (abfd) = name; | |
1665 | } | |
1666 | ||
e56f61be L |
1667 | int |
1668 | bfd_elf_get_dyn_lib_class (bfd *abfd) | |
1669 | { | |
1670 | int lib_class; | |
1671 | if (bfd_get_flavour (abfd) == bfd_target_elf_flavour | |
1672 | && bfd_get_format (abfd) == bfd_object) | |
1673 | lib_class = elf_dyn_lib_class (abfd); | |
1674 | else | |
1675 | lib_class = 0; | |
1676 | return lib_class; | |
1677 | } | |
1678 | ||
74816898 | 1679 | void |
23fe9577 | 1680 | bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class) |
74816898 L |
1681 | { |
1682 | if (bfd_get_flavour (abfd) == bfd_target_elf_flavour | |
1683 | && bfd_get_format (abfd) == bfd_object) | |
4a43e768 | 1684 | elf_dyn_lib_class (abfd) = lib_class; |
74816898 L |
1685 | } |
1686 | ||
252b5132 RH |
1687 | /* Get the list of DT_NEEDED entries for a link. This is a hook for |
1688 | the linker ELF emulation code. */ | |
1689 | ||
1690 | struct bfd_link_needed_list * | |
217aa764 AM |
1691 | bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED, |
1692 | struct bfd_link_info *info) | |
252b5132 | 1693 | { |
0eddce27 | 1694 | if (! is_elf_hash_table (info->hash)) |
252b5132 RH |
1695 | return NULL; |
1696 | return elf_hash_table (info)->needed; | |
1697 | } | |
1698 | ||
a963dc6a L |
1699 | /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a |
1700 | hook for the linker ELF emulation code. */ | |
1701 | ||
1702 | struct bfd_link_needed_list * | |
217aa764 AM |
1703 | bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED, |
1704 | struct bfd_link_info *info) | |
a963dc6a | 1705 | { |
0eddce27 | 1706 | if (! is_elf_hash_table (info->hash)) |
a963dc6a L |
1707 | return NULL; |
1708 | return elf_hash_table (info)->runpath; | |
1709 | } | |
1710 | ||
252b5132 RH |
1711 | /* Get the name actually used for a dynamic object for a link. This |
1712 | is the SONAME entry if there is one. Otherwise, it is the string | |
1713 | passed to bfd_elf_set_dt_needed_name, or it is the filename. */ | |
1714 | ||
1715 | const char * | |
217aa764 | 1716 | bfd_elf_get_dt_soname (bfd *abfd) |
252b5132 RH |
1717 | { |
1718 | if (bfd_get_flavour (abfd) == bfd_target_elf_flavour | |
1719 | && bfd_get_format (abfd) == bfd_object) | |
1720 | return elf_dt_name (abfd); | |
1721 | return NULL; | |
1722 | } | |
1723 | ||
1724 | /* Get the list of DT_NEEDED entries from a BFD. This is a hook for | |
1725 | the ELF linker emulation code. */ | |
1726 | ||
b34976b6 | 1727 | bfd_boolean |
217aa764 AM |
1728 | bfd_elf_get_bfd_needed_list (bfd *abfd, |
1729 | struct bfd_link_needed_list **pneeded) | |
252b5132 RH |
1730 | { |
1731 | asection *s; | |
1732 | bfd_byte *dynbuf = NULL; | |
1733 | int elfsec; | |
dc810e39 | 1734 | unsigned long shlink; |
252b5132 RH |
1735 | bfd_byte *extdyn, *extdynend; |
1736 | size_t extdynsize; | |
217aa764 | 1737 | void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *); |
252b5132 RH |
1738 | |
1739 | *pneeded = NULL; | |
1740 | ||
1741 | if (bfd_get_flavour (abfd) != bfd_target_elf_flavour | |
1742 | || bfd_get_format (abfd) != bfd_object) | |
b34976b6 | 1743 | return TRUE; |
252b5132 RH |
1744 | |
1745 | s = bfd_get_section_by_name (abfd, ".dynamic"); | |
eea6121a | 1746 | if (s == NULL || s->size == 0) |
b34976b6 | 1747 | return TRUE; |
252b5132 | 1748 | |
eea6121a | 1749 | if (!bfd_malloc_and_get_section (abfd, s, &dynbuf)) |
252b5132 RH |
1750 | goto error_return; |
1751 | ||
1752 | elfsec = _bfd_elf_section_from_bfd_section (abfd, s); | |
1753 | if (elfsec == -1) | |
1754 | goto error_return; | |
1755 | ||
dc810e39 | 1756 | shlink = elf_elfsections (abfd)[elfsec]->sh_link; |
252b5132 RH |
1757 | |
1758 | extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn; | |
1759 | swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in; | |
1760 | ||
1761 | extdyn = dynbuf; | |
eea6121a | 1762 | extdynend = extdyn + s->size; |
252b5132 RH |
1763 | for (; extdyn < extdynend; extdyn += extdynsize) |
1764 | { | |
1765 | Elf_Internal_Dyn dyn; | |
1766 | ||
217aa764 | 1767 | (*swap_dyn_in) (abfd, extdyn, &dyn); |
252b5132 RH |
1768 | |
1769 | if (dyn.d_tag == DT_NULL) | |
1770 | break; | |
1771 | ||
1772 | if (dyn.d_tag == DT_NEEDED) | |
1773 | { | |
1774 | const char *string; | |
1775 | struct bfd_link_needed_list *l; | |
dc810e39 AM |
1776 | unsigned int tagv = dyn.d_un.d_val; |
1777 | bfd_size_type amt; | |
252b5132 | 1778 | |
dc810e39 | 1779 | string = bfd_elf_string_from_elf_section (abfd, shlink, tagv); |
252b5132 RH |
1780 | if (string == NULL) |
1781 | goto error_return; | |
1782 | ||
dc810e39 | 1783 | amt = sizeof *l; |
217aa764 | 1784 | l = bfd_alloc (abfd, amt); |
252b5132 RH |
1785 | if (l == NULL) |
1786 | goto error_return; | |
1787 | ||
1788 | l->by = abfd; | |
1789 | l->name = string; | |
1790 | l->next = *pneeded; | |
1791 | *pneeded = l; | |
1792 | } | |
1793 | } | |
1794 | ||
1795 | free (dynbuf); | |
1796 | ||
b34976b6 | 1797 | return TRUE; |
252b5132 RH |
1798 | |
1799 | error_return: | |
1800 | if (dynbuf != NULL) | |
1801 | free (dynbuf); | |
b34976b6 | 1802 | return FALSE; |
252b5132 RH |
1803 | } |
1804 | \f | |
1805 | /* Allocate an ELF string table--force the first byte to be zero. */ | |
1806 | ||
1807 | struct bfd_strtab_hash * | |
217aa764 | 1808 | _bfd_elf_stringtab_init (void) |
252b5132 RH |
1809 | { |
1810 | struct bfd_strtab_hash *ret; | |
1811 | ||
1812 | ret = _bfd_stringtab_init (); | |
1813 | if (ret != NULL) | |
1814 | { | |
1815 | bfd_size_type loc; | |
1816 | ||
b34976b6 | 1817 | loc = _bfd_stringtab_add (ret, "", TRUE, FALSE); |
252b5132 RH |
1818 | BFD_ASSERT (loc == 0 || loc == (bfd_size_type) -1); |
1819 | if (loc == (bfd_size_type) -1) | |
1820 | { | |
1821 | _bfd_stringtab_free (ret); | |
1822 | ret = NULL; | |
1823 | } | |
1824 | } | |
1825 | return ret; | |
1826 | } | |
1827 | \f | |
1828 | /* ELF .o/exec file reading */ | |
1829 | ||
c044fabd | 1830 | /* Create a new bfd section from an ELF section header. */ |
252b5132 | 1831 | |
b34976b6 | 1832 | bfd_boolean |
217aa764 | 1833 | bfd_section_from_shdr (bfd *abfd, unsigned int shindex) |
252b5132 RH |
1834 | { |
1835 | Elf_Internal_Shdr *hdr = elf_elfsections (abfd)[shindex]; | |
1836 | Elf_Internal_Ehdr *ehdr = elf_elfheader (abfd); | |
9c5bfbb7 | 1837 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
90937f86 | 1838 | const char *name; |
252b5132 | 1839 | |
1b3a8575 AM |
1840 | name = bfd_elf_string_from_elf_section (abfd, |
1841 | elf_elfheader (abfd)->e_shstrndx, | |
1842 | hdr->sh_name); | |
933d961a JJ |
1843 | if (name == NULL) |
1844 | return FALSE; | |
252b5132 RH |
1845 | |
1846 | switch (hdr->sh_type) | |
1847 | { | |
1848 | case SHT_NULL: | |
1849 | /* Inactive section. Throw it away. */ | |
b34976b6 | 1850 | return TRUE; |
252b5132 RH |
1851 | |
1852 | case SHT_PROGBITS: /* Normal section with contents. */ | |
252b5132 RH |
1853 | case SHT_NOBITS: /* .bss section. */ |
1854 | case SHT_HASH: /* .hash section. */ | |
1855 | case SHT_NOTE: /* .note section. */ | |
25e27870 L |
1856 | case SHT_INIT_ARRAY: /* .init_array section. */ |
1857 | case SHT_FINI_ARRAY: /* .fini_array section. */ | |
1858 | case SHT_PREINIT_ARRAY: /* .preinit_array section. */ | |
7f1204bb | 1859 | case SHT_GNU_LIBLIST: /* .gnu.liblist section. */ |
fdc90cb4 | 1860 | case SHT_GNU_HASH: /* .gnu.hash section. */ |
6dc132d9 | 1861 | return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); |
252b5132 | 1862 | |
797fc050 | 1863 | case SHT_DYNAMIC: /* Dynamic linking information. */ |
6dc132d9 | 1864 | if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex)) |
b34976b6 | 1865 | return FALSE; |
8e0ed13f NC |
1866 | if (hdr->sh_link > elf_numsections (abfd) |
1867 | || elf_elfsections (abfd)[hdr->sh_link] == NULL) | |
1868 | return FALSE; | |
797fc050 AM |
1869 | if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_STRTAB) |
1870 | { | |
1871 | Elf_Internal_Shdr *dynsymhdr; | |
1872 | ||
1873 | /* The shared libraries distributed with hpux11 have a bogus | |
1874 | sh_link field for the ".dynamic" section. Find the | |
1875 | string table for the ".dynsym" section instead. */ | |
1876 | if (elf_dynsymtab (abfd) != 0) | |
1877 | { | |
1878 | dynsymhdr = elf_elfsections (abfd)[elf_dynsymtab (abfd)]; | |
1879 | hdr->sh_link = dynsymhdr->sh_link; | |
1880 | } | |
1881 | else | |
1882 | { | |
1883 | unsigned int i, num_sec; | |
1884 | ||
1885 | num_sec = elf_numsections (abfd); | |
1886 | for (i = 1; i < num_sec; i++) | |
1887 | { | |
1888 | dynsymhdr = elf_elfsections (abfd)[i]; | |
1889 | if (dynsymhdr->sh_type == SHT_DYNSYM) | |
1890 | { | |
1891 | hdr->sh_link = dynsymhdr->sh_link; | |
1892 | break; | |
1893 | } | |
1894 | } | |
1895 | } | |
1896 | } | |
1897 | break; | |
1898 | ||
252b5132 RH |
1899 | case SHT_SYMTAB: /* A symbol table */ |
1900 | if (elf_onesymtab (abfd) == shindex) | |
b34976b6 | 1901 | return TRUE; |
252b5132 | 1902 | |
a50b2160 JJ |
1903 | if (hdr->sh_entsize != bed->s->sizeof_sym) |
1904 | return FALSE; | |
252b5132 RH |
1905 | BFD_ASSERT (elf_onesymtab (abfd) == 0); |
1906 | elf_onesymtab (abfd) = shindex; | |
1907 | elf_tdata (abfd)->symtab_hdr = *hdr; | |
1908 | elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->symtab_hdr; | |
1909 | abfd->flags |= HAS_SYMS; | |
1910 | ||
1911 | /* Sometimes a shared object will map in the symbol table. If | |
1912 | SHF_ALLOC is set, and this is a shared object, then we also | |
1913 | treat this section as a BFD section. We can not base the | |
1914 | decision purely on SHF_ALLOC, because that flag is sometimes | |
1049f94e | 1915 | set in a relocatable object file, which would confuse the |
252b5132 RH |
1916 | linker. */ |
1917 | if ((hdr->sh_flags & SHF_ALLOC) != 0 | |
1918 | && (abfd->flags & DYNAMIC) != 0 | |
6dc132d9 L |
1919 | && ! _bfd_elf_make_section_from_shdr (abfd, hdr, name, |
1920 | shindex)) | |
b34976b6 | 1921 | return FALSE; |
252b5132 | 1922 | |
1b3a8575 AM |
1923 | /* Go looking for SHT_SYMTAB_SHNDX too, since if there is one we |
1924 | can't read symbols without that section loaded as well. It | |
1925 | is most likely specified by the next section header. */ | |
1926 | if (elf_elfsections (abfd)[elf_symtab_shndx (abfd)]->sh_link != shindex) | |
1927 | { | |
1928 | unsigned int i, num_sec; | |
1929 | ||
1930 | num_sec = elf_numsections (abfd); | |
1931 | for (i = shindex + 1; i < num_sec; i++) | |
1932 | { | |
1933 | Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i]; | |
1934 | if (hdr2->sh_type == SHT_SYMTAB_SHNDX | |
1935 | && hdr2->sh_link == shindex) | |
1936 | break; | |
1937 | } | |
1938 | if (i == num_sec) | |
1939 | for (i = 1; i < shindex; i++) | |
1940 | { | |
1941 | Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i]; | |
1942 | if (hdr2->sh_type == SHT_SYMTAB_SHNDX | |
1943 | && hdr2->sh_link == shindex) | |
1944 | break; | |
1945 | } | |
1946 | if (i != shindex) | |
1947 | return bfd_section_from_shdr (abfd, i); | |
1948 | } | |
b34976b6 | 1949 | return TRUE; |
252b5132 RH |
1950 | |
1951 | case SHT_DYNSYM: /* A dynamic symbol table */ | |
1952 | if (elf_dynsymtab (abfd) == shindex) | |
b34976b6 | 1953 | return TRUE; |
252b5132 | 1954 | |
a50b2160 JJ |
1955 | if (hdr->sh_entsize != bed->s->sizeof_sym) |
1956 | return FALSE; | |
252b5132 RH |
1957 | BFD_ASSERT (elf_dynsymtab (abfd) == 0); |
1958 | elf_dynsymtab (abfd) = shindex; | |
1959 | elf_tdata (abfd)->dynsymtab_hdr = *hdr; | |
1960 | elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->dynsymtab_hdr; | |
1961 | abfd->flags |= HAS_SYMS; | |
1962 | ||
1963 | /* Besides being a symbol table, we also treat this as a regular | |
1964 | section, so that objcopy can handle it. */ | |
6dc132d9 | 1965 | return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); |
252b5132 | 1966 | |
9ad5cbcf AM |
1967 | case SHT_SYMTAB_SHNDX: /* Symbol section indices when >64k sections */ |
1968 | if (elf_symtab_shndx (abfd) == shindex) | |
b34976b6 | 1969 | return TRUE; |
9ad5cbcf | 1970 | |
1b3a8575 | 1971 | BFD_ASSERT (elf_symtab_shndx (abfd) == 0); |
9ad5cbcf AM |
1972 | elf_symtab_shndx (abfd) = shindex; |
1973 | elf_tdata (abfd)->symtab_shndx_hdr = *hdr; | |
1974 | elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->symtab_shndx_hdr; | |
b34976b6 | 1975 | return TRUE; |
9ad5cbcf | 1976 | |
252b5132 RH |
1977 | case SHT_STRTAB: /* A string table */ |
1978 | if (hdr->bfd_section != NULL) | |
b34976b6 | 1979 | return TRUE; |
252b5132 RH |
1980 | if (ehdr->e_shstrndx == shindex) |
1981 | { | |
1982 | elf_tdata (abfd)->shstrtab_hdr = *hdr; | |
1983 | elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->shstrtab_hdr; | |
b34976b6 | 1984 | return TRUE; |
252b5132 | 1985 | } |
1b3a8575 AM |
1986 | if (elf_elfsections (abfd)[elf_onesymtab (abfd)]->sh_link == shindex) |
1987 | { | |
1988 | symtab_strtab: | |
1989 | elf_tdata (abfd)->strtab_hdr = *hdr; | |
1990 | elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->strtab_hdr; | |
1991 | return TRUE; | |
1992 | } | |
1993 | if (elf_elfsections (abfd)[elf_dynsymtab (abfd)]->sh_link == shindex) | |
1994 | { | |
1995 | dynsymtab_strtab: | |
1996 | elf_tdata (abfd)->dynstrtab_hdr = *hdr; | |
1997 | hdr = &elf_tdata (abfd)->dynstrtab_hdr; | |
1998 | elf_elfsections (abfd)[shindex] = hdr; | |
1999 | /* We also treat this as a regular section, so that objcopy | |
2000 | can handle it. */ | |
6dc132d9 L |
2001 | return _bfd_elf_make_section_from_shdr (abfd, hdr, name, |
2002 | shindex); | |
1b3a8575 | 2003 | } |
252b5132 | 2004 | |
1b3a8575 AM |
2005 | /* If the string table isn't one of the above, then treat it as a |
2006 | regular section. We need to scan all the headers to be sure, | |
2007 | just in case this strtab section appeared before the above. */ | |
2008 | if (elf_onesymtab (abfd) == 0 || elf_dynsymtab (abfd) == 0) | |
2009 | { | |
2010 | unsigned int i, num_sec; | |
252b5132 | 2011 | |
1b3a8575 AM |
2012 | num_sec = elf_numsections (abfd); |
2013 | for (i = 1; i < num_sec; i++) | |
2014 | { | |
2015 | Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i]; | |
2016 | if (hdr2->sh_link == shindex) | |
2017 | { | |
933d961a JJ |
2018 | /* Prevent endless recursion on broken objects. */ |
2019 | if (i == shindex) | |
2020 | return FALSE; | |
1b3a8575 AM |
2021 | if (! bfd_section_from_shdr (abfd, i)) |
2022 | return FALSE; | |
2023 | if (elf_onesymtab (abfd) == i) | |
2024 | goto symtab_strtab; | |
2025 | if (elf_dynsymtab (abfd) == i) | |
2026 | goto dynsymtab_strtab; | |
2027 | } | |
2028 | } | |
2029 | } | |
6dc132d9 | 2030 | return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); |
252b5132 RH |
2031 | |
2032 | case SHT_REL: | |
2033 | case SHT_RELA: | |
2034 | /* *These* do a lot of work -- but build no sections! */ | |
2035 | { | |
2036 | asection *target_sect; | |
2037 | Elf_Internal_Shdr *hdr2; | |
9ad5cbcf | 2038 | unsigned int num_sec = elf_numsections (abfd); |
252b5132 | 2039 | |
aa2ca951 JJ |
2040 | if (hdr->sh_entsize |
2041 | != (bfd_size_type) (hdr->sh_type == SHT_REL | |
a50b2160 JJ |
2042 | ? bed->s->sizeof_rel : bed->s->sizeof_rela)) |
2043 | return FALSE; | |
2044 | ||
03ae5f59 | 2045 | /* Check for a bogus link to avoid crashing. */ |
9ad5cbcf AM |
2046 | if ((hdr->sh_link >= SHN_LORESERVE && hdr->sh_link <= SHN_HIRESERVE) |
2047 | || hdr->sh_link >= num_sec) | |
03ae5f59 ILT |
2048 | { |
2049 | ((*_bfd_error_handler) | |
d003868e AM |
2050 | (_("%B: invalid link %lu for reloc section %s (index %u)"), |
2051 | abfd, hdr->sh_link, name, shindex)); | |
6dc132d9 L |
2052 | return _bfd_elf_make_section_from_shdr (abfd, hdr, name, |
2053 | shindex); | |
03ae5f59 ILT |
2054 | } |
2055 | ||
252b5132 RH |
2056 | /* For some incomprehensible reason Oracle distributes |
2057 | libraries for Solaris in which some of the objects have | |
2058 | bogus sh_link fields. It would be nice if we could just | |
2059 | reject them, but, unfortunately, some people need to use | |
2060 | them. We scan through the section headers; if we find only | |
2061 | one suitable symbol table, we clobber the sh_link to point | |
2062 | to it. I hope this doesn't break anything. */ | |
2063 | if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_SYMTAB | |
2064 | && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_DYNSYM) | |
2065 | { | |
9ad5cbcf | 2066 | unsigned int scan; |
252b5132 RH |
2067 | int found; |
2068 | ||
2069 | found = 0; | |
9ad5cbcf | 2070 | for (scan = 1; scan < num_sec; scan++) |
252b5132 RH |
2071 | { |
2072 | if (elf_elfsections (abfd)[scan]->sh_type == SHT_SYMTAB | |
2073 | || elf_elfsections (abfd)[scan]->sh_type == SHT_DYNSYM) | |
2074 | { | |
2075 | if (found != 0) | |
2076 | { | |
2077 | found = 0; | |
2078 | break; | |
2079 | } | |
2080 | found = scan; | |
2081 | } | |
2082 | } | |
2083 | if (found != 0) | |
2084 | hdr->sh_link = found; | |
2085 | } | |
2086 | ||
2087 | /* Get the symbol table. */ | |
1b3a8575 AM |
2088 | if ((elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_SYMTAB |
2089 | || elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_DYNSYM) | |
252b5132 | 2090 | && ! bfd_section_from_shdr (abfd, hdr->sh_link)) |
b34976b6 | 2091 | return FALSE; |
252b5132 RH |
2092 | |
2093 | /* If this reloc section does not use the main symbol table we | |
2094 | don't treat it as a reloc section. BFD can't adequately | |
2095 | represent such a section, so at least for now, we don't | |
c044fabd | 2096 | try. We just present it as a normal section. We also |
60bcf0fa | 2097 | can't use it as a reloc section if it points to the null |
185ef66d AM |
2098 | section, an invalid section, or another reloc section. */ |
2099 | if (hdr->sh_link != elf_onesymtab (abfd) | |
2100 | || hdr->sh_info == SHN_UNDEF | |
2101 | || (hdr->sh_info >= SHN_LORESERVE && hdr->sh_info <= SHN_HIRESERVE) | |
2102 | || hdr->sh_info >= num_sec | |
2103 | || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_REL | |
2104 | || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_RELA) | |
6dc132d9 L |
2105 | return _bfd_elf_make_section_from_shdr (abfd, hdr, name, |
2106 | shindex); | |
252b5132 RH |
2107 | |
2108 | if (! bfd_section_from_shdr (abfd, hdr->sh_info)) | |
b34976b6 | 2109 | return FALSE; |
252b5132 RH |
2110 | target_sect = bfd_section_from_elf_index (abfd, hdr->sh_info); |
2111 | if (target_sect == NULL) | |
b34976b6 | 2112 | return FALSE; |
252b5132 RH |
2113 | |
2114 | if ((target_sect->flags & SEC_RELOC) == 0 | |
2115 | || target_sect->reloc_count == 0) | |
2116 | hdr2 = &elf_section_data (target_sect)->rel_hdr; | |
2117 | else | |
2118 | { | |
dc810e39 | 2119 | bfd_size_type amt; |
252b5132 | 2120 | BFD_ASSERT (elf_section_data (target_sect)->rel_hdr2 == NULL); |
dc810e39 | 2121 | amt = sizeof (*hdr2); |
217aa764 | 2122 | hdr2 = bfd_alloc (abfd, amt); |
252b5132 RH |
2123 | elf_section_data (target_sect)->rel_hdr2 = hdr2; |
2124 | } | |
2125 | *hdr2 = *hdr; | |
2126 | elf_elfsections (abfd)[shindex] = hdr2; | |
d9bc7a44 | 2127 | target_sect->reloc_count += NUM_SHDR_ENTRIES (hdr); |
252b5132 RH |
2128 | target_sect->flags |= SEC_RELOC; |
2129 | target_sect->relocation = NULL; | |
2130 | target_sect->rel_filepos = hdr->sh_offset; | |
bf572ba0 MM |
2131 | /* In the section to which the relocations apply, mark whether |
2132 | its relocations are of the REL or RELA variety. */ | |
72730e0c | 2133 | if (hdr->sh_size != 0) |
68bfbfcc | 2134 | target_sect->use_rela_p = hdr->sh_type == SHT_RELA; |
252b5132 | 2135 | abfd->flags |= HAS_RELOC; |
b34976b6 | 2136 | return TRUE; |
252b5132 | 2137 | } |
252b5132 RH |
2138 | |
2139 | case SHT_GNU_verdef: | |
2140 | elf_dynverdef (abfd) = shindex; | |
2141 | elf_tdata (abfd)->dynverdef_hdr = *hdr; | |
6dc132d9 | 2142 | return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); |
252b5132 RH |
2143 | |
2144 | case SHT_GNU_versym: | |
a50b2160 JJ |
2145 | if (hdr->sh_entsize != sizeof (Elf_External_Versym)) |
2146 | return FALSE; | |
252b5132 RH |
2147 | elf_dynversym (abfd) = shindex; |
2148 | elf_tdata (abfd)->dynversym_hdr = *hdr; | |
6dc132d9 | 2149 | return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); |
252b5132 RH |
2150 | |
2151 | case SHT_GNU_verneed: | |
2152 | elf_dynverref (abfd) = shindex; | |
2153 | elf_tdata (abfd)->dynverref_hdr = *hdr; | |
6dc132d9 | 2154 | return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); |
252b5132 RH |
2155 | |
2156 | case SHT_SHLIB: | |
b34976b6 | 2157 | return TRUE; |
252b5132 | 2158 | |
dbb410c3 | 2159 | case SHT_GROUP: |
b885599b AM |
2160 | /* We need a BFD section for objcopy and relocatable linking, |
2161 | and it's handy to have the signature available as the section | |
2162 | name. */ | |
1783205a | 2163 | if (! IS_VALID_GROUP_SECTION_HEADER (hdr)) |
a50b2160 | 2164 | return FALSE; |
b885599b AM |
2165 | name = group_signature (abfd, hdr); |
2166 | if (name == NULL) | |
b34976b6 | 2167 | return FALSE; |
6dc132d9 | 2168 | if (!_bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex)) |
b34976b6 | 2169 | return FALSE; |
dbb410c3 AM |
2170 | if (hdr->contents != NULL) |
2171 | { | |
2172 | Elf_Internal_Group *idx = (Elf_Internal_Group *) hdr->contents; | |
1783205a | 2173 | unsigned int n_elt = hdr->sh_size / GRP_ENTRY_SIZE; |
dbb410c3 AM |
2174 | asection *s; |
2175 | ||
b885599b AM |
2176 | if (idx->flags & GRP_COMDAT) |
2177 | hdr->bfd_section->flags | |
2178 | |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD; | |
2179 | ||
45c5e9ed L |
2180 | /* We try to keep the same section order as it comes in. */ |
2181 | idx += n_elt; | |
dbb410c3 | 2182 | while (--n_elt != 0) |
1783205a NC |
2183 | { |
2184 | --idx; | |
2185 | ||
2186 | if (idx->shdr != NULL | |
2187 | && (s = idx->shdr->bfd_section) != NULL | |
2188 | && elf_next_in_group (s) != NULL) | |
2189 | { | |
2190 | elf_next_in_group (hdr->bfd_section) = s; | |
2191 | break; | |
2192 | } | |
2193 | } | |
dbb410c3 AM |
2194 | } |
2195 | break; | |
2196 | ||
252b5132 RH |
2197 | default: |
2198 | /* Check for any processor-specific section types. */ | |
3eb70a79 L |
2199 | if (bed->elf_backend_section_from_shdr (abfd, hdr, name, shindex)) |
2200 | return TRUE; | |
2201 | ||
2202 | if (hdr->sh_type >= SHT_LOUSER && hdr->sh_type <= SHT_HIUSER) | |
2203 | { | |
2204 | if ((hdr->sh_flags & SHF_ALLOC) != 0) | |
2205 | /* FIXME: How to properly handle allocated section reserved | |
2206 | for applications? */ | |
2207 | (*_bfd_error_handler) | |
2208 | (_("%B: don't know how to handle allocated, application " | |
2209 | "specific section `%s' [0x%8x]"), | |
2210 | abfd, name, hdr->sh_type); | |
2211 | else | |
2212 | /* Allow sections reserved for applications. */ | |
2213 | return _bfd_elf_make_section_from_shdr (abfd, hdr, name, | |
2214 | shindex); | |
2215 | } | |
2216 | else if (hdr->sh_type >= SHT_LOPROC | |
2217 | && hdr->sh_type <= SHT_HIPROC) | |
2218 | /* FIXME: We should handle this section. */ | |
2219 | (*_bfd_error_handler) | |
2220 | (_("%B: don't know how to handle processor specific section " | |
2221 | "`%s' [0x%8x]"), | |
2222 | abfd, name, hdr->sh_type); | |
2223 | else if (hdr->sh_type >= SHT_LOOS && hdr->sh_type <= SHT_HIOS) | |
ff15b240 NC |
2224 | { |
2225 | /* Unrecognised OS-specific sections. */ | |
2226 | if ((hdr->sh_flags & SHF_OS_NONCONFORMING) != 0) | |
2227 | /* SHF_OS_NONCONFORMING indicates that special knowledge is | |
2228 | required to correctly process the section and the file should | |
2229 | be rejected with an error message. */ | |
2230 | (*_bfd_error_handler) | |
2231 | (_("%B: don't know how to handle OS specific section " | |
2232 | "`%s' [0x%8x]"), | |
2233 | abfd, name, hdr->sh_type); | |
2234 | else | |
2235 | /* Otherwise it should be processed. */ | |
2236 | return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); | |
2237 | } | |
3eb70a79 L |
2238 | else |
2239 | /* FIXME: We should handle this section. */ | |
2240 | (*_bfd_error_handler) | |
2241 | (_("%B: don't know how to handle section `%s' [0x%8x]"), | |
2242 | abfd, name, hdr->sh_type); | |
2243 | ||
2244 | return FALSE; | |
252b5132 RH |
2245 | } |
2246 | ||
b34976b6 | 2247 | return TRUE; |
252b5132 RH |
2248 | } |
2249 | ||
ec338859 AM |
2250 | /* Return the section for the local symbol specified by ABFD, R_SYMNDX. |
2251 | Return SEC for sections that have no elf section, and NULL on error. */ | |
2252 | ||
2253 | asection * | |
217aa764 AM |
2254 | bfd_section_from_r_symndx (bfd *abfd, |
2255 | struct sym_sec_cache *cache, | |
2256 | asection *sec, | |
2257 | unsigned long r_symndx) | |
ec338859 | 2258 | { |
ec338859 | 2259 | Elf_Internal_Shdr *symtab_hdr; |
6cdc0ccc AM |
2260 | unsigned char esym[sizeof (Elf64_External_Sym)]; |
2261 | Elf_External_Sym_Shndx eshndx; | |
2262 | Elf_Internal_Sym isym; | |
ec338859 AM |
2263 | unsigned int ent = r_symndx % LOCAL_SYM_CACHE_SIZE; |
2264 | ||
2265 | if (cache->abfd == abfd && cache->indx[ent] == r_symndx) | |
2266 | return cache->sec[ent]; | |
2267 | ||
2268 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; | |
6cdc0ccc AM |
2269 | if (bfd_elf_get_elf_syms (abfd, symtab_hdr, 1, r_symndx, |
2270 | &isym, esym, &eshndx) == NULL) | |
ec338859 | 2271 | return NULL; |
9ad5cbcf | 2272 | |
ec338859 AM |
2273 | if (cache->abfd != abfd) |
2274 | { | |
2275 | memset (cache->indx, -1, sizeof (cache->indx)); | |
2276 | cache->abfd = abfd; | |
2277 | } | |
2278 | cache->indx[ent] = r_symndx; | |
2279 | cache->sec[ent] = sec; | |
50bc7936 AM |
2280 | if ((isym.st_shndx != SHN_UNDEF && isym.st_shndx < SHN_LORESERVE) |
2281 | || isym.st_shndx > SHN_HIRESERVE) | |
ec338859 AM |
2282 | { |
2283 | asection *s; | |
6cdc0ccc | 2284 | s = bfd_section_from_elf_index (abfd, isym.st_shndx); |
ec338859 AM |
2285 | if (s != NULL) |
2286 | cache->sec[ent] = s; | |
2287 | } | |
2288 | return cache->sec[ent]; | |
2289 | } | |
2290 | ||
252b5132 RH |
2291 | /* Given an ELF section number, retrieve the corresponding BFD |
2292 | section. */ | |
2293 | ||
2294 | asection * | |
217aa764 | 2295 | bfd_section_from_elf_index (bfd *abfd, unsigned int index) |
252b5132 | 2296 | { |
9ad5cbcf | 2297 | if (index >= elf_numsections (abfd)) |
252b5132 RH |
2298 | return NULL; |
2299 | return elf_elfsections (abfd)[index]->bfd_section; | |
2300 | } | |
2301 | ||
b35d266b | 2302 | static const struct bfd_elf_special_section special_sections_b[] = |
2f89ff8d | 2303 | { |
0112cd26 NC |
2304 | { STRING_COMMA_LEN (".bss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE }, |
2305 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2306 | }; |
2307 | ||
b35d266b | 2308 | static const struct bfd_elf_special_section special_sections_c[] = |
7f4d3958 | 2309 | { |
0112cd26 NC |
2310 | { STRING_COMMA_LEN (".comment"), 0, SHT_PROGBITS, 0 }, |
2311 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2312 | }; |
2313 | ||
b35d266b | 2314 | static const struct bfd_elf_special_section special_sections_d[] = |
7f4d3958 | 2315 | { |
0112cd26 NC |
2316 | { STRING_COMMA_LEN (".data"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE }, |
2317 | { STRING_COMMA_LEN (".data1"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE }, | |
2318 | { STRING_COMMA_LEN (".debug"), 0, SHT_PROGBITS, 0 }, | |
2319 | { STRING_COMMA_LEN (".debug_line"), 0, SHT_PROGBITS, 0 }, | |
2320 | { STRING_COMMA_LEN (".debug_info"), 0, SHT_PROGBITS, 0 }, | |
2321 | { STRING_COMMA_LEN (".debug_abbrev"), 0, SHT_PROGBITS, 0 }, | |
2322 | { STRING_COMMA_LEN (".debug_aranges"), 0, SHT_PROGBITS, 0 }, | |
2323 | { STRING_COMMA_LEN (".dynamic"), 0, SHT_DYNAMIC, SHF_ALLOC }, | |
2324 | { STRING_COMMA_LEN (".dynstr"), 0, SHT_STRTAB, SHF_ALLOC }, | |
2325 | { STRING_COMMA_LEN (".dynsym"), 0, SHT_DYNSYM, SHF_ALLOC }, | |
2326 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2327 | }; |
2328 | ||
b35d266b | 2329 | static const struct bfd_elf_special_section special_sections_f[] = |
7f4d3958 | 2330 | { |
0112cd26 NC |
2331 | { STRING_COMMA_LEN (".fini"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR }, |
2332 | { STRING_COMMA_LEN (".fini_array"), 0, SHT_FINI_ARRAY, SHF_ALLOC + SHF_WRITE }, | |
2333 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2334 | }; |
2335 | ||
b35d266b | 2336 | static const struct bfd_elf_special_section special_sections_g[] = |
7f4d3958 | 2337 | { |
0112cd26 NC |
2338 | { STRING_COMMA_LEN (".gnu.linkonce.b"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE }, |
2339 | { STRING_COMMA_LEN (".got"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE }, | |
2340 | { STRING_COMMA_LEN (".gnu.version"), 0, SHT_GNU_versym, 0 }, | |
2341 | { STRING_COMMA_LEN (".gnu.version_d"), 0, SHT_GNU_verdef, 0 }, | |
2342 | { STRING_COMMA_LEN (".gnu.version_r"), 0, SHT_GNU_verneed, 0 }, | |
2343 | { STRING_COMMA_LEN (".gnu.liblist"), 0, SHT_GNU_LIBLIST, SHF_ALLOC }, | |
2344 | { STRING_COMMA_LEN (".gnu.conflict"), 0, SHT_RELA, SHF_ALLOC }, | |
2345 | { STRING_COMMA_LEN (".gnu.hash"), 0, SHT_GNU_HASH, SHF_ALLOC }, | |
2346 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2347 | }; |
2348 | ||
b35d266b | 2349 | static const struct bfd_elf_special_section special_sections_h[] = |
7f4d3958 | 2350 | { |
0112cd26 NC |
2351 | { STRING_COMMA_LEN (".hash"), 0, SHT_HASH, SHF_ALLOC }, |
2352 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2353 | }; |
2354 | ||
b35d266b | 2355 | static const struct bfd_elf_special_section special_sections_i[] = |
7f4d3958 | 2356 | { |
0112cd26 NC |
2357 | { STRING_COMMA_LEN (".init"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR }, |
2358 | { STRING_COMMA_LEN (".init_array"), 0, SHT_INIT_ARRAY, SHF_ALLOC + SHF_WRITE }, | |
2359 | { STRING_COMMA_LEN (".interp"), 0, SHT_PROGBITS, 0 }, | |
2360 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2361 | }; |
2362 | ||
b35d266b | 2363 | static const struct bfd_elf_special_section special_sections_l[] = |
7f4d3958 | 2364 | { |
0112cd26 NC |
2365 | { STRING_COMMA_LEN (".line"), 0, SHT_PROGBITS, 0 }, |
2366 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2367 | }; |
2368 | ||
b35d266b | 2369 | static const struct bfd_elf_special_section special_sections_n[] = |
7f4d3958 | 2370 | { |
0112cd26 NC |
2371 | { STRING_COMMA_LEN (".note.GNU-stack"), 0, SHT_PROGBITS, 0 }, |
2372 | { STRING_COMMA_LEN (".note"), -1, SHT_NOTE, 0 }, | |
2373 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2374 | }; |
2375 | ||
b35d266b | 2376 | static const struct bfd_elf_special_section special_sections_p[] = |
7f4d3958 | 2377 | { |
0112cd26 NC |
2378 | { STRING_COMMA_LEN (".preinit_array"), 0, SHT_PREINIT_ARRAY, SHF_ALLOC + SHF_WRITE }, |
2379 | { STRING_COMMA_LEN (".plt"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR }, | |
2380 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2381 | }; |
2382 | ||
b35d266b | 2383 | static const struct bfd_elf_special_section special_sections_r[] = |
7f4d3958 | 2384 | { |
0112cd26 NC |
2385 | { STRING_COMMA_LEN (".rodata"), -2, SHT_PROGBITS, SHF_ALLOC }, |
2386 | { STRING_COMMA_LEN (".rodata1"), 0, SHT_PROGBITS, SHF_ALLOC }, | |
2387 | { STRING_COMMA_LEN (".rela"), -1, SHT_RELA, 0 }, | |
2388 | { STRING_COMMA_LEN (".rel"), -1, SHT_REL, 0 }, | |
2389 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2390 | }; |
2391 | ||
b35d266b | 2392 | static const struct bfd_elf_special_section special_sections_s[] = |
7f4d3958 | 2393 | { |
0112cd26 NC |
2394 | { STRING_COMMA_LEN (".shstrtab"), 0, SHT_STRTAB, 0 }, |
2395 | { STRING_COMMA_LEN (".strtab"), 0, SHT_STRTAB, 0 }, | |
2396 | { STRING_COMMA_LEN (".symtab"), 0, SHT_SYMTAB, 0 }, | |
60ff4dc4 HPN |
2397 | /* See struct bfd_elf_special_section declaration for the semantics of |
2398 | this special case where .prefix_length != strlen (.prefix). */ | |
2399 | { ".stabstr", 5, 3, SHT_STRTAB, 0 }, | |
0112cd26 | 2400 | { NULL, 0, 0, 0, 0 } |
2f89ff8d L |
2401 | }; |
2402 | ||
b35d266b | 2403 | static const struct bfd_elf_special_section special_sections_t[] = |
7f4d3958 | 2404 | { |
0112cd26 NC |
2405 | { STRING_COMMA_LEN (".text"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR }, |
2406 | { STRING_COMMA_LEN (".tbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS }, | |
2407 | { STRING_COMMA_LEN (".tdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS }, | |
2408 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2409 | }; |
2410 | ||
b35d266b | 2411 | static const struct bfd_elf_special_section *special_sections[] = |
7f4d3958 | 2412 | { |
7f4d3958 L |
2413 | special_sections_b, /* 'b' */ |
2414 | special_sections_c, /* 'b' */ | |
2415 | special_sections_d, /* 'd' */ | |
2416 | NULL, /* 'e' */ | |
2417 | special_sections_f, /* 'f' */ | |
2418 | special_sections_g, /* 'g' */ | |
2419 | special_sections_h, /* 'h' */ | |
2420 | special_sections_i, /* 'i' */ | |
2421 | NULL, /* 'j' */ | |
2422 | NULL, /* 'k' */ | |
2423 | special_sections_l, /* 'l' */ | |
2424 | NULL, /* 'm' */ | |
2425 | special_sections_n, /* 'n' */ | |
2426 | NULL, /* 'o' */ | |
2427 | special_sections_p, /* 'p' */ | |
2428 | NULL, /* 'q' */ | |
2429 | special_sections_r, /* 'r' */ | |
2430 | special_sections_s, /* 's' */ | |
2431 | special_sections_t, /* 't' */ | |
7f4d3958 L |
2432 | }; |
2433 | ||
551b43fd AM |
2434 | const struct bfd_elf_special_section * |
2435 | _bfd_elf_get_special_section (const char *name, | |
2436 | const struct bfd_elf_special_section *spec, | |
2437 | unsigned int rela) | |
2f89ff8d L |
2438 | { |
2439 | int i; | |
7f4d3958 | 2440 | int len; |
7f4d3958 | 2441 | |
551b43fd | 2442 | len = strlen (name); |
7f4d3958 | 2443 | |
551b43fd | 2444 | for (i = 0; spec[i].prefix != NULL; i++) |
7dcb9820 AM |
2445 | { |
2446 | int suffix_len; | |
551b43fd | 2447 | int prefix_len = spec[i].prefix_length; |
7dcb9820 AM |
2448 | |
2449 | if (len < prefix_len) | |
2450 | continue; | |
551b43fd | 2451 | if (memcmp (name, spec[i].prefix, prefix_len) != 0) |
7dcb9820 AM |
2452 | continue; |
2453 | ||
551b43fd | 2454 | suffix_len = spec[i].suffix_length; |
7dcb9820 AM |
2455 | if (suffix_len <= 0) |
2456 | { | |
2457 | if (name[prefix_len] != 0) | |
2458 | { | |
2459 | if (suffix_len == 0) | |
2460 | continue; | |
2461 | if (name[prefix_len] != '.' | |
2462 | && (suffix_len == -2 | |
551b43fd | 2463 | || (rela && spec[i].type == SHT_REL))) |
7dcb9820 AM |
2464 | continue; |
2465 | } | |
2466 | } | |
2467 | else | |
2468 | { | |
2469 | if (len < prefix_len + suffix_len) | |
2470 | continue; | |
2471 | if (memcmp (name + len - suffix_len, | |
551b43fd | 2472 | spec[i].prefix + prefix_len, |
7dcb9820 AM |
2473 | suffix_len) != 0) |
2474 | continue; | |
2475 | } | |
551b43fd | 2476 | return &spec[i]; |
7dcb9820 | 2477 | } |
2f89ff8d L |
2478 | |
2479 | return NULL; | |
2480 | } | |
2481 | ||
7dcb9820 | 2482 | const struct bfd_elf_special_section * |
29ef7005 | 2483 | _bfd_elf_get_sec_type_attr (bfd *abfd, asection *sec) |
2f89ff8d | 2484 | { |
551b43fd AM |
2485 | int i; |
2486 | const struct bfd_elf_special_section *spec; | |
29ef7005 | 2487 | const struct elf_backend_data *bed; |
2f89ff8d L |
2488 | |
2489 | /* See if this is one of the special sections. */ | |
551b43fd AM |
2490 | if (sec->name == NULL) |
2491 | return NULL; | |
2f89ff8d | 2492 | |
29ef7005 L |
2493 | bed = get_elf_backend_data (abfd); |
2494 | spec = bed->special_sections; | |
2495 | if (spec) | |
2496 | { | |
2497 | spec = _bfd_elf_get_special_section (sec->name, | |
2498 | bed->special_sections, | |
2499 | sec->use_rela_p); | |
2500 | if (spec != NULL) | |
2501 | return spec; | |
2502 | } | |
2503 | ||
551b43fd AM |
2504 | if (sec->name[0] != '.') |
2505 | return NULL; | |
2f89ff8d | 2506 | |
551b43fd AM |
2507 | i = sec->name[1] - 'b'; |
2508 | if (i < 0 || i > 't' - 'b') | |
2509 | return NULL; | |
2510 | ||
2511 | spec = special_sections[i]; | |
2f89ff8d | 2512 | |
551b43fd AM |
2513 | if (spec == NULL) |
2514 | return NULL; | |
2515 | ||
2516 | return _bfd_elf_get_special_section (sec->name, spec, sec->use_rela_p); | |
2f89ff8d L |
2517 | } |
2518 | ||
b34976b6 | 2519 | bfd_boolean |
217aa764 | 2520 | _bfd_elf_new_section_hook (bfd *abfd, asection *sec) |
252b5132 RH |
2521 | { |
2522 | struct bfd_elf_section_data *sdata; | |
551b43fd | 2523 | const struct elf_backend_data *bed; |
7dcb9820 | 2524 | const struct bfd_elf_special_section *ssect; |
252b5132 | 2525 | |
f0abc2a1 AM |
2526 | sdata = (struct bfd_elf_section_data *) sec->used_by_bfd; |
2527 | if (sdata == NULL) | |
2528 | { | |
217aa764 | 2529 | sdata = bfd_zalloc (abfd, sizeof (*sdata)); |
f0abc2a1 AM |
2530 | if (sdata == NULL) |
2531 | return FALSE; | |
217aa764 | 2532 | sec->used_by_bfd = sdata; |
f0abc2a1 | 2533 | } |
bf572ba0 | 2534 | |
551b43fd AM |
2535 | /* Indicate whether or not this section should use RELA relocations. */ |
2536 | bed = get_elf_backend_data (abfd); | |
2537 | sec->use_rela_p = bed->default_use_rela_p; | |
2538 | ||
e843e0f8 L |
2539 | /* When we read a file, we don't need to set ELF section type and |
2540 | flags. They will be overridden in _bfd_elf_make_section_from_shdr | |
2541 | anyway. We will set ELF section type and flags for all linker | |
2542 | created sections. If user specifies BFD section flags, we will | |
2543 | set ELF section type and flags based on BFD section flags in | |
2544 | elf_fake_sections. */ | |
2545 | if ((!sec->flags && abfd->direction != read_direction) | |
3496cb2a | 2546 | || (sec->flags & SEC_LINKER_CREATED) != 0) |
2f89ff8d | 2547 | { |
551b43fd | 2548 | ssect = (*bed->get_sec_type_attr) (abfd, sec); |
a31501e9 L |
2549 | if (ssect != NULL) |
2550 | { | |
2551 | elf_section_type (sec) = ssect->type; | |
2552 | elf_section_flags (sec) = ssect->attr; | |
2553 | } | |
2f89ff8d L |
2554 | } |
2555 | ||
f592407e | 2556 | return _bfd_generic_new_section_hook (abfd, sec); |
252b5132 RH |
2557 | } |
2558 | ||
2559 | /* Create a new bfd section from an ELF program header. | |
2560 | ||
2561 | Since program segments have no names, we generate a synthetic name | |
2562 | of the form segment<NUM>, where NUM is generally the index in the | |
2563 | program header table. For segments that are split (see below) we | |
2564 | generate the names segment<NUM>a and segment<NUM>b. | |
2565 | ||
2566 | Note that some program segments may have a file size that is different than | |
2567 | (less than) the memory size. All this means is that at execution the | |
2568 | system must allocate the amount of memory specified by the memory size, | |
2569 | but only initialize it with the first "file size" bytes read from the | |
2570 | file. This would occur for example, with program segments consisting | |
2571 | of combined data+bss. | |
2572 | ||
2573 | To handle the above situation, this routine generates TWO bfd sections | |
2574 | for the single program segment. The first has the length specified by | |
2575 | the file size of the segment, and the second has the length specified | |
2576 | by the difference between the two sizes. In effect, the segment is split | |
2577 | into it's initialized and uninitialized parts. | |
2578 | ||
2579 | */ | |
2580 | ||
b34976b6 | 2581 | bfd_boolean |
217aa764 AM |
2582 | _bfd_elf_make_section_from_phdr (bfd *abfd, |
2583 | Elf_Internal_Phdr *hdr, | |
2584 | int index, | |
2585 | const char *typename) | |
252b5132 RH |
2586 | { |
2587 | asection *newsect; | |
2588 | char *name; | |
2589 | char namebuf[64]; | |
d4c88bbb | 2590 | size_t len; |
252b5132 RH |
2591 | int split; |
2592 | ||
2593 | split = ((hdr->p_memsz > 0) | |
2594 | && (hdr->p_filesz > 0) | |
2595 | && (hdr->p_memsz > hdr->p_filesz)); | |
27ac83bf | 2596 | sprintf (namebuf, "%s%d%s", typename, index, split ? "a" : ""); |
d4c88bbb | 2597 | len = strlen (namebuf) + 1; |
217aa764 | 2598 | name = bfd_alloc (abfd, len); |
252b5132 | 2599 | if (!name) |
b34976b6 | 2600 | return FALSE; |
d4c88bbb | 2601 | memcpy (name, namebuf, len); |
252b5132 RH |
2602 | newsect = bfd_make_section (abfd, name); |
2603 | if (newsect == NULL) | |
b34976b6 | 2604 | return FALSE; |
252b5132 RH |
2605 | newsect->vma = hdr->p_vaddr; |
2606 | newsect->lma = hdr->p_paddr; | |
eea6121a | 2607 | newsect->size = hdr->p_filesz; |
252b5132 RH |
2608 | newsect->filepos = hdr->p_offset; |
2609 | newsect->flags |= SEC_HAS_CONTENTS; | |
57e24cbf | 2610 | newsect->alignment_power = bfd_log2 (hdr->p_align); |
252b5132 RH |
2611 | if (hdr->p_type == PT_LOAD) |
2612 | { | |
2613 | newsect->flags |= SEC_ALLOC; | |
2614 | newsect->flags |= SEC_LOAD; | |
2615 | if (hdr->p_flags & PF_X) | |
2616 | { | |
2617 | /* FIXME: all we known is that it has execute PERMISSION, | |
c044fabd | 2618 | may be data. */ |
252b5132 RH |
2619 | newsect->flags |= SEC_CODE; |
2620 | } | |
2621 | } | |
2622 | if (!(hdr->p_flags & PF_W)) | |
2623 | { | |
2624 | newsect->flags |= SEC_READONLY; | |
2625 | } | |
2626 | ||
2627 | if (split) | |
2628 | { | |
27ac83bf | 2629 | sprintf (namebuf, "%s%db", typename, index); |
d4c88bbb | 2630 | len = strlen (namebuf) + 1; |
217aa764 | 2631 | name = bfd_alloc (abfd, len); |
252b5132 | 2632 | if (!name) |
b34976b6 | 2633 | return FALSE; |
d4c88bbb | 2634 | memcpy (name, namebuf, len); |
252b5132 RH |
2635 | newsect = bfd_make_section (abfd, name); |
2636 | if (newsect == NULL) | |
b34976b6 | 2637 | return FALSE; |
252b5132 RH |
2638 | newsect->vma = hdr->p_vaddr + hdr->p_filesz; |
2639 | newsect->lma = hdr->p_paddr + hdr->p_filesz; | |
eea6121a | 2640 | newsect->size = hdr->p_memsz - hdr->p_filesz; |
252b5132 RH |
2641 | if (hdr->p_type == PT_LOAD) |
2642 | { | |
2643 | newsect->flags |= SEC_ALLOC; | |
2644 | if (hdr->p_flags & PF_X) | |
2645 | newsect->flags |= SEC_CODE; | |
2646 | } | |
2647 | if (!(hdr->p_flags & PF_W)) | |
2648 | newsect->flags |= SEC_READONLY; | |
2649 | } | |
2650 | ||
b34976b6 | 2651 | return TRUE; |
252b5132 RH |
2652 | } |
2653 | ||
b34976b6 | 2654 | bfd_boolean |
217aa764 | 2655 | bfd_section_from_phdr (bfd *abfd, Elf_Internal_Phdr *hdr, int index) |
20cfcaae | 2656 | { |
9c5bfbb7 | 2657 | const struct elf_backend_data *bed; |
20cfcaae NC |
2658 | |
2659 | switch (hdr->p_type) | |
2660 | { | |
2661 | case PT_NULL: | |
2662 | return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "null"); | |
2663 | ||
2664 | case PT_LOAD: | |
2665 | return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "load"); | |
2666 | ||
2667 | case PT_DYNAMIC: | |
2668 | return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "dynamic"); | |
2669 | ||
2670 | case PT_INTERP: | |
2671 | return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "interp"); | |
2672 | ||
2673 | case PT_NOTE: | |
2674 | if (! _bfd_elf_make_section_from_phdr (abfd, hdr, index, "note")) | |
b34976b6 | 2675 | return FALSE; |
217aa764 | 2676 | if (! elfcore_read_notes (abfd, hdr->p_offset, hdr->p_filesz)) |
b34976b6 AM |
2677 | return FALSE; |
2678 | return TRUE; | |
20cfcaae NC |
2679 | |
2680 | case PT_SHLIB: | |
2681 | return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "shlib"); | |
2682 | ||
2683 | case PT_PHDR: | |
2684 | return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "phdr"); | |
2685 | ||
811072d8 RM |
2686 | case PT_GNU_EH_FRAME: |
2687 | return _bfd_elf_make_section_from_phdr (abfd, hdr, index, | |
2688 | "eh_frame_hdr"); | |
2689 | ||
9ee5e499 JJ |
2690 | case PT_GNU_STACK: |
2691 | return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "stack"); | |
2692 | ||
8c37241b JJ |
2693 | case PT_GNU_RELRO: |
2694 | return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "relro"); | |
2695 | ||
20cfcaae | 2696 | default: |
8c1acd09 | 2697 | /* Check for any processor-specific program segment types. */ |
20cfcaae | 2698 | bed = get_elf_backend_data (abfd); |
d27f5fa1 | 2699 | return bed->elf_backend_section_from_phdr (abfd, hdr, index, "proc"); |
20cfcaae NC |
2700 | } |
2701 | } | |
2702 | ||
23bc299b | 2703 | /* Initialize REL_HDR, the section-header for new section, containing |
b34976b6 | 2704 | relocations against ASECT. If USE_RELA_P is TRUE, we use RELA |
23bc299b MM |
2705 | relocations; otherwise, we use REL relocations. */ |
2706 | ||
b34976b6 | 2707 | bfd_boolean |
217aa764 AM |
2708 | _bfd_elf_init_reloc_shdr (bfd *abfd, |
2709 | Elf_Internal_Shdr *rel_hdr, | |
2710 | asection *asect, | |
2711 | bfd_boolean use_rela_p) | |
23bc299b MM |
2712 | { |
2713 | char *name; | |
9c5bfbb7 | 2714 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
dc810e39 | 2715 | bfd_size_type amt = sizeof ".rela" + strlen (asect->name); |
23bc299b | 2716 | |
dc810e39 | 2717 | name = bfd_alloc (abfd, amt); |
23bc299b | 2718 | if (name == NULL) |
b34976b6 | 2719 | return FALSE; |
23bc299b MM |
2720 | sprintf (name, "%s%s", use_rela_p ? ".rela" : ".rel", asect->name); |
2721 | rel_hdr->sh_name = | |
2b0f7ef9 | 2722 | (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), name, |
b34976b6 | 2723 | FALSE); |
23bc299b | 2724 | if (rel_hdr->sh_name == (unsigned int) -1) |
b34976b6 | 2725 | return FALSE; |
23bc299b MM |
2726 | rel_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL; |
2727 | rel_hdr->sh_entsize = (use_rela_p | |
2728 | ? bed->s->sizeof_rela | |
2729 | : bed->s->sizeof_rel); | |
45d6a902 | 2730 | rel_hdr->sh_addralign = 1 << bed->s->log_file_align; |
23bc299b MM |
2731 | rel_hdr->sh_flags = 0; |
2732 | rel_hdr->sh_addr = 0; | |
2733 | rel_hdr->sh_size = 0; | |
2734 | rel_hdr->sh_offset = 0; | |
2735 | ||
b34976b6 | 2736 | return TRUE; |
23bc299b MM |
2737 | } |
2738 | ||
252b5132 RH |
2739 | /* Set up an ELF internal section header for a section. */ |
2740 | ||
252b5132 | 2741 | static void |
217aa764 | 2742 | elf_fake_sections (bfd *abfd, asection *asect, void *failedptrarg) |
252b5132 | 2743 | { |
9c5bfbb7 | 2744 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
217aa764 | 2745 | bfd_boolean *failedptr = failedptrarg; |
252b5132 RH |
2746 | Elf_Internal_Shdr *this_hdr; |
2747 | ||
2748 | if (*failedptr) | |
2749 | { | |
2750 | /* We already failed; just get out of the bfd_map_over_sections | |
2751 | loop. */ | |
2752 | return; | |
2753 | } | |
2754 | ||
2755 | this_hdr = &elf_section_data (asect)->this_hdr; | |
2756 | ||
e57b5356 AM |
2757 | this_hdr->sh_name = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), |
2758 | asect->name, FALSE); | |
2759 | if (this_hdr->sh_name == (unsigned int) -1) | |
252b5132 | 2760 | { |
b34976b6 | 2761 | *failedptr = TRUE; |
252b5132 RH |
2762 | return; |
2763 | } | |
2764 | ||
a4d8e49b | 2765 | /* Don't clear sh_flags. Assembler may set additional bits. */ |
252b5132 RH |
2766 | |
2767 | if ((asect->flags & SEC_ALLOC) != 0 | |
2768 | || asect->user_set_vma) | |
2769 | this_hdr->sh_addr = asect->vma; | |
2770 | else | |
2771 | this_hdr->sh_addr = 0; | |
2772 | ||
2773 | this_hdr->sh_offset = 0; | |
eea6121a | 2774 | this_hdr->sh_size = asect->size; |
252b5132 RH |
2775 | this_hdr->sh_link = 0; |
2776 | this_hdr->sh_addralign = 1 << asect->alignment_power; | |
2777 | /* The sh_entsize and sh_info fields may have been set already by | |
2778 | copy_private_section_data. */ | |
2779 | ||
2780 | this_hdr->bfd_section = asect; | |
2781 | this_hdr->contents = NULL; | |
2782 | ||
3cddba1e L |
2783 | /* If the section type is unspecified, we set it based on |
2784 | asect->flags. */ | |
2785 | if (this_hdr->sh_type == SHT_NULL) | |
2786 | { | |
45c5e9ed | 2787 | if ((asect->flags & SEC_GROUP) != 0) |
ccd2ec6a | 2788 | this_hdr->sh_type = SHT_GROUP; |
45c5e9ed | 2789 | else if ((asect->flags & SEC_ALLOC) != 0 |
1ea63fd2 AM |
2790 | && (((asect->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0) |
2791 | || (asect->flags & SEC_NEVER_LOAD) != 0)) | |
3cddba1e L |
2792 | this_hdr->sh_type = SHT_NOBITS; |
2793 | else | |
2794 | this_hdr->sh_type = SHT_PROGBITS; | |
2795 | } | |
2796 | ||
2f89ff8d | 2797 | switch (this_hdr->sh_type) |
252b5132 | 2798 | { |
2f89ff8d | 2799 | default: |
2f89ff8d L |
2800 | break; |
2801 | ||
2802 | case SHT_STRTAB: | |
2803 | case SHT_INIT_ARRAY: | |
2804 | case SHT_FINI_ARRAY: | |
2805 | case SHT_PREINIT_ARRAY: | |
2806 | case SHT_NOTE: | |
2807 | case SHT_NOBITS: | |
2808 | case SHT_PROGBITS: | |
2809 | break; | |
2810 | ||
2811 | case SHT_HASH: | |
c7ac6ff8 | 2812 | this_hdr->sh_entsize = bed->s->sizeof_hash_entry; |
2f89ff8d | 2813 | break; |
5de3bf90 | 2814 | |
2f89ff8d | 2815 | case SHT_DYNSYM: |
252b5132 | 2816 | this_hdr->sh_entsize = bed->s->sizeof_sym; |
2f89ff8d L |
2817 | break; |
2818 | ||
2819 | case SHT_DYNAMIC: | |
252b5132 | 2820 | this_hdr->sh_entsize = bed->s->sizeof_dyn; |
2f89ff8d L |
2821 | break; |
2822 | ||
2823 | case SHT_RELA: | |
2824 | if (get_elf_backend_data (abfd)->may_use_rela_p) | |
2825 | this_hdr->sh_entsize = bed->s->sizeof_rela; | |
2826 | break; | |
2827 | ||
2828 | case SHT_REL: | |
2829 | if (get_elf_backend_data (abfd)->may_use_rel_p) | |
2830 | this_hdr->sh_entsize = bed->s->sizeof_rel; | |
2831 | break; | |
2832 | ||
2833 | case SHT_GNU_versym: | |
252b5132 | 2834 | this_hdr->sh_entsize = sizeof (Elf_External_Versym); |
2f89ff8d L |
2835 | break; |
2836 | ||
2837 | case SHT_GNU_verdef: | |
252b5132 RH |
2838 | this_hdr->sh_entsize = 0; |
2839 | /* objcopy or strip will copy over sh_info, but may not set | |
2840 | cverdefs. The linker will set cverdefs, but sh_info will be | |
2841 | zero. */ | |
2842 | if (this_hdr->sh_info == 0) | |
2843 | this_hdr->sh_info = elf_tdata (abfd)->cverdefs; | |
2844 | else | |
2845 | BFD_ASSERT (elf_tdata (abfd)->cverdefs == 0 | |
2846 | || this_hdr->sh_info == elf_tdata (abfd)->cverdefs); | |
2f89ff8d L |
2847 | break; |
2848 | ||
2849 | case SHT_GNU_verneed: | |
252b5132 RH |
2850 | this_hdr->sh_entsize = 0; |
2851 | /* objcopy or strip will copy over sh_info, but may not set | |
2852 | cverrefs. The linker will set cverrefs, but sh_info will be | |
2853 | zero. */ | |
2854 | if (this_hdr->sh_info == 0) | |
2855 | this_hdr->sh_info = elf_tdata (abfd)->cverrefs; | |
2856 | else | |
2857 | BFD_ASSERT (elf_tdata (abfd)->cverrefs == 0 | |
2858 | || this_hdr->sh_info == elf_tdata (abfd)->cverrefs); | |
2f89ff8d L |
2859 | break; |
2860 | ||
2861 | case SHT_GROUP: | |
1783205a | 2862 | this_hdr->sh_entsize = GRP_ENTRY_SIZE; |
2f89ff8d | 2863 | break; |
fdc90cb4 JJ |
2864 | |
2865 | case SHT_GNU_HASH: | |
2866 | this_hdr->sh_entsize = bed->s->arch_size == 64 ? 0 : 4; | |
2867 | break; | |
dbb410c3 | 2868 | } |
252b5132 RH |
2869 | |
2870 | if ((asect->flags & SEC_ALLOC) != 0) | |
2871 | this_hdr->sh_flags |= SHF_ALLOC; | |
2872 | if ((asect->flags & SEC_READONLY) == 0) | |
2873 | this_hdr->sh_flags |= SHF_WRITE; | |
2874 | if ((asect->flags & SEC_CODE) != 0) | |
2875 | this_hdr->sh_flags |= SHF_EXECINSTR; | |
f5fa8ca2 JJ |
2876 | if ((asect->flags & SEC_MERGE) != 0) |
2877 | { | |
2878 | this_hdr->sh_flags |= SHF_MERGE; | |
2879 | this_hdr->sh_entsize = asect->entsize; | |
2880 | if ((asect->flags & SEC_STRINGS) != 0) | |
2881 | this_hdr->sh_flags |= SHF_STRINGS; | |
2882 | } | |
1126897b | 2883 | if ((asect->flags & SEC_GROUP) == 0 && elf_group_name (asect) != NULL) |
dbb410c3 | 2884 | this_hdr->sh_flags |= SHF_GROUP; |
13ae64f3 | 2885 | if ((asect->flags & SEC_THREAD_LOCAL) != 0) |
704afa60 JJ |
2886 | { |
2887 | this_hdr->sh_flags |= SHF_TLS; | |
3a800eb9 AM |
2888 | if (asect->size == 0 |
2889 | && (asect->flags & SEC_HAS_CONTENTS) == 0) | |
704afa60 | 2890 | { |
3a800eb9 | 2891 | struct bfd_link_order *o = asect->map_tail.link_order; |
b34976b6 | 2892 | |
704afa60 | 2893 | this_hdr->sh_size = 0; |
3a800eb9 AM |
2894 | if (o != NULL) |
2895 | { | |
704afa60 | 2896 | this_hdr->sh_size = o->offset + o->size; |
3a800eb9 AM |
2897 | if (this_hdr->sh_size != 0) |
2898 | this_hdr->sh_type = SHT_NOBITS; | |
2899 | } | |
704afa60 JJ |
2900 | } |
2901 | } | |
252b5132 RH |
2902 | |
2903 | /* Check for processor-specific section types. */ | |
e1fddb6b AO |
2904 | if (bed->elf_backend_fake_sections |
2905 | && !(*bed->elf_backend_fake_sections) (abfd, this_hdr, asect)) | |
b34976b6 | 2906 | *failedptr = TRUE; |
252b5132 RH |
2907 | |
2908 | /* If the section has relocs, set up a section header for the | |
23bc299b MM |
2909 | SHT_REL[A] section. If two relocation sections are required for |
2910 | this section, it is up to the processor-specific back-end to | |
c044fabd | 2911 | create the other. */ |
23bc299b | 2912 | if ((asect->flags & SEC_RELOC) != 0 |
c044fabd | 2913 | && !_bfd_elf_init_reloc_shdr (abfd, |
23bc299b | 2914 | &elf_section_data (asect)->rel_hdr, |
c044fabd | 2915 | asect, |
68bfbfcc | 2916 | asect->use_rela_p)) |
b34976b6 | 2917 | *failedptr = TRUE; |
252b5132 RH |
2918 | } |
2919 | ||
dbb410c3 AM |
2920 | /* Fill in the contents of a SHT_GROUP section. */ |
2921 | ||
1126897b | 2922 | void |
217aa764 | 2923 | bfd_elf_set_group_contents (bfd *abfd, asection *sec, void *failedptrarg) |
dbb410c3 | 2924 | { |
217aa764 | 2925 | bfd_boolean *failedptr = failedptrarg; |
dbb410c3 | 2926 | unsigned long symindx; |
9dce4196 | 2927 | asection *elt, *first; |
dbb410c3 | 2928 | unsigned char *loc; |
b34976b6 | 2929 | bfd_boolean gas; |
dbb410c3 | 2930 | |
7e4111ad L |
2931 | /* Ignore linker created group section. See elfNN_ia64_object_p in |
2932 | elfxx-ia64.c. */ | |
2933 | if (((sec->flags & (SEC_GROUP | SEC_LINKER_CREATED)) != SEC_GROUP) | |
dbb410c3 AM |
2934 | || *failedptr) |
2935 | return; | |
2936 | ||
1126897b AM |
2937 | symindx = 0; |
2938 | if (elf_group_id (sec) != NULL) | |
2939 | symindx = elf_group_id (sec)->udata.i; | |
2940 | ||
2941 | if (symindx == 0) | |
2942 | { | |
2943 | /* If called from the assembler, swap_out_syms will have set up | |
2944 | elf_section_syms; If called for "ld -r", use target_index. */ | |
2945 | if (elf_section_syms (abfd) != NULL) | |
2946 | symindx = elf_section_syms (abfd)[sec->index]->udata.i; | |
2947 | else | |
2948 | symindx = sec->target_index; | |
2949 | } | |
dbb410c3 AM |
2950 | elf_section_data (sec)->this_hdr.sh_info = symindx; |
2951 | ||
1126897b | 2952 | /* The contents won't be allocated for "ld -r" or objcopy. */ |
b34976b6 | 2953 | gas = TRUE; |
dbb410c3 AM |
2954 | if (sec->contents == NULL) |
2955 | { | |
b34976b6 | 2956 | gas = FALSE; |
eea6121a | 2957 | sec->contents = bfd_alloc (abfd, sec->size); |
9dce4196 AM |
2958 | |
2959 | /* Arrange for the section to be written out. */ | |
2960 | elf_section_data (sec)->this_hdr.contents = sec->contents; | |
dbb410c3 AM |
2961 | if (sec->contents == NULL) |
2962 | { | |
b34976b6 | 2963 | *failedptr = TRUE; |
dbb410c3 AM |
2964 | return; |
2965 | } | |
2966 | } | |
2967 | ||
eea6121a | 2968 | loc = sec->contents + sec->size; |
dbb410c3 | 2969 | |
9dce4196 AM |
2970 | /* Get the pointer to the first section in the group that gas |
2971 | squirreled away here. objcopy arranges for this to be set to the | |
2972 | start of the input section group. */ | |
2973 | first = elt = elf_next_in_group (sec); | |
dbb410c3 AM |
2974 | |
2975 | /* First element is a flag word. Rest of section is elf section | |
2976 | indices for all the sections of the group. Write them backwards | |
2977 | just to keep the group in the same order as given in .section | |
2978 | directives, not that it matters. */ | |
2979 | while (elt != NULL) | |
2980 | { | |
9dce4196 AM |
2981 | asection *s; |
2982 | unsigned int idx; | |
2983 | ||
dbb410c3 | 2984 | loc -= 4; |
9dce4196 AM |
2985 | s = elt; |
2986 | if (!gas) | |
2987 | s = s->output_section; | |
2988 | idx = 0; | |
2989 | if (s != NULL) | |
2990 | idx = elf_section_data (s)->this_idx; | |
2991 | H_PUT_32 (abfd, idx, loc); | |
945906ff | 2992 | elt = elf_next_in_group (elt); |
9dce4196 AM |
2993 | if (elt == first) |
2994 | break; | |
dbb410c3 AM |
2995 | } |
2996 | ||
3d7f7666 | 2997 | if ((loc -= 4) != sec->contents) |
9dce4196 | 2998 | abort (); |
dbb410c3 | 2999 | |
9dce4196 | 3000 | H_PUT_32 (abfd, sec->flags & SEC_LINK_ONCE ? GRP_COMDAT : 0, loc); |
dbb410c3 AM |
3001 | } |
3002 | ||
252b5132 RH |
3003 | /* Assign all ELF section numbers. The dummy first section is handled here |
3004 | too. The link/info pointers for the standard section types are filled | |
3005 | in here too, while we're at it. */ | |
3006 | ||
b34976b6 | 3007 | static bfd_boolean |
da9f89d4 | 3008 | assign_section_numbers (bfd *abfd, struct bfd_link_info *link_info) |
252b5132 RH |
3009 | { |
3010 | struct elf_obj_tdata *t = elf_tdata (abfd); | |
3011 | asection *sec; | |
2b0f7ef9 | 3012 | unsigned int section_number, secn; |
252b5132 | 3013 | Elf_Internal_Shdr **i_shdrp; |
47cc2cf5 | 3014 | struct bfd_elf_section_data *d; |
252b5132 RH |
3015 | |
3016 | section_number = 1; | |
3017 | ||
2b0f7ef9 JJ |
3018 | _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd)); |
3019 | ||
da9f89d4 L |
3020 | /* SHT_GROUP sections are in relocatable files only. */ |
3021 | if (link_info == NULL || link_info->relocatable) | |
252b5132 | 3022 | { |
da9f89d4 | 3023 | /* Put SHT_GROUP sections first. */ |
04dd1667 | 3024 | for (sec = abfd->sections; sec != NULL; sec = sec->next) |
47cc2cf5 | 3025 | { |
5daa8fe7 | 3026 | d = elf_section_data (sec); |
da9f89d4 L |
3027 | |
3028 | if (d->this_hdr.sh_type == SHT_GROUP) | |
3029 | { | |
5daa8fe7 | 3030 | if (sec->flags & SEC_LINKER_CREATED) |
da9f89d4 L |
3031 | { |
3032 | /* Remove the linker created SHT_GROUP sections. */ | |
5daa8fe7 | 3033 | bfd_section_list_remove (abfd, sec); |
da9f89d4 | 3034 | abfd->section_count--; |
da9f89d4 L |
3035 | } |
3036 | else | |
3037 | { | |
3038 | if (section_number == SHN_LORESERVE) | |
3039 | section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE; | |
3040 | d->this_idx = section_number++; | |
3041 | } | |
3042 | } | |
47cc2cf5 PB |
3043 | } |
3044 | } | |
3045 | ||
3046 | for (sec = abfd->sections; sec; sec = sec->next) | |
3047 | { | |
3048 | d = elf_section_data (sec); | |
3049 | ||
3050 | if (d->this_hdr.sh_type != SHT_GROUP) | |
3051 | { | |
3052 | if (section_number == SHN_LORESERVE) | |
3053 | section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE; | |
3054 | d->this_idx = section_number++; | |
3055 | } | |
2b0f7ef9 | 3056 | _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->this_hdr.sh_name); |
252b5132 RH |
3057 | if ((sec->flags & SEC_RELOC) == 0) |
3058 | d->rel_idx = 0; | |
3059 | else | |
2b0f7ef9 | 3060 | { |
9ad5cbcf AM |
3061 | if (section_number == SHN_LORESERVE) |
3062 | section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE; | |
2b0f7ef9 JJ |
3063 | d->rel_idx = section_number++; |
3064 | _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel_hdr.sh_name); | |
3065 | } | |
23bc299b MM |
3066 | |
3067 | if (d->rel_hdr2) | |
2b0f7ef9 | 3068 | { |
9ad5cbcf AM |
3069 | if (section_number == SHN_LORESERVE) |
3070 | section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE; | |
2b0f7ef9 JJ |
3071 | d->rel_idx2 = section_number++; |
3072 | _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel_hdr2->sh_name); | |
3073 | } | |
23bc299b MM |
3074 | else |
3075 | d->rel_idx2 = 0; | |
252b5132 RH |
3076 | } |
3077 | ||
9ad5cbcf AM |
3078 | if (section_number == SHN_LORESERVE) |
3079 | section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE; | |
252b5132 | 3080 | t->shstrtab_section = section_number++; |
2b0f7ef9 | 3081 | _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->shstrtab_hdr.sh_name); |
252b5132 | 3082 | elf_elfheader (abfd)->e_shstrndx = t->shstrtab_section; |
252b5132 RH |
3083 | |
3084 | if (bfd_get_symcount (abfd) > 0) | |
3085 | { | |
9ad5cbcf AM |
3086 | if (section_number == SHN_LORESERVE) |
3087 | section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE; | |
252b5132 | 3088 | t->symtab_section = section_number++; |
2b0f7ef9 | 3089 | _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->symtab_hdr.sh_name); |
9ad5cbcf AM |
3090 | if (section_number > SHN_LORESERVE - 2) |
3091 | { | |
3092 | if (section_number == SHN_LORESERVE) | |
3093 | section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE; | |
3094 | t->symtab_shndx_section = section_number++; | |
3095 | t->symtab_shndx_hdr.sh_name | |
3096 | = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), | |
b34976b6 | 3097 | ".symtab_shndx", FALSE); |
9ad5cbcf | 3098 | if (t->symtab_shndx_hdr.sh_name == (unsigned int) -1) |
b34976b6 | 3099 | return FALSE; |
9ad5cbcf AM |
3100 | } |
3101 | if (section_number == SHN_LORESERVE) | |
3102 | section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE; | |
252b5132 | 3103 | t->strtab_section = section_number++; |
2b0f7ef9 | 3104 | _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->strtab_hdr.sh_name); |
252b5132 RH |
3105 | } |
3106 | ||
2b0f7ef9 JJ |
3107 | _bfd_elf_strtab_finalize (elf_shstrtab (abfd)); |
3108 | t->shstrtab_hdr.sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd)); | |
9ad5cbcf AM |
3109 | |
3110 | elf_numsections (abfd) = section_number; | |
252b5132 | 3111 | elf_elfheader (abfd)->e_shnum = section_number; |
9ad5cbcf AM |
3112 | if (section_number > SHN_LORESERVE) |
3113 | elf_elfheader (abfd)->e_shnum -= SHN_HIRESERVE + 1 - SHN_LORESERVE; | |
252b5132 RH |
3114 | |
3115 | /* Set up the list of section header pointers, in agreement with the | |
3116 | indices. */ | |
d0fb9a8d | 3117 | i_shdrp = bfd_zalloc2 (abfd, section_number, sizeof (Elf_Internal_Shdr *)); |
252b5132 | 3118 | if (i_shdrp == NULL) |
b34976b6 | 3119 | return FALSE; |
252b5132 | 3120 | |
d0fb9a8d | 3121 | i_shdrp[0] = bfd_zalloc (abfd, sizeof (Elf_Internal_Shdr)); |
252b5132 RH |
3122 | if (i_shdrp[0] == NULL) |
3123 | { | |
3124 | bfd_release (abfd, i_shdrp); | |
b34976b6 | 3125 | return FALSE; |
252b5132 | 3126 | } |
252b5132 RH |
3127 | |
3128 | elf_elfsections (abfd) = i_shdrp; | |
3129 | ||
3130 | i_shdrp[t->shstrtab_section] = &t->shstrtab_hdr; | |
3131 | if (bfd_get_symcount (abfd) > 0) | |
3132 | { | |
3133 | i_shdrp[t->symtab_section] = &t->symtab_hdr; | |
9ad5cbcf AM |
3134 | if (elf_numsections (abfd) > SHN_LORESERVE) |
3135 | { | |
3136 | i_shdrp[t->symtab_shndx_section] = &t->symtab_shndx_hdr; | |
3137 | t->symtab_shndx_hdr.sh_link = t->symtab_section; | |
3138 | } | |
252b5132 RH |
3139 | i_shdrp[t->strtab_section] = &t->strtab_hdr; |
3140 | t->symtab_hdr.sh_link = t->strtab_section; | |
3141 | } | |
38ce5b11 | 3142 | |
252b5132 RH |
3143 | for (sec = abfd->sections; sec; sec = sec->next) |
3144 | { | |
3145 | struct bfd_elf_section_data *d = elf_section_data (sec); | |
3146 | asection *s; | |
3147 | const char *name; | |
3148 | ||
3149 | i_shdrp[d->this_idx] = &d->this_hdr; | |
3150 | if (d->rel_idx != 0) | |
3151 | i_shdrp[d->rel_idx] = &d->rel_hdr; | |
23bc299b MM |
3152 | if (d->rel_idx2 != 0) |
3153 | i_shdrp[d->rel_idx2] = d->rel_hdr2; | |
252b5132 RH |
3154 | |
3155 | /* Fill in the sh_link and sh_info fields while we're at it. */ | |
3156 | ||
3157 | /* sh_link of a reloc section is the section index of the symbol | |
3158 | table. sh_info is the section index of the section to which | |
3159 | the relocation entries apply. */ | |
3160 | if (d->rel_idx != 0) | |
3161 | { | |
3162 | d->rel_hdr.sh_link = t->symtab_section; | |
3163 | d->rel_hdr.sh_info = d->this_idx; | |
3164 | } | |
23bc299b MM |
3165 | if (d->rel_idx2 != 0) |
3166 | { | |
3167 | d->rel_hdr2->sh_link = t->symtab_section; | |
3168 | d->rel_hdr2->sh_info = d->this_idx; | |
3169 | } | |
252b5132 | 3170 | |
38ce5b11 L |
3171 | /* We need to set up sh_link for SHF_LINK_ORDER. */ |
3172 | if ((d->this_hdr.sh_flags & SHF_LINK_ORDER) != 0) | |
3173 | { | |
3174 | s = elf_linked_to_section (sec); | |
3175 | if (s) | |
38ce5b11 | 3176 | { |
f2876037 | 3177 | /* elf_linked_to_section points to the input section. */ |
ccd2ec6a | 3178 | if (link_info != NULL) |
38ce5b11 | 3179 | { |
f2876037 | 3180 | /* Check discarded linkonce section. */ |
ccd2ec6a | 3181 | if (elf_discarded_section (s)) |
38ce5b11 | 3182 | { |
ccd2ec6a L |
3183 | asection *kept; |
3184 | (*_bfd_error_handler) | |
3185 | (_("%B: sh_link of section `%A' points to discarded section `%A' of `%B'"), | |
3186 | abfd, d->this_hdr.bfd_section, | |
3187 | s, s->owner); | |
3188 | /* Point to the kept section if it has the same | |
3189 | size as the discarded one. */ | |
c0f00686 | 3190 | kept = _bfd_elf_check_kept_section (s, link_info); |
ccd2ec6a | 3191 | if (kept == NULL) |
185d09ad | 3192 | { |
ccd2ec6a L |
3193 | bfd_set_error (bfd_error_bad_value); |
3194 | return FALSE; | |
185d09ad | 3195 | } |
ccd2ec6a | 3196 | s = kept; |
38ce5b11 | 3197 | } |
e424ecc8 | 3198 | |
ccd2ec6a L |
3199 | s = s->output_section; |
3200 | BFD_ASSERT (s != NULL); | |
38ce5b11 | 3201 | } |
f2876037 L |
3202 | else |
3203 | { | |
3204 | /* Handle objcopy. */ | |
3205 | if (s->output_section == NULL) | |
3206 | { | |
3207 | (*_bfd_error_handler) | |
3208 | (_("%B: sh_link of section `%A' points to removed section `%A' of `%B'"), | |
3209 | abfd, d->this_hdr.bfd_section, s, s->owner); | |
3210 | bfd_set_error (bfd_error_bad_value); | |
3211 | return FALSE; | |
3212 | } | |
3213 | s = s->output_section; | |
3214 | } | |
ccd2ec6a L |
3215 | d->this_hdr.sh_link = elf_section_data (s)->this_idx; |
3216 | } | |
3217 | else | |
3218 | { | |
3219 | /* PR 290: | |
3220 | The Intel C compiler generates SHT_IA_64_UNWIND with | |
3221 | SHF_LINK_ORDER. But it doesn't set the sh_link or | |
3222 | sh_info fields. Hence we could get the situation | |
3223 | where s is NULL. */ | |
3224 | const struct elf_backend_data *bed | |
3225 | = get_elf_backend_data (abfd); | |
3226 | if (bed->link_order_error_handler) | |
3227 | bed->link_order_error_handler | |
3228 | (_("%B: warning: sh_link not set for section `%A'"), | |
3229 | abfd, sec); | |
38ce5b11 L |
3230 | } |
3231 | } | |
3232 | ||
252b5132 RH |
3233 | switch (d->this_hdr.sh_type) |
3234 | { | |
3235 | case SHT_REL: | |
3236 | case SHT_RELA: | |
3237 | /* A reloc section which we are treating as a normal BFD | |
3238 | section. sh_link is the section index of the symbol | |
3239 | table. sh_info is the section index of the section to | |
3240 | which the relocation entries apply. We assume that an | |
3241 | allocated reloc section uses the dynamic symbol table. | |
3242 | FIXME: How can we be sure? */ | |
3243 | s = bfd_get_section_by_name (abfd, ".dynsym"); | |
3244 | if (s != NULL) | |
3245 | d->this_hdr.sh_link = elf_section_data (s)->this_idx; | |
3246 | ||
3247 | /* We look up the section the relocs apply to by name. */ | |
3248 | name = sec->name; | |
3249 | if (d->this_hdr.sh_type == SHT_REL) | |
3250 | name += 4; | |
3251 | else | |
3252 | name += 5; | |
3253 | s = bfd_get_section_by_name (abfd, name); | |
3254 | if (s != NULL) | |
3255 | d->this_hdr.sh_info = elf_section_data (s)->this_idx; | |
3256 | break; | |
3257 | ||
3258 | case SHT_STRTAB: | |
3259 | /* We assume that a section named .stab*str is a stabs | |
3260 | string section. We look for a section with the same name | |
3261 | but without the trailing ``str'', and set its sh_link | |
3262 | field to point to this section. */ | |
0112cd26 | 3263 | if (CONST_STRNEQ (sec->name, ".stab") |
252b5132 RH |
3264 | && strcmp (sec->name + strlen (sec->name) - 3, "str") == 0) |
3265 | { | |
3266 | size_t len; | |
3267 | char *alc; | |
3268 | ||
3269 | len = strlen (sec->name); | |
217aa764 | 3270 | alc = bfd_malloc (len - 2); |
252b5132 | 3271 | if (alc == NULL) |
b34976b6 | 3272 | return FALSE; |
d4c88bbb | 3273 | memcpy (alc, sec->name, len - 3); |
252b5132 RH |
3274 | alc[len - 3] = '\0'; |
3275 | s = bfd_get_section_by_name (abfd, alc); | |
3276 | free (alc); | |
3277 | if (s != NULL) | |
3278 | { | |
3279 | elf_section_data (s)->this_hdr.sh_link = d->this_idx; | |
3280 | ||
3281 | /* This is a .stab section. */ | |
0594c12d AM |
3282 | if (elf_section_data (s)->this_hdr.sh_entsize == 0) |
3283 | elf_section_data (s)->this_hdr.sh_entsize | |
3284 | = 4 + 2 * bfd_get_arch_size (abfd) / 8; | |
252b5132 RH |
3285 | } |
3286 | } | |
3287 | break; | |
3288 | ||
3289 | case SHT_DYNAMIC: | |
3290 | case SHT_DYNSYM: | |
3291 | case SHT_GNU_verneed: | |
3292 | case SHT_GNU_verdef: | |
3293 | /* sh_link is the section header index of the string table | |
3294 | used for the dynamic entries, or the symbol table, or the | |
3295 | version strings. */ | |
3296 | s = bfd_get_section_by_name (abfd, ".dynstr"); | |
3297 | if (s != NULL) | |
3298 | d->this_hdr.sh_link = elf_section_data (s)->this_idx; | |
3299 | break; | |
3300 | ||
7f1204bb JJ |
3301 | case SHT_GNU_LIBLIST: |
3302 | /* sh_link is the section header index of the prelink library | |
3303 | list | |
3304 | used for the dynamic entries, or the symbol table, or the | |
3305 | version strings. */ | |
3306 | s = bfd_get_section_by_name (abfd, (sec->flags & SEC_ALLOC) | |
3307 | ? ".dynstr" : ".gnu.libstr"); | |
3308 | if (s != NULL) | |
3309 | d->this_hdr.sh_link = elf_section_data (s)->this_idx; | |
3310 | break; | |
3311 | ||
252b5132 | 3312 | case SHT_HASH: |
fdc90cb4 | 3313 | case SHT_GNU_HASH: |
252b5132 RH |
3314 | case SHT_GNU_versym: |
3315 | /* sh_link is the section header index of the symbol table | |
3316 | this hash table or version table is for. */ | |
3317 | s = bfd_get_section_by_name (abfd, ".dynsym"); | |
3318 | if (s != NULL) | |
3319 | d->this_hdr.sh_link = elf_section_data (s)->this_idx; | |
3320 | break; | |
dbb410c3 AM |
3321 | |
3322 | case SHT_GROUP: | |
3323 | d->this_hdr.sh_link = t->symtab_section; | |
252b5132 RH |
3324 | } |
3325 | } | |
3326 | ||
2b0f7ef9 | 3327 | for (secn = 1; secn < section_number; ++secn) |
9ad5cbcf AM |
3328 | if (i_shdrp[secn] == NULL) |
3329 | i_shdrp[secn] = i_shdrp[0]; | |
3330 | else | |
3331 | i_shdrp[secn]->sh_name = _bfd_elf_strtab_offset (elf_shstrtab (abfd), | |
3332 | i_shdrp[secn]->sh_name); | |
b34976b6 | 3333 | return TRUE; |
252b5132 RH |
3334 | } |
3335 | ||
3336 | /* Map symbol from it's internal number to the external number, moving | |
3337 | all local symbols to be at the head of the list. */ | |
3338 | ||
5372391b | 3339 | static bfd_boolean |
217aa764 | 3340 | sym_is_global (bfd *abfd, asymbol *sym) |
252b5132 RH |
3341 | { |
3342 | /* If the backend has a special mapping, use it. */ | |
9c5bfbb7 | 3343 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
217aa764 AM |
3344 | if (bed->elf_backend_sym_is_global) |
3345 | return (*bed->elf_backend_sym_is_global) (abfd, sym); | |
252b5132 RH |
3346 | |
3347 | return ((sym->flags & (BSF_GLOBAL | BSF_WEAK)) != 0 | |
3348 | || bfd_is_und_section (bfd_get_section (sym)) | |
3349 | || bfd_is_com_section (bfd_get_section (sym))); | |
3350 | } | |
3351 | ||
5372391b AM |
3352 | /* Don't output section symbols for sections that are not going to be |
3353 | output. Also, don't output section symbols for reloc and other | |
3354 | special sections. */ | |
3355 | ||
3356 | static bfd_boolean | |
3357 | ignore_section_sym (bfd *abfd, asymbol *sym) | |
3358 | { | |
3359 | return ((sym->flags & BSF_SECTION_SYM) != 0 | |
3360 | && (sym->value != 0 | |
3361 | || (sym->section->owner != abfd | |
3362 | && (sym->section->output_section->owner != abfd | |
3363 | || sym->section->output_offset != 0)))); | |
3364 | } | |
3365 | ||
b34976b6 | 3366 | static bfd_boolean |
217aa764 | 3367 | elf_map_symbols (bfd *abfd) |
252b5132 | 3368 | { |
dc810e39 | 3369 | unsigned int symcount = bfd_get_symcount (abfd); |
252b5132 RH |
3370 | asymbol **syms = bfd_get_outsymbols (abfd); |
3371 | asymbol **sect_syms; | |
dc810e39 AM |
3372 | unsigned int num_locals = 0; |
3373 | unsigned int num_globals = 0; | |
3374 | unsigned int num_locals2 = 0; | |
3375 | unsigned int num_globals2 = 0; | |
252b5132 | 3376 | int max_index = 0; |
dc810e39 | 3377 | unsigned int idx; |
252b5132 RH |
3378 | asection *asect; |
3379 | asymbol **new_syms; | |
252b5132 RH |
3380 | |
3381 | #ifdef DEBUG | |
3382 | fprintf (stderr, "elf_map_symbols\n"); | |
3383 | fflush (stderr); | |
3384 | #endif | |
3385 | ||
252b5132 RH |
3386 | for (asect = abfd->sections; asect; asect = asect->next) |
3387 | { | |
3388 | if (max_index < asect->index) | |
3389 | max_index = asect->index; | |
3390 | } | |
3391 | ||
3392 | max_index++; | |
d0fb9a8d | 3393 | sect_syms = bfd_zalloc2 (abfd, max_index, sizeof (asymbol *)); |
252b5132 | 3394 | if (sect_syms == NULL) |
b34976b6 | 3395 | return FALSE; |
252b5132 | 3396 | elf_section_syms (abfd) = sect_syms; |
4e89ac30 | 3397 | elf_num_section_syms (abfd) = max_index; |
252b5132 | 3398 | |
079e9a2f AM |
3399 | /* Init sect_syms entries for any section symbols we have already |
3400 | decided to output. */ | |
252b5132 RH |
3401 | for (idx = 0; idx < symcount; idx++) |
3402 | { | |
dc810e39 | 3403 | asymbol *sym = syms[idx]; |
c044fabd | 3404 | |
252b5132 | 3405 | if ((sym->flags & BSF_SECTION_SYM) != 0 |
5372391b | 3406 | && !ignore_section_sym (abfd, sym)) |
252b5132 | 3407 | { |
5372391b | 3408 | asection *sec = sym->section; |
252b5132 | 3409 | |
5372391b AM |
3410 | if (sec->owner != abfd) |
3411 | sec = sec->output_section; | |
252b5132 | 3412 | |
5372391b | 3413 | sect_syms[sec->index] = syms[idx]; |
252b5132 RH |
3414 | } |
3415 | } | |
3416 | ||
252b5132 RH |
3417 | /* Classify all of the symbols. */ |
3418 | for (idx = 0; idx < symcount; idx++) | |
3419 | { | |
5372391b AM |
3420 | if (ignore_section_sym (abfd, syms[idx])) |
3421 | continue; | |
252b5132 RH |
3422 | if (!sym_is_global (abfd, syms[idx])) |
3423 | num_locals++; | |
3424 | else | |
3425 | num_globals++; | |
3426 | } | |
079e9a2f | 3427 | |
5372391b | 3428 | /* We will be adding a section symbol for each normal BFD section. Most |
079e9a2f AM |
3429 | sections will already have a section symbol in outsymbols, but |
3430 | eg. SHT_GROUP sections will not, and we need the section symbol mapped | |
3431 | at least in that case. */ | |
252b5132 RH |
3432 | for (asect = abfd->sections; asect; asect = asect->next) |
3433 | { | |
079e9a2f | 3434 | if (sect_syms[asect->index] == NULL) |
252b5132 | 3435 | { |
079e9a2f | 3436 | if (!sym_is_global (abfd, asect->symbol)) |
252b5132 RH |
3437 | num_locals++; |
3438 | else | |
3439 | num_globals++; | |
252b5132 RH |
3440 | } |
3441 | } | |
3442 | ||
3443 | /* Now sort the symbols so the local symbols are first. */ | |
d0fb9a8d | 3444 | new_syms = bfd_alloc2 (abfd, num_locals + num_globals, sizeof (asymbol *)); |
dc810e39 | 3445 | |
252b5132 | 3446 | if (new_syms == NULL) |
b34976b6 | 3447 | return FALSE; |
252b5132 RH |
3448 | |
3449 | for (idx = 0; idx < symcount; idx++) | |
3450 | { | |
3451 | asymbol *sym = syms[idx]; | |
dc810e39 | 3452 | unsigned int i; |
252b5132 | 3453 | |
5372391b AM |
3454 | if (ignore_section_sym (abfd, sym)) |
3455 | continue; | |
252b5132 RH |
3456 | if (!sym_is_global (abfd, sym)) |
3457 | i = num_locals2++; | |
3458 | else | |
3459 | i = num_locals + num_globals2++; | |
3460 | new_syms[i] = sym; | |
3461 | sym->udata.i = i + 1; | |
3462 | } | |
3463 | for (asect = abfd->sections; asect; asect = asect->next) | |
3464 | { | |
079e9a2f | 3465 | if (sect_syms[asect->index] == NULL) |
252b5132 | 3466 | { |
079e9a2f | 3467 | asymbol *sym = asect->symbol; |
dc810e39 | 3468 | unsigned int i; |
252b5132 | 3469 | |
079e9a2f | 3470 | sect_syms[asect->index] = sym; |
252b5132 RH |
3471 | if (!sym_is_global (abfd, sym)) |
3472 | i = num_locals2++; | |
3473 | else | |
3474 | i = num_locals + num_globals2++; | |
3475 | new_syms[i] = sym; | |
3476 | sym->udata.i = i + 1; | |
3477 | } | |
3478 | } | |
3479 | ||
3480 | bfd_set_symtab (abfd, new_syms, num_locals + num_globals); | |
3481 | ||
3482 | elf_num_locals (abfd) = num_locals; | |
3483 | elf_num_globals (abfd) = num_globals; | |
b34976b6 | 3484 | return TRUE; |
252b5132 RH |
3485 | } |
3486 | ||
3487 | /* Align to the maximum file alignment that could be required for any | |
3488 | ELF data structure. */ | |
3489 | ||
268b6b39 | 3490 | static inline file_ptr |
217aa764 | 3491 | align_file_position (file_ptr off, int align) |
252b5132 RH |
3492 | { |
3493 | return (off + align - 1) & ~(align - 1); | |
3494 | } | |
3495 | ||
3496 | /* Assign a file position to a section, optionally aligning to the | |
3497 | required section alignment. */ | |
3498 | ||
217aa764 AM |
3499 | file_ptr |
3500 | _bfd_elf_assign_file_position_for_section (Elf_Internal_Shdr *i_shdrp, | |
3501 | file_ptr offset, | |
3502 | bfd_boolean align) | |
252b5132 RH |
3503 | { |
3504 | if (align) | |
3505 | { | |
3506 | unsigned int al; | |
3507 | ||
3508 | al = i_shdrp->sh_addralign; | |
3509 | if (al > 1) | |
3510 | offset = BFD_ALIGN (offset, al); | |
3511 | } | |
3512 | i_shdrp->sh_offset = offset; | |
3513 | if (i_shdrp->bfd_section != NULL) | |
3514 | i_shdrp->bfd_section->filepos = offset; | |
3515 | if (i_shdrp->sh_type != SHT_NOBITS) | |
3516 | offset += i_shdrp->sh_size; | |
3517 | return offset; | |
3518 | } | |
3519 | ||
3520 | /* Compute the file positions we are going to put the sections at, and | |
3521 | otherwise prepare to begin writing out the ELF file. If LINK_INFO | |
3522 | is not NULL, this is being called by the ELF backend linker. */ | |
3523 | ||
b34976b6 | 3524 | bfd_boolean |
217aa764 AM |
3525 | _bfd_elf_compute_section_file_positions (bfd *abfd, |
3526 | struct bfd_link_info *link_info) | |
252b5132 | 3527 | { |
9c5bfbb7 | 3528 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
b34976b6 | 3529 | bfd_boolean failed; |
4b6c0f2f | 3530 | struct bfd_strtab_hash *strtab = NULL; |
252b5132 RH |
3531 | Elf_Internal_Shdr *shstrtab_hdr; |
3532 | ||
3533 | if (abfd->output_has_begun) | |
b34976b6 | 3534 | return TRUE; |
252b5132 RH |
3535 | |
3536 | /* Do any elf backend specific processing first. */ | |
3537 | if (bed->elf_backend_begin_write_processing) | |
3538 | (*bed->elf_backend_begin_write_processing) (abfd, link_info); | |
3539 | ||
3540 | if (! prep_headers (abfd)) | |
b34976b6 | 3541 | return FALSE; |
252b5132 | 3542 | |
e6c51ed4 NC |
3543 | /* Post process the headers if necessary. */ |
3544 | if (bed->elf_backend_post_process_headers) | |
3545 | (*bed->elf_backend_post_process_headers) (abfd, link_info); | |
3546 | ||
b34976b6 | 3547 | failed = FALSE; |
252b5132 RH |
3548 | bfd_map_over_sections (abfd, elf_fake_sections, &failed); |
3549 | if (failed) | |
b34976b6 | 3550 | return FALSE; |
252b5132 | 3551 | |
da9f89d4 | 3552 | if (!assign_section_numbers (abfd, link_info)) |
b34976b6 | 3553 | return FALSE; |
252b5132 RH |
3554 | |
3555 | /* The backend linker builds symbol table information itself. */ | |
3556 | if (link_info == NULL && bfd_get_symcount (abfd) > 0) | |
3557 | { | |
3558 | /* Non-zero if doing a relocatable link. */ | |
3559 | int relocatable_p = ! (abfd->flags & (EXEC_P | DYNAMIC)); | |
3560 | ||
3561 | if (! swap_out_syms (abfd, &strtab, relocatable_p)) | |
b34976b6 | 3562 | return FALSE; |
252b5132 RH |
3563 | } |
3564 | ||
1126897b | 3565 | if (link_info == NULL) |
dbb410c3 | 3566 | { |
1126897b | 3567 | bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed); |
dbb410c3 | 3568 | if (failed) |
b34976b6 | 3569 | return FALSE; |
dbb410c3 AM |
3570 | } |
3571 | ||
252b5132 RH |
3572 | shstrtab_hdr = &elf_tdata (abfd)->shstrtab_hdr; |
3573 | /* sh_name was set in prep_headers. */ | |
3574 | shstrtab_hdr->sh_type = SHT_STRTAB; | |
3575 | shstrtab_hdr->sh_flags = 0; | |
3576 | shstrtab_hdr->sh_addr = 0; | |
2b0f7ef9 | 3577 | shstrtab_hdr->sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd)); |
252b5132 RH |
3578 | shstrtab_hdr->sh_entsize = 0; |
3579 | shstrtab_hdr->sh_link = 0; | |
3580 | shstrtab_hdr->sh_info = 0; | |
3581 | /* sh_offset is set in assign_file_positions_except_relocs. */ | |
3582 | shstrtab_hdr->sh_addralign = 1; | |
3583 | ||
c84fca4d | 3584 | if (!assign_file_positions_except_relocs (abfd, link_info)) |
b34976b6 | 3585 | return FALSE; |
252b5132 RH |
3586 | |
3587 | if (link_info == NULL && bfd_get_symcount (abfd) > 0) | |
3588 | { | |
3589 | file_ptr off; | |
3590 | Elf_Internal_Shdr *hdr; | |
3591 | ||
3592 | off = elf_tdata (abfd)->next_file_pos; | |
3593 | ||
3594 | hdr = &elf_tdata (abfd)->symtab_hdr; | |
b34976b6 | 3595 | off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE); |
252b5132 | 3596 | |
9ad5cbcf AM |
3597 | hdr = &elf_tdata (abfd)->symtab_shndx_hdr; |
3598 | if (hdr->sh_size != 0) | |
b34976b6 | 3599 | off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE); |
9ad5cbcf | 3600 | |
252b5132 | 3601 | hdr = &elf_tdata (abfd)->strtab_hdr; |
b34976b6 | 3602 | off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE); |
252b5132 RH |
3603 | |
3604 | elf_tdata (abfd)->next_file_pos = off; | |
3605 | ||
3606 | /* Now that we know where the .strtab section goes, write it | |
3607 | out. */ | |
3608 | if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0 | |
3609 | || ! _bfd_stringtab_emit (abfd, strtab)) | |
b34976b6 | 3610 | return FALSE; |
252b5132 RH |
3611 | _bfd_stringtab_free (strtab); |
3612 | } | |
3613 | ||
b34976b6 | 3614 | abfd->output_has_begun = TRUE; |
252b5132 | 3615 | |
b34976b6 | 3616 | return TRUE; |
252b5132 RH |
3617 | } |
3618 | ||
8ded5a0f AM |
3619 | /* Make an initial estimate of the size of the program header. If we |
3620 | get the number wrong here, we'll redo section placement. */ | |
3621 | ||
3622 | static bfd_size_type | |
3623 | get_program_header_size (bfd *abfd, struct bfd_link_info *info) | |
3624 | { | |
3625 | size_t segs; | |
3626 | asection *s; | |
3627 | const struct elf_backend_data *bed; | |
3628 | ||
3629 | /* Assume we will need exactly two PT_LOAD segments: one for text | |
3630 | and one for data. */ | |
3631 | segs = 2; | |
3632 | ||
3633 | s = bfd_get_section_by_name (abfd, ".interp"); | |
3634 | if (s != NULL && (s->flags & SEC_LOAD) != 0) | |
3635 | { | |
3636 | /* If we have a loadable interpreter section, we need a | |
3637 | PT_INTERP segment. In this case, assume we also need a | |
3638 | PT_PHDR segment, although that may not be true for all | |
3639 | targets. */ | |
3640 | segs += 2; | |
3641 | } | |
3642 | ||
3643 | if (bfd_get_section_by_name (abfd, ".dynamic") != NULL) | |
3644 | { | |
3645 | /* We need a PT_DYNAMIC segment. */ | |
3646 | ++segs; | |
c9df6640 L |
3647 | |
3648 | if (elf_tdata (abfd)->relro) | |
3649 | { | |
3650 | /* We need a PT_GNU_RELRO segment only when there is a | |
3651 | PT_DYNAMIC segment. */ | |
3652 | ++segs; | |
3653 | } | |
8ded5a0f AM |
3654 | } |
3655 | ||
3656 | if (elf_tdata (abfd)->eh_frame_hdr) | |
3657 | { | |
3658 | /* We need a PT_GNU_EH_FRAME segment. */ | |
3659 | ++segs; | |
3660 | } | |
3661 | ||
3662 | if (elf_tdata (abfd)->stack_flags) | |
3663 | { | |
3664 | /* We need a PT_GNU_STACK segment. */ | |
3665 | ++segs; | |
3666 | } | |
3667 | ||
8ded5a0f AM |
3668 | for (s = abfd->sections; s != NULL; s = s->next) |
3669 | { | |
3670 | if ((s->flags & SEC_LOAD) != 0 | |
0112cd26 | 3671 | && CONST_STRNEQ (s->name, ".note")) |
8ded5a0f AM |
3672 | { |
3673 | /* We need a PT_NOTE segment. */ | |
3674 | ++segs; | |
3675 | } | |
3676 | } | |
3677 | ||
3678 | for (s = abfd->sections; s != NULL; s = s->next) | |
3679 | { | |
3680 | if (s->flags & SEC_THREAD_LOCAL) | |
3681 | { | |
3682 | /* We need a PT_TLS segment. */ | |
3683 | ++segs; | |
3684 | break; | |
3685 | } | |
3686 | } | |
3687 | ||
3688 | /* Let the backend count up any program headers it might need. */ | |
3689 | bed = get_elf_backend_data (abfd); | |
3690 | if (bed->elf_backend_additional_program_headers) | |
3691 | { | |
3692 | int a; | |
3693 | ||
3694 | a = (*bed->elf_backend_additional_program_headers) (abfd, info); | |
3695 | if (a == -1) | |
3696 | abort (); | |
3697 | segs += a; | |
3698 | } | |
3699 | ||
3700 | return segs * bed->s->sizeof_phdr; | |
3701 | } | |
3702 | ||
252b5132 RH |
3703 | /* Create a mapping from a set of sections to a program segment. */ |
3704 | ||
217aa764 AM |
3705 | static struct elf_segment_map * |
3706 | make_mapping (bfd *abfd, | |
3707 | asection **sections, | |
3708 | unsigned int from, | |
3709 | unsigned int to, | |
3710 | bfd_boolean phdr) | |
252b5132 RH |
3711 | { |
3712 | struct elf_segment_map *m; | |
3713 | unsigned int i; | |
3714 | asection **hdrpp; | |
dc810e39 | 3715 | bfd_size_type amt; |
252b5132 | 3716 | |
dc810e39 AM |
3717 | amt = sizeof (struct elf_segment_map); |
3718 | amt += (to - from - 1) * sizeof (asection *); | |
217aa764 | 3719 | m = bfd_zalloc (abfd, amt); |
252b5132 RH |
3720 | if (m == NULL) |
3721 | return NULL; | |
3722 | m->next = NULL; | |
3723 | m->p_type = PT_LOAD; | |
3724 | for (i = from, hdrpp = sections + from; i < to; i++, hdrpp++) | |
3725 | m->sections[i - from] = *hdrpp; | |
3726 | m->count = to - from; | |
3727 | ||
3728 | if (from == 0 && phdr) | |
3729 | { | |
3730 | /* Include the headers in the first PT_LOAD segment. */ | |
3731 | m->includes_filehdr = 1; | |
3732 | m->includes_phdrs = 1; | |
3733 | } | |
3734 | ||
3735 | return m; | |
3736 | } | |
3737 | ||
229fcec5 MM |
3738 | /* Create the PT_DYNAMIC segment, which includes DYNSEC. Returns NULL |
3739 | on failure. */ | |
3740 | ||
3741 | struct elf_segment_map * | |
3742 | _bfd_elf_make_dynamic_segment (bfd *abfd, asection *dynsec) | |
3743 | { | |
3744 | struct elf_segment_map *m; | |
3745 | ||
3746 | m = bfd_zalloc (abfd, sizeof (struct elf_segment_map)); | |
3747 | if (m == NULL) | |
3748 | return NULL; | |
3749 | m->next = NULL; | |
3750 | m->p_type = PT_DYNAMIC; | |
3751 | m->count = 1; | |
3752 | m->sections[0] = dynsec; | |
3753 | ||
3754 | return m; | |
3755 | } | |
3756 | ||
8ded5a0f | 3757 | /* Possibly add or remove segments from the segment map. */ |
252b5132 | 3758 | |
b34976b6 | 3759 | static bfd_boolean |
8ded5a0f | 3760 | elf_modify_segment_map (bfd *abfd, struct bfd_link_info *info) |
252b5132 | 3761 | { |
252e386e | 3762 | struct elf_segment_map **m; |
8ded5a0f | 3763 | const struct elf_backend_data *bed; |
252b5132 | 3764 | |
8ded5a0f AM |
3765 | /* The placement algorithm assumes that non allocated sections are |
3766 | not in PT_LOAD segments. We ensure this here by removing such | |
3767 | sections from the segment map. We also remove excluded | |
252e386e AM |
3768 | sections. Finally, any PT_LOAD segment without sections is |
3769 | removed. */ | |
3770 | m = &elf_tdata (abfd)->segment_map; | |
3771 | while (*m) | |
8ded5a0f AM |
3772 | { |
3773 | unsigned int i, new_count; | |
252b5132 | 3774 | |
252e386e | 3775 | for (new_count = 0, i = 0; i < (*m)->count; i++) |
8ded5a0f | 3776 | { |
252e386e AM |
3777 | if (((*m)->sections[i]->flags & SEC_EXCLUDE) == 0 |
3778 | && (((*m)->sections[i]->flags & SEC_ALLOC) != 0 | |
3779 | || (*m)->p_type != PT_LOAD)) | |
8ded5a0f | 3780 | { |
252e386e AM |
3781 | (*m)->sections[new_count] = (*m)->sections[i]; |
3782 | new_count++; | |
8ded5a0f AM |
3783 | } |
3784 | } | |
252e386e | 3785 | (*m)->count = new_count; |
252b5132 | 3786 | |
252e386e AM |
3787 | if ((*m)->p_type == PT_LOAD && (*m)->count == 0) |
3788 | *m = (*m)->next; | |
3789 | else | |
3790 | m = &(*m)->next; | |
8ded5a0f | 3791 | } |
252b5132 | 3792 | |
8ded5a0f AM |
3793 | bed = get_elf_backend_data (abfd); |
3794 | if (bed->elf_backend_modify_segment_map != NULL) | |
252b5132 | 3795 | { |
252e386e | 3796 | if (!(*bed->elf_backend_modify_segment_map) (abfd, info)) |
8ded5a0f | 3797 | return FALSE; |
252b5132 | 3798 | } |
252b5132 | 3799 | |
8ded5a0f AM |
3800 | return TRUE; |
3801 | } | |
252b5132 | 3802 | |
8ded5a0f | 3803 | /* Set up a mapping from BFD sections to program segments. */ |
252b5132 | 3804 | |
8ded5a0f AM |
3805 | bfd_boolean |
3806 | _bfd_elf_map_sections_to_segments (bfd *abfd, struct bfd_link_info *info) | |
3807 | { | |
3808 | unsigned int count; | |
3809 | struct elf_segment_map *m; | |
3810 | asection **sections = NULL; | |
3811 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
252b5132 | 3812 | |
8ded5a0f AM |
3813 | if (elf_tdata (abfd)->segment_map == NULL |
3814 | && bfd_count_sections (abfd) != 0) | |
252b5132 | 3815 | { |
8ded5a0f AM |
3816 | asection *s; |
3817 | unsigned int i; | |
3818 | struct elf_segment_map *mfirst; | |
3819 | struct elf_segment_map **pm; | |
3820 | asection *last_hdr; | |
3821 | bfd_vma last_size; | |
3822 | unsigned int phdr_index; | |
3823 | bfd_vma maxpagesize; | |
3824 | asection **hdrpp; | |
3825 | bfd_boolean phdr_in_segment = TRUE; | |
3826 | bfd_boolean writable; | |
3827 | int tls_count = 0; | |
3828 | asection *first_tls = NULL; | |
3829 | asection *dynsec, *eh_frame_hdr; | |
3830 | bfd_size_type amt; | |
252b5132 | 3831 | |
8ded5a0f | 3832 | /* Select the allocated sections, and sort them. */ |
252b5132 | 3833 | |
8ded5a0f AM |
3834 | sections = bfd_malloc2 (bfd_count_sections (abfd), sizeof (asection *)); |
3835 | if (sections == NULL) | |
252b5132 | 3836 | goto error_return; |
252b5132 | 3837 | |
8ded5a0f AM |
3838 | i = 0; |
3839 | for (s = abfd->sections; s != NULL; s = s->next) | |
3840 | { | |
3841 | if ((s->flags & SEC_ALLOC) != 0) | |
3842 | { | |
3843 | sections[i] = s; | |
3844 | ++i; | |
3845 | } | |
3846 | } | |
3847 | BFD_ASSERT (i <= bfd_count_sections (abfd)); | |
3848 | count = i; | |
252b5132 | 3849 | |
8ded5a0f | 3850 | qsort (sections, (size_t) count, sizeof (asection *), elf_sort_sections); |
252b5132 | 3851 | |
8ded5a0f | 3852 | /* Build the mapping. */ |
252b5132 | 3853 | |
8ded5a0f AM |
3854 | mfirst = NULL; |
3855 | pm = &mfirst; | |
252b5132 | 3856 | |
8ded5a0f AM |
3857 | /* If we have a .interp section, then create a PT_PHDR segment for |
3858 | the program headers and a PT_INTERP segment for the .interp | |
3859 | section. */ | |
3860 | s = bfd_get_section_by_name (abfd, ".interp"); | |
3861 | if (s != NULL && (s->flags & SEC_LOAD) != 0) | |
3862 | { | |
3863 | amt = sizeof (struct elf_segment_map); | |
3864 | m = bfd_zalloc (abfd, amt); | |
3865 | if (m == NULL) | |
3866 | goto error_return; | |
3867 | m->next = NULL; | |
3868 | m->p_type = PT_PHDR; | |
3869 | /* FIXME: UnixWare and Solaris set PF_X, Irix 5 does not. */ | |
3870 | m->p_flags = PF_R | PF_X; | |
3871 | m->p_flags_valid = 1; | |
3872 | m->includes_phdrs = 1; | |
252b5132 | 3873 | |
8ded5a0f AM |
3874 | *pm = m; |
3875 | pm = &m->next; | |
252b5132 | 3876 | |
8ded5a0f AM |
3877 | amt = sizeof (struct elf_segment_map); |
3878 | m = bfd_zalloc (abfd, amt); | |
3879 | if (m == NULL) | |
3880 | goto error_return; | |
3881 | m->next = NULL; | |
3882 | m->p_type = PT_INTERP; | |
3883 | m->count = 1; | |
3884 | m->sections[0] = s; | |
3885 | ||
3886 | *pm = m; | |
3887 | pm = &m->next; | |
252b5132 | 3888 | } |
8ded5a0f AM |
3889 | |
3890 | /* Look through the sections. We put sections in the same program | |
3891 | segment when the start of the second section can be placed within | |
3892 | a few bytes of the end of the first section. */ | |
3893 | last_hdr = NULL; | |
3894 | last_size = 0; | |
3895 | phdr_index = 0; | |
3896 | maxpagesize = bed->maxpagesize; | |
3897 | writable = FALSE; | |
3898 | dynsec = bfd_get_section_by_name (abfd, ".dynamic"); | |
3899 | if (dynsec != NULL | |
3900 | && (dynsec->flags & SEC_LOAD) == 0) | |
3901 | dynsec = NULL; | |
3902 | ||
3903 | /* Deal with -Ttext or something similar such that the first section | |
3904 | is not adjacent to the program headers. This is an | |
3905 | approximation, since at this point we don't know exactly how many | |
3906 | program headers we will need. */ | |
3907 | if (count > 0) | |
252b5132 | 3908 | { |
8ded5a0f AM |
3909 | bfd_size_type phdr_size = elf_tdata (abfd)->program_header_size; |
3910 | ||
62d7a5f6 | 3911 | if (phdr_size == (bfd_size_type) -1) |
8ded5a0f AM |
3912 | phdr_size = get_program_header_size (abfd, info); |
3913 | if ((abfd->flags & D_PAGED) == 0 | |
3914 | || sections[0]->lma < phdr_size | |
3915 | || sections[0]->lma % maxpagesize < phdr_size % maxpagesize) | |
3916 | phdr_in_segment = FALSE; | |
252b5132 RH |
3917 | } |
3918 | ||
8ded5a0f | 3919 | for (i = 0, hdrpp = sections; i < count; i++, hdrpp++) |
252b5132 | 3920 | { |
8ded5a0f AM |
3921 | asection *hdr; |
3922 | bfd_boolean new_segment; | |
3923 | ||
3924 | hdr = *hdrpp; | |
3925 | ||
3926 | /* See if this section and the last one will fit in the same | |
3927 | segment. */ | |
3928 | ||
3929 | if (last_hdr == NULL) | |
3930 | { | |
3931 | /* If we don't have a segment yet, then we don't need a new | |
3932 | one (we build the last one after this loop). */ | |
3933 | new_segment = FALSE; | |
3934 | } | |
3935 | else if (last_hdr->lma - last_hdr->vma != hdr->lma - hdr->vma) | |
3936 | { | |
3937 | /* If this section has a different relation between the | |
3938 | virtual address and the load address, then we need a new | |
3939 | segment. */ | |
3940 | new_segment = TRUE; | |
3941 | } | |
3942 | else if (BFD_ALIGN (last_hdr->lma + last_size, maxpagesize) | |
3943 | < BFD_ALIGN (hdr->lma, maxpagesize)) | |
3944 | { | |
3945 | /* If putting this section in this segment would force us to | |
3946 | skip a page in the segment, then we need a new segment. */ | |
3947 | new_segment = TRUE; | |
3948 | } | |
3949 | else if ((last_hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0 | |
3950 | && (hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) != 0) | |
3951 | { | |
3952 | /* We don't want to put a loadable section after a | |
3953 | nonloadable section in the same segment. | |
3954 | Consider .tbss sections as loadable for this purpose. */ | |
3955 | new_segment = TRUE; | |
3956 | } | |
3957 | else if ((abfd->flags & D_PAGED) == 0) | |
3958 | { | |
3959 | /* If the file is not demand paged, which means that we | |
3960 | don't require the sections to be correctly aligned in the | |
3961 | file, then there is no other reason for a new segment. */ | |
3962 | new_segment = FALSE; | |
3963 | } | |
3964 | else if (! writable | |
3965 | && (hdr->flags & SEC_READONLY) == 0 | |
3966 | && (((last_hdr->lma + last_size - 1) | |
3967 | & ~(maxpagesize - 1)) | |
3968 | != (hdr->lma & ~(maxpagesize - 1)))) | |
3969 | { | |
3970 | /* We don't want to put a writable section in a read only | |
3971 | segment, unless they are on the same page in memory | |
3972 | anyhow. We already know that the last section does not | |
3973 | bring us past the current section on the page, so the | |
3974 | only case in which the new section is not on the same | |
3975 | page as the previous section is when the previous section | |
3976 | ends precisely on a page boundary. */ | |
3977 | new_segment = TRUE; | |
3978 | } | |
3979 | else | |
3980 | { | |
3981 | /* Otherwise, we can use the same segment. */ | |
3982 | new_segment = FALSE; | |
3983 | } | |
3984 | ||
2889e75b NC |
3985 | /* Allow interested parties a chance to override our decision. */ |
3986 | if (last_hdr && info->callbacks->override_segment_assignment) | |
3987 | new_segment = info->callbacks->override_segment_assignment (info, abfd, hdr, last_hdr, new_segment); | |
3988 | ||
8ded5a0f AM |
3989 | if (! new_segment) |
3990 | { | |
3991 | if ((hdr->flags & SEC_READONLY) == 0) | |
3992 | writable = TRUE; | |
3993 | last_hdr = hdr; | |
3994 | /* .tbss sections effectively have zero size. */ | |
3995 | if ((hdr->flags & (SEC_THREAD_LOCAL | SEC_LOAD)) | |
3996 | != SEC_THREAD_LOCAL) | |
3997 | last_size = hdr->size; | |
3998 | else | |
3999 | last_size = 0; | |
4000 | continue; | |
4001 | } | |
4002 | ||
4003 | /* We need a new program segment. We must create a new program | |
4004 | header holding all the sections from phdr_index until hdr. */ | |
4005 | ||
4006 | m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment); | |
4007 | if (m == NULL) | |
4008 | goto error_return; | |
4009 | ||
4010 | *pm = m; | |
4011 | pm = &m->next; | |
4012 | ||
252b5132 | 4013 | if ((hdr->flags & SEC_READONLY) == 0) |
b34976b6 | 4014 | writable = TRUE; |
8ded5a0f AM |
4015 | else |
4016 | writable = FALSE; | |
4017 | ||
baaff79e JJ |
4018 | last_hdr = hdr; |
4019 | /* .tbss sections effectively have zero size. */ | |
e5caec89 | 4020 | if ((hdr->flags & (SEC_THREAD_LOCAL | SEC_LOAD)) != SEC_THREAD_LOCAL) |
eea6121a | 4021 | last_size = hdr->size; |
baaff79e JJ |
4022 | else |
4023 | last_size = 0; | |
8ded5a0f AM |
4024 | phdr_index = i; |
4025 | phdr_in_segment = FALSE; | |
252b5132 RH |
4026 | } |
4027 | ||
8ded5a0f AM |
4028 | /* Create a final PT_LOAD program segment. */ |
4029 | if (last_hdr != NULL) | |
4030 | { | |
4031 | m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment); | |
4032 | if (m == NULL) | |
4033 | goto error_return; | |
252b5132 | 4034 | |
8ded5a0f AM |
4035 | *pm = m; |
4036 | pm = &m->next; | |
4037 | } | |
252b5132 | 4038 | |
8ded5a0f AM |
4039 | /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */ |
4040 | if (dynsec != NULL) | |
4041 | { | |
4042 | m = _bfd_elf_make_dynamic_segment (abfd, dynsec); | |
4043 | if (m == NULL) | |
4044 | goto error_return; | |
4045 | *pm = m; | |
4046 | pm = &m->next; | |
4047 | } | |
252b5132 | 4048 | |
8ded5a0f AM |
4049 | /* For each loadable .note section, add a PT_NOTE segment. We don't |
4050 | use bfd_get_section_by_name, because if we link together | |
4051 | nonloadable .note sections and loadable .note sections, we will | |
4052 | generate two .note sections in the output file. FIXME: Using | |
4053 | names for section types is bogus anyhow. */ | |
4054 | for (s = abfd->sections; s != NULL; s = s->next) | |
4055 | { | |
4056 | if ((s->flags & SEC_LOAD) != 0 | |
0112cd26 | 4057 | && CONST_STRNEQ (s->name, ".note")) |
8ded5a0f AM |
4058 | { |
4059 | amt = sizeof (struct elf_segment_map); | |
4060 | m = bfd_zalloc (abfd, amt); | |
4061 | if (m == NULL) | |
4062 | goto error_return; | |
4063 | m->next = NULL; | |
4064 | m->p_type = PT_NOTE; | |
4065 | m->count = 1; | |
4066 | m->sections[0] = s; | |
252b5132 | 4067 | |
8ded5a0f AM |
4068 | *pm = m; |
4069 | pm = &m->next; | |
4070 | } | |
4071 | if (s->flags & SEC_THREAD_LOCAL) | |
4072 | { | |
4073 | if (! tls_count) | |
4074 | first_tls = s; | |
4075 | tls_count++; | |
4076 | } | |
4077 | } | |
252b5132 | 4078 | |
8ded5a0f AM |
4079 | /* If there are any SHF_TLS output sections, add PT_TLS segment. */ |
4080 | if (tls_count > 0) | |
4081 | { | |
4082 | int i; | |
252b5132 | 4083 | |
8ded5a0f AM |
4084 | amt = sizeof (struct elf_segment_map); |
4085 | amt += (tls_count - 1) * sizeof (asection *); | |
4086 | m = bfd_zalloc (abfd, amt); | |
4087 | if (m == NULL) | |
4088 | goto error_return; | |
4089 | m->next = NULL; | |
4090 | m->p_type = PT_TLS; | |
4091 | m->count = tls_count; | |
4092 | /* Mandated PF_R. */ | |
4093 | m->p_flags = PF_R; | |
4094 | m->p_flags_valid = 1; | |
4095 | for (i = 0; i < tls_count; ++i) | |
4096 | { | |
4097 | BFD_ASSERT (first_tls->flags & SEC_THREAD_LOCAL); | |
4098 | m->sections[i] = first_tls; | |
4099 | first_tls = first_tls->next; | |
4100 | } | |
252b5132 | 4101 | |
8ded5a0f AM |
4102 | *pm = m; |
4103 | pm = &m->next; | |
4104 | } | |
252b5132 | 4105 | |
8ded5a0f AM |
4106 | /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME |
4107 | segment. */ | |
4108 | eh_frame_hdr = elf_tdata (abfd)->eh_frame_hdr; | |
4109 | if (eh_frame_hdr != NULL | |
4110 | && (eh_frame_hdr->output_section->flags & SEC_LOAD) != 0) | |
252b5132 | 4111 | { |
dc810e39 | 4112 | amt = sizeof (struct elf_segment_map); |
217aa764 | 4113 | m = bfd_zalloc (abfd, amt); |
252b5132 RH |
4114 | if (m == NULL) |
4115 | goto error_return; | |
4116 | m->next = NULL; | |
8ded5a0f | 4117 | m->p_type = PT_GNU_EH_FRAME; |
252b5132 | 4118 | m->count = 1; |
8ded5a0f | 4119 | m->sections[0] = eh_frame_hdr->output_section; |
252b5132 RH |
4120 | |
4121 | *pm = m; | |
4122 | pm = &m->next; | |
4123 | } | |
13ae64f3 | 4124 | |
8ded5a0f | 4125 | if (elf_tdata (abfd)->stack_flags) |
13ae64f3 | 4126 | { |
8ded5a0f AM |
4127 | amt = sizeof (struct elf_segment_map); |
4128 | m = bfd_zalloc (abfd, amt); | |
4129 | if (m == NULL) | |
4130 | goto error_return; | |
4131 | m->next = NULL; | |
4132 | m->p_type = PT_GNU_STACK; | |
4133 | m->p_flags = elf_tdata (abfd)->stack_flags; | |
4134 | m->p_flags_valid = 1; | |
252b5132 | 4135 | |
8ded5a0f AM |
4136 | *pm = m; |
4137 | pm = &m->next; | |
4138 | } | |
65765700 | 4139 | |
c9df6640 | 4140 | if (dynsec != NULL && elf_tdata (abfd)->relro) |
8ded5a0f | 4141 | { |
c9df6640 L |
4142 | /* We make a PT_GNU_RELRO segment only when there is a |
4143 | PT_DYNAMIC segment. */ | |
8ded5a0f AM |
4144 | amt = sizeof (struct elf_segment_map); |
4145 | m = bfd_zalloc (abfd, amt); | |
4146 | if (m == NULL) | |
4147 | goto error_return; | |
4148 | m->next = NULL; | |
4149 | m->p_type = PT_GNU_RELRO; | |
4150 | m->p_flags = PF_R; | |
4151 | m->p_flags_valid = 1; | |
65765700 | 4152 | |
8ded5a0f AM |
4153 | *pm = m; |
4154 | pm = &m->next; | |
4155 | } | |
9ee5e499 | 4156 | |
8ded5a0f AM |
4157 | free (sections); |
4158 | elf_tdata (abfd)->segment_map = mfirst; | |
9ee5e499 JJ |
4159 | } |
4160 | ||
8ded5a0f AM |
4161 | if (!elf_modify_segment_map (abfd, info)) |
4162 | return FALSE; | |
8c37241b | 4163 | |
8ded5a0f AM |
4164 | for (count = 0, m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next) |
4165 | ++count; | |
4166 | elf_tdata (abfd)->program_header_size = count * bed->s->sizeof_phdr; | |
252b5132 | 4167 | |
b34976b6 | 4168 | return TRUE; |
252b5132 RH |
4169 | |
4170 | error_return: | |
4171 | if (sections != NULL) | |
4172 | free (sections); | |
b34976b6 | 4173 | return FALSE; |
252b5132 RH |
4174 | } |
4175 | ||
4176 | /* Sort sections by address. */ | |
4177 | ||
4178 | static int | |
217aa764 | 4179 | elf_sort_sections (const void *arg1, const void *arg2) |
252b5132 RH |
4180 | { |
4181 | const asection *sec1 = *(const asection **) arg1; | |
4182 | const asection *sec2 = *(const asection **) arg2; | |
eecdbe52 | 4183 | bfd_size_type size1, size2; |
252b5132 RH |
4184 | |
4185 | /* Sort by LMA first, since this is the address used to | |
4186 | place the section into a segment. */ | |
4187 | if (sec1->lma < sec2->lma) | |
4188 | return -1; | |
4189 | else if (sec1->lma > sec2->lma) | |
4190 | return 1; | |
4191 | ||
4192 | /* Then sort by VMA. Normally the LMA and the VMA will be | |
4193 | the same, and this will do nothing. */ | |
4194 | if (sec1->vma < sec2->vma) | |
4195 | return -1; | |
4196 | else if (sec1->vma > sec2->vma) | |
4197 | return 1; | |
4198 | ||
4199 | /* Put !SEC_LOAD sections after SEC_LOAD ones. */ | |
4200 | ||
07c6e936 | 4201 | #define TOEND(x) (((x)->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0) |
252b5132 RH |
4202 | |
4203 | if (TOEND (sec1)) | |
4204 | { | |
4205 | if (TOEND (sec2)) | |
00a7cdc5 NC |
4206 | { |
4207 | /* If the indicies are the same, do not return 0 | |
4208 | here, but continue to try the next comparison. */ | |
4209 | if (sec1->target_index - sec2->target_index != 0) | |
4210 | return sec1->target_index - sec2->target_index; | |
4211 | } | |
252b5132 RH |
4212 | else |
4213 | return 1; | |
4214 | } | |
00a7cdc5 | 4215 | else if (TOEND (sec2)) |
252b5132 RH |
4216 | return -1; |
4217 | ||
4218 | #undef TOEND | |
4219 | ||
00a7cdc5 NC |
4220 | /* Sort by size, to put zero sized sections |
4221 | before others at the same address. */ | |
252b5132 | 4222 | |
eea6121a AM |
4223 | size1 = (sec1->flags & SEC_LOAD) ? sec1->size : 0; |
4224 | size2 = (sec2->flags & SEC_LOAD) ? sec2->size : 0; | |
eecdbe52 JJ |
4225 | |
4226 | if (size1 < size2) | |
252b5132 | 4227 | return -1; |
eecdbe52 | 4228 | if (size1 > size2) |
252b5132 RH |
4229 | return 1; |
4230 | ||
4231 | return sec1->target_index - sec2->target_index; | |
4232 | } | |
4233 | ||
340b6d91 AC |
4234 | /* Ian Lance Taylor writes: |
4235 | ||
4236 | We shouldn't be using % with a negative signed number. That's just | |
4237 | not good. We have to make sure either that the number is not | |
4238 | negative, or that the number has an unsigned type. When the types | |
4239 | are all the same size they wind up as unsigned. When file_ptr is a | |
4240 | larger signed type, the arithmetic winds up as signed long long, | |
4241 | which is wrong. | |
4242 | ||
4243 | What we're trying to say here is something like ``increase OFF by | |
4244 | the least amount that will cause it to be equal to the VMA modulo | |
4245 | the page size.'' */ | |
4246 | /* In other words, something like: | |
4247 | ||
4248 | vma_offset = m->sections[0]->vma % bed->maxpagesize; | |
4249 | off_offset = off % bed->maxpagesize; | |
4250 | if (vma_offset < off_offset) | |
4251 | adjustment = vma_offset + bed->maxpagesize - off_offset; | |
4252 | else | |
4253 | adjustment = vma_offset - off_offset; | |
4254 | ||
4255 | which can can be collapsed into the expression below. */ | |
4256 | ||
4257 | static file_ptr | |
4258 | vma_page_aligned_bias (bfd_vma vma, ufile_ptr off, bfd_vma maxpagesize) | |
4259 | { | |
4260 | return ((vma - off) % maxpagesize); | |
4261 | } | |
4262 | ||
252b5132 RH |
4263 | /* Assign file positions to the sections based on the mapping from |
4264 | sections to segments. This function also sets up some fields in | |
f3520d2f | 4265 | the file header. */ |
252b5132 | 4266 | |
b34976b6 | 4267 | static bfd_boolean |
f3520d2f AM |
4268 | assign_file_positions_for_load_sections (bfd *abfd, |
4269 | struct bfd_link_info *link_info) | |
252b5132 RH |
4270 | { |
4271 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
252b5132 | 4272 | struct elf_segment_map *m; |
252b5132 | 4273 | Elf_Internal_Phdr *phdrs; |
252b5132 | 4274 | Elf_Internal_Phdr *p; |
f3520d2f | 4275 | file_ptr off, voff; |
3f570048 | 4276 | bfd_size_type maxpagesize; |
f3520d2f | 4277 | unsigned int alloc; |
0920dee7 | 4278 | unsigned int i, j; |
252b5132 | 4279 | |
e36284ab AM |
4280 | if (link_info == NULL |
4281 | && !elf_modify_segment_map (abfd, link_info)) | |
8ded5a0f | 4282 | return FALSE; |
252b5132 | 4283 | |
8ded5a0f | 4284 | alloc = 0; |
252b5132 | 4285 | for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next) |
8ded5a0f | 4286 | ++alloc; |
252b5132 RH |
4287 | |
4288 | elf_elfheader (abfd)->e_phoff = bed->s->sizeof_ehdr; | |
4289 | elf_elfheader (abfd)->e_phentsize = bed->s->sizeof_phdr; | |
8ded5a0f | 4290 | elf_elfheader (abfd)->e_phnum = alloc; |
252b5132 | 4291 | |
62d7a5f6 | 4292 | if (elf_tdata (abfd)->program_header_size == (bfd_size_type) -1) |
8ded5a0f AM |
4293 | elf_tdata (abfd)->program_header_size = alloc * bed->s->sizeof_phdr; |
4294 | else | |
4295 | BFD_ASSERT (elf_tdata (abfd)->program_header_size | |
59e0647f | 4296 | >= alloc * bed->s->sizeof_phdr); |
252b5132 RH |
4297 | |
4298 | if (alloc == 0) | |
f3520d2f | 4299 | { |
8ded5a0f AM |
4300 | elf_tdata (abfd)->next_file_pos = bed->s->sizeof_ehdr; |
4301 | return TRUE; | |
f3520d2f | 4302 | } |
252b5132 | 4303 | |
d0fb9a8d | 4304 | phdrs = bfd_alloc2 (abfd, alloc, sizeof (Elf_Internal_Phdr)); |
f3520d2f | 4305 | elf_tdata (abfd)->phdr = phdrs; |
252b5132 | 4306 | if (phdrs == NULL) |
b34976b6 | 4307 | return FALSE; |
252b5132 | 4308 | |
3f570048 AM |
4309 | maxpagesize = 1; |
4310 | if ((abfd->flags & D_PAGED) != 0) | |
4311 | maxpagesize = bed->maxpagesize; | |
4312 | ||
252b5132 RH |
4313 | off = bed->s->sizeof_ehdr; |
4314 | off += alloc * bed->s->sizeof_phdr; | |
4315 | ||
0920dee7 | 4316 | for (m = elf_tdata (abfd)->segment_map, p = phdrs, j = 0; |
252b5132 | 4317 | m != NULL; |
0920dee7 | 4318 | m = m->next, p++, j++) |
252b5132 | 4319 | { |
252b5132 RH |
4320 | asection **secpp; |
4321 | ||
4322 | /* If elf_segment_map is not from map_sections_to_segments, the | |
47d9a591 | 4323 | sections may not be correctly ordered. NOTE: sorting should |
52e9b619 MS |
4324 | not be done to the PT_NOTE section of a corefile, which may |
4325 | contain several pseudo-sections artificially created by bfd. | |
4326 | Sorting these pseudo-sections breaks things badly. */ | |
47d9a591 AM |
4327 | if (m->count > 1 |
4328 | && !(elf_elfheader (abfd)->e_type == ET_CORE | |
52e9b619 | 4329 | && m->p_type == PT_NOTE)) |
252b5132 RH |
4330 | qsort (m->sections, (size_t) m->count, sizeof (asection *), |
4331 | elf_sort_sections); | |
4332 | ||
b301b248 AM |
4333 | /* An ELF segment (described by Elf_Internal_Phdr) may contain a |
4334 | number of sections with contents contributing to both p_filesz | |
4335 | and p_memsz, followed by a number of sections with no contents | |
4336 | that just contribute to p_memsz. In this loop, OFF tracks next | |
4337 | available file offset for PT_LOAD and PT_NOTE segments. VOFF is | |
4338 | an adjustment we use for segments that have no file contents | |
4339 | but need zero filled memory allocation. */ | |
4340 | voff = 0; | |
252b5132 | 4341 | p->p_type = m->p_type; |
28a7f3e7 | 4342 | p->p_flags = m->p_flags; |
252b5132 | 4343 | |
3f570048 AM |
4344 | if (m->count == 0) |
4345 | p->p_vaddr = 0; | |
4346 | else | |
3271a814 | 4347 | p->p_vaddr = m->sections[0]->vma - m->p_vaddr_offset; |
3f570048 AM |
4348 | |
4349 | if (m->p_paddr_valid) | |
4350 | p->p_paddr = m->p_paddr; | |
4351 | else if (m->count == 0) | |
4352 | p->p_paddr = 0; | |
4353 | else | |
4354 | p->p_paddr = m->sections[0]->lma; | |
4355 | ||
4356 | if (p->p_type == PT_LOAD | |
4357 | && (abfd->flags & D_PAGED) != 0) | |
4358 | { | |
4359 | /* p_align in demand paged PT_LOAD segments effectively stores | |
4360 | the maximum page size. When copying an executable with | |
4361 | objcopy, we set m->p_align from the input file. Use this | |
4362 | value for maxpagesize rather than bed->maxpagesize, which | |
4363 | may be different. Note that we use maxpagesize for PT_TLS | |
4364 | segment alignment later in this function, so we are relying | |
4365 | on at least one PT_LOAD segment appearing before a PT_TLS | |
4366 | segment. */ | |
4367 | if (m->p_align_valid) | |
4368 | maxpagesize = m->p_align; | |
4369 | ||
4370 | p->p_align = maxpagesize; | |
4371 | } | |
4372 | else if (m->count == 0) | |
4373 | p->p_align = 1 << bed->s->log_file_align; | |
3271a814 NS |
4374 | else if (m->p_align_valid) |
4375 | p->p_align = m->p_align; | |
3f570048 AM |
4376 | else |
4377 | p->p_align = 0; | |
4378 | ||
252b5132 | 4379 | if (p->p_type == PT_LOAD |
b301b248 | 4380 | && m->count > 0) |
252b5132 | 4381 | { |
b301b248 AM |
4382 | bfd_size_type align; |
4383 | bfd_vma adjust; | |
a49e53ed | 4384 | unsigned int align_power = 0; |
b301b248 | 4385 | |
3271a814 NS |
4386 | if (m->p_align_valid) |
4387 | align = p->p_align; | |
4388 | else | |
252b5132 | 4389 | { |
3271a814 NS |
4390 | for (i = 0, secpp = m->sections; i < m->count; i++, secpp++) |
4391 | { | |
4392 | unsigned int secalign; | |
4393 | ||
4394 | secalign = bfd_get_section_alignment (abfd, *secpp); | |
4395 | if (secalign > align_power) | |
4396 | align_power = secalign; | |
4397 | } | |
4398 | align = (bfd_size_type) 1 << align_power; | |
4399 | if (align < maxpagesize) | |
4400 | align = maxpagesize; | |
b301b248 | 4401 | } |
252b5132 | 4402 | |
b301b248 AM |
4403 | adjust = vma_page_aligned_bias (m->sections[0]->vma, off, align); |
4404 | off += adjust; | |
4405 | if (adjust != 0 | |
4406 | && !m->includes_filehdr | |
4407 | && !m->includes_phdrs | |
4408 | && (ufile_ptr) off >= align) | |
4409 | { | |
4410 | /* If the first section isn't loadable, the same holds for | |
4411 | any other sections. Since the segment won't need file | |
4412 | space, we can make p_offset overlap some prior segment. | |
4413 | However, .tbss is special. If a segment starts with | |
4414 | .tbss, we need to look at the next section to decide | |
4415 | whether the segment has any loadable sections. */ | |
4416 | i = 0; | |
252e386e AM |
4417 | while ((m->sections[i]->flags & SEC_LOAD) == 0 |
4418 | && (m->sections[i]->flags & SEC_HAS_CONTENTS) == 0) | |
b301b248 AM |
4419 | { |
4420 | if ((m->sections[i]->flags & SEC_THREAD_LOCAL) == 0 | |
4421 | || ++i >= m->count) | |
4422 | { | |
4423 | off -= adjust; | |
4424 | voff = adjust - align; | |
4425 | break; | |
4426 | } | |
4427 | } | |
252b5132 RH |
4428 | } |
4429 | } | |
b1a6d0b1 NC |
4430 | /* Make sure the .dynamic section is the first section in the |
4431 | PT_DYNAMIC segment. */ | |
4432 | else if (p->p_type == PT_DYNAMIC | |
4433 | && m->count > 1 | |
4434 | && strcmp (m->sections[0]->name, ".dynamic") != 0) | |
4435 | { | |
4436 | _bfd_error_handler | |
b301b248 AM |
4437 | (_("%B: The first section in the PT_DYNAMIC segment is not the .dynamic section"), |
4438 | abfd); | |
b1a6d0b1 NC |
4439 | bfd_set_error (bfd_error_bad_value); |
4440 | return FALSE; | |
4441 | } | |
252b5132 | 4442 | |
252b5132 RH |
4443 | p->p_offset = 0; |
4444 | p->p_filesz = 0; | |
4445 | p->p_memsz = 0; | |
4446 | ||
4447 | if (m->includes_filehdr) | |
4448 | { | |
4449 | if (! m->p_flags_valid) | |
4450 | p->p_flags |= PF_R; | |
252b5132 RH |
4451 | p->p_filesz = bed->s->sizeof_ehdr; |
4452 | p->p_memsz = bed->s->sizeof_ehdr; | |
4453 | if (m->count > 0) | |
4454 | { | |
4455 | BFD_ASSERT (p->p_type == PT_LOAD); | |
4456 | ||
4457 | if (p->p_vaddr < (bfd_vma) off) | |
4458 | { | |
caf47ea6 | 4459 | (*_bfd_error_handler) |
b301b248 AM |
4460 | (_("%B: Not enough room for program headers, try linking with -N"), |
4461 | abfd); | |
252b5132 | 4462 | bfd_set_error (bfd_error_bad_value); |
b34976b6 | 4463 | return FALSE; |
252b5132 RH |
4464 | } |
4465 | ||
4466 | p->p_vaddr -= off; | |
4467 | if (! m->p_paddr_valid) | |
4468 | p->p_paddr -= off; | |
4469 | } | |
252b5132 RH |
4470 | } |
4471 | ||
4472 | if (m->includes_phdrs) | |
4473 | { | |
4474 | if (! m->p_flags_valid) | |
4475 | p->p_flags |= PF_R; | |
4476 | ||
f3520d2f | 4477 | if (!m->includes_filehdr) |
252b5132 RH |
4478 | { |
4479 | p->p_offset = bed->s->sizeof_ehdr; | |
4480 | ||
4481 | if (m->count > 0) | |
4482 | { | |
4483 | BFD_ASSERT (p->p_type == PT_LOAD); | |
4484 | p->p_vaddr -= off - p->p_offset; | |
4485 | if (! m->p_paddr_valid) | |
4486 | p->p_paddr -= off - p->p_offset; | |
4487 | } | |
252b5132 RH |
4488 | } |
4489 | ||
4490 | p->p_filesz += alloc * bed->s->sizeof_phdr; | |
4491 | p->p_memsz += alloc * bed->s->sizeof_phdr; | |
4492 | } | |
4493 | ||
4494 | if (p->p_type == PT_LOAD | |
4495 | || (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core)) | |
4496 | { | |
4497 | if (! m->includes_filehdr && ! m->includes_phdrs) | |
b301b248 | 4498 | p->p_offset = off + voff; |
252b5132 RH |
4499 | else |
4500 | { | |
4501 | file_ptr adjust; | |
4502 | ||
4503 | adjust = off - (p->p_offset + p->p_filesz); | |
4504 | p->p_filesz += adjust; | |
4505 | p->p_memsz += adjust; | |
4506 | } | |
4507 | } | |
4508 | ||
1ea63fd2 AM |
4509 | /* Set up p_filesz, p_memsz, p_align and p_flags from the section |
4510 | maps. Set filepos for sections in PT_LOAD segments, and in | |
4511 | core files, for sections in PT_NOTE segments. | |
4512 | assign_file_positions_for_non_load_sections will set filepos | |
4513 | for other sections and update p_filesz for other segments. */ | |
252b5132 RH |
4514 | for (i = 0, secpp = m->sections; i < m->count; i++, secpp++) |
4515 | { | |
4516 | asection *sec; | |
4517 | flagword flags; | |
4518 | bfd_size_type align; | |
627b32bc | 4519 | Elf_Internal_Shdr *this_hdr; |
252b5132 RH |
4520 | |
4521 | sec = *secpp; | |
4522 | flags = sec->flags; | |
3f570048 | 4523 | align = (bfd_size_type) 1 << bfd_get_section_alignment (abfd, sec); |
252b5132 | 4524 | |
b301b248 AM |
4525 | if (p->p_type == PT_LOAD |
4526 | || p->p_type == PT_TLS) | |
252b5132 | 4527 | { |
8c252fd9 | 4528 | bfd_signed_vma adjust = sec->lma - (p->p_paddr + p->p_memsz); |
252b5132 | 4529 | |
0e922b77 AM |
4530 | if ((flags & SEC_LOAD) != 0 |
4531 | || ((flags & SEC_ALLOC) != 0 | |
4532 | && ((flags & SEC_THREAD_LOCAL) == 0 | |
4533 | || p->p_type == PT_TLS))) | |
252b5132 | 4534 | { |
252b5132 | 4535 | if (adjust < 0) |
b301b248 AM |
4536 | { |
4537 | (*_bfd_error_handler) | |
4538 | (_("%B: section %A lma 0x%lx overlaps previous sections"), | |
4539 | abfd, sec, (unsigned long) sec->lma); | |
4540 | adjust = 0; | |
4541 | } | |
252b5132 | 4542 | p->p_memsz += adjust; |
0e922b77 AM |
4543 | |
4544 | if ((flags & SEC_LOAD) != 0) | |
4545 | { | |
4546 | off += adjust; | |
4547 | p->p_filesz += adjust; | |
4548 | } | |
252b5132 | 4549 | } |
252b5132 RH |
4550 | } |
4551 | ||
627b32bc | 4552 | this_hdr = &elf_section_data (sec)->this_hdr; |
252b5132 RH |
4553 | if (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core) |
4554 | { | |
b301b248 AM |
4555 | /* The section at i == 0 is the one that actually contains |
4556 | everything. */ | |
4a938328 MS |
4557 | if (i == 0) |
4558 | { | |
627b32bc | 4559 | this_hdr->sh_offset = sec->filepos = off; |
eea6121a | 4560 | off += sec->size; |
b301b248 AM |
4561 | p->p_filesz = sec->size; |
4562 | p->p_memsz = 0; | |
4563 | p->p_align = 1; | |
252b5132 | 4564 | } |
4a938328 | 4565 | else |
252b5132 | 4566 | { |
b301b248 | 4567 | /* The rest are fake sections that shouldn't be written. */ |
252b5132 | 4568 | sec->filepos = 0; |
eea6121a | 4569 | sec->size = 0; |
b301b248 AM |
4570 | sec->flags = 0; |
4571 | continue; | |
252b5132 | 4572 | } |
252b5132 RH |
4573 | } |
4574 | else | |
4575 | { | |
b301b248 AM |
4576 | if (p->p_type == PT_LOAD) |
4577 | { | |
627b32bc | 4578 | this_hdr->sh_offset = sec->filepos = off + voff; |
5efb6261 AM |
4579 | /* FIXME: The SEC_HAS_CONTENTS test here dates back to |
4580 | 1997, and the exact reason for it isn't clear. One | |
4581 | plausible explanation is that it is to work around | |
4582 | a problem we have with linker scripts using data | |
4583 | statements in NOLOAD sections. I don't think it | |
4584 | makes a great deal of sense to have such a section | |
4585 | assigned to a PT_LOAD segment, but apparently | |
4586 | people do this. The data statement results in a | |
4587 | bfd_data_link_order being built, and these need | |
4588 | section contents to write into. Eventually, we get | |
4589 | to _bfd_elf_write_object_contents which writes any | |
4590 | section with contents to the output. Make room | |
4591 | here for the write, so that following segments are | |
4592 | not trashed. */ | |
4593 | if ((flags & SEC_LOAD) != 0 | |
4594 | || (flags & SEC_HAS_CONTENTS) != 0) | |
b301b248 | 4595 | off += sec->size; |
627b32bc AM |
4596 | else |
4597 | /* If we aren't making room for this section, then | |
4598 | it must be SHT_NOBITS regardless of what we've | |
4599 | set via struct bfd_elf_special_section. */ | |
4600 | this_hdr->sh_type = SHT_NOBITS; | |
b301b248 | 4601 | } |
252b5132 | 4602 | |
5efb6261 | 4603 | if ((flags & SEC_LOAD) != 0) |
b301b248 AM |
4604 | { |
4605 | p->p_filesz += sec->size; | |
627b32bc AM |
4606 | /* SEC_LOAD without SEC_ALLOC is a weird combination |
4607 | used by note sections to signify that a PT_NOTE | |
4608 | segment should be created. These take file space | |
4609 | but are not actually loaded into memory. */ | |
4610 | if ((flags & SEC_ALLOC) != 0) | |
4611 | p->p_memsz += sec->size; | |
b301b248 | 4612 | } |
4b6c0f2f | 4613 | |
b301b248 AM |
4614 | /* .tbss is special. It doesn't contribute to p_memsz of |
4615 | normal segments. */ | |
1ea63fd2 AM |
4616 | else if ((flags & SEC_ALLOC) != 0 |
4617 | && ((flags & SEC_THREAD_LOCAL) == 0 | |
4618 | || p->p_type == PT_TLS)) | |
b301b248 | 4619 | p->p_memsz += sec->size; |
252b5132 | 4620 | |
13ae64f3 | 4621 | if (p->p_type == PT_TLS |
eea6121a | 4622 | && sec->size == 0 |
13ae64f3 JJ |
4623 | && (sec->flags & SEC_HAS_CONTENTS) == 0) |
4624 | { | |
3a800eb9 AM |
4625 | struct bfd_link_order *o = sec->map_tail.link_order; |
4626 | if (o != NULL) | |
4627 | p->p_memsz += o->offset + o->size; | |
13ae64f3 JJ |
4628 | } |
4629 | ||
c9df6640 L |
4630 | if (p->p_type == PT_GNU_RELRO) |
4631 | p->p_align = 1; | |
4632 | else if (align > p->p_align | |
3271a814 | 4633 | && !m->p_align_valid |
c9df6640 L |
4634 | && (p->p_type != PT_LOAD |
4635 | || (abfd->flags & D_PAGED) == 0)) | |
252b5132 RH |
4636 | p->p_align = align; |
4637 | } | |
4638 | ||
4639 | if (! m->p_flags_valid) | |
4640 | { | |
4641 | p->p_flags |= PF_R; | |
4642 | if ((flags & SEC_CODE) != 0) | |
4643 | p->p_flags |= PF_X; | |
4644 | if ((flags & SEC_READONLY) == 0) | |
4645 | p->p_flags |= PF_W; | |
4646 | } | |
4647 | } | |
0920dee7 | 4648 | |
7c928300 AM |
4649 | /* Check that all sections are in a PT_LOAD segment. |
4650 | Don't check funky gdb generated core files. */ | |
4651 | if (p->p_type == PT_LOAD && bfd_get_format (abfd) != bfd_core) | |
0920dee7 L |
4652 | for (i = 0, secpp = m->sections; i < m->count; i++, secpp++) |
4653 | { | |
4654 | Elf_Internal_Shdr *this_hdr; | |
4655 | asection *sec; | |
4656 | ||
4657 | sec = *secpp; | |
4658 | this_hdr = &(elf_section_data(sec)->this_hdr); | |
4659 | if (this_hdr->sh_size != 0 | |
4660 | && !ELF_IS_SECTION_IN_SEGMENT_FILE (this_hdr, p)) | |
4661 | { | |
4662 | (*_bfd_error_handler) | |
4663 | (_("%B: section `%A' can't be allocated in segment %d"), | |
4664 | abfd, sec, j); | |
4665 | bfd_set_error (bfd_error_bad_value); | |
4666 | return FALSE; | |
4667 | } | |
4668 | } | |
252b5132 RH |
4669 | } |
4670 | ||
f3520d2f AM |
4671 | elf_tdata (abfd)->next_file_pos = off; |
4672 | return TRUE; | |
4673 | } | |
4674 | ||
4675 | /* Assign file positions for the other sections. */ | |
4676 | ||
4677 | static bfd_boolean | |
4678 | assign_file_positions_for_non_load_sections (bfd *abfd, | |
4679 | struct bfd_link_info *link_info) | |
4680 | { | |
4681 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
4682 | Elf_Internal_Shdr **i_shdrpp; | |
4683 | Elf_Internal_Shdr **hdrpp; | |
4684 | Elf_Internal_Phdr *phdrs; | |
4685 | Elf_Internal_Phdr *p; | |
4686 | struct elf_segment_map *m; | |
4687 | bfd_vma filehdr_vaddr, filehdr_paddr; | |
4688 | bfd_vma phdrs_vaddr, phdrs_paddr; | |
4689 | file_ptr off; | |
4690 | unsigned int num_sec; | |
4691 | unsigned int i; | |
4692 | unsigned int count; | |
4693 | ||
5c182d5f AM |
4694 | i_shdrpp = elf_elfsections (abfd); |
4695 | num_sec = elf_numsections (abfd); | |
f3520d2f | 4696 | off = elf_tdata (abfd)->next_file_pos; |
5c182d5f AM |
4697 | for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++) |
4698 | { | |
4699 | struct elf_obj_tdata *tdata = elf_tdata (abfd); | |
4700 | Elf_Internal_Shdr *hdr; | |
4701 | ||
4702 | hdr = *hdrpp; | |
4703 | if (hdr->bfd_section != NULL | |
252e386e AM |
4704 | && (hdr->bfd_section->filepos != 0 |
4705 | || (hdr->sh_type == SHT_NOBITS | |
4706 | && hdr->contents == NULL))) | |
627b32bc | 4707 | BFD_ASSERT (hdr->sh_offset == hdr->bfd_section->filepos); |
5c182d5f AM |
4708 | else if ((hdr->sh_flags & SHF_ALLOC) != 0) |
4709 | { | |
49c13adb L |
4710 | if (hdr->sh_size != 0) |
4711 | ((*_bfd_error_handler) | |
4712 | (_("%B: warning: allocated section `%s' not in segment"), | |
3ba71138 L |
4713 | abfd, |
4714 | (hdr->bfd_section == NULL | |
4715 | ? "*unknown*" | |
4716 | : hdr->bfd_section->name))); | |
4717 | /* We don't need to page align empty sections. */ | |
4718 | if ((abfd->flags & D_PAGED) != 0 && hdr->sh_size != 0) | |
5c182d5f AM |
4719 | off += vma_page_aligned_bias (hdr->sh_addr, off, |
4720 | bed->maxpagesize); | |
4721 | else | |
4722 | off += vma_page_aligned_bias (hdr->sh_addr, off, | |
4723 | hdr->sh_addralign); | |
4724 | off = _bfd_elf_assign_file_position_for_section (hdr, off, | |
4725 | FALSE); | |
4726 | } | |
4727 | else if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA) | |
4728 | && hdr->bfd_section == NULL) | |
4729 | || hdr == i_shdrpp[tdata->symtab_section] | |
4730 | || hdr == i_shdrpp[tdata->symtab_shndx_section] | |
4731 | || hdr == i_shdrpp[tdata->strtab_section]) | |
4732 | hdr->sh_offset = -1; | |
4733 | else | |
4734 | off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE); | |
4735 | ||
4736 | if (i == SHN_LORESERVE - 1) | |
4737 | { | |
4738 | i += SHN_HIRESERVE + 1 - SHN_LORESERVE; | |
4739 | hdrpp += SHN_HIRESERVE + 1 - SHN_LORESERVE; | |
4740 | } | |
4741 | } | |
4742 | ||
252b5132 RH |
4743 | /* Now that we have set the section file positions, we can set up |
4744 | the file positions for the non PT_LOAD segments. */ | |
f3520d2f AM |
4745 | count = 0; |
4746 | filehdr_vaddr = 0; | |
4747 | filehdr_paddr = 0; | |
4748 | phdrs_vaddr = bed->maxpagesize + bed->s->sizeof_ehdr; | |
4749 | phdrs_paddr = 0; | |
4750 | phdrs = elf_tdata (abfd)->phdr; | |
4751 | for (m = elf_tdata (abfd)->segment_map, p = phdrs; | |
4752 | m != NULL; | |
4753 | m = m->next, p++) | |
4754 | { | |
4755 | ++count; | |
4756 | if (p->p_type != PT_LOAD) | |
4757 | continue; | |
4758 | ||
4759 | if (m->includes_filehdr) | |
4760 | { | |
4761 | filehdr_vaddr = p->p_vaddr; | |
4762 | filehdr_paddr = p->p_paddr; | |
4763 | } | |
4764 | if (m->includes_phdrs) | |
4765 | { | |
4766 | phdrs_vaddr = p->p_vaddr; | |
4767 | phdrs_paddr = p->p_paddr; | |
4768 | if (m->includes_filehdr) | |
4769 | { | |
4770 | phdrs_vaddr += bed->s->sizeof_ehdr; | |
4771 | phdrs_paddr += bed->s->sizeof_ehdr; | |
4772 | } | |
4773 | } | |
4774 | } | |
4775 | ||
252b5132 RH |
4776 | for (m = elf_tdata (abfd)->segment_map, p = phdrs; |
4777 | m != NULL; | |
4778 | m = m->next, p++) | |
4779 | { | |
1ea63fd2 | 4780 | if (m->count != 0) |
252b5132 | 4781 | { |
1ea63fd2 AM |
4782 | if (p->p_type != PT_LOAD |
4783 | && (p->p_type != PT_NOTE || bfd_get_format (abfd) != bfd_core)) | |
229fcec5 | 4784 | { |
1ea63fd2 AM |
4785 | Elf_Internal_Shdr *hdr; |
4786 | BFD_ASSERT (!m->includes_filehdr && !m->includes_phdrs); | |
4787 | ||
4788 | hdr = &elf_section_data (m->sections[m->count - 1])->this_hdr; | |
4789 | p->p_filesz = (m->sections[m->count - 1]->filepos | |
4790 | - m->sections[0]->filepos); | |
4791 | if (hdr->sh_type != SHT_NOBITS) | |
4792 | p->p_filesz += hdr->sh_size; | |
4793 | ||
4794 | p->p_offset = m->sections[0]->filepos; | |
229fcec5 | 4795 | } |
252b5132 | 4796 | } |
1ea63fd2 | 4797 | else |
252b5132 RH |
4798 | { |
4799 | if (m->includes_filehdr) | |
4800 | { | |
4801 | p->p_vaddr = filehdr_vaddr; | |
4802 | if (! m->p_paddr_valid) | |
4803 | p->p_paddr = filehdr_paddr; | |
4804 | } | |
4805 | else if (m->includes_phdrs) | |
4806 | { | |
4807 | p->p_vaddr = phdrs_vaddr; | |
4808 | if (! m->p_paddr_valid) | |
4809 | p->p_paddr = phdrs_paddr; | |
4810 | } | |
8c37241b JJ |
4811 | else if (p->p_type == PT_GNU_RELRO) |
4812 | { | |
4813 | Elf_Internal_Phdr *lp; | |
4814 | ||
4815 | for (lp = phdrs; lp < phdrs + count; ++lp) | |
4816 | { | |
4817 | if (lp->p_type == PT_LOAD | |
4818 | && lp->p_vaddr <= link_info->relro_end | |
4819 | && lp->p_vaddr >= link_info->relro_start | |
e36284ab AM |
4820 | && (lp->p_vaddr + lp->p_filesz |
4821 | >= link_info->relro_end)) | |
8c37241b JJ |
4822 | break; |
4823 | } | |
4824 | ||
4825 | if (lp < phdrs + count | |
4826 | && link_info->relro_end > lp->p_vaddr) | |
4827 | { | |
4828 | p->p_vaddr = lp->p_vaddr; | |
4829 | p->p_paddr = lp->p_paddr; | |
4830 | p->p_offset = lp->p_offset; | |
4831 | p->p_filesz = link_info->relro_end - lp->p_vaddr; | |
4832 | p->p_memsz = p->p_filesz; | |
4833 | p->p_align = 1; | |
4834 | p->p_flags = (lp->p_flags & ~PF_W); | |
4835 | } | |
4836 | else | |
4837 | { | |
4838 | memset (p, 0, sizeof *p); | |
4839 | p->p_type = PT_NULL; | |
4840 | } | |
4841 | } | |
252b5132 RH |
4842 | } |
4843 | } | |
4844 | ||
252b5132 RH |
4845 | elf_tdata (abfd)->next_file_pos = off; |
4846 | ||
b34976b6 | 4847 | return TRUE; |
252b5132 RH |
4848 | } |
4849 | ||
252b5132 RH |
4850 | /* Work out the file positions of all the sections. This is called by |
4851 | _bfd_elf_compute_section_file_positions. All the section sizes and | |
4852 | VMAs must be known before this is called. | |
4853 | ||
e0638f70 AM |
4854 | Reloc sections come in two flavours: Those processed specially as |
4855 | "side-channel" data attached to a section to which they apply, and | |
4856 | those that bfd doesn't process as relocations. The latter sort are | |
4857 | stored in a normal bfd section by bfd_section_from_shdr. We don't | |
4858 | consider the former sort here, unless they form part of the loadable | |
4859 | image. Reloc sections not assigned here will be handled later by | |
4860 | assign_file_positions_for_relocs. | |
252b5132 RH |
4861 | |
4862 | We also don't set the positions of the .symtab and .strtab here. */ | |
4863 | ||
b34976b6 | 4864 | static bfd_boolean |
c84fca4d AO |
4865 | assign_file_positions_except_relocs (bfd *abfd, |
4866 | struct bfd_link_info *link_info) | |
252b5132 | 4867 | { |
5c182d5f AM |
4868 | struct elf_obj_tdata *tdata = elf_tdata (abfd); |
4869 | Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd); | |
252b5132 | 4870 | file_ptr off; |
9c5bfbb7 | 4871 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
252b5132 RH |
4872 | |
4873 | if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0 | |
4874 | && bfd_get_format (abfd) != bfd_core) | |
4875 | { | |
5c182d5f AM |
4876 | Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd); |
4877 | unsigned int num_sec = elf_numsections (abfd); | |
252b5132 RH |
4878 | Elf_Internal_Shdr **hdrpp; |
4879 | unsigned int i; | |
4880 | ||
4881 | /* Start after the ELF header. */ | |
4882 | off = i_ehdrp->e_ehsize; | |
4883 | ||
4884 | /* We are not creating an executable, which means that we are | |
4885 | not creating a program header, and that the actual order of | |
4886 | the sections in the file is unimportant. */ | |
9ad5cbcf | 4887 | for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++) |
252b5132 RH |
4888 | { |
4889 | Elf_Internal_Shdr *hdr; | |
4890 | ||
4891 | hdr = *hdrpp; | |
e0638f70 AM |
4892 | if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA) |
4893 | && hdr->bfd_section == NULL) | |
9ad5cbcf AM |
4894 | || i == tdata->symtab_section |
4895 | || i == tdata->symtab_shndx_section | |
252b5132 RH |
4896 | || i == tdata->strtab_section) |
4897 | { | |
4898 | hdr->sh_offset = -1; | |
252b5132 | 4899 | } |
9ad5cbcf | 4900 | else |
b34976b6 | 4901 | off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE); |
252b5132 | 4902 | |
9ad5cbcf AM |
4903 | if (i == SHN_LORESERVE - 1) |
4904 | { | |
4905 | i += SHN_HIRESERVE + 1 - SHN_LORESERVE; | |
4906 | hdrpp += SHN_HIRESERVE + 1 - SHN_LORESERVE; | |
4907 | } | |
252b5132 RH |
4908 | } |
4909 | } | |
4910 | else | |
4911 | { | |
f3520d2f AM |
4912 | unsigned int alloc; |
4913 | ||
252b5132 RH |
4914 | /* Assign file positions for the loaded sections based on the |
4915 | assignment of sections to segments. */ | |
f3520d2f AM |
4916 | if (!assign_file_positions_for_load_sections (abfd, link_info)) |
4917 | return FALSE; | |
4918 | ||
4919 | /* And for non-load sections. */ | |
4920 | if (!assign_file_positions_for_non_load_sections (abfd, link_info)) | |
4921 | return FALSE; | |
4922 | ||
e36284ab AM |
4923 | if (bed->elf_backend_modify_program_headers != NULL) |
4924 | { | |
4925 | if (!(*bed->elf_backend_modify_program_headers) (abfd, link_info)) | |
4926 | return FALSE; | |
4927 | } | |
4928 | ||
f3520d2f AM |
4929 | /* Write out the program headers. */ |
4930 | alloc = tdata->program_header_size / bed->s->sizeof_phdr; | |
4931 | if (bfd_seek (abfd, (bfd_signed_vma) bed->s->sizeof_ehdr, SEEK_SET) != 0 | |
4932 | || bed->s->write_out_phdrs (abfd, tdata->phdr, alloc) != 0) | |
b34976b6 | 4933 | return FALSE; |
252b5132 | 4934 | |
5c182d5f | 4935 | off = tdata->next_file_pos; |
252b5132 RH |
4936 | } |
4937 | ||
4938 | /* Place the section headers. */ | |
45d6a902 | 4939 | off = align_file_position (off, 1 << bed->s->log_file_align); |
252b5132 RH |
4940 | i_ehdrp->e_shoff = off; |
4941 | off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize; | |
4942 | ||
5c182d5f | 4943 | tdata->next_file_pos = off; |
252b5132 | 4944 | |
b34976b6 | 4945 | return TRUE; |
252b5132 RH |
4946 | } |
4947 | ||
b34976b6 | 4948 | static bfd_boolean |
217aa764 | 4949 | prep_headers (bfd *abfd) |
252b5132 RH |
4950 | { |
4951 | Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */ | |
4952 | Elf_Internal_Phdr *i_phdrp = 0; /* Program header table, internal form */ | |
4953 | Elf_Internal_Shdr **i_shdrp; /* Section header table, internal form */ | |
2b0f7ef9 | 4954 | struct elf_strtab_hash *shstrtab; |
9c5bfbb7 | 4955 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
252b5132 RH |
4956 | |
4957 | i_ehdrp = elf_elfheader (abfd); | |
4958 | i_shdrp = elf_elfsections (abfd); | |
4959 | ||
2b0f7ef9 | 4960 | shstrtab = _bfd_elf_strtab_init (); |
252b5132 | 4961 | if (shstrtab == NULL) |
b34976b6 | 4962 | return FALSE; |
252b5132 RH |
4963 | |
4964 | elf_shstrtab (abfd) = shstrtab; | |
4965 | ||
4966 | i_ehdrp->e_ident[EI_MAG0] = ELFMAG0; | |
4967 | i_ehdrp->e_ident[EI_MAG1] = ELFMAG1; | |
4968 | i_ehdrp->e_ident[EI_MAG2] = ELFMAG2; | |
4969 | i_ehdrp->e_ident[EI_MAG3] = ELFMAG3; | |
4970 | ||
4971 | i_ehdrp->e_ident[EI_CLASS] = bed->s->elfclass; | |
4972 | i_ehdrp->e_ident[EI_DATA] = | |
4973 | bfd_big_endian (abfd) ? ELFDATA2MSB : ELFDATA2LSB; | |
4974 | i_ehdrp->e_ident[EI_VERSION] = bed->s->ev_current; | |
4975 | ||
252b5132 RH |
4976 | if ((abfd->flags & DYNAMIC) != 0) |
4977 | i_ehdrp->e_type = ET_DYN; | |
4978 | else if ((abfd->flags & EXEC_P) != 0) | |
4979 | i_ehdrp->e_type = ET_EXEC; | |
4980 | else if (bfd_get_format (abfd) == bfd_core) | |
4981 | i_ehdrp->e_type = ET_CORE; | |
4982 | else | |
4983 | i_ehdrp->e_type = ET_REL; | |
4984 | ||
4985 | switch (bfd_get_arch (abfd)) | |
4986 | { | |
4987 | case bfd_arch_unknown: | |
4988 | i_ehdrp->e_machine = EM_NONE; | |
4989 | break; | |
aa4f99bb AO |
4990 | |
4991 | /* There used to be a long list of cases here, each one setting | |
4992 | e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE | |
4993 | in the corresponding bfd definition. To avoid duplication, | |
4994 | the switch was removed. Machines that need special handling | |
4995 | can generally do it in elf_backend_final_write_processing(), | |
4996 | unless they need the information earlier than the final write. | |
4997 | Such need can generally be supplied by replacing the tests for | |
4998 | e_machine with the conditions used to determine it. */ | |
252b5132 | 4999 | default: |
9c5bfbb7 AM |
5000 | i_ehdrp->e_machine = bed->elf_machine_code; |
5001 | } | |
aa4f99bb | 5002 | |
252b5132 RH |
5003 | i_ehdrp->e_version = bed->s->ev_current; |
5004 | i_ehdrp->e_ehsize = bed->s->sizeof_ehdr; | |
5005 | ||
c044fabd | 5006 | /* No program header, for now. */ |
252b5132 RH |
5007 | i_ehdrp->e_phoff = 0; |
5008 | i_ehdrp->e_phentsize = 0; | |
5009 | i_ehdrp->e_phnum = 0; | |
5010 | ||
c044fabd | 5011 | /* Each bfd section is section header entry. */ |
252b5132 RH |
5012 | i_ehdrp->e_entry = bfd_get_start_address (abfd); |
5013 | i_ehdrp->e_shentsize = bed->s->sizeof_shdr; | |
5014 | ||
c044fabd | 5015 | /* If we're building an executable, we'll need a program header table. */ |
252b5132 | 5016 | if (abfd->flags & EXEC_P) |
0e71e495 BE |
5017 | /* It all happens later. */ |
5018 | ; | |
252b5132 RH |
5019 | else |
5020 | { | |
5021 | i_ehdrp->e_phentsize = 0; | |
5022 | i_phdrp = 0; | |
5023 | i_ehdrp->e_phoff = 0; | |
5024 | } | |
5025 | ||
5026 | elf_tdata (abfd)->symtab_hdr.sh_name = | |
b34976b6 | 5027 | (unsigned int) _bfd_elf_strtab_add (shstrtab, ".symtab", FALSE); |
252b5132 | 5028 | elf_tdata (abfd)->strtab_hdr.sh_name = |
b34976b6 | 5029 | (unsigned int) _bfd_elf_strtab_add (shstrtab, ".strtab", FALSE); |
252b5132 | 5030 | elf_tdata (abfd)->shstrtab_hdr.sh_name = |
b34976b6 | 5031 | (unsigned int) _bfd_elf_strtab_add (shstrtab, ".shstrtab", FALSE); |
252b5132 RH |
5032 | if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1 |
5033 | || elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1 | |
5034 | || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1) | |
b34976b6 | 5035 | return FALSE; |
252b5132 | 5036 | |
b34976b6 | 5037 | return TRUE; |
252b5132 RH |
5038 | } |
5039 | ||
5040 | /* Assign file positions for all the reloc sections which are not part | |
5041 | of the loadable file image. */ | |
5042 | ||
5043 | void | |
217aa764 | 5044 | _bfd_elf_assign_file_positions_for_relocs (bfd *abfd) |
252b5132 RH |
5045 | { |
5046 | file_ptr off; | |
9ad5cbcf | 5047 | unsigned int i, num_sec; |
252b5132 RH |
5048 | Elf_Internal_Shdr **shdrpp; |
5049 | ||
5050 | off = elf_tdata (abfd)->next_file_pos; | |
5051 | ||
9ad5cbcf AM |
5052 | num_sec = elf_numsections (abfd); |
5053 | for (i = 1, shdrpp = elf_elfsections (abfd) + 1; i < num_sec; i++, shdrpp++) | |
252b5132 RH |
5054 | { |
5055 | Elf_Internal_Shdr *shdrp; | |
5056 | ||
5057 | shdrp = *shdrpp; | |
5058 | if ((shdrp->sh_type == SHT_REL || shdrp->sh_type == SHT_RELA) | |
5059 | && shdrp->sh_offset == -1) | |
b34976b6 | 5060 | off = _bfd_elf_assign_file_position_for_section (shdrp, off, TRUE); |
252b5132 RH |
5061 | } |
5062 | ||
5063 | elf_tdata (abfd)->next_file_pos = off; | |
5064 | } | |
5065 | ||
b34976b6 | 5066 | bfd_boolean |
217aa764 | 5067 | _bfd_elf_write_object_contents (bfd *abfd) |
252b5132 | 5068 | { |
9c5bfbb7 | 5069 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
252b5132 RH |
5070 | Elf_Internal_Ehdr *i_ehdrp; |
5071 | Elf_Internal_Shdr **i_shdrp; | |
b34976b6 | 5072 | bfd_boolean failed; |
9ad5cbcf | 5073 | unsigned int count, num_sec; |
252b5132 RH |
5074 | |
5075 | if (! abfd->output_has_begun | |
217aa764 | 5076 | && ! _bfd_elf_compute_section_file_positions (abfd, NULL)) |
b34976b6 | 5077 | return FALSE; |
252b5132 RH |
5078 | |
5079 | i_shdrp = elf_elfsections (abfd); | |
5080 | i_ehdrp = elf_elfheader (abfd); | |
5081 | ||
b34976b6 | 5082 | failed = FALSE; |
252b5132 RH |
5083 | bfd_map_over_sections (abfd, bed->s->write_relocs, &failed); |
5084 | if (failed) | |
b34976b6 | 5085 | return FALSE; |
252b5132 RH |
5086 | |
5087 | _bfd_elf_assign_file_positions_for_relocs (abfd); | |
5088 | ||
c044fabd | 5089 | /* After writing the headers, we need to write the sections too... */ |
9ad5cbcf AM |
5090 | num_sec = elf_numsections (abfd); |
5091 | for (count = 1; count < num_sec; count++) | |
252b5132 RH |
5092 | { |
5093 | if (bed->elf_backend_section_processing) | |
5094 | (*bed->elf_backend_section_processing) (abfd, i_shdrp[count]); | |
5095 | if (i_shdrp[count]->contents) | |
5096 | { | |
dc810e39 AM |
5097 | bfd_size_type amt = i_shdrp[count]->sh_size; |
5098 | ||
252b5132 | 5099 | if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0 |
dc810e39 | 5100 | || bfd_bwrite (i_shdrp[count]->contents, amt, abfd) != amt) |
b34976b6 | 5101 | return FALSE; |
252b5132 | 5102 | } |
9ad5cbcf AM |
5103 | if (count == SHN_LORESERVE - 1) |
5104 | count += SHN_HIRESERVE + 1 - SHN_LORESERVE; | |
252b5132 RH |
5105 | } |
5106 | ||
5107 | /* Write out the section header names. */ | |
26ae6d5e DJ |
5108 | if (elf_shstrtab (abfd) != NULL |
5109 | && (bfd_seek (abfd, elf_tdata (abfd)->shstrtab_hdr.sh_offset, SEEK_SET) != 0 | |
5110 | || ! _bfd_elf_strtab_emit (abfd, elf_shstrtab (abfd)))) | |
b34976b6 | 5111 | return FALSE; |
252b5132 RH |
5112 | |
5113 | if (bed->elf_backend_final_write_processing) | |
5114 | (*bed->elf_backend_final_write_processing) (abfd, | |
5115 | elf_tdata (abfd)->linker); | |
5116 | ||
5117 | return bed->s->write_shdrs_and_ehdr (abfd); | |
5118 | } | |
5119 | ||
b34976b6 | 5120 | bfd_boolean |
217aa764 | 5121 | _bfd_elf_write_corefile_contents (bfd *abfd) |
252b5132 | 5122 | { |
c044fabd | 5123 | /* Hopefully this can be done just like an object file. */ |
252b5132 RH |
5124 | return _bfd_elf_write_object_contents (abfd); |
5125 | } | |
c044fabd KH |
5126 | |
5127 | /* Given a section, search the header to find them. */ | |
5128 | ||
252b5132 | 5129 | int |
198beae2 | 5130 | _bfd_elf_section_from_bfd_section (bfd *abfd, struct bfd_section *asect) |
252b5132 | 5131 | { |
9c5bfbb7 | 5132 | const struct elf_backend_data *bed; |
252b5132 | 5133 | int index; |
252b5132 | 5134 | |
9ad5cbcf AM |
5135 | if (elf_section_data (asect) != NULL |
5136 | && elf_section_data (asect)->this_idx != 0) | |
5137 | return elf_section_data (asect)->this_idx; | |
5138 | ||
5139 | if (bfd_is_abs_section (asect)) | |
af746e92 AM |
5140 | index = SHN_ABS; |
5141 | else if (bfd_is_com_section (asect)) | |
5142 | index = SHN_COMMON; | |
5143 | else if (bfd_is_und_section (asect)) | |
5144 | index = SHN_UNDEF; | |
5145 | else | |
6dc132d9 | 5146 | index = -1; |
252b5132 | 5147 | |
af746e92 | 5148 | bed = get_elf_backend_data (abfd); |
252b5132 RH |
5149 | if (bed->elf_backend_section_from_bfd_section) |
5150 | { | |
af746e92 | 5151 | int retval = index; |
9ad5cbcf | 5152 | |
af746e92 AM |
5153 | if ((*bed->elf_backend_section_from_bfd_section) (abfd, asect, &retval)) |
5154 | return retval; | |
252b5132 RH |
5155 | } |
5156 | ||
af746e92 AM |
5157 | if (index == -1) |
5158 | bfd_set_error (bfd_error_nonrepresentable_section); | |
252b5132 | 5159 | |
af746e92 | 5160 | return index; |
252b5132 RH |
5161 | } |
5162 | ||
5163 | /* Given a BFD symbol, return the index in the ELF symbol table, or -1 | |
5164 | on error. */ | |
5165 | ||
5166 | int | |
217aa764 | 5167 | _bfd_elf_symbol_from_bfd_symbol (bfd *abfd, asymbol **asym_ptr_ptr) |
252b5132 RH |
5168 | { |
5169 | asymbol *asym_ptr = *asym_ptr_ptr; | |
5170 | int idx; | |
5171 | flagword flags = asym_ptr->flags; | |
5172 | ||
5173 | /* When gas creates relocations against local labels, it creates its | |
5174 | own symbol for the section, but does put the symbol into the | |
5175 | symbol chain, so udata is 0. When the linker is generating | |
5176 | relocatable output, this section symbol may be for one of the | |
5177 | input sections rather than the output section. */ | |
5178 | if (asym_ptr->udata.i == 0 | |
5179 | && (flags & BSF_SECTION_SYM) | |
5180 | && asym_ptr->section) | |
5181 | { | |
5372391b | 5182 | asection *sec; |
252b5132 RH |
5183 | int indx; |
5184 | ||
5372391b AM |
5185 | sec = asym_ptr->section; |
5186 | if (sec->owner != abfd && sec->output_section != NULL) | |
5187 | sec = sec->output_section; | |
5188 | if (sec->owner == abfd | |
5189 | && (indx = sec->index) < elf_num_section_syms (abfd) | |
4e89ac30 | 5190 | && elf_section_syms (abfd)[indx] != NULL) |
252b5132 RH |
5191 | asym_ptr->udata.i = elf_section_syms (abfd)[indx]->udata.i; |
5192 | } | |
5193 | ||
5194 | idx = asym_ptr->udata.i; | |
5195 | ||
5196 | if (idx == 0) | |
5197 | { | |
5198 | /* This case can occur when using --strip-symbol on a symbol | |
5199 | which is used in a relocation entry. */ | |
5200 | (*_bfd_error_handler) | |
d003868e AM |
5201 | (_("%B: symbol `%s' required but not present"), |
5202 | abfd, bfd_asymbol_name (asym_ptr)); | |
252b5132 RH |
5203 | bfd_set_error (bfd_error_no_symbols); |
5204 | return -1; | |
5205 | } | |
5206 | ||
5207 | #if DEBUG & 4 | |
5208 | { | |
5209 | fprintf (stderr, | |
661a3fd4 | 5210 | "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8lx%s\n", |
252b5132 RH |
5211 | (long) asym_ptr, asym_ptr->name, idx, flags, |
5212 | elf_symbol_flags (flags)); | |
5213 | fflush (stderr); | |
5214 | } | |
5215 | #endif | |
5216 | ||
5217 | return idx; | |
5218 | } | |
5219 | ||
84d1d650 | 5220 | /* Rewrite program header information. */ |
252b5132 | 5221 | |
b34976b6 | 5222 | static bfd_boolean |
84d1d650 | 5223 | rewrite_elf_program_header (bfd *ibfd, bfd *obfd) |
252b5132 | 5224 | { |
b34976b6 AM |
5225 | Elf_Internal_Ehdr *iehdr; |
5226 | struct elf_segment_map *map; | |
5227 | struct elf_segment_map *map_first; | |
5228 | struct elf_segment_map **pointer_to_map; | |
5229 | Elf_Internal_Phdr *segment; | |
5230 | asection *section; | |
5231 | unsigned int i; | |
5232 | unsigned int num_segments; | |
5233 | bfd_boolean phdr_included = FALSE; | |
5234 | bfd_vma maxpagesize; | |
5235 | struct elf_segment_map *phdr_adjust_seg = NULL; | |
5236 | unsigned int phdr_adjust_num = 0; | |
9c5bfbb7 | 5237 | const struct elf_backend_data *bed; |
bc67d8a6 | 5238 | |
caf47ea6 | 5239 | bed = get_elf_backend_data (ibfd); |
252b5132 RH |
5240 | iehdr = elf_elfheader (ibfd); |
5241 | ||
bc67d8a6 | 5242 | map_first = NULL; |
c044fabd | 5243 | pointer_to_map = &map_first; |
252b5132 RH |
5244 | |
5245 | num_segments = elf_elfheader (ibfd)->e_phnum; | |
bc67d8a6 NC |
5246 | maxpagesize = get_elf_backend_data (obfd)->maxpagesize; |
5247 | ||
5248 | /* Returns the end address of the segment + 1. */ | |
aecc8f8a AM |
5249 | #define SEGMENT_END(segment, start) \ |
5250 | (start + (segment->p_memsz > segment->p_filesz \ | |
5251 | ? segment->p_memsz : segment->p_filesz)) | |
bc67d8a6 | 5252 | |
eecdbe52 JJ |
5253 | #define SECTION_SIZE(section, segment) \ |
5254 | (((section->flags & (SEC_HAS_CONTENTS | SEC_THREAD_LOCAL)) \ | |
5255 | != SEC_THREAD_LOCAL || segment->p_type == PT_TLS) \ | |
eea6121a | 5256 | ? section->size : 0) |
eecdbe52 | 5257 | |
b34976b6 | 5258 | /* Returns TRUE if the given section is contained within |
bc67d8a6 | 5259 | the given segment. VMA addresses are compared. */ |
aecc8f8a AM |
5260 | #define IS_CONTAINED_BY_VMA(section, segment) \ |
5261 | (section->vma >= segment->p_vaddr \ | |
eecdbe52 | 5262 | && (section->vma + SECTION_SIZE (section, segment) \ |
aecc8f8a | 5263 | <= (SEGMENT_END (segment, segment->p_vaddr)))) |
c044fabd | 5264 | |
b34976b6 | 5265 | /* Returns TRUE if the given section is contained within |
bc67d8a6 | 5266 | the given segment. LMA addresses are compared. */ |
aecc8f8a AM |
5267 | #define IS_CONTAINED_BY_LMA(section, segment, base) \ |
5268 | (section->lma >= base \ | |
eecdbe52 | 5269 | && (section->lma + SECTION_SIZE (section, segment) \ |
aecc8f8a | 5270 | <= SEGMENT_END (segment, base))) |
252b5132 | 5271 | |
c044fabd | 5272 | /* Special case: corefile "NOTE" section containing regs, prpsinfo etc. */ |
aecc8f8a AM |
5273 | #define IS_COREFILE_NOTE(p, s) \ |
5274 | (p->p_type == PT_NOTE \ | |
5275 | && bfd_get_format (ibfd) == bfd_core \ | |
5276 | && s->vma == 0 && s->lma == 0 \ | |
5277 | && (bfd_vma) s->filepos >= p->p_offset \ | |
cb3ff1e5 | 5278 | && ((bfd_vma) s->filepos + s->size \ |
aecc8f8a | 5279 | <= p->p_offset + p->p_filesz)) |
252b5132 RH |
5280 | |
5281 | /* The complicated case when p_vaddr is 0 is to handle the Solaris | |
5282 | linker, which generates a PT_INTERP section with p_vaddr and | |
5283 | p_memsz set to 0. */ | |
aecc8f8a AM |
5284 | #define IS_SOLARIS_PT_INTERP(p, s) \ |
5285 | (p->p_vaddr == 0 \ | |
5286 | && p->p_paddr == 0 \ | |
5287 | && p->p_memsz == 0 \ | |
5288 | && p->p_filesz > 0 \ | |
5289 | && (s->flags & SEC_HAS_CONTENTS) != 0 \ | |
eea6121a | 5290 | && s->size > 0 \ |
aecc8f8a | 5291 | && (bfd_vma) s->filepos >= p->p_offset \ |
cb3ff1e5 | 5292 | && ((bfd_vma) s->filepos + s->size \ |
aecc8f8a | 5293 | <= p->p_offset + p->p_filesz)) |
5c440b1e | 5294 | |
bc67d8a6 NC |
5295 | /* Decide if the given section should be included in the given segment. |
5296 | A section will be included if: | |
f5ffc919 NC |
5297 | 1. It is within the address space of the segment -- we use the LMA |
5298 | if that is set for the segment and the VMA otherwise, | |
bc67d8a6 NC |
5299 | 2. It is an allocated segment, |
5300 | 3. There is an output section associated with it, | |
eecdbe52 | 5301 | 4. The section has not already been allocated to a previous segment. |
03394ac9 NC |
5302 | 5. PT_GNU_STACK segments do not include any sections. |
5303 | 6. PT_TLS segment includes only SHF_TLS sections. | |
6f79b219 JJ |
5304 | 7. SHF_TLS sections are only in PT_TLS or PT_LOAD segments. |
5305 | 8. PT_DYNAMIC should not contain empty sections at the beginning | |
5306 | (with the possible exception of .dynamic). */ | |
9f17e2a6 | 5307 | #define IS_SECTION_IN_INPUT_SEGMENT(section, segment, bed) \ |
aecc8f8a AM |
5308 | ((((segment->p_paddr \ |
5309 | ? IS_CONTAINED_BY_LMA (section, segment, segment->p_paddr) \ | |
5310 | : IS_CONTAINED_BY_VMA (section, segment)) \ | |
f5ffc919 | 5311 | && (section->flags & SEC_ALLOC) != 0) \ |
b6821651 | 5312 | || IS_COREFILE_NOTE (segment, section)) \ |
03394ac9 | 5313 | && segment->p_type != PT_GNU_STACK \ |
eecdbe52 JJ |
5314 | && (segment->p_type != PT_TLS \ |
5315 | || (section->flags & SEC_THREAD_LOCAL)) \ | |
5316 | && (segment->p_type == PT_LOAD \ | |
5317 | || segment->p_type == PT_TLS \ | |
5318 | || (section->flags & SEC_THREAD_LOCAL) == 0) \ | |
6f79b219 JJ |
5319 | && (segment->p_type != PT_DYNAMIC \ |
5320 | || SECTION_SIZE (section, segment) > 0 \ | |
5321 | || (segment->p_paddr \ | |
5322 | ? segment->p_paddr != section->lma \ | |
5323 | : segment->p_vaddr != section->vma) \ | |
5324 | || (strcmp (bfd_get_section_name (ibfd, section), ".dynamic") \ | |
5325 | == 0)) \ | |
82e51918 | 5326 | && ! section->segment_mark) |
bc67d8a6 | 5327 | |
9f17e2a6 L |
5328 | /* If the output section of a section in the input segment is NULL, |
5329 | it is removed from the corresponding output segment. */ | |
5330 | #define INCLUDE_SECTION_IN_SEGMENT(section, segment, bed) \ | |
5331 | (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed) \ | |
5332 | && section->output_section != NULL) | |
5333 | ||
b34976b6 | 5334 | /* Returns TRUE iff seg1 starts after the end of seg2. */ |
b5f852ea NC |
5335 | #define SEGMENT_AFTER_SEGMENT(seg1, seg2, field) \ |
5336 | (seg1->field >= SEGMENT_END (seg2, seg2->field)) | |
5337 | ||
5338 | /* Returns TRUE iff seg1 and seg2 overlap. Segments overlap iff both | |
5339 | their VMA address ranges and their LMA address ranges overlap. | |
5340 | It is possible to have overlapping VMA ranges without overlapping LMA | |
5341 | ranges. RedBoot images for example can have both .data and .bss mapped | |
5342 | to the same VMA range, but with the .data section mapped to a different | |
5343 | LMA. */ | |
aecc8f8a | 5344 | #define SEGMENT_OVERLAPS(seg1, seg2) \ |
b5f852ea NC |
5345 | ( !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_vaddr) \ |
5346 | || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_vaddr)) \ | |
5347 | && !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_paddr) \ | |
5348 | || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_paddr))) | |
bc67d8a6 NC |
5349 | |
5350 | /* Initialise the segment mark field. */ | |
5351 | for (section = ibfd->sections; section != NULL; section = section->next) | |
b34976b6 | 5352 | section->segment_mark = FALSE; |
bc67d8a6 | 5353 | |
252b5132 | 5354 | /* Scan through the segments specified in the program header |
bc67d8a6 | 5355 | of the input BFD. For this first scan we look for overlaps |
9ad5cbcf | 5356 | in the loadable segments. These can be created by weird |
aecc8f8a | 5357 | parameters to objcopy. Also, fix some solaris weirdness. */ |
bc67d8a6 NC |
5358 | for (i = 0, segment = elf_tdata (ibfd)->phdr; |
5359 | i < num_segments; | |
c044fabd | 5360 | i++, segment++) |
252b5132 | 5361 | { |
252b5132 | 5362 | unsigned int j; |
c044fabd | 5363 | Elf_Internal_Phdr *segment2; |
252b5132 | 5364 | |
aecc8f8a AM |
5365 | if (segment->p_type == PT_INTERP) |
5366 | for (section = ibfd->sections; section; section = section->next) | |
5367 | if (IS_SOLARIS_PT_INTERP (segment, section)) | |
5368 | { | |
5369 | /* Mininal change so that the normal section to segment | |
4cc11e76 | 5370 | assignment code will work. */ |
aecc8f8a AM |
5371 | segment->p_vaddr = section->vma; |
5372 | break; | |
5373 | } | |
5374 | ||
bc67d8a6 NC |
5375 | if (segment->p_type != PT_LOAD) |
5376 | continue; | |
c044fabd | 5377 | |
bc67d8a6 | 5378 | /* Determine if this segment overlaps any previous segments. */ |
c044fabd | 5379 | for (j = 0, segment2 = elf_tdata (ibfd)->phdr; j < i; j++, segment2 ++) |
bc67d8a6 NC |
5380 | { |
5381 | bfd_signed_vma extra_length; | |
c044fabd | 5382 | |
bc67d8a6 NC |
5383 | if (segment2->p_type != PT_LOAD |
5384 | || ! SEGMENT_OVERLAPS (segment, segment2)) | |
5385 | continue; | |
c044fabd | 5386 | |
bc67d8a6 NC |
5387 | /* Merge the two segments together. */ |
5388 | if (segment2->p_vaddr < segment->p_vaddr) | |
5389 | { | |
c044fabd KH |
5390 | /* Extend SEGMENT2 to include SEGMENT and then delete |
5391 | SEGMENT. */ | |
bc67d8a6 NC |
5392 | extra_length = |
5393 | SEGMENT_END (segment, segment->p_vaddr) | |
5394 | - SEGMENT_END (segment2, segment2->p_vaddr); | |
c044fabd | 5395 | |
bc67d8a6 NC |
5396 | if (extra_length > 0) |
5397 | { | |
5398 | segment2->p_memsz += extra_length; | |
5399 | segment2->p_filesz += extra_length; | |
5400 | } | |
c044fabd | 5401 | |
bc67d8a6 | 5402 | segment->p_type = PT_NULL; |
c044fabd | 5403 | |
bc67d8a6 NC |
5404 | /* Since we have deleted P we must restart the outer loop. */ |
5405 | i = 0; | |
5406 | segment = elf_tdata (ibfd)->phdr; | |
5407 | break; | |
5408 | } | |
5409 | else | |
5410 | { | |
c044fabd KH |
5411 | /* Extend SEGMENT to include SEGMENT2 and then delete |
5412 | SEGMENT2. */ | |
bc67d8a6 NC |
5413 | extra_length = |
5414 | SEGMENT_END (segment2, segment2->p_vaddr) | |
5415 | - SEGMENT_END (segment, segment->p_vaddr); | |
c044fabd | 5416 | |
bc67d8a6 NC |
5417 | if (extra_length > 0) |
5418 | { | |
5419 | segment->p_memsz += extra_length; | |
5420 | segment->p_filesz += extra_length; | |
5421 | } | |
c044fabd | 5422 | |
bc67d8a6 NC |
5423 | segment2->p_type = PT_NULL; |
5424 | } | |
5425 | } | |
5426 | } | |
c044fabd | 5427 | |
bc67d8a6 NC |
5428 | /* The second scan attempts to assign sections to segments. */ |
5429 | for (i = 0, segment = elf_tdata (ibfd)->phdr; | |
5430 | i < num_segments; | |
5431 | i ++, segment ++) | |
5432 | { | |
5433 | unsigned int section_count; | |
5434 | asection ** sections; | |
5435 | asection * output_section; | |
5436 | unsigned int isec; | |
5437 | bfd_vma matching_lma; | |
5438 | bfd_vma suggested_lma; | |
5439 | unsigned int j; | |
dc810e39 | 5440 | bfd_size_type amt; |
9f17e2a6 | 5441 | asection * first_section; |
bc67d8a6 NC |
5442 | |
5443 | if (segment->p_type == PT_NULL) | |
5444 | continue; | |
c044fabd | 5445 | |
9f17e2a6 | 5446 | first_section = NULL; |
bc67d8a6 | 5447 | /* Compute how many sections might be placed into this segment. */ |
b5f852ea NC |
5448 | for (section = ibfd->sections, section_count = 0; |
5449 | section != NULL; | |
5450 | section = section->next) | |
9f17e2a6 L |
5451 | { |
5452 | /* Find the first section in the input segment, which may be | |
5453 | removed from the corresponding output segment. */ | |
5454 | if (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed)) | |
5455 | { | |
5456 | if (first_section == NULL) | |
5457 | first_section = section; | |
5458 | if (section->output_section != NULL) | |
5459 | ++section_count; | |
5460 | } | |
5461 | } | |
811072d8 | 5462 | |
b5f852ea NC |
5463 | /* Allocate a segment map big enough to contain |
5464 | all of the sections we have selected. */ | |
dc810e39 AM |
5465 | amt = sizeof (struct elf_segment_map); |
5466 | amt += ((bfd_size_type) section_count - 1) * sizeof (asection *); | |
41f8ce69 | 5467 | map = bfd_zalloc (obfd, amt); |
bc67d8a6 | 5468 | if (map == NULL) |
b34976b6 | 5469 | return FALSE; |
252b5132 RH |
5470 | |
5471 | /* Initialise the fields of the segment map. Default to | |
5472 | using the physical address of the segment in the input BFD. */ | |
bc67d8a6 NC |
5473 | map->next = NULL; |
5474 | map->p_type = segment->p_type; | |
5475 | map->p_flags = segment->p_flags; | |
5476 | map->p_flags_valid = 1; | |
55d55ac7 | 5477 | |
9f17e2a6 L |
5478 | /* If the first section in the input segment is removed, there is |
5479 | no need to preserve segment physical address in the corresponding | |
5480 | output segment. */ | |
945c025a | 5481 | if (!first_section || first_section->output_section != NULL) |
9f17e2a6 L |
5482 | { |
5483 | map->p_paddr = segment->p_paddr; | |
5484 | map->p_paddr_valid = 1; | |
5485 | } | |
252b5132 RH |
5486 | |
5487 | /* Determine if this segment contains the ELF file header | |
5488 | and if it contains the program headers themselves. */ | |
bc67d8a6 NC |
5489 | map->includes_filehdr = (segment->p_offset == 0 |
5490 | && segment->p_filesz >= iehdr->e_ehsize); | |
252b5132 | 5491 | |
bc67d8a6 | 5492 | map->includes_phdrs = 0; |
252b5132 | 5493 | |
bc67d8a6 | 5494 | if (! phdr_included || segment->p_type != PT_LOAD) |
252b5132 | 5495 | { |
bc67d8a6 NC |
5496 | map->includes_phdrs = |
5497 | (segment->p_offset <= (bfd_vma) iehdr->e_phoff | |
5498 | && (segment->p_offset + segment->p_filesz | |
252b5132 RH |
5499 | >= ((bfd_vma) iehdr->e_phoff |
5500 | + iehdr->e_phnum * iehdr->e_phentsize))); | |
c044fabd | 5501 | |
bc67d8a6 | 5502 | if (segment->p_type == PT_LOAD && map->includes_phdrs) |
b34976b6 | 5503 | phdr_included = TRUE; |
252b5132 RH |
5504 | } |
5505 | ||
bc67d8a6 | 5506 | if (section_count == 0) |
252b5132 RH |
5507 | { |
5508 | /* Special segments, such as the PT_PHDR segment, may contain | |
5509 | no sections, but ordinary, loadable segments should contain | |
1ed89aa9 NC |
5510 | something. They are allowed by the ELF spec however, so only |
5511 | a warning is produced. */ | |
bc67d8a6 | 5512 | if (segment->p_type == PT_LOAD) |
caf47ea6 | 5513 | (*_bfd_error_handler) |
d003868e AM |
5514 | (_("%B: warning: Empty loadable segment detected, is this intentional ?\n"), |
5515 | ibfd); | |
252b5132 | 5516 | |
bc67d8a6 | 5517 | map->count = 0; |
c044fabd KH |
5518 | *pointer_to_map = map; |
5519 | pointer_to_map = &map->next; | |
252b5132 RH |
5520 | |
5521 | continue; | |
5522 | } | |
5523 | ||
5524 | /* Now scan the sections in the input BFD again and attempt | |
5525 | to add their corresponding output sections to the segment map. | |
5526 | The problem here is how to handle an output section which has | |
5527 | been moved (ie had its LMA changed). There are four possibilities: | |
5528 | ||
5529 | 1. None of the sections have been moved. | |
5530 | In this case we can continue to use the segment LMA from the | |
5531 | input BFD. | |
5532 | ||
5533 | 2. All of the sections have been moved by the same amount. | |
5534 | In this case we can change the segment's LMA to match the LMA | |
5535 | of the first section. | |
5536 | ||
5537 | 3. Some of the sections have been moved, others have not. | |
5538 | In this case those sections which have not been moved can be | |
5539 | placed in the current segment which will have to have its size, | |
5540 | and possibly its LMA changed, and a new segment or segments will | |
5541 | have to be created to contain the other sections. | |
5542 | ||
b5f852ea | 5543 | 4. The sections have been moved, but not by the same amount. |
252b5132 RH |
5544 | In this case we can change the segment's LMA to match the LMA |
5545 | of the first section and we will have to create a new segment | |
5546 | or segments to contain the other sections. | |
5547 | ||
5548 | In order to save time, we allocate an array to hold the section | |
5549 | pointers that we are interested in. As these sections get assigned | |
5550 | to a segment, they are removed from this array. */ | |
5551 | ||
0b14c2aa L |
5552 | /* Gcc 2.96 miscompiles this code on mips. Don't do casting here |
5553 | to work around this long long bug. */ | |
d0fb9a8d | 5554 | sections = bfd_malloc2 (section_count, sizeof (asection *)); |
252b5132 | 5555 | if (sections == NULL) |
b34976b6 | 5556 | return FALSE; |
252b5132 RH |
5557 | |
5558 | /* Step One: Scan for segment vs section LMA conflicts. | |
5559 | Also add the sections to the section array allocated above. | |
5560 | Also add the sections to the current segment. In the common | |
5561 | case, where the sections have not been moved, this means that | |
5562 | we have completely filled the segment, and there is nothing | |
5563 | more to do. */ | |
252b5132 | 5564 | isec = 0; |
72730e0c | 5565 | matching_lma = 0; |
252b5132 RH |
5566 | suggested_lma = 0; |
5567 | ||
bc67d8a6 NC |
5568 | for (j = 0, section = ibfd->sections; |
5569 | section != NULL; | |
5570 | section = section->next) | |
252b5132 | 5571 | { |
caf47ea6 | 5572 | if (INCLUDE_SECTION_IN_SEGMENT (section, segment, bed)) |
c0f7859b | 5573 | { |
bc67d8a6 NC |
5574 | output_section = section->output_section; |
5575 | ||
5576 | sections[j ++] = section; | |
252b5132 RH |
5577 | |
5578 | /* The Solaris native linker always sets p_paddr to 0. | |
5579 | We try to catch that case here, and set it to the | |
5e8d7549 NC |
5580 | correct value. Note - some backends require that |
5581 | p_paddr be left as zero. */ | |
bc67d8a6 | 5582 | if (segment->p_paddr == 0 |
4455705d | 5583 | && segment->p_vaddr != 0 |
5e8d7549 | 5584 | && (! bed->want_p_paddr_set_to_zero) |
252b5132 | 5585 | && isec == 0 |
bc67d8a6 NC |
5586 | && output_section->lma != 0 |
5587 | && (output_section->vma == (segment->p_vaddr | |
5588 | + (map->includes_filehdr | |
5589 | ? iehdr->e_ehsize | |
5590 | : 0) | |
5591 | + (map->includes_phdrs | |
079e9a2f AM |
5592 | ? (iehdr->e_phnum |
5593 | * iehdr->e_phentsize) | |
bc67d8a6 NC |
5594 | : 0)))) |
5595 | map->p_paddr = segment->p_vaddr; | |
252b5132 RH |
5596 | |
5597 | /* Match up the physical address of the segment with the | |
5598 | LMA address of the output section. */ | |
bc67d8a6 | 5599 | if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr) |
5e8d7549 NC |
5600 | || IS_COREFILE_NOTE (segment, section) |
5601 | || (bed->want_p_paddr_set_to_zero && | |
5602 | IS_CONTAINED_BY_VMA (output_section, segment)) | |
5603 | ) | |
252b5132 RH |
5604 | { |
5605 | if (matching_lma == 0) | |
bc67d8a6 | 5606 | matching_lma = output_section->lma; |
252b5132 RH |
5607 | |
5608 | /* We assume that if the section fits within the segment | |
bc67d8a6 | 5609 | then it does not overlap any other section within that |
252b5132 | 5610 | segment. */ |
bc67d8a6 | 5611 | map->sections[isec ++] = output_section; |
252b5132 RH |
5612 | } |
5613 | else if (suggested_lma == 0) | |
bc67d8a6 | 5614 | suggested_lma = output_section->lma; |
252b5132 RH |
5615 | } |
5616 | } | |
5617 | ||
bc67d8a6 | 5618 | BFD_ASSERT (j == section_count); |
252b5132 RH |
5619 | |
5620 | /* Step Two: Adjust the physical address of the current segment, | |
5621 | if necessary. */ | |
bc67d8a6 | 5622 | if (isec == section_count) |
252b5132 RH |
5623 | { |
5624 | /* All of the sections fitted within the segment as currently | |
5625 | specified. This is the default case. Add the segment to | |
5626 | the list of built segments and carry on to process the next | |
5627 | program header in the input BFD. */ | |
bc67d8a6 | 5628 | map->count = section_count; |
c044fabd KH |
5629 | *pointer_to_map = map; |
5630 | pointer_to_map = &map->next; | |
3271a814 NS |
5631 | |
5632 | if (matching_lma != map->p_paddr | |
5633 | && !map->includes_filehdr && !map->includes_phdrs) | |
5634 | /* There is some padding before the first section in the | |
5635 | segment. So, we must account for that in the output | |
5636 | segment's vma. */ | |
5637 | map->p_vaddr_offset = matching_lma - map->p_paddr; | |
5638 | ||
252b5132 RH |
5639 | free (sections); |
5640 | continue; | |
5641 | } | |
252b5132 RH |
5642 | else |
5643 | { | |
72730e0c AM |
5644 | if (matching_lma != 0) |
5645 | { | |
5646 | /* At least one section fits inside the current segment. | |
5647 | Keep it, but modify its physical address to match the | |
5648 | LMA of the first section that fitted. */ | |
bc67d8a6 | 5649 | map->p_paddr = matching_lma; |
72730e0c AM |
5650 | } |
5651 | else | |
5652 | { | |
5653 | /* None of the sections fitted inside the current segment. | |
5654 | Change the current segment's physical address to match | |
5655 | the LMA of the first section. */ | |
bc67d8a6 | 5656 | map->p_paddr = suggested_lma; |
72730e0c AM |
5657 | } |
5658 | ||
bc67d8a6 NC |
5659 | /* Offset the segment physical address from the lma |
5660 | to allow for space taken up by elf headers. */ | |
5661 | if (map->includes_filehdr) | |
5662 | map->p_paddr -= iehdr->e_ehsize; | |
252b5132 | 5663 | |
bc67d8a6 NC |
5664 | if (map->includes_phdrs) |
5665 | { | |
5666 | map->p_paddr -= iehdr->e_phnum * iehdr->e_phentsize; | |
5667 | ||
5668 | /* iehdr->e_phnum is just an estimate of the number | |
5669 | of program headers that we will need. Make a note | |
5670 | here of the number we used and the segment we chose | |
5671 | to hold these headers, so that we can adjust the | |
5672 | offset when we know the correct value. */ | |
5673 | phdr_adjust_num = iehdr->e_phnum; | |
5674 | phdr_adjust_seg = map; | |
5675 | } | |
252b5132 RH |
5676 | } |
5677 | ||
5678 | /* Step Three: Loop over the sections again, this time assigning | |
caf47ea6 | 5679 | those that fit to the current segment and removing them from the |
252b5132 RH |
5680 | sections array; but making sure not to leave large gaps. Once all |
5681 | possible sections have been assigned to the current segment it is | |
5682 | added to the list of built segments and if sections still remain | |
5683 | to be assigned, a new segment is constructed before repeating | |
5684 | the loop. */ | |
5685 | isec = 0; | |
5686 | do | |
5687 | { | |
bc67d8a6 | 5688 | map->count = 0; |
252b5132 RH |
5689 | suggested_lma = 0; |
5690 | ||
5691 | /* Fill the current segment with sections that fit. */ | |
bc67d8a6 | 5692 | for (j = 0; j < section_count; j++) |
252b5132 | 5693 | { |
bc67d8a6 | 5694 | section = sections[j]; |
252b5132 | 5695 | |
bc67d8a6 | 5696 | if (section == NULL) |
252b5132 RH |
5697 | continue; |
5698 | ||
bc67d8a6 | 5699 | output_section = section->output_section; |
252b5132 | 5700 | |
bc67d8a6 | 5701 | BFD_ASSERT (output_section != NULL); |
c044fabd | 5702 | |
bc67d8a6 NC |
5703 | if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr) |
5704 | || IS_COREFILE_NOTE (segment, section)) | |
252b5132 | 5705 | { |
bc67d8a6 | 5706 | if (map->count == 0) |
252b5132 RH |
5707 | { |
5708 | /* If the first section in a segment does not start at | |
bc67d8a6 NC |
5709 | the beginning of the segment, then something is |
5710 | wrong. */ | |
5711 | if (output_section->lma != | |
5712 | (map->p_paddr | |
5713 | + (map->includes_filehdr ? iehdr->e_ehsize : 0) | |
5714 | + (map->includes_phdrs | |
5715 | ? iehdr->e_phnum * iehdr->e_phentsize | |
5716 | : 0))) | |
252b5132 RH |
5717 | abort (); |
5718 | } | |
5719 | else | |
5720 | { | |
5721 | asection * prev_sec; | |
252b5132 | 5722 | |
bc67d8a6 | 5723 | prev_sec = map->sections[map->count - 1]; |
252b5132 RH |
5724 | |
5725 | /* If the gap between the end of the previous section | |
bc67d8a6 NC |
5726 | and the start of this section is more than |
5727 | maxpagesize then we need to start a new segment. */ | |
eea6121a | 5728 | if ((BFD_ALIGN (prev_sec->lma + prev_sec->size, |
079e9a2f | 5729 | maxpagesize) |
caf47ea6 | 5730 | < BFD_ALIGN (output_section->lma, maxpagesize)) |
eea6121a | 5731 | || ((prev_sec->lma + prev_sec->size) |
079e9a2f | 5732 | > output_section->lma)) |
252b5132 RH |
5733 | { |
5734 | if (suggested_lma == 0) | |
bc67d8a6 | 5735 | suggested_lma = output_section->lma; |
252b5132 RH |
5736 | |
5737 | continue; | |
5738 | } | |
5739 | } | |
5740 | ||
bc67d8a6 | 5741 | map->sections[map->count++] = output_section; |
252b5132 RH |
5742 | ++isec; |
5743 | sections[j] = NULL; | |
b34976b6 | 5744 | section->segment_mark = TRUE; |
252b5132 RH |
5745 | } |
5746 | else if (suggested_lma == 0) | |
bc67d8a6 | 5747 | suggested_lma = output_section->lma; |
252b5132 RH |
5748 | } |
5749 | ||
bc67d8a6 | 5750 | BFD_ASSERT (map->count > 0); |
252b5132 RH |
5751 | |
5752 | /* Add the current segment to the list of built segments. */ | |
c044fabd KH |
5753 | *pointer_to_map = map; |
5754 | pointer_to_map = &map->next; | |
252b5132 | 5755 | |
bc67d8a6 | 5756 | if (isec < section_count) |
252b5132 RH |
5757 | { |
5758 | /* We still have not allocated all of the sections to | |
5759 | segments. Create a new segment here, initialise it | |
5760 | and carry on looping. */ | |
dc810e39 AM |
5761 | amt = sizeof (struct elf_segment_map); |
5762 | amt += ((bfd_size_type) section_count - 1) * sizeof (asection *); | |
217aa764 | 5763 | map = bfd_alloc (obfd, amt); |
bc67d8a6 | 5764 | if (map == NULL) |
5ed6aba4 NC |
5765 | { |
5766 | free (sections); | |
5767 | return FALSE; | |
5768 | } | |
252b5132 RH |
5769 | |
5770 | /* Initialise the fields of the segment map. Set the physical | |
5771 | physical address to the LMA of the first section that has | |
5772 | not yet been assigned. */ | |
bc67d8a6 NC |
5773 | map->next = NULL; |
5774 | map->p_type = segment->p_type; | |
5775 | map->p_flags = segment->p_flags; | |
5776 | map->p_flags_valid = 1; | |
5777 | map->p_paddr = suggested_lma; | |
5778 | map->p_paddr_valid = 1; | |
5779 | map->includes_filehdr = 0; | |
5780 | map->includes_phdrs = 0; | |
252b5132 RH |
5781 | } |
5782 | } | |
bc67d8a6 | 5783 | while (isec < section_count); |
252b5132 RH |
5784 | |
5785 | free (sections); | |
5786 | } | |
5787 | ||
5788 | /* The Solaris linker creates program headers in which all the | |
5789 | p_paddr fields are zero. When we try to objcopy or strip such a | |
5790 | file, we get confused. Check for this case, and if we find it | |
5791 | reset the p_paddr_valid fields. */ | |
bc67d8a6 NC |
5792 | for (map = map_first; map != NULL; map = map->next) |
5793 | if (map->p_paddr != 0) | |
252b5132 | 5794 | break; |
bc67d8a6 | 5795 | if (map == NULL) |
b5f852ea NC |
5796 | for (map = map_first; map != NULL; map = map->next) |
5797 | map->p_paddr_valid = 0; | |
252b5132 | 5798 | |
bc67d8a6 NC |
5799 | elf_tdata (obfd)->segment_map = map_first; |
5800 | ||
5801 | /* If we had to estimate the number of program headers that were | |
9ad5cbcf | 5802 | going to be needed, then check our estimate now and adjust |
bc67d8a6 NC |
5803 | the offset if necessary. */ |
5804 | if (phdr_adjust_seg != NULL) | |
5805 | { | |
5806 | unsigned int count; | |
c044fabd | 5807 | |
bc67d8a6 | 5808 | for (count = 0, map = map_first; map != NULL; map = map->next) |
c044fabd | 5809 | count++; |
252b5132 | 5810 | |
bc67d8a6 NC |
5811 | if (count > phdr_adjust_num) |
5812 | phdr_adjust_seg->p_paddr | |
5813 | -= (count - phdr_adjust_num) * iehdr->e_phentsize; | |
5814 | } | |
c044fabd | 5815 | |
bc67d8a6 | 5816 | #undef SEGMENT_END |
eecdbe52 | 5817 | #undef SECTION_SIZE |
bc67d8a6 NC |
5818 | #undef IS_CONTAINED_BY_VMA |
5819 | #undef IS_CONTAINED_BY_LMA | |
252b5132 | 5820 | #undef IS_COREFILE_NOTE |
bc67d8a6 | 5821 | #undef IS_SOLARIS_PT_INTERP |
9f17e2a6 | 5822 | #undef IS_SECTION_IN_INPUT_SEGMENT |
bc67d8a6 NC |
5823 | #undef INCLUDE_SECTION_IN_SEGMENT |
5824 | #undef SEGMENT_AFTER_SEGMENT | |
5825 | #undef SEGMENT_OVERLAPS | |
b34976b6 | 5826 | return TRUE; |
252b5132 RH |
5827 | } |
5828 | ||
84d1d650 L |
5829 | /* Copy ELF program header information. */ |
5830 | ||
5831 | static bfd_boolean | |
5832 | copy_elf_program_header (bfd *ibfd, bfd *obfd) | |
5833 | { | |
5834 | Elf_Internal_Ehdr *iehdr; | |
5835 | struct elf_segment_map *map; | |
5836 | struct elf_segment_map *map_first; | |
5837 | struct elf_segment_map **pointer_to_map; | |
5838 | Elf_Internal_Phdr *segment; | |
5839 | unsigned int i; | |
5840 | unsigned int num_segments; | |
5841 | bfd_boolean phdr_included = FALSE; | |
5842 | ||
5843 | iehdr = elf_elfheader (ibfd); | |
5844 | ||
5845 | map_first = NULL; | |
5846 | pointer_to_map = &map_first; | |
5847 | ||
5848 | num_segments = elf_elfheader (ibfd)->e_phnum; | |
5849 | for (i = 0, segment = elf_tdata (ibfd)->phdr; | |
5850 | i < num_segments; | |
5851 | i++, segment++) | |
5852 | { | |
5853 | asection *section; | |
5854 | unsigned int section_count; | |
5855 | bfd_size_type amt; | |
5856 | Elf_Internal_Shdr *this_hdr; | |
53020534 | 5857 | asection *first_section = NULL; |
84d1d650 L |
5858 | |
5859 | /* FIXME: Do we need to copy PT_NULL segment? */ | |
5860 | if (segment->p_type == PT_NULL) | |
5861 | continue; | |
5862 | ||
5863 | /* Compute how many sections are in this segment. */ | |
5864 | for (section = ibfd->sections, section_count = 0; | |
5865 | section != NULL; | |
5866 | section = section->next) | |
5867 | { | |
5868 | this_hdr = &(elf_section_data(section)->this_hdr); | |
5869 | if (ELF_IS_SECTION_IN_SEGMENT_FILE (this_hdr, segment)) | |
3271a814 | 5870 | { |
53020534 L |
5871 | if (!first_section) |
5872 | first_section = section; | |
3271a814 NS |
5873 | section_count++; |
5874 | } | |
84d1d650 L |
5875 | } |
5876 | ||
5877 | /* Allocate a segment map big enough to contain | |
5878 | all of the sections we have selected. */ | |
5879 | amt = sizeof (struct elf_segment_map); | |
5880 | if (section_count != 0) | |
5881 | amt += ((bfd_size_type) section_count - 1) * sizeof (asection *); | |
41f8ce69 | 5882 | map = bfd_zalloc (obfd, amt); |
84d1d650 L |
5883 | if (map == NULL) |
5884 | return FALSE; | |
5885 | ||
5886 | /* Initialize the fields of the output segment map with the | |
5887 | input segment. */ | |
5888 | map->next = NULL; | |
5889 | map->p_type = segment->p_type; | |
5890 | map->p_flags = segment->p_flags; | |
5891 | map->p_flags_valid = 1; | |
5892 | map->p_paddr = segment->p_paddr; | |
5893 | map->p_paddr_valid = 1; | |
3f570048 AM |
5894 | map->p_align = segment->p_align; |
5895 | map->p_align_valid = 1; | |
3271a814 | 5896 | map->p_vaddr_offset = 0; |
84d1d650 L |
5897 | |
5898 | /* Determine if this segment contains the ELF file header | |
5899 | and if it contains the program headers themselves. */ | |
5900 | map->includes_filehdr = (segment->p_offset == 0 | |
5901 | && segment->p_filesz >= iehdr->e_ehsize); | |
5902 | ||
5903 | map->includes_phdrs = 0; | |
5904 | if (! phdr_included || segment->p_type != PT_LOAD) | |
5905 | { | |
5906 | map->includes_phdrs = | |
5907 | (segment->p_offset <= (bfd_vma) iehdr->e_phoff | |
5908 | && (segment->p_offset + segment->p_filesz | |
5909 | >= ((bfd_vma) iehdr->e_phoff | |
5910 | + iehdr->e_phnum * iehdr->e_phentsize))); | |
5911 | ||
5912 | if (segment->p_type == PT_LOAD && map->includes_phdrs) | |
5913 | phdr_included = TRUE; | |
5914 | } | |
5915 | ||
3271a814 NS |
5916 | if (!map->includes_phdrs && !map->includes_filehdr) |
5917 | /* There is some other padding before the first section. */ | |
53020534 L |
5918 | map->p_vaddr_offset = ((first_section ? first_section->lma : 0) |
5919 | - segment->p_paddr); | |
3271a814 | 5920 | |
84d1d650 L |
5921 | if (section_count != 0) |
5922 | { | |
5923 | unsigned int isec = 0; | |
5924 | ||
53020534 | 5925 | for (section = first_section; |
84d1d650 L |
5926 | section != NULL; |
5927 | section = section->next) | |
5928 | { | |
5929 | this_hdr = &(elf_section_data(section)->this_hdr); | |
5930 | if (ELF_IS_SECTION_IN_SEGMENT_FILE (this_hdr, segment)) | |
53020534 L |
5931 | { |
5932 | map->sections[isec++] = section->output_section; | |
5933 | if (isec == section_count) | |
5934 | break; | |
5935 | } | |
84d1d650 L |
5936 | } |
5937 | } | |
5938 | ||
5939 | map->count = section_count; | |
5940 | *pointer_to_map = map; | |
5941 | pointer_to_map = &map->next; | |
5942 | } | |
5943 | ||
5944 | elf_tdata (obfd)->segment_map = map_first; | |
5945 | return TRUE; | |
5946 | } | |
5947 | ||
5948 | /* Copy private BFD data. This copies or rewrites ELF program header | |
5949 | information. */ | |
5950 | ||
5951 | static bfd_boolean | |
5952 | copy_private_bfd_data (bfd *ibfd, bfd *obfd) | |
5953 | { | |
84d1d650 L |
5954 | if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour |
5955 | || bfd_get_flavour (obfd) != bfd_target_elf_flavour) | |
5956 | return TRUE; | |
5957 | ||
5958 | if (elf_tdata (ibfd)->phdr == NULL) | |
5959 | return TRUE; | |
5960 | ||
5961 | if (ibfd->xvec == obfd->xvec) | |
5962 | { | |
cb3ff1e5 NC |
5963 | /* Check to see if any sections in the input BFD |
5964 | covered by ELF program header have changed. */ | |
d55ce4e2 | 5965 | Elf_Internal_Phdr *segment; |
84d1d650 L |
5966 | asection *section, *osec; |
5967 | unsigned int i, num_segments; | |
5968 | Elf_Internal_Shdr *this_hdr; | |
5969 | ||
5970 | /* Initialize the segment mark field. */ | |
5971 | for (section = obfd->sections; section != NULL; | |
5972 | section = section->next) | |
5973 | section->segment_mark = FALSE; | |
5974 | ||
5975 | num_segments = elf_elfheader (ibfd)->e_phnum; | |
5976 | for (i = 0, segment = elf_tdata (ibfd)->phdr; | |
5977 | i < num_segments; | |
5978 | i++, segment++) | |
5979 | { | |
5f6999aa NC |
5980 | /* PR binutils/3535. The Solaris linker always sets the p_paddr |
5981 | and p_memsz fields of special segments (DYNAMIC, INTERP) to 0 | |
5982 | which severly confuses things, so always regenerate the segment | |
5983 | map in this case. */ | |
5984 | if (segment->p_paddr == 0 | |
5985 | && segment->p_memsz == 0 | |
5986 | && (segment->p_type == PT_INTERP || segment->p_type == PT_DYNAMIC)) | |
cb3ff1e5 | 5987 | goto rewrite; |
5f6999aa | 5988 | |
84d1d650 L |
5989 | for (section = ibfd->sections; |
5990 | section != NULL; section = section->next) | |
5991 | { | |
5992 | /* We mark the output section so that we know it comes | |
5993 | from the input BFD. */ | |
5994 | osec = section->output_section; | |
5995 | if (osec) | |
5996 | osec->segment_mark = TRUE; | |
5997 | ||
5998 | /* Check if this section is covered by the segment. */ | |
5999 | this_hdr = &(elf_section_data(section)->this_hdr); | |
6000 | if (ELF_IS_SECTION_IN_SEGMENT_FILE (this_hdr, segment)) | |
6001 | { | |
6002 | /* FIXME: Check if its output section is changed or | |
6003 | removed. What else do we need to check? */ | |
6004 | if (osec == NULL | |
6005 | || section->flags != osec->flags | |
6006 | || section->lma != osec->lma | |
6007 | || section->vma != osec->vma | |
6008 | || section->size != osec->size | |
6009 | || section->rawsize != osec->rawsize | |
6010 | || section->alignment_power != osec->alignment_power) | |
6011 | goto rewrite; | |
6012 | } | |
6013 | } | |
6014 | } | |
6015 | ||
cb3ff1e5 | 6016 | /* Check to see if any output section do not come from the |
84d1d650 L |
6017 | input BFD. */ |
6018 | for (section = obfd->sections; section != NULL; | |
6019 | section = section->next) | |
6020 | { | |
6021 | if (section->segment_mark == FALSE) | |
6022 | goto rewrite; | |
6023 | else | |
6024 | section->segment_mark = FALSE; | |
6025 | } | |
6026 | ||
6027 | return copy_elf_program_header (ibfd, obfd); | |
6028 | } | |
6029 | ||
6030 | rewrite: | |
6031 | return rewrite_elf_program_header (ibfd, obfd); | |
6032 | } | |
6033 | ||
ccd2ec6a L |
6034 | /* Initialize private output section information from input section. */ |
6035 | ||
6036 | bfd_boolean | |
6037 | _bfd_elf_init_private_section_data (bfd *ibfd, | |
6038 | asection *isec, | |
6039 | bfd *obfd, | |
6040 | asection *osec, | |
6041 | struct bfd_link_info *link_info) | |
6042 | ||
6043 | { | |
6044 | Elf_Internal_Shdr *ihdr, *ohdr; | |
6045 | bfd_boolean need_group = link_info == NULL || link_info->relocatable; | |
6046 | ||
6047 | if (ibfd->xvec->flavour != bfd_target_elf_flavour | |
6048 | || obfd->xvec->flavour != bfd_target_elf_flavour) | |
6049 | return TRUE; | |
6050 | ||
e843e0f8 | 6051 | /* Don't copy the output ELF section type from input if the |
d3fd4074 | 6052 | output BFD section flags have been set to something different. |
e843e0f8 L |
6053 | elf_fake_sections will set ELF section type based on BFD |
6054 | section flags. */ | |
d270463e L |
6055 | if (osec->flags == isec->flags || !osec->flags) |
6056 | { | |
6057 | BFD_ASSERT (osec->flags == isec->flags | |
6058 | || (!osec->flags | |
6059 | && elf_section_type (osec) == SHT_NULL)); | |
6060 | elf_section_type (osec) = elf_section_type (isec); | |
6061 | } | |
6062 | ||
6063 | /* FIXME: Is this correct for all OS/PROC specific flags? */ | |
6064 | elf_section_flags (osec) |= (elf_section_flags (isec) | |
6065 | & (SHF_MASKOS | SHF_MASKPROC)); | |
ccd2ec6a L |
6066 | |
6067 | /* Set things up for objcopy and relocatable link. The output | |
6068 | SHT_GROUP section will have its elf_next_in_group pointing back | |
6069 | to the input group members. Ignore linker created group section. | |
6070 | See elfNN_ia64_object_p in elfxx-ia64.c. */ | |
ccd2ec6a L |
6071 | if (need_group) |
6072 | { | |
6073 | if (elf_sec_group (isec) == NULL | |
6074 | || (elf_sec_group (isec)->flags & SEC_LINKER_CREATED) == 0) | |
6075 | { | |
6076 | if (elf_section_flags (isec) & SHF_GROUP) | |
6077 | elf_section_flags (osec) |= SHF_GROUP; | |
6078 | elf_next_in_group (osec) = elf_next_in_group (isec); | |
6079 | elf_group_name (osec) = elf_group_name (isec); | |
6080 | } | |
6081 | } | |
6082 | ||
6083 | ihdr = &elf_section_data (isec)->this_hdr; | |
6084 | ||
6085 | /* We need to handle elf_linked_to_section for SHF_LINK_ORDER. We | |
6086 | don't use the output section of the linked-to section since it | |
6087 | may be NULL at this point. */ | |
6088 | if ((ihdr->sh_flags & SHF_LINK_ORDER) != 0) | |
6089 | { | |
6090 | ohdr = &elf_section_data (osec)->this_hdr; | |
6091 | ohdr->sh_flags |= SHF_LINK_ORDER; | |
6092 | elf_linked_to_section (osec) = elf_linked_to_section (isec); | |
6093 | } | |
6094 | ||
6095 | osec->use_rela_p = isec->use_rela_p; | |
6096 | ||
6097 | return TRUE; | |
6098 | } | |
6099 | ||
252b5132 RH |
6100 | /* Copy private section information. This copies over the entsize |
6101 | field, and sometimes the info field. */ | |
6102 | ||
b34976b6 | 6103 | bfd_boolean |
217aa764 AM |
6104 | _bfd_elf_copy_private_section_data (bfd *ibfd, |
6105 | asection *isec, | |
6106 | bfd *obfd, | |
6107 | asection *osec) | |
252b5132 RH |
6108 | { |
6109 | Elf_Internal_Shdr *ihdr, *ohdr; | |
6110 | ||
6111 | if (ibfd->xvec->flavour != bfd_target_elf_flavour | |
6112 | || obfd->xvec->flavour != bfd_target_elf_flavour) | |
b34976b6 | 6113 | return TRUE; |
252b5132 | 6114 | |
252b5132 RH |
6115 | ihdr = &elf_section_data (isec)->this_hdr; |
6116 | ohdr = &elf_section_data (osec)->this_hdr; | |
6117 | ||
6118 | ohdr->sh_entsize = ihdr->sh_entsize; | |
6119 | ||
6120 | if (ihdr->sh_type == SHT_SYMTAB | |
6121 | || ihdr->sh_type == SHT_DYNSYM | |
6122 | || ihdr->sh_type == SHT_GNU_verneed | |
6123 | || ihdr->sh_type == SHT_GNU_verdef) | |
6124 | ohdr->sh_info = ihdr->sh_info; | |
6125 | ||
ccd2ec6a L |
6126 | return _bfd_elf_init_private_section_data (ibfd, isec, obfd, osec, |
6127 | NULL); | |
252b5132 RH |
6128 | } |
6129 | ||
80fccad2 BW |
6130 | /* Copy private header information. */ |
6131 | ||
6132 | bfd_boolean | |
6133 | _bfd_elf_copy_private_header_data (bfd *ibfd, bfd *obfd) | |
6134 | { | |
30288845 AM |
6135 | asection *isec; |
6136 | ||
80fccad2 BW |
6137 | if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour |
6138 | || bfd_get_flavour (obfd) != bfd_target_elf_flavour) | |
6139 | return TRUE; | |
6140 | ||
6141 | /* Copy over private BFD data if it has not already been copied. | |
6142 | This must be done here, rather than in the copy_private_bfd_data | |
6143 | entry point, because the latter is called after the section | |
6144 | contents have been set, which means that the program headers have | |
6145 | already been worked out. */ | |
6146 | if (elf_tdata (obfd)->segment_map == NULL && elf_tdata (ibfd)->phdr != NULL) | |
6147 | { | |
6148 | if (! copy_private_bfd_data (ibfd, obfd)) | |
6149 | return FALSE; | |
6150 | } | |
6151 | ||
30288845 AM |
6152 | /* _bfd_elf_copy_private_section_data copied over the SHF_GROUP flag |
6153 | but this might be wrong if we deleted the group section. */ | |
6154 | for (isec = ibfd->sections; isec != NULL; isec = isec->next) | |
6155 | if (elf_section_type (isec) == SHT_GROUP | |
6156 | && isec->output_section == NULL) | |
6157 | { | |
6158 | asection *first = elf_next_in_group (isec); | |
6159 | asection *s = first; | |
6160 | while (s != NULL) | |
6161 | { | |
6162 | if (s->output_section != NULL) | |
6163 | { | |
6164 | elf_section_flags (s->output_section) &= ~SHF_GROUP; | |
6165 | elf_group_name (s->output_section) = NULL; | |
6166 | } | |
6167 | s = elf_next_in_group (s); | |
6168 | if (s == first) | |
6169 | break; | |
6170 | } | |
6171 | } | |
6172 | ||
80fccad2 BW |
6173 | return TRUE; |
6174 | } | |
6175 | ||
252b5132 RH |
6176 | /* Copy private symbol information. If this symbol is in a section |
6177 | which we did not map into a BFD section, try to map the section | |
6178 | index correctly. We use special macro definitions for the mapped | |
6179 | section indices; these definitions are interpreted by the | |
6180 | swap_out_syms function. */ | |
6181 | ||
9ad5cbcf AM |
6182 | #define MAP_ONESYMTAB (SHN_HIOS + 1) |
6183 | #define MAP_DYNSYMTAB (SHN_HIOS + 2) | |
6184 | #define MAP_STRTAB (SHN_HIOS + 3) | |
6185 | #define MAP_SHSTRTAB (SHN_HIOS + 4) | |
6186 | #define MAP_SYM_SHNDX (SHN_HIOS + 5) | |
252b5132 | 6187 | |
b34976b6 | 6188 | bfd_boolean |
217aa764 AM |
6189 | _bfd_elf_copy_private_symbol_data (bfd *ibfd, |
6190 | asymbol *isymarg, | |
6191 | bfd *obfd, | |
6192 | asymbol *osymarg) | |
252b5132 RH |
6193 | { |
6194 | elf_symbol_type *isym, *osym; | |
6195 | ||
6196 | if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour | |
6197 | || bfd_get_flavour (obfd) != bfd_target_elf_flavour) | |
b34976b6 | 6198 | return TRUE; |
252b5132 RH |
6199 | |
6200 | isym = elf_symbol_from (ibfd, isymarg); | |
6201 | osym = elf_symbol_from (obfd, osymarg); | |
6202 | ||
6203 | if (isym != NULL | |
6204 | && osym != NULL | |
6205 | && bfd_is_abs_section (isym->symbol.section)) | |
6206 | { | |
6207 | unsigned int shndx; | |
6208 | ||
6209 | shndx = isym->internal_elf_sym.st_shndx; | |
6210 | if (shndx == elf_onesymtab (ibfd)) | |
6211 | shndx = MAP_ONESYMTAB; | |
6212 | else if (shndx == elf_dynsymtab (ibfd)) | |
6213 | shndx = MAP_DYNSYMTAB; | |
6214 | else if (shndx == elf_tdata (ibfd)->strtab_section) | |
6215 | shndx = MAP_STRTAB; | |
6216 | else if (shndx == elf_tdata (ibfd)->shstrtab_section) | |
6217 | shndx = MAP_SHSTRTAB; | |
9ad5cbcf AM |
6218 | else if (shndx == elf_tdata (ibfd)->symtab_shndx_section) |
6219 | shndx = MAP_SYM_SHNDX; | |
252b5132 RH |
6220 | osym->internal_elf_sym.st_shndx = shndx; |
6221 | } | |
6222 | ||
b34976b6 | 6223 | return TRUE; |
252b5132 RH |
6224 | } |
6225 | ||
6226 | /* Swap out the symbols. */ | |
6227 | ||
b34976b6 | 6228 | static bfd_boolean |
217aa764 AM |
6229 | swap_out_syms (bfd *abfd, |
6230 | struct bfd_strtab_hash **sttp, | |
6231 | int relocatable_p) | |
252b5132 | 6232 | { |
9c5bfbb7 | 6233 | const struct elf_backend_data *bed; |
079e9a2f AM |
6234 | int symcount; |
6235 | asymbol **syms; | |
6236 | struct bfd_strtab_hash *stt; | |
6237 | Elf_Internal_Shdr *symtab_hdr; | |
9ad5cbcf | 6238 | Elf_Internal_Shdr *symtab_shndx_hdr; |
079e9a2f | 6239 | Elf_Internal_Shdr *symstrtab_hdr; |
f075ee0c AM |
6240 | bfd_byte *outbound_syms; |
6241 | bfd_byte *outbound_shndx; | |
079e9a2f AM |
6242 | int idx; |
6243 | bfd_size_type amt; | |
174fd7f9 | 6244 | bfd_boolean name_local_sections; |
252b5132 RH |
6245 | |
6246 | if (!elf_map_symbols (abfd)) | |
b34976b6 | 6247 | return FALSE; |
252b5132 | 6248 | |
c044fabd | 6249 | /* Dump out the symtabs. */ |
079e9a2f AM |
6250 | stt = _bfd_elf_stringtab_init (); |
6251 | if (stt == NULL) | |
b34976b6 | 6252 | return FALSE; |
252b5132 | 6253 | |
079e9a2f AM |
6254 | bed = get_elf_backend_data (abfd); |
6255 | symcount = bfd_get_symcount (abfd); | |
6256 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; | |
6257 | symtab_hdr->sh_type = SHT_SYMTAB; | |
6258 | symtab_hdr->sh_entsize = bed->s->sizeof_sym; | |
6259 | symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1); | |
6260 | symtab_hdr->sh_info = elf_num_locals (abfd) + 1; | |
45d6a902 | 6261 | symtab_hdr->sh_addralign = 1 << bed->s->log_file_align; |
079e9a2f AM |
6262 | |
6263 | symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr; | |
6264 | symstrtab_hdr->sh_type = SHT_STRTAB; | |
6265 | ||
d0fb9a8d | 6266 | outbound_syms = bfd_alloc2 (abfd, 1 + symcount, bed->s->sizeof_sym); |
079e9a2f | 6267 | if (outbound_syms == NULL) |
5ed6aba4 NC |
6268 | { |
6269 | _bfd_stringtab_free (stt); | |
6270 | return FALSE; | |
6271 | } | |
217aa764 | 6272 | symtab_hdr->contents = outbound_syms; |
252b5132 | 6273 | |
9ad5cbcf AM |
6274 | outbound_shndx = NULL; |
6275 | symtab_shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr; | |
6276 | if (symtab_shndx_hdr->sh_name != 0) | |
6277 | { | |
6278 | amt = (bfd_size_type) (1 + symcount) * sizeof (Elf_External_Sym_Shndx); | |
d0fb9a8d JJ |
6279 | outbound_shndx = bfd_zalloc2 (abfd, 1 + symcount, |
6280 | sizeof (Elf_External_Sym_Shndx)); | |
9ad5cbcf | 6281 | if (outbound_shndx == NULL) |
5ed6aba4 NC |
6282 | { |
6283 | _bfd_stringtab_free (stt); | |
6284 | return FALSE; | |
6285 | } | |
6286 | ||
9ad5cbcf AM |
6287 | symtab_shndx_hdr->contents = outbound_shndx; |
6288 | symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX; | |
6289 | symtab_shndx_hdr->sh_size = amt; | |
6290 | symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx); | |
6291 | symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx); | |
6292 | } | |
6293 | ||
589e6347 | 6294 | /* Now generate the data (for "contents"). */ |
079e9a2f AM |
6295 | { |
6296 | /* Fill in zeroth symbol and swap it out. */ | |
6297 | Elf_Internal_Sym sym; | |
6298 | sym.st_name = 0; | |
6299 | sym.st_value = 0; | |
6300 | sym.st_size = 0; | |
6301 | sym.st_info = 0; | |
6302 | sym.st_other = 0; | |
6303 | sym.st_shndx = SHN_UNDEF; | |
9ad5cbcf | 6304 | bed->s->swap_symbol_out (abfd, &sym, outbound_syms, outbound_shndx); |
079e9a2f | 6305 | outbound_syms += bed->s->sizeof_sym; |
9ad5cbcf AM |
6306 | if (outbound_shndx != NULL) |
6307 | outbound_shndx += sizeof (Elf_External_Sym_Shndx); | |
079e9a2f | 6308 | } |
252b5132 | 6309 | |
174fd7f9 RS |
6310 | name_local_sections |
6311 | = (bed->elf_backend_name_local_section_symbols | |
6312 | && bed->elf_backend_name_local_section_symbols (abfd)); | |
6313 | ||
079e9a2f AM |
6314 | syms = bfd_get_outsymbols (abfd); |
6315 | for (idx = 0; idx < symcount; idx++) | |
252b5132 | 6316 | { |
252b5132 | 6317 | Elf_Internal_Sym sym; |
079e9a2f AM |
6318 | bfd_vma value = syms[idx]->value; |
6319 | elf_symbol_type *type_ptr; | |
6320 | flagword flags = syms[idx]->flags; | |
6321 | int type; | |
252b5132 | 6322 | |
174fd7f9 RS |
6323 | if (!name_local_sections |
6324 | && (flags & (BSF_SECTION_SYM | BSF_GLOBAL)) == BSF_SECTION_SYM) | |
079e9a2f AM |
6325 | { |
6326 | /* Local section symbols have no name. */ | |
6327 | sym.st_name = 0; | |
6328 | } | |
6329 | else | |
6330 | { | |
6331 | sym.st_name = (unsigned long) _bfd_stringtab_add (stt, | |
6332 | syms[idx]->name, | |
b34976b6 | 6333 | TRUE, FALSE); |
079e9a2f | 6334 | if (sym.st_name == (unsigned long) -1) |
5ed6aba4 NC |
6335 | { |
6336 | _bfd_stringtab_free (stt); | |
6337 | return FALSE; | |
6338 | } | |
079e9a2f | 6339 | } |
252b5132 | 6340 | |
079e9a2f | 6341 | type_ptr = elf_symbol_from (abfd, syms[idx]); |
252b5132 | 6342 | |
079e9a2f AM |
6343 | if ((flags & BSF_SECTION_SYM) == 0 |
6344 | && bfd_is_com_section (syms[idx]->section)) | |
6345 | { | |
6346 | /* ELF common symbols put the alignment into the `value' field, | |
6347 | and the size into the `size' field. This is backwards from | |
6348 | how BFD handles it, so reverse it here. */ | |
6349 | sym.st_size = value; | |
6350 | if (type_ptr == NULL | |
6351 | || type_ptr->internal_elf_sym.st_value == 0) | |
6352 | sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value)); | |
6353 | else | |
6354 | sym.st_value = type_ptr->internal_elf_sym.st_value; | |
6355 | sym.st_shndx = _bfd_elf_section_from_bfd_section | |
6356 | (abfd, syms[idx]->section); | |
6357 | } | |
6358 | else | |
6359 | { | |
6360 | asection *sec = syms[idx]->section; | |
6361 | int shndx; | |
252b5132 | 6362 | |
079e9a2f AM |
6363 | if (sec->output_section) |
6364 | { | |
6365 | value += sec->output_offset; | |
6366 | sec = sec->output_section; | |
6367 | } | |
589e6347 | 6368 | |
079e9a2f AM |
6369 | /* Don't add in the section vma for relocatable output. */ |
6370 | if (! relocatable_p) | |
6371 | value += sec->vma; | |
6372 | sym.st_value = value; | |
6373 | sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0; | |
6374 | ||
6375 | if (bfd_is_abs_section (sec) | |
6376 | && type_ptr != NULL | |
6377 | && type_ptr->internal_elf_sym.st_shndx != 0) | |
6378 | { | |
6379 | /* This symbol is in a real ELF section which we did | |
6380 | not create as a BFD section. Undo the mapping done | |
6381 | by copy_private_symbol_data. */ | |
6382 | shndx = type_ptr->internal_elf_sym.st_shndx; | |
6383 | switch (shndx) | |
6384 | { | |
6385 | case MAP_ONESYMTAB: | |
6386 | shndx = elf_onesymtab (abfd); | |
6387 | break; | |
6388 | case MAP_DYNSYMTAB: | |
6389 | shndx = elf_dynsymtab (abfd); | |
6390 | break; | |
6391 | case MAP_STRTAB: | |
6392 | shndx = elf_tdata (abfd)->strtab_section; | |
6393 | break; | |
6394 | case MAP_SHSTRTAB: | |
6395 | shndx = elf_tdata (abfd)->shstrtab_section; | |
6396 | break; | |
9ad5cbcf AM |
6397 | case MAP_SYM_SHNDX: |
6398 | shndx = elf_tdata (abfd)->symtab_shndx_section; | |
6399 | break; | |
079e9a2f AM |
6400 | default: |
6401 | break; | |
6402 | } | |
6403 | } | |
6404 | else | |
6405 | { | |
6406 | shndx = _bfd_elf_section_from_bfd_section (abfd, sec); | |
252b5132 | 6407 | |
079e9a2f AM |
6408 | if (shndx == -1) |
6409 | { | |
6410 | asection *sec2; | |
6411 | ||
6412 | /* Writing this would be a hell of a lot easier if | |
6413 | we had some decent documentation on bfd, and | |
6414 | knew what to expect of the library, and what to | |
6415 | demand of applications. For example, it | |
6416 | appears that `objcopy' might not set the | |
6417 | section of a symbol to be a section that is | |
6418 | actually in the output file. */ | |
6419 | sec2 = bfd_get_section_by_name (abfd, sec->name); | |
589e6347 NC |
6420 | if (sec2 == NULL) |
6421 | { | |
6422 | _bfd_error_handler (_("\ | |
6423 | Unable to find equivalent output section for symbol '%s' from section '%s'"), | |
6424 | syms[idx]->name ? syms[idx]->name : "<Local sym>", | |
6425 | sec->name); | |
811072d8 | 6426 | bfd_set_error (bfd_error_invalid_operation); |
5ed6aba4 | 6427 | _bfd_stringtab_free (stt); |
589e6347 NC |
6428 | return FALSE; |
6429 | } | |
811072d8 | 6430 | |
079e9a2f AM |
6431 | shndx = _bfd_elf_section_from_bfd_section (abfd, sec2); |
6432 | BFD_ASSERT (shndx != -1); | |
6433 | } | |
6434 | } | |
252b5132 | 6435 | |
079e9a2f AM |
6436 | sym.st_shndx = shndx; |
6437 | } | |
252b5132 | 6438 | |
13ae64f3 JJ |
6439 | if ((flags & BSF_THREAD_LOCAL) != 0) |
6440 | type = STT_TLS; | |
6441 | else if ((flags & BSF_FUNCTION) != 0) | |
079e9a2f AM |
6442 | type = STT_FUNC; |
6443 | else if ((flags & BSF_OBJECT) != 0) | |
6444 | type = STT_OBJECT; | |
d9352518 DB |
6445 | else if ((flags & BSF_RELC) != 0) |
6446 | type = STT_RELC; | |
6447 | else if ((flags & BSF_SRELC) != 0) | |
6448 | type = STT_SRELC; | |
079e9a2f AM |
6449 | else |
6450 | type = STT_NOTYPE; | |
252b5132 | 6451 | |
13ae64f3 JJ |
6452 | if (syms[idx]->section->flags & SEC_THREAD_LOCAL) |
6453 | type = STT_TLS; | |
6454 | ||
589e6347 | 6455 | /* Processor-specific types. */ |
079e9a2f AM |
6456 | if (type_ptr != NULL |
6457 | && bed->elf_backend_get_symbol_type) | |
6458 | type = ((*bed->elf_backend_get_symbol_type) | |
6459 | (&type_ptr->internal_elf_sym, type)); | |
252b5132 | 6460 | |
079e9a2f AM |
6461 | if (flags & BSF_SECTION_SYM) |
6462 | { | |
6463 | if (flags & BSF_GLOBAL) | |
6464 | sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION); | |
6465 | else | |
6466 | sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION); | |
6467 | } | |
6468 | else if (bfd_is_com_section (syms[idx]->section)) | |
6469 | sym.st_info = ELF_ST_INFO (STB_GLOBAL, type); | |
6470 | else if (bfd_is_und_section (syms[idx]->section)) | |
6471 | sym.st_info = ELF_ST_INFO (((flags & BSF_WEAK) | |
6472 | ? STB_WEAK | |
6473 | : STB_GLOBAL), | |
6474 | type); | |
6475 | else if (flags & BSF_FILE) | |
6476 | sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE); | |
6477 | else | |
6478 | { | |
6479 | int bind = STB_LOCAL; | |
252b5132 | 6480 | |
079e9a2f AM |
6481 | if (flags & BSF_LOCAL) |
6482 | bind = STB_LOCAL; | |
6483 | else if (flags & BSF_WEAK) | |
6484 | bind = STB_WEAK; | |
6485 | else if (flags & BSF_GLOBAL) | |
6486 | bind = STB_GLOBAL; | |
252b5132 | 6487 | |
079e9a2f AM |
6488 | sym.st_info = ELF_ST_INFO (bind, type); |
6489 | } | |
252b5132 | 6490 | |
079e9a2f AM |
6491 | if (type_ptr != NULL) |
6492 | sym.st_other = type_ptr->internal_elf_sym.st_other; | |
6493 | else | |
6494 | sym.st_other = 0; | |
252b5132 | 6495 | |
9ad5cbcf | 6496 | bed->s->swap_symbol_out (abfd, &sym, outbound_syms, outbound_shndx); |
079e9a2f | 6497 | outbound_syms += bed->s->sizeof_sym; |
9ad5cbcf AM |
6498 | if (outbound_shndx != NULL) |
6499 | outbound_shndx += sizeof (Elf_External_Sym_Shndx); | |
079e9a2f | 6500 | } |
252b5132 | 6501 | |
079e9a2f AM |
6502 | *sttp = stt; |
6503 | symstrtab_hdr->sh_size = _bfd_stringtab_size (stt); | |
6504 | symstrtab_hdr->sh_type = SHT_STRTAB; | |
252b5132 | 6505 | |
079e9a2f AM |
6506 | symstrtab_hdr->sh_flags = 0; |
6507 | symstrtab_hdr->sh_addr = 0; | |
6508 | symstrtab_hdr->sh_entsize = 0; | |
6509 | symstrtab_hdr->sh_link = 0; | |
6510 | symstrtab_hdr->sh_info = 0; | |
6511 | symstrtab_hdr->sh_addralign = 1; | |
252b5132 | 6512 | |
b34976b6 | 6513 | return TRUE; |
252b5132 RH |
6514 | } |
6515 | ||
6516 | /* Return the number of bytes required to hold the symtab vector. | |
6517 | ||
6518 | Note that we base it on the count plus 1, since we will null terminate | |
6519 | the vector allocated based on this size. However, the ELF symbol table | |
6520 | always has a dummy entry as symbol #0, so it ends up even. */ | |
6521 | ||
6522 | long | |
217aa764 | 6523 | _bfd_elf_get_symtab_upper_bound (bfd *abfd) |
252b5132 RH |
6524 | { |
6525 | long symcount; | |
6526 | long symtab_size; | |
6527 | Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr; | |
6528 | ||
6529 | symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym; | |
b99d1833 AM |
6530 | symtab_size = (symcount + 1) * (sizeof (asymbol *)); |
6531 | if (symcount > 0) | |
6532 | symtab_size -= sizeof (asymbol *); | |
252b5132 RH |
6533 | |
6534 | return symtab_size; | |
6535 | } | |
6536 | ||
6537 | long | |
217aa764 | 6538 | _bfd_elf_get_dynamic_symtab_upper_bound (bfd *abfd) |
252b5132 RH |
6539 | { |
6540 | long symcount; | |
6541 | long symtab_size; | |
6542 | Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr; | |
6543 | ||
6544 | if (elf_dynsymtab (abfd) == 0) | |
6545 | { | |
6546 | bfd_set_error (bfd_error_invalid_operation); | |
6547 | return -1; | |
6548 | } | |
6549 | ||
6550 | symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym; | |
b99d1833 AM |
6551 | symtab_size = (symcount + 1) * (sizeof (asymbol *)); |
6552 | if (symcount > 0) | |
6553 | symtab_size -= sizeof (asymbol *); | |
252b5132 RH |
6554 | |
6555 | return symtab_size; | |
6556 | } | |
6557 | ||
6558 | long | |
217aa764 AM |
6559 | _bfd_elf_get_reloc_upper_bound (bfd *abfd ATTRIBUTE_UNUSED, |
6560 | sec_ptr asect) | |
252b5132 RH |
6561 | { |
6562 | return (asect->reloc_count + 1) * sizeof (arelent *); | |
6563 | } | |
6564 | ||
6565 | /* Canonicalize the relocs. */ | |
6566 | ||
6567 | long | |
217aa764 AM |
6568 | _bfd_elf_canonicalize_reloc (bfd *abfd, |
6569 | sec_ptr section, | |
6570 | arelent **relptr, | |
6571 | asymbol **symbols) | |
252b5132 RH |
6572 | { |
6573 | arelent *tblptr; | |
6574 | unsigned int i; | |
9c5bfbb7 | 6575 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
252b5132 | 6576 | |
b34976b6 | 6577 | if (! bed->s->slurp_reloc_table (abfd, section, symbols, FALSE)) |
252b5132 RH |
6578 | return -1; |
6579 | ||
6580 | tblptr = section->relocation; | |
6581 | for (i = 0; i < section->reloc_count; i++) | |
6582 | *relptr++ = tblptr++; | |
6583 | ||
6584 | *relptr = NULL; | |
6585 | ||
6586 | return section->reloc_count; | |
6587 | } | |
6588 | ||
6589 | long | |
6cee3f79 | 6590 | _bfd_elf_canonicalize_symtab (bfd *abfd, asymbol **allocation) |
252b5132 | 6591 | { |
9c5bfbb7 | 6592 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
217aa764 | 6593 | long symcount = bed->s->slurp_symbol_table (abfd, allocation, FALSE); |
252b5132 RH |
6594 | |
6595 | if (symcount >= 0) | |
6596 | bfd_get_symcount (abfd) = symcount; | |
6597 | return symcount; | |
6598 | } | |
6599 | ||
6600 | long | |
217aa764 AM |
6601 | _bfd_elf_canonicalize_dynamic_symtab (bfd *abfd, |
6602 | asymbol **allocation) | |
252b5132 | 6603 | { |
9c5bfbb7 | 6604 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
217aa764 | 6605 | long symcount = bed->s->slurp_symbol_table (abfd, allocation, TRUE); |
1f70368c DJ |
6606 | |
6607 | if (symcount >= 0) | |
6608 | bfd_get_dynamic_symcount (abfd) = symcount; | |
6609 | return symcount; | |
252b5132 RH |
6610 | } |
6611 | ||
8615f3f2 AM |
6612 | /* Return the size required for the dynamic reloc entries. Any loadable |
6613 | section that was actually installed in the BFD, and has type SHT_REL | |
6614 | or SHT_RELA, and uses the dynamic symbol table, is considered to be a | |
6615 | dynamic reloc section. */ | |
252b5132 RH |
6616 | |
6617 | long | |
217aa764 | 6618 | _bfd_elf_get_dynamic_reloc_upper_bound (bfd *abfd) |
252b5132 RH |
6619 | { |
6620 | long ret; | |
6621 | asection *s; | |
6622 | ||
6623 | if (elf_dynsymtab (abfd) == 0) | |
6624 | { | |
6625 | bfd_set_error (bfd_error_invalid_operation); | |
6626 | return -1; | |
6627 | } | |
6628 | ||
6629 | ret = sizeof (arelent *); | |
6630 | for (s = abfd->sections; s != NULL; s = s->next) | |
8615f3f2 AM |
6631 | if ((s->flags & SEC_LOAD) != 0 |
6632 | && elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd) | |
252b5132 RH |
6633 | && (elf_section_data (s)->this_hdr.sh_type == SHT_REL |
6634 | || elf_section_data (s)->this_hdr.sh_type == SHT_RELA)) | |
eea6121a | 6635 | ret += ((s->size / elf_section_data (s)->this_hdr.sh_entsize) |
252b5132 RH |
6636 | * sizeof (arelent *)); |
6637 | ||
6638 | return ret; | |
6639 | } | |
6640 | ||
8615f3f2 AM |
6641 | /* Canonicalize the dynamic relocation entries. Note that we return the |
6642 | dynamic relocations as a single block, although they are actually | |
6643 | associated with particular sections; the interface, which was | |
6644 | designed for SunOS style shared libraries, expects that there is only | |
6645 | one set of dynamic relocs. Any loadable section that was actually | |
6646 | installed in the BFD, and has type SHT_REL or SHT_RELA, and uses the | |
6647 | dynamic symbol table, is considered to be a dynamic reloc section. */ | |
252b5132 RH |
6648 | |
6649 | long | |
217aa764 AM |
6650 | _bfd_elf_canonicalize_dynamic_reloc (bfd *abfd, |
6651 | arelent **storage, | |
6652 | asymbol **syms) | |
252b5132 | 6653 | { |
217aa764 | 6654 | bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean); |
252b5132 RH |
6655 | asection *s; |
6656 | long ret; | |
6657 | ||
6658 | if (elf_dynsymtab (abfd) == 0) | |
6659 | { | |
6660 | bfd_set_error (bfd_error_invalid_operation); | |
6661 | return -1; | |
6662 | } | |
6663 | ||
6664 | slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table; | |
6665 | ret = 0; | |
6666 | for (s = abfd->sections; s != NULL; s = s->next) | |
6667 | { | |
8615f3f2 AM |
6668 | if ((s->flags & SEC_LOAD) != 0 |
6669 | && elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd) | |
252b5132 RH |
6670 | && (elf_section_data (s)->this_hdr.sh_type == SHT_REL |
6671 | || elf_section_data (s)->this_hdr.sh_type == SHT_RELA)) | |
6672 | { | |
6673 | arelent *p; | |
6674 | long count, i; | |
6675 | ||
b34976b6 | 6676 | if (! (*slurp_relocs) (abfd, s, syms, TRUE)) |
252b5132 | 6677 | return -1; |
eea6121a | 6678 | count = s->size / elf_section_data (s)->this_hdr.sh_entsize; |
252b5132 RH |
6679 | p = s->relocation; |
6680 | for (i = 0; i < count; i++) | |
6681 | *storage++ = p++; | |
6682 | ret += count; | |
6683 | } | |
6684 | } | |
6685 | ||
6686 | *storage = NULL; | |
6687 | ||
6688 | return ret; | |
6689 | } | |
6690 | \f | |
6691 | /* Read in the version information. */ | |
6692 | ||
b34976b6 | 6693 | bfd_boolean |
fc0e6df6 | 6694 | _bfd_elf_slurp_version_tables (bfd *abfd, bfd_boolean default_imported_symver) |
252b5132 RH |
6695 | { |
6696 | bfd_byte *contents = NULL; | |
fc0e6df6 PB |
6697 | unsigned int freeidx = 0; |
6698 | ||
6699 | if (elf_dynverref (abfd) != 0) | |
6700 | { | |
6701 | Elf_Internal_Shdr *hdr; | |
6702 | Elf_External_Verneed *everneed; | |
6703 | Elf_Internal_Verneed *iverneed; | |
6704 | unsigned int i; | |
d0fb9a8d | 6705 | bfd_byte *contents_end; |
fc0e6df6 PB |
6706 | |
6707 | hdr = &elf_tdata (abfd)->dynverref_hdr; | |
6708 | ||
d0fb9a8d JJ |
6709 | elf_tdata (abfd)->verref = bfd_zalloc2 (abfd, hdr->sh_info, |
6710 | sizeof (Elf_Internal_Verneed)); | |
fc0e6df6 PB |
6711 | if (elf_tdata (abfd)->verref == NULL) |
6712 | goto error_return; | |
6713 | ||
6714 | elf_tdata (abfd)->cverrefs = hdr->sh_info; | |
6715 | ||
6716 | contents = bfd_malloc (hdr->sh_size); | |
6717 | if (contents == NULL) | |
d0fb9a8d JJ |
6718 | { |
6719 | error_return_verref: | |
6720 | elf_tdata (abfd)->verref = NULL; | |
6721 | elf_tdata (abfd)->cverrefs = 0; | |
6722 | goto error_return; | |
6723 | } | |
fc0e6df6 PB |
6724 | if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0 |
6725 | || bfd_bread (contents, hdr->sh_size, abfd) != hdr->sh_size) | |
d0fb9a8d | 6726 | goto error_return_verref; |
fc0e6df6 | 6727 | |
d0fb9a8d JJ |
6728 | if (hdr->sh_info && hdr->sh_size < sizeof (Elf_External_Verneed)) |
6729 | goto error_return_verref; | |
6730 | ||
6731 | BFD_ASSERT (sizeof (Elf_External_Verneed) | |
6732 | == sizeof (Elf_External_Vernaux)); | |
6733 | contents_end = contents + hdr->sh_size - sizeof (Elf_External_Verneed); | |
fc0e6df6 PB |
6734 | everneed = (Elf_External_Verneed *) contents; |
6735 | iverneed = elf_tdata (abfd)->verref; | |
6736 | for (i = 0; i < hdr->sh_info; i++, iverneed++) | |
6737 | { | |
6738 | Elf_External_Vernaux *evernaux; | |
6739 | Elf_Internal_Vernaux *ivernaux; | |
6740 | unsigned int j; | |
6741 | ||
6742 | _bfd_elf_swap_verneed_in (abfd, everneed, iverneed); | |
6743 | ||
6744 | iverneed->vn_bfd = abfd; | |
6745 | ||
6746 | iverneed->vn_filename = | |
6747 | bfd_elf_string_from_elf_section (abfd, hdr->sh_link, | |
6748 | iverneed->vn_file); | |
6749 | if (iverneed->vn_filename == NULL) | |
d0fb9a8d | 6750 | goto error_return_verref; |
fc0e6df6 | 6751 | |
d0fb9a8d JJ |
6752 | if (iverneed->vn_cnt == 0) |
6753 | iverneed->vn_auxptr = NULL; | |
6754 | else | |
6755 | { | |
6756 | iverneed->vn_auxptr = bfd_alloc2 (abfd, iverneed->vn_cnt, | |
6757 | sizeof (Elf_Internal_Vernaux)); | |
6758 | if (iverneed->vn_auxptr == NULL) | |
6759 | goto error_return_verref; | |
6760 | } | |
6761 | ||
6762 | if (iverneed->vn_aux | |
6763 | > (size_t) (contents_end - (bfd_byte *) everneed)) | |
6764 | goto error_return_verref; | |
fc0e6df6 PB |
6765 | |
6766 | evernaux = ((Elf_External_Vernaux *) | |
6767 | ((bfd_byte *) everneed + iverneed->vn_aux)); | |
6768 | ivernaux = iverneed->vn_auxptr; | |
6769 | for (j = 0; j < iverneed->vn_cnt; j++, ivernaux++) | |
6770 | { | |
6771 | _bfd_elf_swap_vernaux_in (abfd, evernaux, ivernaux); | |
6772 | ||
6773 | ivernaux->vna_nodename = | |
6774 | bfd_elf_string_from_elf_section (abfd, hdr->sh_link, | |
6775 | ivernaux->vna_name); | |
6776 | if (ivernaux->vna_nodename == NULL) | |
d0fb9a8d | 6777 | goto error_return_verref; |
fc0e6df6 PB |
6778 | |
6779 | if (j + 1 < iverneed->vn_cnt) | |
6780 | ivernaux->vna_nextptr = ivernaux + 1; | |
6781 | else | |
6782 | ivernaux->vna_nextptr = NULL; | |
6783 | ||
d0fb9a8d JJ |
6784 | if (ivernaux->vna_next |
6785 | > (size_t) (contents_end - (bfd_byte *) evernaux)) | |
6786 | goto error_return_verref; | |
6787 | ||
fc0e6df6 PB |
6788 | evernaux = ((Elf_External_Vernaux *) |
6789 | ((bfd_byte *) evernaux + ivernaux->vna_next)); | |
6790 | ||
6791 | if (ivernaux->vna_other > freeidx) | |
6792 | freeidx = ivernaux->vna_other; | |
6793 | } | |
6794 | ||
6795 | if (i + 1 < hdr->sh_info) | |
6796 | iverneed->vn_nextref = iverneed + 1; | |
6797 | else | |
6798 | iverneed->vn_nextref = NULL; | |
6799 | ||
d0fb9a8d JJ |
6800 | if (iverneed->vn_next |
6801 | > (size_t) (contents_end - (bfd_byte *) everneed)) | |
6802 | goto error_return_verref; | |
6803 | ||
fc0e6df6 PB |
6804 | everneed = ((Elf_External_Verneed *) |
6805 | ((bfd_byte *) everneed + iverneed->vn_next)); | |
6806 | } | |
6807 | ||
6808 | free (contents); | |
6809 | contents = NULL; | |
6810 | } | |
252b5132 RH |
6811 | |
6812 | if (elf_dynverdef (abfd) != 0) | |
6813 | { | |
6814 | Elf_Internal_Shdr *hdr; | |
6815 | Elf_External_Verdef *everdef; | |
6816 | Elf_Internal_Verdef *iverdef; | |
f631889e UD |
6817 | Elf_Internal_Verdef *iverdefarr; |
6818 | Elf_Internal_Verdef iverdefmem; | |
252b5132 | 6819 | unsigned int i; |
062e2358 | 6820 | unsigned int maxidx; |
d0fb9a8d | 6821 | bfd_byte *contents_end_def, *contents_end_aux; |
252b5132 RH |
6822 | |
6823 | hdr = &elf_tdata (abfd)->dynverdef_hdr; | |
6824 | ||
217aa764 | 6825 | contents = bfd_malloc (hdr->sh_size); |
252b5132 RH |
6826 | if (contents == NULL) |
6827 | goto error_return; | |
6828 | if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0 | |
217aa764 | 6829 | || bfd_bread (contents, hdr->sh_size, abfd) != hdr->sh_size) |
252b5132 RH |
6830 | goto error_return; |
6831 | ||
d0fb9a8d JJ |
6832 | if (hdr->sh_info && hdr->sh_size < sizeof (Elf_External_Verdef)) |
6833 | goto error_return; | |
6834 | ||
6835 | BFD_ASSERT (sizeof (Elf_External_Verdef) | |
6836 | >= sizeof (Elf_External_Verdaux)); | |
6837 | contents_end_def = contents + hdr->sh_size | |
6838 | - sizeof (Elf_External_Verdef); | |
6839 | contents_end_aux = contents + hdr->sh_size | |
6840 | - sizeof (Elf_External_Verdaux); | |
6841 | ||
f631889e UD |
6842 | /* We know the number of entries in the section but not the maximum |
6843 | index. Therefore we have to run through all entries and find | |
6844 | the maximum. */ | |
252b5132 | 6845 | everdef = (Elf_External_Verdef *) contents; |
f631889e UD |
6846 | maxidx = 0; |
6847 | for (i = 0; i < hdr->sh_info; ++i) | |
6848 | { | |
6849 | _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem); | |
6850 | ||
062e2358 AM |
6851 | if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) > maxidx) |
6852 | maxidx = iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION); | |
f631889e | 6853 | |
d0fb9a8d JJ |
6854 | if (iverdefmem.vd_next |
6855 | > (size_t) (contents_end_def - (bfd_byte *) everdef)) | |
6856 | goto error_return; | |
6857 | ||
f631889e UD |
6858 | everdef = ((Elf_External_Verdef *) |
6859 | ((bfd_byte *) everdef + iverdefmem.vd_next)); | |
6860 | } | |
6861 | ||
fc0e6df6 PB |
6862 | if (default_imported_symver) |
6863 | { | |
6864 | if (freeidx > maxidx) | |
6865 | maxidx = ++freeidx; | |
6866 | else | |
6867 | freeidx = ++maxidx; | |
6868 | } | |
d0fb9a8d JJ |
6869 | elf_tdata (abfd)->verdef = bfd_zalloc2 (abfd, maxidx, |
6870 | sizeof (Elf_Internal_Verdef)); | |
f631889e UD |
6871 | if (elf_tdata (abfd)->verdef == NULL) |
6872 | goto error_return; | |
6873 | ||
6874 | elf_tdata (abfd)->cverdefs = maxidx; | |
6875 | ||
6876 | everdef = (Elf_External_Verdef *) contents; | |
6877 | iverdefarr = elf_tdata (abfd)->verdef; | |
6878 | for (i = 0; i < hdr->sh_info; i++) | |
252b5132 RH |
6879 | { |
6880 | Elf_External_Verdaux *everdaux; | |
6881 | Elf_Internal_Verdaux *iverdaux; | |
6882 | unsigned int j; | |
6883 | ||
f631889e UD |
6884 | _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem); |
6885 | ||
d0fb9a8d JJ |
6886 | if ((iverdefmem.vd_ndx & VERSYM_VERSION) == 0) |
6887 | { | |
6888 | error_return_verdef: | |
6889 | elf_tdata (abfd)->verdef = NULL; | |
6890 | elf_tdata (abfd)->cverdefs = 0; | |
6891 | goto error_return; | |
6892 | } | |
6893 | ||
f631889e UD |
6894 | iverdef = &iverdefarr[(iverdefmem.vd_ndx & VERSYM_VERSION) - 1]; |
6895 | memcpy (iverdef, &iverdefmem, sizeof (Elf_Internal_Verdef)); | |
252b5132 RH |
6896 | |
6897 | iverdef->vd_bfd = abfd; | |
6898 | ||
d0fb9a8d JJ |
6899 | if (iverdef->vd_cnt == 0) |
6900 | iverdef->vd_auxptr = NULL; | |
6901 | else | |
6902 | { | |
6903 | iverdef->vd_auxptr = bfd_alloc2 (abfd, iverdef->vd_cnt, | |
6904 | sizeof (Elf_Internal_Verdaux)); | |
6905 | if (iverdef->vd_auxptr == NULL) | |
6906 | goto error_return_verdef; | |
6907 | } | |
6908 | ||
6909 | if (iverdef->vd_aux | |
6910 | > (size_t) (contents_end_aux - (bfd_byte *) everdef)) | |
6911 | goto error_return_verdef; | |
252b5132 RH |
6912 | |
6913 | everdaux = ((Elf_External_Verdaux *) | |
6914 | ((bfd_byte *) everdef + iverdef->vd_aux)); | |
6915 | iverdaux = iverdef->vd_auxptr; | |
6916 | for (j = 0; j < iverdef->vd_cnt; j++, iverdaux++) | |
6917 | { | |
6918 | _bfd_elf_swap_verdaux_in (abfd, everdaux, iverdaux); | |
6919 | ||
6920 | iverdaux->vda_nodename = | |
6921 | bfd_elf_string_from_elf_section (abfd, hdr->sh_link, | |
6922 | iverdaux->vda_name); | |
6923 | if (iverdaux->vda_nodename == NULL) | |
d0fb9a8d | 6924 | goto error_return_verdef; |
252b5132 RH |
6925 | |
6926 | if (j + 1 < iverdef->vd_cnt) | |
6927 | iverdaux->vda_nextptr = iverdaux + 1; | |
6928 | else | |
6929 | iverdaux->vda_nextptr = NULL; | |
6930 | ||
d0fb9a8d JJ |
6931 | if (iverdaux->vda_next |
6932 | > (size_t) (contents_end_aux - (bfd_byte *) everdaux)) | |
6933 | goto error_return_verdef; | |
6934 | ||
252b5132 RH |
6935 | everdaux = ((Elf_External_Verdaux *) |
6936 | ((bfd_byte *) everdaux + iverdaux->vda_next)); | |
6937 | } | |
6938 | ||
d0fb9a8d JJ |
6939 | if (iverdef->vd_cnt) |
6940 | iverdef->vd_nodename = iverdef->vd_auxptr->vda_nodename; | |
252b5132 | 6941 | |
d0fb9a8d | 6942 | if ((size_t) (iverdef - iverdefarr) + 1 < maxidx) |
252b5132 RH |
6943 | iverdef->vd_nextdef = iverdef + 1; |
6944 | else | |
6945 | iverdef->vd_nextdef = NULL; | |
6946 | ||
6947 | everdef = ((Elf_External_Verdef *) | |
6948 | ((bfd_byte *) everdef + iverdef->vd_next)); | |
6949 | } | |
6950 | ||
6951 | free (contents); | |
6952 | contents = NULL; | |
6953 | } | |
fc0e6df6 | 6954 | else if (default_imported_symver) |
252b5132 | 6955 | { |
fc0e6df6 PB |
6956 | if (freeidx < 3) |
6957 | freeidx = 3; | |
6958 | else | |
6959 | freeidx++; | |
252b5132 | 6960 | |
d0fb9a8d JJ |
6961 | elf_tdata (abfd)->verdef = bfd_zalloc2 (abfd, freeidx, |
6962 | sizeof (Elf_Internal_Verdef)); | |
fc0e6df6 | 6963 | if (elf_tdata (abfd)->verdef == NULL) |
252b5132 RH |
6964 | goto error_return; |
6965 | ||
fc0e6df6 PB |
6966 | elf_tdata (abfd)->cverdefs = freeidx; |
6967 | } | |
252b5132 | 6968 | |
fc0e6df6 PB |
6969 | /* Create a default version based on the soname. */ |
6970 | if (default_imported_symver) | |
6971 | { | |
6972 | Elf_Internal_Verdef *iverdef; | |
6973 | Elf_Internal_Verdaux *iverdaux; | |
252b5132 | 6974 | |
fc0e6df6 | 6975 | iverdef = &elf_tdata (abfd)->verdef[freeidx - 1];; |
252b5132 | 6976 | |
fc0e6df6 PB |
6977 | iverdef->vd_version = VER_DEF_CURRENT; |
6978 | iverdef->vd_flags = 0; | |
6979 | iverdef->vd_ndx = freeidx; | |
6980 | iverdef->vd_cnt = 1; | |
252b5132 | 6981 | |
fc0e6df6 | 6982 | iverdef->vd_bfd = abfd; |
252b5132 | 6983 | |
fc0e6df6 PB |
6984 | iverdef->vd_nodename = bfd_elf_get_dt_soname (abfd); |
6985 | if (iverdef->vd_nodename == NULL) | |
d0fb9a8d | 6986 | goto error_return_verdef; |
fc0e6df6 | 6987 | iverdef->vd_nextdef = NULL; |
d0fb9a8d JJ |
6988 | iverdef->vd_auxptr = bfd_alloc (abfd, sizeof (Elf_Internal_Verdaux)); |
6989 | if (iverdef->vd_auxptr == NULL) | |
6990 | goto error_return_verdef; | |
252b5132 | 6991 | |
fc0e6df6 PB |
6992 | iverdaux = iverdef->vd_auxptr; |
6993 | iverdaux->vda_nodename = iverdef->vd_nodename; | |
6994 | iverdaux->vda_nextptr = NULL; | |
252b5132 RH |
6995 | } |
6996 | ||
b34976b6 | 6997 | return TRUE; |
252b5132 RH |
6998 | |
6999 | error_return: | |
5ed6aba4 | 7000 | if (contents != NULL) |
252b5132 | 7001 | free (contents); |
b34976b6 | 7002 | return FALSE; |
252b5132 RH |
7003 | } |
7004 | \f | |
7005 | asymbol * | |
217aa764 | 7006 | _bfd_elf_make_empty_symbol (bfd *abfd) |
252b5132 RH |
7007 | { |
7008 | elf_symbol_type *newsym; | |
dc810e39 | 7009 | bfd_size_type amt = sizeof (elf_symbol_type); |
252b5132 | 7010 | |
217aa764 | 7011 | newsym = bfd_zalloc (abfd, amt); |
252b5132 RH |
7012 | if (!newsym) |
7013 | return NULL; | |
7014 | else | |
7015 | { | |
7016 | newsym->symbol.the_bfd = abfd; | |
7017 | return &newsym->symbol; | |
7018 | } | |
7019 | } | |
7020 | ||
7021 | void | |
217aa764 AM |
7022 | _bfd_elf_get_symbol_info (bfd *abfd ATTRIBUTE_UNUSED, |
7023 | asymbol *symbol, | |
7024 | symbol_info *ret) | |
252b5132 RH |
7025 | { |
7026 | bfd_symbol_info (symbol, ret); | |
7027 | } | |
7028 | ||
7029 | /* Return whether a symbol name implies a local symbol. Most targets | |
7030 | use this function for the is_local_label_name entry point, but some | |
7031 | override it. */ | |
7032 | ||
b34976b6 | 7033 | bfd_boolean |
217aa764 AM |
7034 | _bfd_elf_is_local_label_name (bfd *abfd ATTRIBUTE_UNUSED, |
7035 | const char *name) | |
252b5132 RH |
7036 | { |
7037 | /* Normal local symbols start with ``.L''. */ | |
7038 | if (name[0] == '.' && name[1] == 'L') | |
b34976b6 | 7039 | return TRUE; |
252b5132 RH |
7040 | |
7041 | /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate | |
7042 | DWARF debugging symbols starting with ``..''. */ | |
7043 | if (name[0] == '.' && name[1] == '.') | |
b34976b6 | 7044 | return TRUE; |
252b5132 RH |
7045 | |
7046 | /* gcc will sometimes generate symbols beginning with ``_.L_'' when | |
7047 | emitting DWARF debugging output. I suspect this is actually a | |
7048 | small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call | |
7049 | ASM_GENERATE_INTERNAL_LABEL, and this causes the leading | |
7050 | underscore to be emitted on some ELF targets). For ease of use, | |
7051 | we treat such symbols as local. */ | |
7052 | if (name[0] == '_' && name[1] == '.' && name[2] == 'L' && name[3] == '_') | |
b34976b6 | 7053 | return TRUE; |
252b5132 | 7054 | |
b34976b6 | 7055 | return FALSE; |
252b5132 RH |
7056 | } |
7057 | ||
7058 | alent * | |
217aa764 AM |
7059 | _bfd_elf_get_lineno (bfd *abfd ATTRIBUTE_UNUSED, |
7060 | asymbol *symbol ATTRIBUTE_UNUSED) | |
252b5132 RH |
7061 | { |
7062 | abort (); | |
7063 | return NULL; | |
7064 | } | |
7065 | ||
b34976b6 | 7066 | bfd_boolean |
217aa764 AM |
7067 | _bfd_elf_set_arch_mach (bfd *abfd, |
7068 | enum bfd_architecture arch, | |
7069 | unsigned long machine) | |
252b5132 RH |
7070 | { |
7071 | /* If this isn't the right architecture for this backend, and this | |
7072 | isn't the generic backend, fail. */ | |
7073 | if (arch != get_elf_backend_data (abfd)->arch | |
7074 | && arch != bfd_arch_unknown | |
7075 | && get_elf_backend_data (abfd)->arch != bfd_arch_unknown) | |
b34976b6 | 7076 | return FALSE; |
252b5132 RH |
7077 | |
7078 | return bfd_default_set_arch_mach (abfd, arch, machine); | |
7079 | } | |
7080 | ||
d1fad7c6 NC |
7081 | /* Find the function to a particular section and offset, |
7082 | for error reporting. */ | |
252b5132 | 7083 | |
b34976b6 | 7084 | static bfd_boolean |
217aa764 AM |
7085 | elf_find_function (bfd *abfd ATTRIBUTE_UNUSED, |
7086 | asection *section, | |
7087 | asymbol **symbols, | |
7088 | bfd_vma offset, | |
7089 | const char **filename_ptr, | |
7090 | const char **functionname_ptr) | |
252b5132 | 7091 | { |
252b5132 | 7092 | const char *filename; |
57426232 | 7093 | asymbol *func, *file; |
252b5132 RH |
7094 | bfd_vma low_func; |
7095 | asymbol **p; | |
57426232 JB |
7096 | /* ??? Given multiple file symbols, it is impossible to reliably |
7097 | choose the right file name for global symbols. File symbols are | |
7098 | local symbols, and thus all file symbols must sort before any | |
7099 | global symbols. The ELF spec may be interpreted to say that a | |
7100 | file symbol must sort before other local symbols, but currently | |
7101 | ld -r doesn't do this. So, for ld -r output, it is possible to | |
7102 | make a better choice of file name for local symbols by ignoring | |
7103 | file symbols appearing after a given local symbol. */ | |
7104 | enum { nothing_seen, symbol_seen, file_after_symbol_seen } state; | |
252b5132 | 7105 | |
252b5132 RH |
7106 | filename = NULL; |
7107 | func = NULL; | |
57426232 | 7108 | file = NULL; |
252b5132 | 7109 | low_func = 0; |
57426232 | 7110 | state = nothing_seen; |
252b5132 RH |
7111 | |
7112 | for (p = symbols; *p != NULL; p++) | |
7113 | { | |
7114 | elf_symbol_type *q; | |
7115 | ||
7116 | q = (elf_symbol_type *) *p; | |
7117 | ||
252b5132 RH |
7118 | switch (ELF_ST_TYPE (q->internal_elf_sym.st_info)) |
7119 | { | |
7120 | default: | |
7121 | break; | |
7122 | case STT_FILE: | |
57426232 JB |
7123 | file = &q->symbol; |
7124 | if (state == symbol_seen) | |
7125 | state = file_after_symbol_seen; | |
7126 | continue; | |
252b5132 RH |
7127 | case STT_NOTYPE: |
7128 | case STT_FUNC: | |
6b40fcba | 7129 | if (bfd_get_section (&q->symbol) == section |
252b5132 RH |
7130 | && q->symbol.value >= low_func |
7131 | && q->symbol.value <= offset) | |
7132 | { | |
7133 | func = (asymbol *) q; | |
7134 | low_func = q->symbol.value; | |
a1923858 AM |
7135 | filename = NULL; |
7136 | if (file != NULL | |
7137 | && (ELF_ST_BIND (q->internal_elf_sym.st_info) == STB_LOCAL | |
7138 | || state != file_after_symbol_seen)) | |
57426232 | 7139 | filename = bfd_asymbol_name (file); |
252b5132 RH |
7140 | } |
7141 | break; | |
7142 | } | |
57426232 JB |
7143 | if (state == nothing_seen) |
7144 | state = symbol_seen; | |
252b5132 RH |
7145 | } |
7146 | ||
7147 | if (func == NULL) | |
b34976b6 | 7148 | return FALSE; |
252b5132 | 7149 | |
d1fad7c6 NC |
7150 | if (filename_ptr) |
7151 | *filename_ptr = filename; | |
7152 | if (functionname_ptr) | |
7153 | *functionname_ptr = bfd_asymbol_name (func); | |
7154 | ||
b34976b6 | 7155 | return TRUE; |
d1fad7c6 NC |
7156 | } |
7157 | ||
7158 | /* Find the nearest line to a particular section and offset, | |
7159 | for error reporting. */ | |
7160 | ||
b34976b6 | 7161 | bfd_boolean |
217aa764 AM |
7162 | _bfd_elf_find_nearest_line (bfd *abfd, |
7163 | asection *section, | |
7164 | asymbol **symbols, | |
7165 | bfd_vma offset, | |
7166 | const char **filename_ptr, | |
7167 | const char **functionname_ptr, | |
7168 | unsigned int *line_ptr) | |
d1fad7c6 | 7169 | { |
b34976b6 | 7170 | bfd_boolean found; |
d1fad7c6 NC |
7171 | |
7172 | if (_bfd_dwarf1_find_nearest_line (abfd, section, symbols, offset, | |
4e8a9624 AM |
7173 | filename_ptr, functionname_ptr, |
7174 | line_ptr)) | |
d1fad7c6 NC |
7175 | { |
7176 | if (!*functionname_ptr) | |
4e8a9624 AM |
7177 | elf_find_function (abfd, section, symbols, offset, |
7178 | *filename_ptr ? NULL : filename_ptr, | |
7179 | functionname_ptr); | |
7180 | ||
b34976b6 | 7181 | return TRUE; |
d1fad7c6 NC |
7182 | } |
7183 | ||
7184 | if (_bfd_dwarf2_find_nearest_line (abfd, section, symbols, offset, | |
4e8a9624 AM |
7185 | filename_ptr, functionname_ptr, |
7186 | line_ptr, 0, | |
7187 | &elf_tdata (abfd)->dwarf2_find_line_info)) | |
d1fad7c6 NC |
7188 | { |
7189 | if (!*functionname_ptr) | |
4e8a9624 AM |
7190 | elf_find_function (abfd, section, symbols, offset, |
7191 | *filename_ptr ? NULL : filename_ptr, | |
7192 | functionname_ptr); | |
7193 | ||
b34976b6 | 7194 | return TRUE; |
d1fad7c6 NC |
7195 | } |
7196 | ||
7197 | if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset, | |
4e8a9624 AM |
7198 | &found, filename_ptr, |
7199 | functionname_ptr, line_ptr, | |
7200 | &elf_tdata (abfd)->line_info)) | |
b34976b6 | 7201 | return FALSE; |
dc43ada5 | 7202 | if (found && (*functionname_ptr || *line_ptr)) |
b34976b6 | 7203 | return TRUE; |
d1fad7c6 NC |
7204 | |
7205 | if (symbols == NULL) | |
b34976b6 | 7206 | return FALSE; |
d1fad7c6 NC |
7207 | |
7208 | if (! elf_find_function (abfd, section, symbols, offset, | |
4e8a9624 | 7209 | filename_ptr, functionname_ptr)) |
b34976b6 | 7210 | return FALSE; |
d1fad7c6 | 7211 | |
252b5132 | 7212 | *line_ptr = 0; |
b34976b6 | 7213 | return TRUE; |
252b5132 RH |
7214 | } |
7215 | ||
5420f73d L |
7216 | /* Find the line for a symbol. */ |
7217 | ||
7218 | bfd_boolean | |
7219 | _bfd_elf_find_line (bfd *abfd, asymbol **symbols, asymbol *symbol, | |
7220 | const char **filename_ptr, unsigned int *line_ptr) | |
7221 | { | |
7222 | return _bfd_dwarf2_find_line (abfd, symbols, symbol, | |
7223 | filename_ptr, line_ptr, 0, | |
7224 | &elf_tdata (abfd)->dwarf2_find_line_info); | |
7225 | } | |
7226 | ||
4ab527b0 FF |
7227 | /* After a call to bfd_find_nearest_line, successive calls to |
7228 | bfd_find_inliner_info can be used to get source information about | |
7229 | each level of function inlining that terminated at the address | |
7230 | passed to bfd_find_nearest_line. Currently this is only supported | |
7231 | for DWARF2 with appropriate DWARF3 extensions. */ | |
7232 | ||
7233 | bfd_boolean | |
7234 | _bfd_elf_find_inliner_info (bfd *abfd, | |
7235 | const char **filename_ptr, | |
7236 | const char **functionname_ptr, | |
7237 | unsigned int *line_ptr) | |
7238 | { | |
7239 | bfd_boolean found; | |
7240 | found = _bfd_dwarf2_find_inliner_info (abfd, filename_ptr, | |
7241 | functionname_ptr, line_ptr, | |
7242 | & elf_tdata (abfd)->dwarf2_find_line_info); | |
7243 | return found; | |
7244 | } | |
7245 | ||
252b5132 | 7246 | int |
a6b96beb | 7247 | _bfd_elf_sizeof_headers (bfd *abfd, struct bfd_link_info *info) |
252b5132 | 7248 | { |
8ded5a0f AM |
7249 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
7250 | int ret = bed->s->sizeof_ehdr; | |
252b5132 | 7251 | |
a6b96beb | 7252 | if (!info->relocatable) |
8ded5a0f | 7253 | { |
62d7a5f6 | 7254 | bfd_size_type phdr_size = elf_tdata (abfd)->program_header_size; |
8ded5a0f | 7255 | |
62d7a5f6 AM |
7256 | if (phdr_size == (bfd_size_type) -1) |
7257 | { | |
7258 | struct elf_segment_map *m; | |
7259 | ||
7260 | phdr_size = 0; | |
7261 | for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next) | |
7262 | phdr_size += bed->s->sizeof_phdr; | |
8ded5a0f | 7263 | |
62d7a5f6 AM |
7264 | if (phdr_size == 0) |
7265 | phdr_size = get_program_header_size (abfd, info); | |
7266 | } | |
8ded5a0f AM |
7267 | |
7268 | elf_tdata (abfd)->program_header_size = phdr_size; | |
7269 | ret += phdr_size; | |
7270 | } | |
7271 | ||
252b5132 RH |
7272 | return ret; |
7273 | } | |
7274 | ||
b34976b6 | 7275 | bfd_boolean |
217aa764 AM |
7276 | _bfd_elf_set_section_contents (bfd *abfd, |
7277 | sec_ptr section, | |
0f867abe | 7278 | const void *location, |
217aa764 AM |
7279 | file_ptr offset, |
7280 | bfd_size_type count) | |
252b5132 RH |
7281 | { |
7282 | Elf_Internal_Shdr *hdr; | |
dc810e39 | 7283 | bfd_signed_vma pos; |
252b5132 RH |
7284 | |
7285 | if (! abfd->output_has_begun | |
217aa764 | 7286 | && ! _bfd_elf_compute_section_file_positions (abfd, NULL)) |
b34976b6 | 7287 | return FALSE; |
252b5132 RH |
7288 | |
7289 | hdr = &elf_section_data (section)->this_hdr; | |
dc810e39 AM |
7290 | pos = hdr->sh_offset + offset; |
7291 | if (bfd_seek (abfd, pos, SEEK_SET) != 0 | |
7292 | || bfd_bwrite (location, count, abfd) != count) | |
b34976b6 | 7293 | return FALSE; |
252b5132 | 7294 | |
b34976b6 | 7295 | return TRUE; |
252b5132 RH |
7296 | } |
7297 | ||
7298 | void | |
217aa764 AM |
7299 | _bfd_elf_no_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED, |
7300 | arelent *cache_ptr ATTRIBUTE_UNUSED, | |
7301 | Elf_Internal_Rela *dst ATTRIBUTE_UNUSED) | |
252b5132 RH |
7302 | { |
7303 | abort (); | |
7304 | } | |
7305 | ||
252b5132 RH |
7306 | /* Try to convert a non-ELF reloc into an ELF one. */ |
7307 | ||
b34976b6 | 7308 | bfd_boolean |
217aa764 | 7309 | _bfd_elf_validate_reloc (bfd *abfd, arelent *areloc) |
252b5132 | 7310 | { |
c044fabd | 7311 | /* Check whether we really have an ELF howto. */ |
252b5132 RH |
7312 | |
7313 | if ((*areloc->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec) | |
7314 | { | |
7315 | bfd_reloc_code_real_type code; | |
7316 | reloc_howto_type *howto; | |
7317 | ||
7318 | /* Alien reloc: Try to determine its type to replace it with an | |
c044fabd | 7319 | equivalent ELF reloc. */ |
252b5132 RH |
7320 | |
7321 | if (areloc->howto->pc_relative) | |
7322 | { | |
7323 | switch (areloc->howto->bitsize) | |
7324 | { | |
7325 | case 8: | |
7326 | code = BFD_RELOC_8_PCREL; | |
7327 | break; | |
7328 | case 12: | |
7329 | code = BFD_RELOC_12_PCREL; | |
7330 | break; | |
7331 | case 16: | |
7332 | code = BFD_RELOC_16_PCREL; | |
7333 | break; | |
7334 | case 24: | |
7335 | code = BFD_RELOC_24_PCREL; | |
7336 | break; | |
7337 | case 32: | |
7338 | code = BFD_RELOC_32_PCREL; | |
7339 | break; | |
7340 | case 64: | |
7341 | code = BFD_RELOC_64_PCREL; | |
7342 | break; | |
7343 | default: | |
7344 | goto fail; | |
7345 | } | |
7346 | ||
7347 | howto = bfd_reloc_type_lookup (abfd, code); | |
7348 | ||
7349 | if (areloc->howto->pcrel_offset != howto->pcrel_offset) | |
7350 | { | |
7351 | if (howto->pcrel_offset) | |
7352 | areloc->addend += areloc->address; | |
7353 | else | |
7354 | areloc->addend -= areloc->address; /* addend is unsigned!! */ | |
7355 | } | |
7356 | } | |
7357 | else | |
7358 | { | |
7359 | switch (areloc->howto->bitsize) | |
7360 | { | |
7361 | case 8: | |
7362 | code = BFD_RELOC_8; | |
7363 | break; | |
7364 | case 14: | |
7365 | code = BFD_RELOC_14; | |
7366 | break; | |
7367 | case 16: | |
7368 | code = BFD_RELOC_16; | |
7369 | break; | |
7370 | case 26: | |
7371 | code = BFD_RELOC_26; | |
7372 | break; | |
7373 | case 32: | |
7374 | code = BFD_RELOC_32; | |
7375 | break; | |
7376 | case 64: | |
7377 | code = BFD_RELOC_64; | |
7378 | break; | |
7379 | default: | |
7380 | goto fail; | |
7381 | } | |
7382 | ||
7383 | howto = bfd_reloc_type_lookup (abfd, code); | |
7384 | } | |
7385 | ||
7386 | if (howto) | |
7387 | areloc->howto = howto; | |
7388 | else | |
7389 | goto fail; | |
7390 | } | |
7391 | ||
b34976b6 | 7392 | return TRUE; |
252b5132 RH |
7393 | |
7394 | fail: | |
7395 | (*_bfd_error_handler) | |
d003868e AM |
7396 | (_("%B: unsupported relocation type %s"), |
7397 | abfd, areloc->howto->name); | |
252b5132 | 7398 | bfd_set_error (bfd_error_bad_value); |
b34976b6 | 7399 | return FALSE; |
252b5132 RH |
7400 | } |
7401 | ||
b34976b6 | 7402 | bfd_boolean |
217aa764 | 7403 | _bfd_elf_close_and_cleanup (bfd *abfd) |
252b5132 RH |
7404 | { |
7405 | if (bfd_get_format (abfd) == bfd_object) | |
7406 | { | |
b25e3d87 | 7407 | if (elf_tdata (abfd) != NULL && elf_shstrtab (abfd) != NULL) |
2b0f7ef9 | 7408 | _bfd_elf_strtab_free (elf_shstrtab (abfd)); |
6f140a15 | 7409 | _bfd_dwarf2_cleanup_debug_info (abfd); |
252b5132 RH |
7410 | } |
7411 | ||
7412 | return _bfd_generic_close_and_cleanup (abfd); | |
7413 | } | |
7414 | ||
7415 | /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY | |
7416 | in the relocation's offset. Thus we cannot allow any sort of sanity | |
7417 | range-checking to interfere. There is nothing else to do in processing | |
7418 | this reloc. */ | |
7419 | ||
7420 | bfd_reloc_status_type | |
217aa764 AM |
7421 | _bfd_elf_rel_vtable_reloc_fn |
7422 | (bfd *abfd ATTRIBUTE_UNUSED, arelent *re ATTRIBUTE_UNUSED, | |
fc0a2244 | 7423 | struct bfd_symbol *symbol ATTRIBUTE_UNUSED, |
217aa764 AM |
7424 | void *data ATTRIBUTE_UNUSED, asection *is ATTRIBUTE_UNUSED, |
7425 | bfd *obfd ATTRIBUTE_UNUSED, char **errmsg ATTRIBUTE_UNUSED) | |
252b5132 RH |
7426 | { |
7427 | return bfd_reloc_ok; | |
7428 | } | |
252b5132 RH |
7429 | \f |
7430 | /* Elf core file support. Much of this only works on native | |
7431 | toolchains, since we rely on knowing the | |
7432 | machine-dependent procfs structure in order to pick | |
c044fabd | 7433 | out details about the corefile. */ |
252b5132 RH |
7434 | |
7435 | #ifdef HAVE_SYS_PROCFS_H | |
7436 | # include <sys/procfs.h> | |
7437 | #endif | |
7438 | ||
c044fabd | 7439 | /* FIXME: this is kinda wrong, but it's what gdb wants. */ |
252b5132 RH |
7440 | |
7441 | static int | |
217aa764 | 7442 | elfcore_make_pid (bfd *abfd) |
252b5132 RH |
7443 | { |
7444 | return ((elf_tdata (abfd)->core_lwpid << 16) | |
7445 | + (elf_tdata (abfd)->core_pid)); | |
7446 | } | |
7447 | ||
252b5132 RH |
7448 | /* If there isn't a section called NAME, make one, using |
7449 | data from SECT. Note, this function will generate a | |
7450 | reference to NAME, so you shouldn't deallocate or | |
c044fabd | 7451 | overwrite it. */ |
252b5132 | 7452 | |
b34976b6 | 7453 | static bfd_boolean |
217aa764 | 7454 | elfcore_maybe_make_sect (bfd *abfd, char *name, asection *sect) |
252b5132 | 7455 | { |
c044fabd | 7456 | asection *sect2; |
252b5132 RH |
7457 | |
7458 | if (bfd_get_section_by_name (abfd, name) != NULL) | |
b34976b6 | 7459 | return TRUE; |
252b5132 | 7460 | |
117ed4f8 | 7461 | sect2 = bfd_make_section_with_flags (abfd, name, sect->flags); |
252b5132 | 7462 | if (sect2 == NULL) |
b34976b6 | 7463 | return FALSE; |
252b5132 | 7464 | |
eea6121a | 7465 | sect2->size = sect->size; |
252b5132 | 7466 | sect2->filepos = sect->filepos; |
252b5132 | 7467 | sect2->alignment_power = sect->alignment_power; |
b34976b6 | 7468 | return TRUE; |
252b5132 RH |
7469 | } |
7470 | ||
bb0082d6 AM |
7471 | /* Create a pseudosection containing SIZE bytes at FILEPOS. This |
7472 | actually creates up to two pseudosections: | |
7473 | - For the single-threaded case, a section named NAME, unless | |
7474 | such a section already exists. | |
7475 | - For the multi-threaded case, a section named "NAME/PID", where | |
7476 | PID is elfcore_make_pid (abfd). | |
7477 | Both pseudosections have identical contents. */ | |
b34976b6 | 7478 | bfd_boolean |
217aa764 AM |
7479 | _bfd_elfcore_make_pseudosection (bfd *abfd, |
7480 | char *name, | |
7481 | size_t size, | |
7482 | ufile_ptr filepos) | |
bb0082d6 AM |
7483 | { |
7484 | char buf[100]; | |
7485 | char *threaded_name; | |
d4c88bbb | 7486 | size_t len; |
bb0082d6 AM |
7487 | asection *sect; |
7488 | ||
7489 | /* Build the section name. */ | |
7490 | ||
7491 | sprintf (buf, "%s/%d", name, elfcore_make_pid (abfd)); | |
d4c88bbb | 7492 | len = strlen (buf) + 1; |
217aa764 | 7493 | threaded_name = bfd_alloc (abfd, len); |
bb0082d6 | 7494 | if (threaded_name == NULL) |
b34976b6 | 7495 | return FALSE; |
d4c88bbb | 7496 | memcpy (threaded_name, buf, len); |
bb0082d6 | 7497 | |
117ed4f8 AM |
7498 | sect = bfd_make_section_anyway_with_flags (abfd, threaded_name, |
7499 | SEC_HAS_CONTENTS); | |
bb0082d6 | 7500 | if (sect == NULL) |
b34976b6 | 7501 | return FALSE; |
eea6121a | 7502 | sect->size = size; |
bb0082d6 | 7503 | sect->filepos = filepos; |
bb0082d6 AM |
7504 | sect->alignment_power = 2; |
7505 | ||
936e320b | 7506 | return elfcore_maybe_make_sect (abfd, name, sect); |
bb0082d6 AM |
7507 | } |
7508 | ||
252b5132 | 7509 | /* prstatus_t exists on: |
4a938328 | 7510 | solaris 2.5+ |
252b5132 RH |
7511 | linux 2.[01] + glibc |
7512 | unixware 4.2 | |
7513 | */ | |
7514 | ||
7515 | #if defined (HAVE_PRSTATUS_T) | |
a7b97311 | 7516 | |
b34976b6 | 7517 | static bfd_boolean |
217aa764 | 7518 | elfcore_grok_prstatus (bfd *abfd, Elf_Internal_Note *note) |
252b5132 | 7519 | { |
eea6121a | 7520 | size_t size; |
7ee38065 | 7521 | int offset; |
252b5132 | 7522 | |
4a938328 MS |
7523 | if (note->descsz == sizeof (prstatus_t)) |
7524 | { | |
7525 | prstatus_t prstat; | |
252b5132 | 7526 | |
eea6121a | 7527 | size = sizeof (prstat.pr_reg); |
7ee38065 | 7528 | offset = offsetof (prstatus_t, pr_reg); |
4a938328 | 7529 | memcpy (&prstat, note->descdata, sizeof (prstat)); |
252b5132 | 7530 | |
fa49d224 NC |
7531 | /* Do not overwrite the core signal if it |
7532 | has already been set by another thread. */ | |
7533 | if (elf_tdata (abfd)->core_signal == 0) | |
7534 | elf_tdata (abfd)->core_signal = prstat.pr_cursig; | |
4a938328 | 7535 | elf_tdata (abfd)->core_pid = prstat.pr_pid; |
252b5132 | 7536 | |
4a938328 MS |
7537 | /* pr_who exists on: |
7538 | solaris 2.5+ | |
7539 | unixware 4.2 | |
7540 | pr_who doesn't exist on: | |
7541 | linux 2.[01] | |
7542 | */ | |
252b5132 | 7543 | #if defined (HAVE_PRSTATUS_T_PR_WHO) |
4a938328 | 7544 | elf_tdata (abfd)->core_lwpid = prstat.pr_who; |
252b5132 | 7545 | #endif |
4a938328 | 7546 | } |
7ee38065 | 7547 | #if defined (HAVE_PRSTATUS32_T) |
4a938328 MS |
7548 | else if (note->descsz == sizeof (prstatus32_t)) |
7549 | { | |
7550 | /* 64-bit host, 32-bit corefile */ | |
7551 | prstatus32_t prstat; | |
7552 | ||
eea6121a | 7553 | size = sizeof (prstat.pr_reg); |
7ee38065 | 7554 | offset = offsetof (prstatus32_t, pr_reg); |
4a938328 MS |
7555 | memcpy (&prstat, note->descdata, sizeof (prstat)); |
7556 | ||
fa49d224 NC |
7557 | /* Do not overwrite the core signal if it |
7558 | has already been set by another thread. */ | |
7559 | if (elf_tdata (abfd)->core_signal == 0) | |
7560 | elf_tdata (abfd)->core_signal = prstat.pr_cursig; | |
4a938328 MS |
7561 | elf_tdata (abfd)->core_pid = prstat.pr_pid; |
7562 | ||
7563 | /* pr_who exists on: | |
7564 | solaris 2.5+ | |
7565 | unixware 4.2 | |
7566 | pr_who doesn't exist on: | |
7567 | linux 2.[01] | |
7568 | */ | |
7ee38065 | 7569 | #if defined (HAVE_PRSTATUS32_T_PR_WHO) |
4a938328 MS |
7570 | elf_tdata (abfd)->core_lwpid = prstat.pr_who; |
7571 | #endif | |
7572 | } | |
7ee38065 | 7573 | #endif /* HAVE_PRSTATUS32_T */ |
4a938328 MS |
7574 | else |
7575 | { | |
7576 | /* Fail - we don't know how to handle any other | |
7577 | note size (ie. data object type). */ | |
b34976b6 | 7578 | return TRUE; |
4a938328 | 7579 | } |
252b5132 | 7580 | |
bb0082d6 | 7581 | /* Make a ".reg/999" section and a ".reg" section. */ |
936e320b | 7582 | return _bfd_elfcore_make_pseudosection (abfd, ".reg", |
eea6121a | 7583 | size, note->descpos + offset); |
252b5132 RH |
7584 | } |
7585 | #endif /* defined (HAVE_PRSTATUS_T) */ | |
7586 | ||
bb0082d6 | 7587 | /* Create a pseudosection containing the exact contents of NOTE. */ |
b34976b6 | 7588 | static bfd_boolean |
217aa764 AM |
7589 | elfcore_make_note_pseudosection (bfd *abfd, |
7590 | char *name, | |
7591 | Elf_Internal_Note *note) | |
252b5132 | 7592 | { |
936e320b AM |
7593 | return _bfd_elfcore_make_pseudosection (abfd, name, |
7594 | note->descsz, note->descpos); | |
252b5132 RH |
7595 | } |
7596 | ||
ff08c6bb JB |
7597 | /* There isn't a consistent prfpregset_t across platforms, |
7598 | but it doesn't matter, because we don't have to pick this | |
c044fabd KH |
7599 | data structure apart. */ |
7600 | ||
b34976b6 | 7601 | static bfd_boolean |
217aa764 | 7602 | elfcore_grok_prfpreg (bfd *abfd, Elf_Internal_Note *note) |
ff08c6bb JB |
7603 | { |
7604 | return elfcore_make_note_pseudosection (abfd, ".reg2", note); | |
7605 | } | |
7606 | ||
ff08c6bb JB |
7607 | /* Linux dumps the Intel SSE regs in a note named "LINUX" with a note |
7608 | type of 5 (NT_PRXFPREG). Just include the whole note's contents | |
7609 | literally. */ | |
c044fabd | 7610 | |
b34976b6 | 7611 | static bfd_boolean |
217aa764 | 7612 | elfcore_grok_prxfpreg (bfd *abfd, Elf_Internal_Note *note) |
ff08c6bb JB |
7613 | { |
7614 | return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note); | |
7615 | } | |
7616 | ||
252b5132 | 7617 | #if defined (HAVE_PRPSINFO_T) |
4a938328 | 7618 | typedef prpsinfo_t elfcore_psinfo_t; |
7ee38065 | 7619 | #if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */ |
4a938328 MS |
7620 | typedef prpsinfo32_t elfcore_psinfo32_t; |
7621 | #endif | |
252b5132 RH |
7622 | #endif |
7623 | ||
7624 | #if defined (HAVE_PSINFO_T) | |
4a938328 | 7625 | typedef psinfo_t elfcore_psinfo_t; |
7ee38065 | 7626 | #if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */ |
4a938328 MS |
7627 | typedef psinfo32_t elfcore_psinfo32_t; |
7628 | #endif | |
252b5132 RH |
7629 | #endif |
7630 | ||
252b5132 RH |
7631 | /* return a malloc'ed copy of a string at START which is at |
7632 | most MAX bytes long, possibly without a terminating '\0'. | |
c044fabd | 7633 | the copy will always have a terminating '\0'. */ |
252b5132 | 7634 | |
936e320b | 7635 | char * |
217aa764 | 7636 | _bfd_elfcore_strndup (bfd *abfd, char *start, size_t max) |
252b5132 | 7637 | { |
dc810e39 | 7638 | char *dups; |
c044fabd | 7639 | char *end = memchr (start, '\0', max); |
dc810e39 | 7640 | size_t len; |
252b5132 RH |
7641 | |
7642 | if (end == NULL) | |
7643 | len = max; | |
7644 | else | |
7645 | len = end - start; | |
7646 | ||
217aa764 | 7647 | dups = bfd_alloc (abfd, len + 1); |
dc810e39 | 7648 | if (dups == NULL) |
252b5132 RH |
7649 | return NULL; |
7650 | ||
dc810e39 AM |
7651 | memcpy (dups, start, len); |
7652 | dups[len] = '\0'; | |
252b5132 | 7653 | |
dc810e39 | 7654 | return dups; |
252b5132 RH |
7655 | } |
7656 | ||
bb0082d6 | 7657 | #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) |
b34976b6 | 7658 | static bfd_boolean |
217aa764 | 7659 | elfcore_grok_psinfo (bfd *abfd, Elf_Internal_Note *note) |
252b5132 | 7660 | { |
4a938328 MS |
7661 | if (note->descsz == sizeof (elfcore_psinfo_t)) |
7662 | { | |
7663 | elfcore_psinfo_t psinfo; | |
252b5132 | 7664 | |
7ee38065 | 7665 | memcpy (&psinfo, note->descdata, sizeof (psinfo)); |
252b5132 | 7666 | |
4a938328 | 7667 | elf_tdata (abfd)->core_program |
936e320b AM |
7668 | = _bfd_elfcore_strndup (abfd, psinfo.pr_fname, |
7669 | sizeof (psinfo.pr_fname)); | |
252b5132 | 7670 | |
4a938328 | 7671 | elf_tdata (abfd)->core_command |
936e320b AM |
7672 | = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs, |
7673 | sizeof (psinfo.pr_psargs)); | |
4a938328 | 7674 | } |
7ee38065 | 7675 | #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T) |
4a938328 MS |
7676 | else if (note->descsz == sizeof (elfcore_psinfo32_t)) |
7677 | { | |
7678 | /* 64-bit host, 32-bit corefile */ | |
7679 | elfcore_psinfo32_t psinfo; | |
7680 | ||
7ee38065 | 7681 | memcpy (&psinfo, note->descdata, sizeof (psinfo)); |
252b5132 | 7682 | |
4a938328 | 7683 | elf_tdata (abfd)->core_program |
936e320b AM |
7684 | = _bfd_elfcore_strndup (abfd, psinfo.pr_fname, |
7685 | sizeof (psinfo.pr_fname)); | |
4a938328 MS |
7686 | |
7687 | elf_tdata (abfd)->core_command | |
936e320b AM |
7688 | = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs, |
7689 | sizeof (psinfo.pr_psargs)); | |
4a938328 MS |
7690 | } |
7691 | #endif | |
7692 | ||
7693 | else | |
7694 | { | |
7695 | /* Fail - we don't know how to handle any other | |
7696 | note size (ie. data object type). */ | |
b34976b6 | 7697 | return TRUE; |
4a938328 | 7698 | } |
252b5132 RH |
7699 | |
7700 | /* Note that for some reason, a spurious space is tacked | |
7701 | onto the end of the args in some (at least one anyway) | |
c044fabd | 7702 | implementations, so strip it off if it exists. */ |
252b5132 RH |
7703 | |
7704 | { | |
c044fabd | 7705 | char *command = elf_tdata (abfd)->core_command; |
252b5132 RH |
7706 | int n = strlen (command); |
7707 | ||
7708 | if (0 < n && command[n - 1] == ' ') | |
7709 | command[n - 1] = '\0'; | |
7710 | } | |
7711 | ||
b34976b6 | 7712 | return TRUE; |
252b5132 RH |
7713 | } |
7714 | #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */ | |
7715 | ||
252b5132 | 7716 | #if defined (HAVE_PSTATUS_T) |
b34976b6 | 7717 | static bfd_boolean |
217aa764 | 7718 | elfcore_grok_pstatus (bfd *abfd, Elf_Internal_Note *note) |
252b5132 | 7719 | { |
f572a39d AM |
7720 | if (note->descsz == sizeof (pstatus_t) |
7721 | #if defined (HAVE_PXSTATUS_T) | |
7722 | || note->descsz == sizeof (pxstatus_t) | |
7723 | #endif | |
7724 | ) | |
4a938328 MS |
7725 | { |
7726 | pstatus_t pstat; | |
252b5132 | 7727 | |
4a938328 | 7728 | memcpy (&pstat, note->descdata, sizeof (pstat)); |
252b5132 | 7729 | |
4a938328 MS |
7730 | elf_tdata (abfd)->core_pid = pstat.pr_pid; |
7731 | } | |
7ee38065 | 7732 | #if defined (HAVE_PSTATUS32_T) |
4a938328 MS |
7733 | else if (note->descsz == sizeof (pstatus32_t)) |
7734 | { | |
7735 | /* 64-bit host, 32-bit corefile */ | |
7736 | pstatus32_t pstat; | |
252b5132 | 7737 | |
4a938328 | 7738 | memcpy (&pstat, note->descdata, sizeof (pstat)); |
252b5132 | 7739 | |
4a938328 MS |
7740 | elf_tdata (abfd)->core_pid = pstat.pr_pid; |
7741 | } | |
7742 | #endif | |
252b5132 RH |
7743 | /* Could grab some more details from the "representative" |
7744 | lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an | |
c044fabd | 7745 | NT_LWPSTATUS note, presumably. */ |
252b5132 | 7746 | |
b34976b6 | 7747 | return TRUE; |
252b5132 RH |
7748 | } |
7749 | #endif /* defined (HAVE_PSTATUS_T) */ | |
7750 | ||
252b5132 | 7751 | #if defined (HAVE_LWPSTATUS_T) |
b34976b6 | 7752 | static bfd_boolean |
217aa764 | 7753 | elfcore_grok_lwpstatus (bfd *abfd, Elf_Internal_Note *note) |
252b5132 RH |
7754 | { |
7755 | lwpstatus_t lwpstat; | |
7756 | char buf[100]; | |
c044fabd | 7757 | char *name; |
d4c88bbb | 7758 | size_t len; |
c044fabd | 7759 | asection *sect; |
252b5132 | 7760 | |
f572a39d AM |
7761 | if (note->descsz != sizeof (lwpstat) |
7762 | #if defined (HAVE_LWPXSTATUS_T) | |
7763 | && note->descsz != sizeof (lwpxstatus_t) | |
7764 | #endif | |
7765 | ) | |
b34976b6 | 7766 | return TRUE; |
252b5132 RH |
7767 | |
7768 | memcpy (&lwpstat, note->descdata, sizeof (lwpstat)); | |
7769 | ||
7770 | elf_tdata (abfd)->core_lwpid = lwpstat.pr_lwpid; | |
7771 | elf_tdata (abfd)->core_signal = lwpstat.pr_cursig; | |
7772 | ||
c044fabd | 7773 | /* Make a ".reg/999" section. */ |
252b5132 RH |
7774 | |
7775 | sprintf (buf, ".reg/%d", elfcore_make_pid (abfd)); | |
d4c88bbb | 7776 | len = strlen (buf) + 1; |
217aa764 | 7777 | name = bfd_alloc (abfd, len); |
252b5132 | 7778 | if (name == NULL) |
b34976b6 | 7779 | return FALSE; |
d4c88bbb | 7780 | memcpy (name, buf, len); |
252b5132 | 7781 | |
117ed4f8 | 7782 | sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); |
252b5132 | 7783 | if (sect == NULL) |
b34976b6 | 7784 | return FALSE; |
252b5132 RH |
7785 | |
7786 | #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT) | |
eea6121a | 7787 | sect->size = sizeof (lwpstat.pr_context.uc_mcontext.gregs); |
252b5132 RH |
7788 | sect->filepos = note->descpos |
7789 | + offsetof (lwpstatus_t, pr_context.uc_mcontext.gregs); | |
7790 | #endif | |
7791 | ||
7792 | #if defined (HAVE_LWPSTATUS_T_PR_REG) | |
eea6121a | 7793 | sect->size = sizeof (lwpstat.pr_reg); |
252b5132 RH |
7794 | sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_reg); |
7795 | #endif | |
7796 | ||
252b5132 RH |
7797 | sect->alignment_power = 2; |
7798 | ||
7799 | if (!elfcore_maybe_make_sect (abfd, ".reg", sect)) | |
b34976b6 | 7800 | return FALSE; |
252b5132 RH |
7801 | |
7802 | /* Make a ".reg2/999" section */ | |
7803 | ||
7804 | sprintf (buf, ".reg2/%d", elfcore_make_pid (abfd)); | |
d4c88bbb | 7805 | len = strlen (buf) + 1; |
217aa764 | 7806 | name = bfd_alloc (abfd, len); |
252b5132 | 7807 | if (name == NULL) |
b34976b6 | 7808 | return FALSE; |
d4c88bbb | 7809 | memcpy (name, buf, len); |
252b5132 | 7810 | |
117ed4f8 | 7811 | sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); |
252b5132 | 7812 | if (sect == NULL) |
b34976b6 | 7813 | return FALSE; |
252b5132 RH |
7814 | |
7815 | #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT) | |
eea6121a | 7816 | sect->size = sizeof (lwpstat.pr_context.uc_mcontext.fpregs); |
252b5132 RH |
7817 | sect->filepos = note->descpos |
7818 | + offsetof (lwpstatus_t, pr_context.uc_mcontext.fpregs); | |
7819 | #endif | |
7820 | ||
7821 | #if defined (HAVE_LWPSTATUS_T_PR_FPREG) | |
eea6121a | 7822 | sect->size = sizeof (lwpstat.pr_fpreg); |
252b5132 RH |
7823 | sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_fpreg); |
7824 | #endif | |
7825 | ||
252b5132 RH |
7826 | sect->alignment_power = 2; |
7827 | ||
936e320b | 7828 | return elfcore_maybe_make_sect (abfd, ".reg2", sect); |
252b5132 RH |
7829 | } |
7830 | #endif /* defined (HAVE_LWPSTATUS_T) */ | |
7831 | ||
16e9c715 | 7832 | #if defined (HAVE_WIN32_PSTATUS_T) |
b34976b6 | 7833 | static bfd_boolean |
217aa764 | 7834 | elfcore_grok_win32pstatus (bfd *abfd, Elf_Internal_Note *note) |
16e9c715 NC |
7835 | { |
7836 | char buf[30]; | |
c044fabd | 7837 | char *name; |
d4c88bbb | 7838 | size_t len; |
c044fabd | 7839 | asection *sect; |
16e9c715 NC |
7840 | win32_pstatus_t pstatus; |
7841 | ||
7842 | if (note->descsz < sizeof (pstatus)) | |
b34976b6 | 7843 | return TRUE; |
16e9c715 | 7844 | |
e8eab623 | 7845 | memcpy (&pstatus, note->descdata, sizeof (pstatus)); |
c044fabd KH |
7846 | |
7847 | switch (pstatus.data_type) | |
16e9c715 NC |
7848 | { |
7849 | case NOTE_INFO_PROCESS: | |
7850 | /* FIXME: need to add ->core_command. */ | |
7851 | elf_tdata (abfd)->core_signal = pstatus.data.process_info.signal; | |
7852 | elf_tdata (abfd)->core_pid = pstatus.data.process_info.pid; | |
c044fabd | 7853 | break; |
16e9c715 NC |
7854 | |
7855 | case NOTE_INFO_THREAD: | |
7856 | /* Make a ".reg/999" section. */ | |
1f170678 | 7857 | sprintf (buf, ".reg/%ld", (long) pstatus.data.thread_info.tid); |
c044fabd | 7858 | |
d4c88bbb | 7859 | len = strlen (buf) + 1; |
217aa764 | 7860 | name = bfd_alloc (abfd, len); |
16e9c715 | 7861 | if (name == NULL) |
b34976b6 | 7862 | return FALSE; |
c044fabd | 7863 | |
d4c88bbb | 7864 | memcpy (name, buf, len); |
16e9c715 | 7865 | |
117ed4f8 | 7866 | sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); |
16e9c715 | 7867 | if (sect == NULL) |
b34976b6 | 7868 | return FALSE; |
c044fabd | 7869 | |
eea6121a | 7870 | sect->size = sizeof (pstatus.data.thread_info.thread_context); |
079e9a2f AM |
7871 | sect->filepos = (note->descpos |
7872 | + offsetof (struct win32_pstatus, | |
7873 | data.thread_info.thread_context)); | |
16e9c715 NC |
7874 | sect->alignment_power = 2; |
7875 | ||
7876 | if (pstatus.data.thread_info.is_active_thread) | |
7877 | if (! elfcore_maybe_make_sect (abfd, ".reg", sect)) | |
b34976b6 | 7878 | return FALSE; |
16e9c715 NC |
7879 | break; |
7880 | ||
7881 | case NOTE_INFO_MODULE: | |
7882 | /* Make a ".module/xxxxxxxx" section. */ | |
1f170678 AM |
7883 | sprintf (buf, ".module/%08lx", |
7884 | (long) pstatus.data.module_info.base_address); | |
c044fabd | 7885 | |
d4c88bbb | 7886 | len = strlen (buf) + 1; |
217aa764 | 7887 | name = bfd_alloc (abfd, len); |
16e9c715 | 7888 | if (name == NULL) |
b34976b6 | 7889 | return FALSE; |
c044fabd | 7890 | |
d4c88bbb | 7891 | memcpy (name, buf, len); |
252b5132 | 7892 | |
117ed4f8 | 7893 | sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); |
c044fabd | 7894 | |
16e9c715 | 7895 | if (sect == NULL) |
b34976b6 | 7896 | return FALSE; |
c044fabd | 7897 | |
eea6121a | 7898 | sect->size = note->descsz; |
16e9c715 | 7899 | sect->filepos = note->descpos; |
16e9c715 NC |
7900 | sect->alignment_power = 2; |
7901 | break; | |
7902 | ||
7903 | default: | |
b34976b6 | 7904 | return TRUE; |
16e9c715 NC |
7905 | } |
7906 | ||
b34976b6 | 7907 | return TRUE; |
16e9c715 NC |
7908 | } |
7909 | #endif /* HAVE_WIN32_PSTATUS_T */ | |
252b5132 | 7910 | |
b34976b6 | 7911 | static bfd_boolean |
217aa764 | 7912 | elfcore_grok_note (bfd *abfd, Elf_Internal_Note *note) |
252b5132 | 7913 | { |
9c5bfbb7 | 7914 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
bb0082d6 | 7915 | |
252b5132 RH |
7916 | switch (note->type) |
7917 | { | |
7918 | default: | |
b34976b6 | 7919 | return TRUE; |
252b5132 | 7920 | |
252b5132 | 7921 | case NT_PRSTATUS: |
bb0082d6 AM |
7922 | if (bed->elf_backend_grok_prstatus) |
7923 | if ((*bed->elf_backend_grok_prstatus) (abfd, note)) | |
b34976b6 | 7924 | return TRUE; |
bb0082d6 | 7925 | #if defined (HAVE_PRSTATUS_T) |
252b5132 | 7926 | return elfcore_grok_prstatus (abfd, note); |
bb0082d6 | 7927 | #else |
b34976b6 | 7928 | return TRUE; |
252b5132 RH |
7929 | #endif |
7930 | ||
7931 | #if defined (HAVE_PSTATUS_T) | |
7932 | case NT_PSTATUS: | |
7933 | return elfcore_grok_pstatus (abfd, note); | |
7934 | #endif | |
7935 | ||
7936 | #if defined (HAVE_LWPSTATUS_T) | |
7937 | case NT_LWPSTATUS: | |
7938 | return elfcore_grok_lwpstatus (abfd, note); | |
7939 | #endif | |
7940 | ||
7941 | case NT_FPREGSET: /* FIXME: rename to NT_PRFPREG */ | |
7942 | return elfcore_grok_prfpreg (abfd, note); | |
7943 | ||
16e9c715 | 7944 | #if defined (HAVE_WIN32_PSTATUS_T) |
c044fabd | 7945 | case NT_WIN32PSTATUS: |
16e9c715 NC |
7946 | return elfcore_grok_win32pstatus (abfd, note); |
7947 | #endif | |
7948 | ||
c044fabd | 7949 | case NT_PRXFPREG: /* Linux SSE extension */ |
e377ab71 MK |
7950 | if (note->namesz == 6 |
7951 | && strcmp (note->namedata, "LINUX") == 0) | |
ff08c6bb JB |
7952 | return elfcore_grok_prxfpreg (abfd, note); |
7953 | else | |
b34976b6 | 7954 | return TRUE; |
ff08c6bb | 7955 | |
252b5132 RH |
7956 | case NT_PRPSINFO: |
7957 | case NT_PSINFO: | |
bb0082d6 AM |
7958 | if (bed->elf_backend_grok_psinfo) |
7959 | if ((*bed->elf_backend_grok_psinfo) (abfd, note)) | |
b34976b6 | 7960 | return TRUE; |
bb0082d6 | 7961 | #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) |
252b5132 | 7962 | return elfcore_grok_psinfo (abfd, note); |
bb0082d6 | 7963 | #else |
b34976b6 | 7964 | return TRUE; |
252b5132 | 7965 | #endif |
3333a7c3 RM |
7966 | |
7967 | case NT_AUXV: | |
7968 | { | |
117ed4f8 AM |
7969 | asection *sect = bfd_make_section_anyway_with_flags (abfd, ".auxv", |
7970 | SEC_HAS_CONTENTS); | |
3333a7c3 RM |
7971 | |
7972 | if (sect == NULL) | |
7973 | return FALSE; | |
eea6121a | 7974 | sect->size = note->descsz; |
3333a7c3 | 7975 | sect->filepos = note->descpos; |
3333a7c3 RM |
7976 | sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32; |
7977 | ||
7978 | return TRUE; | |
7979 | } | |
252b5132 RH |
7980 | } |
7981 | } | |
7982 | ||
b34976b6 | 7983 | static bfd_boolean |
217aa764 | 7984 | elfcore_netbsd_get_lwpid (Elf_Internal_Note *note, int *lwpidp) |
50b2bdb7 AM |
7985 | { |
7986 | char *cp; | |
7987 | ||
7988 | cp = strchr (note->namedata, '@'); | |
7989 | if (cp != NULL) | |
7990 | { | |
d2b64500 | 7991 | *lwpidp = atoi(cp + 1); |
b34976b6 | 7992 | return TRUE; |
50b2bdb7 | 7993 | } |
b34976b6 | 7994 | return FALSE; |
50b2bdb7 AM |
7995 | } |
7996 | ||
b34976b6 | 7997 | static bfd_boolean |
217aa764 | 7998 | elfcore_grok_netbsd_procinfo (bfd *abfd, Elf_Internal_Note *note) |
50b2bdb7 AM |
7999 | { |
8000 | ||
8001 | /* Signal number at offset 0x08. */ | |
8002 | elf_tdata (abfd)->core_signal | |
8003 | = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08); | |
8004 | ||
8005 | /* Process ID at offset 0x50. */ | |
8006 | elf_tdata (abfd)->core_pid | |
8007 | = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x50); | |
8008 | ||
8009 | /* Command name at 0x7c (max 32 bytes, including nul). */ | |
8010 | elf_tdata (abfd)->core_command | |
8011 | = _bfd_elfcore_strndup (abfd, note->descdata + 0x7c, 31); | |
8012 | ||
7720ba9f MK |
8013 | return elfcore_make_note_pseudosection (abfd, ".note.netbsdcore.procinfo", |
8014 | note); | |
50b2bdb7 AM |
8015 | } |
8016 | ||
b34976b6 | 8017 | static bfd_boolean |
217aa764 | 8018 | elfcore_grok_netbsd_note (bfd *abfd, Elf_Internal_Note *note) |
50b2bdb7 AM |
8019 | { |
8020 | int lwp; | |
8021 | ||
8022 | if (elfcore_netbsd_get_lwpid (note, &lwp)) | |
8023 | elf_tdata (abfd)->core_lwpid = lwp; | |
8024 | ||
b4db1224 | 8025 | if (note->type == NT_NETBSDCORE_PROCINFO) |
50b2bdb7 AM |
8026 | { |
8027 | /* NetBSD-specific core "procinfo". Note that we expect to | |
8028 | find this note before any of the others, which is fine, | |
8029 | since the kernel writes this note out first when it | |
8030 | creates a core file. */ | |
47d9a591 | 8031 | |
50b2bdb7 AM |
8032 | return elfcore_grok_netbsd_procinfo (abfd, note); |
8033 | } | |
8034 | ||
b4db1224 JT |
8035 | /* As of Jan 2002 there are no other machine-independent notes |
8036 | defined for NetBSD core files. If the note type is less | |
8037 | than the start of the machine-dependent note types, we don't | |
8038 | understand it. */ | |
47d9a591 | 8039 | |
b4db1224 | 8040 | if (note->type < NT_NETBSDCORE_FIRSTMACH) |
b34976b6 | 8041 | return TRUE; |
50b2bdb7 AM |
8042 | |
8043 | ||
8044 | switch (bfd_get_arch (abfd)) | |
8045 | { | |
8046 | /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and | |
8047 | PT_GETFPREGS == mach+2. */ | |
8048 | ||
8049 | case bfd_arch_alpha: | |
8050 | case bfd_arch_sparc: | |
8051 | switch (note->type) | |
8052 | { | |
b4db1224 | 8053 | case NT_NETBSDCORE_FIRSTMACH+0: |
50b2bdb7 AM |
8054 | return elfcore_make_note_pseudosection (abfd, ".reg", note); |
8055 | ||
b4db1224 | 8056 | case NT_NETBSDCORE_FIRSTMACH+2: |
50b2bdb7 AM |
8057 | return elfcore_make_note_pseudosection (abfd, ".reg2", note); |
8058 | ||
8059 | default: | |
b34976b6 | 8060 | return TRUE; |
50b2bdb7 AM |
8061 | } |
8062 | ||
8063 | /* On all other arch's, PT_GETREGS == mach+1 and | |
8064 | PT_GETFPREGS == mach+3. */ | |
8065 | ||
8066 | default: | |
8067 | switch (note->type) | |
8068 | { | |
b4db1224 | 8069 | case NT_NETBSDCORE_FIRSTMACH+1: |
50b2bdb7 AM |
8070 | return elfcore_make_note_pseudosection (abfd, ".reg", note); |
8071 | ||
b4db1224 | 8072 | case NT_NETBSDCORE_FIRSTMACH+3: |
50b2bdb7 AM |
8073 | return elfcore_make_note_pseudosection (abfd, ".reg2", note); |
8074 | ||
8075 | default: | |
b34976b6 | 8076 | return TRUE; |
50b2bdb7 AM |
8077 | } |
8078 | } | |
8079 | /* NOTREACHED */ | |
8080 | } | |
8081 | ||
07c6e936 | 8082 | static bfd_boolean |
d3fd4074 | 8083 | elfcore_grok_nto_status (bfd *abfd, Elf_Internal_Note *note, long *tid) |
07c6e936 NC |
8084 | { |
8085 | void *ddata = note->descdata; | |
8086 | char buf[100]; | |
8087 | char *name; | |
8088 | asection *sect; | |
f8843e87 AM |
8089 | short sig; |
8090 | unsigned flags; | |
07c6e936 NC |
8091 | |
8092 | /* nto_procfs_status 'pid' field is at offset 0. */ | |
8093 | elf_tdata (abfd)->core_pid = bfd_get_32 (abfd, (bfd_byte *) ddata); | |
8094 | ||
f8843e87 AM |
8095 | /* nto_procfs_status 'tid' field is at offset 4. Pass it back. */ |
8096 | *tid = bfd_get_32 (abfd, (bfd_byte *) ddata + 4); | |
8097 | ||
8098 | /* nto_procfs_status 'flags' field is at offset 8. */ | |
8099 | flags = bfd_get_32 (abfd, (bfd_byte *) ddata + 8); | |
07c6e936 NC |
8100 | |
8101 | /* nto_procfs_status 'what' field is at offset 14. */ | |
f8843e87 AM |
8102 | if ((sig = bfd_get_16 (abfd, (bfd_byte *) ddata + 14)) > 0) |
8103 | { | |
8104 | elf_tdata (abfd)->core_signal = sig; | |
8105 | elf_tdata (abfd)->core_lwpid = *tid; | |
8106 | } | |
07c6e936 | 8107 | |
f8843e87 AM |
8108 | /* _DEBUG_FLAG_CURTID (current thread) is 0x80. Some cores |
8109 | do not come from signals so we make sure we set the current | |
8110 | thread just in case. */ | |
8111 | if (flags & 0x00000080) | |
8112 | elf_tdata (abfd)->core_lwpid = *tid; | |
07c6e936 NC |
8113 | |
8114 | /* Make a ".qnx_core_status/%d" section. */ | |
d3fd4074 | 8115 | sprintf (buf, ".qnx_core_status/%ld", *tid); |
07c6e936 | 8116 | |
217aa764 | 8117 | name = bfd_alloc (abfd, strlen (buf) + 1); |
07c6e936 NC |
8118 | if (name == NULL) |
8119 | return FALSE; | |
8120 | strcpy (name, buf); | |
8121 | ||
117ed4f8 | 8122 | sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); |
07c6e936 NC |
8123 | if (sect == NULL) |
8124 | return FALSE; | |
8125 | ||
eea6121a | 8126 | sect->size = note->descsz; |
07c6e936 | 8127 | sect->filepos = note->descpos; |
07c6e936 NC |
8128 | sect->alignment_power = 2; |
8129 | ||
8130 | return (elfcore_maybe_make_sect (abfd, ".qnx_core_status", sect)); | |
8131 | } | |
8132 | ||
8133 | static bfd_boolean | |
d69f560c KW |
8134 | elfcore_grok_nto_regs (bfd *abfd, |
8135 | Elf_Internal_Note *note, | |
d3fd4074 | 8136 | long tid, |
d69f560c | 8137 | char *base) |
07c6e936 NC |
8138 | { |
8139 | char buf[100]; | |
8140 | char *name; | |
8141 | asection *sect; | |
8142 | ||
d69f560c | 8143 | /* Make a "(base)/%d" section. */ |
d3fd4074 | 8144 | sprintf (buf, "%s/%ld", base, tid); |
07c6e936 | 8145 | |
217aa764 | 8146 | name = bfd_alloc (abfd, strlen (buf) + 1); |
07c6e936 NC |
8147 | if (name == NULL) |
8148 | return FALSE; | |
8149 | strcpy (name, buf); | |
8150 | ||
117ed4f8 | 8151 | sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); |
07c6e936 NC |
8152 | if (sect == NULL) |
8153 | return FALSE; | |
8154 | ||
eea6121a | 8155 | sect->size = note->descsz; |
07c6e936 | 8156 | sect->filepos = note->descpos; |
07c6e936 NC |
8157 | sect->alignment_power = 2; |
8158 | ||
f8843e87 AM |
8159 | /* This is the current thread. */ |
8160 | if (elf_tdata (abfd)->core_lwpid == tid) | |
d69f560c | 8161 | return elfcore_maybe_make_sect (abfd, base, sect); |
f8843e87 AM |
8162 | |
8163 | return TRUE; | |
07c6e936 NC |
8164 | } |
8165 | ||
8166 | #define BFD_QNT_CORE_INFO 7 | |
8167 | #define BFD_QNT_CORE_STATUS 8 | |
8168 | #define BFD_QNT_CORE_GREG 9 | |
8169 | #define BFD_QNT_CORE_FPREG 10 | |
8170 | ||
8171 | static bfd_boolean | |
217aa764 | 8172 | elfcore_grok_nto_note (bfd *abfd, Elf_Internal_Note *note) |
07c6e936 NC |
8173 | { |
8174 | /* Every GREG section has a STATUS section before it. Store the | |
811072d8 | 8175 | tid from the previous call to pass down to the next gregs |
07c6e936 | 8176 | function. */ |
d3fd4074 | 8177 | static long tid = 1; |
07c6e936 NC |
8178 | |
8179 | switch (note->type) | |
8180 | { | |
d69f560c KW |
8181 | case BFD_QNT_CORE_INFO: |
8182 | return elfcore_make_note_pseudosection (abfd, ".qnx_core_info", note); | |
8183 | case BFD_QNT_CORE_STATUS: | |
8184 | return elfcore_grok_nto_status (abfd, note, &tid); | |
8185 | case BFD_QNT_CORE_GREG: | |
8186 | return elfcore_grok_nto_regs (abfd, note, tid, ".reg"); | |
8187 | case BFD_QNT_CORE_FPREG: | |
8188 | return elfcore_grok_nto_regs (abfd, note, tid, ".reg2"); | |
8189 | default: | |
8190 | return TRUE; | |
07c6e936 NC |
8191 | } |
8192 | } | |
8193 | ||
7c76fa91 MS |
8194 | /* Function: elfcore_write_note |
8195 | ||
47d9a591 | 8196 | Inputs: |
a39f3346 | 8197 | buffer to hold note, and current size of buffer |
7c76fa91 MS |
8198 | name of note |
8199 | type of note | |
8200 | data for note | |
8201 | size of data for note | |
8202 | ||
a39f3346 AM |
8203 | Writes note to end of buffer. ELF64 notes are written exactly as |
8204 | for ELF32, despite the current (as of 2006) ELF gabi specifying | |
8205 | that they ought to have 8-byte namesz and descsz field, and have | |
8206 | 8-byte alignment. Other writers, eg. Linux kernel, do the same. | |
8207 | ||
7c76fa91 | 8208 | Return: |
a39f3346 | 8209 | Pointer to realloc'd buffer, *BUFSIZ updated. */ |
7c76fa91 MS |
8210 | |
8211 | char * | |
a39f3346 | 8212 | elfcore_write_note (bfd *abfd, |
217aa764 | 8213 | char *buf, |
a39f3346 | 8214 | int *bufsiz, |
217aa764 | 8215 | const char *name, |
a39f3346 | 8216 | int type, |
217aa764 | 8217 | const void *input, |
a39f3346 | 8218 | int size) |
7c76fa91 MS |
8219 | { |
8220 | Elf_External_Note *xnp; | |
d4c88bbb | 8221 | size_t namesz; |
d4c88bbb | 8222 | size_t newspace; |
a39f3346 | 8223 | char *dest; |
7c76fa91 | 8224 | |
d4c88bbb | 8225 | namesz = 0; |
d4c88bbb | 8226 | if (name != NULL) |
a39f3346 | 8227 | namesz = strlen (name) + 1; |
d4c88bbb | 8228 | |
a39f3346 | 8229 | newspace = 12 + ((namesz + 3) & -4) + ((size + 3) & -4); |
d4c88bbb | 8230 | |
a39f3346 AM |
8231 | buf = realloc (buf, *bufsiz + newspace); |
8232 | dest = buf + *bufsiz; | |
7c76fa91 MS |
8233 | *bufsiz += newspace; |
8234 | xnp = (Elf_External_Note *) dest; | |
8235 | H_PUT_32 (abfd, namesz, xnp->namesz); | |
8236 | H_PUT_32 (abfd, size, xnp->descsz); | |
8237 | H_PUT_32 (abfd, type, xnp->type); | |
d4c88bbb AM |
8238 | dest = xnp->name; |
8239 | if (name != NULL) | |
8240 | { | |
8241 | memcpy (dest, name, namesz); | |
8242 | dest += namesz; | |
a39f3346 | 8243 | while (namesz & 3) |
d4c88bbb AM |
8244 | { |
8245 | *dest++ = '\0'; | |
a39f3346 | 8246 | ++namesz; |
d4c88bbb AM |
8247 | } |
8248 | } | |
8249 | memcpy (dest, input, size); | |
a39f3346 AM |
8250 | dest += size; |
8251 | while (size & 3) | |
8252 | { | |
8253 | *dest++ = '\0'; | |
8254 | ++size; | |
8255 | } | |
8256 | return buf; | |
7c76fa91 MS |
8257 | } |
8258 | ||
8259 | #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) | |
8260 | char * | |
217aa764 AM |
8261 | elfcore_write_prpsinfo (bfd *abfd, |
8262 | char *buf, | |
8263 | int *bufsiz, | |
8264 | const char *fname, | |
8265 | const char *psargs) | |
7c76fa91 | 8266 | { |
183e98be AM |
8267 | const char *note_name = "CORE"; |
8268 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
8269 | ||
8270 | if (bed->elf_backend_write_core_note != NULL) | |
8271 | { | |
8272 | char *ret; | |
8273 | ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz, | |
8274 | NT_PRPSINFO, fname, psargs); | |
8275 | if (ret != NULL) | |
8276 | return ret; | |
8277 | } | |
7c76fa91 | 8278 | |
183e98be AM |
8279 | #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T) |
8280 | if (bed->s->elfclass == ELFCLASS32) | |
8281 | { | |
8282 | #if defined (HAVE_PSINFO32_T) | |
8283 | psinfo32_t data; | |
8284 | int note_type = NT_PSINFO; | |
8285 | #else | |
8286 | prpsinfo32_t data; | |
8287 | int note_type = NT_PRPSINFO; | |
8288 | #endif | |
8289 | ||
8290 | memset (&data, 0, sizeof (data)); | |
8291 | strncpy (data.pr_fname, fname, sizeof (data.pr_fname)); | |
8292 | strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs)); | |
8293 | return elfcore_write_note (abfd, buf, bufsiz, | |
8294 | note_name, note_type, &data, sizeof (data)); | |
8295 | } | |
8296 | else | |
8297 | #endif | |
8298 | { | |
7c76fa91 | 8299 | #if defined (HAVE_PSINFO_T) |
183e98be AM |
8300 | psinfo_t data; |
8301 | int note_type = NT_PSINFO; | |
7c76fa91 | 8302 | #else |
183e98be AM |
8303 | prpsinfo_t data; |
8304 | int note_type = NT_PRPSINFO; | |
7c76fa91 MS |
8305 | #endif |
8306 | ||
183e98be AM |
8307 | memset (&data, 0, sizeof (data)); |
8308 | strncpy (data.pr_fname, fname, sizeof (data.pr_fname)); | |
8309 | strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs)); | |
8310 | return elfcore_write_note (abfd, buf, bufsiz, | |
8311 | note_name, note_type, &data, sizeof (data)); | |
8312 | } | |
7c76fa91 MS |
8313 | } |
8314 | #endif /* PSINFO_T or PRPSINFO_T */ | |
8315 | ||
8316 | #if defined (HAVE_PRSTATUS_T) | |
8317 | char * | |
217aa764 AM |
8318 | elfcore_write_prstatus (bfd *abfd, |
8319 | char *buf, | |
8320 | int *bufsiz, | |
8321 | long pid, | |
8322 | int cursig, | |
8323 | const void *gregs) | |
7c76fa91 | 8324 | { |
183e98be AM |
8325 | const char *note_name = "CORE"; |
8326 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
7c76fa91 | 8327 | |
183e98be AM |
8328 | if (bed->elf_backend_write_core_note != NULL) |
8329 | { | |
8330 | char *ret; | |
8331 | ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz, | |
8332 | NT_PRSTATUS, | |
8333 | pid, cursig, gregs); | |
8334 | if (ret != NULL) | |
8335 | return ret; | |
8336 | } | |
8337 | ||
8338 | #if defined (HAVE_PRSTATUS32_T) | |
8339 | if (bed->s->elfclass == ELFCLASS32) | |
8340 | { | |
8341 | prstatus32_t prstat; | |
8342 | ||
8343 | memset (&prstat, 0, sizeof (prstat)); | |
8344 | prstat.pr_pid = pid; | |
8345 | prstat.pr_cursig = cursig; | |
8346 | memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg)); | |
8347 | return elfcore_write_note (abfd, buf, bufsiz, note_name, | |
8348 | NT_PRSTATUS, &prstat, sizeof (prstat)); | |
8349 | } | |
8350 | else | |
8351 | #endif | |
8352 | { | |
8353 | prstatus_t prstat; | |
8354 | ||
8355 | memset (&prstat, 0, sizeof (prstat)); | |
8356 | prstat.pr_pid = pid; | |
8357 | prstat.pr_cursig = cursig; | |
8358 | memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg)); | |
8359 | return elfcore_write_note (abfd, buf, bufsiz, note_name, | |
8360 | NT_PRSTATUS, &prstat, sizeof (prstat)); | |
8361 | } | |
7c76fa91 MS |
8362 | } |
8363 | #endif /* HAVE_PRSTATUS_T */ | |
8364 | ||
51316059 MS |
8365 | #if defined (HAVE_LWPSTATUS_T) |
8366 | char * | |
217aa764 AM |
8367 | elfcore_write_lwpstatus (bfd *abfd, |
8368 | char *buf, | |
8369 | int *bufsiz, | |
8370 | long pid, | |
8371 | int cursig, | |
8372 | const void *gregs) | |
51316059 MS |
8373 | { |
8374 | lwpstatus_t lwpstat; | |
183e98be | 8375 | const char *note_name = "CORE"; |
51316059 MS |
8376 | |
8377 | memset (&lwpstat, 0, sizeof (lwpstat)); | |
8378 | lwpstat.pr_lwpid = pid >> 16; | |
8379 | lwpstat.pr_cursig = cursig; | |
8380 | #if defined (HAVE_LWPSTATUS_T_PR_REG) | |
8381 | memcpy (lwpstat.pr_reg, gregs, sizeof (lwpstat.pr_reg)); | |
8382 | #elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT) | |
8383 | #if !defined(gregs) | |
8384 | memcpy (lwpstat.pr_context.uc_mcontext.gregs, | |
8385 | gregs, sizeof (lwpstat.pr_context.uc_mcontext.gregs)); | |
8386 | #else | |
8387 | memcpy (lwpstat.pr_context.uc_mcontext.__gregs, | |
8388 | gregs, sizeof (lwpstat.pr_context.uc_mcontext.__gregs)); | |
8389 | #endif | |
8390 | #endif | |
47d9a591 | 8391 | return elfcore_write_note (abfd, buf, bufsiz, note_name, |
51316059 MS |
8392 | NT_LWPSTATUS, &lwpstat, sizeof (lwpstat)); |
8393 | } | |
8394 | #endif /* HAVE_LWPSTATUS_T */ | |
8395 | ||
7c76fa91 MS |
8396 | #if defined (HAVE_PSTATUS_T) |
8397 | char * | |
217aa764 AM |
8398 | elfcore_write_pstatus (bfd *abfd, |
8399 | char *buf, | |
8400 | int *bufsiz, | |
8401 | long pid, | |
6c10990d NC |
8402 | int cursig ATTRIBUTE_UNUSED, |
8403 | const void *gregs ATTRIBUTE_UNUSED) | |
7c76fa91 | 8404 | { |
183e98be AM |
8405 | const char *note_name = "CORE"; |
8406 | #if defined (HAVE_PSTATUS32_T) | |
8407 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
7c76fa91 | 8408 | |
183e98be AM |
8409 | if (bed->s->elfclass == ELFCLASS32) |
8410 | { | |
8411 | pstatus32_t pstat; | |
8412 | ||
8413 | memset (&pstat, 0, sizeof (pstat)); | |
8414 | pstat.pr_pid = pid & 0xffff; | |
8415 | buf = elfcore_write_note (abfd, buf, bufsiz, note_name, | |
8416 | NT_PSTATUS, &pstat, sizeof (pstat)); | |
8417 | return buf; | |
8418 | } | |
8419 | else | |
8420 | #endif | |
8421 | { | |
8422 | pstatus_t pstat; | |
8423 | ||
8424 | memset (&pstat, 0, sizeof (pstat)); | |
8425 | pstat.pr_pid = pid & 0xffff; | |
8426 | buf = elfcore_write_note (abfd, buf, bufsiz, note_name, | |
8427 | NT_PSTATUS, &pstat, sizeof (pstat)); | |
8428 | return buf; | |
8429 | } | |
7c76fa91 MS |
8430 | } |
8431 | #endif /* HAVE_PSTATUS_T */ | |
8432 | ||
8433 | char * | |
217aa764 AM |
8434 | elfcore_write_prfpreg (bfd *abfd, |
8435 | char *buf, | |
8436 | int *bufsiz, | |
8437 | const void *fpregs, | |
8438 | int size) | |
7c76fa91 | 8439 | { |
183e98be | 8440 | const char *note_name = "CORE"; |
47d9a591 | 8441 | return elfcore_write_note (abfd, buf, bufsiz, |
7c76fa91 MS |
8442 | note_name, NT_FPREGSET, fpregs, size); |
8443 | } | |
8444 | ||
8445 | char * | |
217aa764 AM |
8446 | elfcore_write_prxfpreg (bfd *abfd, |
8447 | char *buf, | |
8448 | int *bufsiz, | |
8449 | const void *xfpregs, | |
8450 | int size) | |
7c76fa91 MS |
8451 | { |
8452 | char *note_name = "LINUX"; | |
47d9a591 | 8453 | return elfcore_write_note (abfd, buf, bufsiz, |
7c76fa91 MS |
8454 | note_name, NT_PRXFPREG, xfpregs, size); |
8455 | } | |
8456 | ||
b34976b6 | 8457 | static bfd_boolean |
217aa764 | 8458 | elfcore_read_notes (bfd *abfd, file_ptr offset, bfd_size_type size) |
252b5132 | 8459 | { |
c044fabd KH |
8460 | char *buf; |
8461 | char *p; | |
252b5132 RH |
8462 | |
8463 | if (size <= 0) | |
b34976b6 | 8464 | return TRUE; |
252b5132 | 8465 | |
dc810e39 | 8466 | if (bfd_seek (abfd, offset, SEEK_SET) != 0) |
b34976b6 | 8467 | return FALSE; |
252b5132 | 8468 | |
dc810e39 | 8469 | buf = bfd_malloc (size); |
252b5132 | 8470 | if (buf == NULL) |
b34976b6 | 8471 | return FALSE; |
252b5132 | 8472 | |
dc810e39 | 8473 | if (bfd_bread (buf, size, abfd) != size) |
252b5132 RH |
8474 | { |
8475 | error: | |
8476 | free (buf); | |
b34976b6 | 8477 | return FALSE; |
252b5132 RH |
8478 | } |
8479 | ||
8480 | p = buf; | |
8481 | while (p < buf + size) | |
8482 | { | |
c044fabd KH |
8483 | /* FIXME: bad alignment assumption. */ |
8484 | Elf_External_Note *xnp = (Elf_External_Note *) p; | |
252b5132 RH |
8485 | Elf_Internal_Note in; |
8486 | ||
dc810e39 | 8487 | in.type = H_GET_32 (abfd, xnp->type); |
252b5132 | 8488 | |
dc810e39 | 8489 | in.namesz = H_GET_32 (abfd, xnp->namesz); |
252b5132 RH |
8490 | in.namedata = xnp->name; |
8491 | ||
dc810e39 | 8492 | in.descsz = H_GET_32 (abfd, xnp->descsz); |
252b5132 RH |
8493 | in.descdata = in.namedata + BFD_ALIGN (in.namesz, 4); |
8494 | in.descpos = offset + (in.descdata - buf); | |
8495 | ||
0112cd26 | 8496 | if (CONST_STRNEQ (in.namedata, "NetBSD-CORE")) |
50b2bdb7 AM |
8497 | { |
8498 | if (! elfcore_grok_netbsd_note (abfd, &in)) | |
8499 | goto error; | |
8500 | } | |
0112cd26 | 8501 | else if (CONST_STRNEQ (in.namedata, "QNX")) |
07c6e936 NC |
8502 | { |
8503 | if (! elfcore_grok_nto_note (abfd, &in)) | |
8504 | goto error; | |
8505 | } | |
50b2bdb7 AM |
8506 | else |
8507 | { | |
8508 | if (! elfcore_grok_note (abfd, &in)) | |
8509 | goto error; | |
8510 | } | |
252b5132 RH |
8511 | |
8512 | p = in.descdata + BFD_ALIGN (in.descsz, 4); | |
8513 | } | |
8514 | ||
8515 | free (buf); | |
b34976b6 | 8516 | return TRUE; |
252b5132 | 8517 | } |
98d8431c JB |
8518 | \f |
8519 | /* Providing external access to the ELF program header table. */ | |
8520 | ||
8521 | /* Return an upper bound on the number of bytes required to store a | |
8522 | copy of ABFD's program header table entries. Return -1 if an error | |
8523 | occurs; bfd_get_error will return an appropriate code. */ | |
c044fabd | 8524 | |
98d8431c | 8525 | long |
217aa764 | 8526 | bfd_get_elf_phdr_upper_bound (bfd *abfd) |
98d8431c JB |
8527 | { |
8528 | if (abfd->xvec->flavour != bfd_target_elf_flavour) | |
8529 | { | |
8530 | bfd_set_error (bfd_error_wrong_format); | |
8531 | return -1; | |
8532 | } | |
8533 | ||
936e320b | 8534 | return elf_elfheader (abfd)->e_phnum * sizeof (Elf_Internal_Phdr); |
98d8431c JB |
8535 | } |
8536 | ||
98d8431c JB |
8537 | /* Copy ABFD's program header table entries to *PHDRS. The entries |
8538 | will be stored as an array of Elf_Internal_Phdr structures, as | |
8539 | defined in include/elf/internal.h. To find out how large the | |
8540 | buffer needs to be, call bfd_get_elf_phdr_upper_bound. | |
8541 | ||
8542 | Return the number of program header table entries read, or -1 if an | |
8543 | error occurs; bfd_get_error will return an appropriate code. */ | |
c044fabd | 8544 | |
98d8431c | 8545 | int |
217aa764 | 8546 | bfd_get_elf_phdrs (bfd *abfd, void *phdrs) |
98d8431c JB |
8547 | { |
8548 | int num_phdrs; | |
8549 | ||
8550 | if (abfd->xvec->flavour != bfd_target_elf_flavour) | |
8551 | { | |
8552 | bfd_set_error (bfd_error_wrong_format); | |
8553 | return -1; | |
8554 | } | |
8555 | ||
8556 | num_phdrs = elf_elfheader (abfd)->e_phnum; | |
c044fabd | 8557 | memcpy (phdrs, elf_tdata (abfd)->phdr, |
98d8431c JB |
8558 | num_phdrs * sizeof (Elf_Internal_Phdr)); |
8559 | ||
8560 | return num_phdrs; | |
8561 | } | |
ae4221d7 L |
8562 | |
8563 | void | |
217aa764 | 8564 | _bfd_elf_sprintf_vma (bfd *abfd ATTRIBUTE_UNUSED, char *buf, bfd_vma value) |
ae4221d7 | 8565 | { |
d3b05f8d | 8566 | #ifdef BFD64 |
ae4221d7 L |
8567 | Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */ |
8568 | ||
8569 | i_ehdrp = elf_elfheader (abfd); | |
8570 | if (i_ehdrp == NULL) | |
8571 | sprintf_vma (buf, value); | |
8572 | else | |
8573 | { | |
8574 | if (i_ehdrp->e_ident[EI_CLASS] == ELFCLASS64) | |
cc55aec9 | 8575 | { |
ae4221d7 | 8576 | #if BFD_HOST_64BIT_LONG |
cc55aec9 | 8577 | sprintf (buf, "%016lx", value); |
ae4221d7 | 8578 | #else |
cc55aec9 AM |
8579 | sprintf (buf, "%08lx%08lx", _bfd_int64_high (value), |
8580 | _bfd_int64_low (value)); | |
ae4221d7 | 8581 | #endif |
cc55aec9 | 8582 | } |
ae4221d7 L |
8583 | else |
8584 | sprintf (buf, "%08lx", (unsigned long) (value & 0xffffffff)); | |
8585 | } | |
d3b05f8d L |
8586 | #else |
8587 | sprintf_vma (buf, value); | |
8588 | #endif | |
ae4221d7 L |
8589 | } |
8590 | ||
8591 | void | |
217aa764 | 8592 | _bfd_elf_fprintf_vma (bfd *abfd ATTRIBUTE_UNUSED, void *stream, bfd_vma value) |
ae4221d7 | 8593 | { |
d3b05f8d | 8594 | #ifdef BFD64 |
ae4221d7 L |
8595 | Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */ |
8596 | ||
8597 | i_ehdrp = elf_elfheader (abfd); | |
8598 | if (i_ehdrp == NULL) | |
8599 | fprintf_vma ((FILE *) stream, value); | |
8600 | else | |
8601 | { | |
8602 | if (i_ehdrp->e_ident[EI_CLASS] == ELFCLASS64) | |
cc55aec9 | 8603 | { |
ae4221d7 | 8604 | #if BFD_HOST_64BIT_LONG |
cc55aec9 | 8605 | fprintf ((FILE *) stream, "%016lx", value); |
ae4221d7 | 8606 | #else |
cc55aec9 AM |
8607 | fprintf ((FILE *) stream, "%08lx%08lx", |
8608 | _bfd_int64_high (value), _bfd_int64_low (value)); | |
ae4221d7 | 8609 | #endif |
cc55aec9 | 8610 | } |
ae4221d7 L |
8611 | else |
8612 | fprintf ((FILE *) stream, "%08lx", | |
8613 | (unsigned long) (value & 0xffffffff)); | |
8614 | } | |
d3b05f8d L |
8615 | #else |
8616 | fprintf_vma ((FILE *) stream, value); | |
8617 | #endif | |
ae4221d7 | 8618 | } |
db6751f2 JJ |
8619 | |
8620 | enum elf_reloc_type_class | |
217aa764 | 8621 | _bfd_elf_reloc_type_class (const Elf_Internal_Rela *rela ATTRIBUTE_UNUSED) |
db6751f2 JJ |
8622 | { |
8623 | return reloc_class_normal; | |
8624 | } | |
f8df10f4 | 8625 | |
47d9a591 | 8626 | /* For RELA architectures, return the relocation value for a |
f8df10f4 JJ |
8627 | relocation against a local symbol. */ |
8628 | ||
8629 | bfd_vma | |
217aa764 AM |
8630 | _bfd_elf_rela_local_sym (bfd *abfd, |
8631 | Elf_Internal_Sym *sym, | |
8517fae7 | 8632 | asection **psec, |
217aa764 | 8633 | Elf_Internal_Rela *rel) |
f8df10f4 | 8634 | { |
8517fae7 | 8635 | asection *sec = *psec; |
f8df10f4 JJ |
8636 | bfd_vma relocation; |
8637 | ||
8638 | relocation = (sec->output_section->vma | |
8639 | + sec->output_offset | |
8640 | + sym->st_value); | |
8641 | if ((sec->flags & SEC_MERGE) | |
c629eae0 | 8642 | && ELF_ST_TYPE (sym->st_info) == STT_SECTION |
68bfbfcc | 8643 | && sec->sec_info_type == ELF_INFO_TYPE_MERGE) |
f8df10f4 | 8644 | { |
f8df10f4 | 8645 | rel->r_addend = |
8517fae7 | 8646 | _bfd_merged_section_offset (abfd, psec, |
65765700 | 8647 | elf_section_data (sec)->sec_info, |
753731ee AM |
8648 | sym->st_value + rel->r_addend); |
8649 | if (sec != *psec) | |
8650 | { | |
8651 | /* If we have changed the section, and our original section is | |
8652 | marked with SEC_EXCLUDE, it means that the original | |
8653 | SEC_MERGE section has been completely subsumed in some | |
8654 | other SEC_MERGE section. In this case, we need to leave | |
8655 | some info around for --emit-relocs. */ | |
8656 | if ((sec->flags & SEC_EXCLUDE) != 0) | |
8657 | sec->kept_section = *psec; | |
8658 | sec = *psec; | |
8659 | } | |
8517fae7 AM |
8660 | rel->r_addend -= relocation; |
8661 | rel->r_addend += sec->output_section->vma + sec->output_offset; | |
f8df10f4 JJ |
8662 | } |
8663 | return relocation; | |
8664 | } | |
c629eae0 JJ |
8665 | |
8666 | bfd_vma | |
217aa764 AM |
8667 | _bfd_elf_rel_local_sym (bfd *abfd, |
8668 | Elf_Internal_Sym *sym, | |
8669 | asection **psec, | |
8670 | bfd_vma addend) | |
47d9a591 | 8671 | { |
c629eae0 JJ |
8672 | asection *sec = *psec; |
8673 | ||
68bfbfcc | 8674 | if (sec->sec_info_type != ELF_INFO_TYPE_MERGE) |
c629eae0 JJ |
8675 | return sym->st_value + addend; |
8676 | ||
8677 | return _bfd_merged_section_offset (abfd, psec, | |
65765700 | 8678 | elf_section_data (sec)->sec_info, |
753731ee | 8679 | sym->st_value + addend); |
c629eae0 JJ |
8680 | } |
8681 | ||
8682 | bfd_vma | |
217aa764 | 8683 | _bfd_elf_section_offset (bfd *abfd, |
92e4ec35 | 8684 | struct bfd_link_info *info, |
217aa764 AM |
8685 | asection *sec, |
8686 | bfd_vma offset) | |
c629eae0 | 8687 | { |
68bfbfcc | 8688 | switch (sec->sec_info_type) |
65765700 JJ |
8689 | { |
8690 | case ELF_INFO_TYPE_STABS: | |
eea6121a AM |
8691 | return _bfd_stab_section_offset (sec, elf_section_data (sec)->sec_info, |
8692 | offset); | |
65765700 | 8693 | case ELF_INFO_TYPE_EH_FRAME: |
92e4ec35 | 8694 | return _bfd_elf_eh_frame_section_offset (abfd, info, sec, offset); |
65765700 JJ |
8695 | default: |
8696 | return offset; | |
8697 | } | |
c629eae0 | 8698 | } |
3333a7c3 RM |
8699 | \f |
8700 | /* Create a new BFD as if by bfd_openr. Rather than opening a file, | |
8701 | reconstruct an ELF file by reading the segments out of remote memory | |
8702 | based on the ELF file header at EHDR_VMA and the ELF program headers it | |
8703 | points to. If not null, *LOADBASEP is filled in with the difference | |
8704 | between the VMAs from which the segments were read, and the VMAs the | |
8705 | file headers (and hence BFD's idea of each section's VMA) put them at. | |
8706 | ||
8707 | The function TARGET_READ_MEMORY is called to copy LEN bytes from the | |
8708 | remote memory at target address VMA into the local buffer at MYADDR; it | |
8709 | should return zero on success or an `errno' code on failure. TEMPL must | |
8710 | be a BFD for an ELF target with the word size and byte order found in | |
8711 | the remote memory. */ | |
8712 | ||
8713 | bfd * | |
217aa764 AM |
8714 | bfd_elf_bfd_from_remote_memory |
8715 | (bfd *templ, | |
8716 | bfd_vma ehdr_vma, | |
8717 | bfd_vma *loadbasep, | |
f075ee0c | 8718 | int (*target_read_memory) (bfd_vma, bfd_byte *, int)) |
3333a7c3 RM |
8719 | { |
8720 | return (*get_elf_backend_data (templ)->elf_backend_bfd_from_remote_memory) | |
8721 | (templ, ehdr_vma, loadbasep, target_read_memory); | |
8722 | } | |
4c45e5c9 JJ |
8723 | \f |
8724 | long | |
c9727e01 AM |
8725 | _bfd_elf_get_synthetic_symtab (bfd *abfd, |
8726 | long symcount ATTRIBUTE_UNUSED, | |
8727 | asymbol **syms ATTRIBUTE_UNUSED, | |
8615f3f2 | 8728 | long dynsymcount, |
c9727e01 AM |
8729 | asymbol **dynsyms, |
8730 | asymbol **ret) | |
4c45e5c9 JJ |
8731 | { |
8732 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
8733 | asection *relplt; | |
8734 | asymbol *s; | |
8735 | const char *relplt_name; | |
8736 | bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean); | |
8737 | arelent *p; | |
8738 | long count, i, n; | |
8739 | size_t size; | |
8740 | Elf_Internal_Shdr *hdr; | |
8741 | char *names; | |
8742 | asection *plt; | |
8743 | ||
8615f3f2 AM |
8744 | *ret = NULL; |
8745 | ||
90e3cdf2 JJ |
8746 | if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0) |
8747 | return 0; | |
8748 | ||
8615f3f2 AM |
8749 | if (dynsymcount <= 0) |
8750 | return 0; | |
8751 | ||
4c45e5c9 JJ |
8752 | if (!bed->plt_sym_val) |
8753 | return 0; | |
8754 | ||
8755 | relplt_name = bed->relplt_name; | |
8756 | if (relplt_name == NULL) | |
8757 | relplt_name = bed->default_use_rela_p ? ".rela.plt" : ".rel.plt"; | |
8758 | relplt = bfd_get_section_by_name (abfd, relplt_name); | |
8759 | if (relplt == NULL) | |
8760 | return 0; | |
8761 | ||
8762 | hdr = &elf_section_data (relplt)->this_hdr; | |
8763 | if (hdr->sh_link != elf_dynsymtab (abfd) | |
8764 | || (hdr->sh_type != SHT_REL && hdr->sh_type != SHT_RELA)) | |
8765 | return 0; | |
8766 | ||
8767 | plt = bfd_get_section_by_name (abfd, ".plt"); | |
8768 | if (plt == NULL) | |
8769 | return 0; | |
8770 | ||
8771 | slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table; | |
c9727e01 | 8772 | if (! (*slurp_relocs) (abfd, relplt, dynsyms, TRUE)) |
4c45e5c9 JJ |
8773 | return -1; |
8774 | ||
eea6121a | 8775 | count = relplt->size / hdr->sh_entsize; |
4c45e5c9 JJ |
8776 | size = count * sizeof (asymbol); |
8777 | p = relplt->relocation; | |
8778 | for (i = 0; i < count; i++, s++, p++) | |
8779 | size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt"); | |
8780 | ||
8781 | s = *ret = bfd_malloc (size); | |
8782 | if (s == NULL) | |
8783 | return -1; | |
8784 | ||
8785 | names = (char *) (s + count); | |
8786 | p = relplt->relocation; | |
8787 | n = 0; | |
8788 | for (i = 0; i < count; i++, s++, p++) | |
8789 | { | |
8790 | size_t len; | |
8791 | bfd_vma addr; | |
8792 | ||
8793 | addr = bed->plt_sym_val (i, plt, p); | |
8794 | if (addr == (bfd_vma) -1) | |
8795 | continue; | |
8796 | ||
8797 | *s = **p->sym_ptr_ptr; | |
65a7a66f AM |
8798 | /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since |
8799 | we are defining a symbol, ensure one of them is set. */ | |
8800 | if ((s->flags & BSF_LOCAL) == 0) | |
8801 | s->flags |= BSF_GLOBAL; | |
4c45e5c9 JJ |
8802 | s->section = plt; |
8803 | s->value = addr - plt->vma; | |
8804 | s->name = names; | |
8805 | len = strlen ((*p->sym_ptr_ptr)->name); | |
8806 | memcpy (names, (*p->sym_ptr_ptr)->name, len); | |
8807 | names += len; | |
8808 | memcpy (names, "@plt", sizeof ("@plt")); | |
8809 | names += sizeof ("@plt"); | |
8810 | ++n; | |
8811 | } | |
8812 | ||
8813 | return n; | |
8814 | } | |
3d7f7666 | 8815 | |
c15f73f9 | 8816 | struct elf_symbuf_symbol |
3d7f7666 | 8817 | { |
c15f73f9 JJ |
8818 | unsigned long st_name; /* Symbol name, index in string tbl */ |
8819 | unsigned char st_info; /* Type and binding attributes */ | |
8820 | unsigned char st_other; /* Visibilty, and target specific */ | |
8821 | }; | |
3d7f7666 | 8822 | |
c15f73f9 JJ |
8823 | struct elf_symbuf_head |
8824 | { | |
8825 | struct elf_symbuf_symbol *ssym; | |
8826 | bfd_size_type count; | |
8827 | unsigned int st_shndx; | |
8828 | }; | |
3d7f7666 L |
8829 | |
8830 | struct elf_symbol | |
8831 | { | |
c15f73f9 JJ |
8832 | union |
8833 | { | |
8834 | Elf_Internal_Sym *isym; | |
8835 | struct elf_symbuf_symbol *ssym; | |
8836 | } u; | |
3d7f7666 L |
8837 | const char *name; |
8838 | }; | |
8839 | ||
c15f73f9 JJ |
8840 | /* Sort references to symbols by ascending section number. */ |
8841 | ||
8842 | static int | |
8843 | elf_sort_elf_symbol (const void *arg1, const void *arg2) | |
8844 | { | |
8845 | const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1; | |
8846 | const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2; | |
8847 | ||
8848 | return s1->st_shndx - s2->st_shndx; | |
8849 | } | |
8850 | ||
3d7f7666 L |
8851 | static int |
8852 | elf_sym_name_compare (const void *arg1, const void *arg2) | |
8853 | { | |
8854 | const struct elf_symbol *s1 = (const struct elf_symbol *) arg1; | |
8855 | const struct elf_symbol *s2 = (const struct elf_symbol *) arg2; | |
8856 | return strcmp (s1->name, s2->name); | |
8857 | } | |
8858 | ||
c15f73f9 JJ |
8859 | static struct elf_symbuf_head * |
8860 | elf_create_symbuf (bfd_size_type symcount, Elf_Internal_Sym *isymbuf) | |
8861 | { | |
8862 | Elf_Internal_Sym **ind, **indbufend, **indbuf | |
8863 | = bfd_malloc2 (symcount, sizeof (*indbuf)); | |
8864 | struct elf_symbuf_symbol *ssym; | |
8865 | struct elf_symbuf_head *ssymbuf, *ssymhead; | |
8866 | bfd_size_type i, shndx_count; | |
8867 | ||
8868 | if (indbuf == NULL) | |
8869 | return NULL; | |
8870 | ||
8871 | for (ind = indbuf, i = 0; i < symcount; i++) | |
8872 | if (isymbuf[i].st_shndx != SHN_UNDEF) | |
8873 | *ind++ = &isymbuf[i]; | |
8874 | indbufend = ind; | |
8875 | ||
8876 | qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *), | |
8877 | elf_sort_elf_symbol); | |
8878 | ||
8879 | shndx_count = 0; | |
8880 | if (indbufend > indbuf) | |
8881 | for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++) | |
8882 | if (ind[0]->st_shndx != ind[1]->st_shndx) | |
8883 | shndx_count++; | |
8884 | ||
8885 | ssymbuf = bfd_malloc ((shndx_count + 1) * sizeof (*ssymbuf) | |
8886 | + (indbufend - indbuf) * sizeof (*ssymbuf)); | |
8887 | if (ssymbuf == NULL) | |
8888 | { | |
8889 | free (indbuf); | |
8890 | return NULL; | |
8891 | } | |
8892 | ||
8893 | ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count); | |
8894 | ssymbuf->ssym = NULL; | |
8895 | ssymbuf->count = shndx_count; | |
8896 | ssymbuf->st_shndx = 0; | |
8897 | for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++) | |
8898 | { | |
8899 | if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx) | |
8900 | { | |
8901 | ssymhead++; | |
8902 | ssymhead->ssym = ssym; | |
8903 | ssymhead->count = 0; | |
8904 | ssymhead->st_shndx = (*ind)->st_shndx; | |
8905 | } | |
8906 | ssym->st_name = (*ind)->st_name; | |
8907 | ssym->st_info = (*ind)->st_info; | |
8908 | ssym->st_other = (*ind)->st_other; | |
8909 | ssymhead->count++; | |
8910 | } | |
8911 | BFD_ASSERT ((bfd_size_type) (ssymhead - ssymbuf) == shndx_count); | |
8912 | ||
8913 | free (indbuf); | |
8914 | return ssymbuf; | |
8915 | } | |
8916 | ||
3d7f7666 L |
8917 | /* Check if 2 sections define the same set of local and global |
8918 | symbols. */ | |
8919 | ||
8920 | bfd_boolean | |
c0f00686 L |
8921 | bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2, |
8922 | struct bfd_link_info *info) | |
3d7f7666 L |
8923 | { |
8924 | bfd *bfd1, *bfd2; | |
8925 | const struct elf_backend_data *bed1, *bed2; | |
8926 | Elf_Internal_Shdr *hdr1, *hdr2; | |
8927 | bfd_size_type symcount1, symcount2; | |
8928 | Elf_Internal_Sym *isymbuf1, *isymbuf2; | |
c15f73f9 JJ |
8929 | struct elf_symbuf_head *ssymbuf1, *ssymbuf2; |
8930 | Elf_Internal_Sym *isym, *isymend; | |
8931 | struct elf_symbol *symtable1 = NULL, *symtable2 = NULL; | |
3d7f7666 L |
8932 | bfd_size_type count1, count2, i; |
8933 | int shndx1, shndx2; | |
8934 | bfd_boolean result; | |
8935 | ||
8936 | bfd1 = sec1->owner; | |
8937 | bfd2 = sec2->owner; | |
8938 | ||
8939 | /* If both are .gnu.linkonce sections, they have to have the same | |
8940 | section name. */ | |
0112cd26 NC |
8941 | if (CONST_STRNEQ (sec1->name, ".gnu.linkonce") |
8942 | && CONST_STRNEQ (sec2->name, ".gnu.linkonce")) | |
3d7f7666 L |
8943 | return strcmp (sec1->name + sizeof ".gnu.linkonce", |
8944 | sec2->name + sizeof ".gnu.linkonce") == 0; | |
8945 | ||
8946 | /* Both sections have to be in ELF. */ | |
8947 | if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour | |
8948 | || bfd_get_flavour (bfd2) != bfd_target_elf_flavour) | |
8949 | return FALSE; | |
8950 | ||
8951 | if (elf_section_type (sec1) != elf_section_type (sec2)) | |
8952 | return FALSE; | |
8953 | ||
8954 | if ((elf_section_flags (sec1) & SHF_GROUP) != 0 | |
8955 | && (elf_section_flags (sec2) & SHF_GROUP) != 0) | |
8956 | { | |
8957 | /* If both are members of section groups, they have to have the | |
8958 | same group name. */ | |
8959 | if (strcmp (elf_group_name (sec1), elf_group_name (sec2)) != 0) | |
8960 | return FALSE; | |
8961 | } | |
8962 | ||
8963 | shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1); | |
8964 | shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2); | |
8965 | if (shndx1 == -1 || shndx2 == -1) | |
8966 | return FALSE; | |
8967 | ||
8968 | bed1 = get_elf_backend_data (bfd1); | |
8969 | bed2 = get_elf_backend_data (bfd2); | |
8970 | hdr1 = &elf_tdata (bfd1)->symtab_hdr; | |
8971 | symcount1 = hdr1->sh_size / bed1->s->sizeof_sym; | |
8972 | hdr2 = &elf_tdata (bfd2)->symtab_hdr; | |
8973 | symcount2 = hdr2->sh_size / bed2->s->sizeof_sym; | |
8974 | ||
8975 | if (symcount1 == 0 || symcount2 == 0) | |
8976 | return FALSE; | |
8977 | ||
3d7f7666 | 8978 | result = FALSE; |
c15f73f9 JJ |
8979 | isymbuf1 = NULL; |
8980 | isymbuf2 = NULL; | |
8981 | ssymbuf1 = elf_tdata (bfd1)->symbuf; | |
8982 | ssymbuf2 = elf_tdata (bfd2)->symbuf; | |
3d7f7666 | 8983 | |
c15f73f9 | 8984 | if (ssymbuf1 == NULL) |
c0f00686 L |
8985 | { |
8986 | isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0, | |
8987 | NULL, NULL, NULL); | |
8988 | if (isymbuf1 == NULL) | |
8989 | goto done; | |
c15f73f9 | 8990 | |
c0f00686 | 8991 | if (!info->reduce_memory_overheads) |
c15f73f9 JJ |
8992 | elf_tdata (bfd1)->symbuf = ssymbuf1 |
8993 | = elf_create_symbuf (symcount1, isymbuf1); | |
c0f00686 L |
8994 | } |
8995 | ||
c15f73f9 | 8996 | if (ssymbuf1 == NULL || ssymbuf2 == NULL) |
c0f00686 L |
8997 | { |
8998 | isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0, | |
8999 | NULL, NULL, NULL); | |
9000 | if (isymbuf2 == NULL) | |
9001 | goto done; | |
c15f73f9 JJ |
9002 | |
9003 | if (ssymbuf1 != NULL && !info->reduce_memory_overheads) | |
9004 | elf_tdata (bfd2)->symbuf = ssymbuf2 | |
9005 | = elf_create_symbuf (symcount2, isymbuf2); | |
c0f00686 | 9006 | } |
3d7f7666 | 9007 | |
c15f73f9 | 9008 | if (ssymbuf1 != NULL && ssymbuf2 != NULL) |
3d7f7666 | 9009 | { |
c15f73f9 JJ |
9010 | /* Optimized faster version. */ |
9011 | bfd_size_type lo, hi, mid; | |
9012 | struct elf_symbol *symp; | |
9013 | struct elf_symbuf_symbol *ssym, *ssymend; | |
9014 | ||
9015 | lo = 0; | |
9016 | hi = ssymbuf1->count; | |
9017 | ssymbuf1++; | |
9018 | count1 = 0; | |
9019 | while (lo < hi) | |
3d7f7666 | 9020 | { |
c15f73f9 JJ |
9021 | mid = (lo + hi) / 2; |
9022 | if ((unsigned int) shndx1 < ssymbuf1[mid].st_shndx) | |
9023 | hi = mid; | |
9024 | else if ((unsigned int) shndx1 > ssymbuf1[mid].st_shndx) | |
9025 | lo = mid + 1; | |
9026 | else | |
9027 | { | |
9028 | count1 = ssymbuf1[mid].count; | |
9029 | ssymbuf1 += mid; | |
9030 | break; | |
9031 | } | |
3d7f7666 L |
9032 | } |
9033 | ||
c15f73f9 JJ |
9034 | lo = 0; |
9035 | hi = ssymbuf2->count; | |
9036 | ssymbuf2++; | |
9037 | count2 = 0; | |
9038 | while (lo < hi) | |
9039 | { | |
9040 | mid = (lo + hi) / 2; | |
9041 | if ((unsigned int) shndx2 < ssymbuf2[mid].st_shndx) | |
9042 | hi = mid; | |
9043 | else if ((unsigned int) shndx2 > ssymbuf2[mid].st_shndx) | |
9044 | lo = mid + 1; | |
9045 | else | |
9046 | { | |
9047 | count2 = ssymbuf2[mid].count; | |
9048 | ssymbuf2 += mid; | |
9049 | break; | |
9050 | } | |
9051 | } | |
3d7f7666 | 9052 | |
c15f73f9 JJ |
9053 | if (count1 == 0 || count2 == 0 || count1 != count2) |
9054 | goto done; | |
9055 | ||
9056 | symtable1 = bfd_malloc (count1 * sizeof (struct elf_symbol)); | |
9057 | symtable2 = bfd_malloc (count2 * sizeof (struct elf_symbol)); | |
9058 | if (symtable1 == NULL || symtable2 == NULL) | |
9059 | goto done; | |
9060 | ||
9061 | symp = symtable1; | |
9062 | for (ssym = ssymbuf1->ssym, ssymend = ssym + count1; | |
9063 | ssym < ssymend; ssym++, symp++) | |
3d7f7666 | 9064 | { |
c15f73f9 JJ |
9065 | symp->u.ssym = ssym; |
9066 | symp->name = bfd_elf_string_from_elf_section (bfd1, | |
9067 | hdr1->sh_link, | |
9068 | ssym->st_name); | |
3d7f7666 L |
9069 | } |
9070 | ||
c15f73f9 JJ |
9071 | symp = symtable2; |
9072 | for (ssym = ssymbuf2->ssym, ssymend = ssym + count2; | |
9073 | ssym < ssymend; ssym++, symp++) | |
9074 | { | |
9075 | symp->u.ssym = ssym; | |
9076 | symp->name = bfd_elf_string_from_elf_section (bfd2, | |
9077 | hdr2->sh_link, | |
9078 | ssym->st_name); | |
9079 | } | |
9080 | ||
9081 | /* Sort symbol by name. */ | |
9082 | qsort (symtable1, count1, sizeof (struct elf_symbol), | |
9083 | elf_sym_name_compare); | |
9084 | qsort (symtable2, count1, sizeof (struct elf_symbol), | |
9085 | elf_sym_name_compare); | |
9086 | ||
9087 | for (i = 0; i < count1; i++) | |
9088 | /* Two symbols must have the same binding, type and name. */ | |
9089 | if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info | |
9090 | || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other | |
9091 | || strcmp (symtable1 [i].name, symtable2 [i].name) != 0) | |
9092 | goto done; | |
9093 | ||
9094 | result = TRUE; | |
9095 | goto done; | |
3d7f7666 L |
9096 | } |
9097 | ||
c15f73f9 JJ |
9098 | symtable1 = bfd_malloc (symcount1 * sizeof (struct elf_symbol)); |
9099 | symtable2 = bfd_malloc (symcount2 * sizeof (struct elf_symbol)); | |
9100 | if (symtable1 == NULL || symtable2 == NULL) | |
3d7f7666 L |
9101 | goto done; |
9102 | ||
c15f73f9 JJ |
9103 | /* Count definitions in the section. */ |
9104 | count1 = 0; | |
9105 | for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++) | |
9106 | if (isym->st_shndx == (unsigned int) shndx1) | |
9107 | symtable1[count1++].u.isym = isym; | |
3d7f7666 | 9108 | |
c15f73f9 JJ |
9109 | count2 = 0; |
9110 | for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++) | |
9111 | if (isym->st_shndx == (unsigned int) shndx2) | |
9112 | symtable2[count2++].u.isym = isym; | |
9113 | ||
9114 | if (count1 == 0 || count2 == 0 || count1 != count2) | |
3d7f7666 L |
9115 | goto done; |
9116 | ||
c15f73f9 JJ |
9117 | for (i = 0; i < count1; i++) |
9118 | symtable1[i].name | |
9119 | = bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link, | |
9120 | symtable1[i].u.isym->st_name); | |
9121 | ||
9122 | for (i = 0; i < count2; i++) | |
9123 | symtable2[i].name | |
9124 | = bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link, | |
9125 | symtable2[i].u.isym->st_name); | |
9126 | ||
3d7f7666 L |
9127 | /* Sort symbol by name. */ |
9128 | qsort (symtable1, count1, sizeof (struct elf_symbol), | |
9129 | elf_sym_name_compare); | |
9130 | qsort (symtable2, count1, sizeof (struct elf_symbol), | |
9131 | elf_sym_name_compare); | |
9132 | ||
9133 | for (i = 0; i < count1; i++) | |
9134 | /* Two symbols must have the same binding, type and name. */ | |
c15f73f9 JJ |
9135 | if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info |
9136 | || symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other | |
3d7f7666 L |
9137 | || strcmp (symtable1 [i].name, symtable2 [i].name) != 0) |
9138 | goto done; | |
9139 | ||
9140 | result = TRUE; | |
9141 | ||
9142 | done: | |
9143 | if (symtable1) | |
9144 | free (symtable1); | |
9145 | if (symtable2) | |
9146 | free (symtable2); | |
c15f73f9 JJ |
9147 | if (isymbuf1) |
9148 | free (isymbuf1); | |
9149 | if (isymbuf2) | |
9150 | free (isymbuf2); | |
3d7f7666 L |
9151 | |
9152 | return result; | |
9153 | } | |
3b22753a L |
9154 | |
9155 | /* It is only used by x86-64 so far. */ | |
9156 | asection _bfd_elf_large_com_section | |
9157 | = BFD_FAKE_SECTION (_bfd_elf_large_com_section, | |
f592407e | 9158 | SEC_IS_COMMON, NULL, "LARGE_COMMON", 0); |
ecca9871 L |
9159 | |
9160 | /* Return TRUE if 2 section types are compatible. */ | |
9161 | ||
9162 | bfd_boolean | |
9163 | _bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec, | |
9164 | bfd *bbfd, const asection *bsec) | |
9165 | { | |
9166 | if (asec == NULL | |
9167 | || bsec == NULL | |
9168 | || abfd->xvec->flavour != bfd_target_elf_flavour | |
9169 | || bbfd->xvec->flavour != bfd_target_elf_flavour) | |
9170 | return TRUE; | |
9171 | ||
9172 | return elf_section_type (asec) == elf_section_type (bsec); | |
9173 | } | |
d1036acb L |
9174 | |
9175 | void | |
9176 | _bfd_elf_set_osabi (bfd * abfd, | |
9177 | struct bfd_link_info * link_info ATTRIBUTE_UNUSED) | |
9178 | { | |
9179 | Elf_Internal_Ehdr * i_ehdrp; /* ELF file header, internal form. */ | |
9180 | ||
9181 | i_ehdrp = elf_elfheader (abfd); | |
9182 | ||
9183 | i_ehdrp->e_ident[EI_OSABI] = get_elf_backend_data (abfd)->elf_osabi; | |
9184 | } | |
fcb93ecf PB |
9185 | |
9186 | ||
9187 | /* Return TRUE for ELF symbol types that represent functions. | |
9188 | This is the default version of this function, which is sufficient for | |
9189 | most targets. It returns true if TYPE is STT_FUNC. */ | |
9190 | ||
9191 | bfd_boolean | |
9192 | _bfd_elf_is_function_type (unsigned int type) | |
9193 | { | |
9194 | return (type == STT_FUNC); | |
9195 | } |