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