]>
Commit | Line | Data |
---|---|---|
252b5132 | 1 | /* ELF executable support for BFD. |
340b6d91 | 2 | |
2571583a | 3 | Copyright (C) 1993-2017 Free Software Foundation, Inc. |
252b5132 | 4 | |
5e8d7549 | 5 | This file is part of BFD, the Binary File Descriptor library. |
252b5132 | 6 | |
5e8d7549 NC |
7 | This program is free software; you can redistribute it and/or modify |
8 | it under the terms of the GNU General Public License as published by | |
cd123cb7 | 9 | the Free Software Foundation; either version 3 of the License, or |
5e8d7549 | 10 | (at your option) any later version. |
252b5132 | 11 | |
5e8d7549 NC |
12 | This program is distributed in the hope that it will be useful, |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
252b5132 | 16 | |
5e8d7549 | 17 | You should have received a copy of the GNU General Public License |
b34976b6 | 18 | along with this program; if not, write to the Free Software |
cd123cb7 NC |
19 | Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, |
20 | MA 02110-1301, USA. */ | |
21 | ||
252b5132 | 22 | |
1b74d094 BW |
23 | /* |
24 | SECTION | |
252b5132 RH |
25 | ELF backends |
26 | ||
27 | BFD support for ELF formats is being worked on. | |
28 | Currently, the best supported back ends are for sparc and i386 | |
29 | (running svr4 or Solaris 2). | |
30 | ||
31 | Documentation of the internals of the support code still needs | |
32 | to be written. The code is changing quickly enough that we | |
661a3fd4 | 33 | haven't bothered yet. */ |
252b5132 | 34 | |
7ee38065 MS |
35 | /* For sparc64-cross-sparc32. */ |
36 | #define _SYSCALL32 | |
252b5132 | 37 | #include "sysdep.h" |
3db64b00 | 38 | #include "bfd.h" |
252b5132 RH |
39 | #include "bfdlink.h" |
40 | #include "libbfd.h" | |
41 | #define ARCH_SIZE 0 | |
42 | #include "elf-bfd.h" | |
e0e8c97f | 43 | #include "libiberty.h" |
ff59fc36 | 44 | #include "safe-ctype.h" |
de64ce13 | 45 | #include "elf-linux-core.h" |
252b5132 | 46 | |
8bc7f138 L |
47 | #ifdef CORE_HEADER |
48 | #include CORE_HEADER | |
49 | #endif | |
50 | ||
217aa764 | 51 | static int elf_sort_sections (const void *, const void *); |
c84fca4d | 52 | static bfd_boolean assign_file_positions_except_relocs (bfd *, struct bfd_link_info *); |
217aa764 | 53 | static bfd_boolean prep_headers (bfd *); |
ef10c3ac | 54 | static bfd_boolean swap_out_syms (bfd *, struct elf_strtab_hash **, int) ; |
718175fa JK |
55 | static bfd_boolean elf_read_notes (bfd *, file_ptr, bfd_size_type) ; |
56 | static bfd_boolean elf_parse_notes (bfd *abfd, char *buf, size_t size, | |
57 | file_ptr offset); | |
50b2bdb7 | 58 | |
252b5132 RH |
59 | /* Swap version information in and out. The version information is |
60 | currently size independent. If that ever changes, this code will | |
61 | need to move into elfcode.h. */ | |
62 | ||
63 | /* Swap in a Verdef structure. */ | |
64 | ||
65 | void | |
217aa764 AM |
66 | _bfd_elf_swap_verdef_in (bfd *abfd, |
67 | const Elf_External_Verdef *src, | |
68 | Elf_Internal_Verdef *dst) | |
252b5132 | 69 | { |
dc810e39 AM |
70 | dst->vd_version = H_GET_16 (abfd, src->vd_version); |
71 | dst->vd_flags = H_GET_16 (abfd, src->vd_flags); | |
72 | dst->vd_ndx = H_GET_16 (abfd, src->vd_ndx); | |
73 | dst->vd_cnt = H_GET_16 (abfd, src->vd_cnt); | |
74 | dst->vd_hash = H_GET_32 (abfd, src->vd_hash); | |
75 | dst->vd_aux = H_GET_32 (abfd, src->vd_aux); | |
76 | dst->vd_next = H_GET_32 (abfd, src->vd_next); | |
252b5132 RH |
77 | } |
78 | ||
79 | /* Swap out a Verdef structure. */ | |
80 | ||
81 | void | |
217aa764 AM |
82 | _bfd_elf_swap_verdef_out (bfd *abfd, |
83 | const Elf_Internal_Verdef *src, | |
84 | Elf_External_Verdef *dst) | |
252b5132 | 85 | { |
dc810e39 AM |
86 | H_PUT_16 (abfd, src->vd_version, dst->vd_version); |
87 | H_PUT_16 (abfd, src->vd_flags, dst->vd_flags); | |
88 | H_PUT_16 (abfd, src->vd_ndx, dst->vd_ndx); | |
89 | H_PUT_16 (abfd, src->vd_cnt, dst->vd_cnt); | |
90 | H_PUT_32 (abfd, src->vd_hash, dst->vd_hash); | |
91 | H_PUT_32 (abfd, src->vd_aux, dst->vd_aux); | |
92 | H_PUT_32 (abfd, src->vd_next, dst->vd_next); | |
252b5132 RH |
93 | } |
94 | ||
95 | /* Swap in a Verdaux structure. */ | |
96 | ||
97 | void | |
217aa764 AM |
98 | _bfd_elf_swap_verdaux_in (bfd *abfd, |
99 | const Elf_External_Verdaux *src, | |
100 | Elf_Internal_Verdaux *dst) | |
252b5132 | 101 | { |
dc810e39 AM |
102 | dst->vda_name = H_GET_32 (abfd, src->vda_name); |
103 | dst->vda_next = H_GET_32 (abfd, src->vda_next); | |
252b5132 RH |
104 | } |
105 | ||
106 | /* Swap out a Verdaux structure. */ | |
107 | ||
108 | void | |
217aa764 AM |
109 | _bfd_elf_swap_verdaux_out (bfd *abfd, |
110 | const Elf_Internal_Verdaux *src, | |
111 | Elf_External_Verdaux *dst) | |
252b5132 | 112 | { |
dc810e39 AM |
113 | H_PUT_32 (abfd, src->vda_name, dst->vda_name); |
114 | H_PUT_32 (abfd, src->vda_next, dst->vda_next); | |
252b5132 RH |
115 | } |
116 | ||
117 | /* Swap in a Verneed structure. */ | |
118 | ||
119 | void | |
217aa764 AM |
120 | _bfd_elf_swap_verneed_in (bfd *abfd, |
121 | const Elf_External_Verneed *src, | |
122 | Elf_Internal_Verneed *dst) | |
252b5132 | 123 | { |
dc810e39 AM |
124 | dst->vn_version = H_GET_16 (abfd, src->vn_version); |
125 | dst->vn_cnt = H_GET_16 (abfd, src->vn_cnt); | |
126 | dst->vn_file = H_GET_32 (abfd, src->vn_file); | |
127 | dst->vn_aux = H_GET_32 (abfd, src->vn_aux); | |
128 | dst->vn_next = H_GET_32 (abfd, src->vn_next); | |
252b5132 RH |
129 | } |
130 | ||
131 | /* Swap out a Verneed structure. */ | |
132 | ||
133 | void | |
217aa764 AM |
134 | _bfd_elf_swap_verneed_out (bfd *abfd, |
135 | const Elf_Internal_Verneed *src, | |
136 | Elf_External_Verneed *dst) | |
252b5132 | 137 | { |
dc810e39 AM |
138 | H_PUT_16 (abfd, src->vn_version, dst->vn_version); |
139 | H_PUT_16 (abfd, src->vn_cnt, dst->vn_cnt); | |
140 | H_PUT_32 (abfd, src->vn_file, dst->vn_file); | |
141 | H_PUT_32 (abfd, src->vn_aux, dst->vn_aux); | |
142 | H_PUT_32 (abfd, src->vn_next, dst->vn_next); | |
252b5132 RH |
143 | } |
144 | ||
145 | /* Swap in a Vernaux structure. */ | |
146 | ||
147 | void | |
217aa764 AM |
148 | _bfd_elf_swap_vernaux_in (bfd *abfd, |
149 | const Elf_External_Vernaux *src, | |
150 | Elf_Internal_Vernaux *dst) | |
252b5132 | 151 | { |
dc810e39 AM |
152 | dst->vna_hash = H_GET_32 (abfd, src->vna_hash); |
153 | dst->vna_flags = H_GET_16 (abfd, src->vna_flags); | |
154 | dst->vna_other = H_GET_16 (abfd, src->vna_other); | |
155 | dst->vna_name = H_GET_32 (abfd, src->vna_name); | |
156 | dst->vna_next = H_GET_32 (abfd, src->vna_next); | |
252b5132 RH |
157 | } |
158 | ||
159 | /* Swap out a Vernaux structure. */ | |
160 | ||
161 | void | |
217aa764 AM |
162 | _bfd_elf_swap_vernaux_out (bfd *abfd, |
163 | const Elf_Internal_Vernaux *src, | |
164 | Elf_External_Vernaux *dst) | |
252b5132 | 165 | { |
dc810e39 AM |
166 | H_PUT_32 (abfd, src->vna_hash, dst->vna_hash); |
167 | H_PUT_16 (abfd, src->vna_flags, dst->vna_flags); | |
168 | H_PUT_16 (abfd, src->vna_other, dst->vna_other); | |
169 | H_PUT_32 (abfd, src->vna_name, dst->vna_name); | |
170 | H_PUT_32 (abfd, src->vna_next, dst->vna_next); | |
252b5132 RH |
171 | } |
172 | ||
173 | /* Swap in a Versym structure. */ | |
174 | ||
175 | void | |
217aa764 AM |
176 | _bfd_elf_swap_versym_in (bfd *abfd, |
177 | const Elf_External_Versym *src, | |
178 | Elf_Internal_Versym *dst) | |
252b5132 | 179 | { |
dc810e39 | 180 | dst->vs_vers = H_GET_16 (abfd, src->vs_vers); |
252b5132 RH |
181 | } |
182 | ||
183 | /* Swap out a Versym structure. */ | |
184 | ||
185 | void | |
217aa764 AM |
186 | _bfd_elf_swap_versym_out (bfd *abfd, |
187 | const Elf_Internal_Versym *src, | |
188 | Elf_External_Versym *dst) | |
252b5132 | 189 | { |
dc810e39 | 190 | H_PUT_16 (abfd, src->vs_vers, dst->vs_vers); |
252b5132 RH |
191 | } |
192 | ||
193 | /* Standard ELF hash function. Do not change this function; you will | |
194 | cause invalid hash tables to be generated. */ | |
3a99b017 | 195 | |
252b5132 | 196 | unsigned long |
217aa764 | 197 | bfd_elf_hash (const char *namearg) |
252b5132 | 198 | { |
3a99b017 | 199 | const unsigned char *name = (const unsigned char *) namearg; |
252b5132 RH |
200 | unsigned long h = 0; |
201 | unsigned long g; | |
202 | int ch; | |
203 | ||
204 | while ((ch = *name++) != '\0') | |
205 | { | |
206 | h = (h << 4) + ch; | |
207 | if ((g = (h & 0xf0000000)) != 0) | |
208 | { | |
209 | h ^= g >> 24; | |
210 | /* The ELF ABI says `h &= ~g', but this is equivalent in | |
211 | this case and on some machines one insn instead of two. */ | |
212 | h ^= g; | |
213 | } | |
214 | } | |
32dfa85d | 215 | return h & 0xffffffff; |
252b5132 RH |
216 | } |
217 | ||
fdc90cb4 JJ |
218 | /* DT_GNU_HASH hash function. Do not change this function; you will |
219 | cause invalid hash tables to be generated. */ | |
220 | ||
221 | unsigned long | |
222 | bfd_elf_gnu_hash (const char *namearg) | |
223 | { | |
224 | const unsigned char *name = (const unsigned char *) namearg; | |
225 | unsigned long h = 5381; | |
226 | unsigned char ch; | |
227 | ||
228 | while ((ch = *name++) != '\0') | |
229 | h = (h << 5) + h + ch; | |
230 | return h & 0xffffffff; | |
231 | } | |
232 | ||
0c8d6e5c AM |
233 | /* Create a tdata field OBJECT_SIZE bytes in length, zeroed out and with |
234 | the object_id field of an elf_obj_tdata field set to OBJECT_ID. */ | |
b34976b6 | 235 | bfd_boolean |
0c8d6e5c | 236 | bfd_elf_allocate_object (bfd *abfd, |
0ffa91dd | 237 | size_t object_size, |
4dfe6ac6 | 238 | enum elf_target_id object_id) |
252b5132 | 239 | { |
0ffa91dd NC |
240 | BFD_ASSERT (object_size >= sizeof (struct elf_obj_tdata)); |
241 | abfd->tdata.any = bfd_zalloc (abfd, object_size); | |
242 | if (abfd->tdata.any == NULL) | |
243 | return FALSE; | |
252b5132 | 244 | |
0ffa91dd | 245 | elf_object_id (abfd) = object_id; |
c0355132 AM |
246 | if (abfd->direction != read_direction) |
247 | { | |
248 | struct output_elf_obj_tdata *o = bfd_zalloc (abfd, sizeof *o); | |
249 | if (o == NULL) | |
250 | return FALSE; | |
251 | elf_tdata (abfd)->o = o; | |
252 | elf_program_header_size (abfd) = (bfd_size_type) -1; | |
253 | } | |
b34976b6 | 254 | return TRUE; |
252b5132 RH |
255 | } |
256 | ||
0ffa91dd NC |
257 | |
258 | bfd_boolean | |
ae95ffa6 | 259 | bfd_elf_make_object (bfd *abfd) |
0ffa91dd | 260 | { |
ae95ffa6 | 261 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
0ffa91dd | 262 | return bfd_elf_allocate_object (abfd, sizeof (struct elf_obj_tdata), |
ae95ffa6 | 263 | bed->target_id); |
0ffa91dd NC |
264 | } |
265 | ||
b34976b6 | 266 | bfd_boolean |
217aa764 | 267 | bfd_elf_mkcorefile (bfd *abfd) |
252b5132 | 268 | { |
c044fabd | 269 | /* I think this can be done just like an object file. */ |
228e534f AM |
270 | if (!abfd->xvec->_bfd_set_format[(int) bfd_object] (abfd)) |
271 | return FALSE; | |
272 | elf_tdata (abfd)->core = bfd_zalloc (abfd, sizeof (*elf_tdata (abfd)->core)); | |
273 | return elf_tdata (abfd)->core != NULL; | |
252b5132 RH |
274 | } |
275 | ||
72a80a16 | 276 | static char * |
217aa764 | 277 | bfd_elf_get_str_section (bfd *abfd, unsigned int shindex) |
252b5132 RH |
278 | { |
279 | Elf_Internal_Shdr **i_shdrp; | |
f075ee0c | 280 | bfd_byte *shstrtab = NULL; |
dc810e39 AM |
281 | file_ptr offset; |
282 | bfd_size_type shstrtabsize; | |
252b5132 RH |
283 | |
284 | i_shdrp = elf_elfsections (abfd); | |
74f2e02b AM |
285 | if (i_shdrp == 0 |
286 | || shindex >= elf_numsections (abfd) | |
287 | || i_shdrp[shindex] == 0) | |
f075ee0c | 288 | return NULL; |
252b5132 | 289 | |
f075ee0c | 290 | shstrtab = i_shdrp[shindex]->contents; |
252b5132 RH |
291 | if (shstrtab == NULL) |
292 | { | |
c044fabd | 293 | /* No cached one, attempt to read, and cache what we read. */ |
252b5132 RH |
294 | offset = i_shdrp[shindex]->sh_offset; |
295 | shstrtabsize = i_shdrp[shindex]->sh_size; | |
c6c60d09 JJ |
296 | |
297 | /* Allocate and clear an extra byte at the end, to prevent crashes | |
298 | in case the string table is not terminated. */ | |
3471d59d | 299 | if (shstrtabsize + 1 <= 1 |
06614111 NC |
300 | || bfd_seek (abfd, offset, SEEK_SET) != 0 |
301 | || (shstrtab = (bfd_byte *) bfd_alloc (abfd, shstrtabsize + 1)) == NULL) | |
c6c60d09 JJ |
302 | shstrtab = NULL; |
303 | else if (bfd_bread (shstrtab, shstrtabsize, abfd) != shstrtabsize) | |
304 | { | |
305 | if (bfd_get_error () != bfd_error_system_call) | |
306 | bfd_set_error (bfd_error_file_truncated); | |
06614111 | 307 | bfd_release (abfd, shstrtab); |
c6c60d09 | 308 | shstrtab = NULL; |
3471d59d CC |
309 | /* Once we've failed to read it, make sure we don't keep |
310 | trying. Otherwise, we'll keep allocating space for | |
311 | the string table over and over. */ | |
312 | i_shdrp[shindex]->sh_size = 0; | |
c6c60d09 JJ |
313 | } |
314 | else | |
315 | shstrtab[shstrtabsize] = '\0'; | |
217aa764 | 316 | i_shdrp[shindex]->contents = shstrtab; |
252b5132 | 317 | } |
f075ee0c | 318 | return (char *) shstrtab; |
252b5132 RH |
319 | } |
320 | ||
321 | char * | |
217aa764 AM |
322 | bfd_elf_string_from_elf_section (bfd *abfd, |
323 | unsigned int shindex, | |
324 | unsigned int strindex) | |
252b5132 RH |
325 | { |
326 | Elf_Internal_Shdr *hdr; | |
327 | ||
328 | if (strindex == 0) | |
329 | return ""; | |
330 | ||
74f2e02b AM |
331 | if (elf_elfsections (abfd) == NULL || shindex >= elf_numsections (abfd)) |
332 | return NULL; | |
333 | ||
252b5132 RH |
334 | hdr = elf_elfsections (abfd)[shindex]; |
335 | ||
06614111 NC |
336 | if (hdr->contents == NULL) |
337 | { | |
338 | if (hdr->sh_type != SHT_STRTAB && hdr->sh_type < SHT_LOOS) | |
339 | { | |
340 | /* PR 17512: file: f057ec89. */ | |
695344c0 | 341 | /* xgettext:c-format */ |
63a5468a AM |
342 | _bfd_error_handler (_("%B: attempt to load strings from" |
343 | " a non-string section (number %d)"), | |
06614111 NC |
344 | abfd, shindex); |
345 | return NULL; | |
346 | } | |
b1fa9dd6 | 347 | |
06614111 NC |
348 | if (bfd_elf_get_str_section (abfd, shindex) == NULL) |
349 | return NULL; | |
350 | } | |
252b5132 RH |
351 | |
352 | if (strindex >= hdr->sh_size) | |
353 | { | |
1b3a8575 | 354 | unsigned int shstrndx = elf_elfheader(abfd)->e_shstrndx; |
4eca0228 | 355 | _bfd_error_handler |
695344c0 | 356 | /* xgettext:c-format */ |
d42c267e AM |
357 | (_("%B: invalid string offset %u >= %Lu for section `%s'"), |
358 | abfd, strindex, hdr->sh_size, | |
1b3a8575 | 359 | (shindex == shstrndx && strindex == hdr->sh_name |
252b5132 | 360 | ? ".shstrtab" |
1b3a8575 | 361 | : bfd_elf_string_from_elf_section (abfd, shstrndx, hdr->sh_name))); |
45b222d6 | 362 | return NULL; |
252b5132 RH |
363 | } |
364 | ||
365 | return ((char *) hdr->contents) + strindex; | |
366 | } | |
367 | ||
6cdc0ccc AM |
368 | /* Read and convert symbols to internal format. |
369 | SYMCOUNT specifies the number of symbols to read, starting from | |
370 | symbol SYMOFFSET. If any of INTSYM_BUF, EXTSYM_BUF or EXTSHNDX_BUF | |
371 | are non-NULL, they are used to store the internal symbols, external | |
b7c368d0 NC |
372 | symbols, and symbol section index extensions, respectively. |
373 | Returns a pointer to the internal symbol buffer (malloced if necessary) | |
374 | or NULL if there were no symbols or some kind of problem. */ | |
6cdc0ccc AM |
375 | |
376 | Elf_Internal_Sym * | |
217aa764 AM |
377 | bfd_elf_get_elf_syms (bfd *ibfd, |
378 | Elf_Internal_Shdr *symtab_hdr, | |
379 | size_t symcount, | |
380 | size_t symoffset, | |
381 | Elf_Internal_Sym *intsym_buf, | |
382 | void *extsym_buf, | |
383 | Elf_External_Sym_Shndx *extshndx_buf) | |
6cdc0ccc AM |
384 | { |
385 | Elf_Internal_Shdr *shndx_hdr; | |
217aa764 | 386 | void *alloc_ext; |
df622259 | 387 | const bfd_byte *esym; |
6cdc0ccc AM |
388 | Elf_External_Sym_Shndx *alloc_extshndx; |
389 | Elf_External_Sym_Shndx *shndx; | |
4dd07732 | 390 | Elf_Internal_Sym *alloc_intsym; |
6cdc0ccc AM |
391 | Elf_Internal_Sym *isym; |
392 | Elf_Internal_Sym *isymend; | |
9c5bfbb7 | 393 | const struct elf_backend_data *bed; |
6cdc0ccc AM |
394 | size_t extsym_size; |
395 | bfd_size_type amt; | |
396 | file_ptr pos; | |
397 | ||
e44a2c9c AM |
398 | if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour) |
399 | abort (); | |
400 | ||
6cdc0ccc AM |
401 | if (symcount == 0) |
402 | return intsym_buf; | |
403 | ||
404 | /* Normal syms might have section extension entries. */ | |
405 | shndx_hdr = NULL; | |
6a40cf0c NC |
406 | if (elf_symtab_shndx_list (ibfd) != NULL) |
407 | { | |
408 | elf_section_list * entry; | |
409 | Elf_Internal_Shdr **sections = elf_elfsections (ibfd); | |
410 | ||
411 | /* Find an index section that is linked to this symtab section. */ | |
412 | for (entry = elf_symtab_shndx_list (ibfd); entry != NULL; entry = entry->next) | |
315350be NC |
413 | { |
414 | /* PR 20063. */ | |
415 | if (entry->hdr.sh_link >= elf_numsections (ibfd)) | |
416 | continue; | |
417 | ||
418 | if (sections[entry->hdr.sh_link] == symtab_hdr) | |
419 | { | |
420 | shndx_hdr = & entry->hdr; | |
421 | break; | |
422 | }; | |
423 | } | |
6a40cf0c NC |
424 | |
425 | if (shndx_hdr == NULL) | |
426 | { | |
427 | if (symtab_hdr == & elf_symtab_hdr (ibfd)) | |
428 | /* Not really accurate, but this was how the old code used to work. */ | |
429 | shndx_hdr = & elf_symtab_shndx_list (ibfd)->hdr; | |
430 | /* Otherwise we do nothing. The assumption is that | |
431 | the index table will not be needed. */ | |
432 | } | |
433 | } | |
6cdc0ccc AM |
434 | |
435 | /* Read the symbols. */ | |
436 | alloc_ext = NULL; | |
437 | alloc_extshndx = NULL; | |
4dd07732 | 438 | alloc_intsym = NULL; |
6cdc0ccc AM |
439 | bed = get_elf_backend_data (ibfd); |
440 | extsym_size = bed->s->sizeof_sym; | |
ef53be89 | 441 | amt = (bfd_size_type) symcount * extsym_size; |
6cdc0ccc AM |
442 | pos = symtab_hdr->sh_offset + symoffset * extsym_size; |
443 | if (extsym_buf == NULL) | |
444 | { | |
d0fb9a8d | 445 | alloc_ext = bfd_malloc2 (symcount, extsym_size); |
6cdc0ccc AM |
446 | extsym_buf = alloc_ext; |
447 | } | |
448 | if (extsym_buf == NULL | |
449 | || bfd_seek (ibfd, pos, SEEK_SET) != 0 | |
450 | || bfd_bread (extsym_buf, amt, ibfd) != amt) | |
451 | { | |
452 | intsym_buf = NULL; | |
453 | goto out; | |
454 | } | |
455 | ||
456 | if (shndx_hdr == NULL || shndx_hdr->sh_size == 0) | |
457 | extshndx_buf = NULL; | |
458 | else | |
459 | { | |
ef53be89 | 460 | amt = (bfd_size_type) symcount * sizeof (Elf_External_Sym_Shndx); |
6cdc0ccc AM |
461 | pos = shndx_hdr->sh_offset + symoffset * sizeof (Elf_External_Sym_Shndx); |
462 | if (extshndx_buf == NULL) | |
463 | { | |
a50b1753 NC |
464 | alloc_extshndx = (Elf_External_Sym_Shndx *) |
465 | bfd_malloc2 (symcount, sizeof (Elf_External_Sym_Shndx)); | |
6cdc0ccc AM |
466 | extshndx_buf = alloc_extshndx; |
467 | } | |
468 | if (extshndx_buf == NULL | |
469 | || bfd_seek (ibfd, pos, SEEK_SET) != 0 | |
470 | || bfd_bread (extshndx_buf, amt, ibfd) != amt) | |
471 | { | |
472 | intsym_buf = NULL; | |
473 | goto out; | |
474 | } | |
475 | } | |
476 | ||
477 | if (intsym_buf == NULL) | |
478 | { | |
a50b1753 NC |
479 | alloc_intsym = (Elf_Internal_Sym *) |
480 | bfd_malloc2 (symcount, sizeof (Elf_Internal_Sym)); | |
4dd07732 | 481 | intsym_buf = alloc_intsym; |
6cdc0ccc AM |
482 | if (intsym_buf == NULL) |
483 | goto out; | |
484 | } | |
485 | ||
486 | /* Convert the symbols to internal form. */ | |
487 | isymend = intsym_buf + symcount; | |
a50b1753 NC |
488 | for (esym = (const bfd_byte *) extsym_buf, isym = intsym_buf, |
489 | shndx = extshndx_buf; | |
6cdc0ccc AM |
490 | isym < isymend; |
491 | esym += extsym_size, isym++, shndx = shndx != NULL ? shndx + 1 : NULL) | |
8384fb8f AM |
492 | if (!(*bed->s->swap_symbol_in) (ibfd, esym, shndx, isym)) |
493 | { | |
494 | symoffset += (esym - (bfd_byte *) extsym_buf) / extsym_size; | |
695344c0 | 495 | /* xgettext:c-format */ |
63a5468a AM |
496 | _bfd_error_handler (_("%B symbol number %lu references" |
497 | " nonexistent SHT_SYMTAB_SHNDX section"), | |
4eca0228 | 498 | ibfd, (unsigned long) symoffset); |
4dd07732 AM |
499 | if (alloc_intsym != NULL) |
500 | free (alloc_intsym); | |
8384fb8f AM |
501 | intsym_buf = NULL; |
502 | goto out; | |
503 | } | |
6cdc0ccc AM |
504 | |
505 | out: | |
506 | if (alloc_ext != NULL) | |
507 | free (alloc_ext); | |
508 | if (alloc_extshndx != NULL) | |
509 | free (alloc_extshndx); | |
510 | ||
511 | return intsym_buf; | |
512 | } | |
513 | ||
5cab59f6 AM |
514 | /* Look up a symbol name. */ |
515 | const char * | |
be8dd2ca AM |
516 | bfd_elf_sym_name (bfd *abfd, |
517 | Elf_Internal_Shdr *symtab_hdr, | |
26c61ae5 L |
518 | Elf_Internal_Sym *isym, |
519 | asection *sym_sec) | |
5cab59f6 | 520 | { |
26c61ae5 | 521 | const char *name; |
5cab59f6 | 522 | unsigned int iname = isym->st_name; |
be8dd2ca | 523 | unsigned int shindex = symtab_hdr->sh_link; |
26c61ae5 | 524 | |
138f35cc JJ |
525 | if (iname == 0 && ELF_ST_TYPE (isym->st_info) == STT_SECTION |
526 | /* Check for a bogus st_shndx to avoid crashing. */ | |
4fbb74a6 | 527 | && isym->st_shndx < elf_numsections (abfd)) |
5cab59f6 AM |
528 | { |
529 | iname = elf_elfsections (abfd)[isym->st_shndx]->sh_name; | |
530 | shindex = elf_elfheader (abfd)->e_shstrndx; | |
531 | } | |
532 | ||
26c61ae5 L |
533 | name = bfd_elf_string_from_elf_section (abfd, shindex, iname); |
534 | if (name == NULL) | |
535 | name = "(null)"; | |
536 | else if (sym_sec && *name == '\0') | |
537 | name = bfd_section_name (abfd, sym_sec); | |
538 | ||
539 | return name; | |
5cab59f6 AM |
540 | } |
541 | ||
dbb410c3 AM |
542 | /* Elf_Internal_Shdr->contents is an array of these for SHT_GROUP |
543 | sections. The first element is the flags, the rest are section | |
544 | pointers. */ | |
545 | ||
546 | typedef union elf_internal_group { | |
547 | Elf_Internal_Shdr *shdr; | |
548 | unsigned int flags; | |
549 | } Elf_Internal_Group; | |
550 | ||
b885599b AM |
551 | /* Return the name of the group signature symbol. Why isn't the |
552 | signature just a string? */ | |
553 | ||
554 | static const char * | |
217aa764 | 555 | group_signature (bfd *abfd, Elf_Internal_Shdr *ghdr) |
b885599b | 556 | { |
9dce4196 | 557 | Elf_Internal_Shdr *hdr; |
9dce4196 AM |
558 | unsigned char esym[sizeof (Elf64_External_Sym)]; |
559 | Elf_External_Sym_Shndx eshndx; | |
560 | Elf_Internal_Sym isym; | |
b885599b | 561 | |
13792e9d L |
562 | /* First we need to ensure the symbol table is available. Make sure |
563 | that it is a symbol table section. */ | |
4fbb74a6 AM |
564 | if (ghdr->sh_link >= elf_numsections (abfd)) |
565 | return NULL; | |
13792e9d L |
566 | hdr = elf_elfsections (abfd) [ghdr->sh_link]; |
567 | if (hdr->sh_type != SHT_SYMTAB | |
568 | || ! bfd_section_from_shdr (abfd, ghdr->sh_link)) | |
b885599b AM |
569 | return NULL; |
570 | ||
9dce4196 AM |
571 | /* Go read the symbol. */ |
572 | hdr = &elf_tdata (abfd)->symtab_hdr; | |
6cdc0ccc AM |
573 | if (bfd_elf_get_elf_syms (abfd, hdr, 1, ghdr->sh_info, |
574 | &isym, esym, &eshndx) == NULL) | |
b885599b | 575 | return NULL; |
9dce4196 | 576 | |
26c61ae5 | 577 | return bfd_elf_sym_name (abfd, hdr, &isym, NULL); |
b885599b AM |
578 | } |
579 | ||
dbb410c3 AM |
580 | /* Set next_in_group list pointer, and group name for NEWSECT. */ |
581 | ||
b34976b6 | 582 | static bfd_boolean |
217aa764 | 583 | setup_group (bfd *abfd, Elf_Internal_Shdr *hdr, asection *newsect) |
dbb410c3 AM |
584 | { |
585 | unsigned int num_group = elf_tdata (abfd)->num_group; | |
586 | ||
587 | /* If num_group is zero, read in all SHT_GROUP sections. The count | |
588 | is set to -1 if there are no SHT_GROUP sections. */ | |
589 | if (num_group == 0) | |
590 | { | |
591 | unsigned int i, shnum; | |
592 | ||
593 | /* First count the number of groups. If we have a SHT_GROUP | |
594 | section with just a flag word (ie. sh_size is 4), ignore it. */ | |
9ad5cbcf | 595 | shnum = elf_numsections (abfd); |
dbb410c3 | 596 | num_group = 0; |
08a40648 | 597 | |
44534af3 | 598 | #define IS_VALID_GROUP_SECTION_HEADER(shdr, minsize) \ |
1783205a | 599 | ( (shdr)->sh_type == SHT_GROUP \ |
44534af3 | 600 | && (shdr)->sh_size >= minsize \ |
1783205a NC |
601 | && (shdr)->sh_entsize == GRP_ENTRY_SIZE \ |
602 | && ((shdr)->sh_size % GRP_ENTRY_SIZE) == 0) | |
08a40648 | 603 | |
dbb410c3 AM |
604 | for (i = 0; i < shnum; i++) |
605 | { | |
606 | Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i]; | |
1783205a | 607 | |
44534af3 | 608 | if (IS_VALID_GROUP_SECTION_HEADER (shdr, 2 * GRP_ENTRY_SIZE)) |
dbb410c3 AM |
609 | num_group += 1; |
610 | } | |
611 | ||
612 | if (num_group == 0) | |
20dbb49d L |
613 | { |
614 | num_group = (unsigned) -1; | |
615 | elf_tdata (abfd)->num_group = num_group; | |
ce497010 | 616 | elf_tdata (abfd)->group_sect_ptr = NULL; |
20dbb49d L |
617 | } |
618 | else | |
dbb410c3 AM |
619 | { |
620 | /* We keep a list of elf section headers for group sections, | |
621 | so we can find them quickly. */ | |
20dbb49d | 622 | bfd_size_type amt; |
d0fb9a8d | 623 | |
20dbb49d | 624 | elf_tdata (abfd)->num_group = num_group; |
a50b1753 NC |
625 | elf_tdata (abfd)->group_sect_ptr = (Elf_Internal_Shdr **) |
626 | bfd_alloc2 (abfd, num_group, sizeof (Elf_Internal_Shdr *)); | |
dbb410c3 | 627 | if (elf_tdata (abfd)->group_sect_ptr == NULL) |
b34976b6 | 628 | return FALSE; |
ce497010 | 629 | memset (elf_tdata (abfd)->group_sect_ptr, 0, num_group * sizeof (Elf_Internal_Shdr *)); |
dbb410c3 | 630 | num_group = 0; |
ce497010 | 631 | |
dbb410c3 AM |
632 | for (i = 0; i < shnum; i++) |
633 | { | |
634 | Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i]; | |
1783205a | 635 | |
44534af3 | 636 | if (IS_VALID_GROUP_SECTION_HEADER (shdr, 2 * GRP_ENTRY_SIZE)) |
dbb410c3 | 637 | { |
973ffd63 | 638 | unsigned char *src; |
dbb410c3 AM |
639 | Elf_Internal_Group *dest; |
640 | ||
78e8a2ff JM |
641 | /* Make sure the group section has a BFD section |
642 | attached to it. */ | |
643 | if (!bfd_section_from_shdr (abfd, i)) | |
644 | return FALSE; | |
645 | ||
dbb410c3 AM |
646 | /* Add to list of sections. */ |
647 | elf_tdata (abfd)->group_sect_ptr[num_group] = shdr; | |
648 | num_group += 1; | |
649 | ||
650 | /* Read the raw contents. */ | |
651 | BFD_ASSERT (sizeof (*dest) >= 4); | |
652 | amt = shdr->sh_size * sizeof (*dest) / 4; | |
a50b1753 NC |
653 | shdr->contents = (unsigned char *) |
654 | bfd_alloc2 (abfd, shdr->sh_size, sizeof (*dest) / 4); | |
1783205a NC |
655 | /* PR binutils/4110: Handle corrupt group headers. */ |
656 | if (shdr->contents == NULL) | |
657 | { | |
658 | _bfd_error_handler | |
695344c0 | 659 | /* xgettext:c-format */ |
63a5468a | 660 | (_("%B: corrupt size field in group section" |
76cfced5 | 661 | " header: %#Lx"), abfd, shdr->sh_size); |
1783205a | 662 | bfd_set_error (bfd_error_bad_value); |
493a3386 NC |
663 | -- num_group; |
664 | continue; | |
1783205a NC |
665 | } |
666 | ||
667 | memset (shdr->contents, 0, amt); | |
668 | ||
669 | if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0 | |
dbb410c3 AM |
670 | || (bfd_bread (shdr->contents, shdr->sh_size, abfd) |
671 | != shdr->sh_size)) | |
493a3386 NC |
672 | { |
673 | _bfd_error_handler | |
695344c0 | 674 | /* xgettext:c-format */ |
63a5468a | 675 | (_("%B: invalid size field in group section" |
76cfced5 | 676 | " header: %#Lx"), abfd, shdr->sh_size); |
493a3386 NC |
677 | bfd_set_error (bfd_error_bad_value); |
678 | -- num_group; | |
63a5468a AM |
679 | /* PR 17510: If the group contents are even |
680 | partially corrupt, do not allow any of the | |
681 | contents to be used. */ | |
493a3386 NC |
682 | memset (shdr->contents, 0, amt); |
683 | continue; | |
684 | } | |
708d7d0d | 685 | |
dbb410c3 AM |
686 | /* Translate raw contents, a flag word followed by an |
687 | array of elf section indices all in target byte order, | |
688 | to the flag word followed by an array of elf section | |
689 | pointers. */ | |
690 | src = shdr->contents + shdr->sh_size; | |
691 | dest = (Elf_Internal_Group *) (shdr->contents + amt); | |
06614111 | 692 | |
dbb410c3 AM |
693 | while (1) |
694 | { | |
695 | unsigned int idx; | |
696 | ||
697 | src -= 4; | |
698 | --dest; | |
699 | idx = H_GET_32 (abfd, src); | |
700 | if (src == shdr->contents) | |
701 | { | |
702 | dest->flags = idx; | |
b885599b AM |
703 | if (shdr->bfd_section != NULL && (idx & GRP_COMDAT)) |
704 | shdr->bfd_section->flags | |
705 | |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD; | |
dbb410c3 AM |
706 | break; |
707 | } | |
708 | if (idx >= shnum) | |
709 | { | |
4eca0228 AM |
710 | _bfd_error_handler |
711 | (_("%B: invalid SHT_GROUP entry"), abfd); | |
dbb410c3 AM |
712 | idx = 0; |
713 | } | |
714 | dest->shdr = elf_elfsections (abfd)[idx]; | |
715 | } | |
716 | } | |
717 | } | |
493a3386 NC |
718 | |
719 | /* PR 17510: Corrupt binaries might contain invalid groups. */ | |
720 | if (num_group != (unsigned) elf_tdata (abfd)->num_group) | |
721 | { | |
722 | elf_tdata (abfd)->num_group = num_group; | |
723 | ||
724 | /* If all groups are invalid then fail. */ | |
725 | if (num_group == 0) | |
726 | { | |
727 | elf_tdata (abfd)->group_sect_ptr = NULL; | |
728 | elf_tdata (abfd)->num_group = num_group = -1; | |
4eca0228 AM |
729 | _bfd_error_handler |
730 | (_("%B: no valid group sections found"), abfd); | |
493a3386 NC |
731 | bfd_set_error (bfd_error_bad_value); |
732 | } | |
733 | } | |
dbb410c3 AM |
734 | } |
735 | } | |
736 | ||
737 | if (num_group != (unsigned) -1) | |
738 | { | |
739 | unsigned int i; | |
740 | ||
741 | for (i = 0; i < num_group; i++) | |
742 | { | |
743 | Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i]; | |
ce497010 NC |
744 | Elf_Internal_Group *idx; |
745 | unsigned int n_elt; | |
746 | ||
747 | if (shdr == NULL) | |
748 | continue; | |
749 | ||
750 | idx = (Elf_Internal_Group *) shdr->contents; | |
751 | n_elt = shdr->sh_size / 4; | |
dbb410c3 AM |
752 | |
753 | /* Look through this group's sections to see if current | |
754 | section is a member. */ | |
755 | while (--n_elt != 0) | |
756 | if ((++idx)->shdr == hdr) | |
757 | { | |
e0e8c97f | 758 | asection *s = NULL; |
dbb410c3 AM |
759 | |
760 | /* We are a member of this group. Go looking through | |
761 | other members to see if any others are linked via | |
762 | next_in_group. */ | |
763 | idx = (Elf_Internal_Group *) shdr->contents; | |
764 | n_elt = shdr->sh_size / 4; | |
765 | while (--n_elt != 0) | |
766 | if ((s = (++idx)->shdr->bfd_section) != NULL | |
945906ff | 767 | && elf_next_in_group (s) != NULL) |
dbb410c3 AM |
768 | break; |
769 | if (n_elt != 0) | |
770 | { | |
dbb410c3 AM |
771 | /* Snarf the group name from other member, and |
772 | insert current section in circular list. */ | |
945906ff AM |
773 | elf_group_name (newsect) = elf_group_name (s); |
774 | elf_next_in_group (newsect) = elf_next_in_group (s); | |
775 | elf_next_in_group (s) = newsect; | |
dbb410c3 AM |
776 | } |
777 | else | |
778 | { | |
dbb410c3 AM |
779 | const char *gname; |
780 | ||
b885599b AM |
781 | gname = group_signature (abfd, shdr); |
782 | if (gname == NULL) | |
b34976b6 | 783 | return FALSE; |
945906ff | 784 | elf_group_name (newsect) = gname; |
dbb410c3 AM |
785 | |
786 | /* Start a circular list with one element. */ | |
945906ff | 787 | elf_next_in_group (newsect) = newsect; |
dbb410c3 | 788 | } |
b885599b | 789 | |
9dce4196 AM |
790 | /* If the group section has been created, point to the |
791 | new member. */ | |
dbb410c3 | 792 | if (shdr->bfd_section != NULL) |
945906ff | 793 | elf_next_in_group (shdr->bfd_section) = newsect; |
b885599b | 794 | |
dbb410c3 AM |
795 | i = num_group - 1; |
796 | break; | |
797 | } | |
798 | } | |
799 | } | |
800 | ||
945906ff | 801 | if (elf_group_name (newsect) == NULL) |
dbb410c3 | 802 | { |
695344c0 | 803 | /* xgettext:c-format */ |
4eca0228 AM |
804 | _bfd_error_handler (_("%B: no group info for section %A"), |
805 | abfd, newsect); | |
493a3386 | 806 | return FALSE; |
dbb410c3 | 807 | } |
b34976b6 | 808 | return TRUE; |
dbb410c3 AM |
809 | } |
810 | ||
3d7f7666 | 811 | bfd_boolean |
dd863624 | 812 | _bfd_elf_setup_sections (bfd *abfd) |
3d7f7666 L |
813 | { |
814 | unsigned int i; | |
815 | unsigned int num_group = elf_tdata (abfd)->num_group; | |
816 | bfd_boolean result = TRUE; | |
dd863624 L |
817 | asection *s; |
818 | ||
819 | /* Process SHF_LINK_ORDER. */ | |
820 | for (s = abfd->sections; s != NULL; s = s->next) | |
821 | { | |
822 | Elf_Internal_Shdr *this_hdr = &elf_section_data (s)->this_hdr; | |
823 | if ((this_hdr->sh_flags & SHF_LINK_ORDER) != 0) | |
824 | { | |
825 | unsigned int elfsec = this_hdr->sh_link; | |
826 | /* FIXME: The old Intel compiler and old strip/objcopy may | |
827 | not set the sh_link or sh_info fields. Hence we could | |
828 | get the situation where elfsec is 0. */ | |
829 | if (elfsec == 0) | |
830 | { | |
4fbb74a6 | 831 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
dd863624 L |
832 | if (bed->link_order_error_handler) |
833 | bed->link_order_error_handler | |
695344c0 | 834 | /* xgettext:c-format */ |
dd863624 L |
835 | (_("%B: warning: sh_link not set for section `%A'"), |
836 | abfd, s); | |
837 | } | |
838 | else | |
839 | { | |
91d6fa6a | 840 | asection *linksec = NULL; |
25bbc984 | 841 | |
4fbb74a6 AM |
842 | if (elfsec < elf_numsections (abfd)) |
843 | { | |
844 | this_hdr = elf_elfsections (abfd)[elfsec]; | |
91d6fa6a | 845 | linksec = this_hdr->bfd_section; |
4fbb74a6 | 846 | } |
25bbc984 L |
847 | |
848 | /* PR 1991, 2008: | |
849 | Some strip/objcopy may leave an incorrect value in | |
850 | sh_link. We don't want to proceed. */ | |
91d6fa6a | 851 | if (linksec == NULL) |
25bbc984 | 852 | { |
4eca0228 | 853 | _bfd_error_handler |
695344c0 | 854 | /* xgettext:c-format */ |
25bbc984 | 855 | (_("%B: sh_link [%d] in section `%A' is incorrect"), |
c08bb8dd | 856 | s->owner, elfsec, s); |
25bbc984 L |
857 | result = FALSE; |
858 | } | |
859 | ||
91d6fa6a | 860 | elf_linked_to_section (s) = linksec; |
dd863624 L |
861 | } |
862 | } | |
53720c49 AM |
863 | else if (this_hdr->sh_type == SHT_GROUP |
864 | && elf_next_in_group (s) == NULL) | |
865 | { | |
4eca0228 | 866 | _bfd_error_handler |
695344c0 | 867 | /* xgettext:c-format */ |
53720c49 AM |
868 | (_("%B: SHT_GROUP section [index %d] has no SHF_GROUP sections"), |
869 | abfd, elf_section_data (s)->this_idx); | |
870 | result = FALSE; | |
871 | } | |
dd863624 | 872 | } |
3d7f7666 | 873 | |
dd863624 | 874 | /* Process section groups. */ |
3d7f7666 L |
875 | if (num_group == (unsigned) -1) |
876 | return result; | |
877 | ||
878 | for (i = 0; i < num_group; i++) | |
879 | { | |
880 | Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i]; | |
4b0e8a5f NC |
881 | Elf_Internal_Group *idx; |
882 | unsigned int n_elt; | |
3d7f7666 | 883 | |
4b0e8a5f NC |
884 | /* PR binutils/18758: Beware of corrupt binaries with invalid group data. */ |
885 | if (shdr == NULL || shdr->bfd_section == NULL || shdr->contents == NULL) | |
886 | { | |
4eca0228 | 887 | _bfd_error_handler |
695344c0 | 888 | /* xgettext:c-format */ |
4b0e8a5f NC |
889 | (_("%B: section group entry number %u is corrupt"), |
890 | abfd, i); | |
891 | result = FALSE; | |
892 | continue; | |
893 | } | |
894 | ||
895 | idx = (Elf_Internal_Group *) shdr->contents; | |
896 | n_elt = shdr->sh_size / 4; | |
1b786873 | 897 | |
3d7f7666 | 898 | while (--n_elt != 0) |
24d3e51b NC |
899 | { |
900 | ++ idx; | |
901 | ||
902 | if (idx->shdr == NULL) | |
903 | continue; | |
904 | else if (idx->shdr->bfd_section) | |
905 | elf_sec_group (idx->shdr->bfd_section) = shdr->bfd_section; | |
906 | else if (idx->shdr->sh_type == SHT_RELA | |
907 | || idx->shdr->sh_type == SHT_REL) | |
908 | /* We won't include relocation sections in section groups in | |
909 | output object files. We adjust the group section size here | |
910 | so that relocatable link will work correctly when | |
911 | relocation sections are in section group in input object | |
912 | files. */ | |
913 | shdr->bfd_section->size -= 4; | |
914 | else | |
915 | { | |
916 | /* There are some unknown sections in the group. */ | |
917 | _bfd_error_handler | |
918 | /* xgettext:c-format */ | |
919 | (_("%B: unknown type [%#x] section `%s' in group [%A]"), | |
920 | abfd, | |
921 | idx->shdr->sh_type, | |
922 | bfd_elf_string_from_elf_section (abfd, | |
923 | (elf_elfheader (abfd) | |
924 | ->e_shstrndx), | |
925 | idx->shdr->sh_name), | |
926 | shdr->bfd_section); | |
927 | result = FALSE; | |
928 | } | |
929 | } | |
3d7f7666 | 930 | } |
24d3e51b | 931 | |
3d7f7666 L |
932 | return result; |
933 | } | |
934 | ||
72adc230 AM |
935 | bfd_boolean |
936 | bfd_elf_is_group_section (bfd *abfd ATTRIBUTE_UNUSED, const asection *sec) | |
937 | { | |
938 | return elf_next_in_group (sec) != NULL; | |
939 | } | |
940 | ||
f6fe1ccd L |
941 | static char * |
942 | convert_debug_to_zdebug (bfd *abfd, const char *name) | |
943 | { | |
944 | unsigned int len = strlen (name); | |
945 | char *new_name = bfd_alloc (abfd, len + 2); | |
946 | if (new_name == NULL) | |
947 | return NULL; | |
948 | new_name[0] = '.'; | |
949 | new_name[1] = 'z'; | |
950 | memcpy (new_name + 2, name + 1, len); | |
951 | return new_name; | |
952 | } | |
953 | ||
954 | static char * | |
955 | convert_zdebug_to_debug (bfd *abfd, const char *name) | |
956 | { | |
957 | unsigned int len = strlen (name); | |
958 | char *new_name = bfd_alloc (abfd, len); | |
959 | if (new_name == NULL) | |
960 | return NULL; | |
961 | new_name[0] = '.'; | |
962 | memcpy (new_name + 1, name + 2, len - 1); | |
963 | return new_name; | |
964 | } | |
965 | ||
252b5132 RH |
966 | /* Make a BFD section from an ELF section. We store a pointer to the |
967 | BFD section in the bfd_section field of the header. */ | |
968 | ||
b34976b6 | 969 | bfd_boolean |
217aa764 AM |
970 | _bfd_elf_make_section_from_shdr (bfd *abfd, |
971 | Elf_Internal_Shdr *hdr, | |
6dc132d9 L |
972 | const char *name, |
973 | int shindex) | |
252b5132 RH |
974 | { |
975 | asection *newsect; | |
976 | flagword flags; | |
9c5bfbb7 | 977 | const struct elf_backend_data *bed; |
252b5132 RH |
978 | |
979 | if (hdr->bfd_section != NULL) | |
4e011fb5 | 980 | return TRUE; |
252b5132 RH |
981 | |
982 | newsect = bfd_make_section_anyway (abfd, name); | |
983 | if (newsect == NULL) | |
b34976b6 | 984 | return FALSE; |
252b5132 | 985 | |
1829f4b2 AM |
986 | hdr->bfd_section = newsect; |
987 | elf_section_data (newsect)->this_hdr = *hdr; | |
6dc132d9 | 988 | elf_section_data (newsect)->this_idx = shindex; |
1829f4b2 | 989 | |
2f89ff8d L |
990 | /* Always use the real type/flags. */ |
991 | elf_section_type (newsect) = hdr->sh_type; | |
992 | elf_section_flags (newsect) = hdr->sh_flags; | |
993 | ||
252b5132 RH |
994 | newsect->filepos = hdr->sh_offset; |
995 | ||
996 | if (! bfd_set_section_vma (abfd, newsect, hdr->sh_addr) | |
997 | || ! bfd_set_section_size (abfd, newsect, hdr->sh_size) | |
998 | || ! bfd_set_section_alignment (abfd, newsect, | |
72de5009 | 999 | bfd_log2 (hdr->sh_addralign))) |
b34976b6 | 1000 | return FALSE; |
252b5132 RH |
1001 | |
1002 | flags = SEC_NO_FLAGS; | |
1003 | if (hdr->sh_type != SHT_NOBITS) | |
1004 | flags |= SEC_HAS_CONTENTS; | |
dbb410c3 | 1005 | if (hdr->sh_type == SHT_GROUP) |
7bdf4127 | 1006 | flags |= SEC_GROUP; |
252b5132 RH |
1007 | if ((hdr->sh_flags & SHF_ALLOC) != 0) |
1008 | { | |
1009 | flags |= SEC_ALLOC; | |
1010 | if (hdr->sh_type != SHT_NOBITS) | |
1011 | flags |= SEC_LOAD; | |
1012 | } | |
1013 | if ((hdr->sh_flags & SHF_WRITE) == 0) | |
1014 | flags |= SEC_READONLY; | |
1015 | if ((hdr->sh_flags & SHF_EXECINSTR) != 0) | |
1016 | flags |= SEC_CODE; | |
1017 | else if ((flags & SEC_LOAD) != 0) | |
1018 | flags |= SEC_DATA; | |
f5fa8ca2 JJ |
1019 | if ((hdr->sh_flags & SHF_MERGE) != 0) |
1020 | { | |
1021 | flags |= SEC_MERGE; | |
1022 | newsect->entsize = hdr->sh_entsize; | |
f5fa8ca2 | 1023 | } |
84865015 NC |
1024 | if ((hdr->sh_flags & SHF_STRINGS) != 0) |
1025 | flags |= SEC_STRINGS; | |
dbb410c3 AM |
1026 | if (hdr->sh_flags & SHF_GROUP) |
1027 | if (!setup_group (abfd, hdr, newsect)) | |
b34976b6 | 1028 | return FALSE; |
13ae64f3 JJ |
1029 | if ((hdr->sh_flags & SHF_TLS) != 0) |
1030 | flags |= SEC_THREAD_LOCAL; | |
18ae9cc1 L |
1031 | if ((hdr->sh_flags & SHF_EXCLUDE) != 0) |
1032 | flags |= SEC_EXCLUDE; | |
252b5132 | 1033 | |
3d2b39cf | 1034 | if ((flags & SEC_ALLOC) == 0) |
7a6cc5fb | 1035 | { |
3d2b39cf L |
1036 | /* The debugging sections appear to be recognized only by name, |
1037 | not any sort of flag. Their SEC_ALLOC bits are cleared. */ | |
3d2b39cf L |
1038 | if (name [0] == '.') |
1039 | { | |
f073ced3 AM |
1040 | const char *p; |
1041 | int n; | |
1042 | if (name[1] == 'd') | |
1043 | p = ".debug", n = 6; | |
1044 | else if (name[1] == 'g' && name[2] == 'n') | |
1045 | p = ".gnu.linkonce.wi.", n = 17; | |
1046 | else if (name[1] == 'g' && name[2] == 'd') | |
1047 | p = ".gdb_index", n = 11; /* yes we really do mean 11. */ | |
1048 | else if (name[1] == 'l') | |
1049 | p = ".line", n = 5; | |
1050 | else if (name[1] == 's') | |
1051 | p = ".stab", n = 5; | |
1052 | else if (name[1] == 'z') | |
1053 | p = ".zdebug", n = 7; | |
1054 | else | |
1055 | p = NULL, n = 0; | |
1056 | if (p != NULL && strncmp (name, p, n) == 0) | |
3d2b39cf L |
1057 | flags |= SEC_DEBUGGING; |
1058 | } | |
1059 | } | |
252b5132 RH |
1060 | |
1061 | /* As a GNU extension, if the name begins with .gnu.linkonce, we | |
1062 | only link a single copy of the section. This is used to support | |
1063 | g++. g++ will emit each template expansion in its own section. | |
1064 | The symbols will be defined as weak, so that multiple definitions | |
1065 | are permitted. The GNU linker extension is to actually discard | |
1066 | all but one of the sections. */ | |
0112cd26 | 1067 | if (CONST_STRNEQ (name, ".gnu.linkonce") |
b885599b | 1068 | && elf_next_in_group (newsect) == NULL) |
252b5132 RH |
1069 | flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD; |
1070 | ||
fa152c49 JW |
1071 | bed = get_elf_backend_data (abfd); |
1072 | if (bed->elf_backend_section_flags) | |
1073 | if (! bed->elf_backend_section_flags (&flags, hdr)) | |
b34976b6 | 1074 | return FALSE; |
fa152c49 | 1075 | |
252b5132 | 1076 | if (! bfd_set_section_flags (abfd, newsect, flags)) |
b34976b6 | 1077 | return FALSE; |
252b5132 | 1078 | |
718175fa JK |
1079 | /* We do not parse the PT_NOTE segments as we are interested even in the |
1080 | separate debug info files which may have the segments offsets corrupted. | |
1081 | PT_NOTEs from the core files are currently not parsed using BFD. */ | |
1082 | if (hdr->sh_type == SHT_NOTE) | |
1083 | { | |
baea7ef1 | 1084 | bfd_byte *contents; |
718175fa | 1085 | |
baea7ef1 | 1086 | if (!bfd_malloc_and_get_section (abfd, newsect, &contents)) |
718175fa JK |
1087 | return FALSE; |
1088 | ||
08698b7b | 1089 | elf_parse_notes (abfd, (char *) contents, hdr->sh_size, hdr->sh_offset); |
718175fa JK |
1090 | free (contents); |
1091 | } | |
1092 | ||
252b5132 RH |
1093 | if ((flags & SEC_ALLOC) != 0) |
1094 | { | |
1095 | Elf_Internal_Phdr *phdr; | |
6ffd7900 AM |
1096 | unsigned int i, nload; |
1097 | ||
1098 | /* Some ELF linkers produce binaries with all the program header | |
1099 | p_paddr fields zero. If we have such a binary with more than | |
1100 | one PT_LOAD header, then leave the section lma equal to vma | |
1101 | so that we don't create sections with overlapping lma. */ | |
1102 | phdr = elf_tdata (abfd)->phdr; | |
1103 | for (nload = 0, i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++) | |
1104 | if (phdr->p_paddr != 0) | |
1105 | break; | |
1106 | else if (phdr->p_type == PT_LOAD && phdr->p_memsz != 0) | |
1107 | ++nload; | |
1108 | if (i >= elf_elfheader (abfd)->e_phnum && nload > 1) | |
1109 | return TRUE; | |
252b5132 | 1110 | |
252b5132 RH |
1111 | phdr = elf_tdata (abfd)->phdr; |
1112 | for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++) | |
1113 | { | |
86b2281f AM |
1114 | if (((phdr->p_type == PT_LOAD |
1115 | && (hdr->sh_flags & SHF_TLS) == 0) | |
1116 | || phdr->p_type == PT_TLS) | |
9a83a553 | 1117 | && ELF_SECTION_IN_SEGMENT (hdr, phdr)) |
252b5132 | 1118 | { |
88967714 AM |
1119 | if ((flags & SEC_LOAD) == 0) |
1120 | newsect->lma = (phdr->p_paddr | |
1121 | + hdr->sh_addr - phdr->p_vaddr); | |
1122 | else | |
1123 | /* We used to use the same adjustment for SEC_LOAD | |
1124 | sections, but that doesn't work if the segment | |
1125 | is packed with code from multiple VMAs. | |
1126 | Instead we calculate the section LMA based on | |
1127 | the segment LMA. It is assumed that the | |
1128 | segment will contain sections with contiguous | |
1129 | LMAs, even if the VMAs are not. */ | |
1130 | newsect->lma = (phdr->p_paddr | |
1131 | + hdr->sh_offset - phdr->p_offset); | |
1132 | ||
1133 | /* With contiguous segments, we can't tell from file | |
1134 | offsets whether a section with zero size should | |
1135 | be placed at the end of one segment or the | |
1136 | beginning of the next. Decide based on vaddr. */ | |
1137 | if (hdr->sh_addr >= phdr->p_vaddr | |
1138 | && (hdr->sh_addr + hdr->sh_size | |
1139 | <= phdr->p_vaddr + phdr->p_memsz)) | |
1140 | break; | |
252b5132 RH |
1141 | } |
1142 | } | |
1143 | } | |
1144 | ||
4a114e3e L |
1145 | /* Compress/decompress DWARF debug sections with names: .debug_* and |
1146 | .zdebug_*, after the section flags is set. */ | |
1147 | if ((flags & SEC_DEBUGGING) | |
1148 | && ((name[1] == 'd' && name[6] == '_') | |
1149 | || (name[1] == 'z' && name[7] == '_'))) | |
1150 | { | |
1151 | enum { nothing, compress, decompress } action = nothing; | |
151411f8 | 1152 | int compression_header_size; |
dab394de | 1153 | bfd_size_type uncompressed_size; |
151411f8 L |
1154 | bfd_boolean compressed |
1155 | = bfd_is_section_compressed_with_header (abfd, newsect, | |
dab394de L |
1156 | &compression_header_size, |
1157 | &uncompressed_size); | |
4a114e3e | 1158 | |
151411f8 | 1159 | if (compressed) |
4a114e3e L |
1160 | { |
1161 | /* Compressed section. Check if we should decompress. */ | |
1162 | if ((abfd->flags & BFD_DECOMPRESS)) | |
1163 | action = decompress; | |
1164 | } | |
151411f8 L |
1165 | |
1166 | /* Compress the uncompressed section or convert from/to .zdebug* | |
1167 | section. Check if we should compress. */ | |
1168 | if (action == nothing) | |
4a114e3e | 1169 | { |
151411f8 L |
1170 | if (newsect->size != 0 |
1171 | && (abfd->flags & BFD_COMPRESS) | |
1172 | && compression_header_size >= 0 | |
dab394de | 1173 | && uncompressed_size > 0 |
151411f8 L |
1174 | && (!compressed |
1175 | || ((compression_header_size > 0) | |
1176 | != ((abfd->flags & BFD_COMPRESS_GABI) != 0)))) | |
4a114e3e | 1177 | action = compress; |
151411f8 L |
1178 | else |
1179 | return TRUE; | |
4a114e3e L |
1180 | } |
1181 | ||
151411f8 | 1182 | if (action == compress) |
4a114e3e | 1183 | { |
4a114e3e L |
1184 | if (!bfd_init_section_compress_status (abfd, newsect)) |
1185 | { | |
4eca0228 | 1186 | _bfd_error_handler |
695344c0 | 1187 | /* xgettext:c-format */ |
bc823199 | 1188 | (_("%B: unable to initialize compress status for section %s"), |
4a114e3e L |
1189 | abfd, name); |
1190 | return FALSE; | |
1191 | } | |
151411f8 L |
1192 | } |
1193 | else | |
1194 | { | |
4a114e3e L |
1195 | if (!bfd_init_section_decompress_status (abfd, newsect)) |
1196 | { | |
4eca0228 | 1197 | _bfd_error_handler |
695344c0 | 1198 | /* xgettext:c-format */ |
bc823199 | 1199 | (_("%B: unable to initialize decompress status for section %s"), |
4a114e3e L |
1200 | abfd, name); |
1201 | return FALSE; | |
1202 | } | |
151411f8 L |
1203 | } |
1204 | ||
f6fe1ccd | 1205 | if (abfd->is_linker_input) |
151411f8 | 1206 | { |
f6fe1ccd L |
1207 | if (name[1] == 'z' |
1208 | && (action == decompress | |
1209 | || (action == compress | |
1210 | && (abfd->flags & BFD_COMPRESS_GABI) != 0))) | |
4e011fb5 | 1211 | { |
f6fe1ccd L |
1212 | /* Convert section name from .zdebug_* to .debug_* so |
1213 | that linker will consider this section as a debug | |
1214 | section. */ | |
1215 | char *new_name = convert_zdebug_to_debug (abfd, name); | |
151411f8 L |
1216 | if (new_name == NULL) |
1217 | return FALSE; | |
f6fe1ccd | 1218 | bfd_rename_section (abfd, newsect, new_name); |
151411f8 | 1219 | } |
4a114e3e | 1220 | } |
f6fe1ccd L |
1221 | else |
1222 | /* For objdump, don't rename the section. For objcopy, delay | |
1223 | section rename to elf_fake_sections. */ | |
1224 | newsect->flags |= SEC_ELF_RENAME; | |
4a114e3e L |
1225 | } |
1226 | ||
b34976b6 | 1227 | return TRUE; |
252b5132 RH |
1228 | } |
1229 | ||
84865015 NC |
1230 | const char *const bfd_elf_section_type_names[] = |
1231 | { | |
252b5132 RH |
1232 | "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB", |
1233 | "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE", | |
1234 | "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM", | |
1235 | }; | |
1236 | ||
1049f94e | 1237 | /* ELF relocs are against symbols. If we are producing relocatable |
252b5132 RH |
1238 | output, and the reloc is against an external symbol, and nothing |
1239 | has given us any additional addend, the resulting reloc will also | |
1240 | be against the same symbol. In such a case, we don't want to | |
1241 | change anything about the way the reloc is handled, since it will | |
1242 | all be done at final link time. Rather than put special case code | |
1243 | into bfd_perform_relocation, all the reloc types use this howto | |
1244 | function. It just short circuits the reloc if producing | |
1049f94e | 1245 | relocatable output against an external symbol. */ |
252b5132 | 1246 | |
252b5132 | 1247 | bfd_reloc_status_type |
217aa764 AM |
1248 | bfd_elf_generic_reloc (bfd *abfd ATTRIBUTE_UNUSED, |
1249 | arelent *reloc_entry, | |
1250 | asymbol *symbol, | |
1251 | void *data ATTRIBUTE_UNUSED, | |
1252 | asection *input_section, | |
1253 | bfd *output_bfd, | |
1254 | char **error_message ATTRIBUTE_UNUSED) | |
1255 | { | |
1256 | if (output_bfd != NULL | |
252b5132 RH |
1257 | && (symbol->flags & BSF_SECTION_SYM) == 0 |
1258 | && (! reloc_entry->howto->partial_inplace | |
1259 | || reloc_entry->addend == 0)) | |
1260 | { | |
1261 | reloc_entry->address += input_section->output_offset; | |
1262 | return bfd_reloc_ok; | |
1263 | } | |
1264 | ||
1265 | return bfd_reloc_continue; | |
1266 | } | |
1267 | \f | |
84865015 NC |
1268 | /* Returns TRUE if section A matches section B. |
1269 | Names, addresses and links may be different, but everything else | |
1270 | should be the same. */ | |
1271 | ||
1272 | static bfd_boolean | |
5522f910 NC |
1273 | section_match (const Elf_Internal_Shdr * a, |
1274 | const Elf_Internal_Shdr * b) | |
84865015 NC |
1275 | { |
1276 | return | |
1277 | a->sh_type == b->sh_type | |
5522f910 NC |
1278 | && (a->sh_flags & ~ SHF_INFO_LINK) |
1279 | == (b->sh_flags & ~ SHF_INFO_LINK) | |
84865015 NC |
1280 | && a->sh_addralign == b->sh_addralign |
1281 | && a->sh_size == b->sh_size | |
1282 | && a->sh_entsize == b->sh_entsize | |
1283 | /* FIXME: Check sh_addr ? */ | |
1284 | ; | |
1285 | } | |
1286 | ||
1287 | /* Find a section in OBFD that has the same characteristics | |
1288 | as IHEADER. Return the index of this section or SHN_UNDEF if | |
1289 | none can be found. Check's section HINT first, as this is likely | |
1290 | to be the correct section. */ | |
1291 | ||
1292 | static unsigned int | |
5cc4ca83 ST |
1293 | find_link (const bfd *obfd, const Elf_Internal_Shdr *iheader, |
1294 | const unsigned int hint) | |
84865015 NC |
1295 | { |
1296 | Elf_Internal_Shdr ** oheaders = elf_elfsections (obfd); | |
1297 | unsigned int i; | |
1298 | ||
a55c9876 NC |
1299 | BFD_ASSERT (iheader != NULL); |
1300 | ||
1301 | /* See PR 20922 for a reproducer of the NULL test. */ | |
5cc4ca83 ST |
1302 | if (hint < elf_numsections (obfd) |
1303 | && oheaders[hint] != NULL | |
a55c9876 | 1304 | && section_match (oheaders[hint], iheader)) |
84865015 NC |
1305 | return hint; |
1306 | ||
1307 | for (i = 1; i < elf_numsections (obfd); i++) | |
1308 | { | |
1309 | Elf_Internal_Shdr * oheader = oheaders[i]; | |
1310 | ||
a55c9876 NC |
1311 | if (oheader == NULL) |
1312 | continue; | |
84865015 NC |
1313 | if (section_match (oheader, iheader)) |
1314 | /* FIXME: Do we care if there is a potential for | |
1315 | multiple matches ? */ | |
1316 | return i; | |
1317 | } | |
1318 | ||
1319 | return SHN_UNDEF; | |
1320 | } | |
1321 | ||
5522f910 NC |
1322 | /* PR 19938: Attempt to set the ELF section header fields of an OS or |
1323 | Processor specific section, based upon a matching input section. | |
1324 | Returns TRUE upon success, FALSE otherwise. */ | |
1325 | ||
1326 | static bfd_boolean | |
1327 | copy_special_section_fields (const bfd *ibfd, | |
1328 | bfd *obfd, | |
1329 | const Elf_Internal_Shdr *iheader, | |
1330 | Elf_Internal_Shdr *oheader, | |
1331 | const unsigned int secnum) | |
1332 | { | |
1333 | const struct elf_backend_data *bed = get_elf_backend_data (obfd); | |
1334 | const Elf_Internal_Shdr **iheaders = (const Elf_Internal_Shdr **) elf_elfsections (ibfd); | |
1335 | bfd_boolean changed = FALSE; | |
1336 | unsigned int sh_link; | |
1337 | ||
1338 | if (oheader->sh_type == SHT_NOBITS) | |
1339 | { | |
1340 | /* This is a feature for objcopy --only-keep-debug: | |
1341 | When a section's type is changed to NOBITS, we preserve | |
1342 | the sh_link and sh_info fields so that they can be | |
1343 | matched up with the original. | |
1344 | ||
1345 | Note: Strictly speaking these assignments are wrong. | |
1346 | The sh_link and sh_info fields should point to the | |
1347 | relevent sections in the output BFD, which may not be in | |
1348 | the same location as they were in the input BFD. But | |
1349 | the whole point of this action is to preserve the | |
1350 | original values of the sh_link and sh_info fields, so | |
1351 | that they can be matched up with the section headers in | |
1352 | the original file. So strictly speaking we may be | |
1353 | creating an invalid ELF file, but it is only for a file | |
1354 | that just contains debug info and only for sections | |
1355 | without any contents. */ | |
1356 | if (oheader->sh_link == 0) | |
1357 | oheader->sh_link = iheader->sh_link; | |
1358 | if (oheader->sh_info == 0) | |
1359 | oheader->sh_info = iheader->sh_info; | |
1360 | return TRUE; | |
1361 | } | |
1362 | ||
1363 | /* Allow the target a chance to decide how these fields should be set. */ | |
1364 | if (bed->elf_backend_copy_special_section_fields != NULL | |
1365 | && bed->elf_backend_copy_special_section_fields | |
1366 | (ibfd, obfd, iheader, oheader)) | |
1367 | return TRUE; | |
1368 | ||
1369 | /* We have an iheader which might match oheader, and which has non-zero | |
1370 | sh_info and/or sh_link fields. Attempt to follow those links and find | |
1371 | the section in the output bfd which corresponds to the linked section | |
1372 | in the input bfd. */ | |
1373 | if (iheader->sh_link != SHN_UNDEF) | |
1374 | { | |
4f3ca05b NC |
1375 | /* See PR 20931 for a reproducer. */ |
1376 | if (iheader->sh_link >= elf_numsections (ibfd)) | |
1377 | { | |
76cfced5 | 1378 | _bfd_error_handler |
4f3ca05b NC |
1379 | /* xgettext:c-format */ |
1380 | (_("%B: Invalid sh_link field (%d) in section number %d"), | |
1381 | ibfd, iheader->sh_link, secnum); | |
1382 | return FALSE; | |
1383 | } | |
1384 | ||
5522f910 NC |
1385 | sh_link = find_link (obfd, iheaders[iheader->sh_link], iheader->sh_link); |
1386 | if (sh_link != SHN_UNDEF) | |
1387 | { | |
1388 | oheader->sh_link = sh_link; | |
1389 | changed = TRUE; | |
1390 | } | |
1391 | else | |
1392 | /* FIXME: Should we install iheader->sh_link | |
1393 | if we could not find a match ? */ | |
76cfced5 | 1394 | _bfd_error_handler |
695344c0 | 1395 | /* xgettext:c-format */ |
5522f910 NC |
1396 | (_("%B: Failed to find link section for section %d"), obfd, secnum); |
1397 | } | |
1398 | ||
1399 | if (iheader->sh_info) | |
1400 | { | |
1401 | /* The sh_info field can hold arbitrary information, but if the | |
1402 | SHF_LINK_INFO flag is set then it should be interpreted as a | |
1403 | section index. */ | |
1404 | if (iheader->sh_flags & SHF_INFO_LINK) | |
1405 | { | |
1406 | sh_link = find_link (obfd, iheaders[iheader->sh_info], | |
1407 | iheader->sh_info); | |
1408 | if (sh_link != SHN_UNDEF) | |
1409 | oheader->sh_flags |= SHF_INFO_LINK; | |
1410 | } | |
1411 | else | |
1412 | /* No idea what it means - just copy it. */ | |
1413 | sh_link = iheader->sh_info; | |
1414 | ||
1415 | if (sh_link != SHN_UNDEF) | |
1416 | { | |
1417 | oheader->sh_info = sh_link; | |
1418 | changed = TRUE; | |
1419 | } | |
1420 | else | |
76cfced5 | 1421 | _bfd_error_handler |
695344c0 | 1422 | /* xgettext:c-format */ |
5522f910 NC |
1423 | (_("%B: Failed to find info section for section %d"), obfd, secnum); |
1424 | } | |
1425 | ||
1426 | return changed; | |
1427 | } | |
1428 | ||
0ac4564e L |
1429 | /* Copy the program header and other data from one object module to |
1430 | another. */ | |
252b5132 | 1431 | |
b34976b6 | 1432 | bfd_boolean |
217aa764 | 1433 | _bfd_elf_copy_private_bfd_data (bfd *ibfd, bfd *obfd) |
2d502050 | 1434 | { |
5522f910 NC |
1435 | const Elf_Internal_Shdr **iheaders = (const Elf_Internal_Shdr **) elf_elfsections (ibfd); |
1436 | Elf_Internal_Shdr **oheaders = elf_elfsections (obfd); | |
1437 | const struct elf_backend_data *bed; | |
84865015 NC |
1438 | unsigned int i; |
1439 | ||
2d502050 | 1440 | if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour |
84865015 | 1441 | || bfd_get_flavour (obfd) != bfd_target_elf_flavour) |
b34976b6 | 1442 | return TRUE; |
2d502050 | 1443 | |
57b828ef L |
1444 | if (!elf_flags_init (obfd)) |
1445 | { | |
1446 | elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags; | |
1447 | elf_flags_init (obfd) = TRUE; | |
1448 | } | |
2d502050 | 1449 | |
0ac4564e | 1450 | elf_gp (obfd) = elf_gp (ibfd); |
57b828ef L |
1451 | |
1452 | /* Also copy the EI_OSABI field. */ | |
1453 | elf_elfheader (obfd)->e_ident[EI_OSABI] = | |
1454 | elf_elfheader (ibfd)->e_ident[EI_OSABI]; | |
104d59d1 | 1455 | |
5522f910 NC |
1456 | /* If set, copy the EI_ABIVERSION field. */ |
1457 | if (elf_elfheader (ibfd)->e_ident[EI_ABIVERSION]) | |
1458 | elf_elfheader (obfd)->e_ident[EI_ABIVERSION] | |
1459 | = elf_elfheader (ibfd)->e_ident[EI_ABIVERSION]; | |
1460 | ||
104d59d1 JM |
1461 | /* Copy object attributes. */ |
1462 | _bfd_elf_copy_obj_attributes (ibfd, obfd); | |
63b9bbb7 | 1463 | |
84865015 NC |
1464 | if (iheaders == NULL || oheaders == NULL) |
1465 | return TRUE; | |
63b9bbb7 | 1466 | |
5522f910 NC |
1467 | bed = get_elf_backend_data (obfd); |
1468 | ||
1469 | /* Possibly copy other fields in the section header. */ | |
84865015 | 1470 | for (i = 1; i < elf_numsections (obfd); i++) |
63b9bbb7 | 1471 | { |
84865015 NC |
1472 | unsigned int j; |
1473 | Elf_Internal_Shdr * oheader = oheaders[i]; | |
63b9bbb7 | 1474 | |
5522f910 NC |
1475 | /* Ignore ordinary sections. SHT_NOBITS sections are considered however |
1476 | because of a special case need for generating separate debug info | |
1477 | files. See below for more details. */ | |
84865015 NC |
1478 | if (oheader == NULL |
1479 | || (oheader->sh_type != SHT_NOBITS | |
5522f910 NC |
1480 | && oheader->sh_type < SHT_LOOS)) |
1481 | continue; | |
1482 | ||
1483 | /* Ignore empty sections, and sections whose | |
1484 | fields have already been initialised. */ | |
1485 | if (oheader->sh_size == 0 | |
84865015 NC |
1486 | || (oheader->sh_info != 0 && oheader->sh_link != 0)) |
1487 | continue; | |
63b9bbb7 | 1488 | |
84865015 | 1489 | /* Scan for the matching section in the input bfd. |
5522f910 NC |
1490 | First we try for a direct mapping between the input and output sections. */ |
1491 | for (j = 1; j < elf_numsections (ibfd); j++) | |
1492 | { | |
1493 | const Elf_Internal_Shdr * iheader = iheaders[j]; | |
1494 | ||
1495 | if (iheader == NULL) | |
1496 | continue; | |
1497 | ||
1498 | if (oheader->bfd_section != NULL | |
1499 | && iheader->bfd_section != NULL | |
1500 | && iheader->bfd_section->output_section != NULL | |
1501 | && iheader->bfd_section->output_section == oheader->bfd_section) | |
1502 | { | |
1503 | /* We have found a connection from the input section to the | |
1504 | output section. Attempt to copy the header fields. If | |
1505 | this fails then do not try any further sections - there | |
1506 | should only be a one-to-one mapping between input and output. */ | |
1507 | if (! copy_special_section_fields (ibfd, obfd, iheader, oheader, i)) | |
1508 | j = elf_numsections (ibfd); | |
1509 | break; | |
1510 | } | |
1511 | } | |
1512 | ||
1513 | if (j < elf_numsections (ibfd)) | |
1514 | continue; | |
1515 | ||
1516 | /* That failed. So try to deduce the corresponding input section. | |
84865015 NC |
1517 | Unfortunately we cannot compare names as the output string table |
1518 | is empty, so instead we check size, address and type. */ | |
1519 | for (j = 1; j < elf_numsections (ibfd); j++) | |
1520 | { | |
5522f910 | 1521 | const Elf_Internal_Shdr * iheader = iheaders[j]; |
84865015 | 1522 | |
5522f910 NC |
1523 | if (iheader == NULL) |
1524 | continue; | |
1525 | ||
1526 | /* Try matching fields in the input section's header. | |
1527 | Since --only-keep-debug turns all non-debug sections into | |
84865015 NC |
1528 | SHT_NOBITS sections, the output SHT_NOBITS type matches any |
1529 | input type. */ | |
1530 | if ((oheader->sh_type == SHT_NOBITS | |
1531 | || iheader->sh_type == oheader->sh_type) | |
5522f910 NC |
1532 | && (iheader->sh_flags & ~ SHF_INFO_LINK) |
1533 | == (oheader->sh_flags & ~ SHF_INFO_LINK) | |
84865015 NC |
1534 | && iheader->sh_addralign == oheader->sh_addralign |
1535 | && iheader->sh_entsize == oheader->sh_entsize | |
1536 | && iheader->sh_size == oheader->sh_size | |
1537 | && iheader->sh_addr == oheader->sh_addr | |
1538 | && (iheader->sh_info != oheader->sh_info | |
1539 | || iheader->sh_link != oheader->sh_link)) | |
63b9bbb7 | 1540 | { |
5522f910 NC |
1541 | if (copy_special_section_fields (ibfd, obfd, iheader, oheader, i)) |
1542 | break; | |
63b9bbb7 NC |
1543 | } |
1544 | } | |
5522f910 NC |
1545 | |
1546 | if (j == elf_numsections (ibfd) && oheader->sh_type >= SHT_LOOS) | |
1547 | { | |
1548 | /* Final attempt. Call the backend copy function | |
1549 | with a NULL input section. */ | |
1550 | if (bed->elf_backend_copy_special_section_fields != NULL) | |
1551 | bed->elf_backend_copy_special_section_fields (ibfd, obfd, NULL, oheader); | |
1552 | } | |
63b9bbb7 NC |
1553 | } |
1554 | ||
b34976b6 | 1555 | return TRUE; |
2d502050 L |
1556 | } |
1557 | ||
cedc298e L |
1558 | static const char * |
1559 | get_segment_type (unsigned int p_type) | |
1560 | { | |
1561 | const char *pt; | |
1562 | switch (p_type) | |
1563 | { | |
1564 | case PT_NULL: pt = "NULL"; break; | |
1565 | case PT_LOAD: pt = "LOAD"; break; | |
1566 | case PT_DYNAMIC: pt = "DYNAMIC"; break; | |
1567 | case PT_INTERP: pt = "INTERP"; break; | |
1568 | case PT_NOTE: pt = "NOTE"; break; | |
1569 | case PT_SHLIB: pt = "SHLIB"; break; | |
1570 | case PT_PHDR: pt = "PHDR"; break; | |
1571 | case PT_TLS: pt = "TLS"; break; | |
1572 | case PT_GNU_EH_FRAME: pt = "EH_FRAME"; break; | |
2b05f1b7 | 1573 | case PT_GNU_STACK: pt = "STACK"; break; |
cedc298e L |
1574 | case PT_GNU_RELRO: pt = "RELRO"; break; |
1575 | default: pt = NULL; break; | |
1576 | } | |
1577 | return pt; | |
1578 | } | |
1579 | ||
f0b79d91 L |
1580 | /* Print out the program headers. */ |
1581 | ||
b34976b6 | 1582 | bfd_boolean |
217aa764 | 1583 | _bfd_elf_print_private_bfd_data (bfd *abfd, void *farg) |
252b5132 | 1584 | { |
a50b1753 | 1585 | FILE *f = (FILE *) farg; |
252b5132 RH |
1586 | Elf_Internal_Phdr *p; |
1587 | asection *s; | |
1588 | bfd_byte *dynbuf = NULL; | |
1589 | ||
1590 | p = elf_tdata (abfd)->phdr; | |
1591 | if (p != NULL) | |
1592 | { | |
1593 | unsigned int i, c; | |
1594 | ||
1595 | fprintf (f, _("\nProgram Header:\n")); | |
1596 | c = elf_elfheader (abfd)->e_phnum; | |
1597 | for (i = 0; i < c; i++, p++) | |
1598 | { | |
cedc298e | 1599 | const char *pt = get_segment_type (p->p_type); |
252b5132 RH |
1600 | char buf[20]; |
1601 | ||
cedc298e | 1602 | if (pt == NULL) |
252b5132 | 1603 | { |
cedc298e L |
1604 | sprintf (buf, "0x%lx", p->p_type); |
1605 | pt = buf; | |
252b5132 | 1606 | } |
dc810e39 | 1607 | fprintf (f, "%8s off 0x", pt); |
60b89a18 | 1608 | bfd_fprintf_vma (abfd, f, p->p_offset); |
252b5132 | 1609 | fprintf (f, " vaddr 0x"); |
60b89a18 | 1610 | bfd_fprintf_vma (abfd, f, p->p_vaddr); |
252b5132 | 1611 | fprintf (f, " paddr 0x"); |
60b89a18 | 1612 | bfd_fprintf_vma (abfd, f, p->p_paddr); |
252b5132 RH |
1613 | fprintf (f, " align 2**%u\n", bfd_log2 (p->p_align)); |
1614 | fprintf (f, " filesz 0x"); | |
60b89a18 | 1615 | bfd_fprintf_vma (abfd, f, p->p_filesz); |
252b5132 | 1616 | fprintf (f, " memsz 0x"); |
60b89a18 | 1617 | bfd_fprintf_vma (abfd, f, p->p_memsz); |
252b5132 RH |
1618 | fprintf (f, " flags %c%c%c", |
1619 | (p->p_flags & PF_R) != 0 ? 'r' : '-', | |
1620 | (p->p_flags & PF_W) != 0 ? 'w' : '-', | |
1621 | (p->p_flags & PF_X) != 0 ? 'x' : '-'); | |
dc810e39 AM |
1622 | if ((p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X)) != 0) |
1623 | fprintf (f, " %lx", p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X)); | |
252b5132 RH |
1624 | fprintf (f, "\n"); |
1625 | } | |
1626 | } | |
1627 | ||
1628 | s = bfd_get_section_by_name (abfd, ".dynamic"); | |
1629 | if (s != NULL) | |
1630 | { | |
cb33740c | 1631 | unsigned int elfsec; |
dc810e39 | 1632 | unsigned long shlink; |
252b5132 RH |
1633 | bfd_byte *extdyn, *extdynend; |
1634 | size_t extdynsize; | |
217aa764 | 1635 | void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *); |
252b5132 RH |
1636 | |
1637 | fprintf (f, _("\nDynamic Section:\n")); | |
1638 | ||
eea6121a | 1639 | if (!bfd_malloc_and_get_section (abfd, s, &dynbuf)) |
252b5132 RH |
1640 | goto error_return; |
1641 | ||
1642 | elfsec = _bfd_elf_section_from_bfd_section (abfd, s); | |
cb33740c | 1643 | if (elfsec == SHN_BAD) |
252b5132 | 1644 | goto error_return; |
dc810e39 | 1645 | shlink = elf_elfsections (abfd)[elfsec]->sh_link; |
252b5132 RH |
1646 | |
1647 | extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn; | |
1648 | swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in; | |
1649 | ||
1650 | extdyn = dynbuf; | |
06614111 NC |
1651 | /* PR 17512: file: 6f427532. */ |
1652 | if (s->size < extdynsize) | |
1653 | goto error_return; | |
eea6121a | 1654 | extdynend = extdyn + s->size; |
1036838a NC |
1655 | /* PR 17512: file: id:000006,sig:06,src:000000,op:flip4,pos:5664. |
1656 | Fix range check. */ | |
1657 | for (; extdyn <= (extdynend - extdynsize); extdyn += extdynsize) | |
252b5132 RH |
1658 | { |
1659 | Elf_Internal_Dyn dyn; | |
ad9563d6 | 1660 | const char *name = ""; |
252b5132 | 1661 | char ab[20]; |
b34976b6 | 1662 | bfd_boolean stringp; |
ad9563d6 | 1663 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
252b5132 | 1664 | |
217aa764 | 1665 | (*swap_dyn_in) (abfd, extdyn, &dyn); |
252b5132 RH |
1666 | |
1667 | if (dyn.d_tag == DT_NULL) | |
1668 | break; | |
1669 | ||
b34976b6 | 1670 | stringp = FALSE; |
252b5132 RH |
1671 | switch (dyn.d_tag) |
1672 | { | |
1673 | default: | |
ad9563d6 CM |
1674 | if (bed->elf_backend_get_target_dtag) |
1675 | name = (*bed->elf_backend_get_target_dtag) (dyn.d_tag); | |
1676 | ||
1677 | if (!strcmp (name, "")) | |
1678 | { | |
cd9af601 | 1679 | sprintf (ab, "%#" BFD_VMA_FMT "x", dyn.d_tag); |
ad9563d6 CM |
1680 | name = ab; |
1681 | } | |
252b5132 RH |
1682 | break; |
1683 | ||
b34976b6 | 1684 | case DT_NEEDED: name = "NEEDED"; stringp = TRUE; break; |
252b5132 RH |
1685 | case DT_PLTRELSZ: name = "PLTRELSZ"; break; |
1686 | case DT_PLTGOT: name = "PLTGOT"; break; | |
1687 | case DT_HASH: name = "HASH"; break; | |
1688 | case DT_STRTAB: name = "STRTAB"; break; | |
1689 | case DT_SYMTAB: name = "SYMTAB"; break; | |
1690 | case DT_RELA: name = "RELA"; break; | |
1691 | case DT_RELASZ: name = "RELASZ"; break; | |
1692 | case DT_RELAENT: name = "RELAENT"; break; | |
1693 | case DT_STRSZ: name = "STRSZ"; break; | |
1694 | case DT_SYMENT: name = "SYMENT"; break; | |
1695 | case DT_INIT: name = "INIT"; break; | |
1696 | case DT_FINI: name = "FINI"; break; | |
b34976b6 AM |
1697 | case DT_SONAME: name = "SONAME"; stringp = TRUE; break; |
1698 | case DT_RPATH: name = "RPATH"; stringp = TRUE; break; | |
252b5132 RH |
1699 | case DT_SYMBOLIC: name = "SYMBOLIC"; break; |
1700 | case DT_REL: name = "REL"; break; | |
1701 | case DT_RELSZ: name = "RELSZ"; break; | |
1702 | case DT_RELENT: name = "RELENT"; break; | |
1703 | case DT_PLTREL: name = "PLTREL"; break; | |
1704 | case DT_DEBUG: name = "DEBUG"; break; | |
1705 | case DT_TEXTREL: name = "TEXTREL"; break; | |
1706 | case DT_JMPREL: name = "JMPREL"; break; | |
94558834 L |
1707 | case DT_BIND_NOW: name = "BIND_NOW"; break; |
1708 | case DT_INIT_ARRAY: name = "INIT_ARRAY"; break; | |
1709 | case DT_FINI_ARRAY: name = "FINI_ARRAY"; break; | |
1710 | case DT_INIT_ARRAYSZ: name = "INIT_ARRAYSZ"; break; | |
1711 | case DT_FINI_ARRAYSZ: name = "FINI_ARRAYSZ"; break; | |
b34976b6 | 1712 | case DT_RUNPATH: name = "RUNPATH"; stringp = TRUE; break; |
94558834 L |
1713 | case DT_FLAGS: name = "FLAGS"; break; |
1714 | case DT_PREINIT_ARRAY: name = "PREINIT_ARRAY"; break; | |
1715 | case DT_PREINIT_ARRAYSZ: name = "PREINIT_ARRAYSZ"; break; | |
d48188b9 | 1716 | case DT_CHECKSUM: name = "CHECKSUM"; break; |
94558834 L |
1717 | case DT_PLTPADSZ: name = "PLTPADSZ"; break; |
1718 | case DT_MOVEENT: name = "MOVEENT"; break; | |
1719 | case DT_MOVESZ: name = "MOVESZ"; break; | |
1720 | case DT_FEATURE: name = "FEATURE"; break; | |
1721 | case DT_POSFLAG_1: name = "POSFLAG_1"; break; | |
1722 | case DT_SYMINSZ: name = "SYMINSZ"; break; | |
1723 | case DT_SYMINENT: name = "SYMINENT"; break; | |
b34976b6 AM |
1724 | case DT_CONFIG: name = "CONFIG"; stringp = TRUE; break; |
1725 | case DT_DEPAUDIT: name = "DEPAUDIT"; stringp = TRUE; break; | |
1726 | case DT_AUDIT: name = "AUDIT"; stringp = TRUE; break; | |
94558834 L |
1727 | case DT_PLTPAD: name = "PLTPAD"; break; |
1728 | case DT_MOVETAB: name = "MOVETAB"; break; | |
1729 | case DT_SYMINFO: name = "SYMINFO"; break; | |
1730 | case DT_RELACOUNT: name = "RELACOUNT"; break; | |
1731 | case DT_RELCOUNT: name = "RELCOUNT"; break; | |
1732 | case DT_FLAGS_1: name = "FLAGS_1"; break; | |
252b5132 RH |
1733 | case DT_VERSYM: name = "VERSYM"; break; |
1734 | case DT_VERDEF: name = "VERDEF"; break; | |
1735 | case DT_VERDEFNUM: name = "VERDEFNUM"; break; | |
1736 | case DT_VERNEED: name = "VERNEED"; break; | |
1737 | case DT_VERNEEDNUM: name = "VERNEEDNUM"; break; | |
b34976b6 | 1738 | case DT_AUXILIARY: name = "AUXILIARY"; stringp = TRUE; break; |
94558834 | 1739 | case DT_USED: name = "USED"; break; |
b34976b6 | 1740 | case DT_FILTER: name = "FILTER"; stringp = TRUE; break; |
fdc90cb4 | 1741 | case DT_GNU_HASH: name = "GNU_HASH"; break; |
252b5132 RH |
1742 | } |
1743 | ||
ad9563d6 | 1744 | fprintf (f, " %-20s ", name); |
252b5132 | 1745 | if (! stringp) |
a1f3c56e AN |
1746 | { |
1747 | fprintf (f, "0x"); | |
1748 | bfd_fprintf_vma (abfd, f, dyn.d_un.d_val); | |
1749 | } | |
252b5132 RH |
1750 | else |
1751 | { | |
1752 | const char *string; | |
dc810e39 | 1753 | unsigned int tagv = dyn.d_un.d_val; |
252b5132 | 1754 | |
dc810e39 | 1755 | string = bfd_elf_string_from_elf_section (abfd, shlink, tagv); |
252b5132 RH |
1756 | if (string == NULL) |
1757 | goto error_return; | |
1758 | fprintf (f, "%s", string); | |
1759 | } | |
1760 | fprintf (f, "\n"); | |
1761 | } | |
1762 | ||
1763 | free (dynbuf); | |
1764 | dynbuf = NULL; | |
1765 | } | |
1766 | ||
1767 | if ((elf_dynverdef (abfd) != 0 && elf_tdata (abfd)->verdef == NULL) | |
1768 | || (elf_dynverref (abfd) != 0 && elf_tdata (abfd)->verref == NULL)) | |
1769 | { | |
fc0e6df6 | 1770 | if (! _bfd_elf_slurp_version_tables (abfd, FALSE)) |
b34976b6 | 1771 | return FALSE; |
252b5132 RH |
1772 | } |
1773 | ||
1774 | if (elf_dynverdef (abfd) != 0) | |
1775 | { | |
1776 | Elf_Internal_Verdef *t; | |
1777 | ||
1778 | fprintf (f, _("\nVersion definitions:\n")); | |
1779 | for (t = elf_tdata (abfd)->verdef; t != NULL; t = t->vd_nextdef) | |
1780 | { | |
1781 | fprintf (f, "%d 0x%2.2x 0x%8.8lx %s\n", t->vd_ndx, | |
d0fb9a8d JJ |
1782 | t->vd_flags, t->vd_hash, |
1783 | t->vd_nodename ? t->vd_nodename : "<corrupt>"); | |
1784 | if (t->vd_auxptr != NULL && t->vd_auxptr->vda_nextptr != NULL) | |
252b5132 RH |
1785 | { |
1786 | Elf_Internal_Verdaux *a; | |
1787 | ||
1788 | fprintf (f, "\t"); | |
1789 | for (a = t->vd_auxptr->vda_nextptr; | |
1790 | a != NULL; | |
1791 | a = a->vda_nextptr) | |
d0fb9a8d JJ |
1792 | fprintf (f, "%s ", |
1793 | a->vda_nodename ? a->vda_nodename : "<corrupt>"); | |
252b5132 RH |
1794 | fprintf (f, "\n"); |
1795 | } | |
1796 | } | |
1797 | } | |
1798 | ||
1799 | if (elf_dynverref (abfd) != 0) | |
1800 | { | |
1801 | Elf_Internal_Verneed *t; | |
1802 | ||
1803 | fprintf (f, _("\nVersion References:\n")); | |
1804 | for (t = elf_tdata (abfd)->verref; t != NULL; t = t->vn_nextref) | |
1805 | { | |
1806 | Elf_Internal_Vernaux *a; | |
1807 | ||
d0fb9a8d JJ |
1808 | fprintf (f, _(" required from %s:\n"), |
1809 | t->vn_filename ? t->vn_filename : "<corrupt>"); | |
252b5132 RH |
1810 | for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) |
1811 | fprintf (f, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a->vna_hash, | |
d0fb9a8d JJ |
1812 | a->vna_flags, a->vna_other, |
1813 | a->vna_nodename ? a->vna_nodename : "<corrupt>"); | |
252b5132 RH |
1814 | } |
1815 | } | |
1816 | ||
b34976b6 | 1817 | return TRUE; |
252b5132 RH |
1818 | |
1819 | error_return: | |
1820 | if (dynbuf != NULL) | |
1821 | free (dynbuf); | |
b34976b6 | 1822 | return FALSE; |
252b5132 RH |
1823 | } |
1824 | ||
bb4d2ac2 L |
1825 | /* Get version string. */ |
1826 | ||
1827 | const char * | |
60bb06bc L |
1828 | _bfd_elf_get_symbol_version_string (bfd *abfd, asymbol *symbol, |
1829 | bfd_boolean *hidden) | |
bb4d2ac2 L |
1830 | { |
1831 | const char *version_string = NULL; | |
1832 | if (elf_dynversym (abfd) != 0 | |
1833 | && (elf_dynverdef (abfd) != 0 || elf_dynverref (abfd) != 0)) | |
1834 | { | |
1835 | unsigned int vernum = ((elf_symbol_type *) symbol)->version; | |
1836 | ||
1837 | *hidden = (vernum & VERSYM_HIDDEN) != 0; | |
1838 | vernum &= VERSYM_VERSION; | |
1839 | ||
1840 | if (vernum == 0) | |
1841 | version_string = ""; | |
1842 | else if (vernum == 1) | |
1843 | version_string = "Base"; | |
1844 | else if (vernum <= elf_tdata (abfd)->cverdefs) | |
1845 | version_string = | |
1846 | elf_tdata (abfd)->verdef[vernum - 1].vd_nodename; | |
1847 | else | |
1848 | { | |
1849 | Elf_Internal_Verneed *t; | |
1850 | ||
1851 | version_string = ""; | |
1852 | for (t = elf_tdata (abfd)->verref; | |
1853 | t != NULL; | |
1854 | t = t->vn_nextref) | |
1855 | { | |
1856 | Elf_Internal_Vernaux *a; | |
1857 | ||
1858 | for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) | |
1859 | { | |
1860 | if (a->vna_other == vernum) | |
1861 | { | |
1862 | version_string = a->vna_nodename; | |
1863 | break; | |
1864 | } | |
1865 | } | |
1866 | } | |
1867 | } | |
1868 | } | |
1869 | return version_string; | |
1870 | } | |
1871 | ||
252b5132 RH |
1872 | /* Display ELF-specific fields of a symbol. */ |
1873 | ||
1874 | void | |
217aa764 AM |
1875 | bfd_elf_print_symbol (bfd *abfd, |
1876 | void *filep, | |
1877 | asymbol *symbol, | |
1878 | bfd_print_symbol_type how) | |
252b5132 | 1879 | { |
a50b1753 | 1880 | FILE *file = (FILE *) filep; |
252b5132 RH |
1881 | switch (how) |
1882 | { | |
1883 | case bfd_print_symbol_name: | |
1884 | fprintf (file, "%s", symbol->name); | |
1885 | break; | |
1886 | case bfd_print_symbol_more: | |
1887 | fprintf (file, "elf "); | |
60b89a18 | 1888 | bfd_fprintf_vma (abfd, file, symbol->value); |
cd9af601 | 1889 | fprintf (file, " %x", symbol->flags); |
252b5132 RH |
1890 | break; |
1891 | case bfd_print_symbol_all: | |
1892 | { | |
4e8a9624 AM |
1893 | const char *section_name; |
1894 | const char *name = NULL; | |
9c5bfbb7 | 1895 | const struct elf_backend_data *bed; |
7a13edea | 1896 | unsigned char st_other; |
dbb410c3 | 1897 | bfd_vma val; |
bb4d2ac2 L |
1898 | const char *version_string; |
1899 | bfd_boolean hidden; | |
c044fabd | 1900 | |
252b5132 | 1901 | section_name = symbol->section ? symbol->section->name : "(*none*)"; |
587ff49e RH |
1902 | |
1903 | bed = get_elf_backend_data (abfd); | |
1904 | if (bed->elf_backend_print_symbol_all) | |
c044fabd | 1905 | name = (*bed->elf_backend_print_symbol_all) (abfd, filep, symbol); |
587ff49e RH |
1906 | |
1907 | if (name == NULL) | |
1908 | { | |
7ee38065 | 1909 | name = symbol->name; |
217aa764 | 1910 | bfd_print_symbol_vandf (abfd, file, symbol); |
587ff49e RH |
1911 | } |
1912 | ||
252b5132 RH |
1913 | fprintf (file, " %s\t", section_name); |
1914 | /* Print the "other" value for a symbol. For common symbols, | |
1915 | we've already printed the size; now print the alignment. | |
1916 | For other symbols, we have no specified alignment, and | |
1917 | we've printed the address; now print the size. */ | |
dcf6c779 | 1918 | if (symbol->section && bfd_is_com_section (symbol->section)) |
dbb410c3 AM |
1919 | val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_value; |
1920 | else | |
1921 | val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_size; | |
1922 | bfd_fprintf_vma (abfd, file, val); | |
252b5132 RH |
1923 | |
1924 | /* If we have version information, print it. */ | |
60bb06bc L |
1925 | version_string = _bfd_elf_get_symbol_version_string (abfd, |
1926 | symbol, | |
1927 | &hidden); | |
bb4d2ac2 | 1928 | if (version_string) |
252b5132 | 1929 | { |
bb4d2ac2 | 1930 | if (!hidden) |
252b5132 RH |
1931 | fprintf (file, " %-11s", version_string); |
1932 | else | |
1933 | { | |
1934 | int i; | |
1935 | ||
1936 | fprintf (file, " (%s)", version_string); | |
1937 | for (i = 10 - strlen (version_string); i > 0; --i) | |
1938 | putc (' ', file); | |
1939 | } | |
1940 | } | |
1941 | ||
1942 | /* If the st_other field is not zero, print it. */ | |
7a13edea | 1943 | st_other = ((elf_symbol_type *) symbol)->internal_elf_sym.st_other; |
c044fabd | 1944 | |
7a13edea NC |
1945 | switch (st_other) |
1946 | { | |
1947 | case 0: break; | |
1948 | case STV_INTERNAL: fprintf (file, " .internal"); break; | |
1949 | case STV_HIDDEN: fprintf (file, " .hidden"); break; | |
1950 | case STV_PROTECTED: fprintf (file, " .protected"); break; | |
1951 | default: | |
1952 | /* Some other non-defined flags are also present, so print | |
1953 | everything hex. */ | |
1954 | fprintf (file, " 0x%02x", (unsigned int) st_other); | |
1955 | } | |
252b5132 | 1956 | |
587ff49e | 1957 | fprintf (file, " %s", name); |
252b5132 RH |
1958 | } |
1959 | break; | |
1960 | } | |
1961 | } | |
252b5132 RH |
1962 | \f |
1963 | /* ELF .o/exec file reading */ | |
1964 | ||
c044fabd | 1965 | /* Create a new bfd section from an ELF section header. */ |
252b5132 | 1966 | |
b34976b6 | 1967 | bfd_boolean |
217aa764 | 1968 | bfd_section_from_shdr (bfd *abfd, unsigned int shindex) |
252b5132 | 1969 | { |
4fbb74a6 AM |
1970 | Elf_Internal_Shdr *hdr; |
1971 | Elf_Internal_Ehdr *ehdr; | |
1972 | const struct elf_backend_data *bed; | |
90937f86 | 1973 | const char *name; |
bf67003b NC |
1974 | bfd_boolean ret = TRUE; |
1975 | static bfd_boolean * sections_being_created = NULL; | |
5a4b0ccc | 1976 | static bfd * sections_being_created_abfd = NULL; |
bf67003b | 1977 | static unsigned int nesting = 0; |
252b5132 | 1978 | |
4fbb74a6 AM |
1979 | if (shindex >= elf_numsections (abfd)) |
1980 | return FALSE; | |
1981 | ||
bf67003b NC |
1982 | if (++ nesting > 3) |
1983 | { | |
1984 | /* PR17512: A corrupt ELF binary might contain a recursive group of | |
06614111 | 1985 | sections, with each the string indicies pointing to the next in the |
bf67003b NC |
1986 | loop. Detect this here, by refusing to load a section that we are |
1987 | already in the process of loading. We only trigger this test if | |
1988 | we have nested at least three sections deep as normal ELF binaries | |
5a4b0ccc NC |
1989 | can expect to recurse at least once. |
1990 | ||
1991 | FIXME: It would be better if this array was attached to the bfd, | |
1992 | rather than being held in a static pointer. */ | |
1993 | ||
1994 | if (sections_being_created_abfd != abfd) | |
1995 | sections_being_created = NULL; | |
bf67003b NC |
1996 | if (sections_being_created == NULL) |
1997 | { | |
1998 | /* FIXME: It would be more efficient to attach this array to the bfd somehow. */ | |
1999 | sections_being_created = (bfd_boolean *) | |
2000 | bfd_zalloc (abfd, elf_numsections (abfd) * sizeof (bfd_boolean)); | |
5a4b0ccc | 2001 | sections_being_created_abfd = abfd; |
bf67003b NC |
2002 | } |
2003 | if (sections_being_created [shindex]) | |
2004 | { | |
4eca0228 | 2005 | _bfd_error_handler |
bf67003b NC |
2006 | (_("%B: warning: loop in section dependencies detected"), abfd); |
2007 | return FALSE; | |
2008 | } | |
2009 | sections_being_created [shindex] = TRUE; | |
2010 | } | |
2011 | ||
4fbb74a6 AM |
2012 | hdr = elf_elfsections (abfd)[shindex]; |
2013 | ehdr = elf_elfheader (abfd); | |
2014 | name = bfd_elf_string_from_elf_section (abfd, ehdr->e_shstrndx, | |
1b3a8575 | 2015 | hdr->sh_name); |
933d961a | 2016 | if (name == NULL) |
bf67003b | 2017 | goto fail; |
252b5132 | 2018 | |
4fbb74a6 | 2019 | bed = get_elf_backend_data (abfd); |
252b5132 RH |
2020 | switch (hdr->sh_type) |
2021 | { | |
2022 | case SHT_NULL: | |
2023 | /* Inactive section. Throw it away. */ | |
bf67003b | 2024 | goto success; |
252b5132 | 2025 | |
bf67003b NC |
2026 | case SHT_PROGBITS: /* Normal section with contents. */ |
2027 | case SHT_NOBITS: /* .bss section. */ | |
2028 | case SHT_HASH: /* .hash section. */ | |
2029 | case SHT_NOTE: /* .note section. */ | |
25e27870 L |
2030 | case SHT_INIT_ARRAY: /* .init_array section. */ |
2031 | case SHT_FINI_ARRAY: /* .fini_array section. */ | |
2032 | case SHT_PREINIT_ARRAY: /* .preinit_array section. */ | |
7f1204bb | 2033 | case SHT_GNU_LIBLIST: /* .gnu.liblist section. */ |
fdc90cb4 | 2034 | case SHT_GNU_HASH: /* .gnu.hash section. */ |
bf67003b NC |
2035 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); |
2036 | goto success; | |
252b5132 | 2037 | |
797fc050 | 2038 | case SHT_DYNAMIC: /* Dynamic linking information. */ |
6dc132d9 | 2039 | if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex)) |
bf67003b NC |
2040 | goto fail; |
2041 | ||
cfcac11d NC |
2042 | if (hdr->sh_link > elf_numsections (abfd)) |
2043 | { | |
caa83f8b | 2044 | /* PR 10478: Accept Solaris binaries with a sh_link |
cfcac11d NC |
2045 | field set to SHN_BEFORE or SHN_AFTER. */ |
2046 | switch (bfd_get_arch (abfd)) | |
2047 | { | |
caa83f8b | 2048 | case bfd_arch_i386: |
cfcac11d NC |
2049 | case bfd_arch_sparc: |
2050 | if (hdr->sh_link == (SHN_LORESERVE & 0xffff) /* SHN_BEFORE */ | |
2051 | || hdr->sh_link == ((SHN_LORESERVE + 1) & 0xffff) /* SHN_AFTER */) | |
2052 | break; | |
2053 | /* Otherwise fall through. */ | |
2054 | default: | |
bf67003b | 2055 | goto fail; |
cfcac11d NC |
2056 | } |
2057 | } | |
2058 | else if (elf_elfsections (abfd)[hdr->sh_link] == NULL) | |
bf67003b | 2059 | goto fail; |
cfcac11d | 2060 | else if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_STRTAB) |
797fc050 AM |
2061 | { |
2062 | Elf_Internal_Shdr *dynsymhdr; | |
2063 | ||
2064 | /* The shared libraries distributed with hpux11 have a bogus | |
2065 | sh_link field for the ".dynamic" section. Find the | |
2066 | string table for the ".dynsym" section instead. */ | |
2067 | if (elf_dynsymtab (abfd) != 0) | |
2068 | { | |
2069 | dynsymhdr = elf_elfsections (abfd)[elf_dynsymtab (abfd)]; | |
2070 | hdr->sh_link = dynsymhdr->sh_link; | |
2071 | } | |
2072 | else | |
2073 | { | |
2074 | unsigned int i, num_sec; | |
2075 | ||
2076 | num_sec = elf_numsections (abfd); | |
2077 | for (i = 1; i < num_sec; i++) | |
2078 | { | |
2079 | dynsymhdr = elf_elfsections (abfd)[i]; | |
2080 | if (dynsymhdr->sh_type == SHT_DYNSYM) | |
2081 | { | |
2082 | hdr->sh_link = dynsymhdr->sh_link; | |
2083 | break; | |
2084 | } | |
2085 | } | |
2086 | } | |
2087 | } | |
bf67003b | 2088 | goto success; |
797fc050 | 2089 | |
bf67003b | 2090 | case SHT_SYMTAB: /* A symbol table. */ |
252b5132 | 2091 | if (elf_onesymtab (abfd) == shindex) |
bf67003b | 2092 | goto success; |
252b5132 | 2093 | |
a50b2160 | 2094 | if (hdr->sh_entsize != bed->s->sizeof_sym) |
bf67003b NC |
2095 | goto fail; |
2096 | ||
3337c1e5 | 2097 | if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size) |
eee3b786 AM |
2098 | { |
2099 | if (hdr->sh_size != 0) | |
bf67003b | 2100 | goto fail; |
eee3b786 AM |
2101 | /* Some assemblers erroneously set sh_info to one with a |
2102 | zero sh_size. ld sees this as a global symbol count | |
2103 | of (unsigned) -1. Fix it here. */ | |
2104 | hdr->sh_info = 0; | |
bf67003b | 2105 | goto success; |
eee3b786 | 2106 | } |
bf67003b | 2107 | |
16ad13ec NC |
2108 | /* PR 18854: A binary might contain more than one symbol table. |
2109 | Unusual, but possible. Warn, but continue. */ | |
2110 | if (elf_onesymtab (abfd) != 0) | |
2111 | { | |
4eca0228 | 2112 | _bfd_error_handler |
695344c0 | 2113 | /* xgettext:c-format */ |
63a5468a AM |
2114 | (_("%B: warning: multiple symbol tables detected" |
2115 | " - ignoring the table in section %u"), | |
16ad13ec NC |
2116 | abfd, shindex); |
2117 | goto success; | |
2118 | } | |
252b5132 | 2119 | elf_onesymtab (abfd) = shindex; |
6a40cf0c NC |
2120 | elf_symtab_hdr (abfd) = *hdr; |
2121 | elf_elfsections (abfd)[shindex] = hdr = & elf_symtab_hdr (abfd); | |
252b5132 RH |
2122 | abfd->flags |= HAS_SYMS; |
2123 | ||
2124 | /* Sometimes a shared object will map in the symbol table. If | |
08a40648 AM |
2125 | SHF_ALLOC is set, and this is a shared object, then we also |
2126 | treat this section as a BFD section. We can not base the | |
2127 | decision purely on SHF_ALLOC, because that flag is sometimes | |
2128 | set in a relocatable object file, which would confuse the | |
2129 | linker. */ | |
252b5132 RH |
2130 | if ((hdr->sh_flags & SHF_ALLOC) != 0 |
2131 | && (abfd->flags & DYNAMIC) != 0 | |
6dc132d9 L |
2132 | && ! _bfd_elf_make_section_from_shdr (abfd, hdr, name, |
2133 | shindex)) | |
bf67003b | 2134 | goto fail; |
252b5132 | 2135 | |
1b3a8575 AM |
2136 | /* Go looking for SHT_SYMTAB_SHNDX too, since if there is one we |
2137 | can't read symbols without that section loaded as well. It | |
2138 | is most likely specified by the next section header. */ | |
6a40cf0c NC |
2139 | { |
2140 | elf_section_list * entry; | |
2141 | unsigned int i, num_sec; | |
1b3a8575 | 2142 | |
6a40cf0c NC |
2143 | for (entry = elf_symtab_shndx_list (abfd); entry != NULL; entry = entry->next) |
2144 | if (entry->hdr.sh_link == shindex) | |
2145 | goto success; | |
2146 | ||
2147 | num_sec = elf_numsections (abfd); | |
2148 | for (i = shindex + 1; i < num_sec; i++) | |
2149 | { | |
2150 | Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i]; | |
2151 | ||
2152 | if (hdr2->sh_type == SHT_SYMTAB_SHNDX | |
2153 | && hdr2->sh_link == shindex) | |
2154 | break; | |
2155 | } | |
2156 | ||
2157 | if (i == num_sec) | |
2158 | for (i = 1; i < shindex; i++) | |
1b3a8575 AM |
2159 | { |
2160 | Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i]; | |
6a40cf0c | 2161 | |
1b3a8575 AM |
2162 | if (hdr2->sh_type == SHT_SYMTAB_SHNDX |
2163 | && hdr2->sh_link == shindex) | |
2164 | break; | |
2165 | } | |
6a40cf0c NC |
2166 | |
2167 | if (i != shindex) | |
2168 | ret = bfd_section_from_shdr (abfd, i); | |
2169 | /* else FIXME: we have failed to find the symbol table - should we issue an error ? */ | |
2170 | goto success; | |
2171 | } | |
252b5132 | 2172 | |
bf67003b | 2173 | case SHT_DYNSYM: /* A dynamic symbol table. */ |
252b5132 | 2174 | if (elf_dynsymtab (abfd) == shindex) |
bf67003b | 2175 | goto success; |
252b5132 | 2176 | |
a50b2160 | 2177 | if (hdr->sh_entsize != bed->s->sizeof_sym) |
bf67003b NC |
2178 | goto fail; |
2179 | ||
eee3b786 AM |
2180 | if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size) |
2181 | { | |
2182 | if (hdr->sh_size != 0) | |
bf67003b NC |
2183 | goto fail; |
2184 | ||
eee3b786 AM |
2185 | /* Some linkers erroneously set sh_info to one with a |
2186 | zero sh_size. ld sees this as a global symbol count | |
2187 | of (unsigned) -1. Fix it here. */ | |
2188 | hdr->sh_info = 0; | |
bf67003b | 2189 | goto success; |
eee3b786 | 2190 | } |
bf67003b | 2191 | |
16ad13ec NC |
2192 | /* PR 18854: A binary might contain more than one dynamic symbol table. |
2193 | Unusual, but possible. Warn, but continue. */ | |
2194 | if (elf_dynsymtab (abfd) != 0) | |
2195 | { | |
4eca0228 | 2196 | _bfd_error_handler |
695344c0 | 2197 | /* xgettext:c-format */ |
63a5468a AM |
2198 | (_("%B: warning: multiple dynamic symbol tables detected" |
2199 | " - ignoring the table in section %u"), | |
16ad13ec NC |
2200 | abfd, shindex); |
2201 | goto success; | |
2202 | } | |
252b5132 RH |
2203 | elf_dynsymtab (abfd) = shindex; |
2204 | elf_tdata (abfd)->dynsymtab_hdr = *hdr; | |
2205 | elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->dynsymtab_hdr; | |
2206 | abfd->flags |= HAS_SYMS; | |
2207 | ||
2208 | /* Besides being a symbol table, we also treat this as a regular | |
2209 | section, so that objcopy can handle it. */ | |
bf67003b NC |
2210 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); |
2211 | goto success; | |
252b5132 | 2212 | |
bf67003b | 2213 | case SHT_SYMTAB_SHNDX: /* Symbol section indices when >64k sections. */ |
6a40cf0c NC |
2214 | { |
2215 | elf_section_list * entry; | |
9ad5cbcf | 2216 | |
6a40cf0c NC |
2217 | for (entry = elf_symtab_shndx_list (abfd); entry != NULL; entry = entry->next) |
2218 | if (entry->ndx == shindex) | |
2219 | goto success; | |
2220 | ||
2221 | entry = bfd_alloc (abfd, sizeof * entry); | |
2222 | if (entry == NULL) | |
2223 | goto fail; | |
2224 | entry->ndx = shindex; | |
2225 | entry->hdr = * hdr; | |
2226 | entry->next = elf_symtab_shndx_list (abfd); | |
2227 | elf_symtab_shndx_list (abfd) = entry; | |
2228 | elf_elfsections (abfd)[shindex] = & entry->hdr; | |
2229 | goto success; | |
2230 | } | |
9ad5cbcf | 2231 | |
bf67003b | 2232 | case SHT_STRTAB: /* A string table. */ |
252b5132 | 2233 | if (hdr->bfd_section != NULL) |
bf67003b NC |
2234 | goto success; |
2235 | ||
252b5132 RH |
2236 | if (ehdr->e_shstrndx == shindex) |
2237 | { | |
2238 | elf_tdata (abfd)->shstrtab_hdr = *hdr; | |
2239 | elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->shstrtab_hdr; | |
bf67003b | 2240 | goto success; |
252b5132 | 2241 | } |
bf67003b | 2242 | |
1b3a8575 AM |
2243 | if (elf_elfsections (abfd)[elf_onesymtab (abfd)]->sh_link == shindex) |
2244 | { | |
2245 | symtab_strtab: | |
2246 | elf_tdata (abfd)->strtab_hdr = *hdr; | |
2247 | elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->strtab_hdr; | |
bf67003b | 2248 | goto success; |
1b3a8575 | 2249 | } |
bf67003b | 2250 | |
1b3a8575 AM |
2251 | if (elf_elfsections (abfd)[elf_dynsymtab (abfd)]->sh_link == shindex) |
2252 | { | |
2253 | dynsymtab_strtab: | |
2254 | elf_tdata (abfd)->dynstrtab_hdr = *hdr; | |
2255 | hdr = &elf_tdata (abfd)->dynstrtab_hdr; | |
2256 | elf_elfsections (abfd)[shindex] = hdr; | |
2257 | /* We also treat this as a regular section, so that objcopy | |
2258 | can handle it. */ | |
bf67003b NC |
2259 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, |
2260 | shindex); | |
2261 | goto success; | |
1b3a8575 | 2262 | } |
252b5132 | 2263 | |
1b3a8575 AM |
2264 | /* If the string table isn't one of the above, then treat it as a |
2265 | regular section. We need to scan all the headers to be sure, | |
2266 | just in case this strtab section appeared before the above. */ | |
2267 | if (elf_onesymtab (abfd) == 0 || elf_dynsymtab (abfd) == 0) | |
2268 | { | |
2269 | unsigned int i, num_sec; | |
252b5132 | 2270 | |
1b3a8575 AM |
2271 | num_sec = elf_numsections (abfd); |
2272 | for (i = 1; i < num_sec; i++) | |
2273 | { | |
2274 | Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i]; | |
2275 | if (hdr2->sh_link == shindex) | |
2276 | { | |
933d961a JJ |
2277 | /* Prevent endless recursion on broken objects. */ |
2278 | if (i == shindex) | |
bf67003b | 2279 | goto fail; |
1b3a8575 | 2280 | if (! bfd_section_from_shdr (abfd, i)) |
bf67003b | 2281 | goto fail; |
1b3a8575 AM |
2282 | if (elf_onesymtab (abfd) == i) |
2283 | goto symtab_strtab; | |
2284 | if (elf_dynsymtab (abfd) == i) | |
2285 | goto dynsymtab_strtab; | |
2286 | } | |
2287 | } | |
2288 | } | |
bf67003b NC |
2289 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); |
2290 | goto success; | |
252b5132 RH |
2291 | |
2292 | case SHT_REL: | |
2293 | case SHT_RELA: | |
2294 | /* *These* do a lot of work -- but build no sections! */ | |
2295 | { | |
2296 | asection *target_sect; | |
d4730f92 | 2297 | Elf_Internal_Shdr *hdr2, **p_hdr; |
9ad5cbcf | 2298 | unsigned int num_sec = elf_numsections (abfd); |
d4730f92 | 2299 | struct bfd_elf_section_data *esdt; |
252b5132 | 2300 | |
aa2ca951 JJ |
2301 | if (hdr->sh_entsize |
2302 | != (bfd_size_type) (hdr->sh_type == SHT_REL | |
a50b2160 | 2303 | ? bed->s->sizeof_rel : bed->s->sizeof_rela)) |
bf67003b | 2304 | goto fail; |
a50b2160 | 2305 | |
03ae5f59 | 2306 | /* Check for a bogus link to avoid crashing. */ |
4fbb74a6 | 2307 | if (hdr->sh_link >= num_sec) |
03ae5f59 | 2308 | { |
4eca0228 | 2309 | _bfd_error_handler |
695344c0 | 2310 | /* xgettext:c-format */ |
76cfced5 | 2311 | (_("%B: invalid link %u for reloc section %s (index %u)"), |
4eca0228 | 2312 | abfd, hdr->sh_link, name, shindex); |
bf67003b NC |
2313 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, |
2314 | shindex); | |
2315 | goto success; | |
03ae5f59 ILT |
2316 | } |
2317 | ||
252b5132 RH |
2318 | /* For some incomprehensible reason Oracle distributes |
2319 | libraries for Solaris in which some of the objects have | |
2320 | bogus sh_link fields. It would be nice if we could just | |
2321 | reject them, but, unfortunately, some people need to use | |
2322 | them. We scan through the section headers; if we find only | |
2323 | one suitable symbol table, we clobber the sh_link to point | |
83b89087 L |
2324 | to it. I hope this doesn't break anything. |
2325 | ||
2326 | Don't do it on executable nor shared library. */ | |
2327 | if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0 | |
2328 | && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_SYMTAB | |
252b5132 RH |
2329 | && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_DYNSYM) |
2330 | { | |
9ad5cbcf | 2331 | unsigned int scan; |
252b5132 RH |
2332 | int found; |
2333 | ||
2334 | found = 0; | |
9ad5cbcf | 2335 | for (scan = 1; scan < num_sec; scan++) |
252b5132 RH |
2336 | { |
2337 | if (elf_elfsections (abfd)[scan]->sh_type == SHT_SYMTAB | |
2338 | || elf_elfsections (abfd)[scan]->sh_type == SHT_DYNSYM) | |
2339 | { | |
2340 | if (found != 0) | |
2341 | { | |
2342 | found = 0; | |
2343 | break; | |
2344 | } | |
2345 | found = scan; | |
2346 | } | |
2347 | } | |
2348 | if (found != 0) | |
2349 | hdr->sh_link = found; | |
2350 | } | |
2351 | ||
2352 | /* Get the symbol table. */ | |
1b3a8575 AM |
2353 | if ((elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_SYMTAB |
2354 | || elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_DYNSYM) | |
252b5132 | 2355 | && ! bfd_section_from_shdr (abfd, hdr->sh_link)) |
bf67003b | 2356 | goto fail; |
252b5132 RH |
2357 | |
2358 | /* If this reloc section does not use the main symbol table we | |
2359 | don't treat it as a reloc section. BFD can't adequately | |
2360 | represent such a section, so at least for now, we don't | |
c044fabd | 2361 | try. We just present it as a normal section. We also |
60bcf0fa | 2362 | can't use it as a reloc section if it points to the null |
83b89087 L |
2363 | section, an invalid section, another reloc section, or its |
2364 | sh_link points to the null section. */ | |
185ef66d | 2365 | if (hdr->sh_link != elf_onesymtab (abfd) |
83b89087 | 2366 | || hdr->sh_link == SHN_UNDEF |
185ef66d | 2367 | || hdr->sh_info == SHN_UNDEF |
185ef66d AM |
2368 | || hdr->sh_info >= num_sec |
2369 | || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_REL | |
2370 | || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_RELA) | |
bf67003b NC |
2371 | { |
2372 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, | |
2373 | shindex); | |
2374 | goto success; | |
2375 | } | |
252b5132 RH |
2376 | |
2377 | if (! bfd_section_from_shdr (abfd, hdr->sh_info)) | |
bf67003b NC |
2378 | goto fail; |
2379 | ||
252b5132 RH |
2380 | target_sect = bfd_section_from_elf_index (abfd, hdr->sh_info); |
2381 | if (target_sect == NULL) | |
bf67003b | 2382 | goto fail; |
252b5132 | 2383 | |
d4730f92 BS |
2384 | esdt = elf_section_data (target_sect); |
2385 | if (hdr->sh_type == SHT_RELA) | |
2386 | p_hdr = &esdt->rela.hdr; | |
252b5132 | 2387 | else |
d4730f92 BS |
2388 | p_hdr = &esdt->rel.hdr; |
2389 | ||
06614111 NC |
2390 | /* PR 17512: file: 0b4f81b7. */ |
2391 | if (*p_hdr != NULL) | |
2392 | goto fail; | |
ef53be89 | 2393 | hdr2 = (Elf_Internal_Shdr *) bfd_alloc (abfd, sizeof (*hdr2)); |
d4730f92 | 2394 | if (hdr2 == NULL) |
bf67003b | 2395 | goto fail; |
252b5132 | 2396 | *hdr2 = *hdr; |
d4730f92 | 2397 | *p_hdr = hdr2; |
252b5132 | 2398 | elf_elfsections (abfd)[shindex] = hdr2; |
056bafd4 MR |
2399 | target_sect->reloc_count += (NUM_SHDR_ENTRIES (hdr) |
2400 | * bed->s->int_rels_per_ext_rel); | |
252b5132 RH |
2401 | target_sect->flags |= SEC_RELOC; |
2402 | target_sect->relocation = NULL; | |
2403 | target_sect->rel_filepos = hdr->sh_offset; | |
bf572ba0 MM |
2404 | /* In the section to which the relocations apply, mark whether |
2405 | its relocations are of the REL or RELA variety. */ | |
72730e0c | 2406 | if (hdr->sh_size != 0) |
d4730f92 BS |
2407 | { |
2408 | if (hdr->sh_type == SHT_RELA) | |
2409 | target_sect->use_rela_p = 1; | |
2410 | } | |
252b5132 | 2411 | abfd->flags |= HAS_RELOC; |
bf67003b | 2412 | goto success; |
252b5132 | 2413 | } |
252b5132 RH |
2414 | |
2415 | case SHT_GNU_verdef: | |
2416 | elf_dynverdef (abfd) = shindex; | |
2417 | elf_tdata (abfd)->dynverdef_hdr = *hdr; | |
bf67003b NC |
2418 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); |
2419 | goto success; | |
252b5132 RH |
2420 | |
2421 | case SHT_GNU_versym: | |
a50b2160 | 2422 | if (hdr->sh_entsize != sizeof (Elf_External_Versym)) |
bf67003b NC |
2423 | goto fail; |
2424 | ||
252b5132 RH |
2425 | elf_dynversym (abfd) = shindex; |
2426 | elf_tdata (abfd)->dynversym_hdr = *hdr; | |
bf67003b NC |
2427 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); |
2428 | goto success; | |
252b5132 RH |
2429 | |
2430 | case SHT_GNU_verneed: | |
2431 | elf_dynverref (abfd) = shindex; | |
2432 | elf_tdata (abfd)->dynverref_hdr = *hdr; | |
bf67003b NC |
2433 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); |
2434 | goto success; | |
252b5132 RH |
2435 | |
2436 | case SHT_SHLIB: | |
bf67003b | 2437 | goto success; |
252b5132 | 2438 | |
dbb410c3 | 2439 | case SHT_GROUP: |
44534af3 | 2440 | if (! IS_VALID_GROUP_SECTION_HEADER (hdr, GRP_ENTRY_SIZE)) |
bf67003b NC |
2441 | goto fail; |
2442 | ||
6dc132d9 | 2443 | if (!_bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex)) |
bf67003b NC |
2444 | goto fail; |
2445 | ||
bf67003b | 2446 | goto success; |
dbb410c3 | 2447 | |
252b5132 | 2448 | default: |
104d59d1 JM |
2449 | /* Possibly an attributes section. */ |
2450 | if (hdr->sh_type == SHT_GNU_ATTRIBUTES | |
2451 | || hdr->sh_type == bed->obj_attrs_section_type) | |
2452 | { | |
2453 | if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex)) | |
bf67003b | 2454 | goto fail; |
104d59d1 | 2455 | _bfd_elf_parse_attributes (abfd, hdr); |
bf67003b | 2456 | goto success; |
104d59d1 JM |
2457 | } |
2458 | ||
252b5132 | 2459 | /* Check for any processor-specific section types. */ |
3eb70a79 | 2460 | if (bed->elf_backend_section_from_shdr (abfd, hdr, name, shindex)) |
bf67003b | 2461 | goto success; |
3eb70a79 L |
2462 | |
2463 | if (hdr->sh_type >= SHT_LOUSER && hdr->sh_type <= SHT_HIUSER) | |
2464 | { | |
2465 | if ((hdr->sh_flags & SHF_ALLOC) != 0) | |
2466 | /* FIXME: How to properly handle allocated section reserved | |
2467 | for applications? */ | |
4eca0228 | 2468 | _bfd_error_handler |
695344c0 | 2469 | /* xgettext:c-format */ |
76cfced5 AM |
2470 | (_("%B: unknown type [%#x] section `%s'"), |
2471 | abfd, hdr->sh_type, name); | |
3eb70a79 | 2472 | else |
bf67003b NC |
2473 | { |
2474 | /* Allow sections reserved for applications. */ | |
2475 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, | |
2476 | shindex); | |
2477 | goto success; | |
2478 | } | |
3eb70a79 L |
2479 | } |
2480 | else if (hdr->sh_type >= SHT_LOPROC | |
2481 | && hdr->sh_type <= SHT_HIPROC) | |
2482 | /* FIXME: We should handle this section. */ | |
4eca0228 | 2483 | _bfd_error_handler |
695344c0 | 2484 | /* xgettext:c-format */ |
76cfced5 AM |
2485 | (_("%B: unknown type [%#x] section `%s'"), |
2486 | abfd, hdr->sh_type, name); | |
3eb70a79 | 2487 | else if (hdr->sh_type >= SHT_LOOS && hdr->sh_type <= SHT_HIOS) |
ff15b240 NC |
2488 | { |
2489 | /* Unrecognised OS-specific sections. */ | |
2490 | if ((hdr->sh_flags & SHF_OS_NONCONFORMING) != 0) | |
2491 | /* SHF_OS_NONCONFORMING indicates that special knowledge is | |
08a40648 | 2492 | required to correctly process the section and the file should |
ff15b240 | 2493 | be rejected with an error message. */ |
4eca0228 | 2494 | _bfd_error_handler |
695344c0 | 2495 | /* xgettext:c-format */ |
76cfced5 AM |
2496 | (_("%B: unknown type [%#x] section `%s'"), |
2497 | abfd, hdr->sh_type, name); | |
ff15b240 | 2498 | else |
bf67003b NC |
2499 | { |
2500 | /* Otherwise it should be processed. */ | |
2501 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); | |
2502 | goto success; | |
2503 | } | |
ff15b240 | 2504 | } |
3eb70a79 L |
2505 | else |
2506 | /* FIXME: We should handle this section. */ | |
4eca0228 | 2507 | _bfd_error_handler |
695344c0 | 2508 | /* xgettext:c-format */ |
76cfced5 AM |
2509 | (_("%B: unknown type [%#x] section `%s'"), |
2510 | abfd, hdr->sh_type, name); | |
3eb70a79 | 2511 | |
bf67003b | 2512 | goto fail; |
252b5132 RH |
2513 | } |
2514 | ||
bf67003b NC |
2515 | fail: |
2516 | ret = FALSE; | |
2517 | success: | |
e5b470e2 | 2518 | if (sections_being_created && sections_being_created_abfd == abfd) |
bf67003b NC |
2519 | sections_being_created [shindex] = FALSE; |
2520 | if (-- nesting == 0) | |
5a4b0ccc NC |
2521 | { |
2522 | sections_being_created = NULL; | |
2523 | sections_being_created_abfd = abfd; | |
2524 | } | |
bf67003b | 2525 | return ret; |
252b5132 RH |
2526 | } |
2527 | ||
87d72d41 | 2528 | /* Return the local symbol specified by ABFD, R_SYMNDX. */ |
ec338859 | 2529 | |
87d72d41 AM |
2530 | Elf_Internal_Sym * |
2531 | bfd_sym_from_r_symndx (struct sym_cache *cache, | |
2532 | bfd *abfd, | |
2533 | unsigned long r_symndx) | |
ec338859 | 2534 | { |
ec338859 AM |
2535 | unsigned int ent = r_symndx % LOCAL_SYM_CACHE_SIZE; |
2536 | ||
a5d1b3b5 AM |
2537 | if (cache->abfd != abfd || cache->indx[ent] != r_symndx) |
2538 | { | |
2539 | Elf_Internal_Shdr *symtab_hdr; | |
2540 | unsigned char esym[sizeof (Elf64_External_Sym)]; | |
2541 | Elf_External_Sym_Shndx eshndx; | |
ec338859 | 2542 | |
a5d1b3b5 AM |
2543 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; |
2544 | if (bfd_elf_get_elf_syms (abfd, symtab_hdr, 1, r_symndx, | |
87d72d41 | 2545 | &cache->sym[ent], esym, &eshndx) == NULL) |
a5d1b3b5 | 2546 | return NULL; |
9ad5cbcf | 2547 | |
a5d1b3b5 AM |
2548 | if (cache->abfd != abfd) |
2549 | { | |
2550 | memset (cache->indx, -1, sizeof (cache->indx)); | |
2551 | cache->abfd = abfd; | |
2552 | } | |
2553 | cache->indx[ent] = r_symndx; | |
ec338859 | 2554 | } |
a5d1b3b5 | 2555 | |
87d72d41 | 2556 | return &cache->sym[ent]; |
ec338859 AM |
2557 | } |
2558 | ||
252b5132 RH |
2559 | /* Given an ELF section number, retrieve the corresponding BFD |
2560 | section. */ | |
2561 | ||
2562 | asection * | |
91d6fa6a | 2563 | bfd_section_from_elf_index (bfd *abfd, unsigned int sec_index) |
252b5132 | 2564 | { |
91d6fa6a | 2565 | if (sec_index >= elf_numsections (abfd)) |
252b5132 | 2566 | return NULL; |
91d6fa6a | 2567 | return elf_elfsections (abfd)[sec_index]->bfd_section; |
252b5132 RH |
2568 | } |
2569 | ||
b35d266b | 2570 | static const struct bfd_elf_special_section special_sections_b[] = |
2f89ff8d | 2571 | { |
0112cd26 NC |
2572 | { STRING_COMMA_LEN (".bss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE }, |
2573 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2574 | }; |
2575 | ||
b35d266b | 2576 | static const struct bfd_elf_special_section special_sections_c[] = |
7f4d3958 | 2577 | { |
0112cd26 NC |
2578 | { STRING_COMMA_LEN (".comment"), 0, SHT_PROGBITS, 0 }, |
2579 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2580 | }; |
2581 | ||
b35d266b | 2582 | static const struct bfd_elf_special_section special_sections_d[] = |
7f4d3958 | 2583 | { |
0112cd26 NC |
2584 | { STRING_COMMA_LEN (".data"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE }, |
2585 | { STRING_COMMA_LEN (".data1"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE }, | |
a9a72a65 DE |
2586 | /* There are more DWARF sections than these, but they needn't be added here |
2587 | unless you have to cope with broken compilers that don't emit section | |
2588 | attributes or you want to help the user writing assembler. */ | |
0112cd26 NC |
2589 | { STRING_COMMA_LEN (".debug"), 0, SHT_PROGBITS, 0 }, |
2590 | { STRING_COMMA_LEN (".debug_line"), 0, SHT_PROGBITS, 0 }, | |
2591 | { STRING_COMMA_LEN (".debug_info"), 0, SHT_PROGBITS, 0 }, | |
2592 | { STRING_COMMA_LEN (".debug_abbrev"), 0, SHT_PROGBITS, 0 }, | |
2593 | { STRING_COMMA_LEN (".debug_aranges"), 0, SHT_PROGBITS, 0 }, | |
2594 | { STRING_COMMA_LEN (".dynamic"), 0, SHT_DYNAMIC, SHF_ALLOC }, | |
2595 | { STRING_COMMA_LEN (".dynstr"), 0, SHT_STRTAB, SHF_ALLOC }, | |
2596 | { STRING_COMMA_LEN (".dynsym"), 0, SHT_DYNSYM, SHF_ALLOC }, | |
2597 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2598 | }; |
2599 | ||
b35d266b | 2600 | static const struct bfd_elf_special_section special_sections_f[] = |
7f4d3958 | 2601 | { |
6f9dbcd4 AM |
2602 | { STRING_COMMA_LEN (".fini"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR }, |
2603 | { STRING_COMMA_LEN (".fini_array"), -2, SHT_FINI_ARRAY, SHF_ALLOC + SHF_WRITE }, | |
2604 | { NULL, 0 , 0, 0, 0 } | |
7f4d3958 L |
2605 | }; |
2606 | ||
b35d266b | 2607 | static const struct bfd_elf_special_section special_sections_g[] = |
7f4d3958 | 2608 | { |
0112cd26 | 2609 | { STRING_COMMA_LEN (".gnu.linkonce.b"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE }, |
68efed41 | 2610 | { STRING_COMMA_LEN (".gnu.lto_"), -1, SHT_PROGBITS, SHF_EXCLUDE }, |
0112cd26 NC |
2611 | { STRING_COMMA_LEN (".got"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE }, |
2612 | { STRING_COMMA_LEN (".gnu.version"), 0, SHT_GNU_versym, 0 }, | |
2613 | { STRING_COMMA_LEN (".gnu.version_d"), 0, SHT_GNU_verdef, 0 }, | |
2614 | { STRING_COMMA_LEN (".gnu.version_r"), 0, SHT_GNU_verneed, 0 }, | |
2615 | { STRING_COMMA_LEN (".gnu.liblist"), 0, SHT_GNU_LIBLIST, SHF_ALLOC }, | |
2616 | { STRING_COMMA_LEN (".gnu.conflict"), 0, SHT_RELA, SHF_ALLOC }, | |
2617 | { STRING_COMMA_LEN (".gnu.hash"), 0, SHT_GNU_HASH, SHF_ALLOC }, | |
2618 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2619 | }; |
2620 | ||
b35d266b | 2621 | static const struct bfd_elf_special_section special_sections_h[] = |
7f4d3958 | 2622 | { |
0112cd26 NC |
2623 | { STRING_COMMA_LEN (".hash"), 0, SHT_HASH, SHF_ALLOC }, |
2624 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2625 | }; |
2626 | ||
b35d266b | 2627 | static const struct bfd_elf_special_section special_sections_i[] = |
7f4d3958 | 2628 | { |
6f9dbcd4 AM |
2629 | { STRING_COMMA_LEN (".init"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR }, |
2630 | { STRING_COMMA_LEN (".init_array"), -2, SHT_INIT_ARRAY, SHF_ALLOC + SHF_WRITE }, | |
2631 | { STRING_COMMA_LEN (".interp"), 0, SHT_PROGBITS, 0 }, | |
2632 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2633 | }; |
2634 | ||
b35d266b | 2635 | static const struct bfd_elf_special_section special_sections_l[] = |
7f4d3958 | 2636 | { |
0112cd26 NC |
2637 | { STRING_COMMA_LEN (".line"), 0, SHT_PROGBITS, 0 }, |
2638 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2639 | }; |
2640 | ||
b35d266b | 2641 | static const struct bfd_elf_special_section special_sections_n[] = |
7f4d3958 | 2642 | { |
0112cd26 NC |
2643 | { STRING_COMMA_LEN (".note.GNU-stack"), 0, SHT_PROGBITS, 0 }, |
2644 | { STRING_COMMA_LEN (".note"), -1, SHT_NOTE, 0 }, | |
2645 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2646 | }; |
2647 | ||
b35d266b | 2648 | static const struct bfd_elf_special_section special_sections_p[] = |
7f4d3958 | 2649 | { |
6f9dbcd4 AM |
2650 | { STRING_COMMA_LEN (".preinit_array"), -2, SHT_PREINIT_ARRAY, SHF_ALLOC + SHF_WRITE }, |
2651 | { STRING_COMMA_LEN (".plt"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR }, | |
2652 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2653 | }; |
2654 | ||
b35d266b | 2655 | static const struct bfd_elf_special_section special_sections_r[] = |
7f4d3958 | 2656 | { |
0112cd26 NC |
2657 | { STRING_COMMA_LEN (".rodata"), -2, SHT_PROGBITS, SHF_ALLOC }, |
2658 | { STRING_COMMA_LEN (".rodata1"), 0, SHT_PROGBITS, SHF_ALLOC }, | |
2659 | { STRING_COMMA_LEN (".rela"), -1, SHT_RELA, 0 }, | |
2660 | { STRING_COMMA_LEN (".rel"), -1, SHT_REL, 0 }, | |
2661 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2662 | }; |
2663 | ||
b35d266b | 2664 | static const struct bfd_elf_special_section special_sections_s[] = |
7f4d3958 | 2665 | { |
0112cd26 NC |
2666 | { STRING_COMMA_LEN (".shstrtab"), 0, SHT_STRTAB, 0 }, |
2667 | { STRING_COMMA_LEN (".strtab"), 0, SHT_STRTAB, 0 }, | |
2668 | { STRING_COMMA_LEN (".symtab"), 0, SHT_SYMTAB, 0 }, | |
60ff4dc4 HPN |
2669 | /* See struct bfd_elf_special_section declaration for the semantics of |
2670 | this special case where .prefix_length != strlen (.prefix). */ | |
2671 | { ".stabstr", 5, 3, SHT_STRTAB, 0 }, | |
0112cd26 | 2672 | { NULL, 0, 0, 0, 0 } |
2f89ff8d L |
2673 | }; |
2674 | ||
b35d266b | 2675 | static const struct bfd_elf_special_section special_sections_t[] = |
7f4d3958 | 2676 | { |
0112cd26 NC |
2677 | { STRING_COMMA_LEN (".text"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR }, |
2678 | { STRING_COMMA_LEN (".tbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS }, | |
2679 | { STRING_COMMA_LEN (".tdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS }, | |
2680 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2681 | }; |
2682 | ||
1b315056 CS |
2683 | static const struct bfd_elf_special_section special_sections_z[] = |
2684 | { | |
2685 | { STRING_COMMA_LEN (".zdebug_line"), 0, SHT_PROGBITS, 0 }, | |
2686 | { STRING_COMMA_LEN (".zdebug_info"), 0, SHT_PROGBITS, 0 }, | |
2687 | { STRING_COMMA_LEN (".zdebug_abbrev"), 0, SHT_PROGBITS, 0 }, | |
2688 | { STRING_COMMA_LEN (".zdebug_aranges"), 0, SHT_PROGBITS, 0 }, | |
2689 | { NULL, 0, 0, 0, 0 } | |
2690 | }; | |
2691 | ||
e4c93b56 | 2692 | static const struct bfd_elf_special_section * const special_sections[] = |
7f4d3958 | 2693 | { |
7f4d3958 | 2694 | special_sections_b, /* 'b' */ |
98ece1b3 | 2695 | special_sections_c, /* 'c' */ |
7f4d3958 L |
2696 | special_sections_d, /* 'd' */ |
2697 | NULL, /* 'e' */ | |
2698 | special_sections_f, /* 'f' */ | |
2699 | special_sections_g, /* 'g' */ | |
2700 | special_sections_h, /* 'h' */ | |
2701 | special_sections_i, /* 'i' */ | |
2702 | NULL, /* 'j' */ | |
2703 | NULL, /* 'k' */ | |
2704 | special_sections_l, /* 'l' */ | |
2705 | NULL, /* 'm' */ | |
2706 | special_sections_n, /* 'n' */ | |
2707 | NULL, /* 'o' */ | |
2708 | special_sections_p, /* 'p' */ | |
2709 | NULL, /* 'q' */ | |
2710 | special_sections_r, /* 'r' */ | |
2711 | special_sections_s, /* 's' */ | |
2712 | special_sections_t, /* 't' */ | |
1b315056 CS |
2713 | NULL, /* 'u' */ |
2714 | NULL, /* 'v' */ | |
2715 | NULL, /* 'w' */ | |
2716 | NULL, /* 'x' */ | |
2717 | NULL, /* 'y' */ | |
2718 | special_sections_z /* 'z' */ | |
7f4d3958 L |
2719 | }; |
2720 | ||
551b43fd AM |
2721 | const struct bfd_elf_special_section * |
2722 | _bfd_elf_get_special_section (const char *name, | |
2723 | const struct bfd_elf_special_section *spec, | |
2724 | unsigned int rela) | |
2f89ff8d L |
2725 | { |
2726 | int i; | |
7f4d3958 | 2727 | int len; |
7f4d3958 | 2728 | |
551b43fd | 2729 | len = strlen (name); |
7f4d3958 | 2730 | |
551b43fd | 2731 | for (i = 0; spec[i].prefix != NULL; i++) |
7dcb9820 AM |
2732 | { |
2733 | int suffix_len; | |
551b43fd | 2734 | int prefix_len = spec[i].prefix_length; |
7dcb9820 AM |
2735 | |
2736 | if (len < prefix_len) | |
2737 | continue; | |
551b43fd | 2738 | if (memcmp (name, spec[i].prefix, prefix_len) != 0) |
7dcb9820 AM |
2739 | continue; |
2740 | ||
551b43fd | 2741 | suffix_len = spec[i].suffix_length; |
7dcb9820 AM |
2742 | if (suffix_len <= 0) |
2743 | { | |
2744 | if (name[prefix_len] != 0) | |
2745 | { | |
2746 | if (suffix_len == 0) | |
2747 | continue; | |
2748 | if (name[prefix_len] != '.' | |
2749 | && (suffix_len == -2 | |
551b43fd | 2750 | || (rela && spec[i].type == SHT_REL))) |
7dcb9820 AM |
2751 | continue; |
2752 | } | |
2753 | } | |
2754 | else | |
2755 | { | |
2756 | if (len < prefix_len + suffix_len) | |
2757 | continue; | |
2758 | if (memcmp (name + len - suffix_len, | |
551b43fd | 2759 | spec[i].prefix + prefix_len, |
7dcb9820 AM |
2760 | suffix_len) != 0) |
2761 | continue; | |
2762 | } | |
551b43fd | 2763 | return &spec[i]; |
7dcb9820 | 2764 | } |
2f89ff8d L |
2765 | |
2766 | return NULL; | |
2767 | } | |
2768 | ||
7dcb9820 | 2769 | const struct bfd_elf_special_section * |
29ef7005 | 2770 | _bfd_elf_get_sec_type_attr (bfd *abfd, asection *sec) |
2f89ff8d | 2771 | { |
551b43fd AM |
2772 | int i; |
2773 | const struct bfd_elf_special_section *spec; | |
29ef7005 | 2774 | const struct elf_backend_data *bed; |
2f89ff8d L |
2775 | |
2776 | /* See if this is one of the special sections. */ | |
551b43fd AM |
2777 | if (sec->name == NULL) |
2778 | return NULL; | |
2f89ff8d | 2779 | |
29ef7005 L |
2780 | bed = get_elf_backend_data (abfd); |
2781 | spec = bed->special_sections; | |
2782 | if (spec) | |
2783 | { | |
2784 | spec = _bfd_elf_get_special_section (sec->name, | |
2785 | bed->special_sections, | |
2786 | sec->use_rela_p); | |
2787 | if (spec != NULL) | |
2788 | return spec; | |
2789 | } | |
2790 | ||
551b43fd AM |
2791 | if (sec->name[0] != '.') |
2792 | return NULL; | |
2f89ff8d | 2793 | |
551b43fd | 2794 | i = sec->name[1] - 'b'; |
1b315056 | 2795 | if (i < 0 || i > 'z' - 'b') |
551b43fd AM |
2796 | return NULL; |
2797 | ||
2798 | spec = special_sections[i]; | |
2f89ff8d | 2799 | |
551b43fd AM |
2800 | if (spec == NULL) |
2801 | return NULL; | |
2802 | ||
2803 | return _bfd_elf_get_special_section (sec->name, spec, sec->use_rela_p); | |
2f89ff8d L |
2804 | } |
2805 | ||
b34976b6 | 2806 | bfd_boolean |
217aa764 | 2807 | _bfd_elf_new_section_hook (bfd *abfd, asection *sec) |
252b5132 RH |
2808 | { |
2809 | struct bfd_elf_section_data *sdata; | |
551b43fd | 2810 | const struct elf_backend_data *bed; |
7dcb9820 | 2811 | const struct bfd_elf_special_section *ssect; |
252b5132 | 2812 | |
f0abc2a1 AM |
2813 | sdata = (struct bfd_elf_section_data *) sec->used_by_bfd; |
2814 | if (sdata == NULL) | |
2815 | { | |
a50b1753 NC |
2816 | sdata = (struct bfd_elf_section_data *) bfd_zalloc (abfd, |
2817 | sizeof (*sdata)); | |
f0abc2a1 AM |
2818 | if (sdata == NULL) |
2819 | return FALSE; | |
217aa764 | 2820 | sec->used_by_bfd = sdata; |
f0abc2a1 | 2821 | } |
bf572ba0 | 2822 | |
551b43fd AM |
2823 | /* Indicate whether or not this section should use RELA relocations. */ |
2824 | bed = get_elf_backend_data (abfd); | |
2825 | sec->use_rela_p = bed->default_use_rela_p; | |
2826 | ||
e843e0f8 L |
2827 | /* When we read a file, we don't need to set ELF section type and |
2828 | flags. They will be overridden in _bfd_elf_make_section_from_shdr | |
2829 | anyway. We will set ELF section type and flags for all linker | |
2830 | created sections. If user specifies BFD section flags, we will | |
2831 | set ELF section type and flags based on BFD section flags in | |
02ecc8e9 L |
2832 | elf_fake_sections. Special handling for .init_array/.fini_array |
2833 | output sections since they may contain .ctors/.dtors input | |
2834 | sections. We don't want _bfd_elf_init_private_section_data to | |
2835 | copy ELF section type from .ctors/.dtors input sections. */ | |
2836 | if (abfd->direction != read_direction | |
3496cb2a | 2837 | || (sec->flags & SEC_LINKER_CREATED) != 0) |
2f89ff8d | 2838 | { |
551b43fd | 2839 | ssect = (*bed->get_sec_type_attr) (abfd, sec); |
02ecc8e9 L |
2840 | if (ssect != NULL |
2841 | && (!sec->flags | |
2842 | || (sec->flags & SEC_LINKER_CREATED) != 0 | |
2843 | || ssect->type == SHT_INIT_ARRAY | |
2844 | || ssect->type == SHT_FINI_ARRAY)) | |
a31501e9 L |
2845 | { |
2846 | elf_section_type (sec) = ssect->type; | |
2847 | elf_section_flags (sec) = ssect->attr; | |
2848 | } | |
2f89ff8d L |
2849 | } |
2850 | ||
f592407e | 2851 | return _bfd_generic_new_section_hook (abfd, sec); |
252b5132 RH |
2852 | } |
2853 | ||
2854 | /* Create a new bfd section from an ELF program header. | |
2855 | ||
2856 | Since program segments have no names, we generate a synthetic name | |
2857 | of the form segment<NUM>, where NUM is generally the index in the | |
2858 | program header table. For segments that are split (see below) we | |
2859 | generate the names segment<NUM>a and segment<NUM>b. | |
2860 | ||
2861 | Note that some program segments may have a file size that is different than | |
2862 | (less than) the memory size. All this means is that at execution the | |
2863 | system must allocate the amount of memory specified by the memory size, | |
2864 | but only initialize it with the first "file size" bytes read from the | |
2865 | file. This would occur for example, with program segments consisting | |
2866 | of combined data+bss. | |
2867 | ||
2868 | To handle the above situation, this routine generates TWO bfd sections | |
2869 | for the single program segment. The first has the length specified by | |
2870 | the file size of the segment, and the second has the length specified | |
2871 | by the difference between the two sizes. In effect, the segment is split | |
d5191d0c | 2872 | into its initialized and uninitialized parts. |
252b5132 RH |
2873 | |
2874 | */ | |
2875 | ||
b34976b6 | 2876 | bfd_boolean |
217aa764 AM |
2877 | _bfd_elf_make_section_from_phdr (bfd *abfd, |
2878 | Elf_Internal_Phdr *hdr, | |
91d6fa6a | 2879 | int hdr_index, |
a50b1753 | 2880 | const char *type_name) |
252b5132 RH |
2881 | { |
2882 | asection *newsect; | |
2883 | char *name; | |
2884 | char namebuf[64]; | |
d4c88bbb | 2885 | size_t len; |
252b5132 RH |
2886 | int split; |
2887 | ||
2888 | split = ((hdr->p_memsz > 0) | |
2889 | && (hdr->p_filesz > 0) | |
2890 | && (hdr->p_memsz > hdr->p_filesz)); | |
d5191d0c AM |
2891 | |
2892 | if (hdr->p_filesz > 0) | |
252b5132 | 2893 | { |
91d6fa6a | 2894 | sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "a" : ""); |
d5191d0c | 2895 | len = strlen (namebuf) + 1; |
a50b1753 | 2896 | name = (char *) bfd_alloc (abfd, len); |
d5191d0c AM |
2897 | if (!name) |
2898 | return FALSE; | |
2899 | memcpy (name, namebuf, len); | |
2900 | newsect = bfd_make_section (abfd, name); | |
2901 | if (newsect == NULL) | |
2902 | return FALSE; | |
2903 | newsect->vma = hdr->p_vaddr; | |
2904 | newsect->lma = hdr->p_paddr; | |
2905 | newsect->size = hdr->p_filesz; | |
2906 | newsect->filepos = hdr->p_offset; | |
2907 | newsect->flags |= SEC_HAS_CONTENTS; | |
2908 | newsect->alignment_power = bfd_log2 (hdr->p_align); | |
2909 | if (hdr->p_type == PT_LOAD) | |
252b5132 | 2910 | { |
d5191d0c AM |
2911 | newsect->flags |= SEC_ALLOC; |
2912 | newsect->flags |= SEC_LOAD; | |
2913 | if (hdr->p_flags & PF_X) | |
2914 | { | |
2915 | /* FIXME: all we known is that it has execute PERMISSION, | |
2916 | may be data. */ | |
2917 | newsect->flags |= SEC_CODE; | |
2918 | } | |
2919 | } | |
2920 | if (!(hdr->p_flags & PF_W)) | |
2921 | { | |
2922 | newsect->flags |= SEC_READONLY; | |
252b5132 | 2923 | } |
252b5132 RH |
2924 | } |
2925 | ||
d5191d0c | 2926 | if (hdr->p_memsz > hdr->p_filesz) |
252b5132 | 2927 | { |
d5191d0c AM |
2928 | bfd_vma align; |
2929 | ||
91d6fa6a | 2930 | sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "b" : ""); |
d4c88bbb | 2931 | len = strlen (namebuf) + 1; |
a50b1753 | 2932 | name = (char *) bfd_alloc (abfd, len); |
252b5132 | 2933 | if (!name) |
b34976b6 | 2934 | return FALSE; |
d4c88bbb | 2935 | memcpy (name, namebuf, len); |
252b5132 RH |
2936 | newsect = bfd_make_section (abfd, name); |
2937 | if (newsect == NULL) | |
b34976b6 | 2938 | return FALSE; |
252b5132 RH |
2939 | newsect->vma = hdr->p_vaddr + hdr->p_filesz; |
2940 | newsect->lma = hdr->p_paddr + hdr->p_filesz; | |
eea6121a | 2941 | newsect->size = hdr->p_memsz - hdr->p_filesz; |
d5191d0c AM |
2942 | newsect->filepos = hdr->p_offset + hdr->p_filesz; |
2943 | align = newsect->vma & -newsect->vma; | |
2944 | if (align == 0 || align > hdr->p_align) | |
2945 | align = hdr->p_align; | |
2946 | newsect->alignment_power = bfd_log2 (align); | |
252b5132 RH |
2947 | if (hdr->p_type == PT_LOAD) |
2948 | { | |
d5191d0c AM |
2949 | /* Hack for gdb. Segments that have not been modified do |
2950 | not have their contents written to a core file, on the | |
2951 | assumption that a debugger can find the contents in the | |
2952 | executable. We flag this case by setting the fake | |
2953 | section size to zero. Note that "real" bss sections will | |
2954 | always have their contents dumped to the core file. */ | |
2955 | if (bfd_get_format (abfd) == bfd_core) | |
2956 | newsect->size = 0; | |
252b5132 RH |
2957 | newsect->flags |= SEC_ALLOC; |
2958 | if (hdr->p_flags & PF_X) | |
2959 | newsect->flags |= SEC_CODE; | |
2960 | } | |
2961 | if (!(hdr->p_flags & PF_W)) | |
2962 | newsect->flags |= SEC_READONLY; | |
2963 | } | |
2964 | ||
b34976b6 | 2965 | return TRUE; |
252b5132 RH |
2966 | } |
2967 | ||
b34976b6 | 2968 | bfd_boolean |
91d6fa6a | 2969 | bfd_section_from_phdr (bfd *abfd, Elf_Internal_Phdr *hdr, int hdr_index) |
20cfcaae | 2970 | { |
9c5bfbb7 | 2971 | const struct elf_backend_data *bed; |
20cfcaae NC |
2972 | |
2973 | switch (hdr->p_type) | |
2974 | { | |
2975 | case PT_NULL: | |
91d6fa6a | 2976 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "null"); |
20cfcaae NC |
2977 | |
2978 | case PT_LOAD: | |
91d6fa6a | 2979 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "load"); |
20cfcaae NC |
2980 | |
2981 | case PT_DYNAMIC: | |
91d6fa6a | 2982 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "dynamic"); |
20cfcaae NC |
2983 | |
2984 | case PT_INTERP: | |
91d6fa6a | 2985 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "interp"); |
20cfcaae NC |
2986 | |
2987 | case PT_NOTE: | |
91d6fa6a | 2988 | if (! _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "note")) |
b34976b6 | 2989 | return FALSE; |
718175fa | 2990 | if (! elf_read_notes (abfd, hdr->p_offset, hdr->p_filesz)) |
b34976b6 AM |
2991 | return FALSE; |
2992 | return TRUE; | |
20cfcaae NC |
2993 | |
2994 | case PT_SHLIB: | |
91d6fa6a | 2995 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "shlib"); |
20cfcaae NC |
2996 | |
2997 | case PT_PHDR: | |
91d6fa6a | 2998 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "phdr"); |
20cfcaae | 2999 | |
811072d8 | 3000 | case PT_GNU_EH_FRAME: |
91d6fa6a | 3001 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, |
811072d8 RM |
3002 | "eh_frame_hdr"); |
3003 | ||
2b05f1b7 | 3004 | case PT_GNU_STACK: |
91d6fa6a | 3005 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "stack"); |
9ee5e499 | 3006 | |
8c37241b | 3007 | case PT_GNU_RELRO: |
91d6fa6a | 3008 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "relro"); |
8c37241b | 3009 | |
20cfcaae | 3010 | default: |
8c1acd09 | 3011 | /* Check for any processor-specific program segment types. */ |
20cfcaae | 3012 | bed = get_elf_backend_data (abfd); |
91d6fa6a | 3013 | return bed->elf_backend_section_from_phdr (abfd, hdr, hdr_index, "proc"); |
20cfcaae NC |
3014 | } |
3015 | } | |
3016 | ||
d4730f92 BS |
3017 | /* Return the REL_HDR for SEC, assuming there is only a single one, either |
3018 | REL or RELA. */ | |
3019 | ||
3020 | Elf_Internal_Shdr * | |
3021 | _bfd_elf_single_rel_hdr (asection *sec) | |
3022 | { | |
3023 | if (elf_section_data (sec)->rel.hdr) | |
3024 | { | |
3025 | BFD_ASSERT (elf_section_data (sec)->rela.hdr == NULL); | |
3026 | return elf_section_data (sec)->rel.hdr; | |
3027 | } | |
3028 | else | |
3029 | return elf_section_data (sec)->rela.hdr; | |
3030 | } | |
3031 | ||
3e19fb8f L |
3032 | static bfd_boolean |
3033 | _bfd_elf_set_reloc_sh_name (bfd *abfd, | |
3034 | Elf_Internal_Shdr *rel_hdr, | |
3035 | const char *sec_name, | |
3036 | bfd_boolean use_rela_p) | |
3037 | { | |
3038 | char *name = (char *) bfd_alloc (abfd, | |
3039 | sizeof ".rela" + strlen (sec_name)); | |
3040 | if (name == NULL) | |
3041 | return FALSE; | |
3042 | ||
3043 | sprintf (name, "%s%s", use_rela_p ? ".rela" : ".rel", sec_name); | |
3044 | rel_hdr->sh_name = | |
3045 | (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), name, | |
3046 | FALSE); | |
3047 | if (rel_hdr->sh_name == (unsigned int) -1) | |
3048 | return FALSE; | |
3049 | ||
3050 | return TRUE; | |
3051 | } | |
3052 | ||
d4730f92 BS |
3053 | /* Allocate and initialize a section-header for a new reloc section, |
3054 | containing relocations against ASECT. It is stored in RELDATA. If | |
3055 | USE_RELA_P is TRUE, we use RELA relocations; otherwise, we use REL | |
3056 | relocations. */ | |
23bc299b | 3057 | |
5d13b3b3 | 3058 | static bfd_boolean |
217aa764 | 3059 | _bfd_elf_init_reloc_shdr (bfd *abfd, |
d4730f92 | 3060 | struct bfd_elf_section_reloc_data *reldata, |
f6fe1ccd | 3061 | const char *sec_name, |
3e19fb8f L |
3062 | bfd_boolean use_rela_p, |
3063 | bfd_boolean delay_st_name_p) | |
23bc299b | 3064 | { |
d4730f92 | 3065 | Elf_Internal_Shdr *rel_hdr; |
9c5bfbb7 | 3066 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
d4730f92 | 3067 | |
d4730f92 | 3068 | BFD_ASSERT (reldata->hdr == NULL); |
ef53be89 | 3069 | rel_hdr = bfd_zalloc (abfd, sizeof (*rel_hdr)); |
d4730f92 | 3070 | reldata->hdr = rel_hdr; |
23bc299b | 3071 | |
3e19fb8f L |
3072 | if (delay_st_name_p) |
3073 | rel_hdr->sh_name = (unsigned int) -1; | |
3074 | else if (!_bfd_elf_set_reloc_sh_name (abfd, rel_hdr, sec_name, | |
3075 | use_rela_p)) | |
b34976b6 | 3076 | return FALSE; |
23bc299b MM |
3077 | rel_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL; |
3078 | rel_hdr->sh_entsize = (use_rela_p | |
3079 | ? bed->s->sizeof_rela | |
3080 | : bed->s->sizeof_rel); | |
72de5009 | 3081 | rel_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align; |
23bc299b MM |
3082 | rel_hdr->sh_flags = 0; |
3083 | rel_hdr->sh_addr = 0; | |
3084 | rel_hdr->sh_size = 0; | |
3085 | rel_hdr->sh_offset = 0; | |
3086 | ||
b34976b6 | 3087 | return TRUE; |
23bc299b MM |
3088 | } |
3089 | ||
94be91de JB |
3090 | /* Return the default section type based on the passed in section flags. */ |
3091 | ||
3092 | int | |
3093 | bfd_elf_get_default_section_type (flagword flags) | |
3094 | { | |
3095 | if ((flags & SEC_ALLOC) != 0 | |
2e76e85a | 3096 | && (flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0) |
94be91de JB |
3097 | return SHT_NOBITS; |
3098 | return SHT_PROGBITS; | |
3099 | } | |
3100 | ||
d4730f92 BS |
3101 | struct fake_section_arg |
3102 | { | |
3103 | struct bfd_link_info *link_info; | |
3104 | bfd_boolean failed; | |
3105 | }; | |
3106 | ||
252b5132 RH |
3107 | /* Set up an ELF internal section header for a section. */ |
3108 | ||
252b5132 | 3109 | static void |
d4730f92 | 3110 | elf_fake_sections (bfd *abfd, asection *asect, void *fsarg) |
252b5132 | 3111 | { |
d4730f92 | 3112 | struct fake_section_arg *arg = (struct fake_section_arg *)fsarg; |
9c5bfbb7 | 3113 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
d4730f92 | 3114 | struct bfd_elf_section_data *esd = elf_section_data (asect); |
252b5132 | 3115 | Elf_Internal_Shdr *this_hdr; |
0414f35b | 3116 | unsigned int sh_type; |
0ce398f1 | 3117 | const char *name = asect->name; |
3e19fb8f | 3118 | bfd_boolean delay_st_name_p = FALSE; |
252b5132 | 3119 | |
d4730f92 | 3120 | if (arg->failed) |
252b5132 RH |
3121 | { |
3122 | /* We already failed; just get out of the bfd_map_over_sections | |
08a40648 | 3123 | loop. */ |
252b5132 RH |
3124 | return; |
3125 | } | |
3126 | ||
d4730f92 | 3127 | this_hdr = &esd->this_hdr; |
252b5132 | 3128 | |
f6fe1ccd | 3129 | if (arg->link_info) |
0ce398f1 | 3130 | { |
f6fe1ccd L |
3131 | /* ld: compress DWARF debug sections with names: .debug_*. */ |
3132 | if ((arg->link_info->compress_debug & COMPRESS_DEBUG) | |
3133 | && (asect->flags & SEC_DEBUGGING) | |
3134 | && name[1] == 'd' | |
3135 | && name[6] == '_') | |
3136 | { | |
3137 | /* Set SEC_ELF_COMPRESS to indicate this section should be | |
3138 | compressed. */ | |
3139 | asect->flags |= SEC_ELF_COMPRESS; | |
0ce398f1 | 3140 | |
dd905818 | 3141 | /* If this section will be compressed, delay adding section |
3e19fb8f L |
3142 | name to section name section after it is compressed in |
3143 | _bfd_elf_assign_file_positions_for_non_load. */ | |
3144 | delay_st_name_p = TRUE; | |
f6fe1ccd L |
3145 | } |
3146 | } | |
3147 | else if ((asect->flags & SEC_ELF_RENAME)) | |
3148 | { | |
3149 | /* objcopy: rename output DWARF debug section. */ | |
3150 | if ((abfd->flags & (BFD_DECOMPRESS | BFD_COMPRESS_GABI))) | |
3151 | { | |
3152 | /* When we decompress or compress with SHF_COMPRESSED, | |
3153 | convert section name from .zdebug_* to .debug_* if | |
3154 | needed. */ | |
3155 | if (name[1] == 'z') | |
3156 | { | |
3157 | char *new_name = convert_zdebug_to_debug (abfd, name); | |
3158 | if (new_name == NULL) | |
3159 | { | |
3160 | arg->failed = TRUE; | |
3161 | return; | |
3162 | } | |
3163 | name = new_name; | |
3164 | } | |
3165 | } | |
3166 | else if (asect->compress_status == COMPRESS_SECTION_DONE) | |
0ce398f1 | 3167 | { |
f6fe1ccd L |
3168 | /* PR binutils/18087: Compression does not always make a |
3169 | section smaller. So only rename the section when | |
3170 | compression has actually taken place. If input section | |
3171 | name is .zdebug_*, we should never compress it again. */ | |
3172 | char *new_name = convert_debug_to_zdebug (abfd, name); | |
0ce398f1 L |
3173 | if (new_name == NULL) |
3174 | { | |
3175 | arg->failed = TRUE; | |
3176 | return; | |
3177 | } | |
f6fe1ccd L |
3178 | BFD_ASSERT (name[1] != 'z'); |
3179 | name = new_name; | |
0ce398f1 L |
3180 | } |
3181 | } | |
3182 | ||
3e19fb8f L |
3183 | if (delay_st_name_p) |
3184 | this_hdr->sh_name = (unsigned int) -1; | |
3185 | else | |
252b5132 | 3186 | { |
3e19fb8f L |
3187 | this_hdr->sh_name |
3188 | = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), | |
3189 | name, FALSE); | |
3190 | if (this_hdr->sh_name == (unsigned int) -1) | |
3191 | { | |
3192 | arg->failed = TRUE; | |
3193 | return; | |
3194 | } | |
252b5132 RH |
3195 | } |
3196 | ||
a4d8e49b | 3197 | /* Don't clear sh_flags. Assembler may set additional bits. */ |
252b5132 RH |
3198 | |
3199 | if ((asect->flags & SEC_ALLOC) != 0 | |
3200 | || asect->user_set_vma) | |
3201 | this_hdr->sh_addr = asect->vma; | |
3202 | else | |
3203 | this_hdr->sh_addr = 0; | |
3204 | ||
3205 | this_hdr->sh_offset = 0; | |
eea6121a | 3206 | this_hdr->sh_size = asect->size; |
252b5132 | 3207 | this_hdr->sh_link = 0; |
c86934ce NC |
3208 | /* PR 17512: file: 0eb809fe, 8b0535ee. */ |
3209 | if (asect->alignment_power >= (sizeof (bfd_vma) * 8) - 1) | |
3210 | { | |
4eca0228 | 3211 | _bfd_error_handler |
695344c0 | 3212 | /* xgettext:c-format */ |
c86934ce | 3213 | (_("%B: error: Alignment power %d of section `%A' is too big"), |
c08bb8dd | 3214 | abfd, asect->alignment_power, asect); |
c86934ce NC |
3215 | arg->failed = TRUE; |
3216 | return; | |
3217 | } | |
72de5009 | 3218 | this_hdr->sh_addralign = (bfd_vma) 1 << asect->alignment_power; |
252b5132 RH |
3219 | /* The sh_entsize and sh_info fields may have been set already by |
3220 | copy_private_section_data. */ | |
3221 | ||
3222 | this_hdr->bfd_section = asect; | |
3223 | this_hdr->contents = NULL; | |
3224 | ||
3cddba1e L |
3225 | /* If the section type is unspecified, we set it based on |
3226 | asect->flags. */ | |
98ece1b3 AM |
3227 | if ((asect->flags & SEC_GROUP) != 0) |
3228 | sh_type = SHT_GROUP; | |
98ece1b3 | 3229 | else |
94be91de | 3230 | sh_type = bfd_elf_get_default_section_type (asect->flags); |
98ece1b3 | 3231 | |
3cddba1e | 3232 | if (this_hdr->sh_type == SHT_NULL) |
98ece1b3 AM |
3233 | this_hdr->sh_type = sh_type; |
3234 | else if (this_hdr->sh_type == SHT_NOBITS | |
3235 | && sh_type == SHT_PROGBITS | |
3236 | && (asect->flags & SEC_ALLOC) != 0) | |
3cddba1e | 3237 | { |
98ece1b3 AM |
3238 | /* Warn if we are changing a NOBITS section to PROGBITS, but |
3239 | allow the link to proceed. This can happen when users link | |
3240 | non-bss input sections to bss output sections, or emit data | |
3241 | to a bss output section via a linker script. */ | |
4eca0228 | 3242 | _bfd_error_handler |
58f0869b | 3243 | (_("warning: section `%A' type changed to PROGBITS"), asect); |
98ece1b3 | 3244 | this_hdr->sh_type = sh_type; |
3cddba1e L |
3245 | } |
3246 | ||
2f89ff8d | 3247 | switch (this_hdr->sh_type) |
252b5132 | 3248 | { |
2f89ff8d | 3249 | default: |
2f89ff8d L |
3250 | break; |
3251 | ||
3252 | case SHT_STRTAB: | |
2f89ff8d L |
3253 | case SHT_NOTE: |
3254 | case SHT_NOBITS: | |
3255 | case SHT_PROGBITS: | |
3256 | break; | |
606851fb AM |
3257 | |
3258 | case SHT_INIT_ARRAY: | |
3259 | case SHT_FINI_ARRAY: | |
3260 | case SHT_PREINIT_ARRAY: | |
3261 | this_hdr->sh_entsize = bed->s->arch_size / 8; | |
3262 | break; | |
2f89ff8d L |
3263 | |
3264 | case SHT_HASH: | |
c7ac6ff8 | 3265 | this_hdr->sh_entsize = bed->s->sizeof_hash_entry; |
2f89ff8d | 3266 | break; |
5de3bf90 | 3267 | |
2f89ff8d | 3268 | case SHT_DYNSYM: |
252b5132 | 3269 | this_hdr->sh_entsize = bed->s->sizeof_sym; |
2f89ff8d L |
3270 | break; |
3271 | ||
3272 | case SHT_DYNAMIC: | |
252b5132 | 3273 | this_hdr->sh_entsize = bed->s->sizeof_dyn; |
2f89ff8d L |
3274 | break; |
3275 | ||
3276 | case SHT_RELA: | |
3277 | if (get_elf_backend_data (abfd)->may_use_rela_p) | |
3278 | this_hdr->sh_entsize = bed->s->sizeof_rela; | |
3279 | break; | |
3280 | ||
3281 | case SHT_REL: | |
3282 | if (get_elf_backend_data (abfd)->may_use_rel_p) | |
3283 | this_hdr->sh_entsize = bed->s->sizeof_rel; | |
3284 | break; | |
3285 | ||
3286 | case SHT_GNU_versym: | |
252b5132 | 3287 | this_hdr->sh_entsize = sizeof (Elf_External_Versym); |
2f89ff8d L |
3288 | break; |
3289 | ||
3290 | case SHT_GNU_verdef: | |
252b5132 RH |
3291 | this_hdr->sh_entsize = 0; |
3292 | /* objcopy or strip will copy over sh_info, but may not set | |
08a40648 AM |
3293 | cverdefs. The linker will set cverdefs, but sh_info will be |
3294 | zero. */ | |
252b5132 RH |
3295 | if (this_hdr->sh_info == 0) |
3296 | this_hdr->sh_info = elf_tdata (abfd)->cverdefs; | |
3297 | else | |
3298 | BFD_ASSERT (elf_tdata (abfd)->cverdefs == 0 | |
3299 | || this_hdr->sh_info == elf_tdata (abfd)->cverdefs); | |
2f89ff8d L |
3300 | break; |
3301 | ||
3302 | case SHT_GNU_verneed: | |
252b5132 RH |
3303 | this_hdr->sh_entsize = 0; |
3304 | /* objcopy or strip will copy over sh_info, but may not set | |
08a40648 AM |
3305 | cverrefs. The linker will set cverrefs, but sh_info will be |
3306 | zero. */ | |
252b5132 RH |
3307 | if (this_hdr->sh_info == 0) |
3308 | this_hdr->sh_info = elf_tdata (abfd)->cverrefs; | |
3309 | else | |
3310 | BFD_ASSERT (elf_tdata (abfd)->cverrefs == 0 | |
3311 | || this_hdr->sh_info == elf_tdata (abfd)->cverrefs); | |
2f89ff8d L |
3312 | break; |
3313 | ||
3314 | case SHT_GROUP: | |
1783205a | 3315 | this_hdr->sh_entsize = GRP_ENTRY_SIZE; |
2f89ff8d | 3316 | break; |
fdc90cb4 JJ |
3317 | |
3318 | case SHT_GNU_HASH: | |
3319 | this_hdr->sh_entsize = bed->s->arch_size == 64 ? 0 : 4; | |
3320 | break; | |
dbb410c3 | 3321 | } |
252b5132 RH |
3322 | |
3323 | if ((asect->flags & SEC_ALLOC) != 0) | |
3324 | this_hdr->sh_flags |= SHF_ALLOC; | |
3325 | if ((asect->flags & SEC_READONLY) == 0) | |
3326 | this_hdr->sh_flags |= SHF_WRITE; | |
3327 | if ((asect->flags & SEC_CODE) != 0) | |
3328 | this_hdr->sh_flags |= SHF_EXECINSTR; | |
f5fa8ca2 JJ |
3329 | if ((asect->flags & SEC_MERGE) != 0) |
3330 | { | |
3331 | this_hdr->sh_flags |= SHF_MERGE; | |
3332 | this_hdr->sh_entsize = asect->entsize; | |
f5fa8ca2 | 3333 | } |
84865015 NC |
3334 | if ((asect->flags & SEC_STRINGS) != 0) |
3335 | this_hdr->sh_flags |= SHF_STRINGS; | |
1126897b | 3336 | if ((asect->flags & SEC_GROUP) == 0 && elf_group_name (asect) != NULL) |
dbb410c3 | 3337 | this_hdr->sh_flags |= SHF_GROUP; |
13ae64f3 | 3338 | if ((asect->flags & SEC_THREAD_LOCAL) != 0) |
704afa60 JJ |
3339 | { |
3340 | this_hdr->sh_flags |= SHF_TLS; | |
3a800eb9 AM |
3341 | if (asect->size == 0 |
3342 | && (asect->flags & SEC_HAS_CONTENTS) == 0) | |
704afa60 | 3343 | { |
3a800eb9 | 3344 | struct bfd_link_order *o = asect->map_tail.link_order; |
b34976b6 | 3345 | |
704afa60 | 3346 | this_hdr->sh_size = 0; |
3a800eb9 AM |
3347 | if (o != NULL) |
3348 | { | |
704afa60 | 3349 | this_hdr->sh_size = o->offset + o->size; |
3a800eb9 AM |
3350 | if (this_hdr->sh_size != 0) |
3351 | this_hdr->sh_type = SHT_NOBITS; | |
3352 | } | |
704afa60 JJ |
3353 | } |
3354 | } | |
18ae9cc1 L |
3355 | if ((asect->flags & (SEC_GROUP | SEC_EXCLUDE)) == SEC_EXCLUDE) |
3356 | this_hdr->sh_flags |= SHF_EXCLUDE; | |
252b5132 | 3357 | |
d4730f92 BS |
3358 | /* If the section has relocs, set up a section header for the |
3359 | SHT_REL[A] section. If two relocation sections are required for | |
3360 | this section, it is up to the processor-specific back-end to | |
3361 | create the other. */ | |
3362 | if ((asect->flags & SEC_RELOC) != 0) | |
3363 | { | |
3364 | /* When doing a relocatable link, create both REL and RELA sections if | |
3365 | needed. */ | |
3366 | if (arg->link_info | |
3367 | /* Do the normal setup if we wouldn't create any sections here. */ | |
3368 | && esd->rel.count + esd->rela.count > 0 | |
0e1862bb L |
3369 | && (bfd_link_relocatable (arg->link_info) |
3370 | || arg->link_info->emitrelocations)) | |
d4730f92 BS |
3371 | { |
3372 | if (esd->rel.count && esd->rel.hdr == NULL | |
3e19fb8f L |
3373 | && !_bfd_elf_init_reloc_shdr (abfd, &esd->rel, name, FALSE, |
3374 | delay_st_name_p)) | |
d4730f92 BS |
3375 | { |
3376 | arg->failed = TRUE; | |
3377 | return; | |
3378 | } | |
3379 | if (esd->rela.count && esd->rela.hdr == NULL | |
3e19fb8f L |
3380 | && !_bfd_elf_init_reloc_shdr (abfd, &esd->rela, name, TRUE, |
3381 | delay_st_name_p)) | |
d4730f92 BS |
3382 | { |
3383 | arg->failed = TRUE; | |
3384 | return; | |
3385 | } | |
3386 | } | |
3387 | else if (!_bfd_elf_init_reloc_shdr (abfd, | |
3388 | (asect->use_rela_p | |
3389 | ? &esd->rela : &esd->rel), | |
f6fe1ccd | 3390 | name, |
3e19fb8f L |
3391 | asect->use_rela_p, |
3392 | delay_st_name_p)) | |
d4730f92 BS |
3393 | arg->failed = TRUE; |
3394 | } | |
3395 | ||
252b5132 | 3396 | /* Check for processor-specific section types. */ |
0414f35b | 3397 | sh_type = this_hdr->sh_type; |
e1fddb6b AO |
3398 | if (bed->elf_backend_fake_sections |
3399 | && !(*bed->elf_backend_fake_sections) (abfd, this_hdr, asect)) | |
d4730f92 | 3400 | arg->failed = TRUE; |
252b5132 | 3401 | |
42bb2e33 | 3402 | if (sh_type == SHT_NOBITS && asect->size != 0) |
0414f35b AM |
3403 | { |
3404 | /* Don't change the header type from NOBITS if we are being | |
42bb2e33 | 3405 | called for objcopy --only-keep-debug. */ |
0414f35b AM |
3406 | this_hdr->sh_type = sh_type; |
3407 | } | |
252b5132 RH |
3408 | } |
3409 | ||
bcacc0f5 AM |
3410 | /* Fill in the contents of a SHT_GROUP section. Called from |
3411 | _bfd_elf_compute_section_file_positions for gas, objcopy, and | |
3412 | when ELF targets use the generic linker, ld. Called for ld -r | |
3413 | from bfd_elf_final_link. */ | |
dbb410c3 | 3414 | |
1126897b | 3415 | void |
217aa764 | 3416 | bfd_elf_set_group_contents (bfd *abfd, asection *sec, void *failedptrarg) |
dbb410c3 | 3417 | { |
a50b1753 | 3418 | bfd_boolean *failedptr = (bfd_boolean *) failedptrarg; |
9dce4196 | 3419 | asection *elt, *first; |
dbb410c3 | 3420 | unsigned char *loc; |
b34976b6 | 3421 | bfd_boolean gas; |
dbb410c3 | 3422 | |
7e4111ad L |
3423 | /* Ignore linker created group section. See elfNN_ia64_object_p in |
3424 | elfxx-ia64.c. */ | |
3425 | if (((sec->flags & (SEC_GROUP | SEC_LINKER_CREATED)) != SEC_GROUP) | |
dbb410c3 AM |
3426 | || *failedptr) |
3427 | return; | |
3428 | ||
bcacc0f5 AM |
3429 | if (elf_section_data (sec)->this_hdr.sh_info == 0) |
3430 | { | |
3431 | unsigned long symindx = 0; | |
3432 | ||
3433 | /* elf_group_id will have been set up by objcopy and the | |
3434 | generic linker. */ | |
3435 | if (elf_group_id (sec) != NULL) | |
3436 | symindx = elf_group_id (sec)->udata.i; | |
1126897b | 3437 | |
bcacc0f5 AM |
3438 | if (symindx == 0) |
3439 | { | |
3440 | /* If called from the assembler, swap_out_syms will have set up | |
3441 | elf_section_syms. */ | |
3442 | BFD_ASSERT (elf_section_syms (abfd) != NULL); | |
3443 | symindx = elf_section_syms (abfd)[sec->index]->udata.i; | |
3444 | } | |
3445 | elf_section_data (sec)->this_hdr.sh_info = symindx; | |
3446 | } | |
3447 | else if (elf_section_data (sec)->this_hdr.sh_info == (unsigned int) -2) | |
1126897b | 3448 | { |
bcacc0f5 AM |
3449 | /* The ELF backend linker sets sh_info to -2 when the group |
3450 | signature symbol is global, and thus the index can't be | |
3451 | set until all local symbols are output. */ | |
53720c49 AM |
3452 | asection *igroup; |
3453 | struct bfd_elf_section_data *sec_data; | |
3454 | unsigned long symndx; | |
3455 | unsigned long extsymoff; | |
bcacc0f5 AM |
3456 | struct elf_link_hash_entry *h; |
3457 | ||
53720c49 AM |
3458 | /* The point of this little dance to the first SHF_GROUP section |
3459 | then back to the SHT_GROUP section is that this gets us to | |
3460 | the SHT_GROUP in the input object. */ | |
3461 | igroup = elf_sec_group (elf_next_in_group (sec)); | |
3462 | sec_data = elf_section_data (igroup); | |
3463 | symndx = sec_data->this_hdr.sh_info; | |
3464 | extsymoff = 0; | |
bcacc0f5 AM |
3465 | if (!elf_bad_symtab (igroup->owner)) |
3466 | { | |
3467 | Elf_Internal_Shdr *symtab_hdr; | |
3468 | ||
3469 | symtab_hdr = &elf_tdata (igroup->owner)->symtab_hdr; | |
3470 | extsymoff = symtab_hdr->sh_info; | |
3471 | } | |
3472 | h = elf_sym_hashes (igroup->owner)[symndx - extsymoff]; | |
3473 | while (h->root.type == bfd_link_hash_indirect | |
3474 | || h->root.type == bfd_link_hash_warning) | |
3475 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
3476 | ||
3477 | elf_section_data (sec)->this_hdr.sh_info = h->indx; | |
1126897b | 3478 | } |
dbb410c3 | 3479 | |
1126897b | 3480 | /* The contents won't be allocated for "ld -r" or objcopy. */ |
b34976b6 | 3481 | gas = TRUE; |
dbb410c3 AM |
3482 | if (sec->contents == NULL) |
3483 | { | |
b34976b6 | 3484 | gas = FALSE; |
a50b1753 | 3485 | sec->contents = (unsigned char *) bfd_alloc (abfd, sec->size); |
9dce4196 AM |
3486 | |
3487 | /* Arrange for the section to be written out. */ | |
3488 | elf_section_data (sec)->this_hdr.contents = sec->contents; | |
dbb410c3 AM |
3489 | if (sec->contents == NULL) |
3490 | { | |
b34976b6 | 3491 | *failedptr = TRUE; |
dbb410c3 AM |
3492 | return; |
3493 | } | |
3494 | } | |
3495 | ||
eea6121a | 3496 | loc = sec->contents + sec->size; |
dbb410c3 | 3497 | |
9dce4196 AM |
3498 | /* Get the pointer to the first section in the group that gas |
3499 | squirreled away here. objcopy arranges for this to be set to the | |
3500 | start of the input section group. */ | |
3501 | first = elt = elf_next_in_group (sec); | |
dbb410c3 AM |
3502 | |
3503 | /* First element is a flag word. Rest of section is elf section | |
3504 | indices for all the sections of the group. Write them backwards | |
3505 | just to keep the group in the same order as given in .section | |
3506 | directives, not that it matters. */ | |
3507 | while (elt != NULL) | |
3508 | { | |
9dce4196 | 3509 | asection *s; |
9dce4196 | 3510 | |
9dce4196 | 3511 | s = elt; |
415f38a6 AM |
3512 | if (!gas) |
3513 | s = s->output_section; | |
3514 | if (s != NULL | |
3515 | && !bfd_is_abs_section (s)) | |
01e1a5bc | 3516 | { |
415f38a6 AM |
3517 | unsigned int idx = elf_section_data (s)->this_idx; |
3518 | ||
01e1a5bc | 3519 | loc -= 4; |
01e1a5bc NC |
3520 | H_PUT_32 (abfd, idx, loc); |
3521 | } | |
945906ff | 3522 | elt = elf_next_in_group (elt); |
9dce4196 AM |
3523 | if (elt == first) |
3524 | break; | |
dbb410c3 AM |
3525 | } |
3526 | ||
7bdf4127 AB |
3527 | loc -= 4; |
3528 | BFD_ASSERT (loc == sec->contents); | |
dbb410c3 | 3529 | |
9dce4196 | 3530 | H_PUT_32 (abfd, sec->flags & SEC_LINK_ONCE ? GRP_COMDAT : 0, loc); |
dbb410c3 AM |
3531 | } |
3532 | ||
bce964aa AM |
3533 | /* Given NAME, the name of a relocation section stripped of its |
3534 | .rel/.rela prefix, return the section in ABFD to which the | |
3535 | relocations apply. */ | |
bd53a53a L |
3536 | |
3537 | asection * | |
bce964aa AM |
3538 | _bfd_elf_plt_get_reloc_section (bfd *abfd, const char *name) |
3539 | { | |
3540 | /* If a target needs .got.plt section, relocations in rela.plt/rel.plt | |
3541 | section likely apply to .got.plt or .got section. */ | |
3542 | if (get_elf_backend_data (abfd)->want_got_plt | |
3543 | && strcmp (name, ".plt") == 0) | |
3544 | { | |
3545 | asection *sec; | |
3546 | ||
3547 | name = ".got.plt"; | |
3548 | sec = bfd_get_section_by_name (abfd, name); | |
3549 | if (sec != NULL) | |
3550 | return sec; | |
3551 | name = ".got"; | |
3552 | } | |
3553 | ||
3554 | return bfd_get_section_by_name (abfd, name); | |
3555 | } | |
3556 | ||
3557 | /* Return the section to which RELOC_SEC applies. */ | |
3558 | ||
3559 | static asection * | |
3560 | elf_get_reloc_section (asection *reloc_sec) | |
bd53a53a L |
3561 | { |
3562 | const char *name; | |
3563 | unsigned int type; | |
3564 | bfd *abfd; | |
bce964aa | 3565 | const struct elf_backend_data *bed; |
bd53a53a L |
3566 | |
3567 | type = elf_section_data (reloc_sec)->this_hdr.sh_type; | |
3568 | if (type != SHT_REL && type != SHT_RELA) | |
3569 | return NULL; | |
3570 | ||
3571 | /* We look up the section the relocs apply to by name. */ | |
3572 | name = reloc_sec->name; | |
bce964aa AM |
3573 | if (strncmp (name, ".rel", 4) != 0) |
3574 | return NULL; | |
3575 | name += 4; | |
3576 | if (type == SHT_RELA && *name++ != 'a') | |
3577 | return NULL; | |
bd53a53a | 3578 | |
bd53a53a | 3579 | abfd = reloc_sec->owner; |
bce964aa AM |
3580 | bed = get_elf_backend_data (abfd); |
3581 | return bed->get_reloc_section (abfd, name); | |
bd53a53a L |
3582 | } |
3583 | ||
252b5132 RH |
3584 | /* Assign all ELF section numbers. The dummy first section is handled here |
3585 | too. The link/info pointers for the standard section types are filled | |
3586 | in here too, while we're at it. */ | |
3587 | ||
b34976b6 | 3588 | static bfd_boolean |
da9f89d4 | 3589 | assign_section_numbers (bfd *abfd, struct bfd_link_info *link_info) |
252b5132 RH |
3590 | { |
3591 | struct elf_obj_tdata *t = elf_tdata (abfd); | |
3592 | asection *sec; | |
3e19fb8f | 3593 | unsigned int section_number; |
252b5132 | 3594 | Elf_Internal_Shdr **i_shdrp; |
47cc2cf5 | 3595 | struct bfd_elf_section_data *d; |
3516e984 | 3596 | bfd_boolean need_symtab; |
252b5132 RH |
3597 | |
3598 | section_number = 1; | |
3599 | ||
2b0f7ef9 JJ |
3600 | _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd)); |
3601 | ||
da9f89d4 | 3602 | /* SHT_GROUP sections are in relocatable files only. */ |
7bdf4127 | 3603 | if (link_info == NULL || !link_info->resolve_section_groups) |
252b5132 | 3604 | { |
ef53be89 | 3605 | size_t reloc_count = 0; |
14f2c699 | 3606 | |
da9f89d4 | 3607 | /* Put SHT_GROUP sections first. */ |
04dd1667 | 3608 | for (sec = abfd->sections; sec != NULL; sec = sec->next) |
47cc2cf5 | 3609 | { |
5daa8fe7 | 3610 | d = elf_section_data (sec); |
da9f89d4 L |
3611 | |
3612 | if (d->this_hdr.sh_type == SHT_GROUP) | |
08a40648 | 3613 | { |
5daa8fe7 | 3614 | if (sec->flags & SEC_LINKER_CREATED) |
da9f89d4 L |
3615 | { |
3616 | /* Remove the linker created SHT_GROUP sections. */ | |
5daa8fe7 | 3617 | bfd_section_list_remove (abfd, sec); |
da9f89d4 | 3618 | abfd->section_count--; |
da9f89d4 | 3619 | } |
08a40648 | 3620 | else |
4fbb74a6 | 3621 | d->this_idx = section_number++; |
da9f89d4 | 3622 | } |
14f2c699 L |
3623 | |
3624 | /* Count relocations. */ | |
3625 | reloc_count += sec->reloc_count; | |
47cc2cf5 | 3626 | } |
14f2c699 L |
3627 | |
3628 | /* Clear HAS_RELOC if there are no relocations. */ | |
3629 | if (reloc_count == 0) | |
3630 | abfd->flags &= ~HAS_RELOC; | |
47cc2cf5 PB |
3631 | } |
3632 | ||
3633 | for (sec = abfd->sections; sec; sec = sec->next) | |
3634 | { | |
3635 | d = elf_section_data (sec); | |
3636 | ||
3637 | if (d->this_hdr.sh_type != SHT_GROUP) | |
4fbb74a6 | 3638 | d->this_idx = section_number++; |
3e19fb8f L |
3639 | if (d->this_hdr.sh_name != (unsigned int) -1) |
3640 | _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->this_hdr.sh_name); | |
d4730f92 | 3641 | if (d->rel.hdr) |
2b0f7ef9 | 3642 | { |
d4730f92 | 3643 | d->rel.idx = section_number++; |
3e19fb8f L |
3644 | if (d->rel.hdr->sh_name != (unsigned int) -1) |
3645 | _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel.hdr->sh_name); | |
2b0f7ef9 | 3646 | } |
d4730f92 BS |
3647 | else |
3648 | d->rel.idx = 0; | |
23bc299b | 3649 | |
d4730f92 | 3650 | if (d->rela.hdr) |
2b0f7ef9 | 3651 | { |
d4730f92 | 3652 | d->rela.idx = section_number++; |
3e19fb8f L |
3653 | if (d->rela.hdr->sh_name != (unsigned int) -1) |
3654 | _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rela.hdr->sh_name); | |
2b0f7ef9 | 3655 | } |
23bc299b | 3656 | else |
d4730f92 | 3657 | d->rela.idx = 0; |
252b5132 RH |
3658 | } |
3659 | ||
3516e984 L |
3660 | need_symtab = (bfd_get_symcount (abfd) > 0 |
3661 | || (link_info == NULL | |
3662 | && ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC)) | |
3663 | == HAS_RELOC))); | |
3664 | if (need_symtab) | |
252b5132 | 3665 | { |
12bd6957 | 3666 | elf_onesymtab (abfd) = section_number++; |
2b0f7ef9 | 3667 | _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->symtab_hdr.sh_name); |
4fbb74a6 | 3668 | if (section_number > ((SHN_LORESERVE - 2) & 0xFFFF)) |
9ad5cbcf | 3669 | { |
6a40cf0c NC |
3670 | elf_section_list * entry; |
3671 | ||
3672 | BFD_ASSERT (elf_symtab_shndx_list (abfd) == NULL); | |
3673 | ||
3674 | entry = bfd_zalloc (abfd, sizeof * entry); | |
3675 | entry->ndx = section_number++; | |
3676 | elf_symtab_shndx_list (abfd) = entry; | |
3677 | entry->hdr.sh_name | |
9ad5cbcf | 3678 | = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), |
b34976b6 | 3679 | ".symtab_shndx", FALSE); |
6a40cf0c | 3680 | if (entry->hdr.sh_name == (unsigned int) -1) |
b34976b6 | 3681 | return FALSE; |
9ad5cbcf | 3682 | } |
12bd6957 | 3683 | elf_strtab_sec (abfd) = section_number++; |
2b0f7ef9 | 3684 | _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->strtab_hdr.sh_name); |
252b5132 RH |
3685 | } |
3686 | ||
dd905818 NC |
3687 | elf_shstrtab_sec (abfd) = section_number++; |
3688 | _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->shstrtab_hdr.sh_name); | |
3689 | elf_elfheader (abfd)->e_shstrndx = elf_shstrtab_sec (abfd); | |
3690 | ||
1c52a645 L |
3691 | if (section_number >= SHN_LORESERVE) |
3692 | { | |
695344c0 | 3693 | /* xgettext:c-format */ |
1c52a645 L |
3694 | _bfd_error_handler (_("%B: too many sections: %u"), |
3695 | abfd, section_number); | |
3696 | return FALSE; | |
3697 | } | |
3698 | ||
9ad5cbcf | 3699 | elf_numsections (abfd) = section_number; |
252b5132 RH |
3700 | elf_elfheader (abfd)->e_shnum = section_number; |
3701 | ||
3702 | /* Set up the list of section header pointers, in agreement with the | |
3703 | indices. */ | |
a50b1753 NC |
3704 | i_shdrp = (Elf_Internal_Shdr **) bfd_zalloc2 (abfd, section_number, |
3705 | sizeof (Elf_Internal_Shdr *)); | |
252b5132 | 3706 | if (i_shdrp == NULL) |
b34976b6 | 3707 | return FALSE; |
252b5132 | 3708 | |
a50b1753 NC |
3709 | i_shdrp[0] = (Elf_Internal_Shdr *) bfd_zalloc (abfd, |
3710 | sizeof (Elf_Internal_Shdr)); | |
252b5132 RH |
3711 | if (i_shdrp[0] == NULL) |
3712 | { | |
3713 | bfd_release (abfd, i_shdrp); | |
b34976b6 | 3714 | return FALSE; |
252b5132 | 3715 | } |
252b5132 RH |
3716 | |
3717 | elf_elfsections (abfd) = i_shdrp; | |
3718 | ||
12bd6957 | 3719 | i_shdrp[elf_shstrtab_sec (abfd)] = &t->shstrtab_hdr; |
3516e984 | 3720 | if (need_symtab) |
252b5132 | 3721 | { |
12bd6957 | 3722 | i_shdrp[elf_onesymtab (abfd)] = &t->symtab_hdr; |
4fbb74a6 | 3723 | if (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF)) |
9ad5cbcf | 3724 | { |
6a40cf0c NC |
3725 | elf_section_list * entry = elf_symtab_shndx_list (abfd); |
3726 | BFD_ASSERT (entry != NULL); | |
3727 | i_shdrp[entry->ndx] = & entry->hdr; | |
3728 | entry->hdr.sh_link = elf_onesymtab (abfd); | |
9ad5cbcf | 3729 | } |
12bd6957 AM |
3730 | i_shdrp[elf_strtab_sec (abfd)] = &t->strtab_hdr; |
3731 | t->symtab_hdr.sh_link = elf_strtab_sec (abfd); | |
252b5132 | 3732 | } |
38ce5b11 | 3733 | |
252b5132 RH |
3734 | for (sec = abfd->sections; sec; sec = sec->next) |
3735 | { | |
252b5132 | 3736 | asection *s; |
252b5132 | 3737 | |
91d6fa6a NC |
3738 | d = elf_section_data (sec); |
3739 | ||
252b5132 | 3740 | i_shdrp[d->this_idx] = &d->this_hdr; |
d4730f92 BS |
3741 | if (d->rel.idx != 0) |
3742 | i_shdrp[d->rel.idx] = d->rel.hdr; | |
3743 | if (d->rela.idx != 0) | |
3744 | i_shdrp[d->rela.idx] = d->rela.hdr; | |
252b5132 RH |
3745 | |
3746 | /* Fill in the sh_link and sh_info fields while we're at it. */ | |
3747 | ||
3748 | /* sh_link of a reloc section is the section index of the symbol | |
3749 | table. sh_info is the section index of the section to which | |
3750 | the relocation entries apply. */ | |
d4730f92 | 3751 | if (d->rel.idx != 0) |
252b5132 | 3752 | { |
12bd6957 | 3753 | d->rel.hdr->sh_link = elf_onesymtab (abfd); |
d4730f92 | 3754 | d->rel.hdr->sh_info = d->this_idx; |
9ef5d938 | 3755 | d->rel.hdr->sh_flags |= SHF_INFO_LINK; |
252b5132 | 3756 | } |
d4730f92 | 3757 | if (d->rela.idx != 0) |
23bc299b | 3758 | { |
12bd6957 | 3759 | d->rela.hdr->sh_link = elf_onesymtab (abfd); |
d4730f92 | 3760 | d->rela.hdr->sh_info = d->this_idx; |
9ef5d938 | 3761 | d->rela.hdr->sh_flags |= SHF_INFO_LINK; |
23bc299b | 3762 | } |
252b5132 | 3763 | |
38ce5b11 L |
3764 | /* We need to set up sh_link for SHF_LINK_ORDER. */ |
3765 | if ((d->this_hdr.sh_flags & SHF_LINK_ORDER) != 0) | |
3766 | { | |
3767 | s = elf_linked_to_section (sec); | |
3768 | if (s) | |
38ce5b11 | 3769 | { |
f2876037 | 3770 | /* elf_linked_to_section points to the input section. */ |
ccd2ec6a | 3771 | if (link_info != NULL) |
38ce5b11 | 3772 | { |
f2876037 | 3773 | /* Check discarded linkonce section. */ |
dbaa2011 | 3774 | if (discarded_section (s)) |
38ce5b11 | 3775 | { |
ccd2ec6a | 3776 | asection *kept; |
4eca0228 | 3777 | _bfd_error_handler |
695344c0 | 3778 | /* xgettext:c-format */ |
63a5468a AM |
3779 | (_("%B: sh_link of section `%A' points to" |
3780 | " discarded section `%A' of `%B'"), | |
ccd2ec6a L |
3781 | abfd, d->this_hdr.bfd_section, |
3782 | s, s->owner); | |
3783 | /* Point to the kept section if it has the same | |
3784 | size as the discarded one. */ | |
c0f00686 | 3785 | kept = _bfd_elf_check_kept_section (s, link_info); |
ccd2ec6a | 3786 | if (kept == NULL) |
185d09ad | 3787 | { |
ccd2ec6a L |
3788 | bfd_set_error (bfd_error_bad_value); |
3789 | return FALSE; | |
185d09ad | 3790 | } |
ccd2ec6a | 3791 | s = kept; |
38ce5b11 | 3792 | } |
e424ecc8 | 3793 | |
ccd2ec6a L |
3794 | s = s->output_section; |
3795 | BFD_ASSERT (s != NULL); | |
38ce5b11 | 3796 | } |
f2876037 L |
3797 | else |
3798 | { | |
3799 | /* Handle objcopy. */ | |
3800 | if (s->output_section == NULL) | |
3801 | { | |
4eca0228 | 3802 | _bfd_error_handler |
695344c0 | 3803 | /* xgettext:c-format */ |
63a5468a AM |
3804 | (_("%B: sh_link of section `%A' points to" |
3805 | " removed section `%A' of `%B'"), | |
f2876037 L |
3806 | abfd, d->this_hdr.bfd_section, s, s->owner); |
3807 | bfd_set_error (bfd_error_bad_value); | |
3808 | return FALSE; | |
3809 | } | |
3810 | s = s->output_section; | |
3811 | } | |
ccd2ec6a L |
3812 | d->this_hdr.sh_link = elf_section_data (s)->this_idx; |
3813 | } | |
3814 | else | |
3815 | { | |
3816 | /* PR 290: | |
3817 | The Intel C compiler generates SHT_IA_64_UNWIND with | |
3818 | SHF_LINK_ORDER. But it doesn't set the sh_link or | |
3819 | sh_info fields. Hence we could get the situation | |
08a40648 | 3820 | where s is NULL. */ |
ccd2ec6a L |
3821 | const struct elf_backend_data *bed |
3822 | = get_elf_backend_data (abfd); | |
3823 | if (bed->link_order_error_handler) | |
3824 | bed->link_order_error_handler | |
695344c0 | 3825 | /* xgettext:c-format */ |
ccd2ec6a L |
3826 | (_("%B: warning: sh_link not set for section `%A'"), |
3827 | abfd, sec); | |
38ce5b11 L |
3828 | } |
3829 | } | |
3830 | ||
252b5132 RH |
3831 | switch (d->this_hdr.sh_type) |
3832 | { | |
3833 | case SHT_REL: | |
3834 | case SHT_RELA: | |
3835 | /* A reloc section which we are treating as a normal BFD | |
3836 | section. sh_link is the section index of the symbol | |
3837 | table. sh_info is the section index of the section to | |
3838 | which the relocation entries apply. We assume that an | |
3839 | allocated reloc section uses the dynamic symbol table. | |
3840 | FIXME: How can we be sure? */ | |
3841 | s = bfd_get_section_by_name (abfd, ".dynsym"); | |
3842 | if (s != NULL) | |
3843 | d->this_hdr.sh_link = elf_section_data (s)->this_idx; | |
3844 | ||
bce964aa | 3845 | s = elf_get_reloc_section (sec); |
252b5132 | 3846 | if (s != NULL) |
9ef5d938 L |
3847 | { |
3848 | d->this_hdr.sh_info = elf_section_data (s)->this_idx; | |
3849 | d->this_hdr.sh_flags |= SHF_INFO_LINK; | |
3850 | } | |
252b5132 RH |
3851 | break; |
3852 | ||
3853 | case SHT_STRTAB: | |
3854 | /* We assume that a section named .stab*str is a stabs | |
3855 | string section. We look for a section with the same name | |
3856 | but without the trailing ``str'', and set its sh_link | |
3857 | field to point to this section. */ | |
0112cd26 | 3858 | if (CONST_STRNEQ (sec->name, ".stab") |
252b5132 RH |
3859 | && strcmp (sec->name + strlen (sec->name) - 3, "str") == 0) |
3860 | { | |
3861 | size_t len; | |
3862 | char *alc; | |
3863 | ||
3864 | len = strlen (sec->name); | |
a50b1753 | 3865 | alc = (char *) bfd_malloc (len - 2); |
252b5132 | 3866 | if (alc == NULL) |
b34976b6 | 3867 | return FALSE; |
d4c88bbb | 3868 | memcpy (alc, sec->name, len - 3); |
252b5132 RH |
3869 | alc[len - 3] = '\0'; |
3870 | s = bfd_get_section_by_name (abfd, alc); | |
3871 | free (alc); | |
3872 | if (s != NULL) | |
3873 | { | |
3874 | elf_section_data (s)->this_hdr.sh_link = d->this_idx; | |
3875 | ||
3876 | /* This is a .stab section. */ | |
0594c12d AM |
3877 | if (elf_section_data (s)->this_hdr.sh_entsize == 0) |
3878 | elf_section_data (s)->this_hdr.sh_entsize | |
3879 | = 4 + 2 * bfd_get_arch_size (abfd) / 8; | |
252b5132 RH |
3880 | } |
3881 | } | |
3882 | break; | |
3883 | ||
3884 | case SHT_DYNAMIC: | |
3885 | case SHT_DYNSYM: | |
3886 | case SHT_GNU_verneed: | |
3887 | case SHT_GNU_verdef: | |
3888 | /* sh_link is the section header index of the string table | |
3889 | used for the dynamic entries, or the symbol table, or the | |
3890 | version strings. */ | |
3891 | s = bfd_get_section_by_name (abfd, ".dynstr"); | |
3892 | if (s != NULL) | |
3893 | d->this_hdr.sh_link = elf_section_data (s)->this_idx; | |
3894 | break; | |
3895 | ||
7f1204bb JJ |
3896 | case SHT_GNU_LIBLIST: |
3897 | /* sh_link is the section header index of the prelink library | |
08a40648 AM |
3898 | list used for the dynamic entries, or the symbol table, or |
3899 | the version strings. */ | |
7f1204bb JJ |
3900 | s = bfd_get_section_by_name (abfd, (sec->flags & SEC_ALLOC) |
3901 | ? ".dynstr" : ".gnu.libstr"); | |
3902 | if (s != NULL) | |
3903 | d->this_hdr.sh_link = elf_section_data (s)->this_idx; | |
3904 | break; | |
3905 | ||
252b5132 | 3906 | case SHT_HASH: |
fdc90cb4 | 3907 | case SHT_GNU_HASH: |
252b5132 RH |
3908 | case SHT_GNU_versym: |
3909 | /* sh_link is the section header index of the symbol table | |
3910 | this hash table or version table is for. */ | |
3911 | s = bfd_get_section_by_name (abfd, ".dynsym"); | |
3912 | if (s != NULL) | |
3913 | d->this_hdr.sh_link = elf_section_data (s)->this_idx; | |
3914 | break; | |
dbb410c3 AM |
3915 | |
3916 | case SHT_GROUP: | |
12bd6957 | 3917 | d->this_hdr.sh_link = elf_onesymtab (abfd); |
252b5132 RH |
3918 | } |
3919 | } | |
3920 | ||
3e19fb8f L |
3921 | /* Delay setting sh_name to _bfd_elf_write_object_contents so that |
3922 | _bfd_elf_assign_file_positions_for_non_load can convert DWARF | |
3923 | debug section name from .debug_* to .zdebug_* if needed. */ | |
3924 | ||
b34976b6 | 3925 | return TRUE; |
252b5132 RH |
3926 | } |
3927 | ||
5372391b | 3928 | static bfd_boolean |
217aa764 | 3929 | sym_is_global (bfd *abfd, asymbol *sym) |
252b5132 RH |
3930 | { |
3931 | /* If the backend has a special mapping, use it. */ | |
9c5bfbb7 | 3932 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
217aa764 AM |
3933 | if (bed->elf_backend_sym_is_global) |
3934 | return (*bed->elf_backend_sym_is_global) (abfd, sym); | |
252b5132 | 3935 | |
e47bf690 | 3936 | return ((sym->flags & (BSF_GLOBAL | BSF_WEAK | BSF_GNU_UNIQUE)) != 0 |
252b5132 RH |
3937 | || bfd_is_und_section (bfd_get_section (sym)) |
3938 | || bfd_is_com_section (bfd_get_section (sym))); | |
3939 | } | |
3940 | ||
76359541 TP |
3941 | /* Filter global symbols of ABFD to include in the import library. All |
3942 | SYMCOUNT symbols of ABFD can be examined from their pointers in | |
3943 | SYMS. Pointers of symbols to keep should be stored contiguously at | |
3944 | the beginning of that array. | |
3945 | ||
3946 | Returns the number of symbols to keep. */ | |
3947 | ||
3948 | unsigned int | |
3949 | _bfd_elf_filter_global_symbols (bfd *abfd, struct bfd_link_info *info, | |
3950 | asymbol **syms, long symcount) | |
3951 | { | |
3952 | long src_count, dst_count = 0; | |
3953 | ||
3954 | for (src_count = 0; src_count < symcount; src_count++) | |
3955 | { | |
3956 | asymbol *sym = syms[src_count]; | |
3957 | char *name = (char *) bfd_asymbol_name (sym); | |
3958 | struct bfd_link_hash_entry *h; | |
3959 | ||
3960 | if (!sym_is_global (abfd, sym)) | |
3961 | continue; | |
3962 | ||
3963 | h = bfd_link_hash_lookup (info->hash, name, FALSE, FALSE, FALSE); | |
5df1bc57 AM |
3964 | if (h == NULL) |
3965 | continue; | |
76359541 TP |
3966 | if (h->type != bfd_link_hash_defined && h->type != bfd_link_hash_defweak) |
3967 | continue; | |
76359541 TP |
3968 | if (h->linker_def || h->ldscript_def) |
3969 | continue; | |
3970 | ||
3971 | syms[dst_count++] = sym; | |
3972 | } | |
3973 | ||
3974 | syms[dst_count] = NULL; | |
3975 | ||
3976 | return dst_count; | |
3977 | } | |
3978 | ||
5372391b | 3979 | /* Don't output section symbols for sections that are not going to be |
c6d8cab4 | 3980 | output, that are duplicates or there is no BFD section. */ |
5372391b AM |
3981 | |
3982 | static bfd_boolean | |
3983 | ignore_section_sym (bfd *abfd, asymbol *sym) | |
3984 | { | |
c6d8cab4 L |
3985 | elf_symbol_type *type_ptr; |
3986 | ||
3987 | if ((sym->flags & BSF_SECTION_SYM) == 0) | |
3988 | return FALSE; | |
3989 | ||
3990 | type_ptr = elf_symbol_from (abfd, sym); | |
3991 | return ((type_ptr != NULL | |
3992 | && type_ptr->internal_elf_sym.st_shndx != 0 | |
3993 | && bfd_is_abs_section (sym->section)) | |
3994 | || !(sym->section->owner == abfd | |
0f0a5e58 | 3995 | || (sym->section->output_section->owner == abfd |
2633a79c AM |
3996 | && sym->section->output_offset == 0) |
3997 | || bfd_is_abs_section (sym->section))); | |
5372391b AM |
3998 | } |
3999 | ||
2633a79c AM |
4000 | /* Map symbol from it's internal number to the external number, moving |
4001 | all local symbols to be at the head of the list. */ | |
4002 | ||
b34976b6 | 4003 | static bfd_boolean |
12bd6957 | 4004 | elf_map_symbols (bfd *abfd, unsigned int *pnum_locals) |
252b5132 | 4005 | { |
dc810e39 | 4006 | unsigned int symcount = bfd_get_symcount (abfd); |
252b5132 RH |
4007 | asymbol **syms = bfd_get_outsymbols (abfd); |
4008 | asymbol **sect_syms; | |
dc810e39 AM |
4009 | unsigned int num_locals = 0; |
4010 | unsigned int num_globals = 0; | |
4011 | unsigned int num_locals2 = 0; | |
4012 | unsigned int num_globals2 = 0; | |
7292b3ac | 4013 | unsigned int max_index = 0; |
dc810e39 | 4014 | unsigned int idx; |
252b5132 RH |
4015 | asection *asect; |
4016 | asymbol **new_syms; | |
252b5132 RH |
4017 | |
4018 | #ifdef DEBUG | |
4019 | fprintf (stderr, "elf_map_symbols\n"); | |
4020 | fflush (stderr); | |
4021 | #endif | |
4022 | ||
252b5132 RH |
4023 | for (asect = abfd->sections; asect; asect = asect->next) |
4024 | { | |
4025 | if (max_index < asect->index) | |
4026 | max_index = asect->index; | |
4027 | } | |
4028 | ||
4029 | max_index++; | |
a50b1753 | 4030 | sect_syms = (asymbol **) bfd_zalloc2 (abfd, max_index, sizeof (asymbol *)); |
252b5132 | 4031 | if (sect_syms == NULL) |
b34976b6 | 4032 | return FALSE; |
252b5132 | 4033 | elf_section_syms (abfd) = sect_syms; |
4e89ac30 | 4034 | elf_num_section_syms (abfd) = max_index; |
252b5132 | 4035 | |
079e9a2f AM |
4036 | /* Init sect_syms entries for any section symbols we have already |
4037 | decided to output. */ | |
252b5132 RH |
4038 | for (idx = 0; idx < symcount; idx++) |
4039 | { | |
dc810e39 | 4040 | asymbol *sym = syms[idx]; |
c044fabd | 4041 | |
252b5132 | 4042 | if ((sym->flags & BSF_SECTION_SYM) != 0 |
0f0a5e58 | 4043 | && sym->value == 0 |
2633a79c AM |
4044 | && !ignore_section_sym (abfd, sym) |
4045 | && !bfd_is_abs_section (sym->section)) | |
252b5132 | 4046 | { |
5372391b | 4047 | asection *sec = sym->section; |
252b5132 | 4048 | |
5372391b AM |
4049 | if (sec->owner != abfd) |
4050 | sec = sec->output_section; | |
252b5132 | 4051 | |
5372391b | 4052 | sect_syms[sec->index] = syms[idx]; |
252b5132 RH |
4053 | } |
4054 | } | |
4055 | ||
252b5132 RH |
4056 | /* Classify all of the symbols. */ |
4057 | for (idx = 0; idx < symcount; idx++) | |
4058 | { | |
2633a79c | 4059 | if (sym_is_global (abfd, syms[idx])) |
252b5132 | 4060 | num_globals++; |
2633a79c AM |
4061 | else if (!ignore_section_sym (abfd, syms[idx])) |
4062 | num_locals++; | |
252b5132 | 4063 | } |
079e9a2f | 4064 | |
5372391b | 4065 | /* We will be adding a section symbol for each normal BFD section. Most |
079e9a2f AM |
4066 | sections will already have a section symbol in outsymbols, but |
4067 | eg. SHT_GROUP sections will not, and we need the section symbol mapped | |
4068 | at least in that case. */ | |
252b5132 RH |
4069 | for (asect = abfd->sections; asect; asect = asect->next) |
4070 | { | |
079e9a2f | 4071 | if (sect_syms[asect->index] == NULL) |
252b5132 | 4072 | { |
079e9a2f | 4073 | if (!sym_is_global (abfd, asect->symbol)) |
252b5132 RH |
4074 | num_locals++; |
4075 | else | |
4076 | num_globals++; | |
252b5132 RH |
4077 | } |
4078 | } | |
4079 | ||
4080 | /* Now sort the symbols so the local symbols are first. */ | |
a50b1753 NC |
4081 | new_syms = (asymbol **) bfd_alloc2 (abfd, num_locals + num_globals, |
4082 | sizeof (asymbol *)); | |
dc810e39 | 4083 | |
252b5132 | 4084 | if (new_syms == NULL) |
b34976b6 | 4085 | return FALSE; |
252b5132 RH |
4086 | |
4087 | for (idx = 0; idx < symcount; idx++) | |
4088 | { | |
4089 | asymbol *sym = syms[idx]; | |
dc810e39 | 4090 | unsigned int i; |
252b5132 | 4091 | |
2633a79c AM |
4092 | if (sym_is_global (abfd, sym)) |
4093 | i = num_locals + num_globals2++; | |
4094 | else if (!ignore_section_sym (abfd, sym)) | |
252b5132 RH |
4095 | i = num_locals2++; |
4096 | else | |
2633a79c | 4097 | continue; |
252b5132 RH |
4098 | new_syms[i] = sym; |
4099 | sym->udata.i = i + 1; | |
4100 | } | |
4101 | for (asect = abfd->sections; asect; asect = asect->next) | |
4102 | { | |
079e9a2f | 4103 | if (sect_syms[asect->index] == NULL) |
252b5132 | 4104 | { |
079e9a2f | 4105 | asymbol *sym = asect->symbol; |
dc810e39 | 4106 | unsigned int i; |
252b5132 | 4107 | |
079e9a2f | 4108 | sect_syms[asect->index] = sym; |
252b5132 RH |
4109 | if (!sym_is_global (abfd, sym)) |
4110 | i = num_locals2++; | |
4111 | else | |
4112 | i = num_locals + num_globals2++; | |
4113 | new_syms[i] = sym; | |
4114 | sym->udata.i = i + 1; | |
4115 | } | |
4116 | } | |
4117 | ||
4118 | bfd_set_symtab (abfd, new_syms, num_locals + num_globals); | |
4119 | ||
12bd6957 | 4120 | *pnum_locals = num_locals; |
b34976b6 | 4121 | return TRUE; |
252b5132 RH |
4122 | } |
4123 | ||
4124 | /* Align to the maximum file alignment that could be required for any | |
4125 | ELF data structure. */ | |
4126 | ||
268b6b39 | 4127 | static inline file_ptr |
217aa764 | 4128 | align_file_position (file_ptr off, int align) |
252b5132 RH |
4129 | { |
4130 | return (off + align - 1) & ~(align - 1); | |
4131 | } | |
4132 | ||
4133 | /* Assign a file position to a section, optionally aligning to the | |
4134 | required section alignment. */ | |
4135 | ||
217aa764 AM |
4136 | file_ptr |
4137 | _bfd_elf_assign_file_position_for_section (Elf_Internal_Shdr *i_shdrp, | |
4138 | file_ptr offset, | |
4139 | bfd_boolean align) | |
252b5132 | 4140 | { |
72de5009 AM |
4141 | if (align && i_shdrp->sh_addralign > 1) |
4142 | offset = BFD_ALIGN (offset, i_shdrp->sh_addralign); | |
252b5132 RH |
4143 | i_shdrp->sh_offset = offset; |
4144 | if (i_shdrp->bfd_section != NULL) | |
4145 | i_shdrp->bfd_section->filepos = offset; | |
4146 | if (i_shdrp->sh_type != SHT_NOBITS) | |
4147 | offset += i_shdrp->sh_size; | |
4148 | return offset; | |
4149 | } | |
4150 | ||
4151 | /* Compute the file positions we are going to put the sections at, and | |
4152 | otherwise prepare to begin writing out the ELF file. If LINK_INFO | |
4153 | is not NULL, this is being called by the ELF backend linker. */ | |
4154 | ||
b34976b6 | 4155 | bfd_boolean |
217aa764 AM |
4156 | _bfd_elf_compute_section_file_positions (bfd *abfd, |
4157 | struct bfd_link_info *link_info) | |
252b5132 | 4158 | { |
9c5bfbb7 | 4159 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
d4730f92 | 4160 | struct fake_section_arg fsargs; |
b34976b6 | 4161 | bfd_boolean failed; |
ef10c3ac | 4162 | struct elf_strtab_hash *strtab = NULL; |
252b5132 | 4163 | Elf_Internal_Shdr *shstrtab_hdr; |
3516e984 | 4164 | bfd_boolean need_symtab; |
252b5132 RH |
4165 | |
4166 | if (abfd->output_has_begun) | |
b34976b6 | 4167 | return TRUE; |
252b5132 RH |
4168 | |
4169 | /* Do any elf backend specific processing first. */ | |
4170 | if (bed->elf_backend_begin_write_processing) | |
4171 | (*bed->elf_backend_begin_write_processing) (abfd, link_info); | |
4172 | ||
4173 | if (! prep_headers (abfd)) | |
b34976b6 | 4174 | return FALSE; |
252b5132 | 4175 | |
e6c51ed4 | 4176 | /* Post process the headers if necessary. */ |
78245035 | 4177 | (*bed->elf_backend_post_process_headers) (abfd, link_info); |
e6c51ed4 | 4178 | |
d4730f92 BS |
4179 | fsargs.failed = FALSE; |
4180 | fsargs.link_info = link_info; | |
4181 | bfd_map_over_sections (abfd, elf_fake_sections, &fsargs); | |
4182 | if (fsargs.failed) | |
b34976b6 | 4183 | return FALSE; |
252b5132 | 4184 | |
da9f89d4 | 4185 | if (!assign_section_numbers (abfd, link_info)) |
b34976b6 | 4186 | return FALSE; |
252b5132 RH |
4187 | |
4188 | /* The backend linker builds symbol table information itself. */ | |
3516e984 L |
4189 | need_symtab = (link_info == NULL |
4190 | && (bfd_get_symcount (abfd) > 0 | |
4191 | || ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC)) | |
4192 | == HAS_RELOC))); | |
4193 | if (need_symtab) | |
252b5132 RH |
4194 | { |
4195 | /* Non-zero if doing a relocatable link. */ | |
4196 | int relocatable_p = ! (abfd->flags & (EXEC_P | DYNAMIC)); | |
4197 | ||
4198 | if (! swap_out_syms (abfd, &strtab, relocatable_p)) | |
b34976b6 | 4199 | return FALSE; |
252b5132 RH |
4200 | } |
4201 | ||
d4730f92 | 4202 | failed = FALSE; |
1126897b | 4203 | if (link_info == NULL) |
dbb410c3 | 4204 | { |
1126897b | 4205 | bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed); |
dbb410c3 | 4206 | if (failed) |
b34976b6 | 4207 | return FALSE; |
dbb410c3 AM |
4208 | } |
4209 | ||
252b5132 RH |
4210 | shstrtab_hdr = &elf_tdata (abfd)->shstrtab_hdr; |
4211 | /* sh_name was set in prep_headers. */ | |
4212 | shstrtab_hdr->sh_type = SHT_STRTAB; | |
84865015 | 4213 | shstrtab_hdr->sh_flags = bed->elf_strtab_flags; |
252b5132 | 4214 | shstrtab_hdr->sh_addr = 0; |
946748d5 | 4215 | /* sh_size is set in _bfd_elf_assign_file_positions_for_non_load. */ |
252b5132 RH |
4216 | shstrtab_hdr->sh_entsize = 0; |
4217 | shstrtab_hdr->sh_link = 0; | |
4218 | shstrtab_hdr->sh_info = 0; | |
3e19fb8f | 4219 | /* sh_offset is set in _bfd_elf_assign_file_positions_for_non_load. */ |
252b5132 RH |
4220 | shstrtab_hdr->sh_addralign = 1; |
4221 | ||
c84fca4d | 4222 | if (!assign_file_positions_except_relocs (abfd, link_info)) |
b34976b6 | 4223 | return FALSE; |
252b5132 | 4224 | |
3516e984 | 4225 | if (need_symtab) |
252b5132 RH |
4226 | { |
4227 | file_ptr off; | |
4228 | Elf_Internal_Shdr *hdr; | |
4229 | ||
12bd6957 | 4230 | off = elf_next_file_pos (abfd); |
252b5132 | 4231 | |
6a40cf0c | 4232 | hdr = & elf_symtab_hdr (abfd); |
b34976b6 | 4233 | off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE); |
252b5132 | 4234 | |
6a40cf0c NC |
4235 | if (elf_symtab_shndx_list (abfd) != NULL) |
4236 | { | |
4237 | hdr = & elf_symtab_shndx_list (abfd)->hdr; | |
4238 | if (hdr->sh_size != 0) | |
4239 | off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE); | |
4240 | /* FIXME: What about other symtab_shndx sections in the list ? */ | |
4241 | } | |
9ad5cbcf | 4242 | |
252b5132 | 4243 | hdr = &elf_tdata (abfd)->strtab_hdr; |
b34976b6 | 4244 | off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE); |
252b5132 | 4245 | |
12bd6957 | 4246 | elf_next_file_pos (abfd) = off; |
252b5132 RH |
4247 | |
4248 | /* Now that we know where the .strtab section goes, write it | |
08a40648 | 4249 | out. */ |
252b5132 | 4250 | if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0 |
ef10c3ac | 4251 | || ! _bfd_elf_strtab_emit (abfd, strtab)) |
b34976b6 | 4252 | return FALSE; |
ef10c3ac | 4253 | _bfd_elf_strtab_free (strtab); |
252b5132 RH |
4254 | } |
4255 | ||
b34976b6 | 4256 | abfd->output_has_begun = TRUE; |
252b5132 | 4257 | |
b34976b6 | 4258 | return TRUE; |
252b5132 RH |
4259 | } |
4260 | ||
8ded5a0f AM |
4261 | /* Make an initial estimate of the size of the program header. If we |
4262 | get the number wrong here, we'll redo section placement. */ | |
4263 | ||
4264 | static bfd_size_type | |
4265 | get_program_header_size (bfd *abfd, struct bfd_link_info *info) | |
4266 | { | |
4267 | size_t segs; | |
4268 | asection *s; | |
2b05f1b7 | 4269 | const struct elf_backend_data *bed; |
8ded5a0f AM |
4270 | |
4271 | /* Assume we will need exactly two PT_LOAD segments: one for text | |
4272 | and one for data. */ | |
4273 | segs = 2; | |
4274 | ||
4275 | s = bfd_get_section_by_name (abfd, ".interp"); | |
4276 | if (s != NULL && (s->flags & SEC_LOAD) != 0) | |
4277 | { | |
4278 | /* If we have a loadable interpreter section, we need a | |
4279 | PT_INTERP segment. In this case, assume we also need a | |
4280 | PT_PHDR segment, although that may not be true for all | |
4281 | targets. */ | |
e9a38e0f | 4282 | segs += 2; |
8ded5a0f AM |
4283 | } |
4284 | ||
4285 | if (bfd_get_section_by_name (abfd, ".dynamic") != NULL) | |
4286 | { | |
4287 | /* We need a PT_DYNAMIC segment. */ | |
4288 | ++segs; | |
f210dcff | 4289 | } |
08a40648 | 4290 | |
ceae84aa | 4291 | if (info != NULL && info->relro) |
f210dcff L |
4292 | { |
4293 | /* We need a PT_GNU_RELRO segment. */ | |
4294 | ++segs; | |
8ded5a0f AM |
4295 | } |
4296 | ||
12bd6957 | 4297 | if (elf_eh_frame_hdr (abfd)) |
8ded5a0f AM |
4298 | { |
4299 | /* We need a PT_GNU_EH_FRAME segment. */ | |
4300 | ++segs; | |
4301 | } | |
4302 | ||
12bd6957 | 4303 | if (elf_stack_flags (abfd)) |
8ded5a0f | 4304 | { |
2b05f1b7 L |
4305 | /* We need a PT_GNU_STACK segment. */ |
4306 | ++segs; | |
4307 | } | |
94b11780 | 4308 | |
2b05f1b7 L |
4309 | for (s = abfd->sections; s != NULL; s = s->next) |
4310 | { | |
8ded5a0f | 4311 | if ((s->flags & SEC_LOAD) != 0 |
0112cd26 | 4312 | && CONST_STRNEQ (s->name, ".note")) |
8ded5a0f AM |
4313 | { |
4314 | /* We need a PT_NOTE segment. */ | |
4315 | ++segs; | |
1c5265b5 JJ |
4316 | /* Try to create just one PT_NOTE segment |
4317 | for all adjacent loadable .note* sections. | |
4318 | gABI requires that within a PT_NOTE segment | |
4319 | (and also inside of each SHT_NOTE section) | |
4320 | each note is padded to a multiple of 4 size, | |
4321 | so we check whether the sections are correctly | |
4322 | aligned. */ | |
4323 | if (s->alignment_power == 2) | |
4324 | while (s->next != NULL | |
4325 | && s->next->alignment_power == 2 | |
4326 | && (s->next->flags & SEC_LOAD) != 0 | |
4327 | && CONST_STRNEQ (s->next->name, ".note")) | |
4328 | s = s->next; | |
8ded5a0f AM |
4329 | } |
4330 | } | |
4331 | ||
4332 | for (s = abfd->sections; s != NULL; s = s->next) | |
4333 | { | |
4334 | if (s->flags & SEC_THREAD_LOCAL) | |
4335 | { | |
4336 | /* We need a PT_TLS segment. */ | |
4337 | ++segs; | |
4338 | break; | |
4339 | } | |
4340 | } | |
4341 | ||
2b05f1b7 | 4342 | bed = get_elf_backend_data (abfd); |
a91e1603 L |
4343 | |
4344 | if ((abfd->flags & D_PAGED) != 0) | |
4345 | { | |
4346 | /* Add a PT_GNU_MBIND segment for each mbind section. */ | |
4347 | unsigned int page_align_power = bfd_log2 (bed->commonpagesize); | |
4348 | for (s = abfd->sections; s != NULL; s = s->next) | |
4349 | if (elf_section_flags (s) & SHF_GNU_MBIND) | |
4350 | { | |
4351 | if (elf_section_data (s)->this_hdr.sh_info | |
4352 | > PT_GNU_MBIND_NUM) | |
4353 | { | |
4354 | _bfd_error_handler | |
4355 | /* xgettext:c-format */ | |
4356 | (_("%B: GNU_MBIN section `%A' has invalid sh_info field: %d"), | |
4357 | abfd, s, elf_section_data (s)->this_hdr.sh_info); | |
4358 | continue; | |
4359 | } | |
4360 | /* Align mbind section to page size. */ | |
4361 | if (s->alignment_power < page_align_power) | |
4362 | s->alignment_power = page_align_power; | |
4363 | segs ++; | |
4364 | } | |
4365 | } | |
4366 | ||
4367 | /* Let the backend count up any program headers it might need. */ | |
4368 | if (bed->elf_backend_additional_program_headers) | |
8ded5a0f AM |
4369 | { |
4370 | int a; | |
4371 | ||
4372 | a = (*bed->elf_backend_additional_program_headers) (abfd, info); | |
4373 | if (a == -1) | |
4374 | abort (); | |
4375 | segs += a; | |
4376 | } | |
4377 | ||
4378 | return segs * bed->s->sizeof_phdr; | |
4379 | } | |
4380 | ||
2ea37f1c NC |
4381 | /* Find the segment that contains the output_section of section. */ |
4382 | ||
4383 | Elf_Internal_Phdr * | |
4384 | _bfd_elf_find_segment_containing_section (bfd * abfd, asection * section) | |
4385 | { | |
4386 | struct elf_segment_map *m; | |
4387 | Elf_Internal_Phdr *p; | |
4388 | ||
12bd6957 | 4389 | for (m = elf_seg_map (abfd), p = elf_tdata (abfd)->phdr; |
2ea37f1c NC |
4390 | m != NULL; |
4391 | m = m->next, p++) | |
4392 | { | |
4393 | int i; | |
4394 | ||
4395 | for (i = m->count - 1; i >= 0; i--) | |
4396 | if (m->sections[i] == section) | |
4397 | return p; | |
4398 | } | |
4399 | ||
4400 | return NULL; | |
4401 | } | |
4402 | ||
252b5132 RH |
4403 | /* Create a mapping from a set of sections to a program segment. */ |
4404 | ||
217aa764 AM |
4405 | static struct elf_segment_map * |
4406 | make_mapping (bfd *abfd, | |
4407 | asection **sections, | |
4408 | unsigned int from, | |
4409 | unsigned int to, | |
4410 | bfd_boolean phdr) | |
252b5132 RH |
4411 | { |
4412 | struct elf_segment_map *m; | |
4413 | unsigned int i; | |
4414 | asection **hdrpp; | |
dc810e39 | 4415 | bfd_size_type amt; |
252b5132 | 4416 | |
dc810e39 AM |
4417 | amt = sizeof (struct elf_segment_map); |
4418 | amt += (to - from - 1) * sizeof (asection *); | |
a50b1753 | 4419 | m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); |
252b5132 RH |
4420 | if (m == NULL) |
4421 | return NULL; | |
4422 | m->next = NULL; | |
4423 | m->p_type = PT_LOAD; | |
4424 | for (i = from, hdrpp = sections + from; i < to; i++, hdrpp++) | |
4425 | m->sections[i - from] = *hdrpp; | |
4426 | m->count = to - from; | |
4427 | ||
4428 | if (from == 0 && phdr) | |
4429 | { | |
4430 | /* Include the headers in the first PT_LOAD segment. */ | |
4431 | m->includes_filehdr = 1; | |
4432 | m->includes_phdrs = 1; | |
4433 | } | |
4434 | ||
4435 | return m; | |
4436 | } | |
4437 | ||
229fcec5 MM |
4438 | /* Create the PT_DYNAMIC segment, which includes DYNSEC. Returns NULL |
4439 | on failure. */ | |
4440 | ||
4441 | struct elf_segment_map * | |
4442 | _bfd_elf_make_dynamic_segment (bfd *abfd, asection *dynsec) | |
4443 | { | |
4444 | struct elf_segment_map *m; | |
4445 | ||
a50b1753 NC |
4446 | m = (struct elf_segment_map *) bfd_zalloc (abfd, |
4447 | sizeof (struct elf_segment_map)); | |
229fcec5 MM |
4448 | if (m == NULL) |
4449 | return NULL; | |
4450 | m->next = NULL; | |
4451 | m->p_type = PT_DYNAMIC; | |
4452 | m->count = 1; | |
4453 | m->sections[0] = dynsec; | |
08a40648 | 4454 | |
229fcec5 MM |
4455 | return m; |
4456 | } | |
4457 | ||
8ded5a0f | 4458 | /* Possibly add or remove segments from the segment map. */ |
252b5132 | 4459 | |
b34976b6 | 4460 | static bfd_boolean |
3dea8fca AM |
4461 | elf_modify_segment_map (bfd *abfd, |
4462 | struct bfd_link_info *info, | |
4463 | bfd_boolean remove_empty_load) | |
252b5132 | 4464 | { |
252e386e | 4465 | struct elf_segment_map **m; |
8ded5a0f | 4466 | const struct elf_backend_data *bed; |
252b5132 | 4467 | |
8ded5a0f AM |
4468 | /* The placement algorithm assumes that non allocated sections are |
4469 | not in PT_LOAD segments. We ensure this here by removing such | |
4470 | sections from the segment map. We also remove excluded | |
252e386e AM |
4471 | sections. Finally, any PT_LOAD segment without sections is |
4472 | removed. */ | |
12bd6957 | 4473 | m = &elf_seg_map (abfd); |
252e386e | 4474 | while (*m) |
8ded5a0f AM |
4475 | { |
4476 | unsigned int i, new_count; | |
252b5132 | 4477 | |
252e386e | 4478 | for (new_count = 0, i = 0; i < (*m)->count; i++) |
8ded5a0f | 4479 | { |
252e386e AM |
4480 | if (((*m)->sections[i]->flags & SEC_EXCLUDE) == 0 |
4481 | && (((*m)->sections[i]->flags & SEC_ALLOC) != 0 | |
4482 | || (*m)->p_type != PT_LOAD)) | |
8ded5a0f | 4483 | { |
252e386e AM |
4484 | (*m)->sections[new_count] = (*m)->sections[i]; |
4485 | new_count++; | |
8ded5a0f AM |
4486 | } |
4487 | } | |
252e386e | 4488 | (*m)->count = new_count; |
252b5132 | 4489 | |
1a9ccd70 NC |
4490 | if (remove_empty_load |
4491 | && (*m)->p_type == PT_LOAD | |
4492 | && (*m)->count == 0 | |
4493 | && !(*m)->includes_phdrs) | |
252e386e AM |
4494 | *m = (*m)->next; |
4495 | else | |
4496 | m = &(*m)->next; | |
8ded5a0f | 4497 | } |
252b5132 | 4498 | |
8ded5a0f AM |
4499 | bed = get_elf_backend_data (abfd); |
4500 | if (bed->elf_backend_modify_segment_map != NULL) | |
252b5132 | 4501 | { |
252e386e | 4502 | if (!(*bed->elf_backend_modify_segment_map) (abfd, info)) |
8ded5a0f | 4503 | return FALSE; |
252b5132 | 4504 | } |
252b5132 | 4505 | |
8ded5a0f AM |
4506 | return TRUE; |
4507 | } | |
252b5132 | 4508 | |
8ded5a0f | 4509 | /* Set up a mapping from BFD sections to program segments. */ |
252b5132 | 4510 | |
8ded5a0f AM |
4511 | bfd_boolean |
4512 | _bfd_elf_map_sections_to_segments (bfd *abfd, struct bfd_link_info *info) | |
4513 | { | |
4514 | unsigned int count; | |
4515 | struct elf_segment_map *m; | |
4516 | asection **sections = NULL; | |
4517 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
3dea8fca | 4518 | bfd_boolean no_user_phdrs; |
252b5132 | 4519 | |
12bd6957 | 4520 | no_user_phdrs = elf_seg_map (abfd) == NULL; |
d324f6d6 RM |
4521 | |
4522 | if (info != NULL) | |
4523 | info->user_phdrs = !no_user_phdrs; | |
4524 | ||
3dea8fca | 4525 | if (no_user_phdrs && bfd_count_sections (abfd) != 0) |
252b5132 | 4526 | { |
8ded5a0f AM |
4527 | asection *s; |
4528 | unsigned int i; | |
4529 | struct elf_segment_map *mfirst; | |
4530 | struct elf_segment_map **pm; | |
4531 | asection *last_hdr; | |
4532 | bfd_vma last_size; | |
4533 | unsigned int phdr_index; | |
4534 | bfd_vma maxpagesize; | |
4535 | asection **hdrpp; | |
4536 | bfd_boolean phdr_in_segment = TRUE; | |
4537 | bfd_boolean writable; | |
4538 | int tls_count = 0; | |
4539 | asection *first_tls = NULL; | |
a91e1603 | 4540 | asection *first_mbind = NULL; |
8ded5a0f AM |
4541 | asection *dynsec, *eh_frame_hdr; |
4542 | bfd_size_type amt; | |
8d06853e | 4543 | bfd_vma addr_mask, wrap_to = 0; |
1a9ccd70 | 4544 | bfd_boolean linker_created_pt_phdr_segment = FALSE; |
252b5132 | 4545 | |
8ded5a0f | 4546 | /* Select the allocated sections, and sort them. */ |
252b5132 | 4547 | |
a50b1753 NC |
4548 | sections = (asection **) bfd_malloc2 (bfd_count_sections (abfd), |
4549 | sizeof (asection *)); | |
8ded5a0f | 4550 | if (sections == NULL) |
252b5132 | 4551 | goto error_return; |
252b5132 | 4552 | |
8d06853e AM |
4553 | /* Calculate top address, avoiding undefined behaviour of shift |
4554 | left operator when shift count is equal to size of type | |
4555 | being shifted. */ | |
4556 | addr_mask = ((bfd_vma) 1 << (bfd_arch_bits_per_address (abfd) - 1)) - 1; | |
4557 | addr_mask = (addr_mask << 1) + 1; | |
4558 | ||
8ded5a0f AM |
4559 | i = 0; |
4560 | for (s = abfd->sections; s != NULL; s = s->next) | |
4561 | { | |
4562 | if ((s->flags & SEC_ALLOC) != 0) | |
4563 | { | |
4564 | sections[i] = s; | |
4565 | ++i; | |
8d06853e AM |
4566 | /* A wrapping section potentially clashes with header. */ |
4567 | if (((s->lma + s->size) & addr_mask) < (s->lma & addr_mask)) | |
4568 | wrap_to = (s->lma + s->size) & addr_mask; | |
8ded5a0f AM |
4569 | } |
4570 | } | |
4571 | BFD_ASSERT (i <= bfd_count_sections (abfd)); | |
4572 | count = i; | |
252b5132 | 4573 | |
8ded5a0f | 4574 | qsort (sections, (size_t) count, sizeof (asection *), elf_sort_sections); |
252b5132 | 4575 | |
8ded5a0f | 4576 | /* Build the mapping. */ |
252b5132 | 4577 | |
8ded5a0f AM |
4578 | mfirst = NULL; |
4579 | pm = &mfirst; | |
252b5132 | 4580 | |
8ded5a0f AM |
4581 | /* If we have a .interp section, then create a PT_PHDR segment for |
4582 | the program headers and a PT_INTERP segment for the .interp | |
4583 | section. */ | |
4584 | s = bfd_get_section_by_name (abfd, ".interp"); | |
4585 | if (s != NULL && (s->flags & SEC_LOAD) != 0) | |
4586 | { | |
4587 | amt = sizeof (struct elf_segment_map); | |
a50b1753 | 4588 | m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); |
8ded5a0f AM |
4589 | if (m == NULL) |
4590 | goto error_return; | |
4591 | m->next = NULL; | |
4592 | m->p_type = PT_PHDR; | |
4593 | /* FIXME: UnixWare and Solaris set PF_X, Irix 5 does not. */ | |
4594 | m->p_flags = PF_R | PF_X; | |
4595 | m->p_flags_valid = 1; | |
4596 | m->includes_phdrs = 1; | |
1a9ccd70 | 4597 | linker_created_pt_phdr_segment = TRUE; |
8ded5a0f AM |
4598 | *pm = m; |
4599 | pm = &m->next; | |
252b5132 | 4600 | |
8ded5a0f | 4601 | amt = sizeof (struct elf_segment_map); |
a50b1753 | 4602 | m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); |
8ded5a0f AM |
4603 | if (m == NULL) |
4604 | goto error_return; | |
4605 | m->next = NULL; | |
4606 | m->p_type = PT_INTERP; | |
4607 | m->count = 1; | |
4608 | m->sections[0] = s; | |
4609 | ||
4610 | *pm = m; | |
4611 | pm = &m->next; | |
252b5132 | 4612 | } |
8ded5a0f AM |
4613 | |
4614 | /* Look through the sections. We put sections in the same program | |
4615 | segment when the start of the second section can be placed within | |
4616 | a few bytes of the end of the first section. */ | |
4617 | last_hdr = NULL; | |
4618 | last_size = 0; | |
4619 | phdr_index = 0; | |
4620 | maxpagesize = bed->maxpagesize; | |
063bb025 NC |
4621 | /* PR 17512: file: c8455299. |
4622 | Avoid divide-by-zero errors later on. | |
4623 | FIXME: Should we abort if the maxpagesize is zero ? */ | |
4624 | if (maxpagesize == 0) | |
4625 | maxpagesize = 1; | |
8ded5a0f AM |
4626 | writable = FALSE; |
4627 | dynsec = bfd_get_section_by_name (abfd, ".dynamic"); | |
4628 | if (dynsec != NULL | |
4629 | && (dynsec->flags & SEC_LOAD) == 0) | |
4630 | dynsec = NULL; | |
4631 | ||
4632 | /* Deal with -Ttext or something similar such that the first section | |
4633 | is not adjacent to the program headers. This is an | |
4634 | approximation, since at this point we don't know exactly how many | |
4635 | program headers we will need. */ | |
4636 | if (count > 0) | |
252b5132 | 4637 | { |
12bd6957 | 4638 | bfd_size_type phdr_size = elf_program_header_size (abfd); |
8ded5a0f | 4639 | |
62d7a5f6 | 4640 | if (phdr_size == (bfd_size_type) -1) |
8ded5a0f | 4641 | phdr_size = get_program_header_size (abfd, info); |
d2bcb0d1 | 4642 | phdr_size += bed->s->sizeof_ehdr; |
8ded5a0f | 4643 | if ((abfd->flags & D_PAGED) == 0 |
8d06853e AM |
4644 | || (sections[0]->lma & addr_mask) < phdr_size |
4645 | || ((sections[0]->lma & addr_mask) % maxpagesize | |
4646 | < phdr_size % maxpagesize) | |
4647 | || (sections[0]->lma & addr_mask & -maxpagesize) < wrap_to) | |
1a9ccd70 NC |
4648 | { |
4649 | /* PR 20815: The ELF standard says that a PT_PHDR segment, if | |
4650 | present, must be included as part of the memory image of the | |
4651 | program. Ie it must be part of a PT_LOAD segment as well. | |
4652 | If we have had to create our own PT_PHDR segment, but it is | |
4653 | not going to be covered by the first PT_LOAD segment, then | |
4654 | force the inclusion if we can... */ | |
4655 | if ((abfd->flags & D_PAGED) != 0 | |
4656 | && linker_created_pt_phdr_segment) | |
4657 | phdr_in_segment = TRUE; | |
4658 | else | |
4659 | phdr_in_segment = FALSE; | |
4660 | } | |
252b5132 RH |
4661 | } |
4662 | ||
8ded5a0f | 4663 | for (i = 0, hdrpp = sections; i < count; i++, hdrpp++) |
252b5132 | 4664 | { |
8ded5a0f AM |
4665 | asection *hdr; |
4666 | bfd_boolean new_segment; | |
4667 | ||
4668 | hdr = *hdrpp; | |
4669 | ||
4670 | /* See if this section and the last one will fit in the same | |
4671 | segment. */ | |
4672 | ||
4673 | if (last_hdr == NULL) | |
4674 | { | |
4675 | /* If we don't have a segment yet, then we don't need a new | |
4676 | one (we build the last one after this loop). */ | |
4677 | new_segment = FALSE; | |
4678 | } | |
4679 | else if (last_hdr->lma - last_hdr->vma != hdr->lma - hdr->vma) | |
4680 | { | |
4681 | /* If this section has a different relation between the | |
4682 | virtual address and the load address, then we need a new | |
4683 | segment. */ | |
4684 | new_segment = TRUE; | |
4685 | } | |
b5599592 AM |
4686 | else if (hdr->lma < last_hdr->lma + last_size |
4687 | || last_hdr->lma + last_size < last_hdr->lma) | |
4688 | { | |
4689 | /* If this section has a load address that makes it overlap | |
4690 | the previous section, then we need a new segment. */ | |
4691 | new_segment = TRUE; | |
4692 | } | |
39948a60 NC |
4693 | /* In the next test we have to be careful when last_hdr->lma is close |
4694 | to the end of the address space. If the aligned address wraps | |
4695 | around to the start of the address space, then there are no more | |
4696 | pages left in memory and it is OK to assume that the current | |
4697 | section can be included in the current segment. */ | |
4698 | else if ((BFD_ALIGN (last_hdr->lma + last_size, maxpagesize) + maxpagesize | |
4699 | > last_hdr->lma) | |
4700 | && (BFD_ALIGN (last_hdr->lma + last_size, maxpagesize) + maxpagesize | |
4ff73856 | 4701 | <= hdr->lma)) |
8ded5a0f AM |
4702 | { |
4703 | /* If putting this section in this segment would force us to | |
4704 | skip a page in the segment, then we need a new segment. */ | |
4705 | new_segment = TRUE; | |
4706 | } | |
4707 | else if ((last_hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0 | |
e5654c0f AM |
4708 | && (hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) != 0 |
4709 | && ((abfd->flags & D_PAGED) == 0 | |
4710 | || (((last_hdr->lma + last_size - 1) & -maxpagesize) | |
4711 | != (hdr->lma & -maxpagesize)))) | |
8ded5a0f | 4712 | { |
e5654c0f AM |
4713 | /* We don't want to put a loaded section after a |
4714 | nonloaded (ie. bss style) section in the same segment | |
4715 | as that will force the non-loaded section to be loaded. | |
4716 | Consider .tbss sections as loaded for this purpose. | |
4717 | However, like the writable/non-writable case below, | |
4718 | if they are on the same page then they must be put | |
4719 | in the same segment. */ | |
8ded5a0f AM |
4720 | new_segment = TRUE; |
4721 | } | |
4722 | else if ((abfd->flags & D_PAGED) == 0) | |
4723 | { | |
4724 | /* If the file is not demand paged, which means that we | |
4725 | don't require the sections to be correctly aligned in the | |
4726 | file, then there is no other reason for a new segment. */ | |
4727 | new_segment = FALSE; | |
4728 | } | |
4729 | else if (! writable | |
4730 | && (hdr->flags & SEC_READONLY) == 0 | |
8d06853e AM |
4731 | && (((last_hdr->lma + last_size - 1) & -maxpagesize) |
4732 | != (hdr->lma & -maxpagesize))) | |
8ded5a0f AM |
4733 | { |
4734 | /* We don't want to put a writable section in a read only | |
4735 | segment, unless they are on the same page in memory | |
4736 | anyhow. We already know that the last section does not | |
4737 | bring us past the current section on the page, so the | |
4738 | only case in which the new section is not on the same | |
4739 | page as the previous section is when the previous section | |
4740 | ends precisely on a page boundary. */ | |
4741 | new_segment = TRUE; | |
4742 | } | |
4743 | else | |
4744 | { | |
4745 | /* Otherwise, we can use the same segment. */ | |
4746 | new_segment = FALSE; | |
4747 | } | |
4748 | ||
2889e75b | 4749 | /* Allow interested parties a chance to override our decision. */ |
ceae84aa AM |
4750 | if (last_hdr != NULL |
4751 | && info != NULL | |
4752 | && info->callbacks->override_segment_assignment != NULL) | |
4753 | new_segment | |
4754 | = info->callbacks->override_segment_assignment (info, abfd, hdr, | |
4755 | last_hdr, | |
4756 | new_segment); | |
2889e75b | 4757 | |
8ded5a0f AM |
4758 | if (! new_segment) |
4759 | { | |
4760 | if ((hdr->flags & SEC_READONLY) == 0) | |
4761 | writable = TRUE; | |
4762 | last_hdr = hdr; | |
4763 | /* .tbss sections effectively have zero size. */ | |
4764 | if ((hdr->flags & (SEC_THREAD_LOCAL | SEC_LOAD)) | |
4765 | != SEC_THREAD_LOCAL) | |
4766 | last_size = hdr->size; | |
4767 | else | |
4768 | last_size = 0; | |
4769 | continue; | |
4770 | } | |
4771 | ||
4772 | /* We need a new program segment. We must create a new program | |
4773 | header holding all the sections from phdr_index until hdr. */ | |
4774 | ||
4775 | m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment); | |
4776 | if (m == NULL) | |
4777 | goto error_return; | |
4778 | ||
4779 | *pm = m; | |
4780 | pm = &m->next; | |
4781 | ||
252b5132 | 4782 | if ((hdr->flags & SEC_READONLY) == 0) |
b34976b6 | 4783 | writable = TRUE; |
8ded5a0f AM |
4784 | else |
4785 | writable = FALSE; | |
4786 | ||
baaff79e JJ |
4787 | last_hdr = hdr; |
4788 | /* .tbss sections effectively have zero size. */ | |
e5caec89 | 4789 | if ((hdr->flags & (SEC_THREAD_LOCAL | SEC_LOAD)) != SEC_THREAD_LOCAL) |
eea6121a | 4790 | last_size = hdr->size; |
baaff79e JJ |
4791 | else |
4792 | last_size = 0; | |
8ded5a0f AM |
4793 | phdr_index = i; |
4794 | phdr_in_segment = FALSE; | |
252b5132 RH |
4795 | } |
4796 | ||
86b2281f AM |
4797 | /* Create a final PT_LOAD program segment, but not if it's just |
4798 | for .tbss. */ | |
4799 | if (last_hdr != NULL | |
4800 | && (i - phdr_index != 1 | |
4801 | || ((last_hdr->flags & (SEC_THREAD_LOCAL | SEC_LOAD)) | |
4802 | != SEC_THREAD_LOCAL))) | |
8ded5a0f AM |
4803 | { |
4804 | m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment); | |
4805 | if (m == NULL) | |
4806 | goto error_return; | |
252b5132 | 4807 | |
8ded5a0f AM |
4808 | *pm = m; |
4809 | pm = &m->next; | |
4810 | } | |
252b5132 | 4811 | |
8ded5a0f AM |
4812 | /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */ |
4813 | if (dynsec != NULL) | |
4814 | { | |
4815 | m = _bfd_elf_make_dynamic_segment (abfd, dynsec); | |
4816 | if (m == NULL) | |
4817 | goto error_return; | |
4818 | *pm = m; | |
4819 | pm = &m->next; | |
4820 | } | |
252b5132 | 4821 | |
1c5265b5 JJ |
4822 | /* For each batch of consecutive loadable .note sections, |
4823 | add a PT_NOTE segment. We don't use bfd_get_section_by_name, | |
4824 | because if we link together nonloadable .note sections and | |
4825 | loadable .note sections, we will generate two .note sections | |
4826 | in the output file. FIXME: Using names for section types is | |
4827 | bogus anyhow. */ | |
8ded5a0f AM |
4828 | for (s = abfd->sections; s != NULL; s = s->next) |
4829 | { | |
4830 | if ((s->flags & SEC_LOAD) != 0 | |
0112cd26 | 4831 | && CONST_STRNEQ (s->name, ".note")) |
8ded5a0f | 4832 | { |
1c5265b5 | 4833 | asection *s2; |
91d6fa6a NC |
4834 | |
4835 | count = 1; | |
8ded5a0f | 4836 | amt = sizeof (struct elf_segment_map); |
1c5265b5 JJ |
4837 | if (s->alignment_power == 2) |
4838 | for (s2 = s; s2->next != NULL; s2 = s2->next) | |
55b581a6 JJ |
4839 | { |
4840 | if (s2->next->alignment_power == 2 | |
4841 | && (s2->next->flags & SEC_LOAD) != 0 | |
4842 | && CONST_STRNEQ (s2->next->name, ".note") | |
8d06853e AM |
4843 | && align_power (s2->lma + s2->size, 2) |
4844 | == s2->next->lma) | |
55b581a6 JJ |
4845 | count++; |
4846 | else | |
4847 | break; | |
4848 | } | |
1c5265b5 | 4849 | amt += (count - 1) * sizeof (asection *); |
a50b1753 | 4850 | m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); |
8ded5a0f AM |
4851 | if (m == NULL) |
4852 | goto error_return; | |
4853 | m->next = NULL; | |
4854 | m->p_type = PT_NOTE; | |
1c5265b5 JJ |
4855 | m->count = count; |
4856 | while (count > 1) | |
4857 | { | |
4858 | m->sections[m->count - count--] = s; | |
4859 | BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0); | |
4860 | s = s->next; | |
4861 | } | |
4862 | m->sections[m->count - 1] = s; | |
4863 | BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0); | |
8ded5a0f AM |
4864 | *pm = m; |
4865 | pm = &m->next; | |
4866 | } | |
4867 | if (s->flags & SEC_THREAD_LOCAL) | |
4868 | { | |
4869 | if (! tls_count) | |
4870 | first_tls = s; | |
4871 | tls_count++; | |
4872 | } | |
a91e1603 L |
4873 | if (first_mbind == NULL |
4874 | && (elf_section_flags (s) & SHF_GNU_MBIND) != 0) | |
4875 | first_mbind = s; | |
8ded5a0f | 4876 | } |
252b5132 | 4877 | |
8ded5a0f AM |
4878 | /* If there are any SHF_TLS output sections, add PT_TLS segment. */ |
4879 | if (tls_count > 0) | |
4880 | { | |
8ded5a0f AM |
4881 | amt = sizeof (struct elf_segment_map); |
4882 | amt += (tls_count - 1) * sizeof (asection *); | |
a50b1753 | 4883 | m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); |
8ded5a0f AM |
4884 | if (m == NULL) |
4885 | goto error_return; | |
4886 | m->next = NULL; | |
4887 | m->p_type = PT_TLS; | |
4888 | m->count = tls_count; | |
4889 | /* Mandated PF_R. */ | |
4890 | m->p_flags = PF_R; | |
4891 | m->p_flags_valid = 1; | |
d923cae0 | 4892 | s = first_tls; |
91d6fa6a | 4893 | for (i = 0; i < (unsigned int) tls_count; ++i) |
8ded5a0f | 4894 | { |
d923cae0 L |
4895 | if ((s->flags & SEC_THREAD_LOCAL) == 0) |
4896 | { | |
4897 | _bfd_error_handler | |
4898 | (_("%B: TLS sections are not adjacent:"), abfd); | |
4899 | s = first_tls; | |
4900 | i = 0; | |
4901 | while (i < (unsigned int) tls_count) | |
4902 | { | |
4903 | if ((s->flags & SEC_THREAD_LOCAL) != 0) | |
4904 | { | |
4905 | _bfd_error_handler (_(" TLS: %A"), s); | |
4906 | i++; | |
4907 | } | |
4908 | else | |
4909 | _bfd_error_handler (_(" non-TLS: %A"), s); | |
4910 | s = s->next; | |
4911 | } | |
4912 | bfd_set_error (bfd_error_bad_value); | |
4913 | goto error_return; | |
4914 | } | |
4915 | m->sections[i] = s; | |
4916 | s = s->next; | |
8ded5a0f | 4917 | } |
252b5132 | 4918 | |
8ded5a0f AM |
4919 | *pm = m; |
4920 | pm = &m->next; | |
4921 | } | |
252b5132 | 4922 | |
a91e1603 L |
4923 | if (first_mbind && (abfd->flags & D_PAGED) != 0) |
4924 | for (s = first_mbind; s != NULL; s = s->next) | |
4925 | if ((elf_section_flags (s) & SHF_GNU_MBIND) != 0 | |
4926 | && (elf_section_data (s)->this_hdr.sh_info | |
4927 | <= PT_GNU_MBIND_NUM)) | |
4928 | { | |
4929 | /* Mandated PF_R. */ | |
4930 | unsigned long p_flags = PF_R; | |
4931 | if ((s->flags & SEC_READONLY) == 0) | |
4932 | p_flags |= PF_W; | |
4933 | if ((s->flags & SEC_CODE) != 0) | |
4934 | p_flags |= PF_X; | |
4935 | ||
4936 | amt = sizeof (struct elf_segment_map) + sizeof (asection *); | |
4937 | m = bfd_zalloc (abfd, amt); | |
4938 | if (m == NULL) | |
4939 | goto error_return; | |
4940 | m->next = NULL; | |
4941 | m->p_type = (PT_GNU_MBIND_LO | |
4942 | + elf_section_data (s)->this_hdr.sh_info); | |
4943 | m->count = 1; | |
4944 | m->p_flags_valid = 1; | |
4945 | m->sections[0] = s; | |
4946 | m->p_flags = p_flags; | |
4947 | ||
4948 | *pm = m; | |
4949 | pm = &m->next; | |
4950 | } | |
4951 | ||
8ded5a0f AM |
4952 | /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME |
4953 | segment. */ | |
12bd6957 | 4954 | eh_frame_hdr = elf_eh_frame_hdr (abfd); |
8ded5a0f AM |
4955 | if (eh_frame_hdr != NULL |
4956 | && (eh_frame_hdr->output_section->flags & SEC_LOAD) != 0) | |
252b5132 | 4957 | { |
dc810e39 | 4958 | amt = sizeof (struct elf_segment_map); |
a50b1753 | 4959 | m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); |
252b5132 RH |
4960 | if (m == NULL) |
4961 | goto error_return; | |
4962 | m->next = NULL; | |
8ded5a0f | 4963 | m->p_type = PT_GNU_EH_FRAME; |
252b5132 | 4964 | m->count = 1; |
8ded5a0f | 4965 | m->sections[0] = eh_frame_hdr->output_section; |
252b5132 RH |
4966 | |
4967 | *pm = m; | |
4968 | pm = &m->next; | |
4969 | } | |
13ae64f3 | 4970 | |
12bd6957 | 4971 | if (elf_stack_flags (abfd)) |
13ae64f3 | 4972 | { |
8ded5a0f | 4973 | amt = sizeof (struct elf_segment_map); |
a50b1753 | 4974 | m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); |
8ded5a0f AM |
4975 | if (m == NULL) |
4976 | goto error_return; | |
4977 | m->next = NULL; | |
2b05f1b7 | 4978 | m->p_type = PT_GNU_STACK; |
12bd6957 | 4979 | m->p_flags = elf_stack_flags (abfd); |
04c3a755 | 4980 | m->p_align = bed->stack_align; |
8ded5a0f | 4981 | m->p_flags_valid = 1; |
04c3a755 NS |
4982 | m->p_align_valid = m->p_align != 0; |
4983 | if (info->stacksize > 0) | |
4984 | { | |
4985 | m->p_size = info->stacksize; | |
4986 | m->p_size_valid = 1; | |
4987 | } | |
252b5132 | 4988 | |
8ded5a0f AM |
4989 | *pm = m; |
4990 | pm = &m->next; | |
4991 | } | |
65765700 | 4992 | |
ceae84aa | 4993 | if (info != NULL && info->relro) |
8ded5a0f | 4994 | { |
f210dcff L |
4995 | for (m = mfirst; m != NULL; m = m->next) |
4996 | { | |
3832a4d8 AM |
4997 | if (m->p_type == PT_LOAD |
4998 | && m->count != 0 | |
4999 | && m->sections[0]->vma >= info->relro_start | |
5000 | && m->sections[0]->vma < info->relro_end) | |
f210dcff | 5001 | { |
3832a4d8 AM |
5002 | i = m->count; |
5003 | while (--i != (unsigned) -1) | |
5004 | if ((m->sections[i]->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) | |
5005 | == (SEC_LOAD | SEC_HAS_CONTENTS)) | |
5006 | break; | |
5007 | ||
43a8475c | 5008 | if (i != (unsigned) -1) |
f210dcff L |
5009 | break; |
5010 | } | |
be01b344 | 5011 | } |
f210dcff L |
5012 | |
5013 | /* Make a PT_GNU_RELRO segment only when it isn't empty. */ | |
5014 | if (m != NULL) | |
5015 | { | |
5016 | amt = sizeof (struct elf_segment_map); | |
a50b1753 | 5017 | m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); |
f210dcff L |
5018 | if (m == NULL) |
5019 | goto error_return; | |
5020 | m->next = NULL; | |
5021 | m->p_type = PT_GNU_RELRO; | |
f210dcff L |
5022 | *pm = m; |
5023 | pm = &m->next; | |
5024 | } | |
8ded5a0f | 5025 | } |
9ee5e499 | 5026 | |
8ded5a0f | 5027 | free (sections); |
12bd6957 | 5028 | elf_seg_map (abfd) = mfirst; |
9ee5e499 JJ |
5029 | } |
5030 | ||
3dea8fca | 5031 | if (!elf_modify_segment_map (abfd, info, no_user_phdrs)) |
8ded5a0f | 5032 | return FALSE; |
8c37241b | 5033 | |
12bd6957 | 5034 | for (count = 0, m = elf_seg_map (abfd); m != NULL; m = m->next) |
8ded5a0f | 5035 | ++count; |
12bd6957 | 5036 | elf_program_header_size (abfd) = count * bed->s->sizeof_phdr; |
252b5132 | 5037 | |
b34976b6 | 5038 | return TRUE; |
252b5132 RH |
5039 | |
5040 | error_return: | |
5041 | if (sections != NULL) | |
5042 | free (sections); | |
b34976b6 | 5043 | return FALSE; |
252b5132 RH |
5044 | } |
5045 | ||
5046 | /* Sort sections by address. */ | |
5047 | ||
5048 | static int | |
217aa764 | 5049 | elf_sort_sections (const void *arg1, const void *arg2) |
252b5132 RH |
5050 | { |
5051 | const asection *sec1 = *(const asection **) arg1; | |
5052 | const asection *sec2 = *(const asection **) arg2; | |
eecdbe52 | 5053 | bfd_size_type size1, size2; |
252b5132 RH |
5054 | |
5055 | /* Sort by LMA first, since this is the address used to | |
5056 | place the section into a segment. */ | |
5057 | if (sec1->lma < sec2->lma) | |
5058 | return -1; | |
5059 | else if (sec1->lma > sec2->lma) | |
5060 | return 1; | |
5061 | ||
5062 | /* Then sort by VMA. Normally the LMA and the VMA will be | |
5063 | the same, and this will do nothing. */ | |
5064 | if (sec1->vma < sec2->vma) | |
5065 | return -1; | |
5066 | else if (sec1->vma > sec2->vma) | |
5067 | return 1; | |
5068 | ||
5069 | /* Put !SEC_LOAD sections after SEC_LOAD ones. */ | |
5070 | ||
07c6e936 | 5071 | #define TOEND(x) (((x)->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0) |
252b5132 RH |
5072 | |
5073 | if (TOEND (sec1)) | |
5074 | { | |
5075 | if (TOEND (sec2)) | |
00a7cdc5 NC |
5076 | { |
5077 | /* If the indicies are the same, do not return 0 | |
5078 | here, but continue to try the next comparison. */ | |
5079 | if (sec1->target_index - sec2->target_index != 0) | |
5080 | return sec1->target_index - sec2->target_index; | |
5081 | } | |
252b5132 RH |
5082 | else |
5083 | return 1; | |
5084 | } | |
00a7cdc5 | 5085 | else if (TOEND (sec2)) |
252b5132 RH |
5086 | return -1; |
5087 | ||
5088 | #undef TOEND | |
5089 | ||
00a7cdc5 NC |
5090 | /* Sort by size, to put zero sized sections |
5091 | before others at the same address. */ | |
252b5132 | 5092 | |
eea6121a AM |
5093 | size1 = (sec1->flags & SEC_LOAD) ? sec1->size : 0; |
5094 | size2 = (sec2->flags & SEC_LOAD) ? sec2->size : 0; | |
eecdbe52 JJ |
5095 | |
5096 | if (size1 < size2) | |
252b5132 | 5097 | return -1; |
eecdbe52 | 5098 | if (size1 > size2) |
252b5132 RH |
5099 | return 1; |
5100 | ||
5101 | return sec1->target_index - sec2->target_index; | |
5102 | } | |
5103 | ||
340b6d91 AC |
5104 | /* Ian Lance Taylor writes: |
5105 | ||
5106 | We shouldn't be using % with a negative signed number. That's just | |
5107 | not good. We have to make sure either that the number is not | |
5108 | negative, or that the number has an unsigned type. When the types | |
5109 | are all the same size they wind up as unsigned. When file_ptr is a | |
5110 | larger signed type, the arithmetic winds up as signed long long, | |
5111 | which is wrong. | |
5112 | ||
5113 | What we're trying to say here is something like ``increase OFF by | |
5114 | the least amount that will cause it to be equal to the VMA modulo | |
5115 | the page size.'' */ | |
5116 | /* In other words, something like: | |
5117 | ||
5118 | vma_offset = m->sections[0]->vma % bed->maxpagesize; | |
5119 | off_offset = off % bed->maxpagesize; | |
5120 | if (vma_offset < off_offset) | |
5121 | adjustment = vma_offset + bed->maxpagesize - off_offset; | |
5122 | else | |
5123 | adjustment = vma_offset - off_offset; | |
08a40648 | 5124 | |
de194d85 | 5125 | which can be collapsed into the expression below. */ |
340b6d91 AC |
5126 | |
5127 | static file_ptr | |
5128 | vma_page_aligned_bias (bfd_vma vma, ufile_ptr off, bfd_vma maxpagesize) | |
5129 | { | |
dc9155b2 NC |
5130 | /* PR binutils/16199: Handle an alignment of zero. */ |
5131 | if (maxpagesize == 0) | |
5132 | maxpagesize = 1; | |
340b6d91 AC |
5133 | return ((vma - off) % maxpagesize); |
5134 | } | |
5135 | ||
6d33f217 L |
5136 | static void |
5137 | print_segment_map (const struct elf_segment_map *m) | |
5138 | { | |
5139 | unsigned int j; | |
5140 | const char *pt = get_segment_type (m->p_type); | |
5141 | char buf[32]; | |
5142 | ||
5143 | if (pt == NULL) | |
5144 | { | |
5145 | if (m->p_type >= PT_LOPROC && m->p_type <= PT_HIPROC) | |
5146 | sprintf (buf, "LOPROC+%7.7x", | |
5147 | (unsigned int) (m->p_type - PT_LOPROC)); | |
5148 | else if (m->p_type >= PT_LOOS && m->p_type <= PT_HIOS) | |
5149 | sprintf (buf, "LOOS+%7.7x", | |
5150 | (unsigned int) (m->p_type - PT_LOOS)); | |
5151 | else | |
5152 | snprintf (buf, sizeof (buf), "%8.8x", | |
5153 | (unsigned int) m->p_type); | |
5154 | pt = buf; | |
5155 | } | |
4a97a0e5 | 5156 | fflush (stdout); |
6d33f217 L |
5157 | fprintf (stderr, "%s:", pt); |
5158 | for (j = 0; j < m->count; j++) | |
5159 | fprintf (stderr, " %s", m->sections [j]->name); | |
5160 | putc ('\n',stderr); | |
4a97a0e5 | 5161 | fflush (stderr); |
6d33f217 L |
5162 | } |
5163 | ||
32812159 AM |
5164 | static bfd_boolean |
5165 | write_zeros (bfd *abfd, file_ptr pos, bfd_size_type len) | |
5166 | { | |
5167 | void *buf; | |
5168 | bfd_boolean ret; | |
5169 | ||
5170 | if (bfd_seek (abfd, pos, SEEK_SET) != 0) | |
5171 | return FALSE; | |
5172 | buf = bfd_zmalloc (len); | |
5173 | if (buf == NULL) | |
5174 | return FALSE; | |
5175 | ret = bfd_bwrite (buf, len, abfd) == len; | |
5176 | free (buf); | |
5177 | return ret; | |
5178 | } | |
5179 | ||
252b5132 RH |
5180 | /* Assign file positions to the sections based on the mapping from |
5181 | sections to segments. This function also sets up some fields in | |
f3520d2f | 5182 | the file header. */ |
252b5132 | 5183 | |
b34976b6 | 5184 | static bfd_boolean |
f3520d2f AM |
5185 | assign_file_positions_for_load_sections (bfd *abfd, |
5186 | struct bfd_link_info *link_info) | |
252b5132 RH |
5187 | { |
5188 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
252b5132 | 5189 | struct elf_segment_map *m; |
252b5132 | 5190 | Elf_Internal_Phdr *phdrs; |
252b5132 | 5191 | Elf_Internal_Phdr *p; |
02bf8d82 | 5192 | file_ptr off; |
3f570048 | 5193 | bfd_size_type maxpagesize; |
a8c75b76 | 5194 | unsigned int pt_load_count = 0; |
f3520d2f | 5195 | unsigned int alloc; |
0920dee7 | 5196 | unsigned int i, j; |
2b0bc088 | 5197 | bfd_vma header_pad = 0; |
252b5132 | 5198 | |
e36284ab | 5199 | if (link_info == NULL |
ceae84aa | 5200 | && !_bfd_elf_map_sections_to_segments (abfd, link_info)) |
8ded5a0f | 5201 | return FALSE; |
252b5132 | 5202 | |
8ded5a0f | 5203 | alloc = 0; |
12bd6957 | 5204 | for (m = elf_seg_map (abfd); m != NULL; m = m->next) |
2b0bc088 NC |
5205 | { |
5206 | ++alloc; | |
5207 | if (m->header_size) | |
5208 | header_pad = m->header_size; | |
5209 | } | |
252b5132 | 5210 | |
82f2dbf7 NC |
5211 | if (alloc) |
5212 | { | |
5213 | elf_elfheader (abfd)->e_phoff = bed->s->sizeof_ehdr; | |
5214 | elf_elfheader (abfd)->e_phentsize = bed->s->sizeof_phdr; | |
5215 | } | |
5216 | else | |
5217 | { | |
5218 | /* PR binutils/12467. */ | |
5219 | elf_elfheader (abfd)->e_phoff = 0; | |
5220 | elf_elfheader (abfd)->e_phentsize = 0; | |
5221 | } | |
d324f6d6 | 5222 | |
8ded5a0f | 5223 | elf_elfheader (abfd)->e_phnum = alloc; |
252b5132 | 5224 | |
12bd6957 AM |
5225 | if (elf_program_header_size (abfd) == (bfd_size_type) -1) |
5226 | elf_program_header_size (abfd) = alloc * bed->s->sizeof_phdr; | |
8ded5a0f | 5227 | else |
12bd6957 | 5228 | BFD_ASSERT (elf_program_header_size (abfd) |
59e0647f | 5229 | >= alloc * bed->s->sizeof_phdr); |
252b5132 RH |
5230 | |
5231 | if (alloc == 0) | |
f3520d2f | 5232 | { |
12bd6957 | 5233 | elf_next_file_pos (abfd) = bed->s->sizeof_ehdr; |
8ded5a0f | 5234 | return TRUE; |
f3520d2f | 5235 | } |
252b5132 | 5236 | |
12bd6957 | 5237 | /* We're writing the size in elf_program_header_size (abfd), |
57268894 HPN |
5238 | see assign_file_positions_except_relocs, so make sure we have |
5239 | that amount allocated, with trailing space cleared. | |
12bd6957 AM |
5240 | The variable alloc contains the computed need, while |
5241 | elf_program_header_size (abfd) contains the size used for the | |
57268894 HPN |
5242 | layout. |
5243 | See ld/emultempl/elf-generic.em:gld${EMULATION_NAME}_map_segments | |
5244 | where the layout is forced to according to a larger size in the | |
5245 | last iterations for the testcase ld-elf/header. */ | |
12bd6957 | 5246 | BFD_ASSERT (elf_program_header_size (abfd) % bed->s->sizeof_phdr |
57268894 | 5247 | == 0); |
a50b1753 NC |
5248 | phdrs = (Elf_Internal_Phdr *) |
5249 | bfd_zalloc2 (abfd, | |
12bd6957 | 5250 | (elf_program_header_size (abfd) / bed->s->sizeof_phdr), |
a50b1753 | 5251 | sizeof (Elf_Internal_Phdr)); |
f3520d2f | 5252 | elf_tdata (abfd)->phdr = phdrs; |
252b5132 | 5253 | if (phdrs == NULL) |
b34976b6 | 5254 | return FALSE; |
252b5132 | 5255 | |
3f570048 AM |
5256 | maxpagesize = 1; |
5257 | if ((abfd->flags & D_PAGED) != 0) | |
5258 | maxpagesize = bed->maxpagesize; | |
5259 | ||
252b5132 RH |
5260 | off = bed->s->sizeof_ehdr; |
5261 | off += alloc * bed->s->sizeof_phdr; | |
2b0bc088 NC |
5262 | if (header_pad < (bfd_vma) off) |
5263 | header_pad = 0; | |
5264 | else | |
5265 | header_pad -= off; | |
5266 | off += header_pad; | |
252b5132 | 5267 | |
12bd6957 | 5268 | for (m = elf_seg_map (abfd), p = phdrs, j = 0; |
252b5132 | 5269 | m != NULL; |
0920dee7 | 5270 | m = m->next, p++, j++) |
252b5132 | 5271 | { |
252b5132 | 5272 | asection **secpp; |
bf988460 AM |
5273 | bfd_vma off_adjust; |
5274 | bfd_boolean no_contents; | |
252b5132 RH |
5275 | |
5276 | /* If elf_segment_map is not from map_sections_to_segments, the | |
08a40648 | 5277 | sections may not be correctly ordered. NOTE: sorting should |
52e9b619 MS |
5278 | not be done to the PT_NOTE section of a corefile, which may |
5279 | contain several pseudo-sections artificially created by bfd. | |
5280 | Sorting these pseudo-sections breaks things badly. */ | |
47d9a591 AM |
5281 | if (m->count > 1 |
5282 | && !(elf_elfheader (abfd)->e_type == ET_CORE | |
52e9b619 | 5283 | && m->p_type == PT_NOTE)) |
252b5132 RH |
5284 | qsort (m->sections, (size_t) m->count, sizeof (asection *), |
5285 | elf_sort_sections); | |
5286 | ||
b301b248 AM |
5287 | /* An ELF segment (described by Elf_Internal_Phdr) may contain a |
5288 | number of sections with contents contributing to both p_filesz | |
5289 | and p_memsz, followed by a number of sections with no contents | |
5290 | that just contribute to p_memsz. In this loop, OFF tracks next | |
02bf8d82 | 5291 | available file offset for PT_LOAD and PT_NOTE segments. */ |
252b5132 | 5292 | p->p_type = m->p_type; |
28a7f3e7 | 5293 | p->p_flags = m->p_flags; |
252b5132 | 5294 | |
3f570048 AM |
5295 | if (m->count == 0) |
5296 | p->p_vaddr = 0; | |
5297 | else | |
3271a814 | 5298 | p->p_vaddr = m->sections[0]->vma - m->p_vaddr_offset; |
3f570048 AM |
5299 | |
5300 | if (m->p_paddr_valid) | |
5301 | p->p_paddr = m->p_paddr; | |
5302 | else if (m->count == 0) | |
5303 | p->p_paddr = 0; | |
5304 | else | |
08a40648 | 5305 | p->p_paddr = m->sections[0]->lma - m->p_vaddr_offset; |
3f570048 AM |
5306 | |
5307 | if (p->p_type == PT_LOAD | |
5308 | && (abfd->flags & D_PAGED) != 0) | |
5309 | { | |
5310 | /* p_align in demand paged PT_LOAD segments effectively stores | |
5311 | the maximum page size. When copying an executable with | |
5312 | objcopy, we set m->p_align from the input file. Use this | |
5313 | value for maxpagesize rather than bed->maxpagesize, which | |
5314 | may be different. Note that we use maxpagesize for PT_TLS | |
5315 | segment alignment later in this function, so we are relying | |
5316 | on at least one PT_LOAD segment appearing before a PT_TLS | |
5317 | segment. */ | |
5318 | if (m->p_align_valid) | |
5319 | maxpagesize = m->p_align; | |
5320 | ||
5321 | p->p_align = maxpagesize; | |
a8c75b76 | 5322 | pt_load_count += 1; |
3f570048 | 5323 | } |
3271a814 NS |
5324 | else if (m->p_align_valid) |
5325 | p->p_align = m->p_align; | |
e970b90a DJ |
5326 | else if (m->count == 0) |
5327 | p->p_align = 1 << bed->s->log_file_align; | |
3f570048 AM |
5328 | else |
5329 | p->p_align = 0; | |
5330 | ||
bf988460 AM |
5331 | no_contents = FALSE; |
5332 | off_adjust = 0; | |
252b5132 | 5333 | if (p->p_type == PT_LOAD |
b301b248 | 5334 | && m->count > 0) |
252b5132 | 5335 | { |
b301b248 | 5336 | bfd_size_type align; |
a49e53ed | 5337 | unsigned int align_power = 0; |
b301b248 | 5338 | |
3271a814 NS |
5339 | if (m->p_align_valid) |
5340 | align = p->p_align; | |
5341 | else | |
252b5132 | 5342 | { |
3271a814 NS |
5343 | for (i = 0, secpp = m->sections; i < m->count; i++, secpp++) |
5344 | { | |
5345 | unsigned int secalign; | |
08a40648 | 5346 | |
3271a814 NS |
5347 | secalign = bfd_get_section_alignment (abfd, *secpp); |
5348 | if (secalign > align_power) | |
5349 | align_power = secalign; | |
5350 | } | |
5351 | align = (bfd_size_type) 1 << align_power; | |
5352 | if (align < maxpagesize) | |
5353 | align = maxpagesize; | |
b301b248 | 5354 | } |
252b5132 | 5355 | |
02bf8d82 AM |
5356 | for (i = 0; i < m->count; i++) |
5357 | if ((m->sections[i]->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0) | |
5358 | /* If we aren't making room for this section, then | |
5359 | it must be SHT_NOBITS regardless of what we've | |
5360 | set via struct bfd_elf_special_section. */ | |
5361 | elf_section_type (m->sections[i]) = SHT_NOBITS; | |
5362 | ||
bf988460 | 5363 | /* Find out whether this segment contains any loadable |
aea274d3 AM |
5364 | sections. */ |
5365 | no_contents = TRUE; | |
5366 | for (i = 0; i < m->count; i++) | |
5367 | if (elf_section_type (m->sections[i]) != SHT_NOBITS) | |
5368 | { | |
5369 | no_contents = FALSE; | |
5370 | break; | |
5371 | } | |
bf988460 | 5372 | |
85cfcbfb | 5373 | off_adjust = vma_page_aligned_bias (p->p_vaddr, off, align); |
a8c75b76 AM |
5374 | |
5375 | /* Broken hardware and/or kernel require that files do not | |
5376 | map the same page with different permissions on some hppa | |
5377 | processors. */ | |
5378 | if (pt_load_count > 1 | |
5379 | && bed->no_page_alias | |
5380 | && (off & (maxpagesize - 1)) != 0 | |
5381 | && (off & -maxpagesize) == ((off + off_adjust) & -maxpagesize)) | |
5382 | off_adjust += maxpagesize; | |
bf988460 AM |
5383 | off += off_adjust; |
5384 | if (no_contents) | |
5385 | { | |
5386 | /* We shouldn't need to align the segment on disk since | |
5387 | the segment doesn't need file space, but the gABI | |
5388 | arguably requires the alignment and glibc ld.so | |
5389 | checks it. So to comply with the alignment | |
5390 | requirement but not waste file space, we adjust | |
5391 | p_offset for just this segment. (OFF_ADJUST is | |
5392 | subtracted from OFF later.) This may put p_offset | |
5393 | past the end of file, but that shouldn't matter. */ | |
5394 | } | |
5395 | else | |
5396 | off_adjust = 0; | |
252b5132 | 5397 | } |
b1a6d0b1 NC |
5398 | /* Make sure the .dynamic section is the first section in the |
5399 | PT_DYNAMIC segment. */ | |
5400 | else if (p->p_type == PT_DYNAMIC | |
5401 | && m->count > 1 | |
5402 | && strcmp (m->sections[0]->name, ".dynamic") != 0) | |
5403 | { | |
5404 | _bfd_error_handler | |
63a5468a AM |
5405 | (_("%B: The first section in the PT_DYNAMIC segment" |
5406 | " is not the .dynamic section"), | |
b301b248 | 5407 | abfd); |
b1a6d0b1 NC |
5408 | bfd_set_error (bfd_error_bad_value); |
5409 | return FALSE; | |
5410 | } | |
3f001e84 JK |
5411 | /* Set the note section type to SHT_NOTE. */ |
5412 | else if (p->p_type == PT_NOTE) | |
5413 | for (i = 0; i < m->count; i++) | |
5414 | elf_section_type (m->sections[i]) = SHT_NOTE; | |
252b5132 | 5415 | |
252b5132 RH |
5416 | p->p_offset = 0; |
5417 | p->p_filesz = 0; | |
5418 | p->p_memsz = 0; | |
5419 | ||
5420 | if (m->includes_filehdr) | |
5421 | { | |
bf988460 | 5422 | if (!m->p_flags_valid) |
252b5132 | 5423 | p->p_flags |= PF_R; |
252b5132 RH |
5424 | p->p_filesz = bed->s->sizeof_ehdr; |
5425 | p->p_memsz = bed->s->sizeof_ehdr; | |
5426 | if (m->count > 0) | |
5427 | { | |
9ab82472 L |
5428 | if (p->p_vaddr < (bfd_vma) off |
5429 | || (!m->p_paddr_valid | |
5430 | && p->p_paddr < (bfd_vma) off)) | |
252b5132 | 5431 | { |
4eca0228 | 5432 | _bfd_error_handler |
63a5468a AM |
5433 | (_("%B: Not enough room for program headers," |
5434 | " try linking with -N"), | |
b301b248 | 5435 | abfd); |
252b5132 | 5436 | bfd_set_error (bfd_error_bad_value); |
b34976b6 | 5437 | return FALSE; |
252b5132 RH |
5438 | } |
5439 | ||
5440 | p->p_vaddr -= off; | |
bf988460 | 5441 | if (!m->p_paddr_valid) |
252b5132 RH |
5442 | p->p_paddr -= off; |
5443 | } | |
252b5132 RH |
5444 | } |
5445 | ||
5446 | if (m->includes_phdrs) | |
5447 | { | |
bf988460 | 5448 | if (!m->p_flags_valid) |
252b5132 RH |
5449 | p->p_flags |= PF_R; |
5450 | ||
f3520d2f | 5451 | if (!m->includes_filehdr) |
252b5132 RH |
5452 | { |
5453 | p->p_offset = bed->s->sizeof_ehdr; | |
5454 | ||
5455 | if (m->count > 0) | |
5456 | { | |
252b5132 | 5457 | p->p_vaddr -= off - p->p_offset; |
bf988460 | 5458 | if (!m->p_paddr_valid) |
252b5132 RH |
5459 | p->p_paddr -= off - p->p_offset; |
5460 | } | |
252b5132 RH |
5461 | } |
5462 | ||
5463 | p->p_filesz += alloc * bed->s->sizeof_phdr; | |
5464 | p->p_memsz += alloc * bed->s->sizeof_phdr; | |
2b0bc088 NC |
5465 | if (m->count) |
5466 | { | |
5467 | p->p_filesz += header_pad; | |
5468 | p->p_memsz += header_pad; | |
5469 | } | |
252b5132 RH |
5470 | } |
5471 | ||
5472 | if (p->p_type == PT_LOAD | |
5473 | || (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core)) | |
5474 | { | |
bf988460 | 5475 | if (!m->includes_filehdr && !m->includes_phdrs) |
02bf8d82 | 5476 | p->p_offset = off; |
252b5132 RH |
5477 | else |
5478 | { | |
5479 | file_ptr adjust; | |
5480 | ||
5481 | adjust = off - (p->p_offset + p->p_filesz); | |
bf988460 AM |
5482 | if (!no_contents) |
5483 | p->p_filesz += adjust; | |
252b5132 RH |
5484 | p->p_memsz += adjust; |
5485 | } | |
5486 | } | |
5487 | ||
1ea63fd2 AM |
5488 | /* Set up p_filesz, p_memsz, p_align and p_flags from the section |
5489 | maps. Set filepos for sections in PT_LOAD segments, and in | |
5490 | core files, for sections in PT_NOTE segments. | |
5491 | assign_file_positions_for_non_load_sections will set filepos | |
5492 | for other sections and update p_filesz for other segments. */ | |
252b5132 RH |
5493 | for (i = 0, secpp = m->sections; i < m->count; i++, secpp++) |
5494 | { | |
5495 | asection *sec; | |
252b5132 | 5496 | bfd_size_type align; |
627b32bc | 5497 | Elf_Internal_Shdr *this_hdr; |
252b5132 RH |
5498 | |
5499 | sec = *secpp; | |
02bf8d82 | 5500 | this_hdr = &elf_section_data (sec)->this_hdr; |
3f570048 | 5501 | align = (bfd_size_type) 1 << bfd_get_section_alignment (abfd, sec); |
252b5132 | 5502 | |
88967714 AM |
5503 | if ((p->p_type == PT_LOAD |
5504 | || p->p_type == PT_TLS) | |
5505 | && (this_hdr->sh_type != SHT_NOBITS | |
5506 | || ((this_hdr->sh_flags & SHF_ALLOC) != 0 | |
5507 | && ((this_hdr->sh_flags & SHF_TLS) == 0 | |
5508 | || p->p_type == PT_TLS)))) | |
252b5132 | 5509 | { |
b5599592 AM |
5510 | bfd_vma p_start = p->p_paddr; |
5511 | bfd_vma p_end = p_start + p->p_memsz; | |
5512 | bfd_vma s_start = sec->lma; | |
5513 | bfd_vma adjust = s_start - p_end; | |
252b5132 | 5514 | |
a2d1e028 L |
5515 | if (adjust != 0 |
5516 | && (s_start < p_end | |
5517 | || p_end < p_start)) | |
252b5132 | 5518 | { |
4eca0228 | 5519 | _bfd_error_handler |
695344c0 | 5520 | /* xgettext:c-format */ |
76cfced5 AM |
5521 | (_("%B: section %A lma %#Lx adjusted to %#Lx"), |
5522 | abfd, sec, s_start, p_end); | |
88967714 | 5523 | adjust = 0; |
b5599592 | 5524 | sec->lma = p_end; |
1cfb7d1e | 5525 | } |
3ac9b6c9 | 5526 | p->p_memsz += adjust; |
1cfb7d1e | 5527 | |
88967714 AM |
5528 | if (this_hdr->sh_type != SHT_NOBITS) |
5529 | { | |
32812159 AM |
5530 | if (p->p_filesz + adjust < p->p_memsz) |
5531 | { | |
5532 | /* We have a PROGBITS section following NOBITS ones. | |
5533 | Allocate file space for the NOBITS section(s) and | |
5534 | zero it. */ | |
5535 | adjust = p->p_memsz - p->p_filesz; | |
5536 | if (!write_zeros (abfd, off, adjust)) | |
5537 | return FALSE; | |
5538 | } | |
88967714 AM |
5539 | off += adjust; |
5540 | p->p_filesz += adjust; | |
252b5132 | 5541 | } |
252b5132 RH |
5542 | } |
5543 | ||
5544 | if (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core) | |
5545 | { | |
b301b248 AM |
5546 | /* The section at i == 0 is the one that actually contains |
5547 | everything. */ | |
4a938328 MS |
5548 | if (i == 0) |
5549 | { | |
627b32bc | 5550 | this_hdr->sh_offset = sec->filepos = off; |
6a3cd2b4 AM |
5551 | off += this_hdr->sh_size; |
5552 | p->p_filesz = this_hdr->sh_size; | |
b301b248 AM |
5553 | p->p_memsz = 0; |
5554 | p->p_align = 1; | |
252b5132 | 5555 | } |
4a938328 | 5556 | else |
252b5132 | 5557 | { |
b301b248 | 5558 | /* The rest are fake sections that shouldn't be written. */ |
252b5132 | 5559 | sec->filepos = 0; |
eea6121a | 5560 | sec->size = 0; |
b301b248 AM |
5561 | sec->flags = 0; |
5562 | continue; | |
252b5132 | 5563 | } |
252b5132 RH |
5564 | } |
5565 | else | |
5566 | { | |
1e951488 | 5567 | if (p->p_type == PT_LOAD) |
b301b248 | 5568 | { |
1e951488 AM |
5569 | this_hdr->sh_offset = sec->filepos = off; |
5570 | if (this_hdr->sh_type != SHT_NOBITS) | |
5571 | off += this_hdr->sh_size; | |
5572 | } | |
5573 | else if (this_hdr->sh_type == SHT_NOBITS | |
5574 | && (this_hdr->sh_flags & SHF_TLS) != 0 | |
5575 | && this_hdr->sh_offset == 0) | |
5576 | { | |
5577 | /* This is a .tbss section that didn't get a PT_LOAD. | |
5578 | (See _bfd_elf_map_sections_to_segments "Create a | |
5579 | final PT_LOAD".) Set sh_offset to the value it | |
5580 | would have if we had created a zero p_filesz and | |
5581 | p_memsz PT_LOAD header for the section. This | |
5582 | also makes the PT_TLS header have the same | |
5583 | p_offset value. */ | |
5584 | bfd_vma adjust = vma_page_aligned_bias (this_hdr->sh_addr, | |
5585 | off, align); | |
5586 | this_hdr->sh_offset = sec->filepos = off + adjust; | |
b301b248 | 5587 | } |
252b5132 | 5588 | |
02bf8d82 | 5589 | if (this_hdr->sh_type != SHT_NOBITS) |
b301b248 | 5590 | { |
6a3cd2b4 | 5591 | p->p_filesz += this_hdr->sh_size; |
02bf8d82 AM |
5592 | /* A load section without SHF_ALLOC is something like |
5593 | a note section in a PT_NOTE segment. These take | |
5594 | file space but are not loaded into memory. */ | |
5595 | if ((this_hdr->sh_flags & SHF_ALLOC) != 0) | |
6a3cd2b4 | 5596 | p->p_memsz += this_hdr->sh_size; |
b301b248 | 5597 | } |
6a3cd2b4 | 5598 | else if ((this_hdr->sh_flags & SHF_ALLOC) != 0) |
13ae64f3 | 5599 | { |
6a3cd2b4 AM |
5600 | if (p->p_type == PT_TLS) |
5601 | p->p_memsz += this_hdr->sh_size; | |
5602 | ||
5603 | /* .tbss is special. It doesn't contribute to p_memsz of | |
5604 | normal segments. */ | |
5605 | else if ((this_hdr->sh_flags & SHF_TLS) == 0) | |
5606 | p->p_memsz += this_hdr->sh_size; | |
13ae64f3 JJ |
5607 | } |
5608 | ||
b10a8ae0 L |
5609 | if (align > p->p_align |
5610 | && !m->p_align_valid | |
5611 | && (p->p_type != PT_LOAD | |
5612 | || (abfd->flags & D_PAGED) == 0)) | |
252b5132 RH |
5613 | p->p_align = align; |
5614 | } | |
5615 | ||
bf988460 | 5616 | if (!m->p_flags_valid) |
252b5132 RH |
5617 | { |
5618 | p->p_flags |= PF_R; | |
02bf8d82 | 5619 | if ((this_hdr->sh_flags & SHF_EXECINSTR) != 0) |
252b5132 | 5620 | p->p_flags |= PF_X; |
02bf8d82 | 5621 | if ((this_hdr->sh_flags & SHF_WRITE) != 0) |
252b5132 RH |
5622 | p->p_flags |= PF_W; |
5623 | } | |
5624 | } | |
43a8475c | 5625 | |
bf988460 | 5626 | off -= off_adjust; |
0920dee7 | 5627 | |
7c928300 AM |
5628 | /* Check that all sections are in a PT_LOAD segment. |
5629 | Don't check funky gdb generated core files. */ | |
5630 | if (p->p_type == PT_LOAD && bfd_get_format (abfd) != bfd_core) | |
9a83a553 AM |
5631 | { |
5632 | bfd_boolean check_vma = TRUE; | |
5633 | ||
5634 | for (i = 1; i < m->count; i++) | |
5635 | if (m->sections[i]->vma == m->sections[i - 1]->vma | |
5636 | && ELF_SECTION_SIZE (&(elf_section_data (m->sections[i]) | |
5637 | ->this_hdr), p) != 0 | |
5638 | && ELF_SECTION_SIZE (&(elf_section_data (m->sections[i - 1]) | |
5639 | ->this_hdr), p) != 0) | |
0920dee7 | 5640 | { |
9a83a553 AM |
5641 | /* Looks like we have overlays packed into the segment. */ |
5642 | check_vma = FALSE; | |
5643 | break; | |
0920dee7 | 5644 | } |
9a83a553 AM |
5645 | |
5646 | for (i = 0; i < m->count; i++) | |
5647 | { | |
5648 | Elf_Internal_Shdr *this_hdr; | |
5649 | asection *sec; | |
5650 | ||
5651 | sec = m->sections[i]; | |
5652 | this_hdr = &(elf_section_data(sec)->this_hdr); | |
86b2281f AM |
5653 | if (!ELF_SECTION_IN_SEGMENT_1 (this_hdr, p, check_vma, 0) |
5654 | && !ELF_TBSS_SPECIAL (this_hdr, p)) | |
9a83a553 | 5655 | { |
4eca0228 | 5656 | _bfd_error_handler |
695344c0 | 5657 | /* xgettext:c-format */ |
9a83a553 AM |
5658 | (_("%B: section `%A' can't be allocated in segment %d"), |
5659 | abfd, sec, j); | |
5660 | print_segment_map (m); | |
5661 | } | |
5662 | } | |
5663 | } | |
252b5132 RH |
5664 | } |
5665 | ||
12bd6957 | 5666 | elf_next_file_pos (abfd) = off; |
f3520d2f AM |
5667 | return TRUE; |
5668 | } | |
5669 | ||
5670 | /* Assign file positions for the other sections. */ | |
5671 | ||
5672 | static bfd_boolean | |
5673 | assign_file_positions_for_non_load_sections (bfd *abfd, | |
5674 | struct bfd_link_info *link_info) | |
5675 | { | |
5676 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
5677 | Elf_Internal_Shdr **i_shdrpp; | |
e06efbf1 | 5678 | Elf_Internal_Shdr **hdrpp, **end_hdrpp; |
f3520d2f AM |
5679 | Elf_Internal_Phdr *phdrs; |
5680 | Elf_Internal_Phdr *p; | |
5681 | struct elf_segment_map *m; | |
62655c7b | 5682 | struct elf_segment_map *hdrs_segment; |
f3520d2f AM |
5683 | bfd_vma filehdr_vaddr, filehdr_paddr; |
5684 | bfd_vma phdrs_vaddr, phdrs_paddr; | |
5685 | file_ptr off; | |
f3520d2f AM |
5686 | unsigned int count; |
5687 | ||
5c182d5f | 5688 | i_shdrpp = elf_elfsections (abfd); |
e06efbf1 | 5689 | end_hdrpp = i_shdrpp + elf_numsections (abfd); |
12bd6957 | 5690 | off = elf_next_file_pos (abfd); |
e06efbf1 | 5691 | for (hdrpp = i_shdrpp + 1; hdrpp < end_hdrpp; hdrpp++) |
5c182d5f | 5692 | { |
5c182d5f AM |
5693 | Elf_Internal_Shdr *hdr; |
5694 | ||
5695 | hdr = *hdrpp; | |
5696 | if (hdr->bfd_section != NULL | |
252e386e AM |
5697 | && (hdr->bfd_section->filepos != 0 |
5698 | || (hdr->sh_type == SHT_NOBITS | |
5699 | && hdr->contents == NULL))) | |
627b32bc | 5700 | BFD_ASSERT (hdr->sh_offset == hdr->bfd_section->filepos); |
5c182d5f AM |
5701 | else if ((hdr->sh_flags & SHF_ALLOC) != 0) |
5702 | { | |
e8d2ba53 | 5703 | if (hdr->sh_size != 0) |
4eca0228 | 5704 | _bfd_error_handler |
695344c0 | 5705 | /* xgettext:c-format */ |
e8d2ba53 AM |
5706 | (_("%B: warning: allocated section `%s' not in segment"), |
5707 | abfd, | |
5708 | (hdr->bfd_section == NULL | |
5709 | ? "*unknown*" | |
5710 | : hdr->bfd_section->name)); | |
3ba71138 L |
5711 | /* We don't need to page align empty sections. */ |
5712 | if ((abfd->flags & D_PAGED) != 0 && hdr->sh_size != 0) | |
5c182d5f AM |
5713 | off += vma_page_aligned_bias (hdr->sh_addr, off, |
5714 | bed->maxpagesize); | |
5715 | else | |
5716 | off += vma_page_aligned_bias (hdr->sh_addr, off, | |
5717 | hdr->sh_addralign); | |
5718 | off = _bfd_elf_assign_file_position_for_section (hdr, off, | |
5719 | FALSE); | |
5720 | } | |
5721 | else if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA) | |
5722 | && hdr->bfd_section == NULL) | |
0ce398f1 L |
5723 | || (hdr->bfd_section != NULL |
5724 | && (hdr->bfd_section->flags & SEC_ELF_COMPRESS)) | |
5725 | /* Compress DWARF debug sections. */ | |
12bd6957 | 5726 | || hdr == i_shdrpp[elf_onesymtab (abfd)] |
6a40cf0c NC |
5727 | || (elf_symtab_shndx_list (abfd) != NULL |
5728 | && hdr == i_shdrpp[elf_symtab_shndx_list (abfd)->ndx]) | |
3e19fb8f L |
5729 | || hdr == i_shdrpp[elf_strtab_sec (abfd)] |
5730 | || hdr == i_shdrpp[elf_shstrtab_sec (abfd)]) | |
5c182d5f AM |
5731 | hdr->sh_offset = -1; |
5732 | else | |
5733 | off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE); | |
5c182d5f AM |
5734 | } |
5735 | ||
252b5132 RH |
5736 | /* Now that we have set the section file positions, we can set up |
5737 | the file positions for the non PT_LOAD segments. */ | |
f3520d2f AM |
5738 | count = 0; |
5739 | filehdr_vaddr = 0; | |
5740 | filehdr_paddr = 0; | |
5741 | phdrs_vaddr = bed->maxpagesize + bed->s->sizeof_ehdr; | |
5742 | phdrs_paddr = 0; | |
62655c7b | 5743 | hdrs_segment = NULL; |
f3520d2f | 5744 | phdrs = elf_tdata (abfd)->phdr; |
12bd6957 | 5745 | for (m = elf_seg_map (abfd), p = phdrs; m != NULL; m = m->next, p++) |
f3520d2f AM |
5746 | { |
5747 | ++count; | |
5748 | if (p->p_type != PT_LOAD) | |
5749 | continue; | |
5750 | ||
5751 | if (m->includes_filehdr) | |
5752 | { | |
5753 | filehdr_vaddr = p->p_vaddr; | |
5754 | filehdr_paddr = p->p_paddr; | |
5755 | } | |
5756 | if (m->includes_phdrs) | |
5757 | { | |
5758 | phdrs_vaddr = p->p_vaddr; | |
5759 | phdrs_paddr = p->p_paddr; | |
5760 | if (m->includes_filehdr) | |
5761 | { | |
62655c7b | 5762 | hdrs_segment = m; |
f3520d2f AM |
5763 | phdrs_vaddr += bed->s->sizeof_ehdr; |
5764 | phdrs_paddr += bed->s->sizeof_ehdr; | |
5765 | } | |
5766 | } | |
5767 | } | |
5768 | ||
62655c7b RM |
5769 | if (hdrs_segment != NULL && link_info != NULL) |
5770 | { | |
5771 | /* There is a segment that contains both the file headers and the | |
5772 | program headers, so provide a symbol __ehdr_start pointing there. | |
5773 | A program can use this to examine itself robustly. */ | |
5774 | ||
5775 | struct elf_link_hash_entry *hash | |
5776 | = elf_link_hash_lookup (elf_hash_table (link_info), "__ehdr_start", | |
5777 | FALSE, FALSE, TRUE); | |
5778 | /* If the symbol was referenced and not defined, define it. */ | |
5779 | if (hash != NULL | |
5780 | && (hash->root.type == bfd_link_hash_new | |
5781 | || hash->root.type == bfd_link_hash_undefined | |
5782 | || hash->root.type == bfd_link_hash_undefweak | |
5783 | || hash->root.type == bfd_link_hash_common)) | |
5784 | { | |
5785 | asection *s = NULL; | |
5786 | if (hdrs_segment->count != 0) | |
5787 | /* The segment contains sections, so use the first one. */ | |
5788 | s = hdrs_segment->sections[0]; | |
5789 | else | |
5790 | /* Use the first (i.e. lowest-addressed) section in any segment. */ | |
12bd6957 | 5791 | for (m = elf_seg_map (abfd); m != NULL; m = m->next) |
62655c7b RM |
5792 | if (m->count != 0) |
5793 | { | |
5794 | s = m->sections[0]; | |
5795 | break; | |
5796 | } | |
5797 | ||
5798 | if (s != NULL) | |
5799 | { | |
5800 | hash->root.u.def.value = filehdr_vaddr - s->vma; | |
5801 | hash->root.u.def.section = s; | |
5802 | } | |
5803 | else | |
5804 | { | |
5805 | hash->root.u.def.value = filehdr_vaddr; | |
5806 | hash->root.u.def.section = bfd_abs_section_ptr; | |
5807 | } | |
5808 | ||
5809 | hash->root.type = bfd_link_hash_defined; | |
5810 | hash->def_regular = 1; | |
5811 | hash->non_elf = 0; | |
5812 | } | |
5813 | } | |
5814 | ||
12bd6957 | 5815 | for (m = elf_seg_map (abfd), p = phdrs; m != NULL; m = m->next, p++) |
252b5132 | 5816 | { |
129af99f | 5817 | if (p->p_type == PT_GNU_RELRO) |
252b5132 | 5818 | { |
b84a33b5 | 5819 | const Elf_Internal_Phdr *lp; |
3146fac4 | 5820 | struct elf_segment_map *lm; |
1ea63fd2 | 5821 | |
129af99f | 5822 | if (link_info != NULL) |
8c37241b | 5823 | { |
129af99f AS |
5824 | /* During linking the range of the RELRO segment is passed |
5825 | in link_info. */ | |
12bd6957 | 5826 | for (lm = elf_seg_map (abfd), lp = phdrs; |
3146fac4 AM |
5827 | lm != NULL; |
5828 | lm = lm->next, lp++) | |
8c37241b JJ |
5829 | { |
5830 | if (lp->p_type == PT_LOAD | |
b84a33b5 | 5831 | && lp->p_vaddr < link_info->relro_end |
3146fac4 AM |
5832 | && lm->count != 0 |
5833 | && lm->sections[0]->vma >= link_info->relro_start) | |
8c37241b JJ |
5834 | break; |
5835 | } | |
8981c88a | 5836 | |
3146fac4 | 5837 | BFD_ASSERT (lm != NULL); |
8c37241b | 5838 | } |
129af99f AS |
5839 | else |
5840 | { | |
5841 | /* Otherwise we are copying an executable or shared | |
b84a33b5 | 5842 | library, but we need to use the same linker logic. */ |
129af99f AS |
5843 | for (lp = phdrs; lp < phdrs + count; ++lp) |
5844 | { | |
5845 | if (lp->p_type == PT_LOAD | |
5846 | && lp->p_paddr == p->p_paddr) | |
5847 | break; | |
5848 | } | |
b84a33b5 AM |
5849 | } |
5850 | ||
5851 | if (lp < phdrs + count) | |
5852 | { | |
5853 | p->p_vaddr = lp->p_vaddr; | |
5854 | p->p_paddr = lp->p_paddr; | |
5855 | p->p_offset = lp->p_offset; | |
5856 | if (link_info != NULL) | |
5857 | p->p_filesz = link_info->relro_end - lp->p_vaddr; | |
5858 | else if (m->p_size_valid) | |
5859 | p->p_filesz = m->p_size; | |
129af99f AS |
5860 | else |
5861 | abort (); | |
b84a33b5 | 5862 | p->p_memsz = p->p_filesz; |
f3944f72 L |
5863 | /* Preserve the alignment and flags if they are valid. The |
5864 | gold linker generates RW/4 for the PT_GNU_RELRO section. | |
5865 | It is better for objcopy/strip to honor these attributes | |
5866 | otherwise gdb will choke when using separate debug files. | |
5867 | */ | |
5868 | if (!m->p_align_valid) | |
5869 | p->p_align = 1; | |
5870 | if (!m->p_flags_valid) | |
341b8ee7 | 5871 | p->p_flags = PF_R; |
129af99f | 5872 | } |
9433b9b1 | 5873 | else |
b84a33b5 AM |
5874 | { |
5875 | memset (p, 0, sizeof *p); | |
5876 | p->p_type = PT_NULL; | |
5877 | } | |
129af99f | 5878 | } |
04c3a755 NS |
5879 | else if (p->p_type == PT_GNU_STACK) |
5880 | { | |
5881 | if (m->p_size_valid) | |
5882 | p->p_memsz = m->p_size; | |
5883 | } | |
129af99f AS |
5884 | else if (m->count != 0) |
5885 | { | |
e06efbf1 | 5886 | unsigned int i; |
1a9ccd70 | 5887 | |
129af99f AS |
5888 | if (p->p_type != PT_LOAD |
5889 | && (p->p_type != PT_NOTE | |
5890 | || bfd_get_format (abfd) != bfd_core)) | |
5891 | { | |
1a9ccd70 NC |
5892 | /* A user specified segment layout may include a PHDR |
5893 | segment that overlaps with a LOAD segment... */ | |
5894 | if (p->p_type == PT_PHDR) | |
5895 | { | |
5896 | m->count = 0; | |
5897 | continue; | |
5898 | } | |
5899 | ||
c86934ce NC |
5900 | if (m->includes_filehdr || m->includes_phdrs) |
5901 | { | |
b1fa9dd6 | 5902 | /* PR 17512: file: 2195325e. */ |
4eca0228 | 5903 | _bfd_error_handler |
76cfced5 AM |
5904 | (_("%B: error: non-load segment %d includes file header " |
5905 | "and/or program header"), | |
5906 | abfd, (int) (p - phdrs)); | |
c86934ce NC |
5907 | return FALSE; |
5908 | } | |
129af99f | 5909 | |
86b2281f | 5910 | p->p_filesz = 0; |
129af99f | 5911 | p->p_offset = m->sections[0]->filepos; |
86b2281f AM |
5912 | for (i = m->count; i-- != 0;) |
5913 | { | |
5914 | asection *sect = m->sections[i]; | |
5915 | Elf_Internal_Shdr *hdr = &elf_section_data (sect)->this_hdr; | |
5916 | if (hdr->sh_type != SHT_NOBITS) | |
5917 | { | |
5918 | p->p_filesz = (sect->filepos - m->sections[0]->filepos | |
5919 | + hdr->sh_size); | |
5920 | break; | |
5921 | } | |
5922 | } | |
129af99f AS |
5923 | } |
5924 | } | |
5925 | else if (m->includes_filehdr) | |
5926 | { | |
5927 | p->p_vaddr = filehdr_vaddr; | |
5928 | if (! m->p_paddr_valid) | |
5929 | p->p_paddr = filehdr_paddr; | |
5930 | } | |
5931 | else if (m->includes_phdrs) | |
5932 | { | |
5933 | p->p_vaddr = phdrs_vaddr; | |
5934 | if (! m->p_paddr_valid) | |
5935 | p->p_paddr = phdrs_paddr; | |
252b5132 RH |
5936 | } |
5937 | } | |
5938 | ||
12bd6957 | 5939 | elf_next_file_pos (abfd) = off; |
252b5132 | 5940 | |
b34976b6 | 5941 | return TRUE; |
252b5132 RH |
5942 | } |
5943 | ||
6a40cf0c NC |
5944 | static elf_section_list * |
5945 | find_section_in_list (unsigned int i, elf_section_list * list) | |
5946 | { | |
5947 | for (;list != NULL; list = list->next) | |
5948 | if (list->ndx == i) | |
5949 | break; | |
5950 | return list; | |
5951 | } | |
5952 | ||
252b5132 RH |
5953 | /* Work out the file positions of all the sections. This is called by |
5954 | _bfd_elf_compute_section_file_positions. All the section sizes and | |
5955 | VMAs must be known before this is called. | |
5956 | ||
e0638f70 AM |
5957 | Reloc sections come in two flavours: Those processed specially as |
5958 | "side-channel" data attached to a section to which they apply, and | |
5959 | those that bfd doesn't process as relocations. The latter sort are | |
5960 | stored in a normal bfd section by bfd_section_from_shdr. We don't | |
5961 | consider the former sort here, unless they form part of the loadable | |
5962 | image. Reloc sections not assigned here will be handled later by | |
5963 | assign_file_positions_for_relocs. | |
252b5132 RH |
5964 | |
5965 | We also don't set the positions of the .symtab and .strtab here. */ | |
5966 | ||
b34976b6 | 5967 | static bfd_boolean |
c84fca4d AO |
5968 | assign_file_positions_except_relocs (bfd *abfd, |
5969 | struct bfd_link_info *link_info) | |
252b5132 | 5970 | { |
5c182d5f AM |
5971 | struct elf_obj_tdata *tdata = elf_tdata (abfd); |
5972 | Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd); | |
9c5bfbb7 | 5973 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
252b5132 RH |
5974 | |
5975 | if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0 | |
5976 | && bfd_get_format (abfd) != bfd_core) | |
5977 | { | |
5c182d5f AM |
5978 | Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd); |
5979 | unsigned int num_sec = elf_numsections (abfd); | |
252b5132 RH |
5980 | Elf_Internal_Shdr **hdrpp; |
5981 | unsigned int i; | |
a485e98e | 5982 | file_ptr off; |
252b5132 RH |
5983 | |
5984 | /* Start after the ELF header. */ | |
5985 | off = i_ehdrp->e_ehsize; | |
5986 | ||
5987 | /* We are not creating an executable, which means that we are | |
5988 | not creating a program header, and that the actual order of | |
5989 | the sections in the file is unimportant. */ | |
9ad5cbcf | 5990 | for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++) |
252b5132 RH |
5991 | { |
5992 | Elf_Internal_Shdr *hdr; | |
5993 | ||
5994 | hdr = *hdrpp; | |
e0638f70 AM |
5995 | if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA) |
5996 | && hdr->bfd_section == NULL) | |
0ce398f1 L |
5997 | || (hdr->bfd_section != NULL |
5998 | && (hdr->bfd_section->flags & SEC_ELF_COMPRESS)) | |
5999 | /* Compress DWARF debug sections. */ | |
12bd6957 | 6000 | || i == elf_onesymtab (abfd) |
6a40cf0c NC |
6001 | || (elf_symtab_shndx_list (abfd) != NULL |
6002 | && hdr == i_shdrpp[elf_symtab_shndx_list (abfd)->ndx]) | |
3e19fb8f L |
6003 | || i == elf_strtab_sec (abfd) |
6004 | || i == elf_shstrtab_sec (abfd)) | |
252b5132 RH |
6005 | { |
6006 | hdr->sh_offset = -1; | |
252b5132 | 6007 | } |
9ad5cbcf | 6008 | else |
b34976b6 | 6009 | off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE); |
252b5132 | 6010 | } |
a485e98e AM |
6011 | |
6012 | elf_next_file_pos (abfd) = off; | |
252b5132 RH |
6013 | } |
6014 | else | |
6015 | { | |
f3520d2f AM |
6016 | unsigned int alloc; |
6017 | ||
252b5132 | 6018 | /* Assign file positions for the loaded sections based on the |
08a40648 | 6019 | assignment of sections to segments. */ |
f3520d2f AM |
6020 | if (!assign_file_positions_for_load_sections (abfd, link_info)) |
6021 | return FALSE; | |
6022 | ||
6023 | /* And for non-load sections. */ | |
6024 | if (!assign_file_positions_for_non_load_sections (abfd, link_info)) | |
6025 | return FALSE; | |
6026 | ||
e36284ab AM |
6027 | if (bed->elf_backend_modify_program_headers != NULL) |
6028 | { | |
6029 | if (!(*bed->elf_backend_modify_program_headers) (abfd, link_info)) | |
6030 | return FALSE; | |
6031 | } | |
6032 | ||
58e7ebac | 6033 | /* Set e_type in ELF header to ET_EXEC for -pie -Ttext-segment=. */ |
0e1862bb | 6034 | if (link_info != NULL && bfd_link_pie (link_info)) |
58e7ebac L |
6035 | { |
6036 | unsigned int num_segments = elf_elfheader (abfd)->e_phnum; | |
6037 | Elf_Internal_Phdr *segment = elf_tdata (abfd)->phdr; | |
6038 | Elf_Internal_Phdr *end_segment = &segment[num_segments]; | |
6039 | ||
6040 | /* Find the lowest p_vaddr in PT_LOAD segments. */ | |
6041 | bfd_vma p_vaddr = (bfd_vma) -1; | |
6042 | for (; segment < end_segment; segment++) | |
6043 | if (segment->p_type == PT_LOAD && p_vaddr > segment->p_vaddr) | |
6044 | p_vaddr = segment->p_vaddr; | |
6045 | ||
6046 | /* Set e_type to ET_EXEC if the lowest p_vaddr in PT_LOAD | |
6047 | segments is non-zero. */ | |
6048 | if (p_vaddr) | |
6049 | i_ehdrp->e_type = ET_EXEC; | |
6050 | } | |
6051 | ||
f3520d2f | 6052 | /* Write out the program headers. */ |
12bd6957 | 6053 | alloc = elf_program_header_size (abfd) / bed->s->sizeof_phdr; |
1a9ccd70 NC |
6054 | |
6055 | /* Sort the program headers into the ordering required by the ELF standard. */ | |
6056 | if (alloc == 0) | |
6057 | return TRUE; | |
6058 | ||
1a9ccd70 NC |
6059 | /* PR ld/20815 - Check that the program header segment, if present, will |
6060 | be loaded into memory. FIXME: The check below is not sufficient as | |
6061 | really all PT_LOAD segments should be checked before issuing an error | |
6062 | message. Plus the PHDR segment does not have to be the first segment | |
6063 | in the program header table. But this version of the check should | |
cd584857 NC |
6064 | catch all real world use cases. |
6065 | ||
6066 | FIXME: We used to have code here to sort the PT_LOAD segments into | |
6067 | ascending order, as per the ELF spec. But this breaks some programs, | |
6068 | including the Linux kernel. But really either the spec should be | |
6069 | changed or the programs updated. */ | |
1a9ccd70 | 6070 | if (alloc > 1 |
cd584857 NC |
6071 | && tdata->phdr[0].p_type == PT_PHDR |
6072 | && ! bed->elf_backend_allow_non_load_phdr (abfd, tdata->phdr, alloc) | |
6073 | && tdata->phdr[1].p_type == PT_LOAD | |
6074 | && (tdata->phdr[1].p_vaddr > tdata->phdr[0].p_vaddr | |
6075 | || (tdata->phdr[1].p_vaddr + tdata->phdr[1].p_memsz) | |
6076 | < (tdata->phdr[0].p_vaddr + tdata->phdr[0].p_memsz))) | |
1a9ccd70 NC |
6077 | { |
6078 | /* The fix for this error is usually to edit the linker script being | |
6079 | used and set up the program headers manually. Either that or | |
6080 | leave room for the headers at the start of the SECTIONS. */ | |
6081 | _bfd_error_handler (_("\ | |
6082 | %B: error: PHDR segment not covered by LOAD segment"), | |
6083 | abfd); | |
1a9ccd70 NC |
6084 | return FALSE; |
6085 | } | |
6086 | ||
f3520d2f | 6087 | if (bfd_seek (abfd, (bfd_signed_vma) bed->s->sizeof_ehdr, SEEK_SET) != 0 |
cd584857 NC |
6088 | || bed->s->write_out_phdrs (abfd, tdata->phdr, alloc) != 0) |
6089 | return FALSE; | |
252b5132 RH |
6090 | } |
6091 | ||
b34976b6 | 6092 | return TRUE; |
252b5132 RH |
6093 | } |
6094 | ||
b34976b6 | 6095 | static bfd_boolean |
217aa764 | 6096 | prep_headers (bfd *abfd) |
252b5132 | 6097 | { |
3d540e93 | 6098 | Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form. */ |
2b0f7ef9 | 6099 | struct elf_strtab_hash *shstrtab; |
9c5bfbb7 | 6100 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
252b5132 RH |
6101 | |
6102 | i_ehdrp = elf_elfheader (abfd); | |
252b5132 | 6103 | |
2b0f7ef9 | 6104 | shstrtab = _bfd_elf_strtab_init (); |
252b5132 | 6105 | if (shstrtab == NULL) |
b34976b6 | 6106 | return FALSE; |
252b5132 RH |
6107 | |
6108 | elf_shstrtab (abfd) = shstrtab; | |
6109 | ||
6110 | i_ehdrp->e_ident[EI_MAG0] = ELFMAG0; | |
6111 | i_ehdrp->e_ident[EI_MAG1] = ELFMAG1; | |
6112 | i_ehdrp->e_ident[EI_MAG2] = ELFMAG2; | |
6113 | i_ehdrp->e_ident[EI_MAG3] = ELFMAG3; | |
6114 | ||
6115 | i_ehdrp->e_ident[EI_CLASS] = bed->s->elfclass; | |
6116 | i_ehdrp->e_ident[EI_DATA] = | |
6117 | bfd_big_endian (abfd) ? ELFDATA2MSB : ELFDATA2LSB; | |
6118 | i_ehdrp->e_ident[EI_VERSION] = bed->s->ev_current; | |
6119 | ||
252b5132 RH |
6120 | if ((abfd->flags & DYNAMIC) != 0) |
6121 | i_ehdrp->e_type = ET_DYN; | |
6122 | else if ((abfd->flags & EXEC_P) != 0) | |
6123 | i_ehdrp->e_type = ET_EXEC; | |
6124 | else if (bfd_get_format (abfd) == bfd_core) | |
6125 | i_ehdrp->e_type = ET_CORE; | |
6126 | else | |
6127 | i_ehdrp->e_type = ET_REL; | |
6128 | ||
6129 | switch (bfd_get_arch (abfd)) | |
6130 | { | |
6131 | case bfd_arch_unknown: | |
6132 | i_ehdrp->e_machine = EM_NONE; | |
6133 | break; | |
aa4f99bb AO |
6134 | |
6135 | /* There used to be a long list of cases here, each one setting | |
6136 | e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE | |
6137 | in the corresponding bfd definition. To avoid duplication, | |
6138 | the switch was removed. Machines that need special handling | |
6139 | can generally do it in elf_backend_final_write_processing(), | |
6140 | unless they need the information earlier than the final write. | |
6141 | Such need can generally be supplied by replacing the tests for | |
6142 | e_machine with the conditions used to determine it. */ | |
252b5132 | 6143 | default: |
9c5bfbb7 AM |
6144 | i_ehdrp->e_machine = bed->elf_machine_code; |
6145 | } | |
aa4f99bb | 6146 | |
252b5132 RH |
6147 | i_ehdrp->e_version = bed->s->ev_current; |
6148 | i_ehdrp->e_ehsize = bed->s->sizeof_ehdr; | |
6149 | ||
c044fabd | 6150 | /* No program header, for now. */ |
252b5132 RH |
6151 | i_ehdrp->e_phoff = 0; |
6152 | i_ehdrp->e_phentsize = 0; | |
6153 | i_ehdrp->e_phnum = 0; | |
6154 | ||
c044fabd | 6155 | /* Each bfd section is section header entry. */ |
252b5132 RH |
6156 | i_ehdrp->e_entry = bfd_get_start_address (abfd); |
6157 | i_ehdrp->e_shentsize = bed->s->sizeof_shdr; | |
6158 | ||
c044fabd | 6159 | /* If we're building an executable, we'll need a program header table. */ |
252b5132 | 6160 | if (abfd->flags & EXEC_P) |
0e71e495 BE |
6161 | /* It all happens later. */ |
6162 | ; | |
252b5132 RH |
6163 | else |
6164 | { | |
6165 | i_ehdrp->e_phentsize = 0; | |
252b5132 RH |
6166 | i_ehdrp->e_phoff = 0; |
6167 | } | |
6168 | ||
6169 | elf_tdata (abfd)->symtab_hdr.sh_name = | |
b34976b6 | 6170 | (unsigned int) _bfd_elf_strtab_add (shstrtab, ".symtab", FALSE); |
252b5132 | 6171 | elf_tdata (abfd)->strtab_hdr.sh_name = |
b34976b6 | 6172 | (unsigned int) _bfd_elf_strtab_add (shstrtab, ".strtab", FALSE); |
252b5132 | 6173 | elf_tdata (abfd)->shstrtab_hdr.sh_name = |
b34976b6 | 6174 | (unsigned int) _bfd_elf_strtab_add (shstrtab, ".shstrtab", FALSE); |
252b5132 | 6175 | if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1 |
17ca87fc | 6176 | || elf_tdata (abfd)->strtab_hdr.sh_name == (unsigned int) -1 |
252b5132 | 6177 | || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1) |
b34976b6 | 6178 | return FALSE; |
252b5132 | 6179 | |
b34976b6 | 6180 | return TRUE; |
252b5132 RH |
6181 | } |
6182 | ||
6183 | /* Assign file positions for all the reloc sections which are not part | |
a485e98e | 6184 | of the loadable file image, and the file position of section headers. */ |
252b5132 | 6185 | |
0ce398f1 L |
6186 | static bfd_boolean |
6187 | _bfd_elf_assign_file_positions_for_non_load (bfd *abfd) | |
252b5132 RH |
6188 | { |
6189 | file_ptr off; | |
e06efbf1 | 6190 | Elf_Internal_Shdr **shdrpp, **end_shdrpp; |
3e19fb8f | 6191 | Elf_Internal_Shdr *shdrp; |
a485e98e AM |
6192 | Elf_Internal_Ehdr *i_ehdrp; |
6193 | const struct elf_backend_data *bed; | |
252b5132 | 6194 | |
12bd6957 | 6195 | off = elf_next_file_pos (abfd); |
252b5132 | 6196 | |
e06efbf1 L |
6197 | shdrpp = elf_elfsections (abfd); |
6198 | end_shdrpp = shdrpp + elf_numsections (abfd); | |
6199 | for (shdrpp++; shdrpp < end_shdrpp; shdrpp++) | |
252b5132 | 6200 | { |
252b5132 | 6201 | shdrp = *shdrpp; |
0ce398f1 L |
6202 | if (shdrp->sh_offset == -1) |
6203 | { | |
3e19fb8f | 6204 | asection *sec = shdrp->bfd_section; |
0ce398f1 L |
6205 | bfd_boolean is_rel = (shdrp->sh_type == SHT_REL |
6206 | || shdrp->sh_type == SHT_RELA); | |
6207 | if (is_rel | |
3e19fb8f | 6208 | || (sec != NULL && (sec->flags & SEC_ELF_COMPRESS))) |
0ce398f1 L |
6209 | { |
6210 | if (!is_rel) | |
6211 | { | |
3e19fb8f L |
6212 | const char *name = sec->name; |
6213 | struct bfd_elf_section_data *d; | |
6214 | ||
0ce398f1 | 6215 | /* Compress DWARF debug sections. */ |
3e19fb8f | 6216 | if (!bfd_compress_section (abfd, sec, |
0ce398f1 L |
6217 | shdrp->contents)) |
6218 | return FALSE; | |
3e19fb8f L |
6219 | |
6220 | if (sec->compress_status == COMPRESS_SECTION_DONE | |
6221 | && (abfd->flags & BFD_COMPRESS_GABI) == 0) | |
6222 | { | |
6223 | /* If section is compressed with zlib-gnu, convert | |
6224 | section name from .debug_* to .zdebug_*. */ | |
6225 | char *new_name | |
6226 | = convert_debug_to_zdebug (abfd, name); | |
6227 | if (new_name == NULL) | |
6228 | return FALSE; | |
6229 | name = new_name; | |
6230 | } | |
dd905818 | 6231 | /* Add section name to section name section. */ |
3e19fb8f L |
6232 | if (shdrp->sh_name != (unsigned int) -1) |
6233 | abort (); | |
6234 | shdrp->sh_name | |
6235 | = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), | |
6236 | name, FALSE); | |
6237 | d = elf_section_data (sec); | |
6238 | ||
dd905818 | 6239 | /* Add reloc section name to section name section. */ |
3e19fb8f L |
6240 | if (d->rel.hdr |
6241 | && !_bfd_elf_set_reloc_sh_name (abfd, | |
6242 | d->rel.hdr, | |
6243 | name, FALSE)) | |
6244 | return FALSE; | |
6245 | if (d->rela.hdr | |
6246 | && !_bfd_elf_set_reloc_sh_name (abfd, | |
6247 | d->rela.hdr, | |
91cb26da | 6248 | name, TRUE)) |
3e19fb8f L |
6249 | return FALSE; |
6250 | ||
0ce398f1 | 6251 | /* Update section size and contents. */ |
3e19fb8f L |
6252 | shdrp->sh_size = sec->size; |
6253 | shdrp->contents = sec->contents; | |
0ce398f1 L |
6254 | shdrp->bfd_section->contents = NULL; |
6255 | } | |
6256 | off = _bfd_elf_assign_file_position_for_section (shdrp, | |
6257 | off, | |
6258 | TRUE); | |
6259 | } | |
6260 | } | |
252b5132 RH |
6261 | } |
6262 | ||
3e19fb8f L |
6263 | /* Place section name section after DWARF debug sections have been |
6264 | compressed. */ | |
6265 | _bfd_elf_strtab_finalize (elf_shstrtab (abfd)); | |
6266 | shdrp = &elf_tdata (abfd)->shstrtab_hdr; | |
6267 | shdrp->sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd)); | |
6268 | off = _bfd_elf_assign_file_position_for_section (shdrp, off, TRUE); | |
6269 | ||
6270 | /* Place the section headers. */ | |
a485e98e AM |
6271 | i_ehdrp = elf_elfheader (abfd); |
6272 | bed = get_elf_backend_data (abfd); | |
6273 | off = align_file_position (off, 1 << bed->s->log_file_align); | |
6274 | i_ehdrp->e_shoff = off; | |
6275 | off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize; | |
12bd6957 | 6276 | elf_next_file_pos (abfd) = off; |
0ce398f1 L |
6277 | |
6278 | return TRUE; | |
252b5132 RH |
6279 | } |
6280 | ||
b34976b6 | 6281 | bfd_boolean |
217aa764 | 6282 | _bfd_elf_write_object_contents (bfd *abfd) |
252b5132 | 6283 | { |
9c5bfbb7 | 6284 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
252b5132 | 6285 | Elf_Internal_Shdr **i_shdrp; |
b34976b6 | 6286 | bfd_boolean failed; |
9ad5cbcf | 6287 | unsigned int count, num_sec; |
30e8ee25 | 6288 | struct elf_obj_tdata *t; |
252b5132 RH |
6289 | |
6290 | if (! abfd->output_has_begun | |
217aa764 | 6291 | && ! _bfd_elf_compute_section_file_positions (abfd, NULL)) |
b34976b6 | 6292 | return FALSE; |
252b5132 RH |
6293 | |
6294 | i_shdrp = elf_elfsections (abfd); | |
252b5132 | 6295 | |
b34976b6 | 6296 | failed = FALSE; |
252b5132 RH |
6297 | bfd_map_over_sections (abfd, bed->s->write_relocs, &failed); |
6298 | if (failed) | |
b34976b6 | 6299 | return FALSE; |
252b5132 | 6300 | |
0ce398f1 L |
6301 | if (!_bfd_elf_assign_file_positions_for_non_load (abfd)) |
6302 | return FALSE; | |
252b5132 | 6303 | |
c044fabd | 6304 | /* After writing the headers, we need to write the sections too... */ |
9ad5cbcf AM |
6305 | num_sec = elf_numsections (abfd); |
6306 | for (count = 1; count < num_sec; count++) | |
252b5132 | 6307 | { |
3e19fb8f L |
6308 | i_shdrp[count]->sh_name |
6309 | = _bfd_elf_strtab_offset (elf_shstrtab (abfd), | |
6310 | i_shdrp[count]->sh_name); | |
252b5132 RH |
6311 | if (bed->elf_backend_section_processing) |
6312 | (*bed->elf_backend_section_processing) (abfd, i_shdrp[count]); | |
6313 | if (i_shdrp[count]->contents) | |
6314 | { | |
dc810e39 AM |
6315 | bfd_size_type amt = i_shdrp[count]->sh_size; |
6316 | ||
252b5132 | 6317 | if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0 |
dc810e39 | 6318 | || bfd_bwrite (i_shdrp[count]->contents, amt, abfd) != amt) |
b34976b6 | 6319 | return FALSE; |
252b5132 RH |
6320 | } |
6321 | } | |
6322 | ||
6323 | /* Write out the section header names. */ | |
30e8ee25 | 6324 | t = elf_tdata (abfd); |
26ae6d5e | 6325 | if (elf_shstrtab (abfd) != NULL |
30e8ee25 | 6326 | && (bfd_seek (abfd, t->shstrtab_hdr.sh_offset, SEEK_SET) != 0 |
08a40648 | 6327 | || !_bfd_elf_strtab_emit (abfd, elf_shstrtab (abfd)))) |
b34976b6 | 6328 | return FALSE; |
252b5132 RH |
6329 | |
6330 | if (bed->elf_backend_final_write_processing) | |
12bd6957 | 6331 | (*bed->elf_backend_final_write_processing) (abfd, elf_linker (abfd)); |
252b5132 | 6332 | |
ff59fc36 RM |
6333 | if (!bed->s->write_shdrs_and_ehdr (abfd)) |
6334 | return FALSE; | |
6335 | ||
6336 | /* This is last since write_shdrs_and_ehdr can touch i_shdrp[0]. */ | |
c0355132 AM |
6337 | if (t->o->build_id.after_write_object_contents != NULL) |
6338 | return (*t->o->build_id.after_write_object_contents) (abfd); | |
ff59fc36 RM |
6339 | |
6340 | return TRUE; | |
252b5132 RH |
6341 | } |
6342 | ||
b34976b6 | 6343 | bfd_boolean |
217aa764 | 6344 | _bfd_elf_write_corefile_contents (bfd *abfd) |
252b5132 | 6345 | { |
c044fabd | 6346 | /* Hopefully this can be done just like an object file. */ |
252b5132 RH |
6347 | return _bfd_elf_write_object_contents (abfd); |
6348 | } | |
c044fabd KH |
6349 | |
6350 | /* Given a section, search the header to find them. */ | |
6351 | ||
cb33740c | 6352 | unsigned int |
198beae2 | 6353 | _bfd_elf_section_from_bfd_section (bfd *abfd, struct bfd_section *asect) |
252b5132 | 6354 | { |
9c5bfbb7 | 6355 | const struct elf_backend_data *bed; |
91d6fa6a | 6356 | unsigned int sec_index; |
252b5132 | 6357 | |
9ad5cbcf AM |
6358 | if (elf_section_data (asect) != NULL |
6359 | && elf_section_data (asect)->this_idx != 0) | |
6360 | return elf_section_data (asect)->this_idx; | |
6361 | ||
6362 | if (bfd_is_abs_section (asect)) | |
91d6fa6a | 6363 | sec_index = SHN_ABS; |
af746e92 | 6364 | else if (bfd_is_com_section (asect)) |
91d6fa6a | 6365 | sec_index = SHN_COMMON; |
af746e92 | 6366 | else if (bfd_is_und_section (asect)) |
91d6fa6a | 6367 | sec_index = SHN_UNDEF; |
af746e92 | 6368 | else |
91d6fa6a | 6369 | sec_index = SHN_BAD; |
252b5132 | 6370 | |
af746e92 | 6371 | bed = get_elf_backend_data (abfd); |
252b5132 RH |
6372 | if (bed->elf_backend_section_from_bfd_section) |
6373 | { | |
91d6fa6a | 6374 | int retval = sec_index; |
9ad5cbcf | 6375 | |
af746e92 AM |
6376 | if ((*bed->elf_backend_section_from_bfd_section) (abfd, asect, &retval)) |
6377 | return retval; | |
252b5132 RH |
6378 | } |
6379 | ||
91d6fa6a | 6380 | if (sec_index == SHN_BAD) |
af746e92 | 6381 | bfd_set_error (bfd_error_nonrepresentable_section); |
252b5132 | 6382 | |
91d6fa6a | 6383 | return sec_index; |
252b5132 RH |
6384 | } |
6385 | ||
6386 | /* Given a BFD symbol, return the index in the ELF symbol table, or -1 | |
6387 | on error. */ | |
6388 | ||
6389 | int | |
217aa764 | 6390 | _bfd_elf_symbol_from_bfd_symbol (bfd *abfd, asymbol **asym_ptr_ptr) |
252b5132 RH |
6391 | { |
6392 | asymbol *asym_ptr = *asym_ptr_ptr; | |
6393 | int idx; | |
6394 | flagword flags = asym_ptr->flags; | |
6395 | ||
6396 | /* When gas creates relocations against local labels, it creates its | |
6397 | own symbol for the section, but does put the symbol into the | |
6398 | symbol chain, so udata is 0. When the linker is generating | |
6399 | relocatable output, this section symbol may be for one of the | |
6400 | input sections rather than the output section. */ | |
6401 | if (asym_ptr->udata.i == 0 | |
6402 | && (flags & BSF_SECTION_SYM) | |
6403 | && asym_ptr->section) | |
6404 | { | |
5372391b | 6405 | asection *sec; |
252b5132 RH |
6406 | int indx; |
6407 | ||
5372391b AM |
6408 | sec = asym_ptr->section; |
6409 | if (sec->owner != abfd && sec->output_section != NULL) | |
6410 | sec = sec->output_section; | |
6411 | if (sec->owner == abfd | |
6412 | && (indx = sec->index) < elf_num_section_syms (abfd) | |
4e89ac30 | 6413 | && elf_section_syms (abfd)[indx] != NULL) |
252b5132 RH |
6414 | asym_ptr->udata.i = elf_section_syms (abfd)[indx]->udata.i; |
6415 | } | |
6416 | ||
6417 | idx = asym_ptr->udata.i; | |
6418 | ||
6419 | if (idx == 0) | |
6420 | { | |
6421 | /* This case can occur when using --strip-symbol on a symbol | |
08a40648 | 6422 | which is used in a relocation entry. */ |
4eca0228 | 6423 | _bfd_error_handler |
695344c0 | 6424 | /* xgettext:c-format */ |
d003868e AM |
6425 | (_("%B: symbol `%s' required but not present"), |
6426 | abfd, bfd_asymbol_name (asym_ptr)); | |
252b5132 RH |
6427 | bfd_set_error (bfd_error_no_symbols); |
6428 | return -1; | |
6429 | } | |
6430 | ||
6431 | #if DEBUG & 4 | |
6432 | { | |
6433 | fprintf (stderr, | |
cd9af601 AM |
6434 | "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8x\n", |
6435 | (long) asym_ptr, asym_ptr->name, idx, flags); | |
252b5132 RH |
6436 | fflush (stderr); |
6437 | } | |
6438 | #endif | |
6439 | ||
6440 | return idx; | |
6441 | } | |
6442 | ||
84d1d650 | 6443 | /* Rewrite program header information. */ |
252b5132 | 6444 | |
b34976b6 | 6445 | static bfd_boolean |
84d1d650 | 6446 | rewrite_elf_program_header (bfd *ibfd, bfd *obfd) |
252b5132 | 6447 | { |
b34976b6 AM |
6448 | Elf_Internal_Ehdr *iehdr; |
6449 | struct elf_segment_map *map; | |
6450 | struct elf_segment_map *map_first; | |
6451 | struct elf_segment_map **pointer_to_map; | |
6452 | Elf_Internal_Phdr *segment; | |
6453 | asection *section; | |
6454 | unsigned int i; | |
6455 | unsigned int num_segments; | |
6456 | bfd_boolean phdr_included = FALSE; | |
5c44b38e | 6457 | bfd_boolean p_paddr_valid; |
b34976b6 AM |
6458 | bfd_vma maxpagesize; |
6459 | struct elf_segment_map *phdr_adjust_seg = NULL; | |
6460 | unsigned int phdr_adjust_num = 0; | |
9c5bfbb7 | 6461 | const struct elf_backend_data *bed; |
bc67d8a6 | 6462 | |
caf47ea6 | 6463 | bed = get_elf_backend_data (ibfd); |
252b5132 RH |
6464 | iehdr = elf_elfheader (ibfd); |
6465 | ||
bc67d8a6 | 6466 | map_first = NULL; |
c044fabd | 6467 | pointer_to_map = &map_first; |
252b5132 RH |
6468 | |
6469 | num_segments = elf_elfheader (ibfd)->e_phnum; | |
bc67d8a6 NC |
6470 | maxpagesize = get_elf_backend_data (obfd)->maxpagesize; |
6471 | ||
6472 | /* Returns the end address of the segment + 1. */ | |
aecc8f8a AM |
6473 | #define SEGMENT_END(segment, start) \ |
6474 | (start + (segment->p_memsz > segment->p_filesz \ | |
6475 | ? segment->p_memsz : segment->p_filesz)) | |
bc67d8a6 | 6476 | |
eecdbe52 JJ |
6477 | #define SECTION_SIZE(section, segment) \ |
6478 | (((section->flags & (SEC_HAS_CONTENTS | SEC_THREAD_LOCAL)) \ | |
6479 | != SEC_THREAD_LOCAL || segment->p_type == PT_TLS) \ | |
eea6121a | 6480 | ? section->size : 0) |
eecdbe52 | 6481 | |
b34976b6 | 6482 | /* Returns TRUE if the given section is contained within |
bc67d8a6 | 6483 | the given segment. VMA addresses are compared. */ |
aecc8f8a AM |
6484 | #define IS_CONTAINED_BY_VMA(section, segment) \ |
6485 | (section->vma >= segment->p_vaddr \ | |
eecdbe52 | 6486 | && (section->vma + SECTION_SIZE (section, segment) \ |
aecc8f8a | 6487 | <= (SEGMENT_END (segment, segment->p_vaddr)))) |
c044fabd | 6488 | |
b34976b6 | 6489 | /* Returns TRUE if the given section is contained within |
bc67d8a6 | 6490 | the given segment. LMA addresses are compared. */ |
aecc8f8a AM |
6491 | #define IS_CONTAINED_BY_LMA(section, segment, base) \ |
6492 | (section->lma >= base \ | |
eecdbe52 | 6493 | && (section->lma + SECTION_SIZE (section, segment) \ |
aecc8f8a | 6494 | <= SEGMENT_END (segment, base))) |
252b5132 | 6495 | |
0efc80c8 L |
6496 | /* Handle PT_NOTE segment. */ |
6497 | #define IS_NOTE(p, s) \ | |
aecc8f8a | 6498 | (p->p_type == PT_NOTE \ |
0efc80c8 | 6499 | && elf_section_type (s) == SHT_NOTE \ |
aecc8f8a | 6500 | && (bfd_vma) s->filepos >= p->p_offset \ |
cb3ff1e5 | 6501 | && ((bfd_vma) s->filepos + s->size \ |
aecc8f8a | 6502 | <= p->p_offset + p->p_filesz)) |
252b5132 | 6503 | |
0efc80c8 L |
6504 | /* Special case: corefile "NOTE" section containing regs, prpsinfo |
6505 | etc. */ | |
6506 | #define IS_COREFILE_NOTE(p, s) \ | |
6507 | (IS_NOTE (p, s) \ | |
6508 | && bfd_get_format (ibfd) == bfd_core \ | |
6509 | && s->vma == 0 \ | |
6510 | && s->lma == 0) | |
6511 | ||
252b5132 RH |
6512 | /* The complicated case when p_vaddr is 0 is to handle the Solaris |
6513 | linker, which generates a PT_INTERP section with p_vaddr and | |
6514 | p_memsz set to 0. */ | |
aecc8f8a AM |
6515 | #define IS_SOLARIS_PT_INTERP(p, s) \ |
6516 | (p->p_vaddr == 0 \ | |
6517 | && p->p_paddr == 0 \ | |
6518 | && p->p_memsz == 0 \ | |
6519 | && p->p_filesz > 0 \ | |
6520 | && (s->flags & SEC_HAS_CONTENTS) != 0 \ | |
eea6121a | 6521 | && s->size > 0 \ |
aecc8f8a | 6522 | && (bfd_vma) s->filepos >= p->p_offset \ |
cb3ff1e5 | 6523 | && ((bfd_vma) s->filepos + s->size \ |
aecc8f8a | 6524 | <= p->p_offset + p->p_filesz)) |
5c440b1e | 6525 | |
bc67d8a6 NC |
6526 | /* Decide if the given section should be included in the given segment. |
6527 | A section will be included if: | |
f5ffc919 | 6528 | 1. It is within the address space of the segment -- we use the LMA |
08a40648 | 6529 | if that is set for the segment and the VMA otherwise, |
0efc80c8 | 6530 | 2. It is an allocated section or a NOTE section in a PT_NOTE |
d324f6d6 | 6531 | segment. |
bc67d8a6 | 6532 | 3. There is an output section associated with it, |
eecdbe52 | 6533 | 4. The section has not already been allocated to a previous segment. |
2b05f1b7 | 6534 | 5. PT_GNU_STACK segments do not include any sections. |
03394ac9 | 6535 | 6. PT_TLS segment includes only SHF_TLS sections. |
6f79b219 JJ |
6536 | 7. SHF_TLS sections are only in PT_TLS or PT_LOAD segments. |
6537 | 8. PT_DYNAMIC should not contain empty sections at the beginning | |
08a40648 | 6538 | (with the possible exception of .dynamic). */ |
9f17e2a6 | 6539 | #define IS_SECTION_IN_INPUT_SEGMENT(section, segment, bed) \ |
2b05f1b7 L |
6540 | ((((segment->p_paddr \ |
6541 | ? IS_CONTAINED_BY_LMA (section, segment, segment->p_paddr) \ | |
6542 | : IS_CONTAINED_BY_VMA (section, segment)) \ | |
6543 | && (section->flags & SEC_ALLOC) != 0) \ | |
0efc80c8 | 6544 | || IS_NOTE (segment, section)) \ |
2b05f1b7 L |
6545 | && segment->p_type != PT_GNU_STACK \ |
6546 | && (segment->p_type != PT_TLS \ | |
6547 | || (section->flags & SEC_THREAD_LOCAL)) \ | |
6548 | && (segment->p_type == PT_LOAD \ | |
6549 | || segment->p_type == PT_TLS \ | |
6550 | || (section->flags & SEC_THREAD_LOCAL) == 0) \ | |
6551 | && (segment->p_type != PT_DYNAMIC \ | |
6552 | || SECTION_SIZE (section, segment) > 0 \ | |
6553 | || (segment->p_paddr \ | |
6554 | ? segment->p_paddr != section->lma \ | |
6555 | : segment->p_vaddr != section->vma) \ | |
6556 | || (strcmp (bfd_get_section_name (ibfd, section), ".dynamic") \ | |
6557 | == 0)) \ | |
0067a569 | 6558 | && !section->segment_mark) |
bc67d8a6 | 6559 | |
9f17e2a6 L |
6560 | /* If the output section of a section in the input segment is NULL, |
6561 | it is removed from the corresponding output segment. */ | |
6562 | #define INCLUDE_SECTION_IN_SEGMENT(section, segment, bed) \ | |
6563 | (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed) \ | |
6564 | && section->output_section != NULL) | |
6565 | ||
b34976b6 | 6566 | /* Returns TRUE iff seg1 starts after the end of seg2. */ |
b5f852ea NC |
6567 | #define SEGMENT_AFTER_SEGMENT(seg1, seg2, field) \ |
6568 | (seg1->field >= SEGMENT_END (seg2, seg2->field)) | |
6569 | ||
6570 | /* Returns TRUE iff seg1 and seg2 overlap. Segments overlap iff both | |
6571 | their VMA address ranges and their LMA address ranges overlap. | |
6572 | It is possible to have overlapping VMA ranges without overlapping LMA | |
6573 | ranges. RedBoot images for example can have both .data and .bss mapped | |
6574 | to the same VMA range, but with the .data section mapped to a different | |
6575 | LMA. */ | |
aecc8f8a | 6576 | #define SEGMENT_OVERLAPS(seg1, seg2) \ |
b5f852ea | 6577 | ( !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_vaddr) \ |
08a40648 | 6578 | || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_vaddr)) \ |
b5f852ea | 6579 | && !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_paddr) \ |
08a40648 | 6580 | || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_paddr))) |
bc67d8a6 NC |
6581 | |
6582 | /* Initialise the segment mark field. */ | |
6583 | for (section = ibfd->sections; section != NULL; section = section->next) | |
b34976b6 | 6584 | section->segment_mark = FALSE; |
bc67d8a6 | 6585 | |
5c44b38e AM |
6586 | /* The Solaris linker creates program headers in which all the |
6587 | p_paddr fields are zero. When we try to objcopy or strip such a | |
6588 | file, we get confused. Check for this case, and if we find it | |
6589 | don't set the p_paddr_valid fields. */ | |
6590 | p_paddr_valid = FALSE; | |
6591 | for (i = 0, segment = elf_tdata (ibfd)->phdr; | |
6592 | i < num_segments; | |
6593 | i++, segment++) | |
6594 | if (segment->p_paddr != 0) | |
6595 | { | |
6596 | p_paddr_valid = TRUE; | |
6597 | break; | |
6598 | } | |
6599 | ||
252b5132 | 6600 | /* Scan through the segments specified in the program header |
bc67d8a6 | 6601 | of the input BFD. For this first scan we look for overlaps |
9ad5cbcf | 6602 | in the loadable segments. These can be created by weird |
aecc8f8a | 6603 | parameters to objcopy. Also, fix some solaris weirdness. */ |
bc67d8a6 NC |
6604 | for (i = 0, segment = elf_tdata (ibfd)->phdr; |
6605 | i < num_segments; | |
c044fabd | 6606 | i++, segment++) |
252b5132 | 6607 | { |
252b5132 | 6608 | unsigned int j; |
c044fabd | 6609 | Elf_Internal_Phdr *segment2; |
252b5132 | 6610 | |
aecc8f8a AM |
6611 | if (segment->p_type == PT_INTERP) |
6612 | for (section = ibfd->sections; section; section = section->next) | |
6613 | if (IS_SOLARIS_PT_INTERP (segment, section)) | |
6614 | { | |
6615 | /* Mininal change so that the normal section to segment | |
4cc11e76 | 6616 | assignment code will work. */ |
aecc8f8a AM |
6617 | segment->p_vaddr = section->vma; |
6618 | break; | |
6619 | } | |
6620 | ||
bc67d8a6 | 6621 | if (segment->p_type != PT_LOAD) |
b10a8ae0 L |
6622 | { |
6623 | /* Remove PT_GNU_RELRO segment. */ | |
6624 | if (segment->p_type == PT_GNU_RELRO) | |
6625 | segment->p_type = PT_NULL; | |
6626 | continue; | |
6627 | } | |
c044fabd | 6628 | |
bc67d8a6 | 6629 | /* Determine if this segment overlaps any previous segments. */ |
0067a569 | 6630 | for (j = 0, segment2 = elf_tdata (ibfd)->phdr; j < i; j++, segment2++) |
bc67d8a6 NC |
6631 | { |
6632 | bfd_signed_vma extra_length; | |
c044fabd | 6633 | |
bc67d8a6 | 6634 | if (segment2->p_type != PT_LOAD |
0067a569 | 6635 | || !SEGMENT_OVERLAPS (segment, segment2)) |
bc67d8a6 | 6636 | continue; |
c044fabd | 6637 | |
bc67d8a6 NC |
6638 | /* Merge the two segments together. */ |
6639 | if (segment2->p_vaddr < segment->p_vaddr) | |
6640 | { | |
c044fabd | 6641 | /* Extend SEGMENT2 to include SEGMENT and then delete |
08a40648 | 6642 | SEGMENT. */ |
0067a569 AM |
6643 | extra_length = (SEGMENT_END (segment, segment->p_vaddr) |
6644 | - SEGMENT_END (segment2, segment2->p_vaddr)); | |
c044fabd | 6645 | |
bc67d8a6 NC |
6646 | if (extra_length > 0) |
6647 | { | |
0067a569 | 6648 | segment2->p_memsz += extra_length; |
bc67d8a6 NC |
6649 | segment2->p_filesz += extra_length; |
6650 | } | |
c044fabd | 6651 | |
bc67d8a6 | 6652 | segment->p_type = PT_NULL; |
c044fabd | 6653 | |
bc67d8a6 NC |
6654 | /* Since we have deleted P we must restart the outer loop. */ |
6655 | i = 0; | |
6656 | segment = elf_tdata (ibfd)->phdr; | |
6657 | break; | |
6658 | } | |
6659 | else | |
6660 | { | |
c044fabd | 6661 | /* Extend SEGMENT to include SEGMENT2 and then delete |
08a40648 | 6662 | SEGMENT2. */ |
0067a569 AM |
6663 | extra_length = (SEGMENT_END (segment2, segment2->p_vaddr) |
6664 | - SEGMENT_END (segment, segment->p_vaddr)); | |
c044fabd | 6665 | |
bc67d8a6 NC |
6666 | if (extra_length > 0) |
6667 | { | |
0067a569 | 6668 | segment->p_memsz += extra_length; |
bc67d8a6 NC |
6669 | segment->p_filesz += extra_length; |
6670 | } | |
c044fabd | 6671 | |
bc67d8a6 NC |
6672 | segment2->p_type = PT_NULL; |
6673 | } | |
6674 | } | |
6675 | } | |
c044fabd | 6676 | |
bc67d8a6 NC |
6677 | /* The second scan attempts to assign sections to segments. */ |
6678 | for (i = 0, segment = elf_tdata (ibfd)->phdr; | |
6679 | i < num_segments; | |
0067a569 | 6680 | i++, segment++) |
bc67d8a6 | 6681 | { |
0067a569 AM |
6682 | unsigned int section_count; |
6683 | asection **sections; | |
6684 | asection *output_section; | |
6685 | unsigned int isec; | |
6686 | bfd_vma matching_lma; | |
6687 | bfd_vma suggested_lma; | |
6688 | unsigned int j; | |
dc810e39 | 6689 | bfd_size_type amt; |
0067a569 AM |
6690 | asection *first_section; |
6691 | bfd_boolean first_matching_lma; | |
6692 | bfd_boolean first_suggested_lma; | |
bc67d8a6 NC |
6693 | |
6694 | if (segment->p_type == PT_NULL) | |
6695 | continue; | |
c044fabd | 6696 | |
9f17e2a6 | 6697 | first_section = NULL; |
bc67d8a6 | 6698 | /* Compute how many sections might be placed into this segment. */ |
b5f852ea NC |
6699 | for (section = ibfd->sections, section_count = 0; |
6700 | section != NULL; | |
6701 | section = section->next) | |
9f17e2a6 L |
6702 | { |
6703 | /* Find the first section in the input segment, which may be | |
6704 | removed from the corresponding output segment. */ | |
6705 | if (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed)) | |
6706 | { | |
6707 | if (first_section == NULL) | |
6708 | first_section = section; | |
6709 | if (section->output_section != NULL) | |
6710 | ++section_count; | |
6711 | } | |
6712 | } | |
811072d8 | 6713 | |
b5f852ea NC |
6714 | /* Allocate a segment map big enough to contain |
6715 | all of the sections we have selected. */ | |
dc810e39 AM |
6716 | amt = sizeof (struct elf_segment_map); |
6717 | amt += ((bfd_size_type) section_count - 1) * sizeof (asection *); | |
a50b1753 | 6718 | map = (struct elf_segment_map *) bfd_zalloc (obfd, amt); |
bc67d8a6 | 6719 | if (map == NULL) |
b34976b6 | 6720 | return FALSE; |
252b5132 RH |
6721 | |
6722 | /* Initialise the fields of the segment map. Default to | |
6723 | using the physical address of the segment in the input BFD. */ | |
0067a569 AM |
6724 | map->next = NULL; |
6725 | map->p_type = segment->p_type; | |
6726 | map->p_flags = segment->p_flags; | |
bc67d8a6 | 6727 | map->p_flags_valid = 1; |
55d55ac7 | 6728 | |
9f17e2a6 L |
6729 | /* If the first section in the input segment is removed, there is |
6730 | no need to preserve segment physical address in the corresponding | |
6731 | output segment. */ | |
945c025a | 6732 | if (!first_section || first_section->output_section != NULL) |
9f17e2a6 L |
6733 | { |
6734 | map->p_paddr = segment->p_paddr; | |
5c44b38e | 6735 | map->p_paddr_valid = p_paddr_valid; |
9f17e2a6 | 6736 | } |
252b5132 RH |
6737 | |
6738 | /* Determine if this segment contains the ELF file header | |
6739 | and if it contains the program headers themselves. */ | |
bc67d8a6 NC |
6740 | map->includes_filehdr = (segment->p_offset == 0 |
6741 | && segment->p_filesz >= iehdr->e_ehsize); | |
bc67d8a6 | 6742 | map->includes_phdrs = 0; |
252b5132 | 6743 | |
0067a569 | 6744 | if (!phdr_included || segment->p_type != PT_LOAD) |
252b5132 | 6745 | { |
bc67d8a6 NC |
6746 | map->includes_phdrs = |
6747 | (segment->p_offset <= (bfd_vma) iehdr->e_phoff | |
6748 | && (segment->p_offset + segment->p_filesz | |
252b5132 RH |
6749 | >= ((bfd_vma) iehdr->e_phoff |
6750 | + iehdr->e_phnum * iehdr->e_phentsize))); | |
c044fabd | 6751 | |
bc67d8a6 | 6752 | if (segment->p_type == PT_LOAD && map->includes_phdrs) |
b34976b6 | 6753 | phdr_included = TRUE; |
252b5132 RH |
6754 | } |
6755 | ||
bc67d8a6 | 6756 | if (section_count == 0) |
252b5132 RH |
6757 | { |
6758 | /* Special segments, such as the PT_PHDR segment, may contain | |
6759 | no sections, but ordinary, loadable segments should contain | |
1ed89aa9 | 6760 | something. They are allowed by the ELF spec however, so only |
f98450c6 NC |
6761 | a warning is produced. |
6762 | There is however the valid use case of embedded systems which | |
6763 | have segments with p_filesz of 0 and a p_memsz > 0 to initialize | |
6764 | flash memory with zeros. No warning is shown for that case. */ | |
6765 | if (segment->p_type == PT_LOAD | |
6766 | && (segment->p_filesz > 0 || segment->p_memsz == 0)) | |
6767 | /* xgettext:c-format */ | |
76cfced5 AM |
6768 | _bfd_error_handler (_("%B: warning: Empty loadable segment detected" |
6769 | " at vaddr=%#Lx, is this intentional?"), | |
f98450c6 | 6770 | ibfd, segment->p_vaddr); |
252b5132 | 6771 | |
bc67d8a6 | 6772 | map->count = 0; |
c044fabd KH |
6773 | *pointer_to_map = map; |
6774 | pointer_to_map = &map->next; | |
252b5132 RH |
6775 | |
6776 | continue; | |
6777 | } | |
6778 | ||
6779 | /* Now scan the sections in the input BFD again and attempt | |
6780 | to add their corresponding output sections to the segment map. | |
6781 | The problem here is how to handle an output section which has | |
6782 | been moved (ie had its LMA changed). There are four possibilities: | |
6783 | ||
6784 | 1. None of the sections have been moved. | |
6785 | In this case we can continue to use the segment LMA from the | |
6786 | input BFD. | |
6787 | ||
6788 | 2. All of the sections have been moved by the same amount. | |
6789 | In this case we can change the segment's LMA to match the LMA | |
6790 | of the first section. | |
6791 | ||
6792 | 3. Some of the sections have been moved, others have not. | |
6793 | In this case those sections which have not been moved can be | |
6794 | placed in the current segment which will have to have its size, | |
6795 | and possibly its LMA changed, and a new segment or segments will | |
6796 | have to be created to contain the other sections. | |
6797 | ||
b5f852ea | 6798 | 4. The sections have been moved, but not by the same amount. |
252b5132 RH |
6799 | In this case we can change the segment's LMA to match the LMA |
6800 | of the first section and we will have to create a new segment | |
6801 | or segments to contain the other sections. | |
6802 | ||
6803 | In order to save time, we allocate an array to hold the section | |
6804 | pointers that we are interested in. As these sections get assigned | |
6805 | to a segment, they are removed from this array. */ | |
6806 | ||
a50b1753 | 6807 | sections = (asection **) bfd_malloc2 (section_count, sizeof (asection *)); |
252b5132 | 6808 | if (sections == NULL) |
b34976b6 | 6809 | return FALSE; |
252b5132 RH |
6810 | |
6811 | /* Step One: Scan for segment vs section LMA conflicts. | |
6812 | Also add the sections to the section array allocated above. | |
6813 | Also add the sections to the current segment. In the common | |
6814 | case, where the sections have not been moved, this means that | |
6815 | we have completely filled the segment, and there is nothing | |
6816 | more to do. */ | |
252b5132 | 6817 | isec = 0; |
72730e0c | 6818 | matching_lma = 0; |
252b5132 | 6819 | suggested_lma = 0; |
0067a569 AM |
6820 | first_matching_lma = TRUE; |
6821 | first_suggested_lma = TRUE; | |
252b5132 | 6822 | |
461c4b2e | 6823 | for (section = first_section, j = 0; |
bc67d8a6 NC |
6824 | section != NULL; |
6825 | section = section->next) | |
252b5132 | 6826 | { |
caf47ea6 | 6827 | if (INCLUDE_SECTION_IN_SEGMENT (section, segment, bed)) |
c0f7859b | 6828 | { |
bc67d8a6 NC |
6829 | output_section = section->output_section; |
6830 | ||
0067a569 | 6831 | sections[j++] = section; |
252b5132 RH |
6832 | |
6833 | /* The Solaris native linker always sets p_paddr to 0. | |
6834 | We try to catch that case here, and set it to the | |
5e8d7549 NC |
6835 | correct value. Note - some backends require that |
6836 | p_paddr be left as zero. */ | |
5c44b38e | 6837 | if (!p_paddr_valid |
4455705d | 6838 | && segment->p_vaddr != 0 |
0067a569 | 6839 | && !bed->want_p_paddr_set_to_zero |
252b5132 | 6840 | && isec == 0 |
bc67d8a6 | 6841 | && output_section->lma != 0 |
0067a569 AM |
6842 | && output_section->vma == (segment->p_vaddr |
6843 | + (map->includes_filehdr | |
6844 | ? iehdr->e_ehsize | |
6845 | : 0) | |
6846 | + (map->includes_phdrs | |
6847 | ? (iehdr->e_phnum | |
6848 | * iehdr->e_phentsize) | |
6849 | : 0))) | |
bc67d8a6 | 6850 | map->p_paddr = segment->p_vaddr; |
252b5132 RH |
6851 | |
6852 | /* Match up the physical address of the segment with the | |
6853 | LMA address of the output section. */ | |
bc67d8a6 | 6854 | if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr) |
5e8d7549 | 6855 | || IS_COREFILE_NOTE (segment, section) |
0067a569 AM |
6856 | || (bed->want_p_paddr_set_to_zero |
6857 | && IS_CONTAINED_BY_VMA (output_section, segment))) | |
252b5132 | 6858 | { |
0067a569 AM |
6859 | if (first_matching_lma || output_section->lma < matching_lma) |
6860 | { | |
6861 | matching_lma = output_section->lma; | |
6862 | first_matching_lma = FALSE; | |
6863 | } | |
252b5132 RH |
6864 | |
6865 | /* We assume that if the section fits within the segment | |
bc67d8a6 | 6866 | then it does not overlap any other section within that |
252b5132 | 6867 | segment. */ |
0067a569 AM |
6868 | map->sections[isec++] = output_section; |
6869 | } | |
6870 | else if (first_suggested_lma) | |
6871 | { | |
6872 | suggested_lma = output_section->lma; | |
6873 | first_suggested_lma = FALSE; | |
252b5132 | 6874 | } |
147d51c2 L |
6875 | |
6876 | if (j == section_count) | |
6877 | break; | |
252b5132 RH |
6878 | } |
6879 | } | |
6880 | ||
bc67d8a6 | 6881 | BFD_ASSERT (j == section_count); |
252b5132 RH |
6882 | |
6883 | /* Step Two: Adjust the physical address of the current segment, | |
6884 | if necessary. */ | |
bc67d8a6 | 6885 | if (isec == section_count) |
252b5132 RH |
6886 | { |
6887 | /* All of the sections fitted within the segment as currently | |
6888 | specified. This is the default case. Add the segment to | |
6889 | the list of built segments and carry on to process the next | |
6890 | program header in the input BFD. */ | |
bc67d8a6 | 6891 | map->count = section_count; |
c044fabd KH |
6892 | *pointer_to_map = map; |
6893 | pointer_to_map = &map->next; | |
08a40648 | 6894 | |
5c44b38e AM |
6895 | if (p_paddr_valid |
6896 | && !bed->want_p_paddr_set_to_zero | |
147d51c2 | 6897 | && matching_lma != map->p_paddr |
5c44b38e AM |
6898 | && !map->includes_filehdr |
6899 | && !map->includes_phdrs) | |
3271a814 NS |
6900 | /* There is some padding before the first section in the |
6901 | segment. So, we must account for that in the output | |
6902 | segment's vma. */ | |
6903 | map->p_vaddr_offset = matching_lma - map->p_paddr; | |
08a40648 | 6904 | |
252b5132 RH |
6905 | free (sections); |
6906 | continue; | |
6907 | } | |
252b5132 RH |
6908 | else |
6909 | { | |
0067a569 | 6910 | if (!first_matching_lma) |
72730e0c AM |
6911 | { |
6912 | /* At least one section fits inside the current segment. | |
6913 | Keep it, but modify its physical address to match the | |
6914 | LMA of the first section that fitted. */ | |
bc67d8a6 | 6915 | map->p_paddr = matching_lma; |
72730e0c AM |
6916 | } |
6917 | else | |
6918 | { | |
6919 | /* None of the sections fitted inside the current segment. | |
6920 | Change the current segment's physical address to match | |
6921 | the LMA of the first section. */ | |
bc67d8a6 | 6922 | map->p_paddr = suggested_lma; |
72730e0c AM |
6923 | } |
6924 | ||
bc67d8a6 NC |
6925 | /* Offset the segment physical address from the lma |
6926 | to allow for space taken up by elf headers. */ | |
6927 | if (map->includes_filehdr) | |
010c8431 AM |
6928 | { |
6929 | if (map->p_paddr >= iehdr->e_ehsize) | |
6930 | map->p_paddr -= iehdr->e_ehsize; | |
6931 | else | |
6932 | { | |
6933 | map->includes_filehdr = FALSE; | |
6934 | map->includes_phdrs = FALSE; | |
6935 | } | |
6936 | } | |
252b5132 | 6937 | |
bc67d8a6 NC |
6938 | if (map->includes_phdrs) |
6939 | { | |
010c8431 AM |
6940 | if (map->p_paddr >= iehdr->e_phnum * iehdr->e_phentsize) |
6941 | { | |
6942 | map->p_paddr -= iehdr->e_phnum * iehdr->e_phentsize; | |
6943 | ||
6944 | /* iehdr->e_phnum is just an estimate of the number | |
6945 | of program headers that we will need. Make a note | |
6946 | here of the number we used and the segment we chose | |
6947 | to hold these headers, so that we can adjust the | |
6948 | offset when we know the correct value. */ | |
6949 | phdr_adjust_num = iehdr->e_phnum; | |
6950 | phdr_adjust_seg = map; | |
6951 | } | |
6952 | else | |
6953 | map->includes_phdrs = FALSE; | |
bc67d8a6 | 6954 | } |
252b5132 RH |
6955 | } |
6956 | ||
6957 | /* Step Three: Loop over the sections again, this time assigning | |
caf47ea6 | 6958 | those that fit to the current segment and removing them from the |
252b5132 RH |
6959 | sections array; but making sure not to leave large gaps. Once all |
6960 | possible sections have been assigned to the current segment it is | |
6961 | added to the list of built segments and if sections still remain | |
6962 | to be assigned, a new segment is constructed before repeating | |
6963 | the loop. */ | |
6964 | isec = 0; | |
6965 | do | |
6966 | { | |
bc67d8a6 | 6967 | map->count = 0; |
252b5132 | 6968 | suggested_lma = 0; |
0067a569 | 6969 | first_suggested_lma = TRUE; |
252b5132 RH |
6970 | |
6971 | /* Fill the current segment with sections that fit. */ | |
bc67d8a6 | 6972 | for (j = 0; j < section_count; j++) |
252b5132 | 6973 | { |
bc67d8a6 | 6974 | section = sections[j]; |
252b5132 | 6975 | |
bc67d8a6 | 6976 | if (section == NULL) |
252b5132 RH |
6977 | continue; |
6978 | ||
bc67d8a6 | 6979 | output_section = section->output_section; |
252b5132 | 6980 | |
bc67d8a6 | 6981 | BFD_ASSERT (output_section != NULL); |
c044fabd | 6982 | |
bc67d8a6 NC |
6983 | if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr) |
6984 | || IS_COREFILE_NOTE (segment, section)) | |
252b5132 | 6985 | { |
bc67d8a6 | 6986 | if (map->count == 0) |
252b5132 RH |
6987 | { |
6988 | /* If the first section in a segment does not start at | |
bc67d8a6 NC |
6989 | the beginning of the segment, then something is |
6990 | wrong. */ | |
0067a569 AM |
6991 | if (output_section->lma |
6992 | != (map->p_paddr | |
6993 | + (map->includes_filehdr ? iehdr->e_ehsize : 0) | |
6994 | + (map->includes_phdrs | |
6995 | ? iehdr->e_phnum * iehdr->e_phentsize | |
6996 | : 0))) | |
252b5132 RH |
6997 | abort (); |
6998 | } | |
6999 | else | |
7000 | { | |
0067a569 | 7001 | asection *prev_sec; |
252b5132 | 7002 | |
bc67d8a6 | 7003 | prev_sec = map->sections[map->count - 1]; |
252b5132 RH |
7004 | |
7005 | /* If the gap between the end of the previous section | |
bc67d8a6 NC |
7006 | and the start of this section is more than |
7007 | maxpagesize then we need to start a new segment. */ | |
eea6121a | 7008 | if ((BFD_ALIGN (prev_sec->lma + prev_sec->size, |
079e9a2f | 7009 | maxpagesize) |
caf47ea6 | 7010 | < BFD_ALIGN (output_section->lma, maxpagesize)) |
0067a569 | 7011 | || (prev_sec->lma + prev_sec->size |
079e9a2f | 7012 | > output_section->lma)) |
252b5132 | 7013 | { |
0067a569 AM |
7014 | if (first_suggested_lma) |
7015 | { | |
7016 | suggested_lma = output_section->lma; | |
7017 | first_suggested_lma = FALSE; | |
7018 | } | |
252b5132 RH |
7019 | |
7020 | continue; | |
7021 | } | |
7022 | } | |
7023 | ||
bc67d8a6 | 7024 | map->sections[map->count++] = output_section; |
252b5132 RH |
7025 | ++isec; |
7026 | sections[j] = NULL; | |
b34976b6 | 7027 | section->segment_mark = TRUE; |
252b5132 | 7028 | } |
0067a569 AM |
7029 | else if (first_suggested_lma) |
7030 | { | |
7031 | suggested_lma = output_section->lma; | |
7032 | first_suggested_lma = FALSE; | |
7033 | } | |
252b5132 RH |
7034 | } |
7035 | ||
bc67d8a6 | 7036 | BFD_ASSERT (map->count > 0); |
252b5132 RH |
7037 | |
7038 | /* Add the current segment to the list of built segments. */ | |
c044fabd KH |
7039 | *pointer_to_map = map; |
7040 | pointer_to_map = &map->next; | |
252b5132 | 7041 | |
bc67d8a6 | 7042 | if (isec < section_count) |
252b5132 RH |
7043 | { |
7044 | /* We still have not allocated all of the sections to | |
7045 | segments. Create a new segment here, initialise it | |
7046 | and carry on looping. */ | |
dc810e39 AM |
7047 | amt = sizeof (struct elf_segment_map); |
7048 | amt += ((bfd_size_type) section_count - 1) * sizeof (asection *); | |
5964fc3a | 7049 | map = (struct elf_segment_map *) bfd_zalloc (obfd, amt); |
bc67d8a6 | 7050 | if (map == NULL) |
5ed6aba4 NC |
7051 | { |
7052 | free (sections); | |
7053 | return FALSE; | |
7054 | } | |
252b5132 RH |
7055 | |
7056 | /* Initialise the fields of the segment map. Set the physical | |
7057 | physical address to the LMA of the first section that has | |
7058 | not yet been assigned. */ | |
0067a569 AM |
7059 | map->next = NULL; |
7060 | map->p_type = segment->p_type; | |
7061 | map->p_flags = segment->p_flags; | |
7062 | map->p_flags_valid = 1; | |
7063 | map->p_paddr = suggested_lma; | |
5c44b38e | 7064 | map->p_paddr_valid = p_paddr_valid; |
bc67d8a6 | 7065 | map->includes_filehdr = 0; |
0067a569 | 7066 | map->includes_phdrs = 0; |
252b5132 RH |
7067 | } |
7068 | } | |
bc67d8a6 | 7069 | while (isec < section_count); |
252b5132 RH |
7070 | |
7071 | free (sections); | |
7072 | } | |
7073 | ||
12bd6957 | 7074 | elf_seg_map (obfd) = map_first; |
bc67d8a6 NC |
7075 | |
7076 | /* If we had to estimate the number of program headers that were | |
9ad5cbcf | 7077 | going to be needed, then check our estimate now and adjust |
bc67d8a6 NC |
7078 | the offset if necessary. */ |
7079 | if (phdr_adjust_seg != NULL) | |
7080 | { | |
7081 | unsigned int count; | |
c044fabd | 7082 | |
bc67d8a6 | 7083 | for (count = 0, map = map_first; map != NULL; map = map->next) |
c044fabd | 7084 | count++; |
252b5132 | 7085 | |
bc67d8a6 NC |
7086 | if (count > phdr_adjust_num) |
7087 | phdr_adjust_seg->p_paddr | |
7088 | -= (count - phdr_adjust_num) * iehdr->e_phentsize; | |
7089 | } | |
c044fabd | 7090 | |
bc67d8a6 | 7091 | #undef SEGMENT_END |
eecdbe52 | 7092 | #undef SECTION_SIZE |
bc67d8a6 NC |
7093 | #undef IS_CONTAINED_BY_VMA |
7094 | #undef IS_CONTAINED_BY_LMA | |
0efc80c8 | 7095 | #undef IS_NOTE |
252b5132 | 7096 | #undef IS_COREFILE_NOTE |
bc67d8a6 | 7097 | #undef IS_SOLARIS_PT_INTERP |
9f17e2a6 | 7098 | #undef IS_SECTION_IN_INPUT_SEGMENT |
bc67d8a6 NC |
7099 | #undef INCLUDE_SECTION_IN_SEGMENT |
7100 | #undef SEGMENT_AFTER_SEGMENT | |
7101 | #undef SEGMENT_OVERLAPS | |
b34976b6 | 7102 | return TRUE; |
252b5132 RH |
7103 | } |
7104 | ||
84d1d650 L |
7105 | /* Copy ELF program header information. */ |
7106 | ||
7107 | static bfd_boolean | |
7108 | copy_elf_program_header (bfd *ibfd, bfd *obfd) | |
7109 | { | |
7110 | Elf_Internal_Ehdr *iehdr; | |
7111 | struct elf_segment_map *map; | |
7112 | struct elf_segment_map *map_first; | |
7113 | struct elf_segment_map **pointer_to_map; | |
7114 | Elf_Internal_Phdr *segment; | |
7115 | unsigned int i; | |
7116 | unsigned int num_segments; | |
7117 | bfd_boolean phdr_included = FALSE; | |
88967714 | 7118 | bfd_boolean p_paddr_valid; |
84d1d650 L |
7119 | |
7120 | iehdr = elf_elfheader (ibfd); | |
7121 | ||
7122 | map_first = NULL; | |
7123 | pointer_to_map = &map_first; | |
7124 | ||
88967714 AM |
7125 | /* If all the segment p_paddr fields are zero, don't set |
7126 | map->p_paddr_valid. */ | |
7127 | p_paddr_valid = FALSE; | |
84d1d650 | 7128 | num_segments = elf_elfheader (ibfd)->e_phnum; |
88967714 AM |
7129 | for (i = 0, segment = elf_tdata (ibfd)->phdr; |
7130 | i < num_segments; | |
7131 | i++, segment++) | |
7132 | if (segment->p_paddr != 0) | |
7133 | { | |
7134 | p_paddr_valid = TRUE; | |
7135 | break; | |
7136 | } | |
7137 | ||
84d1d650 L |
7138 | for (i = 0, segment = elf_tdata (ibfd)->phdr; |
7139 | i < num_segments; | |
7140 | i++, segment++) | |
7141 | { | |
7142 | asection *section; | |
7143 | unsigned int section_count; | |
7144 | bfd_size_type amt; | |
7145 | Elf_Internal_Shdr *this_hdr; | |
53020534 | 7146 | asection *first_section = NULL; |
a76e6f2f | 7147 | asection *lowest_section; |
84d1d650 | 7148 | |
84d1d650 L |
7149 | /* Compute how many sections are in this segment. */ |
7150 | for (section = ibfd->sections, section_count = 0; | |
7151 | section != NULL; | |
7152 | section = section->next) | |
7153 | { | |
7154 | this_hdr = &(elf_section_data(section)->this_hdr); | |
f4638467 | 7155 | if (ELF_SECTION_IN_SEGMENT (this_hdr, segment)) |
3271a814 | 7156 | { |
a76e6f2f AM |
7157 | if (first_section == NULL) |
7158 | first_section = section; | |
3271a814 NS |
7159 | section_count++; |
7160 | } | |
84d1d650 L |
7161 | } |
7162 | ||
7163 | /* Allocate a segment map big enough to contain | |
7164 | all of the sections we have selected. */ | |
7165 | amt = sizeof (struct elf_segment_map); | |
7166 | if (section_count != 0) | |
7167 | amt += ((bfd_size_type) section_count - 1) * sizeof (asection *); | |
a50b1753 | 7168 | map = (struct elf_segment_map *) bfd_zalloc (obfd, amt); |
84d1d650 L |
7169 | if (map == NULL) |
7170 | return FALSE; | |
7171 | ||
7172 | /* Initialize the fields of the output segment map with the | |
7173 | input segment. */ | |
7174 | map->next = NULL; | |
7175 | map->p_type = segment->p_type; | |
7176 | map->p_flags = segment->p_flags; | |
7177 | map->p_flags_valid = 1; | |
7178 | map->p_paddr = segment->p_paddr; | |
88967714 | 7179 | map->p_paddr_valid = p_paddr_valid; |
3f570048 AM |
7180 | map->p_align = segment->p_align; |
7181 | map->p_align_valid = 1; | |
3271a814 | 7182 | map->p_vaddr_offset = 0; |
84d1d650 | 7183 | |
04c3a755 NS |
7184 | if (map->p_type == PT_GNU_RELRO |
7185 | || map->p_type == PT_GNU_STACK) | |
b10a8ae0 L |
7186 | { |
7187 | /* The PT_GNU_RELRO segment may contain the first a few | |
7188 | bytes in the .got.plt section even if the whole .got.plt | |
7189 | section isn't in the PT_GNU_RELRO segment. We won't | |
04c3a755 NS |
7190 | change the size of the PT_GNU_RELRO segment. |
7191 | Similarly, PT_GNU_STACK size is significant on uclinux | |
7192 | systems. */ | |
9433b9b1 | 7193 | map->p_size = segment->p_memsz; |
b10a8ae0 L |
7194 | map->p_size_valid = 1; |
7195 | } | |
7196 | ||
84d1d650 L |
7197 | /* Determine if this segment contains the ELF file header |
7198 | and if it contains the program headers themselves. */ | |
7199 | map->includes_filehdr = (segment->p_offset == 0 | |
7200 | && segment->p_filesz >= iehdr->e_ehsize); | |
7201 | ||
7202 | map->includes_phdrs = 0; | |
7203 | if (! phdr_included || segment->p_type != PT_LOAD) | |
7204 | { | |
7205 | map->includes_phdrs = | |
7206 | (segment->p_offset <= (bfd_vma) iehdr->e_phoff | |
7207 | && (segment->p_offset + segment->p_filesz | |
7208 | >= ((bfd_vma) iehdr->e_phoff | |
7209 | + iehdr->e_phnum * iehdr->e_phentsize))); | |
7210 | ||
7211 | if (segment->p_type == PT_LOAD && map->includes_phdrs) | |
7212 | phdr_included = TRUE; | |
7213 | } | |
7214 | ||
bbefd0a9 | 7215 | lowest_section = NULL; |
84d1d650 L |
7216 | if (section_count != 0) |
7217 | { | |
7218 | unsigned int isec = 0; | |
7219 | ||
53020534 | 7220 | for (section = first_section; |
84d1d650 L |
7221 | section != NULL; |
7222 | section = section->next) | |
7223 | { | |
7224 | this_hdr = &(elf_section_data(section)->this_hdr); | |
f4638467 | 7225 | if (ELF_SECTION_IN_SEGMENT (this_hdr, segment)) |
53020534 L |
7226 | { |
7227 | map->sections[isec++] = section->output_section; | |
a76e6f2f AM |
7228 | if ((section->flags & SEC_ALLOC) != 0) |
7229 | { | |
7230 | bfd_vma seg_off; | |
7231 | ||
bbefd0a9 AM |
7232 | if (lowest_section == NULL |
7233 | || section->lma < lowest_section->lma) | |
fb8a5684 AM |
7234 | lowest_section = section; |
7235 | ||
a76e6f2f AM |
7236 | /* Section lmas are set up from PT_LOAD header |
7237 | p_paddr in _bfd_elf_make_section_from_shdr. | |
7238 | If this header has a p_paddr that disagrees | |
7239 | with the section lma, flag the p_paddr as | |
7240 | invalid. */ | |
7241 | if ((section->flags & SEC_LOAD) != 0) | |
7242 | seg_off = this_hdr->sh_offset - segment->p_offset; | |
7243 | else | |
7244 | seg_off = this_hdr->sh_addr - segment->p_vaddr; | |
7245 | if (section->lma - segment->p_paddr != seg_off) | |
7246 | map->p_paddr_valid = FALSE; | |
7247 | } | |
53020534 L |
7248 | if (isec == section_count) |
7249 | break; | |
7250 | } | |
84d1d650 L |
7251 | } |
7252 | } | |
7253 | ||
a76e6f2f AM |
7254 | if (map->includes_filehdr && lowest_section != NULL) |
7255 | /* We need to keep the space used by the headers fixed. */ | |
7256 | map->header_size = lowest_section->vma - segment->p_vaddr; | |
d324f6d6 | 7257 | |
a76e6f2f AM |
7258 | if (!map->includes_phdrs |
7259 | && !map->includes_filehdr | |
7260 | && map->p_paddr_valid) | |
7261 | /* There is some other padding before the first section. */ | |
7262 | map->p_vaddr_offset = ((lowest_section ? lowest_section->lma : 0) | |
7263 | - segment->p_paddr); | |
7264 | ||
84d1d650 L |
7265 | map->count = section_count; |
7266 | *pointer_to_map = map; | |
7267 | pointer_to_map = &map->next; | |
7268 | } | |
7269 | ||
12bd6957 | 7270 | elf_seg_map (obfd) = map_first; |
84d1d650 L |
7271 | return TRUE; |
7272 | } | |
7273 | ||
7274 | /* Copy private BFD data. This copies or rewrites ELF program header | |
7275 | information. */ | |
7276 | ||
7277 | static bfd_boolean | |
7278 | copy_private_bfd_data (bfd *ibfd, bfd *obfd) | |
7279 | { | |
84d1d650 L |
7280 | if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour |
7281 | || bfd_get_flavour (obfd) != bfd_target_elf_flavour) | |
7282 | return TRUE; | |
7283 | ||
7284 | if (elf_tdata (ibfd)->phdr == NULL) | |
7285 | return TRUE; | |
7286 | ||
7287 | if (ibfd->xvec == obfd->xvec) | |
7288 | { | |
cb3ff1e5 NC |
7289 | /* Check to see if any sections in the input BFD |
7290 | covered by ELF program header have changed. */ | |
d55ce4e2 | 7291 | Elf_Internal_Phdr *segment; |
84d1d650 L |
7292 | asection *section, *osec; |
7293 | unsigned int i, num_segments; | |
7294 | Elf_Internal_Shdr *this_hdr; | |
147d51c2 L |
7295 | const struct elf_backend_data *bed; |
7296 | ||
7297 | bed = get_elf_backend_data (ibfd); | |
7298 | ||
7299 | /* Regenerate the segment map if p_paddr is set to 0. */ | |
7300 | if (bed->want_p_paddr_set_to_zero) | |
7301 | goto rewrite; | |
84d1d650 L |
7302 | |
7303 | /* Initialize the segment mark field. */ | |
7304 | for (section = obfd->sections; section != NULL; | |
7305 | section = section->next) | |
7306 | section->segment_mark = FALSE; | |
7307 | ||
7308 | num_segments = elf_elfheader (ibfd)->e_phnum; | |
7309 | for (i = 0, segment = elf_tdata (ibfd)->phdr; | |
7310 | i < num_segments; | |
7311 | i++, segment++) | |
7312 | { | |
5f6999aa NC |
7313 | /* PR binutils/3535. The Solaris linker always sets the p_paddr |
7314 | and p_memsz fields of special segments (DYNAMIC, INTERP) to 0 | |
7315 | which severly confuses things, so always regenerate the segment | |
7316 | map in this case. */ | |
7317 | if (segment->p_paddr == 0 | |
7318 | && segment->p_memsz == 0 | |
7319 | && (segment->p_type == PT_INTERP || segment->p_type == PT_DYNAMIC)) | |
cb3ff1e5 | 7320 | goto rewrite; |
5f6999aa | 7321 | |
84d1d650 L |
7322 | for (section = ibfd->sections; |
7323 | section != NULL; section = section->next) | |
7324 | { | |
7325 | /* We mark the output section so that we know it comes | |
7326 | from the input BFD. */ | |
7327 | osec = section->output_section; | |
7328 | if (osec) | |
7329 | osec->segment_mark = TRUE; | |
7330 | ||
7331 | /* Check if this section is covered by the segment. */ | |
7332 | this_hdr = &(elf_section_data(section)->this_hdr); | |
f4638467 | 7333 | if (ELF_SECTION_IN_SEGMENT (this_hdr, segment)) |
84d1d650 L |
7334 | { |
7335 | /* FIXME: Check if its output section is changed or | |
7336 | removed. What else do we need to check? */ | |
7337 | if (osec == NULL | |
7338 | || section->flags != osec->flags | |
7339 | || section->lma != osec->lma | |
7340 | || section->vma != osec->vma | |
7341 | || section->size != osec->size | |
7342 | || section->rawsize != osec->rawsize | |
7343 | || section->alignment_power != osec->alignment_power) | |
7344 | goto rewrite; | |
7345 | } | |
7346 | } | |
7347 | } | |
7348 | ||
cb3ff1e5 | 7349 | /* Check to see if any output section do not come from the |
84d1d650 L |
7350 | input BFD. */ |
7351 | for (section = obfd->sections; section != NULL; | |
7352 | section = section->next) | |
7353 | { | |
535b785f | 7354 | if (!section->segment_mark) |
84d1d650 L |
7355 | goto rewrite; |
7356 | else | |
7357 | section->segment_mark = FALSE; | |
7358 | } | |
7359 | ||
7360 | return copy_elf_program_header (ibfd, obfd); | |
7361 | } | |
7362 | ||
7363 | rewrite: | |
f1d85785 L |
7364 | if (ibfd->xvec == obfd->xvec) |
7365 | { | |
7366 | /* When rewriting program header, set the output maxpagesize to | |
7367 | the maximum alignment of input PT_LOAD segments. */ | |
7368 | Elf_Internal_Phdr *segment; | |
7369 | unsigned int i; | |
7370 | unsigned int num_segments = elf_elfheader (ibfd)->e_phnum; | |
7371 | bfd_vma maxpagesize = 0; | |
7372 | ||
7373 | for (i = 0, segment = elf_tdata (ibfd)->phdr; | |
7374 | i < num_segments; | |
7375 | i++, segment++) | |
7376 | if (segment->p_type == PT_LOAD | |
7377 | && maxpagesize < segment->p_align) | |
c86934ce NC |
7378 | { |
7379 | /* PR 17512: file: f17299af. */ | |
7380 | if (segment->p_align > (bfd_vma) 1 << ((sizeof (bfd_vma) * 8) - 2)) | |
695344c0 | 7381 | /* xgettext:c-format */ |
76cfced5 AM |
7382 | _bfd_error_handler (_("%B: warning: segment alignment of %#Lx" |
7383 | " is too large"), | |
7384 | ibfd, segment->p_align); | |
c86934ce NC |
7385 | else |
7386 | maxpagesize = segment->p_align; | |
7387 | } | |
f1d85785 L |
7388 | |
7389 | if (maxpagesize != get_elf_backend_data (obfd)->maxpagesize) | |
7390 | bfd_emul_set_maxpagesize (bfd_get_target (obfd), maxpagesize); | |
7391 | } | |
7392 | ||
84d1d650 L |
7393 | return rewrite_elf_program_header (ibfd, obfd); |
7394 | } | |
7395 | ||
ccd2ec6a L |
7396 | /* Initialize private output section information from input section. */ |
7397 | ||
7398 | bfd_boolean | |
7399 | _bfd_elf_init_private_section_data (bfd *ibfd, | |
7400 | asection *isec, | |
7401 | bfd *obfd, | |
7402 | asection *osec, | |
7403 | struct bfd_link_info *link_info) | |
7404 | ||
7405 | { | |
7406 | Elf_Internal_Shdr *ihdr, *ohdr; | |
0e1862bb L |
7407 | bfd_boolean final_link = (link_info != NULL |
7408 | && !bfd_link_relocatable (link_info)); | |
ccd2ec6a L |
7409 | |
7410 | if (ibfd->xvec->flavour != bfd_target_elf_flavour | |
7411 | || obfd->xvec->flavour != bfd_target_elf_flavour) | |
7412 | return TRUE; | |
7413 | ||
ba85c43e NC |
7414 | BFD_ASSERT (elf_section_data (osec) != NULL); |
7415 | ||
dfa7b0b8 AM |
7416 | /* For objcopy and relocatable link, don't copy the output ELF |
7417 | section type from input if the output BFD section flags have been | |
7418 | set to something different. For a final link allow some flags | |
7419 | that the linker clears to differ. */ | |
42bb2e33 | 7420 | if (elf_section_type (osec) == SHT_NULL |
dfa7b0b8 AM |
7421 | && (osec->flags == isec->flags |
7422 | || (final_link | |
7423 | && ((osec->flags ^ isec->flags) | |
0814be7d | 7424 | & ~(SEC_LINK_ONCE | SEC_LINK_DUPLICATES | SEC_RELOC)) == 0))) |
42bb2e33 | 7425 | elf_section_type (osec) = elf_section_type (isec); |
d270463e L |
7426 | |
7427 | /* FIXME: Is this correct for all OS/PROC specific flags? */ | |
7428 | elf_section_flags (osec) |= (elf_section_flags (isec) | |
7429 | & (SHF_MASKOS | SHF_MASKPROC)); | |
ccd2ec6a | 7430 | |
a91e1603 L |
7431 | /* Copy sh_info from input for mbind section. */ |
7432 | if (elf_section_flags (isec) & SHF_GNU_MBIND) | |
7433 | elf_section_data (osec)->this_hdr.sh_info | |
7434 | = elf_section_data (isec)->this_hdr.sh_info; | |
7435 | ||
ccd2ec6a L |
7436 | /* Set things up for objcopy and relocatable link. The output |
7437 | SHT_GROUP section will have its elf_next_in_group pointing back | |
7438 | to the input group members. Ignore linker created group section. | |
7439 | See elfNN_ia64_object_p in elfxx-ia64.c. */ | |
7bdf4127 AB |
7440 | if ((link_info == NULL |
7441 | || !link_info->resolve_section_groups) | |
7442 | && (elf_sec_group (isec) == NULL | |
7443 | || (elf_sec_group (isec)->flags & SEC_LINKER_CREATED) == 0)) | |
ccd2ec6a | 7444 | { |
7bdf4127 AB |
7445 | if (elf_section_flags (isec) & SHF_GROUP) |
7446 | elf_section_flags (osec) |= SHF_GROUP; | |
7447 | elf_next_in_group (osec) = elf_next_in_group (isec); | |
7448 | elf_section_data (osec)->group = elf_section_data (isec)->group; | |
ccd2ec6a L |
7449 | } |
7450 | ||
7bdf4127 AB |
7451 | /* If not decompress, preserve SHF_COMPRESSED. */ |
7452 | if (!final_link && (ibfd->flags & BFD_DECOMPRESS) == 0) | |
7453 | elf_section_flags (osec) |= (elf_section_flags (isec) | |
7454 | & SHF_COMPRESSED); | |
7455 | ||
ccd2ec6a L |
7456 | ihdr = &elf_section_data (isec)->this_hdr; |
7457 | ||
7458 | /* We need to handle elf_linked_to_section for SHF_LINK_ORDER. We | |
7459 | don't use the output section of the linked-to section since it | |
7460 | may be NULL at this point. */ | |
7461 | if ((ihdr->sh_flags & SHF_LINK_ORDER) != 0) | |
7462 | { | |
7463 | ohdr = &elf_section_data (osec)->this_hdr; | |
7464 | ohdr->sh_flags |= SHF_LINK_ORDER; | |
7465 | elf_linked_to_section (osec) = elf_linked_to_section (isec); | |
7466 | } | |
7467 | ||
7468 | osec->use_rela_p = isec->use_rela_p; | |
7469 | ||
7470 | return TRUE; | |
7471 | } | |
7472 | ||
252b5132 RH |
7473 | /* Copy private section information. This copies over the entsize |
7474 | field, and sometimes the info field. */ | |
7475 | ||
b34976b6 | 7476 | bfd_boolean |
217aa764 AM |
7477 | _bfd_elf_copy_private_section_data (bfd *ibfd, |
7478 | asection *isec, | |
7479 | bfd *obfd, | |
7480 | asection *osec) | |
252b5132 RH |
7481 | { |
7482 | Elf_Internal_Shdr *ihdr, *ohdr; | |
7483 | ||
7484 | if (ibfd->xvec->flavour != bfd_target_elf_flavour | |
7485 | || obfd->xvec->flavour != bfd_target_elf_flavour) | |
b34976b6 | 7486 | return TRUE; |
252b5132 | 7487 | |
252b5132 RH |
7488 | ihdr = &elf_section_data (isec)->this_hdr; |
7489 | ohdr = &elf_section_data (osec)->this_hdr; | |
7490 | ||
7491 | ohdr->sh_entsize = ihdr->sh_entsize; | |
7492 | ||
7493 | if (ihdr->sh_type == SHT_SYMTAB | |
7494 | || ihdr->sh_type == SHT_DYNSYM | |
7495 | || ihdr->sh_type == SHT_GNU_verneed | |
7496 | || ihdr->sh_type == SHT_GNU_verdef) | |
7497 | ohdr->sh_info = ihdr->sh_info; | |
7498 | ||
ccd2ec6a L |
7499 | return _bfd_elf_init_private_section_data (ibfd, isec, obfd, osec, |
7500 | NULL); | |
252b5132 RH |
7501 | } |
7502 | ||
d0bf826b AM |
7503 | /* Look at all the SHT_GROUP sections in IBFD, making any adjustments |
7504 | necessary if we are removing either the SHT_GROUP section or any of | |
7505 | the group member sections. DISCARDED is the value that a section's | |
7506 | output_section has if the section will be discarded, NULL when this | |
7507 | function is called from objcopy, bfd_abs_section_ptr when called | |
7508 | from the linker. */ | |
80fccad2 BW |
7509 | |
7510 | bfd_boolean | |
d0bf826b | 7511 | _bfd_elf_fixup_group_sections (bfd *ibfd, asection *discarded) |
80fccad2 | 7512 | { |
30288845 AM |
7513 | asection *isec; |
7514 | ||
30288845 | 7515 | for (isec = ibfd->sections; isec != NULL; isec = isec->next) |
415f38a6 | 7516 | if (elf_section_type (isec) == SHT_GROUP) |
30288845 AM |
7517 | { |
7518 | asection *first = elf_next_in_group (isec); | |
7519 | asection *s = first; | |
d0bf826b AM |
7520 | bfd_size_type removed = 0; |
7521 | ||
30288845 AM |
7522 | while (s != NULL) |
7523 | { | |
415f38a6 AM |
7524 | /* If this member section is being output but the |
7525 | SHT_GROUP section is not, then clear the group info | |
7526 | set up by _bfd_elf_copy_private_section_data. */ | |
d0bf826b AM |
7527 | if (s->output_section != discarded |
7528 | && isec->output_section == discarded) | |
30288845 AM |
7529 | { |
7530 | elf_section_flags (s->output_section) &= ~SHF_GROUP; | |
7531 | elf_group_name (s->output_section) = NULL; | |
7532 | } | |
415f38a6 AM |
7533 | /* Conversely, if the member section is not being output |
7534 | but the SHT_GROUP section is, then adjust its size. */ | |
d0bf826b AM |
7535 | else if (s->output_section == discarded |
7536 | && isec->output_section != discarded) | |
7537 | removed += 4; | |
30288845 AM |
7538 | s = elf_next_in_group (s); |
7539 | if (s == first) | |
7540 | break; | |
7541 | } | |
d0bf826b AM |
7542 | if (removed != 0) |
7543 | { | |
7544 | if (discarded != NULL) | |
7545 | { | |
7546 | /* If we've been called for ld -r, then we need to | |
7547 | adjust the input section size. This function may | |
7548 | be called multiple times, so save the original | |
7549 | size. */ | |
7550 | if (isec->rawsize == 0) | |
7551 | isec->rawsize = isec->size; | |
7552 | isec->size = isec->rawsize - removed; | |
7553 | } | |
7554 | else | |
7555 | { | |
7556 | /* Adjust the output section size when called from | |
7557 | objcopy. */ | |
7558 | isec->output_section->size -= removed; | |
7559 | } | |
7560 | } | |
30288845 AM |
7561 | } |
7562 | ||
80fccad2 BW |
7563 | return TRUE; |
7564 | } | |
7565 | ||
d0bf826b AM |
7566 | /* Copy private header information. */ |
7567 | ||
7568 | bfd_boolean | |
7569 | _bfd_elf_copy_private_header_data (bfd *ibfd, bfd *obfd) | |
7570 | { | |
7571 | if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour | |
7572 | || bfd_get_flavour (obfd) != bfd_target_elf_flavour) | |
7573 | return TRUE; | |
7574 | ||
7575 | /* Copy over private BFD data if it has not already been copied. | |
7576 | This must be done here, rather than in the copy_private_bfd_data | |
7577 | entry point, because the latter is called after the section | |
7578 | contents have been set, which means that the program headers have | |
7579 | already been worked out. */ | |
12bd6957 | 7580 | if (elf_seg_map (obfd) == NULL && elf_tdata (ibfd)->phdr != NULL) |
d0bf826b AM |
7581 | { |
7582 | if (! copy_private_bfd_data (ibfd, obfd)) | |
7583 | return FALSE; | |
7584 | } | |
7585 | ||
7586 | return _bfd_elf_fixup_group_sections (ibfd, NULL); | |
7587 | } | |
7588 | ||
252b5132 RH |
7589 | /* Copy private symbol information. If this symbol is in a section |
7590 | which we did not map into a BFD section, try to map the section | |
7591 | index correctly. We use special macro definitions for the mapped | |
7592 | section indices; these definitions are interpreted by the | |
7593 | swap_out_syms function. */ | |
7594 | ||
9ad5cbcf AM |
7595 | #define MAP_ONESYMTAB (SHN_HIOS + 1) |
7596 | #define MAP_DYNSYMTAB (SHN_HIOS + 2) | |
7597 | #define MAP_STRTAB (SHN_HIOS + 3) | |
7598 | #define MAP_SHSTRTAB (SHN_HIOS + 4) | |
7599 | #define MAP_SYM_SHNDX (SHN_HIOS + 5) | |
252b5132 | 7600 | |
b34976b6 | 7601 | bfd_boolean |
217aa764 AM |
7602 | _bfd_elf_copy_private_symbol_data (bfd *ibfd, |
7603 | asymbol *isymarg, | |
7604 | bfd *obfd, | |
7605 | asymbol *osymarg) | |
252b5132 RH |
7606 | { |
7607 | elf_symbol_type *isym, *osym; | |
7608 | ||
7609 | if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour | |
7610 | || bfd_get_flavour (obfd) != bfd_target_elf_flavour) | |
b34976b6 | 7611 | return TRUE; |
252b5132 RH |
7612 | |
7613 | isym = elf_symbol_from (ibfd, isymarg); | |
7614 | osym = elf_symbol_from (obfd, osymarg); | |
7615 | ||
7616 | if (isym != NULL | |
8424d8f5 | 7617 | && isym->internal_elf_sym.st_shndx != 0 |
252b5132 RH |
7618 | && osym != NULL |
7619 | && bfd_is_abs_section (isym->symbol.section)) | |
7620 | { | |
7621 | unsigned int shndx; | |
7622 | ||
7623 | shndx = isym->internal_elf_sym.st_shndx; | |
7624 | if (shndx == elf_onesymtab (ibfd)) | |
7625 | shndx = MAP_ONESYMTAB; | |
7626 | else if (shndx == elf_dynsymtab (ibfd)) | |
7627 | shndx = MAP_DYNSYMTAB; | |
12bd6957 | 7628 | else if (shndx == elf_strtab_sec (ibfd)) |
252b5132 | 7629 | shndx = MAP_STRTAB; |
12bd6957 | 7630 | else if (shndx == elf_shstrtab_sec (ibfd)) |
252b5132 | 7631 | shndx = MAP_SHSTRTAB; |
6a40cf0c | 7632 | else if (find_section_in_list (shndx, elf_symtab_shndx_list (ibfd))) |
9ad5cbcf | 7633 | shndx = MAP_SYM_SHNDX; |
252b5132 RH |
7634 | osym->internal_elf_sym.st_shndx = shndx; |
7635 | } | |
7636 | ||
b34976b6 | 7637 | return TRUE; |
252b5132 RH |
7638 | } |
7639 | ||
7640 | /* Swap out the symbols. */ | |
7641 | ||
b34976b6 | 7642 | static bfd_boolean |
217aa764 | 7643 | swap_out_syms (bfd *abfd, |
ef10c3ac | 7644 | struct elf_strtab_hash **sttp, |
217aa764 | 7645 | int relocatable_p) |
252b5132 | 7646 | { |
9c5bfbb7 | 7647 | const struct elf_backend_data *bed; |
079e9a2f AM |
7648 | int symcount; |
7649 | asymbol **syms; | |
ef10c3ac | 7650 | struct elf_strtab_hash *stt; |
079e9a2f | 7651 | Elf_Internal_Shdr *symtab_hdr; |
9ad5cbcf | 7652 | Elf_Internal_Shdr *symtab_shndx_hdr; |
079e9a2f | 7653 | Elf_Internal_Shdr *symstrtab_hdr; |
ef10c3ac | 7654 | struct elf_sym_strtab *symstrtab; |
f075ee0c AM |
7655 | bfd_byte *outbound_syms; |
7656 | bfd_byte *outbound_shndx; | |
ef10c3ac L |
7657 | unsigned long outbound_syms_index; |
7658 | unsigned long outbound_shndx_index; | |
079e9a2f | 7659 | int idx; |
12bd6957 | 7660 | unsigned int num_locals; |
079e9a2f | 7661 | bfd_size_type amt; |
174fd7f9 | 7662 | bfd_boolean name_local_sections; |
252b5132 | 7663 | |
12bd6957 | 7664 | if (!elf_map_symbols (abfd, &num_locals)) |
b34976b6 | 7665 | return FALSE; |
252b5132 | 7666 | |
c044fabd | 7667 | /* Dump out the symtabs. */ |
ef10c3ac | 7668 | stt = _bfd_elf_strtab_init (); |
079e9a2f | 7669 | if (stt == NULL) |
b34976b6 | 7670 | return FALSE; |
252b5132 | 7671 | |
079e9a2f AM |
7672 | bed = get_elf_backend_data (abfd); |
7673 | symcount = bfd_get_symcount (abfd); | |
7674 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; | |
7675 | symtab_hdr->sh_type = SHT_SYMTAB; | |
7676 | symtab_hdr->sh_entsize = bed->s->sizeof_sym; | |
7677 | symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1); | |
12bd6957 | 7678 | symtab_hdr->sh_info = num_locals + 1; |
72de5009 | 7679 | symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align; |
079e9a2f AM |
7680 | |
7681 | symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr; | |
7682 | symstrtab_hdr->sh_type = SHT_STRTAB; | |
7683 | ||
ef10c3ac L |
7684 | /* Allocate buffer to swap out the .strtab section. */ |
7685 | symstrtab = (struct elf_sym_strtab *) bfd_malloc ((symcount + 1) | |
7686 | * sizeof (*symstrtab)); | |
7687 | if (symstrtab == NULL) | |
7688 | { | |
7689 | _bfd_elf_strtab_free (stt); | |
7690 | return FALSE; | |
7691 | } | |
7692 | ||
a50b1753 NC |
7693 | outbound_syms = (bfd_byte *) bfd_alloc2 (abfd, 1 + symcount, |
7694 | bed->s->sizeof_sym); | |
079e9a2f | 7695 | if (outbound_syms == NULL) |
5ed6aba4 | 7696 | { |
ef10c3ac L |
7697 | error_return: |
7698 | _bfd_elf_strtab_free (stt); | |
7699 | free (symstrtab); | |
5ed6aba4 NC |
7700 | return FALSE; |
7701 | } | |
217aa764 | 7702 | symtab_hdr->contents = outbound_syms; |
ef10c3ac | 7703 | outbound_syms_index = 0; |
252b5132 | 7704 | |
9ad5cbcf | 7705 | outbound_shndx = NULL; |
ef10c3ac | 7706 | outbound_shndx_index = 0; |
6a40cf0c NC |
7707 | |
7708 | if (elf_symtab_shndx_list (abfd)) | |
9ad5cbcf | 7709 | { |
6a40cf0c NC |
7710 | symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr; |
7711 | if (symtab_shndx_hdr->sh_name != 0) | |
7712 | { | |
7713 | amt = (bfd_size_type) (1 + symcount) * sizeof (Elf_External_Sym_Shndx); | |
7714 | outbound_shndx = (bfd_byte *) | |
7715 | bfd_zalloc2 (abfd, 1 + symcount, sizeof (Elf_External_Sym_Shndx)); | |
7716 | if (outbound_shndx == NULL) | |
7717 | goto error_return; | |
5ed6aba4 | 7718 | |
6a40cf0c NC |
7719 | symtab_shndx_hdr->contents = outbound_shndx; |
7720 | symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX; | |
7721 | symtab_shndx_hdr->sh_size = amt; | |
7722 | symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx); | |
7723 | symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx); | |
7724 | } | |
7725 | /* FIXME: What about any other headers in the list ? */ | |
9ad5cbcf AM |
7726 | } |
7727 | ||
589e6347 | 7728 | /* Now generate the data (for "contents"). */ |
079e9a2f AM |
7729 | { |
7730 | /* Fill in zeroth symbol and swap it out. */ | |
7731 | Elf_Internal_Sym sym; | |
7732 | sym.st_name = 0; | |
7733 | sym.st_value = 0; | |
7734 | sym.st_size = 0; | |
7735 | sym.st_info = 0; | |
7736 | sym.st_other = 0; | |
7737 | sym.st_shndx = SHN_UNDEF; | |
35fc36a8 | 7738 | sym.st_target_internal = 0; |
ef10c3ac L |
7739 | symstrtab[0].sym = sym; |
7740 | symstrtab[0].dest_index = outbound_syms_index; | |
7741 | symstrtab[0].destshndx_index = outbound_shndx_index; | |
7742 | outbound_syms_index++; | |
9ad5cbcf | 7743 | if (outbound_shndx != NULL) |
ef10c3ac | 7744 | outbound_shndx_index++; |
079e9a2f | 7745 | } |
252b5132 | 7746 | |
174fd7f9 RS |
7747 | name_local_sections |
7748 | = (bed->elf_backend_name_local_section_symbols | |
7749 | && bed->elf_backend_name_local_section_symbols (abfd)); | |
7750 | ||
079e9a2f | 7751 | syms = bfd_get_outsymbols (abfd); |
ef10c3ac | 7752 | for (idx = 0; idx < symcount;) |
252b5132 | 7753 | { |
252b5132 | 7754 | Elf_Internal_Sym sym; |
079e9a2f AM |
7755 | bfd_vma value = syms[idx]->value; |
7756 | elf_symbol_type *type_ptr; | |
7757 | flagword flags = syms[idx]->flags; | |
7758 | int type; | |
252b5132 | 7759 | |
174fd7f9 RS |
7760 | if (!name_local_sections |
7761 | && (flags & (BSF_SECTION_SYM | BSF_GLOBAL)) == BSF_SECTION_SYM) | |
079e9a2f AM |
7762 | { |
7763 | /* Local section symbols have no name. */ | |
ef10c3ac | 7764 | sym.st_name = (unsigned long) -1; |
079e9a2f AM |
7765 | } |
7766 | else | |
7767 | { | |
ef10c3ac L |
7768 | /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize |
7769 | to get the final offset for st_name. */ | |
7770 | sym.st_name | |
7771 | = (unsigned long) _bfd_elf_strtab_add (stt, syms[idx]->name, | |
7772 | FALSE); | |
079e9a2f | 7773 | if (sym.st_name == (unsigned long) -1) |
ef10c3ac | 7774 | goto error_return; |
079e9a2f | 7775 | } |
252b5132 | 7776 | |
079e9a2f | 7777 | type_ptr = elf_symbol_from (abfd, syms[idx]); |
252b5132 | 7778 | |
079e9a2f AM |
7779 | if ((flags & BSF_SECTION_SYM) == 0 |
7780 | && bfd_is_com_section (syms[idx]->section)) | |
7781 | { | |
7782 | /* ELF common symbols put the alignment into the `value' field, | |
7783 | and the size into the `size' field. This is backwards from | |
7784 | how BFD handles it, so reverse it here. */ | |
7785 | sym.st_size = value; | |
7786 | if (type_ptr == NULL | |
7787 | || type_ptr->internal_elf_sym.st_value == 0) | |
7788 | sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value)); | |
7789 | else | |
7790 | sym.st_value = type_ptr->internal_elf_sym.st_value; | |
7791 | sym.st_shndx = _bfd_elf_section_from_bfd_section | |
7792 | (abfd, syms[idx]->section); | |
7793 | } | |
7794 | else | |
7795 | { | |
7796 | asection *sec = syms[idx]->section; | |
cb33740c | 7797 | unsigned int shndx; |
252b5132 | 7798 | |
079e9a2f AM |
7799 | if (sec->output_section) |
7800 | { | |
7801 | value += sec->output_offset; | |
7802 | sec = sec->output_section; | |
7803 | } | |
589e6347 | 7804 | |
079e9a2f AM |
7805 | /* Don't add in the section vma for relocatable output. */ |
7806 | if (! relocatable_p) | |
7807 | value += sec->vma; | |
7808 | sym.st_value = value; | |
7809 | sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0; | |
7810 | ||
7811 | if (bfd_is_abs_section (sec) | |
7812 | && type_ptr != NULL | |
7813 | && type_ptr->internal_elf_sym.st_shndx != 0) | |
7814 | { | |
7815 | /* This symbol is in a real ELF section which we did | |
7816 | not create as a BFD section. Undo the mapping done | |
7817 | by copy_private_symbol_data. */ | |
7818 | shndx = type_ptr->internal_elf_sym.st_shndx; | |
7819 | switch (shndx) | |
7820 | { | |
7821 | case MAP_ONESYMTAB: | |
7822 | shndx = elf_onesymtab (abfd); | |
7823 | break; | |
7824 | case MAP_DYNSYMTAB: | |
7825 | shndx = elf_dynsymtab (abfd); | |
7826 | break; | |
7827 | case MAP_STRTAB: | |
12bd6957 | 7828 | shndx = elf_strtab_sec (abfd); |
079e9a2f AM |
7829 | break; |
7830 | case MAP_SHSTRTAB: | |
12bd6957 | 7831 | shndx = elf_shstrtab_sec (abfd); |
079e9a2f | 7832 | break; |
9ad5cbcf | 7833 | case MAP_SYM_SHNDX: |
6a40cf0c NC |
7834 | if (elf_symtab_shndx_list (abfd)) |
7835 | shndx = elf_symtab_shndx_list (abfd)->ndx; | |
9ad5cbcf | 7836 | break; |
079e9a2f | 7837 | default: |
15bc576a | 7838 | shndx = SHN_ABS; |
079e9a2f AM |
7839 | break; |
7840 | } | |
7841 | } | |
7842 | else | |
7843 | { | |
7844 | shndx = _bfd_elf_section_from_bfd_section (abfd, sec); | |
252b5132 | 7845 | |
cb33740c | 7846 | if (shndx == SHN_BAD) |
079e9a2f AM |
7847 | { |
7848 | asection *sec2; | |
7849 | ||
7850 | /* Writing this would be a hell of a lot easier if | |
7851 | we had some decent documentation on bfd, and | |
7852 | knew what to expect of the library, and what to | |
7853 | demand of applications. For example, it | |
7854 | appears that `objcopy' might not set the | |
7855 | section of a symbol to be a section that is | |
7856 | actually in the output file. */ | |
7857 | sec2 = bfd_get_section_by_name (abfd, sec->name); | |
5df1bc57 AM |
7858 | if (sec2 != NULL) |
7859 | shndx = _bfd_elf_section_from_bfd_section (abfd, sec2); | |
7860 | if (shndx == SHN_BAD) | |
589e6347 | 7861 | { |
695344c0 | 7862 | /* xgettext:c-format */ |
589e6347 NC |
7863 | _bfd_error_handler (_("\ |
7864 | Unable to find equivalent output section for symbol '%s' from section '%s'"), | |
7865 | syms[idx]->name ? syms[idx]->name : "<Local sym>", | |
7866 | sec->name); | |
811072d8 | 7867 | bfd_set_error (bfd_error_invalid_operation); |
ef10c3ac | 7868 | goto error_return; |
589e6347 | 7869 | } |
079e9a2f AM |
7870 | } |
7871 | } | |
252b5132 | 7872 | |
079e9a2f AM |
7873 | sym.st_shndx = shndx; |
7874 | } | |
252b5132 | 7875 | |
13ae64f3 JJ |
7876 | if ((flags & BSF_THREAD_LOCAL) != 0) |
7877 | type = STT_TLS; | |
d8045f23 NC |
7878 | else if ((flags & BSF_GNU_INDIRECT_FUNCTION) != 0) |
7879 | type = STT_GNU_IFUNC; | |
13ae64f3 | 7880 | else if ((flags & BSF_FUNCTION) != 0) |
079e9a2f AM |
7881 | type = STT_FUNC; |
7882 | else if ((flags & BSF_OBJECT) != 0) | |
7883 | type = STT_OBJECT; | |
d9352518 DB |
7884 | else if ((flags & BSF_RELC) != 0) |
7885 | type = STT_RELC; | |
7886 | else if ((flags & BSF_SRELC) != 0) | |
7887 | type = STT_SRELC; | |
079e9a2f AM |
7888 | else |
7889 | type = STT_NOTYPE; | |
252b5132 | 7890 | |
13ae64f3 JJ |
7891 | if (syms[idx]->section->flags & SEC_THREAD_LOCAL) |
7892 | type = STT_TLS; | |
7893 | ||
589e6347 | 7894 | /* Processor-specific types. */ |
079e9a2f AM |
7895 | if (type_ptr != NULL |
7896 | && bed->elf_backend_get_symbol_type) | |
7897 | type = ((*bed->elf_backend_get_symbol_type) | |
7898 | (&type_ptr->internal_elf_sym, type)); | |
252b5132 | 7899 | |
079e9a2f AM |
7900 | if (flags & BSF_SECTION_SYM) |
7901 | { | |
7902 | if (flags & BSF_GLOBAL) | |
7903 | sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION); | |
7904 | else | |
7905 | sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION); | |
7906 | } | |
7907 | else if (bfd_is_com_section (syms[idx]->section)) | |
0a40daed | 7908 | { |
b8871f35 L |
7909 | if (type != STT_TLS) |
7910 | { | |
7911 | if ((abfd->flags & BFD_CONVERT_ELF_COMMON)) | |
7912 | type = ((abfd->flags & BFD_USE_ELF_STT_COMMON) | |
7913 | ? STT_COMMON : STT_OBJECT); | |
7914 | else | |
7915 | type = ((flags & BSF_ELF_COMMON) != 0 | |
7916 | ? STT_COMMON : STT_OBJECT); | |
7917 | } | |
7918 | sym.st_info = ELF_ST_INFO (STB_GLOBAL, type); | |
0a40daed | 7919 | } |
079e9a2f AM |
7920 | else if (bfd_is_und_section (syms[idx]->section)) |
7921 | sym.st_info = ELF_ST_INFO (((flags & BSF_WEAK) | |
7922 | ? STB_WEAK | |
7923 | : STB_GLOBAL), | |
7924 | type); | |
7925 | else if (flags & BSF_FILE) | |
7926 | sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE); | |
7927 | else | |
7928 | { | |
7929 | int bind = STB_LOCAL; | |
252b5132 | 7930 | |
079e9a2f AM |
7931 | if (flags & BSF_LOCAL) |
7932 | bind = STB_LOCAL; | |
3e7a7d11 NC |
7933 | else if (flags & BSF_GNU_UNIQUE) |
7934 | bind = STB_GNU_UNIQUE; | |
079e9a2f AM |
7935 | else if (flags & BSF_WEAK) |
7936 | bind = STB_WEAK; | |
7937 | else if (flags & BSF_GLOBAL) | |
7938 | bind = STB_GLOBAL; | |
252b5132 | 7939 | |
079e9a2f AM |
7940 | sym.st_info = ELF_ST_INFO (bind, type); |
7941 | } | |
252b5132 | 7942 | |
079e9a2f | 7943 | if (type_ptr != NULL) |
35fc36a8 RS |
7944 | { |
7945 | sym.st_other = type_ptr->internal_elf_sym.st_other; | |
7946 | sym.st_target_internal | |
7947 | = type_ptr->internal_elf_sym.st_target_internal; | |
7948 | } | |
079e9a2f | 7949 | else |
35fc36a8 RS |
7950 | { |
7951 | sym.st_other = 0; | |
7952 | sym.st_target_internal = 0; | |
7953 | } | |
252b5132 | 7954 | |
ef10c3ac L |
7955 | idx++; |
7956 | symstrtab[idx].sym = sym; | |
7957 | symstrtab[idx].dest_index = outbound_syms_index; | |
7958 | symstrtab[idx].destshndx_index = outbound_shndx_index; | |
7959 | ||
7960 | outbound_syms_index++; | |
9ad5cbcf | 7961 | if (outbound_shndx != NULL) |
ef10c3ac L |
7962 | outbound_shndx_index++; |
7963 | } | |
7964 | ||
7965 | /* Finalize the .strtab section. */ | |
7966 | _bfd_elf_strtab_finalize (stt); | |
7967 | ||
7968 | /* Swap out the .strtab section. */ | |
7969 | for (idx = 0; idx <= symcount; idx++) | |
7970 | { | |
7971 | struct elf_sym_strtab *elfsym = &symstrtab[idx]; | |
7972 | if (elfsym->sym.st_name == (unsigned long) -1) | |
7973 | elfsym->sym.st_name = 0; | |
7974 | else | |
7975 | elfsym->sym.st_name = _bfd_elf_strtab_offset (stt, | |
7976 | elfsym->sym.st_name); | |
7977 | bed->s->swap_symbol_out (abfd, &elfsym->sym, | |
7978 | (outbound_syms | |
7979 | + (elfsym->dest_index | |
7980 | * bed->s->sizeof_sym)), | |
7981 | (outbound_shndx | |
7982 | + (elfsym->destshndx_index | |
7983 | * sizeof (Elf_External_Sym_Shndx)))); | |
079e9a2f | 7984 | } |
ef10c3ac | 7985 | free (symstrtab); |
252b5132 | 7986 | |
079e9a2f | 7987 | *sttp = stt; |
ef10c3ac | 7988 | symstrtab_hdr->sh_size = _bfd_elf_strtab_size (stt); |
079e9a2f | 7989 | symstrtab_hdr->sh_type = SHT_STRTAB; |
84865015 | 7990 | symstrtab_hdr->sh_flags = bed->elf_strtab_flags; |
079e9a2f AM |
7991 | symstrtab_hdr->sh_addr = 0; |
7992 | symstrtab_hdr->sh_entsize = 0; | |
7993 | symstrtab_hdr->sh_link = 0; | |
7994 | symstrtab_hdr->sh_info = 0; | |
7995 | symstrtab_hdr->sh_addralign = 1; | |
252b5132 | 7996 | |
b34976b6 | 7997 | return TRUE; |
252b5132 RH |
7998 | } |
7999 | ||
8000 | /* Return the number of bytes required to hold the symtab vector. | |
8001 | ||
8002 | Note that we base it on the count plus 1, since we will null terminate | |
8003 | the vector allocated based on this size. However, the ELF symbol table | |
8004 | always has a dummy entry as symbol #0, so it ends up even. */ | |
8005 | ||
8006 | long | |
217aa764 | 8007 | _bfd_elf_get_symtab_upper_bound (bfd *abfd) |
252b5132 RH |
8008 | { |
8009 | long symcount; | |
8010 | long symtab_size; | |
8011 | Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr; | |
8012 | ||
8013 | symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym; | |
b99d1833 AM |
8014 | symtab_size = (symcount + 1) * (sizeof (asymbol *)); |
8015 | if (symcount > 0) | |
8016 | symtab_size -= sizeof (asymbol *); | |
252b5132 RH |
8017 | |
8018 | return symtab_size; | |
8019 | } | |
8020 | ||
8021 | long | |
217aa764 | 8022 | _bfd_elf_get_dynamic_symtab_upper_bound (bfd *abfd) |
252b5132 RH |
8023 | { |
8024 | long symcount; | |
8025 | long symtab_size; | |
8026 | Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr; | |
8027 | ||
8028 | if (elf_dynsymtab (abfd) == 0) | |
8029 | { | |
8030 | bfd_set_error (bfd_error_invalid_operation); | |
8031 | return -1; | |
8032 | } | |
8033 | ||
8034 | symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym; | |
b99d1833 AM |
8035 | symtab_size = (symcount + 1) * (sizeof (asymbol *)); |
8036 | if (symcount > 0) | |
8037 | symtab_size -= sizeof (asymbol *); | |
252b5132 RH |
8038 | |
8039 | return symtab_size; | |
8040 | } | |
8041 | ||
8042 | long | |
217aa764 AM |
8043 | _bfd_elf_get_reloc_upper_bound (bfd *abfd ATTRIBUTE_UNUSED, |
8044 | sec_ptr asect) | |
252b5132 RH |
8045 | { |
8046 | return (asect->reloc_count + 1) * sizeof (arelent *); | |
8047 | } | |
8048 | ||
8049 | /* Canonicalize the relocs. */ | |
8050 | ||
8051 | long | |
217aa764 AM |
8052 | _bfd_elf_canonicalize_reloc (bfd *abfd, |
8053 | sec_ptr section, | |
8054 | arelent **relptr, | |
8055 | asymbol **symbols) | |
252b5132 RH |
8056 | { |
8057 | arelent *tblptr; | |
8058 | unsigned int i; | |
9c5bfbb7 | 8059 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
252b5132 | 8060 | |
b34976b6 | 8061 | if (! bed->s->slurp_reloc_table (abfd, section, symbols, FALSE)) |
252b5132 RH |
8062 | return -1; |
8063 | ||
8064 | tblptr = section->relocation; | |
8065 | for (i = 0; i < section->reloc_count; i++) | |
8066 | *relptr++ = tblptr++; | |
8067 | ||
8068 | *relptr = NULL; | |
8069 | ||
8070 | return section->reloc_count; | |
8071 | } | |
8072 | ||
8073 | long | |
6cee3f79 | 8074 | _bfd_elf_canonicalize_symtab (bfd *abfd, asymbol **allocation) |
252b5132 | 8075 | { |
9c5bfbb7 | 8076 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
217aa764 | 8077 | long symcount = bed->s->slurp_symbol_table (abfd, allocation, FALSE); |
252b5132 RH |
8078 | |
8079 | if (symcount >= 0) | |
8080 | bfd_get_symcount (abfd) = symcount; | |
8081 | return symcount; | |
8082 | } | |
8083 | ||
8084 | long | |
217aa764 AM |
8085 | _bfd_elf_canonicalize_dynamic_symtab (bfd *abfd, |
8086 | asymbol **allocation) | |
252b5132 | 8087 | { |
9c5bfbb7 | 8088 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
217aa764 | 8089 | long symcount = bed->s->slurp_symbol_table (abfd, allocation, TRUE); |
1f70368c DJ |
8090 | |
8091 | if (symcount >= 0) | |
8092 | bfd_get_dynamic_symcount (abfd) = symcount; | |
8093 | return symcount; | |
252b5132 RH |
8094 | } |
8095 | ||
8615f3f2 AM |
8096 | /* Return the size required for the dynamic reloc entries. Any loadable |
8097 | section that was actually installed in the BFD, and has type SHT_REL | |
8098 | or SHT_RELA, and uses the dynamic symbol table, is considered to be a | |
8099 | dynamic reloc section. */ | |
252b5132 RH |
8100 | |
8101 | long | |
217aa764 | 8102 | _bfd_elf_get_dynamic_reloc_upper_bound (bfd *abfd) |
252b5132 RH |
8103 | { |
8104 | long ret; | |
8105 | asection *s; | |
8106 | ||
8107 | if (elf_dynsymtab (abfd) == 0) | |
8108 | { | |
8109 | bfd_set_error (bfd_error_invalid_operation); | |
8110 | return -1; | |
8111 | } | |
8112 | ||
8113 | ret = sizeof (arelent *); | |
8114 | for (s = abfd->sections; s != NULL; s = s->next) | |
266b05cf | 8115 | if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd) |
252b5132 RH |
8116 | && (elf_section_data (s)->this_hdr.sh_type == SHT_REL |
8117 | || elf_section_data (s)->this_hdr.sh_type == SHT_RELA)) | |
eea6121a | 8118 | ret += ((s->size / elf_section_data (s)->this_hdr.sh_entsize) |
252b5132 RH |
8119 | * sizeof (arelent *)); |
8120 | ||
8121 | return ret; | |
8122 | } | |
8123 | ||
8615f3f2 AM |
8124 | /* Canonicalize the dynamic relocation entries. Note that we return the |
8125 | dynamic relocations as a single block, although they are actually | |
8126 | associated with particular sections; the interface, which was | |
8127 | designed for SunOS style shared libraries, expects that there is only | |
8128 | one set of dynamic relocs. Any loadable section that was actually | |
8129 | installed in the BFD, and has type SHT_REL or SHT_RELA, and uses the | |
8130 | dynamic symbol table, is considered to be a dynamic reloc section. */ | |
252b5132 RH |
8131 | |
8132 | long | |
217aa764 AM |
8133 | _bfd_elf_canonicalize_dynamic_reloc (bfd *abfd, |
8134 | arelent **storage, | |
8135 | asymbol **syms) | |
252b5132 | 8136 | { |
217aa764 | 8137 | bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean); |
252b5132 RH |
8138 | asection *s; |
8139 | long ret; | |
8140 | ||
8141 | if (elf_dynsymtab (abfd) == 0) | |
8142 | { | |
8143 | bfd_set_error (bfd_error_invalid_operation); | |
8144 | return -1; | |
8145 | } | |
8146 | ||
8147 | slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table; | |
8148 | ret = 0; | |
8149 | for (s = abfd->sections; s != NULL; s = s->next) | |
8150 | { | |
266b05cf | 8151 | if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd) |
252b5132 RH |
8152 | && (elf_section_data (s)->this_hdr.sh_type == SHT_REL |
8153 | || elf_section_data (s)->this_hdr.sh_type == SHT_RELA)) | |
8154 | { | |
8155 | arelent *p; | |
8156 | long count, i; | |
8157 | ||
b34976b6 | 8158 | if (! (*slurp_relocs) (abfd, s, syms, TRUE)) |
252b5132 | 8159 | return -1; |
eea6121a | 8160 | count = s->size / elf_section_data (s)->this_hdr.sh_entsize; |
252b5132 RH |
8161 | p = s->relocation; |
8162 | for (i = 0; i < count; i++) | |
8163 | *storage++ = p++; | |
8164 | ret += count; | |
8165 | } | |
8166 | } | |
8167 | ||
8168 | *storage = NULL; | |
8169 | ||
8170 | return ret; | |
8171 | } | |
8172 | \f | |
8173 | /* Read in the version information. */ | |
8174 | ||
b34976b6 | 8175 | bfd_boolean |
fc0e6df6 | 8176 | _bfd_elf_slurp_version_tables (bfd *abfd, bfd_boolean default_imported_symver) |
252b5132 RH |
8177 | { |
8178 | bfd_byte *contents = NULL; | |
fc0e6df6 PB |
8179 | unsigned int freeidx = 0; |
8180 | ||
8181 | if (elf_dynverref (abfd) != 0) | |
8182 | { | |
8183 | Elf_Internal_Shdr *hdr; | |
8184 | Elf_External_Verneed *everneed; | |
8185 | Elf_Internal_Verneed *iverneed; | |
8186 | unsigned int i; | |
d0fb9a8d | 8187 | bfd_byte *contents_end; |
fc0e6df6 PB |
8188 | |
8189 | hdr = &elf_tdata (abfd)->dynverref_hdr; | |
8190 | ||
601a03ba | 8191 | if (hdr->sh_info == 0 || hdr->sh_size < sizeof (Elf_External_Verneed)) |
d0fb9a8d | 8192 | { |
601a03ba | 8193 | error_return_bad_verref: |
4eca0228 | 8194 | _bfd_error_handler |
601a03ba AM |
8195 | (_("%B: .gnu.version_r invalid entry"), abfd); |
8196 | bfd_set_error (bfd_error_bad_value); | |
d0fb9a8d JJ |
8197 | error_return_verref: |
8198 | elf_tdata (abfd)->verref = NULL; | |
8199 | elf_tdata (abfd)->cverrefs = 0; | |
8200 | goto error_return; | |
8201 | } | |
601a03ba AM |
8202 | |
8203 | contents = (bfd_byte *) bfd_malloc (hdr->sh_size); | |
8204 | if (contents == NULL) | |
8205 | goto error_return_verref; | |
8206 | ||
fc0e6df6 PB |
8207 | if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0 |
8208 | || bfd_bread (contents, hdr->sh_size, abfd) != hdr->sh_size) | |
d0fb9a8d | 8209 | goto error_return_verref; |
fc0e6df6 | 8210 | |
601a03ba AM |
8211 | elf_tdata (abfd)->verref = (Elf_Internal_Verneed *) |
8212 | bfd_zalloc2 (abfd, hdr->sh_info, sizeof (Elf_Internal_Verneed)); | |
8213 | ||
8214 | if (elf_tdata (abfd)->verref == NULL) | |
d0fb9a8d JJ |
8215 | goto error_return_verref; |
8216 | ||
8217 | BFD_ASSERT (sizeof (Elf_External_Verneed) | |
8218 | == sizeof (Elf_External_Vernaux)); | |
8219 | contents_end = contents + hdr->sh_size - sizeof (Elf_External_Verneed); | |
fc0e6df6 PB |
8220 | everneed = (Elf_External_Verneed *) contents; |
8221 | iverneed = elf_tdata (abfd)->verref; | |
8222 | for (i = 0; i < hdr->sh_info; i++, iverneed++) | |
8223 | { | |
8224 | Elf_External_Vernaux *evernaux; | |
8225 | Elf_Internal_Vernaux *ivernaux; | |
8226 | unsigned int j; | |
8227 | ||
8228 | _bfd_elf_swap_verneed_in (abfd, everneed, iverneed); | |
8229 | ||
8230 | iverneed->vn_bfd = abfd; | |
8231 | ||
8232 | iverneed->vn_filename = | |
8233 | bfd_elf_string_from_elf_section (abfd, hdr->sh_link, | |
8234 | iverneed->vn_file); | |
8235 | if (iverneed->vn_filename == NULL) | |
601a03ba | 8236 | goto error_return_bad_verref; |
fc0e6df6 | 8237 | |
d0fb9a8d JJ |
8238 | if (iverneed->vn_cnt == 0) |
8239 | iverneed->vn_auxptr = NULL; | |
8240 | else | |
8241 | { | |
a50b1753 NC |
8242 | iverneed->vn_auxptr = (struct elf_internal_vernaux *) |
8243 | bfd_alloc2 (abfd, iverneed->vn_cnt, | |
8244 | sizeof (Elf_Internal_Vernaux)); | |
d0fb9a8d JJ |
8245 | if (iverneed->vn_auxptr == NULL) |
8246 | goto error_return_verref; | |
8247 | } | |
8248 | ||
8249 | if (iverneed->vn_aux | |
8250 | > (size_t) (contents_end - (bfd_byte *) everneed)) | |
601a03ba | 8251 | goto error_return_bad_verref; |
fc0e6df6 PB |
8252 | |
8253 | evernaux = ((Elf_External_Vernaux *) | |
8254 | ((bfd_byte *) everneed + iverneed->vn_aux)); | |
8255 | ivernaux = iverneed->vn_auxptr; | |
8256 | for (j = 0; j < iverneed->vn_cnt; j++, ivernaux++) | |
8257 | { | |
8258 | _bfd_elf_swap_vernaux_in (abfd, evernaux, ivernaux); | |
8259 | ||
8260 | ivernaux->vna_nodename = | |
8261 | bfd_elf_string_from_elf_section (abfd, hdr->sh_link, | |
8262 | ivernaux->vna_name); | |
8263 | if (ivernaux->vna_nodename == NULL) | |
601a03ba | 8264 | goto error_return_bad_verref; |
fc0e6df6 | 8265 | |
25ff461f AM |
8266 | if (ivernaux->vna_other > freeidx) |
8267 | freeidx = ivernaux->vna_other; | |
8268 | ||
8269 | ivernaux->vna_nextptr = NULL; | |
8270 | if (ivernaux->vna_next == 0) | |
8271 | { | |
8272 | iverneed->vn_cnt = j + 1; | |
8273 | break; | |
8274 | } | |
fc0e6df6 PB |
8275 | if (j + 1 < iverneed->vn_cnt) |
8276 | ivernaux->vna_nextptr = ivernaux + 1; | |
fc0e6df6 | 8277 | |
d0fb9a8d JJ |
8278 | if (ivernaux->vna_next |
8279 | > (size_t) (contents_end - (bfd_byte *) evernaux)) | |
601a03ba | 8280 | goto error_return_bad_verref; |
d0fb9a8d | 8281 | |
fc0e6df6 PB |
8282 | evernaux = ((Elf_External_Vernaux *) |
8283 | ((bfd_byte *) evernaux + ivernaux->vna_next)); | |
fc0e6df6 PB |
8284 | } |
8285 | ||
25ff461f AM |
8286 | iverneed->vn_nextref = NULL; |
8287 | if (iverneed->vn_next == 0) | |
8288 | break; | |
fc0e6df6 PB |
8289 | if (i + 1 < hdr->sh_info) |
8290 | iverneed->vn_nextref = iverneed + 1; | |
fc0e6df6 | 8291 | |
d0fb9a8d JJ |
8292 | if (iverneed->vn_next |
8293 | > (size_t) (contents_end - (bfd_byte *) everneed)) | |
601a03ba | 8294 | goto error_return_bad_verref; |
d0fb9a8d | 8295 | |
fc0e6df6 PB |
8296 | everneed = ((Elf_External_Verneed *) |
8297 | ((bfd_byte *) everneed + iverneed->vn_next)); | |
8298 | } | |
25ff461f | 8299 | elf_tdata (abfd)->cverrefs = i; |
fc0e6df6 PB |
8300 | |
8301 | free (contents); | |
8302 | contents = NULL; | |
8303 | } | |
252b5132 RH |
8304 | |
8305 | if (elf_dynverdef (abfd) != 0) | |
8306 | { | |
8307 | Elf_Internal_Shdr *hdr; | |
8308 | Elf_External_Verdef *everdef; | |
8309 | Elf_Internal_Verdef *iverdef; | |
f631889e UD |
8310 | Elf_Internal_Verdef *iverdefarr; |
8311 | Elf_Internal_Verdef iverdefmem; | |
252b5132 | 8312 | unsigned int i; |
062e2358 | 8313 | unsigned int maxidx; |
d0fb9a8d | 8314 | bfd_byte *contents_end_def, *contents_end_aux; |
252b5132 RH |
8315 | |
8316 | hdr = &elf_tdata (abfd)->dynverdef_hdr; | |
8317 | ||
601a03ba AM |
8318 | if (hdr->sh_info == 0 || hdr->sh_size < sizeof (Elf_External_Verdef)) |
8319 | { | |
8320 | error_return_bad_verdef: | |
4eca0228 | 8321 | _bfd_error_handler |
601a03ba AM |
8322 | (_("%B: .gnu.version_d invalid entry"), abfd); |
8323 | bfd_set_error (bfd_error_bad_value); | |
8324 | error_return_verdef: | |
8325 | elf_tdata (abfd)->verdef = NULL; | |
8326 | elf_tdata (abfd)->cverdefs = 0; | |
8327 | goto error_return; | |
8328 | } | |
8329 | ||
a50b1753 | 8330 | contents = (bfd_byte *) bfd_malloc (hdr->sh_size); |
252b5132 | 8331 | if (contents == NULL) |
601a03ba | 8332 | goto error_return_verdef; |
252b5132 | 8333 | if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0 |
217aa764 | 8334 | || bfd_bread (contents, hdr->sh_size, abfd) != hdr->sh_size) |
601a03ba | 8335 | goto error_return_verdef; |
d0fb9a8d JJ |
8336 | |
8337 | BFD_ASSERT (sizeof (Elf_External_Verdef) | |
8338 | >= sizeof (Elf_External_Verdaux)); | |
8339 | contents_end_def = contents + hdr->sh_size | |
8340 | - sizeof (Elf_External_Verdef); | |
8341 | contents_end_aux = contents + hdr->sh_size | |
8342 | - sizeof (Elf_External_Verdaux); | |
8343 | ||
f631889e UD |
8344 | /* We know the number of entries in the section but not the maximum |
8345 | index. Therefore we have to run through all entries and find | |
8346 | the maximum. */ | |
252b5132 | 8347 | everdef = (Elf_External_Verdef *) contents; |
f631889e UD |
8348 | maxidx = 0; |
8349 | for (i = 0; i < hdr->sh_info; ++i) | |
8350 | { | |
8351 | _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem); | |
8352 | ||
601a03ba AM |
8353 | if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) == 0) |
8354 | goto error_return_bad_verdef; | |
062e2358 AM |
8355 | if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) > maxidx) |
8356 | maxidx = iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION); | |
f631889e | 8357 | |
25ff461f AM |
8358 | if (iverdefmem.vd_next == 0) |
8359 | break; | |
8360 | ||
d0fb9a8d JJ |
8361 | if (iverdefmem.vd_next |
8362 | > (size_t) (contents_end_def - (bfd_byte *) everdef)) | |
601a03ba | 8363 | goto error_return_bad_verdef; |
d0fb9a8d | 8364 | |
f631889e UD |
8365 | everdef = ((Elf_External_Verdef *) |
8366 | ((bfd_byte *) everdef + iverdefmem.vd_next)); | |
8367 | } | |
8368 | ||
fc0e6df6 PB |
8369 | if (default_imported_symver) |
8370 | { | |
8371 | if (freeidx > maxidx) | |
8372 | maxidx = ++freeidx; | |
8373 | else | |
8374 | freeidx = ++maxidx; | |
8375 | } | |
201159ec | 8376 | |
601a03ba AM |
8377 | elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *) |
8378 | bfd_zalloc2 (abfd, maxidx, sizeof (Elf_Internal_Verdef)); | |
f631889e | 8379 | if (elf_tdata (abfd)->verdef == NULL) |
601a03ba | 8380 | goto error_return_verdef; |
f631889e UD |
8381 | |
8382 | elf_tdata (abfd)->cverdefs = maxidx; | |
8383 | ||
8384 | everdef = (Elf_External_Verdef *) contents; | |
8385 | iverdefarr = elf_tdata (abfd)->verdef; | |
8386 | for (i = 0; i < hdr->sh_info; i++) | |
252b5132 RH |
8387 | { |
8388 | Elf_External_Verdaux *everdaux; | |
8389 | Elf_Internal_Verdaux *iverdaux; | |
8390 | unsigned int j; | |
8391 | ||
f631889e UD |
8392 | _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem); |
8393 | ||
d0fb9a8d | 8394 | if ((iverdefmem.vd_ndx & VERSYM_VERSION) == 0) |
601a03ba | 8395 | goto error_return_bad_verdef; |
d0fb9a8d | 8396 | |
f631889e | 8397 | iverdef = &iverdefarr[(iverdefmem.vd_ndx & VERSYM_VERSION) - 1]; |
595bce75 | 8398 | memcpy (iverdef, &iverdefmem, offsetof (Elf_Internal_Verdef, vd_bfd)); |
252b5132 RH |
8399 | |
8400 | iverdef->vd_bfd = abfd; | |
8401 | ||
d0fb9a8d JJ |
8402 | if (iverdef->vd_cnt == 0) |
8403 | iverdef->vd_auxptr = NULL; | |
8404 | else | |
8405 | { | |
a50b1753 NC |
8406 | iverdef->vd_auxptr = (struct elf_internal_verdaux *) |
8407 | bfd_alloc2 (abfd, iverdef->vd_cnt, | |
8408 | sizeof (Elf_Internal_Verdaux)); | |
d0fb9a8d JJ |
8409 | if (iverdef->vd_auxptr == NULL) |
8410 | goto error_return_verdef; | |
8411 | } | |
8412 | ||
8413 | if (iverdef->vd_aux | |
8414 | > (size_t) (contents_end_aux - (bfd_byte *) everdef)) | |
601a03ba | 8415 | goto error_return_bad_verdef; |
252b5132 RH |
8416 | |
8417 | everdaux = ((Elf_External_Verdaux *) | |
8418 | ((bfd_byte *) everdef + iverdef->vd_aux)); | |
8419 | iverdaux = iverdef->vd_auxptr; | |
8420 | for (j = 0; j < iverdef->vd_cnt; j++, iverdaux++) | |
8421 | { | |
8422 | _bfd_elf_swap_verdaux_in (abfd, everdaux, iverdaux); | |
8423 | ||
8424 | iverdaux->vda_nodename = | |
8425 | bfd_elf_string_from_elf_section (abfd, hdr->sh_link, | |
8426 | iverdaux->vda_name); | |
8427 | if (iverdaux->vda_nodename == NULL) | |
601a03ba | 8428 | goto error_return_bad_verdef; |
252b5132 | 8429 | |
25ff461f AM |
8430 | iverdaux->vda_nextptr = NULL; |
8431 | if (iverdaux->vda_next == 0) | |
8432 | { | |
8433 | iverdef->vd_cnt = j + 1; | |
8434 | break; | |
8435 | } | |
252b5132 RH |
8436 | if (j + 1 < iverdef->vd_cnt) |
8437 | iverdaux->vda_nextptr = iverdaux + 1; | |
252b5132 | 8438 | |
d0fb9a8d JJ |
8439 | if (iverdaux->vda_next |
8440 | > (size_t) (contents_end_aux - (bfd_byte *) everdaux)) | |
601a03ba | 8441 | goto error_return_bad_verdef; |
d0fb9a8d | 8442 | |
252b5132 RH |
8443 | everdaux = ((Elf_External_Verdaux *) |
8444 | ((bfd_byte *) everdaux + iverdaux->vda_next)); | |
8445 | } | |
8446 | ||
595bce75 | 8447 | iverdef->vd_nodename = NULL; |
d0fb9a8d JJ |
8448 | if (iverdef->vd_cnt) |
8449 | iverdef->vd_nodename = iverdef->vd_auxptr->vda_nodename; | |
252b5132 | 8450 | |
25ff461f AM |
8451 | iverdef->vd_nextdef = NULL; |
8452 | if (iverdef->vd_next == 0) | |
8453 | break; | |
d0fb9a8d | 8454 | if ((size_t) (iverdef - iverdefarr) + 1 < maxidx) |
252b5132 | 8455 | iverdef->vd_nextdef = iverdef + 1; |
252b5132 RH |
8456 | |
8457 | everdef = ((Elf_External_Verdef *) | |
8458 | ((bfd_byte *) everdef + iverdef->vd_next)); | |
8459 | } | |
8460 | ||
8461 | free (contents); | |
8462 | contents = NULL; | |
8463 | } | |
fc0e6df6 | 8464 | else if (default_imported_symver) |
252b5132 | 8465 | { |
fc0e6df6 PB |
8466 | if (freeidx < 3) |
8467 | freeidx = 3; | |
8468 | else | |
8469 | freeidx++; | |
252b5132 | 8470 | |
a50b1753 NC |
8471 | elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *) |
8472 | bfd_zalloc2 (abfd, freeidx, sizeof (Elf_Internal_Verdef)); | |
fc0e6df6 | 8473 | if (elf_tdata (abfd)->verdef == NULL) |
252b5132 RH |
8474 | goto error_return; |
8475 | ||
fc0e6df6 PB |
8476 | elf_tdata (abfd)->cverdefs = freeidx; |
8477 | } | |
252b5132 | 8478 | |
fc0e6df6 PB |
8479 | /* Create a default version based on the soname. */ |
8480 | if (default_imported_symver) | |
8481 | { | |
8482 | Elf_Internal_Verdef *iverdef; | |
8483 | Elf_Internal_Verdaux *iverdaux; | |
252b5132 | 8484 | |
5bb3703f | 8485 | iverdef = &elf_tdata (abfd)->verdef[freeidx - 1]; |
252b5132 | 8486 | |
fc0e6df6 PB |
8487 | iverdef->vd_version = VER_DEF_CURRENT; |
8488 | iverdef->vd_flags = 0; | |
8489 | iverdef->vd_ndx = freeidx; | |
8490 | iverdef->vd_cnt = 1; | |
252b5132 | 8491 | |
fc0e6df6 | 8492 | iverdef->vd_bfd = abfd; |
252b5132 | 8493 | |
fc0e6df6 PB |
8494 | iverdef->vd_nodename = bfd_elf_get_dt_soname (abfd); |
8495 | if (iverdef->vd_nodename == NULL) | |
d0fb9a8d | 8496 | goto error_return_verdef; |
fc0e6df6 | 8497 | iverdef->vd_nextdef = NULL; |
601a03ba AM |
8498 | iverdef->vd_auxptr = ((struct elf_internal_verdaux *) |
8499 | bfd_zalloc (abfd, sizeof (Elf_Internal_Verdaux))); | |
d0fb9a8d JJ |
8500 | if (iverdef->vd_auxptr == NULL) |
8501 | goto error_return_verdef; | |
252b5132 | 8502 | |
fc0e6df6 PB |
8503 | iverdaux = iverdef->vd_auxptr; |
8504 | iverdaux->vda_nodename = iverdef->vd_nodename; | |
252b5132 RH |
8505 | } |
8506 | ||
b34976b6 | 8507 | return TRUE; |
252b5132 RH |
8508 | |
8509 | error_return: | |
5ed6aba4 | 8510 | if (contents != NULL) |
252b5132 | 8511 | free (contents); |
b34976b6 | 8512 | return FALSE; |
252b5132 RH |
8513 | } |
8514 | \f | |
8515 | asymbol * | |
217aa764 | 8516 | _bfd_elf_make_empty_symbol (bfd *abfd) |
252b5132 RH |
8517 | { |
8518 | elf_symbol_type *newsym; | |
8519 | ||
201159ec | 8520 | newsym = (elf_symbol_type *) bfd_zalloc (abfd, sizeof * newsym); |
252b5132 RH |
8521 | if (!newsym) |
8522 | return NULL; | |
201159ec NC |
8523 | newsym->symbol.the_bfd = abfd; |
8524 | return &newsym->symbol; | |
252b5132 RH |
8525 | } |
8526 | ||
8527 | void | |
217aa764 AM |
8528 | _bfd_elf_get_symbol_info (bfd *abfd ATTRIBUTE_UNUSED, |
8529 | asymbol *symbol, | |
8530 | symbol_info *ret) | |
252b5132 RH |
8531 | { |
8532 | bfd_symbol_info (symbol, ret); | |
8533 | } | |
8534 | ||
8535 | /* Return whether a symbol name implies a local symbol. Most targets | |
8536 | use this function for the is_local_label_name entry point, but some | |
8537 | override it. */ | |
8538 | ||
b34976b6 | 8539 | bfd_boolean |
217aa764 AM |
8540 | _bfd_elf_is_local_label_name (bfd *abfd ATTRIBUTE_UNUSED, |
8541 | const char *name) | |
252b5132 RH |
8542 | { |
8543 | /* Normal local symbols start with ``.L''. */ | |
8544 | if (name[0] == '.' && name[1] == 'L') | |
b34976b6 | 8545 | return TRUE; |
252b5132 RH |
8546 | |
8547 | /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate | |
8548 | DWARF debugging symbols starting with ``..''. */ | |
8549 | if (name[0] == '.' && name[1] == '.') | |
b34976b6 | 8550 | return TRUE; |
252b5132 RH |
8551 | |
8552 | /* gcc will sometimes generate symbols beginning with ``_.L_'' when | |
8553 | emitting DWARF debugging output. I suspect this is actually a | |
8554 | small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call | |
8555 | ASM_GENERATE_INTERNAL_LABEL, and this causes the leading | |
8556 | underscore to be emitted on some ELF targets). For ease of use, | |
8557 | we treat such symbols as local. */ | |
8558 | if (name[0] == '_' && name[1] == '.' && name[2] == 'L' && name[3] == '_') | |
b34976b6 | 8559 | return TRUE; |
252b5132 | 8560 | |
b1fa9dd6 NC |
8561 | /* Treat assembler generated fake symbols, dollar local labels and |
8562 | forward-backward labels (aka local labels) as locals. | |
8563 | These labels have the form: | |
8564 | ||
8565 | L0^A.* (fake symbols) | |
8566 | ||
8567 | [.]?L[0123456789]+{^A|^B}[0123456789]* (local labels) | |
8568 | ||
8569 | Versions which start with .L will have already been matched above, | |
8570 | so we only need to match the rest. */ | |
8571 | if (name[0] == 'L' && ISDIGIT (name[1])) | |
8572 | { | |
8573 | bfd_boolean ret = FALSE; | |
8574 | const char * p; | |
8575 | char c; | |
8576 | ||
8577 | for (p = name + 2; (c = *p); p++) | |
8578 | { | |
8579 | if (c == 1 || c == 2) | |
8580 | { | |
8581 | if (c == 1 && p == name + 2) | |
8582 | /* A fake symbol. */ | |
8583 | return TRUE; | |
8584 | ||
8585 | /* FIXME: We are being paranoid here and treating symbols like | |
8586 | L0^Bfoo as if there were non-local, on the grounds that the | |
8587 | assembler will never generate them. But can any symbol | |
8588 | containing an ASCII value in the range 1-31 ever be anything | |
8589 | other than some kind of local ? */ | |
8590 | ret = TRUE; | |
8591 | } | |
8592 | ||
8593 | if (! ISDIGIT (c)) | |
8594 | { | |
8595 | ret = FALSE; | |
8596 | break; | |
8597 | } | |
8598 | } | |
8599 | return ret; | |
8600 | } | |
ffa54770 | 8601 | |
b34976b6 | 8602 | return FALSE; |
252b5132 RH |
8603 | } |
8604 | ||
8605 | alent * | |
217aa764 AM |
8606 | _bfd_elf_get_lineno (bfd *abfd ATTRIBUTE_UNUSED, |
8607 | asymbol *symbol ATTRIBUTE_UNUSED) | |
252b5132 RH |
8608 | { |
8609 | abort (); | |
8610 | return NULL; | |
8611 | } | |
8612 | ||
b34976b6 | 8613 | bfd_boolean |
217aa764 AM |
8614 | _bfd_elf_set_arch_mach (bfd *abfd, |
8615 | enum bfd_architecture arch, | |
8616 | unsigned long machine) | |
252b5132 RH |
8617 | { |
8618 | /* If this isn't the right architecture for this backend, and this | |
8619 | isn't the generic backend, fail. */ | |
8620 | if (arch != get_elf_backend_data (abfd)->arch | |
8621 | && arch != bfd_arch_unknown | |
8622 | && get_elf_backend_data (abfd)->arch != bfd_arch_unknown) | |
b34976b6 | 8623 | return FALSE; |
252b5132 RH |
8624 | |
8625 | return bfd_default_set_arch_mach (abfd, arch, machine); | |
8626 | } | |
8627 | ||
d1fad7c6 NC |
8628 | /* Find the nearest line to a particular section and offset, |
8629 | for error reporting. */ | |
8630 | ||
b34976b6 | 8631 | bfd_boolean |
217aa764 | 8632 | _bfd_elf_find_nearest_line (bfd *abfd, |
217aa764 | 8633 | asymbol **symbols, |
fb167eb2 | 8634 | asection *section, |
217aa764 AM |
8635 | bfd_vma offset, |
8636 | const char **filename_ptr, | |
8637 | const char **functionname_ptr, | |
fb167eb2 AM |
8638 | unsigned int *line_ptr, |
8639 | unsigned int *discriminator_ptr) | |
d1fad7c6 | 8640 | { |
b34976b6 | 8641 | bfd_boolean found; |
d1fad7c6 | 8642 | |
fb167eb2 | 8643 | if (_bfd_dwarf2_find_nearest_line (abfd, symbols, NULL, section, offset, |
4e8a9624 | 8644 | filename_ptr, functionname_ptr, |
fb167eb2 AM |
8645 | line_ptr, discriminator_ptr, |
8646 | dwarf_debug_sections, 0, | |
e00e8198 AM |
8647 | &elf_tdata (abfd)->dwarf2_find_line_info) |
8648 | || _bfd_dwarf1_find_nearest_line (abfd, symbols, section, offset, | |
8649 | filename_ptr, functionname_ptr, | |
8650 | line_ptr)) | |
d1fad7c6 NC |
8651 | { |
8652 | if (!*functionname_ptr) | |
e00e8198 AM |
8653 | _bfd_elf_find_function (abfd, symbols, section, offset, |
8654 | *filename_ptr ? NULL : filename_ptr, | |
8655 | functionname_ptr); | |
b34976b6 | 8656 | return TRUE; |
d1fad7c6 NC |
8657 | } |
8658 | ||
8659 | if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset, | |
4e8a9624 AM |
8660 | &found, filename_ptr, |
8661 | functionname_ptr, line_ptr, | |
8662 | &elf_tdata (abfd)->line_info)) | |
b34976b6 | 8663 | return FALSE; |
dc43ada5 | 8664 | if (found && (*functionname_ptr || *line_ptr)) |
b34976b6 | 8665 | return TRUE; |
d1fad7c6 NC |
8666 | |
8667 | if (symbols == NULL) | |
b34976b6 | 8668 | return FALSE; |
d1fad7c6 | 8669 | |
e00e8198 AM |
8670 | if (! _bfd_elf_find_function (abfd, symbols, section, offset, |
8671 | filename_ptr, functionname_ptr)) | |
b34976b6 | 8672 | return FALSE; |
d1fad7c6 | 8673 | |
252b5132 | 8674 | *line_ptr = 0; |
b34976b6 | 8675 | return TRUE; |
252b5132 RH |
8676 | } |
8677 | ||
5420f73d L |
8678 | /* Find the line for a symbol. */ |
8679 | ||
8680 | bfd_boolean | |
8681 | _bfd_elf_find_line (bfd *abfd, asymbol **symbols, asymbol *symbol, | |
8682 | const char **filename_ptr, unsigned int *line_ptr) | |
9b8d1a36 | 8683 | { |
fb167eb2 AM |
8684 | return _bfd_dwarf2_find_nearest_line (abfd, symbols, symbol, NULL, 0, |
8685 | filename_ptr, NULL, line_ptr, NULL, | |
8686 | dwarf_debug_sections, 0, | |
8687 | &elf_tdata (abfd)->dwarf2_find_line_info); | |
5420f73d L |
8688 | } |
8689 | ||
4ab527b0 FF |
8690 | /* After a call to bfd_find_nearest_line, successive calls to |
8691 | bfd_find_inliner_info can be used to get source information about | |
8692 | each level of function inlining that terminated at the address | |
8693 | passed to bfd_find_nearest_line. Currently this is only supported | |
8694 | for DWARF2 with appropriate DWARF3 extensions. */ | |
8695 | ||
8696 | bfd_boolean | |
8697 | _bfd_elf_find_inliner_info (bfd *abfd, | |
8698 | const char **filename_ptr, | |
8699 | const char **functionname_ptr, | |
8700 | unsigned int *line_ptr) | |
8701 | { | |
8702 | bfd_boolean found; | |
8703 | found = _bfd_dwarf2_find_inliner_info (abfd, filename_ptr, | |
8704 | functionname_ptr, line_ptr, | |
8705 | & elf_tdata (abfd)->dwarf2_find_line_info); | |
8706 | return found; | |
8707 | } | |
8708 | ||
252b5132 | 8709 | int |
a6b96beb | 8710 | _bfd_elf_sizeof_headers (bfd *abfd, struct bfd_link_info *info) |
252b5132 | 8711 | { |
8ded5a0f AM |
8712 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
8713 | int ret = bed->s->sizeof_ehdr; | |
252b5132 | 8714 | |
0e1862bb | 8715 | if (!bfd_link_relocatable (info)) |
8ded5a0f | 8716 | { |
12bd6957 | 8717 | bfd_size_type phdr_size = elf_program_header_size (abfd); |
8ded5a0f | 8718 | |
62d7a5f6 AM |
8719 | if (phdr_size == (bfd_size_type) -1) |
8720 | { | |
8721 | struct elf_segment_map *m; | |
8722 | ||
8723 | phdr_size = 0; | |
12bd6957 | 8724 | for (m = elf_seg_map (abfd); m != NULL; m = m->next) |
62d7a5f6 | 8725 | phdr_size += bed->s->sizeof_phdr; |
8ded5a0f | 8726 | |
62d7a5f6 AM |
8727 | if (phdr_size == 0) |
8728 | phdr_size = get_program_header_size (abfd, info); | |
8729 | } | |
8ded5a0f | 8730 | |
12bd6957 | 8731 | elf_program_header_size (abfd) = phdr_size; |
8ded5a0f AM |
8732 | ret += phdr_size; |
8733 | } | |
8734 | ||
252b5132 RH |
8735 | return ret; |
8736 | } | |
8737 | ||
b34976b6 | 8738 | bfd_boolean |
217aa764 AM |
8739 | _bfd_elf_set_section_contents (bfd *abfd, |
8740 | sec_ptr section, | |
0f867abe | 8741 | const void *location, |
217aa764 AM |
8742 | file_ptr offset, |
8743 | bfd_size_type count) | |
252b5132 RH |
8744 | { |
8745 | Elf_Internal_Shdr *hdr; | |
1b6aeedb | 8746 | file_ptr pos; |
252b5132 RH |
8747 | |
8748 | if (! abfd->output_has_begun | |
217aa764 | 8749 | && ! _bfd_elf_compute_section_file_positions (abfd, NULL)) |
b34976b6 | 8750 | return FALSE; |
252b5132 | 8751 | |
0ce398f1 L |
8752 | if (!count) |
8753 | return TRUE; | |
8754 | ||
252b5132 | 8755 | hdr = &elf_section_data (section)->this_hdr; |
0ce398f1 L |
8756 | if (hdr->sh_offset == (file_ptr) -1) |
8757 | { | |
8758 | /* We must compress this section. Write output to the buffer. */ | |
8759 | unsigned char *contents = hdr->contents; | |
8760 | if ((offset + count) > hdr->sh_size | |
8761 | || (section->flags & SEC_ELF_COMPRESS) == 0 | |
8762 | || contents == NULL) | |
8763 | abort (); | |
8764 | memcpy (contents + offset, location, count); | |
8765 | return TRUE; | |
8766 | } | |
dc810e39 AM |
8767 | pos = hdr->sh_offset + offset; |
8768 | if (bfd_seek (abfd, pos, SEEK_SET) != 0 | |
8769 | || bfd_bwrite (location, count, abfd) != count) | |
b34976b6 | 8770 | return FALSE; |
252b5132 | 8771 | |
b34976b6 | 8772 | return TRUE; |
252b5132 RH |
8773 | } |
8774 | ||
8775 | void | |
217aa764 AM |
8776 | _bfd_elf_no_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED, |
8777 | arelent *cache_ptr ATTRIBUTE_UNUSED, | |
8778 | Elf_Internal_Rela *dst ATTRIBUTE_UNUSED) | |
252b5132 RH |
8779 | { |
8780 | abort (); | |
8781 | } | |
8782 | ||
252b5132 RH |
8783 | /* Try to convert a non-ELF reloc into an ELF one. */ |
8784 | ||
b34976b6 | 8785 | bfd_boolean |
217aa764 | 8786 | _bfd_elf_validate_reloc (bfd *abfd, arelent *areloc) |
252b5132 | 8787 | { |
c044fabd | 8788 | /* Check whether we really have an ELF howto. */ |
252b5132 RH |
8789 | |
8790 | if ((*areloc->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec) | |
8791 | { | |
8792 | bfd_reloc_code_real_type code; | |
8793 | reloc_howto_type *howto; | |
8794 | ||
8795 | /* Alien reloc: Try to determine its type to replace it with an | |
c044fabd | 8796 | equivalent ELF reloc. */ |
252b5132 RH |
8797 | |
8798 | if (areloc->howto->pc_relative) | |
8799 | { | |
8800 | switch (areloc->howto->bitsize) | |
8801 | { | |
8802 | case 8: | |
8803 | code = BFD_RELOC_8_PCREL; | |
8804 | break; | |
8805 | case 12: | |
8806 | code = BFD_RELOC_12_PCREL; | |
8807 | break; | |
8808 | case 16: | |
8809 | code = BFD_RELOC_16_PCREL; | |
8810 | break; | |
8811 | case 24: | |
8812 | code = BFD_RELOC_24_PCREL; | |
8813 | break; | |
8814 | case 32: | |
8815 | code = BFD_RELOC_32_PCREL; | |
8816 | break; | |
8817 | case 64: | |
8818 | code = BFD_RELOC_64_PCREL; | |
8819 | break; | |
8820 | default: | |
8821 | goto fail; | |
8822 | } | |
8823 | ||
8824 | howto = bfd_reloc_type_lookup (abfd, code); | |
8825 | ||
8826 | if (areloc->howto->pcrel_offset != howto->pcrel_offset) | |
8827 | { | |
8828 | if (howto->pcrel_offset) | |
8829 | areloc->addend += areloc->address; | |
8830 | else | |
8831 | areloc->addend -= areloc->address; /* addend is unsigned!! */ | |
8832 | } | |
8833 | } | |
8834 | else | |
8835 | { | |
8836 | switch (areloc->howto->bitsize) | |
8837 | { | |
8838 | case 8: | |
8839 | code = BFD_RELOC_8; | |
8840 | break; | |
8841 | case 14: | |
8842 | code = BFD_RELOC_14; | |
8843 | break; | |
8844 | case 16: | |
8845 | code = BFD_RELOC_16; | |
8846 | break; | |
8847 | case 26: | |
8848 | code = BFD_RELOC_26; | |
8849 | break; | |
8850 | case 32: | |
8851 | code = BFD_RELOC_32; | |
8852 | break; | |
8853 | case 64: | |
8854 | code = BFD_RELOC_64; | |
8855 | break; | |
8856 | default: | |
8857 | goto fail; | |
8858 | } | |
8859 | ||
8860 | howto = bfd_reloc_type_lookup (abfd, code); | |
8861 | } | |
8862 | ||
8863 | if (howto) | |
8864 | areloc->howto = howto; | |
8865 | else | |
8866 | goto fail; | |
8867 | } | |
8868 | ||
b34976b6 | 8869 | return TRUE; |
252b5132 RH |
8870 | |
8871 | fail: | |
4eca0228 | 8872 | _bfd_error_handler |
695344c0 | 8873 | /* xgettext:c-format */ |
d003868e AM |
8874 | (_("%B: unsupported relocation type %s"), |
8875 | abfd, areloc->howto->name); | |
252b5132 | 8876 | bfd_set_error (bfd_error_bad_value); |
b34976b6 | 8877 | return FALSE; |
252b5132 RH |
8878 | } |
8879 | ||
b34976b6 | 8880 | bfd_boolean |
217aa764 | 8881 | _bfd_elf_close_and_cleanup (bfd *abfd) |
252b5132 | 8882 | { |
d9071b0c TG |
8883 | struct elf_obj_tdata *tdata = elf_tdata (abfd); |
8884 | if (bfd_get_format (abfd) == bfd_object && tdata != NULL) | |
252b5132 | 8885 | { |
c0355132 | 8886 | if (elf_tdata (abfd)->o != NULL && elf_shstrtab (abfd) != NULL) |
2b0f7ef9 | 8887 | _bfd_elf_strtab_free (elf_shstrtab (abfd)); |
d9071b0c | 8888 | _bfd_dwarf2_cleanup_debug_info (abfd, &tdata->dwarf2_find_line_info); |
252b5132 RH |
8889 | } |
8890 | ||
8891 | return _bfd_generic_close_and_cleanup (abfd); | |
8892 | } | |
8893 | ||
8894 | /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY | |
8895 | in the relocation's offset. Thus we cannot allow any sort of sanity | |
8896 | range-checking to interfere. There is nothing else to do in processing | |
8897 | this reloc. */ | |
8898 | ||
8899 | bfd_reloc_status_type | |
217aa764 AM |
8900 | _bfd_elf_rel_vtable_reloc_fn |
8901 | (bfd *abfd ATTRIBUTE_UNUSED, arelent *re ATTRIBUTE_UNUSED, | |
fc0a2244 | 8902 | struct bfd_symbol *symbol ATTRIBUTE_UNUSED, |
217aa764 AM |
8903 | void *data ATTRIBUTE_UNUSED, asection *is ATTRIBUTE_UNUSED, |
8904 | bfd *obfd ATTRIBUTE_UNUSED, char **errmsg ATTRIBUTE_UNUSED) | |
252b5132 RH |
8905 | { |
8906 | return bfd_reloc_ok; | |
8907 | } | |
252b5132 RH |
8908 | \f |
8909 | /* Elf core file support. Much of this only works on native | |
8910 | toolchains, since we rely on knowing the | |
8911 | machine-dependent procfs structure in order to pick | |
c044fabd | 8912 | out details about the corefile. */ |
252b5132 RH |
8913 | |
8914 | #ifdef HAVE_SYS_PROCFS_H | |
16231b7b DG |
8915 | /* Needed for new procfs interface on sparc-solaris. */ |
8916 | # define _STRUCTURED_PROC 1 | |
252b5132 RH |
8917 | # include <sys/procfs.h> |
8918 | #endif | |
8919 | ||
261b8d08 PA |
8920 | /* Return a PID that identifies a "thread" for threaded cores, or the |
8921 | PID of the main process for non-threaded cores. */ | |
252b5132 RH |
8922 | |
8923 | static int | |
217aa764 | 8924 | elfcore_make_pid (bfd *abfd) |
252b5132 | 8925 | { |
261b8d08 PA |
8926 | int pid; |
8927 | ||
228e534f | 8928 | pid = elf_tdata (abfd)->core->lwpid; |
261b8d08 | 8929 | if (pid == 0) |
228e534f | 8930 | pid = elf_tdata (abfd)->core->pid; |
261b8d08 PA |
8931 | |
8932 | return pid; | |
252b5132 RH |
8933 | } |
8934 | ||
252b5132 RH |
8935 | /* If there isn't a section called NAME, make one, using |
8936 | data from SECT. Note, this function will generate a | |
8937 | reference to NAME, so you shouldn't deallocate or | |
c044fabd | 8938 | overwrite it. */ |
252b5132 | 8939 | |
b34976b6 | 8940 | static bfd_boolean |
217aa764 | 8941 | elfcore_maybe_make_sect (bfd *abfd, char *name, asection *sect) |
252b5132 | 8942 | { |
c044fabd | 8943 | asection *sect2; |
252b5132 RH |
8944 | |
8945 | if (bfd_get_section_by_name (abfd, name) != NULL) | |
b34976b6 | 8946 | return TRUE; |
252b5132 | 8947 | |
117ed4f8 | 8948 | sect2 = bfd_make_section_with_flags (abfd, name, sect->flags); |
252b5132 | 8949 | if (sect2 == NULL) |
b34976b6 | 8950 | return FALSE; |
252b5132 | 8951 | |
eea6121a | 8952 | sect2->size = sect->size; |
252b5132 | 8953 | sect2->filepos = sect->filepos; |
252b5132 | 8954 | sect2->alignment_power = sect->alignment_power; |
b34976b6 | 8955 | return TRUE; |
252b5132 RH |
8956 | } |
8957 | ||
bb0082d6 AM |
8958 | /* Create a pseudosection containing SIZE bytes at FILEPOS. This |
8959 | actually creates up to two pseudosections: | |
8960 | - For the single-threaded case, a section named NAME, unless | |
8961 | such a section already exists. | |
8962 | - For the multi-threaded case, a section named "NAME/PID", where | |
8963 | PID is elfcore_make_pid (abfd). | |
24d3e51b | 8964 | Both pseudosections have identical contents. */ |
b34976b6 | 8965 | bfd_boolean |
217aa764 AM |
8966 | _bfd_elfcore_make_pseudosection (bfd *abfd, |
8967 | char *name, | |
8968 | size_t size, | |
8969 | ufile_ptr filepos) | |
bb0082d6 AM |
8970 | { |
8971 | char buf[100]; | |
8972 | char *threaded_name; | |
d4c88bbb | 8973 | size_t len; |
bb0082d6 AM |
8974 | asection *sect; |
8975 | ||
8976 | /* Build the section name. */ | |
8977 | ||
8978 | sprintf (buf, "%s/%d", name, elfcore_make_pid (abfd)); | |
d4c88bbb | 8979 | len = strlen (buf) + 1; |
a50b1753 | 8980 | threaded_name = (char *) bfd_alloc (abfd, len); |
bb0082d6 | 8981 | if (threaded_name == NULL) |
b34976b6 | 8982 | return FALSE; |
d4c88bbb | 8983 | memcpy (threaded_name, buf, len); |
bb0082d6 | 8984 | |
117ed4f8 AM |
8985 | sect = bfd_make_section_anyway_with_flags (abfd, threaded_name, |
8986 | SEC_HAS_CONTENTS); | |
bb0082d6 | 8987 | if (sect == NULL) |
b34976b6 | 8988 | return FALSE; |
eea6121a | 8989 | sect->size = size; |
bb0082d6 | 8990 | sect->filepos = filepos; |
bb0082d6 AM |
8991 | sect->alignment_power = 2; |
8992 | ||
936e320b | 8993 | return elfcore_maybe_make_sect (abfd, name, sect); |
bb0082d6 AM |
8994 | } |
8995 | ||
252b5132 | 8996 | /* prstatus_t exists on: |
4a938328 | 8997 | solaris 2.5+ |
252b5132 RH |
8998 | linux 2.[01] + glibc |
8999 | unixware 4.2 | |
9000 | */ | |
9001 | ||
9002 | #if defined (HAVE_PRSTATUS_T) | |
a7b97311 | 9003 | |
b34976b6 | 9004 | static bfd_boolean |
217aa764 | 9005 | elfcore_grok_prstatus (bfd *abfd, Elf_Internal_Note *note) |
252b5132 | 9006 | { |
eea6121a | 9007 | size_t size; |
7ee38065 | 9008 | int offset; |
252b5132 | 9009 | |
4a938328 MS |
9010 | if (note->descsz == sizeof (prstatus_t)) |
9011 | { | |
9012 | prstatus_t prstat; | |
252b5132 | 9013 | |
eea6121a | 9014 | size = sizeof (prstat.pr_reg); |
7ee38065 | 9015 | offset = offsetof (prstatus_t, pr_reg); |
4a938328 | 9016 | memcpy (&prstat, note->descdata, sizeof (prstat)); |
252b5132 | 9017 | |
fa49d224 NC |
9018 | /* Do not overwrite the core signal if it |
9019 | has already been set by another thread. */ | |
228e534f AM |
9020 | if (elf_tdata (abfd)->core->signal == 0) |
9021 | elf_tdata (abfd)->core->signal = prstat.pr_cursig; | |
9022 | if (elf_tdata (abfd)->core->pid == 0) | |
9023 | elf_tdata (abfd)->core->pid = prstat.pr_pid; | |
252b5132 | 9024 | |
4a938328 MS |
9025 | /* pr_who exists on: |
9026 | solaris 2.5+ | |
9027 | unixware 4.2 | |
9028 | pr_who doesn't exist on: | |
9029 | linux 2.[01] | |
9030 | */ | |
252b5132 | 9031 | #if defined (HAVE_PRSTATUS_T_PR_WHO) |
228e534f | 9032 | elf_tdata (abfd)->core->lwpid = prstat.pr_who; |
261b8d08 | 9033 | #else |
228e534f | 9034 | elf_tdata (abfd)->core->lwpid = prstat.pr_pid; |
252b5132 | 9035 | #endif |
4a938328 | 9036 | } |
7ee38065 | 9037 | #if defined (HAVE_PRSTATUS32_T) |
4a938328 MS |
9038 | else if (note->descsz == sizeof (prstatus32_t)) |
9039 | { | |
9040 | /* 64-bit host, 32-bit corefile */ | |
9041 | prstatus32_t prstat; | |
9042 | ||
eea6121a | 9043 | size = sizeof (prstat.pr_reg); |
7ee38065 | 9044 | offset = offsetof (prstatus32_t, pr_reg); |
4a938328 MS |
9045 | memcpy (&prstat, note->descdata, sizeof (prstat)); |
9046 | ||
fa49d224 NC |
9047 | /* Do not overwrite the core signal if it |
9048 | has already been set by another thread. */ | |
228e534f AM |
9049 | if (elf_tdata (abfd)->core->signal == 0) |
9050 | elf_tdata (abfd)->core->signal = prstat.pr_cursig; | |
9051 | if (elf_tdata (abfd)->core->pid == 0) | |
9052 | elf_tdata (abfd)->core->pid = prstat.pr_pid; | |
4a938328 MS |
9053 | |
9054 | /* pr_who exists on: | |
9055 | solaris 2.5+ | |
9056 | unixware 4.2 | |
9057 | pr_who doesn't exist on: | |
9058 | linux 2.[01] | |
9059 | */ | |
7ee38065 | 9060 | #if defined (HAVE_PRSTATUS32_T_PR_WHO) |
228e534f | 9061 | elf_tdata (abfd)->core->lwpid = prstat.pr_who; |
261b8d08 | 9062 | #else |
228e534f | 9063 | elf_tdata (abfd)->core->lwpid = prstat.pr_pid; |
4a938328 MS |
9064 | #endif |
9065 | } | |
7ee38065 | 9066 | #endif /* HAVE_PRSTATUS32_T */ |
4a938328 MS |
9067 | else |
9068 | { | |
9069 | /* Fail - we don't know how to handle any other | |
9070 | note size (ie. data object type). */ | |
b34976b6 | 9071 | return TRUE; |
4a938328 | 9072 | } |
252b5132 | 9073 | |
bb0082d6 | 9074 | /* Make a ".reg/999" section and a ".reg" section. */ |
936e320b | 9075 | return _bfd_elfcore_make_pseudosection (abfd, ".reg", |
eea6121a | 9076 | size, note->descpos + offset); |
252b5132 RH |
9077 | } |
9078 | #endif /* defined (HAVE_PRSTATUS_T) */ | |
9079 | ||
bb0082d6 | 9080 | /* Create a pseudosection containing the exact contents of NOTE. */ |
b34976b6 | 9081 | static bfd_boolean |
217aa764 AM |
9082 | elfcore_make_note_pseudosection (bfd *abfd, |
9083 | char *name, | |
9084 | Elf_Internal_Note *note) | |
252b5132 | 9085 | { |
936e320b AM |
9086 | return _bfd_elfcore_make_pseudosection (abfd, name, |
9087 | note->descsz, note->descpos); | |
252b5132 RH |
9088 | } |
9089 | ||
ff08c6bb JB |
9090 | /* There isn't a consistent prfpregset_t across platforms, |
9091 | but it doesn't matter, because we don't have to pick this | |
c044fabd KH |
9092 | data structure apart. */ |
9093 | ||
b34976b6 | 9094 | static bfd_boolean |
217aa764 | 9095 | elfcore_grok_prfpreg (bfd *abfd, Elf_Internal_Note *note) |
ff08c6bb JB |
9096 | { |
9097 | return elfcore_make_note_pseudosection (abfd, ".reg2", note); | |
9098 | } | |
9099 | ||
ff08c6bb | 9100 | /* Linux dumps the Intel SSE regs in a note named "LINUX" with a note |
971d4640 | 9101 | type of NT_PRXFPREG. Just include the whole note's contents |
ff08c6bb | 9102 | literally. */ |
c044fabd | 9103 | |
b34976b6 | 9104 | static bfd_boolean |
217aa764 | 9105 | elfcore_grok_prxfpreg (bfd *abfd, Elf_Internal_Note *note) |
ff08c6bb JB |
9106 | { |
9107 | return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note); | |
9108 | } | |
9109 | ||
4339cae0 L |
9110 | /* Linux dumps the Intel XSAVE extended state in a note named "LINUX" |
9111 | with a note type of NT_X86_XSTATE. Just include the whole note's | |
9112 | contents literally. */ | |
9113 | ||
9114 | static bfd_boolean | |
9115 | elfcore_grok_xstatereg (bfd *abfd, Elf_Internal_Note *note) | |
9116 | { | |
9117 | return elfcore_make_note_pseudosection (abfd, ".reg-xstate", note); | |
9118 | } | |
9119 | ||
97753bd5 AM |
9120 | static bfd_boolean |
9121 | elfcore_grok_ppc_vmx (bfd *abfd, Elf_Internal_Note *note) | |
9122 | { | |
9123 | return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vmx", note); | |
9124 | } | |
9125 | ||
89eeb0bc LM |
9126 | static bfd_boolean |
9127 | elfcore_grok_ppc_vsx (bfd *abfd, Elf_Internal_Note *note) | |
9128 | { | |
9129 | return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vsx", note); | |
9130 | } | |
97753bd5 | 9131 | |
0675e188 UW |
9132 | static bfd_boolean |
9133 | elfcore_grok_s390_high_gprs (bfd *abfd, Elf_Internal_Note *note) | |
9134 | { | |
9135 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-high-gprs", note); | |
9136 | } | |
9137 | ||
d7eeb400 MS |
9138 | static bfd_boolean |
9139 | elfcore_grok_s390_timer (bfd *abfd, Elf_Internal_Note *note) | |
9140 | { | |
9141 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-timer", note); | |
9142 | } | |
9143 | ||
9144 | static bfd_boolean | |
9145 | elfcore_grok_s390_todcmp (bfd *abfd, Elf_Internal_Note *note) | |
9146 | { | |
9147 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-todcmp", note); | |
9148 | } | |
9149 | ||
9150 | static bfd_boolean | |
9151 | elfcore_grok_s390_todpreg (bfd *abfd, Elf_Internal_Note *note) | |
9152 | { | |
9153 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-todpreg", note); | |
9154 | } | |
9155 | ||
9156 | static bfd_boolean | |
9157 | elfcore_grok_s390_ctrs (bfd *abfd, Elf_Internal_Note *note) | |
9158 | { | |
9159 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-ctrs", note); | |
9160 | } | |
9161 | ||
9162 | static bfd_boolean | |
9163 | elfcore_grok_s390_prefix (bfd *abfd, Elf_Internal_Note *note) | |
9164 | { | |
9165 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-prefix", note); | |
9166 | } | |
9167 | ||
355b81d9 UW |
9168 | static bfd_boolean |
9169 | elfcore_grok_s390_last_break (bfd *abfd, Elf_Internal_Note *note) | |
9170 | { | |
9171 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-last-break", note); | |
9172 | } | |
9173 | ||
9174 | static bfd_boolean | |
9175 | elfcore_grok_s390_system_call (bfd *abfd, Elf_Internal_Note *note) | |
9176 | { | |
9177 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-system-call", note); | |
9178 | } | |
9179 | ||
abb3f6cc NC |
9180 | static bfd_boolean |
9181 | elfcore_grok_s390_tdb (bfd *abfd, Elf_Internal_Note *note) | |
9182 | { | |
9183 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-tdb", note); | |
9184 | } | |
9185 | ||
4ef9f41a AA |
9186 | static bfd_boolean |
9187 | elfcore_grok_s390_vxrs_low (bfd *abfd, Elf_Internal_Note *note) | |
9188 | { | |
9189 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-vxrs-low", note); | |
9190 | } | |
9191 | ||
9192 | static bfd_boolean | |
9193 | elfcore_grok_s390_vxrs_high (bfd *abfd, Elf_Internal_Note *note) | |
9194 | { | |
9195 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-vxrs-high", note); | |
9196 | } | |
9197 | ||
88ab90e8 AA |
9198 | static bfd_boolean |
9199 | elfcore_grok_s390_gs_cb (bfd *abfd, Elf_Internal_Note *note) | |
9200 | { | |
9201 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-gs-cb", note); | |
9202 | } | |
9203 | ||
9204 | static bfd_boolean | |
9205 | elfcore_grok_s390_gs_bc (bfd *abfd, Elf_Internal_Note *note) | |
9206 | { | |
9207 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-gs-bc", note); | |
9208 | } | |
9209 | ||
faa9a424 UW |
9210 | static bfd_boolean |
9211 | elfcore_grok_arm_vfp (bfd *abfd, Elf_Internal_Note *note) | |
9212 | { | |
9213 | return elfcore_make_note_pseudosection (abfd, ".reg-arm-vfp", note); | |
9214 | } | |
9215 | ||
652451f8 YZ |
9216 | static bfd_boolean |
9217 | elfcore_grok_aarch_tls (bfd *abfd, Elf_Internal_Note *note) | |
9218 | { | |
9219 | return elfcore_make_note_pseudosection (abfd, ".reg-aarch-tls", note); | |
9220 | } | |
9221 | ||
9222 | static bfd_boolean | |
9223 | elfcore_grok_aarch_hw_break (bfd *abfd, Elf_Internal_Note *note) | |
9224 | { | |
9225 | return elfcore_make_note_pseudosection (abfd, ".reg-aarch-hw-break", note); | |
9226 | } | |
9227 | ||
9228 | static bfd_boolean | |
9229 | elfcore_grok_aarch_hw_watch (bfd *abfd, Elf_Internal_Note *note) | |
9230 | { | |
9231 | return elfcore_make_note_pseudosection (abfd, ".reg-aarch-hw-watch", note); | |
9232 | } | |
9233 | ||
252b5132 | 9234 | #if defined (HAVE_PRPSINFO_T) |
4a938328 | 9235 | typedef prpsinfo_t elfcore_psinfo_t; |
7ee38065 | 9236 | #if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */ |
4a938328 MS |
9237 | typedef prpsinfo32_t elfcore_psinfo32_t; |
9238 | #endif | |
252b5132 RH |
9239 | #endif |
9240 | ||
9241 | #if defined (HAVE_PSINFO_T) | |
4a938328 | 9242 | typedef psinfo_t elfcore_psinfo_t; |
7ee38065 | 9243 | #if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */ |
4a938328 MS |
9244 | typedef psinfo32_t elfcore_psinfo32_t; |
9245 | #endif | |
252b5132 RH |
9246 | #endif |
9247 | ||
252b5132 RH |
9248 | /* return a malloc'ed copy of a string at START which is at |
9249 | most MAX bytes long, possibly without a terminating '\0'. | |
c044fabd | 9250 | the copy will always have a terminating '\0'. */ |
252b5132 | 9251 | |
936e320b | 9252 | char * |
217aa764 | 9253 | _bfd_elfcore_strndup (bfd *abfd, char *start, size_t max) |
252b5132 | 9254 | { |
dc810e39 | 9255 | char *dups; |
a50b1753 | 9256 | char *end = (char *) memchr (start, '\0', max); |
dc810e39 | 9257 | size_t len; |
252b5132 RH |
9258 | |
9259 | if (end == NULL) | |
9260 | len = max; | |
9261 | else | |
9262 | len = end - start; | |
9263 | ||
a50b1753 | 9264 | dups = (char *) bfd_alloc (abfd, len + 1); |
dc810e39 | 9265 | if (dups == NULL) |
252b5132 RH |
9266 | return NULL; |
9267 | ||
dc810e39 AM |
9268 | memcpy (dups, start, len); |
9269 | dups[len] = '\0'; | |
252b5132 | 9270 | |
dc810e39 | 9271 | return dups; |
252b5132 RH |
9272 | } |
9273 | ||
bb0082d6 | 9274 | #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) |
b34976b6 | 9275 | static bfd_boolean |
217aa764 | 9276 | elfcore_grok_psinfo (bfd *abfd, Elf_Internal_Note *note) |
252b5132 | 9277 | { |
4a938328 MS |
9278 | if (note->descsz == sizeof (elfcore_psinfo_t)) |
9279 | { | |
9280 | elfcore_psinfo_t psinfo; | |
252b5132 | 9281 | |
7ee38065 | 9282 | memcpy (&psinfo, note->descdata, sizeof (psinfo)); |
252b5132 | 9283 | |
335e41d4 | 9284 | #if defined (HAVE_PSINFO_T_PR_PID) || defined (HAVE_PRPSINFO_T_PR_PID) |
228e534f | 9285 | elf_tdata (abfd)->core->pid = psinfo.pr_pid; |
335e41d4 | 9286 | #endif |
228e534f | 9287 | elf_tdata (abfd)->core->program |
936e320b AM |
9288 | = _bfd_elfcore_strndup (abfd, psinfo.pr_fname, |
9289 | sizeof (psinfo.pr_fname)); | |
252b5132 | 9290 | |
228e534f | 9291 | elf_tdata (abfd)->core->command |
936e320b AM |
9292 | = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs, |
9293 | sizeof (psinfo.pr_psargs)); | |
4a938328 | 9294 | } |
7ee38065 | 9295 | #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T) |
4a938328 MS |
9296 | else if (note->descsz == sizeof (elfcore_psinfo32_t)) |
9297 | { | |
9298 | /* 64-bit host, 32-bit corefile */ | |
9299 | elfcore_psinfo32_t psinfo; | |
9300 | ||
7ee38065 | 9301 | memcpy (&psinfo, note->descdata, sizeof (psinfo)); |
252b5132 | 9302 | |
335e41d4 | 9303 | #if defined (HAVE_PSINFO32_T_PR_PID) || defined (HAVE_PRPSINFO32_T_PR_PID) |
228e534f | 9304 | elf_tdata (abfd)->core->pid = psinfo.pr_pid; |
335e41d4 | 9305 | #endif |
228e534f | 9306 | elf_tdata (abfd)->core->program |
936e320b AM |
9307 | = _bfd_elfcore_strndup (abfd, psinfo.pr_fname, |
9308 | sizeof (psinfo.pr_fname)); | |
4a938328 | 9309 | |
228e534f | 9310 | elf_tdata (abfd)->core->command |
936e320b AM |
9311 | = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs, |
9312 | sizeof (psinfo.pr_psargs)); | |
4a938328 MS |
9313 | } |
9314 | #endif | |
9315 | ||
9316 | else | |
9317 | { | |
9318 | /* Fail - we don't know how to handle any other | |
9319 | note size (ie. data object type). */ | |
b34976b6 | 9320 | return TRUE; |
4a938328 | 9321 | } |
252b5132 RH |
9322 | |
9323 | /* Note that for some reason, a spurious space is tacked | |
9324 | onto the end of the args in some (at least one anyway) | |
c044fabd | 9325 | implementations, so strip it off if it exists. */ |
252b5132 RH |
9326 | |
9327 | { | |
228e534f | 9328 | char *command = elf_tdata (abfd)->core->command; |
252b5132 RH |
9329 | int n = strlen (command); |
9330 | ||
9331 | if (0 < n && command[n - 1] == ' ') | |
9332 | command[n - 1] = '\0'; | |
9333 | } | |
9334 | ||
b34976b6 | 9335 | return TRUE; |
252b5132 RH |
9336 | } |
9337 | #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */ | |
9338 | ||
252b5132 | 9339 | #if defined (HAVE_PSTATUS_T) |
b34976b6 | 9340 | static bfd_boolean |
217aa764 | 9341 | elfcore_grok_pstatus (bfd *abfd, Elf_Internal_Note *note) |
252b5132 | 9342 | { |
f572a39d AM |
9343 | if (note->descsz == sizeof (pstatus_t) |
9344 | #if defined (HAVE_PXSTATUS_T) | |
9345 | || note->descsz == sizeof (pxstatus_t) | |
9346 | #endif | |
9347 | ) | |
4a938328 MS |
9348 | { |
9349 | pstatus_t pstat; | |
252b5132 | 9350 | |
4a938328 | 9351 | memcpy (&pstat, note->descdata, sizeof (pstat)); |
252b5132 | 9352 | |
228e534f | 9353 | elf_tdata (abfd)->core->pid = pstat.pr_pid; |
4a938328 | 9354 | } |
7ee38065 | 9355 | #if defined (HAVE_PSTATUS32_T) |
4a938328 MS |
9356 | else if (note->descsz == sizeof (pstatus32_t)) |
9357 | { | |
9358 | /* 64-bit host, 32-bit corefile */ | |
9359 | pstatus32_t pstat; | |
252b5132 | 9360 | |
4a938328 | 9361 | memcpy (&pstat, note->descdata, sizeof (pstat)); |
252b5132 | 9362 | |
228e534f | 9363 | elf_tdata (abfd)->core->pid = pstat.pr_pid; |
4a938328 MS |
9364 | } |
9365 | #endif | |
252b5132 RH |
9366 | /* Could grab some more details from the "representative" |
9367 | lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an | |
c044fabd | 9368 | NT_LWPSTATUS note, presumably. */ |
252b5132 | 9369 | |
b34976b6 | 9370 | return TRUE; |
252b5132 RH |
9371 | } |
9372 | #endif /* defined (HAVE_PSTATUS_T) */ | |
9373 | ||
252b5132 | 9374 | #if defined (HAVE_LWPSTATUS_T) |
b34976b6 | 9375 | static bfd_boolean |
217aa764 | 9376 | elfcore_grok_lwpstatus (bfd *abfd, Elf_Internal_Note *note) |
252b5132 RH |
9377 | { |
9378 | lwpstatus_t lwpstat; | |
9379 | char buf[100]; | |
c044fabd | 9380 | char *name; |
d4c88bbb | 9381 | size_t len; |
c044fabd | 9382 | asection *sect; |
252b5132 | 9383 | |
f572a39d AM |
9384 | if (note->descsz != sizeof (lwpstat) |
9385 | #if defined (HAVE_LWPXSTATUS_T) | |
9386 | && note->descsz != sizeof (lwpxstatus_t) | |
9387 | #endif | |
9388 | ) | |
b34976b6 | 9389 | return TRUE; |
252b5132 RH |
9390 | |
9391 | memcpy (&lwpstat, note->descdata, sizeof (lwpstat)); | |
9392 | ||
228e534f | 9393 | elf_tdata (abfd)->core->lwpid = lwpstat.pr_lwpid; |
a1504221 JB |
9394 | /* Do not overwrite the core signal if it has already been set by |
9395 | another thread. */ | |
228e534f AM |
9396 | if (elf_tdata (abfd)->core->signal == 0) |
9397 | elf_tdata (abfd)->core->signal = lwpstat.pr_cursig; | |
252b5132 | 9398 | |
c044fabd | 9399 | /* Make a ".reg/999" section. */ |
252b5132 RH |
9400 | |
9401 | sprintf (buf, ".reg/%d", elfcore_make_pid (abfd)); | |
d4c88bbb | 9402 | len = strlen (buf) + 1; |
217aa764 | 9403 | name = bfd_alloc (abfd, len); |
252b5132 | 9404 | if (name == NULL) |
b34976b6 | 9405 | return FALSE; |
d4c88bbb | 9406 | memcpy (name, buf, len); |
252b5132 | 9407 | |
117ed4f8 | 9408 | sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); |
252b5132 | 9409 | if (sect == NULL) |
b34976b6 | 9410 | return FALSE; |
252b5132 RH |
9411 | |
9412 | #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT) | |
eea6121a | 9413 | sect->size = sizeof (lwpstat.pr_context.uc_mcontext.gregs); |
252b5132 RH |
9414 | sect->filepos = note->descpos |
9415 | + offsetof (lwpstatus_t, pr_context.uc_mcontext.gregs); | |
9416 | #endif | |
9417 | ||
9418 | #if defined (HAVE_LWPSTATUS_T_PR_REG) | |
eea6121a | 9419 | sect->size = sizeof (lwpstat.pr_reg); |
252b5132 RH |
9420 | sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_reg); |
9421 | #endif | |
9422 | ||
252b5132 RH |
9423 | sect->alignment_power = 2; |
9424 | ||
9425 | if (!elfcore_maybe_make_sect (abfd, ".reg", sect)) | |
b34976b6 | 9426 | return FALSE; |
252b5132 RH |
9427 | |
9428 | /* Make a ".reg2/999" section */ | |
9429 | ||
9430 | sprintf (buf, ".reg2/%d", elfcore_make_pid (abfd)); | |
d4c88bbb | 9431 | len = strlen (buf) + 1; |
217aa764 | 9432 | name = bfd_alloc (abfd, len); |
252b5132 | 9433 | if (name == NULL) |
b34976b6 | 9434 | return FALSE; |
d4c88bbb | 9435 | memcpy (name, buf, len); |
252b5132 | 9436 | |
117ed4f8 | 9437 | sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); |
252b5132 | 9438 | if (sect == NULL) |
b34976b6 | 9439 | return FALSE; |
252b5132 RH |
9440 | |
9441 | #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT) | |
eea6121a | 9442 | sect->size = sizeof (lwpstat.pr_context.uc_mcontext.fpregs); |
252b5132 RH |
9443 | sect->filepos = note->descpos |
9444 | + offsetof (lwpstatus_t, pr_context.uc_mcontext.fpregs); | |
9445 | #endif | |
9446 | ||
9447 | #if defined (HAVE_LWPSTATUS_T_PR_FPREG) | |
eea6121a | 9448 | sect->size = sizeof (lwpstat.pr_fpreg); |
252b5132 RH |
9449 | sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_fpreg); |
9450 | #endif | |
9451 | ||
252b5132 RH |
9452 | sect->alignment_power = 2; |
9453 | ||
936e320b | 9454 | return elfcore_maybe_make_sect (abfd, ".reg2", sect); |
252b5132 RH |
9455 | } |
9456 | #endif /* defined (HAVE_LWPSTATUS_T) */ | |
9457 | ||
b34976b6 | 9458 | static bfd_boolean |
217aa764 | 9459 | elfcore_grok_win32pstatus (bfd *abfd, Elf_Internal_Note *note) |
16e9c715 NC |
9460 | { |
9461 | char buf[30]; | |
c044fabd | 9462 | char *name; |
d4c88bbb | 9463 | size_t len; |
c044fabd | 9464 | asection *sect; |
4a6636fb PA |
9465 | int type; |
9466 | int is_active_thread; | |
9467 | bfd_vma base_addr; | |
16e9c715 | 9468 | |
4a6636fb | 9469 | if (note->descsz < 728) |
b34976b6 | 9470 | return TRUE; |
16e9c715 | 9471 | |
4a6636fb PA |
9472 | if (! CONST_STRNEQ (note->namedata, "win32")) |
9473 | return TRUE; | |
9474 | ||
9475 | type = bfd_get_32 (abfd, note->descdata); | |
c044fabd | 9476 | |
4a6636fb | 9477 | switch (type) |
16e9c715 | 9478 | { |
4a6636fb | 9479 | case 1 /* NOTE_INFO_PROCESS */: |
228e534f | 9480 | /* FIXME: need to add ->core->command. */ |
4a6636fb | 9481 | /* process_info.pid */ |
228e534f | 9482 | elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, note->descdata + 8); |
4a6636fb | 9483 | /* process_info.signal */ |
228e534f | 9484 | elf_tdata (abfd)->core->signal = bfd_get_32 (abfd, note->descdata + 12); |
c044fabd | 9485 | break; |
16e9c715 | 9486 | |
4a6636fb | 9487 | case 2 /* NOTE_INFO_THREAD */: |
16e9c715 | 9488 | /* Make a ".reg/999" section. */ |
4a6636fb PA |
9489 | /* thread_info.tid */ |
9490 | sprintf (buf, ".reg/%ld", (long) bfd_get_32 (abfd, note->descdata + 8)); | |
c044fabd | 9491 | |
d4c88bbb | 9492 | len = strlen (buf) + 1; |
a50b1753 | 9493 | name = (char *) bfd_alloc (abfd, len); |
16e9c715 | 9494 | if (name == NULL) |
b34976b6 | 9495 | return FALSE; |
c044fabd | 9496 | |
d4c88bbb | 9497 | memcpy (name, buf, len); |
16e9c715 | 9498 | |
117ed4f8 | 9499 | sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); |
16e9c715 | 9500 | if (sect == NULL) |
b34976b6 | 9501 | return FALSE; |
c044fabd | 9502 | |
4a6636fb PA |
9503 | /* sizeof (thread_info.thread_context) */ |
9504 | sect->size = 716; | |
9505 | /* offsetof (thread_info.thread_context) */ | |
9506 | sect->filepos = note->descpos + 12; | |
16e9c715 NC |
9507 | sect->alignment_power = 2; |
9508 | ||
4a6636fb PA |
9509 | /* thread_info.is_active_thread */ |
9510 | is_active_thread = bfd_get_32 (abfd, note->descdata + 8); | |
9511 | ||
9512 | if (is_active_thread) | |
16e9c715 | 9513 | if (! elfcore_maybe_make_sect (abfd, ".reg", sect)) |
b34976b6 | 9514 | return FALSE; |
16e9c715 NC |
9515 | break; |
9516 | ||
4a6636fb | 9517 | case 3 /* NOTE_INFO_MODULE */: |
16e9c715 | 9518 | /* Make a ".module/xxxxxxxx" section. */ |
4a6636fb PA |
9519 | /* module_info.base_address */ |
9520 | base_addr = bfd_get_32 (abfd, note->descdata + 4); | |
0af1713e | 9521 | sprintf (buf, ".module/%08lx", (unsigned long) base_addr); |
c044fabd | 9522 | |
d4c88bbb | 9523 | len = strlen (buf) + 1; |
a50b1753 | 9524 | name = (char *) bfd_alloc (abfd, len); |
16e9c715 | 9525 | if (name == NULL) |
b34976b6 | 9526 | return FALSE; |
c044fabd | 9527 | |
d4c88bbb | 9528 | memcpy (name, buf, len); |
252b5132 | 9529 | |
117ed4f8 | 9530 | sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); |
c044fabd | 9531 | |
16e9c715 | 9532 | if (sect == NULL) |
b34976b6 | 9533 | return FALSE; |
c044fabd | 9534 | |
eea6121a | 9535 | sect->size = note->descsz; |
16e9c715 | 9536 | sect->filepos = note->descpos; |
16e9c715 NC |
9537 | sect->alignment_power = 2; |
9538 | break; | |
9539 | ||
9540 | default: | |
b34976b6 | 9541 | return TRUE; |
16e9c715 NC |
9542 | } |
9543 | ||
b34976b6 | 9544 | return TRUE; |
16e9c715 | 9545 | } |
252b5132 | 9546 | |
b34976b6 | 9547 | static bfd_boolean |
217aa764 | 9548 | elfcore_grok_note (bfd *abfd, Elf_Internal_Note *note) |
252b5132 | 9549 | { |
9c5bfbb7 | 9550 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
bb0082d6 | 9551 | |
252b5132 RH |
9552 | switch (note->type) |
9553 | { | |
9554 | default: | |
b34976b6 | 9555 | return TRUE; |
252b5132 | 9556 | |
252b5132 | 9557 | case NT_PRSTATUS: |
bb0082d6 AM |
9558 | if (bed->elf_backend_grok_prstatus) |
9559 | if ((*bed->elf_backend_grok_prstatus) (abfd, note)) | |
b34976b6 | 9560 | return TRUE; |
bb0082d6 | 9561 | #if defined (HAVE_PRSTATUS_T) |
252b5132 | 9562 | return elfcore_grok_prstatus (abfd, note); |
bb0082d6 | 9563 | #else |
b34976b6 | 9564 | return TRUE; |
252b5132 RH |
9565 | #endif |
9566 | ||
9567 | #if defined (HAVE_PSTATUS_T) | |
9568 | case NT_PSTATUS: | |
9569 | return elfcore_grok_pstatus (abfd, note); | |
9570 | #endif | |
9571 | ||
9572 | #if defined (HAVE_LWPSTATUS_T) | |
9573 | case NT_LWPSTATUS: | |
9574 | return elfcore_grok_lwpstatus (abfd, note); | |
9575 | #endif | |
9576 | ||
9577 | case NT_FPREGSET: /* FIXME: rename to NT_PRFPREG */ | |
9578 | return elfcore_grok_prfpreg (abfd, note); | |
9579 | ||
c044fabd | 9580 | case NT_WIN32PSTATUS: |
16e9c715 | 9581 | return elfcore_grok_win32pstatus (abfd, note); |
16e9c715 | 9582 | |
c044fabd | 9583 | case NT_PRXFPREG: /* Linux SSE extension */ |
e377ab71 MK |
9584 | if (note->namesz == 6 |
9585 | && strcmp (note->namedata, "LINUX") == 0) | |
ff08c6bb JB |
9586 | return elfcore_grok_prxfpreg (abfd, note); |
9587 | else | |
b34976b6 | 9588 | return TRUE; |
ff08c6bb | 9589 | |
4339cae0 L |
9590 | case NT_X86_XSTATE: /* Linux XSAVE extension */ |
9591 | if (note->namesz == 6 | |
9592 | && strcmp (note->namedata, "LINUX") == 0) | |
9593 | return elfcore_grok_xstatereg (abfd, note); | |
9594 | else | |
9595 | return TRUE; | |
9596 | ||
97753bd5 AM |
9597 | case NT_PPC_VMX: |
9598 | if (note->namesz == 6 | |
9599 | && strcmp (note->namedata, "LINUX") == 0) | |
9600 | return elfcore_grok_ppc_vmx (abfd, note); | |
9601 | else | |
9602 | return TRUE; | |
9603 | ||
89eeb0bc LM |
9604 | case NT_PPC_VSX: |
9605 | if (note->namesz == 6 | |
9606 | && strcmp (note->namedata, "LINUX") == 0) | |
9607 | return elfcore_grok_ppc_vsx (abfd, note); | |
9608 | else | |
9609 | return TRUE; | |
9610 | ||
0675e188 UW |
9611 | case NT_S390_HIGH_GPRS: |
9612 | if (note->namesz == 6 | |
9613 | && strcmp (note->namedata, "LINUX") == 0) | |
9614 | return elfcore_grok_s390_high_gprs (abfd, note); | |
9615 | else | |
9616 | return TRUE; | |
9617 | ||
d7eeb400 MS |
9618 | case NT_S390_TIMER: |
9619 | if (note->namesz == 6 | |
9620 | && strcmp (note->namedata, "LINUX") == 0) | |
9621 | return elfcore_grok_s390_timer (abfd, note); | |
9622 | else | |
9623 | return TRUE; | |
9624 | ||
9625 | case NT_S390_TODCMP: | |
9626 | if (note->namesz == 6 | |
9627 | && strcmp (note->namedata, "LINUX") == 0) | |
9628 | return elfcore_grok_s390_todcmp (abfd, note); | |
9629 | else | |
9630 | return TRUE; | |
9631 | ||
9632 | case NT_S390_TODPREG: | |
9633 | if (note->namesz == 6 | |
9634 | && strcmp (note->namedata, "LINUX") == 0) | |
9635 | return elfcore_grok_s390_todpreg (abfd, note); | |
9636 | else | |
9637 | return TRUE; | |
9638 | ||
9639 | case NT_S390_CTRS: | |
9640 | if (note->namesz == 6 | |
9641 | && strcmp (note->namedata, "LINUX") == 0) | |
9642 | return elfcore_grok_s390_ctrs (abfd, note); | |
9643 | else | |
9644 | return TRUE; | |
9645 | ||
9646 | case NT_S390_PREFIX: | |
9647 | if (note->namesz == 6 | |
9648 | && strcmp (note->namedata, "LINUX") == 0) | |
9649 | return elfcore_grok_s390_prefix (abfd, note); | |
9650 | else | |
9651 | return TRUE; | |
9652 | ||
355b81d9 UW |
9653 | case NT_S390_LAST_BREAK: |
9654 | if (note->namesz == 6 | |
9655 | && strcmp (note->namedata, "LINUX") == 0) | |
9656 | return elfcore_grok_s390_last_break (abfd, note); | |
9657 | else | |
9658 | return TRUE; | |
9659 | ||
9660 | case NT_S390_SYSTEM_CALL: | |
9661 | if (note->namesz == 6 | |
9662 | && strcmp (note->namedata, "LINUX") == 0) | |
9663 | return elfcore_grok_s390_system_call (abfd, note); | |
9664 | else | |
9665 | return TRUE; | |
9666 | ||
abb3f6cc NC |
9667 | case NT_S390_TDB: |
9668 | if (note->namesz == 6 | |
9669 | && strcmp (note->namedata, "LINUX") == 0) | |
9670 | return elfcore_grok_s390_tdb (abfd, note); | |
9671 | else | |
9672 | return TRUE; | |
9673 | ||
4ef9f41a AA |
9674 | case NT_S390_VXRS_LOW: |
9675 | if (note->namesz == 6 | |
9676 | && strcmp (note->namedata, "LINUX") == 0) | |
9677 | return elfcore_grok_s390_vxrs_low (abfd, note); | |
9678 | else | |
9679 | return TRUE; | |
9680 | ||
9681 | case NT_S390_VXRS_HIGH: | |
9682 | if (note->namesz == 6 | |
9683 | && strcmp (note->namedata, "LINUX") == 0) | |
9684 | return elfcore_grok_s390_vxrs_high (abfd, note); | |
9685 | else | |
9686 | return TRUE; | |
9687 | ||
88ab90e8 AA |
9688 | case NT_S390_GS_CB: |
9689 | if (note->namesz == 6 | |
9690 | && strcmp (note->namedata, "LINUX") == 0) | |
9691 | return elfcore_grok_s390_gs_bc (abfd, note); | |
9692 | else | |
9693 | return TRUE; | |
9694 | ||
9695 | case NT_S390_GS_BC: | |
9696 | if (note->namesz == 6 | |
9697 | && strcmp (note->namedata, "LINUX") == 0) | |
9698 | return elfcore_grok_s390_gs_cb (abfd, note); | |
9699 | else | |
9700 | return TRUE; | |
9701 | ||
faa9a424 UW |
9702 | case NT_ARM_VFP: |
9703 | if (note->namesz == 6 | |
9704 | && strcmp (note->namedata, "LINUX") == 0) | |
9705 | return elfcore_grok_arm_vfp (abfd, note); | |
9706 | else | |
9707 | return TRUE; | |
9708 | ||
652451f8 YZ |
9709 | case NT_ARM_TLS: |
9710 | if (note->namesz == 6 | |
9711 | && strcmp (note->namedata, "LINUX") == 0) | |
9712 | return elfcore_grok_aarch_tls (abfd, note); | |
9713 | else | |
9714 | return TRUE; | |
9715 | ||
9716 | case NT_ARM_HW_BREAK: | |
9717 | if (note->namesz == 6 | |
9718 | && strcmp (note->namedata, "LINUX") == 0) | |
9719 | return elfcore_grok_aarch_hw_break (abfd, note); | |
9720 | else | |
9721 | return TRUE; | |
9722 | ||
9723 | case NT_ARM_HW_WATCH: | |
9724 | if (note->namesz == 6 | |
9725 | && strcmp (note->namedata, "LINUX") == 0) | |
9726 | return elfcore_grok_aarch_hw_watch (abfd, note); | |
9727 | else | |
9728 | return TRUE; | |
9729 | ||
252b5132 RH |
9730 | case NT_PRPSINFO: |
9731 | case NT_PSINFO: | |
bb0082d6 AM |
9732 | if (bed->elf_backend_grok_psinfo) |
9733 | if ((*bed->elf_backend_grok_psinfo) (abfd, note)) | |
b34976b6 | 9734 | return TRUE; |
bb0082d6 | 9735 | #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) |
252b5132 | 9736 | return elfcore_grok_psinfo (abfd, note); |
bb0082d6 | 9737 | #else |
b34976b6 | 9738 | return TRUE; |
252b5132 | 9739 | #endif |
3333a7c3 RM |
9740 | |
9741 | case NT_AUXV: | |
9742 | { | |
117ed4f8 AM |
9743 | asection *sect = bfd_make_section_anyway_with_flags (abfd, ".auxv", |
9744 | SEC_HAS_CONTENTS); | |
3333a7c3 RM |
9745 | |
9746 | if (sect == NULL) | |
9747 | return FALSE; | |
eea6121a | 9748 | sect->size = note->descsz; |
3333a7c3 | 9749 | sect->filepos = note->descpos; |
3333a7c3 RM |
9750 | sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32; |
9751 | ||
9752 | return TRUE; | |
9753 | } | |
9015683b | 9754 | |
451b7c33 TT |
9755 | case NT_FILE: |
9756 | return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.file", | |
9757 | note); | |
9758 | ||
9015683b TT |
9759 | case NT_SIGINFO: |
9760 | return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.siginfo", | |
9761 | note); | |
5b2c414d | 9762 | |
252b5132 RH |
9763 | } |
9764 | } | |
9765 | ||
718175fa JK |
9766 | static bfd_boolean |
9767 | elfobj_grok_gnu_build_id (bfd *abfd, Elf_Internal_Note *note) | |
9768 | { | |
c74f7d1c | 9769 | struct bfd_build_id* build_id; |
30e8ee25 AM |
9770 | |
9771 | if (note->descsz == 0) | |
9772 | return FALSE; | |
9773 | ||
c74f7d1c JT |
9774 | build_id = bfd_alloc (abfd, sizeof (struct bfd_build_id) - 1 + note->descsz); |
9775 | if (build_id == NULL) | |
718175fa JK |
9776 | return FALSE; |
9777 | ||
c74f7d1c JT |
9778 | build_id->size = note->descsz; |
9779 | memcpy (build_id->data, note->descdata, note->descsz); | |
9780 | abfd->build_id = build_id; | |
718175fa JK |
9781 | |
9782 | return TRUE; | |
9783 | } | |
9784 | ||
9785 | static bfd_boolean | |
9786 | elfobj_grok_gnu_note (bfd *abfd, Elf_Internal_Note *note) | |
9787 | { | |
9788 | switch (note->type) | |
9789 | { | |
9790 | default: | |
9791 | return TRUE; | |
9792 | ||
46bed679 L |
9793 | case NT_GNU_PROPERTY_TYPE_0: |
9794 | return _bfd_elf_parse_gnu_properties (abfd, note); | |
9795 | ||
718175fa JK |
9796 | case NT_GNU_BUILD_ID: |
9797 | return elfobj_grok_gnu_build_id (abfd, note); | |
9798 | } | |
9799 | } | |
9800 | ||
e21e5835 NC |
9801 | static bfd_boolean |
9802 | elfobj_grok_stapsdt_note_1 (bfd *abfd, Elf_Internal_Note *note) | |
9803 | { | |
9804 | struct sdt_note *cur = | |
9805 | (struct sdt_note *) bfd_alloc (abfd, sizeof (struct sdt_note) | |
9806 | + note->descsz); | |
9807 | ||
9808 | cur->next = (struct sdt_note *) (elf_tdata (abfd))->sdt_note_head; | |
9809 | cur->size = (bfd_size_type) note->descsz; | |
9810 | memcpy (cur->data, note->descdata, note->descsz); | |
9811 | ||
9812 | elf_tdata (abfd)->sdt_note_head = cur; | |
9813 | ||
9814 | return TRUE; | |
9815 | } | |
9816 | ||
9817 | static bfd_boolean | |
9818 | elfobj_grok_stapsdt_note (bfd *abfd, Elf_Internal_Note *note) | |
9819 | { | |
9820 | switch (note->type) | |
9821 | { | |
9822 | case NT_STAPSDT: | |
9823 | return elfobj_grok_stapsdt_note_1 (abfd, note); | |
9824 | ||
9825 | default: | |
9826 | return TRUE; | |
9827 | } | |
9828 | } | |
9829 | ||
aa1ed4a9 JB |
9830 | static bfd_boolean |
9831 | elfcore_grok_freebsd_psinfo (bfd *abfd, Elf_Internal_Note *note) | |
9832 | { | |
9833 | size_t offset; | |
9834 | ||
b5430a3c | 9835 | switch (elf_elfheader (abfd)->e_ident[EI_CLASS]) |
aa1ed4a9 | 9836 | { |
b5430a3c | 9837 | case ELFCLASS32: |
0064d223 JB |
9838 | if (note->descsz < 108) |
9839 | return FALSE; | |
aa1ed4a9 JB |
9840 | break; |
9841 | ||
b5430a3c | 9842 | case ELFCLASS64: |
0064d223 JB |
9843 | if (note->descsz < 120) |
9844 | return FALSE; | |
aa1ed4a9 JB |
9845 | break; |
9846 | ||
9847 | default: | |
9848 | return FALSE; | |
9849 | } | |
9850 | ||
0064d223 JB |
9851 | /* Check for version 1 in pr_version. */ |
9852 | if (bfd_h_get_32 (abfd, (bfd_byte *) note->descdata) != 1) | |
9853 | return FALSE; | |
9854 | offset = 4; | |
9855 | ||
9856 | /* Skip over pr_psinfosz. */ | |
b5430a3c | 9857 | if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS32) |
0064d223 JB |
9858 | offset += 4; |
9859 | else | |
9860 | { | |
9861 | offset += 4; /* Padding before pr_psinfosz. */ | |
9862 | offset += 8; | |
9863 | } | |
9864 | ||
aa1ed4a9 JB |
9865 | /* pr_fname is PRFNAMESZ (16) + 1 bytes in size. */ |
9866 | elf_tdata (abfd)->core->program | |
9867 | = _bfd_elfcore_strndup (abfd, note->descdata + offset, 17); | |
9868 | offset += 17; | |
9869 | ||
9870 | /* pr_psargs is PRARGSZ (80) + 1 bytes in size. */ | |
9871 | elf_tdata (abfd)->core->command | |
9872 | = _bfd_elfcore_strndup (abfd, note->descdata + offset, 81); | |
0064d223 JB |
9873 | offset += 81; |
9874 | ||
9875 | /* Padding before pr_pid. */ | |
9876 | offset += 2; | |
9877 | ||
9878 | /* The pr_pid field was added in version "1a". */ | |
9879 | if (note->descsz < offset + 4) | |
9880 | return TRUE; | |
9881 | ||
9882 | elf_tdata (abfd)->core->pid | |
9883 | = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset); | |
aa1ed4a9 JB |
9884 | |
9885 | return TRUE; | |
9886 | } | |
9887 | ||
9888 | static bfd_boolean | |
9889 | elfcore_grok_freebsd_prstatus (bfd *abfd, Elf_Internal_Note *note) | |
9890 | { | |
9891 | size_t offset; | |
9892 | size_t size; | |
24d3e51b | 9893 | size_t min_size; |
aa1ed4a9 | 9894 | |
24d3e51b NC |
9895 | /* Compute offset of pr_getregsz, skipping over pr_statussz. |
9896 | Also compute minimum size of this note. */ | |
b5430a3c | 9897 | switch (elf_elfheader (abfd)->e_ident[EI_CLASS]) |
aa1ed4a9 | 9898 | { |
b5430a3c | 9899 | case ELFCLASS32: |
24d3e51b NC |
9900 | offset = 4 + 4; |
9901 | min_size = offset + (4 * 2) + 4 + 4 + 4; | |
aa1ed4a9 JB |
9902 | break; |
9903 | ||
b5430a3c | 9904 | case ELFCLASS64: |
24d3e51b NC |
9905 | offset = 4 + 4 + 8; /* Includes padding before pr_statussz. */ |
9906 | min_size = offset + (8 * 2) + 4 + 4 + 4 + 4; | |
aa1ed4a9 JB |
9907 | break; |
9908 | ||
9909 | default: | |
9910 | return FALSE; | |
9911 | } | |
9912 | ||
24d3e51b NC |
9913 | if (note->descsz < min_size) |
9914 | return FALSE; | |
9915 | ||
9916 | /* Check for version 1 in pr_version. */ | |
9917 | if (bfd_h_get_32 (abfd, (bfd_byte *) note->descdata) != 1) | |
9918 | return FALSE; | |
aa1ed4a9 | 9919 | |
24d3e51b NC |
9920 | /* Extract size of pr_reg from pr_gregsetsz. */ |
9921 | /* Skip over pr_gregsetsz and pr_fpregsetsz. */ | |
b5430a3c | 9922 | if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS32) |
24d3e51b NC |
9923 | { |
9924 | size = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset); | |
9925 | offset += 4 * 2; | |
9926 | } | |
b5430a3c | 9927 | else |
24d3e51b NC |
9928 | { |
9929 | size = bfd_h_get_64 (abfd, (bfd_byte *) note->descdata + offset); | |
9930 | offset += 8 * 2; | |
9931 | } | |
aa1ed4a9 | 9932 | |
24d3e51b | 9933 | /* Skip over pr_osreldate. */ |
aa1ed4a9 JB |
9934 | offset += 4; |
9935 | ||
24d3e51b | 9936 | /* Read signal from pr_cursig. */ |
aa1ed4a9 JB |
9937 | if (elf_tdata (abfd)->core->signal == 0) |
9938 | elf_tdata (abfd)->core->signal | |
9939 | = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset); | |
9940 | offset += 4; | |
9941 | ||
24d3e51b | 9942 | /* Read TID from pr_pid. */ |
aa1ed4a9 JB |
9943 | elf_tdata (abfd)->core->lwpid |
9944 | = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset); | |
9945 | offset += 4; | |
9946 | ||
24d3e51b | 9947 | /* Padding before pr_reg. */ |
b5430a3c | 9948 | if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS64) |
aa1ed4a9 JB |
9949 | offset += 4; |
9950 | ||
24d3e51b NC |
9951 | /* Make sure that there is enough data remaining in the note. */ |
9952 | if ((note->descsz - offset) < size) | |
9953 | return FALSE; | |
9954 | ||
aa1ed4a9 JB |
9955 | /* Make a ".reg/999" section and a ".reg" section. */ |
9956 | return _bfd_elfcore_make_pseudosection (abfd, ".reg", | |
9957 | size, note->descpos + offset); | |
9958 | } | |
9959 | ||
9960 | static bfd_boolean | |
9961 | elfcore_grok_freebsd_note (bfd *abfd, Elf_Internal_Note *note) | |
9962 | { | |
9963 | switch (note->type) | |
9964 | { | |
9965 | case NT_PRSTATUS: | |
9966 | return elfcore_grok_freebsd_prstatus (abfd, note); | |
9967 | ||
9968 | case NT_FPREGSET: | |
9969 | return elfcore_grok_prfpreg (abfd, note); | |
9970 | ||
9971 | case NT_PRPSINFO: | |
9972 | return elfcore_grok_freebsd_psinfo (abfd, note); | |
9973 | ||
9974 | case NT_FREEBSD_THRMISC: | |
9975 | if (note->namesz == 8) | |
9976 | return elfcore_make_note_pseudosection (abfd, ".thrmisc", note); | |
9977 | else | |
9978 | return TRUE; | |
9979 | ||
3350c5f5 JB |
9980 | case NT_FREEBSD_PROCSTAT_AUXV: |
9981 | { | |
9982 | asection *sect = bfd_make_section_anyway_with_flags (abfd, ".auxv", | |
9983 | SEC_HAS_CONTENTS); | |
9984 | ||
9985 | if (sect == NULL) | |
9986 | return FALSE; | |
9987 | sect->size = note->descsz - 4; | |
9988 | sect->filepos = note->descpos + 4; | |
9989 | sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32; | |
9990 | ||
9991 | return TRUE; | |
9992 | } | |
9993 | ||
aa1ed4a9 JB |
9994 | case NT_X86_XSTATE: |
9995 | if (note->namesz == 8) | |
9996 | return elfcore_grok_xstatereg (abfd, note); | |
9997 | else | |
9998 | return TRUE; | |
9999 | ||
e6f3b9c3 JB |
10000 | case NT_FREEBSD_PTLWPINFO: |
10001 | return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.lwpinfo", | |
10002 | note); | |
10003 | ||
aa1ed4a9 JB |
10004 | default: |
10005 | return TRUE; | |
10006 | } | |
10007 | } | |
10008 | ||
b34976b6 | 10009 | static bfd_boolean |
217aa764 | 10010 | elfcore_netbsd_get_lwpid (Elf_Internal_Note *note, int *lwpidp) |
50b2bdb7 AM |
10011 | { |
10012 | char *cp; | |
10013 | ||
10014 | cp = strchr (note->namedata, '@'); | |
10015 | if (cp != NULL) | |
10016 | { | |
d2b64500 | 10017 | *lwpidp = atoi(cp + 1); |
b34976b6 | 10018 | return TRUE; |
50b2bdb7 | 10019 | } |
b34976b6 | 10020 | return FALSE; |
50b2bdb7 AM |
10021 | } |
10022 | ||
b34976b6 | 10023 | static bfd_boolean |
217aa764 | 10024 | elfcore_grok_netbsd_procinfo (bfd *abfd, Elf_Internal_Note *note) |
50b2bdb7 | 10025 | { |
50b2bdb7 | 10026 | /* Signal number at offset 0x08. */ |
228e534f | 10027 | elf_tdata (abfd)->core->signal |
50b2bdb7 AM |
10028 | = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08); |
10029 | ||
10030 | /* Process ID at offset 0x50. */ | |
228e534f | 10031 | elf_tdata (abfd)->core->pid |
50b2bdb7 AM |
10032 | = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x50); |
10033 | ||
10034 | /* Command name at 0x7c (max 32 bytes, including nul). */ | |
228e534f | 10035 | elf_tdata (abfd)->core->command |
50b2bdb7 AM |
10036 | = _bfd_elfcore_strndup (abfd, note->descdata + 0x7c, 31); |
10037 | ||
7720ba9f MK |
10038 | return elfcore_make_note_pseudosection (abfd, ".note.netbsdcore.procinfo", |
10039 | note); | |
50b2bdb7 AM |
10040 | } |
10041 | ||
b34976b6 | 10042 | static bfd_boolean |
217aa764 | 10043 | elfcore_grok_netbsd_note (bfd *abfd, Elf_Internal_Note *note) |
50b2bdb7 AM |
10044 | { |
10045 | int lwp; | |
10046 | ||
10047 | if (elfcore_netbsd_get_lwpid (note, &lwp)) | |
228e534f | 10048 | elf_tdata (abfd)->core->lwpid = lwp; |
50b2bdb7 | 10049 | |
b4db1224 | 10050 | if (note->type == NT_NETBSDCORE_PROCINFO) |
50b2bdb7 AM |
10051 | { |
10052 | /* NetBSD-specific core "procinfo". Note that we expect to | |
08a40648 AM |
10053 | find this note before any of the others, which is fine, |
10054 | since the kernel writes this note out first when it | |
10055 | creates a core file. */ | |
47d9a591 | 10056 | |
50b2bdb7 AM |
10057 | return elfcore_grok_netbsd_procinfo (abfd, note); |
10058 | } | |
10059 | ||
b4db1224 JT |
10060 | /* As of Jan 2002 there are no other machine-independent notes |
10061 | defined for NetBSD core files. If the note type is less | |
10062 | than the start of the machine-dependent note types, we don't | |
10063 | understand it. */ | |
47d9a591 | 10064 | |
b4db1224 | 10065 | if (note->type < NT_NETBSDCORE_FIRSTMACH) |
b34976b6 | 10066 | return TRUE; |
50b2bdb7 AM |
10067 | |
10068 | ||
10069 | switch (bfd_get_arch (abfd)) | |
10070 | { | |
08a40648 AM |
10071 | /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and |
10072 | PT_GETFPREGS == mach+2. */ | |
50b2bdb7 AM |
10073 | |
10074 | case bfd_arch_alpha: | |
10075 | case bfd_arch_sparc: | |
10076 | switch (note->type) | |
08a40648 AM |
10077 | { |
10078 | case NT_NETBSDCORE_FIRSTMACH+0: | |
10079 | return elfcore_make_note_pseudosection (abfd, ".reg", note); | |
50b2bdb7 | 10080 | |
08a40648 AM |
10081 | case NT_NETBSDCORE_FIRSTMACH+2: |
10082 | return elfcore_make_note_pseudosection (abfd, ".reg2", note); | |
50b2bdb7 | 10083 | |
08a40648 AM |
10084 | default: |
10085 | return TRUE; | |
10086 | } | |
50b2bdb7 | 10087 | |
08a40648 AM |
10088 | /* On all other arch's, PT_GETREGS == mach+1 and |
10089 | PT_GETFPREGS == mach+3. */ | |
50b2bdb7 AM |
10090 | |
10091 | default: | |
10092 | switch (note->type) | |
08a40648 AM |
10093 | { |
10094 | case NT_NETBSDCORE_FIRSTMACH+1: | |
10095 | return elfcore_make_note_pseudosection (abfd, ".reg", note); | |
50b2bdb7 | 10096 | |
08a40648 AM |
10097 | case NT_NETBSDCORE_FIRSTMACH+3: |
10098 | return elfcore_make_note_pseudosection (abfd, ".reg2", note); | |
50b2bdb7 | 10099 | |
08a40648 AM |
10100 | default: |
10101 | return TRUE; | |
10102 | } | |
50b2bdb7 AM |
10103 | } |
10104 | /* NOTREACHED */ | |
10105 | } | |
10106 | ||
67cc5033 MK |
10107 | static bfd_boolean |
10108 | elfcore_grok_openbsd_procinfo (bfd *abfd, Elf_Internal_Note *note) | |
10109 | { | |
10110 | /* Signal number at offset 0x08. */ | |
228e534f | 10111 | elf_tdata (abfd)->core->signal |
67cc5033 MK |
10112 | = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08); |
10113 | ||
10114 | /* Process ID at offset 0x20. */ | |
228e534f | 10115 | elf_tdata (abfd)->core->pid |
67cc5033 MK |
10116 | = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x20); |
10117 | ||
10118 | /* Command name at 0x48 (max 32 bytes, including nul). */ | |
228e534f | 10119 | elf_tdata (abfd)->core->command |
67cc5033 MK |
10120 | = _bfd_elfcore_strndup (abfd, note->descdata + 0x48, 31); |
10121 | ||
10122 | return TRUE; | |
10123 | } | |
10124 | ||
10125 | static bfd_boolean | |
10126 | elfcore_grok_openbsd_note (bfd *abfd, Elf_Internal_Note *note) | |
10127 | { | |
10128 | if (note->type == NT_OPENBSD_PROCINFO) | |
10129 | return elfcore_grok_openbsd_procinfo (abfd, note); | |
10130 | ||
10131 | if (note->type == NT_OPENBSD_REGS) | |
10132 | return elfcore_make_note_pseudosection (abfd, ".reg", note); | |
10133 | ||
10134 | if (note->type == NT_OPENBSD_FPREGS) | |
10135 | return elfcore_make_note_pseudosection (abfd, ".reg2", note); | |
10136 | ||
10137 | if (note->type == NT_OPENBSD_XFPREGS) | |
10138 | return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note); | |
10139 | ||
10140 | if (note->type == NT_OPENBSD_AUXV) | |
10141 | { | |
10142 | asection *sect = bfd_make_section_anyway_with_flags (abfd, ".auxv", | |
10143 | SEC_HAS_CONTENTS); | |
10144 | ||
10145 | if (sect == NULL) | |
10146 | return FALSE; | |
10147 | sect->size = note->descsz; | |
10148 | sect->filepos = note->descpos; | |
10149 | sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32; | |
10150 | ||
10151 | return TRUE; | |
10152 | } | |
10153 | ||
10154 | if (note->type == NT_OPENBSD_WCOOKIE) | |
10155 | { | |
10156 | asection *sect = bfd_make_section_anyway_with_flags (abfd, ".wcookie", | |
10157 | SEC_HAS_CONTENTS); | |
10158 | ||
10159 | if (sect == NULL) | |
10160 | return FALSE; | |
10161 | sect->size = note->descsz; | |
10162 | sect->filepos = note->descpos; | |
10163 | sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32; | |
10164 | ||
10165 | return TRUE; | |
10166 | } | |
10167 | ||
10168 | return TRUE; | |
10169 | } | |
10170 | ||
07c6e936 | 10171 | static bfd_boolean |
d3fd4074 | 10172 | elfcore_grok_nto_status (bfd *abfd, Elf_Internal_Note *note, long *tid) |
07c6e936 NC |
10173 | { |
10174 | void *ddata = note->descdata; | |
10175 | char buf[100]; | |
10176 | char *name; | |
10177 | asection *sect; | |
f8843e87 AM |
10178 | short sig; |
10179 | unsigned flags; | |
07c6e936 NC |
10180 | |
10181 | /* nto_procfs_status 'pid' field is at offset 0. */ | |
228e534f | 10182 | elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, (bfd_byte *) ddata); |
07c6e936 | 10183 | |
f8843e87 AM |
10184 | /* nto_procfs_status 'tid' field is at offset 4. Pass it back. */ |
10185 | *tid = bfd_get_32 (abfd, (bfd_byte *) ddata + 4); | |
10186 | ||
10187 | /* nto_procfs_status 'flags' field is at offset 8. */ | |
10188 | flags = bfd_get_32 (abfd, (bfd_byte *) ddata + 8); | |
07c6e936 NC |
10189 | |
10190 | /* nto_procfs_status 'what' field is at offset 14. */ | |
f8843e87 AM |
10191 | if ((sig = bfd_get_16 (abfd, (bfd_byte *) ddata + 14)) > 0) |
10192 | { | |
228e534f AM |
10193 | elf_tdata (abfd)->core->signal = sig; |
10194 | elf_tdata (abfd)->core->lwpid = *tid; | |
f8843e87 | 10195 | } |
07c6e936 | 10196 | |
f8843e87 AM |
10197 | /* _DEBUG_FLAG_CURTID (current thread) is 0x80. Some cores |
10198 | do not come from signals so we make sure we set the current | |
10199 | thread just in case. */ | |
10200 | if (flags & 0x00000080) | |
228e534f | 10201 | elf_tdata (abfd)->core->lwpid = *tid; |
07c6e936 NC |
10202 | |
10203 | /* Make a ".qnx_core_status/%d" section. */ | |
d3fd4074 | 10204 | sprintf (buf, ".qnx_core_status/%ld", *tid); |
07c6e936 | 10205 | |
a50b1753 | 10206 | name = (char *) bfd_alloc (abfd, strlen (buf) + 1); |
07c6e936 NC |
10207 | if (name == NULL) |
10208 | return FALSE; | |
10209 | strcpy (name, buf); | |
10210 | ||
117ed4f8 | 10211 | sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); |
07c6e936 NC |
10212 | if (sect == NULL) |
10213 | return FALSE; | |
10214 | ||
eea6121a | 10215 | sect->size = note->descsz; |
07c6e936 | 10216 | sect->filepos = note->descpos; |
07c6e936 NC |
10217 | sect->alignment_power = 2; |
10218 | ||
10219 | return (elfcore_maybe_make_sect (abfd, ".qnx_core_status", sect)); | |
10220 | } | |
10221 | ||
10222 | static bfd_boolean | |
d69f560c KW |
10223 | elfcore_grok_nto_regs (bfd *abfd, |
10224 | Elf_Internal_Note *note, | |
d3fd4074 | 10225 | long tid, |
d69f560c | 10226 | char *base) |
07c6e936 NC |
10227 | { |
10228 | char buf[100]; | |
10229 | char *name; | |
10230 | asection *sect; | |
10231 | ||
d69f560c | 10232 | /* Make a "(base)/%d" section. */ |
d3fd4074 | 10233 | sprintf (buf, "%s/%ld", base, tid); |
07c6e936 | 10234 | |
a50b1753 | 10235 | name = (char *) bfd_alloc (abfd, strlen (buf) + 1); |
07c6e936 NC |
10236 | if (name == NULL) |
10237 | return FALSE; | |
10238 | strcpy (name, buf); | |
10239 | ||
117ed4f8 | 10240 | sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); |
07c6e936 NC |
10241 | if (sect == NULL) |
10242 | return FALSE; | |
10243 | ||
eea6121a | 10244 | sect->size = note->descsz; |
07c6e936 | 10245 | sect->filepos = note->descpos; |
07c6e936 NC |
10246 | sect->alignment_power = 2; |
10247 | ||
f8843e87 | 10248 | /* This is the current thread. */ |
228e534f | 10249 | if (elf_tdata (abfd)->core->lwpid == tid) |
d69f560c | 10250 | return elfcore_maybe_make_sect (abfd, base, sect); |
f8843e87 AM |
10251 | |
10252 | return TRUE; | |
07c6e936 NC |
10253 | } |
10254 | ||
10255 | #define BFD_QNT_CORE_INFO 7 | |
10256 | #define BFD_QNT_CORE_STATUS 8 | |
10257 | #define BFD_QNT_CORE_GREG 9 | |
10258 | #define BFD_QNT_CORE_FPREG 10 | |
10259 | ||
10260 | static bfd_boolean | |
217aa764 | 10261 | elfcore_grok_nto_note (bfd *abfd, Elf_Internal_Note *note) |
07c6e936 NC |
10262 | { |
10263 | /* Every GREG section has a STATUS section before it. Store the | |
811072d8 | 10264 | tid from the previous call to pass down to the next gregs |
07c6e936 | 10265 | function. */ |
d3fd4074 | 10266 | static long tid = 1; |
07c6e936 NC |
10267 | |
10268 | switch (note->type) | |
10269 | { | |
d69f560c KW |
10270 | case BFD_QNT_CORE_INFO: |
10271 | return elfcore_make_note_pseudosection (abfd, ".qnx_core_info", note); | |
10272 | case BFD_QNT_CORE_STATUS: | |
10273 | return elfcore_grok_nto_status (abfd, note, &tid); | |
10274 | case BFD_QNT_CORE_GREG: | |
10275 | return elfcore_grok_nto_regs (abfd, note, tid, ".reg"); | |
10276 | case BFD_QNT_CORE_FPREG: | |
10277 | return elfcore_grok_nto_regs (abfd, note, tid, ".reg2"); | |
10278 | default: | |
10279 | return TRUE; | |
07c6e936 NC |
10280 | } |
10281 | } | |
10282 | ||
b15fa79e AM |
10283 | static bfd_boolean |
10284 | elfcore_grok_spu_note (bfd *abfd, Elf_Internal_Note *note) | |
10285 | { | |
10286 | char *name; | |
10287 | asection *sect; | |
10288 | size_t len; | |
10289 | ||
10290 | /* Use note name as section name. */ | |
10291 | len = note->namesz; | |
a50b1753 | 10292 | name = (char *) bfd_alloc (abfd, len); |
b15fa79e AM |
10293 | if (name == NULL) |
10294 | return FALSE; | |
10295 | memcpy (name, note->namedata, len); | |
10296 | name[len - 1] = '\0'; | |
10297 | ||
10298 | sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); | |
10299 | if (sect == NULL) | |
10300 | return FALSE; | |
10301 | ||
10302 | sect->size = note->descsz; | |
10303 | sect->filepos = note->descpos; | |
10304 | sect->alignment_power = 1; | |
10305 | ||
10306 | return TRUE; | |
10307 | } | |
10308 | ||
7c76fa91 MS |
10309 | /* Function: elfcore_write_note |
10310 | ||
47d9a591 | 10311 | Inputs: |
a39f3346 | 10312 | buffer to hold note, and current size of buffer |
7c76fa91 MS |
10313 | name of note |
10314 | type of note | |
10315 | data for note | |
10316 | size of data for note | |
10317 | ||
a39f3346 AM |
10318 | Writes note to end of buffer. ELF64 notes are written exactly as |
10319 | for ELF32, despite the current (as of 2006) ELF gabi specifying | |
10320 | that they ought to have 8-byte namesz and descsz field, and have | |
10321 | 8-byte alignment. Other writers, eg. Linux kernel, do the same. | |
10322 | ||
7c76fa91 | 10323 | Return: |
a39f3346 | 10324 | Pointer to realloc'd buffer, *BUFSIZ updated. */ |
7c76fa91 MS |
10325 | |
10326 | char * | |
a39f3346 | 10327 | elfcore_write_note (bfd *abfd, |
217aa764 | 10328 | char *buf, |
a39f3346 | 10329 | int *bufsiz, |
217aa764 | 10330 | const char *name, |
a39f3346 | 10331 | int type, |
217aa764 | 10332 | const void *input, |
a39f3346 | 10333 | int size) |
7c76fa91 MS |
10334 | { |
10335 | Elf_External_Note *xnp; | |
d4c88bbb | 10336 | size_t namesz; |
d4c88bbb | 10337 | size_t newspace; |
a39f3346 | 10338 | char *dest; |
7c76fa91 | 10339 | |
d4c88bbb | 10340 | namesz = 0; |
d4c88bbb | 10341 | if (name != NULL) |
a39f3346 | 10342 | namesz = strlen (name) + 1; |
d4c88bbb | 10343 | |
a39f3346 | 10344 | newspace = 12 + ((namesz + 3) & -4) + ((size + 3) & -4); |
d4c88bbb | 10345 | |
a50b1753 | 10346 | buf = (char *) realloc (buf, *bufsiz + newspace); |
14b1c01e AM |
10347 | if (buf == NULL) |
10348 | return buf; | |
a39f3346 | 10349 | dest = buf + *bufsiz; |
7c76fa91 MS |
10350 | *bufsiz += newspace; |
10351 | xnp = (Elf_External_Note *) dest; | |
10352 | H_PUT_32 (abfd, namesz, xnp->namesz); | |
10353 | H_PUT_32 (abfd, size, xnp->descsz); | |
10354 | H_PUT_32 (abfd, type, xnp->type); | |
d4c88bbb AM |
10355 | dest = xnp->name; |
10356 | if (name != NULL) | |
10357 | { | |
10358 | memcpy (dest, name, namesz); | |
10359 | dest += namesz; | |
a39f3346 | 10360 | while (namesz & 3) |
d4c88bbb AM |
10361 | { |
10362 | *dest++ = '\0'; | |
a39f3346 | 10363 | ++namesz; |
d4c88bbb AM |
10364 | } |
10365 | } | |
10366 | memcpy (dest, input, size); | |
a39f3346 AM |
10367 | dest += size; |
10368 | while (size & 3) | |
10369 | { | |
10370 | *dest++ = '\0'; | |
10371 | ++size; | |
10372 | } | |
10373 | return buf; | |
7c76fa91 MS |
10374 | } |
10375 | ||
7c76fa91 | 10376 | char * |
217aa764 AM |
10377 | elfcore_write_prpsinfo (bfd *abfd, |
10378 | char *buf, | |
10379 | int *bufsiz, | |
10380 | const char *fname, | |
10381 | const char *psargs) | |
7c76fa91 | 10382 | { |
183e98be AM |
10383 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
10384 | ||
10385 | if (bed->elf_backend_write_core_note != NULL) | |
10386 | { | |
10387 | char *ret; | |
10388 | ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz, | |
10389 | NT_PRPSINFO, fname, psargs); | |
10390 | if (ret != NULL) | |
10391 | return ret; | |
10392 | } | |
7c76fa91 | 10393 | |
1f20dca5 | 10394 | #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) |
183e98be AM |
10395 | #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T) |
10396 | if (bed->s->elfclass == ELFCLASS32) | |
10397 | { | |
10398 | #if defined (HAVE_PSINFO32_T) | |
10399 | psinfo32_t data; | |
10400 | int note_type = NT_PSINFO; | |
10401 | #else | |
10402 | prpsinfo32_t data; | |
10403 | int note_type = NT_PRPSINFO; | |
10404 | #endif | |
10405 | ||
10406 | memset (&data, 0, sizeof (data)); | |
10407 | strncpy (data.pr_fname, fname, sizeof (data.pr_fname)); | |
10408 | strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs)); | |
10409 | return elfcore_write_note (abfd, buf, bufsiz, | |
1f20dca5 | 10410 | "CORE", note_type, &data, sizeof (data)); |
183e98be AM |
10411 | } |
10412 | else | |
10413 | #endif | |
10414 | { | |
7c76fa91 | 10415 | #if defined (HAVE_PSINFO_T) |
183e98be AM |
10416 | psinfo_t data; |
10417 | int note_type = NT_PSINFO; | |
7c76fa91 | 10418 | #else |
183e98be AM |
10419 | prpsinfo_t data; |
10420 | int note_type = NT_PRPSINFO; | |
7c76fa91 MS |
10421 | #endif |
10422 | ||
183e98be AM |
10423 | memset (&data, 0, sizeof (data)); |
10424 | strncpy (data.pr_fname, fname, sizeof (data.pr_fname)); | |
10425 | strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs)); | |
10426 | return elfcore_write_note (abfd, buf, bufsiz, | |
1f20dca5 | 10427 | "CORE", note_type, &data, sizeof (data)); |
183e98be | 10428 | } |
7c76fa91 MS |
10429 | #endif /* PSINFO_T or PRPSINFO_T */ |
10430 | ||
1f20dca5 UW |
10431 | free (buf); |
10432 | return NULL; | |
10433 | } | |
10434 | ||
70a38d42 SDJ |
10435 | char * |
10436 | elfcore_write_linux_prpsinfo32 | |
10437 | (bfd *abfd, char *buf, int *bufsiz, | |
10438 | const struct elf_internal_linux_prpsinfo *prpsinfo) | |
10439 | { | |
10440 | struct elf_external_linux_prpsinfo32 data; | |
10441 | ||
aeb70569 | 10442 | swap_linux_prpsinfo32_out (abfd, prpsinfo, &data); |
70a38d42 SDJ |
10443 | return elfcore_write_note (abfd, buf, bufsiz, "CORE", NT_PRPSINFO, |
10444 | &data, sizeof (data)); | |
10445 | } | |
10446 | ||
10447 | char * | |
10448 | elfcore_write_linux_prpsinfo64 | |
10449 | (bfd *abfd, char *buf, int *bufsiz, | |
10450 | const struct elf_internal_linux_prpsinfo *prpsinfo) | |
10451 | { | |
10452 | struct elf_external_linux_prpsinfo64 data; | |
10453 | ||
aeb70569 | 10454 | swap_linux_prpsinfo64_out (abfd, prpsinfo, &data); |
70a38d42 SDJ |
10455 | return elfcore_write_note (abfd, buf, bufsiz, |
10456 | "CORE", NT_PRPSINFO, &data, sizeof (data)); | |
10457 | } | |
10458 | ||
7c76fa91 | 10459 | char * |
217aa764 AM |
10460 | elfcore_write_prstatus (bfd *abfd, |
10461 | char *buf, | |
10462 | int *bufsiz, | |
10463 | long pid, | |
10464 | int cursig, | |
10465 | const void *gregs) | |
7c76fa91 | 10466 | { |
183e98be | 10467 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
7c76fa91 | 10468 | |
183e98be AM |
10469 | if (bed->elf_backend_write_core_note != NULL) |
10470 | { | |
10471 | char *ret; | |
10472 | ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz, | |
10473 | NT_PRSTATUS, | |
10474 | pid, cursig, gregs); | |
10475 | if (ret != NULL) | |
10476 | return ret; | |
10477 | } | |
10478 | ||
1f20dca5 | 10479 | #if defined (HAVE_PRSTATUS_T) |
183e98be AM |
10480 | #if defined (HAVE_PRSTATUS32_T) |
10481 | if (bed->s->elfclass == ELFCLASS32) | |
10482 | { | |
10483 | prstatus32_t prstat; | |
10484 | ||
10485 | memset (&prstat, 0, sizeof (prstat)); | |
10486 | prstat.pr_pid = pid; | |
10487 | prstat.pr_cursig = cursig; | |
10488 | memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg)); | |
1f20dca5 | 10489 | return elfcore_write_note (abfd, buf, bufsiz, "CORE", |
183e98be AM |
10490 | NT_PRSTATUS, &prstat, sizeof (prstat)); |
10491 | } | |
10492 | else | |
10493 | #endif | |
10494 | { | |
10495 | prstatus_t prstat; | |
10496 | ||
10497 | memset (&prstat, 0, sizeof (prstat)); | |
10498 | prstat.pr_pid = pid; | |
10499 | prstat.pr_cursig = cursig; | |
10500 | memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg)); | |
1f20dca5 | 10501 | return elfcore_write_note (abfd, buf, bufsiz, "CORE", |
183e98be AM |
10502 | NT_PRSTATUS, &prstat, sizeof (prstat)); |
10503 | } | |
7c76fa91 MS |
10504 | #endif /* HAVE_PRSTATUS_T */ |
10505 | ||
1f20dca5 UW |
10506 | free (buf); |
10507 | return NULL; | |
10508 | } | |
10509 | ||
51316059 MS |
10510 | #if defined (HAVE_LWPSTATUS_T) |
10511 | char * | |
217aa764 AM |
10512 | elfcore_write_lwpstatus (bfd *abfd, |
10513 | char *buf, | |
10514 | int *bufsiz, | |
10515 | long pid, | |
10516 | int cursig, | |
10517 | const void *gregs) | |
51316059 MS |
10518 | { |
10519 | lwpstatus_t lwpstat; | |
183e98be | 10520 | const char *note_name = "CORE"; |
51316059 MS |
10521 | |
10522 | memset (&lwpstat, 0, sizeof (lwpstat)); | |
10523 | lwpstat.pr_lwpid = pid >> 16; | |
10524 | lwpstat.pr_cursig = cursig; | |
10525 | #if defined (HAVE_LWPSTATUS_T_PR_REG) | |
d1e8523e | 10526 | memcpy (&lwpstat.pr_reg, gregs, sizeof (lwpstat.pr_reg)); |
51316059 MS |
10527 | #elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT) |
10528 | #if !defined(gregs) | |
10529 | memcpy (lwpstat.pr_context.uc_mcontext.gregs, | |
10530 | gregs, sizeof (lwpstat.pr_context.uc_mcontext.gregs)); | |
10531 | #else | |
10532 | memcpy (lwpstat.pr_context.uc_mcontext.__gregs, | |
10533 | gregs, sizeof (lwpstat.pr_context.uc_mcontext.__gregs)); | |
10534 | #endif | |
10535 | #endif | |
47d9a591 | 10536 | return elfcore_write_note (abfd, buf, bufsiz, note_name, |
51316059 MS |
10537 | NT_LWPSTATUS, &lwpstat, sizeof (lwpstat)); |
10538 | } | |
10539 | #endif /* HAVE_LWPSTATUS_T */ | |
10540 | ||
7c76fa91 MS |
10541 | #if defined (HAVE_PSTATUS_T) |
10542 | char * | |
217aa764 AM |
10543 | elfcore_write_pstatus (bfd *abfd, |
10544 | char *buf, | |
10545 | int *bufsiz, | |
10546 | long pid, | |
6c10990d NC |
10547 | int cursig ATTRIBUTE_UNUSED, |
10548 | const void *gregs ATTRIBUTE_UNUSED) | |
7c76fa91 | 10549 | { |
183e98be AM |
10550 | const char *note_name = "CORE"; |
10551 | #if defined (HAVE_PSTATUS32_T) | |
10552 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
7c76fa91 | 10553 | |
183e98be AM |
10554 | if (bed->s->elfclass == ELFCLASS32) |
10555 | { | |
10556 | pstatus32_t pstat; | |
10557 | ||
10558 | memset (&pstat, 0, sizeof (pstat)); | |
10559 | pstat.pr_pid = pid & 0xffff; | |
10560 | buf = elfcore_write_note (abfd, buf, bufsiz, note_name, | |
10561 | NT_PSTATUS, &pstat, sizeof (pstat)); | |
10562 | return buf; | |
10563 | } | |
10564 | else | |
10565 | #endif | |
10566 | { | |
10567 | pstatus_t pstat; | |
10568 | ||
10569 | memset (&pstat, 0, sizeof (pstat)); | |
10570 | pstat.pr_pid = pid & 0xffff; | |
10571 | buf = elfcore_write_note (abfd, buf, bufsiz, note_name, | |
10572 | NT_PSTATUS, &pstat, sizeof (pstat)); | |
10573 | return buf; | |
10574 | } | |
7c76fa91 MS |
10575 | } |
10576 | #endif /* HAVE_PSTATUS_T */ | |
10577 | ||
10578 | char * | |
217aa764 AM |
10579 | elfcore_write_prfpreg (bfd *abfd, |
10580 | char *buf, | |
10581 | int *bufsiz, | |
10582 | const void *fpregs, | |
10583 | int size) | |
7c76fa91 | 10584 | { |
183e98be | 10585 | const char *note_name = "CORE"; |
47d9a591 | 10586 | return elfcore_write_note (abfd, buf, bufsiz, |
7c76fa91 MS |
10587 | note_name, NT_FPREGSET, fpregs, size); |
10588 | } | |
10589 | ||
10590 | char * | |
217aa764 AM |
10591 | elfcore_write_prxfpreg (bfd *abfd, |
10592 | char *buf, | |
10593 | int *bufsiz, | |
10594 | const void *xfpregs, | |
10595 | int size) | |
7c76fa91 MS |
10596 | { |
10597 | char *note_name = "LINUX"; | |
47d9a591 | 10598 | return elfcore_write_note (abfd, buf, bufsiz, |
7c76fa91 MS |
10599 | note_name, NT_PRXFPREG, xfpregs, size); |
10600 | } | |
10601 | ||
4339cae0 L |
10602 | char * |
10603 | elfcore_write_xstatereg (bfd *abfd, char *buf, int *bufsiz, | |
10604 | const void *xfpregs, int size) | |
10605 | { | |
97de3545 JB |
10606 | char *note_name; |
10607 | if (get_elf_backend_data (abfd)->elf_osabi == ELFOSABI_FREEBSD) | |
10608 | note_name = "FreeBSD"; | |
10609 | else | |
10610 | note_name = "LINUX"; | |
4339cae0 L |
10611 | return elfcore_write_note (abfd, buf, bufsiz, |
10612 | note_name, NT_X86_XSTATE, xfpregs, size); | |
10613 | } | |
10614 | ||
97753bd5 AM |
10615 | char * |
10616 | elfcore_write_ppc_vmx (bfd *abfd, | |
10617 | char *buf, | |
10618 | int *bufsiz, | |
10619 | const void *ppc_vmx, | |
10620 | int size) | |
10621 | { | |
10622 | char *note_name = "LINUX"; | |
10623 | return elfcore_write_note (abfd, buf, bufsiz, | |
10624 | note_name, NT_PPC_VMX, ppc_vmx, size); | |
10625 | } | |
10626 | ||
89eeb0bc LM |
10627 | char * |
10628 | elfcore_write_ppc_vsx (bfd *abfd, | |
10629 | char *buf, | |
10630 | int *bufsiz, | |
10631 | const void *ppc_vsx, | |
10632 | int size) | |
10633 | { | |
10634 | char *note_name = "LINUX"; | |
10635 | return elfcore_write_note (abfd, buf, bufsiz, | |
10636 | note_name, NT_PPC_VSX, ppc_vsx, size); | |
10637 | } | |
10638 | ||
0675e188 UW |
10639 | static char * |
10640 | elfcore_write_s390_high_gprs (bfd *abfd, | |
10641 | char *buf, | |
10642 | int *bufsiz, | |
10643 | const void *s390_high_gprs, | |
10644 | int size) | |
10645 | { | |
10646 | char *note_name = "LINUX"; | |
10647 | return elfcore_write_note (abfd, buf, bufsiz, | |
10648 | note_name, NT_S390_HIGH_GPRS, | |
10649 | s390_high_gprs, size); | |
10650 | } | |
10651 | ||
d7eeb400 MS |
10652 | char * |
10653 | elfcore_write_s390_timer (bfd *abfd, | |
10654 | char *buf, | |
10655 | int *bufsiz, | |
10656 | const void *s390_timer, | |
10657 | int size) | |
10658 | { | |
10659 | char *note_name = "LINUX"; | |
10660 | return elfcore_write_note (abfd, buf, bufsiz, | |
10661 | note_name, NT_S390_TIMER, s390_timer, size); | |
10662 | } | |
10663 | ||
10664 | char * | |
10665 | elfcore_write_s390_todcmp (bfd *abfd, | |
10666 | char *buf, | |
10667 | int *bufsiz, | |
10668 | const void *s390_todcmp, | |
10669 | int size) | |
10670 | { | |
10671 | char *note_name = "LINUX"; | |
10672 | return elfcore_write_note (abfd, buf, bufsiz, | |
10673 | note_name, NT_S390_TODCMP, s390_todcmp, size); | |
10674 | } | |
10675 | ||
10676 | char * | |
10677 | elfcore_write_s390_todpreg (bfd *abfd, | |
10678 | char *buf, | |
10679 | int *bufsiz, | |
10680 | const void *s390_todpreg, | |
10681 | int size) | |
10682 | { | |
10683 | char *note_name = "LINUX"; | |
10684 | return elfcore_write_note (abfd, buf, bufsiz, | |
10685 | note_name, NT_S390_TODPREG, s390_todpreg, size); | |
10686 | } | |
10687 | ||
10688 | char * | |
10689 | elfcore_write_s390_ctrs (bfd *abfd, | |
10690 | char *buf, | |
10691 | int *bufsiz, | |
10692 | const void *s390_ctrs, | |
10693 | int size) | |
10694 | { | |
10695 | char *note_name = "LINUX"; | |
10696 | return elfcore_write_note (abfd, buf, bufsiz, | |
10697 | note_name, NT_S390_CTRS, s390_ctrs, size); | |
10698 | } | |
10699 | ||
10700 | char * | |
10701 | elfcore_write_s390_prefix (bfd *abfd, | |
10702 | char *buf, | |
10703 | int *bufsiz, | |
10704 | const void *s390_prefix, | |
10705 | int size) | |
10706 | { | |
10707 | char *note_name = "LINUX"; | |
10708 | return elfcore_write_note (abfd, buf, bufsiz, | |
10709 | note_name, NT_S390_PREFIX, s390_prefix, size); | |
10710 | } | |
10711 | ||
355b81d9 UW |
10712 | char * |
10713 | elfcore_write_s390_last_break (bfd *abfd, | |
10714 | char *buf, | |
10715 | int *bufsiz, | |
10716 | const void *s390_last_break, | |
10717 | int size) | |
10718 | { | |
10719 | char *note_name = "LINUX"; | |
10720 | return elfcore_write_note (abfd, buf, bufsiz, | |
10721 | note_name, NT_S390_LAST_BREAK, | |
10722 | s390_last_break, size); | |
10723 | } | |
10724 | ||
10725 | char * | |
10726 | elfcore_write_s390_system_call (bfd *abfd, | |
10727 | char *buf, | |
10728 | int *bufsiz, | |
10729 | const void *s390_system_call, | |
10730 | int size) | |
10731 | { | |
10732 | char *note_name = "LINUX"; | |
10733 | return elfcore_write_note (abfd, buf, bufsiz, | |
10734 | note_name, NT_S390_SYSTEM_CALL, | |
10735 | s390_system_call, size); | |
10736 | } | |
10737 | ||
abb3f6cc NC |
10738 | char * |
10739 | elfcore_write_s390_tdb (bfd *abfd, | |
10740 | char *buf, | |
10741 | int *bufsiz, | |
10742 | const void *s390_tdb, | |
10743 | int size) | |
10744 | { | |
10745 | char *note_name = "LINUX"; | |
10746 | return elfcore_write_note (abfd, buf, bufsiz, | |
10747 | note_name, NT_S390_TDB, s390_tdb, size); | |
10748 | } | |
10749 | ||
4ef9f41a AA |
10750 | char * |
10751 | elfcore_write_s390_vxrs_low (bfd *abfd, | |
10752 | char *buf, | |
10753 | int *bufsiz, | |
10754 | const void *s390_vxrs_low, | |
10755 | int size) | |
10756 | { | |
10757 | char *note_name = "LINUX"; | |
10758 | return elfcore_write_note (abfd, buf, bufsiz, | |
10759 | note_name, NT_S390_VXRS_LOW, s390_vxrs_low, size); | |
10760 | } | |
10761 | ||
10762 | char * | |
10763 | elfcore_write_s390_vxrs_high (bfd *abfd, | |
10764 | char *buf, | |
10765 | int *bufsiz, | |
10766 | const void *s390_vxrs_high, | |
10767 | int size) | |
10768 | { | |
10769 | char *note_name = "LINUX"; | |
10770 | return elfcore_write_note (abfd, buf, bufsiz, | |
10771 | note_name, NT_S390_VXRS_HIGH, | |
10772 | s390_vxrs_high, size); | |
10773 | } | |
10774 | ||
88ab90e8 AA |
10775 | char * |
10776 | elfcore_write_s390_gs_cb (bfd *abfd, | |
10777 | char *buf, | |
10778 | int *bufsiz, | |
10779 | const void *s390_gs_cb, | |
10780 | int size) | |
10781 | { | |
10782 | char *note_name = "LINUX"; | |
10783 | return elfcore_write_note (abfd, buf, bufsiz, | |
10784 | note_name, NT_S390_GS_CB, | |
10785 | s390_gs_cb, size); | |
10786 | } | |
10787 | ||
10788 | char * | |
10789 | elfcore_write_s390_gs_bc (bfd *abfd, | |
10790 | char *buf, | |
10791 | int *bufsiz, | |
10792 | const void *s390_gs_bc, | |
10793 | int size) | |
10794 | { | |
10795 | char *note_name = "LINUX"; | |
10796 | return elfcore_write_note (abfd, buf, bufsiz, | |
10797 | note_name, NT_S390_GS_BC, | |
10798 | s390_gs_bc, size); | |
10799 | } | |
10800 | ||
faa9a424 UW |
10801 | char * |
10802 | elfcore_write_arm_vfp (bfd *abfd, | |
10803 | char *buf, | |
10804 | int *bufsiz, | |
10805 | const void *arm_vfp, | |
10806 | int size) | |
10807 | { | |
10808 | char *note_name = "LINUX"; | |
10809 | return elfcore_write_note (abfd, buf, bufsiz, | |
10810 | note_name, NT_ARM_VFP, arm_vfp, size); | |
10811 | } | |
10812 | ||
652451f8 YZ |
10813 | char * |
10814 | elfcore_write_aarch_tls (bfd *abfd, | |
10815 | char *buf, | |
10816 | int *bufsiz, | |
10817 | const void *aarch_tls, | |
10818 | int size) | |
10819 | { | |
10820 | char *note_name = "LINUX"; | |
10821 | return elfcore_write_note (abfd, buf, bufsiz, | |
10822 | note_name, NT_ARM_TLS, aarch_tls, size); | |
10823 | } | |
10824 | ||
10825 | char * | |
10826 | elfcore_write_aarch_hw_break (bfd *abfd, | |
10827 | char *buf, | |
10828 | int *bufsiz, | |
10829 | const void *aarch_hw_break, | |
10830 | int size) | |
10831 | { | |
10832 | char *note_name = "LINUX"; | |
10833 | return elfcore_write_note (abfd, buf, bufsiz, | |
10834 | note_name, NT_ARM_HW_BREAK, aarch_hw_break, size); | |
10835 | } | |
10836 | ||
10837 | char * | |
10838 | elfcore_write_aarch_hw_watch (bfd *abfd, | |
10839 | char *buf, | |
10840 | int *bufsiz, | |
10841 | const void *aarch_hw_watch, | |
10842 | int size) | |
10843 | { | |
10844 | char *note_name = "LINUX"; | |
10845 | return elfcore_write_note (abfd, buf, bufsiz, | |
10846 | note_name, NT_ARM_HW_WATCH, aarch_hw_watch, size); | |
10847 | } | |
10848 | ||
bb864ac1 CES |
10849 | char * |
10850 | elfcore_write_register_note (bfd *abfd, | |
10851 | char *buf, | |
10852 | int *bufsiz, | |
10853 | const char *section, | |
10854 | const void *data, | |
10855 | int size) | |
10856 | { | |
10857 | if (strcmp (section, ".reg2") == 0) | |
10858 | return elfcore_write_prfpreg (abfd, buf, bufsiz, data, size); | |
10859 | if (strcmp (section, ".reg-xfp") == 0) | |
10860 | return elfcore_write_prxfpreg (abfd, buf, bufsiz, data, size); | |
4339cae0 L |
10861 | if (strcmp (section, ".reg-xstate") == 0) |
10862 | return elfcore_write_xstatereg (abfd, buf, bufsiz, data, size); | |
bb864ac1 CES |
10863 | if (strcmp (section, ".reg-ppc-vmx") == 0) |
10864 | return elfcore_write_ppc_vmx (abfd, buf, bufsiz, data, size); | |
89eeb0bc LM |
10865 | if (strcmp (section, ".reg-ppc-vsx") == 0) |
10866 | return elfcore_write_ppc_vsx (abfd, buf, bufsiz, data, size); | |
0675e188 UW |
10867 | if (strcmp (section, ".reg-s390-high-gprs") == 0) |
10868 | return elfcore_write_s390_high_gprs (abfd, buf, bufsiz, data, size); | |
d7eeb400 MS |
10869 | if (strcmp (section, ".reg-s390-timer") == 0) |
10870 | return elfcore_write_s390_timer (abfd, buf, bufsiz, data, size); | |
10871 | if (strcmp (section, ".reg-s390-todcmp") == 0) | |
10872 | return elfcore_write_s390_todcmp (abfd, buf, bufsiz, data, size); | |
10873 | if (strcmp (section, ".reg-s390-todpreg") == 0) | |
10874 | return elfcore_write_s390_todpreg (abfd, buf, bufsiz, data, size); | |
10875 | if (strcmp (section, ".reg-s390-ctrs") == 0) | |
10876 | return elfcore_write_s390_ctrs (abfd, buf, bufsiz, data, size); | |
10877 | if (strcmp (section, ".reg-s390-prefix") == 0) | |
10878 | return elfcore_write_s390_prefix (abfd, buf, bufsiz, data, size); | |
355b81d9 UW |
10879 | if (strcmp (section, ".reg-s390-last-break") == 0) |
10880 | return elfcore_write_s390_last_break (abfd, buf, bufsiz, data, size); | |
10881 | if (strcmp (section, ".reg-s390-system-call") == 0) | |
10882 | return elfcore_write_s390_system_call (abfd, buf, bufsiz, data, size); | |
abb3f6cc NC |
10883 | if (strcmp (section, ".reg-s390-tdb") == 0) |
10884 | return elfcore_write_s390_tdb (abfd, buf, bufsiz, data, size); | |
4ef9f41a AA |
10885 | if (strcmp (section, ".reg-s390-vxrs-low") == 0) |
10886 | return elfcore_write_s390_vxrs_low (abfd, buf, bufsiz, data, size); | |
10887 | if (strcmp (section, ".reg-s390-vxrs-high") == 0) | |
10888 | return elfcore_write_s390_vxrs_high (abfd, buf, bufsiz, data, size); | |
88ab90e8 AA |
10889 | if (strcmp (section, ".reg-s390-gs-cb") == 0) |
10890 | return elfcore_write_s390_gs_cb (abfd, buf, bufsiz, data, size); | |
10891 | if (strcmp (section, ".reg-s390-gs-bc") == 0) | |
10892 | return elfcore_write_s390_gs_bc (abfd, buf, bufsiz, data, size); | |
faa9a424 UW |
10893 | if (strcmp (section, ".reg-arm-vfp") == 0) |
10894 | return elfcore_write_arm_vfp (abfd, buf, bufsiz, data, size); | |
652451f8 YZ |
10895 | if (strcmp (section, ".reg-aarch-tls") == 0) |
10896 | return elfcore_write_aarch_tls (abfd, buf, bufsiz, data, size); | |
10897 | if (strcmp (section, ".reg-aarch-hw-break") == 0) | |
10898 | return elfcore_write_aarch_hw_break (abfd, buf, bufsiz, data, size); | |
10899 | if (strcmp (section, ".reg-aarch-hw-watch") == 0) | |
10900 | return elfcore_write_aarch_hw_watch (abfd, buf, bufsiz, data, size); | |
bb864ac1 CES |
10901 | return NULL; |
10902 | } | |
10903 | ||
b34976b6 | 10904 | static bfd_boolean |
718175fa | 10905 | elf_parse_notes (bfd *abfd, char *buf, size_t size, file_ptr offset) |
252b5132 | 10906 | { |
c044fabd | 10907 | char *p; |
252b5132 | 10908 | |
252b5132 RH |
10909 | p = buf; |
10910 | while (p < buf + size) | |
10911 | { | |
c044fabd KH |
10912 | /* FIXME: bad alignment assumption. */ |
10913 | Elf_External_Note *xnp = (Elf_External_Note *) p; | |
252b5132 RH |
10914 | Elf_Internal_Note in; |
10915 | ||
baea7ef1 AM |
10916 | if (offsetof (Elf_External_Note, name) > buf - p + size) |
10917 | return FALSE; | |
10918 | ||
dc810e39 | 10919 | in.type = H_GET_32 (abfd, xnp->type); |
252b5132 | 10920 | |
dc810e39 | 10921 | in.namesz = H_GET_32 (abfd, xnp->namesz); |
252b5132 | 10922 | in.namedata = xnp->name; |
baea7ef1 AM |
10923 | if (in.namesz > buf - in.namedata + size) |
10924 | return FALSE; | |
252b5132 | 10925 | |
dc810e39 | 10926 | in.descsz = H_GET_32 (abfd, xnp->descsz); |
252b5132 RH |
10927 | in.descdata = in.namedata + BFD_ALIGN (in.namesz, 4); |
10928 | in.descpos = offset + (in.descdata - buf); | |
baea7ef1 AM |
10929 | if (in.descsz != 0 |
10930 | && (in.descdata >= buf + size | |
10931 | || in.descsz > buf - in.descdata + size)) | |
10932 | return FALSE; | |
252b5132 | 10933 | |
718175fa JK |
10934 | switch (bfd_get_format (abfd)) |
10935 | { | |
10936 | default: | |
10937 | return TRUE; | |
10938 | ||
10939 | case bfd_core: | |
f64e188b | 10940 | { |
8acbedd6 | 10941 | #define GROKER_ELEMENT(S,F) {S, sizeof (S) - 1, F} |
f64e188b | 10942 | struct |
718175fa | 10943 | { |
f64e188b | 10944 | const char * string; |
8acbedd6 | 10945 | size_t len; |
f64e188b | 10946 | bfd_boolean (* func)(bfd *, Elf_Internal_Note *); |
718175fa | 10947 | } |
f64e188b | 10948 | grokers[] = |
b15fa79e | 10949 | { |
8acbedd6 | 10950 | GROKER_ELEMENT ("", elfcore_grok_note), |
aa1ed4a9 | 10951 | GROKER_ELEMENT ("FreeBSD", elfcore_grok_freebsd_note), |
8acbedd6 KS |
10952 | GROKER_ELEMENT ("NetBSD-CORE", elfcore_grok_netbsd_note), |
10953 | GROKER_ELEMENT ( "OpenBSD", elfcore_grok_openbsd_note), | |
10954 | GROKER_ELEMENT ("QNX", elfcore_grok_nto_note), | |
10955 | GROKER_ELEMENT ("SPU/", elfcore_grok_spu_note) | |
f64e188b | 10956 | }; |
8acbedd6 | 10957 | #undef GROKER_ELEMENT |
f64e188b NC |
10958 | int i; |
10959 | ||
10960 | for (i = ARRAY_SIZE (grokers); i--;) | |
8acbedd6 KS |
10961 | { |
10962 | if (in.namesz >= grokers[i].len | |
10963 | && strncmp (in.namedata, grokers[i].string, | |
10964 | grokers[i].len) == 0) | |
10965 | { | |
10966 | if (! grokers[i].func (abfd, & in)) | |
10967 | return FALSE; | |
10968 | break; | |
10969 | } | |
10970 | } | |
f64e188b NC |
10971 | break; |
10972 | } | |
718175fa JK |
10973 | |
10974 | case bfd_object: | |
10975 | if (in.namesz == sizeof "GNU" && strcmp (in.namedata, "GNU") == 0) | |
10976 | { | |
10977 | if (! elfobj_grok_gnu_note (abfd, &in)) | |
10978 | return FALSE; | |
10979 | } | |
e21e5835 NC |
10980 | else if (in.namesz == sizeof "stapsdt" |
10981 | && strcmp (in.namedata, "stapsdt") == 0) | |
10982 | { | |
10983 | if (! elfobj_grok_stapsdt_note (abfd, &in)) | |
10984 | return FALSE; | |
10985 | } | |
718175fa | 10986 | break; |
08a40648 | 10987 | } |
252b5132 RH |
10988 | |
10989 | p = in.descdata + BFD_ALIGN (in.descsz, 4); | |
10990 | } | |
10991 | ||
718175fa JK |
10992 | return TRUE; |
10993 | } | |
10994 | ||
10995 | static bfd_boolean | |
10996 | elf_read_notes (bfd *abfd, file_ptr offset, bfd_size_type size) | |
10997 | { | |
10998 | char *buf; | |
10999 | ||
11000 | if (size <= 0) | |
11001 | return TRUE; | |
11002 | ||
11003 | if (bfd_seek (abfd, offset, SEEK_SET) != 0) | |
11004 | return FALSE; | |
11005 | ||
f64e188b | 11006 | buf = (char *) bfd_malloc (size + 1); |
718175fa JK |
11007 | if (buf == NULL) |
11008 | return FALSE; | |
11009 | ||
f64e188b NC |
11010 | /* PR 17512: file: ec08f814 |
11011 | 0-termintate the buffer so that string searches will not overflow. */ | |
11012 | buf[size] = 0; | |
11013 | ||
718175fa JK |
11014 | if (bfd_bread (buf, size, abfd) != size |
11015 | || !elf_parse_notes (abfd, buf, size, offset)) | |
11016 | { | |
11017 | free (buf); | |
11018 | return FALSE; | |
11019 | } | |
11020 | ||
252b5132 | 11021 | free (buf); |
b34976b6 | 11022 | return TRUE; |
252b5132 | 11023 | } |
98d8431c JB |
11024 | \f |
11025 | /* Providing external access to the ELF program header table. */ | |
11026 | ||
11027 | /* Return an upper bound on the number of bytes required to store a | |
11028 | copy of ABFD's program header table entries. Return -1 if an error | |
11029 | occurs; bfd_get_error will return an appropriate code. */ | |
c044fabd | 11030 | |
98d8431c | 11031 | long |
217aa764 | 11032 | bfd_get_elf_phdr_upper_bound (bfd *abfd) |
98d8431c JB |
11033 | { |
11034 | if (abfd->xvec->flavour != bfd_target_elf_flavour) | |
11035 | { | |
11036 | bfd_set_error (bfd_error_wrong_format); | |
11037 | return -1; | |
11038 | } | |
11039 | ||
936e320b | 11040 | return elf_elfheader (abfd)->e_phnum * sizeof (Elf_Internal_Phdr); |
98d8431c JB |
11041 | } |
11042 | ||
98d8431c JB |
11043 | /* Copy ABFD's program header table entries to *PHDRS. The entries |
11044 | will be stored as an array of Elf_Internal_Phdr structures, as | |
11045 | defined in include/elf/internal.h. To find out how large the | |
11046 | buffer needs to be, call bfd_get_elf_phdr_upper_bound. | |
11047 | ||
11048 | Return the number of program header table entries read, or -1 if an | |
11049 | error occurs; bfd_get_error will return an appropriate code. */ | |
c044fabd | 11050 | |
98d8431c | 11051 | int |
217aa764 | 11052 | bfd_get_elf_phdrs (bfd *abfd, void *phdrs) |
98d8431c JB |
11053 | { |
11054 | int num_phdrs; | |
11055 | ||
11056 | if (abfd->xvec->flavour != bfd_target_elf_flavour) | |
11057 | { | |
11058 | bfd_set_error (bfd_error_wrong_format); | |
11059 | return -1; | |
11060 | } | |
11061 | ||
11062 | num_phdrs = elf_elfheader (abfd)->e_phnum; | |
c044fabd | 11063 | memcpy (phdrs, elf_tdata (abfd)->phdr, |
98d8431c JB |
11064 | num_phdrs * sizeof (Elf_Internal_Phdr)); |
11065 | ||
11066 | return num_phdrs; | |
11067 | } | |
ae4221d7 | 11068 | |
db6751f2 | 11069 | enum elf_reloc_type_class |
7e612e98 AM |
11070 | _bfd_elf_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED, |
11071 | const asection *rel_sec ATTRIBUTE_UNUSED, | |
11072 | const Elf_Internal_Rela *rela ATTRIBUTE_UNUSED) | |
db6751f2 JJ |
11073 | { |
11074 | return reloc_class_normal; | |
11075 | } | |
f8df10f4 | 11076 | |
47d9a591 | 11077 | /* For RELA architectures, return the relocation value for a |
f8df10f4 JJ |
11078 | relocation against a local symbol. */ |
11079 | ||
11080 | bfd_vma | |
217aa764 AM |
11081 | _bfd_elf_rela_local_sym (bfd *abfd, |
11082 | Elf_Internal_Sym *sym, | |
8517fae7 | 11083 | asection **psec, |
217aa764 | 11084 | Elf_Internal_Rela *rel) |
f8df10f4 | 11085 | { |
8517fae7 | 11086 | asection *sec = *psec; |
f8df10f4 JJ |
11087 | bfd_vma relocation; |
11088 | ||
11089 | relocation = (sec->output_section->vma | |
11090 | + sec->output_offset | |
11091 | + sym->st_value); | |
11092 | if ((sec->flags & SEC_MERGE) | |
c629eae0 | 11093 | && ELF_ST_TYPE (sym->st_info) == STT_SECTION |
dbaa2011 | 11094 | && sec->sec_info_type == SEC_INFO_TYPE_MERGE) |
f8df10f4 | 11095 | { |
f8df10f4 | 11096 | rel->r_addend = |
8517fae7 | 11097 | _bfd_merged_section_offset (abfd, psec, |
65765700 | 11098 | elf_section_data (sec)->sec_info, |
753731ee AM |
11099 | sym->st_value + rel->r_addend); |
11100 | if (sec != *psec) | |
11101 | { | |
11102 | /* If we have changed the section, and our original section is | |
11103 | marked with SEC_EXCLUDE, it means that the original | |
11104 | SEC_MERGE section has been completely subsumed in some | |
11105 | other SEC_MERGE section. In this case, we need to leave | |
11106 | some info around for --emit-relocs. */ | |
11107 | if ((sec->flags & SEC_EXCLUDE) != 0) | |
11108 | sec->kept_section = *psec; | |
11109 | sec = *psec; | |
11110 | } | |
8517fae7 AM |
11111 | rel->r_addend -= relocation; |
11112 | rel->r_addend += sec->output_section->vma + sec->output_offset; | |
f8df10f4 JJ |
11113 | } |
11114 | return relocation; | |
11115 | } | |
c629eae0 JJ |
11116 | |
11117 | bfd_vma | |
217aa764 AM |
11118 | _bfd_elf_rel_local_sym (bfd *abfd, |
11119 | Elf_Internal_Sym *sym, | |
11120 | asection **psec, | |
11121 | bfd_vma addend) | |
47d9a591 | 11122 | { |
c629eae0 JJ |
11123 | asection *sec = *psec; |
11124 | ||
dbaa2011 | 11125 | if (sec->sec_info_type != SEC_INFO_TYPE_MERGE) |
c629eae0 JJ |
11126 | return sym->st_value + addend; |
11127 | ||
11128 | return _bfd_merged_section_offset (abfd, psec, | |
65765700 | 11129 | elf_section_data (sec)->sec_info, |
753731ee | 11130 | sym->st_value + addend); |
c629eae0 JJ |
11131 | } |
11132 | ||
37b01f6a DG |
11133 | /* Adjust an address within a section. Given OFFSET within SEC, return |
11134 | the new offset within the section, based upon changes made to the | |
11135 | section. Returns -1 if the offset is now invalid. | |
11136 | The offset (in abnd out) is in target sized bytes, however big a | |
11137 | byte may be. */ | |
11138 | ||
c629eae0 | 11139 | bfd_vma |
217aa764 | 11140 | _bfd_elf_section_offset (bfd *abfd, |
92e4ec35 | 11141 | struct bfd_link_info *info, |
217aa764 AM |
11142 | asection *sec, |
11143 | bfd_vma offset) | |
c629eae0 | 11144 | { |
68bfbfcc | 11145 | switch (sec->sec_info_type) |
65765700 | 11146 | { |
dbaa2011 | 11147 | case SEC_INFO_TYPE_STABS: |
eea6121a AM |
11148 | return _bfd_stab_section_offset (sec, elf_section_data (sec)->sec_info, |
11149 | offset); | |
dbaa2011 | 11150 | case SEC_INFO_TYPE_EH_FRAME: |
92e4ec35 | 11151 | return _bfd_elf_eh_frame_section_offset (abfd, info, sec, offset); |
37b01f6a | 11152 | |
65765700 | 11153 | default: |
310fd250 L |
11154 | if ((sec->flags & SEC_ELF_REVERSE_COPY) != 0) |
11155 | { | |
37b01f6a | 11156 | /* Reverse the offset. */ |
310fd250 L |
11157 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
11158 | bfd_size_type address_size = bed->s->arch_size / 8; | |
37b01f6a DG |
11159 | |
11160 | /* address_size and sec->size are in octets. Convert | |
11161 | to bytes before subtracting the original offset. */ | |
11162 | offset = (sec->size - address_size) / bfd_octets_per_byte (abfd) - offset; | |
310fd250 | 11163 | } |
65765700 JJ |
11164 | return offset; |
11165 | } | |
c629eae0 | 11166 | } |
3333a7c3 RM |
11167 | \f |
11168 | /* Create a new BFD as if by bfd_openr. Rather than opening a file, | |
11169 | reconstruct an ELF file by reading the segments out of remote memory | |
11170 | based on the ELF file header at EHDR_VMA and the ELF program headers it | |
11171 | points to. If not null, *LOADBASEP is filled in with the difference | |
11172 | between the VMAs from which the segments were read, and the VMAs the | |
11173 | file headers (and hence BFD's idea of each section's VMA) put them at. | |
11174 | ||
11175 | The function TARGET_READ_MEMORY is called to copy LEN bytes from the | |
11176 | remote memory at target address VMA into the local buffer at MYADDR; it | |
11177 | should return zero on success or an `errno' code on failure. TEMPL must | |
11178 | be a BFD for an ELF target with the word size and byte order found in | |
11179 | the remote memory. */ | |
11180 | ||
11181 | bfd * | |
217aa764 AM |
11182 | bfd_elf_bfd_from_remote_memory |
11183 | (bfd *templ, | |
11184 | bfd_vma ehdr_vma, | |
f0a5d95a | 11185 | bfd_size_type size, |
217aa764 | 11186 | bfd_vma *loadbasep, |
fe78531d | 11187 | int (*target_read_memory) (bfd_vma, bfd_byte *, bfd_size_type)) |
3333a7c3 RM |
11188 | { |
11189 | return (*get_elf_backend_data (templ)->elf_backend_bfd_from_remote_memory) | |
5979d6b6 | 11190 | (templ, ehdr_vma, size, loadbasep, target_read_memory); |
3333a7c3 | 11191 | } |
4c45e5c9 JJ |
11192 | \f |
11193 | long | |
c9727e01 AM |
11194 | _bfd_elf_get_synthetic_symtab (bfd *abfd, |
11195 | long symcount ATTRIBUTE_UNUSED, | |
11196 | asymbol **syms ATTRIBUTE_UNUSED, | |
8615f3f2 | 11197 | long dynsymcount, |
c9727e01 AM |
11198 | asymbol **dynsyms, |
11199 | asymbol **ret) | |
4c45e5c9 JJ |
11200 | { |
11201 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
11202 | asection *relplt; | |
11203 | asymbol *s; | |
11204 | const char *relplt_name; | |
11205 | bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean); | |
11206 | arelent *p; | |
11207 | long count, i, n; | |
11208 | size_t size; | |
11209 | Elf_Internal_Shdr *hdr; | |
11210 | char *names; | |
11211 | asection *plt; | |
11212 | ||
8615f3f2 AM |
11213 | *ret = NULL; |
11214 | ||
90e3cdf2 JJ |
11215 | if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0) |
11216 | return 0; | |
11217 | ||
8615f3f2 AM |
11218 | if (dynsymcount <= 0) |
11219 | return 0; | |
11220 | ||
4c45e5c9 JJ |
11221 | if (!bed->plt_sym_val) |
11222 | return 0; | |
11223 | ||
11224 | relplt_name = bed->relplt_name; | |
11225 | if (relplt_name == NULL) | |
d35fd659 | 11226 | relplt_name = bed->rela_plts_and_copies_p ? ".rela.plt" : ".rel.plt"; |
4c45e5c9 JJ |
11227 | relplt = bfd_get_section_by_name (abfd, relplt_name); |
11228 | if (relplt == NULL) | |
11229 | return 0; | |
11230 | ||
11231 | hdr = &elf_section_data (relplt)->this_hdr; | |
11232 | if (hdr->sh_link != elf_dynsymtab (abfd) | |
11233 | || (hdr->sh_type != SHT_REL && hdr->sh_type != SHT_RELA)) | |
11234 | return 0; | |
11235 | ||
11236 | plt = bfd_get_section_by_name (abfd, ".plt"); | |
11237 | if (plt == NULL) | |
11238 | return 0; | |
11239 | ||
11240 | slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table; | |
c9727e01 | 11241 | if (! (*slurp_relocs) (abfd, relplt, dynsyms, TRUE)) |
4c45e5c9 JJ |
11242 | return -1; |
11243 | ||
eea6121a | 11244 | count = relplt->size / hdr->sh_entsize; |
4c45e5c9 JJ |
11245 | size = count * sizeof (asymbol); |
11246 | p = relplt->relocation; | |
cb53bf42 | 11247 | for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel) |
041de40d AM |
11248 | { |
11249 | size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt"); | |
11250 | if (p->addend != 0) | |
11251 | { | |
11252 | #ifdef BFD64 | |
11253 | size += sizeof ("+0x") - 1 + 8 + 8 * (bed->s->elfclass == ELFCLASS64); | |
11254 | #else | |
11255 | size += sizeof ("+0x") - 1 + 8; | |
11256 | #endif | |
11257 | } | |
11258 | } | |
4c45e5c9 | 11259 | |
a50b1753 | 11260 | s = *ret = (asymbol *) bfd_malloc (size); |
4c45e5c9 JJ |
11261 | if (s == NULL) |
11262 | return -1; | |
11263 | ||
11264 | names = (char *) (s + count); | |
11265 | p = relplt->relocation; | |
11266 | n = 0; | |
cb53bf42 | 11267 | for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel) |
4c45e5c9 JJ |
11268 | { |
11269 | size_t len; | |
11270 | bfd_vma addr; | |
11271 | ||
11272 | addr = bed->plt_sym_val (i, plt, p); | |
11273 | if (addr == (bfd_vma) -1) | |
11274 | continue; | |
11275 | ||
11276 | *s = **p->sym_ptr_ptr; | |
65a7a66f AM |
11277 | /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since |
11278 | we are defining a symbol, ensure one of them is set. */ | |
11279 | if ((s->flags & BSF_LOCAL) == 0) | |
11280 | s->flags |= BSF_GLOBAL; | |
6ba2a415 | 11281 | s->flags |= BSF_SYNTHETIC; |
4c45e5c9 JJ |
11282 | s->section = plt; |
11283 | s->value = addr - plt->vma; | |
11284 | s->name = names; | |
8f39ba8e | 11285 | s->udata.p = NULL; |
4c45e5c9 JJ |
11286 | len = strlen ((*p->sym_ptr_ptr)->name); |
11287 | memcpy (names, (*p->sym_ptr_ptr)->name, len); | |
11288 | names += len; | |
041de40d AM |
11289 | if (p->addend != 0) |
11290 | { | |
1d770845 | 11291 | char buf[30], *a; |
d324f6d6 | 11292 | |
041de40d AM |
11293 | memcpy (names, "+0x", sizeof ("+0x") - 1); |
11294 | names += sizeof ("+0x") - 1; | |
1d770845 L |
11295 | bfd_sprintf_vma (abfd, buf, p->addend); |
11296 | for (a = buf; *a == '0'; ++a) | |
11297 | ; | |
11298 | len = strlen (a); | |
11299 | memcpy (names, a, len); | |
11300 | names += len; | |
041de40d | 11301 | } |
4c45e5c9 JJ |
11302 | memcpy (names, "@plt", sizeof ("@plt")); |
11303 | names += sizeof ("@plt"); | |
8f39ba8e | 11304 | ++s, ++n; |
4c45e5c9 JJ |
11305 | } |
11306 | ||
11307 | return n; | |
11308 | } | |
3d7f7666 | 11309 | |
821e6ff6 AM |
11310 | /* It is only used by x86-64 so far. |
11311 | ??? This repeats *COM* id of zero. sec->id is supposed to be unique, | |
7eacd66b AM |
11312 | but current usage would allow all of _bfd_std_section to be zero. */ |
11313 | static const asymbol lcomm_sym | |
11314 | = GLOBAL_SYM_INIT ("LARGE_COMMON", &_bfd_elf_large_com_section); | |
3b22753a | 11315 | asection _bfd_elf_large_com_section |
7eacd66b | 11316 | = BFD_FAKE_SECTION (_bfd_elf_large_com_section, &lcomm_sym, |
821e6ff6 | 11317 | "LARGE_COMMON", 0, SEC_IS_COMMON); |
ecca9871 | 11318 | |
d1036acb | 11319 | void |
78245035 L |
11320 | _bfd_elf_post_process_headers (bfd * abfd, |
11321 | struct bfd_link_info * link_info ATTRIBUTE_UNUSED) | |
d1036acb L |
11322 | { |
11323 | Elf_Internal_Ehdr * i_ehdrp; /* ELF file header, internal form. */ | |
11324 | ||
11325 | i_ehdrp = elf_elfheader (abfd); | |
11326 | ||
11327 | i_ehdrp->e_ident[EI_OSABI] = get_elf_backend_data (abfd)->elf_osabi; | |
d8045f23 NC |
11328 | |
11329 | /* To make things simpler for the loader on Linux systems we set the | |
9c55345c | 11330 | osabi field to ELFOSABI_GNU if the binary contains symbols of |
f64b2e8d | 11331 | the STT_GNU_IFUNC type or STB_GNU_UNIQUE binding. */ |
d8045f23 | 11332 | if (i_ehdrp->e_ident[EI_OSABI] == ELFOSABI_NONE |
f64b2e8d | 11333 | && elf_tdata (abfd)->has_gnu_symbols) |
9c55345c | 11334 | i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_GNU; |
d1036acb | 11335 | } |
fcb93ecf PB |
11336 | |
11337 | ||
11338 | /* Return TRUE for ELF symbol types that represent functions. | |
11339 | This is the default version of this function, which is sufficient for | |
d8045f23 | 11340 | most targets. It returns true if TYPE is STT_FUNC or STT_GNU_IFUNC. */ |
fcb93ecf PB |
11341 | |
11342 | bfd_boolean | |
11343 | _bfd_elf_is_function_type (unsigned int type) | |
11344 | { | |
d8045f23 NC |
11345 | return (type == STT_FUNC |
11346 | || type == STT_GNU_IFUNC); | |
fcb93ecf | 11347 | } |
9f296da3 | 11348 | |
aef36ac1 AM |
11349 | /* If the ELF symbol SYM might be a function in SEC, return the |
11350 | function size and set *CODE_OFF to the function's entry point, | |
11351 | otherwise return zero. */ | |
9f296da3 | 11352 | |
aef36ac1 AM |
11353 | bfd_size_type |
11354 | _bfd_elf_maybe_function_sym (const asymbol *sym, asection *sec, | |
11355 | bfd_vma *code_off) | |
9f296da3 | 11356 | { |
aef36ac1 AM |
11357 | bfd_size_type size; |
11358 | ||
ff9e0f5b | 11359 | if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT |
aef36ac1 AM |
11360 | | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0 |
11361 | || sym->section != sec) | |
11362 | return 0; | |
ff9e0f5b | 11363 | |
ff9e0f5b | 11364 | *code_off = sym->value; |
aef36ac1 AM |
11365 | size = 0; |
11366 | if (!(sym->flags & BSF_SYNTHETIC)) | |
11367 | size = ((elf_symbol_type *) sym)->internal_elf_sym.st_size; | |
11368 | if (size == 0) | |
11369 | size = 1; | |
11370 | return size; | |
9f296da3 | 11371 | } |