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